d3d9: Toggle depth test state based on auto depth stencil.
[wine.git] / dlls / d3d9 / device.c
blob20f73c0e212bbb7b004f0f2f90acf208f3268ae6
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 swapchain_desc->refresh_rate = present_parameters->FullScreen_RefreshRateInHz;
243 swapchain_desc->swap_interval = present_parameters->PresentationInterval;
244 swapchain_desc->auto_restore_display_mode = TRUE;
246 if (present_parameters->Flags & ~D3DPRESENTFLAGS_MASK)
247 FIXME("Unhandled flags %#x.\n", present_parameters->Flags & ~D3DPRESENTFLAGS_MASK);
249 return TRUE;
252 void d3dcaps_from_wined3dcaps(D3DCAPS9 *caps, const WINED3DCAPS *wined3d_caps)
254 static const DWORD ps_minor_version[] = {0, 4, 0, 0};
255 static const DWORD vs_minor_version[] = {0, 1, 0, 0};
256 static const DWORD texture_filter_caps =
257 D3DPTFILTERCAPS_MINFPOINT | D3DPTFILTERCAPS_MINFLINEAR | D3DPTFILTERCAPS_MINFANISOTROPIC |
258 D3DPTFILTERCAPS_MINFPYRAMIDALQUAD | D3DPTFILTERCAPS_MINFGAUSSIANQUAD|
259 D3DPTFILTERCAPS_MIPFPOINT | D3DPTFILTERCAPS_MIPFLINEAR | D3DPTFILTERCAPS_MAGFPOINT |
260 D3DPTFILTERCAPS_MAGFLINEAR |D3DPTFILTERCAPS_MAGFANISOTROPIC|D3DPTFILTERCAPS_MAGFPYRAMIDALQUAD|
261 D3DPTFILTERCAPS_MAGFGAUSSIANQUAD;
263 caps->DeviceType = (D3DDEVTYPE)wined3d_caps->DeviceType;
264 caps->AdapterOrdinal = wined3d_caps->AdapterOrdinal;
265 caps->Caps = wined3d_caps->Caps;
266 caps->Caps2 = wined3d_caps->Caps2;
267 caps->Caps3 = wined3d_caps->Caps3;
268 caps->PresentationIntervals = wined3d_caps->PresentationIntervals;
269 caps->CursorCaps = wined3d_caps->CursorCaps;
270 caps->DevCaps = wined3d_caps->DevCaps;
271 caps->PrimitiveMiscCaps = wined3d_caps->PrimitiveMiscCaps;
272 caps->RasterCaps = wined3d_caps->RasterCaps;
273 caps->ZCmpCaps = wined3d_caps->ZCmpCaps;
274 caps->SrcBlendCaps = wined3d_caps->SrcBlendCaps;
275 caps->DestBlendCaps = wined3d_caps->DestBlendCaps;
276 caps->AlphaCmpCaps = wined3d_caps->AlphaCmpCaps;
277 caps->ShadeCaps = wined3d_caps->ShadeCaps;
278 caps->TextureCaps = wined3d_caps->TextureCaps;
279 caps->TextureFilterCaps = wined3d_caps->TextureFilterCaps;
280 caps->CubeTextureFilterCaps = wined3d_caps->CubeTextureFilterCaps;
281 caps->VolumeTextureFilterCaps = wined3d_caps->VolumeTextureFilterCaps;
282 caps->TextureAddressCaps = wined3d_caps->TextureAddressCaps;
283 caps->VolumeTextureAddressCaps = wined3d_caps->VolumeTextureAddressCaps;
284 caps->LineCaps = wined3d_caps->LineCaps;
285 caps->MaxTextureWidth = wined3d_caps->MaxTextureWidth;
286 caps->MaxTextureHeight = wined3d_caps->MaxTextureHeight;
287 caps->MaxVolumeExtent = wined3d_caps->MaxVolumeExtent;
288 caps->MaxTextureRepeat = wined3d_caps->MaxTextureRepeat;
289 caps->MaxTextureAspectRatio = wined3d_caps->MaxTextureAspectRatio;
290 caps->MaxAnisotropy = wined3d_caps->MaxAnisotropy;
291 caps->MaxVertexW = wined3d_caps->MaxVertexW;
292 caps->GuardBandLeft = wined3d_caps->GuardBandLeft;
293 caps->GuardBandTop = wined3d_caps->GuardBandTop;
294 caps->GuardBandRight = wined3d_caps->GuardBandRight;
295 caps->GuardBandBottom = wined3d_caps->GuardBandBottom;
296 caps->ExtentsAdjust = wined3d_caps->ExtentsAdjust;
297 caps->StencilCaps = wined3d_caps->StencilCaps;
298 caps->FVFCaps = wined3d_caps->FVFCaps;
299 caps->TextureOpCaps = wined3d_caps->TextureOpCaps;
300 caps->MaxTextureBlendStages = wined3d_caps->MaxTextureBlendStages;
301 caps->MaxSimultaneousTextures = wined3d_caps->MaxSimultaneousTextures;
302 caps->VertexProcessingCaps = wined3d_caps->VertexProcessingCaps;
303 caps->MaxActiveLights = wined3d_caps->MaxActiveLights;
304 caps->MaxUserClipPlanes = wined3d_caps->MaxUserClipPlanes;
305 caps->MaxVertexBlendMatrices = wined3d_caps->MaxVertexBlendMatrices;
306 caps->MaxVertexBlendMatrixIndex = wined3d_caps->MaxVertexBlendMatrixIndex;
307 caps->MaxPointSize = wined3d_caps->MaxPointSize;
308 caps->MaxPrimitiveCount = wined3d_caps->MaxPrimitiveCount;
309 caps->MaxVertexIndex = wined3d_caps->MaxVertexIndex;
310 caps->MaxStreams = wined3d_caps->MaxStreams;
311 caps->MaxStreamStride = wined3d_caps->MaxStreamStride;
312 caps->VertexShaderVersion = wined3d_caps->VertexShaderVersion;
313 caps->MaxVertexShaderConst = wined3d_caps->MaxVertexShaderConst;
314 caps->PixelShaderVersion = wined3d_caps->PixelShaderVersion;
315 caps->PixelShader1xMaxValue = wined3d_caps->PixelShader1xMaxValue;
316 caps->DevCaps2 = wined3d_caps->DevCaps2;
317 caps->MaxNpatchTessellationLevel = wined3d_caps->MaxNpatchTessellationLevel;
318 caps->MasterAdapterOrdinal = wined3d_caps->MasterAdapterOrdinal;
319 caps->AdapterOrdinalInGroup = wined3d_caps->AdapterOrdinalInGroup;
320 caps->NumberOfAdaptersInGroup = wined3d_caps->NumberOfAdaptersInGroup;
321 caps->DeclTypes = wined3d_caps->DeclTypes;
322 caps->NumSimultaneousRTs = wined3d_caps->NumSimultaneousRTs;
323 caps->StretchRectFilterCaps = wined3d_caps->StretchRectFilterCaps;
324 caps->VS20Caps.Caps = wined3d_caps->VS20Caps.caps;
325 caps->VS20Caps.DynamicFlowControlDepth = wined3d_caps->VS20Caps.dynamic_flow_control_depth;
326 caps->VS20Caps.NumTemps = wined3d_caps->VS20Caps.temp_count;
327 caps->VS20Caps.StaticFlowControlDepth = wined3d_caps->VS20Caps.static_flow_control_depth;
328 caps->PS20Caps.Caps = wined3d_caps->PS20Caps.caps;
329 caps->PS20Caps.DynamicFlowControlDepth = wined3d_caps->PS20Caps.dynamic_flow_control_depth;
330 caps->PS20Caps.NumTemps = wined3d_caps->PS20Caps.temp_count;
331 caps->PS20Caps.StaticFlowControlDepth = wined3d_caps->PS20Caps.static_flow_control_depth;
332 caps->PS20Caps.NumInstructionSlots = wined3d_caps->PS20Caps.instruction_slot_count;
333 caps->VertexTextureFilterCaps = wined3d_caps->VertexTextureFilterCaps;
334 caps->MaxVShaderInstructionsExecuted = wined3d_caps->MaxVShaderInstructionsExecuted;
335 caps->MaxPShaderInstructionsExecuted = wined3d_caps->MaxPShaderInstructionsExecuted;
336 caps->MaxVertexShader30InstructionSlots = wined3d_caps->MaxVertexShader30InstructionSlots;
337 caps->MaxPixelShader30InstructionSlots = wined3d_caps->MaxPixelShader30InstructionSlots;
339 /* Some functionality is implemented in d3d9.dll, not wined3d.dll. Add the needed caps. */
340 caps->DevCaps2 |= D3DDEVCAPS2_CAN_STRETCHRECT_FROM_TEXTURES;
342 /* Filter wined3d caps. */
343 caps->TextureFilterCaps &= texture_filter_caps;
344 caps->CubeTextureFilterCaps &= texture_filter_caps;
345 caps->VolumeTextureFilterCaps &= texture_filter_caps;
347 caps->DevCaps &=
348 D3DDEVCAPS_EXECUTESYSTEMMEMORY | D3DDEVCAPS_EXECUTEVIDEOMEMORY | D3DDEVCAPS_TLVERTEXSYSTEMMEMORY |
349 D3DDEVCAPS_TLVERTEXVIDEOMEMORY | D3DDEVCAPS_TEXTURESYSTEMMEMORY| D3DDEVCAPS_TEXTUREVIDEOMEMORY |
350 D3DDEVCAPS_DRAWPRIMTLVERTEX | D3DDEVCAPS_CANRENDERAFTERFLIP | D3DDEVCAPS_TEXTURENONLOCALVIDMEM|
351 D3DDEVCAPS_DRAWPRIMITIVES2 | D3DDEVCAPS_SEPARATETEXTUREMEMORIES |
352 D3DDEVCAPS_DRAWPRIMITIVES2EX | D3DDEVCAPS_HWTRANSFORMANDLIGHT| D3DDEVCAPS_CANBLTSYSTONONLOCAL |
353 D3DDEVCAPS_HWRASTERIZATION | D3DDEVCAPS_PUREDEVICE | D3DDEVCAPS_QUINTICRTPATCHES |
354 D3DDEVCAPS_RTPATCHES | D3DDEVCAPS_RTPATCHHANDLEZERO | D3DDEVCAPS_NPATCHES;
356 caps->ShadeCaps &=
357 D3DPSHADECAPS_COLORGOURAUDRGB | D3DPSHADECAPS_SPECULARGOURAUDRGB |
358 D3DPSHADECAPS_ALPHAGOURAUDBLEND | D3DPSHADECAPS_FOGGOURAUD;
360 caps->RasterCaps &=
361 D3DPRASTERCAPS_DITHER | D3DPRASTERCAPS_ZTEST | D3DPRASTERCAPS_FOGVERTEX |
362 D3DPRASTERCAPS_FOGTABLE | D3DPRASTERCAPS_MIPMAPLODBIAS | D3DPRASTERCAPS_ZBUFFERLESSHSR |
363 D3DPRASTERCAPS_FOGRANGE | D3DPRASTERCAPS_ANISOTROPY | D3DPRASTERCAPS_WBUFFER |
364 D3DPRASTERCAPS_WFOG | D3DPRASTERCAPS_ZFOG | D3DPRASTERCAPS_COLORPERSPECTIVE |
365 D3DPRASTERCAPS_SCISSORTEST | D3DPRASTERCAPS_SLOPESCALEDEPTHBIAS |
366 D3DPRASTERCAPS_DEPTHBIAS | D3DPRASTERCAPS_MULTISAMPLE_TOGGLE;
368 caps->DevCaps2 &=
369 D3DDEVCAPS2_STREAMOFFSET | D3DDEVCAPS2_DMAPNPATCH | D3DDEVCAPS2_ADAPTIVETESSRTPATCH |
370 D3DDEVCAPS2_ADAPTIVETESSNPATCH | D3DDEVCAPS2_CAN_STRETCHRECT_FROM_TEXTURES |
371 D3DDEVCAPS2_PRESAMPLEDDMAPNPATCH| D3DDEVCAPS2_VERTEXELEMENTSCANSHARESTREAMOFFSET;
373 caps->Caps2 &=
374 D3DCAPS2_FULLSCREENGAMMA | D3DCAPS2_CANCALIBRATEGAMMA | D3DCAPS2_RESERVED |
375 D3DCAPS2_CANMANAGERESOURCE | D3DCAPS2_DYNAMICTEXTURES | D3DCAPS2_CANAUTOGENMIPMAP;
377 caps->VertexProcessingCaps &=
378 D3DVTXPCAPS_TEXGEN | D3DVTXPCAPS_MATERIALSOURCE7 | D3DVTXPCAPS_DIRECTIONALLIGHTS |
379 D3DVTXPCAPS_POSITIONALLIGHTS | D3DVTXPCAPS_LOCALVIEWER | D3DVTXPCAPS_TWEENING |
380 D3DVTXPCAPS_TEXGEN_SPHEREMAP | D3DVTXPCAPS_NO_TEXGEN_NONLOCALVIEWER;
382 caps->TextureCaps &=
383 D3DPTEXTURECAPS_PERSPECTIVE | D3DPTEXTURECAPS_POW2 | D3DPTEXTURECAPS_ALPHA |
384 D3DPTEXTURECAPS_SQUAREONLY | D3DPTEXTURECAPS_TEXREPEATNOTSCALEDBYSIZE |
385 D3DPTEXTURECAPS_ALPHAPALETTE | D3DPTEXTURECAPS_NONPOW2CONDITIONAL |
386 D3DPTEXTURECAPS_PROJECTED | D3DPTEXTURECAPS_CUBEMAP | D3DPTEXTURECAPS_VOLUMEMAP |
387 D3DPTEXTURECAPS_MIPMAP | D3DPTEXTURECAPS_MIPVOLUMEMAP | D3DPTEXTURECAPS_MIPCUBEMAP |
388 D3DPTEXTURECAPS_CUBEMAP_POW2 | D3DPTEXTURECAPS_VOLUMEMAP_POW2| D3DPTEXTURECAPS_NOPROJECTEDBUMPENV;
390 caps->MaxVertexShaderConst = min(D3D9_MAX_VERTEX_SHADER_CONSTANTF, caps->MaxVertexShaderConst);
391 caps->NumSimultaneousRTs = min(D3D9_MAX_SIMULTANEOUS_RENDERTARGETS, caps->NumSimultaneousRTs);
393 if (caps->PixelShaderVersion > 3)
395 caps->PixelShaderVersion = D3DPS_VERSION(3, 0);
397 else
399 DWORD major = caps->PixelShaderVersion;
400 caps->PixelShaderVersion = D3DPS_VERSION(major, ps_minor_version[major]);
403 if (caps->VertexShaderVersion > 3)
405 caps->VertexShaderVersion = D3DVS_VERSION(3, 0);
407 else
409 DWORD major = caps->VertexShaderVersion;
410 caps->VertexShaderVersion = D3DVS_VERSION(major, vs_minor_version[major]);
414 static HRESULT WINAPI d3d9_device_QueryInterface(IDirect3DDevice9Ex *iface, REFIID riid, void **out)
416 TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
418 if (IsEqualGUID(riid, &IID_IDirect3DDevice9)
419 || IsEqualGUID(riid, &IID_IUnknown))
421 IDirect3DDevice9Ex_AddRef(iface);
422 *out = iface;
423 return S_OK;
426 if (IsEqualGUID(riid, &IID_IDirect3DDevice9Ex))
428 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
430 /* Find out if the creating d3d9 interface was created with Direct3DCreate9Ex.
431 * It doesn't matter with which function the device was created. */
432 if (!device->d3d_parent->extended)
434 WARN("IDirect3D9 instance wasn't created with CreateDirect3D9Ex, returning E_NOINTERFACE.\n");
435 *out = NULL;
436 return E_NOINTERFACE;
439 IDirect3DDevice9Ex_AddRef(iface);
440 *out = iface;
441 return S_OK;
444 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
446 *out = NULL;
447 return E_NOINTERFACE;
450 static ULONG WINAPI d3d9_device_AddRef(IDirect3DDevice9Ex *iface)
452 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
453 ULONG refcount = InterlockedIncrement(&device->refcount);
455 TRACE("%p increasing refcount to %u.\n", iface, refcount);
457 return refcount;
460 static ULONG WINAPI DECLSPEC_HOTPATCH d3d9_device_Release(IDirect3DDevice9Ex *iface)
462 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
463 ULONG refcount;
465 if (device->in_destruction)
466 return 0;
468 refcount = InterlockedDecrement(&device->refcount);
470 TRACE("%p decreasing refcount to %u.\n", iface, refcount);
472 if (!refcount)
474 unsigned i;
475 device->in_destruction = TRUE;
477 wined3d_mutex_lock();
478 for (i = 0; i < device->fvf_decl_count; ++i)
480 wined3d_vertex_declaration_decref(device->fvf_decls[i].decl);
482 HeapFree(GetProcessHeap(), 0, device->fvf_decls);
484 if (device->vertex_buffer)
485 wined3d_buffer_decref(device->vertex_buffer);
486 if (device->index_buffer)
487 wined3d_buffer_decref(device->index_buffer);
489 HeapFree(GetProcessHeap(), 0, device->implicit_swapchains);
491 wined3d_device_uninit_3d(device->wined3d_device);
492 wined3d_device_release_focus_window(device->wined3d_device);
493 wined3d_device_decref(device->wined3d_device);
494 wined3d_mutex_unlock();
496 IDirect3D9Ex_Release(&device->d3d_parent->IDirect3D9Ex_iface);
498 HeapFree(GetProcessHeap(), 0, device);
501 return refcount;
504 static HRESULT WINAPI d3d9_device_TestCooperativeLevel(IDirect3DDevice9Ex *iface)
506 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
508 TRACE("iface %p.\n", iface);
510 TRACE("device state: %#x.\n", device->device_state);
512 if (device->d3d_parent->extended)
513 return D3D_OK;
515 switch (device->device_state)
517 default:
518 case D3D9_DEVICE_STATE_OK:
519 return D3D_OK;
520 case D3D9_DEVICE_STATE_LOST:
521 return D3DERR_DEVICELOST;
522 case D3D9_DEVICE_STATE_NOT_RESET:
523 return D3DERR_DEVICENOTRESET;
527 static UINT WINAPI d3d9_device_GetAvailableTextureMem(IDirect3DDevice9Ex *iface)
529 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
530 UINT ret;
532 TRACE("iface %p.\n", iface);
534 wined3d_mutex_lock();
535 ret = wined3d_device_get_available_texture_mem(device->wined3d_device);
536 wined3d_mutex_unlock();
538 return ret;
541 static HRESULT WINAPI d3d9_device_EvictManagedResources(IDirect3DDevice9Ex *iface)
543 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
545 TRACE("iface %p.\n", iface);
547 wined3d_mutex_lock();
548 wined3d_device_evict_managed_resources(device->wined3d_device);
549 wined3d_mutex_unlock();
551 return D3D_OK;
554 static HRESULT WINAPI d3d9_device_GetDirect3D(IDirect3DDevice9Ex *iface, IDirect3D9 **d3d9)
556 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
558 TRACE("iface %p, d3d9 %p.\n", iface, d3d9);
560 if (!d3d9)
561 return D3DERR_INVALIDCALL;
563 return IDirect3D9Ex_QueryInterface(&device->d3d_parent->IDirect3D9Ex_iface, &IID_IDirect3D9, (void **)d3d9);
566 static HRESULT WINAPI d3d9_device_GetDeviceCaps(IDirect3DDevice9Ex *iface, D3DCAPS9 *caps)
568 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
569 WINED3DCAPS wined3d_caps;
570 HRESULT hr;
572 TRACE("iface %p, caps %p.\n", iface, caps);
574 if (!caps)
575 return D3DERR_INVALIDCALL;
577 memset(caps, 0, sizeof(*caps));
579 wined3d_mutex_lock();
580 hr = wined3d_device_get_device_caps(device->wined3d_device, &wined3d_caps);
581 wined3d_mutex_unlock();
583 d3dcaps_from_wined3dcaps(caps, &wined3d_caps);
585 return hr;
588 static HRESULT WINAPI d3d9_device_GetDisplayMode(IDirect3DDevice9Ex *iface, UINT swapchain, D3DDISPLAYMODE *mode)
590 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
591 struct wined3d_display_mode wined3d_mode;
592 HRESULT hr;
594 TRACE("iface %p, swapchain %u, mode %p.\n", iface, swapchain, mode);
596 wined3d_mutex_lock();
597 hr = wined3d_device_get_display_mode(device->wined3d_device, swapchain, &wined3d_mode, NULL);
598 wined3d_mutex_unlock();
600 if (SUCCEEDED(hr))
602 mode->Width = wined3d_mode.width;
603 mode->Height = wined3d_mode.height;
604 mode->RefreshRate = wined3d_mode.refresh_rate;
605 mode->Format = d3dformat_from_wined3dformat(wined3d_mode.format_id);
608 return hr;
611 static HRESULT WINAPI d3d9_device_GetCreationParameters(IDirect3DDevice9Ex *iface,
612 D3DDEVICE_CREATION_PARAMETERS *parameters)
614 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
616 TRACE("iface %p, parameters %p.\n", iface, parameters);
618 wined3d_mutex_lock();
619 wined3d_device_get_creation_parameters(device->wined3d_device,
620 (struct wined3d_device_creation_parameters *)parameters);
621 wined3d_mutex_unlock();
623 return D3D_OK;
626 static HRESULT WINAPI d3d9_device_SetCursorProperties(IDirect3DDevice9Ex *iface,
627 UINT hotspot_x, UINT hotspot_y, IDirect3DSurface9 *bitmap)
629 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
630 struct d3d9_surface *bitmap_impl = unsafe_impl_from_IDirect3DSurface9(bitmap);
631 HRESULT hr;
633 TRACE("iface %p, hotspot_x %u, hotspot_y %u, bitmap %p.\n",
634 iface, hotspot_x, hotspot_y, bitmap);
636 if (!bitmap)
638 WARN("No cursor bitmap, returning D3DERR_INVALIDCALL.\n");
639 return D3DERR_INVALIDCALL;
642 wined3d_mutex_lock();
643 hr = wined3d_device_set_cursor_properties(device->wined3d_device,
644 hotspot_x, hotspot_y, bitmap_impl->wined3d_texture, bitmap_impl->sub_resource_idx);
645 wined3d_mutex_unlock();
647 return hr;
650 static void WINAPI d3d9_device_SetCursorPosition(IDirect3DDevice9Ex *iface, int x, int y, DWORD flags)
652 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
654 TRACE("iface %p, x %u, y %u, flags %#x.\n", iface, x, y, flags);
656 wined3d_mutex_lock();
657 wined3d_device_set_cursor_position(device->wined3d_device, x, y, flags);
658 wined3d_mutex_unlock();
661 static BOOL WINAPI d3d9_device_ShowCursor(IDirect3DDevice9Ex *iface, BOOL show)
663 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
664 BOOL ret;
666 TRACE("iface %p, show %#x.\n", iface, show);
668 wined3d_mutex_lock();
669 ret = wined3d_device_show_cursor(device->wined3d_device, show);
670 wined3d_mutex_unlock();
672 return ret;
675 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_CreateAdditionalSwapChain(IDirect3DDevice9Ex *iface,
676 D3DPRESENT_PARAMETERS *present_parameters, IDirect3DSwapChain9 **swapchain)
678 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
679 struct wined3d_swapchain_desc desc;
680 struct d3d9_swapchain *object;
681 UINT i, count;
682 HRESULT hr;
684 TRACE("iface %p, present_parameters %p, swapchain %p.\n",
685 iface, present_parameters, swapchain);
687 if (!present_parameters->Windowed)
689 WARN("Trying to create an additional fullscreen swapchain, returning D3DERR_INVALIDCALL.\n");
690 return D3DERR_INVALIDCALL;
693 wined3d_mutex_lock();
694 count = wined3d_device_get_swapchain_count(device->wined3d_device);
695 for (i = 0; i < count; ++i)
697 struct wined3d_swapchain *wined3d_swapchain;
699 wined3d_swapchain = wined3d_device_get_swapchain(device->wined3d_device, i);
700 wined3d_swapchain_get_desc(wined3d_swapchain, &desc);
702 if (!desc.windowed)
704 wined3d_mutex_unlock();
705 WARN("Trying to create an additional swapchain in fullscreen mode, returning D3DERR_INVALIDCALL.\n");
706 return D3DERR_INVALIDCALL;
709 wined3d_mutex_unlock();
711 if (!wined3d_swapchain_desc_from_present_parameters(&desc, present_parameters,
712 device->d3d_parent->extended))
713 return D3DERR_INVALIDCALL;
714 if (SUCCEEDED(hr = d3d9_swapchain_create(device, &desc, &object)))
715 *swapchain = (IDirect3DSwapChain9 *)&object->IDirect3DSwapChain9Ex_iface;
716 present_parameters_from_wined3d_swapchain_desc(present_parameters, &desc);
718 return hr;
721 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_GetSwapChain(IDirect3DDevice9Ex *iface,
722 UINT swapchain_idx, IDirect3DSwapChain9 **swapchain)
724 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
725 HRESULT hr;
727 TRACE("iface %p, swapchain_idx %u, swapchain %p.\n", iface, swapchain_idx, swapchain);
729 wined3d_mutex_lock();
730 if (swapchain_idx < device->implicit_swapchain_count)
732 *swapchain = (IDirect3DSwapChain9 *)&device->implicit_swapchains[swapchain_idx]->IDirect3DSwapChain9Ex_iface;
733 IDirect3DSwapChain9Ex_AddRef(*swapchain);
734 hr = D3D_OK;
736 else
738 *swapchain = NULL;
739 hr = D3DERR_INVALIDCALL;
741 wined3d_mutex_unlock();
743 return hr;
746 static UINT WINAPI d3d9_device_GetNumberOfSwapChains(IDirect3DDevice9Ex *iface)
748 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
749 UINT count;
751 TRACE("iface %p.\n", iface);
753 wined3d_mutex_lock();
754 count = wined3d_device_get_swapchain_count(device->wined3d_device);
755 wined3d_mutex_unlock();
757 return count;
760 static HRESULT CDECL reset_enum_callback(struct wined3d_resource *resource)
762 struct wined3d_resource_desc desc;
763 IDirect3DBaseTexture9 *texture;
764 struct d3d9_surface *surface;
765 IUnknown *parent;
767 wined3d_resource_get_desc(resource, &desc);
768 if (desc.pool != WINED3D_POOL_DEFAULT)
769 return D3D_OK;
771 if (desc.resource_type != WINED3D_RTYPE_TEXTURE_2D)
773 WARN("Resource %p in pool D3DPOOL_DEFAULT blocks the Reset call.\n", resource);
774 return D3DERR_INVALIDCALL;
777 parent = wined3d_resource_get_parent(resource);
778 if (parent && SUCCEEDED(IUnknown_QueryInterface(parent, &IID_IDirect3DBaseTexture9, (void **)&texture)))
780 IDirect3DBaseTexture9_Release(texture);
781 WARN("Texture %p (resource %p) in pool D3DPOOL_DEFAULT blocks the Reset call.\n", texture, resource);
782 return D3DERR_INVALIDCALL;
785 surface = wined3d_texture_get_sub_resource_parent(wined3d_texture_from_resource(resource), 0);
786 if (!surface->resource.refcount)
787 return D3D_OK;
789 WARN("Surface %p in pool D3DPOOL_DEFAULT blocks the Reset call.\n", surface);
790 return D3DERR_INVALIDCALL;
793 static HRESULT d3d9_device_get_swapchains(struct d3d9_device *device)
795 UINT i, new_swapchain_count = wined3d_device_get_swapchain_count(device->wined3d_device);
796 struct wined3d_swapchain *wined3d_swapchain;
798 if (!(device->implicit_swapchains = HeapAlloc(GetProcessHeap(), 0,
799 new_swapchain_count * sizeof(*device->implicit_swapchains))))
800 return E_OUTOFMEMORY;
802 for (i = 0; i < new_swapchain_count; ++i)
804 wined3d_swapchain = wined3d_device_get_swapchain(device->wined3d_device, i);
805 device->implicit_swapchains[i] = wined3d_swapchain_get_parent(wined3d_swapchain);
807 device->implicit_swapchain_count = new_swapchain_count;
809 return D3D_OK;
812 static HRESULT d3d9_device_reset(struct d3d9_device *device,
813 D3DPRESENT_PARAMETERS *present_parameters, D3DDISPLAYMODEEX *mode)
815 struct wined3d_swapchain_desc swapchain_desc;
816 struct wined3d_display_mode wined3d_mode;
817 HRESULT hr;
819 if (!device->d3d_parent->extended && device->device_state == D3D9_DEVICE_STATE_LOST)
821 WARN("App not active, returning D3DERR_DEVICELOST.\n");
822 return D3DERR_DEVICELOST;
825 if (mode)
827 wined3d_mode.width = mode->Width;
828 wined3d_mode.height = mode->Height;
829 wined3d_mode.refresh_rate = mode->RefreshRate;
830 wined3d_mode.format_id = wined3dformat_from_d3dformat(mode->Format);
831 wined3d_mode.scanline_ordering = mode->ScanLineOrdering;
834 if (!wined3d_swapchain_desc_from_present_parameters(&swapchain_desc, present_parameters,
835 device->d3d_parent->extended))
836 return D3DERR_INVALIDCALL;
838 wined3d_mutex_lock();
840 if (device->vertex_buffer)
842 wined3d_buffer_decref(device->vertex_buffer);
843 device->vertex_buffer = NULL;
844 device->vertex_buffer_size = 0;
847 if (device->index_buffer)
849 wined3d_buffer_decref(device->index_buffer);
850 device->index_buffer = NULL;
851 device->index_buffer_size = 0;
854 if (SUCCEEDED(hr = wined3d_device_reset(device->wined3d_device, &swapchain_desc,
855 mode ? &wined3d_mode : NULL, reset_enum_callback, !device->d3d_parent->extended)))
857 HeapFree(GetProcessHeap(), 0, device->implicit_swapchains);
859 if (FAILED(hr = d3d9_device_get_swapchains(device)))
861 device->device_state = D3D9_DEVICE_STATE_NOT_RESET;
863 else
865 wined3d_swapchain_get_desc(device->implicit_swapchains[0]->wined3d_swapchain, &swapchain_desc);
866 present_parameters->BackBufferWidth = swapchain_desc.backbuffer_width;
867 present_parameters->BackBufferHeight = swapchain_desc.backbuffer_height;
868 present_parameters->BackBufferFormat = d3dformat_from_wined3dformat(swapchain_desc.backbuffer_format);
869 present_parameters->BackBufferCount = swapchain_desc.backbuffer_count;
871 device->device_state = D3D9_DEVICE_STATE_OK;
874 else if (!device->d3d_parent->extended)
876 device->device_state = D3D9_DEVICE_STATE_NOT_RESET;
879 wined3d_mutex_unlock();
881 return hr;
884 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_Reset(IDirect3DDevice9Ex *iface,
885 D3DPRESENT_PARAMETERS *present_parameters)
887 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
889 TRACE("iface %p, present_parameters %p.\n", iface, present_parameters);
891 return d3d9_device_reset(device, present_parameters, NULL);
894 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_Present(IDirect3DDevice9Ex *iface,
895 const RECT *src_rect, const RECT *dst_rect, HWND dst_window_override, const RGNDATA *dirty_region)
897 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
898 UINT i;
899 HRESULT hr;
901 TRACE("iface %p, src_rect %p, dst_rect %p, dst_window_override %p, dirty_region %p.\n",
902 iface, src_rect, dst_rect, dst_window_override, dirty_region);
904 if (device->device_state != D3D9_DEVICE_STATE_OK)
905 return device->d3d_parent->extended ? S_PRESENT_OCCLUDED : D3DERR_DEVICELOST;
907 if (dirty_region)
908 FIXME("Ignoring dirty_region %p.\n", dirty_region);
910 wined3d_mutex_lock();
911 for (i = 0; i < device->implicit_swapchain_count; ++i)
913 if (FAILED(hr = wined3d_swapchain_present(device->implicit_swapchains[i]->wined3d_swapchain,
914 src_rect, dst_rect, dst_window_override, 0)))
916 wined3d_mutex_unlock();
917 return hr;
920 wined3d_mutex_unlock();
922 return D3D_OK;
925 static HRESULT WINAPI d3d9_device_GetBackBuffer(IDirect3DDevice9Ex *iface, UINT swapchain,
926 UINT backbuffer_idx, D3DBACKBUFFER_TYPE backbuffer_type, IDirect3DSurface9 **backbuffer)
928 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
929 HRESULT hr;
931 TRACE("iface %p, swapchain %u, backbuffer_idx %u, backbuffer_type %#x, backbuffer %p.\n",
932 iface, swapchain, backbuffer_idx, backbuffer_type, backbuffer);
934 /* backbuffer_type is ignored by native. */
936 /* No need to check for backbuffer == NULL, Windows crashes in that case. */
937 *backbuffer = NULL;
939 wined3d_mutex_lock();
940 if (swapchain >= device->implicit_swapchain_count)
942 wined3d_mutex_unlock();
943 WARN("Swapchain index %u is out of range, returning D3DERR_INVALIDCALL.\n", swapchain);
944 return D3DERR_INVALIDCALL;
947 hr = IDirect3DSwapChain9Ex_GetBackBuffer(&device->implicit_swapchains[swapchain]->IDirect3DSwapChain9Ex_iface,
948 backbuffer_idx, backbuffer_type, backbuffer);
949 wined3d_mutex_unlock();
951 return hr;
954 static HRESULT WINAPI d3d9_device_GetRasterStatus(IDirect3DDevice9Ex *iface,
955 UINT swapchain, D3DRASTER_STATUS *raster_status)
957 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
958 HRESULT hr;
960 TRACE("iface %p, swapchain %u, raster_status %p.\n", iface, swapchain, raster_status);
962 wined3d_mutex_lock();
963 hr = wined3d_device_get_raster_status(device->wined3d_device,
964 swapchain, (struct wined3d_raster_status *)raster_status);
965 wined3d_mutex_unlock();
967 return hr;
970 static HRESULT WINAPI d3d9_device_SetDialogBoxMode(IDirect3DDevice9Ex *iface, BOOL enable)
972 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
973 HRESULT hr;
975 TRACE("iface %p, enable %#x.\n", iface, enable);
977 wined3d_mutex_lock();
978 hr = wined3d_device_set_dialog_box_mode(device->wined3d_device, enable);
979 wined3d_mutex_unlock();
981 return hr;
984 static void WINAPI d3d9_device_SetGammaRamp(IDirect3DDevice9Ex *iface,
985 UINT swapchain, DWORD flags, const D3DGAMMARAMP *ramp)
987 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
989 TRACE("iface %p, swapchain %u, flags %#x, ramp %p.\n", iface, swapchain, flags, ramp);
991 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
992 wined3d_mutex_lock();
993 wined3d_device_set_gamma_ramp(device->wined3d_device, swapchain, flags, (const struct wined3d_gamma_ramp *)ramp);
994 wined3d_mutex_unlock();
997 static void WINAPI d3d9_device_GetGammaRamp(IDirect3DDevice9Ex *iface, UINT swapchain, D3DGAMMARAMP *ramp)
999 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1001 TRACE("iface %p, swapchain %u, ramp %p.\n", iface, swapchain, ramp);
1003 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
1004 wined3d_mutex_lock();
1005 wined3d_device_get_gamma_ramp(device->wined3d_device, swapchain, (struct wined3d_gamma_ramp *)ramp);
1006 wined3d_mutex_unlock();
1009 static HRESULT WINAPI d3d9_device_CreateTexture(IDirect3DDevice9Ex *iface,
1010 UINT width, UINT height, UINT levels, DWORD usage, D3DFORMAT format,
1011 D3DPOOL pool, IDirect3DTexture9 **texture, HANDLE *shared_handle)
1013 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1014 struct d3d9_texture *object;
1015 BOOL set_mem = FALSE;
1016 HRESULT hr;
1018 TRACE("iface %p, width %u, height %u, levels %u, usage %#x, format %#x, pool %#x, texture %p, shared_handle %p.\n",
1019 iface, width, height, levels, usage, format, pool, texture, shared_handle);
1021 *texture = NULL;
1022 if (shared_handle)
1024 if (!device->d3d_parent->extended)
1026 WARN("Trying to create a shared or user memory texture on a non-ex device.\n");
1027 return E_NOTIMPL;
1030 if (pool == D3DPOOL_SYSTEMMEM)
1032 if (levels != 1)
1033 return D3DERR_INVALIDCALL;
1034 set_mem = TRUE;
1036 else
1038 if (pool != D3DPOOL_DEFAULT)
1040 WARN("Trying to create a shared texture in pool %#x.\n", pool);
1041 return D3DERR_INVALIDCALL;
1043 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1047 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1048 if (!object)
1049 return D3DERR_OUTOFVIDEOMEMORY;
1051 hr = texture_init(object, device, width, height, levels, usage, format, pool);
1052 if (FAILED(hr))
1054 WARN("Failed to initialize texture, hr %#x.\n", hr);
1055 HeapFree(GetProcessHeap(), 0, object);
1056 return hr;
1059 if (set_mem)
1060 wined3d_texture_update_desc(object->wined3d_texture, width, height,
1061 wined3dformat_from_d3dformat(format), WINED3D_MULTISAMPLE_NONE, 0,
1062 *shared_handle, 0);
1064 TRACE("Created texture %p.\n", object);
1065 *texture = (IDirect3DTexture9 *)&object->IDirect3DBaseTexture9_iface;
1067 return D3D_OK;
1070 static HRESULT WINAPI d3d9_device_CreateVolumeTexture(IDirect3DDevice9Ex *iface,
1071 UINT width, UINT height, UINT depth, UINT levels, DWORD usage, D3DFORMAT format,
1072 D3DPOOL pool, IDirect3DVolumeTexture9 **texture, HANDLE *shared_handle)
1074 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1075 struct d3d9_texture *object;
1076 HRESULT hr;
1078 TRACE("iface %p, width %u, height %u, depth %u, levels %u, "
1079 "usage %#x, format %#x, pool %#x, texture %p, shared_handle %p.\n",
1080 iface, width, height, depth, levels,
1081 usage, format, pool, texture, shared_handle);
1083 *texture = NULL;
1084 if (shared_handle)
1086 if (!device->d3d_parent->extended)
1088 WARN("Trying to create a shared volume texture on a non-ex device.\n");
1089 return E_NOTIMPL;
1092 if (pool != D3DPOOL_DEFAULT)
1094 WARN("Trying to create a shared volume texture in pool %#x.\n", pool);
1095 return D3DERR_INVALIDCALL;
1097 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1100 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1101 if (!object)
1102 return D3DERR_OUTOFVIDEOMEMORY;
1104 hr = volumetexture_init(object, device, width, height, depth, levels, usage, format, pool);
1105 if (FAILED(hr))
1107 WARN("Failed to initialize volume texture, hr %#x.\n", hr);
1108 HeapFree(GetProcessHeap(), 0, object);
1109 return hr;
1112 TRACE("Created volume texture %p.\n", object);
1113 *texture = (IDirect3DVolumeTexture9 *)&object->IDirect3DBaseTexture9_iface;
1115 return D3D_OK;
1118 static HRESULT WINAPI d3d9_device_CreateCubeTexture(IDirect3DDevice9Ex *iface,
1119 UINT edge_length, UINT levels, DWORD usage, D3DFORMAT format, D3DPOOL pool,
1120 IDirect3DCubeTexture9 **texture, HANDLE *shared_handle)
1122 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1123 struct d3d9_texture *object;
1124 HRESULT hr;
1126 TRACE("iface %p, edge_length %u, levels %u, usage %#x, format %#x, pool %#x, texture %p, shared_handle %p.\n",
1127 iface, edge_length, levels, usage, format, pool, texture, shared_handle);
1129 *texture = NULL;
1130 if (shared_handle)
1132 if (!device->d3d_parent->extended)
1134 WARN("Trying to create a shared cube texture on a non-ex device.\n");
1135 return E_NOTIMPL;
1138 if (pool != D3DPOOL_DEFAULT)
1140 WARN("Trying to create a shared cube texture in pool %#x.\n", pool);
1141 return D3DERR_INVALIDCALL;
1143 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1146 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1147 if (!object)
1148 return D3DERR_OUTOFVIDEOMEMORY;
1150 hr = cubetexture_init(object, device, edge_length, levels, usage, format, pool);
1151 if (FAILED(hr))
1153 WARN("Failed to initialize cube texture, hr %#x.\n", hr);
1154 HeapFree(GetProcessHeap(), 0, object);
1155 return hr;
1158 TRACE("Created cube texture %p.\n", object);
1159 *texture = (IDirect3DCubeTexture9 *)&object->IDirect3DBaseTexture9_iface;
1161 return D3D_OK;
1164 static HRESULT WINAPI d3d9_device_CreateVertexBuffer(IDirect3DDevice9Ex *iface, UINT size,
1165 DWORD usage, DWORD fvf, D3DPOOL pool, IDirect3DVertexBuffer9 **buffer,
1166 HANDLE *shared_handle)
1168 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1169 struct d3d9_vertexbuffer *object;
1170 HRESULT hr;
1172 TRACE("iface %p, size %u, usage %#x, fvf %#x, pool %#x, buffer %p, shared_handle %p.\n",
1173 iface, size, usage, fvf, pool, buffer, shared_handle);
1175 if (shared_handle)
1177 if (!device->d3d_parent->extended)
1179 WARN("Trying to create a shared vertex buffer on a non-ex device.\n");
1180 return E_NOTIMPL;
1183 if (pool != D3DPOOL_DEFAULT)
1185 WARN("Trying to create a shared vertex buffer in pool %#x.\n", pool);
1186 return D3DERR_NOTAVAILABLE;
1188 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1191 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1192 if (!object)
1193 return D3DERR_OUTOFVIDEOMEMORY;
1195 hr = vertexbuffer_init(object, device, size, usage, fvf, pool);
1196 if (FAILED(hr))
1198 WARN("Failed to initialize vertex buffer, hr %#x.\n", hr);
1199 HeapFree(GetProcessHeap(), 0, object);
1200 return hr;
1203 TRACE("Created vertex buffer %p.\n", object);
1204 *buffer = &object->IDirect3DVertexBuffer9_iface;
1206 return D3D_OK;
1209 static HRESULT WINAPI d3d9_device_CreateIndexBuffer(IDirect3DDevice9Ex *iface, UINT size,
1210 DWORD usage, D3DFORMAT format, D3DPOOL pool, IDirect3DIndexBuffer9 **buffer,
1211 HANDLE *shared_handle)
1213 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1214 struct d3d9_indexbuffer *object;
1215 HRESULT hr;
1217 TRACE("iface %p, size %u, usage %#x, format %#x, pool %#x, buffer %p, shared_handle %p.\n",
1218 iface, size, usage, format, pool, buffer, shared_handle);
1220 if (shared_handle)
1222 if (!device->d3d_parent->extended)
1224 WARN("Trying to create a shared index buffer on a non-ex device.\n");
1225 return E_NOTIMPL;
1228 if (pool != D3DPOOL_DEFAULT)
1230 WARN("Trying to create a shared index buffer in pool %#x.\n", pool);
1231 return D3DERR_NOTAVAILABLE;
1233 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1236 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1237 if (!object)
1238 return D3DERR_OUTOFVIDEOMEMORY;
1240 hr = indexbuffer_init(object, device, size, usage, format, pool);
1241 if (FAILED(hr))
1243 WARN("Failed to initialize index buffer, hr %#x.\n", hr);
1244 HeapFree(GetProcessHeap(), 0, object);
1245 return hr;
1248 TRACE("Created index buffer %p.\n", object);
1249 *buffer = &object->IDirect3DIndexBuffer9_iface;
1251 return D3D_OK;
1254 static HRESULT d3d9_device_create_surface(struct d3d9_device *device, UINT width, UINT height,
1255 D3DFORMAT format, DWORD flags, IDirect3DSurface9 **surface, UINT usage, D3DPOOL pool,
1256 D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality, void *user_mem)
1258 struct wined3d_resource_desc desc;
1259 struct d3d9_surface *surface_impl;
1260 struct wined3d_texture *texture;
1261 HRESULT hr;
1263 TRACE("device %p, width %u, height %u, format %#x, flags %#x, surface %p.\n"
1264 "usage %#x, pool %#x, multisample_type %#x, multisample_quality %u.\n",
1265 device, width, height, format, flags, surface, usage, pool,
1266 multisample_type, multisample_quality);
1268 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
1269 desc.format = wined3dformat_from_d3dformat(format);
1270 desc.multisample_type = multisample_type;
1271 desc.multisample_quality = multisample_quality;
1272 desc.usage = usage & WINED3DUSAGE_MASK;
1273 desc.pool = pool;
1274 desc.width = width;
1275 desc.height = height;
1276 desc.depth = 1;
1277 desc.size = 0;
1279 if (is_gdi_compat_wined3dformat(desc.format))
1280 flags |= WINED3D_TEXTURE_CREATE_GET_DC;
1282 wined3d_mutex_lock();
1284 if (FAILED(hr = wined3d_texture_create(device->wined3d_device, &desc,
1285 1, 1, flags, NULL, NULL, &d3d9_null_wined3d_parent_ops, &texture)))
1287 wined3d_mutex_unlock();
1288 WARN("Failed to create texture, hr %#x.\n", hr);
1289 if (hr == WINED3DERR_NOTAVAILABLE)
1290 hr = D3DERR_INVALIDCALL;
1291 return hr;
1294 surface_impl = wined3d_texture_get_sub_resource_parent(texture, 0);
1295 surface_impl->parent_device = &device->IDirect3DDevice9Ex_iface;
1296 *surface = &surface_impl->IDirect3DSurface9_iface;
1297 IDirect3DSurface9_AddRef(*surface);
1299 if (user_mem)
1300 wined3d_texture_update_desc(texture, width, height,
1301 desc.format, multisample_type, multisample_quality, user_mem, 0);
1303 wined3d_texture_decref(texture);
1305 wined3d_mutex_unlock();
1307 return D3D_OK;
1310 BOOL is_gdi_compat_wined3dformat(enum wined3d_format_id format)
1312 switch (format)
1314 case WINED3DFMT_B8G8R8A8_UNORM:
1315 case WINED3DFMT_B8G8R8X8_UNORM:
1316 case WINED3DFMT_B5G6R5_UNORM:
1317 case WINED3DFMT_B5G5R5X1_UNORM:
1318 case WINED3DFMT_B5G5R5A1_UNORM:
1319 case WINED3DFMT_B8G8R8_UNORM:
1320 return TRUE;
1321 default:
1322 return FALSE;
1326 static HRESULT WINAPI d3d9_device_CreateRenderTarget(IDirect3DDevice9Ex *iface, UINT width, UINT height,
1327 D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
1328 BOOL lockable, IDirect3DSurface9 **surface, HANDLE *shared_handle)
1330 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1331 DWORD flags = 0;
1333 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u.\n"
1334 "lockable %#x, surface %p, shared_handle %p.\n",
1335 iface, width, height, format, multisample_type, multisample_quality,
1336 lockable, surface, shared_handle);
1338 *surface = NULL;
1339 if (shared_handle)
1341 if (!device->d3d_parent->extended)
1343 WARN("Trying to create a shared render target on a non-ex device.\n");
1344 return E_NOTIMPL;
1347 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1350 if (lockable)
1351 flags |= WINED3D_TEXTURE_CREATE_MAPPABLE;
1353 return d3d9_device_create_surface(device, width, height, format, flags, surface,
1354 D3DUSAGE_RENDERTARGET, D3DPOOL_DEFAULT, multisample_type, multisample_quality, NULL);
1357 static HRESULT WINAPI d3d9_device_CreateDepthStencilSurface(IDirect3DDevice9Ex *iface, UINT width, UINT height,
1358 D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
1359 BOOL discard, IDirect3DSurface9 **surface, HANDLE *shared_handle)
1361 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1362 DWORD flags = WINED3D_TEXTURE_CREATE_MAPPABLE;
1364 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u.\n"
1365 "discard %#x, surface %p, shared_handle %p.\n",
1366 iface, width, height, format, multisample_type, multisample_quality,
1367 discard, surface, shared_handle);
1369 *surface = NULL;
1370 if (shared_handle)
1372 if (!device->d3d_parent->extended)
1374 WARN("Trying to create a shared depth stencil on a non-ex device.\n");
1375 return E_NOTIMPL;
1378 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1381 if (discard)
1382 flags |= WINED3D_TEXTURE_CREATE_DISCARD;
1384 return d3d9_device_create_surface(device, width, height, format, flags, surface,
1385 D3DUSAGE_DEPTHSTENCIL, D3DPOOL_DEFAULT, multisample_type, multisample_quality, NULL);
1389 static HRESULT WINAPI d3d9_device_UpdateSurface(IDirect3DDevice9Ex *iface,
1390 IDirect3DSurface9 *src_surface, const RECT *src_rect,
1391 IDirect3DSurface9 *dst_surface, const POINT *dst_point)
1393 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1394 struct d3d9_surface *src = unsafe_impl_from_IDirect3DSurface9(src_surface);
1395 struct d3d9_surface *dst = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1396 struct wined3d_sub_resource_desc src_desc, dst_desc;
1397 struct wined3d_box src_box;
1398 HRESULT hr;
1400 TRACE("iface %p, src_surface %p, src_rect %p, dst_surface %p, dst_point %p.\n",
1401 iface, src_surface, src_rect, dst_surface, dst_point);
1403 wined3d_mutex_lock();
1405 wined3d_texture_get_sub_resource_desc(src->wined3d_texture, src->sub_resource_idx, &src_desc);
1406 wined3d_texture_get_sub_resource_desc(dst->wined3d_texture, dst->sub_resource_idx, &dst_desc);
1407 if (src_desc.format != dst_desc.format)
1409 wined3d_mutex_unlock();
1410 WARN("Surface formats (%#x/%#x) don't match.\n",
1411 d3dformat_from_wined3dformat(src_desc.format),
1412 d3dformat_from_wined3dformat(dst_desc.format));
1413 return D3DERR_INVALIDCALL;
1416 if (src_rect)
1417 wined3d_box_set(&src_box, src_rect->left, src_rect->top, src_rect->right, src_rect->bottom, 0, 1);
1418 else
1419 wined3d_box_set(&src_box, 0, 0, src_desc.width, src_desc.height, 0, 1);
1421 hr = wined3d_device_copy_sub_resource_region(device->wined3d_device,
1422 wined3d_texture_get_resource(dst->wined3d_texture), dst->sub_resource_idx, dst_point ? dst_point->x : 0,
1423 dst_point ? dst_point->y : 0, 0, wined3d_texture_get_resource(src->wined3d_texture),
1424 src->sub_resource_idx, &src_box);
1425 wined3d_mutex_unlock();
1427 if (FAILED(hr))
1428 return D3DERR_INVALIDCALL;
1430 return hr;
1433 static HRESULT WINAPI d3d9_device_UpdateTexture(IDirect3DDevice9Ex *iface,
1434 IDirect3DBaseTexture9 *src_texture, IDirect3DBaseTexture9 *dst_texture)
1436 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1437 struct d3d9_texture *src_impl, *dst_impl;
1438 HRESULT hr;
1440 TRACE("iface %p, src_texture %p, dst_texture %p.\n", iface, src_texture, dst_texture);
1442 src_impl = unsafe_impl_from_IDirect3DBaseTexture9(src_texture);
1443 dst_impl = unsafe_impl_from_IDirect3DBaseTexture9(dst_texture);
1445 wined3d_mutex_lock();
1446 hr = wined3d_device_update_texture(device->wined3d_device,
1447 src_impl->wined3d_texture, dst_impl->wined3d_texture);
1448 wined3d_mutex_unlock();
1450 return hr;
1453 static HRESULT WINAPI d3d9_device_GetRenderTargetData(IDirect3DDevice9Ex *iface,
1454 IDirect3DSurface9 *render_target, IDirect3DSurface9 *dst_surface)
1456 struct d3d9_surface *rt_impl = unsafe_impl_from_IDirect3DSurface9(render_target);
1457 struct d3d9_surface *dst_impl = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1458 struct wined3d_sub_resource_desc wined3d_desc;
1459 RECT dst_rect, src_rect;
1460 HRESULT hr;
1462 TRACE("iface %p, render_target %p, dst_surface %p.\n", iface, render_target, dst_surface);
1464 if (!render_target || !dst_surface)
1465 return D3DERR_INVALIDCALL;
1467 wined3d_mutex_lock();
1468 wined3d_texture_get_sub_resource_desc(dst_impl->wined3d_texture, dst_impl->sub_resource_idx, &wined3d_desc);
1469 SetRect(&dst_rect, 0, 0, wined3d_desc.width, wined3d_desc.height);
1471 wined3d_texture_get_sub_resource_desc(rt_impl->wined3d_texture, rt_impl->sub_resource_idx, &wined3d_desc);
1472 SetRect(&src_rect, 0, 0, wined3d_desc.width, wined3d_desc.height);
1474 /* TODO: Check surface sizes, pools, etc. */
1475 if (wined3d_desc.multisample_type)
1476 hr = D3DERR_INVALIDCALL;
1477 else
1478 hr = wined3d_texture_blt(dst_impl->wined3d_texture, dst_impl->sub_resource_idx, &dst_rect,
1479 rt_impl->wined3d_texture, rt_impl->sub_resource_idx, &src_rect, 0, NULL, WINED3D_TEXF_POINT);
1480 wined3d_mutex_unlock();
1482 return hr;
1485 static HRESULT WINAPI d3d9_device_GetFrontBufferData(IDirect3DDevice9Ex *iface,
1486 UINT swapchain, IDirect3DSurface9 *dst_surface)
1488 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1489 struct d3d9_surface *dst_impl = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1490 HRESULT hr = D3DERR_INVALIDCALL;
1492 TRACE("iface %p, swapchain %u, dst_surface %p.\n", iface, swapchain, dst_surface);
1494 wined3d_mutex_lock();
1495 if (swapchain < device->implicit_swapchain_count)
1496 hr = wined3d_swapchain_get_front_buffer_data(device->implicit_swapchains[swapchain]->wined3d_swapchain,
1497 dst_impl->wined3d_texture, dst_impl->sub_resource_idx);
1498 wined3d_mutex_unlock();
1500 return hr;
1503 static HRESULT WINAPI d3d9_device_StretchRect(IDirect3DDevice9Ex *iface, IDirect3DSurface9 *src_surface,
1504 const RECT *src_rect, IDirect3DSurface9 *dst_surface, const RECT *dst_rect, D3DTEXTUREFILTERTYPE filter)
1506 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1507 struct d3d9_surface *src = unsafe_impl_from_IDirect3DSurface9(src_surface);
1508 struct d3d9_surface *dst = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1509 struct wined3d_sub_resource_desc src_desc, dst_desc;
1510 HRESULT hr = D3DERR_INVALIDCALL;
1511 RECT d, s;
1513 TRACE("iface %p, src_surface %p, src_rect %p, dst_surface %p, dst_rect %p, filter %#x.\n",
1514 iface, src_surface, src_rect, dst_surface, dst_rect, filter);
1516 wined3d_mutex_lock();
1517 wined3d_texture_get_sub_resource_desc(dst->wined3d_texture, dst->sub_resource_idx, &dst_desc);
1518 if (!dst_rect)
1520 SetRect(&d, 0, 0, dst_desc.width, dst_desc.height);
1521 dst_rect = &d;
1524 wined3d_texture_get_sub_resource_desc(src->wined3d_texture, src->sub_resource_idx, &src_desc);
1525 if (!src_rect)
1527 SetRect(&s, 0, 0, src_desc.width, src_desc.height);
1528 src_rect = &s;
1531 if (src_desc.usage & WINED3DUSAGE_DEPTHSTENCIL)
1533 if (device->in_scene)
1535 WARN("Rejecting depth / stencil blit while in scene.\n");
1536 goto done;
1539 if (src_rect->left || src_rect->top || src_rect->right != src_desc.width
1540 || src_rect->bottom != src_desc.height)
1542 WARN("Rejecting depth / stencil blit with invalid source rect %s.\n",
1543 wine_dbgstr_rect(src_rect));
1544 goto done;
1547 if (dst_rect->left || dst_rect->top || dst_rect->right != dst_desc.width
1548 || dst_rect->bottom != dst_desc.height)
1550 WARN("Rejecting depth / stencil blit with invalid destination rect %s.\n",
1551 wine_dbgstr_rect(dst_rect));
1552 goto done;
1555 if (src_desc.width != dst_desc.width || src_desc.height != dst_desc.height)
1557 WARN("Rejecting depth / stencil blit with mismatched surface sizes.\n");
1558 goto done;
1562 hr = wined3d_texture_blt(dst->wined3d_texture, dst->sub_resource_idx, dst_rect,
1563 src->wined3d_texture, src->sub_resource_idx, src_rect, 0, NULL, filter);
1564 if (hr == WINEDDERR_INVALIDRECT)
1565 hr = D3DERR_INVALIDCALL;
1567 done:
1568 wined3d_mutex_unlock();
1569 return hr;
1572 static HRESULT WINAPI d3d9_device_ColorFill(IDirect3DDevice9Ex *iface,
1573 IDirect3DSurface9 *surface, const RECT *rect, D3DCOLOR color)
1575 const struct wined3d_color c =
1577 ((color >> 16) & 0xff) / 255.0f,
1578 ((color >> 8) & 0xff) / 255.0f,
1579 (color & 0xff) / 255.0f,
1580 ((color >> 24) & 0xff) / 255.0f,
1582 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1583 struct d3d9_surface *surface_impl = unsafe_impl_from_IDirect3DSurface9(surface);
1584 struct wined3d_sub_resource_desc desc;
1585 struct wined3d_rendertarget_view *rtv;
1586 HRESULT hr;
1588 TRACE("iface %p, surface %p, rect %p, color 0x%08x.\n", iface, surface, rect, color);
1590 wined3d_mutex_lock();
1592 if (FAILED(wined3d_texture_get_sub_resource_desc(surface_impl->wined3d_texture,
1593 surface_impl->sub_resource_idx, &desc)))
1595 wined3d_mutex_unlock();
1596 return D3DERR_INVALIDCALL;
1599 if (desc.pool != WINED3D_POOL_DEFAULT)
1601 wined3d_mutex_unlock();
1602 WARN("Colorfill is not allowed on surfaces in pool %#x, returning D3DERR_INVALIDCALL.\n", desc.pool);
1603 return D3DERR_INVALIDCALL;
1605 if ((desc.usage & (WINED3DUSAGE_RENDERTARGET | WINED3DUSAGE_TEXTURE)) == WINED3DUSAGE_TEXTURE)
1607 wined3d_mutex_unlock();
1608 WARN("Colorfill is not allowed on non-RT textures, returning D3DERR_INVALIDCALL.\n");
1609 return D3DERR_INVALIDCALL;
1611 if (desc.usage & WINED3DUSAGE_DEPTHSTENCIL)
1613 wined3d_mutex_unlock();
1614 WARN("Colorfill is not allowed on depth stencil surfaces, returning D3DERR_INVALIDCALL.\n");
1615 return D3DERR_INVALIDCALL;
1618 rtv = d3d9_surface_acquire_rendertarget_view(surface_impl);
1619 hr = wined3d_device_clear_rendertarget_view(device->wined3d_device,
1620 rtv, rect, WINED3DCLEAR_TARGET, &c, 0.0f, 0);
1621 d3d9_surface_release_rendertarget_view(surface_impl, rtv);
1623 wined3d_mutex_unlock();
1625 return hr;
1628 static HRESULT WINAPI d3d9_device_CreateOffscreenPlainSurface(IDirect3DDevice9Ex *iface,
1629 UINT width, UINT height, D3DFORMAT format, D3DPOOL pool, IDirect3DSurface9 **surface,
1630 HANDLE *shared_handle)
1632 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1633 void *user_mem = NULL;
1635 TRACE("iface %p, width %u, height %u, format %#x, pool %#x, surface %p, shared_handle %p.\n",
1636 iface, width, height, format, pool, surface, shared_handle);
1638 *surface = NULL;
1639 if (pool == D3DPOOL_MANAGED)
1641 WARN("Attempting to create a managed offscreen plain surface.\n");
1642 return D3DERR_INVALIDCALL;
1645 if (shared_handle)
1647 if (!device->d3d_parent->extended)
1649 WARN("Trying to create a shared or user memory surface on a non-ex device.\n");
1650 return E_NOTIMPL;
1653 if (pool == D3DPOOL_SYSTEMMEM)
1654 user_mem = *shared_handle;
1655 else
1657 if (pool != D3DPOOL_DEFAULT)
1659 WARN("Trying to create a shared surface in pool %#x.\n", pool);
1660 return D3DERR_INVALIDCALL;
1662 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1666 /* FIXME: Offscreen surfaces are supposed to be always lockable,
1667 * regardless of the pool they're created in. Should we set dynamic usage
1668 * here? */
1669 return d3d9_device_create_surface(device, width, height, format,
1670 WINED3D_TEXTURE_CREATE_MAPPABLE, surface, 0, pool, D3DMULTISAMPLE_NONE, 0, user_mem);
1673 static HRESULT WINAPI d3d9_device_SetRenderTarget(IDirect3DDevice9Ex *iface, DWORD idx, IDirect3DSurface9 *surface)
1675 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1676 struct d3d9_surface *surface_impl = unsafe_impl_from_IDirect3DSurface9(surface);
1677 struct wined3d_rendertarget_view *rtv;
1678 HRESULT hr;
1680 TRACE("iface %p, idx %u, surface %p.\n", iface, idx, surface);
1682 if (idx >= D3D9_MAX_SIMULTANEOUS_RENDERTARGETS)
1684 WARN("Invalid index %u specified.\n", idx);
1685 return D3DERR_INVALIDCALL;
1688 if (!idx && !surface_impl)
1690 WARN("Trying to set render target 0 to NULL.\n");
1691 return D3DERR_INVALIDCALL;
1694 if (surface_impl && d3d9_surface_get_device(surface_impl) != device)
1696 WARN("Render target surface does not match device.\n");
1697 return D3DERR_INVALIDCALL;
1700 wined3d_mutex_lock();
1701 rtv = surface_impl ? d3d9_surface_acquire_rendertarget_view(surface_impl) : NULL;
1702 hr = wined3d_device_set_rendertarget_view(device->wined3d_device, idx, rtv, TRUE);
1703 d3d9_surface_release_rendertarget_view(surface_impl, rtv);
1704 wined3d_mutex_unlock();
1706 return hr;
1709 static HRESULT WINAPI d3d9_device_GetRenderTarget(IDirect3DDevice9Ex *iface, DWORD idx, IDirect3DSurface9 **surface)
1711 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1712 struct wined3d_rendertarget_view *wined3d_rtv;
1713 struct d3d9_surface *surface_impl;
1714 HRESULT hr = D3D_OK;
1716 TRACE("iface %p, idx %u, surface %p.\n", iface, idx, surface);
1718 if (!surface)
1719 return D3DERR_INVALIDCALL;
1721 if (idx >= D3D9_MAX_SIMULTANEOUS_RENDERTARGETS)
1723 WARN("Invalid index %u specified.\n", idx);
1724 return D3DERR_INVALIDCALL;
1727 wined3d_mutex_lock();
1728 if ((wined3d_rtv = wined3d_device_get_rendertarget_view(device->wined3d_device, idx)))
1730 /* We want the sub resource parent here, since the view itself may be
1731 * internal to wined3d and may not have a parent. */
1732 surface_impl = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_rtv);
1733 *surface = &surface_impl->IDirect3DSurface9_iface;
1734 IDirect3DSurface9_AddRef(*surface);
1736 else
1738 hr = D3DERR_NOTFOUND;
1739 *surface = NULL;
1741 wined3d_mutex_unlock();
1743 return hr;
1746 static HRESULT WINAPI d3d9_device_SetDepthStencilSurface(IDirect3DDevice9Ex *iface, IDirect3DSurface9 *depth_stencil)
1748 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1749 struct d3d9_surface *ds_impl = unsafe_impl_from_IDirect3DSurface9(depth_stencil);
1750 struct wined3d_rendertarget_view *rtv;
1752 TRACE("iface %p, depth_stencil %p.\n", iface, depth_stencil);
1754 wined3d_mutex_lock();
1755 rtv = ds_impl ? d3d9_surface_acquire_rendertarget_view(ds_impl) : NULL;
1756 wined3d_device_set_depth_stencil_view(device->wined3d_device, rtv);
1757 d3d9_surface_release_rendertarget_view(ds_impl, rtv);
1758 wined3d_mutex_unlock();
1760 return D3D_OK;
1763 static HRESULT WINAPI d3d9_device_GetDepthStencilSurface(IDirect3DDevice9Ex *iface, IDirect3DSurface9 **depth_stencil)
1765 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1766 struct wined3d_rendertarget_view *wined3d_dsv;
1767 struct d3d9_surface *surface_impl;
1768 HRESULT hr = D3D_OK;
1770 TRACE("iface %p, depth_stencil %p.\n", iface, depth_stencil);
1772 if (!depth_stencil)
1773 return D3DERR_INVALIDCALL;
1775 wined3d_mutex_lock();
1776 if ((wined3d_dsv = wined3d_device_get_depth_stencil_view(device->wined3d_device)))
1778 /* We want the sub resource parent here, since the view itself may be
1779 * internal to wined3d and may not have a parent. */
1780 surface_impl = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_dsv);
1781 *depth_stencil = &surface_impl->IDirect3DSurface9_iface;
1782 IDirect3DSurface9_AddRef(*depth_stencil);
1784 else
1786 hr = D3DERR_NOTFOUND;
1787 *depth_stencil = NULL;
1789 wined3d_mutex_unlock();
1791 return hr;
1794 static HRESULT WINAPI d3d9_device_BeginScene(IDirect3DDevice9Ex *iface)
1796 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1797 HRESULT hr;
1799 TRACE("iface %p.\n", iface);
1801 wined3d_mutex_lock();
1802 if (SUCCEEDED(hr = wined3d_device_begin_scene(device->wined3d_device)))
1803 device->in_scene = TRUE;
1804 wined3d_mutex_unlock();
1806 return hr;
1809 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_EndScene(IDirect3DDevice9Ex *iface)
1811 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1812 HRESULT hr;
1814 TRACE("iface %p.\n", iface);
1816 wined3d_mutex_lock();
1817 if (SUCCEEDED(hr = wined3d_device_end_scene(device->wined3d_device)))
1818 device->in_scene = FALSE;
1819 wined3d_mutex_unlock();
1821 return hr;
1824 static HRESULT WINAPI d3d9_device_Clear(IDirect3DDevice9Ex *iface, DWORD rect_count,
1825 const D3DRECT *rects, DWORD flags, D3DCOLOR color, float z, DWORD stencil)
1827 const struct wined3d_color c =
1829 ((color >> 16) & 0xff) / 255.0f,
1830 ((color >> 8) & 0xff) / 255.0f,
1831 (color & 0xff) / 255.0f,
1832 ((color >> 24) & 0xff) / 255.0f,
1834 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1835 HRESULT hr;
1837 TRACE("iface %p, rect_count %u, rects %p, flags %#x, color 0x%08x, z %.8e, stencil %u.\n",
1838 iface, rect_count, rects, flags, color, z, stencil);
1840 if (rect_count && !rects)
1842 WARN("count %u with NULL rects.\n", rect_count);
1843 rect_count = 0;
1846 wined3d_mutex_lock();
1847 hr = wined3d_device_clear(device->wined3d_device, rect_count, (const RECT *)rects, flags, &c, z, stencil);
1848 wined3d_mutex_unlock();
1850 return hr;
1853 static HRESULT WINAPI d3d9_device_SetTransform(IDirect3DDevice9Ex *iface,
1854 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
1856 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1858 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1860 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1861 wined3d_mutex_lock();
1862 wined3d_device_set_transform(device->wined3d_device, state, (const struct wined3d_matrix *)matrix);
1863 wined3d_mutex_unlock();
1865 return D3D_OK;
1868 static HRESULT WINAPI d3d9_device_GetTransform(IDirect3DDevice9Ex *iface,
1869 D3DTRANSFORMSTATETYPE state, D3DMATRIX *matrix)
1871 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1873 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1875 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1876 wined3d_mutex_lock();
1877 wined3d_device_get_transform(device->wined3d_device, state, (struct wined3d_matrix *)matrix);
1878 wined3d_mutex_unlock();
1880 return D3D_OK;
1883 static HRESULT WINAPI d3d9_device_MultiplyTransform(IDirect3DDevice9Ex *iface,
1884 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
1886 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1888 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1890 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1891 wined3d_mutex_lock();
1892 wined3d_device_multiply_transform(device->wined3d_device, state, (const struct wined3d_matrix *)matrix);
1893 wined3d_mutex_unlock();
1895 return D3D_OK;
1898 static HRESULT WINAPI d3d9_device_SetViewport(IDirect3DDevice9Ex *iface, const D3DVIEWPORT9 *viewport)
1900 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1901 struct wined3d_viewport vp;
1903 TRACE("iface %p, viewport %p.\n", iface, viewport);
1905 vp.x = viewport->X;
1906 vp.y = viewport->Y;
1907 vp.width = viewport->Width;
1908 vp.height = viewport->Height;
1909 vp.min_z = viewport->MinZ;
1910 vp.max_z = viewport->MaxZ;
1912 wined3d_mutex_lock();
1913 wined3d_device_set_viewport(device->wined3d_device, &vp);
1914 wined3d_mutex_unlock();
1916 return D3D_OK;
1919 static HRESULT WINAPI d3d9_device_GetViewport(IDirect3DDevice9Ex *iface, D3DVIEWPORT9 *viewport)
1921 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1922 struct wined3d_viewport wined3d_viewport;
1924 TRACE("iface %p, viewport %p.\n", iface, viewport);
1926 wined3d_mutex_lock();
1927 wined3d_device_get_viewport(device->wined3d_device, &wined3d_viewport);
1928 wined3d_mutex_unlock();
1930 viewport->X = wined3d_viewport.x;
1931 viewport->Y = wined3d_viewport.y;
1932 viewport->Width = wined3d_viewport.width;
1933 viewport->Height = wined3d_viewport.height;
1934 viewport->MinZ = wined3d_viewport.min_z;
1935 viewport->MaxZ = wined3d_viewport.max_z;
1937 return D3D_OK;
1940 static HRESULT WINAPI d3d9_device_SetMaterial(IDirect3DDevice9Ex *iface, const D3DMATERIAL9 *material)
1942 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1944 TRACE("iface %p, material %p.\n", iface, material);
1946 /* Note: D3DMATERIAL9 is compatible with struct wined3d_material. */
1947 wined3d_mutex_lock();
1948 wined3d_device_set_material(device->wined3d_device, (const struct wined3d_material *)material);
1949 wined3d_mutex_unlock();
1951 return D3D_OK;
1954 static HRESULT WINAPI d3d9_device_GetMaterial(IDirect3DDevice9Ex *iface, D3DMATERIAL9 *material)
1956 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1958 TRACE("iface %p, material %p.\n", iface, material);
1960 /* Note: D3DMATERIAL9 is compatible with struct wined3d_material. */
1961 wined3d_mutex_lock();
1962 wined3d_device_get_material(device->wined3d_device, (struct wined3d_material *)material);
1963 wined3d_mutex_unlock();
1965 return D3D_OK;
1968 static HRESULT WINAPI d3d9_device_SetLight(IDirect3DDevice9Ex *iface, DWORD index, const D3DLIGHT9 *light)
1970 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1971 HRESULT hr;
1973 TRACE("iface %p, index %u, light %p.\n", iface, index, light);
1975 /* Note: D3DLIGHT9 is compatible with struct wined3d_light. */
1976 wined3d_mutex_lock();
1977 hr = wined3d_device_set_light(device->wined3d_device, index, (const struct wined3d_light *)light);
1978 wined3d_mutex_unlock();
1980 return hr;
1983 static HRESULT WINAPI d3d9_device_GetLight(IDirect3DDevice9Ex *iface, DWORD index, D3DLIGHT9 *light)
1985 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1986 HRESULT hr;
1988 TRACE("iface %p, index %u, light %p.\n", iface, index, light);
1990 /* Note: D3DLIGHT9 is compatible with struct wined3d_light. */
1991 wined3d_mutex_lock();
1992 hr = wined3d_device_get_light(device->wined3d_device, index, (struct wined3d_light *)light);
1993 wined3d_mutex_unlock();
1995 return hr;
1998 static HRESULT WINAPI d3d9_device_LightEnable(IDirect3DDevice9Ex *iface, DWORD index, BOOL enable)
2000 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2001 HRESULT hr;
2003 TRACE("iface %p, index %u, enable %#x.\n", iface, index, enable);
2005 wined3d_mutex_lock();
2006 hr = wined3d_device_set_light_enable(device->wined3d_device, index, enable);
2007 wined3d_mutex_unlock();
2009 return hr;
2012 static HRESULT WINAPI d3d9_device_GetLightEnable(IDirect3DDevice9Ex *iface, DWORD index, BOOL *enable)
2014 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2015 HRESULT hr;
2017 TRACE("iface %p, index %u, enable %p.\n", iface, index, enable);
2019 wined3d_mutex_lock();
2020 hr = wined3d_device_get_light_enable(device->wined3d_device, index, enable);
2021 wined3d_mutex_unlock();
2023 return hr;
2026 static HRESULT WINAPI d3d9_device_SetClipPlane(IDirect3DDevice9Ex *iface, DWORD index, const float *plane)
2028 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2029 HRESULT hr;
2031 TRACE("iface %p, index %u, plane %p.\n", iface, index, plane);
2033 index = min(index, device->max_user_clip_planes - 1);
2035 wined3d_mutex_lock();
2036 hr = wined3d_device_set_clip_plane(device->wined3d_device, index, (const struct wined3d_vec4 *)plane);
2037 wined3d_mutex_unlock();
2039 return hr;
2042 static HRESULT WINAPI d3d9_device_GetClipPlane(IDirect3DDevice9Ex *iface, DWORD index, float *plane)
2044 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2045 HRESULT hr;
2047 TRACE("iface %p, index %u, plane %p.\n", iface, index, plane);
2049 index = min(index, device->max_user_clip_planes - 1);
2051 wined3d_mutex_lock();
2052 hr = wined3d_device_get_clip_plane(device->wined3d_device, index, (struct wined3d_vec4 *)plane);
2053 wined3d_mutex_unlock();
2055 return hr;
2058 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_SetRenderState(IDirect3DDevice9Ex *iface,
2059 D3DRENDERSTATETYPE state, DWORD value)
2061 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2063 TRACE("iface %p, state %#x, value %#x.\n", iface, state, value);
2065 wined3d_mutex_lock();
2066 wined3d_device_set_render_state(device->wined3d_device, state, value);
2067 wined3d_mutex_unlock();
2069 return D3D_OK;
2072 static HRESULT WINAPI d3d9_device_GetRenderState(IDirect3DDevice9Ex *iface,
2073 D3DRENDERSTATETYPE state, DWORD *value)
2075 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2077 TRACE("iface %p, state %#x, value %p.\n", iface, state, value);
2079 wined3d_mutex_lock();
2080 *value = wined3d_device_get_render_state(device->wined3d_device, state);
2081 wined3d_mutex_unlock();
2083 return D3D_OK;
2086 static HRESULT WINAPI d3d9_device_CreateStateBlock(IDirect3DDevice9Ex *iface,
2087 D3DSTATEBLOCKTYPE type, IDirect3DStateBlock9 **stateblock)
2089 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2090 struct d3d9_stateblock *object;
2091 HRESULT hr;
2093 TRACE("iface %p, type %#x, stateblock %p.\n", iface, type, stateblock);
2095 if (type != D3DSBT_ALL && type != D3DSBT_PIXELSTATE && type != D3DSBT_VERTEXSTATE)
2097 WARN("Unexpected stateblock type, returning D3DERR_INVALIDCALL.\n");
2098 return D3DERR_INVALIDCALL;
2101 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2102 if (!object)
2103 return E_OUTOFMEMORY;
2105 hr = stateblock_init(object, device, type, NULL);
2106 if (FAILED(hr))
2108 WARN("Failed to initialize stateblock, hr %#x.\n", hr);
2109 HeapFree(GetProcessHeap(), 0, object);
2110 return hr;
2113 TRACE("Created stateblock %p.\n", object);
2114 *stateblock = &object->IDirect3DStateBlock9_iface;
2116 return D3D_OK;
2119 static HRESULT WINAPI d3d9_device_BeginStateBlock(IDirect3DDevice9Ex *iface)
2121 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2122 HRESULT hr;
2124 TRACE("iface %p.\n", iface);
2126 wined3d_mutex_lock();
2127 hr = wined3d_device_begin_stateblock(device->wined3d_device);
2128 wined3d_mutex_unlock();
2130 return hr;
2133 static HRESULT WINAPI d3d9_device_EndStateBlock(IDirect3DDevice9Ex *iface, IDirect3DStateBlock9 **stateblock)
2135 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2136 struct wined3d_stateblock *wined3d_stateblock;
2137 struct d3d9_stateblock *object;
2138 HRESULT hr;
2140 TRACE("iface %p, stateblock %p.\n", iface, stateblock);
2142 wined3d_mutex_lock();
2143 hr = wined3d_device_end_stateblock(device->wined3d_device, &wined3d_stateblock);
2144 wined3d_mutex_unlock();
2145 if (FAILED(hr))
2147 WARN("Failed to end the state block, hr %#x.\n", hr);
2148 return hr;
2151 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2152 if (!object)
2154 wined3d_mutex_lock();
2155 wined3d_stateblock_decref(wined3d_stateblock);
2156 wined3d_mutex_unlock();
2157 return E_OUTOFMEMORY;
2160 hr = stateblock_init(object, device, 0, wined3d_stateblock);
2161 if (FAILED(hr))
2163 WARN("Failed to initialize stateblock, hr %#x.\n", hr);
2164 wined3d_mutex_lock();
2165 wined3d_stateblock_decref(wined3d_stateblock);
2166 wined3d_mutex_unlock();
2167 HeapFree(GetProcessHeap(), 0, object);
2168 return hr;
2171 TRACE("Created stateblock %p.\n", object);
2172 *stateblock = &object->IDirect3DStateBlock9_iface;
2174 return D3D_OK;
2177 static HRESULT WINAPI d3d9_device_SetClipStatus(IDirect3DDevice9Ex *iface, const D3DCLIPSTATUS9 *clip_status)
2179 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2180 HRESULT hr;
2182 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
2184 wined3d_mutex_lock();
2185 hr = wined3d_device_set_clip_status(device->wined3d_device, (const struct wined3d_clip_status *)clip_status);
2186 wined3d_mutex_unlock();
2188 return hr;
2191 static HRESULT WINAPI d3d9_device_GetClipStatus(IDirect3DDevice9Ex *iface, D3DCLIPSTATUS9 *clip_status)
2193 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2194 HRESULT hr;
2196 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
2198 wined3d_mutex_lock();
2199 hr = wined3d_device_get_clip_status(device->wined3d_device, (struct wined3d_clip_status *)clip_status);
2200 wined3d_mutex_unlock();
2202 return hr;
2205 static HRESULT WINAPI d3d9_device_GetTexture(IDirect3DDevice9Ex *iface, DWORD stage, IDirect3DBaseTexture9 **texture)
2207 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2208 struct wined3d_texture *wined3d_texture = NULL;
2209 struct d3d9_texture *texture_impl;
2211 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
2213 if (!texture)
2214 return D3DERR_INVALIDCALL;
2216 wined3d_mutex_lock();
2217 if ((wined3d_texture = wined3d_device_get_texture(device->wined3d_device, stage)))
2219 texture_impl = wined3d_texture_get_parent(wined3d_texture);
2220 *texture = &texture_impl->IDirect3DBaseTexture9_iface;
2221 IDirect3DBaseTexture9_AddRef(*texture);
2223 else
2225 *texture = NULL;
2227 wined3d_mutex_unlock();
2229 return D3D_OK;
2232 static HRESULT WINAPI d3d9_device_SetTexture(IDirect3DDevice9Ex *iface, DWORD stage, IDirect3DBaseTexture9 *texture)
2234 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2235 struct d3d9_texture *texture_impl;
2236 HRESULT hr;
2238 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
2240 texture_impl = unsafe_impl_from_IDirect3DBaseTexture9(texture);
2242 wined3d_mutex_lock();
2243 hr = wined3d_device_set_texture(device->wined3d_device, stage,
2244 texture_impl ? texture_impl->wined3d_texture : NULL);
2245 wined3d_mutex_unlock();
2247 return hr;
2250 static const enum wined3d_texture_stage_state tss_lookup[] =
2252 WINED3D_TSS_INVALID, /* 0, unused */
2253 WINED3D_TSS_COLOR_OP, /* 1, D3DTSS_COLOROP */
2254 WINED3D_TSS_COLOR_ARG1, /* 2, D3DTSS_COLORARG1 */
2255 WINED3D_TSS_COLOR_ARG2, /* 3, D3DTSS_COLORARG2 */
2256 WINED3D_TSS_ALPHA_OP, /* 4, D3DTSS_ALPHAOP */
2257 WINED3D_TSS_ALPHA_ARG1, /* 5, D3DTSS_ALPHAARG1 */
2258 WINED3D_TSS_ALPHA_ARG2, /* 6, D3DTSS_ALPHAARG2 */
2259 WINED3D_TSS_BUMPENV_MAT00, /* 7, D3DTSS_BUMPENVMAT00 */
2260 WINED3D_TSS_BUMPENV_MAT01, /* 8, D3DTSS_BUMPENVMAT01 */
2261 WINED3D_TSS_BUMPENV_MAT10, /* 9, D3DTSS_BUMPENVMAT10 */
2262 WINED3D_TSS_BUMPENV_MAT11, /* 10, D3DTSS_BUMPENVMAT11 */
2263 WINED3D_TSS_TEXCOORD_INDEX, /* 11, D3DTSS_TEXCOORDINDEX */
2264 WINED3D_TSS_INVALID, /* 12, unused */
2265 WINED3D_TSS_INVALID, /* 13, unused */
2266 WINED3D_TSS_INVALID, /* 14, unused */
2267 WINED3D_TSS_INVALID, /* 15, unused */
2268 WINED3D_TSS_INVALID, /* 16, unused */
2269 WINED3D_TSS_INVALID, /* 17, unused */
2270 WINED3D_TSS_INVALID, /* 18, unused */
2271 WINED3D_TSS_INVALID, /* 19, unused */
2272 WINED3D_TSS_INVALID, /* 20, unused */
2273 WINED3D_TSS_INVALID, /* 21, unused */
2274 WINED3D_TSS_BUMPENV_LSCALE, /* 22, D3DTSS_BUMPENVLSCALE */
2275 WINED3D_TSS_BUMPENV_LOFFSET, /* 23, D3DTSS_BUMPENVLOFFSET */
2276 WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS, /* 24, D3DTSS_TEXTURETRANSFORMFLAGS */
2277 WINED3D_TSS_INVALID, /* 25, unused */
2278 WINED3D_TSS_COLOR_ARG0, /* 26, D3DTSS_COLORARG0 */
2279 WINED3D_TSS_ALPHA_ARG0, /* 27, D3DTSS_ALPHAARG0 */
2280 WINED3D_TSS_RESULT_ARG, /* 28, D3DTSS_RESULTARG */
2281 WINED3D_TSS_INVALID, /* 29, unused */
2282 WINED3D_TSS_INVALID, /* 30, unused */
2283 WINED3D_TSS_INVALID, /* 31, unused */
2284 WINED3D_TSS_CONSTANT, /* 32, D3DTSS_CONSTANT */
2287 static HRESULT WINAPI d3d9_device_GetTextureStageState(IDirect3DDevice9Ex *iface,
2288 DWORD stage, D3DTEXTURESTAGESTATETYPE state, DWORD *value)
2290 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2292 TRACE("iface %p, stage %u, state %#x, value %p.\n", iface, stage, state, value);
2294 if (state >= sizeof(tss_lookup) / sizeof(*tss_lookup))
2296 WARN("Invalid state %#x passed.\n", state);
2297 return D3D_OK;
2300 wined3d_mutex_lock();
2301 *value = wined3d_device_get_texture_stage_state(device->wined3d_device, stage, tss_lookup[state]);
2302 wined3d_mutex_unlock();
2304 return D3D_OK;
2307 static HRESULT WINAPI d3d9_device_SetTextureStageState(IDirect3DDevice9Ex *iface,
2308 DWORD stage, D3DTEXTURESTAGESTATETYPE state, DWORD value)
2310 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2312 TRACE("iface %p, stage %u, state %#x, value %#x.\n", iface, stage, state, value);
2314 if (state >= sizeof(tss_lookup) / sizeof(*tss_lookup))
2316 WARN("Invalid state %#x passed.\n", state);
2317 return D3D_OK;
2320 wined3d_mutex_lock();
2321 wined3d_device_set_texture_stage_state(device->wined3d_device, stage, tss_lookup[state], value);
2322 wined3d_mutex_unlock();
2324 return D3D_OK;
2327 static HRESULT WINAPI d3d9_device_GetSamplerState(IDirect3DDevice9Ex *iface,
2328 DWORD sampler, D3DSAMPLERSTATETYPE state, DWORD *value)
2330 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2332 TRACE("iface %p, sampler %u, state %#x, value %p.\n", iface, sampler, state, value);
2334 wined3d_mutex_lock();
2335 *value = wined3d_device_get_sampler_state(device->wined3d_device, sampler, state);
2336 wined3d_mutex_unlock();
2338 return D3D_OK;
2341 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_SetSamplerState(IDirect3DDevice9Ex *iface,
2342 DWORD sampler, D3DSAMPLERSTATETYPE state, DWORD value)
2344 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2346 TRACE("iface %p, sampler %u, state %#x, value %#x.\n", iface, sampler, state, value);
2348 wined3d_mutex_lock();
2349 wined3d_device_set_sampler_state(device->wined3d_device, sampler, state, value);
2350 wined3d_mutex_unlock();
2352 return D3D_OK;
2355 static HRESULT WINAPI d3d9_device_ValidateDevice(IDirect3DDevice9Ex *iface, DWORD *pass_count)
2357 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2358 HRESULT hr;
2360 TRACE("iface %p, pass_count %p.\n", iface, pass_count);
2362 wined3d_mutex_lock();
2363 hr = wined3d_device_validate_device(device->wined3d_device, pass_count);
2364 wined3d_mutex_unlock();
2366 return hr;
2369 static HRESULT WINAPI d3d9_device_SetPaletteEntries(IDirect3DDevice9Ex *iface,
2370 UINT palette_idx, const PALETTEENTRY *entries)
2372 WARN("iface %p, palette_idx %u, entries %p unimplemented.\n", iface, palette_idx, entries);
2374 /* The d3d9 palette API is non-functional on Windows. Getters and setters are implemented,
2375 * and some drivers allow the creation of P8 surfaces. These surfaces can be copied to
2376 * other P8 surfaces with StretchRect, but cannot be converted to (A)RGB.
2378 * Some older(dx7) cards may have support for P8 textures, but games cannot rely on this. */
2379 return D3D_OK;
2382 static HRESULT WINAPI d3d9_device_GetPaletteEntries(IDirect3DDevice9Ex *iface,
2383 UINT palette_idx, PALETTEENTRY *entries)
2385 FIXME("iface %p, palette_idx %u, entries %p unimplemented.\n", iface, palette_idx, entries);
2387 return D3DERR_INVALIDCALL;
2390 static HRESULT WINAPI d3d9_device_SetCurrentTexturePalette(IDirect3DDevice9Ex *iface, UINT palette_idx)
2392 WARN("iface %p, palette_idx %u unimplemented.\n", iface, palette_idx);
2394 return D3D_OK;
2397 static HRESULT WINAPI d3d9_device_GetCurrentTexturePalette(IDirect3DDevice9Ex *iface, UINT *palette_idx)
2399 FIXME("iface %p, palette_idx %p.\n", iface, palette_idx);
2401 return D3DERR_INVALIDCALL;
2404 static HRESULT WINAPI d3d9_device_SetScissorRect(IDirect3DDevice9Ex *iface, const RECT *rect)
2406 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2408 TRACE("iface %p, rect %p.\n", iface, rect);
2410 wined3d_mutex_lock();
2411 wined3d_device_set_scissor_rect(device->wined3d_device, rect);
2412 wined3d_mutex_unlock();
2414 return D3D_OK;
2417 static HRESULT WINAPI d3d9_device_GetScissorRect(IDirect3DDevice9Ex *iface, RECT *rect)
2419 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2421 TRACE("iface %p, rect %p.\n", iface, rect);
2423 wined3d_mutex_lock();
2424 wined3d_device_get_scissor_rect(device->wined3d_device, rect);
2425 wined3d_mutex_unlock();
2427 return D3D_OK;
2430 static HRESULT WINAPI d3d9_device_SetSoftwareVertexProcessing(IDirect3DDevice9Ex *iface, BOOL software)
2432 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2434 TRACE("iface %p, software %#x.\n", iface, software);
2436 wined3d_mutex_lock();
2437 wined3d_device_set_software_vertex_processing(device->wined3d_device, software);
2438 wined3d_mutex_unlock();
2440 return D3D_OK;
2443 static BOOL WINAPI d3d9_device_GetSoftwareVertexProcessing(IDirect3DDevice9Ex *iface)
2445 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2446 BOOL ret;
2448 TRACE("iface %p.\n", iface);
2450 wined3d_mutex_lock();
2451 ret = wined3d_device_get_software_vertex_processing(device->wined3d_device);
2452 wined3d_mutex_unlock();
2454 return ret;
2457 static HRESULT WINAPI d3d9_device_SetNPatchMode(IDirect3DDevice9Ex *iface, float segment_count)
2459 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2460 HRESULT hr;
2462 TRACE("iface %p, segment_count %.8e.\n", iface, segment_count);
2464 wined3d_mutex_lock();
2465 hr = wined3d_device_set_npatch_mode(device->wined3d_device, segment_count);
2466 wined3d_mutex_unlock();
2468 return hr;
2471 static float WINAPI d3d9_device_GetNPatchMode(IDirect3DDevice9Ex *iface)
2473 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2474 float ret;
2476 TRACE("iface %p.\n", iface);
2478 wined3d_mutex_lock();
2479 ret = wined3d_device_get_npatch_mode(device->wined3d_device);
2480 wined3d_mutex_unlock();
2482 return ret;
2485 static HRESULT WINAPI d3d9_device_DrawPrimitive(IDirect3DDevice9Ex *iface,
2486 D3DPRIMITIVETYPE primitive_type, UINT start_vertex, UINT primitive_count)
2488 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2489 HRESULT hr;
2491 TRACE("iface %p, primitive_type %#x, start_vertex %u, primitive_count %u.\n",
2492 iface, primitive_type, start_vertex, primitive_count);
2494 wined3d_mutex_lock();
2495 if (!device->has_vertex_declaration)
2497 wined3d_mutex_unlock();
2498 WARN("Called without a valid vertex declaration set.\n");
2499 return D3DERR_INVALIDCALL;
2501 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type, 0);
2502 hr = wined3d_device_draw_primitive(device->wined3d_device, start_vertex,
2503 vertex_count_from_primitive_count(primitive_type, primitive_count));
2504 wined3d_mutex_unlock();
2506 return hr;
2509 static HRESULT WINAPI d3d9_device_DrawIndexedPrimitive(IDirect3DDevice9Ex *iface,
2510 D3DPRIMITIVETYPE primitive_type, INT base_vertex_idx, UINT min_vertex_idx,
2511 UINT vertex_count, UINT start_idx, UINT primitive_count)
2513 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2514 HRESULT hr;
2516 TRACE("iface %p, primitive_type %#x, base_vertex_idx %u, min_vertex_idx %u, "
2517 "vertex_count %u, start_idx %u, primitive_count %u.\n",
2518 iface, primitive_type, base_vertex_idx, min_vertex_idx,
2519 vertex_count, start_idx, primitive_count);
2521 wined3d_mutex_lock();
2522 if (!device->has_vertex_declaration)
2524 wined3d_mutex_unlock();
2525 WARN("Called without a valid vertex declaration set.\n");
2526 return D3DERR_INVALIDCALL;
2528 wined3d_device_set_base_vertex_index(device->wined3d_device, base_vertex_idx);
2529 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type, 0);
2530 hr = wined3d_device_draw_indexed_primitive(device->wined3d_device, start_idx,
2531 vertex_count_from_primitive_count(primitive_type, primitive_count));
2532 wined3d_mutex_unlock();
2534 return hr;
2537 /* The caller is responsible for wined3d locking */
2538 static HRESULT d3d9_device_prepare_vertex_buffer(struct d3d9_device *device, UINT min_size)
2540 HRESULT hr;
2542 if (device->vertex_buffer_size < min_size || !device->vertex_buffer)
2544 UINT size = max(device->vertex_buffer_size * 2, min_size);
2545 struct wined3d_buffer *buffer;
2547 TRACE("Growing vertex buffer to %u bytes.\n", size);
2549 hr = wined3d_buffer_create_vb(device->wined3d_device, size, WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_WRITEONLY,
2550 WINED3D_POOL_DEFAULT, NULL, &d3d9_null_wined3d_parent_ops, &buffer);
2551 if (FAILED(hr))
2553 ERR("(%p) wined3d_buffer_create_vb failed with hr = %08x.\n", device, hr);
2554 return hr;
2557 if (device->vertex_buffer)
2558 wined3d_buffer_decref(device->vertex_buffer);
2560 device->vertex_buffer = buffer;
2561 device->vertex_buffer_size = size;
2562 device->vertex_buffer_pos = 0;
2564 return D3D_OK;
2567 static HRESULT WINAPI d3d9_device_DrawPrimitiveUP(IDirect3DDevice9Ex *iface,
2568 D3DPRIMITIVETYPE primitive_type, UINT primitive_count, const void *data, UINT stride)
2570 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2571 HRESULT hr;
2572 UINT vtx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2573 struct wined3d_map_desc wined3d_map_desc;
2574 struct wined3d_box wined3d_box = {0};
2575 UINT size = vtx_count * stride;
2576 struct wined3d_resource *vb;
2577 UINT vb_pos, align;
2579 TRACE("iface %p, primitive_type %#x, primitive_count %u, data %p, stride %u.\n",
2580 iface, primitive_type, primitive_count, data, stride);
2582 if (!primitive_count)
2584 WARN("primitive_count is 0, returning D3D_OK\n");
2585 return D3D_OK;
2588 wined3d_mutex_lock();
2590 if (!device->has_vertex_declaration)
2592 wined3d_mutex_unlock();
2593 WARN("Called without a valid vertex declaration set.\n");
2594 return D3DERR_INVALIDCALL;
2597 hr = d3d9_device_prepare_vertex_buffer(device, size);
2598 if (FAILED(hr))
2599 goto done;
2601 vb_pos = device->vertex_buffer_pos;
2602 align = vb_pos % stride;
2603 if (align) align = stride - align;
2604 if (vb_pos + size + align > device->vertex_buffer_size)
2605 vb_pos = 0;
2606 else
2607 vb_pos += align;
2609 wined3d_box.left = vb_pos;
2610 wined3d_box.right = vb_pos + size;
2611 vb = wined3d_buffer_get_resource(device->vertex_buffer);
2612 if (FAILED(hr = wined3d_resource_map(vb, 0, &wined3d_map_desc, &wined3d_box,
2613 vb_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD)))
2614 goto done;
2615 memcpy(wined3d_map_desc.data, data, size);
2616 wined3d_resource_unmap(vb, 0);
2617 device->vertex_buffer_pos = vb_pos + size;
2619 hr = wined3d_device_set_stream_source(device->wined3d_device, 0, device->vertex_buffer, 0, stride);
2620 if (FAILED(hr))
2621 goto done;
2623 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type, 0);
2624 hr = wined3d_device_draw_primitive(device->wined3d_device, vb_pos / stride, vtx_count);
2625 wined3d_device_set_stream_source(device->wined3d_device, 0, NULL, 0, 0);
2627 done:
2628 wined3d_mutex_unlock();
2629 return hr;
2632 /* The caller is responsible for wined3d locking */
2633 static HRESULT d3d9_device_prepare_index_buffer(struct d3d9_device *device, UINT min_size)
2635 HRESULT hr;
2637 if (device->index_buffer_size < min_size || !device->index_buffer)
2639 UINT size = max(device->index_buffer_size * 2, min_size);
2640 struct wined3d_buffer *buffer;
2642 TRACE("Growing index buffer to %u bytes.\n", size);
2644 hr = wined3d_buffer_create_ib(device->wined3d_device, size, WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_WRITEONLY,
2645 WINED3D_POOL_DEFAULT, NULL, &d3d9_null_wined3d_parent_ops, &buffer);
2646 if (FAILED(hr))
2648 ERR("(%p) wined3d_buffer_create_ib failed with hr = %08x.\n", device, hr);
2649 return hr;
2652 if (device->index_buffer)
2653 wined3d_buffer_decref(device->index_buffer);
2655 device->index_buffer = buffer;
2656 device->index_buffer_size = size;
2657 device->index_buffer_pos = 0;
2659 return D3D_OK;
2662 static HRESULT WINAPI d3d9_device_DrawIndexedPrimitiveUP(IDirect3DDevice9Ex *iface,
2663 D3DPRIMITIVETYPE primitive_type, UINT min_vertex_idx, UINT vertex_count,
2664 UINT primitive_count, const void *index_data, D3DFORMAT index_format,
2665 const void *vertex_data, UINT vertex_stride)
2667 UINT idx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2668 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2669 UINT idx_fmt_size = index_format == D3DFMT_INDEX16 ? 2 : 4;
2670 UINT vtx_size = vertex_count * vertex_stride;
2671 UINT idx_size = idx_count * idx_fmt_size;
2672 struct wined3d_map_desc wined3d_map_desc;
2673 struct wined3d_box wined3d_box = {0};
2674 struct wined3d_resource *ib, *vb;
2675 UINT vb_pos, ib_pos, align;
2676 HRESULT hr;
2678 TRACE("iface %p, primitive_type %#x, min_vertex_idx %u, vertex_count %u, primitive_count %u, "
2679 "index_data %p, index_format %#x, vertex_data %p, vertex_stride %u.\n",
2680 iface, primitive_type, min_vertex_idx, vertex_count, primitive_count,
2681 index_data, index_format, vertex_data, vertex_stride);
2683 if (!primitive_count)
2685 WARN("primitive_count is 0, returning D3D_OK.\n");
2686 return D3D_OK;
2689 wined3d_mutex_lock();
2691 if (!device->has_vertex_declaration)
2693 wined3d_mutex_unlock();
2694 WARN("Called without a valid vertex declaration set.\n");
2695 return D3DERR_INVALIDCALL;
2698 hr = d3d9_device_prepare_vertex_buffer(device, vtx_size);
2699 if (FAILED(hr))
2700 goto done;
2702 vb_pos = device->vertex_buffer_pos;
2703 align = vb_pos % vertex_stride;
2704 if (align) align = vertex_stride - align;
2705 if (vb_pos + vtx_size + align > device->vertex_buffer_size)
2706 vb_pos = 0;
2707 else
2708 vb_pos += align;
2710 wined3d_box.left = vb_pos;
2711 wined3d_box.right = vb_pos + vtx_size;
2712 vb = wined3d_buffer_get_resource(device->vertex_buffer);
2713 if (FAILED(hr = wined3d_resource_map(vb, 0, &wined3d_map_desc, &wined3d_box,
2714 vb_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD)))
2715 goto done;
2716 memcpy(wined3d_map_desc.data, (char *)vertex_data + min_vertex_idx * vertex_stride, vtx_size);
2717 wined3d_resource_unmap(vb, 0);
2718 device->vertex_buffer_pos = vb_pos + vtx_size;
2720 hr = d3d9_device_prepare_index_buffer(device, idx_size);
2721 if (FAILED(hr))
2722 goto done;
2724 ib_pos = device->index_buffer_pos;
2725 align = ib_pos % idx_fmt_size;
2726 if (align) align = idx_fmt_size - align;
2727 if (ib_pos + idx_size + align > device->index_buffer_size)
2728 ib_pos = 0;
2729 else
2730 ib_pos += align;
2732 wined3d_box.left = ib_pos;
2733 wined3d_box.right = ib_pos + idx_size;
2734 ib = wined3d_buffer_get_resource(device->index_buffer);
2735 if (FAILED(hr = wined3d_resource_map(ib, 0, &wined3d_map_desc, &wined3d_box,
2736 ib_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD)))
2737 goto done;
2738 memcpy(wined3d_map_desc.data, index_data, idx_size);
2739 wined3d_resource_unmap(ib, 0);
2740 device->index_buffer_pos = ib_pos + idx_size;
2742 hr = wined3d_device_set_stream_source(device->wined3d_device, 0, device->vertex_buffer, 0, vertex_stride);
2743 if (FAILED(hr))
2744 goto done;
2746 wined3d_device_set_index_buffer(device->wined3d_device, device->index_buffer,
2747 wined3dformat_from_d3dformat(index_format), 0);
2748 wined3d_device_set_base_vertex_index(device->wined3d_device, vb_pos / vertex_stride - min_vertex_idx);
2750 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type, 0);
2751 hr = wined3d_device_draw_indexed_primitive(device->wined3d_device, ib_pos / idx_fmt_size, idx_count);
2753 wined3d_device_set_stream_source(device->wined3d_device, 0, NULL, 0, 0);
2754 wined3d_device_set_index_buffer(device->wined3d_device, NULL, WINED3DFMT_UNKNOWN, 0);
2755 wined3d_device_set_base_vertex_index(device->wined3d_device, 0);
2757 done:
2758 wined3d_mutex_unlock();
2759 return hr;
2762 static HRESULT WINAPI d3d9_device_ProcessVertices(IDirect3DDevice9Ex *iface,
2763 UINT src_start_idx, UINT dst_idx, UINT vertex_count, IDirect3DVertexBuffer9 *dst_buffer,
2764 IDirect3DVertexDeclaration9 *declaration, DWORD flags)
2766 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2767 struct d3d9_vertexbuffer *dst_impl = unsafe_impl_from_IDirect3DVertexBuffer9(dst_buffer);
2768 struct d3d9_vertex_declaration *decl_impl = unsafe_impl_from_IDirect3DVertexDeclaration9(declaration);
2769 HRESULT hr;
2771 TRACE("iface %p, src_start_idx %u, dst_idx %u, vertex_count %u, dst_buffer %p, declaration %p, flags %#x.\n",
2772 iface, src_start_idx, dst_idx, vertex_count, dst_buffer, declaration, flags);
2774 wined3d_mutex_lock();
2775 hr = wined3d_device_process_vertices(device->wined3d_device, src_start_idx, dst_idx, vertex_count,
2776 dst_impl->wined3d_buffer, decl_impl ? decl_impl->wined3d_declaration : NULL,
2777 flags, dst_impl->fvf);
2778 wined3d_mutex_unlock();
2780 return hr;
2783 static HRESULT WINAPI d3d9_device_CreateVertexDeclaration(IDirect3DDevice9Ex *iface,
2784 const D3DVERTEXELEMENT9 *elements, IDirect3DVertexDeclaration9 **declaration)
2786 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2787 struct d3d9_vertex_declaration *object;
2788 HRESULT hr;
2790 TRACE("iface %p, elements %p, declaration %p.\n", iface, elements, declaration);
2792 if (!declaration)
2794 WARN("Caller passed a NULL declaration, returning D3DERR_INVALIDCALL.\n");
2795 return D3DERR_INVALIDCALL;
2798 if (SUCCEEDED(hr = d3d9_vertex_declaration_create(device, elements, &object)))
2799 *declaration = &object->IDirect3DVertexDeclaration9_iface;
2801 return hr;
2804 static HRESULT WINAPI d3d9_device_SetVertexDeclaration(IDirect3DDevice9Ex *iface,
2805 IDirect3DVertexDeclaration9 *declaration)
2807 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2808 struct d3d9_vertex_declaration *decl_impl = unsafe_impl_from_IDirect3DVertexDeclaration9(declaration);
2810 TRACE("iface %p, declaration %p.\n", iface, declaration);
2812 wined3d_mutex_lock();
2813 wined3d_device_set_vertex_declaration(device->wined3d_device,
2814 decl_impl ? decl_impl->wined3d_declaration : NULL);
2815 device->has_vertex_declaration = !!decl_impl;
2816 wined3d_mutex_unlock();
2818 return D3D_OK;
2821 static HRESULT WINAPI d3d9_device_GetVertexDeclaration(IDirect3DDevice9Ex *iface,
2822 IDirect3DVertexDeclaration9 **declaration)
2824 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2825 struct wined3d_vertex_declaration *wined3d_declaration;
2826 struct d3d9_vertex_declaration *declaration_impl;
2828 TRACE("iface %p, declaration %p.\n", iface, declaration);
2830 if (!declaration) return D3DERR_INVALIDCALL;
2832 wined3d_mutex_lock();
2833 if ((wined3d_declaration = wined3d_device_get_vertex_declaration(device->wined3d_device)))
2835 declaration_impl = wined3d_vertex_declaration_get_parent(wined3d_declaration);
2836 *declaration = &declaration_impl->IDirect3DVertexDeclaration9_iface;
2837 IDirect3DVertexDeclaration9_AddRef(*declaration);
2839 else
2841 *declaration = NULL;
2843 wined3d_mutex_unlock();
2845 TRACE("Returning %p.\n", *declaration);
2846 return D3D_OK;
2849 static struct wined3d_vertex_declaration *device_get_fvf_declaration(struct d3d9_device *device, DWORD fvf)
2851 struct wined3d_vertex_declaration *wined3d_declaration;
2852 struct fvf_declaration *fvf_decls = device->fvf_decls;
2853 struct d3d9_vertex_declaration *d3d9_declaration;
2854 D3DVERTEXELEMENT9 *elements;
2855 int p, low, high; /* deliberately signed */
2856 HRESULT hr;
2858 TRACE("Searching for declaration for fvf %08x... ", fvf);
2860 low = 0;
2861 high = device->fvf_decl_count - 1;
2862 while (low <= high)
2864 p = (low + high) >> 1;
2865 TRACE("%d ", p);
2867 if (fvf_decls[p].fvf == fvf)
2869 TRACE("found %p.\n", fvf_decls[p].decl);
2870 return fvf_decls[p].decl;
2873 if (fvf_decls[p].fvf < fvf)
2874 low = p + 1;
2875 else
2876 high = p - 1;
2878 TRACE("not found. Creating and inserting at position %d.\n", low);
2880 if (FAILED(hr = vdecl_convert_fvf(fvf, &elements)))
2881 return NULL;
2883 hr = d3d9_vertex_declaration_create(device, elements, &d3d9_declaration);
2884 HeapFree(GetProcessHeap(), 0, elements);
2885 if (FAILED(hr))
2886 return NULL;
2888 if (device->fvf_decl_size == device->fvf_decl_count)
2890 UINT grow = max(device->fvf_decl_size / 2, 8);
2892 fvf_decls = HeapReAlloc(GetProcessHeap(), 0, fvf_decls, sizeof(*fvf_decls) * (device->fvf_decl_size + grow));
2893 if (!fvf_decls)
2895 IDirect3DVertexDeclaration9_Release(&d3d9_declaration->IDirect3DVertexDeclaration9_iface);
2896 return NULL;
2898 device->fvf_decls = fvf_decls;
2899 device->fvf_decl_size += grow;
2902 d3d9_declaration->fvf = fvf;
2903 wined3d_declaration = d3d9_declaration->wined3d_declaration;
2904 wined3d_vertex_declaration_incref(wined3d_declaration);
2905 IDirect3DVertexDeclaration9_Release(&d3d9_declaration->IDirect3DVertexDeclaration9_iface);
2907 memmove(fvf_decls + low + 1, fvf_decls + low, sizeof(*fvf_decls) * (device->fvf_decl_count - low));
2908 fvf_decls[low].decl = wined3d_declaration;
2909 fvf_decls[low].fvf = fvf;
2910 ++device->fvf_decl_count;
2912 TRACE("Returning %p. %u declarations in array.\n", wined3d_declaration, device->fvf_decl_count);
2914 return wined3d_declaration;
2917 static HRESULT WINAPI d3d9_device_SetFVF(IDirect3DDevice9Ex *iface, DWORD fvf)
2919 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2920 struct wined3d_vertex_declaration *decl;
2922 TRACE("iface %p, fvf %#x.\n", iface, fvf);
2924 if (!fvf)
2926 WARN("%#x is not a valid FVF.\n", fvf);
2927 return D3D_OK;
2930 wined3d_mutex_lock();
2931 if (!(decl = device_get_fvf_declaration(device, fvf)))
2933 wined3d_mutex_unlock();
2934 ERR("Failed to create a vertex declaration for fvf %#x.\n", fvf);
2935 return D3DERR_DRIVERINTERNALERROR;
2938 wined3d_device_set_vertex_declaration(device->wined3d_device, decl);
2939 device->has_vertex_declaration = TRUE;
2940 wined3d_mutex_unlock();
2942 return D3D_OK;
2945 static HRESULT WINAPI d3d9_device_GetFVF(IDirect3DDevice9Ex *iface, DWORD *fvf)
2947 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2948 struct wined3d_vertex_declaration *wined3d_declaration;
2949 struct d3d9_vertex_declaration *d3d9_declaration;
2951 TRACE("iface %p, fvf %p.\n", iface, fvf);
2953 wined3d_mutex_lock();
2954 if ((wined3d_declaration = wined3d_device_get_vertex_declaration(device->wined3d_device)))
2956 d3d9_declaration = wined3d_vertex_declaration_get_parent(wined3d_declaration);
2957 *fvf = d3d9_declaration->fvf;
2959 else
2961 *fvf = 0;
2963 wined3d_mutex_unlock();
2965 TRACE("Returning FVF %#x.\n", *fvf);
2967 return D3D_OK;
2970 static HRESULT WINAPI d3d9_device_CreateVertexShader(IDirect3DDevice9Ex *iface,
2971 const DWORD *byte_code, IDirect3DVertexShader9 **shader)
2973 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2974 struct d3d9_vertexshader *object;
2975 HRESULT hr;
2977 TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
2979 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2980 if (!object)
2981 return E_OUTOFMEMORY;
2983 hr = vertexshader_init(object, device, byte_code);
2984 if (FAILED(hr))
2986 WARN("Failed to initialize vertex shader, hr %#x.\n", hr);
2987 HeapFree(GetProcessHeap(), 0, object);
2988 return hr;
2991 TRACE("Created vertex shader %p.\n", object);
2992 *shader = &object->IDirect3DVertexShader9_iface;
2994 return D3D_OK;
2997 static HRESULT WINAPI d3d9_device_SetVertexShader(IDirect3DDevice9Ex *iface, IDirect3DVertexShader9 *shader)
2999 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3000 struct d3d9_vertexshader *shader_obj = unsafe_impl_from_IDirect3DVertexShader9(shader);
3002 TRACE("iface %p, shader %p.\n", iface, shader);
3004 wined3d_mutex_lock();
3005 wined3d_device_set_vertex_shader(device->wined3d_device,
3006 shader_obj ? shader_obj->wined3d_shader : NULL);
3007 wined3d_mutex_unlock();
3009 return D3D_OK;
3012 static HRESULT WINAPI d3d9_device_GetVertexShader(IDirect3DDevice9Ex *iface, IDirect3DVertexShader9 **shader)
3014 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3015 struct d3d9_vertexshader *shader_impl;
3016 struct wined3d_shader *wined3d_shader;
3018 TRACE("iface %p, shader %p.\n", iface, shader);
3020 wined3d_mutex_lock();
3021 if ((wined3d_shader = wined3d_device_get_vertex_shader(device->wined3d_device)))
3023 shader_impl = wined3d_shader_get_parent(wined3d_shader);
3024 *shader = &shader_impl->IDirect3DVertexShader9_iface;
3025 IDirect3DVertexShader9_AddRef(*shader);
3027 else
3029 *shader = NULL;
3031 wined3d_mutex_unlock();
3033 TRACE("Returning %p.\n", *shader);
3035 return D3D_OK;
3038 static HRESULT WINAPI d3d9_device_SetVertexShaderConstantF(IDirect3DDevice9Ex *iface,
3039 UINT reg_idx, const float *data, UINT count)
3041 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3042 HRESULT hr;
3044 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3046 if (reg_idx + count > D3D9_MAX_VERTEX_SHADER_CONSTANTF)
3048 WARN("Trying to access %u constants, but d3d9 only supports %u\n",
3049 reg_idx + count, D3D9_MAX_VERTEX_SHADER_CONSTANTF);
3050 return D3DERR_INVALIDCALL;
3053 wined3d_mutex_lock();
3054 hr = wined3d_device_set_vs_consts_f(device->wined3d_device,
3055 reg_idx, count, (const struct wined3d_vec4 *)data);
3056 wined3d_mutex_unlock();
3058 return hr;
3061 static HRESULT WINAPI d3d9_device_GetVertexShaderConstantF(IDirect3DDevice9Ex *iface,
3062 UINT reg_idx, float *data, UINT count)
3064 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3065 HRESULT hr;
3067 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3069 if (reg_idx + count > D3D9_MAX_VERTEX_SHADER_CONSTANTF)
3071 WARN("Trying to access %u constants, but d3d9 only supports %u\n",
3072 reg_idx + count, D3D9_MAX_VERTEX_SHADER_CONSTANTF);
3073 return D3DERR_INVALIDCALL;
3076 wined3d_mutex_lock();
3077 hr = wined3d_device_get_vs_consts_f(device->wined3d_device,
3078 reg_idx, count, (struct wined3d_vec4 *)data);
3079 wined3d_mutex_unlock();
3081 return hr;
3084 static HRESULT WINAPI d3d9_device_SetVertexShaderConstantI(IDirect3DDevice9Ex *iface,
3085 UINT reg_idx, const int *data, UINT count)
3087 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3088 HRESULT hr;
3090 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3092 wined3d_mutex_lock();
3093 hr = wined3d_device_set_vs_consts_i(device->wined3d_device,
3094 reg_idx, count, (const struct wined3d_ivec4 *)data);
3095 wined3d_mutex_unlock();
3097 return hr;
3100 static HRESULT WINAPI d3d9_device_GetVertexShaderConstantI(IDirect3DDevice9Ex *iface,
3101 UINT reg_idx, int *data, UINT count)
3103 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3104 HRESULT hr;
3106 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3108 wined3d_mutex_lock();
3109 hr = wined3d_device_get_vs_consts_i(device->wined3d_device,
3110 reg_idx, count, (struct wined3d_ivec4 *)data);
3111 wined3d_mutex_unlock();
3113 return hr;
3116 static HRESULT WINAPI d3d9_device_SetVertexShaderConstantB(IDirect3DDevice9Ex *iface,
3117 UINT reg_idx, const BOOL *data, UINT count)
3119 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3120 HRESULT hr;
3122 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3124 wined3d_mutex_lock();
3125 hr = wined3d_device_set_vs_consts_b(device->wined3d_device, reg_idx, count, data);
3126 wined3d_mutex_unlock();
3128 return hr;
3131 static HRESULT WINAPI d3d9_device_GetVertexShaderConstantB(IDirect3DDevice9Ex *iface,
3132 UINT reg_idx, BOOL *data, UINT count)
3134 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3135 HRESULT hr;
3137 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3139 wined3d_mutex_lock();
3140 hr = wined3d_device_get_vs_consts_b(device->wined3d_device, reg_idx, count, data);
3141 wined3d_mutex_unlock();
3143 return hr;
3146 static HRESULT WINAPI d3d9_device_SetStreamSource(IDirect3DDevice9Ex *iface,
3147 UINT stream_idx, IDirect3DVertexBuffer9 *buffer, UINT offset, UINT stride)
3149 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3150 struct d3d9_vertexbuffer *buffer_impl = unsafe_impl_from_IDirect3DVertexBuffer9(buffer);
3151 HRESULT hr;
3153 TRACE("iface %p, stream_idx %u, buffer %p, offset %u, stride %u.\n",
3154 iface, stream_idx, buffer, offset, stride);
3156 wined3d_mutex_lock();
3157 hr = wined3d_device_set_stream_source(device->wined3d_device, stream_idx,
3158 buffer_impl ? buffer_impl->wined3d_buffer : NULL, offset, stride);
3159 wined3d_mutex_unlock();
3161 return hr;
3164 static HRESULT WINAPI d3d9_device_GetStreamSource(IDirect3DDevice9Ex *iface,
3165 UINT stream_idx, IDirect3DVertexBuffer9 **buffer, UINT *offset, UINT *stride)
3167 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3168 struct d3d9_vertexbuffer *buffer_impl;
3169 struct wined3d_buffer *wined3d_buffer;
3170 HRESULT hr;
3172 TRACE("iface %p, stream_idx %u, buffer %p, offset %p, stride %p.\n",
3173 iface, stream_idx, buffer, offset, stride);
3175 if (!buffer)
3176 return D3DERR_INVALIDCALL;
3178 wined3d_mutex_lock();
3179 hr = wined3d_device_get_stream_source(device->wined3d_device, stream_idx, &wined3d_buffer, offset, stride);
3180 if (SUCCEEDED(hr) && wined3d_buffer)
3182 buffer_impl = wined3d_buffer_get_parent(wined3d_buffer);
3183 *buffer = &buffer_impl->IDirect3DVertexBuffer9_iface;
3184 IDirect3DVertexBuffer9_AddRef(*buffer);
3186 else
3188 if (FAILED(hr))
3189 FIXME("Call to GetStreamSource failed %p %p\n", offset, stride);
3190 *buffer = NULL;
3192 wined3d_mutex_unlock();
3194 return hr;
3197 static HRESULT WINAPI d3d9_device_SetStreamSourceFreq(IDirect3DDevice9Ex *iface, UINT stream_idx, UINT freq)
3199 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3200 HRESULT hr;
3202 TRACE("iface %p, stream_idx %u, freq %u.\n", iface, stream_idx, freq);
3204 wined3d_mutex_lock();
3205 hr = wined3d_device_set_stream_source_freq(device->wined3d_device, stream_idx, freq);
3206 wined3d_mutex_unlock();
3208 return hr;
3211 static HRESULT WINAPI d3d9_device_GetStreamSourceFreq(IDirect3DDevice9Ex *iface, UINT stream_idx, UINT *freq)
3213 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3214 HRESULT hr;
3216 TRACE("iface %p, stream_idx %u, freq %p.\n", iface, stream_idx, freq);
3218 wined3d_mutex_lock();
3219 hr = wined3d_device_get_stream_source_freq(device->wined3d_device, stream_idx, freq);
3220 wined3d_mutex_unlock();
3222 return hr;
3225 static HRESULT WINAPI d3d9_device_SetIndices(IDirect3DDevice9Ex *iface, IDirect3DIndexBuffer9 *buffer)
3227 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3228 struct d3d9_indexbuffer *ib = unsafe_impl_from_IDirect3DIndexBuffer9(buffer);
3230 TRACE("iface %p, buffer %p.\n", iface, buffer);
3232 wined3d_mutex_lock();
3233 wined3d_device_set_index_buffer(device->wined3d_device,
3234 ib ? ib->wined3d_buffer : NULL, ib ? ib->format : WINED3DFMT_UNKNOWN, 0);
3235 wined3d_mutex_unlock();
3237 return D3D_OK;
3240 static HRESULT WINAPI d3d9_device_GetIndices(IDirect3DDevice9Ex *iface, IDirect3DIndexBuffer9 **buffer)
3242 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3243 enum wined3d_format_id wined3d_format;
3244 struct wined3d_buffer *wined3d_buffer;
3245 struct d3d9_indexbuffer *buffer_impl;
3247 TRACE("iface %p, buffer %p.\n", iface, buffer);
3249 if (!buffer)
3250 return D3DERR_INVALIDCALL;
3252 wined3d_mutex_lock();
3253 if ((wined3d_buffer = wined3d_device_get_index_buffer(device->wined3d_device, &wined3d_format, NULL)))
3255 buffer_impl = wined3d_buffer_get_parent(wined3d_buffer);
3256 *buffer = &buffer_impl->IDirect3DIndexBuffer9_iface;
3257 IDirect3DIndexBuffer9_AddRef(*buffer);
3259 else
3261 *buffer = NULL;
3263 wined3d_mutex_unlock();
3265 return D3D_OK;
3268 static HRESULT WINAPI d3d9_device_CreatePixelShader(IDirect3DDevice9Ex *iface,
3269 const DWORD *byte_code, IDirect3DPixelShader9 **shader)
3271 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3272 struct d3d9_pixelshader *object;
3273 HRESULT hr;
3275 TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
3277 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
3278 if (!object)
3280 FIXME("Failed to allocate pixel shader memory.\n");
3281 return E_OUTOFMEMORY;
3284 hr = pixelshader_init(object, device, byte_code);
3285 if (FAILED(hr))
3287 WARN("Failed to initialize pixel shader, hr %#x.\n", hr);
3288 HeapFree(GetProcessHeap(), 0, object);
3289 return hr;
3292 TRACE("Created pixel shader %p.\n", object);
3293 *shader = &object->IDirect3DPixelShader9_iface;
3295 return D3D_OK;
3298 static HRESULT WINAPI d3d9_device_SetPixelShader(IDirect3DDevice9Ex *iface, IDirect3DPixelShader9 *shader)
3300 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3301 struct d3d9_pixelshader *shader_obj = unsafe_impl_from_IDirect3DPixelShader9(shader);
3303 TRACE("iface %p, shader %p.\n", iface, shader);
3305 wined3d_mutex_lock();
3306 wined3d_device_set_pixel_shader(device->wined3d_device,
3307 shader_obj ? shader_obj->wined3d_shader : NULL);
3308 wined3d_mutex_unlock();
3310 return D3D_OK;
3313 static HRESULT WINAPI d3d9_device_GetPixelShader(IDirect3DDevice9Ex *iface, IDirect3DPixelShader9 **shader)
3315 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3316 struct d3d9_pixelshader *shader_impl;
3317 struct wined3d_shader *wined3d_shader;
3319 TRACE("iface %p, shader %p.\n", iface, shader);
3321 if (!shader) return D3DERR_INVALIDCALL;
3323 wined3d_mutex_lock();
3324 if ((wined3d_shader = wined3d_device_get_pixel_shader(device->wined3d_device)))
3326 shader_impl = wined3d_shader_get_parent(wined3d_shader);
3327 *shader = &shader_impl->IDirect3DPixelShader9_iface;
3328 IDirect3DPixelShader9_AddRef(*shader);
3330 else
3332 *shader = NULL;
3334 wined3d_mutex_unlock();
3336 TRACE("Returning %p.\n", *shader);
3338 return D3D_OK;
3341 static HRESULT WINAPI d3d9_device_SetPixelShaderConstantF(IDirect3DDevice9Ex *iface,
3342 UINT reg_idx, const float *data, UINT count)
3344 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3345 HRESULT hr;
3347 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3349 wined3d_mutex_lock();
3350 hr = wined3d_device_set_ps_consts_f(device->wined3d_device,
3351 reg_idx, count, (const struct wined3d_vec4 *)data);
3352 wined3d_mutex_unlock();
3354 return hr;
3357 static HRESULT WINAPI d3d9_device_GetPixelShaderConstantF(IDirect3DDevice9Ex *iface,
3358 UINT reg_idx, float *data, UINT count)
3360 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3361 HRESULT hr;
3363 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3365 wined3d_mutex_lock();
3366 hr = wined3d_device_get_ps_consts_f(device->wined3d_device,
3367 reg_idx, count, (struct wined3d_vec4 *)data);
3368 wined3d_mutex_unlock();
3370 return hr;
3373 static HRESULT WINAPI d3d9_device_SetPixelShaderConstantI(IDirect3DDevice9Ex *iface,
3374 UINT reg_idx, const int *data, UINT count)
3376 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3377 HRESULT hr;
3379 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3381 wined3d_mutex_lock();
3382 hr = wined3d_device_set_ps_consts_i(device->wined3d_device,
3383 reg_idx, count, (const struct wined3d_ivec4 *)data);
3384 wined3d_mutex_unlock();
3386 return hr;
3389 static HRESULT WINAPI d3d9_device_GetPixelShaderConstantI(IDirect3DDevice9Ex *iface,
3390 UINT reg_idx, int *data, UINT count)
3392 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3393 HRESULT hr;
3395 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3397 wined3d_mutex_lock();
3398 hr = wined3d_device_get_ps_consts_i(device->wined3d_device,
3399 reg_idx, count, (struct wined3d_ivec4 *)data);
3400 wined3d_mutex_unlock();
3402 return hr;
3405 static HRESULT WINAPI d3d9_device_SetPixelShaderConstantB(IDirect3DDevice9Ex *iface,
3406 UINT reg_idx, const BOOL *data, UINT count)
3408 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3409 HRESULT hr;
3411 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3413 wined3d_mutex_lock();
3414 hr = wined3d_device_set_ps_consts_b(device->wined3d_device, reg_idx, count, data);
3415 wined3d_mutex_unlock();
3417 return hr;
3420 static HRESULT WINAPI d3d9_device_GetPixelShaderConstantB(IDirect3DDevice9Ex *iface,
3421 UINT reg_idx, BOOL *data, UINT count)
3423 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3424 HRESULT hr;
3426 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3428 wined3d_mutex_lock();
3429 hr = wined3d_device_get_ps_consts_b(device->wined3d_device, reg_idx, count, data);
3430 wined3d_mutex_unlock();
3432 return hr;
3435 static HRESULT WINAPI d3d9_device_DrawRectPatch(IDirect3DDevice9Ex *iface, UINT handle,
3436 const float *segment_count, const D3DRECTPATCH_INFO *patch_info)
3438 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
3439 iface, handle, segment_count, patch_info);
3440 return D3D_OK;
3443 static HRESULT WINAPI d3d9_device_DrawTriPatch(IDirect3DDevice9Ex *iface, UINT handle,
3444 const float *segment_count, const D3DTRIPATCH_INFO *patch_info)
3446 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
3447 iface, handle, segment_count, patch_info);
3448 return D3D_OK;
3451 static HRESULT WINAPI d3d9_device_DeletePatch(IDirect3DDevice9Ex *iface, UINT handle)
3453 FIXME("iface %p, handle %#x unimplemented.\n", iface, handle);
3454 return D3DERR_INVALIDCALL;
3457 static HRESULT WINAPI d3d9_device_CreateQuery(IDirect3DDevice9Ex *iface, D3DQUERYTYPE type, IDirect3DQuery9 **query)
3459 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3460 struct d3d9_query *object;
3461 HRESULT hr;
3463 TRACE("iface %p, type %#x, query %p.\n", iface, type, query);
3465 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
3466 if (!object)
3467 return E_OUTOFMEMORY;
3469 hr = query_init(object, device, type);
3470 if (FAILED(hr))
3472 WARN("Failed to initialize query, hr %#x.\n", hr);
3473 HeapFree(GetProcessHeap(), 0, object);
3474 return hr;
3477 TRACE("Created query %p.\n", object);
3478 if (query) *query = &object->IDirect3DQuery9_iface;
3479 else IDirect3DQuery9_Release(&object->IDirect3DQuery9_iface);
3481 return D3D_OK;
3484 static HRESULT WINAPI d3d9_device_SetConvolutionMonoKernel(IDirect3DDevice9Ex *iface,
3485 UINT width, UINT height, float *rows, float *columns)
3487 FIXME("iface %p, width %u, height %u, rows %p, columns %p stub!\n",
3488 iface, width, height, rows, columns);
3490 return E_NOTIMPL;
3493 static HRESULT WINAPI d3d9_device_ComposeRects(IDirect3DDevice9Ex *iface,
3494 IDirect3DSurface9 *src_surface, IDirect3DSurface9 *dst_surface, IDirect3DVertexBuffer9 *src_descs,
3495 UINT rect_count, IDirect3DVertexBuffer9 *dst_descs, D3DCOMPOSERECTSOP operation, INT offset_x, INT offset_y)
3497 FIXME("iface %p, src_surface %p, dst_surface %p, src_descs %p, rect_count %u, "
3498 "dst_descs %p, operation %#x, offset_x %u, offset_y %u stub!\n",
3499 iface, src_surface, dst_surface, src_descs, rect_count,
3500 dst_descs, operation, offset_x, offset_y);
3502 return E_NOTIMPL;
3505 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_PresentEx(IDirect3DDevice9Ex *iface,
3506 const RECT *src_rect, const RECT *dst_rect, HWND dst_window_override,
3507 const RGNDATA *dirty_region, DWORD flags)
3509 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3510 UINT i;
3511 HRESULT hr;
3513 TRACE("iface %p, src_rect %s, dst_rect %s, dst_window_override %p, dirty_region %p, flags %#x.\n",
3514 iface, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect),
3515 dst_window_override, dirty_region, flags);
3517 if (device->device_state != D3D9_DEVICE_STATE_OK)
3518 return S_PRESENT_OCCLUDED;
3520 if (dirty_region)
3521 FIXME("Ignoring dirty_region %p.\n", dirty_region);
3523 wined3d_mutex_lock();
3524 for (i = 0; i < device->implicit_swapchain_count; ++i)
3526 if (FAILED(hr = wined3d_swapchain_present(device->implicit_swapchains[i]->wined3d_swapchain,
3527 src_rect, dst_rect, dst_window_override, flags)))
3529 wined3d_mutex_unlock();
3530 return hr;
3533 wined3d_mutex_unlock();
3535 return D3D_OK;
3538 static HRESULT WINAPI d3d9_device_GetGPUThreadPriority(IDirect3DDevice9Ex *iface, INT *priority)
3540 FIXME("iface %p, priority %p stub!\n", iface, priority);
3542 return E_NOTIMPL;
3545 static HRESULT WINAPI d3d9_device_SetGPUThreadPriority(IDirect3DDevice9Ex *iface, INT priority)
3547 FIXME("iface %p, priority %d stub!\n", iface, priority);
3549 return E_NOTIMPL;
3552 static HRESULT WINAPI d3d9_device_WaitForVBlank(IDirect3DDevice9Ex *iface, UINT swapchain_idx)
3554 FIXME("iface %p, swapchain_idx %u stub!\n", iface, swapchain_idx);
3556 return E_NOTIMPL;
3559 static HRESULT WINAPI d3d9_device_CheckResourceResidency(IDirect3DDevice9Ex *iface,
3560 IDirect3DResource9 **resources, UINT32 resource_count)
3562 FIXME("iface %p, resources %p, resource_count %u stub!\n",
3563 iface, resources, resource_count);
3565 return E_NOTIMPL;
3568 static HRESULT WINAPI d3d9_device_SetMaximumFrameLatency(IDirect3DDevice9Ex *iface, UINT max_latency)
3570 TRACE("iface %p, max_latency %u.\n", iface, max_latency);
3572 if (max_latency)
3573 FIXME("Ignoring max_latency %u.\n", max_latency);
3575 return S_OK;
3578 static HRESULT WINAPI d3d9_device_GetMaximumFrameLatency(IDirect3DDevice9Ex *iface, UINT *max_latency)
3580 FIXME("iface %p, max_latency %p stub!\n", iface, max_latency);
3582 *max_latency = 2;
3584 return E_NOTIMPL;
3587 static HRESULT WINAPI d3d9_device_CheckDeviceState(IDirect3DDevice9Ex *iface, HWND dst_window)
3589 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3590 struct wined3d_swapchain_desc swapchain_desc;
3592 TRACE("iface %p, dst_window %p.\n", iface, dst_window);
3594 wined3d_mutex_lock();
3595 wined3d_swapchain_get_desc(device->implicit_swapchains[0]->wined3d_swapchain, &swapchain_desc);
3596 wined3d_mutex_unlock();
3598 if (swapchain_desc.windowed)
3599 return D3D_OK;
3601 /* FIXME: This is actually supposed to check if any other device is in
3602 * fullscreen mode. */
3603 if (dst_window != swapchain_desc.device_window)
3604 return device->device_state == D3D9_DEVICE_STATE_OK ? S_PRESENT_OCCLUDED : D3D_OK;
3606 return device->device_state == D3D9_DEVICE_STATE_OK ? D3D_OK : S_PRESENT_OCCLUDED;
3609 static HRESULT WINAPI d3d9_device_CreateRenderTargetEx(IDirect3DDevice9Ex *iface,
3610 UINT width, UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
3611 BOOL lockable, IDirect3DSurface9 **surface, HANDLE *shared_handle, DWORD usage)
3613 FIXME("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u, "
3614 "lockable %#x, surface %p, shared_handle %p, usage %#x stub!\n",
3615 iface, width, height, format, multisample_type, multisample_quality,
3616 lockable, surface, shared_handle, usage);
3618 *surface = NULL;
3619 if (shared_handle)
3620 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
3622 return E_NOTIMPL;
3625 static HRESULT WINAPI d3d9_device_CreateOffscreenPlainSurfaceEx(IDirect3DDevice9Ex *iface,
3626 UINT width, UINT height, D3DFORMAT format, D3DPOOL pool, IDirect3DSurface9 **surface,
3627 HANDLE *shared_handle, DWORD usage)
3629 FIXME("iface %p, width %u, height %u, format %#x, pool %#x, surface %p, shared_handle %p, usage %#x stub!\n",
3630 iface, width, height, format, pool, surface, shared_handle, usage);
3632 return E_NOTIMPL;
3635 static HRESULT WINAPI d3d9_device_CreateDepthStencilSurfaceEx(IDirect3DDevice9Ex *iface,
3636 UINT width, UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
3637 BOOL discard, IDirect3DSurface9 **surface, HANDLE *shared_handle, DWORD usage)
3639 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3640 DWORD flags = WINED3D_TEXTURE_CREATE_MAPPABLE;
3642 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u, "
3643 "discard %#x, surface %p, shared_handle %p, usage %#x.\n",
3644 iface, width, height, format, multisample_type, multisample_quality,
3645 discard, surface, shared_handle, usage);
3647 if (usage & D3DUSAGE_DEPTHSTENCIL)
3649 WARN("Invalid usage %#x.\n", usage);
3650 return D3DERR_INVALIDCALL;
3653 if (shared_handle)
3654 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
3656 if (discard)
3657 flags |= WINED3D_TEXTURE_CREATE_DISCARD;
3659 *surface = NULL;
3660 return d3d9_device_create_surface(device, width, height, format, flags, surface,
3661 D3DUSAGE_DEPTHSTENCIL | usage, D3DPOOL_DEFAULT, multisample_type, multisample_quality, NULL);
3664 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_ResetEx(IDirect3DDevice9Ex *iface,
3665 D3DPRESENT_PARAMETERS *present_parameters, D3DDISPLAYMODEEX *mode)
3667 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3669 TRACE("iface %p, present_parameters %p, mode %p.\n", iface, present_parameters, mode);
3671 if (!present_parameters->Windowed == !mode)
3673 WARN("Mode can be passed if and only if Windowed is FALSE.\n");
3674 return D3DERR_INVALIDCALL;
3677 if (mode && (mode->Width != present_parameters->BackBufferWidth
3678 || mode->Height != present_parameters->BackBufferHeight))
3680 WARN("Mode and back buffer mismatch (mode %ux%u, backbuffer %ux%u).\n",
3681 mode->Width, mode->Height,
3682 present_parameters->BackBufferWidth, present_parameters->BackBufferHeight);
3683 return D3DERR_INVALIDCALL;
3686 return d3d9_device_reset(device, present_parameters, mode);
3689 static HRESULT WINAPI d3d9_device_GetDisplayModeEx(IDirect3DDevice9Ex *iface,
3690 UINT swapchain_idx, D3DDISPLAYMODEEX *mode, D3DDISPLAYROTATION *rotation)
3692 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3693 struct wined3d_display_mode wined3d_mode;
3694 HRESULT hr;
3696 TRACE("iface %p, swapchain_idx %u, mode %p, rotation %p.\n",
3697 iface, swapchain_idx, mode, rotation);
3699 if (mode->Size != sizeof(*mode))
3700 return D3DERR_INVALIDCALL;
3702 wined3d_mutex_lock();
3703 hr = wined3d_device_get_display_mode(device->wined3d_device, swapchain_idx, &wined3d_mode,
3704 (enum wined3d_display_rotation *)rotation);
3705 wined3d_mutex_unlock();
3707 if (SUCCEEDED(hr))
3709 mode->Width = wined3d_mode.width;
3710 mode->Height = wined3d_mode.height;
3711 mode->RefreshRate = wined3d_mode.refresh_rate;
3712 mode->Format = d3dformat_from_wined3dformat(wined3d_mode.format_id);
3713 mode->ScanLineOrdering = wined3d_mode.scanline_ordering;
3716 return hr;
3719 static const struct IDirect3DDevice9ExVtbl d3d9_device_vtbl =
3721 /* IUnknown */
3722 d3d9_device_QueryInterface,
3723 d3d9_device_AddRef,
3724 d3d9_device_Release,
3725 /* IDirect3DDevice9 */
3726 d3d9_device_TestCooperativeLevel,
3727 d3d9_device_GetAvailableTextureMem,
3728 d3d9_device_EvictManagedResources,
3729 d3d9_device_GetDirect3D,
3730 d3d9_device_GetDeviceCaps,
3731 d3d9_device_GetDisplayMode,
3732 d3d9_device_GetCreationParameters,
3733 d3d9_device_SetCursorProperties,
3734 d3d9_device_SetCursorPosition,
3735 d3d9_device_ShowCursor,
3736 d3d9_device_CreateAdditionalSwapChain,
3737 d3d9_device_GetSwapChain,
3738 d3d9_device_GetNumberOfSwapChains,
3739 d3d9_device_Reset,
3740 d3d9_device_Present,
3741 d3d9_device_GetBackBuffer,
3742 d3d9_device_GetRasterStatus,
3743 d3d9_device_SetDialogBoxMode,
3744 d3d9_device_SetGammaRamp,
3745 d3d9_device_GetGammaRamp,
3746 d3d9_device_CreateTexture,
3747 d3d9_device_CreateVolumeTexture,
3748 d3d9_device_CreateCubeTexture,
3749 d3d9_device_CreateVertexBuffer,
3750 d3d9_device_CreateIndexBuffer,
3751 d3d9_device_CreateRenderTarget,
3752 d3d9_device_CreateDepthStencilSurface,
3753 d3d9_device_UpdateSurface,
3754 d3d9_device_UpdateTexture,
3755 d3d9_device_GetRenderTargetData,
3756 d3d9_device_GetFrontBufferData,
3757 d3d9_device_StretchRect,
3758 d3d9_device_ColorFill,
3759 d3d9_device_CreateOffscreenPlainSurface,
3760 d3d9_device_SetRenderTarget,
3761 d3d9_device_GetRenderTarget,
3762 d3d9_device_SetDepthStencilSurface,
3763 d3d9_device_GetDepthStencilSurface,
3764 d3d9_device_BeginScene,
3765 d3d9_device_EndScene,
3766 d3d9_device_Clear,
3767 d3d9_device_SetTransform,
3768 d3d9_device_GetTransform,
3769 d3d9_device_MultiplyTransform,
3770 d3d9_device_SetViewport,
3771 d3d9_device_GetViewport,
3772 d3d9_device_SetMaterial,
3773 d3d9_device_GetMaterial,
3774 d3d9_device_SetLight,
3775 d3d9_device_GetLight,
3776 d3d9_device_LightEnable,
3777 d3d9_device_GetLightEnable,
3778 d3d9_device_SetClipPlane,
3779 d3d9_device_GetClipPlane,
3780 d3d9_device_SetRenderState,
3781 d3d9_device_GetRenderState,
3782 d3d9_device_CreateStateBlock,
3783 d3d9_device_BeginStateBlock,
3784 d3d9_device_EndStateBlock,
3785 d3d9_device_SetClipStatus,
3786 d3d9_device_GetClipStatus,
3787 d3d9_device_GetTexture,
3788 d3d9_device_SetTexture,
3789 d3d9_device_GetTextureStageState,
3790 d3d9_device_SetTextureStageState,
3791 d3d9_device_GetSamplerState,
3792 d3d9_device_SetSamplerState,
3793 d3d9_device_ValidateDevice,
3794 d3d9_device_SetPaletteEntries,
3795 d3d9_device_GetPaletteEntries,
3796 d3d9_device_SetCurrentTexturePalette,
3797 d3d9_device_GetCurrentTexturePalette,
3798 d3d9_device_SetScissorRect,
3799 d3d9_device_GetScissorRect,
3800 d3d9_device_SetSoftwareVertexProcessing,
3801 d3d9_device_GetSoftwareVertexProcessing,
3802 d3d9_device_SetNPatchMode,
3803 d3d9_device_GetNPatchMode,
3804 d3d9_device_DrawPrimitive,
3805 d3d9_device_DrawIndexedPrimitive,
3806 d3d9_device_DrawPrimitiveUP,
3807 d3d9_device_DrawIndexedPrimitiveUP,
3808 d3d9_device_ProcessVertices,
3809 d3d9_device_CreateVertexDeclaration,
3810 d3d9_device_SetVertexDeclaration,
3811 d3d9_device_GetVertexDeclaration,
3812 d3d9_device_SetFVF,
3813 d3d9_device_GetFVF,
3814 d3d9_device_CreateVertexShader,
3815 d3d9_device_SetVertexShader,
3816 d3d9_device_GetVertexShader,
3817 d3d9_device_SetVertexShaderConstantF,
3818 d3d9_device_GetVertexShaderConstantF,
3819 d3d9_device_SetVertexShaderConstantI,
3820 d3d9_device_GetVertexShaderConstantI,
3821 d3d9_device_SetVertexShaderConstantB,
3822 d3d9_device_GetVertexShaderConstantB,
3823 d3d9_device_SetStreamSource,
3824 d3d9_device_GetStreamSource,
3825 d3d9_device_SetStreamSourceFreq,
3826 d3d9_device_GetStreamSourceFreq,
3827 d3d9_device_SetIndices,
3828 d3d9_device_GetIndices,
3829 d3d9_device_CreatePixelShader,
3830 d3d9_device_SetPixelShader,
3831 d3d9_device_GetPixelShader,
3832 d3d9_device_SetPixelShaderConstantF,
3833 d3d9_device_GetPixelShaderConstantF,
3834 d3d9_device_SetPixelShaderConstantI,
3835 d3d9_device_GetPixelShaderConstantI,
3836 d3d9_device_SetPixelShaderConstantB,
3837 d3d9_device_GetPixelShaderConstantB,
3838 d3d9_device_DrawRectPatch,
3839 d3d9_device_DrawTriPatch,
3840 d3d9_device_DeletePatch,
3841 d3d9_device_CreateQuery,
3842 /* IDirect3DDevice9Ex */
3843 d3d9_device_SetConvolutionMonoKernel,
3844 d3d9_device_ComposeRects,
3845 d3d9_device_PresentEx,
3846 d3d9_device_GetGPUThreadPriority,
3847 d3d9_device_SetGPUThreadPriority,
3848 d3d9_device_WaitForVBlank,
3849 d3d9_device_CheckResourceResidency,
3850 d3d9_device_SetMaximumFrameLatency,
3851 d3d9_device_GetMaximumFrameLatency,
3852 d3d9_device_CheckDeviceState,
3853 d3d9_device_CreateRenderTargetEx,
3854 d3d9_device_CreateOffscreenPlainSurfaceEx,
3855 d3d9_device_CreateDepthStencilSurfaceEx,
3856 d3d9_device_ResetEx,
3857 d3d9_device_GetDisplayModeEx,
3860 static inline struct d3d9_device *device_from_device_parent(struct wined3d_device_parent *device_parent)
3862 return CONTAINING_RECORD(device_parent, struct d3d9_device, device_parent);
3865 static void CDECL device_parent_wined3d_device_created(struct wined3d_device_parent *device_parent,
3866 struct wined3d_device *device)
3868 TRACE("device_parent %p, device %p.\n", device_parent, device);
3871 static void CDECL device_parent_mode_changed(struct wined3d_device_parent *device_parent)
3873 TRACE("device_parent %p.\n", device_parent);
3876 static void CDECL device_parent_activate(struct wined3d_device_parent *device_parent, BOOL activate)
3878 struct d3d9_device *device = device_from_device_parent(device_parent);
3880 TRACE("device_parent %p, activate %#x.\n", device_parent, activate);
3882 if (!device->d3d_parent)
3883 return;
3885 if (!activate)
3886 InterlockedCompareExchange(&device->device_state, D3D9_DEVICE_STATE_LOST, D3D9_DEVICE_STATE_OK);
3887 else if (device->d3d_parent->extended)
3888 InterlockedCompareExchange(&device->device_state, D3D9_DEVICE_STATE_OK, D3D9_DEVICE_STATE_LOST);
3889 else
3890 InterlockedCompareExchange(&device->device_state, D3D9_DEVICE_STATE_NOT_RESET, D3D9_DEVICE_STATE_LOST);
3893 static HRESULT CDECL device_parent_surface_created(struct wined3d_device_parent *device_parent,
3894 struct wined3d_texture *wined3d_texture, unsigned int sub_resource_idx,
3895 void **parent, const struct wined3d_parent_ops **parent_ops)
3897 struct d3d9_surface *d3d_surface;
3899 TRACE("device_parent %p, wined3d_texture %p, sub_resource_idx %u, parent %p, parent_ops %p.\n",
3900 device_parent, wined3d_texture, sub_resource_idx, parent, parent_ops);
3902 if (!(d3d_surface = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*d3d_surface))))
3903 return E_OUTOFMEMORY;
3905 surface_init(d3d_surface, wined3d_texture, sub_resource_idx, parent_ops);
3906 *parent = d3d_surface;
3907 TRACE("Created surface %p.\n", d3d_surface);
3909 return D3D_OK;
3912 static HRESULT CDECL device_parent_volume_created(struct wined3d_device_parent *device_parent,
3913 struct wined3d_texture *wined3d_texture, unsigned int sub_resource_idx,
3914 void **parent, const struct wined3d_parent_ops **parent_ops)
3916 struct d3d9_volume *d3d_volume;
3918 TRACE("device_parent %p, texture %p, sub_resource_idx %u, parent %p, parent_ops %p.\n",
3919 device_parent, wined3d_texture, sub_resource_idx, parent, parent_ops);
3921 if (!(d3d_volume = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*d3d_volume))))
3922 return E_OUTOFMEMORY;
3924 volume_init(d3d_volume, wined3d_texture, sub_resource_idx, parent_ops);
3925 *parent = d3d_volume;
3926 TRACE("Created volume %p.\n", d3d_volume);
3928 return D3D_OK;
3931 static HRESULT CDECL device_parent_create_swapchain_texture(struct wined3d_device_parent *device_parent,
3932 void *container_parent, const struct wined3d_resource_desc *desc, DWORD texture_flags,
3933 struct wined3d_texture **texture)
3935 struct d3d9_device *device = device_from_device_parent(device_parent);
3936 struct d3d9_surface *d3d_surface;
3937 HRESULT hr;
3939 TRACE("device_parent %p, container_parent %p, desc %p, texture flags %#x, texture %p.\n",
3940 device_parent, container_parent, desc, texture_flags, texture);
3942 if (container_parent == device_parent)
3943 container_parent = &device->IDirect3DDevice9Ex_iface;
3945 if (is_gdi_compat_wined3dformat(desc->format))
3946 texture_flags |= WINED3D_TEXTURE_CREATE_GET_DC;
3948 if (FAILED(hr = wined3d_texture_create(device->wined3d_device, desc, 1, 1,
3949 texture_flags | WINED3D_TEXTURE_CREATE_MAPPABLE, NULL, container_parent,
3950 &d3d9_null_wined3d_parent_ops, texture)))
3952 WARN("Failed to create texture, hr %#x.\n", hr);
3953 return hr;
3956 d3d_surface = wined3d_texture_get_sub_resource_parent(*texture, 0);
3957 d3d_surface->parent_device = &device->IDirect3DDevice9Ex_iface;
3959 return hr;
3962 static HRESULT CDECL device_parent_create_swapchain(struct wined3d_device_parent *device_parent,
3963 struct wined3d_swapchain_desc *desc, struct wined3d_swapchain **swapchain)
3965 struct d3d9_device *device = device_from_device_parent(device_parent);
3966 struct d3d9_swapchain *d3d_swapchain;
3967 HRESULT hr;
3969 TRACE("device_parent %p, desc %p, swapchain %p\n", device_parent, desc, swapchain);
3971 hr = d3d9_swapchain_create(device, desc, &d3d_swapchain);
3972 if (FAILED(hr))
3974 WARN("Failed to create swapchain, hr %#x.\n", hr);
3975 *swapchain = NULL;
3976 return hr;
3979 *swapchain = d3d_swapchain->wined3d_swapchain;
3980 wined3d_swapchain_incref(*swapchain);
3981 IDirect3DSwapChain9Ex_Release(&d3d_swapchain->IDirect3DSwapChain9Ex_iface);
3983 return hr;
3986 static const struct wined3d_device_parent_ops d3d9_wined3d_device_parent_ops =
3988 device_parent_wined3d_device_created,
3989 device_parent_mode_changed,
3990 device_parent_activate,
3991 device_parent_surface_created,
3992 device_parent_volume_created,
3993 device_parent_create_swapchain_texture,
3994 device_parent_create_swapchain,
3997 static void setup_fpu(void)
3999 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
4000 WORD cw;
4001 __asm__ volatile ("fnstcw %0" : "=m" (cw));
4002 cw = (cw & ~0xf3f) | 0x3f;
4003 __asm__ volatile ("fldcw %0" : : "m" (cw));
4004 #elif defined(__i386__) && defined(_MSC_VER)
4005 WORD cw;
4006 __asm fnstcw cw;
4007 cw = (cw & ~0xf3f) | 0x3f;
4008 __asm fldcw cw;
4009 #else
4010 FIXME("FPU setup not implemented for this platform.\n");
4011 #endif
4014 HRESULT device_init(struct d3d9_device *device, struct d3d9 *parent, struct wined3d *wined3d,
4015 UINT adapter, D3DDEVTYPE device_type, HWND focus_window, DWORD flags,
4016 D3DPRESENT_PARAMETERS *parameters, D3DDISPLAYMODEEX *mode)
4018 struct wined3d_swapchain_desc *swapchain_desc;
4019 unsigned i, count = 1;
4020 WINED3DCAPS caps;
4021 HRESULT hr;
4023 if (mode)
4024 FIXME("Ignoring display mode.\n");
4026 device->IDirect3DDevice9Ex_iface.lpVtbl = &d3d9_device_vtbl;
4027 device->device_parent.ops = &d3d9_wined3d_device_parent_ops;
4028 device->refcount = 1;
4030 if (!(flags & D3DCREATE_FPU_PRESERVE)) setup_fpu();
4032 wined3d_mutex_lock();
4033 if (FAILED(hr = wined3d_device_create(wined3d, adapter, device_type, focus_window, flags, 4,
4034 &device->device_parent, &device->wined3d_device)))
4036 WARN("Failed to create wined3d device, hr %#x.\n", hr);
4037 wined3d_mutex_unlock();
4038 return hr;
4041 wined3d_get_device_caps(wined3d, adapter, device_type, &caps);
4042 device->max_user_clip_planes = caps.MaxUserClipPlanes;
4043 if (flags & D3DCREATE_ADAPTERGROUP_DEVICE)
4044 count = caps.NumberOfAdaptersInGroup;
4046 if (flags & D3DCREATE_MULTITHREADED)
4047 wined3d_device_set_multithreaded(device->wined3d_device);
4049 if (!parameters->Windowed)
4051 if (!focus_window)
4052 focus_window = parameters->hDeviceWindow;
4053 if (FAILED(hr = wined3d_device_acquire_focus_window(device->wined3d_device, focus_window)))
4055 ERR("Failed to acquire focus window, hr %#x.\n", hr);
4056 wined3d_device_decref(device->wined3d_device);
4057 wined3d_mutex_unlock();
4058 return hr;
4061 for (i = 0; i < count; ++i)
4063 HWND device_window = parameters[i].hDeviceWindow;
4065 if (!device_window) device_window = focus_window;
4066 wined3d_device_setup_fullscreen_window(device->wined3d_device, device_window,
4067 parameters[i].BackBufferWidth,
4068 parameters[i].BackBufferHeight);
4072 swapchain_desc = HeapAlloc(GetProcessHeap(), 0, sizeof(*swapchain_desc) * count);
4073 if (!swapchain_desc)
4075 ERR("Failed to allocate wined3d parameters.\n");
4076 wined3d_device_release_focus_window(device->wined3d_device);
4077 wined3d_device_decref(device->wined3d_device);
4078 wined3d_mutex_unlock();
4079 return E_OUTOFMEMORY;
4082 for (i = 0; i < count; ++i)
4084 if (!wined3d_swapchain_desc_from_present_parameters(&swapchain_desc[i], &parameters[i],
4085 parent->extended))
4087 wined3d_device_release_focus_window(device->wined3d_device);
4088 wined3d_device_decref(device->wined3d_device);
4089 HeapFree(GetProcessHeap(), 0, swapchain_desc);
4090 wined3d_mutex_unlock();
4091 return D3DERR_INVALIDCALL;
4095 if (FAILED(hr = wined3d_device_init_3d(device->wined3d_device, swapchain_desc)))
4097 WARN("Failed to initialize 3D, hr %#x.\n", hr);
4098 wined3d_device_release_focus_window(device->wined3d_device);
4099 HeapFree(GetProcessHeap(), 0, swapchain_desc);
4100 wined3d_device_decref(device->wined3d_device);
4101 wined3d_mutex_unlock();
4102 return hr;
4105 wined3d_device_set_render_state(device->wined3d_device,
4106 WINED3D_RS_ZENABLE, !!swapchain_desc->enable_auto_depth_stencil);
4108 if (FAILED(hr = d3d9_device_get_swapchains(device)))
4110 wined3d_device_uninit_3d(device->wined3d_device);
4111 wined3d_device_release_focus_window(device->wined3d_device);
4112 wined3d_device_decref(device->wined3d_device);
4113 wined3d_mutex_unlock();
4114 return E_OUTOFMEMORY;
4117 for (i = 0; i < count; ++i)
4119 present_parameters_from_wined3d_swapchain_desc(&parameters[i], &swapchain_desc[i]);
4122 wined3d_mutex_unlock();
4124 HeapFree(GetProcessHeap(), 0, swapchain_desc);
4126 /* Initialize the converted declaration array. This creates a valid pointer
4127 * and when adding decls HeapReAlloc() can be used without further checking. */
4128 device->fvf_decls = HeapAlloc(GetProcessHeap(), 0, 0);
4129 if (!device->fvf_decls)
4131 ERR("Failed to allocate FVF vertex declaration map memory.\n");
4132 wined3d_mutex_lock();
4133 HeapFree(GetProcessHeap(), 0, device->implicit_swapchains);
4134 wined3d_device_uninit_3d(device->wined3d_device);
4135 wined3d_device_release_focus_window(device->wined3d_device);
4136 wined3d_device_decref(device->wined3d_device);
4137 wined3d_mutex_unlock();
4138 return E_OUTOFMEMORY;
4141 IDirect3D9Ex_AddRef(&parent->IDirect3D9Ex_iface);
4142 device->d3d_parent = parent;
4144 return D3D_OK;