xref: /reactos/dll/directx/wine/wined3d/device.c (revision 81cffd76)
1c2c66affSColin Finck /*
2c2c66affSColin Finck  * Copyright 2002 Lionel Ulmer
3c2c66affSColin Finck  * Copyright 2002-2005 Jason Edmeades
4c2c66affSColin Finck  * Copyright 2003-2004 Raphael Junqueira
5c2c66affSColin Finck  * Copyright 2004 Christian Costa
6c2c66affSColin Finck  * Copyright 2005 Oliver Stieber
7c2c66affSColin Finck  * Copyright 2006-2008 Stefan Dösinger for CodeWeavers
8c2c66affSColin Finck  * Copyright 2006-2008 Henri Verbeet
9c2c66affSColin Finck  * Copyright 2007 Andrew Riedi
10c2c66affSColin Finck  * Copyright 2009-2011 Henri Verbeet for CodeWeavers
11c2c66affSColin Finck  *
12c2c66affSColin Finck  * This library is free software; you can redistribute it and/or
13c2c66affSColin Finck  * modify it under the terms of the GNU Lesser General Public
14c2c66affSColin Finck  * License as published by the Free Software Foundation; either
15c2c66affSColin Finck  * version 2.1 of the License, or (at your option) any later version.
16c2c66affSColin Finck  *
17c2c66affSColin Finck  * This library is distributed in the hope that it will be useful,
18c2c66affSColin Finck  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19c2c66affSColin Finck  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20c2c66affSColin Finck  * Lesser General Public License for more details.
21c2c66affSColin Finck  *
22c2c66affSColin Finck  * You should have received a copy of the GNU Lesser General Public
23c2c66affSColin Finck  * License along with this library; if not, write to the Free Software
24c2c66affSColin Finck  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25c2c66affSColin Finck  */
26c2c66affSColin Finck 
279987f029SAmine Khaldi #include "config.h"
289987f029SAmine Khaldi #include "wine/port.h"
299987f029SAmine Khaldi 
309987f029SAmine Khaldi #include <stdio.h>
319987f029SAmine Khaldi #ifdef HAVE_FLOAT_H
329987f029SAmine Khaldi # include <float.h>
339987f029SAmine Khaldi #endif
349987f029SAmine Khaldi 
35c2c66affSColin Finck #include "wined3d_private.h"
36c2c66affSColin Finck 
37c2c66affSColin Finck WINE_DEFAULT_DEBUG_CHANNEL(d3d);
38c2c66affSColin Finck WINE_DECLARE_DEBUG_CHANNEL(winediag);
39c2c66affSColin Finck 
40c2c66affSColin Finck /* Define the default light parameters as specified by MSDN. */
41c2c66affSColin Finck const struct wined3d_light WINED3D_default_light =
42c2c66affSColin Finck {
43c2c66affSColin Finck     WINED3D_LIGHT_DIRECTIONAL,  /* Type */
44c2c66affSColin Finck     { 1.0f, 1.0f, 1.0f, 0.0f }, /* Diffuse r,g,b,a */
45c2c66affSColin Finck     { 0.0f, 0.0f, 0.0f, 0.0f }, /* Specular r,g,b,a */
46c2c66affSColin Finck     { 0.0f, 0.0f, 0.0f, 0.0f }, /* Ambient r,g,b,a, */
47c2c66affSColin Finck     { 0.0f, 0.0f, 0.0f },       /* Position x,y,z */
48c2c66affSColin Finck     { 0.0f, 0.0f, 1.0f },       /* Direction x,y,z */
49c2c66affSColin Finck     0.0f,                       /* Range */
50c2c66affSColin Finck     0.0f,                       /* Falloff */
51c2c66affSColin Finck     0.0f, 0.0f, 0.0f,           /* Attenuation 0,1,2 */
52c2c66affSColin Finck     0.0f,                       /* Theta */
53c2c66affSColin Finck     0.0f                        /* Phi */
54c2c66affSColin Finck };
55c2c66affSColin Finck 
56c2c66affSColin Finck /* Note that except for WINED3DPT_POINTLIST and WINED3DPT_LINELIST these
57c2c66affSColin Finck  * actually have the same values in GL and D3D. */
gl_primitive_type_from_d3d(enum wined3d_primitive_type primitive_type)58c2c66affSColin Finck GLenum gl_primitive_type_from_d3d(enum wined3d_primitive_type primitive_type)
59c2c66affSColin Finck {
60c2c66affSColin Finck     switch (primitive_type)
61c2c66affSColin Finck     {
62c2c66affSColin Finck         case WINED3D_PT_POINTLIST:
63c2c66affSColin Finck             return GL_POINTS;
64c2c66affSColin Finck 
65c2c66affSColin Finck         case WINED3D_PT_LINELIST:
66c2c66affSColin Finck             return GL_LINES;
67c2c66affSColin Finck 
68c2c66affSColin Finck         case WINED3D_PT_LINESTRIP:
69c2c66affSColin Finck             return GL_LINE_STRIP;
70c2c66affSColin Finck 
71c2c66affSColin Finck         case WINED3D_PT_TRIANGLELIST:
72c2c66affSColin Finck             return GL_TRIANGLES;
73c2c66affSColin Finck 
74c2c66affSColin Finck         case WINED3D_PT_TRIANGLESTRIP:
75c2c66affSColin Finck             return GL_TRIANGLE_STRIP;
76c2c66affSColin Finck 
77c2c66affSColin Finck         case WINED3D_PT_TRIANGLEFAN:
78c2c66affSColin Finck             return GL_TRIANGLE_FAN;
79c2c66affSColin Finck 
80c2c66affSColin Finck         case WINED3D_PT_LINELIST_ADJ:
81c2c66affSColin Finck             return GL_LINES_ADJACENCY_ARB;
82c2c66affSColin Finck 
83c2c66affSColin Finck         case WINED3D_PT_LINESTRIP_ADJ:
84c2c66affSColin Finck             return GL_LINE_STRIP_ADJACENCY_ARB;
85c2c66affSColin Finck 
86c2c66affSColin Finck         case WINED3D_PT_TRIANGLELIST_ADJ:
87c2c66affSColin Finck             return GL_TRIANGLES_ADJACENCY_ARB;
88c2c66affSColin Finck 
89c2c66affSColin Finck         case WINED3D_PT_TRIANGLESTRIP_ADJ:
90c2c66affSColin Finck             return GL_TRIANGLE_STRIP_ADJACENCY_ARB;
91c2c66affSColin Finck 
92c2c66affSColin Finck         case WINED3D_PT_PATCH:
93c2c66affSColin Finck             return GL_PATCHES;
94c2c66affSColin Finck 
95c2c66affSColin Finck         default:
96c2c66affSColin Finck             FIXME("Unhandled primitive type %s.\n", debug_d3dprimitivetype(primitive_type));
97c2c66affSColin Finck         case WINED3D_PT_UNDEFINED:
98c2c66affSColin Finck             return ~0u;
99c2c66affSColin Finck     }
100c2c66affSColin Finck }
101c2c66affSColin Finck 
d3d_primitive_type_from_gl(GLenum primitive_type)102*81cffd76SJoachim Henze static enum wined3d_primitive_type d3d_primitive_type_from_gl(GLenum primitive_type)
103c2c66affSColin Finck {
104c2c66affSColin Finck     switch (primitive_type)
105c2c66affSColin Finck     {
106c2c66affSColin Finck         case GL_POINTS:
107c2c66affSColin Finck             return WINED3D_PT_POINTLIST;
108c2c66affSColin Finck 
109c2c66affSColin Finck         case GL_LINES:
110c2c66affSColin Finck             return WINED3D_PT_LINELIST;
111c2c66affSColin Finck 
112c2c66affSColin Finck         case GL_LINE_STRIP:
113c2c66affSColin Finck             return WINED3D_PT_LINESTRIP;
114c2c66affSColin Finck 
115c2c66affSColin Finck         case GL_TRIANGLES:
116c2c66affSColin Finck             return WINED3D_PT_TRIANGLELIST;
117c2c66affSColin Finck 
118c2c66affSColin Finck         case GL_TRIANGLE_STRIP:
119c2c66affSColin Finck             return WINED3D_PT_TRIANGLESTRIP;
120c2c66affSColin Finck 
121c2c66affSColin Finck         case GL_TRIANGLE_FAN:
122c2c66affSColin Finck             return WINED3D_PT_TRIANGLEFAN;
123c2c66affSColin Finck 
124c2c66affSColin Finck         case GL_LINES_ADJACENCY_ARB:
125c2c66affSColin Finck             return WINED3D_PT_LINELIST_ADJ;
126c2c66affSColin Finck 
127c2c66affSColin Finck         case GL_LINE_STRIP_ADJACENCY_ARB:
128c2c66affSColin Finck             return WINED3D_PT_LINESTRIP_ADJ;
129c2c66affSColin Finck 
130c2c66affSColin Finck         case GL_TRIANGLES_ADJACENCY_ARB:
131c2c66affSColin Finck             return WINED3D_PT_TRIANGLELIST_ADJ;
132c2c66affSColin Finck 
133c2c66affSColin Finck         case GL_TRIANGLE_STRIP_ADJACENCY_ARB:
134c2c66affSColin Finck             return WINED3D_PT_TRIANGLESTRIP_ADJ;
135c2c66affSColin Finck 
136c2c66affSColin Finck         case GL_PATCHES:
137c2c66affSColin Finck             return WINED3D_PT_PATCH;
138c2c66affSColin Finck 
139c2c66affSColin Finck         default:
140c2c66affSColin Finck             FIXME("Unhandled primitive type %s.\n", debug_d3dprimitivetype(primitive_type));
141c2c66affSColin Finck         case ~0u:
142c2c66affSColin Finck             return WINED3D_PT_UNDEFINED;
143c2c66affSColin Finck     }
144c2c66affSColin Finck }
145c2c66affSColin Finck 
device_context_add(struct wined3d_device * device,struct wined3d_context * context)146c2c66affSColin Finck BOOL device_context_add(struct wined3d_device *device, struct wined3d_context *context)
147c2c66affSColin Finck {
148c2c66affSColin Finck     struct wined3d_context **new_array;
149c2c66affSColin Finck 
150c2c66affSColin Finck     TRACE("Adding context %p.\n", context);
151c2c66affSColin Finck 
1529987f029SAmine Khaldi     if (!(new_array = heap_realloc(device->contexts, sizeof(*new_array) * (device->context_count + 1))))
153c2c66affSColin Finck     {
154c2c66affSColin Finck         ERR("Failed to grow the context array.\n");
155c2c66affSColin Finck         return FALSE;
156c2c66affSColin Finck     }
157c2c66affSColin Finck 
158c2c66affSColin Finck     new_array[device->context_count++] = context;
159c2c66affSColin Finck     device->contexts = new_array;
160c2c66affSColin Finck     return TRUE;
161c2c66affSColin Finck }
162c2c66affSColin Finck 
device_context_remove(struct wined3d_device * device,struct wined3d_context * context)163c2c66affSColin Finck void device_context_remove(struct wined3d_device *device, struct wined3d_context *context)
164c2c66affSColin Finck {
165c2c66affSColin Finck     struct wined3d_context **new_array;
166c2c66affSColin Finck     BOOL found = FALSE;
167c2c66affSColin Finck     UINT i;
168c2c66affSColin Finck 
169c2c66affSColin Finck     TRACE("Removing context %p.\n", context);
170c2c66affSColin Finck 
171c2c66affSColin Finck     for (i = 0; i < device->context_count; ++i)
172c2c66affSColin Finck     {
173c2c66affSColin Finck         if (device->contexts[i] == context)
174c2c66affSColin Finck         {
175c2c66affSColin Finck             found = TRUE;
176c2c66affSColin Finck             break;
177c2c66affSColin Finck         }
178c2c66affSColin Finck     }
179c2c66affSColin Finck 
180c2c66affSColin Finck     if (!found)
181c2c66affSColin Finck     {
182c2c66affSColin Finck         ERR("Context %p doesn't exist in context array.\n", context);
183c2c66affSColin Finck         return;
184c2c66affSColin Finck     }
185c2c66affSColin Finck 
186c2c66affSColin Finck     if (!--device->context_count)
187c2c66affSColin Finck     {
1889987f029SAmine Khaldi         heap_free(device->contexts);
189c2c66affSColin Finck         device->contexts = NULL;
190c2c66affSColin Finck         return;
191c2c66affSColin Finck     }
192c2c66affSColin Finck 
193c2c66affSColin Finck     memmove(&device->contexts[i], &device->contexts[i + 1], (device->context_count - i) * sizeof(*device->contexts));
1949987f029SAmine Khaldi     if (!(new_array = heap_realloc(device->contexts, device->context_count * sizeof(*device->contexts))))
195c2c66affSColin Finck     {
196c2c66affSColin Finck         ERR("Failed to shrink context array. Oh well.\n");
197c2c66affSColin Finck         return;
198c2c66affSColin Finck     }
199c2c66affSColin Finck 
200c2c66affSColin Finck     device->contexts = new_array;
201c2c66affSColin Finck }
202c2c66affSColin Finck 
is_full_clear(const struct wined3d_texture * texture,unsigned int sub_resource_idx,const RECT * draw_rect,const RECT * clear_rect)2039987f029SAmine Khaldi static BOOL is_full_clear(const struct wined3d_texture *texture, unsigned int sub_resource_idx,
2049987f029SAmine Khaldi         const RECT *draw_rect, const RECT *clear_rect)
205c2c66affSColin Finck {
2069987f029SAmine Khaldi     unsigned int width, height, level;
2079987f029SAmine Khaldi 
2089987f029SAmine Khaldi     level = sub_resource_idx % texture->level_count;
2099987f029SAmine Khaldi     width = wined3d_texture_get_level_width(texture, level);
2109987f029SAmine Khaldi     height = wined3d_texture_get_level_height(texture, level);
211c2c66affSColin Finck 
212c2c66affSColin Finck     /* partial draw rect */
213c2c66affSColin Finck     if (draw_rect->left || draw_rect->top || draw_rect->right < width || draw_rect->bottom < height)
214c2c66affSColin Finck         return FALSE;
215c2c66affSColin Finck 
216c2c66affSColin Finck     /* partial clear rect */
217c2c66affSColin Finck     if (clear_rect && (clear_rect->left > 0 || clear_rect->top > 0
218c2c66affSColin Finck             || clear_rect->right < width || clear_rect->bottom < height))
219c2c66affSColin Finck         return FALSE;
220c2c66affSColin Finck 
221c2c66affSColin Finck     return TRUE;
222c2c66affSColin Finck }
223c2c66affSColin Finck 
device_clear_render_targets(struct wined3d_device * device,UINT rt_count,const struct wined3d_fb_state * fb,UINT rect_count,const RECT * clear_rect,const RECT * draw_rect,DWORD flags,const struct wined3d_color * color,float depth,DWORD stencil)224c2c66affSColin Finck void device_clear_render_targets(struct wined3d_device *device, UINT rt_count, const struct wined3d_fb_state *fb,
225c2c66affSColin Finck         UINT rect_count, const RECT *clear_rect, const RECT *draw_rect, DWORD flags, const struct wined3d_color *color,
226c2c66affSColin Finck         float depth, DWORD stencil)
227c2c66affSColin Finck {
228c2c66affSColin Finck     struct wined3d_rendertarget_view *rtv = rt_count ? fb->render_targets[0] : NULL;
229*81cffd76SJoachim Henze     struct wined3d_surface *target = rtv ? wined3d_rendertarget_view_get_surface(rtv) : NULL;
230c2c66affSColin Finck     struct wined3d_rendertarget_view *dsv = fb->depth_stencil;
231*81cffd76SJoachim Henze     struct wined3d_surface *depth_stencil = dsv ? wined3d_rendertarget_view_get_surface(dsv) : NULL;
232c2c66affSColin Finck     const struct wined3d_state *state = &device->cs->state;
233c2c66affSColin Finck     const struct wined3d_gl_info *gl_info;
234c2c66affSColin Finck     UINT drawable_width, drawable_height;
235c2c66affSColin Finck     struct wined3d_color corrected_color;
236c2c66affSColin Finck     struct wined3d_context *context;
237c2c66affSColin Finck     GLbitfield clear_mask = 0;
238c2c66affSColin Finck     BOOL render_offscreen;
239c2c66affSColin Finck     unsigned int i;
240c2c66affSColin Finck 
241*81cffd76SJoachim Henze     if (target)
242*81cffd76SJoachim Henze         context = context_acquire(device, target->container, rtv->sub_resource_idx);
243c2c66affSColin Finck     else
244c2c66affSColin Finck         context = context_acquire(device, NULL, 0);
245c2c66affSColin Finck     if (!context->valid)
246c2c66affSColin Finck     {
247c2c66affSColin Finck         context_release(context);
248c2c66affSColin Finck         WARN("Invalid context, skipping clear.\n");
249c2c66affSColin Finck         return;
250c2c66affSColin Finck     }
251c2c66affSColin Finck     gl_info = context->gl_info;
252c2c66affSColin Finck 
253c2c66affSColin Finck     /* When we're clearing parts of the drawable, make sure that the target surface is well up to date in the
254c2c66affSColin Finck      * drawable. After the clear we'll mark the drawable up to date, so we have to make sure that this is true
255c2c66affSColin Finck      * for the cleared parts, and the untouched parts.
256c2c66affSColin Finck      *
257c2c66affSColin Finck      * If we're clearing the whole target there is no need to copy it into the drawable, it will be overwritten
258c2c66affSColin Finck      * anyway. If we're not clearing the color buffer we don't have to copy either since we're not going to set
259c2c66affSColin Finck      * the drawable up to date. We have to check all settings that limit the clear area though. Do not bother
260c2c66affSColin Finck      * checking all this if the dest surface is in the drawable anyway. */
261c2c66affSColin Finck     for (i = 0; i < rt_count; ++i)
262c2c66affSColin Finck     {
263c2c66affSColin Finck         struct wined3d_rendertarget_view *rtv = fb->render_targets[i];
264c2c66affSColin Finck 
265c2c66affSColin Finck         if (rtv && rtv->format->id != WINED3DFMT_NULL)
266c2c66affSColin Finck         {
267c2c66affSColin Finck             struct wined3d_texture *rt = wined3d_texture_from_resource(rtv->resource);
268c2c66affSColin Finck 
2699987f029SAmine Khaldi             if (flags & WINED3DCLEAR_TARGET && !is_full_clear(rt, rtv->sub_resource_idx,
2709987f029SAmine Khaldi                     draw_rect, rect_count ? clear_rect : NULL))
271c2c66affSColin Finck                 wined3d_texture_load_location(rt, rtv->sub_resource_idx, context, rtv->resource->draw_binding);
272c2c66affSColin Finck             else
273c2c66affSColin Finck                 wined3d_texture_prepare_location(rt, rtv->sub_resource_idx, context, rtv->resource->draw_binding);
274c2c66affSColin Finck         }
275c2c66affSColin Finck     }
276c2c66affSColin Finck 
277c2c66affSColin Finck     if (target)
278c2c66affSColin Finck     {
279c2c66affSColin Finck         render_offscreen = context->render_offscreen;
280c2c66affSColin Finck         wined3d_rendertarget_view_get_drawable_size(rtv, context, &drawable_width, &drawable_height);
281c2c66affSColin Finck     }
282c2c66affSColin Finck     else
283c2c66affSColin Finck     {
284*81cffd76SJoachim Henze         unsigned int ds_level = dsv->sub_resource_idx % depth_stencil->container->level_count;
2859987f029SAmine Khaldi 
286c2c66affSColin Finck         render_offscreen = TRUE;
287*81cffd76SJoachim Henze         drawable_width = wined3d_texture_get_level_pow2_width(depth_stencil->container, ds_level);
288*81cffd76SJoachim Henze         drawable_height = wined3d_texture_get_level_pow2_height(depth_stencil->container, ds_level);
289c2c66affSColin Finck     }
290c2c66affSColin Finck 
2919987f029SAmine Khaldi     if (depth_stencil)
292c2c66affSColin Finck     {
2939987f029SAmine Khaldi         DWORD ds_location = render_offscreen ? dsv->resource->draw_binding : WINED3D_LOCATION_DRAWABLE;
2949987f029SAmine Khaldi         struct wined3d_texture *ds = wined3d_texture_from_resource(dsv->resource);
295c2c66affSColin Finck 
2969987f029SAmine Khaldi         if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL)
2979987f029SAmine Khaldi                 && !is_full_clear(ds, dsv->sub_resource_idx, draw_rect, rect_count ? clear_rect : NULL))
2989987f029SAmine Khaldi             wined3d_texture_load_location(ds, dsv->sub_resource_idx, context, ds_location);
2999987f029SAmine Khaldi         else
3009987f029SAmine Khaldi             wined3d_texture_prepare_location(ds, dsv->sub_resource_idx, context, ds_location);
3019987f029SAmine Khaldi 
3029987f029SAmine Khaldi         if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
3039987f029SAmine Khaldi         {
3049987f029SAmine Khaldi             wined3d_texture_validate_location(ds, dsv->sub_resource_idx, ds_location);
3059987f029SAmine Khaldi             wined3d_texture_invalidate_location(ds, dsv->sub_resource_idx, ~ds_location);
3069987f029SAmine Khaldi         }
307c2c66affSColin Finck     }
308c2c66affSColin Finck 
309c2c66affSColin Finck     if (!context_apply_clear_state(context, state, rt_count, fb))
310c2c66affSColin Finck     {
311c2c66affSColin Finck         context_release(context);
312c2c66affSColin Finck         WARN("Failed to apply clear state, skipping clear.\n");
313c2c66affSColin Finck         return;
314c2c66affSColin Finck     }
315c2c66affSColin Finck 
316c2c66affSColin Finck     /* Only set the values up once, as they are not changing. */
317c2c66affSColin Finck     if (flags & WINED3DCLEAR_STENCIL)
318c2c66affSColin Finck     {
319c2c66affSColin Finck         if (gl_info->supported[EXT_STENCIL_TWO_SIDE])
320c2c66affSColin Finck         {
321c2c66affSColin Finck             gl_info->gl_ops.gl.p_glDisable(GL_STENCIL_TEST_TWO_SIDE_EXT);
322c2c66affSColin Finck             context_invalidate_state(context, STATE_RENDER(WINED3D_RS_TWOSIDEDSTENCILMODE));
323c2c66affSColin Finck         }
324c2c66affSColin Finck         gl_info->gl_ops.gl.p_glStencilMask(~0U);
325c2c66affSColin Finck         context_invalidate_state(context, STATE_RENDER(WINED3D_RS_STENCILWRITEMASK));
326c2c66affSColin Finck         gl_info->gl_ops.gl.p_glClearStencil(stencil);
327c2c66affSColin Finck         checkGLcall("glClearStencil");
328c2c66affSColin Finck         clear_mask = clear_mask | GL_STENCIL_BUFFER_BIT;
329c2c66affSColin Finck     }
330c2c66affSColin Finck 
331c2c66affSColin Finck     if (flags & WINED3DCLEAR_ZBUFFER)
332c2c66affSColin Finck     {
333c2c66affSColin Finck         gl_info->gl_ops.gl.p_glDepthMask(GL_TRUE);
334c2c66affSColin Finck         context_invalidate_state(context, STATE_RENDER(WINED3D_RS_ZWRITEENABLE));
335c2c66affSColin Finck         gl_info->gl_ops.gl.p_glClearDepth(depth);
336c2c66affSColin Finck         checkGLcall("glClearDepth");
337c2c66affSColin Finck         clear_mask = clear_mask | GL_DEPTH_BUFFER_BIT;
338c2c66affSColin Finck     }
339c2c66affSColin Finck 
340c2c66affSColin Finck     if (flags & WINED3DCLEAR_TARGET)
341c2c66affSColin Finck     {
342c2c66affSColin Finck         for (i = 0; i < rt_count; ++i)
343c2c66affSColin Finck         {
344c2c66affSColin Finck             struct wined3d_rendertarget_view *rtv = fb->render_targets[i];
345c2c66affSColin Finck             struct wined3d_texture *texture;
346c2c66affSColin Finck 
347c2c66affSColin Finck             if (!rtv)
348c2c66affSColin Finck                 continue;
349c2c66affSColin Finck 
350c2c66affSColin Finck             if (rtv->resource->type == WINED3D_RTYPE_BUFFER)
351c2c66affSColin Finck             {
352c2c66affSColin Finck                 FIXME("Not supported on buffer resources.\n");
353c2c66affSColin Finck                 continue;
354c2c66affSColin Finck             }
355c2c66affSColin Finck 
356c2c66affSColin Finck             texture = texture_from_resource(rtv->resource);
357c2c66affSColin Finck             wined3d_texture_validate_location(texture, rtv->sub_resource_idx, rtv->resource->draw_binding);
358c2c66affSColin Finck             wined3d_texture_invalidate_location(texture, rtv->sub_resource_idx, ~rtv->resource->draw_binding);
359c2c66affSColin Finck         }
360c2c66affSColin Finck 
361c2c66affSColin Finck         if (!gl_info->supported[ARB_FRAMEBUFFER_SRGB] && needs_srgb_write(context, state, fb))
362c2c66affSColin Finck         {
363c2c66affSColin Finck             if (rt_count > 1)
364c2c66affSColin Finck                 WARN("Clearing multiple sRGB render targets with no GL_ARB_framebuffer_sRGB "
365c2c66affSColin Finck                         "support, this might cause graphical issues.\n");
366c2c66affSColin Finck 
367c2c66affSColin Finck             corrected_color.r = color->r < wined3d_srgb_const1[0]
368c2c66affSColin Finck                     ? color->r * wined3d_srgb_const0[3]
369c2c66affSColin Finck                     : pow(color->r, wined3d_srgb_const0[0]) * wined3d_srgb_const0[1]
370c2c66affSColin Finck                     - wined3d_srgb_const0[2];
371c2c66affSColin Finck             corrected_color.r = min(max(corrected_color.r, 0.0f), 1.0f);
372c2c66affSColin Finck             corrected_color.g = color->g < wined3d_srgb_const1[0]
373c2c66affSColin Finck                     ? color->g * wined3d_srgb_const0[3]
374c2c66affSColin Finck                     : pow(color->g, wined3d_srgb_const0[0]) * wined3d_srgb_const0[1]
375c2c66affSColin Finck                     - wined3d_srgb_const0[2];
376c2c66affSColin Finck             corrected_color.g = min(max(corrected_color.g, 0.0f), 1.0f);
377c2c66affSColin Finck             corrected_color.b = color->b < wined3d_srgb_const1[0]
378c2c66affSColin Finck                     ? color->b * wined3d_srgb_const0[3]
379c2c66affSColin Finck                     : pow(color->b, wined3d_srgb_const0[0]) * wined3d_srgb_const0[1]
380c2c66affSColin Finck                     - wined3d_srgb_const0[2];
381c2c66affSColin Finck             corrected_color.b = min(max(corrected_color.b, 0.0f), 1.0f);
382c2c66affSColin Finck             color = &corrected_color;
383c2c66affSColin Finck         }
384c2c66affSColin Finck 
385c2c66affSColin Finck         gl_info->gl_ops.gl.p_glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
3869987f029SAmine Khaldi         for (i = 0; i < MAX_RENDER_TARGETS; ++i)
3879987f029SAmine Khaldi             context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITE(i)));
388c2c66affSColin Finck         gl_info->gl_ops.gl.p_glClearColor(color->r, color->g, color->b, color->a);
389c2c66affSColin Finck         checkGLcall("glClearColor");
390c2c66affSColin Finck         clear_mask = clear_mask | GL_COLOR_BUFFER_BIT;
391c2c66affSColin Finck     }
392c2c66affSColin Finck 
393c2c66affSColin Finck     if (!rect_count)
394c2c66affSColin Finck     {
395c2c66affSColin Finck         if (render_offscreen)
396c2c66affSColin Finck         {
397c2c66affSColin Finck             gl_info->gl_ops.gl.p_glScissor(draw_rect->left, draw_rect->top,
398c2c66affSColin Finck                     draw_rect->right - draw_rect->left, draw_rect->bottom - draw_rect->top);
399c2c66affSColin Finck         }
400c2c66affSColin Finck         else
401c2c66affSColin Finck         {
402c2c66affSColin Finck             gl_info->gl_ops.gl.p_glScissor(draw_rect->left, drawable_height - draw_rect->bottom,
403c2c66affSColin Finck                         draw_rect->right - draw_rect->left, draw_rect->bottom - draw_rect->top);
404c2c66affSColin Finck         }
405*81cffd76SJoachim Henze         checkGLcall("glScissor");
406c2c66affSColin Finck         gl_info->gl_ops.gl.p_glClear(clear_mask);
407*81cffd76SJoachim Henze         checkGLcall("glClear");
408c2c66affSColin Finck     }
409c2c66affSColin Finck     else
410c2c66affSColin Finck     {
411c2c66affSColin Finck         RECT current_rect;
412c2c66affSColin Finck 
413c2c66affSColin Finck         /* Now process each rect in turn. */
414c2c66affSColin Finck         for (i = 0; i < rect_count; ++i)
415c2c66affSColin Finck         {
416c2c66affSColin Finck             /* Note that GL uses lower left, width/height. */
417c2c66affSColin Finck             IntersectRect(&current_rect, draw_rect, &clear_rect[i]);
418c2c66affSColin Finck 
419c2c66affSColin Finck             TRACE("clear_rect[%u] %s, current_rect %s.\n", i,
420c2c66affSColin Finck                     wine_dbgstr_rect(&clear_rect[i]),
421c2c66affSColin Finck                     wine_dbgstr_rect(&current_rect));
422c2c66affSColin Finck 
423c2c66affSColin Finck             /* Tests show that rectangles where x1 > x2 or y1 > y2 are ignored silently.
424c2c66affSColin Finck              * The rectangle is not cleared, no error is returned, but further rectangles are
425c2c66affSColin Finck              * still cleared if they are valid. */
426c2c66affSColin Finck             if (current_rect.left > current_rect.right || current_rect.top > current_rect.bottom)
427c2c66affSColin Finck             {
428c2c66affSColin Finck                 TRACE("Rectangle with negative dimensions, ignoring.\n");
429c2c66affSColin Finck                 continue;
430c2c66affSColin Finck             }
431c2c66affSColin Finck 
432c2c66affSColin Finck             if (render_offscreen)
433c2c66affSColin Finck             {
434c2c66affSColin Finck                 gl_info->gl_ops.gl.p_glScissor(current_rect.left, current_rect.top,
435c2c66affSColin Finck                         current_rect.right - current_rect.left, current_rect.bottom - current_rect.top);
436c2c66affSColin Finck             }
437c2c66affSColin Finck             else
438c2c66affSColin Finck             {
439c2c66affSColin Finck                 gl_info->gl_ops.gl.p_glScissor(current_rect.left, drawable_height - current_rect.bottom,
440c2c66affSColin Finck                           current_rect.right - current_rect.left, current_rect.bottom - current_rect.top);
441c2c66affSColin Finck             }
442*81cffd76SJoachim Henze             checkGLcall("glScissor");
443c2c66affSColin Finck 
444*81cffd76SJoachim Henze             gl_info->gl_ops.gl.p_glClear(clear_mask);
445*81cffd76SJoachim Henze             checkGLcall("glClear");
446*81cffd76SJoachim Henze         }
447*81cffd76SJoachim Henze     }
448*81cffd76SJoachim Henze 
449*81cffd76SJoachim Henze     if (wined3d_settings.strict_draw_ordering || (flags & WINED3DCLEAR_TARGET
450*81cffd76SJoachim Henze             && target->container->swapchain && target->container->swapchain->front_buffer == target->container))
451*81cffd76SJoachim Henze         gl_info->gl_ops.gl.p_glFlush(); /* Flush to ensure ordering across contexts. */
452c2c66affSColin Finck 
453c2c66affSColin Finck     context_release(context);
454c2c66affSColin Finck }
455c2c66affSColin Finck 
wined3d_device_incref(struct wined3d_device * device)456c2c66affSColin Finck ULONG CDECL wined3d_device_incref(struct wined3d_device *device)
457c2c66affSColin Finck {
458c2c66affSColin Finck     ULONG refcount = InterlockedIncrement(&device->ref);
459c2c66affSColin Finck 
460c2c66affSColin Finck     TRACE("%p increasing refcount to %u.\n", device, refcount);
461c2c66affSColin Finck 
462c2c66affSColin Finck     return refcount;
463c2c66affSColin Finck }
464c2c66affSColin Finck 
device_leftover_sampler(struct wine_rb_entry * entry,void * context)465c2c66affSColin Finck static void device_leftover_sampler(struct wine_rb_entry *entry, void *context)
466c2c66affSColin Finck {
467c2c66affSColin Finck     struct wined3d_sampler *sampler = WINE_RB_ENTRY_VALUE(entry, struct wined3d_sampler, entry);
468c2c66affSColin Finck 
469c2c66affSColin Finck     ERR("Leftover sampler %p.\n", sampler);
470c2c66affSColin Finck }
471c2c66affSColin Finck 
wined3d_device_decref(struct wined3d_device * device)472c2c66affSColin Finck ULONG CDECL wined3d_device_decref(struct wined3d_device *device)
473c2c66affSColin Finck {
474c2c66affSColin Finck     ULONG refcount = InterlockedDecrement(&device->ref);
475c2c66affSColin Finck 
476c2c66affSColin Finck     TRACE("%p decreasing refcount to %u.\n", device, refcount);
477c2c66affSColin Finck 
478c2c66affSColin Finck     if (!refcount)
479c2c66affSColin Finck     {
480c2c66affSColin Finck         UINT i;
481c2c66affSColin Finck 
482c2c66affSColin Finck         wined3d_cs_destroy(device->cs);
483c2c66affSColin Finck 
484c2c66affSColin Finck         if (device->recording && wined3d_stateblock_decref(device->recording))
485c2c66affSColin Finck             ERR("Something's still holding the recording stateblock.\n");
486c2c66affSColin Finck         device->recording = NULL;
487c2c66affSColin Finck 
488c2c66affSColin Finck         state_cleanup(&device->state);
489c2c66affSColin Finck 
490d6ac0a71SAmine Khaldi         for (i = 0; i < ARRAY_SIZE(device->multistate_funcs); ++i)
491c2c66affSColin Finck         {
4929987f029SAmine Khaldi             heap_free(device->multistate_funcs[i]);
493c2c66affSColin Finck             device->multistate_funcs[i] = NULL;
494c2c66affSColin Finck         }
495c2c66affSColin Finck 
496c2c66affSColin Finck         if (!list_empty(&device->resources))
497c2c66affSColin Finck         {
498c2c66affSColin Finck             struct wined3d_resource *resource;
499c2c66affSColin Finck 
500c2c66affSColin Finck             ERR("Device released with resources still bound.\n");
501c2c66affSColin Finck 
502c2c66affSColin Finck             LIST_FOR_EACH_ENTRY(resource, &device->resources, struct wined3d_resource, resource_list_entry)
503c2c66affSColin Finck             {
504c2c66affSColin Finck                 ERR("Leftover resource %p with type %s (%#x).\n",
505c2c66affSColin Finck                         resource, debug_d3dresourcetype(resource->type), resource->type);
506c2c66affSColin Finck             }
507c2c66affSColin Finck         }
508c2c66affSColin Finck 
509c2c66affSColin Finck         if (device->contexts)
510c2c66affSColin Finck             ERR("Context array not freed!\n");
511c2c66affSColin Finck         if (device->hardwareCursor)
512c2c66affSColin Finck             DestroyCursor(device->hardwareCursor);
513c2c66affSColin Finck         device->hardwareCursor = 0;
514c2c66affSColin Finck 
515c2c66affSColin Finck         wine_rb_destroy(&device->samplers, device_leftover_sampler, NULL);
516c2c66affSColin Finck 
517c2c66affSColin Finck         wined3d_decref(device->wined3d);
518c2c66affSColin Finck         device->wined3d = NULL;
5199987f029SAmine Khaldi         heap_free(device);
520c2c66affSColin Finck         TRACE("Freed device %p.\n", device);
521c2c66affSColin Finck     }
522c2c66affSColin Finck 
523c2c66affSColin Finck     return refcount;
524c2c66affSColin Finck }
525c2c66affSColin Finck 
wined3d_device_get_swapchain_count(const struct wined3d_device * device)526c2c66affSColin Finck UINT CDECL wined3d_device_get_swapchain_count(const struct wined3d_device *device)
527c2c66affSColin Finck {
528c2c66affSColin Finck     TRACE("device %p.\n", device);
529c2c66affSColin Finck 
530c2c66affSColin Finck     return device->swapchain_count;
531c2c66affSColin Finck }
532c2c66affSColin Finck 
wined3d_device_get_swapchain(const struct wined3d_device * device,UINT swapchain_idx)533c2c66affSColin Finck struct wined3d_swapchain * CDECL wined3d_device_get_swapchain(const struct wined3d_device *device, UINT swapchain_idx)
534c2c66affSColin Finck {
535c2c66affSColin Finck     TRACE("device %p, swapchain_idx %u.\n", device, swapchain_idx);
536c2c66affSColin Finck 
537c2c66affSColin Finck     if (swapchain_idx >= device->swapchain_count)
538c2c66affSColin Finck     {
539c2c66affSColin Finck         WARN("swapchain_idx %u >= swapchain_count %u.\n",
540c2c66affSColin Finck                 swapchain_idx, device->swapchain_count);
541c2c66affSColin Finck         return NULL;
542c2c66affSColin Finck     }
543c2c66affSColin Finck 
544c2c66affSColin Finck     return device->swapchains[swapchain_idx];
545c2c66affSColin Finck }
546c2c66affSColin Finck 
device_load_logo(struct wined3d_device * device,const char * filename)547c2c66affSColin Finck static void device_load_logo(struct wined3d_device *device, const char *filename)
548c2c66affSColin Finck {
549c2c66affSColin Finck     struct wined3d_color_key color_key;
550c2c66affSColin Finck     struct wined3d_resource_desc desc;
551c2c66affSColin Finck     HBITMAP hbm;
552c2c66affSColin Finck     BITMAP bm;
553c2c66affSColin Finck     HRESULT hr;
554c2c66affSColin Finck     HDC dcb = NULL, dcs = NULL;
555c2c66affSColin Finck 
556c2c66affSColin Finck     if (!(hbm = LoadImageA(NULL, filename, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION)))
557c2c66affSColin Finck     {
558c2c66affSColin Finck         ERR_(winediag)("Failed to load logo %s.\n", wine_dbgstr_a(filename));
559c2c66affSColin Finck         return;
560c2c66affSColin Finck     }
561c2c66affSColin Finck     GetObjectA(hbm, sizeof(BITMAP), &bm);
562c2c66affSColin Finck 
563c2c66affSColin Finck     if (!(dcb = CreateCompatibleDC(NULL)))
564c2c66affSColin Finck         goto out;
565c2c66affSColin Finck     SelectObject(dcb, hbm);
566c2c66affSColin Finck 
567c2c66affSColin Finck     desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
568c2c66affSColin Finck     desc.format = WINED3DFMT_B5G6R5_UNORM;
569c2c66affSColin Finck     desc.multisample_type = WINED3D_MULTISAMPLE_NONE;
570c2c66affSColin Finck     desc.multisample_quality = 0;
571c2c66affSColin Finck     desc.usage = WINED3DUSAGE_DYNAMIC;
5729987f029SAmine Khaldi     desc.access = WINED3D_RESOURCE_ACCESS_GPU;
573c2c66affSColin Finck     desc.width = bm.bmWidth;
574c2c66affSColin Finck     desc.height = bm.bmHeight;
575c2c66affSColin Finck     desc.depth = 1;
576c2c66affSColin Finck     desc.size = 0;
577c2c66affSColin Finck     if (FAILED(hr = wined3d_texture_create(device, &desc, 1, 1,
578c2c66affSColin Finck             WINED3D_TEXTURE_CREATE_MAPPABLE | WINED3D_TEXTURE_CREATE_GET_DC,
579c2c66affSColin Finck             NULL, NULL, &wined3d_null_parent_ops, &device->logo_texture)))
580c2c66affSColin Finck     {
581c2c66affSColin Finck         ERR("Wine logo requested, but failed to create texture, hr %#x.\n", hr);
582c2c66affSColin Finck         goto out;
583c2c66affSColin Finck     }
584c2c66affSColin Finck 
585c2c66affSColin Finck     if (FAILED(hr = wined3d_texture_get_dc(device->logo_texture, 0, &dcs)))
586c2c66affSColin Finck     {
587c2c66affSColin Finck         wined3d_texture_decref(device->logo_texture);
588c2c66affSColin Finck         device->logo_texture = NULL;
589c2c66affSColin Finck         goto out;
590c2c66affSColin Finck     }
591c2c66affSColin Finck     BitBlt(dcs, 0, 0, bm.bmWidth, bm.bmHeight, dcb, 0, 0, SRCCOPY);
592c2c66affSColin Finck     wined3d_texture_release_dc(device->logo_texture, 0, dcs);
593c2c66affSColin Finck 
594c2c66affSColin Finck     color_key.color_space_low_value = 0;
595c2c66affSColin Finck     color_key.color_space_high_value = 0;
596c2c66affSColin Finck     wined3d_texture_set_color_key(device->logo_texture, WINED3D_CKEY_SRC_BLT, &color_key);
597c2c66affSColin Finck 
598c2c66affSColin Finck out:
599c2c66affSColin Finck     if (dcb) DeleteDC(dcb);
600c2c66affSColin Finck     if (hbm) DeleteObject(hbm);
601c2c66affSColin Finck }
602c2c66affSColin Finck 
603c2c66affSColin Finck /* Context activation is done by the caller. */
create_dummy_textures(struct wined3d_device * device,struct wined3d_context * context)604c2c66affSColin Finck static void create_dummy_textures(struct wined3d_device *device, struct wined3d_context *context)
605c2c66affSColin Finck {
606c2c66affSColin Finck     const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
607c2c66affSColin Finck     const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
6089987f029SAmine Khaldi     struct wined3d_dummy_textures *textures = &device->dummy_textures;
609c2c66affSColin Finck     unsigned int i;
610c2c66affSColin Finck     DWORD color;
611c2c66affSColin Finck 
612c2c66affSColin Finck     if (d3d_info->wined3d_creation_flags & WINED3D_LEGACY_UNBOUND_RESOURCE_COLOR)
613c2c66affSColin Finck         color = 0x000000ff;
614c2c66affSColin Finck     else
615c2c66affSColin Finck         color = 0x00000000;
616c2c66affSColin Finck 
617c2c66affSColin Finck     /* Under DirectX you can sample even if no texture is bound, whereas
618c2c66affSColin Finck      * OpenGL will only allow that when a valid texture is bound.
619c2c66affSColin Finck      * We emulate this by creating dummy textures and binding them
620c2c66affSColin Finck      * to each texture stage when the currently set D3D texture is NULL. */
621c2c66affSColin Finck     context_active_texture(context, gl_info, 0);
622c2c66affSColin Finck 
6239987f029SAmine Khaldi     gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_1d);
6249987f029SAmine Khaldi     TRACE("Dummy 1D texture given name %u.\n", textures->tex_1d);
6259987f029SAmine Khaldi     gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_1D, textures->tex_1d);
6269987f029SAmine Khaldi     gl_info->gl_ops.gl.p_glTexImage1D(GL_TEXTURE_1D, 0, GL_RGBA8, 1, 0,
6279987f029SAmine Khaldi             GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
628c2c66affSColin Finck 
6299987f029SAmine Khaldi     gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_2d);
6309987f029SAmine Khaldi     TRACE("Dummy 2D texture given name %u.\n", textures->tex_2d);
6319987f029SAmine Khaldi     gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D, textures->tex_2d);
632c2c66affSColin Finck     gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 1, 1, 0,
633c2c66affSColin Finck             GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
634c2c66affSColin Finck 
635c2c66affSColin Finck     if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
636c2c66affSColin Finck     {
6379987f029SAmine Khaldi         gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_rect);
6389987f029SAmine Khaldi         TRACE("Dummy rectangle texture given name %u.\n", textures->tex_rect);
6399987f029SAmine Khaldi         gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_RECTANGLE_ARB, textures->tex_rect);
640c2c66affSColin Finck         gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA8, 1, 1, 0,
641c2c66affSColin Finck                 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
642c2c66affSColin Finck     }
643c2c66affSColin Finck 
644c2c66affSColin Finck     if (gl_info->supported[EXT_TEXTURE3D])
645c2c66affSColin Finck     {
6469987f029SAmine Khaldi         gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_3d);
6479987f029SAmine Khaldi         TRACE("Dummy 3D texture given name %u.\n", textures->tex_3d);
6489987f029SAmine Khaldi         gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_3D, textures->tex_3d);
649c2c66affSColin Finck         GL_EXTCALL(glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA8, 1, 1, 1, 0,
650c2c66affSColin Finck                     GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color));
651c2c66affSColin Finck     }
652c2c66affSColin Finck 
653c2c66affSColin Finck     if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
654c2c66affSColin Finck     {
6559987f029SAmine Khaldi         gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_cube);
6569987f029SAmine Khaldi         TRACE("Dummy cube texture given name %u.\n", textures->tex_cube);
6579987f029SAmine Khaldi         gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_CUBE_MAP, textures->tex_cube);
658c2c66affSColin Finck         for (i = GL_TEXTURE_CUBE_MAP_POSITIVE_X; i <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z; ++i)
659c2c66affSColin Finck         {
660c2c66affSColin Finck             gl_info->gl_ops.gl.p_glTexImage2D(i, 0, GL_RGBA8, 1, 1, 0,
661c2c66affSColin Finck                     GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
662c2c66affSColin Finck         }
663c2c66affSColin Finck     }
664c2c66affSColin Finck 
665c2c66affSColin Finck     if (gl_info->supported[ARB_TEXTURE_CUBE_MAP_ARRAY])
666c2c66affSColin Finck     {
667c2c66affSColin Finck         DWORD cube_array_data[6];
668c2c66affSColin Finck 
6699987f029SAmine Khaldi         gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_cube_array);
6709987f029SAmine Khaldi         TRACE("Dummy cube array texture given name %u.\n", textures->tex_cube_array);
6719987f029SAmine Khaldi         gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_CUBE_MAP_ARRAY, textures->tex_cube_array);
672c2c66affSColin Finck         for (i = 0; i < ARRAY_SIZE(cube_array_data); ++i)
673c2c66affSColin Finck             cube_array_data[i] = color;
674c2c66affSColin Finck         GL_EXTCALL(glTexImage3D(GL_TEXTURE_CUBE_MAP_ARRAY, 0, GL_RGBA8, 1, 1, 6, 0,
675c2c66affSColin Finck                     GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, cube_array_data));
676c2c66affSColin Finck     }
677c2c66affSColin Finck 
678c2c66affSColin Finck     if (gl_info->supported[EXT_TEXTURE_ARRAY])
679c2c66affSColin Finck     {
680*81cffd76SJoachim Henze 
6819987f029SAmine Khaldi         gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_1d_array);
6829987f029SAmine Khaldi         TRACE("Dummy 1D array texture given name %u.\n", textures->tex_1d_array);
6839987f029SAmine Khaldi         gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_1D_ARRAY, textures->tex_1d_array);
6849987f029SAmine Khaldi         gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_1D_ARRAY, 0, GL_RGBA8, 1, 1, 0,
6859987f029SAmine Khaldi                 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
686c2c66affSColin Finck 
6879987f029SAmine Khaldi         gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_2d_array);
6889987f029SAmine Khaldi         TRACE("Dummy 2D array texture given name %u.\n", textures->tex_2d_array);
6899987f029SAmine Khaldi         gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D_ARRAY, textures->tex_2d_array);
690c2c66affSColin Finck         GL_EXTCALL(glTexImage3D(GL_TEXTURE_2D_ARRAY, 0, GL_RGBA8, 1, 1, 1, 0,
691c2c66affSColin Finck                     GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color));
692c2c66affSColin Finck     }
693c2c66affSColin Finck 
694c2c66affSColin Finck     if (gl_info->supported[ARB_TEXTURE_BUFFER_OBJECT])
695c2c66affSColin Finck     {
696c2c66affSColin Finck         GLuint buffer;
697c2c66affSColin Finck 
698c2c66affSColin Finck         GL_EXTCALL(glGenBuffers(1, &buffer));
699c2c66affSColin Finck         GL_EXTCALL(glBindBuffer(GL_TEXTURE_BUFFER, buffer));
700c2c66affSColin Finck         GL_EXTCALL(glBufferData(GL_TEXTURE_BUFFER, sizeof(color), &color, GL_STATIC_DRAW));
701c2c66affSColin Finck         GL_EXTCALL(glBindBuffer(GL_TEXTURE_BUFFER, 0));
702c2c66affSColin Finck 
7039987f029SAmine Khaldi         gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_buffer);
7049987f029SAmine Khaldi         TRACE("Dummy buffer texture given name %u.\n", textures->tex_buffer);
7059987f029SAmine Khaldi         gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_BUFFER, textures->tex_buffer);
706c2c66affSColin Finck         GL_EXTCALL(glTexBuffer(GL_TEXTURE_BUFFER, GL_RGBA8, buffer));
707c2c66affSColin Finck         GL_EXTCALL(glDeleteBuffers(1, &buffer));
708c2c66affSColin Finck     }
709c2c66affSColin Finck 
7109987f029SAmine Khaldi     if (gl_info->supported[ARB_TEXTURE_MULTISAMPLE])
7119987f029SAmine Khaldi     {
7129987f029SAmine Khaldi         gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_2d_ms);
7139987f029SAmine Khaldi         TRACE("Dummy multisample texture given name %u.\n", textures->tex_2d_ms);
7149987f029SAmine Khaldi         gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, textures->tex_2d_ms);
7159987f029SAmine Khaldi         GL_EXTCALL(glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, 1, GL_RGBA8, 1, 1, GL_TRUE));
7169987f029SAmine Khaldi 
7179987f029SAmine Khaldi         gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_2d_ms_array);
7189987f029SAmine Khaldi         TRACE("Dummy multisample array texture given name %u.\n", textures->tex_2d_ms_array);
7199987f029SAmine Khaldi         gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D_MULTISAMPLE_ARRAY, textures->tex_2d_ms_array);
7209987f029SAmine Khaldi         GL_EXTCALL(glTexImage3DMultisample(GL_TEXTURE_2D_MULTISAMPLE_ARRAY, 1, GL_RGBA8, 1, 1, 1, GL_TRUE));
7219987f029SAmine Khaldi 
7229987f029SAmine Khaldi         if (gl_info->supported[ARB_CLEAR_TEXTURE])
7239987f029SAmine Khaldi         {
7249987f029SAmine Khaldi             GL_EXTCALL(glClearTexImage(textures->tex_2d_ms, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color));
7259987f029SAmine Khaldi             GL_EXTCALL(glClearTexImage(textures->tex_2d_ms_array, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color));
7269987f029SAmine Khaldi         }
7279987f029SAmine Khaldi         else
7289987f029SAmine Khaldi         {
7299987f029SAmine Khaldi             WARN("ARB_clear_texture is currently required to clear dummy multisample textures.\n");
7309987f029SAmine Khaldi         }
7319987f029SAmine Khaldi     }
7329987f029SAmine Khaldi 
7339987f029SAmine Khaldi     checkGLcall("create dummy textures");
7349987f029SAmine Khaldi 
735c2c66affSColin Finck     context_bind_dummy_textures(device, context);
736c2c66affSColin Finck }
737c2c66affSColin Finck 
738c2c66affSColin Finck /* Context activation is done by the caller. */
destroy_dummy_textures(struct wined3d_device * device,struct wined3d_context * context)739c2c66affSColin Finck static void destroy_dummy_textures(struct wined3d_device *device, struct wined3d_context *context)
740c2c66affSColin Finck {
7419987f029SAmine Khaldi     struct wined3d_dummy_textures *dummy_textures = &device->dummy_textures;
742c2c66affSColin Finck     const struct wined3d_gl_info *gl_info = context->gl_info;
743c2c66affSColin Finck 
7449987f029SAmine Khaldi     if (gl_info->supported[ARB_TEXTURE_MULTISAMPLE])
7459987f029SAmine Khaldi     {
7469987f029SAmine Khaldi         gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_2d_ms);
7479987f029SAmine Khaldi         gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_2d_ms_array);
7489987f029SAmine Khaldi     }
7499987f029SAmine Khaldi 
750c2c66affSColin Finck     if (gl_info->supported[ARB_TEXTURE_BUFFER_OBJECT])
7519987f029SAmine Khaldi         gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_buffer);
752c2c66affSColin Finck 
753c2c66affSColin Finck     if (gl_info->supported[EXT_TEXTURE_ARRAY])
7549987f029SAmine Khaldi     {
7557af3969eSAmine Khaldi         gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_1d_array);
756*81cffd76SJoachim Henze         gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_2d_array);
7579987f029SAmine Khaldi     }
758c2c66affSColin Finck 
759c2c66affSColin Finck     if (gl_info->supported[ARB_TEXTURE_CUBE_MAP_ARRAY])
7609987f029SAmine Khaldi         gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_cube_array);
761c2c66affSColin Finck 
762c2c66affSColin Finck     if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
7639987f029SAmine Khaldi         gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_cube);
764c2c66affSColin Finck 
765c2c66affSColin Finck     if (gl_info->supported[EXT_TEXTURE3D])
7669987f029SAmine Khaldi         gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_3d);
767c2c66affSColin Finck 
768c2c66affSColin Finck     if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
7699987f029SAmine Khaldi         gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_rect);
770c2c66affSColin Finck 
7719987f029SAmine Khaldi     gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_2d);
7729987f029SAmine Khaldi     gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_1d);
773c2c66affSColin Finck 
7749987f029SAmine Khaldi     checkGLcall("delete dummy textures");
775c2c66affSColin Finck 
7769987f029SAmine Khaldi     memset(dummy_textures, 0, sizeof(*dummy_textures));
777c2c66affSColin Finck }
778c2c66affSColin Finck 
779c2c66affSColin Finck /* Context activation is done by the caller. */
create_default_samplers(struct wined3d_device * device,struct wined3d_context * context)780c2c66affSColin Finck static void create_default_samplers(struct wined3d_device *device, struct wined3d_context *context)
781c2c66affSColin Finck {
782c2c66affSColin Finck     struct wined3d_sampler_desc desc;
783c2c66affSColin Finck     HRESULT hr;
784c2c66affSColin Finck 
785c2c66affSColin Finck     desc.address_u = WINED3D_TADDRESS_WRAP;
786c2c66affSColin Finck     desc.address_v = WINED3D_TADDRESS_WRAP;
787c2c66affSColin Finck     desc.address_w = WINED3D_TADDRESS_WRAP;
788c2c66affSColin Finck     memset(desc.border_color, 0, sizeof(desc.border_color));
789c2c66affSColin Finck     desc.mag_filter = WINED3D_TEXF_POINT;
790c2c66affSColin Finck     desc.min_filter = WINED3D_TEXF_POINT;
791c2c66affSColin Finck     desc.mip_filter = WINED3D_TEXF_NONE;
792c2c66affSColin Finck     desc.lod_bias = 0.0f;
793c2c66affSColin Finck     desc.min_lod = -1000.0f;
794c2c66affSColin Finck     desc.max_lod =  1000.0f;
795c2c66affSColin Finck     desc.mip_base_level = 0;
796c2c66affSColin Finck     desc.max_anisotropy = 1;
797c2c66affSColin Finck     desc.compare = FALSE;
798c2c66affSColin Finck     desc.comparison_func = WINED3D_CMP_NEVER;
799c2c66affSColin Finck     desc.srgb_decode = TRUE;
800c2c66affSColin Finck 
801c2c66affSColin Finck     /* In SM4+ shaders there is a separation between resources and samplers. Some shader
802c2c66affSColin Finck      * instructions allow access to resources without using samplers.
803c2c66affSColin Finck      * In GLSL, resources are always accessed through sampler or image variables. The default
804c2c66affSColin Finck      * sampler object is used to emulate the direct resource access when there is no sampler state
805c2c66affSColin Finck      * to use.
806c2c66affSColin Finck      */
807c2c66affSColin Finck     if (FAILED(hr = wined3d_sampler_create(device, &desc, NULL, &wined3d_null_parent_ops, &device->default_sampler)))
808c2c66affSColin Finck     {
809c2c66affSColin Finck         ERR("Failed to create default sampler, hr %#x.\n", hr);
810c2c66affSColin Finck         device->default_sampler = NULL;
811c2c66affSColin Finck     }
812c2c66affSColin Finck 
813c2c66affSColin Finck     /* In D3D10+, a NULL sampler maps to the default sampler state. */
814c2c66affSColin Finck     desc.address_u = WINED3D_TADDRESS_CLAMP;
815c2c66affSColin Finck     desc.address_v = WINED3D_TADDRESS_CLAMP;
816c2c66affSColin Finck     desc.address_w = WINED3D_TADDRESS_CLAMP;
817c2c66affSColin Finck     desc.mag_filter = WINED3D_TEXF_LINEAR;
818c2c66affSColin Finck     desc.min_filter = WINED3D_TEXF_LINEAR;
819c2c66affSColin Finck     desc.mip_filter = WINED3D_TEXF_LINEAR;
820c2c66affSColin Finck     if (FAILED(hr = wined3d_sampler_create(device, &desc, NULL, &wined3d_null_parent_ops, &device->null_sampler)))
821c2c66affSColin Finck     {
822c2c66affSColin Finck         ERR("Failed to create null sampler, hr %#x.\n", hr);
823c2c66affSColin Finck         device->null_sampler = NULL;
824c2c66affSColin Finck     }
825c2c66affSColin Finck }
826c2c66affSColin Finck 
827c2c66affSColin Finck /* Context activation is done by the caller. */
destroy_default_samplers(struct wined3d_device * device,struct wined3d_context * context)828c2c66affSColin Finck static void destroy_default_samplers(struct wined3d_device *device, struct wined3d_context *context)
829c2c66affSColin Finck {
830c2c66affSColin Finck     wined3d_sampler_decref(device->default_sampler);
831c2c66affSColin Finck     device->default_sampler = NULL;
832c2c66affSColin Finck     wined3d_sampler_decref(device->null_sampler);
833c2c66affSColin Finck     device->null_sampler = NULL;
834c2c66affSColin Finck }
835c2c66affSColin Finck 
fullscreen_style(LONG style)836c2c66affSColin Finck static LONG fullscreen_style(LONG style)
837c2c66affSColin Finck {
838c2c66affSColin Finck     /* Make sure the window is managed, otherwise we won't get keyboard input. */
839c2c66affSColin Finck     style |= WS_POPUP | WS_SYSMENU;
840c2c66affSColin Finck     style &= ~(WS_CAPTION | WS_THICKFRAME);
841c2c66affSColin Finck 
842c2c66affSColin Finck     return style;
843c2c66affSColin Finck }
844c2c66affSColin Finck 
fullscreen_exstyle(LONG exstyle)845c2c66affSColin Finck static LONG fullscreen_exstyle(LONG exstyle)
846c2c66affSColin Finck {
847c2c66affSColin Finck     /* Filter out window decorations. */
848c2c66affSColin Finck     exstyle &= ~(WS_EX_WINDOWEDGE | WS_EX_CLIENTEDGE);
849c2c66affSColin Finck 
850c2c66affSColin Finck     return exstyle;
851c2c66affSColin Finck }
852c2c66affSColin Finck 
wined3d_device_setup_fullscreen_window(struct wined3d_device * device,HWND window,UINT w,UINT h)853c2c66affSColin Finck void CDECL wined3d_device_setup_fullscreen_window(struct wined3d_device *device, HWND window, UINT w, UINT h)
854c2c66affSColin Finck {
855c2c66affSColin Finck     BOOL filter_messages;
856c2c66affSColin Finck     LONG style, exstyle;
857c2c66affSColin Finck 
858c2c66affSColin Finck     TRACE("Setting up window %p for fullscreen mode.\n", window);
859c2c66affSColin Finck 
860c2c66affSColin Finck     if (device->style || device->exStyle)
861c2c66affSColin Finck     {
862c2c66affSColin Finck         ERR("Changing the window style for window %p, but another style (%08x, %08x) is already stored.\n",
863c2c66affSColin Finck                 window, device->style, device->exStyle);
864c2c66affSColin Finck     }
865c2c66affSColin Finck 
866c2c66affSColin Finck     device->style = GetWindowLongW(window, GWL_STYLE);
867c2c66affSColin Finck     device->exStyle = GetWindowLongW(window, GWL_EXSTYLE);
868c2c66affSColin Finck 
869c2c66affSColin Finck     style = fullscreen_style(device->style);
870c2c66affSColin Finck     exstyle = fullscreen_exstyle(device->exStyle);
871c2c66affSColin Finck 
872c2c66affSColin Finck     TRACE("Old style was %08x, %08x, setting to %08x, %08x.\n",
873c2c66affSColin Finck             device->style, device->exStyle, style, exstyle);
874c2c66affSColin Finck 
875c2c66affSColin Finck     filter_messages = device->filter_messages;
876c2c66affSColin Finck     device->filter_messages = TRUE;
877c2c66affSColin Finck 
878c2c66affSColin Finck     SetWindowLongW(window, GWL_STYLE, style);
879c2c66affSColin Finck     SetWindowLongW(window, GWL_EXSTYLE, exstyle);
880c2c66affSColin Finck     SetWindowPos(window, HWND_TOPMOST, 0, 0, w, h, SWP_FRAMECHANGED | SWP_SHOWWINDOW | SWP_NOACTIVATE);
881c2c66affSColin Finck 
882c2c66affSColin Finck     device->filter_messages = filter_messages;
883c2c66affSColin Finck }
884c2c66affSColin Finck 
wined3d_device_restore_fullscreen_window(struct wined3d_device * device,HWND window,const RECT * window_rect)885c2c66affSColin Finck void CDECL wined3d_device_restore_fullscreen_window(struct wined3d_device *device, HWND window,
886c2c66affSColin Finck         const RECT *window_rect)
887c2c66affSColin Finck {
888c2c66affSColin Finck     unsigned int window_pos_flags = SWP_FRAMECHANGED | SWP_NOZORDER | SWP_NOACTIVATE;
889c2c66affSColin Finck     BOOL filter_messages;
890c2c66affSColin Finck     LONG style, exstyle;
891c2c66affSColin Finck     RECT rect = {0};
892c2c66affSColin Finck 
893c2c66affSColin Finck     if (!device->style && !device->exStyle)
894c2c66affSColin Finck         return;
895c2c66affSColin Finck 
896c2c66affSColin Finck     style = GetWindowLongW(window, GWL_STYLE);
897c2c66affSColin Finck     exstyle = GetWindowLongW(window, GWL_EXSTYLE);
898c2c66affSColin Finck 
899c2c66affSColin Finck     /* These flags are set by wined3d_device_setup_fullscreen_window, not the
900c2c66affSColin Finck      * application, and we want to ignore them in the test below, since it's
901c2c66affSColin Finck      * not the application's fault that they changed. Additionally, we want to
902c2c66affSColin Finck      * preserve the current status of these flags (i.e. don't restore them) to
903c2c66affSColin Finck      * more closely emulate the behavior of Direct3D, which leaves these flags
904c2c66affSColin Finck      * alone when returning to windowed mode. */
905c2c66affSColin Finck     device->style ^= (device->style ^ style) & WS_VISIBLE;
906c2c66affSColin Finck     device->exStyle ^= (device->exStyle ^ exstyle) & WS_EX_TOPMOST;
907c2c66affSColin Finck 
908c2c66affSColin Finck     TRACE("Restoring window style of window %p to %08x, %08x.\n",
909c2c66affSColin Finck             window, device->style, device->exStyle);
910c2c66affSColin Finck 
911c2c66affSColin Finck     filter_messages = device->filter_messages;
912c2c66affSColin Finck     device->filter_messages = TRUE;
913c2c66affSColin Finck 
914c2c66affSColin Finck     /* Only restore the style if the application didn't modify it during the
915c2c66affSColin Finck      * fullscreen phase. Some applications change it before calling Reset()
916c2c66affSColin Finck      * when switching between windowed and fullscreen modes (HL2), some
917c2c66affSColin Finck      * depend on the original style (Eve Online). */
918c2c66affSColin Finck     if (style == fullscreen_style(device->style) && exstyle == fullscreen_exstyle(device->exStyle))
919c2c66affSColin Finck     {
920c2c66affSColin Finck         SetWindowLongW(window, GWL_STYLE, device->style);
921c2c66affSColin Finck         SetWindowLongW(window, GWL_EXSTYLE, device->exStyle);
922c2c66affSColin Finck     }
923c2c66affSColin Finck 
924c2c66affSColin Finck     if (window_rect)
925c2c66affSColin Finck         rect = *window_rect;
926c2c66affSColin Finck     else
927c2c66affSColin Finck         window_pos_flags |= (SWP_NOMOVE | SWP_NOSIZE);
928c2c66affSColin Finck     SetWindowPos(window, 0, rect.left, rect.top,
929c2c66affSColin Finck             rect.right - rect.left, rect.bottom - rect.top, window_pos_flags);
930c2c66affSColin Finck 
931c2c66affSColin Finck     device->filter_messages = filter_messages;
932c2c66affSColin Finck 
933c2c66affSColin Finck     /* Delete the old values. */
934c2c66affSColin Finck     device->style = 0;
935c2c66affSColin Finck     device->exStyle = 0;
936c2c66affSColin Finck }
937c2c66affSColin Finck 
wined3d_device_acquire_focus_window(struct wined3d_device * device,HWND window)938c2c66affSColin Finck HRESULT CDECL wined3d_device_acquire_focus_window(struct wined3d_device *device, HWND window)
939c2c66affSColin Finck {
940c2c66affSColin Finck     TRACE("device %p, window %p.\n", device, window);
941c2c66affSColin Finck 
942c2c66affSColin Finck     if (!wined3d_register_window(window, device))
943c2c66affSColin Finck     {
944c2c66affSColin Finck         ERR("Failed to register window %p.\n", window);
945c2c66affSColin Finck         return E_FAIL;
946c2c66affSColin Finck     }
947c2c66affSColin Finck 
948c2c66affSColin Finck     InterlockedExchangePointer((void **)&device->focus_window, window);
949c2c66affSColin Finck     SetWindowPos(window, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
950c2c66affSColin Finck 
951c2c66affSColin Finck     return WINED3D_OK;
952c2c66affSColin Finck }
953c2c66affSColin Finck 
wined3d_device_release_focus_window(struct wined3d_device * device)954c2c66affSColin Finck void CDECL wined3d_device_release_focus_window(struct wined3d_device *device)
955c2c66affSColin Finck {
956c2c66affSColin Finck     TRACE("device %p.\n", device);
957c2c66affSColin Finck 
958c2c66affSColin Finck     if (device->focus_window) wined3d_unregister_window(device->focus_window);
959c2c66affSColin Finck     InterlockedExchangePointer((void **)&device->focus_window, NULL);
960c2c66affSColin Finck }
961c2c66affSColin Finck 
device_init_swapchain_state(struct wined3d_device * device,struct wined3d_swapchain * swapchain)962c2c66affSColin Finck static void device_init_swapchain_state(struct wined3d_device *device, struct wined3d_swapchain *swapchain)
963c2c66affSColin Finck {
964d6ac0a71SAmine Khaldi     BOOL ds_enable = swapchain->desc.enable_auto_depth_stencil;
965c2c66affSColin Finck     unsigned int i;
966c2c66affSColin Finck 
967c2c66affSColin Finck     for (i = 0; i < device->adapter->gl_info.limits.buffers; ++i)
968c2c66affSColin Finck     {
969c2c66affSColin Finck         wined3d_device_set_rendertarget_view(device, i, NULL, FALSE);
970c2c66affSColin Finck     }
971c2c66affSColin Finck     if (device->back_buffer_view)
972c2c66affSColin Finck         wined3d_device_set_rendertarget_view(device, 0, device->back_buffer_view, TRUE);
973c2c66affSColin Finck 
974c2c66affSColin Finck     wined3d_device_set_depth_stencil_view(device, ds_enable ? device->auto_depth_stencil_view : NULL);
975c2c66affSColin Finck }
976c2c66affSColin Finck 
wined3d_device_delete_opengl_contexts_cs(void * object)977c2c66affSColin Finck static void wined3d_device_delete_opengl_contexts_cs(void *object)
978c2c66affSColin Finck {
979c2c66affSColin Finck     struct wined3d_resource *resource, *cursor;
980c2c66affSColin Finck     struct wined3d_device *device = object;
981c2c66affSColin Finck     struct wined3d_context *context;
982c2c66affSColin Finck     struct wined3d_shader *shader;
983c2c66affSColin Finck 
984c2c66affSColin Finck     LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
985c2c66affSColin Finck     {
986c2c66affSColin Finck         TRACE("Unloading resource %p.\n", resource);
987c2c66affSColin Finck         wined3d_cs_emit_unload_resource(device->cs, resource);
988c2c66affSColin Finck     }
989c2c66affSColin Finck 
990c2c66affSColin Finck     LIST_FOR_EACH_ENTRY(shader, &device->shaders, struct wined3d_shader, shader_list_entry)
991c2c66affSColin Finck     {
992c2c66affSColin Finck         device->shader_backend->shader_destroy(shader);
993c2c66affSColin Finck     }
994c2c66affSColin Finck 
995c2c66affSColin Finck     context = context_acquire(device, NULL, 0);
996c2c66affSColin Finck     device->blitter->ops->blitter_destroy(device->blitter, context);
997c2c66affSColin Finck     device->shader_backend->shader_free_private(device);
998c2c66affSColin Finck     destroy_dummy_textures(device, context);
999c2c66affSColin Finck     destroy_default_samplers(device, context);
1000c2c66affSColin Finck     context_release(context);
1001c2c66affSColin Finck 
1002c2c66affSColin Finck     while (device->context_count)
1003c2c66affSColin Finck     {
1004c2c66affSColin Finck         if (device->contexts[0]->swapchain)
1005c2c66affSColin Finck             swapchain_destroy_contexts(device->contexts[0]->swapchain);
1006c2c66affSColin Finck         else
1007c2c66affSColin Finck             context_destroy(device, device->contexts[0]);
1008c2c66affSColin Finck     }
1009c2c66affSColin Finck }
1010c2c66affSColin Finck 
wined3d_device_delete_opengl_contexts(struct wined3d_device * device)1011c2c66affSColin Finck static void wined3d_device_delete_opengl_contexts(struct wined3d_device *device)
1012c2c66affSColin Finck {
1013c2c66affSColin Finck     wined3d_cs_destroy_object(device->cs, wined3d_device_delete_opengl_contexts_cs, device);
1014c2c66affSColin Finck     device->cs->ops->finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
1015c2c66affSColin Finck }
1016c2c66affSColin Finck 
wined3d_device_create_primary_opengl_context_cs(void * object)1017c2c66affSColin Finck static void wined3d_device_create_primary_opengl_context_cs(void *object)
1018c2c66affSColin Finck {
1019c2c66affSColin Finck     struct wined3d_device *device = object;
1020c2c66affSColin Finck     struct wined3d_swapchain *swapchain;
1021c2c66affSColin Finck     struct wined3d_context *context;
1022c2c66affSColin Finck     struct wined3d_texture *target;
1023c2c66affSColin Finck     HRESULT hr;
1024c2c66affSColin Finck 
1025c2c66affSColin Finck     if (FAILED(hr = device->shader_backend->shader_alloc_private(device,
1026c2c66affSColin Finck             device->adapter->vertex_pipe, device->adapter->fragment_pipe)))
1027c2c66affSColin Finck     {
1028c2c66affSColin Finck         ERR("Failed to allocate shader private data, hr %#x.\n", hr);
1029c2c66affSColin Finck         return;
1030c2c66affSColin Finck     }
1031c2c66affSColin Finck 
1032c2c66affSColin Finck     if (!(device->blitter = wined3d_cpu_blitter_create()))
1033c2c66affSColin Finck     {
1034c2c66affSColin Finck         ERR("Failed to create CPU blitter.\n");
1035c2c66affSColin Finck         device->shader_backend->shader_free_private(device);
1036c2c66affSColin Finck         return;
1037c2c66affSColin Finck     }
1038c2c66affSColin Finck     wined3d_ffp_blitter_create(&device->blitter, &device->adapter->gl_info);
1039c2c66affSColin Finck     wined3d_arbfp_blitter_create(&device->blitter, device);
1040c2c66affSColin Finck     wined3d_fbo_blitter_create(&device->blitter, &device->adapter->gl_info);
1041d6ac0a71SAmine Khaldi     wined3d_raw_blitter_create(&device->blitter, &device->adapter->gl_info);
1042c2c66affSColin Finck 
1043c2c66affSColin Finck     swapchain = device->swapchains[0];
1044c2c66affSColin Finck     target = swapchain->back_buffers ? swapchain->back_buffers[0] : swapchain->front_buffer;
1045c2c66affSColin Finck     context = context_acquire(device, target, 0);
1046c2c66affSColin Finck     create_dummy_textures(device, context);
1047c2c66affSColin Finck     create_default_samplers(device, context);
1048c2c66affSColin Finck     context_release(context);
1049c2c66affSColin Finck }
1050c2c66affSColin Finck 
wined3d_device_create_primary_opengl_context(struct wined3d_device * device)1051c2c66affSColin Finck static HRESULT wined3d_device_create_primary_opengl_context(struct wined3d_device *device)
1052c2c66affSColin Finck {
1053c2c66affSColin Finck     wined3d_cs_init_object(device->cs, wined3d_device_create_primary_opengl_context_cs, device);
1054c2c66affSColin Finck     device->cs->ops->finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
1055c2c66affSColin Finck     if (!device->swapchains[0]->num_contexts)
1056c2c66affSColin Finck         return E_FAIL;
1057c2c66affSColin Finck 
1058c2c66affSColin Finck     return WINED3D_OK;
1059c2c66affSColin Finck }
1060c2c66affSColin Finck 
wined3d_device_init_3d(struct wined3d_device * device,struct wined3d_swapchain_desc * swapchain_desc)1061c2c66affSColin Finck HRESULT CDECL wined3d_device_init_3d(struct wined3d_device *device,
1062c2c66affSColin Finck         struct wined3d_swapchain_desc *swapchain_desc)
1063c2c66affSColin Finck {
1064c2c66affSColin Finck     static const struct wined3d_color black = {0.0f, 0.0f, 0.0f, 0.0f};
1065c2c66affSColin Finck     struct wined3d_swapchain *swapchain = NULL;
1066c2c66affSColin Finck     DWORD clear_flags = 0;
1067c2c66affSColin Finck     HRESULT hr;
1068c2c66affSColin Finck 
1069c2c66affSColin Finck     TRACE("device %p, swapchain_desc %p.\n", device, swapchain_desc);
1070c2c66affSColin Finck 
1071c2c66affSColin Finck     if (device->d3d_initialized)
1072c2c66affSColin Finck         return WINED3DERR_INVALIDCALL;
1073c2c66affSColin Finck     if (device->wined3d->flags & WINED3D_NO3D)
1074c2c66affSColin Finck         return WINED3DERR_INVALIDCALL;
1075c2c66affSColin Finck 
10769987f029SAmine Khaldi     memset(device->fb.render_targets, 0, sizeof(device->fb.render_targets));
1077c2c66affSColin Finck 
1078c2c66affSColin Finck     /* Setup the implicit swapchain. This also initializes a context. */
10799987f029SAmine Khaldi     TRACE("Creating implicit swapchain.\n");
10809987f029SAmine Khaldi     if (FAILED(hr = device->device_parent->ops->create_swapchain(device->device_parent,
10819987f029SAmine Khaldi             swapchain_desc, &swapchain)))
1082c2c66affSColin Finck     {
10839987f029SAmine Khaldi         WARN("Failed to create implicit swapchain.\n");
1084c2c66affSColin Finck         goto err_out;
1085c2c66affSColin Finck     }
1086c2c66affSColin Finck 
1087*81cffd76SJoachim Henze     if (swapchain_desc->backbuffer_count)
1088c2c66affSColin Finck     {
1089c2c66affSColin Finck         struct wined3d_resource *back_buffer = &swapchain->back_buffers[0]->resource;
1090c2c66affSColin Finck         struct wined3d_view_desc view_desc;
1091c2c66affSColin Finck 
1092c2c66affSColin Finck         view_desc.format_id = back_buffer->format->id;
1093c2c66affSColin Finck         view_desc.flags = 0;
1094c2c66affSColin Finck         view_desc.u.texture.level_idx = 0;
1095c2c66affSColin Finck         view_desc.u.texture.level_count = 1;
1096c2c66affSColin Finck         view_desc.u.texture.layer_idx = 0;
1097c2c66affSColin Finck         view_desc.u.texture.layer_count = 1;
1098c2c66affSColin Finck         if (FAILED(hr = wined3d_rendertarget_view_create(&view_desc, back_buffer,
1099c2c66affSColin Finck                 NULL, &wined3d_null_parent_ops, &device->back_buffer_view)))
1100c2c66affSColin Finck         {
1101c2c66affSColin Finck             ERR("Failed to create rendertarget view, hr %#x.\n", hr);
1102c2c66affSColin Finck             goto err_out;
1103c2c66affSColin Finck         }
1104c2c66affSColin Finck     }
1105c2c66affSColin Finck 
1106c2c66affSColin Finck     device->swapchain_count = 1;
11079987f029SAmine Khaldi     if (!(device->swapchains = heap_calloc(device->swapchain_count, sizeof(*device->swapchains))))
1108c2c66affSColin Finck     {
1109c2c66affSColin Finck         ERR("Out of memory!\n");
1110c2c66affSColin Finck         goto err_out;
1111c2c66affSColin Finck     }
1112c2c66affSColin Finck     device->swapchains[0] = swapchain;
1113c2c66affSColin Finck 
1114c2c66affSColin Finck     if (FAILED(hr = wined3d_device_create_primary_opengl_context(device)))
1115c2c66affSColin Finck         goto err_out;
1116c2c66affSColin Finck     device_init_swapchain_state(device, swapchain);
1117c2c66affSColin Finck 
1118c2c66affSColin Finck     device->contexts[0]->last_was_rhw = 0;
1119c2c66affSColin Finck 
11209987f029SAmine Khaldi     TRACE("All defaults now set up.\n");
1121c2c66affSColin Finck 
1122c2c66affSColin Finck     /* Clear the screen */
1123*81cffd76SJoachim Henze     if (swapchain->back_buffers && swapchain->back_buffers[0])
1124c2c66affSColin Finck         clear_flags |= WINED3DCLEAR_TARGET;
1125c2c66affSColin Finck     if (swapchain_desc->enable_auto_depth_stencil)
1126c2c66affSColin Finck         clear_flags |= WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL;
1127c2c66affSColin Finck     if (clear_flags)
1128c2c66affSColin Finck         wined3d_device_clear(device, 0, NULL, clear_flags, &black, 1.0f, 0);
1129c2c66affSColin Finck 
1130c2c66affSColin Finck     device->d3d_initialized = TRUE;
1131c2c66affSColin Finck 
1132c2c66affSColin Finck     if (wined3d_settings.logo)
1133c2c66affSColin Finck         device_load_logo(device, wined3d_settings.logo);
1134c2c66affSColin Finck     return WINED3D_OK;
1135c2c66affSColin Finck 
1136c2c66affSColin Finck err_out:
11379987f029SAmine Khaldi     heap_free(device->swapchains);
1138c2c66affSColin Finck     device->swapchain_count = 0;
1139c2c66affSColin Finck     if (device->back_buffer_view)
1140c2c66affSColin Finck         wined3d_rendertarget_view_decref(device->back_buffer_view);
1141c2c66affSColin Finck     if (swapchain)
1142c2c66affSColin Finck         wined3d_swapchain_decref(swapchain);
1143c2c66affSColin Finck 
1144c2c66affSColin Finck     return hr;
1145c2c66affSColin Finck }
1146c2c66affSColin Finck 
wined3d_device_init_gdi(struct wined3d_device * device,struct wined3d_swapchain_desc * swapchain_desc)1147c2c66affSColin Finck HRESULT CDECL wined3d_device_init_gdi(struct wined3d_device *device,
1148c2c66affSColin Finck         struct wined3d_swapchain_desc *swapchain_desc)
1149c2c66affSColin Finck {
1150c2c66affSColin Finck     struct wined3d_swapchain *swapchain = NULL;
1151c2c66affSColin Finck     HRESULT hr;
1152c2c66affSColin Finck 
1153c2c66affSColin Finck     TRACE("device %p, swapchain_desc %p.\n", device, swapchain_desc);
1154c2c66affSColin Finck 
1155c2c66affSColin Finck     /* Setup the implicit swapchain */
1156c2c66affSColin Finck     TRACE("Creating implicit swapchain\n");
1157c2c66affSColin Finck     hr = device->device_parent->ops->create_swapchain(device->device_parent,
1158c2c66affSColin Finck             swapchain_desc, &swapchain);
1159c2c66affSColin Finck     if (FAILED(hr))
1160c2c66affSColin Finck     {
1161c2c66affSColin Finck         WARN("Failed to create implicit swapchain\n");
1162c2c66affSColin Finck         goto err_out;
1163c2c66affSColin Finck     }
1164c2c66affSColin Finck 
1165c2c66affSColin Finck     device->swapchain_count = 1;
11669987f029SAmine Khaldi     if (!(device->swapchains = heap_calloc(device->swapchain_count, sizeof(*device->swapchains))))
1167c2c66affSColin Finck     {
1168c2c66affSColin Finck         ERR("Out of memory!\n");
1169c2c66affSColin Finck         goto err_out;
1170c2c66affSColin Finck     }
1171c2c66affSColin Finck     device->swapchains[0] = swapchain;
1172c2c66affSColin Finck 
1173c2c66affSColin Finck     if (!(device->blitter = wined3d_cpu_blitter_create()))
1174c2c66affSColin Finck     {
1175c2c66affSColin Finck         ERR("Failed to create CPU blitter.\n");
11769987f029SAmine Khaldi         heap_free(device->swapchains);
1177c2c66affSColin Finck         device->swapchain_count = 0;
1178c2c66affSColin Finck         goto err_out;
1179c2c66affSColin Finck     }
1180c2c66affSColin Finck 
1181c2c66affSColin Finck     return WINED3D_OK;
1182c2c66affSColin Finck 
1183c2c66affSColin Finck err_out:
1184c2c66affSColin Finck     wined3d_swapchain_decref(swapchain);
1185c2c66affSColin Finck     return hr;
1186c2c66affSColin Finck }
1187c2c66affSColin Finck 
device_free_sampler(struct wine_rb_entry * entry,void * context)1188c2c66affSColin Finck static void device_free_sampler(struct wine_rb_entry *entry, void *context)
1189c2c66affSColin Finck {
1190c2c66affSColin Finck     struct wined3d_sampler *sampler = WINE_RB_ENTRY_VALUE(entry, struct wined3d_sampler, entry);
1191c2c66affSColin Finck 
1192c2c66affSColin Finck     wined3d_sampler_decref(sampler);
1193c2c66affSColin Finck }
1194c2c66affSColin Finck 
wined3d_device_uninit_3d(struct wined3d_device * device)1195c2c66affSColin Finck HRESULT CDECL wined3d_device_uninit_3d(struct wined3d_device *device)
1196c2c66affSColin Finck {
11979987f029SAmine Khaldi     unsigned int i;
1198c2c66affSColin Finck 
1199c2c66affSColin Finck     TRACE("device %p.\n", device);
1200c2c66affSColin Finck 
1201c2c66affSColin Finck     if (!device->d3d_initialized)
1202c2c66affSColin Finck         return WINED3DERR_INVALIDCALL;
1203c2c66affSColin Finck 
1204c2c66affSColin Finck     device->cs->ops->finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
1205c2c66affSColin Finck 
1206c2c66affSColin Finck     if (device->logo_texture)
1207c2c66affSColin Finck         wined3d_texture_decref(device->logo_texture);
1208c2c66affSColin Finck     if (device->cursor_texture)
1209c2c66affSColin Finck         wined3d_texture_decref(device->cursor_texture);
1210c2c66affSColin Finck 
1211c2c66affSColin Finck     state_unbind_resources(&device->state);
1212c2c66affSColin Finck 
1213c2c66affSColin Finck     wine_rb_clear(&device->samplers, device_free_sampler, NULL);
1214c2c66affSColin Finck 
1215*81cffd76SJoachim Henze #if defined(STAGING_CSMT)
12169987f029SAmine Khaldi     context_set_current(NULL);
1217*81cffd76SJoachim Henze #endif /* STAGING_CSMT */
1218c2c66affSColin Finck     wined3d_device_delete_opengl_contexts(device);
1219c2c66affSColin Finck 
1220c2c66affSColin Finck     if (device->fb.depth_stencil)
1221c2c66affSColin Finck     {
1222c2c66affSColin Finck         struct wined3d_rendertarget_view *view = device->fb.depth_stencil;
1223c2c66affSColin Finck 
1224c2c66affSColin Finck         TRACE("Releasing depth/stencil view %p.\n", view);
1225c2c66affSColin Finck 
1226c2c66affSColin Finck         device->fb.depth_stencil = NULL;
1227c2c66affSColin Finck         wined3d_rendertarget_view_decref(view);
1228c2c66affSColin Finck     }
1229c2c66affSColin Finck 
1230c2c66affSColin Finck     if (device->auto_depth_stencil_view)
1231c2c66affSColin Finck     {
1232c2c66affSColin Finck         struct wined3d_rendertarget_view *view = device->auto_depth_stencil_view;
1233c2c66affSColin Finck 
1234c2c66affSColin Finck         device->auto_depth_stencil_view = NULL;
1235c2c66affSColin Finck         if (wined3d_rendertarget_view_decref(view))
1236c2c66affSColin Finck             ERR("Something's still holding the auto depth/stencil view (%p).\n", view);
1237c2c66affSColin Finck     }
1238c2c66affSColin Finck 
1239c2c66affSColin Finck     for (i = 0; i < device->adapter->gl_info.limits.buffers; ++i)
1240c2c66affSColin Finck     {
1241c2c66affSColin Finck         wined3d_device_set_rendertarget_view(device, i, NULL, FALSE);
1242c2c66affSColin Finck     }
1243c2c66affSColin Finck     if (device->back_buffer_view)
1244c2c66affSColin Finck     {
1245c2c66affSColin Finck         wined3d_rendertarget_view_decref(device->back_buffer_view);
1246c2c66affSColin Finck         device->back_buffer_view = NULL;
1247c2c66affSColin Finck     }
1248c2c66affSColin Finck 
1249c2c66affSColin Finck     for (i = 0; i < device->swapchain_count; ++i)
1250c2c66affSColin Finck     {
1251c2c66affSColin Finck         TRACE("Releasing the implicit swapchain %u.\n", i);
1252c2c66affSColin Finck         if (wined3d_swapchain_decref(device->swapchains[i]))
1253c2c66affSColin Finck             FIXME("Something's still holding the implicit swapchain.\n");
1254c2c66affSColin Finck     }
1255c2c66affSColin Finck 
12569987f029SAmine Khaldi     heap_free(device->swapchains);
1257c2c66affSColin Finck     device->swapchains = NULL;
1258c2c66affSColin Finck     device->swapchain_count = 0;
1259c2c66affSColin Finck 
1260c2c66affSColin Finck     device->d3d_initialized = FALSE;
1261c2c66affSColin Finck 
1262c2c66affSColin Finck     return WINED3D_OK;
1263c2c66affSColin Finck }
1264c2c66affSColin Finck 
wined3d_device_uninit_gdi(struct wined3d_device * device)1265c2c66affSColin Finck HRESULT CDECL wined3d_device_uninit_gdi(struct wined3d_device *device)
1266c2c66affSColin Finck {
1267c2c66affSColin Finck     unsigned int i;
1268c2c66affSColin Finck 
1269c2c66affSColin Finck     device->blitter->ops->blitter_destroy(device->blitter, NULL);
1270c2c66affSColin Finck 
1271c2c66affSColin Finck     for (i = 0; i < device->swapchain_count; ++i)
1272c2c66affSColin Finck     {
1273c2c66affSColin Finck         TRACE("Releasing the implicit swapchain %u.\n", i);
1274c2c66affSColin Finck         if (wined3d_swapchain_decref(device->swapchains[i]))
1275c2c66affSColin Finck             FIXME("Something's still holding the implicit swapchain.\n");
1276c2c66affSColin Finck     }
1277c2c66affSColin Finck 
12789987f029SAmine Khaldi     heap_free(device->swapchains);
1279c2c66affSColin Finck     device->swapchains = NULL;
1280c2c66affSColin Finck     device->swapchain_count = 0;
1281c2c66affSColin Finck     return WINED3D_OK;
1282c2c66affSColin Finck }
1283c2c66affSColin Finck 
1284c2c66affSColin Finck /* Enables thread safety in the wined3d device and its resources. Called by DirectDraw
1285c2c66affSColin Finck  * from SetCooperativeLevel if DDSCL_MULTITHREADED is specified, and by d3d8/9 from
1286c2c66affSColin Finck  * CreateDevice if D3DCREATE_MULTITHREADED is passed.
1287c2c66affSColin Finck  *
1288c2c66affSColin Finck  * There is no way to deactivate thread safety once it is enabled.
1289c2c66affSColin Finck  */
wined3d_device_set_multithreaded(struct wined3d_device * device)1290c2c66affSColin Finck void CDECL wined3d_device_set_multithreaded(struct wined3d_device *device)
1291c2c66affSColin Finck {
1292c2c66affSColin Finck     TRACE("device %p.\n", device);
1293c2c66affSColin Finck 
1294c2c66affSColin Finck     /* For now just store the flag (needed in case of ddraw). */
1295c2c66affSColin Finck     device->create_parms.flags |= WINED3DCREATE_MULTITHREADED;
1296c2c66affSColin Finck }
1297c2c66affSColin Finck 
wined3d_device_get_available_texture_mem(const struct wined3d_device * device)1298c2c66affSColin Finck UINT CDECL wined3d_device_get_available_texture_mem(const struct wined3d_device *device)
1299c2c66affSColin Finck {
13009987f029SAmine Khaldi     /* const struct wined3d_gl_info *gl_info = &device->adapter->gl_info; */
13019987f029SAmine Khaldi 
1302c2c66affSColin Finck     TRACE("device %p.\n", device);
1303c2c66affSColin Finck 
13049987f029SAmine Khaldi     /* We can not acquire the context unless there is a swapchain. */
13059987f029SAmine Khaldi     /*
13069987f029SAmine Khaldi     if (device->swapchains && gl_info->supported[NVX_GPU_MEMORY_INFO] &&
13079987f029SAmine Khaldi             !wined3d_settings.emulated_textureram)
13089987f029SAmine Khaldi     {
13099987f029SAmine Khaldi         GLint vram_free_kb;
13109987f029SAmine Khaldi         UINT64 vram_free;
13119987f029SAmine Khaldi 
13129987f029SAmine Khaldi         struct wined3d_context *context = context_acquire(device, NULL, 0);
13139987f029SAmine Khaldi         gl_info->gl_ops.gl.p_glGetIntegerv(GL_GPU_MEMORY_INFO_CURRENT_AVAILABLE_VIDMEM_NVX, &vram_free_kb);
13149987f029SAmine Khaldi         vram_free = (UINT64)vram_free_kb * 1024;
13159987f029SAmine Khaldi         context_release(context);
13169987f029SAmine Khaldi 
13179987f029SAmine Khaldi         TRACE("Total 0x%s bytes. emulation 0x%s left, driver 0x%s left.\n",
13189987f029SAmine Khaldi                 wine_dbgstr_longlong(device->adapter->vram_bytes),
13199987f029SAmine Khaldi                 wine_dbgstr_longlong(device->adapter->vram_bytes - device->adapter->vram_bytes_used),
13209987f029SAmine Khaldi                 wine_dbgstr_longlong(vram_free));
13219987f029SAmine Khaldi 
13229987f029SAmine Khaldi         vram_free = min(vram_free, device->adapter->vram_bytes - device->adapter->vram_bytes_used);
13239987f029SAmine Khaldi         return min(UINT_MAX, vram_free);
13249987f029SAmine Khaldi     }
13259987f029SAmine Khaldi     */
13269987f029SAmine Khaldi 
1327c2c66affSColin Finck     TRACE("Emulating 0x%s bytes. 0x%s used, returning 0x%s left.\n",
1328c2c66affSColin Finck             wine_dbgstr_longlong(device->adapter->vram_bytes),
1329c2c66affSColin Finck             wine_dbgstr_longlong(device->adapter->vram_bytes_used),
1330c2c66affSColin Finck             wine_dbgstr_longlong(device->adapter->vram_bytes - device->adapter->vram_bytes_used));
1331c2c66affSColin Finck 
1332c2c66affSColin Finck     return min(UINT_MAX, device->adapter->vram_bytes - device->adapter->vram_bytes_used);
1333c2c66affSColin Finck }
1334c2c66affSColin Finck 
wined3d_device_set_stream_output(struct wined3d_device * device,UINT idx,struct wined3d_buffer * buffer,UINT offset)1335c2c66affSColin Finck void CDECL wined3d_device_set_stream_output(struct wined3d_device *device, UINT idx,
1336c2c66affSColin Finck         struct wined3d_buffer *buffer, UINT offset)
1337c2c66affSColin Finck {
1338c2c66affSColin Finck     struct wined3d_stream_output *stream;
1339c2c66affSColin Finck     struct wined3d_buffer *prev_buffer;
1340c2c66affSColin Finck 
1341c2c66affSColin Finck     TRACE("device %p, idx %u, buffer %p, offset %u.\n", device, idx, buffer, offset);
1342c2c66affSColin Finck 
1343c2c66affSColin Finck     if (idx >= WINED3D_MAX_STREAM_OUTPUT_BUFFERS)
1344c2c66affSColin Finck     {
1345c2c66affSColin Finck         WARN("Invalid stream output %u.\n", idx);
1346c2c66affSColin Finck         return;
1347c2c66affSColin Finck     }
1348c2c66affSColin Finck 
1349c2c66affSColin Finck     stream = &device->update_state->stream_output[idx];
1350c2c66affSColin Finck     prev_buffer = stream->buffer;
1351c2c66affSColin Finck 
1352c2c66affSColin Finck     if (buffer)
1353c2c66affSColin Finck         wined3d_buffer_incref(buffer);
1354c2c66affSColin Finck     stream->buffer = buffer;
1355c2c66affSColin Finck     stream->offset = offset;
1356c2c66affSColin Finck     if (!device->recording)
1357c2c66affSColin Finck         wined3d_cs_emit_set_stream_output(device->cs, idx, buffer, offset);
1358c2c66affSColin Finck     if (prev_buffer)
1359c2c66affSColin Finck         wined3d_buffer_decref(prev_buffer);
1360c2c66affSColin Finck }
1361c2c66affSColin Finck 
wined3d_device_get_stream_output(struct wined3d_device * device,UINT idx,UINT * offset)1362c2c66affSColin Finck struct wined3d_buffer * CDECL wined3d_device_get_stream_output(struct wined3d_device *device,
1363c2c66affSColin Finck         UINT idx, UINT *offset)
1364c2c66affSColin Finck {
1365c2c66affSColin Finck     TRACE("device %p, idx %u, offset %p.\n", device, idx, offset);
1366c2c66affSColin Finck 
1367c2c66affSColin Finck     if (idx >= WINED3D_MAX_STREAM_OUTPUT_BUFFERS)
1368c2c66affSColin Finck     {
1369c2c66affSColin Finck         WARN("Invalid stream output %u.\n", idx);
1370c2c66affSColin Finck         return NULL;
1371c2c66affSColin Finck     }
1372c2c66affSColin Finck 
1373c2c66affSColin Finck     if (offset)
1374c2c66affSColin Finck         *offset = device->state.stream_output[idx].offset;
1375c2c66affSColin Finck     return device->state.stream_output[idx].buffer;
1376c2c66affSColin Finck }
1377c2c66affSColin Finck 
wined3d_device_set_stream_source(struct wined3d_device * device,UINT stream_idx,struct wined3d_buffer * buffer,UINT offset,UINT stride)1378c2c66affSColin Finck HRESULT CDECL wined3d_device_set_stream_source(struct wined3d_device *device, UINT stream_idx,
1379c2c66affSColin Finck         struct wined3d_buffer *buffer, UINT offset, UINT stride)
1380c2c66affSColin Finck {
1381c2c66affSColin Finck     struct wined3d_stream_state *stream;
1382c2c66affSColin Finck     struct wined3d_buffer *prev_buffer;
1383c2c66affSColin Finck 
1384c2c66affSColin Finck     TRACE("device %p, stream_idx %u, buffer %p, offset %u, stride %u.\n",
1385c2c66affSColin Finck             device, stream_idx, buffer, offset, stride);
1386c2c66affSColin Finck 
1387c2c66affSColin Finck     if (stream_idx >= MAX_STREAMS)
1388c2c66affSColin Finck     {
1389c2c66affSColin Finck         WARN("Stream index %u out of range.\n", stream_idx);
1390c2c66affSColin Finck         return WINED3DERR_INVALIDCALL;
1391c2c66affSColin Finck     }
1392c2c66affSColin Finck     else if (offset & 0x3)
1393c2c66affSColin Finck     {
1394c2c66affSColin Finck         WARN("Offset %u is not 4 byte aligned.\n", offset);
1395c2c66affSColin Finck         return WINED3DERR_INVALIDCALL;
1396c2c66affSColin Finck     }
1397c2c66affSColin Finck 
1398c2c66affSColin Finck     stream = &device->update_state->streams[stream_idx];
1399c2c66affSColin Finck     prev_buffer = stream->buffer;
1400c2c66affSColin Finck 
1401c2c66affSColin Finck     if (device->recording)
1402c2c66affSColin Finck         device->recording->changed.streamSource |= 1u << stream_idx;
1403c2c66affSColin Finck 
1404c2c66affSColin Finck     if (prev_buffer == buffer
1405c2c66affSColin Finck             && stream->stride == stride
1406c2c66affSColin Finck             && stream->offset == offset)
1407c2c66affSColin Finck     {
1408c2c66affSColin Finck        TRACE("Application is setting the old values over, nothing to do.\n");
1409c2c66affSColin Finck        return WINED3D_OK;
1410c2c66affSColin Finck     }
1411c2c66affSColin Finck 
1412c2c66affSColin Finck     stream->buffer = buffer;
1413c2c66affSColin Finck     if (buffer)
1414c2c66affSColin Finck     {
1415c2c66affSColin Finck         stream->stride = stride;
1416c2c66affSColin Finck         stream->offset = offset;
1417c2c66affSColin Finck         wined3d_buffer_incref(buffer);
1418c2c66affSColin Finck     }
1419c2c66affSColin Finck 
1420c2c66affSColin Finck     if (!device->recording)
1421c2c66affSColin Finck         wined3d_cs_emit_set_stream_source(device->cs, stream_idx, buffer, offset, stride);
1422c2c66affSColin Finck     if (prev_buffer)
1423c2c66affSColin Finck         wined3d_buffer_decref(prev_buffer);
1424c2c66affSColin Finck 
1425c2c66affSColin Finck     return WINED3D_OK;
1426c2c66affSColin Finck }
1427c2c66affSColin Finck 
wined3d_device_get_stream_source(const struct wined3d_device * device,UINT stream_idx,struct wined3d_buffer ** buffer,UINT * offset,UINT * stride)1428c2c66affSColin Finck HRESULT CDECL wined3d_device_get_stream_source(const struct wined3d_device *device,
1429c2c66affSColin Finck         UINT stream_idx, struct wined3d_buffer **buffer, UINT *offset, UINT *stride)
1430c2c66affSColin Finck {
1431c2c66affSColin Finck     const struct wined3d_stream_state *stream;
1432c2c66affSColin Finck 
1433c2c66affSColin Finck     TRACE("device %p, stream_idx %u, buffer %p, offset %p, stride %p.\n",
1434c2c66affSColin Finck             device, stream_idx, buffer, offset, stride);
1435c2c66affSColin Finck 
1436c2c66affSColin Finck     if (stream_idx >= MAX_STREAMS)
1437c2c66affSColin Finck     {
1438c2c66affSColin Finck         WARN("Stream index %u out of range.\n", stream_idx);
1439c2c66affSColin Finck         return WINED3DERR_INVALIDCALL;
1440c2c66affSColin Finck     }
1441c2c66affSColin Finck 
1442c2c66affSColin Finck     stream = &device->state.streams[stream_idx];
1443c2c66affSColin Finck     *buffer = stream->buffer;
1444c2c66affSColin Finck     if (offset)
1445c2c66affSColin Finck         *offset = stream->offset;
1446c2c66affSColin Finck     *stride = stream->stride;
1447c2c66affSColin Finck 
1448c2c66affSColin Finck     return WINED3D_OK;
1449c2c66affSColin Finck }
1450c2c66affSColin Finck 
wined3d_device_set_stream_source_freq(struct wined3d_device * device,UINT stream_idx,UINT divider)1451c2c66affSColin Finck HRESULT CDECL wined3d_device_set_stream_source_freq(struct wined3d_device *device, UINT stream_idx, UINT divider)
1452c2c66affSColin Finck {
1453c2c66affSColin Finck     struct wined3d_stream_state *stream;
1454c2c66affSColin Finck     UINT old_flags, old_freq;
1455c2c66affSColin Finck 
1456c2c66affSColin Finck     TRACE("device %p, stream_idx %u, divider %#x.\n", device, stream_idx, divider);
1457c2c66affSColin Finck 
1458c2c66affSColin Finck     /* Verify input. At least in d3d9 this is invalid. */
1459c2c66affSColin Finck     if ((divider & WINED3DSTREAMSOURCE_INSTANCEDATA) && (divider & WINED3DSTREAMSOURCE_INDEXEDDATA))
1460c2c66affSColin Finck     {
1461c2c66affSColin Finck         WARN("INSTANCEDATA and INDEXEDDATA were set, returning D3DERR_INVALIDCALL.\n");
1462c2c66affSColin Finck         return WINED3DERR_INVALIDCALL;
1463c2c66affSColin Finck     }
1464c2c66affSColin Finck     if ((divider & WINED3DSTREAMSOURCE_INSTANCEDATA) && !stream_idx)
1465c2c66affSColin Finck     {
1466c2c66affSColin Finck         WARN("INSTANCEDATA used on stream 0, returning D3DERR_INVALIDCALL.\n");
1467c2c66affSColin Finck         return WINED3DERR_INVALIDCALL;
1468c2c66affSColin Finck     }
1469c2c66affSColin Finck     if (!divider)
1470c2c66affSColin Finck     {
1471c2c66affSColin Finck         WARN("Divider is 0, returning D3DERR_INVALIDCALL.\n");
1472c2c66affSColin Finck         return WINED3DERR_INVALIDCALL;
1473c2c66affSColin Finck     }
1474c2c66affSColin Finck 
1475c2c66affSColin Finck     stream = &device->update_state->streams[stream_idx];
1476c2c66affSColin Finck     old_flags = stream->flags;
1477c2c66affSColin Finck     old_freq = stream->frequency;
1478c2c66affSColin Finck 
1479c2c66affSColin Finck     stream->flags = divider & (WINED3DSTREAMSOURCE_INSTANCEDATA | WINED3DSTREAMSOURCE_INDEXEDDATA);
1480c2c66affSColin Finck     stream->frequency = divider & 0x7fffff;
1481c2c66affSColin Finck 
1482c2c66affSColin Finck     if (device->recording)
1483c2c66affSColin Finck         device->recording->changed.streamFreq |= 1u << stream_idx;
1484c2c66affSColin Finck     else if (stream->frequency != old_freq || stream->flags != old_flags)
1485c2c66affSColin Finck         wined3d_cs_emit_set_stream_source_freq(device->cs, stream_idx, stream->frequency, stream->flags);
1486c2c66affSColin Finck 
1487c2c66affSColin Finck     return WINED3D_OK;
1488c2c66affSColin Finck }
1489c2c66affSColin Finck 
wined3d_device_get_stream_source_freq(const struct wined3d_device * device,UINT stream_idx,UINT * divider)1490c2c66affSColin Finck HRESULT CDECL wined3d_device_get_stream_source_freq(const struct wined3d_device *device,
1491c2c66affSColin Finck         UINT stream_idx, UINT *divider)
1492c2c66affSColin Finck {
1493c2c66affSColin Finck     const struct wined3d_stream_state *stream;
1494c2c66affSColin Finck 
1495c2c66affSColin Finck     TRACE("device %p, stream_idx %u, divider %p.\n", device, stream_idx, divider);
1496c2c66affSColin Finck 
1497c2c66affSColin Finck     stream = &device->state.streams[stream_idx];
1498c2c66affSColin Finck     *divider = stream->flags | stream->frequency;
1499c2c66affSColin Finck 
1500c2c66affSColin Finck     TRACE("Returning %#x.\n", *divider);
1501c2c66affSColin Finck 
1502c2c66affSColin Finck     return WINED3D_OK;
1503c2c66affSColin Finck }
1504c2c66affSColin Finck 
wined3d_device_set_transform(struct wined3d_device * device,enum wined3d_transform_state d3dts,const struct wined3d_matrix * matrix)1505c2c66affSColin Finck void CDECL wined3d_device_set_transform(struct wined3d_device *device,
1506c2c66affSColin Finck         enum wined3d_transform_state d3dts, const struct wined3d_matrix *matrix)
1507c2c66affSColin Finck {
1508c2c66affSColin Finck     TRACE("device %p, state %s, matrix %p.\n",
1509c2c66affSColin Finck             device, debug_d3dtstype(d3dts), matrix);
1510c2c66affSColin Finck     TRACE("%.8e %.8e %.8e %.8e\n", matrix->_11, matrix->_12, matrix->_13, matrix->_14);
1511c2c66affSColin Finck     TRACE("%.8e %.8e %.8e %.8e\n", matrix->_21, matrix->_22, matrix->_23, matrix->_24);
1512c2c66affSColin Finck     TRACE("%.8e %.8e %.8e %.8e\n", matrix->_31, matrix->_32, matrix->_33, matrix->_34);
1513c2c66affSColin Finck     TRACE("%.8e %.8e %.8e %.8e\n", matrix->_41, matrix->_42, matrix->_43, matrix->_44);
1514c2c66affSColin Finck 
1515c2c66affSColin Finck     /* Handle recording of state blocks. */
1516c2c66affSColin Finck     if (device->recording)
1517c2c66affSColin Finck     {
1518c2c66affSColin Finck         TRACE("Recording... not performing anything.\n");
1519c2c66affSColin Finck         device->recording->changed.transform[d3dts >> 5] |= 1u << (d3dts & 0x1f);
1520c2c66affSColin Finck         device->update_state->transforms[d3dts] = *matrix;
1521c2c66affSColin Finck         return;
1522c2c66affSColin Finck     }
1523c2c66affSColin Finck 
1524c2c66affSColin Finck     /* If the new matrix is the same as the current one,
1525c2c66affSColin Finck      * we cut off any further processing. this seems to be a reasonable
1526c2c66affSColin Finck      * optimization because as was noticed, some apps (warcraft3 for example)
1527c2c66affSColin Finck      * tend towards setting the same matrix repeatedly for some reason.
1528c2c66affSColin Finck      *
1529c2c66affSColin Finck      * From here on we assume that the new matrix is different, wherever it matters. */
1530c2c66affSColin Finck     if (!memcmp(&device->state.transforms[d3dts], matrix, sizeof(*matrix)))
1531c2c66affSColin Finck     {
1532c2c66affSColin Finck         TRACE("The application is setting the same matrix over again.\n");
1533c2c66affSColin Finck         return;
1534c2c66affSColin Finck     }
1535c2c66affSColin Finck 
1536c2c66affSColin Finck     device->state.transforms[d3dts] = *matrix;
1537c2c66affSColin Finck     wined3d_cs_emit_set_transform(device->cs, d3dts, matrix);
1538c2c66affSColin Finck }
1539c2c66affSColin Finck 
wined3d_device_get_transform(const struct wined3d_device * device,enum wined3d_transform_state state,struct wined3d_matrix * matrix)1540c2c66affSColin Finck void CDECL wined3d_device_get_transform(const struct wined3d_device *device,
1541c2c66affSColin Finck         enum wined3d_transform_state state, struct wined3d_matrix *matrix)
1542c2c66affSColin Finck {
1543c2c66affSColin Finck     TRACE("device %p, state %s, matrix %p.\n", device, debug_d3dtstype(state), matrix);
1544c2c66affSColin Finck 
1545c2c66affSColin Finck     *matrix = device->state.transforms[state];
1546c2c66affSColin Finck }
1547c2c66affSColin Finck 
wined3d_device_multiply_transform(struct wined3d_device * device,enum wined3d_transform_state state,const struct wined3d_matrix * matrix)1548c2c66affSColin Finck void CDECL wined3d_device_multiply_transform(struct wined3d_device *device,
1549c2c66affSColin Finck         enum wined3d_transform_state state, const struct wined3d_matrix *matrix)
1550c2c66affSColin Finck {
1551c2c66affSColin Finck     const struct wined3d_matrix *mat;
1552c2c66affSColin Finck     struct wined3d_matrix temp;
1553c2c66affSColin Finck 
1554c2c66affSColin Finck     TRACE("device %p, state %s, matrix %p.\n", device, debug_d3dtstype(state), matrix);
1555c2c66affSColin Finck 
1556c2c66affSColin Finck     /* Note: Using 'updateStateBlock' rather than 'stateblock' in the code
1557c2c66affSColin Finck      * below means it will be recorded in a state block change, but it
1558c2c66affSColin Finck      * works regardless where it is recorded.
1559c2c66affSColin Finck      * If this is found to be wrong, change to StateBlock. */
1560c2c66affSColin Finck     if (state > HIGHEST_TRANSFORMSTATE)
1561c2c66affSColin Finck     {
1562c2c66affSColin Finck         WARN("Unhandled transform state %#x.\n", state);
1563c2c66affSColin Finck         return;
1564c2c66affSColin Finck     }
1565c2c66affSColin Finck 
1566c2c66affSColin Finck     mat = &device->update_state->transforms[state];
1567c2c66affSColin Finck     multiply_matrix(&temp, mat, matrix);
1568c2c66affSColin Finck 
1569c2c66affSColin Finck     /* Apply change via set transform - will reapply to eg. lights this way. */
1570c2c66affSColin Finck     wined3d_device_set_transform(device, state, &temp);
1571c2c66affSColin Finck }
1572c2c66affSColin Finck 
1573c2c66affSColin Finck /* Note lights are real special cases. Although the device caps state only
1574c2c66affSColin Finck  * e.g. 8 are supported, you can reference any indexes you want as long as
1575c2c66affSColin Finck  * that number max are enabled at any one point in time. Therefore since the
1576c2c66affSColin Finck  * indices can be anything, we need a hashmap of them. However, this causes
1577c2c66affSColin Finck  * stateblock problems. When capturing the state block, I duplicate the
1578c2c66affSColin Finck  * hashmap, but when recording, just build a chain pretty much of commands to
1579c2c66affSColin Finck  * be replayed. */
wined3d_device_set_light(struct wined3d_device * device,UINT light_idx,const struct wined3d_light * light)1580c2c66affSColin Finck HRESULT CDECL wined3d_device_set_light(struct wined3d_device *device,
1581c2c66affSColin Finck         UINT light_idx, const struct wined3d_light *light)
1582c2c66affSColin Finck {
1583c2c66affSColin Finck     UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1584c2c66affSColin Finck     struct wined3d_light_info *object = NULL;
1585c2c66affSColin Finck     float rho;
1586c2c66affSColin Finck 
1587c2c66affSColin Finck     TRACE("device %p, light_idx %u, light %p.\n", device, light_idx, light);
1588c2c66affSColin Finck 
1589c2c66affSColin Finck     /* Check the parameter range. Need for speed most wanted sets junk lights
1590c2c66affSColin Finck      * which confuse the GL driver. */
1591c2c66affSColin Finck     if (!light)
1592c2c66affSColin Finck         return WINED3DERR_INVALIDCALL;
1593c2c66affSColin Finck 
1594c2c66affSColin Finck     switch (light->type)
1595c2c66affSColin Finck     {
1596c2c66affSColin Finck         case WINED3D_LIGHT_POINT:
1597c2c66affSColin Finck         case WINED3D_LIGHT_SPOT:
1598c2c66affSColin Finck         case WINED3D_LIGHT_GLSPOT:
1599c2c66affSColin Finck             /* Incorrect attenuation values can cause the gl driver to crash.
1600c2c66affSColin Finck              * Happens with Need for speed most wanted. */
1601c2c66affSColin Finck             if (light->attenuation0 < 0.0f || light->attenuation1 < 0.0f || light->attenuation2 < 0.0f)
1602c2c66affSColin Finck             {
1603c2c66affSColin Finck                 WARN("Attenuation is negative, returning WINED3DERR_INVALIDCALL.\n");
1604c2c66affSColin Finck                 return WINED3DERR_INVALIDCALL;
1605c2c66affSColin Finck             }
1606c2c66affSColin Finck             break;
1607c2c66affSColin Finck 
1608c2c66affSColin Finck         case WINED3D_LIGHT_DIRECTIONAL:
1609c2c66affSColin Finck         case WINED3D_LIGHT_PARALLELPOINT:
1610c2c66affSColin Finck             /* Ignores attenuation */
1611c2c66affSColin Finck             break;
1612c2c66affSColin Finck 
1613c2c66affSColin Finck         default:
1614c2c66affSColin Finck         WARN("Light type out of range, returning WINED3DERR_INVALIDCALL\n");
1615c2c66affSColin Finck         return WINED3DERR_INVALIDCALL;
1616c2c66affSColin Finck     }
1617c2c66affSColin Finck 
1618c2c66affSColin Finck     if (!(object = wined3d_state_get_light(device->update_state, light_idx)))
1619c2c66affSColin Finck     {
1620c2c66affSColin Finck         TRACE("Adding new light\n");
16219987f029SAmine Khaldi         if (!(object = heap_alloc_zero(sizeof(*object))))
1622c2c66affSColin Finck             return E_OUTOFMEMORY;
1623c2c66affSColin Finck 
1624c2c66affSColin Finck         list_add_head(&device->update_state->light_map[hash_idx], &object->entry);
1625c2c66affSColin Finck         object->glIndex = -1;
1626c2c66affSColin Finck         object->OriginalIndex = light_idx;
1627c2c66affSColin Finck     }
1628c2c66affSColin Finck 
1629c2c66affSColin Finck     /* Initialize the object. */
1630c2c66affSColin Finck     TRACE("Light %u setting to type %#x, diffuse %s, specular %s, ambient %s, "
1631c2c66affSColin Finck             "position {%.8e, %.8e, %.8e}, direction {%.8e, %.8e, %.8e}, "
1632c2c66affSColin Finck             "range %.8e, falloff %.8e, theta %.8e, phi %.8e.\n",
1633c2c66affSColin Finck             light_idx, light->type, debug_color(&light->diffuse),
1634c2c66affSColin Finck             debug_color(&light->specular), debug_color(&light->ambient),
1635c2c66affSColin Finck             light->position.x, light->position.y, light->position.z,
1636c2c66affSColin Finck             light->direction.x, light->direction.y, light->direction.z,
1637c2c66affSColin Finck             light->range, light->falloff, light->theta, light->phi);
1638c2c66affSColin Finck 
1639c2c66affSColin Finck     /* Save away the information. */
1640c2c66affSColin Finck     object->OriginalParms = *light;
1641c2c66affSColin Finck 
1642c2c66affSColin Finck     switch (light->type)
1643c2c66affSColin Finck     {
1644c2c66affSColin Finck         case WINED3D_LIGHT_POINT:
1645c2c66affSColin Finck             /* Position */
1646c2c66affSColin Finck             object->position.x = light->position.x;
1647c2c66affSColin Finck             object->position.y = light->position.y;
1648c2c66affSColin Finck             object->position.z = light->position.z;
1649c2c66affSColin Finck             object->position.w = 1.0f;
1650c2c66affSColin Finck             object->cutoff = 180.0f;
1651c2c66affSColin Finck             /* FIXME: Range */
1652c2c66affSColin Finck             break;
1653c2c66affSColin Finck 
1654c2c66affSColin Finck         case WINED3D_LIGHT_DIRECTIONAL:
1655c2c66affSColin Finck             /* Direction */
1656c2c66affSColin Finck             object->direction.x = -light->direction.x;
1657c2c66affSColin Finck             object->direction.y = -light->direction.y;
1658c2c66affSColin Finck             object->direction.z = -light->direction.z;
1659c2c66affSColin Finck             object->direction.w = 0.0f;
1660c2c66affSColin Finck             object->exponent = 0.0f;
1661c2c66affSColin Finck             object->cutoff = 180.0f;
1662c2c66affSColin Finck             break;
1663c2c66affSColin Finck 
1664c2c66affSColin Finck         case WINED3D_LIGHT_SPOT:
1665c2c66affSColin Finck             /* Position */
1666c2c66affSColin Finck             object->position.x = light->position.x;
1667c2c66affSColin Finck             object->position.y = light->position.y;
1668c2c66affSColin Finck             object->position.z = light->position.z;
1669c2c66affSColin Finck             object->position.w = 1.0f;
1670c2c66affSColin Finck 
1671c2c66affSColin Finck             /* Direction */
1672c2c66affSColin Finck             object->direction.x = light->direction.x;
1673c2c66affSColin Finck             object->direction.y = light->direction.y;
1674c2c66affSColin Finck             object->direction.z = light->direction.z;
1675c2c66affSColin Finck             object->direction.w = 0.0f;
1676c2c66affSColin Finck 
1677c2c66affSColin Finck             /* opengl-ish and d3d-ish spot lights use too different models
1678c2c66affSColin Finck              * for the light "intensity" as a function of the angle towards
1679c2c66affSColin Finck              * the main light direction, so we only can approximate very
1680c2c66affSColin Finck              * roughly. However, spot lights are rather rarely used in games
1681c2c66affSColin Finck              * (if ever used at all). Furthermore if still used, probably
1682c2c66affSColin Finck              * nobody pays attention to such details. */
1683c2c66affSColin Finck             if (!light->falloff)
1684c2c66affSColin Finck             {
1685c2c66affSColin Finck                 /* Falloff = 0 is easy, because d3d's and opengl's spot light
1686c2c66affSColin Finck                  * equations have the falloff resp. exponent parameter as an
1687c2c66affSColin Finck                  * exponent, so the spot light lighting will always be 1.0 for
1688c2c66affSColin Finck                  * both of them, and we don't have to care for the rest of the
1689c2c66affSColin Finck                  * rather complex calculation. */
1690c2c66affSColin Finck                 object->exponent = 0.0f;
1691c2c66affSColin Finck             }
1692c2c66affSColin Finck             else
1693c2c66affSColin Finck             {
1694c2c66affSColin Finck                 rho = light->theta + (light->phi - light->theta) / (2 * light->falloff);
1695c2c66affSColin Finck                 if (rho < 0.0001f)
1696c2c66affSColin Finck                     rho = 0.0001f;
1697c2c66affSColin Finck                 object->exponent = -0.3f / logf(cosf(rho / 2));
1698c2c66affSColin Finck             }
1699c2c66affSColin Finck 
1700c2c66affSColin Finck             if (object->exponent > 128.0f)
1701c2c66affSColin Finck                 object->exponent = 128.0f;
1702c2c66affSColin Finck 
1703c2c66affSColin Finck             object->cutoff = (float)(light->phi * 90 / M_PI);
1704c2c66affSColin Finck             /* FIXME: Range */
1705c2c66affSColin Finck             break;
1706c2c66affSColin Finck 
1707c2c66affSColin Finck         case WINED3D_LIGHT_PARALLELPOINT:
1708c2c66affSColin Finck             object->position.x = light->position.x;
1709c2c66affSColin Finck             object->position.y = light->position.y;
1710c2c66affSColin Finck             object->position.z = light->position.z;
1711c2c66affSColin Finck             object->position.w = 1.0f;
1712c2c66affSColin Finck             break;
1713c2c66affSColin Finck 
1714c2c66affSColin Finck         default:
1715c2c66affSColin Finck             FIXME("Unrecognized light type %#x.\n", light->type);
1716c2c66affSColin Finck     }
1717c2c66affSColin Finck 
1718c2c66affSColin Finck     if (!device->recording)
1719c2c66affSColin Finck         wined3d_cs_emit_set_light(device->cs, object);
1720c2c66affSColin Finck 
1721c2c66affSColin Finck     return WINED3D_OK;
1722c2c66affSColin Finck }
1723c2c66affSColin Finck 
wined3d_device_get_light(const struct wined3d_device * device,UINT light_idx,struct wined3d_light * light)1724c2c66affSColin Finck HRESULT CDECL wined3d_device_get_light(const struct wined3d_device *device,
1725c2c66affSColin Finck         UINT light_idx, struct wined3d_light *light)
1726c2c66affSColin Finck {
1727c2c66affSColin Finck     struct wined3d_light_info *light_info;
1728c2c66affSColin Finck 
1729c2c66affSColin Finck     TRACE("device %p, light_idx %u, light %p.\n", device, light_idx, light);
1730c2c66affSColin Finck 
1731c2c66affSColin Finck     if (!(light_info = wined3d_state_get_light(&device->state, light_idx)))
1732c2c66affSColin Finck     {
1733c2c66affSColin Finck         TRACE("Light information requested but light not defined\n");
1734c2c66affSColin Finck         return WINED3DERR_INVALIDCALL;
1735c2c66affSColin Finck     }
1736c2c66affSColin Finck 
1737c2c66affSColin Finck     *light = light_info->OriginalParms;
1738c2c66affSColin Finck     return WINED3D_OK;
1739c2c66affSColin Finck }
1740c2c66affSColin Finck 
wined3d_device_set_light_enable(struct wined3d_device * device,UINT light_idx,BOOL enable)1741c2c66affSColin Finck HRESULT CDECL wined3d_device_set_light_enable(struct wined3d_device *device, UINT light_idx, BOOL enable)
1742c2c66affSColin Finck {
1743c2c66affSColin Finck     struct wined3d_light_info *light_info;
1744c2c66affSColin Finck 
1745c2c66affSColin Finck     TRACE("device %p, light_idx %u, enable %#x.\n", device, light_idx, enable);
1746c2c66affSColin Finck 
1747c2c66affSColin Finck     /* Special case - enabling an undefined light creates one with a strict set of parameters. */
1748c2c66affSColin Finck     if (!(light_info = wined3d_state_get_light(device->update_state, light_idx)))
1749c2c66affSColin Finck     {
1750c2c66affSColin Finck         TRACE("Light enabled requested but light not defined, so defining one!\n");
1751c2c66affSColin Finck         wined3d_device_set_light(device, light_idx, &WINED3D_default_light);
1752c2c66affSColin Finck 
1753c2c66affSColin Finck         if (!(light_info = wined3d_state_get_light(device->update_state, light_idx)))
1754c2c66affSColin Finck         {
1755c2c66affSColin Finck             FIXME("Adding default lights has failed dismally\n");
1756c2c66affSColin Finck             return WINED3DERR_INVALIDCALL;
1757c2c66affSColin Finck         }
1758c2c66affSColin Finck     }
1759c2c66affSColin Finck 
1760c2c66affSColin Finck     wined3d_state_enable_light(device->update_state, &device->adapter->d3d_info, light_info, enable);
1761c2c66affSColin Finck     if (!device->recording)
1762c2c66affSColin Finck         wined3d_cs_emit_set_light_enable(device->cs, light_idx, enable);
1763c2c66affSColin Finck 
1764c2c66affSColin Finck     return WINED3D_OK;
1765c2c66affSColin Finck }
1766c2c66affSColin Finck 
wined3d_device_get_light_enable(const struct wined3d_device * device,UINT light_idx,BOOL * enable)1767c2c66affSColin Finck HRESULT CDECL wined3d_device_get_light_enable(const struct wined3d_device *device, UINT light_idx, BOOL *enable)
1768c2c66affSColin Finck {
1769c2c66affSColin Finck     struct wined3d_light_info *light_info;
1770c2c66affSColin Finck 
1771c2c66affSColin Finck     TRACE("device %p, light_idx %u, enable %p.\n", device, light_idx, enable);
1772c2c66affSColin Finck 
1773c2c66affSColin Finck     if (!(light_info = wined3d_state_get_light(&device->state, light_idx)))
1774c2c66affSColin Finck     {
1775c2c66affSColin Finck         TRACE("Light enabled state requested but light not defined.\n");
1776c2c66affSColin Finck         return WINED3DERR_INVALIDCALL;
1777c2c66affSColin Finck     }
1778c2c66affSColin Finck     /* true is 128 according to SetLightEnable */
1779c2c66affSColin Finck     *enable = light_info->enabled ? 128 : 0;
1780c2c66affSColin Finck     return WINED3D_OK;
1781c2c66affSColin Finck }
1782c2c66affSColin Finck 
wined3d_device_set_clip_plane(struct wined3d_device * device,UINT plane_idx,const struct wined3d_vec4 * plane)1783c2c66affSColin Finck HRESULT CDECL wined3d_device_set_clip_plane(struct wined3d_device *device,
1784c2c66affSColin Finck         UINT plane_idx, const struct wined3d_vec4 *plane)
1785c2c66affSColin Finck {
1786c2c66affSColin Finck     TRACE("device %p, plane_idx %u, plane %p.\n", device, plane_idx, plane);
1787c2c66affSColin Finck 
1788c2c66affSColin Finck     if (plane_idx >= device->adapter->gl_info.limits.user_clip_distances)
1789c2c66affSColin Finck     {
1790c2c66affSColin Finck         TRACE("Application has requested clipplane this device doesn't support.\n");
1791c2c66affSColin Finck         return WINED3DERR_INVALIDCALL;
1792c2c66affSColin Finck     }
1793c2c66affSColin Finck 
1794c2c66affSColin Finck     if (device->recording)
1795c2c66affSColin Finck         device->recording->changed.clipplane |= 1u << plane_idx;
1796c2c66affSColin Finck 
1797c2c66affSColin Finck     if (!memcmp(&device->update_state->clip_planes[plane_idx], plane, sizeof(*plane)))
1798c2c66affSColin Finck     {
1799c2c66affSColin Finck         TRACE("Application is setting old values over, nothing to do.\n");
1800c2c66affSColin Finck         return WINED3D_OK;
1801c2c66affSColin Finck     }
1802c2c66affSColin Finck 
1803c2c66affSColin Finck     device->update_state->clip_planes[plane_idx] = *plane;
1804c2c66affSColin Finck 
1805c2c66affSColin Finck     if (!device->recording)
1806c2c66affSColin Finck         wined3d_cs_emit_set_clip_plane(device->cs, plane_idx, plane);
1807c2c66affSColin Finck 
1808c2c66affSColin Finck     return WINED3D_OK;
1809c2c66affSColin Finck }
1810c2c66affSColin Finck 
wined3d_device_get_clip_plane(const struct wined3d_device * device,UINT plane_idx,struct wined3d_vec4 * plane)1811c2c66affSColin Finck HRESULT CDECL wined3d_device_get_clip_plane(const struct wined3d_device *device,
1812c2c66affSColin Finck         UINT plane_idx, struct wined3d_vec4 *plane)
1813c2c66affSColin Finck {
1814c2c66affSColin Finck     TRACE("device %p, plane_idx %u, plane %p.\n", device, plane_idx, plane);
1815c2c66affSColin Finck 
1816c2c66affSColin Finck     if (plane_idx >= device->adapter->gl_info.limits.user_clip_distances)
1817c2c66affSColin Finck     {
1818c2c66affSColin Finck         TRACE("Application has requested clipplane this device doesn't support.\n");
1819c2c66affSColin Finck         return WINED3DERR_INVALIDCALL;
1820c2c66affSColin Finck     }
1821c2c66affSColin Finck 
1822c2c66affSColin Finck     *plane = device->state.clip_planes[plane_idx];
1823c2c66affSColin Finck 
1824c2c66affSColin Finck     return WINED3D_OK;
1825c2c66affSColin Finck }
1826c2c66affSColin Finck 
wined3d_device_set_clip_status(struct wined3d_device * device,const struct wined3d_clip_status * clip_status)1827c2c66affSColin Finck HRESULT CDECL wined3d_device_set_clip_status(struct wined3d_device *device,
1828c2c66affSColin Finck         const struct wined3d_clip_status *clip_status)
1829c2c66affSColin Finck {
1830c2c66affSColin Finck     FIXME("device %p, clip_status %p stub!\n", device, clip_status);
1831c2c66affSColin Finck 
1832c2c66affSColin Finck     if (!clip_status)
1833c2c66affSColin Finck         return WINED3DERR_INVALIDCALL;
1834c2c66affSColin Finck 
1835c2c66affSColin Finck     return WINED3D_OK;
1836c2c66affSColin Finck }
1837c2c66affSColin Finck 
wined3d_device_get_clip_status(const struct wined3d_device * device,struct wined3d_clip_status * clip_status)1838c2c66affSColin Finck HRESULT CDECL wined3d_device_get_clip_status(const struct wined3d_device *device,
1839c2c66affSColin Finck         struct wined3d_clip_status *clip_status)
1840c2c66affSColin Finck {
1841c2c66affSColin Finck     FIXME("device %p, clip_status %p stub!\n", device, clip_status);
1842c2c66affSColin Finck 
1843c2c66affSColin Finck     if (!clip_status)
1844c2c66affSColin Finck         return WINED3DERR_INVALIDCALL;
1845c2c66affSColin Finck 
1846c2c66affSColin Finck     return WINED3D_OK;
1847c2c66affSColin Finck }
1848c2c66affSColin Finck 
wined3d_device_set_material(struct wined3d_device * device,const struct wined3d_material * material)1849c2c66affSColin Finck void CDECL wined3d_device_set_material(struct wined3d_device *device, const struct wined3d_material *material)
1850c2c66affSColin Finck {
1851c2c66affSColin Finck     TRACE("device %p, material %p.\n", device, material);
1852c2c66affSColin Finck 
1853c2c66affSColin Finck     device->update_state->material = *material;
1854c2c66affSColin Finck 
1855c2c66affSColin Finck     if (device->recording)
1856c2c66affSColin Finck         device->recording->changed.material = TRUE;
1857c2c66affSColin Finck     else
1858c2c66affSColin Finck         wined3d_cs_emit_set_material(device->cs, material);
1859c2c66affSColin Finck }
1860c2c66affSColin Finck 
wined3d_device_get_material(const struct wined3d_device * device,struct wined3d_material * material)1861c2c66affSColin Finck void CDECL wined3d_device_get_material(const struct wined3d_device *device, struct wined3d_material *material)
1862c2c66affSColin Finck {
1863c2c66affSColin Finck     TRACE("device %p, material %p.\n", device, material);
1864c2c66affSColin Finck 
1865c2c66affSColin Finck     *material = device->state.material;
1866c2c66affSColin Finck 
1867c2c66affSColin Finck     TRACE("diffuse %s\n", debug_color(&material->diffuse));
1868c2c66affSColin Finck     TRACE("ambient %s\n", debug_color(&material->ambient));
1869c2c66affSColin Finck     TRACE("specular %s\n", debug_color(&material->specular));
1870c2c66affSColin Finck     TRACE("emissive %s\n", debug_color(&material->emissive));
1871c2c66affSColin Finck     TRACE("power %.8e.\n", material->power);
1872c2c66affSColin Finck }
1873c2c66affSColin Finck 
wined3d_device_set_index_buffer(struct wined3d_device * device,struct wined3d_buffer * buffer,enum wined3d_format_id format_id,unsigned int offset)1874c2c66affSColin Finck void CDECL wined3d_device_set_index_buffer(struct wined3d_device *device,
1875c2c66affSColin Finck         struct wined3d_buffer *buffer, enum wined3d_format_id format_id, unsigned int offset)
1876c2c66affSColin Finck {
1877c2c66affSColin Finck     enum wined3d_format_id prev_format;
1878c2c66affSColin Finck     struct wined3d_buffer *prev_buffer;
1879c2c66affSColin Finck     unsigned int prev_offset;
1880c2c66affSColin Finck 
1881c2c66affSColin Finck     TRACE("device %p, buffer %p, format %s, offset %u.\n",
1882c2c66affSColin Finck             device, buffer, debug_d3dformat(format_id), offset);
1883c2c66affSColin Finck 
1884c2c66affSColin Finck     prev_buffer = device->update_state->index_buffer;
1885c2c66affSColin Finck     prev_format = device->update_state->index_format;
1886c2c66affSColin Finck     prev_offset = device->update_state->index_offset;
1887c2c66affSColin Finck 
1888c2c66affSColin Finck     device->update_state->index_buffer = buffer;
1889c2c66affSColin Finck     device->update_state->index_format = format_id;
1890c2c66affSColin Finck     device->update_state->index_offset = offset;
1891c2c66affSColin Finck 
1892c2c66affSColin Finck     if (device->recording)
1893c2c66affSColin Finck         device->recording->changed.indices = TRUE;
1894c2c66affSColin Finck 
1895c2c66affSColin Finck     if (prev_buffer == buffer && prev_format == format_id && prev_offset == offset)
1896c2c66affSColin Finck         return;
1897c2c66affSColin Finck 
1898c2c66affSColin Finck     if (buffer)
1899c2c66affSColin Finck         wined3d_buffer_incref(buffer);
1900c2c66affSColin Finck     if (!device->recording)
1901c2c66affSColin Finck         wined3d_cs_emit_set_index_buffer(device->cs, buffer, format_id, offset);
1902c2c66affSColin Finck     if (prev_buffer)
1903c2c66affSColin Finck         wined3d_buffer_decref(prev_buffer);
1904c2c66affSColin Finck }
1905c2c66affSColin Finck 
wined3d_device_get_index_buffer(const struct wined3d_device * device,enum wined3d_format_id * format,unsigned int * offset)1906c2c66affSColin Finck struct wined3d_buffer * CDECL wined3d_device_get_index_buffer(const struct wined3d_device *device,
1907c2c66affSColin Finck         enum wined3d_format_id *format, unsigned int *offset)
1908c2c66affSColin Finck {
1909c2c66affSColin Finck     TRACE("device %p, format %p, offset %p.\n", device, format, offset);
1910c2c66affSColin Finck 
1911c2c66affSColin Finck     *format = device->state.index_format;
1912c2c66affSColin Finck     if (offset)
1913c2c66affSColin Finck         *offset = device->state.index_offset;
1914c2c66affSColin Finck     return device->state.index_buffer;
1915c2c66affSColin Finck }
1916c2c66affSColin Finck 
wined3d_device_set_base_vertex_index(struct wined3d_device * device,INT base_index)1917c2c66affSColin Finck void CDECL wined3d_device_set_base_vertex_index(struct wined3d_device *device, INT base_index)
1918c2c66affSColin Finck {
1919c2c66affSColin Finck     TRACE("device %p, base_index %d.\n", device, base_index);
1920c2c66affSColin Finck 
1921c2c66affSColin Finck     device->update_state->base_vertex_index = base_index;
1922c2c66affSColin Finck }
1923c2c66affSColin Finck 
wined3d_device_get_base_vertex_index(const struct wined3d_device * device)1924c2c66affSColin Finck INT CDECL wined3d_device_get_base_vertex_index(const struct wined3d_device *device)
1925c2c66affSColin Finck {
1926c2c66affSColin Finck     TRACE("device %p.\n", device);
1927c2c66affSColin Finck 
1928c2c66affSColin Finck     return device->state.base_vertex_index;
1929c2c66affSColin Finck }
1930c2c66affSColin Finck 
wined3d_device_set_viewport(struct wined3d_device * device,const struct wined3d_viewport * viewport)1931*81cffd76SJoachim Henze void CDECL wined3d_device_set_viewport(struct wined3d_device *device, const struct wined3d_viewport *viewport)
1932c2c66affSColin Finck {
1933*81cffd76SJoachim Henze     TRACE("device %p, viewport %p.\n", device, viewport);
1934*81cffd76SJoachim Henze     TRACE("x %.8e, y %.8e, w %.8e, h %.8e, min_z %.8e, max_z %.8e.\n",
1935*81cffd76SJoachim Henze           viewport->x, viewport->y, viewport->width, viewport->height, viewport->min_z, viewport->max_z);
1936c2c66affSColin Finck 
1937*81cffd76SJoachim Henze     device->update_state->viewport = *viewport;
1938c2c66affSColin Finck 
1939c2c66affSColin Finck     /* Handle recording of state blocks */
1940c2c66affSColin Finck     if (device->recording)
1941c2c66affSColin Finck     {
1942c2c66affSColin Finck         TRACE("Recording... not performing anything\n");
1943c2c66affSColin Finck         device->recording->changed.viewport = TRUE;
1944c2c66affSColin Finck         return;
1945c2c66affSColin Finck     }
1946c2c66affSColin Finck 
1947*81cffd76SJoachim Henze     wined3d_cs_emit_set_viewport(device->cs, viewport);
1948c2c66affSColin Finck }
1949c2c66affSColin Finck 
wined3d_device_get_viewport(const struct wined3d_device * device,struct wined3d_viewport * viewport)1950*81cffd76SJoachim Henze void CDECL wined3d_device_get_viewport(const struct wined3d_device *device, struct wined3d_viewport *viewport)
1951c2c66affSColin Finck {
1952*81cffd76SJoachim Henze     TRACE("device %p, viewport %p.\n", device, viewport);
1953c2c66affSColin Finck 
1954*81cffd76SJoachim Henze     *viewport = device->state.viewport;
1955c2c66affSColin Finck }
1956c2c66affSColin Finck 
resolve_depth_buffer(struct wined3d_device * device)19579987f029SAmine Khaldi static void resolve_depth_buffer(struct wined3d_device *device)
1958c2c66affSColin Finck {
19599987f029SAmine Khaldi     const struct wined3d_state *state = &device->state;
1960c2c66affSColin Finck     struct wined3d_rendertarget_view *src_view;
19619987f029SAmine Khaldi     struct wined3d_resource *dst_resource;
19629987f029SAmine Khaldi     struct wined3d_texture *dst_texture;
1963c2c66affSColin Finck 
19649987f029SAmine Khaldi     if (!(dst_texture = state->textures[0]))
1965c2c66affSColin Finck         return;
19669987f029SAmine Khaldi     dst_resource = &dst_texture->resource;
19679987f029SAmine Khaldi     if (!(dst_resource->format_flags & WINED3DFMT_FLAG_DEPTH))
19689987f029SAmine Khaldi         return;
1969c2c66affSColin Finck     if (!(src_view = state->fb->depth_stencil))
1970c2c66affSColin Finck         return;
19719987f029SAmine Khaldi 
19729987f029SAmine Khaldi     wined3d_device_resolve_sub_resource(device, dst_resource, 0,
19739987f029SAmine Khaldi             src_view->resource, src_view->sub_resource_idx, dst_resource->format->id);
1974c2c66affSColin Finck }
1975c2c66affSColin Finck 
wined3d_device_set_blend_state(struct wined3d_device * device,struct wined3d_blend_state * blend_state)19769987f029SAmine Khaldi void CDECL wined3d_device_set_blend_state(struct wined3d_device *device, struct wined3d_blend_state *blend_state)
19779987f029SAmine Khaldi {
19789987f029SAmine Khaldi     struct wined3d_blend_state *prev;
19799987f029SAmine Khaldi 
19809987f029SAmine Khaldi     TRACE("device %p, blend_state %p.\n", device, blend_state);
19819987f029SAmine Khaldi 
19829987f029SAmine Khaldi     prev = device->update_state->blend_state;
19839987f029SAmine Khaldi     if (prev == blend_state)
19849987f029SAmine Khaldi         return;
19859987f029SAmine Khaldi 
19869987f029SAmine Khaldi     if (blend_state)
19879987f029SAmine Khaldi         wined3d_blend_state_incref(blend_state);
19889987f029SAmine Khaldi     device->update_state->blend_state = blend_state;
19899987f029SAmine Khaldi     wined3d_cs_emit_set_blend_state(device->cs, blend_state);
19909987f029SAmine Khaldi     if (prev)
19919987f029SAmine Khaldi         wined3d_blend_state_decref(prev);
19929987f029SAmine Khaldi }
19939987f029SAmine Khaldi 
wined3d_device_get_blend_state(const struct wined3d_device * device)19949987f029SAmine Khaldi struct wined3d_blend_state * CDECL wined3d_device_get_blend_state(const struct wined3d_device *device)
19959987f029SAmine Khaldi {
19969987f029SAmine Khaldi     TRACE("device %p.\n", device);
19979987f029SAmine Khaldi 
19989987f029SAmine Khaldi     return device->state.blend_state;
1999c2c66affSColin Finck }
2000c2c66affSColin Finck 
wined3d_device_set_rasterizer_state(struct wined3d_device * device,struct wined3d_rasterizer_state * rasterizer_state)2001c2c66affSColin Finck void CDECL wined3d_device_set_rasterizer_state(struct wined3d_device *device,
2002c2c66affSColin Finck         struct wined3d_rasterizer_state *rasterizer_state)
2003c2c66affSColin Finck {
2004c2c66affSColin Finck     struct wined3d_rasterizer_state *prev;
2005c2c66affSColin Finck 
2006c2c66affSColin Finck     TRACE("device %p, rasterizer_state %p.\n", device, rasterizer_state);
2007c2c66affSColin Finck 
2008c2c66affSColin Finck     prev = device->update_state->rasterizer_state;
2009c2c66affSColin Finck     if (prev == rasterizer_state)
2010c2c66affSColin Finck         return;
2011c2c66affSColin Finck 
2012c2c66affSColin Finck     if (rasterizer_state)
2013c2c66affSColin Finck         wined3d_rasterizer_state_incref(rasterizer_state);
2014c2c66affSColin Finck     device->update_state->rasterizer_state = rasterizer_state;
2015c2c66affSColin Finck     wined3d_cs_emit_set_rasterizer_state(device->cs, rasterizer_state);
2016c2c66affSColin Finck     if (prev)
2017c2c66affSColin Finck         wined3d_rasterizer_state_decref(prev);
2018c2c66affSColin Finck }
2019c2c66affSColin Finck 
wined3d_device_get_rasterizer_state(struct wined3d_device * device)2020c2c66affSColin Finck struct wined3d_rasterizer_state * CDECL wined3d_device_get_rasterizer_state(struct wined3d_device *device)
2021c2c66affSColin Finck {
2022c2c66affSColin Finck     TRACE("device %p.\n", device);
2023c2c66affSColin Finck 
2024c2c66affSColin Finck     return device->state.rasterizer_state;
2025c2c66affSColin Finck }
2026c2c66affSColin Finck 
wined3d_device_set_render_state(struct wined3d_device * device,enum wined3d_render_state state,DWORD value)2027c2c66affSColin Finck void CDECL wined3d_device_set_render_state(struct wined3d_device *device,
2028c2c66affSColin Finck         enum wined3d_render_state state, DWORD value)
2029c2c66affSColin Finck {
2030c2c66affSColin Finck     DWORD old_value;
2031c2c66affSColin Finck 
2032c2c66affSColin Finck     TRACE("device %p, state %s (%#x), value %#x.\n", device, debug_d3drenderstate(state), state, value);
2033c2c66affSColin Finck 
2034c2c66affSColin Finck     if (state > WINEHIGHEST_RENDER_STATE)
2035c2c66affSColin Finck     {
2036c2c66affSColin Finck         WARN("Unhandled render state %#x.\n", state);
2037c2c66affSColin Finck         return;
2038c2c66affSColin Finck     }
2039c2c66affSColin Finck 
2040c2c66affSColin Finck     old_value = device->state.render_states[state];
2041c2c66affSColin Finck     device->update_state->render_states[state] = value;
2042c2c66affSColin Finck 
2043c2c66affSColin Finck     /* Handle recording of state blocks. */
2044c2c66affSColin Finck     if (device->recording)
2045c2c66affSColin Finck     {
2046c2c66affSColin Finck         TRACE("Recording... not performing anything.\n");
2047c2c66affSColin Finck         device->recording->changed.renderState[state >> 5] |= 1u << (state & 0x1f);
2048c2c66affSColin Finck         return;
2049c2c66affSColin Finck     }
2050c2c66affSColin Finck 
2051c2c66affSColin Finck     /* Compared here and not before the assignment to allow proper stateblock recording. */
2052c2c66affSColin Finck     if (value == old_value)
2053c2c66affSColin Finck         TRACE("Application is setting the old value over, nothing to do.\n");
2054c2c66affSColin Finck     else
2055c2c66affSColin Finck         wined3d_cs_emit_set_render_state(device->cs, state, value);
2056c2c66affSColin Finck 
2057c2c66affSColin Finck     if (state == WINED3D_RS_POINTSIZE && value == WINED3D_RESZ_CODE)
2058c2c66affSColin Finck     {
2059c2c66affSColin Finck         TRACE("RESZ multisampled depth buffer resolve triggered.\n");
20609987f029SAmine Khaldi         resolve_depth_buffer(device);
2061c2c66affSColin Finck     }
2062c2c66affSColin Finck }
2063c2c66affSColin Finck 
wined3d_device_get_render_state(const struct wined3d_device * device,enum wined3d_render_state state)2064c2c66affSColin Finck DWORD CDECL wined3d_device_get_render_state(const struct wined3d_device *device, enum wined3d_render_state state)
2065c2c66affSColin Finck {
2066c2c66affSColin Finck     TRACE("device %p, state %s (%#x).\n", device, debug_d3drenderstate(state), state);
2067c2c66affSColin Finck 
2068c2c66affSColin Finck     return device->state.render_states[state];
2069c2c66affSColin Finck }
2070c2c66affSColin Finck 
wined3d_device_set_sampler_state(struct wined3d_device * device,UINT sampler_idx,enum wined3d_sampler_state state,DWORD value)2071c2c66affSColin Finck void CDECL wined3d_device_set_sampler_state(struct wined3d_device *device,
2072c2c66affSColin Finck         UINT sampler_idx, enum wined3d_sampler_state state, DWORD value)
2073c2c66affSColin Finck {
2074c2c66affSColin Finck     DWORD old_value;
2075c2c66affSColin Finck 
2076c2c66affSColin Finck     TRACE("device %p, sampler_idx %u, state %s, value %#x.\n",
2077c2c66affSColin Finck             device, sampler_idx, debug_d3dsamplerstate(state), value);
2078c2c66affSColin Finck 
2079c2c66affSColin Finck     if (sampler_idx >= WINED3DVERTEXTEXTURESAMPLER0 && sampler_idx <= WINED3DVERTEXTEXTURESAMPLER3)
2080c2c66affSColin Finck         sampler_idx -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
2081c2c66affSColin Finck 
2082d6ac0a71SAmine Khaldi     if (sampler_idx >= ARRAY_SIZE(device->state.sampler_states))
2083c2c66affSColin Finck     {
2084c2c66affSColin Finck         WARN("Invalid sampler %u.\n", sampler_idx);
2085c2c66affSColin Finck         return; /* Windows accepts overflowing this array ... we do not. */
2086c2c66affSColin Finck     }
2087c2c66affSColin Finck 
2088c2c66affSColin Finck     old_value = device->state.sampler_states[sampler_idx][state];
2089c2c66affSColin Finck     device->update_state->sampler_states[sampler_idx][state] = value;
2090c2c66affSColin Finck 
2091c2c66affSColin Finck     /* Handle recording of state blocks. */
2092c2c66affSColin Finck     if (device->recording)
2093c2c66affSColin Finck     {
2094c2c66affSColin Finck         TRACE("Recording... not performing anything.\n");
2095c2c66affSColin Finck         device->recording->changed.samplerState[sampler_idx] |= 1u << state;
2096c2c66affSColin Finck         return;
2097c2c66affSColin Finck     }
2098c2c66affSColin Finck 
2099c2c66affSColin Finck     if (old_value == value)
2100c2c66affSColin Finck     {
2101c2c66affSColin Finck         TRACE("Application is setting the old value over, nothing to do.\n");
2102c2c66affSColin Finck         return;
2103c2c66affSColin Finck     }
2104c2c66affSColin Finck 
2105c2c66affSColin Finck     wined3d_cs_emit_set_sampler_state(device->cs, sampler_idx, state, value);
2106c2c66affSColin Finck }
2107c2c66affSColin Finck 
wined3d_device_get_sampler_state(const struct wined3d_device * device,UINT sampler_idx,enum wined3d_sampler_state state)2108c2c66affSColin Finck DWORD CDECL wined3d_device_get_sampler_state(const struct wined3d_device *device,
2109c2c66affSColin Finck         UINT sampler_idx, enum wined3d_sampler_state state)
2110c2c66affSColin Finck {
2111c2c66affSColin Finck     TRACE("device %p, sampler_idx %u, state %s.\n",
2112c2c66affSColin Finck             device, sampler_idx, debug_d3dsamplerstate(state));
2113c2c66affSColin Finck 
2114c2c66affSColin Finck     if (sampler_idx >= WINED3DVERTEXTEXTURESAMPLER0 && sampler_idx <= WINED3DVERTEXTEXTURESAMPLER3)
2115c2c66affSColin Finck         sampler_idx -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
2116c2c66affSColin Finck 
2117d6ac0a71SAmine Khaldi     if (sampler_idx >= ARRAY_SIZE(device->state.sampler_states))
2118c2c66affSColin Finck     {
2119c2c66affSColin Finck         WARN("Invalid sampler %u.\n", sampler_idx);
2120c2c66affSColin Finck         return 0; /* Windows accepts overflowing this array ... we do not. */
2121c2c66affSColin Finck     }
2122c2c66affSColin Finck 
2123c2c66affSColin Finck     return device->state.sampler_states[sampler_idx][state];
2124c2c66affSColin Finck }
2125c2c66affSColin Finck 
wined3d_device_set_scissor_rect(struct wined3d_device * device,const RECT * rect)2126*81cffd76SJoachim Henze void CDECL wined3d_device_set_scissor_rect(struct wined3d_device *device, const RECT *rect)
2127c2c66affSColin Finck {
2128*81cffd76SJoachim Henze     TRACE("device %p, rect %s.\n", device, wine_dbgstr_rect(rect));
2129c2c66affSColin Finck 
2130c2c66affSColin Finck     if (device->recording)
2131c2c66affSColin Finck         device->recording->changed.scissorRect = TRUE;
2132c2c66affSColin Finck 
2133*81cffd76SJoachim Henze     if (EqualRect(&device->update_state->scissor_rect, rect))
2134c2c66affSColin Finck     {
2135*81cffd76SJoachim Henze         TRACE("App is setting the old scissor rectangle over, nothing to do.\n");
2136c2c66affSColin Finck         return;
2137c2c66affSColin Finck     }
2138*81cffd76SJoachim Henze     CopyRect(&device->update_state->scissor_rect, rect);
2139c2c66affSColin Finck 
2140c2c66affSColin Finck     if (device->recording)
2141c2c66affSColin Finck     {
2142c2c66affSColin Finck         TRACE("Recording... not performing anything.\n");
2143c2c66affSColin Finck         return;
2144c2c66affSColin Finck     }
2145c2c66affSColin Finck 
2146*81cffd76SJoachim Henze     wined3d_cs_emit_set_scissor_rect(device->cs, rect);
2147c2c66affSColin Finck }
2148c2c66affSColin Finck 
wined3d_device_get_scissor_rect(const struct wined3d_device * device,RECT * rect)2149*81cffd76SJoachim Henze void CDECL wined3d_device_get_scissor_rect(const struct wined3d_device *device, RECT *rect)
2150c2c66affSColin Finck {
2151*81cffd76SJoachim Henze     TRACE("device %p, rect %p.\n", device, rect);
2152c2c66affSColin Finck 
2153*81cffd76SJoachim Henze     *rect = device->state.scissor_rect;
2154*81cffd76SJoachim Henze     TRACE("Returning rect %s.\n", wine_dbgstr_rect(rect));
2155c2c66affSColin Finck }
2156c2c66affSColin Finck 
wined3d_device_set_vertex_declaration(struct wined3d_device * device,struct wined3d_vertex_declaration * declaration)2157c2c66affSColin Finck void CDECL wined3d_device_set_vertex_declaration(struct wined3d_device *device,
2158c2c66affSColin Finck         struct wined3d_vertex_declaration *declaration)
2159c2c66affSColin Finck {
2160c2c66affSColin Finck     struct wined3d_vertex_declaration *prev = device->update_state->vertex_declaration;
2161c2c66affSColin Finck 
2162c2c66affSColin Finck     TRACE("device %p, declaration %p.\n", device, declaration);
2163c2c66affSColin Finck 
2164c2c66affSColin Finck     if (device->recording)
2165c2c66affSColin Finck         device->recording->changed.vertexDecl = TRUE;
2166c2c66affSColin Finck 
2167c2c66affSColin Finck     if (declaration == prev)
2168c2c66affSColin Finck         return;
2169c2c66affSColin Finck 
2170c2c66affSColin Finck     if (declaration)
2171c2c66affSColin Finck         wined3d_vertex_declaration_incref(declaration);
2172c2c66affSColin Finck     device->update_state->vertex_declaration = declaration;
2173c2c66affSColin Finck     if (!device->recording)
2174c2c66affSColin Finck         wined3d_cs_emit_set_vertex_declaration(device->cs, declaration);
2175c2c66affSColin Finck     if (prev)
2176c2c66affSColin Finck         wined3d_vertex_declaration_decref(prev);
2177c2c66affSColin Finck }
2178c2c66affSColin Finck 
wined3d_device_get_vertex_declaration(const struct wined3d_device * device)2179c2c66affSColin Finck struct wined3d_vertex_declaration * CDECL wined3d_device_get_vertex_declaration(const struct wined3d_device *device)
2180c2c66affSColin Finck {
2181c2c66affSColin Finck     TRACE("device %p.\n", device);
2182c2c66affSColin Finck 
2183c2c66affSColin Finck     return device->state.vertex_declaration;
2184c2c66affSColin Finck }
2185c2c66affSColin Finck 
wined3d_device_set_vertex_shader(struct wined3d_device * device,struct wined3d_shader * shader)2186c2c66affSColin Finck void CDECL wined3d_device_set_vertex_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2187c2c66affSColin Finck {
2188c2c66affSColin Finck     struct wined3d_shader *prev = device->update_state->shader[WINED3D_SHADER_TYPE_VERTEX];
2189c2c66affSColin Finck 
2190c2c66affSColin Finck     TRACE("device %p, shader %p.\n", device, shader);
2191c2c66affSColin Finck 
2192c2c66affSColin Finck     if (device->recording)
2193c2c66affSColin Finck         device->recording->changed.vertexShader = TRUE;
2194c2c66affSColin Finck 
2195c2c66affSColin Finck     if (shader == prev)
2196c2c66affSColin Finck         return;
2197c2c66affSColin Finck 
2198c2c66affSColin Finck     if (shader)
2199c2c66affSColin Finck         wined3d_shader_incref(shader);
2200c2c66affSColin Finck     device->update_state->shader[WINED3D_SHADER_TYPE_VERTEX] = shader;
2201c2c66affSColin Finck     if (!device->recording)
2202c2c66affSColin Finck         wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_VERTEX, shader);
2203c2c66affSColin Finck     if (prev)
2204c2c66affSColin Finck         wined3d_shader_decref(prev);
2205c2c66affSColin Finck }
2206c2c66affSColin Finck 
wined3d_device_get_vertex_shader(const struct wined3d_device * device)2207c2c66affSColin Finck struct wined3d_shader * CDECL wined3d_device_get_vertex_shader(const struct wined3d_device *device)
2208c2c66affSColin Finck {
2209c2c66affSColin Finck     TRACE("device %p.\n", device);
2210c2c66affSColin Finck 
2211c2c66affSColin Finck     return device->state.shader[WINED3D_SHADER_TYPE_VERTEX];
2212c2c66affSColin Finck }
2213c2c66affSColin Finck 
wined3d_device_set_constant_buffer(struct wined3d_device * device,enum wined3d_shader_type type,UINT idx,struct wined3d_buffer * buffer)2214c2c66affSColin Finck static void wined3d_device_set_constant_buffer(struct wined3d_device *device,
2215c2c66affSColin Finck         enum wined3d_shader_type type, UINT idx, struct wined3d_buffer *buffer)
2216c2c66affSColin Finck {
2217c2c66affSColin Finck     struct wined3d_buffer *prev;
2218c2c66affSColin Finck 
2219c2c66affSColin Finck     if (idx >= MAX_CONSTANT_BUFFERS)
2220c2c66affSColin Finck     {
2221c2c66affSColin Finck         WARN("Invalid constant buffer index %u.\n", idx);
2222c2c66affSColin Finck         return;
2223c2c66affSColin Finck     }
2224c2c66affSColin Finck 
2225c2c66affSColin Finck     prev = device->update_state->cb[type][idx];
2226c2c66affSColin Finck     if (buffer == prev)
2227c2c66affSColin Finck         return;
2228c2c66affSColin Finck 
2229c2c66affSColin Finck     if (buffer)
2230c2c66affSColin Finck         wined3d_buffer_incref(buffer);
2231c2c66affSColin Finck     device->update_state->cb[type][idx] = buffer;
2232c2c66affSColin Finck     if (!device->recording)
2233c2c66affSColin Finck         wined3d_cs_emit_set_constant_buffer(device->cs, type, idx, buffer);
2234c2c66affSColin Finck     if (prev)
2235c2c66affSColin Finck         wined3d_buffer_decref(prev);
2236c2c66affSColin Finck }
2237c2c66affSColin Finck 
wined3d_device_set_vs_cb(struct wined3d_device * device,UINT idx,struct wined3d_buffer * buffer)2238c2c66affSColin Finck void CDECL wined3d_device_set_vs_cb(struct wined3d_device *device, UINT idx, struct wined3d_buffer *buffer)
2239c2c66affSColin Finck {
2240c2c66affSColin Finck     TRACE("device %p, idx %u, buffer %p.\n", device, idx, buffer);
2241c2c66affSColin Finck 
2242c2c66affSColin Finck     wined3d_device_set_constant_buffer(device, WINED3D_SHADER_TYPE_VERTEX, idx, buffer);
2243c2c66affSColin Finck }
2244c2c66affSColin Finck 
wined3d_device_get_constant_buffer(const struct wined3d_device * device,enum wined3d_shader_type shader_type,unsigned int idx)2245c2c66affSColin Finck static struct wined3d_buffer *wined3d_device_get_constant_buffer(const struct wined3d_device *device,
2246c2c66affSColin Finck         enum wined3d_shader_type shader_type, unsigned int idx)
2247c2c66affSColin Finck {
2248c2c66affSColin Finck     if (idx >= MAX_CONSTANT_BUFFERS)
2249c2c66affSColin Finck     {
2250c2c66affSColin Finck         WARN("Invalid constant buffer index %u.\n", idx);
2251c2c66affSColin Finck         return NULL;
2252c2c66affSColin Finck     }
2253c2c66affSColin Finck 
2254c2c66affSColin Finck     return device->state.cb[shader_type][idx];
2255c2c66affSColin Finck }
2256c2c66affSColin Finck 
wined3d_device_get_vs_cb(const struct wined3d_device * device,UINT idx)2257c2c66affSColin Finck struct wined3d_buffer * CDECL wined3d_device_get_vs_cb(const struct wined3d_device *device, UINT idx)
2258c2c66affSColin Finck {
2259c2c66affSColin Finck     TRACE("device %p, idx %u.\n", device, idx);
2260c2c66affSColin Finck 
2261c2c66affSColin Finck     return wined3d_device_get_constant_buffer(device, WINED3D_SHADER_TYPE_VERTEX, idx);
2262c2c66affSColin Finck }
2263c2c66affSColin Finck 
wined3d_device_set_shader_resource_view(struct wined3d_device * device,enum wined3d_shader_type type,UINT idx,struct wined3d_shader_resource_view * view)2264c2c66affSColin Finck static void wined3d_device_set_shader_resource_view(struct wined3d_device *device,
2265c2c66affSColin Finck         enum wined3d_shader_type type, UINT idx, struct wined3d_shader_resource_view *view)
2266c2c66affSColin Finck {
2267c2c66affSColin Finck     struct wined3d_shader_resource_view *prev;
2268c2c66affSColin Finck 
2269c2c66affSColin Finck     if (idx >= MAX_SHADER_RESOURCE_VIEWS)
2270c2c66affSColin Finck     {
2271c2c66affSColin Finck         WARN("Invalid view index %u.\n", idx);
2272c2c66affSColin Finck         return;
2273c2c66affSColin Finck     }
2274c2c66affSColin Finck 
2275c2c66affSColin Finck     prev = device->update_state->shader_resource_view[type][idx];
2276c2c66affSColin Finck     if (view == prev)
2277c2c66affSColin Finck         return;
2278c2c66affSColin Finck 
2279c2c66affSColin Finck     if (view)
2280c2c66affSColin Finck         wined3d_shader_resource_view_incref(view);
2281c2c66affSColin Finck     device->update_state->shader_resource_view[type][idx] = view;
2282c2c66affSColin Finck     if (!device->recording)
2283c2c66affSColin Finck         wined3d_cs_emit_set_shader_resource_view(device->cs, type, idx, view);
2284c2c66affSColin Finck     if (prev)
2285c2c66affSColin Finck         wined3d_shader_resource_view_decref(prev);
2286c2c66affSColin Finck }
2287c2c66affSColin Finck 
wined3d_device_set_vs_resource_view(struct wined3d_device * device,UINT idx,struct wined3d_shader_resource_view * view)2288c2c66affSColin Finck void CDECL wined3d_device_set_vs_resource_view(struct wined3d_device *device,
2289c2c66affSColin Finck         UINT idx, struct wined3d_shader_resource_view *view)
2290c2c66affSColin Finck {
2291c2c66affSColin Finck     TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2292c2c66affSColin Finck 
2293c2c66affSColin Finck     wined3d_device_set_shader_resource_view(device, WINED3D_SHADER_TYPE_VERTEX, idx, view);
2294c2c66affSColin Finck }
2295c2c66affSColin Finck 
wined3d_device_get_shader_resource_view(const struct wined3d_device * device,enum wined3d_shader_type shader_type,unsigned int idx)2296c2c66affSColin Finck static struct wined3d_shader_resource_view *wined3d_device_get_shader_resource_view(
2297c2c66affSColin Finck         const struct wined3d_device *device, enum wined3d_shader_type shader_type, unsigned int idx)
2298c2c66affSColin Finck {
2299c2c66affSColin Finck     if (idx >= MAX_SHADER_RESOURCE_VIEWS)
2300c2c66affSColin Finck     {
2301c2c66affSColin Finck         WARN("Invalid view index %u.\n", idx);
2302c2c66affSColin Finck         return NULL;
2303c2c66affSColin Finck     }
2304c2c66affSColin Finck 
2305c2c66affSColin Finck     return device->state.shader_resource_view[shader_type][idx];
2306c2c66affSColin Finck }
2307c2c66affSColin Finck 
wined3d_device_get_vs_resource_view(const struct wined3d_device * device,UINT idx)2308c2c66affSColin Finck struct wined3d_shader_resource_view * CDECL wined3d_device_get_vs_resource_view(const struct wined3d_device *device,
2309c2c66affSColin Finck         UINT idx)
2310c2c66affSColin Finck {
2311c2c66affSColin Finck     TRACE("device %p, idx %u.\n", device, idx);
2312c2c66affSColin Finck 
2313c2c66affSColin Finck     return wined3d_device_get_shader_resource_view(device, WINED3D_SHADER_TYPE_VERTEX, idx);
2314c2c66affSColin Finck }
2315c2c66affSColin Finck 
wined3d_device_set_sampler(struct wined3d_device * device,enum wined3d_shader_type type,UINT idx,struct wined3d_sampler * sampler)2316c2c66affSColin Finck static void wined3d_device_set_sampler(struct wined3d_device *device,
2317c2c66affSColin Finck         enum wined3d_shader_type type, UINT idx, struct wined3d_sampler *sampler)
2318c2c66affSColin Finck {
2319c2c66affSColin Finck     struct wined3d_sampler *prev;
2320c2c66affSColin Finck 
2321c2c66affSColin Finck     if (idx >= MAX_SAMPLER_OBJECTS)
2322c2c66affSColin Finck     {
2323c2c66affSColin Finck         WARN("Invalid sampler index %u.\n", idx);
2324c2c66affSColin Finck         return;
2325c2c66affSColin Finck     }
2326c2c66affSColin Finck 
2327c2c66affSColin Finck     prev = device->update_state->sampler[type][idx];
2328c2c66affSColin Finck     if (sampler == prev)
2329c2c66affSColin Finck         return;
2330c2c66affSColin Finck 
2331c2c66affSColin Finck     if (sampler)
2332c2c66affSColin Finck         wined3d_sampler_incref(sampler);
2333c2c66affSColin Finck     device->update_state->sampler[type][idx] = sampler;
2334c2c66affSColin Finck     if (!device->recording)
2335c2c66affSColin Finck         wined3d_cs_emit_set_sampler(device->cs, type, idx, sampler);
2336c2c66affSColin Finck     if (prev)
2337c2c66affSColin Finck         wined3d_sampler_decref(prev);
2338c2c66affSColin Finck }
2339c2c66affSColin Finck 
wined3d_device_set_vs_sampler(struct wined3d_device * device,UINT idx,struct wined3d_sampler * sampler)2340c2c66affSColin Finck void CDECL wined3d_device_set_vs_sampler(struct wined3d_device *device, UINT idx, struct wined3d_sampler *sampler)
2341c2c66affSColin Finck {
2342c2c66affSColin Finck     TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2343c2c66affSColin Finck 
2344c2c66affSColin Finck     wined3d_device_set_sampler(device, WINED3D_SHADER_TYPE_VERTEX, idx, sampler);
2345c2c66affSColin Finck }
2346c2c66affSColin Finck 
wined3d_device_get_sampler(const struct wined3d_device * device,enum wined3d_shader_type shader_type,unsigned int idx)2347c2c66affSColin Finck static struct wined3d_sampler *wined3d_device_get_sampler(const struct wined3d_device *device,
2348c2c66affSColin Finck         enum wined3d_shader_type shader_type, unsigned int idx)
2349c2c66affSColin Finck {
2350c2c66affSColin Finck     if (idx >= MAX_SAMPLER_OBJECTS)
2351c2c66affSColin Finck     {
2352c2c66affSColin Finck         WARN("Invalid sampler index %u.\n", idx);
2353c2c66affSColin Finck         return NULL;
2354c2c66affSColin Finck     }
2355c2c66affSColin Finck 
2356c2c66affSColin Finck     return device->state.sampler[shader_type][idx];
2357c2c66affSColin Finck }
2358c2c66affSColin Finck 
wined3d_device_get_vs_sampler(const struct wined3d_device * device,UINT idx)2359c2c66affSColin Finck struct wined3d_sampler * CDECL wined3d_device_get_vs_sampler(const struct wined3d_device *device, UINT idx)
2360c2c66affSColin Finck {
2361c2c66affSColin Finck     TRACE("device %p, idx %u.\n", device, idx);
2362c2c66affSColin Finck 
2363c2c66affSColin Finck     return wined3d_device_get_sampler(device, WINED3D_SHADER_TYPE_VERTEX, idx);
2364c2c66affSColin Finck }
2365c2c66affSColin Finck 
wined3d_device_set_vs_consts_b(struct wined3d_device * device,unsigned int start_idx,unsigned int count,const BOOL * constants)2366c2c66affSColin Finck HRESULT CDECL wined3d_device_set_vs_consts_b(struct wined3d_device *device,
2367c2c66affSColin Finck         unsigned int start_idx, unsigned int count, const BOOL *constants)
2368c2c66affSColin Finck {
2369c2c66affSColin Finck     unsigned int i;
2370c2c66affSColin Finck 
2371c2c66affSColin Finck     TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2372c2c66affSColin Finck             device, start_idx, count, constants);
2373c2c66affSColin Finck 
2374c2c66affSColin Finck     if (!constants || start_idx >= WINED3D_MAX_CONSTS_B)
2375c2c66affSColin Finck         return WINED3DERR_INVALIDCALL;
2376c2c66affSColin Finck 
2377c2c66affSColin Finck     if (count > WINED3D_MAX_CONSTS_B - start_idx)
2378c2c66affSColin Finck         count = WINED3D_MAX_CONSTS_B - start_idx;
2379c2c66affSColin Finck     memcpy(&device->update_state->vs_consts_b[start_idx], constants, count * sizeof(*constants));
2380c2c66affSColin Finck     if (TRACE_ON(d3d))
2381c2c66affSColin Finck     {
2382c2c66affSColin Finck         for (i = 0; i < count; ++i)
2383c2c66affSColin Finck             TRACE("Set BOOL constant %u to %#x.\n", start_idx + i, constants[i]);
2384c2c66affSColin Finck     }
2385c2c66affSColin Finck 
2386c2c66affSColin Finck     if (device->recording)
2387c2c66affSColin Finck     {
2388c2c66affSColin Finck         for (i = start_idx; i < count + start_idx; ++i)
2389c2c66affSColin Finck             device->recording->changed.vertexShaderConstantsB |= (1u << i);
2390c2c66affSColin Finck     }
2391c2c66affSColin Finck     else
2392c2c66affSColin Finck     {
2393c2c66affSColin Finck         wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_VS_B, start_idx, count, constants);
2394c2c66affSColin Finck     }
2395c2c66affSColin Finck 
2396c2c66affSColin Finck     return WINED3D_OK;
2397c2c66affSColin Finck }
2398c2c66affSColin Finck 
wined3d_device_get_vs_consts_b(const struct wined3d_device * device,unsigned int start_idx,unsigned int count,BOOL * constants)2399c2c66affSColin Finck HRESULT CDECL wined3d_device_get_vs_consts_b(const struct wined3d_device *device,
2400c2c66affSColin Finck         unsigned int start_idx, unsigned int count, BOOL *constants)
2401c2c66affSColin Finck {
2402c2c66affSColin Finck     TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2403c2c66affSColin Finck             device, start_idx, count, constants);
2404c2c66affSColin Finck 
2405c2c66affSColin Finck     if (!constants || start_idx >= WINED3D_MAX_CONSTS_B)
2406c2c66affSColin Finck         return WINED3DERR_INVALIDCALL;
2407c2c66affSColin Finck 
2408c2c66affSColin Finck     if (count > WINED3D_MAX_CONSTS_B - start_idx)
2409c2c66affSColin Finck         count = WINED3D_MAX_CONSTS_B - start_idx;
2410c2c66affSColin Finck     memcpy(constants, &device->state.vs_consts_b[start_idx], count * sizeof(*constants));
2411c2c66affSColin Finck 
2412c2c66affSColin Finck     return WINED3D_OK;
2413c2c66affSColin Finck }
2414c2c66affSColin Finck 
wined3d_device_set_vs_consts_i(struct wined3d_device * device,unsigned int start_idx,unsigned int count,const struct wined3d_ivec4 * constants)2415c2c66affSColin Finck HRESULT CDECL wined3d_device_set_vs_consts_i(struct wined3d_device *device,
2416c2c66affSColin Finck         unsigned int start_idx, unsigned int count, const struct wined3d_ivec4 *constants)
2417c2c66affSColin Finck {
2418c2c66affSColin Finck     unsigned int i;
2419c2c66affSColin Finck 
2420c2c66affSColin Finck     TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2421c2c66affSColin Finck             device, start_idx, count, constants);
2422c2c66affSColin Finck 
2423c2c66affSColin Finck     if (!constants || start_idx >= WINED3D_MAX_CONSTS_I)
2424c2c66affSColin Finck         return WINED3DERR_INVALIDCALL;
2425c2c66affSColin Finck 
2426c2c66affSColin Finck     if (count > WINED3D_MAX_CONSTS_I - start_idx)
2427c2c66affSColin Finck         count = WINED3D_MAX_CONSTS_I - start_idx;
2428c2c66affSColin Finck     memcpy(&device->update_state->vs_consts_i[start_idx], constants, count * sizeof(*constants));
2429c2c66affSColin Finck     if (TRACE_ON(d3d))
2430c2c66affSColin Finck     {
2431c2c66affSColin Finck         for (i = 0; i < count; ++i)
2432c2c66affSColin Finck             TRACE("Set ivec4 constant %u to %s.\n", start_idx + i, debug_ivec4(&constants[i]));
2433c2c66affSColin Finck     }
2434c2c66affSColin Finck 
2435c2c66affSColin Finck     if (device->recording)
2436c2c66affSColin Finck     {
2437c2c66affSColin Finck         for (i = start_idx; i < count + start_idx; ++i)
2438c2c66affSColin Finck             device->recording->changed.vertexShaderConstantsI |= (1u << i);
2439c2c66affSColin Finck     }
2440c2c66affSColin Finck     else
2441c2c66affSColin Finck     {
2442c2c66affSColin Finck         wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_VS_I, start_idx, count, constants);
2443c2c66affSColin Finck     }
2444c2c66affSColin Finck 
2445c2c66affSColin Finck     return WINED3D_OK;
2446c2c66affSColin Finck }
2447c2c66affSColin Finck 
wined3d_device_get_vs_consts_i(const struct wined3d_device * device,unsigned int start_idx,unsigned int count,struct wined3d_ivec4 * constants)2448c2c66affSColin Finck HRESULT CDECL wined3d_device_get_vs_consts_i(const struct wined3d_device *device,
2449c2c66affSColin Finck         unsigned int start_idx, unsigned int count, struct wined3d_ivec4 *constants)
2450c2c66affSColin Finck {
2451c2c66affSColin Finck     TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2452c2c66affSColin Finck             device, start_idx, count, constants);
2453c2c66affSColin Finck 
2454c2c66affSColin Finck     if (!constants || start_idx >= WINED3D_MAX_CONSTS_I)
2455c2c66affSColin Finck         return WINED3DERR_INVALIDCALL;
2456c2c66affSColin Finck 
2457c2c66affSColin Finck     if (count > WINED3D_MAX_CONSTS_I - start_idx)
2458c2c66affSColin Finck         count = WINED3D_MAX_CONSTS_I - start_idx;
2459c2c66affSColin Finck     memcpy(constants, &device->state.vs_consts_i[start_idx], count * sizeof(*constants));
2460c2c66affSColin Finck     return WINED3D_OK;
2461c2c66affSColin Finck }
2462c2c66affSColin Finck 
wined3d_device_set_vs_consts_f(struct wined3d_device * device,unsigned int start_idx,unsigned int count,const struct wined3d_vec4 * constants)2463c2c66affSColin Finck HRESULT CDECL wined3d_device_set_vs_consts_f(struct wined3d_device *device,
2464c2c66affSColin Finck         unsigned int start_idx, unsigned int count, const struct wined3d_vec4 *constants)
2465c2c66affSColin Finck {
2466c2c66affSColin Finck     const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2467c2c66affSColin Finck     unsigned int i;
2468c2c66affSColin Finck 
2469c2c66affSColin Finck     TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2470c2c66affSColin Finck             device, start_idx, count, constants);
2471c2c66affSColin Finck 
2472c2c66affSColin Finck     if (!constants || start_idx >= d3d_info->limits.vs_uniform_count
2473c2c66affSColin Finck             || count > d3d_info->limits.vs_uniform_count - start_idx)
2474c2c66affSColin Finck         return WINED3DERR_INVALIDCALL;
2475c2c66affSColin Finck 
2476c2c66affSColin Finck     memcpy(&device->update_state->vs_consts_f[start_idx], constants, count * sizeof(*constants));
2477c2c66affSColin Finck     if (TRACE_ON(d3d))
2478c2c66affSColin Finck     {
2479c2c66affSColin Finck         for (i = 0; i < count; ++i)
2480c2c66affSColin Finck             TRACE("Set vec4 constant %u to %s.\n", start_idx + i, debug_vec4(&constants[i]));
2481c2c66affSColin Finck     }
2482c2c66affSColin Finck 
2483c2c66affSColin Finck     if (device->recording)
2484c2c66affSColin Finck         memset(&device->recording->changed.vs_consts_f[start_idx], 1,
2485c2c66affSColin Finck                 count * sizeof(*device->recording->changed.vs_consts_f));
2486c2c66affSColin Finck     else
2487c2c66affSColin Finck         wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_VS_F, start_idx, count, constants);
2488c2c66affSColin Finck 
2489c2c66affSColin Finck     return WINED3D_OK;
2490c2c66affSColin Finck }
2491c2c66affSColin Finck 
wined3d_device_get_vs_consts_f(const struct wined3d_device * device,unsigned int start_idx,unsigned int count,struct wined3d_vec4 * constants)2492c2c66affSColin Finck HRESULT CDECL wined3d_device_get_vs_consts_f(const struct wined3d_device *device,
2493c2c66affSColin Finck         unsigned int start_idx, unsigned int count, struct wined3d_vec4 *constants)
2494c2c66affSColin Finck {
2495c2c66affSColin Finck     const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2496c2c66affSColin Finck 
2497c2c66affSColin Finck     TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2498c2c66affSColin Finck             device, start_idx, count, constants);
2499c2c66affSColin Finck 
2500c2c66affSColin Finck     if (!constants || start_idx >= d3d_info->limits.vs_uniform_count
2501c2c66affSColin Finck             || count > d3d_info->limits.vs_uniform_count - start_idx)
2502c2c66affSColin Finck         return WINED3DERR_INVALIDCALL;
2503c2c66affSColin Finck 
2504c2c66affSColin Finck     memcpy(constants, &device->state.vs_consts_f[start_idx], count * sizeof(*constants));
2505c2c66affSColin Finck 
2506c2c66affSColin Finck     return WINED3D_OK;
2507c2c66affSColin Finck }
2508c2c66affSColin Finck 
wined3d_device_set_pixel_shader(struct wined3d_device * device,struct wined3d_shader * shader)2509c2c66affSColin Finck void CDECL wined3d_device_set_pixel_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2510c2c66affSColin Finck {
2511c2c66affSColin Finck     struct wined3d_shader *prev = device->update_state->shader[WINED3D_SHADER_TYPE_PIXEL];
2512c2c66affSColin Finck 
2513c2c66affSColin Finck     TRACE("device %p, shader %p.\n", device, shader);
2514c2c66affSColin Finck 
2515c2c66affSColin Finck     if (device->recording)
2516c2c66affSColin Finck         device->recording->changed.pixelShader = TRUE;
2517c2c66affSColin Finck 
2518c2c66affSColin Finck     if (shader == prev)
2519c2c66affSColin Finck         return;
2520c2c66affSColin Finck 
2521c2c66affSColin Finck     if (shader)
2522c2c66affSColin Finck         wined3d_shader_incref(shader);
2523c2c66affSColin Finck     device->update_state->shader[WINED3D_SHADER_TYPE_PIXEL] = shader;
2524c2c66affSColin Finck     if (!device->recording)
2525c2c66affSColin Finck         wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_PIXEL, shader);
2526c2c66affSColin Finck     if (prev)
2527c2c66affSColin Finck         wined3d_shader_decref(prev);
2528c2c66affSColin Finck }
2529c2c66affSColin Finck 
wined3d_device_get_pixel_shader(const struct wined3d_device * device)2530c2c66affSColin Finck struct wined3d_shader * CDECL wined3d_device_get_pixel_shader(const struct wined3d_device *device)
2531c2c66affSColin Finck {
2532c2c66affSColin Finck     TRACE("device %p.\n", device);
2533c2c66affSColin Finck 
2534c2c66affSColin Finck     return device->state.shader[WINED3D_SHADER_TYPE_PIXEL];
2535c2c66affSColin Finck }
2536c2c66affSColin Finck 
wined3d_device_set_ps_cb(struct wined3d_device * device,UINT idx,struct wined3d_buffer * buffer)2537c2c66affSColin Finck void CDECL wined3d_device_set_ps_cb(struct wined3d_device *device, UINT idx, struct wined3d_buffer *buffer)
2538c2c66affSColin Finck {
2539c2c66affSColin Finck     TRACE("device %p, idx %u, buffer %p.\n", device, idx, buffer);
2540c2c66affSColin Finck 
2541c2c66affSColin Finck     wined3d_device_set_constant_buffer(device, WINED3D_SHADER_TYPE_PIXEL, idx, buffer);
2542c2c66affSColin Finck }
2543c2c66affSColin Finck 
wined3d_device_get_ps_cb(const struct wined3d_device * device,UINT idx)2544c2c66affSColin Finck struct wined3d_buffer * CDECL wined3d_device_get_ps_cb(const struct wined3d_device *device, UINT idx)
2545c2c66affSColin Finck {
2546c2c66affSColin Finck     TRACE("device %p, idx %u.\n", device, idx);
2547c2c66affSColin Finck 
2548c2c66affSColin Finck     return wined3d_device_get_constant_buffer(device, WINED3D_SHADER_TYPE_PIXEL, idx);
2549c2c66affSColin Finck }
2550c2c66affSColin Finck 
wined3d_device_set_ps_resource_view(struct wined3d_device * device,UINT idx,struct wined3d_shader_resource_view * view)2551c2c66affSColin Finck void CDECL wined3d_device_set_ps_resource_view(struct wined3d_device *device,
2552c2c66affSColin Finck         UINT idx, struct wined3d_shader_resource_view *view)
2553c2c66affSColin Finck {
2554c2c66affSColin Finck     TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2555c2c66affSColin Finck 
2556c2c66affSColin Finck     wined3d_device_set_shader_resource_view(device, WINED3D_SHADER_TYPE_PIXEL, idx, view);
2557c2c66affSColin Finck }
2558c2c66affSColin Finck 
wined3d_device_get_ps_resource_view(const struct wined3d_device * device,UINT idx)2559c2c66affSColin Finck struct wined3d_shader_resource_view * CDECL wined3d_device_get_ps_resource_view(const struct wined3d_device *device,
2560c2c66affSColin Finck         UINT idx)
2561c2c66affSColin Finck {
2562c2c66affSColin Finck     TRACE("device %p, idx %u.\n", device, idx);
2563c2c66affSColin Finck 
2564c2c66affSColin Finck     return wined3d_device_get_shader_resource_view(device, WINED3D_SHADER_TYPE_PIXEL, idx);
2565c2c66affSColin Finck }
2566c2c66affSColin Finck 
wined3d_device_set_ps_sampler(struct wined3d_device * device,UINT idx,struct wined3d_sampler * sampler)2567c2c66affSColin Finck void CDECL wined3d_device_set_ps_sampler(struct wined3d_device *device, UINT idx, struct wined3d_sampler *sampler)
2568c2c66affSColin Finck {
2569c2c66affSColin Finck     TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2570c2c66affSColin Finck 
2571c2c66affSColin Finck     wined3d_device_set_sampler(device, WINED3D_SHADER_TYPE_PIXEL, idx, sampler);
2572c2c66affSColin Finck }
2573c2c66affSColin Finck 
wined3d_device_get_ps_sampler(const struct wined3d_device * device,UINT idx)2574c2c66affSColin Finck struct wined3d_sampler * CDECL wined3d_device_get_ps_sampler(const struct wined3d_device *device, UINT idx)
2575c2c66affSColin Finck {
2576c2c66affSColin Finck     TRACE("device %p, idx %u.\n", device, idx);
2577c2c66affSColin Finck 
2578c2c66affSColin Finck     return wined3d_device_get_sampler(device, WINED3D_SHADER_TYPE_PIXEL, idx);
2579c2c66affSColin Finck }
2580c2c66affSColin Finck 
wined3d_device_set_ps_consts_b(struct wined3d_device * device,unsigned int start_idx,unsigned int count,const BOOL * constants)2581c2c66affSColin Finck HRESULT CDECL wined3d_device_set_ps_consts_b(struct wined3d_device *device,
2582c2c66affSColin Finck         unsigned int start_idx, unsigned int count, const BOOL *constants)
2583c2c66affSColin Finck {
2584c2c66affSColin Finck     unsigned int i;
2585c2c66affSColin Finck 
2586c2c66affSColin Finck     TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2587c2c66affSColin Finck             device, start_idx, count, constants);
2588c2c66affSColin Finck 
2589c2c66affSColin Finck     if (!constants || start_idx >= WINED3D_MAX_CONSTS_B)
2590c2c66affSColin Finck         return WINED3DERR_INVALIDCALL;
2591c2c66affSColin Finck 
2592c2c66affSColin Finck     if (count > WINED3D_MAX_CONSTS_B - start_idx)
2593c2c66affSColin Finck         count = WINED3D_MAX_CONSTS_B - start_idx;
2594c2c66affSColin Finck     memcpy(&device->update_state->ps_consts_b[start_idx], constants, count * sizeof(*constants));
2595c2c66affSColin Finck     if (TRACE_ON(d3d))
2596c2c66affSColin Finck     {
2597c2c66affSColin Finck         for (i = 0; i < count; ++i)
2598c2c66affSColin Finck             TRACE("Set BOOL constant %u to %#x.\n", start_idx + i, constants[i]);
2599c2c66affSColin Finck     }
2600c2c66affSColin Finck 
2601c2c66affSColin Finck     if (device->recording)
2602c2c66affSColin Finck     {
2603c2c66affSColin Finck         for (i = start_idx; i < count + start_idx; ++i)
2604c2c66affSColin Finck             device->recording->changed.pixelShaderConstantsB |= (1u << i);
2605c2c66affSColin Finck     }
2606c2c66affSColin Finck     else
2607c2c66affSColin Finck     {
2608c2c66affSColin Finck         wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_PS_B, start_idx, count, constants);
2609c2c66affSColin Finck     }
2610c2c66affSColin Finck 
2611c2c66affSColin Finck     return WINED3D_OK;
2612c2c66affSColin Finck }
2613c2c66affSColin Finck 
wined3d_device_get_ps_consts_b(const struct wined3d_device * device,unsigned int start_idx,unsigned int count,BOOL * constants)2614c2c66affSColin Finck HRESULT CDECL wined3d_device_get_ps_consts_b(const struct wined3d_device *device,
2615c2c66affSColin Finck         unsigned int start_idx, unsigned int count, BOOL *constants)
2616c2c66affSColin Finck {
2617c2c66affSColin Finck     TRACE("device %p, start_idx %u, count %u,constants %p.\n",
2618c2c66affSColin Finck             device, start_idx, count, constants);
2619c2c66affSColin Finck 
2620c2c66affSColin Finck     if (!constants || start_idx >= WINED3D_MAX_CONSTS_B)
2621c2c66affSColin Finck         return WINED3DERR_INVALIDCALL;
2622c2c66affSColin Finck 
2623c2c66affSColin Finck     if (count > WINED3D_MAX_CONSTS_B - start_idx)
2624c2c66affSColin Finck         count = WINED3D_MAX_CONSTS_B - start_idx;
2625c2c66affSColin Finck     memcpy(constants, &device->state.ps_consts_b[start_idx], count * sizeof(*constants));
2626c2c66affSColin Finck 
2627c2c66affSColin Finck     return WINED3D_OK;
2628c2c66affSColin Finck }
2629c2c66affSColin Finck 
wined3d_device_set_ps_consts_i(struct wined3d_device * device,unsigned int start_idx,unsigned int count,const struct wined3d_ivec4 * constants)2630c2c66affSColin Finck HRESULT CDECL wined3d_device_set_ps_consts_i(struct wined3d_device *device,
2631c2c66affSColin Finck         unsigned int start_idx, unsigned int count, const struct wined3d_ivec4 *constants)
2632c2c66affSColin Finck {
2633c2c66affSColin Finck     unsigned int i;
2634c2c66affSColin Finck 
2635c2c66affSColin Finck     TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2636c2c66affSColin Finck             device, start_idx, count, constants);
2637c2c66affSColin Finck 
2638c2c66affSColin Finck     if (!constants || start_idx >= WINED3D_MAX_CONSTS_I)
2639c2c66affSColin Finck         return WINED3DERR_INVALIDCALL;
2640c2c66affSColin Finck 
2641c2c66affSColin Finck     if (count > WINED3D_MAX_CONSTS_I - start_idx)
2642c2c66affSColin Finck         count = WINED3D_MAX_CONSTS_I - start_idx;
2643c2c66affSColin Finck     memcpy(&device->update_state->ps_consts_i[start_idx], constants, count * sizeof(*constants));
2644c2c66affSColin Finck     if (TRACE_ON(d3d))
2645c2c66affSColin Finck     {
2646c2c66affSColin Finck         for (i = 0; i < count; ++i)
2647c2c66affSColin Finck             TRACE("Set ivec4 constant %u to %s.\n", start_idx + i, debug_ivec4(&constants[i]));
2648c2c66affSColin Finck     }
2649c2c66affSColin Finck 
2650c2c66affSColin Finck     if (device->recording)
2651c2c66affSColin Finck     {
2652c2c66affSColin Finck         for (i = start_idx; i < count + start_idx; ++i)
2653c2c66affSColin Finck             device->recording->changed.pixelShaderConstantsI |= (1u << i);
2654c2c66affSColin Finck     }
2655c2c66affSColin Finck     else
2656c2c66affSColin Finck     {
2657c2c66affSColin Finck         wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_PS_I, start_idx, count, constants);
2658c2c66affSColin Finck     }
2659c2c66affSColin Finck 
2660c2c66affSColin Finck     return WINED3D_OK;
2661c2c66affSColin Finck }
2662c2c66affSColin Finck 
wined3d_device_get_ps_consts_i(const struct wined3d_device * device,unsigned int start_idx,unsigned int count,struct wined3d_ivec4 * constants)2663c2c66affSColin Finck HRESULT CDECL wined3d_device_get_ps_consts_i(const struct wined3d_device *device,
2664c2c66affSColin Finck         unsigned int start_idx, unsigned int count, struct wined3d_ivec4 *constants)
2665c2c66affSColin Finck {
2666c2c66affSColin Finck     TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2667c2c66affSColin Finck             device, start_idx, count, constants);
2668c2c66affSColin Finck 
2669c2c66affSColin Finck     if (!constants || start_idx >= WINED3D_MAX_CONSTS_I)
2670c2c66affSColin Finck         return WINED3DERR_INVALIDCALL;
2671c2c66affSColin Finck 
2672c2c66affSColin Finck     if (count > WINED3D_MAX_CONSTS_I - start_idx)
2673c2c66affSColin Finck         count = WINED3D_MAX_CONSTS_I - start_idx;
2674c2c66affSColin Finck     memcpy(constants, &device->state.ps_consts_i[start_idx], count * sizeof(*constants));
2675c2c66affSColin Finck 
2676c2c66affSColin Finck     return WINED3D_OK;
2677c2c66affSColin Finck }
2678c2c66affSColin Finck 
wined3d_device_set_ps_consts_f(struct wined3d_device * device,unsigned int start_idx,unsigned int count,const struct wined3d_vec4 * constants)2679c2c66affSColin Finck HRESULT CDECL wined3d_device_set_ps_consts_f(struct wined3d_device *device,
2680c2c66affSColin Finck         unsigned int start_idx, unsigned int count, const struct wined3d_vec4 *constants)
2681c2c66affSColin Finck {
2682c2c66affSColin Finck     const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2683c2c66affSColin Finck     unsigned int i;
2684c2c66affSColin Finck 
2685c2c66affSColin Finck     TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2686c2c66affSColin Finck             device, start_idx, count, constants);
2687c2c66affSColin Finck 
2688c2c66affSColin Finck     if (!constants || start_idx >= d3d_info->limits.ps_uniform_count
2689c2c66affSColin Finck             || count > d3d_info->limits.ps_uniform_count - start_idx)
2690c2c66affSColin Finck         return WINED3DERR_INVALIDCALL;
2691c2c66affSColin Finck 
2692c2c66affSColin Finck     memcpy(&device->update_state->ps_consts_f[start_idx], constants, count * sizeof(*constants));
2693c2c66affSColin Finck     if (TRACE_ON(d3d))
2694c2c66affSColin Finck     {
2695c2c66affSColin Finck         for (i = 0; i < count; ++i)
2696c2c66affSColin Finck             TRACE("Set vec4 constant %u to %s.\n", start_idx + i, debug_vec4(&constants[i]));
2697c2c66affSColin Finck     }
2698c2c66affSColin Finck 
2699c2c66affSColin Finck     if (device->recording)
2700c2c66affSColin Finck         memset(&device->recording->changed.ps_consts_f[start_idx], 1,
2701c2c66affSColin Finck                 count * sizeof(*device->recording->changed.ps_consts_f));
2702c2c66affSColin Finck     else
2703c2c66affSColin Finck         wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_PS_F, start_idx, count, constants);
2704c2c66affSColin Finck 
2705c2c66affSColin Finck     return WINED3D_OK;
2706c2c66affSColin Finck }
2707c2c66affSColin Finck 
wined3d_device_get_ps_consts_f(const struct wined3d_device * device,unsigned int start_idx,unsigned int count,struct wined3d_vec4 * constants)2708c2c66affSColin Finck HRESULT CDECL wined3d_device_get_ps_consts_f(const struct wined3d_device *device,
2709c2c66affSColin Finck         unsigned int start_idx, unsigned int count, struct wined3d_vec4 *constants)
2710c2c66affSColin Finck {
2711c2c66affSColin Finck     const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2712c2c66affSColin Finck 
2713c2c66affSColin Finck     TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2714c2c66affSColin Finck             device, start_idx, count, constants);
2715c2c66affSColin Finck 
2716c2c66affSColin Finck     if (!constants || start_idx >= d3d_info->limits.ps_uniform_count
2717c2c66affSColin Finck             || count > d3d_info->limits.ps_uniform_count - start_idx)
2718c2c66affSColin Finck         return WINED3DERR_INVALIDCALL;
2719c2c66affSColin Finck 
2720c2c66affSColin Finck     memcpy(constants, &device->state.ps_consts_f[start_idx], count * sizeof(*constants));
2721c2c66affSColin Finck 
2722c2c66affSColin Finck     return WINED3D_OK;
2723c2c66affSColin Finck }
2724c2c66affSColin Finck 
wined3d_device_set_hull_shader(struct wined3d_device * device,struct wined3d_shader * shader)2725c2c66affSColin Finck void CDECL wined3d_device_set_hull_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2726c2c66affSColin Finck {
2727c2c66affSColin Finck     struct wined3d_shader *prev;
2728c2c66affSColin Finck 
2729c2c66affSColin Finck     TRACE("device %p, shader %p.\n", device, shader);
2730c2c66affSColin Finck 
2731c2c66affSColin Finck     prev = device->update_state->shader[WINED3D_SHADER_TYPE_HULL];
2732c2c66affSColin Finck     if (shader == prev)
2733c2c66affSColin Finck         return;
2734c2c66affSColin Finck     if (shader)
2735c2c66affSColin Finck         wined3d_shader_incref(shader);
2736c2c66affSColin Finck     device->update_state->shader[WINED3D_SHADER_TYPE_HULL] = shader;
2737c2c66affSColin Finck     wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_HULL, shader);
2738c2c66affSColin Finck     if (prev)
2739c2c66affSColin Finck         wined3d_shader_decref(prev);
2740c2c66affSColin Finck }
2741c2c66affSColin Finck 
wined3d_device_get_hull_shader(const struct wined3d_device * device)2742c2c66affSColin Finck struct wined3d_shader * CDECL wined3d_device_get_hull_shader(const struct wined3d_device *device)
2743c2c66affSColin Finck {
2744c2c66affSColin Finck     TRACE("device %p.\n", device);
2745c2c66affSColin Finck 
2746c2c66affSColin Finck     return device->state.shader[WINED3D_SHADER_TYPE_HULL];
2747c2c66affSColin Finck }
2748c2c66affSColin Finck 
wined3d_device_set_hs_cb(struct wined3d_device * device,unsigned int idx,struct wined3d_buffer * buffer)2749c2c66affSColin Finck void CDECL wined3d_device_set_hs_cb(struct wined3d_device *device, unsigned int idx, struct wined3d_buffer *buffer)
2750c2c66affSColin Finck {
2751c2c66affSColin Finck     TRACE("device %p, idx %u, buffer %p.\n", device, idx, buffer);
2752c2c66affSColin Finck 
2753c2c66affSColin Finck     wined3d_device_set_constant_buffer(device, WINED3D_SHADER_TYPE_HULL, idx, buffer);
2754c2c66affSColin Finck }
2755c2c66affSColin Finck 
wined3d_device_get_hs_cb(const struct wined3d_device * device,unsigned int idx)2756c2c66affSColin Finck struct wined3d_buffer * CDECL wined3d_device_get_hs_cb(const struct wined3d_device *device, unsigned int idx)
2757c2c66affSColin Finck {
2758c2c66affSColin Finck     TRACE("device %p, idx %u.\n", device, idx);
2759c2c66affSColin Finck 
2760c2c66affSColin Finck     return wined3d_device_get_constant_buffer(device, WINED3D_SHADER_TYPE_HULL, idx);
2761c2c66affSColin Finck }
2762c2c66affSColin Finck 
wined3d_device_set_hs_resource_view(struct wined3d_device * device,unsigned int idx,struct wined3d_shader_resource_view * view)2763c2c66affSColin Finck void CDECL wined3d_device_set_hs_resource_view(struct wined3d_device *device,
2764c2c66affSColin Finck         unsigned int idx, struct wined3d_shader_resource_view *view)
2765c2c66affSColin Finck {
2766c2c66affSColin Finck     TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2767c2c66affSColin Finck 
2768c2c66affSColin Finck     wined3d_device_set_shader_resource_view(device, WINED3D_SHADER_TYPE_HULL, idx, view);
2769c2c66affSColin Finck }
2770c2c66affSColin Finck 
wined3d_device_get_hs_resource_view(const struct wined3d_device * device,unsigned int idx)2771c2c66affSColin Finck struct wined3d_shader_resource_view * CDECL wined3d_device_get_hs_resource_view(const struct wined3d_device *device,
2772c2c66affSColin Finck         unsigned int idx)
2773c2c66affSColin Finck {
2774c2c66affSColin Finck     TRACE("device %p, idx %u.\n", device, idx);
2775c2c66affSColin Finck 
2776c2c66affSColin Finck     return wined3d_device_get_shader_resource_view(device, WINED3D_SHADER_TYPE_HULL, idx);
2777c2c66affSColin Finck }
2778c2c66affSColin Finck 
wined3d_device_set_hs_sampler(struct wined3d_device * device,unsigned int idx,struct wined3d_sampler * sampler)2779c2c66affSColin Finck void CDECL wined3d_device_set_hs_sampler(struct wined3d_device *device,
2780c2c66affSColin Finck         unsigned int idx, struct wined3d_sampler *sampler)
2781c2c66affSColin Finck {
2782c2c66affSColin Finck     TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2783c2c66affSColin Finck 
2784c2c66affSColin Finck     wined3d_device_set_sampler(device, WINED3D_SHADER_TYPE_HULL, idx, sampler);
2785c2c66affSColin Finck }
2786c2c66affSColin Finck 
wined3d_device_get_hs_sampler(const struct wined3d_device * device,unsigned int idx)2787c2c66affSColin Finck struct wined3d_sampler * CDECL wined3d_device_get_hs_sampler(const struct wined3d_device *device, unsigned int idx)
2788c2c66affSColin Finck {
2789c2c66affSColin Finck     TRACE("device %p, idx %u.\n", device, idx);
2790c2c66affSColin Finck 
2791c2c66affSColin Finck     return wined3d_device_get_sampler(device, WINED3D_SHADER_TYPE_HULL, idx);
2792c2c66affSColin Finck }
2793c2c66affSColin Finck 
wined3d_device_set_domain_shader(struct wined3d_device * device,struct wined3d_shader * shader)2794c2c66affSColin Finck void CDECL wined3d_device_set_domain_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2795c2c66affSColin Finck {
2796c2c66affSColin Finck     struct wined3d_shader *prev;
2797c2c66affSColin Finck 
2798c2c66affSColin Finck     TRACE("device %p, shader %p.\n", device, shader);
2799c2c66affSColin Finck 
2800c2c66affSColin Finck     prev = device->update_state->shader[WINED3D_SHADER_TYPE_DOMAIN];
2801c2c66affSColin Finck     if (shader == prev)
2802c2c66affSColin Finck         return;
2803c2c66affSColin Finck     if (shader)
2804c2c66affSColin Finck         wined3d_shader_incref(shader);
2805c2c66affSColin Finck     device->update_state->shader[WINED3D_SHADER_TYPE_DOMAIN] = shader;
2806c2c66affSColin Finck     wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_DOMAIN, shader);
2807c2c66affSColin Finck     if (prev)
2808c2c66affSColin Finck         wined3d_shader_decref(prev);
2809c2c66affSColin Finck }
2810c2c66affSColin Finck 
wined3d_device_get_domain_shader(const struct wined3d_device * device)2811c2c66affSColin Finck struct wined3d_shader * CDECL wined3d_device_get_domain_shader(const struct wined3d_device *device)
2812c2c66affSColin Finck {
2813c2c66affSColin Finck     TRACE("device %p.\n", device);
2814c2c66affSColin Finck 
2815c2c66affSColin Finck     return device->state.shader[WINED3D_SHADER_TYPE_DOMAIN];
2816c2c66affSColin Finck }
2817c2c66affSColin Finck 
wined3d_device_set_ds_cb(struct wined3d_device * device,unsigned int idx,struct wined3d_buffer * buffer)2818c2c66affSColin Finck void CDECL wined3d_device_set_ds_cb(struct wined3d_device *device, unsigned int idx, struct wined3d_buffer *buffer)
2819c2c66affSColin Finck {
2820c2c66affSColin Finck     TRACE("device %p, idx %u, buffer %p.\n", device, idx, buffer);
2821c2c66affSColin Finck 
2822c2c66affSColin Finck     wined3d_device_set_constant_buffer(device, WINED3D_SHADER_TYPE_DOMAIN, idx, buffer);
2823c2c66affSColin Finck }
2824c2c66affSColin Finck 
wined3d_device_get_ds_cb(const struct wined3d_device * device,unsigned int idx)2825c2c66affSColin Finck struct wined3d_buffer * CDECL wined3d_device_get_ds_cb(const struct wined3d_device *device, unsigned int idx)
2826c2c66affSColin Finck {
2827c2c66affSColin Finck     TRACE("device %p, idx %u.\n", device, idx);
2828c2c66affSColin Finck 
2829c2c66affSColin Finck     return wined3d_device_get_constant_buffer(device, WINED3D_SHADER_TYPE_DOMAIN, idx);
2830c2c66affSColin Finck }
2831c2c66affSColin Finck 
wined3d_device_set_ds_resource_view(struct wined3d_device * device,unsigned int idx,struct wined3d_shader_resource_view * view)2832c2c66affSColin Finck void CDECL wined3d_device_set_ds_resource_view(struct wined3d_device *device,
2833c2c66affSColin Finck         unsigned int idx, struct wined3d_shader_resource_view *view)
2834c2c66affSColin Finck {
2835c2c66affSColin Finck     TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2836c2c66affSColin Finck 
2837c2c66affSColin Finck     wined3d_device_set_shader_resource_view(device, WINED3D_SHADER_TYPE_DOMAIN, idx, view);
2838c2c66affSColin Finck }
2839c2c66affSColin Finck 
wined3d_device_get_ds_resource_view(const struct wined3d_device * device,unsigned int idx)2840c2c66affSColin Finck struct wined3d_shader_resource_view * CDECL wined3d_device_get_ds_resource_view(const struct wined3d_device *device,
2841c2c66affSColin Finck         unsigned int idx)
2842c2c66affSColin Finck {
2843c2c66affSColin Finck     TRACE("device %p, idx %u.\n", device, idx);
2844c2c66affSColin Finck 
2845c2c66affSColin Finck     return wined3d_device_get_shader_resource_view(device, WINED3D_SHADER_TYPE_DOMAIN, idx);
2846c2c66affSColin Finck }
2847c2c66affSColin Finck 
wined3d_device_set_ds_sampler(struct wined3d_device * device,unsigned int idx,struct wined3d_sampler * sampler)2848c2c66affSColin Finck void CDECL wined3d_device_set_ds_sampler(struct wined3d_device *device,
2849c2c66affSColin Finck         unsigned int idx, struct wined3d_sampler *sampler)
2850c2c66affSColin Finck {
2851c2c66affSColin Finck     TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2852c2c66affSColin Finck 
2853c2c66affSColin Finck     wined3d_device_set_sampler(device, WINED3D_SHADER_TYPE_DOMAIN, idx, sampler);
2854c2c66affSColin Finck }
2855c2c66affSColin Finck 
wined3d_device_get_ds_sampler(const struct wined3d_device * device,unsigned int idx)2856c2c66affSColin Finck struct wined3d_sampler * CDECL wined3d_device_get_ds_sampler(const struct wined3d_device *device, unsigned int idx)
2857c2c66affSColin Finck {
2858c2c66affSColin Finck     TRACE("device %p, idx %u.\n", device, idx);
2859c2c66affSColin Finck 
2860c2c66affSColin Finck     return wined3d_device_get_sampler(device, WINED3D_SHADER_TYPE_DOMAIN, idx);
2861c2c66affSColin Finck }
2862c2c66affSColin Finck 
wined3d_device_set_geometry_shader(struct wined3d_device * device,struct wined3d_shader * shader)2863c2c66affSColin Finck void CDECL wined3d_device_set_geometry_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2864c2c66affSColin Finck {
2865c2c66affSColin Finck     struct wined3d_shader *prev = device->update_state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
2866c2c66affSColin Finck 
2867c2c66affSColin Finck     TRACE("device %p, shader %p.\n", device, shader);
2868c2c66affSColin Finck 
2869c2c66affSColin Finck     if (device->recording || shader == prev)
2870c2c66affSColin Finck         return;
2871c2c66affSColin Finck     if (shader)
2872c2c66affSColin Finck         wined3d_shader_incref(shader);
2873c2c66affSColin Finck     device->update_state->shader[WINED3D_SHADER_TYPE_GEOMETRY] = shader;
2874c2c66affSColin Finck     wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_GEOMETRY, shader);
2875c2c66affSColin Finck     if (prev)
2876c2c66affSColin Finck         wined3d_shader_decref(prev);
2877c2c66affSColin Finck }
2878c2c66affSColin Finck 
wined3d_device_get_geometry_shader(const struct wined3d_device * device)2879c2c66affSColin Finck struct wined3d_shader * CDECL wined3d_device_get_geometry_shader(const struct wined3d_device *device)
2880c2c66affSColin Finck {
2881c2c66affSColin Finck     TRACE("device %p.\n", device);
2882c2c66affSColin Finck 
2883c2c66affSColin Finck     return device->state.shader[WINED3D_SHADER_TYPE_GEOMETRY];
2884c2c66affSColin Finck }
2885c2c66affSColin Finck 
wined3d_device_set_gs_cb(struct wined3d_device * device,UINT idx,struct wined3d_buffer * buffer)2886c2c66affSColin Finck void CDECL wined3d_device_set_gs_cb(struct wined3d_device *device, UINT idx, struct wined3d_buffer *buffer)
2887c2c66affSColin Finck {
2888c2c66affSColin Finck     TRACE("device %p, idx %u, buffer %p.\n", device, idx, buffer);
2889c2c66affSColin Finck 
2890c2c66affSColin Finck     wined3d_device_set_constant_buffer(device, WINED3D_SHADER_TYPE_GEOMETRY, idx, buffer);
2891c2c66affSColin Finck }
2892c2c66affSColin Finck 
wined3d_device_get_gs_cb(const struct wined3d_device * device,UINT idx)2893c2c66affSColin Finck struct wined3d_buffer * CDECL wined3d_device_get_gs_cb(const struct wined3d_device *device, UINT idx)
2894c2c66affSColin Finck {
2895c2c66affSColin Finck     TRACE("device %p, idx %u.\n", device, idx);
2896c2c66affSColin Finck 
2897c2c66affSColin Finck     return wined3d_device_get_constant_buffer(device, WINED3D_SHADER_TYPE_GEOMETRY, idx);
2898c2c66affSColin Finck }
2899c2c66affSColin Finck 
wined3d_device_set_gs_resource_view(struct wined3d_device * device,UINT idx,struct wined3d_shader_resource_view * view)2900c2c66affSColin Finck void CDECL wined3d_device_set_gs_resource_view(struct wined3d_device *device,
2901c2c66affSColin Finck         UINT idx, struct wined3d_shader_resource_view *view)
2902c2c66affSColin Finck {
2903c2c66affSColin Finck     TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2904c2c66affSColin Finck 
2905c2c66affSColin Finck     wined3d_device_set_shader_resource_view(device, WINED3D_SHADER_TYPE_GEOMETRY, idx, view);
2906c2c66affSColin Finck }
2907c2c66affSColin Finck 
wined3d_device_get_gs_resource_view(const struct wined3d_device * device,UINT idx)2908c2c66affSColin Finck struct wined3d_shader_resource_view * CDECL wined3d_device_get_gs_resource_view(const struct wined3d_device *device,
2909c2c66affSColin Finck         UINT idx)
2910c2c66affSColin Finck {
2911c2c66affSColin Finck     TRACE("device %p, idx %u.\n", device, idx);
2912c2c66affSColin Finck 
2913c2c66affSColin Finck     return wined3d_device_get_shader_resource_view(device, WINED3D_SHADER_TYPE_GEOMETRY, idx);
2914c2c66affSColin Finck }
2915c2c66affSColin Finck 
wined3d_device_set_gs_sampler(struct wined3d_device * device,UINT idx,struct wined3d_sampler * sampler)2916c2c66affSColin Finck void CDECL wined3d_device_set_gs_sampler(struct wined3d_device *device, UINT idx, struct wined3d_sampler *sampler)
2917c2c66affSColin Finck {
2918c2c66affSColin Finck     TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2919c2c66affSColin Finck 
2920c2c66affSColin Finck     wined3d_device_set_sampler(device, WINED3D_SHADER_TYPE_GEOMETRY, idx, sampler);
2921c2c66affSColin Finck }
2922c2c66affSColin Finck 
wined3d_device_get_gs_sampler(const struct wined3d_device * device,UINT idx)2923c2c66affSColin Finck struct wined3d_sampler * CDECL wined3d_device_get_gs_sampler(const struct wined3d_device *device, UINT idx)
2924c2c66affSColin Finck {
2925c2c66affSColin Finck     TRACE("device %p, idx %u.\n", device, idx);
2926c2c66affSColin Finck 
2927c2c66affSColin Finck     return wined3d_device_get_sampler(device, WINED3D_SHADER_TYPE_GEOMETRY, idx);
2928c2c66affSColin Finck }
2929c2c66affSColin Finck 
wined3d_device_set_compute_shader(struct wined3d_device * device,struct wined3d_shader * shader)2930c2c66affSColin Finck void CDECL wined3d_device_set_compute_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2931c2c66affSColin Finck {
2932c2c66affSColin Finck     struct wined3d_shader *prev;
2933c2c66affSColin Finck 
2934c2c66affSColin Finck     TRACE("device %p, shader %p.\n", device, shader);
2935c2c66affSColin Finck 
2936c2c66affSColin Finck     prev = device->update_state->shader[WINED3D_SHADER_TYPE_COMPUTE];
2937c2c66affSColin Finck     if (device->recording || shader == prev)
2938c2c66affSColin Finck         return;
2939c2c66affSColin Finck     if (shader)
2940c2c66affSColin Finck         wined3d_shader_incref(shader);
2941c2c66affSColin Finck     device->update_state->shader[WINED3D_SHADER_TYPE_COMPUTE] = shader;
2942c2c66affSColin Finck     wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_COMPUTE, shader);
2943c2c66affSColin Finck     if (prev)
2944c2c66affSColin Finck         wined3d_shader_decref(prev);
2945c2c66affSColin Finck }
2946c2c66affSColin Finck 
wined3d_device_get_compute_shader(const struct wined3d_device * device)2947c2c66affSColin Finck struct wined3d_shader * CDECL wined3d_device_get_compute_shader(const struct wined3d_device *device)
2948c2c66affSColin Finck {
2949c2c66affSColin Finck     TRACE("device %p.\n", device);
2950c2c66affSColin Finck 
2951c2c66affSColin Finck     return device->state.shader[WINED3D_SHADER_TYPE_COMPUTE];
2952c2c66affSColin Finck }
2953c2c66affSColin Finck 
wined3d_device_set_cs_cb(struct wined3d_device * device,unsigned int idx,struct wined3d_buffer * buffer)2954c2c66affSColin Finck void CDECL wined3d_device_set_cs_cb(struct wined3d_device *device, unsigned int idx, struct wined3d_buffer *buffer)
2955c2c66affSColin Finck {
2956c2c66affSColin Finck     TRACE("device %p, idx %u, buffer %p.\n", device, idx, buffer);
2957c2c66affSColin Finck 
2958c2c66affSColin Finck     wined3d_device_set_constant_buffer(device, WINED3D_SHADER_TYPE_COMPUTE, idx, buffer);
2959c2c66affSColin Finck }
2960c2c66affSColin Finck 
wined3d_device_get_cs_cb(const struct wined3d_device * device,unsigned int idx)2961c2c66affSColin Finck struct wined3d_buffer * CDECL wined3d_device_get_cs_cb(const struct wined3d_device *device, unsigned int idx)
2962c2c66affSColin Finck {
2963c2c66affSColin Finck     TRACE("device %p, idx %u.\n", device, idx);
2964c2c66affSColin Finck 
2965c2c66affSColin Finck     return wined3d_device_get_constant_buffer(device, WINED3D_SHADER_TYPE_COMPUTE, idx);
2966c2c66affSColin Finck }
2967c2c66affSColin Finck 
wined3d_device_set_cs_resource_view(struct wined3d_device * device,unsigned int idx,struct wined3d_shader_resource_view * view)2968c2c66affSColin Finck void CDECL wined3d_device_set_cs_resource_view(struct wined3d_device *device,
2969c2c66affSColin Finck         unsigned int idx, struct wined3d_shader_resource_view *view)
2970c2c66affSColin Finck {
2971c2c66affSColin Finck     TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2972c2c66affSColin Finck 
2973c2c66affSColin Finck     wined3d_device_set_shader_resource_view(device, WINED3D_SHADER_TYPE_COMPUTE, idx, view);
2974c2c66affSColin Finck }
2975c2c66affSColin Finck 
wined3d_device_get_cs_resource_view(const struct wined3d_device * device,unsigned int idx)2976c2c66affSColin Finck struct wined3d_shader_resource_view * CDECL wined3d_device_get_cs_resource_view(const struct wined3d_device *device,
2977c2c66affSColin Finck         unsigned int idx)
2978c2c66affSColin Finck {
2979c2c66affSColin Finck     TRACE("device %p, idx %u.\n", device, idx);
2980c2c66affSColin Finck 
2981c2c66affSColin Finck     return wined3d_device_get_shader_resource_view(device, WINED3D_SHADER_TYPE_COMPUTE, idx);
2982c2c66affSColin Finck }
2983c2c66affSColin Finck 
wined3d_device_set_cs_sampler(struct wined3d_device * device,unsigned int idx,struct wined3d_sampler * sampler)2984c2c66affSColin Finck void CDECL wined3d_device_set_cs_sampler(struct wined3d_device *device,
2985c2c66affSColin Finck         unsigned int idx, struct wined3d_sampler *sampler)
2986c2c66affSColin Finck {
2987c2c66affSColin Finck     TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2988c2c66affSColin Finck 
2989c2c66affSColin Finck     wined3d_device_set_sampler(device, WINED3D_SHADER_TYPE_COMPUTE, idx, sampler);
2990c2c66affSColin Finck }
2991c2c66affSColin Finck 
wined3d_device_get_cs_sampler(const struct wined3d_device * device,unsigned int idx)2992c2c66affSColin Finck struct wined3d_sampler * CDECL wined3d_device_get_cs_sampler(const struct wined3d_device *device, unsigned int idx)
2993c2c66affSColin Finck {
2994c2c66affSColin Finck     TRACE("device %p, idx %u.\n", device, idx);
2995c2c66affSColin Finck 
2996c2c66affSColin Finck     return wined3d_device_get_sampler(device, WINED3D_SHADER_TYPE_COMPUTE, idx);
2997c2c66affSColin Finck }
2998c2c66affSColin Finck 
wined3d_device_set_pipeline_unordered_access_view(struct wined3d_device * device,enum wined3d_pipeline pipeline,unsigned int idx,struct wined3d_unordered_access_view * uav,unsigned int initial_count)2999c2c66affSColin Finck static void wined3d_device_set_pipeline_unordered_access_view(struct wined3d_device *device,
3000c2c66affSColin Finck         enum wined3d_pipeline pipeline, unsigned int idx, struct wined3d_unordered_access_view *uav,
3001c2c66affSColin Finck         unsigned int initial_count)
3002c2c66affSColin Finck {
3003c2c66affSColin Finck     struct wined3d_unordered_access_view *prev;
3004c2c66affSColin Finck 
3005c2c66affSColin Finck     if (idx >= MAX_UNORDERED_ACCESS_VIEWS)
3006c2c66affSColin Finck     {
3007c2c66affSColin Finck         WARN("Invalid UAV index %u.\n", idx);
3008c2c66affSColin Finck         return;
3009c2c66affSColin Finck     }
3010c2c66affSColin Finck 
3011c2c66affSColin Finck     prev = device->update_state->unordered_access_view[pipeline][idx];
3012c2c66affSColin Finck     if (uav == prev && initial_count == ~0u)
3013c2c66affSColin Finck         return;
3014c2c66affSColin Finck 
3015c2c66affSColin Finck     if (uav)
3016c2c66affSColin Finck         wined3d_unordered_access_view_incref(uav);
3017c2c66affSColin Finck     device->update_state->unordered_access_view[pipeline][idx] = uav;
3018c2c66affSColin Finck     if (!device->recording)
3019c2c66affSColin Finck         wined3d_cs_emit_set_unordered_access_view(device->cs, pipeline, idx, uav, initial_count);
3020c2c66affSColin Finck     if (prev)
3021c2c66affSColin Finck         wined3d_unordered_access_view_decref(prev);
3022c2c66affSColin Finck }
3023c2c66affSColin Finck 
wined3d_device_get_pipeline_unordered_access_view(const struct wined3d_device * device,enum wined3d_pipeline pipeline,unsigned int idx)3024c2c66affSColin Finck static struct wined3d_unordered_access_view *wined3d_device_get_pipeline_unordered_access_view(
3025c2c66affSColin Finck         const struct wined3d_device *device, enum wined3d_pipeline pipeline, unsigned int idx)
3026c2c66affSColin Finck {
3027c2c66affSColin Finck     if (idx >= MAX_UNORDERED_ACCESS_VIEWS)
3028c2c66affSColin Finck     {
3029c2c66affSColin Finck         WARN("Invalid UAV index %u.\n", idx);
3030c2c66affSColin Finck         return NULL;
3031c2c66affSColin Finck     }
3032c2c66affSColin Finck 
3033c2c66affSColin Finck     return device->state.unordered_access_view[pipeline][idx];
3034c2c66affSColin Finck }
3035c2c66affSColin Finck 
wined3d_device_set_cs_uav(struct wined3d_device * device,unsigned int idx,struct wined3d_unordered_access_view * uav,unsigned int initial_count)3036c2c66affSColin Finck void CDECL wined3d_device_set_cs_uav(struct wined3d_device *device, unsigned int idx,
3037c2c66affSColin Finck         struct wined3d_unordered_access_view *uav, unsigned int initial_count)
3038c2c66affSColin Finck {
3039c2c66affSColin Finck     TRACE("device %p, idx %u, uav %p, initial_count %#x.\n", device, idx, uav, initial_count);
3040c2c66affSColin Finck 
3041c2c66affSColin Finck     wined3d_device_set_pipeline_unordered_access_view(device, WINED3D_PIPELINE_COMPUTE, idx, uav, initial_count);
3042c2c66affSColin Finck }
3043c2c66affSColin Finck 
wined3d_device_get_cs_uav(const struct wined3d_device * device,unsigned int idx)3044c2c66affSColin Finck struct wined3d_unordered_access_view * CDECL wined3d_device_get_cs_uav(const struct wined3d_device *device,
3045c2c66affSColin Finck         unsigned int idx)
3046c2c66affSColin Finck {
3047c2c66affSColin Finck     TRACE("device %p, idx %u.\n", device, idx);
3048c2c66affSColin Finck 
3049c2c66affSColin Finck     return wined3d_device_get_pipeline_unordered_access_view(device, WINED3D_PIPELINE_COMPUTE, idx);
3050c2c66affSColin Finck }
3051c2c66affSColin Finck 
wined3d_device_set_unordered_access_view(struct wined3d_device * device,unsigned int idx,struct wined3d_unordered_access_view * uav,unsigned int initial_count)3052c2c66affSColin Finck void CDECL wined3d_device_set_unordered_access_view(struct wined3d_device *device,
3053c2c66affSColin Finck         unsigned int idx, struct wined3d_unordered_access_view *uav, unsigned int initial_count)
3054c2c66affSColin Finck {
3055c2c66affSColin Finck     TRACE("device %p, idx %u, uav %p, initial_count %#x.\n", device, idx, uav, initial_count);
3056c2c66affSColin Finck 
3057c2c66affSColin Finck     wined3d_device_set_pipeline_unordered_access_view(device, WINED3D_PIPELINE_GRAPHICS, idx, uav, initial_count);
3058c2c66affSColin Finck }
3059c2c66affSColin Finck 
wined3d_device_get_unordered_access_view(const struct wined3d_device * device,unsigned int idx)3060c2c66affSColin Finck struct wined3d_unordered_access_view * CDECL wined3d_device_get_unordered_access_view(
3061c2c66affSColin Finck         const struct wined3d_device *device, unsigned int idx)
3062c2c66affSColin Finck {
3063c2c66affSColin Finck     TRACE("device %p, idx %u.\n", device, idx);
3064c2c66affSColin Finck 
3065c2c66affSColin Finck     return wined3d_device_get_pipeline_unordered_access_view(device, WINED3D_PIPELINE_GRAPHICS, idx);
3066c2c66affSColin Finck }
3067c2c66affSColin Finck 
3068c2c66affSColin Finck /* Context activation is done by the caller. */
3069c2c66affSColin Finck #define copy_and_next(dest, src, size) memcpy(dest, src, size); dest += (size)
process_vertices_strided(const struct wined3d_device * device,DWORD dwDestIndex,DWORD dwCount,const struct wined3d_stream_info * stream_info,struct wined3d_buffer * dest,DWORD flags,DWORD DestFVF)3070c2c66affSColin Finck static HRESULT process_vertices_strided(const struct wined3d_device *device, DWORD dwDestIndex, DWORD dwCount,
3071c2c66affSColin Finck         const struct wined3d_stream_info *stream_info, struct wined3d_buffer *dest, DWORD flags,
3072c2c66affSColin Finck         DWORD DestFVF)
3073c2c66affSColin Finck {
3074c2c66affSColin Finck     struct wined3d_matrix mat, proj_mat, view_mat, world_mat;
3075c2c66affSColin Finck     struct wined3d_map_desc map_desc;
3076c2c66affSColin Finck     struct wined3d_box box = {0};
3077c2c66affSColin Finck     struct wined3d_viewport vp;
3078c2c66affSColin Finck     UINT vertex_size;
3079c2c66affSColin Finck     unsigned int i;
3080c2c66affSColin Finck     BYTE *dest_ptr;
3081c2c66affSColin Finck     BOOL doClip;
3082c2c66affSColin Finck     DWORD numTextures;
3083c2c66affSColin Finck     HRESULT hr;
3084c2c66affSColin Finck 
3085c2c66affSColin Finck     if (stream_info->use_map & (1u << WINED3D_FFP_NORMAL))
3086c2c66affSColin Finck     {
3087c2c66affSColin Finck         WARN(" lighting state not saved yet... Some strange stuff may happen !\n");
3088c2c66affSColin Finck     }
3089c2c66affSColin Finck 
3090c2c66affSColin Finck     if (!(stream_info->use_map & (1u << WINED3D_FFP_POSITION)))
3091c2c66affSColin Finck     {
3092c2c66affSColin Finck         ERR("Source has no position mask\n");
3093c2c66affSColin Finck         return WINED3DERR_INVALIDCALL;
3094c2c66affSColin Finck     }
3095c2c66affSColin Finck 
3096c2c66affSColin Finck     if (device->state.render_states[WINED3D_RS_CLIPPING])
3097c2c66affSColin Finck     {
3098c2c66affSColin Finck         static BOOL warned = FALSE;
3099c2c66affSColin Finck         /*
3100c2c66affSColin Finck          * The clipping code is not quite correct. Some things need
3101c2c66affSColin Finck          * to be checked against IDirect3DDevice3 (!), d3d8 and d3d9,
3102c2c66affSColin Finck          * so disable clipping for now.
3103c2c66affSColin Finck          * (The graphics in Half-Life are broken, and my processvertices
3104c2c66affSColin Finck          *  test crashes with IDirect3DDevice3)
3105c2c66affSColin Finck         doClip = TRUE;
3106c2c66affSColin Finck          */
3107c2c66affSColin Finck         doClip = FALSE;
3108c2c66affSColin Finck         if(!warned) {
3109c2c66affSColin Finck            warned = TRUE;
3110c2c66affSColin Finck            FIXME("Clipping is broken and disabled for now\n");
3111c2c66affSColin Finck         }
3112c2c66affSColin Finck     }
3113c2c66affSColin Finck     else
3114c2c66affSColin Finck         doClip = FALSE;
3115c2c66affSColin Finck 
3116c2c66affSColin Finck     vertex_size = get_flexible_vertex_size(DestFVF);
3117c2c66affSColin Finck     box.left = dwDestIndex * vertex_size;
3118c2c66affSColin Finck     box.right = box.left + dwCount * vertex_size;
31199987f029SAmine Khaldi     if (FAILED(hr = wined3d_resource_map(&dest->resource, 0, &map_desc, &box, WINED3D_MAP_WRITE)))
3120c2c66affSColin Finck     {
3121c2c66affSColin Finck         WARN("Failed to map buffer, hr %#x.\n", hr);
3122c2c66affSColin Finck         return hr;
3123c2c66affSColin Finck     }
3124c2c66affSColin Finck     dest_ptr = map_desc.data;
3125c2c66affSColin Finck 
3126c2c66affSColin Finck     wined3d_device_get_transform(device, WINED3D_TS_VIEW, &view_mat);
3127c2c66affSColin Finck     wined3d_device_get_transform(device, WINED3D_TS_PROJECTION, &proj_mat);
3128c2c66affSColin Finck     wined3d_device_get_transform(device, WINED3D_TS_WORLD_MATRIX(0), &world_mat);
3129c2c66affSColin Finck 
3130c2c66affSColin Finck     TRACE("View mat:\n");
3131c2c66affSColin Finck     TRACE("%.8e %.8e %.8e %.8e\n", view_mat._11, view_mat._12, view_mat._13, view_mat._14);
3132c2c66affSColin Finck     TRACE("%.8e %.8e %.8e %.8e\n", view_mat._21, view_mat._22, view_mat._23, view_mat._24);
3133c2c66affSColin Finck     TRACE("%.8e %.8e %.8e %.8e\n", view_mat._31, view_mat._32, view_mat._33, view_mat._34);
3134c2c66affSColin Finck     TRACE("%.8e %.8e %.8e %.8e\n", view_mat._41, view_mat._42, view_mat._43, view_mat._44);
3135c2c66affSColin Finck 
3136c2c66affSColin Finck     TRACE("Proj mat:\n");
3137c2c66affSColin Finck     TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._11, proj_mat._12, proj_mat._13, proj_mat._14);
3138c2c66affSColin Finck     TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._21, proj_mat._22, proj_mat._23, proj_mat._24);
3139c2c66affSColin Finck     TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._31, proj_mat._32, proj_mat._33, proj_mat._34);
3140c2c66affSColin Finck     TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._41, proj_mat._42, proj_mat._43, proj_mat._44);
3141c2c66affSColin Finck 
3142c2c66affSColin Finck     TRACE("World mat:\n");
3143c2c66affSColin Finck     TRACE("%.8e %.8e %.8e %.8e\n", world_mat._11, world_mat._12, world_mat._13, world_mat._14);
3144c2c66affSColin Finck     TRACE("%.8e %.8e %.8e %.8e\n", world_mat._21, world_mat._22, world_mat._23, world_mat._24);
3145c2c66affSColin Finck     TRACE("%.8e %.8e %.8e %.8e\n", world_mat._31, world_mat._32, world_mat._33, world_mat._34);
3146c2c66affSColin Finck     TRACE("%.8e %.8e %.8e %.8e\n", world_mat._41, world_mat._42, world_mat._43, world_mat._44);
3147c2c66affSColin Finck 
3148c2c66affSColin Finck     /* Get the viewport */
3149*81cffd76SJoachim Henze     wined3d_device_get_viewport(device, &vp);
3150c2c66affSColin Finck     TRACE("viewport  x %.8e, y %.8e, width %.8e, height %.8e, min_z %.8e, max_z %.8e.\n",
3151c2c66affSColin Finck           vp.x, vp.y, vp.width, vp.height, vp.min_z, vp.max_z);
3152c2c66affSColin Finck 
3153c2c66affSColin Finck     multiply_matrix(&mat,&view_mat,&world_mat);
3154c2c66affSColin Finck     multiply_matrix(&mat,&proj_mat,&mat);
3155c2c66affSColin Finck 
3156c2c66affSColin Finck     numTextures = (DestFVF & WINED3DFVF_TEXCOUNT_MASK) >> WINED3DFVF_TEXCOUNT_SHIFT;
3157c2c66affSColin Finck 
3158c2c66affSColin Finck     for (i = 0; i < dwCount; i+= 1) {
3159c2c66affSColin Finck         unsigned int tex_index;
3160c2c66affSColin Finck 
3161c2c66affSColin Finck         if ( ((DestFVF & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZ ) ||
3162c2c66affSColin Finck              ((DestFVF & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZRHW ) ) {
3163c2c66affSColin Finck             /* The position first */
3164c2c66affSColin Finck             const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_POSITION];
3165c2c66affSColin Finck             const float *p = (const float *)(element->data.addr + i * element->stride);
3166c2c66affSColin Finck             float x, y, z, rhw;
3167c2c66affSColin Finck             TRACE("In: ( %06.2f %06.2f %06.2f )\n", p[0], p[1], p[2]);
3168c2c66affSColin Finck 
3169c2c66affSColin Finck             /* Multiplication with world, view and projection matrix. */
3170c2c66affSColin Finck             x   = (p[0] * mat._11) + (p[1] * mat._21) + (p[2] * mat._31) + mat._41;
3171c2c66affSColin Finck             y   = (p[0] * mat._12) + (p[1] * mat._22) + (p[2] * mat._32) + mat._42;
3172c2c66affSColin Finck             z   = (p[0] * mat._13) + (p[1] * mat._23) + (p[2] * mat._33) + mat._43;
3173c2c66affSColin Finck             rhw = (p[0] * mat._14) + (p[1] * mat._24) + (p[2] * mat._34) + mat._44;
3174c2c66affSColin Finck 
3175c2c66affSColin Finck             TRACE("x=%f y=%f z=%f rhw=%f\n", x, y, z, rhw);
3176c2c66affSColin Finck 
3177c2c66affSColin Finck             /* WARNING: The following things are taken from d3d7 and were not yet checked
3178c2c66affSColin Finck              * against d3d8 or d3d9!
3179c2c66affSColin Finck              */
3180c2c66affSColin Finck 
3181c2c66affSColin Finck             /* Clipping conditions: From msdn
3182c2c66affSColin Finck              *
3183c2c66affSColin Finck              * A vertex is clipped if it does not match the following requirements
3184c2c66affSColin Finck              * -rhw < x <= rhw
3185c2c66affSColin Finck              * -rhw < y <= rhw
3186c2c66affSColin Finck              *    0 < z <= rhw
3187c2c66affSColin Finck              *    0 < rhw ( Not in d3d7, but tested in d3d7)
3188c2c66affSColin Finck              *
3189c2c66affSColin Finck              * If clipping is on is determined by the D3DVOP_CLIP flag in D3D7, and
3190c2c66affSColin Finck              * by the D3DRS_CLIPPING in D3D9(according to the msdn, not checked)
3191c2c66affSColin Finck              *
3192c2c66affSColin Finck              */
3193c2c66affSColin Finck 
3194c2c66affSColin Finck             if( !doClip ||
3195c2c66affSColin Finck                 ( (-rhw -eps < x) && (-rhw -eps < y) && ( -eps < z) &&
3196c2c66affSColin Finck                   (x <= rhw + eps) && (y <= rhw + eps ) && (z <= rhw + eps) &&
3197c2c66affSColin Finck                   ( rhw > eps ) ) ) {
3198c2c66affSColin Finck 
3199c2c66affSColin Finck                 /* "Normal" viewport transformation (not clipped)
3200c2c66affSColin Finck                  * 1) The values are divided by rhw
3201c2c66affSColin Finck                  * 2) The y axis is negative, so multiply it with -1
3202c2c66affSColin Finck                  * 3) Screen coordinates go from -(Width/2) to +(Width/2) and
3203c2c66affSColin Finck                  *    -(Height/2) to +(Height/2). The z range is MinZ to MaxZ
3204c2c66affSColin Finck                  * 4) Multiply x with Width/2 and add Width/2
3205c2c66affSColin Finck                  * 5) The same for the height
3206c2c66affSColin Finck                  * 6) Add the viewpoint X and Y to the 2D coordinates and
3207c2c66affSColin Finck                  *    The minimum Z value to z
3208c2c66affSColin Finck                  * 7) rhw = 1 / rhw Reciprocal of Homogeneous W....
3209c2c66affSColin Finck                  *
3210c2c66affSColin Finck                  * Well, basically it's simply a linear transformation into viewport
3211c2c66affSColin Finck                  * coordinates
3212c2c66affSColin Finck                  */
3213c2c66affSColin Finck 
3214c2c66affSColin Finck                 x /= rhw;
3215c2c66affSColin Finck                 y /= rhw;
3216c2c66affSColin Finck                 z /= rhw;
3217c2c66affSColin Finck 
3218c2c66affSColin Finck                 y *= -1;
3219c2c66affSColin Finck 
3220c2c66affSColin Finck                 x *= vp.width / 2;
3221c2c66affSColin Finck                 y *= vp.height / 2;
3222c2c66affSColin Finck                 z *= vp.max_z - vp.min_z;
3223c2c66affSColin Finck 
3224c2c66affSColin Finck                 x += vp.width / 2 + vp.x;
3225c2c66affSColin Finck                 y += vp.height / 2 + vp.y;
3226c2c66affSColin Finck                 z += vp.min_z;
3227c2c66affSColin Finck 
3228c2c66affSColin Finck                 rhw = 1 / rhw;
3229c2c66affSColin Finck             } else {
3230c2c66affSColin Finck                 /* That vertex got clipped
3231c2c66affSColin Finck                  * Contrary to OpenGL it is not dropped completely, it just
3232c2c66affSColin Finck                  * undergoes a different calculation.
3233c2c66affSColin Finck                  */
3234c2c66affSColin Finck                 TRACE("Vertex got clipped\n");
3235c2c66affSColin Finck                 x += rhw;
3236c2c66affSColin Finck                 y += rhw;
3237c2c66affSColin Finck 
3238c2c66affSColin Finck                 x  /= 2;
3239c2c66affSColin Finck                 y  /= 2;
3240c2c66affSColin Finck 
3241c2c66affSColin Finck                 /* Msdn mentions that Direct3D9 keeps a list of clipped vertices
3242c2c66affSColin Finck                  * outside of the main vertex buffer memory. That needs some more
3243c2c66affSColin Finck                  * investigation...
3244c2c66affSColin Finck                  */
3245c2c66affSColin Finck             }
3246c2c66affSColin Finck 
3247c2c66affSColin Finck             TRACE("Writing (%f %f %f) %f\n", x, y, z, rhw);
3248c2c66affSColin Finck 
3249c2c66affSColin Finck 
3250c2c66affSColin Finck             ( (float *) dest_ptr)[0] = x;
3251c2c66affSColin Finck             ( (float *) dest_ptr)[1] = y;
3252c2c66affSColin Finck             ( (float *) dest_ptr)[2] = z;
3253c2c66affSColin Finck             ( (float *) dest_ptr)[3] = rhw; /* SIC, see ddraw test! */
3254c2c66affSColin Finck 
3255c2c66affSColin Finck             dest_ptr += 3 * sizeof(float);
3256c2c66affSColin Finck 
3257c2c66affSColin Finck             if ((DestFVF & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZRHW)
3258c2c66affSColin Finck                 dest_ptr += sizeof(float);
3259c2c66affSColin Finck         }
3260c2c66affSColin Finck 
3261c2c66affSColin Finck         if (DestFVF & WINED3DFVF_PSIZE)
3262c2c66affSColin Finck             dest_ptr += sizeof(DWORD);
3263c2c66affSColin Finck 
3264c2c66affSColin Finck         if (DestFVF & WINED3DFVF_NORMAL)
3265c2c66affSColin Finck         {
3266c2c66affSColin Finck             const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_NORMAL];
3267c2c66affSColin Finck             const float *normal = (const float *)(element->data.addr + i * element->stride);
3268c2c66affSColin Finck             /* AFAIK this should go into the lighting information */
3269c2c66affSColin Finck             FIXME("Didn't expect the destination to have a normal\n");
3270c2c66affSColin Finck             copy_and_next(dest_ptr, normal, 3 * sizeof(float));
3271c2c66affSColin Finck         }
3272c2c66affSColin Finck 
3273c2c66affSColin Finck         if (DestFVF & WINED3DFVF_DIFFUSE)
3274c2c66affSColin Finck         {
3275c2c66affSColin Finck             const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_DIFFUSE];
3276c2c66affSColin Finck             const DWORD *color_d = (const DWORD *)(element->data.addr + i * element->stride);
3277c2c66affSColin Finck             if (!(stream_info->use_map & (1u << WINED3D_FFP_DIFFUSE)))
3278c2c66affSColin Finck             {
3279c2c66affSColin Finck                 static BOOL warned = FALSE;
3280c2c66affSColin Finck 
3281c2c66affSColin Finck                 if(!warned) {
3282c2c66affSColin Finck                     ERR("No diffuse color in source, but destination has one\n");
3283c2c66affSColin Finck                     warned = TRUE;
3284c2c66affSColin Finck                 }
3285c2c66affSColin Finck 
3286c2c66affSColin Finck                 *( (DWORD *) dest_ptr) = 0xffffffff;
3287c2c66affSColin Finck                 dest_ptr += sizeof(DWORD);
3288c2c66affSColin Finck             }
3289c2c66affSColin Finck             else
3290c2c66affSColin Finck             {
3291c2c66affSColin Finck                 copy_and_next(dest_ptr, color_d, sizeof(DWORD));
3292c2c66affSColin Finck             }
3293c2c66affSColin Finck         }
3294c2c66affSColin Finck 
3295c2c66affSColin Finck         if (DestFVF & WINED3DFVF_SPECULAR)
3296c2c66affSColin Finck         {
3297c2c66affSColin Finck             /* What's the color value in the feedback buffer? */
3298c2c66affSColin Finck             const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_SPECULAR];
3299c2c66affSColin Finck             const DWORD *color_s = (const DWORD *)(element->data.addr + i * element->stride);
3300c2c66affSColin Finck             if (!(stream_info->use_map & (1u << WINED3D_FFP_SPECULAR)))
3301c2c66affSColin Finck             {
3302c2c66affSColin Finck                 static BOOL warned = FALSE;
3303c2c66affSColin Finck 
3304c2c66affSColin Finck                 if(!warned) {
3305c2c66affSColin Finck                     ERR("No specular color in source, but destination has one\n");
3306c2c66affSColin Finck                     warned = TRUE;
3307c2c66affSColin Finck                 }
3308c2c66affSColin Finck 
3309c2c66affSColin Finck                 *(DWORD *)dest_ptr = 0xff000000;
3310c2c66affSColin Finck                 dest_ptr += sizeof(DWORD);
3311c2c66affSColin Finck             }
3312c2c66affSColin Finck             else
3313c2c66affSColin Finck             {
3314c2c66affSColin Finck                 copy_and_next(dest_ptr, color_s, sizeof(DWORD));
3315c2c66affSColin Finck             }
3316c2c66affSColin Finck         }
3317c2c66affSColin Finck 
3318c2c66affSColin Finck         for (tex_index = 0; tex_index < numTextures; ++tex_index)
3319c2c66affSColin Finck         {
3320c2c66affSColin Finck             const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_TEXCOORD0 + tex_index];
3321c2c66affSColin Finck             const float *tex_coord = (const float *)(element->data.addr + i * element->stride);
3322c2c66affSColin Finck             if (!(stream_info->use_map & (1u << (WINED3D_FFP_TEXCOORD0 + tex_index))))
3323c2c66affSColin Finck             {
3324c2c66affSColin Finck                 ERR("No source texture, but destination requests one\n");
3325c2c66affSColin Finck                 dest_ptr += GET_TEXCOORD_SIZE_FROM_FVF(DestFVF, tex_index) * sizeof(float);
3326c2c66affSColin Finck             }
3327c2c66affSColin Finck             else
3328c2c66affSColin Finck             {
3329c2c66affSColin Finck                 copy_and_next(dest_ptr, tex_coord, GET_TEXCOORD_SIZE_FROM_FVF(DestFVF, tex_index) * sizeof(float));
3330c2c66affSColin Finck             }
3331c2c66affSColin Finck         }
3332c2c66affSColin Finck     }
3333c2c66affSColin Finck 
3334c2c66affSColin Finck     wined3d_resource_unmap(&dest->resource, 0);
3335c2c66affSColin Finck 
3336c2c66affSColin Finck     return WINED3D_OK;
3337c2c66affSColin Finck }
3338c2c66affSColin Finck #undef copy_and_next
3339c2c66affSColin Finck 
wined3d_device_process_vertices(struct wined3d_device * device,UINT src_start_idx,UINT dst_idx,UINT vertex_count,struct wined3d_buffer * dst_buffer,const struct wined3d_vertex_declaration * declaration,DWORD flags,DWORD dst_fvf)3340c2c66affSColin Finck HRESULT CDECL wined3d_device_process_vertices(struct wined3d_device *device,
3341c2c66affSColin Finck         UINT src_start_idx, UINT dst_idx, UINT vertex_count, struct wined3d_buffer *dst_buffer,
3342c2c66affSColin Finck         const struct wined3d_vertex_declaration *declaration, DWORD flags, DWORD dst_fvf)
3343c2c66affSColin Finck {
3344c2c66affSColin Finck     struct wined3d_state *state = &device->state;
3345c2c66affSColin Finck     struct wined3d_stream_info stream_info;
3346c2c66affSColin Finck     struct wined3d_resource *resource;
3347c2c66affSColin Finck     struct wined3d_box box = {0};
3348c2c66affSColin Finck     struct wined3d_shader *vs;
3349c2c66affSColin Finck     unsigned int i;
3350c2c66affSColin Finck     HRESULT hr;
3351c2c66affSColin Finck     WORD map;
3352c2c66affSColin Finck 
3353c2c66affSColin Finck     TRACE("device %p, src_start_idx %u, dst_idx %u, vertex_count %u, "
3354c2c66affSColin Finck             "dst_buffer %p, declaration %p, flags %#x, dst_fvf %#x.\n",
3355c2c66affSColin Finck             device, src_start_idx, dst_idx, vertex_count,
3356c2c66affSColin Finck             dst_buffer, declaration, flags, dst_fvf);
3357c2c66affSColin Finck 
3358c2c66affSColin Finck     if (declaration)
3359c2c66affSColin Finck         FIXME("Output vertex declaration not implemented yet.\n");
3360c2c66affSColin Finck 
3361c2c66affSColin Finck     vs = state->shader[WINED3D_SHADER_TYPE_VERTEX];
3362c2c66affSColin Finck     state->shader[WINED3D_SHADER_TYPE_VERTEX] = NULL;
3363c2c66affSColin Finck     wined3d_stream_info_from_declaration(&stream_info, state, &device->adapter->gl_info, &device->adapter->d3d_info);
3364c2c66affSColin Finck     state->shader[WINED3D_SHADER_TYPE_VERTEX] = vs;
3365c2c66affSColin Finck 
3366c2c66affSColin Finck     /* We can't convert FROM a VBO, and vertex buffers used to source into
3367c2c66affSColin Finck      * process_vertices() are unlikely to ever be used for drawing. Release
3368c2c66affSColin Finck      * VBOs in those buffers and fix up the stream_info structure.
3369c2c66affSColin Finck      *
3370c2c66affSColin Finck      * Also apply the start index. */
3371c2c66affSColin Finck     for (i = 0, map = stream_info.use_map; map; map >>= 1, ++i)
3372c2c66affSColin Finck     {
3373c2c66affSColin Finck         struct wined3d_stream_info_element *e;
3374c2c66affSColin Finck         struct wined3d_map_desc map_desc;
3375c2c66affSColin Finck 
3376c2c66affSColin Finck         if (!(map & 1))
3377c2c66affSColin Finck             continue;
3378c2c66affSColin Finck 
3379c2c66affSColin Finck         e = &stream_info.elements[i];
3380c2c66affSColin Finck         resource = &state->streams[e->stream_idx].buffer->resource;
3381c2c66affSColin Finck         box.left = src_start_idx * e->stride;
3382c2c66affSColin Finck         box.right = box.left + vertex_count * e->stride;
33839987f029SAmine Khaldi         if (FAILED(wined3d_resource_map(resource, 0, &map_desc, &box, WINED3D_MAP_READ)))
3384c2c66affSColin Finck             ERR("Failed to map resource.\n");
3385c2c66affSColin Finck         e->data.buffer_object = 0;
3386c2c66affSColin Finck         e->data.addr += (ULONG_PTR)map_desc.data;
3387c2c66affSColin Finck     }
3388c2c66affSColin Finck 
3389c2c66affSColin Finck     hr = process_vertices_strided(device, dst_idx, vertex_count,
3390c2c66affSColin Finck             &stream_info, dst_buffer, flags, dst_fvf);
3391c2c66affSColin Finck 
3392c2c66affSColin Finck     for (i = 0, map = stream_info.use_map; map; map >>= 1, ++i)
3393c2c66affSColin Finck     {
3394c2c66affSColin Finck         if (!(map & 1))
3395c2c66affSColin Finck             continue;
3396c2c66affSColin Finck 
3397c2c66affSColin Finck         resource = &state->streams[stream_info.elements[i].stream_idx].buffer->resource;
3398c2c66affSColin Finck         if (FAILED(wined3d_resource_unmap(resource, 0)))
3399c2c66affSColin Finck             ERR("Failed to unmap resource.\n");
3400c2c66affSColin Finck     }
3401c2c66affSColin Finck 
3402c2c66affSColin Finck     return hr;
3403c2c66affSColin Finck }
3404c2c66affSColin Finck 
wined3d_device_set_texture_stage_state(struct wined3d_device * device,UINT stage,enum wined3d_texture_stage_state state,DWORD value)3405c2c66affSColin Finck void CDECL wined3d_device_set_texture_stage_state(struct wined3d_device *device,
3406c2c66affSColin Finck         UINT stage, enum wined3d_texture_stage_state state, DWORD value)
3407c2c66affSColin Finck {
3408c2c66affSColin Finck     const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
3409c2c66affSColin Finck     DWORD old_value;
3410c2c66affSColin Finck 
3411c2c66affSColin Finck     TRACE("device %p, stage %u, state %s, value %#x.\n",
3412c2c66affSColin Finck             device, stage, debug_d3dtexturestate(state), value);
3413c2c66affSColin Finck 
3414c2c66affSColin Finck     if (state > WINED3D_HIGHEST_TEXTURE_STATE)
3415c2c66affSColin Finck     {
3416c2c66affSColin Finck         WARN("Invalid state %#x passed.\n", state);
3417c2c66affSColin Finck         return;
3418c2c66affSColin Finck     }
3419c2c66affSColin Finck 
3420c2c66affSColin Finck     if (stage >= d3d_info->limits.ffp_blend_stages)
3421c2c66affSColin Finck     {
3422c2c66affSColin Finck         WARN("Attempting to set stage %u which is higher than the max stage %u, ignoring.\n",
3423c2c66affSColin Finck                 stage, d3d_info->limits.ffp_blend_stages - 1);
3424c2c66affSColin Finck         return;
3425c2c66affSColin Finck     }
3426c2c66affSColin Finck 
3427c2c66affSColin Finck     old_value = device->update_state->texture_states[stage][state];
3428c2c66affSColin Finck     device->update_state->texture_states[stage][state] = value;
3429c2c66affSColin Finck 
3430c2c66affSColin Finck     if (device->recording)
3431c2c66affSColin Finck     {
3432c2c66affSColin Finck         TRACE("Recording... not performing anything.\n");
3433c2c66affSColin Finck         device->recording->changed.textureState[stage] |= 1u << state;
3434c2c66affSColin Finck         return;
3435c2c66affSColin Finck     }
3436c2c66affSColin Finck 
3437c2c66affSColin Finck     /* Checked after the assignments to allow proper stateblock recording. */
3438c2c66affSColin Finck     if (old_value == value)
3439c2c66affSColin Finck     {
3440c2c66affSColin Finck         TRACE("Application is setting the old value over, nothing to do.\n");
3441c2c66affSColin Finck         return;
3442c2c66affSColin Finck     }
3443c2c66affSColin Finck 
3444c2c66affSColin Finck     wined3d_cs_emit_set_texture_state(device->cs, stage, state, value);
3445c2c66affSColin Finck }
3446c2c66affSColin Finck 
wined3d_device_get_texture_stage_state(const struct wined3d_device * device,UINT stage,enum wined3d_texture_stage_state state)3447c2c66affSColin Finck DWORD CDECL wined3d_device_get_texture_stage_state(const struct wined3d_device *device,
3448c2c66affSColin Finck         UINT stage, enum wined3d_texture_stage_state state)
3449c2c66affSColin Finck {
3450c2c66affSColin Finck     TRACE("device %p, stage %u, state %s.\n",
3451c2c66affSColin Finck             device, stage, debug_d3dtexturestate(state));
3452c2c66affSColin Finck 
3453c2c66affSColin Finck     if (state > WINED3D_HIGHEST_TEXTURE_STATE)
3454c2c66affSColin Finck     {
3455c2c66affSColin Finck         WARN("Invalid state %#x passed.\n", state);
3456c2c66affSColin Finck         return 0;
3457c2c66affSColin Finck     }
3458c2c66affSColin Finck 
3459c2c66affSColin Finck     return device->state.texture_states[stage][state];
3460c2c66affSColin Finck }
3461c2c66affSColin Finck 
wined3d_device_set_texture(struct wined3d_device * device,UINT stage,struct wined3d_texture * texture)3462c2c66affSColin Finck HRESULT CDECL wined3d_device_set_texture(struct wined3d_device *device,
3463c2c66affSColin Finck         UINT stage, struct wined3d_texture *texture)
3464c2c66affSColin Finck {
3465c2c66affSColin Finck     struct wined3d_texture *prev;
3466c2c66affSColin Finck 
3467c2c66affSColin Finck     TRACE("device %p, stage %u, texture %p.\n", device, stage, texture);
3468c2c66affSColin Finck 
3469c2c66affSColin Finck     if (stage >= WINED3DVERTEXTEXTURESAMPLER0 && stage <= WINED3DVERTEXTEXTURESAMPLER3)
3470c2c66affSColin Finck         stage -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
3471c2c66affSColin Finck 
3472c2c66affSColin Finck     /* Windows accepts overflowing this array... we do not. */
3473d6ac0a71SAmine Khaldi     if (stage >= ARRAY_SIZE(device->state.textures))
3474c2c66affSColin Finck     {
3475c2c66affSColin Finck         WARN("Ignoring invalid stage %u.\n", stage);
3476c2c66affSColin Finck         return WINED3D_OK;
3477c2c66affSColin Finck     }
3478c2c66affSColin Finck 
34799987f029SAmine Khaldi     if (texture && texture->resource.usage & WINED3DUSAGE_SCRATCH)
3480c2c66affSColin Finck     {
3481c2c66affSColin Finck         WARN("Rejecting attempt to set scratch texture.\n");
3482c2c66affSColin Finck         return WINED3DERR_INVALIDCALL;
3483c2c66affSColin Finck     }
3484c2c66affSColin Finck 
3485c2c66affSColin Finck     if (device->recording)
3486c2c66affSColin Finck         device->recording->changed.textures |= 1u << stage;
3487c2c66affSColin Finck 
3488c2c66affSColin Finck     prev = device->update_state->textures[stage];
3489c2c66affSColin Finck     TRACE("Previous texture %p.\n", prev);
3490c2c66affSColin Finck 
3491c2c66affSColin Finck     if (texture == prev)
3492c2c66affSColin Finck     {
3493c2c66affSColin Finck         TRACE("App is setting the same texture again, nothing to do.\n");
3494c2c66affSColin Finck         return WINED3D_OK;
3495c2c66affSColin Finck     }
3496c2c66affSColin Finck 
3497c2c66affSColin Finck     TRACE("Setting new texture to %p.\n", texture);
3498c2c66affSColin Finck     device->update_state->textures[stage] = texture;
3499c2c66affSColin Finck 
3500c2c66affSColin Finck     if (texture)
3501c2c66affSColin Finck         wined3d_texture_incref(texture);
3502c2c66affSColin Finck     if (!device->recording)
3503c2c66affSColin Finck         wined3d_cs_emit_set_texture(device->cs, stage, texture);
3504c2c66affSColin Finck     if (prev)
3505c2c66affSColin Finck         wined3d_texture_decref(prev);
3506c2c66affSColin Finck 
3507c2c66affSColin Finck     return WINED3D_OK;
3508c2c66affSColin Finck }
3509c2c66affSColin Finck 
wined3d_device_get_texture(const struct wined3d_device * device,UINT stage)3510c2c66affSColin Finck struct wined3d_texture * CDECL wined3d_device_get_texture(const struct wined3d_device *device, UINT stage)
3511c2c66affSColin Finck {
3512c2c66affSColin Finck     TRACE("device %p, stage %u.\n", device, stage);
3513c2c66affSColin Finck 
3514c2c66affSColin Finck     if (stage >= WINED3DVERTEXTEXTURESAMPLER0 && stage <= WINED3DVERTEXTEXTURESAMPLER3)
3515c2c66affSColin Finck         stage -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
3516c2c66affSColin Finck 
3517d6ac0a71SAmine Khaldi     if (stage >= ARRAY_SIZE(device->state.textures))
3518c2c66affSColin Finck     {
3519c2c66affSColin Finck         WARN("Ignoring invalid stage %u.\n", stage);
3520c2c66affSColin Finck         return NULL; /* Windows accepts overflowing this array ... we do not. */
3521c2c66affSColin Finck     }
3522c2c66affSColin Finck 
3523c2c66affSColin Finck     return device->state.textures[stage];
3524c2c66affSColin Finck }
3525c2c66affSColin Finck 
wined3d_device_get_device_caps(const struct wined3d_device * device,WINED3DCAPS * caps)3526c2c66affSColin Finck HRESULT CDECL wined3d_device_get_device_caps(const struct wined3d_device *device, WINED3DCAPS *caps)
3527c2c66affSColin Finck {
35289987f029SAmine Khaldi     HRESULT hr;
35299987f029SAmine Khaldi 
3530c2c66affSColin Finck     TRACE("device %p, caps %p.\n", device, caps);
3531c2c66affSColin Finck 
35329987f029SAmine Khaldi     hr = wined3d_get_device_caps(device->wined3d, device->adapter->ordinal,
3533c2c66affSColin Finck             device->create_parms.device_type, caps);
35349987f029SAmine Khaldi 
35359987f029SAmine Khaldi     if (SUCCEEDED(hr) && use_software_vertex_processing(device))
35369987f029SAmine Khaldi         caps->MaxVertexBlendMatrixIndex = 255;
35379987f029SAmine Khaldi 
35389987f029SAmine Khaldi     return hr;
3539c2c66affSColin Finck }
3540c2c66affSColin Finck 
wined3d_device_get_display_mode(const struct wined3d_device * device,UINT swapchain_idx,struct wined3d_display_mode * mode,enum wined3d_display_rotation * rotation)3541c2c66affSColin Finck HRESULT CDECL wined3d_device_get_display_mode(const struct wined3d_device *device, UINT swapchain_idx,
3542c2c66affSColin Finck         struct wined3d_display_mode *mode, enum wined3d_display_rotation *rotation)
3543c2c66affSColin Finck {
3544c2c66affSColin Finck     struct wined3d_swapchain *swapchain;
3545c2c66affSColin Finck 
3546c2c66affSColin Finck     TRACE("device %p, swapchain_idx %u, mode %p, rotation %p.\n",
3547c2c66affSColin Finck             device, swapchain_idx, mode, rotation);
3548c2c66affSColin Finck 
3549c2c66affSColin Finck     if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
3550c2c66affSColin Finck         return WINED3DERR_INVALIDCALL;
3551c2c66affSColin Finck 
3552c2c66affSColin Finck     return wined3d_swapchain_get_display_mode(swapchain, mode, rotation);
3553c2c66affSColin Finck }
3554c2c66affSColin Finck 
wined3d_device_begin_stateblock(struct wined3d_device * device)3555c2c66affSColin Finck HRESULT CDECL wined3d_device_begin_stateblock(struct wined3d_device *device)
3556c2c66affSColin Finck {
3557c2c66affSColin Finck     struct wined3d_stateblock *stateblock;
3558c2c66affSColin Finck     HRESULT hr;
3559c2c66affSColin Finck 
3560c2c66affSColin Finck     TRACE("device %p.\n", device);
3561c2c66affSColin Finck 
3562c2c66affSColin Finck     if (device->recording)
3563c2c66affSColin Finck         return WINED3DERR_INVALIDCALL;
3564c2c66affSColin Finck 
3565c2c66affSColin Finck     hr = wined3d_stateblock_create(device, WINED3D_SBT_RECORDED, &stateblock);
3566c2c66affSColin Finck     if (FAILED(hr))
3567c2c66affSColin Finck         return hr;
3568c2c66affSColin Finck 
3569c2c66affSColin Finck     device->recording = stateblock;
3570c2c66affSColin Finck     device->update_state = &stateblock->state;
3571c2c66affSColin Finck 
3572c2c66affSColin Finck     TRACE("Recording stateblock %p.\n", stateblock);
3573c2c66affSColin Finck 
3574c2c66affSColin Finck     return WINED3D_OK;
3575c2c66affSColin Finck }
3576c2c66affSColin Finck 
wined3d_device_end_stateblock(struct wined3d_device * device,struct wined3d_stateblock ** stateblock)3577c2c66affSColin Finck HRESULT CDECL wined3d_device_end_stateblock(struct wined3d_device *device,
3578c2c66affSColin Finck         struct wined3d_stateblock **stateblock)
3579c2c66affSColin Finck {
3580c2c66affSColin Finck     struct wined3d_stateblock *object = device->recording;
3581c2c66affSColin Finck 
3582c2c66affSColin Finck     TRACE("device %p, stateblock %p.\n", device, stateblock);
3583c2c66affSColin Finck 
3584c2c66affSColin Finck     if (!device->recording)
3585c2c66affSColin Finck     {
3586c2c66affSColin Finck         WARN("Not recording.\n");
3587c2c66affSColin Finck         *stateblock = NULL;
3588c2c66affSColin Finck         return WINED3DERR_INVALIDCALL;
3589c2c66affSColin Finck     }
3590c2c66affSColin Finck 
3591c2c66affSColin Finck     stateblock_init_contained_states(object);
3592c2c66affSColin Finck 
3593c2c66affSColin Finck     *stateblock = object;
3594c2c66affSColin Finck     device->recording = NULL;
3595c2c66affSColin Finck     device->update_state = &device->state;
3596c2c66affSColin Finck 
3597c2c66affSColin Finck     TRACE("Returning stateblock %p.\n", *stateblock);
3598c2c66affSColin Finck 
3599c2c66affSColin Finck     return WINED3D_OK;
3600c2c66affSColin Finck }
3601c2c66affSColin Finck 
wined3d_device_begin_scene(struct wined3d_device * device)3602c2c66affSColin Finck HRESULT CDECL wined3d_device_begin_scene(struct wined3d_device *device)
3603c2c66affSColin Finck {
3604c2c66affSColin Finck     /* At the moment we have no need for any functionality at the beginning
3605c2c66affSColin Finck      * of a scene. */
3606c2c66affSColin Finck     TRACE("device %p.\n", device);
3607c2c66affSColin Finck 
3608c2c66affSColin Finck     if (device->inScene)
3609c2c66affSColin Finck     {
3610c2c66affSColin Finck         WARN("Already in scene, returning WINED3DERR_INVALIDCALL.\n");
3611c2c66affSColin Finck         return WINED3DERR_INVALIDCALL;
3612c2c66affSColin Finck     }
3613c2c66affSColin Finck     device->inScene = TRUE;
3614c2c66affSColin Finck     return WINED3D_OK;
3615c2c66affSColin Finck }
3616c2c66affSColin Finck 
wined3d_device_end_scene(struct wined3d_device * device)3617c2c66affSColin Finck HRESULT CDECL wined3d_device_end_scene(struct wined3d_device *device)
3618c2c66affSColin Finck {
3619c2c66affSColin Finck     TRACE("device %p.\n", device);
3620c2c66affSColin Finck 
3621c2c66affSColin Finck     if (!device->inScene)
3622c2c66affSColin Finck     {
3623c2c66affSColin Finck         WARN("Not in scene, returning WINED3DERR_INVALIDCALL.\n");
3624c2c66affSColin Finck         return WINED3DERR_INVALIDCALL;
3625c2c66affSColin Finck     }
3626c2c66affSColin Finck 
3627c2c66affSColin Finck     wined3d_cs_emit_flush(device->cs);
3628c2c66affSColin Finck 
3629c2c66affSColin Finck     device->inScene = FALSE;
3630c2c66affSColin Finck     return WINED3D_OK;
3631c2c66affSColin Finck }
3632c2c66affSColin Finck 
wined3d_device_clear(struct wined3d_device * device,DWORD rect_count,const RECT * rects,DWORD flags,const struct wined3d_color * color,float depth,DWORD stencil)3633c2c66affSColin Finck HRESULT CDECL wined3d_device_clear(struct wined3d_device *device, DWORD rect_count,
3634c2c66affSColin Finck         const RECT *rects, DWORD flags, const struct wined3d_color *color, float depth, DWORD stencil)
3635c2c66affSColin Finck {
3636c2c66affSColin Finck     TRACE("device %p, rect_count %u, rects %p, flags %#x, color %s, depth %.8e, stencil %u.\n",
3637c2c66affSColin Finck             device, rect_count, rects, flags, debug_color(color), depth, stencil);
3638c2c66affSColin Finck 
3639c2c66affSColin Finck     if (!rect_count && rects)
3640c2c66affSColin Finck     {
3641c2c66affSColin Finck         WARN("Rects is %p, but rect_count is 0, ignoring clear\n", rects);
3642c2c66affSColin Finck         return WINED3D_OK;
3643c2c66affSColin Finck     }
3644c2c66affSColin Finck 
3645c2c66affSColin Finck     if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
3646c2c66affSColin Finck     {
3647c2c66affSColin Finck         struct wined3d_rendertarget_view *ds = device->fb.depth_stencil;
3648c2c66affSColin Finck         if (!ds)
3649c2c66affSColin Finck         {
3650c2c66affSColin Finck             WARN("Clearing depth and/or stencil without a depth stencil buffer attached, returning WINED3DERR_INVALIDCALL\n");
3651c2c66affSColin Finck             /* TODO: What about depth stencil buffers without stencil bits? */
3652c2c66affSColin Finck             return WINED3DERR_INVALIDCALL;
3653c2c66affSColin Finck         }
3654c2c66affSColin Finck         else if (flags & WINED3DCLEAR_TARGET)
3655c2c66affSColin Finck         {
3656c2c66affSColin Finck             if (ds->width < device->fb.render_targets[0]->width
3657c2c66affSColin Finck                     || ds->height < device->fb.render_targets[0]->height)
3658c2c66affSColin Finck             {
3659c2c66affSColin Finck                 WARN("Silently ignoring depth and target clear with mismatching sizes\n");
3660c2c66affSColin Finck                 return WINED3D_OK;
3661c2c66affSColin Finck             }
3662c2c66affSColin Finck         }
3663c2c66affSColin Finck     }
3664c2c66affSColin Finck 
3665c2c66affSColin Finck     wined3d_cs_emit_clear(device->cs, rect_count, rects, flags, color, depth, stencil);
3666c2c66affSColin Finck 
3667c2c66affSColin Finck     return WINED3D_OK;
3668c2c66affSColin Finck }
3669c2c66affSColin Finck 
wined3d_device_set_predication(struct wined3d_device * device,struct wined3d_query * predicate,BOOL value)3670c2c66affSColin Finck void CDECL wined3d_device_set_predication(struct wined3d_device *device,
3671c2c66affSColin Finck         struct wined3d_query *predicate, BOOL value)
3672c2c66affSColin Finck {
3673c2c66affSColin Finck     struct wined3d_query *prev;
3674c2c66affSColin Finck 
3675c2c66affSColin Finck     TRACE("device %p, predicate %p, value %#x.\n", device, predicate, value);
3676c2c66affSColin Finck 
3677c2c66affSColin Finck     prev = device->update_state->predicate;
3678c2c66affSColin Finck     if (predicate)
3679c2c66affSColin Finck     {
3680c2c66affSColin Finck         FIXME("Predicated rendering not implemented.\n");
3681c2c66affSColin Finck         wined3d_query_incref(predicate);
3682c2c66affSColin Finck     }
3683c2c66affSColin Finck     device->update_state->predicate = predicate;
3684c2c66affSColin Finck     device->update_state->predicate_value = value;
3685c2c66affSColin Finck     if (!device->recording)
3686c2c66affSColin Finck         wined3d_cs_emit_set_predication(device->cs, predicate, value);
3687c2c66affSColin Finck     if (prev)
3688c2c66affSColin Finck         wined3d_query_decref(prev);
3689c2c66affSColin Finck }
3690c2c66affSColin Finck 
wined3d_device_get_predication(struct wined3d_device * device,BOOL * value)3691c2c66affSColin Finck struct wined3d_query * CDECL wined3d_device_get_predication(struct wined3d_device *device, BOOL *value)
3692c2c66affSColin Finck {
3693c2c66affSColin Finck     TRACE("device %p, value %p.\n", device, value);
3694c2c66affSColin Finck 
3695c2c66affSColin Finck     if (value)
3696c2c66affSColin Finck         *value = device->state.predicate_value;
3697c2c66affSColin Finck     return device->state.predicate;
3698c2c66affSColin Finck }
3699c2c66affSColin Finck 
wined3d_device_dispatch_compute(struct wined3d_device * device,unsigned int group_count_x,unsigned int group_count_y,unsigned int group_count_z)3700c2c66affSColin Finck void CDECL wined3d_device_dispatch_compute(struct wined3d_device *device,
3701c2c66affSColin Finck         unsigned int group_count_x, unsigned int group_count_y, unsigned int group_count_z)
3702c2c66affSColin Finck {
3703c2c66affSColin Finck     TRACE("device %p, group_count_x %u, group_count_y %u, group_count_z %u.\n",
3704c2c66affSColin Finck             device, group_count_x, group_count_y, group_count_z);
3705c2c66affSColin Finck 
3706c2c66affSColin Finck     wined3d_cs_emit_dispatch(device->cs, group_count_x, group_count_y, group_count_z);
3707c2c66affSColin Finck }
3708c2c66affSColin Finck 
wined3d_device_dispatch_compute_indirect(struct wined3d_device * device,struct wined3d_buffer * buffer,unsigned int offset)3709c2c66affSColin Finck void CDECL wined3d_device_dispatch_compute_indirect(struct wined3d_device *device,
3710c2c66affSColin Finck         struct wined3d_buffer *buffer, unsigned int offset)
3711c2c66affSColin Finck {
3712c2c66affSColin Finck     TRACE("device %p, buffer %p, offset %u.\n", device, buffer, offset);
3713c2c66affSColin Finck 
3714c2c66affSColin Finck     wined3d_cs_emit_dispatch_indirect(device->cs, buffer, offset);
3715c2c66affSColin Finck }
3716c2c66affSColin Finck 
wined3d_device_set_primitive_type(struct wined3d_device * device,enum wined3d_primitive_type primitive_type,unsigned int patch_vertex_count)3717c2c66affSColin Finck void CDECL wined3d_device_set_primitive_type(struct wined3d_device *device,
3718c2c66affSColin Finck         enum wined3d_primitive_type primitive_type, unsigned int patch_vertex_count)
3719c2c66affSColin Finck {
3720c2c66affSColin Finck     TRACE("device %p, primitive_type %s, patch_vertex_count %u.\n",
3721c2c66affSColin Finck             device, debug_d3dprimitivetype(primitive_type), patch_vertex_count);
3722c2c66affSColin Finck 
3723c2c66affSColin Finck     device->state.gl_primitive_type = gl_primitive_type_from_d3d(primitive_type);
3724c2c66affSColin Finck     device->state.gl_patch_vertices = patch_vertex_count;
3725c2c66affSColin Finck }
3726c2c66affSColin Finck 
wined3d_device_get_primitive_type(const struct wined3d_device * device,enum wined3d_primitive_type * primitive_type,unsigned int * patch_vertex_count)3727c2c66affSColin Finck void CDECL wined3d_device_get_primitive_type(const struct wined3d_device *device,
3728c2c66affSColin Finck         enum wined3d_primitive_type *primitive_type, unsigned int *patch_vertex_count)
3729c2c66affSColin Finck {
3730c2c66affSColin Finck     TRACE("device %p, primitive_type %p, patch_vertex_count %p.\n",
3731c2c66affSColin Finck             device, primitive_type, patch_vertex_count);
3732c2c66affSColin Finck 
3733c2c66affSColin Finck     *primitive_type = d3d_primitive_type_from_gl(device->state.gl_primitive_type);
3734c2c66affSColin Finck     if (patch_vertex_count)
3735c2c66affSColin Finck         *patch_vertex_count = device->state.gl_patch_vertices;
3736c2c66affSColin Finck 
3737c2c66affSColin Finck     TRACE("Returning %s.\n", debug_d3dprimitivetype(*primitive_type));
3738c2c66affSColin Finck }
3739c2c66affSColin Finck 
wined3d_device_draw_primitive(struct wined3d_device * device,UINT start_vertex,UINT vertex_count)3740c2c66affSColin Finck HRESULT CDECL wined3d_device_draw_primitive(struct wined3d_device *device, UINT start_vertex, UINT vertex_count)
3741c2c66affSColin Finck {
3742c2c66affSColin Finck     TRACE("device %p, start_vertex %u, vertex_count %u.\n", device, start_vertex, vertex_count);
3743c2c66affSColin Finck 
3744c2c66affSColin Finck     wined3d_cs_emit_draw(device->cs, device->state.gl_primitive_type, device->state.gl_patch_vertices,
3745c2c66affSColin Finck             0, start_vertex, vertex_count, 0, 0, FALSE);
3746c2c66affSColin Finck 
3747c2c66affSColin Finck     return WINED3D_OK;
3748c2c66affSColin Finck }
3749c2c66affSColin Finck 
wined3d_device_draw_primitive_instanced(struct wined3d_device * device,UINT start_vertex,UINT vertex_count,UINT start_instance,UINT instance_count)3750c2c66affSColin Finck void CDECL wined3d_device_draw_primitive_instanced(struct wined3d_device *device,
3751c2c66affSColin Finck         UINT start_vertex, UINT vertex_count, UINT start_instance, UINT instance_count)
3752c2c66affSColin Finck {
3753c2c66affSColin Finck     TRACE("device %p, start_vertex %u, vertex_count %u, start_instance %u, instance_count %u.\n",
3754c2c66affSColin Finck             device, start_vertex, vertex_count, start_instance, instance_count);
3755c2c66affSColin Finck 
3756c2c66affSColin Finck     wined3d_cs_emit_draw(device->cs, device->state.gl_primitive_type, device->state.gl_patch_vertices,
3757c2c66affSColin Finck             0, start_vertex, vertex_count, start_instance, instance_count, FALSE);
3758c2c66affSColin Finck }
3759c2c66affSColin Finck 
wined3d_device_draw_primitive_instanced_indirect(struct wined3d_device * device,struct wined3d_buffer * buffer,unsigned int offset)3760c2c66affSColin Finck void CDECL wined3d_device_draw_primitive_instanced_indirect(struct wined3d_device *device,
3761c2c66affSColin Finck         struct wined3d_buffer *buffer, unsigned int offset)
3762c2c66affSColin Finck {
3763c2c66affSColin Finck     TRACE("device %p, buffer %p, offset %u.\n", device, buffer, offset);
3764c2c66affSColin Finck 
3765c2c66affSColin Finck     wined3d_cs_emit_draw_indirect(device->cs, device->state.gl_primitive_type, device->state.gl_patch_vertices,
3766c2c66affSColin Finck             buffer, offset, FALSE);
3767c2c66affSColin Finck }
3768c2c66affSColin Finck 
wined3d_device_draw_indexed_primitive(struct wined3d_device * device,UINT start_idx,UINT index_count)3769c2c66affSColin Finck HRESULT CDECL wined3d_device_draw_indexed_primitive(struct wined3d_device *device, UINT start_idx, UINT index_count)
3770c2c66affSColin Finck {
3771c2c66affSColin Finck     TRACE("device %p, start_idx %u, index_count %u.\n", device, start_idx, index_count);
3772c2c66affSColin Finck 
3773c2c66affSColin Finck     if (!device->state.index_buffer)
3774c2c66affSColin Finck     {
3775c2c66affSColin Finck         /* D3D9 returns D3DERR_INVALIDCALL when DrawIndexedPrimitive is called
3776c2c66affSColin Finck          * without an index buffer set. (The first time at least...)
3777c2c66affSColin Finck          * D3D8 simply dies, but I doubt it can do much harm to return
3778c2c66affSColin Finck          * D3DERR_INVALIDCALL there as well. */
3779c2c66affSColin Finck         WARN("Called without a valid index buffer set, returning WINED3DERR_INVALIDCALL.\n");
3780c2c66affSColin Finck         return WINED3DERR_INVALIDCALL;
3781c2c66affSColin Finck     }
3782c2c66affSColin Finck 
3783c2c66affSColin Finck     wined3d_cs_emit_draw(device->cs, device->state.gl_primitive_type, device->state.gl_patch_vertices,
3784c2c66affSColin Finck             device->state.base_vertex_index, start_idx, index_count, 0, 0, TRUE);
3785c2c66affSColin Finck 
3786c2c66affSColin Finck     return WINED3D_OK;
3787c2c66affSColin Finck }
3788c2c66affSColin Finck 
wined3d_device_draw_indexed_primitive_instanced(struct wined3d_device * device,UINT start_idx,UINT index_count,UINT start_instance,UINT instance_count)3789c2c66affSColin Finck void CDECL wined3d_device_draw_indexed_primitive_instanced(struct wined3d_device *device,
3790c2c66affSColin Finck         UINT start_idx, UINT index_count, UINT start_instance, UINT instance_count)
3791c2c66affSColin Finck {
3792c2c66affSColin Finck     TRACE("device %p, start_idx %u, index_count %u, start_instance %u, instance_count %u.\n",
3793c2c66affSColin Finck             device, start_idx, index_count, start_instance, instance_count);
3794c2c66affSColin Finck 
3795c2c66affSColin Finck     wined3d_cs_emit_draw(device->cs, device->state.gl_primitive_type, device->state.gl_patch_vertices,
3796c2c66affSColin Finck             device->state.base_vertex_index, start_idx, index_count, start_instance, instance_count, TRUE);
3797c2c66affSColin Finck }
3798c2c66affSColin Finck 
wined3d_device_draw_indexed_primitive_instanced_indirect(struct wined3d_device * device,struct wined3d_buffer * buffer,unsigned int offset)3799c2c66affSColin Finck void CDECL wined3d_device_draw_indexed_primitive_instanced_indirect(struct wined3d_device *device,
3800c2c66affSColin Finck         struct wined3d_buffer *buffer, unsigned int offset)
3801c2c66affSColin Finck {
3802c2c66affSColin Finck     TRACE("device %p, buffer %p, offset %u.\n", device, buffer, offset);
3803c2c66affSColin Finck 
3804c2c66affSColin Finck     wined3d_cs_emit_draw_indirect(device->cs, device->state.gl_primitive_type, device->state.gl_patch_vertices,
3805c2c66affSColin Finck             buffer, offset, TRUE);
3806c2c66affSColin Finck }
3807c2c66affSColin Finck 
wined3d_device_update_texture(struct wined3d_device * device,struct wined3d_texture * src_texture,struct wined3d_texture * dst_texture)3808c2c66affSColin Finck HRESULT CDECL wined3d_device_update_texture(struct wined3d_device *device,
3809c2c66affSColin Finck         struct wined3d_texture *src_texture, struct wined3d_texture *dst_texture)
3810c2c66affSColin Finck {
3811c2c66affSColin Finck     unsigned int src_size, dst_size, src_skip_levels = 0;
3812c2c66affSColin Finck     unsigned int src_level_count, dst_level_count;
3813c2c66affSColin Finck     unsigned int layer_count, level_count, i, j;
3814c2c66affSColin Finck     unsigned int width, height, depth;
3815c2c66affSColin Finck     enum wined3d_resource_type type;
3816c2c66affSColin Finck     struct wined3d_box box;
3817c2c66affSColin Finck 
3818c2c66affSColin Finck     TRACE("device %p, src_texture %p, dst_texture %p.\n", device, src_texture, dst_texture);
3819c2c66affSColin Finck 
3820c2c66affSColin Finck     /* Verify that the source and destination textures are non-NULL. */
3821c2c66affSColin Finck     if (!src_texture || !dst_texture)
3822c2c66affSColin Finck     {
3823c2c66affSColin Finck         WARN("Source and destination textures must be non-NULL, returning WINED3DERR_INVALIDCALL.\n");
3824c2c66affSColin Finck         return WINED3DERR_INVALIDCALL;
3825c2c66affSColin Finck     }
3826c2c66affSColin Finck 
38279987f029SAmine Khaldi     if (src_texture->resource.access & WINED3D_RESOURCE_ACCESS_GPU
38289987f029SAmine Khaldi             || src_texture->resource.usage & WINED3DUSAGE_SCRATCH)
3829c2c66affSColin Finck     {
38309987f029SAmine Khaldi         WARN("Source resource is GPU accessible or a scratch resource.\n");
3831c2c66affSColin Finck         return WINED3DERR_INVALIDCALL;
3832c2c66affSColin Finck     }
38339987f029SAmine Khaldi     if (dst_texture->resource.access & WINED3D_RESOURCE_ACCESS_CPU)
3834c2c66affSColin Finck     {
38359987f029SAmine Khaldi         WARN("Destination resource is CPU accessible.\n");
3836c2c66affSColin Finck         return WINED3DERR_INVALIDCALL;
3837c2c66affSColin Finck     }
3838c2c66affSColin Finck 
3839c2c66affSColin Finck     /* Verify that the source and destination textures are the same type. */
3840c2c66affSColin Finck     type = src_texture->resource.type;
3841c2c66affSColin Finck     if (dst_texture->resource.type != type)
3842c2c66affSColin Finck     {
3843c2c66affSColin Finck         WARN("Source and destination have different types, returning WINED3DERR_INVALIDCALL.\n");
3844c2c66affSColin Finck         return WINED3DERR_INVALIDCALL;
3845c2c66affSColin Finck     }
3846c2c66affSColin Finck 
3847c2c66affSColin Finck     layer_count = src_texture->layer_count;
3848c2c66affSColin Finck     if (layer_count != dst_texture->layer_count)
3849c2c66affSColin Finck     {
3850c2c66affSColin Finck         WARN("Source and destination have different layer counts.\n");
3851c2c66affSColin Finck         return WINED3DERR_INVALIDCALL;
3852c2c66affSColin Finck     }
3853c2c66affSColin Finck 
3854c2c66affSColin Finck     if (src_texture->resource.format != dst_texture->resource.format)
3855c2c66affSColin Finck     {
3856c2c66affSColin Finck         WARN("Source and destination formats do not match.\n");
3857c2c66affSColin Finck         return WINED3DERR_INVALIDCALL;
3858c2c66affSColin Finck     }
3859c2c66affSColin Finck 
3860c2c66affSColin Finck     src_level_count = src_texture->level_count;
3861c2c66affSColin Finck     dst_level_count = dst_texture->level_count;
3862c2c66affSColin Finck     level_count = min(src_level_count, dst_level_count);
3863c2c66affSColin Finck 
3864c2c66affSColin Finck     src_size = max(src_texture->resource.width, src_texture->resource.height);
38657af3969eSAmine Khaldi     dst_size = max(dst_texture->resource.width, dst_texture->resource.height);
3866*81cffd76SJoachim Henze     if (type == WINED3D_RTYPE_TEXTURE_3D)
3867*81cffd76SJoachim Henze     {
3868*81cffd76SJoachim Henze         src_size = max(src_size, src_texture->resource.depth);
3869c2c66affSColin Finck         dst_size = max(dst_size, dst_texture->resource.depth);
3870*81cffd76SJoachim Henze     }
3871c2c66affSColin Finck     while (src_size > dst_size)
3872c2c66affSColin Finck     {
3873c2c66affSColin Finck         src_size >>= 1;
3874c2c66affSColin Finck         ++src_skip_levels;
3875c2c66affSColin Finck     }
3876c2c66affSColin Finck 
3877c2c66affSColin Finck     if (wined3d_texture_get_level_width(src_texture, src_skip_levels) != dst_texture->resource.width
3878c2c66affSColin Finck             || wined3d_texture_get_level_height(src_texture, src_skip_levels) != dst_texture->resource.height
3879c2c66affSColin Finck             || wined3d_texture_get_level_depth(src_texture, src_skip_levels) != dst_texture->resource.depth)
3880c2c66affSColin Finck     {
3881c2c66affSColin Finck         WARN("Source and destination dimensions do not match.\n");
3882c2c66affSColin Finck         return WINED3DERR_INVALIDCALL;
3883c2c66affSColin Finck     }
3884c2c66affSColin Finck 
3885c2c66affSColin Finck     /* Update every surface level of the texture. */
3886c2c66affSColin Finck     for (i = 0; i < level_count; ++i)
3887c2c66affSColin Finck     {
3888c2c66affSColin Finck         width = wined3d_texture_get_level_width(dst_texture, i);
3889c2c66affSColin Finck         height = wined3d_texture_get_level_height(dst_texture, i);
3890c2c66affSColin Finck         depth = wined3d_texture_get_level_depth(dst_texture, i);
3891c2c66affSColin Finck         wined3d_box_set(&box, 0, 0, width, height, 0, depth);
3892c2c66affSColin Finck 
3893c2c66affSColin Finck         for (j = 0; j < layer_count; ++j)
3894c2c66affSColin Finck         {
3895c2c66affSColin Finck             wined3d_cs_emit_blt_sub_resource(device->cs,
3896c2c66affSColin Finck                     &dst_texture->resource, j * dst_level_count + i, &box,
3897c2c66affSColin Finck                     &src_texture->resource, j * src_level_count + i + src_skip_levels, &box,
3898c2c66affSColin Finck                     0, NULL, WINED3D_TEXF_POINT);
3899c2c66affSColin Finck         }
3900c2c66affSColin Finck     }
3901c2c66affSColin Finck 
3902c2c66affSColin Finck     return WINED3D_OK;
3903c2c66affSColin Finck }
3904c2c66affSColin Finck 
wined3d_device_validate_device(const struct wined3d_device * device,DWORD * num_passes)3905c2c66affSColin Finck HRESULT CDECL wined3d_device_validate_device(const struct wined3d_device *device, DWORD *num_passes)
3906c2c66affSColin Finck {
3907c2c66affSColin Finck     const struct wined3d_state *state = &device->state;
3908c2c66affSColin Finck     struct wined3d_texture *texture;
3909c2c66affSColin Finck     DWORD i;
3910c2c66affSColin Finck 
3911c2c66affSColin Finck     TRACE("device %p, num_passes %p.\n", device, num_passes);
3912c2c66affSColin Finck 
3913c2c66affSColin Finck     for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i)
3914c2c66affSColin Finck     {
3915c2c66affSColin Finck         if (state->sampler_states[i][WINED3D_SAMP_MIN_FILTER] == WINED3D_TEXF_NONE)
3916c2c66affSColin Finck         {
3917c2c66affSColin Finck             WARN("Sampler state %u has minfilter D3DTEXF_NONE, returning D3DERR_UNSUPPORTEDTEXTUREFILTER\n", i);
3918c2c66affSColin Finck             return WINED3DERR_UNSUPPORTEDTEXTUREFILTER;
3919c2c66affSColin Finck         }
3920c2c66affSColin Finck         if (state->sampler_states[i][WINED3D_SAMP_MAG_FILTER] == WINED3D_TEXF_NONE)
3921c2c66affSColin Finck         {
3922c2c66affSColin Finck             WARN("Sampler state %u has magfilter D3DTEXF_NONE, returning D3DERR_UNSUPPORTEDTEXTUREFILTER\n", i);
3923c2c66affSColin Finck             return WINED3DERR_UNSUPPORTEDTEXTUREFILTER;
3924c2c66affSColin Finck         }
3925c2c66affSColin Finck 
3926c2c66affSColin Finck         texture = state->textures[i];
3927c2c66affSColin Finck         if (!texture || texture->resource.format_flags & WINED3DFMT_FLAG_FILTERING) continue;
3928c2c66affSColin Finck 
3929c2c66affSColin Finck         if (state->sampler_states[i][WINED3D_SAMP_MAG_FILTER] != WINED3D_TEXF_POINT)
3930c2c66affSColin Finck         {
3931c2c66affSColin Finck             WARN("Non-filterable texture and mag filter enabled on sampler %u, returning E_FAIL\n", i);
3932c2c66affSColin Finck             return E_FAIL;
3933c2c66affSColin Finck         }
3934c2c66affSColin Finck         if (state->sampler_states[i][WINED3D_SAMP_MIN_FILTER] != WINED3D_TEXF_POINT)
3935c2c66affSColin Finck         {
3936c2c66affSColin Finck             WARN("Non-filterable texture and min filter enabled on sampler %u, returning E_FAIL\n", i);
3937c2c66affSColin Finck             return E_FAIL;
3938c2c66affSColin Finck         }
3939c2c66affSColin Finck         if (state->sampler_states[i][WINED3D_SAMP_MIP_FILTER] != WINED3D_TEXF_NONE
3940c2c66affSColin Finck                 && state->sampler_states[i][WINED3D_SAMP_MIP_FILTER] != WINED3D_TEXF_POINT)
3941c2c66affSColin Finck         {
3942c2c66affSColin Finck             WARN("Non-filterable texture and mip filter enabled on sampler %u, returning E_FAIL\n", i);
3943c2c66affSColin Finck             return E_FAIL;
3944c2c66affSColin Finck         }
3945c2c66affSColin Finck     }
3946c2c66affSColin Finck 
3947c2c66affSColin Finck     if (state->render_states[WINED3D_RS_ZENABLE] || state->render_states[WINED3D_RS_ZWRITEENABLE]
3948c2c66affSColin Finck             || state->render_states[WINED3D_RS_STENCILENABLE])
3949c2c66affSColin Finck     {
3950c2c66affSColin Finck         struct wined3d_rendertarget_view *rt = device->fb.render_targets[0];
3951c2c66affSColin Finck         struct wined3d_rendertarget_view *ds = device->fb.depth_stencil;
3952c2c66affSColin Finck 
3953c2c66affSColin Finck         if (ds && rt && (ds->width < rt->width || ds->height < rt->height))
3954c2c66affSColin Finck         {
3955c2c66affSColin Finck             WARN("Depth stencil is smaller than the color buffer, returning D3DERR_CONFLICTINGRENDERSTATE\n");
3956c2c66affSColin Finck             return WINED3DERR_CONFLICTINGRENDERSTATE;
3957c2c66affSColin Finck         }
3958c2c66affSColin Finck     }
3959c2c66affSColin Finck 
3960c2c66affSColin Finck     /* return a sensible default */
3961c2c66affSColin Finck     *num_passes = 1;
3962c2c66affSColin Finck 
3963c2c66affSColin Finck     TRACE("returning D3D_OK\n");
3964c2c66affSColin Finck     return WINED3D_OK;
3965c2c66affSColin Finck }
3966c2c66affSColin Finck 
wined3d_device_set_software_vertex_processing(struct wined3d_device * device,BOOL software)3967c2c66affSColin Finck void CDECL wined3d_device_set_software_vertex_processing(struct wined3d_device *device, BOOL software)
3968c2c66affSColin Finck {
3969c2c66affSColin Finck     TRACE("device %p, software %#x.\n", device, software);
3970c2c66affSColin Finck 
3971c2c66affSColin Finck     device->softwareVertexProcessing = software;
3972c2c66affSColin Finck }
3973c2c66affSColin Finck 
wined3d_device_get_software_vertex_processing(const struct wined3d_device * device)3974c2c66affSColin Finck BOOL CDECL wined3d_device_get_software_vertex_processing(const struct wined3d_device *device)
3975c2c66affSColin Finck {
3976c2c66affSColin Finck     TRACE("device %p.\n", device);
3977c2c66affSColin Finck 
3978c2c66affSColin Finck     return device->softwareVertexProcessing;
3979c2c66affSColin Finck }
3980c2c66affSColin Finck 
wined3d_device_get_raster_status(const struct wined3d_device * device,UINT swapchain_idx,struct wined3d_raster_status * raster_status)3981c2c66affSColin Finck HRESULT CDECL wined3d_device_get_raster_status(const struct wined3d_device *device,
3982c2c66affSColin Finck         UINT swapchain_idx, struct wined3d_raster_status *raster_status)
3983c2c66affSColin Finck {
3984c2c66affSColin Finck     struct wined3d_swapchain *swapchain;
3985c2c66affSColin Finck 
3986c2c66affSColin Finck     TRACE("device %p, swapchain_idx %u, raster_status %p.\n",
3987c2c66affSColin Finck             device, swapchain_idx, raster_status);
3988c2c66affSColin Finck 
3989c2c66affSColin Finck     if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
3990c2c66affSColin Finck         return WINED3DERR_INVALIDCALL;
3991c2c66affSColin Finck 
3992c2c66affSColin Finck     return wined3d_swapchain_get_raster_status(swapchain, raster_status);
3993c2c66affSColin Finck }
3994c2c66affSColin Finck 
wined3d_device_set_npatch_mode(struct wined3d_device * device,float segments)3995c2c66affSColin Finck HRESULT CDECL wined3d_device_set_npatch_mode(struct wined3d_device *device, float segments)
3996c2c66affSColin Finck {
3997c2c66affSColin Finck     static BOOL warned;
3998c2c66affSColin Finck 
3999c2c66affSColin Finck     TRACE("device %p, segments %.8e.\n", device, segments);
4000c2c66affSColin Finck 
4001c2c66affSColin Finck     if (segments != 0.0f)
4002c2c66affSColin Finck     {
4003c2c66affSColin Finck         if (!warned)
4004c2c66affSColin Finck         {
4005c2c66affSColin Finck             FIXME("device %p, segments %.8e stub!\n", device, segments);
4006c2c66affSColin Finck             warned = TRUE;
4007c2c66affSColin Finck         }
4008c2c66affSColin Finck     }
4009c2c66affSColin Finck 
4010c2c66affSColin Finck     return WINED3D_OK;
4011c2c66affSColin Finck }
4012c2c66affSColin Finck 
wined3d_device_get_npatch_mode(const struct wined3d_device * device)4013c2c66affSColin Finck float CDECL wined3d_device_get_npatch_mode(const struct wined3d_device *device)
4014c2c66affSColin Finck {
4015c2c66affSColin Finck     static BOOL warned;
4016c2c66affSColin Finck 
4017c2c66affSColin Finck     TRACE("device %p.\n", device);
4018c2c66affSColin Finck 
4019c2c66affSColin Finck     if (!warned)
4020c2c66affSColin Finck     {
4021c2c66affSColin Finck         FIXME("device %p stub!\n", device);
4022c2c66affSColin Finck         warned = TRUE;
4023c2c66affSColin Finck     }
4024c2c66affSColin Finck 
4025c2c66affSColin Finck     return 0.0f;
4026c2c66affSColin Finck }
4027c2c66affSColin Finck 
wined3d_device_copy_uav_counter(struct wined3d_device * device,struct wined3d_buffer * dst_buffer,unsigned int offset,struct wined3d_unordered_access_view * uav)4028c2c66affSColin Finck void CDECL wined3d_device_copy_uav_counter(struct wined3d_device *device,
4029c2c66affSColin Finck         struct wined3d_buffer *dst_buffer, unsigned int offset, struct wined3d_unordered_access_view *uav)
4030c2c66affSColin Finck {
4031c2c66affSColin Finck     TRACE("device %p, dst_buffer %p, offset %u, uav %p.\n",
4032c2c66affSColin Finck             device, dst_buffer, offset, uav);
4033c2c66affSColin Finck 
40349987f029SAmine Khaldi     if (offset + sizeof(GLuint) > dst_buffer->resource.size)
40359987f029SAmine Khaldi     {
40369987f029SAmine Khaldi         WARN("Offset %u too large.\n", offset);
40379987f029SAmine Khaldi         return;
40389987f029SAmine Khaldi     }
40399987f029SAmine Khaldi 
4040c2c66affSColin Finck     wined3d_cs_emit_copy_uav_counter(device->cs, dst_buffer, offset, uav);
4041c2c66affSColin Finck }
4042c2c66affSColin Finck 
wined3d_device_copy_resource(struct wined3d_device * device,struct wined3d_resource * dst_resource,struct wined3d_resource * src_resource)4043c2c66affSColin Finck void CDECL wined3d_device_copy_resource(struct wined3d_device *device,
4044c2c66affSColin Finck         struct wined3d_resource *dst_resource, struct wined3d_resource *src_resource)
4045c2c66affSColin Finck {
4046c2c66affSColin Finck     struct wined3d_texture *dst_texture, *src_texture;
4047c2c66affSColin Finck     struct wined3d_box box;
4048c2c66affSColin Finck     unsigned int i, j;
4049c2c66affSColin Finck 
4050c2c66affSColin Finck     TRACE("device %p, dst_resource %p, src_resource %p.\n", device, dst_resource, src_resource);
4051c2c66affSColin Finck 
4052c2c66affSColin Finck     if (src_resource == dst_resource)
4053c2c66affSColin Finck     {
4054c2c66affSColin Finck         WARN("Source and destination are the same resource.\n");
4055c2c66affSColin Finck         return;
4056c2c66affSColin Finck     }
4057c2c66affSColin Finck 
4058c2c66affSColin Finck     if (src_resource->type != dst_resource->type)
4059c2c66affSColin Finck     {
4060c2c66affSColin Finck         WARN("Resource types (%s / %s) don't match.\n",
4061c2c66affSColin Finck                 debug_d3dresourcetype(dst_resource->type),
4062c2c66affSColin Finck                 debug_d3dresourcetype(src_resource->type));
4063c2c66affSColin Finck         return;
4064c2c66affSColin Finck     }
4065c2c66affSColin Finck 
4066c2c66affSColin Finck     if (src_resource->width != dst_resource->width
4067c2c66affSColin Finck             || src_resource->height != dst_resource->height
4068c2c66affSColin Finck             || src_resource->depth != dst_resource->depth)
4069c2c66affSColin Finck     {
4070c2c66affSColin Finck         WARN("Resource dimensions (%ux%ux%u / %ux%ux%u) don't match.\n",
4071c2c66affSColin Finck                 dst_resource->width, dst_resource->height, dst_resource->depth,
4072c2c66affSColin Finck                 src_resource->width, src_resource->height, src_resource->depth);
4073c2c66affSColin Finck         return;
4074c2c66affSColin Finck     }
4075c2c66affSColin Finck 
4076d6ac0a71SAmine Khaldi     if (src_resource->format->typeless_id != dst_resource->format->typeless_id
4077d6ac0a71SAmine Khaldi             || (!src_resource->format->typeless_id && src_resource->format->id != dst_resource->format->id))
4078c2c66affSColin Finck     {
4079d6ac0a71SAmine Khaldi         WARN("Resource formats %s and %s are incompatible.\n",
4080c2c66affSColin Finck                 debug_d3dformat(dst_resource->format->id),
4081c2c66affSColin Finck                 debug_d3dformat(src_resource->format->id));
4082c2c66affSColin Finck         return;
4083c2c66affSColin Finck     }
4084c2c66affSColin Finck 
4085c2c66affSColin Finck     if (dst_resource->type == WINED3D_RTYPE_BUFFER)
4086c2c66affSColin Finck     {
4087c2c66affSColin Finck         wined3d_box_set(&box, 0, 0, src_resource->size, 1, 0, 1);
4088d6ac0a71SAmine Khaldi         wined3d_cs_emit_blt_sub_resource(device->cs, dst_resource, 0, &box,
4089d6ac0a71SAmine Khaldi                 src_resource, 0, &box, WINED3D_BLT_RAW, NULL, WINED3D_TEXF_POINT);
4090c2c66affSColin Finck         return;
4091c2c66affSColin Finck     }
4092c2c66affSColin Finck 
4093c2c66affSColin Finck     dst_texture = texture_from_resource(dst_resource);
4094c2c66affSColin Finck     src_texture = texture_from_resource(src_resource);
4095c2c66affSColin Finck 
4096c2c66affSColin Finck     if (src_texture->layer_count != dst_texture->layer_count
4097c2c66affSColin Finck             || src_texture->level_count != dst_texture->level_count)
4098c2c66affSColin Finck     {
4099c2c66affSColin Finck         WARN("Subresource layouts (%ux%u / %ux%u) don't match.\n",
4100c2c66affSColin Finck                 dst_texture->layer_count, dst_texture->level_count,
4101c2c66affSColin Finck                 src_texture->layer_count, src_texture->level_count);
4102c2c66affSColin Finck         return;
4103c2c66affSColin Finck     }
4104c2c66affSColin Finck 
4105c2c66affSColin Finck     for (i = 0; i < dst_texture->level_count; ++i)
4106c2c66affSColin Finck     {
4107c2c66affSColin Finck         wined3d_box_set(&box, 0, 0,
4108c2c66affSColin Finck                 wined3d_texture_get_level_width(dst_texture, i),
4109c2c66affSColin Finck                 wined3d_texture_get_level_height(dst_texture, i),
4110c2c66affSColin Finck                 0, wined3d_texture_get_level_depth(dst_texture, i));
4111c2c66affSColin Finck         for (j = 0; j < dst_texture->layer_count; ++j)
4112c2c66affSColin Finck         {
4113c2c66affSColin Finck             unsigned int idx = j * dst_texture->level_count + i;
4114c2c66affSColin Finck 
4115d6ac0a71SAmine Khaldi             wined3d_cs_emit_blt_sub_resource(device->cs, dst_resource, idx, &box,
4116d6ac0a71SAmine Khaldi                     src_resource, idx, &box, WINED3D_BLT_RAW, NULL, WINED3D_TEXF_POINT);
4117c2c66affSColin Finck         }
4118c2c66affSColin Finck     }
4119c2c66affSColin Finck }
4120c2c66affSColin Finck 
wined3d_device_copy_sub_resource_region(struct wined3d_device * device,struct wined3d_resource * dst_resource,unsigned int dst_sub_resource_idx,unsigned int dst_x,unsigned int dst_y,unsigned int dst_z,struct wined3d_resource * src_resource,unsigned int src_sub_resource_idx,const struct wined3d_box * src_box)4121c2c66affSColin Finck HRESULT CDECL wined3d_device_copy_sub_resource_region(struct wined3d_device *device,
4122c2c66affSColin Finck         struct wined3d_resource *dst_resource, unsigned int dst_sub_resource_idx, unsigned int dst_x,
4123c2c66affSColin Finck         unsigned int dst_y, unsigned int dst_z, struct wined3d_resource *src_resource,
4124*81cffd76SJoachim Henze         unsigned int src_sub_resource_idx, const struct wined3d_box *src_box)
4125c2c66affSColin Finck {
4126c2c66affSColin Finck     struct wined3d_box dst_box, b;
4127c2c66affSColin Finck 
4128c2c66affSColin Finck     TRACE("device %p, dst_resource %p, dst_sub_resource_idx %u, dst_x %u, dst_y %u, dst_z %u, "
4129*81cffd76SJoachim Henze             "src_resource %p, src_sub_resource_idx %u, src_box %s.\n",
4130c2c66affSColin Finck             device, dst_resource, dst_sub_resource_idx, dst_x, dst_y, dst_z,
4131*81cffd76SJoachim Henze             src_resource, src_sub_resource_idx, debug_box(src_box));
4132c2c66affSColin Finck 
4133c2c66affSColin Finck     if (src_resource == dst_resource && src_sub_resource_idx == dst_sub_resource_idx)
4134c2c66affSColin Finck     {
4135c2c66affSColin Finck         WARN("Source and destination are the same sub-resource.\n");
4136c2c66affSColin Finck         return WINED3DERR_INVALIDCALL;
4137c2c66affSColin Finck     }
4138c2c66affSColin Finck 
4139c2c66affSColin Finck     if (src_resource->type != dst_resource->type)
4140c2c66affSColin Finck     {
4141c2c66affSColin Finck         WARN("Resource types (%s / %s) don't match.\n",
4142c2c66affSColin Finck                 debug_d3dresourcetype(dst_resource->type),
4143c2c66affSColin Finck                 debug_d3dresourcetype(src_resource->type));
4144c2c66affSColin Finck         return WINED3DERR_INVALIDCALL;
4145c2c66affSColin Finck     }
4146c2c66affSColin Finck 
4147d6ac0a71SAmine Khaldi     if (src_resource->format->typeless_id != dst_resource->format->typeless_id
4148d6ac0a71SAmine Khaldi             || (!src_resource->format->typeless_id && src_resource->format->id != dst_resource->format->id))
4149c2c66affSColin Finck     {
4150d6ac0a71SAmine Khaldi         WARN("Resource formats %s and %s are incompatible.\n",
4151c2c66affSColin Finck                 debug_d3dformat(dst_resource->format->id),
4152c2c66affSColin Finck                 debug_d3dformat(src_resource->format->id));
4153c2c66affSColin Finck         return WINED3DERR_INVALIDCALL;
4154c2c66affSColin Finck     }
4155c2c66affSColin Finck 
4156c2c66affSColin Finck     if (dst_resource->type == WINED3D_RTYPE_BUFFER)
4157c2c66affSColin Finck     {
4158c2c66affSColin Finck         if (dst_sub_resource_idx)
4159c2c66affSColin Finck         {
4160c2c66affSColin Finck             WARN("Invalid dst_sub_resource_idx %u.\n", dst_sub_resource_idx);
4161c2c66affSColin Finck             return WINED3DERR_INVALIDCALL;
4162c2c66affSColin Finck         }
4163c2c66affSColin Finck 
4164c2c66affSColin Finck         if (src_sub_resource_idx)
4165c2c66affSColin Finck         {
4166c2c66affSColin Finck             WARN("Invalid src_sub_resource_idx %u.\n", src_sub_resource_idx);
4167c2c66affSColin Finck             return WINED3DERR_INVALIDCALL;
4168c2c66affSColin Finck         }
4169c2c66affSColin Finck 
4170c2c66affSColin Finck         if (!src_box)
4171c2c66affSColin Finck         {
4172d6ac0a71SAmine Khaldi             unsigned int dst_w;
4173d6ac0a71SAmine Khaldi 
4174d6ac0a71SAmine Khaldi             dst_w = dst_resource->size - dst_x;
4175d6ac0a71SAmine Khaldi             wined3d_box_set(&b, 0, 0, min(src_resource->size, dst_w), 1, 0, 1);
4176c2c66affSColin Finck             src_box = &b;
4177c2c66affSColin Finck         }
4178c2c66affSColin Finck         else if ((src_box->left >= src_box->right
4179c2c66affSColin Finck                 || src_box->top >= src_box->bottom
4180c2c66affSColin Finck                 || src_box->front >= src_box->back))
4181c2c66affSColin Finck         {
4182c2c66affSColin Finck             WARN("Invalid box %s specified.\n", debug_box(src_box));
4183c2c66affSColin Finck             return WINED3DERR_INVALIDCALL;
4184c2c66affSColin Finck         }
4185c2c66affSColin Finck 
4186c2c66affSColin Finck         if (src_box->right > src_resource->size || dst_x >= dst_resource->size
4187c2c66affSColin Finck                 || src_box->right - src_box->left > dst_resource->size - dst_x)
4188c2c66affSColin Finck         {
4189c2c66affSColin Finck             WARN("Invalid range specified, dst_offset %u, src_offset %u, size %u.\n",
4190c2c66affSColin Finck                     dst_x, src_box->left, src_box->right - src_box->left);
4191c2c66affSColin Finck             return WINED3DERR_INVALIDCALL;
4192c2c66affSColin Finck         }
4193c2c66affSColin Finck 
4194c2c66affSColin Finck         wined3d_box_set(&dst_box, dst_x, 0, dst_x + (src_box->right - src_box->left), 1, 0, 1);
4195c2c66affSColin Finck     }
4196*81cffd76SJoachim Henze     else if (dst_resource->type == WINED3D_RTYPE_TEXTURE_2D)
4197c2c66affSColin Finck     {
4198c2c66affSColin Finck         struct wined3d_texture *dst_texture = texture_from_resource(dst_resource);
4199c2c66affSColin Finck         struct wined3d_texture *src_texture = texture_from_resource(src_resource);
4200c2c66affSColin Finck         unsigned int src_level = src_sub_resource_idx % src_texture->level_count;
4201c2c66affSColin Finck 
4202c2c66affSColin Finck         if (dst_sub_resource_idx >= dst_texture->level_count * dst_texture->layer_count)
4203c2c66affSColin Finck         {
4204c2c66affSColin Finck             WARN("Invalid destination sub-resource %u.\n", dst_sub_resource_idx);
4205c2c66affSColin Finck             return WINED3DERR_INVALIDCALL;
4206c2c66affSColin Finck         }
4207c2c66affSColin Finck 
4208c2c66affSColin Finck         if (src_sub_resource_idx >= src_texture->level_count * src_texture->layer_count)
4209c2c66affSColin Finck         {
4210c2c66affSColin Finck             WARN("Invalid source sub-resource %u.\n", src_sub_resource_idx);
4211c2c66affSColin Finck             return WINED3DERR_INVALIDCALL;
4212c2c66affSColin Finck         }
4213c2c66affSColin Finck 
4214*81cffd76SJoachim Henze #if !defined(STAGING_CSMT)
4215c2c66affSColin Finck         if (dst_texture->sub_resources[dst_sub_resource_idx].map_count)
4216c2c66affSColin Finck         {
4217c2c66affSColin Finck             WARN("Destination sub-resource %u is mapped.\n", dst_sub_resource_idx);
4218c2c66affSColin Finck             return WINED3DERR_INVALIDCALL;
4219c2c66affSColin Finck         }
4220c2c66affSColin Finck 
4221c2c66affSColin Finck         if (src_texture->sub_resources[src_sub_resource_idx].map_count)
4222c2c66affSColin Finck         {
4223c2c66affSColin Finck             WARN("Source sub-resource %u is mapped.\n", src_sub_resource_idx);
4224c2c66affSColin Finck             return WINED3DERR_INVALIDCALL;
4225*81cffd76SJoachim Henze #else  /* STAGING_CSMT */
4226*81cffd76SJoachim Henze         if (dst_texture->sub_resources[dst_sub_resource_idx].map_count ||
4227*81cffd76SJoachim Henze             src_texture->sub_resources[src_sub_resource_idx].map_count)
4228*81cffd76SJoachim Henze         {
4229*81cffd76SJoachim Henze             struct wined3d_device *device = dst_texture->resource.device;
4230*81cffd76SJoachim Henze             device->cs->ops->finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
4231*81cffd76SJoachim Henze             if (dst_texture->sub_resources[dst_sub_resource_idx].map_count ||
4232*81cffd76SJoachim Henze                 src_texture->sub_resources[src_sub_resource_idx].map_count)
4233*81cffd76SJoachim Henze             {
4234*81cffd76SJoachim Henze                 WARN("Destination or source sub-resource is mapped.\n");
4235*81cffd76SJoachim Henze                 return WINEDDERR_SURFACEBUSY;
4236*81cffd76SJoachim Henze             }
4237*81cffd76SJoachim Henze #endif /* STAGING_CSMT */
4238c2c66affSColin Finck         }
4239c2c66affSColin Finck 
4240c2c66affSColin Finck         if (!src_box)
4241c2c66affSColin Finck         {
4242*81cffd76SJoachim Henze             unsigned int src_w, src_h, dst_w, dst_h, dst_level;
4243d6ac0a71SAmine Khaldi 
4244d6ac0a71SAmine Khaldi             src_w = wined3d_texture_get_level_width(src_texture, src_level);
4245d6ac0a71SAmine Khaldi             src_h = wined3d_texture_get_level_height(src_texture, src_level);
4246d6ac0a71SAmine Khaldi 
4247d6ac0a71SAmine Khaldi             dst_level = dst_sub_resource_idx % dst_texture->level_count;
4248d6ac0a71SAmine Khaldi             dst_w = wined3d_texture_get_level_width(dst_texture, dst_level) - dst_x;
4249d6ac0a71SAmine Khaldi             dst_h = wined3d_texture_get_level_height(dst_texture, dst_level) - dst_y;
4250d6ac0a71SAmine Khaldi 
4251*81cffd76SJoachim Henze             wined3d_box_set(&b, 0, 0, min(src_w, dst_w), min(src_h, dst_h), 0, 1);
4252c2c66affSColin Finck             src_box = &b;
4253c2c66affSColin Finck         }
4254c2c66affSColin Finck         else if (FAILED(wined3d_texture_check_box_dimensions(src_texture, src_level, src_box)))
4255c2c66affSColin Finck         {
4256c2c66affSColin Finck             WARN("Invalid source box %s.\n", debug_box(src_box));
4257c2c66affSColin Finck             return WINED3DERR_INVALIDCALL;
4258c2c66affSColin Finck         }
4259c2c66affSColin Finck 
4260c2c66affSColin Finck         wined3d_box_set(&dst_box, dst_x, dst_y, dst_x + (src_box->right - src_box->left),
4261*81cffd76SJoachim Henze                 dst_y + (src_box->bottom - src_box->top), 0, 1);
4262c2c66affSColin Finck         if (FAILED(wined3d_texture_check_box_dimensions(dst_texture,
4263c2c66affSColin Finck                 dst_sub_resource_idx % dst_texture->level_count, &dst_box)))
4264c2c66affSColin Finck         {
4265c2c66affSColin Finck             WARN("Invalid destination box %s.\n", debug_box(&dst_box));
4266c2c66affSColin Finck             return WINED3DERR_INVALIDCALL;
4267c2c66affSColin Finck         }
4268c2c66affSColin Finck     }
4269*81cffd76SJoachim Henze     else
4270*81cffd76SJoachim Henze     {
4271*81cffd76SJoachim Henze         FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(dst_resource->type));
4272*81cffd76SJoachim Henze         return WINED3DERR_INVALIDCALL;
4273*81cffd76SJoachim Henze     }
4274c2c66affSColin Finck 
4275d6ac0a71SAmine Khaldi     wined3d_cs_emit_blt_sub_resource(device->cs, dst_resource, dst_sub_resource_idx, &dst_box,
4276d6ac0a71SAmine Khaldi             src_resource, src_sub_resource_idx, src_box, WINED3D_BLT_RAW, NULL, WINED3D_TEXF_POINT);
4277c2c66affSColin Finck 
4278c2c66affSColin Finck     return WINED3D_OK;
4279c2c66affSColin Finck }
4280c2c66affSColin Finck 
4281c2c66affSColin Finck void CDECL wined3d_device_update_sub_resource(struct wined3d_device *device, struct wined3d_resource *resource,
4282c2c66affSColin Finck         unsigned int sub_resource_idx, const struct wined3d_box *box, const void *data, unsigned int row_pitch,
4283*81cffd76SJoachim Henze         unsigned int depth_pitch)
4284c2c66affSColin Finck {
4285c2c66affSColin Finck     unsigned int width, height, depth;
4286c2c66affSColin Finck     struct wined3d_box b;
4287c2c66affSColin Finck 
4288*81cffd76SJoachim Henze     TRACE("device %p, resource %p, sub_resource_idx %u, box %s, data %p, row_pitch %u, depth_pitch %u.\n",
4289*81cffd76SJoachim Henze             device, resource, sub_resource_idx, debug_box(box), data, row_pitch, depth_pitch);
4290c2c66affSColin Finck 
4291c2c66affSColin Finck     if (resource->type == WINED3D_RTYPE_BUFFER)
4292c2c66affSColin Finck     {
4293c2c66affSColin Finck         if (sub_resource_idx > 0)
4294c2c66affSColin Finck         {
4295c2c66affSColin Finck             WARN("Invalid sub_resource_idx %u.\n", sub_resource_idx);
4296c2c66affSColin Finck             return;
4297c2c66affSColin Finck         }
4298c2c66affSColin Finck 
4299c2c66affSColin Finck         width = resource->size;
4300c2c66affSColin Finck         height = 1;
4301c2c66affSColin Finck         depth = 1;
4302c2c66affSColin Finck     }
4303*81cffd76SJoachim Henze     else if (resource->type == WINED3D_RTYPE_TEXTURE_1D ||
4304*81cffd76SJoachim Henze             resource->type == WINED3D_RTYPE_TEXTURE_2D || resource->type == WINED3D_RTYPE_TEXTURE_3D)
4305c2c66affSColin Finck     {
4306c2c66affSColin Finck         struct wined3d_texture *texture = texture_from_resource(resource);
4307c2c66affSColin Finck         unsigned int level;
4308c2c66affSColin Finck 
4309c2c66affSColin Finck         if (sub_resource_idx >= texture->level_count * texture->layer_count)
4310c2c66affSColin Finck         {
4311c2c66affSColin Finck             WARN("Invalid sub_resource_idx %u.\n", sub_resource_idx);
4312c2c66affSColin Finck             return;
4313c2c66affSColin Finck         }
4314c2c66affSColin Finck 
4315c2c66affSColin Finck         level = sub_resource_idx % texture->level_count;
4316c2c66affSColin Finck         width = wined3d_texture_get_level_width(texture, level);
4317c2c66affSColin Finck         height = wined3d_texture_get_level_height(texture, level);
4318c2c66affSColin Finck         depth = wined3d_texture_get_level_depth(texture, level);
4319c2c66affSColin Finck     }
4320*81cffd76SJoachim Henze     else
4321*81cffd76SJoachim Henze     {
4322*81cffd76SJoachim Henze         FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(resource->type));
4323*81cffd76SJoachim Henze         return;
4324*81cffd76SJoachim Henze     }
4325c2c66affSColin Finck 
4326c2c66affSColin Finck     if (!box)
4327c2c66affSColin Finck     {
4328c2c66affSColin Finck         wined3d_box_set(&b, 0, 0, width, height, 0, depth);
4329c2c66affSColin Finck         box = &b;
4330c2c66affSColin Finck     }
4331c2c66affSColin Finck     else if (box->left >= box->right || box->right > width
4332c2c66affSColin Finck             || box->top >= box->bottom || box->bottom > height
4333c2c66affSColin Finck             || box->front >= box->back || box->back > depth)
4334c2c66affSColin Finck     {
4335c2c66affSColin Finck         WARN("Invalid box %s specified.\n", debug_box(box));
4336c2c66affSColin Finck         return;
4337c2c66affSColin Finck     }
4338c2c66affSColin Finck 
4339*81cffd76SJoachim Henze #if !defined(STAGING_CSMT)
4340*81cffd76SJoachim Henze     wined3d_resource_wait_idle(resource);
4341*81cffd76SJoachim Henze 
4342*81cffd76SJoachim Henze #endif /* STAGING_CSMT */
4343c2c66affSColin Finck     wined3d_cs_emit_update_sub_resource(device->cs, resource, sub_resource_idx, box, data, row_pitch, depth_pitch);
4344c2c66affSColin Finck }
4345c2c66affSColin Finck 
43469987f029SAmine Khaldi void CDECL wined3d_device_resolve_sub_resource(struct wined3d_device *device,
43479987f029SAmine Khaldi         struct wined3d_resource *dst_resource, unsigned int dst_sub_resource_idx,
43489987f029SAmine Khaldi         struct wined3d_resource *src_resource, unsigned int src_sub_resource_idx,
43499987f029SAmine Khaldi         enum wined3d_format_id format_id)
43509987f029SAmine Khaldi {
43519987f029SAmine Khaldi     struct wined3d_texture *dst_texture, *src_texture;
43529987f029SAmine Khaldi     unsigned int dst_level, src_level;
43539987f029SAmine Khaldi     RECT dst_rect, src_rect;
43549987f029SAmine Khaldi 
43559987f029SAmine Khaldi     TRACE("device %p, dst_resource %p, dst_sub_resource_idx %u, "
43569987f029SAmine Khaldi             "src_resource %p, src_sub_resource_idx %u, format %s.\n",
43579987f029SAmine Khaldi             device, dst_resource, dst_sub_resource_idx,
43589987f029SAmine Khaldi             src_resource, src_sub_resource_idx, debug_d3dformat(format_id));
43599987f029SAmine Khaldi 
43609987f029SAmine Khaldi     if (wined3d_format_is_typeless(dst_resource->format)
43619987f029SAmine Khaldi             || wined3d_format_is_typeless(src_resource->format))
43629987f029SAmine Khaldi     {
43639987f029SAmine Khaldi         FIXME("Unhandled multisample resolve, dst_format %s, src_format %s, format %s.\n",
43649987f029SAmine Khaldi                 debug_d3dformat(dst_resource->format->id), debug_d3dformat(src_resource->format->id),
43659987f029SAmine Khaldi                 debug_d3dformat(format_id));
43669987f029SAmine Khaldi         return;
43679987f029SAmine Khaldi     }
43689987f029SAmine Khaldi     if (dst_resource->type != WINED3D_RTYPE_TEXTURE_2D)
43699987f029SAmine Khaldi     {
43709987f029SAmine Khaldi         WARN("Invalid destination resource type %s.\n", debug_d3dresourcetype(dst_resource->type));
43719987f029SAmine Khaldi         return;
43729987f029SAmine Khaldi     }
43739987f029SAmine Khaldi     if (src_resource->type != WINED3D_RTYPE_TEXTURE_2D)
43749987f029SAmine Khaldi     {
43759987f029SAmine Khaldi         WARN("Invalid source resource type %s.\n", debug_d3dresourcetype(src_resource->type));
43769987f029SAmine Khaldi         return;
43779987f029SAmine Khaldi     }
43789987f029SAmine Khaldi 
43799987f029SAmine Khaldi     dst_texture = texture_from_resource(dst_resource);
43809987f029SAmine Khaldi     src_texture = texture_from_resource(src_resource);
43819987f029SAmine Khaldi 
43829987f029SAmine Khaldi     dst_level = dst_sub_resource_idx % dst_texture->level_count;
43839987f029SAmine Khaldi     SetRect(&dst_rect, 0, 0, wined3d_texture_get_level_width(dst_texture, dst_level),
43849987f029SAmine Khaldi             wined3d_texture_get_level_height(dst_texture, dst_level));
43859987f029SAmine Khaldi     src_level = src_sub_resource_idx % src_texture->level_count;
43869987f029SAmine Khaldi     SetRect(&src_rect, 0, 0, wined3d_texture_get_level_width(src_texture, src_level),
43879987f029SAmine Khaldi             wined3d_texture_get_level_height(src_texture, src_level));
43889987f029SAmine Khaldi     wined3d_texture_blt(dst_texture, dst_sub_resource_idx, &dst_rect,
43899987f029SAmine Khaldi             src_texture, src_sub_resource_idx, &src_rect, 0, NULL, WINED3D_TEXF_POINT);
43909987f029SAmine Khaldi }
43919987f029SAmine Khaldi 
4392c2c66affSColin Finck HRESULT CDECL wined3d_device_clear_rendertarget_view(struct wined3d_device *device,
4393c2c66affSColin Finck         struct wined3d_rendertarget_view *view, const RECT *rect, DWORD flags,
4394c2c66affSColin Finck         const struct wined3d_color *color, float depth, DWORD stencil)
4395c2c66affSColin Finck {
4396c2c66affSColin Finck     struct wined3d_resource *resource;
4397c2c66affSColin Finck     RECT r;
4398c2c66affSColin Finck 
4399c2c66affSColin Finck     TRACE("device %p, view %p, rect %s, flags %#x, color %s, depth %.8e, stencil %u.\n",
4400c2c66affSColin Finck             device, view, wine_dbgstr_rect(rect), flags, debug_color(color), depth, stencil);
4401c2c66affSColin Finck 
4402c2c66affSColin Finck     if (!flags)
4403c2c66affSColin Finck         return WINED3D_OK;
4404c2c66affSColin Finck 
4405c2c66affSColin Finck     resource = view->resource;
4406*81cffd76SJoachim Henze     if (resource->type != WINED3D_RTYPE_TEXTURE_2D)
4407c2c66affSColin Finck     {
4408c2c66affSColin Finck         FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(resource->type));
4409c2c66affSColin Finck         return WINED3DERR_INVALIDCALL;
4410c2c66affSColin Finck     }
4411c2c66affSColin Finck 
4412c2c66affSColin Finck     if (view->layer_count > 1)
4413c2c66affSColin Finck     {
4414c2c66affSColin Finck         FIXME("Layered clears not implemented.\n");
4415c2c66affSColin Finck         return WINED3DERR_INVALIDCALL;
4416c2c66affSColin Finck     }
4417c2c66affSColin Finck 
4418c2c66affSColin Finck     if (!rect)
4419c2c66affSColin Finck     {
4420c2c66affSColin Finck         SetRect(&r, 0, 0, view->width, view->height);
4421c2c66affSColin Finck         rect = &r;
4422c2c66affSColin Finck     }
4423c2c66affSColin Finck     else
4424c2c66affSColin Finck     {
4425c2c66affSColin Finck         struct wined3d_box b = {rect->left, rect->top, rect->right, rect->bottom, 0, 1};
4426c2c66affSColin Finck         struct wined3d_texture *texture = texture_from_resource(view->resource);
4427c2c66affSColin Finck         HRESULT hr;
4428c2c66affSColin Finck 
4429c2c66affSColin Finck         if (FAILED(hr = wined3d_texture_check_box_dimensions(texture,
4430c2c66affSColin Finck                 view->sub_resource_idx % texture->level_count, &b)))
4431c2c66affSColin Finck             return hr;
4432c2c66affSColin Finck     }
4433c2c66affSColin Finck 
4434c2c66affSColin Finck     wined3d_cs_emit_clear_rendertarget_view(device->cs, view, rect, flags, color, depth, stencil);
4435c2c66affSColin Finck 
4436c2c66affSColin Finck     return WINED3D_OK;
4437c2c66affSColin Finck }
4438c2c66affSColin Finck 
4439c2c66affSColin Finck void CDECL wined3d_device_clear_unordered_access_view_uint(struct wined3d_device *device,
4440c2c66affSColin Finck         struct wined3d_unordered_access_view *view, const struct wined3d_uvec4 *clear_value)
4441c2c66affSColin Finck {
4442c2c66affSColin Finck     TRACE("device %p, view %p, clear_value %s.\n", device, view, debug_uvec4(clear_value));
4443c2c66affSColin Finck 
4444c2c66affSColin Finck     wined3d_cs_emit_clear_unordered_access_view_uint(device->cs, view, clear_value);
4445c2c66affSColin Finck }
4446c2c66affSColin Finck 
4447c2c66affSColin Finck struct wined3d_rendertarget_view * CDECL wined3d_device_get_rendertarget_view(const struct wined3d_device *device,
4448c2c66affSColin Finck         unsigned int view_idx)
4449c2c66affSColin Finck {
4450c2c66affSColin Finck     TRACE("device %p, view_idx %u.\n", device, view_idx);
4451c2c66affSColin Finck 
4452c2c66affSColin Finck     if (view_idx >= device->adapter->gl_info.limits.buffers)
4453c2c66affSColin Finck     {
4454c2c66affSColin Finck         WARN("Only %u render targets are supported.\n", device->adapter->gl_info.limits.buffers);
4455c2c66affSColin Finck         return NULL;
4456c2c66affSColin Finck     }
4457c2c66affSColin Finck 
4458c2c66affSColin Finck     return device->fb.render_targets[view_idx];
4459c2c66affSColin Finck }
4460c2c66affSColin Finck 
4461c2c66affSColin Finck struct wined3d_rendertarget_view * CDECL wined3d_device_get_depth_stencil_view(const struct wined3d_device *device)
4462c2c66affSColin Finck {
4463c2c66affSColin Finck     TRACE("device %p.\n", device);
4464c2c66affSColin Finck 
4465c2c66affSColin Finck     return device->fb.depth_stencil;
4466c2c66affSColin Finck }
4467c2c66affSColin Finck 
4468c2c66affSColin Finck HRESULT CDECL wined3d_device_set_rendertarget_view(struct wined3d_device *device,
4469c2c66affSColin Finck         unsigned int view_idx, struct wined3d_rendertarget_view *view, BOOL set_viewport)
4470c2c66affSColin Finck {
4471c2c66affSColin Finck     struct wined3d_rendertarget_view *prev;
4472c2c66affSColin Finck 
4473c2c66affSColin Finck     TRACE("device %p, view_idx %u, view %p, set_viewport %#x.\n",
4474c2c66affSColin Finck             device, view_idx, view, set_viewport);
4475c2c66affSColin Finck 
4476c2c66affSColin Finck     if (view_idx >= device->adapter->gl_info.limits.buffers)
4477c2c66affSColin Finck     {
4478c2c66affSColin Finck         WARN("Only %u render targets are supported.\n", device->adapter->gl_info.limits.buffers);
4479c2c66affSColin Finck         return WINED3DERR_INVALIDCALL;
4480c2c66affSColin Finck     }
4481c2c66affSColin Finck 
4482c2c66affSColin Finck     if (view && !(view->resource->usage & WINED3DUSAGE_RENDERTARGET))
4483c2c66affSColin Finck     {
4484c2c66affSColin Finck         WARN("View resource %p doesn't have render target usage.\n", view->resource);
4485c2c66affSColin Finck         return WINED3DERR_INVALIDCALL;
4486c2c66affSColin Finck     }
4487c2c66affSColin Finck 
4488c2c66affSColin Finck     /* Set the viewport and scissor rectangles, if requested. Tests show that
4489c2c66affSColin Finck      * stateblock recording is ignored, the change goes directly into the
4490c2c66affSColin Finck      * primary stateblock. */
4491c2c66affSColin Finck     if (!view_idx && set_viewport)
4492c2c66affSColin Finck     {
4493c2c66affSColin Finck         struct wined3d_state *state = &device->state;
4494c2c66affSColin Finck 
4495*81cffd76SJoachim Henze         state->viewport.x = 0;
4496*81cffd76SJoachim Henze         state->viewport.y = 0;
4497*81cffd76SJoachim Henze         state->viewport.width = view->width;
4498*81cffd76SJoachim Henze         state->viewport.height = view->height;
4499*81cffd76SJoachim Henze         state->viewport.min_z = 0.0f;
4500*81cffd76SJoachim Henze         state->viewport.max_z = 1.0f;
4501*81cffd76SJoachim Henze         wined3d_cs_emit_set_viewport(device->cs, &state->viewport);
4502c2c66affSColin Finck 
4503*81cffd76SJoachim Henze         SetRect(&state->scissor_rect, 0, 0, view->width, view->height);
4504*81cffd76SJoachim Henze         wined3d_cs_emit_set_scissor_rect(device->cs, &state->scissor_rect);
4505c2c66affSColin Finck     }
4506c2c66affSColin Finck 
4507*81cffd76SJoachim Henze 
4508c2c66affSColin Finck     prev = device->fb.render_targets[view_idx];
4509c2c66affSColin Finck     if (view == prev)
4510c2c66affSColin Finck         return WINED3D_OK;
4511c2c66affSColin Finck 
4512c2c66affSColin Finck     if (view)
4513c2c66affSColin Finck         wined3d_rendertarget_view_incref(view);
4514c2c66affSColin Finck     device->fb.render_targets[view_idx] = view;
4515c2c66affSColin Finck     wined3d_cs_emit_set_rendertarget_view(device->cs, view_idx, view);
4516c2c66affSColin Finck     /* Release after the assignment, to prevent device_resource_released()
4517c2c66affSColin Finck      * from seeing the surface as still in use. */
4518c2c66affSColin Finck     if (prev)
4519c2c66affSColin Finck         wined3d_rendertarget_view_decref(prev);
4520c2c66affSColin Finck 
4521c2c66affSColin Finck     return WINED3D_OK;
4522c2c66affSColin Finck }
4523c2c66affSColin Finck 
4524c2c66affSColin Finck void CDECL wined3d_device_set_depth_stencil_view(struct wined3d_device *device, struct wined3d_rendertarget_view *view)
4525c2c66affSColin Finck {
4526c2c66affSColin Finck     struct wined3d_rendertarget_view *prev;
4527c2c66affSColin Finck 
4528c2c66affSColin Finck     TRACE("device %p, view %p.\n", device, view);
4529c2c66affSColin Finck 
4530c2c66affSColin Finck     prev = device->fb.depth_stencil;
4531c2c66affSColin Finck     if (prev == view)
4532c2c66affSColin Finck     {
4533c2c66affSColin Finck         TRACE("Trying to do a NOP SetRenderTarget operation.\n");
4534c2c66affSColin Finck         return;
4535c2c66affSColin Finck     }
4536c2c66affSColin Finck 
4537c2c66affSColin Finck     if ((device->fb.depth_stencil = view))
4538c2c66affSColin Finck         wined3d_rendertarget_view_incref(view);
4539c2c66affSColin Finck     wined3d_cs_emit_set_depth_stencil_view(device->cs, view);
4540c2c66affSColin Finck     if (prev)
4541c2c66affSColin Finck         wined3d_rendertarget_view_decref(prev);
4542c2c66affSColin Finck }
4543c2c66affSColin Finck 
4544c2c66affSColin Finck static struct wined3d_texture *wined3d_device_create_cursor_texture(struct wined3d_device *device,
4545c2c66affSColin Finck         struct wined3d_texture *cursor_image, unsigned int sub_resource_idx)
4546c2c66affSColin Finck {
4547c2c66affSColin Finck     unsigned int texture_level = sub_resource_idx % cursor_image->level_count;
4548c2c66affSColin Finck     struct wined3d_sub_resource_data data;
4549c2c66affSColin Finck     struct wined3d_resource_desc desc;
4550c2c66affSColin Finck     struct wined3d_map_desc map_desc;
4551c2c66affSColin Finck     struct wined3d_texture *texture;
4552c2c66affSColin Finck     HRESULT hr;
4553c2c66affSColin Finck 
45549987f029SAmine Khaldi     if (FAILED(wined3d_resource_map(&cursor_image->resource, sub_resource_idx, &map_desc, NULL, WINED3D_MAP_READ)))
4555c2c66affSColin Finck     {
4556c2c66affSColin Finck         ERR("Failed to map source texture.\n");
4557c2c66affSColin Finck         return NULL;
4558c2c66affSColin Finck     }
4559c2c66affSColin Finck 
4560c2c66affSColin Finck     data.data = map_desc.data;
4561c2c66affSColin Finck     data.row_pitch = map_desc.row_pitch;
4562c2c66affSColin Finck     data.slice_pitch = map_desc.slice_pitch;
4563c2c66affSColin Finck 
4564c2c66affSColin Finck     desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
4565c2c66affSColin Finck     desc.format = WINED3DFMT_B8G8R8A8_UNORM;
4566c2c66affSColin Finck     desc.multisample_type = WINED3D_MULTISAMPLE_NONE;
4567c2c66affSColin Finck     desc.multisample_quality = 0;
4568c2c66affSColin Finck     desc.usage = WINED3DUSAGE_DYNAMIC;
45699987f029SAmine Khaldi     desc.access = WINED3D_RESOURCE_ACCESS_GPU;
4570c2c66affSColin Finck     desc.width = wined3d_texture_get_level_width(cursor_image, texture_level);
4571c2c66affSColin Finck     desc.height = wined3d_texture_get_level_height(cursor_image, texture_level);
4572c2c66affSColin Finck     desc.depth = 1;
4573c2c66affSColin Finck     desc.size = 0;
4574c2c66affSColin Finck 
4575c2c66affSColin Finck     hr = wined3d_texture_create(device, &desc, 1, 1, WINED3D_TEXTURE_CREATE_MAPPABLE,
4576c2c66affSColin Finck             &data, NULL, &wined3d_null_parent_ops, &texture);
4577c2c66affSColin Finck     wined3d_resource_unmap(&cursor_image->resource, sub_resource_idx);
4578c2c66affSColin Finck     if (FAILED(hr))
4579c2c66affSColin Finck     {
4580c2c66affSColin Finck         ERR("Failed to create cursor texture.\n");
4581c2c66affSColin Finck         return NULL;
4582c2c66affSColin Finck     }
4583c2c66affSColin Finck 
4584c2c66affSColin Finck     return texture;
4585c2c66affSColin Finck }
4586c2c66affSColin Finck 
4587c2c66affSColin Finck HRESULT CDECL wined3d_device_set_cursor_properties(struct wined3d_device *device,
4588c2c66affSColin Finck         UINT x_hotspot, UINT y_hotspot, struct wined3d_texture *texture, unsigned int sub_resource_idx)
4589c2c66affSColin Finck {
4590c2c66affSColin Finck     unsigned int texture_level = sub_resource_idx % texture->level_count;
4591c2c66affSColin Finck     unsigned int cursor_width, cursor_height;
4592c2c66affSColin Finck     struct wined3d_display_mode mode;
4593c2c66affSColin Finck     struct wined3d_map_desc map_desc;
4594c2c66affSColin Finck     HRESULT hr;
4595c2c66affSColin Finck 
4596c2c66affSColin Finck     TRACE("device %p, x_hotspot %u, y_hotspot %u, texture %p, sub_resource_idx %u.\n",
4597c2c66affSColin Finck             device, x_hotspot, y_hotspot, texture, sub_resource_idx);
4598c2c66affSColin Finck 
4599c2c66affSColin Finck     if (sub_resource_idx >= texture->level_count * texture->layer_count
4600c2c66affSColin Finck             || texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
4601c2c66affSColin Finck         return WINED3DERR_INVALIDCALL;
4602c2c66affSColin Finck 
4603c2c66affSColin Finck     if (device->cursor_texture)
4604c2c66affSColin Finck     {
4605c2c66affSColin Finck         wined3d_texture_decref(device->cursor_texture);
4606c2c66affSColin Finck         device->cursor_texture = NULL;
4607c2c66affSColin Finck     }
4608c2c66affSColin Finck 
4609c2c66affSColin Finck     if (texture->resource.format->id != WINED3DFMT_B8G8R8A8_UNORM)
4610c2c66affSColin Finck     {
4611c2c66affSColin Finck         WARN("Texture %p has invalid format %s.\n",
4612c2c66affSColin Finck                 texture, debug_d3dformat(texture->resource.format->id));
4613c2c66affSColin Finck         return WINED3DERR_INVALIDCALL;
4614c2c66affSColin Finck     }
4615c2c66affSColin Finck 
4616c2c66affSColin Finck     if (FAILED(hr = wined3d_get_adapter_display_mode(device->wined3d, device->adapter->ordinal, &mode, NULL)))
4617c2c66affSColin Finck     {
4618c2c66affSColin Finck         ERR("Failed to get display mode, hr %#x.\n", hr);
4619c2c66affSColin Finck         return WINED3DERR_INVALIDCALL;
4620c2c66affSColin Finck     }
4621c2c66affSColin Finck 
4622c2c66affSColin Finck     cursor_width = wined3d_texture_get_level_width(texture, texture_level);
4623c2c66affSColin Finck     cursor_height = wined3d_texture_get_level_height(texture, texture_level);
4624c2c66affSColin Finck     if (cursor_width > mode.width || cursor_height > mode.height)
4625c2c66affSColin Finck     {
4626c2c66affSColin Finck         WARN("Texture %p, sub-resource %u dimensions are %ux%u, but screen dimensions are %ux%u.\n",
4627c2c66affSColin Finck                 texture, sub_resource_idx, cursor_width, cursor_height, mode.width, mode.height);
4628c2c66affSColin Finck         return WINED3DERR_INVALIDCALL;
4629c2c66affSColin Finck     }
4630c2c66affSColin Finck 
4631c2c66affSColin Finck     /* TODO: MSDN: Cursor sizes must be a power of 2 */
4632c2c66affSColin Finck 
4633c2c66affSColin Finck     /* Do not store the surface's pointer because the application may
4634c2c66affSColin Finck      * release it after setting the cursor image. Windows doesn't
4635c2c66affSColin Finck      * addref the set surface, so we can't do this either without
4636c2c66affSColin Finck      * creating circular refcount dependencies. */
4637c2c66affSColin Finck     if (!(device->cursor_texture = wined3d_device_create_cursor_texture(device, texture, sub_resource_idx)))
4638c2c66affSColin Finck     {
4639c2c66affSColin Finck         ERR("Failed to create cursor texture.\n");
4640c2c66affSColin Finck         return WINED3DERR_INVALIDCALL;
4641c2c66affSColin Finck     }
4642c2c66affSColin Finck 
4643c2c66affSColin Finck     if (cursor_width == 32 && cursor_height == 32)
4644c2c66affSColin Finck     {
4645c2c66affSColin Finck         UINT mask_size = cursor_width * cursor_height / 8;
4646c2c66affSColin Finck         ICONINFO cursor_info;
4647c2c66affSColin Finck         DWORD *mask_bits;
4648c2c66affSColin Finck         HCURSOR cursor;
4649c2c66affSColin Finck 
4650c2c66affSColin Finck         /* 32-bit user32 cursors ignore the alpha channel if it's all
4651c2c66affSColin Finck          * zeroes, and use the mask instead. Fill the mask with all ones
4652c2c66affSColin Finck          * to ensure we still get a fully transparent cursor. */
46539987f029SAmine Khaldi         if (!(mask_bits = heap_alloc(mask_size)))
4654c2c66affSColin Finck             return E_OUTOFMEMORY;
4655c2c66affSColin Finck         memset(mask_bits, 0xff, mask_size);
4656c2c66affSColin Finck 
4657c2c66affSColin Finck         wined3d_resource_map(&texture->resource, sub_resource_idx, &map_desc, NULL,
46589987f029SAmine Khaldi                 WINED3D_MAP_NO_DIRTY_UPDATE | WINED3D_MAP_READ);
4659c2c66affSColin Finck         cursor_info.fIcon = FALSE;
4660c2c66affSColin Finck         cursor_info.xHotspot = x_hotspot;
4661c2c66affSColin Finck         cursor_info.yHotspot = y_hotspot;
4662c2c66affSColin Finck         cursor_info.hbmMask = CreateBitmap(cursor_width, cursor_height, 1, 1, mask_bits);
4663c2c66affSColin Finck         cursor_info.hbmColor = CreateBitmap(cursor_width, cursor_height, 1, 32, map_desc.data);
4664c2c66affSColin Finck         wined3d_resource_unmap(&texture->resource, sub_resource_idx);
4665c2c66affSColin Finck 
4666c2c66affSColin Finck         /* Create our cursor and clean up. */
4667c2c66affSColin Finck         cursor = CreateIconIndirect(&cursor_info);
4668c2c66affSColin Finck         if (cursor_info.hbmMask)
4669c2c66affSColin Finck             DeleteObject(cursor_info.hbmMask);
4670c2c66affSColin Finck         if (cursor_info.hbmColor)
4671c2c66affSColin Finck             DeleteObject(cursor_info.hbmColor);
4672c2c66affSColin Finck         if (device->hardwareCursor)
4673c2c66affSColin Finck             DestroyCursor(device->hardwareCursor);
4674c2c66affSColin Finck         device->hardwareCursor = cursor;
4675c2c66affSColin Finck         if (device->bCursorVisible)
4676c2c66affSColin Finck             SetCursor(cursor);
4677c2c66affSColin Finck 
46789987f029SAmine Khaldi         heap_free(mask_bits);
4679c2c66affSColin Finck     }
4680c2c66affSColin Finck 
4681c2c66affSColin Finck     TRACE("New cursor dimensions are %ux%u.\n", cursor_width, cursor_height);
4682c2c66affSColin Finck     device->cursorWidth = cursor_width;
4683c2c66affSColin Finck     device->cursorHeight = cursor_height;
4684c2c66affSColin Finck     device->xHotSpot = x_hotspot;
4685c2c66affSColin Finck     device->yHotSpot = y_hotspot;
4686c2c66affSColin Finck 
4687c2c66affSColin Finck     return WINED3D_OK;
4688c2c66affSColin Finck }
4689c2c66affSColin Finck 
4690c2c66affSColin Finck void CDECL wined3d_device_set_cursor_position(struct wined3d_device *device,
4691c2c66affSColin Finck         int x_screen_space, int y_screen_space, DWORD flags)
4692c2c66affSColin Finck {
4693c2c66affSColin Finck     TRACE("device %p, x %d, y %d, flags %#x.\n",
4694c2c66affSColin Finck             device, x_screen_space, y_screen_space, flags);
4695c2c66affSColin Finck 
4696c2c66affSColin Finck     device->xScreenSpace = x_screen_space;
4697c2c66affSColin Finck     device->yScreenSpace = y_screen_space;
4698c2c66affSColin Finck 
4699c2c66affSColin Finck     if (device->hardwareCursor)
4700c2c66affSColin Finck     {
4701c2c66affSColin Finck         POINT pt;
4702c2c66affSColin Finck 
4703c2c66affSColin Finck         GetCursorPos( &pt );
4704c2c66affSColin Finck         if (x_screen_space == pt.x && y_screen_space == pt.y)
4705c2c66affSColin Finck             return;
4706c2c66affSColin Finck         SetCursorPos( x_screen_space, y_screen_space );
4707c2c66affSColin Finck 
4708c2c66affSColin Finck         /* Switch to the software cursor if position diverges from the hardware one. */
4709c2c66affSColin Finck         GetCursorPos( &pt );
4710c2c66affSColin Finck         if (x_screen_space != pt.x || y_screen_space != pt.y)
4711c2c66affSColin Finck         {
4712c2c66affSColin Finck             if (device->bCursorVisible) SetCursor( NULL );
4713c2c66affSColin Finck             DestroyCursor( device->hardwareCursor );
4714c2c66affSColin Finck             device->hardwareCursor = 0;
4715c2c66affSColin Finck         }
4716c2c66affSColin Finck     }
4717c2c66affSColin Finck }
4718c2c66affSColin Finck 
4719c2c66affSColin Finck BOOL CDECL wined3d_device_show_cursor(struct wined3d_device *device, BOOL show)
4720c2c66affSColin Finck {
4721c2c66affSColin Finck     BOOL oldVisible = device->bCursorVisible;
4722c2c66affSColin Finck 
4723c2c66affSColin Finck     TRACE("device %p, show %#x.\n", device, show);
4724c2c66affSColin Finck 
4725c2c66affSColin Finck     /*
4726c2c66affSColin Finck      * When ShowCursor is first called it should make the cursor appear at the OS's last
4727c2c66affSColin Finck      * known cursor position.
4728c2c66affSColin Finck      */
4729c2c66affSColin Finck     if (show && !oldVisible)
4730c2c66affSColin Finck     {
4731c2c66affSColin Finck         POINT pt;
4732c2c66affSColin Finck         GetCursorPos(&pt);
4733c2c66affSColin Finck         device->xScreenSpace = pt.x;
4734c2c66affSColin Finck         device->yScreenSpace = pt.y;
4735c2c66affSColin Finck     }
4736c2c66affSColin Finck 
4737c2c66affSColin Finck     if (device->hardwareCursor)
4738c2c66affSColin Finck     {
4739c2c66affSColin Finck         device->bCursorVisible = show;
4740c2c66affSColin Finck         if (show)
4741c2c66affSColin Finck             SetCursor(device->hardwareCursor);
4742c2c66affSColin Finck         else
4743c2c66affSColin Finck             SetCursor(NULL);
4744c2c66affSColin Finck     }
4745c2c66affSColin Finck     else if (device->cursor_texture)
4746c2c66affSColin Finck     {
4747c2c66affSColin Finck         device->bCursorVisible = show;
4748c2c66affSColin Finck     }
4749c2c66affSColin Finck 
4750c2c66affSColin Finck     return oldVisible;
4751c2c66affSColin Finck }
4752c2c66affSColin Finck 
4753c2c66affSColin Finck void CDECL wined3d_device_evict_managed_resources(struct wined3d_device *device)
4754c2c66affSColin Finck {
4755c2c66affSColin Finck     struct wined3d_resource *resource, *cursor;
4756c2c66affSColin Finck 
4757c2c66affSColin Finck     TRACE("device %p.\n", device);
4758c2c66affSColin Finck 
4759c2c66affSColin Finck     LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
4760c2c66affSColin Finck     {
4761c2c66affSColin Finck         TRACE("Checking resource %p for eviction.\n", resource);
4762c2c66affSColin Finck 
47639987f029SAmine Khaldi         if (wined3d_resource_access_is_managed(resource->access) && !resource->map_count)
4764c2c66affSColin Finck         {
4765c2c66affSColin Finck             TRACE("Evicting %p.\n", resource);
4766c2c66affSColin Finck             wined3d_cs_emit_unload_resource(device->cs, resource);
4767c2c66affSColin Finck         }
4768c2c66affSColin Finck     }
4769c2c66affSColin Finck }
4770c2c66affSColin Finck 
4771c2c66affSColin Finck HRESULT CDECL wined3d_device_reset(struct wined3d_device *device,
4772c2c66affSColin Finck         const struct wined3d_swapchain_desc *swapchain_desc, const struct wined3d_display_mode *mode,
4773c2c66affSColin Finck         wined3d_device_reset_cb callback, BOOL reset_state)
4774c2c66affSColin Finck {
4775c2c66affSColin Finck     struct wined3d_resource *resource, *cursor;
4776c2c66affSColin Finck     struct wined3d_swapchain *swapchain;
4777c2c66affSColin Finck     struct wined3d_view_desc view_desc;
4778c2c66affSColin Finck     BOOL backbuffer_resized;
4779c2c66affSColin Finck     HRESULT hr = WINED3D_OK;
4780c2c66affSColin Finck     unsigned int i;
4781c2c66affSColin Finck 
4782c2c66affSColin Finck     TRACE("device %p, swapchain_desc %p, mode %p, callback %p, reset_state %#x.\n",
4783c2c66affSColin Finck             device, swapchain_desc, mode, callback, reset_state);
4784c2c66affSColin Finck 
4785c2c66affSColin Finck     device->cs->ops->finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
4786c2c66affSColin Finck 
4787c2c66affSColin Finck     if (!(swapchain = wined3d_device_get_swapchain(device, 0)))
4788c2c66affSColin Finck     {
4789c2c66affSColin Finck         ERR("Failed to get the first implicit swapchain.\n");
4790c2c66affSColin Finck         return WINED3DERR_INVALIDCALL;
4791c2c66affSColin Finck     }
4792c2c66affSColin Finck 
4793c2c66affSColin Finck     if (reset_state)
4794c2c66affSColin Finck     {
4795c2c66affSColin Finck         if (device->logo_texture)
4796c2c66affSColin Finck         {
4797c2c66affSColin Finck             wined3d_texture_decref(device->logo_texture);
4798c2c66affSColin Finck             device->logo_texture = NULL;
4799c2c66affSColin Finck         }
4800c2c66affSColin Finck         if (device->cursor_texture)
4801c2c66affSColin Finck         {
4802c2c66affSColin Finck             wined3d_texture_decref(device->cursor_texture);
4803c2c66affSColin Finck             device->cursor_texture = NULL;
4804c2c66affSColin Finck         }
4805c2c66affSColin Finck         state_unbind_resources(&device->state);
4806c2c66affSColin Finck     }
4807c2c66affSColin Finck 
4808c2c66affSColin Finck     for (i = 0; i < device->adapter->gl_info.limits.buffers; ++i)
4809c2c66affSColin Finck     {
4810c2c66affSColin Finck         wined3d_device_set_rendertarget_view(device, i, NULL, FALSE);
4811c2c66affSColin Finck     }
4812c2c66affSColin Finck     wined3d_device_set_depth_stencil_view(device, NULL);
4813c2c66affSColin Finck 
4814c2c66affSColin Finck     if (reset_state)
4815c2c66affSColin Finck     {
4816c2c66affSColin Finck         LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
4817c2c66affSColin Finck         {
4818c2c66affSColin Finck             TRACE("Enumerating resource %p.\n", resource);
4819c2c66affSColin Finck             if (FAILED(hr = callback(resource)))
4820c2c66affSColin Finck                 return hr;
4821c2c66affSColin Finck         }
4822c2c66affSColin Finck     }
4823c2c66affSColin Finck 
4824c2c66affSColin Finck     TRACE("New params:\n");
4825c2c66affSColin Finck     TRACE("backbuffer_width %u\n", swapchain_desc->backbuffer_width);
4826c2c66affSColin Finck     TRACE("backbuffer_height %u\n", swapchain_desc->backbuffer_height);
4827c2c66affSColin Finck     TRACE("backbuffer_format %s\n", debug_d3dformat(swapchain_desc->backbuffer_format));
4828c2c66affSColin Finck     TRACE("backbuffer_count %u\n", swapchain_desc->backbuffer_count);
4829c2c66affSColin Finck     TRACE("multisample_type %#x\n", swapchain_desc->multisample_type);
4830c2c66affSColin Finck     TRACE("multisample_quality %u\n", swapchain_desc->multisample_quality);
4831c2c66affSColin Finck     TRACE("swap_effect %#x\n", swapchain_desc->swap_effect);
4832c2c66affSColin Finck     TRACE("device_window %p\n", swapchain_desc->device_window);
4833c2c66affSColin Finck     TRACE("windowed %#x\n", swapchain_desc->windowed);
4834c2c66affSColin Finck     TRACE("enable_auto_depth_stencil %#x\n", swapchain_desc->enable_auto_depth_stencil);
4835c2c66affSColin Finck     if (swapchain_desc->enable_auto_depth_stencil)
4836c2c66affSColin Finck         TRACE("auto_depth_stencil_format %s\n", debug_d3dformat(swapchain_desc->auto_depth_stencil_format));
4837c2c66affSColin Finck     TRACE("flags %#x\n", swapchain_desc->flags);
4838c2c66affSColin Finck     TRACE("refresh_rate %u\n", swapchain_desc->refresh_rate);
4839*81cffd76SJoachim Henze     TRACE("swap_interval %u\n", swapchain_desc->swap_interval);
4840c2c66affSColin Finck     TRACE("auto_restore_display_mode %#x\n", swapchain_desc->auto_restore_display_mode);
4841c2c66affSColin Finck 
4842*81cffd76SJoachim Henze     if (swapchain_desc->backbuffer_usage != WINED3DUSAGE_RENDERTARGET)
48439987f029SAmine Khaldi         FIXME("Got unexpected backbuffer usage %#x.\n", swapchain_desc->backbuffer_usage);
48449987f029SAmine Khaldi 
48459987f029SAmine Khaldi     if (swapchain_desc->swap_effect != WINED3D_SWAP_EFFECT_DISCARD
48469987f029SAmine Khaldi             && swapchain_desc->swap_effect != WINED3D_SWAP_EFFECT_SEQUENTIAL
48479987f029SAmine Khaldi             && swapchain_desc->swap_effect != WINED3D_SWAP_EFFECT_COPY)
48489987f029SAmine Khaldi         FIXME("Unimplemented swap effect %#x.\n", swapchain_desc->swap_effect);
48499987f029SAmine Khaldi 
4850c2c66affSColin Finck     /* No special treatment of these parameters. Just store them */
4851c2c66affSColin Finck     swapchain->desc.swap_effect = swapchain_desc->swap_effect;
4852c2c66affSColin Finck     swapchain->desc.enable_auto_depth_stencil = swapchain_desc->enable_auto_depth_stencil;
4853c2c66affSColin Finck     swapchain->desc.auto_depth_stencil_format = swapchain_desc->auto_depth_stencil_format;
4854c2c66affSColin Finck     swapchain->desc.flags = swapchain_desc->flags;
4855c2c66affSColin Finck     swapchain->desc.refresh_rate = swapchain_desc->refresh_rate;
4856*81cffd76SJoachim Henze     swapchain->desc.swap_interval = swapchain_desc->swap_interval;
4857c2c66affSColin Finck     swapchain->desc.auto_restore_display_mode = swapchain_desc->auto_restore_display_mode;
4858c2c66affSColin Finck 
4859c2c66affSColin Finck     if (swapchain_desc->device_window
4860c2c66affSColin Finck             && swapchain_desc->device_window != swapchain->desc.device_window)
4861c2c66affSColin Finck     {
4862c2c66affSColin Finck         TRACE("Changing the device window from %p to %p.\n",
4863c2c66affSColin Finck                 swapchain->desc.device_window, swapchain_desc->device_window);
4864c2c66affSColin Finck         swapchain->desc.device_window = swapchain_desc->device_window;
4865c2c66affSColin Finck         swapchain->device_window = swapchain_desc->device_window;
4866c2c66affSColin Finck         wined3d_swapchain_set_window(swapchain, NULL);
4867c2c66affSColin Finck     }
4868c2c66affSColin Finck 
4869c2c66affSColin Finck     backbuffer_resized = swapchain_desc->backbuffer_width != swapchain->desc.backbuffer_width
4870c2c66affSColin Finck             || swapchain_desc->backbuffer_height != swapchain->desc.backbuffer_height;
4871c2c66affSColin Finck 
4872c2c66affSColin Finck     if (!swapchain_desc->windowed != !swapchain->desc.windowed
4873c2c66affSColin Finck             || swapchain->reapply_mode || mode
4874c2c66affSColin Finck             || (!swapchain_desc->windowed && backbuffer_resized))
4875c2c66affSColin Finck     {
4876c2c66affSColin Finck         if (FAILED(hr = wined3d_swapchain_set_fullscreen(swapchain, swapchain_desc, mode)))
4877c2c66affSColin Finck             return hr;
4878c2c66affSColin Finck     }
4879c2c66affSColin Finck     else if (!swapchain_desc->windowed)
4880c2c66affSColin Finck     {
4881c2c66affSColin Finck         DWORD style = device->style;
4882c2c66affSColin Finck         DWORD exStyle = device->exStyle;
4883c2c66affSColin Finck         /* If we're in fullscreen, and the mode wasn't changed, we have to get the window back into
4884c2c66affSColin Finck          * the right position. Some applications(Battlefield 2, Guild Wars) move it and then call
4885c2c66affSColin Finck          * Reset to clear up their mess. Guild Wars also loses the device during that.
4886c2c66affSColin Finck          */
4887c2c66affSColin Finck         device->style = 0;
4888c2c66affSColin Finck         device->exStyle = 0;
4889c2c66affSColin Finck         wined3d_device_setup_fullscreen_window(device, swapchain->device_window,
4890c2c66affSColin Finck                 swapchain_desc->backbuffer_width,
4891c2c66affSColin Finck                 swapchain_desc->backbuffer_height);
4892c2c66affSColin Finck         device->style = style;
4893c2c66affSColin Finck         device->exStyle = exStyle;
4894c2c66affSColin Finck     }
4895c2c66affSColin Finck 
4896c2c66affSColin Finck     if (FAILED(hr = wined3d_swapchain_resize_buffers(swapchain, swapchain_desc->backbuffer_count,
4897c2c66affSColin Finck             swapchain_desc->backbuffer_width, swapchain_desc->backbuffer_height, swapchain_desc->backbuffer_format,
4898c2c66affSColin Finck             swapchain_desc->multisample_type, swapchain_desc->multisample_quality)))
4899c2c66affSColin Finck         return hr;
4900c2c66affSColin Finck 
4901c2c66affSColin Finck     if (device->auto_depth_stencil_view)
4902c2c66affSColin Finck     {
4903c2c66affSColin Finck         wined3d_rendertarget_view_decref(device->auto_depth_stencil_view);
4904c2c66affSColin Finck         device->auto_depth_stencil_view = NULL;
4905c2c66affSColin Finck     }
4906c2c66affSColin Finck     if (swapchain->desc.enable_auto_depth_stencil)
4907c2c66affSColin Finck     {
4908c2c66affSColin Finck         struct wined3d_resource_desc texture_desc;
4909c2c66affSColin Finck         struct wined3d_texture *texture;
4910c2c66affSColin Finck         DWORD flags = 0;
4911c2c66affSColin Finck 
4912c2c66affSColin Finck         TRACE("Creating the depth stencil buffer.\n");
4913c2c66affSColin Finck 
4914c2c66affSColin Finck         texture_desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
4915c2c66affSColin Finck         texture_desc.format = swapchain->desc.auto_depth_stencil_format;
4916c2c66affSColin Finck         texture_desc.multisample_type = swapchain->desc.multisample_type;
4917c2c66affSColin Finck         texture_desc.multisample_quality = swapchain->desc.multisample_quality;
4918c2c66affSColin Finck         texture_desc.usage = WINED3DUSAGE_DEPTHSTENCIL;
49199987f029SAmine Khaldi         texture_desc.access = WINED3D_RESOURCE_ACCESS_GPU;
4920c2c66affSColin Finck         texture_desc.width = swapchain->desc.backbuffer_width;
4921c2c66affSColin Finck         texture_desc.height = swapchain->desc.backbuffer_height;
4922c2c66affSColin Finck         texture_desc.depth = 1;
4923c2c66affSColin Finck         texture_desc.size = 0;
4924c2c66affSColin Finck 
4925c2c66affSColin Finck         if (swapchain_desc->flags & WINED3D_SWAPCHAIN_GDI_COMPATIBLE)
4926c2c66affSColin Finck             flags |= WINED3D_TEXTURE_CREATE_GET_DC;
4927c2c66affSColin Finck 
4928c2c66affSColin Finck         if (FAILED(hr = device->device_parent->ops->create_swapchain_texture(device->device_parent,
4929c2c66affSColin Finck                 device->device_parent, &texture_desc, flags, &texture)))
4930c2c66affSColin Finck         {
4931c2c66affSColin Finck             ERR("Failed to create the auto depth/stencil surface, hr %#x.\n", hr);
4932c2c66affSColin Finck             return WINED3DERR_INVALIDCALL;
4933c2c66affSColin Finck         }
4934c2c66affSColin Finck 
4935c2c66affSColin Finck         view_desc.format_id = texture->resource.format->id;
4936c2c66affSColin Finck         view_desc.flags = 0;
4937c2c66affSColin Finck         view_desc.u.texture.level_idx = 0;
4938c2c66affSColin Finck         view_desc.u.texture.level_count = 1;
4939c2c66affSColin Finck         view_desc.u.texture.layer_idx = 0;
4940c2c66affSColin Finck         view_desc.u.texture.layer_count = 1;
4941c2c66affSColin Finck         hr = wined3d_rendertarget_view_create(&view_desc, &texture->resource,
4942c2c66affSColin Finck                 NULL, &wined3d_null_parent_ops, &device->auto_depth_stencil_view);
4943c2c66affSColin Finck         wined3d_texture_decref(texture);
4944c2c66affSColin Finck         if (FAILED(hr))
4945c2c66affSColin Finck         {
4946c2c66affSColin Finck             ERR("Failed to create rendertarget view, hr %#x.\n", hr);
4947c2c66affSColin Finck             return hr;
4948c2c66affSColin Finck         }
4949c2c66affSColin Finck 
4950c2c66affSColin Finck         wined3d_device_set_depth_stencil_view(device, device->auto_depth_stencil_view);
4951c2c66affSColin Finck     }
4952c2c66affSColin Finck 
4953c2c66affSColin Finck     if (device->back_buffer_view)
4954c2c66affSColin Finck     {
4955c2c66affSColin Finck         wined3d_rendertarget_view_decref(device->back_buffer_view);
4956c2c66affSColin Finck         device->back_buffer_view = NULL;
4957c2c66affSColin Finck     }
4958*81cffd76SJoachim Henze     if (swapchain->desc.backbuffer_count)
4959c2c66affSColin Finck     {
4960c2c66affSColin Finck         struct wined3d_resource *back_buffer = &swapchain->back_buffers[0]->resource;
4961c2c66affSColin Finck 
4962c2c66affSColin Finck         view_desc.format_id = back_buffer->format->id;
4963c2c66affSColin Finck         view_desc.flags = 0;
4964c2c66affSColin Finck         view_desc.u.texture.level_idx = 0;
4965c2c66affSColin Finck         view_desc.u.texture.level_count = 1;
4966c2c66affSColin Finck         view_desc.u.texture.layer_idx = 0;
4967c2c66affSColin Finck         view_desc.u.texture.layer_count = 1;
4968c2c66affSColin Finck         if (FAILED(hr = wined3d_rendertarget_view_create(&view_desc, back_buffer,
4969c2c66affSColin Finck                 NULL, &wined3d_null_parent_ops, &device->back_buffer_view)))
4970c2c66affSColin Finck         {
4971c2c66affSColin Finck             ERR("Failed to create rendertarget view, hr %#x.\n", hr);
4972c2c66affSColin Finck             return hr;
4973c2c66affSColin Finck         }
4974c2c66affSColin Finck     }
4975c2c66affSColin Finck 
4976c2c66affSColin Finck     wine_rb_clear(&device->samplers, device_free_sampler, NULL);
4977c2c66affSColin Finck 
4978c2c66affSColin Finck     if (reset_state)
4979c2c66affSColin Finck     {
4980c2c66affSColin Finck         TRACE("Resetting stateblock.\n");
4981c2c66affSColin Finck         if (device->recording)
4982c2c66affSColin Finck         {
4983c2c66affSColin Finck             wined3d_stateblock_decref(device->recording);
4984c2c66affSColin Finck             device->recording = NULL;
4985c2c66affSColin Finck         }
4986c2c66affSColin Finck         wined3d_cs_emit_reset_state(device->cs);
4987c2c66affSColin Finck         state_cleanup(&device->state);
4988c2c66affSColin Finck 
4989c2c66affSColin Finck         if (device->d3d_initialized)
4990c2c66affSColin Finck             wined3d_device_delete_opengl_contexts(device);
4991c2c66affSColin Finck 
4992c2c66affSColin Finck         memset(&device->state, 0, sizeof(device->state));
4993c2c66affSColin Finck         state_init(&device->state, &device->fb, &device->adapter->gl_info,
4994c2c66affSColin Finck                 &device->adapter->d3d_info, WINED3D_STATE_INIT_DEFAULT);
4995c2c66affSColin Finck         device->update_state = &device->state;
4996c2c66affSColin Finck 
4997c2c66affSColin Finck         device_init_swapchain_state(device, swapchain);
4998c2c66affSColin Finck         if (wined3d_settings.logo)
4999c2c66affSColin Finck             device_load_logo(device, wined3d_settings.logo);
5000c2c66affSColin Finck     }
5001c2c66affSColin Finck     else if (device->back_buffer_view)
5002c2c66affSColin Finck     {
5003c2c66affSColin Finck         struct wined3d_rendertarget_view *view = device->back_buffer_view;
5004c2c66affSColin Finck         struct wined3d_state *state = &device->state;
5005c2c66affSColin Finck 
5006c2c66affSColin Finck         wined3d_device_set_rendertarget_view(device, 0, view, FALSE);
5007c2c66affSColin Finck 
5008c2c66affSColin Finck         /* Note the min_z / max_z is not reset. */
5009*81cffd76SJoachim Henze         state->viewport.x = 0;
5010*81cffd76SJoachim Henze         state->viewport.y = 0;
5011*81cffd76SJoachim Henze         state->viewport.width = view->width;
5012*81cffd76SJoachim Henze         state->viewport.height = view->height;
5013*81cffd76SJoachim Henze         wined3d_cs_emit_set_viewport(device->cs, &state->viewport);
5014c2c66affSColin Finck 
5015*81cffd76SJoachim Henze         SetRect(&state->scissor_rect, 0, 0, view->width, view->height);
5016*81cffd76SJoachim Henze         wined3d_cs_emit_set_scissor_rect(device->cs, &state->scissor_rect);
5017c2c66affSColin Finck     }
5018c2c66affSColin Finck 
5019c2c66affSColin Finck     if (device->d3d_initialized)
5020c2c66affSColin Finck     {
5021c2c66affSColin Finck         if (reset_state)
5022c2c66affSColin Finck             hr = wined3d_device_create_primary_opengl_context(device);
5023*81cffd76SJoachim Henze         swapchain_update_swap_interval(swapchain);
5024c2c66affSColin Finck     }
5025c2c66affSColin Finck 
5026c2c66affSColin Finck     /* All done. There is no need to reload resources or shaders, this will happen automatically on the
5027c2c66affSColin Finck      * first use
5028c2c66affSColin Finck      */
5029c2c66affSColin Finck     return hr;
5030c2c66affSColin Finck }
5031c2c66affSColin Finck 
5032c2c66affSColin Finck HRESULT CDECL wined3d_device_set_dialog_box_mode(struct wined3d_device *device, BOOL enable_dialogs)
5033c2c66affSColin Finck {
5034c2c66affSColin Finck     TRACE("device %p, enable_dialogs %#x.\n", device, enable_dialogs);
5035c2c66affSColin Finck 
5036c2c66affSColin Finck     if (!enable_dialogs) FIXME("Dialogs cannot be disabled yet.\n");
5037c2c66affSColin Finck 
5038c2c66affSColin Finck     return WINED3D_OK;
5039c2c66affSColin Finck }
5040c2c66affSColin Finck 
5041c2c66affSColin Finck 
5042c2c66affSColin Finck void CDECL wined3d_device_get_creation_parameters(const struct wined3d_device *device,
5043c2c66affSColin Finck         struct wined3d_device_creation_parameters *parameters)
5044c2c66affSColin Finck {
5045c2c66affSColin Finck     TRACE("device %p, parameters %p.\n", device, parameters);
5046c2c66affSColin Finck 
5047c2c66affSColin Finck     *parameters = device->create_parms;
5048c2c66affSColin Finck }
5049c2c66affSColin Finck 
5050c2c66affSColin Finck struct wined3d * CDECL wined3d_device_get_wined3d(const struct wined3d_device *device)
5051c2c66affSColin Finck {
5052c2c66affSColin Finck     TRACE("device %p.\n", device);
5053c2c66affSColin Finck 
5054c2c66affSColin Finck     return device->wined3d;
5055c2c66affSColin Finck }
5056c2c66affSColin Finck 
5057c2c66affSColin Finck void CDECL wined3d_device_set_gamma_ramp(const struct wined3d_device *device,
5058c2c66affSColin Finck         UINT swapchain_idx, DWORD flags, const struct wined3d_gamma_ramp *ramp)
5059c2c66affSColin Finck {
5060c2c66affSColin Finck     struct wined3d_swapchain *swapchain;
5061c2c66affSColin Finck 
5062c2c66affSColin Finck     TRACE("device %p, swapchain_idx %u, flags %#x, ramp %p.\n",
5063c2c66affSColin Finck             device, swapchain_idx, flags, ramp);
5064c2c66affSColin Finck 
5065c2c66affSColin Finck     if ((swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
5066c2c66affSColin Finck         wined3d_swapchain_set_gamma_ramp(swapchain, flags, ramp);
5067c2c66affSColin Finck }
5068c2c66affSColin Finck 
5069c2c66affSColin Finck void CDECL wined3d_device_get_gamma_ramp(const struct wined3d_device *device,
5070c2c66affSColin Finck         UINT swapchain_idx, struct wined3d_gamma_ramp *ramp)
5071c2c66affSColin Finck {
5072c2c66affSColin Finck     struct wined3d_swapchain *swapchain;
5073c2c66affSColin Finck 
5074c2c66affSColin Finck     TRACE("device %p, swapchain_idx %u, ramp %p.\n",
5075c2c66affSColin Finck             device, swapchain_idx, ramp);
5076c2c66affSColin Finck 
5077c2c66affSColin Finck     if ((swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
5078c2c66affSColin Finck         wined3d_swapchain_get_gamma_ramp(swapchain, ramp);
5079c2c66affSColin Finck }
5080c2c66affSColin Finck 
5081c2c66affSColin Finck void device_resource_add(struct wined3d_device *device, struct wined3d_resource *resource)
5082c2c66affSColin Finck {
5083c2c66affSColin Finck     TRACE("device %p, resource %p.\n", device, resource);
5084c2c66affSColin Finck 
5085c2c66affSColin Finck     wined3d_not_from_cs(device->cs);
5086c2c66affSColin Finck 
5087c2c66affSColin Finck     list_add_head(&device->resources, &resource->resource_list_entry);
5088c2c66affSColin Finck }
5089c2c66affSColin Finck 
5090c2c66affSColin Finck static void device_resource_remove(struct wined3d_device *device, struct wined3d_resource *resource)
5091c2c66affSColin Finck {
5092c2c66affSColin Finck     TRACE("device %p, resource %p.\n", device, resource);
5093c2c66affSColin Finck 
5094c2c66affSColin Finck     wined3d_not_from_cs(device->cs);
5095c2c66affSColin Finck 
5096c2c66affSColin Finck     list_remove(&resource->resource_list_entry);
5097c2c66affSColin Finck }
5098c2c66affSColin Finck 
5099c2c66affSColin Finck void device_resource_released(struct wined3d_device *device, struct wined3d_resource *resource)
5100c2c66affSColin Finck {
5101c2c66affSColin Finck     enum wined3d_resource_type type = resource->type;
5102c2c66affSColin Finck     struct wined3d_rendertarget_view *rtv;
5103c2c66affSColin Finck     unsigned int i;
5104c2c66affSColin Finck 
5105c2c66affSColin Finck     TRACE("device %p, resource %p, type %s.\n", device, resource, debug_d3dresourcetype(type));
5106c2c66affSColin Finck 
5107c2c66affSColin Finck     if (device->d3d_initialized)
5108c2c66affSColin Finck     {
5109c2c66affSColin Finck         for (i = 0; i < device->adapter->gl_info.limits.buffers; ++i)
5110c2c66affSColin Finck         {
5111c2c66affSColin Finck             if ((rtv = device->fb.render_targets[i]) && rtv->resource == resource)
5112c2c66affSColin Finck                 ERR("Resource %p is still in use as render target %u.\n", resource, i);
5113c2c66affSColin Finck         }
5114c2c66affSColin Finck 
5115c2c66affSColin Finck         if ((rtv = device->fb.depth_stencil) && rtv->resource == resource)
5116c2c66affSColin Finck             ERR("Resource %p is still in use as depth/stencil buffer.\n", resource);
5117c2c66affSColin Finck     }
5118c2c66affSColin Finck 
5119c2c66affSColin Finck     switch (type)
5120c2c66affSColin Finck     {
51219987f029SAmine Khaldi         case WINED3D_RTYPE_TEXTURE_1D:
5122c2c66affSColin Finck         case WINED3D_RTYPE_TEXTURE_2D:
5123c2c66affSColin Finck         case WINED3D_RTYPE_TEXTURE_3D:
5124c2c66affSColin Finck             for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i)
5125c2c66affSColin Finck             {
5126*81cffd76SJoachim Henze                 struct wined3d_texture *texture = texture_from_resource(resource);
5127*81cffd76SJoachim Henze 
5128*81cffd76SJoachim Henze                 if (device->state.textures[i] == texture)
5129c2c66affSColin Finck                 {
5130*81cffd76SJoachim Henze                     ERR("Texture %p is still in use, stage %u.\n", texture, i);
5131c2c66affSColin Finck                     device->state.textures[i] = NULL;
5132c2c66affSColin Finck                 }
5133c2c66affSColin Finck 
5134*81cffd76SJoachim Henze                 if (device->recording && device->update_state->textures[i] == texture)
5135c2c66affSColin Finck                 {
5136*81cffd76SJoachim Henze                     ERR("Texture %p is still in use by recording stateblock %p, stage %u.\n",
5137*81cffd76SJoachim Henze                             texture, device->recording, i);
5138c2c66affSColin Finck                     device->update_state->textures[i] = NULL;
5139c2c66affSColin Finck                 }
5140c2c66affSColin Finck             }
5141c2c66affSColin Finck             break;
5142c2c66affSColin Finck 
5143c2c66affSColin Finck         case WINED3D_RTYPE_BUFFER:
5144*81cffd76SJoachim Henze             {
5145*81cffd76SJoachim Henze                 struct wined3d_buffer *buffer = buffer_from_resource(resource);
5146*81cffd76SJoachim Henze 
5147c2c66affSColin Finck                 for (i = 0; i < MAX_STREAMS; ++i)
5148c2c66affSColin Finck                 {
5149*81cffd76SJoachim Henze                     if (device->state.streams[i].buffer == buffer)
5150c2c66affSColin Finck                     {
5151*81cffd76SJoachim Henze                         ERR("Buffer %p is still in use, stream %u.\n", buffer, i);
5152c2c66affSColin Finck                         device->state.streams[i].buffer = NULL;
5153c2c66affSColin Finck                     }
5154c2c66affSColin Finck 
5155*81cffd76SJoachim Henze                     if (device->recording && device->update_state->streams[i].buffer == buffer)
5156c2c66affSColin Finck                     {
5157*81cffd76SJoachim Henze                         ERR("Buffer %p is still in use by stateblock %p, stream %u.\n",
5158*81cffd76SJoachim Henze                                 buffer, device->recording, i);
5159c2c66affSColin Finck                         device->update_state->streams[i].buffer = NULL;
5160c2c66affSColin Finck                     }
5161c2c66affSColin Finck                 }
5162c2c66affSColin Finck 
5163*81cffd76SJoachim Henze                 if (device->state.index_buffer == buffer)
5164c2c66affSColin Finck                 {
5165*81cffd76SJoachim Henze                     ERR("Buffer %p is still in use as index buffer.\n", buffer);
5166c2c66affSColin Finck                     device->state.index_buffer =  NULL;
5167c2c66affSColin Finck                 }
5168c2c66affSColin Finck 
5169*81cffd76SJoachim Henze                 if (device->recording && device->update_state->index_buffer == buffer)
5170c2c66affSColin Finck                 {
5171*81cffd76SJoachim Henze                     ERR("Buffer %p is still in use by stateblock %p as index buffer.\n",
5172*81cffd76SJoachim Henze                             buffer, device->recording);
5173c2c66affSColin Finck                     device->update_state->index_buffer =  NULL;
5174c2c66affSColin Finck                 }
5175*81cffd76SJoachim Henze             }
5176c2c66affSColin Finck             break;
5177c2c66affSColin Finck 
5178c2c66affSColin Finck         default:
5179c2c66affSColin Finck             break;
5180c2c66affSColin Finck     }
5181c2c66affSColin Finck 
5182c2c66affSColin Finck     /* Remove the resource from the resourceStore */
5183c2c66affSColin Finck     device_resource_remove(device, resource);
5184c2c66affSColin Finck 
5185c2c66affSColin Finck     TRACE("Resource released.\n");
5186c2c66affSColin Finck }
5187c2c66affSColin Finck 
5188c2c66affSColin Finck static int wined3d_sampler_compare(const void *key, const struct wine_rb_entry *entry)
5189c2c66affSColin Finck {
5190c2c66affSColin Finck     const struct wined3d_sampler *sampler = WINE_RB_ENTRY_VALUE(entry, struct wined3d_sampler, entry);
5191c2c66affSColin Finck 
5192c2c66affSColin Finck     return memcmp(&sampler->desc, key, sizeof(sampler->desc));
5193c2c66affSColin Finck }
5194c2c66affSColin Finck 
5195c2c66affSColin Finck HRESULT device_init(struct wined3d_device *device, struct wined3d *wined3d,
5196c2c66affSColin Finck         UINT adapter_idx, enum wined3d_device_type device_type, HWND focus_window, DWORD flags,
5197c2c66affSColin Finck         BYTE surface_alignment, struct wined3d_device_parent *device_parent)
5198c2c66affSColin Finck {
5199c2c66affSColin Finck     struct wined3d_adapter *adapter = &wined3d->adapters[adapter_idx];
5200c2c66affSColin Finck     const struct fragment_pipeline *fragment_pipeline;
5201c2c66affSColin Finck     const struct wined3d_vertex_pipe_ops *vertex_pipeline;
5202c2c66affSColin Finck     unsigned int i;
5203c2c66affSColin Finck     HRESULT hr;
5204c2c66affSColin Finck 
5205c2c66affSColin Finck     device->ref = 1;
5206c2c66affSColin Finck     device->wined3d = wined3d;
5207c2c66affSColin Finck     wined3d_incref(device->wined3d);
5208c2c66affSColin Finck     device->adapter = wined3d->adapter_count ? adapter : NULL;
5209c2c66affSColin Finck     device->device_parent = device_parent;
5210c2c66affSColin Finck     list_init(&device->resources);
5211c2c66affSColin Finck     list_init(&device->shaders);
5212c2c66affSColin Finck     device->surface_alignment = surface_alignment;
5213c2c66affSColin Finck 
5214c2c66affSColin Finck     /* Save the creation parameters. */
5215c2c66affSColin Finck     device->create_parms.adapter_idx = adapter_idx;
5216c2c66affSColin Finck     device->create_parms.device_type = device_type;
5217c2c66affSColin Finck     device->create_parms.focus_window = focus_window;
5218c2c66affSColin Finck     device->create_parms.flags = flags;
5219c2c66affSColin Finck 
5220c2c66affSColin Finck     device->shader_backend = adapter->shader_backend;
5221c2c66affSColin Finck 
5222c2c66affSColin Finck     vertex_pipeline = adapter->vertex_pipe;
5223c2c66affSColin Finck 
5224c2c66affSColin Finck     fragment_pipeline = adapter->fragment_pipe;
5225c2c66affSColin Finck 
5226c2c66affSColin Finck     wine_rb_init(&device->samplers, wined3d_sampler_compare);
5227c2c66affSColin Finck 
5228c2c66affSColin Finck     if (vertex_pipeline->vp_states && fragment_pipeline->states
5229c2c66affSColin Finck             && FAILED(hr = compile_state_table(device->StateTable, device->multistate_funcs,
5230c2c66affSColin Finck             &adapter->gl_info, &adapter->d3d_info, vertex_pipeline,
5231c2c66affSColin Finck             fragment_pipeline, misc_state_template)))
5232c2c66affSColin Finck     {
5233c2c66affSColin Finck         ERR("Failed to compile state table, hr %#x.\n", hr);
5234c2c66affSColin Finck         wine_rb_destroy(&device->samplers, NULL, NULL);
5235c2c66affSColin Finck         wined3d_decref(device->wined3d);
5236c2c66affSColin Finck         return hr;
5237c2c66affSColin Finck     }
5238c2c66affSColin Finck 
5239c2c66affSColin Finck     state_init(&device->state, &device->fb, &adapter->gl_info,
5240c2c66affSColin Finck             &adapter->d3d_info, WINED3D_STATE_INIT_DEFAULT);
5241c2c66affSColin Finck     device->update_state = &device->state;
5242c2c66affSColin Finck 
5243c2c66affSColin Finck     if (!(device->cs = wined3d_cs_create(device)))
5244c2c66affSColin Finck     {
5245c2c66affSColin Finck         WARN("Failed to create command stream.\n");
5246c2c66affSColin Finck         state_cleanup(&device->state);
5247c2c66affSColin Finck         hr = E_FAIL;
5248c2c66affSColin Finck         goto err;
5249c2c66affSColin Finck     }
5250c2c66affSColin Finck 
5251c2c66affSColin Finck     return WINED3D_OK;
5252c2c66affSColin Finck 
5253c2c66affSColin Finck err:
5254d6ac0a71SAmine Khaldi     for (i = 0; i < ARRAY_SIZE(device->multistate_funcs); ++i)
5255c2c66affSColin Finck     {
52569987f029SAmine Khaldi         heap_free(device->multistate_funcs[i]);
5257c2c66affSColin Finck     }
5258c2c66affSColin Finck     wine_rb_destroy(&device->samplers, NULL, NULL);
5259c2c66affSColin Finck     wined3d_decref(device->wined3d);
5260c2c66affSColin Finck     return hr;
5261c2c66affSColin Finck }
5262c2c66affSColin Finck 
5263c2c66affSColin Finck void device_invalidate_state(const struct wined3d_device *device, DWORD state)
5264c2c66affSColin Finck {
5265c2c66affSColin Finck     DWORD rep = device->StateTable[state].representative;
5266c2c66affSColin Finck     struct wined3d_context *context;
5267c2c66affSColin Finck     DWORD idx;
5268c2c66affSColin Finck     BYTE shift;
5269c2c66affSColin Finck     UINT i;
5270c2c66affSColin Finck 
5271c2c66affSColin Finck     wined3d_from_cs(device->cs);
5272c2c66affSColin Finck 
5273c2c66affSColin Finck     if (STATE_IS_COMPUTE(state))
5274c2c66affSColin Finck     {
5275c2c66affSColin Finck         for (i = 0; i < device->context_count; ++i)
5276c2c66affSColin Finck             context_invalidate_compute_state(device->contexts[i], state);
5277c2c66affSColin Finck         return;
5278c2c66affSColin Finck     }
5279c2c66affSColin Finck 
5280c2c66affSColin Finck     for (i = 0; i < device->context_count; ++i)
5281c2c66affSColin Finck     {
5282c2c66affSColin Finck         context = device->contexts[i];
5283c2c66affSColin Finck         if(isStateDirty(context, rep)) continue;
5284c2c66affSColin Finck 
5285c2c66affSColin Finck         context->dirtyArray[context->numDirtyEntries++] = rep;
5286c2c66affSColin Finck         idx = rep / (sizeof(*context->isStateDirty) * CHAR_BIT);
5287c2c66affSColin Finck         shift = rep & ((sizeof(*context->isStateDirty) * CHAR_BIT) - 1);
5288c2c66affSColin Finck         context->isStateDirty[idx] |= (1u << shift);
5289c2c66affSColin Finck     }
5290c2c66affSColin Finck }
5291c2c66affSColin Finck 
5292c2c66affSColin Finck LRESULT device_process_message(struct wined3d_device *device, HWND window, BOOL unicode,
5293c2c66affSColin Finck         UINT message, WPARAM wparam, LPARAM lparam, WNDPROC proc)
5294c2c66affSColin Finck {
5295c2c66affSColin Finck     if (device->filter_messages && message != WM_DISPLAYCHANGE)
5296c2c66affSColin Finck     {
5297c2c66affSColin Finck         TRACE("Filtering message: window %p, message %#x, wparam %#lx, lparam %#lx.\n",
5298c2c66affSColin Finck                 window, message, wparam, lparam);
5299c2c66affSColin Finck         if (unicode)
5300c2c66affSColin Finck             return DefWindowProcW(window, message, wparam, lparam);
5301c2c66affSColin Finck         else
5302c2c66affSColin Finck             return DefWindowProcA(window, message, wparam, lparam);
5303c2c66affSColin Finck     }
5304c2c66affSColin Finck 
5305c2c66affSColin Finck     if (message == WM_DESTROY)
5306c2c66affSColin Finck     {
5307c2c66affSColin Finck         TRACE("unregister window %p.\n", window);
5308c2c66affSColin Finck         wined3d_unregister_window(window);
5309c2c66affSColin Finck 
5310c2c66affSColin Finck         if (InterlockedCompareExchangePointer((void **)&device->focus_window, NULL, window) != window)
5311c2c66affSColin Finck             ERR("Window %p is not the focus window for device %p.\n", window, device);
5312c2c66affSColin Finck     }
5313c2c66affSColin Finck     else if (message == WM_DISPLAYCHANGE)
5314c2c66affSColin Finck     {
5315c2c66affSColin Finck         device->device_parent->ops->mode_changed(device->device_parent);
5316c2c66affSColin Finck     }
5317c2c66affSColin Finck     else if (message == WM_ACTIVATEAPP)
5318c2c66affSColin Finck     {
5319c2c66affSColin Finck         UINT i;
5320c2c66affSColin Finck 
5321c2c66affSColin Finck         for (i = 0; i < device->swapchain_count; i++)
5322c2c66affSColin Finck             wined3d_swapchain_activate(device->swapchains[i], wparam);
5323c2c66affSColin Finck 
5324c2c66affSColin Finck         device->device_parent->ops->activate(device->device_parent, wparam);
5325c2c66affSColin Finck     }
5326c2c66affSColin Finck     else if (message == WM_SYSCOMMAND)
5327c2c66affSColin Finck     {
5328c2c66affSColin Finck         if (wparam == SC_RESTORE && device->wined3d->flags & WINED3D_HANDLE_RESTORE)
5329c2c66affSColin Finck         {
5330c2c66affSColin Finck             if (unicode)
5331c2c66affSColin Finck                 DefWindowProcW(window, message, wparam, lparam);
5332c2c66affSColin Finck             else
5333c2c66affSColin Finck                 DefWindowProcA(window, message, wparam, lparam);
5334c2c66affSColin Finck         }
5335c2c66affSColin Finck     }
5336c2c66affSColin Finck 
5337c2c66affSColin Finck     if (unicode)
5338c2c66affSColin Finck         return CallWindowProcW(proc, window, message, wparam, lparam);
5339c2c66affSColin Finck     else
5340c2c66affSColin Finck         return CallWindowProcA(proc, window, message, wparam, lparam);
5341c2c66affSColin Finck }
5342*81cffd76SJoachim Henze #if defined(STAGING_CSMT)
5343*81cffd76SJoachim Henze 
5344*81cffd76SJoachim Henze /* Context activation is done by the caller */
5345*81cffd76SJoachim Henze struct wined3d_gl_bo *wined3d_device_get_bo(struct wined3d_device *device, UINT size, GLenum gl_usage,
5346*81cffd76SJoachim Henze         GLenum type_hint, struct wined3d_context *context)
5347*81cffd76SJoachim Henze {
5348*81cffd76SJoachim Henze     struct wined3d_gl_bo *ret;
5349*81cffd76SJoachim Henze     const struct wined3d_gl_info *gl_info;
5350*81cffd76SJoachim Henze 
5351*81cffd76SJoachim Henze     TRACE("device %p, size %u, gl_usage %u, type_hint %u\n", device, size, gl_usage,
5352*81cffd76SJoachim Henze             type_hint);
5353*81cffd76SJoachim Henze 
5354*81cffd76SJoachim Henze     ret = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ret));
5355*81cffd76SJoachim Henze     if(!ret)
5356*81cffd76SJoachim Henze         return NULL;
5357*81cffd76SJoachim Henze     ret->type_hint = type_hint;
5358*81cffd76SJoachim Henze     ret->size = size;
5359*81cffd76SJoachim Henze     ret->usage = gl_usage;
5360*81cffd76SJoachim Henze 
5361*81cffd76SJoachim Henze     gl_info = context->gl_info;
5362*81cffd76SJoachim Henze 
5363*81cffd76SJoachim Henze     GL_EXTCALL(glGenBuffers(1, &ret->name));
5364*81cffd76SJoachim Henze     if (type_hint == GL_ELEMENT_ARRAY_BUFFER)
5365*81cffd76SJoachim Henze         context_invalidate_state(context, STATE_INDEXBUFFER);
5366*81cffd76SJoachim Henze     GL_EXTCALL(glBindBuffer(type_hint, ret->name));
5367*81cffd76SJoachim Henze     GL_EXTCALL(glBufferData(type_hint, size, NULL, gl_usage));
5368*81cffd76SJoachim Henze     GL_EXTCALL(glBindBuffer(type_hint, 0));
5369*81cffd76SJoachim Henze     checkGLcall("Create buffer object");
5370*81cffd76SJoachim Henze 
5371*81cffd76SJoachim Henze     TRACE("Successfully created and set up buffer %u\n", ret->name);
5372*81cffd76SJoachim Henze     return ret;
5373*81cffd76SJoachim Henze }
5374*81cffd76SJoachim Henze 
5375*81cffd76SJoachim Henze /* Context activation is done by the caller */
5376*81cffd76SJoachim Henze static void wined3d_device_destroy_bo(struct wined3d_device *device, const struct wined3d_context *context,
5377*81cffd76SJoachim Henze         struct wined3d_gl_bo *bo)
5378*81cffd76SJoachim Henze {
5379*81cffd76SJoachim Henze     const struct wined3d_gl_info *gl_info = context->gl_info;
5380*81cffd76SJoachim Henze     TRACE("device %p, bo %p, GL bo %u\n", device, bo, bo->name);
5381*81cffd76SJoachim Henze 
5382*81cffd76SJoachim Henze     GL_EXTCALL(glDeleteBuffers(1, &bo->name));
5383*81cffd76SJoachim Henze     checkGLcall("glDeleteBuffers");
5384*81cffd76SJoachim Henze 
5385*81cffd76SJoachim Henze     HeapFree(GetProcessHeap(), 0, bo);
5386*81cffd76SJoachim Henze }
5387*81cffd76SJoachim Henze 
5388*81cffd76SJoachim Henze /* Context activation is done by the caller */
5389*81cffd76SJoachim Henze void wined3d_device_release_bo(struct wined3d_device *device, struct wined3d_gl_bo *bo,
5390*81cffd76SJoachim Henze         const struct wined3d_context *context)
5391*81cffd76SJoachim Henze {
5392*81cffd76SJoachim Henze     TRACE("device %p, bo %p, GL bo %u\n", device, bo, bo->name);
5393*81cffd76SJoachim Henze 
5394*81cffd76SJoachim Henze     wined3d_device_destroy_bo(device, context, bo);
5395*81cffd76SJoachim Henze }
5396*81cffd76SJoachim Henze #endif /* STAGING_CSMT */
5397