ntdll: Validate blocks in the heap pending free request list.
[wine.git] / dlls / d3d9 / device.c
blobeddb2a0ebddf42a0cb5c6583f50945cb6a98fe0a
1 /*
2 * IDirect3DDevice9 implementation
4 * Copyright 2002-2005 Jason Edmeades
5 * Copyright 2002-2005 Raphael Junqueira
6 * Copyright 2005 Oliver Stieber
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "d3d9_private.h"
25 WINE_DEFAULT_DEBUG_CHANNEL(d3d9);
27 static void STDMETHODCALLTYPE d3d9_null_wined3d_object_destroyed(void *parent) {}
29 const struct wined3d_parent_ops d3d9_null_wined3d_parent_ops =
31 d3d9_null_wined3d_object_destroyed,
34 static void wined3d_color_from_d3dcolor(struct wined3d_color *wined3d_colour, D3DCOLOR d3d_colour)
36 wined3d_colour->r = ((d3d_colour >> 16) & 0xff) / 255.0f;
37 wined3d_colour->g = ((d3d_colour >> 8) & 0xff) / 255.0f;
38 wined3d_colour->b = (d3d_colour & 0xff) / 255.0f;
39 wined3d_colour->a = ((d3d_colour >> 24) & 0xff) / 255.0f;
42 D3DFORMAT d3dformat_from_wined3dformat(enum wined3d_format_id format)
44 BYTE *c = (BYTE *)&format;
46 /* Don't translate FOURCC formats */
47 if (isprint(c[0]) && isprint(c[1]) && isprint(c[2]) && isprint(c[3]))
48 return (D3DFORMAT)format;
50 switch(format)
52 case WINED3DFMT_UNKNOWN: return D3DFMT_UNKNOWN;
53 case WINED3DFMT_B8G8R8_UNORM: return D3DFMT_R8G8B8;
54 case WINED3DFMT_B8G8R8A8_UNORM: return D3DFMT_A8R8G8B8;
55 case WINED3DFMT_B8G8R8X8_UNORM: return D3DFMT_X8R8G8B8;
56 case WINED3DFMT_B5G6R5_UNORM: return D3DFMT_R5G6B5;
57 case WINED3DFMT_B5G5R5X1_UNORM: return D3DFMT_X1R5G5B5;
58 case WINED3DFMT_B5G5R5A1_UNORM: return D3DFMT_A1R5G5B5;
59 case WINED3DFMT_B4G4R4A4_UNORM: return D3DFMT_A4R4G4B4;
60 case WINED3DFMT_B2G3R3_UNORM: return D3DFMT_R3G3B2;
61 case WINED3DFMT_A8_UNORM: return D3DFMT_A8;
62 case WINED3DFMT_B2G3R3A8_UNORM: return D3DFMT_A8R3G3B2;
63 case WINED3DFMT_B4G4R4X4_UNORM: return D3DFMT_X4R4G4B4;
64 case WINED3DFMT_R10G10B10A2_UNORM: return D3DFMT_A2B10G10R10;
65 case WINED3DFMT_R8G8B8A8_UNORM: return D3DFMT_A8B8G8R8;
66 case WINED3DFMT_R8G8B8X8_UNORM: return D3DFMT_X8B8G8R8;
67 case WINED3DFMT_R16G16_UNORM: return D3DFMT_G16R16;
68 case WINED3DFMT_B10G10R10A2_UNORM: return D3DFMT_A2R10G10B10;
69 case WINED3DFMT_R16G16B16A16_UNORM: return D3DFMT_A16B16G16R16;
70 case WINED3DFMT_P8_UINT_A8_UNORM: return D3DFMT_A8P8;
71 case WINED3DFMT_P8_UINT: return D3DFMT_P8;
72 case WINED3DFMT_L8_UNORM: return D3DFMT_L8;
73 case WINED3DFMT_L8A8_UNORM: return D3DFMT_A8L8;
74 case WINED3DFMT_L4A4_UNORM: return D3DFMT_A4L4;
75 case WINED3DFMT_R8G8_SNORM: return D3DFMT_V8U8;
76 case WINED3DFMT_R5G5_SNORM_L6_UNORM: return D3DFMT_L6V5U5;
77 case WINED3DFMT_R8G8_SNORM_L8X8_UNORM: return D3DFMT_X8L8V8U8;
78 case WINED3DFMT_R8G8B8A8_SNORM: return D3DFMT_Q8W8V8U8;
79 case WINED3DFMT_R16G16_SNORM: return D3DFMT_V16U16;
80 case WINED3DFMT_R10G10B10_SNORM_A2_UNORM: return D3DFMT_A2W10V10U10;
81 case WINED3DFMT_D16_LOCKABLE: return D3DFMT_D16_LOCKABLE;
82 case WINED3DFMT_D32_UNORM: return D3DFMT_D32;
83 case WINED3DFMT_S1_UINT_D15_UNORM: return D3DFMT_D15S1;
84 case WINED3DFMT_D24_UNORM_S8_UINT: return D3DFMT_D24S8;
85 case WINED3DFMT_X8D24_UNORM: return D3DFMT_D24X8;
86 case WINED3DFMT_S4X4_UINT_D24_UNORM: return D3DFMT_D24X4S4;
87 case WINED3DFMT_D16_UNORM: return D3DFMT_D16;
88 case WINED3DFMT_L16_UNORM: return D3DFMT_L16;
89 case WINED3DFMT_D32_FLOAT: return D3DFMT_D32F_LOCKABLE;
90 case WINED3DFMT_S8_UINT_D24_FLOAT: return D3DFMT_D24FS8;
91 case WINED3DFMT_R16_UINT: return D3DFMT_INDEX16;
92 case WINED3DFMT_R32_UINT: return D3DFMT_INDEX32;
93 case WINED3DFMT_R16G16B16A16_SNORM: return D3DFMT_Q16W16V16U16;
94 case WINED3DFMT_R16_FLOAT: return D3DFMT_R16F;
95 case WINED3DFMT_R16G16_FLOAT: return D3DFMT_G16R16F;
96 case WINED3DFMT_R16G16B16A16_FLOAT: return D3DFMT_A16B16G16R16F;
97 case WINED3DFMT_R32_FLOAT: return D3DFMT_R32F;
98 case WINED3DFMT_R32G32_FLOAT: return D3DFMT_G32R32F;
99 case WINED3DFMT_R32G32B32A32_FLOAT: return D3DFMT_A32B32G32R32F;
100 case WINED3DFMT_R8G8_SNORM_Cx: return D3DFMT_CxV8U8;
101 default:
102 FIXME("Unhandled wined3d format %#x.\n", format);
103 return D3DFMT_UNKNOWN;
107 enum wined3d_format_id wined3dformat_from_d3dformat(D3DFORMAT format)
109 BYTE *c = (BYTE *)&format;
111 /* Don't translate FOURCC formats */
112 if (isprint(c[0]) && isprint(c[1]) && isprint(c[2]) && isprint(c[3]))
113 return (enum wined3d_format_id)format;
115 switch(format)
117 case D3DFMT_UNKNOWN: return WINED3DFMT_UNKNOWN;
118 case D3DFMT_R8G8B8: return WINED3DFMT_B8G8R8_UNORM;
119 case D3DFMT_A8R8G8B8: return WINED3DFMT_B8G8R8A8_UNORM;
120 case D3DFMT_X8R8G8B8: return WINED3DFMT_B8G8R8X8_UNORM;
121 case D3DFMT_R5G6B5: return WINED3DFMT_B5G6R5_UNORM;
122 case D3DFMT_X1R5G5B5: return WINED3DFMT_B5G5R5X1_UNORM;
123 case D3DFMT_A1R5G5B5: return WINED3DFMT_B5G5R5A1_UNORM;
124 case D3DFMT_A4R4G4B4: return WINED3DFMT_B4G4R4A4_UNORM;
125 case D3DFMT_R3G3B2: return WINED3DFMT_B2G3R3_UNORM;
126 case D3DFMT_A8: return WINED3DFMT_A8_UNORM;
127 case D3DFMT_A8R3G3B2: return WINED3DFMT_B2G3R3A8_UNORM;
128 case D3DFMT_X4R4G4B4: return WINED3DFMT_B4G4R4X4_UNORM;
129 case D3DFMT_A2B10G10R10: return WINED3DFMT_R10G10B10A2_UNORM;
130 case D3DFMT_A8B8G8R8: return WINED3DFMT_R8G8B8A8_UNORM;
131 case D3DFMT_X8B8G8R8: return WINED3DFMT_R8G8B8X8_UNORM;
132 case D3DFMT_G16R16: return WINED3DFMT_R16G16_UNORM;
133 case D3DFMT_A2R10G10B10: return WINED3DFMT_B10G10R10A2_UNORM;
134 case D3DFMT_A16B16G16R16: return WINED3DFMT_R16G16B16A16_UNORM;
135 case D3DFMT_A8P8: return WINED3DFMT_P8_UINT_A8_UNORM;
136 case D3DFMT_P8: return WINED3DFMT_P8_UINT;
137 case D3DFMT_L8: return WINED3DFMT_L8_UNORM;
138 case D3DFMT_A8L8: return WINED3DFMT_L8A8_UNORM;
139 case D3DFMT_A4L4: return WINED3DFMT_L4A4_UNORM;
140 case D3DFMT_V8U8: return WINED3DFMT_R8G8_SNORM;
141 case D3DFMT_L6V5U5: return WINED3DFMT_R5G5_SNORM_L6_UNORM;
142 case D3DFMT_X8L8V8U8: return WINED3DFMT_R8G8_SNORM_L8X8_UNORM;
143 case D3DFMT_Q8W8V8U8: return WINED3DFMT_R8G8B8A8_SNORM;
144 case D3DFMT_V16U16: return WINED3DFMT_R16G16_SNORM;
145 case D3DFMT_A2W10V10U10: return WINED3DFMT_R10G10B10_SNORM_A2_UNORM;
146 case D3DFMT_D16_LOCKABLE: return WINED3DFMT_D16_LOCKABLE;
147 case D3DFMT_D32: return WINED3DFMT_D32_UNORM;
148 case D3DFMT_D15S1: return WINED3DFMT_S1_UINT_D15_UNORM;
149 case D3DFMT_D24S8: return WINED3DFMT_D24_UNORM_S8_UINT;
150 case D3DFMT_D24X8: return WINED3DFMT_X8D24_UNORM;
151 case D3DFMT_D24X4S4: return WINED3DFMT_S4X4_UINT_D24_UNORM;
152 case D3DFMT_D16: return WINED3DFMT_D16_UNORM;
153 case D3DFMT_L16: return WINED3DFMT_L16_UNORM;
154 case D3DFMT_D32F_LOCKABLE: return WINED3DFMT_D32_FLOAT;
155 case D3DFMT_D24FS8: return WINED3DFMT_S8_UINT_D24_FLOAT;
156 case D3DFMT_INDEX16: return WINED3DFMT_R16_UINT;
157 case D3DFMT_INDEX32: return WINED3DFMT_R32_UINT;
158 case D3DFMT_Q16W16V16U16: return WINED3DFMT_R16G16B16A16_SNORM;
159 case D3DFMT_R16F: return WINED3DFMT_R16_FLOAT;
160 case D3DFMT_G16R16F: return WINED3DFMT_R16G16_FLOAT;
161 case D3DFMT_A16B16G16R16F: return WINED3DFMT_R16G16B16A16_FLOAT;
162 case D3DFMT_R32F: return WINED3DFMT_R32_FLOAT;
163 case D3DFMT_G32R32F: return WINED3DFMT_R32G32_FLOAT;
164 case D3DFMT_A32B32G32R32F: return WINED3DFMT_R32G32B32A32_FLOAT;
165 case D3DFMT_CxV8U8: return WINED3DFMT_R8G8_SNORM_Cx;
166 default:
167 FIXME("Unhandled D3DFORMAT %#x.\n", format);
168 return WINED3DFMT_UNKNOWN;
172 unsigned int wined3dmapflags_from_d3dmapflags(unsigned int flags, unsigned int usage)
174 static const unsigned int handled = D3DLOCK_NOSYSLOCK
175 | D3DLOCK_NOOVERWRITE
176 | D3DLOCK_DISCARD
177 | D3DLOCK_DONOTWAIT
178 | D3DLOCK_NO_DIRTY_UPDATE;
179 unsigned int wined3d_flags;
181 wined3d_flags = flags & handled;
182 if (~usage & D3DUSAGE_WRITEONLY && !(flags & (D3DLOCK_NOOVERWRITE | D3DLOCK_DISCARD)))
183 wined3d_flags |= WINED3D_MAP_READ;
184 if (!(flags & D3DLOCK_READONLY))
185 wined3d_flags |= WINED3D_MAP_WRITE;
186 if (!(wined3d_flags & (WINED3D_MAP_READ | WINED3D_MAP_WRITE)))
187 wined3d_flags |= WINED3D_MAP_WRITE;
188 flags &= ~(handled | D3DLOCK_READONLY);
190 if (flags)
191 FIXME("Unhandled flags %#x.\n", flags);
193 return wined3d_flags;
196 static UINT vertex_count_from_primitive_count(D3DPRIMITIVETYPE primitive_type, UINT primitive_count)
198 switch (primitive_type)
200 case D3DPT_POINTLIST:
201 return primitive_count;
203 case D3DPT_LINELIST:
204 return primitive_count * 2;
206 case D3DPT_LINESTRIP:
207 return primitive_count + 1;
209 case D3DPT_TRIANGLELIST:
210 return primitive_count * 3;
212 case D3DPT_TRIANGLESTRIP:
213 case D3DPT_TRIANGLEFAN:
214 return primitive_count + 2;
216 default:
217 FIXME("Unhandled primitive type %#x.\n", primitive_type);
218 return 0;
222 static D3DSWAPEFFECT d3dswapeffect_from_wined3dswapeffect(enum wined3d_swap_effect effect)
224 switch (effect)
226 case WINED3D_SWAP_EFFECT_DISCARD:
227 return D3DSWAPEFFECT_DISCARD;
228 case WINED3D_SWAP_EFFECT_SEQUENTIAL:
229 return D3DSWAPEFFECT_FLIP;
230 case WINED3D_SWAP_EFFECT_COPY:
231 return D3DSWAPEFFECT_COPY;
232 case WINED3D_SWAP_EFFECT_OVERLAY:
233 return D3DSWAPEFFECT_OVERLAY;
234 case WINED3D_SWAP_EFFECT_FLIP_SEQUENTIAL:
235 return D3DSWAPEFFECT_FLIPEX;
236 default:
237 FIXME("Unhandled swap effect %#x.\n", effect);
238 return D3DSWAPEFFECT_FLIP;
242 void present_parameters_from_wined3d_swapchain_desc(D3DPRESENT_PARAMETERS *present_parameters,
243 const struct wined3d_swapchain_desc *swapchain_desc, DWORD presentation_interval)
245 present_parameters->BackBufferWidth = swapchain_desc->backbuffer_width;
246 present_parameters->BackBufferHeight = swapchain_desc->backbuffer_height;
247 present_parameters->BackBufferFormat = d3dformat_from_wined3dformat(swapchain_desc->backbuffer_format);
248 present_parameters->BackBufferCount = swapchain_desc->backbuffer_count;
249 present_parameters->MultiSampleType = d3dmultisample_type_from_wined3d(swapchain_desc->multisample_type);
250 present_parameters->MultiSampleQuality = swapchain_desc->multisample_quality;
251 present_parameters->SwapEffect = d3dswapeffect_from_wined3dswapeffect(swapchain_desc->swap_effect);
252 present_parameters->hDeviceWindow = swapchain_desc->device_window;
253 present_parameters->Windowed = swapchain_desc->windowed;
254 present_parameters->EnableAutoDepthStencil = swapchain_desc->enable_auto_depth_stencil;
255 present_parameters->AutoDepthStencilFormat
256 = d3dformat_from_wined3dformat(swapchain_desc->auto_depth_stencil_format);
257 present_parameters->Flags = swapchain_desc->flags & D3DPRESENTFLAGS_MASK;
258 present_parameters->FullScreen_RefreshRateInHz = swapchain_desc->refresh_rate;
259 present_parameters->PresentationInterval = presentation_interval;
262 static enum wined3d_swap_effect wined3dswapeffect_from_d3dswapeffect(D3DSWAPEFFECT effect)
264 switch (effect)
266 case D3DSWAPEFFECT_DISCARD:
267 return WINED3D_SWAP_EFFECT_DISCARD;
268 case D3DSWAPEFFECT_FLIP:
269 return WINED3D_SWAP_EFFECT_SEQUENTIAL;
270 case D3DSWAPEFFECT_COPY:
271 return WINED3D_SWAP_EFFECT_COPY;
272 case D3DSWAPEFFECT_OVERLAY:
273 return WINED3D_SWAP_EFFECT_OVERLAY;
274 case D3DSWAPEFFECT_FLIPEX:
275 return WINED3D_SWAP_EFFECT_FLIP_SEQUENTIAL;
276 default:
277 FIXME("Unhandled swap effect %#x.\n", effect);
278 return WINED3D_SWAP_EFFECT_SEQUENTIAL;
282 static enum wined3d_swap_interval wined3dswapinterval_from_d3d(DWORD interval)
284 switch (interval)
286 case D3DPRESENT_INTERVAL_IMMEDIATE:
287 return WINED3D_SWAP_INTERVAL_IMMEDIATE;
288 case D3DPRESENT_INTERVAL_ONE:
289 return WINED3D_SWAP_INTERVAL_ONE;
290 case D3DPRESENT_INTERVAL_TWO:
291 return WINED3D_SWAP_INTERVAL_TWO;
292 case D3DPRESENT_INTERVAL_THREE:
293 return WINED3D_SWAP_INTERVAL_THREE;
294 case D3DPRESENT_INTERVAL_FOUR:
295 return WINED3D_SWAP_INTERVAL_FOUR;
296 default:
297 FIXME("Unhandled presentation interval %#lx.\n", interval);
298 case D3DPRESENT_INTERVAL_DEFAULT:
299 return WINED3D_SWAP_INTERVAL_DEFAULT;
303 static BOOL wined3d_swapchain_desc_from_d3d9(struct wined3d_swapchain_desc *swapchain_desc,
304 struct wined3d_output *output, const D3DPRESENT_PARAMETERS *present_parameters,
305 BOOL extended)
307 D3DSWAPEFFECT highest_swapeffect = extended ? D3DSWAPEFFECT_FLIPEX : D3DSWAPEFFECT_COPY;
308 UINT highest_bb_count = extended ? 30 : 3;
310 if (!present_parameters->SwapEffect || present_parameters->SwapEffect > highest_swapeffect)
312 WARN("Invalid swap effect %u passed.\n", present_parameters->SwapEffect);
313 return FALSE;
315 if (present_parameters->BackBufferCount > highest_bb_count
316 || (present_parameters->SwapEffect == D3DSWAPEFFECT_COPY
317 && present_parameters->BackBufferCount > 1))
319 WARN("Invalid backbuffer count %u.\n", present_parameters->BackBufferCount);
320 return FALSE;
322 switch (present_parameters->PresentationInterval)
324 case D3DPRESENT_INTERVAL_DEFAULT:
325 case D3DPRESENT_INTERVAL_ONE:
326 case D3DPRESENT_INTERVAL_TWO:
327 case D3DPRESENT_INTERVAL_THREE:
328 case D3DPRESENT_INTERVAL_FOUR:
329 case D3DPRESENT_INTERVAL_IMMEDIATE:
330 break;
331 default:
332 WARN("Invalid presentation interval %#x.\n", present_parameters->PresentationInterval);
333 return FALSE;
336 swapchain_desc->output = output;
337 swapchain_desc->backbuffer_width = present_parameters->BackBufferWidth;
338 swapchain_desc->backbuffer_height = present_parameters->BackBufferHeight;
339 swapchain_desc->backbuffer_format = wined3dformat_from_d3dformat(present_parameters->BackBufferFormat);
340 swapchain_desc->backbuffer_count = max(1, present_parameters->BackBufferCount);
341 swapchain_desc->backbuffer_bind_flags = WINED3D_BIND_RENDER_TARGET;
342 swapchain_desc->multisample_type = wined3d_multisample_type_from_d3d(present_parameters->MultiSampleType);
343 swapchain_desc->multisample_quality = present_parameters->MultiSampleQuality;
344 swapchain_desc->swap_effect = wined3dswapeffect_from_d3dswapeffect(present_parameters->SwapEffect);
345 swapchain_desc->device_window = present_parameters->hDeviceWindow;
346 swapchain_desc->windowed = present_parameters->Windowed;
347 swapchain_desc->enable_auto_depth_stencil = present_parameters->EnableAutoDepthStencil;
348 swapchain_desc->auto_depth_stencil_format
349 = wined3dformat_from_d3dformat(present_parameters->AutoDepthStencilFormat);
350 swapchain_desc->flags
351 = (present_parameters->Flags & D3DPRESENTFLAGS_MASK) | WINED3D_SWAPCHAIN_ALLOW_MODE_SWITCH;
352 if (extended)
353 swapchain_desc->flags |= WINED3D_SWAPCHAIN_RESTORE_WINDOW_STATE;
354 if ((present_parameters->Flags & D3DPRESENTFLAG_LOCKABLE_BACKBUFFER)
355 && (is_gdi_compat_wined3dformat(swapchain_desc->backbuffer_format)
356 /* WINED3DFMT_UNKNOWN creates the swapchain with the current
357 * display format, which is always GDI-compatible. */
358 || swapchain_desc->backbuffer_format == WINED3DFMT_UNKNOWN))
359 swapchain_desc->flags |= WINED3D_SWAPCHAIN_GDI_COMPATIBLE;
360 swapchain_desc->refresh_rate = present_parameters->FullScreen_RefreshRateInHz;
361 swapchain_desc->auto_restore_display_mode = TRUE;
363 if (present_parameters->Flags & ~D3DPRESENTFLAGS_MASK)
364 FIXME("Unhandled flags %#lx.\n", present_parameters->Flags & ~D3DPRESENTFLAGS_MASK);
366 return TRUE;
369 void d3d9_caps_from_wined3dcaps(const struct d3d9 *d3d9, unsigned int adapter_ordinal,
370 D3DCAPS9 *caps, const struct wined3d_caps *wined3d_caps)
372 static const DWORD ps_minor_version[] = {0, 4, 0, 0};
373 static const DWORD vs_minor_version[] = {0, 1, 0, 0};
374 static const DWORD texture_filter_caps =
375 D3DPTFILTERCAPS_MINFPOINT | D3DPTFILTERCAPS_MINFLINEAR | D3DPTFILTERCAPS_MINFANISOTROPIC |
376 D3DPTFILTERCAPS_MINFPYRAMIDALQUAD | D3DPTFILTERCAPS_MINFGAUSSIANQUAD|
377 D3DPTFILTERCAPS_MIPFPOINT | D3DPTFILTERCAPS_MIPFLINEAR | D3DPTFILTERCAPS_MAGFPOINT |
378 D3DPTFILTERCAPS_MAGFLINEAR |D3DPTFILTERCAPS_MAGFANISOTROPIC|D3DPTFILTERCAPS_MAGFPYRAMIDALQUAD|
379 D3DPTFILTERCAPS_MAGFGAUSSIANQUAD;
380 struct wined3d_adapter *wined3d_adapter;
381 struct wined3d_output_desc output_desc;
382 struct wined3d_output *wined3d_output;
383 unsigned int output_idx;
384 HRESULT hr;
386 caps->DeviceType = (D3DDEVTYPE)wined3d_caps->DeviceType;
387 caps->AdapterOrdinal = adapter_ordinal;
388 caps->Caps = wined3d_caps->Caps;
389 caps->Caps2 = wined3d_caps->Caps2;
390 caps->Caps3 = wined3d_caps->Caps3;
391 caps->PresentationIntervals = D3DPRESENT_INTERVAL_IMMEDIATE | D3DPRESENT_INTERVAL_ONE;
392 caps->CursorCaps = wined3d_caps->CursorCaps;
393 caps->DevCaps = wined3d_caps->DevCaps;
394 caps->PrimitiveMiscCaps = wined3d_caps->PrimitiveMiscCaps;
395 caps->RasterCaps = wined3d_caps->RasterCaps;
396 caps->ZCmpCaps = wined3d_caps->ZCmpCaps;
397 caps->SrcBlendCaps = wined3d_caps->SrcBlendCaps;
398 caps->DestBlendCaps = wined3d_caps->DestBlendCaps;
399 caps->AlphaCmpCaps = wined3d_caps->AlphaCmpCaps;
400 caps->ShadeCaps = wined3d_caps->ShadeCaps;
401 caps->TextureCaps = wined3d_caps->TextureCaps;
402 caps->TextureFilterCaps = wined3d_caps->TextureFilterCaps;
403 caps->CubeTextureFilterCaps = wined3d_caps->CubeTextureFilterCaps;
404 caps->VolumeTextureFilterCaps = wined3d_caps->VolumeTextureFilterCaps;
405 caps->TextureAddressCaps = wined3d_caps->TextureAddressCaps;
406 caps->VolumeTextureAddressCaps = wined3d_caps->VolumeTextureAddressCaps;
407 caps->LineCaps = wined3d_caps->LineCaps;
408 caps->MaxTextureWidth = wined3d_caps->MaxTextureWidth;
409 caps->MaxTextureHeight = wined3d_caps->MaxTextureHeight;
410 caps->MaxVolumeExtent = wined3d_caps->MaxVolumeExtent;
411 caps->MaxTextureRepeat = wined3d_caps->MaxTextureRepeat;
412 caps->MaxTextureAspectRatio = wined3d_caps->MaxTextureAspectRatio;
413 caps->MaxAnisotropy = wined3d_caps->MaxAnisotropy;
414 caps->MaxVertexW = wined3d_caps->MaxVertexW;
415 caps->GuardBandLeft = wined3d_caps->GuardBandLeft;
416 caps->GuardBandTop = wined3d_caps->GuardBandTop;
417 caps->GuardBandRight = wined3d_caps->GuardBandRight;
418 caps->GuardBandBottom = wined3d_caps->GuardBandBottom;
419 caps->ExtentsAdjust = wined3d_caps->ExtentsAdjust;
420 caps->StencilCaps = wined3d_caps->StencilCaps;
421 caps->FVFCaps = wined3d_caps->FVFCaps;
422 caps->TextureOpCaps = wined3d_caps->TextureOpCaps;
423 caps->MaxTextureBlendStages = wined3d_caps->MaxTextureBlendStages;
424 caps->MaxSimultaneousTextures = wined3d_caps->MaxSimultaneousTextures;
425 caps->VertexProcessingCaps = wined3d_caps->VertexProcessingCaps;
426 caps->MaxActiveLights = wined3d_caps->MaxActiveLights;
427 caps->MaxUserClipPlanes = wined3d_caps->MaxUserClipPlanes;
428 caps->MaxVertexBlendMatrices = wined3d_caps->MaxVertexBlendMatrices;
429 caps->MaxVertexBlendMatrixIndex = wined3d_caps->MaxVertexBlendMatrixIndex;
430 caps->MaxPointSize = wined3d_caps->MaxPointSize;
431 caps->MaxPrimitiveCount = wined3d_caps->MaxPrimitiveCount;
432 caps->MaxVertexIndex = wined3d_caps->MaxVertexIndex;
433 caps->MaxStreams = wined3d_caps->MaxStreams;
434 caps->MaxStreamStride = wined3d_caps->MaxStreamStride;
435 caps->VertexShaderVersion = wined3d_caps->VertexShaderVersion;
436 caps->MaxVertexShaderConst = wined3d_caps->MaxVertexShaderConst;
437 caps->PixelShaderVersion = wined3d_caps->PixelShaderVersion;
438 caps->PixelShader1xMaxValue = wined3d_caps->PixelShader1xMaxValue;
439 caps->DevCaps2 = wined3d_caps->DevCaps2;
440 caps->MaxNpatchTessellationLevel = wined3d_caps->MaxNpatchTessellationLevel;
441 caps->DeclTypes = wined3d_caps->DeclTypes;
442 caps->NumSimultaneousRTs = wined3d_caps->NumSimultaneousRTs;
443 caps->StretchRectFilterCaps = wined3d_caps->StretchRectFilterCaps;
444 caps->VS20Caps.Caps = wined3d_caps->VS20Caps.caps;
445 caps->VS20Caps.DynamicFlowControlDepth = wined3d_caps->VS20Caps.dynamic_flow_control_depth;
446 caps->VS20Caps.NumTemps = wined3d_caps->VS20Caps.temp_count;
447 caps->VS20Caps.StaticFlowControlDepth = wined3d_caps->VS20Caps.static_flow_control_depth;
448 caps->PS20Caps.Caps = wined3d_caps->PS20Caps.caps;
449 caps->PS20Caps.DynamicFlowControlDepth = wined3d_caps->PS20Caps.dynamic_flow_control_depth;
450 caps->PS20Caps.NumTemps = wined3d_caps->PS20Caps.temp_count;
451 caps->PS20Caps.StaticFlowControlDepth = wined3d_caps->PS20Caps.static_flow_control_depth;
452 caps->PS20Caps.NumInstructionSlots = wined3d_caps->PS20Caps.instruction_slot_count;
453 caps->VertexTextureFilterCaps = wined3d_caps->VertexTextureFilterCaps;
454 caps->MaxVShaderInstructionsExecuted = wined3d_caps->MaxVShaderInstructionsExecuted;
455 caps->MaxPShaderInstructionsExecuted = wined3d_caps->MaxPShaderInstructionsExecuted;
456 caps->MaxVertexShader30InstructionSlots = wined3d_caps->MaxVertexShader30InstructionSlots;
457 caps->MaxPixelShader30InstructionSlots = wined3d_caps->MaxPixelShader30InstructionSlots;
459 /* Some functionality is implemented in d3d9.dll, not wined3d.dll. Add the needed caps. */
460 caps->DevCaps2 |= D3DDEVCAPS2_CAN_STRETCHRECT_FROM_TEXTURES;
462 /* Filter wined3d caps. */
463 caps->TextureFilterCaps &= texture_filter_caps;
464 caps->CubeTextureFilterCaps &= texture_filter_caps;
465 caps->VolumeTextureFilterCaps &= texture_filter_caps;
467 caps->DevCaps &=
468 D3DDEVCAPS_EXECUTESYSTEMMEMORY | D3DDEVCAPS_EXECUTEVIDEOMEMORY | D3DDEVCAPS_TLVERTEXSYSTEMMEMORY |
469 D3DDEVCAPS_TLVERTEXVIDEOMEMORY | D3DDEVCAPS_TEXTURESYSTEMMEMORY| D3DDEVCAPS_TEXTUREVIDEOMEMORY |
470 D3DDEVCAPS_DRAWPRIMTLVERTEX | D3DDEVCAPS_CANRENDERAFTERFLIP | D3DDEVCAPS_TEXTURENONLOCALVIDMEM|
471 D3DDEVCAPS_DRAWPRIMITIVES2 | D3DDEVCAPS_SEPARATETEXTUREMEMORIES |
472 D3DDEVCAPS_DRAWPRIMITIVES2EX | D3DDEVCAPS_HWTRANSFORMANDLIGHT| D3DDEVCAPS_CANBLTSYSTONONLOCAL |
473 D3DDEVCAPS_HWRASTERIZATION | D3DDEVCAPS_PUREDEVICE | D3DDEVCAPS_QUINTICRTPATCHES |
474 D3DDEVCAPS_RTPATCHES | D3DDEVCAPS_RTPATCHHANDLEZERO | D3DDEVCAPS_NPATCHES;
476 caps->ShadeCaps &=
477 D3DPSHADECAPS_COLORGOURAUDRGB | D3DPSHADECAPS_SPECULARGOURAUDRGB |
478 D3DPSHADECAPS_ALPHAGOURAUDBLEND | D3DPSHADECAPS_FOGGOURAUD;
480 caps->RasterCaps &=
481 D3DPRASTERCAPS_DITHER | D3DPRASTERCAPS_ZTEST | D3DPRASTERCAPS_FOGVERTEX |
482 D3DPRASTERCAPS_FOGTABLE | D3DPRASTERCAPS_MIPMAPLODBIAS | D3DPRASTERCAPS_ZBUFFERLESSHSR |
483 D3DPRASTERCAPS_FOGRANGE | D3DPRASTERCAPS_ANISOTROPY | D3DPRASTERCAPS_WBUFFER |
484 D3DPRASTERCAPS_WFOG | D3DPRASTERCAPS_ZFOG | D3DPRASTERCAPS_COLORPERSPECTIVE |
485 D3DPRASTERCAPS_SCISSORTEST | D3DPRASTERCAPS_SLOPESCALEDEPTHBIAS |
486 D3DPRASTERCAPS_DEPTHBIAS | D3DPRASTERCAPS_MULTISAMPLE_TOGGLE;
488 caps->DevCaps2 &=
489 D3DDEVCAPS2_STREAMOFFSET | D3DDEVCAPS2_DMAPNPATCH | D3DDEVCAPS2_ADAPTIVETESSRTPATCH |
490 D3DDEVCAPS2_ADAPTIVETESSNPATCH | D3DDEVCAPS2_CAN_STRETCHRECT_FROM_TEXTURES |
491 D3DDEVCAPS2_PRESAMPLEDDMAPNPATCH| D3DDEVCAPS2_VERTEXELEMENTSCANSHARESTREAMOFFSET;
493 caps->Caps2 &=
494 D3DCAPS2_FULLSCREENGAMMA | D3DCAPS2_CANCALIBRATEGAMMA | D3DCAPS2_RESERVED |
495 D3DCAPS2_CANMANAGERESOURCE | D3DCAPS2_DYNAMICTEXTURES | D3DCAPS2_CANAUTOGENMIPMAP;
497 caps->VertexProcessingCaps &=
498 D3DVTXPCAPS_TEXGEN | D3DVTXPCAPS_MATERIALSOURCE7 | D3DVTXPCAPS_DIRECTIONALLIGHTS |
499 D3DVTXPCAPS_POSITIONALLIGHTS | D3DVTXPCAPS_LOCALVIEWER | D3DVTXPCAPS_TWEENING |
500 D3DVTXPCAPS_TEXGEN_SPHEREMAP | D3DVTXPCAPS_NO_TEXGEN_NONLOCALVIEWER;
502 caps->TextureCaps &=
503 D3DPTEXTURECAPS_PERSPECTIVE | D3DPTEXTURECAPS_POW2 | D3DPTEXTURECAPS_ALPHA |
504 D3DPTEXTURECAPS_SQUAREONLY | D3DPTEXTURECAPS_TEXREPEATNOTSCALEDBYSIZE |
505 D3DPTEXTURECAPS_ALPHAPALETTE | D3DPTEXTURECAPS_NONPOW2CONDITIONAL |
506 D3DPTEXTURECAPS_PROJECTED | D3DPTEXTURECAPS_CUBEMAP | D3DPTEXTURECAPS_VOLUMEMAP |
507 D3DPTEXTURECAPS_MIPMAP | D3DPTEXTURECAPS_MIPVOLUMEMAP | D3DPTEXTURECAPS_MIPCUBEMAP |
508 D3DPTEXTURECAPS_CUBEMAP_POW2 | D3DPTEXTURECAPS_VOLUMEMAP_POW2| D3DPTEXTURECAPS_NOPROJECTEDBUMPENV;
510 caps->MaxVertexShaderConst = min(D3D9_MAX_VERTEX_SHADER_CONSTANTF, caps->MaxVertexShaderConst);
511 caps->NumSimultaneousRTs = min(D3D_MAX_SIMULTANEOUS_RENDERTARGETS, caps->NumSimultaneousRTs);
513 if (caps->PixelShaderVersion > 3)
515 caps->PixelShaderVersion = D3DPS_VERSION(3, 0);
517 else
519 DWORD major = caps->PixelShaderVersion;
520 caps->PixelShaderVersion = D3DPS_VERSION(major, ps_minor_version[major]);
523 if (caps->VertexShaderVersion > 3)
525 caps->VertexShaderVersion = D3DVS_VERSION(3, 0);
527 else
529 DWORD major = caps->VertexShaderVersion;
530 caps->VertexShaderVersion = D3DVS_VERSION(major, vs_minor_version[major]);
533 /* Get adapter group information */
534 output_idx = adapter_ordinal;
535 wined3d_output = d3d9->wined3d_outputs[output_idx];
537 wined3d_mutex_lock();
538 hr = wined3d_output_get_desc(wined3d_output, &output_desc);
539 wined3d_mutex_unlock();
541 if (FAILED(hr))
543 ERR("Failed to get output desc, hr %#lx.\n", hr);
544 return;
547 caps->MasterAdapterOrdinal = output_idx - output_desc.ordinal;
548 caps->AdapterOrdinalInGroup = output_desc.ordinal;
549 if (!caps->AdapterOrdinalInGroup)
551 wined3d_adapter = wined3d_output_get_adapter(wined3d_output);
552 caps->NumberOfAdaptersInGroup = wined3d_adapter_get_output_count(wined3d_adapter);
554 else
556 caps->NumberOfAdaptersInGroup = 0;
560 static enum wined3d_texture_filter_type wined3d_texture_filter_type_from_d3d(D3DTEXTUREFILTERTYPE type)
562 return (enum wined3d_texture_filter_type)type;
565 static enum wined3d_transform_state wined3d_transform_state_from_d3d(D3DTRANSFORMSTATETYPE type)
567 return (enum wined3d_transform_state)type;
570 static enum wined3d_render_state wined3d_render_state_from_d3d(D3DRENDERSTATETYPE type)
572 return (enum wined3d_render_state)type;
575 static enum wined3d_sampler_state wined3d_sampler_state_from_d3d(D3DSAMPLERSTATETYPE type)
577 return (enum wined3d_sampler_state)type;
580 static enum wined3d_primitive_type wined3d_primitive_type_from_d3d(D3DPRIMITIVETYPE type)
582 return (enum wined3d_primitive_type)type;
585 static void device_reset_viewport_state(struct d3d9_device *device)
587 struct wined3d_viewport vp;
588 RECT rect;
590 wined3d_device_context_get_viewports(device->immediate_context, NULL, &vp);
591 wined3d_stateblock_set_viewport(device->state, &vp);
592 wined3d_device_context_get_scissor_rects(device->immediate_context, NULL, &rect);
593 wined3d_stateblock_set_scissor_rect(device->state, &rect);
596 static HRESULT WINAPI d3d9_device_QueryInterface(IDirect3DDevice9Ex *iface, REFIID riid, void **out)
598 TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
600 if (IsEqualGUID(riid, &IID_IDirect3DDevice9)
601 || IsEqualGUID(riid, &IID_IUnknown))
603 IDirect3DDevice9Ex_AddRef(iface);
604 *out = iface;
605 return S_OK;
608 if (IsEqualGUID(riid, &IID_IDirect3DDevice9Ex))
610 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
612 /* Find out if the creating d3d9 interface was created with Direct3DCreate9Ex.
613 * It doesn't matter with which function the device was created. */
614 if (!device->d3d_parent->extended)
616 WARN("IDirect3D9 instance wasn't created with CreateDirect3D9Ex, returning E_NOINTERFACE.\n");
617 *out = NULL;
618 return E_NOINTERFACE;
621 IDirect3DDevice9Ex_AddRef(iface);
622 *out = iface;
623 return S_OK;
626 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
628 *out = NULL;
629 return E_NOINTERFACE;
632 static ULONG WINAPI d3d9_device_AddRef(IDirect3DDevice9Ex *iface)
634 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
635 ULONG refcount = InterlockedIncrement(&device->refcount);
637 TRACE("%p increasing refcount to %lu.\n", iface, refcount);
639 return refcount;
642 static ULONG WINAPI DECLSPEC_HOTPATCH d3d9_device_Release(IDirect3DDevice9Ex *iface)
644 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
645 ULONG refcount;
647 if (device->in_destruction)
648 return 0;
650 refcount = InterlockedDecrement(&device->refcount);
652 TRACE("%p decreasing refcount to %lu.\n", iface, refcount);
654 if (!refcount)
656 unsigned i;
657 device->in_destruction = TRUE;
659 wined3d_mutex_lock();
660 for (i = 0; i < device->fvf_decl_count; ++i)
662 wined3d_vertex_declaration_decref(device->fvf_decls[i].decl);
664 heap_free(device->fvf_decls);
666 wined3d_streaming_buffer_cleanup(&device->vertex_buffer);
667 wined3d_streaming_buffer_cleanup(&device->index_buffer);
669 for (i = 0; i < device->implicit_swapchain_count; ++i)
671 wined3d_swapchain_decref(device->implicit_swapchains[i]);
673 heap_free(device->implicit_swapchains);
675 if (device->recording)
676 wined3d_stateblock_decref(device->recording);
677 wined3d_stateblock_decref(device->state);
679 wined3d_device_release_focus_window(device->wined3d_device);
680 wined3d_device_decref(device->wined3d_device);
681 wined3d_mutex_unlock();
683 IDirect3D9Ex_Release(&device->d3d_parent->IDirect3D9Ex_iface);
685 heap_free(device);
688 return refcount;
691 static HRESULT WINAPI d3d9_device_TestCooperativeLevel(IDirect3DDevice9Ex *iface)
693 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
695 TRACE("iface %p.\n", iface);
697 TRACE("device state: %#lx.\n", device->device_state);
699 if (device->d3d_parent->extended)
700 return D3D_OK;
702 switch (device->device_state)
704 default:
705 case D3D9_DEVICE_STATE_OK:
706 return D3D_OK;
707 case D3D9_DEVICE_STATE_LOST:
708 return D3DERR_DEVICELOST;
709 case D3D9_DEVICE_STATE_NOT_RESET:
710 return D3DERR_DEVICENOTRESET;
714 static UINT WINAPI d3d9_device_GetAvailableTextureMem(IDirect3DDevice9Ex *iface)
716 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
717 UINT ret;
719 TRACE("iface %p.\n", iface);
721 wined3d_mutex_lock();
722 ret = wined3d_device_get_available_texture_mem(device->wined3d_device);
723 wined3d_mutex_unlock();
725 return ret;
728 static HRESULT WINAPI d3d9_device_EvictManagedResources(IDirect3DDevice9Ex *iface)
730 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
732 TRACE("iface %p.\n", iface);
734 wined3d_mutex_lock();
735 wined3d_device_evict_managed_resources(device->wined3d_device);
736 wined3d_mutex_unlock();
738 return D3D_OK;
741 static HRESULT WINAPI d3d9_device_GetDirect3D(IDirect3DDevice9Ex *iface, IDirect3D9 **d3d9)
743 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
745 TRACE("iface %p, d3d9 %p.\n", iface, d3d9);
747 if (!d3d9)
748 return D3DERR_INVALIDCALL;
750 return IDirect3D9Ex_QueryInterface(&device->d3d_parent->IDirect3D9Ex_iface, &IID_IDirect3D9, (void **)d3d9);
753 static HRESULT WINAPI d3d9_device_GetDeviceCaps(IDirect3DDevice9Ex *iface, D3DCAPS9 *caps)
755 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
756 struct wined3d_caps wined3d_caps;
757 HRESULT hr;
759 TRACE("iface %p, caps %p.\n", iface, caps);
761 if (!caps)
762 return D3DERR_INVALIDCALL;
764 memset(caps, 0, sizeof(*caps));
766 wined3d_mutex_lock();
767 hr = wined3d_device_get_device_caps(device->wined3d_device, &wined3d_caps);
768 wined3d_mutex_unlock();
770 d3d9_caps_from_wined3dcaps(device->d3d_parent, device->adapter_ordinal, caps, &wined3d_caps);
772 return hr;
775 static HRESULT WINAPI d3d9_device_GetDisplayMode(IDirect3DDevice9Ex *iface, UINT swapchain, D3DDISPLAYMODE *mode)
777 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
778 struct wined3d_display_mode wined3d_mode;
779 HRESULT hr;
781 TRACE("iface %p, swapchain %u, mode %p.\n", iface, swapchain, mode);
783 wined3d_mutex_lock();
784 hr = wined3d_device_get_display_mode(device->wined3d_device, swapchain, &wined3d_mode, NULL);
785 wined3d_mutex_unlock();
787 if (SUCCEEDED(hr))
789 mode->Width = wined3d_mode.width;
790 mode->Height = wined3d_mode.height;
791 mode->RefreshRate = wined3d_mode.refresh_rate;
792 mode->Format = d3dformat_from_wined3dformat(wined3d_mode.format_id);
795 return hr;
798 static HRESULT WINAPI d3d9_device_GetCreationParameters(IDirect3DDevice9Ex *iface,
799 D3DDEVICE_CREATION_PARAMETERS *parameters)
801 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
803 TRACE("iface %p, parameters %p.\n", iface, parameters);
805 wined3d_mutex_lock();
806 wined3d_device_get_creation_parameters(device->wined3d_device,
807 (struct wined3d_device_creation_parameters *)parameters);
808 wined3d_mutex_unlock();
810 parameters->AdapterOrdinal = device->adapter_ordinal;
811 return D3D_OK;
814 static HRESULT WINAPI d3d9_device_SetCursorProperties(IDirect3DDevice9Ex *iface,
815 UINT hotspot_x, UINT hotspot_y, IDirect3DSurface9 *bitmap)
817 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
818 struct d3d9_surface *bitmap_impl = unsafe_impl_from_IDirect3DSurface9(bitmap);
819 D3DSURFACE_DESC surface_desc;
820 D3DDISPLAYMODE mode;
821 HRESULT hr;
823 TRACE("iface %p, hotspot_x %u, hotspot_y %u, bitmap %p.\n",
824 iface, hotspot_x, hotspot_y, bitmap);
826 if (!bitmap)
828 WARN("No cursor bitmap, returning D3DERR_INVALIDCALL.\n");
829 return D3DERR_INVALIDCALL;
832 if (FAILED(hr = IDirect3DSurface9_GetDesc(bitmap, &surface_desc)))
834 WARN("Failed to get surface description, hr %#lx.\n", hr);
835 return hr;
838 if (FAILED(hr = IDirect3D9_GetAdapterDisplayMode(&device->d3d_parent->IDirect3D9Ex_iface,
839 device->adapter_ordinal, &mode)))
841 WARN("Failed to get device display mode, hr %#lx.\n", hr);
842 return hr;
845 if (surface_desc.Width > mode.Width || surface_desc.Height > mode.Height)
847 WARN("Surface dimension %ux%u exceeds display mode %ux%u.\n", surface_desc.Width,
848 surface_desc.Height, mode.Width, mode.Height);
849 return D3DERR_INVALIDCALL;
852 wined3d_mutex_lock();
853 hr = wined3d_device_set_cursor_properties(device->wined3d_device,
854 hotspot_x, hotspot_y, bitmap_impl->wined3d_texture, bitmap_impl->sub_resource_idx);
855 wined3d_mutex_unlock();
857 return hr;
860 static void WINAPI d3d9_device_SetCursorPosition(IDirect3DDevice9Ex *iface, int x, int y, DWORD flags)
862 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
864 TRACE("iface %p, x %u, y %u, flags %#lx.\n", iface, x, y, flags);
866 wined3d_mutex_lock();
867 wined3d_device_set_cursor_position(device->wined3d_device, x, y, flags);
868 wined3d_mutex_unlock();
871 static BOOL WINAPI d3d9_device_ShowCursor(IDirect3DDevice9Ex *iface, BOOL show)
873 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
874 BOOL ret;
876 TRACE("iface %p, show %#x.\n", iface, show);
878 wined3d_mutex_lock();
879 ret = wined3d_device_show_cursor(device->wined3d_device, show);
880 wined3d_mutex_unlock();
882 return ret;
885 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_CreateAdditionalSwapChain(IDirect3DDevice9Ex *iface,
886 D3DPRESENT_PARAMETERS *present_parameters, IDirect3DSwapChain9 **swapchain)
888 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
889 struct wined3d_device_creation_parameters creation_parameters;
890 struct wined3d_swapchain_desc desc;
891 struct d3d9_swapchain *object;
892 unsigned int swap_interval;
893 unsigned int output_idx;
894 unsigned int i, count;
895 HRESULT hr;
897 TRACE("iface %p, present_parameters %p, swapchain %p.\n",
898 iface, present_parameters, swapchain);
900 if (!present_parameters->Windowed)
902 WARN("Trying to create an additional fullscreen swapchain, returning D3DERR_INVALIDCALL.\n");
903 return D3DERR_INVALIDCALL;
906 wined3d_mutex_lock();
907 count = wined3d_device_get_swapchain_count(device->wined3d_device);
908 for (i = 0; i < count; ++i)
910 struct wined3d_swapchain *wined3d_swapchain;
912 wined3d_swapchain = wined3d_device_get_swapchain(device->wined3d_device, i);
913 wined3d_swapchain_get_desc(wined3d_swapchain, &desc);
915 if (!desc.windowed)
917 wined3d_mutex_unlock();
918 WARN("Trying to create an additional swapchain in fullscreen mode, returning D3DERR_INVALIDCALL.\n");
919 return D3DERR_INVALIDCALL;
922 wined3d_mutex_unlock();
924 output_idx = device->adapter_ordinal;
925 if (!wined3d_swapchain_desc_from_d3d9(&desc, device->d3d_parent->wined3d_outputs[output_idx],
926 present_parameters, device->d3d_parent->extended))
927 return D3DERR_INVALIDCALL;
928 wined3d_device_get_creation_parameters(device->wined3d_device, &creation_parameters);
929 if (creation_parameters.flags & WINED3DCREATE_NOWINDOWCHANGES)
930 desc.flags |= WINED3D_SWAPCHAIN_NO_WINDOW_CHANGES;
931 swap_interval = wined3dswapinterval_from_d3d(present_parameters->PresentationInterval);
932 if (SUCCEEDED(hr = d3d9_swapchain_create(device, &desc, swap_interval, &object)))
933 *swapchain = (IDirect3DSwapChain9 *)&object->IDirect3DSwapChain9Ex_iface;
934 present_parameters_from_wined3d_swapchain_desc(present_parameters,
935 &desc, present_parameters->PresentationInterval);
937 return hr;
940 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_GetSwapChain(IDirect3DDevice9Ex *iface,
941 UINT swapchain_idx, IDirect3DSwapChain9 **swapchain)
943 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
944 struct d3d9_swapchain *d3d9_swapchain;
945 HRESULT hr;
947 TRACE("iface %p, swapchain_idx %u, swapchain %p.\n", iface, swapchain_idx, swapchain);
949 wined3d_mutex_lock();
950 if (swapchain_idx < device->implicit_swapchain_count)
952 d3d9_swapchain = wined3d_swapchain_get_parent(device->implicit_swapchains[swapchain_idx]);
953 *swapchain = (IDirect3DSwapChain9 *)&d3d9_swapchain->IDirect3DSwapChain9Ex_iface;
954 IDirect3DSwapChain9Ex_AddRef(*swapchain);
955 hr = D3D_OK;
957 else
959 *swapchain = NULL;
960 hr = D3DERR_INVALIDCALL;
962 wined3d_mutex_unlock();
964 return hr;
967 static UINT WINAPI d3d9_device_GetNumberOfSwapChains(IDirect3DDevice9Ex *iface)
969 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
970 UINT count;
972 TRACE("iface %p.\n", iface);
974 wined3d_mutex_lock();
975 count = wined3d_device_get_swapchain_count(device->wined3d_device);
976 wined3d_mutex_unlock();
978 return count;
981 static HRESULT CDECL reset_enum_callback(struct wined3d_resource *resource)
983 struct d3d9_vertexbuffer *vertex_buffer;
984 struct d3d9_indexbuffer *index_buffer;
985 struct wined3d_resource_desc desc;
986 IDirect3DBaseTexture9 *texture;
987 struct d3d9_surface *surface;
988 IUnknown *parent;
990 wined3d_resource_get_desc(resource, &desc);
991 if ((desc.access & WINED3D_RESOURCE_ACCESS_CPU) || (desc.usage & WINED3DUSAGE_MANAGED))
992 return D3D_OK;
994 if (desc.resource_type != WINED3D_RTYPE_TEXTURE_2D)
996 if (desc.bind_flags & WINED3D_BIND_VERTEX_BUFFER)
998 vertex_buffer = wined3d_resource_get_parent(resource);
999 if (vertex_buffer && vertex_buffer->draw_buffer)
1000 return D3D_OK;
1002 if (desc.bind_flags & WINED3D_BIND_INDEX_BUFFER)
1004 index_buffer = wined3d_resource_get_parent(resource);
1005 if (index_buffer && index_buffer->sysmem)
1006 return D3D_OK;
1009 WARN("Resource %p in pool D3DPOOL_DEFAULT blocks the Reset call.\n", resource);
1010 return D3DERR_INVALIDCALL;
1013 parent = wined3d_resource_get_parent(resource);
1014 if (parent && SUCCEEDED(IUnknown_QueryInterface(parent, &IID_IDirect3DBaseTexture9, (void **)&texture)))
1016 IDirect3DBaseTexture9_Release(texture);
1017 WARN("Texture %p (resource %p) in pool D3DPOOL_DEFAULT blocks the Reset call.\n", texture, resource);
1018 return D3DERR_INVALIDCALL;
1021 surface = wined3d_texture_get_sub_resource_parent(wined3d_texture_from_resource(resource), 0);
1022 if (!surface->resource.refcount)
1023 return D3D_OK;
1025 WARN("Surface %p in pool D3DPOOL_DEFAULT blocks the Reset call.\n", surface);
1026 return D3DERR_INVALIDCALL;
1029 static HRESULT d3d9_device_get_swapchains(struct d3d9_device *device)
1031 UINT i, new_swapchain_count = wined3d_device_get_swapchain_count(device->wined3d_device);
1033 if (!(device->implicit_swapchains = heap_alloc(new_swapchain_count * sizeof(*device->implicit_swapchains))))
1034 return E_OUTOFMEMORY;
1036 for (i = 0; i < new_swapchain_count; ++i)
1038 device->implicit_swapchains[i] = wined3d_device_get_swapchain(device->wined3d_device, i);
1040 device->implicit_swapchain_count = new_swapchain_count;
1042 return D3D_OK;
1045 static HRESULT d3d9_device_reset(struct d3d9_device *device,
1046 D3DPRESENT_PARAMETERS *present_parameters, D3DDISPLAYMODEEX *mode)
1048 BOOL extended = device->d3d_parent->extended;
1049 struct wined3d_swapchain_desc swapchain_desc;
1050 struct wined3d_display_mode wined3d_mode;
1051 struct wined3d_rendertarget_view *rtv;
1052 struct d3d9_swapchain *d3d9_swapchain;
1053 unsigned int output_idx;
1054 unsigned int i;
1055 HRESULT hr;
1057 if (!extended && device->device_state == D3D9_DEVICE_STATE_LOST)
1059 WARN("App not active, returning D3DERR_DEVICELOST.\n");
1060 return D3DERR_DEVICELOST;
1063 if (mode)
1065 wined3d_mode.width = mode->Width;
1066 wined3d_mode.height = mode->Height;
1067 wined3d_mode.refresh_rate = mode->RefreshRate;
1068 wined3d_mode.format_id = wined3dformat_from_d3dformat(mode->Format);
1069 wined3d_mode.scanline_ordering = wined3d_scanline_ordering_from_d3d(mode->ScanLineOrdering);
1072 output_idx = device->adapter_ordinal;
1073 if (!wined3d_swapchain_desc_from_d3d9(&swapchain_desc,
1074 device->d3d_parent->wined3d_outputs[output_idx], present_parameters, extended))
1075 return D3DERR_INVALIDCALL;
1076 swapchain_desc.flags |= WINED3D_SWAPCHAIN_IMPLICIT;
1078 wined3d_mutex_lock();
1080 wined3d_streaming_buffer_cleanup(&device->vertex_buffer);
1081 wined3d_streaming_buffer_cleanup(&device->index_buffer);
1083 if (!extended)
1085 if (device->recording)
1086 wined3d_stateblock_decref(device->recording);
1087 device->recording = NULL;
1088 device->update_state = device->state;
1089 wined3d_stateblock_reset(device->state);
1092 if (SUCCEEDED(hr = wined3d_device_reset(device->wined3d_device, &swapchain_desc,
1093 mode ? &wined3d_mode : NULL, reset_enum_callback, !extended)))
1095 heap_free(device->implicit_swapchains);
1097 if (!extended)
1099 device->auto_mipmaps = 0;
1100 wined3d_stateblock_set_render_state(device->state, WINED3D_RS_ZENABLE,
1101 !!swapchain_desc.enable_auto_depth_stencil);
1102 device_reset_viewport_state(device);
1105 if (FAILED(hr = d3d9_device_get_swapchains(device)))
1107 device->device_state = D3D9_DEVICE_STATE_NOT_RESET;
1109 else
1111 d3d9_swapchain = wined3d_swapchain_get_parent(device->implicit_swapchains[0]);
1112 d3d9_swapchain->swap_interval
1113 = wined3dswapinterval_from_d3d(present_parameters->PresentationInterval);
1114 wined3d_swapchain_get_desc(d3d9_swapchain->wined3d_swapchain, &swapchain_desc);
1115 present_parameters->BackBufferWidth = swapchain_desc.backbuffer_width;
1116 present_parameters->BackBufferHeight = swapchain_desc.backbuffer_height;
1117 present_parameters->BackBufferFormat = d3dformat_from_wined3dformat(swapchain_desc.backbuffer_format);
1118 present_parameters->BackBufferCount = swapchain_desc.backbuffer_count;
1120 device->device_state = D3D9_DEVICE_STATE_OK;
1122 if (extended)
1124 const struct wined3d_viewport *current = &device->stateblock_state->viewport;
1125 struct wined3d_viewport vp;
1126 RECT rect;
1128 vp.x = 0;
1129 vp.y = 0;
1130 vp.width = swapchain_desc.backbuffer_width;
1131 vp.height = swapchain_desc.backbuffer_height;
1132 vp.min_z = current->min_z;
1133 vp.max_z = current->max_z;
1134 wined3d_stateblock_set_viewport(device->state, &vp);
1136 SetRect(&rect, 0, 0, swapchain_desc.backbuffer_width, swapchain_desc.backbuffer_height);
1137 wined3d_stateblock_set_scissor_rect(device->state, &rect);
1141 rtv = wined3d_device_context_get_rendertarget_view(device->immediate_context, 0);
1142 device->render_targets[0] = wined3d_rendertarget_view_get_sub_resource_parent(rtv);
1143 for (i = 1; i < ARRAY_SIZE(device->render_targets); ++i)
1144 device->render_targets[i] = NULL;
1146 else if (!extended)
1148 device->device_state = D3D9_DEVICE_STATE_NOT_RESET;
1151 wined3d_mutex_unlock();
1153 return hr;
1156 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_Reset(IDirect3DDevice9Ex *iface,
1157 D3DPRESENT_PARAMETERS *present_parameters)
1159 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1161 TRACE("iface %p, present_parameters %p.\n", iface, present_parameters);
1163 return d3d9_device_reset(device, present_parameters, NULL);
1166 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_Present(IDirect3DDevice9Ex *iface,
1167 const RECT *src_rect, const RECT *dst_rect, HWND dst_window_override, const RGNDATA *dirty_region)
1169 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1170 struct d3d9_swapchain *swapchain;
1171 unsigned int i;
1172 HRESULT hr;
1174 TRACE("iface %p, src_rect %s, dst_rect %s, dst_window_override %p, dirty_region %p.\n",
1175 iface, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect), dst_window_override, dirty_region);
1177 if (device->device_state != D3D9_DEVICE_STATE_OK)
1178 return device->d3d_parent->extended ? S_PRESENT_OCCLUDED : D3DERR_DEVICELOST;
1180 if (dirty_region)
1181 FIXME("Ignoring dirty_region %p.\n", dirty_region);
1183 wined3d_mutex_lock();
1184 for (i = 0; i < device->implicit_swapchain_count; ++i)
1186 swapchain = wined3d_swapchain_get_parent(device->implicit_swapchains[i]);
1187 if (FAILED(hr = wined3d_swapchain_present(swapchain->wined3d_swapchain,
1188 src_rect, dst_rect, dst_window_override, swapchain->swap_interval, 0)))
1190 wined3d_mutex_unlock();
1191 return hr;
1194 wined3d_mutex_unlock();
1196 return D3D_OK;
1199 static HRESULT WINAPI d3d9_device_GetBackBuffer(IDirect3DDevice9Ex *iface, UINT swapchain,
1200 UINT backbuffer_idx, D3DBACKBUFFER_TYPE backbuffer_type, IDirect3DSurface9 **backbuffer)
1202 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1203 struct d3d9_swapchain *d3d9_swapchain;
1204 HRESULT hr;
1206 TRACE("iface %p, swapchain %u, backbuffer_idx %u, backbuffer_type %#x, backbuffer %p.\n",
1207 iface, swapchain, backbuffer_idx, backbuffer_type, backbuffer);
1209 /* backbuffer_type is ignored by native. */
1211 /* No need to check for backbuffer == NULL, Windows crashes in that case. */
1212 *backbuffer = NULL;
1214 wined3d_mutex_lock();
1215 if (swapchain >= device->implicit_swapchain_count)
1217 wined3d_mutex_unlock();
1218 WARN("Swapchain index %u is out of range, returning D3DERR_INVALIDCALL.\n", swapchain);
1219 return D3DERR_INVALIDCALL;
1222 d3d9_swapchain = wined3d_swapchain_get_parent(device->implicit_swapchains[swapchain]);
1223 hr = IDirect3DSwapChain9Ex_GetBackBuffer(&d3d9_swapchain->IDirect3DSwapChain9Ex_iface,
1224 backbuffer_idx, backbuffer_type, backbuffer);
1225 wined3d_mutex_unlock();
1227 return hr;
1230 static HRESULT WINAPI d3d9_device_GetRasterStatus(IDirect3DDevice9Ex *iface,
1231 UINT swapchain, D3DRASTER_STATUS *raster_status)
1233 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1234 HRESULT hr;
1236 TRACE("iface %p, swapchain %u, raster_status %p.\n", iface, swapchain, raster_status);
1238 wined3d_mutex_lock();
1239 hr = wined3d_device_get_raster_status(device->wined3d_device,
1240 swapchain, (struct wined3d_raster_status *)raster_status);
1241 wined3d_mutex_unlock();
1243 return hr;
1246 static HRESULT WINAPI d3d9_device_SetDialogBoxMode(IDirect3DDevice9Ex *iface, BOOL enable)
1248 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1249 HRESULT hr;
1251 TRACE("iface %p, enable %#x.\n", iface, enable);
1253 wined3d_mutex_lock();
1254 hr = wined3d_device_set_dialog_box_mode(device->wined3d_device, enable);
1255 wined3d_mutex_unlock();
1257 return hr;
1260 static void WINAPI d3d9_device_SetGammaRamp(IDirect3DDevice9Ex *iface,
1261 UINT swapchain, DWORD flags, const D3DGAMMARAMP *ramp)
1263 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1265 TRACE("iface %p, swapchain %u, flags %#lx, ramp %p.\n", iface, swapchain, flags, ramp);
1267 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
1268 wined3d_mutex_lock();
1269 wined3d_device_set_gamma_ramp(device->wined3d_device, swapchain, flags, (const struct wined3d_gamma_ramp *)ramp);
1270 wined3d_mutex_unlock();
1273 static void WINAPI d3d9_device_GetGammaRamp(IDirect3DDevice9Ex *iface, UINT swapchain, D3DGAMMARAMP *ramp)
1275 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1277 TRACE("iface %p, swapchain %u, ramp %p.\n", iface, swapchain, ramp);
1279 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
1280 wined3d_mutex_lock();
1281 wined3d_device_get_gamma_ramp(device->wined3d_device, swapchain, (struct wined3d_gamma_ramp *)ramp);
1282 wined3d_mutex_unlock();
1285 static HRESULT WINAPI d3d9_device_CreateTexture(IDirect3DDevice9Ex *iface,
1286 UINT width, UINT height, UINT levels, DWORD usage, D3DFORMAT format,
1287 D3DPOOL pool, IDirect3DTexture9 **texture, HANDLE *shared_handle)
1289 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1290 struct d3d9_texture *object;
1291 BOOL set_mem = FALSE;
1292 HRESULT hr;
1294 TRACE("iface %p, width %u, height %u, levels %u, usage %#lx, format %#x, pool %#x, texture %p, shared_handle %p.\n",
1295 iface, width, height, levels, usage, format, pool, texture, shared_handle);
1297 *texture = NULL;
1298 if (shared_handle)
1300 if (!device->d3d_parent->extended)
1302 WARN("Trying to create a shared or user memory texture on a non-ex device.\n");
1303 return E_NOTIMPL;
1306 if (pool == D3DPOOL_SYSTEMMEM)
1308 if (levels != 1)
1309 return D3DERR_INVALIDCALL;
1310 set_mem = TRUE;
1312 else
1314 if (pool != D3DPOOL_DEFAULT)
1316 WARN("Trying to create a shared texture in pool %#x.\n", pool);
1317 return D3DERR_INVALIDCALL;
1319 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1323 if (!(object = heap_alloc_zero(sizeof(*object))))
1324 return D3DERR_OUTOFVIDEOMEMORY;
1326 hr = d3d9_texture_2d_init(object, device, width, height, levels, usage, format, pool);
1327 if (FAILED(hr))
1329 WARN("Failed to initialize texture, hr %#lx.\n", hr);
1330 heap_free(object);
1331 return hr;
1334 if (set_mem)
1336 wined3d_mutex_lock();
1337 wined3d_texture_update_desc(object->wined3d_texture, 0, width, height,
1338 wined3dformat_from_d3dformat(format), WINED3D_MULTISAMPLE_NONE, 0,
1339 *shared_handle, 0);
1340 wined3d_mutex_unlock();
1343 TRACE("Created texture %p.\n", object);
1344 *texture = (IDirect3DTexture9 *)&object->IDirect3DBaseTexture9_iface;
1346 return D3D_OK;
1349 static HRESULT WINAPI d3d9_device_CreateVolumeTexture(IDirect3DDevice9Ex *iface,
1350 UINT width, UINT height, UINT depth, UINT levels, DWORD usage, D3DFORMAT format,
1351 D3DPOOL pool, IDirect3DVolumeTexture9 **texture, HANDLE *shared_handle)
1353 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1354 struct d3d9_texture *object;
1355 HRESULT hr;
1357 TRACE("iface %p, width %u, height %u, depth %u, levels %u, "
1358 "usage %#lx, format %#x, pool %#x, texture %p, shared_handle %p.\n",
1359 iface, width, height, depth, levels,
1360 usage, format, pool, texture, shared_handle);
1362 *texture = NULL;
1363 if (shared_handle)
1365 if (!device->d3d_parent->extended)
1367 WARN("Trying to create a shared volume texture on a non-ex device.\n");
1368 return E_NOTIMPL;
1371 if (pool != D3DPOOL_DEFAULT)
1373 WARN("Trying to create a shared volume texture in pool %#x.\n", pool);
1374 return D3DERR_INVALIDCALL;
1376 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1379 if (!(object = heap_alloc_zero(sizeof(*object))))
1380 return D3DERR_OUTOFVIDEOMEMORY;
1382 hr = d3d9_texture_3d_init(object, device, width, height, depth, levels, usage, format, pool);
1383 if (FAILED(hr))
1385 WARN("Failed to initialize volume texture, hr %#lx.\n", hr);
1386 heap_free(object);
1387 return hr;
1390 TRACE("Created volume texture %p.\n", object);
1391 *texture = (IDirect3DVolumeTexture9 *)&object->IDirect3DBaseTexture9_iface;
1393 return D3D_OK;
1396 static HRESULT WINAPI d3d9_device_CreateCubeTexture(IDirect3DDevice9Ex *iface,
1397 UINT edge_length, UINT levels, DWORD usage, D3DFORMAT format, D3DPOOL pool,
1398 IDirect3DCubeTexture9 **texture, HANDLE *shared_handle)
1400 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1401 struct d3d9_texture *object;
1402 HRESULT hr;
1404 TRACE("iface %p, edge_length %u, levels %u, usage %#lx, format %#x, pool %#x, texture %p, shared_handle %p.\n",
1405 iface, edge_length, levels, usage, format, pool, texture, shared_handle);
1407 *texture = NULL;
1408 if (shared_handle)
1410 if (!device->d3d_parent->extended)
1412 WARN("Trying to create a shared cube texture on a non-ex device.\n");
1413 return E_NOTIMPL;
1416 if (pool != D3DPOOL_DEFAULT)
1418 WARN("Trying to create a shared cube texture in pool %#x.\n", pool);
1419 return D3DERR_INVALIDCALL;
1421 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1424 if (!(object = heap_alloc_zero(sizeof(*object))))
1425 return D3DERR_OUTOFVIDEOMEMORY;
1427 hr = d3d9_texture_cube_init(object, device, edge_length, levels, usage, format, pool);
1428 if (FAILED(hr))
1430 WARN("Failed to initialize cube texture, hr %#lx.\n", hr);
1431 heap_free(object);
1432 return hr;
1435 TRACE("Created cube texture %p.\n", object);
1436 *texture = (IDirect3DCubeTexture9 *)&object->IDirect3DBaseTexture9_iface;
1438 return D3D_OK;
1441 static HRESULT WINAPI d3d9_device_CreateVertexBuffer(IDirect3DDevice9Ex *iface, UINT size,
1442 DWORD usage, DWORD fvf, D3DPOOL pool, IDirect3DVertexBuffer9 **buffer,
1443 HANDLE *shared_handle)
1445 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1446 struct d3d9_vertexbuffer *object;
1447 HRESULT hr;
1449 TRACE("iface %p, size %u, usage %#lx, fvf %#lx, pool %#x, buffer %p, shared_handle %p.\n",
1450 iface, size, usage, fvf, pool, buffer, shared_handle);
1452 if (shared_handle)
1454 if (!device->d3d_parent->extended)
1456 WARN("Trying to create a shared vertex buffer on a non-ex device.\n");
1457 return E_NOTIMPL;
1460 if (pool != D3DPOOL_DEFAULT)
1462 WARN("Trying to create a shared vertex buffer in pool %#x.\n", pool);
1463 return D3DERR_NOTAVAILABLE;
1465 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1468 if (!(object = heap_alloc_zero(sizeof(*object))))
1469 return D3DERR_OUTOFVIDEOMEMORY;
1471 hr = vertexbuffer_init(object, device, size, usage, fvf, pool);
1472 if (FAILED(hr))
1474 WARN("Failed to initialize vertex buffer, hr %#lx.\n", hr);
1475 heap_free(object);
1476 return hr;
1479 TRACE("Created vertex buffer %p.\n", object);
1480 *buffer = &object->IDirect3DVertexBuffer9_iface;
1482 return D3D_OK;
1485 static HRESULT WINAPI d3d9_device_CreateIndexBuffer(IDirect3DDevice9Ex *iface, UINT size,
1486 DWORD usage, D3DFORMAT format, D3DPOOL pool, IDirect3DIndexBuffer9 **buffer,
1487 HANDLE *shared_handle)
1489 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1490 struct d3d9_indexbuffer *object;
1491 HRESULT hr;
1493 TRACE("iface %p, size %u, usage %#lx, format %#x, pool %#x, buffer %p, shared_handle %p.\n",
1494 iface, size, usage, format, pool, buffer, shared_handle);
1496 if (shared_handle)
1498 if (!device->d3d_parent->extended)
1500 WARN("Trying to create a shared index buffer on a non-ex device.\n");
1501 return E_NOTIMPL;
1504 if (pool != D3DPOOL_DEFAULT)
1506 WARN("Trying to create a shared index buffer in pool %#x.\n", pool);
1507 return D3DERR_NOTAVAILABLE;
1509 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1512 if (!(object = heap_alloc_zero(sizeof(*object))))
1513 return D3DERR_OUTOFVIDEOMEMORY;
1515 hr = indexbuffer_init(object, device, size, usage, format, pool);
1516 if (FAILED(hr))
1518 WARN("Failed to initialize index buffer, hr %#lx.\n", hr);
1519 heap_free(object);
1520 return hr;
1523 TRACE("Created index buffer %p.\n", object);
1524 *buffer = &object->IDirect3DIndexBuffer9_iface;
1526 return D3D_OK;
1529 static HRESULT d3d9_device_create_surface(struct d3d9_device *device, unsigned int flags,
1530 enum wined3d_format_id format, enum wined3d_multisample_type multisample_type,
1531 unsigned int multisample_quality, unsigned int usage, unsigned int bind_flags, unsigned int access,
1532 unsigned int width, unsigned int height, void *user_mem, IDirect3DSurface9 **surface)
1534 struct wined3d_resource_desc desc;
1535 struct d3d9_surface *surface_impl;
1536 struct wined3d_texture *texture;
1537 HRESULT hr;
1539 TRACE("device %p, flags %#x, format %#x, multisample_type %#x, multisample_quality %u, "
1540 "usage %#x, bind_flags %#x, access %#x, width %u, height %u, user_mem %p, surface %p.\n",
1541 device, flags, format, multisample_type, multisample_quality, usage,
1542 bind_flags, access, width, height, user_mem, surface);
1544 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
1545 desc.format = format;
1546 desc.multisample_type = multisample_type;
1547 desc.multisample_quality = multisample_quality;
1548 desc.usage = usage;
1549 desc.bind_flags = bind_flags;
1550 desc.access = access;
1551 desc.width = width;
1552 desc.height = height;
1553 desc.depth = 1;
1554 desc.size = 0;
1556 if (is_gdi_compat_wined3dformat(desc.format))
1557 flags |= WINED3D_TEXTURE_CREATE_GET_DC;
1559 wined3d_mutex_lock();
1561 if (FAILED(hr = wined3d_texture_create(device->wined3d_device, &desc,
1562 1, 1, flags, NULL, NULL, &d3d9_null_wined3d_parent_ops, &texture)))
1564 wined3d_mutex_unlock();
1565 WARN("Failed to create texture, hr %#lx.\n", hr);
1566 if (hr == WINED3DERR_NOTAVAILABLE)
1567 hr = D3DERR_INVALIDCALL;
1568 return hr;
1571 surface_impl = wined3d_texture_get_sub_resource_parent(texture, 0);
1572 surface_impl->parent_device = &device->IDirect3DDevice9Ex_iface;
1573 *surface = &surface_impl->IDirect3DSurface9_iface;
1574 IDirect3DSurface9_AddRef(*surface);
1576 if (user_mem)
1577 wined3d_texture_update_desc(texture, 0, width, height, desc.format,
1578 multisample_type, multisample_quality, user_mem, 0);
1580 wined3d_texture_decref(texture);
1582 wined3d_mutex_unlock();
1584 return D3D_OK;
1587 BOOL is_gdi_compat_wined3dformat(enum wined3d_format_id format)
1589 switch (format)
1591 case WINED3DFMT_B8G8R8A8_UNORM:
1592 case WINED3DFMT_B8G8R8X8_UNORM:
1593 case WINED3DFMT_B5G6R5_UNORM:
1594 case WINED3DFMT_B5G5R5X1_UNORM:
1595 case WINED3DFMT_B5G5R5A1_UNORM:
1596 case WINED3DFMT_B8G8R8_UNORM:
1597 return TRUE;
1598 default:
1599 return FALSE;
1603 static HRESULT WINAPI d3d9_device_CreateRenderTarget(IDirect3DDevice9Ex *iface, UINT width, UINT height,
1604 D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
1605 BOOL lockable, IDirect3DSurface9 **surface, HANDLE *shared_handle)
1607 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1608 unsigned int access = WINED3D_RESOURCE_ACCESS_GPU;
1610 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %lu.\n"
1611 "lockable %#x, surface %p, shared_handle %p.\n",
1612 iface, width, height, format, multisample_type, multisample_quality,
1613 lockable, surface, shared_handle);
1615 *surface = NULL;
1616 if (shared_handle)
1618 if (!device->d3d_parent->extended)
1620 WARN("Trying to create a shared render target on a non-ex device.\n");
1621 return E_NOTIMPL;
1624 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1627 if (lockable)
1628 access |= WINED3D_RESOURCE_ACCESS_MAP_R | WINED3D_RESOURCE_ACCESS_MAP_W;
1630 return d3d9_device_create_surface(device, 0, wined3dformat_from_d3dformat(format),
1631 wined3d_multisample_type_from_d3d(multisample_type), multisample_quality, 0,
1632 WINED3D_BIND_RENDER_TARGET, access, width, height, NULL, surface);
1635 static HRESULT WINAPI d3d9_device_CreateDepthStencilSurface(IDirect3DDevice9Ex *iface, UINT width, UINT height,
1636 D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
1637 BOOL discard, IDirect3DSurface9 **surface, HANDLE *shared_handle)
1639 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1640 DWORD flags = 0;
1642 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %lu.\n"
1643 "discard %#x, surface %p, shared_handle %p.\n",
1644 iface, width, height, format, multisample_type, multisample_quality,
1645 discard, surface, shared_handle);
1647 *surface = NULL;
1648 if (shared_handle)
1650 if (!device->d3d_parent->extended)
1652 WARN("Trying to create a shared depth stencil on a non-ex device.\n");
1653 return E_NOTIMPL;
1656 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1659 if (discard)
1660 flags |= WINED3D_TEXTURE_CREATE_DISCARD;
1662 return d3d9_device_create_surface(device, flags, wined3dformat_from_d3dformat(format),
1663 wined3d_multisample_type_from_d3d(multisample_type), multisample_quality, 0,
1664 WINED3D_BIND_DEPTH_STENCIL, WINED3D_RESOURCE_ACCESS_GPU, width, height, NULL, surface);
1668 static HRESULT WINAPI d3d9_device_UpdateSurface(IDirect3DDevice9Ex *iface,
1669 IDirect3DSurface9 *src_surface, const RECT *src_rect,
1670 IDirect3DSurface9 *dst_surface, const POINT *dst_point)
1672 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1673 struct d3d9_surface *src = unsafe_impl_from_IDirect3DSurface9(src_surface);
1674 struct d3d9_surface *dst = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1675 struct wined3d_sub_resource_desc src_desc, dst_desc;
1676 struct wined3d_box src_box;
1677 HRESULT hr;
1679 TRACE("iface %p, src_surface %p, src_rect %s, dst_surface %p, dst_point %p.\n",
1680 iface, src_surface, wine_dbgstr_rect(src_rect), dst_surface, dst_point);
1682 wined3d_mutex_lock();
1684 wined3d_texture_get_sub_resource_desc(src->wined3d_texture, src->sub_resource_idx, &src_desc);
1685 wined3d_texture_get_sub_resource_desc(dst->wined3d_texture, dst->sub_resource_idx, &dst_desc);
1686 if (src_desc.format != dst_desc.format)
1688 wined3d_mutex_unlock();
1689 WARN("Surface formats (%#x/%#x) don't match.\n",
1690 d3dformat_from_wined3dformat(src_desc.format),
1691 d3dformat_from_wined3dformat(dst_desc.format));
1692 return D3DERR_INVALIDCALL;
1695 if (src_rect)
1696 wined3d_box_set(&src_box, src_rect->left, src_rect->top, src_rect->right, src_rect->bottom, 0, 1);
1697 else
1698 wined3d_box_set(&src_box, 0, 0, src_desc.width, src_desc.height, 0, 1);
1700 hr = wined3d_device_context_copy_sub_resource_region(device->immediate_context,
1701 wined3d_texture_get_resource(dst->wined3d_texture), dst->sub_resource_idx, dst_point ? dst_point->x : 0,
1702 dst_point ? dst_point->y : 0, 0, wined3d_texture_get_resource(src->wined3d_texture),
1703 src->sub_resource_idx, &src_box, 0);
1704 if (SUCCEEDED(hr) && dst->texture)
1705 d3d9_texture_flag_auto_gen_mipmap(dst->texture);
1707 wined3d_mutex_unlock();
1709 if (FAILED(hr))
1710 return D3DERR_INVALIDCALL;
1712 return hr;
1715 static HRESULT WINAPI d3d9_device_UpdateTexture(IDirect3DDevice9Ex *iface,
1716 IDirect3DBaseTexture9 *src_texture, IDirect3DBaseTexture9 *dst_texture)
1718 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1719 struct d3d9_texture *src_impl, *dst_impl;
1720 HRESULT hr;
1722 TRACE("iface %p, src_texture %p, dst_texture %p.\n", iface, src_texture, dst_texture);
1724 src_impl = unsafe_impl_from_IDirect3DBaseTexture9(src_texture);
1725 dst_impl = unsafe_impl_from_IDirect3DBaseTexture9(dst_texture);
1727 if (src_impl->draw_texture || dst_impl->draw_texture)
1729 WARN("Source or destination is managed; returning D3DERR_INVALIDCALL.\n");
1730 return D3DERR_INVALIDCALL;
1733 wined3d_mutex_lock();
1734 hr = wined3d_device_update_texture(device->wined3d_device,
1735 src_impl->wined3d_texture, dst_impl->wined3d_texture);
1736 if (SUCCEEDED(hr))
1737 d3d9_texture_flag_auto_gen_mipmap(dst_impl);
1738 wined3d_mutex_unlock();
1740 return hr;
1743 static HRESULT WINAPI d3d9_device_GetRenderTargetData(IDirect3DDevice9Ex *iface,
1744 IDirect3DSurface9 *render_target, IDirect3DSurface9 *dst_surface)
1746 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1747 struct d3d9_surface *rt_impl = unsafe_impl_from_IDirect3DSurface9(render_target);
1748 struct d3d9_surface *dst_impl = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1749 struct wined3d_sub_resource_desc wined3d_desc;
1750 RECT dst_rect, src_rect;
1751 HRESULT hr;
1753 TRACE("iface %p, render_target %p, dst_surface %p.\n", iface, render_target, dst_surface);
1755 if (!render_target || !dst_surface)
1756 return D3DERR_INVALIDCALL;
1758 wined3d_mutex_lock();
1759 wined3d_texture_get_sub_resource_desc(dst_impl->wined3d_texture, dst_impl->sub_resource_idx, &wined3d_desc);
1760 SetRect(&dst_rect, 0, 0, wined3d_desc.width, wined3d_desc.height);
1762 wined3d_texture_get_sub_resource_desc(rt_impl->wined3d_texture, rt_impl->sub_resource_idx, &wined3d_desc);
1763 SetRect(&src_rect, 0, 0, wined3d_desc.width, wined3d_desc.height);
1765 /* TODO: Check surface sizes, pools, etc. */
1766 if (wined3d_desc.multisample_type)
1767 hr = D3DERR_INVALIDCALL;
1768 else
1769 hr = wined3d_device_context_blt(device->immediate_context,
1770 dst_impl->wined3d_texture, dst_impl->sub_resource_idx, &dst_rect,
1771 rt_impl->wined3d_texture, rt_impl->sub_resource_idx, &src_rect, 0, NULL, WINED3D_TEXF_POINT);
1772 wined3d_mutex_unlock();
1774 return hr;
1777 static HRESULT WINAPI d3d9_device_GetFrontBufferData(IDirect3DDevice9Ex *iface,
1778 UINT swapchain, IDirect3DSurface9 *dst_surface)
1780 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1781 struct d3d9_surface *dst_impl = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1782 HRESULT hr = D3DERR_INVALIDCALL;
1784 TRACE("iface %p, swapchain %u, dst_surface %p.\n", iface, swapchain, dst_surface);
1786 wined3d_mutex_lock();
1787 if (swapchain < device->implicit_swapchain_count)
1788 hr = wined3d_swapchain_get_front_buffer_data(device->implicit_swapchains[swapchain],
1789 dst_impl->wined3d_texture, dst_impl->sub_resource_idx);
1790 wined3d_mutex_unlock();
1792 return hr;
1795 static HRESULT WINAPI d3d9_device_StretchRect(IDirect3DDevice9Ex *iface, IDirect3DSurface9 *src_surface,
1796 const RECT *src_rect, IDirect3DSurface9 *dst_surface, const RECT *dst_rect, D3DTEXTUREFILTERTYPE filter)
1798 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1799 struct d3d9_surface *src = unsafe_impl_from_IDirect3DSurface9(src_surface);
1800 struct d3d9_surface *dst = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1801 struct wined3d_sub_resource_desc src_desc, dst_desc;
1802 HRESULT hr = D3DERR_INVALIDCALL;
1803 RECT d, s;
1805 TRACE("iface %p, src_surface %p, src_rect %s, dst_surface %p, dst_rect %s, filter %#x.\n",
1806 iface, src_surface, wine_dbgstr_rect(src_rect), dst_surface, wine_dbgstr_rect(dst_rect), filter);
1808 wined3d_mutex_lock();
1809 wined3d_texture_get_sub_resource_desc(dst->wined3d_texture, dst->sub_resource_idx, &dst_desc);
1810 if (!dst_rect)
1812 SetRect(&d, 0, 0, dst_desc.width, dst_desc.height);
1813 dst_rect = &d;
1816 wined3d_texture_get_sub_resource_desc(src->wined3d_texture, src->sub_resource_idx, &src_desc);
1817 if (!src_rect)
1819 SetRect(&s, 0, 0, src_desc.width, src_desc.height);
1820 src_rect = &s;
1823 if (dst_desc.access & WINED3D_RESOURCE_ACCESS_CPU)
1825 WARN("Destination resource is not in DEFAULT pool.\n");
1826 goto done;
1828 if (src_desc.access & WINED3D_RESOURCE_ACCESS_CPU)
1830 WARN("Source resource is not in DEFAULT pool.\n");
1831 goto done;
1834 if (dst->texture && !(dst_desc.bind_flags & (WINED3D_BIND_RENDER_TARGET | WINED3D_BIND_DEPTH_STENCIL)))
1836 WARN("Destination is a regular texture.\n");
1837 goto done;
1840 if (src_desc.bind_flags & WINED3D_BIND_DEPTH_STENCIL)
1842 if (device->in_scene)
1844 WARN("Rejecting depth / stencil blit while in scene.\n");
1845 goto done;
1848 if (src_rect->left || src_rect->top || src_rect->right != src_desc.width
1849 || src_rect->bottom != src_desc.height)
1851 WARN("Rejecting depth / stencil blit with invalid source rect %s.\n",
1852 wine_dbgstr_rect(src_rect));
1853 goto done;
1856 if (dst_rect->left || dst_rect->top || dst_rect->right != dst_desc.width
1857 || dst_rect->bottom != dst_desc.height)
1859 WARN("Rejecting depth / stencil blit with invalid destination rect %s.\n",
1860 wine_dbgstr_rect(dst_rect));
1861 goto done;
1864 if (src_desc.width != dst_desc.width || src_desc.height != dst_desc.height)
1866 WARN("Rejecting depth / stencil blit with mismatched surface sizes.\n");
1867 goto done;
1871 hr = wined3d_device_context_blt(device->immediate_context, dst->wined3d_texture,
1872 dst->sub_resource_idx, dst_rect, src->wined3d_texture, src->sub_resource_idx,
1873 src_rect, 0, NULL, wined3d_texture_filter_type_from_d3d(filter));
1874 if (hr == WINEDDERR_INVALIDRECT)
1875 hr = D3DERR_INVALIDCALL;
1876 if (SUCCEEDED(hr) && dst->texture)
1877 d3d9_texture_flag_auto_gen_mipmap(dst->texture);
1879 done:
1880 wined3d_mutex_unlock();
1881 return hr;
1884 static HRESULT WINAPI d3d9_device_ColorFill(IDirect3DDevice9Ex *iface,
1885 IDirect3DSurface9 *surface, const RECT *rect, D3DCOLOR color)
1887 const struct wined3d_color c =
1889 ((color >> 16) & 0xff) / 255.0f,
1890 ((color >> 8) & 0xff) / 255.0f,
1891 (color & 0xff) / 255.0f,
1892 ((color >> 24) & 0xff) / 255.0f,
1894 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1895 struct d3d9_surface *surface_impl = unsafe_impl_from_IDirect3DSurface9(surface);
1896 struct wined3d_sub_resource_desc desc;
1897 struct wined3d_rendertarget_view *rtv;
1898 HRESULT hr;
1900 TRACE("iface %p, surface %p, rect %p, color 0x%08lx.\n", iface, surface, rect, color);
1902 if (!surface)
1903 return D3DERR_INVALIDCALL;
1905 wined3d_mutex_lock();
1907 if (FAILED(wined3d_texture_get_sub_resource_desc(surface_impl->wined3d_texture,
1908 surface_impl->sub_resource_idx, &desc)))
1910 wined3d_mutex_unlock();
1911 return D3DERR_INVALIDCALL;
1914 if (desc.access & WINED3D_RESOURCE_ACCESS_CPU)
1916 wined3d_mutex_unlock();
1917 WARN("Colour fills are not allowed on surfaces with resource access %#x.\n", desc.access);
1918 return D3DERR_INVALIDCALL;
1920 if ((desc.bind_flags & (WINED3D_BIND_RENDER_TARGET | WINED3D_BIND_SHADER_RESOURCE)) == WINED3D_BIND_SHADER_RESOURCE)
1922 wined3d_mutex_unlock();
1923 WARN("Colorfill is not allowed on non-RT textures, returning D3DERR_INVALIDCALL.\n");
1924 return D3DERR_INVALIDCALL;
1926 if (desc.bind_flags & WINED3D_BIND_DEPTH_STENCIL)
1928 wined3d_mutex_unlock();
1929 WARN("Colorfill is not allowed on depth stencil surfaces, returning D3DERR_INVALIDCALL.\n");
1930 return D3DERR_INVALIDCALL;
1933 wined3d_device_apply_stateblock(device->wined3d_device, device->state);
1934 rtv = d3d9_surface_acquire_rendertarget_view(surface_impl);
1935 hr = wined3d_device_context_clear_rendertarget_view(device->immediate_context,
1936 rtv, rect, WINED3DCLEAR_TARGET, &c, 0.0f, 0);
1937 d3d9_surface_release_rendertarget_view(surface_impl, rtv);
1938 if (SUCCEEDED(hr) && surface_impl->texture)
1939 d3d9_texture_flag_auto_gen_mipmap(surface_impl->texture);
1941 wined3d_mutex_unlock();
1943 return hr;
1946 static HRESULT WINAPI d3d9_device_CreateOffscreenPlainSurface(IDirect3DDevice9Ex *iface,
1947 UINT width, UINT height, D3DFORMAT format, D3DPOOL pool, IDirect3DSurface9 **surface,
1948 HANDLE *shared_handle)
1950 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1951 unsigned int usage, access;
1952 void *user_mem = NULL;
1954 TRACE("iface %p, width %u, height %u, format %#x, pool %#x, surface %p, shared_handle %p.\n",
1955 iface, width, height, format, pool, surface, shared_handle);
1957 *surface = NULL;
1958 if (pool == D3DPOOL_MANAGED)
1960 WARN("Attempting to create a managed offscreen plain surface.\n");
1961 return D3DERR_INVALIDCALL;
1964 if (shared_handle)
1966 if (!device->d3d_parent->extended)
1968 WARN("Trying to create a shared or user memory surface on a non-ex device.\n");
1969 return E_NOTIMPL;
1972 if (pool == D3DPOOL_SYSTEMMEM)
1973 user_mem = *shared_handle;
1974 else
1976 if (pool != D3DPOOL_DEFAULT)
1978 WARN("Trying to create a shared surface in pool %#x.\n", pool);
1979 return D3DERR_INVALIDCALL;
1981 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1985 usage = wined3d_usage_from_d3d(pool, 0);
1986 access = wined3daccess_from_d3dpool(pool, 0)
1987 | WINED3D_RESOURCE_ACCESS_MAP_R | WINED3D_RESOURCE_ACCESS_MAP_W;
1989 return d3d9_device_create_surface(device, 0, wined3dformat_from_d3dformat(format),
1990 WINED3D_MULTISAMPLE_NONE, 0, usage, 0, access, width, height, user_mem, surface);
1993 static HRESULT WINAPI d3d9_device_SetRenderTarget(IDirect3DDevice9Ex *iface, DWORD idx, IDirect3DSurface9 *surface)
1995 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1996 struct d3d9_surface *surface_impl = unsafe_impl_from_IDirect3DSurface9(surface);
1997 struct wined3d_rendertarget_view *rtv;
1998 HRESULT hr;
2000 TRACE("iface %p, idx %lu, surface %p.\n", iface, idx, surface);
2002 if (idx >= D3D_MAX_SIMULTANEOUS_RENDERTARGETS)
2004 WARN("Invalid index %lu specified.\n", idx);
2005 return D3DERR_INVALIDCALL;
2008 if (!idx && !surface_impl)
2010 WARN("Trying to set render target 0 to NULL.\n");
2011 return D3DERR_INVALIDCALL;
2014 if (surface_impl && d3d9_surface_get_device(surface_impl) != device)
2016 WARN("Render target surface does not match device.\n");
2017 return D3DERR_INVALIDCALL;
2020 wined3d_mutex_lock();
2021 rtv = surface_impl ? d3d9_surface_acquire_rendertarget_view(surface_impl) : NULL;
2022 hr = wined3d_device_context_set_rendertarget_views(device->immediate_context, idx, 1, &rtv, TRUE);
2023 d3d9_surface_release_rendertarget_view(surface_impl, rtv);
2024 if (SUCCEEDED(hr))
2026 if (!idx)
2027 device_reset_viewport_state(device);
2028 device->render_targets[idx] = surface_impl;
2030 wined3d_mutex_unlock();
2032 return hr;
2035 static HRESULT WINAPI d3d9_device_GetRenderTarget(IDirect3DDevice9Ex *iface, DWORD idx, IDirect3DSurface9 **surface)
2037 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2038 struct wined3d_rendertarget_view *wined3d_rtv;
2039 struct d3d9_surface *surface_impl;
2040 HRESULT hr = D3D_OK;
2042 TRACE("iface %p, idx %lu, surface %p.\n", iface, idx, surface);
2044 if (!surface)
2045 return D3DERR_INVALIDCALL;
2047 if (idx >= D3D_MAX_SIMULTANEOUS_RENDERTARGETS)
2049 WARN("Invalid index %lu specified.\n", idx);
2050 return D3DERR_INVALIDCALL;
2053 wined3d_mutex_lock();
2054 if ((wined3d_rtv = wined3d_device_context_get_rendertarget_view(device->immediate_context, idx)))
2056 /* We want the sub resource parent here, since the view itself may be
2057 * internal to wined3d and may not have a parent. */
2058 surface_impl = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_rtv);
2059 *surface = &surface_impl->IDirect3DSurface9_iface;
2060 IDirect3DSurface9_AddRef(*surface);
2062 else
2064 hr = D3DERR_NOTFOUND;
2065 *surface = NULL;
2067 wined3d_mutex_unlock();
2069 return hr;
2072 static HRESULT WINAPI d3d9_device_SetDepthStencilSurface(IDirect3DDevice9Ex *iface, IDirect3DSurface9 *depth_stencil)
2074 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2075 struct d3d9_surface *ds_impl = unsafe_impl_from_IDirect3DSurface9(depth_stencil);
2076 struct wined3d_rendertarget_view *rtv;
2077 HRESULT hr;
2079 TRACE("iface %p, depth_stencil %p.\n", iface, depth_stencil);
2081 wined3d_mutex_lock();
2082 rtv = ds_impl ? d3d9_surface_acquire_rendertarget_view(ds_impl) : NULL;
2083 hr = wined3d_device_context_set_depth_stencil_view(device->immediate_context, rtv);
2084 d3d9_surface_release_rendertarget_view(ds_impl, rtv);
2085 wined3d_mutex_unlock();
2087 return hr;
2090 static HRESULT WINAPI d3d9_device_GetDepthStencilSurface(IDirect3DDevice9Ex *iface, IDirect3DSurface9 **depth_stencil)
2092 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2093 struct wined3d_rendertarget_view *wined3d_dsv;
2094 struct d3d9_surface *surface_impl;
2095 HRESULT hr = D3D_OK;
2097 TRACE("iface %p, depth_stencil %p.\n", iface, depth_stencil);
2099 if (!depth_stencil)
2100 return D3DERR_INVALIDCALL;
2102 wined3d_mutex_lock();
2103 if ((wined3d_dsv = wined3d_device_context_get_depth_stencil_view(device->immediate_context)))
2105 /* We want the sub resource parent here, since the view itself may be
2106 * internal to wined3d and may not have a parent. */
2107 surface_impl = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_dsv);
2108 *depth_stencil = &surface_impl->IDirect3DSurface9_iface;
2109 IDirect3DSurface9_AddRef(*depth_stencil);
2111 else
2113 hr = D3DERR_NOTFOUND;
2114 *depth_stencil = NULL;
2116 wined3d_mutex_unlock();
2118 return hr;
2121 static HRESULT WINAPI d3d9_device_BeginScene(IDirect3DDevice9Ex *iface)
2123 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2124 HRESULT hr;
2126 TRACE("iface %p.\n", iface);
2128 wined3d_mutex_lock();
2129 if (SUCCEEDED(hr = wined3d_device_begin_scene(device->wined3d_device)))
2130 device->in_scene = TRUE;
2131 wined3d_mutex_unlock();
2133 return hr;
2136 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_EndScene(IDirect3DDevice9Ex *iface)
2138 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2139 HRESULT hr;
2141 TRACE("iface %p.\n", iface);
2143 wined3d_mutex_lock();
2144 if (SUCCEEDED(hr = wined3d_device_end_scene(device->wined3d_device)))
2145 device->in_scene = FALSE;
2146 wined3d_mutex_unlock();
2148 return hr;
2151 static void d3d9_rts_flag_auto_gen_mipmap(struct d3d9_device *device)
2153 unsigned int i;
2155 for (i = 0; i < ARRAY_SIZE(device->render_targets); ++i)
2157 struct d3d9_surface *surface = device->render_targets[i];
2159 if (surface && surface->texture)
2160 d3d9_texture_flag_auto_gen_mipmap(surface->texture);
2164 static HRESULT WINAPI d3d9_device_Clear(IDirect3DDevice9Ex *iface, DWORD rect_count,
2165 const D3DRECT *rects, DWORD flags, D3DCOLOR color, float z, DWORD stencil)
2167 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2168 struct wined3d_color c;
2169 HRESULT hr;
2171 TRACE("iface %p, rect_count %lu, rects %p, flags %#lx, color 0x%08lx, z %.8e, stencil %lu.\n",
2172 iface, rect_count, rects, flags, color, z, stencil);
2174 if (rect_count && !rects)
2176 WARN("count %lu with NULL rects.\n", rect_count);
2177 rect_count = 0;
2180 wined3d_color_from_d3dcolor(&c, color);
2181 wined3d_mutex_lock();
2182 wined3d_device_apply_stateblock(device->wined3d_device, device->state);
2183 hr = wined3d_device_clear(device->wined3d_device, rect_count, (const RECT *)rects, flags, &c, z, stencil);
2184 if (SUCCEEDED(hr))
2185 d3d9_rts_flag_auto_gen_mipmap(device);
2186 wined3d_mutex_unlock();
2188 return hr;
2191 static HRESULT WINAPI d3d9_device_SetTransform(IDirect3DDevice9Ex *iface,
2192 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
2194 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2196 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
2198 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
2199 wined3d_mutex_lock();
2200 wined3d_stateblock_set_transform(device->update_state,
2201 wined3d_transform_state_from_d3d(state), (const struct wined3d_matrix *)matrix);
2202 wined3d_mutex_unlock();
2204 return D3D_OK;
2207 static HRESULT WINAPI d3d9_device_GetTransform(IDirect3DDevice9Ex *iface,
2208 D3DTRANSFORMSTATETYPE state, D3DMATRIX *matrix)
2210 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2212 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
2214 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
2215 wined3d_mutex_lock();
2216 memcpy(matrix, &device->stateblock_state->transforms[state], sizeof(*matrix));
2217 wined3d_mutex_unlock();
2219 return D3D_OK;
2222 static HRESULT WINAPI d3d9_device_MultiplyTransform(IDirect3DDevice9Ex *iface,
2223 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
2225 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2227 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
2229 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
2230 wined3d_mutex_lock();
2231 wined3d_stateblock_multiply_transform(device->state,
2232 wined3d_transform_state_from_d3d(state), (const struct wined3d_matrix *)matrix);
2233 wined3d_mutex_unlock();
2235 return D3D_OK;
2238 static HRESULT WINAPI d3d9_device_SetViewport(IDirect3DDevice9Ex *iface, const D3DVIEWPORT9 *viewport)
2240 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2241 struct wined3d_viewport vp;
2243 TRACE("iface %p, viewport %p.\n", iface, viewport);
2245 vp.x = viewport->X;
2246 vp.y = viewport->Y;
2247 vp.width = viewport->Width;
2248 vp.height = viewport->Height;
2249 vp.min_z = viewport->MinZ;
2250 vp.max_z = viewport->MaxZ;
2252 wined3d_mutex_lock();
2253 wined3d_stateblock_set_viewport(device->update_state, &vp);
2254 wined3d_mutex_unlock();
2256 return D3D_OK;
2259 static HRESULT WINAPI d3d9_device_GetViewport(IDirect3DDevice9Ex *iface, D3DVIEWPORT9 *viewport)
2261 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2262 struct wined3d_viewport wined3d_viewport;
2264 TRACE("iface %p, viewport %p.\n", iface, viewport);
2266 wined3d_mutex_lock();
2267 wined3d_viewport = device->stateblock_state->viewport;
2268 wined3d_mutex_unlock();
2270 viewport->X = wined3d_viewport.x;
2271 viewport->Y = wined3d_viewport.y;
2272 viewport->Width = wined3d_viewport.width;
2273 viewport->Height = wined3d_viewport.height;
2274 viewport->MinZ = wined3d_viewport.min_z;
2275 viewport->MaxZ = wined3d_viewport.max_z;
2277 return D3D_OK;
2280 static HRESULT WINAPI d3d9_device_SetMaterial(IDirect3DDevice9Ex *iface, const D3DMATERIAL9 *material)
2282 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2284 TRACE("iface %p, material %p.\n", iface, material);
2286 /* Note: D3DMATERIAL9 is compatible with struct wined3d_material. */
2287 wined3d_mutex_lock();
2288 wined3d_stateblock_set_material(device->update_state, (const struct wined3d_material *)material);
2289 wined3d_mutex_unlock();
2291 return D3D_OK;
2294 static HRESULT WINAPI d3d9_device_GetMaterial(IDirect3DDevice9Ex *iface, D3DMATERIAL9 *material)
2296 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2298 TRACE("iface %p, material %p.\n", iface, material);
2300 /* Note: D3DMATERIAL9 is compatible with struct wined3d_material. */
2301 wined3d_mutex_lock();
2302 memcpy(material, &device->stateblock_state->material, sizeof(*material));
2303 wined3d_mutex_unlock();
2305 return D3D_OK;
2308 static HRESULT WINAPI d3d9_device_SetLight(IDirect3DDevice9Ex *iface, DWORD index, const D3DLIGHT9 *light)
2310 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2311 HRESULT hr;
2313 TRACE("iface %p, index %lu, light %p.\n", iface, index, light);
2315 /* Note: D3DLIGHT9 is compatible with struct wined3d_light. */
2316 wined3d_mutex_lock();
2317 hr = wined3d_stateblock_set_light(device->update_state, index, (const struct wined3d_light *)light);
2318 wined3d_mutex_unlock();
2320 return hr;
2323 static HRESULT WINAPI d3d9_device_GetLight(IDirect3DDevice9Ex *iface, DWORD index, D3DLIGHT9 *light)
2325 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2326 BOOL enabled;
2327 HRESULT hr;
2329 TRACE("iface %p, index %lu, light %p.\n", iface, index, light);
2331 /* Note: D3DLIGHT9 is compatible with struct wined3d_light. */
2332 wined3d_mutex_lock();
2333 hr = wined3d_stateblock_get_light(device->state, index, (struct wined3d_light *)light, &enabled);
2334 wined3d_mutex_unlock();
2336 return hr;
2339 static HRESULT WINAPI d3d9_device_LightEnable(IDirect3DDevice9Ex *iface, DWORD index, BOOL enable)
2341 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2342 HRESULT hr;
2344 TRACE("iface %p, index %lu, enable %#x.\n", iface, index, enable);
2346 wined3d_mutex_lock();
2347 hr = wined3d_stateblock_set_light_enable(device->update_state, index, enable);
2348 wined3d_mutex_unlock();
2350 return hr;
2353 static HRESULT WINAPI d3d9_device_GetLightEnable(IDirect3DDevice9Ex *iface, DWORD index, BOOL *enabled)
2355 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2356 struct wined3d_light light;
2357 HRESULT hr;
2359 TRACE("iface %p, index %lu, enabled %p.\n", iface, index, enabled);
2361 wined3d_mutex_lock();
2362 hr = wined3d_stateblock_get_light(device->state, index, &light, enabled);
2363 wined3d_mutex_unlock();
2365 return hr;
2368 static HRESULT WINAPI d3d9_device_SetClipPlane(IDirect3DDevice9Ex *iface, DWORD index, const float *plane)
2370 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2371 HRESULT hr;
2373 TRACE("iface %p, index %lu, plane %p.\n", iface, index, plane);
2375 index = min(index, device->max_user_clip_planes - 1);
2377 wined3d_mutex_lock();
2378 hr = wined3d_stateblock_set_clip_plane(device->update_state, index, (const struct wined3d_vec4 *)plane);
2379 wined3d_mutex_unlock();
2381 return hr;
2384 static HRESULT WINAPI d3d9_device_GetClipPlane(IDirect3DDevice9Ex *iface, DWORD index, float *plane)
2386 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2388 TRACE("iface %p, index %lu, plane %p.\n", iface, index, plane);
2390 index = min(index, device->max_user_clip_planes - 1);
2392 wined3d_mutex_lock();
2393 memcpy(plane, &device->stateblock_state->clip_planes[index], sizeof(struct wined3d_vec4));
2394 wined3d_mutex_unlock();
2396 return D3D_OK;
2399 static void resolve_depth_buffer(struct d3d9_device *device)
2401 const struct wined3d_stateblock_state *state = device->stateblock_state;
2402 struct wined3d_rendertarget_view *wined3d_dsv;
2403 struct wined3d_resource *dst_resource;
2404 struct wined3d_texture *dst_texture;
2405 struct wined3d_resource_desc desc;
2406 struct d3d9_surface *d3d9_dsv;
2408 if (!(dst_texture = state->textures[0]))
2409 return;
2410 dst_resource = wined3d_texture_get_resource(dst_texture);
2411 wined3d_resource_get_desc(dst_resource, &desc);
2412 if (desc.format != WINED3DFMT_D24_UNORM_S8_UINT
2413 && desc.format != WINED3DFMT_X8D24_UNORM
2414 && desc.format != WINED3DFMT_DF16
2415 && desc.format != WINED3DFMT_DF24
2416 && desc.format != WINED3DFMT_INTZ)
2417 return;
2419 if (!(wined3d_dsv = wined3d_device_context_get_depth_stencil_view(device->immediate_context)))
2420 return;
2421 d3d9_dsv = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_dsv);
2423 wined3d_device_context_resolve_sub_resource(device->immediate_context, dst_resource, 0,
2424 wined3d_rendertarget_view_get_resource(wined3d_dsv), d3d9_dsv->sub_resource_idx, desc.format);
2427 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_SetRenderState(IDirect3DDevice9Ex *iface,
2428 D3DRENDERSTATETYPE state, DWORD value)
2430 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2432 TRACE("iface %p, state %#x, value %#lx.\n", iface, state, value);
2434 wined3d_mutex_lock();
2435 wined3d_stateblock_set_render_state(device->update_state, wined3d_render_state_from_d3d(state), value);
2436 if (state == D3DRS_POINTSIZE && value == D3D9_RESZ_CODE)
2437 resolve_depth_buffer(device);
2438 wined3d_mutex_unlock();
2440 return D3D_OK;
2443 static HRESULT WINAPI d3d9_device_GetRenderState(IDirect3DDevice9Ex *iface,
2444 D3DRENDERSTATETYPE state, DWORD *value)
2446 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2447 const struct wined3d_stateblock_state *device_state;
2449 TRACE("iface %p, state %#x, value %p.\n", iface, state, value);
2451 wined3d_mutex_lock();
2452 device_state = device->stateblock_state;
2453 *value = device_state->rs[state];
2454 wined3d_mutex_unlock();
2456 return D3D_OK;
2459 static HRESULT WINAPI d3d9_device_CreateStateBlock(IDirect3DDevice9Ex *iface,
2460 D3DSTATEBLOCKTYPE type, IDirect3DStateBlock9 **stateblock)
2462 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2463 struct d3d9_stateblock *object;
2464 HRESULT hr;
2466 TRACE("iface %p, type %#x, stateblock %p.\n", iface, type, stateblock);
2468 if (type != D3DSBT_ALL && type != D3DSBT_PIXELSTATE && type != D3DSBT_VERTEXSTATE)
2470 WARN("Unexpected stateblock type, returning D3DERR_INVALIDCALL.\n");
2471 return D3DERR_INVALIDCALL;
2474 wined3d_mutex_lock();
2475 if (device->recording)
2477 wined3d_mutex_unlock();
2478 WARN("Trying to create a stateblock while recording, returning D3DERR_INVALIDCALL.\n");
2479 return D3DERR_INVALIDCALL;
2481 wined3d_mutex_unlock();
2483 if (!(object = heap_alloc_zero(sizeof(*object))))
2484 return E_OUTOFMEMORY;
2486 hr = stateblock_init(object, device, type, NULL);
2487 if (FAILED(hr))
2489 WARN("Failed to initialize stateblock, hr %#lx.\n", hr);
2490 heap_free(object);
2491 return hr;
2494 TRACE("Created stateblock %p.\n", object);
2495 *stateblock = &object->IDirect3DStateBlock9_iface;
2497 return D3D_OK;
2500 static HRESULT WINAPI d3d9_device_BeginStateBlock(IDirect3DDevice9Ex *iface)
2502 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2503 struct wined3d_stateblock *stateblock;
2504 HRESULT hr;
2506 TRACE("iface %p.\n", iface);
2508 wined3d_mutex_lock();
2509 if (device->recording)
2511 wined3d_mutex_unlock();
2512 WARN("Trying to begin a stateblock while recording, returning D3DERR_INVALIDCALL.\n");
2513 return D3DERR_INVALIDCALL;
2516 if (SUCCEEDED(hr = wined3d_stateblock_create(device->wined3d_device, device->state, WINED3D_SBT_RECORDED, &stateblock)))
2517 device->update_state = device->recording = stateblock;
2518 wined3d_mutex_unlock();
2520 return hr;
2523 static HRESULT WINAPI d3d9_device_EndStateBlock(IDirect3DDevice9Ex *iface, IDirect3DStateBlock9 **stateblock)
2525 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2526 struct wined3d_stateblock *wined3d_stateblock;
2527 struct d3d9_stateblock *object;
2528 HRESULT hr;
2530 TRACE("iface %p, stateblock %p.\n", iface, stateblock);
2532 wined3d_mutex_lock();
2533 if (!device->recording)
2535 wined3d_mutex_unlock();
2536 WARN("Trying to end a stateblock, but no stateblock is being recorded.\n");
2537 return D3DERR_INVALIDCALL;
2539 wined3d_stateblock = device->recording;
2540 wined3d_stateblock_init_contained_states(wined3d_stateblock);
2541 device->recording = NULL;
2542 device->update_state = device->state;
2543 wined3d_mutex_unlock();
2545 if (!(object = heap_alloc_zero(sizeof(*object))))
2547 wined3d_stateblock_decref(wined3d_stateblock);
2548 return E_OUTOFMEMORY;
2551 hr = stateblock_init(object, device, 0, wined3d_stateblock);
2552 if (FAILED(hr))
2554 WARN("Failed to initialize stateblock, hr %#lx.\n", hr);
2555 wined3d_stateblock_decref(wined3d_stateblock);
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 %lu, 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 %lu.\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 %lu, 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 ? d3d9_texture_get_draw_texture(texture_impl) : 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 %lu, 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 %lu, state %#x, value %#lx.\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 %lu, 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 %lu.\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 %lu, state %#x, value %#lx.\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_context_copy_sub_resource_region(device->immediate_context,
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 HRESULT 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 unsigned int src_offset, idx_size, buffer_size, pos;
2981 struct wined3d_resource_desc resource_desc;
2982 struct wined3d_resource *index_buffer;
2983 struct wined3d_map_desc map_desc;
2984 struct wined3d_box box;
2985 HRESULT hr;
2987 if (!device->sysmem_ib)
2988 return S_OK;
2990 index_buffer = wined3d_buffer_get_resource(state->index_buffer);
2991 wined3d_resource_get_desc(index_buffer, &resource_desc);
2992 idx_size = (state->index_format == WINED3DFMT_R16_UINT) ? 2 : 4;
2994 src_offset = (*start_idx) * idx_size;
2995 buffer_size = min(idx_count * idx_size, resource_desc.size - src_offset);
2997 wined3d_box_set(&box, src_offset, 0, buffer_size, 1, 0, 1);
2998 if (FAILED(hr = wined3d_resource_map(index_buffer, 0, &map_desc, &box, WINED3D_MAP_READ)))
3000 ERR("Failed to map index buffer, hr %#lx.\n", hr);
3001 return hr;
3003 wined3d_streaming_buffer_upload(device->wined3d_device, &device->index_buffer,
3004 map_desc.data, buffer_size, idx_size, &pos);
3005 wined3d_resource_unmap(index_buffer, 0);
3007 wined3d_device_context_set_index_buffer(device->immediate_context,
3008 device->index_buffer.buffer, state->index_format, pos);
3009 *start_idx = 0;
3010 return S_OK;
3013 static void d3d9_device_upload_managed_textures(struct d3d9_device *device)
3015 const struct wined3d_stateblock_state *state = device->stateblock_state;
3016 unsigned int i;
3018 for (i = 0; i < WINED3D_MAX_COMBINED_SAMPLERS; ++i)
3020 struct wined3d_texture *wined3d_texture = state->textures[i];
3021 struct d3d9_texture *d3d9_texture;
3023 if (!wined3d_texture)
3024 continue;
3025 d3d9_texture = wined3d_texture_get_parent(wined3d_texture);
3026 if (d3d9_texture->draw_texture)
3027 wined3d_device_update_texture(device->wined3d_device,
3028 d3d9_texture->wined3d_texture, d3d9_texture->draw_texture);
3032 static HRESULT WINAPI d3d9_device_DrawPrimitive(IDirect3DDevice9Ex *iface,
3033 D3DPRIMITIVETYPE primitive_type, UINT start_vertex, UINT primitive_count)
3035 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3036 unsigned int vertex_count;
3038 TRACE("iface %p, primitive_type %#x, start_vertex %u, primitive_count %u.\n",
3039 iface, primitive_type, start_vertex, primitive_count);
3041 wined3d_mutex_lock();
3042 if (!device->stateblock_state->vertex_declaration)
3044 wined3d_mutex_unlock();
3045 WARN("Called without a valid vertex declaration set.\n");
3046 return D3DERR_INVALIDCALL;
3048 wined3d_device_apply_stateblock(device->wined3d_device, device->state);
3049 vertex_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
3050 d3d9_device_upload_managed_textures(device);
3051 d3d9_device_upload_sysmem_vertex_buffers(device, 0, start_vertex, vertex_count);
3052 d3d9_generate_auto_mipmaps(device);
3053 wined3d_device_context_set_primitive_type(device->immediate_context,
3054 wined3d_primitive_type_from_d3d(primitive_type), 0);
3055 wined3d_device_context_draw(device->immediate_context, start_vertex, vertex_count, 0, 0);
3056 d3d9_rts_flag_auto_gen_mipmap(device);
3057 wined3d_mutex_unlock();
3059 return D3D_OK;
3062 static HRESULT WINAPI d3d9_device_DrawIndexedPrimitive(IDirect3DDevice9Ex *iface,
3063 D3DPRIMITIVETYPE primitive_type, INT base_vertex_idx, UINT min_vertex_idx,
3064 UINT vertex_count, UINT start_idx, UINT primitive_count)
3066 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3067 unsigned int index_count;
3069 TRACE("iface %p, primitive_type %#x, base_vertex_idx %u, min_vertex_idx %u, "
3070 "vertex_count %u, start_idx %u, primitive_count %u.\n",
3071 iface, primitive_type, base_vertex_idx, min_vertex_idx,
3072 vertex_count, start_idx, primitive_count);
3074 wined3d_mutex_lock();
3075 if (!device->stateblock_state->vertex_declaration)
3077 wined3d_mutex_unlock();
3078 WARN("Called without a valid vertex declaration set.\n");
3079 return D3DERR_INVALIDCALL;
3081 if (!device->stateblock_state->index_buffer)
3083 wined3d_mutex_unlock();
3084 WARN("Called without a valid index buffer set.\n");
3085 return D3DERR_INVALIDCALL;
3087 index_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
3088 d3d9_device_upload_managed_textures(device);
3089 d3d9_device_upload_sysmem_vertex_buffers(device, base_vertex_idx, min_vertex_idx, vertex_count);
3090 d3d9_generate_auto_mipmaps(device);
3091 wined3d_device_context_set_primitive_type(device->immediate_context,
3092 wined3d_primitive_type_from_d3d(primitive_type), 0);
3093 wined3d_device_apply_stateblock(device->wined3d_device, device->state);
3094 d3d9_device_upload_sysmem_index_buffer(device, &start_idx, index_count);
3095 wined3d_device_context_draw_indexed(device->immediate_context, base_vertex_idx, start_idx, index_count, 0, 0);
3096 d3d9_rts_flag_auto_gen_mipmap(device);
3097 wined3d_mutex_unlock();
3099 return D3D_OK;
3102 static HRESULT WINAPI d3d9_device_DrawPrimitiveUP(IDirect3DDevice9Ex *iface,
3103 D3DPRIMITIVETYPE primitive_type, UINT primitive_count, const void *data, UINT stride)
3105 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3106 UINT vtx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
3107 UINT size = vtx_count * stride;
3108 unsigned int vb_pos;
3109 HRESULT hr;
3111 TRACE("iface %p, primitive_type %#x, primitive_count %u, data %p, stride %u.\n",
3112 iface, primitive_type, primitive_count, data, stride);
3114 if (!stride)
3116 WARN("stride is 0, returning D3DERR_INVALIDCALL.\n");
3117 return D3DERR_INVALIDCALL;
3119 if (!primitive_count)
3121 WARN("primitive_count is 0, returning D3D_OK.\n");
3122 return D3D_OK;
3125 wined3d_mutex_lock();
3127 if (!device->stateblock_state->vertex_declaration)
3129 wined3d_mutex_unlock();
3130 WARN("Called without a valid vertex declaration set.\n");
3131 return D3DERR_INVALIDCALL;
3134 if (FAILED(hr = wined3d_streaming_buffer_upload(device->wined3d_device,
3135 &device->vertex_buffer, data, size, stride, &vb_pos)))
3136 goto done;
3138 hr = wined3d_stateblock_set_stream_source(device->state, 0, device->vertex_buffer.buffer, 0, stride);
3139 if (FAILED(hr))
3140 goto done;
3142 d3d9_generate_auto_mipmaps(device);
3143 d3d9_device_upload_managed_textures(device);
3144 wined3d_device_context_set_primitive_type(device->immediate_context,
3145 wined3d_primitive_type_from_d3d(primitive_type), 0);
3146 wined3d_device_apply_stateblock(device->wined3d_device, device->state);
3147 wined3d_device_context_draw(device->immediate_context, vb_pos / stride, vtx_count, 0, 0);
3148 wined3d_stateblock_set_stream_source(device->state, 0, NULL, 0, 0);
3149 d3d9_rts_flag_auto_gen_mipmap(device);
3151 done:
3152 wined3d_mutex_unlock();
3153 return hr;
3156 static HRESULT WINAPI d3d9_device_DrawIndexedPrimitiveUP(IDirect3DDevice9Ex *iface,
3157 D3DPRIMITIVETYPE primitive_type, UINT min_vertex_idx, UINT vertex_count,
3158 UINT primitive_count, const void *index_data, D3DFORMAT index_format,
3159 const void *vertex_data, UINT vertex_stride)
3161 UINT idx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
3162 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3163 UINT idx_fmt_size = index_format == D3DFMT_INDEX16 ? 2 : 4;
3164 UINT vtx_size = vertex_count * vertex_stride;
3165 UINT idx_size = idx_count * idx_fmt_size;
3166 UINT vb_pos, ib_pos;
3167 HRESULT hr;
3169 TRACE("iface %p, primitive_type %#x, min_vertex_idx %u, vertex_count %u, primitive_count %u, "
3170 "index_data %p, index_format %#x, vertex_data %p, vertex_stride %u.\n",
3171 iface, primitive_type, min_vertex_idx, vertex_count, primitive_count,
3172 index_data, index_format, vertex_data, vertex_stride);
3174 if (!vertex_stride)
3176 WARN("vertex_stride is 0, returning D3DERR_INVALIDCALL.\n");
3177 return D3DERR_INVALIDCALL;
3179 if (!primitive_count)
3181 WARN("primitive_count is 0, returning D3D_OK.\n");
3182 return D3D_OK;
3185 wined3d_mutex_lock();
3187 if (!device->stateblock_state->vertex_declaration)
3189 wined3d_mutex_unlock();
3190 WARN("Called without a valid vertex declaration set.\n");
3191 return D3DERR_INVALIDCALL;
3194 if (FAILED(hr = wined3d_streaming_buffer_upload(device->wined3d_device, &device->vertex_buffer,
3195 (char *)vertex_data + min_vertex_idx * vertex_stride, vtx_size, vertex_stride, &vb_pos)))
3196 goto done;
3198 if (FAILED(hr = wined3d_streaming_buffer_upload(device->wined3d_device, &device->index_buffer,
3199 index_data, idx_size, idx_fmt_size, &ib_pos)))
3200 goto done;
3202 hr = wined3d_stateblock_set_stream_source(device->state, 0, device->vertex_buffer.buffer, 0, vertex_stride);
3203 if (FAILED(hr))
3204 goto done;
3205 wined3d_stateblock_set_index_buffer(device->state, device->index_buffer.buffer,
3206 wined3dformat_from_d3dformat(index_format));
3208 d3d9_generate_auto_mipmaps(device);
3209 d3d9_device_upload_managed_textures(device);
3210 wined3d_device_apply_stateblock(device->wined3d_device, device->state);
3211 wined3d_device_context_set_primitive_type(device->immediate_context,
3212 wined3d_primitive_type_from_d3d(primitive_type), 0);
3213 wined3d_device_context_draw_indexed(device->immediate_context,
3214 vb_pos / vertex_stride - min_vertex_idx, ib_pos / idx_fmt_size, idx_count, 0, 0);
3216 wined3d_stateblock_set_stream_source(device->state, 0, NULL, 0, 0);
3217 wined3d_stateblock_set_index_buffer(device->state, NULL, WINED3DFMT_UNKNOWN);
3219 d3d9_rts_flag_auto_gen_mipmap(device);
3221 done:
3222 wined3d_mutex_unlock();
3223 return hr;
3226 static HRESULT WINAPI d3d9_device_ProcessVertices(IDirect3DDevice9Ex *iface,
3227 UINT src_start_idx, UINT dst_idx, UINT vertex_count, IDirect3DVertexBuffer9 *dst_buffer,
3228 IDirect3DVertexDeclaration9 *declaration, DWORD flags)
3230 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3231 struct d3d9_vertexbuffer *dst_impl = unsafe_impl_from_IDirect3DVertexBuffer9(dst_buffer);
3232 struct d3d9_vertex_declaration *decl_impl = unsafe_impl_from_IDirect3DVertexDeclaration9(declaration);
3233 const struct wined3d_stateblock_state *state;
3234 const struct wined3d_stream_state *stream;
3235 struct d3d9_vertexbuffer *d3d9_buffer;
3236 unsigned int i, map;
3237 HRESULT hr;
3239 TRACE("iface %p, src_start_idx %u, dst_idx %u, vertex_count %u, dst_buffer %p, declaration %p, flags %#lx.\n",
3240 iface, src_start_idx, dst_idx, vertex_count, dst_buffer, declaration, flags);
3242 wined3d_mutex_lock();
3243 state = device->stateblock_state;
3245 /* Note that an alternative approach would be to simply create these
3246 * buffers with WINED3D_RESOURCE_ACCESS_MAP_R and update them here like we
3247 * do for draws. In some regards that would be easier, but it seems less
3248 * than optimal to upload data to the GPU only to subsequently download it
3249 * again. */
3250 map = device->sysmem_vb;
3251 while (map)
3253 i = wined3d_bit_scan(&map);
3254 stream = &state->streams[i];
3256 d3d9_buffer = wined3d_buffer_get_parent(stream->buffer);
3257 if (FAILED(wined3d_stateblock_set_stream_source(device->state,
3258 i, d3d9_buffer->wined3d_buffer, stream->offset, stream->stride)))
3259 ERR("Failed to set stream source.\n");
3262 wined3d_device_apply_stateblock(device->wined3d_device, device->state);
3263 hr = wined3d_device_process_vertices(device->wined3d_device, src_start_idx, dst_idx, vertex_count,
3264 dst_impl->wined3d_buffer, decl_impl ? decl_impl->wined3d_declaration : NULL,
3265 flags, dst_impl->fvf);
3267 map = device->sysmem_vb;
3268 while (map)
3270 i = wined3d_bit_scan(&map);
3271 stream = &state->streams[i];
3273 d3d9_buffer = wined3d_buffer_get_parent(stream->buffer);
3274 if (FAILED(wined3d_stateblock_set_stream_source(device->state,
3275 i, d3d9_buffer->draw_buffer, stream->offset, stream->stride)))
3276 ERR("Failed to set stream source.\n");
3279 wined3d_mutex_unlock();
3281 return hr;
3284 static HRESULT WINAPI d3d9_device_CreateVertexDeclaration(IDirect3DDevice9Ex *iface,
3285 const D3DVERTEXELEMENT9 *elements, IDirect3DVertexDeclaration9 **declaration)
3287 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3288 struct d3d9_vertex_declaration *object;
3289 HRESULT hr;
3291 TRACE("iface %p, elements %p, declaration %p.\n", iface, elements, declaration);
3293 if (!declaration)
3295 WARN("Caller passed a NULL declaration, returning D3DERR_INVALIDCALL.\n");
3296 return D3DERR_INVALIDCALL;
3299 if (SUCCEEDED(hr = d3d9_vertex_declaration_create(device, elements, &object)))
3300 *declaration = &object->IDirect3DVertexDeclaration9_iface;
3302 return hr;
3305 static HRESULT WINAPI d3d9_device_SetVertexDeclaration(IDirect3DDevice9Ex *iface,
3306 IDirect3DVertexDeclaration9 *declaration)
3308 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3309 struct d3d9_vertex_declaration *decl_impl = unsafe_impl_from_IDirect3DVertexDeclaration9(declaration);
3311 TRACE("iface %p, declaration %p.\n", iface, declaration);
3313 wined3d_mutex_lock();
3314 wined3d_stateblock_set_vertex_declaration(device->update_state,
3315 decl_impl ? decl_impl->wined3d_declaration : NULL);
3316 wined3d_mutex_unlock();
3318 return D3D_OK;
3321 static HRESULT WINAPI d3d9_device_GetVertexDeclaration(IDirect3DDevice9Ex *iface,
3322 IDirect3DVertexDeclaration9 **declaration)
3324 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3325 struct wined3d_vertex_declaration *wined3d_declaration;
3326 struct d3d9_vertex_declaration *declaration_impl;
3328 TRACE("iface %p, declaration %p.\n", iface, declaration);
3330 if (!declaration) return D3DERR_INVALIDCALL;
3332 wined3d_mutex_lock();
3333 if ((wined3d_declaration = device->stateblock_state->vertex_declaration))
3335 declaration_impl = wined3d_vertex_declaration_get_parent(wined3d_declaration);
3336 *declaration = &declaration_impl->IDirect3DVertexDeclaration9_iface;
3337 IDirect3DVertexDeclaration9_AddRef(*declaration);
3339 else
3341 *declaration = NULL;
3343 wined3d_mutex_unlock();
3345 TRACE("Returning %p.\n", *declaration);
3346 return D3D_OK;
3349 static struct wined3d_vertex_declaration *device_get_fvf_declaration(struct d3d9_device *device, DWORD fvf)
3351 struct wined3d_vertex_declaration *wined3d_declaration;
3352 struct fvf_declaration *fvf_decls = device->fvf_decls;
3353 struct d3d9_vertex_declaration *d3d9_declaration;
3354 D3DVERTEXELEMENT9 *elements;
3355 int p, low, high; /* deliberately signed */
3356 HRESULT hr;
3358 TRACE("Searching for declaration for fvf %08lx... ", fvf);
3360 low = 0;
3361 high = device->fvf_decl_count - 1;
3362 while (low <= high)
3364 p = (low + high) >> 1;
3365 TRACE("%d ", p);
3367 if (fvf_decls[p].fvf == fvf)
3369 TRACE("found %p.\n", fvf_decls[p].decl);
3370 return fvf_decls[p].decl;
3373 if (fvf_decls[p].fvf < fvf)
3374 low = p + 1;
3375 else
3376 high = p - 1;
3378 TRACE("not found. Creating and inserting at position %d.\n", low);
3380 if (FAILED(hr = vdecl_convert_fvf(fvf, &elements)))
3381 return NULL;
3383 hr = d3d9_vertex_declaration_create(device, elements, &d3d9_declaration);
3384 heap_free(elements);
3385 if (FAILED(hr))
3386 return NULL;
3388 if (device->fvf_decl_size == device->fvf_decl_count)
3390 UINT grow = max(device->fvf_decl_size / 2, 8);
3392 if (!(fvf_decls = heap_realloc(fvf_decls, sizeof(*fvf_decls) * (device->fvf_decl_size + grow))))
3394 IDirect3DVertexDeclaration9_Release(&d3d9_declaration->IDirect3DVertexDeclaration9_iface);
3395 return NULL;
3397 device->fvf_decls = fvf_decls;
3398 device->fvf_decl_size += grow;
3401 d3d9_declaration->fvf = fvf;
3402 wined3d_declaration = d3d9_declaration->wined3d_declaration;
3403 wined3d_vertex_declaration_incref(wined3d_declaration);
3404 IDirect3DVertexDeclaration9_Release(&d3d9_declaration->IDirect3DVertexDeclaration9_iface);
3406 memmove(fvf_decls + low + 1, fvf_decls + low, sizeof(*fvf_decls) * (device->fvf_decl_count - low));
3407 fvf_decls[low].decl = wined3d_declaration;
3408 fvf_decls[low].fvf = fvf;
3409 ++device->fvf_decl_count;
3411 TRACE("Returning %p. %u declarations in array.\n", wined3d_declaration, device->fvf_decl_count);
3413 return wined3d_declaration;
3416 static HRESULT WINAPI d3d9_device_SetFVF(IDirect3DDevice9Ex *iface, DWORD fvf)
3418 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3419 struct wined3d_vertex_declaration *decl;
3421 TRACE("iface %p, fvf %#lx.\n", iface, fvf);
3423 if (!fvf)
3425 WARN("%#lx is not a valid FVF.\n", fvf);
3426 return D3D_OK;
3429 wined3d_mutex_lock();
3430 if (!(decl = device_get_fvf_declaration(device, fvf)))
3432 wined3d_mutex_unlock();
3433 ERR("Failed to create a vertex declaration for fvf %#lx.\n", fvf);
3434 return D3DERR_DRIVERINTERNALERROR;
3437 wined3d_stateblock_set_vertex_declaration(device->update_state, decl);
3438 wined3d_mutex_unlock();
3440 return D3D_OK;
3443 static HRESULT WINAPI d3d9_device_GetFVF(IDirect3DDevice9Ex *iface, DWORD *fvf)
3445 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3446 struct wined3d_vertex_declaration *wined3d_declaration;
3447 struct d3d9_vertex_declaration *d3d9_declaration;
3449 TRACE("iface %p, fvf %p.\n", iface, fvf);
3451 wined3d_mutex_lock();
3452 if ((wined3d_declaration = device->stateblock_state->vertex_declaration))
3454 d3d9_declaration = wined3d_vertex_declaration_get_parent(wined3d_declaration);
3455 *fvf = d3d9_declaration->fvf;
3457 else
3459 *fvf = 0;
3461 wined3d_mutex_unlock();
3463 TRACE("Returning FVF %#lx.\n", *fvf);
3465 return D3D_OK;
3468 static HRESULT WINAPI d3d9_device_CreateVertexShader(IDirect3DDevice9Ex *iface,
3469 const DWORD *byte_code, IDirect3DVertexShader9 **shader)
3471 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3472 struct d3d9_vertexshader *object;
3473 HRESULT hr;
3475 TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
3477 if (!(object = heap_alloc_zero(sizeof(*object))))
3478 return E_OUTOFMEMORY;
3480 hr = vertexshader_init(object, device, byte_code);
3481 if (FAILED(hr))
3483 WARN("Failed to initialize vertex shader, hr %#lx.\n", hr);
3484 heap_free(object);
3485 return hr;
3488 TRACE("Created vertex shader %p.\n", object);
3489 *shader = &object->IDirect3DVertexShader9_iface;
3491 return D3D_OK;
3494 static HRESULT WINAPI d3d9_device_SetVertexShader(IDirect3DDevice9Ex *iface, IDirect3DVertexShader9 *shader)
3496 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3497 struct d3d9_vertexshader *shader_obj = unsafe_impl_from_IDirect3DVertexShader9(shader);
3499 TRACE("iface %p, shader %p.\n", iface, shader);
3501 wined3d_mutex_lock();
3502 wined3d_stateblock_set_vertex_shader(device->update_state,
3503 shader_obj ? shader_obj->wined3d_shader : NULL);
3504 wined3d_mutex_unlock();
3506 return D3D_OK;
3509 static HRESULT WINAPI d3d9_device_GetVertexShader(IDirect3DDevice9Ex *iface, IDirect3DVertexShader9 **shader)
3511 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3512 struct d3d9_vertexshader *shader_impl;
3513 struct wined3d_shader *wined3d_shader;
3515 TRACE("iface %p, shader %p.\n", iface, shader);
3517 wined3d_mutex_lock();
3518 if ((wined3d_shader = device->stateblock_state->vs))
3520 shader_impl = wined3d_shader_get_parent(wined3d_shader);
3521 *shader = &shader_impl->IDirect3DVertexShader9_iface;
3522 IDirect3DVertexShader9_AddRef(*shader);
3524 else
3526 *shader = NULL;
3528 wined3d_mutex_unlock();
3530 TRACE("Returning %p.\n", *shader);
3532 return D3D_OK;
3535 static HRESULT WINAPI d3d9_device_SetVertexShaderConstantF(IDirect3DDevice9Ex *iface,
3536 UINT reg_idx, const float *data, UINT count)
3538 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3539 HRESULT hr;
3541 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3543 if (reg_idx + count > D3D9_MAX_VERTEX_SHADER_CONSTANTF)
3545 WARN("Trying to access %u constants, but d3d9 only supports %u\n",
3546 reg_idx + count, D3D9_MAX_VERTEX_SHADER_CONSTANTF);
3547 return D3DERR_INVALIDCALL;
3550 wined3d_mutex_lock();
3551 hr = wined3d_stateblock_set_vs_consts_f(device->update_state, reg_idx,
3552 count, (const struct wined3d_vec4 *)data);
3553 wined3d_mutex_unlock();
3555 return hr;
3558 static HRESULT WINAPI d3d9_device_GetVertexShaderConstantF(IDirect3DDevice9Ex *iface,
3559 UINT start_idx, float *constants, UINT count)
3561 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3562 const struct wined3d_vec4 *src;
3564 TRACE("iface %p, start_idx %u, constants %p, count %u.\n", iface, start_idx, constants, count);
3566 if (!constants)
3567 return D3DERR_INVALIDCALL;
3569 if (!wined3d_bound_range(start_idx, count, device->vs_uniform_count))
3571 WARN("Trying to access %u constants, but d3d9 only supports %u\n",
3572 start_idx + count, device->vs_uniform_count);
3573 return D3DERR_INVALIDCALL;
3576 wined3d_mutex_lock();
3577 src = device->stateblock_state->vs_consts_f;
3578 memcpy(constants, &src[start_idx], count * sizeof(*src));
3579 wined3d_mutex_unlock();
3581 return D3D_OK;
3584 static HRESULT WINAPI d3d9_device_SetVertexShaderConstantI(IDirect3DDevice9Ex *iface,
3585 UINT reg_idx, const int *data, UINT count)
3587 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3588 HRESULT hr;
3590 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3592 wined3d_mutex_lock();
3593 hr = wined3d_stateblock_set_vs_consts_i(device->update_state,
3594 reg_idx, count, (const struct wined3d_ivec4 *)data);
3595 wined3d_mutex_unlock();
3597 return hr;
3600 static HRESULT WINAPI d3d9_device_GetVertexShaderConstantI(IDirect3DDevice9Ex *iface,
3601 UINT start_idx, int *constants, UINT count)
3603 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3604 const struct wined3d_ivec4 *src;
3606 TRACE("iface %p, start_idx %u, constants %p, count %u.\n", iface, start_idx, constants, count);
3608 if (!constants || start_idx >= WINED3D_MAX_CONSTS_I)
3609 return WINED3DERR_INVALIDCALL;
3610 if (count > WINED3D_MAX_CONSTS_I - start_idx)
3611 count = WINED3D_MAX_CONSTS_I - start_idx;
3613 wined3d_mutex_lock();
3614 src = device->stateblock_state->vs_consts_i;
3615 memcpy(constants, &src[start_idx], count * sizeof(*src));
3616 wined3d_mutex_unlock();
3618 return D3D_OK;
3621 static HRESULT WINAPI d3d9_device_SetVertexShaderConstantB(IDirect3DDevice9Ex *iface,
3622 UINT reg_idx, const BOOL *data, UINT count)
3624 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3625 HRESULT hr;
3627 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3629 wined3d_mutex_lock();
3630 hr = wined3d_stateblock_set_vs_consts_b(device->update_state, reg_idx, count, data);
3631 wined3d_mutex_unlock();
3633 return hr;
3636 static HRESULT WINAPI d3d9_device_GetVertexShaderConstantB(IDirect3DDevice9Ex *iface,
3637 UINT start_idx, BOOL *constants, UINT count)
3639 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3641 TRACE("iface %p, start_idx %u, constants %p, count %u.\n", iface, start_idx, constants, count);
3643 if (!constants || start_idx >= WINED3D_MAX_CONSTS_B)
3644 return WINED3DERR_INVALIDCALL;
3645 if (count > WINED3D_MAX_CONSTS_B - start_idx)
3646 count = WINED3D_MAX_CONSTS_B - start_idx;
3648 wined3d_mutex_lock();
3649 memcpy(constants, &device->stateblock_state->vs_consts_b[start_idx], count * sizeof(*constants));
3650 wined3d_mutex_unlock();
3652 return D3D_OK;
3655 static HRESULT WINAPI d3d9_device_SetStreamSource(IDirect3DDevice9Ex *iface,
3656 UINT stream_idx, IDirect3DVertexBuffer9 *buffer, UINT offset, UINT stride)
3658 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3659 struct d3d9_vertexbuffer *buffer_impl = unsafe_impl_from_IDirect3DVertexBuffer9(buffer);
3660 struct wined3d_buffer *wined3d_buffer;
3661 HRESULT hr;
3663 TRACE("iface %p, stream_idx %u, buffer %p, offset %u, stride %u.\n",
3664 iface, stream_idx, buffer, offset, stride);
3666 if (stream_idx >= ARRAY_SIZE(device->stateblock_state->streams))
3668 WARN("Stream index %u out of range.\n", stream_idx);
3669 return WINED3DERR_INVALIDCALL;
3672 wined3d_mutex_lock();
3673 if (!buffer_impl)
3675 const struct wined3d_stream_state *stream = &device->stateblock_state->streams[stream_idx];
3677 offset = stream->offset;
3678 stride = stream->stride;
3679 wined3d_buffer = NULL;
3681 else if (buffer_impl->draw_buffer)
3683 wined3d_buffer = buffer_impl->draw_buffer;
3685 else
3687 wined3d_buffer = buffer_impl->wined3d_buffer;
3690 hr = wined3d_stateblock_set_stream_source(device->update_state, stream_idx, wined3d_buffer, offset, stride);
3691 if (SUCCEEDED(hr) && !device->recording)
3693 if (buffer_impl && buffer_impl->draw_buffer)
3694 device->sysmem_vb |= (1u << stream_idx);
3695 else
3696 device->sysmem_vb &= ~(1u << stream_idx);
3699 wined3d_mutex_unlock();
3701 return hr;
3704 static HRESULT WINAPI d3d9_device_GetStreamSource(IDirect3DDevice9Ex *iface,
3705 UINT stream_idx, IDirect3DVertexBuffer9 **buffer, UINT *offset, UINT *stride)
3707 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3708 const struct wined3d_stateblock_state *state;
3709 const struct wined3d_stream_state *stream;
3710 struct d3d9_vertexbuffer *buffer_impl;
3712 TRACE("iface %p, stream_idx %u, buffer %p, offset %p, stride %p.\n",
3713 iface, stream_idx, buffer, offset, stride);
3715 if (!buffer)
3716 return D3DERR_INVALIDCALL;
3718 if (stream_idx >= ARRAY_SIZE(state->streams))
3720 WARN("Stream index %u out of range.\n", stream_idx);
3721 return WINED3DERR_INVALIDCALL;
3724 wined3d_mutex_lock();
3725 stream = &device->stateblock_state->streams[stream_idx];
3726 if (stream->buffer)
3728 buffer_impl = wined3d_buffer_get_parent(stream->buffer);
3729 *buffer = &buffer_impl->IDirect3DVertexBuffer9_iface;
3730 IDirect3DVertexBuffer9_AddRef(*buffer);
3732 else
3733 *buffer = NULL;
3734 if (offset)
3735 *offset = stream->offset;
3736 *stride = stream->stride;
3737 wined3d_mutex_unlock();
3739 return D3D_OK;
3742 static HRESULT WINAPI d3d9_device_SetStreamSourceFreq(IDirect3DDevice9Ex *iface, UINT stream_idx, UINT freq)
3744 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3745 HRESULT hr;
3747 TRACE("iface %p, stream_idx %u, freq %u.\n", iface, stream_idx, freq);
3749 wined3d_mutex_lock();
3750 hr = wined3d_stateblock_set_stream_source_freq(device->update_state, stream_idx, freq);
3751 wined3d_mutex_unlock();
3753 return hr;
3756 static HRESULT WINAPI d3d9_device_GetStreamSourceFreq(IDirect3DDevice9Ex *iface, UINT stream_idx, UINT *freq)
3758 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3759 const struct wined3d_stream_state *stream;
3761 TRACE("iface %p, stream_idx %u, freq %p.\n", iface, stream_idx, freq);
3763 wined3d_mutex_lock();
3764 stream = &device->stateblock_state->streams[stream_idx];
3765 *freq = stream->flags | stream->frequency;
3766 wined3d_mutex_unlock();
3768 return D3D_OK;
3771 static HRESULT WINAPI d3d9_device_SetIndices(IDirect3DDevice9Ex *iface, IDirect3DIndexBuffer9 *buffer)
3773 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3774 struct d3d9_indexbuffer *ib = unsafe_impl_from_IDirect3DIndexBuffer9(buffer);
3775 struct wined3d_buffer *wined3d_buffer;
3777 TRACE("iface %p, buffer %p.\n", iface, buffer);
3779 if (!ib)
3780 wined3d_buffer = NULL;
3781 else
3782 wined3d_buffer = ib->wined3d_buffer;
3784 wined3d_mutex_lock();
3785 wined3d_stateblock_set_index_buffer(device->update_state, wined3d_buffer, ib ? ib->format : WINED3DFMT_UNKNOWN);
3786 if (!device->recording)
3787 device->sysmem_ib = ib && ib->sysmem;
3788 wined3d_mutex_unlock();
3790 return D3D_OK;
3793 static HRESULT WINAPI d3d9_device_GetIndices(IDirect3DDevice9Ex *iface, IDirect3DIndexBuffer9 **buffer)
3795 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3796 struct wined3d_buffer *wined3d_buffer;
3797 struct d3d9_indexbuffer *buffer_impl;
3799 TRACE("iface %p, buffer %p.\n", iface, buffer);
3801 if (!buffer)
3802 return D3DERR_INVALIDCALL;
3804 wined3d_mutex_lock();
3805 if ((wined3d_buffer = device->stateblock_state->index_buffer))
3807 buffer_impl = wined3d_buffer_get_parent(wined3d_buffer);
3808 *buffer = &buffer_impl->IDirect3DIndexBuffer9_iface;
3809 IDirect3DIndexBuffer9_AddRef(*buffer);
3811 else
3813 *buffer = NULL;
3815 wined3d_mutex_unlock();
3817 return D3D_OK;
3820 static HRESULT WINAPI d3d9_device_CreatePixelShader(IDirect3DDevice9Ex *iface,
3821 const DWORD *byte_code, IDirect3DPixelShader9 **shader)
3823 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3824 struct d3d9_pixelshader *object;
3825 HRESULT hr;
3827 TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
3829 if (!(object = heap_alloc_zero(sizeof(*object))))
3831 FIXME("Failed to allocate pixel shader memory.\n");
3832 return E_OUTOFMEMORY;
3835 hr = pixelshader_init(object, device, byte_code);
3836 if (FAILED(hr))
3838 WARN("Failed to initialize pixel shader, hr %#lx.\n", hr);
3839 heap_free(object);
3840 return hr;
3843 TRACE("Created pixel shader %p.\n", object);
3844 *shader = &object->IDirect3DPixelShader9_iface;
3846 return D3D_OK;
3849 static HRESULT WINAPI d3d9_device_SetPixelShader(IDirect3DDevice9Ex *iface, IDirect3DPixelShader9 *shader)
3851 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3852 struct d3d9_pixelshader *shader_obj = unsafe_impl_from_IDirect3DPixelShader9(shader);
3854 TRACE("iface %p, shader %p.\n", iface, shader);
3856 wined3d_mutex_lock();
3857 wined3d_stateblock_set_pixel_shader(device->update_state,
3858 shader_obj ? shader_obj->wined3d_shader : NULL);
3859 wined3d_mutex_unlock();
3861 return D3D_OK;
3864 static HRESULT WINAPI d3d9_device_GetPixelShader(IDirect3DDevice9Ex *iface, IDirect3DPixelShader9 **shader)
3866 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3867 struct d3d9_pixelshader *shader_impl;
3868 struct wined3d_shader *wined3d_shader;
3870 TRACE("iface %p, shader %p.\n", iface, shader);
3872 if (!shader) return D3DERR_INVALIDCALL;
3874 wined3d_mutex_lock();
3875 if ((wined3d_shader = device->stateblock_state->ps))
3877 shader_impl = wined3d_shader_get_parent(wined3d_shader);
3878 *shader = &shader_impl->IDirect3DPixelShader9_iface;
3879 IDirect3DPixelShader9_AddRef(*shader);
3881 else
3883 *shader = NULL;
3885 wined3d_mutex_unlock();
3887 TRACE("Returning %p.\n", *shader);
3889 return D3D_OK;
3892 static HRESULT WINAPI d3d9_device_SetPixelShaderConstantF(IDirect3DDevice9Ex *iface,
3893 UINT reg_idx, const float *data, UINT count)
3895 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3896 HRESULT hr;
3898 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3900 wined3d_mutex_lock();
3901 hr = wined3d_stateblock_set_ps_consts_f(device->update_state,
3902 reg_idx, count, (const struct wined3d_vec4 *)data);
3903 wined3d_mutex_unlock();
3905 return hr;
3908 static HRESULT WINAPI d3d9_device_GetPixelShaderConstantF(IDirect3DDevice9Ex *iface,
3909 UINT start_idx, float *constants, UINT count)
3911 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3912 const struct wined3d_vec4 *src;
3914 TRACE("iface %p, start_idx %u, constants %p, count %u.\n", iface, start_idx, constants, count);
3916 if (!constants || !wined3d_bound_range(start_idx, count, WINED3D_MAX_PS_CONSTS_F))
3917 return WINED3DERR_INVALIDCALL;
3919 wined3d_mutex_lock();
3920 src = device->stateblock_state->ps_consts_f;
3921 memcpy(constants, &src[start_idx], count * sizeof(*src));
3922 wined3d_mutex_unlock();
3924 return D3D_OK;
3927 static HRESULT WINAPI d3d9_device_SetPixelShaderConstantI(IDirect3DDevice9Ex *iface,
3928 UINT reg_idx, const int *data, UINT count)
3930 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3931 HRESULT hr;
3933 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3935 wined3d_mutex_lock();
3936 hr = wined3d_stateblock_set_ps_consts_i(device->update_state,
3937 reg_idx, count, (const struct wined3d_ivec4 *)data);
3938 wined3d_mutex_unlock();
3940 return hr;
3943 static HRESULT WINAPI d3d9_device_GetPixelShaderConstantI(IDirect3DDevice9Ex *iface,
3944 UINT start_idx, int *constants, UINT count)
3946 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3947 const struct wined3d_ivec4 *src;
3949 TRACE("iface %p, start_idx %u, constants %p, count %u.\n", iface, start_idx, constants, count);
3951 if (!constants || start_idx >= WINED3D_MAX_CONSTS_I)
3952 return WINED3DERR_INVALIDCALL;
3953 if (count > WINED3D_MAX_CONSTS_I - start_idx)
3954 count = WINED3D_MAX_CONSTS_I - start_idx;
3956 wined3d_mutex_lock();
3957 src = device->stateblock_state->ps_consts_i;
3958 memcpy(constants, &src[start_idx], count * sizeof(*src));
3959 wined3d_mutex_unlock();
3961 return D3D_OK;
3964 static HRESULT WINAPI d3d9_device_SetPixelShaderConstantB(IDirect3DDevice9Ex *iface,
3965 UINT reg_idx, const BOOL *data, UINT count)
3967 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3968 HRESULT hr;
3970 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3972 wined3d_mutex_lock();
3973 hr = wined3d_stateblock_set_ps_consts_b(device->update_state, reg_idx, count, data);
3974 wined3d_mutex_unlock();
3976 return hr;
3979 static HRESULT WINAPI d3d9_device_GetPixelShaderConstantB(IDirect3DDevice9Ex *iface,
3980 UINT start_idx, BOOL *constants, UINT count)
3982 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3984 TRACE("iface %p, start_idx %u, constants %p, count %u.\n", iface, start_idx, constants, count);
3986 if (!constants || start_idx >= WINED3D_MAX_CONSTS_I)
3987 return WINED3DERR_INVALIDCALL;
3988 if (count > WINED3D_MAX_CONSTS_I - start_idx)
3989 count = WINED3D_MAX_CONSTS_I - start_idx;
3991 wined3d_mutex_lock();
3992 memcpy(constants, &device->stateblock_state->ps_consts_b[start_idx], count * sizeof(*constants));
3993 wined3d_mutex_unlock();
3995 return D3D_OK;
3998 static HRESULT WINAPI d3d9_device_DrawRectPatch(IDirect3DDevice9Ex *iface, UINT handle,
3999 const float *segment_count, const D3DRECTPATCH_INFO *patch_info)
4001 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
4002 iface, handle, segment_count, patch_info);
4003 return D3D_OK;
4006 static HRESULT WINAPI d3d9_device_DrawTriPatch(IDirect3DDevice9Ex *iface, UINT handle,
4007 const float *segment_count, const D3DTRIPATCH_INFO *patch_info)
4009 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
4010 iface, handle, segment_count, patch_info);
4011 return D3D_OK;
4014 static HRESULT WINAPI d3d9_device_DeletePatch(IDirect3DDevice9Ex *iface, UINT handle)
4016 FIXME("iface %p, handle %#x unimplemented.\n", iface, handle);
4017 return D3DERR_INVALIDCALL;
4020 static HRESULT WINAPI d3d9_device_CreateQuery(IDirect3DDevice9Ex *iface, D3DQUERYTYPE type, IDirect3DQuery9 **query)
4022 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
4023 struct d3d9_query *object;
4024 HRESULT hr;
4026 TRACE("iface %p, type %#x, query %p.\n", iface, type, query);
4028 if (!(object = heap_alloc_zero(sizeof(*object))))
4029 return E_OUTOFMEMORY;
4031 hr = query_init(object, device, type);
4032 if (FAILED(hr))
4034 WARN("Failed to initialize query, hr %#lx.\n", hr);
4035 heap_free(object);
4036 return hr;
4039 TRACE("Created query %p.\n", object);
4040 if (query) *query = &object->IDirect3DQuery9_iface;
4041 else IDirect3DQuery9_Release(&object->IDirect3DQuery9_iface);
4043 return D3D_OK;
4046 static HRESULT WINAPI d3d9_device_SetConvolutionMonoKernel(IDirect3DDevice9Ex *iface,
4047 UINT width, UINT height, float *rows, float *columns)
4049 FIXME("iface %p, width %u, height %u, rows %p, columns %p stub!\n",
4050 iface, width, height, rows, columns);
4052 return E_NOTIMPL;
4055 static HRESULT WINAPI d3d9_device_ComposeRects(IDirect3DDevice9Ex *iface,
4056 IDirect3DSurface9 *src_surface, IDirect3DSurface9 *dst_surface, IDirect3DVertexBuffer9 *src_descs,
4057 UINT rect_count, IDirect3DVertexBuffer9 *dst_descs, D3DCOMPOSERECTSOP operation, INT offset_x, INT offset_y)
4059 FIXME("iface %p, src_surface %p, dst_surface %p, src_descs %p, rect_count %u, "
4060 "dst_descs %p, operation %#x, offset_x %u, offset_y %u stub!\n",
4061 iface, src_surface, dst_surface, src_descs, rect_count,
4062 dst_descs, operation, offset_x, offset_y);
4064 return E_NOTIMPL;
4067 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_PresentEx(IDirect3DDevice9Ex *iface,
4068 const RECT *src_rect, const RECT *dst_rect, HWND dst_window_override,
4069 const RGNDATA *dirty_region, DWORD flags)
4071 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
4072 struct d3d9_swapchain *swapchain;
4073 unsigned int i;
4074 HRESULT hr;
4076 TRACE("iface %p, src_rect %s, dst_rect %s, dst_window_override %p, dirty_region %p, flags %#lx.\n",
4077 iface, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect),
4078 dst_window_override, dirty_region, flags);
4080 if (device->device_state != D3D9_DEVICE_STATE_OK)
4081 return S_PRESENT_OCCLUDED;
4083 if (dirty_region)
4084 FIXME("Ignoring dirty_region %p.\n", dirty_region);
4086 wined3d_mutex_lock();
4087 for (i = 0; i < device->implicit_swapchain_count; ++i)
4089 swapchain = wined3d_swapchain_get_parent(device->implicit_swapchains[i]);
4090 if (FAILED(hr = wined3d_swapchain_present(swapchain->wined3d_swapchain,
4091 src_rect, dst_rect, dst_window_override, swapchain->swap_interval, flags)))
4093 wined3d_mutex_unlock();
4094 return hr;
4097 wined3d_mutex_unlock();
4099 return D3D_OK;
4102 static HRESULT WINAPI d3d9_device_GetGPUThreadPriority(IDirect3DDevice9Ex *iface, INT *priority)
4104 FIXME("iface %p, priority %p stub!\n", iface, priority);
4106 return E_NOTIMPL;
4109 static HRESULT WINAPI d3d9_device_SetGPUThreadPriority(IDirect3DDevice9Ex *iface, INT priority)
4111 FIXME("iface %p, priority %d stub!\n", iface, priority);
4113 return E_NOTIMPL;
4116 static HRESULT WINAPI d3d9_device_WaitForVBlank(IDirect3DDevice9Ex *iface, UINT swapchain_idx)
4118 FIXME("iface %p, swapchain_idx %u stub!\n", iface, swapchain_idx);
4120 return E_NOTIMPL;
4123 static HRESULT WINAPI d3d9_device_CheckResourceResidency(IDirect3DDevice9Ex *iface,
4124 IDirect3DResource9 **resources, UINT32 resource_count)
4126 FIXME("iface %p, resources %p, resource_count %u stub!\n",
4127 iface, resources, resource_count);
4129 return E_NOTIMPL;
4132 static HRESULT WINAPI d3d9_device_SetMaximumFrameLatency(IDirect3DDevice9Ex *iface, UINT max_latency)
4134 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
4136 TRACE("iface %p, max_latency %u.\n", iface, max_latency);
4138 if (max_latency > 30)
4139 return D3DERR_INVALIDCALL;
4141 wined3d_mutex_lock();
4142 wined3d_device_set_max_frame_latency(device->wined3d_device, max_latency);
4143 wined3d_mutex_unlock();
4145 return S_OK;
4148 static HRESULT WINAPI d3d9_device_GetMaximumFrameLatency(IDirect3DDevice9Ex *iface, UINT *max_latency)
4150 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
4152 TRACE("iface %p, max_latency %p.\n", iface, max_latency);
4154 wined3d_mutex_lock();
4155 *max_latency = wined3d_device_get_max_frame_latency(device->wined3d_device);
4156 wined3d_mutex_unlock();
4158 return S_OK;
4161 static HRESULT WINAPI d3d9_device_CheckDeviceState(IDirect3DDevice9Ex *iface, HWND dst_window)
4163 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
4164 struct wined3d_swapchain_desc swapchain_desc;
4166 TRACE("iface %p, dst_window %p.\n", iface, dst_window);
4168 wined3d_mutex_lock();
4169 wined3d_swapchain_get_desc(device->implicit_swapchains[0], &swapchain_desc);
4170 wined3d_mutex_unlock();
4172 if (swapchain_desc.windowed)
4173 return D3D_OK;
4175 /* FIXME: This is actually supposed to check if any other device is in
4176 * fullscreen mode. */
4177 if (dst_window != swapchain_desc.device_window)
4178 return device->device_state == D3D9_DEVICE_STATE_OK ? S_PRESENT_OCCLUDED : D3D_OK;
4180 return device->device_state == D3D9_DEVICE_STATE_OK ? D3D_OK : S_PRESENT_OCCLUDED;
4183 static HRESULT WINAPI d3d9_device_CreateRenderTargetEx(IDirect3DDevice9Ex *iface,
4184 UINT width, UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
4185 BOOL lockable, IDirect3DSurface9 **surface, HANDLE *shared_handle, DWORD usage)
4187 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
4188 unsigned int access = WINED3D_RESOURCE_ACCESS_GPU;
4190 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %lu, "
4191 "lockable %#x, surface %p, shared_handle %p, usage %#lx.\n",
4192 iface, width, height, format, multisample_type, multisample_quality,
4193 lockable, surface, shared_handle, usage);
4195 *surface = NULL;
4197 if (usage & (D3DUSAGE_DEPTHSTENCIL | D3DUSAGE_RENDERTARGET))
4199 WARN("Invalid usage %#lx.\n", usage);
4200 return D3DERR_INVALIDCALL;
4203 if (shared_handle)
4204 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
4206 if (lockable)
4207 access |= WINED3D_RESOURCE_ACCESS_MAP_R | WINED3D_RESOURCE_ACCESS_MAP_W;
4209 return d3d9_device_create_surface(device, 0, wined3dformat_from_d3dformat(format),
4210 wined3d_multisample_type_from_d3d(multisample_type), multisample_quality,
4211 usage & WINED3DUSAGE_MASK, WINED3D_BIND_RENDER_TARGET, access, width, height, NULL, surface);
4214 static HRESULT WINAPI d3d9_device_CreateOffscreenPlainSurfaceEx(IDirect3DDevice9Ex *iface,
4215 UINT width, UINT height, D3DFORMAT format, D3DPOOL pool, IDirect3DSurface9 **surface,
4216 HANDLE *shared_handle, DWORD usage)
4218 FIXME("iface %p, width %u, height %u, format %#x, pool %#x, surface %p, shared_handle %p, usage %#lx stub!\n",
4219 iface, width, height, format, pool, surface, shared_handle, usage);
4221 return E_NOTIMPL;
4224 static HRESULT WINAPI d3d9_device_CreateDepthStencilSurfaceEx(IDirect3DDevice9Ex *iface,
4225 UINT width, UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
4226 BOOL discard, IDirect3DSurface9 **surface, HANDLE *shared_handle, DWORD usage)
4228 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
4229 DWORD flags = 0;
4231 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %lu, "
4232 "discard %#x, surface %p, shared_handle %p, usage %#lx.\n",
4233 iface, width, height, format, multisample_type, multisample_quality,
4234 discard, surface, shared_handle, usage);
4236 if (usage & (D3DUSAGE_DEPTHSTENCIL | D3DUSAGE_RENDERTARGET))
4238 WARN("Invalid usage %#lx.\n", usage);
4239 return D3DERR_INVALIDCALL;
4242 if (shared_handle)
4243 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
4245 if (discard)
4246 flags |= WINED3D_TEXTURE_CREATE_DISCARD;
4248 *surface = NULL;
4249 return d3d9_device_create_surface(device, flags, wined3dformat_from_d3dformat(format),
4250 wined3d_multisample_type_from_d3d(multisample_type), multisample_quality, usage & WINED3DUSAGE_MASK,
4251 WINED3D_BIND_DEPTH_STENCIL, WINED3D_RESOURCE_ACCESS_GPU, width, height, NULL, surface);
4254 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_ResetEx(IDirect3DDevice9Ex *iface,
4255 D3DPRESENT_PARAMETERS *present_parameters, D3DDISPLAYMODEEX *mode)
4257 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
4259 TRACE("iface %p, present_parameters %p, mode %p.\n", iface, present_parameters, mode);
4261 if (!present_parameters->Windowed == !mode)
4263 WARN("Mode can be passed if and only if Windowed is FALSE.\n");
4264 return D3DERR_INVALIDCALL;
4267 if (mode && (mode->Width != present_parameters->BackBufferWidth
4268 || mode->Height != present_parameters->BackBufferHeight))
4270 WARN("Mode and back buffer mismatch (mode %ux%u, backbuffer %ux%u).\n",
4271 mode->Width, mode->Height,
4272 present_parameters->BackBufferWidth, present_parameters->BackBufferHeight);
4273 return D3DERR_INVALIDCALL;
4276 return d3d9_device_reset(device, present_parameters, mode);
4279 static HRESULT WINAPI d3d9_device_GetDisplayModeEx(IDirect3DDevice9Ex *iface,
4280 UINT swapchain_idx, D3DDISPLAYMODEEX *mode, D3DDISPLAYROTATION *rotation)
4282 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
4283 struct wined3d_display_mode wined3d_mode;
4284 HRESULT hr;
4286 TRACE("iface %p, swapchain_idx %u, mode %p, rotation %p.\n",
4287 iface, swapchain_idx, mode, rotation);
4289 if (mode->Size != sizeof(*mode))
4290 return D3DERR_INVALIDCALL;
4292 wined3d_mutex_lock();
4293 hr = wined3d_device_get_display_mode(device->wined3d_device, swapchain_idx, &wined3d_mode,
4294 (enum wined3d_display_rotation *)rotation);
4295 wined3d_mutex_unlock();
4297 if (SUCCEEDED(hr))
4299 mode->Width = wined3d_mode.width;
4300 mode->Height = wined3d_mode.height;
4301 mode->RefreshRate = wined3d_mode.refresh_rate;
4302 mode->Format = d3dformat_from_wined3dformat(wined3d_mode.format_id);
4303 mode->ScanLineOrdering = d3dscanlineordering_from_wined3d(wined3d_mode.scanline_ordering);
4306 return hr;
4309 static const struct IDirect3DDevice9ExVtbl d3d9_device_vtbl =
4311 /* IUnknown */
4312 d3d9_device_QueryInterface,
4313 d3d9_device_AddRef,
4314 d3d9_device_Release,
4315 /* IDirect3DDevice9 */
4316 d3d9_device_TestCooperativeLevel,
4317 d3d9_device_GetAvailableTextureMem,
4318 d3d9_device_EvictManagedResources,
4319 d3d9_device_GetDirect3D,
4320 d3d9_device_GetDeviceCaps,
4321 d3d9_device_GetDisplayMode,
4322 d3d9_device_GetCreationParameters,
4323 d3d9_device_SetCursorProperties,
4324 d3d9_device_SetCursorPosition,
4325 d3d9_device_ShowCursor,
4326 d3d9_device_CreateAdditionalSwapChain,
4327 d3d9_device_GetSwapChain,
4328 d3d9_device_GetNumberOfSwapChains,
4329 d3d9_device_Reset,
4330 d3d9_device_Present,
4331 d3d9_device_GetBackBuffer,
4332 d3d9_device_GetRasterStatus,
4333 d3d9_device_SetDialogBoxMode,
4334 d3d9_device_SetGammaRamp,
4335 d3d9_device_GetGammaRamp,
4336 d3d9_device_CreateTexture,
4337 d3d9_device_CreateVolumeTexture,
4338 d3d9_device_CreateCubeTexture,
4339 d3d9_device_CreateVertexBuffer,
4340 d3d9_device_CreateIndexBuffer,
4341 d3d9_device_CreateRenderTarget,
4342 d3d9_device_CreateDepthStencilSurface,
4343 d3d9_device_UpdateSurface,
4344 d3d9_device_UpdateTexture,
4345 d3d9_device_GetRenderTargetData,
4346 d3d9_device_GetFrontBufferData,
4347 d3d9_device_StretchRect,
4348 d3d9_device_ColorFill,
4349 d3d9_device_CreateOffscreenPlainSurface,
4350 d3d9_device_SetRenderTarget,
4351 d3d9_device_GetRenderTarget,
4352 d3d9_device_SetDepthStencilSurface,
4353 d3d9_device_GetDepthStencilSurface,
4354 d3d9_device_BeginScene,
4355 d3d9_device_EndScene,
4356 d3d9_device_Clear,
4357 d3d9_device_SetTransform,
4358 d3d9_device_GetTransform,
4359 d3d9_device_MultiplyTransform,
4360 d3d9_device_SetViewport,
4361 d3d9_device_GetViewport,
4362 d3d9_device_SetMaterial,
4363 d3d9_device_GetMaterial,
4364 d3d9_device_SetLight,
4365 d3d9_device_GetLight,
4366 d3d9_device_LightEnable,
4367 d3d9_device_GetLightEnable,
4368 d3d9_device_SetClipPlane,
4369 d3d9_device_GetClipPlane,
4370 d3d9_device_SetRenderState,
4371 d3d9_device_GetRenderState,
4372 d3d9_device_CreateStateBlock,
4373 d3d9_device_BeginStateBlock,
4374 d3d9_device_EndStateBlock,
4375 d3d9_device_SetClipStatus,
4376 d3d9_device_GetClipStatus,
4377 d3d9_device_GetTexture,
4378 d3d9_device_SetTexture,
4379 d3d9_device_GetTextureStageState,
4380 d3d9_device_SetTextureStageState,
4381 d3d9_device_GetSamplerState,
4382 d3d9_device_SetSamplerState,
4383 d3d9_device_ValidateDevice,
4384 d3d9_device_SetPaletteEntries,
4385 d3d9_device_GetPaletteEntries,
4386 d3d9_device_SetCurrentTexturePalette,
4387 d3d9_device_GetCurrentTexturePalette,
4388 d3d9_device_SetScissorRect,
4389 d3d9_device_GetScissorRect,
4390 d3d9_device_SetSoftwareVertexProcessing,
4391 d3d9_device_GetSoftwareVertexProcessing,
4392 d3d9_device_SetNPatchMode,
4393 d3d9_device_GetNPatchMode,
4394 d3d9_device_DrawPrimitive,
4395 d3d9_device_DrawIndexedPrimitive,
4396 d3d9_device_DrawPrimitiveUP,
4397 d3d9_device_DrawIndexedPrimitiveUP,
4398 d3d9_device_ProcessVertices,
4399 d3d9_device_CreateVertexDeclaration,
4400 d3d9_device_SetVertexDeclaration,
4401 d3d9_device_GetVertexDeclaration,
4402 d3d9_device_SetFVF,
4403 d3d9_device_GetFVF,
4404 d3d9_device_CreateVertexShader,
4405 d3d9_device_SetVertexShader,
4406 d3d9_device_GetVertexShader,
4407 d3d9_device_SetVertexShaderConstantF,
4408 d3d9_device_GetVertexShaderConstantF,
4409 d3d9_device_SetVertexShaderConstantI,
4410 d3d9_device_GetVertexShaderConstantI,
4411 d3d9_device_SetVertexShaderConstantB,
4412 d3d9_device_GetVertexShaderConstantB,
4413 d3d9_device_SetStreamSource,
4414 d3d9_device_GetStreamSource,
4415 d3d9_device_SetStreamSourceFreq,
4416 d3d9_device_GetStreamSourceFreq,
4417 d3d9_device_SetIndices,
4418 d3d9_device_GetIndices,
4419 d3d9_device_CreatePixelShader,
4420 d3d9_device_SetPixelShader,
4421 d3d9_device_GetPixelShader,
4422 d3d9_device_SetPixelShaderConstantF,
4423 d3d9_device_GetPixelShaderConstantF,
4424 d3d9_device_SetPixelShaderConstantI,
4425 d3d9_device_GetPixelShaderConstantI,
4426 d3d9_device_SetPixelShaderConstantB,
4427 d3d9_device_GetPixelShaderConstantB,
4428 d3d9_device_DrawRectPatch,
4429 d3d9_device_DrawTriPatch,
4430 d3d9_device_DeletePatch,
4431 d3d9_device_CreateQuery,
4432 /* IDirect3DDevice9Ex */
4433 d3d9_device_SetConvolutionMonoKernel,
4434 d3d9_device_ComposeRects,
4435 d3d9_device_PresentEx,
4436 d3d9_device_GetGPUThreadPriority,
4437 d3d9_device_SetGPUThreadPriority,
4438 d3d9_device_WaitForVBlank,
4439 d3d9_device_CheckResourceResidency,
4440 d3d9_device_SetMaximumFrameLatency,
4441 d3d9_device_GetMaximumFrameLatency,
4442 d3d9_device_CheckDeviceState,
4443 d3d9_device_CreateRenderTargetEx,
4444 d3d9_device_CreateOffscreenPlainSurfaceEx,
4445 d3d9_device_CreateDepthStencilSurfaceEx,
4446 d3d9_device_ResetEx,
4447 d3d9_device_GetDisplayModeEx,
4450 static inline struct d3d9_device *device_from_device_parent(struct wined3d_device_parent *device_parent)
4452 return CONTAINING_RECORD(device_parent, struct d3d9_device, device_parent);
4455 static void CDECL device_parent_wined3d_device_created(struct wined3d_device_parent *device_parent,
4456 struct wined3d_device *device)
4458 TRACE("device_parent %p, device %p.\n", device_parent, device);
4461 static void CDECL device_parent_mode_changed(struct wined3d_device_parent *device_parent)
4463 TRACE("device_parent %p.\n", device_parent);
4466 static void CDECL device_parent_activate(struct wined3d_device_parent *device_parent, BOOL activate)
4468 struct d3d9_device *device = device_from_device_parent(device_parent);
4470 TRACE("device_parent %p, activate %#x.\n", device_parent, activate);
4472 if (!device->d3d_parent)
4473 return;
4475 if (!activate)
4476 InterlockedCompareExchange(&device->device_state, D3D9_DEVICE_STATE_LOST, D3D9_DEVICE_STATE_OK);
4477 else if (device->d3d_parent->extended)
4478 InterlockedCompareExchange(&device->device_state, D3D9_DEVICE_STATE_OK, D3D9_DEVICE_STATE_LOST);
4479 else
4480 InterlockedCompareExchange(&device->device_state, D3D9_DEVICE_STATE_NOT_RESET, D3D9_DEVICE_STATE_LOST);
4483 static HRESULT CDECL device_parent_texture_sub_resource_created(struct wined3d_device_parent *device_parent,
4484 enum wined3d_resource_type type, struct wined3d_texture *wined3d_texture, unsigned int sub_resource_idx,
4485 void **parent, const struct wined3d_parent_ops **parent_ops)
4487 TRACE("device_parent %p, type %#x, wined3d_texture %p, sub_resource_idx %u, parent %p, parent_ops %p.\n",
4488 device_parent, type, wined3d_texture, sub_resource_idx, parent, parent_ops);
4490 if (type == WINED3D_RTYPE_TEXTURE_2D)
4492 struct d3d9_surface *d3d_surface;
4494 if (!(d3d_surface = heap_alloc_zero(sizeof(*d3d_surface))))
4495 return E_OUTOFMEMORY;
4497 surface_init(d3d_surface, wined3d_texture, sub_resource_idx, parent_ops);
4498 *parent = d3d_surface;
4499 TRACE("Created surface %p.\n", d3d_surface);
4501 else if (type == WINED3D_RTYPE_TEXTURE_3D)
4503 struct d3d9_volume *d3d_volume;
4505 if (!(d3d_volume = heap_alloc_zero(sizeof(*d3d_volume))))
4506 return E_OUTOFMEMORY;
4508 volume_init(d3d_volume, wined3d_texture, sub_resource_idx, parent_ops);
4509 *parent = d3d_volume;
4510 TRACE("Created volume %p.\n", d3d_volume);
4512 else
4514 ERR("Unhandled resource type %#x.\n", type);
4515 return E_FAIL;
4518 return D3D_OK;
4521 static HRESULT CDECL device_parent_create_swapchain_texture(struct wined3d_device_parent *device_parent,
4522 void *container_parent, const struct wined3d_resource_desc *desc, DWORD texture_flags,
4523 struct wined3d_texture **texture)
4525 struct d3d9_device *device = device_from_device_parent(device_parent);
4526 struct d3d9_surface *d3d_surface;
4527 HRESULT hr;
4529 TRACE("device_parent %p, container_parent %p, desc %p, texture flags %#lx, texture %p.\n",
4530 device_parent, container_parent, desc, texture_flags, texture);
4532 if (container_parent == device_parent)
4533 container_parent = &device->IDirect3DDevice9Ex_iface;
4535 if (FAILED(hr = wined3d_texture_create(device->wined3d_device, desc, 1, 1,
4536 texture_flags, NULL, container_parent, &d3d9_null_wined3d_parent_ops, texture)))
4538 WARN("Failed to create texture, hr %#lx.\n", hr);
4539 return hr;
4542 d3d_surface = wined3d_texture_get_sub_resource_parent(*texture, 0);
4543 d3d_surface->parent_device = &device->IDirect3DDevice9Ex_iface;
4545 return hr;
4548 static const struct wined3d_device_parent_ops d3d9_wined3d_device_parent_ops =
4550 device_parent_wined3d_device_created,
4551 device_parent_mode_changed,
4552 device_parent_activate,
4553 device_parent_texture_sub_resource_created,
4554 device_parent_create_swapchain_texture,
4557 static void setup_fpu(void)
4559 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
4560 WORD cw;
4561 __asm__ volatile ("fnstcw %0" : "=m" (cw));
4562 cw = (cw & ~0xf3f) | 0x3f;
4563 __asm__ volatile ("fldcw %0" : : "m" (cw));
4564 #elif defined(__i386__) && defined(_MSC_VER)
4565 WORD cw;
4566 __asm fnstcw cw;
4567 cw = (cw & ~0xf3f) | 0x3f;
4568 __asm fldcw cw;
4569 #else
4570 FIXME("FPU setup not implemented for this platform.\n");
4571 #endif
4574 HRESULT device_init(struct d3d9_device *device, struct d3d9 *parent, struct wined3d *wined3d,
4575 UINT adapter, D3DDEVTYPE device_type, HWND focus_window, DWORD flags,
4576 D3DPRESENT_PARAMETERS *parameters, D3DDISPLAYMODEEX *mode)
4578 struct wined3d_swapchain_desc *swapchain_desc;
4579 struct wined3d_adapter *wined3d_adapter;
4580 struct d3d9_swapchain *d3d_swapchain;
4581 struct wined3d_caps wined3d_caps;
4582 unsigned int output_idx;
4583 unsigned i, count = 1;
4584 D3DCAPS9 caps;
4585 HRESULT hr;
4587 static const enum wined3d_feature_level feature_levels[] =
4589 WINED3D_FEATURE_LEVEL_9_3,
4590 WINED3D_FEATURE_LEVEL_9_2,
4591 WINED3D_FEATURE_LEVEL_9_1,
4592 WINED3D_FEATURE_LEVEL_8,
4593 WINED3D_FEATURE_LEVEL_7,
4594 WINED3D_FEATURE_LEVEL_6,
4595 WINED3D_FEATURE_LEVEL_5,
4598 output_idx = adapter;
4599 if (output_idx >= parent->wined3d_output_count)
4600 return D3DERR_INVALIDCALL;
4602 if (mode)
4603 FIXME("Ignoring display mode.\n");
4605 device->IDirect3DDevice9Ex_iface.lpVtbl = &d3d9_device_vtbl;
4606 device->device_parent.ops = &d3d9_wined3d_device_parent_ops;
4607 device->adapter_ordinal = adapter;
4608 device->refcount = 1;
4610 if (!(flags & D3DCREATE_FPU_PRESERVE)) setup_fpu();
4612 wined3d_mutex_lock();
4613 wined3d_adapter = wined3d_output_get_adapter(parent->wined3d_outputs[output_idx]);
4614 if (FAILED(hr = wined3d_device_create(wined3d, wined3d_adapter, wined3d_device_type_from_d3d(device_type),
4615 focus_window, flags, 4, feature_levels, ARRAY_SIZE(feature_levels),
4616 &device->device_parent, &device->wined3d_device)))
4618 WARN("Failed to create wined3d device, hr %#lx.\n", hr);
4619 wined3d_mutex_unlock();
4620 return hr;
4623 device->immediate_context = wined3d_device_get_immediate_context(device->wined3d_device);
4624 wined3d_get_device_caps(wined3d_adapter, wined3d_device_type_from_d3d(device_type), &wined3d_caps);
4625 d3d9_caps_from_wined3dcaps(parent, adapter, &caps, &wined3d_caps);
4626 device->max_user_clip_planes = caps.MaxUserClipPlanes;
4627 device->vs_uniform_count = caps.MaxVertexShaderConst;
4628 if (flags & D3DCREATE_ADAPTERGROUP_DEVICE)
4629 count = caps.NumberOfAdaptersInGroup;
4631 if (FAILED(hr = wined3d_stateblock_create(device->wined3d_device, NULL, WINED3D_SBT_PRIMARY, &device->state)))
4633 ERR("Failed to create the primary stateblock, hr %#lx.\n", hr);
4634 wined3d_device_decref(device->wined3d_device);
4635 wined3d_mutex_unlock();
4636 return hr;
4638 device->stateblock_state = wined3d_stateblock_get_state(device->state);
4639 device->update_state = device->state;
4641 wined3d_streaming_buffer_init(&device->vertex_buffer, WINED3D_BIND_VERTEX_BUFFER);
4642 wined3d_streaming_buffer_init(&device->index_buffer, WINED3D_BIND_INDEX_BUFFER);
4644 if (flags & D3DCREATE_MULTITHREADED)
4645 wined3d_device_set_multithreaded(device->wined3d_device);
4647 if (!parameters->Windowed)
4649 if (!focus_window)
4650 focus_window = parameters->hDeviceWindow;
4651 if (FAILED(hr = wined3d_device_acquire_focus_window(device->wined3d_device, focus_window)))
4653 ERR("Failed to acquire focus window, hr %#lx.\n", hr);
4654 wined3d_device_decref(device->wined3d_device);
4655 wined3d_mutex_unlock();
4656 return hr;
4660 if (!(swapchain_desc = heap_alloc(sizeof(*swapchain_desc) * count)))
4662 ERR("Failed to allocate wined3d parameters.\n");
4663 wined3d_device_release_focus_window(device->wined3d_device);
4664 wined3d_device_decref(device->wined3d_device);
4665 wined3d_mutex_unlock();
4666 return E_OUTOFMEMORY;
4669 for (i = 0; i < count; ++i)
4671 if (!wined3d_swapchain_desc_from_d3d9(&swapchain_desc[i],
4672 parent->wined3d_outputs[output_idx + i], &parameters[i], parent->extended))
4674 wined3d_device_release_focus_window(device->wined3d_device);
4675 wined3d_device_decref(device->wined3d_device);
4676 heap_free(swapchain_desc);
4677 wined3d_mutex_unlock();
4678 return D3DERR_INVALIDCALL;
4680 swapchain_desc[i].flags |= WINED3D_SWAPCHAIN_IMPLICIT;
4681 if (flags & D3DCREATE_NOWINDOWCHANGES)
4682 swapchain_desc[i].flags |= WINED3D_SWAPCHAIN_NO_WINDOW_CHANGES;
4685 if (FAILED(hr = d3d9_swapchain_create(device, swapchain_desc,
4686 wined3dswapinterval_from_d3d(parameters->PresentationInterval), &d3d_swapchain)))
4688 WARN("Failed to create swapchain, hr %#lx.\n", hr);
4689 wined3d_device_release_focus_window(device->wined3d_device);
4690 heap_free(swapchain_desc);
4691 wined3d_device_decref(device->wined3d_device);
4692 wined3d_mutex_unlock();
4693 return hr;
4696 wined3d_swapchain_incref(d3d_swapchain->wined3d_swapchain);
4697 IDirect3DSwapChain9Ex_Release(&d3d_swapchain->IDirect3DSwapChain9Ex_iface);
4699 wined3d_stateblock_set_render_state(device->state, WINED3D_RS_ZENABLE,
4700 !!swapchain_desc->enable_auto_depth_stencil);
4701 device_reset_viewport_state(device);
4703 if (FAILED(hr = d3d9_device_get_swapchains(device)))
4705 wined3d_swapchain_decref(d3d_swapchain->wined3d_swapchain);
4706 wined3d_device_release_focus_window(device->wined3d_device);
4707 wined3d_device_decref(device->wined3d_device);
4708 heap_free(swapchain_desc);
4709 wined3d_mutex_unlock();
4710 return E_OUTOFMEMORY;
4713 for (i = 0; i < count; ++i)
4715 present_parameters_from_wined3d_swapchain_desc(&parameters[i],
4716 &swapchain_desc[i], parameters[i].PresentationInterval);
4719 wined3d_mutex_unlock();
4721 heap_free(swapchain_desc);
4723 /* Initialize the converted declaration array. This creates a valid pointer
4724 * and when adding decls HeapReAlloc() can be used without further checking. */
4725 if (!(device->fvf_decls = heap_alloc(0)))
4727 ERR("Failed to allocate FVF vertex declaration map memory.\n");
4728 wined3d_mutex_lock();
4729 heap_free(device->implicit_swapchains);
4730 wined3d_swapchain_decref(d3d_swapchain->wined3d_swapchain);
4731 wined3d_device_release_focus_window(device->wined3d_device);
4732 wined3d_device_decref(device->wined3d_device);
4733 wined3d_mutex_unlock();
4734 return E_OUTOFMEMORY;
4737 /* We could also simply ignore the initial rendertarget since it's known
4738 * not to be a texture (we currently use these only for automatic mipmap
4739 * generation). */
4740 wined3d_mutex_lock();
4741 device->render_targets[0] = wined3d_rendertarget_view_get_sub_resource_parent(
4742 wined3d_device_context_get_rendertarget_view(device->immediate_context, 0));
4743 wined3d_mutex_unlock();
4745 IDirect3D9Ex_AddRef(&parent->IDirect3D9Ex_iface);
4746 device->d3d_parent = parent;
4748 return D3D_OK;