wined3d: Pass a wined3d_device_context to wined3d_cs_emit_blt_sub_resource().
[wine/zf.git] / dlls / d3d9 / device.c
blob2cd9d73d0d94356a575bde57b3e96cd23287c23f
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_get_viewports(device->wined3d_device, NULL, &vp);
591 wined3d_stateblock_set_viewport(device->state, &vp);
592 wined3d_device_get_scissor_rects(device->wined3d_device, 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_get_rendertarget_view(device->wined3d_device, 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_copy_sub_resource_region(device->wined3d_device,
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_surface *rt_impl = unsafe_impl_from_IDirect3DSurface9(render_target);
1754 struct d3d9_surface *dst_impl = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1755 struct wined3d_sub_resource_desc wined3d_desc;
1756 RECT dst_rect, src_rect;
1757 HRESULT hr;
1759 TRACE("iface %p, render_target %p, dst_surface %p.\n", iface, render_target, dst_surface);
1761 if (!render_target || !dst_surface)
1762 return D3DERR_INVALIDCALL;
1764 wined3d_mutex_lock();
1765 wined3d_texture_get_sub_resource_desc(dst_impl->wined3d_texture, dst_impl->sub_resource_idx, &wined3d_desc);
1766 SetRect(&dst_rect, 0, 0, wined3d_desc.width, wined3d_desc.height);
1768 wined3d_texture_get_sub_resource_desc(rt_impl->wined3d_texture, rt_impl->sub_resource_idx, &wined3d_desc);
1769 SetRect(&src_rect, 0, 0, wined3d_desc.width, wined3d_desc.height);
1771 /* TODO: Check surface sizes, pools, etc. */
1772 if (wined3d_desc.multisample_type)
1773 hr = D3DERR_INVALIDCALL;
1774 else
1775 hr = wined3d_texture_blt(dst_impl->wined3d_texture, dst_impl->sub_resource_idx, &dst_rect,
1776 rt_impl->wined3d_texture, rt_impl->sub_resource_idx, &src_rect, 0, NULL, WINED3D_TEXF_POINT);
1777 wined3d_mutex_unlock();
1779 return hr;
1782 static HRESULT WINAPI d3d9_device_GetFrontBufferData(IDirect3DDevice9Ex *iface,
1783 UINT swapchain, IDirect3DSurface9 *dst_surface)
1785 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1786 struct d3d9_surface *dst_impl = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1787 HRESULT hr = D3DERR_INVALIDCALL;
1789 TRACE("iface %p, swapchain %u, dst_surface %p.\n", iface, swapchain, dst_surface);
1791 wined3d_mutex_lock();
1792 if (swapchain < device->implicit_swapchain_count)
1793 hr = wined3d_swapchain_get_front_buffer_data(device->implicit_swapchains[swapchain],
1794 dst_impl->wined3d_texture, dst_impl->sub_resource_idx);
1795 wined3d_mutex_unlock();
1797 return hr;
1800 static HRESULT WINAPI d3d9_device_StretchRect(IDirect3DDevice9Ex *iface, IDirect3DSurface9 *src_surface,
1801 const RECT *src_rect, IDirect3DSurface9 *dst_surface, const RECT *dst_rect, D3DTEXTUREFILTERTYPE filter)
1803 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1804 struct d3d9_surface *src = unsafe_impl_from_IDirect3DSurface9(src_surface);
1805 struct d3d9_surface *dst = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1806 struct wined3d_sub_resource_desc src_desc, dst_desc;
1807 HRESULT hr = D3DERR_INVALIDCALL;
1808 RECT d, s;
1810 TRACE("iface %p, src_surface %p, src_rect %s, dst_surface %p, dst_rect %s, filter %#x.\n",
1811 iface, src_surface, wine_dbgstr_rect(src_rect), dst_surface, wine_dbgstr_rect(dst_rect), filter);
1813 wined3d_mutex_lock();
1814 wined3d_texture_get_sub_resource_desc(dst->wined3d_texture, dst->sub_resource_idx, &dst_desc);
1815 if (!dst_rect)
1817 SetRect(&d, 0, 0, dst_desc.width, dst_desc.height);
1818 dst_rect = &d;
1821 wined3d_texture_get_sub_resource_desc(src->wined3d_texture, src->sub_resource_idx, &src_desc);
1822 if (!src_rect)
1824 SetRect(&s, 0, 0, src_desc.width, src_desc.height);
1825 src_rect = &s;
1828 if (dst_desc.access & WINED3D_RESOURCE_ACCESS_CPU)
1830 WARN("Destination resource is not in DEFAULT pool.\n");
1831 goto done;
1833 if (src_desc.access & WINED3D_RESOURCE_ACCESS_CPU)
1835 WARN("Source resource is not in DEFAULT pool.\n");
1836 goto done;
1839 if (dst->texture && !(dst_desc.bind_flags & (WINED3D_BIND_RENDER_TARGET | WINED3D_BIND_DEPTH_STENCIL)))
1841 WARN("Destination is a regular texture.\n");
1842 goto done;
1845 if (src_desc.bind_flags & WINED3D_BIND_DEPTH_STENCIL)
1847 if (device->in_scene)
1849 WARN("Rejecting depth / stencil blit while in scene.\n");
1850 goto done;
1853 if (src_rect->left || src_rect->top || src_rect->right != src_desc.width
1854 || src_rect->bottom != src_desc.height)
1856 WARN("Rejecting depth / stencil blit with invalid source rect %s.\n",
1857 wine_dbgstr_rect(src_rect));
1858 goto done;
1861 if (dst_rect->left || dst_rect->top || dst_rect->right != dst_desc.width
1862 || dst_rect->bottom != dst_desc.height)
1864 WARN("Rejecting depth / stencil blit with invalid destination rect %s.\n",
1865 wine_dbgstr_rect(dst_rect));
1866 goto done;
1869 if (src_desc.width != dst_desc.width || src_desc.height != dst_desc.height)
1871 WARN("Rejecting depth / stencil blit with mismatched surface sizes.\n");
1872 goto done;
1876 hr = wined3d_texture_blt(dst->wined3d_texture, dst->sub_resource_idx, dst_rect, src->wined3d_texture,
1877 src->sub_resource_idx, src_rect, 0, NULL, wined3d_texture_filter_type_from_d3d(filter));
1878 if (hr == WINEDDERR_INVALIDRECT)
1879 hr = D3DERR_INVALIDCALL;
1880 if (SUCCEEDED(hr) && dst->texture)
1881 d3d9_texture_flag_auto_gen_mipmap(dst->texture);
1883 done:
1884 wined3d_mutex_unlock();
1885 return hr;
1888 static HRESULT WINAPI d3d9_device_ColorFill(IDirect3DDevice9Ex *iface,
1889 IDirect3DSurface9 *surface, const RECT *rect, D3DCOLOR color)
1891 const struct wined3d_color c =
1893 ((color >> 16) & 0xff) / 255.0f,
1894 ((color >> 8) & 0xff) / 255.0f,
1895 (color & 0xff) / 255.0f,
1896 ((color >> 24) & 0xff) / 255.0f,
1898 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1899 struct d3d9_surface *surface_impl = unsafe_impl_from_IDirect3DSurface9(surface);
1900 struct wined3d_sub_resource_desc desc;
1901 struct wined3d_rendertarget_view *rtv;
1902 HRESULT hr;
1904 TRACE("iface %p, surface %p, rect %p, color 0x%08x.\n", iface, surface, rect, color);
1906 if (!surface)
1907 return D3DERR_INVALIDCALL;
1909 wined3d_mutex_lock();
1911 if (FAILED(wined3d_texture_get_sub_resource_desc(surface_impl->wined3d_texture,
1912 surface_impl->sub_resource_idx, &desc)))
1914 wined3d_mutex_unlock();
1915 return D3DERR_INVALIDCALL;
1918 if (desc.access & WINED3D_RESOURCE_ACCESS_CPU)
1920 wined3d_mutex_unlock();
1921 WARN("Colour fills are not allowed on surfaces with resource access %#x.\n", desc.access);
1922 return D3DERR_INVALIDCALL;
1924 if ((desc.bind_flags & (WINED3D_BIND_RENDER_TARGET | WINED3D_BIND_SHADER_RESOURCE)) == WINED3D_BIND_SHADER_RESOURCE)
1926 wined3d_mutex_unlock();
1927 WARN("Colorfill is not allowed on non-RT textures, returning D3DERR_INVALIDCALL.\n");
1928 return D3DERR_INVALIDCALL;
1930 if (desc.bind_flags & WINED3D_BIND_DEPTH_STENCIL)
1932 wined3d_mutex_unlock();
1933 WARN("Colorfill is not allowed on depth stencil surfaces, returning D3DERR_INVALIDCALL.\n");
1934 return D3DERR_INVALIDCALL;
1937 wined3d_device_apply_stateblock(device->wined3d_device, device->state);
1938 rtv = d3d9_surface_acquire_rendertarget_view(surface_impl);
1939 hr = wined3d_device_clear_rendertarget_view(device->wined3d_device,
1940 rtv, rect, WINED3DCLEAR_TARGET, &c, 0.0f, 0);
1941 d3d9_surface_release_rendertarget_view(surface_impl, rtv);
1942 if (SUCCEEDED(hr) && surface_impl->texture)
1943 d3d9_texture_flag_auto_gen_mipmap(surface_impl->texture);
1945 wined3d_mutex_unlock();
1947 return hr;
1950 static HRESULT WINAPI d3d9_device_CreateOffscreenPlainSurface(IDirect3DDevice9Ex *iface,
1951 UINT width, UINT height, D3DFORMAT format, D3DPOOL pool, IDirect3DSurface9 **surface,
1952 HANDLE *shared_handle)
1954 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1955 unsigned int usage, access;
1956 void *user_mem = NULL;
1958 TRACE("iface %p, width %u, height %u, format %#x, pool %#x, surface %p, shared_handle %p.\n",
1959 iface, width, height, format, pool, surface, shared_handle);
1961 *surface = NULL;
1962 if (pool == D3DPOOL_MANAGED)
1964 WARN("Attempting to create a managed offscreen plain surface.\n");
1965 return D3DERR_INVALIDCALL;
1968 if (shared_handle)
1970 if (!device->d3d_parent->extended)
1972 WARN("Trying to create a shared or user memory surface on a non-ex device.\n");
1973 return E_NOTIMPL;
1976 if (pool == D3DPOOL_SYSTEMMEM)
1977 user_mem = *shared_handle;
1978 else
1980 if (pool != D3DPOOL_DEFAULT)
1982 WARN("Trying to create a shared surface in pool %#x.\n", pool);
1983 return D3DERR_INVALIDCALL;
1985 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1989 usage = 0;
1990 if (pool == D3DPOOL_SCRATCH)
1991 usage |= WINED3DUSAGE_SCRATCH;
1992 access = wined3daccess_from_d3dpool(pool, usage)
1993 | WINED3D_RESOURCE_ACCESS_MAP_R | WINED3D_RESOURCE_ACCESS_MAP_W;
1995 return d3d9_device_create_surface(device, 0, wined3dformat_from_d3dformat(format),
1996 WINED3D_MULTISAMPLE_NONE, 0, usage, 0, access, width, height, user_mem, surface);
1999 static HRESULT WINAPI d3d9_device_SetRenderTarget(IDirect3DDevice9Ex *iface, DWORD idx, IDirect3DSurface9 *surface)
2001 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2002 struct d3d9_surface *surface_impl = unsafe_impl_from_IDirect3DSurface9(surface);
2003 struct wined3d_rendertarget_view *rtv;
2004 HRESULT hr;
2006 TRACE("iface %p, idx %u, surface %p.\n", iface, idx, surface);
2008 if (idx >= D3D_MAX_SIMULTANEOUS_RENDERTARGETS)
2010 WARN("Invalid index %u specified.\n", idx);
2011 return D3DERR_INVALIDCALL;
2014 if (!idx && !surface_impl)
2016 WARN("Trying to set render target 0 to NULL.\n");
2017 return D3DERR_INVALIDCALL;
2020 if (surface_impl && d3d9_surface_get_device(surface_impl) != device)
2022 WARN("Render target surface does not match device.\n");
2023 return D3DERR_INVALIDCALL;
2026 wined3d_mutex_lock();
2027 rtv = surface_impl ? d3d9_surface_acquire_rendertarget_view(surface_impl) : NULL;
2028 hr = wined3d_device_context_set_rendertarget_view(device->immediate_context, idx, rtv, TRUE);
2029 d3d9_surface_release_rendertarget_view(surface_impl, rtv);
2030 if (SUCCEEDED(hr))
2032 if (!idx)
2033 device_reset_viewport_state(device);
2034 device->render_targets[idx] = surface_impl;
2036 wined3d_mutex_unlock();
2038 return hr;
2041 static HRESULT WINAPI d3d9_device_GetRenderTarget(IDirect3DDevice9Ex *iface, DWORD idx, IDirect3DSurface9 **surface)
2043 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2044 struct wined3d_rendertarget_view *wined3d_rtv;
2045 struct d3d9_surface *surface_impl;
2046 HRESULT hr = D3D_OK;
2048 TRACE("iface %p, idx %u, surface %p.\n", iface, idx, surface);
2050 if (!surface)
2051 return D3DERR_INVALIDCALL;
2053 if (idx >= D3D_MAX_SIMULTANEOUS_RENDERTARGETS)
2055 WARN("Invalid index %u specified.\n", idx);
2056 return D3DERR_INVALIDCALL;
2059 wined3d_mutex_lock();
2060 if ((wined3d_rtv = wined3d_device_get_rendertarget_view(device->wined3d_device, idx)))
2062 /* We want the sub resource parent here, since the view itself may be
2063 * internal to wined3d and may not have a parent. */
2064 surface_impl = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_rtv);
2065 *surface = &surface_impl->IDirect3DSurface9_iface;
2066 IDirect3DSurface9_AddRef(*surface);
2068 else
2070 hr = D3DERR_NOTFOUND;
2071 *surface = NULL;
2073 wined3d_mutex_unlock();
2075 return hr;
2078 static HRESULT WINAPI d3d9_device_SetDepthStencilSurface(IDirect3DDevice9Ex *iface, IDirect3DSurface9 *depth_stencil)
2080 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2081 struct d3d9_surface *ds_impl = unsafe_impl_from_IDirect3DSurface9(depth_stencil);
2082 struct wined3d_rendertarget_view *rtv;
2083 HRESULT hr;
2085 TRACE("iface %p, depth_stencil %p.\n", iface, depth_stencil);
2087 wined3d_mutex_lock();
2088 rtv = ds_impl ? d3d9_surface_acquire_rendertarget_view(ds_impl) : NULL;
2089 hr = wined3d_device_context_set_depth_stencil_view(device->immediate_context, rtv);
2090 d3d9_surface_release_rendertarget_view(ds_impl, rtv);
2091 wined3d_mutex_unlock();
2093 return hr;
2096 static HRESULT WINAPI d3d9_device_GetDepthStencilSurface(IDirect3DDevice9Ex *iface, IDirect3DSurface9 **depth_stencil)
2098 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2099 struct wined3d_rendertarget_view *wined3d_dsv;
2100 struct d3d9_surface *surface_impl;
2101 HRESULT hr = D3D_OK;
2103 TRACE("iface %p, depth_stencil %p.\n", iface, depth_stencil);
2105 if (!depth_stencil)
2106 return D3DERR_INVALIDCALL;
2108 wined3d_mutex_lock();
2109 if ((wined3d_dsv = wined3d_device_get_depth_stencil_view(device->wined3d_device)))
2111 /* We want the sub resource parent here, since the view itself may be
2112 * internal to wined3d and may not have a parent. */
2113 surface_impl = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_dsv);
2114 *depth_stencil = &surface_impl->IDirect3DSurface9_iface;
2115 IDirect3DSurface9_AddRef(*depth_stencil);
2117 else
2119 hr = D3DERR_NOTFOUND;
2120 *depth_stencil = NULL;
2122 wined3d_mutex_unlock();
2124 return hr;
2127 static HRESULT WINAPI d3d9_device_BeginScene(IDirect3DDevice9Ex *iface)
2129 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2130 HRESULT hr;
2132 TRACE("iface %p.\n", iface);
2134 wined3d_mutex_lock();
2135 if (SUCCEEDED(hr = wined3d_device_begin_scene(device->wined3d_device)))
2136 device->in_scene = TRUE;
2137 wined3d_mutex_unlock();
2139 return hr;
2142 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_EndScene(IDirect3DDevice9Ex *iface)
2144 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2145 HRESULT hr;
2147 TRACE("iface %p.\n", iface);
2149 wined3d_mutex_lock();
2150 if (SUCCEEDED(hr = wined3d_device_end_scene(device->wined3d_device)))
2151 device->in_scene = FALSE;
2152 wined3d_mutex_unlock();
2154 return hr;
2157 static void d3d9_rts_flag_auto_gen_mipmap(struct d3d9_device *device)
2159 unsigned int i;
2161 for (i = 0; i < ARRAY_SIZE(device->render_targets); ++i)
2163 struct d3d9_surface *surface = device->render_targets[i];
2165 if (surface && surface->texture)
2166 d3d9_texture_flag_auto_gen_mipmap(surface->texture);
2170 static HRESULT WINAPI d3d9_device_Clear(IDirect3DDevice9Ex *iface, DWORD rect_count,
2171 const D3DRECT *rects, DWORD flags, D3DCOLOR color, float z, DWORD stencil)
2173 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2174 struct wined3d_color c;
2175 HRESULT hr;
2177 TRACE("iface %p, rect_count %u, rects %p, flags %#x, color 0x%08x, z %.8e, stencil %u.\n",
2178 iface, rect_count, rects, flags, color, z, stencil);
2180 if (rect_count && !rects)
2182 WARN("count %u with NULL rects.\n", rect_count);
2183 rect_count = 0;
2186 wined3d_color_from_d3dcolor(&c, color);
2187 wined3d_mutex_lock();
2188 wined3d_device_apply_stateblock(device->wined3d_device, device->state);
2189 hr = wined3d_device_clear(device->wined3d_device, rect_count, (const RECT *)rects, flags, &c, z, stencil);
2190 if (SUCCEEDED(hr))
2191 d3d9_rts_flag_auto_gen_mipmap(device);
2192 wined3d_mutex_unlock();
2194 return hr;
2197 static HRESULT WINAPI d3d9_device_SetTransform(IDirect3DDevice9Ex *iface,
2198 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
2200 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2202 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
2204 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
2205 wined3d_mutex_lock();
2206 wined3d_stateblock_set_transform(device->update_state,
2207 wined3d_transform_state_from_d3d(state), (const struct wined3d_matrix *)matrix);
2208 wined3d_mutex_unlock();
2210 return D3D_OK;
2213 static HRESULT WINAPI d3d9_device_GetTransform(IDirect3DDevice9Ex *iface,
2214 D3DTRANSFORMSTATETYPE state, D3DMATRIX *matrix)
2216 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2218 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
2220 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
2221 wined3d_mutex_lock();
2222 memcpy(matrix, &device->stateblock_state->transforms[state], sizeof(*matrix));
2223 wined3d_mutex_unlock();
2225 return D3D_OK;
2228 static HRESULT WINAPI d3d9_device_MultiplyTransform(IDirect3DDevice9Ex *iface,
2229 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
2231 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2233 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
2235 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
2236 wined3d_mutex_lock();
2237 wined3d_stateblock_multiply_transform(device->state,
2238 wined3d_transform_state_from_d3d(state), (const struct wined3d_matrix *)matrix);
2239 wined3d_mutex_unlock();
2241 return D3D_OK;
2244 static HRESULT WINAPI d3d9_device_SetViewport(IDirect3DDevice9Ex *iface, const D3DVIEWPORT9 *viewport)
2246 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2247 struct wined3d_viewport vp;
2249 TRACE("iface %p, viewport %p.\n", iface, viewport);
2251 vp.x = viewport->X;
2252 vp.y = viewport->Y;
2253 vp.width = viewport->Width;
2254 vp.height = viewport->Height;
2255 vp.min_z = viewport->MinZ;
2256 vp.max_z = viewport->MaxZ;
2258 wined3d_mutex_lock();
2259 wined3d_stateblock_set_viewport(device->update_state, &vp);
2260 wined3d_mutex_unlock();
2262 return D3D_OK;
2265 static HRESULT WINAPI d3d9_device_GetViewport(IDirect3DDevice9Ex *iface, D3DVIEWPORT9 *viewport)
2267 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2268 struct wined3d_viewport wined3d_viewport;
2270 TRACE("iface %p, viewport %p.\n", iface, viewport);
2272 wined3d_mutex_lock();
2273 wined3d_viewport = device->stateblock_state->viewport;
2274 wined3d_mutex_unlock();
2276 viewport->X = wined3d_viewport.x;
2277 viewport->Y = wined3d_viewport.y;
2278 viewport->Width = wined3d_viewport.width;
2279 viewport->Height = wined3d_viewport.height;
2280 viewport->MinZ = wined3d_viewport.min_z;
2281 viewport->MaxZ = wined3d_viewport.max_z;
2283 return D3D_OK;
2286 static HRESULT WINAPI d3d9_device_SetMaterial(IDirect3DDevice9Ex *iface, const D3DMATERIAL9 *material)
2288 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2290 TRACE("iface %p, material %p.\n", iface, material);
2292 /* Note: D3DMATERIAL9 is compatible with struct wined3d_material. */
2293 wined3d_mutex_lock();
2294 wined3d_stateblock_set_material(device->update_state, (const struct wined3d_material *)material);
2295 wined3d_mutex_unlock();
2297 return D3D_OK;
2300 static HRESULT WINAPI d3d9_device_GetMaterial(IDirect3DDevice9Ex *iface, D3DMATERIAL9 *material)
2302 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2304 TRACE("iface %p, material %p.\n", iface, material);
2306 /* Note: D3DMATERIAL9 is compatible with struct wined3d_material. */
2307 wined3d_mutex_lock();
2308 memcpy(material, &device->stateblock_state->material, sizeof(*material));
2309 wined3d_mutex_unlock();
2311 return D3D_OK;
2314 static HRESULT WINAPI d3d9_device_SetLight(IDirect3DDevice9Ex *iface, DWORD index, const D3DLIGHT9 *light)
2316 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2317 HRESULT hr;
2319 TRACE("iface %p, index %u, light %p.\n", iface, index, light);
2321 /* Note: D3DLIGHT9 is compatible with struct wined3d_light. */
2322 wined3d_mutex_lock();
2323 hr = wined3d_stateblock_set_light(device->update_state, index, (const struct wined3d_light *)light);
2324 wined3d_mutex_unlock();
2326 return hr;
2329 static HRESULT WINAPI d3d9_device_GetLight(IDirect3DDevice9Ex *iface, DWORD index, D3DLIGHT9 *light)
2331 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2332 BOOL enabled;
2333 HRESULT hr;
2335 TRACE("iface %p, index %u, light %p.\n", iface, index, light);
2337 /* Note: D3DLIGHT9 is compatible with struct wined3d_light. */
2338 wined3d_mutex_lock();
2339 hr = wined3d_stateblock_get_light(device->state, index, (struct wined3d_light *)light, &enabled);
2340 wined3d_mutex_unlock();
2342 return hr;
2345 static HRESULT WINAPI d3d9_device_LightEnable(IDirect3DDevice9Ex *iface, DWORD index, BOOL enable)
2347 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2348 HRESULT hr;
2350 TRACE("iface %p, index %u, enable %#x.\n", iface, index, enable);
2352 wined3d_mutex_lock();
2353 hr = wined3d_stateblock_set_light_enable(device->update_state, index, enable);
2354 wined3d_mutex_unlock();
2356 return hr;
2359 static HRESULT WINAPI d3d9_device_GetLightEnable(IDirect3DDevice9Ex *iface, DWORD index, BOOL *enabled)
2361 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2362 struct wined3d_light light;
2363 HRESULT hr;
2365 TRACE("iface %p, index %u, enabled %p.\n", iface, index, enabled);
2367 wined3d_mutex_lock();
2368 hr = wined3d_stateblock_get_light(device->state, index, &light, enabled);
2369 wined3d_mutex_unlock();
2371 return hr;
2374 static HRESULT WINAPI d3d9_device_SetClipPlane(IDirect3DDevice9Ex *iface, DWORD index, const float *plane)
2376 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2377 HRESULT hr;
2379 TRACE("iface %p, index %u, plane %p.\n", iface, index, plane);
2381 index = min(index, device->max_user_clip_planes - 1);
2383 wined3d_mutex_lock();
2384 hr = wined3d_stateblock_set_clip_plane(device->update_state, index, (const struct wined3d_vec4 *)plane);
2385 wined3d_mutex_unlock();
2387 return hr;
2390 static HRESULT WINAPI d3d9_device_GetClipPlane(IDirect3DDevice9Ex *iface, DWORD index, float *plane)
2392 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2394 TRACE("iface %p, index %u, plane %p.\n", iface, index, plane);
2396 index = min(index, device->max_user_clip_planes - 1);
2398 wined3d_mutex_lock();
2399 memcpy(plane, &device->stateblock_state->clip_planes[index], sizeof(struct wined3d_vec4));
2400 wined3d_mutex_unlock();
2402 return D3D_OK;
2405 static void resolve_depth_buffer(struct d3d9_device *device)
2407 const struct wined3d_stateblock_state *state = device->stateblock_state;
2408 struct wined3d_rendertarget_view *wined3d_dsv;
2409 struct wined3d_resource *dst_resource;
2410 struct wined3d_texture *dst_texture;
2411 struct wined3d_resource_desc desc;
2412 struct d3d9_surface *d3d9_dsv;
2414 if (!(dst_texture = state->textures[0]))
2415 return;
2416 dst_resource = wined3d_texture_get_resource(dst_texture);
2417 wined3d_resource_get_desc(dst_resource, &desc);
2418 if (desc.format != WINED3DFMT_D24_UNORM_S8_UINT
2419 && desc.format != WINED3DFMT_X8D24_UNORM
2420 && desc.format != WINED3DFMT_DF16
2421 && desc.format != WINED3DFMT_DF24
2422 && desc.format != WINED3DFMT_INTZ)
2423 return;
2425 if (!(wined3d_dsv = wined3d_device_get_depth_stencil_view(device->wined3d_device)))
2426 return;
2427 d3d9_dsv = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_dsv);
2429 wined3d_device_resolve_sub_resource(device->wined3d_device, dst_resource, 0,
2430 wined3d_rendertarget_view_get_resource(wined3d_dsv), d3d9_dsv->sub_resource_idx, desc.format);
2433 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_SetRenderState(IDirect3DDevice9Ex *iface,
2434 D3DRENDERSTATETYPE state, DWORD value)
2436 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2438 TRACE("iface %p, state %#x, value %#x.\n", iface, state, value);
2440 wined3d_mutex_lock();
2441 wined3d_stateblock_set_render_state(device->update_state, wined3d_render_state_from_d3d(state), value);
2442 if (state == D3DRS_POINTSIZE && value == D3D9_RESZ_CODE)
2443 resolve_depth_buffer(device);
2444 wined3d_mutex_unlock();
2446 return D3D_OK;
2449 static HRESULT WINAPI d3d9_device_GetRenderState(IDirect3DDevice9Ex *iface,
2450 D3DRENDERSTATETYPE state, DWORD *value)
2452 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2453 const struct wined3d_stateblock_state *device_state;
2455 TRACE("iface %p, state %#x, value %p.\n", iface, state, value);
2457 wined3d_mutex_lock();
2458 device_state = device->stateblock_state;
2459 *value = device_state->rs[state];
2460 wined3d_mutex_unlock();
2462 return D3D_OK;
2465 static HRESULT WINAPI d3d9_device_CreateStateBlock(IDirect3DDevice9Ex *iface,
2466 D3DSTATEBLOCKTYPE type, IDirect3DStateBlock9 **stateblock)
2468 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2469 struct d3d9_stateblock *object;
2470 HRESULT hr;
2472 TRACE("iface %p, type %#x, stateblock %p.\n", iface, type, stateblock);
2474 if (type != D3DSBT_ALL && type != D3DSBT_PIXELSTATE && type != D3DSBT_VERTEXSTATE)
2476 WARN("Unexpected stateblock type, returning D3DERR_INVALIDCALL.\n");
2477 return D3DERR_INVALIDCALL;
2480 wined3d_mutex_lock();
2481 if (device->recording)
2483 wined3d_mutex_unlock();
2484 WARN("Trying to create a stateblock while recording, returning D3DERR_INVALIDCALL.\n");
2485 return D3DERR_INVALIDCALL;
2487 wined3d_mutex_unlock();
2489 if (!(object = heap_alloc_zero(sizeof(*object))))
2490 return E_OUTOFMEMORY;
2492 hr = stateblock_init(object, device, type, NULL);
2493 if (FAILED(hr))
2495 WARN("Failed to initialize stateblock, hr %#x.\n", hr);
2496 heap_free(object);
2497 return hr;
2500 TRACE("Created stateblock %p.\n", object);
2501 *stateblock = &object->IDirect3DStateBlock9_iface;
2503 return D3D_OK;
2506 static HRESULT WINAPI d3d9_device_BeginStateBlock(IDirect3DDevice9Ex *iface)
2508 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2509 struct wined3d_stateblock *stateblock;
2510 HRESULT hr;
2512 TRACE("iface %p.\n", iface);
2514 wined3d_mutex_lock();
2515 if (device->recording)
2517 wined3d_mutex_unlock();
2518 WARN("Trying to begin a stateblock while recording, returning D3DERR_INVALIDCALL.\n");
2519 return D3DERR_INVALIDCALL;
2522 if (SUCCEEDED(hr = wined3d_stateblock_create(device->wined3d_device, device->state, WINED3D_SBT_RECORDED, &stateblock)))
2523 device->update_state = device->recording = stateblock;
2524 wined3d_mutex_unlock();
2526 return hr;
2529 static HRESULT WINAPI d3d9_device_EndStateBlock(IDirect3DDevice9Ex *iface, IDirect3DStateBlock9 **stateblock)
2531 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2532 struct wined3d_stateblock *wined3d_stateblock;
2533 struct d3d9_stateblock *object;
2534 HRESULT hr;
2536 TRACE("iface %p, stateblock %p.\n", iface, stateblock);
2538 wined3d_mutex_lock();
2539 if (!device->recording)
2541 wined3d_mutex_unlock();
2542 WARN("Trying to end a stateblock, but no stateblock is being recorded.\n");
2543 return D3DERR_INVALIDCALL;
2545 wined3d_stateblock = device->recording;
2546 wined3d_stateblock_init_contained_states(wined3d_stateblock);
2547 device->recording = NULL;
2548 device->update_state = device->state;
2549 wined3d_mutex_unlock();
2551 if (!(object = heap_alloc_zero(sizeof(*object))))
2553 wined3d_mutex_lock();
2554 wined3d_stateblock_decref(wined3d_stateblock);
2555 wined3d_mutex_unlock();
2556 return E_OUTOFMEMORY;
2559 hr = stateblock_init(object, device, 0, wined3d_stateblock);
2560 if (FAILED(hr))
2562 WARN("Failed to initialize stateblock, hr %#x.\n", hr);
2563 wined3d_mutex_lock();
2564 wined3d_stateblock_decref(wined3d_stateblock);
2565 wined3d_mutex_unlock();
2566 heap_free(object);
2567 return hr;
2570 TRACE("Created stateblock %p.\n", object);
2571 *stateblock = &object->IDirect3DStateBlock9_iface;
2573 return D3D_OK;
2576 static HRESULT WINAPI d3d9_device_SetClipStatus(IDirect3DDevice9Ex *iface, const D3DCLIPSTATUS9 *clip_status)
2578 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2579 HRESULT hr;
2581 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
2583 wined3d_mutex_lock();
2584 hr = wined3d_device_set_clip_status(device->wined3d_device, (const struct wined3d_clip_status *)clip_status);
2585 wined3d_mutex_unlock();
2587 return hr;
2590 static HRESULT WINAPI d3d9_device_GetClipStatus(IDirect3DDevice9Ex *iface, D3DCLIPSTATUS9 *clip_status)
2592 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2593 HRESULT hr;
2595 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
2597 wined3d_mutex_lock();
2598 hr = wined3d_device_get_clip_status(device->wined3d_device, (struct wined3d_clip_status *)clip_status);
2599 wined3d_mutex_unlock();
2601 return hr;
2604 static HRESULT WINAPI d3d9_device_GetTexture(IDirect3DDevice9Ex *iface, DWORD stage, IDirect3DBaseTexture9 **texture)
2606 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2607 struct wined3d_texture *wined3d_texture = NULL;
2608 const struct wined3d_stateblock_state *state;
2609 struct d3d9_texture *texture_impl;
2611 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
2613 if (!texture)
2614 return D3DERR_INVALIDCALL;
2616 if (stage >= D3DVERTEXTEXTURESAMPLER0 && stage <= D3DVERTEXTEXTURESAMPLER3)
2617 stage -= D3DVERTEXTEXTURESAMPLER0 - WINED3D_VERTEX_SAMPLER_OFFSET;
2619 if (stage >= ARRAY_SIZE(state->textures))
2621 WARN("Ignoring invalid stage %u.\n", stage);
2622 *texture = NULL;
2623 return D3D_OK;
2626 wined3d_mutex_lock();
2627 state = device->stateblock_state;
2628 if ((wined3d_texture = state->textures[stage]))
2630 texture_impl = wined3d_texture_get_parent(wined3d_texture);
2631 *texture = &texture_impl->IDirect3DBaseTexture9_iface;
2632 IDirect3DBaseTexture9_AddRef(*texture);
2634 else
2636 *texture = NULL;
2638 wined3d_mutex_unlock();
2640 return D3D_OK;
2643 static HRESULT WINAPI d3d9_device_SetTexture(IDirect3DDevice9Ex *iface, DWORD stage, IDirect3DBaseTexture9 *texture)
2645 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2646 struct d3d9_texture *texture_impl;
2648 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
2650 texture_impl = unsafe_impl_from_IDirect3DBaseTexture9(texture);
2652 if (stage >= D3DVERTEXTEXTURESAMPLER0 && stage <= D3DVERTEXTEXTURESAMPLER3)
2653 stage -= D3DVERTEXTEXTURESAMPLER0 - WINED3D_VERTEX_SAMPLER_OFFSET;
2655 wined3d_mutex_lock();
2656 wined3d_stateblock_set_texture(device->update_state, stage,
2657 texture_impl ? texture_impl->wined3d_texture : NULL);
2658 if (!device->recording)
2660 if (stage < D3D9_MAX_TEXTURE_UNITS)
2662 if (texture_impl && texture_impl->usage & D3DUSAGE_AUTOGENMIPMAP)
2663 device->auto_mipmaps |= 1u << stage;
2664 else
2665 device->auto_mipmaps &= ~(1u << stage);
2668 wined3d_mutex_unlock();
2670 return D3D_OK;
2673 static const enum wined3d_texture_stage_state tss_lookup[] =
2675 WINED3D_TSS_INVALID, /* 0, unused */
2676 WINED3D_TSS_COLOR_OP, /* 1, D3DTSS_COLOROP */
2677 WINED3D_TSS_COLOR_ARG1, /* 2, D3DTSS_COLORARG1 */
2678 WINED3D_TSS_COLOR_ARG2, /* 3, D3DTSS_COLORARG2 */
2679 WINED3D_TSS_ALPHA_OP, /* 4, D3DTSS_ALPHAOP */
2680 WINED3D_TSS_ALPHA_ARG1, /* 5, D3DTSS_ALPHAARG1 */
2681 WINED3D_TSS_ALPHA_ARG2, /* 6, D3DTSS_ALPHAARG2 */
2682 WINED3D_TSS_BUMPENV_MAT00, /* 7, D3DTSS_BUMPENVMAT00 */
2683 WINED3D_TSS_BUMPENV_MAT01, /* 8, D3DTSS_BUMPENVMAT01 */
2684 WINED3D_TSS_BUMPENV_MAT10, /* 9, D3DTSS_BUMPENVMAT10 */
2685 WINED3D_TSS_BUMPENV_MAT11, /* 10, D3DTSS_BUMPENVMAT11 */
2686 WINED3D_TSS_TEXCOORD_INDEX, /* 11, D3DTSS_TEXCOORDINDEX */
2687 WINED3D_TSS_INVALID, /* 12, unused */
2688 WINED3D_TSS_INVALID, /* 13, unused */
2689 WINED3D_TSS_INVALID, /* 14, unused */
2690 WINED3D_TSS_INVALID, /* 15, unused */
2691 WINED3D_TSS_INVALID, /* 16, unused */
2692 WINED3D_TSS_INVALID, /* 17, unused */
2693 WINED3D_TSS_INVALID, /* 18, unused */
2694 WINED3D_TSS_INVALID, /* 19, unused */
2695 WINED3D_TSS_INVALID, /* 20, unused */
2696 WINED3D_TSS_INVALID, /* 21, unused */
2697 WINED3D_TSS_BUMPENV_LSCALE, /* 22, D3DTSS_BUMPENVLSCALE */
2698 WINED3D_TSS_BUMPENV_LOFFSET, /* 23, D3DTSS_BUMPENVLOFFSET */
2699 WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS, /* 24, D3DTSS_TEXTURETRANSFORMFLAGS */
2700 WINED3D_TSS_INVALID, /* 25, unused */
2701 WINED3D_TSS_COLOR_ARG0, /* 26, D3DTSS_COLORARG0 */
2702 WINED3D_TSS_ALPHA_ARG0, /* 27, D3DTSS_ALPHAARG0 */
2703 WINED3D_TSS_RESULT_ARG, /* 28, D3DTSS_RESULTARG */
2704 WINED3D_TSS_INVALID, /* 29, unused */
2705 WINED3D_TSS_INVALID, /* 30, unused */
2706 WINED3D_TSS_INVALID, /* 31, unused */
2707 WINED3D_TSS_CONSTANT, /* 32, D3DTSS_CONSTANT */
2710 static HRESULT WINAPI d3d9_device_GetTextureStageState(IDirect3DDevice9Ex *iface,
2711 DWORD stage, D3DTEXTURESTAGESTATETYPE state, DWORD *value)
2713 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2715 TRACE("iface %p, stage %u, state %#x, value %p.\n", iface, stage, state, value);
2717 if (state >= ARRAY_SIZE(tss_lookup) || tss_lookup[state] == WINED3D_TSS_INVALID)
2719 WARN("Invalid state %#x passed.\n", state);
2720 return D3D_OK;
2723 wined3d_mutex_lock();
2724 *value = device->stateblock_state->texture_states[stage][tss_lookup[state]];
2725 wined3d_mutex_unlock();
2727 return D3D_OK;
2730 static HRESULT WINAPI d3d9_device_SetTextureStageState(IDirect3DDevice9Ex *iface,
2731 DWORD stage, D3DTEXTURESTAGESTATETYPE state, DWORD value)
2733 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2735 TRACE("iface %p, stage %u, state %#x, value %#x.\n", iface, stage, state, value);
2737 if (state >= ARRAY_SIZE(tss_lookup))
2739 WARN("Invalid state %#x passed.\n", state);
2740 return D3D_OK;
2743 wined3d_mutex_lock();
2744 wined3d_stateblock_set_texture_stage_state(device->update_state, stage, tss_lookup[state], value);
2745 wined3d_mutex_unlock();
2747 return D3D_OK;
2750 static HRESULT WINAPI d3d9_device_GetSamplerState(IDirect3DDevice9Ex *iface,
2751 DWORD sampler_idx, D3DSAMPLERSTATETYPE state, DWORD *value)
2753 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2754 const struct wined3d_stateblock_state *device_state;
2756 TRACE("iface %p, sampler_idx %u, state %#x, value %p.\n", iface, sampler_idx, state, value);
2758 if (sampler_idx >= D3DVERTEXTEXTURESAMPLER0 && sampler_idx <= D3DVERTEXTEXTURESAMPLER3)
2759 sampler_idx -= D3DVERTEXTEXTURESAMPLER0 - WINED3D_VERTEX_SAMPLER_OFFSET;
2761 if (sampler_idx >= ARRAY_SIZE(device_state->sampler_states))
2763 WARN("Invalid sampler %u.\n", sampler_idx);
2764 *value = 0;
2765 return D3D_OK;
2768 wined3d_mutex_lock();
2769 device_state = device->stateblock_state;
2770 *value = device_state->sampler_states[sampler_idx][state];
2771 wined3d_mutex_unlock();
2773 return D3D_OK;
2776 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_SetSamplerState(IDirect3DDevice9Ex *iface,
2777 DWORD sampler, D3DSAMPLERSTATETYPE state, DWORD value)
2779 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2781 TRACE("iface %p, sampler %u, state %#x, value %#x.\n", iface, sampler, state, value);
2783 if (sampler >= D3DVERTEXTEXTURESAMPLER0 && sampler <= D3DVERTEXTEXTURESAMPLER3)
2784 sampler -= D3DVERTEXTEXTURESAMPLER0 - WINED3D_VERTEX_SAMPLER_OFFSET;
2786 wined3d_mutex_lock();
2787 wined3d_stateblock_set_sampler_state(device->update_state, sampler, wined3d_sampler_state_from_d3d(state), value);
2788 wined3d_mutex_unlock();
2790 return D3D_OK;
2793 static HRESULT WINAPI d3d9_device_ValidateDevice(IDirect3DDevice9Ex *iface, DWORD *pass_count)
2795 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2796 HRESULT hr;
2798 TRACE("iface %p, pass_count %p.\n", iface, pass_count);
2800 wined3d_mutex_lock();
2801 wined3d_device_apply_stateblock(device->wined3d_device, device->state);
2802 hr = wined3d_device_validate_device(device->wined3d_device, pass_count);
2803 wined3d_mutex_unlock();
2805 return hr;
2808 static HRESULT WINAPI d3d9_device_SetPaletteEntries(IDirect3DDevice9Ex *iface,
2809 UINT palette_idx, const PALETTEENTRY *entries)
2811 WARN("iface %p, palette_idx %u, entries %p unimplemented.\n", iface, palette_idx, entries);
2813 /* The d3d9 palette API is non-functional on Windows. Getters and setters are implemented,
2814 * and some drivers allow the creation of P8 surfaces. These surfaces can be copied to
2815 * other P8 surfaces with StretchRect, but cannot be converted to (A)RGB.
2817 * Some older(dx7) cards may have support for P8 textures, but games cannot rely on this. */
2818 return D3D_OK;
2821 static HRESULT WINAPI d3d9_device_GetPaletteEntries(IDirect3DDevice9Ex *iface,
2822 UINT palette_idx, PALETTEENTRY *entries)
2824 FIXME("iface %p, palette_idx %u, entries %p unimplemented.\n", iface, palette_idx, entries);
2826 return D3DERR_INVALIDCALL;
2829 static HRESULT WINAPI d3d9_device_SetCurrentTexturePalette(IDirect3DDevice9Ex *iface, UINT palette_idx)
2831 WARN("iface %p, palette_idx %u unimplemented.\n", iface, palette_idx);
2833 return D3D_OK;
2836 static HRESULT WINAPI d3d9_device_GetCurrentTexturePalette(IDirect3DDevice9Ex *iface, UINT *palette_idx)
2838 FIXME("iface %p, palette_idx %p.\n", iface, palette_idx);
2840 return D3DERR_INVALIDCALL;
2843 static HRESULT WINAPI d3d9_device_SetScissorRect(IDirect3DDevice9Ex *iface, const RECT *rect)
2845 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2847 TRACE("iface %p, rect %p.\n", iface, rect);
2849 wined3d_mutex_lock();
2850 wined3d_stateblock_set_scissor_rect(device->update_state, rect);
2851 wined3d_mutex_unlock();
2853 return D3D_OK;
2856 static HRESULT WINAPI d3d9_device_GetScissorRect(IDirect3DDevice9Ex *iface, RECT *rect)
2858 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2860 TRACE("iface %p, rect %p.\n", iface, rect);
2862 wined3d_mutex_lock();
2863 *rect = device->stateblock_state->scissor_rect;
2864 wined3d_mutex_unlock();
2866 return D3D_OK;
2869 static HRESULT WINAPI d3d9_device_SetSoftwareVertexProcessing(IDirect3DDevice9Ex *iface, BOOL software)
2871 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2873 TRACE("iface %p, software %#x.\n", iface, software);
2875 wined3d_mutex_lock();
2876 wined3d_device_set_software_vertex_processing(device->wined3d_device, software);
2877 wined3d_mutex_unlock();
2879 return D3D_OK;
2882 static BOOL WINAPI d3d9_device_GetSoftwareVertexProcessing(IDirect3DDevice9Ex *iface)
2884 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2885 BOOL ret;
2887 TRACE("iface %p.\n", iface);
2889 wined3d_mutex_lock();
2890 ret = wined3d_device_get_software_vertex_processing(device->wined3d_device);
2891 wined3d_mutex_unlock();
2893 return ret;
2896 static HRESULT WINAPI d3d9_device_SetNPatchMode(IDirect3DDevice9Ex *iface, float segment_count)
2898 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2899 HRESULT hr;
2901 TRACE("iface %p, segment_count %.8e.\n", iface, segment_count);
2903 wined3d_mutex_lock();
2904 hr = wined3d_device_set_npatch_mode(device->wined3d_device, segment_count);
2905 wined3d_mutex_unlock();
2907 return hr;
2910 static float WINAPI d3d9_device_GetNPatchMode(IDirect3DDevice9Ex *iface)
2912 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2913 float ret;
2915 TRACE("iface %p.\n", iface);
2917 wined3d_mutex_lock();
2918 ret = wined3d_device_get_npatch_mode(device->wined3d_device);
2919 wined3d_mutex_unlock();
2921 return ret;
2924 /* wined3d critical section must be taken by the caller. */
2925 static void d3d9_generate_auto_mipmaps(struct d3d9_device *device)
2927 const struct wined3d_stateblock_state *state = device->stateblock_state;
2928 struct wined3d_texture *texture;
2929 unsigned int i, map;
2931 map = device->auto_mipmaps;
2932 while (map)
2934 i = wined3d_bit_scan(&map);
2935 if ((texture = state->textures[i]))
2936 d3d9_texture_gen_auto_mipmap(wined3d_texture_get_parent(texture));
2940 static void d3d9_device_upload_sysmem_vertex_buffers(struct d3d9_device *device,
2941 int base_vertex, unsigned int start_vertex, unsigned int vertex_count)
2943 const struct wined3d_stateblock_state *state = device->stateblock_state;
2944 struct wined3d_vertex_declaration *wined3d_decl;
2945 struct wined3d_box box = {0, 0, 0, 1, 0, 1};
2946 const struct wined3d_stream_state *stream;
2947 struct d3d9_vertexbuffer *d3d9_buffer;
2948 struct wined3d_resource *dst_resource;
2949 struct d3d9_vertex_declaration *decl;
2950 struct wined3d_buffer *dst_buffer;
2951 struct wined3d_resource_desc desc;
2952 unsigned int stride, map;
2953 HRESULT hr;
2955 if (!device->sysmem_vb)
2956 return;
2957 wined3d_decl = state->vertex_declaration;
2958 if (!wined3d_decl)
2959 return;
2961 if (base_vertex >= 0 || start_vertex >= -base_vertex)
2962 start_vertex += base_vertex;
2963 else
2964 FIXME("System memory vertex data offset is negative.\n");
2966 decl = wined3d_vertex_declaration_get_parent(wined3d_decl);
2967 map = decl->stream_map & device->sysmem_vb;
2968 while (map)
2970 stream = &state->streams[wined3d_bit_scan(&map)];
2971 dst_buffer = stream->buffer;
2972 stride = stream->stride;
2974 d3d9_buffer = wined3d_buffer_get_parent(dst_buffer);
2975 dst_resource = wined3d_buffer_get_resource(dst_buffer);
2976 wined3d_resource_get_desc(dst_resource, &desc);
2977 box.left = stream->offset + start_vertex * stride;
2978 box.right = min(box.left + vertex_count * stride, desc.size);
2979 if (FAILED(hr = wined3d_device_copy_sub_resource_region(device->wined3d_device,
2980 dst_resource, 0, box.left, 0, 0,
2981 wined3d_buffer_get_resource(d3d9_buffer->wined3d_buffer), 0, &box, 0)))
2982 ERR("Failed to update buffer.\n");
2986 static void d3d9_device_upload_sysmem_index_buffer(struct d3d9_device *device,
2987 unsigned int start_idx, unsigned int idx_count)
2989 const struct wined3d_stateblock_state *state = device->stateblock_state;
2990 struct wined3d_box box = {0, 0, 0, 1, 0, 1};
2991 struct wined3d_resource *dst_resource;
2992 struct d3d9_indexbuffer *d3d9_buffer;
2993 struct wined3d_buffer *dst_buffer;
2994 struct wined3d_resource_desc desc;
2995 enum wined3d_format_id format;
2996 unsigned int idx_size;
2997 HRESULT hr;
2999 if (!device->sysmem_ib)
3000 return;
3002 dst_buffer = state->index_buffer;
3003 format = state->index_format;
3004 d3d9_buffer = wined3d_buffer_get_parent(dst_buffer);
3005 dst_resource = wined3d_buffer_get_resource(dst_buffer);
3006 wined3d_resource_get_desc(dst_resource, &desc);
3007 idx_size = format == WINED3DFMT_R16_UINT ? 2 : 4;
3008 box.left = start_idx * idx_size;
3009 box.right = min(box.left + idx_count * idx_size, desc.size);
3010 if (FAILED(hr = wined3d_device_copy_sub_resource_region(device->wined3d_device,
3011 dst_resource, 0, box.left, 0, 0,
3012 wined3d_buffer_get_resource(d3d9_buffer->wined3d_buffer), 0, &box, 0)))
3013 ERR("Failed to update buffer.\n");
3016 static HRESULT WINAPI d3d9_device_DrawPrimitive(IDirect3DDevice9Ex *iface,
3017 D3DPRIMITIVETYPE primitive_type, UINT start_vertex, UINT primitive_count)
3019 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3020 unsigned int vertex_count;
3022 TRACE("iface %p, primitive_type %#x, start_vertex %u, primitive_count %u.\n",
3023 iface, primitive_type, start_vertex, primitive_count);
3025 wined3d_mutex_lock();
3026 if (!device->stateblock_state->vertex_declaration)
3028 wined3d_mutex_unlock();
3029 WARN("Called without a valid vertex declaration set.\n");
3030 return D3DERR_INVALIDCALL;
3032 wined3d_device_apply_stateblock(device->wined3d_device, device->state);
3033 vertex_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
3034 d3d9_device_upload_sysmem_vertex_buffers(device, 0, start_vertex, vertex_count);
3035 d3d9_generate_auto_mipmaps(device);
3036 wined3d_device_set_primitive_type(device->wined3d_device, 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_set_primitive_type(device->wined3d_device, wined3d_primitive_type_from_d3d(primitive_type), 0);
3074 wined3d_device_apply_stateblock(device->wined3d_device, device->state);
3075 wined3d_device_context_draw_indexed(device->immediate_context, base_vertex_idx, start_idx, index_count, 0, 0);
3076 d3d9_rts_flag_auto_gen_mipmap(device);
3077 wined3d_mutex_unlock();
3079 return D3D_OK;
3082 /* The caller is responsible for wined3d locking */
3083 static HRESULT d3d9_device_prepare_vertex_buffer(struct d3d9_device *device, UINT min_size)
3085 HRESULT hr;
3087 if (device->vertex_buffer_size < min_size || !device->vertex_buffer)
3089 UINT size = max(device->vertex_buffer_size * 2, min_size);
3090 struct wined3d_buffer_desc desc;
3091 struct wined3d_buffer *buffer;
3093 TRACE("Growing vertex buffer to %u bytes.\n", size);
3095 desc.byte_width = size;
3096 desc.usage = WINED3DUSAGE_DYNAMIC;
3097 desc.bind_flags = WINED3D_BIND_VERTEX_BUFFER;
3098 desc.access = WINED3D_RESOURCE_ACCESS_GPU | WINED3D_RESOURCE_ACCESS_MAP_W;
3099 desc.misc_flags = 0;
3100 desc.structure_byte_stride = 0;
3102 if (FAILED(hr = wined3d_buffer_create(device->wined3d_device, &desc,
3103 NULL, NULL, &d3d9_null_wined3d_parent_ops, &buffer)))
3105 ERR("Failed to create vertex buffer, hr %#x.\n", hr);
3106 return hr;
3109 if (device->vertex_buffer)
3110 wined3d_buffer_decref(device->vertex_buffer);
3112 device->vertex_buffer = buffer;
3113 device->vertex_buffer_size = size;
3114 device->vertex_buffer_pos = 0;
3116 return D3D_OK;
3119 static HRESULT WINAPI d3d9_device_DrawPrimitiveUP(IDirect3DDevice9Ex *iface,
3120 D3DPRIMITIVETYPE primitive_type, UINT primitive_count, const void *data, UINT stride)
3122 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3123 HRESULT hr;
3124 UINT vtx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
3125 struct wined3d_map_desc wined3d_map_desc;
3126 struct wined3d_box wined3d_box = {0};
3127 UINT size = vtx_count * stride;
3128 struct wined3d_resource *vb;
3129 UINT vb_pos, align;
3131 TRACE("iface %p, primitive_type %#x, primitive_count %u, data %p, stride %u.\n",
3132 iface, primitive_type, primitive_count, data, stride);
3134 if (!stride)
3136 WARN("stride is 0, returning D3DERR_INVALIDCALL.\n");
3137 return D3DERR_INVALIDCALL;
3139 if (!primitive_count)
3141 WARN("primitive_count is 0, returning D3D_OK.\n");
3142 return D3D_OK;
3145 wined3d_mutex_lock();
3147 if (!device->stateblock_state->vertex_declaration)
3149 wined3d_mutex_unlock();
3150 WARN("Called without a valid vertex declaration set.\n");
3151 return D3DERR_INVALIDCALL;
3154 hr = d3d9_device_prepare_vertex_buffer(device, size);
3155 if (FAILED(hr))
3156 goto done;
3158 vb_pos = device->vertex_buffer_pos;
3159 align = vb_pos % stride;
3160 if (align) align = stride - align;
3161 if (vb_pos + size + align > device->vertex_buffer_size)
3162 vb_pos = 0;
3163 else
3164 vb_pos += align;
3166 wined3d_box.left = vb_pos;
3167 wined3d_box.right = vb_pos + size;
3168 vb = wined3d_buffer_get_resource(device->vertex_buffer);
3169 if (FAILED(hr = wined3d_resource_map(vb, 0, &wined3d_map_desc, &wined3d_box,
3170 WINED3D_MAP_WRITE | (vb_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD))))
3171 goto done;
3172 memcpy(wined3d_map_desc.data, data, size);
3173 wined3d_resource_unmap(vb, 0);
3174 device->vertex_buffer_pos = vb_pos + size;
3176 hr = wined3d_stateblock_set_stream_source(device->state, 0, device->vertex_buffer, 0, stride);
3177 if (FAILED(hr))
3178 goto done;
3180 d3d9_generate_auto_mipmaps(device);
3181 wined3d_device_set_primitive_type(device->wined3d_device, wined3d_primitive_type_from_d3d(primitive_type), 0);
3182 wined3d_device_apply_stateblock(device->wined3d_device, device->state);
3183 wined3d_device_context_draw(device->immediate_context, vb_pos / stride, vtx_count, 0, 0);
3184 wined3d_stateblock_set_stream_source(device->state, 0, NULL, 0, 0);
3185 d3d9_rts_flag_auto_gen_mipmap(device);
3187 done:
3188 wined3d_mutex_unlock();
3189 return hr;
3192 /* The caller is responsible for wined3d locking */
3193 static HRESULT d3d9_device_prepare_index_buffer(struct d3d9_device *device, UINT min_size)
3195 HRESULT hr;
3197 if (device->index_buffer_size < min_size || !device->index_buffer)
3199 UINT size = max(device->index_buffer_size * 2, min_size);
3200 struct wined3d_buffer_desc desc;
3201 struct wined3d_buffer *buffer;
3203 TRACE("Growing index buffer to %u bytes.\n", size);
3205 desc.byte_width = size;
3206 desc.usage = WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_STATICDECL;
3207 desc.bind_flags = WINED3D_BIND_INDEX_BUFFER;
3208 desc.access = WINED3D_RESOURCE_ACCESS_GPU | WINED3D_RESOURCE_ACCESS_MAP_W;
3209 desc.misc_flags = 0;
3210 desc.structure_byte_stride = 0;
3212 if (FAILED(hr = wined3d_buffer_create(device->wined3d_device, &desc,
3213 NULL, NULL, &d3d9_null_wined3d_parent_ops, &buffer)))
3215 ERR("Failed to create index buffer, hr %#x.\n", hr);
3216 return hr;
3219 if (device->index_buffer)
3220 wined3d_buffer_decref(device->index_buffer);
3222 device->index_buffer = buffer;
3223 device->index_buffer_size = size;
3224 device->index_buffer_pos = 0;
3226 return D3D_OK;
3229 static HRESULT WINAPI d3d9_device_DrawIndexedPrimitiveUP(IDirect3DDevice9Ex *iface,
3230 D3DPRIMITIVETYPE primitive_type, UINT min_vertex_idx, UINT vertex_count,
3231 UINT primitive_count, const void *index_data, D3DFORMAT index_format,
3232 const void *vertex_data, UINT vertex_stride)
3234 UINT idx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
3235 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3236 UINT idx_fmt_size = index_format == D3DFMT_INDEX16 ? 2 : 4;
3237 UINT vtx_size = vertex_count * vertex_stride;
3238 UINT idx_size = idx_count * idx_fmt_size;
3239 struct wined3d_map_desc wined3d_map_desc;
3240 struct wined3d_box wined3d_box = {0};
3241 struct wined3d_resource *ib, *vb;
3242 UINT vb_pos, ib_pos, align;
3243 HRESULT hr;
3245 TRACE("iface %p, primitive_type %#x, min_vertex_idx %u, vertex_count %u, primitive_count %u, "
3246 "index_data %p, index_format %#x, vertex_data %p, vertex_stride %u.\n",
3247 iface, primitive_type, min_vertex_idx, vertex_count, primitive_count,
3248 index_data, index_format, vertex_data, vertex_stride);
3250 if (!vertex_stride)
3252 WARN("vertex_stride is 0, returning D3DERR_INVALIDCALL.\n");
3253 return D3DERR_INVALIDCALL;
3255 if (!primitive_count)
3257 WARN("primitive_count is 0, returning D3D_OK.\n");
3258 return D3D_OK;
3261 wined3d_mutex_lock();
3263 if (!device->stateblock_state->vertex_declaration)
3265 wined3d_mutex_unlock();
3266 WARN("Called without a valid vertex declaration set.\n");
3267 return D3DERR_INVALIDCALL;
3270 hr = d3d9_device_prepare_vertex_buffer(device, vtx_size);
3271 if (FAILED(hr))
3272 goto done;
3274 vb_pos = device->vertex_buffer_pos;
3275 align = vb_pos % vertex_stride;
3276 if (align) align = vertex_stride - align;
3277 if (vb_pos + vtx_size + align > device->vertex_buffer_size)
3278 vb_pos = 0;
3279 else
3280 vb_pos += align;
3282 wined3d_box.left = vb_pos;
3283 wined3d_box.right = vb_pos + vtx_size;
3284 vb = wined3d_buffer_get_resource(device->vertex_buffer);
3285 if (FAILED(hr = wined3d_resource_map(vb, 0, &wined3d_map_desc, &wined3d_box,
3286 WINED3D_MAP_WRITE | (vb_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD))))
3287 goto done;
3288 memcpy(wined3d_map_desc.data, (char *)vertex_data + min_vertex_idx * vertex_stride, vtx_size);
3289 wined3d_resource_unmap(vb, 0);
3290 device->vertex_buffer_pos = vb_pos + vtx_size;
3292 hr = d3d9_device_prepare_index_buffer(device, idx_size);
3293 if (FAILED(hr))
3294 goto done;
3296 ib_pos = device->index_buffer_pos;
3297 align = ib_pos % idx_fmt_size;
3298 if (align) align = idx_fmt_size - align;
3299 if (ib_pos + idx_size + align > device->index_buffer_size)
3300 ib_pos = 0;
3301 else
3302 ib_pos += align;
3304 wined3d_box.left = ib_pos;
3305 wined3d_box.right = ib_pos + idx_size;
3306 ib = wined3d_buffer_get_resource(device->index_buffer);
3307 if (FAILED(hr = wined3d_resource_map(ib, 0, &wined3d_map_desc, &wined3d_box,
3308 WINED3D_MAP_WRITE | (ib_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD))))
3309 goto done;
3310 memcpy(wined3d_map_desc.data, index_data, idx_size);
3311 wined3d_resource_unmap(ib, 0);
3312 device->index_buffer_pos = ib_pos + idx_size;
3314 hr = wined3d_stateblock_set_stream_source(device->state, 0, device->vertex_buffer, 0, vertex_stride);
3315 if (FAILED(hr))
3316 goto done;
3318 d3d9_generate_auto_mipmaps(device);
3319 wined3d_stateblock_set_index_buffer(device->state, device->index_buffer,
3320 wined3dformat_from_d3dformat(index_format));
3322 wined3d_device_apply_stateblock(device->wined3d_device, device->state);
3323 wined3d_device_set_primitive_type(device->wined3d_device, wined3d_primitive_type_from_d3d(primitive_type), 0);
3324 wined3d_device_context_draw_indexed(device->immediate_context,
3325 vb_pos / vertex_stride - min_vertex_idx, ib_pos / idx_fmt_size, idx_count, 0, 0);
3327 wined3d_stateblock_set_stream_source(device->state, 0, NULL, 0, 0);
3328 wined3d_stateblock_set_index_buffer(device->state, NULL, WINED3DFMT_UNKNOWN);
3330 d3d9_rts_flag_auto_gen_mipmap(device);
3332 done:
3333 wined3d_mutex_unlock();
3334 return hr;
3337 static HRESULT WINAPI d3d9_device_ProcessVertices(IDirect3DDevice9Ex *iface,
3338 UINT src_start_idx, UINT dst_idx, UINT vertex_count, IDirect3DVertexBuffer9 *dst_buffer,
3339 IDirect3DVertexDeclaration9 *declaration, DWORD flags)
3341 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3342 struct d3d9_vertexbuffer *dst_impl = unsafe_impl_from_IDirect3DVertexBuffer9(dst_buffer);
3343 struct d3d9_vertex_declaration *decl_impl = unsafe_impl_from_IDirect3DVertexDeclaration9(declaration);
3344 const struct wined3d_stateblock_state *state;
3345 const struct wined3d_stream_state *stream;
3346 struct d3d9_vertexbuffer *d3d9_buffer;
3347 unsigned int i, map;
3348 HRESULT hr;
3350 TRACE("iface %p, src_start_idx %u, dst_idx %u, vertex_count %u, dst_buffer %p, declaration %p, flags %#x.\n",
3351 iface, src_start_idx, dst_idx, vertex_count, dst_buffer, declaration, flags);
3353 wined3d_mutex_lock();
3354 state = device->stateblock_state;
3356 /* Note that an alternative approach would be to simply create these
3357 * buffers with WINED3D_RESOURCE_ACCESS_MAP_R and update them here like we
3358 * do for draws. In some regards that would be easier, but it seems less
3359 * than optimal to upload data to the GPU only to subsequently download it
3360 * again. */
3361 map = device->sysmem_vb;
3362 while (map)
3364 i = wined3d_bit_scan(&map);
3365 stream = &state->streams[i];
3367 d3d9_buffer = wined3d_buffer_get_parent(stream->buffer);
3368 if (FAILED(wined3d_stateblock_set_stream_source(device->state,
3369 i, d3d9_buffer->wined3d_buffer, stream->offset, stream->stride)))
3370 ERR("Failed to set stream source.\n");
3373 wined3d_device_apply_stateblock(device->wined3d_device, device->state);
3374 hr = wined3d_device_process_vertices(device->wined3d_device, src_start_idx, dst_idx, vertex_count,
3375 dst_impl->wined3d_buffer, decl_impl ? decl_impl->wined3d_declaration : NULL,
3376 flags, dst_impl->fvf);
3378 map = device->sysmem_vb;
3379 while (map)
3381 i = wined3d_bit_scan(&map);
3382 stream = &state->streams[i];
3384 d3d9_buffer = wined3d_buffer_get_parent(stream->buffer);
3385 if (FAILED(wined3d_stateblock_set_stream_source(device->state,
3386 i, d3d9_buffer->draw_buffer, stream->offset, stream->stride)))
3387 ERR("Failed to set stream source.\n");
3390 wined3d_mutex_unlock();
3392 return hr;
3395 static HRESULT WINAPI d3d9_device_CreateVertexDeclaration(IDirect3DDevice9Ex *iface,
3396 const D3DVERTEXELEMENT9 *elements, IDirect3DVertexDeclaration9 **declaration)
3398 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3399 struct d3d9_vertex_declaration *object;
3400 HRESULT hr;
3402 TRACE("iface %p, elements %p, declaration %p.\n", iface, elements, declaration);
3404 if (!declaration)
3406 WARN("Caller passed a NULL declaration, returning D3DERR_INVALIDCALL.\n");
3407 return D3DERR_INVALIDCALL;
3410 if (SUCCEEDED(hr = d3d9_vertex_declaration_create(device, elements, &object)))
3411 *declaration = &object->IDirect3DVertexDeclaration9_iface;
3413 return hr;
3416 static HRESULT WINAPI d3d9_device_SetVertexDeclaration(IDirect3DDevice9Ex *iface,
3417 IDirect3DVertexDeclaration9 *declaration)
3419 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3420 struct d3d9_vertex_declaration *decl_impl = unsafe_impl_from_IDirect3DVertexDeclaration9(declaration);
3422 TRACE("iface %p, declaration %p.\n", iface, declaration);
3424 wined3d_mutex_lock();
3425 wined3d_stateblock_set_vertex_declaration(device->update_state,
3426 decl_impl ? decl_impl->wined3d_declaration : NULL);
3427 wined3d_mutex_unlock();
3429 return D3D_OK;
3432 static HRESULT WINAPI d3d9_device_GetVertexDeclaration(IDirect3DDevice9Ex *iface,
3433 IDirect3DVertexDeclaration9 **declaration)
3435 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3436 struct wined3d_vertex_declaration *wined3d_declaration;
3437 struct d3d9_vertex_declaration *declaration_impl;
3439 TRACE("iface %p, declaration %p.\n", iface, declaration);
3441 if (!declaration) return D3DERR_INVALIDCALL;
3443 wined3d_mutex_lock();
3444 if ((wined3d_declaration = device->stateblock_state->vertex_declaration))
3446 declaration_impl = wined3d_vertex_declaration_get_parent(wined3d_declaration);
3447 *declaration = &declaration_impl->IDirect3DVertexDeclaration9_iface;
3448 IDirect3DVertexDeclaration9_AddRef(*declaration);
3450 else
3452 *declaration = NULL;
3454 wined3d_mutex_unlock();
3456 TRACE("Returning %p.\n", *declaration);
3457 return D3D_OK;
3460 static struct wined3d_vertex_declaration *device_get_fvf_declaration(struct d3d9_device *device, DWORD fvf)
3462 struct wined3d_vertex_declaration *wined3d_declaration;
3463 struct fvf_declaration *fvf_decls = device->fvf_decls;
3464 struct d3d9_vertex_declaration *d3d9_declaration;
3465 D3DVERTEXELEMENT9 *elements;
3466 int p, low, high; /* deliberately signed */
3467 HRESULT hr;
3469 TRACE("Searching for declaration for fvf %08x... ", fvf);
3471 low = 0;
3472 high = device->fvf_decl_count - 1;
3473 while (low <= high)
3475 p = (low + high) >> 1;
3476 TRACE("%d ", p);
3478 if (fvf_decls[p].fvf == fvf)
3480 TRACE("found %p.\n", fvf_decls[p].decl);
3481 return fvf_decls[p].decl;
3484 if (fvf_decls[p].fvf < fvf)
3485 low = p + 1;
3486 else
3487 high = p - 1;
3489 TRACE("not found. Creating and inserting at position %d.\n", low);
3491 if (FAILED(hr = vdecl_convert_fvf(fvf, &elements)))
3492 return NULL;
3494 hr = d3d9_vertex_declaration_create(device, elements, &d3d9_declaration);
3495 heap_free(elements);
3496 if (FAILED(hr))
3497 return NULL;
3499 if (device->fvf_decl_size == device->fvf_decl_count)
3501 UINT grow = max(device->fvf_decl_size / 2, 8);
3503 if (!(fvf_decls = heap_realloc(fvf_decls, sizeof(*fvf_decls) * (device->fvf_decl_size + grow))))
3505 IDirect3DVertexDeclaration9_Release(&d3d9_declaration->IDirect3DVertexDeclaration9_iface);
3506 return NULL;
3508 device->fvf_decls = fvf_decls;
3509 device->fvf_decl_size += grow;
3512 d3d9_declaration->fvf = fvf;
3513 wined3d_declaration = d3d9_declaration->wined3d_declaration;
3514 wined3d_vertex_declaration_incref(wined3d_declaration);
3515 IDirect3DVertexDeclaration9_Release(&d3d9_declaration->IDirect3DVertexDeclaration9_iface);
3517 memmove(fvf_decls + low + 1, fvf_decls + low, sizeof(*fvf_decls) * (device->fvf_decl_count - low));
3518 fvf_decls[low].decl = wined3d_declaration;
3519 fvf_decls[low].fvf = fvf;
3520 ++device->fvf_decl_count;
3522 TRACE("Returning %p. %u declarations in array.\n", wined3d_declaration, device->fvf_decl_count);
3524 return wined3d_declaration;
3527 static HRESULT WINAPI d3d9_device_SetFVF(IDirect3DDevice9Ex *iface, DWORD fvf)
3529 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3530 struct wined3d_vertex_declaration *decl;
3532 TRACE("iface %p, fvf %#x.\n", iface, fvf);
3534 if (!fvf)
3536 WARN("%#x is not a valid FVF.\n", fvf);
3537 return D3D_OK;
3540 wined3d_mutex_lock();
3541 if (!(decl = device_get_fvf_declaration(device, fvf)))
3543 wined3d_mutex_unlock();
3544 ERR("Failed to create a vertex declaration for fvf %#x.\n", fvf);
3545 return D3DERR_DRIVERINTERNALERROR;
3548 wined3d_stateblock_set_vertex_declaration(device->update_state, decl);
3549 wined3d_mutex_unlock();
3551 return D3D_OK;
3554 static HRESULT WINAPI d3d9_device_GetFVF(IDirect3DDevice9Ex *iface, DWORD *fvf)
3556 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3557 struct wined3d_vertex_declaration *wined3d_declaration;
3558 struct d3d9_vertex_declaration *d3d9_declaration;
3560 TRACE("iface %p, fvf %p.\n", iface, fvf);
3562 wined3d_mutex_lock();
3563 if ((wined3d_declaration = device->stateblock_state->vertex_declaration))
3565 d3d9_declaration = wined3d_vertex_declaration_get_parent(wined3d_declaration);
3566 *fvf = d3d9_declaration->fvf;
3568 else
3570 *fvf = 0;
3572 wined3d_mutex_unlock();
3574 TRACE("Returning FVF %#x.\n", *fvf);
3576 return D3D_OK;
3579 static HRESULT WINAPI d3d9_device_CreateVertexShader(IDirect3DDevice9Ex *iface,
3580 const DWORD *byte_code, IDirect3DVertexShader9 **shader)
3582 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3583 struct d3d9_vertexshader *object;
3584 HRESULT hr;
3586 TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
3588 if (!(object = heap_alloc_zero(sizeof(*object))))
3589 return E_OUTOFMEMORY;
3591 hr = vertexshader_init(object, device, byte_code);
3592 if (FAILED(hr))
3594 WARN("Failed to initialize vertex shader, hr %#x.\n", hr);
3595 heap_free(object);
3596 return hr;
3599 TRACE("Created vertex shader %p.\n", object);
3600 *shader = &object->IDirect3DVertexShader9_iface;
3602 return D3D_OK;
3605 static HRESULT WINAPI d3d9_device_SetVertexShader(IDirect3DDevice9Ex *iface, IDirect3DVertexShader9 *shader)
3607 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3608 struct d3d9_vertexshader *shader_obj = unsafe_impl_from_IDirect3DVertexShader9(shader);
3610 TRACE("iface %p, shader %p.\n", iface, shader);
3612 wined3d_mutex_lock();
3613 wined3d_stateblock_set_vertex_shader(device->update_state,
3614 shader_obj ? shader_obj->wined3d_shader : NULL);
3615 wined3d_mutex_unlock();
3617 return D3D_OK;
3620 static HRESULT WINAPI d3d9_device_GetVertexShader(IDirect3DDevice9Ex *iface, IDirect3DVertexShader9 **shader)
3622 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3623 struct d3d9_vertexshader *shader_impl;
3624 struct wined3d_shader *wined3d_shader;
3626 TRACE("iface %p, shader %p.\n", iface, shader);
3628 wined3d_mutex_lock();
3629 if ((wined3d_shader = device->stateblock_state->vs))
3631 shader_impl = wined3d_shader_get_parent(wined3d_shader);
3632 *shader = &shader_impl->IDirect3DVertexShader9_iface;
3633 IDirect3DVertexShader9_AddRef(*shader);
3635 else
3637 *shader = NULL;
3639 wined3d_mutex_unlock();
3641 TRACE("Returning %p.\n", *shader);
3643 return D3D_OK;
3646 static HRESULT WINAPI d3d9_device_SetVertexShaderConstantF(IDirect3DDevice9Ex *iface,
3647 UINT reg_idx, const float *data, UINT count)
3649 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3650 HRESULT hr;
3652 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3654 if (reg_idx + count > D3D9_MAX_VERTEX_SHADER_CONSTANTF)
3656 WARN("Trying to access %u constants, but d3d9 only supports %u\n",
3657 reg_idx + count, D3D9_MAX_VERTEX_SHADER_CONSTANTF);
3658 return D3DERR_INVALIDCALL;
3661 wined3d_mutex_lock();
3662 hr = wined3d_stateblock_set_vs_consts_f(device->update_state, reg_idx,
3663 count, (const struct wined3d_vec4 *)data);
3664 wined3d_mutex_unlock();
3666 return hr;
3669 static HRESULT WINAPI d3d9_device_GetVertexShaderConstantF(IDirect3DDevice9Ex *iface,
3670 UINT start_idx, float *constants, UINT count)
3672 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3673 const struct wined3d_vec4 *src;
3675 TRACE("iface %p, start_idx %u, constants %p, count %u.\n", iface, start_idx, constants, count);
3677 if (!constants)
3678 return D3DERR_INVALIDCALL;
3680 if (start_idx >= device->vs_uniform_count || count > device->vs_uniform_count - start_idx)
3682 WARN("Trying to access %u constants, but d3d9 only supports %u\n",
3683 start_idx + count, device->vs_uniform_count);
3684 return D3DERR_INVALIDCALL;
3687 wined3d_mutex_lock();
3688 src = device->stateblock_state->vs_consts_f;
3689 memcpy(constants, &src[start_idx], count * sizeof(*src));
3690 wined3d_mutex_unlock();
3692 return D3D_OK;
3695 static HRESULT WINAPI d3d9_device_SetVertexShaderConstantI(IDirect3DDevice9Ex *iface,
3696 UINT reg_idx, const int *data, UINT count)
3698 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3699 HRESULT hr;
3701 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3703 wined3d_mutex_lock();
3704 hr = wined3d_stateblock_set_vs_consts_i(device->update_state,
3705 reg_idx, count, (const struct wined3d_ivec4 *)data);
3706 wined3d_mutex_unlock();
3708 return hr;
3711 static HRESULT WINAPI d3d9_device_GetVertexShaderConstantI(IDirect3DDevice9Ex *iface,
3712 UINT start_idx, int *constants, UINT count)
3714 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3715 const struct wined3d_ivec4 *src;
3717 TRACE("iface %p, start_idx %u, constants %p, count %u.\n", iface, start_idx, constants, count);
3719 if (!constants || start_idx >= WINED3D_MAX_CONSTS_I)
3720 return WINED3DERR_INVALIDCALL;
3721 if (count > WINED3D_MAX_CONSTS_I - start_idx)
3722 count = WINED3D_MAX_CONSTS_I - start_idx;
3724 wined3d_mutex_lock();
3725 src = device->stateblock_state->vs_consts_i;
3726 memcpy(constants, &src[start_idx], count * sizeof(*src));
3727 wined3d_mutex_unlock();
3729 return D3D_OK;
3732 static HRESULT WINAPI d3d9_device_SetVertexShaderConstantB(IDirect3DDevice9Ex *iface,
3733 UINT reg_idx, const BOOL *data, UINT count)
3735 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3736 HRESULT hr;
3738 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3740 wined3d_mutex_lock();
3741 hr = wined3d_stateblock_set_vs_consts_b(device->update_state, reg_idx, count, data);
3742 wined3d_mutex_unlock();
3744 return hr;
3747 static HRESULT WINAPI d3d9_device_GetVertexShaderConstantB(IDirect3DDevice9Ex *iface,
3748 UINT start_idx, BOOL *constants, UINT count)
3750 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3752 TRACE("iface %p, start_idx %u, constants %p, count %u.\n", iface, start_idx, constants, count);
3754 if (!constants || start_idx >= WINED3D_MAX_CONSTS_B)
3755 return WINED3DERR_INVALIDCALL;
3756 if (count > WINED3D_MAX_CONSTS_B - start_idx)
3757 count = WINED3D_MAX_CONSTS_B - start_idx;
3759 wined3d_mutex_lock();
3760 memcpy(constants, &device->stateblock_state->vs_consts_b[start_idx], count * sizeof(*constants));
3761 wined3d_mutex_unlock();
3763 return D3D_OK;
3766 static HRESULT WINAPI d3d9_device_SetStreamSource(IDirect3DDevice9Ex *iface,
3767 UINT stream_idx, IDirect3DVertexBuffer9 *buffer, UINT offset, UINT stride)
3769 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3770 struct d3d9_vertexbuffer *buffer_impl = unsafe_impl_from_IDirect3DVertexBuffer9(buffer);
3771 struct wined3d_buffer *wined3d_buffer;
3772 HRESULT hr;
3774 TRACE("iface %p, stream_idx %u, buffer %p, offset %u, stride %u.\n",
3775 iface, stream_idx, buffer, offset, stride);
3777 if (stream_idx >= ARRAY_SIZE(device->stateblock_state->streams))
3779 WARN("Stream index %u out of range.\n", stream_idx);
3780 return WINED3DERR_INVALIDCALL;
3783 wined3d_mutex_lock();
3784 if (!buffer_impl)
3786 const struct wined3d_stream_state *stream = &device->stateblock_state->streams[stream_idx];
3788 offset = stream->offset;
3789 stride = stream->stride;
3790 wined3d_buffer = NULL;
3792 else if (buffer_impl->draw_buffer)
3794 wined3d_buffer = buffer_impl->draw_buffer;
3796 else
3798 wined3d_buffer = buffer_impl->wined3d_buffer;
3801 hr = wined3d_stateblock_set_stream_source(device->update_state, stream_idx, wined3d_buffer, offset, stride);
3802 if (SUCCEEDED(hr) && !device->recording)
3804 if (buffer_impl && buffer_impl->draw_buffer)
3805 device->sysmem_vb |= (1u << stream_idx);
3806 else
3807 device->sysmem_vb &= ~(1u << stream_idx);
3810 wined3d_mutex_unlock();
3812 return hr;
3815 static HRESULT WINAPI d3d9_device_GetStreamSource(IDirect3DDevice9Ex *iface,
3816 UINT stream_idx, IDirect3DVertexBuffer9 **buffer, UINT *offset, UINT *stride)
3818 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3819 const struct wined3d_stateblock_state *state;
3820 const struct wined3d_stream_state *stream;
3821 struct d3d9_vertexbuffer *buffer_impl;
3823 TRACE("iface %p, stream_idx %u, buffer %p, offset %p, stride %p.\n",
3824 iface, stream_idx, buffer, offset, stride);
3826 if (!buffer)
3827 return D3DERR_INVALIDCALL;
3829 if (stream_idx >= ARRAY_SIZE(state->streams))
3831 WARN("Stream index %u out of range.\n", stream_idx);
3832 return WINED3DERR_INVALIDCALL;
3835 wined3d_mutex_lock();
3836 stream = &device->stateblock_state->streams[stream_idx];
3837 if (stream->buffer)
3839 buffer_impl = wined3d_buffer_get_parent(stream->buffer);
3840 *buffer = &buffer_impl->IDirect3DVertexBuffer9_iface;
3841 IDirect3DVertexBuffer9_AddRef(*buffer);
3843 else
3844 *buffer = NULL;
3845 if (offset)
3846 *offset = stream->offset;
3847 *stride = stream->stride;
3848 wined3d_mutex_unlock();
3850 return D3D_OK;
3853 static HRESULT WINAPI d3d9_device_SetStreamSourceFreq(IDirect3DDevice9Ex *iface, UINT stream_idx, UINT freq)
3855 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3856 HRESULT hr;
3858 TRACE("iface %p, stream_idx %u, freq %u.\n", iface, stream_idx, freq);
3860 wined3d_mutex_lock();
3861 hr = wined3d_stateblock_set_stream_source_freq(device->update_state, stream_idx, freq);
3862 wined3d_mutex_unlock();
3864 return hr;
3867 static HRESULT WINAPI d3d9_device_GetStreamSourceFreq(IDirect3DDevice9Ex *iface, UINT stream_idx, UINT *freq)
3869 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3870 const struct wined3d_stream_state *stream;
3872 TRACE("iface %p, stream_idx %u, freq %p.\n", iface, stream_idx, freq);
3874 wined3d_mutex_lock();
3875 stream = &device->stateblock_state->streams[stream_idx];
3876 *freq = stream->flags | stream->frequency;
3877 wined3d_mutex_unlock();
3879 return D3D_OK;
3882 static HRESULT WINAPI d3d9_device_SetIndices(IDirect3DDevice9Ex *iface, IDirect3DIndexBuffer9 *buffer)
3884 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3885 struct d3d9_indexbuffer *ib = unsafe_impl_from_IDirect3DIndexBuffer9(buffer);
3886 struct wined3d_buffer *wined3d_buffer;
3888 TRACE("iface %p, buffer %p.\n", iface, buffer);
3890 if (!ib)
3891 wined3d_buffer = NULL;
3892 else if (ib->draw_buffer)
3893 wined3d_buffer = ib->draw_buffer;
3894 else
3895 wined3d_buffer = ib->wined3d_buffer;
3897 wined3d_mutex_lock();
3898 wined3d_stateblock_set_index_buffer(device->update_state, wined3d_buffer, ib ? ib->format : WINED3DFMT_UNKNOWN);
3899 if (!device->recording)
3900 device->sysmem_ib = ib && ib->draw_buffer;
3901 wined3d_mutex_unlock();
3903 return D3D_OK;
3906 static HRESULT WINAPI d3d9_device_GetIndices(IDirect3DDevice9Ex *iface, IDirect3DIndexBuffer9 **buffer)
3908 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3909 struct wined3d_buffer *wined3d_buffer;
3910 struct d3d9_indexbuffer *buffer_impl;
3912 TRACE("iface %p, buffer %p.\n", iface, buffer);
3914 if (!buffer)
3915 return D3DERR_INVALIDCALL;
3917 wined3d_mutex_lock();
3918 if ((wined3d_buffer = device->stateblock_state->index_buffer))
3920 buffer_impl = wined3d_buffer_get_parent(wined3d_buffer);
3921 *buffer = &buffer_impl->IDirect3DIndexBuffer9_iface;
3922 IDirect3DIndexBuffer9_AddRef(*buffer);
3924 else
3926 *buffer = NULL;
3928 wined3d_mutex_unlock();
3930 return D3D_OK;
3933 static HRESULT WINAPI d3d9_device_CreatePixelShader(IDirect3DDevice9Ex *iface,
3934 const DWORD *byte_code, IDirect3DPixelShader9 **shader)
3936 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3937 struct d3d9_pixelshader *object;
3938 HRESULT hr;
3940 TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
3942 if (!(object = heap_alloc_zero(sizeof(*object))))
3944 FIXME("Failed to allocate pixel shader memory.\n");
3945 return E_OUTOFMEMORY;
3948 hr = pixelshader_init(object, device, byte_code);
3949 if (FAILED(hr))
3951 WARN("Failed to initialize pixel shader, hr %#x.\n", hr);
3952 heap_free(object);
3953 return hr;
3956 TRACE("Created pixel shader %p.\n", object);
3957 *shader = &object->IDirect3DPixelShader9_iface;
3959 return D3D_OK;
3962 static HRESULT WINAPI d3d9_device_SetPixelShader(IDirect3DDevice9Ex *iface, IDirect3DPixelShader9 *shader)
3964 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3965 struct d3d9_pixelshader *shader_obj = unsafe_impl_from_IDirect3DPixelShader9(shader);
3967 TRACE("iface %p, shader %p.\n", iface, shader);
3969 wined3d_mutex_lock();
3970 wined3d_stateblock_set_pixel_shader(device->update_state,
3971 shader_obj ? shader_obj->wined3d_shader : NULL);
3972 wined3d_mutex_unlock();
3974 return D3D_OK;
3977 static HRESULT WINAPI d3d9_device_GetPixelShader(IDirect3DDevice9Ex *iface, IDirect3DPixelShader9 **shader)
3979 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3980 struct d3d9_pixelshader *shader_impl;
3981 struct wined3d_shader *wined3d_shader;
3983 TRACE("iface %p, shader %p.\n", iface, shader);
3985 if (!shader) return D3DERR_INVALIDCALL;
3987 wined3d_mutex_lock();
3988 if ((wined3d_shader = device->stateblock_state->ps))
3990 shader_impl = wined3d_shader_get_parent(wined3d_shader);
3991 *shader = &shader_impl->IDirect3DPixelShader9_iface;
3992 IDirect3DPixelShader9_AddRef(*shader);
3994 else
3996 *shader = NULL;
3998 wined3d_mutex_unlock();
4000 TRACE("Returning %p.\n", *shader);
4002 return D3D_OK;
4005 static HRESULT WINAPI d3d9_device_SetPixelShaderConstantF(IDirect3DDevice9Ex *iface,
4006 UINT reg_idx, const float *data, UINT count)
4008 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
4009 HRESULT hr;
4011 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
4013 wined3d_mutex_lock();
4014 hr = wined3d_stateblock_set_ps_consts_f(device->update_state,
4015 reg_idx, count, (const struct wined3d_vec4 *)data);
4016 wined3d_mutex_unlock();
4018 return hr;
4021 static HRESULT WINAPI d3d9_device_GetPixelShaderConstantF(IDirect3DDevice9Ex *iface,
4022 UINT start_idx, float *constants, UINT count)
4024 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
4025 const struct wined3d_vec4 *src;
4027 TRACE("iface %p, start_idx %u, constants %p, count %u.\n", iface, start_idx, constants, count);
4029 if (!constants || start_idx >= WINED3D_MAX_PS_CONSTS_F || count > WINED3D_MAX_PS_CONSTS_F - start_idx)
4030 return WINED3DERR_INVALIDCALL;
4032 wined3d_mutex_lock();
4033 src = device->stateblock_state->ps_consts_f;
4034 memcpy(constants, &src[start_idx], count * sizeof(*src));
4035 wined3d_mutex_unlock();
4037 return D3D_OK;
4040 static HRESULT WINAPI d3d9_device_SetPixelShaderConstantI(IDirect3DDevice9Ex *iface,
4041 UINT reg_idx, const int *data, UINT count)
4043 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
4044 HRESULT hr;
4046 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
4048 wined3d_mutex_lock();
4049 hr = wined3d_stateblock_set_ps_consts_i(device->update_state,
4050 reg_idx, count, (const struct wined3d_ivec4 *)data);
4051 wined3d_mutex_unlock();
4053 return hr;
4056 static HRESULT WINAPI d3d9_device_GetPixelShaderConstantI(IDirect3DDevice9Ex *iface,
4057 UINT start_idx, int *constants, UINT count)
4059 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
4060 const struct wined3d_ivec4 *src;
4062 TRACE("iface %p, start_idx %u, constants %p, count %u.\n", iface, start_idx, constants, count);
4064 if (!constants || start_idx >= WINED3D_MAX_CONSTS_I)
4065 return WINED3DERR_INVALIDCALL;
4066 if (count > WINED3D_MAX_CONSTS_I - start_idx)
4067 count = WINED3D_MAX_CONSTS_I - start_idx;
4069 wined3d_mutex_lock();
4070 src = device->stateblock_state->ps_consts_i;
4071 memcpy(constants, &src[start_idx], count * sizeof(*src));
4072 wined3d_mutex_unlock();
4074 return D3D_OK;
4077 static HRESULT WINAPI d3d9_device_SetPixelShaderConstantB(IDirect3DDevice9Ex *iface,
4078 UINT reg_idx, const BOOL *data, UINT count)
4080 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
4081 HRESULT hr;
4083 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
4085 wined3d_mutex_lock();
4086 hr = wined3d_stateblock_set_ps_consts_b(device->update_state, reg_idx, count, data);
4087 wined3d_mutex_unlock();
4089 return hr;
4092 static HRESULT WINAPI d3d9_device_GetPixelShaderConstantB(IDirect3DDevice9Ex *iface,
4093 UINT start_idx, BOOL *constants, UINT count)
4095 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
4097 TRACE("iface %p, start_idx %u, constants %p, count %u.\n", iface, start_idx, constants, count);
4099 if (!constants || start_idx >= WINED3D_MAX_CONSTS_I)
4100 return WINED3DERR_INVALIDCALL;
4101 if (count > WINED3D_MAX_CONSTS_I - start_idx)
4102 count = WINED3D_MAX_CONSTS_I - start_idx;
4104 wined3d_mutex_lock();
4105 memcpy(constants, &device->stateblock_state->ps_consts_b[start_idx], count * sizeof(*constants));
4106 wined3d_mutex_unlock();
4108 return D3D_OK;
4111 static HRESULT WINAPI d3d9_device_DrawRectPatch(IDirect3DDevice9Ex *iface, UINT handle,
4112 const float *segment_count, const D3DRECTPATCH_INFO *patch_info)
4114 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
4115 iface, handle, segment_count, patch_info);
4116 return D3D_OK;
4119 static HRESULT WINAPI d3d9_device_DrawTriPatch(IDirect3DDevice9Ex *iface, UINT handle,
4120 const float *segment_count, const D3DTRIPATCH_INFO *patch_info)
4122 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
4123 iface, handle, segment_count, patch_info);
4124 return D3D_OK;
4127 static HRESULT WINAPI d3d9_device_DeletePatch(IDirect3DDevice9Ex *iface, UINT handle)
4129 FIXME("iface %p, handle %#x unimplemented.\n", iface, handle);
4130 return D3DERR_INVALIDCALL;
4133 static HRESULT WINAPI d3d9_device_CreateQuery(IDirect3DDevice9Ex *iface, D3DQUERYTYPE type, IDirect3DQuery9 **query)
4135 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
4136 struct d3d9_query *object;
4137 HRESULT hr;
4139 TRACE("iface %p, type %#x, query %p.\n", iface, type, query);
4141 if (!(object = heap_alloc_zero(sizeof(*object))))
4142 return E_OUTOFMEMORY;
4144 hr = query_init(object, device, type);
4145 if (FAILED(hr))
4147 WARN("Failed to initialize query, hr %#x.\n", hr);
4148 heap_free(object);
4149 return hr;
4152 TRACE("Created query %p.\n", object);
4153 if (query) *query = &object->IDirect3DQuery9_iface;
4154 else IDirect3DQuery9_Release(&object->IDirect3DQuery9_iface);
4156 return D3D_OK;
4159 static HRESULT WINAPI d3d9_device_SetConvolutionMonoKernel(IDirect3DDevice9Ex *iface,
4160 UINT width, UINT height, float *rows, float *columns)
4162 FIXME("iface %p, width %u, height %u, rows %p, columns %p stub!\n",
4163 iface, width, height, rows, columns);
4165 return E_NOTIMPL;
4168 static HRESULT WINAPI d3d9_device_ComposeRects(IDirect3DDevice9Ex *iface,
4169 IDirect3DSurface9 *src_surface, IDirect3DSurface9 *dst_surface, IDirect3DVertexBuffer9 *src_descs,
4170 UINT rect_count, IDirect3DVertexBuffer9 *dst_descs, D3DCOMPOSERECTSOP operation, INT offset_x, INT offset_y)
4172 FIXME("iface %p, src_surface %p, dst_surface %p, src_descs %p, rect_count %u, "
4173 "dst_descs %p, operation %#x, offset_x %u, offset_y %u stub!\n",
4174 iface, src_surface, dst_surface, src_descs, rect_count,
4175 dst_descs, operation, offset_x, offset_y);
4177 return E_NOTIMPL;
4180 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_PresentEx(IDirect3DDevice9Ex *iface,
4181 const RECT *src_rect, const RECT *dst_rect, HWND dst_window_override,
4182 const RGNDATA *dirty_region, DWORD flags)
4184 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
4185 struct d3d9_swapchain *swapchain;
4186 unsigned int i;
4187 HRESULT hr;
4189 TRACE("iface %p, src_rect %s, dst_rect %s, dst_window_override %p, dirty_region %p, flags %#x.\n",
4190 iface, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect),
4191 dst_window_override, dirty_region, flags);
4193 if (device->device_state != D3D9_DEVICE_STATE_OK)
4194 return S_PRESENT_OCCLUDED;
4196 if (dirty_region)
4197 FIXME("Ignoring dirty_region %p.\n", dirty_region);
4199 wined3d_mutex_lock();
4200 for (i = 0; i < device->implicit_swapchain_count; ++i)
4202 swapchain = wined3d_swapchain_get_parent(device->implicit_swapchains[i]);
4203 if (FAILED(hr = wined3d_swapchain_present(swapchain->wined3d_swapchain,
4204 src_rect, dst_rect, dst_window_override, swapchain->swap_interval, flags)))
4206 wined3d_mutex_unlock();
4207 return hr;
4210 wined3d_mutex_unlock();
4212 return D3D_OK;
4215 static HRESULT WINAPI d3d9_device_GetGPUThreadPriority(IDirect3DDevice9Ex *iface, INT *priority)
4217 FIXME("iface %p, priority %p stub!\n", iface, priority);
4219 return E_NOTIMPL;
4222 static HRESULT WINAPI d3d9_device_SetGPUThreadPriority(IDirect3DDevice9Ex *iface, INT priority)
4224 FIXME("iface %p, priority %d stub!\n", iface, priority);
4226 return E_NOTIMPL;
4229 static HRESULT WINAPI d3d9_device_WaitForVBlank(IDirect3DDevice9Ex *iface, UINT swapchain_idx)
4231 FIXME("iface %p, swapchain_idx %u stub!\n", iface, swapchain_idx);
4233 return E_NOTIMPL;
4236 static HRESULT WINAPI d3d9_device_CheckResourceResidency(IDirect3DDevice9Ex *iface,
4237 IDirect3DResource9 **resources, UINT32 resource_count)
4239 FIXME("iface %p, resources %p, resource_count %u stub!\n",
4240 iface, resources, resource_count);
4242 return E_NOTIMPL;
4245 static HRESULT WINAPI d3d9_device_SetMaximumFrameLatency(IDirect3DDevice9Ex *iface, UINT max_latency)
4247 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
4249 TRACE("iface %p, max_latency %u.\n", iface, max_latency);
4251 if (max_latency > 30)
4252 return D3DERR_INVALIDCALL;
4254 wined3d_mutex_lock();
4255 wined3d_device_set_max_frame_latency(device->wined3d_device, max_latency);
4256 wined3d_mutex_unlock();
4258 return S_OK;
4261 static HRESULT WINAPI d3d9_device_GetMaximumFrameLatency(IDirect3DDevice9Ex *iface, UINT *max_latency)
4263 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
4265 TRACE("iface %p, max_latency %p.\n", iface, max_latency);
4267 wined3d_mutex_lock();
4268 *max_latency = wined3d_device_get_max_frame_latency(device->wined3d_device);
4269 wined3d_mutex_unlock();
4271 return S_OK;
4274 static HRESULT WINAPI d3d9_device_CheckDeviceState(IDirect3DDevice9Ex *iface, HWND dst_window)
4276 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
4277 struct wined3d_swapchain_desc swapchain_desc;
4279 TRACE("iface %p, dst_window %p.\n", iface, dst_window);
4281 wined3d_mutex_lock();
4282 wined3d_swapchain_get_desc(device->implicit_swapchains[0], &swapchain_desc);
4283 wined3d_mutex_unlock();
4285 if (swapchain_desc.windowed)
4286 return D3D_OK;
4288 /* FIXME: This is actually supposed to check if any other device is in
4289 * fullscreen mode. */
4290 if (dst_window != swapchain_desc.device_window)
4291 return device->device_state == D3D9_DEVICE_STATE_OK ? S_PRESENT_OCCLUDED : D3D_OK;
4293 return device->device_state == D3D9_DEVICE_STATE_OK ? D3D_OK : S_PRESENT_OCCLUDED;
4296 static HRESULT WINAPI d3d9_device_CreateRenderTargetEx(IDirect3DDevice9Ex *iface,
4297 UINT width, UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
4298 BOOL lockable, IDirect3DSurface9 **surface, HANDLE *shared_handle, DWORD usage)
4300 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
4301 unsigned int access = WINED3D_RESOURCE_ACCESS_GPU;
4303 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u, "
4304 "lockable %#x, surface %p, shared_handle %p, usage %#x.\n",
4305 iface, width, height, format, multisample_type, multisample_quality,
4306 lockable, surface, shared_handle, usage);
4308 *surface = NULL;
4310 if (usage & (D3DUSAGE_DEPTHSTENCIL | D3DUSAGE_RENDERTARGET))
4312 WARN("Invalid usage %#x.\n", usage);
4313 return D3DERR_INVALIDCALL;
4316 if (shared_handle)
4317 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
4319 if (lockable)
4320 access |= WINED3D_RESOURCE_ACCESS_MAP_R | WINED3D_RESOURCE_ACCESS_MAP_W;
4322 return d3d9_device_create_surface(device, 0, wined3dformat_from_d3dformat(format),
4323 wined3d_multisample_type_from_d3d(multisample_type), multisample_quality,
4324 usage & WINED3DUSAGE_MASK, WINED3D_BIND_RENDER_TARGET, access, width, height, NULL, surface);
4327 static HRESULT WINAPI d3d9_device_CreateOffscreenPlainSurfaceEx(IDirect3DDevice9Ex *iface,
4328 UINT width, UINT height, D3DFORMAT format, D3DPOOL pool, IDirect3DSurface9 **surface,
4329 HANDLE *shared_handle, DWORD usage)
4331 FIXME("iface %p, width %u, height %u, format %#x, pool %#x, surface %p, shared_handle %p, usage %#x stub!\n",
4332 iface, width, height, format, pool, surface, shared_handle, usage);
4334 return E_NOTIMPL;
4337 static HRESULT WINAPI d3d9_device_CreateDepthStencilSurfaceEx(IDirect3DDevice9Ex *iface,
4338 UINT width, UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
4339 BOOL discard, IDirect3DSurface9 **surface, HANDLE *shared_handle, DWORD usage)
4341 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
4342 DWORD flags = 0;
4344 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u, "
4345 "discard %#x, surface %p, shared_handle %p, usage %#x.\n",
4346 iface, width, height, format, multisample_type, multisample_quality,
4347 discard, surface, shared_handle, usage);
4349 if (usage & (D3DUSAGE_DEPTHSTENCIL | D3DUSAGE_RENDERTARGET))
4351 WARN("Invalid usage %#x.\n", usage);
4352 return D3DERR_INVALIDCALL;
4355 if (shared_handle)
4356 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
4358 if (discard)
4359 flags |= WINED3D_TEXTURE_CREATE_DISCARD;
4361 *surface = NULL;
4362 return d3d9_device_create_surface(device, flags, wined3dformat_from_d3dformat(format),
4363 wined3d_multisample_type_from_d3d(multisample_type), multisample_quality, usage & WINED3DUSAGE_MASK,
4364 WINED3D_BIND_DEPTH_STENCIL, WINED3D_RESOURCE_ACCESS_GPU, width, height, NULL, surface);
4367 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_ResetEx(IDirect3DDevice9Ex *iface,
4368 D3DPRESENT_PARAMETERS *present_parameters, D3DDISPLAYMODEEX *mode)
4370 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
4372 TRACE("iface %p, present_parameters %p, mode %p.\n", iface, present_parameters, mode);
4374 if (!present_parameters->Windowed == !mode)
4376 WARN("Mode can be passed if and only if Windowed is FALSE.\n");
4377 return D3DERR_INVALIDCALL;
4380 if (mode && (mode->Width != present_parameters->BackBufferWidth
4381 || mode->Height != present_parameters->BackBufferHeight))
4383 WARN("Mode and back buffer mismatch (mode %ux%u, backbuffer %ux%u).\n",
4384 mode->Width, mode->Height,
4385 present_parameters->BackBufferWidth, present_parameters->BackBufferHeight);
4386 return D3DERR_INVALIDCALL;
4389 return d3d9_device_reset(device, present_parameters, mode);
4392 static HRESULT WINAPI d3d9_device_GetDisplayModeEx(IDirect3DDevice9Ex *iface,
4393 UINT swapchain_idx, D3DDISPLAYMODEEX *mode, D3DDISPLAYROTATION *rotation)
4395 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
4396 struct wined3d_display_mode wined3d_mode;
4397 HRESULT hr;
4399 TRACE("iface %p, swapchain_idx %u, mode %p, rotation %p.\n",
4400 iface, swapchain_idx, mode, rotation);
4402 if (mode->Size != sizeof(*mode))
4403 return D3DERR_INVALIDCALL;
4405 wined3d_mutex_lock();
4406 hr = wined3d_device_get_display_mode(device->wined3d_device, swapchain_idx, &wined3d_mode,
4407 (enum wined3d_display_rotation *)rotation);
4408 wined3d_mutex_unlock();
4410 if (SUCCEEDED(hr))
4412 mode->Width = wined3d_mode.width;
4413 mode->Height = wined3d_mode.height;
4414 mode->RefreshRate = wined3d_mode.refresh_rate;
4415 mode->Format = d3dformat_from_wined3dformat(wined3d_mode.format_id);
4416 mode->ScanLineOrdering = d3dscanlineordering_from_wined3d(wined3d_mode.scanline_ordering);
4419 return hr;
4422 static const struct IDirect3DDevice9ExVtbl d3d9_device_vtbl =
4424 /* IUnknown */
4425 d3d9_device_QueryInterface,
4426 d3d9_device_AddRef,
4427 d3d9_device_Release,
4428 /* IDirect3DDevice9 */
4429 d3d9_device_TestCooperativeLevel,
4430 d3d9_device_GetAvailableTextureMem,
4431 d3d9_device_EvictManagedResources,
4432 d3d9_device_GetDirect3D,
4433 d3d9_device_GetDeviceCaps,
4434 d3d9_device_GetDisplayMode,
4435 d3d9_device_GetCreationParameters,
4436 d3d9_device_SetCursorProperties,
4437 d3d9_device_SetCursorPosition,
4438 d3d9_device_ShowCursor,
4439 d3d9_device_CreateAdditionalSwapChain,
4440 d3d9_device_GetSwapChain,
4441 d3d9_device_GetNumberOfSwapChains,
4442 d3d9_device_Reset,
4443 d3d9_device_Present,
4444 d3d9_device_GetBackBuffer,
4445 d3d9_device_GetRasterStatus,
4446 d3d9_device_SetDialogBoxMode,
4447 d3d9_device_SetGammaRamp,
4448 d3d9_device_GetGammaRamp,
4449 d3d9_device_CreateTexture,
4450 d3d9_device_CreateVolumeTexture,
4451 d3d9_device_CreateCubeTexture,
4452 d3d9_device_CreateVertexBuffer,
4453 d3d9_device_CreateIndexBuffer,
4454 d3d9_device_CreateRenderTarget,
4455 d3d9_device_CreateDepthStencilSurface,
4456 d3d9_device_UpdateSurface,
4457 d3d9_device_UpdateTexture,
4458 d3d9_device_GetRenderTargetData,
4459 d3d9_device_GetFrontBufferData,
4460 d3d9_device_StretchRect,
4461 d3d9_device_ColorFill,
4462 d3d9_device_CreateOffscreenPlainSurface,
4463 d3d9_device_SetRenderTarget,
4464 d3d9_device_GetRenderTarget,
4465 d3d9_device_SetDepthStencilSurface,
4466 d3d9_device_GetDepthStencilSurface,
4467 d3d9_device_BeginScene,
4468 d3d9_device_EndScene,
4469 d3d9_device_Clear,
4470 d3d9_device_SetTransform,
4471 d3d9_device_GetTransform,
4472 d3d9_device_MultiplyTransform,
4473 d3d9_device_SetViewport,
4474 d3d9_device_GetViewport,
4475 d3d9_device_SetMaterial,
4476 d3d9_device_GetMaterial,
4477 d3d9_device_SetLight,
4478 d3d9_device_GetLight,
4479 d3d9_device_LightEnable,
4480 d3d9_device_GetLightEnable,
4481 d3d9_device_SetClipPlane,
4482 d3d9_device_GetClipPlane,
4483 d3d9_device_SetRenderState,
4484 d3d9_device_GetRenderState,
4485 d3d9_device_CreateStateBlock,
4486 d3d9_device_BeginStateBlock,
4487 d3d9_device_EndStateBlock,
4488 d3d9_device_SetClipStatus,
4489 d3d9_device_GetClipStatus,
4490 d3d9_device_GetTexture,
4491 d3d9_device_SetTexture,
4492 d3d9_device_GetTextureStageState,
4493 d3d9_device_SetTextureStageState,
4494 d3d9_device_GetSamplerState,
4495 d3d9_device_SetSamplerState,
4496 d3d9_device_ValidateDevice,
4497 d3d9_device_SetPaletteEntries,
4498 d3d9_device_GetPaletteEntries,
4499 d3d9_device_SetCurrentTexturePalette,
4500 d3d9_device_GetCurrentTexturePalette,
4501 d3d9_device_SetScissorRect,
4502 d3d9_device_GetScissorRect,
4503 d3d9_device_SetSoftwareVertexProcessing,
4504 d3d9_device_GetSoftwareVertexProcessing,
4505 d3d9_device_SetNPatchMode,
4506 d3d9_device_GetNPatchMode,
4507 d3d9_device_DrawPrimitive,
4508 d3d9_device_DrawIndexedPrimitive,
4509 d3d9_device_DrawPrimitiveUP,
4510 d3d9_device_DrawIndexedPrimitiveUP,
4511 d3d9_device_ProcessVertices,
4512 d3d9_device_CreateVertexDeclaration,
4513 d3d9_device_SetVertexDeclaration,
4514 d3d9_device_GetVertexDeclaration,
4515 d3d9_device_SetFVF,
4516 d3d9_device_GetFVF,
4517 d3d9_device_CreateVertexShader,
4518 d3d9_device_SetVertexShader,
4519 d3d9_device_GetVertexShader,
4520 d3d9_device_SetVertexShaderConstantF,
4521 d3d9_device_GetVertexShaderConstantF,
4522 d3d9_device_SetVertexShaderConstantI,
4523 d3d9_device_GetVertexShaderConstantI,
4524 d3d9_device_SetVertexShaderConstantB,
4525 d3d9_device_GetVertexShaderConstantB,
4526 d3d9_device_SetStreamSource,
4527 d3d9_device_GetStreamSource,
4528 d3d9_device_SetStreamSourceFreq,
4529 d3d9_device_GetStreamSourceFreq,
4530 d3d9_device_SetIndices,
4531 d3d9_device_GetIndices,
4532 d3d9_device_CreatePixelShader,
4533 d3d9_device_SetPixelShader,
4534 d3d9_device_GetPixelShader,
4535 d3d9_device_SetPixelShaderConstantF,
4536 d3d9_device_GetPixelShaderConstantF,
4537 d3d9_device_SetPixelShaderConstantI,
4538 d3d9_device_GetPixelShaderConstantI,
4539 d3d9_device_SetPixelShaderConstantB,
4540 d3d9_device_GetPixelShaderConstantB,
4541 d3d9_device_DrawRectPatch,
4542 d3d9_device_DrawTriPatch,
4543 d3d9_device_DeletePatch,
4544 d3d9_device_CreateQuery,
4545 /* IDirect3DDevice9Ex */
4546 d3d9_device_SetConvolutionMonoKernel,
4547 d3d9_device_ComposeRects,
4548 d3d9_device_PresentEx,
4549 d3d9_device_GetGPUThreadPriority,
4550 d3d9_device_SetGPUThreadPriority,
4551 d3d9_device_WaitForVBlank,
4552 d3d9_device_CheckResourceResidency,
4553 d3d9_device_SetMaximumFrameLatency,
4554 d3d9_device_GetMaximumFrameLatency,
4555 d3d9_device_CheckDeviceState,
4556 d3d9_device_CreateRenderTargetEx,
4557 d3d9_device_CreateOffscreenPlainSurfaceEx,
4558 d3d9_device_CreateDepthStencilSurfaceEx,
4559 d3d9_device_ResetEx,
4560 d3d9_device_GetDisplayModeEx,
4563 static inline struct d3d9_device *device_from_device_parent(struct wined3d_device_parent *device_parent)
4565 return CONTAINING_RECORD(device_parent, struct d3d9_device, device_parent);
4568 static void CDECL device_parent_wined3d_device_created(struct wined3d_device_parent *device_parent,
4569 struct wined3d_device *device)
4571 TRACE("device_parent %p, device %p.\n", device_parent, device);
4574 static void CDECL device_parent_mode_changed(struct wined3d_device_parent *device_parent)
4576 TRACE("device_parent %p.\n", device_parent);
4579 static void CDECL device_parent_activate(struct wined3d_device_parent *device_parent, BOOL activate)
4581 struct d3d9_device *device = device_from_device_parent(device_parent);
4583 TRACE("device_parent %p, activate %#x.\n", device_parent, activate);
4585 if (!device->d3d_parent)
4586 return;
4588 if (!activate)
4589 InterlockedCompareExchange(&device->device_state, D3D9_DEVICE_STATE_LOST, D3D9_DEVICE_STATE_OK);
4590 else if (device->d3d_parent->extended)
4591 InterlockedCompareExchange(&device->device_state, D3D9_DEVICE_STATE_OK, D3D9_DEVICE_STATE_LOST);
4592 else
4593 InterlockedCompareExchange(&device->device_state, D3D9_DEVICE_STATE_NOT_RESET, D3D9_DEVICE_STATE_LOST);
4596 static HRESULT CDECL device_parent_texture_sub_resource_created(struct wined3d_device_parent *device_parent,
4597 enum wined3d_resource_type type, struct wined3d_texture *wined3d_texture, unsigned int sub_resource_idx,
4598 void **parent, const struct wined3d_parent_ops **parent_ops)
4600 TRACE("device_parent %p, type %#x, wined3d_texture %p, sub_resource_idx %u, parent %p, parent_ops %p.\n",
4601 device_parent, type, wined3d_texture, sub_resource_idx, parent, parent_ops);
4603 if (type == WINED3D_RTYPE_TEXTURE_2D)
4605 struct d3d9_surface *d3d_surface;
4607 if (!(d3d_surface = heap_alloc_zero(sizeof(*d3d_surface))))
4608 return E_OUTOFMEMORY;
4610 surface_init(d3d_surface, wined3d_texture, sub_resource_idx, parent_ops);
4611 *parent = d3d_surface;
4612 TRACE("Created surface %p.\n", d3d_surface);
4614 else if (type == WINED3D_RTYPE_TEXTURE_3D)
4616 struct d3d9_volume *d3d_volume;
4618 if (!(d3d_volume = heap_alloc_zero(sizeof(*d3d_volume))))
4619 return E_OUTOFMEMORY;
4621 volume_init(d3d_volume, wined3d_texture, sub_resource_idx, parent_ops);
4622 *parent = d3d_volume;
4623 TRACE("Created volume %p.\n", d3d_volume);
4625 else
4627 ERR("Unhandled resource type %#x.\n", type);
4628 return E_FAIL;
4631 return D3D_OK;
4634 static HRESULT CDECL device_parent_create_swapchain_texture(struct wined3d_device_parent *device_parent,
4635 void *container_parent, const struct wined3d_resource_desc *desc, DWORD texture_flags,
4636 struct wined3d_texture **texture)
4638 struct d3d9_device *device = device_from_device_parent(device_parent);
4639 struct d3d9_surface *d3d_surface;
4640 HRESULT hr;
4642 TRACE("device_parent %p, container_parent %p, desc %p, texture flags %#x, texture %p.\n",
4643 device_parent, container_parent, desc, texture_flags, texture);
4645 if (container_parent == device_parent)
4646 container_parent = &device->IDirect3DDevice9Ex_iface;
4648 if (FAILED(hr = wined3d_texture_create(device->wined3d_device, desc, 1, 1,
4649 texture_flags, NULL, container_parent, &d3d9_null_wined3d_parent_ops, texture)))
4651 WARN("Failed to create texture, hr %#x.\n", hr);
4652 return hr;
4655 d3d_surface = wined3d_texture_get_sub_resource_parent(*texture, 0);
4656 d3d_surface->parent_device = &device->IDirect3DDevice9Ex_iface;
4658 return hr;
4661 static const struct wined3d_device_parent_ops d3d9_wined3d_device_parent_ops =
4663 device_parent_wined3d_device_created,
4664 device_parent_mode_changed,
4665 device_parent_activate,
4666 device_parent_texture_sub_resource_created,
4667 device_parent_create_swapchain_texture,
4670 static void setup_fpu(void)
4672 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
4673 WORD cw;
4674 __asm__ volatile ("fnstcw %0" : "=m" (cw));
4675 cw = (cw & ~0xf3f) | 0x3f;
4676 __asm__ volatile ("fldcw %0" : : "m" (cw));
4677 #elif defined(__i386__) && defined(_MSC_VER)
4678 WORD cw;
4679 __asm fnstcw cw;
4680 cw = (cw & ~0xf3f) | 0x3f;
4681 __asm fldcw cw;
4682 #else
4683 FIXME("FPU setup not implemented for this platform.\n");
4684 #endif
4687 HRESULT device_init(struct d3d9_device *device, struct d3d9 *parent, struct wined3d *wined3d,
4688 UINT adapter, D3DDEVTYPE device_type, HWND focus_window, DWORD flags,
4689 D3DPRESENT_PARAMETERS *parameters, D3DDISPLAYMODEEX *mode)
4691 struct wined3d_swapchain_desc *swapchain_desc;
4692 struct wined3d_adapter *wined3d_adapter;
4693 struct d3d9_swapchain *d3d_swapchain;
4694 struct wined3d_caps wined3d_caps;
4695 unsigned int output_idx;
4696 unsigned i, count = 1;
4697 D3DCAPS9 caps;
4698 HRESULT hr;
4700 static const enum wined3d_feature_level feature_levels[] =
4702 WINED3D_FEATURE_LEVEL_9_3,
4703 WINED3D_FEATURE_LEVEL_9_2,
4704 WINED3D_FEATURE_LEVEL_9_1,
4705 WINED3D_FEATURE_LEVEL_8,
4706 WINED3D_FEATURE_LEVEL_7,
4707 WINED3D_FEATURE_LEVEL_6,
4708 WINED3D_FEATURE_LEVEL_5,
4711 output_idx = adapter;
4712 if (output_idx >= parent->wined3d_output_count)
4713 return D3DERR_INVALIDCALL;
4715 if (mode)
4716 FIXME("Ignoring display mode.\n");
4718 device->IDirect3DDevice9Ex_iface.lpVtbl = &d3d9_device_vtbl;
4719 device->device_parent.ops = &d3d9_wined3d_device_parent_ops;
4720 device->adapter_ordinal = adapter;
4721 device->refcount = 1;
4723 if (!(flags & D3DCREATE_FPU_PRESERVE)) setup_fpu();
4725 wined3d_mutex_lock();
4726 wined3d_adapter = wined3d_output_get_adapter(parent->wined3d_outputs[output_idx]);
4727 if (FAILED(hr = wined3d_device_create(wined3d, wined3d_adapter, wined3d_device_type_from_d3d(device_type),
4728 focus_window, flags, 4, feature_levels, ARRAY_SIZE(feature_levels),
4729 &device->device_parent, &device->wined3d_device)))
4731 WARN("Failed to create wined3d device, hr %#x.\n", hr);
4732 wined3d_mutex_unlock();
4733 return hr;
4736 device->immediate_context = wined3d_device_get_immediate_context(device->wined3d_device);
4737 wined3d_get_device_caps(wined3d_adapter, wined3d_device_type_from_d3d(device_type), &wined3d_caps);
4738 d3d9_caps_from_wined3dcaps(parent, adapter, &caps, &wined3d_caps);
4739 device->max_user_clip_planes = caps.MaxUserClipPlanes;
4740 device->vs_uniform_count = caps.MaxVertexShaderConst;
4741 if (flags & D3DCREATE_ADAPTERGROUP_DEVICE)
4742 count = caps.NumberOfAdaptersInGroup;
4744 if (FAILED(hr = wined3d_stateblock_create(device->wined3d_device, NULL, WINED3D_SBT_PRIMARY, &device->state)))
4746 ERR("Failed to create the primary stateblock, hr %#x.\n", hr);
4747 wined3d_device_decref(device->wined3d_device);
4748 wined3d_mutex_unlock();
4749 return hr;
4751 device->stateblock_state = wined3d_stateblock_get_state(device->state);
4752 device->update_state = device->state;
4754 if (flags & D3DCREATE_MULTITHREADED)
4755 wined3d_device_set_multithreaded(device->wined3d_device);
4757 if (!parameters->Windowed)
4759 if (!focus_window)
4760 focus_window = parameters->hDeviceWindow;
4761 if (FAILED(hr = wined3d_device_acquire_focus_window(device->wined3d_device, focus_window)))
4763 ERR("Failed to acquire focus window, hr %#x.\n", hr);
4764 wined3d_device_decref(device->wined3d_device);
4765 wined3d_mutex_unlock();
4766 return hr;
4770 if (!(swapchain_desc = heap_alloc(sizeof(*swapchain_desc) * count)))
4772 ERR("Failed to allocate wined3d parameters.\n");
4773 wined3d_device_release_focus_window(device->wined3d_device);
4774 wined3d_device_decref(device->wined3d_device);
4775 wined3d_mutex_unlock();
4776 return E_OUTOFMEMORY;
4779 for (i = 0; i < count; ++i)
4781 if (!wined3d_swapchain_desc_from_d3d9(&swapchain_desc[i],
4782 parent->wined3d_outputs[output_idx + i], &parameters[i], parent->extended))
4784 wined3d_device_release_focus_window(device->wined3d_device);
4785 wined3d_device_decref(device->wined3d_device);
4786 heap_free(swapchain_desc);
4787 wined3d_mutex_unlock();
4788 return D3DERR_INVALIDCALL;
4790 swapchain_desc[i].flags |= WINED3D_SWAPCHAIN_IMPLICIT;
4791 if (flags & D3DCREATE_NOWINDOWCHANGES)
4792 swapchain_desc[i].flags |= WINED3D_SWAPCHAIN_NO_WINDOW_CHANGES;
4795 if (FAILED(hr = d3d9_swapchain_create(device, swapchain_desc,
4796 wined3dswapinterval_from_d3d(parameters->PresentationInterval), &d3d_swapchain)))
4798 WARN("Failed to create swapchain, hr %#x.\n", hr);
4799 wined3d_device_release_focus_window(device->wined3d_device);
4800 heap_free(swapchain_desc);
4801 wined3d_device_decref(device->wined3d_device);
4802 wined3d_mutex_unlock();
4803 return hr;
4806 wined3d_swapchain_incref(d3d_swapchain->wined3d_swapchain);
4807 IDirect3DSwapChain9Ex_Release(&d3d_swapchain->IDirect3DSwapChain9Ex_iface);
4809 wined3d_stateblock_set_render_state(device->state, WINED3D_RS_ZENABLE,
4810 !!swapchain_desc->enable_auto_depth_stencil);
4811 device_reset_viewport_state(device);
4813 if (FAILED(hr = d3d9_device_get_swapchains(device)))
4815 wined3d_swapchain_decref(d3d_swapchain->wined3d_swapchain);
4816 wined3d_device_release_focus_window(device->wined3d_device);
4817 wined3d_device_decref(device->wined3d_device);
4818 heap_free(swapchain_desc);
4819 wined3d_mutex_unlock();
4820 return E_OUTOFMEMORY;
4823 for (i = 0; i < count; ++i)
4825 present_parameters_from_wined3d_swapchain_desc(&parameters[i],
4826 &swapchain_desc[i], parameters[i].PresentationInterval);
4829 wined3d_mutex_unlock();
4831 heap_free(swapchain_desc);
4833 /* Initialize the converted declaration array. This creates a valid pointer
4834 * and when adding decls HeapReAlloc() can be used without further checking. */
4835 if (!(device->fvf_decls = heap_alloc(0)))
4837 ERR("Failed to allocate FVF vertex declaration map memory.\n");
4838 wined3d_mutex_lock();
4839 heap_free(device->implicit_swapchains);
4840 wined3d_swapchain_decref(d3d_swapchain->wined3d_swapchain);
4841 wined3d_device_release_focus_window(device->wined3d_device);
4842 wined3d_device_decref(device->wined3d_device);
4843 wined3d_mutex_unlock();
4844 return E_OUTOFMEMORY;
4847 /* We could also simply ignore the initial rendertarget since it's known
4848 * not to be a texture (we currently use these only for automatic mipmap
4849 * generation). */
4850 wined3d_mutex_lock();
4851 device->render_targets[0] = wined3d_rendertarget_view_get_sub_resource_parent(
4852 wined3d_device_get_rendertarget_view(device->wined3d_device, 0));
4853 wined3d_mutex_unlock();
4855 IDirect3D9Ex_AddRef(&parent->IDirect3D9Ex_iface);
4856 device->d3d_parent = parent;
4858 return D3D_OK;