d3d9: Remove reference to IWineD3DDevice.
[wine.git] / dlls / d3d9 / device.c
blob74070af1303715a3519c7cceb660eb9080f47cea
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 "config.h"
24 #include "d3d9_private.h"
26 WINE_DEFAULT_DEBUG_CHANNEL(d3d9);
28 static void STDMETHODCALLTYPE d3d9_null_wined3d_object_destroyed(void *parent) {}
30 static const struct wined3d_parent_ops d3d9_null_wined3d_parent_ops =
32 d3d9_null_wined3d_object_destroyed,
35 D3DFORMAT d3dformat_from_wined3dformat(enum wined3d_format_id format)
37 BYTE *c = (BYTE *)&format;
39 /* Don't translate FOURCC formats */
40 if (isprint(c[0]) && isprint(c[1]) && isprint(c[2]) && isprint(c[3])) return format;
42 switch(format)
44 case WINED3DFMT_UNKNOWN: return D3DFMT_UNKNOWN;
45 case WINED3DFMT_B8G8R8_UNORM: return D3DFMT_R8G8B8;
46 case WINED3DFMT_B8G8R8A8_UNORM: return D3DFMT_A8R8G8B8;
47 case WINED3DFMT_B8G8R8X8_UNORM: return D3DFMT_X8R8G8B8;
48 case WINED3DFMT_B5G6R5_UNORM: return D3DFMT_R5G6B5;
49 case WINED3DFMT_B5G5R5X1_UNORM: return D3DFMT_X1R5G5B5;
50 case WINED3DFMT_B5G5R5A1_UNORM: return D3DFMT_A1R5G5B5;
51 case WINED3DFMT_B4G4R4A4_UNORM: return D3DFMT_A4R4G4B4;
52 case WINED3DFMT_B2G3R3_UNORM: return D3DFMT_R3G3B2;
53 case WINED3DFMT_A8_UNORM: return D3DFMT_A8;
54 case WINED3DFMT_B2G3R3A8_UNORM: return D3DFMT_A8R3G3B2;
55 case WINED3DFMT_B4G4R4X4_UNORM: return D3DFMT_X4R4G4B4;
56 case WINED3DFMT_R10G10B10A2_UNORM: return D3DFMT_A2B10G10R10;
57 case WINED3DFMT_R8G8B8A8_UNORM: return D3DFMT_A8B8G8R8;
58 case WINED3DFMT_R8G8B8X8_UNORM: return D3DFMT_X8B8G8R8;
59 case WINED3DFMT_R16G16_UNORM: return D3DFMT_G16R16;
60 case WINED3DFMT_B10G10R10A2_UNORM: return D3DFMT_A2R10G10B10;
61 case WINED3DFMT_R16G16B16A16_UNORM: return D3DFMT_A16B16G16R16;
62 case WINED3DFMT_P8_UINT_A8_UNORM: return D3DFMT_A8P8;
63 case WINED3DFMT_P8_UINT: return D3DFMT_P8;
64 case WINED3DFMT_L8_UNORM: return D3DFMT_L8;
65 case WINED3DFMT_L8A8_UNORM: return D3DFMT_A8L8;
66 case WINED3DFMT_L4A4_UNORM: return D3DFMT_A4L4;
67 case WINED3DFMT_R8G8_SNORM: return D3DFMT_V8U8;
68 case WINED3DFMT_R5G5_SNORM_L6_UNORM: return D3DFMT_L6V5U5;
69 case WINED3DFMT_R8G8_SNORM_L8X8_UNORM: return D3DFMT_X8L8V8U8;
70 case WINED3DFMT_R8G8B8A8_SNORM: return D3DFMT_Q8W8V8U8;
71 case WINED3DFMT_R16G16_SNORM: return D3DFMT_V16U16;
72 case WINED3DFMT_R10G10B10_SNORM_A2_UNORM: return D3DFMT_A2W10V10U10;
73 case WINED3DFMT_D16_LOCKABLE: return D3DFMT_D16_LOCKABLE;
74 case WINED3DFMT_D32_UNORM: return D3DFMT_D32;
75 case WINED3DFMT_S1_UINT_D15_UNORM: return D3DFMT_D15S1;
76 case WINED3DFMT_D24_UNORM_S8_UINT: return D3DFMT_D24S8;
77 case WINED3DFMT_X8D24_UNORM: return D3DFMT_D24X8;
78 case WINED3DFMT_S4X4_UINT_D24_UNORM: return D3DFMT_D24X4S4;
79 case WINED3DFMT_D16_UNORM: return D3DFMT_D16;
80 case WINED3DFMT_L16_UNORM: return D3DFMT_L16;
81 case WINED3DFMT_D32_FLOAT: return D3DFMT_D32F_LOCKABLE;
82 case WINED3DFMT_S8_UINT_D24_FLOAT: return D3DFMT_D24FS8;
83 case WINED3DFMT_R16_UINT: return D3DFMT_INDEX16;
84 case WINED3DFMT_R32_UINT: return D3DFMT_INDEX32;
85 case WINED3DFMT_R16G16B16A16_SNORM: return D3DFMT_Q16W16V16U16;
86 case WINED3DFMT_R16_FLOAT: return D3DFMT_R16F;
87 case WINED3DFMT_R16G16_FLOAT: return D3DFMT_G16R16F;
88 case WINED3DFMT_R16G16B16A16_FLOAT: return D3DFMT_A16B16G16R16F;
89 case WINED3DFMT_R32_FLOAT: return D3DFMT_R32F;
90 case WINED3DFMT_R32G32_FLOAT: return D3DFMT_G32R32F;
91 case WINED3DFMT_R32G32B32A32_FLOAT: return D3DFMT_A32B32G32R32F;
92 case WINED3DFMT_R8G8_SNORM_Cx: return D3DFMT_CxV8U8;
93 default:
94 FIXME("Unhandled wined3d format %#x.\n", format);
95 return D3DFMT_UNKNOWN;
99 enum wined3d_format_id wined3dformat_from_d3dformat(D3DFORMAT format)
101 BYTE *c = (BYTE *)&format;
103 /* Don't translate FOURCC formats */
104 if (isprint(c[0]) && isprint(c[1]) && isprint(c[2]) && isprint(c[3])) return format;
106 switch(format)
108 case D3DFMT_UNKNOWN: return WINED3DFMT_UNKNOWN;
109 case D3DFMT_R8G8B8: return WINED3DFMT_B8G8R8_UNORM;
110 case D3DFMT_A8R8G8B8: return WINED3DFMT_B8G8R8A8_UNORM;
111 case D3DFMT_X8R8G8B8: return WINED3DFMT_B8G8R8X8_UNORM;
112 case D3DFMT_R5G6B5: return WINED3DFMT_B5G6R5_UNORM;
113 case D3DFMT_X1R5G5B5: return WINED3DFMT_B5G5R5X1_UNORM;
114 case D3DFMT_A1R5G5B5: return WINED3DFMT_B5G5R5A1_UNORM;
115 case D3DFMT_A4R4G4B4: return WINED3DFMT_B4G4R4A4_UNORM;
116 case D3DFMT_R3G3B2: return WINED3DFMT_B2G3R3_UNORM;
117 case D3DFMT_A8: return WINED3DFMT_A8_UNORM;
118 case D3DFMT_A8R3G3B2: return WINED3DFMT_B2G3R3A8_UNORM;
119 case D3DFMT_X4R4G4B4: return WINED3DFMT_B4G4R4X4_UNORM;
120 case D3DFMT_A2B10G10R10: return WINED3DFMT_R10G10B10A2_UNORM;
121 case D3DFMT_A8B8G8R8: return WINED3DFMT_R8G8B8A8_UNORM;
122 case D3DFMT_X8B8G8R8: return WINED3DFMT_R8G8B8X8_UNORM;
123 case D3DFMT_G16R16: return WINED3DFMT_R16G16_UNORM;
124 case D3DFMT_A2R10G10B10: return WINED3DFMT_B10G10R10A2_UNORM;
125 case D3DFMT_A16B16G16R16: return WINED3DFMT_R16G16B16A16_UNORM;
126 case D3DFMT_A8P8: return WINED3DFMT_P8_UINT_A8_UNORM;
127 case D3DFMT_P8: return WINED3DFMT_P8_UINT;
128 case D3DFMT_L8: return WINED3DFMT_L8_UNORM;
129 case D3DFMT_A8L8: return WINED3DFMT_L8A8_UNORM;
130 case D3DFMT_A4L4: return WINED3DFMT_L4A4_UNORM;
131 case D3DFMT_V8U8: return WINED3DFMT_R8G8_SNORM;
132 case D3DFMT_L6V5U5: return WINED3DFMT_R5G5_SNORM_L6_UNORM;
133 case D3DFMT_X8L8V8U8: return WINED3DFMT_R8G8_SNORM_L8X8_UNORM;
134 case D3DFMT_Q8W8V8U8: return WINED3DFMT_R8G8B8A8_SNORM;
135 case D3DFMT_V16U16: return WINED3DFMT_R16G16_SNORM;
136 case D3DFMT_A2W10V10U10: return WINED3DFMT_R10G10B10_SNORM_A2_UNORM;
137 case D3DFMT_D16_LOCKABLE: return WINED3DFMT_D16_LOCKABLE;
138 case D3DFMT_D32: return WINED3DFMT_D32_UNORM;
139 case D3DFMT_D15S1: return WINED3DFMT_S1_UINT_D15_UNORM;
140 case D3DFMT_D24S8: return WINED3DFMT_D24_UNORM_S8_UINT;
141 case D3DFMT_D24X8: return WINED3DFMT_X8D24_UNORM;
142 case D3DFMT_D24X4S4: return WINED3DFMT_S4X4_UINT_D24_UNORM;
143 case D3DFMT_D16: return WINED3DFMT_D16_UNORM;
144 case D3DFMT_L16: return WINED3DFMT_L16_UNORM;
145 case D3DFMT_D32F_LOCKABLE: return WINED3DFMT_D32_FLOAT;
146 case D3DFMT_D24FS8: return WINED3DFMT_S8_UINT_D24_FLOAT;
147 case D3DFMT_INDEX16: return WINED3DFMT_R16_UINT;
148 case D3DFMT_INDEX32: return WINED3DFMT_R32_UINT;
149 case D3DFMT_Q16W16V16U16: return WINED3DFMT_R16G16B16A16_SNORM;
150 case D3DFMT_R16F: return WINED3DFMT_R16_FLOAT;
151 case D3DFMT_G16R16F: return WINED3DFMT_R16G16_FLOAT;
152 case D3DFMT_A16B16G16R16F: return WINED3DFMT_R16G16B16A16_FLOAT;
153 case D3DFMT_R32F: return WINED3DFMT_R32_FLOAT;
154 case D3DFMT_G32R32F: return WINED3DFMT_R32G32_FLOAT;
155 case D3DFMT_A32B32G32R32F: return WINED3DFMT_R32G32B32A32_FLOAT;
156 case D3DFMT_CxV8U8: return WINED3DFMT_R8G8_SNORM_Cx;
157 default:
158 FIXME("Unhandled D3DFORMAT %#x.\n", format);
159 return WINED3DFMT_UNKNOWN;
163 static UINT vertex_count_from_primitive_count(D3DPRIMITIVETYPE primitive_type, UINT primitive_count)
165 switch(primitive_type)
167 case D3DPT_POINTLIST:
168 return primitive_count;
170 case D3DPT_LINELIST:
171 return primitive_count * 2;
173 case D3DPT_LINESTRIP:
174 return primitive_count + 1;
176 case D3DPT_TRIANGLELIST:
177 return primitive_count * 3;
179 case D3DPT_TRIANGLESTRIP:
180 case D3DPT_TRIANGLEFAN:
181 return primitive_count + 2;
183 default:
184 FIXME("Unhandled primitive type %#x\n", primitive_type);
185 return 0;
189 void present_parameters_from_wined3d_swapchain_desc(D3DPRESENT_PARAMETERS *present_parameters,
190 const struct wined3d_swapchain_desc *swapchain_desc)
192 present_parameters->BackBufferWidth = swapchain_desc->backbuffer_width;
193 present_parameters->BackBufferHeight = swapchain_desc->backbuffer_height;
194 present_parameters->BackBufferFormat = d3dformat_from_wined3dformat(swapchain_desc->backbuffer_format);
195 present_parameters->BackBufferCount = swapchain_desc->backbuffer_count;
196 present_parameters->MultiSampleType = swapchain_desc->multisample_type;
197 present_parameters->MultiSampleQuality = swapchain_desc->multisample_quality;
198 present_parameters->SwapEffect = swapchain_desc->swap_effect;
199 present_parameters->hDeviceWindow = swapchain_desc->device_window;
200 present_parameters->Windowed = swapchain_desc->windowed;
201 present_parameters->EnableAutoDepthStencil = swapchain_desc->enable_auto_depth_stencil;
202 present_parameters->AutoDepthStencilFormat
203 = d3dformat_from_wined3dformat(swapchain_desc->auto_depth_stencil_format);
204 present_parameters->Flags = swapchain_desc->flags & D3DPRESENTFLAGS_MASK;
205 present_parameters->FullScreen_RefreshRateInHz = swapchain_desc->refresh_rate;
206 present_parameters->PresentationInterval = swapchain_desc->swap_interval;
209 static BOOL wined3d_swapchain_desc_from_present_parameters(struct wined3d_swapchain_desc *swapchain_desc,
210 const D3DPRESENT_PARAMETERS *present_parameters, BOOL extended)
212 D3DSWAPEFFECT highest_swapeffect = extended ? D3DSWAPEFFECT_FLIPEX : D3DSWAPEFFECT_COPY;
213 UINT highest_bb_count = extended ? 30 : 3;
215 if (!present_parameters->SwapEffect || present_parameters->SwapEffect > highest_swapeffect)
217 WARN("Invalid swap effect %u passed.\n", present_parameters->SwapEffect);
218 return FALSE;
220 if (present_parameters->BackBufferCount > highest_bb_count
221 || (present_parameters->SwapEffect == D3DSWAPEFFECT_COPY
222 && present_parameters->BackBufferCount > 1))
224 WARN("Invalid backbuffer count %u.\n", present_parameters->BackBufferCount);
225 return FALSE;
228 swapchain_desc->backbuffer_width = present_parameters->BackBufferWidth;
229 swapchain_desc->backbuffer_height = present_parameters->BackBufferHeight;
230 swapchain_desc->backbuffer_format = wined3dformat_from_d3dformat(present_parameters->BackBufferFormat);
231 swapchain_desc->backbuffer_count = max(1, present_parameters->BackBufferCount);
232 swapchain_desc->multisample_type = present_parameters->MultiSampleType;
233 swapchain_desc->multisample_quality = present_parameters->MultiSampleQuality;
234 swapchain_desc->swap_effect = present_parameters->SwapEffect;
235 swapchain_desc->device_window = present_parameters->hDeviceWindow;
236 swapchain_desc->windowed = present_parameters->Windowed;
237 swapchain_desc->enable_auto_depth_stencil = present_parameters->EnableAutoDepthStencil;
238 swapchain_desc->auto_depth_stencil_format
239 = wined3dformat_from_d3dformat(present_parameters->AutoDepthStencilFormat);
240 swapchain_desc->flags
241 = (present_parameters->Flags & D3DPRESENTFLAGS_MASK) | WINED3D_SWAPCHAIN_ALLOW_MODE_SWITCH;
242 if (is_gdi_compat_wined3dformat(swapchain_desc->backbuffer_format))
243 swapchain_desc->flags |= WINED3D_SWAPCHAIN_GDI_COMPATIBLE;
244 swapchain_desc->refresh_rate = present_parameters->FullScreen_RefreshRateInHz;
245 swapchain_desc->swap_interval = present_parameters->PresentationInterval;
246 swapchain_desc->auto_restore_display_mode = TRUE;
248 if (present_parameters->Flags & ~D3DPRESENTFLAGS_MASK)
249 FIXME("Unhandled flags %#x.\n", present_parameters->Flags & ~D3DPRESENTFLAGS_MASK);
251 return TRUE;
254 void d3dcaps_from_wined3dcaps(D3DCAPS9 *caps, const WINED3DCAPS *wined3d_caps)
256 static const DWORD ps_minor_version[] = {0, 4, 0, 0};
257 static const DWORD vs_minor_version[] = {0, 1, 0, 0};
258 static const DWORD texture_filter_caps =
259 D3DPTFILTERCAPS_MINFPOINT | D3DPTFILTERCAPS_MINFLINEAR | D3DPTFILTERCAPS_MINFANISOTROPIC |
260 D3DPTFILTERCAPS_MINFPYRAMIDALQUAD | D3DPTFILTERCAPS_MINFGAUSSIANQUAD|
261 D3DPTFILTERCAPS_MIPFPOINT | D3DPTFILTERCAPS_MIPFLINEAR | D3DPTFILTERCAPS_MAGFPOINT |
262 D3DPTFILTERCAPS_MAGFLINEAR |D3DPTFILTERCAPS_MAGFANISOTROPIC|D3DPTFILTERCAPS_MAGFPYRAMIDALQUAD|
263 D3DPTFILTERCAPS_MAGFGAUSSIANQUAD;
265 caps->DeviceType = (D3DDEVTYPE)wined3d_caps->DeviceType;
266 caps->AdapterOrdinal = wined3d_caps->AdapterOrdinal;
267 caps->Caps = wined3d_caps->Caps;
268 caps->Caps2 = wined3d_caps->Caps2;
269 caps->Caps3 = wined3d_caps->Caps3;
270 caps->PresentationIntervals = wined3d_caps->PresentationIntervals;
271 caps->CursorCaps = wined3d_caps->CursorCaps;
272 caps->DevCaps = wined3d_caps->DevCaps;
273 caps->PrimitiveMiscCaps = wined3d_caps->PrimitiveMiscCaps;
274 caps->RasterCaps = wined3d_caps->RasterCaps;
275 caps->ZCmpCaps = wined3d_caps->ZCmpCaps;
276 caps->SrcBlendCaps = wined3d_caps->SrcBlendCaps;
277 caps->DestBlendCaps = wined3d_caps->DestBlendCaps;
278 caps->AlphaCmpCaps = wined3d_caps->AlphaCmpCaps;
279 caps->ShadeCaps = wined3d_caps->ShadeCaps;
280 caps->TextureCaps = wined3d_caps->TextureCaps;
281 caps->TextureFilterCaps = wined3d_caps->TextureFilterCaps;
282 caps->CubeTextureFilterCaps = wined3d_caps->CubeTextureFilterCaps;
283 caps->VolumeTextureFilterCaps = wined3d_caps->VolumeTextureFilterCaps;
284 caps->TextureAddressCaps = wined3d_caps->TextureAddressCaps;
285 caps->VolumeTextureAddressCaps = wined3d_caps->VolumeTextureAddressCaps;
286 caps->LineCaps = wined3d_caps->LineCaps;
287 caps->MaxTextureWidth = wined3d_caps->MaxTextureWidth;
288 caps->MaxTextureHeight = wined3d_caps->MaxTextureHeight;
289 caps->MaxVolumeExtent = wined3d_caps->MaxVolumeExtent;
290 caps->MaxTextureRepeat = wined3d_caps->MaxTextureRepeat;
291 caps->MaxTextureAspectRatio = wined3d_caps->MaxTextureAspectRatio;
292 caps->MaxAnisotropy = wined3d_caps->MaxAnisotropy;
293 caps->MaxVertexW = wined3d_caps->MaxVertexW;
294 caps->GuardBandLeft = wined3d_caps->GuardBandLeft;
295 caps->GuardBandTop = wined3d_caps->GuardBandTop;
296 caps->GuardBandRight = wined3d_caps->GuardBandRight;
297 caps->GuardBandBottom = wined3d_caps->GuardBandBottom;
298 caps->ExtentsAdjust = wined3d_caps->ExtentsAdjust;
299 caps->StencilCaps = wined3d_caps->StencilCaps;
300 caps->FVFCaps = wined3d_caps->FVFCaps;
301 caps->TextureOpCaps = wined3d_caps->TextureOpCaps;
302 caps->MaxTextureBlendStages = wined3d_caps->MaxTextureBlendStages;
303 caps->MaxSimultaneousTextures = wined3d_caps->MaxSimultaneousTextures;
304 caps->VertexProcessingCaps = wined3d_caps->VertexProcessingCaps;
305 caps->MaxActiveLights = wined3d_caps->MaxActiveLights;
306 caps->MaxUserClipPlanes = wined3d_caps->MaxUserClipPlanes;
307 caps->MaxVertexBlendMatrices = wined3d_caps->MaxVertexBlendMatrices;
308 caps->MaxVertexBlendMatrixIndex = wined3d_caps->MaxVertexBlendMatrixIndex;
309 caps->MaxPointSize = wined3d_caps->MaxPointSize;
310 caps->MaxPrimitiveCount = wined3d_caps->MaxPrimitiveCount;
311 caps->MaxVertexIndex = wined3d_caps->MaxVertexIndex;
312 caps->MaxStreams = wined3d_caps->MaxStreams;
313 caps->MaxStreamStride = wined3d_caps->MaxStreamStride;
314 caps->VertexShaderVersion = wined3d_caps->VertexShaderVersion;
315 caps->MaxVertexShaderConst = wined3d_caps->MaxVertexShaderConst;
316 caps->PixelShaderVersion = wined3d_caps->PixelShaderVersion;
317 caps->PixelShader1xMaxValue = wined3d_caps->PixelShader1xMaxValue;
318 caps->DevCaps2 = wined3d_caps->DevCaps2;
319 caps->MaxNpatchTessellationLevel = wined3d_caps->MaxNpatchTessellationLevel;
320 caps->MasterAdapterOrdinal = wined3d_caps->MasterAdapterOrdinal;
321 caps->AdapterOrdinalInGroup = wined3d_caps->AdapterOrdinalInGroup;
322 caps->NumberOfAdaptersInGroup = wined3d_caps->NumberOfAdaptersInGroup;
323 caps->DeclTypes = wined3d_caps->DeclTypes;
324 caps->NumSimultaneousRTs = wined3d_caps->NumSimultaneousRTs;
325 caps->StretchRectFilterCaps = wined3d_caps->StretchRectFilterCaps;
326 caps->VS20Caps.Caps = wined3d_caps->VS20Caps.caps;
327 caps->VS20Caps.DynamicFlowControlDepth = wined3d_caps->VS20Caps.dynamic_flow_control_depth;
328 caps->VS20Caps.NumTemps = wined3d_caps->VS20Caps.temp_count;
329 caps->VS20Caps.StaticFlowControlDepth = wined3d_caps->VS20Caps.static_flow_control_depth;
330 caps->PS20Caps.Caps = wined3d_caps->PS20Caps.caps;
331 caps->PS20Caps.DynamicFlowControlDepth = wined3d_caps->PS20Caps.dynamic_flow_control_depth;
332 caps->PS20Caps.NumTemps = wined3d_caps->PS20Caps.temp_count;
333 caps->PS20Caps.StaticFlowControlDepth = wined3d_caps->PS20Caps.static_flow_control_depth;
334 caps->PS20Caps.NumInstructionSlots = wined3d_caps->PS20Caps.instruction_slot_count;
335 caps->VertexTextureFilterCaps = wined3d_caps->VertexTextureFilterCaps;
336 caps->MaxVShaderInstructionsExecuted = wined3d_caps->MaxVShaderInstructionsExecuted;
337 caps->MaxPShaderInstructionsExecuted = wined3d_caps->MaxPShaderInstructionsExecuted;
338 caps->MaxVertexShader30InstructionSlots = wined3d_caps->MaxVertexShader30InstructionSlots;
339 caps->MaxPixelShader30InstructionSlots = wined3d_caps->MaxPixelShader30InstructionSlots;
341 /* Some functionality is implemented in d3d9.dll, not wined3d.dll. Add the needed caps. */
342 caps->DevCaps2 |= D3DDEVCAPS2_CAN_STRETCHRECT_FROM_TEXTURES;
344 /* Filter wined3d caps. */
345 caps->TextureFilterCaps &= texture_filter_caps;
346 caps->CubeTextureFilterCaps &= texture_filter_caps;
347 caps->VolumeTextureFilterCaps &= texture_filter_caps;
349 caps->DevCaps &=
350 D3DDEVCAPS_EXECUTESYSTEMMEMORY | D3DDEVCAPS_EXECUTEVIDEOMEMORY | D3DDEVCAPS_TLVERTEXSYSTEMMEMORY |
351 D3DDEVCAPS_TLVERTEXVIDEOMEMORY | D3DDEVCAPS_TEXTURESYSTEMMEMORY| D3DDEVCAPS_TEXTUREVIDEOMEMORY |
352 D3DDEVCAPS_DRAWPRIMTLVERTEX | D3DDEVCAPS_CANRENDERAFTERFLIP | D3DDEVCAPS_TEXTURENONLOCALVIDMEM|
353 D3DDEVCAPS_DRAWPRIMITIVES2 | D3DDEVCAPS_SEPARATETEXTUREMEMORIES |
354 D3DDEVCAPS_DRAWPRIMITIVES2EX | D3DDEVCAPS_HWTRANSFORMANDLIGHT| D3DDEVCAPS_CANBLTSYSTONONLOCAL |
355 D3DDEVCAPS_HWRASTERIZATION | D3DDEVCAPS_PUREDEVICE | D3DDEVCAPS_QUINTICRTPATCHES |
356 D3DDEVCAPS_RTPATCHES | D3DDEVCAPS_RTPATCHHANDLEZERO | D3DDEVCAPS_NPATCHES;
358 caps->ShadeCaps &=
359 D3DPSHADECAPS_COLORGOURAUDRGB | D3DPSHADECAPS_SPECULARGOURAUDRGB |
360 D3DPSHADECAPS_ALPHAGOURAUDBLEND | D3DPSHADECAPS_FOGGOURAUD;
362 caps->RasterCaps &=
363 D3DPRASTERCAPS_DITHER | D3DPRASTERCAPS_ZTEST | D3DPRASTERCAPS_FOGVERTEX |
364 D3DPRASTERCAPS_FOGTABLE | D3DPRASTERCAPS_MIPMAPLODBIAS | D3DPRASTERCAPS_ZBUFFERLESSHSR |
365 D3DPRASTERCAPS_FOGRANGE | D3DPRASTERCAPS_ANISOTROPY | D3DPRASTERCAPS_WBUFFER |
366 D3DPRASTERCAPS_WFOG | D3DPRASTERCAPS_ZFOG | D3DPRASTERCAPS_COLORPERSPECTIVE |
367 D3DPRASTERCAPS_SCISSORTEST | D3DPRASTERCAPS_SLOPESCALEDEPTHBIAS |
368 D3DPRASTERCAPS_DEPTHBIAS | D3DPRASTERCAPS_MULTISAMPLE_TOGGLE;
370 caps->DevCaps2 &=
371 D3DDEVCAPS2_STREAMOFFSET | D3DDEVCAPS2_DMAPNPATCH | D3DDEVCAPS2_ADAPTIVETESSRTPATCH |
372 D3DDEVCAPS2_ADAPTIVETESSNPATCH | D3DDEVCAPS2_CAN_STRETCHRECT_FROM_TEXTURES |
373 D3DDEVCAPS2_PRESAMPLEDDMAPNPATCH| D3DDEVCAPS2_VERTEXELEMENTSCANSHARESTREAMOFFSET;
375 caps->Caps2 &=
376 D3DCAPS2_FULLSCREENGAMMA | D3DCAPS2_CANCALIBRATEGAMMA | D3DCAPS2_RESERVED |
377 D3DCAPS2_CANMANAGERESOURCE | D3DCAPS2_DYNAMICTEXTURES | D3DCAPS2_CANAUTOGENMIPMAP;
379 caps->VertexProcessingCaps &=
380 D3DVTXPCAPS_TEXGEN | D3DVTXPCAPS_MATERIALSOURCE7 | D3DVTXPCAPS_DIRECTIONALLIGHTS |
381 D3DVTXPCAPS_POSITIONALLIGHTS | D3DVTXPCAPS_LOCALVIEWER | D3DVTXPCAPS_TWEENING |
382 D3DVTXPCAPS_TEXGEN_SPHEREMAP | D3DVTXPCAPS_NO_TEXGEN_NONLOCALVIEWER;
384 caps->TextureCaps &=
385 D3DPTEXTURECAPS_PERSPECTIVE | D3DPTEXTURECAPS_POW2 | D3DPTEXTURECAPS_ALPHA |
386 D3DPTEXTURECAPS_SQUAREONLY | D3DPTEXTURECAPS_TEXREPEATNOTSCALEDBYSIZE |
387 D3DPTEXTURECAPS_ALPHAPALETTE | D3DPTEXTURECAPS_NONPOW2CONDITIONAL |
388 D3DPTEXTURECAPS_PROJECTED | D3DPTEXTURECAPS_CUBEMAP | D3DPTEXTURECAPS_VOLUMEMAP |
389 D3DPTEXTURECAPS_MIPMAP | D3DPTEXTURECAPS_MIPVOLUMEMAP | D3DPTEXTURECAPS_MIPCUBEMAP |
390 D3DPTEXTURECAPS_CUBEMAP_POW2 | D3DPTEXTURECAPS_VOLUMEMAP_POW2| D3DPTEXTURECAPS_NOPROJECTEDBUMPENV;
392 caps->MaxVertexShaderConst = min(D3D9_MAX_VERTEX_SHADER_CONSTANTF, caps->MaxVertexShaderConst);
393 caps->NumSimultaneousRTs = min(D3D9_MAX_SIMULTANEOUS_RENDERTARGETS, caps->NumSimultaneousRTs);
395 if (caps->PixelShaderVersion > 3)
397 caps->PixelShaderVersion = D3DPS_VERSION(3, 0);
399 else
401 DWORD major = caps->PixelShaderVersion;
402 caps->PixelShaderVersion = D3DPS_VERSION(major, ps_minor_version[major]);
405 if (caps->VertexShaderVersion > 3)
407 caps->VertexShaderVersion = D3DVS_VERSION(3, 0);
409 else
411 DWORD major = caps->VertexShaderVersion;
412 caps->VertexShaderVersion = D3DVS_VERSION(major, vs_minor_version[major]);
416 static HRESULT WINAPI d3d9_device_QueryInterface(IDirect3DDevice9Ex *iface, REFIID riid, void **out)
418 TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
420 if (IsEqualGUID(riid, &IID_IDirect3DDevice9)
421 || IsEqualGUID(riid, &IID_IUnknown))
423 IDirect3DDevice9Ex_AddRef(iface);
424 *out = iface;
425 return S_OK;
428 if (IsEqualGUID(riid, &IID_IDirect3DDevice9Ex))
430 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
432 /* Find out if the creating d3d9 interface was created with Direct3DCreate9Ex.
433 * It doesn't matter with which function the device was created. */
434 if (!device->d3d_parent->extended)
436 WARN("IDirect3D9 instance wasn't created with CreateDirect3D9Ex, returning E_NOINTERFACE.\n");
437 *out = NULL;
438 return E_NOINTERFACE;
441 IDirect3DDevice9Ex_AddRef(iface);
442 *out = iface;
443 return S_OK;
446 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
448 *out = NULL;
449 return E_NOINTERFACE;
452 static ULONG WINAPI d3d9_device_AddRef(IDirect3DDevice9Ex *iface)
454 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
455 ULONG refcount = InterlockedIncrement(&device->refcount);
457 TRACE("%p increasing refcount to %u.\n", iface, refcount);
459 return refcount;
462 static ULONG WINAPI DECLSPEC_HOTPATCH d3d9_device_Release(IDirect3DDevice9Ex *iface)
464 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
465 ULONG refcount;
467 if (device->in_destruction)
468 return 0;
470 refcount = InterlockedDecrement(&device->refcount);
472 TRACE("%p decreasing refcount to %u.\n", iface, refcount);
474 if (!refcount)
476 unsigned i;
477 device->in_destruction = TRUE;
479 wined3d_mutex_lock();
480 for (i = 0; i < device->fvf_decl_count; ++i)
482 wined3d_vertex_declaration_decref(device->fvf_decls[i].decl);
484 HeapFree(GetProcessHeap(), 0, device->fvf_decls);
486 if (device->vertex_buffer)
487 wined3d_buffer_decref(device->vertex_buffer);
488 if (device->index_buffer)
489 wined3d_buffer_decref(device->index_buffer);
491 HeapFree(GetProcessHeap(), 0, device->implicit_swapchains);
493 wined3d_device_uninit_3d(device->wined3d_device);
494 wined3d_device_release_focus_window(device->wined3d_device);
495 wined3d_device_decref(device->wined3d_device);
496 wined3d_mutex_unlock();
498 IDirect3D9Ex_Release(&device->d3d_parent->IDirect3D9Ex_iface);
500 HeapFree(GetProcessHeap(), 0, device);
503 return refcount;
506 static HRESULT WINAPI d3d9_device_TestCooperativeLevel(IDirect3DDevice9Ex *iface)
508 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
510 TRACE("iface %p.\n", iface);
512 TRACE("device state: %#x.\n", device->device_state);
514 if (device->d3d_parent->extended)
515 return D3D_OK;
517 switch (device->device_state)
519 default:
520 case D3D9_DEVICE_STATE_OK:
521 return D3D_OK;
522 case D3D9_DEVICE_STATE_LOST:
523 return D3DERR_DEVICELOST;
524 case D3D9_DEVICE_STATE_NOT_RESET:
525 return D3DERR_DEVICENOTRESET;
529 static UINT WINAPI d3d9_device_GetAvailableTextureMem(IDirect3DDevice9Ex *iface)
531 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
532 UINT ret;
534 TRACE("iface %p.\n", iface);
536 wined3d_mutex_lock();
537 ret = wined3d_device_get_available_texture_mem(device->wined3d_device);
538 wined3d_mutex_unlock();
540 return ret;
543 static HRESULT WINAPI d3d9_device_EvictManagedResources(IDirect3DDevice9Ex *iface)
545 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
547 TRACE("iface %p.\n", iface);
549 wined3d_mutex_lock();
550 wined3d_device_evict_managed_resources(device->wined3d_device);
551 wined3d_mutex_unlock();
553 return D3D_OK;
556 static HRESULT WINAPI d3d9_device_GetDirect3D(IDirect3DDevice9Ex *iface, IDirect3D9 **d3d9)
558 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
560 TRACE("iface %p, d3d9 %p.\n", iface, d3d9);
562 if (!d3d9)
563 return D3DERR_INVALIDCALL;
565 return IDirect3D9Ex_QueryInterface(&device->d3d_parent->IDirect3D9Ex_iface, &IID_IDirect3D9, (void **)d3d9);
568 static HRESULT WINAPI d3d9_device_GetDeviceCaps(IDirect3DDevice9Ex *iface, D3DCAPS9 *caps)
570 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
571 WINED3DCAPS wined3d_caps;
572 HRESULT hr;
574 TRACE("iface %p, caps %p.\n", iface, caps);
576 if (!caps)
577 return D3DERR_INVALIDCALL;
579 memset(caps, 0, sizeof(*caps));
581 wined3d_mutex_lock();
582 hr = wined3d_device_get_device_caps(device->wined3d_device, &wined3d_caps);
583 wined3d_mutex_unlock();
585 d3dcaps_from_wined3dcaps(caps, &wined3d_caps);
587 return hr;
590 static HRESULT WINAPI d3d9_device_GetDisplayMode(IDirect3DDevice9Ex *iface, UINT swapchain, D3DDISPLAYMODE *mode)
592 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
593 struct wined3d_display_mode wined3d_mode;
594 HRESULT hr;
596 TRACE("iface %p, swapchain %u, mode %p.\n", iface, swapchain, mode);
598 wined3d_mutex_lock();
599 hr = wined3d_device_get_display_mode(device->wined3d_device, swapchain, &wined3d_mode, NULL);
600 wined3d_mutex_unlock();
602 if (SUCCEEDED(hr))
604 mode->Width = wined3d_mode.width;
605 mode->Height = wined3d_mode.height;
606 mode->RefreshRate = wined3d_mode.refresh_rate;
607 mode->Format = d3dformat_from_wined3dformat(wined3d_mode.format_id);
610 return hr;
613 static HRESULT WINAPI d3d9_device_GetCreationParameters(IDirect3DDevice9Ex *iface,
614 D3DDEVICE_CREATION_PARAMETERS *parameters)
616 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
618 TRACE("iface %p, parameters %p.\n", iface, parameters);
620 wined3d_mutex_lock();
621 wined3d_device_get_creation_parameters(device->wined3d_device,
622 (struct wined3d_device_creation_parameters *)parameters);
623 wined3d_mutex_unlock();
625 return D3D_OK;
628 static HRESULT WINAPI d3d9_device_SetCursorProperties(IDirect3DDevice9Ex *iface,
629 UINT hotspot_x, UINT hotspot_y, IDirect3DSurface9 *bitmap)
631 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
632 struct d3d9_surface *bitmap_impl = unsafe_impl_from_IDirect3DSurface9(bitmap);
633 HRESULT hr;
635 TRACE("iface %p, hotspot_x %u, hotspot_y %u, bitmap %p.\n",
636 iface, hotspot_x, hotspot_y, bitmap);
638 if (!bitmap)
640 WARN("No cursor bitmap, returning D3DERR_INVALIDCALL.\n");
641 return D3DERR_INVALIDCALL;
644 wined3d_mutex_lock();
645 hr = wined3d_device_set_cursor_properties(device->wined3d_device,
646 hotspot_x, hotspot_y, bitmap_impl->wined3d_texture, bitmap_impl->sub_resource_idx);
647 wined3d_mutex_unlock();
649 return hr;
652 static void WINAPI d3d9_device_SetCursorPosition(IDirect3DDevice9Ex *iface, int x, int y, DWORD flags)
654 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
656 TRACE("iface %p, x %u, y %u, flags %#x.\n", iface, x, y, flags);
658 wined3d_mutex_lock();
659 wined3d_device_set_cursor_position(device->wined3d_device, x, y, flags);
660 wined3d_mutex_unlock();
663 static BOOL WINAPI d3d9_device_ShowCursor(IDirect3DDevice9Ex *iface, BOOL show)
665 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
666 BOOL ret;
668 TRACE("iface %p, show %#x.\n", iface, show);
670 wined3d_mutex_lock();
671 ret = wined3d_device_show_cursor(device->wined3d_device, show);
672 wined3d_mutex_unlock();
674 return ret;
677 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_CreateAdditionalSwapChain(IDirect3DDevice9Ex *iface,
678 D3DPRESENT_PARAMETERS *present_parameters, IDirect3DSwapChain9 **swapchain)
680 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
681 struct wined3d_swapchain_desc desc;
682 struct d3d9_swapchain *object;
683 UINT i, count;
684 HRESULT hr;
686 TRACE("iface %p, present_parameters %p, swapchain %p.\n",
687 iface, present_parameters, swapchain);
689 if (!present_parameters->Windowed)
691 WARN("Trying to create an additional fullscreen swapchain, returning D3DERR_INVALIDCALL.\n");
692 return D3DERR_INVALIDCALL;
695 wined3d_mutex_lock();
696 count = wined3d_device_get_swapchain_count(device->wined3d_device);
697 for (i = 0; i < count; ++i)
699 struct wined3d_swapchain *wined3d_swapchain;
701 wined3d_swapchain = wined3d_device_get_swapchain(device->wined3d_device, i);
702 wined3d_swapchain_get_desc(wined3d_swapchain, &desc);
704 if (!desc.windowed)
706 wined3d_mutex_unlock();
707 WARN("Trying to create an additional swapchain in fullscreen mode, returning D3DERR_INVALIDCALL.\n");
708 return D3DERR_INVALIDCALL;
711 wined3d_mutex_unlock();
713 if (!wined3d_swapchain_desc_from_present_parameters(&desc, present_parameters,
714 device->d3d_parent->extended))
715 return D3DERR_INVALIDCALL;
716 if (SUCCEEDED(hr = d3d9_swapchain_create(device, &desc, &object)))
717 *swapchain = (IDirect3DSwapChain9 *)&object->IDirect3DSwapChain9Ex_iface;
718 present_parameters_from_wined3d_swapchain_desc(present_parameters, &desc);
720 return hr;
723 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_GetSwapChain(IDirect3DDevice9Ex *iface,
724 UINT swapchain_idx, IDirect3DSwapChain9 **swapchain)
726 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
727 HRESULT hr;
729 TRACE("iface %p, swapchain_idx %u, swapchain %p.\n", iface, swapchain_idx, swapchain);
731 wined3d_mutex_lock();
732 if (swapchain_idx < device->implicit_swapchain_count)
734 *swapchain = (IDirect3DSwapChain9 *)&device->implicit_swapchains[swapchain_idx]->IDirect3DSwapChain9Ex_iface;
735 IDirect3DSwapChain9Ex_AddRef(*swapchain);
736 hr = D3D_OK;
738 else
740 *swapchain = NULL;
741 hr = D3DERR_INVALIDCALL;
743 wined3d_mutex_unlock();
745 return hr;
748 static UINT WINAPI d3d9_device_GetNumberOfSwapChains(IDirect3DDevice9Ex *iface)
750 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
751 UINT count;
753 TRACE("iface %p.\n", iface);
755 wined3d_mutex_lock();
756 count = wined3d_device_get_swapchain_count(device->wined3d_device);
757 wined3d_mutex_unlock();
759 return count;
762 static HRESULT CDECL reset_enum_callback(struct wined3d_resource *resource)
764 struct wined3d_resource_desc desc;
765 IDirect3DBaseTexture9 *texture;
766 struct d3d9_surface *surface;
767 IUnknown *parent;
769 wined3d_resource_get_desc(resource, &desc);
770 if (desc.pool != WINED3D_POOL_DEFAULT)
771 return D3D_OK;
773 if (desc.resource_type != WINED3D_RTYPE_TEXTURE_2D)
775 WARN("Resource %p in pool D3DPOOL_DEFAULT blocks the Reset call.\n", resource);
776 return D3DERR_INVALIDCALL;
779 parent = wined3d_resource_get_parent(resource);
780 if (parent && SUCCEEDED(IUnknown_QueryInterface(parent, &IID_IDirect3DBaseTexture9, (void **)&texture)))
782 IDirect3DBaseTexture9_Release(texture);
783 WARN("Texture %p (resource %p) in pool D3DPOOL_DEFAULT blocks the Reset call.\n", texture, resource);
784 return D3DERR_INVALIDCALL;
787 surface = wined3d_texture_get_sub_resource_parent(wined3d_texture_from_resource(resource), 0);
788 if (!surface->resource.refcount)
789 return D3D_OK;
791 WARN("Surface %p in pool D3DPOOL_DEFAULT blocks the Reset call.\n", surface);
792 return D3DERR_INVALIDCALL;
795 static HRESULT d3d9_device_get_swapchains(struct d3d9_device *device)
797 UINT i, new_swapchain_count = wined3d_device_get_swapchain_count(device->wined3d_device);
798 struct wined3d_swapchain *wined3d_swapchain;
800 if (!(device->implicit_swapchains = HeapAlloc(GetProcessHeap(), 0,
801 new_swapchain_count * sizeof(*device->implicit_swapchains))))
802 return E_OUTOFMEMORY;
804 for (i = 0; i < new_swapchain_count; ++i)
806 wined3d_swapchain = wined3d_device_get_swapchain(device->wined3d_device, i);
807 device->implicit_swapchains[i] = wined3d_swapchain_get_parent(wined3d_swapchain);
809 device->implicit_swapchain_count = new_swapchain_count;
811 return D3D_OK;
814 static HRESULT d3d9_device_reset(struct d3d9_device *device,
815 D3DPRESENT_PARAMETERS *present_parameters, D3DDISPLAYMODEEX *mode)
817 struct wined3d_swapchain_desc swapchain_desc;
818 struct wined3d_display_mode wined3d_mode;
819 HRESULT hr;
821 if (!device->d3d_parent->extended && device->device_state == D3D9_DEVICE_STATE_LOST)
823 WARN("App not active, returning D3DERR_DEVICELOST.\n");
824 return D3DERR_DEVICELOST;
827 if (mode)
829 wined3d_mode.width = mode->Width;
830 wined3d_mode.height = mode->Height;
831 wined3d_mode.refresh_rate = mode->RefreshRate;
832 wined3d_mode.format_id = wined3dformat_from_d3dformat(mode->Format);
833 wined3d_mode.scanline_ordering = mode->ScanLineOrdering;
836 if (!wined3d_swapchain_desc_from_present_parameters(&swapchain_desc, present_parameters,
837 device->d3d_parent->extended))
838 return D3DERR_INVALIDCALL;
840 wined3d_mutex_lock();
842 if (device->vertex_buffer)
844 wined3d_buffer_decref(device->vertex_buffer);
845 device->vertex_buffer = NULL;
846 device->vertex_buffer_size = 0;
849 if (device->index_buffer)
851 wined3d_buffer_decref(device->index_buffer);
852 device->index_buffer = NULL;
853 device->index_buffer_size = 0;
856 if (SUCCEEDED(hr = wined3d_device_reset(device->wined3d_device, &swapchain_desc,
857 mode ? &wined3d_mode : NULL, reset_enum_callback, !device->d3d_parent->extended)))
859 HeapFree(GetProcessHeap(), 0, device->implicit_swapchains);
861 if (FAILED(hr = d3d9_device_get_swapchains(device)))
863 device->device_state = D3D9_DEVICE_STATE_NOT_RESET;
865 else
867 wined3d_swapchain_get_desc(device->implicit_swapchains[0]->wined3d_swapchain, &swapchain_desc);
868 present_parameters->BackBufferWidth = swapchain_desc.backbuffer_width;
869 present_parameters->BackBufferHeight = swapchain_desc.backbuffer_height;
870 present_parameters->BackBufferFormat = d3dformat_from_wined3dformat(swapchain_desc.backbuffer_format);
871 present_parameters->BackBufferCount = swapchain_desc.backbuffer_count;
873 device->device_state = D3D9_DEVICE_STATE_OK;
876 else if (!device->d3d_parent->extended)
878 device->device_state = D3D9_DEVICE_STATE_NOT_RESET;
881 wined3d_mutex_unlock();
883 return hr;
886 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_Reset(IDirect3DDevice9Ex *iface,
887 D3DPRESENT_PARAMETERS *present_parameters)
889 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
891 TRACE("iface %p, present_parameters %p.\n", iface, present_parameters);
893 return d3d9_device_reset(device, present_parameters, NULL);
896 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_Present(IDirect3DDevice9Ex *iface,
897 const RECT *src_rect, const RECT *dst_rect, HWND dst_window_override, const RGNDATA *dirty_region)
899 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
900 UINT i;
901 HRESULT hr;
903 TRACE("iface %p, src_rect %p, dst_rect %p, dst_window_override %p, dirty_region %p.\n",
904 iface, src_rect, dst_rect, dst_window_override, dirty_region);
906 if (device->device_state != D3D9_DEVICE_STATE_OK)
907 return device->d3d_parent->extended ? S_PRESENT_OCCLUDED : D3DERR_DEVICELOST;
909 if (dirty_region)
910 FIXME("Ignoring dirty_region %p.\n", dirty_region);
912 wined3d_mutex_lock();
913 for (i = 0; i < device->implicit_swapchain_count; ++i)
915 if (FAILED(hr = wined3d_swapchain_present(device->implicit_swapchains[i]->wined3d_swapchain,
916 src_rect, dst_rect, dst_window_override, 0)))
918 wined3d_mutex_unlock();
919 return hr;
922 wined3d_mutex_unlock();
924 return D3D_OK;
927 static HRESULT WINAPI d3d9_device_GetBackBuffer(IDirect3DDevice9Ex *iface, UINT swapchain,
928 UINT backbuffer_idx, D3DBACKBUFFER_TYPE backbuffer_type, IDirect3DSurface9 **backbuffer)
930 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
931 HRESULT hr;
933 TRACE("iface %p, swapchain %u, backbuffer_idx %u, backbuffer_type %#x, backbuffer %p.\n",
934 iface, swapchain, backbuffer_idx, backbuffer_type, backbuffer);
936 /* backbuffer_type is ignored by native. */
938 /* No need to check for backbuffer == NULL, Windows crashes in that case. */
939 *backbuffer = NULL;
941 wined3d_mutex_lock();
942 if (swapchain >= device->implicit_swapchain_count)
944 wined3d_mutex_unlock();
945 WARN("Swapchain index %u is out of range, returning D3DERR_INVALIDCALL.\n", swapchain);
946 return D3DERR_INVALIDCALL;
949 hr = IDirect3DSwapChain9Ex_GetBackBuffer(&device->implicit_swapchains[swapchain]->IDirect3DSwapChain9Ex_iface,
950 backbuffer_idx, backbuffer_type, backbuffer);
951 wined3d_mutex_unlock();
953 return hr;
956 static HRESULT WINAPI d3d9_device_GetRasterStatus(IDirect3DDevice9Ex *iface,
957 UINT swapchain, D3DRASTER_STATUS *raster_status)
959 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
960 HRESULT hr;
962 TRACE("iface %p, swapchain %u, raster_status %p.\n", iface, swapchain, raster_status);
964 wined3d_mutex_lock();
965 hr = wined3d_device_get_raster_status(device->wined3d_device,
966 swapchain, (struct wined3d_raster_status *)raster_status);
967 wined3d_mutex_unlock();
969 return hr;
972 static HRESULT WINAPI d3d9_device_SetDialogBoxMode(IDirect3DDevice9Ex *iface, BOOL enable)
974 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
975 HRESULT hr;
977 TRACE("iface %p, enable %#x.\n", iface, enable);
979 wined3d_mutex_lock();
980 hr = wined3d_device_set_dialog_box_mode(device->wined3d_device, enable);
981 wined3d_mutex_unlock();
983 return hr;
986 static void WINAPI d3d9_device_SetGammaRamp(IDirect3DDevice9Ex *iface,
987 UINT swapchain, DWORD flags, const D3DGAMMARAMP *ramp)
989 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
991 TRACE("iface %p, swapchain %u, flags %#x, ramp %p.\n", iface, swapchain, flags, ramp);
993 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
994 wined3d_mutex_lock();
995 wined3d_device_set_gamma_ramp(device->wined3d_device, swapchain, flags, (const struct wined3d_gamma_ramp *)ramp);
996 wined3d_mutex_unlock();
999 static void WINAPI d3d9_device_GetGammaRamp(IDirect3DDevice9Ex *iface, UINT swapchain, D3DGAMMARAMP *ramp)
1001 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1003 TRACE("iface %p, swapchain %u, ramp %p.\n", iface, swapchain, ramp);
1005 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
1006 wined3d_mutex_lock();
1007 wined3d_device_get_gamma_ramp(device->wined3d_device, swapchain, (struct wined3d_gamma_ramp *)ramp);
1008 wined3d_mutex_unlock();
1011 static HRESULT WINAPI d3d9_device_CreateTexture(IDirect3DDevice9Ex *iface,
1012 UINT width, UINT height, UINT levels, DWORD usage, D3DFORMAT format,
1013 D3DPOOL pool, IDirect3DTexture9 **texture, HANDLE *shared_handle)
1015 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1016 struct d3d9_texture *object;
1017 BOOL set_mem = FALSE;
1018 HRESULT hr;
1020 TRACE("iface %p, width %u, height %u, levels %u, usage %#x, format %#x, pool %#x, texture %p, shared_handle %p.\n",
1021 iface, width, height, levels, usage, format, pool, texture, shared_handle);
1023 *texture = NULL;
1024 if (shared_handle)
1026 if (!device->d3d_parent->extended)
1028 WARN("Trying to create a shared or user memory texture on a non-ex device.\n");
1029 return E_NOTIMPL;
1032 if (pool == D3DPOOL_SYSTEMMEM)
1034 if (levels != 1)
1035 return D3DERR_INVALIDCALL;
1036 set_mem = TRUE;
1038 else
1040 if (pool != D3DPOOL_DEFAULT)
1042 WARN("Trying to create a shared texture in pool %#x.\n", pool);
1043 return D3DERR_INVALIDCALL;
1045 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1049 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1050 if (!object)
1051 return D3DERR_OUTOFVIDEOMEMORY;
1053 hr = texture_init(object, device, width, height, levels, usage, format, pool);
1054 if (FAILED(hr))
1056 WARN("Failed to initialize texture, hr %#x.\n", hr);
1057 HeapFree(GetProcessHeap(), 0, object);
1058 return hr;
1061 if (set_mem)
1062 wined3d_texture_update_desc(object->wined3d_texture, width, height,
1063 wined3dformat_from_d3dformat(format), WINED3D_MULTISAMPLE_NONE, 0,
1064 *shared_handle, 0);
1066 TRACE("Created texture %p.\n", object);
1067 *texture = (IDirect3DTexture9 *)&object->IDirect3DBaseTexture9_iface;
1069 return D3D_OK;
1072 static HRESULT WINAPI d3d9_device_CreateVolumeTexture(IDirect3DDevice9Ex *iface,
1073 UINT width, UINT height, UINT depth, UINT levels, DWORD usage, D3DFORMAT format,
1074 D3DPOOL pool, IDirect3DVolumeTexture9 **texture, HANDLE *shared_handle)
1076 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1077 struct d3d9_texture *object;
1078 HRESULT hr;
1080 TRACE("iface %p, width %u, height %u, depth %u, levels %u, "
1081 "usage %#x, format %#x, pool %#x, texture %p, shared_handle %p.\n",
1082 iface, width, height, depth, levels,
1083 usage, format, pool, texture, shared_handle);
1085 *texture = NULL;
1086 if (shared_handle)
1088 if (!device->d3d_parent->extended)
1090 WARN("Trying to create a shared volume texture on a non-ex device.\n");
1091 return E_NOTIMPL;
1094 if (pool != D3DPOOL_DEFAULT)
1096 WARN("Trying to create a shared volume texture in pool %#x.\n", pool);
1097 return D3DERR_INVALIDCALL;
1099 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1102 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1103 if (!object)
1104 return D3DERR_OUTOFVIDEOMEMORY;
1106 hr = volumetexture_init(object, device, width, height, depth, levels, usage, format, pool);
1107 if (FAILED(hr))
1109 WARN("Failed to initialize volume texture, hr %#x.\n", hr);
1110 HeapFree(GetProcessHeap(), 0, object);
1111 return hr;
1114 TRACE("Created volume texture %p.\n", object);
1115 *texture = (IDirect3DVolumeTexture9 *)&object->IDirect3DBaseTexture9_iface;
1117 return D3D_OK;
1120 static HRESULT WINAPI d3d9_device_CreateCubeTexture(IDirect3DDevice9Ex *iface,
1121 UINT edge_length, UINT levels, DWORD usage, D3DFORMAT format, D3DPOOL pool,
1122 IDirect3DCubeTexture9 **texture, HANDLE *shared_handle)
1124 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1125 struct d3d9_texture *object;
1126 HRESULT hr;
1128 TRACE("iface %p, edge_length %u, levels %u, usage %#x, format %#x, pool %#x, texture %p, shared_handle %p.\n",
1129 iface, edge_length, levels, usage, format, pool, texture, shared_handle);
1131 *texture = NULL;
1132 if (shared_handle)
1134 if (!device->d3d_parent->extended)
1136 WARN("Trying to create a shared cube texture on a non-ex device.\n");
1137 return E_NOTIMPL;
1140 if (pool != D3DPOOL_DEFAULT)
1142 WARN("Trying to create a shared cube texture in pool %#x.\n", pool);
1143 return D3DERR_INVALIDCALL;
1145 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1148 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1149 if (!object)
1150 return D3DERR_OUTOFVIDEOMEMORY;
1152 hr = cubetexture_init(object, device, edge_length, levels, usage, format, pool);
1153 if (FAILED(hr))
1155 WARN("Failed to initialize cube texture, hr %#x.\n", hr);
1156 HeapFree(GetProcessHeap(), 0, object);
1157 return hr;
1160 TRACE("Created cube texture %p.\n", object);
1161 *texture = (IDirect3DCubeTexture9 *)&object->IDirect3DBaseTexture9_iface;
1163 return D3D_OK;
1166 static HRESULT WINAPI d3d9_device_CreateVertexBuffer(IDirect3DDevice9Ex *iface, UINT size,
1167 DWORD usage, DWORD fvf, D3DPOOL pool, IDirect3DVertexBuffer9 **buffer,
1168 HANDLE *shared_handle)
1170 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1171 struct d3d9_vertexbuffer *object;
1172 HRESULT hr;
1174 TRACE("iface %p, size %u, usage %#x, fvf %#x, pool %#x, buffer %p, shared_handle %p.\n",
1175 iface, size, usage, fvf, pool, buffer, shared_handle);
1177 if (shared_handle)
1179 if (!device->d3d_parent->extended)
1181 WARN("Trying to create a shared vertex buffer on a non-ex device.\n");
1182 return E_NOTIMPL;
1185 if (pool != D3DPOOL_DEFAULT)
1187 WARN("Trying to create a shared vertex buffer in pool %#x.\n", pool);
1188 return D3DERR_NOTAVAILABLE;
1190 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1193 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1194 if (!object)
1195 return D3DERR_OUTOFVIDEOMEMORY;
1197 hr = vertexbuffer_init(object, device, size, usage, fvf, pool);
1198 if (FAILED(hr))
1200 WARN("Failed to initialize vertex buffer, hr %#x.\n", hr);
1201 HeapFree(GetProcessHeap(), 0, object);
1202 return hr;
1205 TRACE("Created vertex buffer %p.\n", object);
1206 *buffer = &object->IDirect3DVertexBuffer9_iface;
1208 return D3D_OK;
1211 static HRESULT WINAPI d3d9_device_CreateIndexBuffer(IDirect3DDevice9Ex *iface, UINT size,
1212 DWORD usage, D3DFORMAT format, D3DPOOL pool, IDirect3DIndexBuffer9 **buffer,
1213 HANDLE *shared_handle)
1215 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1216 struct d3d9_indexbuffer *object;
1217 HRESULT hr;
1219 TRACE("iface %p, size %u, usage %#x, format %#x, pool %#x, buffer %p, shared_handle %p.\n",
1220 iface, size, usage, format, pool, buffer, shared_handle);
1222 if (shared_handle)
1224 if (!device->d3d_parent->extended)
1226 WARN("Trying to create a shared index buffer on a non-ex device.\n");
1227 return E_NOTIMPL;
1230 if (pool != D3DPOOL_DEFAULT)
1232 WARN("Trying to create a shared index buffer in pool %#x.\n", pool);
1233 return D3DERR_NOTAVAILABLE;
1235 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1238 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1239 if (!object)
1240 return D3DERR_OUTOFVIDEOMEMORY;
1242 hr = indexbuffer_init(object, device, size, usage, format, pool);
1243 if (FAILED(hr))
1245 WARN("Failed to initialize index buffer, hr %#x.\n", hr);
1246 HeapFree(GetProcessHeap(), 0, object);
1247 return hr;
1250 TRACE("Created index buffer %p.\n", object);
1251 *buffer = &object->IDirect3DIndexBuffer9_iface;
1253 return D3D_OK;
1256 static HRESULT d3d9_device_create_surface(struct d3d9_device *device, UINT width, UINT height,
1257 D3DFORMAT format, DWORD flags, IDirect3DSurface9 **surface, UINT usage, D3DPOOL pool,
1258 D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality, void *user_mem)
1260 struct wined3d_resource_desc desc;
1261 struct d3d9_surface *surface_impl;
1262 struct wined3d_texture *texture;
1263 HRESULT hr;
1265 TRACE("device %p, width %u, height %u, format %#x, flags %#x, surface %p.\n"
1266 "usage %#x, pool %#x, multisample_type %#x, multisample_quality %u.\n",
1267 device, width, height, format, flags, surface, usage, pool,
1268 multisample_type, multisample_quality);
1270 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
1271 desc.format = wined3dformat_from_d3dformat(format);
1272 desc.multisample_type = multisample_type;
1273 desc.multisample_quality = multisample_quality;
1274 desc.usage = usage & WINED3DUSAGE_MASK;
1275 desc.pool = pool;
1276 desc.width = width;
1277 desc.height = height;
1278 desc.depth = 1;
1279 desc.size = 0;
1281 if (is_gdi_compat_wined3dformat(desc.format))
1282 flags |= WINED3D_TEXTURE_CREATE_GET_DC;
1284 wined3d_mutex_lock();
1286 if (FAILED(hr = wined3d_texture_create(device->wined3d_device, &desc,
1287 1, 1, flags, NULL, NULL, &d3d9_null_wined3d_parent_ops, &texture)))
1289 wined3d_mutex_unlock();
1290 WARN("Failed to create texture, hr %#x.\n", hr);
1291 if (hr == WINED3DERR_NOTAVAILABLE)
1292 hr = D3DERR_INVALIDCALL;
1293 return hr;
1296 surface_impl = wined3d_texture_get_sub_resource_parent(texture, 0);
1297 surface_impl->parent_device = &device->IDirect3DDevice9Ex_iface;
1298 *surface = &surface_impl->IDirect3DSurface9_iface;
1299 IDirect3DSurface9_AddRef(*surface);
1301 if (user_mem)
1302 wined3d_texture_update_desc(texture, width, height,
1303 desc.format, multisample_type, multisample_quality, user_mem, 0);
1305 wined3d_texture_decref(texture);
1307 wined3d_mutex_unlock();
1309 return D3D_OK;
1312 BOOL is_gdi_compat_wined3dformat(enum wined3d_format_id format)
1314 switch (format)
1316 case WINED3DFMT_B8G8R8A8_UNORM:
1317 case WINED3DFMT_B8G8R8X8_UNORM:
1318 case WINED3DFMT_B5G6R5_UNORM:
1319 case WINED3DFMT_B5G5R5X1_UNORM:
1320 case WINED3DFMT_B5G5R5A1_UNORM:
1321 case WINED3DFMT_B8G8R8_UNORM:
1322 return TRUE;
1323 default:
1324 return FALSE;
1328 static HRESULT WINAPI d3d9_device_CreateRenderTarget(IDirect3DDevice9Ex *iface, UINT width, UINT height,
1329 D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
1330 BOOL lockable, IDirect3DSurface9 **surface, HANDLE *shared_handle)
1332 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1333 DWORD flags = 0;
1335 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u.\n"
1336 "lockable %#x, surface %p, shared_handle %p.\n",
1337 iface, width, height, format, multisample_type, multisample_quality,
1338 lockable, surface, shared_handle);
1340 *surface = NULL;
1341 if (shared_handle)
1343 if (!device->d3d_parent->extended)
1345 WARN("Trying to create a shared render target on a non-ex device.\n");
1346 return E_NOTIMPL;
1349 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1352 if (lockable)
1353 flags |= WINED3D_TEXTURE_CREATE_MAPPABLE;
1355 return d3d9_device_create_surface(device, width, height, format, flags, surface,
1356 D3DUSAGE_RENDERTARGET, D3DPOOL_DEFAULT, multisample_type, multisample_quality, NULL);
1359 static HRESULT WINAPI d3d9_device_CreateDepthStencilSurface(IDirect3DDevice9Ex *iface, UINT width, UINT height,
1360 D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
1361 BOOL discard, IDirect3DSurface9 **surface, HANDLE *shared_handle)
1363 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1364 DWORD flags = WINED3D_TEXTURE_CREATE_MAPPABLE;
1366 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u.\n"
1367 "discard %#x, surface %p, shared_handle %p.\n",
1368 iface, width, height, format, multisample_type, multisample_quality,
1369 discard, surface, shared_handle);
1371 *surface = NULL;
1372 if (shared_handle)
1374 if (!device->d3d_parent->extended)
1376 WARN("Trying to create a shared depth stencil on a non-ex device.\n");
1377 return E_NOTIMPL;
1380 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1383 if (discard)
1384 flags |= WINED3D_TEXTURE_CREATE_DISCARD;
1386 return d3d9_device_create_surface(device, width, height, format, flags, surface,
1387 D3DUSAGE_DEPTHSTENCIL, D3DPOOL_DEFAULT, multisample_type, multisample_quality, NULL);
1391 static HRESULT WINAPI d3d9_device_UpdateSurface(IDirect3DDevice9Ex *iface,
1392 IDirect3DSurface9 *src_surface, const RECT *src_rect,
1393 IDirect3DSurface9 *dst_surface, const POINT *dst_point)
1395 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1396 struct d3d9_surface *src = unsafe_impl_from_IDirect3DSurface9(src_surface);
1397 struct d3d9_surface *dst = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1398 struct wined3d_box src_box;
1399 HRESULT hr;
1401 TRACE("iface %p, src_surface %p, src_rect %p, dst_surface %p, dst_point %p.\n",
1402 iface, src_surface, src_rect, dst_surface, dst_point);
1404 if (src_rect)
1406 src_box.left = src_rect->left;
1407 src_box.top = src_rect->top;
1408 src_box.right = src_rect->right;
1409 src_box.bottom = src_rect->bottom;
1410 src_box.front = 0;
1411 src_box.back = 1;
1414 wined3d_mutex_lock();
1415 hr = wined3d_device_copy_sub_resource_region(device->wined3d_device,
1416 wined3d_texture_get_resource(dst->wined3d_texture), dst->sub_resource_idx, dst_point ? dst_point->x : 0,
1417 dst_point ? dst_point->y : 0, 0, wined3d_texture_get_resource(src->wined3d_texture),
1418 src->sub_resource_idx, src_rect ? &src_box : NULL);
1419 wined3d_mutex_unlock();
1421 if (FAILED(hr))
1422 return D3DERR_INVALIDCALL;
1424 return hr;
1427 static HRESULT WINAPI d3d9_device_UpdateTexture(IDirect3DDevice9Ex *iface,
1428 IDirect3DBaseTexture9 *src_texture, IDirect3DBaseTexture9 *dst_texture)
1430 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1431 struct d3d9_texture *src_impl, *dst_impl;
1432 HRESULT hr;
1434 TRACE("iface %p, src_texture %p, dst_texture %p.\n", iface, src_texture, dst_texture);
1436 src_impl = unsafe_impl_from_IDirect3DBaseTexture9(src_texture);
1437 dst_impl = unsafe_impl_from_IDirect3DBaseTexture9(dst_texture);
1439 wined3d_mutex_lock();
1440 hr = wined3d_device_update_texture(device->wined3d_device,
1441 src_impl->wined3d_texture, dst_impl->wined3d_texture);
1442 wined3d_mutex_unlock();
1444 return hr;
1447 static HRESULT WINAPI d3d9_device_GetRenderTargetData(IDirect3DDevice9Ex *iface,
1448 IDirect3DSurface9 *render_target, IDirect3DSurface9 *dst_surface)
1450 struct d3d9_surface *rt_impl = unsafe_impl_from_IDirect3DSurface9(render_target);
1451 struct d3d9_surface *dst_impl = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1452 struct wined3d_sub_resource_desc wined3d_desc;
1453 RECT dst_rect, src_rect;
1454 HRESULT hr;
1456 TRACE("iface %p, render_target %p, dst_surface %p.\n", iface, render_target, dst_surface);
1458 if (!render_target || !dst_surface)
1459 return D3DERR_INVALIDCALL;
1461 wined3d_mutex_lock();
1462 wined3d_texture_get_sub_resource_desc(dst_impl->wined3d_texture, dst_impl->sub_resource_idx, &wined3d_desc);
1463 SetRect(&dst_rect, 0, 0, wined3d_desc.width, wined3d_desc.height);
1465 wined3d_texture_get_sub_resource_desc(rt_impl->wined3d_texture, rt_impl->sub_resource_idx, &wined3d_desc);
1466 SetRect(&src_rect, 0, 0, wined3d_desc.width, wined3d_desc.height);
1468 /* TODO: Check surface sizes, pools, etc. */
1469 if (wined3d_desc.multisample_type)
1470 hr = D3DERR_INVALIDCALL;
1471 else
1472 hr = wined3d_texture_blt(dst_impl->wined3d_texture, dst_impl->sub_resource_idx, &dst_rect,
1473 rt_impl->wined3d_texture, rt_impl->sub_resource_idx, &src_rect, 0, NULL, WINED3D_TEXF_POINT);
1474 wined3d_mutex_unlock();
1476 return hr;
1479 static HRESULT WINAPI d3d9_device_GetFrontBufferData(IDirect3DDevice9Ex *iface,
1480 UINT swapchain, IDirect3DSurface9 *dst_surface)
1482 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1483 struct d3d9_surface *dst_impl = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1484 HRESULT hr = D3DERR_INVALIDCALL;
1486 TRACE("iface %p, swapchain %u, dst_surface %p.\n", iface, swapchain, dst_surface);
1488 wined3d_mutex_lock();
1489 if (swapchain < device->implicit_swapchain_count)
1490 hr = wined3d_swapchain_get_front_buffer_data(device->implicit_swapchains[swapchain]->wined3d_swapchain,
1491 dst_impl->wined3d_texture, dst_impl->sub_resource_idx);
1492 wined3d_mutex_unlock();
1494 return hr;
1497 static HRESULT WINAPI d3d9_device_StretchRect(IDirect3DDevice9Ex *iface, IDirect3DSurface9 *src_surface,
1498 const RECT *src_rect, IDirect3DSurface9 *dst_surface, const RECT *dst_rect, D3DTEXTUREFILTERTYPE filter)
1500 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1501 struct d3d9_surface *src = unsafe_impl_from_IDirect3DSurface9(src_surface);
1502 struct d3d9_surface *dst = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1503 struct wined3d_sub_resource_desc src_desc, dst_desc;
1504 HRESULT hr = D3DERR_INVALIDCALL;
1505 RECT d, s;
1507 TRACE("iface %p, src_surface %p, src_rect %p, dst_surface %p, dst_rect %p, filter %#x.\n",
1508 iface, src_surface, src_rect, dst_surface, dst_rect, filter);
1510 wined3d_mutex_lock();
1511 wined3d_texture_get_sub_resource_desc(dst->wined3d_texture, dst->sub_resource_idx, &dst_desc);
1512 if (!dst_rect)
1514 SetRect(&d, 0, 0, dst_desc.width, dst_desc.height);
1515 dst_rect = &d;
1518 wined3d_texture_get_sub_resource_desc(src->wined3d_texture, src->sub_resource_idx, &src_desc);
1519 if (!src_rect)
1521 SetRect(&s, 0, 0, src_desc.width, src_desc.height);
1522 src_rect = &s;
1525 if (src_desc.usage & WINED3DUSAGE_DEPTHSTENCIL)
1527 if (device->in_scene)
1529 WARN("Rejecting depth / stencil blit while in scene.\n");
1530 goto done;
1533 if (src_rect->left || src_rect->top || src_rect->right != src_desc.width
1534 || src_rect->bottom != src_desc.height)
1536 WARN("Rejecting depth / stencil blit with invalid source rect %s.\n",
1537 wine_dbgstr_rect(src_rect));
1538 goto done;
1541 if (dst_rect->left || dst_rect->top || dst_rect->right != dst_desc.width
1542 || dst_rect->bottom != dst_desc.height)
1544 WARN("Rejecting depth / stencil blit with invalid destination rect %s.\n",
1545 wine_dbgstr_rect(dst_rect));
1546 goto done;
1549 if (src_desc.width != dst_desc.width || src_desc.height != dst_desc.height)
1551 WARN("Rejecting depth / stencil blit with mismatched surface sizes.\n");
1552 goto done;
1556 hr = wined3d_texture_blt(dst->wined3d_texture, dst->sub_resource_idx, dst_rect,
1557 src->wined3d_texture, src->sub_resource_idx, src_rect, 0, NULL, filter);
1558 if (hr == WINEDDERR_INVALIDRECT)
1559 hr = D3DERR_INVALIDCALL;
1561 done:
1562 wined3d_mutex_unlock();
1563 return hr;
1566 static HRESULT WINAPI d3d9_device_ColorFill(IDirect3DDevice9Ex *iface,
1567 IDirect3DSurface9 *surface, const RECT *rect, D3DCOLOR color)
1569 const struct wined3d_color c =
1571 ((color >> 16) & 0xff) / 255.0f,
1572 ((color >> 8) & 0xff) / 255.0f,
1573 (color & 0xff) / 255.0f,
1574 ((color >> 24) & 0xff) / 255.0f,
1576 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1577 struct d3d9_surface *surface_impl = unsafe_impl_from_IDirect3DSurface9(surface);
1578 struct wined3d_sub_resource_desc desc;
1579 struct wined3d_rendertarget_view *rtv;
1580 HRESULT hr;
1582 TRACE("iface %p, surface %p, rect %p, color 0x%08x.\n", iface, surface, rect, color);
1584 wined3d_mutex_lock();
1586 if (FAILED(wined3d_texture_get_sub_resource_desc(surface_impl->wined3d_texture,
1587 surface_impl->sub_resource_idx, &desc)))
1589 wined3d_mutex_unlock();
1590 return D3DERR_INVALIDCALL;
1593 if (desc.pool != WINED3D_POOL_DEFAULT)
1595 wined3d_mutex_unlock();
1596 WARN("Colorfill is not allowed on surfaces in pool %#x, returning D3DERR_INVALIDCALL.\n", desc.pool);
1597 return D3DERR_INVALIDCALL;
1599 if ((desc.usage & (WINED3DUSAGE_RENDERTARGET | WINED3DUSAGE_TEXTURE)) == WINED3DUSAGE_TEXTURE)
1601 wined3d_mutex_unlock();
1602 WARN("Colorfill is not allowed on non-RT textures, returning D3DERR_INVALIDCALL.\n");
1603 return D3DERR_INVALIDCALL;
1605 if (desc.usage & WINED3DUSAGE_DEPTHSTENCIL)
1607 wined3d_mutex_unlock();
1608 WARN("Colorfill is not allowed on depth stencil surfaces, returning D3DERR_INVALIDCALL.\n");
1609 return D3DERR_INVALIDCALL;
1612 rtv = d3d9_surface_acquire_rendertarget_view(surface_impl);
1613 hr = wined3d_device_clear_rendertarget_view(device->wined3d_device,
1614 rtv, rect, WINED3DCLEAR_TARGET, &c, 0.0f, 0);
1615 d3d9_surface_release_rendertarget_view(surface_impl, rtv);
1617 wined3d_mutex_unlock();
1619 return hr;
1622 static HRESULT WINAPI d3d9_device_CreateOffscreenPlainSurface(IDirect3DDevice9Ex *iface,
1623 UINT width, UINT height, D3DFORMAT format, D3DPOOL pool, IDirect3DSurface9 **surface,
1624 HANDLE *shared_handle)
1626 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1627 void *user_mem = NULL;
1629 TRACE("iface %p, width %u, height %u, format %#x, pool %#x, surface %p, shared_handle %p.\n",
1630 iface, width, height, format, pool, surface, shared_handle);
1632 *surface = NULL;
1633 if (pool == D3DPOOL_MANAGED)
1635 WARN("Attempting to create a managed offscreen plain surface.\n");
1636 return D3DERR_INVALIDCALL;
1639 if (shared_handle)
1641 if (!device->d3d_parent->extended)
1643 WARN("Trying to create a shared or user memory surface on a non-ex device.\n");
1644 return E_NOTIMPL;
1647 if (pool == D3DPOOL_SYSTEMMEM)
1648 user_mem = *shared_handle;
1649 else
1651 if (pool != D3DPOOL_DEFAULT)
1653 WARN("Trying to create a shared surface in pool %#x.\n", pool);
1654 return D3DERR_INVALIDCALL;
1656 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1660 /* FIXME: Offscreen surfaces are supposed to be always lockable,
1661 * regardless of the pool they're created in. Should we set dynamic usage
1662 * here? */
1663 return d3d9_device_create_surface(device, width, height, format,
1664 WINED3D_TEXTURE_CREATE_MAPPABLE, surface, 0, pool, D3DMULTISAMPLE_NONE, 0, user_mem);
1667 static HRESULT WINAPI d3d9_device_SetRenderTarget(IDirect3DDevice9Ex *iface, DWORD idx, IDirect3DSurface9 *surface)
1669 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1670 struct d3d9_surface *surface_impl = unsafe_impl_from_IDirect3DSurface9(surface);
1671 struct wined3d_rendertarget_view *rtv;
1672 HRESULT hr;
1674 TRACE("iface %p, idx %u, surface %p.\n", iface, idx, surface);
1676 if (idx >= D3D9_MAX_SIMULTANEOUS_RENDERTARGETS)
1678 WARN("Invalid index %u specified.\n", idx);
1679 return D3DERR_INVALIDCALL;
1682 if (!idx && !surface_impl)
1684 WARN("Trying to set render target 0 to NULL.\n");
1685 return D3DERR_INVALIDCALL;
1688 if (surface_impl && d3d9_surface_get_device(surface_impl) != device)
1690 WARN("Render target surface does not match device.\n");
1691 return D3DERR_INVALIDCALL;
1694 wined3d_mutex_lock();
1695 rtv = surface_impl ? d3d9_surface_acquire_rendertarget_view(surface_impl) : NULL;
1696 hr = wined3d_device_set_rendertarget_view(device->wined3d_device, idx, rtv, TRUE);
1697 d3d9_surface_release_rendertarget_view(surface_impl, rtv);
1698 wined3d_mutex_unlock();
1700 return hr;
1703 static HRESULT WINAPI d3d9_device_GetRenderTarget(IDirect3DDevice9Ex *iface, DWORD idx, IDirect3DSurface9 **surface)
1705 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1706 struct wined3d_rendertarget_view *wined3d_rtv;
1707 struct d3d9_surface *surface_impl;
1708 HRESULT hr = D3D_OK;
1710 TRACE("iface %p, idx %u, surface %p.\n", iface, idx, surface);
1712 if (!surface)
1713 return D3DERR_INVALIDCALL;
1715 if (idx >= D3D9_MAX_SIMULTANEOUS_RENDERTARGETS)
1717 WARN("Invalid index %u specified.\n", idx);
1718 return D3DERR_INVALIDCALL;
1721 wined3d_mutex_lock();
1722 if ((wined3d_rtv = wined3d_device_get_rendertarget_view(device->wined3d_device, idx)))
1724 /* We want the sub resource parent here, since the view itself may be
1725 * internal to wined3d and may not have a parent. */
1726 surface_impl = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_rtv);
1727 *surface = &surface_impl->IDirect3DSurface9_iface;
1728 IDirect3DSurface9_AddRef(*surface);
1730 else
1732 hr = D3DERR_NOTFOUND;
1733 *surface = NULL;
1735 wined3d_mutex_unlock();
1737 return hr;
1740 static HRESULT WINAPI d3d9_device_SetDepthStencilSurface(IDirect3DDevice9Ex *iface, IDirect3DSurface9 *depth_stencil)
1742 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1743 struct d3d9_surface *ds_impl = unsafe_impl_from_IDirect3DSurface9(depth_stencil);
1744 struct wined3d_rendertarget_view *rtv;
1746 TRACE("iface %p, depth_stencil %p.\n", iface, depth_stencil);
1748 wined3d_mutex_lock();
1749 rtv = ds_impl ? d3d9_surface_acquire_rendertarget_view(ds_impl) : NULL;
1750 wined3d_device_set_depth_stencil_view(device->wined3d_device, rtv);
1751 d3d9_surface_release_rendertarget_view(ds_impl, rtv);
1752 wined3d_mutex_unlock();
1754 return D3D_OK;
1757 static HRESULT WINAPI d3d9_device_GetDepthStencilSurface(IDirect3DDevice9Ex *iface, IDirect3DSurface9 **depth_stencil)
1759 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1760 struct wined3d_rendertarget_view *wined3d_dsv;
1761 struct d3d9_surface *surface_impl;
1762 HRESULT hr = D3D_OK;
1764 TRACE("iface %p, depth_stencil %p.\n", iface, depth_stencil);
1766 if (!depth_stencil)
1767 return D3DERR_INVALIDCALL;
1769 wined3d_mutex_lock();
1770 if ((wined3d_dsv = wined3d_device_get_depth_stencil_view(device->wined3d_device)))
1772 /* We want the sub resource parent here, since the view itself may be
1773 * internal to wined3d and may not have a parent. */
1774 surface_impl = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_dsv);
1775 *depth_stencil = &surface_impl->IDirect3DSurface9_iface;
1776 IDirect3DSurface9_AddRef(*depth_stencil);
1778 else
1780 hr = D3DERR_NOTFOUND;
1781 *depth_stencil = NULL;
1783 wined3d_mutex_unlock();
1785 return hr;
1788 static HRESULT WINAPI d3d9_device_BeginScene(IDirect3DDevice9Ex *iface)
1790 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1791 HRESULT hr;
1793 TRACE("iface %p.\n", iface);
1795 wined3d_mutex_lock();
1796 if (SUCCEEDED(hr = wined3d_device_begin_scene(device->wined3d_device)))
1797 device->in_scene = TRUE;
1798 wined3d_mutex_unlock();
1800 return hr;
1803 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_EndScene(IDirect3DDevice9Ex *iface)
1805 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1806 HRESULT hr;
1808 TRACE("iface %p.\n", iface);
1810 wined3d_mutex_lock();
1811 if (SUCCEEDED(hr = wined3d_device_end_scene(device->wined3d_device)))
1812 device->in_scene = FALSE;
1813 wined3d_mutex_unlock();
1815 return hr;
1818 static HRESULT WINAPI d3d9_device_Clear(IDirect3DDevice9Ex *iface, DWORD rect_count,
1819 const D3DRECT *rects, DWORD flags, D3DCOLOR color, float z, DWORD stencil)
1821 const struct wined3d_color c =
1823 ((color >> 16) & 0xff) / 255.0f,
1824 ((color >> 8) & 0xff) / 255.0f,
1825 (color & 0xff) / 255.0f,
1826 ((color >> 24) & 0xff) / 255.0f,
1828 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1829 HRESULT hr;
1831 TRACE("iface %p, rect_count %u, rects %p, flags %#x, color 0x%08x, z %.8e, stencil %u.\n",
1832 iface, rect_count, rects, flags, color, z, stencil);
1834 if (rect_count && !rects)
1836 WARN("count %u with NULL rects.\n", rect_count);
1837 rect_count = 0;
1840 wined3d_mutex_lock();
1841 hr = wined3d_device_clear(device->wined3d_device, rect_count, (const RECT *)rects, flags, &c, z, stencil);
1842 wined3d_mutex_unlock();
1844 return hr;
1847 static HRESULT WINAPI d3d9_device_SetTransform(IDirect3DDevice9Ex *iface,
1848 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
1850 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1852 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1854 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1855 wined3d_mutex_lock();
1856 wined3d_device_set_transform(device->wined3d_device, state, (const struct wined3d_matrix *)matrix);
1857 wined3d_mutex_unlock();
1859 return D3D_OK;
1862 static HRESULT WINAPI d3d9_device_GetTransform(IDirect3DDevice9Ex *iface,
1863 D3DTRANSFORMSTATETYPE state, D3DMATRIX *matrix)
1865 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1867 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1869 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1870 wined3d_mutex_lock();
1871 wined3d_device_get_transform(device->wined3d_device, state, (struct wined3d_matrix *)matrix);
1872 wined3d_mutex_unlock();
1874 return D3D_OK;
1877 static HRESULT WINAPI d3d9_device_MultiplyTransform(IDirect3DDevice9Ex *iface,
1878 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
1880 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1882 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1884 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1885 wined3d_mutex_lock();
1886 wined3d_device_multiply_transform(device->wined3d_device, state, (const struct wined3d_matrix *)matrix);
1887 wined3d_mutex_unlock();
1889 return D3D_OK;
1892 static HRESULT WINAPI d3d9_device_SetViewport(IDirect3DDevice9Ex *iface, const D3DVIEWPORT9 *viewport)
1894 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1896 TRACE("iface %p, viewport %p.\n", iface, viewport);
1898 /* Note: D3DVIEWPORT9 is compatible with struct wined3d_viewport. */
1899 wined3d_mutex_lock();
1900 wined3d_device_set_viewport(device->wined3d_device, (const struct wined3d_viewport *)viewport);
1901 wined3d_mutex_unlock();
1903 return D3D_OK;
1906 static HRESULT WINAPI d3d9_device_GetViewport(IDirect3DDevice9Ex *iface, D3DVIEWPORT9 *viewport)
1908 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1910 TRACE("iface %p, viewport %p.\n", iface, viewport);
1912 /* Note: D3DVIEWPORT9 is compatible with struct wined3d_viewport. */
1913 wined3d_mutex_lock();
1914 wined3d_device_get_viewport(device->wined3d_device, (struct wined3d_viewport *)viewport);
1915 wined3d_mutex_unlock();
1917 return D3D_OK;
1920 static HRESULT WINAPI d3d9_device_SetMaterial(IDirect3DDevice9Ex *iface, const D3DMATERIAL9 *material)
1922 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1924 TRACE("iface %p, material %p.\n", iface, material);
1926 /* Note: D3DMATERIAL9 is compatible with struct wined3d_material. */
1927 wined3d_mutex_lock();
1928 wined3d_device_set_material(device->wined3d_device, (const struct wined3d_material *)material);
1929 wined3d_mutex_unlock();
1931 return D3D_OK;
1934 static HRESULT WINAPI d3d9_device_GetMaterial(IDirect3DDevice9Ex *iface, D3DMATERIAL9 *material)
1936 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1938 TRACE("iface %p, material %p.\n", iface, material);
1940 /* Note: D3DMATERIAL9 is compatible with struct wined3d_material. */
1941 wined3d_mutex_lock();
1942 wined3d_device_get_material(device->wined3d_device, (struct wined3d_material *)material);
1943 wined3d_mutex_unlock();
1945 return D3D_OK;
1948 static HRESULT WINAPI d3d9_device_SetLight(IDirect3DDevice9Ex *iface, DWORD index, const D3DLIGHT9 *light)
1950 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1951 HRESULT hr;
1953 TRACE("iface %p, index %u, light %p.\n", iface, index, light);
1955 /* Note: D3DLIGHT9 is compatible with struct wined3d_light. */
1956 wined3d_mutex_lock();
1957 hr = wined3d_device_set_light(device->wined3d_device, index, (const struct wined3d_light *)light);
1958 wined3d_mutex_unlock();
1960 return hr;
1963 static HRESULT WINAPI d3d9_device_GetLight(IDirect3DDevice9Ex *iface, DWORD index, D3DLIGHT9 *light)
1965 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1966 HRESULT hr;
1968 TRACE("iface %p, index %u, light %p.\n", iface, index, light);
1970 /* Note: D3DLIGHT9 is compatible with struct wined3d_light. */
1971 wined3d_mutex_lock();
1972 hr = wined3d_device_get_light(device->wined3d_device, index, (struct wined3d_light *)light);
1973 wined3d_mutex_unlock();
1975 return hr;
1978 static HRESULT WINAPI d3d9_device_LightEnable(IDirect3DDevice9Ex *iface, DWORD index, BOOL enable)
1980 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1981 HRESULT hr;
1983 TRACE("iface %p, index %u, enable %#x.\n", iface, index, enable);
1985 wined3d_mutex_lock();
1986 hr = wined3d_device_set_light_enable(device->wined3d_device, index, enable);
1987 wined3d_mutex_unlock();
1989 return hr;
1992 static HRESULT WINAPI d3d9_device_GetLightEnable(IDirect3DDevice9Ex *iface, DWORD index, BOOL *enable)
1994 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1995 HRESULT hr;
1997 TRACE("iface %p, index %u, enable %p.\n", iface, index, enable);
1999 wined3d_mutex_lock();
2000 hr = wined3d_device_get_light_enable(device->wined3d_device, index, enable);
2001 wined3d_mutex_unlock();
2003 return hr;
2006 static HRESULT WINAPI d3d9_device_SetClipPlane(IDirect3DDevice9Ex *iface, DWORD index, const float *plane)
2008 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2009 HRESULT hr;
2011 TRACE("iface %p, index %u, plane %p.\n", iface, index, plane);
2013 wined3d_mutex_lock();
2014 hr = wined3d_device_set_clip_plane(device->wined3d_device, index, (const struct wined3d_vec4 *)plane);
2015 wined3d_mutex_unlock();
2017 return hr;
2020 static HRESULT WINAPI d3d9_device_GetClipPlane(IDirect3DDevice9Ex *iface, DWORD index, float *plane)
2022 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2023 HRESULT hr;
2025 TRACE("iface %p, index %u, plane %p.\n", iface, index, plane);
2027 wined3d_mutex_lock();
2028 hr = wined3d_device_get_clip_plane(device->wined3d_device, index, (struct wined3d_vec4 *)plane);
2029 wined3d_mutex_unlock();
2031 return hr;
2034 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_SetRenderState(IDirect3DDevice9Ex *iface,
2035 D3DRENDERSTATETYPE state, DWORD value)
2037 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2039 TRACE("iface %p, state %#x, value %#x.\n", iface, state, value);
2041 wined3d_mutex_lock();
2042 wined3d_device_set_render_state(device->wined3d_device, state, value);
2043 wined3d_mutex_unlock();
2045 return D3D_OK;
2048 static HRESULT WINAPI d3d9_device_GetRenderState(IDirect3DDevice9Ex *iface,
2049 D3DRENDERSTATETYPE state, DWORD *value)
2051 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2053 TRACE("iface %p, state %#x, value %p.\n", iface, state, value);
2055 wined3d_mutex_lock();
2056 *value = wined3d_device_get_render_state(device->wined3d_device, state);
2057 wined3d_mutex_unlock();
2059 return D3D_OK;
2062 static HRESULT WINAPI d3d9_device_CreateStateBlock(IDirect3DDevice9Ex *iface,
2063 D3DSTATEBLOCKTYPE type, IDirect3DStateBlock9 **stateblock)
2065 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2066 struct d3d9_stateblock *object;
2067 HRESULT hr;
2069 TRACE("iface %p, type %#x, stateblock %p.\n", iface, type, stateblock);
2071 if (type != D3DSBT_ALL && type != D3DSBT_PIXELSTATE && type != D3DSBT_VERTEXSTATE)
2073 WARN("Unexpected stateblock type, returning D3DERR_INVALIDCALL.\n");
2074 return D3DERR_INVALIDCALL;
2077 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2078 if (!object)
2079 return E_OUTOFMEMORY;
2081 hr = stateblock_init(object, device, type, NULL);
2082 if (FAILED(hr))
2084 WARN("Failed to initialize stateblock, hr %#x.\n", hr);
2085 HeapFree(GetProcessHeap(), 0, object);
2086 return hr;
2089 TRACE("Created stateblock %p.\n", object);
2090 *stateblock = &object->IDirect3DStateBlock9_iface;
2092 return D3D_OK;
2095 static HRESULT WINAPI d3d9_device_BeginStateBlock(IDirect3DDevice9Ex *iface)
2097 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2098 HRESULT hr;
2100 TRACE("iface %p.\n", iface);
2102 wined3d_mutex_lock();
2103 hr = wined3d_device_begin_stateblock(device->wined3d_device);
2104 wined3d_mutex_unlock();
2106 return hr;
2109 static HRESULT WINAPI d3d9_device_EndStateBlock(IDirect3DDevice9Ex *iface, IDirect3DStateBlock9 **stateblock)
2111 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2112 struct wined3d_stateblock *wined3d_stateblock;
2113 struct d3d9_stateblock *object;
2114 HRESULT hr;
2116 TRACE("iface %p, stateblock %p.\n", iface, stateblock);
2118 wined3d_mutex_lock();
2119 hr = wined3d_device_end_stateblock(device->wined3d_device, &wined3d_stateblock);
2120 wined3d_mutex_unlock();
2121 if (FAILED(hr))
2123 WARN("Failed to end the state block, hr %#x.\n", hr);
2124 return hr;
2127 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2128 if (!object)
2130 wined3d_mutex_lock();
2131 wined3d_stateblock_decref(wined3d_stateblock);
2132 wined3d_mutex_unlock();
2133 return E_OUTOFMEMORY;
2136 hr = stateblock_init(object, device, 0, wined3d_stateblock);
2137 if (FAILED(hr))
2139 WARN("Failed to initialize stateblock, hr %#x.\n", hr);
2140 wined3d_mutex_lock();
2141 wined3d_stateblock_decref(wined3d_stateblock);
2142 wined3d_mutex_unlock();
2143 HeapFree(GetProcessHeap(), 0, object);
2144 return hr;
2147 TRACE("Created stateblock %p.\n", object);
2148 *stateblock = &object->IDirect3DStateBlock9_iface;
2150 return D3D_OK;
2153 static HRESULT WINAPI d3d9_device_SetClipStatus(IDirect3DDevice9Ex *iface, const D3DCLIPSTATUS9 *clip_status)
2155 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2156 HRESULT hr;
2158 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
2160 wined3d_mutex_lock();
2161 hr = wined3d_device_set_clip_status(device->wined3d_device, (const struct wined3d_clip_status *)clip_status);
2162 wined3d_mutex_unlock();
2164 return hr;
2167 static HRESULT WINAPI d3d9_device_GetClipStatus(IDirect3DDevice9Ex *iface, D3DCLIPSTATUS9 *clip_status)
2169 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2170 HRESULT hr;
2172 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
2174 wined3d_mutex_lock();
2175 hr = wined3d_device_get_clip_status(device->wined3d_device, (struct wined3d_clip_status *)clip_status);
2176 wined3d_mutex_unlock();
2178 return hr;
2181 static HRESULT WINAPI d3d9_device_GetTexture(IDirect3DDevice9Ex *iface, DWORD stage, IDirect3DBaseTexture9 **texture)
2183 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2184 struct wined3d_texture *wined3d_texture = NULL;
2185 struct d3d9_texture *texture_impl;
2187 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
2189 if (!texture)
2190 return D3DERR_INVALIDCALL;
2192 wined3d_mutex_lock();
2193 if ((wined3d_texture = wined3d_device_get_texture(device->wined3d_device, stage)))
2195 texture_impl = wined3d_texture_get_parent(wined3d_texture);
2196 *texture = &texture_impl->IDirect3DBaseTexture9_iface;
2197 IDirect3DBaseTexture9_AddRef(*texture);
2199 else
2201 *texture = NULL;
2203 wined3d_mutex_unlock();
2205 return D3D_OK;
2208 static HRESULT WINAPI d3d9_device_SetTexture(IDirect3DDevice9Ex *iface, DWORD stage, IDirect3DBaseTexture9 *texture)
2210 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2211 struct d3d9_texture *texture_impl;
2212 HRESULT hr;
2214 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
2216 texture_impl = unsafe_impl_from_IDirect3DBaseTexture9(texture);
2218 wined3d_mutex_lock();
2219 hr = wined3d_device_set_texture(device->wined3d_device, stage,
2220 texture_impl ? texture_impl->wined3d_texture : NULL);
2221 wined3d_mutex_unlock();
2223 return hr;
2226 static const enum wined3d_texture_stage_state tss_lookup[] =
2228 WINED3D_TSS_INVALID, /* 0, unused */
2229 WINED3D_TSS_COLOR_OP, /* 1, D3DTSS_COLOROP */
2230 WINED3D_TSS_COLOR_ARG1, /* 2, D3DTSS_COLORARG1 */
2231 WINED3D_TSS_COLOR_ARG2, /* 3, D3DTSS_COLORARG2 */
2232 WINED3D_TSS_ALPHA_OP, /* 4, D3DTSS_ALPHAOP */
2233 WINED3D_TSS_ALPHA_ARG1, /* 5, D3DTSS_ALPHAARG1 */
2234 WINED3D_TSS_ALPHA_ARG2, /* 6, D3DTSS_ALPHAARG2 */
2235 WINED3D_TSS_BUMPENV_MAT00, /* 7, D3DTSS_BUMPENVMAT00 */
2236 WINED3D_TSS_BUMPENV_MAT01, /* 8, D3DTSS_BUMPENVMAT01 */
2237 WINED3D_TSS_BUMPENV_MAT10, /* 9, D3DTSS_BUMPENVMAT10 */
2238 WINED3D_TSS_BUMPENV_MAT11, /* 10, D3DTSS_BUMPENVMAT11 */
2239 WINED3D_TSS_TEXCOORD_INDEX, /* 11, D3DTSS_TEXCOORDINDEX */
2240 WINED3D_TSS_INVALID, /* 12, unused */
2241 WINED3D_TSS_INVALID, /* 13, unused */
2242 WINED3D_TSS_INVALID, /* 14, unused */
2243 WINED3D_TSS_INVALID, /* 15, unused */
2244 WINED3D_TSS_INVALID, /* 16, unused */
2245 WINED3D_TSS_INVALID, /* 17, unused */
2246 WINED3D_TSS_INVALID, /* 18, unused */
2247 WINED3D_TSS_INVALID, /* 19, unused */
2248 WINED3D_TSS_INVALID, /* 20, unused */
2249 WINED3D_TSS_INVALID, /* 21, unused */
2250 WINED3D_TSS_BUMPENV_LSCALE, /* 22, D3DTSS_BUMPENVLSCALE */
2251 WINED3D_TSS_BUMPENV_LOFFSET, /* 23, D3DTSS_BUMPENVLOFFSET */
2252 WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS, /* 24, D3DTSS_TEXTURETRANSFORMFLAGS */
2253 WINED3D_TSS_INVALID, /* 25, unused */
2254 WINED3D_TSS_COLOR_ARG0, /* 26, D3DTSS_COLORARG0 */
2255 WINED3D_TSS_ALPHA_ARG0, /* 27, D3DTSS_ALPHAARG0 */
2256 WINED3D_TSS_RESULT_ARG, /* 28, D3DTSS_RESULTARG */
2257 WINED3D_TSS_INVALID, /* 29, unused */
2258 WINED3D_TSS_INVALID, /* 30, unused */
2259 WINED3D_TSS_INVALID, /* 31, unused */
2260 WINED3D_TSS_CONSTANT, /* 32, D3DTSS_CONSTANT */
2263 static HRESULT WINAPI d3d9_device_GetTextureStageState(IDirect3DDevice9Ex *iface,
2264 DWORD stage, D3DTEXTURESTAGESTATETYPE state, DWORD *value)
2266 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2268 TRACE("iface %p, stage %u, state %#x, value %p.\n", iface, stage, state, value);
2270 if (state >= sizeof(tss_lookup) / sizeof(*tss_lookup))
2272 WARN("Invalid state %#x passed.\n", state);
2273 return D3D_OK;
2276 wined3d_mutex_lock();
2277 *value = wined3d_device_get_texture_stage_state(device->wined3d_device, stage, tss_lookup[state]);
2278 wined3d_mutex_unlock();
2280 return D3D_OK;
2283 static HRESULT WINAPI d3d9_device_SetTextureStageState(IDirect3DDevice9Ex *iface,
2284 DWORD stage, D3DTEXTURESTAGESTATETYPE state, DWORD value)
2286 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2288 TRACE("iface %p, stage %u, state %#x, value %#x.\n", iface, stage, state, value);
2290 if (state >= sizeof(tss_lookup) / sizeof(*tss_lookup))
2292 WARN("Invalid state %#x passed.\n", state);
2293 return D3D_OK;
2296 wined3d_mutex_lock();
2297 wined3d_device_set_texture_stage_state(device->wined3d_device, stage, tss_lookup[state], value);
2298 wined3d_mutex_unlock();
2300 return D3D_OK;
2303 static HRESULT WINAPI d3d9_device_GetSamplerState(IDirect3DDevice9Ex *iface,
2304 DWORD sampler, D3DSAMPLERSTATETYPE state, DWORD *value)
2306 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2308 TRACE("iface %p, sampler %u, state %#x, value %p.\n", iface, sampler, state, value);
2310 wined3d_mutex_lock();
2311 *value = wined3d_device_get_sampler_state(device->wined3d_device, sampler, state);
2312 wined3d_mutex_unlock();
2314 return D3D_OK;
2317 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_SetSamplerState(IDirect3DDevice9Ex *iface,
2318 DWORD sampler, D3DSAMPLERSTATETYPE state, DWORD value)
2320 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2322 TRACE("iface %p, sampler %u, state %#x, value %#x.\n", iface, sampler, state, value);
2324 wined3d_mutex_lock();
2325 wined3d_device_set_sampler_state(device->wined3d_device, sampler, state, value);
2326 wined3d_mutex_unlock();
2328 return D3D_OK;
2331 static HRESULT WINAPI d3d9_device_ValidateDevice(IDirect3DDevice9Ex *iface, DWORD *pass_count)
2333 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2334 HRESULT hr;
2336 TRACE("iface %p, pass_count %p.\n", iface, pass_count);
2338 wined3d_mutex_lock();
2339 hr = wined3d_device_validate_device(device->wined3d_device, pass_count);
2340 wined3d_mutex_unlock();
2342 return hr;
2345 static HRESULT WINAPI d3d9_device_SetPaletteEntries(IDirect3DDevice9Ex *iface,
2346 UINT palette_idx, const PALETTEENTRY *entries)
2348 WARN("iface %p, palette_idx %u, entries %p unimplemented.\n", iface, palette_idx, entries);
2350 /* The d3d9 palette API is non-functional on Windows. Getters and setters are implemented,
2351 * and some drivers allow the creation of P8 surfaces. These surfaces can be copied to
2352 * other P8 surfaces with StretchRect, but cannot be converted to (A)RGB.
2354 * Some older(dx7) cards may have support for P8 textures, but games cannot rely on this. */
2355 return D3D_OK;
2358 static HRESULT WINAPI d3d9_device_GetPaletteEntries(IDirect3DDevice9Ex *iface,
2359 UINT palette_idx, PALETTEENTRY *entries)
2361 FIXME("iface %p, palette_idx %u, entries %p unimplemented.\n", iface, palette_idx, entries);
2363 return D3DERR_INVALIDCALL;
2366 static HRESULT WINAPI d3d9_device_SetCurrentTexturePalette(IDirect3DDevice9Ex *iface, UINT palette_idx)
2368 WARN("iface %p, palette_idx %u unimplemented.\n", iface, palette_idx);
2370 return D3D_OK;
2373 static HRESULT WINAPI d3d9_device_GetCurrentTexturePalette(IDirect3DDevice9Ex *iface, UINT *palette_idx)
2375 FIXME("iface %p, palette_idx %p.\n", iface, palette_idx);
2377 return D3DERR_INVALIDCALL;
2380 static HRESULT WINAPI d3d9_device_SetScissorRect(IDirect3DDevice9Ex *iface, const RECT *rect)
2382 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2384 TRACE("iface %p, rect %p.\n", iface, rect);
2386 wined3d_mutex_lock();
2387 wined3d_device_set_scissor_rect(device->wined3d_device, rect);
2388 wined3d_mutex_unlock();
2390 return D3D_OK;
2393 static HRESULT WINAPI d3d9_device_GetScissorRect(IDirect3DDevice9Ex *iface, RECT *rect)
2395 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2397 TRACE("iface %p, rect %p.\n", iface, rect);
2399 wined3d_mutex_lock();
2400 wined3d_device_get_scissor_rect(device->wined3d_device, rect);
2401 wined3d_mutex_unlock();
2403 return D3D_OK;
2406 static HRESULT WINAPI d3d9_device_SetSoftwareVertexProcessing(IDirect3DDevice9Ex *iface, BOOL software)
2408 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2410 TRACE("iface %p, software %#x.\n", iface, software);
2412 wined3d_mutex_lock();
2413 wined3d_device_set_software_vertex_processing(device->wined3d_device, software);
2414 wined3d_mutex_unlock();
2416 return D3D_OK;
2419 static BOOL WINAPI d3d9_device_GetSoftwareVertexProcessing(IDirect3DDevice9Ex *iface)
2421 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2422 BOOL ret;
2424 TRACE("iface %p.\n", iface);
2426 wined3d_mutex_lock();
2427 ret = wined3d_device_get_software_vertex_processing(device->wined3d_device);
2428 wined3d_mutex_unlock();
2430 return ret;
2433 static HRESULT WINAPI d3d9_device_SetNPatchMode(IDirect3DDevice9Ex *iface, float segment_count)
2435 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2436 HRESULT hr;
2438 TRACE("iface %p, segment_count %.8e.\n", iface, segment_count);
2440 wined3d_mutex_lock();
2441 hr = wined3d_device_set_npatch_mode(device->wined3d_device, segment_count);
2442 wined3d_mutex_unlock();
2444 return hr;
2447 static float WINAPI d3d9_device_GetNPatchMode(IDirect3DDevice9Ex *iface)
2449 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2450 float ret;
2452 TRACE("iface %p.\n", iface);
2454 wined3d_mutex_lock();
2455 ret = wined3d_device_get_npatch_mode(device->wined3d_device);
2456 wined3d_mutex_unlock();
2458 return ret;
2461 static HRESULT WINAPI d3d9_device_DrawPrimitive(IDirect3DDevice9Ex *iface,
2462 D3DPRIMITIVETYPE primitive_type, UINT start_vertex, UINT primitive_count)
2464 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2465 HRESULT hr;
2467 TRACE("iface %p, primitive_type %#x, start_vertex %u, primitive_count %u.\n",
2468 iface, primitive_type, start_vertex, primitive_count);
2470 wined3d_mutex_lock();
2471 if (!device->has_vertex_declaration)
2473 wined3d_mutex_unlock();
2474 WARN("Called without a valid vertex declaration set.\n");
2475 return D3DERR_INVALIDCALL;
2477 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
2478 hr = wined3d_device_draw_primitive(device->wined3d_device, start_vertex,
2479 vertex_count_from_primitive_count(primitive_type, primitive_count));
2480 wined3d_mutex_unlock();
2482 return hr;
2485 static HRESULT WINAPI d3d9_device_DrawIndexedPrimitive(IDirect3DDevice9Ex *iface,
2486 D3DPRIMITIVETYPE primitive_type, INT base_vertex_idx, UINT min_vertex_idx,
2487 UINT vertex_count, UINT start_idx, UINT primitive_count)
2489 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2490 HRESULT hr;
2492 TRACE("iface %p, primitive_type %#x, base_vertex_idx %u, min_vertex_idx %u, "
2493 "vertex_count %u, start_idx %u, primitive_count %u.\n",
2494 iface, primitive_type, base_vertex_idx, min_vertex_idx,
2495 vertex_count, start_idx, primitive_count);
2497 wined3d_mutex_lock();
2498 if (!device->has_vertex_declaration)
2500 wined3d_mutex_unlock();
2501 WARN("Called without a valid vertex declaration set.\n");
2502 return D3DERR_INVALIDCALL;
2504 wined3d_device_set_base_vertex_index(device->wined3d_device, base_vertex_idx);
2505 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
2506 hr = wined3d_device_draw_indexed_primitive(device->wined3d_device, start_idx,
2507 vertex_count_from_primitive_count(primitive_type, primitive_count));
2508 wined3d_mutex_unlock();
2510 return hr;
2513 /* The caller is responsible for wined3d locking */
2514 static HRESULT d3d9_device_prepare_vertex_buffer(struct d3d9_device *device, UINT min_size)
2516 HRESULT hr;
2518 if (device->vertex_buffer_size < min_size || !device->vertex_buffer)
2520 UINT size = max(device->vertex_buffer_size * 2, min_size);
2521 struct wined3d_buffer *buffer;
2523 TRACE("Growing vertex buffer to %u bytes.\n", size);
2525 hr = wined3d_buffer_create_vb(device->wined3d_device, size, WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_WRITEONLY,
2526 WINED3D_POOL_DEFAULT, NULL, &d3d9_null_wined3d_parent_ops, &buffer);
2527 if (FAILED(hr))
2529 ERR("(%p) wined3d_buffer_create_vb failed with hr = %08x.\n", device, hr);
2530 return hr;
2533 if (device->vertex_buffer)
2534 wined3d_buffer_decref(device->vertex_buffer);
2536 device->vertex_buffer = buffer;
2537 device->vertex_buffer_size = size;
2538 device->vertex_buffer_pos = 0;
2540 return D3D_OK;
2543 static HRESULT WINAPI d3d9_device_DrawPrimitiveUP(IDirect3DDevice9Ex *iface,
2544 D3DPRIMITIVETYPE primitive_type, UINT primitive_count, const void *data, UINT stride)
2546 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2547 HRESULT hr;
2548 UINT vtx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2549 struct wined3d_map_desc wined3d_map_desc;
2550 struct wined3d_box wined3d_box = {0};
2551 UINT size = vtx_count * stride;
2552 struct wined3d_resource *vb;
2553 UINT vb_pos, align;
2555 TRACE("iface %p, primitive_type %#x, primitive_count %u, data %p, stride %u.\n",
2556 iface, primitive_type, primitive_count, data, stride);
2558 if (!primitive_count)
2560 WARN("primitive_count is 0, returning D3D_OK\n");
2561 return D3D_OK;
2564 wined3d_mutex_lock();
2566 if (!device->has_vertex_declaration)
2568 wined3d_mutex_unlock();
2569 WARN("Called without a valid vertex declaration set.\n");
2570 return D3DERR_INVALIDCALL;
2573 hr = d3d9_device_prepare_vertex_buffer(device, size);
2574 if (FAILED(hr))
2575 goto done;
2577 vb_pos = device->vertex_buffer_pos;
2578 align = vb_pos % stride;
2579 if (align) align = stride - align;
2580 if (vb_pos + size + align > device->vertex_buffer_size)
2581 vb_pos = 0;
2582 else
2583 vb_pos += align;
2585 wined3d_box.left = vb_pos;
2586 wined3d_box.right = vb_pos + size;
2587 vb = wined3d_buffer_get_resource(device->vertex_buffer);
2588 if (FAILED(hr = wined3d_resource_map(vb, 0, &wined3d_map_desc, &wined3d_box,
2589 vb_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD)))
2590 goto done;
2591 memcpy(wined3d_map_desc.data, data, size);
2592 wined3d_resource_unmap(vb, 0);
2593 device->vertex_buffer_pos = vb_pos + size;
2595 hr = wined3d_device_set_stream_source(device->wined3d_device, 0, device->vertex_buffer, 0, stride);
2596 if (FAILED(hr))
2597 goto done;
2599 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
2600 hr = wined3d_device_draw_primitive(device->wined3d_device, vb_pos / stride, vtx_count);
2601 wined3d_device_set_stream_source(device->wined3d_device, 0, NULL, 0, 0);
2603 done:
2604 wined3d_mutex_unlock();
2605 return hr;
2608 /* The caller is responsible for wined3d locking */
2609 static HRESULT d3d9_device_prepare_index_buffer(struct d3d9_device *device, UINT min_size)
2611 HRESULT hr;
2613 if (device->index_buffer_size < min_size || !device->index_buffer)
2615 UINT size = max(device->index_buffer_size * 2, min_size);
2616 struct wined3d_buffer *buffer;
2618 TRACE("Growing index buffer to %u bytes.\n", size);
2620 hr = wined3d_buffer_create_ib(device->wined3d_device, size, WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_WRITEONLY,
2621 WINED3D_POOL_DEFAULT, NULL, &d3d9_null_wined3d_parent_ops, &buffer);
2622 if (FAILED(hr))
2624 ERR("(%p) wined3d_buffer_create_ib failed with hr = %08x.\n", device, hr);
2625 return hr;
2628 if (device->index_buffer)
2629 wined3d_buffer_decref(device->index_buffer);
2631 device->index_buffer = buffer;
2632 device->index_buffer_size = size;
2633 device->index_buffer_pos = 0;
2635 return D3D_OK;
2638 static HRESULT WINAPI d3d9_device_DrawIndexedPrimitiveUP(IDirect3DDevice9Ex *iface,
2639 D3DPRIMITIVETYPE primitive_type, UINT min_vertex_idx, UINT vertex_count,
2640 UINT primitive_count, const void *index_data, D3DFORMAT index_format,
2641 const void *vertex_data, UINT vertex_stride)
2643 UINT idx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2644 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2645 UINT idx_fmt_size = index_format == D3DFMT_INDEX16 ? 2 : 4;
2646 UINT vtx_size = vertex_count * vertex_stride;
2647 UINT idx_size = idx_count * idx_fmt_size;
2648 struct wined3d_map_desc wined3d_map_desc;
2649 struct wined3d_box wined3d_box = {0};
2650 struct wined3d_resource *ib, *vb;
2651 UINT vb_pos, ib_pos, align;
2652 HRESULT hr;
2654 TRACE("iface %p, primitive_type %#x, min_vertex_idx %u, vertex_count %u, primitive_count %u, "
2655 "index_data %p, index_format %#x, vertex_data %p, vertex_stride %u.\n",
2656 iface, primitive_type, min_vertex_idx, vertex_count, primitive_count,
2657 index_data, index_format, vertex_data, vertex_stride);
2659 if (!primitive_count)
2661 WARN("primitive_count is 0, returning D3D_OK.\n");
2662 return D3D_OK;
2665 wined3d_mutex_lock();
2667 if (!device->has_vertex_declaration)
2669 wined3d_mutex_unlock();
2670 WARN("Called without a valid vertex declaration set.\n");
2671 return D3DERR_INVALIDCALL;
2674 hr = d3d9_device_prepare_vertex_buffer(device, vtx_size);
2675 if (FAILED(hr))
2676 goto done;
2678 vb_pos = device->vertex_buffer_pos;
2679 align = vb_pos % vertex_stride;
2680 if (align) align = vertex_stride - align;
2681 if (vb_pos + vtx_size + align > device->vertex_buffer_size)
2682 vb_pos = 0;
2683 else
2684 vb_pos += align;
2686 wined3d_box.left = vb_pos;
2687 wined3d_box.right = vb_pos + vtx_size;
2688 vb = wined3d_buffer_get_resource(device->vertex_buffer);
2689 if (FAILED(hr = wined3d_resource_map(vb, 0, &wined3d_map_desc, &wined3d_box,
2690 vb_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD)))
2691 goto done;
2692 memcpy(wined3d_map_desc.data, (char *)vertex_data + min_vertex_idx * vertex_stride, vtx_size);
2693 wined3d_resource_unmap(vb, 0);
2694 device->vertex_buffer_pos = vb_pos + vtx_size;
2696 hr = d3d9_device_prepare_index_buffer(device, idx_size);
2697 if (FAILED(hr))
2698 goto done;
2700 ib_pos = device->index_buffer_pos;
2701 align = ib_pos % idx_fmt_size;
2702 if (align) align = idx_fmt_size - align;
2703 if (ib_pos + idx_size + align > device->index_buffer_size)
2704 ib_pos = 0;
2705 else
2706 ib_pos += align;
2708 wined3d_box.left = ib_pos;
2709 wined3d_box.right = ib_pos + idx_size;
2710 ib = wined3d_buffer_get_resource(device->index_buffer);
2711 if (FAILED(hr = wined3d_resource_map(ib, 0, &wined3d_map_desc, &wined3d_box,
2712 ib_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD)))
2713 goto done;
2714 memcpy(wined3d_map_desc.data, index_data, idx_size);
2715 wined3d_resource_unmap(ib, 0);
2716 device->index_buffer_pos = ib_pos + idx_size;
2718 hr = wined3d_device_set_stream_source(device->wined3d_device, 0, device->vertex_buffer, 0, vertex_stride);
2719 if (FAILED(hr))
2720 goto done;
2722 wined3d_device_set_index_buffer(device->wined3d_device, device->index_buffer,
2723 wined3dformat_from_d3dformat(index_format), 0);
2724 wined3d_device_set_base_vertex_index(device->wined3d_device, vb_pos / vertex_stride - min_vertex_idx);
2726 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
2727 hr = wined3d_device_draw_indexed_primitive(device->wined3d_device, ib_pos / idx_fmt_size, idx_count);
2729 wined3d_device_set_stream_source(device->wined3d_device, 0, NULL, 0, 0);
2730 wined3d_device_set_index_buffer(device->wined3d_device, NULL, WINED3DFMT_UNKNOWN, 0);
2731 wined3d_device_set_base_vertex_index(device->wined3d_device, 0);
2733 done:
2734 wined3d_mutex_unlock();
2735 return hr;
2738 static HRESULT WINAPI d3d9_device_ProcessVertices(IDirect3DDevice9Ex *iface,
2739 UINT src_start_idx, UINT dst_idx, UINT vertex_count, IDirect3DVertexBuffer9 *dst_buffer,
2740 IDirect3DVertexDeclaration9 *declaration, DWORD flags)
2742 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2743 struct d3d9_vertexbuffer *dst_impl = unsafe_impl_from_IDirect3DVertexBuffer9(dst_buffer);
2744 struct d3d9_vertex_declaration *decl_impl = unsafe_impl_from_IDirect3DVertexDeclaration9(declaration);
2745 HRESULT hr;
2747 TRACE("iface %p, src_start_idx %u, dst_idx %u, vertex_count %u, dst_buffer %p, declaration %p, flags %#x.\n",
2748 iface, src_start_idx, dst_idx, vertex_count, dst_buffer, declaration, flags);
2750 wined3d_mutex_lock();
2751 hr = wined3d_device_process_vertices(device->wined3d_device, src_start_idx, dst_idx, vertex_count,
2752 dst_impl->wined3d_buffer, decl_impl ? decl_impl->wined3d_declaration : NULL,
2753 flags, dst_impl->fvf);
2754 wined3d_mutex_unlock();
2756 return hr;
2759 static HRESULT WINAPI d3d9_device_CreateVertexDeclaration(IDirect3DDevice9Ex *iface,
2760 const D3DVERTEXELEMENT9 *elements, IDirect3DVertexDeclaration9 **declaration)
2762 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2763 struct d3d9_vertex_declaration *object;
2764 HRESULT hr;
2766 TRACE("iface %p, elements %p, declaration %p.\n", iface, elements, declaration);
2768 if (!declaration)
2770 WARN("Caller passed a NULL declaration, returning D3DERR_INVALIDCALL.\n");
2771 return D3DERR_INVALIDCALL;
2774 if (SUCCEEDED(hr = d3d9_vertex_declaration_create(device, elements, &object)))
2775 *declaration = &object->IDirect3DVertexDeclaration9_iface;
2777 return hr;
2780 static HRESULT WINAPI d3d9_device_SetVertexDeclaration(IDirect3DDevice9Ex *iface,
2781 IDirect3DVertexDeclaration9 *declaration)
2783 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2784 struct d3d9_vertex_declaration *decl_impl = unsafe_impl_from_IDirect3DVertexDeclaration9(declaration);
2786 TRACE("iface %p, declaration %p.\n", iface, declaration);
2788 wined3d_mutex_lock();
2789 wined3d_device_set_vertex_declaration(device->wined3d_device,
2790 decl_impl ? decl_impl->wined3d_declaration : NULL);
2791 device->has_vertex_declaration = !!decl_impl;
2792 wined3d_mutex_unlock();
2794 return D3D_OK;
2797 static HRESULT WINAPI d3d9_device_GetVertexDeclaration(IDirect3DDevice9Ex *iface,
2798 IDirect3DVertexDeclaration9 **declaration)
2800 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2801 struct wined3d_vertex_declaration *wined3d_declaration;
2802 struct d3d9_vertex_declaration *declaration_impl;
2804 TRACE("iface %p, declaration %p.\n", iface, declaration);
2806 if (!declaration) return D3DERR_INVALIDCALL;
2808 wined3d_mutex_lock();
2809 if ((wined3d_declaration = wined3d_device_get_vertex_declaration(device->wined3d_device)))
2811 declaration_impl = wined3d_vertex_declaration_get_parent(wined3d_declaration);
2812 *declaration = &declaration_impl->IDirect3DVertexDeclaration9_iface;
2813 IDirect3DVertexDeclaration9_AddRef(*declaration);
2815 else
2817 *declaration = NULL;
2819 wined3d_mutex_unlock();
2821 TRACE("Returning %p.\n", *declaration);
2822 return D3D_OK;
2825 static struct wined3d_vertex_declaration *device_get_fvf_declaration(struct d3d9_device *device, DWORD fvf)
2827 struct wined3d_vertex_declaration *wined3d_declaration;
2828 struct fvf_declaration *fvf_decls = device->fvf_decls;
2829 struct d3d9_vertex_declaration *d3d9_declaration;
2830 D3DVERTEXELEMENT9 *elements;
2831 int p, low, high; /* deliberately signed */
2832 HRESULT hr;
2834 TRACE("Searching for declaration for fvf %08x... ", fvf);
2836 low = 0;
2837 high = device->fvf_decl_count - 1;
2838 while (low <= high)
2840 p = (low + high) >> 1;
2841 TRACE("%d ", p);
2843 if (fvf_decls[p].fvf == fvf)
2845 TRACE("found %p.\n", fvf_decls[p].decl);
2846 return fvf_decls[p].decl;
2849 if (fvf_decls[p].fvf < fvf)
2850 low = p + 1;
2851 else
2852 high = p - 1;
2854 TRACE("not found. Creating and inserting at position %d.\n", low);
2856 if (FAILED(hr = vdecl_convert_fvf(fvf, &elements)))
2857 return NULL;
2859 hr = d3d9_vertex_declaration_create(device, elements, &d3d9_declaration);
2860 HeapFree(GetProcessHeap(), 0, elements);
2861 if (FAILED(hr))
2862 return NULL;
2864 if (device->fvf_decl_size == device->fvf_decl_count)
2866 UINT grow = max(device->fvf_decl_size / 2, 8);
2868 fvf_decls = HeapReAlloc(GetProcessHeap(), 0, fvf_decls, sizeof(*fvf_decls) * (device->fvf_decl_size + grow));
2869 if (!fvf_decls)
2871 IDirect3DVertexDeclaration9_Release(&d3d9_declaration->IDirect3DVertexDeclaration9_iface);
2872 return NULL;
2874 device->fvf_decls = fvf_decls;
2875 device->fvf_decl_size += grow;
2878 d3d9_declaration->fvf = fvf;
2879 wined3d_declaration = d3d9_declaration->wined3d_declaration;
2880 wined3d_vertex_declaration_incref(wined3d_declaration);
2881 IDirect3DVertexDeclaration9_Release(&d3d9_declaration->IDirect3DVertexDeclaration9_iface);
2883 memmove(fvf_decls + low + 1, fvf_decls + low, sizeof(*fvf_decls) * (device->fvf_decl_count - low));
2884 fvf_decls[low].decl = wined3d_declaration;
2885 fvf_decls[low].fvf = fvf;
2886 ++device->fvf_decl_count;
2888 TRACE("Returning %p. %u declarations in array.\n", wined3d_declaration, device->fvf_decl_count);
2890 return wined3d_declaration;
2893 static HRESULT WINAPI d3d9_device_SetFVF(IDirect3DDevice9Ex *iface, DWORD fvf)
2895 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2896 struct wined3d_vertex_declaration *decl;
2898 TRACE("iface %p, fvf %#x.\n", iface, fvf);
2900 if (!fvf)
2902 WARN("%#x is not a valid FVF.\n", fvf);
2903 return D3D_OK;
2906 wined3d_mutex_lock();
2907 if (!(decl = device_get_fvf_declaration(device, fvf)))
2909 wined3d_mutex_unlock();
2910 ERR("Failed to create a vertex declaration for fvf %#x.\n", fvf);
2911 return D3DERR_DRIVERINTERNALERROR;
2914 wined3d_device_set_vertex_declaration(device->wined3d_device, decl);
2915 device->has_vertex_declaration = TRUE;
2916 wined3d_mutex_unlock();
2918 return D3D_OK;
2921 static HRESULT WINAPI d3d9_device_GetFVF(IDirect3DDevice9Ex *iface, DWORD *fvf)
2923 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2924 struct wined3d_vertex_declaration *wined3d_declaration;
2925 struct d3d9_vertex_declaration *d3d9_declaration;
2927 TRACE("iface %p, fvf %p.\n", iface, fvf);
2929 wined3d_mutex_lock();
2930 if ((wined3d_declaration = wined3d_device_get_vertex_declaration(device->wined3d_device)))
2932 d3d9_declaration = wined3d_vertex_declaration_get_parent(wined3d_declaration);
2933 *fvf = d3d9_declaration->fvf;
2935 else
2937 *fvf = 0;
2939 wined3d_mutex_unlock();
2941 TRACE("Returning FVF %#x.\n", *fvf);
2943 return D3D_OK;
2946 static HRESULT WINAPI d3d9_device_CreateVertexShader(IDirect3DDevice9Ex *iface,
2947 const DWORD *byte_code, IDirect3DVertexShader9 **shader)
2949 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2950 struct d3d9_vertexshader *object;
2951 HRESULT hr;
2953 TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
2955 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2956 if (!object)
2957 return E_OUTOFMEMORY;
2959 hr = vertexshader_init(object, device, byte_code);
2960 if (FAILED(hr))
2962 WARN("Failed to initialize vertex shader, hr %#x.\n", hr);
2963 HeapFree(GetProcessHeap(), 0, object);
2964 return hr;
2967 TRACE("Created vertex shader %p.\n", object);
2968 *shader = &object->IDirect3DVertexShader9_iface;
2970 return D3D_OK;
2973 static HRESULT WINAPI d3d9_device_SetVertexShader(IDirect3DDevice9Ex *iface, IDirect3DVertexShader9 *shader)
2975 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2976 struct d3d9_vertexshader *shader_obj = unsafe_impl_from_IDirect3DVertexShader9(shader);
2978 TRACE("iface %p, shader %p.\n", iface, shader);
2980 wined3d_mutex_lock();
2981 wined3d_device_set_vertex_shader(device->wined3d_device,
2982 shader_obj ? shader_obj->wined3d_shader : NULL);
2983 wined3d_mutex_unlock();
2985 return D3D_OK;
2988 static HRESULT WINAPI d3d9_device_GetVertexShader(IDirect3DDevice9Ex *iface, IDirect3DVertexShader9 **shader)
2990 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2991 struct d3d9_vertexshader *shader_impl;
2992 struct wined3d_shader *wined3d_shader;
2994 TRACE("iface %p, shader %p.\n", iface, shader);
2996 wined3d_mutex_lock();
2997 if ((wined3d_shader = wined3d_device_get_vertex_shader(device->wined3d_device)))
2999 shader_impl = wined3d_shader_get_parent(wined3d_shader);
3000 *shader = &shader_impl->IDirect3DVertexShader9_iface;
3001 IDirect3DVertexShader9_AddRef(*shader);
3003 else
3005 *shader = NULL;
3007 wined3d_mutex_unlock();
3009 TRACE("Returning %p.\n", *shader);
3011 return D3D_OK;
3014 static HRESULT WINAPI d3d9_device_SetVertexShaderConstantF(IDirect3DDevice9Ex *iface,
3015 UINT reg_idx, const float *data, UINT count)
3017 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3018 HRESULT hr;
3020 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3022 if (reg_idx + count > D3D9_MAX_VERTEX_SHADER_CONSTANTF)
3024 WARN("Trying to access %u constants, but d3d9 only supports %u\n",
3025 reg_idx + count, D3D9_MAX_VERTEX_SHADER_CONSTANTF);
3026 return D3DERR_INVALIDCALL;
3029 wined3d_mutex_lock();
3030 hr = wined3d_device_set_vs_consts_f(device->wined3d_device,
3031 reg_idx, count, (const struct wined3d_vec4 *)data);
3032 wined3d_mutex_unlock();
3034 return hr;
3037 static HRESULT WINAPI d3d9_device_GetVertexShaderConstantF(IDirect3DDevice9Ex *iface,
3038 UINT reg_idx, float *data, UINT count)
3040 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3041 HRESULT hr;
3043 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3045 if (reg_idx + count > D3D9_MAX_VERTEX_SHADER_CONSTANTF)
3047 WARN("Trying to access %u constants, but d3d9 only supports %u\n",
3048 reg_idx + count, D3D9_MAX_VERTEX_SHADER_CONSTANTF);
3049 return D3DERR_INVALIDCALL;
3052 wined3d_mutex_lock();
3053 hr = wined3d_device_get_vs_consts_f(device->wined3d_device,
3054 reg_idx, count, (struct wined3d_vec4 *)data);
3055 wined3d_mutex_unlock();
3057 return hr;
3060 static HRESULT WINAPI d3d9_device_SetVertexShaderConstantI(IDirect3DDevice9Ex *iface,
3061 UINT reg_idx, const int *data, UINT count)
3063 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3064 HRESULT hr;
3066 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3068 wined3d_mutex_lock();
3069 hr = wined3d_device_set_vs_consts_i(device->wined3d_device,
3070 reg_idx, count, (const struct wined3d_ivec4 *)data);
3071 wined3d_mutex_unlock();
3073 return hr;
3076 static HRESULT WINAPI d3d9_device_GetVertexShaderConstantI(IDirect3DDevice9Ex *iface,
3077 UINT reg_idx, int *data, UINT count)
3079 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3080 HRESULT hr;
3082 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3084 wined3d_mutex_lock();
3085 hr = wined3d_device_get_vs_consts_i(device->wined3d_device,
3086 reg_idx, count, (struct wined3d_ivec4 *)data);
3087 wined3d_mutex_unlock();
3089 return hr;
3092 static HRESULT WINAPI d3d9_device_SetVertexShaderConstantB(IDirect3DDevice9Ex *iface,
3093 UINT reg_idx, const BOOL *data, UINT count)
3095 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3096 HRESULT hr;
3098 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3100 wined3d_mutex_lock();
3101 hr = wined3d_device_set_vs_consts_b(device->wined3d_device, reg_idx, count, data);
3102 wined3d_mutex_unlock();
3104 return hr;
3107 static HRESULT WINAPI d3d9_device_GetVertexShaderConstantB(IDirect3DDevice9Ex *iface,
3108 UINT reg_idx, BOOL *data, UINT count)
3110 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3111 HRESULT hr;
3113 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3115 wined3d_mutex_lock();
3116 hr = wined3d_device_get_vs_consts_b(device->wined3d_device, reg_idx, count, data);
3117 wined3d_mutex_unlock();
3119 return hr;
3122 static HRESULT WINAPI d3d9_device_SetStreamSource(IDirect3DDevice9Ex *iface,
3123 UINT stream_idx, IDirect3DVertexBuffer9 *buffer, UINT offset, UINT stride)
3125 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3126 struct d3d9_vertexbuffer *buffer_impl = unsafe_impl_from_IDirect3DVertexBuffer9(buffer);
3127 HRESULT hr;
3129 TRACE("iface %p, stream_idx %u, buffer %p, offset %u, stride %u.\n",
3130 iface, stream_idx, buffer, offset, stride);
3132 wined3d_mutex_lock();
3133 hr = wined3d_device_set_stream_source(device->wined3d_device, stream_idx,
3134 buffer_impl ? buffer_impl->wined3d_buffer : NULL, offset, stride);
3135 wined3d_mutex_unlock();
3137 return hr;
3140 static HRESULT WINAPI d3d9_device_GetStreamSource(IDirect3DDevice9Ex *iface,
3141 UINT stream_idx, IDirect3DVertexBuffer9 **buffer, UINT *offset, UINT *stride)
3143 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3144 struct d3d9_vertexbuffer *buffer_impl;
3145 struct wined3d_buffer *wined3d_buffer;
3146 HRESULT hr;
3148 TRACE("iface %p, stream_idx %u, buffer %p, offset %p, stride %p.\n",
3149 iface, stream_idx, buffer, offset, stride);
3151 if (!buffer)
3152 return D3DERR_INVALIDCALL;
3154 wined3d_mutex_lock();
3155 hr = wined3d_device_get_stream_source(device->wined3d_device, stream_idx, &wined3d_buffer, offset, stride);
3156 if (SUCCEEDED(hr) && wined3d_buffer)
3158 buffer_impl = wined3d_buffer_get_parent(wined3d_buffer);
3159 *buffer = &buffer_impl->IDirect3DVertexBuffer9_iface;
3160 IDirect3DVertexBuffer9_AddRef(*buffer);
3162 else
3164 if (FAILED(hr))
3165 FIXME("Call to GetStreamSource failed %p %p\n", offset, stride);
3166 *buffer = NULL;
3168 wined3d_mutex_unlock();
3170 return hr;
3173 static HRESULT WINAPI d3d9_device_SetStreamSourceFreq(IDirect3DDevice9Ex *iface, UINT stream_idx, UINT freq)
3175 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3176 HRESULT hr;
3178 TRACE("iface %p, stream_idx %u, freq %u.\n", iface, stream_idx, freq);
3180 wined3d_mutex_lock();
3181 hr = wined3d_device_set_stream_source_freq(device->wined3d_device, stream_idx, freq);
3182 wined3d_mutex_unlock();
3184 return hr;
3187 static HRESULT WINAPI d3d9_device_GetStreamSourceFreq(IDirect3DDevice9Ex *iface, UINT stream_idx, UINT *freq)
3189 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3190 HRESULT hr;
3192 TRACE("iface %p, stream_idx %u, freq %p.\n", iface, stream_idx, freq);
3194 wined3d_mutex_lock();
3195 hr = wined3d_device_get_stream_source_freq(device->wined3d_device, stream_idx, freq);
3196 wined3d_mutex_unlock();
3198 return hr;
3201 static HRESULT WINAPI d3d9_device_SetIndices(IDirect3DDevice9Ex *iface, IDirect3DIndexBuffer9 *buffer)
3203 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3204 struct d3d9_indexbuffer *ib = unsafe_impl_from_IDirect3DIndexBuffer9(buffer);
3206 TRACE("iface %p, buffer %p.\n", iface, buffer);
3208 wined3d_mutex_lock();
3209 wined3d_device_set_index_buffer(device->wined3d_device,
3210 ib ? ib->wined3d_buffer : NULL, ib ? ib->format : WINED3DFMT_UNKNOWN, 0);
3211 wined3d_mutex_unlock();
3213 return D3D_OK;
3216 static HRESULT WINAPI d3d9_device_GetIndices(IDirect3DDevice9Ex *iface, IDirect3DIndexBuffer9 **buffer)
3218 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3219 enum wined3d_format_id wined3d_format;
3220 struct wined3d_buffer *wined3d_buffer;
3221 struct d3d9_indexbuffer *buffer_impl;
3223 TRACE("iface %p, buffer %p.\n", iface, buffer);
3225 if (!buffer)
3226 return D3DERR_INVALIDCALL;
3228 wined3d_mutex_lock();
3229 if ((wined3d_buffer = wined3d_device_get_index_buffer(device->wined3d_device, &wined3d_format, NULL)))
3231 buffer_impl = wined3d_buffer_get_parent(wined3d_buffer);
3232 *buffer = &buffer_impl->IDirect3DIndexBuffer9_iface;
3233 IDirect3DIndexBuffer9_AddRef(*buffer);
3235 else
3237 *buffer = NULL;
3239 wined3d_mutex_unlock();
3241 return D3D_OK;
3244 static HRESULT WINAPI d3d9_device_CreatePixelShader(IDirect3DDevice9Ex *iface,
3245 const DWORD *byte_code, IDirect3DPixelShader9 **shader)
3247 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3248 struct d3d9_pixelshader *object;
3249 HRESULT hr;
3251 TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
3253 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
3254 if (!object)
3256 FIXME("Failed to allocate pixel shader memory.\n");
3257 return E_OUTOFMEMORY;
3260 hr = pixelshader_init(object, device, byte_code);
3261 if (FAILED(hr))
3263 WARN("Failed to initialize pixel shader, hr %#x.\n", hr);
3264 HeapFree(GetProcessHeap(), 0, object);
3265 return hr;
3268 TRACE("Created pixel shader %p.\n", object);
3269 *shader = &object->IDirect3DPixelShader9_iface;
3271 return D3D_OK;
3274 static HRESULT WINAPI d3d9_device_SetPixelShader(IDirect3DDevice9Ex *iface, IDirect3DPixelShader9 *shader)
3276 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3277 struct d3d9_pixelshader *shader_obj = unsafe_impl_from_IDirect3DPixelShader9(shader);
3279 TRACE("iface %p, shader %p.\n", iface, shader);
3281 wined3d_mutex_lock();
3282 wined3d_device_set_pixel_shader(device->wined3d_device,
3283 shader_obj ? shader_obj->wined3d_shader : NULL);
3284 wined3d_mutex_unlock();
3286 return D3D_OK;
3289 static HRESULT WINAPI d3d9_device_GetPixelShader(IDirect3DDevice9Ex *iface, IDirect3DPixelShader9 **shader)
3291 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3292 struct d3d9_pixelshader *shader_impl;
3293 struct wined3d_shader *wined3d_shader;
3295 TRACE("iface %p, shader %p.\n", iface, shader);
3297 if (!shader) return D3DERR_INVALIDCALL;
3299 wined3d_mutex_lock();
3300 if ((wined3d_shader = wined3d_device_get_pixel_shader(device->wined3d_device)))
3302 shader_impl = wined3d_shader_get_parent(wined3d_shader);
3303 *shader = &shader_impl->IDirect3DPixelShader9_iface;
3304 IDirect3DPixelShader9_AddRef(*shader);
3306 else
3308 *shader = NULL;
3310 wined3d_mutex_unlock();
3312 TRACE("Returning %p.\n", *shader);
3314 return D3D_OK;
3317 static HRESULT WINAPI d3d9_device_SetPixelShaderConstantF(IDirect3DDevice9Ex *iface,
3318 UINT reg_idx, const float *data, UINT count)
3320 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3321 HRESULT hr;
3323 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3325 wined3d_mutex_lock();
3326 hr = wined3d_device_set_ps_consts_f(device->wined3d_device,
3327 reg_idx, count, (const struct wined3d_vec4 *)data);
3328 wined3d_mutex_unlock();
3330 return hr;
3333 static HRESULT WINAPI d3d9_device_GetPixelShaderConstantF(IDirect3DDevice9Ex *iface,
3334 UINT reg_idx, float *data, UINT count)
3336 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3337 HRESULT hr;
3339 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3341 wined3d_mutex_lock();
3342 hr = wined3d_device_get_ps_consts_f(device->wined3d_device,
3343 reg_idx, count, (struct wined3d_vec4 *)data);
3344 wined3d_mutex_unlock();
3346 return hr;
3349 static HRESULT WINAPI d3d9_device_SetPixelShaderConstantI(IDirect3DDevice9Ex *iface,
3350 UINT reg_idx, const int *data, UINT count)
3352 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3353 HRESULT hr;
3355 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3357 wined3d_mutex_lock();
3358 hr = wined3d_device_set_ps_consts_i(device->wined3d_device,
3359 reg_idx, count, (const struct wined3d_ivec4 *)data);
3360 wined3d_mutex_unlock();
3362 return hr;
3365 static HRESULT WINAPI d3d9_device_GetPixelShaderConstantI(IDirect3DDevice9Ex *iface,
3366 UINT reg_idx, int *data, UINT count)
3368 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3369 HRESULT hr;
3371 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3373 wined3d_mutex_lock();
3374 hr = wined3d_device_get_ps_consts_i(device->wined3d_device,
3375 reg_idx, count, (struct wined3d_ivec4 *)data);
3376 wined3d_mutex_unlock();
3378 return hr;
3381 static HRESULT WINAPI d3d9_device_SetPixelShaderConstantB(IDirect3DDevice9Ex *iface,
3382 UINT reg_idx, const BOOL *data, UINT count)
3384 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3385 HRESULT hr;
3387 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3389 wined3d_mutex_lock();
3390 hr = wined3d_device_set_ps_consts_b(device->wined3d_device, reg_idx, count, data);
3391 wined3d_mutex_unlock();
3393 return hr;
3396 static HRESULT WINAPI d3d9_device_GetPixelShaderConstantB(IDirect3DDevice9Ex *iface,
3397 UINT reg_idx, BOOL *data, UINT count)
3399 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3400 HRESULT hr;
3402 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3404 wined3d_mutex_lock();
3405 hr = wined3d_device_get_ps_consts_b(device->wined3d_device, reg_idx, count, data);
3406 wined3d_mutex_unlock();
3408 return hr;
3411 static HRESULT WINAPI d3d9_device_DrawRectPatch(IDirect3DDevice9Ex *iface, UINT handle,
3412 const float *segment_count, const D3DRECTPATCH_INFO *patch_info)
3414 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
3415 iface, handle, segment_count, patch_info);
3416 return D3D_OK;
3419 static HRESULT WINAPI d3d9_device_DrawTriPatch(IDirect3DDevice9Ex *iface, UINT handle,
3420 const float *segment_count, const D3DTRIPATCH_INFO *patch_info)
3422 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
3423 iface, handle, segment_count, patch_info);
3424 return D3D_OK;
3427 static HRESULT WINAPI d3d9_device_DeletePatch(IDirect3DDevice9Ex *iface, UINT handle)
3429 FIXME("iface %p, handle %#x unimplemented.\n", iface, handle);
3430 return D3DERR_INVALIDCALL;
3433 static HRESULT WINAPI d3d9_device_CreateQuery(IDirect3DDevice9Ex *iface, D3DQUERYTYPE type, IDirect3DQuery9 **query)
3435 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3436 struct d3d9_query *object;
3437 HRESULT hr;
3439 TRACE("iface %p, type %#x, query %p.\n", iface, type, query);
3441 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
3442 if (!object)
3443 return E_OUTOFMEMORY;
3445 hr = query_init(object, device, type);
3446 if (FAILED(hr))
3448 WARN("Failed to initialize query, hr %#x.\n", hr);
3449 HeapFree(GetProcessHeap(), 0, object);
3450 return hr;
3453 TRACE("Created query %p.\n", object);
3454 if (query) *query = &object->IDirect3DQuery9_iface;
3455 else IDirect3DQuery9_Release(&object->IDirect3DQuery9_iface);
3457 return D3D_OK;
3460 static HRESULT WINAPI d3d9_device_SetConvolutionMonoKernel(IDirect3DDevice9Ex *iface,
3461 UINT width, UINT height, float *rows, float *columns)
3463 FIXME("iface %p, width %u, height %u, rows %p, columns %p stub!\n",
3464 iface, width, height, rows, columns);
3466 return E_NOTIMPL;
3469 static HRESULT WINAPI d3d9_device_ComposeRects(IDirect3DDevice9Ex *iface,
3470 IDirect3DSurface9 *src_surface, IDirect3DSurface9 *dst_surface, IDirect3DVertexBuffer9 *src_descs,
3471 UINT rect_count, IDirect3DVertexBuffer9 *dst_descs, D3DCOMPOSERECTSOP operation, INT offset_x, INT offset_y)
3473 FIXME("iface %p, src_surface %p, dst_surface %p, src_descs %p, rect_count %u, "
3474 "dst_descs %p, operation %#x, offset_x %u, offset_y %u stub!\n",
3475 iface, src_surface, dst_surface, src_descs, rect_count,
3476 dst_descs, operation, offset_x, offset_y);
3478 return E_NOTIMPL;
3481 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_PresentEx(IDirect3DDevice9Ex *iface,
3482 const RECT *src_rect, const RECT *dst_rect, HWND dst_window_override,
3483 const RGNDATA *dirty_region, DWORD flags)
3485 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3486 UINT i;
3487 HRESULT hr;
3489 TRACE("iface %p, src_rect %s, dst_rect %s, dst_window_override %p, dirty_region %p, flags %#x.\n",
3490 iface, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect),
3491 dst_window_override, dirty_region, flags);
3493 if (device->device_state != D3D9_DEVICE_STATE_OK)
3494 return S_PRESENT_OCCLUDED;
3496 if (dirty_region)
3497 FIXME("Ignoring dirty_region %p.\n", dirty_region);
3499 wined3d_mutex_lock();
3500 for (i = 0; i < device->implicit_swapchain_count; ++i)
3502 if (FAILED(hr = wined3d_swapchain_present(device->implicit_swapchains[i]->wined3d_swapchain,
3503 src_rect, dst_rect, dst_window_override, flags)))
3505 wined3d_mutex_unlock();
3506 return hr;
3509 wined3d_mutex_unlock();
3511 return D3D_OK;
3514 static HRESULT WINAPI d3d9_device_GetGPUThreadPriority(IDirect3DDevice9Ex *iface, INT *priority)
3516 FIXME("iface %p, priority %p stub!\n", iface, priority);
3518 return E_NOTIMPL;
3521 static HRESULT WINAPI d3d9_device_SetGPUThreadPriority(IDirect3DDevice9Ex *iface, INT priority)
3523 FIXME("iface %p, priority %d stub!\n", iface, priority);
3525 return E_NOTIMPL;
3528 static HRESULT WINAPI d3d9_device_WaitForVBlank(IDirect3DDevice9Ex *iface, UINT swapchain_idx)
3530 FIXME("iface %p, swapchain_idx %u stub!\n", iface, swapchain_idx);
3532 return E_NOTIMPL;
3535 static HRESULT WINAPI d3d9_device_CheckResourceResidency(IDirect3DDevice9Ex *iface,
3536 IDirect3DResource9 **resources, UINT32 resource_count)
3538 FIXME("iface %p, resources %p, resource_count %u stub!\n",
3539 iface, resources, resource_count);
3541 return E_NOTIMPL;
3544 static HRESULT WINAPI d3d9_device_SetMaximumFrameLatency(IDirect3DDevice9Ex *iface, UINT max_latency)
3546 FIXME("iface %p, max_latency %u stub!\n", iface, max_latency);
3548 return E_NOTIMPL;
3551 static HRESULT WINAPI d3d9_device_GetMaximumFrameLatency(IDirect3DDevice9Ex *iface, UINT *max_latency)
3553 FIXME("iface %p, max_latency %p stub!\n", iface, max_latency);
3555 *max_latency = 2;
3557 return E_NOTIMPL;
3560 static HRESULT WINAPI d3d9_device_CheckDeviceState(IDirect3DDevice9Ex *iface, HWND dst_window)
3562 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3563 struct wined3d_swapchain_desc swapchain_desc;
3565 TRACE("iface %p, dst_window %p.\n", iface, dst_window);
3567 wined3d_mutex_lock();
3568 wined3d_swapchain_get_desc(device->implicit_swapchains[0]->wined3d_swapchain, &swapchain_desc);
3569 wined3d_mutex_unlock();
3571 if (swapchain_desc.windowed)
3572 return D3D_OK;
3574 /* FIXME: This is actually supposed to check if any other device is in
3575 * fullscreen mode. */
3576 if (dst_window != swapchain_desc.device_window)
3577 return device->device_state == D3D9_DEVICE_STATE_OK ? S_PRESENT_OCCLUDED : D3D_OK;
3579 return device->device_state == D3D9_DEVICE_STATE_OK ? D3D_OK : S_PRESENT_OCCLUDED;
3582 static HRESULT WINAPI d3d9_device_CreateRenderTargetEx(IDirect3DDevice9Ex *iface,
3583 UINT width, UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
3584 BOOL lockable, IDirect3DSurface9 **surface, HANDLE *shared_handle, DWORD usage)
3586 FIXME("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u, "
3587 "lockable %#x, surface %p, shared_handle %p, usage %#x stub!\n",
3588 iface, width, height, format, multisample_type, multisample_quality,
3589 lockable, surface, shared_handle, usage);
3591 *surface = NULL;
3592 if (shared_handle)
3593 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
3595 return E_NOTIMPL;
3598 static HRESULT WINAPI d3d9_device_CreateOffscreenPlainSurfaceEx(IDirect3DDevice9Ex *iface,
3599 UINT width, UINT height, D3DFORMAT format, D3DPOOL pool, IDirect3DSurface9 **surface,
3600 HANDLE *shared_handle, DWORD usage)
3602 FIXME("iface %p, width %u, height %u, format %#x, pool %#x, surface %p, shared_handle %p, usage %#x stub!\n",
3603 iface, width, height, format, pool, surface, shared_handle, usage);
3605 return E_NOTIMPL;
3608 static HRESULT WINAPI d3d9_device_CreateDepthStencilSurfaceEx(IDirect3DDevice9Ex *iface,
3609 UINT width, UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
3610 BOOL discard, IDirect3DSurface9 **surface, HANDLE *shared_handle, DWORD usage)
3612 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3613 DWORD flags = WINED3D_TEXTURE_CREATE_MAPPABLE;
3615 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u, "
3616 "discard %#x, surface %p, shared_handle %p, usage %#x.\n",
3617 iface, width, height, format, multisample_type, multisample_quality,
3618 discard, surface, shared_handle, usage);
3620 if (usage & D3DUSAGE_DEPTHSTENCIL)
3622 WARN("Invalid usage %#x.\n", usage);
3623 return D3DERR_INVALIDCALL;
3626 if (shared_handle)
3627 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
3629 if (discard)
3630 flags |= WINED3D_TEXTURE_CREATE_DISCARD;
3632 *surface = NULL;
3633 return d3d9_device_create_surface(device, width, height, format, flags, surface,
3634 D3DUSAGE_DEPTHSTENCIL | usage, D3DPOOL_DEFAULT, multisample_type, multisample_quality, NULL);
3637 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_ResetEx(IDirect3DDevice9Ex *iface,
3638 D3DPRESENT_PARAMETERS *present_parameters, D3DDISPLAYMODEEX *mode)
3640 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3642 TRACE("iface %p, present_parameters %p, mode %p.\n", iface, present_parameters, mode);
3644 if (!present_parameters->Windowed == !mode)
3646 WARN("Mode can be passed if and only if Windowed is FALSE.\n");
3647 return D3DERR_INVALIDCALL;
3650 if (mode && (mode->Width != present_parameters->BackBufferWidth
3651 || mode->Height != present_parameters->BackBufferHeight))
3653 WARN("Mode and back buffer mismatch (mode %ux%u, backbuffer %ux%u).\n",
3654 mode->Width, mode->Height,
3655 present_parameters->BackBufferWidth, present_parameters->BackBufferHeight);
3656 return D3DERR_INVALIDCALL;
3659 return d3d9_device_reset(device, present_parameters, mode);
3662 static HRESULT WINAPI d3d9_device_GetDisplayModeEx(IDirect3DDevice9Ex *iface,
3663 UINT swapchain_idx, D3DDISPLAYMODEEX *mode, D3DDISPLAYROTATION *rotation)
3665 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3666 struct wined3d_display_mode wined3d_mode;
3667 HRESULT hr;
3669 TRACE("iface %p, swapchain_idx %u, mode %p, rotation %p.\n",
3670 iface, swapchain_idx, mode, rotation);
3672 if (mode->Size != sizeof(*mode))
3673 return D3DERR_INVALIDCALL;
3675 wined3d_mutex_lock();
3676 hr = wined3d_device_get_display_mode(device->wined3d_device, swapchain_idx, &wined3d_mode,
3677 (enum wined3d_display_rotation *)rotation);
3678 wined3d_mutex_unlock();
3680 if (SUCCEEDED(hr))
3682 mode->Width = wined3d_mode.width;
3683 mode->Height = wined3d_mode.height;
3684 mode->RefreshRate = wined3d_mode.refresh_rate;
3685 mode->Format = d3dformat_from_wined3dformat(wined3d_mode.format_id);
3686 mode->ScanLineOrdering = wined3d_mode.scanline_ordering;
3689 return hr;
3692 static const struct IDirect3DDevice9ExVtbl d3d9_device_vtbl =
3694 /* IUnknown */
3695 d3d9_device_QueryInterface,
3696 d3d9_device_AddRef,
3697 d3d9_device_Release,
3698 /* IDirect3DDevice9 */
3699 d3d9_device_TestCooperativeLevel,
3700 d3d9_device_GetAvailableTextureMem,
3701 d3d9_device_EvictManagedResources,
3702 d3d9_device_GetDirect3D,
3703 d3d9_device_GetDeviceCaps,
3704 d3d9_device_GetDisplayMode,
3705 d3d9_device_GetCreationParameters,
3706 d3d9_device_SetCursorProperties,
3707 d3d9_device_SetCursorPosition,
3708 d3d9_device_ShowCursor,
3709 d3d9_device_CreateAdditionalSwapChain,
3710 d3d9_device_GetSwapChain,
3711 d3d9_device_GetNumberOfSwapChains,
3712 d3d9_device_Reset,
3713 d3d9_device_Present,
3714 d3d9_device_GetBackBuffer,
3715 d3d9_device_GetRasterStatus,
3716 d3d9_device_SetDialogBoxMode,
3717 d3d9_device_SetGammaRamp,
3718 d3d9_device_GetGammaRamp,
3719 d3d9_device_CreateTexture,
3720 d3d9_device_CreateVolumeTexture,
3721 d3d9_device_CreateCubeTexture,
3722 d3d9_device_CreateVertexBuffer,
3723 d3d9_device_CreateIndexBuffer,
3724 d3d9_device_CreateRenderTarget,
3725 d3d9_device_CreateDepthStencilSurface,
3726 d3d9_device_UpdateSurface,
3727 d3d9_device_UpdateTexture,
3728 d3d9_device_GetRenderTargetData,
3729 d3d9_device_GetFrontBufferData,
3730 d3d9_device_StretchRect,
3731 d3d9_device_ColorFill,
3732 d3d9_device_CreateOffscreenPlainSurface,
3733 d3d9_device_SetRenderTarget,
3734 d3d9_device_GetRenderTarget,
3735 d3d9_device_SetDepthStencilSurface,
3736 d3d9_device_GetDepthStencilSurface,
3737 d3d9_device_BeginScene,
3738 d3d9_device_EndScene,
3739 d3d9_device_Clear,
3740 d3d9_device_SetTransform,
3741 d3d9_device_GetTransform,
3742 d3d9_device_MultiplyTransform,
3743 d3d9_device_SetViewport,
3744 d3d9_device_GetViewport,
3745 d3d9_device_SetMaterial,
3746 d3d9_device_GetMaterial,
3747 d3d9_device_SetLight,
3748 d3d9_device_GetLight,
3749 d3d9_device_LightEnable,
3750 d3d9_device_GetLightEnable,
3751 d3d9_device_SetClipPlane,
3752 d3d9_device_GetClipPlane,
3753 d3d9_device_SetRenderState,
3754 d3d9_device_GetRenderState,
3755 d3d9_device_CreateStateBlock,
3756 d3d9_device_BeginStateBlock,
3757 d3d9_device_EndStateBlock,
3758 d3d9_device_SetClipStatus,
3759 d3d9_device_GetClipStatus,
3760 d3d9_device_GetTexture,
3761 d3d9_device_SetTexture,
3762 d3d9_device_GetTextureStageState,
3763 d3d9_device_SetTextureStageState,
3764 d3d9_device_GetSamplerState,
3765 d3d9_device_SetSamplerState,
3766 d3d9_device_ValidateDevice,
3767 d3d9_device_SetPaletteEntries,
3768 d3d9_device_GetPaletteEntries,
3769 d3d9_device_SetCurrentTexturePalette,
3770 d3d9_device_GetCurrentTexturePalette,
3771 d3d9_device_SetScissorRect,
3772 d3d9_device_GetScissorRect,
3773 d3d9_device_SetSoftwareVertexProcessing,
3774 d3d9_device_GetSoftwareVertexProcessing,
3775 d3d9_device_SetNPatchMode,
3776 d3d9_device_GetNPatchMode,
3777 d3d9_device_DrawPrimitive,
3778 d3d9_device_DrawIndexedPrimitive,
3779 d3d9_device_DrawPrimitiveUP,
3780 d3d9_device_DrawIndexedPrimitiveUP,
3781 d3d9_device_ProcessVertices,
3782 d3d9_device_CreateVertexDeclaration,
3783 d3d9_device_SetVertexDeclaration,
3784 d3d9_device_GetVertexDeclaration,
3785 d3d9_device_SetFVF,
3786 d3d9_device_GetFVF,
3787 d3d9_device_CreateVertexShader,
3788 d3d9_device_SetVertexShader,
3789 d3d9_device_GetVertexShader,
3790 d3d9_device_SetVertexShaderConstantF,
3791 d3d9_device_GetVertexShaderConstantF,
3792 d3d9_device_SetVertexShaderConstantI,
3793 d3d9_device_GetVertexShaderConstantI,
3794 d3d9_device_SetVertexShaderConstantB,
3795 d3d9_device_GetVertexShaderConstantB,
3796 d3d9_device_SetStreamSource,
3797 d3d9_device_GetStreamSource,
3798 d3d9_device_SetStreamSourceFreq,
3799 d3d9_device_GetStreamSourceFreq,
3800 d3d9_device_SetIndices,
3801 d3d9_device_GetIndices,
3802 d3d9_device_CreatePixelShader,
3803 d3d9_device_SetPixelShader,
3804 d3d9_device_GetPixelShader,
3805 d3d9_device_SetPixelShaderConstantF,
3806 d3d9_device_GetPixelShaderConstantF,
3807 d3d9_device_SetPixelShaderConstantI,
3808 d3d9_device_GetPixelShaderConstantI,
3809 d3d9_device_SetPixelShaderConstantB,
3810 d3d9_device_GetPixelShaderConstantB,
3811 d3d9_device_DrawRectPatch,
3812 d3d9_device_DrawTriPatch,
3813 d3d9_device_DeletePatch,
3814 d3d9_device_CreateQuery,
3815 /* IDirect3DDevice9Ex */
3816 d3d9_device_SetConvolutionMonoKernel,
3817 d3d9_device_ComposeRects,
3818 d3d9_device_PresentEx,
3819 d3d9_device_GetGPUThreadPriority,
3820 d3d9_device_SetGPUThreadPriority,
3821 d3d9_device_WaitForVBlank,
3822 d3d9_device_CheckResourceResidency,
3823 d3d9_device_SetMaximumFrameLatency,
3824 d3d9_device_GetMaximumFrameLatency,
3825 d3d9_device_CheckDeviceState,
3826 d3d9_device_CreateRenderTargetEx,
3827 d3d9_device_CreateOffscreenPlainSurfaceEx,
3828 d3d9_device_CreateDepthStencilSurfaceEx,
3829 d3d9_device_ResetEx,
3830 d3d9_device_GetDisplayModeEx,
3833 static inline struct d3d9_device *device_from_device_parent(struct wined3d_device_parent *device_parent)
3835 return CONTAINING_RECORD(device_parent, struct d3d9_device, device_parent);
3838 static void CDECL device_parent_wined3d_device_created(struct wined3d_device_parent *device_parent,
3839 struct wined3d_device *device)
3841 TRACE("device_parent %p, device %p.\n", device_parent, device);
3844 static void CDECL device_parent_mode_changed(struct wined3d_device_parent *device_parent)
3846 TRACE("device_parent %p.\n", device_parent);
3849 static void CDECL device_parent_activate(struct wined3d_device_parent *device_parent, BOOL activate)
3851 struct d3d9_device *device = device_from_device_parent(device_parent);
3853 TRACE("device_parent %p, activate %#x.\n", device_parent, activate);
3855 if (!device->d3d_parent)
3856 return;
3858 if (!activate)
3859 InterlockedCompareExchange(&device->device_state, D3D9_DEVICE_STATE_LOST, D3D9_DEVICE_STATE_OK);
3860 else if (device->d3d_parent->extended)
3861 InterlockedCompareExchange(&device->device_state, D3D9_DEVICE_STATE_OK, D3D9_DEVICE_STATE_LOST);
3862 else
3863 InterlockedCompareExchange(&device->device_state, D3D9_DEVICE_STATE_NOT_RESET, D3D9_DEVICE_STATE_LOST);
3866 static HRESULT CDECL device_parent_surface_created(struct wined3d_device_parent *device_parent,
3867 struct wined3d_texture *wined3d_texture, unsigned int sub_resource_idx,
3868 void **parent, const struct wined3d_parent_ops **parent_ops)
3870 struct d3d9_surface *d3d_surface;
3872 TRACE("device_parent %p, wined3d_texture %p, sub_resource_idx %u, parent %p, parent_ops %p.\n",
3873 device_parent, wined3d_texture, sub_resource_idx, parent, parent_ops);
3875 if (!(d3d_surface = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*d3d_surface))))
3876 return E_OUTOFMEMORY;
3878 surface_init(d3d_surface, wined3d_texture, sub_resource_idx, parent_ops);
3879 *parent = d3d_surface;
3880 TRACE("Created surface %p.\n", d3d_surface);
3882 return D3D_OK;
3885 static HRESULT CDECL device_parent_volume_created(struct wined3d_device_parent *device_parent,
3886 struct wined3d_texture *wined3d_texture, unsigned int sub_resource_idx,
3887 void **parent, const struct wined3d_parent_ops **parent_ops)
3889 struct d3d9_volume *d3d_volume;
3891 TRACE("device_parent %p, texture %p, sub_resource_idx %u, parent %p, parent_ops %p.\n",
3892 device_parent, wined3d_texture, sub_resource_idx, parent, parent_ops);
3894 if (!(d3d_volume = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*d3d_volume))))
3895 return E_OUTOFMEMORY;
3897 volume_init(d3d_volume, wined3d_texture, sub_resource_idx, parent_ops);
3898 *parent = d3d_volume;
3899 TRACE("Created volume %p.\n", d3d_volume);
3901 return D3D_OK;
3904 static HRESULT CDECL device_parent_create_swapchain_texture(struct wined3d_device_parent *device_parent,
3905 void *container_parent, const struct wined3d_resource_desc *desc, DWORD texture_flags,
3906 struct wined3d_texture **texture)
3908 struct d3d9_device *device = device_from_device_parent(device_parent);
3909 struct d3d9_surface *d3d_surface;
3910 HRESULT hr;
3912 TRACE("device_parent %p, container_parent %p, desc %p, texture flags %#x, texture %p.\n",
3913 device_parent, container_parent, desc, texture_flags, texture);
3915 if (container_parent == device_parent)
3916 container_parent = &device->IDirect3DDevice9Ex_iface;
3918 if (FAILED(hr = wined3d_texture_create(device->wined3d_device, desc, 1, 1,
3919 texture_flags | WINED3D_TEXTURE_CREATE_MAPPABLE, NULL, container_parent,
3920 &d3d9_null_wined3d_parent_ops, texture)))
3922 WARN("Failed to create texture, hr %#x.\n", hr);
3923 return hr;
3926 d3d_surface = wined3d_texture_get_sub_resource_parent(*texture, 0);
3927 d3d_surface->parent_device = &device->IDirect3DDevice9Ex_iface;
3929 return hr;
3932 static HRESULT CDECL device_parent_create_swapchain(struct wined3d_device_parent *device_parent,
3933 struct wined3d_swapchain_desc *desc, struct wined3d_swapchain **swapchain)
3935 struct d3d9_device *device = device_from_device_parent(device_parent);
3936 struct d3d9_swapchain *d3d_swapchain;
3937 HRESULT hr;
3939 TRACE("device_parent %p, desc %p, swapchain %p\n", device_parent, desc, swapchain);
3941 hr = d3d9_swapchain_create(device, desc, &d3d_swapchain);
3942 if (FAILED(hr))
3944 WARN("Failed to create swapchain, hr %#x.\n", hr);
3945 *swapchain = NULL;
3946 return hr;
3949 *swapchain = d3d_swapchain->wined3d_swapchain;
3950 wined3d_swapchain_incref(*swapchain);
3951 IDirect3DSwapChain9Ex_Release(&d3d_swapchain->IDirect3DSwapChain9Ex_iface);
3953 return hr;
3956 static const struct wined3d_device_parent_ops d3d9_wined3d_device_parent_ops =
3958 device_parent_wined3d_device_created,
3959 device_parent_mode_changed,
3960 device_parent_activate,
3961 device_parent_surface_created,
3962 device_parent_volume_created,
3963 device_parent_create_swapchain_texture,
3964 device_parent_create_swapchain,
3967 static void setup_fpu(void)
3969 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
3970 WORD cw;
3971 __asm__ volatile ("fnstcw %0" : "=m" (cw));
3972 cw = (cw & ~0xf3f) | 0x3f;
3973 __asm__ volatile ("fldcw %0" : : "m" (cw));
3974 #elif defined(__i386__) && defined(_MSC_VER)
3975 WORD cw;
3976 __asm fnstcw cw;
3977 cw = (cw & ~0xf3f) | 0x3f;
3978 __asm fldcw cw;
3979 #else
3980 FIXME("FPU setup not implemented for this platform.\n");
3981 #endif
3984 HRESULT device_init(struct d3d9_device *device, struct d3d9 *parent, struct wined3d *wined3d,
3985 UINT adapter, D3DDEVTYPE device_type, HWND focus_window, DWORD flags,
3986 D3DPRESENT_PARAMETERS *parameters, D3DDISPLAYMODEEX *mode)
3988 struct wined3d_swapchain_desc *swapchain_desc;
3989 UINT i, count = 1;
3990 HRESULT hr;
3992 if (mode)
3993 FIXME("Ignoring display mode.\n");
3995 device->IDirect3DDevice9Ex_iface.lpVtbl = &d3d9_device_vtbl;
3996 device->device_parent.ops = &d3d9_wined3d_device_parent_ops;
3997 device->refcount = 1;
3999 if (!(flags & D3DCREATE_FPU_PRESERVE)) setup_fpu();
4001 wined3d_mutex_lock();
4002 hr = wined3d_device_create(wined3d, adapter, device_type, focus_window, flags, 4,
4003 &device->device_parent, &device->wined3d_device);
4004 if (FAILED(hr))
4006 WARN("Failed to create wined3d device, hr %#x.\n", hr);
4007 wined3d_mutex_unlock();
4008 return hr;
4011 if (flags & D3DCREATE_ADAPTERGROUP_DEVICE)
4013 WINED3DCAPS caps;
4015 wined3d_get_device_caps(wined3d, adapter, device_type, &caps);
4016 count = caps.NumberOfAdaptersInGroup;
4019 if (flags & D3DCREATE_MULTITHREADED)
4020 wined3d_device_set_multithreaded(device->wined3d_device);
4022 if (!parameters->Windowed)
4024 if (!focus_window)
4025 focus_window = parameters->hDeviceWindow;
4026 if (FAILED(hr = wined3d_device_acquire_focus_window(device->wined3d_device, focus_window)))
4028 ERR("Failed to acquire focus window, hr %#x.\n", hr);
4029 wined3d_device_decref(device->wined3d_device);
4030 wined3d_mutex_unlock();
4031 return hr;
4034 for (i = 0; i < count; ++i)
4036 HWND device_window = parameters[i].hDeviceWindow;
4038 if (!device_window) device_window = focus_window;
4039 wined3d_device_setup_fullscreen_window(device->wined3d_device, device_window,
4040 parameters[i].BackBufferWidth,
4041 parameters[i].BackBufferHeight);
4045 swapchain_desc = HeapAlloc(GetProcessHeap(), 0, sizeof(*swapchain_desc) * count);
4046 if (!swapchain_desc)
4048 ERR("Failed to allocate wined3d parameters.\n");
4049 wined3d_device_release_focus_window(device->wined3d_device);
4050 wined3d_device_decref(device->wined3d_device);
4051 wined3d_mutex_unlock();
4052 return E_OUTOFMEMORY;
4055 for (i = 0; i < count; ++i)
4057 if (!wined3d_swapchain_desc_from_present_parameters(&swapchain_desc[i], &parameters[i],
4058 parent->extended))
4060 wined3d_device_release_focus_window(device->wined3d_device);
4061 wined3d_device_decref(device->wined3d_device);
4062 HeapFree(GetProcessHeap(), 0, swapchain_desc);
4063 wined3d_mutex_unlock();
4064 return D3DERR_INVALIDCALL;
4068 hr = wined3d_device_init_3d(device->wined3d_device, swapchain_desc);
4069 if (FAILED(hr))
4071 WARN("Failed to initialize 3D, hr %#x.\n", hr);
4072 wined3d_device_release_focus_window(device->wined3d_device);
4073 HeapFree(GetProcessHeap(), 0, swapchain_desc);
4074 wined3d_device_decref(device->wined3d_device);
4075 wined3d_mutex_unlock();
4076 return hr;
4079 if (FAILED(hr = d3d9_device_get_swapchains(device)))
4081 wined3d_device_uninit_3d(device->wined3d_device);
4082 wined3d_device_release_focus_window(device->wined3d_device);
4083 wined3d_device_decref(device->wined3d_device);
4084 wined3d_mutex_unlock();
4085 return E_OUTOFMEMORY;
4088 for (i = 0; i < count; ++i)
4090 present_parameters_from_wined3d_swapchain_desc(&parameters[i], &swapchain_desc[i]);
4093 wined3d_mutex_unlock();
4095 HeapFree(GetProcessHeap(), 0, swapchain_desc);
4097 /* Initialize the converted declaration array. This creates a valid pointer
4098 * and when adding decls HeapReAlloc() can be used without further checking. */
4099 device->fvf_decls = HeapAlloc(GetProcessHeap(), 0, 0);
4100 if (!device->fvf_decls)
4102 ERR("Failed to allocate FVF vertex declaration map memory.\n");
4103 wined3d_mutex_lock();
4104 HeapFree(GetProcessHeap(), 0, device->implicit_swapchains);
4105 wined3d_device_uninit_3d(device->wined3d_device);
4106 wined3d_device_release_focus_window(device->wined3d_device);
4107 wined3d_device_decref(device->wined3d_device);
4108 wined3d_mutex_unlock();
4109 return E_OUTOFMEMORY;
4112 IDirect3D9Ex_AddRef(&parent->IDirect3D9Ex_iface);
4113 device->d3d_parent = parent;
4115 return D3D_OK;