usp10: Fall back to 'dflt' language if shaping language tag isn't found.
[wine.git] / dlls / d3d9 / device.c
blobb4cb25316c749a72676d0bddf21283e095e59f80
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 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)
1405 wined3d_box_set(&src_box, src_rect->left, src_rect->top, src_rect->right, src_rect->bottom, 0, 1);
1407 wined3d_mutex_lock();
1408 hr = wined3d_device_copy_sub_resource_region(device->wined3d_device,
1409 wined3d_texture_get_resource(dst->wined3d_texture), dst->sub_resource_idx, dst_point ? dst_point->x : 0,
1410 dst_point ? dst_point->y : 0, 0, wined3d_texture_get_resource(src->wined3d_texture),
1411 src->sub_resource_idx, src_rect ? &src_box : NULL);
1412 wined3d_mutex_unlock();
1414 if (FAILED(hr))
1415 return D3DERR_INVALIDCALL;
1417 return hr;
1420 static HRESULT WINAPI d3d9_device_UpdateTexture(IDirect3DDevice9Ex *iface,
1421 IDirect3DBaseTexture9 *src_texture, IDirect3DBaseTexture9 *dst_texture)
1423 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1424 struct d3d9_texture *src_impl, *dst_impl;
1425 HRESULT hr;
1427 TRACE("iface %p, src_texture %p, dst_texture %p.\n", iface, src_texture, dst_texture);
1429 src_impl = unsafe_impl_from_IDirect3DBaseTexture9(src_texture);
1430 dst_impl = unsafe_impl_from_IDirect3DBaseTexture9(dst_texture);
1432 wined3d_mutex_lock();
1433 hr = wined3d_device_update_texture(device->wined3d_device,
1434 src_impl->wined3d_texture, dst_impl->wined3d_texture);
1435 wined3d_mutex_unlock();
1437 return hr;
1440 static HRESULT WINAPI d3d9_device_GetRenderTargetData(IDirect3DDevice9Ex *iface,
1441 IDirect3DSurface9 *render_target, IDirect3DSurface9 *dst_surface)
1443 struct d3d9_surface *rt_impl = unsafe_impl_from_IDirect3DSurface9(render_target);
1444 struct d3d9_surface *dst_impl = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1445 struct wined3d_sub_resource_desc wined3d_desc;
1446 RECT dst_rect, src_rect;
1447 HRESULT hr;
1449 TRACE("iface %p, render_target %p, dst_surface %p.\n", iface, render_target, dst_surface);
1451 if (!render_target || !dst_surface)
1452 return D3DERR_INVALIDCALL;
1454 wined3d_mutex_lock();
1455 wined3d_texture_get_sub_resource_desc(dst_impl->wined3d_texture, dst_impl->sub_resource_idx, &wined3d_desc);
1456 SetRect(&dst_rect, 0, 0, wined3d_desc.width, wined3d_desc.height);
1458 wined3d_texture_get_sub_resource_desc(rt_impl->wined3d_texture, rt_impl->sub_resource_idx, &wined3d_desc);
1459 SetRect(&src_rect, 0, 0, wined3d_desc.width, wined3d_desc.height);
1461 /* TODO: Check surface sizes, pools, etc. */
1462 if (wined3d_desc.multisample_type)
1463 hr = D3DERR_INVALIDCALL;
1464 else
1465 hr = wined3d_texture_blt(dst_impl->wined3d_texture, dst_impl->sub_resource_idx, &dst_rect,
1466 rt_impl->wined3d_texture, rt_impl->sub_resource_idx, &src_rect, 0, NULL, WINED3D_TEXF_POINT);
1467 wined3d_mutex_unlock();
1469 return hr;
1472 static HRESULT WINAPI d3d9_device_GetFrontBufferData(IDirect3DDevice9Ex *iface,
1473 UINT swapchain, IDirect3DSurface9 *dst_surface)
1475 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1476 struct d3d9_surface *dst_impl = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1477 HRESULT hr = D3DERR_INVALIDCALL;
1479 TRACE("iface %p, swapchain %u, dst_surface %p.\n", iface, swapchain, dst_surface);
1481 wined3d_mutex_lock();
1482 if (swapchain < device->implicit_swapchain_count)
1483 hr = wined3d_swapchain_get_front_buffer_data(device->implicit_swapchains[swapchain]->wined3d_swapchain,
1484 dst_impl->wined3d_texture, dst_impl->sub_resource_idx);
1485 wined3d_mutex_unlock();
1487 return hr;
1490 static HRESULT WINAPI d3d9_device_StretchRect(IDirect3DDevice9Ex *iface, IDirect3DSurface9 *src_surface,
1491 const RECT *src_rect, IDirect3DSurface9 *dst_surface, const RECT *dst_rect, D3DTEXTUREFILTERTYPE filter)
1493 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1494 struct d3d9_surface *src = unsafe_impl_from_IDirect3DSurface9(src_surface);
1495 struct d3d9_surface *dst = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1496 struct wined3d_sub_resource_desc src_desc, dst_desc;
1497 HRESULT hr = D3DERR_INVALIDCALL;
1498 RECT d, s;
1500 TRACE("iface %p, src_surface %p, src_rect %p, dst_surface %p, dst_rect %p, filter %#x.\n",
1501 iface, src_surface, src_rect, dst_surface, dst_rect, filter);
1503 wined3d_mutex_lock();
1504 wined3d_texture_get_sub_resource_desc(dst->wined3d_texture, dst->sub_resource_idx, &dst_desc);
1505 if (!dst_rect)
1507 SetRect(&d, 0, 0, dst_desc.width, dst_desc.height);
1508 dst_rect = &d;
1511 wined3d_texture_get_sub_resource_desc(src->wined3d_texture, src->sub_resource_idx, &src_desc);
1512 if (!src_rect)
1514 SetRect(&s, 0, 0, src_desc.width, src_desc.height);
1515 src_rect = &s;
1518 if (src_desc.usage & WINED3DUSAGE_DEPTHSTENCIL)
1520 if (device->in_scene)
1522 WARN("Rejecting depth / stencil blit while in scene.\n");
1523 goto done;
1526 if (src_rect->left || src_rect->top || src_rect->right != src_desc.width
1527 || src_rect->bottom != src_desc.height)
1529 WARN("Rejecting depth / stencil blit with invalid source rect %s.\n",
1530 wine_dbgstr_rect(src_rect));
1531 goto done;
1534 if (dst_rect->left || dst_rect->top || dst_rect->right != dst_desc.width
1535 || dst_rect->bottom != dst_desc.height)
1537 WARN("Rejecting depth / stencil blit with invalid destination rect %s.\n",
1538 wine_dbgstr_rect(dst_rect));
1539 goto done;
1542 if (src_desc.width != dst_desc.width || src_desc.height != dst_desc.height)
1544 WARN("Rejecting depth / stencil blit with mismatched surface sizes.\n");
1545 goto done;
1549 hr = wined3d_texture_blt(dst->wined3d_texture, dst->sub_resource_idx, dst_rect,
1550 src->wined3d_texture, src->sub_resource_idx, src_rect, 0, NULL, filter);
1551 if (hr == WINEDDERR_INVALIDRECT)
1552 hr = D3DERR_INVALIDCALL;
1554 done:
1555 wined3d_mutex_unlock();
1556 return hr;
1559 static HRESULT WINAPI d3d9_device_ColorFill(IDirect3DDevice9Ex *iface,
1560 IDirect3DSurface9 *surface, const RECT *rect, D3DCOLOR color)
1562 const struct wined3d_color c =
1564 ((color >> 16) & 0xff) / 255.0f,
1565 ((color >> 8) & 0xff) / 255.0f,
1566 (color & 0xff) / 255.0f,
1567 ((color >> 24) & 0xff) / 255.0f,
1569 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1570 struct d3d9_surface *surface_impl = unsafe_impl_from_IDirect3DSurface9(surface);
1571 struct wined3d_sub_resource_desc desc;
1572 struct wined3d_rendertarget_view *rtv;
1573 HRESULT hr;
1575 TRACE("iface %p, surface %p, rect %p, color 0x%08x.\n", iface, surface, rect, color);
1577 wined3d_mutex_lock();
1579 if (FAILED(wined3d_texture_get_sub_resource_desc(surface_impl->wined3d_texture,
1580 surface_impl->sub_resource_idx, &desc)))
1582 wined3d_mutex_unlock();
1583 return D3DERR_INVALIDCALL;
1586 if (desc.pool != WINED3D_POOL_DEFAULT)
1588 wined3d_mutex_unlock();
1589 WARN("Colorfill is not allowed on surfaces in pool %#x, returning D3DERR_INVALIDCALL.\n", desc.pool);
1590 return D3DERR_INVALIDCALL;
1592 if ((desc.usage & (WINED3DUSAGE_RENDERTARGET | WINED3DUSAGE_TEXTURE)) == WINED3DUSAGE_TEXTURE)
1594 wined3d_mutex_unlock();
1595 WARN("Colorfill is not allowed on non-RT textures, returning D3DERR_INVALIDCALL.\n");
1596 return D3DERR_INVALIDCALL;
1598 if (desc.usage & WINED3DUSAGE_DEPTHSTENCIL)
1600 wined3d_mutex_unlock();
1601 WARN("Colorfill is not allowed on depth stencil surfaces, returning D3DERR_INVALIDCALL.\n");
1602 return D3DERR_INVALIDCALL;
1605 rtv = d3d9_surface_acquire_rendertarget_view(surface_impl);
1606 hr = wined3d_device_clear_rendertarget_view(device->wined3d_device,
1607 rtv, rect, WINED3DCLEAR_TARGET, &c, 0.0f, 0);
1608 d3d9_surface_release_rendertarget_view(surface_impl, rtv);
1610 wined3d_mutex_unlock();
1612 return hr;
1615 static HRESULT WINAPI d3d9_device_CreateOffscreenPlainSurface(IDirect3DDevice9Ex *iface,
1616 UINT width, UINT height, D3DFORMAT format, D3DPOOL pool, IDirect3DSurface9 **surface,
1617 HANDLE *shared_handle)
1619 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1620 void *user_mem = NULL;
1622 TRACE("iface %p, width %u, height %u, format %#x, pool %#x, surface %p, shared_handle %p.\n",
1623 iface, width, height, format, pool, surface, shared_handle);
1625 *surface = NULL;
1626 if (pool == D3DPOOL_MANAGED)
1628 WARN("Attempting to create a managed offscreen plain surface.\n");
1629 return D3DERR_INVALIDCALL;
1632 if (shared_handle)
1634 if (!device->d3d_parent->extended)
1636 WARN("Trying to create a shared or user memory surface on a non-ex device.\n");
1637 return E_NOTIMPL;
1640 if (pool == D3DPOOL_SYSTEMMEM)
1641 user_mem = *shared_handle;
1642 else
1644 if (pool != D3DPOOL_DEFAULT)
1646 WARN("Trying to create a shared surface in pool %#x.\n", pool);
1647 return D3DERR_INVALIDCALL;
1649 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1653 /* FIXME: Offscreen surfaces are supposed to be always lockable,
1654 * regardless of the pool they're created in. Should we set dynamic usage
1655 * here? */
1656 return d3d9_device_create_surface(device, width, height, format,
1657 WINED3D_TEXTURE_CREATE_MAPPABLE, surface, 0, pool, D3DMULTISAMPLE_NONE, 0, user_mem);
1660 static HRESULT WINAPI d3d9_device_SetRenderTarget(IDirect3DDevice9Ex *iface, DWORD idx, IDirect3DSurface9 *surface)
1662 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1663 struct d3d9_surface *surface_impl = unsafe_impl_from_IDirect3DSurface9(surface);
1664 struct wined3d_rendertarget_view *rtv;
1665 HRESULT hr;
1667 TRACE("iface %p, idx %u, surface %p.\n", iface, idx, surface);
1669 if (idx >= D3D9_MAX_SIMULTANEOUS_RENDERTARGETS)
1671 WARN("Invalid index %u specified.\n", idx);
1672 return D3DERR_INVALIDCALL;
1675 if (!idx && !surface_impl)
1677 WARN("Trying to set render target 0 to NULL.\n");
1678 return D3DERR_INVALIDCALL;
1681 if (surface_impl && d3d9_surface_get_device(surface_impl) != device)
1683 WARN("Render target surface does not match device.\n");
1684 return D3DERR_INVALIDCALL;
1687 wined3d_mutex_lock();
1688 rtv = surface_impl ? d3d9_surface_acquire_rendertarget_view(surface_impl) : NULL;
1689 hr = wined3d_device_set_rendertarget_view(device->wined3d_device, idx, rtv, TRUE);
1690 d3d9_surface_release_rendertarget_view(surface_impl, rtv);
1691 wined3d_mutex_unlock();
1693 return hr;
1696 static HRESULT WINAPI d3d9_device_GetRenderTarget(IDirect3DDevice9Ex *iface, DWORD idx, IDirect3DSurface9 **surface)
1698 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1699 struct wined3d_rendertarget_view *wined3d_rtv;
1700 struct d3d9_surface *surface_impl;
1701 HRESULT hr = D3D_OK;
1703 TRACE("iface %p, idx %u, surface %p.\n", iface, idx, surface);
1705 if (!surface)
1706 return D3DERR_INVALIDCALL;
1708 if (idx >= D3D9_MAX_SIMULTANEOUS_RENDERTARGETS)
1710 WARN("Invalid index %u specified.\n", idx);
1711 return D3DERR_INVALIDCALL;
1714 wined3d_mutex_lock();
1715 if ((wined3d_rtv = wined3d_device_get_rendertarget_view(device->wined3d_device, idx)))
1717 /* We want the sub resource parent here, since the view itself may be
1718 * internal to wined3d and may not have a parent. */
1719 surface_impl = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_rtv);
1720 *surface = &surface_impl->IDirect3DSurface9_iface;
1721 IDirect3DSurface9_AddRef(*surface);
1723 else
1725 hr = D3DERR_NOTFOUND;
1726 *surface = NULL;
1728 wined3d_mutex_unlock();
1730 return hr;
1733 static HRESULT WINAPI d3d9_device_SetDepthStencilSurface(IDirect3DDevice9Ex *iface, IDirect3DSurface9 *depth_stencil)
1735 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1736 struct d3d9_surface *ds_impl = unsafe_impl_from_IDirect3DSurface9(depth_stencil);
1737 struct wined3d_rendertarget_view *rtv;
1739 TRACE("iface %p, depth_stencil %p.\n", iface, depth_stencil);
1741 wined3d_mutex_lock();
1742 rtv = ds_impl ? d3d9_surface_acquire_rendertarget_view(ds_impl) : NULL;
1743 wined3d_device_set_depth_stencil_view(device->wined3d_device, rtv);
1744 d3d9_surface_release_rendertarget_view(ds_impl, rtv);
1745 wined3d_mutex_unlock();
1747 return D3D_OK;
1750 static HRESULT WINAPI d3d9_device_GetDepthStencilSurface(IDirect3DDevice9Ex *iface, IDirect3DSurface9 **depth_stencil)
1752 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1753 struct wined3d_rendertarget_view *wined3d_dsv;
1754 struct d3d9_surface *surface_impl;
1755 HRESULT hr = D3D_OK;
1757 TRACE("iface %p, depth_stencil %p.\n", iface, depth_stencil);
1759 if (!depth_stencil)
1760 return D3DERR_INVALIDCALL;
1762 wined3d_mutex_lock();
1763 if ((wined3d_dsv = wined3d_device_get_depth_stencil_view(device->wined3d_device)))
1765 /* We want the sub resource parent here, since the view itself may be
1766 * internal to wined3d and may not have a parent. */
1767 surface_impl = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_dsv);
1768 *depth_stencil = &surface_impl->IDirect3DSurface9_iface;
1769 IDirect3DSurface9_AddRef(*depth_stencil);
1771 else
1773 hr = D3DERR_NOTFOUND;
1774 *depth_stencil = NULL;
1776 wined3d_mutex_unlock();
1778 return hr;
1781 static HRESULT WINAPI d3d9_device_BeginScene(IDirect3DDevice9Ex *iface)
1783 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1784 HRESULT hr;
1786 TRACE("iface %p.\n", iface);
1788 wined3d_mutex_lock();
1789 if (SUCCEEDED(hr = wined3d_device_begin_scene(device->wined3d_device)))
1790 device->in_scene = TRUE;
1791 wined3d_mutex_unlock();
1793 return hr;
1796 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_EndScene(IDirect3DDevice9Ex *iface)
1798 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1799 HRESULT hr;
1801 TRACE("iface %p.\n", iface);
1803 wined3d_mutex_lock();
1804 if (SUCCEEDED(hr = wined3d_device_end_scene(device->wined3d_device)))
1805 device->in_scene = FALSE;
1806 wined3d_mutex_unlock();
1808 return hr;
1811 static HRESULT WINAPI d3d9_device_Clear(IDirect3DDevice9Ex *iface, DWORD rect_count,
1812 const D3DRECT *rects, DWORD flags, D3DCOLOR color, float z, DWORD stencil)
1814 const struct wined3d_color c =
1816 ((color >> 16) & 0xff) / 255.0f,
1817 ((color >> 8) & 0xff) / 255.0f,
1818 (color & 0xff) / 255.0f,
1819 ((color >> 24) & 0xff) / 255.0f,
1821 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1822 HRESULT hr;
1824 TRACE("iface %p, rect_count %u, rects %p, flags %#x, color 0x%08x, z %.8e, stencil %u.\n",
1825 iface, rect_count, rects, flags, color, z, stencil);
1827 if (rect_count && !rects)
1829 WARN("count %u with NULL rects.\n", rect_count);
1830 rect_count = 0;
1833 wined3d_mutex_lock();
1834 hr = wined3d_device_clear(device->wined3d_device, rect_count, (const RECT *)rects, flags, &c, z, stencil);
1835 wined3d_mutex_unlock();
1837 return hr;
1840 static HRESULT WINAPI d3d9_device_SetTransform(IDirect3DDevice9Ex *iface,
1841 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
1843 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1845 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1847 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1848 wined3d_mutex_lock();
1849 wined3d_device_set_transform(device->wined3d_device, state, (const struct wined3d_matrix *)matrix);
1850 wined3d_mutex_unlock();
1852 return D3D_OK;
1855 static HRESULT WINAPI d3d9_device_GetTransform(IDirect3DDevice9Ex *iface,
1856 D3DTRANSFORMSTATETYPE state, D3DMATRIX *matrix)
1858 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1860 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1862 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1863 wined3d_mutex_lock();
1864 wined3d_device_get_transform(device->wined3d_device, state, (struct wined3d_matrix *)matrix);
1865 wined3d_mutex_unlock();
1867 return D3D_OK;
1870 static HRESULT WINAPI d3d9_device_MultiplyTransform(IDirect3DDevice9Ex *iface,
1871 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
1873 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1875 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1877 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1878 wined3d_mutex_lock();
1879 wined3d_device_multiply_transform(device->wined3d_device, state, (const struct wined3d_matrix *)matrix);
1880 wined3d_mutex_unlock();
1882 return D3D_OK;
1885 static HRESULT WINAPI d3d9_device_SetViewport(IDirect3DDevice9Ex *iface, const D3DVIEWPORT9 *viewport)
1887 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1888 struct wined3d_viewport vp;
1890 TRACE("iface %p, viewport %p.\n", iface, viewport);
1892 vp.x = viewport->X;
1893 vp.y = viewport->Y;
1894 vp.width = viewport->Width;
1895 vp.height = viewport->Height;
1896 vp.min_z = viewport->MinZ;
1897 vp.max_z = viewport->MaxZ;
1899 wined3d_mutex_lock();
1900 wined3d_device_set_viewport(device->wined3d_device, &vp);
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);
1909 struct wined3d_viewport wined3d_viewport;
1911 TRACE("iface %p, viewport %p.\n", iface, viewport);
1913 wined3d_mutex_lock();
1914 wined3d_device_get_viewport(device->wined3d_device, &wined3d_viewport);
1915 wined3d_mutex_unlock();
1917 viewport->X = wined3d_viewport.x;
1918 viewport->Y = wined3d_viewport.y;
1919 viewport->Width = wined3d_viewport.width;
1920 viewport->Height = wined3d_viewport.height;
1921 viewport->MinZ = wined3d_viewport.min_z;
1922 viewport->MaxZ = wined3d_viewport.max_z;
1924 return D3D_OK;
1927 static HRESULT WINAPI d3d9_device_SetMaterial(IDirect3DDevice9Ex *iface, const D3DMATERIAL9 *material)
1929 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1931 TRACE("iface %p, material %p.\n", iface, material);
1933 /* Note: D3DMATERIAL9 is compatible with struct wined3d_material. */
1934 wined3d_mutex_lock();
1935 wined3d_device_set_material(device->wined3d_device, (const struct wined3d_material *)material);
1936 wined3d_mutex_unlock();
1938 return D3D_OK;
1941 static HRESULT WINAPI d3d9_device_GetMaterial(IDirect3DDevice9Ex *iface, D3DMATERIAL9 *material)
1943 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1945 TRACE("iface %p, material %p.\n", iface, material);
1947 /* Note: D3DMATERIAL9 is compatible with struct wined3d_material. */
1948 wined3d_mutex_lock();
1949 wined3d_device_get_material(device->wined3d_device, (struct wined3d_material *)material);
1950 wined3d_mutex_unlock();
1952 return D3D_OK;
1955 static HRESULT WINAPI d3d9_device_SetLight(IDirect3DDevice9Ex *iface, DWORD index, const D3DLIGHT9 *light)
1957 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1958 HRESULT hr;
1960 TRACE("iface %p, index %u, light %p.\n", iface, index, light);
1962 /* Note: D3DLIGHT9 is compatible with struct wined3d_light. */
1963 wined3d_mutex_lock();
1964 hr = wined3d_device_set_light(device->wined3d_device, index, (const struct wined3d_light *)light);
1965 wined3d_mutex_unlock();
1967 return hr;
1970 static HRESULT WINAPI d3d9_device_GetLight(IDirect3DDevice9Ex *iface, DWORD index, D3DLIGHT9 *light)
1972 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1973 HRESULT hr;
1975 TRACE("iface %p, index %u, light %p.\n", iface, index, light);
1977 /* Note: D3DLIGHT9 is compatible with struct wined3d_light. */
1978 wined3d_mutex_lock();
1979 hr = wined3d_device_get_light(device->wined3d_device, index, (struct wined3d_light *)light);
1980 wined3d_mutex_unlock();
1982 return hr;
1985 static HRESULT WINAPI d3d9_device_LightEnable(IDirect3DDevice9Ex *iface, DWORD index, BOOL enable)
1987 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1988 HRESULT hr;
1990 TRACE("iface %p, index %u, enable %#x.\n", iface, index, enable);
1992 wined3d_mutex_lock();
1993 hr = wined3d_device_set_light_enable(device->wined3d_device, index, enable);
1994 wined3d_mutex_unlock();
1996 return hr;
1999 static HRESULT WINAPI d3d9_device_GetLightEnable(IDirect3DDevice9Ex *iface, DWORD index, BOOL *enable)
2001 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2002 HRESULT hr;
2004 TRACE("iface %p, index %u, enable %p.\n", iface, index, enable);
2006 wined3d_mutex_lock();
2007 hr = wined3d_device_get_light_enable(device->wined3d_device, index, enable);
2008 wined3d_mutex_unlock();
2010 return hr;
2013 static HRESULT WINAPI d3d9_device_SetClipPlane(IDirect3DDevice9Ex *iface, DWORD index, const float *plane)
2015 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2016 HRESULT hr;
2018 TRACE("iface %p, index %u, plane %p.\n", iface, index, plane);
2020 wined3d_mutex_lock();
2021 hr = wined3d_device_set_clip_plane(device->wined3d_device, index, (const struct wined3d_vec4 *)plane);
2022 wined3d_mutex_unlock();
2024 return hr;
2027 static HRESULT WINAPI d3d9_device_GetClipPlane(IDirect3DDevice9Ex *iface, DWORD index, float *plane)
2029 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2030 HRESULT hr;
2032 TRACE("iface %p, index %u, plane %p.\n", iface, index, plane);
2034 wined3d_mutex_lock();
2035 hr = wined3d_device_get_clip_plane(device->wined3d_device, index, (struct wined3d_vec4 *)plane);
2036 wined3d_mutex_unlock();
2038 return hr;
2041 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_SetRenderState(IDirect3DDevice9Ex *iface,
2042 D3DRENDERSTATETYPE state, DWORD value)
2044 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2046 TRACE("iface %p, state %#x, value %#x.\n", iface, state, value);
2048 wined3d_mutex_lock();
2049 wined3d_device_set_render_state(device->wined3d_device, state, value);
2050 wined3d_mutex_unlock();
2052 return D3D_OK;
2055 static HRESULT WINAPI d3d9_device_GetRenderState(IDirect3DDevice9Ex *iface,
2056 D3DRENDERSTATETYPE state, DWORD *value)
2058 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2060 TRACE("iface %p, state %#x, value %p.\n", iface, state, value);
2062 wined3d_mutex_lock();
2063 *value = wined3d_device_get_render_state(device->wined3d_device, state);
2064 wined3d_mutex_unlock();
2066 return D3D_OK;
2069 static HRESULT WINAPI d3d9_device_CreateStateBlock(IDirect3DDevice9Ex *iface,
2070 D3DSTATEBLOCKTYPE type, IDirect3DStateBlock9 **stateblock)
2072 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2073 struct d3d9_stateblock *object;
2074 HRESULT hr;
2076 TRACE("iface %p, type %#x, stateblock %p.\n", iface, type, stateblock);
2078 if (type != D3DSBT_ALL && type != D3DSBT_PIXELSTATE && type != D3DSBT_VERTEXSTATE)
2080 WARN("Unexpected stateblock type, returning D3DERR_INVALIDCALL.\n");
2081 return D3DERR_INVALIDCALL;
2084 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2085 if (!object)
2086 return E_OUTOFMEMORY;
2088 hr = stateblock_init(object, device, type, NULL);
2089 if (FAILED(hr))
2091 WARN("Failed to initialize stateblock, hr %#x.\n", hr);
2092 HeapFree(GetProcessHeap(), 0, object);
2093 return hr;
2096 TRACE("Created stateblock %p.\n", object);
2097 *stateblock = &object->IDirect3DStateBlock9_iface;
2099 return D3D_OK;
2102 static HRESULT WINAPI d3d9_device_BeginStateBlock(IDirect3DDevice9Ex *iface)
2104 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2105 HRESULT hr;
2107 TRACE("iface %p.\n", iface);
2109 wined3d_mutex_lock();
2110 hr = wined3d_device_begin_stateblock(device->wined3d_device);
2111 wined3d_mutex_unlock();
2113 return hr;
2116 static HRESULT WINAPI d3d9_device_EndStateBlock(IDirect3DDevice9Ex *iface, IDirect3DStateBlock9 **stateblock)
2118 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2119 struct wined3d_stateblock *wined3d_stateblock;
2120 struct d3d9_stateblock *object;
2121 HRESULT hr;
2123 TRACE("iface %p, stateblock %p.\n", iface, stateblock);
2125 wined3d_mutex_lock();
2126 hr = wined3d_device_end_stateblock(device->wined3d_device, &wined3d_stateblock);
2127 wined3d_mutex_unlock();
2128 if (FAILED(hr))
2130 WARN("Failed to end the state block, hr %#x.\n", hr);
2131 return hr;
2134 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2135 if (!object)
2137 wined3d_mutex_lock();
2138 wined3d_stateblock_decref(wined3d_stateblock);
2139 wined3d_mutex_unlock();
2140 return E_OUTOFMEMORY;
2143 hr = stateblock_init(object, device, 0, wined3d_stateblock);
2144 if (FAILED(hr))
2146 WARN("Failed to initialize stateblock, hr %#x.\n", hr);
2147 wined3d_mutex_lock();
2148 wined3d_stateblock_decref(wined3d_stateblock);
2149 wined3d_mutex_unlock();
2150 HeapFree(GetProcessHeap(), 0, object);
2151 return hr;
2154 TRACE("Created stateblock %p.\n", object);
2155 *stateblock = &object->IDirect3DStateBlock9_iface;
2157 return D3D_OK;
2160 static HRESULT WINAPI d3d9_device_SetClipStatus(IDirect3DDevice9Ex *iface, const D3DCLIPSTATUS9 *clip_status)
2162 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2163 HRESULT hr;
2165 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
2167 wined3d_mutex_lock();
2168 hr = wined3d_device_set_clip_status(device->wined3d_device, (const struct wined3d_clip_status *)clip_status);
2169 wined3d_mutex_unlock();
2171 return hr;
2174 static HRESULT WINAPI d3d9_device_GetClipStatus(IDirect3DDevice9Ex *iface, D3DCLIPSTATUS9 *clip_status)
2176 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2177 HRESULT hr;
2179 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
2181 wined3d_mutex_lock();
2182 hr = wined3d_device_get_clip_status(device->wined3d_device, (struct wined3d_clip_status *)clip_status);
2183 wined3d_mutex_unlock();
2185 return hr;
2188 static HRESULT WINAPI d3d9_device_GetTexture(IDirect3DDevice9Ex *iface, DWORD stage, IDirect3DBaseTexture9 **texture)
2190 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2191 struct wined3d_texture *wined3d_texture = NULL;
2192 struct d3d9_texture *texture_impl;
2194 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
2196 if (!texture)
2197 return D3DERR_INVALIDCALL;
2199 wined3d_mutex_lock();
2200 if ((wined3d_texture = wined3d_device_get_texture(device->wined3d_device, stage)))
2202 texture_impl = wined3d_texture_get_parent(wined3d_texture);
2203 *texture = &texture_impl->IDirect3DBaseTexture9_iface;
2204 IDirect3DBaseTexture9_AddRef(*texture);
2206 else
2208 *texture = NULL;
2210 wined3d_mutex_unlock();
2212 return D3D_OK;
2215 static HRESULT WINAPI d3d9_device_SetTexture(IDirect3DDevice9Ex *iface, DWORD stage, IDirect3DBaseTexture9 *texture)
2217 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2218 struct d3d9_texture *texture_impl;
2219 HRESULT hr;
2221 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
2223 texture_impl = unsafe_impl_from_IDirect3DBaseTexture9(texture);
2225 wined3d_mutex_lock();
2226 hr = wined3d_device_set_texture(device->wined3d_device, stage,
2227 texture_impl ? texture_impl->wined3d_texture : NULL);
2228 wined3d_mutex_unlock();
2230 return hr;
2233 static const enum wined3d_texture_stage_state tss_lookup[] =
2235 WINED3D_TSS_INVALID, /* 0, unused */
2236 WINED3D_TSS_COLOR_OP, /* 1, D3DTSS_COLOROP */
2237 WINED3D_TSS_COLOR_ARG1, /* 2, D3DTSS_COLORARG1 */
2238 WINED3D_TSS_COLOR_ARG2, /* 3, D3DTSS_COLORARG2 */
2239 WINED3D_TSS_ALPHA_OP, /* 4, D3DTSS_ALPHAOP */
2240 WINED3D_TSS_ALPHA_ARG1, /* 5, D3DTSS_ALPHAARG1 */
2241 WINED3D_TSS_ALPHA_ARG2, /* 6, D3DTSS_ALPHAARG2 */
2242 WINED3D_TSS_BUMPENV_MAT00, /* 7, D3DTSS_BUMPENVMAT00 */
2243 WINED3D_TSS_BUMPENV_MAT01, /* 8, D3DTSS_BUMPENVMAT01 */
2244 WINED3D_TSS_BUMPENV_MAT10, /* 9, D3DTSS_BUMPENVMAT10 */
2245 WINED3D_TSS_BUMPENV_MAT11, /* 10, D3DTSS_BUMPENVMAT11 */
2246 WINED3D_TSS_TEXCOORD_INDEX, /* 11, D3DTSS_TEXCOORDINDEX */
2247 WINED3D_TSS_INVALID, /* 12, unused */
2248 WINED3D_TSS_INVALID, /* 13, unused */
2249 WINED3D_TSS_INVALID, /* 14, unused */
2250 WINED3D_TSS_INVALID, /* 15, unused */
2251 WINED3D_TSS_INVALID, /* 16, unused */
2252 WINED3D_TSS_INVALID, /* 17, unused */
2253 WINED3D_TSS_INVALID, /* 18, unused */
2254 WINED3D_TSS_INVALID, /* 19, unused */
2255 WINED3D_TSS_INVALID, /* 20, unused */
2256 WINED3D_TSS_INVALID, /* 21, unused */
2257 WINED3D_TSS_BUMPENV_LSCALE, /* 22, D3DTSS_BUMPENVLSCALE */
2258 WINED3D_TSS_BUMPENV_LOFFSET, /* 23, D3DTSS_BUMPENVLOFFSET */
2259 WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS, /* 24, D3DTSS_TEXTURETRANSFORMFLAGS */
2260 WINED3D_TSS_INVALID, /* 25, unused */
2261 WINED3D_TSS_COLOR_ARG0, /* 26, D3DTSS_COLORARG0 */
2262 WINED3D_TSS_ALPHA_ARG0, /* 27, D3DTSS_ALPHAARG0 */
2263 WINED3D_TSS_RESULT_ARG, /* 28, D3DTSS_RESULTARG */
2264 WINED3D_TSS_INVALID, /* 29, unused */
2265 WINED3D_TSS_INVALID, /* 30, unused */
2266 WINED3D_TSS_INVALID, /* 31, unused */
2267 WINED3D_TSS_CONSTANT, /* 32, D3DTSS_CONSTANT */
2270 static HRESULT WINAPI d3d9_device_GetTextureStageState(IDirect3DDevice9Ex *iface,
2271 DWORD stage, D3DTEXTURESTAGESTATETYPE state, DWORD *value)
2273 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2275 TRACE("iface %p, stage %u, state %#x, value %p.\n", iface, stage, state, value);
2277 if (state >= sizeof(tss_lookup) / sizeof(*tss_lookup))
2279 WARN("Invalid state %#x passed.\n", state);
2280 return D3D_OK;
2283 wined3d_mutex_lock();
2284 *value = wined3d_device_get_texture_stage_state(device->wined3d_device, stage, tss_lookup[state]);
2285 wined3d_mutex_unlock();
2287 return D3D_OK;
2290 static HRESULT WINAPI d3d9_device_SetTextureStageState(IDirect3DDevice9Ex *iface,
2291 DWORD stage, D3DTEXTURESTAGESTATETYPE state, DWORD value)
2293 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2295 TRACE("iface %p, stage %u, state %#x, value %#x.\n", iface, stage, state, value);
2297 if (state >= sizeof(tss_lookup) / sizeof(*tss_lookup))
2299 WARN("Invalid state %#x passed.\n", state);
2300 return D3D_OK;
2303 wined3d_mutex_lock();
2304 wined3d_device_set_texture_stage_state(device->wined3d_device, stage, tss_lookup[state], value);
2305 wined3d_mutex_unlock();
2307 return D3D_OK;
2310 static HRESULT WINAPI d3d9_device_GetSamplerState(IDirect3DDevice9Ex *iface,
2311 DWORD sampler, D3DSAMPLERSTATETYPE state, DWORD *value)
2313 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2315 TRACE("iface %p, sampler %u, state %#x, value %p.\n", iface, sampler, state, value);
2317 wined3d_mutex_lock();
2318 *value = wined3d_device_get_sampler_state(device->wined3d_device, sampler, state);
2319 wined3d_mutex_unlock();
2321 return D3D_OK;
2324 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_SetSamplerState(IDirect3DDevice9Ex *iface,
2325 DWORD sampler, D3DSAMPLERSTATETYPE state, DWORD value)
2327 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2329 TRACE("iface %p, sampler %u, state %#x, value %#x.\n", iface, sampler, state, value);
2331 wined3d_mutex_lock();
2332 wined3d_device_set_sampler_state(device->wined3d_device, sampler, state, value);
2333 wined3d_mutex_unlock();
2335 return D3D_OK;
2338 static HRESULT WINAPI d3d9_device_ValidateDevice(IDirect3DDevice9Ex *iface, DWORD *pass_count)
2340 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2341 HRESULT hr;
2343 TRACE("iface %p, pass_count %p.\n", iface, pass_count);
2345 wined3d_mutex_lock();
2346 hr = wined3d_device_validate_device(device->wined3d_device, pass_count);
2347 wined3d_mutex_unlock();
2349 return hr;
2352 static HRESULT WINAPI d3d9_device_SetPaletteEntries(IDirect3DDevice9Ex *iface,
2353 UINT palette_idx, const PALETTEENTRY *entries)
2355 WARN("iface %p, palette_idx %u, entries %p unimplemented.\n", iface, palette_idx, entries);
2357 /* The d3d9 palette API is non-functional on Windows. Getters and setters are implemented,
2358 * and some drivers allow the creation of P8 surfaces. These surfaces can be copied to
2359 * other P8 surfaces with StretchRect, but cannot be converted to (A)RGB.
2361 * Some older(dx7) cards may have support for P8 textures, but games cannot rely on this. */
2362 return D3D_OK;
2365 static HRESULT WINAPI d3d9_device_GetPaletteEntries(IDirect3DDevice9Ex *iface,
2366 UINT palette_idx, PALETTEENTRY *entries)
2368 FIXME("iface %p, palette_idx %u, entries %p unimplemented.\n", iface, palette_idx, entries);
2370 return D3DERR_INVALIDCALL;
2373 static HRESULT WINAPI d3d9_device_SetCurrentTexturePalette(IDirect3DDevice9Ex *iface, UINT palette_idx)
2375 WARN("iface %p, palette_idx %u unimplemented.\n", iface, palette_idx);
2377 return D3D_OK;
2380 static HRESULT WINAPI d3d9_device_GetCurrentTexturePalette(IDirect3DDevice9Ex *iface, UINT *palette_idx)
2382 FIXME("iface %p, palette_idx %p.\n", iface, palette_idx);
2384 return D3DERR_INVALIDCALL;
2387 static HRESULT WINAPI d3d9_device_SetScissorRect(IDirect3DDevice9Ex *iface, const RECT *rect)
2389 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2391 TRACE("iface %p, rect %p.\n", iface, rect);
2393 wined3d_mutex_lock();
2394 wined3d_device_set_scissor_rect(device->wined3d_device, rect);
2395 wined3d_mutex_unlock();
2397 return D3D_OK;
2400 static HRESULT WINAPI d3d9_device_GetScissorRect(IDirect3DDevice9Ex *iface, RECT *rect)
2402 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2404 TRACE("iface %p, rect %p.\n", iface, rect);
2406 wined3d_mutex_lock();
2407 wined3d_device_get_scissor_rect(device->wined3d_device, rect);
2408 wined3d_mutex_unlock();
2410 return D3D_OK;
2413 static HRESULT WINAPI d3d9_device_SetSoftwareVertexProcessing(IDirect3DDevice9Ex *iface, BOOL software)
2415 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2417 TRACE("iface %p, software %#x.\n", iface, software);
2419 wined3d_mutex_lock();
2420 wined3d_device_set_software_vertex_processing(device->wined3d_device, software);
2421 wined3d_mutex_unlock();
2423 return D3D_OK;
2426 static BOOL WINAPI d3d9_device_GetSoftwareVertexProcessing(IDirect3DDevice9Ex *iface)
2428 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2429 BOOL ret;
2431 TRACE("iface %p.\n", iface);
2433 wined3d_mutex_lock();
2434 ret = wined3d_device_get_software_vertex_processing(device->wined3d_device);
2435 wined3d_mutex_unlock();
2437 return ret;
2440 static HRESULT WINAPI d3d9_device_SetNPatchMode(IDirect3DDevice9Ex *iface, float segment_count)
2442 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2443 HRESULT hr;
2445 TRACE("iface %p, segment_count %.8e.\n", iface, segment_count);
2447 wined3d_mutex_lock();
2448 hr = wined3d_device_set_npatch_mode(device->wined3d_device, segment_count);
2449 wined3d_mutex_unlock();
2451 return hr;
2454 static float WINAPI d3d9_device_GetNPatchMode(IDirect3DDevice9Ex *iface)
2456 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2457 float ret;
2459 TRACE("iface %p.\n", iface);
2461 wined3d_mutex_lock();
2462 ret = wined3d_device_get_npatch_mode(device->wined3d_device);
2463 wined3d_mutex_unlock();
2465 return ret;
2468 static HRESULT WINAPI d3d9_device_DrawPrimitive(IDirect3DDevice9Ex *iface,
2469 D3DPRIMITIVETYPE primitive_type, UINT start_vertex, UINT primitive_count)
2471 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2472 HRESULT hr;
2474 TRACE("iface %p, primitive_type %#x, start_vertex %u, primitive_count %u.\n",
2475 iface, primitive_type, start_vertex, primitive_count);
2477 wined3d_mutex_lock();
2478 if (!device->has_vertex_declaration)
2480 wined3d_mutex_unlock();
2481 WARN("Called without a valid vertex declaration set.\n");
2482 return D3DERR_INVALIDCALL;
2484 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type, 0);
2485 hr = wined3d_device_draw_primitive(device->wined3d_device, start_vertex,
2486 vertex_count_from_primitive_count(primitive_type, primitive_count));
2487 wined3d_mutex_unlock();
2489 return hr;
2492 static HRESULT WINAPI d3d9_device_DrawIndexedPrimitive(IDirect3DDevice9Ex *iface,
2493 D3DPRIMITIVETYPE primitive_type, INT base_vertex_idx, UINT min_vertex_idx,
2494 UINT vertex_count, UINT start_idx, UINT primitive_count)
2496 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2497 HRESULT hr;
2499 TRACE("iface %p, primitive_type %#x, base_vertex_idx %u, min_vertex_idx %u, "
2500 "vertex_count %u, start_idx %u, primitive_count %u.\n",
2501 iface, primitive_type, base_vertex_idx, min_vertex_idx,
2502 vertex_count, start_idx, primitive_count);
2504 wined3d_mutex_lock();
2505 if (!device->has_vertex_declaration)
2507 wined3d_mutex_unlock();
2508 WARN("Called without a valid vertex declaration set.\n");
2509 return D3DERR_INVALIDCALL;
2511 wined3d_device_set_base_vertex_index(device->wined3d_device, base_vertex_idx);
2512 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type, 0);
2513 hr = wined3d_device_draw_indexed_primitive(device->wined3d_device, start_idx,
2514 vertex_count_from_primitive_count(primitive_type, primitive_count));
2515 wined3d_mutex_unlock();
2517 return hr;
2520 /* The caller is responsible for wined3d locking */
2521 static HRESULT d3d9_device_prepare_vertex_buffer(struct d3d9_device *device, UINT min_size)
2523 HRESULT hr;
2525 if (device->vertex_buffer_size < min_size || !device->vertex_buffer)
2527 UINT size = max(device->vertex_buffer_size * 2, min_size);
2528 struct wined3d_buffer *buffer;
2530 TRACE("Growing vertex buffer to %u bytes.\n", size);
2532 hr = wined3d_buffer_create_vb(device->wined3d_device, size, WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_WRITEONLY,
2533 WINED3D_POOL_DEFAULT, NULL, &d3d9_null_wined3d_parent_ops, &buffer);
2534 if (FAILED(hr))
2536 ERR("(%p) wined3d_buffer_create_vb failed with hr = %08x.\n", device, hr);
2537 return hr;
2540 if (device->vertex_buffer)
2541 wined3d_buffer_decref(device->vertex_buffer);
2543 device->vertex_buffer = buffer;
2544 device->vertex_buffer_size = size;
2545 device->vertex_buffer_pos = 0;
2547 return D3D_OK;
2550 static HRESULT WINAPI d3d9_device_DrawPrimitiveUP(IDirect3DDevice9Ex *iface,
2551 D3DPRIMITIVETYPE primitive_type, UINT primitive_count, const void *data, UINT stride)
2553 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2554 HRESULT hr;
2555 UINT vtx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2556 struct wined3d_map_desc wined3d_map_desc;
2557 struct wined3d_box wined3d_box = {0};
2558 UINT size = vtx_count * stride;
2559 struct wined3d_resource *vb;
2560 UINT vb_pos, align;
2562 TRACE("iface %p, primitive_type %#x, primitive_count %u, data %p, stride %u.\n",
2563 iface, primitive_type, primitive_count, data, stride);
2565 if (!primitive_count)
2567 WARN("primitive_count is 0, returning D3D_OK\n");
2568 return D3D_OK;
2571 wined3d_mutex_lock();
2573 if (!device->has_vertex_declaration)
2575 wined3d_mutex_unlock();
2576 WARN("Called without a valid vertex declaration set.\n");
2577 return D3DERR_INVALIDCALL;
2580 hr = d3d9_device_prepare_vertex_buffer(device, size);
2581 if (FAILED(hr))
2582 goto done;
2584 vb_pos = device->vertex_buffer_pos;
2585 align = vb_pos % stride;
2586 if (align) align = stride - align;
2587 if (vb_pos + size + align > device->vertex_buffer_size)
2588 vb_pos = 0;
2589 else
2590 vb_pos += align;
2592 wined3d_box.left = vb_pos;
2593 wined3d_box.right = vb_pos + size;
2594 vb = wined3d_buffer_get_resource(device->vertex_buffer);
2595 if (FAILED(hr = wined3d_resource_map(vb, 0, &wined3d_map_desc, &wined3d_box,
2596 vb_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD)))
2597 goto done;
2598 memcpy(wined3d_map_desc.data, data, size);
2599 wined3d_resource_unmap(vb, 0);
2600 device->vertex_buffer_pos = vb_pos + size;
2602 hr = wined3d_device_set_stream_source(device->wined3d_device, 0, device->vertex_buffer, 0, stride);
2603 if (FAILED(hr))
2604 goto done;
2606 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type, 0);
2607 hr = wined3d_device_draw_primitive(device->wined3d_device, vb_pos / stride, vtx_count);
2608 wined3d_device_set_stream_source(device->wined3d_device, 0, NULL, 0, 0);
2610 done:
2611 wined3d_mutex_unlock();
2612 return hr;
2615 /* The caller is responsible for wined3d locking */
2616 static HRESULT d3d9_device_prepare_index_buffer(struct d3d9_device *device, UINT min_size)
2618 HRESULT hr;
2620 if (device->index_buffer_size < min_size || !device->index_buffer)
2622 UINT size = max(device->index_buffer_size * 2, min_size);
2623 struct wined3d_buffer *buffer;
2625 TRACE("Growing index buffer to %u bytes.\n", size);
2627 hr = wined3d_buffer_create_ib(device->wined3d_device, size, WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_WRITEONLY,
2628 WINED3D_POOL_DEFAULT, NULL, &d3d9_null_wined3d_parent_ops, &buffer);
2629 if (FAILED(hr))
2631 ERR("(%p) wined3d_buffer_create_ib failed with hr = %08x.\n", device, hr);
2632 return hr;
2635 if (device->index_buffer)
2636 wined3d_buffer_decref(device->index_buffer);
2638 device->index_buffer = buffer;
2639 device->index_buffer_size = size;
2640 device->index_buffer_pos = 0;
2642 return D3D_OK;
2645 static HRESULT WINAPI d3d9_device_DrawIndexedPrimitiveUP(IDirect3DDevice9Ex *iface,
2646 D3DPRIMITIVETYPE primitive_type, UINT min_vertex_idx, UINT vertex_count,
2647 UINT primitive_count, const void *index_data, D3DFORMAT index_format,
2648 const void *vertex_data, UINT vertex_stride)
2650 UINT idx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2651 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2652 UINT idx_fmt_size = index_format == D3DFMT_INDEX16 ? 2 : 4;
2653 UINT vtx_size = vertex_count * vertex_stride;
2654 UINT idx_size = idx_count * idx_fmt_size;
2655 struct wined3d_map_desc wined3d_map_desc;
2656 struct wined3d_box wined3d_box = {0};
2657 struct wined3d_resource *ib, *vb;
2658 UINT vb_pos, ib_pos, align;
2659 HRESULT hr;
2661 TRACE("iface %p, primitive_type %#x, min_vertex_idx %u, vertex_count %u, primitive_count %u, "
2662 "index_data %p, index_format %#x, vertex_data %p, vertex_stride %u.\n",
2663 iface, primitive_type, min_vertex_idx, vertex_count, primitive_count,
2664 index_data, index_format, vertex_data, vertex_stride);
2666 if (!primitive_count)
2668 WARN("primitive_count is 0, returning D3D_OK.\n");
2669 return D3D_OK;
2672 wined3d_mutex_lock();
2674 if (!device->has_vertex_declaration)
2676 wined3d_mutex_unlock();
2677 WARN("Called without a valid vertex declaration set.\n");
2678 return D3DERR_INVALIDCALL;
2681 hr = d3d9_device_prepare_vertex_buffer(device, vtx_size);
2682 if (FAILED(hr))
2683 goto done;
2685 vb_pos = device->vertex_buffer_pos;
2686 align = vb_pos % vertex_stride;
2687 if (align) align = vertex_stride - align;
2688 if (vb_pos + vtx_size + align > device->vertex_buffer_size)
2689 vb_pos = 0;
2690 else
2691 vb_pos += align;
2693 wined3d_box.left = vb_pos;
2694 wined3d_box.right = vb_pos + vtx_size;
2695 vb = wined3d_buffer_get_resource(device->vertex_buffer);
2696 if (FAILED(hr = wined3d_resource_map(vb, 0, &wined3d_map_desc, &wined3d_box,
2697 vb_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD)))
2698 goto done;
2699 memcpy(wined3d_map_desc.data, (char *)vertex_data + min_vertex_idx * vertex_stride, vtx_size);
2700 wined3d_resource_unmap(vb, 0);
2701 device->vertex_buffer_pos = vb_pos + vtx_size;
2703 hr = d3d9_device_prepare_index_buffer(device, idx_size);
2704 if (FAILED(hr))
2705 goto done;
2707 ib_pos = device->index_buffer_pos;
2708 align = ib_pos % idx_fmt_size;
2709 if (align) align = idx_fmt_size - align;
2710 if (ib_pos + idx_size + align > device->index_buffer_size)
2711 ib_pos = 0;
2712 else
2713 ib_pos += align;
2715 wined3d_box.left = ib_pos;
2716 wined3d_box.right = ib_pos + idx_size;
2717 ib = wined3d_buffer_get_resource(device->index_buffer);
2718 if (FAILED(hr = wined3d_resource_map(ib, 0, &wined3d_map_desc, &wined3d_box,
2719 ib_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD)))
2720 goto done;
2721 memcpy(wined3d_map_desc.data, index_data, idx_size);
2722 wined3d_resource_unmap(ib, 0);
2723 device->index_buffer_pos = ib_pos + idx_size;
2725 hr = wined3d_device_set_stream_source(device->wined3d_device, 0, device->vertex_buffer, 0, vertex_stride);
2726 if (FAILED(hr))
2727 goto done;
2729 wined3d_device_set_index_buffer(device->wined3d_device, device->index_buffer,
2730 wined3dformat_from_d3dformat(index_format), 0);
2731 wined3d_device_set_base_vertex_index(device->wined3d_device, vb_pos / vertex_stride - min_vertex_idx);
2733 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type, 0);
2734 hr = wined3d_device_draw_indexed_primitive(device->wined3d_device, ib_pos / idx_fmt_size, idx_count);
2736 wined3d_device_set_stream_source(device->wined3d_device, 0, NULL, 0, 0);
2737 wined3d_device_set_index_buffer(device->wined3d_device, NULL, WINED3DFMT_UNKNOWN, 0);
2738 wined3d_device_set_base_vertex_index(device->wined3d_device, 0);
2740 done:
2741 wined3d_mutex_unlock();
2742 return hr;
2745 static HRESULT WINAPI d3d9_device_ProcessVertices(IDirect3DDevice9Ex *iface,
2746 UINT src_start_idx, UINT dst_idx, UINT vertex_count, IDirect3DVertexBuffer9 *dst_buffer,
2747 IDirect3DVertexDeclaration9 *declaration, DWORD flags)
2749 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2750 struct d3d9_vertexbuffer *dst_impl = unsafe_impl_from_IDirect3DVertexBuffer9(dst_buffer);
2751 struct d3d9_vertex_declaration *decl_impl = unsafe_impl_from_IDirect3DVertexDeclaration9(declaration);
2752 HRESULT hr;
2754 TRACE("iface %p, src_start_idx %u, dst_idx %u, vertex_count %u, dst_buffer %p, declaration %p, flags %#x.\n",
2755 iface, src_start_idx, dst_idx, vertex_count, dst_buffer, declaration, flags);
2757 wined3d_mutex_lock();
2758 hr = wined3d_device_process_vertices(device->wined3d_device, src_start_idx, dst_idx, vertex_count,
2759 dst_impl->wined3d_buffer, decl_impl ? decl_impl->wined3d_declaration : NULL,
2760 flags, dst_impl->fvf);
2761 wined3d_mutex_unlock();
2763 return hr;
2766 static HRESULT WINAPI d3d9_device_CreateVertexDeclaration(IDirect3DDevice9Ex *iface,
2767 const D3DVERTEXELEMENT9 *elements, IDirect3DVertexDeclaration9 **declaration)
2769 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2770 struct d3d9_vertex_declaration *object;
2771 HRESULT hr;
2773 TRACE("iface %p, elements %p, declaration %p.\n", iface, elements, declaration);
2775 if (!declaration)
2777 WARN("Caller passed a NULL declaration, returning D3DERR_INVALIDCALL.\n");
2778 return D3DERR_INVALIDCALL;
2781 if (SUCCEEDED(hr = d3d9_vertex_declaration_create(device, elements, &object)))
2782 *declaration = &object->IDirect3DVertexDeclaration9_iface;
2784 return hr;
2787 static HRESULT WINAPI d3d9_device_SetVertexDeclaration(IDirect3DDevice9Ex *iface,
2788 IDirect3DVertexDeclaration9 *declaration)
2790 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2791 struct d3d9_vertex_declaration *decl_impl = unsafe_impl_from_IDirect3DVertexDeclaration9(declaration);
2793 TRACE("iface %p, declaration %p.\n", iface, declaration);
2795 wined3d_mutex_lock();
2796 wined3d_device_set_vertex_declaration(device->wined3d_device,
2797 decl_impl ? decl_impl->wined3d_declaration : NULL);
2798 device->has_vertex_declaration = !!decl_impl;
2799 wined3d_mutex_unlock();
2801 return D3D_OK;
2804 static HRESULT WINAPI d3d9_device_GetVertexDeclaration(IDirect3DDevice9Ex *iface,
2805 IDirect3DVertexDeclaration9 **declaration)
2807 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2808 struct wined3d_vertex_declaration *wined3d_declaration;
2809 struct d3d9_vertex_declaration *declaration_impl;
2811 TRACE("iface %p, declaration %p.\n", iface, declaration);
2813 if (!declaration) return D3DERR_INVALIDCALL;
2815 wined3d_mutex_lock();
2816 if ((wined3d_declaration = wined3d_device_get_vertex_declaration(device->wined3d_device)))
2818 declaration_impl = wined3d_vertex_declaration_get_parent(wined3d_declaration);
2819 *declaration = &declaration_impl->IDirect3DVertexDeclaration9_iface;
2820 IDirect3DVertexDeclaration9_AddRef(*declaration);
2822 else
2824 *declaration = NULL;
2826 wined3d_mutex_unlock();
2828 TRACE("Returning %p.\n", *declaration);
2829 return D3D_OK;
2832 static struct wined3d_vertex_declaration *device_get_fvf_declaration(struct d3d9_device *device, DWORD fvf)
2834 struct wined3d_vertex_declaration *wined3d_declaration;
2835 struct fvf_declaration *fvf_decls = device->fvf_decls;
2836 struct d3d9_vertex_declaration *d3d9_declaration;
2837 D3DVERTEXELEMENT9 *elements;
2838 int p, low, high; /* deliberately signed */
2839 HRESULT hr;
2841 TRACE("Searching for declaration for fvf %08x... ", fvf);
2843 low = 0;
2844 high = device->fvf_decl_count - 1;
2845 while (low <= high)
2847 p = (low + high) >> 1;
2848 TRACE("%d ", p);
2850 if (fvf_decls[p].fvf == fvf)
2852 TRACE("found %p.\n", fvf_decls[p].decl);
2853 return fvf_decls[p].decl;
2856 if (fvf_decls[p].fvf < fvf)
2857 low = p + 1;
2858 else
2859 high = p - 1;
2861 TRACE("not found. Creating and inserting at position %d.\n", low);
2863 if (FAILED(hr = vdecl_convert_fvf(fvf, &elements)))
2864 return NULL;
2866 hr = d3d9_vertex_declaration_create(device, elements, &d3d9_declaration);
2867 HeapFree(GetProcessHeap(), 0, elements);
2868 if (FAILED(hr))
2869 return NULL;
2871 if (device->fvf_decl_size == device->fvf_decl_count)
2873 UINT grow = max(device->fvf_decl_size / 2, 8);
2875 fvf_decls = HeapReAlloc(GetProcessHeap(), 0, fvf_decls, sizeof(*fvf_decls) * (device->fvf_decl_size + grow));
2876 if (!fvf_decls)
2878 IDirect3DVertexDeclaration9_Release(&d3d9_declaration->IDirect3DVertexDeclaration9_iface);
2879 return NULL;
2881 device->fvf_decls = fvf_decls;
2882 device->fvf_decl_size += grow;
2885 d3d9_declaration->fvf = fvf;
2886 wined3d_declaration = d3d9_declaration->wined3d_declaration;
2887 wined3d_vertex_declaration_incref(wined3d_declaration);
2888 IDirect3DVertexDeclaration9_Release(&d3d9_declaration->IDirect3DVertexDeclaration9_iface);
2890 memmove(fvf_decls + low + 1, fvf_decls + low, sizeof(*fvf_decls) * (device->fvf_decl_count - low));
2891 fvf_decls[low].decl = wined3d_declaration;
2892 fvf_decls[low].fvf = fvf;
2893 ++device->fvf_decl_count;
2895 TRACE("Returning %p. %u declarations in array.\n", wined3d_declaration, device->fvf_decl_count);
2897 return wined3d_declaration;
2900 static HRESULT WINAPI d3d9_device_SetFVF(IDirect3DDevice9Ex *iface, DWORD fvf)
2902 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2903 struct wined3d_vertex_declaration *decl;
2905 TRACE("iface %p, fvf %#x.\n", iface, fvf);
2907 if (!fvf)
2909 WARN("%#x is not a valid FVF.\n", fvf);
2910 return D3D_OK;
2913 wined3d_mutex_lock();
2914 if (!(decl = device_get_fvf_declaration(device, fvf)))
2916 wined3d_mutex_unlock();
2917 ERR("Failed to create a vertex declaration for fvf %#x.\n", fvf);
2918 return D3DERR_DRIVERINTERNALERROR;
2921 wined3d_device_set_vertex_declaration(device->wined3d_device, decl);
2922 device->has_vertex_declaration = TRUE;
2923 wined3d_mutex_unlock();
2925 return D3D_OK;
2928 static HRESULT WINAPI d3d9_device_GetFVF(IDirect3DDevice9Ex *iface, DWORD *fvf)
2930 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2931 struct wined3d_vertex_declaration *wined3d_declaration;
2932 struct d3d9_vertex_declaration *d3d9_declaration;
2934 TRACE("iface %p, fvf %p.\n", iface, fvf);
2936 wined3d_mutex_lock();
2937 if ((wined3d_declaration = wined3d_device_get_vertex_declaration(device->wined3d_device)))
2939 d3d9_declaration = wined3d_vertex_declaration_get_parent(wined3d_declaration);
2940 *fvf = d3d9_declaration->fvf;
2942 else
2944 *fvf = 0;
2946 wined3d_mutex_unlock();
2948 TRACE("Returning FVF %#x.\n", *fvf);
2950 return D3D_OK;
2953 static HRESULT WINAPI d3d9_device_CreateVertexShader(IDirect3DDevice9Ex *iface,
2954 const DWORD *byte_code, IDirect3DVertexShader9 **shader)
2956 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2957 struct d3d9_vertexshader *object;
2958 HRESULT hr;
2960 TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
2962 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2963 if (!object)
2964 return E_OUTOFMEMORY;
2966 hr = vertexshader_init(object, device, byte_code);
2967 if (FAILED(hr))
2969 WARN("Failed to initialize vertex shader, hr %#x.\n", hr);
2970 HeapFree(GetProcessHeap(), 0, object);
2971 return hr;
2974 TRACE("Created vertex shader %p.\n", object);
2975 *shader = &object->IDirect3DVertexShader9_iface;
2977 return D3D_OK;
2980 static HRESULT WINAPI d3d9_device_SetVertexShader(IDirect3DDevice9Ex *iface, IDirect3DVertexShader9 *shader)
2982 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2983 struct d3d9_vertexshader *shader_obj = unsafe_impl_from_IDirect3DVertexShader9(shader);
2985 TRACE("iface %p, shader %p.\n", iface, shader);
2987 wined3d_mutex_lock();
2988 wined3d_device_set_vertex_shader(device->wined3d_device,
2989 shader_obj ? shader_obj->wined3d_shader : NULL);
2990 wined3d_mutex_unlock();
2992 return D3D_OK;
2995 static HRESULT WINAPI d3d9_device_GetVertexShader(IDirect3DDevice9Ex *iface, IDirect3DVertexShader9 **shader)
2997 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2998 struct d3d9_vertexshader *shader_impl;
2999 struct wined3d_shader *wined3d_shader;
3001 TRACE("iface %p, shader %p.\n", iface, shader);
3003 wined3d_mutex_lock();
3004 if ((wined3d_shader = wined3d_device_get_vertex_shader(device->wined3d_device)))
3006 shader_impl = wined3d_shader_get_parent(wined3d_shader);
3007 *shader = &shader_impl->IDirect3DVertexShader9_iface;
3008 IDirect3DVertexShader9_AddRef(*shader);
3010 else
3012 *shader = NULL;
3014 wined3d_mutex_unlock();
3016 TRACE("Returning %p.\n", *shader);
3018 return D3D_OK;
3021 static HRESULT WINAPI d3d9_device_SetVertexShaderConstantF(IDirect3DDevice9Ex *iface,
3022 UINT reg_idx, const float *data, UINT count)
3024 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3025 HRESULT hr;
3027 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3029 if (reg_idx + count > D3D9_MAX_VERTEX_SHADER_CONSTANTF)
3031 WARN("Trying to access %u constants, but d3d9 only supports %u\n",
3032 reg_idx + count, D3D9_MAX_VERTEX_SHADER_CONSTANTF);
3033 return D3DERR_INVALIDCALL;
3036 wined3d_mutex_lock();
3037 hr = wined3d_device_set_vs_consts_f(device->wined3d_device,
3038 reg_idx, count, (const struct wined3d_vec4 *)data);
3039 wined3d_mutex_unlock();
3041 return hr;
3044 static HRESULT WINAPI d3d9_device_GetVertexShaderConstantF(IDirect3DDevice9Ex *iface,
3045 UINT reg_idx, float *data, UINT count)
3047 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3048 HRESULT hr;
3050 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3052 if (reg_idx + count > D3D9_MAX_VERTEX_SHADER_CONSTANTF)
3054 WARN("Trying to access %u constants, but d3d9 only supports %u\n",
3055 reg_idx + count, D3D9_MAX_VERTEX_SHADER_CONSTANTF);
3056 return D3DERR_INVALIDCALL;
3059 wined3d_mutex_lock();
3060 hr = wined3d_device_get_vs_consts_f(device->wined3d_device,
3061 reg_idx, count, (struct wined3d_vec4 *)data);
3062 wined3d_mutex_unlock();
3064 return hr;
3067 static HRESULT WINAPI d3d9_device_SetVertexShaderConstantI(IDirect3DDevice9Ex *iface,
3068 UINT reg_idx, const int *data, UINT count)
3070 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3071 HRESULT hr;
3073 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3075 wined3d_mutex_lock();
3076 hr = wined3d_device_set_vs_consts_i(device->wined3d_device,
3077 reg_idx, count, (const struct wined3d_ivec4 *)data);
3078 wined3d_mutex_unlock();
3080 return hr;
3083 static HRESULT WINAPI d3d9_device_GetVertexShaderConstantI(IDirect3DDevice9Ex *iface,
3084 UINT reg_idx, int *data, UINT count)
3086 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3087 HRESULT hr;
3089 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3091 wined3d_mutex_lock();
3092 hr = wined3d_device_get_vs_consts_i(device->wined3d_device,
3093 reg_idx, count, (struct wined3d_ivec4 *)data);
3094 wined3d_mutex_unlock();
3096 return hr;
3099 static HRESULT WINAPI d3d9_device_SetVertexShaderConstantB(IDirect3DDevice9Ex *iface,
3100 UINT reg_idx, const BOOL *data, UINT count)
3102 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3103 HRESULT hr;
3105 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3107 wined3d_mutex_lock();
3108 hr = wined3d_device_set_vs_consts_b(device->wined3d_device, reg_idx, count, data);
3109 wined3d_mutex_unlock();
3111 return hr;
3114 static HRESULT WINAPI d3d9_device_GetVertexShaderConstantB(IDirect3DDevice9Ex *iface,
3115 UINT reg_idx, BOOL *data, UINT count)
3117 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3118 HRESULT hr;
3120 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3122 wined3d_mutex_lock();
3123 hr = wined3d_device_get_vs_consts_b(device->wined3d_device, reg_idx, count, data);
3124 wined3d_mutex_unlock();
3126 return hr;
3129 static HRESULT WINAPI d3d9_device_SetStreamSource(IDirect3DDevice9Ex *iface,
3130 UINT stream_idx, IDirect3DVertexBuffer9 *buffer, UINT offset, UINT stride)
3132 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3133 struct d3d9_vertexbuffer *buffer_impl = unsafe_impl_from_IDirect3DVertexBuffer9(buffer);
3134 HRESULT hr;
3136 TRACE("iface %p, stream_idx %u, buffer %p, offset %u, stride %u.\n",
3137 iface, stream_idx, buffer, offset, stride);
3139 wined3d_mutex_lock();
3140 hr = wined3d_device_set_stream_source(device->wined3d_device, stream_idx,
3141 buffer_impl ? buffer_impl->wined3d_buffer : NULL, offset, stride);
3142 wined3d_mutex_unlock();
3144 return hr;
3147 static HRESULT WINAPI d3d9_device_GetStreamSource(IDirect3DDevice9Ex *iface,
3148 UINT stream_idx, IDirect3DVertexBuffer9 **buffer, UINT *offset, UINT *stride)
3150 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3151 struct d3d9_vertexbuffer *buffer_impl;
3152 struct wined3d_buffer *wined3d_buffer;
3153 HRESULT hr;
3155 TRACE("iface %p, stream_idx %u, buffer %p, offset %p, stride %p.\n",
3156 iface, stream_idx, buffer, offset, stride);
3158 if (!buffer)
3159 return D3DERR_INVALIDCALL;
3161 wined3d_mutex_lock();
3162 hr = wined3d_device_get_stream_source(device->wined3d_device, stream_idx, &wined3d_buffer, offset, stride);
3163 if (SUCCEEDED(hr) && wined3d_buffer)
3165 buffer_impl = wined3d_buffer_get_parent(wined3d_buffer);
3166 *buffer = &buffer_impl->IDirect3DVertexBuffer9_iface;
3167 IDirect3DVertexBuffer9_AddRef(*buffer);
3169 else
3171 if (FAILED(hr))
3172 FIXME("Call to GetStreamSource failed %p %p\n", offset, stride);
3173 *buffer = NULL;
3175 wined3d_mutex_unlock();
3177 return hr;
3180 static HRESULT WINAPI d3d9_device_SetStreamSourceFreq(IDirect3DDevice9Ex *iface, UINT stream_idx, UINT freq)
3182 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3183 HRESULT hr;
3185 TRACE("iface %p, stream_idx %u, freq %u.\n", iface, stream_idx, freq);
3187 wined3d_mutex_lock();
3188 hr = wined3d_device_set_stream_source_freq(device->wined3d_device, stream_idx, freq);
3189 wined3d_mutex_unlock();
3191 return hr;
3194 static HRESULT WINAPI d3d9_device_GetStreamSourceFreq(IDirect3DDevice9Ex *iface, UINT stream_idx, UINT *freq)
3196 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3197 HRESULT hr;
3199 TRACE("iface %p, stream_idx %u, freq %p.\n", iface, stream_idx, freq);
3201 wined3d_mutex_lock();
3202 hr = wined3d_device_get_stream_source_freq(device->wined3d_device, stream_idx, freq);
3203 wined3d_mutex_unlock();
3205 return hr;
3208 static HRESULT WINAPI d3d9_device_SetIndices(IDirect3DDevice9Ex *iface, IDirect3DIndexBuffer9 *buffer)
3210 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3211 struct d3d9_indexbuffer *ib = unsafe_impl_from_IDirect3DIndexBuffer9(buffer);
3213 TRACE("iface %p, buffer %p.\n", iface, buffer);
3215 wined3d_mutex_lock();
3216 wined3d_device_set_index_buffer(device->wined3d_device,
3217 ib ? ib->wined3d_buffer : NULL, ib ? ib->format : WINED3DFMT_UNKNOWN, 0);
3218 wined3d_mutex_unlock();
3220 return D3D_OK;
3223 static HRESULT WINAPI d3d9_device_GetIndices(IDirect3DDevice9Ex *iface, IDirect3DIndexBuffer9 **buffer)
3225 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3226 enum wined3d_format_id wined3d_format;
3227 struct wined3d_buffer *wined3d_buffer;
3228 struct d3d9_indexbuffer *buffer_impl;
3230 TRACE("iface %p, buffer %p.\n", iface, buffer);
3232 if (!buffer)
3233 return D3DERR_INVALIDCALL;
3235 wined3d_mutex_lock();
3236 if ((wined3d_buffer = wined3d_device_get_index_buffer(device->wined3d_device, &wined3d_format, NULL)))
3238 buffer_impl = wined3d_buffer_get_parent(wined3d_buffer);
3239 *buffer = &buffer_impl->IDirect3DIndexBuffer9_iface;
3240 IDirect3DIndexBuffer9_AddRef(*buffer);
3242 else
3244 *buffer = NULL;
3246 wined3d_mutex_unlock();
3248 return D3D_OK;
3251 static HRESULT WINAPI d3d9_device_CreatePixelShader(IDirect3DDevice9Ex *iface,
3252 const DWORD *byte_code, IDirect3DPixelShader9 **shader)
3254 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3255 struct d3d9_pixelshader *object;
3256 HRESULT hr;
3258 TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
3260 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
3261 if (!object)
3263 FIXME("Failed to allocate pixel shader memory.\n");
3264 return E_OUTOFMEMORY;
3267 hr = pixelshader_init(object, device, byte_code);
3268 if (FAILED(hr))
3270 WARN("Failed to initialize pixel shader, hr %#x.\n", hr);
3271 HeapFree(GetProcessHeap(), 0, object);
3272 return hr;
3275 TRACE("Created pixel shader %p.\n", object);
3276 *shader = &object->IDirect3DPixelShader9_iface;
3278 return D3D_OK;
3281 static HRESULT WINAPI d3d9_device_SetPixelShader(IDirect3DDevice9Ex *iface, IDirect3DPixelShader9 *shader)
3283 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3284 struct d3d9_pixelshader *shader_obj = unsafe_impl_from_IDirect3DPixelShader9(shader);
3286 TRACE("iface %p, shader %p.\n", iface, shader);
3288 wined3d_mutex_lock();
3289 wined3d_device_set_pixel_shader(device->wined3d_device,
3290 shader_obj ? shader_obj->wined3d_shader : NULL);
3291 wined3d_mutex_unlock();
3293 return D3D_OK;
3296 static HRESULT WINAPI d3d9_device_GetPixelShader(IDirect3DDevice9Ex *iface, IDirect3DPixelShader9 **shader)
3298 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3299 struct d3d9_pixelshader *shader_impl;
3300 struct wined3d_shader *wined3d_shader;
3302 TRACE("iface %p, shader %p.\n", iface, shader);
3304 if (!shader) return D3DERR_INVALIDCALL;
3306 wined3d_mutex_lock();
3307 if ((wined3d_shader = wined3d_device_get_pixel_shader(device->wined3d_device)))
3309 shader_impl = wined3d_shader_get_parent(wined3d_shader);
3310 *shader = &shader_impl->IDirect3DPixelShader9_iface;
3311 IDirect3DPixelShader9_AddRef(*shader);
3313 else
3315 *shader = NULL;
3317 wined3d_mutex_unlock();
3319 TRACE("Returning %p.\n", *shader);
3321 return D3D_OK;
3324 static HRESULT WINAPI d3d9_device_SetPixelShaderConstantF(IDirect3DDevice9Ex *iface,
3325 UINT reg_idx, const float *data, UINT count)
3327 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3328 HRESULT hr;
3330 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3332 wined3d_mutex_lock();
3333 hr = wined3d_device_set_ps_consts_f(device->wined3d_device,
3334 reg_idx, count, (const struct wined3d_vec4 *)data);
3335 wined3d_mutex_unlock();
3337 return hr;
3340 static HRESULT WINAPI d3d9_device_GetPixelShaderConstantF(IDirect3DDevice9Ex *iface,
3341 UINT reg_idx, float *data, UINT count)
3343 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3344 HRESULT hr;
3346 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3348 wined3d_mutex_lock();
3349 hr = wined3d_device_get_ps_consts_f(device->wined3d_device,
3350 reg_idx, count, (struct wined3d_vec4 *)data);
3351 wined3d_mutex_unlock();
3353 return hr;
3356 static HRESULT WINAPI d3d9_device_SetPixelShaderConstantI(IDirect3DDevice9Ex *iface,
3357 UINT reg_idx, const int *data, UINT count)
3359 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3360 HRESULT hr;
3362 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3364 wined3d_mutex_lock();
3365 hr = wined3d_device_set_ps_consts_i(device->wined3d_device,
3366 reg_idx, count, (const struct wined3d_ivec4 *)data);
3367 wined3d_mutex_unlock();
3369 return hr;
3372 static HRESULT WINAPI d3d9_device_GetPixelShaderConstantI(IDirect3DDevice9Ex *iface,
3373 UINT reg_idx, int *data, UINT count)
3375 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3376 HRESULT hr;
3378 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3380 wined3d_mutex_lock();
3381 hr = wined3d_device_get_ps_consts_i(device->wined3d_device,
3382 reg_idx, count, (struct wined3d_ivec4 *)data);
3383 wined3d_mutex_unlock();
3385 return hr;
3388 static HRESULT WINAPI d3d9_device_SetPixelShaderConstantB(IDirect3DDevice9Ex *iface,
3389 UINT reg_idx, const BOOL *data, UINT count)
3391 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3392 HRESULT hr;
3394 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3396 wined3d_mutex_lock();
3397 hr = wined3d_device_set_ps_consts_b(device->wined3d_device, reg_idx, count, data);
3398 wined3d_mutex_unlock();
3400 return hr;
3403 static HRESULT WINAPI d3d9_device_GetPixelShaderConstantB(IDirect3DDevice9Ex *iface,
3404 UINT reg_idx, BOOL *data, UINT count)
3406 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3407 HRESULT hr;
3409 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3411 wined3d_mutex_lock();
3412 hr = wined3d_device_get_ps_consts_b(device->wined3d_device, reg_idx, count, data);
3413 wined3d_mutex_unlock();
3415 return hr;
3418 static HRESULT WINAPI d3d9_device_DrawRectPatch(IDirect3DDevice9Ex *iface, UINT handle,
3419 const float *segment_count, const D3DRECTPATCH_INFO *patch_info)
3421 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
3422 iface, handle, segment_count, patch_info);
3423 return D3D_OK;
3426 static HRESULT WINAPI d3d9_device_DrawTriPatch(IDirect3DDevice9Ex *iface, UINT handle,
3427 const float *segment_count, const D3DTRIPATCH_INFO *patch_info)
3429 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
3430 iface, handle, segment_count, patch_info);
3431 return D3D_OK;
3434 static HRESULT WINAPI d3d9_device_DeletePatch(IDirect3DDevice9Ex *iface, UINT handle)
3436 FIXME("iface %p, handle %#x unimplemented.\n", iface, handle);
3437 return D3DERR_INVALIDCALL;
3440 static HRESULT WINAPI d3d9_device_CreateQuery(IDirect3DDevice9Ex *iface, D3DQUERYTYPE type, IDirect3DQuery9 **query)
3442 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3443 struct d3d9_query *object;
3444 HRESULT hr;
3446 TRACE("iface %p, type %#x, query %p.\n", iface, type, query);
3448 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
3449 if (!object)
3450 return E_OUTOFMEMORY;
3452 hr = query_init(object, device, type);
3453 if (FAILED(hr))
3455 WARN("Failed to initialize query, hr %#x.\n", hr);
3456 HeapFree(GetProcessHeap(), 0, object);
3457 return hr;
3460 TRACE("Created query %p.\n", object);
3461 if (query) *query = &object->IDirect3DQuery9_iface;
3462 else IDirect3DQuery9_Release(&object->IDirect3DQuery9_iface);
3464 return D3D_OK;
3467 static HRESULT WINAPI d3d9_device_SetConvolutionMonoKernel(IDirect3DDevice9Ex *iface,
3468 UINT width, UINT height, float *rows, float *columns)
3470 FIXME("iface %p, width %u, height %u, rows %p, columns %p stub!\n",
3471 iface, width, height, rows, columns);
3473 return E_NOTIMPL;
3476 static HRESULT WINAPI d3d9_device_ComposeRects(IDirect3DDevice9Ex *iface,
3477 IDirect3DSurface9 *src_surface, IDirect3DSurface9 *dst_surface, IDirect3DVertexBuffer9 *src_descs,
3478 UINT rect_count, IDirect3DVertexBuffer9 *dst_descs, D3DCOMPOSERECTSOP operation, INT offset_x, INT offset_y)
3480 FIXME("iface %p, src_surface %p, dst_surface %p, src_descs %p, rect_count %u, "
3481 "dst_descs %p, operation %#x, offset_x %u, offset_y %u stub!\n",
3482 iface, src_surface, dst_surface, src_descs, rect_count,
3483 dst_descs, operation, offset_x, offset_y);
3485 return E_NOTIMPL;
3488 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_PresentEx(IDirect3DDevice9Ex *iface,
3489 const RECT *src_rect, const RECT *dst_rect, HWND dst_window_override,
3490 const RGNDATA *dirty_region, DWORD flags)
3492 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3493 UINT i;
3494 HRESULT hr;
3496 TRACE("iface %p, src_rect %s, dst_rect %s, dst_window_override %p, dirty_region %p, flags %#x.\n",
3497 iface, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect),
3498 dst_window_override, dirty_region, flags);
3500 if (device->device_state != D3D9_DEVICE_STATE_OK)
3501 return S_PRESENT_OCCLUDED;
3503 if (dirty_region)
3504 FIXME("Ignoring dirty_region %p.\n", dirty_region);
3506 wined3d_mutex_lock();
3507 for (i = 0; i < device->implicit_swapchain_count; ++i)
3509 if (FAILED(hr = wined3d_swapchain_present(device->implicit_swapchains[i]->wined3d_swapchain,
3510 src_rect, dst_rect, dst_window_override, flags)))
3512 wined3d_mutex_unlock();
3513 return hr;
3516 wined3d_mutex_unlock();
3518 return D3D_OK;
3521 static HRESULT WINAPI d3d9_device_GetGPUThreadPriority(IDirect3DDevice9Ex *iface, INT *priority)
3523 FIXME("iface %p, priority %p stub!\n", iface, priority);
3525 return E_NOTIMPL;
3528 static HRESULT WINAPI d3d9_device_SetGPUThreadPriority(IDirect3DDevice9Ex *iface, INT priority)
3530 FIXME("iface %p, priority %d stub!\n", iface, priority);
3532 return E_NOTIMPL;
3535 static HRESULT WINAPI d3d9_device_WaitForVBlank(IDirect3DDevice9Ex *iface, UINT swapchain_idx)
3537 FIXME("iface %p, swapchain_idx %u stub!\n", iface, swapchain_idx);
3539 return E_NOTIMPL;
3542 static HRESULT WINAPI d3d9_device_CheckResourceResidency(IDirect3DDevice9Ex *iface,
3543 IDirect3DResource9 **resources, UINT32 resource_count)
3545 FIXME("iface %p, resources %p, resource_count %u stub!\n",
3546 iface, resources, resource_count);
3548 return E_NOTIMPL;
3551 static HRESULT WINAPI d3d9_device_SetMaximumFrameLatency(IDirect3DDevice9Ex *iface, UINT max_latency)
3553 FIXME("iface %p, max_latency %u stub!\n", iface, max_latency);
3555 return E_NOTIMPL;
3558 static HRESULT WINAPI d3d9_device_GetMaximumFrameLatency(IDirect3DDevice9Ex *iface, UINT *max_latency)
3560 FIXME("iface %p, max_latency %p stub!\n", iface, max_latency);
3562 *max_latency = 2;
3564 return E_NOTIMPL;
3567 static HRESULT WINAPI d3d9_device_CheckDeviceState(IDirect3DDevice9Ex *iface, HWND dst_window)
3569 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3570 struct wined3d_swapchain_desc swapchain_desc;
3572 TRACE("iface %p, dst_window %p.\n", iface, dst_window);
3574 wined3d_mutex_lock();
3575 wined3d_swapchain_get_desc(device->implicit_swapchains[0]->wined3d_swapchain, &swapchain_desc);
3576 wined3d_mutex_unlock();
3578 if (swapchain_desc.windowed)
3579 return D3D_OK;
3581 /* FIXME: This is actually supposed to check if any other device is in
3582 * fullscreen mode. */
3583 if (dst_window != swapchain_desc.device_window)
3584 return device->device_state == D3D9_DEVICE_STATE_OK ? S_PRESENT_OCCLUDED : D3D_OK;
3586 return device->device_state == D3D9_DEVICE_STATE_OK ? D3D_OK : S_PRESENT_OCCLUDED;
3589 static HRESULT WINAPI d3d9_device_CreateRenderTargetEx(IDirect3DDevice9Ex *iface,
3590 UINT width, UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
3591 BOOL lockable, IDirect3DSurface9 **surface, HANDLE *shared_handle, DWORD usage)
3593 FIXME("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u, "
3594 "lockable %#x, surface %p, shared_handle %p, usage %#x stub!\n",
3595 iface, width, height, format, multisample_type, multisample_quality,
3596 lockable, surface, shared_handle, usage);
3598 *surface = NULL;
3599 if (shared_handle)
3600 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
3602 return E_NOTIMPL;
3605 static HRESULT WINAPI d3d9_device_CreateOffscreenPlainSurfaceEx(IDirect3DDevice9Ex *iface,
3606 UINT width, UINT height, D3DFORMAT format, D3DPOOL pool, IDirect3DSurface9 **surface,
3607 HANDLE *shared_handle, DWORD usage)
3609 FIXME("iface %p, width %u, height %u, format %#x, pool %#x, surface %p, shared_handle %p, usage %#x stub!\n",
3610 iface, width, height, format, pool, surface, shared_handle, usage);
3612 return E_NOTIMPL;
3615 static HRESULT WINAPI d3d9_device_CreateDepthStencilSurfaceEx(IDirect3DDevice9Ex *iface,
3616 UINT width, UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
3617 BOOL discard, IDirect3DSurface9 **surface, HANDLE *shared_handle, DWORD usage)
3619 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3620 DWORD flags = WINED3D_TEXTURE_CREATE_MAPPABLE;
3622 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u, "
3623 "discard %#x, surface %p, shared_handle %p, usage %#x.\n",
3624 iface, width, height, format, multisample_type, multisample_quality,
3625 discard, surface, shared_handle, usage);
3627 if (usage & D3DUSAGE_DEPTHSTENCIL)
3629 WARN("Invalid usage %#x.\n", usage);
3630 return D3DERR_INVALIDCALL;
3633 if (shared_handle)
3634 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
3636 if (discard)
3637 flags |= WINED3D_TEXTURE_CREATE_DISCARD;
3639 *surface = NULL;
3640 return d3d9_device_create_surface(device, width, height, format, flags, surface,
3641 D3DUSAGE_DEPTHSTENCIL | usage, D3DPOOL_DEFAULT, multisample_type, multisample_quality, NULL);
3644 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_ResetEx(IDirect3DDevice9Ex *iface,
3645 D3DPRESENT_PARAMETERS *present_parameters, D3DDISPLAYMODEEX *mode)
3647 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3649 TRACE("iface %p, present_parameters %p, mode %p.\n", iface, present_parameters, mode);
3651 if (!present_parameters->Windowed == !mode)
3653 WARN("Mode can be passed if and only if Windowed is FALSE.\n");
3654 return D3DERR_INVALIDCALL;
3657 if (mode && (mode->Width != present_parameters->BackBufferWidth
3658 || mode->Height != present_parameters->BackBufferHeight))
3660 WARN("Mode and back buffer mismatch (mode %ux%u, backbuffer %ux%u).\n",
3661 mode->Width, mode->Height,
3662 present_parameters->BackBufferWidth, present_parameters->BackBufferHeight);
3663 return D3DERR_INVALIDCALL;
3666 return d3d9_device_reset(device, present_parameters, mode);
3669 static HRESULT WINAPI d3d9_device_GetDisplayModeEx(IDirect3DDevice9Ex *iface,
3670 UINT swapchain_idx, D3DDISPLAYMODEEX *mode, D3DDISPLAYROTATION *rotation)
3672 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3673 struct wined3d_display_mode wined3d_mode;
3674 HRESULT hr;
3676 TRACE("iface %p, swapchain_idx %u, mode %p, rotation %p.\n",
3677 iface, swapchain_idx, mode, rotation);
3679 if (mode->Size != sizeof(*mode))
3680 return D3DERR_INVALIDCALL;
3682 wined3d_mutex_lock();
3683 hr = wined3d_device_get_display_mode(device->wined3d_device, swapchain_idx, &wined3d_mode,
3684 (enum wined3d_display_rotation *)rotation);
3685 wined3d_mutex_unlock();
3687 if (SUCCEEDED(hr))
3689 mode->Width = wined3d_mode.width;
3690 mode->Height = wined3d_mode.height;
3691 mode->RefreshRate = wined3d_mode.refresh_rate;
3692 mode->Format = d3dformat_from_wined3dformat(wined3d_mode.format_id);
3693 mode->ScanLineOrdering = wined3d_mode.scanline_ordering;
3696 return hr;
3699 static const struct IDirect3DDevice9ExVtbl d3d9_device_vtbl =
3701 /* IUnknown */
3702 d3d9_device_QueryInterface,
3703 d3d9_device_AddRef,
3704 d3d9_device_Release,
3705 /* IDirect3DDevice9 */
3706 d3d9_device_TestCooperativeLevel,
3707 d3d9_device_GetAvailableTextureMem,
3708 d3d9_device_EvictManagedResources,
3709 d3d9_device_GetDirect3D,
3710 d3d9_device_GetDeviceCaps,
3711 d3d9_device_GetDisplayMode,
3712 d3d9_device_GetCreationParameters,
3713 d3d9_device_SetCursorProperties,
3714 d3d9_device_SetCursorPosition,
3715 d3d9_device_ShowCursor,
3716 d3d9_device_CreateAdditionalSwapChain,
3717 d3d9_device_GetSwapChain,
3718 d3d9_device_GetNumberOfSwapChains,
3719 d3d9_device_Reset,
3720 d3d9_device_Present,
3721 d3d9_device_GetBackBuffer,
3722 d3d9_device_GetRasterStatus,
3723 d3d9_device_SetDialogBoxMode,
3724 d3d9_device_SetGammaRamp,
3725 d3d9_device_GetGammaRamp,
3726 d3d9_device_CreateTexture,
3727 d3d9_device_CreateVolumeTexture,
3728 d3d9_device_CreateCubeTexture,
3729 d3d9_device_CreateVertexBuffer,
3730 d3d9_device_CreateIndexBuffer,
3731 d3d9_device_CreateRenderTarget,
3732 d3d9_device_CreateDepthStencilSurface,
3733 d3d9_device_UpdateSurface,
3734 d3d9_device_UpdateTexture,
3735 d3d9_device_GetRenderTargetData,
3736 d3d9_device_GetFrontBufferData,
3737 d3d9_device_StretchRect,
3738 d3d9_device_ColorFill,
3739 d3d9_device_CreateOffscreenPlainSurface,
3740 d3d9_device_SetRenderTarget,
3741 d3d9_device_GetRenderTarget,
3742 d3d9_device_SetDepthStencilSurface,
3743 d3d9_device_GetDepthStencilSurface,
3744 d3d9_device_BeginScene,
3745 d3d9_device_EndScene,
3746 d3d9_device_Clear,
3747 d3d9_device_SetTransform,
3748 d3d9_device_GetTransform,
3749 d3d9_device_MultiplyTransform,
3750 d3d9_device_SetViewport,
3751 d3d9_device_GetViewport,
3752 d3d9_device_SetMaterial,
3753 d3d9_device_GetMaterial,
3754 d3d9_device_SetLight,
3755 d3d9_device_GetLight,
3756 d3d9_device_LightEnable,
3757 d3d9_device_GetLightEnable,
3758 d3d9_device_SetClipPlane,
3759 d3d9_device_GetClipPlane,
3760 d3d9_device_SetRenderState,
3761 d3d9_device_GetRenderState,
3762 d3d9_device_CreateStateBlock,
3763 d3d9_device_BeginStateBlock,
3764 d3d9_device_EndStateBlock,
3765 d3d9_device_SetClipStatus,
3766 d3d9_device_GetClipStatus,
3767 d3d9_device_GetTexture,
3768 d3d9_device_SetTexture,
3769 d3d9_device_GetTextureStageState,
3770 d3d9_device_SetTextureStageState,
3771 d3d9_device_GetSamplerState,
3772 d3d9_device_SetSamplerState,
3773 d3d9_device_ValidateDevice,
3774 d3d9_device_SetPaletteEntries,
3775 d3d9_device_GetPaletteEntries,
3776 d3d9_device_SetCurrentTexturePalette,
3777 d3d9_device_GetCurrentTexturePalette,
3778 d3d9_device_SetScissorRect,
3779 d3d9_device_GetScissorRect,
3780 d3d9_device_SetSoftwareVertexProcessing,
3781 d3d9_device_GetSoftwareVertexProcessing,
3782 d3d9_device_SetNPatchMode,
3783 d3d9_device_GetNPatchMode,
3784 d3d9_device_DrawPrimitive,
3785 d3d9_device_DrawIndexedPrimitive,
3786 d3d9_device_DrawPrimitiveUP,
3787 d3d9_device_DrawIndexedPrimitiveUP,
3788 d3d9_device_ProcessVertices,
3789 d3d9_device_CreateVertexDeclaration,
3790 d3d9_device_SetVertexDeclaration,
3791 d3d9_device_GetVertexDeclaration,
3792 d3d9_device_SetFVF,
3793 d3d9_device_GetFVF,
3794 d3d9_device_CreateVertexShader,
3795 d3d9_device_SetVertexShader,
3796 d3d9_device_GetVertexShader,
3797 d3d9_device_SetVertexShaderConstantF,
3798 d3d9_device_GetVertexShaderConstantF,
3799 d3d9_device_SetVertexShaderConstantI,
3800 d3d9_device_GetVertexShaderConstantI,
3801 d3d9_device_SetVertexShaderConstantB,
3802 d3d9_device_GetVertexShaderConstantB,
3803 d3d9_device_SetStreamSource,
3804 d3d9_device_GetStreamSource,
3805 d3d9_device_SetStreamSourceFreq,
3806 d3d9_device_GetStreamSourceFreq,
3807 d3d9_device_SetIndices,
3808 d3d9_device_GetIndices,
3809 d3d9_device_CreatePixelShader,
3810 d3d9_device_SetPixelShader,
3811 d3d9_device_GetPixelShader,
3812 d3d9_device_SetPixelShaderConstantF,
3813 d3d9_device_GetPixelShaderConstantF,
3814 d3d9_device_SetPixelShaderConstantI,
3815 d3d9_device_GetPixelShaderConstantI,
3816 d3d9_device_SetPixelShaderConstantB,
3817 d3d9_device_GetPixelShaderConstantB,
3818 d3d9_device_DrawRectPatch,
3819 d3d9_device_DrawTriPatch,
3820 d3d9_device_DeletePatch,
3821 d3d9_device_CreateQuery,
3822 /* IDirect3DDevice9Ex */
3823 d3d9_device_SetConvolutionMonoKernel,
3824 d3d9_device_ComposeRects,
3825 d3d9_device_PresentEx,
3826 d3d9_device_GetGPUThreadPriority,
3827 d3d9_device_SetGPUThreadPriority,
3828 d3d9_device_WaitForVBlank,
3829 d3d9_device_CheckResourceResidency,
3830 d3d9_device_SetMaximumFrameLatency,
3831 d3d9_device_GetMaximumFrameLatency,
3832 d3d9_device_CheckDeviceState,
3833 d3d9_device_CreateRenderTargetEx,
3834 d3d9_device_CreateOffscreenPlainSurfaceEx,
3835 d3d9_device_CreateDepthStencilSurfaceEx,
3836 d3d9_device_ResetEx,
3837 d3d9_device_GetDisplayModeEx,
3840 static inline struct d3d9_device *device_from_device_parent(struct wined3d_device_parent *device_parent)
3842 return CONTAINING_RECORD(device_parent, struct d3d9_device, device_parent);
3845 static void CDECL device_parent_wined3d_device_created(struct wined3d_device_parent *device_parent,
3846 struct wined3d_device *device)
3848 TRACE("device_parent %p, device %p.\n", device_parent, device);
3851 static void CDECL device_parent_mode_changed(struct wined3d_device_parent *device_parent)
3853 TRACE("device_parent %p.\n", device_parent);
3856 static void CDECL device_parent_activate(struct wined3d_device_parent *device_parent, BOOL activate)
3858 struct d3d9_device *device = device_from_device_parent(device_parent);
3860 TRACE("device_parent %p, activate %#x.\n", device_parent, activate);
3862 if (!device->d3d_parent)
3863 return;
3865 if (!activate)
3866 InterlockedCompareExchange(&device->device_state, D3D9_DEVICE_STATE_LOST, D3D9_DEVICE_STATE_OK);
3867 else if (device->d3d_parent->extended)
3868 InterlockedCompareExchange(&device->device_state, D3D9_DEVICE_STATE_OK, D3D9_DEVICE_STATE_LOST);
3869 else
3870 InterlockedCompareExchange(&device->device_state, D3D9_DEVICE_STATE_NOT_RESET, D3D9_DEVICE_STATE_LOST);
3873 static HRESULT CDECL device_parent_surface_created(struct wined3d_device_parent *device_parent,
3874 struct wined3d_texture *wined3d_texture, unsigned int sub_resource_idx,
3875 void **parent, const struct wined3d_parent_ops **parent_ops)
3877 struct d3d9_surface *d3d_surface;
3879 TRACE("device_parent %p, wined3d_texture %p, sub_resource_idx %u, parent %p, parent_ops %p.\n",
3880 device_parent, wined3d_texture, sub_resource_idx, parent, parent_ops);
3882 if (!(d3d_surface = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*d3d_surface))))
3883 return E_OUTOFMEMORY;
3885 surface_init(d3d_surface, wined3d_texture, sub_resource_idx, parent_ops);
3886 *parent = d3d_surface;
3887 TRACE("Created surface %p.\n", d3d_surface);
3889 return D3D_OK;
3892 static HRESULT CDECL device_parent_volume_created(struct wined3d_device_parent *device_parent,
3893 struct wined3d_texture *wined3d_texture, unsigned int sub_resource_idx,
3894 void **parent, const struct wined3d_parent_ops **parent_ops)
3896 struct d3d9_volume *d3d_volume;
3898 TRACE("device_parent %p, texture %p, sub_resource_idx %u, parent %p, parent_ops %p.\n",
3899 device_parent, wined3d_texture, sub_resource_idx, parent, parent_ops);
3901 if (!(d3d_volume = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*d3d_volume))))
3902 return E_OUTOFMEMORY;
3904 volume_init(d3d_volume, wined3d_texture, sub_resource_idx, parent_ops);
3905 *parent = d3d_volume;
3906 TRACE("Created volume %p.\n", d3d_volume);
3908 return D3D_OK;
3911 static HRESULT CDECL device_parent_create_swapchain_texture(struct wined3d_device_parent *device_parent,
3912 void *container_parent, const struct wined3d_resource_desc *desc, DWORD texture_flags,
3913 struct wined3d_texture **texture)
3915 struct d3d9_device *device = device_from_device_parent(device_parent);
3916 struct d3d9_surface *d3d_surface;
3917 HRESULT hr;
3919 TRACE("device_parent %p, container_parent %p, desc %p, texture flags %#x, texture %p.\n",
3920 device_parent, container_parent, desc, texture_flags, texture);
3922 if (container_parent == device_parent)
3923 container_parent = &device->IDirect3DDevice9Ex_iface;
3925 if (FAILED(hr = wined3d_texture_create(device->wined3d_device, desc, 1, 1,
3926 texture_flags | WINED3D_TEXTURE_CREATE_MAPPABLE, NULL, container_parent,
3927 &d3d9_null_wined3d_parent_ops, texture)))
3929 WARN("Failed to create texture, hr %#x.\n", hr);
3930 return hr;
3933 d3d_surface = wined3d_texture_get_sub_resource_parent(*texture, 0);
3934 d3d_surface->parent_device = &device->IDirect3DDevice9Ex_iface;
3936 return hr;
3939 static HRESULT CDECL device_parent_create_swapchain(struct wined3d_device_parent *device_parent,
3940 struct wined3d_swapchain_desc *desc, struct wined3d_swapchain **swapchain)
3942 struct d3d9_device *device = device_from_device_parent(device_parent);
3943 struct d3d9_swapchain *d3d_swapchain;
3944 HRESULT hr;
3946 TRACE("device_parent %p, desc %p, swapchain %p\n", device_parent, desc, swapchain);
3948 hr = d3d9_swapchain_create(device, desc, &d3d_swapchain);
3949 if (FAILED(hr))
3951 WARN("Failed to create swapchain, hr %#x.\n", hr);
3952 *swapchain = NULL;
3953 return hr;
3956 *swapchain = d3d_swapchain->wined3d_swapchain;
3957 wined3d_swapchain_incref(*swapchain);
3958 IDirect3DSwapChain9Ex_Release(&d3d_swapchain->IDirect3DSwapChain9Ex_iface);
3960 return hr;
3963 static const struct wined3d_device_parent_ops d3d9_wined3d_device_parent_ops =
3965 device_parent_wined3d_device_created,
3966 device_parent_mode_changed,
3967 device_parent_activate,
3968 device_parent_surface_created,
3969 device_parent_volume_created,
3970 device_parent_create_swapchain_texture,
3971 device_parent_create_swapchain,
3974 static void setup_fpu(void)
3976 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
3977 WORD cw;
3978 __asm__ volatile ("fnstcw %0" : "=m" (cw));
3979 cw = (cw & ~0xf3f) | 0x3f;
3980 __asm__ volatile ("fldcw %0" : : "m" (cw));
3981 #elif defined(__i386__) && defined(_MSC_VER)
3982 WORD cw;
3983 __asm fnstcw cw;
3984 cw = (cw & ~0xf3f) | 0x3f;
3985 __asm fldcw cw;
3986 #else
3987 FIXME("FPU setup not implemented for this platform.\n");
3988 #endif
3991 HRESULT device_init(struct d3d9_device *device, struct d3d9 *parent, struct wined3d *wined3d,
3992 UINT adapter, D3DDEVTYPE device_type, HWND focus_window, DWORD flags,
3993 D3DPRESENT_PARAMETERS *parameters, D3DDISPLAYMODEEX *mode)
3995 struct wined3d_swapchain_desc *swapchain_desc;
3996 UINT i, count = 1;
3997 HRESULT hr;
3999 if (mode)
4000 FIXME("Ignoring display mode.\n");
4002 device->IDirect3DDevice9Ex_iface.lpVtbl = &d3d9_device_vtbl;
4003 device->device_parent.ops = &d3d9_wined3d_device_parent_ops;
4004 device->refcount = 1;
4006 if (!(flags & D3DCREATE_FPU_PRESERVE)) setup_fpu();
4008 wined3d_mutex_lock();
4009 hr = wined3d_device_create(wined3d, adapter, device_type, focus_window, flags, 4,
4010 &device->device_parent, &device->wined3d_device);
4011 if (FAILED(hr))
4013 WARN("Failed to create wined3d device, hr %#x.\n", hr);
4014 wined3d_mutex_unlock();
4015 return hr;
4018 if (flags & D3DCREATE_ADAPTERGROUP_DEVICE)
4020 WINED3DCAPS caps;
4022 wined3d_get_device_caps(wined3d, adapter, device_type, &caps);
4023 count = caps.NumberOfAdaptersInGroup;
4026 if (flags & D3DCREATE_MULTITHREADED)
4027 wined3d_device_set_multithreaded(device->wined3d_device);
4029 if (!parameters->Windowed)
4031 if (!focus_window)
4032 focus_window = parameters->hDeviceWindow;
4033 if (FAILED(hr = wined3d_device_acquire_focus_window(device->wined3d_device, focus_window)))
4035 ERR("Failed to acquire focus window, hr %#x.\n", hr);
4036 wined3d_device_decref(device->wined3d_device);
4037 wined3d_mutex_unlock();
4038 return hr;
4041 for (i = 0; i < count; ++i)
4043 HWND device_window = parameters[i].hDeviceWindow;
4045 if (!device_window) device_window = focus_window;
4046 wined3d_device_setup_fullscreen_window(device->wined3d_device, device_window,
4047 parameters[i].BackBufferWidth,
4048 parameters[i].BackBufferHeight);
4052 swapchain_desc = HeapAlloc(GetProcessHeap(), 0, sizeof(*swapchain_desc) * count);
4053 if (!swapchain_desc)
4055 ERR("Failed to allocate wined3d parameters.\n");
4056 wined3d_device_release_focus_window(device->wined3d_device);
4057 wined3d_device_decref(device->wined3d_device);
4058 wined3d_mutex_unlock();
4059 return E_OUTOFMEMORY;
4062 for (i = 0; i < count; ++i)
4064 if (!wined3d_swapchain_desc_from_present_parameters(&swapchain_desc[i], &parameters[i],
4065 parent->extended))
4067 wined3d_device_release_focus_window(device->wined3d_device);
4068 wined3d_device_decref(device->wined3d_device);
4069 HeapFree(GetProcessHeap(), 0, swapchain_desc);
4070 wined3d_mutex_unlock();
4071 return D3DERR_INVALIDCALL;
4075 hr = wined3d_device_init_3d(device->wined3d_device, swapchain_desc);
4076 if (FAILED(hr))
4078 WARN("Failed to initialize 3D, hr %#x.\n", hr);
4079 wined3d_device_release_focus_window(device->wined3d_device);
4080 HeapFree(GetProcessHeap(), 0, swapchain_desc);
4081 wined3d_device_decref(device->wined3d_device);
4082 wined3d_mutex_unlock();
4083 return hr;
4086 if (FAILED(hr = d3d9_device_get_swapchains(device)))
4088 wined3d_device_uninit_3d(device->wined3d_device);
4089 wined3d_device_release_focus_window(device->wined3d_device);
4090 wined3d_device_decref(device->wined3d_device);
4091 wined3d_mutex_unlock();
4092 return E_OUTOFMEMORY;
4095 for (i = 0; i < count; ++i)
4097 present_parameters_from_wined3d_swapchain_desc(&parameters[i], &swapchain_desc[i]);
4100 wined3d_mutex_unlock();
4102 HeapFree(GetProcessHeap(), 0, swapchain_desc);
4104 /* Initialize the converted declaration array. This creates a valid pointer
4105 * and when adding decls HeapReAlloc() can be used without further checking. */
4106 device->fvf_decls = HeapAlloc(GetProcessHeap(), 0, 0);
4107 if (!device->fvf_decls)
4109 ERR("Failed to allocate FVF vertex declaration map memory.\n");
4110 wined3d_mutex_lock();
4111 HeapFree(GetProcessHeap(), 0, device->implicit_swapchains);
4112 wined3d_device_uninit_3d(device->wined3d_device);
4113 wined3d_device_release_focus_window(device->wined3d_device);
4114 wined3d_device_decref(device->wined3d_device);
4115 wined3d_mutex_unlock();
4116 return E_OUTOFMEMORY;
4119 IDirect3D9Ex_AddRef(&parent->IDirect3D9Ex_iface);
4120 device->d3d_parent = parent;
4122 return D3D_OK;