msscript.ocx: Keep script host running as long as any script module is alive.
[wine.git] / dlls / d3d9 / device.c
blob9b1dac0b58c6a833400d06bd3f3a88a8fb0db830
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 %#x.\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 ((present_parameters->Flags & D3DPRESENTFLAG_LOCKABLE_BACKBUFFER)
353 && (is_gdi_compat_wined3dformat(swapchain_desc->backbuffer_format)
354 /* WINED3DFMT_UNKNOWN creates the swapchain with the current
355 * display format, which is always GDI-compatible. */
356 || swapchain_desc->backbuffer_format == WINED3DFMT_UNKNOWN))
357 swapchain_desc->flags |= WINED3D_SWAPCHAIN_GDI_COMPATIBLE;
358 swapchain_desc->refresh_rate = present_parameters->FullScreen_RefreshRateInHz;
359 swapchain_desc->auto_restore_display_mode = TRUE;
361 if (present_parameters->Flags & ~D3DPRESENTFLAGS_MASK)
362 FIXME("Unhandled flags %#x.\n", present_parameters->Flags & ~D3DPRESENTFLAGS_MASK);
364 return TRUE;
367 void d3d9_caps_from_wined3dcaps(const struct d3d9 *d3d9, unsigned int adapter_ordinal,
368 D3DCAPS9 *caps, const struct wined3d_caps *wined3d_caps)
370 static const DWORD ps_minor_version[] = {0, 4, 0, 0};
371 static const DWORD vs_minor_version[] = {0, 1, 0, 0};
372 static const DWORD texture_filter_caps =
373 D3DPTFILTERCAPS_MINFPOINT | D3DPTFILTERCAPS_MINFLINEAR | D3DPTFILTERCAPS_MINFANISOTROPIC |
374 D3DPTFILTERCAPS_MINFPYRAMIDALQUAD | D3DPTFILTERCAPS_MINFGAUSSIANQUAD|
375 D3DPTFILTERCAPS_MIPFPOINT | D3DPTFILTERCAPS_MIPFLINEAR | D3DPTFILTERCAPS_MAGFPOINT |
376 D3DPTFILTERCAPS_MAGFLINEAR |D3DPTFILTERCAPS_MAGFANISOTROPIC|D3DPTFILTERCAPS_MAGFPYRAMIDALQUAD|
377 D3DPTFILTERCAPS_MAGFGAUSSIANQUAD;
378 struct wined3d_adapter *wined3d_adapter;
379 struct wined3d_output_desc output_desc;
380 struct wined3d_output *wined3d_output;
381 unsigned int output_idx;
382 HRESULT hr;
384 caps->DeviceType = (D3DDEVTYPE)wined3d_caps->DeviceType;
385 caps->AdapterOrdinal = adapter_ordinal;
386 caps->Caps = wined3d_caps->Caps;
387 caps->Caps2 = wined3d_caps->Caps2;
388 caps->Caps3 = wined3d_caps->Caps3;
389 caps->PresentationIntervals = D3DPRESENT_INTERVAL_IMMEDIATE | D3DPRESENT_INTERVAL_ONE;
390 caps->CursorCaps = wined3d_caps->CursorCaps;
391 caps->DevCaps = wined3d_caps->DevCaps;
392 caps->PrimitiveMiscCaps = wined3d_caps->PrimitiveMiscCaps;
393 caps->RasterCaps = wined3d_caps->RasterCaps;
394 caps->ZCmpCaps = wined3d_caps->ZCmpCaps;
395 caps->SrcBlendCaps = wined3d_caps->SrcBlendCaps;
396 caps->DestBlendCaps = wined3d_caps->DestBlendCaps;
397 caps->AlphaCmpCaps = wined3d_caps->AlphaCmpCaps;
398 caps->ShadeCaps = wined3d_caps->ShadeCaps;
399 caps->TextureCaps = wined3d_caps->TextureCaps;
400 caps->TextureFilterCaps = wined3d_caps->TextureFilterCaps;
401 caps->CubeTextureFilterCaps = wined3d_caps->CubeTextureFilterCaps;
402 caps->VolumeTextureFilterCaps = wined3d_caps->VolumeTextureFilterCaps;
403 caps->TextureAddressCaps = wined3d_caps->TextureAddressCaps;
404 caps->VolumeTextureAddressCaps = wined3d_caps->VolumeTextureAddressCaps;
405 caps->LineCaps = wined3d_caps->LineCaps;
406 caps->MaxTextureWidth = wined3d_caps->MaxTextureWidth;
407 caps->MaxTextureHeight = wined3d_caps->MaxTextureHeight;
408 caps->MaxVolumeExtent = wined3d_caps->MaxVolumeExtent;
409 caps->MaxTextureRepeat = wined3d_caps->MaxTextureRepeat;
410 caps->MaxTextureAspectRatio = wined3d_caps->MaxTextureAspectRatio;
411 caps->MaxAnisotropy = wined3d_caps->MaxAnisotropy;
412 caps->MaxVertexW = wined3d_caps->MaxVertexW;
413 caps->GuardBandLeft = wined3d_caps->GuardBandLeft;
414 caps->GuardBandTop = wined3d_caps->GuardBandTop;
415 caps->GuardBandRight = wined3d_caps->GuardBandRight;
416 caps->GuardBandBottom = wined3d_caps->GuardBandBottom;
417 caps->ExtentsAdjust = wined3d_caps->ExtentsAdjust;
418 caps->StencilCaps = wined3d_caps->StencilCaps;
419 caps->FVFCaps = wined3d_caps->FVFCaps;
420 caps->TextureOpCaps = wined3d_caps->TextureOpCaps;
421 caps->MaxTextureBlendStages = wined3d_caps->MaxTextureBlendStages;
422 caps->MaxSimultaneousTextures = wined3d_caps->MaxSimultaneousTextures;
423 caps->VertexProcessingCaps = wined3d_caps->VertexProcessingCaps;
424 caps->MaxActiveLights = wined3d_caps->MaxActiveLights;
425 caps->MaxUserClipPlanes = wined3d_caps->MaxUserClipPlanes;
426 caps->MaxVertexBlendMatrices = wined3d_caps->MaxVertexBlendMatrices;
427 caps->MaxVertexBlendMatrixIndex = wined3d_caps->MaxVertexBlendMatrixIndex;
428 caps->MaxPointSize = wined3d_caps->MaxPointSize;
429 caps->MaxPrimitiveCount = wined3d_caps->MaxPrimitiveCount;
430 caps->MaxVertexIndex = wined3d_caps->MaxVertexIndex;
431 caps->MaxStreams = wined3d_caps->MaxStreams;
432 caps->MaxStreamStride = wined3d_caps->MaxStreamStride;
433 caps->VertexShaderVersion = wined3d_caps->VertexShaderVersion;
434 caps->MaxVertexShaderConst = wined3d_caps->MaxVertexShaderConst;
435 caps->PixelShaderVersion = wined3d_caps->PixelShaderVersion;
436 caps->PixelShader1xMaxValue = wined3d_caps->PixelShader1xMaxValue;
437 caps->DevCaps2 = wined3d_caps->DevCaps2;
438 caps->MaxNpatchTessellationLevel = wined3d_caps->MaxNpatchTessellationLevel;
439 caps->DeclTypes = wined3d_caps->DeclTypes;
440 caps->NumSimultaneousRTs = wined3d_caps->NumSimultaneousRTs;
441 caps->StretchRectFilterCaps = wined3d_caps->StretchRectFilterCaps;
442 caps->VS20Caps.Caps = wined3d_caps->VS20Caps.caps;
443 caps->VS20Caps.DynamicFlowControlDepth = wined3d_caps->VS20Caps.dynamic_flow_control_depth;
444 caps->VS20Caps.NumTemps = wined3d_caps->VS20Caps.temp_count;
445 caps->VS20Caps.StaticFlowControlDepth = wined3d_caps->VS20Caps.static_flow_control_depth;
446 caps->PS20Caps.Caps = wined3d_caps->PS20Caps.caps;
447 caps->PS20Caps.DynamicFlowControlDepth = wined3d_caps->PS20Caps.dynamic_flow_control_depth;
448 caps->PS20Caps.NumTemps = wined3d_caps->PS20Caps.temp_count;
449 caps->PS20Caps.StaticFlowControlDepth = wined3d_caps->PS20Caps.static_flow_control_depth;
450 caps->PS20Caps.NumInstructionSlots = wined3d_caps->PS20Caps.instruction_slot_count;
451 caps->VertexTextureFilterCaps = wined3d_caps->VertexTextureFilterCaps;
452 caps->MaxVShaderInstructionsExecuted = wined3d_caps->MaxVShaderInstructionsExecuted;
453 caps->MaxPShaderInstructionsExecuted = wined3d_caps->MaxPShaderInstructionsExecuted;
454 caps->MaxVertexShader30InstructionSlots = wined3d_caps->MaxVertexShader30InstructionSlots;
455 caps->MaxPixelShader30InstructionSlots = wined3d_caps->MaxPixelShader30InstructionSlots;
457 /* Some functionality is implemented in d3d9.dll, not wined3d.dll. Add the needed caps. */
458 caps->DevCaps2 |= D3DDEVCAPS2_CAN_STRETCHRECT_FROM_TEXTURES;
460 /* Filter wined3d caps. */
461 caps->TextureFilterCaps &= texture_filter_caps;
462 caps->CubeTextureFilterCaps &= texture_filter_caps;
463 caps->VolumeTextureFilterCaps &= texture_filter_caps;
465 caps->DevCaps &=
466 D3DDEVCAPS_EXECUTESYSTEMMEMORY | D3DDEVCAPS_EXECUTEVIDEOMEMORY | D3DDEVCAPS_TLVERTEXSYSTEMMEMORY |
467 D3DDEVCAPS_TLVERTEXVIDEOMEMORY | D3DDEVCAPS_TEXTURESYSTEMMEMORY| D3DDEVCAPS_TEXTUREVIDEOMEMORY |
468 D3DDEVCAPS_DRAWPRIMTLVERTEX | D3DDEVCAPS_CANRENDERAFTERFLIP | D3DDEVCAPS_TEXTURENONLOCALVIDMEM|
469 D3DDEVCAPS_DRAWPRIMITIVES2 | D3DDEVCAPS_SEPARATETEXTUREMEMORIES |
470 D3DDEVCAPS_DRAWPRIMITIVES2EX | D3DDEVCAPS_HWTRANSFORMANDLIGHT| D3DDEVCAPS_CANBLTSYSTONONLOCAL |
471 D3DDEVCAPS_HWRASTERIZATION | D3DDEVCAPS_PUREDEVICE | D3DDEVCAPS_QUINTICRTPATCHES |
472 D3DDEVCAPS_RTPATCHES | D3DDEVCAPS_RTPATCHHANDLEZERO | D3DDEVCAPS_NPATCHES;
474 caps->ShadeCaps &=
475 D3DPSHADECAPS_COLORGOURAUDRGB | D3DPSHADECAPS_SPECULARGOURAUDRGB |
476 D3DPSHADECAPS_ALPHAGOURAUDBLEND | D3DPSHADECAPS_FOGGOURAUD;
478 caps->RasterCaps &=
479 D3DPRASTERCAPS_DITHER | D3DPRASTERCAPS_ZTEST | D3DPRASTERCAPS_FOGVERTEX |
480 D3DPRASTERCAPS_FOGTABLE | D3DPRASTERCAPS_MIPMAPLODBIAS | D3DPRASTERCAPS_ZBUFFERLESSHSR |
481 D3DPRASTERCAPS_FOGRANGE | D3DPRASTERCAPS_ANISOTROPY | D3DPRASTERCAPS_WBUFFER |
482 D3DPRASTERCAPS_WFOG | D3DPRASTERCAPS_ZFOG | D3DPRASTERCAPS_COLORPERSPECTIVE |
483 D3DPRASTERCAPS_SCISSORTEST | D3DPRASTERCAPS_SLOPESCALEDEPTHBIAS |
484 D3DPRASTERCAPS_DEPTHBIAS | D3DPRASTERCAPS_MULTISAMPLE_TOGGLE;
486 caps->DevCaps2 &=
487 D3DDEVCAPS2_STREAMOFFSET | D3DDEVCAPS2_DMAPNPATCH | D3DDEVCAPS2_ADAPTIVETESSRTPATCH |
488 D3DDEVCAPS2_ADAPTIVETESSNPATCH | D3DDEVCAPS2_CAN_STRETCHRECT_FROM_TEXTURES |
489 D3DDEVCAPS2_PRESAMPLEDDMAPNPATCH| D3DDEVCAPS2_VERTEXELEMENTSCANSHARESTREAMOFFSET;
491 caps->Caps2 &=
492 D3DCAPS2_FULLSCREENGAMMA | D3DCAPS2_CANCALIBRATEGAMMA | D3DCAPS2_RESERVED |
493 D3DCAPS2_CANMANAGERESOURCE | D3DCAPS2_DYNAMICTEXTURES | D3DCAPS2_CANAUTOGENMIPMAP;
495 caps->VertexProcessingCaps &=
496 D3DVTXPCAPS_TEXGEN | D3DVTXPCAPS_MATERIALSOURCE7 | D3DVTXPCAPS_DIRECTIONALLIGHTS |
497 D3DVTXPCAPS_POSITIONALLIGHTS | D3DVTXPCAPS_LOCALVIEWER | D3DVTXPCAPS_TWEENING |
498 D3DVTXPCAPS_TEXGEN_SPHEREMAP | D3DVTXPCAPS_NO_TEXGEN_NONLOCALVIEWER;
500 caps->TextureCaps &=
501 D3DPTEXTURECAPS_PERSPECTIVE | D3DPTEXTURECAPS_POW2 | D3DPTEXTURECAPS_ALPHA |
502 D3DPTEXTURECAPS_SQUAREONLY | D3DPTEXTURECAPS_TEXREPEATNOTSCALEDBYSIZE |
503 D3DPTEXTURECAPS_ALPHAPALETTE | D3DPTEXTURECAPS_NONPOW2CONDITIONAL |
504 D3DPTEXTURECAPS_PROJECTED | D3DPTEXTURECAPS_CUBEMAP | D3DPTEXTURECAPS_VOLUMEMAP |
505 D3DPTEXTURECAPS_MIPMAP | D3DPTEXTURECAPS_MIPVOLUMEMAP | D3DPTEXTURECAPS_MIPCUBEMAP |
506 D3DPTEXTURECAPS_CUBEMAP_POW2 | D3DPTEXTURECAPS_VOLUMEMAP_POW2| D3DPTEXTURECAPS_NOPROJECTEDBUMPENV;
508 caps->MaxVertexShaderConst = min(D3D9_MAX_VERTEX_SHADER_CONSTANTF, caps->MaxVertexShaderConst);
509 caps->NumSimultaneousRTs = min(D3D_MAX_SIMULTANEOUS_RENDERTARGETS, caps->NumSimultaneousRTs);
511 if (caps->PixelShaderVersion > 3)
513 caps->PixelShaderVersion = D3DPS_VERSION(3, 0);
515 else
517 DWORD major = caps->PixelShaderVersion;
518 caps->PixelShaderVersion = D3DPS_VERSION(major, ps_minor_version[major]);
521 if (caps->VertexShaderVersion > 3)
523 caps->VertexShaderVersion = D3DVS_VERSION(3, 0);
525 else
527 DWORD major = caps->VertexShaderVersion;
528 caps->VertexShaderVersion = D3DVS_VERSION(major, vs_minor_version[major]);
531 /* Get adapter group information */
532 output_idx = adapter_ordinal;
533 wined3d_output = d3d9->wined3d_outputs[output_idx];
535 wined3d_mutex_lock();
536 hr = wined3d_output_get_desc(wined3d_output, &output_desc);
537 wined3d_mutex_unlock();
539 if (FAILED(hr))
541 ERR("Failed to get output desc, hr %#x.\n", hr);
542 return;
545 caps->MasterAdapterOrdinal = output_idx - output_desc.ordinal;
546 caps->AdapterOrdinalInGroup = output_desc.ordinal;
547 if (!caps->AdapterOrdinalInGroup)
549 wined3d_adapter = wined3d_output_get_adapter(wined3d_output);
550 caps->NumberOfAdaptersInGroup = wined3d_adapter_get_output_count(wined3d_adapter);
552 else
554 caps->NumberOfAdaptersInGroup = 0;
558 static enum wined3d_texture_filter_type wined3d_texture_filter_type_from_d3d(D3DTEXTUREFILTERTYPE type)
560 return (enum wined3d_texture_filter_type)type;
563 static enum wined3d_transform_state wined3d_transform_state_from_d3d(D3DTRANSFORMSTATETYPE type)
565 return (enum wined3d_transform_state)type;
568 static enum wined3d_render_state wined3d_render_state_from_d3d(D3DRENDERSTATETYPE type)
570 return (enum wined3d_render_state)type;
573 static enum wined3d_sampler_state wined3d_sampler_state_from_d3d(D3DSAMPLERSTATETYPE type)
575 return (enum wined3d_sampler_state)type;
578 static enum wined3d_primitive_type wined3d_primitive_type_from_d3d(D3DPRIMITIVETYPE type)
580 return (enum wined3d_primitive_type)type;
583 static void device_reset_viewport_state(struct d3d9_device *device)
585 struct wined3d_viewport vp;
586 RECT rect;
588 wined3d_device_get_viewports(device->wined3d_device, NULL, &vp);
589 wined3d_stateblock_set_viewport(device->state, &vp);
590 wined3d_device_get_scissor_rects(device->wined3d_device, NULL, &rect);
591 wined3d_stateblock_set_scissor_rect(device->state, &rect);
594 static HRESULT WINAPI d3d9_device_QueryInterface(IDirect3DDevice9Ex *iface, REFIID riid, void **out)
596 TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
598 if (IsEqualGUID(riid, &IID_IDirect3DDevice9)
599 || IsEqualGUID(riid, &IID_IUnknown))
601 IDirect3DDevice9Ex_AddRef(iface);
602 *out = iface;
603 return S_OK;
606 if (IsEqualGUID(riid, &IID_IDirect3DDevice9Ex))
608 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
610 /* Find out if the creating d3d9 interface was created with Direct3DCreate9Ex.
611 * It doesn't matter with which function the device was created. */
612 if (!device->d3d_parent->extended)
614 WARN("IDirect3D9 instance wasn't created with CreateDirect3D9Ex, returning E_NOINTERFACE.\n");
615 *out = NULL;
616 return E_NOINTERFACE;
619 IDirect3DDevice9Ex_AddRef(iface);
620 *out = iface;
621 return S_OK;
624 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
626 *out = NULL;
627 return E_NOINTERFACE;
630 static ULONG WINAPI d3d9_device_AddRef(IDirect3DDevice9Ex *iface)
632 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
633 ULONG refcount = InterlockedIncrement(&device->refcount);
635 TRACE("%p increasing refcount to %u.\n", iface, refcount);
637 return refcount;
640 static ULONG WINAPI DECLSPEC_HOTPATCH d3d9_device_Release(IDirect3DDevice9Ex *iface)
642 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
643 ULONG refcount;
645 if (device->in_destruction)
646 return 0;
648 refcount = InterlockedDecrement(&device->refcount);
650 TRACE("%p decreasing refcount to %u.\n", iface, refcount);
652 if (!refcount)
654 unsigned i;
655 device->in_destruction = TRUE;
657 wined3d_mutex_lock();
658 for (i = 0; i < device->fvf_decl_count; ++i)
660 wined3d_vertex_declaration_decref(device->fvf_decls[i].decl);
662 heap_free(device->fvf_decls);
664 if (device->vertex_buffer)
665 wined3d_buffer_decref(device->vertex_buffer);
666 if (device->index_buffer)
667 wined3d_buffer_decref(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: %#x.\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 %#x.\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 %#x.\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 %#x.\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_swapchain_desc desc;
890 struct d3d9_swapchain *object;
891 unsigned int swap_interval;
892 unsigned int output_idx;
893 unsigned int i, count;
894 HRESULT hr;
896 TRACE("iface %p, present_parameters %p, swapchain %p.\n",
897 iface, present_parameters, swapchain);
899 if (!present_parameters->Windowed)
901 WARN("Trying to create an additional fullscreen swapchain, returning D3DERR_INVALIDCALL.\n");
902 return D3DERR_INVALIDCALL;
905 wined3d_mutex_lock();
906 count = wined3d_device_get_swapchain_count(device->wined3d_device);
907 for (i = 0; i < count; ++i)
909 struct wined3d_swapchain *wined3d_swapchain;
911 wined3d_swapchain = wined3d_device_get_swapchain(device->wined3d_device, i);
912 wined3d_swapchain_get_desc(wined3d_swapchain, &desc);
914 if (!desc.windowed)
916 wined3d_mutex_unlock();
917 WARN("Trying to create an additional swapchain in fullscreen mode, returning D3DERR_INVALIDCALL.\n");
918 return D3DERR_INVALIDCALL;
921 wined3d_mutex_unlock();
923 output_idx = device->adapter_ordinal;
924 if (!wined3d_swapchain_desc_from_d3d9(&desc, device->d3d_parent->wined3d_outputs[output_idx],
925 present_parameters, device->d3d_parent->extended))
926 return D3DERR_INVALIDCALL;
927 swap_interval = wined3dswapinterval_from_d3d(present_parameters->PresentationInterval);
928 if (SUCCEEDED(hr = d3d9_swapchain_create(device, &desc, swap_interval, &object)))
929 *swapchain = (IDirect3DSwapChain9 *)&object->IDirect3DSwapChain9Ex_iface;
930 present_parameters_from_wined3d_swapchain_desc(present_parameters,
931 &desc, present_parameters->PresentationInterval);
933 return hr;
936 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_GetSwapChain(IDirect3DDevice9Ex *iface,
937 UINT swapchain_idx, IDirect3DSwapChain9 **swapchain)
939 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
940 struct d3d9_swapchain *d3d9_swapchain;
941 HRESULT hr;
943 TRACE("iface %p, swapchain_idx %u, swapchain %p.\n", iface, swapchain_idx, swapchain);
945 wined3d_mutex_lock();
946 if (swapchain_idx < device->implicit_swapchain_count)
948 d3d9_swapchain = wined3d_swapchain_get_parent(device->implicit_swapchains[swapchain_idx]);
949 *swapchain = (IDirect3DSwapChain9 *)&d3d9_swapchain->IDirect3DSwapChain9Ex_iface;
950 IDirect3DSwapChain9Ex_AddRef(*swapchain);
951 hr = D3D_OK;
953 else
955 *swapchain = NULL;
956 hr = D3DERR_INVALIDCALL;
958 wined3d_mutex_unlock();
960 return hr;
963 static UINT WINAPI d3d9_device_GetNumberOfSwapChains(IDirect3DDevice9Ex *iface)
965 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
966 UINT count;
968 TRACE("iface %p.\n", iface);
970 wined3d_mutex_lock();
971 count = wined3d_device_get_swapchain_count(device->wined3d_device);
972 wined3d_mutex_unlock();
974 return count;
977 static HRESULT CDECL reset_enum_callback(struct wined3d_resource *resource)
979 struct d3d9_vertexbuffer *vertex_buffer;
980 struct d3d9_indexbuffer *index_buffer;
981 struct wined3d_resource_desc desc;
982 IDirect3DBaseTexture9 *texture;
983 struct d3d9_surface *surface;
984 IUnknown *parent;
986 wined3d_resource_get_desc(resource, &desc);
987 if (desc.access & WINED3D_RESOURCE_ACCESS_CPU)
988 return D3D_OK;
990 if (desc.resource_type != WINED3D_RTYPE_TEXTURE_2D)
992 if (desc.bind_flags & WINED3D_BIND_VERTEX_BUFFER)
994 vertex_buffer = wined3d_resource_get_parent(resource);
995 if (vertex_buffer && vertex_buffer->draw_buffer)
996 return D3D_OK;
998 if (desc.bind_flags & WINED3D_BIND_INDEX_BUFFER)
1000 index_buffer = wined3d_resource_get_parent(resource);
1001 if (index_buffer && index_buffer->draw_buffer)
1002 return D3D_OK;
1005 WARN("Resource %p in pool D3DPOOL_DEFAULT blocks the Reset call.\n", resource);
1006 return D3DERR_INVALIDCALL;
1009 parent = wined3d_resource_get_parent(resource);
1010 if (parent && SUCCEEDED(IUnknown_QueryInterface(parent, &IID_IDirect3DBaseTexture9, (void **)&texture)))
1012 IDirect3DBaseTexture9_Release(texture);
1013 WARN("Texture %p (resource %p) in pool D3DPOOL_DEFAULT blocks the Reset call.\n", texture, resource);
1014 return D3DERR_INVALIDCALL;
1017 surface = wined3d_texture_get_sub_resource_parent(wined3d_texture_from_resource(resource), 0);
1018 if (!surface->resource.refcount)
1019 return D3D_OK;
1021 WARN("Surface %p in pool D3DPOOL_DEFAULT blocks the Reset call.\n", surface);
1022 return D3DERR_INVALIDCALL;
1025 static HRESULT d3d9_device_get_swapchains(struct d3d9_device *device)
1027 UINT i, new_swapchain_count = wined3d_device_get_swapchain_count(device->wined3d_device);
1029 if (!(device->implicit_swapchains = heap_alloc(new_swapchain_count * sizeof(*device->implicit_swapchains))))
1030 return E_OUTOFMEMORY;
1032 for (i = 0; i < new_swapchain_count; ++i)
1034 device->implicit_swapchains[i] = wined3d_device_get_swapchain(device->wined3d_device, i);
1036 device->implicit_swapchain_count = new_swapchain_count;
1038 return D3D_OK;
1041 static HRESULT d3d9_device_reset(struct d3d9_device *device,
1042 D3DPRESENT_PARAMETERS *present_parameters, D3DDISPLAYMODEEX *mode)
1044 BOOL extended = device->d3d_parent->extended;
1045 struct wined3d_swapchain_desc swapchain_desc;
1046 struct wined3d_display_mode wined3d_mode;
1047 struct wined3d_rendertarget_view *rtv;
1048 struct d3d9_swapchain *d3d9_swapchain;
1049 unsigned int output_idx;
1050 unsigned int i;
1051 HRESULT hr;
1053 if (!extended && device->device_state == D3D9_DEVICE_STATE_LOST)
1055 WARN("App not active, returning D3DERR_DEVICELOST.\n");
1056 return D3DERR_DEVICELOST;
1059 if (mode)
1061 wined3d_mode.width = mode->Width;
1062 wined3d_mode.height = mode->Height;
1063 wined3d_mode.refresh_rate = mode->RefreshRate;
1064 wined3d_mode.format_id = wined3dformat_from_d3dformat(mode->Format);
1065 wined3d_mode.scanline_ordering = wined3d_scanline_ordering_from_d3d(mode->ScanLineOrdering);
1068 output_idx = device->adapter_ordinal;
1069 if (!wined3d_swapchain_desc_from_d3d9(&swapchain_desc,
1070 device->d3d_parent->wined3d_outputs[output_idx], present_parameters, extended))
1071 return D3DERR_INVALIDCALL;
1072 swapchain_desc.flags |= WINED3D_SWAPCHAIN_IMPLICIT;
1074 wined3d_mutex_lock();
1076 if (device->vertex_buffer)
1078 wined3d_buffer_decref(device->vertex_buffer);
1079 device->vertex_buffer = NULL;
1080 device->vertex_buffer_size = 0;
1083 if (device->index_buffer)
1085 wined3d_buffer_decref(device->index_buffer);
1086 device->index_buffer = NULL;
1087 device->index_buffer_size = 0;
1090 if (!extended)
1092 if (device->recording)
1093 wined3d_stateblock_decref(device->recording);
1094 device->recording = NULL;
1095 device->update_state = device->state;
1096 wined3d_stateblock_reset(device->state);
1099 if (SUCCEEDED(hr = wined3d_device_reset(device->wined3d_device, &swapchain_desc,
1100 mode ? &wined3d_mode : NULL, reset_enum_callback, !extended)))
1102 heap_free(device->implicit_swapchains);
1104 if (!extended)
1106 device->auto_mipmaps = 0;
1107 wined3d_stateblock_set_render_state(device->state, WINED3D_RS_ZENABLE,
1108 !!swapchain_desc.enable_auto_depth_stencil);
1109 device_reset_viewport_state(device);
1112 if (FAILED(hr = d3d9_device_get_swapchains(device)))
1114 device->device_state = D3D9_DEVICE_STATE_NOT_RESET;
1116 else
1118 d3d9_swapchain = wined3d_swapchain_get_parent(device->implicit_swapchains[0]);
1119 d3d9_swapchain->swap_interval
1120 = wined3dswapinterval_from_d3d(present_parameters->PresentationInterval);
1121 wined3d_swapchain_get_desc(d3d9_swapchain->wined3d_swapchain, &swapchain_desc);
1122 present_parameters->BackBufferWidth = swapchain_desc.backbuffer_width;
1123 present_parameters->BackBufferHeight = swapchain_desc.backbuffer_height;
1124 present_parameters->BackBufferFormat = d3dformat_from_wined3dformat(swapchain_desc.backbuffer_format);
1125 present_parameters->BackBufferCount = swapchain_desc.backbuffer_count;
1127 device->device_state = D3D9_DEVICE_STATE_OK;
1129 if (extended)
1131 const struct wined3d_viewport *current = &device->stateblock_state->viewport;
1132 struct wined3d_viewport vp;
1133 RECT rect;
1135 vp.x = 0;
1136 vp.y = 0;
1137 vp.width = swapchain_desc.backbuffer_width;
1138 vp.height = swapchain_desc.backbuffer_height;
1139 vp.min_z = current->min_z;
1140 vp.max_z = current->max_z;
1141 wined3d_stateblock_set_viewport(device->state, &vp);
1143 SetRect(&rect, 0, 0, swapchain_desc.backbuffer_width, swapchain_desc.backbuffer_height);
1144 wined3d_stateblock_set_scissor_rect(device->state, &rect);
1148 rtv = wined3d_device_get_rendertarget_view(device->wined3d_device, 0);
1149 device->render_targets[0] = wined3d_rendertarget_view_get_sub_resource_parent(rtv);
1150 for (i = 1; i < ARRAY_SIZE(device->render_targets); ++i)
1151 device->render_targets[i] = NULL;
1153 else if (!extended)
1155 device->device_state = D3D9_DEVICE_STATE_NOT_RESET;
1158 wined3d_mutex_unlock();
1160 return hr;
1163 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_Reset(IDirect3DDevice9Ex *iface,
1164 D3DPRESENT_PARAMETERS *present_parameters)
1166 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1168 TRACE("iface %p, present_parameters %p.\n", iface, present_parameters);
1170 return d3d9_device_reset(device, present_parameters, NULL);
1173 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_Present(IDirect3DDevice9Ex *iface,
1174 const RECT *src_rect, const RECT *dst_rect, HWND dst_window_override, const RGNDATA *dirty_region)
1176 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1177 struct d3d9_swapchain *swapchain;
1178 unsigned int i;
1179 HRESULT hr;
1181 TRACE("iface %p, src_rect %s, dst_rect %s, dst_window_override %p, dirty_region %p.\n",
1182 iface, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect), dst_window_override, dirty_region);
1184 if (device->device_state != D3D9_DEVICE_STATE_OK)
1185 return device->d3d_parent->extended ? S_PRESENT_OCCLUDED : D3DERR_DEVICELOST;
1187 if (dirty_region)
1188 FIXME("Ignoring dirty_region %p.\n", dirty_region);
1190 wined3d_mutex_lock();
1191 for (i = 0; i < device->implicit_swapchain_count; ++i)
1193 swapchain = wined3d_swapchain_get_parent(device->implicit_swapchains[i]);
1194 if (FAILED(hr = wined3d_swapchain_present(swapchain->wined3d_swapchain,
1195 src_rect, dst_rect, dst_window_override, swapchain->swap_interval, 0)))
1197 wined3d_mutex_unlock();
1198 return hr;
1201 wined3d_mutex_unlock();
1203 return D3D_OK;
1206 static HRESULT WINAPI d3d9_device_GetBackBuffer(IDirect3DDevice9Ex *iface, UINT swapchain,
1207 UINT backbuffer_idx, D3DBACKBUFFER_TYPE backbuffer_type, IDirect3DSurface9 **backbuffer)
1209 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1210 struct d3d9_swapchain *d3d9_swapchain;
1211 HRESULT hr;
1213 TRACE("iface %p, swapchain %u, backbuffer_idx %u, backbuffer_type %#x, backbuffer %p.\n",
1214 iface, swapchain, backbuffer_idx, backbuffer_type, backbuffer);
1216 /* backbuffer_type is ignored by native. */
1218 /* No need to check for backbuffer == NULL, Windows crashes in that case. */
1219 *backbuffer = NULL;
1221 wined3d_mutex_lock();
1222 if (swapchain >= device->implicit_swapchain_count)
1224 wined3d_mutex_unlock();
1225 WARN("Swapchain index %u is out of range, returning D3DERR_INVALIDCALL.\n", swapchain);
1226 return D3DERR_INVALIDCALL;
1229 d3d9_swapchain = wined3d_swapchain_get_parent(device->implicit_swapchains[swapchain]);
1230 hr = IDirect3DSwapChain9Ex_GetBackBuffer(&d3d9_swapchain->IDirect3DSwapChain9Ex_iface,
1231 backbuffer_idx, backbuffer_type, backbuffer);
1232 wined3d_mutex_unlock();
1234 return hr;
1237 static HRESULT WINAPI d3d9_device_GetRasterStatus(IDirect3DDevice9Ex *iface,
1238 UINT swapchain, D3DRASTER_STATUS *raster_status)
1240 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1241 HRESULT hr;
1243 TRACE("iface %p, swapchain %u, raster_status %p.\n", iface, swapchain, raster_status);
1245 wined3d_mutex_lock();
1246 hr = wined3d_device_get_raster_status(device->wined3d_device,
1247 swapchain, (struct wined3d_raster_status *)raster_status);
1248 wined3d_mutex_unlock();
1250 return hr;
1253 static HRESULT WINAPI d3d9_device_SetDialogBoxMode(IDirect3DDevice9Ex *iface, BOOL enable)
1255 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1256 HRESULT hr;
1258 TRACE("iface %p, enable %#x.\n", iface, enable);
1260 wined3d_mutex_lock();
1261 hr = wined3d_device_set_dialog_box_mode(device->wined3d_device, enable);
1262 wined3d_mutex_unlock();
1264 return hr;
1267 static void WINAPI d3d9_device_SetGammaRamp(IDirect3DDevice9Ex *iface,
1268 UINT swapchain, DWORD flags, const D3DGAMMARAMP *ramp)
1270 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1272 TRACE("iface %p, swapchain %u, flags %#x, ramp %p.\n", iface, swapchain, flags, ramp);
1274 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
1275 wined3d_mutex_lock();
1276 wined3d_device_set_gamma_ramp(device->wined3d_device, swapchain, flags, (const struct wined3d_gamma_ramp *)ramp);
1277 wined3d_mutex_unlock();
1280 static void WINAPI d3d9_device_GetGammaRamp(IDirect3DDevice9Ex *iface, UINT swapchain, D3DGAMMARAMP *ramp)
1282 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1284 TRACE("iface %p, swapchain %u, ramp %p.\n", iface, swapchain, ramp);
1286 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
1287 wined3d_mutex_lock();
1288 wined3d_device_get_gamma_ramp(device->wined3d_device, swapchain, (struct wined3d_gamma_ramp *)ramp);
1289 wined3d_mutex_unlock();
1292 static HRESULT WINAPI d3d9_device_CreateTexture(IDirect3DDevice9Ex *iface,
1293 UINT width, UINT height, UINT levels, DWORD usage, D3DFORMAT format,
1294 D3DPOOL pool, IDirect3DTexture9 **texture, HANDLE *shared_handle)
1296 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1297 struct d3d9_texture *object;
1298 BOOL set_mem = FALSE;
1299 HRESULT hr;
1301 TRACE("iface %p, width %u, height %u, levels %u, usage %#x, format %#x, pool %#x, texture %p, shared_handle %p.\n",
1302 iface, width, height, levels, usage, format, pool, texture, shared_handle);
1304 *texture = NULL;
1305 if (shared_handle)
1307 if (!device->d3d_parent->extended)
1309 WARN("Trying to create a shared or user memory texture on a non-ex device.\n");
1310 return E_NOTIMPL;
1313 if (pool == D3DPOOL_SYSTEMMEM)
1315 if (levels != 1)
1316 return D3DERR_INVALIDCALL;
1317 set_mem = TRUE;
1319 else
1321 if (pool != D3DPOOL_DEFAULT)
1323 WARN("Trying to create a shared texture in pool %#x.\n", pool);
1324 return D3DERR_INVALIDCALL;
1326 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1330 if (!(object = heap_alloc_zero(sizeof(*object))))
1331 return D3DERR_OUTOFVIDEOMEMORY;
1333 hr = texture_init(object, device, width, height, levels, usage, format, pool);
1334 if (FAILED(hr))
1336 WARN("Failed to initialize texture, hr %#x.\n", hr);
1337 heap_free(object);
1338 return hr;
1341 if (set_mem)
1342 wined3d_texture_update_desc(object->wined3d_texture, 0, width, height,
1343 wined3dformat_from_d3dformat(format), WINED3D_MULTISAMPLE_NONE, 0,
1344 *shared_handle, 0);
1346 TRACE("Created texture %p.\n", object);
1347 *texture = (IDirect3DTexture9 *)&object->IDirect3DBaseTexture9_iface;
1349 return D3D_OK;
1352 static HRESULT WINAPI d3d9_device_CreateVolumeTexture(IDirect3DDevice9Ex *iface,
1353 UINT width, UINT height, UINT depth, UINT levels, DWORD usage, D3DFORMAT format,
1354 D3DPOOL pool, IDirect3DVolumeTexture9 **texture, HANDLE *shared_handle)
1356 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1357 struct d3d9_texture *object;
1358 HRESULT hr;
1360 TRACE("iface %p, width %u, height %u, depth %u, levels %u, "
1361 "usage %#x, format %#x, pool %#x, texture %p, shared_handle %p.\n",
1362 iface, width, height, depth, levels,
1363 usage, format, pool, texture, shared_handle);
1365 *texture = NULL;
1366 if (shared_handle)
1368 if (!device->d3d_parent->extended)
1370 WARN("Trying to create a shared volume texture on a non-ex device.\n");
1371 return E_NOTIMPL;
1374 if (pool != D3DPOOL_DEFAULT)
1376 WARN("Trying to create a shared volume texture in pool %#x.\n", pool);
1377 return D3DERR_INVALIDCALL;
1379 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1382 if (!(object = heap_alloc_zero(sizeof(*object))))
1383 return D3DERR_OUTOFVIDEOMEMORY;
1385 hr = volumetexture_init(object, device, width, height, depth, levels, usage, format, pool);
1386 if (FAILED(hr))
1388 WARN("Failed to initialize volume texture, hr %#x.\n", hr);
1389 heap_free(object);
1390 return hr;
1393 TRACE("Created volume texture %p.\n", object);
1394 *texture = (IDirect3DVolumeTexture9 *)&object->IDirect3DBaseTexture9_iface;
1396 return D3D_OK;
1399 static HRESULT WINAPI d3d9_device_CreateCubeTexture(IDirect3DDevice9Ex *iface,
1400 UINT edge_length, UINT levels, DWORD usage, D3DFORMAT format, D3DPOOL pool,
1401 IDirect3DCubeTexture9 **texture, HANDLE *shared_handle)
1403 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1404 struct d3d9_texture *object;
1405 HRESULT hr;
1407 TRACE("iface %p, edge_length %u, levels %u, usage %#x, format %#x, pool %#x, texture %p, shared_handle %p.\n",
1408 iface, edge_length, levels, usage, format, pool, texture, shared_handle);
1410 *texture = NULL;
1411 if (shared_handle)
1413 if (!device->d3d_parent->extended)
1415 WARN("Trying to create a shared cube texture on a non-ex device.\n");
1416 return E_NOTIMPL;
1419 if (pool != D3DPOOL_DEFAULT)
1421 WARN("Trying to create a shared cube texture in pool %#x.\n", pool);
1422 return D3DERR_INVALIDCALL;
1424 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1427 if (!(object = heap_alloc_zero(sizeof(*object))))
1428 return D3DERR_OUTOFVIDEOMEMORY;
1430 hr = cubetexture_init(object, device, edge_length, levels, usage, format, pool);
1431 if (FAILED(hr))
1433 WARN("Failed to initialize cube texture, hr %#x.\n", hr);
1434 heap_free(object);
1435 return hr;
1438 TRACE("Created cube texture %p.\n", object);
1439 *texture = (IDirect3DCubeTexture9 *)&object->IDirect3DBaseTexture9_iface;
1441 return D3D_OK;
1444 static HRESULT WINAPI d3d9_device_CreateVertexBuffer(IDirect3DDevice9Ex *iface, UINT size,
1445 DWORD usage, DWORD fvf, D3DPOOL pool, IDirect3DVertexBuffer9 **buffer,
1446 HANDLE *shared_handle)
1448 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1449 struct d3d9_vertexbuffer *object;
1450 HRESULT hr;
1452 TRACE("iface %p, size %u, usage %#x, fvf %#x, pool %#x, buffer %p, shared_handle %p.\n",
1453 iface, size, usage, fvf, pool, buffer, shared_handle);
1455 if (shared_handle)
1457 if (!device->d3d_parent->extended)
1459 WARN("Trying to create a shared vertex buffer on a non-ex device.\n");
1460 return E_NOTIMPL;
1463 if (pool != D3DPOOL_DEFAULT)
1465 WARN("Trying to create a shared vertex buffer in pool %#x.\n", pool);
1466 return D3DERR_NOTAVAILABLE;
1468 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1471 if (!(object = heap_alloc_zero(sizeof(*object))))
1472 return D3DERR_OUTOFVIDEOMEMORY;
1474 hr = vertexbuffer_init(object, device, size, usage, fvf, pool);
1475 if (FAILED(hr))
1477 WARN("Failed to initialize vertex buffer, hr %#x.\n", hr);
1478 heap_free(object);
1479 return hr;
1482 TRACE("Created vertex buffer %p.\n", object);
1483 *buffer = &object->IDirect3DVertexBuffer9_iface;
1485 return D3D_OK;
1488 static HRESULT WINAPI d3d9_device_CreateIndexBuffer(IDirect3DDevice9Ex *iface, UINT size,
1489 DWORD usage, D3DFORMAT format, D3DPOOL pool, IDirect3DIndexBuffer9 **buffer,
1490 HANDLE *shared_handle)
1492 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1493 struct d3d9_indexbuffer *object;
1494 HRESULT hr;
1496 TRACE("iface %p, size %u, usage %#x, format %#x, pool %#x, buffer %p, shared_handle %p.\n",
1497 iface, size, usage, format, pool, buffer, shared_handle);
1499 if (shared_handle)
1501 if (!device->d3d_parent->extended)
1503 WARN("Trying to create a shared index buffer on a non-ex device.\n");
1504 return E_NOTIMPL;
1507 if (pool != D3DPOOL_DEFAULT)
1509 WARN("Trying to create a shared index buffer in pool %#x.\n", pool);
1510 return D3DERR_NOTAVAILABLE;
1512 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1515 if (!(object = heap_alloc_zero(sizeof(*object))))
1516 return D3DERR_OUTOFVIDEOMEMORY;
1518 hr = indexbuffer_init(object, device, size, usage, format, pool);
1519 if (FAILED(hr))
1521 WARN("Failed to initialize index buffer, hr %#x.\n", hr);
1522 heap_free(object);
1523 return hr;
1526 TRACE("Created index buffer %p.\n", object);
1527 *buffer = &object->IDirect3DIndexBuffer9_iface;
1529 return D3D_OK;
1532 static HRESULT d3d9_device_create_surface(struct d3d9_device *device, unsigned int flags,
1533 enum wined3d_format_id format, enum wined3d_multisample_type multisample_type,
1534 unsigned int multisample_quality, unsigned int usage, unsigned int bind_flags, unsigned int access,
1535 unsigned int width, unsigned int height, void *user_mem, IDirect3DSurface9 **surface)
1537 struct wined3d_resource_desc desc;
1538 struct d3d9_surface *surface_impl;
1539 struct wined3d_texture *texture;
1540 HRESULT hr;
1542 TRACE("device %p, flags %#x, format %#x, multisample_type %#x, multisample_quality %u, "
1543 "usage %#x, bind_flags %#x, access %#x, width %u, height %u, user_mem %p, surface %p.\n",
1544 device, flags, format, multisample_type, multisample_quality, usage,
1545 bind_flags, access, width, height, user_mem, surface);
1547 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
1548 desc.format = format;
1549 desc.multisample_type = multisample_type;
1550 desc.multisample_quality = multisample_quality;
1551 desc.usage = usage;
1552 desc.bind_flags = bind_flags;
1553 desc.access = access;
1554 desc.width = width;
1555 desc.height = height;
1556 desc.depth = 1;
1557 desc.size = 0;
1559 if (is_gdi_compat_wined3dformat(desc.format))
1560 flags |= WINED3D_TEXTURE_CREATE_GET_DC;
1562 wined3d_mutex_lock();
1564 if (FAILED(hr = wined3d_texture_create(device->wined3d_device, &desc,
1565 1, 1, flags, NULL, NULL, &d3d9_null_wined3d_parent_ops, &texture)))
1567 wined3d_mutex_unlock();
1568 WARN("Failed to create texture, hr %#x.\n", hr);
1569 if (hr == WINED3DERR_NOTAVAILABLE)
1570 hr = D3DERR_INVALIDCALL;
1571 return hr;
1574 surface_impl = wined3d_texture_get_sub_resource_parent(texture, 0);
1575 surface_impl->parent_device = &device->IDirect3DDevice9Ex_iface;
1576 *surface = &surface_impl->IDirect3DSurface9_iface;
1577 IDirect3DSurface9_AddRef(*surface);
1579 if (user_mem)
1580 wined3d_texture_update_desc(texture, 0, width, height, desc.format,
1581 multisample_type, multisample_quality, user_mem, 0);
1583 wined3d_texture_decref(texture);
1585 wined3d_mutex_unlock();
1587 return D3D_OK;
1590 BOOL is_gdi_compat_wined3dformat(enum wined3d_format_id format)
1592 switch (format)
1594 case WINED3DFMT_B8G8R8A8_UNORM:
1595 case WINED3DFMT_B8G8R8X8_UNORM:
1596 case WINED3DFMT_B5G6R5_UNORM:
1597 case WINED3DFMT_B5G5R5X1_UNORM:
1598 case WINED3DFMT_B5G5R5A1_UNORM:
1599 case WINED3DFMT_B8G8R8_UNORM:
1600 return TRUE;
1601 default:
1602 return FALSE;
1606 static HRESULT WINAPI d3d9_device_CreateRenderTarget(IDirect3DDevice9Ex *iface, UINT width, UINT height,
1607 D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
1608 BOOL lockable, IDirect3DSurface9 **surface, HANDLE *shared_handle)
1610 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1611 unsigned int access = WINED3D_RESOURCE_ACCESS_GPU;
1613 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u.\n"
1614 "lockable %#x, surface %p, shared_handle %p.\n",
1615 iface, width, height, format, multisample_type, multisample_quality,
1616 lockable, surface, shared_handle);
1618 *surface = NULL;
1619 if (shared_handle)
1621 if (!device->d3d_parent->extended)
1623 WARN("Trying to create a shared render target on a non-ex device.\n");
1624 return E_NOTIMPL;
1627 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1630 if (lockable)
1631 access |= WINED3D_RESOURCE_ACCESS_MAP_R | WINED3D_RESOURCE_ACCESS_MAP_W;
1633 return d3d9_device_create_surface(device, 0, wined3dformat_from_d3dformat(format),
1634 wined3d_multisample_type_from_d3d(multisample_type), multisample_quality, 0,
1635 WINED3D_BIND_RENDER_TARGET, access, width, height, NULL, surface);
1638 static HRESULT WINAPI d3d9_device_CreateDepthStencilSurface(IDirect3DDevice9Ex *iface, UINT width, UINT height,
1639 D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
1640 BOOL discard, IDirect3DSurface9 **surface, HANDLE *shared_handle)
1642 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1643 DWORD flags = 0;
1645 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u.\n"
1646 "discard %#x, surface %p, shared_handle %p.\n",
1647 iface, width, height, format, multisample_type, multisample_quality,
1648 discard, surface, shared_handle);
1650 *surface = NULL;
1651 if (shared_handle)
1653 if (!device->d3d_parent->extended)
1655 WARN("Trying to create a shared depth stencil on a non-ex device.\n");
1656 return E_NOTIMPL;
1659 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1662 if (discard)
1663 flags |= WINED3D_TEXTURE_CREATE_DISCARD;
1665 return d3d9_device_create_surface(device, flags, wined3dformat_from_d3dformat(format),
1666 wined3d_multisample_type_from_d3d(multisample_type), multisample_quality, 0,
1667 WINED3D_BIND_DEPTH_STENCIL, WINED3D_RESOURCE_ACCESS_GPU, width, height, NULL, surface);
1671 static HRESULT WINAPI d3d9_device_UpdateSurface(IDirect3DDevice9Ex *iface,
1672 IDirect3DSurface9 *src_surface, const RECT *src_rect,
1673 IDirect3DSurface9 *dst_surface, const POINT *dst_point)
1675 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1676 struct d3d9_surface *src = unsafe_impl_from_IDirect3DSurface9(src_surface);
1677 struct d3d9_surface *dst = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1678 struct wined3d_sub_resource_desc src_desc, dst_desc;
1679 struct wined3d_box src_box;
1680 HRESULT hr;
1682 TRACE("iface %p, src_surface %p, src_rect %s, dst_surface %p, dst_point %p.\n",
1683 iface, src_surface, wine_dbgstr_rect(src_rect), dst_surface, dst_point);
1685 wined3d_mutex_lock();
1687 wined3d_texture_get_sub_resource_desc(src->wined3d_texture, src->sub_resource_idx, &src_desc);
1688 wined3d_texture_get_sub_resource_desc(dst->wined3d_texture, dst->sub_resource_idx, &dst_desc);
1689 if (src_desc.format != dst_desc.format)
1691 wined3d_mutex_unlock();
1692 WARN("Surface formats (%#x/%#x) don't match.\n",
1693 d3dformat_from_wined3dformat(src_desc.format),
1694 d3dformat_from_wined3dformat(dst_desc.format));
1695 return D3DERR_INVALIDCALL;
1698 if (src_rect)
1699 wined3d_box_set(&src_box, src_rect->left, src_rect->top, src_rect->right, src_rect->bottom, 0, 1);
1700 else
1701 wined3d_box_set(&src_box, 0, 0, src_desc.width, src_desc.height, 0, 1);
1703 hr = wined3d_device_copy_sub_resource_region(device->wined3d_device,
1704 wined3d_texture_get_resource(dst->wined3d_texture), dst->sub_resource_idx, dst_point ? dst_point->x : 0,
1705 dst_point ? dst_point->y : 0, 0, wined3d_texture_get_resource(src->wined3d_texture),
1706 src->sub_resource_idx, &src_box, 0);
1707 if (SUCCEEDED(hr) && dst->texture)
1708 d3d9_texture_flag_auto_gen_mipmap(dst->texture);
1710 wined3d_mutex_unlock();
1712 if (FAILED(hr))
1713 return D3DERR_INVALIDCALL;
1715 return hr;
1718 static HRESULT WINAPI d3d9_device_UpdateTexture(IDirect3DDevice9Ex *iface,
1719 IDirect3DBaseTexture9 *src_texture, IDirect3DBaseTexture9 *dst_texture)
1721 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1722 struct d3d9_texture *src_impl, *dst_impl;
1723 HRESULT hr;
1725 TRACE("iface %p, src_texture %p, dst_texture %p.\n", iface, src_texture, dst_texture);
1727 src_impl = unsafe_impl_from_IDirect3DBaseTexture9(src_texture);
1728 dst_impl = unsafe_impl_from_IDirect3DBaseTexture9(dst_texture);
1730 wined3d_mutex_lock();
1731 hr = wined3d_device_update_texture(device->wined3d_device,
1732 src_impl->wined3d_texture, dst_impl->wined3d_texture);
1733 if (SUCCEEDED(hr))
1734 d3d9_texture_flag_auto_gen_mipmap(dst_impl);
1735 wined3d_mutex_unlock();
1737 return hr;
1740 static HRESULT WINAPI d3d9_device_GetRenderTargetData(IDirect3DDevice9Ex *iface,
1741 IDirect3DSurface9 *render_target, IDirect3DSurface9 *dst_surface)
1743 struct d3d9_surface *rt_impl = unsafe_impl_from_IDirect3DSurface9(render_target);
1744 struct d3d9_surface *dst_impl = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1745 struct wined3d_sub_resource_desc wined3d_desc;
1746 RECT dst_rect, src_rect;
1747 HRESULT hr;
1749 TRACE("iface %p, render_target %p, dst_surface %p.\n", iface, render_target, dst_surface);
1751 if (!render_target || !dst_surface)
1752 return D3DERR_INVALIDCALL;
1754 wined3d_mutex_lock();
1755 wined3d_texture_get_sub_resource_desc(dst_impl->wined3d_texture, dst_impl->sub_resource_idx, &wined3d_desc);
1756 SetRect(&dst_rect, 0, 0, wined3d_desc.width, wined3d_desc.height);
1758 wined3d_texture_get_sub_resource_desc(rt_impl->wined3d_texture, rt_impl->sub_resource_idx, &wined3d_desc);
1759 SetRect(&src_rect, 0, 0, wined3d_desc.width, wined3d_desc.height);
1761 /* TODO: Check surface sizes, pools, etc. */
1762 if (wined3d_desc.multisample_type)
1763 hr = D3DERR_INVALIDCALL;
1764 else
1765 hr = wined3d_texture_blt(dst_impl->wined3d_texture, dst_impl->sub_resource_idx, &dst_rect,
1766 rt_impl->wined3d_texture, rt_impl->sub_resource_idx, &src_rect, 0, NULL, WINED3D_TEXF_POINT);
1767 wined3d_mutex_unlock();
1769 return hr;
1772 static HRESULT WINAPI d3d9_device_GetFrontBufferData(IDirect3DDevice9Ex *iface,
1773 UINT swapchain, IDirect3DSurface9 *dst_surface)
1775 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1776 struct d3d9_surface *dst_impl = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1777 HRESULT hr = D3DERR_INVALIDCALL;
1779 TRACE("iface %p, swapchain %u, dst_surface %p.\n", iface, swapchain, dst_surface);
1781 wined3d_mutex_lock();
1782 if (swapchain < device->implicit_swapchain_count)
1783 hr = wined3d_swapchain_get_front_buffer_data(device->implicit_swapchains[swapchain],
1784 dst_impl->wined3d_texture, dst_impl->sub_resource_idx);
1785 wined3d_mutex_unlock();
1787 return hr;
1790 static HRESULT WINAPI d3d9_device_StretchRect(IDirect3DDevice9Ex *iface, IDirect3DSurface9 *src_surface,
1791 const RECT *src_rect, IDirect3DSurface9 *dst_surface, const RECT *dst_rect, D3DTEXTUREFILTERTYPE filter)
1793 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1794 struct d3d9_surface *src = unsafe_impl_from_IDirect3DSurface9(src_surface);
1795 struct d3d9_surface *dst = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1796 struct wined3d_sub_resource_desc src_desc, dst_desc;
1797 HRESULT hr = D3DERR_INVALIDCALL;
1798 RECT d, s;
1800 TRACE("iface %p, src_surface %p, src_rect %s, dst_surface %p, dst_rect %s, filter %#x.\n",
1801 iface, src_surface, wine_dbgstr_rect(src_rect), dst_surface, wine_dbgstr_rect(dst_rect), filter);
1803 wined3d_mutex_lock();
1804 wined3d_texture_get_sub_resource_desc(dst->wined3d_texture, dst->sub_resource_idx, &dst_desc);
1805 if (!dst_rect)
1807 SetRect(&d, 0, 0, dst_desc.width, dst_desc.height);
1808 dst_rect = &d;
1811 wined3d_texture_get_sub_resource_desc(src->wined3d_texture, src->sub_resource_idx, &src_desc);
1812 if (!src_rect)
1814 SetRect(&s, 0, 0, src_desc.width, src_desc.height);
1815 src_rect = &s;
1818 if (dst_desc.access & WINED3D_RESOURCE_ACCESS_CPU)
1820 WARN("Destination resource is not in DEFAULT pool.\n");
1821 goto done;
1823 if (src_desc.access & WINED3D_RESOURCE_ACCESS_CPU)
1825 WARN("Source resource is not in DEFAULT pool.\n");
1826 goto done;
1829 if (dst->texture && !(dst_desc.bind_flags & (WINED3D_BIND_RENDER_TARGET | WINED3D_BIND_DEPTH_STENCIL)))
1831 WARN("Destination is a regular texture.\n");
1832 goto done;
1835 if (src_desc.bind_flags & WINED3D_BIND_DEPTH_STENCIL)
1837 if (device->in_scene)
1839 WARN("Rejecting depth / stencil blit while in scene.\n");
1840 goto done;
1843 if (src_rect->left || src_rect->top || src_rect->right != src_desc.width
1844 || src_rect->bottom != src_desc.height)
1846 WARN("Rejecting depth / stencil blit with invalid source rect %s.\n",
1847 wine_dbgstr_rect(src_rect));
1848 goto done;
1851 if (dst_rect->left || dst_rect->top || dst_rect->right != dst_desc.width
1852 || dst_rect->bottom != dst_desc.height)
1854 WARN("Rejecting depth / stencil blit with invalid destination rect %s.\n",
1855 wine_dbgstr_rect(dst_rect));
1856 goto done;
1859 if (src_desc.width != dst_desc.width || src_desc.height != dst_desc.height)
1861 WARN("Rejecting depth / stencil blit with mismatched surface sizes.\n");
1862 goto done;
1866 hr = wined3d_texture_blt(dst->wined3d_texture, dst->sub_resource_idx, dst_rect, src->wined3d_texture,
1867 src->sub_resource_idx, src_rect, 0, NULL, wined3d_texture_filter_type_from_d3d(filter));
1868 if (hr == WINEDDERR_INVALIDRECT)
1869 hr = D3DERR_INVALIDCALL;
1870 if (SUCCEEDED(hr) && dst->texture)
1871 d3d9_texture_flag_auto_gen_mipmap(dst->texture);
1873 done:
1874 wined3d_mutex_unlock();
1875 return hr;
1878 static HRESULT WINAPI d3d9_device_ColorFill(IDirect3DDevice9Ex *iface,
1879 IDirect3DSurface9 *surface, const RECT *rect, D3DCOLOR color)
1881 const struct wined3d_color c =
1883 ((color >> 16) & 0xff) / 255.0f,
1884 ((color >> 8) & 0xff) / 255.0f,
1885 (color & 0xff) / 255.0f,
1886 ((color >> 24) & 0xff) / 255.0f,
1888 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1889 struct d3d9_surface *surface_impl = unsafe_impl_from_IDirect3DSurface9(surface);
1890 struct wined3d_sub_resource_desc desc;
1891 struct wined3d_rendertarget_view *rtv;
1892 HRESULT hr;
1894 TRACE("iface %p, surface %p, rect %p, color 0x%08x.\n", iface, surface, rect, color);
1896 if (!surface)
1897 return D3DERR_INVALIDCALL;
1899 wined3d_mutex_lock();
1901 if (FAILED(wined3d_texture_get_sub_resource_desc(surface_impl->wined3d_texture,
1902 surface_impl->sub_resource_idx, &desc)))
1904 wined3d_mutex_unlock();
1905 return D3DERR_INVALIDCALL;
1908 if (desc.access & WINED3D_RESOURCE_ACCESS_CPU)
1910 wined3d_mutex_unlock();
1911 WARN("Colour fills are not allowed on surfaces with resource access %#x.\n", desc.access);
1912 return D3DERR_INVALIDCALL;
1914 if ((desc.bind_flags & (WINED3D_BIND_RENDER_TARGET | WINED3D_BIND_SHADER_RESOURCE)) == WINED3D_BIND_SHADER_RESOURCE)
1916 wined3d_mutex_unlock();
1917 WARN("Colorfill is not allowed on non-RT textures, returning D3DERR_INVALIDCALL.\n");
1918 return D3DERR_INVALIDCALL;
1920 if (desc.bind_flags & WINED3D_BIND_DEPTH_STENCIL)
1922 wined3d_mutex_unlock();
1923 WARN("Colorfill is not allowed on depth stencil surfaces, returning D3DERR_INVALIDCALL.\n");
1924 return D3DERR_INVALIDCALL;
1927 wined3d_device_apply_stateblock(device->wined3d_device, device->state);
1928 rtv = d3d9_surface_acquire_rendertarget_view(surface_impl);
1929 hr = wined3d_device_clear_rendertarget_view(device->wined3d_device,
1930 rtv, rect, WINED3DCLEAR_TARGET, &c, 0.0f, 0);
1931 d3d9_surface_release_rendertarget_view(surface_impl, rtv);
1932 if (SUCCEEDED(hr) && surface_impl->texture)
1933 d3d9_texture_flag_auto_gen_mipmap(surface_impl->texture);
1935 wined3d_mutex_unlock();
1937 return hr;
1940 static HRESULT WINAPI d3d9_device_CreateOffscreenPlainSurface(IDirect3DDevice9Ex *iface,
1941 UINT width, UINT height, D3DFORMAT format, D3DPOOL pool, IDirect3DSurface9 **surface,
1942 HANDLE *shared_handle)
1944 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1945 unsigned int usage, access;
1946 void *user_mem = NULL;
1948 TRACE("iface %p, width %u, height %u, format %#x, pool %#x, surface %p, shared_handle %p.\n",
1949 iface, width, height, format, pool, surface, shared_handle);
1951 *surface = NULL;
1952 if (pool == D3DPOOL_MANAGED)
1954 WARN("Attempting to create a managed offscreen plain surface.\n");
1955 return D3DERR_INVALIDCALL;
1958 if (shared_handle)
1960 if (!device->d3d_parent->extended)
1962 WARN("Trying to create a shared or user memory surface on a non-ex device.\n");
1963 return E_NOTIMPL;
1966 if (pool == D3DPOOL_SYSTEMMEM)
1967 user_mem = *shared_handle;
1968 else
1970 if (pool != D3DPOOL_DEFAULT)
1972 WARN("Trying to create a shared surface in pool %#x.\n", pool);
1973 return D3DERR_INVALIDCALL;
1975 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1979 usage = 0;
1980 if (pool == D3DPOOL_SCRATCH)
1981 usage |= WINED3DUSAGE_SCRATCH;
1982 access = wined3daccess_from_d3dpool(pool, usage)
1983 | WINED3D_RESOURCE_ACCESS_MAP_R | WINED3D_RESOURCE_ACCESS_MAP_W;
1985 return d3d9_device_create_surface(device, 0, wined3dformat_from_d3dformat(format),
1986 WINED3D_MULTISAMPLE_NONE, 0, usage, 0, access, width, height, user_mem, surface);
1989 static HRESULT WINAPI d3d9_device_SetRenderTarget(IDirect3DDevice9Ex *iface, DWORD idx, IDirect3DSurface9 *surface)
1991 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1992 struct d3d9_surface *surface_impl = unsafe_impl_from_IDirect3DSurface9(surface);
1993 struct wined3d_rendertarget_view *rtv;
1994 HRESULT hr;
1996 TRACE("iface %p, idx %u, surface %p.\n", iface, idx, surface);
1998 if (idx >= D3D_MAX_SIMULTANEOUS_RENDERTARGETS)
2000 WARN("Invalid index %u specified.\n", idx);
2001 return D3DERR_INVALIDCALL;
2004 if (!idx && !surface_impl)
2006 WARN("Trying to set render target 0 to NULL.\n");
2007 return D3DERR_INVALIDCALL;
2010 if (surface_impl && d3d9_surface_get_device(surface_impl) != device)
2012 WARN("Render target surface does not match device.\n");
2013 return D3DERR_INVALIDCALL;
2016 wined3d_mutex_lock();
2017 rtv = surface_impl ? d3d9_surface_acquire_rendertarget_view(surface_impl) : NULL;
2018 hr = wined3d_device_set_rendertarget_view(device->wined3d_device, idx, rtv, TRUE);
2019 d3d9_surface_release_rendertarget_view(surface_impl, rtv);
2020 if (SUCCEEDED(hr))
2022 if (!idx)
2023 device_reset_viewport_state(device);
2024 device->render_targets[idx] = surface_impl;
2026 wined3d_mutex_unlock();
2028 return hr;
2031 static HRESULT WINAPI d3d9_device_GetRenderTarget(IDirect3DDevice9Ex *iface, DWORD idx, IDirect3DSurface9 **surface)
2033 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2034 struct wined3d_rendertarget_view *wined3d_rtv;
2035 struct d3d9_surface *surface_impl;
2036 HRESULT hr = D3D_OK;
2038 TRACE("iface %p, idx %u, surface %p.\n", iface, idx, surface);
2040 if (!surface)
2041 return D3DERR_INVALIDCALL;
2043 if (idx >= D3D_MAX_SIMULTANEOUS_RENDERTARGETS)
2045 WARN("Invalid index %u specified.\n", idx);
2046 return D3DERR_INVALIDCALL;
2049 wined3d_mutex_lock();
2050 if ((wined3d_rtv = wined3d_device_get_rendertarget_view(device->wined3d_device, idx)))
2052 /* We want the sub resource parent here, since the view itself may be
2053 * internal to wined3d and may not have a parent. */
2054 surface_impl = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_rtv);
2055 *surface = &surface_impl->IDirect3DSurface9_iface;
2056 IDirect3DSurface9_AddRef(*surface);
2058 else
2060 hr = D3DERR_NOTFOUND;
2061 *surface = NULL;
2063 wined3d_mutex_unlock();
2065 return hr;
2068 static HRESULT WINAPI d3d9_device_SetDepthStencilSurface(IDirect3DDevice9Ex *iface, IDirect3DSurface9 *depth_stencil)
2070 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2071 struct d3d9_surface *ds_impl = unsafe_impl_from_IDirect3DSurface9(depth_stencil);
2072 struct wined3d_rendertarget_view *rtv;
2073 HRESULT hr;
2075 TRACE("iface %p, depth_stencil %p.\n", iface, depth_stencil);
2077 wined3d_mutex_lock();
2078 rtv = ds_impl ? d3d9_surface_acquire_rendertarget_view(ds_impl) : NULL;
2079 hr = wined3d_device_set_depth_stencil_view(device->wined3d_device, rtv);
2080 d3d9_surface_release_rendertarget_view(ds_impl, rtv);
2081 wined3d_mutex_unlock();
2083 return hr;
2086 static HRESULT WINAPI d3d9_device_GetDepthStencilSurface(IDirect3DDevice9Ex *iface, IDirect3DSurface9 **depth_stencil)
2088 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2089 struct wined3d_rendertarget_view *wined3d_dsv;
2090 struct d3d9_surface *surface_impl;
2091 HRESULT hr = D3D_OK;
2093 TRACE("iface %p, depth_stencil %p.\n", iface, depth_stencil);
2095 if (!depth_stencil)
2096 return D3DERR_INVALIDCALL;
2098 wined3d_mutex_lock();
2099 if ((wined3d_dsv = wined3d_device_get_depth_stencil_view(device->wined3d_device)))
2101 /* We want the sub resource parent here, since the view itself may be
2102 * internal to wined3d and may not have a parent. */
2103 surface_impl = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_dsv);
2104 *depth_stencil = &surface_impl->IDirect3DSurface9_iface;
2105 IDirect3DSurface9_AddRef(*depth_stencil);
2107 else
2109 hr = D3DERR_NOTFOUND;
2110 *depth_stencil = NULL;
2112 wined3d_mutex_unlock();
2114 return hr;
2117 static HRESULT WINAPI d3d9_device_BeginScene(IDirect3DDevice9Ex *iface)
2119 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2120 HRESULT hr;
2122 TRACE("iface %p.\n", iface);
2124 wined3d_mutex_lock();
2125 if (SUCCEEDED(hr = wined3d_device_begin_scene(device->wined3d_device)))
2126 device->in_scene = TRUE;
2127 wined3d_mutex_unlock();
2129 return hr;
2132 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_EndScene(IDirect3DDevice9Ex *iface)
2134 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2135 HRESULT hr;
2137 TRACE("iface %p.\n", iface);
2139 wined3d_mutex_lock();
2140 if (SUCCEEDED(hr = wined3d_device_end_scene(device->wined3d_device)))
2141 device->in_scene = FALSE;
2142 wined3d_mutex_unlock();
2144 return hr;
2147 static void d3d9_rts_flag_auto_gen_mipmap(struct d3d9_device *device)
2149 unsigned int i;
2151 for (i = 0; i < ARRAY_SIZE(device->render_targets); ++i)
2153 struct d3d9_surface *surface = device->render_targets[i];
2155 if (surface && surface->texture)
2156 d3d9_texture_flag_auto_gen_mipmap(surface->texture);
2160 static HRESULT WINAPI d3d9_device_Clear(IDirect3DDevice9Ex *iface, DWORD rect_count,
2161 const D3DRECT *rects, DWORD flags, D3DCOLOR color, float z, DWORD stencil)
2163 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2164 struct wined3d_color c;
2165 HRESULT hr;
2167 TRACE("iface %p, rect_count %u, rects %p, flags %#x, color 0x%08x, z %.8e, stencil %u.\n",
2168 iface, rect_count, rects, flags, color, z, stencil);
2170 if (rect_count && !rects)
2172 WARN("count %u with NULL rects.\n", rect_count);
2173 rect_count = 0;
2176 wined3d_color_from_d3dcolor(&c, color);
2177 wined3d_mutex_lock();
2178 wined3d_device_apply_stateblock(device->wined3d_device, device->state);
2179 hr = wined3d_device_clear(device->wined3d_device, rect_count, (const RECT *)rects, flags, &c, z, stencil);
2180 if (SUCCEEDED(hr))
2181 d3d9_rts_flag_auto_gen_mipmap(device);
2182 wined3d_mutex_unlock();
2184 return hr;
2187 static HRESULT WINAPI d3d9_device_SetTransform(IDirect3DDevice9Ex *iface,
2188 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
2190 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2192 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
2194 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
2195 wined3d_mutex_lock();
2196 wined3d_stateblock_set_transform(device->update_state,
2197 wined3d_transform_state_from_d3d(state), (const struct wined3d_matrix *)matrix);
2198 wined3d_mutex_unlock();
2200 return D3D_OK;
2203 static HRESULT WINAPI d3d9_device_GetTransform(IDirect3DDevice9Ex *iface,
2204 D3DTRANSFORMSTATETYPE state, D3DMATRIX *matrix)
2206 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2208 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
2210 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
2211 wined3d_mutex_lock();
2212 memcpy(matrix, &device->stateblock_state->transforms[state], sizeof(*matrix));
2213 wined3d_mutex_unlock();
2215 return D3D_OK;
2218 static HRESULT WINAPI d3d9_device_MultiplyTransform(IDirect3DDevice9Ex *iface,
2219 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
2221 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2223 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
2225 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
2226 wined3d_mutex_lock();
2227 wined3d_stateblock_multiply_transform(device->state,
2228 wined3d_transform_state_from_d3d(state), (const struct wined3d_matrix *)matrix);
2229 wined3d_mutex_unlock();
2231 return D3D_OK;
2234 static HRESULT WINAPI d3d9_device_SetViewport(IDirect3DDevice9Ex *iface, const D3DVIEWPORT9 *viewport)
2236 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2237 struct wined3d_viewport vp;
2239 TRACE("iface %p, viewport %p.\n", iface, viewport);
2241 vp.x = viewport->X;
2242 vp.y = viewport->Y;
2243 vp.width = viewport->Width;
2244 vp.height = viewport->Height;
2245 vp.min_z = viewport->MinZ;
2246 vp.max_z = viewport->MaxZ;
2248 wined3d_mutex_lock();
2249 wined3d_stateblock_set_viewport(device->update_state, &vp);
2250 wined3d_mutex_unlock();
2252 return D3D_OK;
2255 static HRESULT WINAPI d3d9_device_GetViewport(IDirect3DDevice9Ex *iface, D3DVIEWPORT9 *viewport)
2257 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2258 struct wined3d_viewport wined3d_viewport;
2260 TRACE("iface %p, viewport %p.\n", iface, viewport);
2262 wined3d_mutex_lock();
2263 wined3d_viewport = device->stateblock_state->viewport;
2264 wined3d_mutex_unlock();
2266 viewport->X = wined3d_viewport.x;
2267 viewport->Y = wined3d_viewport.y;
2268 viewport->Width = wined3d_viewport.width;
2269 viewport->Height = wined3d_viewport.height;
2270 viewport->MinZ = wined3d_viewport.min_z;
2271 viewport->MaxZ = wined3d_viewport.max_z;
2273 return D3D_OK;
2276 static HRESULT WINAPI d3d9_device_SetMaterial(IDirect3DDevice9Ex *iface, const D3DMATERIAL9 *material)
2278 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2280 TRACE("iface %p, material %p.\n", iface, material);
2282 /* Note: D3DMATERIAL9 is compatible with struct wined3d_material. */
2283 wined3d_mutex_lock();
2284 wined3d_stateblock_set_material(device->update_state, (const struct wined3d_material *)material);
2285 wined3d_mutex_unlock();
2287 return D3D_OK;
2290 static HRESULT WINAPI d3d9_device_GetMaterial(IDirect3DDevice9Ex *iface, D3DMATERIAL9 *material)
2292 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2294 TRACE("iface %p, material %p.\n", iface, material);
2296 /* Note: D3DMATERIAL9 is compatible with struct wined3d_material. */
2297 wined3d_mutex_lock();
2298 memcpy(material, &device->stateblock_state->material, sizeof(*material));
2299 wined3d_mutex_unlock();
2301 return D3D_OK;
2304 static HRESULT WINAPI d3d9_device_SetLight(IDirect3DDevice9Ex *iface, DWORD index, const D3DLIGHT9 *light)
2306 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2307 HRESULT hr;
2309 TRACE("iface %p, index %u, light %p.\n", iface, index, light);
2311 /* Note: D3DLIGHT9 is compatible with struct wined3d_light. */
2312 wined3d_mutex_lock();
2313 hr = wined3d_stateblock_set_light(device->update_state, index, (const struct wined3d_light *)light);
2314 wined3d_mutex_unlock();
2316 return hr;
2319 static HRESULT WINAPI d3d9_device_GetLight(IDirect3DDevice9Ex *iface, DWORD index, D3DLIGHT9 *light)
2321 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2322 BOOL enabled;
2323 HRESULT hr;
2325 TRACE("iface %p, index %u, light %p.\n", iface, index, light);
2327 /* Note: D3DLIGHT9 is compatible with struct wined3d_light. */
2328 wined3d_mutex_lock();
2329 hr = wined3d_stateblock_get_light(device->state, index, (struct wined3d_light *)light, &enabled);
2330 wined3d_mutex_unlock();
2332 return hr;
2335 static HRESULT WINAPI d3d9_device_LightEnable(IDirect3DDevice9Ex *iface, DWORD index, BOOL enable)
2337 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2338 HRESULT hr;
2340 TRACE("iface %p, index %u, enable %#x.\n", iface, index, enable);
2342 wined3d_mutex_lock();
2343 hr = wined3d_stateblock_set_light_enable(device->update_state, index, enable);
2344 wined3d_mutex_unlock();
2346 return hr;
2349 static HRESULT WINAPI d3d9_device_GetLightEnable(IDirect3DDevice9Ex *iface, DWORD index, BOOL *enabled)
2351 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2352 struct wined3d_light light;
2353 HRESULT hr;
2355 TRACE("iface %p, index %u, enabled %p.\n", iface, index, enabled);
2357 wined3d_mutex_lock();
2358 hr = wined3d_stateblock_get_light(device->state, index, &light, enabled);
2359 wined3d_mutex_unlock();
2361 return hr;
2364 static HRESULT WINAPI d3d9_device_SetClipPlane(IDirect3DDevice9Ex *iface, DWORD index, const float *plane)
2366 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2367 HRESULT hr;
2369 TRACE("iface %p, index %u, plane %p.\n", iface, index, plane);
2371 index = min(index, device->max_user_clip_planes - 1);
2373 wined3d_mutex_lock();
2374 hr = wined3d_stateblock_set_clip_plane(device->update_state, index, (const struct wined3d_vec4 *)plane);
2375 wined3d_mutex_unlock();
2377 return hr;
2380 static HRESULT WINAPI d3d9_device_GetClipPlane(IDirect3DDevice9Ex *iface, DWORD index, float *plane)
2382 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2384 TRACE("iface %p, index %u, plane %p.\n", iface, index, plane);
2386 index = min(index, device->max_user_clip_planes - 1);
2388 wined3d_mutex_lock();
2389 memcpy(plane, &device->stateblock_state->clip_planes[index], sizeof(struct wined3d_vec4));
2390 wined3d_mutex_unlock();
2392 return D3D_OK;
2395 static void resolve_depth_buffer(struct d3d9_device *device)
2397 const struct wined3d_stateblock_state *state = device->stateblock_state;
2398 struct wined3d_rendertarget_view *wined3d_dsv;
2399 struct wined3d_resource *dst_resource;
2400 struct wined3d_texture *dst_texture;
2401 struct wined3d_resource_desc desc;
2402 struct d3d9_surface *d3d9_dsv;
2404 if (!(dst_texture = state->textures[0]))
2405 return;
2406 dst_resource = wined3d_texture_get_resource(dst_texture);
2407 wined3d_resource_get_desc(dst_resource, &desc);
2408 if (desc.format != WINED3DFMT_D24_UNORM_S8_UINT
2409 && desc.format != WINED3DFMT_X8D24_UNORM
2410 && desc.format != WINED3DFMT_DF16
2411 && desc.format != WINED3DFMT_DF24
2412 && desc.format != WINED3DFMT_INTZ)
2413 return;
2415 if (!(wined3d_dsv = wined3d_device_get_depth_stencil_view(device->wined3d_device)))
2416 return;
2417 d3d9_dsv = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_dsv);
2419 wined3d_device_resolve_sub_resource(device->wined3d_device, dst_resource, 0,
2420 wined3d_rendertarget_view_get_resource(wined3d_dsv), d3d9_dsv->sub_resource_idx, desc.format);
2423 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_SetRenderState(IDirect3DDevice9Ex *iface,
2424 D3DRENDERSTATETYPE state, DWORD value)
2426 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2428 TRACE("iface %p, state %#x, value %#x.\n", iface, state, value);
2430 wined3d_mutex_lock();
2431 wined3d_stateblock_set_render_state(device->update_state, wined3d_render_state_from_d3d(state), value);
2432 if (state == D3DRS_POINTSIZE && value == D3D9_RESZ_CODE)
2433 resolve_depth_buffer(device);
2434 wined3d_mutex_unlock();
2436 return D3D_OK;
2439 static HRESULT WINAPI d3d9_device_GetRenderState(IDirect3DDevice9Ex *iface,
2440 D3DRENDERSTATETYPE state, DWORD *value)
2442 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2443 const struct wined3d_stateblock_state *device_state;
2445 TRACE("iface %p, state %#x, value %p.\n", iface, state, value);
2447 wined3d_mutex_lock();
2448 device_state = device->stateblock_state;
2449 *value = device_state->rs[state];
2450 wined3d_mutex_unlock();
2452 return D3D_OK;
2455 static HRESULT WINAPI d3d9_device_CreateStateBlock(IDirect3DDevice9Ex *iface,
2456 D3DSTATEBLOCKTYPE type, IDirect3DStateBlock9 **stateblock)
2458 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2459 struct d3d9_stateblock *object;
2460 HRESULT hr;
2462 TRACE("iface %p, type %#x, stateblock %p.\n", iface, type, stateblock);
2464 if (type != D3DSBT_ALL && type != D3DSBT_PIXELSTATE && type != D3DSBT_VERTEXSTATE)
2466 WARN("Unexpected stateblock type, returning D3DERR_INVALIDCALL.\n");
2467 return D3DERR_INVALIDCALL;
2470 wined3d_mutex_lock();
2471 if (device->recording)
2473 wined3d_mutex_unlock();
2474 WARN("Trying to create a stateblock while recording, returning D3DERR_INVALIDCALL.\n");
2475 return D3DERR_INVALIDCALL;
2477 wined3d_mutex_unlock();
2479 if (!(object = heap_alloc_zero(sizeof(*object))))
2480 return E_OUTOFMEMORY;
2482 hr = stateblock_init(object, device, type, NULL);
2483 if (FAILED(hr))
2485 WARN("Failed to initialize stateblock, hr %#x.\n", hr);
2486 heap_free(object);
2487 return hr;
2490 TRACE("Created stateblock %p.\n", object);
2491 *stateblock = &object->IDirect3DStateBlock9_iface;
2493 return D3D_OK;
2496 static HRESULT WINAPI d3d9_device_BeginStateBlock(IDirect3DDevice9Ex *iface)
2498 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2499 struct wined3d_stateblock *stateblock;
2500 HRESULT hr;
2502 TRACE("iface %p.\n", iface);
2504 wined3d_mutex_lock();
2505 if (device->recording)
2507 wined3d_mutex_unlock();
2508 WARN("Trying to begin a stateblock while recording, returning D3DERR_INVALIDCALL.\n");
2509 return D3DERR_INVALIDCALL;
2512 if (SUCCEEDED(hr = wined3d_stateblock_create(device->wined3d_device, device->state, WINED3D_SBT_RECORDED, &stateblock)))
2513 device->update_state = device->recording = stateblock;
2514 wined3d_mutex_unlock();
2516 return hr;
2519 static HRESULT WINAPI d3d9_device_EndStateBlock(IDirect3DDevice9Ex *iface, IDirect3DStateBlock9 **stateblock)
2521 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2522 struct wined3d_stateblock *wined3d_stateblock;
2523 struct d3d9_stateblock *object;
2524 HRESULT hr;
2526 TRACE("iface %p, stateblock %p.\n", iface, stateblock);
2528 wined3d_mutex_lock();
2529 if (!device->recording)
2531 wined3d_mutex_unlock();
2532 WARN("Trying to end a stateblock, but no stateblock is being recorded.\n");
2533 return D3DERR_INVALIDCALL;
2535 wined3d_stateblock = device->recording;
2536 wined3d_stateblock_init_contained_states(wined3d_stateblock);
2537 device->recording = NULL;
2538 device->update_state = device->state;
2539 wined3d_mutex_unlock();
2541 if (!(object = heap_alloc_zero(sizeof(*object))))
2543 wined3d_mutex_lock();
2544 wined3d_stateblock_decref(wined3d_stateblock);
2545 wined3d_mutex_unlock();
2546 return E_OUTOFMEMORY;
2549 hr = stateblock_init(object, device, 0, wined3d_stateblock);
2550 if (FAILED(hr))
2552 WARN("Failed to initialize stateblock, hr %#x.\n", hr);
2553 wined3d_mutex_lock();
2554 wined3d_stateblock_decref(wined3d_stateblock);
2555 wined3d_mutex_unlock();
2556 heap_free(object);
2557 return hr;
2560 TRACE("Created stateblock %p.\n", object);
2561 *stateblock = &object->IDirect3DStateBlock9_iface;
2563 return D3D_OK;
2566 static HRESULT WINAPI d3d9_device_SetClipStatus(IDirect3DDevice9Ex *iface, const D3DCLIPSTATUS9 *clip_status)
2568 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2569 HRESULT hr;
2571 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
2573 wined3d_mutex_lock();
2574 hr = wined3d_device_set_clip_status(device->wined3d_device, (const struct wined3d_clip_status *)clip_status);
2575 wined3d_mutex_unlock();
2577 return hr;
2580 static HRESULT WINAPI d3d9_device_GetClipStatus(IDirect3DDevice9Ex *iface, D3DCLIPSTATUS9 *clip_status)
2582 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2583 HRESULT hr;
2585 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
2587 wined3d_mutex_lock();
2588 hr = wined3d_device_get_clip_status(device->wined3d_device, (struct wined3d_clip_status *)clip_status);
2589 wined3d_mutex_unlock();
2591 return hr;
2594 static HRESULT WINAPI d3d9_device_GetTexture(IDirect3DDevice9Ex *iface, DWORD stage, IDirect3DBaseTexture9 **texture)
2596 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2597 struct wined3d_texture *wined3d_texture = NULL;
2598 const struct wined3d_stateblock_state *state;
2599 struct d3d9_texture *texture_impl;
2601 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
2603 if (!texture)
2604 return D3DERR_INVALIDCALL;
2606 if (stage >= D3DVERTEXTEXTURESAMPLER0 && stage <= D3DVERTEXTEXTURESAMPLER3)
2607 stage -= D3DVERTEXTEXTURESAMPLER0 - WINED3D_VERTEX_SAMPLER_OFFSET;
2609 if (stage >= ARRAY_SIZE(state->textures))
2611 WARN("Ignoring invalid stage %u.\n", stage);
2612 *texture = NULL;
2613 return D3D_OK;
2616 wined3d_mutex_lock();
2617 state = device->stateblock_state;
2618 if ((wined3d_texture = state->textures[stage]))
2620 texture_impl = wined3d_texture_get_parent(wined3d_texture);
2621 *texture = &texture_impl->IDirect3DBaseTexture9_iface;
2622 IDirect3DBaseTexture9_AddRef(*texture);
2624 else
2626 *texture = NULL;
2628 wined3d_mutex_unlock();
2630 return D3D_OK;
2633 static HRESULT WINAPI d3d9_device_SetTexture(IDirect3DDevice9Ex *iface, DWORD stage, IDirect3DBaseTexture9 *texture)
2635 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2636 struct d3d9_texture *texture_impl;
2638 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
2640 texture_impl = unsafe_impl_from_IDirect3DBaseTexture9(texture);
2642 if (stage >= D3DVERTEXTEXTURESAMPLER0 && stage <= D3DVERTEXTEXTURESAMPLER3)
2643 stage -= D3DVERTEXTEXTURESAMPLER0 - WINED3D_VERTEX_SAMPLER_OFFSET;
2645 wined3d_mutex_lock();
2646 wined3d_stateblock_set_texture(device->update_state, stage,
2647 texture_impl ? texture_impl->wined3d_texture : NULL);
2648 if (!device->recording)
2650 if (stage < D3D9_MAX_TEXTURE_UNITS)
2652 if (texture_impl && texture_impl->usage & D3DUSAGE_AUTOGENMIPMAP)
2653 device->auto_mipmaps |= 1u << stage;
2654 else
2655 device->auto_mipmaps &= ~(1u << stage);
2658 wined3d_mutex_unlock();
2660 return D3D_OK;
2663 static const enum wined3d_texture_stage_state tss_lookup[] =
2665 WINED3D_TSS_INVALID, /* 0, unused */
2666 WINED3D_TSS_COLOR_OP, /* 1, D3DTSS_COLOROP */
2667 WINED3D_TSS_COLOR_ARG1, /* 2, D3DTSS_COLORARG1 */
2668 WINED3D_TSS_COLOR_ARG2, /* 3, D3DTSS_COLORARG2 */
2669 WINED3D_TSS_ALPHA_OP, /* 4, D3DTSS_ALPHAOP */
2670 WINED3D_TSS_ALPHA_ARG1, /* 5, D3DTSS_ALPHAARG1 */
2671 WINED3D_TSS_ALPHA_ARG2, /* 6, D3DTSS_ALPHAARG2 */
2672 WINED3D_TSS_BUMPENV_MAT00, /* 7, D3DTSS_BUMPENVMAT00 */
2673 WINED3D_TSS_BUMPENV_MAT01, /* 8, D3DTSS_BUMPENVMAT01 */
2674 WINED3D_TSS_BUMPENV_MAT10, /* 9, D3DTSS_BUMPENVMAT10 */
2675 WINED3D_TSS_BUMPENV_MAT11, /* 10, D3DTSS_BUMPENVMAT11 */
2676 WINED3D_TSS_TEXCOORD_INDEX, /* 11, D3DTSS_TEXCOORDINDEX */
2677 WINED3D_TSS_INVALID, /* 12, unused */
2678 WINED3D_TSS_INVALID, /* 13, unused */
2679 WINED3D_TSS_INVALID, /* 14, unused */
2680 WINED3D_TSS_INVALID, /* 15, unused */
2681 WINED3D_TSS_INVALID, /* 16, unused */
2682 WINED3D_TSS_INVALID, /* 17, unused */
2683 WINED3D_TSS_INVALID, /* 18, unused */
2684 WINED3D_TSS_INVALID, /* 19, unused */
2685 WINED3D_TSS_INVALID, /* 20, unused */
2686 WINED3D_TSS_INVALID, /* 21, unused */
2687 WINED3D_TSS_BUMPENV_LSCALE, /* 22, D3DTSS_BUMPENVLSCALE */
2688 WINED3D_TSS_BUMPENV_LOFFSET, /* 23, D3DTSS_BUMPENVLOFFSET */
2689 WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS, /* 24, D3DTSS_TEXTURETRANSFORMFLAGS */
2690 WINED3D_TSS_INVALID, /* 25, unused */
2691 WINED3D_TSS_COLOR_ARG0, /* 26, D3DTSS_COLORARG0 */
2692 WINED3D_TSS_ALPHA_ARG0, /* 27, D3DTSS_ALPHAARG0 */
2693 WINED3D_TSS_RESULT_ARG, /* 28, D3DTSS_RESULTARG */
2694 WINED3D_TSS_INVALID, /* 29, unused */
2695 WINED3D_TSS_INVALID, /* 30, unused */
2696 WINED3D_TSS_INVALID, /* 31, unused */
2697 WINED3D_TSS_CONSTANT, /* 32, D3DTSS_CONSTANT */
2700 static HRESULT WINAPI d3d9_device_GetTextureStageState(IDirect3DDevice9Ex *iface,
2701 DWORD stage, D3DTEXTURESTAGESTATETYPE state, DWORD *value)
2703 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2705 TRACE("iface %p, stage %u, state %#x, value %p.\n", iface, stage, state, value);
2707 if (state >= ARRAY_SIZE(tss_lookup) || tss_lookup[state] == WINED3D_TSS_INVALID)
2709 WARN("Invalid state %#x passed.\n", state);
2710 return D3D_OK;
2713 wined3d_mutex_lock();
2714 *value = device->stateblock_state->texture_states[stage][tss_lookup[state]];
2715 wined3d_mutex_unlock();
2717 return D3D_OK;
2720 static HRESULT WINAPI d3d9_device_SetTextureStageState(IDirect3DDevice9Ex *iface,
2721 DWORD stage, D3DTEXTURESTAGESTATETYPE state, DWORD value)
2723 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2725 TRACE("iface %p, stage %u, state %#x, value %#x.\n", iface, stage, state, value);
2727 if (state >= ARRAY_SIZE(tss_lookup))
2729 WARN("Invalid state %#x passed.\n", state);
2730 return D3D_OK;
2733 wined3d_mutex_lock();
2734 wined3d_stateblock_set_texture_stage_state(device->update_state, stage, tss_lookup[state], value);
2735 wined3d_mutex_unlock();
2737 return D3D_OK;
2740 static HRESULT WINAPI d3d9_device_GetSamplerState(IDirect3DDevice9Ex *iface,
2741 DWORD sampler_idx, D3DSAMPLERSTATETYPE state, DWORD *value)
2743 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2744 const struct wined3d_stateblock_state *device_state;
2746 TRACE("iface %p, sampler_idx %u, state %#x, value %p.\n", iface, sampler_idx, state, value);
2748 if (sampler_idx >= D3DVERTEXTEXTURESAMPLER0 && sampler_idx <= D3DVERTEXTEXTURESAMPLER3)
2749 sampler_idx -= D3DVERTEXTEXTURESAMPLER0 - WINED3D_VERTEX_SAMPLER_OFFSET;
2751 if (sampler_idx >= ARRAY_SIZE(device_state->sampler_states))
2753 WARN("Invalid sampler %u.\n", sampler_idx);
2754 *value = 0;
2755 return D3D_OK;
2758 wined3d_mutex_lock();
2759 device_state = device->stateblock_state;
2760 *value = device_state->sampler_states[sampler_idx][state];
2761 wined3d_mutex_unlock();
2763 return D3D_OK;
2766 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_SetSamplerState(IDirect3DDevice9Ex *iface,
2767 DWORD sampler, D3DSAMPLERSTATETYPE state, DWORD value)
2769 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2771 TRACE("iface %p, sampler %u, state %#x, value %#x.\n", iface, sampler, state, value);
2773 if (sampler >= D3DVERTEXTEXTURESAMPLER0 && sampler <= D3DVERTEXTEXTURESAMPLER3)
2774 sampler -= D3DVERTEXTEXTURESAMPLER0 - WINED3D_VERTEX_SAMPLER_OFFSET;
2776 wined3d_mutex_lock();
2777 wined3d_stateblock_set_sampler_state(device->update_state, sampler, wined3d_sampler_state_from_d3d(state), value);
2778 wined3d_mutex_unlock();
2780 return D3D_OK;
2783 static HRESULT WINAPI d3d9_device_ValidateDevice(IDirect3DDevice9Ex *iface, DWORD *pass_count)
2785 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2786 HRESULT hr;
2788 TRACE("iface %p, pass_count %p.\n", iface, pass_count);
2790 wined3d_mutex_lock();
2791 wined3d_device_apply_stateblock(device->wined3d_device, device->state);
2792 hr = wined3d_device_validate_device(device->wined3d_device, pass_count);
2793 wined3d_mutex_unlock();
2795 return hr;
2798 static HRESULT WINAPI d3d9_device_SetPaletteEntries(IDirect3DDevice9Ex *iface,
2799 UINT palette_idx, const PALETTEENTRY *entries)
2801 WARN("iface %p, palette_idx %u, entries %p unimplemented.\n", iface, palette_idx, entries);
2803 /* The d3d9 palette API is non-functional on Windows. Getters and setters are implemented,
2804 * and some drivers allow the creation of P8 surfaces. These surfaces can be copied to
2805 * other P8 surfaces with StretchRect, but cannot be converted to (A)RGB.
2807 * Some older(dx7) cards may have support for P8 textures, but games cannot rely on this. */
2808 return D3D_OK;
2811 static HRESULT WINAPI d3d9_device_GetPaletteEntries(IDirect3DDevice9Ex *iface,
2812 UINT palette_idx, PALETTEENTRY *entries)
2814 FIXME("iface %p, palette_idx %u, entries %p unimplemented.\n", iface, palette_idx, entries);
2816 return D3DERR_INVALIDCALL;
2819 static HRESULT WINAPI d3d9_device_SetCurrentTexturePalette(IDirect3DDevice9Ex *iface, UINT palette_idx)
2821 WARN("iface %p, palette_idx %u unimplemented.\n", iface, palette_idx);
2823 return D3D_OK;
2826 static HRESULT WINAPI d3d9_device_GetCurrentTexturePalette(IDirect3DDevice9Ex *iface, UINT *palette_idx)
2828 FIXME("iface %p, palette_idx %p.\n", iface, palette_idx);
2830 return D3DERR_INVALIDCALL;
2833 static HRESULT WINAPI d3d9_device_SetScissorRect(IDirect3DDevice9Ex *iface, const RECT *rect)
2835 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2837 TRACE("iface %p, rect %p.\n", iface, rect);
2839 wined3d_mutex_lock();
2840 wined3d_stateblock_set_scissor_rect(device->update_state, rect);
2841 wined3d_mutex_unlock();
2843 return D3D_OK;
2846 static HRESULT WINAPI d3d9_device_GetScissorRect(IDirect3DDevice9Ex *iface, RECT *rect)
2848 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2850 TRACE("iface %p, rect %p.\n", iface, rect);
2852 wined3d_mutex_lock();
2853 *rect = device->stateblock_state->scissor_rect;
2854 wined3d_mutex_unlock();
2856 return D3D_OK;
2859 static HRESULT WINAPI d3d9_device_SetSoftwareVertexProcessing(IDirect3DDevice9Ex *iface, BOOL software)
2861 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2863 TRACE("iface %p, software %#x.\n", iface, software);
2865 wined3d_mutex_lock();
2866 wined3d_device_set_software_vertex_processing(device->wined3d_device, software);
2867 wined3d_mutex_unlock();
2869 return D3D_OK;
2872 static BOOL WINAPI d3d9_device_GetSoftwareVertexProcessing(IDirect3DDevice9Ex *iface)
2874 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2875 BOOL ret;
2877 TRACE("iface %p.\n", iface);
2879 wined3d_mutex_lock();
2880 ret = wined3d_device_get_software_vertex_processing(device->wined3d_device);
2881 wined3d_mutex_unlock();
2883 return ret;
2886 static HRESULT WINAPI d3d9_device_SetNPatchMode(IDirect3DDevice9Ex *iface, float segment_count)
2888 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2889 HRESULT hr;
2891 TRACE("iface %p, segment_count %.8e.\n", iface, segment_count);
2893 wined3d_mutex_lock();
2894 hr = wined3d_device_set_npatch_mode(device->wined3d_device, segment_count);
2895 wined3d_mutex_unlock();
2897 return hr;
2900 static float WINAPI d3d9_device_GetNPatchMode(IDirect3DDevice9Ex *iface)
2902 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2903 float ret;
2905 TRACE("iface %p.\n", iface);
2907 wined3d_mutex_lock();
2908 ret = wined3d_device_get_npatch_mode(device->wined3d_device);
2909 wined3d_mutex_unlock();
2911 return ret;
2914 /* wined3d critical section must be taken by the caller. */
2915 static void d3d9_generate_auto_mipmaps(struct d3d9_device *device)
2917 const struct wined3d_stateblock_state *state = device->stateblock_state;
2918 struct wined3d_texture *texture;
2919 unsigned int i, map;
2921 map = device->auto_mipmaps;
2922 while (map)
2924 i = wined3d_bit_scan(&map);
2925 if ((texture = state->textures[i]))
2926 d3d9_texture_gen_auto_mipmap(wined3d_texture_get_parent(texture));
2930 static void d3d9_device_upload_sysmem_vertex_buffers(struct d3d9_device *device,
2931 int base_vertex, unsigned int start_vertex, unsigned int vertex_count)
2933 const struct wined3d_stateblock_state *state = device->stateblock_state;
2934 struct wined3d_vertex_declaration *wined3d_decl;
2935 struct wined3d_box box = {0, 0, 0, 1, 0, 1};
2936 const struct wined3d_stream_state *stream;
2937 struct d3d9_vertexbuffer *d3d9_buffer;
2938 struct wined3d_resource *dst_resource;
2939 struct d3d9_vertex_declaration *decl;
2940 struct wined3d_buffer *dst_buffer;
2941 struct wined3d_resource_desc desc;
2942 unsigned int stride, map;
2943 HRESULT hr;
2945 if (!device->sysmem_vb)
2946 return;
2947 wined3d_decl = state->vertex_declaration;
2948 if (!wined3d_decl)
2949 return;
2951 if (base_vertex >= 0 || start_vertex >= -base_vertex)
2952 start_vertex += base_vertex;
2953 else
2954 FIXME("System memory vertex data offset is negative.\n");
2956 decl = wined3d_vertex_declaration_get_parent(wined3d_decl);
2957 map = decl->stream_map & device->sysmem_vb;
2958 while (map)
2960 stream = &state->streams[wined3d_bit_scan(&map)];
2961 dst_buffer = stream->buffer;
2962 stride = stream->stride;
2964 d3d9_buffer = wined3d_buffer_get_parent(dst_buffer);
2965 dst_resource = wined3d_buffer_get_resource(dst_buffer);
2966 wined3d_resource_get_desc(dst_resource, &desc);
2967 box.left = stream->offset + start_vertex * stride;
2968 box.right = min(box.left + vertex_count * stride, desc.size);
2969 if (FAILED(hr = wined3d_device_copy_sub_resource_region(device->wined3d_device,
2970 dst_resource, 0, box.left, 0, 0,
2971 wined3d_buffer_get_resource(d3d9_buffer->wined3d_buffer), 0, &box, 0)))
2972 ERR("Failed to update buffer.\n");
2976 static void d3d9_device_upload_sysmem_index_buffer(struct d3d9_device *device,
2977 unsigned int start_idx, unsigned int idx_count)
2979 const struct wined3d_stateblock_state *state = device->stateblock_state;
2980 struct wined3d_box box = {0, 0, 0, 1, 0, 1};
2981 struct wined3d_resource *dst_resource;
2982 struct d3d9_indexbuffer *d3d9_buffer;
2983 struct wined3d_buffer *dst_buffer;
2984 struct wined3d_resource_desc desc;
2985 enum wined3d_format_id format;
2986 unsigned int idx_size;
2987 HRESULT hr;
2989 if (!device->sysmem_ib)
2990 return;
2992 dst_buffer = state->index_buffer;
2993 format = state->index_format;
2994 d3d9_buffer = wined3d_buffer_get_parent(dst_buffer);
2995 dst_resource = wined3d_buffer_get_resource(dst_buffer);
2996 wined3d_resource_get_desc(dst_resource, &desc);
2997 idx_size = format == WINED3DFMT_R16_UINT ? 2 : 4;
2998 box.left = start_idx * idx_size;
2999 box.right = min(box.left + idx_count * idx_size, desc.size);
3000 if (FAILED(hr = wined3d_device_copy_sub_resource_region(device->wined3d_device,
3001 dst_resource, 0, box.left, 0, 0,
3002 wined3d_buffer_get_resource(d3d9_buffer->wined3d_buffer), 0, &box, 0)))
3003 ERR("Failed to update buffer.\n");
3006 static HRESULT WINAPI d3d9_device_DrawPrimitive(IDirect3DDevice9Ex *iface,
3007 D3DPRIMITIVETYPE primitive_type, UINT start_vertex, UINT primitive_count)
3009 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3010 unsigned int vertex_count;
3011 HRESULT hr;
3013 TRACE("iface %p, primitive_type %#x, start_vertex %u, primitive_count %u.\n",
3014 iface, primitive_type, start_vertex, primitive_count);
3016 wined3d_mutex_lock();
3017 if (!device->has_vertex_declaration)
3019 wined3d_mutex_unlock();
3020 WARN("Called without a valid vertex declaration set.\n");
3021 return D3DERR_INVALIDCALL;
3023 wined3d_device_apply_stateblock(device->wined3d_device, device->state);
3024 vertex_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
3025 d3d9_device_upload_sysmem_vertex_buffers(device, 0, start_vertex, vertex_count);
3026 d3d9_generate_auto_mipmaps(device);
3027 wined3d_device_set_primitive_type(device->wined3d_device, wined3d_primitive_type_from_d3d(primitive_type), 0);
3028 hr = wined3d_device_draw_primitive(device->wined3d_device, start_vertex, vertex_count);
3029 if (SUCCEEDED(hr))
3030 d3d9_rts_flag_auto_gen_mipmap(device);
3031 wined3d_mutex_unlock();
3033 return hr;
3036 static HRESULT WINAPI d3d9_device_DrawIndexedPrimitive(IDirect3DDevice9Ex *iface,
3037 D3DPRIMITIVETYPE primitive_type, INT base_vertex_idx, UINT min_vertex_idx,
3038 UINT vertex_count, UINT start_idx, UINT primitive_count)
3040 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3041 unsigned int index_count;
3042 HRESULT hr;
3044 TRACE("iface %p, primitive_type %#x, base_vertex_idx %u, min_vertex_idx %u, "
3045 "vertex_count %u, start_idx %u, primitive_count %u.\n",
3046 iface, primitive_type, base_vertex_idx, min_vertex_idx,
3047 vertex_count, start_idx, primitive_count);
3049 wined3d_mutex_lock();
3050 if (!device->has_vertex_declaration)
3052 wined3d_mutex_unlock();
3053 WARN("Called without a valid vertex declaration set.\n");
3054 return D3DERR_INVALIDCALL;
3056 index_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
3057 d3d9_device_upload_sysmem_vertex_buffers(device, base_vertex_idx, min_vertex_idx, vertex_count);
3058 d3d9_device_upload_sysmem_index_buffer(device, start_idx, index_count);
3059 d3d9_generate_auto_mipmaps(device);
3060 wined3d_stateblock_set_base_vertex_index(device->state, base_vertex_idx);
3061 wined3d_device_set_primitive_type(device->wined3d_device, wined3d_primitive_type_from_d3d(primitive_type), 0);
3062 wined3d_device_apply_stateblock(device->wined3d_device, device->state);
3063 hr = wined3d_device_draw_indexed_primitive(device->wined3d_device, start_idx, index_count);
3064 if (SUCCEEDED(hr))
3065 d3d9_rts_flag_auto_gen_mipmap(device);
3066 wined3d_mutex_unlock();
3068 return hr;
3071 /* The caller is responsible for wined3d locking */
3072 static HRESULT d3d9_device_prepare_vertex_buffer(struct d3d9_device *device, UINT min_size)
3074 HRESULT hr;
3076 if (device->vertex_buffer_size < min_size || !device->vertex_buffer)
3078 UINT size = max(device->vertex_buffer_size * 2, min_size);
3079 struct wined3d_buffer_desc desc;
3080 struct wined3d_buffer *buffer;
3082 TRACE("Growing vertex buffer to %u bytes.\n", size);
3084 desc.byte_width = size;
3085 desc.usage = WINED3DUSAGE_DYNAMIC;
3086 desc.bind_flags = WINED3D_BIND_VERTEX_BUFFER;
3087 desc.access = WINED3D_RESOURCE_ACCESS_GPU | WINED3D_RESOURCE_ACCESS_MAP_W;
3088 desc.misc_flags = 0;
3089 desc.structure_byte_stride = 0;
3091 if (FAILED(hr = wined3d_buffer_create(device->wined3d_device, &desc,
3092 NULL, NULL, &d3d9_null_wined3d_parent_ops, &buffer)))
3094 ERR("Failed to create vertex buffer, hr %#x.\n", hr);
3095 return hr;
3098 if (device->vertex_buffer)
3099 wined3d_buffer_decref(device->vertex_buffer);
3101 device->vertex_buffer = buffer;
3102 device->vertex_buffer_size = size;
3103 device->vertex_buffer_pos = 0;
3105 return D3D_OK;
3108 static HRESULT WINAPI d3d9_device_DrawPrimitiveUP(IDirect3DDevice9Ex *iface,
3109 D3DPRIMITIVETYPE primitive_type, UINT primitive_count, const void *data, UINT stride)
3111 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3112 HRESULT hr;
3113 UINT vtx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
3114 struct wined3d_map_desc wined3d_map_desc;
3115 struct wined3d_box wined3d_box = {0};
3116 UINT size = vtx_count * stride;
3117 struct wined3d_resource *vb;
3118 UINT vb_pos, align;
3120 TRACE("iface %p, primitive_type %#x, primitive_count %u, data %p, stride %u.\n",
3121 iface, primitive_type, primitive_count, data, stride);
3123 if (!stride)
3125 WARN("stride is 0, returning D3DERR_INVALIDCALL.\n");
3126 return D3DERR_INVALIDCALL;
3128 if (!primitive_count)
3130 WARN("primitive_count is 0, returning D3D_OK.\n");
3131 return D3D_OK;
3134 wined3d_mutex_lock();
3136 if (!device->has_vertex_declaration)
3138 wined3d_mutex_unlock();
3139 WARN("Called without a valid vertex declaration set.\n");
3140 return D3DERR_INVALIDCALL;
3143 hr = d3d9_device_prepare_vertex_buffer(device, size);
3144 if (FAILED(hr))
3145 goto done;
3147 vb_pos = device->vertex_buffer_pos;
3148 align = vb_pos % stride;
3149 if (align) align = stride - align;
3150 if (vb_pos + size + align > device->vertex_buffer_size)
3151 vb_pos = 0;
3152 else
3153 vb_pos += align;
3155 wined3d_box.left = vb_pos;
3156 wined3d_box.right = vb_pos + size;
3157 vb = wined3d_buffer_get_resource(device->vertex_buffer);
3158 if (FAILED(hr = wined3d_resource_map(vb, 0, &wined3d_map_desc, &wined3d_box,
3159 WINED3D_MAP_WRITE | (vb_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD))))
3160 goto done;
3161 memcpy(wined3d_map_desc.data, data, size);
3162 wined3d_resource_unmap(vb, 0);
3163 device->vertex_buffer_pos = vb_pos + size;
3165 hr = wined3d_stateblock_set_stream_source(device->state, 0, device->vertex_buffer, 0, stride);
3166 if (FAILED(hr))
3167 goto done;
3169 d3d9_generate_auto_mipmaps(device);
3170 wined3d_device_set_primitive_type(device->wined3d_device, wined3d_primitive_type_from_d3d(primitive_type), 0);
3171 wined3d_device_apply_stateblock(device->wined3d_device, device->state);
3172 hr = wined3d_device_draw_primitive(device->wined3d_device, vb_pos / stride, vtx_count);
3173 wined3d_stateblock_set_stream_source(device->state, 0, NULL, 0, 0);
3174 if (SUCCEEDED(hr))
3175 d3d9_rts_flag_auto_gen_mipmap(device);
3177 done:
3178 wined3d_mutex_unlock();
3179 return hr;
3182 /* The caller is responsible for wined3d locking */
3183 static HRESULT d3d9_device_prepare_index_buffer(struct d3d9_device *device, UINT min_size)
3185 HRESULT hr;
3187 if (device->index_buffer_size < min_size || !device->index_buffer)
3189 UINT size = max(device->index_buffer_size * 2, min_size);
3190 struct wined3d_buffer_desc desc;
3191 struct wined3d_buffer *buffer;
3193 TRACE("Growing index buffer to %u bytes.\n", size);
3195 desc.byte_width = size;
3196 desc.usage = WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_STATICDECL;
3197 desc.bind_flags = WINED3D_BIND_INDEX_BUFFER;
3198 desc.access = WINED3D_RESOURCE_ACCESS_GPU | WINED3D_RESOURCE_ACCESS_MAP_W;
3199 desc.misc_flags = 0;
3200 desc.structure_byte_stride = 0;
3202 if (FAILED(hr = wined3d_buffer_create(device->wined3d_device, &desc,
3203 NULL, NULL, &d3d9_null_wined3d_parent_ops, &buffer)))
3205 ERR("Failed to create index buffer, hr %#x.\n", hr);
3206 return hr;
3209 if (device->index_buffer)
3210 wined3d_buffer_decref(device->index_buffer);
3212 device->index_buffer = buffer;
3213 device->index_buffer_size = size;
3214 device->index_buffer_pos = 0;
3216 return D3D_OK;
3219 static HRESULT WINAPI d3d9_device_DrawIndexedPrimitiveUP(IDirect3DDevice9Ex *iface,
3220 D3DPRIMITIVETYPE primitive_type, UINT min_vertex_idx, UINT vertex_count,
3221 UINT primitive_count, const void *index_data, D3DFORMAT index_format,
3222 const void *vertex_data, UINT vertex_stride)
3224 UINT idx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
3225 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3226 UINT idx_fmt_size = index_format == D3DFMT_INDEX16 ? 2 : 4;
3227 UINT vtx_size = vertex_count * vertex_stride;
3228 UINT idx_size = idx_count * idx_fmt_size;
3229 struct wined3d_map_desc wined3d_map_desc;
3230 struct wined3d_box wined3d_box = {0};
3231 struct wined3d_resource *ib, *vb;
3232 UINT vb_pos, ib_pos, align;
3233 HRESULT hr;
3235 TRACE("iface %p, primitive_type %#x, min_vertex_idx %u, vertex_count %u, primitive_count %u, "
3236 "index_data %p, index_format %#x, vertex_data %p, vertex_stride %u.\n",
3237 iface, primitive_type, min_vertex_idx, vertex_count, primitive_count,
3238 index_data, index_format, vertex_data, vertex_stride);
3240 if (!vertex_stride)
3242 WARN("vertex_stride is 0, returning D3DERR_INVALIDCALL.\n");
3243 return D3DERR_INVALIDCALL;
3245 if (!primitive_count)
3247 WARN("primitive_count is 0, returning D3D_OK.\n");
3248 return D3D_OK;
3251 wined3d_mutex_lock();
3253 if (!device->has_vertex_declaration)
3255 wined3d_mutex_unlock();
3256 WARN("Called without a valid vertex declaration set.\n");
3257 return D3DERR_INVALIDCALL;
3260 hr = d3d9_device_prepare_vertex_buffer(device, vtx_size);
3261 if (FAILED(hr))
3262 goto done;
3264 vb_pos = device->vertex_buffer_pos;
3265 align = vb_pos % vertex_stride;
3266 if (align) align = vertex_stride - align;
3267 if (vb_pos + vtx_size + align > device->vertex_buffer_size)
3268 vb_pos = 0;
3269 else
3270 vb_pos += align;
3272 wined3d_box.left = vb_pos;
3273 wined3d_box.right = vb_pos + vtx_size;
3274 vb = wined3d_buffer_get_resource(device->vertex_buffer);
3275 if (FAILED(hr = wined3d_resource_map(vb, 0, &wined3d_map_desc, &wined3d_box,
3276 WINED3D_MAP_WRITE | (vb_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD))))
3277 goto done;
3278 memcpy(wined3d_map_desc.data, (char *)vertex_data + min_vertex_idx * vertex_stride, vtx_size);
3279 wined3d_resource_unmap(vb, 0);
3280 device->vertex_buffer_pos = vb_pos + vtx_size;
3282 hr = d3d9_device_prepare_index_buffer(device, idx_size);
3283 if (FAILED(hr))
3284 goto done;
3286 ib_pos = device->index_buffer_pos;
3287 align = ib_pos % idx_fmt_size;
3288 if (align) align = idx_fmt_size - align;
3289 if (ib_pos + idx_size + align > device->index_buffer_size)
3290 ib_pos = 0;
3291 else
3292 ib_pos += align;
3294 wined3d_box.left = ib_pos;
3295 wined3d_box.right = ib_pos + idx_size;
3296 ib = wined3d_buffer_get_resource(device->index_buffer);
3297 if (FAILED(hr = wined3d_resource_map(ib, 0, &wined3d_map_desc, &wined3d_box,
3298 WINED3D_MAP_WRITE | (ib_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD))))
3299 goto done;
3300 memcpy(wined3d_map_desc.data, index_data, idx_size);
3301 wined3d_resource_unmap(ib, 0);
3302 device->index_buffer_pos = ib_pos + idx_size;
3304 hr = wined3d_stateblock_set_stream_source(device->state, 0, device->vertex_buffer, 0, vertex_stride);
3305 if (FAILED(hr))
3306 goto done;
3308 d3d9_generate_auto_mipmaps(device);
3309 wined3d_stateblock_set_index_buffer(device->state, device->index_buffer,
3310 wined3dformat_from_d3dformat(index_format));
3311 wined3d_stateblock_set_base_vertex_index(device->state, vb_pos / vertex_stride - min_vertex_idx);
3313 wined3d_device_apply_stateblock(device->wined3d_device, device->state);
3314 wined3d_device_set_primitive_type(device->wined3d_device, wined3d_primitive_type_from_d3d(primitive_type), 0);
3315 hr = wined3d_device_draw_indexed_primitive(device->wined3d_device, ib_pos / idx_fmt_size, idx_count);
3317 wined3d_stateblock_set_stream_source(device->state, 0, NULL, 0, 0);
3318 wined3d_stateblock_set_index_buffer(device->state, NULL, WINED3DFMT_UNKNOWN);
3320 if (SUCCEEDED(hr))
3321 d3d9_rts_flag_auto_gen_mipmap(device);
3323 done:
3324 wined3d_mutex_unlock();
3325 return hr;
3328 static HRESULT WINAPI d3d9_device_ProcessVertices(IDirect3DDevice9Ex *iface,
3329 UINT src_start_idx, UINT dst_idx, UINT vertex_count, IDirect3DVertexBuffer9 *dst_buffer,
3330 IDirect3DVertexDeclaration9 *declaration, DWORD flags)
3332 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3333 struct d3d9_vertexbuffer *dst_impl = unsafe_impl_from_IDirect3DVertexBuffer9(dst_buffer);
3334 struct d3d9_vertex_declaration *decl_impl = unsafe_impl_from_IDirect3DVertexDeclaration9(declaration);
3335 const struct wined3d_stateblock_state *state;
3336 const struct wined3d_stream_state *stream;
3337 struct d3d9_vertexbuffer *d3d9_buffer;
3338 unsigned int i, map;
3339 HRESULT hr;
3341 TRACE("iface %p, src_start_idx %u, dst_idx %u, vertex_count %u, dst_buffer %p, declaration %p, flags %#x.\n",
3342 iface, src_start_idx, dst_idx, vertex_count, dst_buffer, declaration, flags);
3344 wined3d_mutex_lock();
3345 state = device->stateblock_state;
3347 /* Note that an alternative approach would be to simply create these
3348 * buffers with WINED3D_RESOURCE_ACCESS_MAP_R and update them here like we
3349 * do for draws. In some regards that would be easier, but it seems less
3350 * than optimal to upload data to the GPU only to subsequently download it
3351 * again. */
3352 map = device->sysmem_vb;
3353 while (map)
3355 i = wined3d_bit_scan(&map);
3356 stream = &state->streams[i];
3358 d3d9_buffer = wined3d_buffer_get_parent(stream->buffer);
3359 if (FAILED(wined3d_stateblock_set_stream_source(device->state,
3360 i, d3d9_buffer->wined3d_buffer, stream->offset, stream->stride)))
3361 ERR("Failed to set stream source.\n");
3364 wined3d_device_apply_stateblock(device->wined3d_device, device->state);
3365 hr = wined3d_device_process_vertices(device->wined3d_device, src_start_idx, dst_idx, vertex_count,
3366 dst_impl->wined3d_buffer, decl_impl ? decl_impl->wined3d_declaration : NULL,
3367 flags, dst_impl->fvf);
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->draw_buffer, stream->offset, stream->stride)))
3378 ERR("Failed to set stream source.\n");
3381 wined3d_mutex_unlock();
3383 return hr;
3386 static HRESULT WINAPI d3d9_device_CreateVertexDeclaration(IDirect3DDevice9Ex *iface,
3387 const D3DVERTEXELEMENT9 *elements, IDirect3DVertexDeclaration9 **declaration)
3389 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3390 struct d3d9_vertex_declaration *object;
3391 HRESULT hr;
3393 TRACE("iface %p, elements %p, declaration %p.\n", iface, elements, declaration);
3395 if (!declaration)
3397 WARN("Caller passed a NULL declaration, returning D3DERR_INVALIDCALL.\n");
3398 return D3DERR_INVALIDCALL;
3401 if (SUCCEEDED(hr = d3d9_vertex_declaration_create(device, elements, &object)))
3402 *declaration = &object->IDirect3DVertexDeclaration9_iface;
3404 return hr;
3407 static HRESULT WINAPI d3d9_device_SetVertexDeclaration(IDirect3DDevice9Ex *iface,
3408 IDirect3DVertexDeclaration9 *declaration)
3410 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3411 struct d3d9_vertex_declaration *decl_impl = unsafe_impl_from_IDirect3DVertexDeclaration9(declaration);
3413 TRACE("iface %p, declaration %p.\n", iface, declaration);
3415 wined3d_mutex_lock();
3416 wined3d_stateblock_set_vertex_declaration(device->update_state,
3417 decl_impl ? decl_impl->wined3d_declaration : NULL);
3418 device->has_vertex_declaration = !!decl_impl;
3419 wined3d_mutex_unlock();
3421 return D3D_OK;
3424 static HRESULT WINAPI d3d9_device_GetVertexDeclaration(IDirect3DDevice9Ex *iface,
3425 IDirect3DVertexDeclaration9 **declaration)
3427 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3428 struct wined3d_vertex_declaration *wined3d_declaration;
3429 struct d3d9_vertex_declaration *declaration_impl;
3431 TRACE("iface %p, declaration %p.\n", iface, declaration);
3433 if (!declaration) return D3DERR_INVALIDCALL;
3435 wined3d_mutex_lock();
3436 if ((wined3d_declaration = device->stateblock_state->vertex_declaration))
3438 declaration_impl = wined3d_vertex_declaration_get_parent(wined3d_declaration);
3439 *declaration = &declaration_impl->IDirect3DVertexDeclaration9_iface;
3440 IDirect3DVertexDeclaration9_AddRef(*declaration);
3442 else
3444 *declaration = NULL;
3446 wined3d_mutex_unlock();
3448 TRACE("Returning %p.\n", *declaration);
3449 return D3D_OK;
3452 static struct wined3d_vertex_declaration *device_get_fvf_declaration(struct d3d9_device *device, DWORD fvf)
3454 struct wined3d_vertex_declaration *wined3d_declaration;
3455 struct fvf_declaration *fvf_decls = device->fvf_decls;
3456 struct d3d9_vertex_declaration *d3d9_declaration;
3457 D3DVERTEXELEMENT9 *elements;
3458 int p, low, high; /* deliberately signed */
3459 HRESULT hr;
3461 TRACE("Searching for declaration for fvf %08x... ", fvf);
3463 low = 0;
3464 high = device->fvf_decl_count - 1;
3465 while (low <= high)
3467 p = (low + high) >> 1;
3468 TRACE("%d ", p);
3470 if (fvf_decls[p].fvf == fvf)
3472 TRACE("found %p.\n", fvf_decls[p].decl);
3473 return fvf_decls[p].decl;
3476 if (fvf_decls[p].fvf < fvf)
3477 low = p + 1;
3478 else
3479 high = p - 1;
3481 TRACE("not found. Creating and inserting at position %d.\n", low);
3483 if (FAILED(hr = vdecl_convert_fvf(fvf, &elements)))
3484 return NULL;
3486 hr = d3d9_vertex_declaration_create(device, elements, &d3d9_declaration);
3487 heap_free(elements);
3488 if (FAILED(hr))
3489 return NULL;
3491 if (device->fvf_decl_size == device->fvf_decl_count)
3493 UINT grow = max(device->fvf_decl_size / 2, 8);
3495 if (!(fvf_decls = heap_realloc(fvf_decls, sizeof(*fvf_decls) * (device->fvf_decl_size + grow))))
3497 IDirect3DVertexDeclaration9_Release(&d3d9_declaration->IDirect3DVertexDeclaration9_iface);
3498 return NULL;
3500 device->fvf_decls = fvf_decls;
3501 device->fvf_decl_size += grow;
3504 d3d9_declaration->fvf = fvf;
3505 wined3d_declaration = d3d9_declaration->wined3d_declaration;
3506 wined3d_vertex_declaration_incref(wined3d_declaration);
3507 IDirect3DVertexDeclaration9_Release(&d3d9_declaration->IDirect3DVertexDeclaration9_iface);
3509 memmove(fvf_decls + low + 1, fvf_decls + low, sizeof(*fvf_decls) * (device->fvf_decl_count - low));
3510 fvf_decls[low].decl = wined3d_declaration;
3511 fvf_decls[low].fvf = fvf;
3512 ++device->fvf_decl_count;
3514 TRACE("Returning %p. %u declarations in array.\n", wined3d_declaration, device->fvf_decl_count);
3516 return wined3d_declaration;
3519 static HRESULT WINAPI d3d9_device_SetFVF(IDirect3DDevice9Ex *iface, DWORD fvf)
3521 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3522 struct wined3d_vertex_declaration *decl;
3524 TRACE("iface %p, fvf %#x.\n", iface, fvf);
3526 if (!fvf)
3528 WARN("%#x is not a valid FVF.\n", fvf);
3529 return D3D_OK;
3532 wined3d_mutex_lock();
3533 if (!(decl = device_get_fvf_declaration(device, fvf)))
3535 wined3d_mutex_unlock();
3536 ERR("Failed to create a vertex declaration for fvf %#x.\n", fvf);
3537 return D3DERR_DRIVERINTERNALERROR;
3540 wined3d_stateblock_set_vertex_declaration(device->update_state, decl);
3541 device->has_vertex_declaration = TRUE;
3542 wined3d_mutex_unlock();
3544 return D3D_OK;
3547 static HRESULT WINAPI d3d9_device_GetFVF(IDirect3DDevice9Ex *iface, DWORD *fvf)
3549 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3550 struct wined3d_vertex_declaration *wined3d_declaration;
3551 struct d3d9_vertex_declaration *d3d9_declaration;
3553 TRACE("iface %p, fvf %p.\n", iface, fvf);
3555 wined3d_mutex_lock();
3556 if ((wined3d_declaration = device->stateblock_state->vertex_declaration))
3558 d3d9_declaration = wined3d_vertex_declaration_get_parent(wined3d_declaration);
3559 *fvf = d3d9_declaration->fvf;
3561 else
3563 *fvf = 0;
3565 wined3d_mutex_unlock();
3567 TRACE("Returning FVF %#x.\n", *fvf);
3569 return D3D_OK;
3572 static HRESULT WINAPI d3d9_device_CreateVertexShader(IDirect3DDevice9Ex *iface,
3573 const DWORD *byte_code, IDirect3DVertexShader9 **shader)
3575 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3576 struct d3d9_vertexshader *object;
3577 HRESULT hr;
3579 TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
3581 if (!(object = heap_alloc_zero(sizeof(*object))))
3582 return E_OUTOFMEMORY;
3584 hr = vertexshader_init(object, device, byte_code);
3585 if (FAILED(hr))
3587 WARN("Failed to initialize vertex shader, hr %#x.\n", hr);
3588 heap_free(object);
3589 return hr;
3592 TRACE("Created vertex shader %p.\n", object);
3593 *shader = &object->IDirect3DVertexShader9_iface;
3595 return D3D_OK;
3598 static HRESULT WINAPI d3d9_device_SetVertexShader(IDirect3DDevice9Ex *iface, IDirect3DVertexShader9 *shader)
3600 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3601 struct d3d9_vertexshader *shader_obj = unsafe_impl_from_IDirect3DVertexShader9(shader);
3603 TRACE("iface %p, shader %p.\n", iface, shader);
3605 wined3d_mutex_lock();
3606 wined3d_stateblock_set_vertex_shader(device->update_state,
3607 shader_obj ? shader_obj->wined3d_shader : NULL);
3608 wined3d_mutex_unlock();
3610 return D3D_OK;
3613 static HRESULT WINAPI d3d9_device_GetVertexShader(IDirect3DDevice9Ex *iface, IDirect3DVertexShader9 **shader)
3615 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3616 struct d3d9_vertexshader *shader_impl;
3617 struct wined3d_shader *wined3d_shader;
3619 TRACE("iface %p, shader %p.\n", iface, shader);
3621 wined3d_mutex_lock();
3622 if ((wined3d_shader = device->stateblock_state->vs))
3624 shader_impl = wined3d_shader_get_parent(wined3d_shader);
3625 *shader = &shader_impl->IDirect3DVertexShader9_iface;
3626 IDirect3DVertexShader9_AddRef(*shader);
3628 else
3630 *shader = NULL;
3632 wined3d_mutex_unlock();
3634 TRACE("Returning %p.\n", *shader);
3636 return D3D_OK;
3639 static HRESULT WINAPI d3d9_device_SetVertexShaderConstantF(IDirect3DDevice9Ex *iface,
3640 UINT reg_idx, const float *data, UINT count)
3642 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3643 HRESULT hr;
3645 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3647 if (reg_idx + count > D3D9_MAX_VERTEX_SHADER_CONSTANTF)
3649 WARN("Trying to access %u constants, but d3d9 only supports %u\n",
3650 reg_idx + count, D3D9_MAX_VERTEX_SHADER_CONSTANTF);
3651 return D3DERR_INVALIDCALL;
3654 wined3d_mutex_lock();
3655 hr = wined3d_stateblock_set_vs_consts_f(device->update_state, reg_idx,
3656 count, (const struct wined3d_vec4 *)data);
3657 wined3d_mutex_unlock();
3659 return hr;
3662 static HRESULT WINAPI d3d9_device_GetVertexShaderConstantF(IDirect3DDevice9Ex *iface,
3663 UINT start_idx, float *constants, UINT count)
3665 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3666 const struct wined3d_vec4 *src;
3668 TRACE("iface %p, start_idx %u, constants %p, count %u.\n", iface, start_idx, constants, count);
3670 if (!constants)
3671 return D3DERR_INVALIDCALL;
3673 if (start_idx >= device->vs_uniform_count || count > device->vs_uniform_count - start_idx)
3675 WARN("Trying to access %u constants, but d3d9 only supports %u\n",
3676 start_idx + count, device->vs_uniform_count);
3677 return D3DERR_INVALIDCALL;
3680 wined3d_mutex_lock();
3681 src = device->stateblock_state->vs_consts_f;
3682 memcpy(constants, &src[start_idx], count * sizeof(*src));
3683 wined3d_mutex_unlock();
3685 return D3D_OK;
3688 static HRESULT WINAPI d3d9_device_SetVertexShaderConstantI(IDirect3DDevice9Ex *iface,
3689 UINT reg_idx, const int *data, UINT count)
3691 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3692 HRESULT hr;
3694 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3696 wined3d_mutex_lock();
3697 hr = wined3d_stateblock_set_vs_consts_i(device->update_state,
3698 reg_idx, count, (const struct wined3d_ivec4 *)data);
3699 wined3d_mutex_unlock();
3701 return hr;
3704 static HRESULT WINAPI d3d9_device_GetVertexShaderConstantI(IDirect3DDevice9Ex *iface,
3705 UINT start_idx, int *constants, UINT count)
3707 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3708 const struct wined3d_ivec4 *src;
3710 TRACE("iface %p, start_idx %u, constants %p, count %u.\n", iface, start_idx, constants, count);
3712 if (!constants || start_idx >= WINED3D_MAX_CONSTS_I)
3713 return WINED3DERR_INVALIDCALL;
3714 if (count > WINED3D_MAX_CONSTS_I - start_idx)
3715 count = WINED3D_MAX_CONSTS_I - start_idx;
3717 wined3d_mutex_lock();
3718 src = device->stateblock_state->vs_consts_i;
3719 memcpy(constants, &src[start_idx], count * sizeof(*src));
3720 wined3d_mutex_unlock();
3722 return D3D_OK;
3725 static HRESULT WINAPI d3d9_device_SetVertexShaderConstantB(IDirect3DDevice9Ex *iface,
3726 UINT reg_idx, const BOOL *data, UINT count)
3728 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3729 HRESULT hr;
3731 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3733 wined3d_mutex_lock();
3734 hr = wined3d_stateblock_set_vs_consts_b(device->update_state, reg_idx, count, data);
3735 wined3d_mutex_unlock();
3737 return hr;
3740 static HRESULT WINAPI d3d9_device_GetVertexShaderConstantB(IDirect3DDevice9Ex *iface,
3741 UINT start_idx, BOOL *constants, UINT count)
3743 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3745 TRACE("iface %p, start_idx %u, constants %p, count %u.\n", iface, start_idx, constants, count);
3747 if (!constants || start_idx >= WINED3D_MAX_CONSTS_B)
3748 return WINED3DERR_INVALIDCALL;
3749 if (count > WINED3D_MAX_CONSTS_B - start_idx)
3750 count = WINED3D_MAX_CONSTS_B - start_idx;
3752 wined3d_mutex_lock();
3753 memcpy(constants, &device->stateblock_state->vs_consts_b[start_idx], count * sizeof(*constants));
3754 wined3d_mutex_unlock();
3756 return D3D_OK;
3759 static HRESULT WINAPI d3d9_device_SetStreamSource(IDirect3DDevice9Ex *iface,
3760 UINT stream_idx, IDirect3DVertexBuffer9 *buffer, UINT offset, UINT stride)
3762 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3763 struct d3d9_vertexbuffer *buffer_impl = unsafe_impl_from_IDirect3DVertexBuffer9(buffer);
3764 struct wined3d_buffer *wined3d_buffer;
3765 HRESULT hr;
3767 TRACE("iface %p, stream_idx %u, buffer %p, offset %u, stride %u.\n",
3768 iface, stream_idx, buffer, offset, stride);
3770 if (stream_idx >= ARRAY_SIZE(device->stateblock_state->streams))
3772 WARN("Stream index %u out of range.\n", stream_idx);
3773 return WINED3DERR_INVALIDCALL;
3776 wined3d_mutex_lock();
3777 if (!buffer_impl)
3779 const struct wined3d_stream_state *stream = &device->stateblock_state->streams[stream_idx];
3781 offset = stream->offset;
3782 stride = stream->stride;
3783 wined3d_buffer = NULL;
3785 else if (buffer_impl->draw_buffer)
3787 wined3d_buffer = buffer_impl->draw_buffer;
3789 else
3791 wined3d_buffer = buffer_impl->wined3d_buffer;
3794 hr = wined3d_stateblock_set_stream_source(device->update_state, stream_idx, wined3d_buffer, offset, stride);
3795 if (SUCCEEDED(hr) && !device->recording)
3797 if (buffer_impl && buffer_impl->draw_buffer)
3798 device->sysmem_vb |= (1u << stream_idx);
3799 else
3800 device->sysmem_vb &= ~(1u << stream_idx);
3803 wined3d_mutex_unlock();
3805 return hr;
3808 static HRESULT WINAPI d3d9_device_GetStreamSource(IDirect3DDevice9Ex *iface,
3809 UINT stream_idx, IDirect3DVertexBuffer9 **buffer, UINT *offset, UINT *stride)
3811 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3812 const struct wined3d_stateblock_state *state;
3813 const struct wined3d_stream_state *stream;
3814 struct d3d9_vertexbuffer *buffer_impl;
3816 TRACE("iface %p, stream_idx %u, buffer %p, offset %p, stride %p.\n",
3817 iface, stream_idx, buffer, offset, stride);
3819 if (!buffer)
3820 return D3DERR_INVALIDCALL;
3822 if (stream_idx >= ARRAY_SIZE(state->streams))
3824 WARN("Stream index %u out of range.\n", stream_idx);
3825 return WINED3DERR_INVALIDCALL;
3828 wined3d_mutex_lock();
3829 stream = &device->stateblock_state->streams[stream_idx];
3830 if (stream->buffer)
3832 buffer_impl = wined3d_buffer_get_parent(stream->buffer);
3833 *buffer = &buffer_impl->IDirect3DVertexBuffer9_iface;
3834 IDirect3DVertexBuffer9_AddRef(*buffer);
3836 else
3837 *buffer = NULL;
3838 if (offset)
3839 *offset = stream->offset;
3840 *stride = stream->stride;
3841 wined3d_mutex_unlock();
3843 return D3D_OK;
3846 static HRESULT WINAPI d3d9_device_SetStreamSourceFreq(IDirect3DDevice9Ex *iface, UINT stream_idx, UINT freq)
3848 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3849 HRESULT hr;
3851 TRACE("iface %p, stream_idx %u, freq %u.\n", iface, stream_idx, freq);
3853 wined3d_mutex_lock();
3854 hr = wined3d_stateblock_set_stream_source_freq(device->update_state, stream_idx, freq);
3855 wined3d_mutex_unlock();
3857 return hr;
3860 static HRESULT WINAPI d3d9_device_GetStreamSourceFreq(IDirect3DDevice9Ex *iface, UINT stream_idx, UINT *freq)
3862 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3863 const struct wined3d_stream_state *stream;
3865 TRACE("iface %p, stream_idx %u, freq %p.\n", iface, stream_idx, freq);
3867 wined3d_mutex_lock();
3868 stream = &device->stateblock_state->streams[stream_idx];
3869 *freq = stream->flags | stream->frequency;
3870 wined3d_mutex_unlock();
3872 return D3D_OK;
3875 static HRESULT WINAPI d3d9_device_SetIndices(IDirect3DDevice9Ex *iface, IDirect3DIndexBuffer9 *buffer)
3877 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3878 struct d3d9_indexbuffer *ib = unsafe_impl_from_IDirect3DIndexBuffer9(buffer);
3879 struct wined3d_buffer *wined3d_buffer;
3881 TRACE("iface %p, buffer %p.\n", iface, buffer);
3883 if (!ib)
3884 wined3d_buffer = NULL;
3885 else if (ib->draw_buffer)
3886 wined3d_buffer = ib->draw_buffer;
3887 else
3888 wined3d_buffer = ib->wined3d_buffer;
3890 wined3d_mutex_lock();
3891 wined3d_stateblock_set_index_buffer(device->update_state, wined3d_buffer, ib ? ib->format : WINED3DFMT_UNKNOWN);
3892 if (!device->recording)
3893 device->sysmem_ib = ib && ib->draw_buffer;
3894 wined3d_mutex_unlock();
3896 return D3D_OK;
3899 static HRESULT WINAPI d3d9_device_GetIndices(IDirect3DDevice9Ex *iface, IDirect3DIndexBuffer9 **buffer)
3901 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3902 struct wined3d_buffer *wined3d_buffer;
3903 struct d3d9_indexbuffer *buffer_impl;
3905 TRACE("iface %p, buffer %p.\n", iface, buffer);
3907 if (!buffer)
3908 return D3DERR_INVALIDCALL;
3910 wined3d_mutex_lock();
3911 if ((wined3d_buffer = device->stateblock_state->index_buffer))
3913 buffer_impl = wined3d_buffer_get_parent(wined3d_buffer);
3914 *buffer = &buffer_impl->IDirect3DIndexBuffer9_iface;
3915 IDirect3DIndexBuffer9_AddRef(*buffer);
3917 else
3919 *buffer = NULL;
3921 wined3d_mutex_unlock();
3923 return D3D_OK;
3926 static HRESULT WINAPI d3d9_device_CreatePixelShader(IDirect3DDevice9Ex *iface,
3927 const DWORD *byte_code, IDirect3DPixelShader9 **shader)
3929 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3930 struct d3d9_pixelshader *object;
3931 HRESULT hr;
3933 TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
3935 if (!(object = heap_alloc_zero(sizeof(*object))))
3937 FIXME("Failed to allocate pixel shader memory.\n");
3938 return E_OUTOFMEMORY;
3941 hr = pixelshader_init(object, device, byte_code);
3942 if (FAILED(hr))
3944 WARN("Failed to initialize pixel shader, hr %#x.\n", hr);
3945 heap_free(object);
3946 return hr;
3949 TRACE("Created pixel shader %p.\n", object);
3950 *shader = &object->IDirect3DPixelShader9_iface;
3952 return D3D_OK;
3955 static HRESULT WINAPI d3d9_device_SetPixelShader(IDirect3DDevice9Ex *iface, IDirect3DPixelShader9 *shader)
3957 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3958 struct d3d9_pixelshader *shader_obj = unsafe_impl_from_IDirect3DPixelShader9(shader);
3960 TRACE("iface %p, shader %p.\n", iface, shader);
3962 wined3d_mutex_lock();
3963 wined3d_stateblock_set_pixel_shader(device->update_state,
3964 shader_obj ? shader_obj->wined3d_shader : NULL);
3965 wined3d_mutex_unlock();
3967 return D3D_OK;
3970 static HRESULT WINAPI d3d9_device_GetPixelShader(IDirect3DDevice9Ex *iface, IDirect3DPixelShader9 **shader)
3972 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3973 struct d3d9_pixelshader *shader_impl;
3974 struct wined3d_shader *wined3d_shader;
3976 TRACE("iface %p, shader %p.\n", iface, shader);
3978 if (!shader) return D3DERR_INVALIDCALL;
3980 wined3d_mutex_lock();
3981 if ((wined3d_shader = device->stateblock_state->ps))
3983 shader_impl = wined3d_shader_get_parent(wined3d_shader);
3984 *shader = &shader_impl->IDirect3DPixelShader9_iface;
3985 IDirect3DPixelShader9_AddRef(*shader);
3987 else
3989 *shader = NULL;
3991 wined3d_mutex_unlock();
3993 TRACE("Returning %p.\n", *shader);
3995 return D3D_OK;
3998 static HRESULT WINAPI d3d9_device_SetPixelShaderConstantF(IDirect3DDevice9Ex *iface,
3999 UINT reg_idx, const float *data, UINT count)
4001 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
4002 HRESULT hr;
4004 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
4006 wined3d_mutex_lock();
4007 hr = wined3d_stateblock_set_ps_consts_f(device->update_state,
4008 reg_idx, count, (const struct wined3d_vec4 *)data);
4009 wined3d_mutex_unlock();
4011 return hr;
4014 static HRESULT WINAPI d3d9_device_GetPixelShaderConstantF(IDirect3DDevice9Ex *iface,
4015 UINT start_idx, float *constants, UINT count)
4017 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
4018 const struct wined3d_vec4 *src;
4020 TRACE("iface %p, start_idx %u, constants %p, count %u.\n", iface, start_idx, constants, count);
4022 if (!constants || start_idx >= WINED3D_MAX_PS_CONSTS_F || count > WINED3D_MAX_PS_CONSTS_F - start_idx)
4023 return WINED3DERR_INVALIDCALL;
4025 wined3d_mutex_lock();
4026 src = device->stateblock_state->ps_consts_f;
4027 memcpy(constants, &src[start_idx], count * sizeof(*src));
4028 wined3d_mutex_unlock();
4030 return D3D_OK;
4033 static HRESULT WINAPI d3d9_device_SetPixelShaderConstantI(IDirect3DDevice9Ex *iface,
4034 UINT reg_idx, const int *data, UINT count)
4036 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
4037 HRESULT hr;
4039 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
4041 wined3d_mutex_lock();
4042 hr = wined3d_stateblock_set_ps_consts_i(device->update_state,
4043 reg_idx, count, (const struct wined3d_ivec4 *)data);
4044 wined3d_mutex_unlock();
4046 return hr;
4049 static HRESULT WINAPI d3d9_device_GetPixelShaderConstantI(IDirect3DDevice9Ex *iface,
4050 UINT start_idx, int *constants, UINT count)
4052 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
4053 const struct wined3d_ivec4 *src;
4055 TRACE("iface %p, start_idx %u, constants %p, count %u.\n", iface, start_idx, constants, count);
4057 if (!constants || start_idx >= WINED3D_MAX_CONSTS_I)
4058 return WINED3DERR_INVALIDCALL;
4059 if (count > WINED3D_MAX_CONSTS_I - start_idx)
4060 count = WINED3D_MAX_CONSTS_I - start_idx;
4062 wined3d_mutex_lock();
4063 src = device->stateblock_state->ps_consts_i;
4064 memcpy(constants, &src[start_idx], count * sizeof(*src));
4065 wined3d_mutex_unlock();
4067 return D3D_OK;
4070 static HRESULT WINAPI d3d9_device_SetPixelShaderConstantB(IDirect3DDevice9Ex *iface,
4071 UINT reg_idx, const BOOL *data, UINT count)
4073 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
4074 HRESULT hr;
4076 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
4078 wined3d_mutex_lock();
4079 hr = wined3d_stateblock_set_ps_consts_b(device->update_state, reg_idx, count, data);
4080 wined3d_mutex_unlock();
4082 return hr;
4085 static HRESULT WINAPI d3d9_device_GetPixelShaderConstantB(IDirect3DDevice9Ex *iface,
4086 UINT start_idx, BOOL *constants, UINT count)
4088 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
4090 TRACE("iface %p, start_idx %u, constants %p, count %u.\n", iface, start_idx, constants, count);
4092 if (!constants || start_idx >= WINED3D_MAX_CONSTS_I)
4093 return WINED3DERR_INVALIDCALL;
4094 if (count > WINED3D_MAX_CONSTS_I - start_idx)
4095 count = WINED3D_MAX_CONSTS_I - start_idx;
4097 wined3d_mutex_lock();
4098 memcpy(constants, &device->stateblock_state->ps_consts_b[start_idx], count * sizeof(*constants));
4099 wined3d_mutex_unlock();
4101 return D3D_OK;
4104 static HRESULT WINAPI d3d9_device_DrawRectPatch(IDirect3DDevice9Ex *iface, UINT handle,
4105 const float *segment_count, const D3DRECTPATCH_INFO *patch_info)
4107 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
4108 iface, handle, segment_count, patch_info);
4109 return D3D_OK;
4112 static HRESULT WINAPI d3d9_device_DrawTriPatch(IDirect3DDevice9Ex *iface, UINT handle,
4113 const float *segment_count, const D3DTRIPATCH_INFO *patch_info)
4115 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
4116 iface, handle, segment_count, patch_info);
4117 return D3D_OK;
4120 static HRESULT WINAPI d3d9_device_DeletePatch(IDirect3DDevice9Ex *iface, UINT handle)
4122 FIXME("iface %p, handle %#x unimplemented.\n", iface, handle);
4123 return D3DERR_INVALIDCALL;
4126 static HRESULT WINAPI d3d9_device_CreateQuery(IDirect3DDevice9Ex *iface, D3DQUERYTYPE type, IDirect3DQuery9 **query)
4128 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
4129 struct d3d9_query *object;
4130 HRESULT hr;
4132 TRACE("iface %p, type %#x, query %p.\n", iface, type, query);
4134 if (!(object = heap_alloc_zero(sizeof(*object))))
4135 return E_OUTOFMEMORY;
4137 hr = query_init(object, device, type);
4138 if (FAILED(hr))
4140 WARN("Failed to initialize query, hr %#x.\n", hr);
4141 heap_free(object);
4142 return hr;
4145 TRACE("Created query %p.\n", object);
4146 if (query) *query = &object->IDirect3DQuery9_iface;
4147 else IDirect3DQuery9_Release(&object->IDirect3DQuery9_iface);
4149 return D3D_OK;
4152 static HRESULT WINAPI d3d9_device_SetConvolutionMonoKernel(IDirect3DDevice9Ex *iface,
4153 UINT width, UINT height, float *rows, float *columns)
4155 FIXME("iface %p, width %u, height %u, rows %p, columns %p stub!\n",
4156 iface, width, height, rows, columns);
4158 return E_NOTIMPL;
4161 static HRESULT WINAPI d3d9_device_ComposeRects(IDirect3DDevice9Ex *iface,
4162 IDirect3DSurface9 *src_surface, IDirect3DSurface9 *dst_surface, IDirect3DVertexBuffer9 *src_descs,
4163 UINT rect_count, IDirect3DVertexBuffer9 *dst_descs, D3DCOMPOSERECTSOP operation, INT offset_x, INT offset_y)
4165 FIXME("iface %p, src_surface %p, dst_surface %p, src_descs %p, rect_count %u, "
4166 "dst_descs %p, operation %#x, offset_x %u, offset_y %u stub!\n",
4167 iface, src_surface, dst_surface, src_descs, rect_count,
4168 dst_descs, operation, offset_x, offset_y);
4170 return E_NOTIMPL;
4173 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_PresentEx(IDirect3DDevice9Ex *iface,
4174 const RECT *src_rect, const RECT *dst_rect, HWND dst_window_override,
4175 const RGNDATA *dirty_region, DWORD flags)
4177 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
4178 struct d3d9_swapchain *swapchain;
4179 unsigned int i;
4180 HRESULT hr;
4182 TRACE("iface %p, src_rect %s, dst_rect %s, dst_window_override %p, dirty_region %p, flags %#x.\n",
4183 iface, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect),
4184 dst_window_override, dirty_region, flags);
4186 if (device->device_state != D3D9_DEVICE_STATE_OK)
4187 return S_PRESENT_OCCLUDED;
4189 if (dirty_region)
4190 FIXME("Ignoring dirty_region %p.\n", dirty_region);
4192 wined3d_mutex_lock();
4193 for (i = 0; i < device->implicit_swapchain_count; ++i)
4195 swapchain = wined3d_swapchain_get_parent(device->implicit_swapchains[i]);
4196 if (FAILED(hr = wined3d_swapchain_present(swapchain->wined3d_swapchain,
4197 src_rect, dst_rect, dst_window_override, swapchain->swap_interval, flags)))
4199 wined3d_mutex_unlock();
4200 return hr;
4203 wined3d_mutex_unlock();
4205 return D3D_OK;
4208 static HRESULT WINAPI d3d9_device_GetGPUThreadPriority(IDirect3DDevice9Ex *iface, INT *priority)
4210 FIXME("iface %p, priority %p stub!\n", iface, priority);
4212 return E_NOTIMPL;
4215 static HRESULT WINAPI d3d9_device_SetGPUThreadPriority(IDirect3DDevice9Ex *iface, INT priority)
4217 FIXME("iface %p, priority %d stub!\n", iface, priority);
4219 return E_NOTIMPL;
4222 static HRESULT WINAPI d3d9_device_WaitForVBlank(IDirect3DDevice9Ex *iface, UINT swapchain_idx)
4224 FIXME("iface %p, swapchain_idx %u stub!\n", iface, swapchain_idx);
4226 return E_NOTIMPL;
4229 static HRESULT WINAPI d3d9_device_CheckResourceResidency(IDirect3DDevice9Ex *iface,
4230 IDirect3DResource9 **resources, UINT32 resource_count)
4232 FIXME("iface %p, resources %p, resource_count %u stub!\n",
4233 iface, resources, resource_count);
4235 return E_NOTIMPL;
4238 static HRESULT WINAPI d3d9_device_SetMaximumFrameLatency(IDirect3DDevice9Ex *iface, UINT max_latency)
4240 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
4242 TRACE("iface %p, max_latency %u.\n", iface, max_latency);
4244 if (max_latency > 30)
4245 return D3DERR_INVALIDCALL;
4247 wined3d_mutex_lock();
4248 wined3d_device_set_max_frame_latency(device->wined3d_device, max_latency);
4249 wined3d_mutex_unlock();
4251 return S_OK;
4254 static HRESULT WINAPI d3d9_device_GetMaximumFrameLatency(IDirect3DDevice9Ex *iface, UINT *max_latency)
4256 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
4258 TRACE("iface %p, max_latency %p.\n", iface, max_latency);
4260 wined3d_mutex_lock();
4261 *max_latency = wined3d_device_get_max_frame_latency(device->wined3d_device);
4262 wined3d_mutex_unlock();
4264 return S_OK;
4267 static HRESULT WINAPI d3d9_device_CheckDeviceState(IDirect3DDevice9Ex *iface, HWND dst_window)
4269 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
4270 struct wined3d_swapchain_desc swapchain_desc;
4272 TRACE("iface %p, dst_window %p.\n", iface, dst_window);
4274 wined3d_mutex_lock();
4275 wined3d_swapchain_get_desc(device->implicit_swapchains[0], &swapchain_desc);
4276 wined3d_mutex_unlock();
4278 if (swapchain_desc.windowed)
4279 return D3D_OK;
4281 /* FIXME: This is actually supposed to check if any other device is in
4282 * fullscreen mode. */
4283 if (dst_window != swapchain_desc.device_window)
4284 return device->device_state == D3D9_DEVICE_STATE_OK ? S_PRESENT_OCCLUDED : D3D_OK;
4286 return device->device_state == D3D9_DEVICE_STATE_OK ? D3D_OK : S_PRESENT_OCCLUDED;
4289 static HRESULT WINAPI d3d9_device_CreateRenderTargetEx(IDirect3DDevice9Ex *iface,
4290 UINT width, UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
4291 BOOL lockable, IDirect3DSurface9 **surface, HANDLE *shared_handle, DWORD usage)
4293 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
4294 unsigned int access = WINED3D_RESOURCE_ACCESS_GPU;
4296 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u, "
4297 "lockable %#x, surface %p, shared_handle %p, usage %#x.\n",
4298 iface, width, height, format, multisample_type, multisample_quality,
4299 lockable, surface, shared_handle, usage);
4301 *surface = NULL;
4303 if (usage & (D3DUSAGE_DEPTHSTENCIL | D3DUSAGE_RENDERTARGET))
4305 WARN("Invalid usage %#x.\n", usage);
4306 return D3DERR_INVALIDCALL;
4309 if (shared_handle)
4310 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
4312 if (lockable)
4313 access |= WINED3D_RESOURCE_ACCESS_MAP_R | WINED3D_RESOURCE_ACCESS_MAP_W;
4315 return d3d9_device_create_surface(device, 0, wined3dformat_from_d3dformat(format),
4316 wined3d_multisample_type_from_d3d(multisample_type), multisample_quality,
4317 usage & WINED3DUSAGE_MASK, WINED3D_BIND_RENDER_TARGET, access, width, height, NULL, surface);
4320 static HRESULT WINAPI d3d9_device_CreateOffscreenPlainSurfaceEx(IDirect3DDevice9Ex *iface,
4321 UINT width, UINT height, D3DFORMAT format, D3DPOOL pool, IDirect3DSurface9 **surface,
4322 HANDLE *shared_handle, DWORD usage)
4324 FIXME("iface %p, width %u, height %u, format %#x, pool %#x, surface %p, shared_handle %p, usage %#x stub!\n",
4325 iface, width, height, format, pool, surface, shared_handle, usage);
4327 return E_NOTIMPL;
4330 static HRESULT WINAPI d3d9_device_CreateDepthStencilSurfaceEx(IDirect3DDevice9Ex *iface,
4331 UINT width, UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
4332 BOOL discard, IDirect3DSurface9 **surface, HANDLE *shared_handle, DWORD usage)
4334 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
4335 DWORD flags = 0;
4337 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u, "
4338 "discard %#x, surface %p, shared_handle %p, usage %#x.\n",
4339 iface, width, height, format, multisample_type, multisample_quality,
4340 discard, surface, shared_handle, usage);
4342 if (usage & (D3DUSAGE_DEPTHSTENCIL | D3DUSAGE_RENDERTARGET))
4344 WARN("Invalid usage %#x.\n", usage);
4345 return D3DERR_INVALIDCALL;
4348 if (shared_handle)
4349 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
4351 if (discard)
4352 flags |= WINED3D_TEXTURE_CREATE_DISCARD;
4354 *surface = NULL;
4355 return d3d9_device_create_surface(device, flags, wined3dformat_from_d3dformat(format),
4356 wined3d_multisample_type_from_d3d(multisample_type), multisample_quality, usage & WINED3DUSAGE_MASK,
4357 WINED3D_BIND_DEPTH_STENCIL, WINED3D_RESOURCE_ACCESS_GPU, width, height, NULL, surface);
4360 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_ResetEx(IDirect3DDevice9Ex *iface,
4361 D3DPRESENT_PARAMETERS *present_parameters, D3DDISPLAYMODEEX *mode)
4363 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
4365 TRACE("iface %p, present_parameters %p, mode %p.\n", iface, present_parameters, mode);
4367 if (!present_parameters->Windowed == !mode)
4369 WARN("Mode can be passed if and only if Windowed is FALSE.\n");
4370 return D3DERR_INVALIDCALL;
4373 if (mode && (mode->Width != present_parameters->BackBufferWidth
4374 || mode->Height != present_parameters->BackBufferHeight))
4376 WARN("Mode and back buffer mismatch (mode %ux%u, backbuffer %ux%u).\n",
4377 mode->Width, mode->Height,
4378 present_parameters->BackBufferWidth, present_parameters->BackBufferHeight);
4379 return D3DERR_INVALIDCALL;
4382 return d3d9_device_reset(device, present_parameters, mode);
4385 static HRESULT WINAPI d3d9_device_GetDisplayModeEx(IDirect3DDevice9Ex *iface,
4386 UINT swapchain_idx, D3DDISPLAYMODEEX *mode, D3DDISPLAYROTATION *rotation)
4388 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
4389 struct wined3d_display_mode wined3d_mode;
4390 HRESULT hr;
4392 TRACE("iface %p, swapchain_idx %u, mode %p, rotation %p.\n",
4393 iface, swapchain_idx, mode, rotation);
4395 if (mode->Size != sizeof(*mode))
4396 return D3DERR_INVALIDCALL;
4398 wined3d_mutex_lock();
4399 hr = wined3d_device_get_display_mode(device->wined3d_device, swapchain_idx, &wined3d_mode,
4400 (enum wined3d_display_rotation *)rotation);
4401 wined3d_mutex_unlock();
4403 if (SUCCEEDED(hr))
4405 mode->Width = wined3d_mode.width;
4406 mode->Height = wined3d_mode.height;
4407 mode->RefreshRate = wined3d_mode.refresh_rate;
4408 mode->Format = d3dformat_from_wined3dformat(wined3d_mode.format_id);
4409 mode->ScanLineOrdering = d3dscanlineordering_from_wined3d(wined3d_mode.scanline_ordering);
4412 return hr;
4415 static const struct IDirect3DDevice9ExVtbl d3d9_device_vtbl =
4417 /* IUnknown */
4418 d3d9_device_QueryInterface,
4419 d3d9_device_AddRef,
4420 d3d9_device_Release,
4421 /* IDirect3DDevice9 */
4422 d3d9_device_TestCooperativeLevel,
4423 d3d9_device_GetAvailableTextureMem,
4424 d3d9_device_EvictManagedResources,
4425 d3d9_device_GetDirect3D,
4426 d3d9_device_GetDeviceCaps,
4427 d3d9_device_GetDisplayMode,
4428 d3d9_device_GetCreationParameters,
4429 d3d9_device_SetCursorProperties,
4430 d3d9_device_SetCursorPosition,
4431 d3d9_device_ShowCursor,
4432 d3d9_device_CreateAdditionalSwapChain,
4433 d3d9_device_GetSwapChain,
4434 d3d9_device_GetNumberOfSwapChains,
4435 d3d9_device_Reset,
4436 d3d9_device_Present,
4437 d3d9_device_GetBackBuffer,
4438 d3d9_device_GetRasterStatus,
4439 d3d9_device_SetDialogBoxMode,
4440 d3d9_device_SetGammaRamp,
4441 d3d9_device_GetGammaRamp,
4442 d3d9_device_CreateTexture,
4443 d3d9_device_CreateVolumeTexture,
4444 d3d9_device_CreateCubeTexture,
4445 d3d9_device_CreateVertexBuffer,
4446 d3d9_device_CreateIndexBuffer,
4447 d3d9_device_CreateRenderTarget,
4448 d3d9_device_CreateDepthStencilSurface,
4449 d3d9_device_UpdateSurface,
4450 d3d9_device_UpdateTexture,
4451 d3d9_device_GetRenderTargetData,
4452 d3d9_device_GetFrontBufferData,
4453 d3d9_device_StretchRect,
4454 d3d9_device_ColorFill,
4455 d3d9_device_CreateOffscreenPlainSurface,
4456 d3d9_device_SetRenderTarget,
4457 d3d9_device_GetRenderTarget,
4458 d3d9_device_SetDepthStencilSurface,
4459 d3d9_device_GetDepthStencilSurface,
4460 d3d9_device_BeginScene,
4461 d3d9_device_EndScene,
4462 d3d9_device_Clear,
4463 d3d9_device_SetTransform,
4464 d3d9_device_GetTransform,
4465 d3d9_device_MultiplyTransform,
4466 d3d9_device_SetViewport,
4467 d3d9_device_GetViewport,
4468 d3d9_device_SetMaterial,
4469 d3d9_device_GetMaterial,
4470 d3d9_device_SetLight,
4471 d3d9_device_GetLight,
4472 d3d9_device_LightEnable,
4473 d3d9_device_GetLightEnable,
4474 d3d9_device_SetClipPlane,
4475 d3d9_device_GetClipPlane,
4476 d3d9_device_SetRenderState,
4477 d3d9_device_GetRenderState,
4478 d3d9_device_CreateStateBlock,
4479 d3d9_device_BeginStateBlock,
4480 d3d9_device_EndStateBlock,
4481 d3d9_device_SetClipStatus,
4482 d3d9_device_GetClipStatus,
4483 d3d9_device_GetTexture,
4484 d3d9_device_SetTexture,
4485 d3d9_device_GetTextureStageState,
4486 d3d9_device_SetTextureStageState,
4487 d3d9_device_GetSamplerState,
4488 d3d9_device_SetSamplerState,
4489 d3d9_device_ValidateDevice,
4490 d3d9_device_SetPaletteEntries,
4491 d3d9_device_GetPaletteEntries,
4492 d3d9_device_SetCurrentTexturePalette,
4493 d3d9_device_GetCurrentTexturePalette,
4494 d3d9_device_SetScissorRect,
4495 d3d9_device_GetScissorRect,
4496 d3d9_device_SetSoftwareVertexProcessing,
4497 d3d9_device_GetSoftwareVertexProcessing,
4498 d3d9_device_SetNPatchMode,
4499 d3d9_device_GetNPatchMode,
4500 d3d9_device_DrawPrimitive,
4501 d3d9_device_DrawIndexedPrimitive,
4502 d3d9_device_DrawPrimitiveUP,
4503 d3d9_device_DrawIndexedPrimitiveUP,
4504 d3d9_device_ProcessVertices,
4505 d3d9_device_CreateVertexDeclaration,
4506 d3d9_device_SetVertexDeclaration,
4507 d3d9_device_GetVertexDeclaration,
4508 d3d9_device_SetFVF,
4509 d3d9_device_GetFVF,
4510 d3d9_device_CreateVertexShader,
4511 d3d9_device_SetVertexShader,
4512 d3d9_device_GetVertexShader,
4513 d3d9_device_SetVertexShaderConstantF,
4514 d3d9_device_GetVertexShaderConstantF,
4515 d3d9_device_SetVertexShaderConstantI,
4516 d3d9_device_GetVertexShaderConstantI,
4517 d3d9_device_SetVertexShaderConstantB,
4518 d3d9_device_GetVertexShaderConstantB,
4519 d3d9_device_SetStreamSource,
4520 d3d9_device_GetStreamSource,
4521 d3d9_device_SetStreamSourceFreq,
4522 d3d9_device_GetStreamSourceFreq,
4523 d3d9_device_SetIndices,
4524 d3d9_device_GetIndices,
4525 d3d9_device_CreatePixelShader,
4526 d3d9_device_SetPixelShader,
4527 d3d9_device_GetPixelShader,
4528 d3d9_device_SetPixelShaderConstantF,
4529 d3d9_device_GetPixelShaderConstantF,
4530 d3d9_device_SetPixelShaderConstantI,
4531 d3d9_device_GetPixelShaderConstantI,
4532 d3d9_device_SetPixelShaderConstantB,
4533 d3d9_device_GetPixelShaderConstantB,
4534 d3d9_device_DrawRectPatch,
4535 d3d9_device_DrawTriPatch,
4536 d3d9_device_DeletePatch,
4537 d3d9_device_CreateQuery,
4538 /* IDirect3DDevice9Ex */
4539 d3d9_device_SetConvolutionMonoKernel,
4540 d3d9_device_ComposeRects,
4541 d3d9_device_PresentEx,
4542 d3d9_device_GetGPUThreadPriority,
4543 d3d9_device_SetGPUThreadPriority,
4544 d3d9_device_WaitForVBlank,
4545 d3d9_device_CheckResourceResidency,
4546 d3d9_device_SetMaximumFrameLatency,
4547 d3d9_device_GetMaximumFrameLatency,
4548 d3d9_device_CheckDeviceState,
4549 d3d9_device_CreateRenderTargetEx,
4550 d3d9_device_CreateOffscreenPlainSurfaceEx,
4551 d3d9_device_CreateDepthStencilSurfaceEx,
4552 d3d9_device_ResetEx,
4553 d3d9_device_GetDisplayModeEx,
4556 static inline struct d3d9_device *device_from_device_parent(struct wined3d_device_parent *device_parent)
4558 return CONTAINING_RECORD(device_parent, struct d3d9_device, device_parent);
4561 static void CDECL device_parent_wined3d_device_created(struct wined3d_device_parent *device_parent,
4562 struct wined3d_device *device)
4564 TRACE("device_parent %p, device %p.\n", device_parent, device);
4567 static void CDECL device_parent_mode_changed(struct wined3d_device_parent *device_parent)
4569 TRACE("device_parent %p.\n", device_parent);
4572 static void CDECL device_parent_activate(struct wined3d_device_parent *device_parent, BOOL activate)
4574 struct d3d9_device *device = device_from_device_parent(device_parent);
4576 TRACE("device_parent %p, activate %#x.\n", device_parent, activate);
4578 if (!device->d3d_parent)
4579 return;
4581 if (!activate)
4582 InterlockedCompareExchange(&device->device_state, D3D9_DEVICE_STATE_LOST, D3D9_DEVICE_STATE_OK);
4583 else if (device->d3d_parent->extended)
4584 InterlockedCompareExchange(&device->device_state, D3D9_DEVICE_STATE_OK, D3D9_DEVICE_STATE_LOST);
4585 else
4586 InterlockedCompareExchange(&device->device_state, D3D9_DEVICE_STATE_NOT_RESET, D3D9_DEVICE_STATE_LOST);
4589 static HRESULT CDECL device_parent_texture_sub_resource_created(struct wined3d_device_parent *device_parent,
4590 enum wined3d_resource_type type, struct wined3d_texture *wined3d_texture, unsigned int sub_resource_idx,
4591 void **parent, const struct wined3d_parent_ops **parent_ops)
4593 TRACE("device_parent %p, type %#x, wined3d_texture %p, sub_resource_idx %u, parent %p, parent_ops %p.\n",
4594 device_parent, type, wined3d_texture, sub_resource_idx, parent, parent_ops);
4596 if (type == WINED3D_RTYPE_TEXTURE_2D)
4598 struct d3d9_surface *d3d_surface;
4600 if (!(d3d_surface = heap_alloc_zero(sizeof(*d3d_surface))))
4601 return E_OUTOFMEMORY;
4603 surface_init(d3d_surface, wined3d_texture, sub_resource_idx, parent_ops);
4604 *parent = d3d_surface;
4605 TRACE("Created surface %p.\n", d3d_surface);
4607 else if (type == WINED3D_RTYPE_TEXTURE_3D)
4609 struct d3d9_volume *d3d_volume;
4611 if (!(d3d_volume = heap_alloc_zero(sizeof(*d3d_volume))))
4612 return E_OUTOFMEMORY;
4614 volume_init(d3d_volume, wined3d_texture, sub_resource_idx, parent_ops);
4615 *parent = d3d_volume;
4616 TRACE("Created volume %p.\n", d3d_volume);
4618 else
4620 ERR("Unhandled resource type %#x.\n", type);
4621 return E_FAIL;
4624 return D3D_OK;
4627 static HRESULT CDECL device_parent_create_swapchain_texture(struct wined3d_device_parent *device_parent,
4628 void *container_parent, const struct wined3d_resource_desc *desc, DWORD texture_flags,
4629 struct wined3d_texture **texture)
4631 struct d3d9_device *device = device_from_device_parent(device_parent);
4632 struct d3d9_surface *d3d_surface;
4633 HRESULT hr;
4635 TRACE("device_parent %p, container_parent %p, desc %p, texture flags %#x, texture %p.\n",
4636 device_parent, container_parent, desc, texture_flags, texture);
4638 if (container_parent == device_parent)
4639 container_parent = &device->IDirect3DDevice9Ex_iface;
4641 if (FAILED(hr = wined3d_texture_create(device->wined3d_device, desc, 1, 1,
4642 texture_flags, NULL, container_parent, &d3d9_null_wined3d_parent_ops, texture)))
4644 WARN("Failed to create texture, hr %#x.\n", hr);
4645 return hr;
4648 d3d_surface = wined3d_texture_get_sub_resource_parent(*texture, 0);
4649 d3d_surface->parent_device = &device->IDirect3DDevice9Ex_iface;
4651 return hr;
4654 static const struct wined3d_device_parent_ops d3d9_wined3d_device_parent_ops =
4656 device_parent_wined3d_device_created,
4657 device_parent_mode_changed,
4658 device_parent_activate,
4659 device_parent_texture_sub_resource_created,
4660 device_parent_create_swapchain_texture,
4663 static void setup_fpu(void)
4665 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
4666 WORD cw;
4667 __asm__ volatile ("fnstcw %0" : "=m" (cw));
4668 cw = (cw & ~0xf3f) | 0x3f;
4669 __asm__ volatile ("fldcw %0" : : "m" (cw));
4670 #elif defined(__i386__) && defined(_MSC_VER)
4671 WORD cw;
4672 __asm fnstcw cw;
4673 cw = (cw & ~0xf3f) | 0x3f;
4674 __asm fldcw cw;
4675 #else
4676 FIXME("FPU setup not implemented for this platform.\n");
4677 #endif
4680 HRESULT device_init(struct d3d9_device *device, struct d3d9 *parent, struct wined3d *wined3d,
4681 UINT adapter, D3DDEVTYPE device_type, HWND focus_window, DWORD flags,
4682 D3DPRESENT_PARAMETERS *parameters, D3DDISPLAYMODEEX *mode)
4684 struct wined3d_swapchain_desc *swapchain_desc;
4685 struct wined3d_adapter *wined3d_adapter;
4686 struct d3d9_swapchain *d3d_swapchain;
4687 struct wined3d_caps wined3d_caps;
4688 unsigned int output_idx;
4689 unsigned i, count = 1;
4690 D3DCAPS9 caps;
4691 HRESULT hr;
4693 static const enum wined3d_feature_level feature_levels[] =
4695 WINED3D_FEATURE_LEVEL_9_3,
4696 WINED3D_FEATURE_LEVEL_9_2,
4697 WINED3D_FEATURE_LEVEL_9_1,
4698 WINED3D_FEATURE_LEVEL_8,
4699 WINED3D_FEATURE_LEVEL_7,
4700 WINED3D_FEATURE_LEVEL_6,
4701 WINED3D_FEATURE_LEVEL_5,
4704 output_idx = adapter;
4705 if (output_idx >= parent->wined3d_output_count)
4706 return D3DERR_INVALIDCALL;
4708 if (mode)
4709 FIXME("Ignoring display mode.\n");
4711 device->IDirect3DDevice9Ex_iface.lpVtbl = &d3d9_device_vtbl;
4712 device->device_parent.ops = &d3d9_wined3d_device_parent_ops;
4713 device->adapter_ordinal = adapter;
4714 device->refcount = 1;
4716 if (!(flags & D3DCREATE_FPU_PRESERVE)) setup_fpu();
4718 wined3d_mutex_lock();
4719 wined3d_adapter = wined3d_output_get_adapter(parent->wined3d_outputs[output_idx]);
4720 if (FAILED(hr = wined3d_device_create(wined3d, wined3d_adapter, wined3d_device_type_from_d3d(device_type),
4721 focus_window, flags, 4, feature_levels, ARRAY_SIZE(feature_levels),
4722 &device->device_parent, &device->wined3d_device)))
4724 WARN("Failed to create wined3d device, hr %#x.\n", hr);
4725 wined3d_mutex_unlock();
4726 return hr;
4729 wined3d_get_device_caps(wined3d_adapter, wined3d_device_type_from_d3d(device_type), &wined3d_caps);
4730 d3d9_caps_from_wined3dcaps(parent, adapter, &caps, &wined3d_caps);
4731 device->max_user_clip_planes = caps.MaxUserClipPlanes;
4732 device->vs_uniform_count = caps.MaxVertexShaderConst;
4733 if (flags & D3DCREATE_ADAPTERGROUP_DEVICE)
4734 count = caps.NumberOfAdaptersInGroup;
4736 if (FAILED(hr = wined3d_stateblock_create(device->wined3d_device, NULL, WINED3D_SBT_PRIMARY, &device->state)))
4738 ERR("Failed to create the primary stateblock, hr %#x.\n", hr);
4739 wined3d_device_decref(device->wined3d_device);
4740 wined3d_mutex_unlock();
4741 return hr;
4743 device->stateblock_state = wined3d_stateblock_get_state(device->state);
4744 device->update_state = device->state;
4746 if (flags & D3DCREATE_MULTITHREADED)
4747 wined3d_device_set_multithreaded(device->wined3d_device);
4749 if (!parameters->Windowed)
4751 if (!focus_window)
4752 focus_window = parameters->hDeviceWindow;
4753 if (FAILED(hr = wined3d_device_acquire_focus_window(device->wined3d_device, focus_window)))
4755 ERR("Failed to acquire focus window, hr %#x.\n", hr);
4756 wined3d_device_decref(device->wined3d_device);
4757 wined3d_mutex_unlock();
4758 return hr;
4762 if (!(swapchain_desc = heap_alloc(sizeof(*swapchain_desc) * count)))
4764 ERR("Failed to allocate wined3d parameters.\n");
4765 wined3d_device_release_focus_window(device->wined3d_device);
4766 wined3d_device_decref(device->wined3d_device);
4767 wined3d_mutex_unlock();
4768 return E_OUTOFMEMORY;
4771 for (i = 0; i < count; ++i)
4773 if (!wined3d_swapchain_desc_from_d3d9(&swapchain_desc[i],
4774 parent->wined3d_outputs[output_idx + i], &parameters[i], parent->extended))
4776 wined3d_device_release_focus_window(device->wined3d_device);
4777 wined3d_device_decref(device->wined3d_device);
4778 heap_free(swapchain_desc);
4779 wined3d_mutex_unlock();
4780 return D3DERR_INVALIDCALL;
4782 swapchain_desc[i].flags |= WINED3D_SWAPCHAIN_IMPLICIT;
4785 if (FAILED(hr = d3d9_swapchain_create(device, swapchain_desc,
4786 wined3dswapinterval_from_d3d(parameters->PresentationInterval), &d3d_swapchain)))
4788 WARN("Failed to create swapchain, hr %#x.\n", hr);
4789 wined3d_device_release_focus_window(device->wined3d_device);
4790 heap_free(swapchain_desc);
4791 wined3d_device_decref(device->wined3d_device);
4792 wined3d_mutex_unlock();
4793 return hr;
4796 wined3d_swapchain_incref(d3d_swapchain->wined3d_swapchain);
4797 IDirect3DSwapChain9Ex_Release(&d3d_swapchain->IDirect3DSwapChain9Ex_iface);
4799 wined3d_stateblock_set_render_state(device->state, WINED3D_RS_ZENABLE,
4800 !!swapchain_desc->enable_auto_depth_stencil);
4801 device_reset_viewport_state(device);
4803 if (FAILED(hr = d3d9_device_get_swapchains(device)))
4805 wined3d_swapchain_decref(d3d_swapchain->wined3d_swapchain);
4806 wined3d_device_release_focus_window(device->wined3d_device);
4807 wined3d_device_decref(device->wined3d_device);
4808 heap_free(swapchain_desc);
4809 wined3d_mutex_unlock();
4810 return E_OUTOFMEMORY;
4813 for (i = 0; i < count; ++i)
4815 present_parameters_from_wined3d_swapchain_desc(&parameters[i],
4816 &swapchain_desc[i], parameters[i].PresentationInterval);
4819 wined3d_mutex_unlock();
4821 heap_free(swapchain_desc);
4823 /* Initialize the converted declaration array. This creates a valid pointer
4824 * and when adding decls HeapReAlloc() can be used without further checking. */
4825 if (!(device->fvf_decls = heap_alloc(0)))
4827 ERR("Failed to allocate FVF vertex declaration map memory.\n");
4828 wined3d_mutex_lock();
4829 heap_free(device->implicit_swapchains);
4830 wined3d_swapchain_decref(d3d_swapchain->wined3d_swapchain);
4831 wined3d_device_release_focus_window(device->wined3d_device);
4832 wined3d_device_decref(device->wined3d_device);
4833 wined3d_mutex_unlock();
4834 return E_OUTOFMEMORY;
4837 /* We could also simply ignore the initial rendertarget since it's known
4838 * not to be a texture (we currently use these only for automatic mipmap
4839 * generation). */
4840 wined3d_mutex_lock();
4841 device->render_targets[0] = wined3d_rendertarget_view_get_sub_resource_parent(
4842 wined3d_device_get_rendertarget_view(device->wined3d_device, 0));
4843 wined3d_mutex_unlock();
4845 IDirect3D9Ex_AddRef(&parent->IDirect3D9Ex_iface);
4846 device->d3d_parent = parent;
4848 return D3D_OK;