opengl32: Update to the current OpenGL spec.
[wine.git] / dlls / d3d9 / device.c
blob51b4896353d8f913b1803b5997a9737df8ae7a11
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 BOOL extended = device->d3d_parent->extended;
816 struct wined3d_swapchain_desc swapchain_desc;
817 struct wined3d_display_mode wined3d_mode;
818 HRESULT hr;
821 if (!extended && device->device_state == D3D9_DEVICE_STATE_LOST)
823 WARN("App not active, returning D3DERR_DEVICELOST.\n");
824 return D3DERR_DEVICELOST;
827 if (mode)
829 wined3d_mode.width = mode->Width;
830 wined3d_mode.height = mode->Height;
831 wined3d_mode.refresh_rate = mode->RefreshRate;
832 wined3d_mode.format_id = wined3dformat_from_d3dformat(mode->Format);
833 wined3d_mode.scanline_ordering = mode->ScanLineOrdering;
836 if (!wined3d_swapchain_desc_from_present_parameters(&swapchain_desc, present_parameters, extended))
837 return D3DERR_INVALIDCALL;
839 wined3d_mutex_lock();
841 if (device->vertex_buffer)
843 wined3d_buffer_decref(device->vertex_buffer);
844 device->vertex_buffer = NULL;
845 device->vertex_buffer_size = 0;
848 if (device->index_buffer)
850 wined3d_buffer_decref(device->index_buffer);
851 device->index_buffer = NULL;
852 device->index_buffer_size = 0;
855 if (SUCCEEDED(hr = wined3d_device_reset(device->wined3d_device, &swapchain_desc,
856 mode ? &wined3d_mode : NULL, reset_enum_callback, !extended)))
858 HeapFree(GetProcessHeap(), 0, device->implicit_swapchains);
860 if (!extended)
862 wined3d_device_set_render_state(device->wined3d_device, WINED3D_RS_ZENABLE,
863 !!swapchain_desc.enable_auto_depth_stencil);
866 if (FAILED(hr = d3d9_device_get_swapchains(device)))
868 device->device_state = D3D9_DEVICE_STATE_NOT_RESET;
870 else
872 wined3d_swapchain_get_desc(device->implicit_swapchains[0]->wined3d_swapchain, &swapchain_desc);
873 present_parameters->BackBufferWidth = swapchain_desc.backbuffer_width;
874 present_parameters->BackBufferHeight = swapchain_desc.backbuffer_height;
875 present_parameters->BackBufferFormat = d3dformat_from_wined3dformat(swapchain_desc.backbuffer_format);
876 present_parameters->BackBufferCount = swapchain_desc.backbuffer_count;
878 device->device_state = D3D9_DEVICE_STATE_OK;
881 else if (!extended)
883 device->device_state = D3D9_DEVICE_STATE_NOT_RESET;
886 wined3d_mutex_unlock();
888 return hr;
891 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_Reset(IDirect3DDevice9Ex *iface,
892 D3DPRESENT_PARAMETERS *present_parameters)
894 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
896 TRACE("iface %p, present_parameters %p.\n", iface, present_parameters);
898 return d3d9_device_reset(device, present_parameters, NULL);
901 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_Present(IDirect3DDevice9Ex *iface,
902 const RECT *src_rect, const RECT *dst_rect, HWND dst_window_override, const RGNDATA *dirty_region)
904 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
905 UINT i;
906 HRESULT hr;
908 TRACE("iface %p, src_rect %p, dst_rect %p, dst_window_override %p, dirty_region %p.\n",
909 iface, src_rect, dst_rect, dst_window_override, dirty_region);
911 if (device->device_state != D3D9_DEVICE_STATE_OK)
912 return device->d3d_parent->extended ? S_PRESENT_OCCLUDED : D3DERR_DEVICELOST;
914 if (dirty_region)
915 FIXME("Ignoring dirty_region %p.\n", dirty_region);
917 wined3d_mutex_lock();
918 for (i = 0; i < device->implicit_swapchain_count; ++i)
920 if (FAILED(hr = wined3d_swapchain_present(device->implicit_swapchains[i]->wined3d_swapchain,
921 src_rect, dst_rect, dst_window_override, 0)))
923 wined3d_mutex_unlock();
924 return hr;
927 wined3d_mutex_unlock();
929 return D3D_OK;
932 static HRESULT WINAPI d3d9_device_GetBackBuffer(IDirect3DDevice9Ex *iface, UINT swapchain,
933 UINT backbuffer_idx, D3DBACKBUFFER_TYPE backbuffer_type, IDirect3DSurface9 **backbuffer)
935 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
936 HRESULT hr;
938 TRACE("iface %p, swapchain %u, backbuffer_idx %u, backbuffer_type %#x, backbuffer %p.\n",
939 iface, swapchain, backbuffer_idx, backbuffer_type, backbuffer);
941 /* backbuffer_type is ignored by native. */
943 /* No need to check for backbuffer == NULL, Windows crashes in that case. */
944 *backbuffer = NULL;
946 wined3d_mutex_lock();
947 if (swapchain >= device->implicit_swapchain_count)
949 wined3d_mutex_unlock();
950 WARN("Swapchain index %u is out of range, returning D3DERR_INVALIDCALL.\n", swapchain);
951 return D3DERR_INVALIDCALL;
954 hr = IDirect3DSwapChain9Ex_GetBackBuffer(&device->implicit_swapchains[swapchain]->IDirect3DSwapChain9Ex_iface,
955 backbuffer_idx, backbuffer_type, backbuffer);
956 wined3d_mutex_unlock();
958 return hr;
961 static HRESULT WINAPI d3d9_device_GetRasterStatus(IDirect3DDevice9Ex *iface,
962 UINT swapchain, D3DRASTER_STATUS *raster_status)
964 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
965 HRESULT hr;
967 TRACE("iface %p, swapchain %u, raster_status %p.\n", iface, swapchain, raster_status);
969 wined3d_mutex_lock();
970 hr = wined3d_device_get_raster_status(device->wined3d_device,
971 swapchain, (struct wined3d_raster_status *)raster_status);
972 wined3d_mutex_unlock();
974 return hr;
977 static HRESULT WINAPI d3d9_device_SetDialogBoxMode(IDirect3DDevice9Ex *iface, BOOL enable)
979 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
980 HRESULT hr;
982 TRACE("iface %p, enable %#x.\n", iface, enable);
984 wined3d_mutex_lock();
985 hr = wined3d_device_set_dialog_box_mode(device->wined3d_device, enable);
986 wined3d_mutex_unlock();
988 return hr;
991 static void WINAPI d3d9_device_SetGammaRamp(IDirect3DDevice9Ex *iface,
992 UINT swapchain, DWORD flags, const D3DGAMMARAMP *ramp)
994 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
996 TRACE("iface %p, swapchain %u, flags %#x, ramp %p.\n", iface, swapchain, flags, ramp);
998 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
999 wined3d_mutex_lock();
1000 wined3d_device_set_gamma_ramp(device->wined3d_device, swapchain, flags, (const struct wined3d_gamma_ramp *)ramp);
1001 wined3d_mutex_unlock();
1004 static void WINAPI d3d9_device_GetGammaRamp(IDirect3DDevice9Ex *iface, UINT swapchain, D3DGAMMARAMP *ramp)
1006 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1008 TRACE("iface %p, swapchain %u, ramp %p.\n", iface, swapchain, ramp);
1010 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
1011 wined3d_mutex_lock();
1012 wined3d_device_get_gamma_ramp(device->wined3d_device, swapchain, (struct wined3d_gamma_ramp *)ramp);
1013 wined3d_mutex_unlock();
1016 static HRESULT WINAPI d3d9_device_CreateTexture(IDirect3DDevice9Ex *iface,
1017 UINT width, UINT height, UINT levels, DWORD usage, D3DFORMAT format,
1018 D3DPOOL pool, IDirect3DTexture9 **texture, HANDLE *shared_handle)
1020 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1021 struct d3d9_texture *object;
1022 BOOL set_mem = FALSE;
1023 HRESULT hr;
1025 TRACE("iface %p, width %u, height %u, levels %u, usage %#x, format %#x, pool %#x, texture %p, shared_handle %p.\n",
1026 iface, width, height, levels, usage, format, pool, texture, shared_handle);
1028 *texture = NULL;
1029 if (shared_handle)
1031 if (!device->d3d_parent->extended)
1033 WARN("Trying to create a shared or user memory texture on a non-ex device.\n");
1034 return E_NOTIMPL;
1037 if (pool == D3DPOOL_SYSTEMMEM)
1039 if (levels != 1)
1040 return D3DERR_INVALIDCALL;
1041 set_mem = TRUE;
1043 else
1045 if (pool != D3DPOOL_DEFAULT)
1047 WARN("Trying to create a shared texture in pool %#x.\n", pool);
1048 return D3DERR_INVALIDCALL;
1050 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1054 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1055 if (!object)
1056 return D3DERR_OUTOFVIDEOMEMORY;
1058 hr = texture_init(object, device, width, height, levels, usage, format, pool);
1059 if (FAILED(hr))
1061 WARN("Failed to initialize texture, hr %#x.\n", hr);
1062 HeapFree(GetProcessHeap(), 0, object);
1063 return hr;
1066 if (set_mem)
1067 wined3d_texture_update_desc(object->wined3d_texture, width, height,
1068 wined3dformat_from_d3dformat(format), WINED3D_MULTISAMPLE_NONE, 0,
1069 *shared_handle, 0);
1071 TRACE("Created texture %p.\n", object);
1072 *texture = (IDirect3DTexture9 *)&object->IDirect3DBaseTexture9_iface;
1074 return D3D_OK;
1077 static HRESULT WINAPI d3d9_device_CreateVolumeTexture(IDirect3DDevice9Ex *iface,
1078 UINT width, UINT height, UINT depth, UINT levels, DWORD usage, D3DFORMAT format,
1079 D3DPOOL pool, IDirect3DVolumeTexture9 **texture, HANDLE *shared_handle)
1081 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1082 struct d3d9_texture *object;
1083 HRESULT hr;
1085 TRACE("iface %p, width %u, height %u, depth %u, levels %u, "
1086 "usage %#x, format %#x, pool %#x, texture %p, shared_handle %p.\n",
1087 iface, width, height, depth, levels,
1088 usage, format, pool, texture, shared_handle);
1090 *texture = NULL;
1091 if (shared_handle)
1093 if (!device->d3d_parent->extended)
1095 WARN("Trying to create a shared volume texture on a non-ex device.\n");
1096 return E_NOTIMPL;
1099 if (pool != D3DPOOL_DEFAULT)
1101 WARN("Trying to create a shared volume texture in pool %#x.\n", pool);
1102 return D3DERR_INVALIDCALL;
1104 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1107 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1108 if (!object)
1109 return D3DERR_OUTOFVIDEOMEMORY;
1111 hr = volumetexture_init(object, device, width, height, depth, levels, usage, format, pool);
1112 if (FAILED(hr))
1114 WARN("Failed to initialize volume texture, hr %#x.\n", hr);
1115 HeapFree(GetProcessHeap(), 0, object);
1116 return hr;
1119 TRACE("Created volume texture %p.\n", object);
1120 *texture = (IDirect3DVolumeTexture9 *)&object->IDirect3DBaseTexture9_iface;
1122 return D3D_OK;
1125 static HRESULT WINAPI d3d9_device_CreateCubeTexture(IDirect3DDevice9Ex *iface,
1126 UINT edge_length, UINT levels, DWORD usage, D3DFORMAT format, D3DPOOL pool,
1127 IDirect3DCubeTexture9 **texture, HANDLE *shared_handle)
1129 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1130 struct d3d9_texture *object;
1131 HRESULT hr;
1133 TRACE("iface %p, edge_length %u, levels %u, usage %#x, format %#x, pool %#x, texture %p, shared_handle %p.\n",
1134 iface, edge_length, levels, usage, format, pool, texture, shared_handle);
1136 *texture = NULL;
1137 if (shared_handle)
1139 if (!device->d3d_parent->extended)
1141 WARN("Trying to create a shared cube texture on a non-ex device.\n");
1142 return E_NOTIMPL;
1145 if (pool != D3DPOOL_DEFAULT)
1147 WARN("Trying to create a shared cube texture in pool %#x.\n", pool);
1148 return D3DERR_INVALIDCALL;
1150 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1153 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1154 if (!object)
1155 return D3DERR_OUTOFVIDEOMEMORY;
1157 hr = cubetexture_init(object, device, edge_length, levels, usage, format, pool);
1158 if (FAILED(hr))
1160 WARN("Failed to initialize cube texture, hr %#x.\n", hr);
1161 HeapFree(GetProcessHeap(), 0, object);
1162 return hr;
1165 TRACE("Created cube texture %p.\n", object);
1166 *texture = (IDirect3DCubeTexture9 *)&object->IDirect3DBaseTexture9_iface;
1168 return D3D_OK;
1171 static HRESULT WINAPI d3d9_device_CreateVertexBuffer(IDirect3DDevice9Ex *iface, UINT size,
1172 DWORD usage, DWORD fvf, D3DPOOL pool, IDirect3DVertexBuffer9 **buffer,
1173 HANDLE *shared_handle)
1175 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1176 struct d3d9_vertexbuffer *object;
1177 HRESULT hr;
1179 TRACE("iface %p, size %u, usage %#x, fvf %#x, pool %#x, buffer %p, shared_handle %p.\n",
1180 iface, size, usage, fvf, pool, buffer, shared_handle);
1182 if (shared_handle)
1184 if (!device->d3d_parent->extended)
1186 WARN("Trying to create a shared vertex buffer on a non-ex device.\n");
1187 return E_NOTIMPL;
1190 if (pool != D3DPOOL_DEFAULT)
1192 WARN("Trying to create a shared vertex buffer in pool %#x.\n", pool);
1193 return D3DERR_NOTAVAILABLE;
1195 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1198 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1199 if (!object)
1200 return D3DERR_OUTOFVIDEOMEMORY;
1202 hr = vertexbuffer_init(object, device, size, usage, fvf, pool);
1203 if (FAILED(hr))
1205 WARN("Failed to initialize vertex buffer, hr %#x.\n", hr);
1206 HeapFree(GetProcessHeap(), 0, object);
1207 return hr;
1210 TRACE("Created vertex buffer %p.\n", object);
1211 *buffer = &object->IDirect3DVertexBuffer9_iface;
1213 return D3D_OK;
1216 static HRESULT WINAPI d3d9_device_CreateIndexBuffer(IDirect3DDevice9Ex *iface, UINT size,
1217 DWORD usage, D3DFORMAT format, D3DPOOL pool, IDirect3DIndexBuffer9 **buffer,
1218 HANDLE *shared_handle)
1220 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1221 struct d3d9_indexbuffer *object;
1222 HRESULT hr;
1224 TRACE("iface %p, size %u, usage %#x, format %#x, pool %#x, buffer %p, shared_handle %p.\n",
1225 iface, size, usage, format, pool, buffer, shared_handle);
1227 if (shared_handle)
1229 if (!device->d3d_parent->extended)
1231 WARN("Trying to create a shared index buffer on a non-ex device.\n");
1232 return E_NOTIMPL;
1235 if (pool != D3DPOOL_DEFAULT)
1237 WARN("Trying to create a shared index buffer in pool %#x.\n", pool);
1238 return D3DERR_NOTAVAILABLE;
1240 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1243 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1244 if (!object)
1245 return D3DERR_OUTOFVIDEOMEMORY;
1247 hr = indexbuffer_init(object, device, size, usage, format, pool);
1248 if (FAILED(hr))
1250 WARN("Failed to initialize index buffer, hr %#x.\n", hr);
1251 HeapFree(GetProcessHeap(), 0, object);
1252 return hr;
1255 TRACE("Created index buffer %p.\n", object);
1256 *buffer = &object->IDirect3DIndexBuffer9_iface;
1258 return D3D_OK;
1261 static HRESULT d3d9_device_create_surface(struct d3d9_device *device, UINT width, UINT height,
1262 D3DFORMAT format, DWORD flags, IDirect3DSurface9 **surface, UINT usage, D3DPOOL pool,
1263 D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality, void *user_mem)
1265 struct wined3d_resource_desc desc;
1266 struct d3d9_surface *surface_impl;
1267 struct wined3d_texture *texture;
1268 HRESULT hr;
1270 TRACE("device %p, width %u, height %u, format %#x, flags %#x, surface %p.\n"
1271 "usage %#x, pool %#x, multisample_type %#x, multisample_quality %u.\n",
1272 device, width, height, format, flags, surface, usage, pool,
1273 multisample_type, multisample_quality);
1275 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
1276 desc.format = wined3dformat_from_d3dformat(format);
1277 desc.multisample_type = multisample_type;
1278 desc.multisample_quality = multisample_quality;
1279 desc.usage = usage & WINED3DUSAGE_MASK;
1280 desc.pool = pool;
1281 desc.width = width;
1282 desc.height = height;
1283 desc.depth = 1;
1284 desc.size = 0;
1286 if (is_gdi_compat_wined3dformat(desc.format))
1287 flags |= WINED3D_TEXTURE_CREATE_GET_DC;
1289 wined3d_mutex_lock();
1291 if (FAILED(hr = wined3d_texture_create(device->wined3d_device, &desc,
1292 1, 1, flags, NULL, NULL, &d3d9_null_wined3d_parent_ops, &texture)))
1294 wined3d_mutex_unlock();
1295 WARN("Failed to create texture, hr %#x.\n", hr);
1296 if (hr == WINED3DERR_NOTAVAILABLE)
1297 hr = D3DERR_INVALIDCALL;
1298 return hr;
1301 surface_impl = wined3d_texture_get_sub_resource_parent(texture, 0);
1302 surface_impl->parent_device = &device->IDirect3DDevice9Ex_iface;
1303 *surface = &surface_impl->IDirect3DSurface9_iface;
1304 IDirect3DSurface9_AddRef(*surface);
1306 if (user_mem)
1307 wined3d_texture_update_desc(texture, width, height,
1308 desc.format, multisample_type, multisample_quality, user_mem, 0);
1310 wined3d_texture_decref(texture);
1312 wined3d_mutex_unlock();
1314 return D3D_OK;
1317 BOOL is_gdi_compat_wined3dformat(enum wined3d_format_id format)
1319 switch (format)
1321 case WINED3DFMT_B8G8R8A8_UNORM:
1322 case WINED3DFMT_B8G8R8X8_UNORM:
1323 case WINED3DFMT_B5G6R5_UNORM:
1324 case WINED3DFMT_B5G5R5X1_UNORM:
1325 case WINED3DFMT_B5G5R5A1_UNORM:
1326 case WINED3DFMT_B8G8R8_UNORM:
1327 return TRUE;
1328 default:
1329 return FALSE;
1333 static HRESULT WINAPI d3d9_device_CreateRenderTarget(IDirect3DDevice9Ex *iface, UINT width, UINT height,
1334 D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
1335 BOOL lockable, IDirect3DSurface9 **surface, HANDLE *shared_handle)
1337 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1338 DWORD flags = 0;
1340 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u.\n"
1341 "lockable %#x, surface %p, shared_handle %p.\n",
1342 iface, width, height, format, multisample_type, multisample_quality,
1343 lockable, surface, shared_handle);
1345 *surface = NULL;
1346 if (shared_handle)
1348 if (!device->d3d_parent->extended)
1350 WARN("Trying to create a shared render target on a non-ex device.\n");
1351 return E_NOTIMPL;
1354 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1357 if (lockable)
1358 flags |= WINED3D_TEXTURE_CREATE_MAPPABLE;
1360 return d3d9_device_create_surface(device, width, height, format, flags, surface,
1361 D3DUSAGE_RENDERTARGET, D3DPOOL_DEFAULT, multisample_type, multisample_quality, NULL);
1364 static HRESULT WINAPI d3d9_device_CreateDepthStencilSurface(IDirect3DDevice9Ex *iface, UINT width, UINT height,
1365 D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
1366 BOOL discard, IDirect3DSurface9 **surface, HANDLE *shared_handle)
1368 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1369 DWORD flags = WINED3D_TEXTURE_CREATE_MAPPABLE;
1371 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u.\n"
1372 "discard %#x, surface %p, shared_handle %p.\n",
1373 iface, width, height, format, multisample_type, multisample_quality,
1374 discard, surface, shared_handle);
1376 *surface = NULL;
1377 if (shared_handle)
1379 if (!device->d3d_parent->extended)
1381 WARN("Trying to create a shared depth stencil on a non-ex device.\n");
1382 return E_NOTIMPL;
1385 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1388 if (discard)
1389 flags |= WINED3D_TEXTURE_CREATE_DISCARD;
1391 return d3d9_device_create_surface(device, width, height, format, flags, surface,
1392 D3DUSAGE_DEPTHSTENCIL, D3DPOOL_DEFAULT, multisample_type, multisample_quality, NULL);
1396 static HRESULT WINAPI d3d9_device_UpdateSurface(IDirect3DDevice9Ex *iface,
1397 IDirect3DSurface9 *src_surface, const RECT *src_rect,
1398 IDirect3DSurface9 *dst_surface, const POINT *dst_point)
1400 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1401 struct d3d9_surface *src = unsafe_impl_from_IDirect3DSurface9(src_surface);
1402 struct d3d9_surface *dst = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1403 struct wined3d_sub_resource_desc src_desc, dst_desc;
1404 struct wined3d_box src_box;
1405 HRESULT hr;
1407 TRACE("iface %p, src_surface %p, src_rect %p, dst_surface %p, dst_point %p.\n",
1408 iface, src_surface, src_rect, dst_surface, dst_point);
1410 wined3d_mutex_lock();
1412 wined3d_texture_get_sub_resource_desc(src->wined3d_texture, src->sub_resource_idx, &src_desc);
1413 wined3d_texture_get_sub_resource_desc(dst->wined3d_texture, dst->sub_resource_idx, &dst_desc);
1414 if (src_desc.format != dst_desc.format)
1416 wined3d_mutex_unlock();
1417 WARN("Surface formats (%#x/%#x) don't match.\n",
1418 d3dformat_from_wined3dformat(src_desc.format),
1419 d3dformat_from_wined3dformat(dst_desc.format));
1420 return D3DERR_INVALIDCALL;
1423 if (src_rect)
1424 wined3d_box_set(&src_box, src_rect->left, src_rect->top, src_rect->right, src_rect->bottom, 0, 1);
1425 else
1426 wined3d_box_set(&src_box, 0, 0, src_desc.width, src_desc.height, 0, 1);
1428 hr = wined3d_device_copy_sub_resource_region(device->wined3d_device,
1429 wined3d_texture_get_resource(dst->wined3d_texture), dst->sub_resource_idx, dst_point ? dst_point->x : 0,
1430 dst_point ? dst_point->y : 0, 0, wined3d_texture_get_resource(src->wined3d_texture),
1431 src->sub_resource_idx, &src_box);
1432 wined3d_mutex_unlock();
1434 if (FAILED(hr))
1435 return D3DERR_INVALIDCALL;
1437 return hr;
1440 static HRESULT WINAPI d3d9_device_UpdateTexture(IDirect3DDevice9Ex *iface,
1441 IDirect3DBaseTexture9 *src_texture, IDirect3DBaseTexture9 *dst_texture)
1443 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1444 struct d3d9_texture *src_impl, *dst_impl;
1445 HRESULT hr;
1447 TRACE("iface %p, src_texture %p, dst_texture %p.\n", iface, src_texture, dst_texture);
1449 src_impl = unsafe_impl_from_IDirect3DBaseTexture9(src_texture);
1450 dst_impl = unsafe_impl_from_IDirect3DBaseTexture9(dst_texture);
1452 wined3d_mutex_lock();
1453 hr = wined3d_device_update_texture(device->wined3d_device,
1454 src_impl->wined3d_texture, dst_impl->wined3d_texture);
1455 wined3d_mutex_unlock();
1457 return hr;
1460 static HRESULT WINAPI d3d9_device_GetRenderTargetData(IDirect3DDevice9Ex *iface,
1461 IDirect3DSurface9 *render_target, IDirect3DSurface9 *dst_surface)
1463 struct d3d9_surface *rt_impl = unsafe_impl_from_IDirect3DSurface9(render_target);
1464 struct d3d9_surface *dst_impl = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1465 struct wined3d_sub_resource_desc wined3d_desc;
1466 RECT dst_rect, src_rect;
1467 HRESULT hr;
1469 TRACE("iface %p, render_target %p, dst_surface %p.\n", iface, render_target, dst_surface);
1471 if (!render_target || !dst_surface)
1472 return D3DERR_INVALIDCALL;
1474 wined3d_mutex_lock();
1475 wined3d_texture_get_sub_resource_desc(dst_impl->wined3d_texture, dst_impl->sub_resource_idx, &wined3d_desc);
1476 SetRect(&dst_rect, 0, 0, wined3d_desc.width, wined3d_desc.height);
1478 wined3d_texture_get_sub_resource_desc(rt_impl->wined3d_texture, rt_impl->sub_resource_idx, &wined3d_desc);
1479 SetRect(&src_rect, 0, 0, wined3d_desc.width, wined3d_desc.height);
1481 /* TODO: Check surface sizes, pools, etc. */
1482 if (wined3d_desc.multisample_type)
1483 hr = D3DERR_INVALIDCALL;
1484 else
1485 hr = wined3d_texture_blt(dst_impl->wined3d_texture, dst_impl->sub_resource_idx, &dst_rect,
1486 rt_impl->wined3d_texture, rt_impl->sub_resource_idx, &src_rect, 0, NULL, WINED3D_TEXF_POINT);
1487 wined3d_mutex_unlock();
1489 return hr;
1492 static HRESULT WINAPI d3d9_device_GetFrontBufferData(IDirect3DDevice9Ex *iface,
1493 UINT swapchain, IDirect3DSurface9 *dst_surface)
1495 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1496 struct d3d9_surface *dst_impl = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1497 HRESULT hr = D3DERR_INVALIDCALL;
1499 TRACE("iface %p, swapchain %u, dst_surface %p.\n", iface, swapchain, dst_surface);
1501 wined3d_mutex_lock();
1502 if (swapchain < device->implicit_swapchain_count)
1503 hr = wined3d_swapchain_get_front_buffer_data(device->implicit_swapchains[swapchain]->wined3d_swapchain,
1504 dst_impl->wined3d_texture, dst_impl->sub_resource_idx);
1505 wined3d_mutex_unlock();
1507 return hr;
1510 static HRESULT WINAPI d3d9_device_StretchRect(IDirect3DDevice9Ex *iface, IDirect3DSurface9 *src_surface,
1511 const RECT *src_rect, IDirect3DSurface9 *dst_surface, const RECT *dst_rect, D3DTEXTUREFILTERTYPE filter)
1513 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1514 struct d3d9_surface *src = unsafe_impl_from_IDirect3DSurface9(src_surface);
1515 struct d3d9_surface *dst = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1516 struct wined3d_sub_resource_desc src_desc, dst_desc;
1517 HRESULT hr = D3DERR_INVALIDCALL;
1518 RECT d, s;
1520 TRACE("iface %p, src_surface %p, src_rect %p, dst_surface %p, dst_rect %p, filter %#x.\n",
1521 iface, src_surface, src_rect, dst_surface, dst_rect, filter);
1523 wined3d_mutex_lock();
1524 wined3d_texture_get_sub_resource_desc(dst->wined3d_texture, dst->sub_resource_idx, &dst_desc);
1525 if (!dst_rect)
1527 SetRect(&d, 0, 0, dst_desc.width, dst_desc.height);
1528 dst_rect = &d;
1531 wined3d_texture_get_sub_resource_desc(src->wined3d_texture, src->sub_resource_idx, &src_desc);
1532 if (!src_rect)
1534 SetRect(&s, 0, 0, src_desc.width, src_desc.height);
1535 src_rect = &s;
1538 if (src_desc.usage & WINED3DUSAGE_DEPTHSTENCIL)
1540 if (device->in_scene)
1542 WARN("Rejecting depth / stencil blit while in scene.\n");
1543 goto done;
1546 if (src_rect->left || src_rect->top || src_rect->right != src_desc.width
1547 || src_rect->bottom != src_desc.height)
1549 WARN("Rejecting depth / stencil blit with invalid source rect %s.\n",
1550 wine_dbgstr_rect(src_rect));
1551 goto done;
1554 if (dst_rect->left || dst_rect->top || dst_rect->right != dst_desc.width
1555 || dst_rect->bottom != dst_desc.height)
1557 WARN("Rejecting depth / stencil blit with invalid destination rect %s.\n",
1558 wine_dbgstr_rect(dst_rect));
1559 goto done;
1562 if (src_desc.width != dst_desc.width || src_desc.height != dst_desc.height)
1564 WARN("Rejecting depth / stencil blit with mismatched surface sizes.\n");
1565 goto done;
1569 hr = wined3d_texture_blt(dst->wined3d_texture, dst->sub_resource_idx, dst_rect,
1570 src->wined3d_texture, src->sub_resource_idx, src_rect, 0, NULL, filter);
1571 if (hr == WINEDDERR_INVALIDRECT)
1572 hr = D3DERR_INVALIDCALL;
1574 done:
1575 wined3d_mutex_unlock();
1576 return hr;
1579 static HRESULT WINAPI d3d9_device_ColorFill(IDirect3DDevice9Ex *iface,
1580 IDirect3DSurface9 *surface, const RECT *rect, D3DCOLOR color)
1582 const struct wined3d_color c =
1584 ((color >> 16) & 0xff) / 255.0f,
1585 ((color >> 8) & 0xff) / 255.0f,
1586 (color & 0xff) / 255.0f,
1587 ((color >> 24) & 0xff) / 255.0f,
1589 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1590 struct d3d9_surface *surface_impl = unsafe_impl_from_IDirect3DSurface9(surface);
1591 struct wined3d_sub_resource_desc desc;
1592 struct wined3d_rendertarget_view *rtv;
1593 HRESULT hr;
1595 TRACE("iface %p, surface %p, rect %p, color 0x%08x.\n", iface, surface, rect, color);
1597 wined3d_mutex_lock();
1599 if (FAILED(wined3d_texture_get_sub_resource_desc(surface_impl->wined3d_texture,
1600 surface_impl->sub_resource_idx, &desc)))
1602 wined3d_mutex_unlock();
1603 return D3DERR_INVALIDCALL;
1606 if (desc.pool != WINED3D_POOL_DEFAULT)
1608 wined3d_mutex_unlock();
1609 WARN("Colorfill is not allowed on surfaces in pool %#x, returning D3DERR_INVALIDCALL.\n", desc.pool);
1610 return D3DERR_INVALIDCALL;
1612 if ((desc.usage & (WINED3DUSAGE_RENDERTARGET | WINED3DUSAGE_TEXTURE)) == WINED3DUSAGE_TEXTURE)
1614 wined3d_mutex_unlock();
1615 WARN("Colorfill is not allowed on non-RT textures, returning D3DERR_INVALIDCALL.\n");
1616 return D3DERR_INVALIDCALL;
1618 if (desc.usage & WINED3DUSAGE_DEPTHSTENCIL)
1620 wined3d_mutex_unlock();
1621 WARN("Colorfill is not allowed on depth stencil surfaces, returning D3DERR_INVALIDCALL.\n");
1622 return D3DERR_INVALIDCALL;
1625 rtv = d3d9_surface_acquire_rendertarget_view(surface_impl);
1626 hr = wined3d_device_clear_rendertarget_view(device->wined3d_device,
1627 rtv, rect, WINED3DCLEAR_TARGET, &c, 0.0f, 0);
1628 d3d9_surface_release_rendertarget_view(surface_impl, rtv);
1630 wined3d_mutex_unlock();
1632 return hr;
1635 static HRESULT WINAPI d3d9_device_CreateOffscreenPlainSurface(IDirect3DDevice9Ex *iface,
1636 UINT width, UINT height, D3DFORMAT format, D3DPOOL pool, IDirect3DSurface9 **surface,
1637 HANDLE *shared_handle)
1639 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1640 void *user_mem = NULL;
1642 TRACE("iface %p, width %u, height %u, format %#x, pool %#x, surface %p, shared_handle %p.\n",
1643 iface, width, height, format, pool, surface, shared_handle);
1645 *surface = NULL;
1646 if (pool == D3DPOOL_MANAGED)
1648 WARN("Attempting to create a managed offscreen plain surface.\n");
1649 return D3DERR_INVALIDCALL;
1652 if (shared_handle)
1654 if (!device->d3d_parent->extended)
1656 WARN("Trying to create a shared or user memory surface on a non-ex device.\n");
1657 return E_NOTIMPL;
1660 if (pool == D3DPOOL_SYSTEMMEM)
1661 user_mem = *shared_handle;
1662 else
1664 if (pool != D3DPOOL_DEFAULT)
1666 WARN("Trying to create a shared surface in pool %#x.\n", pool);
1667 return D3DERR_INVALIDCALL;
1669 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1673 /* FIXME: Offscreen surfaces are supposed to be always lockable,
1674 * regardless of the pool they're created in. Should we set dynamic usage
1675 * here? */
1676 return d3d9_device_create_surface(device, width, height, format,
1677 WINED3D_TEXTURE_CREATE_MAPPABLE, surface, 0, pool, D3DMULTISAMPLE_NONE, 0, user_mem);
1680 static HRESULT WINAPI d3d9_device_SetRenderTarget(IDirect3DDevice9Ex *iface, DWORD idx, IDirect3DSurface9 *surface)
1682 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1683 struct d3d9_surface *surface_impl = unsafe_impl_from_IDirect3DSurface9(surface);
1684 struct wined3d_rendertarget_view *rtv;
1685 HRESULT hr;
1687 TRACE("iface %p, idx %u, surface %p.\n", iface, idx, surface);
1689 if (idx >= D3D9_MAX_SIMULTANEOUS_RENDERTARGETS)
1691 WARN("Invalid index %u specified.\n", idx);
1692 return D3DERR_INVALIDCALL;
1695 if (!idx && !surface_impl)
1697 WARN("Trying to set render target 0 to NULL.\n");
1698 return D3DERR_INVALIDCALL;
1701 if (surface_impl && d3d9_surface_get_device(surface_impl) != device)
1703 WARN("Render target surface does not match device.\n");
1704 return D3DERR_INVALIDCALL;
1707 wined3d_mutex_lock();
1708 rtv = surface_impl ? d3d9_surface_acquire_rendertarget_view(surface_impl) : NULL;
1709 hr = wined3d_device_set_rendertarget_view(device->wined3d_device, idx, rtv, TRUE);
1710 d3d9_surface_release_rendertarget_view(surface_impl, rtv);
1711 wined3d_mutex_unlock();
1713 return hr;
1716 static HRESULT WINAPI d3d9_device_GetRenderTarget(IDirect3DDevice9Ex *iface, DWORD idx, IDirect3DSurface9 **surface)
1718 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1719 struct wined3d_rendertarget_view *wined3d_rtv;
1720 struct d3d9_surface *surface_impl;
1721 HRESULT hr = D3D_OK;
1723 TRACE("iface %p, idx %u, surface %p.\n", iface, idx, surface);
1725 if (!surface)
1726 return D3DERR_INVALIDCALL;
1728 if (idx >= D3D9_MAX_SIMULTANEOUS_RENDERTARGETS)
1730 WARN("Invalid index %u specified.\n", idx);
1731 return D3DERR_INVALIDCALL;
1734 wined3d_mutex_lock();
1735 if ((wined3d_rtv = wined3d_device_get_rendertarget_view(device->wined3d_device, idx)))
1737 /* We want the sub resource parent here, since the view itself may be
1738 * internal to wined3d and may not have a parent. */
1739 surface_impl = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_rtv);
1740 *surface = &surface_impl->IDirect3DSurface9_iface;
1741 IDirect3DSurface9_AddRef(*surface);
1743 else
1745 hr = D3DERR_NOTFOUND;
1746 *surface = NULL;
1748 wined3d_mutex_unlock();
1750 return hr;
1753 static HRESULT WINAPI d3d9_device_SetDepthStencilSurface(IDirect3DDevice9Ex *iface, IDirect3DSurface9 *depth_stencil)
1755 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1756 struct d3d9_surface *ds_impl = unsafe_impl_from_IDirect3DSurface9(depth_stencil);
1757 struct wined3d_rendertarget_view *rtv;
1759 TRACE("iface %p, depth_stencil %p.\n", iface, depth_stencil);
1761 wined3d_mutex_lock();
1762 rtv = ds_impl ? d3d9_surface_acquire_rendertarget_view(ds_impl) : NULL;
1763 wined3d_device_set_depth_stencil_view(device->wined3d_device, rtv);
1764 d3d9_surface_release_rendertarget_view(ds_impl, rtv);
1765 wined3d_mutex_unlock();
1767 return D3D_OK;
1770 static HRESULT WINAPI d3d9_device_GetDepthStencilSurface(IDirect3DDevice9Ex *iface, IDirect3DSurface9 **depth_stencil)
1772 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1773 struct wined3d_rendertarget_view *wined3d_dsv;
1774 struct d3d9_surface *surface_impl;
1775 HRESULT hr = D3D_OK;
1777 TRACE("iface %p, depth_stencil %p.\n", iface, depth_stencil);
1779 if (!depth_stencil)
1780 return D3DERR_INVALIDCALL;
1782 wined3d_mutex_lock();
1783 if ((wined3d_dsv = wined3d_device_get_depth_stencil_view(device->wined3d_device)))
1785 /* We want the sub resource parent here, since the view itself may be
1786 * internal to wined3d and may not have a parent. */
1787 surface_impl = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_dsv);
1788 *depth_stencil = &surface_impl->IDirect3DSurface9_iface;
1789 IDirect3DSurface9_AddRef(*depth_stencil);
1791 else
1793 hr = D3DERR_NOTFOUND;
1794 *depth_stencil = NULL;
1796 wined3d_mutex_unlock();
1798 return hr;
1801 static HRESULT WINAPI d3d9_device_BeginScene(IDirect3DDevice9Ex *iface)
1803 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1804 HRESULT hr;
1806 TRACE("iface %p.\n", iface);
1808 wined3d_mutex_lock();
1809 if (SUCCEEDED(hr = wined3d_device_begin_scene(device->wined3d_device)))
1810 device->in_scene = TRUE;
1811 wined3d_mutex_unlock();
1813 return hr;
1816 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_EndScene(IDirect3DDevice9Ex *iface)
1818 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1819 HRESULT hr;
1821 TRACE("iface %p.\n", iface);
1823 wined3d_mutex_lock();
1824 if (SUCCEEDED(hr = wined3d_device_end_scene(device->wined3d_device)))
1825 device->in_scene = FALSE;
1826 wined3d_mutex_unlock();
1828 return hr;
1831 static HRESULT WINAPI d3d9_device_Clear(IDirect3DDevice9Ex *iface, DWORD rect_count,
1832 const D3DRECT *rects, DWORD flags, D3DCOLOR color, float z, DWORD stencil)
1834 const struct wined3d_color c =
1836 ((color >> 16) & 0xff) / 255.0f,
1837 ((color >> 8) & 0xff) / 255.0f,
1838 (color & 0xff) / 255.0f,
1839 ((color >> 24) & 0xff) / 255.0f,
1841 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1842 HRESULT hr;
1844 TRACE("iface %p, rect_count %u, rects %p, flags %#x, color 0x%08x, z %.8e, stencil %u.\n",
1845 iface, rect_count, rects, flags, color, z, stencil);
1847 if (rect_count && !rects)
1849 WARN("count %u with NULL rects.\n", rect_count);
1850 rect_count = 0;
1853 wined3d_mutex_lock();
1854 hr = wined3d_device_clear(device->wined3d_device, rect_count, (const RECT *)rects, flags, &c, z, stencil);
1855 wined3d_mutex_unlock();
1857 return hr;
1860 static HRESULT WINAPI d3d9_device_SetTransform(IDirect3DDevice9Ex *iface,
1861 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
1863 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1865 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1867 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1868 wined3d_mutex_lock();
1869 wined3d_device_set_transform(device->wined3d_device, state, (const struct wined3d_matrix *)matrix);
1870 wined3d_mutex_unlock();
1872 return D3D_OK;
1875 static HRESULT WINAPI d3d9_device_GetTransform(IDirect3DDevice9Ex *iface,
1876 D3DTRANSFORMSTATETYPE state, D3DMATRIX *matrix)
1878 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1880 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1882 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1883 wined3d_mutex_lock();
1884 wined3d_device_get_transform(device->wined3d_device, state, (struct wined3d_matrix *)matrix);
1885 wined3d_mutex_unlock();
1887 return D3D_OK;
1890 static HRESULT WINAPI d3d9_device_MultiplyTransform(IDirect3DDevice9Ex *iface,
1891 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
1893 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1895 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1897 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1898 wined3d_mutex_lock();
1899 wined3d_device_multiply_transform(device->wined3d_device, state, (const struct wined3d_matrix *)matrix);
1900 wined3d_mutex_unlock();
1902 return D3D_OK;
1905 static HRESULT WINAPI d3d9_device_SetViewport(IDirect3DDevice9Ex *iface, const D3DVIEWPORT9 *viewport)
1907 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1908 struct wined3d_viewport vp;
1910 TRACE("iface %p, viewport %p.\n", iface, viewport);
1912 vp.x = viewport->X;
1913 vp.y = viewport->Y;
1914 vp.width = viewport->Width;
1915 vp.height = viewport->Height;
1916 vp.min_z = viewport->MinZ;
1917 vp.max_z = viewport->MaxZ;
1919 wined3d_mutex_lock();
1920 wined3d_device_set_viewport(device->wined3d_device, &vp);
1921 wined3d_mutex_unlock();
1923 return D3D_OK;
1926 static HRESULT WINAPI d3d9_device_GetViewport(IDirect3DDevice9Ex *iface, D3DVIEWPORT9 *viewport)
1928 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1929 struct wined3d_viewport wined3d_viewport;
1931 TRACE("iface %p, viewport %p.\n", iface, viewport);
1933 wined3d_mutex_lock();
1934 wined3d_device_get_viewport(device->wined3d_device, &wined3d_viewport);
1935 wined3d_mutex_unlock();
1937 viewport->X = wined3d_viewport.x;
1938 viewport->Y = wined3d_viewport.y;
1939 viewport->Width = wined3d_viewport.width;
1940 viewport->Height = wined3d_viewport.height;
1941 viewport->MinZ = wined3d_viewport.min_z;
1942 viewport->MaxZ = wined3d_viewport.max_z;
1944 return D3D_OK;
1947 static HRESULT WINAPI d3d9_device_SetMaterial(IDirect3DDevice9Ex *iface, const D3DMATERIAL9 *material)
1949 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1951 TRACE("iface %p, material %p.\n", iface, material);
1953 /* Note: D3DMATERIAL9 is compatible with struct wined3d_material. */
1954 wined3d_mutex_lock();
1955 wined3d_device_set_material(device->wined3d_device, (const struct wined3d_material *)material);
1956 wined3d_mutex_unlock();
1958 return D3D_OK;
1961 static HRESULT WINAPI d3d9_device_GetMaterial(IDirect3DDevice9Ex *iface, D3DMATERIAL9 *material)
1963 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1965 TRACE("iface %p, material %p.\n", iface, material);
1967 /* Note: D3DMATERIAL9 is compatible with struct wined3d_material. */
1968 wined3d_mutex_lock();
1969 wined3d_device_get_material(device->wined3d_device, (struct wined3d_material *)material);
1970 wined3d_mutex_unlock();
1972 return D3D_OK;
1975 static HRESULT WINAPI d3d9_device_SetLight(IDirect3DDevice9Ex *iface, DWORD index, const D3DLIGHT9 *light)
1977 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1978 HRESULT hr;
1980 TRACE("iface %p, index %u, light %p.\n", iface, index, light);
1982 /* Note: D3DLIGHT9 is compatible with struct wined3d_light. */
1983 wined3d_mutex_lock();
1984 hr = wined3d_device_set_light(device->wined3d_device, index, (const struct wined3d_light *)light);
1985 wined3d_mutex_unlock();
1987 return hr;
1990 static HRESULT WINAPI d3d9_device_GetLight(IDirect3DDevice9Ex *iface, DWORD index, D3DLIGHT9 *light)
1992 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1993 HRESULT hr;
1995 TRACE("iface %p, index %u, light %p.\n", iface, index, light);
1997 /* Note: D3DLIGHT9 is compatible with struct wined3d_light. */
1998 wined3d_mutex_lock();
1999 hr = wined3d_device_get_light(device->wined3d_device, index, (struct wined3d_light *)light);
2000 wined3d_mutex_unlock();
2002 return hr;
2005 static HRESULT WINAPI d3d9_device_LightEnable(IDirect3DDevice9Ex *iface, DWORD index, BOOL enable)
2007 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2008 HRESULT hr;
2010 TRACE("iface %p, index %u, enable %#x.\n", iface, index, enable);
2012 wined3d_mutex_lock();
2013 hr = wined3d_device_set_light_enable(device->wined3d_device, index, enable);
2014 wined3d_mutex_unlock();
2016 return hr;
2019 static HRESULT WINAPI d3d9_device_GetLightEnable(IDirect3DDevice9Ex *iface, DWORD index, BOOL *enable)
2021 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2022 HRESULT hr;
2024 TRACE("iface %p, index %u, enable %p.\n", iface, index, enable);
2026 wined3d_mutex_lock();
2027 hr = wined3d_device_get_light_enable(device->wined3d_device, index, enable);
2028 wined3d_mutex_unlock();
2030 return hr;
2033 static HRESULT WINAPI d3d9_device_SetClipPlane(IDirect3DDevice9Ex *iface, DWORD index, const float *plane)
2035 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2036 HRESULT hr;
2038 TRACE("iface %p, index %u, plane %p.\n", iface, index, plane);
2040 index = min(index, device->max_user_clip_planes - 1);
2042 wined3d_mutex_lock();
2043 hr = wined3d_device_set_clip_plane(device->wined3d_device, index, (const struct wined3d_vec4 *)plane);
2044 wined3d_mutex_unlock();
2046 return hr;
2049 static HRESULT WINAPI d3d9_device_GetClipPlane(IDirect3DDevice9Ex *iface, DWORD index, float *plane)
2051 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2052 HRESULT hr;
2054 TRACE("iface %p, index %u, plane %p.\n", iface, index, plane);
2056 index = min(index, device->max_user_clip_planes - 1);
2058 wined3d_mutex_lock();
2059 hr = wined3d_device_get_clip_plane(device->wined3d_device, index, (struct wined3d_vec4 *)plane);
2060 wined3d_mutex_unlock();
2062 return hr;
2065 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_SetRenderState(IDirect3DDevice9Ex *iface,
2066 D3DRENDERSTATETYPE state, DWORD value)
2068 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2070 TRACE("iface %p, state %#x, value %#x.\n", iface, state, value);
2072 wined3d_mutex_lock();
2073 wined3d_device_set_render_state(device->wined3d_device, state, value);
2074 wined3d_mutex_unlock();
2076 return D3D_OK;
2079 static HRESULT WINAPI d3d9_device_GetRenderState(IDirect3DDevice9Ex *iface,
2080 D3DRENDERSTATETYPE state, DWORD *value)
2082 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2084 TRACE("iface %p, state %#x, value %p.\n", iface, state, value);
2086 wined3d_mutex_lock();
2087 *value = wined3d_device_get_render_state(device->wined3d_device, state);
2088 wined3d_mutex_unlock();
2090 return D3D_OK;
2093 static HRESULT WINAPI d3d9_device_CreateStateBlock(IDirect3DDevice9Ex *iface,
2094 D3DSTATEBLOCKTYPE type, IDirect3DStateBlock9 **stateblock)
2096 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2097 struct d3d9_stateblock *object;
2098 HRESULT hr;
2100 TRACE("iface %p, type %#x, stateblock %p.\n", iface, type, stateblock);
2102 if (type != D3DSBT_ALL && type != D3DSBT_PIXELSTATE && type != D3DSBT_VERTEXSTATE)
2104 WARN("Unexpected stateblock type, returning D3DERR_INVALIDCALL.\n");
2105 return D3DERR_INVALIDCALL;
2108 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2109 if (!object)
2110 return E_OUTOFMEMORY;
2112 hr = stateblock_init(object, device, type, NULL);
2113 if (FAILED(hr))
2115 WARN("Failed to initialize stateblock, hr %#x.\n", hr);
2116 HeapFree(GetProcessHeap(), 0, object);
2117 return hr;
2120 TRACE("Created stateblock %p.\n", object);
2121 *stateblock = &object->IDirect3DStateBlock9_iface;
2123 return D3D_OK;
2126 static HRESULT WINAPI d3d9_device_BeginStateBlock(IDirect3DDevice9Ex *iface)
2128 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2129 HRESULT hr;
2131 TRACE("iface %p.\n", iface);
2133 wined3d_mutex_lock();
2134 hr = wined3d_device_begin_stateblock(device->wined3d_device);
2135 wined3d_mutex_unlock();
2137 return hr;
2140 static HRESULT WINAPI d3d9_device_EndStateBlock(IDirect3DDevice9Ex *iface, IDirect3DStateBlock9 **stateblock)
2142 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2143 struct wined3d_stateblock *wined3d_stateblock;
2144 struct d3d9_stateblock *object;
2145 HRESULT hr;
2147 TRACE("iface %p, stateblock %p.\n", iface, stateblock);
2149 wined3d_mutex_lock();
2150 hr = wined3d_device_end_stateblock(device->wined3d_device, &wined3d_stateblock);
2151 wined3d_mutex_unlock();
2152 if (FAILED(hr))
2154 WARN("Failed to end the state block, hr %#x.\n", hr);
2155 return hr;
2158 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2159 if (!object)
2161 wined3d_mutex_lock();
2162 wined3d_stateblock_decref(wined3d_stateblock);
2163 wined3d_mutex_unlock();
2164 return E_OUTOFMEMORY;
2167 hr = stateblock_init(object, device, 0, wined3d_stateblock);
2168 if (FAILED(hr))
2170 WARN("Failed to initialize stateblock, hr %#x.\n", hr);
2171 wined3d_mutex_lock();
2172 wined3d_stateblock_decref(wined3d_stateblock);
2173 wined3d_mutex_unlock();
2174 HeapFree(GetProcessHeap(), 0, object);
2175 return hr;
2178 TRACE("Created stateblock %p.\n", object);
2179 *stateblock = &object->IDirect3DStateBlock9_iface;
2181 return D3D_OK;
2184 static HRESULT WINAPI d3d9_device_SetClipStatus(IDirect3DDevice9Ex *iface, const D3DCLIPSTATUS9 *clip_status)
2186 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2187 HRESULT hr;
2189 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
2191 wined3d_mutex_lock();
2192 hr = wined3d_device_set_clip_status(device->wined3d_device, (const struct wined3d_clip_status *)clip_status);
2193 wined3d_mutex_unlock();
2195 return hr;
2198 static HRESULT WINAPI d3d9_device_GetClipStatus(IDirect3DDevice9Ex *iface, D3DCLIPSTATUS9 *clip_status)
2200 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2201 HRESULT hr;
2203 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
2205 wined3d_mutex_lock();
2206 hr = wined3d_device_get_clip_status(device->wined3d_device, (struct wined3d_clip_status *)clip_status);
2207 wined3d_mutex_unlock();
2209 return hr;
2212 static HRESULT WINAPI d3d9_device_GetTexture(IDirect3DDevice9Ex *iface, DWORD stage, IDirect3DBaseTexture9 **texture)
2214 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2215 struct wined3d_texture *wined3d_texture = NULL;
2216 struct d3d9_texture *texture_impl;
2218 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
2220 if (!texture)
2221 return D3DERR_INVALIDCALL;
2223 wined3d_mutex_lock();
2224 if ((wined3d_texture = wined3d_device_get_texture(device->wined3d_device, stage)))
2226 texture_impl = wined3d_texture_get_parent(wined3d_texture);
2227 *texture = &texture_impl->IDirect3DBaseTexture9_iface;
2228 IDirect3DBaseTexture9_AddRef(*texture);
2230 else
2232 *texture = NULL;
2234 wined3d_mutex_unlock();
2236 return D3D_OK;
2239 static HRESULT WINAPI d3d9_device_SetTexture(IDirect3DDevice9Ex *iface, DWORD stage, IDirect3DBaseTexture9 *texture)
2241 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2242 struct d3d9_texture *texture_impl;
2243 HRESULT hr;
2245 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
2247 texture_impl = unsafe_impl_from_IDirect3DBaseTexture9(texture);
2249 wined3d_mutex_lock();
2250 hr = wined3d_device_set_texture(device->wined3d_device, stage,
2251 texture_impl ? texture_impl->wined3d_texture : NULL);
2252 wined3d_mutex_unlock();
2254 return hr;
2257 static const enum wined3d_texture_stage_state tss_lookup[] =
2259 WINED3D_TSS_INVALID, /* 0, unused */
2260 WINED3D_TSS_COLOR_OP, /* 1, D3DTSS_COLOROP */
2261 WINED3D_TSS_COLOR_ARG1, /* 2, D3DTSS_COLORARG1 */
2262 WINED3D_TSS_COLOR_ARG2, /* 3, D3DTSS_COLORARG2 */
2263 WINED3D_TSS_ALPHA_OP, /* 4, D3DTSS_ALPHAOP */
2264 WINED3D_TSS_ALPHA_ARG1, /* 5, D3DTSS_ALPHAARG1 */
2265 WINED3D_TSS_ALPHA_ARG2, /* 6, D3DTSS_ALPHAARG2 */
2266 WINED3D_TSS_BUMPENV_MAT00, /* 7, D3DTSS_BUMPENVMAT00 */
2267 WINED3D_TSS_BUMPENV_MAT01, /* 8, D3DTSS_BUMPENVMAT01 */
2268 WINED3D_TSS_BUMPENV_MAT10, /* 9, D3DTSS_BUMPENVMAT10 */
2269 WINED3D_TSS_BUMPENV_MAT11, /* 10, D3DTSS_BUMPENVMAT11 */
2270 WINED3D_TSS_TEXCOORD_INDEX, /* 11, D3DTSS_TEXCOORDINDEX */
2271 WINED3D_TSS_INVALID, /* 12, unused */
2272 WINED3D_TSS_INVALID, /* 13, unused */
2273 WINED3D_TSS_INVALID, /* 14, unused */
2274 WINED3D_TSS_INVALID, /* 15, unused */
2275 WINED3D_TSS_INVALID, /* 16, unused */
2276 WINED3D_TSS_INVALID, /* 17, unused */
2277 WINED3D_TSS_INVALID, /* 18, unused */
2278 WINED3D_TSS_INVALID, /* 19, unused */
2279 WINED3D_TSS_INVALID, /* 20, unused */
2280 WINED3D_TSS_INVALID, /* 21, unused */
2281 WINED3D_TSS_BUMPENV_LSCALE, /* 22, D3DTSS_BUMPENVLSCALE */
2282 WINED3D_TSS_BUMPENV_LOFFSET, /* 23, D3DTSS_BUMPENVLOFFSET */
2283 WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS, /* 24, D3DTSS_TEXTURETRANSFORMFLAGS */
2284 WINED3D_TSS_INVALID, /* 25, unused */
2285 WINED3D_TSS_COLOR_ARG0, /* 26, D3DTSS_COLORARG0 */
2286 WINED3D_TSS_ALPHA_ARG0, /* 27, D3DTSS_ALPHAARG0 */
2287 WINED3D_TSS_RESULT_ARG, /* 28, D3DTSS_RESULTARG */
2288 WINED3D_TSS_INVALID, /* 29, unused */
2289 WINED3D_TSS_INVALID, /* 30, unused */
2290 WINED3D_TSS_INVALID, /* 31, unused */
2291 WINED3D_TSS_CONSTANT, /* 32, D3DTSS_CONSTANT */
2294 static HRESULT WINAPI d3d9_device_GetTextureStageState(IDirect3DDevice9Ex *iface,
2295 DWORD stage, D3DTEXTURESTAGESTATETYPE state, DWORD *value)
2297 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2299 TRACE("iface %p, stage %u, state %#x, value %p.\n", iface, stage, state, value);
2301 if (state >= sizeof(tss_lookup) / sizeof(*tss_lookup))
2303 WARN("Invalid state %#x passed.\n", state);
2304 return D3D_OK;
2307 wined3d_mutex_lock();
2308 *value = wined3d_device_get_texture_stage_state(device->wined3d_device, stage, tss_lookup[state]);
2309 wined3d_mutex_unlock();
2311 return D3D_OK;
2314 static HRESULT WINAPI d3d9_device_SetTextureStageState(IDirect3DDevice9Ex *iface,
2315 DWORD stage, D3DTEXTURESTAGESTATETYPE state, DWORD value)
2317 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2319 TRACE("iface %p, stage %u, state %#x, value %#x.\n", iface, stage, state, value);
2321 if (state >= sizeof(tss_lookup) / sizeof(*tss_lookup))
2323 WARN("Invalid state %#x passed.\n", state);
2324 return D3D_OK;
2327 wined3d_mutex_lock();
2328 wined3d_device_set_texture_stage_state(device->wined3d_device, stage, tss_lookup[state], value);
2329 wined3d_mutex_unlock();
2331 return D3D_OK;
2334 static HRESULT WINAPI d3d9_device_GetSamplerState(IDirect3DDevice9Ex *iface,
2335 DWORD sampler, D3DSAMPLERSTATETYPE state, DWORD *value)
2337 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2339 TRACE("iface %p, sampler %u, state %#x, value %p.\n", iface, sampler, state, value);
2341 wined3d_mutex_lock();
2342 *value = wined3d_device_get_sampler_state(device->wined3d_device, sampler, state);
2343 wined3d_mutex_unlock();
2345 return D3D_OK;
2348 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_SetSamplerState(IDirect3DDevice9Ex *iface,
2349 DWORD sampler, D3DSAMPLERSTATETYPE state, DWORD value)
2351 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2353 TRACE("iface %p, sampler %u, state %#x, value %#x.\n", iface, sampler, state, value);
2355 wined3d_mutex_lock();
2356 wined3d_device_set_sampler_state(device->wined3d_device, sampler, state, value);
2357 wined3d_mutex_unlock();
2359 return D3D_OK;
2362 static HRESULT WINAPI d3d9_device_ValidateDevice(IDirect3DDevice9Ex *iface, DWORD *pass_count)
2364 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2365 HRESULT hr;
2367 TRACE("iface %p, pass_count %p.\n", iface, pass_count);
2369 wined3d_mutex_lock();
2370 hr = wined3d_device_validate_device(device->wined3d_device, pass_count);
2371 wined3d_mutex_unlock();
2373 return hr;
2376 static HRESULT WINAPI d3d9_device_SetPaletteEntries(IDirect3DDevice9Ex *iface,
2377 UINT palette_idx, const PALETTEENTRY *entries)
2379 WARN("iface %p, palette_idx %u, entries %p unimplemented.\n", iface, palette_idx, entries);
2381 /* The d3d9 palette API is non-functional on Windows. Getters and setters are implemented,
2382 * and some drivers allow the creation of P8 surfaces. These surfaces can be copied to
2383 * other P8 surfaces with StretchRect, but cannot be converted to (A)RGB.
2385 * Some older(dx7) cards may have support for P8 textures, but games cannot rely on this. */
2386 return D3D_OK;
2389 static HRESULT WINAPI d3d9_device_GetPaletteEntries(IDirect3DDevice9Ex *iface,
2390 UINT palette_idx, PALETTEENTRY *entries)
2392 FIXME("iface %p, palette_idx %u, entries %p unimplemented.\n", iface, palette_idx, entries);
2394 return D3DERR_INVALIDCALL;
2397 static HRESULT WINAPI d3d9_device_SetCurrentTexturePalette(IDirect3DDevice9Ex *iface, UINT palette_idx)
2399 WARN("iface %p, palette_idx %u unimplemented.\n", iface, palette_idx);
2401 return D3D_OK;
2404 static HRESULT WINAPI d3d9_device_GetCurrentTexturePalette(IDirect3DDevice9Ex *iface, UINT *palette_idx)
2406 FIXME("iface %p, palette_idx %p.\n", iface, palette_idx);
2408 return D3DERR_INVALIDCALL;
2411 static HRESULT WINAPI d3d9_device_SetScissorRect(IDirect3DDevice9Ex *iface, const RECT *rect)
2413 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2415 TRACE("iface %p, rect %p.\n", iface, rect);
2417 wined3d_mutex_lock();
2418 wined3d_device_set_scissor_rect(device->wined3d_device, rect);
2419 wined3d_mutex_unlock();
2421 return D3D_OK;
2424 static HRESULT WINAPI d3d9_device_GetScissorRect(IDirect3DDevice9Ex *iface, RECT *rect)
2426 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2428 TRACE("iface %p, rect %p.\n", iface, rect);
2430 wined3d_mutex_lock();
2431 wined3d_device_get_scissor_rect(device->wined3d_device, rect);
2432 wined3d_mutex_unlock();
2434 return D3D_OK;
2437 static HRESULT WINAPI d3d9_device_SetSoftwareVertexProcessing(IDirect3DDevice9Ex *iface, BOOL software)
2439 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2441 TRACE("iface %p, software %#x.\n", iface, software);
2443 wined3d_mutex_lock();
2444 wined3d_device_set_software_vertex_processing(device->wined3d_device, software);
2445 wined3d_mutex_unlock();
2447 return D3D_OK;
2450 static BOOL WINAPI d3d9_device_GetSoftwareVertexProcessing(IDirect3DDevice9Ex *iface)
2452 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2453 BOOL ret;
2455 TRACE("iface %p.\n", iface);
2457 wined3d_mutex_lock();
2458 ret = wined3d_device_get_software_vertex_processing(device->wined3d_device);
2459 wined3d_mutex_unlock();
2461 return ret;
2464 static HRESULT WINAPI d3d9_device_SetNPatchMode(IDirect3DDevice9Ex *iface, float segment_count)
2466 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2467 HRESULT hr;
2469 TRACE("iface %p, segment_count %.8e.\n", iface, segment_count);
2471 wined3d_mutex_lock();
2472 hr = wined3d_device_set_npatch_mode(device->wined3d_device, segment_count);
2473 wined3d_mutex_unlock();
2475 return hr;
2478 static float WINAPI d3d9_device_GetNPatchMode(IDirect3DDevice9Ex *iface)
2480 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2481 float ret;
2483 TRACE("iface %p.\n", iface);
2485 wined3d_mutex_lock();
2486 ret = wined3d_device_get_npatch_mode(device->wined3d_device);
2487 wined3d_mutex_unlock();
2489 return ret;
2492 static HRESULT WINAPI d3d9_device_DrawPrimitive(IDirect3DDevice9Ex *iface,
2493 D3DPRIMITIVETYPE primitive_type, UINT start_vertex, UINT primitive_count)
2495 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2496 HRESULT hr;
2498 TRACE("iface %p, primitive_type %#x, start_vertex %u, primitive_count %u.\n",
2499 iface, primitive_type, start_vertex, primitive_count);
2501 wined3d_mutex_lock();
2502 if (!device->has_vertex_declaration)
2504 wined3d_mutex_unlock();
2505 WARN("Called without a valid vertex declaration set.\n");
2506 return D3DERR_INVALIDCALL;
2508 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type, 0);
2509 hr = wined3d_device_draw_primitive(device->wined3d_device, start_vertex,
2510 vertex_count_from_primitive_count(primitive_type, primitive_count));
2511 wined3d_mutex_unlock();
2513 return hr;
2516 static HRESULT WINAPI d3d9_device_DrawIndexedPrimitive(IDirect3DDevice9Ex *iface,
2517 D3DPRIMITIVETYPE primitive_type, INT base_vertex_idx, UINT min_vertex_idx,
2518 UINT vertex_count, UINT start_idx, UINT primitive_count)
2520 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2521 HRESULT hr;
2523 TRACE("iface %p, primitive_type %#x, base_vertex_idx %u, min_vertex_idx %u, "
2524 "vertex_count %u, start_idx %u, primitive_count %u.\n",
2525 iface, primitive_type, base_vertex_idx, min_vertex_idx,
2526 vertex_count, start_idx, primitive_count);
2528 wined3d_mutex_lock();
2529 if (!device->has_vertex_declaration)
2531 wined3d_mutex_unlock();
2532 WARN("Called without a valid vertex declaration set.\n");
2533 return D3DERR_INVALIDCALL;
2535 wined3d_device_set_base_vertex_index(device->wined3d_device, base_vertex_idx);
2536 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type, 0);
2537 hr = wined3d_device_draw_indexed_primitive(device->wined3d_device, start_idx,
2538 vertex_count_from_primitive_count(primitive_type, primitive_count));
2539 wined3d_mutex_unlock();
2541 return hr;
2544 /* The caller is responsible for wined3d locking */
2545 static HRESULT d3d9_device_prepare_vertex_buffer(struct d3d9_device *device, UINT min_size)
2547 HRESULT hr;
2549 if (device->vertex_buffer_size < min_size || !device->vertex_buffer)
2551 UINT size = max(device->vertex_buffer_size * 2, min_size);
2552 struct wined3d_buffer *buffer;
2554 TRACE("Growing vertex buffer to %u bytes.\n", size);
2556 hr = wined3d_buffer_create_vb(device->wined3d_device, size, WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_WRITEONLY,
2557 WINED3D_POOL_DEFAULT, NULL, &d3d9_null_wined3d_parent_ops, &buffer);
2558 if (FAILED(hr))
2560 ERR("(%p) wined3d_buffer_create_vb failed with hr = %08x.\n", device, hr);
2561 return hr;
2564 if (device->vertex_buffer)
2565 wined3d_buffer_decref(device->vertex_buffer);
2567 device->vertex_buffer = buffer;
2568 device->vertex_buffer_size = size;
2569 device->vertex_buffer_pos = 0;
2571 return D3D_OK;
2574 static HRESULT WINAPI d3d9_device_DrawPrimitiveUP(IDirect3DDevice9Ex *iface,
2575 D3DPRIMITIVETYPE primitive_type, UINT primitive_count, const void *data, UINT stride)
2577 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2578 HRESULT hr;
2579 UINT vtx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2580 struct wined3d_map_desc wined3d_map_desc;
2581 struct wined3d_box wined3d_box = {0};
2582 UINT size = vtx_count * stride;
2583 struct wined3d_resource *vb;
2584 UINT vb_pos, align;
2586 TRACE("iface %p, primitive_type %#x, primitive_count %u, data %p, stride %u.\n",
2587 iface, primitive_type, primitive_count, data, stride);
2589 if (!primitive_count)
2591 WARN("primitive_count is 0, returning D3D_OK\n");
2592 return D3D_OK;
2595 wined3d_mutex_lock();
2597 if (!device->has_vertex_declaration)
2599 wined3d_mutex_unlock();
2600 WARN("Called without a valid vertex declaration set.\n");
2601 return D3DERR_INVALIDCALL;
2604 hr = d3d9_device_prepare_vertex_buffer(device, size);
2605 if (FAILED(hr))
2606 goto done;
2608 vb_pos = device->vertex_buffer_pos;
2609 align = vb_pos % stride;
2610 if (align) align = stride - align;
2611 if (vb_pos + size + align > device->vertex_buffer_size)
2612 vb_pos = 0;
2613 else
2614 vb_pos += align;
2616 wined3d_box.left = vb_pos;
2617 wined3d_box.right = vb_pos + size;
2618 vb = wined3d_buffer_get_resource(device->vertex_buffer);
2619 if (FAILED(hr = wined3d_resource_map(vb, 0, &wined3d_map_desc, &wined3d_box,
2620 vb_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD)))
2621 goto done;
2622 memcpy(wined3d_map_desc.data, data, size);
2623 wined3d_resource_unmap(vb, 0);
2624 device->vertex_buffer_pos = vb_pos + size;
2626 hr = wined3d_device_set_stream_source(device->wined3d_device, 0, device->vertex_buffer, 0, stride);
2627 if (FAILED(hr))
2628 goto done;
2630 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type, 0);
2631 hr = wined3d_device_draw_primitive(device->wined3d_device, vb_pos / stride, vtx_count);
2632 wined3d_device_set_stream_source(device->wined3d_device, 0, NULL, 0, 0);
2634 done:
2635 wined3d_mutex_unlock();
2636 return hr;
2639 /* The caller is responsible for wined3d locking */
2640 static HRESULT d3d9_device_prepare_index_buffer(struct d3d9_device *device, UINT min_size)
2642 HRESULT hr;
2644 if (device->index_buffer_size < min_size || !device->index_buffer)
2646 UINT size = max(device->index_buffer_size * 2, min_size);
2647 struct wined3d_buffer *buffer;
2649 TRACE("Growing index buffer to %u bytes.\n", size);
2651 hr = wined3d_buffer_create_ib(device->wined3d_device, size, WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_WRITEONLY,
2652 WINED3D_POOL_DEFAULT, NULL, &d3d9_null_wined3d_parent_ops, &buffer);
2653 if (FAILED(hr))
2655 ERR("(%p) wined3d_buffer_create_ib failed with hr = %08x.\n", device, hr);
2656 return hr;
2659 if (device->index_buffer)
2660 wined3d_buffer_decref(device->index_buffer);
2662 device->index_buffer = buffer;
2663 device->index_buffer_size = size;
2664 device->index_buffer_pos = 0;
2666 return D3D_OK;
2669 static HRESULT WINAPI d3d9_device_DrawIndexedPrimitiveUP(IDirect3DDevice9Ex *iface,
2670 D3DPRIMITIVETYPE primitive_type, UINT min_vertex_idx, UINT vertex_count,
2671 UINT primitive_count, const void *index_data, D3DFORMAT index_format,
2672 const void *vertex_data, UINT vertex_stride)
2674 UINT idx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2675 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2676 UINT idx_fmt_size = index_format == D3DFMT_INDEX16 ? 2 : 4;
2677 UINT vtx_size = vertex_count * vertex_stride;
2678 UINT idx_size = idx_count * idx_fmt_size;
2679 struct wined3d_map_desc wined3d_map_desc;
2680 struct wined3d_box wined3d_box = {0};
2681 struct wined3d_resource *ib, *vb;
2682 UINT vb_pos, ib_pos, align;
2683 HRESULT hr;
2685 TRACE("iface %p, primitive_type %#x, min_vertex_idx %u, vertex_count %u, primitive_count %u, "
2686 "index_data %p, index_format %#x, vertex_data %p, vertex_stride %u.\n",
2687 iface, primitive_type, min_vertex_idx, vertex_count, primitive_count,
2688 index_data, index_format, vertex_data, vertex_stride);
2690 if (!primitive_count)
2692 WARN("primitive_count is 0, returning D3D_OK.\n");
2693 return D3D_OK;
2696 wined3d_mutex_lock();
2698 if (!device->has_vertex_declaration)
2700 wined3d_mutex_unlock();
2701 WARN("Called without a valid vertex declaration set.\n");
2702 return D3DERR_INVALIDCALL;
2705 hr = d3d9_device_prepare_vertex_buffer(device, vtx_size);
2706 if (FAILED(hr))
2707 goto done;
2709 vb_pos = device->vertex_buffer_pos;
2710 align = vb_pos % vertex_stride;
2711 if (align) align = vertex_stride - align;
2712 if (vb_pos + vtx_size + align > device->vertex_buffer_size)
2713 vb_pos = 0;
2714 else
2715 vb_pos += align;
2717 wined3d_box.left = vb_pos;
2718 wined3d_box.right = vb_pos + vtx_size;
2719 vb = wined3d_buffer_get_resource(device->vertex_buffer);
2720 if (FAILED(hr = wined3d_resource_map(vb, 0, &wined3d_map_desc, &wined3d_box,
2721 vb_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD)))
2722 goto done;
2723 memcpy(wined3d_map_desc.data, (char *)vertex_data + min_vertex_idx * vertex_stride, vtx_size);
2724 wined3d_resource_unmap(vb, 0);
2725 device->vertex_buffer_pos = vb_pos + vtx_size;
2727 hr = d3d9_device_prepare_index_buffer(device, idx_size);
2728 if (FAILED(hr))
2729 goto done;
2731 ib_pos = device->index_buffer_pos;
2732 align = ib_pos % idx_fmt_size;
2733 if (align) align = idx_fmt_size - align;
2734 if (ib_pos + idx_size + align > device->index_buffer_size)
2735 ib_pos = 0;
2736 else
2737 ib_pos += align;
2739 wined3d_box.left = ib_pos;
2740 wined3d_box.right = ib_pos + idx_size;
2741 ib = wined3d_buffer_get_resource(device->index_buffer);
2742 if (FAILED(hr = wined3d_resource_map(ib, 0, &wined3d_map_desc, &wined3d_box,
2743 ib_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD)))
2744 goto done;
2745 memcpy(wined3d_map_desc.data, index_data, idx_size);
2746 wined3d_resource_unmap(ib, 0);
2747 device->index_buffer_pos = ib_pos + idx_size;
2749 hr = wined3d_device_set_stream_source(device->wined3d_device, 0, device->vertex_buffer, 0, vertex_stride);
2750 if (FAILED(hr))
2751 goto done;
2753 wined3d_device_set_index_buffer(device->wined3d_device, device->index_buffer,
2754 wined3dformat_from_d3dformat(index_format), 0);
2755 wined3d_device_set_base_vertex_index(device->wined3d_device, vb_pos / vertex_stride - min_vertex_idx);
2757 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type, 0);
2758 hr = wined3d_device_draw_indexed_primitive(device->wined3d_device, ib_pos / idx_fmt_size, idx_count);
2760 wined3d_device_set_stream_source(device->wined3d_device, 0, NULL, 0, 0);
2761 wined3d_device_set_index_buffer(device->wined3d_device, NULL, WINED3DFMT_UNKNOWN, 0);
2762 wined3d_device_set_base_vertex_index(device->wined3d_device, 0);
2764 done:
2765 wined3d_mutex_unlock();
2766 return hr;
2769 static HRESULT WINAPI d3d9_device_ProcessVertices(IDirect3DDevice9Ex *iface,
2770 UINT src_start_idx, UINT dst_idx, UINT vertex_count, IDirect3DVertexBuffer9 *dst_buffer,
2771 IDirect3DVertexDeclaration9 *declaration, DWORD flags)
2773 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2774 struct d3d9_vertexbuffer *dst_impl = unsafe_impl_from_IDirect3DVertexBuffer9(dst_buffer);
2775 struct d3d9_vertex_declaration *decl_impl = unsafe_impl_from_IDirect3DVertexDeclaration9(declaration);
2776 HRESULT hr;
2778 TRACE("iface %p, src_start_idx %u, dst_idx %u, vertex_count %u, dst_buffer %p, declaration %p, flags %#x.\n",
2779 iface, src_start_idx, dst_idx, vertex_count, dst_buffer, declaration, flags);
2781 wined3d_mutex_lock();
2782 hr = wined3d_device_process_vertices(device->wined3d_device, src_start_idx, dst_idx, vertex_count,
2783 dst_impl->wined3d_buffer, decl_impl ? decl_impl->wined3d_declaration : NULL,
2784 flags, dst_impl->fvf);
2785 wined3d_mutex_unlock();
2787 return hr;
2790 static HRESULT WINAPI d3d9_device_CreateVertexDeclaration(IDirect3DDevice9Ex *iface,
2791 const D3DVERTEXELEMENT9 *elements, IDirect3DVertexDeclaration9 **declaration)
2793 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2794 struct d3d9_vertex_declaration *object;
2795 HRESULT hr;
2797 TRACE("iface %p, elements %p, declaration %p.\n", iface, elements, declaration);
2799 if (!declaration)
2801 WARN("Caller passed a NULL declaration, returning D3DERR_INVALIDCALL.\n");
2802 return D3DERR_INVALIDCALL;
2805 if (SUCCEEDED(hr = d3d9_vertex_declaration_create(device, elements, &object)))
2806 *declaration = &object->IDirect3DVertexDeclaration9_iface;
2808 return hr;
2811 static HRESULT WINAPI d3d9_device_SetVertexDeclaration(IDirect3DDevice9Ex *iface,
2812 IDirect3DVertexDeclaration9 *declaration)
2814 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2815 struct d3d9_vertex_declaration *decl_impl = unsafe_impl_from_IDirect3DVertexDeclaration9(declaration);
2817 TRACE("iface %p, declaration %p.\n", iface, declaration);
2819 wined3d_mutex_lock();
2820 wined3d_device_set_vertex_declaration(device->wined3d_device,
2821 decl_impl ? decl_impl->wined3d_declaration : NULL);
2822 device->has_vertex_declaration = !!decl_impl;
2823 wined3d_mutex_unlock();
2825 return D3D_OK;
2828 static HRESULT WINAPI d3d9_device_GetVertexDeclaration(IDirect3DDevice9Ex *iface,
2829 IDirect3DVertexDeclaration9 **declaration)
2831 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2832 struct wined3d_vertex_declaration *wined3d_declaration;
2833 struct d3d9_vertex_declaration *declaration_impl;
2835 TRACE("iface %p, declaration %p.\n", iface, declaration);
2837 if (!declaration) return D3DERR_INVALIDCALL;
2839 wined3d_mutex_lock();
2840 if ((wined3d_declaration = wined3d_device_get_vertex_declaration(device->wined3d_device)))
2842 declaration_impl = wined3d_vertex_declaration_get_parent(wined3d_declaration);
2843 *declaration = &declaration_impl->IDirect3DVertexDeclaration9_iface;
2844 IDirect3DVertexDeclaration9_AddRef(*declaration);
2846 else
2848 *declaration = NULL;
2850 wined3d_mutex_unlock();
2852 TRACE("Returning %p.\n", *declaration);
2853 return D3D_OK;
2856 static struct wined3d_vertex_declaration *device_get_fvf_declaration(struct d3d9_device *device, DWORD fvf)
2858 struct wined3d_vertex_declaration *wined3d_declaration;
2859 struct fvf_declaration *fvf_decls = device->fvf_decls;
2860 struct d3d9_vertex_declaration *d3d9_declaration;
2861 D3DVERTEXELEMENT9 *elements;
2862 int p, low, high; /* deliberately signed */
2863 HRESULT hr;
2865 TRACE("Searching for declaration for fvf %08x... ", fvf);
2867 low = 0;
2868 high = device->fvf_decl_count - 1;
2869 while (low <= high)
2871 p = (low + high) >> 1;
2872 TRACE("%d ", p);
2874 if (fvf_decls[p].fvf == fvf)
2876 TRACE("found %p.\n", fvf_decls[p].decl);
2877 return fvf_decls[p].decl;
2880 if (fvf_decls[p].fvf < fvf)
2881 low = p + 1;
2882 else
2883 high = p - 1;
2885 TRACE("not found. Creating and inserting at position %d.\n", low);
2887 if (FAILED(hr = vdecl_convert_fvf(fvf, &elements)))
2888 return NULL;
2890 hr = d3d9_vertex_declaration_create(device, elements, &d3d9_declaration);
2891 HeapFree(GetProcessHeap(), 0, elements);
2892 if (FAILED(hr))
2893 return NULL;
2895 if (device->fvf_decl_size == device->fvf_decl_count)
2897 UINT grow = max(device->fvf_decl_size / 2, 8);
2899 fvf_decls = HeapReAlloc(GetProcessHeap(), 0, fvf_decls, sizeof(*fvf_decls) * (device->fvf_decl_size + grow));
2900 if (!fvf_decls)
2902 IDirect3DVertexDeclaration9_Release(&d3d9_declaration->IDirect3DVertexDeclaration9_iface);
2903 return NULL;
2905 device->fvf_decls = fvf_decls;
2906 device->fvf_decl_size += grow;
2909 d3d9_declaration->fvf = fvf;
2910 wined3d_declaration = d3d9_declaration->wined3d_declaration;
2911 wined3d_vertex_declaration_incref(wined3d_declaration);
2912 IDirect3DVertexDeclaration9_Release(&d3d9_declaration->IDirect3DVertexDeclaration9_iface);
2914 memmove(fvf_decls + low + 1, fvf_decls + low, sizeof(*fvf_decls) * (device->fvf_decl_count - low));
2915 fvf_decls[low].decl = wined3d_declaration;
2916 fvf_decls[low].fvf = fvf;
2917 ++device->fvf_decl_count;
2919 TRACE("Returning %p. %u declarations in array.\n", wined3d_declaration, device->fvf_decl_count);
2921 return wined3d_declaration;
2924 static HRESULT WINAPI d3d9_device_SetFVF(IDirect3DDevice9Ex *iface, DWORD fvf)
2926 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2927 struct wined3d_vertex_declaration *decl;
2929 TRACE("iface %p, fvf %#x.\n", iface, fvf);
2931 if (!fvf)
2933 WARN("%#x is not a valid FVF.\n", fvf);
2934 return D3D_OK;
2937 wined3d_mutex_lock();
2938 if (!(decl = device_get_fvf_declaration(device, fvf)))
2940 wined3d_mutex_unlock();
2941 ERR("Failed to create a vertex declaration for fvf %#x.\n", fvf);
2942 return D3DERR_DRIVERINTERNALERROR;
2945 wined3d_device_set_vertex_declaration(device->wined3d_device, decl);
2946 device->has_vertex_declaration = TRUE;
2947 wined3d_mutex_unlock();
2949 return D3D_OK;
2952 static HRESULT WINAPI d3d9_device_GetFVF(IDirect3DDevice9Ex *iface, DWORD *fvf)
2954 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2955 struct wined3d_vertex_declaration *wined3d_declaration;
2956 struct d3d9_vertex_declaration *d3d9_declaration;
2958 TRACE("iface %p, fvf %p.\n", iface, fvf);
2960 wined3d_mutex_lock();
2961 if ((wined3d_declaration = wined3d_device_get_vertex_declaration(device->wined3d_device)))
2963 d3d9_declaration = wined3d_vertex_declaration_get_parent(wined3d_declaration);
2964 *fvf = d3d9_declaration->fvf;
2966 else
2968 *fvf = 0;
2970 wined3d_mutex_unlock();
2972 TRACE("Returning FVF %#x.\n", *fvf);
2974 return D3D_OK;
2977 static HRESULT WINAPI d3d9_device_CreateVertexShader(IDirect3DDevice9Ex *iface,
2978 const DWORD *byte_code, IDirect3DVertexShader9 **shader)
2980 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2981 struct d3d9_vertexshader *object;
2982 HRESULT hr;
2984 TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
2986 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2987 if (!object)
2988 return E_OUTOFMEMORY;
2990 hr = vertexshader_init(object, device, byte_code);
2991 if (FAILED(hr))
2993 WARN("Failed to initialize vertex shader, hr %#x.\n", hr);
2994 HeapFree(GetProcessHeap(), 0, object);
2995 return hr;
2998 TRACE("Created vertex shader %p.\n", object);
2999 *shader = &object->IDirect3DVertexShader9_iface;
3001 return D3D_OK;
3004 static HRESULT WINAPI d3d9_device_SetVertexShader(IDirect3DDevice9Ex *iface, IDirect3DVertexShader9 *shader)
3006 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3007 struct d3d9_vertexshader *shader_obj = unsafe_impl_from_IDirect3DVertexShader9(shader);
3009 TRACE("iface %p, shader %p.\n", iface, shader);
3011 wined3d_mutex_lock();
3012 wined3d_device_set_vertex_shader(device->wined3d_device,
3013 shader_obj ? shader_obj->wined3d_shader : NULL);
3014 wined3d_mutex_unlock();
3016 return D3D_OK;
3019 static HRESULT WINAPI d3d9_device_GetVertexShader(IDirect3DDevice9Ex *iface, IDirect3DVertexShader9 **shader)
3021 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3022 struct d3d9_vertexshader *shader_impl;
3023 struct wined3d_shader *wined3d_shader;
3025 TRACE("iface %p, shader %p.\n", iface, shader);
3027 wined3d_mutex_lock();
3028 if ((wined3d_shader = wined3d_device_get_vertex_shader(device->wined3d_device)))
3030 shader_impl = wined3d_shader_get_parent(wined3d_shader);
3031 *shader = &shader_impl->IDirect3DVertexShader9_iface;
3032 IDirect3DVertexShader9_AddRef(*shader);
3034 else
3036 *shader = NULL;
3038 wined3d_mutex_unlock();
3040 TRACE("Returning %p.\n", *shader);
3042 return D3D_OK;
3045 static HRESULT WINAPI d3d9_device_SetVertexShaderConstantF(IDirect3DDevice9Ex *iface,
3046 UINT reg_idx, const float *data, UINT count)
3048 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3049 HRESULT hr;
3051 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3053 if (reg_idx + count > D3D9_MAX_VERTEX_SHADER_CONSTANTF)
3055 WARN("Trying to access %u constants, but d3d9 only supports %u\n",
3056 reg_idx + count, D3D9_MAX_VERTEX_SHADER_CONSTANTF);
3057 return D3DERR_INVALIDCALL;
3060 wined3d_mutex_lock();
3061 hr = wined3d_device_set_vs_consts_f(device->wined3d_device,
3062 reg_idx, count, (const struct wined3d_vec4 *)data);
3063 wined3d_mutex_unlock();
3065 return hr;
3068 static HRESULT WINAPI d3d9_device_GetVertexShaderConstantF(IDirect3DDevice9Ex *iface,
3069 UINT reg_idx, float *data, UINT count)
3071 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3072 HRESULT hr;
3074 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3076 if (reg_idx + count > D3D9_MAX_VERTEX_SHADER_CONSTANTF)
3078 WARN("Trying to access %u constants, but d3d9 only supports %u\n",
3079 reg_idx + count, D3D9_MAX_VERTEX_SHADER_CONSTANTF);
3080 return D3DERR_INVALIDCALL;
3083 wined3d_mutex_lock();
3084 hr = wined3d_device_get_vs_consts_f(device->wined3d_device,
3085 reg_idx, count, (struct wined3d_vec4 *)data);
3086 wined3d_mutex_unlock();
3088 return hr;
3091 static HRESULT WINAPI d3d9_device_SetVertexShaderConstantI(IDirect3DDevice9Ex *iface,
3092 UINT reg_idx, const int *data, UINT count)
3094 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3095 HRESULT hr;
3097 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3099 wined3d_mutex_lock();
3100 hr = wined3d_device_set_vs_consts_i(device->wined3d_device,
3101 reg_idx, count, (const struct wined3d_ivec4 *)data);
3102 wined3d_mutex_unlock();
3104 return hr;
3107 static HRESULT WINAPI d3d9_device_GetVertexShaderConstantI(IDirect3DDevice9Ex *iface,
3108 UINT reg_idx, int *data, UINT count)
3110 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3111 HRESULT hr;
3113 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3115 wined3d_mutex_lock();
3116 hr = wined3d_device_get_vs_consts_i(device->wined3d_device,
3117 reg_idx, count, (struct wined3d_ivec4 *)data);
3118 wined3d_mutex_unlock();
3120 return hr;
3123 static HRESULT WINAPI d3d9_device_SetVertexShaderConstantB(IDirect3DDevice9Ex *iface,
3124 UINT reg_idx, const BOOL *data, UINT count)
3126 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3127 HRESULT hr;
3129 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3131 wined3d_mutex_lock();
3132 hr = wined3d_device_set_vs_consts_b(device->wined3d_device, reg_idx, count, data);
3133 wined3d_mutex_unlock();
3135 return hr;
3138 static HRESULT WINAPI d3d9_device_GetVertexShaderConstantB(IDirect3DDevice9Ex *iface,
3139 UINT reg_idx, BOOL *data, UINT count)
3141 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3142 HRESULT hr;
3144 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3146 wined3d_mutex_lock();
3147 hr = wined3d_device_get_vs_consts_b(device->wined3d_device, reg_idx, count, data);
3148 wined3d_mutex_unlock();
3150 return hr;
3153 static HRESULT WINAPI d3d9_device_SetStreamSource(IDirect3DDevice9Ex *iface,
3154 UINT stream_idx, IDirect3DVertexBuffer9 *buffer, UINT offset, UINT stride)
3156 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3157 struct d3d9_vertexbuffer *buffer_impl = unsafe_impl_from_IDirect3DVertexBuffer9(buffer);
3158 HRESULT hr;
3160 TRACE("iface %p, stream_idx %u, buffer %p, offset %u, stride %u.\n",
3161 iface, stream_idx, buffer, offset, stride);
3163 wined3d_mutex_lock();
3164 hr = wined3d_device_set_stream_source(device->wined3d_device, stream_idx,
3165 buffer_impl ? buffer_impl->wined3d_buffer : NULL, offset, stride);
3166 wined3d_mutex_unlock();
3168 return hr;
3171 static HRESULT WINAPI d3d9_device_GetStreamSource(IDirect3DDevice9Ex *iface,
3172 UINT stream_idx, IDirect3DVertexBuffer9 **buffer, UINT *offset, UINT *stride)
3174 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3175 struct d3d9_vertexbuffer *buffer_impl;
3176 struct wined3d_buffer *wined3d_buffer;
3177 HRESULT hr;
3179 TRACE("iface %p, stream_idx %u, buffer %p, offset %p, stride %p.\n",
3180 iface, stream_idx, buffer, offset, stride);
3182 if (!buffer)
3183 return D3DERR_INVALIDCALL;
3185 wined3d_mutex_lock();
3186 hr = wined3d_device_get_stream_source(device->wined3d_device, stream_idx, &wined3d_buffer, offset, stride);
3187 if (SUCCEEDED(hr) && wined3d_buffer)
3189 buffer_impl = wined3d_buffer_get_parent(wined3d_buffer);
3190 *buffer = &buffer_impl->IDirect3DVertexBuffer9_iface;
3191 IDirect3DVertexBuffer9_AddRef(*buffer);
3193 else
3195 if (FAILED(hr))
3196 FIXME("Call to GetStreamSource failed %p %p\n", offset, stride);
3197 *buffer = NULL;
3199 wined3d_mutex_unlock();
3201 return hr;
3204 static HRESULT WINAPI d3d9_device_SetStreamSourceFreq(IDirect3DDevice9Ex *iface, UINT stream_idx, UINT freq)
3206 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3207 HRESULT hr;
3209 TRACE("iface %p, stream_idx %u, freq %u.\n", iface, stream_idx, freq);
3211 wined3d_mutex_lock();
3212 hr = wined3d_device_set_stream_source_freq(device->wined3d_device, stream_idx, freq);
3213 wined3d_mutex_unlock();
3215 return hr;
3218 static HRESULT WINAPI d3d9_device_GetStreamSourceFreq(IDirect3DDevice9Ex *iface, UINT stream_idx, UINT *freq)
3220 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3221 HRESULT hr;
3223 TRACE("iface %p, stream_idx %u, freq %p.\n", iface, stream_idx, freq);
3225 wined3d_mutex_lock();
3226 hr = wined3d_device_get_stream_source_freq(device->wined3d_device, stream_idx, freq);
3227 wined3d_mutex_unlock();
3229 return hr;
3232 static HRESULT WINAPI d3d9_device_SetIndices(IDirect3DDevice9Ex *iface, IDirect3DIndexBuffer9 *buffer)
3234 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3235 struct d3d9_indexbuffer *ib = unsafe_impl_from_IDirect3DIndexBuffer9(buffer);
3237 TRACE("iface %p, buffer %p.\n", iface, buffer);
3239 wined3d_mutex_lock();
3240 wined3d_device_set_index_buffer(device->wined3d_device,
3241 ib ? ib->wined3d_buffer : NULL, ib ? ib->format : WINED3DFMT_UNKNOWN, 0);
3242 wined3d_mutex_unlock();
3244 return D3D_OK;
3247 static HRESULT WINAPI d3d9_device_GetIndices(IDirect3DDevice9Ex *iface, IDirect3DIndexBuffer9 **buffer)
3249 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3250 enum wined3d_format_id wined3d_format;
3251 struct wined3d_buffer *wined3d_buffer;
3252 struct d3d9_indexbuffer *buffer_impl;
3254 TRACE("iface %p, buffer %p.\n", iface, buffer);
3256 if (!buffer)
3257 return D3DERR_INVALIDCALL;
3259 wined3d_mutex_lock();
3260 if ((wined3d_buffer = wined3d_device_get_index_buffer(device->wined3d_device, &wined3d_format, NULL)))
3262 buffer_impl = wined3d_buffer_get_parent(wined3d_buffer);
3263 *buffer = &buffer_impl->IDirect3DIndexBuffer9_iface;
3264 IDirect3DIndexBuffer9_AddRef(*buffer);
3266 else
3268 *buffer = NULL;
3270 wined3d_mutex_unlock();
3272 return D3D_OK;
3275 static HRESULT WINAPI d3d9_device_CreatePixelShader(IDirect3DDevice9Ex *iface,
3276 const DWORD *byte_code, IDirect3DPixelShader9 **shader)
3278 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3279 struct d3d9_pixelshader *object;
3280 HRESULT hr;
3282 TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
3284 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
3285 if (!object)
3287 FIXME("Failed to allocate pixel shader memory.\n");
3288 return E_OUTOFMEMORY;
3291 hr = pixelshader_init(object, device, byte_code);
3292 if (FAILED(hr))
3294 WARN("Failed to initialize pixel shader, hr %#x.\n", hr);
3295 HeapFree(GetProcessHeap(), 0, object);
3296 return hr;
3299 TRACE("Created pixel shader %p.\n", object);
3300 *shader = &object->IDirect3DPixelShader9_iface;
3302 return D3D_OK;
3305 static HRESULT WINAPI d3d9_device_SetPixelShader(IDirect3DDevice9Ex *iface, IDirect3DPixelShader9 *shader)
3307 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3308 struct d3d9_pixelshader *shader_obj = unsafe_impl_from_IDirect3DPixelShader9(shader);
3310 TRACE("iface %p, shader %p.\n", iface, shader);
3312 wined3d_mutex_lock();
3313 wined3d_device_set_pixel_shader(device->wined3d_device,
3314 shader_obj ? shader_obj->wined3d_shader : NULL);
3315 wined3d_mutex_unlock();
3317 return D3D_OK;
3320 static HRESULT WINAPI d3d9_device_GetPixelShader(IDirect3DDevice9Ex *iface, IDirect3DPixelShader9 **shader)
3322 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3323 struct d3d9_pixelshader *shader_impl;
3324 struct wined3d_shader *wined3d_shader;
3326 TRACE("iface %p, shader %p.\n", iface, shader);
3328 if (!shader) return D3DERR_INVALIDCALL;
3330 wined3d_mutex_lock();
3331 if ((wined3d_shader = wined3d_device_get_pixel_shader(device->wined3d_device)))
3333 shader_impl = wined3d_shader_get_parent(wined3d_shader);
3334 *shader = &shader_impl->IDirect3DPixelShader9_iface;
3335 IDirect3DPixelShader9_AddRef(*shader);
3337 else
3339 *shader = NULL;
3341 wined3d_mutex_unlock();
3343 TRACE("Returning %p.\n", *shader);
3345 return D3D_OK;
3348 static HRESULT WINAPI d3d9_device_SetPixelShaderConstantF(IDirect3DDevice9Ex *iface,
3349 UINT reg_idx, const float *data, UINT count)
3351 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3352 HRESULT hr;
3354 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3356 wined3d_mutex_lock();
3357 hr = wined3d_device_set_ps_consts_f(device->wined3d_device,
3358 reg_idx, count, (const struct wined3d_vec4 *)data);
3359 wined3d_mutex_unlock();
3361 return hr;
3364 static HRESULT WINAPI d3d9_device_GetPixelShaderConstantF(IDirect3DDevice9Ex *iface,
3365 UINT reg_idx, float *data, UINT count)
3367 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3368 HRESULT hr;
3370 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3372 wined3d_mutex_lock();
3373 hr = wined3d_device_get_ps_consts_f(device->wined3d_device,
3374 reg_idx, count, (struct wined3d_vec4 *)data);
3375 wined3d_mutex_unlock();
3377 return hr;
3380 static HRESULT WINAPI d3d9_device_SetPixelShaderConstantI(IDirect3DDevice9Ex *iface,
3381 UINT reg_idx, const int *data, UINT count)
3383 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3384 HRESULT hr;
3386 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3388 wined3d_mutex_lock();
3389 hr = wined3d_device_set_ps_consts_i(device->wined3d_device,
3390 reg_idx, count, (const struct wined3d_ivec4 *)data);
3391 wined3d_mutex_unlock();
3393 return hr;
3396 static HRESULT WINAPI d3d9_device_GetPixelShaderConstantI(IDirect3DDevice9Ex *iface,
3397 UINT reg_idx, int *data, UINT count)
3399 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3400 HRESULT hr;
3402 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3404 wined3d_mutex_lock();
3405 hr = wined3d_device_get_ps_consts_i(device->wined3d_device,
3406 reg_idx, count, (struct wined3d_ivec4 *)data);
3407 wined3d_mutex_unlock();
3409 return hr;
3412 static HRESULT WINAPI d3d9_device_SetPixelShaderConstantB(IDirect3DDevice9Ex *iface,
3413 UINT reg_idx, const BOOL *data, UINT count)
3415 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3416 HRESULT hr;
3418 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3420 wined3d_mutex_lock();
3421 hr = wined3d_device_set_ps_consts_b(device->wined3d_device, reg_idx, count, data);
3422 wined3d_mutex_unlock();
3424 return hr;
3427 static HRESULT WINAPI d3d9_device_GetPixelShaderConstantB(IDirect3DDevice9Ex *iface,
3428 UINT reg_idx, BOOL *data, UINT count)
3430 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3431 HRESULT hr;
3433 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3435 wined3d_mutex_lock();
3436 hr = wined3d_device_get_ps_consts_b(device->wined3d_device, reg_idx, count, data);
3437 wined3d_mutex_unlock();
3439 return hr;
3442 static HRESULT WINAPI d3d9_device_DrawRectPatch(IDirect3DDevice9Ex *iface, UINT handle,
3443 const float *segment_count, const D3DRECTPATCH_INFO *patch_info)
3445 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
3446 iface, handle, segment_count, patch_info);
3447 return D3D_OK;
3450 static HRESULT WINAPI d3d9_device_DrawTriPatch(IDirect3DDevice9Ex *iface, UINT handle,
3451 const float *segment_count, const D3DTRIPATCH_INFO *patch_info)
3453 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
3454 iface, handle, segment_count, patch_info);
3455 return D3D_OK;
3458 static HRESULT WINAPI d3d9_device_DeletePatch(IDirect3DDevice9Ex *iface, UINT handle)
3460 FIXME("iface %p, handle %#x unimplemented.\n", iface, handle);
3461 return D3DERR_INVALIDCALL;
3464 static HRESULT WINAPI d3d9_device_CreateQuery(IDirect3DDevice9Ex *iface, D3DQUERYTYPE type, IDirect3DQuery9 **query)
3466 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3467 struct d3d9_query *object;
3468 HRESULT hr;
3470 TRACE("iface %p, type %#x, query %p.\n", iface, type, query);
3472 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
3473 if (!object)
3474 return E_OUTOFMEMORY;
3476 hr = query_init(object, device, type);
3477 if (FAILED(hr))
3479 WARN("Failed to initialize query, hr %#x.\n", hr);
3480 HeapFree(GetProcessHeap(), 0, object);
3481 return hr;
3484 TRACE("Created query %p.\n", object);
3485 if (query) *query = &object->IDirect3DQuery9_iface;
3486 else IDirect3DQuery9_Release(&object->IDirect3DQuery9_iface);
3488 return D3D_OK;
3491 static HRESULT WINAPI d3d9_device_SetConvolutionMonoKernel(IDirect3DDevice9Ex *iface,
3492 UINT width, UINT height, float *rows, float *columns)
3494 FIXME("iface %p, width %u, height %u, rows %p, columns %p stub!\n",
3495 iface, width, height, rows, columns);
3497 return E_NOTIMPL;
3500 static HRESULT WINAPI d3d9_device_ComposeRects(IDirect3DDevice9Ex *iface,
3501 IDirect3DSurface9 *src_surface, IDirect3DSurface9 *dst_surface, IDirect3DVertexBuffer9 *src_descs,
3502 UINT rect_count, IDirect3DVertexBuffer9 *dst_descs, D3DCOMPOSERECTSOP operation, INT offset_x, INT offset_y)
3504 FIXME("iface %p, src_surface %p, dst_surface %p, src_descs %p, rect_count %u, "
3505 "dst_descs %p, operation %#x, offset_x %u, offset_y %u stub!\n",
3506 iface, src_surface, dst_surface, src_descs, rect_count,
3507 dst_descs, operation, offset_x, offset_y);
3509 return E_NOTIMPL;
3512 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_PresentEx(IDirect3DDevice9Ex *iface,
3513 const RECT *src_rect, const RECT *dst_rect, HWND dst_window_override,
3514 const RGNDATA *dirty_region, DWORD flags)
3516 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3517 UINT i;
3518 HRESULT hr;
3520 TRACE("iface %p, src_rect %s, dst_rect %s, dst_window_override %p, dirty_region %p, flags %#x.\n",
3521 iface, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect),
3522 dst_window_override, dirty_region, flags);
3524 if (device->device_state != D3D9_DEVICE_STATE_OK)
3525 return S_PRESENT_OCCLUDED;
3527 if (dirty_region)
3528 FIXME("Ignoring dirty_region %p.\n", dirty_region);
3530 wined3d_mutex_lock();
3531 for (i = 0; i < device->implicit_swapchain_count; ++i)
3533 if (FAILED(hr = wined3d_swapchain_present(device->implicit_swapchains[i]->wined3d_swapchain,
3534 src_rect, dst_rect, dst_window_override, flags)))
3536 wined3d_mutex_unlock();
3537 return hr;
3540 wined3d_mutex_unlock();
3542 return D3D_OK;
3545 static HRESULT WINAPI d3d9_device_GetGPUThreadPriority(IDirect3DDevice9Ex *iface, INT *priority)
3547 FIXME("iface %p, priority %p stub!\n", iface, priority);
3549 return E_NOTIMPL;
3552 static HRESULT WINAPI d3d9_device_SetGPUThreadPriority(IDirect3DDevice9Ex *iface, INT priority)
3554 FIXME("iface %p, priority %d stub!\n", iface, priority);
3556 return E_NOTIMPL;
3559 static HRESULT WINAPI d3d9_device_WaitForVBlank(IDirect3DDevice9Ex *iface, UINT swapchain_idx)
3561 FIXME("iface %p, swapchain_idx %u stub!\n", iface, swapchain_idx);
3563 return E_NOTIMPL;
3566 static HRESULT WINAPI d3d9_device_CheckResourceResidency(IDirect3DDevice9Ex *iface,
3567 IDirect3DResource9 **resources, UINT32 resource_count)
3569 FIXME("iface %p, resources %p, resource_count %u stub!\n",
3570 iface, resources, resource_count);
3572 return E_NOTIMPL;
3575 static HRESULT WINAPI d3d9_device_SetMaximumFrameLatency(IDirect3DDevice9Ex *iface, UINT max_latency)
3577 TRACE("iface %p, max_latency %u.\n", iface, max_latency);
3579 if (max_latency)
3580 FIXME("Ignoring max_latency %u.\n", max_latency);
3582 return S_OK;
3585 static HRESULT WINAPI d3d9_device_GetMaximumFrameLatency(IDirect3DDevice9Ex *iface, UINT *max_latency)
3587 FIXME("iface %p, max_latency %p stub!\n", iface, max_latency);
3589 *max_latency = 2;
3591 return E_NOTIMPL;
3594 static HRESULT WINAPI d3d9_device_CheckDeviceState(IDirect3DDevice9Ex *iface, HWND dst_window)
3596 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3597 struct wined3d_swapchain_desc swapchain_desc;
3599 TRACE("iface %p, dst_window %p.\n", iface, dst_window);
3601 wined3d_mutex_lock();
3602 wined3d_swapchain_get_desc(device->implicit_swapchains[0]->wined3d_swapchain, &swapchain_desc);
3603 wined3d_mutex_unlock();
3605 if (swapchain_desc.windowed)
3606 return D3D_OK;
3608 /* FIXME: This is actually supposed to check if any other device is in
3609 * fullscreen mode. */
3610 if (dst_window != swapchain_desc.device_window)
3611 return device->device_state == D3D9_DEVICE_STATE_OK ? S_PRESENT_OCCLUDED : D3D_OK;
3613 return device->device_state == D3D9_DEVICE_STATE_OK ? D3D_OK : S_PRESENT_OCCLUDED;
3616 static HRESULT WINAPI d3d9_device_CreateRenderTargetEx(IDirect3DDevice9Ex *iface,
3617 UINT width, UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
3618 BOOL lockable, IDirect3DSurface9 **surface, HANDLE *shared_handle, DWORD usage)
3620 FIXME("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u, "
3621 "lockable %#x, surface %p, shared_handle %p, usage %#x stub!\n",
3622 iface, width, height, format, multisample_type, multisample_quality,
3623 lockable, surface, shared_handle, usage);
3625 *surface = NULL;
3626 if (shared_handle)
3627 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
3629 return E_NOTIMPL;
3632 static HRESULT WINAPI d3d9_device_CreateOffscreenPlainSurfaceEx(IDirect3DDevice9Ex *iface,
3633 UINT width, UINT height, D3DFORMAT format, D3DPOOL pool, IDirect3DSurface9 **surface,
3634 HANDLE *shared_handle, DWORD usage)
3636 FIXME("iface %p, width %u, height %u, format %#x, pool %#x, surface %p, shared_handle %p, usage %#x stub!\n",
3637 iface, width, height, format, pool, surface, shared_handle, usage);
3639 return E_NOTIMPL;
3642 static HRESULT WINAPI d3d9_device_CreateDepthStencilSurfaceEx(IDirect3DDevice9Ex *iface,
3643 UINT width, UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
3644 BOOL discard, IDirect3DSurface9 **surface, HANDLE *shared_handle, DWORD usage)
3646 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3647 DWORD flags = WINED3D_TEXTURE_CREATE_MAPPABLE;
3649 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u, "
3650 "discard %#x, surface %p, shared_handle %p, usage %#x.\n",
3651 iface, width, height, format, multisample_type, multisample_quality,
3652 discard, surface, shared_handle, usage);
3654 if (usage & D3DUSAGE_DEPTHSTENCIL)
3656 WARN("Invalid usage %#x.\n", usage);
3657 return D3DERR_INVALIDCALL;
3660 if (shared_handle)
3661 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
3663 if (discard)
3664 flags |= WINED3D_TEXTURE_CREATE_DISCARD;
3666 *surface = NULL;
3667 return d3d9_device_create_surface(device, width, height, format, flags, surface,
3668 D3DUSAGE_DEPTHSTENCIL | usage, D3DPOOL_DEFAULT, multisample_type, multisample_quality, NULL);
3671 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_ResetEx(IDirect3DDevice9Ex *iface,
3672 D3DPRESENT_PARAMETERS *present_parameters, D3DDISPLAYMODEEX *mode)
3674 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3676 TRACE("iface %p, present_parameters %p, mode %p.\n", iface, present_parameters, mode);
3678 if (!present_parameters->Windowed == !mode)
3680 WARN("Mode can be passed if and only if Windowed is FALSE.\n");
3681 return D3DERR_INVALIDCALL;
3684 if (mode && (mode->Width != present_parameters->BackBufferWidth
3685 || mode->Height != present_parameters->BackBufferHeight))
3687 WARN("Mode and back buffer mismatch (mode %ux%u, backbuffer %ux%u).\n",
3688 mode->Width, mode->Height,
3689 present_parameters->BackBufferWidth, present_parameters->BackBufferHeight);
3690 return D3DERR_INVALIDCALL;
3693 return d3d9_device_reset(device, present_parameters, mode);
3696 static HRESULT WINAPI d3d9_device_GetDisplayModeEx(IDirect3DDevice9Ex *iface,
3697 UINT swapchain_idx, D3DDISPLAYMODEEX *mode, D3DDISPLAYROTATION *rotation)
3699 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3700 struct wined3d_display_mode wined3d_mode;
3701 HRESULT hr;
3703 TRACE("iface %p, swapchain_idx %u, mode %p, rotation %p.\n",
3704 iface, swapchain_idx, mode, rotation);
3706 if (mode->Size != sizeof(*mode))
3707 return D3DERR_INVALIDCALL;
3709 wined3d_mutex_lock();
3710 hr = wined3d_device_get_display_mode(device->wined3d_device, swapchain_idx, &wined3d_mode,
3711 (enum wined3d_display_rotation *)rotation);
3712 wined3d_mutex_unlock();
3714 if (SUCCEEDED(hr))
3716 mode->Width = wined3d_mode.width;
3717 mode->Height = wined3d_mode.height;
3718 mode->RefreshRate = wined3d_mode.refresh_rate;
3719 mode->Format = d3dformat_from_wined3dformat(wined3d_mode.format_id);
3720 mode->ScanLineOrdering = wined3d_mode.scanline_ordering;
3723 return hr;
3726 static const struct IDirect3DDevice9ExVtbl d3d9_device_vtbl =
3728 /* IUnknown */
3729 d3d9_device_QueryInterface,
3730 d3d9_device_AddRef,
3731 d3d9_device_Release,
3732 /* IDirect3DDevice9 */
3733 d3d9_device_TestCooperativeLevel,
3734 d3d9_device_GetAvailableTextureMem,
3735 d3d9_device_EvictManagedResources,
3736 d3d9_device_GetDirect3D,
3737 d3d9_device_GetDeviceCaps,
3738 d3d9_device_GetDisplayMode,
3739 d3d9_device_GetCreationParameters,
3740 d3d9_device_SetCursorProperties,
3741 d3d9_device_SetCursorPosition,
3742 d3d9_device_ShowCursor,
3743 d3d9_device_CreateAdditionalSwapChain,
3744 d3d9_device_GetSwapChain,
3745 d3d9_device_GetNumberOfSwapChains,
3746 d3d9_device_Reset,
3747 d3d9_device_Present,
3748 d3d9_device_GetBackBuffer,
3749 d3d9_device_GetRasterStatus,
3750 d3d9_device_SetDialogBoxMode,
3751 d3d9_device_SetGammaRamp,
3752 d3d9_device_GetGammaRamp,
3753 d3d9_device_CreateTexture,
3754 d3d9_device_CreateVolumeTexture,
3755 d3d9_device_CreateCubeTexture,
3756 d3d9_device_CreateVertexBuffer,
3757 d3d9_device_CreateIndexBuffer,
3758 d3d9_device_CreateRenderTarget,
3759 d3d9_device_CreateDepthStencilSurface,
3760 d3d9_device_UpdateSurface,
3761 d3d9_device_UpdateTexture,
3762 d3d9_device_GetRenderTargetData,
3763 d3d9_device_GetFrontBufferData,
3764 d3d9_device_StretchRect,
3765 d3d9_device_ColorFill,
3766 d3d9_device_CreateOffscreenPlainSurface,
3767 d3d9_device_SetRenderTarget,
3768 d3d9_device_GetRenderTarget,
3769 d3d9_device_SetDepthStencilSurface,
3770 d3d9_device_GetDepthStencilSurface,
3771 d3d9_device_BeginScene,
3772 d3d9_device_EndScene,
3773 d3d9_device_Clear,
3774 d3d9_device_SetTransform,
3775 d3d9_device_GetTransform,
3776 d3d9_device_MultiplyTransform,
3777 d3d9_device_SetViewport,
3778 d3d9_device_GetViewport,
3779 d3d9_device_SetMaterial,
3780 d3d9_device_GetMaterial,
3781 d3d9_device_SetLight,
3782 d3d9_device_GetLight,
3783 d3d9_device_LightEnable,
3784 d3d9_device_GetLightEnable,
3785 d3d9_device_SetClipPlane,
3786 d3d9_device_GetClipPlane,
3787 d3d9_device_SetRenderState,
3788 d3d9_device_GetRenderState,
3789 d3d9_device_CreateStateBlock,
3790 d3d9_device_BeginStateBlock,
3791 d3d9_device_EndStateBlock,
3792 d3d9_device_SetClipStatus,
3793 d3d9_device_GetClipStatus,
3794 d3d9_device_GetTexture,
3795 d3d9_device_SetTexture,
3796 d3d9_device_GetTextureStageState,
3797 d3d9_device_SetTextureStageState,
3798 d3d9_device_GetSamplerState,
3799 d3d9_device_SetSamplerState,
3800 d3d9_device_ValidateDevice,
3801 d3d9_device_SetPaletteEntries,
3802 d3d9_device_GetPaletteEntries,
3803 d3d9_device_SetCurrentTexturePalette,
3804 d3d9_device_GetCurrentTexturePalette,
3805 d3d9_device_SetScissorRect,
3806 d3d9_device_GetScissorRect,
3807 d3d9_device_SetSoftwareVertexProcessing,
3808 d3d9_device_GetSoftwareVertexProcessing,
3809 d3d9_device_SetNPatchMode,
3810 d3d9_device_GetNPatchMode,
3811 d3d9_device_DrawPrimitive,
3812 d3d9_device_DrawIndexedPrimitive,
3813 d3d9_device_DrawPrimitiveUP,
3814 d3d9_device_DrawIndexedPrimitiveUP,
3815 d3d9_device_ProcessVertices,
3816 d3d9_device_CreateVertexDeclaration,
3817 d3d9_device_SetVertexDeclaration,
3818 d3d9_device_GetVertexDeclaration,
3819 d3d9_device_SetFVF,
3820 d3d9_device_GetFVF,
3821 d3d9_device_CreateVertexShader,
3822 d3d9_device_SetVertexShader,
3823 d3d9_device_GetVertexShader,
3824 d3d9_device_SetVertexShaderConstantF,
3825 d3d9_device_GetVertexShaderConstantF,
3826 d3d9_device_SetVertexShaderConstantI,
3827 d3d9_device_GetVertexShaderConstantI,
3828 d3d9_device_SetVertexShaderConstantB,
3829 d3d9_device_GetVertexShaderConstantB,
3830 d3d9_device_SetStreamSource,
3831 d3d9_device_GetStreamSource,
3832 d3d9_device_SetStreamSourceFreq,
3833 d3d9_device_GetStreamSourceFreq,
3834 d3d9_device_SetIndices,
3835 d3d9_device_GetIndices,
3836 d3d9_device_CreatePixelShader,
3837 d3d9_device_SetPixelShader,
3838 d3d9_device_GetPixelShader,
3839 d3d9_device_SetPixelShaderConstantF,
3840 d3d9_device_GetPixelShaderConstantF,
3841 d3d9_device_SetPixelShaderConstantI,
3842 d3d9_device_GetPixelShaderConstantI,
3843 d3d9_device_SetPixelShaderConstantB,
3844 d3d9_device_GetPixelShaderConstantB,
3845 d3d9_device_DrawRectPatch,
3846 d3d9_device_DrawTriPatch,
3847 d3d9_device_DeletePatch,
3848 d3d9_device_CreateQuery,
3849 /* IDirect3DDevice9Ex */
3850 d3d9_device_SetConvolutionMonoKernel,
3851 d3d9_device_ComposeRects,
3852 d3d9_device_PresentEx,
3853 d3d9_device_GetGPUThreadPriority,
3854 d3d9_device_SetGPUThreadPriority,
3855 d3d9_device_WaitForVBlank,
3856 d3d9_device_CheckResourceResidency,
3857 d3d9_device_SetMaximumFrameLatency,
3858 d3d9_device_GetMaximumFrameLatency,
3859 d3d9_device_CheckDeviceState,
3860 d3d9_device_CreateRenderTargetEx,
3861 d3d9_device_CreateOffscreenPlainSurfaceEx,
3862 d3d9_device_CreateDepthStencilSurfaceEx,
3863 d3d9_device_ResetEx,
3864 d3d9_device_GetDisplayModeEx,
3867 static inline struct d3d9_device *device_from_device_parent(struct wined3d_device_parent *device_parent)
3869 return CONTAINING_RECORD(device_parent, struct d3d9_device, device_parent);
3872 static void CDECL device_parent_wined3d_device_created(struct wined3d_device_parent *device_parent,
3873 struct wined3d_device *device)
3875 TRACE("device_parent %p, device %p.\n", device_parent, device);
3878 static void CDECL device_parent_mode_changed(struct wined3d_device_parent *device_parent)
3880 TRACE("device_parent %p.\n", device_parent);
3883 static void CDECL device_parent_activate(struct wined3d_device_parent *device_parent, BOOL activate)
3885 struct d3d9_device *device = device_from_device_parent(device_parent);
3887 TRACE("device_parent %p, activate %#x.\n", device_parent, activate);
3889 if (!device->d3d_parent)
3890 return;
3892 if (!activate)
3893 InterlockedCompareExchange(&device->device_state, D3D9_DEVICE_STATE_LOST, D3D9_DEVICE_STATE_OK);
3894 else if (device->d3d_parent->extended)
3895 InterlockedCompareExchange(&device->device_state, D3D9_DEVICE_STATE_OK, D3D9_DEVICE_STATE_LOST);
3896 else
3897 InterlockedCompareExchange(&device->device_state, D3D9_DEVICE_STATE_NOT_RESET, D3D9_DEVICE_STATE_LOST);
3900 static HRESULT CDECL device_parent_surface_created(struct wined3d_device_parent *device_parent,
3901 struct wined3d_texture *wined3d_texture, unsigned int sub_resource_idx,
3902 void **parent, const struct wined3d_parent_ops **parent_ops)
3904 struct d3d9_surface *d3d_surface;
3906 TRACE("device_parent %p, wined3d_texture %p, sub_resource_idx %u, parent %p, parent_ops %p.\n",
3907 device_parent, wined3d_texture, sub_resource_idx, parent, parent_ops);
3909 if (!(d3d_surface = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*d3d_surface))))
3910 return E_OUTOFMEMORY;
3912 surface_init(d3d_surface, wined3d_texture, sub_resource_idx, parent_ops);
3913 *parent = d3d_surface;
3914 TRACE("Created surface %p.\n", d3d_surface);
3916 return D3D_OK;
3919 static HRESULT CDECL device_parent_volume_created(struct wined3d_device_parent *device_parent,
3920 struct wined3d_texture *wined3d_texture, unsigned int sub_resource_idx,
3921 void **parent, const struct wined3d_parent_ops **parent_ops)
3923 struct d3d9_volume *d3d_volume;
3925 TRACE("device_parent %p, texture %p, sub_resource_idx %u, parent %p, parent_ops %p.\n",
3926 device_parent, wined3d_texture, sub_resource_idx, parent, parent_ops);
3928 if (!(d3d_volume = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*d3d_volume))))
3929 return E_OUTOFMEMORY;
3931 volume_init(d3d_volume, wined3d_texture, sub_resource_idx, parent_ops);
3932 *parent = d3d_volume;
3933 TRACE("Created volume %p.\n", d3d_volume);
3935 return D3D_OK;
3938 static HRESULT CDECL device_parent_create_swapchain_texture(struct wined3d_device_parent *device_parent,
3939 void *container_parent, const struct wined3d_resource_desc *desc, DWORD texture_flags,
3940 struct wined3d_texture **texture)
3942 struct d3d9_device *device = device_from_device_parent(device_parent);
3943 struct d3d9_surface *d3d_surface;
3944 HRESULT hr;
3946 TRACE("device_parent %p, container_parent %p, desc %p, texture flags %#x, texture %p.\n",
3947 device_parent, container_parent, desc, texture_flags, texture);
3949 if (container_parent == device_parent)
3950 container_parent = &device->IDirect3DDevice9Ex_iface;
3952 if (is_gdi_compat_wined3dformat(desc->format))
3953 texture_flags |= WINED3D_TEXTURE_CREATE_GET_DC;
3955 if (FAILED(hr = wined3d_texture_create(device->wined3d_device, desc, 1, 1,
3956 texture_flags | WINED3D_TEXTURE_CREATE_MAPPABLE, NULL, container_parent,
3957 &d3d9_null_wined3d_parent_ops, texture)))
3959 WARN("Failed to create texture, hr %#x.\n", hr);
3960 return hr;
3963 d3d_surface = wined3d_texture_get_sub_resource_parent(*texture, 0);
3964 d3d_surface->parent_device = &device->IDirect3DDevice9Ex_iface;
3966 return hr;
3969 static HRESULT CDECL device_parent_create_swapchain(struct wined3d_device_parent *device_parent,
3970 struct wined3d_swapchain_desc *desc, struct wined3d_swapchain **swapchain)
3972 struct d3d9_device *device = device_from_device_parent(device_parent);
3973 struct d3d9_swapchain *d3d_swapchain;
3974 HRESULT hr;
3976 TRACE("device_parent %p, desc %p, swapchain %p\n", device_parent, desc, swapchain);
3978 hr = d3d9_swapchain_create(device, desc, &d3d_swapchain);
3979 if (FAILED(hr))
3981 WARN("Failed to create swapchain, hr %#x.\n", hr);
3982 *swapchain = NULL;
3983 return hr;
3986 *swapchain = d3d_swapchain->wined3d_swapchain;
3987 wined3d_swapchain_incref(*swapchain);
3988 IDirect3DSwapChain9Ex_Release(&d3d_swapchain->IDirect3DSwapChain9Ex_iface);
3990 return hr;
3993 static const struct wined3d_device_parent_ops d3d9_wined3d_device_parent_ops =
3995 device_parent_wined3d_device_created,
3996 device_parent_mode_changed,
3997 device_parent_activate,
3998 device_parent_surface_created,
3999 device_parent_volume_created,
4000 device_parent_create_swapchain_texture,
4001 device_parent_create_swapchain,
4004 static void setup_fpu(void)
4006 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
4007 WORD cw;
4008 __asm__ volatile ("fnstcw %0" : "=m" (cw));
4009 cw = (cw & ~0xf3f) | 0x3f;
4010 __asm__ volatile ("fldcw %0" : : "m" (cw));
4011 #elif defined(__i386__) && defined(_MSC_VER)
4012 WORD cw;
4013 __asm fnstcw cw;
4014 cw = (cw & ~0xf3f) | 0x3f;
4015 __asm fldcw cw;
4016 #else
4017 FIXME("FPU setup not implemented for this platform.\n");
4018 #endif
4021 HRESULT device_init(struct d3d9_device *device, struct d3d9 *parent, struct wined3d *wined3d,
4022 UINT adapter, D3DDEVTYPE device_type, HWND focus_window, DWORD flags,
4023 D3DPRESENT_PARAMETERS *parameters, D3DDISPLAYMODEEX *mode)
4025 struct wined3d_swapchain_desc *swapchain_desc;
4026 unsigned i, count = 1;
4027 WINED3DCAPS caps;
4028 HRESULT hr;
4030 if (mode)
4031 FIXME("Ignoring display mode.\n");
4033 device->IDirect3DDevice9Ex_iface.lpVtbl = &d3d9_device_vtbl;
4034 device->device_parent.ops = &d3d9_wined3d_device_parent_ops;
4035 device->refcount = 1;
4037 if (!(flags & D3DCREATE_FPU_PRESERVE)) setup_fpu();
4039 wined3d_mutex_lock();
4040 if (FAILED(hr = wined3d_device_create(wined3d, adapter, device_type, focus_window, flags, 4,
4041 &device->device_parent, &device->wined3d_device)))
4043 WARN("Failed to create wined3d device, hr %#x.\n", hr);
4044 wined3d_mutex_unlock();
4045 return hr;
4048 wined3d_get_device_caps(wined3d, adapter, device_type, &caps);
4049 device->max_user_clip_planes = caps.MaxUserClipPlanes;
4050 if (flags & D3DCREATE_ADAPTERGROUP_DEVICE)
4051 count = caps.NumberOfAdaptersInGroup;
4053 if (flags & D3DCREATE_MULTITHREADED)
4054 wined3d_device_set_multithreaded(device->wined3d_device);
4056 if (!parameters->Windowed)
4058 if (!focus_window)
4059 focus_window = parameters->hDeviceWindow;
4060 if (FAILED(hr = wined3d_device_acquire_focus_window(device->wined3d_device, focus_window)))
4062 ERR("Failed to acquire focus window, hr %#x.\n", hr);
4063 wined3d_device_decref(device->wined3d_device);
4064 wined3d_mutex_unlock();
4065 return hr;
4068 for (i = 0; i < count; ++i)
4070 HWND device_window = parameters[i].hDeviceWindow;
4072 if (!device_window) device_window = focus_window;
4073 wined3d_device_setup_fullscreen_window(device->wined3d_device, device_window,
4074 parameters[i].BackBufferWidth,
4075 parameters[i].BackBufferHeight);
4079 swapchain_desc = HeapAlloc(GetProcessHeap(), 0, sizeof(*swapchain_desc) * count);
4080 if (!swapchain_desc)
4082 ERR("Failed to allocate wined3d parameters.\n");
4083 wined3d_device_release_focus_window(device->wined3d_device);
4084 wined3d_device_decref(device->wined3d_device);
4085 wined3d_mutex_unlock();
4086 return E_OUTOFMEMORY;
4089 for (i = 0; i < count; ++i)
4091 if (!wined3d_swapchain_desc_from_present_parameters(&swapchain_desc[i], &parameters[i],
4092 parent->extended))
4094 wined3d_device_release_focus_window(device->wined3d_device);
4095 wined3d_device_decref(device->wined3d_device);
4096 HeapFree(GetProcessHeap(), 0, swapchain_desc);
4097 wined3d_mutex_unlock();
4098 return D3DERR_INVALIDCALL;
4102 if (FAILED(hr = wined3d_device_init_3d(device->wined3d_device, swapchain_desc)))
4104 WARN("Failed to initialize 3D, hr %#x.\n", hr);
4105 wined3d_device_release_focus_window(device->wined3d_device);
4106 HeapFree(GetProcessHeap(), 0, swapchain_desc);
4107 wined3d_device_decref(device->wined3d_device);
4108 wined3d_mutex_unlock();
4109 return hr;
4112 wined3d_device_set_render_state(device->wined3d_device,
4113 WINED3D_RS_ZENABLE, !!swapchain_desc->enable_auto_depth_stencil);
4115 if (FAILED(hr = d3d9_device_get_swapchains(device)))
4117 wined3d_device_uninit_3d(device->wined3d_device);
4118 wined3d_device_release_focus_window(device->wined3d_device);
4119 wined3d_device_decref(device->wined3d_device);
4120 wined3d_mutex_unlock();
4121 return E_OUTOFMEMORY;
4124 for (i = 0; i < count; ++i)
4126 present_parameters_from_wined3d_swapchain_desc(&parameters[i], &swapchain_desc[i]);
4129 wined3d_mutex_unlock();
4131 HeapFree(GetProcessHeap(), 0, swapchain_desc);
4133 /* Initialize the converted declaration array. This creates a valid pointer
4134 * and when adding decls HeapReAlloc() can be used without further checking. */
4135 device->fvf_decls = HeapAlloc(GetProcessHeap(), 0, 0);
4136 if (!device->fvf_decls)
4138 ERR("Failed to allocate FVF vertex declaration map memory.\n");
4139 wined3d_mutex_lock();
4140 HeapFree(GetProcessHeap(), 0, device->implicit_swapchains);
4141 wined3d_device_uninit_3d(device->wined3d_device);
4142 wined3d_device_release_focus_window(device->wined3d_device);
4143 wined3d_device_decref(device->wined3d_device);
4144 wined3d_mutex_unlock();
4145 return E_OUTOFMEMORY;
4148 IDirect3D9Ex_AddRef(&parent->IDirect3D9Ex_iface);
4149 device->d3d_parent = parent;
4151 return D3D_OK;