kernel32/tests: Add a test to check some fields in fake dlls.
[wine.git] / dlls / d3d9 / device.c
blob6cc3f180b094afcc9a2595e1c5d5284071e310c7
1 /*
2 * IDirect3DDevice9 implementation
4 * Copyright 2002-2005 Jason Edmeades
5 * Copyright 2002-2005 Raphael Junqueira
6 * Copyright 2005 Oliver Stieber
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "config.h"
24 #include "wine/port.h"
25 #include "d3d9_private.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(d3d9);
29 static void STDMETHODCALLTYPE d3d9_null_wined3d_object_destroyed(void *parent) {}
31 const struct wined3d_parent_ops d3d9_null_wined3d_parent_ops =
33 d3d9_null_wined3d_object_destroyed,
36 static void wined3d_color_from_d3dcolor(struct wined3d_color *wined3d_colour, D3DCOLOR d3d_colour)
38 wined3d_colour->r = ((d3d_colour >> 16) & 0xff) / 255.0f;
39 wined3d_colour->g = ((d3d_colour >> 8) & 0xff) / 255.0f;
40 wined3d_colour->b = (d3d_colour & 0xff) / 255.0f;
41 wined3d_colour->a = ((d3d_colour >> 24) & 0xff) / 255.0f;
44 D3DFORMAT d3dformat_from_wined3dformat(enum wined3d_format_id format)
46 BYTE *c = (BYTE *)&format;
48 /* Don't translate FOURCC formats */
49 if (isprint(c[0]) && isprint(c[1]) && isprint(c[2]) && isprint(c[3])) return format;
51 switch(format)
53 case WINED3DFMT_UNKNOWN: return D3DFMT_UNKNOWN;
54 case WINED3DFMT_B8G8R8_UNORM: return D3DFMT_R8G8B8;
55 case WINED3DFMT_B8G8R8A8_UNORM: return D3DFMT_A8R8G8B8;
56 case WINED3DFMT_B8G8R8X8_UNORM: return D3DFMT_X8R8G8B8;
57 case WINED3DFMT_B5G6R5_UNORM: return D3DFMT_R5G6B5;
58 case WINED3DFMT_B5G5R5X1_UNORM: return D3DFMT_X1R5G5B5;
59 case WINED3DFMT_B5G5R5A1_UNORM: return D3DFMT_A1R5G5B5;
60 case WINED3DFMT_B4G4R4A4_UNORM: return D3DFMT_A4R4G4B4;
61 case WINED3DFMT_B2G3R3_UNORM: return D3DFMT_R3G3B2;
62 case WINED3DFMT_A8_UNORM: return D3DFMT_A8;
63 case WINED3DFMT_B2G3R3A8_UNORM: return D3DFMT_A8R3G3B2;
64 case WINED3DFMT_B4G4R4X4_UNORM: return D3DFMT_X4R4G4B4;
65 case WINED3DFMT_R10G10B10A2_UNORM: return D3DFMT_A2B10G10R10;
66 case WINED3DFMT_R8G8B8A8_UNORM: return D3DFMT_A8B8G8R8;
67 case WINED3DFMT_R8G8B8X8_UNORM: return D3DFMT_X8B8G8R8;
68 case WINED3DFMT_R16G16_UNORM: return D3DFMT_G16R16;
69 case WINED3DFMT_B10G10R10A2_UNORM: return D3DFMT_A2R10G10B10;
70 case WINED3DFMT_R16G16B16A16_UNORM: return D3DFMT_A16B16G16R16;
71 case WINED3DFMT_P8_UINT_A8_UNORM: return D3DFMT_A8P8;
72 case WINED3DFMT_P8_UINT: return D3DFMT_P8;
73 case WINED3DFMT_L8_UNORM: return D3DFMT_L8;
74 case WINED3DFMT_L8A8_UNORM: return D3DFMT_A8L8;
75 case WINED3DFMT_L4A4_UNORM: return D3DFMT_A4L4;
76 case WINED3DFMT_R8G8_SNORM: return D3DFMT_V8U8;
77 case WINED3DFMT_R5G5_SNORM_L6_UNORM: return D3DFMT_L6V5U5;
78 case WINED3DFMT_R8G8_SNORM_L8X8_UNORM: return D3DFMT_X8L8V8U8;
79 case WINED3DFMT_R8G8B8A8_SNORM: return D3DFMT_Q8W8V8U8;
80 case WINED3DFMT_R16G16_SNORM: return D3DFMT_V16U16;
81 case WINED3DFMT_R10G10B10_SNORM_A2_UNORM: return D3DFMT_A2W10V10U10;
82 case WINED3DFMT_D16_LOCKABLE: return D3DFMT_D16_LOCKABLE;
83 case WINED3DFMT_D32_UNORM: return D3DFMT_D32;
84 case WINED3DFMT_S1_UINT_D15_UNORM: return D3DFMT_D15S1;
85 case WINED3DFMT_D24_UNORM_S8_UINT: return D3DFMT_D24S8;
86 case WINED3DFMT_X8D24_UNORM: return D3DFMT_D24X8;
87 case WINED3DFMT_S4X4_UINT_D24_UNORM: return D3DFMT_D24X4S4;
88 case WINED3DFMT_D16_UNORM: return D3DFMT_D16;
89 case WINED3DFMT_L16_UNORM: return D3DFMT_L16;
90 case WINED3DFMT_D32_FLOAT: return D3DFMT_D32F_LOCKABLE;
91 case WINED3DFMT_S8_UINT_D24_FLOAT: return D3DFMT_D24FS8;
92 case WINED3DFMT_R16_UINT: return D3DFMT_INDEX16;
93 case WINED3DFMT_R32_UINT: return D3DFMT_INDEX32;
94 case WINED3DFMT_R16G16B16A16_SNORM: return D3DFMT_Q16W16V16U16;
95 case WINED3DFMT_R16_FLOAT: return D3DFMT_R16F;
96 case WINED3DFMT_R16G16_FLOAT: return D3DFMT_G16R16F;
97 case WINED3DFMT_R16G16B16A16_FLOAT: return D3DFMT_A16B16G16R16F;
98 case WINED3DFMT_R32_FLOAT: return D3DFMT_R32F;
99 case WINED3DFMT_R32G32_FLOAT: return D3DFMT_G32R32F;
100 case WINED3DFMT_R32G32B32A32_FLOAT: return D3DFMT_A32B32G32R32F;
101 case WINED3DFMT_R8G8_SNORM_Cx: return D3DFMT_CxV8U8;
102 default:
103 FIXME("Unhandled wined3d format %#x.\n", format);
104 return D3DFMT_UNKNOWN;
108 enum wined3d_format_id wined3dformat_from_d3dformat(D3DFORMAT format)
110 BYTE *c = (BYTE *)&format;
112 /* Don't translate FOURCC formats */
113 if (isprint(c[0]) && isprint(c[1]) && isprint(c[2]) && isprint(c[3])) return format;
115 switch(format)
117 case D3DFMT_UNKNOWN: return WINED3DFMT_UNKNOWN;
118 case D3DFMT_R8G8B8: return WINED3DFMT_B8G8R8_UNORM;
119 case D3DFMT_A8R8G8B8: return WINED3DFMT_B8G8R8A8_UNORM;
120 case D3DFMT_X8R8G8B8: return WINED3DFMT_B8G8R8X8_UNORM;
121 case D3DFMT_R5G6B5: return WINED3DFMT_B5G6R5_UNORM;
122 case D3DFMT_X1R5G5B5: return WINED3DFMT_B5G5R5X1_UNORM;
123 case D3DFMT_A1R5G5B5: return WINED3DFMT_B5G5R5A1_UNORM;
124 case D3DFMT_A4R4G4B4: return WINED3DFMT_B4G4R4A4_UNORM;
125 case D3DFMT_R3G3B2: return WINED3DFMT_B2G3R3_UNORM;
126 case D3DFMT_A8: return WINED3DFMT_A8_UNORM;
127 case D3DFMT_A8R3G3B2: return WINED3DFMT_B2G3R3A8_UNORM;
128 case D3DFMT_X4R4G4B4: return WINED3DFMT_B4G4R4X4_UNORM;
129 case D3DFMT_A2B10G10R10: return WINED3DFMT_R10G10B10A2_UNORM;
130 case D3DFMT_A8B8G8R8: return WINED3DFMT_R8G8B8A8_UNORM;
131 case D3DFMT_X8B8G8R8: return WINED3DFMT_R8G8B8X8_UNORM;
132 case D3DFMT_G16R16: return WINED3DFMT_R16G16_UNORM;
133 case D3DFMT_A2R10G10B10: return WINED3DFMT_B10G10R10A2_UNORM;
134 case D3DFMT_A16B16G16R16: return WINED3DFMT_R16G16B16A16_UNORM;
135 case D3DFMT_A8P8: return WINED3DFMT_P8_UINT_A8_UNORM;
136 case D3DFMT_P8: return WINED3DFMT_P8_UINT;
137 case D3DFMT_L8: return WINED3DFMT_L8_UNORM;
138 case D3DFMT_A8L8: return WINED3DFMT_L8A8_UNORM;
139 case D3DFMT_A4L4: return WINED3DFMT_L4A4_UNORM;
140 case D3DFMT_V8U8: return WINED3DFMT_R8G8_SNORM;
141 case D3DFMT_L6V5U5: return WINED3DFMT_R5G5_SNORM_L6_UNORM;
142 case D3DFMT_X8L8V8U8: return WINED3DFMT_R8G8_SNORM_L8X8_UNORM;
143 case D3DFMT_Q8W8V8U8: return WINED3DFMT_R8G8B8A8_SNORM;
144 case D3DFMT_V16U16: return WINED3DFMT_R16G16_SNORM;
145 case D3DFMT_A2W10V10U10: return WINED3DFMT_R10G10B10_SNORM_A2_UNORM;
146 case D3DFMT_D16_LOCKABLE: return WINED3DFMT_D16_LOCKABLE;
147 case D3DFMT_D32: return WINED3DFMT_D32_UNORM;
148 case D3DFMT_D15S1: return WINED3DFMT_S1_UINT_D15_UNORM;
149 case D3DFMT_D24S8: return WINED3DFMT_D24_UNORM_S8_UINT;
150 case D3DFMT_D24X8: return WINED3DFMT_X8D24_UNORM;
151 case D3DFMT_D24X4S4: return WINED3DFMT_S4X4_UINT_D24_UNORM;
152 case D3DFMT_D16: return WINED3DFMT_D16_UNORM;
153 case D3DFMT_L16: return WINED3DFMT_L16_UNORM;
154 case D3DFMT_D32F_LOCKABLE: return WINED3DFMT_D32_FLOAT;
155 case D3DFMT_D24FS8: return WINED3DFMT_S8_UINT_D24_FLOAT;
156 case D3DFMT_INDEX16: return WINED3DFMT_R16_UINT;
157 case D3DFMT_INDEX32: return WINED3DFMT_R32_UINT;
158 case D3DFMT_Q16W16V16U16: return WINED3DFMT_R16G16B16A16_SNORM;
159 case D3DFMT_R16F: return WINED3DFMT_R16_FLOAT;
160 case D3DFMT_G16R16F: return WINED3DFMT_R16G16_FLOAT;
161 case D3DFMT_A16B16G16R16F: return WINED3DFMT_R16G16B16A16_FLOAT;
162 case D3DFMT_R32F: return WINED3DFMT_R32_FLOAT;
163 case D3DFMT_G32R32F: return WINED3DFMT_R32G32_FLOAT;
164 case D3DFMT_A32B32G32R32F: return WINED3DFMT_R32G32B32A32_FLOAT;
165 case D3DFMT_CxV8U8: return WINED3DFMT_R8G8_SNORM_Cx;
166 default:
167 FIXME("Unhandled D3DFORMAT %#x.\n", format);
168 return WINED3DFMT_UNKNOWN;
172 unsigned int wined3dmapflags_from_d3dmapflags(unsigned int flags, unsigned int usage)
174 static const unsigned int handled = D3DLOCK_NOSYSLOCK
175 | D3DLOCK_NOOVERWRITE
176 | D3DLOCK_DISCARD
177 | D3DLOCK_DONOTWAIT
178 | D3DLOCK_NO_DIRTY_UPDATE;
179 unsigned int wined3d_flags;
181 wined3d_flags = flags & handled;
182 if (~usage & D3DUSAGE_WRITEONLY && !(flags & (D3DLOCK_NOOVERWRITE | D3DLOCK_DISCARD)))
183 wined3d_flags |= WINED3D_MAP_READ;
184 if (!(flags & D3DLOCK_READONLY))
185 wined3d_flags |= WINED3D_MAP_WRITE;
186 if (!(wined3d_flags & (WINED3D_MAP_READ | WINED3D_MAP_WRITE)))
187 wined3d_flags |= WINED3D_MAP_WRITE;
188 flags &= ~(handled | D3DLOCK_READONLY);
190 if (flags)
191 FIXME("Unhandled flags %#x.\n", flags);
193 return wined3d_flags;
196 static UINT vertex_count_from_primitive_count(D3DPRIMITIVETYPE primitive_type, UINT primitive_count)
198 switch (primitive_type)
200 case D3DPT_POINTLIST:
201 return primitive_count;
203 case D3DPT_LINELIST:
204 return primitive_count * 2;
206 case D3DPT_LINESTRIP:
207 return primitive_count + 1;
209 case D3DPT_TRIANGLELIST:
210 return primitive_count * 3;
212 case D3DPT_TRIANGLESTRIP:
213 case D3DPT_TRIANGLEFAN:
214 return primitive_count + 2;
216 default:
217 FIXME("Unhandled primitive type %#x.\n", primitive_type);
218 return 0;
222 static D3DSWAPEFFECT d3dswapeffect_from_wined3dswapeffect(enum wined3d_swap_effect effect)
224 switch (effect)
226 case WINED3D_SWAP_EFFECT_DISCARD:
227 return D3DSWAPEFFECT_DISCARD;
228 case WINED3D_SWAP_EFFECT_SEQUENTIAL:
229 return D3DSWAPEFFECT_FLIP;
230 case WINED3D_SWAP_EFFECT_COPY:
231 return D3DSWAPEFFECT_COPY;
232 case WINED3D_SWAP_EFFECT_OVERLAY:
233 return D3DSWAPEFFECT_OVERLAY;
234 case WINED3D_SWAP_EFFECT_FLIP_SEQUENTIAL:
235 return D3DSWAPEFFECT_FLIPEX;
236 default:
237 FIXME("Unhandled swap effect %#x.\n", effect);
238 return D3DSWAPEFFECT_FLIP;
242 void present_parameters_from_wined3d_swapchain_desc(D3DPRESENT_PARAMETERS *present_parameters,
243 const struct wined3d_swapchain_desc *swapchain_desc, DWORD presentation_interval)
245 present_parameters->BackBufferWidth = swapchain_desc->backbuffer_width;
246 present_parameters->BackBufferHeight = swapchain_desc->backbuffer_height;
247 present_parameters->BackBufferFormat = d3dformat_from_wined3dformat(swapchain_desc->backbuffer_format);
248 present_parameters->BackBufferCount = swapchain_desc->backbuffer_count;
249 present_parameters->MultiSampleType = swapchain_desc->multisample_type;
250 present_parameters->MultiSampleQuality = swapchain_desc->multisample_quality;
251 present_parameters->SwapEffect = d3dswapeffect_from_wined3dswapeffect(swapchain_desc->swap_effect);
252 present_parameters->hDeviceWindow = swapchain_desc->device_window;
253 present_parameters->Windowed = swapchain_desc->windowed;
254 present_parameters->EnableAutoDepthStencil = swapchain_desc->enable_auto_depth_stencil;
255 present_parameters->AutoDepthStencilFormat
256 = d3dformat_from_wined3dformat(swapchain_desc->auto_depth_stencil_format);
257 present_parameters->Flags = swapchain_desc->flags & D3DPRESENTFLAGS_MASK;
258 present_parameters->FullScreen_RefreshRateInHz = swapchain_desc->refresh_rate;
259 present_parameters->PresentationInterval = presentation_interval;
262 static enum wined3d_swap_effect wined3dswapeffect_from_d3dswapeffect(D3DSWAPEFFECT effect)
264 switch (effect)
266 case D3DSWAPEFFECT_DISCARD:
267 return WINED3D_SWAP_EFFECT_DISCARD;
268 case D3DSWAPEFFECT_FLIP:
269 return WINED3D_SWAP_EFFECT_SEQUENTIAL;
270 case D3DSWAPEFFECT_COPY:
271 return WINED3D_SWAP_EFFECT_COPY;
272 case D3DSWAPEFFECT_OVERLAY:
273 return WINED3D_SWAP_EFFECT_OVERLAY;
274 case D3DSWAPEFFECT_FLIPEX:
275 return WINED3D_SWAP_EFFECT_FLIP_SEQUENTIAL;
276 default:
277 FIXME("Unhandled swap effect %#x.\n", effect);
278 return WINED3D_SWAP_EFFECT_SEQUENTIAL;
282 static enum wined3d_swap_interval wined3dswapinterval_from_d3d(DWORD interval)
284 switch (interval)
286 case D3DPRESENT_INTERVAL_IMMEDIATE:
287 return WINED3D_SWAP_INTERVAL_IMMEDIATE;
288 case D3DPRESENT_INTERVAL_ONE:
289 return WINED3D_SWAP_INTERVAL_ONE;
290 case D3DPRESENT_INTERVAL_TWO:
291 return WINED3D_SWAP_INTERVAL_TWO;
292 case D3DPRESENT_INTERVAL_THREE:
293 return WINED3D_SWAP_INTERVAL_THREE;
294 case D3DPRESENT_INTERVAL_FOUR:
295 return WINED3D_SWAP_INTERVAL_FOUR;
296 default:
297 FIXME("Unhandled presentation interval %#x.\n", interval);
298 case D3DPRESENT_INTERVAL_DEFAULT:
299 return WINED3D_SWAP_INTERVAL_DEFAULT;
303 static BOOL wined3d_swapchain_desc_from_present_parameters(struct wined3d_swapchain_desc *swapchain_desc,
304 const D3DPRESENT_PARAMETERS *present_parameters, BOOL extended)
306 D3DSWAPEFFECT highest_swapeffect = extended ? D3DSWAPEFFECT_FLIPEX : D3DSWAPEFFECT_COPY;
307 UINT highest_bb_count = extended ? 30 : 3;
309 if (!present_parameters->SwapEffect || present_parameters->SwapEffect > highest_swapeffect)
311 WARN("Invalid swap effect %u passed.\n", present_parameters->SwapEffect);
312 return FALSE;
314 if (present_parameters->BackBufferCount > highest_bb_count
315 || (present_parameters->SwapEffect == D3DSWAPEFFECT_COPY
316 && present_parameters->BackBufferCount > 1))
318 WARN("Invalid backbuffer count %u.\n", present_parameters->BackBufferCount);
319 return FALSE;
321 switch (present_parameters->PresentationInterval)
323 case D3DPRESENT_INTERVAL_DEFAULT:
324 case D3DPRESENT_INTERVAL_ONE:
325 case D3DPRESENT_INTERVAL_TWO:
326 case D3DPRESENT_INTERVAL_THREE:
327 case D3DPRESENT_INTERVAL_FOUR:
328 case D3DPRESENT_INTERVAL_IMMEDIATE:
329 break;
330 default:
331 WARN("Invalid presentation interval %#x.\n", present_parameters->PresentationInterval);
332 return FALSE;
335 swapchain_desc->backbuffer_width = present_parameters->BackBufferWidth;
336 swapchain_desc->backbuffer_height = present_parameters->BackBufferHeight;
337 swapchain_desc->backbuffer_format = wined3dformat_from_d3dformat(present_parameters->BackBufferFormat);
338 swapchain_desc->backbuffer_count = max(1, present_parameters->BackBufferCount);
339 swapchain_desc->backbuffer_bind_flags = WINED3D_BIND_RENDER_TARGET;
340 swapchain_desc->multisample_type = present_parameters->MultiSampleType;
341 swapchain_desc->multisample_quality = present_parameters->MultiSampleQuality;
342 swapchain_desc->swap_effect = wined3dswapeffect_from_d3dswapeffect(present_parameters->SwapEffect);
343 swapchain_desc->device_window = present_parameters->hDeviceWindow;
344 swapchain_desc->windowed = present_parameters->Windowed;
345 swapchain_desc->enable_auto_depth_stencil = present_parameters->EnableAutoDepthStencil;
346 swapchain_desc->auto_depth_stencil_format
347 = wined3dformat_from_d3dformat(present_parameters->AutoDepthStencilFormat);
348 swapchain_desc->flags
349 = (present_parameters->Flags & D3DPRESENTFLAGS_MASK) | WINED3D_SWAPCHAIN_ALLOW_MODE_SWITCH;
350 if ((present_parameters->Flags & D3DPRESENTFLAG_LOCKABLE_BACKBUFFER)
351 && (is_gdi_compat_wined3dformat(swapchain_desc->backbuffer_format)
352 /* WINED3DFMT_UNKNOWN creates the swapchain with the current
353 * display format, which is always GDI-compatible. */
354 || swapchain_desc->backbuffer_format == WINED3DFMT_UNKNOWN))
355 swapchain_desc->flags |= WINED3D_SWAPCHAIN_GDI_COMPATIBLE;
356 swapchain_desc->refresh_rate = present_parameters->FullScreen_RefreshRateInHz;
357 swapchain_desc->auto_restore_display_mode = TRUE;
359 if (present_parameters->Flags & ~D3DPRESENTFLAGS_MASK)
360 FIXME("Unhandled flags %#x.\n", present_parameters->Flags & ~D3DPRESENTFLAGS_MASK);
362 return TRUE;
365 void d3dcaps_from_wined3dcaps(D3DCAPS9 *caps, const struct wined3d_caps *wined3d_caps)
367 static const DWORD ps_minor_version[] = {0, 4, 0, 0};
368 static const DWORD vs_minor_version[] = {0, 1, 0, 0};
369 static const DWORD texture_filter_caps =
370 D3DPTFILTERCAPS_MINFPOINT | D3DPTFILTERCAPS_MINFLINEAR | D3DPTFILTERCAPS_MINFANISOTROPIC |
371 D3DPTFILTERCAPS_MINFPYRAMIDALQUAD | D3DPTFILTERCAPS_MINFGAUSSIANQUAD|
372 D3DPTFILTERCAPS_MIPFPOINT | D3DPTFILTERCAPS_MIPFLINEAR | D3DPTFILTERCAPS_MAGFPOINT |
373 D3DPTFILTERCAPS_MAGFLINEAR |D3DPTFILTERCAPS_MAGFANISOTROPIC|D3DPTFILTERCAPS_MAGFPYRAMIDALQUAD|
374 D3DPTFILTERCAPS_MAGFGAUSSIANQUAD;
376 caps->DeviceType = (D3DDEVTYPE)wined3d_caps->DeviceType;
377 caps->AdapterOrdinal = wined3d_caps->AdapterOrdinal;
378 caps->Caps = wined3d_caps->Caps;
379 caps->Caps2 = wined3d_caps->Caps2;
380 caps->Caps3 = wined3d_caps->Caps3;
381 caps->PresentationIntervals = D3DPRESENT_INTERVAL_IMMEDIATE | D3DPRESENT_INTERVAL_ONE;
382 caps->CursorCaps = wined3d_caps->CursorCaps;
383 caps->DevCaps = wined3d_caps->DevCaps;
384 caps->PrimitiveMiscCaps = wined3d_caps->PrimitiveMiscCaps;
385 caps->RasterCaps = wined3d_caps->RasterCaps;
386 caps->ZCmpCaps = wined3d_caps->ZCmpCaps;
387 caps->SrcBlendCaps = wined3d_caps->SrcBlendCaps;
388 caps->DestBlendCaps = wined3d_caps->DestBlendCaps;
389 caps->AlphaCmpCaps = wined3d_caps->AlphaCmpCaps;
390 caps->ShadeCaps = wined3d_caps->ShadeCaps;
391 caps->TextureCaps = wined3d_caps->TextureCaps;
392 caps->TextureFilterCaps = wined3d_caps->TextureFilterCaps;
393 caps->CubeTextureFilterCaps = wined3d_caps->CubeTextureFilterCaps;
394 caps->VolumeTextureFilterCaps = wined3d_caps->VolumeTextureFilterCaps;
395 caps->TextureAddressCaps = wined3d_caps->TextureAddressCaps;
396 caps->VolumeTextureAddressCaps = wined3d_caps->VolumeTextureAddressCaps;
397 caps->LineCaps = wined3d_caps->LineCaps;
398 caps->MaxTextureWidth = wined3d_caps->MaxTextureWidth;
399 caps->MaxTextureHeight = wined3d_caps->MaxTextureHeight;
400 caps->MaxVolumeExtent = wined3d_caps->MaxVolumeExtent;
401 caps->MaxTextureRepeat = wined3d_caps->MaxTextureRepeat;
402 caps->MaxTextureAspectRatio = wined3d_caps->MaxTextureAspectRatio;
403 caps->MaxAnisotropy = wined3d_caps->MaxAnisotropy;
404 caps->MaxVertexW = wined3d_caps->MaxVertexW;
405 caps->GuardBandLeft = wined3d_caps->GuardBandLeft;
406 caps->GuardBandTop = wined3d_caps->GuardBandTop;
407 caps->GuardBandRight = wined3d_caps->GuardBandRight;
408 caps->GuardBandBottom = wined3d_caps->GuardBandBottom;
409 caps->ExtentsAdjust = wined3d_caps->ExtentsAdjust;
410 caps->StencilCaps = wined3d_caps->StencilCaps;
411 caps->FVFCaps = wined3d_caps->FVFCaps;
412 caps->TextureOpCaps = wined3d_caps->TextureOpCaps;
413 caps->MaxTextureBlendStages = wined3d_caps->MaxTextureBlendStages;
414 caps->MaxSimultaneousTextures = wined3d_caps->MaxSimultaneousTextures;
415 caps->VertexProcessingCaps = wined3d_caps->VertexProcessingCaps;
416 caps->MaxActiveLights = wined3d_caps->MaxActiveLights;
417 caps->MaxUserClipPlanes = wined3d_caps->MaxUserClipPlanes;
418 caps->MaxVertexBlendMatrices = wined3d_caps->MaxVertexBlendMatrices;
419 caps->MaxVertexBlendMatrixIndex = wined3d_caps->MaxVertexBlendMatrixIndex;
420 caps->MaxPointSize = wined3d_caps->MaxPointSize;
421 caps->MaxPrimitiveCount = wined3d_caps->MaxPrimitiveCount;
422 caps->MaxVertexIndex = wined3d_caps->MaxVertexIndex;
423 caps->MaxStreams = wined3d_caps->MaxStreams;
424 caps->MaxStreamStride = wined3d_caps->MaxStreamStride;
425 caps->VertexShaderVersion = wined3d_caps->VertexShaderVersion;
426 caps->MaxVertexShaderConst = wined3d_caps->MaxVertexShaderConst;
427 caps->PixelShaderVersion = wined3d_caps->PixelShaderVersion;
428 caps->PixelShader1xMaxValue = wined3d_caps->PixelShader1xMaxValue;
429 caps->DevCaps2 = wined3d_caps->DevCaps2;
430 caps->MaxNpatchTessellationLevel = wined3d_caps->MaxNpatchTessellationLevel;
431 caps->MasterAdapterOrdinal = wined3d_caps->MasterAdapterOrdinal;
432 caps->AdapterOrdinalInGroup = wined3d_caps->AdapterOrdinalInGroup;
433 caps->NumberOfAdaptersInGroup = wined3d_caps->NumberOfAdaptersInGroup;
434 caps->DeclTypes = wined3d_caps->DeclTypes;
435 caps->NumSimultaneousRTs = wined3d_caps->NumSimultaneousRTs;
436 caps->StretchRectFilterCaps = wined3d_caps->StretchRectFilterCaps;
437 caps->VS20Caps.Caps = wined3d_caps->VS20Caps.caps;
438 caps->VS20Caps.DynamicFlowControlDepth = wined3d_caps->VS20Caps.dynamic_flow_control_depth;
439 caps->VS20Caps.NumTemps = wined3d_caps->VS20Caps.temp_count;
440 caps->VS20Caps.StaticFlowControlDepth = wined3d_caps->VS20Caps.static_flow_control_depth;
441 caps->PS20Caps.Caps = wined3d_caps->PS20Caps.caps;
442 caps->PS20Caps.DynamicFlowControlDepth = wined3d_caps->PS20Caps.dynamic_flow_control_depth;
443 caps->PS20Caps.NumTemps = wined3d_caps->PS20Caps.temp_count;
444 caps->PS20Caps.StaticFlowControlDepth = wined3d_caps->PS20Caps.static_flow_control_depth;
445 caps->PS20Caps.NumInstructionSlots = wined3d_caps->PS20Caps.instruction_slot_count;
446 caps->VertexTextureFilterCaps = wined3d_caps->VertexTextureFilterCaps;
447 caps->MaxVShaderInstructionsExecuted = wined3d_caps->MaxVShaderInstructionsExecuted;
448 caps->MaxPShaderInstructionsExecuted = wined3d_caps->MaxPShaderInstructionsExecuted;
449 caps->MaxVertexShader30InstructionSlots = wined3d_caps->MaxVertexShader30InstructionSlots;
450 caps->MaxPixelShader30InstructionSlots = wined3d_caps->MaxPixelShader30InstructionSlots;
452 /* Some functionality is implemented in d3d9.dll, not wined3d.dll. Add the needed caps. */
453 caps->DevCaps2 |= D3DDEVCAPS2_CAN_STRETCHRECT_FROM_TEXTURES;
455 /* Filter wined3d caps. */
456 caps->TextureFilterCaps &= texture_filter_caps;
457 caps->CubeTextureFilterCaps &= texture_filter_caps;
458 caps->VolumeTextureFilterCaps &= texture_filter_caps;
460 caps->DevCaps &=
461 D3DDEVCAPS_EXECUTESYSTEMMEMORY | D3DDEVCAPS_EXECUTEVIDEOMEMORY | D3DDEVCAPS_TLVERTEXSYSTEMMEMORY |
462 D3DDEVCAPS_TLVERTEXVIDEOMEMORY | D3DDEVCAPS_TEXTURESYSTEMMEMORY| D3DDEVCAPS_TEXTUREVIDEOMEMORY |
463 D3DDEVCAPS_DRAWPRIMTLVERTEX | D3DDEVCAPS_CANRENDERAFTERFLIP | D3DDEVCAPS_TEXTURENONLOCALVIDMEM|
464 D3DDEVCAPS_DRAWPRIMITIVES2 | D3DDEVCAPS_SEPARATETEXTUREMEMORIES |
465 D3DDEVCAPS_DRAWPRIMITIVES2EX | D3DDEVCAPS_HWTRANSFORMANDLIGHT| D3DDEVCAPS_CANBLTSYSTONONLOCAL |
466 D3DDEVCAPS_HWRASTERIZATION | D3DDEVCAPS_PUREDEVICE | D3DDEVCAPS_QUINTICRTPATCHES |
467 D3DDEVCAPS_RTPATCHES | D3DDEVCAPS_RTPATCHHANDLEZERO | D3DDEVCAPS_NPATCHES;
469 caps->ShadeCaps &=
470 D3DPSHADECAPS_COLORGOURAUDRGB | D3DPSHADECAPS_SPECULARGOURAUDRGB |
471 D3DPSHADECAPS_ALPHAGOURAUDBLEND | D3DPSHADECAPS_FOGGOURAUD;
473 caps->RasterCaps &=
474 D3DPRASTERCAPS_DITHER | D3DPRASTERCAPS_ZTEST | D3DPRASTERCAPS_FOGVERTEX |
475 D3DPRASTERCAPS_FOGTABLE | D3DPRASTERCAPS_MIPMAPLODBIAS | D3DPRASTERCAPS_ZBUFFERLESSHSR |
476 D3DPRASTERCAPS_FOGRANGE | D3DPRASTERCAPS_ANISOTROPY | D3DPRASTERCAPS_WBUFFER |
477 D3DPRASTERCAPS_WFOG | D3DPRASTERCAPS_ZFOG | D3DPRASTERCAPS_COLORPERSPECTIVE |
478 D3DPRASTERCAPS_SCISSORTEST | D3DPRASTERCAPS_SLOPESCALEDEPTHBIAS |
479 D3DPRASTERCAPS_DEPTHBIAS | D3DPRASTERCAPS_MULTISAMPLE_TOGGLE;
481 caps->DevCaps2 &=
482 D3DDEVCAPS2_STREAMOFFSET | D3DDEVCAPS2_DMAPNPATCH | D3DDEVCAPS2_ADAPTIVETESSRTPATCH |
483 D3DDEVCAPS2_ADAPTIVETESSNPATCH | D3DDEVCAPS2_CAN_STRETCHRECT_FROM_TEXTURES |
484 D3DDEVCAPS2_PRESAMPLEDDMAPNPATCH| D3DDEVCAPS2_VERTEXELEMENTSCANSHARESTREAMOFFSET;
486 caps->Caps2 &=
487 D3DCAPS2_FULLSCREENGAMMA | D3DCAPS2_CANCALIBRATEGAMMA | D3DCAPS2_RESERVED |
488 D3DCAPS2_CANMANAGERESOURCE | D3DCAPS2_DYNAMICTEXTURES | D3DCAPS2_CANAUTOGENMIPMAP;
490 caps->VertexProcessingCaps &=
491 D3DVTXPCAPS_TEXGEN | D3DVTXPCAPS_MATERIALSOURCE7 | D3DVTXPCAPS_DIRECTIONALLIGHTS |
492 D3DVTXPCAPS_POSITIONALLIGHTS | D3DVTXPCAPS_LOCALVIEWER | D3DVTXPCAPS_TWEENING |
493 D3DVTXPCAPS_TEXGEN_SPHEREMAP | D3DVTXPCAPS_NO_TEXGEN_NONLOCALVIEWER;
495 caps->TextureCaps &=
496 D3DPTEXTURECAPS_PERSPECTIVE | D3DPTEXTURECAPS_POW2 | D3DPTEXTURECAPS_ALPHA |
497 D3DPTEXTURECAPS_SQUAREONLY | D3DPTEXTURECAPS_TEXREPEATNOTSCALEDBYSIZE |
498 D3DPTEXTURECAPS_ALPHAPALETTE | D3DPTEXTURECAPS_NONPOW2CONDITIONAL |
499 D3DPTEXTURECAPS_PROJECTED | D3DPTEXTURECAPS_CUBEMAP | D3DPTEXTURECAPS_VOLUMEMAP |
500 D3DPTEXTURECAPS_MIPMAP | D3DPTEXTURECAPS_MIPVOLUMEMAP | D3DPTEXTURECAPS_MIPCUBEMAP |
501 D3DPTEXTURECAPS_CUBEMAP_POW2 | D3DPTEXTURECAPS_VOLUMEMAP_POW2| D3DPTEXTURECAPS_NOPROJECTEDBUMPENV;
503 caps->MaxVertexShaderConst = min(D3D9_MAX_VERTEX_SHADER_CONSTANTF, caps->MaxVertexShaderConst);
504 caps->NumSimultaneousRTs = min(D3D_MAX_SIMULTANEOUS_RENDERTARGETS, caps->NumSimultaneousRTs);
506 if (caps->PixelShaderVersion > 3)
508 caps->PixelShaderVersion = D3DPS_VERSION(3, 0);
510 else
512 DWORD major = caps->PixelShaderVersion;
513 caps->PixelShaderVersion = D3DPS_VERSION(major, ps_minor_version[major]);
516 if (caps->VertexShaderVersion > 3)
518 caps->VertexShaderVersion = D3DVS_VERSION(3, 0);
520 else
522 DWORD major = caps->VertexShaderVersion;
523 caps->VertexShaderVersion = D3DVS_VERSION(major, vs_minor_version[major]);
527 static HRESULT WINAPI d3d9_device_QueryInterface(IDirect3DDevice9Ex *iface, REFIID riid, void **out)
529 TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
531 if (IsEqualGUID(riid, &IID_IDirect3DDevice9)
532 || IsEqualGUID(riid, &IID_IUnknown))
534 IDirect3DDevice9Ex_AddRef(iface);
535 *out = iface;
536 return S_OK;
539 if (IsEqualGUID(riid, &IID_IDirect3DDevice9Ex))
541 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
543 /* Find out if the creating d3d9 interface was created with Direct3DCreate9Ex.
544 * It doesn't matter with which function the device was created. */
545 if (!device->d3d_parent->extended)
547 WARN("IDirect3D9 instance wasn't created with CreateDirect3D9Ex, returning E_NOINTERFACE.\n");
548 *out = NULL;
549 return E_NOINTERFACE;
552 IDirect3DDevice9Ex_AddRef(iface);
553 *out = iface;
554 return S_OK;
557 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
559 *out = NULL;
560 return E_NOINTERFACE;
563 static ULONG WINAPI d3d9_device_AddRef(IDirect3DDevice9Ex *iface)
565 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
566 ULONG refcount = InterlockedIncrement(&device->refcount);
568 TRACE("%p increasing refcount to %u.\n", iface, refcount);
570 return refcount;
573 static ULONG WINAPI DECLSPEC_HOTPATCH d3d9_device_Release(IDirect3DDevice9Ex *iface)
575 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
576 ULONG refcount;
578 if (device->in_destruction)
579 return 0;
581 refcount = InterlockedDecrement(&device->refcount);
583 TRACE("%p decreasing refcount to %u.\n", iface, refcount);
585 if (!refcount)
587 unsigned i;
588 device->in_destruction = TRUE;
590 wined3d_mutex_lock();
591 for (i = 0; i < device->fvf_decl_count; ++i)
593 wined3d_vertex_declaration_decref(device->fvf_decls[i].decl);
595 heap_free(device->fvf_decls);
597 if (device->vertex_buffer)
598 wined3d_buffer_decref(device->vertex_buffer);
599 if (device->index_buffer)
600 wined3d_buffer_decref(device->index_buffer);
602 heap_free(device->implicit_swapchains);
604 wined3d_device_uninit_3d(device->wined3d_device);
605 wined3d_device_release_focus_window(device->wined3d_device);
606 wined3d_device_decref(device->wined3d_device);
607 wined3d_mutex_unlock();
609 IDirect3D9Ex_Release(&device->d3d_parent->IDirect3D9Ex_iface);
611 heap_free(device);
614 return refcount;
617 static HRESULT WINAPI d3d9_device_TestCooperativeLevel(IDirect3DDevice9Ex *iface)
619 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
621 TRACE("iface %p.\n", iface);
623 TRACE("device state: %#x.\n", device->device_state);
625 if (device->d3d_parent->extended)
626 return D3D_OK;
628 switch (device->device_state)
630 default:
631 case D3D9_DEVICE_STATE_OK:
632 return D3D_OK;
633 case D3D9_DEVICE_STATE_LOST:
634 return D3DERR_DEVICELOST;
635 case D3D9_DEVICE_STATE_NOT_RESET:
636 return D3DERR_DEVICENOTRESET;
640 static UINT WINAPI d3d9_device_GetAvailableTextureMem(IDirect3DDevice9Ex *iface)
642 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
643 UINT ret;
645 TRACE("iface %p.\n", iface);
647 wined3d_mutex_lock();
648 ret = wined3d_device_get_available_texture_mem(device->wined3d_device);
649 wined3d_mutex_unlock();
651 return ret;
654 static HRESULT WINAPI d3d9_device_EvictManagedResources(IDirect3DDevice9Ex *iface)
656 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
658 TRACE("iface %p.\n", iface);
660 wined3d_mutex_lock();
661 wined3d_device_evict_managed_resources(device->wined3d_device);
662 wined3d_mutex_unlock();
664 return D3D_OK;
667 static HRESULT WINAPI d3d9_device_GetDirect3D(IDirect3DDevice9Ex *iface, IDirect3D9 **d3d9)
669 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
671 TRACE("iface %p, d3d9 %p.\n", iface, d3d9);
673 if (!d3d9)
674 return D3DERR_INVALIDCALL;
676 return IDirect3D9Ex_QueryInterface(&device->d3d_parent->IDirect3D9Ex_iface, &IID_IDirect3D9, (void **)d3d9);
679 static HRESULT WINAPI d3d9_device_GetDeviceCaps(IDirect3DDevice9Ex *iface, D3DCAPS9 *caps)
681 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
682 struct wined3d_caps wined3d_caps;
683 HRESULT hr;
685 TRACE("iface %p, caps %p.\n", iface, caps);
687 if (!caps)
688 return D3DERR_INVALIDCALL;
690 memset(caps, 0, sizeof(*caps));
692 wined3d_mutex_lock();
693 hr = wined3d_device_get_device_caps(device->wined3d_device, &wined3d_caps);
694 wined3d_mutex_unlock();
696 d3dcaps_from_wined3dcaps(caps, &wined3d_caps);
698 return hr;
701 static HRESULT WINAPI d3d9_device_GetDisplayMode(IDirect3DDevice9Ex *iface, UINT swapchain, D3DDISPLAYMODE *mode)
703 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
704 struct wined3d_display_mode wined3d_mode;
705 HRESULT hr;
707 TRACE("iface %p, swapchain %u, mode %p.\n", iface, swapchain, mode);
709 wined3d_mutex_lock();
710 hr = wined3d_device_get_display_mode(device->wined3d_device, swapchain, &wined3d_mode, NULL);
711 wined3d_mutex_unlock();
713 if (SUCCEEDED(hr))
715 mode->Width = wined3d_mode.width;
716 mode->Height = wined3d_mode.height;
717 mode->RefreshRate = wined3d_mode.refresh_rate;
718 mode->Format = d3dformat_from_wined3dformat(wined3d_mode.format_id);
721 return hr;
724 static HRESULT WINAPI d3d9_device_GetCreationParameters(IDirect3DDevice9Ex *iface,
725 D3DDEVICE_CREATION_PARAMETERS *parameters)
727 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
729 TRACE("iface %p, parameters %p.\n", iface, parameters);
731 wined3d_mutex_lock();
732 wined3d_device_get_creation_parameters(device->wined3d_device,
733 (struct wined3d_device_creation_parameters *)parameters);
734 wined3d_mutex_unlock();
736 return D3D_OK;
739 static HRESULT WINAPI d3d9_device_SetCursorProperties(IDirect3DDevice9Ex *iface,
740 UINT hotspot_x, UINT hotspot_y, IDirect3DSurface9 *bitmap)
742 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
743 struct d3d9_surface *bitmap_impl = unsafe_impl_from_IDirect3DSurface9(bitmap);
744 HRESULT hr;
746 TRACE("iface %p, hotspot_x %u, hotspot_y %u, bitmap %p.\n",
747 iface, hotspot_x, hotspot_y, bitmap);
749 if (!bitmap)
751 WARN("No cursor bitmap, returning D3DERR_INVALIDCALL.\n");
752 return D3DERR_INVALIDCALL;
755 wined3d_mutex_lock();
756 hr = wined3d_device_set_cursor_properties(device->wined3d_device,
757 hotspot_x, hotspot_y, bitmap_impl->wined3d_texture, bitmap_impl->sub_resource_idx);
758 wined3d_mutex_unlock();
760 return hr;
763 static void WINAPI d3d9_device_SetCursorPosition(IDirect3DDevice9Ex *iface, int x, int y, DWORD flags)
765 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
767 TRACE("iface %p, x %u, y %u, flags %#x.\n", iface, x, y, flags);
769 wined3d_mutex_lock();
770 wined3d_device_set_cursor_position(device->wined3d_device, x, y, flags);
771 wined3d_mutex_unlock();
774 static BOOL WINAPI d3d9_device_ShowCursor(IDirect3DDevice9Ex *iface, BOOL show)
776 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
777 BOOL ret;
779 TRACE("iface %p, show %#x.\n", iface, show);
781 wined3d_mutex_lock();
782 ret = wined3d_device_show_cursor(device->wined3d_device, show);
783 wined3d_mutex_unlock();
785 return ret;
788 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_CreateAdditionalSwapChain(IDirect3DDevice9Ex *iface,
789 D3DPRESENT_PARAMETERS *present_parameters, IDirect3DSwapChain9 **swapchain)
791 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
792 struct wined3d_swapchain_desc desc;
793 struct d3d9_swapchain *object;
794 unsigned int swap_interval;
795 unsigned int i, count;
796 HRESULT hr;
798 TRACE("iface %p, present_parameters %p, swapchain %p.\n",
799 iface, present_parameters, swapchain);
801 if (!present_parameters->Windowed)
803 WARN("Trying to create an additional fullscreen swapchain, returning D3DERR_INVALIDCALL.\n");
804 return D3DERR_INVALIDCALL;
807 wined3d_mutex_lock();
808 count = wined3d_device_get_swapchain_count(device->wined3d_device);
809 for (i = 0; i < count; ++i)
811 struct wined3d_swapchain *wined3d_swapchain;
813 wined3d_swapchain = wined3d_device_get_swapchain(device->wined3d_device, i);
814 wined3d_swapchain_get_desc(wined3d_swapchain, &desc);
816 if (!desc.windowed)
818 wined3d_mutex_unlock();
819 WARN("Trying to create an additional swapchain in fullscreen mode, returning D3DERR_INVALIDCALL.\n");
820 return D3DERR_INVALIDCALL;
823 wined3d_mutex_unlock();
825 if (!wined3d_swapchain_desc_from_present_parameters(&desc, present_parameters,
826 device->d3d_parent->extended))
827 return D3DERR_INVALIDCALL;
828 swap_interval = wined3dswapinterval_from_d3d(present_parameters->PresentationInterval);
829 if (SUCCEEDED(hr = d3d9_swapchain_create(device, &desc, swap_interval, &object)))
830 *swapchain = (IDirect3DSwapChain9 *)&object->IDirect3DSwapChain9Ex_iface;
831 present_parameters_from_wined3d_swapchain_desc(present_parameters,
832 &desc, present_parameters->PresentationInterval);
834 return hr;
837 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_GetSwapChain(IDirect3DDevice9Ex *iface,
838 UINT swapchain_idx, IDirect3DSwapChain9 **swapchain)
840 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
841 HRESULT hr;
843 TRACE("iface %p, swapchain_idx %u, swapchain %p.\n", iface, swapchain_idx, swapchain);
845 wined3d_mutex_lock();
846 if (swapchain_idx < device->implicit_swapchain_count)
848 *swapchain = (IDirect3DSwapChain9 *)&device->implicit_swapchains[swapchain_idx]->IDirect3DSwapChain9Ex_iface;
849 IDirect3DSwapChain9Ex_AddRef(*swapchain);
850 hr = D3D_OK;
852 else
854 *swapchain = NULL;
855 hr = D3DERR_INVALIDCALL;
857 wined3d_mutex_unlock();
859 return hr;
862 static UINT WINAPI d3d9_device_GetNumberOfSwapChains(IDirect3DDevice9Ex *iface)
864 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
865 UINT count;
867 TRACE("iface %p.\n", iface);
869 wined3d_mutex_lock();
870 count = wined3d_device_get_swapchain_count(device->wined3d_device);
871 wined3d_mutex_unlock();
873 return count;
876 static HRESULT CDECL reset_enum_callback(struct wined3d_resource *resource)
878 struct d3d9_vertexbuffer *vertex_buffer;
879 struct d3d9_indexbuffer *index_buffer;
880 struct wined3d_resource_desc desc;
881 IDirect3DBaseTexture9 *texture;
882 struct d3d9_surface *surface;
883 IUnknown *parent;
885 wined3d_resource_get_desc(resource, &desc);
886 if (desc.access & WINED3D_RESOURCE_ACCESS_CPU)
887 return D3D_OK;
889 if (desc.resource_type != WINED3D_RTYPE_TEXTURE_2D)
891 if (desc.bind_flags & WINED3D_BIND_VERTEX_BUFFER)
893 vertex_buffer = wined3d_resource_get_parent(resource);
894 if (vertex_buffer && vertex_buffer->draw_buffer)
895 return D3D_OK;
897 if (desc.bind_flags & WINED3D_BIND_INDEX_BUFFER)
899 index_buffer = wined3d_resource_get_parent(resource);
900 if (index_buffer && index_buffer->draw_buffer)
901 return D3D_OK;
904 WARN("Resource %p in pool D3DPOOL_DEFAULT blocks the Reset call.\n", resource);
905 return D3DERR_INVALIDCALL;
908 parent = wined3d_resource_get_parent(resource);
909 if (parent && SUCCEEDED(IUnknown_QueryInterface(parent, &IID_IDirect3DBaseTexture9, (void **)&texture)))
911 IDirect3DBaseTexture9_Release(texture);
912 WARN("Texture %p (resource %p) in pool D3DPOOL_DEFAULT blocks the Reset call.\n", texture, resource);
913 return D3DERR_INVALIDCALL;
916 surface = wined3d_texture_get_sub_resource_parent(wined3d_texture_from_resource(resource), 0);
917 if (!surface->resource.refcount)
918 return D3D_OK;
920 WARN("Surface %p in pool D3DPOOL_DEFAULT blocks the Reset call.\n", surface);
921 return D3DERR_INVALIDCALL;
924 static HRESULT d3d9_device_get_swapchains(struct d3d9_device *device)
926 UINT i, new_swapchain_count = wined3d_device_get_swapchain_count(device->wined3d_device);
927 struct wined3d_swapchain *wined3d_swapchain;
929 if (!(device->implicit_swapchains = heap_alloc(new_swapchain_count * sizeof(*device->implicit_swapchains))))
930 return E_OUTOFMEMORY;
932 for (i = 0; i < new_swapchain_count; ++i)
934 wined3d_swapchain = wined3d_device_get_swapchain(device->wined3d_device, i);
935 device->implicit_swapchains[i] = wined3d_swapchain_get_parent(wined3d_swapchain);
937 device->implicit_swapchain_count = new_swapchain_count;
939 return D3D_OK;
942 static HRESULT d3d9_device_reset(struct d3d9_device *device,
943 D3DPRESENT_PARAMETERS *present_parameters, D3DDISPLAYMODEEX *mode)
945 BOOL extended = device->d3d_parent->extended;
946 struct wined3d_swapchain_desc swapchain_desc;
947 struct wined3d_display_mode wined3d_mode;
948 struct wined3d_rendertarget_view *rtv;
949 unsigned int i;
950 HRESULT hr;
952 if (!extended && device->device_state == D3D9_DEVICE_STATE_LOST)
954 WARN("App not active, returning D3DERR_DEVICELOST.\n");
955 return D3DERR_DEVICELOST;
958 if (mode)
960 wined3d_mode.width = mode->Width;
961 wined3d_mode.height = mode->Height;
962 wined3d_mode.refresh_rate = mode->RefreshRate;
963 wined3d_mode.format_id = wined3dformat_from_d3dformat(mode->Format);
964 wined3d_mode.scanline_ordering = mode->ScanLineOrdering;
967 if (!wined3d_swapchain_desc_from_present_parameters(&swapchain_desc, present_parameters, extended))
968 return D3DERR_INVALIDCALL;
970 wined3d_mutex_lock();
972 if (device->vertex_buffer)
974 wined3d_buffer_decref(device->vertex_buffer);
975 device->vertex_buffer = NULL;
976 device->vertex_buffer_size = 0;
979 if (device->index_buffer)
981 wined3d_buffer_decref(device->index_buffer);
982 device->index_buffer = NULL;
983 device->index_buffer_size = 0;
986 if (SUCCEEDED(hr = wined3d_device_reset(device->wined3d_device, &swapchain_desc,
987 mode ? &wined3d_mode : NULL, reset_enum_callback, !extended)))
989 heap_free(device->implicit_swapchains);
991 if (!extended)
993 device->recording = FALSE;
994 device->auto_mipmaps = 0;
995 wined3d_device_set_render_state(device->wined3d_device, WINED3D_RS_ZENABLE,
996 !!swapchain_desc.enable_auto_depth_stencil);
999 if (FAILED(hr = d3d9_device_get_swapchains(device)))
1001 device->device_state = D3D9_DEVICE_STATE_NOT_RESET;
1003 else
1005 device->implicit_swapchains[0]->swap_interval
1006 = wined3dswapinterval_from_d3d(present_parameters->PresentationInterval);
1007 wined3d_swapchain_get_desc(device->implicit_swapchains[0]->wined3d_swapchain, &swapchain_desc);
1008 present_parameters->BackBufferWidth = swapchain_desc.backbuffer_width;
1009 present_parameters->BackBufferHeight = swapchain_desc.backbuffer_height;
1010 present_parameters->BackBufferFormat = d3dformat_from_wined3dformat(swapchain_desc.backbuffer_format);
1011 present_parameters->BackBufferCount = swapchain_desc.backbuffer_count;
1013 device->device_state = D3D9_DEVICE_STATE_OK;
1016 rtv = wined3d_device_get_rendertarget_view(device->wined3d_device, 0);
1017 device->render_targets[0] = wined3d_rendertarget_view_get_sub_resource_parent(rtv);
1018 for (i = 1; i < ARRAY_SIZE(device->render_targets); ++i)
1019 device->render_targets[i] = NULL;
1021 else if (!extended)
1023 device->device_state = D3D9_DEVICE_STATE_NOT_RESET;
1026 wined3d_mutex_unlock();
1028 return hr;
1031 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_Reset(IDirect3DDevice9Ex *iface,
1032 D3DPRESENT_PARAMETERS *present_parameters)
1034 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1036 TRACE("iface %p, present_parameters %p.\n", iface, present_parameters);
1038 return d3d9_device_reset(device, present_parameters, NULL);
1041 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_Present(IDirect3DDevice9Ex *iface,
1042 const RECT *src_rect, const RECT *dst_rect, HWND dst_window_override, const RGNDATA *dirty_region)
1044 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1045 struct d3d9_swapchain *swapchain;
1046 unsigned int i;
1047 HRESULT hr;
1049 TRACE("iface %p, src_rect %s, dst_rect %s, dst_window_override %p, dirty_region %p.\n",
1050 iface, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect), dst_window_override, dirty_region);
1052 if (device->device_state != D3D9_DEVICE_STATE_OK)
1053 return device->d3d_parent->extended ? S_PRESENT_OCCLUDED : D3DERR_DEVICELOST;
1055 if (dirty_region)
1056 FIXME("Ignoring dirty_region %p.\n", dirty_region);
1058 wined3d_mutex_lock();
1059 for (i = 0; i < device->implicit_swapchain_count; ++i)
1061 swapchain = device->implicit_swapchains[i];
1062 if (FAILED(hr = wined3d_swapchain_present(swapchain->wined3d_swapchain,
1063 src_rect, dst_rect, dst_window_override, swapchain->swap_interval, 0)))
1065 wined3d_mutex_unlock();
1066 return hr;
1069 wined3d_mutex_unlock();
1071 return D3D_OK;
1074 static HRESULT WINAPI d3d9_device_GetBackBuffer(IDirect3DDevice9Ex *iface, UINT swapchain,
1075 UINT backbuffer_idx, D3DBACKBUFFER_TYPE backbuffer_type, IDirect3DSurface9 **backbuffer)
1077 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1078 HRESULT hr;
1080 TRACE("iface %p, swapchain %u, backbuffer_idx %u, backbuffer_type %#x, backbuffer %p.\n",
1081 iface, swapchain, backbuffer_idx, backbuffer_type, backbuffer);
1083 /* backbuffer_type is ignored by native. */
1085 /* No need to check for backbuffer == NULL, Windows crashes in that case. */
1086 *backbuffer = NULL;
1088 wined3d_mutex_lock();
1089 if (swapchain >= device->implicit_swapchain_count)
1091 wined3d_mutex_unlock();
1092 WARN("Swapchain index %u is out of range, returning D3DERR_INVALIDCALL.\n", swapchain);
1093 return D3DERR_INVALIDCALL;
1096 hr = IDirect3DSwapChain9Ex_GetBackBuffer(&device->implicit_swapchains[swapchain]->IDirect3DSwapChain9Ex_iface,
1097 backbuffer_idx, backbuffer_type, backbuffer);
1098 wined3d_mutex_unlock();
1100 return hr;
1103 static HRESULT WINAPI d3d9_device_GetRasterStatus(IDirect3DDevice9Ex *iface,
1104 UINT swapchain, D3DRASTER_STATUS *raster_status)
1106 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1107 HRESULT hr;
1109 TRACE("iface %p, swapchain %u, raster_status %p.\n", iface, swapchain, raster_status);
1111 wined3d_mutex_lock();
1112 hr = wined3d_device_get_raster_status(device->wined3d_device,
1113 swapchain, (struct wined3d_raster_status *)raster_status);
1114 wined3d_mutex_unlock();
1116 return hr;
1119 static HRESULT WINAPI d3d9_device_SetDialogBoxMode(IDirect3DDevice9Ex *iface, BOOL enable)
1121 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1122 HRESULT hr;
1124 TRACE("iface %p, enable %#x.\n", iface, enable);
1126 wined3d_mutex_lock();
1127 hr = wined3d_device_set_dialog_box_mode(device->wined3d_device, enable);
1128 wined3d_mutex_unlock();
1130 return hr;
1133 static void WINAPI d3d9_device_SetGammaRamp(IDirect3DDevice9Ex *iface,
1134 UINT swapchain, DWORD flags, const D3DGAMMARAMP *ramp)
1136 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1138 TRACE("iface %p, swapchain %u, flags %#x, ramp %p.\n", iface, swapchain, flags, ramp);
1140 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
1141 wined3d_mutex_lock();
1142 wined3d_device_set_gamma_ramp(device->wined3d_device, swapchain, flags, (const struct wined3d_gamma_ramp *)ramp);
1143 wined3d_mutex_unlock();
1146 static void WINAPI d3d9_device_GetGammaRamp(IDirect3DDevice9Ex *iface, UINT swapchain, D3DGAMMARAMP *ramp)
1148 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1150 TRACE("iface %p, swapchain %u, ramp %p.\n", iface, swapchain, ramp);
1152 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
1153 wined3d_mutex_lock();
1154 wined3d_device_get_gamma_ramp(device->wined3d_device, swapchain, (struct wined3d_gamma_ramp *)ramp);
1155 wined3d_mutex_unlock();
1158 static HRESULT WINAPI d3d9_device_CreateTexture(IDirect3DDevice9Ex *iface,
1159 UINT width, UINT height, UINT levels, DWORD usage, D3DFORMAT format,
1160 D3DPOOL pool, IDirect3DTexture9 **texture, HANDLE *shared_handle)
1162 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1163 struct d3d9_texture *object;
1164 BOOL set_mem = FALSE;
1165 HRESULT hr;
1167 TRACE("iface %p, width %u, height %u, levels %u, usage %#x, format %#x, pool %#x, texture %p, shared_handle %p.\n",
1168 iface, width, height, levels, usage, format, pool, texture, shared_handle);
1170 *texture = NULL;
1171 if (shared_handle)
1173 if (!device->d3d_parent->extended)
1175 WARN("Trying to create a shared or user memory texture on a non-ex device.\n");
1176 return E_NOTIMPL;
1179 if (pool == D3DPOOL_SYSTEMMEM)
1181 if (levels != 1)
1182 return D3DERR_INVALIDCALL;
1183 set_mem = TRUE;
1185 else
1187 if (pool != D3DPOOL_DEFAULT)
1189 WARN("Trying to create a shared texture in pool %#x.\n", pool);
1190 return D3DERR_INVALIDCALL;
1192 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1196 if (!(object = heap_alloc_zero(sizeof(*object))))
1197 return D3DERR_OUTOFVIDEOMEMORY;
1199 hr = texture_init(object, device, width, height, levels, usage, format, pool);
1200 if (FAILED(hr))
1202 WARN("Failed to initialize texture, hr %#x.\n", hr);
1203 heap_free(object);
1204 return hr;
1207 if (set_mem)
1208 wined3d_texture_update_desc(object->wined3d_texture, width, height,
1209 wined3dformat_from_d3dformat(format), WINED3D_MULTISAMPLE_NONE, 0,
1210 *shared_handle, 0);
1212 TRACE("Created texture %p.\n", object);
1213 *texture = (IDirect3DTexture9 *)&object->IDirect3DBaseTexture9_iface;
1215 return D3D_OK;
1218 static HRESULT WINAPI d3d9_device_CreateVolumeTexture(IDirect3DDevice9Ex *iface,
1219 UINT width, UINT height, UINT depth, UINT levels, DWORD usage, D3DFORMAT format,
1220 D3DPOOL pool, IDirect3DVolumeTexture9 **texture, HANDLE *shared_handle)
1222 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1223 struct d3d9_texture *object;
1224 HRESULT hr;
1226 TRACE("iface %p, width %u, height %u, depth %u, levels %u, "
1227 "usage %#x, format %#x, pool %#x, texture %p, shared_handle %p.\n",
1228 iface, width, height, depth, levels,
1229 usage, format, pool, texture, shared_handle);
1231 *texture = NULL;
1232 if (shared_handle)
1234 if (!device->d3d_parent->extended)
1236 WARN("Trying to create a shared volume texture on a non-ex device.\n");
1237 return E_NOTIMPL;
1240 if (pool != D3DPOOL_DEFAULT)
1242 WARN("Trying to create a shared volume texture in pool %#x.\n", pool);
1243 return D3DERR_INVALIDCALL;
1245 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1248 if (!(object = heap_alloc_zero(sizeof(*object))))
1249 return D3DERR_OUTOFVIDEOMEMORY;
1251 hr = volumetexture_init(object, device, width, height, depth, levels, usage, format, pool);
1252 if (FAILED(hr))
1254 WARN("Failed to initialize volume texture, hr %#x.\n", hr);
1255 heap_free(object);
1256 return hr;
1259 TRACE("Created volume texture %p.\n", object);
1260 *texture = (IDirect3DVolumeTexture9 *)&object->IDirect3DBaseTexture9_iface;
1262 return D3D_OK;
1265 static HRESULT WINAPI d3d9_device_CreateCubeTexture(IDirect3DDevice9Ex *iface,
1266 UINT edge_length, UINT levels, DWORD usage, D3DFORMAT format, D3DPOOL pool,
1267 IDirect3DCubeTexture9 **texture, HANDLE *shared_handle)
1269 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1270 struct d3d9_texture *object;
1271 HRESULT hr;
1273 TRACE("iface %p, edge_length %u, levels %u, usage %#x, format %#x, pool %#x, texture %p, shared_handle %p.\n",
1274 iface, edge_length, levels, usage, format, pool, texture, shared_handle);
1276 *texture = NULL;
1277 if (shared_handle)
1279 if (!device->d3d_parent->extended)
1281 WARN("Trying to create a shared cube texture on a non-ex device.\n");
1282 return E_NOTIMPL;
1285 if (pool != D3DPOOL_DEFAULT)
1287 WARN("Trying to create a shared cube texture in pool %#x.\n", pool);
1288 return D3DERR_INVALIDCALL;
1290 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1293 if (!(object = heap_alloc_zero(sizeof(*object))))
1294 return D3DERR_OUTOFVIDEOMEMORY;
1296 hr = cubetexture_init(object, device, edge_length, levels, usage, format, pool);
1297 if (FAILED(hr))
1299 WARN("Failed to initialize cube texture, hr %#x.\n", hr);
1300 heap_free(object);
1301 return hr;
1304 TRACE("Created cube texture %p.\n", object);
1305 *texture = (IDirect3DCubeTexture9 *)&object->IDirect3DBaseTexture9_iface;
1307 return D3D_OK;
1310 static HRESULT WINAPI d3d9_device_CreateVertexBuffer(IDirect3DDevice9Ex *iface, UINT size,
1311 DWORD usage, DWORD fvf, D3DPOOL pool, IDirect3DVertexBuffer9 **buffer,
1312 HANDLE *shared_handle)
1314 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1315 struct d3d9_vertexbuffer *object;
1316 HRESULT hr;
1318 TRACE("iface %p, size %u, usage %#x, fvf %#x, pool %#x, buffer %p, shared_handle %p.\n",
1319 iface, size, usage, fvf, pool, buffer, shared_handle);
1321 if (shared_handle)
1323 if (!device->d3d_parent->extended)
1325 WARN("Trying to create a shared vertex buffer on a non-ex device.\n");
1326 return E_NOTIMPL;
1329 if (pool != D3DPOOL_DEFAULT)
1331 WARN("Trying to create a shared vertex buffer in pool %#x.\n", pool);
1332 return D3DERR_NOTAVAILABLE;
1334 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1337 if (!(object = heap_alloc_zero(sizeof(*object))))
1338 return D3DERR_OUTOFVIDEOMEMORY;
1340 hr = vertexbuffer_init(object, device, size, usage, fvf, pool);
1341 if (FAILED(hr))
1343 WARN("Failed to initialize vertex buffer, hr %#x.\n", hr);
1344 heap_free(object);
1345 return hr;
1348 TRACE("Created vertex buffer %p.\n", object);
1349 *buffer = &object->IDirect3DVertexBuffer9_iface;
1351 return D3D_OK;
1354 static HRESULT WINAPI d3d9_device_CreateIndexBuffer(IDirect3DDevice9Ex *iface, UINT size,
1355 DWORD usage, D3DFORMAT format, D3DPOOL pool, IDirect3DIndexBuffer9 **buffer,
1356 HANDLE *shared_handle)
1358 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1359 struct d3d9_indexbuffer *object;
1360 HRESULT hr;
1362 TRACE("iface %p, size %u, usage %#x, format %#x, pool %#x, buffer %p, shared_handle %p.\n",
1363 iface, size, usage, format, pool, buffer, shared_handle);
1365 if (shared_handle)
1367 if (!device->d3d_parent->extended)
1369 WARN("Trying to create a shared index buffer on a non-ex device.\n");
1370 return E_NOTIMPL;
1373 if (pool != D3DPOOL_DEFAULT)
1375 WARN("Trying to create a shared index buffer in pool %#x.\n", pool);
1376 return D3DERR_NOTAVAILABLE;
1378 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1381 if (!(object = heap_alloc_zero(sizeof(*object))))
1382 return D3DERR_OUTOFVIDEOMEMORY;
1384 hr = indexbuffer_init(object, device, size, usage, format, pool);
1385 if (FAILED(hr))
1387 WARN("Failed to initialize index buffer, hr %#x.\n", hr);
1388 heap_free(object);
1389 return hr;
1392 TRACE("Created index buffer %p.\n", object);
1393 *buffer = &object->IDirect3DIndexBuffer9_iface;
1395 return D3D_OK;
1398 static HRESULT d3d9_device_create_surface(struct d3d9_device *device, unsigned int flags,
1399 enum wined3d_format_id format, enum wined3d_multisample_type multisample_type,
1400 unsigned int multisample_quality, unsigned int usage, unsigned int bind_flags, unsigned int access,
1401 unsigned int width, unsigned int height, void *user_mem, IDirect3DSurface9 **surface)
1403 struct wined3d_resource_desc desc;
1404 struct d3d9_surface *surface_impl;
1405 struct wined3d_texture *texture;
1406 HRESULT hr;
1408 TRACE("device %p, flags %#x, format %#x, multisample_type %#x, multisample_quality %u, "
1409 "usage %#x, bind_flags %#x, access %#x, width %u, height %u, user_mem %p, surface %p.\n",
1410 device, flags, format, multisample_type, multisample_quality, usage,
1411 bind_flags, access, width, height, user_mem, surface);
1413 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
1414 desc.format = format;
1415 desc.multisample_type = multisample_type;
1416 desc.multisample_quality = multisample_quality;
1417 desc.usage = usage;
1418 desc.bind_flags = bind_flags;
1419 desc.access = access;
1420 desc.width = width;
1421 desc.height = height;
1422 desc.depth = 1;
1423 desc.size = 0;
1425 if (is_gdi_compat_wined3dformat(desc.format))
1426 flags |= WINED3D_TEXTURE_CREATE_GET_DC;
1428 wined3d_mutex_lock();
1430 if (FAILED(hr = wined3d_texture_create(device->wined3d_device, &desc,
1431 1, 1, flags, NULL, NULL, &d3d9_null_wined3d_parent_ops, &texture)))
1433 wined3d_mutex_unlock();
1434 WARN("Failed to create texture, hr %#x.\n", hr);
1435 if (hr == WINED3DERR_NOTAVAILABLE)
1436 hr = D3DERR_INVALIDCALL;
1437 return hr;
1440 surface_impl = wined3d_texture_get_sub_resource_parent(texture, 0);
1441 surface_impl->parent_device = &device->IDirect3DDevice9Ex_iface;
1442 *surface = &surface_impl->IDirect3DSurface9_iface;
1443 IDirect3DSurface9_AddRef(*surface);
1445 if (user_mem)
1446 wined3d_texture_update_desc(texture, width, height,
1447 desc.format, multisample_type, multisample_quality, user_mem, 0);
1449 wined3d_texture_decref(texture);
1451 wined3d_mutex_unlock();
1453 return D3D_OK;
1456 BOOL is_gdi_compat_wined3dformat(enum wined3d_format_id format)
1458 switch (format)
1460 case WINED3DFMT_B8G8R8A8_UNORM:
1461 case WINED3DFMT_B8G8R8X8_UNORM:
1462 case WINED3DFMT_B5G6R5_UNORM:
1463 case WINED3DFMT_B5G5R5X1_UNORM:
1464 case WINED3DFMT_B5G5R5A1_UNORM:
1465 case WINED3DFMT_B8G8R8_UNORM:
1466 return TRUE;
1467 default:
1468 return FALSE;
1472 static HRESULT WINAPI d3d9_device_CreateRenderTarget(IDirect3DDevice9Ex *iface, UINT width, UINT height,
1473 D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
1474 BOOL lockable, IDirect3DSurface9 **surface, HANDLE *shared_handle)
1476 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1477 unsigned int access = WINED3D_RESOURCE_ACCESS_GPU;
1479 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u.\n"
1480 "lockable %#x, surface %p, shared_handle %p.\n",
1481 iface, width, height, format, multisample_type, multisample_quality,
1482 lockable, surface, shared_handle);
1484 *surface = NULL;
1485 if (shared_handle)
1487 if (!device->d3d_parent->extended)
1489 WARN("Trying to create a shared render target on a non-ex device.\n");
1490 return E_NOTIMPL;
1493 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1496 if (lockable)
1497 access |= WINED3D_RESOURCE_ACCESS_MAP_R | WINED3D_RESOURCE_ACCESS_MAP_W;
1499 return d3d9_device_create_surface(device, 0, wined3dformat_from_d3dformat(format), multisample_type,
1500 multisample_quality, 0, WINED3D_BIND_RENDER_TARGET, access, width, height, NULL, surface);
1503 static HRESULT WINAPI d3d9_device_CreateDepthStencilSurface(IDirect3DDevice9Ex *iface, UINT width, UINT height,
1504 D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
1505 BOOL discard, IDirect3DSurface9 **surface, HANDLE *shared_handle)
1507 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1508 DWORD flags = 0;
1510 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u.\n"
1511 "discard %#x, surface %p, shared_handle %p.\n",
1512 iface, width, height, format, multisample_type, multisample_quality,
1513 discard, surface, shared_handle);
1515 *surface = NULL;
1516 if (shared_handle)
1518 if (!device->d3d_parent->extended)
1520 WARN("Trying to create a shared depth stencil on a non-ex device.\n");
1521 return E_NOTIMPL;
1524 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1527 if (discard)
1528 flags |= WINED3D_TEXTURE_CREATE_DISCARD;
1530 return d3d9_device_create_surface(device, flags, wined3dformat_from_d3dformat(format),
1531 multisample_type, multisample_quality, 0, WINED3D_BIND_DEPTH_STENCIL,
1532 WINED3D_RESOURCE_ACCESS_GPU, width, height, NULL, surface);
1536 static HRESULT WINAPI d3d9_device_UpdateSurface(IDirect3DDevice9Ex *iface,
1537 IDirect3DSurface9 *src_surface, const RECT *src_rect,
1538 IDirect3DSurface9 *dst_surface, const POINT *dst_point)
1540 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1541 struct d3d9_surface *src = unsafe_impl_from_IDirect3DSurface9(src_surface);
1542 struct d3d9_surface *dst = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1543 struct wined3d_sub_resource_desc src_desc, dst_desc;
1544 struct wined3d_box src_box;
1545 HRESULT hr;
1547 TRACE("iface %p, src_surface %p, src_rect %s, dst_surface %p, dst_point %p.\n",
1548 iface, src_surface, wine_dbgstr_rect(src_rect), dst_surface, dst_point);
1550 wined3d_mutex_lock();
1552 wined3d_texture_get_sub_resource_desc(src->wined3d_texture, src->sub_resource_idx, &src_desc);
1553 wined3d_texture_get_sub_resource_desc(dst->wined3d_texture, dst->sub_resource_idx, &dst_desc);
1554 if (src_desc.format != dst_desc.format)
1556 wined3d_mutex_unlock();
1557 WARN("Surface formats (%#x/%#x) don't match.\n",
1558 d3dformat_from_wined3dformat(src_desc.format),
1559 d3dformat_from_wined3dformat(dst_desc.format));
1560 return D3DERR_INVALIDCALL;
1563 if (src_rect)
1564 wined3d_box_set(&src_box, src_rect->left, src_rect->top, src_rect->right, src_rect->bottom, 0, 1);
1565 else
1566 wined3d_box_set(&src_box, 0, 0, src_desc.width, src_desc.height, 0, 1);
1568 hr = wined3d_device_copy_sub_resource_region(device->wined3d_device,
1569 wined3d_texture_get_resource(dst->wined3d_texture), dst->sub_resource_idx, dst_point ? dst_point->x : 0,
1570 dst_point ? dst_point->y : 0, 0, wined3d_texture_get_resource(src->wined3d_texture),
1571 src->sub_resource_idx, &src_box, 0);
1572 if (SUCCEEDED(hr) && dst->texture)
1573 d3d9_texture_flag_auto_gen_mipmap(dst->texture);
1575 wined3d_mutex_unlock();
1577 if (FAILED(hr))
1578 return D3DERR_INVALIDCALL;
1580 return hr;
1583 static HRESULT WINAPI d3d9_device_UpdateTexture(IDirect3DDevice9Ex *iface,
1584 IDirect3DBaseTexture9 *src_texture, IDirect3DBaseTexture9 *dst_texture)
1586 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1587 struct d3d9_texture *src_impl, *dst_impl;
1588 HRESULT hr;
1590 TRACE("iface %p, src_texture %p, dst_texture %p.\n", iface, src_texture, dst_texture);
1592 src_impl = unsafe_impl_from_IDirect3DBaseTexture9(src_texture);
1593 dst_impl = unsafe_impl_from_IDirect3DBaseTexture9(dst_texture);
1595 wined3d_mutex_lock();
1596 hr = wined3d_device_update_texture(device->wined3d_device,
1597 src_impl->wined3d_texture, dst_impl->wined3d_texture);
1598 if (SUCCEEDED(hr))
1599 d3d9_texture_flag_auto_gen_mipmap(dst_impl);
1600 wined3d_mutex_unlock();
1602 return hr;
1605 static HRESULT WINAPI d3d9_device_GetRenderTargetData(IDirect3DDevice9Ex *iface,
1606 IDirect3DSurface9 *render_target, IDirect3DSurface9 *dst_surface)
1608 struct d3d9_surface *rt_impl = unsafe_impl_from_IDirect3DSurface9(render_target);
1609 struct d3d9_surface *dst_impl = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1610 struct wined3d_sub_resource_desc wined3d_desc;
1611 RECT dst_rect, src_rect;
1612 HRESULT hr;
1614 TRACE("iface %p, render_target %p, dst_surface %p.\n", iface, render_target, dst_surface);
1616 if (!render_target || !dst_surface)
1617 return D3DERR_INVALIDCALL;
1619 wined3d_mutex_lock();
1620 wined3d_texture_get_sub_resource_desc(dst_impl->wined3d_texture, dst_impl->sub_resource_idx, &wined3d_desc);
1621 SetRect(&dst_rect, 0, 0, wined3d_desc.width, wined3d_desc.height);
1623 wined3d_texture_get_sub_resource_desc(rt_impl->wined3d_texture, rt_impl->sub_resource_idx, &wined3d_desc);
1624 SetRect(&src_rect, 0, 0, wined3d_desc.width, wined3d_desc.height);
1626 /* TODO: Check surface sizes, pools, etc. */
1627 if (wined3d_desc.multisample_type)
1628 hr = D3DERR_INVALIDCALL;
1629 else
1630 hr = wined3d_texture_blt(dst_impl->wined3d_texture, dst_impl->sub_resource_idx, &dst_rect,
1631 rt_impl->wined3d_texture, rt_impl->sub_resource_idx, &src_rect, 0, NULL, WINED3D_TEXF_POINT);
1632 wined3d_mutex_unlock();
1634 return hr;
1637 static HRESULT WINAPI d3d9_device_GetFrontBufferData(IDirect3DDevice9Ex *iface,
1638 UINT swapchain, IDirect3DSurface9 *dst_surface)
1640 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1641 struct d3d9_surface *dst_impl = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1642 HRESULT hr = D3DERR_INVALIDCALL;
1644 TRACE("iface %p, swapchain %u, dst_surface %p.\n", iface, swapchain, dst_surface);
1646 wined3d_mutex_lock();
1647 if (swapchain < device->implicit_swapchain_count)
1648 hr = wined3d_swapchain_get_front_buffer_data(device->implicit_swapchains[swapchain]->wined3d_swapchain,
1649 dst_impl->wined3d_texture, dst_impl->sub_resource_idx);
1650 wined3d_mutex_unlock();
1652 return hr;
1655 static HRESULT WINAPI d3d9_device_StretchRect(IDirect3DDevice9Ex *iface, IDirect3DSurface9 *src_surface,
1656 const RECT *src_rect, IDirect3DSurface9 *dst_surface, const RECT *dst_rect, D3DTEXTUREFILTERTYPE filter)
1658 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1659 struct d3d9_surface *src = unsafe_impl_from_IDirect3DSurface9(src_surface);
1660 struct d3d9_surface *dst = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1661 struct wined3d_sub_resource_desc src_desc, dst_desc;
1662 HRESULT hr = D3DERR_INVALIDCALL;
1663 RECT d, s;
1665 TRACE("iface %p, src_surface %p, src_rect %s, dst_surface %p, dst_rect %s, filter %#x.\n",
1666 iface, src_surface, wine_dbgstr_rect(src_rect), dst_surface, wine_dbgstr_rect(dst_rect), filter);
1668 wined3d_mutex_lock();
1669 wined3d_texture_get_sub_resource_desc(dst->wined3d_texture, dst->sub_resource_idx, &dst_desc);
1670 if (!dst_rect)
1672 SetRect(&d, 0, 0, dst_desc.width, dst_desc.height);
1673 dst_rect = &d;
1676 wined3d_texture_get_sub_resource_desc(src->wined3d_texture, src->sub_resource_idx, &src_desc);
1677 if (!src_rect)
1679 SetRect(&s, 0, 0, src_desc.width, src_desc.height);
1680 src_rect = &s;
1683 if (dst_desc.access & WINED3D_RESOURCE_ACCESS_CPU)
1685 WARN("Destination resource is not in DEFAULT pool.\n");
1686 goto done;
1688 if (src_desc.access & WINED3D_RESOURCE_ACCESS_CPU)
1690 WARN("Source resource is not in DEFAULT pool.\n");
1691 goto done;
1694 if (dst->texture && !(dst_desc.bind_flags & (WINED3D_BIND_RENDER_TARGET | WINED3D_BIND_DEPTH_STENCIL)))
1696 WARN("Destination is a regular texture.\n");
1697 goto done;
1700 if (src_desc.bind_flags & WINED3D_BIND_DEPTH_STENCIL)
1702 if (device->in_scene)
1704 WARN("Rejecting depth / stencil blit while in scene.\n");
1705 goto done;
1708 if (src_rect->left || src_rect->top || src_rect->right != src_desc.width
1709 || src_rect->bottom != src_desc.height)
1711 WARN("Rejecting depth / stencil blit with invalid source rect %s.\n",
1712 wine_dbgstr_rect(src_rect));
1713 goto done;
1716 if (dst_rect->left || dst_rect->top || dst_rect->right != dst_desc.width
1717 || dst_rect->bottom != dst_desc.height)
1719 WARN("Rejecting depth / stencil blit with invalid destination rect %s.\n",
1720 wine_dbgstr_rect(dst_rect));
1721 goto done;
1724 if (src_desc.width != dst_desc.width || src_desc.height != dst_desc.height)
1726 WARN("Rejecting depth / stencil blit with mismatched surface sizes.\n");
1727 goto done;
1731 hr = wined3d_texture_blt(dst->wined3d_texture, dst->sub_resource_idx, dst_rect,
1732 src->wined3d_texture, src->sub_resource_idx, src_rect, 0, NULL, filter);
1733 if (hr == WINEDDERR_INVALIDRECT)
1734 hr = D3DERR_INVALIDCALL;
1735 if (SUCCEEDED(hr) && dst->texture)
1736 d3d9_texture_flag_auto_gen_mipmap(dst->texture);
1738 done:
1739 wined3d_mutex_unlock();
1740 return hr;
1743 static HRESULT WINAPI d3d9_device_ColorFill(IDirect3DDevice9Ex *iface,
1744 IDirect3DSurface9 *surface, const RECT *rect, D3DCOLOR color)
1746 const struct wined3d_color c =
1748 ((color >> 16) & 0xff) / 255.0f,
1749 ((color >> 8) & 0xff) / 255.0f,
1750 (color & 0xff) / 255.0f,
1751 ((color >> 24) & 0xff) / 255.0f,
1753 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1754 struct d3d9_surface *surface_impl = unsafe_impl_from_IDirect3DSurface9(surface);
1755 struct wined3d_sub_resource_desc desc;
1756 struct wined3d_rendertarget_view *rtv;
1757 HRESULT hr;
1759 TRACE("iface %p, surface %p, rect %p, color 0x%08x.\n", iface, surface, rect, color);
1761 if (!surface)
1762 return D3DERR_INVALIDCALL;
1764 wined3d_mutex_lock();
1766 if (FAILED(wined3d_texture_get_sub_resource_desc(surface_impl->wined3d_texture,
1767 surface_impl->sub_resource_idx, &desc)))
1769 wined3d_mutex_unlock();
1770 return D3DERR_INVALIDCALL;
1773 if (desc.access & WINED3D_RESOURCE_ACCESS_CPU)
1775 wined3d_mutex_unlock();
1776 WARN("Colour fills are not allowed on surfaces with resource access %#x.\n", desc.access);
1777 return D3DERR_INVALIDCALL;
1779 if ((desc.bind_flags & (WINED3D_BIND_RENDER_TARGET | WINED3D_BIND_SHADER_RESOURCE)) == WINED3D_BIND_SHADER_RESOURCE)
1781 wined3d_mutex_unlock();
1782 WARN("Colorfill is not allowed on non-RT textures, returning D3DERR_INVALIDCALL.\n");
1783 return D3DERR_INVALIDCALL;
1785 if (desc.bind_flags & WINED3D_BIND_DEPTH_STENCIL)
1787 wined3d_mutex_unlock();
1788 WARN("Colorfill is not allowed on depth stencil surfaces, returning D3DERR_INVALIDCALL.\n");
1789 return D3DERR_INVALIDCALL;
1792 rtv = d3d9_surface_acquire_rendertarget_view(surface_impl);
1793 hr = wined3d_device_clear_rendertarget_view(device->wined3d_device,
1794 rtv, rect, WINED3DCLEAR_TARGET, &c, 0.0f, 0);
1795 d3d9_surface_release_rendertarget_view(surface_impl, rtv);
1796 if (SUCCEEDED(hr) && surface_impl->texture)
1797 d3d9_texture_flag_auto_gen_mipmap(surface_impl->texture);
1799 wined3d_mutex_unlock();
1801 return hr;
1804 static HRESULT WINAPI d3d9_device_CreateOffscreenPlainSurface(IDirect3DDevice9Ex *iface,
1805 UINT width, UINT height, D3DFORMAT format, D3DPOOL pool, IDirect3DSurface9 **surface,
1806 HANDLE *shared_handle)
1808 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1809 unsigned int usage, access;
1810 void *user_mem = NULL;
1812 TRACE("iface %p, width %u, height %u, format %#x, pool %#x, surface %p, shared_handle %p.\n",
1813 iface, width, height, format, pool, surface, shared_handle);
1815 *surface = NULL;
1816 if (pool == D3DPOOL_MANAGED)
1818 WARN("Attempting to create a managed offscreen plain surface.\n");
1819 return D3DERR_INVALIDCALL;
1822 if (shared_handle)
1824 if (!device->d3d_parent->extended)
1826 WARN("Trying to create a shared or user memory surface on a non-ex device.\n");
1827 return E_NOTIMPL;
1830 if (pool == D3DPOOL_SYSTEMMEM)
1831 user_mem = *shared_handle;
1832 else
1834 if (pool != D3DPOOL_DEFAULT)
1836 WARN("Trying to create a shared surface in pool %#x.\n", pool);
1837 return D3DERR_INVALIDCALL;
1839 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1843 usage = 0;
1844 if (pool == D3DPOOL_SCRATCH)
1845 usage |= WINED3DUSAGE_SCRATCH;
1846 access = wined3daccess_from_d3dpool(pool, usage)
1847 | WINED3D_RESOURCE_ACCESS_MAP_R | WINED3D_RESOURCE_ACCESS_MAP_W;
1849 return d3d9_device_create_surface(device, 0, wined3dformat_from_d3dformat(format),
1850 WINED3D_MULTISAMPLE_NONE, 0, usage, 0, access, width, height, user_mem, surface);
1853 static HRESULT WINAPI d3d9_device_SetRenderTarget(IDirect3DDevice9Ex *iface, DWORD idx, IDirect3DSurface9 *surface)
1855 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1856 struct d3d9_surface *surface_impl = unsafe_impl_from_IDirect3DSurface9(surface);
1857 struct wined3d_rendertarget_view *rtv;
1858 HRESULT hr;
1860 TRACE("iface %p, idx %u, surface %p.\n", iface, idx, surface);
1862 if (idx >= D3D_MAX_SIMULTANEOUS_RENDERTARGETS)
1864 WARN("Invalid index %u specified.\n", idx);
1865 return D3DERR_INVALIDCALL;
1868 if (!idx && !surface_impl)
1870 WARN("Trying to set render target 0 to NULL.\n");
1871 return D3DERR_INVALIDCALL;
1874 if (surface_impl && d3d9_surface_get_device(surface_impl) != device)
1876 WARN("Render target surface does not match device.\n");
1877 return D3DERR_INVALIDCALL;
1880 wined3d_mutex_lock();
1881 rtv = surface_impl ? d3d9_surface_acquire_rendertarget_view(surface_impl) : NULL;
1882 hr = wined3d_device_set_rendertarget_view(device->wined3d_device, idx, rtv, TRUE);
1883 d3d9_surface_release_rendertarget_view(surface_impl, rtv);
1884 if (SUCCEEDED(hr))
1885 device->render_targets[idx] = surface_impl;
1886 wined3d_mutex_unlock();
1888 return hr;
1891 static HRESULT WINAPI d3d9_device_GetRenderTarget(IDirect3DDevice9Ex *iface, DWORD idx, IDirect3DSurface9 **surface)
1893 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1894 struct wined3d_rendertarget_view *wined3d_rtv;
1895 struct d3d9_surface *surface_impl;
1896 HRESULT hr = D3D_OK;
1898 TRACE("iface %p, idx %u, surface %p.\n", iface, idx, surface);
1900 if (!surface)
1901 return D3DERR_INVALIDCALL;
1903 if (idx >= D3D_MAX_SIMULTANEOUS_RENDERTARGETS)
1905 WARN("Invalid index %u specified.\n", idx);
1906 return D3DERR_INVALIDCALL;
1909 wined3d_mutex_lock();
1910 if ((wined3d_rtv = wined3d_device_get_rendertarget_view(device->wined3d_device, idx)))
1912 /* We want the sub resource parent here, since the view itself may be
1913 * internal to wined3d and may not have a parent. */
1914 surface_impl = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_rtv);
1915 *surface = &surface_impl->IDirect3DSurface9_iface;
1916 IDirect3DSurface9_AddRef(*surface);
1918 else
1920 hr = D3DERR_NOTFOUND;
1921 *surface = NULL;
1923 wined3d_mutex_unlock();
1925 return hr;
1928 static HRESULT WINAPI d3d9_device_SetDepthStencilSurface(IDirect3DDevice9Ex *iface, IDirect3DSurface9 *depth_stencil)
1930 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1931 struct d3d9_surface *ds_impl = unsafe_impl_from_IDirect3DSurface9(depth_stencil);
1932 struct wined3d_rendertarget_view *rtv;
1933 HRESULT hr;
1935 TRACE("iface %p, depth_stencil %p.\n", iface, depth_stencil);
1937 wined3d_mutex_lock();
1938 rtv = ds_impl ? d3d9_surface_acquire_rendertarget_view(ds_impl) : NULL;
1939 hr = wined3d_device_set_depth_stencil_view(device->wined3d_device, rtv);
1940 d3d9_surface_release_rendertarget_view(ds_impl, rtv);
1941 wined3d_mutex_unlock();
1943 return hr;
1946 static HRESULT WINAPI d3d9_device_GetDepthStencilSurface(IDirect3DDevice9Ex *iface, IDirect3DSurface9 **depth_stencil)
1948 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1949 struct wined3d_rendertarget_view *wined3d_dsv;
1950 struct d3d9_surface *surface_impl;
1951 HRESULT hr = D3D_OK;
1953 TRACE("iface %p, depth_stencil %p.\n", iface, depth_stencil);
1955 if (!depth_stencil)
1956 return D3DERR_INVALIDCALL;
1958 wined3d_mutex_lock();
1959 if ((wined3d_dsv = wined3d_device_get_depth_stencil_view(device->wined3d_device)))
1961 /* We want the sub resource parent here, since the view itself may be
1962 * internal to wined3d and may not have a parent. */
1963 surface_impl = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_dsv);
1964 *depth_stencil = &surface_impl->IDirect3DSurface9_iface;
1965 IDirect3DSurface9_AddRef(*depth_stencil);
1967 else
1969 hr = D3DERR_NOTFOUND;
1970 *depth_stencil = NULL;
1972 wined3d_mutex_unlock();
1974 return hr;
1977 static HRESULT WINAPI d3d9_device_BeginScene(IDirect3DDevice9Ex *iface)
1979 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1980 HRESULT hr;
1982 TRACE("iface %p.\n", iface);
1984 wined3d_mutex_lock();
1985 if (SUCCEEDED(hr = wined3d_device_begin_scene(device->wined3d_device)))
1986 device->in_scene = TRUE;
1987 wined3d_mutex_unlock();
1989 return hr;
1992 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_EndScene(IDirect3DDevice9Ex *iface)
1994 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1995 HRESULT hr;
1997 TRACE("iface %p.\n", iface);
1999 wined3d_mutex_lock();
2000 if (SUCCEEDED(hr = wined3d_device_end_scene(device->wined3d_device)))
2001 device->in_scene = FALSE;
2002 wined3d_mutex_unlock();
2004 return hr;
2007 static void d3d9_rts_flag_auto_gen_mipmap(struct d3d9_device *device)
2009 unsigned int i;
2011 for (i = 0; i < ARRAY_SIZE(device->render_targets); ++i)
2013 struct d3d9_surface *surface = device->render_targets[i];
2015 if (surface && surface->texture)
2016 d3d9_texture_flag_auto_gen_mipmap(surface->texture);
2020 static HRESULT WINAPI d3d9_device_Clear(IDirect3DDevice9Ex *iface, DWORD rect_count,
2021 const D3DRECT *rects, DWORD flags, D3DCOLOR color, float z, DWORD stencil)
2023 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2024 struct wined3d_color c;
2025 HRESULT hr;
2027 TRACE("iface %p, rect_count %u, rects %p, flags %#x, color 0x%08x, z %.8e, stencil %u.\n",
2028 iface, rect_count, rects, flags, color, z, stencil);
2030 if (rect_count && !rects)
2032 WARN("count %u with NULL rects.\n", rect_count);
2033 rect_count = 0;
2036 wined3d_color_from_d3dcolor(&c, color);
2037 wined3d_mutex_lock();
2038 hr = wined3d_device_clear(device->wined3d_device, rect_count, (const RECT *)rects, flags, &c, z, stencil);
2039 if (SUCCEEDED(hr))
2040 d3d9_rts_flag_auto_gen_mipmap(device);
2041 wined3d_mutex_unlock();
2043 return hr;
2046 static HRESULT WINAPI d3d9_device_SetTransform(IDirect3DDevice9Ex *iface,
2047 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
2049 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2051 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
2053 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
2054 wined3d_mutex_lock();
2055 wined3d_device_set_transform(device->wined3d_device, state, (const struct wined3d_matrix *)matrix);
2056 wined3d_mutex_unlock();
2058 return D3D_OK;
2061 static HRESULT WINAPI d3d9_device_GetTransform(IDirect3DDevice9Ex *iface,
2062 D3DTRANSFORMSTATETYPE state, D3DMATRIX *matrix)
2064 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2066 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
2068 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
2069 wined3d_mutex_lock();
2070 wined3d_device_get_transform(device->wined3d_device, state, (struct wined3d_matrix *)matrix);
2071 wined3d_mutex_unlock();
2073 return D3D_OK;
2076 static HRESULT WINAPI d3d9_device_MultiplyTransform(IDirect3DDevice9Ex *iface,
2077 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
2079 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2081 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
2083 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
2084 wined3d_mutex_lock();
2085 wined3d_device_multiply_transform(device->wined3d_device, state, (const struct wined3d_matrix *)matrix);
2086 wined3d_mutex_unlock();
2088 return D3D_OK;
2091 static HRESULT WINAPI d3d9_device_SetViewport(IDirect3DDevice9Ex *iface, const D3DVIEWPORT9 *viewport)
2093 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2094 struct wined3d_viewport vp;
2096 TRACE("iface %p, viewport %p.\n", iface, viewport);
2098 vp.x = viewport->X;
2099 vp.y = viewport->Y;
2100 vp.width = viewport->Width;
2101 vp.height = viewport->Height;
2102 vp.min_z = viewport->MinZ;
2103 vp.max_z = viewport->MaxZ;
2105 wined3d_mutex_lock();
2106 wined3d_device_set_viewports(device->wined3d_device, 1, &vp);
2107 wined3d_mutex_unlock();
2109 return D3D_OK;
2112 static HRESULT WINAPI d3d9_device_GetViewport(IDirect3DDevice9Ex *iface, D3DVIEWPORT9 *viewport)
2114 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2115 struct wined3d_viewport wined3d_viewport;
2117 TRACE("iface %p, viewport %p.\n", iface, viewport);
2119 wined3d_mutex_lock();
2120 wined3d_device_get_viewports(device->wined3d_device, NULL, &wined3d_viewport);
2121 wined3d_mutex_unlock();
2123 viewport->X = wined3d_viewport.x;
2124 viewport->Y = wined3d_viewport.y;
2125 viewport->Width = wined3d_viewport.width;
2126 viewport->Height = wined3d_viewport.height;
2127 viewport->MinZ = wined3d_viewport.min_z;
2128 viewport->MaxZ = wined3d_viewport.max_z;
2130 return D3D_OK;
2133 static HRESULT WINAPI d3d9_device_SetMaterial(IDirect3DDevice9Ex *iface, const D3DMATERIAL9 *material)
2135 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2137 TRACE("iface %p, material %p.\n", iface, material);
2139 /* Note: D3DMATERIAL9 is compatible with struct wined3d_material. */
2140 wined3d_mutex_lock();
2141 wined3d_device_set_material(device->wined3d_device, (const struct wined3d_material *)material);
2142 wined3d_mutex_unlock();
2144 return D3D_OK;
2147 static HRESULT WINAPI d3d9_device_GetMaterial(IDirect3DDevice9Ex *iface, D3DMATERIAL9 *material)
2149 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2151 TRACE("iface %p, material %p.\n", iface, material);
2153 /* Note: D3DMATERIAL9 is compatible with struct wined3d_material. */
2154 wined3d_mutex_lock();
2155 wined3d_device_get_material(device->wined3d_device, (struct wined3d_material *)material);
2156 wined3d_mutex_unlock();
2158 return D3D_OK;
2161 static HRESULT WINAPI d3d9_device_SetLight(IDirect3DDevice9Ex *iface, DWORD index, const D3DLIGHT9 *light)
2163 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2164 HRESULT hr;
2166 TRACE("iface %p, index %u, light %p.\n", iface, index, light);
2168 /* Note: D3DLIGHT9 is compatible with struct wined3d_light. */
2169 wined3d_mutex_lock();
2170 hr = wined3d_device_set_light(device->wined3d_device, index, (const struct wined3d_light *)light);
2171 wined3d_mutex_unlock();
2173 return hr;
2176 static HRESULT WINAPI d3d9_device_GetLight(IDirect3DDevice9Ex *iface, DWORD index, D3DLIGHT9 *light)
2178 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2179 HRESULT hr;
2181 TRACE("iface %p, index %u, light %p.\n", iface, index, light);
2183 /* Note: D3DLIGHT9 is compatible with struct wined3d_light. */
2184 wined3d_mutex_lock();
2185 hr = wined3d_device_get_light(device->wined3d_device, index, (struct wined3d_light *)light);
2186 wined3d_mutex_unlock();
2188 return hr;
2191 static HRESULT WINAPI d3d9_device_LightEnable(IDirect3DDevice9Ex *iface, DWORD index, BOOL enable)
2193 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2194 HRESULT hr;
2196 TRACE("iface %p, index %u, enable %#x.\n", iface, index, enable);
2198 wined3d_mutex_lock();
2199 hr = wined3d_device_set_light_enable(device->wined3d_device, index, enable);
2200 wined3d_mutex_unlock();
2202 return hr;
2205 static HRESULT WINAPI d3d9_device_GetLightEnable(IDirect3DDevice9Ex *iface, DWORD index, BOOL *enable)
2207 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2208 HRESULT hr;
2210 TRACE("iface %p, index %u, enable %p.\n", iface, index, enable);
2212 wined3d_mutex_lock();
2213 hr = wined3d_device_get_light_enable(device->wined3d_device, index, enable);
2214 wined3d_mutex_unlock();
2216 return hr;
2219 static HRESULT WINAPI d3d9_device_SetClipPlane(IDirect3DDevice9Ex *iface, DWORD index, const float *plane)
2221 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2222 HRESULT hr;
2224 TRACE("iface %p, index %u, plane %p.\n", iface, index, plane);
2226 index = min(index, device->max_user_clip_planes - 1);
2228 wined3d_mutex_lock();
2229 hr = wined3d_device_set_clip_plane(device->wined3d_device, index, (const struct wined3d_vec4 *)plane);
2230 wined3d_mutex_unlock();
2232 return hr;
2235 static HRESULT WINAPI d3d9_device_GetClipPlane(IDirect3DDevice9Ex *iface, DWORD index, float *plane)
2237 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2238 HRESULT hr;
2240 TRACE("iface %p, index %u, plane %p.\n", iface, index, plane);
2242 index = min(index, device->max_user_clip_planes - 1);
2244 wined3d_mutex_lock();
2245 hr = wined3d_device_get_clip_plane(device->wined3d_device, index, (struct wined3d_vec4 *)plane);
2246 wined3d_mutex_unlock();
2248 return hr;
2251 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_SetRenderState(IDirect3DDevice9Ex *iface,
2252 D3DRENDERSTATETYPE state, DWORD value)
2254 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2255 struct wined3d_color factor;
2257 TRACE("iface %p, state %#x, value %#x.\n", iface, state, value);
2259 if (state == D3DRS_BLENDFACTOR)
2261 wined3d_color_from_d3dcolor(&factor, value);
2262 wined3d_mutex_lock();
2263 wined3d_device_set_blend_state(device->wined3d_device, NULL, &factor);
2264 wined3d_mutex_unlock();
2266 return D3D_OK;
2269 wined3d_mutex_lock();
2270 wined3d_device_set_render_state(device->wined3d_device, state, value);
2271 wined3d_mutex_unlock();
2273 return D3D_OK;
2276 static HRESULT WINAPI d3d9_device_GetRenderState(IDirect3DDevice9Ex *iface,
2277 D3DRENDERSTATETYPE state, DWORD *value)
2279 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2280 struct wined3d_color factor;
2282 TRACE("iface %p, state %#x, value %p.\n", iface, state, value);
2284 if (state == D3DRS_BLENDFACTOR)
2286 wined3d_mutex_lock();
2287 wined3d_device_get_blend_state(device->wined3d_device, &factor);
2288 wined3d_mutex_unlock();
2289 *value = D3DCOLOR_COLORVALUE(factor.r, factor.g, factor.b, factor.a);
2291 return D3D_OK;
2294 wined3d_mutex_lock();
2295 *value = wined3d_device_get_render_state(device->wined3d_device, state);
2296 wined3d_mutex_unlock();
2298 return D3D_OK;
2301 static HRESULT WINAPI d3d9_device_CreateStateBlock(IDirect3DDevice9Ex *iface,
2302 D3DSTATEBLOCKTYPE type, IDirect3DStateBlock9 **stateblock)
2304 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2305 struct d3d9_stateblock *object;
2306 HRESULT hr;
2308 TRACE("iface %p, type %#x, stateblock %p.\n", iface, type, stateblock);
2310 if (type != D3DSBT_ALL && type != D3DSBT_PIXELSTATE && type != D3DSBT_VERTEXSTATE)
2312 WARN("Unexpected stateblock type, returning D3DERR_INVALIDCALL.\n");
2313 return D3DERR_INVALIDCALL;
2316 if (!(object = heap_alloc_zero(sizeof(*object))))
2317 return E_OUTOFMEMORY;
2319 hr = stateblock_init(object, device, type, NULL);
2320 if (FAILED(hr))
2322 WARN("Failed to initialize stateblock, hr %#x.\n", hr);
2323 heap_free(object);
2324 return hr;
2327 TRACE("Created stateblock %p.\n", object);
2328 *stateblock = &object->IDirect3DStateBlock9_iface;
2330 return D3D_OK;
2333 static HRESULT WINAPI d3d9_device_BeginStateBlock(IDirect3DDevice9Ex *iface)
2335 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2336 HRESULT hr;
2338 TRACE("iface %p.\n", iface);
2340 wined3d_mutex_lock();
2341 if (SUCCEEDED(hr = wined3d_device_begin_stateblock(device->wined3d_device)))
2342 device->recording = TRUE;
2343 wined3d_mutex_unlock();
2345 return hr;
2348 static HRESULT WINAPI d3d9_device_EndStateBlock(IDirect3DDevice9Ex *iface, IDirect3DStateBlock9 **stateblock)
2350 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2351 struct wined3d_stateblock *wined3d_stateblock;
2352 struct d3d9_stateblock *object;
2353 HRESULT hr;
2355 TRACE("iface %p, stateblock %p.\n", iface, stateblock);
2357 wined3d_mutex_lock();
2358 hr = wined3d_device_end_stateblock(device->wined3d_device, &wined3d_stateblock);
2359 wined3d_mutex_unlock();
2360 if (FAILED(hr))
2362 WARN("Failed to end the state block, hr %#x.\n", hr);
2363 return hr;
2365 device->recording = FALSE;
2367 if (!(object = heap_alloc_zero(sizeof(*object))))
2369 wined3d_mutex_lock();
2370 wined3d_stateblock_decref(wined3d_stateblock);
2371 wined3d_mutex_unlock();
2372 return E_OUTOFMEMORY;
2375 hr = stateblock_init(object, device, 0, wined3d_stateblock);
2376 if (FAILED(hr))
2378 WARN("Failed to initialize stateblock, hr %#x.\n", hr);
2379 wined3d_mutex_lock();
2380 wined3d_stateblock_decref(wined3d_stateblock);
2381 wined3d_mutex_unlock();
2382 heap_free(object);
2383 return hr;
2386 TRACE("Created stateblock %p.\n", object);
2387 *stateblock = &object->IDirect3DStateBlock9_iface;
2389 return D3D_OK;
2392 static HRESULT WINAPI d3d9_device_SetClipStatus(IDirect3DDevice9Ex *iface, const D3DCLIPSTATUS9 *clip_status)
2394 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2395 HRESULT hr;
2397 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
2399 wined3d_mutex_lock();
2400 hr = wined3d_device_set_clip_status(device->wined3d_device, (const struct wined3d_clip_status *)clip_status);
2401 wined3d_mutex_unlock();
2403 return hr;
2406 static HRESULT WINAPI d3d9_device_GetClipStatus(IDirect3DDevice9Ex *iface, D3DCLIPSTATUS9 *clip_status)
2408 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2409 HRESULT hr;
2411 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
2413 wined3d_mutex_lock();
2414 hr = wined3d_device_get_clip_status(device->wined3d_device, (struct wined3d_clip_status *)clip_status);
2415 wined3d_mutex_unlock();
2417 return hr;
2420 static HRESULT WINAPI d3d9_device_GetTexture(IDirect3DDevice9Ex *iface, DWORD stage, IDirect3DBaseTexture9 **texture)
2422 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2423 struct wined3d_texture *wined3d_texture = NULL;
2424 struct d3d9_texture *texture_impl;
2426 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
2428 if (!texture)
2429 return D3DERR_INVALIDCALL;
2431 wined3d_mutex_lock();
2432 if ((wined3d_texture = wined3d_device_get_texture(device->wined3d_device, stage)))
2434 texture_impl = wined3d_texture_get_parent(wined3d_texture);
2435 *texture = &texture_impl->IDirect3DBaseTexture9_iface;
2436 IDirect3DBaseTexture9_AddRef(*texture);
2438 else
2440 *texture = NULL;
2442 wined3d_mutex_unlock();
2444 return D3D_OK;
2447 static HRESULT WINAPI d3d9_device_SetTexture(IDirect3DDevice9Ex *iface, DWORD stage, IDirect3DBaseTexture9 *texture)
2449 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2450 struct d3d9_texture *texture_impl;
2451 HRESULT hr;
2453 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
2455 texture_impl = unsafe_impl_from_IDirect3DBaseTexture9(texture);
2457 wined3d_mutex_lock();
2458 hr = wined3d_device_set_texture(device->wined3d_device, stage,
2459 texture_impl ? texture_impl->wined3d_texture : NULL);
2460 if (SUCCEEDED(hr) && !device->recording)
2462 unsigned int i = stage < 16 || (stage >= D3DVERTEXTEXTURESAMPLER0 && stage <= D3DVERTEXTEXTURESAMPLER3)
2463 ? stage < 16 ? stage : stage - D3DVERTEXTEXTURESAMPLER0 + 16 : ~0u;
2465 if (i < D3D9_MAX_TEXTURE_UNITS)
2467 if (texture_impl && texture_impl->usage & D3DUSAGE_AUTOGENMIPMAP)
2468 device->auto_mipmaps |= 1u << i;
2469 else
2470 device->auto_mipmaps &= ~(1u << i);
2473 wined3d_mutex_unlock();
2475 return hr;
2478 static const enum wined3d_texture_stage_state tss_lookup[] =
2480 WINED3D_TSS_INVALID, /* 0, unused */
2481 WINED3D_TSS_COLOR_OP, /* 1, D3DTSS_COLOROP */
2482 WINED3D_TSS_COLOR_ARG1, /* 2, D3DTSS_COLORARG1 */
2483 WINED3D_TSS_COLOR_ARG2, /* 3, D3DTSS_COLORARG2 */
2484 WINED3D_TSS_ALPHA_OP, /* 4, D3DTSS_ALPHAOP */
2485 WINED3D_TSS_ALPHA_ARG1, /* 5, D3DTSS_ALPHAARG1 */
2486 WINED3D_TSS_ALPHA_ARG2, /* 6, D3DTSS_ALPHAARG2 */
2487 WINED3D_TSS_BUMPENV_MAT00, /* 7, D3DTSS_BUMPENVMAT00 */
2488 WINED3D_TSS_BUMPENV_MAT01, /* 8, D3DTSS_BUMPENVMAT01 */
2489 WINED3D_TSS_BUMPENV_MAT10, /* 9, D3DTSS_BUMPENVMAT10 */
2490 WINED3D_TSS_BUMPENV_MAT11, /* 10, D3DTSS_BUMPENVMAT11 */
2491 WINED3D_TSS_TEXCOORD_INDEX, /* 11, D3DTSS_TEXCOORDINDEX */
2492 WINED3D_TSS_INVALID, /* 12, unused */
2493 WINED3D_TSS_INVALID, /* 13, unused */
2494 WINED3D_TSS_INVALID, /* 14, unused */
2495 WINED3D_TSS_INVALID, /* 15, unused */
2496 WINED3D_TSS_INVALID, /* 16, unused */
2497 WINED3D_TSS_INVALID, /* 17, unused */
2498 WINED3D_TSS_INVALID, /* 18, unused */
2499 WINED3D_TSS_INVALID, /* 19, unused */
2500 WINED3D_TSS_INVALID, /* 20, unused */
2501 WINED3D_TSS_INVALID, /* 21, unused */
2502 WINED3D_TSS_BUMPENV_LSCALE, /* 22, D3DTSS_BUMPENVLSCALE */
2503 WINED3D_TSS_BUMPENV_LOFFSET, /* 23, D3DTSS_BUMPENVLOFFSET */
2504 WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS, /* 24, D3DTSS_TEXTURETRANSFORMFLAGS */
2505 WINED3D_TSS_INVALID, /* 25, unused */
2506 WINED3D_TSS_COLOR_ARG0, /* 26, D3DTSS_COLORARG0 */
2507 WINED3D_TSS_ALPHA_ARG0, /* 27, D3DTSS_ALPHAARG0 */
2508 WINED3D_TSS_RESULT_ARG, /* 28, D3DTSS_RESULTARG */
2509 WINED3D_TSS_INVALID, /* 29, unused */
2510 WINED3D_TSS_INVALID, /* 30, unused */
2511 WINED3D_TSS_INVALID, /* 31, unused */
2512 WINED3D_TSS_CONSTANT, /* 32, D3DTSS_CONSTANT */
2515 static HRESULT WINAPI d3d9_device_GetTextureStageState(IDirect3DDevice9Ex *iface,
2516 DWORD stage, D3DTEXTURESTAGESTATETYPE state, DWORD *value)
2518 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2520 TRACE("iface %p, stage %u, state %#x, value %p.\n", iface, stage, state, value);
2522 if (state >= ARRAY_SIZE(tss_lookup))
2524 WARN("Invalid state %#x passed.\n", state);
2525 return D3D_OK;
2528 wined3d_mutex_lock();
2529 *value = wined3d_device_get_texture_stage_state(device->wined3d_device, stage, tss_lookup[state]);
2530 wined3d_mutex_unlock();
2532 return D3D_OK;
2535 static HRESULT WINAPI d3d9_device_SetTextureStageState(IDirect3DDevice9Ex *iface,
2536 DWORD stage, D3DTEXTURESTAGESTATETYPE state, DWORD value)
2538 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2540 TRACE("iface %p, stage %u, state %#x, value %#x.\n", iface, stage, state, value);
2542 if (state >= ARRAY_SIZE(tss_lookup))
2544 WARN("Invalid state %#x passed.\n", state);
2545 return D3D_OK;
2548 wined3d_mutex_lock();
2549 wined3d_device_set_texture_stage_state(device->wined3d_device, stage, tss_lookup[state], value);
2550 wined3d_mutex_unlock();
2552 return D3D_OK;
2555 static HRESULT WINAPI d3d9_device_GetSamplerState(IDirect3DDevice9Ex *iface,
2556 DWORD sampler, D3DSAMPLERSTATETYPE state, DWORD *value)
2558 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2560 TRACE("iface %p, sampler %u, state %#x, value %p.\n", iface, sampler, state, value);
2562 wined3d_mutex_lock();
2563 *value = wined3d_device_get_sampler_state(device->wined3d_device, sampler, state);
2564 wined3d_mutex_unlock();
2566 return D3D_OK;
2569 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_SetSamplerState(IDirect3DDevice9Ex *iface,
2570 DWORD sampler, D3DSAMPLERSTATETYPE state, DWORD value)
2572 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2574 TRACE("iface %p, sampler %u, state %#x, value %#x.\n", iface, sampler, state, value);
2576 wined3d_mutex_lock();
2577 wined3d_device_set_sampler_state(device->wined3d_device, sampler, state, value);
2578 wined3d_mutex_unlock();
2580 return D3D_OK;
2583 static HRESULT WINAPI d3d9_device_ValidateDevice(IDirect3DDevice9Ex *iface, DWORD *pass_count)
2585 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2586 HRESULT hr;
2588 TRACE("iface %p, pass_count %p.\n", iface, pass_count);
2590 wined3d_mutex_lock();
2591 hr = wined3d_device_validate_device(device->wined3d_device, pass_count);
2592 wined3d_mutex_unlock();
2594 return hr;
2597 static HRESULT WINAPI d3d9_device_SetPaletteEntries(IDirect3DDevice9Ex *iface,
2598 UINT palette_idx, const PALETTEENTRY *entries)
2600 WARN("iface %p, palette_idx %u, entries %p unimplemented.\n", iface, palette_idx, entries);
2602 /* The d3d9 palette API is non-functional on Windows. Getters and setters are implemented,
2603 * and some drivers allow the creation of P8 surfaces. These surfaces can be copied to
2604 * other P8 surfaces with StretchRect, but cannot be converted to (A)RGB.
2606 * Some older(dx7) cards may have support for P8 textures, but games cannot rely on this. */
2607 return D3D_OK;
2610 static HRESULT WINAPI d3d9_device_GetPaletteEntries(IDirect3DDevice9Ex *iface,
2611 UINT palette_idx, PALETTEENTRY *entries)
2613 FIXME("iface %p, palette_idx %u, entries %p unimplemented.\n", iface, palette_idx, entries);
2615 return D3DERR_INVALIDCALL;
2618 static HRESULT WINAPI d3d9_device_SetCurrentTexturePalette(IDirect3DDevice9Ex *iface, UINT palette_idx)
2620 WARN("iface %p, palette_idx %u unimplemented.\n", iface, palette_idx);
2622 return D3D_OK;
2625 static HRESULT WINAPI d3d9_device_GetCurrentTexturePalette(IDirect3DDevice9Ex *iface, UINT *palette_idx)
2627 FIXME("iface %p, palette_idx %p.\n", iface, palette_idx);
2629 return D3DERR_INVALIDCALL;
2632 static HRESULT WINAPI d3d9_device_SetScissorRect(IDirect3DDevice9Ex *iface, const RECT *rect)
2634 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2636 TRACE("iface %p, rect %p.\n", iface, rect);
2638 wined3d_mutex_lock();
2639 wined3d_device_set_scissor_rects(device->wined3d_device, 1, rect);
2640 wined3d_mutex_unlock();
2642 return D3D_OK;
2645 static HRESULT WINAPI d3d9_device_GetScissorRect(IDirect3DDevice9Ex *iface, RECT *rect)
2647 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2649 TRACE("iface %p, rect %p.\n", iface, rect);
2651 wined3d_mutex_lock();
2652 wined3d_device_get_scissor_rects(device->wined3d_device, NULL, rect);
2653 wined3d_mutex_unlock();
2655 return D3D_OK;
2658 static HRESULT WINAPI d3d9_device_SetSoftwareVertexProcessing(IDirect3DDevice9Ex *iface, BOOL software)
2660 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2662 TRACE("iface %p, software %#x.\n", iface, software);
2664 wined3d_mutex_lock();
2665 wined3d_device_set_software_vertex_processing(device->wined3d_device, software);
2666 wined3d_mutex_unlock();
2668 return D3D_OK;
2671 static BOOL WINAPI d3d9_device_GetSoftwareVertexProcessing(IDirect3DDevice9Ex *iface)
2673 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2674 BOOL ret;
2676 TRACE("iface %p.\n", iface);
2678 wined3d_mutex_lock();
2679 ret = wined3d_device_get_software_vertex_processing(device->wined3d_device);
2680 wined3d_mutex_unlock();
2682 return ret;
2685 static HRESULT WINAPI d3d9_device_SetNPatchMode(IDirect3DDevice9Ex *iface, float segment_count)
2687 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2688 HRESULT hr;
2690 TRACE("iface %p, segment_count %.8e.\n", iface, segment_count);
2692 wined3d_mutex_lock();
2693 hr = wined3d_device_set_npatch_mode(device->wined3d_device, segment_count);
2694 wined3d_mutex_unlock();
2696 return hr;
2699 static float WINAPI d3d9_device_GetNPatchMode(IDirect3DDevice9Ex *iface)
2701 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2702 float ret;
2704 TRACE("iface %p.\n", iface);
2706 wined3d_mutex_lock();
2707 ret = wined3d_device_get_npatch_mode(device->wined3d_device);
2708 wined3d_mutex_unlock();
2710 return ret;
2713 /* wined3d critical section must be taken by the caller. */
2714 static void d3d9_generate_auto_mipmaps(struct d3d9_device *device)
2716 struct wined3d_texture *texture;
2717 unsigned int i, stage, map;
2719 map = device->auto_mipmaps;
2720 while (map)
2722 i = wined3d_bit_scan(&map);
2724 stage = i >= 16 ? i - 16 + D3DVERTEXTEXTURESAMPLER0 : i;
2725 if ((texture = wined3d_device_get_texture(device->wined3d_device, stage)))
2726 d3d9_texture_gen_auto_mipmap(wined3d_texture_get_parent(texture));
2730 static void d3d9_device_upload_sysmem_vertex_buffers(struct d3d9_device *device,
2731 int base_vertex, unsigned int start_vertex, unsigned int vertex_count)
2733 struct wined3d_box box = {0, 0, 0, 1, 0, 1};
2734 struct d3d9_vertexbuffer *d3d9_buffer;
2735 struct wined3d_resource *dst_resource;
2736 unsigned int i, offset, stride, map;
2737 struct wined3d_buffer *dst_buffer;
2738 struct wined3d_resource_desc desc;
2739 HRESULT hr;
2741 if (!device->sysmem_vb)
2742 return;
2744 if (base_vertex >= 0 || start_vertex >= -base_vertex)
2745 start_vertex += base_vertex;
2746 else
2747 FIXME("System memory vertex data offset is negative.\n");
2749 map = device->sysmem_vb;
2750 while (map)
2752 i = wined3d_bit_scan(&map);
2754 if (FAILED(hr = wined3d_device_get_stream_source(device->wined3d_device, i, &dst_buffer, &offset, &stride)))
2755 ERR("Failed to get stream source.\n");
2756 d3d9_buffer = wined3d_buffer_get_parent(dst_buffer);
2757 dst_resource = wined3d_buffer_get_resource(dst_buffer);
2758 wined3d_resource_get_desc(dst_resource, &desc);
2759 box.left = offset + start_vertex * stride;
2760 box.right = min(box.left + vertex_count * stride, desc.size);
2761 if (FAILED(hr = wined3d_device_copy_sub_resource_region(device->wined3d_device,
2762 dst_resource, 0, box.left, 0, 0,
2763 wined3d_buffer_get_resource(d3d9_buffer->wined3d_buffer), 0, &box, 0)))
2764 ERR("Failed to update buffer.\n");
2768 static void d3d9_device_upload_sysmem_index_buffer(struct d3d9_device *device,
2769 unsigned int start_idx, unsigned int idx_count)
2771 struct wined3d_box box = {0, 0, 0, 1, 0, 1};
2772 struct wined3d_resource *dst_resource;
2773 struct d3d9_indexbuffer *d3d9_buffer;
2774 struct wined3d_buffer *dst_buffer;
2775 struct wined3d_resource_desc desc;
2776 enum wined3d_format_id format;
2777 unsigned int offset, idx_size;
2778 HRESULT hr;
2780 if (!device->sysmem_ib)
2781 return;
2783 if (!(dst_buffer = wined3d_device_get_index_buffer(device->wined3d_device, &format, &offset)))
2784 ERR("Failed to get index buffer.\n");
2785 d3d9_buffer = wined3d_buffer_get_parent(dst_buffer);
2786 dst_resource = wined3d_buffer_get_resource(dst_buffer);
2787 wined3d_resource_get_desc(dst_resource, &desc);
2788 idx_size = format == WINED3DFMT_R16_UINT ? 2 : 4;
2789 box.left = offset + start_idx * idx_size;
2790 box.right = min(box.left + idx_count * idx_size, desc.size);
2791 if (FAILED(hr = wined3d_device_copy_sub_resource_region(device->wined3d_device,
2792 dst_resource, 0, box.left, 0, 0,
2793 wined3d_buffer_get_resource(d3d9_buffer->wined3d_buffer), 0, &box, 0)))
2794 ERR("Failed to update buffer.\n");
2797 static HRESULT WINAPI d3d9_device_DrawPrimitive(IDirect3DDevice9Ex *iface,
2798 D3DPRIMITIVETYPE primitive_type, UINT start_vertex, UINT primitive_count)
2800 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2801 unsigned int vertex_count;
2802 HRESULT hr;
2804 TRACE("iface %p, primitive_type %#x, start_vertex %u, primitive_count %u.\n",
2805 iface, primitive_type, start_vertex, primitive_count);
2807 wined3d_mutex_lock();
2808 if (!device->has_vertex_declaration)
2810 wined3d_mutex_unlock();
2811 WARN("Called without a valid vertex declaration set.\n");
2812 return D3DERR_INVALIDCALL;
2814 vertex_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2815 d3d9_device_upload_sysmem_vertex_buffers(device, 0, start_vertex, vertex_count);
2816 d3d9_generate_auto_mipmaps(device);
2817 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type, 0);
2818 hr = wined3d_device_draw_primitive(device->wined3d_device, start_vertex, vertex_count);
2819 if (SUCCEEDED(hr))
2820 d3d9_rts_flag_auto_gen_mipmap(device);
2821 wined3d_mutex_unlock();
2823 return hr;
2826 static HRESULT WINAPI d3d9_device_DrawIndexedPrimitive(IDirect3DDevice9Ex *iface,
2827 D3DPRIMITIVETYPE primitive_type, INT base_vertex_idx, UINT min_vertex_idx,
2828 UINT vertex_count, UINT start_idx, UINT primitive_count)
2830 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2831 unsigned int index_count;
2832 HRESULT hr;
2834 TRACE("iface %p, primitive_type %#x, base_vertex_idx %u, min_vertex_idx %u, "
2835 "vertex_count %u, start_idx %u, primitive_count %u.\n",
2836 iface, primitive_type, base_vertex_idx, min_vertex_idx,
2837 vertex_count, start_idx, primitive_count);
2839 wined3d_mutex_lock();
2840 if (!device->has_vertex_declaration)
2842 wined3d_mutex_unlock();
2843 WARN("Called without a valid vertex declaration set.\n");
2844 return D3DERR_INVALIDCALL;
2846 index_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2847 d3d9_device_upload_sysmem_vertex_buffers(device, base_vertex_idx, min_vertex_idx, vertex_count);
2848 d3d9_device_upload_sysmem_index_buffer(device, start_idx, index_count);
2849 d3d9_generate_auto_mipmaps(device);
2850 wined3d_device_set_base_vertex_index(device->wined3d_device, base_vertex_idx);
2851 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type, 0);
2852 hr = wined3d_device_draw_indexed_primitive(device->wined3d_device, start_idx, index_count);
2853 if (SUCCEEDED(hr))
2854 d3d9_rts_flag_auto_gen_mipmap(device);
2855 wined3d_mutex_unlock();
2857 return hr;
2860 /* The caller is responsible for wined3d locking */
2861 static HRESULT d3d9_device_prepare_vertex_buffer(struct d3d9_device *device, UINT min_size)
2863 HRESULT hr;
2865 if (device->vertex_buffer_size < min_size || !device->vertex_buffer)
2867 UINT size = max(device->vertex_buffer_size * 2, min_size);
2868 struct wined3d_buffer_desc desc;
2869 struct wined3d_buffer *buffer;
2871 TRACE("Growing vertex buffer to %u bytes.\n", size);
2873 desc.byte_width = size;
2874 desc.usage = WINED3DUSAGE_DYNAMIC;
2875 desc.bind_flags = WINED3D_BIND_VERTEX_BUFFER;
2876 desc.access = WINED3D_RESOURCE_ACCESS_GPU | WINED3D_RESOURCE_ACCESS_MAP_W;
2877 desc.misc_flags = 0;
2878 desc.structure_byte_stride = 0;
2880 if (FAILED(hr = wined3d_buffer_create(device->wined3d_device, &desc,
2881 NULL, NULL, &d3d9_null_wined3d_parent_ops, &buffer)))
2883 ERR("Failed to create vertex buffer, hr %#x.\n", hr);
2884 return hr;
2887 if (device->vertex_buffer)
2888 wined3d_buffer_decref(device->vertex_buffer);
2890 device->vertex_buffer = buffer;
2891 device->vertex_buffer_size = size;
2892 device->vertex_buffer_pos = 0;
2894 return D3D_OK;
2897 static HRESULT WINAPI d3d9_device_DrawPrimitiveUP(IDirect3DDevice9Ex *iface,
2898 D3DPRIMITIVETYPE primitive_type, UINT primitive_count, const void *data, UINT stride)
2900 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2901 HRESULT hr;
2902 UINT vtx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2903 struct wined3d_map_desc wined3d_map_desc;
2904 struct wined3d_box wined3d_box = {0};
2905 UINT size = vtx_count * stride;
2906 struct wined3d_resource *vb;
2907 UINT vb_pos, align;
2909 TRACE("iface %p, primitive_type %#x, primitive_count %u, data %p, stride %u.\n",
2910 iface, primitive_type, primitive_count, data, stride);
2912 if (!primitive_count)
2914 WARN("primitive_count is 0, returning D3D_OK\n");
2915 return D3D_OK;
2918 wined3d_mutex_lock();
2920 if (!device->has_vertex_declaration)
2922 wined3d_mutex_unlock();
2923 WARN("Called without a valid vertex declaration set.\n");
2924 return D3DERR_INVALIDCALL;
2927 hr = d3d9_device_prepare_vertex_buffer(device, size);
2928 if (FAILED(hr))
2929 goto done;
2931 vb_pos = device->vertex_buffer_pos;
2932 align = vb_pos % stride;
2933 if (align) align = stride - align;
2934 if (vb_pos + size + align > device->vertex_buffer_size)
2935 vb_pos = 0;
2936 else
2937 vb_pos += align;
2939 wined3d_box.left = vb_pos;
2940 wined3d_box.right = vb_pos + size;
2941 vb = wined3d_buffer_get_resource(device->vertex_buffer);
2942 if (FAILED(hr = wined3d_resource_map(vb, 0, &wined3d_map_desc, &wined3d_box,
2943 WINED3D_MAP_WRITE | (vb_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD))))
2944 goto done;
2945 memcpy(wined3d_map_desc.data, data, size);
2946 wined3d_resource_unmap(vb, 0);
2947 device->vertex_buffer_pos = vb_pos + size;
2949 hr = wined3d_device_set_stream_source(device->wined3d_device, 0, device->vertex_buffer, 0, stride);
2950 if (FAILED(hr))
2951 goto done;
2953 d3d9_generate_auto_mipmaps(device);
2954 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type, 0);
2955 hr = wined3d_device_draw_primitive(device->wined3d_device, vb_pos / stride, vtx_count);
2956 wined3d_device_set_stream_source(device->wined3d_device, 0, NULL, 0, 0);
2957 if (SUCCEEDED(hr))
2958 d3d9_rts_flag_auto_gen_mipmap(device);
2960 done:
2961 wined3d_mutex_unlock();
2962 return hr;
2965 /* The caller is responsible for wined3d locking */
2966 static HRESULT d3d9_device_prepare_index_buffer(struct d3d9_device *device, UINT min_size)
2968 HRESULT hr;
2970 if (device->index_buffer_size < min_size || !device->index_buffer)
2972 UINT size = max(device->index_buffer_size * 2, min_size);
2973 struct wined3d_buffer_desc desc;
2974 struct wined3d_buffer *buffer;
2976 TRACE("Growing index buffer to %u bytes.\n", size);
2978 desc.byte_width = size;
2979 desc.usage = WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_STATICDECL;
2980 desc.bind_flags = WINED3D_BIND_INDEX_BUFFER;
2981 desc.access = WINED3D_RESOURCE_ACCESS_GPU | WINED3D_RESOURCE_ACCESS_MAP_W;
2982 desc.misc_flags = 0;
2983 desc.structure_byte_stride = 0;
2985 if (FAILED(hr = wined3d_buffer_create(device->wined3d_device, &desc,
2986 NULL, NULL, &d3d9_null_wined3d_parent_ops, &buffer)))
2988 ERR("Failed to create index buffer, hr %#x.\n", hr);
2989 return hr;
2992 if (device->index_buffer)
2993 wined3d_buffer_decref(device->index_buffer);
2995 device->index_buffer = buffer;
2996 device->index_buffer_size = size;
2997 device->index_buffer_pos = 0;
2999 return D3D_OK;
3002 static HRESULT WINAPI d3d9_device_DrawIndexedPrimitiveUP(IDirect3DDevice9Ex *iface,
3003 D3DPRIMITIVETYPE primitive_type, UINT min_vertex_idx, UINT vertex_count,
3004 UINT primitive_count, const void *index_data, D3DFORMAT index_format,
3005 const void *vertex_data, UINT vertex_stride)
3007 UINT idx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
3008 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3009 UINT idx_fmt_size = index_format == D3DFMT_INDEX16 ? 2 : 4;
3010 UINT vtx_size = vertex_count * vertex_stride;
3011 UINT idx_size = idx_count * idx_fmt_size;
3012 struct wined3d_map_desc wined3d_map_desc;
3013 struct wined3d_box wined3d_box = {0};
3014 struct wined3d_resource *ib, *vb;
3015 UINT vb_pos, ib_pos, align;
3016 HRESULT hr;
3018 TRACE("iface %p, primitive_type %#x, min_vertex_idx %u, vertex_count %u, primitive_count %u, "
3019 "index_data %p, index_format %#x, vertex_data %p, vertex_stride %u.\n",
3020 iface, primitive_type, min_vertex_idx, vertex_count, primitive_count,
3021 index_data, index_format, vertex_data, vertex_stride);
3023 if (!primitive_count)
3025 WARN("primitive_count is 0, returning D3D_OK.\n");
3026 return D3D_OK;
3029 wined3d_mutex_lock();
3031 if (!device->has_vertex_declaration)
3033 wined3d_mutex_unlock();
3034 WARN("Called without a valid vertex declaration set.\n");
3035 return D3DERR_INVALIDCALL;
3038 hr = d3d9_device_prepare_vertex_buffer(device, vtx_size);
3039 if (FAILED(hr))
3040 goto done;
3042 vb_pos = device->vertex_buffer_pos;
3043 align = vb_pos % vertex_stride;
3044 if (align) align = vertex_stride - align;
3045 if (vb_pos + vtx_size + align > device->vertex_buffer_size)
3046 vb_pos = 0;
3047 else
3048 vb_pos += align;
3050 wined3d_box.left = vb_pos;
3051 wined3d_box.right = vb_pos + vtx_size;
3052 vb = wined3d_buffer_get_resource(device->vertex_buffer);
3053 if (FAILED(hr = wined3d_resource_map(vb, 0, &wined3d_map_desc, &wined3d_box,
3054 WINED3D_MAP_WRITE | (vb_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD))))
3055 goto done;
3056 memcpy(wined3d_map_desc.data, (char *)vertex_data + min_vertex_idx * vertex_stride, vtx_size);
3057 wined3d_resource_unmap(vb, 0);
3058 device->vertex_buffer_pos = vb_pos + vtx_size;
3060 hr = d3d9_device_prepare_index_buffer(device, idx_size);
3061 if (FAILED(hr))
3062 goto done;
3064 ib_pos = device->index_buffer_pos;
3065 align = ib_pos % idx_fmt_size;
3066 if (align) align = idx_fmt_size - align;
3067 if (ib_pos + idx_size + align > device->index_buffer_size)
3068 ib_pos = 0;
3069 else
3070 ib_pos += align;
3072 wined3d_box.left = ib_pos;
3073 wined3d_box.right = ib_pos + idx_size;
3074 ib = wined3d_buffer_get_resource(device->index_buffer);
3075 if (FAILED(hr = wined3d_resource_map(ib, 0, &wined3d_map_desc, &wined3d_box,
3076 WINED3D_MAP_WRITE | (ib_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD))))
3077 goto done;
3078 memcpy(wined3d_map_desc.data, index_data, idx_size);
3079 wined3d_resource_unmap(ib, 0);
3080 device->index_buffer_pos = ib_pos + idx_size;
3082 hr = wined3d_device_set_stream_source(device->wined3d_device, 0, device->vertex_buffer, 0, vertex_stride);
3083 if (FAILED(hr))
3084 goto done;
3086 d3d9_generate_auto_mipmaps(device);
3087 wined3d_device_set_index_buffer(device->wined3d_device, device->index_buffer,
3088 wined3dformat_from_d3dformat(index_format), 0);
3089 wined3d_device_set_base_vertex_index(device->wined3d_device, vb_pos / vertex_stride - min_vertex_idx);
3091 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type, 0);
3092 hr = wined3d_device_draw_indexed_primitive(device->wined3d_device, ib_pos / idx_fmt_size, idx_count);
3094 wined3d_device_set_stream_source(device->wined3d_device, 0, NULL, 0, 0);
3095 wined3d_device_set_index_buffer(device->wined3d_device, NULL, WINED3DFMT_UNKNOWN, 0);
3097 if (SUCCEEDED(hr))
3098 d3d9_rts_flag_auto_gen_mipmap(device);
3100 done:
3101 wined3d_mutex_unlock();
3102 return hr;
3105 static HRESULT WINAPI d3d9_device_ProcessVertices(IDirect3DDevice9Ex *iface,
3106 UINT src_start_idx, UINT dst_idx, UINT vertex_count, IDirect3DVertexBuffer9 *dst_buffer,
3107 IDirect3DVertexDeclaration9 *declaration, DWORD flags)
3109 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3110 struct d3d9_vertexbuffer *dst_impl = unsafe_impl_from_IDirect3DVertexBuffer9(dst_buffer);
3111 struct d3d9_vertex_declaration *decl_impl = unsafe_impl_from_IDirect3DVertexDeclaration9(declaration);
3112 struct d3d9_vertexbuffer *d3d9_buffer;
3113 struct wined3d_buffer *wined3d_buffer;
3114 unsigned int i, offset, stride, map;
3115 HRESULT hr;
3117 TRACE("iface %p, src_start_idx %u, dst_idx %u, vertex_count %u, dst_buffer %p, declaration %p, flags %#x.\n",
3118 iface, src_start_idx, dst_idx, vertex_count, dst_buffer, declaration, flags);
3120 wined3d_mutex_lock();
3122 /* Note that an alternative approach would be to simply create these
3123 * buffers with WINED3D_RESOURCE_ACCESS_MAP_R and update them here like we
3124 * do for draws. In some regards that would be easier, but it seems less
3125 * than optimal to upload data to the GPU only to subsequently download it
3126 * again. */
3127 map = device->sysmem_vb;
3128 while (map)
3130 i = wined3d_bit_scan(&map);
3132 if (FAILED(wined3d_device_get_stream_source(device->wined3d_device,
3133 i, &wined3d_buffer, &offset, &stride)))
3134 ERR("Failed to get stream source.\n");
3135 d3d9_buffer = wined3d_buffer_get_parent(wined3d_buffer);
3136 if (FAILED(wined3d_device_set_stream_source(device->wined3d_device,
3137 i, d3d9_buffer->wined3d_buffer, offset, stride)))
3138 ERR("Failed to set stream source.\n");
3141 hr = wined3d_device_process_vertices(device->wined3d_device, src_start_idx, dst_idx, vertex_count,
3142 dst_impl->wined3d_buffer, decl_impl ? decl_impl->wined3d_declaration : NULL,
3143 flags, dst_impl->fvf);
3145 map = device->sysmem_vb;
3146 while (map)
3148 i = wined3d_bit_scan(&map);
3150 if (FAILED(wined3d_device_get_stream_source(device->wined3d_device,
3151 i, &wined3d_buffer, &offset, &stride)))
3152 ERR("Failed to get stream source.\n");
3153 d3d9_buffer = wined3d_buffer_get_parent(wined3d_buffer);
3154 if (FAILED(wined3d_device_set_stream_source(device->wined3d_device,
3155 i, d3d9_buffer->draw_buffer, offset, stride)))
3156 ERR("Failed to set stream source.\n");
3159 wined3d_mutex_unlock();
3161 return hr;
3164 static HRESULT WINAPI d3d9_device_CreateVertexDeclaration(IDirect3DDevice9Ex *iface,
3165 const D3DVERTEXELEMENT9 *elements, IDirect3DVertexDeclaration9 **declaration)
3167 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3168 struct d3d9_vertex_declaration *object;
3169 HRESULT hr;
3171 TRACE("iface %p, elements %p, declaration %p.\n", iface, elements, declaration);
3173 if (!declaration)
3175 WARN("Caller passed a NULL declaration, returning D3DERR_INVALIDCALL.\n");
3176 return D3DERR_INVALIDCALL;
3179 if (SUCCEEDED(hr = d3d9_vertex_declaration_create(device, elements, &object)))
3180 *declaration = &object->IDirect3DVertexDeclaration9_iface;
3182 return hr;
3185 static HRESULT WINAPI d3d9_device_SetVertexDeclaration(IDirect3DDevice9Ex *iface,
3186 IDirect3DVertexDeclaration9 *declaration)
3188 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3189 struct d3d9_vertex_declaration *decl_impl = unsafe_impl_from_IDirect3DVertexDeclaration9(declaration);
3191 TRACE("iface %p, declaration %p.\n", iface, declaration);
3193 wined3d_mutex_lock();
3194 wined3d_device_set_vertex_declaration(device->wined3d_device,
3195 decl_impl ? decl_impl->wined3d_declaration : NULL);
3196 device->has_vertex_declaration = !!decl_impl;
3197 wined3d_mutex_unlock();
3199 return D3D_OK;
3202 static HRESULT WINAPI d3d9_device_GetVertexDeclaration(IDirect3DDevice9Ex *iface,
3203 IDirect3DVertexDeclaration9 **declaration)
3205 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3206 struct wined3d_vertex_declaration *wined3d_declaration;
3207 struct d3d9_vertex_declaration *declaration_impl;
3209 TRACE("iface %p, declaration %p.\n", iface, declaration);
3211 if (!declaration) return D3DERR_INVALIDCALL;
3213 wined3d_mutex_lock();
3214 if ((wined3d_declaration = wined3d_device_get_vertex_declaration(device->wined3d_device)))
3216 declaration_impl = wined3d_vertex_declaration_get_parent(wined3d_declaration);
3217 *declaration = &declaration_impl->IDirect3DVertexDeclaration9_iface;
3218 IDirect3DVertexDeclaration9_AddRef(*declaration);
3220 else
3222 *declaration = NULL;
3224 wined3d_mutex_unlock();
3226 TRACE("Returning %p.\n", *declaration);
3227 return D3D_OK;
3230 static struct wined3d_vertex_declaration *device_get_fvf_declaration(struct d3d9_device *device, DWORD fvf)
3232 struct wined3d_vertex_declaration *wined3d_declaration;
3233 struct fvf_declaration *fvf_decls = device->fvf_decls;
3234 struct d3d9_vertex_declaration *d3d9_declaration;
3235 D3DVERTEXELEMENT9 *elements;
3236 int p, low, high; /* deliberately signed */
3237 HRESULT hr;
3239 TRACE("Searching for declaration for fvf %08x... ", fvf);
3241 low = 0;
3242 high = device->fvf_decl_count - 1;
3243 while (low <= high)
3245 p = (low + high) >> 1;
3246 TRACE("%d ", p);
3248 if (fvf_decls[p].fvf == fvf)
3250 TRACE("found %p.\n", fvf_decls[p].decl);
3251 return fvf_decls[p].decl;
3254 if (fvf_decls[p].fvf < fvf)
3255 low = p + 1;
3256 else
3257 high = p - 1;
3259 TRACE("not found. Creating and inserting at position %d.\n", low);
3261 if (FAILED(hr = vdecl_convert_fvf(fvf, &elements)))
3262 return NULL;
3264 hr = d3d9_vertex_declaration_create(device, elements, &d3d9_declaration);
3265 heap_free(elements);
3266 if (FAILED(hr))
3267 return NULL;
3269 if (device->fvf_decl_size == device->fvf_decl_count)
3271 UINT grow = max(device->fvf_decl_size / 2, 8);
3273 if (!(fvf_decls = heap_realloc(fvf_decls, sizeof(*fvf_decls) * (device->fvf_decl_size + grow))))
3275 IDirect3DVertexDeclaration9_Release(&d3d9_declaration->IDirect3DVertexDeclaration9_iface);
3276 return NULL;
3278 device->fvf_decls = fvf_decls;
3279 device->fvf_decl_size += grow;
3282 d3d9_declaration->fvf = fvf;
3283 wined3d_declaration = d3d9_declaration->wined3d_declaration;
3284 wined3d_vertex_declaration_incref(wined3d_declaration);
3285 IDirect3DVertexDeclaration9_Release(&d3d9_declaration->IDirect3DVertexDeclaration9_iface);
3287 memmove(fvf_decls + low + 1, fvf_decls + low, sizeof(*fvf_decls) * (device->fvf_decl_count - low));
3288 fvf_decls[low].decl = wined3d_declaration;
3289 fvf_decls[low].fvf = fvf;
3290 ++device->fvf_decl_count;
3292 TRACE("Returning %p. %u declarations in array.\n", wined3d_declaration, device->fvf_decl_count);
3294 return wined3d_declaration;
3297 static HRESULT WINAPI d3d9_device_SetFVF(IDirect3DDevice9Ex *iface, DWORD fvf)
3299 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3300 struct wined3d_vertex_declaration *decl;
3302 TRACE("iface %p, fvf %#x.\n", iface, fvf);
3304 if (!fvf)
3306 WARN("%#x is not a valid FVF.\n", fvf);
3307 return D3D_OK;
3310 wined3d_mutex_lock();
3311 if (!(decl = device_get_fvf_declaration(device, fvf)))
3313 wined3d_mutex_unlock();
3314 ERR("Failed to create a vertex declaration for fvf %#x.\n", fvf);
3315 return D3DERR_DRIVERINTERNALERROR;
3318 wined3d_device_set_vertex_declaration(device->wined3d_device, decl);
3319 device->has_vertex_declaration = TRUE;
3320 wined3d_mutex_unlock();
3322 return D3D_OK;
3325 static HRESULT WINAPI d3d9_device_GetFVF(IDirect3DDevice9Ex *iface, DWORD *fvf)
3327 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3328 struct wined3d_vertex_declaration *wined3d_declaration;
3329 struct d3d9_vertex_declaration *d3d9_declaration;
3331 TRACE("iface %p, fvf %p.\n", iface, fvf);
3333 wined3d_mutex_lock();
3334 if ((wined3d_declaration = wined3d_device_get_vertex_declaration(device->wined3d_device)))
3336 d3d9_declaration = wined3d_vertex_declaration_get_parent(wined3d_declaration);
3337 *fvf = d3d9_declaration->fvf;
3339 else
3341 *fvf = 0;
3343 wined3d_mutex_unlock();
3345 TRACE("Returning FVF %#x.\n", *fvf);
3347 return D3D_OK;
3350 static HRESULT WINAPI d3d9_device_CreateVertexShader(IDirect3DDevice9Ex *iface,
3351 const DWORD *byte_code, IDirect3DVertexShader9 **shader)
3353 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3354 struct d3d9_vertexshader *object;
3355 HRESULT hr;
3357 TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
3359 if (!(object = heap_alloc_zero(sizeof(*object))))
3360 return E_OUTOFMEMORY;
3362 hr = vertexshader_init(object, device, byte_code);
3363 if (FAILED(hr))
3365 WARN("Failed to initialize vertex shader, hr %#x.\n", hr);
3366 heap_free(object);
3367 return hr;
3370 TRACE("Created vertex shader %p.\n", object);
3371 *shader = &object->IDirect3DVertexShader9_iface;
3373 return D3D_OK;
3376 static HRESULT WINAPI d3d9_device_SetVertexShader(IDirect3DDevice9Ex *iface, IDirect3DVertexShader9 *shader)
3378 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3379 struct d3d9_vertexshader *shader_obj = unsafe_impl_from_IDirect3DVertexShader9(shader);
3381 TRACE("iface %p, shader %p.\n", iface, shader);
3383 wined3d_mutex_lock();
3384 wined3d_device_set_vertex_shader(device->wined3d_device,
3385 shader_obj ? shader_obj->wined3d_shader : NULL);
3386 wined3d_mutex_unlock();
3388 return D3D_OK;
3391 static HRESULT WINAPI d3d9_device_GetVertexShader(IDirect3DDevice9Ex *iface, IDirect3DVertexShader9 **shader)
3393 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3394 struct d3d9_vertexshader *shader_impl;
3395 struct wined3d_shader *wined3d_shader;
3397 TRACE("iface %p, shader %p.\n", iface, shader);
3399 wined3d_mutex_lock();
3400 if ((wined3d_shader = wined3d_device_get_vertex_shader(device->wined3d_device)))
3402 shader_impl = wined3d_shader_get_parent(wined3d_shader);
3403 *shader = &shader_impl->IDirect3DVertexShader9_iface;
3404 IDirect3DVertexShader9_AddRef(*shader);
3406 else
3408 *shader = NULL;
3410 wined3d_mutex_unlock();
3412 TRACE("Returning %p.\n", *shader);
3414 return D3D_OK;
3417 static HRESULT WINAPI d3d9_device_SetVertexShaderConstantF(IDirect3DDevice9Ex *iface,
3418 UINT reg_idx, const float *data, UINT count)
3420 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3421 HRESULT hr;
3423 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3425 if (reg_idx + count > D3D9_MAX_VERTEX_SHADER_CONSTANTF)
3427 WARN("Trying to access %u constants, but d3d9 only supports %u\n",
3428 reg_idx + count, D3D9_MAX_VERTEX_SHADER_CONSTANTF);
3429 return D3DERR_INVALIDCALL;
3432 wined3d_mutex_lock();
3433 hr = wined3d_device_set_vs_consts_f(device->wined3d_device,
3434 reg_idx, count, (const struct wined3d_vec4 *)data);
3435 wined3d_mutex_unlock();
3437 return hr;
3440 static HRESULT WINAPI d3d9_device_GetVertexShaderConstantF(IDirect3DDevice9Ex *iface,
3441 UINT reg_idx, float *data, UINT count)
3443 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3444 HRESULT hr;
3446 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3448 if (reg_idx + count > D3D9_MAX_VERTEX_SHADER_CONSTANTF)
3450 WARN("Trying to access %u constants, but d3d9 only supports %u\n",
3451 reg_idx + count, D3D9_MAX_VERTEX_SHADER_CONSTANTF);
3452 return D3DERR_INVALIDCALL;
3455 wined3d_mutex_lock();
3456 hr = wined3d_device_get_vs_consts_f(device->wined3d_device,
3457 reg_idx, count, (struct wined3d_vec4 *)data);
3458 wined3d_mutex_unlock();
3460 return hr;
3463 static HRESULT WINAPI d3d9_device_SetVertexShaderConstantI(IDirect3DDevice9Ex *iface,
3464 UINT reg_idx, const int *data, UINT count)
3466 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3467 HRESULT hr;
3469 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3471 wined3d_mutex_lock();
3472 hr = wined3d_device_set_vs_consts_i(device->wined3d_device,
3473 reg_idx, count, (const struct wined3d_ivec4 *)data);
3474 wined3d_mutex_unlock();
3476 return hr;
3479 static HRESULT WINAPI d3d9_device_GetVertexShaderConstantI(IDirect3DDevice9Ex *iface,
3480 UINT reg_idx, int *data, UINT count)
3482 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3483 HRESULT hr;
3485 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3487 wined3d_mutex_lock();
3488 hr = wined3d_device_get_vs_consts_i(device->wined3d_device,
3489 reg_idx, count, (struct wined3d_ivec4 *)data);
3490 wined3d_mutex_unlock();
3492 return hr;
3495 static HRESULT WINAPI d3d9_device_SetVertexShaderConstantB(IDirect3DDevice9Ex *iface,
3496 UINT reg_idx, const BOOL *data, UINT count)
3498 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3499 HRESULT hr;
3501 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3503 wined3d_mutex_lock();
3504 hr = wined3d_device_set_vs_consts_b(device->wined3d_device, reg_idx, count, data);
3505 wined3d_mutex_unlock();
3507 return hr;
3510 static HRESULT WINAPI d3d9_device_GetVertexShaderConstantB(IDirect3DDevice9Ex *iface,
3511 UINT reg_idx, BOOL *data, UINT count)
3513 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3514 HRESULT hr;
3516 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3518 wined3d_mutex_lock();
3519 hr = wined3d_device_get_vs_consts_b(device->wined3d_device, reg_idx, count, data);
3520 wined3d_mutex_unlock();
3522 return hr;
3525 static HRESULT WINAPI d3d9_device_SetStreamSource(IDirect3DDevice9Ex *iface,
3526 UINT stream_idx, IDirect3DVertexBuffer9 *buffer, UINT offset, UINT stride)
3528 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3529 struct d3d9_vertexbuffer *buffer_impl = unsafe_impl_from_IDirect3DVertexBuffer9(buffer);
3530 struct wined3d_buffer *wined3d_buffer;
3531 HRESULT hr;
3533 TRACE("iface %p, stream_idx %u, buffer %p, offset %u, stride %u.\n",
3534 iface, stream_idx, buffer, offset, stride);
3536 wined3d_mutex_lock();
3537 if (!stride)
3539 unsigned int cur_offset;
3541 hr = wined3d_device_get_stream_source(device->wined3d_device, stream_idx, &wined3d_buffer,
3542 &cur_offset, &stride);
3545 if (!buffer_impl)
3546 wined3d_buffer = NULL;
3547 else if (buffer_impl->draw_buffer)
3548 wined3d_buffer = buffer_impl->draw_buffer;
3549 else
3550 wined3d_buffer = buffer_impl->wined3d_buffer;
3552 hr = wined3d_device_set_stream_source(device->wined3d_device, stream_idx, wined3d_buffer, offset, stride);
3553 if (SUCCEEDED(hr) && !device->recording)
3555 if (buffer_impl && buffer_impl->draw_buffer)
3556 device->sysmem_vb |= (1u << stream_idx);
3557 else
3558 device->sysmem_vb &= ~(1u << stream_idx);
3561 wined3d_mutex_unlock();
3563 return hr;
3566 static HRESULT WINAPI d3d9_device_GetStreamSource(IDirect3DDevice9Ex *iface,
3567 UINT stream_idx, IDirect3DVertexBuffer9 **buffer, UINT *offset, UINT *stride)
3569 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3570 struct d3d9_vertexbuffer *buffer_impl;
3571 struct wined3d_buffer *wined3d_buffer;
3572 HRESULT hr;
3574 TRACE("iface %p, stream_idx %u, buffer %p, offset %p, stride %p.\n",
3575 iface, stream_idx, buffer, offset, stride);
3577 if (!buffer)
3578 return D3DERR_INVALIDCALL;
3580 wined3d_mutex_lock();
3581 hr = wined3d_device_get_stream_source(device->wined3d_device, stream_idx, &wined3d_buffer, offset, stride);
3582 if (SUCCEEDED(hr) && wined3d_buffer)
3584 buffer_impl = wined3d_buffer_get_parent(wined3d_buffer);
3585 *buffer = &buffer_impl->IDirect3DVertexBuffer9_iface;
3586 IDirect3DVertexBuffer9_AddRef(*buffer);
3588 else
3590 if (FAILED(hr))
3591 FIXME("Call to GetStreamSource failed %p %p\n", offset, stride);
3592 *buffer = NULL;
3594 wined3d_mutex_unlock();
3596 return hr;
3599 static HRESULT WINAPI d3d9_device_SetStreamSourceFreq(IDirect3DDevice9Ex *iface, UINT stream_idx, UINT freq)
3601 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3602 HRESULT hr;
3604 TRACE("iface %p, stream_idx %u, freq %u.\n", iface, stream_idx, freq);
3606 wined3d_mutex_lock();
3607 hr = wined3d_device_set_stream_source_freq(device->wined3d_device, stream_idx, freq);
3608 wined3d_mutex_unlock();
3610 return hr;
3613 static HRESULT WINAPI d3d9_device_GetStreamSourceFreq(IDirect3DDevice9Ex *iface, UINT stream_idx, UINT *freq)
3615 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3616 HRESULT hr;
3618 TRACE("iface %p, stream_idx %u, freq %p.\n", iface, stream_idx, freq);
3620 wined3d_mutex_lock();
3621 hr = wined3d_device_get_stream_source_freq(device->wined3d_device, stream_idx, freq);
3622 wined3d_mutex_unlock();
3624 return hr;
3627 static HRESULT WINAPI d3d9_device_SetIndices(IDirect3DDevice9Ex *iface, IDirect3DIndexBuffer9 *buffer)
3629 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3630 struct d3d9_indexbuffer *ib = unsafe_impl_from_IDirect3DIndexBuffer9(buffer);
3631 struct wined3d_buffer *wined3d_buffer;
3633 TRACE("iface %p, buffer %p.\n", iface, buffer);
3635 if (!ib)
3636 wined3d_buffer = NULL;
3637 else if (ib->draw_buffer)
3638 wined3d_buffer = ib->draw_buffer;
3639 else
3640 wined3d_buffer = ib->wined3d_buffer;
3642 wined3d_mutex_lock();
3643 wined3d_device_set_index_buffer(device->wined3d_device, wined3d_buffer, ib ? ib->format : WINED3DFMT_UNKNOWN, 0);
3644 if (!device->recording)
3645 device->sysmem_ib = ib && ib->draw_buffer;
3646 wined3d_mutex_unlock();
3648 return D3D_OK;
3651 static HRESULT WINAPI d3d9_device_GetIndices(IDirect3DDevice9Ex *iface, IDirect3DIndexBuffer9 **buffer)
3653 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3654 enum wined3d_format_id wined3d_format;
3655 struct wined3d_buffer *wined3d_buffer;
3656 struct d3d9_indexbuffer *buffer_impl;
3658 TRACE("iface %p, buffer %p.\n", iface, buffer);
3660 if (!buffer)
3661 return D3DERR_INVALIDCALL;
3663 wined3d_mutex_lock();
3664 if ((wined3d_buffer = wined3d_device_get_index_buffer(device->wined3d_device, &wined3d_format, NULL)))
3666 buffer_impl = wined3d_buffer_get_parent(wined3d_buffer);
3667 *buffer = &buffer_impl->IDirect3DIndexBuffer9_iface;
3668 IDirect3DIndexBuffer9_AddRef(*buffer);
3670 else
3672 *buffer = NULL;
3674 wined3d_mutex_unlock();
3676 return D3D_OK;
3679 static HRESULT WINAPI d3d9_device_CreatePixelShader(IDirect3DDevice9Ex *iface,
3680 const DWORD *byte_code, IDirect3DPixelShader9 **shader)
3682 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3683 struct d3d9_pixelshader *object;
3684 HRESULT hr;
3686 TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
3688 if (!(object = heap_alloc_zero(sizeof(*object))))
3690 FIXME("Failed to allocate pixel shader memory.\n");
3691 return E_OUTOFMEMORY;
3694 hr = pixelshader_init(object, device, byte_code);
3695 if (FAILED(hr))
3697 WARN("Failed to initialize pixel shader, hr %#x.\n", hr);
3698 heap_free(object);
3699 return hr;
3702 TRACE("Created pixel shader %p.\n", object);
3703 *shader = &object->IDirect3DPixelShader9_iface;
3705 return D3D_OK;
3708 static HRESULT WINAPI d3d9_device_SetPixelShader(IDirect3DDevice9Ex *iface, IDirect3DPixelShader9 *shader)
3710 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3711 struct d3d9_pixelshader *shader_obj = unsafe_impl_from_IDirect3DPixelShader9(shader);
3713 TRACE("iface %p, shader %p.\n", iface, shader);
3715 wined3d_mutex_lock();
3716 wined3d_device_set_pixel_shader(device->wined3d_device,
3717 shader_obj ? shader_obj->wined3d_shader : NULL);
3718 wined3d_mutex_unlock();
3720 return D3D_OK;
3723 static HRESULT WINAPI d3d9_device_GetPixelShader(IDirect3DDevice9Ex *iface, IDirect3DPixelShader9 **shader)
3725 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3726 struct d3d9_pixelshader *shader_impl;
3727 struct wined3d_shader *wined3d_shader;
3729 TRACE("iface %p, shader %p.\n", iface, shader);
3731 if (!shader) return D3DERR_INVALIDCALL;
3733 wined3d_mutex_lock();
3734 if ((wined3d_shader = wined3d_device_get_pixel_shader(device->wined3d_device)))
3736 shader_impl = wined3d_shader_get_parent(wined3d_shader);
3737 *shader = &shader_impl->IDirect3DPixelShader9_iface;
3738 IDirect3DPixelShader9_AddRef(*shader);
3740 else
3742 *shader = NULL;
3744 wined3d_mutex_unlock();
3746 TRACE("Returning %p.\n", *shader);
3748 return D3D_OK;
3751 static HRESULT WINAPI d3d9_device_SetPixelShaderConstantF(IDirect3DDevice9Ex *iface,
3752 UINT reg_idx, const float *data, UINT count)
3754 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3755 HRESULT hr;
3757 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3759 wined3d_mutex_lock();
3760 hr = wined3d_device_set_ps_consts_f(device->wined3d_device,
3761 reg_idx, count, (const struct wined3d_vec4 *)data);
3762 wined3d_mutex_unlock();
3764 return hr;
3767 static HRESULT WINAPI d3d9_device_GetPixelShaderConstantF(IDirect3DDevice9Ex *iface,
3768 UINT reg_idx, float *data, UINT count)
3770 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3771 HRESULT hr;
3773 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3775 wined3d_mutex_lock();
3776 hr = wined3d_device_get_ps_consts_f(device->wined3d_device,
3777 reg_idx, count, (struct wined3d_vec4 *)data);
3778 wined3d_mutex_unlock();
3780 return hr;
3783 static HRESULT WINAPI d3d9_device_SetPixelShaderConstantI(IDirect3DDevice9Ex *iface,
3784 UINT reg_idx, const int *data, UINT count)
3786 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3787 HRESULT hr;
3789 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3791 wined3d_mutex_lock();
3792 hr = wined3d_device_set_ps_consts_i(device->wined3d_device,
3793 reg_idx, count, (const struct wined3d_ivec4 *)data);
3794 wined3d_mutex_unlock();
3796 return hr;
3799 static HRESULT WINAPI d3d9_device_GetPixelShaderConstantI(IDirect3DDevice9Ex *iface,
3800 UINT reg_idx, int *data, UINT count)
3802 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3803 HRESULT hr;
3805 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3807 wined3d_mutex_lock();
3808 hr = wined3d_device_get_ps_consts_i(device->wined3d_device,
3809 reg_idx, count, (struct wined3d_ivec4 *)data);
3810 wined3d_mutex_unlock();
3812 return hr;
3815 static HRESULT WINAPI d3d9_device_SetPixelShaderConstantB(IDirect3DDevice9Ex *iface,
3816 UINT reg_idx, const BOOL *data, UINT count)
3818 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3819 HRESULT hr;
3821 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3823 wined3d_mutex_lock();
3824 hr = wined3d_device_set_ps_consts_b(device->wined3d_device, reg_idx, count, data);
3825 wined3d_mutex_unlock();
3827 return hr;
3830 static HRESULT WINAPI d3d9_device_GetPixelShaderConstantB(IDirect3DDevice9Ex *iface,
3831 UINT reg_idx, BOOL *data, UINT count)
3833 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3834 HRESULT hr;
3836 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3838 wined3d_mutex_lock();
3839 hr = wined3d_device_get_ps_consts_b(device->wined3d_device, reg_idx, count, data);
3840 wined3d_mutex_unlock();
3842 return hr;
3845 static HRESULT WINAPI d3d9_device_DrawRectPatch(IDirect3DDevice9Ex *iface, UINT handle,
3846 const float *segment_count, const D3DRECTPATCH_INFO *patch_info)
3848 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
3849 iface, handle, segment_count, patch_info);
3850 return D3D_OK;
3853 static HRESULT WINAPI d3d9_device_DrawTriPatch(IDirect3DDevice9Ex *iface, UINT handle,
3854 const float *segment_count, const D3DTRIPATCH_INFO *patch_info)
3856 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
3857 iface, handle, segment_count, patch_info);
3858 return D3D_OK;
3861 static HRESULT WINAPI d3d9_device_DeletePatch(IDirect3DDevice9Ex *iface, UINT handle)
3863 FIXME("iface %p, handle %#x unimplemented.\n", iface, handle);
3864 return D3DERR_INVALIDCALL;
3867 static HRESULT WINAPI d3d9_device_CreateQuery(IDirect3DDevice9Ex *iface, D3DQUERYTYPE type, IDirect3DQuery9 **query)
3869 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3870 struct d3d9_query *object;
3871 HRESULT hr;
3873 TRACE("iface %p, type %#x, query %p.\n", iface, type, query);
3875 if (!(object = heap_alloc_zero(sizeof(*object))))
3876 return E_OUTOFMEMORY;
3878 hr = query_init(object, device, type);
3879 if (FAILED(hr))
3881 WARN("Failed to initialize query, hr %#x.\n", hr);
3882 heap_free(object);
3883 return hr;
3886 TRACE("Created query %p.\n", object);
3887 if (query) *query = &object->IDirect3DQuery9_iface;
3888 else IDirect3DQuery9_Release(&object->IDirect3DQuery9_iface);
3890 return D3D_OK;
3893 static HRESULT WINAPI d3d9_device_SetConvolutionMonoKernel(IDirect3DDevice9Ex *iface,
3894 UINT width, UINT height, float *rows, float *columns)
3896 FIXME("iface %p, width %u, height %u, rows %p, columns %p stub!\n",
3897 iface, width, height, rows, columns);
3899 return E_NOTIMPL;
3902 static HRESULT WINAPI d3d9_device_ComposeRects(IDirect3DDevice9Ex *iface,
3903 IDirect3DSurface9 *src_surface, IDirect3DSurface9 *dst_surface, IDirect3DVertexBuffer9 *src_descs,
3904 UINT rect_count, IDirect3DVertexBuffer9 *dst_descs, D3DCOMPOSERECTSOP operation, INT offset_x, INT offset_y)
3906 FIXME("iface %p, src_surface %p, dst_surface %p, src_descs %p, rect_count %u, "
3907 "dst_descs %p, operation %#x, offset_x %u, offset_y %u stub!\n",
3908 iface, src_surface, dst_surface, src_descs, rect_count,
3909 dst_descs, operation, offset_x, offset_y);
3911 return E_NOTIMPL;
3914 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_PresentEx(IDirect3DDevice9Ex *iface,
3915 const RECT *src_rect, const RECT *dst_rect, HWND dst_window_override,
3916 const RGNDATA *dirty_region, DWORD flags)
3918 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3919 struct d3d9_swapchain *swapchain;
3920 unsigned int i;
3921 HRESULT hr;
3923 TRACE("iface %p, src_rect %s, dst_rect %s, dst_window_override %p, dirty_region %p, flags %#x.\n",
3924 iface, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect),
3925 dst_window_override, dirty_region, flags);
3927 if (device->device_state != D3D9_DEVICE_STATE_OK)
3928 return S_PRESENT_OCCLUDED;
3930 if (dirty_region)
3931 FIXME("Ignoring dirty_region %p.\n", dirty_region);
3933 wined3d_mutex_lock();
3934 for (i = 0; i < device->implicit_swapchain_count; ++i)
3936 swapchain = device->implicit_swapchains[i];
3937 if (FAILED(hr = wined3d_swapchain_present(swapchain->wined3d_swapchain,
3938 src_rect, dst_rect, dst_window_override, swapchain->swap_interval, flags)))
3940 wined3d_mutex_unlock();
3941 return hr;
3944 wined3d_mutex_unlock();
3946 return D3D_OK;
3949 static HRESULT WINAPI d3d9_device_GetGPUThreadPriority(IDirect3DDevice9Ex *iface, INT *priority)
3951 FIXME("iface %p, priority %p stub!\n", iface, priority);
3953 return E_NOTIMPL;
3956 static HRESULT WINAPI d3d9_device_SetGPUThreadPriority(IDirect3DDevice9Ex *iface, INT priority)
3958 FIXME("iface %p, priority %d stub!\n", iface, priority);
3960 return E_NOTIMPL;
3963 static HRESULT WINAPI d3d9_device_WaitForVBlank(IDirect3DDevice9Ex *iface, UINT swapchain_idx)
3965 FIXME("iface %p, swapchain_idx %u stub!\n", iface, swapchain_idx);
3967 return E_NOTIMPL;
3970 static HRESULT WINAPI d3d9_device_CheckResourceResidency(IDirect3DDevice9Ex *iface,
3971 IDirect3DResource9 **resources, UINT32 resource_count)
3973 FIXME("iface %p, resources %p, resource_count %u stub!\n",
3974 iface, resources, resource_count);
3976 return E_NOTIMPL;
3979 static HRESULT WINAPI d3d9_device_SetMaximumFrameLatency(IDirect3DDevice9Ex *iface, UINT max_latency)
3981 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3983 TRACE("iface %p, max_latency %u.\n", iface, max_latency);
3985 if (max_latency > 30)
3986 return D3DERR_INVALIDCALL;
3988 wined3d_mutex_lock();
3989 wined3d_device_set_max_frame_latency(device->wined3d_device, max_latency);
3990 wined3d_mutex_unlock();
3992 return S_OK;
3995 static HRESULT WINAPI d3d9_device_GetMaximumFrameLatency(IDirect3DDevice9Ex *iface, UINT *max_latency)
3997 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3999 TRACE("iface %p, max_latency %p.\n", iface, max_latency);
4001 wined3d_mutex_lock();
4002 *max_latency = wined3d_device_get_max_frame_latency(device->wined3d_device);
4003 wined3d_mutex_unlock();
4005 return S_OK;
4008 static HRESULT WINAPI d3d9_device_CheckDeviceState(IDirect3DDevice9Ex *iface, HWND dst_window)
4010 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
4011 struct wined3d_swapchain_desc swapchain_desc;
4013 TRACE("iface %p, dst_window %p.\n", iface, dst_window);
4015 wined3d_mutex_lock();
4016 wined3d_swapchain_get_desc(device->implicit_swapchains[0]->wined3d_swapchain, &swapchain_desc);
4017 wined3d_mutex_unlock();
4019 if (swapchain_desc.windowed)
4020 return D3D_OK;
4022 /* FIXME: This is actually supposed to check if any other device is in
4023 * fullscreen mode. */
4024 if (dst_window != swapchain_desc.device_window)
4025 return device->device_state == D3D9_DEVICE_STATE_OK ? S_PRESENT_OCCLUDED : D3D_OK;
4027 return device->device_state == D3D9_DEVICE_STATE_OK ? D3D_OK : S_PRESENT_OCCLUDED;
4030 static HRESULT WINAPI d3d9_device_CreateRenderTargetEx(IDirect3DDevice9Ex *iface,
4031 UINT width, UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
4032 BOOL lockable, IDirect3DSurface9 **surface, HANDLE *shared_handle, DWORD usage)
4034 FIXME("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u, "
4035 "lockable %#x, surface %p, shared_handle %p, usage %#x stub!\n",
4036 iface, width, height, format, multisample_type, multisample_quality,
4037 lockable, surface, shared_handle, usage);
4039 *surface = NULL;
4040 if (shared_handle)
4041 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
4043 return E_NOTIMPL;
4046 static HRESULT WINAPI d3d9_device_CreateOffscreenPlainSurfaceEx(IDirect3DDevice9Ex *iface,
4047 UINT width, UINT height, D3DFORMAT format, D3DPOOL pool, IDirect3DSurface9 **surface,
4048 HANDLE *shared_handle, DWORD usage)
4050 FIXME("iface %p, width %u, height %u, format %#x, pool %#x, surface %p, shared_handle %p, usage %#x stub!\n",
4051 iface, width, height, format, pool, surface, shared_handle, usage);
4053 return E_NOTIMPL;
4056 static HRESULT WINAPI d3d9_device_CreateDepthStencilSurfaceEx(IDirect3DDevice9Ex *iface,
4057 UINT width, UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
4058 BOOL discard, IDirect3DSurface9 **surface, HANDLE *shared_handle, DWORD usage)
4060 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
4061 DWORD flags = 0;
4063 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u, "
4064 "discard %#x, surface %p, shared_handle %p, usage %#x.\n",
4065 iface, width, height, format, multisample_type, multisample_quality,
4066 discard, surface, shared_handle, usage);
4068 if (usage & (D3DUSAGE_DEPTHSTENCIL | D3DUSAGE_RENDERTARGET))
4070 WARN("Invalid usage %#x.\n", usage);
4071 return D3DERR_INVALIDCALL;
4074 if (shared_handle)
4075 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
4077 if (discard)
4078 flags |= WINED3D_TEXTURE_CREATE_DISCARD;
4080 *surface = NULL;
4081 return d3d9_device_create_surface(device, flags, wined3dformat_from_d3dformat(format),
4082 multisample_type, multisample_quality, usage & WINED3DUSAGE_MASK, WINED3D_BIND_DEPTH_STENCIL,
4083 WINED3D_RESOURCE_ACCESS_GPU, width, height, NULL, surface);
4086 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_ResetEx(IDirect3DDevice9Ex *iface,
4087 D3DPRESENT_PARAMETERS *present_parameters, D3DDISPLAYMODEEX *mode)
4089 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
4091 TRACE("iface %p, present_parameters %p, mode %p.\n", iface, present_parameters, mode);
4093 if (!present_parameters->Windowed == !mode)
4095 WARN("Mode can be passed if and only if Windowed is FALSE.\n");
4096 return D3DERR_INVALIDCALL;
4099 if (mode && (mode->Width != present_parameters->BackBufferWidth
4100 || mode->Height != present_parameters->BackBufferHeight))
4102 WARN("Mode and back buffer mismatch (mode %ux%u, backbuffer %ux%u).\n",
4103 mode->Width, mode->Height,
4104 present_parameters->BackBufferWidth, present_parameters->BackBufferHeight);
4105 return D3DERR_INVALIDCALL;
4108 return d3d9_device_reset(device, present_parameters, mode);
4111 static HRESULT WINAPI d3d9_device_GetDisplayModeEx(IDirect3DDevice9Ex *iface,
4112 UINT swapchain_idx, D3DDISPLAYMODEEX *mode, D3DDISPLAYROTATION *rotation)
4114 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
4115 struct wined3d_display_mode wined3d_mode;
4116 HRESULT hr;
4118 TRACE("iface %p, swapchain_idx %u, mode %p, rotation %p.\n",
4119 iface, swapchain_idx, mode, rotation);
4121 if (mode->Size != sizeof(*mode))
4122 return D3DERR_INVALIDCALL;
4124 wined3d_mutex_lock();
4125 hr = wined3d_device_get_display_mode(device->wined3d_device, swapchain_idx, &wined3d_mode,
4126 (enum wined3d_display_rotation *)rotation);
4127 wined3d_mutex_unlock();
4129 if (SUCCEEDED(hr))
4131 mode->Width = wined3d_mode.width;
4132 mode->Height = wined3d_mode.height;
4133 mode->RefreshRate = wined3d_mode.refresh_rate;
4134 mode->Format = d3dformat_from_wined3dformat(wined3d_mode.format_id);
4135 mode->ScanLineOrdering = wined3d_mode.scanline_ordering;
4138 return hr;
4141 static const struct IDirect3DDevice9ExVtbl d3d9_device_vtbl =
4143 /* IUnknown */
4144 d3d9_device_QueryInterface,
4145 d3d9_device_AddRef,
4146 d3d9_device_Release,
4147 /* IDirect3DDevice9 */
4148 d3d9_device_TestCooperativeLevel,
4149 d3d9_device_GetAvailableTextureMem,
4150 d3d9_device_EvictManagedResources,
4151 d3d9_device_GetDirect3D,
4152 d3d9_device_GetDeviceCaps,
4153 d3d9_device_GetDisplayMode,
4154 d3d9_device_GetCreationParameters,
4155 d3d9_device_SetCursorProperties,
4156 d3d9_device_SetCursorPosition,
4157 d3d9_device_ShowCursor,
4158 d3d9_device_CreateAdditionalSwapChain,
4159 d3d9_device_GetSwapChain,
4160 d3d9_device_GetNumberOfSwapChains,
4161 d3d9_device_Reset,
4162 d3d9_device_Present,
4163 d3d9_device_GetBackBuffer,
4164 d3d9_device_GetRasterStatus,
4165 d3d9_device_SetDialogBoxMode,
4166 d3d9_device_SetGammaRamp,
4167 d3d9_device_GetGammaRamp,
4168 d3d9_device_CreateTexture,
4169 d3d9_device_CreateVolumeTexture,
4170 d3d9_device_CreateCubeTexture,
4171 d3d9_device_CreateVertexBuffer,
4172 d3d9_device_CreateIndexBuffer,
4173 d3d9_device_CreateRenderTarget,
4174 d3d9_device_CreateDepthStencilSurface,
4175 d3d9_device_UpdateSurface,
4176 d3d9_device_UpdateTexture,
4177 d3d9_device_GetRenderTargetData,
4178 d3d9_device_GetFrontBufferData,
4179 d3d9_device_StretchRect,
4180 d3d9_device_ColorFill,
4181 d3d9_device_CreateOffscreenPlainSurface,
4182 d3d9_device_SetRenderTarget,
4183 d3d9_device_GetRenderTarget,
4184 d3d9_device_SetDepthStencilSurface,
4185 d3d9_device_GetDepthStencilSurface,
4186 d3d9_device_BeginScene,
4187 d3d9_device_EndScene,
4188 d3d9_device_Clear,
4189 d3d9_device_SetTransform,
4190 d3d9_device_GetTransform,
4191 d3d9_device_MultiplyTransform,
4192 d3d9_device_SetViewport,
4193 d3d9_device_GetViewport,
4194 d3d9_device_SetMaterial,
4195 d3d9_device_GetMaterial,
4196 d3d9_device_SetLight,
4197 d3d9_device_GetLight,
4198 d3d9_device_LightEnable,
4199 d3d9_device_GetLightEnable,
4200 d3d9_device_SetClipPlane,
4201 d3d9_device_GetClipPlane,
4202 d3d9_device_SetRenderState,
4203 d3d9_device_GetRenderState,
4204 d3d9_device_CreateStateBlock,
4205 d3d9_device_BeginStateBlock,
4206 d3d9_device_EndStateBlock,
4207 d3d9_device_SetClipStatus,
4208 d3d9_device_GetClipStatus,
4209 d3d9_device_GetTexture,
4210 d3d9_device_SetTexture,
4211 d3d9_device_GetTextureStageState,
4212 d3d9_device_SetTextureStageState,
4213 d3d9_device_GetSamplerState,
4214 d3d9_device_SetSamplerState,
4215 d3d9_device_ValidateDevice,
4216 d3d9_device_SetPaletteEntries,
4217 d3d9_device_GetPaletteEntries,
4218 d3d9_device_SetCurrentTexturePalette,
4219 d3d9_device_GetCurrentTexturePalette,
4220 d3d9_device_SetScissorRect,
4221 d3d9_device_GetScissorRect,
4222 d3d9_device_SetSoftwareVertexProcessing,
4223 d3d9_device_GetSoftwareVertexProcessing,
4224 d3d9_device_SetNPatchMode,
4225 d3d9_device_GetNPatchMode,
4226 d3d9_device_DrawPrimitive,
4227 d3d9_device_DrawIndexedPrimitive,
4228 d3d9_device_DrawPrimitiveUP,
4229 d3d9_device_DrawIndexedPrimitiveUP,
4230 d3d9_device_ProcessVertices,
4231 d3d9_device_CreateVertexDeclaration,
4232 d3d9_device_SetVertexDeclaration,
4233 d3d9_device_GetVertexDeclaration,
4234 d3d9_device_SetFVF,
4235 d3d9_device_GetFVF,
4236 d3d9_device_CreateVertexShader,
4237 d3d9_device_SetVertexShader,
4238 d3d9_device_GetVertexShader,
4239 d3d9_device_SetVertexShaderConstantF,
4240 d3d9_device_GetVertexShaderConstantF,
4241 d3d9_device_SetVertexShaderConstantI,
4242 d3d9_device_GetVertexShaderConstantI,
4243 d3d9_device_SetVertexShaderConstantB,
4244 d3d9_device_GetVertexShaderConstantB,
4245 d3d9_device_SetStreamSource,
4246 d3d9_device_GetStreamSource,
4247 d3d9_device_SetStreamSourceFreq,
4248 d3d9_device_GetStreamSourceFreq,
4249 d3d9_device_SetIndices,
4250 d3d9_device_GetIndices,
4251 d3d9_device_CreatePixelShader,
4252 d3d9_device_SetPixelShader,
4253 d3d9_device_GetPixelShader,
4254 d3d9_device_SetPixelShaderConstantF,
4255 d3d9_device_GetPixelShaderConstantF,
4256 d3d9_device_SetPixelShaderConstantI,
4257 d3d9_device_GetPixelShaderConstantI,
4258 d3d9_device_SetPixelShaderConstantB,
4259 d3d9_device_GetPixelShaderConstantB,
4260 d3d9_device_DrawRectPatch,
4261 d3d9_device_DrawTriPatch,
4262 d3d9_device_DeletePatch,
4263 d3d9_device_CreateQuery,
4264 /* IDirect3DDevice9Ex */
4265 d3d9_device_SetConvolutionMonoKernel,
4266 d3d9_device_ComposeRects,
4267 d3d9_device_PresentEx,
4268 d3d9_device_GetGPUThreadPriority,
4269 d3d9_device_SetGPUThreadPriority,
4270 d3d9_device_WaitForVBlank,
4271 d3d9_device_CheckResourceResidency,
4272 d3d9_device_SetMaximumFrameLatency,
4273 d3d9_device_GetMaximumFrameLatency,
4274 d3d9_device_CheckDeviceState,
4275 d3d9_device_CreateRenderTargetEx,
4276 d3d9_device_CreateOffscreenPlainSurfaceEx,
4277 d3d9_device_CreateDepthStencilSurfaceEx,
4278 d3d9_device_ResetEx,
4279 d3d9_device_GetDisplayModeEx,
4282 static inline struct d3d9_device *device_from_device_parent(struct wined3d_device_parent *device_parent)
4284 return CONTAINING_RECORD(device_parent, struct d3d9_device, device_parent);
4287 static void CDECL device_parent_wined3d_device_created(struct wined3d_device_parent *device_parent,
4288 struct wined3d_device *device)
4290 TRACE("device_parent %p, device %p.\n", device_parent, device);
4293 static void CDECL device_parent_mode_changed(struct wined3d_device_parent *device_parent)
4295 TRACE("device_parent %p.\n", device_parent);
4298 static void CDECL device_parent_activate(struct wined3d_device_parent *device_parent, BOOL activate)
4300 struct d3d9_device *device = device_from_device_parent(device_parent);
4302 TRACE("device_parent %p, activate %#x.\n", device_parent, activate);
4304 if (!device->d3d_parent)
4305 return;
4307 if (!activate)
4308 InterlockedCompareExchange(&device->device_state, D3D9_DEVICE_STATE_LOST, D3D9_DEVICE_STATE_OK);
4309 else if (device->d3d_parent->extended)
4310 InterlockedCompareExchange(&device->device_state, D3D9_DEVICE_STATE_OK, D3D9_DEVICE_STATE_LOST);
4311 else
4312 InterlockedCompareExchange(&device->device_state, D3D9_DEVICE_STATE_NOT_RESET, D3D9_DEVICE_STATE_LOST);
4315 static HRESULT CDECL device_parent_texture_sub_resource_created(struct wined3d_device_parent *device_parent,
4316 enum wined3d_resource_type type, struct wined3d_texture *wined3d_texture, unsigned int sub_resource_idx,
4317 void **parent, const struct wined3d_parent_ops **parent_ops)
4319 TRACE("device_parent %p, type %#x, wined3d_texture %p, sub_resource_idx %u, parent %p, parent_ops %p.\n",
4320 device_parent, type, wined3d_texture, sub_resource_idx, parent, parent_ops);
4322 if (type == WINED3D_RTYPE_TEXTURE_2D)
4324 struct d3d9_surface *d3d_surface;
4326 if (!(d3d_surface = heap_alloc_zero(sizeof(*d3d_surface))))
4327 return E_OUTOFMEMORY;
4329 surface_init(d3d_surface, wined3d_texture, sub_resource_idx, parent_ops);
4330 *parent = d3d_surface;
4331 TRACE("Created surface %p.\n", d3d_surface);
4333 else if (type == WINED3D_RTYPE_TEXTURE_3D)
4335 struct d3d9_volume *d3d_volume;
4337 if (!(d3d_volume = heap_alloc_zero(sizeof(*d3d_volume))))
4338 return E_OUTOFMEMORY;
4340 volume_init(d3d_volume, wined3d_texture, sub_resource_idx, parent_ops);
4341 *parent = d3d_volume;
4342 TRACE("Created volume %p.\n", d3d_volume);
4344 else
4346 ERR("Unhandled resource type %#x.\n", type);
4347 return E_FAIL;
4350 return D3D_OK;
4353 static HRESULT CDECL device_parent_create_swapchain_texture(struct wined3d_device_parent *device_parent,
4354 void *container_parent, const struct wined3d_resource_desc *desc, DWORD texture_flags,
4355 struct wined3d_texture **texture)
4357 struct d3d9_device *device = device_from_device_parent(device_parent);
4358 struct d3d9_surface *d3d_surface;
4359 HRESULT hr;
4361 TRACE("device_parent %p, container_parent %p, desc %p, texture flags %#x, texture %p.\n",
4362 device_parent, container_parent, desc, texture_flags, texture);
4364 if (container_parent == device_parent)
4365 container_parent = &device->IDirect3DDevice9Ex_iface;
4367 if (FAILED(hr = wined3d_texture_create(device->wined3d_device, desc, 1, 1,
4368 texture_flags, NULL, container_parent, &d3d9_null_wined3d_parent_ops, texture)))
4370 WARN("Failed to create texture, hr %#x.\n", hr);
4371 return hr;
4374 d3d_surface = wined3d_texture_get_sub_resource_parent(*texture, 0);
4375 d3d_surface->parent_device = &device->IDirect3DDevice9Ex_iface;
4377 return hr;
4380 static HRESULT CDECL device_parent_create_swapchain(struct wined3d_device_parent *device_parent,
4381 struct wined3d_swapchain_desc *desc, struct wined3d_swapchain **swapchain)
4383 struct d3d9_device *device = device_from_device_parent(device_parent);
4384 struct d3d9_swapchain *d3d_swapchain;
4385 HRESULT hr;
4387 TRACE("device_parent %p, desc %p, swapchain %p\n", device_parent, desc, swapchain);
4389 if (FAILED(hr = d3d9_swapchain_create(device, desc, WINED3D_SWAP_INTERVAL_DEFAULT, &d3d_swapchain)))
4391 WARN("Failed to create swapchain, hr %#x.\n", hr);
4392 *swapchain = NULL;
4393 return hr;
4396 *swapchain = d3d_swapchain->wined3d_swapchain;
4397 wined3d_swapchain_incref(*swapchain);
4398 IDirect3DSwapChain9Ex_Release(&d3d_swapchain->IDirect3DSwapChain9Ex_iface);
4400 return hr;
4403 static const struct wined3d_device_parent_ops d3d9_wined3d_device_parent_ops =
4405 device_parent_wined3d_device_created,
4406 device_parent_mode_changed,
4407 device_parent_activate,
4408 device_parent_texture_sub_resource_created,
4409 device_parent_create_swapchain_texture,
4410 device_parent_create_swapchain,
4413 static void setup_fpu(void)
4415 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
4416 WORD cw;
4417 __asm__ volatile ("fnstcw %0" : "=m" (cw));
4418 cw = (cw & ~0xf3f) | 0x3f;
4419 __asm__ volatile ("fldcw %0" : : "m" (cw));
4420 #elif defined(__i386__) && defined(_MSC_VER)
4421 WORD cw;
4422 __asm fnstcw cw;
4423 cw = (cw & ~0xf3f) | 0x3f;
4424 __asm fldcw cw;
4425 #else
4426 FIXME("FPU setup not implemented for this platform.\n");
4427 #endif
4430 HRESULT device_init(struct d3d9_device *device, struct d3d9 *parent, struct wined3d *wined3d,
4431 UINT adapter, D3DDEVTYPE device_type, HWND focus_window, DWORD flags,
4432 D3DPRESENT_PARAMETERS *parameters, D3DDISPLAYMODEEX *mode)
4434 struct wined3d_swapchain_desc *swapchain_desc;
4435 struct wined3d_caps caps;
4436 unsigned i, count = 1;
4437 HRESULT hr;
4439 static const enum wined3d_feature_level feature_levels[] =
4441 WINED3D_FEATURE_LEVEL_9_SM3,
4442 WINED3D_FEATURE_LEVEL_9_SM2,
4443 WINED3D_FEATURE_LEVEL_9_1,
4444 WINED3D_FEATURE_LEVEL_8,
4445 WINED3D_FEATURE_LEVEL_7,
4446 WINED3D_FEATURE_LEVEL_6,
4447 WINED3D_FEATURE_LEVEL_5,
4450 if (mode)
4451 FIXME("Ignoring display mode.\n");
4453 device->IDirect3DDevice9Ex_iface.lpVtbl = &d3d9_device_vtbl;
4454 device->device_parent.ops = &d3d9_wined3d_device_parent_ops;
4455 device->refcount = 1;
4457 if (!(flags & D3DCREATE_FPU_PRESERVE)) setup_fpu();
4459 wined3d_mutex_lock();
4460 if (FAILED(hr = wined3d_device_create(wined3d, adapter, device_type,
4461 focus_window, flags, 4, feature_levels, ARRAY_SIZE(feature_levels),
4462 &device->device_parent, &device->wined3d_device)))
4464 WARN("Failed to create wined3d device, hr %#x.\n", hr);
4465 wined3d_mutex_unlock();
4466 return hr;
4469 wined3d_get_device_caps(wined3d, adapter, device_type, &caps);
4470 device->max_user_clip_planes = caps.MaxUserClipPlanes;
4471 if (flags & D3DCREATE_ADAPTERGROUP_DEVICE)
4472 count = caps.NumberOfAdaptersInGroup;
4474 if (flags & D3DCREATE_MULTITHREADED)
4475 wined3d_device_set_multithreaded(device->wined3d_device);
4477 if (!parameters->Windowed)
4479 if (!focus_window)
4480 focus_window = parameters->hDeviceWindow;
4481 if (FAILED(hr = wined3d_device_acquire_focus_window(device->wined3d_device, focus_window)))
4483 ERR("Failed to acquire focus window, hr %#x.\n", hr);
4484 wined3d_device_decref(device->wined3d_device);
4485 wined3d_mutex_unlock();
4486 return hr;
4489 for (i = 0; i < count; ++i)
4491 HWND device_window = parameters[i].hDeviceWindow;
4493 if (!device_window) device_window = focus_window;
4494 wined3d_device_setup_fullscreen_window(device->wined3d_device, device_window,
4495 parameters[i].BackBufferWidth,
4496 parameters[i].BackBufferHeight);
4500 if (!(swapchain_desc = heap_alloc(sizeof(*swapchain_desc) * count)))
4502 ERR("Failed to allocate wined3d parameters.\n");
4503 wined3d_device_release_focus_window(device->wined3d_device);
4504 wined3d_device_decref(device->wined3d_device);
4505 wined3d_mutex_unlock();
4506 return E_OUTOFMEMORY;
4509 for (i = 0; i < count; ++i)
4511 if (!wined3d_swapchain_desc_from_present_parameters(&swapchain_desc[i], &parameters[i],
4512 parent->extended))
4514 wined3d_device_release_focus_window(device->wined3d_device);
4515 wined3d_device_decref(device->wined3d_device);
4516 heap_free(swapchain_desc);
4517 wined3d_mutex_unlock();
4518 return D3DERR_INVALIDCALL;
4522 if (FAILED(hr = wined3d_device_init_3d(device->wined3d_device, swapchain_desc)))
4524 WARN("Failed to initialize 3D, hr %#x.\n", hr);
4525 wined3d_device_release_focus_window(device->wined3d_device);
4526 heap_free(swapchain_desc);
4527 wined3d_device_decref(device->wined3d_device);
4528 wined3d_mutex_unlock();
4529 return hr;
4532 wined3d_device_set_render_state(device->wined3d_device,
4533 WINED3D_RS_ZENABLE, !!swapchain_desc->enable_auto_depth_stencil);
4535 if (FAILED(hr = d3d9_device_get_swapchains(device)))
4537 wined3d_device_uninit_3d(device->wined3d_device);
4538 wined3d_device_release_focus_window(device->wined3d_device);
4539 wined3d_device_decref(device->wined3d_device);
4540 wined3d_mutex_unlock();
4541 return E_OUTOFMEMORY;
4543 for (i = 0; i < device->implicit_swapchain_count; ++i)
4545 device->implicit_swapchains[i]->swap_interval
4546 = wined3dswapinterval_from_d3d(parameters[i].PresentationInterval);
4549 for (i = 0; i < count; ++i)
4551 present_parameters_from_wined3d_swapchain_desc(&parameters[i],
4552 &swapchain_desc[i], parameters[i].PresentationInterval);
4555 wined3d_mutex_unlock();
4557 heap_free(swapchain_desc);
4559 /* Initialize the converted declaration array. This creates a valid pointer
4560 * and when adding decls HeapReAlloc() can be used without further checking. */
4561 if (!(device->fvf_decls = heap_alloc(0)))
4563 ERR("Failed to allocate FVF vertex declaration map memory.\n");
4564 wined3d_mutex_lock();
4565 heap_free(device->implicit_swapchains);
4566 wined3d_device_uninit_3d(device->wined3d_device);
4567 wined3d_device_release_focus_window(device->wined3d_device);
4568 wined3d_device_decref(device->wined3d_device);
4569 wined3d_mutex_unlock();
4570 return E_OUTOFMEMORY;
4573 /* We could also simply ignore the initial rendertarget since it's known
4574 * not to be a texture (we currently use these only for automatic mipmap
4575 * generation). */
4576 wined3d_mutex_lock();
4577 device->render_targets[0] = wined3d_rendertarget_view_get_sub_resource_parent(
4578 wined3d_device_get_rendertarget_view(device->wined3d_device, 0));
4579 wined3d_mutex_unlock();
4581 IDirect3D9Ex_AddRef(&parent->IDirect3D9Ex_iface);
4582 device->d3d_parent = parent;
4584 return D3D_OK;