cmd: DIR command outputs free space for the path.
[wine.git] / dlls / d3d9 / device.c
blob4351f005eb922194f3fc0ba13e8ba9154685f8dd
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 heap_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 heap_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 heap_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 = heap_alloc(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 heap_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 = heap_alloc_zero(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 heap_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 = heap_alloc_zero(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 heap_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 = heap_alloc_zero(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 heap_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 = heap_alloc_zero(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 heap_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 = heap_alloc_zero(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 heap_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 = heap_alloc_zero(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 heap_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 = heap_alloc_zero(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 heap_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 wined3d_device_apply_stateblock(device->wined3d_device, device->state);
2903 hr = wined3d_device_validate_device(device->wined3d_device, pass_count);
2904 wined3d_mutex_unlock();
2906 return hr;
2909 static HRESULT WINAPI d3d9_device_SetPaletteEntries(IDirect3DDevice9Ex *iface,
2910 UINT palette_idx, const PALETTEENTRY *entries)
2912 WARN("iface %p, palette_idx %u, entries %p unimplemented.\n", iface, palette_idx, entries);
2914 /* The d3d9 palette API is non-functional on Windows. Getters and setters are implemented,
2915 * and some drivers allow the creation of P8 surfaces. These surfaces can be copied to
2916 * other P8 surfaces with StretchRect, but cannot be converted to (A)RGB.
2918 * Some older(dx7) cards may have support for P8 textures, but games cannot rely on this. */
2919 return D3D_OK;
2922 static HRESULT WINAPI d3d9_device_GetPaletteEntries(IDirect3DDevice9Ex *iface,
2923 UINT palette_idx, PALETTEENTRY *entries)
2925 FIXME("iface %p, palette_idx %u, entries %p unimplemented.\n", iface, palette_idx, entries);
2927 return D3DERR_INVALIDCALL;
2930 static HRESULT WINAPI d3d9_device_SetCurrentTexturePalette(IDirect3DDevice9Ex *iface, UINT palette_idx)
2932 WARN("iface %p, palette_idx %u unimplemented.\n", iface, palette_idx);
2934 return D3D_OK;
2937 static HRESULT WINAPI d3d9_device_GetCurrentTexturePalette(IDirect3DDevice9Ex *iface, UINT *palette_idx)
2939 FIXME("iface %p, palette_idx %p.\n", iface, palette_idx);
2941 return D3DERR_INVALIDCALL;
2944 static HRESULT WINAPI d3d9_device_SetScissorRect(IDirect3DDevice9Ex *iface, const RECT *rect)
2946 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2948 TRACE("iface %p, rect %p.\n", iface, rect);
2950 wined3d_mutex_lock();
2951 wined3d_stateblock_set_scissor_rect(device->update_state, rect);
2952 wined3d_mutex_unlock();
2954 return D3D_OK;
2957 static HRESULT WINAPI d3d9_device_GetScissorRect(IDirect3DDevice9Ex *iface, RECT *rect)
2959 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2961 TRACE("iface %p, rect %p.\n", iface, rect);
2963 wined3d_mutex_lock();
2964 *rect = device->stateblock_state->scissor_rect;
2965 wined3d_mutex_unlock();
2967 return D3D_OK;
2970 static HRESULT WINAPI d3d9_device_SetSoftwareVertexProcessing(IDirect3DDevice9Ex *iface, BOOL software)
2972 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2974 TRACE("iface %p, software %#x.\n", iface, software);
2976 wined3d_mutex_lock();
2977 wined3d_device_set_software_vertex_processing(device->wined3d_device, software);
2978 wined3d_mutex_unlock();
2980 return D3D_OK;
2983 static BOOL WINAPI d3d9_device_GetSoftwareVertexProcessing(IDirect3DDevice9Ex *iface)
2985 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2986 BOOL ret;
2988 TRACE("iface %p.\n", iface);
2990 wined3d_mutex_lock();
2991 ret = wined3d_device_get_software_vertex_processing(device->wined3d_device);
2992 wined3d_mutex_unlock();
2994 return ret;
2997 static HRESULT WINAPI d3d9_device_SetNPatchMode(IDirect3DDevice9Ex *iface, float segment_count)
2999 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3000 HRESULT hr;
3002 TRACE("iface %p, segment_count %.8e.\n", iface, segment_count);
3004 wined3d_mutex_lock();
3005 hr = wined3d_device_set_npatch_mode(device->wined3d_device, segment_count);
3006 wined3d_mutex_unlock();
3008 return hr;
3011 static float WINAPI d3d9_device_GetNPatchMode(IDirect3DDevice9Ex *iface)
3013 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3014 float ret;
3016 TRACE("iface %p.\n", iface);
3018 wined3d_mutex_lock();
3019 ret = wined3d_device_get_npatch_mode(device->wined3d_device);
3020 wined3d_mutex_unlock();
3022 return ret;
3025 /* wined3d critical section must be taken by the caller. */
3026 static void d3d9_generate_auto_mipmaps(struct d3d9_device *device)
3028 const struct wined3d_stateblock_state *state = device->stateblock_state;
3029 struct wined3d_texture *texture;
3030 unsigned int i, map;
3032 map = device->auto_mipmaps;
3033 while (map)
3035 i = wined3d_bit_scan(&map);
3036 if ((texture = state->textures[i]))
3037 d3d9_texture_gen_auto_mipmap(wined3d_texture_get_parent(texture));
3041 static void d3d9_device_upload_sysmem_vertex_buffers(struct d3d9_device *device,
3042 int base_vertex, unsigned int start_vertex, unsigned int vertex_count)
3044 const struct wined3d_stateblock_state *state = device->stateblock_state;
3045 struct wined3d_vertex_declaration *wined3d_decl;
3046 struct wined3d_box box = {0, 0, 0, 1, 0, 1};
3047 const struct wined3d_stream_state *stream;
3048 struct d3d9_vertexbuffer *d3d9_buffer;
3049 struct wined3d_resource *dst_resource;
3050 struct d3d9_vertex_declaration *decl;
3051 struct wined3d_buffer *dst_buffer;
3052 struct wined3d_resource_desc desc;
3053 unsigned int stride, map;
3054 HRESULT hr;
3056 if (!device->sysmem_vb)
3057 return;
3058 wined3d_decl = state->vertex_declaration;
3059 if (!wined3d_decl)
3060 return;
3062 if (base_vertex >= 0 || start_vertex >= -base_vertex)
3063 start_vertex += base_vertex;
3064 else
3065 FIXME("System memory vertex data offset is negative.\n");
3067 decl = wined3d_vertex_declaration_get_parent(wined3d_decl);
3068 map = decl->stream_map & device->sysmem_vb;
3069 while (map)
3071 stream = &state->streams[wined3d_bit_scan(&map)];
3072 dst_buffer = stream->buffer;
3073 stride = stream->stride;
3075 d3d9_buffer = wined3d_buffer_get_parent(dst_buffer);
3076 dst_resource = wined3d_buffer_get_resource(dst_buffer);
3077 wined3d_resource_get_desc(dst_resource, &desc);
3078 box.left = stream->offset + start_vertex * stride;
3079 box.right = min(box.left + vertex_count * stride, desc.size);
3080 if (FAILED(hr = wined3d_device_context_copy_sub_resource_region(device->immediate_context,
3081 dst_resource, 0, box.left, 0, 0,
3082 wined3d_buffer_get_resource(d3d9_buffer->wined3d_buffer), 0, &box, 0)))
3083 ERR("Failed to update buffer.\n");
3087 static HRESULT d3d9_device_upload_sysmem_index_buffer(struct d3d9_device *device,
3088 unsigned int *start_idx, unsigned int idx_count)
3090 const struct wined3d_stateblock_state *state = device->stateblock_state;
3091 unsigned int src_offset, idx_size, buffer_size, pos;
3092 struct wined3d_resource_desc resource_desc;
3093 struct wined3d_resource *index_buffer;
3094 struct wined3d_map_desc map_desc;
3095 struct wined3d_box box;
3096 HRESULT hr;
3098 if (!device->sysmem_ib)
3099 return S_OK;
3101 index_buffer = wined3d_buffer_get_resource(state->index_buffer);
3102 wined3d_resource_get_desc(index_buffer, &resource_desc);
3103 idx_size = (state->index_format == WINED3DFMT_R16_UINT) ? 2 : 4;
3105 src_offset = (*start_idx) * idx_size;
3106 buffer_size = min(idx_count * idx_size, resource_desc.size - src_offset);
3108 wined3d_box_set(&box, src_offset, 0, buffer_size, 1, 0, 1);
3109 if (FAILED(hr = wined3d_resource_map(index_buffer, 0, &map_desc, &box, WINED3D_MAP_READ)))
3111 ERR("Failed to map index buffer, hr %#lx.\n", hr);
3112 return hr;
3114 wined3d_streaming_buffer_upload(device->wined3d_device, &device->index_buffer,
3115 map_desc.data, buffer_size, idx_size, &pos);
3116 wined3d_resource_unmap(index_buffer, 0);
3118 wined3d_device_context_set_index_buffer(device->immediate_context,
3119 device->index_buffer.buffer, state->index_format, pos);
3120 *start_idx = 0;
3121 return S_OK;
3124 static void d3d9_device_upload_managed_textures(struct d3d9_device *device)
3126 const struct wined3d_stateblock_state *state = device->stateblock_state;
3127 unsigned int i;
3129 for (i = 0; i < WINED3D_MAX_COMBINED_SAMPLERS; ++i)
3131 struct wined3d_texture *wined3d_texture = state->textures[i];
3132 struct d3d9_texture *d3d9_texture;
3134 if (!wined3d_texture)
3135 continue;
3136 d3d9_texture = wined3d_texture_get_parent(wined3d_texture);
3137 if (d3d9_texture->draw_texture)
3138 wined3d_device_update_texture(device->wined3d_device,
3139 d3d9_texture->wined3d_texture, d3d9_texture->draw_texture);
3143 static HRESULT WINAPI d3d9_device_DrawPrimitive(IDirect3DDevice9Ex *iface,
3144 D3DPRIMITIVETYPE primitive_type, UINT start_vertex, UINT primitive_count)
3146 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3147 unsigned int vertex_count;
3149 TRACE("iface %p, primitive_type %#x, start_vertex %u, primitive_count %u.\n",
3150 iface, primitive_type, start_vertex, primitive_count);
3152 wined3d_mutex_lock();
3153 if (!device->stateblock_state->vertex_declaration)
3155 wined3d_mutex_unlock();
3156 WARN("Called without a valid vertex declaration set.\n");
3157 return D3DERR_INVALIDCALL;
3159 wined3d_device_apply_stateblock(device->wined3d_device, device->state);
3160 vertex_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
3161 d3d9_device_upload_managed_textures(device);
3162 d3d9_device_upload_sysmem_vertex_buffers(device, 0, start_vertex, vertex_count);
3163 d3d9_generate_auto_mipmaps(device);
3164 wined3d_device_context_set_primitive_type(device->immediate_context,
3165 wined3d_primitive_type_from_d3d(primitive_type), 0);
3167 /* Instancing is ignored for non-indexed draws. */
3168 wined3d_device_context_draw(device->immediate_context, start_vertex, vertex_count, 0, 1);
3170 d3d9_rts_flag_auto_gen_mipmap(device);
3171 wined3d_mutex_unlock();
3173 return D3D_OK;
3176 static HRESULT WINAPI d3d9_device_DrawIndexedPrimitive(IDirect3DDevice9Ex *iface,
3177 D3DPRIMITIVETYPE primitive_type, INT base_vertex_idx, UINT min_vertex_idx,
3178 UINT vertex_count, UINT start_idx, UINT primitive_count)
3180 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3181 unsigned int index_count;
3183 TRACE("iface %p, primitive_type %#x, base_vertex_idx %u, min_vertex_idx %u, "
3184 "vertex_count %u, start_idx %u, primitive_count %u.\n",
3185 iface, primitive_type, base_vertex_idx, min_vertex_idx,
3186 vertex_count, start_idx, primitive_count);
3188 wined3d_mutex_lock();
3189 if (!device->stateblock_state->vertex_declaration)
3191 wined3d_mutex_unlock();
3192 WARN("Called without a valid vertex declaration set.\n");
3193 return D3DERR_INVALIDCALL;
3195 if (!device->stateblock_state->index_buffer)
3197 wined3d_mutex_unlock();
3198 WARN("Called without a valid index buffer set.\n");
3199 return D3DERR_INVALIDCALL;
3201 index_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
3202 d3d9_device_upload_managed_textures(device);
3203 d3d9_device_upload_sysmem_vertex_buffers(device, base_vertex_idx, min_vertex_idx, vertex_count);
3204 d3d9_generate_auto_mipmaps(device);
3205 wined3d_device_context_set_primitive_type(device->immediate_context,
3206 wined3d_primitive_type_from_d3d(primitive_type), 0);
3207 wined3d_device_apply_stateblock(device->wined3d_device, device->state);
3208 d3d9_device_upload_sysmem_index_buffer(device, &start_idx, index_count);
3209 wined3d_device_context_draw_indexed(device->immediate_context, base_vertex_idx, start_idx, index_count, 0,
3210 device->stateblock_state->streams[0].frequency);
3211 d3d9_rts_flag_auto_gen_mipmap(device);
3212 wined3d_mutex_unlock();
3214 return D3D_OK;
3217 static HRESULT WINAPI d3d9_device_DrawPrimitiveUP(IDirect3DDevice9Ex *iface,
3218 D3DPRIMITIVETYPE primitive_type, UINT primitive_count, const void *data, UINT stride)
3220 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3221 UINT vtx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
3222 UINT size = vtx_count * stride;
3223 unsigned int vb_pos;
3224 HRESULT hr;
3226 TRACE("iface %p, primitive_type %#x, primitive_count %u, data %p, stride %u.\n",
3227 iface, primitive_type, primitive_count, data, stride);
3229 if (!stride)
3231 WARN("stride is 0, returning D3DERR_INVALIDCALL.\n");
3232 return D3DERR_INVALIDCALL;
3234 if (!primitive_count)
3236 WARN("primitive_count is 0, returning D3D_OK.\n");
3237 return D3D_OK;
3240 wined3d_mutex_lock();
3242 if (!device->stateblock_state->vertex_declaration)
3244 wined3d_mutex_unlock();
3245 WARN("Called without a valid vertex declaration set.\n");
3246 return D3DERR_INVALIDCALL;
3249 if (FAILED(hr = wined3d_streaming_buffer_upload(device->wined3d_device,
3250 &device->vertex_buffer, data, size, stride, &vb_pos)))
3251 goto done;
3253 hr = wined3d_stateblock_set_stream_source(device->state, 0, device->vertex_buffer.buffer, 0, stride);
3254 if (FAILED(hr))
3255 goto done;
3257 d3d9_generate_auto_mipmaps(device);
3258 d3d9_device_upload_managed_textures(device);
3259 wined3d_device_context_set_primitive_type(device->immediate_context,
3260 wined3d_primitive_type_from_d3d(primitive_type), 0);
3261 wined3d_device_apply_stateblock(device->wined3d_device, device->state);
3263 /* Instancing is ignored for non-indexed draws. */
3264 wined3d_device_context_draw(device->immediate_context, vb_pos / stride, vtx_count, 0, 1);
3266 wined3d_stateblock_set_stream_source(device->state, 0, NULL, 0, 0);
3267 d3d9_rts_flag_auto_gen_mipmap(device);
3269 done:
3270 wined3d_mutex_unlock();
3271 return hr;
3274 static HRESULT WINAPI d3d9_device_DrawIndexedPrimitiveUP(IDirect3DDevice9Ex *iface,
3275 D3DPRIMITIVETYPE primitive_type, UINT min_vertex_idx, UINT vertex_count,
3276 UINT primitive_count, const void *index_data, D3DFORMAT index_format,
3277 const void *vertex_data, UINT vertex_stride)
3279 UINT idx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
3280 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3281 UINT idx_fmt_size = index_format == D3DFMT_INDEX16 ? 2 : 4;
3282 UINT vtx_size = vertex_count * vertex_stride;
3283 UINT idx_size = idx_count * idx_fmt_size;
3284 UINT vb_pos, ib_pos;
3285 HRESULT hr;
3287 TRACE("iface %p, primitive_type %#x, min_vertex_idx %u, vertex_count %u, primitive_count %u, "
3288 "index_data %p, index_format %#x, vertex_data %p, vertex_stride %u.\n",
3289 iface, primitive_type, min_vertex_idx, vertex_count, primitive_count,
3290 index_data, index_format, vertex_data, vertex_stride);
3292 if (!vertex_stride)
3294 WARN("vertex_stride is 0, returning D3DERR_INVALIDCALL.\n");
3295 return D3DERR_INVALIDCALL;
3297 if (!primitive_count)
3299 WARN("primitive_count is 0, returning D3D_OK.\n");
3300 return D3D_OK;
3303 wined3d_mutex_lock();
3305 if (!device->stateblock_state->vertex_declaration)
3307 wined3d_mutex_unlock();
3308 WARN("Called without a valid vertex declaration set.\n");
3309 return D3DERR_INVALIDCALL;
3312 if (FAILED(hr = wined3d_streaming_buffer_upload(device->wined3d_device, &device->vertex_buffer,
3313 (char *)vertex_data + min_vertex_idx * vertex_stride, vtx_size, vertex_stride, &vb_pos)))
3314 goto done;
3316 if (FAILED(hr = wined3d_streaming_buffer_upload(device->wined3d_device, &device->index_buffer,
3317 index_data, idx_size, idx_fmt_size, &ib_pos)))
3318 goto done;
3320 hr = wined3d_stateblock_set_stream_source(device->state, 0, device->vertex_buffer.buffer, 0, vertex_stride);
3321 if (FAILED(hr))
3322 goto done;
3323 wined3d_stateblock_set_index_buffer(device->state, device->index_buffer.buffer,
3324 wined3dformat_from_d3dformat(index_format));
3326 d3d9_generate_auto_mipmaps(device);
3327 d3d9_device_upload_managed_textures(device);
3328 wined3d_device_apply_stateblock(device->wined3d_device, device->state);
3329 wined3d_device_context_set_primitive_type(device->immediate_context,
3330 wined3d_primitive_type_from_d3d(primitive_type), 0);
3331 wined3d_device_context_draw_indexed(device->immediate_context,
3332 vb_pos / vertex_stride - min_vertex_idx, ib_pos / idx_fmt_size, idx_count, 0,
3333 device->stateblock_state->streams[0].frequency);
3335 wined3d_stateblock_set_stream_source(device->state, 0, NULL, 0, 0);
3336 wined3d_stateblock_set_index_buffer(device->state, NULL, WINED3DFMT_UNKNOWN);
3338 d3d9_rts_flag_auto_gen_mipmap(device);
3340 done:
3341 wined3d_mutex_unlock();
3342 return hr;
3345 static HRESULT WINAPI d3d9_device_ProcessVertices(IDirect3DDevice9Ex *iface,
3346 UINT src_start_idx, UINT dst_idx, UINT vertex_count, IDirect3DVertexBuffer9 *dst_buffer,
3347 IDirect3DVertexDeclaration9 *declaration, DWORD flags)
3349 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3350 struct d3d9_vertexbuffer *dst_impl = unsafe_impl_from_IDirect3DVertexBuffer9(dst_buffer);
3351 struct d3d9_vertex_declaration *decl_impl = unsafe_impl_from_IDirect3DVertexDeclaration9(declaration);
3352 const struct wined3d_stateblock_state *state;
3353 const struct wined3d_stream_state *stream;
3354 struct d3d9_vertexbuffer *d3d9_buffer;
3355 unsigned int i, map;
3356 HRESULT hr;
3358 TRACE("iface %p, src_start_idx %u, dst_idx %u, vertex_count %u, dst_buffer %p, declaration %p, flags %#lx.\n",
3359 iface, src_start_idx, dst_idx, vertex_count, dst_buffer, declaration, flags);
3361 wined3d_mutex_lock();
3362 state = device->stateblock_state;
3364 /* Note that an alternative approach would be to simply create these
3365 * buffers with WINED3D_RESOURCE_ACCESS_MAP_R and update them here like we
3366 * do for draws. In some regards that would be easier, but it seems less
3367 * than optimal to upload data to the GPU only to subsequently download it
3368 * again. */
3369 map = device->sysmem_vb;
3370 while (map)
3372 i = wined3d_bit_scan(&map);
3373 stream = &state->streams[i];
3375 d3d9_buffer = wined3d_buffer_get_parent(stream->buffer);
3376 if (FAILED(wined3d_stateblock_set_stream_source(device->state,
3377 i, d3d9_buffer->wined3d_buffer, stream->offset, stream->stride)))
3378 ERR("Failed to set stream source.\n");
3381 wined3d_device_apply_stateblock(device->wined3d_device, device->state);
3382 hr = wined3d_device_process_vertices(device->wined3d_device, src_start_idx, dst_idx, vertex_count,
3383 dst_impl->wined3d_buffer, decl_impl ? decl_impl->wined3d_declaration : NULL,
3384 flags, dst_impl->fvf);
3386 map = device->sysmem_vb;
3387 while (map)
3389 i = wined3d_bit_scan(&map);
3390 stream = &state->streams[i];
3392 d3d9_buffer = wined3d_buffer_get_parent(stream->buffer);
3393 if (FAILED(wined3d_stateblock_set_stream_source(device->state,
3394 i, d3d9_buffer->draw_buffer, stream->offset, stream->stride)))
3395 ERR("Failed to set stream source.\n");
3398 wined3d_mutex_unlock();
3400 return hr;
3403 static HRESULT WINAPI d3d9_device_CreateVertexDeclaration(IDirect3DDevice9Ex *iface,
3404 const D3DVERTEXELEMENT9 *elements, IDirect3DVertexDeclaration9 **declaration)
3406 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3407 struct d3d9_vertex_declaration *object;
3408 HRESULT hr;
3410 TRACE("iface %p, elements %p, declaration %p.\n", iface, elements, declaration);
3412 if (!declaration)
3414 WARN("Caller passed a NULL declaration, returning D3DERR_INVALIDCALL.\n");
3415 return D3DERR_INVALIDCALL;
3418 if (SUCCEEDED(hr = d3d9_vertex_declaration_create(device, elements, &object)))
3419 *declaration = &object->IDirect3DVertexDeclaration9_iface;
3421 return hr;
3424 static HRESULT WINAPI d3d9_device_SetVertexDeclaration(IDirect3DDevice9Ex *iface,
3425 IDirect3DVertexDeclaration9 *declaration)
3427 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3428 struct d3d9_vertex_declaration *decl_impl = unsafe_impl_from_IDirect3DVertexDeclaration9(declaration);
3430 TRACE("iface %p, declaration %p.\n", iface, declaration);
3432 wined3d_mutex_lock();
3433 wined3d_stateblock_set_vertex_declaration(device->update_state,
3434 decl_impl ? decl_impl->wined3d_declaration : NULL);
3435 wined3d_mutex_unlock();
3437 return D3D_OK;
3440 static HRESULT WINAPI d3d9_device_GetVertexDeclaration(IDirect3DDevice9Ex *iface,
3441 IDirect3DVertexDeclaration9 **declaration)
3443 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3444 struct wined3d_vertex_declaration *wined3d_declaration;
3445 struct d3d9_vertex_declaration *declaration_impl;
3447 TRACE("iface %p, declaration %p.\n", iface, declaration);
3449 if (!declaration) return D3DERR_INVALIDCALL;
3451 wined3d_mutex_lock();
3452 if ((wined3d_declaration = device->stateblock_state->vertex_declaration))
3454 declaration_impl = wined3d_vertex_declaration_get_parent(wined3d_declaration);
3455 *declaration = &declaration_impl->IDirect3DVertexDeclaration9_iface;
3456 IDirect3DVertexDeclaration9_AddRef(*declaration);
3458 else
3460 *declaration = NULL;
3462 wined3d_mutex_unlock();
3464 TRACE("Returning %p.\n", *declaration);
3465 return D3D_OK;
3468 static struct wined3d_vertex_declaration *device_get_fvf_declaration(struct d3d9_device *device, DWORD fvf)
3470 struct wined3d_vertex_declaration *wined3d_declaration;
3471 struct fvf_declaration *fvf_decls = device->fvf_decls;
3472 struct d3d9_vertex_declaration *d3d9_declaration;
3473 D3DVERTEXELEMENT9 *elements;
3474 int p, low, high; /* deliberately signed */
3475 HRESULT hr;
3477 TRACE("Searching for declaration for fvf %08lx... ", fvf);
3479 low = 0;
3480 high = device->fvf_decl_count - 1;
3481 while (low <= high)
3483 p = (low + high) >> 1;
3484 TRACE("%d ", p);
3486 if (fvf_decls[p].fvf == fvf)
3488 TRACE("found %p.\n", fvf_decls[p].decl);
3489 return fvf_decls[p].decl;
3492 if (fvf_decls[p].fvf < fvf)
3493 low = p + 1;
3494 else
3495 high = p - 1;
3497 TRACE("not found. Creating and inserting at position %d.\n", low);
3499 if (FAILED(hr = vdecl_convert_fvf(fvf, &elements)))
3500 return NULL;
3502 hr = d3d9_vertex_declaration_create(device, elements, &d3d9_declaration);
3503 heap_free(elements);
3504 if (FAILED(hr))
3505 return NULL;
3507 if (device->fvf_decl_size == device->fvf_decl_count)
3509 UINT grow = max(device->fvf_decl_size / 2, 8);
3511 if (!(fvf_decls = heap_realloc(fvf_decls, sizeof(*fvf_decls) * (device->fvf_decl_size + grow))))
3513 IDirect3DVertexDeclaration9_Release(&d3d9_declaration->IDirect3DVertexDeclaration9_iface);
3514 return NULL;
3516 device->fvf_decls = fvf_decls;
3517 device->fvf_decl_size += grow;
3520 d3d9_declaration->fvf = fvf;
3521 wined3d_declaration = d3d9_declaration->wined3d_declaration;
3522 wined3d_vertex_declaration_incref(wined3d_declaration);
3523 IDirect3DVertexDeclaration9_Release(&d3d9_declaration->IDirect3DVertexDeclaration9_iface);
3525 memmove(fvf_decls + low + 1, fvf_decls + low, sizeof(*fvf_decls) * (device->fvf_decl_count - low));
3526 fvf_decls[low].decl = wined3d_declaration;
3527 fvf_decls[low].fvf = fvf;
3528 ++device->fvf_decl_count;
3530 TRACE("Returning %p. %u declarations in array.\n", wined3d_declaration, device->fvf_decl_count);
3532 return wined3d_declaration;
3535 static HRESULT WINAPI d3d9_device_SetFVF(IDirect3DDevice9Ex *iface, DWORD fvf)
3537 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3538 struct wined3d_vertex_declaration *decl;
3540 TRACE("iface %p, fvf %#lx.\n", iface, fvf);
3542 if (!fvf)
3544 WARN("%#lx is not a valid FVF.\n", fvf);
3545 return D3D_OK;
3548 wined3d_mutex_lock();
3549 if (!(decl = device_get_fvf_declaration(device, fvf)))
3551 wined3d_mutex_unlock();
3552 ERR("Failed to create a vertex declaration for fvf %#lx.\n", fvf);
3553 return D3DERR_DRIVERINTERNALERROR;
3556 wined3d_stateblock_set_vertex_declaration(device->update_state, decl);
3557 wined3d_mutex_unlock();
3559 return D3D_OK;
3562 static HRESULT WINAPI d3d9_device_GetFVF(IDirect3DDevice9Ex *iface, DWORD *fvf)
3564 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3565 struct wined3d_vertex_declaration *wined3d_declaration;
3566 struct d3d9_vertex_declaration *d3d9_declaration;
3568 TRACE("iface %p, fvf %p.\n", iface, fvf);
3570 wined3d_mutex_lock();
3571 if ((wined3d_declaration = device->stateblock_state->vertex_declaration))
3573 d3d9_declaration = wined3d_vertex_declaration_get_parent(wined3d_declaration);
3574 *fvf = d3d9_declaration->fvf;
3576 else
3578 *fvf = 0;
3580 wined3d_mutex_unlock();
3582 TRACE("Returning FVF %#lx.\n", *fvf);
3584 return D3D_OK;
3587 static HRESULT WINAPI d3d9_device_CreateVertexShader(IDirect3DDevice9Ex *iface,
3588 const DWORD *byte_code, IDirect3DVertexShader9 **shader)
3590 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3591 struct d3d9_vertexshader *object;
3592 HRESULT hr;
3594 TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
3596 if (!(object = heap_alloc_zero(sizeof(*object))))
3597 return E_OUTOFMEMORY;
3599 hr = vertexshader_init(object, device, byte_code);
3600 if (FAILED(hr))
3602 WARN("Failed to initialize vertex shader, hr %#lx.\n", hr);
3603 heap_free(object);
3604 return hr;
3607 TRACE("Created vertex shader %p.\n", object);
3608 *shader = &object->IDirect3DVertexShader9_iface;
3610 return D3D_OK;
3613 static HRESULT WINAPI d3d9_device_SetVertexShader(IDirect3DDevice9Ex *iface, IDirect3DVertexShader9 *shader)
3615 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3616 struct d3d9_vertexshader *shader_obj = unsafe_impl_from_IDirect3DVertexShader9(shader);
3618 TRACE("iface %p, shader %p.\n", iface, shader);
3620 wined3d_mutex_lock();
3621 wined3d_stateblock_set_vertex_shader(device->update_state,
3622 shader_obj ? shader_obj->wined3d_shader : NULL);
3623 wined3d_mutex_unlock();
3625 return D3D_OK;
3628 static HRESULT WINAPI d3d9_device_GetVertexShader(IDirect3DDevice9Ex *iface, IDirect3DVertexShader9 **shader)
3630 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3631 struct d3d9_vertexshader *shader_impl;
3632 struct wined3d_shader *wined3d_shader;
3634 TRACE("iface %p, shader %p.\n", iface, shader);
3636 wined3d_mutex_lock();
3637 if ((wined3d_shader = device->stateblock_state->vs))
3639 shader_impl = wined3d_shader_get_parent(wined3d_shader);
3640 *shader = &shader_impl->IDirect3DVertexShader9_iface;
3641 IDirect3DVertexShader9_AddRef(*shader);
3643 else
3645 *shader = NULL;
3647 wined3d_mutex_unlock();
3649 TRACE("Returning %p.\n", *shader);
3651 return D3D_OK;
3654 static HRESULT WINAPI d3d9_device_SetVertexShaderConstantF(IDirect3DDevice9Ex *iface,
3655 UINT reg_idx, const float *data, UINT count)
3657 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3658 HRESULT hr;
3660 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3662 if (reg_idx + count > D3D9_MAX_VERTEX_SHADER_CONSTANTF)
3664 WARN("Trying to access %u constants, but d3d9 only supports %u\n",
3665 reg_idx + count, D3D9_MAX_VERTEX_SHADER_CONSTANTF);
3666 return D3DERR_INVALIDCALL;
3669 wined3d_mutex_lock();
3670 hr = wined3d_stateblock_set_vs_consts_f(device->update_state, reg_idx,
3671 count, (const struct wined3d_vec4 *)data);
3672 wined3d_mutex_unlock();
3674 return hr;
3677 static HRESULT WINAPI d3d9_device_GetVertexShaderConstantF(IDirect3DDevice9Ex *iface,
3678 UINT start_idx, float *constants, UINT count)
3680 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3681 HRESULT hr;
3683 TRACE("iface %p, start_idx %u, constants %p, count %u.\n", iface, start_idx, constants, count);
3685 wined3d_mutex_lock();
3686 hr = wined3d_stateblock_get_vs_consts_f(device->state, start_idx, count, (struct wined3d_vec4 *)constants);
3687 wined3d_mutex_unlock();
3689 return hr;
3692 static HRESULT WINAPI d3d9_device_SetVertexShaderConstantI(IDirect3DDevice9Ex *iface,
3693 UINT reg_idx, const int *data, UINT count)
3695 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3696 HRESULT hr;
3698 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3700 wined3d_mutex_lock();
3701 hr = wined3d_stateblock_set_vs_consts_i(device->update_state,
3702 reg_idx, count, (const struct wined3d_ivec4 *)data);
3703 wined3d_mutex_unlock();
3705 return hr;
3708 static HRESULT WINAPI d3d9_device_GetVertexShaderConstantI(IDirect3DDevice9Ex *iface,
3709 UINT start_idx, int *constants, UINT count)
3711 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3712 HRESULT hr;
3714 TRACE("iface %p, start_idx %u, constants %p, count %u.\n", iface, start_idx, constants, count);
3716 wined3d_mutex_lock();
3717 hr = wined3d_stateblock_get_vs_consts_i(device->state, start_idx, count, (struct wined3d_ivec4 *)constants);
3718 wined3d_mutex_unlock();
3720 return hr;
3723 static HRESULT WINAPI d3d9_device_SetVertexShaderConstantB(IDirect3DDevice9Ex *iface,
3724 UINT reg_idx, const BOOL *data, UINT count)
3726 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3727 HRESULT hr;
3729 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3731 wined3d_mutex_lock();
3732 hr = wined3d_stateblock_set_vs_consts_b(device->update_state, reg_idx, count, data);
3733 wined3d_mutex_unlock();
3735 return hr;
3738 static HRESULT WINAPI d3d9_device_GetVertexShaderConstantB(IDirect3DDevice9Ex *iface,
3739 UINT start_idx, BOOL *constants, UINT count)
3741 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3742 HRESULT hr;
3744 TRACE("iface %p, start_idx %u, constants %p, count %u.\n", iface, start_idx, constants, count);
3746 wined3d_mutex_lock();
3747 hr = wined3d_stateblock_get_vs_consts_b(device->state, start_idx, count, constants);
3748 wined3d_mutex_unlock();
3750 return hr;
3753 static HRESULT WINAPI d3d9_device_SetStreamSource(IDirect3DDevice9Ex *iface,
3754 UINT stream_idx, IDirect3DVertexBuffer9 *buffer, UINT offset, UINT stride)
3756 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3757 struct d3d9_vertexbuffer *buffer_impl = unsafe_impl_from_IDirect3DVertexBuffer9(buffer);
3758 struct wined3d_buffer *wined3d_buffer;
3759 HRESULT hr;
3761 TRACE("iface %p, stream_idx %u, buffer %p, offset %u, stride %u.\n",
3762 iface, stream_idx, buffer, offset, stride);
3764 if (stream_idx >= ARRAY_SIZE(device->stateblock_state->streams))
3766 WARN("Stream index %u out of range.\n", stream_idx);
3767 return WINED3DERR_INVALIDCALL;
3770 wined3d_mutex_lock();
3771 if (!buffer_impl)
3773 const struct wined3d_stream_state *stream = &device->stateblock_state->streams[stream_idx];
3775 offset = stream->offset;
3776 stride = stream->stride;
3777 wined3d_buffer = NULL;
3779 else if (buffer_impl->draw_buffer)
3781 wined3d_buffer = buffer_impl->draw_buffer;
3783 else
3785 wined3d_buffer = buffer_impl->wined3d_buffer;
3788 hr = wined3d_stateblock_set_stream_source(device->update_state, stream_idx, wined3d_buffer, offset, stride);
3789 if (SUCCEEDED(hr) && !device->recording)
3791 if (buffer_impl && buffer_impl->draw_buffer)
3792 device->sysmem_vb |= (1u << stream_idx);
3793 else
3794 device->sysmem_vb &= ~(1u << stream_idx);
3797 wined3d_mutex_unlock();
3799 return hr;
3802 static HRESULT WINAPI d3d9_device_GetStreamSource(IDirect3DDevice9Ex *iface,
3803 UINT stream_idx, IDirect3DVertexBuffer9 **buffer, UINT *offset, UINT *stride)
3805 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3806 const struct wined3d_stateblock_state *state;
3807 const struct wined3d_stream_state *stream;
3808 struct d3d9_vertexbuffer *buffer_impl;
3810 TRACE("iface %p, stream_idx %u, buffer %p, offset %p, stride %p.\n",
3811 iface, stream_idx, buffer, offset, stride);
3813 if (!buffer)
3814 return D3DERR_INVALIDCALL;
3816 if (stream_idx >= ARRAY_SIZE(state->streams))
3818 WARN("Stream index %u out of range.\n", stream_idx);
3819 return WINED3DERR_INVALIDCALL;
3822 wined3d_mutex_lock();
3823 stream = &device->stateblock_state->streams[stream_idx];
3824 if (stream->buffer)
3826 buffer_impl = wined3d_buffer_get_parent(stream->buffer);
3827 *buffer = &buffer_impl->IDirect3DVertexBuffer9_iface;
3828 IDirect3DVertexBuffer9_AddRef(*buffer);
3830 else
3831 *buffer = NULL;
3832 if (offset)
3833 *offset = stream->offset;
3834 *stride = stream->stride;
3835 wined3d_mutex_unlock();
3837 return D3D_OK;
3840 static HRESULT WINAPI d3d9_device_SetStreamSourceFreq(IDirect3DDevice9Ex *iface, UINT stream_idx, UINT freq)
3842 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3843 HRESULT hr;
3845 TRACE("iface %p, stream_idx %u, freq %u.\n", iface, stream_idx, freq);
3847 wined3d_mutex_lock();
3848 hr = wined3d_stateblock_set_stream_source_freq(device->update_state, stream_idx, freq);
3849 wined3d_mutex_unlock();
3851 return hr;
3854 static HRESULT WINAPI d3d9_device_GetStreamSourceFreq(IDirect3DDevice9Ex *iface, UINT stream_idx, UINT *freq)
3856 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3857 const struct wined3d_stream_state *stream;
3859 TRACE("iface %p, stream_idx %u, freq %p.\n", iface, stream_idx, freq);
3861 wined3d_mutex_lock();
3862 stream = &device->stateblock_state->streams[stream_idx];
3863 *freq = stream->flags | stream->frequency;
3864 wined3d_mutex_unlock();
3866 return D3D_OK;
3869 static HRESULT WINAPI d3d9_device_SetIndices(IDirect3DDevice9Ex *iface, IDirect3DIndexBuffer9 *buffer)
3871 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3872 struct d3d9_indexbuffer *ib = unsafe_impl_from_IDirect3DIndexBuffer9(buffer);
3873 struct wined3d_buffer *wined3d_buffer;
3875 TRACE("iface %p, buffer %p.\n", iface, buffer);
3877 if (!ib)
3878 wined3d_buffer = NULL;
3879 else
3880 wined3d_buffer = ib->wined3d_buffer;
3882 wined3d_mutex_lock();
3883 wined3d_stateblock_set_index_buffer(device->update_state, wined3d_buffer, ib ? ib->format : WINED3DFMT_UNKNOWN);
3884 if (!device->recording)
3885 device->sysmem_ib = ib && ib->sysmem;
3886 wined3d_mutex_unlock();
3888 return D3D_OK;
3891 static HRESULT WINAPI d3d9_device_GetIndices(IDirect3DDevice9Ex *iface, IDirect3DIndexBuffer9 **buffer)
3893 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3894 struct wined3d_buffer *wined3d_buffer;
3895 struct d3d9_indexbuffer *buffer_impl;
3897 TRACE("iface %p, buffer %p.\n", iface, buffer);
3899 if (!buffer)
3900 return D3DERR_INVALIDCALL;
3902 wined3d_mutex_lock();
3903 if ((wined3d_buffer = device->stateblock_state->index_buffer))
3905 buffer_impl = wined3d_buffer_get_parent(wined3d_buffer);
3906 *buffer = &buffer_impl->IDirect3DIndexBuffer9_iface;
3907 IDirect3DIndexBuffer9_AddRef(*buffer);
3909 else
3911 *buffer = NULL;
3913 wined3d_mutex_unlock();
3915 return D3D_OK;
3918 static HRESULT WINAPI d3d9_device_CreatePixelShader(IDirect3DDevice9Ex *iface,
3919 const DWORD *byte_code, IDirect3DPixelShader9 **shader)
3921 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3922 struct d3d9_pixelshader *object;
3923 HRESULT hr;
3925 TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
3927 if (!(object = heap_alloc_zero(sizeof(*object))))
3929 FIXME("Failed to allocate pixel shader memory.\n");
3930 return E_OUTOFMEMORY;
3933 hr = pixelshader_init(object, device, byte_code);
3934 if (FAILED(hr))
3936 WARN("Failed to initialize pixel shader, hr %#lx.\n", hr);
3937 heap_free(object);
3938 return hr;
3941 TRACE("Created pixel shader %p.\n", object);
3942 *shader = &object->IDirect3DPixelShader9_iface;
3944 return D3D_OK;
3947 static HRESULT WINAPI d3d9_device_SetPixelShader(IDirect3DDevice9Ex *iface, IDirect3DPixelShader9 *shader)
3949 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3950 struct d3d9_pixelshader *shader_obj = unsafe_impl_from_IDirect3DPixelShader9(shader);
3952 TRACE("iface %p, shader %p.\n", iface, shader);
3954 wined3d_mutex_lock();
3955 wined3d_stateblock_set_pixel_shader(device->update_state,
3956 shader_obj ? shader_obj->wined3d_shader : NULL);
3957 wined3d_mutex_unlock();
3959 return D3D_OK;
3962 static HRESULT WINAPI d3d9_device_GetPixelShader(IDirect3DDevice9Ex *iface, IDirect3DPixelShader9 **shader)
3964 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3965 struct d3d9_pixelshader *shader_impl;
3966 struct wined3d_shader *wined3d_shader;
3968 TRACE("iface %p, shader %p.\n", iface, shader);
3970 if (!shader) return D3DERR_INVALIDCALL;
3972 wined3d_mutex_lock();
3973 if ((wined3d_shader = device->stateblock_state->ps))
3975 shader_impl = wined3d_shader_get_parent(wined3d_shader);
3976 *shader = &shader_impl->IDirect3DPixelShader9_iface;
3977 IDirect3DPixelShader9_AddRef(*shader);
3979 else
3981 *shader = NULL;
3983 wined3d_mutex_unlock();
3985 TRACE("Returning %p.\n", *shader);
3987 return D3D_OK;
3990 static HRESULT WINAPI d3d9_device_SetPixelShaderConstantF(IDirect3DDevice9Ex *iface,
3991 UINT reg_idx, const float *data, UINT count)
3993 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3994 HRESULT hr;
3996 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3998 wined3d_mutex_lock();
3999 hr = wined3d_stateblock_set_ps_consts_f(device->update_state,
4000 reg_idx, count, (const struct wined3d_vec4 *)data);
4001 wined3d_mutex_unlock();
4003 return hr;
4006 static HRESULT WINAPI d3d9_device_GetPixelShaderConstantF(IDirect3DDevice9Ex *iface,
4007 UINT start_idx, float *constants, UINT count)
4009 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
4010 HRESULT hr;
4012 TRACE("iface %p, start_idx %u, constants %p, count %u.\n", iface, start_idx, constants, count);
4014 wined3d_mutex_lock();
4015 hr = wined3d_stateblock_get_ps_consts_f(device->state, start_idx, count, (struct wined3d_vec4 *)constants);
4016 wined3d_mutex_unlock();
4018 return hr;
4021 static HRESULT WINAPI d3d9_device_SetPixelShaderConstantI(IDirect3DDevice9Ex *iface,
4022 UINT reg_idx, const int *data, UINT count)
4024 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
4025 HRESULT hr;
4027 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
4029 wined3d_mutex_lock();
4030 hr = wined3d_stateblock_set_ps_consts_i(device->update_state,
4031 reg_idx, count, (const struct wined3d_ivec4 *)data);
4032 wined3d_mutex_unlock();
4034 return hr;
4037 static HRESULT WINAPI d3d9_device_GetPixelShaderConstantI(IDirect3DDevice9Ex *iface,
4038 UINT start_idx, int *constants, UINT count)
4040 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
4041 HRESULT hr;
4043 TRACE("iface %p, start_idx %u, constants %p, count %u.\n", iface, start_idx, constants, count);
4045 wined3d_mutex_lock();
4046 hr = wined3d_stateblock_get_ps_consts_i(device->state, start_idx, count, (struct wined3d_ivec4 *)constants);
4047 wined3d_mutex_unlock();
4049 return hr;
4052 static HRESULT WINAPI d3d9_device_SetPixelShaderConstantB(IDirect3DDevice9Ex *iface,
4053 UINT reg_idx, const BOOL *data, UINT count)
4055 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
4056 HRESULT hr;
4058 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
4060 wined3d_mutex_lock();
4061 hr = wined3d_stateblock_set_ps_consts_b(device->update_state, reg_idx, count, data);
4062 wined3d_mutex_unlock();
4064 return hr;
4067 static HRESULT WINAPI d3d9_device_GetPixelShaderConstantB(IDirect3DDevice9Ex *iface,
4068 UINT start_idx, BOOL *constants, UINT count)
4070 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
4071 HRESULT hr;
4073 TRACE("iface %p, start_idx %u, constants %p, count %u.\n", iface, start_idx, constants, count);
4075 wined3d_mutex_lock();
4076 hr = wined3d_stateblock_get_ps_consts_b(device->state, start_idx, count, constants);
4077 wined3d_mutex_unlock();
4079 return hr;
4082 static HRESULT WINAPI d3d9_device_DrawRectPatch(IDirect3DDevice9Ex *iface, UINT handle,
4083 const float *segment_count, const D3DRECTPATCH_INFO *patch_info)
4085 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
4086 iface, handle, segment_count, patch_info);
4087 return D3D_OK;
4090 static HRESULT WINAPI d3d9_device_DrawTriPatch(IDirect3DDevice9Ex *iface, UINT handle,
4091 const float *segment_count, const D3DTRIPATCH_INFO *patch_info)
4093 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
4094 iface, handle, segment_count, patch_info);
4095 return D3D_OK;
4098 static HRESULT WINAPI d3d9_device_DeletePatch(IDirect3DDevice9Ex *iface, UINT handle)
4100 FIXME("iface %p, handle %#x unimplemented.\n", iface, handle);
4101 return D3DERR_INVALIDCALL;
4104 static HRESULT WINAPI d3d9_device_CreateQuery(IDirect3DDevice9Ex *iface, D3DQUERYTYPE type, IDirect3DQuery9 **query)
4106 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
4107 struct d3d9_query *object;
4108 HRESULT hr;
4110 TRACE("iface %p, type %#x, query %p.\n", iface, type, query);
4112 if (!(object = heap_alloc_zero(sizeof(*object))))
4113 return E_OUTOFMEMORY;
4115 hr = query_init(object, device, type);
4116 if (FAILED(hr))
4118 WARN("Failed to initialize query, hr %#lx.\n", hr);
4119 heap_free(object);
4120 return hr;
4123 TRACE("Created query %p.\n", object);
4124 if (query) *query = &object->IDirect3DQuery9_iface;
4125 else IDirect3DQuery9_Release(&object->IDirect3DQuery9_iface);
4127 return D3D_OK;
4130 static HRESULT WINAPI d3d9_device_SetConvolutionMonoKernel(IDirect3DDevice9Ex *iface,
4131 UINT width, UINT height, float *rows, float *columns)
4133 FIXME("iface %p, width %u, height %u, rows %p, columns %p stub!\n",
4134 iface, width, height, rows, columns);
4136 return E_NOTIMPL;
4139 static HRESULT WINAPI d3d9_device_ComposeRects(IDirect3DDevice9Ex *iface,
4140 IDirect3DSurface9 *src_surface, IDirect3DSurface9 *dst_surface, IDirect3DVertexBuffer9 *src_descs,
4141 UINT rect_count, IDirect3DVertexBuffer9 *dst_descs, D3DCOMPOSERECTSOP operation, INT offset_x, INT offset_y)
4143 FIXME("iface %p, src_surface %p, dst_surface %p, src_descs %p, rect_count %u, "
4144 "dst_descs %p, operation %#x, offset_x %u, offset_y %u stub!\n",
4145 iface, src_surface, dst_surface, src_descs, rect_count,
4146 dst_descs, operation, offset_x, offset_y);
4148 return E_NOTIMPL;
4151 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_PresentEx(IDirect3DDevice9Ex *iface,
4152 const RECT *src_rect, const RECT *dst_rect, HWND dst_window_override,
4153 const RGNDATA *dirty_region, DWORD flags)
4155 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
4156 struct d3d9_swapchain *swapchain;
4157 unsigned int i;
4158 HRESULT hr;
4160 TRACE("iface %p, src_rect %s, dst_rect %s, dst_window_override %p, dirty_region %p, flags %#lx.\n",
4161 iface, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect),
4162 dst_window_override, dirty_region, flags);
4164 if (device->device_state != D3D9_DEVICE_STATE_OK)
4165 return S_PRESENT_OCCLUDED;
4167 if (dirty_region)
4168 FIXME("Ignoring dirty_region %p.\n", dirty_region);
4170 wined3d_mutex_lock();
4171 for (i = 0; i < device->implicit_swapchain_count; ++i)
4173 swapchain = wined3d_swapchain_get_parent(device->implicit_swapchains[i]);
4174 if (FAILED(hr = wined3d_swapchain_present(swapchain->wined3d_swapchain,
4175 src_rect, dst_rect, dst_window_override, swapchain->swap_interval, flags)))
4177 wined3d_mutex_unlock();
4178 return hr;
4181 wined3d_mutex_unlock();
4183 return D3D_OK;
4186 static HRESULT WINAPI d3d9_device_GetGPUThreadPriority(IDirect3DDevice9Ex *iface, INT *priority)
4188 FIXME("iface %p, priority %p stub!\n", iface, priority);
4190 return E_NOTIMPL;
4193 static HRESULT WINAPI d3d9_device_SetGPUThreadPriority(IDirect3DDevice9Ex *iface, INT priority)
4195 FIXME("iface %p, priority %d stub!\n", iface, priority);
4197 return E_NOTIMPL;
4200 static HRESULT WINAPI d3d9_device_WaitForVBlank(IDirect3DDevice9Ex *iface, UINT swapchain_idx)
4202 FIXME("iface %p, swapchain_idx %u stub!\n", iface, swapchain_idx);
4204 return E_NOTIMPL;
4207 static HRESULT WINAPI d3d9_device_CheckResourceResidency(IDirect3DDevice9Ex *iface,
4208 IDirect3DResource9 **resources, UINT32 resource_count)
4210 FIXME("iface %p, resources %p, resource_count %u stub!\n",
4211 iface, resources, resource_count);
4213 return E_NOTIMPL;
4216 static HRESULT WINAPI d3d9_device_SetMaximumFrameLatency(IDirect3DDevice9Ex *iface, UINT max_latency)
4218 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
4220 TRACE("iface %p, max_latency %u.\n", iface, max_latency);
4222 if (max_latency > 30)
4223 return D3DERR_INVALIDCALL;
4225 wined3d_mutex_lock();
4226 wined3d_device_set_max_frame_latency(device->wined3d_device, max_latency);
4227 wined3d_mutex_unlock();
4229 return S_OK;
4232 static HRESULT WINAPI d3d9_device_GetMaximumFrameLatency(IDirect3DDevice9Ex *iface, UINT *max_latency)
4234 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
4236 TRACE("iface %p, max_latency %p.\n", iface, max_latency);
4238 wined3d_mutex_lock();
4239 *max_latency = wined3d_device_get_max_frame_latency(device->wined3d_device);
4240 wined3d_mutex_unlock();
4242 return S_OK;
4245 static HRESULT WINAPI d3d9_device_CheckDeviceState(IDirect3DDevice9Ex *iface, HWND dst_window)
4247 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
4248 struct wined3d_swapchain_desc swapchain_desc;
4250 TRACE("iface %p, dst_window %p.\n", iface, dst_window);
4252 wined3d_mutex_lock();
4253 wined3d_swapchain_get_desc(device->implicit_swapchains[0], &swapchain_desc);
4254 wined3d_mutex_unlock();
4256 if (swapchain_desc.windowed)
4257 return D3D_OK;
4259 /* FIXME: This is actually supposed to check if any other device is in
4260 * fullscreen mode. */
4261 if (dst_window != swapchain_desc.device_window)
4262 return device->device_state == D3D9_DEVICE_STATE_OK ? S_PRESENT_OCCLUDED : D3D_OK;
4264 return device->device_state == D3D9_DEVICE_STATE_OK ? D3D_OK : S_PRESENT_OCCLUDED;
4267 static HRESULT WINAPI d3d9_device_CreateRenderTargetEx(IDirect3DDevice9Ex *iface,
4268 UINT width, UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
4269 BOOL lockable, IDirect3DSurface9 **surface, HANDLE *shared_handle, DWORD usage)
4271 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
4272 unsigned int access = WINED3D_RESOURCE_ACCESS_GPU;
4274 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %lu, "
4275 "lockable %#x, surface %p, shared_handle %p, usage %#lx.\n",
4276 iface, width, height, format, multisample_type, multisample_quality,
4277 lockable, surface, shared_handle, usage);
4279 *surface = NULL;
4281 if (usage & (D3DUSAGE_DEPTHSTENCIL | D3DUSAGE_RENDERTARGET))
4283 WARN("Invalid usage %#lx.\n", usage);
4284 return D3DERR_INVALIDCALL;
4287 if (shared_handle)
4288 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
4290 if (lockable)
4291 access |= WINED3D_RESOURCE_ACCESS_MAP_R | WINED3D_RESOURCE_ACCESS_MAP_W;
4293 return d3d9_device_create_surface(device, 0, wined3dformat_from_d3dformat(format),
4294 wined3d_multisample_type_from_d3d(multisample_type), multisample_quality,
4295 usage & WINED3DUSAGE_MASK, WINED3D_BIND_RENDER_TARGET, access, width, height, NULL, surface);
4298 static HRESULT WINAPI d3d9_device_CreateOffscreenPlainSurfaceEx(IDirect3DDevice9Ex *iface,
4299 UINT width, UINT height, D3DFORMAT format, D3DPOOL pool, IDirect3DSurface9 **surface,
4300 HANDLE *shared_handle, DWORD usage)
4302 FIXME("iface %p, width %u, height %u, format %#x, pool %#x, surface %p, shared_handle %p, usage %#lx stub!\n",
4303 iface, width, height, format, pool, surface, shared_handle, usage);
4305 return E_NOTIMPL;
4308 static HRESULT WINAPI d3d9_device_CreateDepthStencilSurfaceEx(IDirect3DDevice9Ex *iface,
4309 UINT width, UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
4310 BOOL discard, IDirect3DSurface9 **surface, HANDLE *shared_handle, DWORD usage)
4312 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
4313 DWORD flags = 0;
4315 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %lu, "
4316 "discard %#x, surface %p, shared_handle %p, usage %#lx.\n",
4317 iface, width, height, format, multisample_type, multisample_quality,
4318 discard, surface, shared_handle, usage);
4320 if (usage & (D3DUSAGE_DEPTHSTENCIL | D3DUSAGE_RENDERTARGET))
4322 WARN("Invalid usage %#lx.\n", usage);
4323 return D3DERR_INVALIDCALL;
4326 if (shared_handle)
4327 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
4329 if (discard)
4330 flags |= WINED3D_TEXTURE_CREATE_DISCARD;
4332 *surface = NULL;
4333 return d3d9_device_create_surface(device, flags, wined3dformat_from_d3dformat(format),
4334 wined3d_multisample_type_from_d3d(multisample_type), multisample_quality, usage & WINED3DUSAGE_MASK,
4335 WINED3D_BIND_DEPTH_STENCIL, WINED3D_RESOURCE_ACCESS_GPU, width, height, NULL, surface);
4338 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_ResetEx(IDirect3DDevice9Ex *iface,
4339 D3DPRESENT_PARAMETERS *present_parameters, D3DDISPLAYMODEEX *mode)
4341 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
4343 TRACE("iface %p, present_parameters %p, mode %p.\n", iface, present_parameters, mode);
4345 if (!present_parameters->Windowed == !mode)
4347 WARN("Mode can be passed if and only if Windowed is FALSE.\n");
4348 return D3DERR_INVALIDCALL;
4351 if (mode && (mode->Width != present_parameters->BackBufferWidth
4352 || mode->Height != present_parameters->BackBufferHeight))
4354 WARN("Mode and back buffer mismatch (mode %ux%u, backbuffer %ux%u).\n",
4355 mode->Width, mode->Height,
4356 present_parameters->BackBufferWidth, present_parameters->BackBufferHeight);
4357 return D3DERR_INVALIDCALL;
4360 return d3d9_device_reset(device, present_parameters, mode);
4363 static HRESULT WINAPI d3d9_device_GetDisplayModeEx(IDirect3DDevice9Ex *iface,
4364 UINT swapchain_idx, D3DDISPLAYMODEEX *mode, D3DDISPLAYROTATION *rotation)
4366 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
4367 struct wined3d_display_mode wined3d_mode;
4368 HRESULT hr;
4370 TRACE("iface %p, swapchain_idx %u, mode %p, rotation %p.\n",
4371 iface, swapchain_idx, mode, rotation);
4373 if (mode->Size != sizeof(*mode))
4374 return D3DERR_INVALIDCALL;
4376 wined3d_mutex_lock();
4377 hr = wined3d_device_get_display_mode(device->wined3d_device, swapchain_idx, &wined3d_mode,
4378 (enum wined3d_display_rotation *)rotation);
4379 wined3d_mutex_unlock();
4381 if (SUCCEEDED(hr))
4383 mode->Width = wined3d_mode.width;
4384 mode->Height = wined3d_mode.height;
4385 mode->RefreshRate = wined3d_mode.refresh_rate;
4386 mode->Format = d3dformat_from_wined3dformat(wined3d_mode.format_id);
4387 mode->ScanLineOrdering = d3dscanlineordering_from_wined3d(wined3d_mode.scanline_ordering);
4390 return hr;
4393 static const struct IDirect3DDevice9ExVtbl d3d9_device_vtbl =
4395 /* IUnknown */
4396 d3d9_device_QueryInterface,
4397 d3d9_device_AddRef,
4398 d3d9_device_Release,
4399 /* IDirect3DDevice9 */
4400 d3d9_device_TestCooperativeLevel,
4401 d3d9_device_GetAvailableTextureMem,
4402 d3d9_device_EvictManagedResources,
4403 d3d9_device_GetDirect3D,
4404 d3d9_device_GetDeviceCaps,
4405 d3d9_device_GetDisplayMode,
4406 d3d9_device_GetCreationParameters,
4407 d3d9_device_SetCursorProperties,
4408 d3d9_device_SetCursorPosition,
4409 d3d9_device_ShowCursor,
4410 d3d9_device_CreateAdditionalSwapChain,
4411 d3d9_device_GetSwapChain,
4412 d3d9_device_GetNumberOfSwapChains,
4413 d3d9_device_Reset,
4414 d3d9_device_Present,
4415 d3d9_device_GetBackBuffer,
4416 d3d9_device_GetRasterStatus,
4417 d3d9_device_SetDialogBoxMode,
4418 d3d9_device_SetGammaRamp,
4419 d3d9_device_GetGammaRamp,
4420 d3d9_device_CreateTexture,
4421 d3d9_device_CreateVolumeTexture,
4422 d3d9_device_CreateCubeTexture,
4423 d3d9_device_CreateVertexBuffer,
4424 d3d9_device_CreateIndexBuffer,
4425 d3d9_device_CreateRenderTarget,
4426 d3d9_device_CreateDepthStencilSurface,
4427 d3d9_device_UpdateSurface,
4428 d3d9_device_UpdateTexture,
4429 d3d9_device_GetRenderTargetData,
4430 d3d9_device_GetFrontBufferData,
4431 d3d9_device_StretchRect,
4432 d3d9_device_ColorFill,
4433 d3d9_device_CreateOffscreenPlainSurface,
4434 d3d9_device_SetRenderTarget,
4435 d3d9_device_GetRenderTarget,
4436 d3d9_device_SetDepthStencilSurface,
4437 d3d9_device_GetDepthStencilSurface,
4438 d3d9_device_BeginScene,
4439 d3d9_device_EndScene,
4440 d3d9_device_Clear,
4441 d3d9_device_SetTransform,
4442 d3d9_device_GetTransform,
4443 d3d9_device_MultiplyTransform,
4444 d3d9_device_SetViewport,
4445 d3d9_device_GetViewport,
4446 d3d9_device_SetMaterial,
4447 d3d9_device_GetMaterial,
4448 d3d9_device_SetLight,
4449 d3d9_device_GetLight,
4450 d3d9_device_LightEnable,
4451 d3d9_device_GetLightEnable,
4452 d3d9_device_SetClipPlane,
4453 d3d9_device_GetClipPlane,
4454 d3d9_device_SetRenderState,
4455 d3d9_device_GetRenderState,
4456 d3d9_device_CreateStateBlock,
4457 d3d9_device_BeginStateBlock,
4458 d3d9_device_EndStateBlock,
4459 d3d9_device_SetClipStatus,
4460 d3d9_device_GetClipStatus,
4461 d3d9_device_GetTexture,
4462 d3d9_device_SetTexture,
4463 d3d9_device_GetTextureStageState,
4464 d3d9_device_SetTextureStageState,
4465 d3d9_device_GetSamplerState,
4466 d3d9_device_SetSamplerState,
4467 d3d9_device_ValidateDevice,
4468 d3d9_device_SetPaletteEntries,
4469 d3d9_device_GetPaletteEntries,
4470 d3d9_device_SetCurrentTexturePalette,
4471 d3d9_device_GetCurrentTexturePalette,
4472 d3d9_device_SetScissorRect,
4473 d3d9_device_GetScissorRect,
4474 d3d9_device_SetSoftwareVertexProcessing,
4475 d3d9_device_GetSoftwareVertexProcessing,
4476 d3d9_device_SetNPatchMode,
4477 d3d9_device_GetNPatchMode,
4478 d3d9_device_DrawPrimitive,
4479 d3d9_device_DrawIndexedPrimitive,
4480 d3d9_device_DrawPrimitiveUP,
4481 d3d9_device_DrawIndexedPrimitiveUP,
4482 d3d9_device_ProcessVertices,
4483 d3d9_device_CreateVertexDeclaration,
4484 d3d9_device_SetVertexDeclaration,
4485 d3d9_device_GetVertexDeclaration,
4486 d3d9_device_SetFVF,
4487 d3d9_device_GetFVF,
4488 d3d9_device_CreateVertexShader,
4489 d3d9_device_SetVertexShader,
4490 d3d9_device_GetVertexShader,
4491 d3d9_device_SetVertexShaderConstantF,
4492 d3d9_device_GetVertexShaderConstantF,
4493 d3d9_device_SetVertexShaderConstantI,
4494 d3d9_device_GetVertexShaderConstantI,
4495 d3d9_device_SetVertexShaderConstantB,
4496 d3d9_device_GetVertexShaderConstantB,
4497 d3d9_device_SetStreamSource,
4498 d3d9_device_GetStreamSource,
4499 d3d9_device_SetStreamSourceFreq,
4500 d3d9_device_GetStreamSourceFreq,
4501 d3d9_device_SetIndices,
4502 d3d9_device_GetIndices,
4503 d3d9_device_CreatePixelShader,
4504 d3d9_device_SetPixelShader,
4505 d3d9_device_GetPixelShader,
4506 d3d9_device_SetPixelShaderConstantF,
4507 d3d9_device_GetPixelShaderConstantF,
4508 d3d9_device_SetPixelShaderConstantI,
4509 d3d9_device_GetPixelShaderConstantI,
4510 d3d9_device_SetPixelShaderConstantB,
4511 d3d9_device_GetPixelShaderConstantB,
4512 d3d9_device_DrawRectPatch,
4513 d3d9_device_DrawTriPatch,
4514 d3d9_device_DeletePatch,
4515 d3d9_device_CreateQuery,
4516 /* IDirect3DDevice9Ex */
4517 d3d9_device_SetConvolutionMonoKernel,
4518 d3d9_device_ComposeRects,
4519 d3d9_device_PresentEx,
4520 d3d9_device_GetGPUThreadPriority,
4521 d3d9_device_SetGPUThreadPriority,
4522 d3d9_device_WaitForVBlank,
4523 d3d9_device_CheckResourceResidency,
4524 d3d9_device_SetMaximumFrameLatency,
4525 d3d9_device_GetMaximumFrameLatency,
4526 d3d9_device_CheckDeviceState,
4527 d3d9_device_CreateRenderTargetEx,
4528 d3d9_device_CreateOffscreenPlainSurfaceEx,
4529 d3d9_device_CreateDepthStencilSurfaceEx,
4530 d3d9_device_ResetEx,
4531 d3d9_device_GetDisplayModeEx,
4534 static inline struct d3d9_device *device_from_device_parent(struct wined3d_device_parent *device_parent)
4536 return CONTAINING_RECORD(device_parent, struct d3d9_device, device_parent);
4539 static void CDECL device_parent_wined3d_device_created(struct wined3d_device_parent *device_parent,
4540 struct wined3d_device *device)
4542 TRACE("device_parent %p, device %p.\n", device_parent, device);
4545 static void CDECL device_parent_mode_changed(struct wined3d_device_parent *device_parent)
4547 TRACE("device_parent %p.\n", device_parent);
4550 static void CDECL device_parent_activate(struct wined3d_device_parent *device_parent, BOOL activate)
4552 struct d3d9_device *device = device_from_device_parent(device_parent);
4554 TRACE("device_parent %p, activate %#x.\n", device_parent, activate);
4556 if (!device->d3d_parent)
4557 return;
4559 if (!activate)
4560 InterlockedCompareExchange(&device->device_state, D3D9_DEVICE_STATE_LOST, D3D9_DEVICE_STATE_OK);
4561 else if (device->d3d_parent->extended)
4562 InterlockedCompareExchange(&device->device_state, D3D9_DEVICE_STATE_OK, D3D9_DEVICE_STATE_LOST);
4563 else
4564 InterlockedCompareExchange(&device->device_state, D3D9_DEVICE_STATE_NOT_RESET, D3D9_DEVICE_STATE_LOST);
4567 static HRESULT CDECL device_parent_texture_sub_resource_created(struct wined3d_device_parent *device_parent,
4568 enum wined3d_resource_type type, struct wined3d_texture *wined3d_texture, unsigned int sub_resource_idx,
4569 void **parent, const struct wined3d_parent_ops **parent_ops)
4571 TRACE("device_parent %p, type %#x, wined3d_texture %p, sub_resource_idx %u, parent %p, parent_ops %p.\n",
4572 device_parent, type, wined3d_texture, sub_resource_idx, parent, parent_ops);
4574 if (type == WINED3D_RTYPE_TEXTURE_3D)
4576 struct d3d9_volume *d3d_volume;
4578 if (!(d3d_volume = heap_alloc_zero(sizeof(*d3d_volume))))
4579 return E_OUTOFMEMORY;
4581 volume_init(d3d_volume, wined3d_texture, sub_resource_idx, parent_ops);
4582 *parent = d3d_volume;
4583 TRACE("Created volume %p.\n", d3d_volume);
4585 else if (type != WINED3D_RTYPE_TEXTURE_2D)
4587 ERR("Unhandled resource type %#x.\n", type);
4588 return E_FAIL;
4591 return D3D_OK;
4594 static const struct wined3d_device_parent_ops d3d9_wined3d_device_parent_ops =
4596 device_parent_wined3d_device_created,
4597 device_parent_mode_changed,
4598 device_parent_activate,
4599 device_parent_texture_sub_resource_created,
4602 static void setup_fpu(void)
4604 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
4605 WORD cw;
4606 __asm__ volatile ("fnstcw %0" : "=m" (cw));
4607 cw = (cw & ~0xf3f) | 0x3f;
4608 __asm__ volatile ("fldcw %0" : : "m" (cw));
4609 #elif defined(__i386__) && defined(_MSC_VER)
4610 WORD cw;
4611 __asm fnstcw cw;
4612 cw = (cw & ~0xf3f) | 0x3f;
4613 __asm fldcw cw;
4614 #else
4615 FIXME("FPU setup not implemented for this platform.\n");
4616 #endif
4619 HRESULT device_init(struct d3d9_device *device, struct d3d9 *parent, struct wined3d *wined3d,
4620 UINT adapter, D3DDEVTYPE device_type, HWND focus_window, DWORD flags,
4621 D3DPRESENT_PARAMETERS *parameters, D3DDISPLAYMODEEX *mode)
4623 struct wined3d_swapchain_desc *swapchain_desc;
4624 struct wined3d_adapter *wined3d_adapter;
4625 struct d3d9_swapchain *d3d_swapchain;
4626 struct wined3d_caps wined3d_caps;
4627 unsigned int output_idx;
4628 unsigned i, count = 1;
4629 D3DCAPS9 caps;
4630 HRESULT hr;
4632 static const enum wined3d_feature_level feature_levels[] =
4634 WINED3D_FEATURE_LEVEL_9_3,
4635 WINED3D_FEATURE_LEVEL_9_2,
4636 WINED3D_FEATURE_LEVEL_9_1,
4637 WINED3D_FEATURE_LEVEL_8,
4638 WINED3D_FEATURE_LEVEL_7,
4639 WINED3D_FEATURE_LEVEL_6,
4640 WINED3D_FEATURE_LEVEL_5,
4643 output_idx = adapter;
4644 if (output_idx >= parent->wined3d_output_count)
4645 return D3DERR_INVALIDCALL;
4647 if (mode)
4648 FIXME("Ignoring display mode.\n");
4650 device->IDirect3DDevice9Ex_iface.lpVtbl = &d3d9_device_vtbl;
4651 device->device_parent.ops = &d3d9_wined3d_device_parent_ops;
4652 device->adapter_ordinal = adapter;
4653 device->refcount = 1;
4655 if (!(flags & D3DCREATE_FPU_PRESERVE)) setup_fpu();
4657 wined3d_mutex_lock();
4658 wined3d_adapter = wined3d_output_get_adapter(parent->wined3d_outputs[output_idx]);
4659 if (FAILED(hr = wined3d_device_create(wined3d, wined3d_adapter, wined3d_device_type_from_d3d(device_type),
4660 focus_window, flags, 4, feature_levels, ARRAY_SIZE(feature_levels),
4661 &device->device_parent, &device->wined3d_device)))
4663 WARN("Failed to create wined3d device, hr %#lx.\n", hr);
4664 wined3d_mutex_unlock();
4665 return hr;
4668 device->immediate_context = wined3d_device_get_immediate_context(device->wined3d_device);
4669 wined3d_get_device_caps(wined3d_adapter, wined3d_device_type_from_d3d(device_type), &wined3d_caps);
4670 d3d9_caps_from_wined3dcaps(parent, adapter, &caps, &wined3d_caps);
4671 device->max_user_clip_planes = caps.MaxUserClipPlanes;
4672 device->vs_uniform_count = caps.MaxVertexShaderConst;
4673 if (flags & D3DCREATE_ADAPTERGROUP_DEVICE)
4674 count = caps.NumberOfAdaptersInGroup;
4676 if (FAILED(hr = wined3d_stateblock_create(device->wined3d_device, NULL, WINED3D_SBT_PRIMARY, &device->state)))
4678 ERR("Failed to create the primary stateblock, hr %#lx.\n", hr);
4679 wined3d_device_decref(device->wined3d_device);
4680 wined3d_mutex_unlock();
4681 return hr;
4683 device->stateblock_state = wined3d_stateblock_get_state(device->state);
4684 device->update_state = device->state;
4686 wined3d_streaming_buffer_init(&device->vertex_buffer, WINED3D_BIND_VERTEX_BUFFER);
4687 wined3d_streaming_buffer_init(&device->index_buffer, WINED3D_BIND_INDEX_BUFFER);
4689 if (flags & D3DCREATE_MULTITHREADED)
4690 wined3d_device_set_multithreaded(device->wined3d_device);
4692 if (!parameters->Windowed)
4694 if (!focus_window)
4695 focus_window = parameters->hDeviceWindow;
4696 if (FAILED(hr = wined3d_device_acquire_focus_window(device->wined3d_device, focus_window)))
4698 ERR("Failed to acquire focus window, hr %#lx.\n", hr);
4699 wined3d_device_decref(device->wined3d_device);
4700 wined3d_mutex_unlock();
4701 return hr;
4705 if (!(swapchain_desc = heap_alloc(sizeof(*swapchain_desc) * count)))
4707 ERR("Failed to allocate wined3d parameters.\n");
4708 wined3d_device_release_focus_window(device->wined3d_device);
4709 wined3d_device_decref(device->wined3d_device);
4710 wined3d_mutex_unlock();
4711 return E_OUTOFMEMORY;
4714 for (i = 0; i < count; ++i)
4716 if (!wined3d_swapchain_desc_from_d3d9(&swapchain_desc[i],
4717 parent->wined3d_outputs[output_idx + i], &parameters[i], parent->extended))
4719 wined3d_device_release_focus_window(device->wined3d_device);
4720 wined3d_device_decref(device->wined3d_device);
4721 heap_free(swapchain_desc);
4722 wined3d_mutex_unlock();
4723 return D3DERR_INVALIDCALL;
4725 swapchain_desc[i].flags |= WINED3D_SWAPCHAIN_IMPLICIT;
4726 if (flags & D3DCREATE_NOWINDOWCHANGES)
4727 swapchain_desc[i].flags |= WINED3D_SWAPCHAIN_NO_WINDOW_CHANGES;
4730 if (FAILED(hr = d3d9_swapchain_create(device, swapchain_desc,
4731 wined3dswapinterval_from_d3d(parameters->PresentationInterval), &d3d_swapchain)))
4733 WARN("Failed to create swapchain, hr %#lx.\n", hr);
4734 wined3d_device_release_focus_window(device->wined3d_device);
4735 heap_free(swapchain_desc);
4736 wined3d_device_decref(device->wined3d_device);
4737 wined3d_mutex_unlock();
4738 return hr;
4741 wined3d_swapchain_incref(d3d_swapchain->wined3d_swapchain);
4742 IDirect3DSwapChain9Ex_Release(&d3d_swapchain->IDirect3DSwapChain9Ex_iface);
4744 wined3d_stateblock_set_render_state(device->state, WINED3D_RS_ZENABLE,
4745 !!swapchain_desc->enable_auto_depth_stencil);
4746 device_reset_viewport_state(device);
4748 if (FAILED(hr = d3d9_device_get_swapchains(device)))
4750 wined3d_swapchain_decref(d3d_swapchain->wined3d_swapchain);
4751 wined3d_device_release_focus_window(device->wined3d_device);
4752 wined3d_device_decref(device->wined3d_device);
4753 heap_free(swapchain_desc);
4754 wined3d_mutex_unlock();
4755 return E_OUTOFMEMORY;
4758 for (i = 0; i < count; ++i)
4760 present_parameters_from_wined3d_swapchain_desc(&parameters[i],
4761 &swapchain_desc[i], parameters[i].PresentationInterval);
4764 wined3d_mutex_unlock();
4766 heap_free(swapchain_desc);
4768 /* Initialize the converted declaration array. This creates a valid pointer
4769 * and when adding decls HeapReAlloc() can be used without further checking. */
4770 if (!(device->fvf_decls = heap_alloc(0)))
4772 ERR("Failed to allocate FVF vertex declaration map memory.\n");
4773 wined3d_mutex_lock();
4774 heap_free(device->implicit_swapchains);
4775 wined3d_swapchain_decref(d3d_swapchain->wined3d_swapchain);
4776 wined3d_device_release_focus_window(device->wined3d_device);
4777 wined3d_device_decref(device->wined3d_device);
4778 wined3d_mutex_unlock();
4779 return E_OUTOFMEMORY;
4782 /* We could also simply ignore the initial rendertarget since it's known
4783 * not to be a texture (we currently use these only for automatic mipmap
4784 * generation). */
4785 wined3d_mutex_lock();
4786 device->render_targets[0] = wined3d_rendertarget_view_get_sub_resource_parent(
4787 wined3d_device_context_get_rendertarget_view(device->immediate_context, 0));
4788 wined3d_mutex_unlock();
4790 IDirect3D9Ex_AddRef(&parent->IDirect3D9Ex_iface);
4791 device->d3d_parent = parent;
4793 return D3D_OK;