kernelbase: Remove unnecessary handler from LocalLock.
[wine.git] / dlls / d3d9 / device.c
blobac6a7054838ef6336a0bdb44cef049e607a0603b
1 /*
2 * IDirect3DDevice9 implementation
4 * Copyright 2002-2005 Jason Edmeades
5 * Copyright 2002-2005 Raphael Junqueira
6 * Copyright 2005 Oliver Stieber
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "d3d9_private.h"
25 WINE_DEFAULT_DEBUG_CHANNEL(d3d9);
27 static void STDMETHODCALLTYPE d3d9_null_wined3d_object_destroyed(void *parent) {}
29 const struct wined3d_parent_ops d3d9_null_wined3d_parent_ops =
31 d3d9_null_wined3d_object_destroyed,
34 static void wined3d_color_from_d3dcolor(struct wined3d_color *wined3d_colour, D3DCOLOR d3d_colour)
36 wined3d_colour->r = ((d3d_colour >> 16) & 0xff) / 255.0f;
37 wined3d_colour->g = ((d3d_colour >> 8) & 0xff) / 255.0f;
38 wined3d_colour->b = (d3d_colour & 0xff) / 255.0f;
39 wined3d_colour->a = ((d3d_colour >> 24) & 0xff) / 255.0f;
42 D3DFORMAT d3dformat_from_wined3dformat(enum wined3d_format_id format)
44 BYTE *c = (BYTE *)&format;
46 /* Don't translate FOURCC formats */
47 if (isprint(c[0]) && isprint(c[1]) && isprint(c[2]) && isprint(c[3]))
48 return (D3DFORMAT)format;
50 switch(format)
52 case WINED3DFMT_UNKNOWN: return D3DFMT_UNKNOWN;
53 case WINED3DFMT_B8G8R8_UNORM: return D3DFMT_R8G8B8;
54 case WINED3DFMT_B8G8R8A8_UNORM: return D3DFMT_A8R8G8B8;
55 case WINED3DFMT_B8G8R8X8_UNORM: return D3DFMT_X8R8G8B8;
56 case WINED3DFMT_B5G6R5_UNORM: return D3DFMT_R5G6B5;
57 case WINED3DFMT_B5G5R5X1_UNORM: return D3DFMT_X1R5G5B5;
58 case WINED3DFMT_B5G5R5A1_UNORM: return D3DFMT_A1R5G5B5;
59 case WINED3DFMT_B4G4R4A4_UNORM: return D3DFMT_A4R4G4B4;
60 case WINED3DFMT_B2G3R3_UNORM: return D3DFMT_R3G3B2;
61 case WINED3DFMT_A8_UNORM: return D3DFMT_A8;
62 case WINED3DFMT_B2G3R3A8_UNORM: return D3DFMT_A8R3G3B2;
63 case WINED3DFMT_B4G4R4X4_UNORM: return D3DFMT_X4R4G4B4;
64 case WINED3DFMT_R10G10B10A2_UNORM: return D3DFMT_A2B10G10R10;
65 case WINED3DFMT_R8G8B8A8_UNORM: return D3DFMT_A8B8G8R8;
66 case WINED3DFMT_R8G8B8X8_UNORM: return D3DFMT_X8B8G8R8;
67 case WINED3DFMT_R16G16_UNORM: return D3DFMT_G16R16;
68 case WINED3DFMT_B10G10R10A2_UNORM: return D3DFMT_A2R10G10B10;
69 case WINED3DFMT_R16G16B16A16_UNORM: return D3DFMT_A16B16G16R16;
70 case WINED3DFMT_P8_UINT_A8_UNORM: return D3DFMT_A8P8;
71 case WINED3DFMT_P8_UINT: return D3DFMT_P8;
72 case WINED3DFMT_L8_UNORM: return D3DFMT_L8;
73 case WINED3DFMT_L8A8_UNORM: return D3DFMT_A8L8;
74 case WINED3DFMT_L4A4_UNORM: return D3DFMT_A4L4;
75 case WINED3DFMT_R8G8_SNORM: return D3DFMT_V8U8;
76 case WINED3DFMT_R5G5_SNORM_L6_UNORM: return D3DFMT_L6V5U5;
77 case WINED3DFMT_R8G8_SNORM_L8X8_UNORM: return D3DFMT_X8L8V8U8;
78 case WINED3DFMT_R8G8B8A8_SNORM: return D3DFMT_Q8W8V8U8;
79 case WINED3DFMT_R16G16_SNORM: return D3DFMT_V16U16;
80 case WINED3DFMT_R10G10B10_SNORM_A2_UNORM: return D3DFMT_A2W10V10U10;
81 case WINED3DFMT_D16_LOCKABLE: return D3DFMT_D16_LOCKABLE;
82 case WINED3DFMT_D32_UNORM: return D3DFMT_D32;
83 case WINED3DFMT_S1_UINT_D15_UNORM: return D3DFMT_D15S1;
84 case WINED3DFMT_D24_UNORM_S8_UINT: return D3DFMT_D24S8;
85 case WINED3DFMT_X8D24_UNORM: return D3DFMT_D24X8;
86 case WINED3DFMT_S4X4_UINT_D24_UNORM: return D3DFMT_D24X4S4;
87 case WINED3DFMT_D16_UNORM: return D3DFMT_D16;
88 case WINED3DFMT_L16_UNORM: return D3DFMT_L16;
89 case WINED3DFMT_D32_FLOAT: return D3DFMT_D32F_LOCKABLE;
90 case WINED3DFMT_S8_UINT_D24_FLOAT: return D3DFMT_D24FS8;
91 case WINED3DFMT_R16_UINT: return D3DFMT_INDEX16;
92 case WINED3DFMT_R32_UINT: return D3DFMT_INDEX32;
93 case WINED3DFMT_R16G16B16A16_SNORM: return D3DFMT_Q16W16V16U16;
94 case WINED3DFMT_R16_FLOAT: return D3DFMT_R16F;
95 case WINED3DFMT_R16G16_FLOAT: return D3DFMT_G16R16F;
96 case WINED3DFMT_R16G16B16A16_FLOAT: return D3DFMT_A16B16G16R16F;
97 case WINED3DFMT_R32_FLOAT: return D3DFMT_R32F;
98 case WINED3DFMT_R32G32_FLOAT: return D3DFMT_G32R32F;
99 case WINED3DFMT_R32G32B32A32_FLOAT: return D3DFMT_A32B32G32R32F;
100 case WINED3DFMT_R8G8_SNORM_Cx: return D3DFMT_CxV8U8;
101 default:
102 FIXME("Unhandled wined3d format %#x.\n", format);
103 return D3DFMT_UNKNOWN;
107 enum wined3d_format_id wined3dformat_from_d3dformat(D3DFORMAT format)
109 BYTE *c = (BYTE *)&format;
111 /* Don't translate FOURCC formats */
112 if (isprint(c[0]) && isprint(c[1]) && isprint(c[2]) && isprint(c[3]))
113 return (enum wined3d_format_id)format;
115 switch(format)
117 case D3DFMT_UNKNOWN: return WINED3DFMT_UNKNOWN;
118 case D3DFMT_R8G8B8: return WINED3DFMT_B8G8R8_UNORM;
119 case D3DFMT_A8R8G8B8: return WINED3DFMT_B8G8R8A8_UNORM;
120 case D3DFMT_X8R8G8B8: return WINED3DFMT_B8G8R8X8_UNORM;
121 case D3DFMT_R5G6B5: return WINED3DFMT_B5G6R5_UNORM;
122 case D3DFMT_X1R5G5B5: return WINED3DFMT_B5G5R5X1_UNORM;
123 case D3DFMT_A1R5G5B5: return WINED3DFMT_B5G5R5A1_UNORM;
124 case D3DFMT_A4R4G4B4: return WINED3DFMT_B4G4R4A4_UNORM;
125 case D3DFMT_R3G3B2: return WINED3DFMT_B2G3R3_UNORM;
126 case D3DFMT_A8: return WINED3DFMT_A8_UNORM;
127 case D3DFMT_A8R3G3B2: return WINED3DFMT_B2G3R3A8_UNORM;
128 case D3DFMT_X4R4G4B4: return WINED3DFMT_B4G4R4X4_UNORM;
129 case D3DFMT_A2B10G10R10: return WINED3DFMT_R10G10B10A2_UNORM;
130 case D3DFMT_A8B8G8R8: return WINED3DFMT_R8G8B8A8_UNORM;
131 case D3DFMT_X8B8G8R8: return WINED3DFMT_R8G8B8X8_UNORM;
132 case D3DFMT_G16R16: return WINED3DFMT_R16G16_UNORM;
133 case D3DFMT_A2R10G10B10: return WINED3DFMT_B10G10R10A2_UNORM;
134 case D3DFMT_A16B16G16R16: return WINED3DFMT_R16G16B16A16_UNORM;
135 case D3DFMT_A8P8: return WINED3DFMT_P8_UINT_A8_UNORM;
136 case D3DFMT_P8: return WINED3DFMT_P8_UINT;
137 case D3DFMT_L8: return WINED3DFMT_L8_UNORM;
138 case D3DFMT_A8L8: return WINED3DFMT_L8A8_UNORM;
139 case D3DFMT_A4L4: return WINED3DFMT_L4A4_UNORM;
140 case D3DFMT_V8U8: return WINED3DFMT_R8G8_SNORM;
141 case D3DFMT_L6V5U5: return WINED3DFMT_R5G5_SNORM_L6_UNORM;
142 case D3DFMT_X8L8V8U8: return WINED3DFMT_R8G8_SNORM_L8X8_UNORM;
143 case D3DFMT_Q8W8V8U8: return WINED3DFMT_R8G8B8A8_SNORM;
144 case D3DFMT_V16U16: return WINED3DFMT_R16G16_SNORM;
145 case D3DFMT_A2W10V10U10: return WINED3DFMT_R10G10B10_SNORM_A2_UNORM;
146 case D3DFMT_D16_LOCKABLE: return WINED3DFMT_D16_LOCKABLE;
147 case D3DFMT_D32: return WINED3DFMT_D32_UNORM;
148 case D3DFMT_D15S1: return WINED3DFMT_S1_UINT_D15_UNORM;
149 case D3DFMT_D24S8: return WINED3DFMT_D24_UNORM_S8_UINT;
150 case D3DFMT_D24X8: return WINED3DFMT_X8D24_UNORM;
151 case D3DFMT_D24X4S4: return WINED3DFMT_S4X4_UINT_D24_UNORM;
152 case D3DFMT_D16: return WINED3DFMT_D16_UNORM;
153 case D3DFMT_L16: return WINED3DFMT_L16_UNORM;
154 case D3DFMT_D32F_LOCKABLE: return WINED3DFMT_D32_FLOAT;
155 case D3DFMT_D24FS8: return WINED3DFMT_S8_UINT_D24_FLOAT;
156 case D3DFMT_INDEX16: return WINED3DFMT_R16_UINT;
157 case D3DFMT_INDEX32: return WINED3DFMT_R32_UINT;
158 case D3DFMT_Q16W16V16U16: return WINED3DFMT_R16G16B16A16_SNORM;
159 case D3DFMT_R16F: return WINED3DFMT_R16_FLOAT;
160 case D3DFMT_G16R16F: return WINED3DFMT_R16G16_FLOAT;
161 case D3DFMT_A16B16G16R16F: return WINED3DFMT_R16G16B16A16_FLOAT;
162 case D3DFMT_R32F: return WINED3DFMT_R32_FLOAT;
163 case D3DFMT_G32R32F: return WINED3DFMT_R32G32_FLOAT;
164 case D3DFMT_A32B32G32R32F: return WINED3DFMT_R32G32B32A32_FLOAT;
165 case D3DFMT_CxV8U8: return WINED3DFMT_R8G8_SNORM_Cx;
166 default:
167 FIXME("Unhandled D3DFORMAT %#x.\n", format);
168 return WINED3DFMT_UNKNOWN;
172 unsigned int wined3dmapflags_from_d3dmapflags(unsigned int flags, unsigned int usage)
174 static const unsigned int handled = D3DLOCK_NOSYSLOCK
175 | D3DLOCK_NOOVERWRITE
176 | D3DLOCK_DISCARD
177 | D3DLOCK_DONOTWAIT
178 | D3DLOCK_NO_DIRTY_UPDATE;
179 unsigned int wined3d_flags;
181 wined3d_flags = flags & handled;
182 if (~usage & D3DUSAGE_WRITEONLY && !(flags & (D3DLOCK_NOOVERWRITE | D3DLOCK_DISCARD)))
183 wined3d_flags |= WINED3D_MAP_READ;
184 if (!(flags & D3DLOCK_READONLY))
185 wined3d_flags |= WINED3D_MAP_WRITE;
186 if (!(wined3d_flags & (WINED3D_MAP_READ | WINED3D_MAP_WRITE)))
187 wined3d_flags |= WINED3D_MAP_WRITE;
188 flags &= ~(handled | D3DLOCK_READONLY);
190 if (flags)
191 FIXME("Unhandled flags %#x.\n", flags);
193 return wined3d_flags;
196 static UINT vertex_count_from_primitive_count(D3DPRIMITIVETYPE primitive_type, UINT primitive_count)
198 switch (primitive_type)
200 case D3DPT_POINTLIST:
201 return primitive_count;
203 case D3DPT_LINELIST:
204 return primitive_count * 2;
206 case D3DPT_LINESTRIP:
207 return primitive_count + 1;
209 case D3DPT_TRIANGLELIST:
210 return primitive_count * 3;
212 case D3DPT_TRIANGLESTRIP:
213 case D3DPT_TRIANGLEFAN:
214 return primitive_count + 2;
216 default:
217 FIXME("Unhandled primitive type %#x.\n", primitive_type);
218 return 0;
222 static D3DSWAPEFFECT d3dswapeffect_from_wined3dswapeffect(enum wined3d_swap_effect effect)
224 switch (effect)
226 case WINED3D_SWAP_EFFECT_DISCARD:
227 return D3DSWAPEFFECT_DISCARD;
228 case WINED3D_SWAP_EFFECT_SEQUENTIAL:
229 return D3DSWAPEFFECT_FLIP;
230 case WINED3D_SWAP_EFFECT_COPY:
231 return D3DSWAPEFFECT_COPY;
232 case WINED3D_SWAP_EFFECT_OVERLAY:
233 return D3DSWAPEFFECT_OVERLAY;
234 case WINED3D_SWAP_EFFECT_FLIP_SEQUENTIAL:
235 return D3DSWAPEFFECT_FLIPEX;
236 default:
237 FIXME("Unhandled swap effect %#x.\n", effect);
238 return D3DSWAPEFFECT_FLIP;
242 void present_parameters_from_wined3d_swapchain_desc(D3DPRESENT_PARAMETERS *present_parameters,
243 const struct wined3d_swapchain_desc *swapchain_desc, DWORD presentation_interval)
245 present_parameters->BackBufferWidth = swapchain_desc->backbuffer_width;
246 present_parameters->BackBufferHeight = swapchain_desc->backbuffer_height;
247 present_parameters->BackBufferFormat = d3dformat_from_wined3dformat(swapchain_desc->backbuffer_format);
248 present_parameters->BackBufferCount = swapchain_desc->backbuffer_count;
249 present_parameters->MultiSampleType = d3dmultisample_type_from_wined3d(swapchain_desc->multisample_type);
250 present_parameters->MultiSampleQuality = swapchain_desc->multisample_quality;
251 present_parameters->SwapEffect = d3dswapeffect_from_wined3dswapeffect(swapchain_desc->swap_effect);
252 present_parameters->hDeviceWindow = swapchain_desc->device_window;
253 present_parameters->Windowed = swapchain_desc->windowed;
254 present_parameters->EnableAutoDepthStencil = swapchain_desc->enable_auto_depth_stencil;
255 present_parameters->AutoDepthStencilFormat
256 = d3dformat_from_wined3dformat(swapchain_desc->auto_depth_stencil_format);
257 present_parameters->Flags = swapchain_desc->flags & D3DPRESENTFLAGS_MASK;
258 present_parameters->FullScreen_RefreshRateInHz = swapchain_desc->refresh_rate;
259 present_parameters->PresentationInterval = presentation_interval;
262 static enum wined3d_swap_effect wined3dswapeffect_from_d3dswapeffect(D3DSWAPEFFECT effect)
264 switch (effect)
266 case D3DSWAPEFFECT_DISCARD:
267 return WINED3D_SWAP_EFFECT_DISCARD;
268 case D3DSWAPEFFECT_FLIP:
269 return WINED3D_SWAP_EFFECT_SEQUENTIAL;
270 case D3DSWAPEFFECT_COPY:
271 return WINED3D_SWAP_EFFECT_COPY;
272 case D3DSWAPEFFECT_OVERLAY:
273 return WINED3D_SWAP_EFFECT_OVERLAY;
274 case D3DSWAPEFFECT_FLIPEX:
275 return WINED3D_SWAP_EFFECT_FLIP_SEQUENTIAL;
276 default:
277 FIXME("Unhandled swap effect %#x.\n", effect);
278 return WINED3D_SWAP_EFFECT_SEQUENTIAL;
282 static enum wined3d_swap_interval wined3dswapinterval_from_d3d(DWORD interval)
284 switch (interval)
286 case D3DPRESENT_INTERVAL_IMMEDIATE:
287 return WINED3D_SWAP_INTERVAL_IMMEDIATE;
288 case D3DPRESENT_INTERVAL_ONE:
289 return WINED3D_SWAP_INTERVAL_ONE;
290 case D3DPRESENT_INTERVAL_TWO:
291 return WINED3D_SWAP_INTERVAL_TWO;
292 case D3DPRESENT_INTERVAL_THREE:
293 return WINED3D_SWAP_INTERVAL_THREE;
294 case D3DPRESENT_INTERVAL_FOUR:
295 return WINED3D_SWAP_INTERVAL_FOUR;
296 default:
297 FIXME("Unhandled presentation interval %#x.\n", interval);
298 case D3DPRESENT_INTERVAL_DEFAULT:
299 return WINED3D_SWAP_INTERVAL_DEFAULT;
303 static BOOL wined3d_swapchain_desc_from_d3d9(struct wined3d_swapchain_desc *swapchain_desc,
304 struct wined3d_output *output, const D3DPRESENT_PARAMETERS *present_parameters,
305 BOOL extended)
307 D3DSWAPEFFECT highest_swapeffect = extended ? D3DSWAPEFFECT_FLIPEX : D3DSWAPEFFECT_COPY;
308 UINT highest_bb_count = extended ? 30 : 3;
310 if (!present_parameters->SwapEffect || present_parameters->SwapEffect > highest_swapeffect)
312 WARN("Invalid swap effect %u passed.\n", present_parameters->SwapEffect);
313 return FALSE;
315 if (present_parameters->BackBufferCount > highest_bb_count
316 || (present_parameters->SwapEffect == D3DSWAPEFFECT_COPY
317 && present_parameters->BackBufferCount > 1))
319 WARN("Invalid backbuffer count %u.\n", present_parameters->BackBufferCount);
320 return FALSE;
322 switch (present_parameters->PresentationInterval)
324 case D3DPRESENT_INTERVAL_DEFAULT:
325 case D3DPRESENT_INTERVAL_ONE:
326 case D3DPRESENT_INTERVAL_TWO:
327 case D3DPRESENT_INTERVAL_THREE:
328 case D3DPRESENT_INTERVAL_FOUR:
329 case D3DPRESENT_INTERVAL_IMMEDIATE:
330 break;
331 default:
332 WARN("Invalid presentation interval %#x.\n", present_parameters->PresentationInterval);
333 return FALSE;
336 swapchain_desc->output = output;
337 swapchain_desc->backbuffer_width = present_parameters->BackBufferWidth;
338 swapchain_desc->backbuffer_height = present_parameters->BackBufferHeight;
339 swapchain_desc->backbuffer_format = wined3dformat_from_d3dformat(present_parameters->BackBufferFormat);
340 swapchain_desc->backbuffer_count = max(1, present_parameters->BackBufferCount);
341 swapchain_desc->backbuffer_bind_flags = WINED3D_BIND_RENDER_TARGET;
342 swapchain_desc->multisample_type = wined3d_multisample_type_from_d3d(present_parameters->MultiSampleType);
343 swapchain_desc->multisample_quality = present_parameters->MultiSampleQuality;
344 swapchain_desc->swap_effect = wined3dswapeffect_from_d3dswapeffect(present_parameters->SwapEffect);
345 swapchain_desc->device_window = present_parameters->hDeviceWindow;
346 swapchain_desc->windowed = present_parameters->Windowed;
347 swapchain_desc->enable_auto_depth_stencil = present_parameters->EnableAutoDepthStencil;
348 swapchain_desc->auto_depth_stencil_format
349 = wined3dformat_from_d3dformat(present_parameters->AutoDepthStencilFormat);
350 swapchain_desc->flags
351 = (present_parameters->Flags & D3DPRESENTFLAGS_MASK) | WINED3D_SWAPCHAIN_ALLOW_MODE_SWITCH;
352 if (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 %#x.\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 %#x.\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 %u.\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 %u.\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 if (device->vertex_buffer)
667 wined3d_buffer_decref(device->vertex_buffer);
668 if (device->index_buffer)
669 wined3d_buffer_decref(device->index_buffer);
671 for (i = 0; i < device->implicit_swapchain_count; ++i)
673 wined3d_swapchain_decref(device->implicit_swapchains[i]);
675 heap_free(device->implicit_swapchains);
677 if (device->recording)
678 wined3d_stateblock_decref(device->recording);
679 wined3d_stateblock_decref(device->state);
681 wined3d_device_release_focus_window(device->wined3d_device);
682 wined3d_device_decref(device->wined3d_device);
683 wined3d_mutex_unlock();
685 IDirect3D9Ex_Release(&device->d3d_parent->IDirect3D9Ex_iface);
687 heap_free(device);
690 return refcount;
693 static HRESULT WINAPI d3d9_device_TestCooperativeLevel(IDirect3DDevice9Ex *iface)
695 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
697 TRACE("iface %p.\n", iface);
699 TRACE("device state: %#x.\n", device->device_state);
701 if (device->d3d_parent->extended)
702 return D3D_OK;
704 switch (device->device_state)
706 default:
707 case D3D9_DEVICE_STATE_OK:
708 return D3D_OK;
709 case D3D9_DEVICE_STATE_LOST:
710 return D3DERR_DEVICELOST;
711 case D3D9_DEVICE_STATE_NOT_RESET:
712 return D3DERR_DEVICENOTRESET;
716 static UINT WINAPI d3d9_device_GetAvailableTextureMem(IDirect3DDevice9Ex *iface)
718 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
719 UINT ret;
721 TRACE("iface %p.\n", iface);
723 wined3d_mutex_lock();
724 ret = wined3d_device_get_available_texture_mem(device->wined3d_device);
725 wined3d_mutex_unlock();
727 return ret;
730 static HRESULT WINAPI d3d9_device_EvictManagedResources(IDirect3DDevice9Ex *iface)
732 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
734 TRACE("iface %p.\n", iface);
736 wined3d_mutex_lock();
737 wined3d_device_evict_managed_resources(device->wined3d_device);
738 wined3d_mutex_unlock();
740 return D3D_OK;
743 static HRESULT WINAPI d3d9_device_GetDirect3D(IDirect3DDevice9Ex *iface, IDirect3D9 **d3d9)
745 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
747 TRACE("iface %p, d3d9 %p.\n", iface, d3d9);
749 if (!d3d9)
750 return D3DERR_INVALIDCALL;
752 return IDirect3D9Ex_QueryInterface(&device->d3d_parent->IDirect3D9Ex_iface, &IID_IDirect3D9, (void **)d3d9);
755 static HRESULT WINAPI d3d9_device_GetDeviceCaps(IDirect3DDevice9Ex *iface, D3DCAPS9 *caps)
757 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
758 struct wined3d_caps wined3d_caps;
759 HRESULT hr;
761 TRACE("iface %p, caps %p.\n", iface, caps);
763 if (!caps)
764 return D3DERR_INVALIDCALL;
766 memset(caps, 0, sizeof(*caps));
768 wined3d_mutex_lock();
769 hr = wined3d_device_get_device_caps(device->wined3d_device, &wined3d_caps);
770 wined3d_mutex_unlock();
772 d3d9_caps_from_wined3dcaps(device->d3d_parent, device->adapter_ordinal, caps, &wined3d_caps);
774 return hr;
777 static HRESULT WINAPI d3d9_device_GetDisplayMode(IDirect3DDevice9Ex *iface, UINT swapchain, D3DDISPLAYMODE *mode)
779 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
780 struct wined3d_display_mode wined3d_mode;
781 HRESULT hr;
783 TRACE("iface %p, swapchain %u, mode %p.\n", iface, swapchain, mode);
785 wined3d_mutex_lock();
786 hr = wined3d_device_get_display_mode(device->wined3d_device, swapchain, &wined3d_mode, NULL);
787 wined3d_mutex_unlock();
789 if (SUCCEEDED(hr))
791 mode->Width = wined3d_mode.width;
792 mode->Height = wined3d_mode.height;
793 mode->RefreshRate = wined3d_mode.refresh_rate;
794 mode->Format = d3dformat_from_wined3dformat(wined3d_mode.format_id);
797 return hr;
800 static HRESULT WINAPI d3d9_device_GetCreationParameters(IDirect3DDevice9Ex *iface,
801 D3DDEVICE_CREATION_PARAMETERS *parameters)
803 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
805 TRACE("iface %p, parameters %p.\n", iface, parameters);
807 wined3d_mutex_lock();
808 wined3d_device_get_creation_parameters(device->wined3d_device,
809 (struct wined3d_device_creation_parameters *)parameters);
810 wined3d_mutex_unlock();
812 parameters->AdapterOrdinal = device->adapter_ordinal;
813 return D3D_OK;
816 static HRESULT WINAPI d3d9_device_SetCursorProperties(IDirect3DDevice9Ex *iface,
817 UINT hotspot_x, UINT hotspot_y, IDirect3DSurface9 *bitmap)
819 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
820 struct d3d9_surface *bitmap_impl = unsafe_impl_from_IDirect3DSurface9(bitmap);
821 D3DSURFACE_DESC surface_desc;
822 D3DDISPLAYMODE mode;
823 HRESULT hr;
825 TRACE("iface %p, hotspot_x %u, hotspot_y %u, bitmap %p.\n",
826 iface, hotspot_x, hotspot_y, bitmap);
828 if (!bitmap)
830 WARN("No cursor bitmap, returning D3DERR_INVALIDCALL.\n");
831 return D3DERR_INVALIDCALL;
834 if (FAILED(hr = IDirect3DSurface9_GetDesc(bitmap, &surface_desc)))
836 WARN("Failed to get surface description, hr %#x.\n", hr);
837 return hr;
840 if (FAILED(hr = IDirect3D9_GetAdapterDisplayMode(&device->d3d_parent->IDirect3D9Ex_iface,
841 device->adapter_ordinal, &mode)))
843 WARN("Failed to get device display mode, hr %#x.\n", hr);
844 return hr;
847 if (surface_desc.Width > mode.Width || surface_desc.Height > mode.Height)
849 WARN("Surface dimension %ux%u exceeds display mode %ux%u.\n", surface_desc.Width,
850 surface_desc.Height, mode.Width, mode.Height);
851 return D3DERR_INVALIDCALL;
854 wined3d_mutex_lock();
855 hr = wined3d_device_set_cursor_properties(device->wined3d_device,
856 hotspot_x, hotspot_y, bitmap_impl->wined3d_texture, bitmap_impl->sub_resource_idx);
857 wined3d_mutex_unlock();
859 return hr;
862 static void WINAPI d3d9_device_SetCursorPosition(IDirect3DDevice9Ex *iface, int x, int y, DWORD flags)
864 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
866 TRACE("iface %p, x %u, y %u, flags %#x.\n", iface, x, y, flags);
868 wined3d_mutex_lock();
869 wined3d_device_set_cursor_position(device->wined3d_device, x, y, flags);
870 wined3d_mutex_unlock();
873 static BOOL WINAPI d3d9_device_ShowCursor(IDirect3DDevice9Ex *iface, BOOL show)
875 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
876 BOOL ret;
878 TRACE("iface %p, show %#x.\n", iface, show);
880 wined3d_mutex_lock();
881 ret = wined3d_device_show_cursor(device->wined3d_device, show);
882 wined3d_mutex_unlock();
884 return ret;
887 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_CreateAdditionalSwapChain(IDirect3DDevice9Ex *iface,
888 D3DPRESENT_PARAMETERS *present_parameters, IDirect3DSwapChain9 **swapchain)
890 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
891 struct wined3d_device_creation_parameters creation_parameters;
892 struct wined3d_swapchain_desc desc;
893 struct d3d9_swapchain *object;
894 unsigned int swap_interval;
895 unsigned int output_idx;
896 unsigned int i, count;
897 HRESULT hr;
899 TRACE("iface %p, present_parameters %p, swapchain %p.\n",
900 iface, present_parameters, swapchain);
902 if (!present_parameters->Windowed)
904 WARN("Trying to create an additional fullscreen swapchain, returning D3DERR_INVALIDCALL.\n");
905 return D3DERR_INVALIDCALL;
908 wined3d_mutex_lock();
909 count = wined3d_device_get_swapchain_count(device->wined3d_device);
910 for (i = 0; i < count; ++i)
912 struct wined3d_swapchain *wined3d_swapchain;
914 wined3d_swapchain = wined3d_device_get_swapchain(device->wined3d_device, i);
915 wined3d_swapchain_get_desc(wined3d_swapchain, &desc);
917 if (!desc.windowed)
919 wined3d_mutex_unlock();
920 WARN("Trying to create an additional swapchain in fullscreen mode, returning D3DERR_INVALIDCALL.\n");
921 return D3DERR_INVALIDCALL;
924 wined3d_mutex_unlock();
926 output_idx = device->adapter_ordinal;
927 if (!wined3d_swapchain_desc_from_d3d9(&desc, device->d3d_parent->wined3d_outputs[output_idx],
928 present_parameters, device->d3d_parent->extended))
929 return D3DERR_INVALIDCALL;
930 wined3d_device_get_creation_parameters(device->wined3d_device, &creation_parameters);
931 if (creation_parameters.flags & WINED3DCREATE_NOWINDOWCHANGES)
932 desc.flags |= WINED3D_SWAPCHAIN_NO_WINDOW_CHANGES;
933 swap_interval = wined3dswapinterval_from_d3d(present_parameters->PresentationInterval);
934 if (SUCCEEDED(hr = d3d9_swapchain_create(device, &desc, swap_interval, &object)))
935 *swapchain = (IDirect3DSwapChain9 *)&object->IDirect3DSwapChain9Ex_iface;
936 present_parameters_from_wined3d_swapchain_desc(present_parameters,
937 &desc, present_parameters->PresentationInterval);
939 return hr;
942 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_GetSwapChain(IDirect3DDevice9Ex *iface,
943 UINT swapchain_idx, IDirect3DSwapChain9 **swapchain)
945 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
946 struct d3d9_swapchain *d3d9_swapchain;
947 HRESULT hr;
949 TRACE("iface %p, swapchain_idx %u, swapchain %p.\n", iface, swapchain_idx, swapchain);
951 wined3d_mutex_lock();
952 if (swapchain_idx < device->implicit_swapchain_count)
954 d3d9_swapchain = wined3d_swapchain_get_parent(device->implicit_swapchains[swapchain_idx]);
955 *swapchain = (IDirect3DSwapChain9 *)&d3d9_swapchain->IDirect3DSwapChain9Ex_iface;
956 IDirect3DSwapChain9Ex_AddRef(*swapchain);
957 hr = D3D_OK;
959 else
961 *swapchain = NULL;
962 hr = D3DERR_INVALIDCALL;
964 wined3d_mutex_unlock();
966 return hr;
969 static UINT WINAPI d3d9_device_GetNumberOfSwapChains(IDirect3DDevice9Ex *iface)
971 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
972 UINT count;
974 TRACE("iface %p.\n", iface);
976 wined3d_mutex_lock();
977 count = wined3d_device_get_swapchain_count(device->wined3d_device);
978 wined3d_mutex_unlock();
980 return count;
983 static HRESULT CDECL reset_enum_callback(struct wined3d_resource *resource)
985 struct d3d9_vertexbuffer *vertex_buffer;
986 struct d3d9_indexbuffer *index_buffer;
987 struct wined3d_resource_desc desc;
988 IDirect3DBaseTexture9 *texture;
989 struct d3d9_surface *surface;
990 IUnknown *parent;
992 wined3d_resource_get_desc(resource, &desc);
993 if (desc.access & WINED3D_RESOURCE_ACCESS_CPU)
994 return D3D_OK;
996 if (desc.resource_type != WINED3D_RTYPE_TEXTURE_2D)
998 if (desc.bind_flags & WINED3D_BIND_VERTEX_BUFFER)
1000 vertex_buffer = wined3d_resource_get_parent(resource);
1001 if (vertex_buffer && vertex_buffer->draw_buffer)
1002 return D3D_OK;
1004 if (desc.bind_flags & WINED3D_BIND_INDEX_BUFFER)
1006 index_buffer = wined3d_resource_get_parent(resource);
1007 if (index_buffer && index_buffer->draw_buffer)
1008 return D3D_OK;
1011 WARN("Resource %p in pool D3DPOOL_DEFAULT blocks the Reset call.\n", resource);
1012 return D3DERR_INVALIDCALL;
1015 parent = wined3d_resource_get_parent(resource);
1016 if (parent && SUCCEEDED(IUnknown_QueryInterface(parent, &IID_IDirect3DBaseTexture9, (void **)&texture)))
1018 IDirect3DBaseTexture9_Release(texture);
1019 WARN("Texture %p (resource %p) in pool D3DPOOL_DEFAULT blocks the Reset call.\n", texture, resource);
1020 return D3DERR_INVALIDCALL;
1023 surface = wined3d_texture_get_sub_resource_parent(wined3d_texture_from_resource(resource), 0);
1024 if (!surface->resource.refcount)
1025 return D3D_OK;
1027 WARN("Surface %p in pool D3DPOOL_DEFAULT blocks the Reset call.\n", surface);
1028 return D3DERR_INVALIDCALL;
1031 static HRESULT d3d9_device_get_swapchains(struct d3d9_device *device)
1033 UINT i, new_swapchain_count = wined3d_device_get_swapchain_count(device->wined3d_device);
1035 if (!(device->implicit_swapchains = heap_alloc(new_swapchain_count * sizeof(*device->implicit_swapchains))))
1036 return E_OUTOFMEMORY;
1038 for (i = 0; i < new_swapchain_count; ++i)
1040 device->implicit_swapchains[i] = wined3d_device_get_swapchain(device->wined3d_device, i);
1042 device->implicit_swapchain_count = new_swapchain_count;
1044 return D3D_OK;
1047 static HRESULT d3d9_device_reset(struct d3d9_device *device,
1048 D3DPRESENT_PARAMETERS *present_parameters, D3DDISPLAYMODEEX *mode)
1050 BOOL extended = device->d3d_parent->extended;
1051 struct wined3d_swapchain_desc swapchain_desc;
1052 struct wined3d_display_mode wined3d_mode;
1053 struct wined3d_rendertarget_view *rtv;
1054 struct d3d9_swapchain *d3d9_swapchain;
1055 unsigned int output_idx;
1056 unsigned int i;
1057 HRESULT hr;
1059 if (!extended && device->device_state == D3D9_DEVICE_STATE_LOST)
1061 WARN("App not active, returning D3DERR_DEVICELOST.\n");
1062 return D3DERR_DEVICELOST;
1065 if (mode)
1067 wined3d_mode.width = mode->Width;
1068 wined3d_mode.height = mode->Height;
1069 wined3d_mode.refresh_rate = mode->RefreshRate;
1070 wined3d_mode.format_id = wined3dformat_from_d3dformat(mode->Format);
1071 wined3d_mode.scanline_ordering = wined3d_scanline_ordering_from_d3d(mode->ScanLineOrdering);
1074 output_idx = device->adapter_ordinal;
1075 if (!wined3d_swapchain_desc_from_d3d9(&swapchain_desc,
1076 device->d3d_parent->wined3d_outputs[output_idx], present_parameters, extended))
1077 return D3DERR_INVALIDCALL;
1078 swapchain_desc.flags |= WINED3D_SWAPCHAIN_IMPLICIT;
1080 wined3d_mutex_lock();
1082 if (device->vertex_buffer)
1084 wined3d_buffer_decref(device->vertex_buffer);
1085 device->vertex_buffer = NULL;
1086 device->vertex_buffer_size = 0;
1089 if (device->index_buffer)
1091 wined3d_buffer_decref(device->index_buffer);
1092 device->index_buffer = NULL;
1093 device->index_buffer_size = 0;
1096 if (!extended)
1098 if (device->recording)
1099 wined3d_stateblock_decref(device->recording);
1100 device->recording = NULL;
1101 device->update_state = device->state;
1102 wined3d_stateblock_reset(device->state);
1105 if (SUCCEEDED(hr = wined3d_device_reset(device->wined3d_device, &swapchain_desc,
1106 mode ? &wined3d_mode : NULL, reset_enum_callback, !extended)))
1108 heap_free(device->implicit_swapchains);
1110 if (!extended)
1112 device->auto_mipmaps = 0;
1113 wined3d_stateblock_set_render_state(device->state, WINED3D_RS_ZENABLE,
1114 !!swapchain_desc.enable_auto_depth_stencil);
1115 device_reset_viewport_state(device);
1118 if (FAILED(hr = d3d9_device_get_swapchains(device)))
1120 device->device_state = D3D9_DEVICE_STATE_NOT_RESET;
1122 else
1124 d3d9_swapchain = wined3d_swapchain_get_parent(device->implicit_swapchains[0]);
1125 d3d9_swapchain->swap_interval
1126 = wined3dswapinterval_from_d3d(present_parameters->PresentationInterval);
1127 wined3d_swapchain_get_desc(d3d9_swapchain->wined3d_swapchain, &swapchain_desc);
1128 present_parameters->BackBufferWidth = swapchain_desc.backbuffer_width;
1129 present_parameters->BackBufferHeight = swapchain_desc.backbuffer_height;
1130 present_parameters->BackBufferFormat = d3dformat_from_wined3dformat(swapchain_desc.backbuffer_format);
1131 present_parameters->BackBufferCount = swapchain_desc.backbuffer_count;
1133 device->device_state = D3D9_DEVICE_STATE_OK;
1135 if (extended)
1137 const struct wined3d_viewport *current = &device->stateblock_state->viewport;
1138 struct wined3d_viewport vp;
1139 RECT rect;
1141 vp.x = 0;
1142 vp.y = 0;
1143 vp.width = swapchain_desc.backbuffer_width;
1144 vp.height = swapchain_desc.backbuffer_height;
1145 vp.min_z = current->min_z;
1146 vp.max_z = current->max_z;
1147 wined3d_stateblock_set_viewport(device->state, &vp);
1149 SetRect(&rect, 0, 0, swapchain_desc.backbuffer_width, swapchain_desc.backbuffer_height);
1150 wined3d_stateblock_set_scissor_rect(device->state, &rect);
1154 rtv = wined3d_device_context_get_rendertarget_view(device->immediate_context, 0);
1155 device->render_targets[0] = wined3d_rendertarget_view_get_sub_resource_parent(rtv);
1156 for (i = 1; i < ARRAY_SIZE(device->render_targets); ++i)
1157 device->render_targets[i] = NULL;
1159 else if (!extended)
1161 device->device_state = D3D9_DEVICE_STATE_NOT_RESET;
1164 wined3d_mutex_unlock();
1166 return hr;
1169 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_Reset(IDirect3DDevice9Ex *iface,
1170 D3DPRESENT_PARAMETERS *present_parameters)
1172 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1174 TRACE("iface %p, present_parameters %p.\n", iface, present_parameters);
1176 return d3d9_device_reset(device, present_parameters, NULL);
1179 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_Present(IDirect3DDevice9Ex *iface,
1180 const RECT *src_rect, const RECT *dst_rect, HWND dst_window_override, const RGNDATA *dirty_region)
1182 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1183 struct d3d9_swapchain *swapchain;
1184 unsigned int i;
1185 HRESULT hr;
1187 TRACE("iface %p, src_rect %s, dst_rect %s, dst_window_override %p, dirty_region %p.\n",
1188 iface, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect), dst_window_override, dirty_region);
1190 if (device->device_state != D3D9_DEVICE_STATE_OK)
1191 return device->d3d_parent->extended ? S_PRESENT_OCCLUDED : D3DERR_DEVICELOST;
1193 if (dirty_region)
1194 FIXME("Ignoring dirty_region %p.\n", dirty_region);
1196 wined3d_mutex_lock();
1197 for (i = 0; i < device->implicit_swapchain_count; ++i)
1199 swapchain = wined3d_swapchain_get_parent(device->implicit_swapchains[i]);
1200 if (FAILED(hr = wined3d_swapchain_present(swapchain->wined3d_swapchain,
1201 src_rect, dst_rect, dst_window_override, swapchain->swap_interval, 0)))
1203 wined3d_mutex_unlock();
1204 return hr;
1207 wined3d_mutex_unlock();
1209 return D3D_OK;
1212 static HRESULT WINAPI d3d9_device_GetBackBuffer(IDirect3DDevice9Ex *iface, UINT swapchain,
1213 UINT backbuffer_idx, D3DBACKBUFFER_TYPE backbuffer_type, IDirect3DSurface9 **backbuffer)
1215 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1216 struct d3d9_swapchain *d3d9_swapchain;
1217 HRESULT hr;
1219 TRACE("iface %p, swapchain %u, backbuffer_idx %u, backbuffer_type %#x, backbuffer %p.\n",
1220 iface, swapchain, backbuffer_idx, backbuffer_type, backbuffer);
1222 /* backbuffer_type is ignored by native. */
1224 /* No need to check for backbuffer == NULL, Windows crashes in that case. */
1225 *backbuffer = NULL;
1227 wined3d_mutex_lock();
1228 if (swapchain >= device->implicit_swapchain_count)
1230 wined3d_mutex_unlock();
1231 WARN("Swapchain index %u is out of range, returning D3DERR_INVALIDCALL.\n", swapchain);
1232 return D3DERR_INVALIDCALL;
1235 d3d9_swapchain = wined3d_swapchain_get_parent(device->implicit_swapchains[swapchain]);
1236 hr = IDirect3DSwapChain9Ex_GetBackBuffer(&d3d9_swapchain->IDirect3DSwapChain9Ex_iface,
1237 backbuffer_idx, backbuffer_type, backbuffer);
1238 wined3d_mutex_unlock();
1240 return hr;
1243 static HRESULT WINAPI d3d9_device_GetRasterStatus(IDirect3DDevice9Ex *iface,
1244 UINT swapchain, D3DRASTER_STATUS *raster_status)
1246 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1247 HRESULT hr;
1249 TRACE("iface %p, swapchain %u, raster_status %p.\n", iface, swapchain, raster_status);
1251 wined3d_mutex_lock();
1252 hr = wined3d_device_get_raster_status(device->wined3d_device,
1253 swapchain, (struct wined3d_raster_status *)raster_status);
1254 wined3d_mutex_unlock();
1256 return hr;
1259 static HRESULT WINAPI d3d9_device_SetDialogBoxMode(IDirect3DDevice9Ex *iface, BOOL enable)
1261 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1262 HRESULT hr;
1264 TRACE("iface %p, enable %#x.\n", iface, enable);
1266 wined3d_mutex_lock();
1267 hr = wined3d_device_set_dialog_box_mode(device->wined3d_device, enable);
1268 wined3d_mutex_unlock();
1270 return hr;
1273 static void WINAPI d3d9_device_SetGammaRamp(IDirect3DDevice9Ex *iface,
1274 UINT swapchain, DWORD flags, const D3DGAMMARAMP *ramp)
1276 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1278 TRACE("iface %p, swapchain %u, flags %#x, ramp %p.\n", iface, swapchain, flags, ramp);
1280 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
1281 wined3d_mutex_lock();
1282 wined3d_device_set_gamma_ramp(device->wined3d_device, swapchain, flags, (const struct wined3d_gamma_ramp *)ramp);
1283 wined3d_mutex_unlock();
1286 static void WINAPI d3d9_device_GetGammaRamp(IDirect3DDevice9Ex *iface, UINT swapchain, D3DGAMMARAMP *ramp)
1288 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1290 TRACE("iface %p, swapchain %u, ramp %p.\n", iface, swapchain, ramp);
1292 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
1293 wined3d_mutex_lock();
1294 wined3d_device_get_gamma_ramp(device->wined3d_device, swapchain, (struct wined3d_gamma_ramp *)ramp);
1295 wined3d_mutex_unlock();
1298 static HRESULT WINAPI d3d9_device_CreateTexture(IDirect3DDevice9Ex *iface,
1299 UINT width, UINT height, UINT levels, DWORD usage, D3DFORMAT format,
1300 D3DPOOL pool, IDirect3DTexture9 **texture, HANDLE *shared_handle)
1302 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1303 struct d3d9_texture *object;
1304 BOOL set_mem = FALSE;
1305 HRESULT hr;
1307 TRACE("iface %p, width %u, height %u, levels %u, usage %#x, format %#x, pool %#x, texture %p, shared_handle %p.\n",
1308 iface, width, height, levels, usage, format, pool, texture, shared_handle);
1310 *texture = NULL;
1311 if (shared_handle)
1313 if (!device->d3d_parent->extended)
1315 WARN("Trying to create a shared or user memory texture on a non-ex device.\n");
1316 return E_NOTIMPL;
1319 if (pool == D3DPOOL_SYSTEMMEM)
1321 if (levels != 1)
1322 return D3DERR_INVALIDCALL;
1323 set_mem = TRUE;
1325 else
1327 if (pool != D3DPOOL_DEFAULT)
1329 WARN("Trying to create a shared texture in pool %#x.\n", pool);
1330 return D3DERR_INVALIDCALL;
1332 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1336 if (!(object = heap_alloc_zero(sizeof(*object))))
1337 return D3DERR_OUTOFVIDEOMEMORY;
1339 hr = texture_init(object, device, width, height, levels, usage, format, pool);
1340 if (FAILED(hr))
1342 WARN("Failed to initialize texture, hr %#x.\n", hr);
1343 heap_free(object);
1344 return hr;
1347 if (set_mem)
1349 wined3d_mutex_lock();
1350 wined3d_texture_update_desc(object->wined3d_texture, 0, width, height,
1351 wined3dformat_from_d3dformat(format), WINED3D_MULTISAMPLE_NONE, 0,
1352 *shared_handle, 0);
1353 wined3d_mutex_unlock();
1356 TRACE("Created texture %p.\n", object);
1357 *texture = (IDirect3DTexture9 *)&object->IDirect3DBaseTexture9_iface;
1359 return D3D_OK;
1362 static HRESULT WINAPI d3d9_device_CreateVolumeTexture(IDirect3DDevice9Ex *iface,
1363 UINT width, UINT height, UINT depth, UINT levels, DWORD usage, D3DFORMAT format,
1364 D3DPOOL pool, IDirect3DVolumeTexture9 **texture, HANDLE *shared_handle)
1366 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1367 struct d3d9_texture *object;
1368 HRESULT hr;
1370 TRACE("iface %p, width %u, height %u, depth %u, levels %u, "
1371 "usage %#x, format %#x, pool %#x, texture %p, shared_handle %p.\n",
1372 iface, width, height, depth, levels,
1373 usage, format, pool, texture, shared_handle);
1375 *texture = NULL;
1376 if (shared_handle)
1378 if (!device->d3d_parent->extended)
1380 WARN("Trying to create a shared volume texture on a non-ex device.\n");
1381 return E_NOTIMPL;
1384 if (pool != D3DPOOL_DEFAULT)
1386 WARN("Trying to create a shared volume texture in pool %#x.\n", pool);
1387 return D3DERR_INVALIDCALL;
1389 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1392 if (!(object = heap_alloc_zero(sizeof(*object))))
1393 return D3DERR_OUTOFVIDEOMEMORY;
1395 hr = volumetexture_init(object, device, width, height, depth, levels, usage, format, pool);
1396 if (FAILED(hr))
1398 WARN("Failed to initialize volume texture, hr %#x.\n", hr);
1399 heap_free(object);
1400 return hr;
1403 TRACE("Created volume texture %p.\n", object);
1404 *texture = (IDirect3DVolumeTexture9 *)&object->IDirect3DBaseTexture9_iface;
1406 return D3D_OK;
1409 static HRESULT WINAPI d3d9_device_CreateCubeTexture(IDirect3DDevice9Ex *iface,
1410 UINT edge_length, UINT levels, DWORD usage, D3DFORMAT format, D3DPOOL pool,
1411 IDirect3DCubeTexture9 **texture, HANDLE *shared_handle)
1413 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1414 struct d3d9_texture *object;
1415 HRESULT hr;
1417 TRACE("iface %p, edge_length %u, levels %u, usage %#x, format %#x, pool %#x, texture %p, shared_handle %p.\n",
1418 iface, edge_length, levels, usage, format, pool, texture, shared_handle);
1420 *texture = NULL;
1421 if (shared_handle)
1423 if (!device->d3d_parent->extended)
1425 WARN("Trying to create a shared cube texture on a non-ex device.\n");
1426 return E_NOTIMPL;
1429 if (pool != D3DPOOL_DEFAULT)
1431 WARN("Trying to create a shared cube texture in pool %#x.\n", pool);
1432 return D3DERR_INVALIDCALL;
1434 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1437 if (!(object = heap_alloc_zero(sizeof(*object))))
1438 return D3DERR_OUTOFVIDEOMEMORY;
1440 hr = cubetexture_init(object, device, edge_length, levels, usage, format, pool);
1441 if (FAILED(hr))
1443 WARN("Failed to initialize cube texture, hr %#x.\n", hr);
1444 heap_free(object);
1445 return hr;
1448 TRACE("Created cube texture %p.\n", object);
1449 *texture = (IDirect3DCubeTexture9 *)&object->IDirect3DBaseTexture9_iface;
1451 return D3D_OK;
1454 static HRESULT WINAPI d3d9_device_CreateVertexBuffer(IDirect3DDevice9Ex *iface, UINT size,
1455 DWORD usage, DWORD fvf, D3DPOOL pool, IDirect3DVertexBuffer9 **buffer,
1456 HANDLE *shared_handle)
1458 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1459 struct d3d9_vertexbuffer *object;
1460 HRESULT hr;
1462 TRACE("iface %p, size %u, usage %#x, fvf %#x, pool %#x, buffer %p, shared_handle %p.\n",
1463 iface, size, usage, fvf, pool, buffer, shared_handle);
1465 if (shared_handle)
1467 if (!device->d3d_parent->extended)
1469 WARN("Trying to create a shared vertex buffer on a non-ex device.\n");
1470 return E_NOTIMPL;
1473 if (pool != D3DPOOL_DEFAULT)
1475 WARN("Trying to create a shared vertex buffer in pool %#x.\n", pool);
1476 return D3DERR_NOTAVAILABLE;
1478 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1481 if (!(object = heap_alloc_zero(sizeof(*object))))
1482 return D3DERR_OUTOFVIDEOMEMORY;
1484 hr = vertexbuffer_init(object, device, size, usage, fvf, pool);
1485 if (FAILED(hr))
1487 WARN("Failed to initialize vertex buffer, hr %#x.\n", hr);
1488 heap_free(object);
1489 return hr;
1492 TRACE("Created vertex buffer %p.\n", object);
1493 *buffer = &object->IDirect3DVertexBuffer9_iface;
1495 return D3D_OK;
1498 static HRESULT WINAPI d3d9_device_CreateIndexBuffer(IDirect3DDevice9Ex *iface, UINT size,
1499 DWORD usage, D3DFORMAT format, D3DPOOL pool, IDirect3DIndexBuffer9 **buffer,
1500 HANDLE *shared_handle)
1502 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1503 struct d3d9_indexbuffer *object;
1504 HRESULT hr;
1506 TRACE("iface %p, size %u, usage %#x, format %#x, pool %#x, buffer %p, shared_handle %p.\n",
1507 iface, size, usage, format, pool, buffer, shared_handle);
1509 if (shared_handle)
1511 if (!device->d3d_parent->extended)
1513 WARN("Trying to create a shared index buffer on a non-ex device.\n");
1514 return E_NOTIMPL;
1517 if (pool != D3DPOOL_DEFAULT)
1519 WARN("Trying to create a shared index buffer in pool %#x.\n", pool);
1520 return D3DERR_NOTAVAILABLE;
1522 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1525 if (!(object = heap_alloc_zero(sizeof(*object))))
1526 return D3DERR_OUTOFVIDEOMEMORY;
1528 hr = indexbuffer_init(object, device, size, usage, format, pool);
1529 if (FAILED(hr))
1531 WARN("Failed to initialize index buffer, hr %#x.\n", hr);
1532 heap_free(object);
1533 return hr;
1536 TRACE("Created index buffer %p.\n", object);
1537 *buffer = &object->IDirect3DIndexBuffer9_iface;
1539 return D3D_OK;
1542 static HRESULT d3d9_device_create_surface(struct d3d9_device *device, unsigned int flags,
1543 enum wined3d_format_id format, enum wined3d_multisample_type multisample_type,
1544 unsigned int multisample_quality, unsigned int usage, unsigned int bind_flags, unsigned int access,
1545 unsigned int width, unsigned int height, void *user_mem, IDirect3DSurface9 **surface)
1547 struct wined3d_resource_desc desc;
1548 struct d3d9_surface *surface_impl;
1549 struct wined3d_texture *texture;
1550 HRESULT hr;
1552 TRACE("device %p, flags %#x, format %#x, multisample_type %#x, multisample_quality %u, "
1553 "usage %#x, bind_flags %#x, access %#x, width %u, height %u, user_mem %p, surface %p.\n",
1554 device, flags, format, multisample_type, multisample_quality, usage,
1555 bind_flags, access, width, height, user_mem, surface);
1557 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
1558 desc.format = format;
1559 desc.multisample_type = multisample_type;
1560 desc.multisample_quality = multisample_quality;
1561 desc.usage = usage;
1562 desc.bind_flags = bind_flags;
1563 desc.access = access;
1564 desc.width = width;
1565 desc.height = height;
1566 desc.depth = 1;
1567 desc.size = 0;
1569 if (is_gdi_compat_wined3dformat(desc.format))
1570 flags |= WINED3D_TEXTURE_CREATE_GET_DC;
1572 wined3d_mutex_lock();
1574 if (FAILED(hr = wined3d_texture_create(device->wined3d_device, &desc,
1575 1, 1, flags, NULL, NULL, &d3d9_null_wined3d_parent_ops, &texture)))
1577 wined3d_mutex_unlock();
1578 WARN("Failed to create texture, hr %#x.\n", hr);
1579 if (hr == WINED3DERR_NOTAVAILABLE)
1580 hr = D3DERR_INVALIDCALL;
1581 return hr;
1584 surface_impl = wined3d_texture_get_sub_resource_parent(texture, 0);
1585 surface_impl->parent_device = &device->IDirect3DDevice9Ex_iface;
1586 *surface = &surface_impl->IDirect3DSurface9_iface;
1587 IDirect3DSurface9_AddRef(*surface);
1589 if (user_mem)
1590 wined3d_texture_update_desc(texture, 0, width, height, desc.format,
1591 multisample_type, multisample_quality, user_mem, 0);
1593 wined3d_texture_decref(texture);
1595 wined3d_mutex_unlock();
1597 return D3D_OK;
1600 BOOL is_gdi_compat_wined3dformat(enum wined3d_format_id format)
1602 switch (format)
1604 case WINED3DFMT_B8G8R8A8_UNORM:
1605 case WINED3DFMT_B8G8R8X8_UNORM:
1606 case WINED3DFMT_B5G6R5_UNORM:
1607 case WINED3DFMT_B5G5R5X1_UNORM:
1608 case WINED3DFMT_B5G5R5A1_UNORM:
1609 case WINED3DFMT_B8G8R8_UNORM:
1610 return TRUE;
1611 default:
1612 return FALSE;
1616 static HRESULT WINAPI d3d9_device_CreateRenderTarget(IDirect3DDevice9Ex *iface, UINT width, UINT height,
1617 D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
1618 BOOL lockable, IDirect3DSurface9 **surface, HANDLE *shared_handle)
1620 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1621 unsigned int access = WINED3D_RESOURCE_ACCESS_GPU;
1623 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u.\n"
1624 "lockable %#x, surface %p, shared_handle %p.\n",
1625 iface, width, height, format, multisample_type, multisample_quality,
1626 lockable, surface, shared_handle);
1628 *surface = NULL;
1629 if (shared_handle)
1631 if (!device->d3d_parent->extended)
1633 WARN("Trying to create a shared render target on a non-ex device.\n");
1634 return E_NOTIMPL;
1637 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1640 if (lockable)
1641 access |= WINED3D_RESOURCE_ACCESS_MAP_R | WINED3D_RESOURCE_ACCESS_MAP_W;
1643 return d3d9_device_create_surface(device, 0, wined3dformat_from_d3dformat(format),
1644 wined3d_multisample_type_from_d3d(multisample_type), multisample_quality, 0,
1645 WINED3D_BIND_RENDER_TARGET, access, width, height, NULL, surface);
1648 static HRESULT WINAPI d3d9_device_CreateDepthStencilSurface(IDirect3DDevice9Ex *iface, UINT width, UINT height,
1649 D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
1650 BOOL discard, IDirect3DSurface9 **surface, HANDLE *shared_handle)
1652 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1653 DWORD flags = 0;
1655 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u.\n"
1656 "discard %#x, surface %p, shared_handle %p.\n",
1657 iface, width, height, format, multisample_type, multisample_quality,
1658 discard, surface, shared_handle);
1660 *surface = NULL;
1661 if (shared_handle)
1663 if (!device->d3d_parent->extended)
1665 WARN("Trying to create a shared depth stencil on a non-ex device.\n");
1666 return E_NOTIMPL;
1669 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1672 if (discard)
1673 flags |= WINED3D_TEXTURE_CREATE_DISCARD;
1675 return d3d9_device_create_surface(device, flags, wined3dformat_from_d3dformat(format),
1676 wined3d_multisample_type_from_d3d(multisample_type), multisample_quality, 0,
1677 WINED3D_BIND_DEPTH_STENCIL, WINED3D_RESOURCE_ACCESS_GPU, width, height, NULL, surface);
1681 static HRESULT WINAPI d3d9_device_UpdateSurface(IDirect3DDevice9Ex *iface,
1682 IDirect3DSurface9 *src_surface, const RECT *src_rect,
1683 IDirect3DSurface9 *dst_surface, const POINT *dst_point)
1685 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1686 struct d3d9_surface *src = unsafe_impl_from_IDirect3DSurface9(src_surface);
1687 struct d3d9_surface *dst = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1688 struct wined3d_sub_resource_desc src_desc, dst_desc;
1689 struct wined3d_box src_box;
1690 HRESULT hr;
1692 TRACE("iface %p, src_surface %p, src_rect %s, dst_surface %p, dst_point %p.\n",
1693 iface, src_surface, wine_dbgstr_rect(src_rect), dst_surface, dst_point);
1695 wined3d_mutex_lock();
1697 wined3d_texture_get_sub_resource_desc(src->wined3d_texture, src->sub_resource_idx, &src_desc);
1698 wined3d_texture_get_sub_resource_desc(dst->wined3d_texture, dst->sub_resource_idx, &dst_desc);
1699 if (src_desc.format != dst_desc.format)
1701 wined3d_mutex_unlock();
1702 WARN("Surface formats (%#x/%#x) don't match.\n",
1703 d3dformat_from_wined3dformat(src_desc.format),
1704 d3dformat_from_wined3dformat(dst_desc.format));
1705 return D3DERR_INVALIDCALL;
1708 if (src_rect)
1709 wined3d_box_set(&src_box, src_rect->left, src_rect->top, src_rect->right, src_rect->bottom, 0, 1);
1710 else
1711 wined3d_box_set(&src_box, 0, 0, src_desc.width, src_desc.height, 0, 1);
1713 hr = wined3d_device_context_copy_sub_resource_region(device->immediate_context,
1714 wined3d_texture_get_resource(dst->wined3d_texture), dst->sub_resource_idx, dst_point ? dst_point->x : 0,
1715 dst_point ? dst_point->y : 0, 0, wined3d_texture_get_resource(src->wined3d_texture),
1716 src->sub_resource_idx, &src_box, 0);
1717 if (SUCCEEDED(hr) && dst->texture)
1718 d3d9_texture_flag_auto_gen_mipmap(dst->texture);
1720 wined3d_mutex_unlock();
1722 if (FAILED(hr))
1723 return D3DERR_INVALIDCALL;
1725 return hr;
1728 static HRESULT WINAPI d3d9_device_UpdateTexture(IDirect3DDevice9Ex *iface,
1729 IDirect3DBaseTexture9 *src_texture, IDirect3DBaseTexture9 *dst_texture)
1731 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1732 struct d3d9_texture *src_impl, *dst_impl;
1733 HRESULT hr;
1735 TRACE("iface %p, src_texture %p, dst_texture %p.\n", iface, src_texture, dst_texture);
1737 src_impl = unsafe_impl_from_IDirect3DBaseTexture9(src_texture);
1738 dst_impl = unsafe_impl_from_IDirect3DBaseTexture9(dst_texture);
1740 wined3d_mutex_lock();
1741 hr = wined3d_device_update_texture(device->wined3d_device,
1742 src_impl->wined3d_texture, dst_impl->wined3d_texture);
1743 if (SUCCEEDED(hr))
1744 d3d9_texture_flag_auto_gen_mipmap(dst_impl);
1745 wined3d_mutex_unlock();
1747 return hr;
1750 static HRESULT WINAPI d3d9_device_GetRenderTargetData(IDirect3DDevice9Ex *iface,
1751 IDirect3DSurface9 *render_target, IDirect3DSurface9 *dst_surface)
1753 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1754 struct d3d9_surface *rt_impl = unsafe_impl_from_IDirect3DSurface9(render_target);
1755 struct d3d9_surface *dst_impl = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1756 struct wined3d_sub_resource_desc wined3d_desc;
1757 RECT dst_rect, src_rect;
1758 HRESULT hr;
1760 TRACE("iface %p, render_target %p, dst_surface %p.\n", iface, render_target, dst_surface);
1762 if (!render_target || !dst_surface)
1763 return D3DERR_INVALIDCALL;
1765 wined3d_mutex_lock();
1766 wined3d_texture_get_sub_resource_desc(dst_impl->wined3d_texture, dst_impl->sub_resource_idx, &wined3d_desc);
1767 SetRect(&dst_rect, 0, 0, wined3d_desc.width, wined3d_desc.height);
1769 wined3d_texture_get_sub_resource_desc(rt_impl->wined3d_texture, rt_impl->sub_resource_idx, &wined3d_desc);
1770 SetRect(&src_rect, 0, 0, wined3d_desc.width, wined3d_desc.height);
1772 /* TODO: Check surface sizes, pools, etc. */
1773 if (wined3d_desc.multisample_type)
1774 hr = D3DERR_INVALIDCALL;
1775 else
1776 hr = wined3d_device_context_blt(device->immediate_context,
1777 dst_impl->wined3d_texture, dst_impl->sub_resource_idx, &dst_rect,
1778 rt_impl->wined3d_texture, rt_impl->sub_resource_idx, &src_rect, 0, NULL, WINED3D_TEXF_POINT);
1779 wined3d_mutex_unlock();
1781 return hr;
1784 static HRESULT WINAPI d3d9_device_GetFrontBufferData(IDirect3DDevice9Ex *iface,
1785 UINT swapchain, IDirect3DSurface9 *dst_surface)
1787 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1788 struct d3d9_surface *dst_impl = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1789 HRESULT hr = D3DERR_INVALIDCALL;
1791 TRACE("iface %p, swapchain %u, dst_surface %p.\n", iface, swapchain, dst_surface);
1793 wined3d_mutex_lock();
1794 if (swapchain < device->implicit_swapchain_count)
1795 hr = wined3d_swapchain_get_front_buffer_data(device->implicit_swapchains[swapchain],
1796 dst_impl->wined3d_texture, dst_impl->sub_resource_idx);
1797 wined3d_mutex_unlock();
1799 return hr;
1802 static HRESULT WINAPI d3d9_device_StretchRect(IDirect3DDevice9Ex *iface, IDirect3DSurface9 *src_surface,
1803 const RECT *src_rect, IDirect3DSurface9 *dst_surface, const RECT *dst_rect, D3DTEXTUREFILTERTYPE filter)
1805 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1806 struct d3d9_surface *src = unsafe_impl_from_IDirect3DSurface9(src_surface);
1807 struct d3d9_surface *dst = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1808 struct wined3d_sub_resource_desc src_desc, dst_desc;
1809 HRESULT hr = D3DERR_INVALIDCALL;
1810 RECT d, s;
1812 TRACE("iface %p, src_surface %p, src_rect %s, dst_surface %p, dst_rect %s, filter %#x.\n",
1813 iface, src_surface, wine_dbgstr_rect(src_rect), dst_surface, wine_dbgstr_rect(dst_rect), filter);
1815 wined3d_mutex_lock();
1816 wined3d_texture_get_sub_resource_desc(dst->wined3d_texture, dst->sub_resource_idx, &dst_desc);
1817 if (!dst_rect)
1819 SetRect(&d, 0, 0, dst_desc.width, dst_desc.height);
1820 dst_rect = &d;
1823 wined3d_texture_get_sub_resource_desc(src->wined3d_texture, src->sub_resource_idx, &src_desc);
1824 if (!src_rect)
1826 SetRect(&s, 0, 0, src_desc.width, src_desc.height);
1827 src_rect = &s;
1830 if (dst_desc.access & WINED3D_RESOURCE_ACCESS_CPU)
1832 WARN("Destination resource is not in DEFAULT pool.\n");
1833 goto done;
1835 if (src_desc.access & WINED3D_RESOURCE_ACCESS_CPU)
1837 WARN("Source resource is not in DEFAULT pool.\n");
1838 goto done;
1841 if (dst->texture && !(dst_desc.bind_flags & (WINED3D_BIND_RENDER_TARGET | WINED3D_BIND_DEPTH_STENCIL)))
1843 WARN("Destination is a regular texture.\n");
1844 goto done;
1847 if (src_desc.bind_flags & WINED3D_BIND_DEPTH_STENCIL)
1849 if (device->in_scene)
1851 WARN("Rejecting depth / stencil blit while in scene.\n");
1852 goto done;
1855 if (src_rect->left || src_rect->top || src_rect->right != src_desc.width
1856 || src_rect->bottom != src_desc.height)
1858 WARN("Rejecting depth / stencil blit with invalid source rect %s.\n",
1859 wine_dbgstr_rect(src_rect));
1860 goto done;
1863 if (dst_rect->left || dst_rect->top || dst_rect->right != dst_desc.width
1864 || dst_rect->bottom != dst_desc.height)
1866 WARN("Rejecting depth / stencil blit with invalid destination rect %s.\n",
1867 wine_dbgstr_rect(dst_rect));
1868 goto done;
1871 if (src_desc.width != dst_desc.width || src_desc.height != dst_desc.height)
1873 WARN("Rejecting depth / stencil blit with mismatched surface sizes.\n");
1874 goto done;
1878 hr = wined3d_device_context_blt(device->immediate_context, dst->wined3d_texture,
1879 dst->sub_resource_idx, dst_rect, src->wined3d_texture, src->sub_resource_idx,
1880 src_rect, 0, NULL, wined3d_texture_filter_type_from_d3d(filter));
1881 if (hr == WINEDDERR_INVALIDRECT)
1882 hr = D3DERR_INVALIDCALL;
1883 if (SUCCEEDED(hr) && dst->texture)
1884 d3d9_texture_flag_auto_gen_mipmap(dst->texture);
1886 done:
1887 wined3d_mutex_unlock();
1888 return hr;
1891 static HRESULT WINAPI d3d9_device_ColorFill(IDirect3DDevice9Ex *iface,
1892 IDirect3DSurface9 *surface, const RECT *rect, D3DCOLOR color)
1894 const struct wined3d_color c =
1896 ((color >> 16) & 0xff) / 255.0f,
1897 ((color >> 8) & 0xff) / 255.0f,
1898 (color & 0xff) / 255.0f,
1899 ((color >> 24) & 0xff) / 255.0f,
1901 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1902 struct d3d9_surface *surface_impl = unsafe_impl_from_IDirect3DSurface9(surface);
1903 struct wined3d_sub_resource_desc desc;
1904 struct wined3d_rendertarget_view *rtv;
1905 HRESULT hr;
1907 TRACE("iface %p, surface %p, rect %p, color 0x%08x.\n", iface, surface, rect, color);
1909 if (!surface)
1910 return D3DERR_INVALIDCALL;
1912 wined3d_mutex_lock();
1914 if (FAILED(wined3d_texture_get_sub_resource_desc(surface_impl->wined3d_texture,
1915 surface_impl->sub_resource_idx, &desc)))
1917 wined3d_mutex_unlock();
1918 return D3DERR_INVALIDCALL;
1921 if (desc.access & WINED3D_RESOURCE_ACCESS_CPU)
1923 wined3d_mutex_unlock();
1924 WARN("Colour fills are not allowed on surfaces with resource access %#x.\n", desc.access);
1925 return D3DERR_INVALIDCALL;
1927 if ((desc.bind_flags & (WINED3D_BIND_RENDER_TARGET | WINED3D_BIND_SHADER_RESOURCE)) == WINED3D_BIND_SHADER_RESOURCE)
1929 wined3d_mutex_unlock();
1930 WARN("Colorfill is not allowed on non-RT textures, returning D3DERR_INVALIDCALL.\n");
1931 return D3DERR_INVALIDCALL;
1933 if (desc.bind_flags & WINED3D_BIND_DEPTH_STENCIL)
1935 wined3d_mutex_unlock();
1936 WARN("Colorfill is not allowed on depth stencil surfaces, returning D3DERR_INVALIDCALL.\n");
1937 return D3DERR_INVALIDCALL;
1940 wined3d_device_apply_stateblock(device->wined3d_device, device->state);
1941 rtv = d3d9_surface_acquire_rendertarget_view(surface_impl);
1942 hr = wined3d_device_context_clear_rendertarget_view(device->immediate_context,
1943 rtv, rect, WINED3DCLEAR_TARGET, &c, 0.0f, 0);
1944 d3d9_surface_release_rendertarget_view(surface_impl, rtv);
1945 if (SUCCEEDED(hr) && surface_impl->texture)
1946 d3d9_texture_flag_auto_gen_mipmap(surface_impl->texture);
1948 wined3d_mutex_unlock();
1950 return hr;
1953 static HRESULT WINAPI d3d9_device_CreateOffscreenPlainSurface(IDirect3DDevice9Ex *iface,
1954 UINT width, UINT height, D3DFORMAT format, D3DPOOL pool, IDirect3DSurface9 **surface,
1955 HANDLE *shared_handle)
1957 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1958 unsigned int usage, access;
1959 void *user_mem = NULL;
1961 TRACE("iface %p, width %u, height %u, format %#x, pool %#x, surface %p, shared_handle %p.\n",
1962 iface, width, height, format, pool, surface, shared_handle);
1964 *surface = NULL;
1965 if (pool == D3DPOOL_MANAGED)
1967 WARN("Attempting to create a managed offscreen plain surface.\n");
1968 return D3DERR_INVALIDCALL;
1971 if (shared_handle)
1973 if (!device->d3d_parent->extended)
1975 WARN("Trying to create a shared or user memory surface on a non-ex device.\n");
1976 return E_NOTIMPL;
1979 if (pool == D3DPOOL_SYSTEMMEM)
1980 user_mem = *shared_handle;
1981 else
1983 if (pool != D3DPOOL_DEFAULT)
1985 WARN("Trying to create a shared surface in pool %#x.\n", pool);
1986 return D3DERR_INVALIDCALL;
1988 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1992 usage = 0;
1993 if (pool == D3DPOOL_SCRATCH)
1994 usage |= WINED3DUSAGE_SCRATCH;
1995 access = wined3daccess_from_d3dpool(pool, usage)
1996 | WINED3D_RESOURCE_ACCESS_MAP_R | WINED3D_RESOURCE_ACCESS_MAP_W;
1998 return d3d9_device_create_surface(device, 0, wined3dformat_from_d3dformat(format),
1999 WINED3D_MULTISAMPLE_NONE, 0, usage, 0, access, width, height, user_mem, surface);
2002 static HRESULT WINAPI d3d9_device_SetRenderTarget(IDirect3DDevice9Ex *iface, DWORD idx, IDirect3DSurface9 *surface)
2004 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2005 struct d3d9_surface *surface_impl = unsafe_impl_from_IDirect3DSurface9(surface);
2006 struct wined3d_rendertarget_view *rtv;
2007 HRESULT hr;
2009 TRACE("iface %p, idx %u, surface %p.\n", iface, idx, surface);
2011 if (idx >= D3D_MAX_SIMULTANEOUS_RENDERTARGETS)
2013 WARN("Invalid index %u specified.\n", idx);
2014 return D3DERR_INVALIDCALL;
2017 if (!idx && !surface_impl)
2019 WARN("Trying to set render target 0 to NULL.\n");
2020 return D3DERR_INVALIDCALL;
2023 if (surface_impl && d3d9_surface_get_device(surface_impl) != device)
2025 WARN("Render target surface does not match device.\n");
2026 return D3DERR_INVALIDCALL;
2029 wined3d_mutex_lock();
2030 rtv = surface_impl ? d3d9_surface_acquire_rendertarget_view(surface_impl) : NULL;
2031 hr = wined3d_device_context_set_rendertarget_views(device->immediate_context, idx, 1, &rtv, TRUE);
2032 d3d9_surface_release_rendertarget_view(surface_impl, rtv);
2033 if (SUCCEEDED(hr))
2035 if (!idx)
2036 device_reset_viewport_state(device);
2037 device->render_targets[idx] = surface_impl;
2039 wined3d_mutex_unlock();
2041 return hr;
2044 static HRESULT WINAPI d3d9_device_GetRenderTarget(IDirect3DDevice9Ex *iface, DWORD idx, IDirect3DSurface9 **surface)
2046 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2047 struct wined3d_rendertarget_view *wined3d_rtv;
2048 struct d3d9_surface *surface_impl;
2049 HRESULT hr = D3D_OK;
2051 TRACE("iface %p, idx %u, surface %p.\n", iface, idx, surface);
2053 if (!surface)
2054 return D3DERR_INVALIDCALL;
2056 if (idx >= D3D_MAX_SIMULTANEOUS_RENDERTARGETS)
2058 WARN("Invalid index %u specified.\n", idx);
2059 return D3DERR_INVALIDCALL;
2062 wined3d_mutex_lock();
2063 if ((wined3d_rtv = wined3d_device_context_get_rendertarget_view(device->immediate_context, idx)))
2065 /* We want the sub resource parent here, since the view itself may be
2066 * internal to wined3d and may not have a parent. */
2067 surface_impl = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_rtv);
2068 *surface = &surface_impl->IDirect3DSurface9_iface;
2069 IDirect3DSurface9_AddRef(*surface);
2071 else
2073 hr = D3DERR_NOTFOUND;
2074 *surface = NULL;
2076 wined3d_mutex_unlock();
2078 return hr;
2081 static HRESULT WINAPI d3d9_device_SetDepthStencilSurface(IDirect3DDevice9Ex *iface, IDirect3DSurface9 *depth_stencil)
2083 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2084 struct d3d9_surface *ds_impl = unsafe_impl_from_IDirect3DSurface9(depth_stencil);
2085 struct wined3d_rendertarget_view *rtv;
2086 HRESULT hr;
2088 TRACE("iface %p, depth_stencil %p.\n", iface, depth_stencil);
2090 wined3d_mutex_lock();
2091 rtv = ds_impl ? d3d9_surface_acquire_rendertarget_view(ds_impl) : NULL;
2092 hr = wined3d_device_context_set_depth_stencil_view(device->immediate_context, rtv);
2093 d3d9_surface_release_rendertarget_view(ds_impl, rtv);
2094 wined3d_mutex_unlock();
2096 return hr;
2099 static HRESULT WINAPI d3d9_device_GetDepthStencilSurface(IDirect3DDevice9Ex *iface, IDirect3DSurface9 **depth_stencil)
2101 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2102 struct wined3d_rendertarget_view *wined3d_dsv;
2103 struct d3d9_surface *surface_impl;
2104 HRESULT hr = D3D_OK;
2106 TRACE("iface %p, depth_stencil %p.\n", iface, depth_stencil);
2108 if (!depth_stencil)
2109 return D3DERR_INVALIDCALL;
2111 wined3d_mutex_lock();
2112 if ((wined3d_dsv = wined3d_device_context_get_depth_stencil_view(device->immediate_context)))
2114 /* We want the sub resource parent here, since the view itself may be
2115 * internal to wined3d and may not have a parent. */
2116 surface_impl = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_dsv);
2117 *depth_stencil = &surface_impl->IDirect3DSurface9_iface;
2118 IDirect3DSurface9_AddRef(*depth_stencil);
2120 else
2122 hr = D3DERR_NOTFOUND;
2123 *depth_stencil = NULL;
2125 wined3d_mutex_unlock();
2127 return hr;
2130 static HRESULT WINAPI d3d9_device_BeginScene(IDirect3DDevice9Ex *iface)
2132 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2133 HRESULT hr;
2135 TRACE("iface %p.\n", iface);
2137 wined3d_mutex_lock();
2138 if (SUCCEEDED(hr = wined3d_device_begin_scene(device->wined3d_device)))
2139 device->in_scene = TRUE;
2140 wined3d_mutex_unlock();
2142 return hr;
2145 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_EndScene(IDirect3DDevice9Ex *iface)
2147 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2148 HRESULT hr;
2150 TRACE("iface %p.\n", iface);
2152 wined3d_mutex_lock();
2153 if (SUCCEEDED(hr = wined3d_device_end_scene(device->wined3d_device)))
2154 device->in_scene = FALSE;
2155 wined3d_mutex_unlock();
2157 return hr;
2160 static void d3d9_rts_flag_auto_gen_mipmap(struct d3d9_device *device)
2162 unsigned int i;
2164 for (i = 0; i < ARRAY_SIZE(device->render_targets); ++i)
2166 struct d3d9_surface *surface = device->render_targets[i];
2168 if (surface && surface->texture)
2169 d3d9_texture_flag_auto_gen_mipmap(surface->texture);
2173 static HRESULT WINAPI d3d9_device_Clear(IDirect3DDevice9Ex *iface, DWORD rect_count,
2174 const D3DRECT *rects, DWORD flags, D3DCOLOR color, float z, DWORD stencil)
2176 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2177 struct wined3d_color c;
2178 HRESULT hr;
2180 TRACE("iface %p, rect_count %u, rects %p, flags %#x, color 0x%08x, z %.8e, stencil %u.\n",
2181 iface, rect_count, rects, flags, color, z, stencil);
2183 if (rect_count && !rects)
2185 WARN("count %u with NULL rects.\n", rect_count);
2186 rect_count = 0;
2189 wined3d_color_from_d3dcolor(&c, color);
2190 wined3d_mutex_lock();
2191 wined3d_device_apply_stateblock(device->wined3d_device, device->state);
2192 hr = wined3d_device_clear(device->wined3d_device, rect_count, (const RECT *)rects, flags, &c, z, stencil);
2193 if (SUCCEEDED(hr))
2194 d3d9_rts_flag_auto_gen_mipmap(device);
2195 wined3d_mutex_unlock();
2197 return hr;
2200 static HRESULT WINAPI d3d9_device_SetTransform(IDirect3DDevice9Ex *iface,
2201 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
2203 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2205 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
2207 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
2208 wined3d_mutex_lock();
2209 wined3d_stateblock_set_transform(device->update_state,
2210 wined3d_transform_state_from_d3d(state), (const struct wined3d_matrix *)matrix);
2211 wined3d_mutex_unlock();
2213 return D3D_OK;
2216 static HRESULT WINAPI d3d9_device_GetTransform(IDirect3DDevice9Ex *iface,
2217 D3DTRANSFORMSTATETYPE state, D3DMATRIX *matrix)
2219 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2221 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
2223 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
2224 wined3d_mutex_lock();
2225 memcpy(matrix, &device->stateblock_state->transforms[state], sizeof(*matrix));
2226 wined3d_mutex_unlock();
2228 return D3D_OK;
2231 static HRESULT WINAPI d3d9_device_MultiplyTransform(IDirect3DDevice9Ex *iface,
2232 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
2234 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2236 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
2238 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
2239 wined3d_mutex_lock();
2240 wined3d_stateblock_multiply_transform(device->state,
2241 wined3d_transform_state_from_d3d(state), (const struct wined3d_matrix *)matrix);
2242 wined3d_mutex_unlock();
2244 return D3D_OK;
2247 static HRESULT WINAPI d3d9_device_SetViewport(IDirect3DDevice9Ex *iface, const D3DVIEWPORT9 *viewport)
2249 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2250 struct wined3d_viewport vp;
2252 TRACE("iface %p, viewport %p.\n", iface, viewport);
2254 vp.x = viewport->X;
2255 vp.y = viewport->Y;
2256 vp.width = viewport->Width;
2257 vp.height = viewport->Height;
2258 vp.min_z = viewport->MinZ;
2259 vp.max_z = viewport->MaxZ;
2261 wined3d_mutex_lock();
2262 wined3d_stateblock_set_viewport(device->update_state, &vp);
2263 wined3d_mutex_unlock();
2265 return D3D_OK;
2268 static HRESULT WINAPI d3d9_device_GetViewport(IDirect3DDevice9Ex *iface, D3DVIEWPORT9 *viewport)
2270 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2271 struct wined3d_viewport wined3d_viewport;
2273 TRACE("iface %p, viewport %p.\n", iface, viewport);
2275 wined3d_mutex_lock();
2276 wined3d_viewport = device->stateblock_state->viewport;
2277 wined3d_mutex_unlock();
2279 viewport->X = wined3d_viewport.x;
2280 viewport->Y = wined3d_viewport.y;
2281 viewport->Width = wined3d_viewport.width;
2282 viewport->Height = wined3d_viewport.height;
2283 viewport->MinZ = wined3d_viewport.min_z;
2284 viewport->MaxZ = wined3d_viewport.max_z;
2286 return D3D_OK;
2289 static HRESULT WINAPI d3d9_device_SetMaterial(IDirect3DDevice9Ex *iface, const D3DMATERIAL9 *material)
2291 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2293 TRACE("iface %p, material %p.\n", iface, material);
2295 /* Note: D3DMATERIAL9 is compatible with struct wined3d_material. */
2296 wined3d_mutex_lock();
2297 wined3d_stateblock_set_material(device->update_state, (const struct wined3d_material *)material);
2298 wined3d_mutex_unlock();
2300 return D3D_OK;
2303 static HRESULT WINAPI d3d9_device_GetMaterial(IDirect3DDevice9Ex *iface, D3DMATERIAL9 *material)
2305 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2307 TRACE("iface %p, material %p.\n", iface, material);
2309 /* Note: D3DMATERIAL9 is compatible with struct wined3d_material. */
2310 wined3d_mutex_lock();
2311 memcpy(material, &device->stateblock_state->material, sizeof(*material));
2312 wined3d_mutex_unlock();
2314 return D3D_OK;
2317 static HRESULT WINAPI d3d9_device_SetLight(IDirect3DDevice9Ex *iface, DWORD index, const D3DLIGHT9 *light)
2319 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2320 HRESULT hr;
2322 TRACE("iface %p, index %u, light %p.\n", iface, index, light);
2324 /* Note: D3DLIGHT9 is compatible with struct wined3d_light. */
2325 wined3d_mutex_lock();
2326 hr = wined3d_stateblock_set_light(device->update_state, index, (const struct wined3d_light *)light);
2327 wined3d_mutex_unlock();
2329 return hr;
2332 static HRESULT WINAPI d3d9_device_GetLight(IDirect3DDevice9Ex *iface, DWORD index, D3DLIGHT9 *light)
2334 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2335 BOOL enabled;
2336 HRESULT hr;
2338 TRACE("iface %p, index %u, light %p.\n", iface, index, light);
2340 /* Note: D3DLIGHT9 is compatible with struct wined3d_light. */
2341 wined3d_mutex_lock();
2342 hr = wined3d_stateblock_get_light(device->state, index, (struct wined3d_light *)light, &enabled);
2343 wined3d_mutex_unlock();
2345 return hr;
2348 static HRESULT WINAPI d3d9_device_LightEnable(IDirect3DDevice9Ex *iface, DWORD index, BOOL enable)
2350 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2351 HRESULT hr;
2353 TRACE("iface %p, index %u, enable %#x.\n", iface, index, enable);
2355 wined3d_mutex_lock();
2356 hr = wined3d_stateblock_set_light_enable(device->update_state, index, enable);
2357 wined3d_mutex_unlock();
2359 return hr;
2362 static HRESULT WINAPI d3d9_device_GetLightEnable(IDirect3DDevice9Ex *iface, DWORD index, BOOL *enabled)
2364 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2365 struct wined3d_light light;
2366 HRESULT hr;
2368 TRACE("iface %p, index %u, enabled %p.\n", iface, index, enabled);
2370 wined3d_mutex_lock();
2371 hr = wined3d_stateblock_get_light(device->state, index, &light, enabled);
2372 wined3d_mutex_unlock();
2374 return hr;
2377 static HRESULT WINAPI d3d9_device_SetClipPlane(IDirect3DDevice9Ex *iface, DWORD index, const float *plane)
2379 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2380 HRESULT hr;
2382 TRACE("iface %p, index %u, plane %p.\n", iface, index, plane);
2384 index = min(index, device->max_user_clip_planes - 1);
2386 wined3d_mutex_lock();
2387 hr = wined3d_stateblock_set_clip_plane(device->update_state, index, (const struct wined3d_vec4 *)plane);
2388 wined3d_mutex_unlock();
2390 return hr;
2393 static HRESULT WINAPI d3d9_device_GetClipPlane(IDirect3DDevice9Ex *iface, DWORD index, float *plane)
2395 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2397 TRACE("iface %p, index %u, plane %p.\n", iface, index, plane);
2399 index = min(index, device->max_user_clip_planes - 1);
2401 wined3d_mutex_lock();
2402 memcpy(plane, &device->stateblock_state->clip_planes[index], sizeof(struct wined3d_vec4));
2403 wined3d_mutex_unlock();
2405 return D3D_OK;
2408 static void resolve_depth_buffer(struct d3d9_device *device)
2410 const struct wined3d_stateblock_state *state = device->stateblock_state;
2411 struct wined3d_rendertarget_view *wined3d_dsv;
2412 struct wined3d_resource *dst_resource;
2413 struct wined3d_texture *dst_texture;
2414 struct wined3d_resource_desc desc;
2415 struct d3d9_surface *d3d9_dsv;
2417 if (!(dst_texture = state->textures[0]))
2418 return;
2419 dst_resource = wined3d_texture_get_resource(dst_texture);
2420 wined3d_resource_get_desc(dst_resource, &desc);
2421 if (desc.format != WINED3DFMT_D24_UNORM_S8_UINT
2422 && desc.format != WINED3DFMT_X8D24_UNORM
2423 && desc.format != WINED3DFMT_DF16
2424 && desc.format != WINED3DFMT_DF24
2425 && desc.format != WINED3DFMT_INTZ)
2426 return;
2428 if (!(wined3d_dsv = wined3d_device_context_get_depth_stencil_view(device->immediate_context)))
2429 return;
2430 d3d9_dsv = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_dsv);
2432 wined3d_device_context_resolve_sub_resource(device->immediate_context, dst_resource, 0,
2433 wined3d_rendertarget_view_get_resource(wined3d_dsv), d3d9_dsv->sub_resource_idx, desc.format);
2436 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_SetRenderState(IDirect3DDevice9Ex *iface,
2437 D3DRENDERSTATETYPE state, DWORD value)
2439 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2441 TRACE("iface %p, state %#x, value %#x.\n", iface, state, value);
2443 wined3d_mutex_lock();
2444 wined3d_stateblock_set_render_state(device->update_state, wined3d_render_state_from_d3d(state), value);
2445 if (state == D3DRS_POINTSIZE && value == D3D9_RESZ_CODE)
2446 resolve_depth_buffer(device);
2447 wined3d_mutex_unlock();
2449 return D3D_OK;
2452 static HRESULT WINAPI d3d9_device_GetRenderState(IDirect3DDevice9Ex *iface,
2453 D3DRENDERSTATETYPE state, DWORD *value)
2455 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2456 const struct wined3d_stateblock_state *device_state;
2458 TRACE("iface %p, state %#x, value %p.\n", iface, state, value);
2460 wined3d_mutex_lock();
2461 device_state = device->stateblock_state;
2462 *value = device_state->rs[state];
2463 wined3d_mutex_unlock();
2465 return D3D_OK;
2468 static HRESULT WINAPI d3d9_device_CreateStateBlock(IDirect3DDevice9Ex *iface,
2469 D3DSTATEBLOCKTYPE type, IDirect3DStateBlock9 **stateblock)
2471 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2472 struct d3d9_stateblock *object;
2473 HRESULT hr;
2475 TRACE("iface %p, type %#x, stateblock %p.\n", iface, type, stateblock);
2477 if (type != D3DSBT_ALL && type != D3DSBT_PIXELSTATE && type != D3DSBT_VERTEXSTATE)
2479 WARN("Unexpected stateblock type, returning D3DERR_INVALIDCALL.\n");
2480 return D3DERR_INVALIDCALL;
2483 wined3d_mutex_lock();
2484 if (device->recording)
2486 wined3d_mutex_unlock();
2487 WARN("Trying to create a stateblock while recording, returning D3DERR_INVALIDCALL.\n");
2488 return D3DERR_INVALIDCALL;
2490 wined3d_mutex_unlock();
2492 if (!(object = heap_alloc_zero(sizeof(*object))))
2493 return E_OUTOFMEMORY;
2495 hr = stateblock_init(object, device, type, NULL);
2496 if (FAILED(hr))
2498 WARN("Failed to initialize stateblock, hr %#x.\n", hr);
2499 heap_free(object);
2500 return hr;
2503 TRACE("Created stateblock %p.\n", object);
2504 *stateblock = &object->IDirect3DStateBlock9_iface;
2506 return D3D_OK;
2509 static HRESULT WINAPI d3d9_device_BeginStateBlock(IDirect3DDevice9Ex *iface)
2511 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2512 struct wined3d_stateblock *stateblock;
2513 HRESULT hr;
2515 TRACE("iface %p.\n", iface);
2517 wined3d_mutex_lock();
2518 if (device->recording)
2520 wined3d_mutex_unlock();
2521 WARN("Trying to begin a stateblock while recording, returning D3DERR_INVALIDCALL.\n");
2522 return D3DERR_INVALIDCALL;
2525 if (SUCCEEDED(hr = wined3d_stateblock_create(device->wined3d_device, device->state, WINED3D_SBT_RECORDED, &stateblock)))
2526 device->update_state = device->recording = stateblock;
2527 wined3d_mutex_unlock();
2529 return hr;
2532 static HRESULT WINAPI d3d9_device_EndStateBlock(IDirect3DDevice9Ex *iface, IDirect3DStateBlock9 **stateblock)
2534 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2535 struct wined3d_stateblock *wined3d_stateblock;
2536 struct d3d9_stateblock *object;
2537 HRESULT hr;
2539 TRACE("iface %p, stateblock %p.\n", iface, stateblock);
2541 wined3d_mutex_lock();
2542 if (!device->recording)
2544 wined3d_mutex_unlock();
2545 WARN("Trying to end a stateblock, but no stateblock is being recorded.\n");
2546 return D3DERR_INVALIDCALL;
2548 wined3d_stateblock = device->recording;
2549 wined3d_stateblock_init_contained_states(wined3d_stateblock);
2550 device->recording = NULL;
2551 device->update_state = device->state;
2552 wined3d_mutex_unlock();
2554 if (!(object = heap_alloc_zero(sizeof(*object))))
2556 wined3d_stateblock_decref(wined3d_stateblock);
2557 return E_OUTOFMEMORY;
2560 hr = stateblock_init(object, device, 0, wined3d_stateblock);
2561 if (FAILED(hr))
2563 WARN("Failed to initialize stateblock, hr %#x.\n", hr);
2564 wined3d_stateblock_decref(wined3d_stateblock);
2565 heap_free(object);
2566 return hr;
2569 TRACE("Created stateblock %p.\n", object);
2570 *stateblock = &object->IDirect3DStateBlock9_iface;
2572 return D3D_OK;
2575 static HRESULT WINAPI d3d9_device_SetClipStatus(IDirect3DDevice9Ex *iface, const D3DCLIPSTATUS9 *clip_status)
2577 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2578 HRESULT hr;
2580 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
2582 wined3d_mutex_lock();
2583 hr = wined3d_device_set_clip_status(device->wined3d_device, (const struct wined3d_clip_status *)clip_status);
2584 wined3d_mutex_unlock();
2586 return hr;
2589 static HRESULT WINAPI d3d9_device_GetClipStatus(IDirect3DDevice9Ex *iface, D3DCLIPSTATUS9 *clip_status)
2591 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2592 HRESULT hr;
2594 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
2596 wined3d_mutex_lock();
2597 hr = wined3d_device_get_clip_status(device->wined3d_device, (struct wined3d_clip_status *)clip_status);
2598 wined3d_mutex_unlock();
2600 return hr;
2603 static HRESULT WINAPI d3d9_device_GetTexture(IDirect3DDevice9Ex *iface, DWORD stage, IDirect3DBaseTexture9 **texture)
2605 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2606 struct wined3d_texture *wined3d_texture = NULL;
2607 const struct wined3d_stateblock_state *state;
2608 struct d3d9_texture *texture_impl;
2610 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
2612 if (!texture)
2613 return D3DERR_INVALIDCALL;
2615 if (stage >= D3DVERTEXTEXTURESAMPLER0 && stage <= D3DVERTEXTEXTURESAMPLER3)
2616 stage -= D3DVERTEXTEXTURESAMPLER0 - WINED3D_VERTEX_SAMPLER_OFFSET;
2618 if (stage >= ARRAY_SIZE(state->textures))
2620 WARN("Ignoring invalid stage %u.\n", stage);
2621 *texture = NULL;
2622 return D3D_OK;
2625 wined3d_mutex_lock();
2626 state = device->stateblock_state;
2627 if ((wined3d_texture = state->textures[stage]))
2629 texture_impl = wined3d_texture_get_parent(wined3d_texture);
2630 *texture = &texture_impl->IDirect3DBaseTexture9_iface;
2631 IDirect3DBaseTexture9_AddRef(*texture);
2633 else
2635 *texture = NULL;
2637 wined3d_mutex_unlock();
2639 return D3D_OK;
2642 static HRESULT WINAPI d3d9_device_SetTexture(IDirect3DDevice9Ex *iface, DWORD stage, IDirect3DBaseTexture9 *texture)
2644 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2645 struct d3d9_texture *texture_impl;
2647 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
2649 texture_impl = unsafe_impl_from_IDirect3DBaseTexture9(texture);
2651 if (stage >= D3DVERTEXTEXTURESAMPLER0 && stage <= D3DVERTEXTEXTURESAMPLER3)
2652 stage -= D3DVERTEXTEXTURESAMPLER0 - WINED3D_VERTEX_SAMPLER_OFFSET;
2654 wined3d_mutex_lock();
2655 wined3d_stateblock_set_texture(device->update_state, stage,
2656 texture_impl ? texture_impl->wined3d_texture : NULL);
2657 if (!device->recording)
2659 if (stage < D3D9_MAX_TEXTURE_UNITS)
2661 if (texture_impl && texture_impl->usage & D3DUSAGE_AUTOGENMIPMAP)
2662 device->auto_mipmaps |= 1u << stage;
2663 else
2664 device->auto_mipmaps &= ~(1u << stage);
2667 wined3d_mutex_unlock();
2669 return D3D_OK;
2672 static const enum wined3d_texture_stage_state tss_lookup[] =
2674 WINED3D_TSS_INVALID, /* 0, unused */
2675 WINED3D_TSS_COLOR_OP, /* 1, D3DTSS_COLOROP */
2676 WINED3D_TSS_COLOR_ARG1, /* 2, D3DTSS_COLORARG1 */
2677 WINED3D_TSS_COLOR_ARG2, /* 3, D3DTSS_COLORARG2 */
2678 WINED3D_TSS_ALPHA_OP, /* 4, D3DTSS_ALPHAOP */
2679 WINED3D_TSS_ALPHA_ARG1, /* 5, D3DTSS_ALPHAARG1 */
2680 WINED3D_TSS_ALPHA_ARG2, /* 6, D3DTSS_ALPHAARG2 */
2681 WINED3D_TSS_BUMPENV_MAT00, /* 7, D3DTSS_BUMPENVMAT00 */
2682 WINED3D_TSS_BUMPENV_MAT01, /* 8, D3DTSS_BUMPENVMAT01 */
2683 WINED3D_TSS_BUMPENV_MAT10, /* 9, D3DTSS_BUMPENVMAT10 */
2684 WINED3D_TSS_BUMPENV_MAT11, /* 10, D3DTSS_BUMPENVMAT11 */
2685 WINED3D_TSS_TEXCOORD_INDEX, /* 11, D3DTSS_TEXCOORDINDEX */
2686 WINED3D_TSS_INVALID, /* 12, unused */
2687 WINED3D_TSS_INVALID, /* 13, unused */
2688 WINED3D_TSS_INVALID, /* 14, unused */
2689 WINED3D_TSS_INVALID, /* 15, unused */
2690 WINED3D_TSS_INVALID, /* 16, unused */
2691 WINED3D_TSS_INVALID, /* 17, unused */
2692 WINED3D_TSS_INVALID, /* 18, unused */
2693 WINED3D_TSS_INVALID, /* 19, unused */
2694 WINED3D_TSS_INVALID, /* 20, unused */
2695 WINED3D_TSS_INVALID, /* 21, unused */
2696 WINED3D_TSS_BUMPENV_LSCALE, /* 22, D3DTSS_BUMPENVLSCALE */
2697 WINED3D_TSS_BUMPENV_LOFFSET, /* 23, D3DTSS_BUMPENVLOFFSET */
2698 WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS, /* 24, D3DTSS_TEXTURETRANSFORMFLAGS */
2699 WINED3D_TSS_INVALID, /* 25, unused */
2700 WINED3D_TSS_COLOR_ARG0, /* 26, D3DTSS_COLORARG0 */
2701 WINED3D_TSS_ALPHA_ARG0, /* 27, D3DTSS_ALPHAARG0 */
2702 WINED3D_TSS_RESULT_ARG, /* 28, D3DTSS_RESULTARG */
2703 WINED3D_TSS_INVALID, /* 29, unused */
2704 WINED3D_TSS_INVALID, /* 30, unused */
2705 WINED3D_TSS_INVALID, /* 31, unused */
2706 WINED3D_TSS_CONSTANT, /* 32, D3DTSS_CONSTANT */
2709 static HRESULT WINAPI d3d9_device_GetTextureStageState(IDirect3DDevice9Ex *iface,
2710 DWORD stage, D3DTEXTURESTAGESTATETYPE state, DWORD *value)
2712 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2714 TRACE("iface %p, stage %u, state %#x, value %p.\n", iface, stage, state, value);
2716 if (state >= ARRAY_SIZE(tss_lookup) || tss_lookup[state] == WINED3D_TSS_INVALID)
2718 WARN("Invalid state %#x passed.\n", state);
2719 return D3D_OK;
2722 wined3d_mutex_lock();
2723 *value = device->stateblock_state->texture_states[stage][tss_lookup[state]];
2724 wined3d_mutex_unlock();
2726 return D3D_OK;
2729 static HRESULT WINAPI d3d9_device_SetTextureStageState(IDirect3DDevice9Ex *iface,
2730 DWORD stage, D3DTEXTURESTAGESTATETYPE state, DWORD value)
2732 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2734 TRACE("iface %p, stage %u, state %#x, value %#x.\n", iface, stage, state, value);
2736 if (state >= ARRAY_SIZE(tss_lookup))
2738 WARN("Invalid state %#x passed.\n", state);
2739 return D3D_OK;
2742 wined3d_mutex_lock();
2743 wined3d_stateblock_set_texture_stage_state(device->update_state, stage, tss_lookup[state], value);
2744 wined3d_mutex_unlock();
2746 return D3D_OK;
2749 static HRESULT WINAPI d3d9_device_GetSamplerState(IDirect3DDevice9Ex *iface,
2750 DWORD sampler_idx, D3DSAMPLERSTATETYPE state, DWORD *value)
2752 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2753 const struct wined3d_stateblock_state *device_state;
2755 TRACE("iface %p, sampler_idx %u, state %#x, value %p.\n", iface, sampler_idx, state, value);
2757 if (sampler_idx >= D3DVERTEXTEXTURESAMPLER0 && sampler_idx <= D3DVERTEXTEXTURESAMPLER3)
2758 sampler_idx -= D3DVERTEXTEXTURESAMPLER0 - WINED3D_VERTEX_SAMPLER_OFFSET;
2760 if (sampler_idx >= ARRAY_SIZE(device_state->sampler_states))
2762 WARN("Invalid sampler %u.\n", sampler_idx);
2763 *value = 0;
2764 return D3D_OK;
2767 wined3d_mutex_lock();
2768 device_state = device->stateblock_state;
2769 *value = device_state->sampler_states[sampler_idx][state];
2770 wined3d_mutex_unlock();
2772 return D3D_OK;
2775 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_SetSamplerState(IDirect3DDevice9Ex *iface,
2776 DWORD sampler, D3DSAMPLERSTATETYPE state, DWORD value)
2778 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2780 TRACE("iface %p, sampler %u, state %#x, value %#x.\n", iface, sampler, state, value);
2782 if (sampler >= D3DVERTEXTEXTURESAMPLER0 && sampler <= D3DVERTEXTEXTURESAMPLER3)
2783 sampler -= D3DVERTEXTEXTURESAMPLER0 - WINED3D_VERTEX_SAMPLER_OFFSET;
2785 wined3d_mutex_lock();
2786 wined3d_stateblock_set_sampler_state(device->update_state, sampler, wined3d_sampler_state_from_d3d(state), value);
2787 wined3d_mutex_unlock();
2789 return D3D_OK;
2792 static HRESULT WINAPI d3d9_device_ValidateDevice(IDirect3DDevice9Ex *iface, DWORD *pass_count)
2794 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2795 HRESULT hr;
2797 TRACE("iface %p, pass_count %p.\n", iface, pass_count);
2799 wined3d_mutex_lock();
2800 wined3d_device_apply_stateblock(device->wined3d_device, device->state);
2801 hr = wined3d_device_validate_device(device->wined3d_device, pass_count);
2802 wined3d_mutex_unlock();
2804 return hr;
2807 static HRESULT WINAPI d3d9_device_SetPaletteEntries(IDirect3DDevice9Ex *iface,
2808 UINT palette_idx, const PALETTEENTRY *entries)
2810 WARN("iface %p, palette_idx %u, entries %p unimplemented.\n", iface, palette_idx, entries);
2812 /* The d3d9 palette API is non-functional on Windows. Getters and setters are implemented,
2813 * and some drivers allow the creation of P8 surfaces. These surfaces can be copied to
2814 * other P8 surfaces with StretchRect, but cannot be converted to (A)RGB.
2816 * Some older(dx7) cards may have support for P8 textures, but games cannot rely on this. */
2817 return D3D_OK;
2820 static HRESULT WINAPI d3d9_device_GetPaletteEntries(IDirect3DDevice9Ex *iface,
2821 UINT palette_idx, PALETTEENTRY *entries)
2823 FIXME("iface %p, palette_idx %u, entries %p unimplemented.\n", iface, palette_idx, entries);
2825 return D3DERR_INVALIDCALL;
2828 static HRESULT WINAPI d3d9_device_SetCurrentTexturePalette(IDirect3DDevice9Ex *iface, UINT palette_idx)
2830 WARN("iface %p, palette_idx %u unimplemented.\n", iface, palette_idx);
2832 return D3D_OK;
2835 static HRESULT WINAPI d3d9_device_GetCurrentTexturePalette(IDirect3DDevice9Ex *iface, UINT *palette_idx)
2837 FIXME("iface %p, palette_idx %p.\n", iface, palette_idx);
2839 return D3DERR_INVALIDCALL;
2842 static HRESULT WINAPI d3d9_device_SetScissorRect(IDirect3DDevice9Ex *iface, const RECT *rect)
2844 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2846 TRACE("iface %p, rect %p.\n", iface, rect);
2848 wined3d_mutex_lock();
2849 wined3d_stateblock_set_scissor_rect(device->update_state, rect);
2850 wined3d_mutex_unlock();
2852 return D3D_OK;
2855 static HRESULT WINAPI d3d9_device_GetScissorRect(IDirect3DDevice9Ex *iface, RECT *rect)
2857 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2859 TRACE("iface %p, rect %p.\n", iface, rect);
2861 wined3d_mutex_lock();
2862 *rect = device->stateblock_state->scissor_rect;
2863 wined3d_mutex_unlock();
2865 return D3D_OK;
2868 static HRESULT WINAPI d3d9_device_SetSoftwareVertexProcessing(IDirect3DDevice9Ex *iface, BOOL software)
2870 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2872 TRACE("iface %p, software %#x.\n", iface, software);
2874 wined3d_mutex_lock();
2875 wined3d_device_set_software_vertex_processing(device->wined3d_device, software);
2876 wined3d_mutex_unlock();
2878 return D3D_OK;
2881 static BOOL WINAPI d3d9_device_GetSoftwareVertexProcessing(IDirect3DDevice9Ex *iface)
2883 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2884 BOOL ret;
2886 TRACE("iface %p.\n", iface);
2888 wined3d_mutex_lock();
2889 ret = wined3d_device_get_software_vertex_processing(device->wined3d_device);
2890 wined3d_mutex_unlock();
2892 return ret;
2895 static HRESULT WINAPI d3d9_device_SetNPatchMode(IDirect3DDevice9Ex *iface, float segment_count)
2897 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2898 HRESULT hr;
2900 TRACE("iface %p, segment_count %.8e.\n", iface, segment_count);
2902 wined3d_mutex_lock();
2903 hr = wined3d_device_set_npatch_mode(device->wined3d_device, segment_count);
2904 wined3d_mutex_unlock();
2906 return hr;
2909 static float WINAPI d3d9_device_GetNPatchMode(IDirect3DDevice9Ex *iface)
2911 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2912 float ret;
2914 TRACE("iface %p.\n", iface);
2916 wined3d_mutex_lock();
2917 ret = wined3d_device_get_npatch_mode(device->wined3d_device);
2918 wined3d_mutex_unlock();
2920 return ret;
2923 /* wined3d critical section must be taken by the caller. */
2924 static void d3d9_generate_auto_mipmaps(struct d3d9_device *device)
2926 const struct wined3d_stateblock_state *state = device->stateblock_state;
2927 struct wined3d_texture *texture;
2928 unsigned int i, map;
2930 map = device->auto_mipmaps;
2931 while (map)
2933 i = wined3d_bit_scan(&map);
2934 if ((texture = state->textures[i]))
2935 d3d9_texture_gen_auto_mipmap(wined3d_texture_get_parent(texture));
2939 static void d3d9_device_upload_sysmem_vertex_buffers(struct d3d9_device *device,
2940 int base_vertex, unsigned int start_vertex, unsigned int vertex_count)
2942 const struct wined3d_stateblock_state *state = device->stateblock_state;
2943 struct wined3d_vertex_declaration *wined3d_decl;
2944 struct wined3d_box box = {0, 0, 0, 1, 0, 1};
2945 const struct wined3d_stream_state *stream;
2946 struct d3d9_vertexbuffer *d3d9_buffer;
2947 struct wined3d_resource *dst_resource;
2948 struct d3d9_vertex_declaration *decl;
2949 struct wined3d_buffer *dst_buffer;
2950 struct wined3d_resource_desc desc;
2951 unsigned int stride, map;
2952 HRESULT hr;
2954 if (!device->sysmem_vb)
2955 return;
2956 wined3d_decl = state->vertex_declaration;
2957 if (!wined3d_decl)
2958 return;
2960 if (base_vertex >= 0 || start_vertex >= -base_vertex)
2961 start_vertex += base_vertex;
2962 else
2963 FIXME("System memory vertex data offset is negative.\n");
2965 decl = wined3d_vertex_declaration_get_parent(wined3d_decl);
2966 map = decl->stream_map & device->sysmem_vb;
2967 while (map)
2969 stream = &state->streams[wined3d_bit_scan(&map)];
2970 dst_buffer = stream->buffer;
2971 stride = stream->stride;
2973 d3d9_buffer = wined3d_buffer_get_parent(dst_buffer);
2974 dst_resource = wined3d_buffer_get_resource(dst_buffer);
2975 wined3d_resource_get_desc(dst_resource, &desc);
2976 box.left = stream->offset + start_vertex * stride;
2977 box.right = min(box.left + vertex_count * stride, desc.size);
2978 if (FAILED(hr = wined3d_device_context_copy_sub_resource_region(device->immediate_context,
2979 dst_resource, 0, box.left, 0, 0,
2980 wined3d_buffer_get_resource(d3d9_buffer->wined3d_buffer), 0, &box, 0)))
2981 ERR("Failed to update buffer.\n");
2985 static void d3d9_device_upload_sysmem_index_buffer(struct d3d9_device *device,
2986 unsigned int start_idx, unsigned int idx_count)
2988 const struct wined3d_stateblock_state *state = device->stateblock_state;
2989 struct wined3d_box box = {0, 0, 0, 1, 0, 1};
2990 struct wined3d_resource *dst_resource;
2991 struct d3d9_indexbuffer *d3d9_buffer;
2992 struct wined3d_buffer *dst_buffer;
2993 struct wined3d_resource_desc desc;
2994 enum wined3d_format_id format;
2995 unsigned int idx_size;
2996 HRESULT hr;
2998 if (!device->sysmem_ib)
2999 return;
3001 dst_buffer = state->index_buffer;
3002 format = state->index_format;
3003 d3d9_buffer = wined3d_buffer_get_parent(dst_buffer);
3004 dst_resource = wined3d_buffer_get_resource(dst_buffer);
3005 wined3d_resource_get_desc(dst_resource, &desc);
3006 idx_size = format == WINED3DFMT_R16_UINT ? 2 : 4;
3007 box.left = start_idx * idx_size;
3008 box.right = min(box.left + idx_count * idx_size, desc.size);
3009 if (FAILED(hr = wined3d_device_context_copy_sub_resource_region(device->immediate_context,
3010 dst_resource, 0, box.left, 0, 0,
3011 wined3d_buffer_get_resource(d3d9_buffer->wined3d_buffer), 0, &box, 0)))
3012 ERR("Failed to update buffer.\n");
3015 static HRESULT WINAPI d3d9_device_DrawPrimitive(IDirect3DDevice9Ex *iface,
3016 D3DPRIMITIVETYPE primitive_type, UINT start_vertex, UINT primitive_count)
3018 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3019 unsigned int vertex_count;
3021 TRACE("iface %p, primitive_type %#x, start_vertex %u, primitive_count %u.\n",
3022 iface, primitive_type, start_vertex, primitive_count);
3024 wined3d_mutex_lock();
3025 if (!device->stateblock_state->vertex_declaration)
3027 wined3d_mutex_unlock();
3028 WARN("Called without a valid vertex declaration set.\n");
3029 return D3DERR_INVALIDCALL;
3031 wined3d_device_apply_stateblock(device->wined3d_device, device->state);
3032 vertex_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
3033 d3d9_device_upload_sysmem_vertex_buffers(device, 0, start_vertex, vertex_count);
3034 d3d9_generate_auto_mipmaps(device);
3035 wined3d_device_context_set_primitive_type(device->immediate_context,
3036 wined3d_primitive_type_from_d3d(primitive_type), 0);
3037 wined3d_device_context_draw(device->immediate_context, start_vertex, vertex_count, 0, 0);
3038 d3d9_rts_flag_auto_gen_mipmap(device);
3039 wined3d_mutex_unlock();
3041 return D3D_OK;
3044 static HRESULT WINAPI d3d9_device_DrawIndexedPrimitive(IDirect3DDevice9Ex *iface,
3045 D3DPRIMITIVETYPE primitive_type, INT base_vertex_idx, UINT min_vertex_idx,
3046 UINT vertex_count, UINT start_idx, UINT primitive_count)
3048 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3049 unsigned int index_count;
3051 TRACE("iface %p, primitive_type %#x, base_vertex_idx %u, min_vertex_idx %u, "
3052 "vertex_count %u, start_idx %u, primitive_count %u.\n",
3053 iface, primitive_type, base_vertex_idx, min_vertex_idx,
3054 vertex_count, start_idx, primitive_count);
3056 wined3d_mutex_lock();
3057 if (!device->stateblock_state->vertex_declaration)
3059 wined3d_mutex_unlock();
3060 WARN("Called without a valid vertex declaration set.\n");
3061 return D3DERR_INVALIDCALL;
3063 if (!device->stateblock_state->index_buffer)
3065 wined3d_mutex_unlock();
3066 WARN("Called without a valid index buffer set.\n");
3067 return D3DERR_INVALIDCALL;
3069 index_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
3070 d3d9_device_upload_sysmem_vertex_buffers(device, base_vertex_idx, min_vertex_idx, vertex_count);
3071 d3d9_device_upload_sysmem_index_buffer(device, start_idx, index_count);
3072 d3d9_generate_auto_mipmaps(device);
3073 wined3d_device_context_set_primitive_type(device->immediate_context,
3074 wined3d_primitive_type_from_d3d(primitive_type), 0);
3075 wined3d_device_apply_stateblock(device->wined3d_device, device->state);
3076 wined3d_device_context_draw_indexed(device->immediate_context, base_vertex_idx, start_idx, index_count, 0, 0);
3077 d3d9_rts_flag_auto_gen_mipmap(device);
3078 wined3d_mutex_unlock();
3080 return D3D_OK;
3083 /* The caller is responsible for wined3d locking */
3084 static HRESULT d3d9_device_prepare_vertex_buffer(struct d3d9_device *device, UINT min_size)
3086 HRESULT hr;
3088 if (device->vertex_buffer_size < min_size || !device->vertex_buffer)
3090 UINT size = max(device->vertex_buffer_size * 2, min_size);
3091 struct wined3d_buffer_desc desc;
3092 struct wined3d_buffer *buffer;
3094 TRACE("Growing vertex buffer to %u bytes.\n", size);
3096 desc.byte_width = size;
3097 desc.usage = WINED3DUSAGE_DYNAMIC;
3098 desc.bind_flags = WINED3D_BIND_VERTEX_BUFFER;
3099 desc.access = WINED3D_RESOURCE_ACCESS_GPU | WINED3D_RESOURCE_ACCESS_MAP_W;
3100 desc.misc_flags = 0;
3101 desc.structure_byte_stride = 0;
3103 if (FAILED(hr = wined3d_buffer_create(device->wined3d_device, &desc,
3104 NULL, NULL, &d3d9_null_wined3d_parent_ops, &buffer)))
3106 ERR("Failed to create vertex buffer, hr %#x.\n", hr);
3107 return hr;
3110 if (device->vertex_buffer)
3111 wined3d_buffer_decref(device->vertex_buffer);
3113 device->vertex_buffer = buffer;
3114 device->vertex_buffer_size = size;
3115 device->vertex_buffer_pos = 0;
3117 return D3D_OK;
3120 static HRESULT WINAPI d3d9_device_DrawPrimitiveUP(IDirect3DDevice9Ex *iface,
3121 D3DPRIMITIVETYPE primitive_type, UINT primitive_count, const void *data, UINT stride)
3123 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3124 HRESULT hr;
3125 UINT vtx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
3126 struct wined3d_map_desc wined3d_map_desc;
3127 struct wined3d_box wined3d_box = {0};
3128 UINT size = vtx_count * stride;
3129 struct wined3d_resource *vb;
3130 UINT vb_pos, align;
3132 TRACE("iface %p, primitive_type %#x, primitive_count %u, data %p, stride %u.\n",
3133 iface, primitive_type, primitive_count, data, stride);
3135 if (!stride)
3137 WARN("stride is 0, returning D3DERR_INVALIDCALL.\n");
3138 return D3DERR_INVALIDCALL;
3140 if (!primitive_count)
3142 WARN("primitive_count is 0, returning D3D_OK.\n");
3143 return D3D_OK;
3146 wined3d_mutex_lock();
3148 if (!device->stateblock_state->vertex_declaration)
3150 wined3d_mutex_unlock();
3151 WARN("Called without a valid vertex declaration set.\n");
3152 return D3DERR_INVALIDCALL;
3155 hr = d3d9_device_prepare_vertex_buffer(device, size);
3156 if (FAILED(hr))
3157 goto done;
3159 vb_pos = device->vertex_buffer_pos;
3160 align = vb_pos % stride;
3161 if (align) align = stride - align;
3162 if (vb_pos + size + align > device->vertex_buffer_size)
3163 vb_pos = 0;
3164 else
3165 vb_pos += align;
3167 wined3d_box.left = vb_pos;
3168 wined3d_box.right = vb_pos + size;
3169 vb = wined3d_buffer_get_resource(device->vertex_buffer);
3170 if (FAILED(hr = wined3d_resource_map(vb, 0, &wined3d_map_desc, &wined3d_box,
3171 WINED3D_MAP_WRITE | (vb_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD))))
3172 goto done;
3173 memcpy(wined3d_map_desc.data, data, size);
3174 wined3d_resource_unmap(vb, 0);
3175 device->vertex_buffer_pos = vb_pos + size;
3177 hr = wined3d_stateblock_set_stream_source(device->state, 0, device->vertex_buffer, 0, stride);
3178 if (FAILED(hr))
3179 goto done;
3181 d3d9_generate_auto_mipmaps(device);
3182 wined3d_device_context_set_primitive_type(device->immediate_context,
3183 wined3d_primitive_type_from_d3d(primitive_type), 0);
3184 wined3d_device_apply_stateblock(device->wined3d_device, device->state);
3185 wined3d_device_context_draw(device->immediate_context, vb_pos / stride, vtx_count, 0, 0);
3186 wined3d_stateblock_set_stream_source(device->state, 0, NULL, 0, 0);
3187 d3d9_rts_flag_auto_gen_mipmap(device);
3189 done:
3190 wined3d_mutex_unlock();
3191 return hr;
3194 /* The caller is responsible for wined3d locking */
3195 static HRESULT d3d9_device_prepare_index_buffer(struct d3d9_device *device, UINT min_size)
3197 HRESULT hr;
3199 if (device->index_buffer_size < min_size || !device->index_buffer)
3201 UINT size = max(device->index_buffer_size * 2, min_size);
3202 struct wined3d_buffer_desc desc;
3203 struct wined3d_buffer *buffer;
3205 TRACE("Growing index buffer to %u bytes.\n", size);
3207 desc.byte_width = size;
3208 desc.usage = WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_STATICDECL;
3209 desc.bind_flags = WINED3D_BIND_INDEX_BUFFER;
3210 desc.access = WINED3D_RESOURCE_ACCESS_GPU | WINED3D_RESOURCE_ACCESS_MAP_W;
3211 desc.misc_flags = 0;
3212 desc.structure_byte_stride = 0;
3214 if (FAILED(hr = wined3d_buffer_create(device->wined3d_device, &desc,
3215 NULL, NULL, &d3d9_null_wined3d_parent_ops, &buffer)))
3217 ERR("Failed to create index buffer, hr %#x.\n", hr);
3218 return hr;
3221 if (device->index_buffer)
3222 wined3d_buffer_decref(device->index_buffer);
3224 device->index_buffer = buffer;
3225 device->index_buffer_size = size;
3226 device->index_buffer_pos = 0;
3228 return D3D_OK;
3231 static HRESULT WINAPI d3d9_device_DrawIndexedPrimitiveUP(IDirect3DDevice9Ex *iface,
3232 D3DPRIMITIVETYPE primitive_type, UINT min_vertex_idx, UINT vertex_count,
3233 UINT primitive_count, const void *index_data, D3DFORMAT index_format,
3234 const void *vertex_data, UINT vertex_stride)
3236 UINT idx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
3237 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3238 UINT idx_fmt_size = index_format == D3DFMT_INDEX16 ? 2 : 4;
3239 UINT vtx_size = vertex_count * vertex_stride;
3240 UINT idx_size = idx_count * idx_fmt_size;
3241 struct wined3d_map_desc wined3d_map_desc;
3242 struct wined3d_box wined3d_box = {0};
3243 struct wined3d_resource *ib, *vb;
3244 UINT vb_pos, ib_pos, align;
3245 HRESULT hr;
3247 TRACE("iface %p, primitive_type %#x, min_vertex_idx %u, vertex_count %u, primitive_count %u, "
3248 "index_data %p, index_format %#x, vertex_data %p, vertex_stride %u.\n",
3249 iface, primitive_type, min_vertex_idx, vertex_count, primitive_count,
3250 index_data, index_format, vertex_data, vertex_stride);
3252 if (!vertex_stride)
3254 WARN("vertex_stride is 0, returning D3DERR_INVALIDCALL.\n");
3255 return D3DERR_INVALIDCALL;
3257 if (!primitive_count)
3259 WARN("primitive_count is 0, returning D3D_OK.\n");
3260 return D3D_OK;
3263 wined3d_mutex_lock();
3265 if (!device->stateblock_state->vertex_declaration)
3267 wined3d_mutex_unlock();
3268 WARN("Called without a valid vertex declaration set.\n");
3269 return D3DERR_INVALIDCALL;
3272 hr = d3d9_device_prepare_vertex_buffer(device, vtx_size);
3273 if (FAILED(hr))
3274 goto done;
3276 vb_pos = device->vertex_buffer_pos;
3277 align = vb_pos % vertex_stride;
3278 if (align) align = vertex_stride - align;
3279 if (vb_pos + vtx_size + align > device->vertex_buffer_size)
3280 vb_pos = 0;
3281 else
3282 vb_pos += align;
3284 wined3d_box.left = vb_pos;
3285 wined3d_box.right = vb_pos + vtx_size;
3286 vb = wined3d_buffer_get_resource(device->vertex_buffer);
3287 if (FAILED(hr = wined3d_resource_map(vb, 0, &wined3d_map_desc, &wined3d_box,
3288 WINED3D_MAP_WRITE | (vb_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD))))
3289 goto done;
3290 memcpy(wined3d_map_desc.data, (char *)vertex_data + min_vertex_idx * vertex_stride, vtx_size);
3291 wined3d_resource_unmap(vb, 0);
3292 device->vertex_buffer_pos = vb_pos + vtx_size;
3294 hr = d3d9_device_prepare_index_buffer(device, idx_size);
3295 if (FAILED(hr))
3296 goto done;
3298 ib_pos = device->index_buffer_pos;
3299 align = ib_pos % idx_fmt_size;
3300 if (align) align = idx_fmt_size - align;
3301 if (ib_pos + idx_size + align > device->index_buffer_size)
3302 ib_pos = 0;
3303 else
3304 ib_pos += align;
3306 wined3d_box.left = ib_pos;
3307 wined3d_box.right = ib_pos + idx_size;
3308 ib = wined3d_buffer_get_resource(device->index_buffer);
3309 if (FAILED(hr = wined3d_resource_map(ib, 0, &wined3d_map_desc, &wined3d_box,
3310 WINED3D_MAP_WRITE | (ib_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD))))
3311 goto done;
3312 memcpy(wined3d_map_desc.data, index_data, idx_size);
3313 wined3d_resource_unmap(ib, 0);
3314 device->index_buffer_pos = ib_pos + idx_size;
3316 hr = wined3d_stateblock_set_stream_source(device->state, 0, device->vertex_buffer, 0, vertex_stride);
3317 if (FAILED(hr))
3318 goto done;
3320 d3d9_generate_auto_mipmaps(device);
3321 wined3d_stateblock_set_index_buffer(device->state, device->index_buffer,
3322 wined3dformat_from_d3dformat(index_format));
3324 wined3d_device_apply_stateblock(device->wined3d_device, device->state);
3325 wined3d_device_context_set_primitive_type(device->immediate_context,
3326 wined3d_primitive_type_from_d3d(primitive_type), 0);
3327 wined3d_device_context_draw_indexed(device->immediate_context,
3328 vb_pos / vertex_stride - min_vertex_idx, ib_pos / idx_fmt_size, idx_count, 0, 0);
3330 wined3d_stateblock_set_stream_source(device->state, 0, NULL, 0, 0);
3331 wined3d_stateblock_set_index_buffer(device->state, NULL, WINED3DFMT_UNKNOWN);
3333 d3d9_rts_flag_auto_gen_mipmap(device);
3335 done:
3336 wined3d_mutex_unlock();
3337 return hr;
3340 static HRESULT WINAPI d3d9_device_ProcessVertices(IDirect3DDevice9Ex *iface,
3341 UINT src_start_idx, UINT dst_idx, UINT vertex_count, IDirect3DVertexBuffer9 *dst_buffer,
3342 IDirect3DVertexDeclaration9 *declaration, DWORD flags)
3344 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3345 struct d3d9_vertexbuffer *dst_impl = unsafe_impl_from_IDirect3DVertexBuffer9(dst_buffer);
3346 struct d3d9_vertex_declaration *decl_impl = unsafe_impl_from_IDirect3DVertexDeclaration9(declaration);
3347 const struct wined3d_stateblock_state *state;
3348 const struct wined3d_stream_state *stream;
3349 struct d3d9_vertexbuffer *d3d9_buffer;
3350 unsigned int i, map;
3351 HRESULT hr;
3353 TRACE("iface %p, src_start_idx %u, dst_idx %u, vertex_count %u, dst_buffer %p, declaration %p, flags %#x.\n",
3354 iface, src_start_idx, dst_idx, vertex_count, dst_buffer, declaration, flags);
3356 wined3d_mutex_lock();
3357 state = device->stateblock_state;
3359 /* Note that an alternative approach would be to simply create these
3360 * buffers with WINED3D_RESOURCE_ACCESS_MAP_R and update them here like we
3361 * do for draws. In some regards that would be easier, but it seems less
3362 * than optimal to upload data to the GPU only to subsequently download it
3363 * again. */
3364 map = device->sysmem_vb;
3365 while (map)
3367 i = wined3d_bit_scan(&map);
3368 stream = &state->streams[i];
3370 d3d9_buffer = wined3d_buffer_get_parent(stream->buffer);
3371 if (FAILED(wined3d_stateblock_set_stream_source(device->state,
3372 i, d3d9_buffer->wined3d_buffer, stream->offset, stream->stride)))
3373 ERR("Failed to set stream source.\n");
3376 wined3d_device_apply_stateblock(device->wined3d_device, device->state);
3377 hr = wined3d_device_process_vertices(device->wined3d_device, src_start_idx, dst_idx, vertex_count,
3378 dst_impl->wined3d_buffer, decl_impl ? decl_impl->wined3d_declaration : NULL,
3379 flags, dst_impl->fvf);
3381 map = device->sysmem_vb;
3382 while (map)
3384 i = wined3d_bit_scan(&map);
3385 stream = &state->streams[i];
3387 d3d9_buffer = wined3d_buffer_get_parent(stream->buffer);
3388 if (FAILED(wined3d_stateblock_set_stream_source(device->state,
3389 i, d3d9_buffer->draw_buffer, stream->offset, stream->stride)))
3390 ERR("Failed to set stream source.\n");
3393 wined3d_mutex_unlock();
3395 return hr;
3398 static HRESULT WINAPI d3d9_device_CreateVertexDeclaration(IDirect3DDevice9Ex *iface,
3399 const D3DVERTEXELEMENT9 *elements, IDirect3DVertexDeclaration9 **declaration)
3401 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3402 struct d3d9_vertex_declaration *object;
3403 HRESULT hr;
3405 TRACE("iface %p, elements %p, declaration %p.\n", iface, elements, declaration);
3407 if (!declaration)
3409 WARN("Caller passed a NULL declaration, returning D3DERR_INVALIDCALL.\n");
3410 return D3DERR_INVALIDCALL;
3413 if (SUCCEEDED(hr = d3d9_vertex_declaration_create(device, elements, &object)))
3414 *declaration = &object->IDirect3DVertexDeclaration9_iface;
3416 return hr;
3419 static HRESULT WINAPI d3d9_device_SetVertexDeclaration(IDirect3DDevice9Ex *iface,
3420 IDirect3DVertexDeclaration9 *declaration)
3422 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3423 struct d3d9_vertex_declaration *decl_impl = unsafe_impl_from_IDirect3DVertexDeclaration9(declaration);
3425 TRACE("iface %p, declaration %p.\n", iface, declaration);
3427 wined3d_mutex_lock();
3428 wined3d_stateblock_set_vertex_declaration(device->update_state,
3429 decl_impl ? decl_impl->wined3d_declaration : NULL);
3430 wined3d_mutex_unlock();
3432 return D3D_OK;
3435 static HRESULT WINAPI d3d9_device_GetVertexDeclaration(IDirect3DDevice9Ex *iface,
3436 IDirect3DVertexDeclaration9 **declaration)
3438 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3439 struct wined3d_vertex_declaration *wined3d_declaration;
3440 struct d3d9_vertex_declaration *declaration_impl;
3442 TRACE("iface %p, declaration %p.\n", iface, declaration);
3444 if (!declaration) return D3DERR_INVALIDCALL;
3446 wined3d_mutex_lock();
3447 if ((wined3d_declaration = device->stateblock_state->vertex_declaration))
3449 declaration_impl = wined3d_vertex_declaration_get_parent(wined3d_declaration);
3450 *declaration = &declaration_impl->IDirect3DVertexDeclaration9_iface;
3451 IDirect3DVertexDeclaration9_AddRef(*declaration);
3453 else
3455 *declaration = NULL;
3457 wined3d_mutex_unlock();
3459 TRACE("Returning %p.\n", *declaration);
3460 return D3D_OK;
3463 static struct wined3d_vertex_declaration *device_get_fvf_declaration(struct d3d9_device *device, DWORD fvf)
3465 struct wined3d_vertex_declaration *wined3d_declaration;
3466 struct fvf_declaration *fvf_decls = device->fvf_decls;
3467 struct d3d9_vertex_declaration *d3d9_declaration;
3468 D3DVERTEXELEMENT9 *elements;
3469 int p, low, high; /* deliberately signed */
3470 HRESULT hr;
3472 TRACE("Searching for declaration for fvf %08x... ", fvf);
3474 low = 0;
3475 high = device->fvf_decl_count - 1;
3476 while (low <= high)
3478 p = (low + high) >> 1;
3479 TRACE("%d ", p);
3481 if (fvf_decls[p].fvf == fvf)
3483 TRACE("found %p.\n", fvf_decls[p].decl);
3484 return fvf_decls[p].decl;
3487 if (fvf_decls[p].fvf < fvf)
3488 low = p + 1;
3489 else
3490 high = p - 1;
3492 TRACE("not found. Creating and inserting at position %d.\n", low);
3494 if (FAILED(hr = vdecl_convert_fvf(fvf, &elements)))
3495 return NULL;
3497 hr = d3d9_vertex_declaration_create(device, elements, &d3d9_declaration);
3498 heap_free(elements);
3499 if (FAILED(hr))
3500 return NULL;
3502 if (device->fvf_decl_size == device->fvf_decl_count)
3504 UINT grow = max(device->fvf_decl_size / 2, 8);
3506 if (!(fvf_decls = heap_realloc(fvf_decls, sizeof(*fvf_decls) * (device->fvf_decl_size + grow))))
3508 IDirect3DVertexDeclaration9_Release(&d3d9_declaration->IDirect3DVertexDeclaration9_iface);
3509 return NULL;
3511 device->fvf_decls = fvf_decls;
3512 device->fvf_decl_size += grow;
3515 d3d9_declaration->fvf = fvf;
3516 wined3d_declaration = d3d9_declaration->wined3d_declaration;
3517 wined3d_vertex_declaration_incref(wined3d_declaration);
3518 IDirect3DVertexDeclaration9_Release(&d3d9_declaration->IDirect3DVertexDeclaration9_iface);
3520 memmove(fvf_decls + low + 1, fvf_decls + low, sizeof(*fvf_decls) * (device->fvf_decl_count - low));
3521 fvf_decls[low].decl = wined3d_declaration;
3522 fvf_decls[low].fvf = fvf;
3523 ++device->fvf_decl_count;
3525 TRACE("Returning %p. %u declarations in array.\n", wined3d_declaration, device->fvf_decl_count);
3527 return wined3d_declaration;
3530 static HRESULT WINAPI d3d9_device_SetFVF(IDirect3DDevice9Ex *iface, DWORD fvf)
3532 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3533 struct wined3d_vertex_declaration *decl;
3535 TRACE("iface %p, fvf %#x.\n", iface, fvf);
3537 if (!fvf)
3539 WARN("%#x is not a valid FVF.\n", fvf);
3540 return D3D_OK;
3543 wined3d_mutex_lock();
3544 if (!(decl = device_get_fvf_declaration(device, fvf)))
3546 wined3d_mutex_unlock();
3547 ERR("Failed to create a vertex declaration for fvf %#x.\n", fvf);
3548 return D3DERR_DRIVERINTERNALERROR;
3551 wined3d_stateblock_set_vertex_declaration(device->update_state, decl);
3552 wined3d_mutex_unlock();
3554 return D3D_OK;
3557 static HRESULT WINAPI d3d9_device_GetFVF(IDirect3DDevice9Ex *iface, DWORD *fvf)
3559 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3560 struct wined3d_vertex_declaration *wined3d_declaration;
3561 struct d3d9_vertex_declaration *d3d9_declaration;
3563 TRACE("iface %p, fvf %p.\n", iface, fvf);
3565 wined3d_mutex_lock();
3566 if ((wined3d_declaration = device->stateblock_state->vertex_declaration))
3568 d3d9_declaration = wined3d_vertex_declaration_get_parent(wined3d_declaration);
3569 *fvf = d3d9_declaration->fvf;
3571 else
3573 *fvf = 0;
3575 wined3d_mutex_unlock();
3577 TRACE("Returning FVF %#x.\n", *fvf);
3579 return D3D_OK;
3582 static HRESULT WINAPI d3d9_device_CreateVertexShader(IDirect3DDevice9Ex *iface,
3583 const DWORD *byte_code, IDirect3DVertexShader9 **shader)
3585 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3586 struct d3d9_vertexshader *object;
3587 HRESULT hr;
3589 TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
3591 if (!(object = heap_alloc_zero(sizeof(*object))))
3592 return E_OUTOFMEMORY;
3594 hr = vertexshader_init(object, device, byte_code);
3595 if (FAILED(hr))
3597 WARN("Failed to initialize vertex shader, hr %#x.\n", hr);
3598 heap_free(object);
3599 return hr;
3602 TRACE("Created vertex shader %p.\n", object);
3603 *shader = &object->IDirect3DVertexShader9_iface;
3605 return D3D_OK;
3608 static HRESULT WINAPI d3d9_device_SetVertexShader(IDirect3DDevice9Ex *iface, IDirect3DVertexShader9 *shader)
3610 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3611 struct d3d9_vertexshader *shader_obj = unsafe_impl_from_IDirect3DVertexShader9(shader);
3613 TRACE("iface %p, shader %p.\n", iface, shader);
3615 wined3d_mutex_lock();
3616 wined3d_stateblock_set_vertex_shader(device->update_state,
3617 shader_obj ? shader_obj->wined3d_shader : NULL);
3618 wined3d_mutex_unlock();
3620 return D3D_OK;
3623 static HRESULT WINAPI d3d9_device_GetVertexShader(IDirect3DDevice9Ex *iface, IDirect3DVertexShader9 **shader)
3625 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3626 struct d3d9_vertexshader *shader_impl;
3627 struct wined3d_shader *wined3d_shader;
3629 TRACE("iface %p, shader %p.\n", iface, shader);
3631 wined3d_mutex_lock();
3632 if ((wined3d_shader = device->stateblock_state->vs))
3634 shader_impl = wined3d_shader_get_parent(wined3d_shader);
3635 *shader = &shader_impl->IDirect3DVertexShader9_iface;
3636 IDirect3DVertexShader9_AddRef(*shader);
3638 else
3640 *shader = NULL;
3642 wined3d_mutex_unlock();
3644 TRACE("Returning %p.\n", *shader);
3646 return D3D_OK;
3649 static HRESULT WINAPI d3d9_device_SetVertexShaderConstantF(IDirect3DDevice9Ex *iface,
3650 UINT reg_idx, const float *data, UINT count)
3652 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3653 HRESULT hr;
3655 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3657 if (reg_idx + count > D3D9_MAX_VERTEX_SHADER_CONSTANTF)
3659 WARN("Trying to access %u constants, but d3d9 only supports %u\n",
3660 reg_idx + count, D3D9_MAX_VERTEX_SHADER_CONSTANTF);
3661 return D3DERR_INVALIDCALL;
3664 wined3d_mutex_lock();
3665 hr = wined3d_stateblock_set_vs_consts_f(device->update_state, reg_idx,
3666 count, (const struct wined3d_vec4 *)data);
3667 wined3d_mutex_unlock();
3669 return hr;
3672 static HRESULT WINAPI d3d9_device_GetVertexShaderConstantF(IDirect3DDevice9Ex *iface,
3673 UINT start_idx, float *constants, UINT count)
3675 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3676 const struct wined3d_vec4 *src;
3678 TRACE("iface %p, start_idx %u, constants %p, count %u.\n", iface, start_idx, constants, count);
3680 if (!constants)
3681 return D3DERR_INVALIDCALL;
3683 if (!wined3d_bound_range(start_idx, count, device->vs_uniform_count))
3685 WARN("Trying to access %u constants, but d3d9 only supports %u\n",
3686 start_idx + count, device->vs_uniform_count);
3687 return D3DERR_INVALIDCALL;
3690 wined3d_mutex_lock();
3691 src = device->stateblock_state->vs_consts_f;
3692 memcpy(constants, &src[start_idx], count * sizeof(*src));
3693 wined3d_mutex_unlock();
3695 return D3D_OK;
3698 static HRESULT WINAPI d3d9_device_SetVertexShaderConstantI(IDirect3DDevice9Ex *iface,
3699 UINT reg_idx, const int *data, UINT count)
3701 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3702 HRESULT hr;
3704 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3706 wined3d_mutex_lock();
3707 hr = wined3d_stateblock_set_vs_consts_i(device->update_state,
3708 reg_idx, count, (const struct wined3d_ivec4 *)data);
3709 wined3d_mutex_unlock();
3711 return hr;
3714 static HRESULT WINAPI d3d9_device_GetVertexShaderConstantI(IDirect3DDevice9Ex *iface,
3715 UINT start_idx, int *constants, UINT count)
3717 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3718 const struct wined3d_ivec4 *src;
3720 TRACE("iface %p, start_idx %u, constants %p, count %u.\n", iface, start_idx, constants, count);
3722 if (!constants || start_idx >= WINED3D_MAX_CONSTS_I)
3723 return WINED3DERR_INVALIDCALL;
3724 if (count > WINED3D_MAX_CONSTS_I - start_idx)
3725 count = WINED3D_MAX_CONSTS_I - start_idx;
3727 wined3d_mutex_lock();
3728 src = device->stateblock_state->vs_consts_i;
3729 memcpy(constants, &src[start_idx], count * sizeof(*src));
3730 wined3d_mutex_unlock();
3732 return D3D_OK;
3735 static HRESULT WINAPI d3d9_device_SetVertexShaderConstantB(IDirect3DDevice9Ex *iface,
3736 UINT reg_idx, const BOOL *data, UINT count)
3738 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3739 HRESULT hr;
3741 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3743 wined3d_mutex_lock();
3744 hr = wined3d_stateblock_set_vs_consts_b(device->update_state, reg_idx, count, data);
3745 wined3d_mutex_unlock();
3747 return hr;
3750 static HRESULT WINAPI d3d9_device_GetVertexShaderConstantB(IDirect3DDevice9Ex *iface,
3751 UINT start_idx, BOOL *constants, UINT count)
3753 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3755 TRACE("iface %p, start_idx %u, constants %p, count %u.\n", iface, start_idx, constants, count);
3757 if (!constants || start_idx >= WINED3D_MAX_CONSTS_B)
3758 return WINED3DERR_INVALIDCALL;
3759 if (count > WINED3D_MAX_CONSTS_B - start_idx)
3760 count = WINED3D_MAX_CONSTS_B - start_idx;
3762 wined3d_mutex_lock();
3763 memcpy(constants, &device->stateblock_state->vs_consts_b[start_idx], count * sizeof(*constants));
3764 wined3d_mutex_unlock();
3766 return D3D_OK;
3769 static HRESULT WINAPI d3d9_device_SetStreamSource(IDirect3DDevice9Ex *iface,
3770 UINT stream_idx, IDirect3DVertexBuffer9 *buffer, UINT offset, UINT stride)
3772 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3773 struct d3d9_vertexbuffer *buffer_impl = unsafe_impl_from_IDirect3DVertexBuffer9(buffer);
3774 struct wined3d_buffer *wined3d_buffer;
3775 HRESULT hr;
3777 TRACE("iface %p, stream_idx %u, buffer %p, offset %u, stride %u.\n",
3778 iface, stream_idx, buffer, offset, stride);
3780 if (stream_idx >= ARRAY_SIZE(device->stateblock_state->streams))
3782 WARN("Stream index %u out of range.\n", stream_idx);
3783 return WINED3DERR_INVALIDCALL;
3786 wined3d_mutex_lock();
3787 if (!buffer_impl)
3789 const struct wined3d_stream_state *stream = &device->stateblock_state->streams[stream_idx];
3791 offset = stream->offset;
3792 stride = stream->stride;
3793 wined3d_buffer = NULL;
3795 else if (buffer_impl->draw_buffer)
3797 wined3d_buffer = buffer_impl->draw_buffer;
3799 else
3801 wined3d_buffer = buffer_impl->wined3d_buffer;
3804 hr = wined3d_stateblock_set_stream_source(device->update_state, stream_idx, wined3d_buffer, offset, stride);
3805 if (SUCCEEDED(hr) && !device->recording)
3807 if (buffer_impl && buffer_impl->draw_buffer)
3808 device->sysmem_vb |= (1u << stream_idx);
3809 else
3810 device->sysmem_vb &= ~(1u << stream_idx);
3813 wined3d_mutex_unlock();
3815 return hr;
3818 static HRESULT WINAPI d3d9_device_GetStreamSource(IDirect3DDevice9Ex *iface,
3819 UINT stream_idx, IDirect3DVertexBuffer9 **buffer, UINT *offset, UINT *stride)
3821 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3822 const struct wined3d_stateblock_state *state;
3823 const struct wined3d_stream_state *stream;
3824 struct d3d9_vertexbuffer *buffer_impl;
3826 TRACE("iface %p, stream_idx %u, buffer %p, offset %p, stride %p.\n",
3827 iface, stream_idx, buffer, offset, stride);
3829 if (!buffer)
3830 return D3DERR_INVALIDCALL;
3832 if (stream_idx >= ARRAY_SIZE(state->streams))
3834 WARN("Stream index %u out of range.\n", stream_idx);
3835 return WINED3DERR_INVALIDCALL;
3838 wined3d_mutex_lock();
3839 stream = &device->stateblock_state->streams[stream_idx];
3840 if (stream->buffer)
3842 buffer_impl = wined3d_buffer_get_parent(stream->buffer);
3843 *buffer = &buffer_impl->IDirect3DVertexBuffer9_iface;
3844 IDirect3DVertexBuffer9_AddRef(*buffer);
3846 else
3847 *buffer = NULL;
3848 if (offset)
3849 *offset = stream->offset;
3850 *stride = stream->stride;
3851 wined3d_mutex_unlock();
3853 return D3D_OK;
3856 static HRESULT WINAPI d3d9_device_SetStreamSourceFreq(IDirect3DDevice9Ex *iface, UINT stream_idx, UINT freq)
3858 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3859 HRESULT hr;
3861 TRACE("iface %p, stream_idx %u, freq %u.\n", iface, stream_idx, freq);
3863 wined3d_mutex_lock();
3864 hr = wined3d_stateblock_set_stream_source_freq(device->update_state, stream_idx, freq);
3865 wined3d_mutex_unlock();
3867 return hr;
3870 static HRESULT WINAPI d3d9_device_GetStreamSourceFreq(IDirect3DDevice9Ex *iface, UINT stream_idx, UINT *freq)
3872 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3873 const struct wined3d_stream_state *stream;
3875 TRACE("iface %p, stream_idx %u, freq %p.\n", iface, stream_idx, freq);
3877 wined3d_mutex_lock();
3878 stream = &device->stateblock_state->streams[stream_idx];
3879 *freq = stream->flags | stream->frequency;
3880 wined3d_mutex_unlock();
3882 return D3D_OK;
3885 static HRESULT WINAPI d3d9_device_SetIndices(IDirect3DDevice9Ex *iface, IDirect3DIndexBuffer9 *buffer)
3887 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3888 struct d3d9_indexbuffer *ib = unsafe_impl_from_IDirect3DIndexBuffer9(buffer);
3889 struct wined3d_buffer *wined3d_buffer;
3891 TRACE("iface %p, buffer %p.\n", iface, buffer);
3893 if (!ib)
3894 wined3d_buffer = NULL;
3895 else if (ib->draw_buffer)
3896 wined3d_buffer = ib->draw_buffer;
3897 else
3898 wined3d_buffer = ib->wined3d_buffer;
3900 wined3d_mutex_lock();
3901 wined3d_stateblock_set_index_buffer(device->update_state, wined3d_buffer, ib ? ib->format : WINED3DFMT_UNKNOWN);
3902 if (!device->recording)
3903 device->sysmem_ib = ib && ib->draw_buffer;
3904 wined3d_mutex_unlock();
3906 return D3D_OK;
3909 static HRESULT WINAPI d3d9_device_GetIndices(IDirect3DDevice9Ex *iface, IDirect3DIndexBuffer9 **buffer)
3911 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3912 struct wined3d_buffer *wined3d_buffer;
3913 struct d3d9_indexbuffer *buffer_impl;
3915 TRACE("iface %p, buffer %p.\n", iface, buffer);
3917 if (!buffer)
3918 return D3DERR_INVALIDCALL;
3920 wined3d_mutex_lock();
3921 if ((wined3d_buffer = device->stateblock_state->index_buffer))
3923 buffer_impl = wined3d_buffer_get_parent(wined3d_buffer);
3924 *buffer = &buffer_impl->IDirect3DIndexBuffer9_iface;
3925 IDirect3DIndexBuffer9_AddRef(*buffer);
3927 else
3929 *buffer = NULL;
3931 wined3d_mutex_unlock();
3933 return D3D_OK;
3936 static HRESULT WINAPI d3d9_device_CreatePixelShader(IDirect3DDevice9Ex *iface,
3937 const DWORD *byte_code, IDirect3DPixelShader9 **shader)
3939 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3940 struct d3d9_pixelshader *object;
3941 HRESULT hr;
3943 TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
3945 if (!(object = heap_alloc_zero(sizeof(*object))))
3947 FIXME("Failed to allocate pixel shader memory.\n");
3948 return E_OUTOFMEMORY;
3951 hr = pixelshader_init(object, device, byte_code);
3952 if (FAILED(hr))
3954 WARN("Failed to initialize pixel shader, hr %#x.\n", hr);
3955 heap_free(object);
3956 return hr;
3959 TRACE("Created pixel shader %p.\n", object);
3960 *shader = &object->IDirect3DPixelShader9_iface;
3962 return D3D_OK;
3965 static HRESULT WINAPI d3d9_device_SetPixelShader(IDirect3DDevice9Ex *iface, IDirect3DPixelShader9 *shader)
3967 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3968 struct d3d9_pixelshader *shader_obj = unsafe_impl_from_IDirect3DPixelShader9(shader);
3970 TRACE("iface %p, shader %p.\n", iface, shader);
3972 wined3d_mutex_lock();
3973 wined3d_stateblock_set_pixel_shader(device->update_state,
3974 shader_obj ? shader_obj->wined3d_shader : NULL);
3975 wined3d_mutex_unlock();
3977 return D3D_OK;
3980 static HRESULT WINAPI d3d9_device_GetPixelShader(IDirect3DDevice9Ex *iface, IDirect3DPixelShader9 **shader)
3982 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3983 struct d3d9_pixelshader *shader_impl;
3984 struct wined3d_shader *wined3d_shader;
3986 TRACE("iface %p, shader %p.\n", iface, shader);
3988 if (!shader) return D3DERR_INVALIDCALL;
3990 wined3d_mutex_lock();
3991 if ((wined3d_shader = device->stateblock_state->ps))
3993 shader_impl = wined3d_shader_get_parent(wined3d_shader);
3994 *shader = &shader_impl->IDirect3DPixelShader9_iface;
3995 IDirect3DPixelShader9_AddRef(*shader);
3997 else
3999 *shader = NULL;
4001 wined3d_mutex_unlock();
4003 TRACE("Returning %p.\n", *shader);
4005 return D3D_OK;
4008 static HRESULT WINAPI d3d9_device_SetPixelShaderConstantF(IDirect3DDevice9Ex *iface,
4009 UINT reg_idx, const float *data, UINT count)
4011 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
4012 HRESULT hr;
4014 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
4016 wined3d_mutex_lock();
4017 hr = wined3d_stateblock_set_ps_consts_f(device->update_state,
4018 reg_idx, count, (const struct wined3d_vec4 *)data);
4019 wined3d_mutex_unlock();
4021 return hr;
4024 static HRESULT WINAPI d3d9_device_GetPixelShaderConstantF(IDirect3DDevice9Ex *iface,
4025 UINT start_idx, float *constants, UINT count)
4027 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
4028 const struct wined3d_vec4 *src;
4030 TRACE("iface %p, start_idx %u, constants %p, count %u.\n", iface, start_idx, constants, count);
4032 if (!constants || !wined3d_bound_range(start_idx, count, WINED3D_MAX_PS_CONSTS_F))
4033 return WINED3DERR_INVALIDCALL;
4035 wined3d_mutex_lock();
4036 src = device->stateblock_state->ps_consts_f;
4037 memcpy(constants, &src[start_idx], count * sizeof(*src));
4038 wined3d_mutex_unlock();
4040 return D3D_OK;
4043 static HRESULT WINAPI d3d9_device_SetPixelShaderConstantI(IDirect3DDevice9Ex *iface,
4044 UINT reg_idx, const int *data, UINT count)
4046 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
4047 HRESULT hr;
4049 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
4051 wined3d_mutex_lock();
4052 hr = wined3d_stateblock_set_ps_consts_i(device->update_state,
4053 reg_idx, count, (const struct wined3d_ivec4 *)data);
4054 wined3d_mutex_unlock();
4056 return hr;
4059 static HRESULT WINAPI d3d9_device_GetPixelShaderConstantI(IDirect3DDevice9Ex *iface,
4060 UINT start_idx, int *constants, UINT count)
4062 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
4063 const struct wined3d_ivec4 *src;
4065 TRACE("iface %p, start_idx %u, constants %p, count %u.\n", iface, start_idx, constants, count);
4067 if (!constants || start_idx >= WINED3D_MAX_CONSTS_I)
4068 return WINED3DERR_INVALIDCALL;
4069 if (count > WINED3D_MAX_CONSTS_I - start_idx)
4070 count = WINED3D_MAX_CONSTS_I - start_idx;
4072 wined3d_mutex_lock();
4073 src = device->stateblock_state->ps_consts_i;
4074 memcpy(constants, &src[start_idx], count * sizeof(*src));
4075 wined3d_mutex_unlock();
4077 return D3D_OK;
4080 static HRESULT WINAPI d3d9_device_SetPixelShaderConstantB(IDirect3DDevice9Ex *iface,
4081 UINT reg_idx, const BOOL *data, UINT count)
4083 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
4084 HRESULT hr;
4086 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
4088 wined3d_mutex_lock();
4089 hr = wined3d_stateblock_set_ps_consts_b(device->update_state, reg_idx, count, data);
4090 wined3d_mutex_unlock();
4092 return hr;
4095 static HRESULT WINAPI d3d9_device_GetPixelShaderConstantB(IDirect3DDevice9Ex *iface,
4096 UINT start_idx, BOOL *constants, UINT count)
4098 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
4100 TRACE("iface %p, start_idx %u, constants %p, count %u.\n", iface, start_idx, constants, count);
4102 if (!constants || start_idx >= WINED3D_MAX_CONSTS_I)
4103 return WINED3DERR_INVALIDCALL;
4104 if (count > WINED3D_MAX_CONSTS_I - start_idx)
4105 count = WINED3D_MAX_CONSTS_I - start_idx;
4107 wined3d_mutex_lock();
4108 memcpy(constants, &device->stateblock_state->ps_consts_b[start_idx], count * sizeof(*constants));
4109 wined3d_mutex_unlock();
4111 return D3D_OK;
4114 static HRESULT WINAPI d3d9_device_DrawRectPatch(IDirect3DDevice9Ex *iface, UINT handle,
4115 const float *segment_count, const D3DRECTPATCH_INFO *patch_info)
4117 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
4118 iface, handle, segment_count, patch_info);
4119 return D3D_OK;
4122 static HRESULT WINAPI d3d9_device_DrawTriPatch(IDirect3DDevice9Ex *iface, UINT handle,
4123 const float *segment_count, const D3DTRIPATCH_INFO *patch_info)
4125 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
4126 iface, handle, segment_count, patch_info);
4127 return D3D_OK;
4130 static HRESULT WINAPI d3d9_device_DeletePatch(IDirect3DDevice9Ex *iface, UINT handle)
4132 FIXME("iface %p, handle %#x unimplemented.\n", iface, handle);
4133 return D3DERR_INVALIDCALL;
4136 static HRESULT WINAPI d3d9_device_CreateQuery(IDirect3DDevice9Ex *iface, D3DQUERYTYPE type, IDirect3DQuery9 **query)
4138 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
4139 struct d3d9_query *object;
4140 HRESULT hr;
4142 TRACE("iface %p, type %#x, query %p.\n", iface, type, query);
4144 if (!(object = heap_alloc_zero(sizeof(*object))))
4145 return E_OUTOFMEMORY;
4147 hr = query_init(object, device, type);
4148 if (FAILED(hr))
4150 WARN("Failed to initialize query, hr %#x.\n", hr);
4151 heap_free(object);
4152 return hr;
4155 TRACE("Created query %p.\n", object);
4156 if (query) *query = &object->IDirect3DQuery9_iface;
4157 else IDirect3DQuery9_Release(&object->IDirect3DQuery9_iface);
4159 return D3D_OK;
4162 static HRESULT WINAPI d3d9_device_SetConvolutionMonoKernel(IDirect3DDevice9Ex *iface,
4163 UINT width, UINT height, float *rows, float *columns)
4165 FIXME("iface %p, width %u, height %u, rows %p, columns %p stub!\n",
4166 iface, width, height, rows, columns);
4168 return E_NOTIMPL;
4171 static HRESULT WINAPI d3d9_device_ComposeRects(IDirect3DDevice9Ex *iface,
4172 IDirect3DSurface9 *src_surface, IDirect3DSurface9 *dst_surface, IDirect3DVertexBuffer9 *src_descs,
4173 UINT rect_count, IDirect3DVertexBuffer9 *dst_descs, D3DCOMPOSERECTSOP operation, INT offset_x, INT offset_y)
4175 FIXME("iface %p, src_surface %p, dst_surface %p, src_descs %p, rect_count %u, "
4176 "dst_descs %p, operation %#x, offset_x %u, offset_y %u stub!\n",
4177 iface, src_surface, dst_surface, src_descs, rect_count,
4178 dst_descs, operation, offset_x, offset_y);
4180 return E_NOTIMPL;
4183 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_PresentEx(IDirect3DDevice9Ex *iface,
4184 const RECT *src_rect, const RECT *dst_rect, HWND dst_window_override,
4185 const RGNDATA *dirty_region, DWORD flags)
4187 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
4188 struct d3d9_swapchain *swapchain;
4189 unsigned int i;
4190 HRESULT hr;
4192 TRACE("iface %p, src_rect %s, dst_rect %s, dst_window_override %p, dirty_region %p, flags %#x.\n",
4193 iface, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect),
4194 dst_window_override, dirty_region, flags);
4196 if (device->device_state != D3D9_DEVICE_STATE_OK)
4197 return S_PRESENT_OCCLUDED;
4199 if (dirty_region)
4200 FIXME("Ignoring dirty_region %p.\n", dirty_region);
4202 wined3d_mutex_lock();
4203 for (i = 0; i < device->implicit_swapchain_count; ++i)
4205 swapchain = wined3d_swapchain_get_parent(device->implicit_swapchains[i]);
4206 if (FAILED(hr = wined3d_swapchain_present(swapchain->wined3d_swapchain,
4207 src_rect, dst_rect, dst_window_override, swapchain->swap_interval, flags)))
4209 wined3d_mutex_unlock();
4210 return hr;
4213 wined3d_mutex_unlock();
4215 return D3D_OK;
4218 static HRESULT WINAPI d3d9_device_GetGPUThreadPriority(IDirect3DDevice9Ex *iface, INT *priority)
4220 FIXME("iface %p, priority %p stub!\n", iface, priority);
4222 return E_NOTIMPL;
4225 static HRESULT WINAPI d3d9_device_SetGPUThreadPriority(IDirect3DDevice9Ex *iface, INT priority)
4227 FIXME("iface %p, priority %d stub!\n", iface, priority);
4229 return E_NOTIMPL;
4232 static HRESULT WINAPI d3d9_device_WaitForVBlank(IDirect3DDevice9Ex *iface, UINT swapchain_idx)
4234 FIXME("iface %p, swapchain_idx %u stub!\n", iface, swapchain_idx);
4236 return E_NOTIMPL;
4239 static HRESULT WINAPI d3d9_device_CheckResourceResidency(IDirect3DDevice9Ex *iface,
4240 IDirect3DResource9 **resources, UINT32 resource_count)
4242 FIXME("iface %p, resources %p, resource_count %u stub!\n",
4243 iface, resources, resource_count);
4245 return E_NOTIMPL;
4248 static HRESULT WINAPI d3d9_device_SetMaximumFrameLatency(IDirect3DDevice9Ex *iface, UINT max_latency)
4250 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
4252 TRACE("iface %p, max_latency %u.\n", iface, max_latency);
4254 if (max_latency > 30)
4255 return D3DERR_INVALIDCALL;
4257 wined3d_mutex_lock();
4258 wined3d_device_set_max_frame_latency(device->wined3d_device, max_latency);
4259 wined3d_mutex_unlock();
4261 return S_OK;
4264 static HRESULT WINAPI d3d9_device_GetMaximumFrameLatency(IDirect3DDevice9Ex *iface, UINT *max_latency)
4266 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
4268 TRACE("iface %p, max_latency %p.\n", iface, max_latency);
4270 wined3d_mutex_lock();
4271 *max_latency = wined3d_device_get_max_frame_latency(device->wined3d_device);
4272 wined3d_mutex_unlock();
4274 return S_OK;
4277 static HRESULT WINAPI d3d9_device_CheckDeviceState(IDirect3DDevice9Ex *iface, HWND dst_window)
4279 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
4280 struct wined3d_swapchain_desc swapchain_desc;
4282 TRACE("iface %p, dst_window %p.\n", iface, dst_window);
4284 wined3d_mutex_lock();
4285 wined3d_swapchain_get_desc(device->implicit_swapchains[0], &swapchain_desc);
4286 wined3d_mutex_unlock();
4288 if (swapchain_desc.windowed)
4289 return D3D_OK;
4291 /* FIXME: This is actually supposed to check if any other device is in
4292 * fullscreen mode. */
4293 if (dst_window != swapchain_desc.device_window)
4294 return device->device_state == D3D9_DEVICE_STATE_OK ? S_PRESENT_OCCLUDED : D3D_OK;
4296 return device->device_state == D3D9_DEVICE_STATE_OK ? D3D_OK : S_PRESENT_OCCLUDED;
4299 static HRESULT WINAPI d3d9_device_CreateRenderTargetEx(IDirect3DDevice9Ex *iface,
4300 UINT width, UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
4301 BOOL lockable, IDirect3DSurface9 **surface, HANDLE *shared_handle, DWORD usage)
4303 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
4304 unsigned int access = WINED3D_RESOURCE_ACCESS_GPU;
4306 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u, "
4307 "lockable %#x, surface %p, shared_handle %p, usage %#x.\n",
4308 iface, width, height, format, multisample_type, multisample_quality,
4309 lockable, surface, shared_handle, usage);
4311 *surface = NULL;
4313 if (usage & (D3DUSAGE_DEPTHSTENCIL | D3DUSAGE_RENDERTARGET))
4315 WARN("Invalid usage %#x.\n", usage);
4316 return D3DERR_INVALIDCALL;
4319 if (shared_handle)
4320 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
4322 if (lockable)
4323 access |= WINED3D_RESOURCE_ACCESS_MAP_R | WINED3D_RESOURCE_ACCESS_MAP_W;
4325 return d3d9_device_create_surface(device, 0, wined3dformat_from_d3dformat(format),
4326 wined3d_multisample_type_from_d3d(multisample_type), multisample_quality,
4327 usage & WINED3DUSAGE_MASK, WINED3D_BIND_RENDER_TARGET, access, width, height, NULL, surface);
4330 static HRESULT WINAPI d3d9_device_CreateOffscreenPlainSurfaceEx(IDirect3DDevice9Ex *iface,
4331 UINT width, UINT height, D3DFORMAT format, D3DPOOL pool, IDirect3DSurface9 **surface,
4332 HANDLE *shared_handle, DWORD usage)
4334 FIXME("iface %p, width %u, height %u, format %#x, pool %#x, surface %p, shared_handle %p, usage %#x stub!\n",
4335 iface, width, height, format, pool, surface, shared_handle, usage);
4337 return E_NOTIMPL;
4340 static HRESULT WINAPI d3d9_device_CreateDepthStencilSurfaceEx(IDirect3DDevice9Ex *iface,
4341 UINT width, UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
4342 BOOL discard, IDirect3DSurface9 **surface, HANDLE *shared_handle, DWORD usage)
4344 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
4345 DWORD flags = 0;
4347 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u, "
4348 "discard %#x, surface %p, shared_handle %p, usage %#x.\n",
4349 iface, width, height, format, multisample_type, multisample_quality,
4350 discard, surface, shared_handle, usage);
4352 if (usage & (D3DUSAGE_DEPTHSTENCIL | D3DUSAGE_RENDERTARGET))
4354 WARN("Invalid usage %#x.\n", usage);
4355 return D3DERR_INVALIDCALL;
4358 if (shared_handle)
4359 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
4361 if (discard)
4362 flags |= WINED3D_TEXTURE_CREATE_DISCARD;
4364 *surface = NULL;
4365 return d3d9_device_create_surface(device, flags, wined3dformat_from_d3dformat(format),
4366 wined3d_multisample_type_from_d3d(multisample_type), multisample_quality, usage & WINED3DUSAGE_MASK,
4367 WINED3D_BIND_DEPTH_STENCIL, WINED3D_RESOURCE_ACCESS_GPU, width, height, NULL, surface);
4370 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_ResetEx(IDirect3DDevice9Ex *iface,
4371 D3DPRESENT_PARAMETERS *present_parameters, D3DDISPLAYMODEEX *mode)
4373 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
4375 TRACE("iface %p, present_parameters %p, mode %p.\n", iface, present_parameters, mode);
4377 if (!present_parameters->Windowed == !mode)
4379 WARN("Mode can be passed if and only if Windowed is FALSE.\n");
4380 return D3DERR_INVALIDCALL;
4383 if (mode && (mode->Width != present_parameters->BackBufferWidth
4384 || mode->Height != present_parameters->BackBufferHeight))
4386 WARN("Mode and back buffer mismatch (mode %ux%u, backbuffer %ux%u).\n",
4387 mode->Width, mode->Height,
4388 present_parameters->BackBufferWidth, present_parameters->BackBufferHeight);
4389 return D3DERR_INVALIDCALL;
4392 return d3d9_device_reset(device, present_parameters, mode);
4395 static HRESULT WINAPI d3d9_device_GetDisplayModeEx(IDirect3DDevice9Ex *iface,
4396 UINT swapchain_idx, D3DDISPLAYMODEEX *mode, D3DDISPLAYROTATION *rotation)
4398 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
4399 struct wined3d_display_mode wined3d_mode;
4400 HRESULT hr;
4402 TRACE("iface %p, swapchain_idx %u, mode %p, rotation %p.\n",
4403 iface, swapchain_idx, mode, rotation);
4405 if (mode->Size != sizeof(*mode))
4406 return D3DERR_INVALIDCALL;
4408 wined3d_mutex_lock();
4409 hr = wined3d_device_get_display_mode(device->wined3d_device, swapchain_idx, &wined3d_mode,
4410 (enum wined3d_display_rotation *)rotation);
4411 wined3d_mutex_unlock();
4413 if (SUCCEEDED(hr))
4415 mode->Width = wined3d_mode.width;
4416 mode->Height = wined3d_mode.height;
4417 mode->RefreshRate = wined3d_mode.refresh_rate;
4418 mode->Format = d3dformat_from_wined3dformat(wined3d_mode.format_id);
4419 mode->ScanLineOrdering = d3dscanlineordering_from_wined3d(wined3d_mode.scanline_ordering);
4422 return hr;
4425 static const struct IDirect3DDevice9ExVtbl d3d9_device_vtbl =
4427 /* IUnknown */
4428 d3d9_device_QueryInterface,
4429 d3d9_device_AddRef,
4430 d3d9_device_Release,
4431 /* IDirect3DDevice9 */
4432 d3d9_device_TestCooperativeLevel,
4433 d3d9_device_GetAvailableTextureMem,
4434 d3d9_device_EvictManagedResources,
4435 d3d9_device_GetDirect3D,
4436 d3d9_device_GetDeviceCaps,
4437 d3d9_device_GetDisplayMode,
4438 d3d9_device_GetCreationParameters,
4439 d3d9_device_SetCursorProperties,
4440 d3d9_device_SetCursorPosition,
4441 d3d9_device_ShowCursor,
4442 d3d9_device_CreateAdditionalSwapChain,
4443 d3d9_device_GetSwapChain,
4444 d3d9_device_GetNumberOfSwapChains,
4445 d3d9_device_Reset,
4446 d3d9_device_Present,
4447 d3d9_device_GetBackBuffer,
4448 d3d9_device_GetRasterStatus,
4449 d3d9_device_SetDialogBoxMode,
4450 d3d9_device_SetGammaRamp,
4451 d3d9_device_GetGammaRamp,
4452 d3d9_device_CreateTexture,
4453 d3d9_device_CreateVolumeTexture,
4454 d3d9_device_CreateCubeTexture,
4455 d3d9_device_CreateVertexBuffer,
4456 d3d9_device_CreateIndexBuffer,
4457 d3d9_device_CreateRenderTarget,
4458 d3d9_device_CreateDepthStencilSurface,
4459 d3d9_device_UpdateSurface,
4460 d3d9_device_UpdateTexture,
4461 d3d9_device_GetRenderTargetData,
4462 d3d9_device_GetFrontBufferData,
4463 d3d9_device_StretchRect,
4464 d3d9_device_ColorFill,
4465 d3d9_device_CreateOffscreenPlainSurface,
4466 d3d9_device_SetRenderTarget,
4467 d3d9_device_GetRenderTarget,
4468 d3d9_device_SetDepthStencilSurface,
4469 d3d9_device_GetDepthStencilSurface,
4470 d3d9_device_BeginScene,
4471 d3d9_device_EndScene,
4472 d3d9_device_Clear,
4473 d3d9_device_SetTransform,
4474 d3d9_device_GetTransform,
4475 d3d9_device_MultiplyTransform,
4476 d3d9_device_SetViewport,
4477 d3d9_device_GetViewport,
4478 d3d9_device_SetMaterial,
4479 d3d9_device_GetMaterial,
4480 d3d9_device_SetLight,
4481 d3d9_device_GetLight,
4482 d3d9_device_LightEnable,
4483 d3d9_device_GetLightEnable,
4484 d3d9_device_SetClipPlane,
4485 d3d9_device_GetClipPlane,
4486 d3d9_device_SetRenderState,
4487 d3d9_device_GetRenderState,
4488 d3d9_device_CreateStateBlock,
4489 d3d9_device_BeginStateBlock,
4490 d3d9_device_EndStateBlock,
4491 d3d9_device_SetClipStatus,
4492 d3d9_device_GetClipStatus,
4493 d3d9_device_GetTexture,
4494 d3d9_device_SetTexture,
4495 d3d9_device_GetTextureStageState,
4496 d3d9_device_SetTextureStageState,
4497 d3d9_device_GetSamplerState,
4498 d3d9_device_SetSamplerState,
4499 d3d9_device_ValidateDevice,
4500 d3d9_device_SetPaletteEntries,
4501 d3d9_device_GetPaletteEntries,
4502 d3d9_device_SetCurrentTexturePalette,
4503 d3d9_device_GetCurrentTexturePalette,
4504 d3d9_device_SetScissorRect,
4505 d3d9_device_GetScissorRect,
4506 d3d9_device_SetSoftwareVertexProcessing,
4507 d3d9_device_GetSoftwareVertexProcessing,
4508 d3d9_device_SetNPatchMode,
4509 d3d9_device_GetNPatchMode,
4510 d3d9_device_DrawPrimitive,
4511 d3d9_device_DrawIndexedPrimitive,
4512 d3d9_device_DrawPrimitiveUP,
4513 d3d9_device_DrawIndexedPrimitiveUP,
4514 d3d9_device_ProcessVertices,
4515 d3d9_device_CreateVertexDeclaration,
4516 d3d9_device_SetVertexDeclaration,
4517 d3d9_device_GetVertexDeclaration,
4518 d3d9_device_SetFVF,
4519 d3d9_device_GetFVF,
4520 d3d9_device_CreateVertexShader,
4521 d3d9_device_SetVertexShader,
4522 d3d9_device_GetVertexShader,
4523 d3d9_device_SetVertexShaderConstantF,
4524 d3d9_device_GetVertexShaderConstantF,
4525 d3d9_device_SetVertexShaderConstantI,
4526 d3d9_device_GetVertexShaderConstantI,
4527 d3d9_device_SetVertexShaderConstantB,
4528 d3d9_device_GetVertexShaderConstantB,
4529 d3d9_device_SetStreamSource,
4530 d3d9_device_GetStreamSource,
4531 d3d9_device_SetStreamSourceFreq,
4532 d3d9_device_GetStreamSourceFreq,
4533 d3d9_device_SetIndices,
4534 d3d9_device_GetIndices,
4535 d3d9_device_CreatePixelShader,
4536 d3d9_device_SetPixelShader,
4537 d3d9_device_GetPixelShader,
4538 d3d9_device_SetPixelShaderConstantF,
4539 d3d9_device_GetPixelShaderConstantF,
4540 d3d9_device_SetPixelShaderConstantI,
4541 d3d9_device_GetPixelShaderConstantI,
4542 d3d9_device_SetPixelShaderConstantB,
4543 d3d9_device_GetPixelShaderConstantB,
4544 d3d9_device_DrawRectPatch,
4545 d3d9_device_DrawTriPatch,
4546 d3d9_device_DeletePatch,
4547 d3d9_device_CreateQuery,
4548 /* IDirect3DDevice9Ex */
4549 d3d9_device_SetConvolutionMonoKernel,
4550 d3d9_device_ComposeRects,
4551 d3d9_device_PresentEx,
4552 d3d9_device_GetGPUThreadPriority,
4553 d3d9_device_SetGPUThreadPriority,
4554 d3d9_device_WaitForVBlank,
4555 d3d9_device_CheckResourceResidency,
4556 d3d9_device_SetMaximumFrameLatency,
4557 d3d9_device_GetMaximumFrameLatency,
4558 d3d9_device_CheckDeviceState,
4559 d3d9_device_CreateRenderTargetEx,
4560 d3d9_device_CreateOffscreenPlainSurfaceEx,
4561 d3d9_device_CreateDepthStencilSurfaceEx,
4562 d3d9_device_ResetEx,
4563 d3d9_device_GetDisplayModeEx,
4566 static inline struct d3d9_device *device_from_device_parent(struct wined3d_device_parent *device_parent)
4568 return CONTAINING_RECORD(device_parent, struct d3d9_device, device_parent);
4571 static void CDECL device_parent_wined3d_device_created(struct wined3d_device_parent *device_parent,
4572 struct wined3d_device *device)
4574 TRACE("device_parent %p, device %p.\n", device_parent, device);
4577 static void CDECL device_parent_mode_changed(struct wined3d_device_parent *device_parent)
4579 TRACE("device_parent %p.\n", device_parent);
4582 static void CDECL device_parent_activate(struct wined3d_device_parent *device_parent, BOOL activate)
4584 struct d3d9_device *device = device_from_device_parent(device_parent);
4586 TRACE("device_parent %p, activate %#x.\n", device_parent, activate);
4588 if (!device->d3d_parent)
4589 return;
4591 if (!activate)
4592 InterlockedCompareExchange(&device->device_state, D3D9_DEVICE_STATE_LOST, D3D9_DEVICE_STATE_OK);
4593 else if (device->d3d_parent->extended)
4594 InterlockedCompareExchange(&device->device_state, D3D9_DEVICE_STATE_OK, D3D9_DEVICE_STATE_LOST);
4595 else
4596 InterlockedCompareExchange(&device->device_state, D3D9_DEVICE_STATE_NOT_RESET, D3D9_DEVICE_STATE_LOST);
4599 static HRESULT CDECL device_parent_texture_sub_resource_created(struct wined3d_device_parent *device_parent,
4600 enum wined3d_resource_type type, struct wined3d_texture *wined3d_texture, unsigned int sub_resource_idx,
4601 void **parent, const struct wined3d_parent_ops **parent_ops)
4603 TRACE("device_parent %p, type %#x, wined3d_texture %p, sub_resource_idx %u, parent %p, parent_ops %p.\n",
4604 device_parent, type, wined3d_texture, sub_resource_idx, parent, parent_ops);
4606 if (type == WINED3D_RTYPE_TEXTURE_2D)
4608 struct d3d9_surface *d3d_surface;
4610 if (!(d3d_surface = heap_alloc_zero(sizeof(*d3d_surface))))
4611 return E_OUTOFMEMORY;
4613 surface_init(d3d_surface, wined3d_texture, sub_resource_idx, parent_ops);
4614 *parent = d3d_surface;
4615 TRACE("Created surface %p.\n", d3d_surface);
4617 else if (type == WINED3D_RTYPE_TEXTURE_3D)
4619 struct d3d9_volume *d3d_volume;
4621 if (!(d3d_volume = heap_alloc_zero(sizeof(*d3d_volume))))
4622 return E_OUTOFMEMORY;
4624 volume_init(d3d_volume, wined3d_texture, sub_resource_idx, parent_ops);
4625 *parent = d3d_volume;
4626 TRACE("Created volume %p.\n", d3d_volume);
4628 else
4630 ERR("Unhandled resource type %#x.\n", type);
4631 return E_FAIL;
4634 return D3D_OK;
4637 static HRESULT CDECL device_parent_create_swapchain_texture(struct wined3d_device_parent *device_parent,
4638 void *container_parent, const struct wined3d_resource_desc *desc, DWORD texture_flags,
4639 struct wined3d_texture **texture)
4641 struct d3d9_device *device = device_from_device_parent(device_parent);
4642 struct d3d9_surface *d3d_surface;
4643 HRESULT hr;
4645 TRACE("device_parent %p, container_parent %p, desc %p, texture flags %#x, texture %p.\n",
4646 device_parent, container_parent, desc, texture_flags, texture);
4648 if (container_parent == device_parent)
4649 container_parent = &device->IDirect3DDevice9Ex_iface;
4651 if (FAILED(hr = wined3d_texture_create(device->wined3d_device, desc, 1, 1,
4652 texture_flags, NULL, container_parent, &d3d9_null_wined3d_parent_ops, texture)))
4654 WARN("Failed to create texture, hr %#x.\n", hr);
4655 return hr;
4658 d3d_surface = wined3d_texture_get_sub_resource_parent(*texture, 0);
4659 d3d_surface->parent_device = &device->IDirect3DDevice9Ex_iface;
4661 return hr;
4664 static const struct wined3d_device_parent_ops d3d9_wined3d_device_parent_ops =
4666 device_parent_wined3d_device_created,
4667 device_parent_mode_changed,
4668 device_parent_activate,
4669 device_parent_texture_sub_resource_created,
4670 device_parent_create_swapchain_texture,
4673 static void setup_fpu(void)
4675 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
4676 WORD cw;
4677 __asm__ volatile ("fnstcw %0" : "=m" (cw));
4678 cw = (cw & ~0xf3f) | 0x3f;
4679 __asm__ volatile ("fldcw %0" : : "m" (cw));
4680 #elif defined(__i386__) && defined(_MSC_VER)
4681 WORD cw;
4682 __asm fnstcw cw;
4683 cw = (cw & ~0xf3f) | 0x3f;
4684 __asm fldcw cw;
4685 #else
4686 FIXME("FPU setup not implemented for this platform.\n");
4687 #endif
4690 HRESULT device_init(struct d3d9_device *device, struct d3d9 *parent, struct wined3d *wined3d,
4691 UINT adapter, D3DDEVTYPE device_type, HWND focus_window, DWORD flags,
4692 D3DPRESENT_PARAMETERS *parameters, D3DDISPLAYMODEEX *mode)
4694 struct wined3d_swapchain_desc *swapchain_desc;
4695 struct wined3d_adapter *wined3d_adapter;
4696 struct d3d9_swapchain *d3d_swapchain;
4697 struct wined3d_caps wined3d_caps;
4698 unsigned int output_idx;
4699 unsigned i, count = 1;
4700 D3DCAPS9 caps;
4701 HRESULT hr;
4703 static const enum wined3d_feature_level feature_levels[] =
4705 WINED3D_FEATURE_LEVEL_9_3,
4706 WINED3D_FEATURE_LEVEL_9_2,
4707 WINED3D_FEATURE_LEVEL_9_1,
4708 WINED3D_FEATURE_LEVEL_8,
4709 WINED3D_FEATURE_LEVEL_7,
4710 WINED3D_FEATURE_LEVEL_6,
4711 WINED3D_FEATURE_LEVEL_5,
4714 output_idx = adapter;
4715 if (output_idx >= parent->wined3d_output_count)
4716 return D3DERR_INVALIDCALL;
4718 if (mode)
4719 FIXME("Ignoring display mode.\n");
4721 device->IDirect3DDevice9Ex_iface.lpVtbl = &d3d9_device_vtbl;
4722 device->device_parent.ops = &d3d9_wined3d_device_parent_ops;
4723 device->adapter_ordinal = adapter;
4724 device->refcount = 1;
4726 if (!(flags & D3DCREATE_FPU_PRESERVE)) setup_fpu();
4728 wined3d_mutex_lock();
4729 wined3d_adapter = wined3d_output_get_adapter(parent->wined3d_outputs[output_idx]);
4730 if (FAILED(hr = wined3d_device_create(wined3d, wined3d_adapter, wined3d_device_type_from_d3d(device_type),
4731 focus_window, flags, 4, feature_levels, ARRAY_SIZE(feature_levels),
4732 &device->device_parent, &device->wined3d_device)))
4734 WARN("Failed to create wined3d device, hr %#x.\n", hr);
4735 wined3d_mutex_unlock();
4736 return hr;
4739 device->immediate_context = wined3d_device_get_immediate_context(device->wined3d_device);
4740 wined3d_get_device_caps(wined3d_adapter, wined3d_device_type_from_d3d(device_type), &wined3d_caps);
4741 d3d9_caps_from_wined3dcaps(parent, adapter, &caps, &wined3d_caps);
4742 device->max_user_clip_planes = caps.MaxUserClipPlanes;
4743 device->vs_uniform_count = caps.MaxVertexShaderConst;
4744 if (flags & D3DCREATE_ADAPTERGROUP_DEVICE)
4745 count = caps.NumberOfAdaptersInGroup;
4747 if (FAILED(hr = wined3d_stateblock_create(device->wined3d_device, NULL, WINED3D_SBT_PRIMARY, &device->state)))
4749 ERR("Failed to create the primary stateblock, hr %#x.\n", hr);
4750 wined3d_device_decref(device->wined3d_device);
4751 wined3d_mutex_unlock();
4752 return hr;
4754 device->stateblock_state = wined3d_stateblock_get_state(device->state);
4755 device->update_state = device->state;
4757 if (flags & D3DCREATE_MULTITHREADED)
4758 wined3d_device_set_multithreaded(device->wined3d_device);
4760 if (!parameters->Windowed)
4762 if (!focus_window)
4763 focus_window = parameters->hDeviceWindow;
4764 if (FAILED(hr = wined3d_device_acquire_focus_window(device->wined3d_device, focus_window)))
4766 ERR("Failed to acquire focus window, hr %#x.\n", hr);
4767 wined3d_device_decref(device->wined3d_device);
4768 wined3d_mutex_unlock();
4769 return hr;
4773 if (!(swapchain_desc = heap_alloc(sizeof(*swapchain_desc) * count)))
4775 ERR("Failed to allocate wined3d parameters.\n");
4776 wined3d_device_release_focus_window(device->wined3d_device);
4777 wined3d_device_decref(device->wined3d_device);
4778 wined3d_mutex_unlock();
4779 return E_OUTOFMEMORY;
4782 for (i = 0; i < count; ++i)
4784 if (!wined3d_swapchain_desc_from_d3d9(&swapchain_desc[i],
4785 parent->wined3d_outputs[output_idx + i], &parameters[i], parent->extended))
4787 wined3d_device_release_focus_window(device->wined3d_device);
4788 wined3d_device_decref(device->wined3d_device);
4789 heap_free(swapchain_desc);
4790 wined3d_mutex_unlock();
4791 return D3DERR_INVALIDCALL;
4793 swapchain_desc[i].flags |= WINED3D_SWAPCHAIN_IMPLICIT;
4794 if (flags & D3DCREATE_NOWINDOWCHANGES)
4795 swapchain_desc[i].flags |= WINED3D_SWAPCHAIN_NO_WINDOW_CHANGES;
4798 if (FAILED(hr = d3d9_swapchain_create(device, swapchain_desc,
4799 wined3dswapinterval_from_d3d(parameters->PresentationInterval), &d3d_swapchain)))
4801 WARN("Failed to create swapchain, hr %#x.\n", hr);
4802 wined3d_device_release_focus_window(device->wined3d_device);
4803 heap_free(swapchain_desc);
4804 wined3d_device_decref(device->wined3d_device);
4805 wined3d_mutex_unlock();
4806 return hr;
4809 wined3d_swapchain_incref(d3d_swapchain->wined3d_swapchain);
4810 IDirect3DSwapChain9Ex_Release(&d3d_swapchain->IDirect3DSwapChain9Ex_iface);
4812 wined3d_stateblock_set_render_state(device->state, WINED3D_RS_ZENABLE,
4813 !!swapchain_desc->enable_auto_depth_stencil);
4814 device_reset_viewport_state(device);
4816 if (FAILED(hr = d3d9_device_get_swapchains(device)))
4818 wined3d_swapchain_decref(d3d_swapchain->wined3d_swapchain);
4819 wined3d_device_release_focus_window(device->wined3d_device);
4820 wined3d_device_decref(device->wined3d_device);
4821 heap_free(swapchain_desc);
4822 wined3d_mutex_unlock();
4823 return E_OUTOFMEMORY;
4826 for (i = 0; i < count; ++i)
4828 present_parameters_from_wined3d_swapchain_desc(&parameters[i],
4829 &swapchain_desc[i], parameters[i].PresentationInterval);
4832 wined3d_mutex_unlock();
4834 heap_free(swapchain_desc);
4836 /* Initialize the converted declaration array. This creates a valid pointer
4837 * and when adding decls HeapReAlloc() can be used without further checking. */
4838 if (!(device->fvf_decls = heap_alloc(0)))
4840 ERR("Failed to allocate FVF vertex declaration map memory.\n");
4841 wined3d_mutex_lock();
4842 heap_free(device->implicit_swapchains);
4843 wined3d_swapchain_decref(d3d_swapchain->wined3d_swapchain);
4844 wined3d_device_release_focus_window(device->wined3d_device);
4845 wined3d_device_decref(device->wined3d_device);
4846 wined3d_mutex_unlock();
4847 return E_OUTOFMEMORY;
4850 /* We could also simply ignore the initial rendertarget since it's known
4851 * not to be a texture (we currently use these only for automatic mipmap
4852 * generation). */
4853 wined3d_mutex_lock();
4854 device->render_targets[0] = wined3d_rendertarget_view_get_sub_resource_parent(
4855 wined3d_device_context_get_rendertarget_view(device->immediate_context, 0));
4856 wined3d_mutex_unlock();
4858 IDirect3D9Ex_AddRef(&parent->IDirect3D9Ex_iface);
4859 device->d3d_parent = parent;
4861 return D3D_OK;