2 * Copyright 2002 Lionel Ulmer
3 * Copyright 2002-2005 Jason Edmeades
4 * Copyright 2003-2004 Raphael Junqueira
5 * Copyright 2004 Christian Costa
6 * Copyright 2005 Oliver Stieber
7 * Copyright 2006-2008 Stefan Dösinger for CodeWeavers
8 * Copyright 2006-2008 Henri Verbeet
9 * Copyright 2007 Andrew Riedi
10 * Copyright 2009-2011 Henri Verbeet for CodeWeavers
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public
14 * License as published by the Free Software Foundation; either
15 * version 2.1 of the License, or (at your option) any later version.
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
28 #include "wine/port.h"
35 #include "wined3d_private.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
39 /* Define the default light parameters as specified by MSDN. */
40 const struct wined3d_light WINED3D_default_light =
42 WINED3D_LIGHT_DIRECTIONAL, /* Type */
43 { 1.0f, 1.0f, 1.0f, 0.0f }, /* Diffuse r,g,b,a */
44 { 0.0f, 0.0f, 0.0f, 0.0f }, /* Specular r,g,b,a */
45 { 0.0f, 0.0f, 0.0f, 0.0f }, /* Ambient r,g,b,a, */
46 { 0.0f, 0.0f, 0.0f }, /* Position x,y,z */
47 { 0.0f, 0.0f, 1.0f }, /* Direction x,y,z */
50 0.0f, 0.0f, 0.0f, /* Attenuation 0,1,2 */
55 /**********************************************************
56 * Global variable / Constants follow
57 **********************************************************/
58 const struct wined3d_matrix identity =
60 1.0f, 0.0f, 0.0f, 0.0f,
61 0.0f, 1.0f, 0.0f, 0.0f,
62 0.0f, 0.0f, 1.0f, 0.0f,
63 0.0f, 0.0f, 0.0f, 1.0f,
64 }}}; /* When needed for comparisons */
66 /* Note that except for WINED3DPT_POINTLIST and WINED3DPT_LINELIST these
67 * actually have the same values in GL and D3D. */
68 GLenum gl_primitive_type_from_d3d(enum wined3d_primitive_type primitive_type)
70 switch(primitive_type)
72 case WINED3D_PT_POINTLIST:
75 case WINED3D_PT_LINELIST:
78 case WINED3D_PT_LINESTRIP:
81 case WINED3D_PT_TRIANGLELIST:
84 case WINED3D_PT_TRIANGLESTRIP:
85 return GL_TRIANGLE_STRIP;
87 case WINED3D_PT_TRIANGLEFAN:
88 return GL_TRIANGLE_FAN;
90 case WINED3D_PT_LINELIST_ADJ:
91 return GL_LINES_ADJACENCY_ARB;
93 case WINED3D_PT_LINESTRIP_ADJ:
94 return GL_LINE_STRIP_ADJACENCY_ARB;
96 case WINED3D_PT_TRIANGLELIST_ADJ:
97 return GL_TRIANGLES_ADJACENCY_ARB;
99 case WINED3D_PT_TRIANGLESTRIP_ADJ:
100 return GL_TRIANGLE_STRIP_ADJACENCY_ARB;
103 FIXME("Unhandled primitive type %s\n", debug_d3dprimitivetype(primitive_type));
108 static enum wined3d_primitive_type d3d_primitive_type_from_gl(GLenum primitive_type)
110 switch(primitive_type)
113 return WINED3D_PT_POINTLIST;
116 return WINED3D_PT_LINELIST;
119 return WINED3D_PT_LINESTRIP;
122 return WINED3D_PT_TRIANGLELIST;
124 case GL_TRIANGLE_STRIP:
125 return WINED3D_PT_TRIANGLESTRIP;
127 case GL_TRIANGLE_FAN:
128 return WINED3D_PT_TRIANGLEFAN;
130 case GL_LINES_ADJACENCY_ARB:
131 return WINED3D_PT_LINELIST_ADJ;
133 case GL_LINE_STRIP_ADJACENCY_ARB:
134 return WINED3D_PT_LINESTRIP_ADJ;
136 case GL_TRIANGLES_ADJACENCY_ARB:
137 return WINED3D_PT_TRIANGLELIST_ADJ;
139 case GL_TRIANGLE_STRIP_ADJACENCY_ARB:
140 return WINED3D_PT_TRIANGLESTRIP_ADJ;
143 FIXME("Unhandled primitive type %s\n", debug_d3dprimitivetype(primitive_type));
144 return WINED3D_PT_UNDEFINED;
148 static BOOL fixed_get_input(BYTE usage, BYTE usage_idx, unsigned int *regnum)
150 if ((usage == WINED3D_DECL_USAGE_POSITION || usage == WINED3D_DECL_USAGE_POSITIONT) && !usage_idx)
151 *regnum = WINED3D_FFP_POSITION;
152 else if (usage == WINED3D_DECL_USAGE_BLEND_WEIGHT && !usage_idx)
153 *regnum = WINED3D_FFP_BLENDWEIGHT;
154 else if (usage == WINED3D_DECL_USAGE_BLEND_INDICES && !usage_idx)
155 *regnum = WINED3D_FFP_BLENDINDICES;
156 else if (usage == WINED3D_DECL_USAGE_NORMAL && !usage_idx)
157 *regnum = WINED3D_FFP_NORMAL;
158 else if (usage == WINED3D_DECL_USAGE_PSIZE && !usage_idx)
159 *regnum = WINED3D_FFP_PSIZE;
160 else if (usage == WINED3D_DECL_USAGE_COLOR && !usage_idx)
161 *regnum = WINED3D_FFP_DIFFUSE;
162 else if (usage == WINED3D_DECL_USAGE_COLOR && usage_idx == 1)
163 *regnum = WINED3D_FFP_SPECULAR;
164 else if (usage == WINED3D_DECL_USAGE_TEXCOORD && usage_idx < WINED3DDP_MAXTEXCOORD)
165 *regnum = WINED3D_FFP_TEXCOORD0 + usage_idx;
168 FIXME("Unsupported input stream [usage=%s, usage_idx=%u]\n", debug_d3ddeclusage(usage), usage_idx);
176 /* Context activation is done by the caller. */
177 void device_stream_info_from_declaration(struct wined3d_device *device, struct wined3d_stream_info *stream_info)
179 const struct wined3d_state *state = &device->stateBlock->state;
180 /* We need to deal with frequency data! */
181 struct wined3d_vertex_declaration *declaration = state->vertex_declaration;
186 stream_info->use_map = 0;
187 stream_info->swizzle_map = 0;
188 stream_info->all_vbo = 1;
190 /* Check for transformed vertices, disable vertex shader if present. */
191 stream_info->position_transformed = declaration->position_transformed;
192 use_vshader = state->vertex_shader && !declaration->position_transformed;
194 /* Translate the declaration into strided data. */
195 for (i = 0; i < declaration->element_count; ++i)
197 const struct wined3d_vertex_declaration_element *element = &declaration->elements[i];
198 const struct wined3d_stream_state *stream = &state->streams[element->input_slot];
199 struct wined3d_buffer *buffer = stream->buffer;
200 struct wined3d_bo_address data;
205 TRACE("%p Element %p (%u of %u)\n", declaration->elements,
206 element, i + 1, declaration->element_count);
208 if (!buffer) continue;
210 stride = stream->stride;
212 TRACE("Stream %u, buffer %p.\n", element->input_slot, buffer);
213 buffer_get_memory(buffer, &device->adapter->gl_info, &data);
215 /* We can't use VBOs if the base vertex index is negative. OpenGL
216 * doesn't accept negative offsets (or rather offsets bigger than the
217 * VBO, because the pointer is unsigned), so use system memory
218 * sources. In most sane cases the pointer - offset will still be > 0,
219 * otherwise it will wrap around to some big value. Hope that with the
220 * indices, the driver wraps it back internally. If not,
221 * drawStridedSlow() is needed, including a vertex buffer path. */
222 if (state->load_base_vertex_index < 0)
224 WARN("load_base_vertex_index is < 0 (%d), not using VBOs.\n", state->load_base_vertex_index);
225 data.buffer_object = 0;
226 data.addr = buffer_get_sysmem(buffer, &device->adapter->gl_info);
227 if ((UINT_PTR)data.addr < -state->load_base_vertex_index * stride)
228 FIXME("System memory vertex data load offset is negative!\n");
230 data.addr += element->offset;
232 TRACE("offset %u input_slot %u usage_idx %d\n", element->offset, element->input_slot, element->usage_idx);
236 if (element->output_slot == ~0U)
238 /* TODO: Assuming vertexdeclarations are usually used with the
239 * same or a similar shader, it might be worth it to store the
240 * last used output slot and try that one first. */
241 stride_used = vshader_get_input(state->vertex_shader,
242 element->usage, element->usage_idx, &idx);
246 idx = element->output_slot;
252 if (!element->ffp_valid)
254 WARN("Skipping unsupported fixed function element of format %s and usage %s\n",
255 debug_d3dformat(element->format->id), debug_d3ddeclusage(element->usage));
260 stride_used = fixed_get_input(element->usage, element->usage_idx, &idx);
266 TRACE("Load %s array %u [usage %s, usage_idx %u, "
267 "input_slot %u, offset %u, stride %u, format %s, buffer_object %u]\n",
268 use_vshader ? "shader": "fixed function", idx,
269 debug_d3ddeclusage(element->usage), element->usage_idx, element->input_slot,
270 element->offset, stride, debug_d3dformat(element->format->id), data.buffer_object);
272 data.addr += stream->offset;
274 stream_info->elements[idx].format = element->format;
275 stream_info->elements[idx].data = data;
276 stream_info->elements[idx].stride = stride;
277 stream_info->elements[idx].stream_idx = element->input_slot;
279 if (!device->adapter->gl_info.supported[ARB_VERTEX_ARRAY_BGRA]
280 && element->format->id == WINED3DFMT_B8G8R8A8_UNORM)
282 stream_info->swizzle_map |= 1 << idx;
284 stream_info->use_map |= 1 << idx;
288 /* Preload the vertex buffers. */
289 device->num_buffer_queries = 0;
290 for (i = 0, map = stream_info->use_map; map; map >>= 1, ++i)
292 struct wined3d_stream_info_element *element;
293 struct wined3d_buffer *buffer;
298 element = &stream_info->elements[i];
299 buffer = state->streams[element->stream_idx].buffer;
300 wined3d_buffer_preload(buffer);
302 /* If the preload dropped the buffer object, update the stream info. */
303 if (buffer->buffer_object != element->data.buffer_object)
305 element->data.buffer_object = 0;
306 element->data.addr = buffer_get_sysmem(buffer, &device->adapter->gl_info) + (ptrdiff_t)element->data.addr;
309 if (!buffer->buffer_object)
310 stream_info->all_vbo = 0;
313 device->buffer_queries[device->num_buffer_queries++] = buffer->query;
317 static void stream_info_element_from_strided(const struct wined3d_gl_info *gl_info,
318 const struct wined3d_strided_element *strided, struct wined3d_stream_info_element *e)
320 e->data.addr = strided->data;
321 e->data.buffer_object = 0;
322 e->format = wined3d_get_format(gl_info, strided->format);
323 e->stride = strided->stride;
327 static void device_stream_info_from_strided(const struct wined3d_gl_info *gl_info,
328 const struct wined3d_strided_data *strided, struct wined3d_stream_info *stream_info)
332 memset(stream_info, 0, sizeof(*stream_info));
334 if (strided->position.data)
335 stream_info_element_from_strided(gl_info, &strided->position, &stream_info->elements[WINED3D_FFP_POSITION]);
336 if (strided->normal.data)
337 stream_info_element_from_strided(gl_info, &strided->normal, &stream_info->elements[WINED3D_FFP_NORMAL]);
338 if (strided->diffuse.data)
339 stream_info_element_from_strided(gl_info, &strided->diffuse, &stream_info->elements[WINED3D_FFP_DIFFUSE]);
340 if (strided->specular.data)
341 stream_info_element_from_strided(gl_info, &strided->specular, &stream_info->elements[WINED3D_FFP_SPECULAR]);
343 for (i = 0; i < WINED3DDP_MAXTEXCOORD; ++i)
345 if (strided->tex_coords[i].data)
346 stream_info_element_from_strided(gl_info, &strided->tex_coords[i],
347 &stream_info->elements[WINED3D_FFP_TEXCOORD0 + i]);
350 stream_info->position_transformed = strided->position_transformed;
352 for (i = 0; i < sizeof(stream_info->elements) / sizeof(*stream_info->elements); ++i)
354 if (!stream_info->elements[i].format) continue;
356 if (!gl_info->supported[ARB_VERTEX_ARRAY_BGRA]
357 && stream_info->elements[i].format->id == WINED3DFMT_B8G8R8A8_UNORM)
359 stream_info->swizzle_map |= 1 << i;
361 stream_info->use_map |= 1 << i;
365 static void device_trace_strided_stream_info(const struct wined3d_stream_info *stream_info)
367 TRACE("Strided Data:\n");
368 TRACE_STRIDED(stream_info, WINED3D_FFP_POSITION);
369 TRACE_STRIDED(stream_info, WINED3D_FFP_BLENDWEIGHT);
370 TRACE_STRIDED(stream_info, WINED3D_FFP_BLENDINDICES);
371 TRACE_STRIDED(stream_info, WINED3D_FFP_NORMAL);
372 TRACE_STRIDED(stream_info, WINED3D_FFP_PSIZE);
373 TRACE_STRIDED(stream_info, WINED3D_FFP_DIFFUSE);
374 TRACE_STRIDED(stream_info, WINED3D_FFP_SPECULAR);
375 TRACE_STRIDED(stream_info, WINED3D_FFP_TEXCOORD0);
376 TRACE_STRIDED(stream_info, WINED3D_FFP_TEXCOORD1);
377 TRACE_STRIDED(stream_info, WINED3D_FFP_TEXCOORD2);
378 TRACE_STRIDED(stream_info, WINED3D_FFP_TEXCOORD3);
379 TRACE_STRIDED(stream_info, WINED3D_FFP_TEXCOORD4);
380 TRACE_STRIDED(stream_info, WINED3D_FFP_TEXCOORD5);
381 TRACE_STRIDED(stream_info, WINED3D_FFP_TEXCOORD6);
382 TRACE_STRIDED(stream_info, WINED3D_FFP_TEXCOORD7);
385 /* Context activation is done by the caller. */
386 void device_update_stream_info(struct wined3d_device *device, const struct wined3d_gl_info *gl_info)
388 struct wined3d_stream_info *stream_info = &device->strided_streams;
389 const struct wined3d_state *state = &device->stateBlock->state;
390 DWORD prev_all_vbo = stream_info->all_vbo;
392 if (device->up_strided)
394 /* Note: this is a ddraw fixed-function code path. */
395 TRACE("=============================== Strided Input ================================\n");
396 device_stream_info_from_strided(gl_info, device->up_strided, stream_info);
397 if (TRACE_ON(d3d)) device_trace_strided_stream_info(stream_info);
401 TRACE("============================= Vertex Declaration =============================\n");
402 device_stream_info_from_declaration(device, stream_info);
405 if (state->vertex_shader && !stream_info->position_transformed)
407 if (state->vertex_declaration->half_float_conv_needed && !stream_info->all_vbo)
409 TRACE("Using drawStridedSlow with vertex shaders for FLOAT16 conversion.\n");
410 device->useDrawStridedSlow = TRUE;
414 device->useDrawStridedSlow = FALSE;
419 WORD slow_mask = (1 << WINED3D_FFP_PSIZE);
420 slow_mask |= -!gl_info->supported[ARB_VERTEX_ARRAY_BGRA]
421 & ((1 << WINED3D_FFP_DIFFUSE) | (1 << WINED3D_FFP_SPECULAR));
423 if ((stream_info->position_transformed || (stream_info->use_map & slow_mask)) && !stream_info->all_vbo)
425 device->useDrawStridedSlow = TRUE;
429 device->useDrawStridedSlow = FALSE;
433 if (prev_all_vbo != stream_info->all_vbo)
434 device_invalidate_state(device, STATE_INDEXBUFFER);
437 static void device_preload_texture(const struct wined3d_state *state, unsigned int idx)
439 struct wined3d_texture *texture;
440 enum WINED3DSRGB srgb;
442 if (!(texture = state->textures[idx])) return;
443 srgb = state->sampler_states[idx][WINED3D_SAMP_SRGB_TEXTURE] ? SRGB_SRGB : SRGB_RGB;
444 texture->texture_ops->texture_preload(texture, srgb);
447 void device_preload_textures(const struct wined3d_device *device)
449 const struct wined3d_state *state = &device->stateBlock->state;
454 for (i = 0; i < MAX_VERTEX_SAMPLERS; ++i)
456 if (state->vertex_shader->reg_maps.sampler_type[i])
457 device_preload_texture(state, MAX_FRAGMENT_SAMPLERS + i);
463 for (i = 0; i < MAX_FRAGMENT_SAMPLERS; ++i)
465 if (state->pixel_shader->reg_maps.sampler_type[i])
466 device_preload_texture(state, i);
471 WORD ffu_map = device->fixed_function_usage_map;
473 for (i = 0; ffu_map; ffu_map >>= 1, ++i)
476 device_preload_texture(state, i);
481 BOOL device_context_add(struct wined3d_device *device, struct wined3d_context *context)
483 struct wined3d_context **new_array;
485 TRACE("Adding context %p.\n", context);
487 if (!device->contexts) new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_array));
488 else new_array = HeapReAlloc(GetProcessHeap(), 0, device->contexts,
489 sizeof(*new_array) * (device->context_count + 1));
493 ERR("Failed to grow the context array.\n");
497 new_array[device->context_count++] = context;
498 device->contexts = new_array;
502 void device_context_remove(struct wined3d_device *device, struct wined3d_context *context)
504 struct wined3d_context **new_array;
508 TRACE("Removing context %p.\n", context);
510 for (i = 0; i < device->context_count; ++i)
512 if (device->contexts[i] == context)
521 ERR("Context %p doesn't exist in context array.\n", context);
525 if (!--device->context_count)
527 HeapFree(GetProcessHeap(), 0, device->contexts);
528 device->contexts = NULL;
532 memmove(&device->contexts[i], &device->contexts[i + 1], (device->context_count - i) * sizeof(*device->contexts));
533 new_array = HeapReAlloc(GetProcessHeap(), 0, device->contexts, device->context_count * sizeof(*device->contexts));
536 ERR("Failed to shrink context array. Oh well.\n");
540 device->contexts = new_array;
543 /* Do not call while under the GL lock. */
544 void device_switch_onscreen_ds(struct wined3d_device *device,
545 struct wined3d_context *context, struct wined3d_surface *depth_stencil)
547 if (device->onscreen_depth_stencil)
549 surface_load_ds_location(device->onscreen_depth_stencil, context, SFLAG_INTEXTURE);
551 surface_modify_ds_location(device->onscreen_depth_stencil, SFLAG_INTEXTURE,
552 device->onscreen_depth_stencil->ds_current_size.cx,
553 device->onscreen_depth_stencil->ds_current_size.cy);
554 wined3d_surface_decref(device->onscreen_depth_stencil);
556 device->onscreen_depth_stencil = depth_stencil;
557 wined3d_surface_incref(device->onscreen_depth_stencil);
560 static BOOL is_full_clear(const struct wined3d_surface *target, const RECT *draw_rect, const RECT *clear_rect)
562 /* partial draw rect */
563 if (draw_rect->left || draw_rect->top
564 || draw_rect->right < target->resource.width
565 || draw_rect->bottom < target->resource.height)
568 /* partial clear rect */
569 if (clear_rect && (clear_rect->left > 0 || clear_rect->top > 0
570 || clear_rect->right < target->resource.width
571 || clear_rect->bottom < target->resource.height))
577 static void prepare_ds_clear(struct wined3d_surface *ds, struct wined3d_context *context,
578 DWORD location, const RECT *draw_rect, UINT rect_count, const RECT *clear_rect, RECT *out_rect)
580 RECT current_rect, r;
582 if (ds->flags & location)
583 SetRect(¤t_rect, 0, 0,
584 ds->ds_current_size.cx,
585 ds->ds_current_size.cy);
587 SetRectEmpty(¤t_rect);
589 IntersectRect(&r, draw_rect, ¤t_rect);
590 if (EqualRect(&r, draw_rect))
592 /* current_rect ⊇ draw_rect, modify only. */
593 SetRect(out_rect, 0, 0, ds->ds_current_size.cx, ds->ds_current_size.cy);
597 if (EqualRect(&r, ¤t_rect))
599 /* draw_rect ⊇ current_rect, test if we're doing a full clear. */
603 /* Full clear, modify only. */
604 *out_rect = *draw_rect;
608 IntersectRect(&r, draw_rect, clear_rect);
609 if (EqualRect(&r, draw_rect))
611 /* clear_rect ⊇ draw_rect, modify only. */
612 *out_rect = *draw_rect;
618 surface_load_ds_location(ds, context, location);
619 SetRect(out_rect, 0, 0, ds->ds_current_size.cx, ds->ds_current_size.cy);
622 /* Do not call while under the GL lock. */
623 void device_clear_render_targets(struct wined3d_device *device, UINT rt_count, const struct wined3d_fb_state *fb,
624 UINT rect_count, const RECT *rects, const RECT *draw_rect, DWORD flags, const struct wined3d_color *color,
625 float depth, DWORD stencil)
627 const RECT *clear_rect = (rect_count > 0 && rects) ? (const RECT *)rects : NULL;
628 struct wined3d_surface *target = rt_count ? fb->render_targets[0] : NULL;
629 const struct wined3d_gl_info *gl_info;
630 UINT drawable_width, drawable_height;
631 struct wined3d_context *context;
632 GLbitfield clear_mask = 0;
633 BOOL render_offscreen;
637 /* When we're clearing parts of the drawable, make sure that the target surface is well up to date in the
638 * drawable. After the clear we'll mark the drawable up to date, so we have to make sure that this is true
639 * for the cleared parts, and the untouched parts.
641 * If we're clearing the whole target there is no need to copy it into the drawable, it will be overwritten
642 * anyway. If we're not clearing the color buffer we don't have to copy either since we're not going to set
643 * the drawable up to date. We have to check all settings that limit the clear area though. Do not bother
644 * checking all this if the dest surface is in the drawable anyway. */
645 if (flags & WINED3DCLEAR_TARGET && !is_full_clear(target, draw_rect, clear_rect))
647 for (i = 0; i < rt_count; ++i)
649 struct wined3d_surface *rt = fb->render_targets[i];
651 surface_load_location(rt, rt->draw_binding, NULL);
655 context = context_acquire(device, target);
658 context_release(context);
659 WARN("Invalid context, skipping clear.\n");
662 gl_info = context->gl_info;
666 render_offscreen = context->render_offscreen;
667 target->get_drawable_size(context, &drawable_width, &drawable_height);
671 render_offscreen = TRUE;
672 drawable_width = fb->depth_stencil->pow2Width;
673 drawable_height = fb->depth_stencil->pow2Height;
676 if (flags & WINED3DCLEAR_ZBUFFER)
678 DWORD location = render_offscreen ? fb->depth_stencil->draw_binding : SFLAG_INDRAWABLE;
680 if (!render_offscreen && fb->depth_stencil != device->onscreen_depth_stencil)
681 device_switch_onscreen_ds(device, context, fb->depth_stencil);
682 prepare_ds_clear(fb->depth_stencil, context, location,
683 draw_rect, rect_count, clear_rect, &ds_rect);
686 if (!context_apply_clear_state(context, device, rt_count, fb))
688 context_release(context);
689 WARN("Failed to apply clear state, skipping clear.\n");
693 /* Only set the values up once, as they are not changing. */
694 if (flags & WINED3DCLEAR_STENCIL)
696 if (gl_info->supported[EXT_STENCIL_TWO_SIDE])
698 gl_info->gl_ops.gl.p_glDisable(GL_STENCIL_TEST_TWO_SIDE_EXT);
699 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_TWOSIDEDSTENCILMODE));
701 gl_info->gl_ops.gl.p_glStencilMask(~0U);
702 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_STENCILWRITEMASK));
703 gl_info->gl_ops.gl.p_glClearStencil(stencil);
704 checkGLcall("glClearStencil");
705 clear_mask = clear_mask | GL_STENCIL_BUFFER_BIT;
708 if (flags & WINED3DCLEAR_ZBUFFER)
710 DWORD location = render_offscreen ? fb->depth_stencil->draw_binding : SFLAG_INDRAWABLE;
712 surface_modify_ds_location(fb->depth_stencil, location, ds_rect.right, ds_rect.bottom);
714 gl_info->gl_ops.gl.p_glDepthMask(GL_TRUE);
715 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_ZWRITEENABLE));
716 gl_info->gl_ops.gl.p_glClearDepth(depth);
717 checkGLcall("glClearDepth");
718 clear_mask = clear_mask | GL_DEPTH_BUFFER_BIT;
721 if (flags & WINED3DCLEAR_TARGET)
723 for (i = 0; i < rt_count; ++i)
725 struct wined3d_surface *rt = fb->render_targets[i];
728 surface_modify_location(rt, rt->draw_binding, TRUE);
731 gl_info->gl_ops.gl.p_glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
732 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE));
733 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE1));
734 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE2));
735 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE3));
736 gl_info->gl_ops.gl.p_glClearColor(color->r, color->g, color->b, color->a);
737 checkGLcall("glClearColor");
738 clear_mask = clear_mask | GL_COLOR_BUFFER_BIT;
743 if (render_offscreen)
745 gl_info->gl_ops.gl.p_glScissor(draw_rect->left, draw_rect->top,
746 draw_rect->right - draw_rect->left, draw_rect->bottom - draw_rect->top);
750 gl_info->gl_ops.gl.p_glScissor(draw_rect->left, drawable_height - draw_rect->bottom,
751 draw_rect->right - draw_rect->left, draw_rect->bottom - draw_rect->top);
753 checkGLcall("glScissor");
754 gl_info->gl_ops.gl.p_glClear(clear_mask);
755 checkGLcall("glClear");
761 /* Now process each rect in turn. */
762 for (i = 0; i < rect_count; ++i)
764 /* Note that GL uses lower left, width/height. */
765 IntersectRect(¤t_rect, draw_rect, &clear_rect[i]);
767 TRACE("clear_rect[%u] %s, current_rect %s.\n", i,
768 wine_dbgstr_rect(&clear_rect[i]),
769 wine_dbgstr_rect(¤t_rect));
771 /* Tests show that rectangles where x1 > x2 or y1 > y2 are ignored silently.
772 * The rectangle is not cleared, no error is returned, but further rectangles are
773 * still cleared if they are valid. */
774 if (current_rect.left > current_rect.right || current_rect.top > current_rect.bottom)
776 TRACE("Rectangle with negative dimensions, ignoring.\n");
780 if (render_offscreen)
782 gl_info->gl_ops.gl.p_glScissor(current_rect.left, current_rect.top,
783 current_rect.right - current_rect.left, current_rect.bottom - current_rect.top);
787 gl_info->gl_ops.gl.p_glScissor(current_rect.left, drawable_height - current_rect.bottom,
788 current_rect.right - current_rect.left, current_rect.bottom - current_rect.top);
790 checkGLcall("glScissor");
792 gl_info->gl_ops.gl.p_glClear(clear_mask);
793 checkGLcall("glClear");
797 if (wined3d_settings.strict_draw_ordering || (flags & WINED3DCLEAR_TARGET
798 && target->container.type == WINED3D_CONTAINER_SWAPCHAIN
799 && target->container.u.swapchain->front_buffer == target))
800 gl_info->gl_ops.gl.p_glFlush(); /* Flush to ensure ordering across contexts. */
802 context_release(context);
805 ULONG CDECL wined3d_device_incref(struct wined3d_device *device)
807 ULONG refcount = InterlockedIncrement(&device->ref);
809 TRACE("%p increasing refcount to %u.\n", device, refcount);
814 ULONG CDECL wined3d_device_decref(struct wined3d_device *device)
816 ULONG refcount = InterlockedDecrement(&device->ref);
818 TRACE("%p decreasing refcount to %u.\n", device, refcount);
822 struct wined3d_stateblock *stateblock;
825 if (wined3d_stateblock_decref(device->updateStateBlock)
826 && device->updateStateBlock != device->stateBlock)
827 FIXME("Something's still holding the update stateblock.\n");
828 device->updateStateBlock = NULL;
830 stateblock = device->stateBlock;
831 device->stateBlock = NULL;
832 if (wined3d_stateblock_decref(stateblock))
833 FIXME("Something's still holding the stateblock.\n");
835 for (i = 0; i < sizeof(device->multistate_funcs) / sizeof(device->multistate_funcs[0]); ++i)
837 HeapFree(GetProcessHeap(), 0, device->multistate_funcs[i]);
838 device->multistate_funcs[i] = NULL;
841 if (!list_empty(&device->resources))
843 struct wined3d_resource *resource;
845 FIXME("Device released with resources still bound, acceptable but unexpected.\n");
847 LIST_FOR_EACH_ENTRY(resource, &device->resources, struct wined3d_resource, resource_list_entry)
849 FIXME("Leftover resource %p with type %s (%#x).\n",
850 resource, debug_d3dresourcetype(resource->type), resource->type);
854 if (device->contexts)
855 ERR("Context array not freed!\n");
856 if (device->hardwareCursor)
857 DestroyCursor(device->hardwareCursor);
858 device->hardwareCursor = 0;
860 wined3d_decref(device->wined3d);
861 device->wined3d = NULL;
862 HeapFree(GetProcessHeap(), 0, device);
863 TRACE("Freed device %p.\n", device);
869 UINT CDECL wined3d_device_get_swapchain_count(const struct wined3d_device *device)
871 TRACE("device %p.\n", device);
873 return device->swapchain_count;
876 struct wined3d_swapchain * CDECL wined3d_device_get_swapchain(const struct wined3d_device *device, UINT swapchain_idx)
878 TRACE("device %p, swapchain_idx %u.\n", device, swapchain_idx);
880 if (swapchain_idx >= device->swapchain_count)
882 WARN("swapchain_idx %u >= swapchain_count %u.\n",
883 swapchain_idx, device->swapchain_count);
887 return device->swapchains[swapchain_idx];
890 static void device_load_logo(struct wined3d_device *device, const char *filename)
892 struct wined3d_color_key color_key;
896 HDC dcb = NULL, dcs = NULL;
898 hbm = LoadImageA(NULL, filename, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION);
901 GetObjectA(hbm, sizeof(BITMAP), &bm);
902 dcb = CreateCompatibleDC(NULL);
904 SelectObject(dcb, hbm);
908 /* Create a 32x32 white surface to indicate that wined3d is used, but the specified image
911 memset(&bm, 0, sizeof(bm));
916 hr = wined3d_surface_create(device, bm.bmWidth, bm.bmHeight, WINED3DFMT_B5G6R5_UNORM, 0,
917 WINED3D_POOL_SYSTEM_MEM, WINED3D_MULTISAMPLE_NONE, 0, WINED3D_SURFACE_MAPPABLE,
918 NULL, &wined3d_null_parent_ops, &device->logo_surface);
921 ERR("Wine logo requested, but failed to create surface, hr %#x.\n", hr);
927 if (FAILED(hr = wined3d_surface_getdc(device->logo_surface, &dcs)))
929 BitBlt(dcs, 0, 0, bm.bmWidth, bm.bmHeight, dcb, 0, 0, SRCCOPY);
930 wined3d_surface_releasedc(device->logo_surface, dcs);
932 color_key.color_space_low_value = 0;
933 color_key.color_space_high_value = 0;
934 wined3d_surface_set_color_key(device->logo_surface, WINEDDCKEY_SRCBLT, &color_key);
938 const struct wined3d_color c = {1.0f, 1.0f, 1.0f, 1.0f};
939 /* Fill the surface with a white color to show that wined3d is there */
940 wined3d_device_color_fill(device, device->logo_surface, NULL, &c);
944 if (dcb) DeleteDC(dcb);
945 if (hbm) DeleteObject(hbm);
948 /* Context activation is done by the caller. */
949 static void create_dummy_textures(struct wined3d_device *device, struct wined3d_context *context)
951 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
952 unsigned int i, j, count;
953 /* Under DirectX you can sample even if no texture is bound, whereas
954 * OpenGL will only allow that when a valid texture is bound.
955 * We emulate this by creating dummy textures and binding them
956 * to each texture stage when the currently set D3D texture is NULL. */
958 if (gl_info->supported[APPLE_CLIENT_STORAGE])
960 /* The dummy texture does not have client storage backing */
961 gl_info->gl_ops.gl.p_glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE);
962 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE)");
965 count = min(MAX_COMBINED_SAMPLERS, gl_info->limits.combined_samplers);
966 for (i = 0; i < count; ++i)
968 DWORD color = 0x000000ff;
970 /* Make appropriate texture active */
971 context_active_texture(context, gl_info, i);
973 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_texture_2d[i]);
974 checkGLcall("glGenTextures");
975 TRACE("Dummy 2D texture %u given name %u.\n", i, device->dummy_texture_2d[i]);
977 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D, device->dummy_texture_2d[i]);
978 checkGLcall("glBindTexture");
980 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 1, 1, 0,
981 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
982 checkGLcall("glTexImage2D");
984 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
986 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_texture_rect[i]);
987 checkGLcall("glGenTextures");
988 TRACE("Dummy rectangle texture %u given name %u.\n", i, device->dummy_texture_rect[i]);
990 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_RECTANGLE_ARB, device->dummy_texture_rect[i]);
991 checkGLcall("glBindTexture");
993 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA8, 1, 1, 0,
994 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
995 checkGLcall("glTexImage2D");
998 if (gl_info->supported[EXT_TEXTURE3D])
1000 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_texture_3d[i]);
1001 checkGLcall("glGenTextures");
1002 TRACE("Dummy 3D texture %u given name %u.\n", i, device->dummy_texture_3d[i]);
1004 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_3D, device->dummy_texture_3d[i]);
1005 checkGLcall("glBindTexture");
1007 GL_EXTCALL(glTexImage3DEXT(GL_TEXTURE_3D, 0, GL_RGBA8, 1, 1, 1, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color));
1008 checkGLcall("glTexImage3D");
1011 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
1013 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_texture_cube[i]);
1014 checkGLcall("glGenTextures");
1015 TRACE("Dummy cube texture %u given name %u.\n", i, device->dummy_texture_cube[i]);
1017 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_CUBE_MAP, device->dummy_texture_cube[i]);
1018 checkGLcall("glBindTexture");
1020 for (j = GL_TEXTURE_CUBE_MAP_POSITIVE_X; j <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z; ++j)
1022 gl_info->gl_ops.gl.p_glTexImage2D(j, 0, GL_RGBA8, 1, 1, 0,
1023 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
1024 checkGLcall("glTexImage2D");
1029 if (gl_info->supported[APPLE_CLIENT_STORAGE])
1031 /* Re-enable because if supported it is enabled by default */
1032 gl_info->gl_ops.gl.p_glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE);
1033 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE)");
1037 /* Context activation is done by the caller. */
1038 static void destroy_dummy_textures(struct wined3d_device *device, const struct wined3d_gl_info *gl_info)
1040 unsigned int count = min(MAX_COMBINED_SAMPLERS, gl_info->limits.combined_samplers);
1042 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
1044 gl_info->gl_ops.gl.p_glDeleteTextures(count, device->dummy_texture_cube);
1045 checkGLcall("glDeleteTextures(count, device->dummy_texture_cube)");
1048 if (gl_info->supported[EXT_TEXTURE3D])
1050 gl_info->gl_ops.gl.p_glDeleteTextures(count, device->dummy_texture_3d);
1051 checkGLcall("glDeleteTextures(count, device->dummy_texture_3d)");
1054 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
1056 gl_info->gl_ops.gl.p_glDeleteTextures(count, device->dummy_texture_rect);
1057 checkGLcall("glDeleteTextures(count, device->dummy_texture_rect)");
1060 gl_info->gl_ops.gl.p_glDeleteTextures(count, device->dummy_texture_2d);
1061 checkGLcall("glDeleteTextures(count, device->dummy_texture_2d)");
1063 memset(device->dummy_texture_cube, 0, gl_info->limits.textures * sizeof(*device->dummy_texture_cube));
1064 memset(device->dummy_texture_3d, 0, gl_info->limits.textures * sizeof(*device->dummy_texture_3d));
1065 memset(device->dummy_texture_rect, 0, gl_info->limits.textures * sizeof(*device->dummy_texture_rect));
1066 memset(device->dummy_texture_2d, 0, gl_info->limits.textures * sizeof(*device->dummy_texture_2d));
1069 static LONG fullscreen_style(LONG style)
1071 /* Make sure the window is managed, otherwise we won't get keyboard input. */
1072 style |= WS_POPUP | WS_SYSMENU;
1073 style &= ~(WS_CAPTION | WS_THICKFRAME);
1078 static LONG fullscreen_exstyle(LONG exstyle)
1080 /* Filter out window decorations. */
1081 exstyle &= ~(WS_EX_WINDOWEDGE | WS_EX_CLIENTEDGE);
1086 void CDECL wined3d_device_setup_fullscreen_window(struct wined3d_device *device, HWND window, UINT w, UINT h)
1088 BOOL filter_messages;
1089 LONG style, exstyle;
1091 TRACE("Setting up window %p for fullscreen mode.\n", window);
1093 if (device->style || device->exStyle)
1095 ERR("Changing the window style for window %p, but another style (%08x, %08x) is already stored.\n",
1096 window, device->style, device->exStyle);
1099 device->style = GetWindowLongW(window, GWL_STYLE);
1100 device->exStyle = GetWindowLongW(window, GWL_EXSTYLE);
1102 style = fullscreen_style(device->style);
1103 exstyle = fullscreen_exstyle(device->exStyle);
1105 TRACE("Old style was %08x, %08x, setting to %08x, %08x.\n",
1106 device->style, device->exStyle, style, exstyle);
1108 filter_messages = device->filter_messages;
1109 device->filter_messages = TRUE;
1111 SetWindowLongW(window, GWL_STYLE, style);
1112 SetWindowLongW(window, GWL_EXSTYLE, exstyle);
1113 SetWindowPos(window, HWND_TOP, 0, 0, w, h, SWP_FRAMECHANGED | SWP_SHOWWINDOW | SWP_NOACTIVATE);
1115 device->filter_messages = filter_messages;
1118 void CDECL wined3d_device_restore_fullscreen_window(struct wined3d_device *device, HWND window)
1120 BOOL filter_messages;
1121 LONG style, exstyle;
1123 if (!device->style && !device->exStyle) return;
1125 TRACE("Restoring window style of window %p to %08x, %08x.\n",
1126 window, device->style, device->exStyle);
1128 style = GetWindowLongW(window, GWL_STYLE);
1129 exstyle = GetWindowLongW(window, GWL_EXSTYLE);
1131 filter_messages = device->filter_messages;
1132 device->filter_messages = TRUE;
1134 /* Only restore the style if the application didn't modify it during the
1135 * fullscreen phase. Some applications change it before calling Reset()
1136 * when switching between windowed and fullscreen modes (HL2), some
1137 * depend on the original style (Eve Online). */
1138 if (style == fullscreen_style(device->style) && exstyle == fullscreen_exstyle(device->exStyle))
1140 SetWindowLongW(window, GWL_STYLE, device->style);
1141 SetWindowLongW(window, GWL_EXSTYLE, device->exStyle);
1143 SetWindowPos(window, 0, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
1145 device->filter_messages = filter_messages;
1147 /* Delete the old values. */
1149 device->exStyle = 0;
1152 HRESULT CDECL wined3d_device_acquire_focus_window(struct wined3d_device *device, HWND window)
1154 TRACE("device %p, window %p.\n", device, window);
1156 if (!wined3d_register_window(window, device))
1158 ERR("Failed to register window %p.\n", window);
1162 InterlockedExchangePointer((void **)&device->focus_window, window);
1163 SetWindowPos(window, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
1168 void CDECL wined3d_device_release_focus_window(struct wined3d_device *device)
1170 TRACE("device %p.\n", device);
1172 if (device->focus_window) wined3d_unregister_window(device->focus_window);
1173 InterlockedExchangePointer((void **)&device->focus_window, NULL);
1176 HRESULT CDECL wined3d_device_init_3d(struct wined3d_device *device,
1177 struct wined3d_swapchain_desc *swapchain_desc)
1179 static const struct wined3d_color black = {0.0f, 0.0f, 0.0f, 0.0f};
1180 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
1181 struct wined3d_swapchain *swapchain = NULL;
1182 struct wined3d_context *context;
1187 TRACE("device %p, swapchain_desc %p.\n", device, swapchain_desc);
1189 if (device->d3d_initialized)
1190 return WINED3DERR_INVALIDCALL;
1191 if (device->wined3d->flags & WINED3D_NO3D)
1192 return WINED3DERR_INVALIDCALL;
1194 device->valid_rt_mask = 0;
1195 for (i = 0; i < gl_info->limits.buffers; ++i)
1196 device->valid_rt_mask |= (1 << i);
1197 device->fb.render_targets = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
1198 sizeof(*device->fb.render_targets) * gl_info->limits.buffers);
1200 /* Initialize the texture unit mapping to a 1:1 mapping */
1201 for (state = 0; state < MAX_COMBINED_SAMPLERS; ++state)
1203 if (state < gl_info->limits.fragment_samplers)
1205 device->texUnitMap[state] = state;
1206 device->rev_tex_unit_map[state] = state;
1210 device->texUnitMap[state] = WINED3D_UNMAPPED_STAGE;
1211 device->rev_tex_unit_map[state] = WINED3D_UNMAPPED_STAGE;
1215 if (FAILED(hr = device->shader_backend->shader_alloc_private(device, device->adapter->fragment_pipe)))
1217 TRACE("Shader private data couldn't be allocated\n");
1220 if (FAILED(hr = device->blitter->alloc_private(device)))
1222 TRACE("Blitter private data couldn't be allocated\n");
1226 /* Setup the implicit swapchain. This also initializes a context. */
1227 TRACE("Creating implicit swapchain\n");
1228 hr = device->device_parent->ops->create_swapchain(device->device_parent,
1229 swapchain_desc, &swapchain);
1232 WARN("Failed to create implicit swapchain\n");
1236 device->swapchain_count = 1;
1237 device->swapchains = HeapAlloc(GetProcessHeap(), 0, device->swapchain_count * sizeof(*device->swapchains));
1238 if (!device->swapchains)
1240 ERR("Out of memory!\n");
1243 device->swapchains[0] = swapchain;
1245 if (swapchain->back_buffers && swapchain->back_buffers[0])
1247 TRACE("Setting rendertarget to %p.\n", swapchain->back_buffers);
1248 device->fb.render_targets[0] = swapchain->back_buffers[0];
1252 TRACE("Setting rendertarget to %p.\n", swapchain->front_buffer);
1253 device->fb.render_targets[0] = swapchain->front_buffer;
1255 wined3d_surface_incref(device->fb.render_targets[0]);
1257 /* Depth Stencil support */
1258 device->fb.depth_stencil = device->auto_depth_stencil;
1259 if (device->fb.depth_stencil)
1260 wined3d_surface_incref(device->fb.depth_stencil);
1262 /* Set up some starting GL setup */
1264 /* Setup all the devices defaults */
1265 stateblock_init_default_state(device->stateBlock);
1267 context = context_acquire(device, swapchain->front_buffer);
1269 create_dummy_textures(device, context);
1271 /* Initialize the current view state */
1272 device->view_ident = 1;
1273 device->contexts[0]->last_was_rhw = 0;
1275 switch (wined3d_settings.offscreen_rendering_mode)
1278 device->offscreenBuffer = GL_COLOR_ATTACHMENT0;
1281 case ORM_BACKBUFFER:
1283 if (context_get_current()->aux_buffers > 0)
1285 TRACE("Using auxiliary buffer for offscreen rendering\n");
1286 device->offscreenBuffer = GL_AUX0;
1290 TRACE("Using back buffer for offscreen rendering\n");
1291 device->offscreenBuffer = GL_BACK;
1296 TRACE("All defaults now set up, leaving 3D init.\n");
1298 context_release(context);
1300 /* Clear the screen */
1301 wined3d_device_clear(device, 0, NULL, WINED3DCLEAR_TARGET
1302 | (swapchain_desc->enable_auto_depth_stencil ? WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL : 0),
1305 device->d3d_initialized = TRUE;
1307 if (wined3d_settings.logo)
1308 device_load_logo(device, wined3d_settings.logo);
1312 HeapFree(GetProcessHeap(), 0, device->fb.render_targets);
1313 HeapFree(GetProcessHeap(), 0, device->swapchains);
1314 device->swapchain_count = 0;
1316 wined3d_swapchain_decref(swapchain);
1317 if (device->blit_priv)
1318 device->blitter->free_private(device);
1319 if (device->shader_priv)
1320 device->shader_backend->shader_free_private(device);
1325 HRESULT CDECL wined3d_device_init_gdi(struct wined3d_device *device,
1326 struct wined3d_swapchain_desc *swapchain_desc)
1328 struct wined3d_swapchain *swapchain = NULL;
1331 TRACE("device %p, swapchain_desc %p.\n", device, swapchain_desc);
1333 /* Setup the implicit swapchain */
1334 TRACE("Creating implicit swapchain\n");
1335 hr = device->device_parent->ops->create_swapchain(device->device_parent,
1336 swapchain_desc, &swapchain);
1339 WARN("Failed to create implicit swapchain\n");
1343 device->swapchain_count = 1;
1344 device->swapchains = HeapAlloc(GetProcessHeap(), 0, device->swapchain_count * sizeof(*device->swapchains));
1345 if (!device->swapchains)
1347 ERR("Out of memory!\n");
1350 device->swapchains[0] = swapchain;
1354 wined3d_swapchain_decref(swapchain);
1358 HRESULT CDECL wined3d_device_uninit_3d(struct wined3d_device *device)
1360 struct wined3d_resource *resource, *cursor;
1361 const struct wined3d_gl_info *gl_info;
1362 struct wined3d_context *context;
1363 struct wined3d_surface *surface;
1366 TRACE("device %p.\n", device);
1368 if (!device->d3d_initialized)
1369 return WINED3DERR_INVALIDCALL;
1371 /* Force making the context current again, to verify it is still valid
1372 * (workaround for broken drivers) */
1373 context_set_current(NULL);
1374 /* I don't think that the interface guarantees that the device is destroyed from the same thread
1375 * it was created. Thus make sure a context is active for the glDelete* calls
1377 context = context_acquire(device, NULL);
1378 gl_info = context->gl_info;
1380 if (device->logo_surface)
1381 wined3d_surface_decref(device->logo_surface);
1383 stateblock_unbind_resources(device->stateBlock);
1385 /* Unload resources */
1386 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
1388 TRACE("Unloading resource %p.\n", resource);
1390 resource->resource_ops->resource_unload(resource);
1393 TRACE("Deleting high order patches\n");
1394 for (i = 0; i < PATCHMAP_SIZE; ++i)
1396 struct wined3d_rect_patch *patch;
1397 struct list *e1, *e2;
1399 LIST_FOR_EACH_SAFE(e1, e2, &device->patches[i])
1401 patch = LIST_ENTRY(e1, struct wined3d_rect_patch, entry);
1402 wined3d_device_delete_patch(device, patch->Handle);
1406 /* Delete the mouse cursor texture */
1407 if (device->cursorTexture)
1409 gl_info->gl_ops.gl.p_glDeleteTextures(1, &device->cursorTexture);
1410 device->cursorTexture = 0;
1413 /* Destroy the depth blt resources, they will be invalid after the reset. Also free shader
1414 * private data, it might contain opengl pointers
1416 if (device->depth_blt_texture)
1418 gl_info->gl_ops.gl.p_glDeleteTextures(1, &device->depth_blt_texture);
1419 device->depth_blt_texture = 0;
1422 /* Destroy the shader backend. Note that this has to happen after all shaders are destroyed. */
1423 device->blitter->free_private(device);
1424 device->shader_backend->shader_free_private(device);
1426 /* Release the buffers (with sanity checks)*/
1427 if (device->onscreen_depth_stencil)
1429 surface = device->onscreen_depth_stencil;
1430 device->onscreen_depth_stencil = NULL;
1431 wined3d_surface_decref(surface);
1434 if (device->fb.depth_stencil)
1436 surface = device->fb.depth_stencil;
1438 TRACE("Releasing depth/stencil buffer %p.\n", surface);
1440 device->fb.depth_stencil = NULL;
1441 wined3d_surface_decref(surface);
1444 if (device->auto_depth_stencil)
1446 surface = device->auto_depth_stencil;
1447 device->auto_depth_stencil = NULL;
1448 if (wined3d_surface_decref(surface))
1449 FIXME("Something's still holding the auto depth stencil buffer (%p).\n", surface);
1452 for (i = 1; i < gl_info->limits.buffers; ++i)
1454 wined3d_device_set_render_target(device, i, NULL, FALSE);
1457 surface = device->fb.render_targets[0];
1458 TRACE("Setting rendertarget 0 to NULL\n");
1459 device->fb.render_targets[0] = NULL;
1460 TRACE("Releasing the render target at %p\n", surface);
1461 wined3d_surface_decref(surface);
1463 context_release(context);
1465 for (i = 0; i < device->swapchain_count; ++i)
1467 TRACE("Releasing the implicit swapchain %u.\n", i);
1468 if (wined3d_swapchain_decref(device->swapchains[i]))
1469 FIXME("Something's still holding the implicit swapchain.\n");
1472 HeapFree(GetProcessHeap(), 0, device->swapchains);
1473 device->swapchains = NULL;
1474 device->swapchain_count = 0;
1476 HeapFree(GetProcessHeap(), 0, device->fb.render_targets);
1477 device->fb.render_targets = NULL;
1479 device->d3d_initialized = FALSE;
1484 HRESULT CDECL wined3d_device_uninit_gdi(struct wined3d_device *device)
1488 for (i = 0; i < device->swapchain_count; ++i)
1490 TRACE("Releasing the implicit swapchain %u.\n", i);
1491 if (wined3d_swapchain_decref(device->swapchains[i]))
1492 FIXME("Something's still holding the implicit swapchain.\n");
1495 HeapFree(GetProcessHeap(), 0, device->swapchains);
1496 device->swapchains = NULL;
1497 device->swapchain_count = 0;
1501 /* Enables thread safety in the wined3d device and its resources. Called by DirectDraw
1502 * from SetCooperativeLevel if DDSCL_MULTITHREADED is specified, and by d3d8/9 from
1503 * CreateDevice if D3DCREATE_MULTITHREADED is passed.
1505 * There is no way to deactivate thread safety once it is enabled.
1507 void CDECL wined3d_device_set_multithreaded(struct wined3d_device *device)
1509 TRACE("device %p.\n", device);
1511 /* For now just store the flag (needed in case of ddraw). */
1512 device->create_parms.flags |= WINED3DCREATE_MULTITHREADED;
1515 UINT CDECL wined3d_device_get_available_texture_mem(const struct wined3d_device *device)
1517 TRACE("device %p.\n", device);
1519 TRACE("Emulating %d MB, returning %d MB left.\n",
1520 device->adapter->TextureRam / (1024 * 1024),
1521 (device->adapter->TextureRam - device->adapter->UsedTextureRam) / (1024 * 1024));
1523 return device->adapter->TextureRam - device->adapter->UsedTextureRam;
1526 void CDECL wined3d_device_set_stream_output(struct wined3d_device *device, UINT idx,
1527 struct wined3d_buffer *buffer, UINT offset)
1529 struct wined3d_buffer *prev_buffer;
1531 TRACE("device %p, idx %u, buffer %p, offset %u.\n", device, idx, buffer, offset);
1533 if (idx >= MAX_STREAM_OUT)
1535 WARN("Invalid stream output %u.\n", idx);
1539 prev_buffer = device->updateStateBlock->state.stream_output[idx].buffer;
1540 device->updateStateBlock->state.stream_output[idx].buffer = buffer;
1541 device->updateStateBlock->state.stream_output[idx].offset = offset;
1543 if (device->isRecordingState)
1546 wined3d_buffer_incref(buffer);
1548 wined3d_buffer_decref(prev_buffer);
1552 if (prev_buffer != buffer)
1556 InterlockedIncrement(&buffer->resource.bind_count);
1557 wined3d_buffer_incref(buffer);
1561 InterlockedDecrement(&prev_buffer->resource.bind_count);
1562 wined3d_buffer_decref(prev_buffer);
1567 struct wined3d_buffer * CDECL wined3d_device_get_stream_output(struct wined3d_device *device,
1568 UINT idx, UINT *offset)
1570 TRACE("device %p, idx %u, offset %p.\n", device, idx, offset);
1572 if (idx >= MAX_STREAM_OUT)
1574 WARN("Invalid stream output %u.\n", idx);
1578 *offset = device->stateBlock->state.stream_output[idx].offset;
1579 return device->stateBlock->state.stream_output[idx].buffer;
1582 HRESULT CDECL wined3d_device_set_stream_source(struct wined3d_device *device, UINT stream_idx,
1583 struct wined3d_buffer *buffer, UINT offset, UINT stride)
1585 struct wined3d_stream_state *stream;
1586 struct wined3d_buffer *prev_buffer;
1588 TRACE("device %p, stream_idx %u, buffer %p, offset %u, stride %u.\n",
1589 device, stream_idx, buffer, offset, stride);
1591 if (stream_idx >= MAX_STREAMS)
1593 WARN("Stream index %u out of range.\n", stream_idx);
1594 return WINED3DERR_INVALIDCALL;
1596 else if (offset & 0x3)
1598 WARN("Offset %u is not 4 byte aligned.\n", offset);
1599 return WINED3DERR_INVALIDCALL;
1602 stream = &device->updateStateBlock->state.streams[stream_idx];
1603 prev_buffer = stream->buffer;
1605 device->updateStateBlock->changed.streamSource |= 1 << stream_idx;
1607 if (prev_buffer == buffer
1608 && stream->stride == stride
1609 && stream->offset == offset)
1611 TRACE("Application is setting the old values over, nothing to do.\n");
1615 stream->buffer = buffer;
1618 stream->stride = stride;
1619 stream->offset = offset;
1622 /* Handle recording of state blocks. */
1623 if (device->isRecordingState)
1625 TRACE("Recording... not performing anything.\n");
1627 wined3d_buffer_incref(buffer);
1629 wined3d_buffer_decref(prev_buffer);
1635 InterlockedIncrement(&buffer->resource.bind_count);
1636 wined3d_buffer_incref(buffer);
1640 InterlockedDecrement(&prev_buffer->resource.bind_count);
1641 wined3d_buffer_decref(prev_buffer);
1644 device_invalidate_state(device, STATE_STREAMSRC);
1649 HRESULT CDECL wined3d_device_get_stream_source(const struct wined3d_device *device,
1650 UINT stream_idx, struct wined3d_buffer **buffer, UINT *offset, UINT *stride)
1652 struct wined3d_stream_state *stream;
1654 TRACE("device %p, stream_idx %u, buffer %p, offset %p, stride %p.\n",
1655 device, stream_idx, buffer, offset, stride);
1657 if (stream_idx >= MAX_STREAMS)
1659 WARN("Stream index %u out of range.\n", stream_idx);
1660 return WINED3DERR_INVALIDCALL;
1663 stream = &device->stateBlock->state.streams[stream_idx];
1664 *buffer = stream->buffer;
1666 wined3d_buffer_incref(*buffer);
1668 *offset = stream->offset;
1669 *stride = stream->stride;
1674 HRESULT CDECL wined3d_device_set_stream_source_freq(struct wined3d_device *device, UINT stream_idx, UINT divider)
1676 struct wined3d_stream_state *stream;
1677 UINT old_flags, old_freq;
1679 TRACE("device %p, stream_idx %u, divider %#x.\n", device, stream_idx, divider);
1681 /* Verify input. At least in d3d9 this is invalid. */
1682 if ((divider & WINED3DSTREAMSOURCE_INSTANCEDATA) && (divider & WINED3DSTREAMSOURCE_INDEXEDDATA))
1684 WARN("INSTANCEDATA and INDEXEDDATA were set, returning D3DERR_INVALIDCALL.\n");
1685 return WINED3DERR_INVALIDCALL;
1687 if ((divider & WINED3DSTREAMSOURCE_INSTANCEDATA) && !stream_idx)
1689 WARN("INSTANCEDATA used on stream 0, returning D3DERR_INVALIDCALL.\n");
1690 return WINED3DERR_INVALIDCALL;
1694 WARN("Divider is 0, returning D3DERR_INVALIDCALL.\n");
1695 return WINED3DERR_INVALIDCALL;
1698 stream = &device->updateStateBlock->state.streams[stream_idx];
1699 old_flags = stream->flags;
1700 old_freq = stream->frequency;
1702 stream->flags = divider & (WINED3DSTREAMSOURCE_INSTANCEDATA | WINED3DSTREAMSOURCE_INDEXEDDATA);
1703 stream->frequency = divider & 0x7fffff;
1705 device->updateStateBlock->changed.streamFreq |= 1 << stream_idx;
1707 if (stream->frequency != old_freq || stream->flags != old_flags)
1708 device_invalidate_state(device, STATE_STREAMSRC);
1713 HRESULT CDECL wined3d_device_get_stream_source_freq(const struct wined3d_device *device,
1714 UINT stream_idx, UINT *divider)
1716 struct wined3d_stream_state *stream;
1718 TRACE("device %p, stream_idx %u, divider %p.\n", device, stream_idx, divider);
1720 stream = &device->updateStateBlock->state.streams[stream_idx];
1721 *divider = stream->flags | stream->frequency;
1723 TRACE("Returning %#x.\n", *divider);
1728 void CDECL wined3d_device_set_transform(struct wined3d_device *device,
1729 enum wined3d_transform_state d3dts, const struct wined3d_matrix *matrix)
1731 TRACE("device %p, state %s, matrix %p.\n",
1732 device, debug_d3dtstype(d3dts), matrix);
1733 TRACE("%.8e %.8e %.8e %.8e\n", matrix->u.s._11, matrix->u.s._12, matrix->u.s._13, matrix->u.s._14);
1734 TRACE("%.8e %.8e %.8e %.8e\n", matrix->u.s._21, matrix->u.s._22, matrix->u.s._23, matrix->u.s._24);
1735 TRACE("%.8e %.8e %.8e %.8e\n", matrix->u.s._31, matrix->u.s._32, matrix->u.s._33, matrix->u.s._34);
1736 TRACE("%.8e %.8e %.8e %.8e\n", matrix->u.s._41, matrix->u.s._42, matrix->u.s._43, matrix->u.s._44);
1738 /* Handle recording of state blocks. */
1739 if (device->isRecordingState)
1741 TRACE("Recording... not performing anything.\n");
1742 device->updateStateBlock->changed.transform[d3dts >> 5] |= 1 << (d3dts & 0x1f);
1743 device->updateStateBlock->state.transforms[d3dts] = *matrix;
1747 /* If the new matrix is the same as the current one,
1748 * we cut off any further processing. this seems to be a reasonable
1749 * optimization because as was noticed, some apps (warcraft3 for example)
1750 * tend towards setting the same matrix repeatedly for some reason.
1752 * From here on we assume that the new matrix is different, wherever it matters. */
1753 if (!memcmp(&device->stateBlock->state.transforms[d3dts].u.m[0][0], matrix, sizeof(*matrix)))
1755 TRACE("The application is setting the same matrix over again.\n");
1759 device->stateBlock->state.transforms[d3dts] = *matrix;
1760 if (d3dts == WINED3D_TS_VIEW)
1761 device->view_ident = !memcmp(matrix, &identity, sizeof(identity));
1763 if (d3dts < WINED3D_TS_WORLD_MATRIX(device->adapter->gl_info.limits.blends))
1764 device_invalidate_state(device, STATE_TRANSFORM(d3dts));
1767 void CDECL wined3d_device_get_transform(const struct wined3d_device *device,
1768 enum wined3d_transform_state state, struct wined3d_matrix *matrix)
1770 TRACE("device %p, state %s, matrix %p.\n", device, debug_d3dtstype(state), matrix);
1772 *matrix = device->stateBlock->state.transforms[state];
1775 void CDECL wined3d_device_multiply_transform(struct wined3d_device *device,
1776 enum wined3d_transform_state state, const struct wined3d_matrix *matrix)
1778 const struct wined3d_matrix *mat;
1779 struct wined3d_matrix temp;
1781 TRACE("device %p, state %s, matrix %p.\n", device, debug_d3dtstype(state), matrix);
1783 /* Note: Using 'updateStateBlock' rather than 'stateblock' in the code
1784 * below means it will be recorded in a state block change, but it
1785 * works regardless where it is recorded.
1786 * If this is found to be wrong, change to StateBlock. */
1787 if (state > HIGHEST_TRANSFORMSTATE)
1789 WARN("Unhandled transform state %#x.\n", state);
1793 mat = &device->updateStateBlock->state.transforms[state];
1794 multiply_matrix(&temp, mat, matrix);
1796 /* Apply change via set transform - will reapply to eg. lights this way. */
1797 wined3d_device_set_transform(device, state, &temp);
1800 /* Note lights are real special cases. Although the device caps state only
1801 * e.g. 8 are supported, you can reference any indexes you want as long as
1802 * that number max are enabled at any one point in time. Therefore since the
1803 * indices can be anything, we need a hashmap of them. However, this causes
1804 * stateblock problems. When capturing the state block, I duplicate the
1805 * hashmap, but when recording, just build a chain pretty much of commands to
1807 HRESULT CDECL wined3d_device_set_light(struct wined3d_device *device,
1808 UINT light_idx, const struct wined3d_light *light)
1810 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1811 struct wined3d_light_info *object = NULL;
1815 TRACE("device %p, light_idx %u, light %p.\n", device, light_idx, light);
1817 /* Check the parameter range. Need for speed most wanted sets junk lights
1818 * which confuse the GL driver. */
1820 return WINED3DERR_INVALIDCALL;
1822 switch (light->type)
1824 case WINED3D_LIGHT_POINT:
1825 case WINED3D_LIGHT_SPOT:
1826 case WINED3D_LIGHT_PARALLELPOINT:
1827 case WINED3D_LIGHT_GLSPOT:
1828 /* Incorrect attenuation values can cause the gl driver to crash.
1829 * Happens with Need for speed most wanted. */
1830 if (light->attenuation0 < 0.0f || light->attenuation1 < 0.0f || light->attenuation2 < 0.0f)
1832 WARN("Attenuation is negative, returning WINED3DERR_INVALIDCALL.\n");
1833 return WINED3DERR_INVALIDCALL;
1837 case WINED3D_LIGHT_DIRECTIONAL:
1838 /* Ignores attenuation */
1842 WARN("Light type out of range, returning WINED3DERR_INVALIDCALL\n");
1843 return WINED3DERR_INVALIDCALL;
1846 LIST_FOR_EACH(e, &device->updateStateBlock->state.light_map[hash_idx])
1848 object = LIST_ENTRY(e, struct wined3d_light_info, entry);
1849 if (object->OriginalIndex == light_idx)
1856 TRACE("Adding new light\n");
1857 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1860 ERR("Out of memory error when allocating a light\n");
1861 return E_OUTOFMEMORY;
1863 list_add_head(&device->updateStateBlock->state.light_map[hash_idx], &object->entry);
1864 object->glIndex = -1;
1865 object->OriginalIndex = light_idx;
1868 /* Initialize the object. */
1869 TRACE("Light %d setting to type %d, Diffuse(%f,%f,%f,%f), Specular(%f,%f,%f,%f), Ambient(%f,%f,%f,%f)\n",
1870 light_idx, light->type,
1871 light->diffuse.r, light->diffuse.g, light->diffuse.b, light->diffuse.a,
1872 light->specular.r, light->specular.g, light->specular.b, light->specular.a,
1873 light->ambient.r, light->ambient.g, light->ambient.b, light->ambient.a);
1874 TRACE("... Pos(%f,%f,%f), Dir(%f,%f,%f)\n", light->position.x, light->position.y, light->position.z,
1875 light->direction.x, light->direction.y, light->direction.z);
1876 TRACE("... Range(%f), Falloff(%f), Theta(%f), Phi(%f)\n",
1877 light->range, light->falloff, light->theta, light->phi);
1879 /* Save away the information. */
1880 object->OriginalParms = *light;
1882 switch (light->type)
1884 case WINED3D_LIGHT_POINT:
1886 object->lightPosn[0] = light->position.x;
1887 object->lightPosn[1] = light->position.y;
1888 object->lightPosn[2] = light->position.z;
1889 object->lightPosn[3] = 1.0f;
1890 object->cutoff = 180.0f;
1894 case WINED3D_LIGHT_DIRECTIONAL:
1896 object->lightPosn[0] = -light->direction.x;
1897 object->lightPosn[1] = -light->direction.y;
1898 object->lightPosn[2] = -light->direction.z;
1899 object->lightPosn[3] = 0.0f;
1900 object->exponent = 0.0f;
1901 object->cutoff = 180.0f;
1904 case WINED3D_LIGHT_SPOT:
1906 object->lightPosn[0] = light->position.x;
1907 object->lightPosn[1] = light->position.y;
1908 object->lightPosn[2] = light->position.z;
1909 object->lightPosn[3] = 1.0f;
1912 object->lightDirn[0] = light->direction.x;
1913 object->lightDirn[1] = light->direction.y;
1914 object->lightDirn[2] = light->direction.z;
1915 object->lightDirn[3] = 1.0f;
1917 /* opengl-ish and d3d-ish spot lights use too different models
1918 * for the light "intensity" as a function of the angle towards
1919 * the main light direction, so we only can approximate very
1920 * roughly. However, spot lights are rather rarely used in games
1921 * (if ever used at all). Furthermore if still used, probably
1922 * nobody pays attention to such details. */
1923 if (!light->falloff)
1925 /* Falloff = 0 is easy, because d3d's and opengl's spot light
1926 * equations have the falloff resp. exponent parameter as an
1927 * exponent, so the spot light lighting will always be 1.0 for
1928 * both of them, and we don't have to care for the rest of the
1929 * rather complex calculation. */
1930 object->exponent = 0.0f;
1934 rho = light->theta + (light->phi - light->theta) / (2 * light->falloff);
1937 object->exponent = -0.3f / logf(cosf(rho / 2));
1940 if (object->exponent > 128.0f)
1941 object->exponent = 128.0f;
1943 object->cutoff = (float)(light->phi * 90 / M_PI);
1948 FIXME("Unrecognized light type %#x.\n", light->type);
1951 /* Update the live definitions if the light is currently assigned a glIndex. */
1952 if (object->glIndex != -1 && !device->isRecordingState)
1953 device_invalidate_state(device, STATE_ACTIVELIGHT(object->glIndex));
1958 HRESULT CDECL wined3d_device_get_light(const struct wined3d_device *device,
1959 UINT light_idx, struct wined3d_light *light)
1961 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1962 struct wined3d_light_info *light_info = NULL;
1965 TRACE("device %p, light_idx %u, light %p.\n", device, light_idx, light);
1967 LIST_FOR_EACH(e, &device->stateBlock->state.light_map[hash_idx])
1969 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
1970 if (light_info->OriginalIndex == light_idx)
1977 TRACE("Light information requested but light not defined\n");
1978 return WINED3DERR_INVALIDCALL;
1981 *light = light_info->OriginalParms;
1985 HRESULT CDECL wined3d_device_set_light_enable(struct wined3d_device *device, UINT light_idx, BOOL enable)
1987 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1988 struct wined3d_light_info *light_info = NULL;
1991 TRACE("device %p, light_idx %u, enable %#x.\n", device, light_idx, enable);
1993 LIST_FOR_EACH(e, &device->updateStateBlock->state.light_map[hash_idx])
1995 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
1996 if (light_info->OriginalIndex == light_idx)
2000 TRACE("Found light %p.\n", light_info);
2002 /* Special case - enabling an undefined light creates one with a strict set of parameters. */
2005 TRACE("Light enabled requested but light not defined, so defining one!\n");
2006 wined3d_device_set_light(device, light_idx, &WINED3D_default_light);
2008 /* Search for it again! Should be fairly quick as near head of list. */
2009 LIST_FOR_EACH(e, &device->updateStateBlock->state.light_map[hash_idx])
2011 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
2012 if (light_info->OriginalIndex == light_idx)
2018 FIXME("Adding default lights has failed dismally\n");
2019 return WINED3DERR_INVALIDCALL;
2025 if (light_info->glIndex != -1)
2027 if (!device->isRecordingState)
2028 device_invalidate_state(device, STATE_ACTIVELIGHT(light_info->glIndex));
2030 device->updateStateBlock->state.lights[light_info->glIndex] = NULL;
2031 light_info->glIndex = -1;
2035 TRACE("Light already disabled, nothing to do\n");
2037 light_info->enabled = FALSE;
2041 light_info->enabled = TRUE;
2042 if (light_info->glIndex != -1)
2044 TRACE("Nothing to do as light was enabled\n");
2049 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
2050 /* Find a free GL light. */
2051 for (i = 0; i < gl_info->limits.lights; ++i)
2053 if (!device->updateStateBlock->state.lights[i])
2055 device->updateStateBlock->state.lights[i] = light_info;
2056 light_info->glIndex = i;
2060 if (light_info->glIndex == -1)
2062 /* Our tests show that Windows returns D3D_OK in this situation, even with
2063 * D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE devices. This
2064 * is consistent among ddraw, d3d8 and d3d9. GetLightEnable returns TRUE
2065 * as well for those lights.
2067 * TODO: Test how this affects rendering. */
2068 WARN("Too many concurrently active lights\n");
2072 /* i == light_info->glIndex */
2073 if (!device->isRecordingState)
2074 device_invalidate_state(device, STATE_ACTIVELIGHT(i));
2081 HRESULT CDECL wined3d_device_get_light_enable(const struct wined3d_device *device, UINT light_idx, BOOL *enable)
2083 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
2084 struct wined3d_light_info *light_info = NULL;
2087 TRACE("device %p, light_idx %u, enable %p.\n", device, light_idx, enable);
2089 LIST_FOR_EACH(e, &device->stateBlock->state.light_map[hash_idx])
2091 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
2092 if (light_info->OriginalIndex == light_idx)
2099 TRACE("Light enabled state requested but light not defined.\n");
2100 return WINED3DERR_INVALIDCALL;
2102 /* true is 128 according to SetLightEnable */
2103 *enable = light_info->enabled ? 128 : 0;
2107 HRESULT CDECL wined3d_device_set_clip_plane(struct wined3d_device *device,
2108 UINT plane_idx, const struct wined3d_vec4 *plane)
2110 TRACE("device %p, plane_idx %u, plane %p.\n", device, plane_idx, plane);
2112 /* Validate plane_idx. */
2113 if (plane_idx >= device->adapter->gl_info.limits.clipplanes)
2115 TRACE("Application has requested clipplane this device doesn't support.\n");
2116 return WINED3DERR_INVALIDCALL;
2119 device->updateStateBlock->changed.clipplane |= 1 << plane_idx;
2121 if (!memcmp(&device->updateStateBlock->state.clip_planes[plane_idx], plane, sizeof(*plane)))
2123 TRACE("Application is setting old values over, nothing to do.\n");
2127 device->updateStateBlock->state.clip_planes[plane_idx] = *plane;
2129 /* Handle recording of state blocks. */
2130 if (device->isRecordingState)
2132 TRACE("Recording... not performing anything.\n");
2136 device_invalidate_state(device, STATE_CLIPPLANE(plane_idx));
2141 HRESULT CDECL wined3d_device_get_clip_plane(const struct wined3d_device *device,
2142 UINT plane_idx, struct wined3d_vec4 *plane)
2144 TRACE("device %p, plane_idx %u, plane %p.\n", device, plane_idx, plane);
2146 /* Validate plane_idx. */
2147 if (plane_idx >= device->adapter->gl_info.limits.clipplanes)
2149 TRACE("Application has requested clipplane this device doesn't support.\n");
2150 return WINED3DERR_INVALIDCALL;
2153 *plane = device->stateBlock->state.clip_planes[plane_idx];
2158 HRESULT CDECL wined3d_device_set_clip_status(struct wined3d_device *device,
2159 const struct wined3d_clip_status *clip_status)
2161 FIXME("device %p, clip_status %p stub!\n", device, clip_status);
2164 return WINED3DERR_INVALIDCALL;
2169 HRESULT CDECL wined3d_device_get_clip_status(const struct wined3d_device *device,
2170 struct wined3d_clip_status *clip_status)
2172 FIXME("device %p, clip_status %p stub!\n", device, clip_status);
2175 return WINED3DERR_INVALIDCALL;
2180 void CDECL wined3d_device_set_material(struct wined3d_device *device, const struct wined3d_material *material)
2182 TRACE("device %p, material %p.\n", device, material);
2184 device->updateStateBlock->changed.material = TRUE;
2185 device->updateStateBlock->state.material = *material;
2187 /* Handle recording of state blocks */
2188 if (device->isRecordingState)
2190 TRACE("Recording... not performing anything.\n");
2194 device_invalidate_state(device, STATE_MATERIAL);
2197 void CDECL wined3d_device_get_material(const struct wined3d_device *device, struct wined3d_material *material)
2199 TRACE("device %p, material %p.\n", device, material);
2201 *material = device->updateStateBlock->state.material;
2203 TRACE("diffuse {%.8e, %.8e, %.8e, %.8e}\n",
2204 material->diffuse.r, material->diffuse.g,
2205 material->diffuse.b, material->diffuse.a);
2206 TRACE("ambient {%.8e, %.8e, %.8e, %.8e}\n",
2207 material->ambient.r, material->ambient.g,
2208 material->ambient.b, material->ambient.a);
2209 TRACE("specular {%.8e, %.8e, %.8e, %.8e}\n",
2210 material->specular.r, material->specular.g,
2211 material->specular.b, material->specular.a);
2212 TRACE("emissive {%.8e, %.8e, %.8e, %.8e}\n",
2213 material->emissive.r, material->emissive.g,
2214 material->emissive.b, material->emissive.a);
2215 TRACE("power %.8e.\n", material->power);
2218 void CDECL wined3d_device_set_index_buffer(struct wined3d_device *device,
2219 struct wined3d_buffer *buffer, enum wined3d_format_id format_id)
2221 struct wined3d_buffer *prev_buffer;
2223 TRACE("device %p, buffer %p, format %s.\n",
2224 device, buffer, debug_d3dformat(format_id));
2226 prev_buffer = device->updateStateBlock->state.index_buffer;
2228 device->updateStateBlock->changed.indices = TRUE;
2229 device->updateStateBlock->state.index_buffer = buffer;
2230 device->updateStateBlock->state.index_format = format_id;
2232 /* Handle recording of state blocks. */
2233 if (device->isRecordingState)
2235 TRACE("Recording... not performing anything.\n");
2237 wined3d_buffer_incref(buffer);
2239 wined3d_buffer_decref(prev_buffer);
2243 if (prev_buffer != buffer)
2245 device_invalidate_state(device, STATE_INDEXBUFFER);
2248 InterlockedIncrement(&buffer->resource.bind_count);
2249 wined3d_buffer_incref(buffer);
2253 InterlockedDecrement(&prev_buffer->resource.bind_count);
2254 wined3d_buffer_decref(prev_buffer);
2259 struct wined3d_buffer * CDECL wined3d_device_get_index_buffer(const struct wined3d_device *device,
2260 enum wined3d_format_id *format)
2262 TRACE("device %p, format %p.\n", device, format);
2264 *format = device->stateBlock->state.index_format;
2265 return device->stateBlock->state.index_buffer;
2268 void CDECL wined3d_device_set_base_vertex_index(struct wined3d_device *device, INT base_index)
2270 TRACE("device %p, base_index %d.\n", device, base_index);
2272 device->updateStateBlock->state.base_vertex_index = base_index;
2275 INT CDECL wined3d_device_get_base_vertex_index(const struct wined3d_device *device)
2277 TRACE("device %p.\n", device);
2279 return device->stateBlock->state.base_vertex_index;
2282 void CDECL wined3d_device_set_viewport(struct wined3d_device *device, const struct wined3d_viewport *viewport)
2284 TRACE("device %p, viewport %p.\n", device, viewport);
2285 TRACE("x %u, y %u, w %u, h %u, min_z %.8e, max_z %.8e.\n",
2286 viewport->x, viewport->y, viewport->width, viewport->height, viewport->min_z, viewport->max_z);
2288 device->updateStateBlock->changed.viewport = TRUE;
2289 device->updateStateBlock->state.viewport = *viewport;
2291 /* Handle recording of state blocks */
2292 if (device->isRecordingState)
2294 TRACE("Recording... not performing anything\n");
2298 device_invalidate_state(device, STATE_VIEWPORT);
2301 void CDECL wined3d_device_get_viewport(const struct wined3d_device *device, struct wined3d_viewport *viewport)
2303 TRACE("device %p, viewport %p.\n", device, viewport);
2305 *viewport = device->stateBlock->state.viewport;
2308 void CDECL wined3d_device_set_render_state(struct wined3d_device *device,
2309 enum wined3d_render_state state, DWORD value)
2311 DWORD old_value = device->stateBlock->state.render_states[state];
2313 TRACE("device %p, state %s (%#x), value %#x.\n", device, debug_d3drenderstate(state), state, value);
2315 device->updateStateBlock->changed.renderState[state >> 5] |= 1 << (state & 0x1f);
2316 device->updateStateBlock->state.render_states[state] = value;
2318 /* Handle recording of state blocks. */
2319 if (device->isRecordingState)
2321 TRACE("Recording... not performing anything.\n");
2325 /* Compared here and not before the assignment to allow proper stateblock recording. */
2326 if (value == old_value)
2327 TRACE("Application is setting the old value over, nothing to do.\n");
2329 device_invalidate_state(device, STATE_RENDER(state));
2332 DWORD CDECL wined3d_device_get_render_state(const struct wined3d_device *device, enum wined3d_render_state state)
2334 TRACE("device %p, state %s (%#x).\n", device, debug_d3drenderstate(state), state);
2336 return device->stateBlock->state.render_states[state];
2339 void CDECL wined3d_device_set_sampler_state(struct wined3d_device *device,
2340 UINT sampler_idx, enum wined3d_sampler_state state, DWORD value)
2344 TRACE("device %p, sampler_idx %u, state %s, value %#x.\n",
2345 device, sampler_idx, debug_d3dsamplerstate(state), value);
2347 if (sampler_idx >= WINED3DVERTEXTEXTURESAMPLER0 && sampler_idx <= WINED3DVERTEXTEXTURESAMPLER3)
2348 sampler_idx -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
2350 if (sampler_idx >= sizeof(device->stateBlock->state.sampler_states)
2351 / sizeof(*device->stateBlock->state.sampler_states))
2353 WARN("Invalid sampler %u.\n", sampler_idx);
2354 return; /* Windows accepts overflowing this array ... we do not. */
2357 old_value = device->stateBlock->state.sampler_states[sampler_idx][state];
2358 device->updateStateBlock->state.sampler_states[sampler_idx][state] = value;
2359 device->updateStateBlock->changed.samplerState[sampler_idx] |= 1 << state;
2361 /* Handle recording of state blocks. */
2362 if (device->isRecordingState)
2364 TRACE("Recording... not performing anything.\n");
2368 if (old_value == value)
2370 TRACE("Application is setting the old value over, nothing to do.\n");
2374 device_invalidate_state(device, STATE_SAMPLER(sampler_idx));
2377 DWORD CDECL wined3d_device_get_sampler_state(const struct wined3d_device *device,
2378 UINT sampler_idx, enum wined3d_sampler_state state)
2380 TRACE("device %p, sampler_idx %u, state %s.\n",
2381 device, sampler_idx, debug_d3dsamplerstate(state));
2383 if (sampler_idx >= WINED3DVERTEXTEXTURESAMPLER0 && sampler_idx <= WINED3DVERTEXTEXTURESAMPLER3)
2384 sampler_idx -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
2386 if (sampler_idx >= sizeof(device->stateBlock->state.sampler_states)
2387 / sizeof(*device->stateBlock->state.sampler_states))
2389 WARN("Invalid sampler %u.\n", sampler_idx);
2390 return 0; /* Windows accepts overflowing this array ... we do not. */
2393 return device->stateBlock->state.sampler_states[sampler_idx][state];
2396 void CDECL wined3d_device_set_scissor_rect(struct wined3d_device *device, const RECT *rect)
2398 TRACE("device %p, rect %s.\n", device, wine_dbgstr_rect(rect));
2400 device->updateStateBlock->changed.scissorRect = TRUE;
2401 if (EqualRect(&device->updateStateBlock->state.scissor_rect, rect))
2403 TRACE("App is setting the old scissor rectangle over, nothing to do.\n");
2406 CopyRect(&device->updateStateBlock->state.scissor_rect, rect);
2408 if (device->isRecordingState)
2410 TRACE("Recording... not performing anything.\n");
2414 device_invalidate_state(device, STATE_SCISSORRECT);
2417 void CDECL wined3d_device_get_scissor_rect(const struct wined3d_device *device, RECT *rect)
2419 TRACE("device %p, rect %p.\n", device, rect);
2421 *rect = device->updateStateBlock->state.scissor_rect;
2422 TRACE("Returning rect %s.\n", wine_dbgstr_rect(rect));
2425 void CDECL wined3d_device_set_vertex_declaration(struct wined3d_device *device,
2426 struct wined3d_vertex_declaration *declaration)
2428 struct wined3d_vertex_declaration *prev = device->updateStateBlock->state.vertex_declaration;
2430 TRACE("device %p, declaration %p.\n", device, declaration);
2433 wined3d_vertex_declaration_incref(declaration);
2435 wined3d_vertex_declaration_decref(prev);
2437 device->updateStateBlock->state.vertex_declaration = declaration;
2438 device->updateStateBlock->changed.vertexDecl = TRUE;
2440 if (device->isRecordingState)
2442 TRACE("Recording... not performing anything.\n");
2446 if (declaration == prev)
2448 /* Checked after the assignment to allow proper stateblock recording. */
2449 TRACE("Application is setting the old declaration over, nothing to do.\n");
2453 device_invalidate_state(device, STATE_VDECL);
2456 struct wined3d_vertex_declaration * CDECL wined3d_device_get_vertex_declaration(const struct wined3d_device *device)
2458 TRACE("device %p.\n", device);
2460 return device->stateBlock->state.vertex_declaration;
2463 void CDECL wined3d_device_set_vertex_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2465 struct wined3d_shader *prev = device->updateStateBlock->state.vertex_shader;
2467 TRACE("device %p, shader %p.\n", device, shader);
2470 wined3d_shader_incref(shader);
2472 wined3d_shader_decref(prev);
2474 device->updateStateBlock->state.vertex_shader = shader;
2475 device->updateStateBlock->changed.vertexShader = TRUE;
2477 if (device->isRecordingState)
2479 TRACE("Recording... not performing anything.\n");
2485 TRACE("Application is setting the old shader over, nothing to do.\n");
2489 device_invalidate_state(device, STATE_VSHADER);
2492 struct wined3d_shader * CDECL wined3d_device_get_vertex_shader(const struct wined3d_device *device)
2494 TRACE("device %p.\n", device);
2496 return device->stateBlock->state.vertex_shader;
2499 void CDECL wined3d_device_set_vs_cb(struct wined3d_device *device, UINT idx, struct wined3d_buffer *buffer)
2501 struct wined3d_buffer *prev;
2503 TRACE("device %p, idx %u, buffer %p.\n", device, idx, buffer);
2505 if (idx >= MAX_CONSTANT_BUFFERS)
2507 WARN("Invalid constant buffer index %u.\n", idx);
2511 prev = device->updateStateBlock->state.vs_cb[idx];
2512 device->updateStateBlock->state.vs_cb[idx] = buffer;
2514 if (device->isRecordingState)
2517 wined3d_buffer_incref(buffer);
2519 wined3d_buffer_decref(prev);
2527 InterlockedIncrement(&buffer->resource.bind_count);
2528 wined3d_buffer_incref(buffer);
2532 InterlockedDecrement(&prev->resource.bind_count);
2533 wined3d_buffer_decref(prev);
2538 struct wined3d_buffer * CDECL wined3d_device_get_vs_cb(const struct wined3d_device *device, UINT idx)
2540 TRACE("device %p, idx %u.\n", device, idx);
2542 if (idx >= MAX_CONSTANT_BUFFERS)
2544 WARN("Invalid constant buffer index %u.\n", idx);
2548 return device->stateBlock->state.vs_cb[idx];
2551 void CDECL wined3d_device_set_vs_sampler(struct wined3d_device *device, UINT idx, struct wined3d_sampler *sampler)
2553 struct wined3d_sampler *prev;
2555 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2557 if (idx >= MAX_SAMPLER_OBJECTS)
2559 WARN("Invalid sampler index %u.\n", idx);
2563 prev = device->updateStateBlock->state.vs_sampler[idx];
2564 device->updateStateBlock->state.vs_sampler[idx] = sampler;
2567 wined3d_sampler_incref(sampler);
2569 wined3d_sampler_decref(prev);
2572 struct wined3d_sampler * CDECL wined3d_device_get_vs_sampler(const struct wined3d_device *device, UINT idx)
2574 TRACE("device %p, idx %u.\n", device, idx);
2576 if (idx >= MAX_SAMPLER_OBJECTS)
2578 WARN("Invalid sampler index %u.\n", idx);
2582 return device->stateBlock->state.vs_sampler[idx];
2585 HRESULT CDECL wined3d_device_set_vs_consts_b(struct wined3d_device *device,
2586 UINT start_register, const BOOL *constants, UINT bool_count)
2588 UINT count = min(bool_count, MAX_CONST_B - start_register);
2591 TRACE("device %p, start_register %u, constants %p, bool_count %u.\n",
2592 device, start_register, constants, bool_count);
2594 if (!constants || start_register >= MAX_CONST_B)
2595 return WINED3DERR_INVALIDCALL;
2597 memcpy(&device->updateStateBlock->state.vs_consts_b[start_register], constants, count * sizeof(BOOL));
2598 for (i = 0; i < count; ++i)
2599 TRACE("Set BOOL constant %u to %s.\n", start_register + i, constants[i] ? "true" : "false");
2601 for (i = start_register; i < count + start_register; ++i)
2602 device->updateStateBlock->changed.vertexShaderConstantsB |= (1 << i);
2604 if (!device->isRecordingState)
2605 device_invalidate_state(device, STATE_VERTEXSHADERCONSTANT);
2610 HRESULT CDECL wined3d_device_get_vs_consts_b(const struct wined3d_device *device,
2611 UINT start_register, BOOL *constants, UINT bool_count)
2613 UINT count = min(bool_count, MAX_CONST_B - start_register);
2615 TRACE("device %p, start_register %u, constants %p, bool_count %u.\n",
2616 device, start_register, constants, bool_count);
2618 if (!constants || start_register >= MAX_CONST_B)
2619 return WINED3DERR_INVALIDCALL;
2621 memcpy(constants, &device->stateBlock->state.vs_consts_b[start_register], count * sizeof(BOOL));
2626 HRESULT CDECL wined3d_device_set_vs_consts_i(struct wined3d_device *device,
2627 UINT start_register, const int *constants, UINT vector4i_count)
2629 UINT count = min(vector4i_count, MAX_CONST_I - start_register);
2632 TRACE("device %p, start_register %u, constants %p, vector4i_count %u.\n",
2633 device, start_register, constants, vector4i_count);
2635 if (!constants || start_register >= MAX_CONST_I)
2636 return WINED3DERR_INVALIDCALL;
2638 memcpy(&device->updateStateBlock->state.vs_consts_i[start_register * 4], constants, count * sizeof(int) * 4);
2639 for (i = 0; i < count; ++i)
2640 TRACE("Set INT constant %u to {%d, %d, %d, %d}.\n", start_register + i,
2641 constants[i * 4], constants[i * 4 + 1],
2642 constants[i * 4 + 2], constants[i * 4 + 3]);
2644 for (i = start_register; i < count + start_register; ++i)
2645 device->updateStateBlock->changed.vertexShaderConstantsI |= (1 << i);
2647 if (!device->isRecordingState)
2648 device_invalidate_state(device, STATE_VERTEXSHADERCONSTANT);
2653 HRESULT CDECL wined3d_device_get_vs_consts_i(const struct wined3d_device *device,
2654 UINT start_register, int *constants, UINT vector4i_count)
2656 UINT count = min(vector4i_count, MAX_CONST_I - start_register);
2658 TRACE("device %p, start_register %u, constants %p, vector4i_count %u.\n",
2659 device, start_register, constants, vector4i_count);
2661 if (!constants || start_register >= MAX_CONST_I)
2662 return WINED3DERR_INVALIDCALL;
2664 memcpy(constants, &device->stateBlock->state.vs_consts_i[start_register * 4], count * sizeof(int) * 4);
2668 HRESULT CDECL wined3d_device_set_vs_consts_f(struct wined3d_device *device,
2669 UINT start_register, const float *constants, UINT vector4f_count)
2673 TRACE("device %p, start_register %u, constants %p, vector4f_count %u.\n",
2674 device, start_register, constants, vector4f_count);
2676 /* Specifically test start_register > limit to catch MAX_UINT overflows
2677 * when adding start_register + vector4f_count. */
2679 || start_register + vector4f_count > device->d3d_vshader_constantF
2680 || start_register > device->d3d_vshader_constantF)
2681 return WINED3DERR_INVALIDCALL;
2683 memcpy(&device->updateStateBlock->state.vs_consts_f[start_register * 4],
2684 constants, vector4f_count * sizeof(float) * 4);
2687 for (i = 0; i < vector4f_count; ++i)
2688 TRACE("Set FLOAT constant %u to {%.8e, %.8e, %.8e, %.8e}.\n", start_register + i,
2689 constants[i * 4], constants[i * 4 + 1],
2690 constants[i * 4 + 2], constants[i * 4 + 3]);
2693 if (!device->isRecordingState)
2695 device->shader_backend->shader_update_float_vertex_constants(device, start_register, vector4f_count);
2696 device_invalidate_state(device, STATE_VERTEXSHADERCONSTANT);
2699 memset(device->updateStateBlock->changed.vertexShaderConstantsF + start_register, 1,
2700 sizeof(*device->updateStateBlock->changed.vertexShaderConstantsF) * vector4f_count);
2705 HRESULT CDECL wined3d_device_get_vs_consts_f(const struct wined3d_device *device,
2706 UINT start_register, float *constants, UINT vector4f_count)
2708 int count = min(vector4f_count, device->d3d_vshader_constantF - start_register);
2710 TRACE("device %p, start_register %u, constants %p, vector4f_count %u.\n",
2711 device, start_register, constants, vector4f_count);
2713 if (!constants || count < 0)
2714 return WINED3DERR_INVALIDCALL;
2716 memcpy(constants, &device->stateBlock->state.vs_consts_f[start_register * 4], count * sizeof(float) * 4);
2721 static void device_invalidate_texture_stage(const struct wined3d_device *device, DWORD stage)
2725 for (i = 0; i <= WINED3D_HIGHEST_TEXTURE_STATE; ++i)
2727 device_invalidate_state(device, STATE_TEXTURESTAGE(stage, i));
2731 static void device_map_stage(struct wined3d_device *device, DWORD stage, DWORD unit)
2733 DWORD i = device->rev_tex_unit_map[unit];
2734 DWORD j = device->texUnitMap[stage];
2736 device->texUnitMap[stage] = unit;
2737 if (i != WINED3D_UNMAPPED_STAGE && i != stage)
2738 device->texUnitMap[i] = WINED3D_UNMAPPED_STAGE;
2740 device->rev_tex_unit_map[unit] = stage;
2741 if (j != WINED3D_UNMAPPED_STAGE && j != unit)
2742 device->rev_tex_unit_map[j] = WINED3D_UNMAPPED_STAGE;
2745 static void device_update_fixed_function_usage_map(struct wined3d_device *device)
2749 device->fixed_function_usage_map = 0;
2750 for (i = 0; i < MAX_TEXTURES; ++i)
2752 const struct wined3d_state *state = &device->stateBlock->state;
2753 enum wined3d_texture_op color_op = state->texture_states[i][WINED3D_TSS_COLOR_OP];
2754 enum wined3d_texture_op alpha_op = state->texture_states[i][WINED3D_TSS_ALPHA_OP];
2755 DWORD color_arg1 = state->texture_states[i][WINED3D_TSS_COLOR_ARG1] & WINED3DTA_SELECTMASK;
2756 DWORD color_arg2 = state->texture_states[i][WINED3D_TSS_COLOR_ARG2] & WINED3DTA_SELECTMASK;
2757 DWORD color_arg3 = state->texture_states[i][WINED3D_TSS_COLOR_ARG0] & WINED3DTA_SELECTMASK;
2758 DWORD alpha_arg1 = state->texture_states[i][WINED3D_TSS_ALPHA_ARG1] & WINED3DTA_SELECTMASK;
2759 DWORD alpha_arg2 = state->texture_states[i][WINED3D_TSS_ALPHA_ARG2] & WINED3DTA_SELECTMASK;
2760 DWORD alpha_arg3 = state->texture_states[i][WINED3D_TSS_ALPHA_ARG0] & WINED3DTA_SELECTMASK;
2762 /* Not used, and disable higher stages. */
2763 if (color_op == WINED3D_TOP_DISABLE)
2766 if (((color_arg1 == WINED3DTA_TEXTURE) && color_op != WINED3D_TOP_SELECT_ARG2)
2767 || ((color_arg2 == WINED3DTA_TEXTURE) && color_op != WINED3D_TOP_SELECT_ARG1)
2768 || ((color_arg3 == WINED3DTA_TEXTURE)
2769 && (color_op == WINED3D_TOP_MULTIPLY_ADD || color_op == WINED3D_TOP_LERP))
2770 || ((alpha_arg1 == WINED3DTA_TEXTURE) && alpha_op != WINED3D_TOP_SELECT_ARG2)
2771 || ((alpha_arg2 == WINED3DTA_TEXTURE) && alpha_op != WINED3D_TOP_SELECT_ARG1)
2772 || ((alpha_arg3 == WINED3DTA_TEXTURE)
2773 && (alpha_op == WINED3D_TOP_MULTIPLY_ADD || alpha_op == WINED3D_TOP_LERP)))
2774 device->fixed_function_usage_map |= (1 << i);
2776 if ((color_op == WINED3D_TOP_BUMPENVMAP || color_op == WINED3D_TOP_BUMPENVMAP_LUMINANCE)
2777 && i < MAX_TEXTURES - 1)
2778 device->fixed_function_usage_map |= (1 << (i + 1));
2782 static void device_map_fixed_function_samplers(struct wined3d_device *device, const struct wined3d_gl_info *gl_info)
2784 unsigned int i, tex;
2787 device_update_fixed_function_usage_map(device);
2788 ffu_map = device->fixed_function_usage_map;
2790 if (device->max_ffp_textures == gl_info->limits.texture_stages
2791 || device->stateBlock->state.lowest_disabled_stage <= device->max_ffp_textures)
2793 for (i = 0; ffu_map; ffu_map >>= 1, ++i)
2795 if (!(ffu_map & 1)) continue;
2797 if (device->texUnitMap[i] != i)
2799 device_map_stage(device, i, i);
2800 device_invalidate_state(device, STATE_SAMPLER(i));
2801 device_invalidate_texture_stage(device, i);
2807 /* Now work out the mapping */
2809 for (i = 0; ffu_map; ffu_map >>= 1, ++i)
2811 if (!(ffu_map & 1)) continue;
2813 if (device->texUnitMap[i] != tex)
2815 device_map_stage(device, i, tex);
2816 device_invalidate_state(device, STATE_SAMPLER(i));
2817 device_invalidate_texture_stage(device, i);
2824 static void device_map_psamplers(struct wined3d_device *device, const struct wined3d_gl_info *gl_info)
2826 const enum wined3d_sampler_texture_type *sampler_type =
2827 device->stateBlock->state.pixel_shader->reg_maps.sampler_type;
2830 for (i = 0; i < MAX_FRAGMENT_SAMPLERS; ++i)
2832 if (sampler_type[i] && device->texUnitMap[i] != i)
2834 device_map_stage(device, i, i);
2835 device_invalidate_state(device, STATE_SAMPLER(i));
2836 if (i < gl_info->limits.texture_stages)
2837 device_invalidate_texture_stage(device, i);
2842 static BOOL device_unit_free_for_vs(const struct wined3d_device *device,
2843 const enum wined3d_sampler_texture_type *pshader_sampler_tokens,
2844 const enum wined3d_sampler_texture_type *vshader_sampler_tokens, DWORD unit)
2846 DWORD current_mapping = device->rev_tex_unit_map[unit];
2848 /* Not currently used */
2849 if (current_mapping == WINED3D_UNMAPPED_STAGE) return TRUE;
2851 if (current_mapping < MAX_FRAGMENT_SAMPLERS) {
2852 /* Used by a fragment sampler */
2854 if (!pshader_sampler_tokens) {
2855 /* No pixel shader, check fixed function */
2856 return current_mapping >= MAX_TEXTURES || !(device->fixed_function_usage_map & (1 << current_mapping));
2859 /* Pixel shader, check the shader's sampler map */
2860 return !pshader_sampler_tokens[current_mapping];
2863 /* Used by a vertex sampler */
2864 return !vshader_sampler_tokens[current_mapping - MAX_FRAGMENT_SAMPLERS];
2867 static void device_map_vsamplers(struct wined3d_device *device, BOOL ps, const struct wined3d_gl_info *gl_info)
2869 const enum wined3d_sampler_texture_type *vshader_sampler_type =
2870 device->stateBlock->state.vertex_shader->reg_maps.sampler_type;
2871 const enum wined3d_sampler_texture_type *pshader_sampler_type = NULL;
2872 int start = min(MAX_COMBINED_SAMPLERS, gl_info->limits.combined_samplers) - 1;
2877 /* Note that we only care if a sampler is sampled or not, not the sampler's specific type.
2878 * Otherwise we'd need to call shader_update_samplers() here for 1.x pixelshaders. */
2879 pshader_sampler_type = device->stateBlock->state.pixel_shader->reg_maps.sampler_type;
2882 for (i = 0; i < MAX_VERTEX_SAMPLERS; ++i) {
2883 DWORD vsampler_idx = i + MAX_FRAGMENT_SAMPLERS;
2884 if (vshader_sampler_type[i])
2886 if (device->texUnitMap[vsampler_idx] != WINED3D_UNMAPPED_STAGE)
2888 /* Already mapped somewhere */
2894 if (device_unit_free_for_vs(device, pshader_sampler_type, vshader_sampler_type, start))
2896 device_map_stage(device, vsampler_idx, start);
2897 device_invalidate_state(device, STATE_SAMPLER(vsampler_idx));
2909 void device_update_tex_unit_map(struct wined3d_device *device)
2911 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
2912 const struct wined3d_state *state = &device->stateBlock->state;
2913 BOOL vs = use_vs(state);
2914 BOOL ps = use_ps(state);
2917 * -> Pixel shaders need a 1:1 map. In theory the shader input could be mapped too, but
2918 * that would be really messy and require shader recompilation
2919 * -> When the mapping of a stage is changed, sampler and ALL texture stage states have
2920 * to be reset. Because of that try to work with a 1:1 mapping as much as possible
2923 device_map_psamplers(device, gl_info);
2925 device_map_fixed_function_samplers(device, gl_info);
2928 device_map_vsamplers(device, ps, gl_info);
2931 void CDECL wined3d_device_set_pixel_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2933 struct wined3d_shader *prev = device->updateStateBlock->state.pixel_shader;
2935 TRACE("device %p, shader %p.\n", device, shader);
2938 wined3d_shader_incref(shader);
2940 wined3d_shader_decref(prev);
2942 device->updateStateBlock->state.pixel_shader = shader;
2943 device->updateStateBlock->changed.pixelShader = TRUE;
2945 if (device->isRecordingState)
2947 TRACE("Recording... not performing anything.\n");
2953 TRACE("Application is setting the old shader over, nothing to do.\n");
2957 device_invalidate_state(device, STATE_PIXELSHADER);
2960 struct wined3d_shader * CDECL wined3d_device_get_pixel_shader(const struct wined3d_device *device)
2962 TRACE("device %p.\n", device);
2964 return device->stateBlock->state.pixel_shader;
2967 void CDECL wined3d_device_set_ps_cb(struct wined3d_device *device, UINT idx, struct wined3d_buffer *buffer)
2969 struct wined3d_buffer *prev;
2971 TRACE("device %p, idx %u, buffer %p.\n", device, idx, buffer);
2973 if (idx >= MAX_CONSTANT_BUFFERS)
2975 WARN("Invalid constant buffer index %u.\n", idx);
2979 prev = device->updateStateBlock->state.ps_cb[idx];
2980 device->updateStateBlock->state.ps_cb[idx] = buffer;
2982 if (device->isRecordingState)
2985 wined3d_buffer_incref(buffer);
2987 wined3d_buffer_decref(prev);
2995 InterlockedIncrement(&buffer->resource.bind_count);
2996 wined3d_buffer_incref(buffer);
3000 InterlockedDecrement(&prev->resource.bind_count);
3001 wined3d_buffer_decref(prev);
3006 struct wined3d_buffer * CDECL wined3d_device_get_ps_cb(const struct wined3d_device *device, UINT idx)
3008 TRACE("device %p, idx %u.\n", device, idx);
3010 if (idx >= MAX_CONSTANT_BUFFERS)
3012 WARN("Invalid constant buffer index %u.\n", idx);
3016 return device->stateBlock->state.ps_cb[idx];
3019 void CDECL wined3d_device_set_ps_sampler(struct wined3d_device *device, UINT idx, struct wined3d_sampler *sampler)
3021 struct wined3d_sampler *prev;
3023 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
3025 if (idx >= MAX_SAMPLER_OBJECTS)
3027 WARN("Invalid sampler index %u.\n", idx);
3031 prev = device->updateStateBlock->state.ps_sampler[idx];
3032 device->updateStateBlock->state.ps_sampler[idx] = sampler;
3035 wined3d_sampler_incref(sampler);
3037 wined3d_sampler_decref(prev);
3040 struct wined3d_sampler * CDECL wined3d_device_get_ps_sampler(const struct wined3d_device *device, UINT idx)
3042 TRACE("device %p, idx %u.\n", device, idx);
3044 if (idx >= MAX_SAMPLER_OBJECTS)
3046 WARN("Invalid sampler index %u.\n", idx);
3050 return device->stateBlock->state.ps_sampler[idx];
3053 HRESULT CDECL wined3d_device_set_ps_consts_b(struct wined3d_device *device,
3054 UINT start_register, const BOOL *constants, UINT bool_count)
3056 UINT count = min(bool_count, MAX_CONST_B - start_register);
3059 TRACE("device %p, start_register %u, constants %p, bool_count %u.\n",
3060 device, start_register, constants, bool_count);
3062 if (!constants || start_register >= MAX_CONST_B)
3063 return WINED3DERR_INVALIDCALL;
3065 memcpy(&device->updateStateBlock->state.ps_consts_b[start_register], constants, count * sizeof(BOOL));
3066 for (i = 0; i < count; ++i)
3067 TRACE("Set BOOL constant %u to %s.\n", start_register + i, constants[i] ? "true" : "false");
3069 for (i = start_register; i < count + start_register; ++i)
3070 device->updateStateBlock->changed.pixelShaderConstantsB |= (1 << i);
3072 if (!device->isRecordingState)
3073 device_invalidate_state(device, STATE_PIXELSHADERCONSTANT);
3078 HRESULT CDECL wined3d_device_get_ps_consts_b(const struct wined3d_device *device,
3079 UINT start_register, BOOL *constants, UINT bool_count)
3081 UINT count = min(bool_count, MAX_CONST_B - start_register);
3083 TRACE("device %p, start_register %u, constants %p, bool_count %u.\n",
3084 device, start_register, constants, bool_count);
3086 if (!constants || start_register >= MAX_CONST_B)
3087 return WINED3DERR_INVALIDCALL;
3089 memcpy(constants, &device->stateBlock->state.ps_consts_b[start_register], count * sizeof(BOOL));
3094 HRESULT CDECL wined3d_device_set_ps_consts_i(struct wined3d_device *device,
3095 UINT start_register, const int *constants, UINT vector4i_count)
3097 UINT count = min(vector4i_count, MAX_CONST_I - start_register);
3100 TRACE("device %p, start_register %u, constants %p, vector4i_count %u.\n",
3101 device, start_register, constants, vector4i_count);
3103 if (!constants || start_register >= MAX_CONST_I)
3104 return WINED3DERR_INVALIDCALL;
3106 memcpy(&device->updateStateBlock->state.ps_consts_i[start_register * 4], constants, count * sizeof(int) * 4);
3107 for (i = 0; i < count; ++i)
3108 TRACE("Set INT constant %u to {%d, %d, %d, %d}.\n", start_register + i,
3109 constants[i * 4], constants[i * 4 + 1],
3110 constants[i * 4 + 2], constants[i * 4 + 3]);
3112 for (i = start_register; i < count + start_register; ++i)
3113 device->updateStateBlock->changed.pixelShaderConstantsI |= (1 << i);
3115 if (!device->isRecordingState)
3116 device_invalidate_state(device, STATE_PIXELSHADERCONSTANT);
3121 HRESULT CDECL wined3d_device_get_ps_consts_i(const struct wined3d_device *device,
3122 UINT start_register, int *constants, UINT vector4i_count)
3124 UINT count = min(vector4i_count, MAX_CONST_I - start_register);
3126 TRACE("device %p, start_register %u, constants %p, vector4i_count %u.\n",
3127 device, start_register, constants, vector4i_count);
3129 if (!constants || start_register >= MAX_CONST_I)
3130 return WINED3DERR_INVALIDCALL;
3132 memcpy(constants, &device->stateBlock->state.ps_consts_i[start_register * 4], count * sizeof(int) * 4);
3137 HRESULT CDECL wined3d_device_set_ps_consts_f(struct wined3d_device *device,
3138 UINT start_register, const float *constants, UINT vector4f_count)
3142 TRACE("device %p, start_register %u, constants %p, vector4f_count %u.\n",
3143 device, start_register, constants, vector4f_count);
3145 /* Specifically test start_register > limit to catch MAX_UINT overflows
3146 * when adding start_register + vector4f_count. */
3148 || start_register + vector4f_count > device->d3d_pshader_constantF
3149 || start_register > device->d3d_pshader_constantF)
3150 return WINED3DERR_INVALIDCALL;
3152 memcpy(&device->updateStateBlock->state.ps_consts_f[start_register * 4],
3153 constants, vector4f_count * sizeof(float) * 4);
3156 for (i = 0; i < vector4f_count; ++i)
3157 TRACE("Set FLOAT constant %u to {%.8e, %.8e, %.8e, %.8e}.\n", start_register + i,
3158 constants[i * 4], constants[i * 4 + 1],
3159 constants[i * 4 + 2], constants[i * 4 + 3]);
3162 if (!device->isRecordingState)
3164 device->shader_backend->shader_update_float_pixel_constants(device, start_register, vector4f_count);
3165 device_invalidate_state(device, STATE_PIXELSHADERCONSTANT);
3168 memset(device->updateStateBlock->changed.pixelShaderConstantsF + start_register, 1,
3169 sizeof(*device->updateStateBlock->changed.pixelShaderConstantsF) * vector4f_count);
3174 HRESULT CDECL wined3d_device_get_ps_consts_f(const struct wined3d_device *device,
3175 UINT start_register, float *constants, UINT vector4f_count)
3177 int count = min(vector4f_count, device->d3d_pshader_constantF - start_register);
3179 TRACE("device %p, start_register %u, constants %p, vector4f_count %u.\n",
3180 device, start_register, constants, vector4f_count);
3182 if (!constants || count < 0)
3183 return WINED3DERR_INVALIDCALL;
3185 memcpy(constants, &device->stateBlock->state.ps_consts_f[start_register * 4], count * sizeof(float) * 4);
3190 void CDECL wined3d_device_set_geometry_shader(struct wined3d_device *device, struct wined3d_shader *shader)
3192 struct wined3d_shader *prev = device->updateStateBlock->state.geometry_shader;
3194 TRACE("device %p, shader %p.\n", device, shader);
3197 wined3d_shader_incref(shader);
3199 wined3d_shader_decref(prev);
3201 device->updateStateBlock->state.geometry_shader = shader;
3203 if (device->isRecordingState || shader == prev)
3206 device_invalidate_state(device, STATE_GEOMETRY_SHADER);
3209 struct wined3d_shader * CDECL wined3d_device_get_geometry_shader(const struct wined3d_device *device)
3211 TRACE("device %p.\n", device);
3213 return device->stateBlock->state.geometry_shader;
3216 void CDECL wined3d_device_set_gs_cb(struct wined3d_device *device, UINT idx, struct wined3d_buffer *buffer)
3218 struct wined3d_buffer *prev;
3220 TRACE("device %p, idx %u, buffer %p.\n", device, idx, buffer);
3222 if (idx >= MAX_CONSTANT_BUFFERS)
3224 WARN("Invalid constant buffer index %u.\n", idx);
3228 prev = device->updateStateBlock->state.gs_cb[idx];
3229 device->updateStateBlock->state.gs_cb[idx] = buffer;
3231 if (device->isRecordingState)
3234 wined3d_buffer_incref(buffer);
3236 wined3d_buffer_decref(prev);
3244 InterlockedIncrement(&buffer->resource.bind_count);
3245 wined3d_buffer_incref(buffer);
3249 InterlockedDecrement(&prev->resource.bind_count);
3250 wined3d_buffer_decref(prev);
3255 struct wined3d_buffer * CDECL wined3d_device_get_gs_cb(const struct wined3d_device *device, UINT idx)
3257 TRACE("device %p, idx %u.\n", device, idx);
3259 if (idx >= MAX_CONSTANT_BUFFERS)
3261 WARN("Invalid constant buffer index %u.\n", idx);
3265 return device->stateBlock->state.gs_cb[idx];
3268 void CDECL wined3d_device_set_gs_sampler(struct wined3d_device *device, UINT idx, struct wined3d_sampler *sampler)
3270 struct wined3d_sampler *prev;
3272 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
3274 if (idx >= MAX_SAMPLER_OBJECTS)
3276 WARN("Invalid sampler index %u.\n", idx);
3280 prev = device->updateStateBlock->state.gs_sampler[idx];
3281 device->updateStateBlock->state.gs_sampler[idx] = sampler;
3284 wined3d_sampler_incref(sampler);
3286 wined3d_sampler_decref(prev);
3289 struct wined3d_sampler * CDECL wined3d_device_get_gs_sampler(const struct wined3d_device *device, UINT idx)
3291 TRACE("device %p, idx %u.\n", device, idx);
3293 if (idx >= MAX_SAMPLER_OBJECTS)
3295 WARN("Invalid sampler index %u.\n", idx);
3299 return device->stateBlock->state.gs_sampler[idx];
3302 /* Context activation is done by the caller. */
3303 /* Do not call while under the GL lock. */
3304 #define copy_and_next(dest, src, size) memcpy(dest, src, size); dest += (size)
3305 static HRESULT process_vertices_strided(const struct wined3d_device *device, DWORD dwDestIndex, DWORD dwCount,
3306 const struct wined3d_stream_info *stream_info, struct wined3d_buffer *dest, DWORD flags,
3309 struct wined3d_matrix mat, proj_mat, view_mat, world_mat;
3310 struct wined3d_viewport vp;
3318 if (stream_info->use_map & (1 << WINED3D_FFP_NORMAL))
3320 WARN(" lighting state not saved yet... Some strange stuff may happen !\n");
3323 if (!(stream_info->use_map & (1 << WINED3D_FFP_POSITION)))
3325 ERR("Source has no position mask\n");
3326 return WINED3DERR_INVALIDCALL;
3329 if (device->stateBlock->state.render_states[WINED3D_RS_CLIPPING])
3331 static BOOL warned = FALSE;
3333 * The clipping code is not quite correct. Some things need
3334 * to be checked against IDirect3DDevice3 (!), d3d8 and d3d9,
3335 * so disable clipping for now.
3336 * (The graphics in Half-Life are broken, and my processvertices
3337 * test crashes with IDirect3DDevice3)
3343 FIXME("Clipping is broken and disabled for now\n");
3349 vertex_size = get_flexible_vertex_size(DestFVF);
3350 if (FAILED(hr = wined3d_buffer_map(dest, dwDestIndex * vertex_size, dwCount * vertex_size, &dest_ptr, 0)))
3352 WARN("Failed to map buffer, hr %#x.\n", hr);
3356 wined3d_device_get_transform(device, WINED3D_TS_VIEW, &view_mat);
3357 wined3d_device_get_transform(device, WINED3D_TS_PROJECTION, &proj_mat);
3358 wined3d_device_get_transform(device, WINED3D_TS_WORLD_MATRIX(0), &world_mat);
3360 TRACE("View mat:\n");
3361 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);
3362 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);
3363 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);
3364 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);
3366 TRACE("Proj mat:\n");
3367 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);
3368 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);
3369 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);
3370 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);
3372 TRACE("World mat:\n");
3373 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);
3374 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);
3375 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);
3376 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);
3378 /* Get the viewport */
3379 wined3d_device_get_viewport(device, &vp);
3380 TRACE("viewport x %u, y %u, width %u, height %u, min_z %.8e, max_z %.8e.\n",
3381 vp.x, vp.y, vp.width, vp.height, vp.min_z, vp.max_z);
3383 multiply_matrix(&mat,&view_mat,&world_mat);
3384 multiply_matrix(&mat,&proj_mat,&mat);
3386 numTextures = (DestFVF & WINED3DFVF_TEXCOUNT_MASK) >> WINED3DFVF_TEXCOUNT_SHIFT;
3388 for (i = 0; i < dwCount; i+= 1) {
3389 unsigned int tex_index;
3391 if ( ((DestFVF & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZ ) ||
3392 ((DestFVF & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZRHW ) ) {
3393 /* The position first */
3394 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_POSITION];
3395 const float *p = (const float *)(element->data.addr + i * element->stride);
3397 TRACE("In: ( %06.2f %06.2f %06.2f )\n", p[0], p[1], p[2]);
3399 /* Multiplication with world, view and projection matrix */
3400 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);
3401 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);
3402 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);
3403 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);
3405 TRACE("x=%f y=%f z=%f rhw=%f\n", x, y, z, rhw);
3407 /* WARNING: The following things are taken from d3d7 and were not yet checked
3408 * against d3d8 or d3d9!
3411 /* Clipping conditions: From msdn
3413 * A vertex is clipped if it does not match the following requirements
3417 * 0 < rhw ( Not in d3d7, but tested in d3d7)
3419 * If clipping is on is determined by the D3DVOP_CLIP flag in D3D7, and
3420 * by the D3DRS_CLIPPING in D3D9(according to the msdn, not checked)
3425 ( (-rhw -eps < x) && (-rhw -eps < y) && ( -eps < z) &&
3426 (x <= rhw + eps) && (y <= rhw + eps ) && (z <= rhw + eps) &&
3429 /* "Normal" viewport transformation (not clipped)
3430 * 1) The values are divided by rhw
3431 * 2) The y axis is negative, so multiply it with -1
3432 * 3) Screen coordinates go from -(Width/2) to +(Width/2) and
3433 * -(Height/2) to +(Height/2). The z range is MinZ to MaxZ
3434 * 4) Multiply x with Width/2 and add Width/2
3435 * 5) The same for the height
3436 * 6) Add the viewpoint X and Y to the 2D coordinates and
3437 * The minimum Z value to z
3438 * 7) rhw = 1 / rhw Reciprocal of Homogeneous W....
3440 * Well, basically it's simply a linear transformation into viewport
3452 z *= vp.max_z - vp.min_z;
3454 x += vp.width / 2 + vp.x;
3455 y += vp.height / 2 + vp.y;
3460 /* That vertex got clipped
3461 * Contrary to OpenGL it is not dropped completely, it just
3462 * undergoes a different calculation.
3464 TRACE("Vertex got clipped\n");
3471 /* Msdn mentions that Direct3D9 keeps a list of clipped vertices
3472 * outside of the main vertex buffer memory. That needs some more
3477 TRACE("Writing (%f %f %f) %f\n", x, y, z, rhw);
3480 ( (float *) dest_ptr)[0] = x;
3481 ( (float *) dest_ptr)[1] = y;
3482 ( (float *) dest_ptr)[2] = z;
3483 ( (float *) dest_ptr)[3] = rhw; /* SIC, see ddraw test! */
3485 dest_ptr += 3 * sizeof(float);
3487 if ((DestFVF & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZRHW)
3488 dest_ptr += sizeof(float);
3491 if (DestFVF & WINED3DFVF_PSIZE)
3492 dest_ptr += sizeof(DWORD);
3494 if (DestFVF & WINED3DFVF_NORMAL)
3496 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_NORMAL];
3497 const float *normal = (const float *)(element->data.addr + i * element->stride);
3498 /* AFAIK this should go into the lighting information */
3499 FIXME("Didn't expect the destination to have a normal\n");
3500 copy_and_next(dest_ptr, normal, 3 * sizeof(float));
3503 if (DestFVF & WINED3DFVF_DIFFUSE)
3505 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_DIFFUSE];
3506 const DWORD *color_d = (const DWORD *)(element->data.addr + i * element->stride);
3507 if (!(stream_info->use_map & (1 << WINED3D_FFP_DIFFUSE)))
3509 static BOOL warned = FALSE;
3512 ERR("No diffuse color in source, but destination has one\n");
3516 *( (DWORD *) dest_ptr) = 0xffffffff;
3517 dest_ptr += sizeof(DWORD);
3521 copy_and_next(dest_ptr, color_d, sizeof(DWORD));
3525 if (DestFVF & WINED3DFVF_SPECULAR)
3527 /* What's the color value in the feedback buffer? */
3528 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_SPECULAR];
3529 const DWORD *color_s = (const DWORD *)(element->data.addr + i * element->stride);
3530 if (!(stream_info->use_map & (1 << WINED3D_FFP_SPECULAR)))
3532 static BOOL warned = FALSE;
3535 ERR("No specular color in source, but destination has one\n");
3539 *(DWORD *)dest_ptr = 0xff000000;
3540 dest_ptr += sizeof(DWORD);
3544 copy_and_next(dest_ptr, color_s, sizeof(DWORD));
3548 for (tex_index = 0; tex_index < numTextures; ++tex_index)
3550 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_TEXCOORD0 + tex_index];
3551 const float *tex_coord = (const float *)(element->data.addr + i * element->stride);
3552 if (!(stream_info->use_map & (1 << (WINED3D_FFP_TEXCOORD0 + tex_index))))
3554 ERR("No source texture, but destination requests one\n");
3555 dest_ptr += GET_TEXCOORD_SIZE_FROM_FVF(DestFVF, tex_index) * sizeof(float);
3559 copy_and_next(dest_ptr, tex_coord, GET_TEXCOORD_SIZE_FROM_FVF(DestFVF, tex_index) * sizeof(float));
3564 wined3d_buffer_unmap(dest);
3568 #undef copy_and_next
3570 /* Do not call while under the GL lock. */
3571 HRESULT CDECL wined3d_device_process_vertices(struct wined3d_device *device,
3572 UINT src_start_idx, UINT dst_idx, UINT vertex_count, struct wined3d_buffer *dst_buffer,
3573 const struct wined3d_vertex_declaration *declaration, DWORD flags, DWORD dst_fvf)
3575 struct wined3d_state *state = &device->stateBlock->state;
3576 struct wined3d_stream_info stream_info;
3577 const struct wined3d_gl_info *gl_info;
3578 struct wined3d_context *context;
3579 struct wined3d_shader *vs;
3583 TRACE("device %p, src_start_idx %u, dst_idx %u, vertex_count %u, "
3584 "dst_buffer %p, declaration %p, flags %#x, dst_fvf %#x.\n",
3585 device, src_start_idx, dst_idx, vertex_count,
3586 dst_buffer, declaration, flags, dst_fvf);
3589 FIXME("Output vertex declaration not implemented yet.\n");
3591 /* Need any context to write to the vbo. */
3592 context = context_acquire(device, NULL);
3593 gl_info = context->gl_info;
3595 vs = state->vertex_shader;
3596 state->vertex_shader = NULL;
3597 device_stream_info_from_declaration(device, &stream_info);
3598 state->vertex_shader = vs;
3600 /* We can't convert FROM a VBO, and vertex buffers used to source into
3601 * process_vertices() are unlikely to ever be used for drawing. Release
3602 * VBOs in those buffers and fix up the stream_info structure.
3604 * Also apply the start index. */
3605 for (i = 0; i < (sizeof(stream_info.elements) / sizeof(*stream_info.elements)); ++i)
3607 struct wined3d_stream_info_element *e;
3609 if (!(stream_info.use_map & (1 << i)))
3612 e = &stream_info.elements[i];
3613 if (e->data.buffer_object)
3615 struct wined3d_buffer *vb = state->streams[e->stream_idx].buffer;
3616 e->data.buffer_object = 0;
3617 e->data.addr = (BYTE *)((ULONG_PTR)e->data.addr + (ULONG_PTR)buffer_get_sysmem(vb, gl_info));
3618 GL_EXTCALL(glDeleteBuffersARB(1, &vb->buffer_object));
3619 vb->buffer_object = 0;
3622 e->data.addr += e->stride * src_start_idx;
3625 hr = process_vertices_strided(device, dst_idx, vertex_count,
3626 &stream_info, dst_buffer, flags, dst_fvf);
3628 context_release(context);
3633 void CDECL wined3d_device_set_texture_stage_state(struct wined3d_device *device,
3634 UINT stage, enum wined3d_texture_stage_state state, DWORD value)
3636 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
3639 TRACE("device %p, stage %u, state %s, value %#x.\n",
3640 device, stage, debug_d3dtexturestate(state), value);
3642 if (state > WINED3D_HIGHEST_TEXTURE_STATE)
3644 WARN("Invalid state %#x passed.\n", state);
3648 if (stage >= gl_info->limits.texture_stages)
3650 WARN("Attempting to set stage %u which is higher than the max stage %u, ignoring.\n",
3651 stage, gl_info->limits.texture_stages - 1);
3655 old_value = device->updateStateBlock->state.texture_states[stage][state];
3656 device->updateStateBlock->changed.textureState[stage] |= 1 << state;
3657 device->updateStateBlock->state.texture_states[stage][state] = value;
3659 if (device->isRecordingState)
3661 TRACE("Recording... not performing anything.\n");
3665 /* Checked after the assignments to allow proper stateblock recording. */
3666 if (old_value == value)
3668 TRACE("Application is setting the old value over, nothing to do.\n");
3672 if (stage > device->stateBlock->state.lowest_disabled_stage
3673 && device->StateTable[STATE_TEXTURESTAGE(0, state)].representative
3674 == STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_OP))
3676 /* Colorop change above lowest disabled stage? That won't change
3677 * anything in the GL setup. Changes in other states are important on
3678 * disabled stages too. */
3682 if (state == WINED3D_TSS_COLOR_OP)
3686 if (value == WINED3D_TOP_DISABLE && old_value != WINED3D_TOP_DISABLE)
3688 /* Previously enabled stage disabled now. Make sure to dirtify
3689 * all enabled stages above stage, they have to be disabled.
3691 * The current stage is dirtified below. */
3692 for (i = stage + 1; i < device->stateBlock->state.lowest_disabled_stage; ++i)
3694 TRACE("Additionally dirtifying stage %u.\n", i);
3695 device_invalidate_state(device, STATE_TEXTURESTAGE(i, WINED3D_TSS_COLOR_OP));
3697 device->stateBlock->state.lowest_disabled_stage = stage;
3698 TRACE("New lowest disabled: %u.\n", stage);
3700 else if (value != WINED3D_TOP_DISABLE && old_value == WINED3D_TOP_DISABLE)
3702 /* Previously disabled stage enabled. Stages above it may need
3703 * enabling. Stage must be lowest_disabled_stage here, if it's
3704 * bigger success is returned above, and stages below the lowest
3705 * disabled stage can't be enabled (because they are enabled
3708 * Again stage stage doesn't need to be dirtified here, it is
3710 for (i = stage + 1; i < gl_info->limits.texture_stages; ++i)
3712 if (device->updateStateBlock->state.texture_states[i][WINED3D_TSS_COLOR_OP] == WINED3D_TOP_DISABLE)
3714 TRACE("Additionally dirtifying stage %u due to enable.\n", i);
3715 device_invalidate_state(device, STATE_TEXTURESTAGE(i, WINED3D_TSS_COLOR_OP));
3717 device->stateBlock->state.lowest_disabled_stage = i;
3718 TRACE("New lowest disabled: %u.\n", i);
3722 device_invalidate_state(device, STATE_TEXTURESTAGE(stage, state));
3725 DWORD CDECL wined3d_device_get_texture_stage_state(const struct wined3d_device *device,
3726 UINT stage, enum wined3d_texture_stage_state state)
3728 TRACE("device %p, stage %u, state %s.\n",
3729 device, stage, debug_d3dtexturestate(state));
3731 if (state > WINED3D_HIGHEST_TEXTURE_STATE)
3733 WARN("Invalid state %#x passed.\n", state);
3737 return device->updateStateBlock->state.texture_states[stage][state];
3740 HRESULT CDECL wined3d_device_set_texture(struct wined3d_device *device,
3741 UINT stage, struct wined3d_texture *texture)
3743 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
3744 struct wined3d_texture *prev;
3746 TRACE("device %p, stage %u, texture %p.\n", device, stage, texture);
3748 if (stage >= WINED3DVERTEXTEXTURESAMPLER0 && stage <= WINED3DVERTEXTEXTURESAMPLER3)
3749 stage -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
3751 /* Windows accepts overflowing this array... we do not. */
3752 if (stage >= sizeof(device->stateBlock->state.textures) / sizeof(*device->stateBlock->state.textures))
3754 WARN("Ignoring invalid stage %u.\n", stage);
3758 if (texture && texture->resource.pool == WINED3D_POOL_SCRATCH)
3760 WARN("Rejecting attempt to set scratch texture.\n");
3761 return WINED3DERR_INVALIDCALL;
3764 device->updateStateBlock->changed.textures |= 1 << stage;
3766 prev = device->updateStateBlock->state.textures[stage];
3767 TRACE("Previous texture %p.\n", prev);
3769 if (texture == prev)
3771 TRACE("App is setting the same texture again, nothing to do.\n");
3775 TRACE("Setting new texture to %p.\n", texture);
3776 device->updateStateBlock->state.textures[stage] = texture;
3778 if (device->isRecordingState)
3780 TRACE("Recording... not performing anything\n");
3782 if (texture) wined3d_texture_incref(texture);
3783 if (prev) wined3d_texture_decref(prev);
3790 LONG bind_count = InterlockedIncrement(&texture->resource.bind_count);
3792 wined3d_texture_incref(texture);
3794 if (!prev || texture->target != prev->target)
3795 device_invalidate_state(device, STATE_PIXELSHADER);
3797 if (!prev && stage < gl_info->limits.texture_stages)
3799 /* The source arguments for color and alpha ops have different
3800 * meanings when a NULL texture is bound, so the COLOR_OP and
3801 * ALPHA_OP have to be dirtified. */
3802 device_invalidate_state(device, STATE_TEXTURESTAGE(stage, WINED3D_TSS_COLOR_OP));
3803 device_invalidate_state(device, STATE_TEXTURESTAGE(stage, WINED3D_TSS_ALPHA_OP));
3806 if (bind_count == 1)
3807 texture->sampler = stage;
3812 LONG bind_count = InterlockedDecrement(&prev->resource.bind_count);
3814 if (!texture && stage < gl_info->limits.texture_stages)
3816 device_invalidate_state(device, STATE_TEXTURESTAGE(stage, WINED3D_TSS_COLOR_OP));
3817 device_invalidate_state(device, STATE_TEXTURESTAGE(stage, WINED3D_TSS_ALPHA_OP));
3820 if (bind_count && prev->sampler == stage)
3824 /* Search for other stages the texture is bound to. Shouldn't
3825 * happen if applications bind textures to a single stage only. */
3826 TRACE("Searching for other stages the texture is bound to.\n");
3827 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i)
3829 if (device->updateStateBlock->state.textures[i] == prev)
3831 TRACE("Texture is also bound to stage %u.\n", i);
3838 wined3d_texture_decref(prev);
3841 device_invalidate_state(device, STATE_SAMPLER(stage));
3846 struct wined3d_texture * CDECL wined3d_device_get_texture(const struct wined3d_device *device, UINT stage)
3848 TRACE("device %p, stage %u.\n", device, stage);
3850 if (stage >= WINED3DVERTEXTEXTURESAMPLER0 && stage <= WINED3DVERTEXTEXTURESAMPLER3)
3851 stage -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
3853 if (stage >= sizeof(device->stateBlock->state.textures) / sizeof(*device->stateBlock->state.textures))
3855 WARN("Ignoring invalid stage %u.\n", stage);
3856 return NULL; /* Windows accepts overflowing this array ... we do not. */
3859 return device->stateBlock->state.textures[stage];
3862 HRESULT CDECL wined3d_device_get_back_buffer(const struct wined3d_device *device, UINT swapchain_idx,
3863 UINT backbuffer_idx, enum wined3d_backbuffer_type backbuffer_type, struct wined3d_surface **backbuffer)
3865 struct wined3d_swapchain *swapchain;
3867 TRACE("device %p, swapchain_idx %u, backbuffer_idx %u, backbuffer_type %#x, backbuffer %p.\n",
3868 device, swapchain_idx, backbuffer_idx, backbuffer_type, backbuffer);
3870 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
3871 return WINED3DERR_INVALIDCALL;
3873 if (!(*backbuffer = wined3d_swapchain_get_back_buffer(swapchain, backbuffer_idx, backbuffer_type)))
3874 return WINED3DERR_INVALIDCALL;
3878 HRESULT CDECL wined3d_device_get_device_caps(const struct wined3d_device *device, WINED3DCAPS *caps)
3880 TRACE("device %p, caps %p.\n", device, caps);
3882 return wined3d_get_device_caps(device->wined3d, device->adapter->ordinal,
3883 device->create_parms.device_type, caps);
3886 HRESULT CDECL wined3d_device_get_display_mode(const struct wined3d_device *device, UINT swapchain_idx,
3887 struct wined3d_display_mode *mode, enum wined3d_display_rotation *rotation)
3889 struct wined3d_swapchain *swapchain;
3891 TRACE("device %p, swapchain_idx %u, mode %p, rotation %p.\n",
3892 device, swapchain_idx, mode, rotation);
3894 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
3895 return WINED3DERR_INVALIDCALL;
3897 return wined3d_swapchain_get_display_mode(swapchain, mode, rotation);
3900 HRESULT CDECL wined3d_device_begin_stateblock(struct wined3d_device *device)
3902 struct wined3d_stateblock *stateblock;
3905 TRACE("device %p.\n", device);
3907 if (device->isRecordingState)
3908 return WINED3DERR_INVALIDCALL;
3910 hr = wined3d_stateblock_create(device, WINED3D_SBT_RECORDED, &stateblock);
3914 wined3d_stateblock_decref(device->updateStateBlock);
3915 device->updateStateBlock = stateblock;
3916 device->isRecordingState = TRUE;
3918 TRACE("Recording stateblock %p.\n", stateblock);
3923 HRESULT CDECL wined3d_device_end_stateblock(struct wined3d_device *device,
3924 struct wined3d_stateblock **stateblock)
3926 struct wined3d_stateblock *object = device->updateStateBlock;
3928 TRACE("device %p, stateblock %p.\n", device, stateblock);
3930 if (!device->isRecordingState)
3932 WARN("Not recording.\n");
3934 return WINED3DERR_INVALIDCALL;
3937 stateblock_init_contained_states(object);
3939 *stateblock = object;
3940 device->isRecordingState = FALSE;
3941 device->updateStateBlock = device->stateBlock;
3942 wined3d_stateblock_incref(device->updateStateBlock);
3944 TRACE("Returning stateblock %p.\n", *stateblock);
3949 HRESULT CDECL wined3d_device_begin_scene(struct wined3d_device *device)
3951 /* At the moment we have no need for any functionality at the beginning
3953 TRACE("device %p.\n", device);
3955 if (device->inScene)
3957 WARN("Already in scene, returning WINED3DERR_INVALIDCALL.\n");
3958 return WINED3DERR_INVALIDCALL;
3960 device->inScene = TRUE;
3964 HRESULT CDECL wined3d_device_end_scene(struct wined3d_device *device)
3966 struct wined3d_context *context;
3968 TRACE("device %p.\n", device);
3970 if (!device->inScene)
3972 WARN("Not in scene, returning WINED3DERR_INVALIDCALL.\n");
3973 return WINED3DERR_INVALIDCALL;
3976 context = context_acquire(device, NULL);
3977 /* We only have to do this if we need to read the, swapbuffers performs a flush for us */
3978 context->gl_info->gl_ops.gl.p_glFlush();
3979 /* No checkGLcall here to avoid locking the lock just for checking a call that hardly ever
3981 context_release(context);
3983 device->inScene = FALSE;
3987 HRESULT CDECL wined3d_device_present(const struct wined3d_device *device, const RECT *src_rect,
3988 const RECT *dst_rect, HWND dst_window_override, const RGNDATA *dirty_region, DWORD flags)
3992 TRACE("device %p, src_rect %s, dst_rect %s, dst_window_override %p, dirty_region %p, flags %#x.\n",
3993 device, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect),
3994 dst_window_override, dirty_region, flags);
3996 for (i = 0; i < device->swapchain_count; ++i)
3998 wined3d_swapchain_present(device->swapchains[i], src_rect,
3999 dst_rect, dst_window_override, dirty_region, flags);
4005 /* Do not call while under the GL lock. */
4006 HRESULT CDECL wined3d_device_clear(struct wined3d_device *device, DWORD rect_count,
4007 const RECT *rects, DWORD flags, const struct wined3d_color *color, float depth, DWORD stencil)
4011 TRACE("device %p, rect_count %u, rects %p, flags %#x, color {%.8e, %.8e, %.8e, %.8e}, depth %.8e, stencil %u.\n",
4012 device, rect_count, rects, flags, color->r, color->g, color->b, color->a, depth, stencil);
4014 if (!rect_count && rects)
4016 WARN("Rects is %p, but rect_count is 0, ignoring clear\n", rects);
4020 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
4022 struct wined3d_surface *ds = device->fb.depth_stencil;
4025 WARN("Clearing depth and/or stencil without a depth stencil buffer attached, returning WINED3DERR_INVALIDCALL\n");
4026 /* TODO: What about depth stencil buffers without stencil bits? */
4027 return WINED3DERR_INVALIDCALL;
4029 else if (flags & WINED3DCLEAR_TARGET)
4031 if (ds->resource.width < device->fb.render_targets[0]->resource.width
4032 || ds->resource.height < device->fb.render_targets[0]->resource.height)
4034 WARN("Silently ignoring depth and target clear with mismatching sizes\n");
4040 wined3d_get_draw_rect(&device->stateBlock->state, &draw_rect);
4041 device_clear_render_targets(device, device->adapter->gl_info.limits.buffers,
4042 &device->fb, rect_count, rects, &draw_rect, flags, color, depth, stencil);
4047 void CDECL wined3d_device_set_primitive_type(struct wined3d_device *device,
4048 enum wined3d_primitive_type primitive_type)
4050 TRACE("device %p, primitive_type %s\n", device, debug_d3dprimitivetype(primitive_type));
4052 device->updateStateBlock->changed.primitive_type = TRUE;
4053 device->updateStateBlock->state.gl_primitive_type = gl_primitive_type_from_d3d(primitive_type);
4056 void CDECL wined3d_device_get_primitive_type(const struct wined3d_device *device,
4057 enum wined3d_primitive_type *primitive_type)
4059 TRACE("device %p, primitive_type %p\n", device, primitive_type);
4061 *primitive_type = d3d_primitive_type_from_gl(device->stateBlock->state.gl_primitive_type);
4063 TRACE("Returning %s\n", debug_d3dprimitivetype(*primitive_type));
4066 HRESULT CDECL wined3d_device_draw_primitive(struct wined3d_device *device, UINT start_vertex, UINT vertex_count)
4068 TRACE("device %p, start_vertex %u, vertex_count %u.\n", device, start_vertex, vertex_count);
4070 if (!device->stateBlock->state.vertex_declaration)
4072 WARN("Called without a valid vertex declaration set.\n");
4073 return WINED3DERR_INVALIDCALL;
4076 if (device->stateBlock->state.load_base_vertex_index)
4078 device->stateBlock->state.load_base_vertex_index = 0;
4079 device_invalidate_state(device, STATE_BASEVERTEXINDEX);
4082 /* Account for the loading offset due to index buffers. Instead of
4083 * reloading all sources correct it with the startvertex parameter. */
4084 draw_primitive(device, start_vertex, vertex_count, 0, 0, FALSE, NULL);
4088 HRESULT CDECL wined3d_device_draw_indexed_primitive(struct wined3d_device *device, UINT start_idx, UINT index_count)
4090 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
4092 TRACE("device %p, start_idx %u, index_count %u.\n", device, start_idx, index_count);
4094 if (!device->stateBlock->state.index_buffer)
4096 /* D3D9 returns D3DERR_INVALIDCALL when DrawIndexedPrimitive is called
4097 * without an index buffer set. (The first time at least...)
4098 * D3D8 simply dies, but I doubt it can do much harm to return
4099 * D3DERR_INVALIDCALL there as well. */
4100 WARN("Called without a valid index buffer set, returning WINED3DERR_INVALIDCALL.\n");
4101 return WINED3DERR_INVALIDCALL;
4104 if (!device->stateBlock->state.vertex_declaration)
4106 WARN("Called without a valid vertex declaration set.\n");
4107 return WINED3DERR_INVALIDCALL;
4110 if (!gl_info->supported[ARB_DRAW_ELEMENTS_BASE_VERTEX] &&
4111 device->stateBlock->state.load_base_vertex_index != device->stateBlock->state.base_vertex_index)
4113 device->stateBlock->state.load_base_vertex_index = device->stateBlock->state.base_vertex_index;
4114 device_invalidate_state(device, STATE_BASEVERTEXINDEX);
4117 draw_primitive(device, start_idx, index_count, 0, 0, TRUE, NULL);
4122 void CDECL wined3d_device_draw_indexed_primitive_instanced(struct wined3d_device *device,
4123 UINT start_idx, UINT index_count, UINT start_instance, UINT instance_count)
4125 TRACE("device %p, start_idx %u, index_count %u.\n", device, start_idx, index_count);
4127 draw_primitive(device, start_idx, index_count, start_instance, instance_count, TRUE, NULL);
4130 HRESULT CDECL wined3d_device_draw_primitive_strided(struct wined3d_device *device,
4131 UINT vertex_count, const struct wined3d_strided_data *strided_data)
4133 /* Mark the state dirty until we have nicer tracking. It's fine to change
4134 * baseVertexIndex because that call is only called by ddraw which does
4135 * not need that value. */
4136 device_invalidate_state(device, STATE_VDECL);
4137 device_invalidate_state(device, STATE_STREAMSRC);
4138 device_invalidate_state(device, STATE_INDEXBUFFER);
4140 device->stateBlock->state.base_vertex_index = 0;
4141 device->up_strided = strided_data;
4142 draw_primitive(device, 0, vertex_count, 0, 0, FALSE, NULL);
4143 device->up_strided = NULL;
4145 /* Invalidate the states again to make sure the values from the stateblock
4146 * are properly applied in the next regular draw. Note that the application-
4147 * provided strided data has ovwritten pretty much the entire vertex and
4148 * and index stream related states */
4149 device_invalidate_state(device, STATE_VDECL);
4150 device_invalidate_state(device, STATE_STREAMSRC);
4151 device_invalidate_state(device, STATE_INDEXBUFFER);
4155 HRESULT CDECL wined3d_device_draw_indexed_primitive_strided(struct wined3d_device *device,
4156 UINT index_count, const struct wined3d_strided_data *strided_data,
4157 UINT vertex_count, const void *index_data, enum wined3d_format_id index_data_format_id)
4159 enum wined3d_format_id prev_idx_format;
4161 /* Mark the state dirty until we have nicer tracking
4162 * its fine to change baseVertexIndex because that call is only called by ddraw which does not need
4165 device_invalidate_state(device, STATE_VDECL);
4166 device_invalidate_state(device, STATE_STREAMSRC);
4167 device_invalidate_state(device, STATE_INDEXBUFFER);
4169 prev_idx_format = device->stateBlock->state.index_format;
4170 device->stateBlock->state.index_format = index_data_format_id;
4171 device->stateBlock->state.base_vertex_index = 0;
4172 device->up_strided = strided_data;
4173 draw_primitive(device, 0, index_count, 0, 0, TRUE, index_data);
4174 device->up_strided = NULL;
4175 device->stateBlock->state.index_format = prev_idx_format;
4177 device_invalidate_state(device, STATE_VDECL);
4178 device_invalidate_state(device, STATE_STREAMSRC);
4179 device_invalidate_state(device, STATE_INDEXBUFFER);
4183 /* This is a helper function for UpdateTexture, there is no UpdateVolume method in D3D. */
4184 static HRESULT device_update_volume(struct wined3d_device *device,
4185 struct wined3d_volume *src_volume, struct wined3d_volume *dst_volume)
4187 struct wined3d_map_desc src;
4188 struct wined3d_map_desc dst;
4191 TRACE("device %p, src_volume %p, dst_volume %p.\n",
4192 device, src_volume, dst_volume);
4194 /* TODO: Implement direct loading into the gl volume instead of using
4195 * memcpy and dirtification to improve loading performance. */
4196 if (FAILED(hr = wined3d_volume_map(src_volume, &src, NULL, WINED3D_MAP_READONLY)))
4198 if (FAILED(hr = wined3d_volume_map(dst_volume, &dst, NULL, WINED3D_MAP_DISCARD)))
4200 wined3d_volume_unmap(src_volume);
4204 memcpy(dst.data, src.data, dst_volume->resource.size);
4206 hr = wined3d_volume_unmap(dst_volume);
4208 wined3d_volume_unmap(src_volume);
4210 hr = wined3d_volume_unmap(src_volume);
4215 HRESULT CDECL wined3d_device_update_texture(struct wined3d_device *device,
4216 struct wined3d_texture *src_texture, struct wined3d_texture *dst_texture)
4218 enum wined3d_resource_type type;
4219 unsigned int level_count, i;
4222 TRACE("device %p, src_texture %p, dst_texture %p.\n", device, src_texture, dst_texture);
4224 /* Verify that the source and destination textures are non-NULL. */
4225 if (!src_texture || !dst_texture)
4227 WARN("Source and destination textures must be non-NULL, returning WINED3DERR_INVALIDCALL.\n");
4228 return WINED3DERR_INVALIDCALL;
4231 if (src_texture == dst_texture)
4233 WARN("Source and destination are the same object, returning WINED3DERR_INVALIDCALL.\n");
4234 return WINED3DERR_INVALIDCALL;
4237 /* Verify that the source and destination textures are the same type. */
4238 type = src_texture->resource.type;
4239 if (dst_texture->resource.type != type)
4241 WARN("Source and destination have different types, returning WINED3DERR_INVALIDCALL.\n");
4242 return WINED3DERR_INVALIDCALL;
4245 /* Check that both textures have the identical numbers of levels. */
4246 level_count = wined3d_texture_get_level_count(src_texture);
4247 if (wined3d_texture_get_level_count(dst_texture) != level_count)
4249 WARN("Source and destination have different level counts, returning WINED3DERR_INVALIDCALL.\n");
4250 return WINED3DERR_INVALIDCALL;
4253 /* Make sure that the destination texture is loaded. */
4254 dst_texture->texture_ops->texture_preload(dst_texture, SRGB_RGB);
4256 /* Update every surface level of the texture. */
4259 case WINED3D_RTYPE_TEXTURE:
4261 struct wined3d_surface *src_surface;
4262 struct wined3d_surface *dst_surface;
4264 for (i = 0; i < level_count; ++i)
4266 src_surface = surface_from_resource(wined3d_texture_get_sub_resource(src_texture, i));
4267 dst_surface = surface_from_resource(wined3d_texture_get_sub_resource(dst_texture, i));
4268 hr = wined3d_device_update_surface(device, src_surface, NULL, dst_surface, NULL);
4271 WARN("Failed to update surface, hr %#x.\n", hr);
4278 case WINED3D_RTYPE_CUBE_TEXTURE:
4280 struct wined3d_surface *src_surface;
4281 struct wined3d_surface *dst_surface;
4283 for (i = 0; i < level_count * 6; ++i)
4285 src_surface = surface_from_resource(wined3d_texture_get_sub_resource(src_texture, i));
4286 dst_surface = surface_from_resource(wined3d_texture_get_sub_resource(dst_texture, i));
4287 hr = wined3d_device_update_surface(device, src_surface, NULL, dst_surface, NULL);
4290 WARN("Failed to update surface, hr %#x.\n", hr);
4297 case WINED3D_RTYPE_VOLUME_TEXTURE:
4299 for (i = 0; i < level_count; ++i)
4301 hr = device_update_volume(device,
4302 volume_from_resource(wined3d_texture_get_sub_resource(src_texture, i)),
4303 volume_from_resource(wined3d_texture_get_sub_resource(dst_texture, i)));
4306 WARN("Failed to update volume, hr %#x.\n", hr);
4314 FIXME("Unsupported texture type %#x.\n", type);
4315 return WINED3DERR_INVALIDCALL;
4321 HRESULT CDECL wined3d_device_get_front_buffer_data(const struct wined3d_device *device,
4322 UINT swapchain_idx, struct wined3d_surface *dst_surface)
4324 struct wined3d_swapchain *swapchain;
4326 TRACE("device %p, swapchain_idx %u, dst_surface %p.\n", device, swapchain_idx, dst_surface);
4328 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
4329 return WINED3DERR_INVALIDCALL;
4331 return wined3d_swapchain_get_front_buffer_data(swapchain, dst_surface);
4334 HRESULT CDECL wined3d_device_validate_device(const struct wined3d_device *device, DWORD *num_passes)
4336 const struct wined3d_state *state = &device->stateBlock->state;
4337 struct wined3d_texture *texture;
4340 TRACE("device %p, num_passes %p.\n", device, num_passes);
4342 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i)
4344 if (state->sampler_states[i][WINED3D_SAMP_MIN_FILTER] == WINED3D_TEXF_NONE)
4346 WARN("Sampler state %u has minfilter D3DTEXF_NONE, returning D3DERR_UNSUPPORTEDTEXTUREFILTER\n", i);
4347 return WINED3DERR_UNSUPPORTEDTEXTUREFILTER;
4349 if (state->sampler_states[i][WINED3D_SAMP_MAG_FILTER] == WINED3D_TEXF_NONE)
4351 WARN("Sampler state %u has magfilter D3DTEXF_NONE, returning D3DERR_UNSUPPORTEDTEXTUREFILTER\n", i);
4352 return WINED3DERR_UNSUPPORTEDTEXTUREFILTER;
4355 texture = state->textures[i];
4356 if (!texture || texture->resource.format->flags & WINED3DFMT_FLAG_FILTERING) continue;
4358 if (state->sampler_states[i][WINED3D_SAMP_MAG_FILTER] != WINED3D_TEXF_POINT)
4360 WARN("Non-filterable texture and mag filter enabled on samper %u, returning E_FAIL\n", i);
4363 if (state->sampler_states[i][WINED3D_SAMP_MIN_FILTER] != WINED3D_TEXF_POINT)
4365 WARN("Non-filterable texture and min filter enabled on samper %u, returning E_FAIL\n", i);
4368 if (state->sampler_states[i][WINED3D_SAMP_MIP_FILTER] != WINED3D_TEXF_NONE
4369 && state->sampler_states[i][WINED3D_SAMP_MIP_FILTER] != WINED3D_TEXF_POINT)
4371 WARN("Non-filterable texture and mip filter enabled on samper %u, returning E_FAIL\n", i);
4376 if (state->render_states[WINED3D_RS_ZENABLE] || state->render_states[WINED3D_RS_ZWRITEENABLE]
4377 || state->render_states[WINED3D_RS_STENCILENABLE])
4379 struct wined3d_surface *ds = device->fb.depth_stencil;
4380 struct wined3d_surface *target = device->fb.render_targets[0];
4383 && (ds->resource.width < target->resource.width || ds->resource.height < target->resource.height))
4385 WARN("Depth stencil is smaller than the color buffer, returning D3DERR_CONFLICTINGRENDERSTATE\n");
4386 return WINED3DERR_CONFLICTINGRENDERSTATE;
4390 /* return a sensible default */
4393 TRACE("returning D3D_OK\n");
4397 void CDECL wined3d_device_set_software_vertex_processing(struct wined3d_device *device, BOOL software)
4401 TRACE("device %p, software %#x.\n", device, software);
4405 FIXME("device %p, software %#x stub!\n", device, software);
4409 device->softwareVertexProcessing = software;
4412 BOOL CDECL wined3d_device_get_software_vertex_processing(const struct wined3d_device *device)
4416 TRACE("device %p.\n", device);
4420 TRACE("device %p stub!\n", device);
4424 return device->softwareVertexProcessing;
4427 HRESULT CDECL wined3d_device_get_raster_status(const struct wined3d_device *device,
4428 UINT swapchain_idx, struct wined3d_raster_status *raster_status)
4430 struct wined3d_swapchain *swapchain;
4432 TRACE("device %p, swapchain_idx %u, raster_status %p.\n",
4433 device, swapchain_idx, raster_status);
4435 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
4436 return WINED3DERR_INVALIDCALL;
4438 return wined3d_swapchain_get_raster_status(swapchain, raster_status);
4441 HRESULT CDECL wined3d_device_set_npatch_mode(struct wined3d_device *device, float segments)
4445 TRACE("device %p, segments %.8e.\n", device, segments);
4447 if (segments != 0.0f)
4451 FIXME("device %p, segments %.8e stub!\n", device, segments);
4459 float CDECL wined3d_device_get_npatch_mode(const struct wined3d_device *device)
4463 TRACE("device %p.\n", device);
4467 FIXME("device %p stub!\n", device);
4474 HRESULT CDECL wined3d_device_update_surface(struct wined3d_device *device,
4475 struct wined3d_surface *src_surface, const RECT *src_rect,
4476 struct wined3d_surface *dst_surface, const POINT *dst_point)
4478 TRACE("device %p, src_surface %p, src_rect %s, dst_surface %p, dst_point %s.\n",
4479 device, src_surface, wine_dbgstr_rect(src_rect),
4480 dst_surface, wine_dbgstr_point(dst_point));
4482 if (src_surface->resource.pool != WINED3D_POOL_SYSTEM_MEM || dst_surface->resource.pool != WINED3D_POOL_DEFAULT)
4484 WARN("source %p must be SYSTEMMEM and dest %p must be DEFAULT, returning WINED3DERR_INVALIDCALL\n",
4485 src_surface, dst_surface);
4486 return WINED3DERR_INVALIDCALL;
4489 return surface_upload_from_surface(dst_surface, dst_point, src_surface, src_rect);
4492 HRESULT CDECL wined3d_device_draw_rect_patch(struct wined3d_device *device, UINT handle,
4493 const float *num_segs, const struct wined3d_rect_patch_info *rect_patch_info)
4495 struct wined3d_rect_patch *patch;
4496 GLenum old_primitive_type;
4501 TRACE("device %p, handle %#x, num_segs %p, rect_patch_info %p.\n",
4502 device, handle, num_segs, rect_patch_info);
4504 if (!(handle || rect_patch_info))
4506 /* TODO: Write a test for the return value, thus the FIXME */
4507 FIXME("Both handle and rect_patch_info are NULL.\n");
4508 return WINED3DERR_INVALIDCALL;
4513 i = PATCHMAP_HASHFUNC(handle);
4515 LIST_FOR_EACH(e, &device->patches[i])
4517 patch = LIST_ENTRY(e, struct wined3d_rect_patch, entry);
4518 if (patch->Handle == handle)
4527 TRACE("Patch does not exist. Creating a new one\n");
4528 patch = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*patch));
4529 patch->Handle = handle;
4530 list_add_head(&device->patches[i], &patch->entry);
4532 TRACE("Found existing patch %p\n", patch);
4537 /* Since opengl does not load tesselated vertex attributes into numbered vertex
4538 * attributes we have to tesselate, read back, and draw. This needs a patch
4539 * management structure instance. Create one.
4541 * A possible improvement is to check if a vertex shader is used, and if not directly
4544 FIXME("Drawing an uncached patch. This is slow\n");
4545 patch = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*patch));
4548 if (num_segs[0] != patch->numSegs[0] || num_segs[1] != patch->numSegs[1]
4549 || num_segs[2] != patch->numSegs[2] || num_segs[3] != patch->numSegs[3]
4550 || (rect_patch_info && memcmp(rect_patch_info, &patch->rect_patch_info, sizeof(*rect_patch_info))))
4553 TRACE("Tesselation density or patch info changed, retesselating\n");
4555 if (rect_patch_info)
4556 patch->rect_patch_info = *rect_patch_info;
4558 patch->numSegs[0] = num_segs[0];
4559 patch->numSegs[1] = num_segs[1];
4560 patch->numSegs[2] = num_segs[2];
4561 patch->numSegs[3] = num_segs[3];
4563 hr = tesselate_rectpatch(device, patch);
4566 WARN("Patch tesselation failed.\n");
4568 /* Do not release the handle to store the params of the patch */
4570 HeapFree(GetProcessHeap(), 0, patch);
4576 old_primitive_type = device->stateBlock->state.gl_primitive_type;
4577 device->stateBlock->state.gl_primitive_type = GL_TRIANGLES;
4578 wined3d_device_draw_primitive_strided(device, patch->numSegs[0] * patch->numSegs[1] * 2 * 3, &patch->strided);
4579 device->stateBlock->state.gl_primitive_type = old_primitive_type;
4581 /* Destroy uncached patches */
4584 HeapFree(GetProcessHeap(), 0, patch->mem);
4585 HeapFree(GetProcessHeap(), 0, patch);
4590 HRESULT CDECL wined3d_device_draw_tri_patch(struct wined3d_device *device, UINT handle,
4591 const float *segment_count, const struct wined3d_tri_patch_info *patch_info)
4593 FIXME("device %p, handle %#x, segment_count %p, patch_info %p stub!\n",
4594 device, handle, segment_count, patch_info);
4599 HRESULT CDECL wined3d_device_delete_patch(struct wined3d_device *device, UINT handle)
4601 struct wined3d_rect_patch *patch;
4605 TRACE("device %p, handle %#x.\n", device, handle);
4607 i = PATCHMAP_HASHFUNC(handle);
4608 LIST_FOR_EACH(e, &device->patches[i])
4610 patch = LIST_ENTRY(e, struct wined3d_rect_patch, entry);
4611 if (patch->Handle == handle)
4613 TRACE("Deleting patch %p\n", patch);
4614 list_remove(&patch->entry);
4615 HeapFree(GetProcessHeap(), 0, patch->mem);
4616 HeapFree(GetProcessHeap(), 0, patch);
4621 /* TODO: Write a test for the return value */
4622 FIXME("Attempt to destroy nonexistent patch\n");
4623 return WINED3DERR_INVALIDCALL;
4626 /* Do not call while under the GL lock. */
4627 HRESULT CDECL wined3d_device_color_fill(struct wined3d_device *device,
4628 struct wined3d_surface *surface, const RECT *rect, const struct wined3d_color *color)
4632 TRACE("device %p, surface %p, rect %s, color {%.8e, %.8e, %.8e, %.8e}.\n",
4633 device, surface, wine_dbgstr_rect(rect),
4634 color->r, color->g, color->b, color->a);
4636 if (surface->resource.pool != WINED3D_POOL_DEFAULT && surface->resource.pool != WINED3D_POOL_SYSTEM_MEM)
4638 WARN("Color-fill not allowed on %s surfaces.\n", debug_d3dpool(surface->resource.pool));
4639 return WINED3DERR_INVALIDCALL;
4644 SetRect(&r, 0, 0, surface->resource.width, surface->resource.height);
4648 return surface_color_fill(surface, rect, color);
4651 /* Do not call while under the GL lock. */
4652 void CDECL wined3d_device_clear_rendertarget_view(struct wined3d_device *device,
4653 struct wined3d_rendertarget_view *rendertarget_view, const struct wined3d_color *color)
4655 struct wined3d_resource *resource;
4659 resource = rendertarget_view->resource;
4660 if (resource->type != WINED3D_RTYPE_SURFACE)
4662 FIXME("Only supported on surface resources\n");
4666 SetRect(&rect, 0, 0, resource->width, resource->height);
4667 hr = surface_color_fill(surface_from_resource(resource), &rect, color);
4668 if (FAILED(hr)) ERR("Color fill failed, hr %#x.\n", hr);
4671 struct wined3d_surface * CDECL wined3d_device_get_render_target(const struct wined3d_device *device,
4672 UINT render_target_idx)
4674 TRACE("device %p, render_target_idx %u.\n", device, render_target_idx);
4676 if (render_target_idx >= device->adapter->gl_info.limits.buffers)
4678 WARN("Only %u render targets are supported.\n", device->adapter->gl_info.limits.buffers);
4682 return device->fb.render_targets[render_target_idx];
4685 struct wined3d_surface * CDECL wined3d_device_get_depth_stencil(const struct wined3d_device *device)
4687 TRACE("device %p.\n", device);
4689 return device->fb.depth_stencil;
4692 HRESULT CDECL wined3d_device_set_render_target(struct wined3d_device *device,
4693 UINT render_target_idx, struct wined3d_surface *render_target, BOOL set_viewport)
4695 struct wined3d_surface *prev;
4697 TRACE("device %p, render_target_idx %u, render_target %p, set_viewport %#x.\n",
4698 device, render_target_idx, render_target, set_viewport);
4700 if (render_target_idx >= device->adapter->gl_info.limits.buffers)
4702 WARN("Only %u render targets are supported.\n", device->adapter->gl_info.limits.buffers);
4703 return WINED3DERR_INVALIDCALL;
4706 /* Render target 0 can't be set to NULL. */
4707 if (!render_target && !render_target_idx)
4709 WARN("Trying to set render target 0 to NULL.\n");
4710 return WINED3DERR_INVALIDCALL;
4713 if (render_target && !(render_target->resource.usage & WINED3DUSAGE_RENDERTARGET))
4715 FIXME("Surface %p doesn't have render target usage.\n", render_target);
4716 return WINED3DERR_INVALIDCALL;
4719 /* Set the viewport and scissor rectangles, if requested. Tests show that
4720 * stateblock recording is ignored, the change goes directly into the
4721 * primary stateblock. */
4722 if (!render_target_idx && set_viewport)
4724 struct wined3d_state *state = &device->stateBlock->state;
4726 state->viewport.x = 0;
4727 state->viewport.y = 0;
4728 state->viewport.width = render_target->resource.width;
4729 state->viewport.height = render_target->resource.height;
4730 state->viewport.min_z = 0.0f;
4731 state->viewport.max_z = 1.0f;
4732 device_invalidate_state(device, STATE_VIEWPORT);
4734 state->scissor_rect.top = 0;
4735 state->scissor_rect.left = 0;
4736 state->scissor_rect.right = render_target->resource.width;
4737 state->scissor_rect.bottom = render_target->resource.height;
4738 device_invalidate_state(device, STATE_SCISSORRECT);
4742 prev = device->fb.render_targets[render_target_idx];
4743 if (render_target == prev)
4747 wined3d_surface_incref(render_target);
4748 device->fb.render_targets[render_target_idx] = render_target;
4749 /* Release after the assignment, to prevent device_resource_released()
4750 * from seeing the surface as still in use. */
4752 wined3d_surface_decref(prev);
4754 device_invalidate_state(device, STATE_FRAMEBUFFER);
4759 void CDECL wined3d_device_set_depth_stencil(struct wined3d_device *device, struct wined3d_surface *depth_stencil)
4761 struct wined3d_surface *prev = device->fb.depth_stencil;
4763 TRACE("device %p, depth_stencil %p, old depth_stencil %p.\n",
4764 device, depth_stencil, prev);
4766 if (prev == depth_stencil)
4768 TRACE("Trying to do a NOP SetRenderTarget operation.\n");
4774 if (device->swapchains[0]->desc.flags & WINED3DPRESENTFLAG_DISCARD_DEPTHSTENCIL
4775 || prev->flags & SFLAG_DISCARD)
4777 surface_modify_ds_location(prev, SFLAG_DISCARDED,
4778 prev->resource.width, prev->resource.height);
4779 if (prev == device->onscreen_depth_stencil)
4781 wined3d_surface_decref(device->onscreen_depth_stencil);
4782 device->onscreen_depth_stencil = NULL;
4787 device->fb.depth_stencil = depth_stencil;
4789 wined3d_surface_incref(depth_stencil);
4791 if (!prev != !depth_stencil)
4793 /* Swapping NULL / non NULL depth stencil affects the depth and tests */
4794 device_invalidate_state(device, STATE_RENDER(WINED3D_RS_ZENABLE));
4795 device_invalidate_state(device, STATE_RENDER(WINED3D_RS_STENCILENABLE));
4796 device_invalidate_state(device, STATE_RENDER(WINED3D_RS_STENCILWRITEMASK));
4797 device_invalidate_state(device, STATE_RENDER(WINED3D_RS_DEPTHBIAS));
4799 else if (prev && prev->resource.format->depth_size != depth_stencil->resource.format->depth_size)
4801 device_invalidate_state(device, STATE_RENDER(WINED3D_RS_DEPTHBIAS));
4804 wined3d_surface_decref(prev);
4806 device_invalidate_state(device, STATE_FRAMEBUFFER);
4811 HRESULT CDECL wined3d_device_set_cursor_properties(struct wined3d_device *device,
4812 UINT x_hotspot, UINT y_hotspot, struct wined3d_surface *cursor_image)
4814 TRACE("device %p, x_hotspot %u, y_hotspot %u, cursor_image %p.\n",
4815 device, x_hotspot, y_hotspot, cursor_image);
4817 /* some basic validation checks */
4818 if (device->cursorTexture)
4820 struct wined3d_context *context = context_acquire(device, NULL);
4821 context->gl_info->gl_ops.gl.p_glDeleteTextures(1, &device->cursorTexture);
4822 context_release(context);
4823 device->cursorTexture = 0;
4828 struct wined3d_display_mode mode;
4829 struct wined3d_map_desc map_desc;
4832 /* MSDN: Cursor must be A8R8G8B8 */
4833 if (cursor_image->resource.format->id != WINED3DFMT_B8G8R8A8_UNORM)
4835 WARN("surface %p has an invalid format.\n", cursor_image);
4836 return WINED3DERR_INVALIDCALL;
4839 if (FAILED(hr = wined3d_get_adapter_display_mode(device->wined3d, device->adapter->ordinal, &mode, NULL)))
4841 ERR("Failed to get display mode, hr %#x.\n", hr);
4842 return WINED3DERR_INVALIDCALL;
4845 /* MSDN: Cursor must be smaller than the display mode */
4846 if (cursor_image->resource.width > mode.width || cursor_image->resource.height > mode.height)
4848 WARN("Surface %p dimensions are %ux%u, but screen dimensions are %ux%u.\n",
4849 cursor_image, cursor_image->resource.width, cursor_image->resource.height,
4850 mode.width, mode.height);
4851 return WINED3DERR_INVALIDCALL;
4854 /* TODO: MSDN: Cursor sizes must be a power of 2 */
4856 /* Do not store the surface's pointer because the application may
4857 * release it after setting the cursor image. Windows doesn't
4858 * addref the set surface, so we can't do this either without
4859 * creating circular refcount dependencies. Copy out the gl texture
4861 device->cursorWidth = cursor_image->resource.width;
4862 device->cursorHeight = cursor_image->resource.height;
4863 if (SUCCEEDED(wined3d_surface_map(cursor_image, &map_desc, NULL, WINED3D_MAP_READONLY)))
4865 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
4866 const struct wined3d_format *format = wined3d_get_format(gl_info, WINED3DFMT_B8G8R8A8_UNORM);
4867 struct wined3d_context *context;
4868 char *mem, *bits = map_desc.data;
4869 GLint intfmt = format->glInternal;
4870 GLint gl_format = format->glFormat;
4871 GLint type = format->glType;
4872 INT height = device->cursorHeight;
4873 INT width = device->cursorWidth;
4874 INT bpp = format->byte_count;
4877 /* Reformat the texture memory (pitch and width can be
4879 mem = HeapAlloc(GetProcessHeap(), 0, width * height * bpp);
4880 for (i = 0; i < height; ++i)
4881 memcpy(&mem[width * bpp * i], &bits[map_desc.row_pitch * i], width * bpp);
4882 wined3d_surface_unmap(cursor_image);
4884 context = context_acquire(device, NULL);
4886 if (gl_info->supported[APPLE_CLIENT_STORAGE])
4888 gl_info->gl_ops.gl.p_glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE);
4889 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE)");
4892 invalidate_active_texture(device, context);
4893 /* Create a new cursor texture */
4894 gl_info->gl_ops.gl.p_glGenTextures(1, &device->cursorTexture);
4895 checkGLcall("glGenTextures");
4896 context_bind_texture(context, GL_TEXTURE_2D, device->cursorTexture);
4897 /* Copy the bitmap memory into the cursor texture */
4898 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_2D, 0, intfmt, width, height, 0, gl_format, type, mem);
4899 checkGLcall("glTexImage2D");
4900 HeapFree(GetProcessHeap(), 0, mem);
4902 if (gl_info->supported[APPLE_CLIENT_STORAGE])
4904 gl_info->gl_ops.gl.p_glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE);
4905 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE)");
4908 context_release(context);
4912 FIXME("A cursor texture was not returned.\n");
4913 device->cursorTexture = 0;
4916 if (cursor_image->resource.width == 32 && cursor_image->resource.height == 32)
4918 UINT mask_size = cursor_image->resource.width * cursor_image->resource.height / 8;
4919 ICONINFO cursorInfo;
4923 /* 32-bit user32 cursors ignore the alpha channel if it's all
4924 * zeroes, and use the mask instead. Fill the mask with all ones
4925 * to ensure we still get a fully transparent cursor. */
4926 maskBits = HeapAlloc(GetProcessHeap(), 0, mask_size);
4927 memset(maskBits, 0xff, mask_size);
4928 wined3d_surface_map(cursor_image, &map_desc, NULL,
4929 WINED3D_MAP_NO_DIRTY_UPDATE | WINED3D_MAP_READONLY);
4930 TRACE("width: %u height: %u.\n", cursor_image->resource.width, cursor_image->resource.height);
4932 cursorInfo.fIcon = FALSE;
4933 cursorInfo.xHotspot = x_hotspot;
4934 cursorInfo.yHotspot = y_hotspot;
4935 cursorInfo.hbmMask = CreateBitmap(cursor_image->resource.width, cursor_image->resource.height,
4937 cursorInfo.hbmColor = CreateBitmap(cursor_image->resource.width, cursor_image->resource.height,
4938 1, 32, map_desc.data);
4939 wined3d_surface_unmap(cursor_image);
4940 /* Create our cursor and clean up. */
4941 cursor = CreateIconIndirect(&cursorInfo);
4942 if (cursorInfo.hbmMask) DeleteObject(cursorInfo.hbmMask);
4943 if (cursorInfo.hbmColor) DeleteObject(cursorInfo.hbmColor);
4944 if (device->hardwareCursor) DestroyCursor(device->hardwareCursor);
4945 device->hardwareCursor = cursor;
4946 if (device->bCursorVisible) SetCursor( cursor );
4947 HeapFree(GetProcessHeap(), 0, maskBits);
4951 device->xHotSpot = x_hotspot;
4952 device->yHotSpot = y_hotspot;
4956 void CDECL wined3d_device_set_cursor_position(struct wined3d_device *device,
4957 int x_screen_space, int y_screen_space, DWORD flags)
4959 TRACE("device %p, x %d, y %d, flags %#x.\n",
4960 device, x_screen_space, y_screen_space, flags);
4962 device->xScreenSpace = x_screen_space;
4963 device->yScreenSpace = y_screen_space;
4965 if (device->hardwareCursor)
4969 GetCursorPos( &pt );
4970 if (x_screen_space == pt.x && y_screen_space == pt.y)
4972 SetCursorPos( x_screen_space, y_screen_space );
4974 /* Switch to the software cursor if position diverges from the hardware one. */
4975 GetCursorPos( &pt );
4976 if (x_screen_space != pt.x || y_screen_space != pt.y)
4978 if (device->bCursorVisible) SetCursor( NULL );
4979 DestroyCursor( device->hardwareCursor );
4980 device->hardwareCursor = 0;
4985 BOOL CDECL wined3d_device_show_cursor(struct wined3d_device *device, BOOL show)
4987 BOOL oldVisible = device->bCursorVisible;
4989 TRACE("device %p, show %#x.\n", device, show);
4992 * When ShowCursor is first called it should make the cursor appear at the OS's last
4993 * known cursor position.
4995 if (show && !oldVisible)
4999 device->xScreenSpace = pt.x;
5000 device->yScreenSpace = pt.y;
5003 if (device->hardwareCursor)
5005 device->bCursorVisible = show;
5007 SetCursor(device->hardwareCursor);
5013 if (device->cursorTexture)
5014 device->bCursorVisible = show;
5020 void CDECL wined3d_device_evict_managed_resources(struct wined3d_device *device)
5022 struct wined3d_resource *resource, *cursor;
5024 TRACE("device %p.\n", device);
5026 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
5028 TRACE("Checking resource %p for eviction.\n", resource);
5030 if (resource->pool == WINED3D_POOL_MANAGED && !resource->map_count)
5032 TRACE("Evicting %p.\n", resource);
5033 resource->resource_ops->resource_unload(resource);
5037 /* Invalidate stream sources, the buffer(s) may have been evicted. */
5038 device_invalidate_state(device, STATE_STREAMSRC);
5041 /* Do not call while under the GL lock. */
5042 static void delete_opengl_contexts(struct wined3d_device *device, struct wined3d_swapchain *swapchain)
5044 struct wined3d_resource *resource, *cursor;
5045 const struct wined3d_gl_info *gl_info;
5046 struct wined3d_context *context;
5047 struct wined3d_shader *shader;
5049 context = context_acquire(device, NULL);
5050 gl_info = context->gl_info;
5052 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
5054 TRACE("Unloading resource %p.\n", resource);
5056 resource->resource_ops->resource_unload(resource);
5059 LIST_FOR_EACH_ENTRY(shader, &device->shaders, struct wined3d_shader, shader_list_entry)
5061 device->shader_backend->shader_destroy(shader);
5064 if (device->depth_blt_texture)
5066 gl_info->gl_ops.gl.p_glDeleteTextures(1, &device->depth_blt_texture);
5067 device->depth_blt_texture = 0;
5069 if (device->cursorTexture)
5071 gl_info->gl_ops.gl.p_glDeleteTextures(1, &device->cursorTexture);
5072 device->cursorTexture = 0;
5075 device->blitter->free_private(device);
5076 device->shader_backend->shader_free_private(device);
5077 destroy_dummy_textures(device, gl_info);
5079 context_release(context);
5081 while (device->context_count)
5083 swapchain_destroy_contexts(device->contexts[0]->swapchain);
5086 HeapFree(GetProcessHeap(), 0, swapchain->context);
5087 swapchain->context = NULL;
5090 /* Do not call while under the GL lock. */
5091 static HRESULT create_primary_opengl_context(struct wined3d_device *device, struct wined3d_swapchain *swapchain)
5093 struct wined3d_context *context;
5094 struct wined3d_surface *target;
5097 if (FAILED(hr = device->shader_backend->shader_alloc_private(device, device->adapter->fragment_pipe)))
5099 ERR("Failed to allocate shader private data, hr %#x.\n", hr);
5103 if (FAILED(hr = device->blitter->alloc_private(device)))
5105 ERR("Failed to allocate blitter private data, hr %#x.\n", hr);
5106 device->shader_backend->shader_free_private(device);
5110 /* Recreate the primary swapchain's context */
5111 swapchain->context = HeapAlloc(GetProcessHeap(), 0, sizeof(*swapchain->context));
5112 if (!swapchain->context)
5114 ERR("Failed to allocate memory for swapchain context array.\n");
5115 device->blitter->free_private(device);
5116 device->shader_backend->shader_free_private(device);
5117 return E_OUTOFMEMORY;
5120 target = swapchain->back_buffers ? swapchain->back_buffers[0] : swapchain->front_buffer;
5121 if (!(context = context_create(swapchain, target, swapchain->ds_format)))
5123 WARN("Failed to create context.\n");
5124 device->blitter->free_private(device);
5125 device->shader_backend->shader_free_private(device);
5126 HeapFree(GetProcessHeap(), 0, swapchain->context);
5130 swapchain->context[0] = context;
5131 swapchain->num_contexts = 1;
5132 create_dummy_textures(device, context);
5133 context_release(context);
5138 /* Do not call while under the GL lock. */
5139 HRESULT CDECL wined3d_device_reset(struct wined3d_device *device,
5140 const struct wined3d_swapchain_desc *swapchain_desc, const struct wined3d_display_mode *mode,
5141 wined3d_device_reset_cb callback, BOOL reset_state)
5143 struct wined3d_resource *resource, *cursor;
5144 struct wined3d_swapchain *swapchain;
5145 struct wined3d_display_mode m;
5146 BOOL DisplayModeChanged = FALSE;
5147 BOOL update_desc = FALSE;
5148 HRESULT hr = WINED3D_OK;
5151 TRACE("device %p, swapchain_desc %p, mode %p, callback %p.\n", device, swapchain_desc, mode, callback);
5153 if (!(swapchain = wined3d_device_get_swapchain(device, 0)))
5155 ERR("Failed to get the first implicit swapchain.\n");
5156 return WINED3DERR_INVALIDCALL;
5160 stateblock_unbind_resources(device->stateBlock);
5162 if (device->fb.render_targets)
5164 if (swapchain->back_buffers && swapchain->back_buffers[0])
5165 wined3d_device_set_render_target(device, 0, swapchain->back_buffers[0], FALSE);
5167 wined3d_device_set_render_target(device, 0, swapchain->front_buffer, FALSE);
5168 for (i = 1; i < device->adapter->gl_info.limits.buffers; ++i)
5170 wined3d_device_set_render_target(device, i, NULL, FALSE);
5173 wined3d_device_set_depth_stencil(device, NULL);
5175 if (device->onscreen_depth_stencil)
5177 wined3d_surface_decref(device->onscreen_depth_stencil);
5178 device->onscreen_depth_stencil = NULL;
5183 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
5185 TRACE("Enumerating resource %p.\n", resource);
5186 if (FAILED(hr = callback(resource)))
5191 /* Is it necessary to recreate the gl context? Actually every setting can be changed
5192 * on an existing gl context, so there's no real need for recreation.
5194 * TODO: Figure out how Reset influences resources in D3DPOOL_DEFAULT, D3DPOOL_SYSTEMMEMORY and D3DPOOL_MANAGED
5196 * TODO: Figure out what happens to explicit swapchains, or if we have more than one implicit swapchain
5198 TRACE("New params:\n");
5199 TRACE("backbuffer_width %u\n", swapchain_desc->backbuffer_width);
5200 TRACE("backbuffer_height %u\n", swapchain_desc->backbuffer_height);
5201 TRACE("backbuffer_format %s\n", debug_d3dformat(swapchain_desc->backbuffer_format));
5202 TRACE("backbuffer_count %u\n", swapchain_desc->backbuffer_count);
5203 TRACE("multisample_type %#x\n", swapchain_desc->multisample_type);
5204 TRACE("multisample_quality %u\n", swapchain_desc->multisample_quality);
5205 TRACE("swap_effect %#x\n", swapchain_desc->swap_effect);
5206 TRACE("device_window %p\n", swapchain_desc->device_window);
5207 TRACE("windowed %#x\n", swapchain_desc->windowed);
5208 TRACE("enable_auto_depth_stencil %#x\n", swapchain_desc->enable_auto_depth_stencil);
5209 if (swapchain_desc->enable_auto_depth_stencil)
5210 TRACE("auto_depth_stencil_format %s\n", debug_d3dformat(swapchain_desc->auto_depth_stencil_format));
5211 TRACE("flags %#x\n", swapchain_desc->flags);
5212 TRACE("refresh_rate %u\n", swapchain_desc->refresh_rate);
5213 TRACE("swap_interval %u\n", swapchain_desc->swap_interval);
5214 TRACE("auto_restore_display_mode %#x\n", swapchain_desc->auto_restore_display_mode);
5216 /* No special treatment of these parameters. Just store them */
5217 swapchain->desc.swap_effect = swapchain_desc->swap_effect;
5218 swapchain->desc.flags = swapchain_desc->flags;
5219 swapchain->desc.swap_interval = swapchain_desc->swap_interval;
5220 swapchain->desc.refresh_rate = swapchain_desc->refresh_rate;
5222 /* What to do about these? */
5223 if (swapchain_desc->backbuffer_count
5224 && swapchain_desc->backbuffer_count != swapchain->desc.backbuffer_count)
5225 FIXME("Cannot change the back buffer count yet.\n");
5227 if (swapchain_desc->device_window
5228 && swapchain_desc->device_window != swapchain->desc.device_window)
5230 TRACE("Changing the device window from %p to %p.\n",
5231 swapchain->desc.device_window, swapchain_desc->device_window);
5232 swapchain->desc.device_window = swapchain_desc->device_window;
5233 swapchain->device_window = swapchain_desc->device_window;
5234 wined3d_swapchain_set_window(swapchain, NULL);
5237 if (swapchain_desc->enable_auto_depth_stencil && !device->auto_depth_stencil)
5239 TRACE("Creating the depth stencil buffer\n");
5241 if (FAILED(hr = device->device_parent->ops->create_swapchain_surface(device->device_parent,
5242 device->device_parent, swapchain_desc->backbuffer_width, swapchain_desc->backbuffer_height,
5243 swapchain_desc->auto_depth_stencil_format, WINED3DUSAGE_DEPTHSTENCIL,
5244 swapchain_desc->multisample_type, swapchain_desc->multisample_quality,
5245 &device->auto_depth_stencil)))
5247 ERR("Failed to create the depth stencil buffer, hr %#x.\n", hr);
5248 return WINED3DERR_INVALIDCALL;
5252 /* Reset the depth stencil */
5253 if (swapchain_desc->enable_auto_depth_stencil)
5254 wined3d_device_set_depth_stencil(device, device->auto_depth_stencil);
5258 DisplayModeChanged = TRUE;
5261 else if (swapchain_desc->windowed)
5263 m.width = swapchain->orig_width;
5264 m.height = swapchain->orig_height;
5266 m.format_id = swapchain->desc.backbuffer_format;
5267 m.scanline_ordering = WINED3D_SCANLINE_ORDERING_UNKNOWN;
5271 m.width = swapchain_desc->backbuffer_width;
5272 m.height = swapchain_desc->backbuffer_height;
5273 m.refresh_rate = swapchain_desc->refresh_rate;
5274 m.format_id = swapchain_desc->backbuffer_format;
5275 m.scanline_ordering = WINED3D_SCANLINE_ORDERING_UNKNOWN;
5278 /* Should Width == 800 && Height == 0 set 800x600? */
5279 if (swapchain_desc->backbuffer_width && swapchain_desc->backbuffer_height
5280 && (swapchain_desc->backbuffer_width != swapchain->desc.backbuffer_width
5281 || swapchain_desc->backbuffer_height != swapchain->desc.backbuffer_height))
5283 if (!swapchain_desc->windowed)
5284 DisplayModeChanged = TRUE;
5286 swapchain->desc.backbuffer_width = swapchain_desc->backbuffer_width;
5287 swapchain->desc.backbuffer_height = swapchain_desc->backbuffer_height;
5291 if (swapchain_desc->backbuffer_format != WINED3DFMT_UNKNOWN
5292 && swapchain_desc->backbuffer_format != swapchain->desc.backbuffer_format)
5294 swapchain->desc.backbuffer_format = swapchain_desc->backbuffer_format;
5298 if (swapchain_desc->multisample_type != swapchain->desc.multisample_type
5299 || swapchain_desc->multisample_quality != swapchain->desc.multisample_quality)
5301 swapchain->desc.multisample_type = swapchain_desc->multisample_type;
5302 swapchain->desc.multisample_quality = swapchain_desc->multisample_quality;
5310 if (FAILED(hr = wined3d_surface_update_desc(swapchain->front_buffer, swapchain->desc.backbuffer_width,
5311 swapchain->desc.backbuffer_height, swapchain->desc.backbuffer_format,
5312 swapchain->desc.multisample_type, swapchain->desc.multisample_quality)))
5315 for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
5317 if (FAILED(hr = wined3d_surface_update_desc(swapchain->back_buffers[i], swapchain->desc.backbuffer_width,
5318 swapchain->desc.backbuffer_height, swapchain->desc.backbuffer_format,
5319 swapchain->desc.multisample_type, swapchain->desc.multisample_quality)))
5322 if (device->auto_depth_stencil)
5324 if (FAILED(hr = wined3d_surface_update_desc(device->auto_depth_stencil, swapchain->desc.backbuffer_width,
5325 swapchain->desc.backbuffer_height, device->auto_depth_stencil->resource.format->id,
5326 swapchain->desc.multisample_type, swapchain->desc.multisample_quality)))
5331 if (!swapchain_desc->windowed != !swapchain->desc.windowed
5332 || DisplayModeChanged)
5334 if (FAILED(hr = wined3d_set_adapter_display_mode(device->wined3d, device->adapter->ordinal, &m)))
5336 WARN("Failed to set display mode, hr %#x.\n", hr);
5337 return WINED3DERR_INVALIDCALL;
5340 if (!swapchain_desc->windowed)
5342 if (swapchain->desc.windowed)
5344 HWND focus_window = device->create_parms.focus_window;
5346 focus_window = swapchain_desc->device_window;
5347 if (FAILED(hr = wined3d_device_acquire_focus_window(device, focus_window)))
5349 ERR("Failed to acquire focus window, hr %#x.\n", hr);
5353 /* switch from windowed to fs */
5354 wined3d_device_setup_fullscreen_window(device, swapchain->device_window,
5355 swapchain_desc->backbuffer_width,
5356 swapchain_desc->backbuffer_height);
5360 /* Fullscreen -> fullscreen mode change */
5361 MoveWindow(swapchain->device_window, 0, 0,
5362 swapchain_desc->backbuffer_width,
5363 swapchain_desc->backbuffer_height,
5367 else if (!swapchain->desc.windowed)
5369 /* Fullscreen -> windowed switch */
5370 wined3d_device_restore_fullscreen_window(device, swapchain->device_window);
5371 wined3d_device_release_focus_window(device);
5373 swapchain->desc.windowed = swapchain_desc->windowed;
5375 else if (!swapchain_desc->windowed)
5377 DWORD style = device->style;
5378 DWORD exStyle = device->exStyle;
5379 /* If we're in fullscreen, and the mode wasn't changed, we have to get the window back into
5380 * the right position. Some applications(Battlefield 2, Guild Wars) move it and then call
5381 * Reset to clear up their mess. Guild Wars also loses the device during that.
5384 device->exStyle = 0;
5385 wined3d_device_setup_fullscreen_window(device, swapchain->device_window,
5386 swapchain_desc->backbuffer_width,
5387 swapchain_desc->backbuffer_height);
5388 device->style = style;
5389 device->exStyle = exStyle;
5394 TRACE("Resetting stateblock.\n");
5395 wined3d_stateblock_decref(device->updateStateBlock);
5396 wined3d_stateblock_decref(device->stateBlock);
5398 if (device->d3d_initialized)
5399 delete_opengl_contexts(device, swapchain);
5401 /* Note: No parent needed for initial internal stateblock */
5402 hr = wined3d_stateblock_create(device, WINED3D_SBT_INIT, &device->stateBlock);
5404 ERR("Resetting the stateblock failed with error %#x.\n", hr);
5406 TRACE("Created stateblock %p.\n", device->stateBlock);
5407 device->updateStateBlock = device->stateBlock;
5408 wined3d_stateblock_incref(device->updateStateBlock);
5410 stateblock_init_default_state(device->stateBlock);
5414 struct wined3d_surface *rt = device->fb.render_targets[0];
5415 struct wined3d_state *state = &device->stateBlock->state;
5417 /* Note the min_z / max_z is not reset. */
5418 state->viewport.x = 0;
5419 state->viewport.y = 0;
5420 state->viewport.width = rt->resource.width;
5421 state->viewport.height = rt->resource.height;
5422 device_invalidate_state(device, STATE_VIEWPORT);
5424 state->scissor_rect.top = 0;
5425 state->scissor_rect.left = 0;
5426 state->scissor_rect.right = rt->resource.width;
5427 state->scissor_rect.bottom = rt->resource.height;
5428 device_invalidate_state(device, STATE_SCISSORRECT);
5431 swapchain_update_render_to_fbo(swapchain);
5432 swapchain_update_draw_bindings(swapchain);
5434 if (reset_state && device->d3d_initialized)
5435 hr = create_primary_opengl_context(device, swapchain);
5437 /* All done. There is no need to reload resources or shaders, this will happen automatically on the
5443 HRESULT CDECL wined3d_device_set_dialog_box_mode(struct wined3d_device *device, BOOL enable_dialogs)
5445 TRACE("device %p, enable_dialogs %#x.\n", device, enable_dialogs);
5447 if (!enable_dialogs) FIXME("Dialogs cannot be disabled yet.\n");
5453 void CDECL wined3d_device_get_creation_parameters(const struct wined3d_device *device,
5454 struct wined3d_device_creation_parameters *parameters)
5456 TRACE("device %p, parameters %p.\n", device, parameters);
5458 *parameters = device->create_parms;
5461 void CDECL wined3d_device_set_gamma_ramp(const struct wined3d_device *device,
5462 UINT swapchain_idx, DWORD flags, const struct wined3d_gamma_ramp *ramp)
5464 struct wined3d_swapchain *swapchain;
5466 TRACE("device %p, swapchain_idx %u, flags %#x, ramp %p.\n",
5467 device, swapchain_idx, flags, ramp);
5469 if ((swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
5470 wined3d_swapchain_set_gamma_ramp(swapchain, flags, ramp);
5473 void CDECL wined3d_device_get_gamma_ramp(const struct wined3d_device *device,
5474 UINT swapchain_idx, struct wined3d_gamma_ramp *ramp)
5476 struct wined3d_swapchain *swapchain;
5478 TRACE("device %p, swapchain_idx %u, ramp %p.\n",
5479 device, swapchain_idx, ramp);
5481 if ((swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
5482 wined3d_swapchain_get_gamma_ramp(swapchain, ramp);
5485 void device_resource_add(struct wined3d_device *device, struct wined3d_resource *resource)
5487 TRACE("device %p, resource %p.\n", device, resource);
5489 list_add_head(&device->resources, &resource->resource_list_entry);
5492 static void device_resource_remove(struct wined3d_device *device, struct wined3d_resource *resource)
5494 TRACE("device %p, resource %p.\n", device, resource);
5496 list_remove(&resource->resource_list_entry);
5499 void device_resource_released(struct wined3d_device *device, struct wined3d_resource *resource)
5501 enum wined3d_resource_type type = resource->type;
5504 TRACE("device %p, resource %p, type %s.\n", device, resource, debug_d3dresourcetype(type));
5506 context_resource_released(device, resource, type);
5510 case WINED3D_RTYPE_SURFACE:
5512 struct wined3d_surface *surface = surface_from_resource(resource);
5514 if (!device->d3d_initialized) break;
5516 for (i = 0; i < device->adapter->gl_info.limits.buffers; ++i)
5518 if (device->fb.render_targets[i] == surface)
5520 ERR("Surface %p is still in use as render target %u.\n", surface, i);
5521 device->fb.render_targets[i] = NULL;
5525 if (device->fb.depth_stencil == surface)
5527 ERR("Surface %p is still in use as depth/stencil buffer.\n", surface);
5528 device->fb.depth_stencil = NULL;
5533 case WINED3D_RTYPE_TEXTURE:
5534 case WINED3D_RTYPE_CUBE_TEXTURE:
5535 case WINED3D_RTYPE_VOLUME_TEXTURE:
5536 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i)
5538 struct wined3d_texture *texture = wined3d_texture_from_resource(resource);
5540 if (device->stateBlock && device->stateBlock->state.textures[i] == texture)
5542 ERR("Texture %p is still in use by stateblock %p, stage %u.\n",
5543 texture, device->stateBlock, i);
5544 device->stateBlock->state.textures[i] = NULL;
5547 if (device->updateStateBlock != device->stateBlock
5548 && device->updateStateBlock->state.textures[i] == texture)
5550 ERR("Texture %p is still in use by stateblock %p, stage %u.\n",
5551 texture, device->updateStateBlock, i);
5552 device->updateStateBlock->state.textures[i] = NULL;
5557 case WINED3D_RTYPE_BUFFER:
5559 struct wined3d_buffer *buffer = buffer_from_resource(resource);
5561 for (i = 0; i < MAX_STREAMS; ++i)
5563 if (device->stateBlock && device->stateBlock->state.streams[i].buffer == buffer)
5565 ERR("Buffer %p is still in use by stateblock %p, stream %u.\n",
5566 buffer, device->stateBlock, i);
5567 device->stateBlock->state.streams[i].buffer = NULL;
5570 if (device->updateStateBlock != device->stateBlock
5571 && device->updateStateBlock->state.streams[i].buffer == buffer)
5573 ERR("Buffer %p is still in use by stateblock %p, stream %u.\n",
5574 buffer, device->updateStateBlock, i);
5575 device->updateStateBlock->state.streams[i].buffer = NULL;
5580 if (device->stateBlock && device->stateBlock->state.index_buffer == buffer)
5582 ERR("Buffer %p is still in use by stateblock %p as index buffer.\n",
5583 buffer, device->stateBlock);
5584 device->stateBlock->state.index_buffer = NULL;
5587 if (device->updateStateBlock != device->stateBlock
5588 && device->updateStateBlock->state.index_buffer == buffer)
5590 ERR("Buffer %p is still in use by stateblock %p as index buffer.\n",
5591 buffer, device->updateStateBlock);
5592 device->updateStateBlock->state.index_buffer = NULL;
5601 /* Remove the resource from the resourceStore */
5602 device_resource_remove(device, resource);
5604 TRACE("Resource released.\n");
5607 struct wined3d_surface * CDECL wined3d_device_get_surface_from_dc(const struct wined3d_device *device, HDC dc)
5609 struct wined3d_resource *resource;
5611 TRACE("device %p, dc %p.\n", device, dc);
5616 LIST_FOR_EACH_ENTRY(resource, &device->resources, struct wined3d_resource, resource_list_entry)
5618 if (resource->type == WINED3D_RTYPE_SURFACE)
5620 struct wined3d_surface *s = surface_from_resource(resource);
5624 TRACE("Found surface %p for dc %p.\n", s, dc);
5633 HRESULT device_init(struct wined3d_device *device, struct wined3d *wined3d,
5634 UINT adapter_idx, enum wined3d_device_type device_type, HWND focus_window, DWORD flags,
5635 BYTE surface_alignment, struct wined3d_device_parent *device_parent)
5637 struct wined3d_adapter *adapter = &wined3d->adapters[adapter_idx];
5638 const struct fragment_pipeline *fragment_pipeline;
5639 struct shader_caps shader_caps;
5640 struct fragment_caps ffp_caps;
5645 device->wined3d = wined3d;
5646 wined3d_incref(device->wined3d);
5647 device->adapter = wined3d->adapter_count ? adapter : NULL;
5648 device->device_parent = device_parent;
5649 list_init(&device->resources);
5650 list_init(&device->shaders);
5651 device->surface_alignment = surface_alignment;
5653 /* Save the creation parameters. */
5654 device->create_parms.adapter_idx = adapter_idx;
5655 device->create_parms.device_type = device_type;
5656 device->create_parms.focus_window = focus_window;
5657 device->create_parms.flags = flags;
5659 for (i = 0; i < PATCHMAP_SIZE; ++i) list_init(&device->patches[i]);
5661 device->shader_backend = adapter->shader_backend;
5662 device->shader_backend->shader_get_caps(&adapter->gl_info, &shader_caps);
5663 device->vs_version = shader_caps.vs_version;
5664 device->gs_version = shader_caps.gs_version;
5665 device->ps_version = shader_caps.ps_version;
5666 device->d3d_vshader_constantF = shader_caps.vs_uniform_count;
5667 device->d3d_pshader_constantF = shader_caps.ps_uniform_count;
5668 device->vs_clipping = shader_caps.wined3d_caps & WINED3D_SHADER_CAP_VS_CLIPPING;
5670 fragment_pipeline = adapter->fragment_pipe;
5671 fragment_pipeline->get_caps(&adapter->gl_info, &ffp_caps);
5672 device->max_ffp_textures = ffp_caps.MaxSimultaneousTextures;
5674 if (fragment_pipeline->states
5675 && FAILED(hr = compile_state_table(device->StateTable, device->multistate_funcs,
5676 &adapter->gl_info, ffp_vertexstate_template, fragment_pipeline, misc_state_template)))
5678 ERR("Failed to compile state table, hr %#x.\n", hr);
5679 wined3d_decref(device->wined3d);
5683 device->blitter = adapter->blitter;
5685 hr = wined3d_stateblock_create(device, WINED3D_SBT_INIT, &device->stateBlock);
5688 WARN("Failed to create stateblock.\n");
5689 for (i = 0; i < sizeof(device->multistate_funcs) / sizeof(device->multistate_funcs[0]); ++i)
5691 HeapFree(GetProcessHeap(), 0, device->multistate_funcs[i]);
5693 wined3d_decref(device->wined3d);
5697 TRACE("Created stateblock %p.\n", device->stateBlock);
5698 device->updateStateBlock = device->stateBlock;
5699 wined3d_stateblock_incref(device->updateStateBlock);
5705 void device_invalidate_state(const struct wined3d_device *device, DWORD state)
5707 DWORD rep = device->StateTable[state].representative;
5708 struct wined3d_context *context;
5713 for (i = 0; i < device->context_count; ++i)
5715 context = device->contexts[i];
5716 if(isStateDirty(context, rep)) continue;
5718 context->dirtyArray[context->numDirtyEntries++] = rep;
5719 idx = rep / (sizeof(*context->isStateDirty) * CHAR_BIT);
5720 shift = rep & ((sizeof(*context->isStateDirty) * CHAR_BIT) - 1);
5721 context->isStateDirty[idx] |= (1 << shift);
5725 void get_drawable_size_fbo(const struct wined3d_context *context, UINT *width, UINT *height)
5727 /* The drawable size of a fbo target is the opengl texture size, which is the power of two size. */
5728 *width = context->current_rt->pow2Width;
5729 *height = context->current_rt->pow2Height;
5732 void get_drawable_size_backbuffer(const struct wined3d_context *context, UINT *width, UINT *height)
5734 const struct wined3d_swapchain *swapchain = context->swapchain;
5735 /* The drawable size of a backbuffer / aux buffer offscreen target is the size of the
5736 * current context's drawable, which is the size of the back buffer of the swapchain
5737 * the active context belongs to. */
5738 *width = swapchain->desc.backbuffer_width;
5739 *height = swapchain->desc.backbuffer_height;
5742 LRESULT device_process_message(struct wined3d_device *device, HWND window, BOOL unicode,
5743 UINT message, WPARAM wparam, LPARAM lparam, WNDPROC proc)
5745 if (device->filter_messages)
5747 TRACE("Filtering message: window %p, message %#x, wparam %#lx, lparam %#lx.\n",
5748 window, message, wparam, lparam);
5750 return DefWindowProcW(window, message, wparam, lparam);
5752 return DefWindowProcA(window, message, wparam, lparam);
5755 if (message == WM_DESTROY)
5757 TRACE("unregister window %p.\n", window);
5758 wined3d_unregister_window(window);
5760 if (InterlockedCompareExchangePointer((void **)&device->focus_window, NULL, window) != window)
5761 ERR("Window %p is not the focus window for device %p.\n", window, device);
5763 else if (message == WM_DISPLAYCHANGE)
5765 device->device_parent->ops->mode_changed(device->device_parent);
5769 return CallWindowProcW(proc, window, message, wparam, lparam);
5771 return CallWindowProcA(proc, window, message, wparam, lparam);