wined3d: Clamp NULL source boxes in wined3d_device_copy_sub_resource_region().
[wine.git] / dlls / d3d9 / device.c
blobf6086bf77a2befae8fb2d9881831818db9b1c112
1 /*
2 * IDirect3DDevice9 implementation
4 * Copyright 2002-2005 Jason Edmeades
5 * Copyright 2002-2005 Raphael Junqueira
6 * Copyright 2005 Oliver Stieber
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "config.h"
24 #include "d3d9_private.h"
26 WINE_DEFAULT_DEBUG_CHANNEL(d3d9);
28 static void STDMETHODCALLTYPE d3d9_null_wined3d_object_destroyed(void *parent) {}
30 const struct wined3d_parent_ops d3d9_null_wined3d_parent_ops =
32 d3d9_null_wined3d_object_destroyed,
35 D3DFORMAT d3dformat_from_wined3dformat(enum wined3d_format_id format)
37 BYTE *c = (BYTE *)&format;
39 /* Don't translate FOURCC formats */
40 if (isprint(c[0]) && isprint(c[1]) && isprint(c[2]) && isprint(c[3])) return format;
42 switch(format)
44 case WINED3DFMT_UNKNOWN: return D3DFMT_UNKNOWN;
45 case WINED3DFMT_B8G8R8_UNORM: return D3DFMT_R8G8B8;
46 case WINED3DFMT_B8G8R8A8_UNORM: return D3DFMT_A8R8G8B8;
47 case WINED3DFMT_B8G8R8X8_UNORM: return D3DFMT_X8R8G8B8;
48 case WINED3DFMT_B5G6R5_UNORM: return D3DFMT_R5G6B5;
49 case WINED3DFMT_B5G5R5X1_UNORM: return D3DFMT_X1R5G5B5;
50 case WINED3DFMT_B5G5R5A1_UNORM: return D3DFMT_A1R5G5B5;
51 case WINED3DFMT_B4G4R4A4_UNORM: return D3DFMT_A4R4G4B4;
52 case WINED3DFMT_B2G3R3_UNORM: return D3DFMT_R3G3B2;
53 case WINED3DFMT_A8_UNORM: return D3DFMT_A8;
54 case WINED3DFMT_B2G3R3A8_UNORM: return D3DFMT_A8R3G3B2;
55 case WINED3DFMT_B4G4R4X4_UNORM: return D3DFMT_X4R4G4B4;
56 case WINED3DFMT_R10G10B10A2_UNORM: return D3DFMT_A2B10G10R10;
57 case WINED3DFMT_R8G8B8A8_UNORM: return D3DFMT_A8B8G8R8;
58 case WINED3DFMT_R8G8B8X8_UNORM: return D3DFMT_X8B8G8R8;
59 case WINED3DFMT_R16G16_UNORM: return D3DFMT_G16R16;
60 case WINED3DFMT_B10G10R10A2_UNORM: return D3DFMT_A2R10G10B10;
61 case WINED3DFMT_R16G16B16A16_UNORM: return D3DFMT_A16B16G16R16;
62 case WINED3DFMT_P8_UINT_A8_UNORM: return D3DFMT_A8P8;
63 case WINED3DFMT_P8_UINT: return D3DFMT_P8;
64 case WINED3DFMT_L8_UNORM: return D3DFMT_L8;
65 case WINED3DFMT_L8A8_UNORM: return D3DFMT_A8L8;
66 case WINED3DFMT_L4A4_UNORM: return D3DFMT_A4L4;
67 case WINED3DFMT_R8G8_SNORM: return D3DFMT_V8U8;
68 case WINED3DFMT_R5G5_SNORM_L6_UNORM: return D3DFMT_L6V5U5;
69 case WINED3DFMT_R8G8_SNORM_L8X8_UNORM: return D3DFMT_X8L8V8U8;
70 case WINED3DFMT_R8G8B8A8_SNORM: return D3DFMT_Q8W8V8U8;
71 case WINED3DFMT_R16G16_SNORM: return D3DFMT_V16U16;
72 case WINED3DFMT_R10G10B10_SNORM_A2_UNORM: return D3DFMT_A2W10V10U10;
73 case WINED3DFMT_D16_LOCKABLE: return D3DFMT_D16_LOCKABLE;
74 case WINED3DFMT_D32_UNORM: return D3DFMT_D32;
75 case WINED3DFMT_S1_UINT_D15_UNORM: return D3DFMT_D15S1;
76 case WINED3DFMT_D24_UNORM_S8_UINT: return D3DFMT_D24S8;
77 case WINED3DFMT_X8D24_UNORM: return D3DFMT_D24X8;
78 case WINED3DFMT_S4X4_UINT_D24_UNORM: return D3DFMT_D24X4S4;
79 case WINED3DFMT_D16_UNORM: return D3DFMT_D16;
80 case WINED3DFMT_L16_UNORM: return D3DFMT_L16;
81 case WINED3DFMT_D32_FLOAT: return D3DFMT_D32F_LOCKABLE;
82 case WINED3DFMT_S8_UINT_D24_FLOAT: return D3DFMT_D24FS8;
83 case WINED3DFMT_R16_UINT: return D3DFMT_INDEX16;
84 case WINED3DFMT_R32_UINT: return D3DFMT_INDEX32;
85 case WINED3DFMT_R16G16B16A16_SNORM: return D3DFMT_Q16W16V16U16;
86 case WINED3DFMT_R16_FLOAT: return D3DFMT_R16F;
87 case WINED3DFMT_R16G16_FLOAT: return D3DFMT_G16R16F;
88 case WINED3DFMT_R16G16B16A16_FLOAT: return D3DFMT_A16B16G16R16F;
89 case WINED3DFMT_R32_FLOAT: return D3DFMT_R32F;
90 case WINED3DFMT_R32G32_FLOAT: return D3DFMT_G32R32F;
91 case WINED3DFMT_R32G32B32A32_FLOAT: return D3DFMT_A32B32G32R32F;
92 case WINED3DFMT_R8G8_SNORM_Cx: return D3DFMT_CxV8U8;
93 default:
94 FIXME("Unhandled wined3d format %#x.\n", format);
95 return D3DFMT_UNKNOWN;
99 enum wined3d_format_id wined3dformat_from_d3dformat(D3DFORMAT format)
101 BYTE *c = (BYTE *)&format;
103 /* Don't translate FOURCC formats */
104 if (isprint(c[0]) && isprint(c[1]) && isprint(c[2]) && isprint(c[3])) return format;
106 switch(format)
108 case D3DFMT_UNKNOWN: return WINED3DFMT_UNKNOWN;
109 case D3DFMT_R8G8B8: return WINED3DFMT_B8G8R8_UNORM;
110 case D3DFMT_A8R8G8B8: return WINED3DFMT_B8G8R8A8_UNORM;
111 case D3DFMT_X8R8G8B8: return WINED3DFMT_B8G8R8X8_UNORM;
112 case D3DFMT_R5G6B5: return WINED3DFMT_B5G6R5_UNORM;
113 case D3DFMT_X1R5G5B5: return WINED3DFMT_B5G5R5X1_UNORM;
114 case D3DFMT_A1R5G5B5: return WINED3DFMT_B5G5R5A1_UNORM;
115 case D3DFMT_A4R4G4B4: return WINED3DFMT_B4G4R4A4_UNORM;
116 case D3DFMT_R3G3B2: return WINED3DFMT_B2G3R3_UNORM;
117 case D3DFMT_A8: return WINED3DFMT_A8_UNORM;
118 case D3DFMT_A8R3G3B2: return WINED3DFMT_B2G3R3A8_UNORM;
119 case D3DFMT_X4R4G4B4: return WINED3DFMT_B4G4R4X4_UNORM;
120 case D3DFMT_A2B10G10R10: return WINED3DFMT_R10G10B10A2_UNORM;
121 case D3DFMT_A8B8G8R8: return WINED3DFMT_R8G8B8A8_UNORM;
122 case D3DFMT_X8B8G8R8: return WINED3DFMT_R8G8B8X8_UNORM;
123 case D3DFMT_G16R16: return WINED3DFMT_R16G16_UNORM;
124 case D3DFMT_A2R10G10B10: return WINED3DFMT_B10G10R10A2_UNORM;
125 case D3DFMT_A16B16G16R16: return WINED3DFMT_R16G16B16A16_UNORM;
126 case D3DFMT_A8P8: return WINED3DFMT_P8_UINT_A8_UNORM;
127 case D3DFMT_P8: return WINED3DFMT_P8_UINT;
128 case D3DFMT_L8: return WINED3DFMT_L8_UNORM;
129 case D3DFMT_A8L8: return WINED3DFMT_L8A8_UNORM;
130 case D3DFMT_A4L4: return WINED3DFMT_L4A4_UNORM;
131 case D3DFMT_V8U8: return WINED3DFMT_R8G8_SNORM;
132 case D3DFMT_L6V5U5: return WINED3DFMT_R5G5_SNORM_L6_UNORM;
133 case D3DFMT_X8L8V8U8: return WINED3DFMT_R8G8_SNORM_L8X8_UNORM;
134 case D3DFMT_Q8W8V8U8: return WINED3DFMT_R8G8B8A8_SNORM;
135 case D3DFMT_V16U16: return WINED3DFMT_R16G16_SNORM;
136 case D3DFMT_A2W10V10U10: return WINED3DFMT_R10G10B10_SNORM_A2_UNORM;
137 case D3DFMT_D16_LOCKABLE: return WINED3DFMT_D16_LOCKABLE;
138 case D3DFMT_D32: return WINED3DFMT_D32_UNORM;
139 case D3DFMT_D15S1: return WINED3DFMT_S1_UINT_D15_UNORM;
140 case D3DFMT_D24S8: return WINED3DFMT_D24_UNORM_S8_UINT;
141 case D3DFMT_D24X8: return WINED3DFMT_X8D24_UNORM;
142 case D3DFMT_D24X4S4: return WINED3DFMT_S4X4_UINT_D24_UNORM;
143 case D3DFMT_D16: return WINED3DFMT_D16_UNORM;
144 case D3DFMT_L16: return WINED3DFMT_L16_UNORM;
145 case D3DFMT_D32F_LOCKABLE: return WINED3DFMT_D32_FLOAT;
146 case D3DFMT_D24FS8: return WINED3DFMT_S8_UINT_D24_FLOAT;
147 case D3DFMT_INDEX16: return WINED3DFMT_R16_UINT;
148 case D3DFMT_INDEX32: return WINED3DFMT_R32_UINT;
149 case D3DFMT_Q16W16V16U16: return WINED3DFMT_R16G16B16A16_SNORM;
150 case D3DFMT_R16F: return WINED3DFMT_R16_FLOAT;
151 case D3DFMT_G16R16F: return WINED3DFMT_R16G16_FLOAT;
152 case D3DFMT_A16B16G16R16F: return WINED3DFMT_R16G16B16A16_FLOAT;
153 case D3DFMT_R32F: return WINED3DFMT_R32_FLOAT;
154 case D3DFMT_G32R32F: return WINED3DFMT_R32G32_FLOAT;
155 case D3DFMT_A32B32G32R32F: return WINED3DFMT_R32G32B32A32_FLOAT;
156 case D3DFMT_CxV8U8: return WINED3DFMT_R8G8_SNORM_Cx;
157 default:
158 FIXME("Unhandled D3DFORMAT %#x.\n", format);
159 return WINED3DFMT_UNKNOWN;
163 static UINT vertex_count_from_primitive_count(D3DPRIMITIVETYPE primitive_type, UINT primitive_count)
165 switch (primitive_type)
167 case D3DPT_POINTLIST:
168 return primitive_count;
170 case D3DPT_LINELIST:
171 return primitive_count * 2;
173 case D3DPT_LINESTRIP:
174 return primitive_count + 1;
176 case D3DPT_TRIANGLELIST:
177 return primitive_count * 3;
179 case D3DPT_TRIANGLESTRIP:
180 case D3DPT_TRIANGLEFAN:
181 return primitive_count + 2;
183 default:
184 FIXME("Unhandled primitive type %#x.\n", primitive_type);
185 return 0;
189 void present_parameters_from_wined3d_swapchain_desc(D3DPRESENT_PARAMETERS *present_parameters,
190 const struct wined3d_swapchain_desc *swapchain_desc)
192 present_parameters->BackBufferWidth = swapchain_desc->backbuffer_width;
193 present_parameters->BackBufferHeight = swapchain_desc->backbuffer_height;
194 present_parameters->BackBufferFormat = d3dformat_from_wined3dformat(swapchain_desc->backbuffer_format);
195 present_parameters->BackBufferCount = swapchain_desc->backbuffer_count;
196 present_parameters->MultiSampleType = swapchain_desc->multisample_type;
197 present_parameters->MultiSampleQuality = swapchain_desc->multisample_quality;
198 present_parameters->SwapEffect = swapchain_desc->swap_effect;
199 present_parameters->hDeviceWindow = swapchain_desc->device_window;
200 present_parameters->Windowed = swapchain_desc->windowed;
201 present_parameters->EnableAutoDepthStencil = swapchain_desc->enable_auto_depth_stencil;
202 present_parameters->AutoDepthStencilFormat
203 = d3dformat_from_wined3dformat(swapchain_desc->auto_depth_stencil_format);
204 present_parameters->Flags = swapchain_desc->flags & D3DPRESENTFLAGS_MASK;
205 present_parameters->FullScreen_RefreshRateInHz = swapchain_desc->refresh_rate;
206 present_parameters->PresentationInterval = swapchain_desc->swap_interval;
209 static BOOL wined3d_swapchain_desc_from_present_parameters(struct wined3d_swapchain_desc *swapchain_desc,
210 const D3DPRESENT_PARAMETERS *present_parameters, BOOL extended)
212 D3DSWAPEFFECT highest_swapeffect = extended ? D3DSWAPEFFECT_FLIPEX : D3DSWAPEFFECT_COPY;
213 UINT highest_bb_count = extended ? 30 : 3;
215 if (!present_parameters->SwapEffect || present_parameters->SwapEffect > highest_swapeffect)
217 WARN("Invalid swap effect %u passed.\n", present_parameters->SwapEffect);
218 return FALSE;
220 if (present_parameters->BackBufferCount > highest_bb_count
221 || (present_parameters->SwapEffect == D3DSWAPEFFECT_COPY
222 && present_parameters->BackBufferCount > 1))
224 WARN("Invalid backbuffer count %u.\n", present_parameters->BackBufferCount);
225 return FALSE;
228 swapchain_desc->backbuffer_width = present_parameters->BackBufferWidth;
229 swapchain_desc->backbuffer_height = present_parameters->BackBufferHeight;
230 swapchain_desc->backbuffer_format = wined3dformat_from_d3dformat(present_parameters->BackBufferFormat);
231 swapchain_desc->backbuffer_count = max(1, present_parameters->BackBufferCount);
232 swapchain_desc->multisample_type = present_parameters->MultiSampleType;
233 swapchain_desc->multisample_quality = present_parameters->MultiSampleQuality;
234 swapchain_desc->swap_effect = present_parameters->SwapEffect;
235 swapchain_desc->device_window = present_parameters->hDeviceWindow;
236 swapchain_desc->windowed = present_parameters->Windowed;
237 swapchain_desc->enable_auto_depth_stencil = present_parameters->EnableAutoDepthStencil;
238 swapchain_desc->auto_depth_stencil_format
239 = wined3dformat_from_d3dformat(present_parameters->AutoDepthStencilFormat);
240 swapchain_desc->flags
241 = (present_parameters->Flags & D3DPRESENTFLAGS_MASK) | WINED3D_SWAPCHAIN_ALLOW_MODE_SWITCH;
242 swapchain_desc->refresh_rate = present_parameters->FullScreen_RefreshRateInHz;
243 swapchain_desc->swap_interval = present_parameters->PresentationInterval;
244 swapchain_desc->auto_restore_display_mode = TRUE;
246 if (present_parameters->Flags & ~D3DPRESENTFLAGS_MASK)
247 FIXME("Unhandled flags %#x.\n", present_parameters->Flags & ~D3DPRESENTFLAGS_MASK);
249 return TRUE;
252 void d3dcaps_from_wined3dcaps(D3DCAPS9 *caps, const WINED3DCAPS *wined3d_caps)
254 static const DWORD ps_minor_version[] = {0, 4, 0, 0};
255 static const DWORD vs_minor_version[] = {0, 1, 0, 0};
256 static const DWORD texture_filter_caps =
257 D3DPTFILTERCAPS_MINFPOINT | D3DPTFILTERCAPS_MINFLINEAR | D3DPTFILTERCAPS_MINFANISOTROPIC |
258 D3DPTFILTERCAPS_MINFPYRAMIDALQUAD | D3DPTFILTERCAPS_MINFGAUSSIANQUAD|
259 D3DPTFILTERCAPS_MIPFPOINT | D3DPTFILTERCAPS_MIPFLINEAR | D3DPTFILTERCAPS_MAGFPOINT |
260 D3DPTFILTERCAPS_MAGFLINEAR |D3DPTFILTERCAPS_MAGFANISOTROPIC|D3DPTFILTERCAPS_MAGFPYRAMIDALQUAD|
261 D3DPTFILTERCAPS_MAGFGAUSSIANQUAD;
263 caps->DeviceType = (D3DDEVTYPE)wined3d_caps->DeviceType;
264 caps->AdapterOrdinal = wined3d_caps->AdapterOrdinal;
265 caps->Caps = wined3d_caps->Caps;
266 caps->Caps2 = wined3d_caps->Caps2;
267 caps->Caps3 = wined3d_caps->Caps3;
268 caps->PresentationIntervals = wined3d_caps->PresentationIntervals;
269 caps->CursorCaps = wined3d_caps->CursorCaps;
270 caps->DevCaps = wined3d_caps->DevCaps;
271 caps->PrimitiveMiscCaps = wined3d_caps->PrimitiveMiscCaps;
272 caps->RasterCaps = wined3d_caps->RasterCaps;
273 caps->ZCmpCaps = wined3d_caps->ZCmpCaps;
274 caps->SrcBlendCaps = wined3d_caps->SrcBlendCaps;
275 caps->DestBlendCaps = wined3d_caps->DestBlendCaps;
276 caps->AlphaCmpCaps = wined3d_caps->AlphaCmpCaps;
277 caps->ShadeCaps = wined3d_caps->ShadeCaps;
278 caps->TextureCaps = wined3d_caps->TextureCaps;
279 caps->TextureFilterCaps = wined3d_caps->TextureFilterCaps;
280 caps->CubeTextureFilterCaps = wined3d_caps->CubeTextureFilterCaps;
281 caps->VolumeTextureFilterCaps = wined3d_caps->VolumeTextureFilterCaps;
282 caps->TextureAddressCaps = wined3d_caps->TextureAddressCaps;
283 caps->VolumeTextureAddressCaps = wined3d_caps->VolumeTextureAddressCaps;
284 caps->LineCaps = wined3d_caps->LineCaps;
285 caps->MaxTextureWidth = wined3d_caps->MaxTextureWidth;
286 caps->MaxTextureHeight = wined3d_caps->MaxTextureHeight;
287 caps->MaxVolumeExtent = wined3d_caps->MaxVolumeExtent;
288 caps->MaxTextureRepeat = wined3d_caps->MaxTextureRepeat;
289 caps->MaxTextureAspectRatio = wined3d_caps->MaxTextureAspectRatio;
290 caps->MaxAnisotropy = wined3d_caps->MaxAnisotropy;
291 caps->MaxVertexW = wined3d_caps->MaxVertexW;
292 caps->GuardBandLeft = wined3d_caps->GuardBandLeft;
293 caps->GuardBandTop = wined3d_caps->GuardBandTop;
294 caps->GuardBandRight = wined3d_caps->GuardBandRight;
295 caps->GuardBandBottom = wined3d_caps->GuardBandBottom;
296 caps->ExtentsAdjust = wined3d_caps->ExtentsAdjust;
297 caps->StencilCaps = wined3d_caps->StencilCaps;
298 caps->FVFCaps = wined3d_caps->FVFCaps;
299 caps->TextureOpCaps = wined3d_caps->TextureOpCaps;
300 caps->MaxTextureBlendStages = wined3d_caps->MaxTextureBlendStages;
301 caps->MaxSimultaneousTextures = wined3d_caps->MaxSimultaneousTextures;
302 caps->VertexProcessingCaps = wined3d_caps->VertexProcessingCaps;
303 caps->MaxActiveLights = wined3d_caps->MaxActiveLights;
304 caps->MaxUserClipPlanes = wined3d_caps->MaxUserClipPlanes;
305 caps->MaxVertexBlendMatrices = wined3d_caps->MaxVertexBlendMatrices;
306 caps->MaxVertexBlendMatrixIndex = wined3d_caps->MaxVertexBlendMatrixIndex;
307 caps->MaxPointSize = wined3d_caps->MaxPointSize;
308 caps->MaxPrimitiveCount = wined3d_caps->MaxPrimitiveCount;
309 caps->MaxVertexIndex = wined3d_caps->MaxVertexIndex;
310 caps->MaxStreams = wined3d_caps->MaxStreams;
311 caps->MaxStreamStride = wined3d_caps->MaxStreamStride;
312 caps->VertexShaderVersion = wined3d_caps->VertexShaderVersion;
313 caps->MaxVertexShaderConst = wined3d_caps->MaxVertexShaderConst;
314 caps->PixelShaderVersion = wined3d_caps->PixelShaderVersion;
315 caps->PixelShader1xMaxValue = wined3d_caps->PixelShader1xMaxValue;
316 caps->DevCaps2 = wined3d_caps->DevCaps2;
317 caps->MaxNpatchTessellationLevel = wined3d_caps->MaxNpatchTessellationLevel;
318 caps->MasterAdapterOrdinal = wined3d_caps->MasterAdapterOrdinal;
319 caps->AdapterOrdinalInGroup = wined3d_caps->AdapterOrdinalInGroup;
320 caps->NumberOfAdaptersInGroup = wined3d_caps->NumberOfAdaptersInGroup;
321 caps->DeclTypes = wined3d_caps->DeclTypes;
322 caps->NumSimultaneousRTs = wined3d_caps->NumSimultaneousRTs;
323 caps->StretchRectFilterCaps = wined3d_caps->StretchRectFilterCaps;
324 caps->VS20Caps.Caps = wined3d_caps->VS20Caps.caps;
325 caps->VS20Caps.DynamicFlowControlDepth = wined3d_caps->VS20Caps.dynamic_flow_control_depth;
326 caps->VS20Caps.NumTemps = wined3d_caps->VS20Caps.temp_count;
327 caps->VS20Caps.StaticFlowControlDepth = wined3d_caps->VS20Caps.static_flow_control_depth;
328 caps->PS20Caps.Caps = wined3d_caps->PS20Caps.caps;
329 caps->PS20Caps.DynamicFlowControlDepth = wined3d_caps->PS20Caps.dynamic_flow_control_depth;
330 caps->PS20Caps.NumTemps = wined3d_caps->PS20Caps.temp_count;
331 caps->PS20Caps.StaticFlowControlDepth = wined3d_caps->PS20Caps.static_flow_control_depth;
332 caps->PS20Caps.NumInstructionSlots = wined3d_caps->PS20Caps.instruction_slot_count;
333 caps->VertexTextureFilterCaps = wined3d_caps->VertexTextureFilterCaps;
334 caps->MaxVShaderInstructionsExecuted = wined3d_caps->MaxVShaderInstructionsExecuted;
335 caps->MaxPShaderInstructionsExecuted = wined3d_caps->MaxPShaderInstructionsExecuted;
336 caps->MaxVertexShader30InstructionSlots = wined3d_caps->MaxVertexShader30InstructionSlots;
337 caps->MaxPixelShader30InstructionSlots = wined3d_caps->MaxPixelShader30InstructionSlots;
339 /* Some functionality is implemented in d3d9.dll, not wined3d.dll. Add the needed caps. */
340 caps->DevCaps2 |= D3DDEVCAPS2_CAN_STRETCHRECT_FROM_TEXTURES;
342 /* Filter wined3d caps. */
343 caps->TextureFilterCaps &= texture_filter_caps;
344 caps->CubeTextureFilterCaps &= texture_filter_caps;
345 caps->VolumeTextureFilterCaps &= texture_filter_caps;
347 caps->DevCaps &=
348 D3DDEVCAPS_EXECUTESYSTEMMEMORY | D3DDEVCAPS_EXECUTEVIDEOMEMORY | D3DDEVCAPS_TLVERTEXSYSTEMMEMORY |
349 D3DDEVCAPS_TLVERTEXVIDEOMEMORY | D3DDEVCAPS_TEXTURESYSTEMMEMORY| D3DDEVCAPS_TEXTUREVIDEOMEMORY |
350 D3DDEVCAPS_DRAWPRIMTLVERTEX | D3DDEVCAPS_CANRENDERAFTERFLIP | D3DDEVCAPS_TEXTURENONLOCALVIDMEM|
351 D3DDEVCAPS_DRAWPRIMITIVES2 | D3DDEVCAPS_SEPARATETEXTUREMEMORIES |
352 D3DDEVCAPS_DRAWPRIMITIVES2EX | D3DDEVCAPS_HWTRANSFORMANDLIGHT| D3DDEVCAPS_CANBLTSYSTONONLOCAL |
353 D3DDEVCAPS_HWRASTERIZATION | D3DDEVCAPS_PUREDEVICE | D3DDEVCAPS_QUINTICRTPATCHES |
354 D3DDEVCAPS_RTPATCHES | D3DDEVCAPS_RTPATCHHANDLEZERO | D3DDEVCAPS_NPATCHES;
356 caps->ShadeCaps &=
357 D3DPSHADECAPS_COLORGOURAUDRGB | D3DPSHADECAPS_SPECULARGOURAUDRGB |
358 D3DPSHADECAPS_ALPHAGOURAUDBLEND | D3DPSHADECAPS_FOGGOURAUD;
360 caps->RasterCaps &=
361 D3DPRASTERCAPS_DITHER | D3DPRASTERCAPS_ZTEST | D3DPRASTERCAPS_FOGVERTEX |
362 D3DPRASTERCAPS_FOGTABLE | D3DPRASTERCAPS_MIPMAPLODBIAS | D3DPRASTERCAPS_ZBUFFERLESSHSR |
363 D3DPRASTERCAPS_FOGRANGE | D3DPRASTERCAPS_ANISOTROPY | D3DPRASTERCAPS_WBUFFER |
364 D3DPRASTERCAPS_WFOG | D3DPRASTERCAPS_ZFOG | D3DPRASTERCAPS_COLORPERSPECTIVE |
365 D3DPRASTERCAPS_SCISSORTEST | D3DPRASTERCAPS_SLOPESCALEDEPTHBIAS |
366 D3DPRASTERCAPS_DEPTHBIAS | D3DPRASTERCAPS_MULTISAMPLE_TOGGLE;
368 caps->DevCaps2 &=
369 D3DDEVCAPS2_STREAMOFFSET | D3DDEVCAPS2_DMAPNPATCH | D3DDEVCAPS2_ADAPTIVETESSRTPATCH |
370 D3DDEVCAPS2_ADAPTIVETESSNPATCH | D3DDEVCAPS2_CAN_STRETCHRECT_FROM_TEXTURES |
371 D3DDEVCAPS2_PRESAMPLEDDMAPNPATCH| D3DDEVCAPS2_VERTEXELEMENTSCANSHARESTREAMOFFSET;
373 caps->Caps2 &=
374 D3DCAPS2_FULLSCREENGAMMA | D3DCAPS2_CANCALIBRATEGAMMA | D3DCAPS2_RESERVED |
375 D3DCAPS2_CANMANAGERESOURCE | D3DCAPS2_DYNAMICTEXTURES | D3DCAPS2_CANAUTOGENMIPMAP;
377 caps->VertexProcessingCaps &=
378 D3DVTXPCAPS_TEXGEN | D3DVTXPCAPS_MATERIALSOURCE7 | D3DVTXPCAPS_DIRECTIONALLIGHTS |
379 D3DVTXPCAPS_POSITIONALLIGHTS | D3DVTXPCAPS_LOCALVIEWER | D3DVTXPCAPS_TWEENING |
380 D3DVTXPCAPS_TEXGEN_SPHEREMAP | D3DVTXPCAPS_NO_TEXGEN_NONLOCALVIEWER;
382 caps->TextureCaps &=
383 D3DPTEXTURECAPS_PERSPECTIVE | D3DPTEXTURECAPS_POW2 | D3DPTEXTURECAPS_ALPHA |
384 D3DPTEXTURECAPS_SQUAREONLY | D3DPTEXTURECAPS_TEXREPEATNOTSCALEDBYSIZE |
385 D3DPTEXTURECAPS_ALPHAPALETTE | D3DPTEXTURECAPS_NONPOW2CONDITIONAL |
386 D3DPTEXTURECAPS_PROJECTED | D3DPTEXTURECAPS_CUBEMAP | D3DPTEXTURECAPS_VOLUMEMAP |
387 D3DPTEXTURECAPS_MIPMAP | D3DPTEXTURECAPS_MIPVOLUMEMAP | D3DPTEXTURECAPS_MIPCUBEMAP |
388 D3DPTEXTURECAPS_CUBEMAP_POW2 | D3DPTEXTURECAPS_VOLUMEMAP_POW2| D3DPTEXTURECAPS_NOPROJECTEDBUMPENV;
390 caps->MaxVertexShaderConst = min(D3D9_MAX_VERTEX_SHADER_CONSTANTF, caps->MaxVertexShaderConst);
391 caps->NumSimultaneousRTs = min(D3D9_MAX_SIMULTANEOUS_RENDERTARGETS, caps->NumSimultaneousRTs);
393 if (caps->PixelShaderVersion > 3)
395 caps->PixelShaderVersion = D3DPS_VERSION(3, 0);
397 else
399 DWORD major = caps->PixelShaderVersion;
400 caps->PixelShaderVersion = D3DPS_VERSION(major, ps_minor_version[major]);
403 if (caps->VertexShaderVersion > 3)
405 caps->VertexShaderVersion = D3DVS_VERSION(3, 0);
407 else
409 DWORD major = caps->VertexShaderVersion;
410 caps->VertexShaderVersion = D3DVS_VERSION(major, vs_minor_version[major]);
414 static HRESULT WINAPI d3d9_device_QueryInterface(IDirect3DDevice9Ex *iface, REFIID riid, void **out)
416 TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
418 if (IsEqualGUID(riid, &IID_IDirect3DDevice9)
419 || IsEqualGUID(riid, &IID_IUnknown))
421 IDirect3DDevice9Ex_AddRef(iface);
422 *out = iface;
423 return S_OK;
426 if (IsEqualGUID(riid, &IID_IDirect3DDevice9Ex))
428 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
430 /* Find out if the creating d3d9 interface was created with Direct3DCreate9Ex.
431 * It doesn't matter with which function the device was created. */
432 if (!device->d3d_parent->extended)
434 WARN("IDirect3D9 instance wasn't created with CreateDirect3D9Ex, returning E_NOINTERFACE.\n");
435 *out = NULL;
436 return E_NOINTERFACE;
439 IDirect3DDevice9Ex_AddRef(iface);
440 *out = iface;
441 return S_OK;
444 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
446 *out = NULL;
447 return E_NOINTERFACE;
450 static ULONG WINAPI d3d9_device_AddRef(IDirect3DDevice9Ex *iface)
452 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
453 ULONG refcount = InterlockedIncrement(&device->refcount);
455 TRACE("%p increasing refcount to %u.\n", iface, refcount);
457 return refcount;
460 static ULONG WINAPI DECLSPEC_HOTPATCH d3d9_device_Release(IDirect3DDevice9Ex *iface)
462 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
463 ULONG refcount;
465 if (device->in_destruction)
466 return 0;
468 refcount = InterlockedDecrement(&device->refcount);
470 TRACE("%p decreasing refcount to %u.\n", iface, refcount);
472 if (!refcount)
474 unsigned i;
475 device->in_destruction = TRUE;
477 wined3d_mutex_lock();
478 for (i = 0; i < device->fvf_decl_count; ++i)
480 wined3d_vertex_declaration_decref(device->fvf_decls[i].decl);
482 HeapFree(GetProcessHeap(), 0, device->fvf_decls);
484 if (device->vertex_buffer)
485 wined3d_buffer_decref(device->vertex_buffer);
486 if (device->index_buffer)
487 wined3d_buffer_decref(device->index_buffer);
489 HeapFree(GetProcessHeap(), 0, device->implicit_swapchains);
491 wined3d_device_uninit_3d(device->wined3d_device);
492 wined3d_device_release_focus_window(device->wined3d_device);
493 wined3d_device_decref(device->wined3d_device);
494 wined3d_mutex_unlock();
496 IDirect3D9Ex_Release(&device->d3d_parent->IDirect3D9Ex_iface);
498 HeapFree(GetProcessHeap(), 0, device);
501 return refcount;
504 static HRESULT WINAPI d3d9_device_TestCooperativeLevel(IDirect3DDevice9Ex *iface)
506 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
508 TRACE("iface %p.\n", iface);
510 TRACE("device state: %#x.\n", device->device_state);
512 if (device->d3d_parent->extended)
513 return D3D_OK;
515 switch (device->device_state)
517 default:
518 case D3D9_DEVICE_STATE_OK:
519 return D3D_OK;
520 case D3D9_DEVICE_STATE_LOST:
521 return D3DERR_DEVICELOST;
522 case D3D9_DEVICE_STATE_NOT_RESET:
523 return D3DERR_DEVICENOTRESET;
527 static UINT WINAPI d3d9_device_GetAvailableTextureMem(IDirect3DDevice9Ex *iface)
529 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
530 UINT ret;
532 TRACE("iface %p.\n", iface);
534 wined3d_mutex_lock();
535 ret = wined3d_device_get_available_texture_mem(device->wined3d_device);
536 wined3d_mutex_unlock();
538 return ret;
541 static HRESULT WINAPI d3d9_device_EvictManagedResources(IDirect3DDevice9Ex *iface)
543 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
545 TRACE("iface %p.\n", iface);
547 wined3d_mutex_lock();
548 wined3d_device_evict_managed_resources(device->wined3d_device);
549 wined3d_mutex_unlock();
551 return D3D_OK;
554 static HRESULT WINAPI d3d9_device_GetDirect3D(IDirect3DDevice9Ex *iface, IDirect3D9 **d3d9)
556 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
558 TRACE("iface %p, d3d9 %p.\n", iface, d3d9);
560 if (!d3d9)
561 return D3DERR_INVALIDCALL;
563 return IDirect3D9Ex_QueryInterface(&device->d3d_parent->IDirect3D9Ex_iface, &IID_IDirect3D9, (void **)d3d9);
566 static HRESULT WINAPI d3d9_device_GetDeviceCaps(IDirect3DDevice9Ex *iface, D3DCAPS9 *caps)
568 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
569 WINED3DCAPS wined3d_caps;
570 HRESULT hr;
572 TRACE("iface %p, caps %p.\n", iface, caps);
574 if (!caps)
575 return D3DERR_INVALIDCALL;
577 memset(caps, 0, sizeof(*caps));
579 wined3d_mutex_lock();
580 hr = wined3d_device_get_device_caps(device->wined3d_device, &wined3d_caps);
581 wined3d_mutex_unlock();
583 d3dcaps_from_wined3dcaps(caps, &wined3d_caps);
585 return hr;
588 static HRESULT WINAPI d3d9_device_GetDisplayMode(IDirect3DDevice9Ex *iface, UINT swapchain, D3DDISPLAYMODE *mode)
590 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
591 struct wined3d_display_mode wined3d_mode;
592 HRESULT hr;
594 TRACE("iface %p, swapchain %u, mode %p.\n", iface, swapchain, mode);
596 wined3d_mutex_lock();
597 hr = wined3d_device_get_display_mode(device->wined3d_device, swapchain, &wined3d_mode, NULL);
598 wined3d_mutex_unlock();
600 if (SUCCEEDED(hr))
602 mode->Width = wined3d_mode.width;
603 mode->Height = wined3d_mode.height;
604 mode->RefreshRate = wined3d_mode.refresh_rate;
605 mode->Format = d3dformat_from_wined3dformat(wined3d_mode.format_id);
608 return hr;
611 static HRESULT WINAPI d3d9_device_GetCreationParameters(IDirect3DDevice9Ex *iface,
612 D3DDEVICE_CREATION_PARAMETERS *parameters)
614 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
616 TRACE("iface %p, parameters %p.\n", iface, parameters);
618 wined3d_mutex_lock();
619 wined3d_device_get_creation_parameters(device->wined3d_device,
620 (struct wined3d_device_creation_parameters *)parameters);
621 wined3d_mutex_unlock();
623 return D3D_OK;
626 static HRESULT WINAPI d3d9_device_SetCursorProperties(IDirect3DDevice9Ex *iface,
627 UINT hotspot_x, UINT hotspot_y, IDirect3DSurface9 *bitmap)
629 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
630 struct d3d9_surface *bitmap_impl = unsafe_impl_from_IDirect3DSurface9(bitmap);
631 HRESULT hr;
633 TRACE("iface %p, hotspot_x %u, hotspot_y %u, bitmap %p.\n",
634 iface, hotspot_x, hotspot_y, bitmap);
636 if (!bitmap)
638 WARN("No cursor bitmap, returning D3DERR_INVALIDCALL.\n");
639 return D3DERR_INVALIDCALL;
642 wined3d_mutex_lock();
643 hr = wined3d_device_set_cursor_properties(device->wined3d_device,
644 hotspot_x, hotspot_y, bitmap_impl->wined3d_texture, bitmap_impl->sub_resource_idx);
645 wined3d_mutex_unlock();
647 return hr;
650 static void WINAPI d3d9_device_SetCursorPosition(IDirect3DDevice9Ex *iface, int x, int y, DWORD flags)
652 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
654 TRACE("iface %p, x %u, y %u, flags %#x.\n", iface, x, y, flags);
656 wined3d_mutex_lock();
657 wined3d_device_set_cursor_position(device->wined3d_device, x, y, flags);
658 wined3d_mutex_unlock();
661 static BOOL WINAPI d3d9_device_ShowCursor(IDirect3DDevice9Ex *iface, BOOL show)
663 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
664 BOOL ret;
666 TRACE("iface %p, show %#x.\n", iface, show);
668 wined3d_mutex_lock();
669 ret = wined3d_device_show_cursor(device->wined3d_device, show);
670 wined3d_mutex_unlock();
672 return ret;
675 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_CreateAdditionalSwapChain(IDirect3DDevice9Ex *iface,
676 D3DPRESENT_PARAMETERS *present_parameters, IDirect3DSwapChain9 **swapchain)
678 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
679 struct wined3d_swapchain_desc desc;
680 struct d3d9_swapchain *object;
681 UINT i, count;
682 HRESULT hr;
684 TRACE("iface %p, present_parameters %p, swapchain %p.\n",
685 iface, present_parameters, swapchain);
687 if (!present_parameters->Windowed)
689 WARN("Trying to create an additional fullscreen swapchain, returning D3DERR_INVALIDCALL.\n");
690 return D3DERR_INVALIDCALL;
693 wined3d_mutex_lock();
694 count = wined3d_device_get_swapchain_count(device->wined3d_device);
695 for (i = 0; i < count; ++i)
697 struct wined3d_swapchain *wined3d_swapchain;
699 wined3d_swapchain = wined3d_device_get_swapchain(device->wined3d_device, i);
700 wined3d_swapchain_get_desc(wined3d_swapchain, &desc);
702 if (!desc.windowed)
704 wined3d_mutex_unlock();
705 WARN("Trying to create an additional swapchain in fullscreen mode, returning D3DERR_INVALIDCALL.\n");
706 return D3DERR_INVALIDCALL;
709 wined3d_mutex_unlock();
711 if (!wined3d_swapchain_desc_from_present_parameters(&desc, present_parameters,
712 device->d3d_parent->extended))
713 return D3DERR_INVALIDCALL;
714 if (SUCCEEDED(hr = d3d9_swapchain_create(device, &desc, &object)))
715 *swapchain = (IDirect3DSwapChain9 *)&object->IDirect3DSwapChain9Ex_iface;
716 present_parameters_from_wined3d_swapchain_desc(present_parameters, &desc);
718 return hr;
721 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_GetSwapChain(IDirect3DDevice9Ex *iface,
722 UINT swapchain_idx, IDirect3DSwapChain9 **swapchain)
724 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
725 HRESULT hr;
727 TRACE("iface %p, swapchain_idx %u, swapchain %p.\n", iface, swapchain_idx, swapchain);
729 wined3d_mutex_lock();
730 if (swapchain_idx < device->implicit_swapchain_count)
732 *swapchain = (IDirect3DSwapChain9 *)&device->implicit_swapchains[swapchain_idx]->IDirect3DSwapChain9Ex_iface;
733 IDirect3DSwapChain9Ex_AddRef(*swapchain);
734 hr = D3D_OK;
736 else
738 *swapchain = NULL;
739 hr = D3DERR_INVALIDCALL;
741 wined3d_mutex_unlock();
743 return hr;
746 static UINT WINAPI d3d9_device_GetNumberOfSwapChains(IDirect3DDevice9Ex *iface)
748 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
749 UINT count;
751 TRACE("iface %p.\n", iface);
753 wined3d_mutex_lock();
754 count = wined3d_device_get_swapchain_count(device->wined3d_device);
755 wined3d_mutex_unlock();
757 return count;
760 static HRESULT CDECL reset_enum_callback(struct wined3d_resource *resource)
762 struct wined3d_resource_desc desc;
763 IDirect3DBaseTexture9 *texture;
764 struct d3d9_surface *surface;
765 IUnknown *parent;
767 wined3d_resource_get_desc(resource, &desc);
768 if (desc.pool != WINED3D_POOL_DEFAULT)
769 return D3D_OK;
771 if (desc.resource_type != WINED3D_RTYPE_TEXTURE_2D)
773 WARN("Resource %p in pool D3DPOOL_DEFAULT blocks the Reset call.\n", resource);
774 return D3DERR_INVALIDCALL;
777 parent = wined3d_resource_get_parent(resource);
778 if (parent && SUCCEEDED(IUnknown_QueryInterface(parent, &IID_IDirect3DBaseTexture9, (void **)&texture)))
780 IDirect3DBaseTexture9_Release(texture);
781 WARN("Texture %p (resource %p) in pool D3DPOOL_DEFAULT blocks the Reset call.\n", texture, resource);
782 return D3DERR_INVALIDCALL;
785 surface = wined3d_texture_get_sub_resource_parent(wined3d_texture_from_resource(resource), 0);
786 if (!surface->resource.refcount)
787 return D3D_OK;
789 WARN("Surface %p in pool D3DPOOL_DEFAULT blocks the Reset call.\n", surface);
790 return D3DERR_INVALIDCALL;
793 static HRESULT d3d9_device_get_swapchains(struct d3d9_device *device)
795 UINT i, new_swapchain_count = wined3d_device_get_swapchain_count(device->wined3d_device);
796 struct wined3d_swapchain *wined3d_swapchain;
798 if (!(device->implicit_swapchains = HeapAlloc(GetProcessHeap(), 0,
799 new_swapchain_count * sizeof(*device->implicit_swapchains))))
800 return E_OUTOFMEMORY;
802 for (i = 0; i < new_swapchain_count; ++i)
804 wined3d_swapchain = wined3d_device_get_swapchain(device->wined3d_device, i);
805 device->implicit_swapchains[i] = wined3d_swapchain_get_parent(wined3d_swapchain);
807 device->implicit_swapchain_count = new_swapchain_count;
809 return D3D_OK;
812 static HRESULT d3d9_device_reset(struct d3d9_device *device,
813 D3DPRESENT_PARAMETERS *present_parameters, D3DDISPLAYMODEEX *mode)
815 struct wined3d_swapchain_desc swapchain_desc;
816 struct wined3d_display_mode wined3d_mode;
817 HRESULT hr;
819 if (!device->d3d_parent->extended && device->device_state == D3D9_DEVICE_STATE_LOST)
821 WARN("App not active, returning D3DERR_DEVICELOST.\n");
822 return D3DERR_DEVICELOST;
825 if (mode)
827 wined3d_mode.width = mode->Width;
828 wined3d_mode.height = mode->Height;
829 wined3d_mode.refresh_rate = mode->RefreshRate;
830 wined3d_mode.format_id = wined3dformat_from_d3dformat(mode->Format);
831 wined3d_mode.scanline_ordering = mode->ScanLineOrdering;
834 if (!wined3d_swapchain_desc_from_present_parameters(&swapchain_desc, present_parameters,
835 device->d3d_parent->extended))
836 return D3DERR_INVALIDCALL;
838 wined3d_mutex_lock();
840 if (device->vertex_buffer)
842 wined3d_buffer_decref(device->vertex_buffer);
843 device->vertex_buffer = NULL;
844 device->vertex_buffer_size = 0;
847 if (device->index_buffer)
849 wined3d_buffer_decref(device->index_buffer);
850 device->index_buffer = NULL;
851 device->index_buffer_size = 0;
854 if (SUCCEEDED(hr = wined3d_device_reset(device->wined3d_device, &swapchain_desc,
855 mode ? &wined3d_mode : NULL, reset_enum_callback, !device->d3d_parent->extended)))
857 HeapFree(GetProcessHeap(), 0, device->implicit_swapchains);
859 if (FAILED(hr = d3d9_device_get_swapchains(device)))
861 device->device_state = D3D9_DEVICE_STATE_NOT_RESET;
863 else
865 wined3d_swapchain_get_desc(device->implicit_swapchains[0]->wined3d_swapchain, &swapchain_desc);
866 present_parameters->BackBufferWidth = swapchain_desc.backbuffer_width;
867 present_parameters->BackBufferHeight = swapchain_desc.backbuffer_height;
868 present_parameters->BackBufferFormat = d3dformat_from_wined3dformat(swapchain_desc.backbuffer_format);
869 present_parameters->BackBufferCount = swapchain_desc.backbuffer_count;
871 device->device_state = D3D9_DEVICE_STATE_OK;
874 else if (!device->d3d_parent->extended)
876 device->device_state = D3D9_DEVICE_STATE_NOT_RESET;
879 wined3d_mutex_unlock();
881 return hr;
884 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_Reset(IDirect3DDevice9Ex *iface,
885 D3DPRESENT_PARAMETERS *present_parameters)
887 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
889 TRACE("iface %p, present_parameters %p.\n", iface, present_parameters);
891 return d3d9_device_reset(device, present_parameters, NULL);
894 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_Present(IDirect3DDevice9Ex *iface,
895 const RECT *src_rect, const RECT *dst_rect, HWND dst_window_override, const RGNDATA *dirty_region)
897 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
898 UINT i;
899 HRESULT hr;
901 TRACE("iface %p, src_rect %p, dst_rect %p, dst_window_override %p, dirty_region %p.\n",
902 iface, src_rect, dst_rect, dst_window_override, dirty_region);
904 if (device->device_state != D3D9_DEVICE_STATE_OK)
905 return device->d3d_parent->extended ? S_PRESENT_OCCLUDED : D3DERR_DEVICELOST;
907 if (dirty_region)
908 FIXME("Ignoring dirty_region %p.\n", dirty_region);
910 wined3d_mutex_lock();
911 for (i = 0; i < device->implicit_swapchain_count; ++i)
913 if (FAILED(hr = wined3d_swapchain_present(device->implicit_swapchains[i]->wined3d_swapchain,
914 src_rect, dst_rect, dst_window_override, 0)))
916 wined3d_mutex_unlock();
917 return hr;
920 wined3d_mutex_unlock();
922 return D3D_OK;
925 static HRESULT WINAPI d3d9_device_GetBackBuffer(IDirect3DDevice9Ex *iface, UINT swapchain,
926 UINT backbuffer_idx, D3DBACKBUFFER_TYPE backbuffer_type, IDirect3DSurface9 **backbuffer)
928 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
929 HRESULT hr;
931 TRACE("iface %p, swapchain %u, backbuffer_idx %u, backbuffer_type %#x, backbuffer %p.\n",
932 iface, swapchain, backbuffer_idx, backbuffer_type, backbuffer);
934 /* backbuffer_type is ignored by native. */
936 /* No need to check for backbuffer == NULL, Windows crashes in that case. */
937 *backbuffer = NULL;
939 wined3d_mutex_lock();
940 if (swapchain >= device->implicit_swapchain_count)
942 wined3d_mutex_unlock();
943 WARN("Swapchain index %u is out of range, returning D3DERR_INVALIDCALL.\n", swapchain);
944 return D3DERR_INVALIDCALL;
947 hr = IDirect3DSwapChain9Ex_GetBackBuffer(&device->implicit_swapchains[swapchain]->IDirect3DSwapChain9Ex_iface,
948 backbuffer_idx, backbuffer_type, backbuffer);
949 wined3d_mutex_unlock();
951 return hr;
954 static HRESULT WINAPI d3d9_device_GetRasterStatus(IDirect3DDevice9Ex *iface,
955 UINT swapchain, D3DRASTER_STATUS *raster_status)
957 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
958 HRESULT hr;
960 TRACE("iface %p, swapchain %u, raster_status %p.\n", iface, swapchain, raster_status);
962 wined3d_mutex_lock();
963 hr = wined3d_device_get_raster_status(device->wined3d_device,
964 swapchain, (struct wined3d_raster_status *)raster_status);
965 wined3d_mutex_unlock();
967 return hr;
970 static HRESULT WINAPI d3d9_device_SetDialogBoxMode(IDirect3DDevice9Ex *iface, BOOL enable)
972 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
973 HRESULT hr;
975 TRACE("iface %p, enable %#x.\n", iface, enable);
977 wined3d_mutex_lock();
978 hr = wined3d_device_set_dialog_box_mode(device->wined3d_device, enable);
979 wined3d_mutex_unlock();
981 return hr;
984 static void WINAPI d3d9_device_SetGammaRamp(IDirect3DDevice9Ex *iface,
985 UINT swapchain, DWORD flags, const D3DGAMMARAMP *ramp)
987 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
989 TRACE("iface %p, swapchain %u, flags %#x, ramp %p.\n", iface, swapchain, flags, ramp);
991 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
992 wined3d_mutex_lock();
993 wined3d_device_set_gamma_ramp(device->wined3d_device, swapchain, flags, (const struct wined3d_gamma_ramp *)ramp);
994 wined3d_mutex_unlock();
997 static void WINAPI d3d9_device_GetGammaRamp(IDirect3DDevice9Ex *iface, UINT swapchain, D3DGAMMARAMP *ramp)
999 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1001 TRACE("iface %p, swapchain %u, ramp %p.\n", iface, swapchain, ramp);
1003 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
1004 wined3d_mutex_lock();
1005 wined3d_device_get_gamma_ramp(device->wined3d_device, swapchain, (struct wined3d_gamma_ramp *)ramp);
1006 wined3d_mutex_unlock();
1009 static HRESULT WINAPI d3d9_device_CreateTexture(IDirect3DDevice9Ex *iface,
1010 UINT width, UINT height, UINT levels, DWORD usage, D3DFORMAT format,
1011 D3DPOOL pool, IDirect3DTexture9 **texture, HANDLE *shared_handle)
1013 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1014 struct d3d9_texture *object;
1015 BOOL set_mem = FALSE;
1016 HRESULT hr;
1018 TRACE("iface %p, width %u, height %u, levels %u, usage %#x, format %#x, pool %#x, texture %p, shared_handle %p.\n",
1019 iface, width, height, levels, usage, format, pool, texture, shared_handle);
1021 *texture = NULL;
1022 if (shared_handle)
1024 if (!device->d3d_parent->extended)
1026 WARN("Trying to create a shared or user memory texture on a non-ex device.\n");
1027 return E_NOTIMPL;
1030 if (pool == D3DPOOL_SYSTEMMEM)
1032 if (levels != 1)
1033 return D3DERR_INVALIDCALL;
1034 set_mem = TRUE;
1036 else
1038 if (pool != D3DPOOL_DEFAULT)
1040 WARN("Trying to create a shared texture in pool %#x.\n", pool);
1041 return D3DERR_INVALIDCALL;
1043 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1047 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1048 if (!object)
1049 return D3DERR_OUTOFVIDEOMEMORY;
1051 hr = texture_init(object, device, width, height, levels, usage, format, pool);
1052 if (FAILED(hr))
1054 WARN("Failed to initialize texture, hr %#x.\n", hr);
1055 HeapFree(GetProcessHeap(), 0, object);
1056 return hr;
1059 if (set_mem)
1060 wined3d_texture_update_desc(object->wined3d_texture, width, height,
1061 wined3dformat_from_d3dformat(format), WINED3D_MULTISAMPLE_NONE, 0,
1062 *shared_handle, 0);
1064 TRACE("Created texture %p.\n", object);
1065 *texture = (IDirect3DTexture9 *)&object->IDirect3DBaseTexture9_iface;
1067 return D3D_OK;
1070 static HRESULT WINAPI d3d9_device_CreateVolumeTexture(IDirect3DDevice9Ex *iface,
1071 UINT width, UINT height, UINT depth, UINT levels, DWORD usage, D3DFORMAT format,
1072 D3DPOOL pool, IDirect3DVolumeTexture9 **texture, HANDLE *shared_handle)
1074 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1075 struct d3d9_texture *object;
1076 HRESULT hr;
1078 TRACE("iface %p, width %u, height %u, depth %u, levels %u, "
1079 "usage %#x, format %#x, pool %#x, texture %p, shared_handle %p.\n",
1080 iface, width, height, depth, levels,
1081 usage, format, pool, texture, shared_handle);
1083 *texture = NULL;
1084 if (shared_handle)
1086 if (!device->d3d_parent->extended)
1088 WARN("Trying to create a shared volume texture on a non-ex device.\n");
1089 return E_NOTIMPL;
1092 if (pool != D3DPOOL_DEFAULT)
1094 WARN("Trying to create a shared volume texture in pool %#x.\n", pool);
1095 return D3DERR_INVALIDCALL;
1097 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1100 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1101 if (!object)
1102 return D3DERR_OUTOFVIDEOMEMORY;
1104 hr = volumetexture_init(object, device, width, height, depth, levels, usage, format, pool);
1105 if (FAILED(hr))
1107 WARN("Failed to initialize volume texture, hr %#x.\n", hr);
1108 HeapFree(GetProcessHeap(), 0, object);
1109 return hr;
1112 TRACE("Created volume texture %p.\n", object);
1113 *texture = (IDirect3DVolumeTexture9 *)&object->IDirect3DBaseTexture9_iface;
1115 return D3D_OK;
1118 static HRESULT WINAPI d3d9_device_CreateCubeTexture(IDirect3DDevice9Ex *iface,
1119 UINT edge_length, UINT levels, DWORD usage, D3DFORMAT format, D3DPOOL pool,
1120 IDirect3DCubeTexture9 **texture, HANDLE *shared_handle)
1122 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1123 struct d3d9_texture *object;
1124 HRESULT hr;
1126 TRACE("iface %p, edge_length %u, levels %u, usage %#x, format %#x, pool %#x, texture %p, shared_handle %p.\n",
1127 iface, edge_length, levels, usage, format, pool, texture, shared_handle);
1129 *texture = NULL;
1130 if (shared_handle)
1132 if (!device->d3d_parent->extended)
1134 WARN("Trying to create a shared cube texture on a non-ex device.\n");
1135 return E_NOTIMPL;
1138 if (pool != D3DPOOL_DEFAULT)
1140 WARN("Trying to create a shared cube texture in pool %#x.\n", pool);
1141 return D3DERR_INVALIDCALL;
1143 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1146 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1147 if (!object)
1148 return D3DERR_OUTOFVIDEOMEMORY;
1150 hr = cubetexture_init(object, device, edge_length, levels, usage, format, pool);
1151 if (FAILED(hr))
1153 WARN("Failed to initialize cube texture, hr %#x.\n", hr);
1154 HeapFree(GetProcessHeap(), 0, object);
1155 return hr;
1158 TRACE("Created cube texture %p.\n", object);
1159 *texture = (IDirect3DCubeTexture9 *)&object->IDirect3DBaseTexture9_iface;
1161 return D3D_OK;
1164 static HRESULT WINAPI d3d9_device_CreateVertexBuffer(IDirect3DDevice9Ex *iface, UINT size,
1165 DWORD usage, DWORD fvf, D3DPOOL pool, IDirect3DVertexBuffer9 **buffer,
1166 HANDLE *shared_handle)
1168 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1169 struct d3d9_vertexbuffer *object;
1170 HRESULT hr;
1172 TRACE("iface %p, size %u, usage %#x, fvf %#x, pool %#x, buffer %p, shared_handle %p.\n",
1173 iface, size, usage, fvf, pool, buffer, shared_handle);
1175 if (shared_handle)
1177 if (!device->d3d_parent->extended)
1179 WARN("Trying to create a shared vertex buffer on a non-ex device.\n");
1180 return E_NOTIMPL;
1183 if (pool != D3DPOOL_DEFAULT)
1185 WARN("Trying to create a shared vertex buffer in pool %#x.\n", pool);
1186 return D3DERR_NOTAVAILABLE;
1188 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1191 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1192 if (!object)
1193 return D3DERR_OUTOFVIDEOMEMORY;
1195 hr = vertexbuffer_init(object, device, size, usage, fvf, pool);
1196 if (FAILED(hr))
1198 WARN("Failed to initialize vertex buffer, hr %#x.\n", hr);
1199 HeapFree(GetProcessHeap(), 0, object);
1200 return hr;
1203 TRACE("Created vertex buffer %p.\n", object);
1204 *buffer = &object->IDirect3DVertexBuffer9_iface;
1206 return D3D_OK;
1209 static HRESULT WINAPI d3d9_device_CreateIndexBuffer(IDirect3DDevice9Ex *iface, UINT size,
1210 DWORD usage, D3DFORMAT format, D3DPOOL pool, IDirect3DIndexBuffer9 **buffer,
1211 HANDLE *shared_handle)
1213 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1214 struct d3d9_indexbuffer *object;
1215 HRESULT hr;
1217 TRACE("iface %p, size %u, usage %#x, format %#x, pool %#x, buffer %p, shared_handle %p.\n",
1218 iface, size, usage, format, pool, buffer, shared_handle);
1220 if (shared_handle)
1222 if (!device->d3d_parent->extended)
1224 WARN("Trying to create a shared index buffer on a non-ex device.\n");
1225 return E_NOTIMPL;
1228 if (pool != D3DPOOL_DEFAULT)
1230 WARN("Trying to create a shared index buffer in pool %#x.\n", pool);
1231 return D3DERR_NOTAVAILABLE;
1233 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1236 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1237 if (!object)
1238 return D3DERR_OUTOFVIDEOMEMORY;
1240 hr = indexbuffer_init(object, device, size, usage, format, pool);
1241 if (FAILED(hr))
1243 WARN("Failed to initialize index buffer, hr %#x.\n", hr);
1244 HeapFree(GetProcessHeap(), 0, object);
1245 return hr;
1248 TRACE("Created index buffer %p.\n", object);
1249 *buffer = &object->IDirect3DIndexBuffer9_iface;
1251 return D3D_OK;
1254 static HRESULT d3d9_device_create_surface(struct d3d9_device *device, UINT width, UINT height,
1255 D3DFORMAT format, DWORD flags, IDirect3DSurface9 **surface, UINT usage, D3DPOOL pool,
1256 D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality, void *user_mem)
1258 struct wined3d_resource_desc desc;
1259 struct d3d9_surface *surface_impl;
1260 struct wined3d_texture *texture;
1261 HRESULT hr;
1263 TRACE("device %p, width %u, height %u, format %#x, flags %#x, surface %p.\n"
1264 "usage %#x, pool %#x, multisample_type %#x, multisample_quality %u.\n",
1265 device, width, height, format, flags, surface, usage, pool,
1266 multisample_type, multisample_quality);
1268 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
1269 desc.format = wined3dformat_from_d3dformat(format);
1270 desc.multisample_type = multisample_type;
1271 desc.multisample_quality = multisample_quality;
1272 desc.usage = usage & WINED3DUSAGE_MASK;
1273 desc.pool = pool;
1274 desc.width = width;
1275 desc.height = height;
1276 desc.depth = 1;
1277 desc.size = 0;
1279 if (is_gdi_compat_wined3dformat(desc.format))
1280 flags |= WINED3D_TEXTURE_CREATE_GET_DC;
1282 wined3d_mutex_lock();
1284 if (FAILED(hr = wined3d_texture_create(device->wined3d_device, &desc,
1285 1, 1, flags, NULL, NULL, &d3d9_null_wined3d_parent_ops, &texture)))
1287 wined3d_mutex_unlock();
1288 WARN("Failed to create texture, hr %#x.\n", hr);
1289 if (hr == WINED3DERR_NOTAVAILABLE)
1290 hr = D3DERR_INVALIDCALL;
1291 return hr;
1294 surface_impl = wined3d_texture_get_sub_resource_parent(texture, 0);
1295 surface_impl->parent_device = &device->IDirect3DDevice9Ex_iface;
1296 *surface = &surface_impl->IDirect3DSurface9_iface;
1297 IDirect3DSurface9_AddRef(*surface);
1299 if (user_mem)
1300 wined3d_texture_update_desc(texture, width, height,
1301 desc.format, multisample_type, multisample_quality, user_mem, 0);
1303 wined3d_texture_decref(texture);
1305 wined3d_mutex_unlock();
1307 return D3D_OK;
1310 BOOL is_gdi_compat_wined3dformat(enum wined3d_format_id format)
1312 switch (format)
1314 case WINED3DFMT_B8G8R8A8_UNORM:
1315 case WINED3DFMT_B8G8R8X8_UNORM:
1316 case WINED3DFMT_B5G6R5_UNORM:
1317 case WINED3DFMT_B5G5R5X1_UNORM:
1318 case WINED3DFMT_B5G5R5A1_UNORM:
1319 case WINED3DFMT_B8G8R8_UNORM:
1320 return TRUE;
1321 default:
1322 return FALSE;
1326 static HRESULT WINAPI d3d9_device_CreateRenderTarget(IDirect3DDevice9Ex *iface, UINT width, UINT height,
1327 D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
1328 BOOL lockable, IDirect3DSurface9 **surface, HANDLE *shared_handle)
1330 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1331 DWORD flags = 0;
1333 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u.\n"
1334 "lockable %#x, surface %p, shared_handle %p.\n",
1335 iface, width, height, format, multisample_type, multisample_quality,
1336 lockable, surface, shared_handle);
1338 *surface = NULL;
1339 if (shared_handle)
1341 if (!device->d3d_parent->extended)
1343 WARN("Trying to create a shared render target on a non-ex device.\n");
1344 return E_NOTIMPL;
1347 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1350 if (lockable)
1351 flags |= WINED3D_TEXTURE_CREATE_MAPPABLE;
1353 return d3d9_device_create_surface(device, width, height, format, flags, surface,
1354 D3DUSAGE_RENDERTARGET, D3DPOOL_DEFAULT, multisample_type, multisample_quality, NULL);
1357 static HRESULT WINAPI d3d9_device_CreateDepthStencilSurface(IDirect3DDevice9Ex *iface, UINT width, UINT height,
1358 D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
1359 BOOL discard, IDirect3DSurface9 **surface, HANDLE *shared_handle)
1361 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1362 DWORD flags = WINED3D_TEXTURE_CREATE_MAPPABLE;
1364 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u.\n"
1365 "discard %#x, surface %p, shared_handle %p.\n",
1366 iface, width, height, format, multisample_type, multisample_quality,
1367 discard, surface, shared_handle);
1369 *surface = NULL;
1370 if (shared_handle)
1372 if (!device->d3d_parent->extended)
1374 WARN("Trying to create a shared depth stencil on a non-ex device.\n");
1375 return E_NOTIMPL;
1378 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1381 if (discard)
1382 flags |= WINED3D_TEXTURE_CREATE_DISCARD;
1384 return d3d9_device_create_surface(device, width, height, format, flags, surface,
1385 D3DUSAGE_DEPTHSTENCIL, D3DPOOL_DEFAULT, multisample_type, multisample_quality, NULL);
1389 static HRESULT WINAPI d3d9_device_UpdateSurface(IDirect3DDevice9Ex *iface,
1390 IDirect3DSurface9 *src_surface, const RECT *src_rect,
1391 IDirect3DSurface9 *dst_surface, const POINT *dst_point)
1393 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1394 struct d3d9_surface *src = unsafe_impl_from_IDirect3DSurface9(src_surface);
1395 struct d3d9_surface *dst = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1396 struct wined3d_sub_resource_desc src_desc, dst_desc;
1397 struct wined3d_box src_box;
1398 HRESULT hr;
1400 TRACE("iface %p, src_surface %p, src_rect %p, dst_surface %p, dst_point %p.\n",
1401 iface, src_surface, src_rect, dst_surface, dst_point);
1403 wined3d_mutex_lock();
1405 wined3d_texture_get_sub_resource_desc(src->wined3d_texture, src->sub_resource_idx, &src_desc);
1406 wined3d_texture_get_sub_resource_desc(dst->wined3d_texture, dst->sub_resource_idx, &dst_desc);
1407 if (src_desc.format != dst_desc.format)
1409 wined3d_mutex_unlock();
1410 WARN("Surface formats (%#x/%#x) don't match.\n",
1411 d3dformat_from_wined3dformat(src_desc.format),
1412 d3dformat_from_wined3dformat(dst_desc.format));
1413 return D3DERR_INVALIDCALL;
1416 if (src_rect)
1417 wined3d_box_set(&src_box, src_rect->left, src_rect->top, src_rect->right, src_rect->bottom, 0, 1);
1418 else
1419 wined3d_box_set(&src_box, 0, 0, src_desc.width, src_desc.height, 0, 1);
1421 hr = wined3d_device_copy_sub_resource_region(device->wined3d_device,
1422 wined3d_texture_get_resource(dst->wined3d_texture), dst->sub_resource_idx, dst_point ? dst_point->x : 0,
1423 dst_point ? dst_point->y : 0, 0, wined3d_texture_get_resource(src->wined3d_texture),
1424 src->sub_resource_idx, &src_box);
1425 wined3d_mutex_unlock();
1427 if (FAILED(hr))
1428 return D3DERR_INVALIDCALL;
1430 return hr;
1433 static HRESULT WINAPI d3d9_device_UpdateTexture(IDirect3DDevice9Ex *iface,
1434 IDirect3DBaseTexture9 *src_texture, IDirect3DBaseTexture9 *dst_texture)
1436 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1437 struct d3d9_texture *src_impl, *dst_impl;
1438 HRESULT hr;
1440 TRACE("iface %p, src_texture %p, dst_texture %p.\n", iface, src_texture, dst_texture);
1442 src_impl = unsafe_impl_from_IDirect3DBaseTexture9(src_texture);
1443 dst_impl = unsafe_impl_from_IDirect3DBaseTexture9(dst_texture);
1445 wined3d_mutex_lock();
1446 hr = wined3d_device_update_texture(device->wined3d_device,
1447 src_impl->wined3d_texture, dst_impl->wined3d_texture);
1448 wined3d_mutex_unlock();
1450 return hr;
1453 static HRESULT WINAPI d3d9_device_GetRenderTargetData(IDirect3DDevice9Ex *iface,
1454 IDirect3DSurface9 *render_target, IDirect3DSurface9 *dst_surface)
1456 struct d3d9_surface *rt_impl = unsafe_impl_from_IDirect3DSurface9(render_target);
1457 struct d3d9_surface *dst_impl = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1458 struct wined3d_sub_resource_desc wined3d_desc;
1459 RECT dst_rect, src_rect;
1460 HRESULT hr;
1462 TRACE("iface %p, render_target %p, dst_surface %p.\n", iface, render_target, dst_surface);
1464 if (!render_target || !dst_surface)
1465 return D3DERR_INVALIDCALL;
1467 wined3d_mutex_lock();
1468 wined3d_texture_get_sub_resource_desc(dst_impl->wined3d_texture, dst_impl->sub_resource_idx, &wined3d_desc);
1469 SetRect(&dst_rect, 0, 0, wined3d_desc.width, wined3d_desc.height);
1471 wined3d_texture_get_sub_resource_desc(rt_impl->wined3d_texture, rt_impl->sub_resource_idx, &wined3d_desc);
1472 SetRect(&src_rect, 0, 0, wined3d_desc.width, wined3d_desc.height);
1474 /* TODO: Check surface sizes, pools, etc. */
1475 if (wined3d_desc.multisample_type)
1476 hr = D3DERR_INVALIDCALL;
1477 else
1478 hr = wined3d_texture_blt(dst_impl->wined3d_texture, dst_impl->sub_resource_idx, &dst_rect,
1479 rt_impl->wined3d_texture, rt_impl->sub_resource_idx, &src_rect, 0, NULL, WINED3D_TEXF_POINT);
1480 wined3d_mutex_unlock();
1482 return hr;
1485 static HRESULT WINAPI d3d9_device_GetFrontBufferData(IDirect3DDevice9Ex *iface,
1486 UINT swapchain, IDirect3DSurface9 *dst_surface)
1488 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1489 struct d3d9_surface *dst_impl = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1490 HRESULT hr = D3DERR_INVALIDCALL;
1492 TRACE("iface %p, swapchain %u, dst_surface %p.\n", iface, swapchain, dst_surface);
1494 wined3d_mutex_lock();
1495 if (swapchain < device->implicit_swapchain_count)
1496 hr = wined3d_swapchain_get_front_buffer_data(device->implicit_swapchains[swapchain]->wined3d_swapchain,
1497 dst_impl->wined3d_texture, dst_impl->sub_resource_idx);
1498 wined3d_mutex_unlock();
1500 return hr;
1503 static HRESULT WINAPI d3d9_device_StretchRect(IDirect3DDevice9Ex *iface, IDirect3DSurface9 *src_surface,
1504 const RECT *src_rect, IDirect3DSurface9 *dst_surface, const RECT *dst_rect, D3DTEXTUREFILTERTYPE filter)
1506 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1507 struct d3d9_surface *src = unsafe_impl_from_IDirect3DSurface9(src_surface);
1508 struct d3d9_surface *dst = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1509 struct wined3d_sub_resource_desc src_desc, dst_desc;
1510 HRESULT hr = D3DERR_INVALIDCALL;
1511 RECT d, s;
1513 TRACE("iface %p, src_surface %p, src_rect %p, dst_surface %p, dst_rect %p, filter %#x.\n",
1514 iface, src_surface, src_rect, dst_surface, dst_rect, filter);
1516 wined3d_mutex_lock();
1517 wined3d_texture_get_sub_resource_desc(dst->wined3d_texture, dst->sub_resource_idx, &dst_desc);
1518 if (!dst_rect)
1520 SetRect(&d, 0, 0, dst_desc.width, dst_desc.height);
1521 dst_rect = &d;
1524 wined3d_texture_get_sub_resource_desc(src->wined3d_texture, src->sub_resource_idx, &src_desc);
1525 if (!src_rect)
1527 SetRect(&s, 0, 0, src_desc.width, src_desc.height);
1528 src_rect = &s;
1531 if (src_desc.usage & WINED3DUSAGE_DEPTHSTENCIL)
1533 if (device->in_scene)
1535 WARN("Rejecting depth / stencil blit while in scene.\n");
1536 goto done;
1539 if (src_rect->left || src_rect->top || src_rect->right != src_desc.width
1540 || src_rect->bottom != src_desc.height)
1542 WARN("Rejecting depth / stencil blit with invalid source rect %s.\n",
1543 wine_dbgstr_rect(src_rect));
1544 goto done;
1547 if (dst_rect->left || dst_rect->top || dst_rect->right != dst_desc.width
1548 || dst_rect->bottom != dst_desc.height)
1550 WARN("Rejecting depth / stencil blit with invalid destination rect %s.\n",
1551 wine_dbgstr_rect(dst_rect));
1552 goto done;
1555 if (src_desc.width != dst_desc.width || src_desc.height != dst_desc.height)
1557 WARN("Rejecting depth / stencil blit with mismatched surface sizes.\n");
1558 goto done;
1562 hr = wined3d_texture_blt(dst->wined3d_texture, dst->sub_resource_idx, dst_rect,
1563 src->wined3d_texture, src->sub_resource_idx, src_rect, 0, NULL, filter);
1564 if (hr == WINEDDERR_INVALIDRECT)
1565 hr = D3DERR_INVALIDCALL;
1567 done:
1568 wined3d_mutex_unlock();
1569 return hr;
1572 static HRESULT WINAPI d3d9_device_ColorFill(IDirect3DDevice9Ex *iface,
1573 IDirect3DSurface9 *surface, const RECT *rect, D3DCOLOR color)
1575 const struct wined3d_color c =
1577 ((color >> 16) & 0xff) / 255.0f,
1578 ((color >> 8) & 0xff) / 255.0f,
1579 (color & 0xff) / 255.0f,
1580 ((color >> 24) & 0xff) / 255.0f,
1582 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1583 struct d3d9_surface *surface_impl = unsafe_impl_from_IDirect3DSurface9(surface);
1584 struct wined3d_sub_resource_desc desc;
1585 struct wined3d_rendertarget_view *rtv;
1586 HRESULT hr;
1588 TRACE("iface %p, surface %p, rect %p, color 0x%08x.\n", iface, surface, rect, color);
1590 wined3d_mutex_lock();
1592 if (FAILED(wined3d_texture_get_sub_resource_desc(surface_impl->wined3d_texture,
1593 surface_impl->sub_resource_idx, &desc)))
1595 wined3d_mutex_unlock();
1596 return D3DERR_INVALIDCALL;
1599 if (desc.pool != WINED3D_POOL_DEFAULT)
1601 wined3d_mutex_unlock();
1602 WARN("Colorfill is not allowed on surfaces in pool %#x, returning D3DERR_INVALIDCALL.\n", desc.pool);
1603 return D3DERR_INVALIDCALL;
1605 if ((desc.usage & (WINED3DUSAGE_RENDERTARGET | WINED3DUSAGE_TEXTURE)) == WINED3DUSAGE_TEXTURE)
1607 wined3d_mutex_unlock();
1608 WARN("Colorfill is not allowed on non-RT textures, returning D3DERR_INVALIDCALL.\n");
1609 return D3DERR_INVALIDCALL;
1611 if (desc.usage & WINED3DUSAGE_DEPTHSTENCIL)
1613 wined3d_mutex_unlock();
1614 WARN("Colorfill is not allowed on depth stencil surfaces, returning D3DERR_INVALIDCALL.\n");
1615 return D3DERR_INVALIDCALL;
1618 rtv = d3d9_surface_acquire_rendertarget_view(surface_impl);
1619 hr = wined3d_device_clear_rendertarget_view(device->wined3d_device,
1620 rtv, rect, WINED3DCLEAR_TARGET, &c, 0.0f, 0);
1621 d3d9_surface_release_rendertarget_view(surface_impl, rtv);
1623 wined3d_mutex_unlock();
1625 return hr;
1628 static HRESULT WINAPI d3d9_device_CreateOffscreenPlainSurface(IDirect3DDevice9Ex *iface,
1629 UINT width, UINT height, D3DFORMAT format, D3DPOOL pool, IDirect3DSurface9 **surface,
1630 HANDLE *shared_handle)
1632 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1633 void *user_mem = NULL;
1635 TRACE("iface %p, width %u, height %u, format %#x, pool %#x, surface %p, shared_handle %p.\n",
1636 iface, width, height, format, pool, surface, shared_handle);
1638 *surface = NULL;
1639 if (pool == D3DPOOL_MANAGED)
1641 WARN("Attempting to create a managed offscreen plain surface.\n");
1642 return D3DERR_INVALIDCALL;
1645 if (shared_handle)
1647 if (!device->d3d_parent->extended)
1649 WARN("Trying to create a shared or user memory surface on a non-ex device.\n");
1650 return E_NOTIMPL;
1653 if (pool == D3DPOOL_SYSTEMMEM)
1654 user_mem = *shared_handle;
1655 else
1657 if (pool != D3DPOOL_DEFAULT)
1659 WARN("Trying to create a shared surface in pool %#x.\n", pool);
1660 return D3DERR_INVALIDCALL;
1662 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1666 /* FIXME: Offscreen surfaces are supposed to be always lockable,
1667 * regardless of the pool they're created in. Should we set dynamic usage
1668 * here? */
1669 return d3d9_device_create_surface(device, width, height, format,
1670 WINED3D_TEXTURE_CREATE_MAPPABLE, surface, 0, pool, D3DMULTISAMPLE_NONE, 0, user_mem);
1673 static HRESULT WINAPI d3d9_device_SetRenderTarget(IDirect3DDevice9Ex *iface, DWORD idx, IDirect3DSurface9 *surface)
1675 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1676 struct d3d9_surface *surface_impl = unsafe_impl_from_IDirect3DSurface9(surface);
1677 struct wined3d_rendertarget_view *rtv;
1678 HRESULT hr;
1680 TRACE("iface %p, idx %u, surface %p.\n", iface, idx, surface);
1682 if (idx >= D3D9_MAX_SIMULTANEOUS_RENDERTARGETS)
1684 WARN("Invalid index %u specified.\n", idx);
1685 return D3DERR_INVALIDCALL;
1688 if (!idx && !surface_impl)
1690 WARN("Trying to set render target 0 to NULL.\n");
1691 return D3DERR_INVALIDCALL;
1694 if (surface_impl && d3d9_surface_get_device(surface_impl) != device)
1696 WARN("Render target surface does not match device.\n");
1697 return D3DERR_INVALIDCALL;
1700 wined3d_mutex_lock();
1701 rtv = surface_impl ? d3d9_surface_acquire_rendertarget_view(surface_impl) : NULL;
1702 hr = wined3d_device_set_rendertarget_view(device->wined3d_device, idx, rtv, TRUE);
1703 d3d9_surface_release_rendertarget_view(surface_impl, rtv);
1704 wined3d_mutex_unlock();
1706 return hr;
1709 static HRESULT WINAPI d3d9_device_GetRenderTarget(IDirect3DDevice9Ex *iface, DWORD idx, IDirect3DSurface9 **surface)
1711 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1712 struct wined3d_rendertarget_view *wined3d_rtv;
1713 struct d3d9_surface *surface_impl;
1714 HRESULT hr = D3D_OK;
1716 TRACE("iface %p, idx %u, surface %p.\n", iface, idx, surface);
1718 if (!surface)
1719 return D3DERR_INVALIDCALL;
1721 if (idx >= D3D9_MAX_SIMULTANEOUS_RENDERTARGETS)
1723 WARN("Invalid index %u specified.\n", idx);
1724 return D3DERR_INVALIDCALL;
1727 wined3d_mutex_lock();
1728 if ((wined3d_rtv = wined3d_device_get_rendertarget_view(device->wined3d_device, idx)))
1730 /* We want the sub resource parent here, since the view itself may be
1731 * internal to wined3d and may not have a parent. */
1732 surface_impl = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_rtv);
1733 *surface = &surface_impl->IDirect3DSurface9_iface;
1734 IDirect3DSurface9_AddRef(*surface);
1736 else
1738 hr = D3DERR_NOTFOUND;
1739 *surface = NULL;
1741 wined3d_mutex_unlock();
1743 return hr;
1746 static HRESULT WINAPI d3d9_device_SetDepthStencilSurface(IDirect3DDevice9Ex *iface, IDirect3DSurface9 *depth_stencil)
1748 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1749 struct d3d9_surface *ds_impl = unsafe_impl_from_IDirect3DSurface9(depth_stencil);
1750 struct wined3d_rendertarget_view *rtv;
1752 TRACE("iface %p, depth_stencil %p.\n", iface, depth_stencil);
1754 wined3d_mutex_lock();
1755 rtv = ds_impl ? d3d9_surface_acquire_rendertarget_view(ds_impl) : NULL;
1756 wined3d_device_set_depth_stencil_view(device->wined3d_device, rtv);
1757 d3d9_surface_release_rendertarget_view(ds_impl, rtv);
1758 wined3d_mutex_unlock();
1760 return D3D_OK;
1763 static HRESULT WINAPI d3d9_device_GetDepthStencilSurface(IDirect3DDevice9Ex *iface, IDirect3DSurface9 **depth_stencil)
1765 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1766 struct wined3d_rendertarget_view *wined3d_dsv;
1767 struct d3d9_surface *surface_impl;
1768 HRESULT hr = D3D_OK;
1770 TRACE("iface %p, depth_stencil %p.\n", iface, depth_stencil);
1772 if (!depth_stencil)
1773 return D3DERR_INVALIDCALL;
1775 wined3d_mutex_lock();
1776 if ((wined3d_dsv = wined3d_device_get_depth_stencil_view(device->wined3d_device)))
1778 /* We want the sub resource parent here, since the view itself may be
1779 * internal to wined3d and may not have a parent. */
1780 surface_impl = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_dsv);
1781 *depth_stencil = &surface_impl->IDirect3DSurface9_iface;
1782 IDirect3DSurface9_AddRef(*depth_stencil);
1784 else
1786 hr = D3DERR_NOTFOUND;
1787 *depth_stencil = NULL;
1789 wined3d_mutex_unlock();
1791 return hr;
1794 static HRESULT WINAPI d3d9_device_BeginScene(IDirect3DDevice9Ex *iface)
1796 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1797 HRESULT hr;
1799 TRACE("iface %p.\n", iface);
1801 wined3d_mutex_lock();
1802 if (SUCCEEDED(hr = wined3d_device_begin_scene(device->wined3d_device)))
1803 device->in_scene = TRUE;
1804 wined3d_mutex_unlock();
1806 return hr;
1809 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_EndScene(IDirect3DDevice9Ex *iface)
1811 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1812 HRESULT hr;
1814 TRACE("iface %p.\n", iface);
1816 wined3d_mutex_lock();
1817 if (SUCCEEDED(hr = wined3d_device_end_scene(device->wined3d_device)))
1818 device->in_scene = FALSE;
1819 wined3d_mutex_unlock();
1821 return hr;
1824 static HRESULT WINAPI d3d9_device_Clear(IDirect3DDevice9Ex *iface, DWORD rect_count,
1825 const D3DRECT *rects, DWORD flags, D3DCOLOR color, float z, DWORD stencil)
1827 const struct wined3d_color c =
1829 ((color >> 16) & 0xff) / 255.0f,
1830 ((color >> 8) & 0xff) / 255.0f,
1831 (color & 0xff) / 255.0f,
1832 ((color >> 24) & 0xff) / 255.0f,
1834 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1835 HRESULT hr;
1837 TRACE("iface %p, rect_count %u, rects %p, flags %#x, color 0x%08x, z %.8e, stencil %u.\n",
1838 iface, rect_count, rects, flags, color, z, stencil);
1840 if (rect_count && !rects)
1842 WARN("count %u with NULL rects.\n", rect_count);
1843 rect_count = 0;
1846 wined3d_mutex_lock();
1847 hr = wined3d_device_clear(device->wined3d_device, rect_count, (const RECT *)rects, flags, &c, z, stencil);
1848 wined3d_mutex_unlock();
1850 return hr;
1853 static HRESULT WINAPI d3d9_device_SetTransform(IDirect3DDevice9Ex *iface,
1854 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
1856 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1858 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1860 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1861 wined3d_mutex_lock();
1862 wined3d_device_set_transform(device->wined3d_device, state, (const struct wined3d_matrix *)matrix);
1863 wined3d_mutex_unlock();
1865 return D3D_OK;
1868 static HRESULT WINAPI d3d9_device_GetTransform(IDirect3DDevice9Ex *iface,
1869 D3DTRANSFORMSTATETYPE state, D3DMATRIX *matrix)
1871 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1873 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1875 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1876 wined3d_mutex_lock();
1877 wined3d_device_get_transform(device->wined3d_device, state, (struct wined3d_matrix *)matrix);
1878 wined3d_mutex_unlock();
1880 return D3D_OK;
1883 static HRESULT WINAPI d3d9_device_MultiplyTransform(IDirect3DDevice9Ex *iface,
1884 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
1886 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1888 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1890 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1891 wined3d_mutex_lock();
1892 wined3d_device_multiply_transform(device->wined3d_device, state, (const struct wined3d_matrix *)matrix);
1893 wined3d_mutex_unlock();
1895 return D3D_OK;
1898 static HRESULT WINAPI d3d9_device_SetViewport(IDirect3DDevice9Ex *iface, const D3DVIEWPORT9 *viewport)
1900 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1901 struct wined3d_viewport vp;
1903 TRACE("iface %p, viewport %p.\n", iface, viewport);
1905 vp.x = viewport->X;
1906 vp.y = viewport->Y;
1907 vp.width = viewport->Width;
1908 vp.height = viewport->Height;
1909 vp.min_z = viewport->MinZ;
1910 vp.max_z = viewport->MaxZ;
1912 wined3d_mutex_lock();
1913 wined3d_device_set_viewport(device->wined3d_device, &vp);
1914 wined3d_mutex_unlock();
1916 return D3D_OK;
1919 static HRESULT WINAPI d3d9_device_GetViewport(IDirect3DDevice9Ex *iface, D3DVIEWPORT9 *viewport)
1921 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1922 struct wined3d_viewport wined3d_viewport;
1924 TRACE("iface %p, viewport %p.\n", iface, viewport);
1926 wined3d_mutex_lock();
1927 wined3d_device_get_viewport(device->wined3d_device, &wined3d_viewport);
1928 wined3d_mutex_unlock();
1930 viewport->X = wined3d_viewport.x;
1931 viewport->Y = wined3d_viewport.y;
1932 viewport->Width = wined3d_viewport.width;
1933 viewport->Height = wined3d_viewport.height;
1934 viewport->MinZ = wined3d_viewport.min_z;
1935 viewport->MaxZ = wined3d_viewport.max_z;
1937 return D3D_OK;
1940 static HRESULT WINAPI d3d9_device_SetMaterial(IDirect3DDevice9Ex *iface, const D3DMATERIAL9 *material)
1942 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1944 TRACE("iface %p, material %p.\n", iface, material);
1946 /* Note: D3DMATERIAL9 is compatible with struct wined3d_material. */
1947 wined3d_mutex_lock();
1948 wined3d_device_set_material(device->wined3d_device, (const struct wined3d_material *)material);
1949 wined3d_mutex_unlock();
1951 return D3D_OK;
1954 static HRESULT WINAPI d3d9_device_GetMaterial(IDirect3DDevice9Ex *iface, D3DMATERIAL9 *material)
1956 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1958 TRACE("iface %p, material %p.\n", iface, material);
1960 /* Note: D3DMATERIAL9 is compatible with struct wined3d_material. */
1961 wined3d_mutex_lock();
1962 wined3d_device_get_material(device->wined3d_device, (struct wined3d_material *)material);
1963 wined3d_mutex_unlock();
1965 return D3D_OK;
1968 static HRESULT WINAPI d3d9_device_SetLight(IDirect3DDevice9Ex *iface, DWORD index, const D3DLIGHT9 *light)
1970 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1971 HRESULT hr;
1973 TRACE("iface %p, index %u, light %p.\n", iface, index, light);
1975 /* Note: D3DLIGHT9 is compatible with struct wined3d_light. */
1976 wined3d_mutex_lock();
1977 hr = wined3d_device_set_light(device->wined3d_device, index, (const struct wined3d_light *)light);
1978 wined3d_mutex_unlock();
1980 return hr;
1983 static HRESULT WINAPI d3d9_device_GetLight(IDirect3DDevice9Ex *iface, DWORD index, D3DLIGHT9 *light)
1985 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1986 HRESULT hr;
1988 TRACE("iface %p, index %u, light %p.\n", iface, index, light);
1990 /* Note: D3DLIGHT9 is compatible with struct wined3d_light. */
1991 wined3d_mutex_lock();
1992 hr = wined3d_device_get_light(device->wined3d_device, index, (struct wined3d_light *)light);
1993 wined3d_mutex_unlock();
1995 return hr;
1998 static HRESULT WINAPI d3d9_device_LightEnable(IDirect3DDevice9Ex *iface, DWORD index, BOOL enable)
2000 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2001 HRESULT hr;
2003 TRACE("iface %p, index %u, enable %#x.\n", iface, index, enable);
2005 wined3d_mutex_lock();
2006 hr = wined3d_device_set_light_enable(device->wined3d_device, index, enable);
2007 wined3d_mutex_unlock();
2009 return hr;
2012 static HRESULT WINAPI d3d9_device_GetLightEnable(IDirect3DDevice9Ex *iface, DWORD index, BOOL *enable)
2014 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2015 HRESULT hr;
2017 TRACE("iface %p, index %u, enable %p.\n", iface, index, enable);
2019 wined3d_mutex_lock();
2020 hr = wined3d_device_get_light_enable(device->wined3d_device, index, enable);
2021 wined3d_mutex_unlock();
2023 return hr;
2026 static HRESULT WINAPI d3d9_device_SetClipPlane(IDirect3DDevice9Ex *iface, DWORD index, const float *plane)
2028 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2029 HRESULT hr;
2031 TRACE("iface %p, index %u, plane %p.\n", iface, index, plane);
2033 wined3d_mutex_lock();
2034 hr = wined3d_device_set_clip_plane(device->wined3d_device, index, (const struct wined3d_vec4 *)plane);
2035 wined3d_mutex_unlock();
2037 return hr;
2040 static HRESULT WINAPI d3d9_device_GetClipPlane(IDirect3DDevice9Ex *iface, DWORD index, float *plane)
2042 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2043 HRESULT hr;
2045 TRACE("iface %p, index %u, plane %p.\n", iface, index, plane);
2047 wined3d_mutex_lock();
2048 hr = wined3d_device_get_clip_plane(device->wined3d_device, index, (struct wined3d_vec4 *)plane);
2049 wined3d_mutex_unlock();
2051 return hr;
2054 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_SetRenderState(IDirect3DDevice9Ex *iface,
2055 D3DRENDERSTATETYPE state, DWORD value)
2057 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2059 TRACE("iface %p, state %#x, value %#x.\n", iface, state, value);
2061 wined3d_mutex_lock();
2062 wined3d_device_set_render_state(device->wined3d_device, state, value);
2063 wined3d_mutex_unlock();
2065 return D3D_OK;
2068 static HRESULT WINAPI d3d9_device_GetRenderState(IDirect3DDevice9Ex *iface,
2069 D3DRENDERSTATETYPE state, DWORD *value)
2071 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2073 TRACE("iface %p, state %#x, value %p.\n", iface, state, value);
2075 wined3d_mutex_lock();
2076 *value = wined3d_device_get_render_state(device->wined3d_device, state);
2077 wined3d_mutex_unlock();
2079 return D3D_OK;
2082 static HRESULT WINAPI d3d9_device_CreateStateBlock(IDirect3DDevice9Ex *iface,
2083 D3DSTATEBLOCKTYPE type, IDirect3DStateBlock9 **stateblock)
2085 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2086 struct d3d9_stateblock *object;
2087 HRESULT hr;
2089 TRACE("iface %p, type %#x, stateblock %p.\n", iface, type, stateblock);
2091 if (type != D3DSBT_ALL && type != D3DSBT_PIXELSTATE && type != D3DSBT_VERTEXSTATE)
2093 WARN("Unexpected stateblock type, returning D3DERR_INVALIDCALL.\n");
2094 return D3DERR_INVALIDCALL;
2097 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2098 if (!object)
2099 return E_OUTOFMEMORY;
2101 hr = stateblock_init(object, device, type, NULL);
2102 if (FAILED(hr))
2104 WARN("Failed to initialize stateblock, hr %#x.\n", hr);
2105 HeapFree(GetProcessHeap(), 0, object);
2106 return hr;
2109 TRACE("Created stateblock %p.\n", object);
2110 *stateblock = &object->IDirect3DStateBlock9_iface;
2112 return D3D_OK;
2115 static HRESULT WINAPI d3d9_device_BeginStateBlock(IDirect3DDevice9Ex *iface)
2117 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2118 HRESULT hr;
2120 TRACE("iface %p.\n", iface);
2122 wined3d_mutex_lock();
2123 hr = wined3d_device_begin_stateblock(device->wined3d_device);
2124 wined3d_mutex_unlock();
2126 return hr;
2129 static HRESULT WINAPI d3d9_device_EndStateBlock(IDirect3DDevice9Ex *iface, IDirect3DStateBlock9 **stateblock)
2131 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2132 struct wined3d_stateblock *wined3d_stateblock;
2133 struct d3d9_stateblock *object;
2134 HRESULT hr;
2136 TRACE("iface %p, stateblock %p.\n", iface, stateblock);
2138 wined3d_mutex_lock();
2139 hr = wined3d_device_end_stateblock(device->wined3d_device, &wined3d_stateblock);
2140 wined3d_mutex_unlock();
2141 if (FAILED(hr))
2143 WARN("Failed to end the state block, hr %#x.\n", hr);
2144 return hr;
2147 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2148 if (!object)
2150 wined3d_mutex_lock();
2151 wined3d_stateblock_decref(wined3d_stateblock);
2152 wined3d_mutex_unlock();
2153 return E_OUTOFMEMORY;
2156 hr = stateblock_init(object, device, 0, wined3d_stateblock);
2157 if (FAILED(hr))
2159 WARN("Failed to initialize stateblock, hr %#x.\n", hr);
2160 wined3d_mutex_lock();
2161 wined3d_stateblock_decref(wined3d_stateblock);
2162 wined3d_mutex_unlock();
2163 HeapFree(GetProcessHeap(), 0, object);
2164 return hr;
2167 TRACE("Created stateblock %p.\n", object);
2168 *stateblock = &object->IDirect3DStateBlock9_iface;
2170 return D3D_OK;
2173 static HRESULT WINAPI d3d9_device_SetClipStatus(IDirect3DDevice9Ex *iface, const D3DCLIPSTATUS9 *clip_status)
2175 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2176 HRESULT hr;
2178 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
2180 wined3d_mutex_lock();
2181 hr = wined3d_device_set_clip_status(device->wined3d_device, (const struct wined3d_clip_status *)clip_status);
2182 wined3d_mutex_unlock();
2184 return hr;
2187 static HRESULT WINAPI d3d9_device_GetClipStatus(IDirect3DDevice9Ex *iface, D3DCLIPSTATUS9 *clip_status)
2189 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2190 HRESULT hr;
2192 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
2194 wined3d_mutex_lock();
2195 hr = wined3d_device_get_clip_status(device->wined3d_device, (struct wined3d_clip_status *)clip_status);
2196 wined3d_mutex_unlock();
2198 return hr;
2201 static HRESULT WINAPI d3d9_device_GetTexture(IDirect3DDevice9Ex *iface, DWORD stage, IDirect3DBaseTexture9 **texture)
2203 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2204 struct wined3d_texture *wined3d_texture = NULL;
2205 struct d3d9_texture *texture_impl;
2207 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
2209 if (!texture)
2210 return D3DERR_INVALIDCALL;
2212 wined3d_mutex_lock();
2213 if ((wined3d_texture = wined3d_device_get_texture(device->wined3d_device, stage)))
2215 texture_impl = wined3d_texture_get_parent(wined3d_texture);
2216 *texture = &texture_impl->IDirect3DBaseTexture9_iface;
2217 IDirect3DBaseTexture9_AddRef(*texture);
2219 else
2221 *texture = NULL;
2223 wined3d_mutex_unlock();
2225 return D3D_OK;
2228 static HRESULT WINAPI d3d9_device_SetTexture(IDirect3DDevice9Ex *iface, DWORD stage, IDirect3DBaseTexture9 *texture)
2230 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2231 struct d3d9_texture *texture_impl;
2232 HRESULT hr;
2234 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
2236 texture_impl = unsafe_impl_from_IDirect3DBaseTexture9(texture);
2238 wined3d_mutex_lock();
2239 hr = wined3d_device_set_texture(device->wined3d_device, stage,
2240 texture_impl ? texture_impl->wined3d_texture : NULL);
2241 wined3d_mutex_unlock();
2243 return hr;
2246 static const enum wined3d_texture_stage_state tss_lookup[] =
2248 WINED3D_TSS_INVALID, /* 0, unused */
2249 WINED3D_TSS_COLOR_OP, /* 1, D3DTSS_COLOROP */
2250 WINED3D_TSS_COLOR_ARG1, /* 2, D3DTSS_COLORARG1 */
2251 WINED3D_TSS_COLOR_ARG2, /* 3, D3DTSS_COLORARG2 */
2252 WINED3D_TSS_ALPHA_OP, /* 4, D3DTSS_ALPHAOP */
2253 WINED3D_TSS_ALPHA_ARG1, /* 5, D3DTSS_ALPHAARG1 */
2254 WINED3D_TSS_ALPHA_ARG2, /* 6, D3DTSS_ALPHAARG2 */
2255 WINED3D_TSS_BUMPENV_MAT00, /* 7, D3DTSS_BUMPENVMAT00 */
2256 WINED3D_TSS_BUMPENV_MAT01, /* 8, D3DTSS_BUMPENVMAT01 */
2257 WINED3D_TSS_BUMPENV_MAT10, /* 9, D3DTSS_BUMPENVMAT10 */
2258 WINED3D_TSS_BUMPENV_MAT11, /* 10, D3DTSS_BUMPENVMAT11 */
2259 WINED3D_TSS_TEXCOORD_INDEX, /* 11, D3DTSS_TEXCOORDINDEX */
2260 WINED3D_TSS_INVALID, /* 12, unused */
2261 WINED3D_TSS_INVALID, /* 13, unused */
2262 WINED3D_TSS_INVALID, /* 14, unused */
2263 WINED3D_TSS_INVALID, /* 15, unused */
2264 WINED3D_TSS_INVALID, /* 16, unused */
2265 WINED3D_TSS_INVALID, /* 17, unused */
2266 WINED3D_TSS_INVALID, /* 18, unused */
2267 WINED3D_TSS_INVALID, /* 19, unused */
2268 WINED3D_TSS_INVALID, /* 20, unused */
2269 WINED3D_TSS_INVALID, /* 21, unused */
2270 WINED3D_TSS_BUMPENV_LSCALE, /* 22, D3DTSS_BUMPENVLSCALE */
2271 WINED3D_TSS_BUMPENV_LOFFSET, /* 23, D3DTSS_BUMPENVLOFFSET */
2272 WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS, /* 24, D3DTSS_TEXTURETRANSFORMFLAGS */
2273 WINED3D_TSS_INVALID, /* 25, unused */
2274 WINED3D_TSS_COLOR_ARG0, /* 26, D3DTSS_COLORARG0 */
2275 WINED3D_TSS_ALPHA_ARG0, /* 27, D3DTSS_ALPHAARG0 */
2276 WINED3D_TSS_RESULT_ARG, /* 28, D3DTSS_RESULTARG */
2277 WINED3D_TSS_INVALID, /* 29, unused */
2278 WINED3D_TSS_INVALID, /* 30, unused */
2279 WINED3D_TSS_INVALID, /* 31, unused */
2280 WINED3D_TSS_CONSTANT, /* 32, D3DTSS_CONSTANT */
2283 static HRESULT WINAPI d3d9_device_GetTextureStageState(IDirect3DDevice9Ex *iface,
2284 DWORD stage, D3DTEXTURESTAGESTATETYPE state, DWORD *value)
2286 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2288 TRACE("iface %p, stage %u, state %#x, value %p.\n", iface, stage, state, value);
2290 if (state >= sizeof(tss_lookup) / sizeof(*tss_lookup))
2292 WARN("Invalid state %#x passed.\n", state);
2293 return D3D_OK;
2296 wined3d_mutex_lock();
2297 *value = wined3d_device_get_texture_stage_state(device->wined3d_device, stage, tss_lookup[state]);
2298 wined3d_mutex_unlock();
2300 return D3D_OK;
2303 static HRESULT WINAPI d3d9_device_SetTextureStageState(IDirect3DDevice9Ex *iface,
2304 DWORD stage, D3DTEXTURESTAGESTATETYPE state, DWORD value)
2306 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2308 TRACE("iface %p, stage %u, state %#x, value %#x.\n", iface, stage, state, value);
2310 if (state >= sizeof(tss_lookup) / sizeof(*tss_lookup))
2312 WARN("Invalid state %#x passed.\n", state);
2313 return D3D_OK;
2316 wined3d_mutex_lock();
2317 wined3d_device_set_texture_stage_state(device->wined3d_device, stage, tss_lookup[state], value);
2318 wined3d_mutex_unlock();
2320 return D3D_OK;
2323 static HRESULT WINAPI d3d9_device_GetSamplerState(IDirect3DDevice9Ex *iface,
2324 DWORD sampler, D3DSAMPLERSTATETYPE state, DWORD *value)
2326 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2328 TRACE("iface %p, sampler %u, state %#x, value %p.\n", iface, sampler, state, value);
2330 wined3d_mutex_lock();
2331 *value = wined3d_device_get_sampler_state(device->wined3d_device, sampler, state);
2332 wined3d_mutex_unlock();
2334 return D3D_OK;
2337 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_SetSamplerState(IDirect3DDevice9Ex *iface,
2338 DWORD sampler, D3DSAMPLERSTATETYPE state, DWORD value)
2340 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2342 TRACE("iface %p, sampler %u, state %#x, value %#x.\n", iface, sampler, state, value);
2344 wined3d_mutex_lock();
2345 wined3d_device_set_sampler_state(device->wined3d_device, sampler, state, value);
2346 wined3d_mutex_unlock();
2348 return D3D_OK;
2351 static HRESULT WINAPI d3d9_device_ValidateDevice(IDirect3DDevice9Ex *iface, DWORD *pass_count)
2353 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2354 HRESULT hr;
2356 TRACE("iface %p, pass_count %p.\n", iface, pass_count);
2358 wined3d_mutex_lock();
2359 hr = wined3d_device_validate_device(device->wined3d_device, pass_count);
2360 wined3d_mutex_unlock();
2362 return hr;
2365 static HRESULT WINAPI d3d9_device_SetPaletteEntries(IDirect3DDevice9Ex *iface,
2366 UINT palette_idx, const PALETTEENTRY *entries)
2368 WARN("iface %p, palette_idx %u, entries %p unimplemented.\n", iface, palette_idx, entries);
2370 /* The d3d9 palette API is non-functional on Windows. Getters and setters are implemented,
2371 * and some drivers allow the creation of P8 surfaces. These surfaces can be copied to
2372 * other P8 surfaces with StretchRect, but cannot be converted to (A)RGB.
2374 * Some older(dx7) cards may have support for P8 textures, but games cannot rely on this. */
2375 return D3D_OK;
2378 static HRESULT WINAPI d3d9_device_GetPaletteEntries(IDirect3DDevice9Ex *iface,
2379 UINT palette_idx, PALETTEENTRY *entries)
2381 FIXME("iface %p, palette_idx %u, entries %p unimplemented.\n", iface, palette_idx, entries);
2383 return D3DERR_INVALIDCALL;
2386 static HRESULT WINAPI d3d9_device_SetCurrentTexturePalette(IDirect3DDevice9Ex *iface, UINT palette_idx)
2388 WARN("iface %p, palette_idx %u unimplemented.\n", iface, palette_idx);
2390 return D3D_OK;
2393 static HRESULT WINAPI d3d9_device_GetCurrentTexturePalette(IDirect3DDevice9Ex *iface, UINT *palette_idx)
2395 FIXME("iface %p, palette_idx %p.\n", iface, palette_idx);
2397 return D3DERR_INVALIDCALL;
2400 static HRESULT WINAPI d3d9_device_SetScissorRect(IDirect3DDevice9Ex *iface, const RECT *rect)
2402 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2404 TRACE("iface %p, rect %p.\n", iface, rect);
2406 wined3d_mutex_lock();
2407 wined3d_device_set_scissor_rect(device->wined3d_device, rect);
2408 wined3d_mutex_unlock();
2410 return D3D_OK;
2413 static HRESULT WINAPI d3d9_device_GetScissorRect(IDirect3DDevice9Ex *iface, RECT *rect)
2415 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2417 TRACE("iface %p, rect %p.\n", iface, rect);
2419 wined3d_mutex_lock();
2420 wined3d_device_get_scissor_rect(device->wined3d_device, rect);
2421 wined3d_mutex_unlock();
2423 return D3D_OK;
2426 static HRESULT WINAPI d3d9_device_SetSoftwareVertexProcessing(IDirect3DDevice9Ex *iface, BOOL software)
2428 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2430 TRACE("iface %p, software %#x.\n", iface, software);
2432 wined3d_mutex_lock();
2433 wined3d_device_set_software_vertex_processing(device->wined3d_device, software);
2434 wined3d_mutex_unlock();
2436 return D3D_OK;
2439 static BOOL WINAPI d3d9_device_GetSoftwareVertexProcessing(IDirect3DDevice9Ex *iface)
2441 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2442 BOOL ret;
2444 TRACE("iface %p.\n", iface);
2446 wined3d_mutex_lock();
2447 ret = wined3d_device_get_software_vertex_processing(device->wined3d_device);
2448 wined3d_mutex_unlock();
2450 return ret;
2453 static HRESULT WINAPI d3d9_device_SetNPatchMode(IDirect3DDevice9Ex *iface, float segment_count)
2455 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2456 HRESULT hr;
2458 TRACE("iface %p, segment_count %.8e.\n", iface, segment_count);
2460 wined3d_mutex_lock();
2461 hr = wined3d_device_set_npatch_mode(device->wined3d_device, segment_count);
2462 wined3d_mutex_unlock();
2464 return hr;
2467 static float WINAPI d3d9_device_GetNPatchMode(IDirect3DDevice9Ex *iface)
2469 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2470 float ret;
2472 TRACE("iface %p.\n", iface);
2474 wined3d_mutex_lock();
2475 ret = wined3d_device_get_npatch_mode(device->wined3d_device);
2476 wined3d_mutex_unlock();
2478 return ret;
2481 static HRESULT WINAPI d3d9_device_DrawPrimitive(IDirect3DDevice9Ex *iface,
2482 D3DPRIMITIVETYPE primitive_type, UINT start_vertex, UINT primitive_count)
2484 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2485 HRESULT hr;
2487 TRACE("iface %p, primitive_type %#x, start_vertex %u, primitive_count %u.\n",
2488 iface, primitive_type, start_vertex, primitive_count);
2490 wined3d_mutex_lock();
2491 if (!device->has_vertex_declaration)
2493 wined3d_mutex_unlock();
2494 WARN("Called without a valid vertex declaration set.\n");
2495 return D3DERR_INVALIDCALL;
2497 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type, 0);
2498 hr = wined3d_device_draw_primitive(device->wined3d_device, start_vertex,
2499 vertex_count_from_primitive_count(primitive_type, primitive_count));
2500 wined3d_mutex_unlock();
2502 return hr;
2505 static HRESULT WINAPI d3d9_device_DrawIndexedPrimitive(IDirect3DDevice9Ex *iface,
2506 D3DPRIMITIVETYPE primitive_type, INT base_vertex_idx, UINT min_vertex_idx,
2507 UINT vertex_count, UINT start_idx, UINT primitive_count)
2509 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2510 HRESULT hr;
2512 TRACE("iface %p, primitive_type %#x, base_vertex_idx %u, min_vertex_idx %u, "
2513 "vertex_count %u, start_idx %u, primitive_count %u.\n",
2514 iface, primitive_type, base_vertex_idx, min_vertex_idx,
2515 vertex_count, start_idx, primitive_count);
2517 wined3d_mutex_lock();
2518 if (!device->has_vertex_declaration)
2520 wined3d_mutex_unlock();
2521 WARN("Called without a valid vertex declaration set.\n");
2522 return D3DERR_INVALIDCALL;
2524 wined3d_device_set_base_vertex_index(device->wined3d_device, base_vertex_idx);
2525 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type, 0);
2526 hr = wined3d_device_draw_indexed_primitive(device->wined3d_device, start_idx,
2527 vertex_count_from_primitive_count(primitive_type, primitive_count));
2528 wined3d_mutex_unlock();
2530 return hr;
2533 /* The caller is responsible for wined3d locking */
2534 static HRESULT d3d9_device_prepare_vertex_buffer(struct d3d9_device *device, UINT min_size)
2536 HRESULT hr;
2538 if (device->vertex_buffer_size < min_size || !device->vertex_buffer)
2540 UINT size = max(device->vertex_buffer_size * 2, min_size);
2541 struct wined3d_buffer *buffer;
2543 TRACE("Growing vertex buffer to %u bytes.\n", size);
2545 hr = wined3d_buffer_create_vb(device->wined3d_device, size, WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_WRITEONLY,
2546 WINED3D_POOL_DEFAULT, NULL, &d3d9_null_wined3d_parent_ops, &buffer);
2547 if (FAILED(hr))
2549 ERR("(%p) wined3d_buffer_create_vb failed with hr = %08x.\n", device, hr);
2550 return hr;
2553 if (device->vertex_buffer)
2554 wined3d_buffer_decref(device->vertex_buffer);
2556 device->vertex_buffer = buffer;
2557 device->vertex_buffer_size = size;
2558 device->vertex_buffer_pos = 0;
2560 return D3D_OK;
2563 static HRESULT WINAPI d3d9_device_DrawPrimitiveUP(IDirect3DDevice9Ex *iface,
2564 D3DPRIMITIVETYPE primitive_type, UINT primitive_count, const void *data, UINT stride)
2566 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2567 HRESULT hr;
2568 UINT vtx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2569 struct wined3d_map_desc wined3d_map_desc;
2570 struct wined3d_box wined3d_box = {0};
2571 UINT size = vtx_count * stride;
2572 struct wined3d_resource *vb;
2573 UINT vb_pos, align;
2575 TRACE("iface %p, primitive_type %#x, primitive_count %u, data %p, stride %u.\n",
2576 iface, primitive_type, primitive_count, data, stride);
2578 if (!primitive_count)
2580 WARN("primitive_count is 0, returning D3D_OK\n");
2581 return D3D_OK;
2584 wined3d_mutex_lock();
2586 if (!device->has_vertex_declaration)
2588 wined3d_mutex_unlock();
2589 WARN("Called without a valid vertex declaration set.\n");
2590 return D3DERR_INVALIDCALL;
2593 hr = d3d9_device_prepare_vertex_buffer(device, size);
2594 if (FAILED(hr))
2595 goto done;
2597 vb_pos = device->vertex_buffer_pos;
2598 align = vb_pos % stride;
2599 if (align) align = stride - align;
2600 if (vb_pos + size + align > device->vertex_buffer_size)
2601 vb_pos = 0;
2602 else
2603 vb_pos += align;
2605 wined3d_box.left = vb_pos;
2606 wined3d_box.right = vb_pos + size;
2607 vb = wined3d_buffer_get_resource(device->vertex_buffer);
2608 if (FAILED(hr = wined3d_resource_map(vb, 0, &wined3d_map_desc, &wined3d_box,
2609 vb_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD)))
2610 goto done;
2611 memcpy(wined3d_map_desc.data, data, size);
2612 wined3d_resource_unmap(vb, 0);
2613 device->vertex_buffer_pos = vb_pos + size;
2615 hr = wined3d_device_set_stream_source(device->wined3d_device, 0, device->vertex_buffer, 0, stride);
2616 if (FAILED(hr))
2617 goto done;
2619 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type, 0);
2620 hr = wined3d_device_draw_primitive(device->wined3d_device, vb_pos / stride, vtx_count);
2621 wined3d_device_set_stream_source(device->wined3d_device, 0, NULL, 0, 0);
2623 done:
2624 wined3d_mutex_unlock();
2625 return hr;
2628 /* The caller is responsible for wined3d locking */
2629 static HRESULT d3d9_device_prepare_index_buffer(struct d3d9_device *device, UINT min_size)
2631 HRESULT hr;
2633 if (device->index_buffer_size < min_size || !device->index_buffer)
2635 UINT size = max(device->index_buffer_size * 2, min_size);
2636 struct wined3d_buffer *buffer;
2638 TRACE("Growing index buffer to %u bytes.\n", size);
2640 hr = wined3d_buffer_create_ib(device->wined3d_device, size, WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_WRITEONLY,
2641 WINED3D_POOL_DEFAULT, NULL, &d3d9_null_wined3d_parent_ops, &buffer);
2642 if (FAILED(hr))
2644 ERR("(%p) wined3d_buffer_create_ib failed with hr = %08x.\n", device, hr);
2645 return hr;
2648 if (device->index_buffer)
2649 wined3d_buffer_decref(device->index_buffer);
2651 device->index_buffer = buffer;
2652 device->index_buffer_size = size;
2653 device->index_buffer_pos = 0;
2655 return D3D_OK;
2658 static HRESULT WINAPI d3d9_device_DrawIndexedPrimitiveUP(IDirect3DDevice9Ex *iface,
2659 D3DPRIMITIVETYPE primitive_type, UINT min_vertex_idx, UINT vertex_count,
2660 UINT primitive_count, const void *index_data, D3DFORMAT index_format,
2661 const void *vertex_data, UINT vertex_stride)
2663 UINT idx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2664 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2665 UINT idx_fmt_size = index_format == D3DFMT_INDEX16 ? 2 : 4;
2666 UINT vtx_size = vertex_count * vertex_stride;
2667 UINT idx_size = idx_count * idx_fmt_size;
2668 struct wined3d_map_desc wined3d_map_desc;
2669 struct wined3d_box wined3d_box = {0};
2670 struct wined3d_resource *ib, *vb;
2671 UINT vb_pos, ib_pos, align;
2672 HRESULT hr;
2674 TRACE("iface %p, primitive_type %#x, min_vertex_idx %u, vertex_count %u, primitive_count %u, "
2675 "index_data %p, index_format %#x, vertex_data %p, vertex_stride %u.\n",
2676 iface, primitive_type, min_vertex_idx, vertex_count, primitive_count,
2677 index_data, index_format, vertex_data, vertex_stride);
2679 if (!primitive_count)
2681 WARN("primitive_count is 0, returning D3D_OK.\n");
2682 return D3D_OK;
2685 wined3d_mutex_lock();
2687 if (!device->has_vertex_declaration)
2689 wined3d_mutex_unlock();
2690 WARN("Called without a valid vertex declaration set.\n");
2691 return D3DERR_INVALIDCALL;
2694 hr = d3d9_device_prepare_vertex_buffer(device, vtx_size);
2695 if (FAILED(hr))
2696 goto done;
2698 vb_pos = device->vertex_buffer_pos;
2699 align = vb_pos % vertex_stride;
2700 if (align) align = vertex_stride - align;
2701 if (vb_pos + vtx_size + align > device->vertex_buffer_size)
2702 vb_pos = 0;
2703 else
2704 vb_pos += align;
2706 wined3d_box.left = vb_pos;
2707 wined3d_box.right = vb_pos + vtx_size;
2708 vb = wined3d_buffer_get_resource(device->vertex_buffer);
2709 if (FAILED(hr = wined3d_resource_map(vb, 0, &wined3d_map_desc, &wined3d_box,
2710 vb_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD)))
2711 goto done;
2712 memcpy(wined3d_map_desc.data, (char *)vertex_data + min_vertex_idx * vertex_stride, vtx_size);
2713 wined3d_resource_unmap(vb, 0);
2714 device->vertex_buffer_pos = vb_pos + vtx_size;
2716 hr = d3d9_device_prepare_index_buffer(device, idx_size);
2717 if (FAILED(hr))
2718 goto done;
2720 ib_pos = device->index_buffer_pos;
2721 align = ib_pos % idx_fmt_size;
2722 if (align) align = idx_fmt_size - align;
2723 if (ib_pos + idx_size + align > device->index_buffer_size)
2724 ib_pos = 0;
2725 else
2726 ib_pos += align;
2728 wined3d_box.left = ib_pos;
2729 wined3d_box.right = ib_pos + idx_size;
2730 ib = wined3d_buffer_get_resource(device->index_buffer);
2731 if (FAILED(hr = wined3d_resource_map(ib, 0, &wined3d_map_desc, &wined3d_box,
2732 ib_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD)))
2733 goto done;
2734 memcpy(wined3d_map_desc.data, index_data, idx_size);
2735 wined3d_resource_unmap(ib, 0);
2736 device->index_buffer_pos = ib_pos + idx_size;
2738 hr = wined3d_device_set_stream_source(device->wined3d_device, 0, device->vertex_buffer, 0, vertex_stride);
2739 if (FAILED(hr))
2740 goto done;
2742 wined3d_device_set_index_buffer(device->wined3d_device, device->index_buffer,
2743 wined3dformat_from_d3dformat(index_format), 0);
2744 wined3d_device_set_base_vertex_index(device->wined3d_device, vb_pos / vertex_stride - min_vertex_idx);
2746 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type, 0);
2747 hr = wined3d_device_draw_indexed_primitive(device->wined3d_device, ib_pos / idx_fmt_size, idx_count);
2749 wined3d_device_set_stream_source(device->wined3d_device, 0, NULL, 0, 0);
2750 wined3d_device_set_index_buffer(device->wined3d_device, NULL, WINED3DFMT_UNKNOWN, 0);
2751 wined3d_device_set_base_vertex_index(device->wined3d_device, 0);
2753 done:
2754 wined3d_mutex_unlock();
2755 return hr;
2758 static HRESULT WINAPI d3d9_device_ProcessVertices(IDirect3DDevice9Ex *iface,
2759 UINT src_start_idx, UINT dst_idx, UINT vertex_count, IDirect3DVertexBuffer9 *dst_buffer,
2760 IDirect3DVertexDeclaration9 *declaration, DWORD flags)
2762 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2763 struct d3d9_vertexbuffer *dst_impl = unsafe_impl_from_IDirect3DVertexBuffer9(dst_buffer);
2764 struct d3d9_vertex_declaration *decl_impl = unsafe_impl_from_IDirect3DVertexDeclaration9(declaration);
2765 HRESULT hr;
2767 TRACE("iface %p, src_start_idx %u, dst_idx %u, vertex_count %u, dst_buffer %p, declaration %p, flags %#x.\n",
2768 iface, src_start_idx, dst_idx, vertex_count, dst_buffer, declaration, flags);
2770 wined3d_mutex_lock();
2771 hr = wined3d_device_process_vertices(device->wined3d_device, src_start_idx, dst_idx, vertex_count,
2772 dst_impl->wined3d_buffer, decl_impl ? decl_impl->wined3d_declaration : NULL,
2773 flags, dst_impl->fvf);
2774 wined3d_mutex_unlock();
2776 return hr;
2779 static HRESULT WINAPI d3d9_device_CreateVertexDeclaration(IDirect3DDevice9Ex *iface,
2780 const D3DVERTEXELEMENT9 *elements, IDirect3DVertexDeclaration9 **declaration)
2782 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2783 struct d3d9_vertex_declaration *object;
2784 HRESULT hr;
2786 TRACE("iface %p, elements %p, declaration %p.\n", iface, elements, declaration);
2788 if (!declaration)
2790 WARN("Caller passed a NULL declaration, returning D3DERR_INVALIDCALL.\n");
2791 return D3DERR_INVALIDCALL;
2794 if (SUCCEEDED(hr = d3d9_vertex_declaration_create(device, elements, &object)))
2795 *declaration = &object->IDirect3DVertexDeclaration9_iface;
2797 return hr;
2800 static HRESULT WINAPI d3d9_device_SetVertexDeclaration(IDirect3DDevice9Ex *iface,
2801 IDirect3DVertexDeclaration9 *declaration)
2803 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2804 struct d3d9_vertex_declaration *decl_impl = unsafe_impl_from_IDirect3DVertexDeclaration9(declaration);
2806 TRACE("iface %p, declaration %p.\n", iface, declaration);
2808 wined3d_mutex_lock();
2809 wined3d_device_set_vertex_declaration(device->wined3d_device,
2810 decl_impl ? decl_impl->wined3d_declaration : NULL);
2811 device->has_vertex_declaration = !!decl_impl;
2812 wined3d_mutex_unlock();
2814 return D3D_OK;
2817 static HRESULT WINAPI d3d9_device_GetVertexDeclaration(IDirect3DDevice9Ex *iface,
2818 IDirect3DVertexDeclaration9 **declaration)
2820 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2821 struct wined3d_vertex_declaration *wined3d_declaration;
2822 struct d3d9_vertex_declaration *declaration_impl;
2824 TRACE("iface %p, declaration %p.\n", iface, declaration);
2826 if (!declaration) return D3DERR_INVALIDCALL;
2828 wined3d_mutex_lock();
2829 if ((wined3d_declaration = wined3d_device_get_vertex_declaration(device->wined3d_device)))
2831 declaration_impl = wined3d_vertex_declaration_get_parent(wined3d_declaration);
2832 *declaration = &declaration_impl->IDirect3DVertexDeclaration9_iface;
2833 IDirect3DVertexDeclaration9_AddRef(*declaration);
2835 else
2837 *declaration = NULL;
2839 wined3d_mutex_unlock();
2841 TRACE("Returning %p.\n", *declaration);
2842 return D3D_OK;
2845 static struct wined3d_vertex_declaration *device_get_fvf_declaration(struct d3d9_device *device, DWORD fvf)
2847 struct wined3d_vertex_declaration *wined3d_declaration;
2848 struct fvf_declaration *fvf_decls = device->fvf_decls;
2849 struct d3d9_vertex_declaration *d3d9_declaration;
2850 D3DVERTEXELEMENT9 *elements;
2851 int p, low, high; /* deliberately signed */
2852 HRESULT hr;
2854 TRACE("Searching for declaration for fvf %08x... ", fvf);
2856 low = 0;
2857 high = device->fvf_decl_count - 1;
2858 while (low <= high)
2860 p = (low + high) >> 1;
2861 TRACE("%d ", p);
2863 if (fvf_decls[p].fvf == fvf)
2865 TRACE("found %p.\n", fvf_decls[p].decl);
2866 return fvf_decls[p].decl;
2869 if (fvf_decls[p].fvf < fvf)
2870 low = p + 1;
2871 else
2872 high = p - 1;
2874 TRACE("not found. Creating and inserting at position %d.\n", low);
2876 if (FAILED(hr = vdecl_convert_fvf(fvf, &elements)))
2877 return NULL;
2879 hr = d3d9_vertex_declaration_create(device, elements, &d3d9_declaration);
2880 HeapFree(GetProcessHeap(), 0, elements);
2881 if (FAILED(hr))
2882 return NULL;
2884 if (device->fvf_decl_size == device->fvf_decl_count)
2886 UINT grow = max(device->fvf_decl_size / 2, 8);
2888 fvf_decls = HeapReAlloc(GetProcessHeap(), 0, fvf_decls, sizeof(*fvf_decls) * (device->fvf_decl_size + grow));
2889 if (!fvf_decls)
2891 IDirect3DVertexDeclaration9_Release(&d3d9_declaration->IDirect3DVertexDeclaration9_iface);
2892 return NULL;
2894 device->fvf_decls = fvf_decls;
2895 device->fvf_decl_size += grow;
2898 d3d9_declaration->fvf = fvf;
2899 wined3d_declaration = d3d9_declaration->wined3d_declaration;
2900 wined3d_vertex_declaration_incref(wined3d_declaration);
2901 IDirect3DVertexDeclaration9_Release(&d3d9_declaration->IDirect3DVertexDeclaration9_iface);
2903 memmove(fvf_decls + low + 1, fvf_decls + low, sizeof(*fvf_decls) * (device->fvf_decl_count - low));
2904 fvf_decls[low].decl = wined3d_declaration;
2905 fvf_decls[low].fvf = fvf;
2906 ++device->fvf_decl_count;
2908 TRACE("Returning %p. %u declarations in array.\n", wined3d_declaration, device->fvf_decl_count);
2910 return wined3d_declaration;
2913 static HRESULT WINAPI d3d9_device_SetFVF(IDirect3DDevice9Ex *iface, DWORD fvf)
2915 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2916 struct wined3d_vertex_declaration *decl;
2918 TRACE("iface %p, fvf %#x.\n", iface, fvf);
2920 if (!fvf)
2922 WARN("%#x is not a valid FVF.\n", fvf);
2923 return D3D_OK;
2926 wined3d_mutex_lock();
2927 if (!(decl = device_get_fvf_declaration(device, fvf)))
2929 wined3d_mutex_unlock();
2930 ERR("Failed to create a vertex declaration for fvf %#x.\n", fvf);
2931 return D3DERR_DRIVERINTERNALERROR;
2934 wined3d_device_set_vertex_declaration(device->wined3d_device, decl);
2935 device->has_vertex_declaration = TRUE;
2936 wined3d_mutex_unlock();
2938 return D3D_OK;
2941 static HRESULT WINAPI d3d9_device_GetFVF(IDirect3DDevice9Ex *iface, DWORD *fvf)
2943 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2944 struct wined3d_vertex_declaration *wined3d_declaration;
2945 struct d3d9_vertex_declaration *d3d9_declaration;
2947 TRACE("iface %p, fvf %p.\n", iface, fvf);
2949 wined3d_mutex_lock();
2950 if ((wined3d_declaration = wined3d_device_get_vertex_declaration(device->wined3d_device)))
2952 d3d9_declaration = wined3d_vertex_declaration_get_parent(wined3d_declaration);
2953 *fvf = d3d9_declaration->fvf;
2955 else
2957 *fvf = 0;
2959 wined3d_mutex_unlock();
2961 TRACE("Returning FVF %#x.\n", *fvf);
2963 return D3D_OK;
2966 static HRESULT WINAPI d3d9_device_CreateVertexShader(IDirect3DDevice9Ex *iface,
2967 const DWORD *byte_code, IDirect3DVertexShader9 **shader)
2969 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2970 struct d3d9_vertexshader *object;
2971 HRESULT hr;
2973 TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
2975 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2976 if (!object)
2977 return E_OUTOFMEMORY;
2979 hr = vertexshader_init(object, device, byte_code);
2980 if (FAILED(hr))
2982 WARN("Failed to initialize vertex shader, hr %#x.\n", hr);
2983 HeapFree(GetProcessHeap(), 0, object);
2984 return hr;
2987 TRACE("Created vertex shader %p.\n", object);
2988 *shader = &object->IDirect3DVertexShader9_iface;
2990 return D3D_OK;
2993 static HRESULT WINAPI d3d9_device_SetVertexShader(IDirect3DDevice9Ex *iface, IDirect3DVertexShader9 *shader)
2995 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2996 struct d3d9_vertexshader *shader_obj = unsafe_impl_from_IDirect3DVertexShader9(shader);
2998 TRACE("iface %p, shader %p.\n", iface, shader);
3000 wined3d_mutex_lock();
3001 wined3d_device_set_vertex_shader(device->wined3d_device,
3002 shader_obj ? shader_obj->wined3d_shader : NULL);
3003 wined3d_mutex_unlock();
3005 return D3D_OK;
3008 static HRESULT WINAPI d3d9_device_GetVertexShader(IDirect3DDevice9Ex *iface, IDirect3DVertexShader9 **shader)
3010 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3011 struct d3d9_vertexshader *shader_impl;
3012 struct wined3d_shader *wined3d_shader;
3014 TRACE("iface %p, shader %p.\n", iface, shader);
3016 wined3d_mutex_lock();
3017 if ((wined3d_shader = wined3d_device_get_vertex_shader(device->wined3d_device)))
3019 shader_impl = wined3d_shader_get_parent(wined3d_shader);
3020 *shader = &shader_impl->IDirect3DVertexShader9_iface;
3021 IDirect3DVertexShader9_AddRef(*shader);
3023 else
3025 *shader = NULL;
3027 wined3d_mutex_unlock();
3029 TRACE("Returning %p.\n", *shader);
3031 return D3D_OK;
3034 static HRESULT WINAPI d3d9_device_SetVertexShaderConstantF(IDirect3DDevice9Ex *iface,
3035 UINT reg_idx, const float *data, UINT count)
3037 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3038 HRESULT hr;
3040 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3042 if (reg_idx + count > D3D9_MAX_VERTEX_SHADER_CONSTANTF)
3044 WARN("Trying to access %u constants, but d3d9 only supports %u\n",
3045 reg_idx + count, D3D9_MAX_VERTEX_SHADER_CONSTANTF);
3046 return D3DERR_INVALIDCALL;
3049 wined3d_mutex_lock();
3050 hr = wined3d_device_set_vs_consts_f(device->wined3d_device,
3051 reg_idx, count, (const struct wined3d_vec4 *)data);
3052 wined3d_mutex_unlock();
3054 return hr;
3057 static HRESULT WINAPI d3d9_device_GetVertexShaderConstantF(IDirect3DDevice9Ex *iface,
3058 UINT reg_idx, float *data, UINT count)
3060 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3061 HRESULT hr;
3063 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3065 if (reg_idx + count > D3D9_MAX_VERTEX_SHADER_CONSTANTF)
3067 WARN("Trying to access %u constants, but d3d9 only supports %u\n",
3068 reg_idx + count, D3D9_MAX_VERTEX_SHADER_CONSTANTF);
3069 return D3DERR_INVALIDCALL;
3072 wined3d_mutex_lock();
3073 hr = wined3d_device_get_vs_consts_f(device->wined3d_device,
3074 reg_idx, count, (struct wined3d_vec4 *)data);
3075 wined3d_mutex_unlock();
3077 return hr;
3080 static HRESULT WINAPI d3d9_device_SetVertexShaderConstantI(IDirect3DDevice9Ex *iface,
3081 UINT reg_idx, const int *data, UINT count)
3083 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3084 HRESULT hr;
3086 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3088 wined3d_mutex_lock();
3089 hr = wined3d_device_set_vs_consts_i(device->wined3d_device,
3090 reg_idx, count, (const struct wined3d_ivec4 *)data);
3091 wined3d_mutex_unlock();
3093 return hr;
3096 static HRESULT WINAPI d3d9_device_GetVertexShaderConstantI(IDirect3DDevice9Ex *iface,
3097 UINT reg_idx, int *data, UINT count)
3099 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3100 HRESULT hr;
3102 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3104 wined3d_mutex_lock();
3105 hr = wined3d_device_get_vs_consts_i(device->wined3d_device,
3106 reg_idx, count, (struct wined3d_ivec4 *)data);
3107 wined3d_mutex_unlock();
3109 return hr;
3112 static HRESULT WINAPI d3d9_device_SetVertexShaderConstantB(IDirect3DDevice9Ex *iface,
3113 UINT reg_idx, const BOOL *data, UINT count)
3115 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3116 HRESULT hr;
3118 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3120 wined3d_mutex_lock();
3121 hr = wined3d_device_set_vs_consts_b(device->wined3d_device, reg_idx, count, data);
3122 wined3d_mutex_unlock();
3124 return hr;
3127 static HRESULT WINAPI d3d9_device_GetVertexShaderConstantB(IDirect3DDevice9Ex *iface,
3128 UINT reg_idx, BOOL *data, UINT count)
3130 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3131 HRESULT hr;
3133 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3135 wined3d_mutex_lock();
3136 hr = wined3d_device_get_vs_consts_b(device->wined3d_device, reg_idx, count, data);
3137 wined3d_mutex_unlock();
3139 return hr;
3142 static HRESULT WINAPI d3d9_device_SetStreamSource(IDirect3DDevice9Ex *iface,
3143 UINT stream_idx, IDirect3DVertexBuffer9 *buffer, UINT offset, UINT stride)
3145 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3146 struct d3d9_vertexbuffer *buffer_impl = unsafe_impl_from_IDirect3DVertexBuffer9(buffer);
3147 HRESULT hr;
3149 TRACE("iface %p, stream_idx %u, buffer %p, offset %u, stride %u.\n",
3150 iface, stream_idx, buffer, offset, stride);
3152 wined3d_mutex_lock();
3153 hr = wined3d_device_set_stream_source(device->wined3d_device, stream_idx,
3154 buffer_impl ? buffer_impl->wined3d_buffer : NULL, offset, stride);
3155 wined3d_mutex_unlock();
3157 return hr;
3160 static HRESULT WINAPI d3d9_device_GetStreamSource(IDirect3DDevice9Ex *iface,
3161 UINT stream_idx, IDirect3DVertexBuffer9 **buffer, UINT *offset, UINT *stride)
3163 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3164 struct d3d9_vertexbuffer *buffer_impl;
3165 struct wined3d_buffer *wined3d_buffer;
3166 HRESULT hr;
3168 TRACE("iface %p, stream_idx %u, buffer %p, offset %p, stride %p.\n",
3169 iface, stream_idx, buffer, offset, stride);
3171 if (!buffer)
3172 return D3DERR_INVALIDCALL;
3174 wined3d_mutex_lock();
3175 hr = wined3d_device_get_stream_source(device->wined3d_device, stream_idx, &wined3d_buffer, offset, stride);
3176 if (SUCCEEDED(hr) && wined3d_buffer)
3178 buffer_impl = wined3d_buffer_get_parent(wined3d_buffer);
3179 *buffer = &buffer_impl->IDirect3DVertexBuffer9_iface;
3180 IDirect3DVertexBuffer9_AddRef(*buffer);
3182 else
3184 if (FAILED(hr))
3185 FIXME("Call to GetStreamSource failed %p %p\n", offset, stride);
3186 *buffer = NULL;
3188 wined3d_mutex_unlock();
3190 return hr;
3193 static HRESULT WINAPI d3d9_device_SetStreamSourceFreq(IDirect3DDevice9Ex *iface, UINT stream_idx, UINT freq)
3195 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3196 HRESULT hr;
3198 TRACE("iface %p, stream_idx %u, freq %u.\n", iface, stream_idx, freq);
3200 wined3d_mutex_lock();
3201 hr = wined3d_device_set_stream_source_freq(device->wined3d_device, stream_idx, freq);
3202 wined3d_mutex_unlock();
3204 return hr;
3207 static HRESULT WINAPI d3d9_device_GetStreamSourceFreq(IDirect3DDevice9Ex *iface, UINT stream_idx, UINT *freq)
3209 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3210 HRESULT hr;
3212 TRACE("iface %p, stream_idx %u, freq %p.\n", iface, stream_idx, freq);
3214 wined3d_mutex_lock();
3215 hr = wined3d_device_get_stream_source_freq(device->wined3d_device, stream_idx, freq);
3216 wined3d_mutex_unlock();
3218 return hr;
3221 static HRESULT WINAPI d3d9_device_SetIndices(IDirect3DDevice9Ex *iface, IDirect3DIndexBuffer9 *buffer)
3223 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3224 struct d3d9_indexbuffer *ib = unsafe_impl_from_IDirect3DIndexBuffer9(buffer);
3226 TRACE("iface %p, buffer %p.\n", iface, buffer);
3228 wined3d_mutex_lock();
3229 wined3d_device_set_index_buffer(device->wined3d_device,
3230 ib ? ib->wined3d_buffer : NULL, ib ? ib->format : WINED3DFMT_UNKNOWN, 0);
3231 wined3d_mutex_unlock();
3233 return D3D_OK;
3236 static HRESULT WINAPI d3d9_device_GetIndices(IDirect3DDevice9Ex *iface, IDirect3DIndexBuffer9 **buffer)
3238 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3239 enum wined3d_format_id wined3d_format;
3240 struct wined3d_buffer *wined3d_buffer;
3241 struct d3d9_indexbuffer *buffer_impl;
3243 TRACE("iface %p, buffer %p.\n", iface, buffer);
3245 if (!buffer)
3246 return D3DERR_INVALIDCALL;
3248 wined3d_mutex_lock();
3249 if ((wined3d_buffer = wined3d_device_get_index_buffer(device->wined3d_device, &wined3d_format, NULL)))
3251 buffer_impl = wined3d_buffer_get_parent(wined3d_buffer);
3252 *buffer = &buffer_impl->IDirect3DIndexBuffer9_iface;
3253 IDirect3DIndexBuffer9_AddRef(*buffer);
3255 else
3257 *buffer = NULL;
3259 wined3d_mutex_unlock();
3261 return D3D_OK;
3264 static HRESULT WINAPI d3d9_device_CreatePixelShader(IDirect3DDevice9Ex *iface,
3265 const DWORD *byte_code, IDirect3DPixelShader9 **shader)
3267 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3268 struct d3d9_pixelshader *object;
3269 HRESULT hr;
3271 TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
3273 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
3274 if (!object)
3276 FIXME("Failed to allocate pixel shader memory.\n");
3277 return E_OUTOFMEMORY;
3280 hr = pixelshader_init(object, device, byte_code);
3281 if (FAILED(hr))
3283 WARN("Failed to initialize pixel shader, hr %#x.\n", hr);
3284 HeapFree(GetProcessHeap(), 0, object);
3285 return hr;
3288 TRACE("Created pixel shader %p.\n", object);
3289 *shader = &object->IDirect3DPixelShader9_iface;
3291 return D3D_OK;
3294 static HRESULT WINAPI d3d9_device_SetPixelShader(IDirect3DDevice9Ex *iface, IDirect3DPixelShader9 *shader)
3296 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3297 struct d3d9_pixelshader *shader_obj = unsafe_impl_from_IDirect3DPixelShader9(shader);
3299 TRACE("iface %p, shader %p.\n", iface, shader);
3301 wined3d_mutex_lock();
3302 wined3d_device_set_pixel_shader(device->wined3d_device,
3303 shader_obj ? shader_obj->wined3d_shader : NULL);
3304 wined3d_mutex_unlock();
3306 return D3D_OK;
3309 static HRESULT WINAPI d3d9_device_GetPixelShader(IDirect3DDevice9Ex *iface, IDirect3DPixelShader9 **shader)
3311 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3312 struct d3d9_pixelshader *shader_impl;
3313 struct wined3d_shader *wined3d_shader;
3315 TRACE("iface %p, shader %p.\n", iface, shader);
3317 if (!shader) return D3DERR_INVALIDCALL;
3319 wined3d_mutex_lock();
3320 if ((wined3d_shader = wined3d_device_get_pixel_shader(device->wined3d_device)))
3322 shader_impl = wined3d_shader_get_parent(wined3d_shader);
3323 *shader = &shader_impl->IDirect3DPixelShader9_iface;
3324 IDirect3DPixelShader9_AddRef(*shader);
3326 else
3328 *shader = NULL;
3330 wined3d_mutex_unlock();
3332 TRACE("Returning %p.\n", *shader);
3334 return D3D_OK;
3337 static HRESULT WINAPI d3d9_device_SetPixelShaderConstantF(IDirect3DDevice9Ex *iface,
3338 UINT reg_idx, const float *data, UINT count)
3340 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3341 HRESULT hr;
3343 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3345 wined3d_mutex_lock();
3346 hr = wined3d_device_set_ps_consts_f(device->wined3d_device,
3347 reg_idx, count, (const struct wined3d_vec4 *)data);
3348 wined3d_mutex_unlock();
3350 return hr;
3353 static HRESULT WINAPI d3d9_device_GetPixelShaderConstantF(IDirect3DDevice9Ex *iface,
3354 UINT reg_idx, float *data, UINT count)
3356 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3357 HRESULT hr;
3359 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3361 wined3d_mutex_lock();
3362 hr = wined3d_device_get_ps_consts_f(device->wined3d_device,
3363 reg_idx, count, (struct wined3d_vec4 *)data);
3364 wined3d_mutex_unlock();
3366 return hr;
3369 static HRESULT WINAPI d3d9_device_SetPixelShaderConstantI(IDirect3DDevice9Ex *iface,
3370 UINT reg_idx, const int *data, UINT count)
3372 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3373 HRESULT hr;
3375 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3377 wined3d_mutex_lock();
3378 hr = wined3d_device_set_ps_consts_i(device->wined3d_device,
3379 reg_idx, count, (const struct wined3d_ivec4 *)data);
3380 wined3d_mutex_unlock();
3382 return hr;
3385 static HRESULT WINAPI d3d9_device_GetPixelShaderConstantI(IDirect3DDevice9Ex *iface,
3386 UINT reg_idx, int *data, UINT count)
3388 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3389 HRESULT hr;
3391 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3393 wined3d_mutex_lock();
3394 hr = wined3d_device_get_ps_consts_i(device->wined3d_device,
3395 reg_idx, count, (struct wined3d_ivec4 *)data);
3396 wined3d_mutex_unlock();
3398 return hr;
3401 static HRESULT WINAPI d3d9_device_SetPixelShaderConstantB(IDirect3DDevice9Ex *iface,
3402 UINT reg_idx, const BOOL *data, UINT count)
3404 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3405 HRESULT hr;
3407 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3409 wined3d_mutex_lock();
3410 hr = wined3d_device_set_ps_consts_b(device->wined3d_device, reg_idx, count, data);
3411 wined3d_mutex_unlock();
3413 return hr;
3416 static HRESULT WINAPI d3d9_device_GetPixelShaderConstantB(IDirect3DDevice9Ex *iface,
3417 UINT reg_idx, BOOL *data, UINT count)
3419 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3420 HRESULT hr;
3422 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3424 wined3d_mutex_lock();
3425 hr = wined3d_device_get_ps_consts_b(device->wined3d_device, reg_idx, count, data);
3426 wined3d_mutex_unlock();
3428 return hr;
3431 static HRESULT WINAPI d3d9_device_DrawRectPatch(IDirect3DDevice9Ex *iface, UINT handle,
3432 const float *segment_count, const D3DRECTPATCH_INFO *patch_info)
3434 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
3435 iface, handle, segment_count, patch_info);
3436 return D3D_OK;
3439 static HRESULT WINAPI d3d9_device_DrawTriPatch(IDirect3DDevice9Ex *iface, UINT handle,
3440 const float *segment_count, const D3DTRIPATCH_INFO *patch_info)
3442 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
3443 iface, handle, segment_count, patch_info);
3444 return D3D_OK;
3447 static HRESULT WINAPI d3d9_device_DeletePatch(IDirect3DDevice9Ex *iface, UINT handle)
3449 FIXME("iface %p, handle %#x unimplemented.\n", iface, handle);
3450 return D3DERR_INVALIDCALL;
3453 static HRESULT WINAPI d3d9_device_CreateQuery(IDirect3DDevice9Ex *iface, D3DQUERYTYPE type, IDirect3DQuery9 **query)
3455 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3456 struct d3d9_query *object;
3457 HRESULT hr;
3459 TRACE("iface %p, type %#x, query %p.\n", iface, type, query);
3461 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
3462 if (!object)
3463 return E_OUTOFMEMORY;
3465 hr = query_init(object, device, type);
3466 if (FAILED(hr))
3468 WARN("Failed to initialize query, hr %#x.\n", hr);
3469 HeapFree(GetProcessHeap(), 0, object);
3470 return hr;
3473 TRACE("Created query %p.\n", object);
3474 if (query) *query = &object->IDirect3DQuery9_iface;
3475 else IDirect3DQuery9_Release(&object->IDirect3DQuery9_iface);
3477 return D3D_OK;
3480 static HRESULT WINAPI d3d9_device_SetConvolutionMonoKernel(IDirect3DDevice9Ex *iface,
3481 UINT width, UINT height, float *rows, float *columns)
3483 FIXME("iface %p, width %u, height %u, rows %p, columns %p stub!\n",
3484 iface, width, height, rows, columns);
3486 return E_NOTIMPL;
3489 static HRESULT WINAPI d3d9_device_ComposeRects(IDirect3DDevice9Ex *iface,
3490 IDirect3DSurface9 *src_surface, IDirect3DSurface9 *dst_surface, IDirect3DVertexBuffer9 *src_descs,
3491 UINT rect_count, IDirect3DVertexBuffer9 *dst_descs, D3DCOMPOSERECTSOP operation, INT offset_x, INT offset_y)
3493 FIXME("iface %p, src_surface %p, dst_surface %p, src_descs %p, rect_count %u, "
3494 "dst_descs %p, operation %#x, offset_x %u, offset_y %u stub!\n",
3495 iface, src_surface, dst_surface, src_descs, rect_count,
3496 dst_descs, operation, offset_x, offset_y);
3498 return E_NOTIMPL;
3501 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_PresentEx(IDirect3DDevice9Ex *iface,
3502 const RECT *src_rect, const RECT *dst_rect, HWND dst_window_override,
3503 const RGNDATA *dirty_region, DWORD flags)
3505 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3506 UINT i;
3507 HRESULT hr;
3509 TRACE("iface %p, src_rect %s, dst_rect %s, dst_window_override %p, dirty_region %p, flags %#x.\n",
3510 iface, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect),
3511 dst_window_override, dirty_region, flags);
3513 if (device->device_state != D3D9_DEVICE_STATE_OK)
3514 return S_PRESENT_OCCLUDED;
3516 if (dirty_region)
3517 FIXME("Ignoring dirty_region %p.\n", dirty_region);
3519 wined3d_mutex_lock();
3520 for (i = 0; i < device->implicit_swapchain_count; ++i)
3522 if (FAILED(hr = wined3d_swapchain_present(device->implicit_swapchains[i]->wined3d_swapchain,
3523 src_rect, dst_rect, dst_window_override, flags)))
3525 wined3d_mutex_unlock();
3526 return hr;
3529 wined3d_mutex_unlock();
3531 return D3D_OK;
3534 static HRESULT WINAPI d3d9_device_GetGPUThreadPriority(IDirect3DDevice9Ex *iface, INT *priority)
3536 FIXME("iface %p, priority %p stub!\n", iface, priority);
3538 return E_NOTIMPL;
3541 static HRESULT WINAPI d3d9_device_SetGPUThreadPriority(IDirect3DDevice9Ex *iface, INT priority)
3543 FIXME("iface %p, priority %d stub!\n", iface, priority);
3545 return E_NOTIMPL;
3548 static HRESULT WINAPI d3d9_device_WaitForVBlank(IDirect3DDevice9Ex *iface, UINT swapchain_idx)
3550 FIXME("iface %p, swapchain_idx %u stub!\n", iface, swapchain_idx);
3552 return E_NOTIMPL;
3555 static HRESULT WINAPI d3d9_device_CheckResourceResidency(IDirect3DDevice9Ex *iface,
3556 IDirect3DResource9 **resources, UINT32 resource_count)
3558 FIXME("iface %p, resources %p, resource_count %u stub!\n",
3559 iface, resources, resource_count);
3561 return E_NOTIMPL;
3564 static HRESULT WINAPI d3d9_device_SetMaximumFrameLatency(IDirect3DDevice9Ex *iface, UINT max_latency)
3566 TRACE("iface %p, max_latency %u.\n", iface, max_latency);
3568 if (max_latency)
3569 FIXME("Ignoring max_latency %u.\n", max_latency);
3571 return S_OK;
3574 static HRESULT WINAPI d3d9_device_GetMaximumFrameLatency(IDirect3DDevice9Ex *iface, UINT *max_latency)
3576 FIXME("iface %p, max_latency %p stub!\n", iface, max_latency);
3578 *max_latency = 2;
3580 return E_NOTIMPL;
3583 static HRESULT WINAPI d3d9_device_CheckDeviceState(IDirect3DDevice9Ex *iface, HWND dst_window)
3585 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3586 struct wined3d_swapchain_desc swapchain_desc;
3588 TRACE("iface %p, dst_window %p.\n", iface, dst_window);
3590 wined3d_mutex_lock();
3591 wined3d_swapchain_get_desc(device->implicit_swapchains[0]->wined3d_swapchain, &swapchain_desc);
3592 wined3d_mutex_unlock();
3594 if (swapchain_desc.windowed)
3595 return D3D_OK;
3597 /* FIXME: This is actually supposed to check if any other device is in
3598 * fullscreen mode. */
3599 if (dst_window != swapchain_desc.device_window)
3600 return device->device_state == D3D9_DEVICE_STATE_OK ? S_PRESENT_OCCLUDED : D3D_OK;
3602 return device->device_state == D3D9_DEVICE_STATE_OK ? D3D_OK : S_PRESENT_OCCLUDED;
3605 static HRESULT WINAPI d3d9_device_CreateRenderTargetEx(IDirect3DDevice9Ex *iface,
3606 UINT width, UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
3607 BOOL lockable, IDirect3DSurface9 **surface, HANDLE *shared_handle, DWORD usage)
3609 FIXME("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u, "
3610 "lockable %#x, surface %p, shared_handle %p, usage %#x stub!\n",
3611 iface, width, height, format, multisample_type, multisample_quality,
3612 lockable, surface, shared_handle, usage);
3614 *surface = NULL;
3615 if (shared_handle)
3616 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
3618 return E_NOTIMPL;
3621 static HRESULT WINAPI d3d9_device_CreateOffscreenPlainSurfaceEx(IDirect3DDevice9Ex *iface,
3622 UINT width, UINT height, D3DFORMAT format, D3DPOOL pool, IDirect3DSurface9 **surface,
3623 HANDLE *shared_handle, DWORD usage)
3625 FIXME("iface %p, width %u, height %u, format %#x, pool %#x, surface %p, shared_handle %p, usage %#x stub!\n",
3626 iface, width, height, format, pool, surface, shared_handle, usage);
3628 return E_NOTIMPL;
3631 static HRESULT WINAPI d3d9_device_CreateDepthStencilSurfaceEx(IDirect3DDevice9Ex *iface,
3632 UINT width, UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
3633 BOOL discard, IDirect3DSurface9 **surface, HANDLE *shared_handle, DWORD usage)
3635 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3636 DWORD flags = WINED3D_TEXTURE_CREATE_MAPPABLE;
3638 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u, "
3639 "discard %#x, surface %p, shared_handle %p, usage %#x.\n",
3640 iface, width, height, format, multisample_type, multisample_quality,
3641 discard, surface, shared_handle, usage);
3643 if (usage & D3DUSAGE_DEPTHSTENCIL)
3645 WARN("Invalid usage %#x.\n", usage);
3646 return D3DERR_INVALIDCALL;
3649 if (shared_handle)
3650 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
3652 if (discard)
3653 flags |= WINED3D_TEXTURE_CREATE_DISCARD;
3655 *surface = NULL;
3656 return d3d9_device_create_surface(device, width, height, format, flags, surface,
3657 D3DUSAGE_DEPTHSTENCIL | usage, D3DPOOL_DEFAULT, multisample_type, multisample_quality, NULL);
3660 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_ResetEx(IDirect3DDevice9Ex *iface,
3661 D3DPRESENT_PARAMETERS *present_parameters, D3DDISPLAYMODEEX *mode)
3663 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3665 TRACE("iface %p, present_parameters %p, mode %p.\n", iface, present_parameters, mode);
3667 if (!present_parameters->Windowed == !mode)
3669 WARN("Mode can be passed if and only if Windowed is FALSE.\n");
3670 return D3DERR_INVALIDCALL;
3673 if (mode && (mode->Width != present_parameters->BackBufferWidth
3674 || mode->Height != present_parameters->BackBufferHeight))
3676 WARN("Mode and back buffer mismatch (mode %ux%u, backbuffer %ux%u).\n",
3677 mode->Width, mode->Height,
3678 present_parameters->BackBufferWidth, present_parameters->BackBufferHeight);
3679 return D3DERR_INVALIDCALL;
3682 return d3d9_device_reset(device, present_parameters, mode);
3685 static HRESULT WINAPI d3d9_device_GetDisplayModeEx(IDirect3DDevice9Ex *iface,
3686 UINT swapchain_idx, D3DDISPLAYMODEEX *mode, D3DDISPLAYROTATION *rotation)
3688 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3689 struct wined3d_display_mode wined3d_mode;
3690 HRESULT hr;
3692 TRACE("iface %p, swapchain_idx %u, mode %p, rotation %p.\n",
3693 iface, swapchain_idx, mode, rotation);
3695 if (mode->Size != sizeof(*mode))
3696 return D3DERR_INVALIDCALL;
3698 wined3d_mutex_lock();
3699 hr = wined3d_device_get_display_mode(device->wined3d_device, swapchain_idx, &wined3d_mode,
3700 (enum wined3d_display_rotation *)rotation);
3701 wined3d_mutex_unlock();
3703 if (SUCCEEDED(hr))
3705 mode->Width = wined3d_mode.width;
3706 mode->Height = wined3d_mode.height;
3707 mode->RefreshRate = wined3d_mode.refresh_rate;
3708 mode->Format = d3dformat_from_wined3dformat(wined3d_mode.format_id);
3709 mode->ScanLineOrdering = wined3d_mode.scanline_ordering;
3712 return hr;
3715 static const struct IDirect3DDevice9ExVtbl d3d9_device_vtbl =
3717 /* IUnknown */
3718 d3d9_device_QueryInterface,
3719 d3d9_device_AddRef,
3720 d3d9_device_Release,
3721 /* IDirect3DDevice9 */
3722 d3d9_device_TestCooperativeLevel,
3723 d3d9_device_GetAvailableTextureMem,
3724 d3d9_device_EvictManagedResources,
3725 d3d9_device_GetDirect3D,
3726 d3d9_device_GetDeviceCaps,
3727 d3d9_device_GetDisplayMode,
3728 d3d9_device_GetCreationParameters,
3729 d3d9_device_SetCursorProperties,
3730 d3d9_device_SetCursorPosition,
3731 d3d9_device_ShowCursor,
3732 d3d9_device_CreateAdditionalSwapChain,
3733 d3d9_device_GetSwapChain,
3734 d3d9_device_GetNumberOfSwapChains,
3735 d3d9_device_Reset,
3736 d3d9_device_Present,
3737 d3d9_device_GetBackBuffer,
3738 d3d9_device_GetRasterStatus,
3739 d3d9_device_SetDialogBoxMode,
3740 d3d9_device_SetGammaRamp,
3741 d3d9_device_GetGammaRamp,
3742 d3d9_device_CreateTexture,
3743 d3d9_device_CreateVolumeTexture,
3744 d3d9_device_CreateCubeTexture,
3745 d3d9_device_CreateVertexBuffer,
3746 d3d9_device_CreateIndexBuffer,
3747 d3d9_device_CreateRenderTarget,
3748 d3d9_device_CreateDepthStencilSurface,
3749 d3d9_device_UpdateSurface,
3750 d3d9_device_UpdateTexture,
3751 d3d9_device_GetRenderTargetData,
3752 d3d9_device_GetFrontBufferData,
3753 d3d9_device_StretchRect,
3754 d3d9_device_ColorFill,
3755 d3d9_device_CreateOffscreenPlainSurface,
3756 d3d9_device_SetRenderTarget,
3757 d3d9_device_GetRenderTarget,
3758 d3d9_device_SetDepthStencilSurface,
3759 d3d9_device_GetDepthStencilSurface,
3760 d3d9_device_BeginScene,
3761 d3d9_device_EndScene,
3762 d3d9_device_Clear,
3763 d3d9_device_SetTransform,
3764 d3d9_device_GetTransform,
3765 d3d9_device_MultiplyTransform,
3766 d3d9_device_SetViewport,
3767 d3d9_device_GetViewport,
3768 d3d9_device_SetMaterial,
3769 d3d9_device_GetMaterial,
3770 d3d9_device_SetLight,
3771 d3d9_device_GetLight,
3772 d3d9_device_LightEnable,
3773 d3d9_device_GetLightEnable,
3774 d3d9_device_SetClipPlane,
3775 d3d9_device_GetClipPlane,
3776 d3d9_device_SetRenderState,
3777 d3d9_device_GetRenderState,
3778 d3d9_device_CreateStateBlock,
3779 d3d9_device_BeginStateBlock,
3780 d3d9_device_EndStateBlock,
3781 d3d9_device_SetClipStatus,
3782 d3d9_device_GetClipStatus,
3783 d3d9_device_GetTexture,
3784 d3d9_device_SetTexture,
3785 d3d9_device_GetTextureStageState,
3786 d3d9_device_SetTextureStageState,
3787 d3d9_device_GetSamplerState,
3788 d3d9_device_SetSamplerState,
3789 d3d9_device_ValidateDevice,
3790 d3d9_device_SetPaletteEntries,
3791 d3d9_device_GetPaletteEntries,
3792 d3d9_device_SetCurrentTexturePalette,
3793 d3d9_device_GetCurrentTexturePalette,
3794 d3d9_device_SetScissorRect,
3795 d3d9_device_GetScissorRect,
3796 d3d9_device_SetSoftwareVertexProcessing,
3797 d3d9_device_GetSoftwareVertexProcessing,
3798 d3d9_device_SetNPatchMode,
3799 d3d9_device_GetNPatchMode,
3800 d3d9_device_DrawPrimitive,
3801 d3d9_device_DrawIndexedPrimitive,
3802 d3d9_device_DrawPrimitiveUP,
3803 d3d9_device_DrawIndexedPrimitiveUP,
3804 d3d9_device_ProcessVertices,
3805 d3d9_device_CreateVertexDeclaration,
3806 d3d9_device_SetVertexDeclaration,
3807 d3d9_device_GetVertexDeclaration,
3808 d3d9_device_SetFVF,
3809 d3d9_device_GetFVF,
3810 d3d9_device_CreateVertexShader,
3811 d3d9_device_SetVertexShader,
3812 d3d9_device_GetVertexShader,
3813 d3d9_device_SetVertexShaderConstantF,
3814 d3d9_device_GetVertexShaderConstantF,
3815 d3d9_device_SetVertexShaderConstantI,
3816 d3d9_device_GetVertexShaderConstantI,
3817 d3d9_device_SetVertexShaderConstantB,
3818 d3d9_device_GetVertexShaderConstantB,
3819 d3d9_device_SetStreamSource,
3820 d3d9_device_GetStreamSource,
3821 d3d9_device_SetStreamSourceFreq,
3822 d3d9_device_GetStreamSourceFreq,
3823 d3d9_device_SetIndices,
3824 d3d9_device_GetIndices,
3825 d3d9_device_CreatePixelShader,
3826 d3d9_device_SetPixelShader,
3827 d3d9_device_GetPixelShader,
3828 d3d9_device_SetPixelShaderConstantF,
3829 d3d9_device_GetPixelShaderConstantF,
3830 d3d9_device_SetPixelShaderConstantI,
3831 d3d9_device_GetPixelShaderConstantI,
3832 d3d9_device_SetPixelShaderConstantB,
3833 d3d9_device_GetPixelShaderConstantB,
3834 d3d9_device_DrawRectPatch,
3835 d3d9_device_DrawTriPatch,
3836 d3d9_device_DeletePatch,
3837 d3d9_device_CreateQuery,
3838 /* IDirect3DDevice9Ex */
3839 d3d9_device_SetConvolutionMonoKernel,
3840 d3d9_device_ComposeRects,
3841 d3d9_device_PresentEx,
3842 d3d9_device_GetGPUThreadPriority,
3843 d3d9_device_SetGPUThreadPriority,
3844 d3d9_device_WaitForVBlank,
3845 d3d9_device_CheckResourceResidency,
3846 d3d9_device_SetMaximumFrameLatency,
3847 d3d9_device_GetMaximumFrameLatency,
3848 d3d9_device_CheckDeviceState,
3849 d3d9_device_CreateRenderTargetEx,
3850 d3d9_device_CreateOffscreenPlainSurfaceEx,
3851 d3d9_device_CreateDepthStencilSurfaceEx,
3852 d3d9_device_ResetEx,
3853 d3d9_device_GetDisplayModeEx,
3856 static inline struct d3d9_device *device_from_device_parent(struct wined3d_device_parent *device_parent)
3858 return CONTAINING_RECORD(device_parent, struct d3d9_device, device_parent);
3861 static void CDECL device_parent_wined3d_device_created(struct wined3d_device_parent *device_parent,
3862 struct wined3d_device *device)
3864 TRACE("device_parent %p, device %p.\n", device_parent, device);
3867 static void CDECL device_parent_mode_changed(struct wined3d_device_parent *device_parent)
3869 TRACE("device_parent %p.\n", device_parent);
3872 static void CDECL device_parent_activate(struct wined3d_device_parent *device_parent, BOOL activate)
3874 struct d3d9_device *device = device_from_device_parent(device_parent);
3876 TRACE("device_parent %p, activate %#x.\n", device_parent, activate);
3878 if (!device->d3d_parent)
3879 return;
3881 if (!activate)
3882 InterlockedCompareExchange(&device->device_state, D3D9_DEVICE_STATE_LOST, D3D9_DEVICE_STATE_OK);
3883 else if (device->d3d_parent->extended)
3884 InterlockedCompareExchange(&device->device_state, D3D9_DEVICE_STATE_OK, D3D9_DEVICE_STATE_LOST);
3885 else
3886 InterlockedCompareExchange(&device->device_state, D3D9_DEVICE_STATE_NOT_RESET, D3D9_DEVICE_STATE_LOST);
3889 static HRESULT CDECL device_parent_surface_created(struct wined3d_device_parent *device_parent,
3890 struct wined3d_texture *wined3d_texture, unsigned int sub_resource_idx,
3891 void **parent, const struct wined3d_parent_ops **parent_ops)
3893 struct d3d9_surface *d3d_surface;
3895 TRACE("device_parent %p, wined3d_texture %p, sub_resource_idx %u, parent %p, parent_ops %p.\n",
3896 device_parent, wined3d_texture, sub_resource_idx, parent, parent_ops);
3898 if (!(d3d_surface = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*d3d_surface))))
3899 return E_OUTOFMEMORY;
3901 surface_init(d3d_surface, wined3d_texture, sub_resource_idx, parent_ops);
3902 *parent = d3d_surface;
3903 TRACE("Created surface %p.\n", d3d_surface);
3905 return D3D_OK;
3908 static HRESULT CDECL device_parent_volume_created(struct wined3d_device_parent *device_parent,
3909 struct wined3d_texture *wined3d_texture, unsigned int sub_resource_idx,
3910 void **parent, const struct wined3d_parent_ops **parent_ops)
3912 struct d3d9_volume *d3d_volume;
3914 TRACE("device_parent %p, texture %p, sub_resource_idx %u, parent %p, parent_ops %p.\n",
3915 device_parent, wined3d_texture, sub_resource_idx, parent, parent_ops);
3917 if (!(d3d_volume = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*d3d_volume))))
3918 return E_OUTOFMEMORY;
3920 volume_init(d3d_volume, wined3d_texture, sub_resource_idx, parent_ops);
3921 *parent = d3d_volume;
3922 TRACE("Created volume %p.\n", d3d_volume);
3924 return D3D_OK;
3927 static HRESULT CDECL device_parent_create_swapchain_texture(struct wined3d_device_parent *device_parent,
3928 void *container_parent, const struct wined3d_resource_desc *desc, DWORD texture_flags,
3929 struct wined3d_texture **texture)
3931 struct d3d9_device *device = device_from_device_parent(device_parent);
3932 struct d3d9_surface *d3d_surface;
3933 HRESULT hr;
3935 TRACE("device_parent %p, container_parent %p, desc %p, texture flags %#x, texture %p.\n",
3936 device_parent, container_parent, desc, texture_flags, texture);
3938 if (container_parent == device_parent)
3939 container_parent = &device->IDirect3DDevice9Ex_iface;
3941 if (is_gdi_compat_wined3dformat(desc->format))
3942 texture_flags |= WINED3D_TEXTURE_CREATE_GET_DC;
3944 if (FAILED(hr = wined3d_texture_create(device->wined3d_device, desc, 1, 1,
3945 texture_flags | WINED3D_TEXTURE_CREATE_MAPPABLE, NULL, container_parent,
3946 &d3d9_null_wined3d_parent_ops, texture)))
3948 WARN("Failed to create texture, hr %#x.\n", hr);
3949 return hr;
3952 d3d_surface = wined3d_texture_get_sub_resource_parent(*texture, 0);
3953 d3d_surface->parent_device = &device->IDirect3DDevice9Ex_iface;
3955 return hr;
3958 static HRESULT CDECL device_parent_create_swapchain(struct wined3d_device_parent *device_parent,
3959 struct wined3d_swapchain_desc *desc, struct wined3d_swapchain **swapchain)
3961 struct d3d9_device *device = device_from_device_parent(device_parent);
3962 struct d3d9_swapchain *d3d_swapchain;
3963 HRESULT hr;
3965 TRACE("device_parent %p, desc %p, swapchain %p\n", device_parent, desc, swapchain);
3967 hr = d3d9_swapchain_create(device, desc, &d3d_swapchain);
3968 if (FAILED(hr))
3970 WARN("Failed to create swapchain, hr %#x.\n", hr);
3971 *swapchain = NULL;
3972 return hr;
3975 *swapchain = d3d_swapchain->wined3d_swapchain;
3976 wined3d_swapchain_incref(*swapchain);
3977 IDirect3DSwapChain9Ex_Release(&d3d_swapchain->IDirect3DSwapChain9Ex_iface);
3979 return hr;
3982 static const struct wined3d_device_parent_ops d3d9_wined3d_device_parent_ops =
3984 device_parent_wined3d_device_created,
3985 device_parent_mode_changed,
3986 device_parent_activate,
3987 device_parent_surface_created,
3988 device_parent_volume_created,
3989 device_parent_create_swapchain_texture,
3990 device_parent_create_swapchain,
3993 static void setup_fpu(void)
3995 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
3996 WORD cw;
3997 __asm__ volatile ("fnstcw %0" : "=m" (cw));
3998 cw = (cw & ~0xf3f) | 0x3f;
3999 __asm__ volatile ("fldcw %0" : : "m" (cw));
4000 #elif defined(__i386__) && defined(_MSC_VER)
4001 WORD cw;
4002 __asm fnstcw cw;
4003 cw = (cw & ~0xf3f) | 0x3f;
4004 __asm fldcw cw;
4005 #else
4006 FIXME("FPU setup not implemented for this platform.\n");
4007 #endif
4010 HRESULT device_init(struct d3d9_device *device, struct d3d9 *parent, struct wined3d *wined3d,
4011 UINT adapter, D3DDEVTYPE device_type, HWND focus_window, DWORD flags,
4012 D3DPRESENT_PARAMETERS *parameters, D3DDISPLAYMODEEX *mode)
4014 struct wined3d_swapchain_desc *swapchain_desc;
4015 UINT i, count = 1;
4016 HRESULT hr;
4018 if (mode)
4019 FIXME("Ignoring display mode.\n");
4021 device->IDirect3DDevice9Ex_iface.lpVtbl = &d3d9_device_vtbl;
4022 device->device_parent.ops = &d3d9_wined3d_device_parent_ops;
4023 device->refcount = 1;
4025 if (!(flags & D3DCREATE_FPU_PRESERVE)) setup_fpu();
4027 wined3d_mutex_lock();
4028 hr = wined3d_device_create(wined3d, adapter, device_type, focus_window, flags, 4,
4029 &device->device_parent, &device->wined3d_device);
4030 if (FAILED(hr))
4032 WARN("Failed to create wined3d device, hr %#x.\n", hr);
4033 wined3d_mutex_unlock();
4034 return hr;
4037 if (flags & D3DCREATE_ADAPTERGROUP_DEVICE)
4039 WINED3DCAPS caps;
4041 wined3d_get_device_caps(wined3d, adapter, device_type, &caps);
4042 count = caps.NumberOfAdaptersInGroup;
4045 if (flags & D3DCREATE_MULTITHREADED)
4046 wined3d_device_set_multithreaded(device->wined3d_device);
4048 if (!parameters->Windowed)
4050 if (!focus_window)
4051 focus_window = parameters->hDeviceWindow;
4052 if (FAILED(hr = wined3d_device_acquire_focus_window(device->wined3d_device, focus_window)))
4054 ERR("Failed to acquire focus window, hr %#x.\n", hr);
4055 wined3d_device_decref(device->wined3d_device);
4056 wined3d_mutex_unlock();
4057 return hr;
4060 for (i = 0; i < count; ++i)
4062 HWND device_window = parameters[i].hDeviceWindow;
4064 if (!device_window) device_window = focus_window;
4065 wined3d_device_setup_fullscreen_window(device->wined3d_device, device_window,
4066 parameters[i].BackBufferWidth,
4067 parameters[i].BackBufferHeight);
4071 swapchain_desc = HeapAlloc(GetProcessHeap(), 0, sizeof(*swapchain_desc) * count);
4072 if (!swapchain_desc)
4074 ERR("Failed to allocate wined3d parameters.\n");
4075 wined3d_device_release_focus_window(device->wined3d_device);
4076 wined3d_device_decref(device->wined3d_device);
4077 wined3d_mutex_unlock();
4078 return E_OUTOFMEMORY;
4081 for (i = 0; i < count; ++i)
4083 if (!wined3d_swapchain_desc_from_present_parameters(&swapchain_desc[i], &parameters[i],
4084 parent->extended))
4086 wined3d_device_release_focus_window(device->wined3d_device);
4087 wined3d_device_decref(device->wined3d_device);
4088 HeapFree(GetProcessHeap(), 0, swapchain_desc);
4089 wined3d_mutex_unlock();
4090 return D3DERR_INVALIDCALL;
4094 hr = wined3d_device_init_3d(device->wined3d_device, swapchain_desc);
4095 if (FAILED(hr))
4097 WARN("Failed to initialize 3D, hr %#x.\n", hr);
4098 wined3d_device_release_focus_window(device->wined3d_device);
4099 HeapFree(GetProcessHeap(), 0, swapchain_desc);
4100 wined3d_device_decref(device->wined3d_device);
4101 wined3d_mutex_unlock();
4102 return hr;
4105 if (FAILED(hr = d3d9_device_get_swapchains(device)))
4107 wined3d_device_uninit_3d(device->wined3d_device);
4108 wined3d_device_release_focus_window(device->wined3d_device);
4109 wined3d_device_decref(device->wined3d_device);
4110 wined3d_mutex_unlock();
4111 return E_OUTOFMEMORY;
4114 for (i = 0; i < count; ++i)
4116 present_parameters_from_wined3d_swapchain_desc(&parameters[i], &swapchain_desc[i]);
4119 wined3d_mutex_unlock();
4121 HeapFree(GetProcessHeap(), 0, swapchain_desc);
4123 /* Initialize the converted declaration array. This creates a valid pointer
4124 * and when adding decls HeapReAlloc() can be used without further checking. */
4125 device->fvf_decls = HeapAlloc(GetProcessHeap(), 0, 0);
4126 if (!device->fvf_decls)
4128 ERR("Failed to allocate FVF vertex declaration map memory.\n");
4129 wined3d_mutex_lock();
4130 HeapFree(GetProcessHeap(), 0, device->implicit_swapchains);
4131 wined3d_device_uninit_3d(device->wined3d_device);
4132 wined3d_device_release_focus_window(device->wined3d_device);
4133 wined3d_device_decref(device->wined3d_device);
4134 wined3d_mutex_unlock();
4135 return E_OUTOFMEMORY;
4138 IDirect3D9Ex_AddRef(&parent->IDirect3D9Ex_iface);
4139 device->d3d_parent = parent;
4141 return D3D_OK;