ddraw/tests: Add a test for drawing to a flippable surface.
[wine.git] / dlls / d3d9 / device.c
blob1eeefac6cc0e5c8c69ee045feb64c6ac2c9a9a66
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 "d3d9_private.h"
25 WINE_DEFAULT_DEBUG_CHANNEL(d3d9);
27 static void STDMETHODCALLTYPE d3d9_null_wined3d_object_destroyed(void *parent) {}
29 const struct wined3d_parent_ops d3d9_null_wined3d_parent_ops =
31 d3d9_null_wined3d_object_destroyed,
34 static void wined3d_color_from_d3dcolor(struct wined3d_color *wined3d_colour, D3DCOLOR d3d_colour)
36 wined3d_colour->r = ((d3d_colour >> 16) & 0xff) / 255.0f;
37 wined3d_colour->g = ((d3d_colour >> 8) & 0xff) / 255.0f;
38 wined3d_colour->b = (d3d_colour & 0xff) / 255.0f;
39 wined3d_colour->a = ((d3d_colour >> 24) & 0xff) / 255.0f;
42 D3DFORMAT d3dformat_from_wined3dformat(enum wined3d_format_id format)
44 BYTE *c = (BYTE *)&format;
46 /* Don't translate FOURCC formats */
47 if (isprint(c[0]) && isprint(c[1]) && isprint(c[2]) && isprint(c[3]))
48 return (D3DFORMAT)format;
50 switch(format)
52 case WINED3DFMT_UNKNOWN: return D3DFMT_UNKNOWN;
53 case WINED3DFMT_B8G8R8_UNORM: return D3DFMT_R8G8B8;
54 case WINED3DFMT_B8G8R8A8_UNORM: return D3DFMT_A8R8G8B8;
55 case WINED3DFMT_B8G8R8X8_UNORM: return D3DFMT_X8R8G8B8;
56 case WINED3DFMT_B5G6R5_UNORM: return D3DFMT_R5G6B5;
57 case WINED3DFMT_B5G5R5X1_UNORM: return D3DFMT_X1R5G5B5;
58 case WINED3DFMT_B5G5R5A1_UNORM: return D3DFMT_A1R5G5B5;
59 case WINED3DFMT_B4G4R4A4_UNORM: return D3DFMT_A4R4G4B4;
60 case WINED3DFMT_B2G3R3_UNORM: return D3DFMT_R3G3B2;
61 case WINED3DFMT_A8_UNORM: return D3DFMT_A8;
62 case WINED3DFMT_B2G3R3A8_UNORM: return D3DFMT_A8R3G3B2;
63 case WINED3DFMT_B4G4R4X4_UNORM: return D3DFMT_X4R4G4B4;
64 case WINED3DFMT_R10G10B10A2_UNORM: return D3DFMT_A2B10G10R10;
65 case WINED3DFMT_R8G8B8A8_UNORM: return D3DFMT_A8B8G8R8;
66 case WINED3DFMT_R8G8B8X8_UNORM: return D3DFMT_X8B8G8R8;
67 case WINED3DFMT_R16G16_UNORM: return D3DFMT_G16R16;
68 case WINED3DFMT_B10G10R10A2_UNORM: return D3DFMT_A2R10G10B10;
69 case WINED3DFMT_R16G16B16A16_UNORM: return D3DFMT_A16B16G16R16;
70 case WINED3DFMT_P8_UINT_A8_UNORM: return D3DFMT_A8P8;
71 case WINED3DFMT_P8_UINT: return D3DFMT_P8;
72 case WINED3DFMT_L8_UNORM: return D3DFMT_L8;
73 case WINED3DFMT_L8A8_UNORM: return D3DFMT_A8L8;
74 case WINED3DFMT_L4A4_UNORM: return D3DFMT_A4L4;
75 case WINED3DFMT_R8G8_SNORM: return D3DFMT_V8U8;
76 case WINED3DFMT_R5G5_SNORM_L6_UNORM: return D3DFMT_L6V5U5;
77 case WINED3DFMT_R8G8_SNORM_L8X8_UNORM: return D3DFMT_X8L8V8U8;
78 case WINED3DFMT_R8G8B8A8_SNORM: return D3DFMT_Q8W8V8U8;
79 case WINED3DFMT_R16G16_SNORM: return D3DFMT_V16U16;
80 case WINED3DFMT_R10G10B10_SNORM_A2_UNORM: return D3DFMT_A2W10V10U10;
81 case WINED3DFMT_D16_LOCKABLE: return D3DFMT_D16_LOCKABLE;
82 case WINED3DFMT_D32_UNORM: return D3DFMT_D32;
83 case WINED3DFMT_S1_UINT_D15_UNORM: return D3DFMT_D15S1;
84 case WINED3DFMT_D24_UNORM_S8_UINT: return D3DFMT_D24S8;
85 case WINED3DFMT_X8D24_UNORM: return D3DFMT_D24X8;
86 case WINED3DFMT_S4X4_UINT_D24_UNORM: return D3DFMT_D24X4S4;
87 case WINED3DFMT_D16_UNORM: return D3DFMT_D16;
88 case WINED3DFMT_L16_UNORM: return D3DFMT_L16;
89 case WINED3DFMT_D32_FLOAT: return D3DFMT_D32F_LOCKABLE;
90 case WINED3DFMT_S8_UINT_D24_FLOAT: return D3DFMT_D24FS8;
91 case WINED3DFMT_R16_UINT: return D3DFMT_INDEX16;
92 case WINED3DFMT_R32_UINT: return D3DFMT_INDEX32;
93 case WINED3DFMT_R16G16B16A16_SNORM: return D3DFMT_Q16W16V16U16;
94 case WINED3DFMT_R16_FLOAT: return D3DFMT_R16F;
95 case WINED3DFMT_R16G16_FLOAT: return D3DFMT_G16R16F;
96 case WINED3DFMT_R16G16B16A16_FLOAT: return D3DFMT_A16B16G16R16F;
97 case WINED3DFMT_R32_FLOAT: return D3DFMT_R32F;
98 case WINED3DFMT_R32G32_FLOAT: return D3DFMT_G32R32F;
99 case WINED3DFMT_R32G32B32A32_FLOAT: return D3DFMT_A32B32G32R32F;
100 case WINED3DFMT_R8G8_SNORM_Cx: return D3DFMT_CxV8U8;
101 default:
102 FIXME("Unhandled wined3d format %#x.\n", format);
103 return D3DFMT_UNKNOWN;
107 enum wined3d_format_id wined3dformat_from_d3dformat(D3DFORMAT format)
109 BYTE *c = (BYTE *)&format;
111 /* Don't translate FOURCC formats */
112 if (isprint(c[0]) && isprint(c[1]) && isprint(c[2]) && isprint(c[3]))
113 return (enum wined3d_format_id)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 = d3dmultisample_type_from_wined3d(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 %#lx.\n", interval);
298 case D3DPRESENT_INTERVAL_DEFAULT:
299 return WINED3D_SWAP_INTERVAL_DEFAULT;
303 static BOOL wined3d_swapchain_desc_from_d3d9(struct wined3d_swapchain_desc *swapchain_desc,
304 struct wined3d_output *output, const D3DPRESENT_PARAMETERS *present_parameters,
305 BOOL extended)
307 D3DSWAPEFFECT highest_swapeffect = extended ? D3DSWAPEFFECT_FLIPEX : D3DSWAPEFFECT_COPY;
308 UINT highest_bb_count = extended ? 30 : 3;
310 if (!present_parameters->SwapEffect || present_parameters->SwapEffect > highest_swapeffect)
312 WARN("Invalid swap effect %u passed.\n", present_parameters->SwapEffect);
313 return FALSE;
315 if (present_parameters->BackBufferCount > highest_bb_count
316 || (present_parameters->SwapEffect == D3DSWAPEFFECT_COPY
317 && present_parameters->BackBufferCount > 1))
319 WARN("Invalid backbuffer count %u.\n", present_parameters->BackBufferCount);
320 return FALSE;
322 switch (present_parameters->PresentationInterval)
324 case D3DPRESENT_INTERVAL_DEFAULT:
325 case D3DPRESENT_INTERVAL_ONE:
326 case D3DPRESENT_INTERVAL_TWO:
327 case D3DPRESENT_INTERVAL_THREE:
328 case D3DPRESENT_INTERVAL_FOUR:
329 case D3DPRESENT_INTERVAL_IMMEDIATE:
330 break;
331 default:
332 WARN("Invalid presentation interval %#x.\n", present_parameters->PresentationInterval);
333 return FALSE;
336 swapchain_desc->output = output;
337 swapchain_desc->backbuffer_width = present_parameters->BackBufferWidth;
338 swapchain_desc->backbuffer_height = present_parameters->BackBufferHeight;
339 swapchain_desc->backbuffer_format = wined3dformat_from_d3dformat(present_parameters->BackBufferFormat);
340 swapchain_desc->backbuffer_count = max(1, present_parameters->BackBufferCount);
341 swapchain_desc->backbuffer_bind_flags = WINED3D_BIND_RENDER_TARGET;
342 swapchain_desc->multisample_type = wined3d_multisample_type_from_d3d(present_parameters->MultiSampleType);
343 swapchain_desc->multisample_quality = present_parameters->MultiSampleQuality;
344 swapchain_desc->swap_effect = wined3dswapeffect_from_d3dswapeffect(present_parameters->SwapEffect);
345 swapchain_desc->device_window = present_parameters->hDeviceWindow;
346 swapchain_desc->windowed = present_parameters->Windowed;
347 swapchain_desc->enable_auto_depth_stencil = present_parameters->EnableAutoDepthStencil;
348 swapchain_desc->auto_depth_stencil_format
349 = wined3dformat_from_d3dformat(present_parameters->AutoDepthStencilFormat);
350 swapchain_desc->flags
351 = (present_parameters->Flags & D3DPRESENTFLAGS_MASK) | WINED3D_SWAPCHAIN_ALLOW_MODE_SWITCH;
352 if (extended)
353 swapchain_desc->flags |= WINED3D_SWAPCHAIN_RESTORE_WINDOW_STATE;
354 if ((present_parameters->Flags & D3DPRESENTFLAG_LOCKABLE_BACKBUFFER)
355 && (is_gdi_compat_wined3dformat(swapchain_desc->backbuffer_format)
356 /* WINED3DFMT_UNKNOWN creates the swapchain with the current
357 * display format, which is always GDI-compatible. */
358 || swapchain_desc->backbuffer_format == WINED3DFMT_UNKNOWN))
359 swapchain_desc->flags |= WINED3D_SWAPCHAIN_GDI_COMPATIBLE;
360 swapchain_desc->refresh_rate = present_parameters->FullScreen_RefreshRateInHz;
361 swapchain_desc->auto_restore_display_mode = TRUE;
363 if (present_parameters->Flags & ~D3DPRESENTFLAGS_MASK)
364 FIXME("Unhandled flags %#lx.\n", present_parameters->Flags & ~D3DPRESENTFLAGS_MASK);
366 return TRUE;
369 void d3d9_caps_from_wined3dcaps(const struct d3d9 *d3d9, unsigned int adapter_ordinal,
370 D3DCAPS9 *caps, const struct wined3d_caps *wined3d_caps)
372 static const DWORD ps_minor_version[] = {0, 4, 0, 0};
373 static const DWORD vs_minor_version[] = {0, 1, 0, 0};
374 static const DWORD texture_filter_caps =
375 D3DPTFILTERCAPS_MINFPOINT | D3DPTFILTERCAPS_MINFLINEAR | D3DPTFILTERCAPS_MINFANISOTROPIC |
376 D3DPTFILTERCAPS_MINFPYRAMIDALQUAD | D3DPTFILTERCAPS_MINFGAUSSIANQUAD|
377 D3DPTFILTERCAPS_MIPFPOINT | D3DPTFILTERCAPS_MIPFLINEAR | D3DPTFILTERCAPS_MAGFPOINT |
378 D3DPTFILTERCAPS_MAGFLINEAR |D3DPTFILTERCAPS_MAGFANISOTROPIC|D3DPTFILTERCAPS_MAGFPYRAMIDALQUAD|
379 D3DPTFILTERCAPS_MAGFGAUSSIANQUAD;
380 struct wined3d_adapter *wined3d_adapter;
381 struct wined3d_output_desc output_desc;
382 struct wined3d_output *wined3d_output;
383 unsigned int output_idx;
384 HRESULT hr;
386 caps->DeviceType = (D3DDEVTYPE)wined3d_caps->DeviceType;
387 caps->AdapterOrdinal = adapter_ordinal;
388 caps->Caps = wined3d_caps->Caps;
389 caps->Caps2 = wined3d_caps->Caps2;
390 caps->Caps3 = wined3d_caps->Caps3;
391 caps->PresentationIntervals = D3DPRESENT_INTERVAL_IMMEDIATE | D3DPRESENT_INTERVAL_ONE;
392 caps->CursorCaps = wined3d_caps->CursorCaps;
393 caps->DevCaps = wined3d_caps->DevCaps;
394 caps->PrimitiveMiscCaps = wined3d_caps->PrimitiveMiscCaps;
395 caps->RasterCaps = wined3d_caps->RasterCaps;
396 caps->ZCmpCaps = wined3d_caps->ZCmpCaps;
397 caps->SrcBlendCaps = wined3d_caps->SrcBlendCaps;
398 caps->DestBlendCaps = wined3d_caps->DestBlendCaps;
399 caps->AlphaCmpCaps = wined3d_caps->AlphaCmpCaps;
400 caps->ShadeCaps = wined3d_caps->ShadeCaps;
401 caps->TextureCaps = wined3d_caps->TextureCaps;
402 caps->TextureFilterCaps = wined3d_caps->TextureFilterCaps;
403 caps->CubeTextureFilterCaps = wined3d_caps->CubeTextureFilterCaps;
404 caps->VolumeTextureFilterCaps = wined3d_caps->VolumeTextureFilterCaps;
405 caps->TextureAddressCaps = wined3d_caps->TextureAddressCaps;
406 caps->VolumeTextureAddressCaps = wined3d_caps->VolumeTextureAddressCaps;
407 caps->LineCaps = wined3d_caps->LineCaps;
408 caps->MaxTextureWidth = wined3d_caps->MaxTextureWidth;
409 caps->MaxTextureHeight = wined3d_caps->MaxTextureHeight;
410 caps->MaxVolumeExtent = wined3d_caps->MaxVolumeExtent;
411 caps->MaxTextureRepeat = wined3d_caps->MaxTextureRepeat;
412 caps->MaxTextureAspectRatio = wined3d_caps->MaxTextureAspectRatio;
413 caps->MaxAnisotropy = wined3d_caps->MaxAnisotropy;
414 caps->MaxVertexW = wined3d_caps->MaxVertexW;
415 caps->GuardBandLeft = wined3d_caps->GuardBandLeft;
416 caps->GuardBandTop = wined3d_caps->GuardBandTop;
417 caps->GuardBandRight = wined3d_caps->GuardBandRight;
418 caps->GuardBandBottom = wined3d_caps->GuardBandBottom;
419 caps->ExtentsAdjust = wined3d_caps->ExtentsAdjust;
420 caps->StencilCaps = wined3d_caps->StencilCaps;
421 caps->FVFCaps = wined3d_caps->FVFCaps;
422 caps->TextureOpCaps = wined3d_caps->TextureOpCaps;
423 caps->MaxTextureBlendStages = wined3d_caps->MaxTextureBlendStages;
424 caps->MaxSimultaneousTextures = wined3d_caps->MaxSimultaneousTextures;
425 caps->VertexProcessingCaps = wined3d_caps->VertexProcessingCaps;
426 caps->MaxActiveLights = wined3d_caps->MaxActiveLights;
427 caps->MaxUserClipPlanes = wined3d_caps->MaxUserClipPlanes;
428 caps->MaxVertexBlendMatrices = wined3d_caps->MaxVertexBlendMatrices;
429 caps->MaxVertexBlendMatrixIndex = wined3d_caps->MaxVertexBlendMatrixIndex;
430 caps->MaxPointSize = wined3d_caps->MaxPointSize;
431 caps->MaxPrimitiveCount = wined3d_caps->MaxPrimitiveCount;
432 caps->MaxVertexIndex = wined3d_caps->MaxVertexIndex;
433 caps->MaxStreams = wined3d_caps->MaxStreams;
434 caps->MaxStreamStride = wined3d_caps->MaxStreamStride;
435 caps->VertexShaderVersion = wined3d_caps->VertexShaderVersion;
436 caps->MaxVertexShaderConst = wined3d_caps->MaxVertexShaderConst;
437 caps->PixelShaderVersion = wined3d_caps->PixelShaderVersion;
438 caps->PixelShader1xMaxValue = wined3d_caps->PixelShader1xMaxValue;
439 caps->DevCaps2 = wined3d_caps->DevCaps2;
440 caps->MaxNpatchTessellationLevel = wined3d_caps->MaxNpatchTessellationLevel;
441 caps->DeclTypes = wined3d_caps->DeclTypes;
442 caps->NumSimultaneousRTs = wined3d_caps->NumSimultaneousRTs;
443 caps->StretchRectFilterCaps = wined3d_caps->StretchRectFilterCaps;
444 caps->VS20Caps.Caps = wined3d_caps->VS20Caps.caps;
445 caps->VS20Caps.DynamicFlowControlDepth = wined3d_caps->VS20Caps.dynamic_flow_control_depth;
446 caps->VS20Caps.NumTemps = wined3d_caps->VS20Caps.temp_count;
447 caps->VS20Caps.StaticFlowControlDepth = wined3d_caps->VS20Caps.static_flow_control_depth;
448 caps->PS20Caps.Caps = wined3d_caps->PS20Caps.caps;
449 caps->PS20Caps.DynamicFlowControlDepth = wined3d_caps->PS20Caps.dynamic_flow_control_depth;
450 caps->PS20Caps.NumTemps = wined3d_caps->PS20Caps.temp_count;
451 caps->PS20Caps.StaticFlowControlDepth = wined3d_caps->PS20Caps.static_flow_control_depth;
452 caps->PS20Caps.NumInstructionSlots = wined3d_caps->PS20Caps.instruction_slot_count;
453 caps->VertexTextureFilterCaps = wined3d_caps->VertexTextureFilterCaps;
454 caps->MaxVShaderInstructionsExecuted = wined3d_caps->MaxVShaderInstructionsExecuted;
455 caps->MaxPShaderInstructionsExecuted = wined3d_caps->MaxPShaderInstructionsExecuted;
456 caps->MaxVertexShader30InstructionSlots = wined3d_caps->MaxVertexShader30InstructionSlots;
457 caps->MaxPixelShader30InstructionSlots = wined3d_caps->MaxPixelShader30InstructionSlots;
459 /* Some functionality is implemented in d3d9.dll, not wined3d.dll. Add the needed caps. */
460 caps->DevCaps2 |= D3DDEVCAPS2_CAN_STRETCHRECT_FROM_TEXTURES;
462 /* Filter wined3d caps. */
463 caps->TextureFilterCaps &= texture_filter_caps;
464 caps->CubeTextureFilterCaps &= texture_filter_caps;
465 caps->VolumeTextureFilterCaps &= texture_filter_caps;
467 caps->DevCaps &=
468 D3DDEVCAPS_EXECUTESYSTEMMEMORY | D3DDEVCAPS_EXECUTEVIDEOMEMORY | D3DDEVCAPS_TLVERTEXSYSTEMMEMORY |
469 D3DDEVCAPS_TLVERTEXVIDEOMEMORY | D3DDEVCAPS_TEXTURESYSTEMMEMORY| D3DDEVCAPS_TEXTUREVIDEOMEMORY |
470 D3DDEVCAPS_DRAWPRIMTLVERTEX | D3DDEVCAPS_CANRENDERAFTERFLIP | D3DDEVCAPS_TEXTURENONLOCALVIDMEM|
471 D3DDEVCAPS_DRAWPRIMITIVES2 | D3DDEVCAPS_SEPARATETEXTUREMEMORIES |
472 D3DDEVCAPS_DRAWPRIMITIVES2EX | D3DDEVCAPS_HWTRANSFORMANDLIGHT| D3DDEVCAPS_CANBLTSYSTONONLOCAL |
473 D3DDEVCAPS_HWRASTERIZATION | D3DDEVCAPS_PUREDEVICE | D3DDEVCAPS_QUINTICRTPATCHES |
474 D3DDEVCAPS_RTPATCHES | D3DDEVCAPS_RTPATCHHANDLEZERO | D3DDEVCAPS_NPATCHES;
476 caps->ShadeCaps &=
477 D3DPSHADECAPS_COLORGOURAUDRGB | D3DPSHADECAPS_SPECULARGOURAUDRGB |
478 D3DPSHADECAPS_ALPHAGOURAUDBLEND | D3DPSHADECAPS_FOGGOURAUD;
480 caps->RasterCaps &=
481 D3DPRASTERCAPS_DITHER | D3DPRASTERCAPS_ZTEST | D3DPRASTERCAPS_FOGVERTEX |
482 D3DPRASTERCAPS_FOGTABLE | D3DPRASTERCAPS_MIPMAPLODBIAS | D3DPRASTERCAPS_ZBUFFERLESSHSR |
483 D3DPRASTERCAPS_FOGRANGE | D3DPRASTERCAPS_ANISOTROPY | D3DPRASTERCAPS_WBUFFER |
484 D3DPRASTERCAPS_WFOG | D3DPRASTERCAPS_ZFOG | D3DPRASTERCAPS_COLORPERSPECTIVE |
485 D3DPRASTERCAPS_SCISSORTEST | D3DPRASTERCAPS_SLOPESCALEDEPTHBIAS |
486 D3DPRASTERCAPS_DEPTHBIAS | D3DPRASTERCAPS_MULTISAMPLE_TOGGLE;
488 caps->DevCaps2 &=
489 D3DDEVCAPS2_STREAMOFFSET | D3DDEVCAPS2_DMAPNPATCH | D3DDEVCAPS2_ADAPTIVETESSRTPATCH |
490 D3DDEVCAPS2_ADAPTIVETESSNPATCH | D3DDEVCAPS2_CAN_STRETCHRECT_FROM_TEXTURES |
491 D3DDEVCAPS2_PRESAMPLEDDMAPNPATCH| D3DDEVCAPS2_VERTEXELEMENTSCANSHARESTREAMOFFSET;
493 caps->Caps2 &=
494 D3DCAPS2_FULLSCREENGAMMA | D3DCAPS2_CANCALIBRATEGAMMA | D3DCAPS2_RESERVED |
495 D3DCAPS2_CANMANAGERESOURCE | D3DCAPS2_DYNAMICTEXTURES | D3DCAPS2_CANAUTOGENMIPMAP;
497 caps->VertexProcessingCaps &=
498 D3DVTXPCAPS_TEXGEN | D3DVTXPCAPS_MATERIALSOURCE7 | D3DVTXPCAPS_DIRECTIONALLIGHTS |
499 D3DVTXPCAPS_POSITIONALLIGHTS | D3DVTXPCAPS_LOCALVIEWER | D3DVTXPCAPS_TWEENING |
500 D3DVTXPCAPS_TEXGEN_SPHEREMAP | D3DVTXPCAPS_NO_TEXGEN_NONLOCALVIEWER;
502 caps->TextureCaps &=
503 D3DPTEXTURECAPS_PERSPECTIVE | D3DPTEXTURECAPS_POW2 | D3DPTEXTURECAPS_ALPHA |
504 D3DPTEXTURECAPS_SQUAREONLY | D3DPTEXTURECAPS_TEXREPEATNOTSCALEDBYSIZE |
505 D3DPTEXTURECAPS_ALPHAPALETTE | D3DPTEXTURECAPS_NONPOW2CONDITIONAL |
506 D3DPTEXTURECAPS_PROJECTED | D3DPTEXTURECAPS_CUBEMAP | D3DPTEXTURECAPS_VOLUMEMAP |
507 D3DPTEXTURECAPS_MIPMAP | D3DPTEXTURECAPS_MIPVOLUMEMAP | D3DPTEXTURECAPS_MIPCUBEMAP |
508 D3DPTEXTURECAPS_CUBEMAP_POW2 | D3DPTEXTURECAPS_VOLUMEMAP_POW2| D3DPTEXTURECAPS_NOPROJECTEDBUMPENV;
510 caps->MaxVertexShaderConst = min(D3D9_MAX_VERTEX_SHADER_CONSTANTF, caps->MaxVertexShaderConst);
511 caps->NumSimultaneousRTs = min(D3D_MAX_SIMULTANEOUS_RENDERTARGETS, caps->NumSimultaneousRTs);
513 if (caps->PixelShaderVersion > 3)
515 caps->PixelShaderVersion = D3DPS_VERSION(3, 0);
517 else
519 DWORD major = caps->PixelShaderVersion;
520 caps->PixelShaderVersion = D3DPS_VERSION(major, ps_minor_version[major]);
523 if (caps->VertexShaderVersion > 3)
525 caps->VertexShaderVersion = D3DVS_VERSION(3, 0);
527 else
529 DWORD major = caps->VertexShaderVersion;
530 caps->VertexShaderVersion = D3DVS_VERSION(major, vs_minor_version[major]);
533 /* Get adapter group information */
534 output_idx = adapter_ordinal;
535 wined3d_output = d3d9->wined3d_outputs[output_idx];
537 wined3d_mutex_lock();
538 hr = wined3d_output_get_desc(wined3d_output, &output_desc);
539 wined3d_mutex_unlock();
541 if (FAILED(hr))
543 ERR("Failed to get output desc, hr %#lx.\n", hr);
544 return;
547 caps->MasterAdapterOrdinal = output_idx - output_desc.ordinal;
548 caps->AdapterOrdinalInGroup = output_desc.ordinal;
549 if (!caps->AdapterOrdinalInGroup)
551 wined3d_adapter = wined3d_output_get_adapter(wined3d_output);
552 caps->NumberOfAdaptersInGroup = wined3d_adapter_get_output_count(wined3d_adapter);
554 else
556 caps->NumberOfAdaptersInGroup = 0;
560 static enum wined3d_texture_filter_type wined3d_texture_filter_type_from_d3d(D3DTEXTUREFILTERTYPE type)
562 return (enum wined3d_texture_filter_type)type;
565 static enum wined3d_transform_state wined3d_transform_state_from_d3d(D3DTRANSFORMSTATETYPE type)
567 return (enum wined3d_transform_state)type;
570 static enum wined3d_render_state wined3d_render_state_from_d3d(D3DRENDERSTATETYPE type)
572 return (enum wined3d_render_state)type;
575 static enum wined3d_sampler_state wined3d_sampler_state_from_d3d(D3DSAMPLERSTATETYPE type)
577 return (enum wined3d_sampler_state)type;
580 static enum wined3d_primitive_type wined3d_primitive_type_from_d3d(D3DPRIMITIVETYPE type)
582 return (enum wined3d_primitive_type)type;
585 static void device_reset_viewport_state(struct d3d9_device *device)
587 struct wined3d_viewport vp;
588 RECT rect;
590 wined3d_device_context_get_viewports(device->immediate_context, NULL, &vp);
591 wined3d_stateblock_set_viewport(device->state, &vp);
592 wined3d_device_context_get_scissor_rects(device->immediate_context, NULL, &rect);
593 wined3d_stateblock_set_scissor_rect(device->state, &rect);
596 static HRESULT WINAPI d3d9_device_QueryInterface(IDirect3DDevice9Ex *iface, REFIID riid, void **out)
598 TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
600 if (IsEqualGUID(riid, &IID_IDirect3DDevice9)
601 || IsEqualGUID(riid, &IID_IUnknown))
603 IDirect3DDevice9Ex_AddRef(iface);
604 *out = iface;
605 return S_OK;
608 if (IsEqualGUID(riid, &IID_IDirect3DDevice9Ex))
610 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
612 /* Find out if the creating d3d9 interface was created with Direct3DCreate9Ex.
613 * It doesn't matter with which function the device was created. */
614 if (!device->d3d_parent->extended)
616 WARN("IDirect3D9 instance wasn't created with CreateDirect3D9Ex, returning E_NOINTERFACE.\n");
617 *out = NULL;
618 return E_NOINTERFACE;
621 IDirect3DDevice9Ex_AddRef(iface);
622 *out = iface;
623 return S_OK;
626 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
628 *out = NULL;
629 return E_NOINTERFACE;
632 static ULONG WINAPI d3d9_device_AddRef(IDirect3DDevice9Ex *iface)
634 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
635 ULONG refcount = InterlockedIncrement(&device->refcount);
637 TRACE("%p increasing refcount to %lu.\n", iface, refcount);
639 return refcount;
642 static ULONG WINAPI DECLSPEC_HOTPATCH d3d9_device_Release(IDirect3DDevice9Ex *iface)
644 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
645 ULONG refcount;
647 if (device->in_destruction)
648 return 0;
650 refcount = InterlockedDecrement(&device->refcount);
652 TRACE("%p decreasing refcount to %lu.\n", iface, refcount);
654 if (!refcount)
656 unsigned i;
657 device->in_destruction = TRUE;
659 wined3d_mutex_lock();
660 for (i = 0; i < device->fvf_decl_count; ++i)
662 wined3d_vertex_declaration_decref(device->fvf_decls[i].decl);
664 free(device->fvf_decls);
666 wined3d_streaming_buffer_cleanup(&device->vertex_buffer);
667 wined3d_streaming_buffer_cleanup(&device->index_buffer);
669 for (i = 0; i < device->implicit_swapchain_count; ++i)
671 wined3d_swapchain_decref(device->implicit_swapchains[i]);
673 free(device->implicit_swapchains);
675 if (device->recording)
676 wined3d_stateblock_decref(device->recording);
677 wined3d_stateblock_decref(device->state);
679 wined3d_device_release_focus_window(device->wined3d_device);
680 wined3d_device_decref(device->wined3d_device);
681 wined3d_mutex_unlock();
683 IDirect3D9Ex_Release(&device->d3d_parent->IDirect3D9Ex_iface);
685 free(device);
688 return refcount;
691 static HRESULT WINAPI d3d9_device_TestCooperativeLevel(IDirect3DDevice9Ex *iface)
693 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
695 TRACE("iface %p.\n", iface);
697 TRACE("device state: %#lx.\n", device->device_state);
699 if (device->d3d_parent->extended)
700 return D3D_OK;
702 switch (device->device_state)
704 default:
705 case D3D9_DEVICE_STATE_OK:
706 return D3D_OK;
707 case D3D9_DEVICE_STATE_LOST:
708 return D3DERR_DEVICELOST;
709 case D3D9_DEVICE_STATE_NOT_RESET:
710 return D3DERR_DEVICENOTRESET;
714 static UINT WINAPI d3d9_device_GetAvailableTextureMem(IDirect3DDevice9Ex *iface)
716 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
717 UINT ret;
719 TRACE("iface %p.\n", iface);
721 wined3d_mutex_lock();
722 ret = wined3d_device_get_available_texture_mem(device->wined3d_device);
723 wined3d_mutex_unlock();
725 return ret;
728 static HRESULT WINAPI d3d9_device_EvictManagedResources(IDirect3DDevice9Ex *iface)
730 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
732 TRACE("iface %p.\n", iface);
734 wined3d_mutex_lock();
735 wined3d_device_evict_managed_resources(device->wined3d_device);
736 wined3d_mutex_unlock();
738 return D3D_OK;
741 static HRESULT WINAPI d3d9_device_GetDirect3D(IDirect3DDevice9Ex *iface, IDirect3D9 **d3d9)
743 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
745 TRACE("iface %p, d3d9 %p.\n", iface, d3d9);
747 if (!d3d9)
748 return D3DERR_INVALIDCALL;
750 return IDirect3D9Ex_QueryInterface(&device->d3d_parent->IDirect3D9Ex_iface, &IID_IDirect3D9, (void **)d3d9);
753 static HRESULT WINAPI d3d9_device_GetDeviceCaps(IDirect3DDevice9Ex *iface, D3DCAPS9 *caps)
755 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
756 struct wined3d_caps wined3d_caps;
757 HRESULT hr;
759 TRACE("iface %p, caps %p.\n", iface, caps);
761 if (!caps)
762 return D3DERR_INVALIDCALL;
764 memset(caps, 0, sizeof(*caps));
766 wined3d_mutex_lock();
767 hr = wined3d_device_get_device_caps(device->wined3d_device, &wined3d_caps);
768 wined3d_mutex_unlock();
770 d3d9_caps_from_wined3dcaps(device->d3d_parent, device->adapter_ordinal, caps, &wined3d_caps);
772 return hr;
775 static HRESULT WINAPI d3d9_device_GetDisplayMode(IDirect3DDevice9Ex *iface, UINT swapchain, D3DDISPLAYMODE *mode)
777 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
778 struct wined3d_display_mode wined3d_mode;
779 HRESULT hr;
781 TRACE("iface %p, swapchain %u, mode %p.\n", iface, swapchain, mode);
783 wined3d_mutex_lock();
784 hr = wined3d_device_get_display_mode(device->wined3d_device, swapchain, &wined3d_mode, NULL);
785 wined3d_mutex_unlock();
787 if (SUCCEEDED(hr))
789 mode->Width = wined3d_mode.width;
790 mode->Height = wined3d_mode.height;
791 mode->RefreshRate = wined3d_mode.refresh_rate;
792 mode->Format = d3dformat_from_wined3dformat(wined3d_mode.format_id);
795 return hr;
798 static HRESULT WINAPI d3d9_device_GetCreationParameters(IDirect3DDevice9Ex *iface,
799 D3DDEVICE_CREATION_PARAMETERS *parameters)
801 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
803 TRACE("iface %p, parameters %p.\n", iface, parameters);
805 wined3d_mutex_lock();
806 wined3d_device_get_creation_parameters(device->wined3d_device,
807 (struct wined3d_device_creation_parameters *)parameters);
808 wined3d_mutex_unlock();
810 parameters->AdapterOrdinal = device->adapter_ordinal;
811 return D3D_OK;
814 static HRESULT WINAPI d3d9_device_SetCursorProperties(IDirect3DDevice9Ex *iface,
815 UINT hotspot_x, UINT hotspot_y, IDirect3DSurface9 *bitmap)
817 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
818 struct d3d9_surface *bitmap_impl = unsafe_impl_from_IDirect3DSurface9(bitmap);
819 D3DSURFACE_DESC surface_desc;
820 D3DDISPLAYMODE mode;
821 HRESULT hr;
823 TRACE("iface %p, hotspot_x %u, hotspot_y %u, bitmap %p.\n",
824 iface, hotspot_x, hotspot_y, bitmap);
826 if (!bitmap)
828 WARN("No cursor bitmap, returning D3DERR_INVALIDCALL.\n");
829 return D3DERR_INVALIDCALL;
832 if (FAILED(hr = IDirect3DSurface9_GetDesc(bitmap, &surface_desc)))
834 WARN("Failed to get surface description, hr %#lx.\n", hr);
835 return hr;
838 if (FAILED(hr = IDirect3D9_GetAdapterDisplayMode(&device->d3d_parent->IDirect3D9Ex_iface,
839 device->adapter_ordinal, &mode)))
841 WARN("Failed to get device display mode, hr %#lx.\n", hr);
842 return hr;
845 if (surface_desc.Width > mode.Width || surface_desc.Height > mode.Height)
847 WARN("Surface dimension %ux%u exceeds display mode %ux%u.\n", surface_desc.Width,
848 surface_desc.Height, mode.Width, mode.Height);
849 return D3DERR_INVALIDCALL;
852 wined3d_mutex_lock();
853 hr = wined3d_device_set_cursor_properties(device->wined3d_device,
854 hotspot_x, hotspot_y, bitmap_impl->wined3d_texture, bitmap_impl->sub_resource_idx);
855 wined3d_mutex_unlock();
857 return hr;
860 static void WINAPI d3d9_device_SetCursorPosition(IDirect3DDevice9Ex *iface, int x, int y, DWORD flags)
862 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
864 TRACE("iface %p, x %u, y %u, flags %#lx.\n", iface, x, y, flags);
866 wined3d_mutex_lock();
867 wined3d_device_set_cursor_position(device->wined3d_device, x, y, flags);
868 wined3d_mutex_unlock();
871 static BOOL WINAPI d3d9_device_ShowCursor(IDirect3DDevice9Ex *iface, BOOL show)
873 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
874 BOOL ret;
876 TRACE("iface %p, show %#x.\n", iface, show);
878 wined3d_mutex_lock();
879 ret = wined3d_device_show_cursor(device->wined3d_device, show);
880 wined3d_mutex_unlock();
882 return ret;
885 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_CreateAdditionalSwapChain(IDirect3DDevice9Ex *iface,
886 D3DPRESENT_PARAMETERS *present_parameters, IDirect3DSwapChain9 **swapchain)
888 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
889 struct wined3d_device_creation_parameters creation_parameters;
890 struct wined3d_swapchain_desc desc;
891 struct d3d9_swapchain *object;
892 unsigned int swap_interval;
893 unsigned int output_idx;
894 unsigned int i, count;
895 HRESULT hr;
897 TRACE("iface %p, present_parameters %p, swapchain %p.\n",
898 iface, present_parameters, swapchain);
900 if (!present_parameters->Windowed)
902 WARN("Trying to create an additional fullscreen swapchain, returning D3DERR_INVALIDCALL.\n");
903 return D3DERR_INVALIDCALL;
906 wined3d_mutex_lock();
907 count = wined3d_device_get_swapchain_count(device->wined3d_device);
908 for (i = 0; i < count; ++i)
910 struct wined3d_swapchain *wined3d_swapchain;
912 wined3d_swapchain = wined3d_device_get_swapchain(device->wined3d_device, i);
913 wined3d_swapchain_get_desc(wined3d_swapchain, &desc);
915 if (!desc.windowed)
917 wined3d_mutex_unlock();
918 WARN("Trying to create an additional swapchain in fullscreen mode, returning D3DERR_INVALIDCALL.\n");
919 return D3DERR_INVALIDCALL;
922 wined3d_mutex_unlock();
924 output_idx = device->adapter_ordinal;
925 if (!wined3d_swapchain_desc_from_d3d9(&desc, device->d3d_parent->wined3d_outputs[output_idx],
926 present_parameters, device->d3d_parent->extended))
927 return D3DERR_INVALIDCALL;
928 wined3d_device_get_creation_parameters(device->wined3d_device, &creation_parameters);
929 if (creation_parameters.flags & WINED3DCREATE_NOWINDOWCHANGES)
930 desc.flags |= WINED3D_SWAPCHAIN_NO_WINDOW_CHANGES;
931 swap_interval = wined3dswapinterval_from_d3d(present_parameters->PresentationInterval);
932 if (SUCCEEDED(hr = d3d9_swapchain_create(device, &desc, swap_interval, &object)))
933 *swapchain = (IDirect3DSwapChain9 *)&object->IDirect3DSwapChain9Ex_iface;
934 present_parameters_from_wined3d_swapchain_desc(present_parameters,
935 &desc, present_parameters->PresentationInterval);
937 return hr;
940 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_GetSwapChain(IDirect3DDevice9Ex *iface,
941 UINT swapchain_idx, IDirect3DSwapChain9 **swapchain)
943 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
944 struct d3d9_swapchain *d3d9_swapchain;
945 HRESULT hr;
947 TRACE("iface %p, swapchain_idx %u, swapchain %p.\n", iface, swapchain_idx, swapchain);
949 wined3d_mutex_lock();
950 if (swapchain_idx < device->implicit_swapchain_count)
952 d3d9_swapchain = wined3d_swapchain_get_parent(device->implicit_swapchains[swapchain_idx]);
953 *swapchain = (IDirect3DSwapChain9 *)&d3d9_swapchain->IDirect3DSwapChain9Ex_iface;
954 IDirect3DSwapChain9Ex_AddRef(*swapchain);
955 hr = D3D_OK;
957 else
959 *swapchain = NULL;
960 hr = D3DERR_INVALIDCALL;
962 wined3d_mutex_unlock();
964 return hr;
967 static UINT WINAPI d3d9_device_GetNumberOfSwapChains(IDirect3DDevice9Ex *iface)
969 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
970 UINT count;
972 TRACE("iface %p.\n", iface);
974 wined3d_mutex_lock();
975 count = wined3d_device_get_swapchain_count(device->wined3d_device);
976 wined3d_mutex_unlock();
978 return count;
981 static HRESULT CDECL reset_enum_callback(struct wined3d_resource *resource)
983 struct d3d9_vertexbuffer *vertex_buffer;
984 struct d3d9_indexbuffer *index_buffer;
985 struct wined3d_resource_desc desc;
986 IDirect3DBaseTexture9 *texture;
987 struct d3d9_surface *surface;
988 IUnknown *parent;
990 wined3d_resource_get_desc(resource, &desc);
991 if ((desc.access & WINED3D_RESOURCE_ACCESS_CPU) || (desc.usage & WINED3DUSAGE_MANAGED))
992 return D3D_OK;
994 if (desc.resource_type != WINED3D_RTYPE_TEXTURE_2D)
996 if (desc.bind_flags & WINED3D_BIND_VERTEX_BUFFER)
998 vertex_buffer = wined3d_resource_get_parent(resource);
999 if (vertex_buffer && vertex_buffer->draw_buffer)
1000 return D3D_OK;
1002 if (desc.bind_flags & WINED3D_BIND_INDEX_BUFFER)
1004 index_buffer = wined3d_resource_get_parent(resource);
1005 if (index_buffer && index_buffer->sysmem)
1006 return D3D_OK;
1009 WARN("Resource %p in pool D3DPOOL_DEFAULT blocks the Reset call.\n", resource);
1010 return D3DERR_INVALIDCALL;
1013 parent = wined3d_resource_get_parent(resource);
1014 if (parent && SUCCEEDED(IUnknown_QueryInterface(parent, &IID_IDirect3DBaseTexture9, (void **)&texture)))
1016 IDirect3DBaseTexture9_Release(texture);
1017 WARN("Texture %p (resource %p) in pool D3DPOOL_DEFAULT blocks the Reset call.\n", texture, resource);
1018 return D3DERR_INVALIDCALL;
1021 surface = wined3d_texture_get_sub_resource_parent(wined3d_texture_from_resource(resource), 0);
1022 if (!surface || !surface->resource.refcount)
1023 return D3D_OK;
1025 WARN("Surface %p in pool D3DPOOL_DEFAULT blocks the Reset call.\n", surface);
1026 return D3DERR_INVALIDCALL;
1029 static HRESULT d3d9_device_get_swapchains(struct d3d9_device *device)
1031 UINT i, new_swapchain_count = wined3d_device_get_swapchain_count(device->wined3d_device);
1033 if (!(device->implicit_swapchains = malloc(new_swapchain_count * sizeof(*device->implicit_swapchains))))
1034 return E_OUTOFMEMORY;
1036 for (i = 0; i < new_swapchain_count; ++i)
1038 device->implicit_swapchains[i] = wined3d_device_get_swapchain(device->wined3d_device, i);
1040 device->implicit_swapchain_count = new_swapchain_count;
1042 return D3D_OK;
1045 static HRESULT d3d9_device_reset(struct d3d9_device *device,
1046 D3DPRESENT_PARAMETERS *present_parameters, D3DDISPLAYMODEEX *mode)
1048 struct wined3d_swapchain_desc swapchain_desc, old_swapchain_desc;
1049 struct d3d9_surface **old_backbuffers = NULL;
1050 BOOL extended = device->d3d_parent->extended;
1051 struct wined3d_display_mode wined3d_mode;
1052 struct wined3d_rendertarget_view *rtv;
1053 struct d3d9_swapchain *d3d9_swapchain;
1054 unsigned int output_idx;
1055 unsigned int i;
1056 HRESULT hr;
1058 if (!extended && device->device_state == D3D9_DEVICE_STATE_LOST)
1060 WARN("App not active, returning D3DERR_DEVICELOST.\n");
1061 return D3DERR_DEVICELOST;
1064 if (mode)
1066 wined3d_mode.width = mode->Width;
1067 wined3d_mode.height = mode->Height;
1068 wined3d_mode.refresh_rate = mode->RefreshRate;
1069 wined3d_mode.format_id = wined3dformat_from_d3dformat(mode->Format);
1070 wined3d_mode.scanline_ordering = wined3d_scanline_ordering_from_d3d(mode->ScanLineOrdering);
1073 output_idx = device->adapter_ordinal;
1074 if (!wined3d_swapchain_desc_from_d3d9(&swapchain_desc,
1075 device->d3d_parent->wined3d_outputs[output_idx], present_parameters, extended))
1076 return D3DERR_INVALIDCALL;
1077 swapchain_desc.flags |= WINED3D_SWAPCHAIN_IMPLICIT;
1079 wined3d_mutex_lock();
1081 wined3d_streaming_buffer_cleanup(&device->vertex_buffer);
1082 wined3d_streaming_buffer_cleanup(&device->index_buffer);
1084 if (!extended)
1086 if (device->recording)
1087 wined3d_stateblock_decref(device->recording);
1088 device->recording = NULL;
1089 device->update_state = device->state;
1090 wined3d_stateblock_reset(device->state);
1093 if (FAILED(hr = d3d9_device_get_swapchains(device)))
1095 wined3d_mutex_unlock();
1096 return hr;
1098 d3d9_swapchain = wined3d_swapchain_get_parent(device->implicit_swapchains[0]);
1100 wined3d_swapchain_get_desc(d3d9_swapchain->wined3d_swapchain, &old_swapchain_desc);
1102 /* wined3d_device_reset() may recreate swapchain textures.
1104 * If the device is not extended, we don't need to remove the reference to
1105 * the wined3d swapchain from the old d3d9 surfaces: we will fail the reset
1106 * if they have 0 references, and therefore they are not actually holding a
1107 * reference to the wined3d swapchain, and will not do anything with it when
1108 * they are destroyed.
1110 * If the device is extended, those swapchain textures can survive the
1111 * reset, and need to be detached from the implicit swapchain here. To add
1112 * some complexity, we need to add a temporary reference, but we can only do
1113 * that in the extended case, otherwise we'll try to validate that there are
1114 * 0 reference and fail. */
1116 if (extended)
1118 if (!(old_backbuffers = malloc(old_swapchain_desc.backbuffer_count * sizeof(*old_backbuffers))))
1120 wined3d_mutex_unlock();
1121 return hr;
1124 for (i = 0; i < old_swapchain_desc.backbuffer_count; ++i)
1126 old_backbuffers[i] = wined3d_texture_get_sub_resource_parent(
1127 wined3d_swapchain_get_back_buffer(d3d9_swapchain->wined3d_swapchain, i), 0);
1128 /* Resetting might drop the last reference, so grab an extra one here.
1129 * This also lets us always decref the swapchain, without needing to
1130 * worry about whether it's externally referenced. */
1131 IDirect3DSurface9_AddRef(&old_backbuffers[i]->IDirect3DSurface9_iface);
1135 if (SUCCEEDED(hr = wined3d_device_reset(device->wined3d_device, &swapchain_desc,
1136 mode ? &wined3d_mode : NULL, reset_enum_callback, !extended)))
1138 struct d3d9_surface *surface;
1140 free(device->implicit_swapchains);
1142 if (!extended)
1144 device->auto_mipmaps = 0;
1145 wined3d_stateblock_set_render_state(device->state, WINED3D_RS_ZENABLE,
1146 !!swapchain_desc.enable_auto_depth_stencil);
1147 device_reset_viewport_state(device);
1150 if (FAILED(hr = d3d9_device_get_swapchains(device)))
1152 device->device_state = D3D9_DEVICE_STATE_NOT_RESET;
1154 else
1156 d3d9_swapchain = wined3d_swapchain_get_parent(device->implicit_swapchains[0]);
1157 d3d9_swapchain->swap_interval
1158 = wined3dswapinterval_from_d3d(present_parameters->PresentationInterval);
1159 wined3d_swapchain_get_desc(d3d9_swapchain->wined3d_swapchain, &swapchain_desc);
1160 present_parameters->BackBufferWidth = swapchain_desc.backbuffer_width;
1161 present_parameters->BackBufferHeight = swapchain_desc.backbuffer_height;
1162 present_parameters->BackBufferFormat = d3dformat_from_wined3dformat(swapchain_desc.backbuffer_format);
1163 present_parameters->BackBufferCount = swapchain_desc.backbuffer_count;
1165 if (extended)
1167 for (i = 0; i < old_swapchain_desc.backbuffer_count; ++i)
1169 surface = old_backbuffers[i];
1170 /* We have a reference to this surface, so we can assert that it's
1171 * currently holding a reference to the swapchain. */
1172 wined3d_swapchain_decref(surface->swapchain);
1173 surface->swapchain = NULL;
1174 surface->container = (IUnknown *)&device->IDirect3DDevice9Ex_iface;
1178 /* FIXME: This should be the new backbuffer count, but we don't support
1179 * changing the backbuffer count in wined3d yet. */
1180 for (i = 0; i < old_swapchain_desc.backbuffer_count; ++i)
1182 struct wined3d_texture *backbuffer = wined3d_swapchain_get_back_buffer(d3d9_swapchain->wined3d_swapchain, i);
1184 if ((surface = d3d9_surface_create(backbuffer, 0, (IUnknown *)&device->IDirect3DDevice9Ex_iface)))
1185 surface->parent_device = &device->IDirect3DDevice9Ex_iface;
1188 device->device_state = D3D9_DEVICE_STATE_OK;
1190 if (extended)
1192 const struct wined3d_viewport *current = &device->stateblock_state->viewport;
1193 struct wined3d_viewport vp;
1194 RECT rect;
1196 vp.x = 0;
1197 vp.y = 0;
1198 vp.width = swapchain_desc.backbuffer_width;
1199 vp.height = swapchain_desc.backbuffer_height;
1200 vp.min_z = current->min_z;
1201 vp.max_z = current->max_z;
1202 wined3d_stateblock_set_viewport(device->state, &vp);
1204 SetRect(&rect, 0, 0, swapchain_desc.backbuffer_width, swapchain_desc.backbuffer_height);
1205 wined3d_stateblock_set_scissor_rect(device->state, &rect);
1209 rtv = wined3d_device_context_get_rendertarget_view(device->immediate_context, 0);
1210 device->render_targets[0] = wined3d_rendertarget_view_get_sub_resource_parent(rtv);
1211 for (i = 1; i < ARRAY_SIZE(device->render_targets); ++i)
1212 device->render_targets[i] = NULL;
1214 if ((rtv = wined3d_device_context_get_depth_stencil_view(device->immediate_context)))
1216 struct wined3d_resource *resource = wined3d_rendertarget_view_get_resource(rtv);
1218 if ((surface = d3d9_surface_create(wined3d_texture_from_resource(resource), 0,
1219 (IUnknown *)&device->IDirect3DDevice9Ex_iface)))
1220 surface->parent_device = &device->IDirect3DDevice9Ex_iface;
1223 else if (!extended)
1225 device->device_state = D3D9_DEVICE_STATE_NOT_RESET;
1228 if (extended)
1230 for (i = 0; i < old_swapchain_desc.backbuffer_count; ++i)
1231 IDirect3DSurface9_Release(&old_backbuffers[i]->IDirect3DSurface9_iface);
1232 free(old_backbuffers);
1235 wined3d_mutex_unlock();
1237 return hr;
1240 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_Reset(IDirect3DDevice9Ex *iface,
1241 D3DPRESENT_PARAMETERS *present_parameters)
1243 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1245 TRACE("iface %p, present_parameters %p.\n", iface, present_parameters);
1247 return d3d9_device_reset(device, present_parameters, NULL);
1250 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_Present(IDirect3DDevice9Ex *iface,
1251 const RECT *src_rect, const RECT *dst_rect, HWND dst_window_override, const RGNDATA *dirty_region)
1253 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1254 struct d3d9_swapchain *swapchain;
1255 unsigned int i;
1256 HRESULT hr;
1258 TRACE("iface %p, src_rect %s, dst_rect %s, dst_window_override %p, dirty_region %p.\n",
1259 iface, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect), dst_window_override, dirty_region);
1261 if (device->device_state != D3D9_DEVICE_STATE_OK)
1262 return device->d3d_parent->extended ? S_PRESENT_OCCLUDED : D3DERR_DEVICELOST;
1264 if (dirty_region)
1265 FIXME("Ignoring dirty_region %p.\n", dirty_region);
1267 wined3d_mutex_lock();
1268 for (i = 0; i < device->implicit_swapchain_count; ++i)
1270 swapchain = wined3d_swapchain_get_parent(device->implicit_swapchains[i]);
1271 if (FAILED(hr = wined3d_swapchain_present(swapchain->wined3d_swapchain,
1272 src_rect, dst_rect, dst_window_override, swapchain->swap_interval, 0)))
1274 wined3d_mutex_unlock();
1275 return hr;
1278 wined3d_mutex_unlock();
1280 return D3D_OK;
1283 static HRESULT WINAPI d3d9_device_GetBackBuffer(IDirect3DDevice9Ex *iface, UINT swapchain,
1284 UINT backbuffer_idx, D3DBACKBUFFER_TYPE backbuffer_type, IDirect3DSurface9 **backbuffer)
1286 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1287 struct d3d9_swapchain *d3d9_swapchain;
1288 HRESULT hr;
1290 TRACE("iface %p, swapchain %u, backbuffer_idx %u, backbuffer_type %#x, backbuffer %p.\n",
1291 iface, swapchain, backbuffer_idx, backbuffer_type, backbuffer);
1293 /* backbuffer_type is ignored by native. */
1295 /* No need to check for backbuffer == NULL, Windows crashes in that case. */
1296 *backbuffer = NULL;
1298 wined3d_mutex_lock();
1299 if (swapchain >= device->implicit_swapchain_count)
1301 wined3d_mutex_unlock();
1302 WARN("Swapchain index %u is out of range, returning D3DERR_INVALIDCALL.\n", swapchain);
1303 return D3DERR_INVALIDCALL;
1306 d3d9_swapchain = wined3d_swapchain_get_parent(device->implicit_swapchains[swapchain]);
1307 hr = IDirect3DSwapChain9Ex_GetBackBuffer(&d3d9_swapchain->IDirect3DSwapChain9Ex_iface,
1308 backbuffer_idx, backbuffer_type, backbuffer);
1309 wined3d_mutex_unlock();
1311 return hr;
1314 static HRESULT WINAPI d3d9_device_GetRasterStatus(IDirect3DDevice9Ex *iface,
1315 UINT swapchain, D3DRASTER_STATUS *raster_status)
1317 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1318 HRESULT hr;
1320 TRACE("iface %p, swapchain %u, raster_status %p.\n", iface, swapchain, raster_status);
1322 wined3d_mutex_lock();
1323 hr = wined3d_device_get_raster_status(device->wined3d_device,
1324 swapchain, (struct wined3d_raster_status *)raster_status);
1325 wined3d_mutex_unlock();
1327 return hr;
1330 static HRESULT WINAPI d3d9_device_SetDialogBoxMode(IDirect3DDevice9Ex *iface, BOOL enable)
1332 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1333 HRESULT hr;
1335 TRACE("iface %p, enable %#x.\n", iface, enable);
1337 wined3d_mutex_lock();
1338 hr = wined3d_device_set_dialog_box_mode(device->wined3d_device, enable);
1339 wined3d_mutex_unlock();
1341 return hr;
1344 static void WINAPI d3d9_device_SetGammaRamp(IDirect3DDevice9Ex *iface,
1345 UINT swapchain, DWORD flags, const D3DGAMMARAMP *ramp)
1347 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1349 TRACE("iface %p, swapchain %u, flags %#lx, ramp %p.\n", iface, swapchain, flags, ramp);
1351 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
1352 wined3d_mutex_lock();
1353 wined3d_device_set_gamma_ramp(device->wined3d_device, swapchain, flags, (const struct wined3d_gamma_ramp *)ramp);
1354 wined3d_mutex_unlock();
1357 static void WINAPI d3d9_device_GetGammaRamp(IDirect3DDevice9Ex *iface, UINT swapchain, D3DGAMMARAMP *ramp)
1359 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1361 TRACE("iface %p, swapchain %u, ramp %p.\n", iface, swapchain, ramp);
1363 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
1364 wined3d_mutex_lock();
1365 wined3d_device_get_gamma_ramp(device->wined3d_device, swapchain, (struct wined3d_gamma_ramp *)ramp);
1366 wined3d_mutex_unlock();
1369 static HRESULT WINAPI d3d9_device_CreateTexture(IDirect3DDevice9Ex *iface,
1370 UINT width, UINT height, UINT levels, DWORD usage, D3DFORMAT format,
1371 D3DPOOL pool, IDirect3DTexture9 **texture, HANDLE *shared_handle)
1373 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1374 struct d3d9_texture *object;
1375 BOOL set_mem = FALSE;
1376 unsigned int i;
1377 HRESULT hr;
1379 TRACE("iface %p, width %u, height %u, levels %u, usage %#lx, format %#x, pool %#x, texture %p, shared_handle %p.\n",
1380 iface, width, height, levels, usage, format, pool, texture, shared_handle);
1382 *texture = NULL;
1383 if (shared_handle)
1385 if (!device->d3d_parent->extended)
1387 WARN("Trying to create a shared or user memory texture on a non-ex device.\n");
1388 return E_NOTIMPL;
1391 if (pool == D3DPOOL_SYSTEMMEM)
1393 if (levels != 1)
1394 return D3DERR_INVALIDCALL;
1395 set_mem = TRUE;
1397 else
1399 if (pool != D3DPOOL_DEFAULT)
1401 WARN("Trying to create a shared texture in pool %#x.\n", pool);
1402 return D3DERR_INVALIDCALL;
1404 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1408 if (!(object = calloc(1, sizeof(*object))))
1409 return D3DERR_OUTOFVIDEOMEMORY;
1411 hr = d3d9_texture_2d_init(object, device, width, height, levels, usage, format, pool);
1412 if (FAILED(hr))
1414 WARN("Failed to initialize texture, hr %#lx.\n", hr);
1415 free(object);
1416 return hr;
1419 if (set_mem)
1421 wined3d_mutex_lock();
1422 wined3d_texture_update_desc(object->wined3d_texture, 0, *shared_handle, 0);
1423 wined3d_mutex_unlock();
1426 levels = wined3d_texture_get_level_count(object->wined3d_texture);
1427 for (i = 0; i < levels; ++i)
1429 if (!d3d9_surface_create(object->wined3d_texture, i, (IUnknown *)&object->IDirect3DBaseTexture9_iface))
1431 IDirect3DTexture9_Release(&object->IDirect3DBaseTexture9_iface);
1432 return E_OUTOFMEMORY;
1436 TRACE("Created texture %p.\n", object);
1437 *texture = (IDirect3DTexture9 *)&object->IDirect3DBaseTexture9_iface;
1439 return D3D_OK;
1442 static HRESULT WINAPI d3d9_device_CreateVolumeTexture(IDirect3DDevice9Ex *iface,
1443 UINT width, UINT height, UINT depth, UINT levels, DWORD usage, D3DFORMAT format,
1444 D3DPOOL pool, IDirect3DVolumeTexture9 **texture, HANDLE *shared_handle)
1446 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1447 struct d3d9_texture *object;
1448 HRESULT hr;
1450 TRACE("iface %p, width %u, height %u, depth %u, levels %u, "
1451 "usage %#lx, format %#x, pool %#x, texture %p, shared_handle %p.\n",
1452 iface, width, height, depth, levels,
1453 usage, format, pool, texture, shared_handle);
1455 *texture = NULL;
1456 if (shared_handle)
1458 if (!device->d3d_parent->extended)
1460 WARN("Trying to create a shared volume texture on a non-ex device.\n");
1461 return E_NOTIMPL;
1464 if (pool != D3DPOOL_DEFAULT)
1466 WARN("Trying to create a shared volume texture in pool %#x.\n", pool);
1467 return D3DERR_INVALIDCALL;
1469 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1472 if (!(object = calloc(1, sizeof(*object))))
1473 return D3DERR_OUTOFVIDEOMEMORY;
1475 hr = d3d9_texture_3d_init(object, device, width, height, depth, levels, usage, format, pool);
1476 if (FAILED(hr))
1478 WARN("Failed to initialize volume texture, hr %#lx.\n", hr);
1479 free(object);
1480 return hr;
1483 TRACE("Created volume texture %p.\n", object);
1484 *texture = (IDirect3DVolumeTexture9 *)&object->IDirect3DBaseTexture9_iface;
1486 return D3D_OK;
1489 static HRESULT WINAPI d3d9_device_CreateCubeTexture(IDirect3DDevice9Ex *iface,
1490 UINT edge_length, UINT levels, DWORD usage, D3DFORMAT format, D3DPOOL pool,
1491 IDirect3DCubeTexture9 **texture, HANDLE *shared_handle)
1493 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1494 struct d3d9_texture *object;
1495 unsigned int i;
1496 HRESULT hr;
1498 TRACE("iface %p, edge_length %u, levels %u, usage %#lx, format %#x, pool %#x, texture %p, shared_handle %p.\n",
1499 iface, edge_length, levels, usage, format, pool, texture, shared_handle);
1501 *texture = NULL;
1502 if (shared_handle)
1504 if (!device->d3d_parent->extended)
1506 WARN("Trying to create a shared cube texture on a non-ex device.\n");
1507 return E_NOTIMPL;
1510 if (pool != D3DPOOL_DEFAULT)
1512 WARN("Trying to create a shared cube texture in pool %#x.\n", pool);
1513 return D3DERR_INVALIDCALL;
1515 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1518 if (!(object = calloc(1, sizeof(*object))))
1519 return D3DERR_OUTOFVIDEOMEMORY;
1521 hr = d3d9_texture_cube_init(object, device, edge_length, levels, usage, format, pool);
1522 if (FAILED(hr))
1524 WARN("Failed to initialize cube texture, hr %#lx.\n", hr);
1525 free(object);
1526 return hr;
1529 levels = wined3d_texture_get_level_count(object->wined3d_texture);
1530 for (i = 0; i < levels * 6; ++i)
1532 if (!d3d9_surface_create(object->wined3d_texture, i, (IUnknown *)&object->IDirect3DBaseTexture9_iface))
1534 IDirect3DTexture9_Release(&object->IDirect3DBaseTexture9_iface);
1535 return E_OUTOFMEMORY;
1539 TRACE("Created cube texture %p.\n", object);
1540 *texture = (IDirect3DCubeTexture9 *)&object->IDirect3DBaseTexture9_iface;
1542 return D3D_OK;
1545 static HRESULT WINAPI d3d9_device_CreateVertexBuffer(IDirect3DDevice9Ex *iface, UINT size,
1546 DWORD usage, DWORD fvf, D3DPOOL pool, IDirect3DVertexBuffer9 **buffer,
1547 HANDLE *shared_handle)
1549 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1550 struct d3d9_vertexbuffer *object;
1551 HRESULT hr;
1553 TRACE("iface %p, size %u, usage %#lx, fvf %#lx, pool %#x, buffer %p, shared_handle %p.\n",
1554 iface, size, usage, fvf, pool, buffer, shared_handle);
1556 if (shared_handle)
1558 if (!device->d3d_parent->extended)
1560 WARN("Trying to create a shared vertex buffer on a non-ex device.\n");
1561 return E_NOTIMPL;
1564 if (pool != D3DPOOL_DEFAULT)
1566 WARN("Trying to create a shared vertex buffer in pool %#x.\n", pool);
1567 return D3DERR_NOTAVAILABLE;
1569 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1572 if (!(object = calloc(1, sizeof(*object))))
1573 return D3DERR_OUTOFVIDEOMEMORY;
1575 hr = vertexbuffer_init(object, device, size, usage, fvf, pool);
1576 if (FAILED(hr))
1578 WARN("Failed to initialize vertex buffer, hr %#lx.\n", hr);
1579 free(object);
1580 return hr;
1583 TRACE("Created vertex buffer %p.\n", object);
1584 *buffer = &object->IDirect3DVertexBuffer9_iface;
1586 return D3D_OK;
1589 static HRESULT WINAPI d3d9_device_CreateIndexBuffer(IDirect3DDevice9Ex *iface, UINT size,
1590 DWORD usage, D3DFORMAT format, D3DPOOL pool, IDirect3DIndexBuffer9 **buffer,
1591 HANDLE *shared_handle)
1593 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1594 struct d3d9_indexbuffer *object;
1595 HRESULT hr;
1597 TRACE("iface %p, size %u, usage %#lx, format %#x, pool %#x, buffer %p, shared_handle %p.\n",
1598 iface, size, usage, format, pool, buffer, shared_handle);
1600 if (shared_handle)
1602 if (!device->d3d_parent->extended)
1604 WARN("Trying to create a shared index buffer on a non-ex device.\n");
1605 return E_NOTIMPL;
1608 if (pool != D3DPOOL_DEFAULT)
1610 WARN("Trying to create a shared index buffer in pool %#x.\n", pool);
1611 return D3DERR_NOTAVAILABLE;
1613 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1616 if (!(object = calloc(1, sizeof(*object))))
1617 return D3DERR_OUTOFVIDEOMEMORY;
1619 hr = indexbuffer_init(object, device, size, usage, format, pool);
1620 if (FAILED(hr))
1622 WARN("Failed to initialize index buffer, hr %#lx.\n", hr);
1623 free(object);
1624 return hr;
1627 TRACE("Created index buffer %p.\n", object);
1628 *buffer = &object->IDirect3DIndexBuffer9_iface;
1630 return D3D_OK;
1633 static HRESULT d3d9_device_create_surface(struct d3d9_device *device, unsigned int flags,
1634 enum wined3d_format_id format, enum wined3d_multisample_type multisample_type,
1635 unsigned int multisample_quality, unsigned int usage, unsigned int bind_flags, unsigned int access,
1636 unsigned int width, unsigned int height, void *user_mem, IDirect3DSurface9 **surface)
1638 struct wined3d_resource_desc desc;
1639 struct d3d9_surface *surface_impl;
1640 struct wined3d_texture *texture;
1641 HRESULT hr;
1643 TRACE("device %p, flags %#x, format %#x, multisample_type %#x, multisample_quality %u, "
1644 "usage %#x, bind_flags %#x, access %#x, width %u, height %u, user_mem %p, surface %p.\n",
1645 device, flags, format, multisample_type, multisample_quality, usage,
1646 bind_flags, access, width, height, user_mem, surface);
1648 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
1649 desc.format = format;
1650 desc.multisample_type = multisample_type;
1651 desc.multisample_quality = multisample_quality;
1652 desc.usage = usage;
1653 desc.bind_flags = bind_flags;
1654 desc.access = access;
1655 desc.width = width;
1656 desc.height = height;
1657 desc.depth = 1;
1658 desc.size = 0;
1660 if (is_gdi_compat_wined3dformat(desc.format))
1661 flags |= WINED3D_TEXTURE_CREATE_GET_DC;
1663 wined3d_mutex_lock();
1665 if (FAILED(hr = wined3d_texture_create(device->wined3d_device, &desc,
1666 1, 1, flags, NULL, NULL, &d3d9_null_wined3d_parent_ops, &texture)))
1668 wined3d_mutex_unlock();
1669 WARN("Failed to create texture, hr %#lx.\n", hr);
1670 if (hr == WINED3DERR_NOTAVAILABLE)
1671 hr = D3DERR_INVALIDCALL;
1672 return hr;
1675 if (!(surface_impl = d3d9_surface_create(texture, 0, NULL)))
1677 wined3d_texture_decref(texture);
1678 wined3d_mutex_unlock();
1679 return E_OUTOFMEMORY;
1681 surface_impl->parent_device = &device->IDirect3DDevice9Ex_iface;
1682 *surface = &surface_impl->IDirect3DSurface9_iface;
1683 IDirect3DSurface9_AddRef(*surface);
1685 if (user_mem)
1686 wined3d_texture_update_desc(texture, 0, user_mem, 0);
1688 wined3d_texture_decref(texture);
1690 wined3d_mutex_unlock();
1692 return D3D_OK;
1695 BOOL is_gdi_compat_wined3dformat(enum wined3d_format_id format)
1697 switch (format)
1699 case WINED3DFMT_B8G8R8A8_UNORM:
1700 case WINED3DFMT_B8G8R8X8_UNORM:
1701 case WINED3DFMT_B5G6R5_UNORM:
1702 case WINED3DFMT_B5G5R5X1_UNORM:
1703 case WINED3DFMT_B5G5R5A1_UNORM:
1704 case WINED3DFMT_B8G8R8_UNORM:
1705 return TRUE;
1706 default:
1707 return FALSE;
1711 static HRESULT WINAPI d3d9_device_CreateRenderTarget(IDirect3DDevice9Ex *iface, UINT width, UINT height,
1712 D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
1713 BOOL lockable, IDirect3DSurface9 **surface, HANDLE *shared_handle)
1715 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1716 unsigned int access = WINED3D_RESOURCE_ACCESS_GPU;
1718 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %lu.\n"
1719 "lockable %#x, surface %p, shared_handle %p.\n",
1720 iface, width, height, format, multisample_type, multisample_quality,
1721 lockable, surface, shared_handle);
1723 *surface = NULL;
1724 if (shared_handle)
1726 if (!device->d3d_parent->extended)
1728 WARN("Trying to create a shared render target on a non-ex device.\n");
1729 return E_NOTIMPL;
1732 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1735 if (lockable)
1736 access |= WINED3D_RESOURCE_ACCESS_MAP_R | WINED3D_RESOURCE_ACCESS_MAP_W;
1738 return d3d9_device_create_surface(device, 0, wined3dformat_from_d3dformat(format),
1739 wined3d_multisample_type_from_d3d(multisample_type), multisample_quality, 0,
1740 WINED3D_BIND_RENDER_TARGET, access, width, height, NULL, surface);
1743 static HRESULT WINAPI d3d9_device_CreateDepthStencilSurface(IDirect3DDevice9Ex *iface, UINT width, UINT height,
1744 D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
1745 BOOL discard, IDirect3DSurface9 **surface, HANDLE *shared_handle)
1747 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1748 DWORD flags = 0;
1750 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %lu.\n"
1751 "discard %#x, surface %p, shared_handle %p.\n",
1752 iface, width, height, format, multisample_type, multisample_quality,
1753 discard, surface, shared_handle);
1755 *surface = NULL;
1756 if (shared_handle)
1758 if (!device->d3d_parent->extended)
1760 WARN("Trying to create a shared depth stencil on a non-ex device.\n");
1761 return E_NOTIMPL;
1764 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1767 if (discard)
1768 flags |= WINED3D_TEXTURE_CREATE_DISCARD;
1770 return d3d9_device_create_surface(device, flags, wined3dformat_from_d3dformat(format),
1771 wined3d_multisample_type_from_d3d(multisample_type), multisample_quality, 0,
1772 WINED3D_BIND_DEPTH_STENCIL, WINED3D_RESOURCE_ACCESS_GPU, width, height, NULL, surface);
1776 static HRESULT WINAPI d3d9_device_UpdateSurface(IDirect3DDevice9Ex *iface,
1777 IDirect3DSurface9 *src_surface, const RECT *src_rect,
1778 IDirect3DSurface9 *dst_surface, const POINT *dst_point)
1780 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1781 struct d3d9_surface *src = unsafe_impl_from_IDirect3DSurface9(src_surface);
1782 struct d3d9_surface *dst = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1783 struct wined3d_sub_resource_desc src_desc, dst_desc;
1784 struct wined3d_box src_box;
1785 HRESULT hr;
1787 TRACE("iface %p, src_surface %p, src_rect %s, dst_surface %p, dst_point %p.\n",
1788 iface, src_surface, wine_dbgstr_rect(src_rect), dst_surface, dst_point);
1790 wined3d_mutex_lock();
1792 wined3d_texture_get_sub_resource_desc(src->wined3d_texture, src->sub_resource_idx, &src_desc);
1793 wined3d_texture_get_sub_resource_desc(dst->wined3d_texture, dst->sub_resource_idx, &dst_desc);
1794 if (src_desc.format != dst_desc.format)
1796 wined3d_mutex_unlock();
1797 WARN("Surface formats (%#x/%#x) don't match.\n",
1798 d3dformat_from_wined3dformat(src_desc.format),
1799 d3dformat_from_wined3dformat(dst_desc.format));
1800 return D3DERR_INVALIDCALL;
1803 if (src_rect)
1804 wined3d_box_set(&src_box, src_rect->left, src_rect->top, src_rect->right, src_rect->bottom, 0, 1);
1805 else
1806 wined3d_box_set(&src_box, 0, 0, src_desc.width, src_desc.height, 0, 1);
1808 hr = wined3d_device_context_copy_sub_resource_region(device->immediate_context,
1809 wined3d_texture_get_resource(dst->wined3d_texture), dst->sub_resource_idx, dst_point ? dst_point->x : 0,
1810 dst_point ? dst_point->y : 0, 0, wined3d_texture_get_resource(src->wined3d_texture),
1811 src->sub_resource_idx, &src_box, 0);
1812 if (SUCCEEDED(hr) && dst->texture)
1813 d3d9_texture_flag_auto_gen_mipmap(dst->texture);
1815 wined3d_mutex_unlock();
1817 if (FAILED(hr))
1818 return D3DERR_INVALIDCALL;
1820 return hr;
1823 static HRESULT WINAPI d3d9_device_UpdateTexture(IDirect3DDevice9Ex *iface,
1824 IDirect3DBaseTexture9 *src_texture, IDirect3DBaseTexture9 *dst_texture)
1826 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1827 struct d3d9_texture *src_impl, *dst_impl;
1828 HRESULT hr;
1830 TRACE("iface %p, src_texture %p, dst_texture %p.\n", iface, src_texture, dst_texture);
1832 src_impl = unsafe_impl_from_IDirect3DBaseTexture9(src_texture);
1833 dst_impl = unsafe_impl_from_IDirect3DBaseTexture9(dst_texture);
1835 if (src_impl->draw_texture || dst_impl->draw_texture)
1837 WARN("Source or destination is managed; returning D3DERR_INVALIDCALL.\n");
1838 return D3DERR_INVALIDCALL;
1841 wined3d_mutex_lock();
1842 hr = wined3d_device_update_texture(device->wined3d_device,
1843 src_impl->wined3d_texture, dst_impl->wined3d_texture);
1844 if (SUCCEEDED(hr))
1845 d3d9_texture_flag_auto_gen_mipmap(dst_impl);
1846 wined3d_mutex_unlock();
1848 return hr;
1851 static HRESULT WINAPI d3d9_device_GetRenderTargetData(IDirect3DDevice9Ex *iface,
1852 IDirect3DSurface9 *render_target, IDirect3DSurface9 *dst_surface)
1854 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1855 struct d3d9_surface *rt_impl = unsafe_impl_from_IDirect3DSurface9(render_target);
1856 struct d3d9_surface *dst_impl = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1857 struct wined3d_sub_resource_desc wined3d_desc;
1858 RECT dst_rect, src_rect;
1859 HRESULT hr;
1861 TRACE("iface %p, render_target %p, dst_surface %p.\n", iface, render_target, dst_surface);
1863 if (!render_target || !dst_surface)
1864 return D3DERR_INVALIDCALL;
1866 wined3d_mutex_lock();
1867 wined3d_texture_get_sub_resource_desc(dst_impl->wined3d_texture, dst_impl->sub_resource_idx, &wined3d_desc);
1868 SetRect(&dst_rect, 0, 0, wined3d_desc.width, wined3d_desc.height);
1870 wined3d_texture_get_sub_resource_desc(rt_impl->wined3d_texture, rt_impl->sub_resource_idx, &wined3d_desc);
1871 SetRect(&src_rect, 0, 0, wined3d_desc.width, wined3d_desc.height);
1873 /* TODO: Check surface sizes, pools, etc. */
1874 if (wined3d_desc.multisample_type)
1875 hr = D3DERR_INVALIDCALL;
1876 else
1877 hr = wined3d_device_context_blt(device->immediate_context,
1878 dst_impl->wined3d_texture, dst_impl->sub_resource_idx, &dst_rect,
1879 rt_impl->wined3d_texture, rt_impl->sub_resource_idx, &src_rect, 0, NULL, WINED3D_TEXF_POINT);
1880 wined3d_mutex_unlock();
1882 return hr;
1885 static HRESULT WINAPI d3d9_device_GetFrontBufferData(IDirect3DDevice9Ex *iface,
1886 UINT swapchain, IDirect3DSurface9 *dst_surface)
1888 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1889 struct d3d9_surface *dst_impl = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1890 HRESULT hr = D3DERR_INVALIDCALL;
1892 TRACE("iface %p, swapchain %u, dst_surface %p.\n", iface, swapchain, dst_surface);
1894 wined3d_mutex_lock();
1895 if (swapchain < device->implicit_swapchain_count)
1896 hr = wined3d_swapchain_get_front_buffer_data(device->implicit_swapchains[swapchain],
1897 dst_impl->wined3d_texture, dst_impl->sub_resource_idx);
1898 wined3d_mutex_unlock();
1900 return hr;
1903 static HRESULT WINAPI d3d9_device_StretchRect(IDirect3DDevice9Ex *iface, IDirect3DSurface9 *src_surface,
1904 const RECT *src_rect, IDirect3DSurface9 *dst_surface, const RECT *dst_rect, D3DTEXTUREFILTERTYPE filter)
1906 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1907 struct d3d9_surface *src = unsafe_impl_from_IDirect3DSurface9(src_surface);
1908 struct d3d9_surface *dst = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1909 struct wined3d_sub_resource_desc src_desc, dst_desc;
1910 HRESULT hr = D3DERR_INVALIDCALL;
1911 RECT d, s;
1913 TRACE("iface %p, src_surface %p, src_rect %s, dst_surface %p, dst_rect %s, filter %#x.\n",
1914 iface, src_surface, wine_dbgstr_rect(src_rect), dst_surface, wine_dbgstr_rect(dst_rect), filter);
1916 wined3d_mutex_lock();
1917 wined3d_texture_get_sub_resource_desc(dst->wined3d_texture, dst->sub_resource_idx, &dst_desc);
1918 if (!dst_rect)
1920 SetRect(&d, 0, 0, dst_desc.width, dst_desc.height);
1921 dst_rect = &d;
1924 wined3d_texture_get_sub_resource_desc(src->wined3d_texture, src->sub_resource_idx, &src_desc);
1925 if (!src_rect)
1927 SetRect(&s, 0, 0, src_desc.width, src_desc.height);
1928 src_rect = &s;
1931 if (dst_desc.access & WINED3D_RESOURCE_ACCESS_CPU)
1933 WARN("Destination resource is not in DEFAULT pool.\n");
1934 goto done;
1936 if (src_desc.access & WINED3D_RESOURCE_ACCESS_CPU)
1938 WARN("Source resource is not in DEFAULT pool.\n");
1939 goto done;
1942 if (dst->texture && !(dst_desc.bind_flags & (WINED3D_BIND_RENDER_TARGET | WINED3D_BIND_DEPTH_STENCIL)))
1944 WARN("Destination is a regular texture.\n");
1945 goto done;
1948 if (src_desc.bind_flags & WINED3D_BIND_DEPTH_STENCIL)
1950 if (device->in_scene)
1952 WARN("Rejecting depth / stencil blit while in scene.\n");
1953 goto done;
1956 if (src_rect->left || src_rect->top || src_rect->right != src_desc.width
1957 || src_rect->bottom != src_desc.height)
1959 WARN("Rejecting depth / stencil blit with invalid source rect %s.\n",
1960 wine_dbgstr_rect(src_rect));
1961 goto done;
1964 if (dst_rect->left || dst_rect->top || dst_rect->right != dst_desc.width
1965 || dst_rect->bottom != dst_desc.height)
1967 WARN("Rejecting depth / stencil blit with invalid destination rect %s.\n",
1968 wine_dbgstr_rect(dst_rect));
1969 goto done;
1972 if (src_desc.width != dst_desc.width || src_desc.height != dst_desc.height)
1974 WARN("Rejecting depth / stencil blit with mismatched surface sizes.\n");
1975 goto done;
1979 hr = wined3d_device_context_blt(device->immediate_context, dst->wined3d_texture,
1980 dst->sub_resource_idx, dst_rect, src->wined3d_texture, src->sub_resource_idx,
1981 src_rect, 0, NULL, wined3d_texture_filter_type_from_d3d(filter));
1982 if (hr == WINEDDERR_INVALIDRECT)
1983 hr = D3DERR_INVALIDCALL;
1984 if (SUCCEEDED(hr) && dst->texture)
1985 d3d9_texture_flag_auto_gen_mipmap(dst->texture);
1987 done:
1988 wined3d_mutex_unlock();
1989 return hr;
1992 static HRESULT WINAPI d3d9_device_ColorFill(IDirect3DDevice9Ex *iface,
1993 IDirect3DSurface9 *surface, const RECT *rect, D3DCOLOR color)
1995 const struct wined3d_color c =
1997 ((color >> 16) & 0xff) / 255.0f,
1998 ((color >> 8) & 0xff) / 255.0f,
1999 (color & 0xff) / 255.0f,
2000 ((color >> 24) & 0xff) / 255.0f,
2002 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2003 struct d3d9_surface *surface_impl = unsafe_impl_from_IDirect3DSurface9(surface);
2004 struct wined3d_sub_resource_desc desc;
2005 struct wined3d_rendertarget_view *rtv;
2006 HRESULT hr;
2008 TRACE("iface %p, surface %p, rect %p, color 0x%08lx.\n", iface, surface, rect, color);
2010 if (!surface)
2011 return D3DERR_INVALIDCALL;
2013 wined3d_mutex_lock();
2015 if (FAILED(wined3d_texture_get_sub_resource_desc(surface_impl->wined3d_texture,
2016 surface_impl->sub_resource_idx, &desc)))
2018 wined3d_mutex_unlock();
2019 return D3DERR_INVALIDCALL;
2022 if (desc.access & WINED3D_RESOURCE_ACCESS_CPU)
2024 wined3d_mutex_unlock();
2025 WARN("Colour fills are not allowed on surfaces with resource access %#x.\n", desc.access);
2026 return D3DERR_INVALIDCALL;
2028 if ((desc.bind_flags & (WINED3D_BIND_RENDER_TARGET | WINED3D_BIND_SHADER_RESOURCE)) == WINED3D_BIND_SHADER_RESOURCE)
2030 wined3d_mutex_unlock();
2031 WARN("Colorfill is not allowed on non-RT textures, returning D3DERR_INVALIDCALL.\n");
2032 return D3DERR_INVALIDCALL;
2034 if (desc.bind_flags & WINED3D_BIND_DEPTH_STENCIL)
2036 wined3d_mutex_unlock();
2037 WARN("Colorfill is not allowed on depth stencil surfaces, returning D3DERR_INVALIDCALL.\n");
2038 return D3DERR_INVALIDCALL;
2041 wined3d_device_apply_stateblock(device->wined3d_device, device->state);
2042 rtv = d3d9_surface_acquire_rendertarget_view(surface_impl);
2043 hr = wined3d_device_context_clear_rendertarget_view(device->immediate_context,
2044 rtv, rect, WINED3DCLEAR_TARGET, &c, 0.0f, 0);
2045 d3d9_surface_release_rendertarget_view(surface_impl, rtv);
2046 if (SUCCEEDED(hr) && surface_impl->texture)
2047 d3d9_texture_flag_auto_gen_mipmap(surface_impl->texture);
2049 wined3d_mutex_unlock();
2051 return hr;
2054 static HRESULT WINAPI d3d9_device_CreateOffscreenPlainSurface(IDirect3DDevice9Ex *iface,
2055 UINT width, UINT height, D3DFORMAT format, D3DPOOL pool, IDirect3DSurface9 **surface,
2056 HANDLE *shared_handle)
2058 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2059 unsigned int usage, access;
2060 void *user_mem = NULL;
2062 TRACE("iface %p, width %u, height %u, format %#x, pool %#x, surface %p, shared_handle %p.\n",
2063 iface, width, height, format, pool, surface, shared_handle);
2065 *surface = NULL;
2066 if (pool == D3DPOOL_MANAGED)
2068 WARN("Attempting to create a managed offscreen plain surface.\n");
2069 return D3DERR_INVALIDCALL;
2072 if (shared_handle)
2074 if (!device->d3d_parent->extended)
2076 WARN("Trying to create a shared or user memory surface on a non-ex device.\n");
2077 return E_NOTIMPL;
2080 if (pool == D3DPOOL_SYSTEMMEM)
2081 user_mem = *shared_handle;
2082 else
2084 if (pool != D3DPOOL_DEFAULT)
2086 WARN("Trying to create a shared surface in pool %#x.\n", pool);
2087 return D3DERR_INVALIDCALL;
2089 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
2093 usage = wined3d_usage_from_d3d(pool, 0);
2094 access = wined3daccess_from_d3dpool(pool, 0)
2095 | WINED3D_RESOURCE_ACCESS_MAP_R | WINED3D_RESOURCE_ACCESS_MAP_W;
2097 if (!device->d3d_parent->extended)
2098 usage |= WINED3DUSAGE_VIDMEM_ACCOUNTING;
2100 return d3d9_device_create_surface(device, 0, wined3dformat_from_d3dformat(format),
2101 WINED3D_MULTISAMPLE_NONE, 0, usage, 0, access, width, height, user_mem, surface);
2104 static HRESULT WINAPI d3d9_device_SetRenderTarget(IDirect3DDevice9Ex *iface, DWORD idx, IDirect3DSurface9 *surface)
2106 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2107 struct d3d9_surface *surface_impl = unsafe_impl_from_IDirect3DSurface9(surface);
2108 struct wined3d_rendertarget_view *rtv;
2109 HRESULT hr;
2111 TRACE("iface %p, idx %lu, surface %p.\n", iface, idx, surface);
2113 if (idx >= D3D_MAX_SIMULTANEOUS_RENDERTARGETS)
2115 WARN("Invalid index %lu specified.\n", idx);
2116 return D3DERR_INVALIDCALL;
2119 if (!idx && !surface_impl)
2121 WARN("Trying to set render target 0 to NULL.\n");
2122 return D3DERR_INVALIDCALL;
2125 if (surface_impl && d3d9_surface_get_device(surface_impl) != device)
2127 WARN("Render target surface does not match device.\n");
2128 return D3DERR_INVALIDCALL;
2131 wined3d_mutex_lock();
2132 rtv = surface_impl ? d3d9_surface_acquire_rendertarget_view(surface_impl) : NULL;
2133 hr = wined3d_device_context_set_rendertarget_views(device->immediate_context, idx, 1, &rtv, TRUE);
2134 d3d9_surface_release_rendertarget_view(surface_impl, rtv);
2135 if (SUCCEEDED(hr))
2137 if (!idx)
2138 device_reset_viewport_state(device);
2139 device->render_targets[idx] = surface_impl;
2141 wined3d_mutex_unlock();
2143 return hr;
2146 static HRESULT WINAPI d3d9_device_GetRenderTarget(IDirect3DDevice9Ex *iface, DWORD idx, IDirect3DSurface9 **surface)
2148 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2149 struct wined3d_rendertarget_view *wined3d_rtv;
2150 struct d3d9_surface *surface_impl;
2151 HRESULT hr = D3D_OK;
2153 TRACE("iface %p, idx %lu, surface %p.\n", iface, idx, surface);
2155 if (!surface)
2156 return D3DERR_INVALIDCALL;
2158 if (idx >= D3D_MAX_SIMULTANEOUS_RENDERTARGETS)
2160 WARN("Invalid index %lu specified.\n", idx);
2161 return D3DERR_INVALIDCALL;
2164 wined3d_mutex_lock();
2165 if ((wined3d_rtv = wined3d_device_context_get_rendertarget_view(device->immediate_context, idx)))
2167 /* We want the sub resource parent here, since the view itself may be
2168 * internal to wined3d and may not have a parent. */
2169 surface_impl = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_rtv);
2170 *surface = &surface_impl->IDirect3DSurface9_iface;
2171 IDirect3DSurface9_AddRef(*surface);
2173 else
2175 hr = D3DERR_NOTFOUND;
2176 *surface = NULL;
2178 wined3d_mutex_unlock();
2180 return hr;
2183 static HRESULT WINAPI d3d9_device_SetDepthStencilSurface(IDirect3DDevice9Ex *iface, IDirect3DSurface9 *depth_stencil)
2185 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2186 struct d3d9_surface *ds_impl = unsafe_impl_from_IDirect3DSurface9(depth_stencil);
2187 struct wined3d_rendertarget_view *rtv;
2188 HRESULT hr;
2190 TRACE("iface %p, depth_stencil %p.\n", iface, depth_stencil);
2192 wined3d_mutex_lock();
2193 rtv = ds_impl ? d3d9_surface_acquire_rendertarget_view(ds_impl) : NULL;
2194 hr = wined3d_device_context_set_depth_stencil_view(device->immediate_context, rtv);
2195 d3d9_surface_release_rendertarget_view(ds_impl, rtv);
2196 wined3d_mutex_unlock();
2198 return hr;
2201 static HRESULT WINAPI d3d9_device_GetDepthStencilSurface(IDirect3DDevice9Ex *iface, IDirect3DSurface9 **depth_stencil)
2203 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2204 struct wined3d_rendertarget_view *wined3d_dsv;
2205 struct d3d9_surface *surface_impl;
2206 HRESULT hr = D3D_OK;
2208 TRACE("iface %p, depth_stencil %p.\n", iface, depth_stencil);
2210 if (!depth_stencil)
2211 return D3DERR_INVALIDCALL;
2213 wined3d_mutex_lock();
2214 if ((wined3d_dsv = wined3d_device_context_get_depth_stencil_view(device->immediate_context)))
2216 /* We want the sub resource parent here, since the view itself may be
2217 * internal to wined3d and may not have a parent. */
2218 surface_impl = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_dsv);
2219 *depth_stencil = &surface_impl->IDirect3DSurface9_iface;
2220 IDirect3DSurface9_AddRef(*depth_stencil);
2222 else
2224 hr = D3DERR_NOTFOUND;
2225 *depth_stencil = NULL;
2227 wined3d_mutex_unlock();
2229 return hr;
2232 static HRESULT WINAPI d3d9_device_BeginScene(IDirect3DDevice9Ex *iface)
2234 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2235 HRESULT hr;
2237 TRACE("iface %p.\n", iface);
2239 wined3d_mutex_lock();
2240 if (SUCCEEDED(hr = wined3d_device_begin_scene(device->wined3d_device)))
2241 device->in_scene = TRUE;
2242 wined3d_mutex_unlock();
2244 return hr;
2247 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_EndScene(IDirect3DDevice9Ex *iface)
2249 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2250 HRESULT hr;
2252 TRACE("iface %p.\n", iface);
2254 wined3d_mutex_lock();
2255 if (SUCCEEDED(hr = wined3d_device_end_scene(device->wined3d_device)))
2256 device->in_scene = FALSE;
2257 wined3d_mutex_unlock();
2259 return hr;
2262 static void d3d9_rts_flag_auto_gen_mipmap(struct d3d9_device *device)
2264 unsigned int i;
2266 for (i = 0; i < ARRAY_SIZE(device->render_targets); ++i)
2268 struct d3d9_surface *surface = device->render_targets[i];
2270 if (surface && surface->texture)
2271 d3d9_texture_flag_auto_gen_mipmap(surface->texture);
2275 static HRESULT WINAPI d3d9_device_Clear(IDirect3DDevice9Ex *iface, DWORD rect_count,
2276 const D3DRECT *rects, DWORD flags, D3DCOLOR color, float z, DWORD stencil)
2278 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2279 struct wined3d_color c;
2280 HRESULT hr;
2282 TRACE("iface %p, rect_count %lu, rects %p, flags %#lx, color 0x%08lx, z %.8e, stencil %lu.\n",
2283 iface, rect_count, rects, flags, color, z, stencil);
2285 if (rect_count && !rects)
2287 WARN("count %lu with NULL rects.\n", rect_count);
2288 rect_count = 0;
2291 wined3d_color_from_d3dcolor(&c, color);
2292 wined3d_mutex_lock();
2293 wined3d_device_apply_stateblock(device->wined3d_device, device->state);
2294 hr = wined3d_device_clear(device->wined3d_device, rect_count, (const RECT *)rects, flags, &c, z, stencil);
2295 if (SUCCEEDED(hr))
2296 d3d9_rts_flag_auto_gen_mipmap(device);
2297 wined3d_mutex_unlock();
2299 return hr;
2302 static HRESULT WINAPI d3d9_device_SetTransform(IDirect3DDevice9Ex *iface,
2303 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
2305 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2307 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
2309 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
2310 wined3d_mutex_lock();
2311 wined3d_stateblock_set_transform(device->update_state,
2312 wined3d_transform_state_from_d3d(state), (const struct wined3d_matrix *)matrix);
2313 wined3d_mutex_unlock();
2315 return D3D_OK;
2318 static HRESULT WINAPI d3d9_device_GetTransform(IDirect3DDevice9Ex *iface,
2319 D3DTRANSFORMSTATETYPE state, D3DMATRIX *matrix)
2321 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2323 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
2325 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
2326 wined3d_mutex_lock();
2327 memcpy(matrix, &device->stateblock_state->transforms[state], sizeof(*matrix));
2328 wined3d_mutex_unlock();
2330 return D3D_OK;
2333 static HRESULT WINAPI d3d9_device_MultiplyTransform(IDirect3DDevice9Ex *iface,
2334 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
2336 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2338 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
2340 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
2341 wined3d_mutex_lock();
2342 wined3d_stateblock_multiply_transform(device->state,
2343 wined3d_transform_state_from_d3d(state), (const struct wined3d_matrix *)matrix);
2344 wined3d_mutex_unlock();
2346 return D3D_OK;
2349 static HRESULT WINAPI d3d9_device_SetViewport(IDirect3DDevice9Ex *iface, const D3DVIEWPORT9 *viewport)
2351 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2352 struct wined3d_viewport vp;
2354 TRACE("iface %p, viewport %p.\n", iface, viewport);
2356 vp.x = viewport->X;
2357 vp.y = viewport->Y;
2358 vp.width = viewport->Width;
2359 vp.height = viewport->Height;
2360 vp.min_z = viewport->MinZ;
2361 vp.max_z = viewport->MaxZ;
2363 wined3d_mutex_lock();
2364 wined3d_stateblock_set_viewport(device->update_state, &vp);
2365 wined3d_mutex_unlock();
2367 return D3D_OK;
2370 static HRESULT WINAPI d3d9_device_GetViewport(IDirect3DDevice9Ex *iface, D3DVIEWPORT9 *viewport)
2372 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2373 struct wined3d_viewport wined3d_viewport;
2375 TRACE("iface %p, viewport %p.\n", iface, viewport);
2377 wined3d_mutex_lock();
2378 wined3d_viewport = device->stateblock_state->viewport;
2379 wined3d_mutex_unlock();
2381 viewport->X = wined3d_viewport.x;
2382 viewport->Y = wined3d_viewport.y;
2383 viewport->Width = wined3d_viewport.width;
2384 viewport->Height = wined3d_viewport.height;
2385 viewport->MinZ = wined3d_viewport.min_z;
2386 viewport->MaxZ = wined3d_viewport.max_z;
2388 return D3D_OK;
2391 static HRESULT WINAPI d3d9_device_SetMaterial(IDirect3DDevice9Ex *iface, const D3DMATERIAL9 *material)
2393 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2395 TRACE("iface %p, material %p.\n", iface, material);
2397 /* Note: D3DMATERIAL9 is compatible with struct wined3d_material. */
2398 wined3d_mutex_lock();
2399 wined3d_stateblock_set_material(device->update_state, (const struct wined3d_material *)material);
2400 wined3d_mutex_unlock();
2402 return D3D_OK;
2405 static HRESULT WINAPI d3d9_device_GetMaterial(IDirect3DDevice9Ex *iface, D3DMATERIAL9 *material)
2407 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2409 TRACE("iface %p, material %p.\n", iface, material);
2411 /* Note: D3DMATERIAL9 is compatible with struct wined3d_material. */
2412 wined3d_mutex_lock();
2413 memcpy(material, &device->stateblock_state->material, sizeof(*material));
2414 wined3d_mutex_unlock();
2416 return D3D_OK;
2419 static HRESULT WINAPI d3d9_device_SetLight(IDirect3DDevice9Ex *iface, DWORD index, const D3DLIGHT9 *light)
2421 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2422 HRESULT hr;
2424 TRACE("iface %p, index %lu, light %p.\n", iface, index, light);
2426 /* Note: D3DLIGHT9 is compatible with struct wined3d_light. */
2427 wined3d_mutex_lock();
2428 hr = wined3d_stateblock_set_light(device->update_state, index, (const struct wined3d_light *)light);
2429 wined3d_mutex_unlock();
2431 return hr;
2434 static HRESULT WINAPI d3d9_device_GetLight(IDirect3DDevice9Ex *iface, DWORD index, D3DLIGHT9 *light)
2436 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2437 BOOL enabled;
2438 HRESULT hr;
2440 TRACE("iface %p, index %lu, light %p.\n", iface, index, light);
2442 /* Note: D3DLIGHT9 is compatible with struct wined3d_light. */
2443 wined3d_mutex_lock();
2444 hr = wined3d_stateblock_get_light(device->state, index, (struct wined3d_light *)light, &enabled);
2445 wined3d_mutex_unlock();
2447 return hr;
2450 static HRESULT WINAPI d3d9_device_LightEnable(IDirect3DDevice9Ex *iface, DWORD index, BOOL enable)
2452 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2453 HRESULT hr;
2455 TRACE("iface %p, index %lu, enable %#x.\n", iface, index, enable);
2457 wined3d_mutex_lock();
2458 hr = wined3d_stateblock_set_light_enable(device->update_state, index, enable);
2459 wined3d_mutex_unlock();
2461 return hr;
2464 static HRESULT WINAPI d3d9_device_GetLightEnable(IDirect3DDevice9Ex *iface, DWORD index, BOOL *enabled)
2466 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2467 struct wined3d_light light;
2468 HRESULT hr;
2470 TRACE("iface %p, index %lu, enabled %p.\n", iface, index, enabled);
2472 wined3d_mutex_lock();
2473 hr = wined3d_stateblock_get_light(device->state, index, &light, enabled);
2474 wined3d_mutex_unlock();
2476 return hr;
2479 static HRESULT WINAPI d3d9_device_SetClipPlane(IDirect3DDevice9Ex *iface, DWORD index, const float *plane)
2481 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2482 HRESULT hr;
2484 TRACE("iface %p, index %lu, plane %p.\n", iface, index, plane);
2486 index = min(index, device->max_user_clip_planes - 1);
2488 wined3d_mutex_lock();
2489 hr = wined3d_stateblock_set_clip_plane(device->update_state, index, (const struct wined3d_vec4 *)plane);
2490 wined3d_mutex_unlock();
2492 return hr;
2495 static HRESULT WINAPI d3d9_device_GetClipPlane(IDirect3DDevice9Ex *iface, DWORD index, float *plane)
2497 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2499 TRACE("iface %p, index %lu, plane %p.\n", iface, index, plane);
2501 index = min(index, device->max_user_clip_planes - 1);
2503 wined3d_mutex_lock();
2504 memcpy(plane, &device->stateblock_state->clip_planes[index], sizeof(struct wined3d_vec4));
2505 wined3d_mutex_unlock();
2507 return D3D_OK;
2510 static void resolve_depth_buffer(struct d3d9_device *device)
2512 const struct wined3d_stateblock_state *state = device->stateblock_state;
2513 struct wined3d_rendertarget_view *wined3d_dsv;
2514 struct wined3d_resource *dst_resource;
2515 struct wined3d_texture *dst_texture;
2516 struct wined3d_resource_desc desc;
2517 struct d3d9_surface *d3d9_dsv;
2519 if (!(dst_texture = state->textures[0]))
2520 return;
2521 dst_resource = wined3d_texture_get_resource(dst_texture);
2522 wined3d_resource_get_desc(dst_resource, &desc);
2523 if (desc.format != WINED3DFMT_D24_UNORM_S8_UINT
2524 && desc.format != WINED3DFMT_X8D24_UNORM
2525 && desc.format != WINED3DFMT_DF16
2526 && desc.format != WINED3DFMT_DF24
2527 && desc.format != WINED3DFMT_INTZ)
2528 return;
2530 if (!(wined3d_dsv = wined3d_device_context_get_depth_stencil_view(device->immediate_context)))
2531 return;
2532 d3d9_dsv = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_dsv);
2534 wined3d_device_context_resolve_sub_resource(device->immediate_context, dst_resource, 0,
2535 wined3d_rendertarget_view_get_resource(wined3d_dsv), d3d9_dsv->sub_resource_idx, desc.format);
2538 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_SetRenderState(IDirect3DDevice9Ex *iface,
2539 D3DRENDERSTATETYPE state, DWORD value)
2541 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2543 TRACE("iface %p, state %#x, value %#lx.\n", iface, state, value);
2545 wined3d_mutex_lock();
2546 wined3d_stateblock_set_render_state(device->update_state, wined3d_render_state_from_d3d(state), value);
2547 if (state == D3DRS_POINTSIZE && value == D3D9_RESZ_CODE)
2548 resolve_depth_buffer(device);
2549 wined3d_mutex_unlock();
2551 return D3D_OK;
2554 static HRESULT WINAPI d3d9_device_GetRenderState(IDirect3DDevice9Ex *iface,
2555 D3DRENDERSTATETYPE state, DWORD *value)
2557 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2558 const struct wined3d_stateblock_state *device_state;
2560 TRACE("iface %p, state %#x, value %p.\n", iface, state, value);
2562 wined3d_mutex_lock();
2563 device_state = device->stateblock_state;
2564 *value = device_state->rs[state];
2565 wined3d_mutex_unlock();
2567 return D3D_OK;
2570 static HRESULT WINAPI d3d9_device_CreateStateBlock(IDirect3DDevice9Ex *iface,
2571 D3DSTATEBLOCKTYPE type, IDirect3DStateBlock9 **stateblock)
2573 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2574 struct d3d9_stateblock *object;
2575 HRESULT hr;
2577 TRACE("iface %p, type %#x, stateblock %p.\n", iface, type, stateblock);
2579 if (type != D3DSBT_ALL && type != D3DSBT_PIXELSTATE && type != D3DSBT_VERTEXSTATE)
2581 WARN("Unexpected stateblock type, returning D3DERR_INVALIDCALL.\n");
2582 return D3DERR_INVALIDCALL;
2585 wined3d_mutex_lock();
2586 if (device->recording)
2588 wined3d_mutex_unlock();
2589 WARN("Trying to create a stateblock while recording, returning D3DERR_INVALIDCALL.\n");
2590 return D3DERR_INVALIDCALL;
2592 wined3d_mutex_unlock();
2594 if (!(object = calloc(1, sizeof(*object))))
2595 return E_OUTOFMEMORY;
2597 hr = stateblock_init(object, device, type, NULL);
2598 if (FAILED(hr))
2600 WARN("Failed to initialize stateblock, hr %#lx.\n", hr);
2601 free(object);
2602 return hr;
2605 TRACE("Created stateblock %p.\n", object);
2606 *stateblock = &object->IDirect3DStateBlock9_iface;
2608 return D3D_OK;
2611 static HRESULT WINAPI d3d9_device_BeginStateBlock(IDirect3DDevice9Ex *iface)
2613 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2614 struct wined3d_stateblock *stateblock;
2615 HRESULT hr;
2617 TRACE("iface %p.\n", iface);
2619 wined3d_mutex_lock();
2620 if (device->recording)
2622 wined3d_mutex_unlock();
2623 WARN("Trying to begin a stateblock while recording, returning D3DERR_INVALIDCALL.\n");
2624 return D3DERR_INVALIDCALL;
2627 if (SUCCEEDED(hr = wined3d_stateblock_create(device->wined3d_device, device->state, WINED3D_SBT_RECORDED, &stateblock)))
2628 device->update_state = device->recording = stateblock;
2629 wined3d_mutex_unlock();
2631 return hr;
2634 static HRESULT WINAPI d3d9_device_EndStateBlock(IDirect3DDevice9Ex *iface, IDirect3DStateBlock9 **stateblock)
2636 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2637 struct wined3d_stateblock *wined3d_stateblock;
2638 struct d3d9_stateblock *object;
2639 HRESULT hr;
2641 TRACE("iface %p, stateblock %p.\n", iface, stateblock);
2643 wined3d_mutex_lock();
2644 if (!device->recording)
2646 wined3d_mutex_unlock();
2647 WARN("Trying to end a stateblock, but no stateblock is being recorded.\n");
2648 return D3DERR_INVALIDCALL;
2650 wined3d_stateblock = device->recording;
2651 wined3d_stateblock_init_contained_states(wined3d_stateblock);
2652 device->recording = NULL;
2653 device->update_state = device->state;
2654 wined3d_mutex_unlock();
2656 if (!(object = calloc(1, sizeof(*object))))
2658 wined3d_stateblock_decref(wined3d_stateblock);
2659 return E_OUTOFMEMORY;
2662 hr = stateblock_init(object, device, 0, wined3d_stateblock);
2663 if (FAILED(hr))
2665 WARN("Failed to initialize stateblock, hr %#lx.\n", hr);
2666 wined3d_stateblock_decref(wined3d_stateblock);
2667 free(object);
2668 return hr;
2671 TRACE("Created stateblock %p.\n", object);
2672 *stateblock = &object->IDirect3DStateBlock9_iface;
2674 return D3D_OK;
2677 static HRESULT WINAPI d3d9_device_SetClipStatus(IDirect3DDevice9Ex *iface, const D3DCLIPSTATUS9 *clip_status)
2679 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2680 HRESULT hr;
2682 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
2684 wined3d_mutex_lock();
2685 hr = wined3d_device_set_clip_status(device->wined3d_device, (const struct wined3d_clip_status *)clip_status);
2686 wined3d_mutex_unlock();
2688 return hr;
2691 static HRESULT WINAPI d3d9_device_GetClipStatus(IDirect3DDevice9Ex *iface, D3DCLIPSTATUS9 *clip_status)
2693 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2694 HRESULT hr;
2696 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
2698 wined3d_mutex_lock();
2699 hr = wined3d_device_get_clip_status(device->wined3d_device, (struct wined3d_clip_status *)clip_status);
2700 wined3d_mutex_unlock();
2702 return hr;
2705 static HRESULT WINAPI d3d9_device_GetTexture(IDirect3DDevice9Ex *iface, DWORD stage, IDirect3DBaseTexture9 **texture)
2707 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2708 struct wined3d_texture *wined3d_texture = NULL;
2709 const struct wined3d_stateblock_state *state;
2710 struct d3d9_texture *texture_impl;
2712 TRACE("iface %p, stage %lu, texture %p.\n", iface, stage, texture);
2714 if (!texture)
2715 return D3DERR_INVALIDCALL;
2717 if (stage >= D3DVERTEXTEXTURESAMPLER0 && stage <= D3DVERTEXTEXTURESAMPLER3)
2718 stage -= D3DVERTEXTEXTURESAMPLER0 - WINED3D_VERTEX_SAMPLER_OFFSET;
2720 if (stage >= ARRAY_SIZE(state->textures))
2722 WARN("Ignoring invalid stage %lu.\n", stage);
2723 *texture = NULL;
2724 return D3D_OK;
2727 wined3d_mutex_lock();
2728 state = device->stateblock_state;
2729 if ((wined3d_texture = state->textures[stage]))
2731 texture_impl = wined3d_texture_get_parent(wined3d_texture);
2732 *texture = &texture_impl->IDirect3DBaseTexture9_iface;
2733 IDirect3DBaseTexture9_AddRef(*texture);
2735 else
2737 *texture = NULL;
2739 wined3d_mutex_unlock();
2741 return D3D_OK;
2744 static HRESULT WINAPI d3d9_device_SetTexture(IDirect3DDevice9Ex *iface, DWORD stage, IDirect3DBaseTexture9 *texture)
2746 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2747 struct d3d9_texture *texture_impl;
2749 TRACE("iface %p, stage %lu, texture %p.\n", iface, stage, texture);
2751 texture_impl = unsafe_impl_from_IDirect3DBaseTexture9(texture);
2753 if (stage >= D3DVERTEXTEXTURESAMPLER0 && stage <= D3DVERTEXTEXTURESAMPLER3)
2754 stage -= D3DVERTEXTEXTURESAMPLER0 - WINED3D_VERTEX_SAMPLER_OFFSET;
2756 wined3d_mutex_lock();
2757 wined3d_stateblock_set_texture(device->update_state, stage,
2758 texture_impl ? d3d9_texture_get_draw_texture(texture_impl) : NULL);
2759 if (!device->recording)
2761 if (stage < D3D9_MAX_TEXTURE_UNITS)
2763 if (texture_impl && texture_impl->usage & D3DUSAGE_AUTOGENMIPMAP)
2764 device->auto_mipmaps |= 1u << stage;
2765 else
2766 device->auto_mipmaps &= ~(1u << stage);
2769 wined3d_mutex_unlock();
2771 return D3D_OK;
2774 static const enum wined3d_texture_stage_state tss_lookup[] =
2776 WINED3D_TSS_INVALID, /* 0, unused */
2777 WINED3D_TSS_COLOR_OP, /* 1, D3DTSS_COLOROP */
2778 WINED3D_TSS_COLOR_ARG1, /* 2, D3DTSS_COLORARG1 */
2779 WINED3D_TSS_COLOR_ARG2, /* 3, D3DTSS_COLORARG2 */
2780 WINED3D_TSS_ALPHA_OP, /* 4, D3DTSS_ALPHAOP */
2781 WINED3D_TSS_ALPHA_ARG1, /* 5, D3DTSS_ALPHAARG1 */
2782 WINED3D_TSS_ALPHA_ARG2, /* 6, D3DTSS_ALPHAARG2 */
2783 WINED3D_TSS_BUMPENV_MAT00, /* 7, D3DTSS_BUMPENVMAT00 */
2784 WINED3D_TSS_BUMPENV_MAT01, /* 8, D3DTSS_BUMPENVMAT01 */
2785 WINED3D_TSS_BUMPENV_MAT10, /* 9, D3DTSS_BUMPENVMAT10 */
2786 WINED3D_TSS_BUMPENV_MAT11, /* 10, D3DTSS_BUMPENVMAT11 */
2787 WINED3D_TSS_TEXCOORD_INDEX, /* 11, D3DTSS_TEXCOORDINDEX */
2788 WINED3D_TSS_INVALID, /* 12, unused */
2789 WINED3D_TSS_INVALID, /* 13, unused */
2790 WINED3D_TSS_INVALID, /* 14, unused */
2791 WINED3D_TSS_INVALID, /* 15, unused */
2792 WINED3D_TSS_INVALID, /* 16, unused */
2793 WINED3D_TSS_INVALID, /* 17, unused */
2794 WINED3D_TSS_INVALID, /* 18, unused */
2795 WINED3D_TSS_INVALID, /* 19, unused */
2796 WINED3D_TSS_INVALID, /* 20, unused */
2797 WINED3D_TSS_INVALID, /* 21, unused */
2798 WINED3D_TSS_BUMPENV_LSCALE, /* 22, D3DTSS_BUMPENVLSCALE */
2799 WINED3D_TSS_BUMPENV_LOFFSET, /* 23, D3DTSS_BUMPENVLOFFSET */
2800 WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS, /* 24, D3DTSS_TEXTURETRANSFORMFLAGS */
2801 WINED3D_TSS_INVALID, /* 25, unused */
2802 WINED3D_TSS_COLOR_ARG0, /* 26, D3DTSS_COLORARG0 */
2803 WINED3D_TSS_ALPHA_ARG0, /* 27, D3DTSS_ALPHAARG0 */
2804 WINED3D_TSS_RESULT_ARG, /* 28, D3DTSS_RESULTARG */
2805 WINED3D_TSS_INVALID, /* 29, unused */
2806 WINED3D_TSS_INVALID, /* 30, unused */
2807 WINED3D_TSS_INVALID, /* 31, unused */
2808 WINED3D_TSS_CONSTANT, /* 32, D3DTSS_CONSTANT */
2811 static HRESULT WINAPI d3d9_device_GetTextureStageState(IDirect3DDevice9Ex *iface,
2812 DWORD stage, D3DTEXTURESTAGESTATETYPE state, DWORD *value)
2814 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2816 TRACE("iface %p, stage %lu, state %#x, value %p.\n", iface, stage, state, value);
2818 if (state >= ARRAY_SIZE(tss_lookup) || tss_lookup[state] == WINED3D_TSS_INVALID)
2820 WARN("Invalid state %#x passed.\n", state);
2821 return D3D_OK;
2824 wined3d_mutex_lock();
2825 *value = device->stateblock_state->texture_states[stage][tss_lookup[state]];
2826 wined3d_mutex_unlock();
2828 return D3D_OK;
2831 static HRESULT WINAPI d3d9_device_SetTextureStageState(IDirect3DDevice9Ex *iface,
2832 DWORD stage, D3DTEXTURESTAGESTATETYPE state, DWORD value)
2834 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2836 TRACE("iface %p, stage %lu, state %#x, value %#lx.\n", iface, stage, state, value);
2838 if (state >= ARRAY_SIZE(tss_lookup))
2840 WARN("Invalid state %#x passed.\n", state);
2841 return D3D_OK;
2844 wined3d_mutex_lock();
2845 wined3d_stateblock_set_texture_stage_state(device->update_state, stage, tss_lookup[state], value);
2846 wined3d_mutex_unlock();
2848 return D3D_OK;
2851 static HRESULT WINAPI d3d9_device_GetSamplerState(IDirect3DDevice9Ex *iface,
2852 DWORD sampler_idx, D3DSAMPLERSTATETYPE state, DWORD *value)
2854 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2855 const struct wined3d_stateblock_state *device_state;
2857 TRACE("iface %p, sampler_idx %lu, state %#x, value %p.\n", iface, sampler_idx, state, value);
2859 if (sampler_idx >= D3DVERTEXTEXTURESAMPLER0 && sampler_idx <= D3DVERTEXTEXTURESAMPLER3)
2860 sampler_idx -= D3DVERTEXTEXTURESAMPLER0 - WINED3D_VERTEX_SAMPLER_OFFSET;
2862 if (sampler_idx >= ARRAY_SIZE(device_state->sampler_states))
2864 WARN("Invalid sampler %lu.\n", sampler_idx);
2865 *value = 0;
2866 return D3D_OK;
2869 wined3d_mutex_lock();
2870 device_state = device->stateblock_state;
2871 *value = device_state->sampler_states[sampler_idx][state];
2872 wined3d_mutex_unlock();
2874 return D3D_OK;
2877 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_SetSamplerState(IDirect3DDevice9Ex *iface,
2878 DWORD sampler, D3DSAMPLERSTATETYPE state, DWORD value)
2880 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2882 TRACE("iface %p, sampler %lu, state %#x, value %#lx.\n", iface, sampler, state, value);
2884 if (sampler >= D3DVERTEXTEXTURESAMPLER0 && sampler <= D3DVERTEXTEXTURESAMPLER3)
2885 sampler -= D3DVERTEXTEXTURESAMPLER0 - WINED3D_VERTEX_SAMPLER_OFFSET;
2887 wined3d_mutex_lock();
2888 wined3d_stateblock_set_sampler_state(device->update_state, sampler, wined3d_sampler_state_from_d3d(state), value);
2889 wined3d_mutex_unlock();
2891 return D3D_OK;
2894 static HRESULT WINAPI d3d9_device_ValidateDevice(IDirect3DDevice9Ex *iface, DWORD *pass_count)
2896 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2897 HRESULT hr;
2899 TRACE("iface %p, pass_count %p.\n", iface, pass_count);
2901 wined3d_mutex_lock();
2902 hr = wined3d_device_validate_device(device->wined3d_device, device->stateblock_state, pass_count);
2903 wined3d_mutex_unlock();
2905 return hr;
2908 static HRESULT WINAPI d3d9_device_SetPaletteEntries(IDirect3DDevice9Ex *iface,
2909 UINT palette_idx, const PALETTEENTRY *entries)
2911 WARN("iface %p, palette_idx %u, entries %p unimplemented.\n", iface, palette_idx, entries);
2913 /* The d3d9 palette API is non-functional on Windows. Getters and setters are implemented,
2914 * and some drivers allow the creation of P8 surfaces. These surfaces can be copied to
2915 * other P8 surfaces with StretchRect, but cannot be converted to (A)RGB.
2917 * Some older(dx7) cards may have support for P8 textures, but games cannot rely on this. */
2918 return D3D_OK;
2921 static HRESULT WINAPI d3d9_device_GetPaletteEntries(IDirect3DDevice9Ex *iface,
2922 UINT palette_idx, PALETTEENTRY *entries)
2924 FIXME("iface %p, palette_idx %u, entries %p unimplemented.\n", iface, palette_idx, entries);
2926 return D3DERR_INVALIDCALL;
2929 static HRESULT WINAPI d3d9_device_SetCurrentTexturePalette(IDirect3DDevice9Ex *iface, UINT palette_idx)
2931 WARN("iface %p, palette_idx %u unimplemented.\n", iface, palette_idx);
2933 return D3D_OK;
2936 static HRESULT WINAPI d3d9_device_GetCurrentTexturePalette(IDirect3DDevice9Ex *iface, UINT *palette_idx)
2938 FIXME("iface %p, palette_idx %p.\n", iface, palette_idx);
2940 return D3DERR_INVALIDCALL;
2943 static HRESULT WINAPI d3d9_device_SetScissorRect(IDirect3DDevice9Ex *iface, const RECT *rect)
2945 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2947 TRACE("iface %p, rect %p.\n", iface, rect);
2949 wined3d_mutex_lock();
2950 wined3d_stateblock_set_scissor_rect(device->update_state, rect);
2951 wined3d_mutex_unlock();
2953 return D3D_OK;
2956 static HRESULT WINAPI d3d9_device_GetScissorRect(IDirect3DDevice9Ex *iface, RECT *rect)
2958 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2960 TRACE("iface %p, rect %p.\n", iface, rect);
2962 wined3d_mutex_lock();
2963 *rect = device->stateblock_state->scissor_rect;
2964 wined3d_mutex_unlock();
2966 return D3D_OK;
2969 static HRESULT WINAPI d3d9_device_SetSoftwareVertexProcessing(IDirect3DDevice9Ex *iface, BOOL software)
2971 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2973 TRACE("iface %p, software %#x.\n", iface, software);
2975 wined3d_mutex_lock();
2976 wined3d_device_set_software_vertex_processing(device->wined3d_device, software);
2977 wined3d_mutex_unlock();
2979 return D3D_OK;
2982 static BOOL WINAPI d3d9_device_GetSoftwareVertexProcessing(IDirect3DDevice9Ex *iface)
2984 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2985 BOOL ret;
2987 TRACE("iface %p.\n", iface);
2989 wined3d_mutex_lock();
2990 ret = wined3d_device_get_software_vertex_processing(device->wined3d_device);
2991 wined3d_mutex_unlock();
2993 return ret;
2996 static HRESULT WINAPI d3d9_device_SetNPatchMode(IDirect3DDevice9Ex *iface, float segment_count)
2998 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2999 HRESULT hr;
3001 TRACE("iface %p, segment_count %.8e.\n", iface, segment_count);
3003 wined3d_mutex_lock();
3004 hr = wined3d_device_set_npatch_mode(device->wined3d_device, segment_count);
3005 wined3d_mutex_unlock();
3007 return hr;
3010 static float WINAPI d3d9_device_GetNPatchMode(IDirect3DDevice9Ex *iface)
3012 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3013 float ret;
3015 TRACE("iface %p.\n", iface);
3017 wined3d_mutex_lock();
3018 ret = wined3d_device_get_npatch_mode(device->wined3d_device);
3019 wined3d_mutex_unlock();
3021 return ret;
3024 /* wined3d critical section must be taken by the caller. */
3025 static void d3d9_generate_auto_mipmaps(struct d3d9_device *device)
3027 const struct wined3d_stateblock_state *state = device->stateblock_state;
3028 struct wined3d_texture *texture;
3029 unsigned int i, map;
3031 map = device->auto_mipmaps;
3032 while (map)
3034 i = wined3d_bit_scan(&map);
3035 if ((texture = state->textures[i]))
3036 d3d9_texture_gen_auto_mipmap(wined3d_texture_get_parent(texture));
3040 static void d3d9_device_upload_sysmem_vertex_buffers(struct d3d9_device *device,
3041 int base_vertex, unsigned int start_vertex, unsigned int vertex_count)
3043 const struct wined3d_stateblock_state *state = device->stateblock_state;
3044 struct wined3d_vertex_declaration *wined3d_decl;
3045 struct wined3d_box box = {0, 0, 0, 1, 0, 1};
3046 const struct wined3d_stream_state *stream;
3047 struct d3d9_vertexbuffer *d3d9_buffer;
3048 struct wined3d_resource *dst_resource;
3049 struct d3d9_vertex_declaration *decl;
3050 struct wined3d_buffer *dst_buffer;
3051 struct wined3d_resource_desc desc;
3052 unsigned int stride, map;
3053 HRESULT hr;
3055 if (!device->sysmem_vb)
3056 return;
3057 wined3d_decl = state->vertex_declaration;
3058 if (!wined3d_decl)
3059 return;
3061 if (base_vertex >= 0 || start_vertex >= -base_vertex)
3062 start_vertex += base_vertex;
3063 else
3064 FIXME("System memory vertex data offset is negative.\n");
3066 decl = wined3d_vertex_declaration_get_parent(wined3d_decl);
3067 map = decl->stream_map & device->sysmem_vb;
3068 while (map)
3070 stream = &state->streams[wined3d_bit_scan(&map)];
3071 dst_buffer = stream->buffer;
3072 stride = stream->stride;
3074 d3d9_buffer = wined3d_buffer_get_parent(dst_buffer);
3075 dst_resource = wined3d_buffer_get_resource(dst_buffer);
3076 wined3d_resource_get_desc(dst_resource, &desc);
3077 box.left = stream->offset + start_vertex * stride;
3078 box.right = min(box.left + vertex_count * stride, desc.size);
3079 if (FAILED(hr = wined3d_device_context_copy_sub_resource_region(device->immediate_context,
3080 dst_resource, 0, box.left, 0, 0,
3081 wined3d_buffer_get_resource(d3d9_buffer->wined3d_buffer), 0, &box, 0)))
3082 ERR("Failed to update buffer.\n");
3086 static HRESULT d3d9_device_upload_sysmem_index_buffer(struct d3d9_device *device,
3087 unsigned int *start_idx, unsigned int idx_count)
3089 const struct wined3d_stateblock_state *state = device->stateblock_state;
3090 unsigned int src_offset, idx_size, buffer_size, pos;
3091 struct wined3d_resource_desc resource_desc;
3092 struct wined3d_resource *index_buffer;
3093 struct wined3d_map_desc map_desc;
3094 struct wined3d_box box;
3095 HRESULT hr;
3097 if (!device->sysmem_ib)
3098 return S_OK;
3100 index_buffer = wined3d_buffer_get_resource(state->index_buffer);
3101 wined3d_resource_get_desc(index_buffer, &resource_desc);
3102 idx_size = (state->index_format == WINED3DFMT_R16_UINT) ? 2 : 4;
3104 src_offset = (*start_idx) * idx_size;
3105 buffer_size = min(idx_count * idx_size, resource_desc.size - src_offset);
3107 wined3d_box_set(&box, src_offset, 0, buffer_size, 1, 0, 1);
3108 if (FAILED(hr = wined3d_resource_map(index_buffer, 0, &map_desc, &box, WINED3D_MAP_READ)))
3110 ERR("Failed to map index buffer, hr %#lx.\n", hr);
3111 return hr;
3113 wined3d_streaming_buffer_upload(device->wined3d_device, &device->index_buffer,
3114 map_desc.data, buffer_size, idx_size, &pos);
3115 wined3d_resource_unmap(index_buffer, 0);
3117 wined3d_device_context_set_index_buffer(device->immediate_context,
3118 device->index_buffer.buffer, state->index_format, pos);
3119 *start_idx = 0;
3120 return S_OK;
3123 static void d3d9_device_upload_managed_textures(struct d3d9_device *device)
3125 const struct wined3d_stateblock_state *state = device->stateblock_state;
3126 unsigned int i;
3128 for (i = 0; i < WINED3D_MAX_COMBINED_SAMPLERS; ++i)
3130 struct wined3d_texture *wined3d_texture = state->textures[i];
3131 struct d3d9_texture *d3d9_texture;
3133 if (!wined3d_texture)
3134 continue;
3135 d3d9_texture = wined3d_texture_get_parent(wined3d_texture);
3136 if (d3d9_texture->draw_texture)
3137 wined3d_device_update_texture(device->wined3d_device,
3138 d3d9_texture->wined3d_texture, d3d9_texture->draw_texture);
3142 static HRESULT WINAPI d3d9_device_DrawPrimitive(IDirect3DDevice9Ex *iface,
3143 D3DPRIMITIVETYPE primitive_type, UINT start_vertex, UINT primitive_count)
3145 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3146 unsigned int vertex_count;
3148 TRACE("iface %p, primitive_type %#x, start_vertex %u, primitive_count %u.\n",
3149 iface, primitive_type, start_vertex, primitive_count);
3151 wined3d_mutex_lock();
3152 if (!device->stateblock_state->vertex_declaration)
3154 wined3d_mutex_unlock();
3155 WARN("Called without a valid vertex declaration set.\n");
3156 return D3DERR_INVALIDCALL;
3158 wined3d_device_apply_stateblock(device->wined3d_device, device->state);
3159 vertex_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
3160 d3d9_device_upload_managed_textures(device);
3161 d3d9_device_upload_sysmem_vertex_buffers(device, 0, start_vertex, vertex_count);
3162 d3d9_generate_auto_mipmaps(device);
3163 wined3d_device_context_set_primitive_type(device->immediate_context,
3164 wined3d_primitive_type_from_d3d(primitive_type), 0);
3166 /* Instancing is ignored for non-indexed draws. */
3167 wined3d_device_context_draw(device->immediate_context, start_vertex, vertex_count, 0, 1);
3169 d3d9_rts_flag_auto_gen_mipmap(device);
3170 wined3d_mutex_unlock();
3172 return D3D_OK;
3175 static HRESULT WINAPI d3d9_device_DrawIndexedPrimitive(IDirect3DDevice9Ex *iface,
3176 D3DPRIMITIVETYPE primitive_type, INT base_vertex_idx, UINT min_vertex_idx,
3177 UINT vertex_count, UINT start_idx, UINT primitive_count)
3179 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3180 unsigned int index_count;
3182 TRACE("iface %p, primitive_type %#x, base_vertex_idx %u, min_vertex_idx %u, "
3183 "vertex_count %u, start_idx %u, primitive_count %u.\n",
3184 iface, primitive_type, base_vertex_idx, min_vertex_idx,
3185 vertex_count, start_idx, primitive_count);
3187 wined3d_mutex_lock();
3188 if (!device->stateblock_state->vertex_declaration)
3190 wined3d_mutex_unlock();
3191 WARN("Called without a valid vertex declaration set.\n");
3192 return D3DERR_INVALIDCALL;
3194 if (!device->stateblock_state->index_buffer)
3196 wined3d_mutex_unlock();
3197 WARN("Called without a valid index buffer set.\n");
3198 return D3DERR_INVALIDCALL;
3200 index_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
3201 d3d9_device_upload_managed_textures(device);
3202 d3d9_device_upload_sysmem_vertex_buffers(device, base_vertex_idx, min_vertex_idx, vertex_count);
3203 d3d9_generate_auto_mipmaps(device);
3204 wined3d_device_context_set_primitive_type(device->immediate_context,
3205 wined3d_primitive_type_from_d3d(primitive_type), 0);
3206 wined3d_device_apply_stateblock(device->wined3d_device, device->state);
3207 d3d9_device_upload_sysmem_index_buffer(device, &start_idx, index_count);
3208 wined3d_device_context_draw_indexed(device->immediate_context, base_vertex_idx, start_idx, index_count, 0,
3209 device->stateblock_state->streams[0].frequency);
3210 d3d9_rts_flag_auto_gen_mipmap(device);
3211 wined3d_mutex_unlock();
3213 return D3D_OK;
3216 static HRESULT WINAPI d3d9_device_DrawPrimitiveUP(IDirect3DDevice9Ex *iface,
3217 D3DPRIMITIVETYPE primitive_type, UINT primitive_count, const void *data, UINT stride)
3219 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3220 UINT vtx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
3221 UINT size = vtx_count * stride;
3222 unsigned int vb_pos;
3223 HRESULT hr;
3225 TRACE("iface %p, primitive_type %#x, primitive_count %u, data %p, stride %u.\n",
3226 iface, primitive_type, primitive_count, data, stride);
3228 if (!stride)
3230 WARN("stride is 0, returning D3DERR_INVALIDCALL.\n");
3231 return D3DERR_INVALIDCALL;
3233 if (!primitive_count)
3235 WARN("primitive_count is 0, returning D3D_OK.\n");
3236 return D3D_OK;
3239 wined3d_mutex_lock();
3241 if (!device->stateblock_state->vertex_declaration)
3243 wined3d_mutex_unlock();
3244 WARN("Called without a valid vertex declaration set.\n");
3245 return D3DERR_INVALIDCALL;
3248 if (FAILED(hr = wined3d_streaming_buffer_upload(device->wined3d_device,
3249 &device->vertex_buffer, data, size, stride, &vb_pos)))
3250 goto done;
3252 hr = wined3d_stateblock_set_stream_source(device->state, 0, device->vertex_buffer.buffer, 0, stride);
3253 if (FAILED(hr))
3254 goto done;
3256 d3d9_generate_auto_mipmaps(device);
3257 d3d9_device_upload_managed_textures(device);
3258 wined3d_device_context_set_primitive_type(device->immediate_context,
3259 wined3d_primitive_type_from_d3d(primitive_type), 0);
3260 wined3d_device_apply_stateblock(device->wined3d_device, device->state);
3262 /* Instancing is ignored for non-indexed draws. */
3263 wined3d_device_context_draw(device->immediate_context, vb_pos / stride, vtx_count, 0, 1);
3265 wined3d_stateblock_set_stream_source(device->state, 0, NULL, 0, 0);
3266 d3d9_rts_flag_auto_gen_mipmap(device);
3268 done:
3269 wined3d_mutex_unlock();
3270 return hr;
3273 static HRESULT WINAPI d3d9_device_DrawIndexedPrimitiveUP(IDirect3DDevice9Ex *iface,
3274 D3DPRIMITIVETYPE primitive_type, UINT min_vertex_idx, UINT vertex_count,
3275 UINT primitive_count, const void *index_data, D3DFORMAT index_format,
3276 const void *vertex_data, UINT vertex_stride)
3278 UINT idx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
3279 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3280 UINT idx_fmt_size = index_format == D3DFMT_INDEX16 ? 2 : 4;
3281 UINT vtx_size = vertex_count * vertex_stride;
3282 UINT idx_size = idx_count * idx_fmt_size;
3283 UINT vb_pos, ib_pos;
3284 HRESULT hr;
3286 TRACE("iface %p, primitive_type %#x, min_vertex_idx %u, vertex_count %u, primitive_count %u, "
3287 "index_data %p, index_format %#x, vertex_data %p, vertex_stride %u.\n",
3288 iface, primitive_type, min_vertex_idx, vertex_count, primitive_count,
3289 index_data, index_format, vertex_data, vertex_stride);
3291 if (!vertex_stride)
3293 WARN("vertex_stride is 0, returning D3DERR_INVALIDCALL.\n");
3294 return D3DERR_INVALIDCALL;
3296 if (!primitive_count)
3298 WARN("primitive_count is 0, returning D3D_OK.\n");
3299 return D3D_OK;
3302 wined3d_mutex_lock();
3304 if (!device->stateblock_state->vertex_declaration)
3306 wined3d_mutex_unlock();
3307 WARN("Called without a valid vertex declaration set.\n");
3308 return D3DERR_INVALIDCALL;
3311 if (FAILED(hr = wined3d_streaming_buffer_upload(device->wined3d_device, &device->vertex_buffer,
3312 (char *)vertex_data + min_vertex_idx * vertex_stride, vtx_size, vertex_stride, &vb_pos)))
3313 goto done;
3315 if (FAILED(hr = wined3d_streaming_buffer_upload(device->wined3d_device, &device->index_buffer,
3316 index_data, idx_size, idx_fmt_size, &ib_pos)))
3317 goto done;
3319 hr = wined3d_stateblock_set_stream_source(device->state, 0, device->vertex_buffer.buffer, 0, vertex_stride);
3320 if (FAILED(hr))
3321 goto done;
3322 wined3d_stateblock_set_index_buffer(device->state, device->index_buffer.buffer,
3323 wined3dformat_from_d3dformat(index_format));
3325 d3d9_generate_auto_mipmaps(device);
3326 d3d9_device_upload_managed_textures(device);
3327 wined3d_device_apply_stateblock(device->wined3d_device, device->state);
3328 wined3d_device_context_set_primitive_type(device->immediate_context,
3329 wined3d_primitive_type_from_d3d(primitive_type), 0);
3330 wined3d_device_context_draw_indexed(device->immediate_context,
3331 vb_pos / vertex_stride - min_vertex_idx, ib_pos / idx_fmt_size, idx_count, 0,
3332 device->stateblock_state->streams[0].frequency);
3334 wined3d_stateblock_set_stream_source(device->state, 0, NULL, 0, 0);
3335 wined3d_stateblock_set_index_buffer(device->state, NULL, WINED3DFMT_UNKNOWN);
3337 d3d9_rts_flag_auto_gen_mipmap(device);
3339 done:
3340 wined3d_mutex_unlock();
3341 return hr;
3344 static HRESULT WINAPI d3d9_device_ProcessVertices(IDirect3DDevice9Ex *iface,
3345 UINT src_start_idx, UINT dst_idx, UINT vertex_count, IDirect3DVertexBuffer9 *dst_buffer,
3346 IDirect3DVertexDeclaration9 *declaration, DWORD flags)
3348 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3349 struct d3d9_vertexbuffer *dst_impl = unsafe_impl_from_IDirect3DVertexBuffer9(dst_buffer);
3350 struct d3d9_vertex_declaration *decl_impl = unsafe_impl_from_IDirect3DVertexDeclaration9(declaration);
3351 const struct wined3d_stateblock_state *state;
3352 const struct wined3d_stream_state *stream;
3353 struct d3d9_vertexbuffer *d3d9_buffer;
3354 unsigned int i, map;
3355 HRESULT hr;
3357 TRACE("iface %p, src_start_idx %u, dst_idx %u, vertex_count %u, dst_buffer %p, declaration %p, flags %#lx.\n",
3358 iface, src_start_idx, dst_idx, vertex_count, dst_buffer, declaration, flags);
3360 wined3d_mutex_lock();
3361 state = device->stateblock_state;
3363 /* Note that an alternative approach would be to simply create these
3364 * buffers with WINED3D_RESOURCE_ACCESS_MAP_R and update them here like we
3365 * do for draws. In some regards that would be easier, but it seems less
3366 * than optimal to upload data to the GPU only to subsequently download it
3367 * again. */
3368 map = device->sysmem_vb;
3369 while (map)
3371 i = wined3d_bit_scan(&map);
3372 stream = &state->streams[i];
3374 d3d9_buffer = wined3d_buffer_get_parent(stream->buffer);
3375 if (FAILED(wined3d_stateblock_set_stream_source(device->state,
3376 i, d3d9_buffer->wined3d_buffer, stream->offset, stream->stride)))
3377 ERR("Failed to set stream source.\n");
3380 wined3d_device_apply_stateblock(device->wined3d_device, device->state);
3381 hr = wined3d_device_process_vertices(device->wined3d_device, src_start_idx, dst_idx, vertex_count,
3382 dst_impl->wined3d_buffer, decl_impl ? decl_impl->wined3d_declaration : NULL,
3383 flags, dst_impl->fvf);
3385 map = device->sysmem_vb;
3386 while (map)
3388 i = wined3d_bit_scan(&map);
3389 stream = &state->streams[i];
3391 d3d9_buffer = wined3d_buffer_get_parent(stream->buffer);
3392 if (FAILED(wined3d_stateblock_set_stream_source(device->state,
3393 i, d3d9_buffer->draw_buffer, stream->offset, stream->stride)))
3394 ERR("Failed to set stream source.\n");
3397 wined3d_mutex_unlock();
3399 return hr;
3402 static HRESULT WINAPI d3d9_device_CreateVertexDeclaration(IDirect3DDevice9Ex *iface,
3403 const D3DVERTEXELEMENT9 *elements, IDirect3DVertexDeclaration9 **declaration)
3405 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3406 struct d3d9_vertex_declaration *object;
3407 HRESULT hr;
3409 TRACE("iface %p, elements %p, declaration %p.\n", iface, elements, declaration);
3411 if (!declaration)
3413 WARN("Caller passed a NULL declaration, returning D3DERR_INVALIDCALL.\n");
3414 return D3DERR_INVALIDCALL;
3417 if (SUCCEEDED(hr = d3d9_vertex_declaration_create(device, elements, &object)))
3418 *declaration = &object->IDirect3DVertexDeclaration9_iface;
3420 return hr;
3423 static HRESULT WINAPI d3d9_device_SetVertexDeclaration(IDirect3DDevice9Ex *iface,
3424 IDirect3DVertexDeclaration9 *declaration)
3426 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3427 struct d3d9_vertex_declaration *decl_impl = unsafe_impl_from_IDirect3DVertexDeclaration9(declaration);
3429 TRACE("iface %p, declaration %p.\n", iface, declaration);
3431 wined3d_mutex_lock();
3432 wined3d_stateblock_set_vertex_declaration(device->update_state,
3433 decl_impl ? decl_impl->wined3d_declaration : NULL);
3434 wined3d_mutex_unlock();
3436 return D3D_OK;
3439 static HRESULT WINAPI d3d9_device_GetVertexDeclaration(IDirect3DDevice9Ex *iface,
3440 IDirect3DVertexDeclaration9 **declaration)
3442 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3443 struct wined3d_vertex_declaration *wined3d_declaration;
3444 struct d3d9_vertex_declaration *declaration_impl;
3446 TRACE("iface %p, declaration %p.\n", iface, declaration);
3448 if (!declaration) return D3DERR_INVALIDCALL;
3450 wined3d_mutex_lock();
3451 if ((wined3d_declaration = device->stateblock_state->vertex_declaration))
3453 declaration_impl = wined3d_vertex_declaration_get_parent(wined3d_declaration);
3454 *declaration = &declaration_impl->IDirect3DVertexDeclaration9_iface;
3455 IDirect3DVertexDeclaration9_AddRef(*declaration);
3457 else
3459 *declaration = NULL;
3461 wined3d_mutex_unlock();
3463 TRACE("Returning %p.\n", *declaration);
3464 return D3D_OK;
3467 static struct wined3d_vertex_declaration *device_get_fvf_declaration(struct d3d9_device *device, DWORD fvf)
3469 struct wined3d_vertex_declaration *wined3d_declaration;
3470 struct fvf_declaration *fvf_decls = device->fvf_decls;
3471 struct d3d9_vertex_declaration *d3d9_declaration;
3472 D3DVERTEXELEMENT9 *elements;
3473 int p, low, high; /* deliberately signed */
3474 HRESULT hr;
3476 TRACE("Searching for declaration for fvf %08lx... ", fvf);
3478 low = 0;
3479 high = device->fvf_decl_count - 1;
3480 while (low <= high)
3482 p = (low + high) >> 1;
3483 TRACE("%d ", p);
3485 if (fvf_decls[p].fvf == fvf)
3487 TRACE("found %p.\n", fvf_decls[p].decl);
3488 return fvf_decls[p].decl;
3491 if (fvf_decls[p].fvf < fvf)
3492 low = p + 1;
3493 else
3494 high = p - 1;
3496 TRACE("not found. Creating and inserting at position %d.\n", low);
3498 if (FAILED(hr = vdecl_convert_fvf(fvf, &elements)))
3499 return NULL;
3501 hr = d3d9_vertex_declaration_create(device, elements, &d3d9_declaration);
3502 free(elements);
3503 if (FAILED(hr))
3504 return NULL;
3506 if (device->fvf_decl_size == device->fvf_decl_count)
3508 UINT grow = max(device->fvf_decl_size / 2, 8);
3510 if (!(fvf_decls = realloc(fvf_decls, sizeof(*fvf_decls) * (device->fvf_decl_size + grow))))
3512 IDirect3DVertexDeclaration9_Release(&d3d9_declaration->IDirect3DVertexDeclaration9_iface);
3513 return NULL;
3515 device->fvf_decls = fvf_decls;
3516 device->fvf_decl_size += grow;
3519 d3d9_declaration->fvf = fvf;
3520 wined3d_declaration = d3d9_declaration->wined3d_declaration;
3521 wined3d_vertex_declaration_incref(wined3d_declaration);
3522 IDirect3DVertexDeclaration9_Release(&d3d9_declaration->IDirect3DVertexDeclaration9_iface);
3524 memmove(fvf_decls + low + 1, fvf_decls + low, sizeof(*fvf_decls) * (device->fvf_decl_count - low));
3525 fvf_decls[low].decl = wined3d_declaration;
3526 fvf_decls[low].fvf = fvf;
3527 ++device->fvf_decl_count;
3529 TRACE("Returning %p. %u declarations in array.\n", wined3d_declaration, device->fvf_decl_count);
3531 return wined3d_declaration;
3534 static HRESULT WINAPI d3d9_device_SetFVF(IDirect3DDevice9Ex *iface, DWORD fvf)
3536 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3537 struct wined3d_vertex_declaration *decl;
3539 TRACE("iface %p, fvf %#lx.\n", iface, fvf);
3541 if (!fvf)
3543 WARN("%#lx is not a valid FVF.\n", fvf);
3544 return D3D_OK;
3547 wined3d_mutex_lock();
3548 if (!(decl = device_get_fvf_declaration(device, fvf)))
3550 wined3d_mutex_unlock();
3551 ERR("Failed to create a vertex declaration for fvf %#lx.\n", fvf);
3552 return D3DERR_DRIVERINTERNALERROR;
3555 wined3d_stateblock_set_vertex_declaration(device->update_state, decl);
3556 wined3d_mutex_unlock();
3558 return D3D_OK;
3561 static HRESULT WINAPI d3d9_device_GetFVF(IDirect3DDevice9Ex *iface, DWORD *fvf)
3563 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3564 struct wined3d_vertex_declaration *wined3d_declaration;
3565 struct d3d9_vertex_declaration *d3d9_declaration;
3567 TRACE("iface %p, fvf %p.\n", iface, fvf);
3569 wined3d_mutex_lock();
3570 if ((wined3d_declaration = device->stateblock_state->vertex_declaration))
3572 d3d9_declaration = wined3d_vertex_declaration_get_parent(wined3d_declaration);
3573 *fvf = d3d9_declaration->fvf;
3575 else
3577 *fvf = 0;
3579 wined3d_mutex_unlock();
3581 TRACE("Returning FVF %#lx.\n", *fvf);
3583 return D3D_OK;
3586 static HRESULT WINAPI d3d9_device_CreateVertexShader(IDirect3DDevice9Ex *iface,
3587 const DWORD *byte_code, IDirect3DVertexShader9 **shader)
3589 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3590 struct d3d9_vertexshader *object;
3591 HRESULT hr;
3593 TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
3595 if (!(object = calloc(1, sizeof(*object))))
3596 return E_OUTOFMEMORY;
3598 hr = vertexshader_init(object, device, byte_code);
3599 if (FAILED(hr))
3601 WARN("Failed to initialize vertex shader, hr %#lx.\n", hr);
3602 free(object);
3603 return hr;
3606 TRACE("Created vertex shader %p.\n", object);
3607 *shader = &object->IDirect3DVertexShader9_iface;
3609 return D3D_OK;
3612 static HRESULT WINAPI d3d9_device_SetVertexShader(IDirect3DDevice9Ex *iface, IDirect3DVertexShader9 *shader)
3614 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3615 struct d3d9_vertexshader *shader_obj = unsafe_impl_from_IDirect3DVertexShader9(shader);
3617 TRACE("iface %p, shader %p.\n", iface, shader);
3619 wined3d_mutex_lock();
3620 wined3d_stateblock_set_vertex_shader(device->update_state,
3621 shader_obj ? shader_obj->wined3d_shader : NULL);
3622 wined3d_mutex_unlock();
3624 return D3D_OK;
3627 static HRESULT WINAPI d3d9_device_GetVertexShader(IDirect3DDevice9Ex *iface, IDirect3DVertexShader9 **shader)
3629 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3630 struct d3d9_vertexshader *shader_impl;
3631 struct wined3d_shader *wined3d_shader;
3633 TRACE("iface %p, shader %p.\n", iface, shader);
3635 wined3d_mutex_lock();
3636 if ((wined3d_shader = device->stateblock_state->vs))
3638 shader_impl = wined3d_shader_get_parent(wined3d_shader);
3639 *shader = &shader_impl->IDirect3DVertexShader9_iface;
3640 IDirect3DVertexShader9_AddRef(*shader);
3642 else
3644 *shader = NULL;
3646 wined3d_mutex_unlock();
3648 TRACE("Returning %p.\n", *shader);
3650 return D3D_OK;
3653 static HRESULT WINAPI d3d9_device_SetVertexShaderConstantF(IDirect3DDevice9Ex *iface,
3654 UINT reg_idx, const float *data, UINT count)
3656 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3657 HRESULT hr;
3659 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3661 if (reg_idx + count > D3D9_MAX_VERTEX_SHADER_CONSTANTF)
3663 WARN("Trying to access %u constants, but d3d9 only supports %u\n",
3664 reg_idx + count, D3D9_MAX_VERTEX_SHADER_CONSTANTF);
3665 return D3DERR_INVALIDCALL;
3668 wined3d_mutex_lock();
3669 hr = wined3d_stateblock_set_vs_consts_f(device->update_state, reg_idx,
3670 count, (const struct wined3d_vec4 *)data);
3671 wined3d_mutex_unlock();
3673 return hr;
3676 static HRESULT WINAPI d3d9_device_GetVertexShaderConstantF(IDirect3DDevice9Ex *iface,
3677 UINT start_idx, float *constants, UINT count)
3679 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3680 HRESULT hr;
3682 TRACE("iface %p, start_idx %u, constants %p, count %u.\n", iface, start_idx, constants, count);
3684 wined3d_mutex_lock();
3685 hr = wined3d_stateblock_get_vs_consts_f(device->state, start_idx, count, (struct wined3d_vec4 *)constants);
3686 wined3d_mutex_unlock();
3688 return hr;
3691 static HRESULT WINAPI d3d9_device_SetVertexShaderConstantI(IDirect3DDevice9Ex *iface,
3692 UINT reg_idx, const int *data, UINT count)
3694 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3695 HRESULT hr;
3697 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3699 wined3d_mutex_lock();
3700 hr = wined3d_stateblock_set_vs_consts_i(device->update_state,
3701 reg_idx, count, (const struct wined3d_ivec4 *)data);
3702 wined3d_mutex_unlock();
3704 return hr;
3707 static HRESULT WINAPI d3d9_device_GetVertexShaderConstantI(IDirect3DDevice9Ex *iface,
3708 UINT start_idx, int *constants, UINT count)
3710 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3711 HRESULT hr;
3713 TRACE("iface %p, start_idx %u, constants %p, count %u.\n", iface, start_idx, constants, count);
3715 wined3d_mutex_lock();
3716 hr = wined3d_stateblock_get_vs_consts_i(device->state, start_idx, count, (struct wined3d_ivec4 *)constants);
3717 wined3d_mutex_unlock();
3719 return hr;
3722 static HRESULT WINAPI d3d9_device_SetVertexShaderConstantB(IDirect3DDevice9Ex *iface,
3723 UINT reg_idx, const BOOL *data, UINT count)
3725 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3726 HRESULT hr;
3728 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3730 wined3d_mutex_lock();
3731 hr = wined3d_stateblock_set_vs_consts_b(device->update_state, reg_idx, count, data);
3732 wined3d_mutex_unlock();
3734 return hr;
3737 static HRESULT WINAPI d3d9_device_GetVertexShaderConstantB(IDirect3DDevice9Ex *iface,
3738 UINT start_idx, BOOL *constants, UINT count)
3740 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3741 HRESULT hr;
3743 TRACE("iface %p, start_idx %u, constants %p, count %u.\n", iface, start_idx, constants, count);
3745 wined3d_mutex_lock();
3746 hr = wined3d_stateblock_get_vs_consts_b(device->state, start_idx, count, constants);
3747 wined3d_mutex_unlock();
3749 return hr;
3752 static HRESULT WINAPI d3d9_device_SetStreamSource(IDirect3DDevice9Ex *iface,
3753 UINT stream_idx, IDirect3DVertexBuffer9 *buffer, UINT offset, UINT stride)
3755 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3756 struct d3d9_vertexbuffer *buffer_impl = unsafe_impl_from_IDirect3DVertexBuffer9(buffer);
3757 struct wined3d_buffer *wined3d_buffer;
3758 HRESULT hr;
3760 TRACE("iface %p, stream_idx %u, buffer %p, offset %u, stride %u.\n",
3761 iface, stream_idx, buffer, offset, stride);
3763 if (stream_idx >= ARRAY_SIZE(device->stateblock_state->streams))
3765 WARN("Stream index %u out of range.\n", stream_idx);
3766 return WINED3DERR_INVALIDCALL;
3769 wined3d_mutex_lock();
3770 if (!buffer_impl)
3772 const struct wined3d_stream_state *stream = &device->stateblock_state->streams[stream_idx];
3774 offset = stream->offset;
3775 stride = stream->stride;
3776 wined3d_buffer = NULL;
3778 else if (buffer_impl->draw_buffer)
3780 wined3d_buffer = buffer_impl->draw_buffer;
3782 else
3784 wined3d_buffer = buffer_impl->wined3d_buffer;
3787 hr = wined3d_stateblock_set_stream_source(device->update_state, stream_idx, wined3d_buffer, offset, stride);
3788 if (SUCCEEDED(hr) && !device->recording)
3790 if (buffer_impl && buffer_impl->draw_buffer)
3791 device->sysmem_vb |= (1u << stream_idx);
3792 else
3793 device->sysmem_vb &= ~(1u << stream_idx);
3796 wined3d_mutex_unlock();
3798 return hr;
3801 static HRESULT WINAPI d3d9_device_GetStreamSource(IDirect3DDevice9Ex *iface,
3802 UINT stream_idx, IDirect3DVertexBuffer9 **buffer, UINT *offset, UINT *stride)
3804 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3805 const struct wined3d_stateblock_state *state;
3806 const struct wined3d_stream_state *stream;
3807 struct d3d9_vertexbuffer *buffer_impl;
3809 TRACE("iface %p, stream_idx %u, buffer %p, offset %p, stride %p.\n",
3810 iface, stream_idx, buffer, offset, stride);
3812 if (!buffer)
3813 return D3DERR_INVALIDCALL;
3815 if (stream_idx >= ARRAY_SIZE(state->streams))
3817 WARN("Stream index %u out of range.\n", stream_idx);
3818 return WINED3DERR_INVALIDCALL;
3821 wined3d_mutex_lock();
3822 stream = &device->stateblock_state->streams[stream_idx];
3823 if (stream->buffer)
3825 buffer_impl = wined3d_buffer_get_parent(stream->buffer);
3826 *buffer = &buffer_impl->IDirect3DVertexBuffer9_iface;
3827 IDirect3DVertexBuffer9_AddRef(*buffer);
3829 else
3830 *buffer = NULL;
3831 if (offset)
3832 *offset = stream->offset;
3833 *stride = stream->stride;
3834 wined3d_mutex_unlock();
3836 return D3D_OK;
3839 static HRESULT WINAPI d3d9_device_SetStreamSourceFreq(IDirect3DDevice9Ex *iface, UINT stream_idx, UINT freq)
3841 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3842 HRESULT hr;
3844 TRACE("iface %p, stream_idx %u, freq %u.\n", iface, stream_idx, freq);
3846 wined3d_mutex_lock();
3847 hr = wined3d_stateblock_set_stream_source_freq(device->update_state, stream_idx, freq);
3848 wined3d_mutex_unlock();
3850 return hr;
3853 static HRESULT WINAPI d3d9_device_GetStreamSourceFreq(IDirect3DDevice9Ex *iface, UINT stream_idx, UINT *freq)
3855 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3856 const struct wined3d_stream_state *stream;
3858 TRACE("iface %p, stream_idx %u, freq %p.\n", iface, stream_idx, freq);
3860 wined3d_mutex_lock();
3861 stream = &device->stateblock_state->streams[stream_idx];
3862 *freq = stream->flags | stream->frequency;
3863 wined3d_mutex_unlock();
3865 return D3D_OK;
3868 static HRESULT WINAPI d3d9_device_SetIndices(IDirect3DDevice9Ex *iface, IDirect3DIndexBuffer9 *buffer)
3870 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3871 struct d3d9_indexbuffer *ib = unsafe_impl_from_IDirect3DIndexBuffer9(buffer);
3872 struct wined3d_buffer *wined3d_buffer;
3874 TRACE("iface %p, buffer %p.\n", iface, buffer);
3876 if (!ib)
3877 wined3d_buffer = NULL;
3878 else
3879 wined3d_buffer = ib->wined3d_buffer;
3881 wined3d_mutex_lock();
3882 wined3d_stateblock_set_index_buffer(device->update_state, wined3d_buffer, ib ? ib->format : WINED3DFMT_UNKNOWN);
3883 if (!device->recording)
3884 device->sysmem_ib = ib && ib->sysmem;
3885 wined3d_mutex_unlock();
3887 return D3D_OK;
3890 static HRESULT WINAPI d3d9_device_GetIndices(IDirect3DDevice9Ex *iface, IDirect3DIndexBuffer9 **buffer)
3892 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3893 struct wined3d_buffer *wined3d_buffer;
3894 struct d3d9_indexbuffer *buffer_impl;
3896 TRACE("iface %p, buffer %p.\n", iface, buffer);
3898 if (!buffer)
3899 return D3DERR_INVALIDCALL;
3901 wined3d_mutex_lock();
3902 if ((wined3d_buffer = device->stateblock_state->index_buffer))
3904 buffer_impl = wined3d_buffer_get_parent(wined3d_buffer);
3905 *buffer = &buffer_impl->IDirect3DIndexBuffer9_iface;
3906 IDirect3DIndexBuffer9_AddRef(*buffer);
3908 else
3910 *buffer = NULL;
3912 wined3d_mutex_unlock();
3914 return D3D_OK;
3917 static HRESULT WINAPI d3d9_device_CreatePixelShader(IDirect3DDevice9Ex *iface,
3918 const DWORD *byte_code, IDirect3DPixelShader9 **shader)
3920 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3921 struct d3d9_pixelshader *object;
3922 HRESULT hr;
3924 TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
3926 if (!(object = calloc(1, sizeof(*object))))
3928 FIXME("Failed to allocate pixel shader memory.\n");
3929 return E_OUTOFMEMORY;
3932 hr = pixelshader_init(object, device, byte_code);
3933 if (FAILED(hr))
3935 WARN("Failed to initialize pixel shader, hr %#lx.\n", hr);
3936 free(object);
3937 return hr;
3940 TRACE("Created pixel shader %p.\n", object);
3941 *shader = &object->IDirect3DPixelShader9_iface;
3943 return D3D_OK;
3946 static HRESULT WINAPI d3d9_device_SetPixelShader(IDirect3DDevice9Ex *iface, IDirect3DPixelShader9 *shader)
3948 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3949 struct d3d9_pixelshader *shader_obj = unsafe_impl_from_IDirect3DPixelShader9(shader);
3951 TRACE("iface %p, shader %p.\n", iface, shader);
3953 wined3d_mutex_lock();
3954 wined3d_stateblock_set_pixel_shader(device->update_state,
3955 shader_obj ? shader_obj->wined3d_shader : NULL);
3956 wined3d_mutex_unlock();
3958 return D3D_OK;
3961 static HRESULT WINAPI d3d9_device_GetPixelShader(IDirect3DDevice9Ex *iface, IDirect3DPixelShader9 **shader)
3963 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3964 struct d3d9_pixelshader *shader_impl;
3965 struct wined3d_shader *wined3d_shader;
3967 TRACE("iface %p, shader %p.\n", iface, shader);
3969 if (!shader) return D3DERR_INVALIDCALL;
3971 wined3d_mutex_lock();
3972 if ((wined3d_shader = device->stateblock_state->ps))
3974 shader_impl = wined3d_shader_get_parent(wined3d_shader);
3975 *shader = &shader_impl->IDirect3DPixelShader9_iface;
3976 IDirect3DPixelShader9_AddRef(*shader);
3978 else
3980 *shader = NULL;
3982 wined3d_mutex_unlock();
3984 TRACE("Returning %p.\n", *shader);
3986 return D3D_OK;
3989 static HRESULT WINAPI d3d9_device_SetPixelShaderConstantF(IDirect3DDevice9Ex *iface,
3990 UINT reg_idx, const float *data, UINT count)
3992 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3993 HRESULT hr;
3995 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3997 wined3d_mutex_lock();
3998 hr = wined3d_stateblock_set_ps_consts_f(device->update_state,
3999 reg_idx, count, (const struct wined3d_vec4 *)data);
4000 wined3d_mutex_unlock();
4002 return hr;
4005 static HRESULT WINAPI d3d9_device_GetPixelShaderConstantF(IDirect3DDevice9Ex *iface,
4006 UINT start_idx, float *constants, UINT count)
4008 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
4009 HRESULT hr;
4011 TRACE("iface %p, start_idx %u, constants %p, count %u.\n", iface, start_idx, constants, count);
4013 wined3d_mutex_lock();
4014 hr = wined3d_stateblock_get_ps_consts_f(device->state, start_idx, count, (struct wined3d_vec4 *)constants);
4015 wined3d_mutex_unlock();
4017 return hr;
4020 static HRESULT WINAPI d3d9_device_SetPixelShaderConstantI(IDirect3DDevice9Ex *iface,
4021 UINT reg_idx, const int *data, UINT count)
4023 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
4024 HRESULT hr;
4026 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
4028 wined3d_mutex_lock();
4029 hr = wined3d_stateblock_set_ps_consts_i(device->update_state,
4030 reg_idx, count, (const struct wined3d_ivec4 *)data);
4031 wined3d_mutex_unlock();
4033 return hr;
4036 static HRESULT WINAPI d3d9_device_GetPixelShaderConstantI(IDirect3DDevice9Ex *iface,
4037 UINT start_idx, int *constants, UINT count)
4039 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
4040 HRESULT hr;
4042 TRACE("iface %p, start_idx %u, constants %p, count %u.\n", iface, start_idx, constants, count);
4044 wined3d_mutex_lock();
4045 hr = wined3d_stateblock_get_ps_consts_i(device->state, start_idx, count, (struct wined3d_ivec4 *)constants);
4046 wined3d_mutex_unlock();
4048 return hr;
4051 static HRESULT WINAPI d3d9_device_SetPixelShaderConstantB(IDirect3DDevice9Ex *iface,
4052 UINT reg_idx, const BOOL *data, UINT count)
4054 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
4055 HRESULT hr;
4057 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
4059 wined3d_mutex_lock();
4060 hr = wined3d_stateblock_set_ps_consts_b(device->update_state, reg_idx, count, data);
4061 wined3d_mutex_unlock();
4063 return hr;
4066 static HRESULT WINAPI d3d9_device_GetPixelShaderConstantB(IDirect3DDevice9Ex *iface,
4067 UINT start_idx, BOOL *constants, UINT count)
4069 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
4070 HRESULT hr;
4072 TRACE("iface %p, start_idx %u, constants %p, count %u.\n", iface, start_idx, constants, count);
4074 wined3d_mutex_lock();
4075 hr = wined3d_stateblock_get_ps_consts_b(device->state, start_idx, count, constants);
4076 wined3d_mutex_unlock();
4078 return hr;
4081 static HRESULT WINAPI d3d9_device_DrawRectPatch(IDirect3DDevice9Ex *iface, UINT handle,
4082 const float *segment_count, const D3DRECTPATCH_INFO *patch_info)
4084 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
4085 iface, handle, segment_count, patch_info);
4086 return D3D_OK;
4089 static HRESULT WINAPI d3d9_device_DrawTriPatch(IDirect3DDevice9Ex *iface, UINT handle,
4090 const float *segment_count, const D3DTRIPATCH_INFO *patch_info)
4092 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
4093 iface, handle, segment_count, patch_info);
4094 return D3D_OK;
4097 static HRESULT WINAPI d3d9_device_DeletePatch(IDirect3DDevice9Ex *iface, UINT handle)
4099 FIXME("iface %p, handle %#x unimplemented.\n", iface, handle);
4100 return D3DERR_INVALIDCALL;
4103 static HRESULT WINAPI d3d9_device_CreateQuery(IDirect3DDevice9Ex *iface, D3DQUERYTYPE type, IDirect3DQuery9 **query)
4105 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
4106 struct d3d9_query *object;
4107 HRESULT hr;
4109 TRACE("iface %p, type %#x, query %p.\n", iface, type, query);
4111 if (!(object = calloc(1, sizeof(*object))))
4112 return E_OUTOFMEMORY;
4114 hr = query_init(object, device, type);
4115 if (FAILED(hr))
4117 WARN("Failed to initialize query, hr %#lx.\n", hr);
4118 free(object);
4119 return hr;
4122 TRACE("Created query %p.\n", object);
4123 if (query) *query = &object->IDirect3DQuery9_iface;
4124 else IDirect3DQuery9_Release(&object->IDirect3DQuery9_iface);
4126 return D3D_OK;
4129 static HRESULT WINAPI d3d9_device_SetConvolutionMonoKernel(IDirect3DDevice9Ex *iface,
4130 UINT width, UINT height, float *rows, float *columns)
4132 FIXME("iface %p, width %u, height %u, rows %p, columns %p stub!\n",
4133 iface, width, height, rows, columns);
4135 return E_NOTIMPL;
4138 static HRESULT WINAPI d3d9_device_ComposeRects(IDirect3DDevice9Ex *iface,
4139 IDirect3DSurface9 *src_surface, IDirect3DSurface9 *dst_surface, IDirect3DVertexBuffer9 *src_descs,
4140 UINT rect_count, IDirect3DVertexBuffer9 *dst_descs, D3DCOMPOSERECTSOP operation, INT offset_x, INT offset_y)
4142 FIXME("iface %p, src_surface %p, dst_surface %p, src_descs %p, rect_count %u, "
4143 "dst_descs %p, operation %#x, offset_x %u, offset_y %u stub!\n",
4144 iface, src_surface, dst_surface, src_descs, rect_count,
4145 dst_descs, operation, offset_x, offset_y);
4147 return E_NOTIMPL;
4150 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_PresentEx(IDirect3DDevice9Ex *iface,
4151 const RECT *src_rect, const RECT *dst_rect, HWND dst_window_override,
4152 const RGNDATA *dirty_region, DWORD flags)
4154 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
4155 struct d3d9_swapchain *swapchain;
4156 unsigned int i;
4157 HRESULT hr;
4159 TRACE("iface %p, src_rect %s, dst_rect %s, dst_window_override %p, dirty_region %p, flags %#lx.\n",
4160 iface, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect),
4161 dst_window_override, dirty_region, flags);
4163 if (device->device_state != D3D9_DEVICE_STATE_OK)
4164 return S_PRESENT_OCCLUDED;
4166 if (dirty_region)
4167 FIXME("Ignoring dirty_region %p.\n", dirty_region);
4169 wined3d_mutex_lock();
4170 for (i = 0; i < device->implicit_swapchain_count; ++i)
4172 swapchain = wined3d_swapchain_get_parent(device->implicit_swapchains[i]);
4173 if (FAILED(hr = wined3d_swapchain_present(swapchain->wined3d_swapchain,
4174 src_rect, dst_rect, dst_window_override, swapchain->swap_interval, flags)))
4176 wined3d_mutex_unlock();
4177 return hr;
4180 wined3d_mutex_unlock();
4182 return D3D_OK;
4185 static HRESULT WINAPI d3d9_device_GetGPUThreadPriority(IDirect3DDevice9Ex *iface, INT *priority)
4187 FIXME("iface %p, priority %p stub!\n", iface, priority);
4189 return E_NOTIMPL;
4192 static HRESULT WINAPI d3d9_device_SetGPUThreadPriority(IDirect3DDevice9Ex *iface, INT priority)
4194 FIXME("iface %p, priority %d stub!\n", iface, priority);
4196 return E_NOTIMPL;
4199 static HRESULT WINAPI d3d9_device_WaitForVBlank(IDirect3DDevice9Ex *iface, UINT swapchain_idx)
4201 FIXME("iface %p, swapchain_idx %u stub!\n", iface, swapchain_idx);
4203 return E_NOTIMPL;
4206 static HRESULT WINAPI d3d9_device_CheckResourceResidency(IDirect3DDevice9Ex *iface,
4207 IDirect3DResource9 **resources, UINT32 resource_count)
4209 FIXME("iface %p, resources %p, resource_count %u stub!\n",
4210 iface, resources, resource_count);
4212 return E_NOTIMPL;
4215 static HRESULT WINAPI d3d9_device_SetMaximumFrameLatency(IDirect3DDevice9Ex *iface, UINT max_latency)
4217 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
4219 TRACE("iface %p, max_latency %u.\n", iface, max_latency);
4221 if (max_latency > 30)
4222 return D3DERR_INVALIDCALL;
4224 wined3d_mutex_lock();
4225 wined3d_device_set_max_frame_latency(device->wined3d_device, max_latency);
4226 wined3d_mutex_unlock();
4228 return S_OK;
4231 static HRESULT WINAPI d3d9_device_GetMaximumFrameLatency(IDirect3DDevice9Ex *iface, UINT *max_latency)
4233 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
4235 TRACE("iface %p, max_latency %p.\n", iface, max_latency);
4237 wined3d_mutex_lock();
4238 *max_latency = wined3d_device_get_max_frame_latency(device->wined3d_device);
4239 wined3d_mutex_unlock();
4241 return S_OK;
4244 static HRESULT WINAPI d3d9_device_CheckDeviceState(IDirect3DDevice9Ex *iface, HWND dst_window)
4246 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
4247 struct wined3d_swapchain_desc swapchain_desc;
4249 TRACE("iface %p, dst_window %p.\n", iface, dst_window);
4251 wined3d_mutex_lock();
4252 wined3d_swapchain_get_desc(device->implicit_swapchains[0], &swapchain_desc);
4253 wined3d_mutex_unlock();
4255 if (swapchain_desc.windowed)
4256 return D3D_OK;
4258 /* FIXME: This is actually supposed to check if any other device is in
4259 * fullscreen mode. */
4260 if (dst_window != swapchain_desc.device_window)
4261 return device->device_state == D3D9_DEVICE_STATE_OK ? S_PRESENT_OCCLUDED : D3D_OK;
4263 return device->device_state == D3D9_DEVICE_STATE_OK ? D3D_OK : S_PRESENT_OCCLUDED;
4266 static HRESULT WINAPI d3d9_device_CreateRenderTargetEx(IDirect3DDevice9Ex *iface,
4267 UINT width, UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
4268 BOOL lockable, IDirect3DSurface9 **surface, HANDLE *shared_handle, DWORD usage)
4270 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
4271 unsigned int access = WINED3D_RESOURCE_ACCESS_GPU;
4273 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %lu, "
4274 "lockable %#x, surface %p, shared_handle %p, usage %#lx.\n",
4275 iface, width, height, format, multisample_type, multisample_quality,
4276 lockable, surface, shared_handle, usage);
4278 *surface = NULL;
4280 if (usage & (D3DUSAGE_DEPTHSTENCIL | D3DUSAGE_RENDERTARGET))
4282 WARN("Invalid usage %#lx.\n", usage);
4283 return D3DERR_INVALIDCALL;
4286 if (shared_handle)
4287 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
4289 if (lockable)
4290 access |= WINED3D_RESOURCE_ACCESS_MAP_R | WINED3D_RESOURCE_ACCESS_MAP_W;
4292 return d3d9_device_create_surface(device, 0, wined3dformat_from_d3dformat(format),
4293 wined3d_multisample_type_from_d3d(multisample_type), multisample_quality,
4294 usage & WINED3DUSAGE_MASK, WINED3D_BIND_RENDER_TARGET, access, width, height, NULL, surface);
4297 static HRESULT WINAPI d3d9_device_CreateOffscreenPlainSurfaceEx(IDirect3DDevice9Ex *iface,
4298 UINT width, UINT height, D3DFORMAT format, D3DPOOL pool, IDirect3DSurface9 **surface,
4299 HANDLE *shared_handle, DWORD usage)
4301 FIXME("iface %p, width %u, height %u, format %#x, pool %#x, surface %p, shared_handle %p, usage %#lx stub!\n",
4302 iface, width, height, format, pool, surface, shared_handle, usage);
4304 return E_NOTIMPL;
4307 static HRESULT WINAPI d3d9_device_CreateDepthStencilSurfaceEx(IDirect3DDevice9Ex *iface,
4308 UINT width, UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
4309 BOOL discard, IDirect3DSurface9 **surface, HANDLE *shared_handle, DWORD usage)
4311 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
4312 DWORD flags = 0;
4314 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %lu, "
4315 "discard %#x, surface %p, shared_handle %p, usage %#lx.\n",
4316 iface, width, height, format, multisample_type, multisample_quality,
4317 discard, surface, shared_handle, usage);
4319 if (usage & (D3DUSAGE_DEPTHSTENCIL | D3DUSAGE_RENDERTARGET))
4321 WARN("Invalid usage %#lx.\n", usage);
4322 return D3DERR_INVALIDCALL;
4325 if (shared_handle)
4326 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
4328 if (discard)
4329 flags |= WINED3D_TEXTURE_CREATE_DISCARD;
4331 *surface = NULL;
4332 return d3d9_device_create_surface(device, flags, wined3dformat_from_d3dformat(format),
4333 wined3d_multisample_type_from_d3d(multisample_type), multisample_quality, usage & WINED3DUSAGE_MASK,
4334 WINED3D_BIND_DEPTH_STENCIL, WINED3D_RESOURCE_ACCESS_GPU, width, height, NULL, surface);
4337 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_ResetEx(IDirect3DDevice9Ex *iface,
4338 D3DPRESENT_PARAMETERS *present_parameters, D3DDISPLAYMODEEX *mode)
4340 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
4342 TRACE("iface %p, present_parameters %p, mode %p.\n", iface, present_parameters, mode);
4344 if (!present_parameters->Windowed == !mode)
4346 WARN("Mode can be passed if and only if Windowed is FALSE.\n");
4347 return D3DERR_INVALIDCALL;
4350 if (mode && (mode->Width != present_parameters->BackBufferWidth
4351 || mode->Height != present_parameters->BackBufferHeight))
4353 WARN("Mode and back buffer mismatch (mode %ux%u, backbuffer %ux%u).\n",
4354 mode->Width, mode->Height,
4355 present_parameters->BackBufferWidth, present_parameters->BackBufferHeight);
4356 return D3DERR_INVALIDCALL;
4359 return d3d9_device_reset(device, present_parameters, mode);
4362 static HRESULT WINAPI d3d9_device_GetDisplayModeEx(IDirect3DDevice9Ex *iface,
4363 UINT swapchain_idx, D3DDISPLAYMODEEX *mode, D3DDISPLAYROTATION *rotation)
4365 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
4366 struct wined3d_display_mode wined3d_mode;
4367 HRESULT hr;
4369 TRACE("iface %p, swapchain_idx %u, mode %p, rotation %p.\n",
4370 iface, swapchain_idx, mode, rotation);
4372 if (mode->Size != sizeof(*mode))
4373 return D3DERR_INVALIDCALL;
4375 wined3d_mutex_lock();
4376 hr = wined3d_device_get_display_mode(device->wined3d_device, swapchain_idx, &wined3d_mode,
4377 (enum wined3d_display_rotation *)rotation);
4378 wined3d_mutex_unlock();
4380 if (SUCCEEDED(hr))
4382 mode->Width = wined3d_mode.width;
4383 mode->Height = wined3d_mode.height;
4384 mode->RefreshRate = wined3d_mode.refresh_rate;
4385 mode->Format = d3dformat_from_wined3dformat(wined3d_mode.format_id);
4386 mode->ScanLineOrdering = d3dscanlineordering_from_wined3d(wined3d_mode.scanline_ordering);
4389 return hr;
4392 static const struct IDirect3DDevice9ExVtbl d3d9_device_vtbl =
4394 /* IUnknown */
4395 d3d9_device_QueryInterface,
4396 d3d9_device_AddRef,
4397 d3d9_device_Release,
4398 /* IDirect3DDevice9 */
4399 d3d9_device_TestCooperativeLevel,
4400 d3d9_device_GetAvailableTextureMem,
4401 d3d9_device_EvictManagedResources,
4402 d3d9_device_GetDirect3D,
4403 d3d9_device_GetDeviceCaps,
4404 d3d9_device_GetDisplayMode,
4405 d3d9_device_GetCreationParameters,
4406 d3d9_device_SetCursorProperties,
4407 d3d9_device_SetCursorPosition,
4408 d3d9_device_ShowCursor,
4409 d3d9_device_CreateAdditionalSwapChain,
4410 d3d9_device_GetSwapChain,
4411 d3d9_device_GetNumberOfSwapChains,
4412 d3d9_device_Reset,
4413 d3d9_device_Present,
4414 d3d9_device_GetBackBuffer,
4415 d3d9_device_GetRasterStatus,
4416 d3d9_device_SetDialogBoxMode,
4417 d3d9_device_SetGammaRamp,
4418 d3d9_device_GetGammaRamp,
4419 d3d9_device_CreateTexture,
4420 d3d9_device_CreateVolumeTexture,
4421 d3d9_device_CreateCubeTexture,
4422 d3d9_device_CreateVertexBuffer,
4423 d3d9_device_CreateIndexBuffer,
4424 d3d9_device_CreateRenderTarget,
4425 d3d9_device_CreateDepthStencilSurface,
4426 d3d9_device_UpdateSurface,
4427 d3d9_device_UpdateTexture,
4428 d3d9_device_GetRenderTargetData,
4429 d3d9_device_GetFrontBufferData,
4430 d3d9_device_StretchRect,
4431 d3d9_device_ColorFill,
4432 d3d9_device_CreateOffscreenPlainSurface,
4433 d3d9_device_SetRenderTarget,
4434 d3d9_device_GetRenderTarget,
4435 d3d9_device_SetDepthStencilSurface,
4436 d3d9_device_GetDepthStencilSurface,
4437 d3d9_device_BeginScene,
4438 d3d9_device_EndScene,
4439 d3d9_device_Clear,
4440 d3d9_device_SetTransform,
4441 d3d9_device_GetTransform,
4442 d3d9_device_MultiplyTransform,
4443 d3d9_device_SetViewport,
4444 d3d9_device_GetViewport,
4445 d3d9_device_SetMaterial,
4446 d3d9_device_GetMaterial,
4447 d3d9_device_SetLight,
4448 d3d9_device_GetLight,
4449 d3d9_device_LightEnable,
4450 d3d9_device_GetLightEnable,
4451 d3d9_device_SetClipPlane,
4452 d3d9_device_GetClipPlane,
4453 d3d9_device_SetRenderState,
4454 d3d9_device_GetRenderState,
4455 d3d9_device_CreateStateBlock,
4456 d3d9_device_BeginStateBlock,
4457 d3d9_device_EndStateBlock,
4458 d3d9_device_SetClipStatus,
4459 d3d9_device_GetClipStatus,
4460 d3d9_device_GetTexture,
4461 d3d9_device_SetTexture,
4462 d3d9_device_GetTextureStageState,
4463 d3d9_device_SetTextureStageState,
4464 d3d9_device_GetSamplerState,
4465 d3d9_device_SetSamplerState,
4466 d3d9_device_ValidateDevice,
4467 d3d9_device_SetPaletteEntries,
4468 d3d9_device_GetPaletteEntries,
4469 d3d9_device_SetCurrentTexturePalette,
4470 d3d9_device_GetCurrentTexturePalette,
4471 d3d9_device_SetScissorRect,
4472 d3d9_device_GetScissorRect,
4473 d3d9_device_SetSoftwareVertexProcessing,
4474 d3d9_device_GetSoftwareVertexProcessing,
4475 d3d9_device_SetNPatchMode,
4476 d3d9_device_GetNPatchMode,
4477 d3d9_device_DrawPrimitive,
4478 d3d9_device_DrawIndexedPrimitive,
4479 d3d9_device_DrawPrimitiveUP,
4480 d3d9_device_DrawIndexedPrimitiveUP,
4481 d3d9_device_ProcessVertices,
4482 d3d9_device_CreateVertexDeclaration,
4483 d3d9_device_SetVertexDeclaration,
4484 d3d9_device_GetVertexDeclaration,
4485 d3d9_device_SetFVF,
4486 d3d9_device_GetFVF,
4487 d3d9_device_CreateVertexShader,
4488 d3d9_device_SetVertexShader,
4489 d3d9_device_GetVertexShader,
4490 d3d9_device_SetVertexShaderConstantF,
4491 d3d9_device_GetVertexShaderConstantF,
4492 d3d9_device_SetVertexShaderConstantI,
4493 d3d9_device_GetVertexShaderConstantI,
4494 d3d9_device_SetVertexShaderConstantB,
4495 d3d9_device_GetVertexShaderConstantB,
4496 d3d9_device_SetStreamSource,
4497 d3d9_device_GetStreamSource,
4498 d3d9_device_SetStreamSourceFreq,
4499 d3d9_device_GetStreamSourceFreq,
4500 d3d9_device_SetIndices,
4501 d3d9_device_GetIndices,
4502 d3d9_device_CreatePixelShader,
4503 d3d9_device_SetPixelShader,
4504 d3d9_device_GetPixelShader,
4505 d3d9_device_SetPixelShaderConstantF,
4506 d3d9_device_GetPixelShaderConstantF,
4507 d3d9_device_SetPixelShaderConstantI,
4508 d3d9_device_GetPixelShaderConstantI,
4509 d3d9_device_SetPixelShaderConstantB,
4510 d3d9_device_GetPixelShaderConstantB,
4511 d3d9_device_DrawRectPatch,
4512 d3d9_device_DrawTriPatch,
4513 d3d9_device_DeletePatch,
4514 d3d9_device_CreateQuery,
4515 /* IDirect3DDevice9Ex */
4516 d3d9_device_SetConvolutionMonoKernel,
4517 d3d9_device_ComposeRects,
4518 d3d9_device_PresentEx,
4519 d3d9_device_GetGPUThreadPriority,
4520 d3d9_device_SetGPUThreadPriority,
4521 d3d9_device_WaitForVBlank,
4522 d3d9_device_CheckResourceResidency,
4523 d3d9_device_SetMaximumFrameLatency,
4524 d3d9_device_GetMaximumFrameLatency,
4525 d3d9_device_CheckDeviceState,
4526 d3d9_device_CreateRenderTargetEx,
4527 d3d9_device_CreateOffscreenPlainSurfaceEx,
4528 d3d9_device_CreateDepthStencilSurfaceEx,
4529 d3d9_device_ResetEx,
4530 d3d9_device_GetDisplayModeEx,
4533 static inline struct d3d9_device *device_from_device_parent(struct wined3d_device_parent *device_parent)
4535 return CONTAINING_RECORD(device_parent, struct d3d9_device, device_parent);
4538 static void CDECL device_parent_wined3d_device_created(struct wined3d_device_parent *device_parent,
4539 struct wined3d_device *device)
4541 TRACE("device_parent %p, device %p.\n", device_parent, device);
4544 static void CDECL device_parent_mode_changed(struct wined3d_device_parent *device_parent)
4546 TRACE("device_parent %p.\n", device_parent);
4549 static void CDECL device_parent_activate(struct wined3d_device_parent *device_parent, BOOL activate)
4551 struct d3d9_device *device = device_from_device_parent(device_parent);
4553 TRACE("device_parent %p, activate %#x.\n", device_parent, activate);
4555 if (!device->d3d_parent)
4556 return;
4558 if (!activate)
4559 InterlockedCompareExchange(&device->device_state, D3D9_DEVICE_STATE_LOST, D3D9_DEVICE_STATE_OK);
4560 else if (device->d3d_parent->extended)
4561 InterlockedCompareExchange(&device->device_state, D3D9_DEVICE_STATE_OK, D3D9_DEVICE_STATE_LOST);
4562 else
4563 InterlockedCompareExchange(&device->device_state, D3D9_DEVICE_STATE_NOT_RESET, D3D9_DEVICE_STATE_LOST);
4566 static HRESULT CDECL device_parent_texture_sub_resource_created(struct wined3d_device_parent *device_parent,
4567 enum wined3d_resource_type type, struct wined3d_texture *wined3d_texture, unsigned int sub_resource_idx,
4568 void **parent, const struct wined3d_parent_ops **parent_ops)
4570 TRACE("device_parent %p, type %#x, wined3d_texture %p, sub_resource_idx %u, parent %p, parent_ops %p.\n",
4571 device_parent, type, wined3d_texture, sub_resource_idx, parent, parent_ops);
4573 if (type == WINED3D_RTYPE_TEXTURE_3D)
4575 struct d3d9_volume *d3d_volume;
4577 if (!(d3d_volume = calloc(1, sizeof(*d3d_volume))))
4578 return E_OUTOFMEMORY;
4580 volume_init(d3d_volume, wined3d_texture, sub_resource_idx, parent_ops);
4581 *parent = d3d_volume;
4582 TRACE("Created volume %p.\n", d3d_volume);
4584 else if (type != WINED3D_RTYPE_TEXTURE_2D)
4586 ERR("Unhandled resource type %#x.\n", type);
4587 return E_FAIL;
4590 return D3D_OK;
4593 static const struct wined3d_device_parent_ops d3d9_wined3d_device_parent_ops =
4595 device_parent_wined3d_device_created,
4596 device_parent_mode_changed,
4597 device_parent_activate,
4598 device_parent_texture_sub_resource_created,
4601 static void setup_fpu(void)
4603 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
4604 WORD cw;
4605 __asm__ volatile ("fnstcw %0" : "=m" (cw));
4606 cw = (cw & ~0xf3f) | 0x3f;
4607 __asm__ volatile ("fldcw %0" : : "m" (cw));
4608 #elif defined(__i386__) && defined(_MSC_VER)
4609 WORD cw;
4610 __asm fnstcw cw;
4611 cw = (cw & ~0xf3f) | 0x3f;
4612 __asm fldcw cw;
4613 #else
4614 FIXME("FPU setup not implemented for this platform.\n");
4615 #endif
4618 HRESULT device_init(struct d3d9_device *device, struct d3d9 *parent, struct wined3d *wined3d,
4619 UINT adapter, D3DDEVTYPE device_type, HWND focus_window, DWORD flags,
4620 D3DPRESENT_PARAMETERS *parameters, D3DDISPLAYMODEEX *mode)
4622 struct wined3d_swapchain_desc *swapchain_desc;
4623 struct wined3d_adapter *wined3d_adapter;
4624 struct d3d9_swapchain *d3d_swapchain;
4625 struct wined3d_caps wined3d_caps;
4626 unsigned int output_idx;
4627 unsigned i, count = 1;
4628 D3DCAPS9 caps;
4629 HRESULT hr;
4631 static const enum wined3d_feature_level feature_levels[] =
4633 WINED3D_FEATURE_LEVEL_9_3,
4634 WINED3D_FEATURE_LEVEL_9_2,
4635 WINED3D_FEATURE_LEVEL_9_1,
4636 WINED3D_FEATURE_LEVEL_8,
4637 WINED3D_FEATURE_LEVEL_7,
4638 WINED3D_FEATURE_LEVEL_6,
4639 WINED3D_FEATURE_LEVEL_5,
4642 output_idx = adapter;
4643 if (output_idx >= parent->wined3d_output_count)
4644 return D3DERR_INVALIDCALL;
4646 if (mode)
4647 FIXME("Ignoring display mode.\n");
4649 device->IDirect3DDevice9Ex_iface.lpVtbl = &d3d9_device_vtbl;
4650 device->device_parent.ops = &d3d9_wined3d_device_parent_ops;
4651 device->adapter_ordinal = adapter;
4652 device->refcount = 1;
4654 if (!(flags & D3DCREATE_FPU_PRESERVE)) setup_fpu();
4656 wined3d_mutex_lock();
4657 wined3d_adapter = wined3d_output_get_adapter(parent->wined3d_outputs[output_idx]);
4658 if (FAILED(hr = wined3d_device_create(wined3d, wined3d_adapter, wined3d_device_type_from_d3d(device_type),
4659 focus_window, flags, 4, feature_levels, ARRAY_SIZE(feature_levels),
4660 &device->device_parent, &device->wined3d_device)))
4662 WARN("Failed to create wined3d device, hr %#lx.\n", hr);
4663 wined3d_mutex_unlock();
4664 return hr;
4667 device->immediate_context = wined3d_device_get_immediate_context(device->wined3d_device);
4668 wined3d_get_device_caps(wined3d_adapter, wined3d_device_type_from_d3d(device_type), &wined3d_caps);
4669 d3d9_caps_from_wined3dcaps(parent, adapter, &caps, &wined3d_caps);
4670 device->max_user_clip_planes = caps.MaxUserClipPlanes;
4671 device->vs_uniform_count = caps.MaxVertexShaderConst;
4672 if (flags & D3DCREATE_ADAPTERGROUP_DEVICE)
4673 count = caps.NumberOfAdaptersInGroup;
4675 if (FAILED(hr = wined3d_stateblock_create(device->wined3d_device, NULL, WINED3D_SBT_PRIMARY, &device->state)))
4677 ERR("Failed to create the primary stateblock, hr %#lx.\n", hr);
4678 wined3d_device_decref(device->wined3d_device);
4679 wined3d_mutex_unlock();
4680 return hr;
4682 device->stateblock_state = wined3d_stateblock_get_state(device->state);
4683 device->update_state = device->state;
4685 wined3d_streaming_buffer_init(&device->vertex_buffer, WINED3D_BIND_VERTEX_BUFFER);
4686 wined3d_streaming_buffer_init(&device->index_buffer, WINED3D_BIND_INDEX_BUFFER);
4688 if (flags & D3DCREATE_MULTITHREADED)
4689 wined3d_device_set_multithreaded(device->wined3d_device);
4691 if (!parameters->Windowed)
4693 if (!focus_window)
4694 focus_window = parameters->hDeviceWindow;
4695 if (FAILED(hr = wined3d_device_acquire_focus_window(device->wined3d_device, focus_window)))
4697 ERR("Failed to acquire focus window, hr %#lx.\n", hr);
4698 wined3d_device_decref(device->wined3d_device);
4699 wined3d_mutex_unlock();
4700 return hr;
4704 if (!(swapchain_desc = malloc(sizeof(*swapchain_desc) * count)))
4706 ERR("Failed to allocate wined3d parameters.\n");
4707 wined3d_device_release_focus_window(device->wined3d_device);
4708 wined3d_device_decref(device->wined3d_device);
4709 wined3d_mutex_unlock();
4710 return E_OUTOFMEMORY;
4713 for (i = 0; i < count; ++i)
4715 if (!wined3d_swapchain_desc_from_d3d9(&swapchain_desc[i],
4716 parent->wined3d_outputs[output_idx + i], &parameters[i], parent->extended))
4718 wined3d_device_release_focus_window(device->wined3d_device);
4719 wined3d_device_decref(device->wined3d_device);
4720 free(swapchain_desc);
4721 wined3d_mutex_unlock();
4722 return D3DERR_INVALIDCALL;
4724 swapchain_desc[i].flags |= WINED3D_SWAPCHAIN_IMPLICIT;
4725 if (flags & D3DCREATE_NOWINDOWCHANGES)
4726 swapchain_desc[i].flags |= WINED3D_SWAPCHAIN_NO_WINDOW_CHANGES;
4729 if (FAILED(hr = d3d9_swapchain_create(device, swapchain_desc,
4730 wined3dswapinterval_from_d3d(parameters->PresentationInterval), &d3d_swapchain)))
4732 WARN("Failed to create swapchain, hr %#lx.\n", hr);
4733 wined3d_device_release_focus_window(device->wined3d_device);
4734 free(swapchain_desc);
4735 wined3d_device_decref(device->wined3d_device);
4736 wined3d_mutex_unlock();
4737 return hr;
4740 wined3d_swapchain_incref(d3d_swapchain->wined3d_swapchain);
4741 IDirect3DSwapChain9Ex_Release(&d3d_swapchain->IDirect3DSwapChain9Ex_iface);
4743 wined3d_stateblock_set_render_state(device->state, WINED3D_RS_ZENABLE,
4744 !!swapchain_desc->enable_auto_depth_stencil);
4745 device_reset_viewport_state(device);
4747 if (FAILED(hr = d3d9_device_get_swapchains(device)))
4749 wined3d_swapchain_decref(d3d_swapchain->wined3d_swapchain);
4750 wined3d_device_release_focus_window(device->wined3d_device);
4751 wined3d_device_decref(device->wined3d_device);
4752 free(swapchain_desc);
4753 wined3d_mutex_unlock();
4754 return E_OUTOFMEMORY;
4757 for (i = 0; i < count; ++i)
4759 present_parameters_from_wined3d_swapchain_desc(&parameters[i],
4760 &swapchain_desc[i], parameters[i].PresentationInterval);
4763 wined3d_mutex_unlock();
4765 free(swapchain_desc);
4767 /* We could also simply ignore the initial rendertarget since it's known
4768 * not to be a texture (we currently use these only for automatic mipmap
4769 * generation). */
4770 wined3d_mutex_lock();
4771 device->render_targets[0] = wined3d_rendertarget_view_get_sub_resource_parent(
4772 wined3d_device_context_get_rendertarget_view(device->immediate_context, 0));
4773 wined3d_mutex_unlock();
4775 IDirect3D9Ex_AddRef(&parent->IDirect3D9Ex_iface);
4776 device->d3d_parent = parent;
4778 return D3D_OK;