[AMSTREAM] We don't need to define WIDL_C_INLINE_WRAPPERS here anymore.
[reactos.git] / dll / directx / wine / d3d9 / device.c
1 /*
2 * IDirect3DDevice9 implementation
3 *
4 * Copyright 2002-2005 Jason Edmeades
5 * Copyright 2002-2005 Raphael Junqueira
6 * Copyright 2005 Oliver Stieber
7 *
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.
12 *
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.
17 *
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
21 */
22
23 #include "d3d9_private.h"
24
25 static void STDMETHODCALLTYPE d3d9_null_wined3d_object_destroyed(void *parent) {}
26
27 const struct wined3d_parent_ops d3d9_null_wined3d_parent_ops =
28 {
29 d3d9_null_wined3d_object_destroyed,
30 };
31
32 D3DFORMAT d3dformat_from_wined3dformat(enum wined3d_format_id format)
33 {
34 BYTE *c = (BYTE *)&format;
35
36 /* Don't translate FOURCC formats */
37 if (isprint(c[0]) && isprint(c[1]) && isprint(c[2]) && isprint(c[3])) return format;
38
39 switch(format)
40 {
41 case WINED3DFMT_UNKNOWN: return D3DFMT_UNKNOWN;
42 case WINED3DFMT_B8G8R8_UNORM: return D3DFMT_R8G8B8;
43 case WINED3DFMT_B8G8R8A8_UNORM: return D3DFMT_A8R8G8B8;
44 case WINED3DFMT_B8G8R8X8_UNORM: return D3DFMT_X8R8G8B8;
45 case WINED3DFMT_B5G6R5_UNORM: return D3DFMT_R5G6B5;
46 case WINED3DFMT_B5G5R5X1_UNORM: return D3DFMT_X1R5G5B5;
47 case WINED3DFMT_B5G5R5A1_UNORM: return D3DFMT_A1R5G5B5;
48 case WINED3DFMT_B4G4R4A4_UNORM: return D3DFMT_A4R4G4B4;
49 case WINED3DFMT_B2G3R3_UNORM: return D3DFMT_R3G3B2;
50 case WINED3DFMT_A8_UNORM: return D3DFMT_A8;
51 case WINED3DFMT_B2G3R3A8_UNORM: return D3DFMT_A8R3G3B2;
52 case WINED3DFMT_B4G4R4X4_UNORM: return D3DFMT_X4R4G4B4;
53 case WINED3DFMT_R10G10B10A2_UNORM: return D3DFMT_A2B10G10R10;
54 case WINED3DFMT_R8G8B8A8_UNORM: return D3DFMT_A8B8G8R8;
55 case WINED3DFMT_R8G8B8X8_UNORM: return D3DFMT_X8B8G8R8;
56 case WINED3DFMT_R16G16_UNORM: return D3DFMT_G16R16;
57 case WINED3DFMT_B10G10R10A2_UNORM: return D3DFMT_A2R10G10B10;
58 case WINED3DFMT_R16G16B16A16_UNORM: return D3DFMT_A16B16G16R16;
59 case WINED3DFMT_P8_UINT_A8_UNORM: return D3DFMT_A8P8;
60 case WINED3DFMT_P8_UINT: return D3DFMT_P8;
61 case WINED3DFMT_L8_UNORM: return D3DFMT_L8;
62 case WINED3DFMT_L8A8_UNORM: return D3DFMT_A8L8;
63 case WINED3DFMT_L4A4_UNORM: return D3DFMT_A4L4;
64 case WINED3DFMT_R8G8_SNORM: return D3DFMT_V8U8;
65 case WINED3DFMT_R5G5_SNORM_L6_UNORM: return D3DFMT_L6V5U5;
66 case WINED3DFMT_R8G8_SNORM_L8X8_UNORM: return D3DFMT_X8L8V8U8;
67 case WINED3DFMT_R8G8B8A8_SNORM: return D3DFMT_Q8W8V8U8;
68 case WINED3DFMT_R16G16_SNORM: return D3DFMT_V16U16;
69 case WINED3DFMT_R10G10B10_SNORM_A2_UNORM: return D3DFMT_A2W10V10U10;
70 case WINED3DFMT_D16_LOCKABLE: return D3DFMT_D16_LOCKABLE;
71 case WINED3DFMT_D32_UNORM: return D3DFMT_D32;
72 case WINED3DFMT_S1_UINT_D15_UNORM: return D3DFMT_D15S1;
73 case WINED3DFMT_D24_UNORM_S8_UINT: return D3DFMT_D24S8;
74 case WINED3DFMT_X8D24_UNORM: return D3DFMT_D24X8;
75 case WINED3DFMT_S4X4_UINT_D24_UNORM: return D3DFMT_D24X4S4;
76 case WINED3DFMT_D16_UNORM: return D3DFMT_D16;
77 case WINED3DFMT_L16_UNORM: return D3DFMT_L16;
78 case WINED3DFMT_D32_FLOAT: return D3DFMT_D32F_LOCKABLE;
79 case WINED3DFMT_S8_UINT_D24_FLOAT: return D3DFMT_D24FS8;
80 case WINED3DFMT_R16_UINT: return D3DFMT_INDEX16;
81 case WINED3DFMT_R32_UINT: return D3DFMT_INDEX32;
82 case WINED3DFMT_R16G16B16A16_SNORM: return D3DFMT_Q16W16V16U16;
83 case WINED3DFMT_R16_FLOAT: return D3DFMT_R16F;
84 case WINED3DFMT_R16G16_FLOAT: return D3DFMT_G16R16F;
85 case WINED3DFMT_R16G16B16A16_FLOAT: return D3DFMT_A16B16G16R16F;
86 case WINED3DFMT_R32_FLOAT: return D3DFMT_R32F;
87 case WINED3DFMT_R32G32_FLOAT: return D3DFMT_G32R32F;
88 case WINED3DFMT_R32G32B32A32_FLOAT: return D3DFMT_A32B32G32R32F;
89 case WINED3DFMT_R8G8_SNORM_Cx: return D3DFMT_CxV8U8;
90 default:
91 FIXME("Unhandled wined3d format %#x.\n", format);
92 return D3DFMT_UNKNOWN;
93 }
94 }
95
96 enum wined3d_format_id wined3dformat_from_d3dformat(D3DFORMAT format)
97 {
98 BYTE *c = (BYTE *)&format;
99
100 /* Don't translate FOURCC formats */
101 if (isprint(c[0]) && isprint(c[1]) && isprint(c[2]) && isprint(c[3])) return format;
102
103 switch(format)
104 {
105 case D3DFMT_UNKNOWN: return WINED3DFMT_UNKNOWN;
106 case D3DFMT_R8G8B8: return WINED3DFMT_B8G8R8_UNORM;
107 case D3DFMT_A8R8G8B8: return WINED3DFMT_B8G8R8A8_UNORM;
108 case D3DFMT_X8R8G8B8: return WINED3DFMT_B8G8R8X8_UNORM;
109 case D3DFMT_R5G6B5: return WINED3DFMT_B5G6R5_UNORM;
110 case D3DFMT_X1R5G5B5: return WINED3DFMT_B5G5R5X1_UNORM;
111 case D3DFMT_A1R5G5B5: return WINED3DFMT_B5G5R5A1_UNORM;
112 case D3DFMT_A4R4G4B4: return WINED3DFMT_B4G4R4A4_UNORM;
113 case D3DFMT_R3G3B2: return WINED3DFMT_B2G3R3_UNORM;
114 case D3DFMT_A8: return WINED3DFMT_A8_UNORM;
115 case D3DFMT_A8R3G3B2: return WINED3DFMT_B2G3R3A8_UNORM;
116 case D3DFMT_X4R4G4B4: return WINED3DFMT_B4G4R4X4_UNORM;
117 case D3DFMT_A2B10G10R10: return WINED3DFMT_R10G10B10A2_UNORM;
118 case D3DFMT_A8B8G8R8: return WINED3DFMT_R8G8B8A8_UNORM;
119 case D3DFMT_X8B8G8R8: return WINED3DFMT_R8G8B8X8_UNORM;
120 case D3DFMT_G16R16: return WINED3DFMT_R16G16_UNORM;
121 case D3DFMT_A2R10G10B10: return WINED3DFMT_B10G10R10A2_UNORM;
122 case D3DFMT_A16B16G16R16: return WINED3DFMT_R16G16B16A16_UNORM;
123 case D3DFMT_A8P8: return WINED3DFMT_P8_UINT_A8_UNORM;
124 case D3DFMT_P8: return WINED3DFMT_P8_UINT;
125 case D3DFMT_L8: return WINED3DFMT_L8_UNORM;
126 case D3DFMT_A8L8: return WINED3DFMT_L8A8_UNORM;
127 case D3DFMT_A4L4: return WINED3DFMT_L4A4_UNORM;
128 case D3DFMT_V8U8: return WINED3DFMT_R8G8_SNORM;
129 case D3DFMT_L6V5U5: return WINED3DFMT_R5G5_SNORM_L6_UNORM;
130 case D3DFMT_X8L8V8U8: return WINED3DFMT_R8G8_SNORM_L8X8_UNORM;
131 case D3DFMT_Q8W8V8U8: return WINED3DFMT_R8G8B8A8_SNORM;
132 case D3DFMT_V16U16: return WINED3DFMT_R16G16_SNORM;
133 case D3DFMT_A2W10V10U10: return WINED3DFMT_R10G10B10_SNORM_A2_UNORM;
134 case D3DFMT_D16_LOCKABLE: return WINED3DFMT_D16_LOCKABLE;
135 case D3DFMT_D32: return WINED3DFMT_D32_UNORM;
136 case D3DFMT_D15S1: return WINED3DFMT_S1_UINT_D15_UNORM;
137 case D3DFMT_D24S8: return WINED3DFMT_D24_UNORM_S8_UINT;
138 case D3DFMT_D24X8: return WINED3DFMT_X8D24_UNORM;
139 case D3DFMT_D24X4S4: return WINED3DFMT_S4X4_UINT_D24_UNORM;
140 case D3DFMT_D16: return WINED3DFMT_D16_UNORM;
141 case D3DFMT_L16: return WINED3DFMT_L16_UNORM;
142 case D3DFMT_D32F_LOCKABLE: return WINED3DFMT_D32_FLOAT;
143 case D3DFMT_D24FS8: return WINED3DFMT_S8_UINT_D24_FLOAT;
144 case D3DFMT_INDEX16: return WINED3DFMT_R16_UINT;
145 case D3DFMT_INDEX32: return WINED3DFMT_R32_UINT;
146 case D3DFMT_Q16W16V16U16: return WINED3DFMT_R16G16B16A16_SNORM;
147 case D3DFMT_R16F: return WINED3DFMT_R16_FLOAT;
148 case D3DFMT_G16R16F: return WINED3DFMT_R16G16_FLOAT;
149 case D3DFMT_A16B16G16R16F: return WINED3DFMT_R16G16B16A16_FLOAT;
150 case D3DFMT_R32F: return WINED3DFMT_R32_FLOAT;
151 case D3DFMT_G32R32F: return WINED3DFMT_R32G32_FLOAT;
152 case D3DFMT_A32B32G32R32F: return WINED3DFMT_R32G32B32A32_FLOAT;
153 case D3DFMT_CxV8U8: return WINED3DFMT_R8G8_SNORM_Cx;
154 default:
155 FIXME("Unhandled D3DFORMAT %#x.\n", format);
156 return WINED3DFMT_UNKNOWN;
157 }
158 }
159
160 static UINT vertex_count_from_primitive_count(D3DPRIMITIVETYPE primitive_type, UINT primitive_count)
161 {
162 switch (primitive_type)
163 {
164 case D3DPT_POINTLIST:
165 return primitive_count;
166
167 case D3DPT_LINELIST:
168 return primitive_count * 2;
169
170 case D3DPT_LINESTRIP:
171 return primitive_count + 1;
172
173 case D3DPT_TRIANGLELIST:
174 return primitive_count * 3;
175
176 case D3DPT_TRIANGLESTRIP:
177 case D3DPT_TRIANGLEFAN:
178 return primitive_count + 2;
179
180 default:
181 FIXME("Unhandled primitive type %#x.\n", primitive_type);
182 return 0;
183 }
184 }
185
186 void present_parameters_from_wined3d_swapchain_desc(D3DPRESENT_PARAMETERS *present_parameters,
187 const struct wined3d_swapchain_desc *swapchain_desc)
188 {
189 present_parameters->BackBufferWidth = swapchain_desc->backbuffer_width;
190 present_parameters->BackBufferHeight = swapchain_desc->backbuffer_height;
191 present_parameters->BackBufferFormat = d3dformat_from_wined3dformat(swapchain_desc->backbuffer_format);
192 present_parameters->BackBufferCount = swapchain_desc->backbuffer_count;
193 present_parameters->MultiSampleType = swapchain_desc->multisample_type;
194 present_parameters->MultiSampleQuality = swapchain_desc->multisample_quality;
195 present_parameters->SwapEffect = swapchain_desc->swap_effect;
196 present_parameters->hDeviceWindow = swapchain_desc->device_window;
197 present_parameters->Windowed = swapchain_desc->windowed;
198 present_parameters->EnableAutoDepthStencil = swapchain_desc->enable_auto_depth_stencil;
199 present_parameters->AutoDepthStencilFormat
200 = d3dformat_from_wined3dformat(swapchain_desc->auto_depth_stencil_format);
201 present_parameters->Flags = swapchain_desc->flags & D3DPRESENTFLAGS_MASK;
202 present_parameters->FullScreen_RefreshRateInHz = swapchain_desc->refresh_rate;
203 present_parameters->PresentationInterval = swapchain_desc->swap_interval;
204 }
205
206 static BOOL wined3d_swapchain_desc_from_present_parameters(struct wined3d_swapchain_desc *swapchain_desc,
207 const D3DPRESENT_PARAMETERS *present_parameters, BOOL extended)
208 {
209 D3DSWAPEFFECT highest_swapeffect = extended ? D3DSWAPEFFECT_FLIPEX : D3DSWAPEFFECT_COPY;
210 UINT highest_bb_count = extended ? 30 : 3;
211
212 if (!present_parameters->SwapEffect || present_parameters->SwapEffect > highest_swapeffect)
213 {
214 WARN("Invalid swap effect %u passed.\n", present_parameters->SwapEffect);
215 return FALSE;
216 }
217 if (present_parameters->BackBufferCount > highest_bb_count
218 || (present_parameters->SwapEffect == D3DSWAPEFFECT_COPY
219 && present_parameters->BackBufferCount > 1))
220 {
221 WARN("Invalid backbuffer count %u.\n", present_parameters->BackBufferCount);
222 return FALSE;
223 }
224
225 swapchain_desc->backbuffer_width = present_parameters->BackBufferWidth;
226 swapchain_desc->backbuffer_height = present_parameters->BackBufferHeight;
227 swapchain_desc->backbuffer_format = wined3dformat_from_d3dformat(present_parameters->BackBufferFormat);
228 swapchain_desc->backbuffer_count = max(1, present_parameters->BackBufferCount);
229 swapchain_desc->multisample_type = present_parameters->MultiSampleType;
230 swapchain_desc->multisample_quality = present_parameters->MultiSampleQuality;
231 swapchain_desc->swap_effect = present_parameters->SwapEffect;
232 swapchain_desc->device_window = present_parameters->hDeviceWindow;
233 swapchain_desc->windowed = present_parameters->Windowed;
234 swapchain_desc->enable_auto_depth_stencil = present_parameters->EnableAutoDepthStencil;
235 swapchain_desc->auto_depth_stencil_format
236 = wined3dformat_from_d3dformat(present_parameters->AutoDepthStencilFormat);
237 swapchain_desc->flags
238 = (present_parameters->Flags & D3DPRESENTFLAGS_MASK) | WINED3D_SWAPCHAIN_ALLOW_MODE_SWITCH;
239 if (is_gdi_compat_wined3dformat(swapchain_desc->backbuffer_format))
240 swapchain_desc->flags |= WINED3D_SWAPCHAIN_GDI_COMPATIBLE;
241 swapchain_desc->refresh_rate = present_parameters->FullScreen_RefreshRateInHz;
242 swapchain_desc->swap_interval = present_parameters->PresentationInterval;
243 swapchain_desc->auto_restore_display_mode = TRUE;
244
245 if (present_parameters->Flags & ~D3DPRESENTFLAGS_MASK)
246 FIXME("Unhandled flags %#x.\n", present_parameters->Flags & ~D3DPRESENTFLAGS_MASK);
247
248 return TRUE;
249 }
250
251 void d3dcaps_from_wined3dcaps(D3DCAPS9 *caps, const WINED3DCAPS *wined3d_caps)
252 {
253 static const DWORD ps_minor_version[] = {0, 4, 0, 0};
254 static const DWORD vs_minor_version[] = {0, 1, 0, 0};
255 static const DWORD texture_filter_caps =
256 D3DPTFILTERCAPS_MINFPOINT | D3DPTFILTERCAPS_MINFLINEAR | D3DPTFILTERCAPS_MINFANISOTROPIC |
257 D3DPTFILTERCAPS_MINFPYRAMIDALQUAD | D3DPTFILTERCAPS_MINFGAUSSIANQUAD|
258 D3DPTFILTERCAPS_MIPFPOINT | D3DPTFILTERCAPS_MIPFLINEAR | D3DPTFILTERCAPS_MAGFPOINT |
259 D3DPTFILTERCAPS_MAGFLINEAR |D3DPTFILTERCAPS_MAGFANISOTROPIC|D3DPTFILTERCAPS_MAGFPYRAMIDALQUAD|
260 D3DPTFILTERCAPS_MAGFGAUSSIANQUAD;
261
262 caps->DeviceType = (D3DDEVTYPE)wined3d_caps->DeviceType;
263 caps->AdapterOrdinal = wined3d_caps->AdapterOrdinal;
264 caps->Caps = wined3d_caps->Caps;
265 caps->Caps2 = wined3d_caps->Caps2;
266 caps->Caps3 = wined3d_caps->Caps3;
267 caps->PresentationIntervals = wined3d_caps->PresentationIntervals;
268 caps->CursorCaps = wined3d_caps->CursorCaps;
269 caps->DevCaps = wined3d_caps->DevCaps;
270 caps->PrimitiveMiscCaps = wined3d_caps->PrimitiveMiscCaps;
271 caps->RasterCaps = wined3d_caps->RasterCaps;
272 caps->ZCmpCaps = wined3d_caps->ZCmpCaps;
273 caps->SrcBlendCaps = wined3d_caps->SrcBlendCaps;
274 caps->DestBlendCaps = wined3d_caps->DestBlendCaps;
275 caps->AlphaCmpCaps = wined3d_caps->AlphaCmpCaps;
276 caps->ShadeCaps = wined3d_caps->ShadeCaps;
277 caps->TextureCaps = wined3d_caps->TextureCaps;
278 caps->TextureFilterCaps = wined3d_caps->TextureFilterCaps;
279 caps->CubeTextureFilterCaps = wined3d_caps->CubeTextureFilterCaps;
280 caps->VolumeTextureFilterCaps = wined3d_caps->VolumeTextureFilterCaps;
281 caps->TextureAddressCaps = wined3d_caps->TextureAddressCaps;
282 caps->VolumeTextureAddressCaps = wined3d_caps->VolumeTextureAddressCaps;
283 caps->LineCaps = wined3d_caps->LineCaps;
284 caps->MaxTextureWidth = wined3d_caps->MaxTextureWidth;
285 caps->MaxTextureHeight = wined3d_caps->MaxTextureHeight;
286 caps->MaxVolumeExtent = wined3d_caps->MaxVolumeExtent;
287 caps->MaxTextureRepeat = wined3d_caps->MaxTextureRepeat;
288 caps->MaxTextureAspectRatio = wined3d_caps->MaxTextureAspectRatio;
289 caps->MaxAnisotropy = wined3d_caps->MaxAnisotropy;
290 caps->MaxVertexW = wined3d_caps->MaxVertexW;
291 caps->GuardBandLeft = wined3d_caps->GuardBandLeft;
292 caps->GuardBandTop = wined3d_caps->GuardBandTop;
293 caps->GuardBandRight = wined3d_caps->GuardBandRight;
294 caps->GuardBandBottom = wined3d_caps->GuardBandBottom;
295 caps->ExtentsAdjust = wined3d_caps->ExtentsAdjust;
296 caps->StencilCaps = wined3d_caps->StencilCaps;
297 caps->FVFCaps = wined3d_caps->FVFCaps;
298 caps->TextureOpCaps = wined3d_caps->TextureOpCaps;
299 caps->MaxTextureBlendStages = wined3d_caps->MaxTextureBlendStages;
300 caps->MaxSimultaneousTextures = wined3d_caps->MaxSimultaneousTextures;
301 caps->VertexProcessingCaps = wined3d_caps->VertexProcessingCaps;
302 caps->MaxActiveLights = wined3d_caps->MaxActiveLights;
303 caps->MaxUserClipPlanes = wined3d_caps->MaxUserClipPlanes;
304 caps->MaxVertexBlendMatrices = wined3d_caps->MaxVertexBlendMatrices;
305 caps->MaxVertexBlendMatrixIndex = wined3d_caps->MaxVertexBlendMatrixIndex;
306 caps->MaxPointSize = wined3d_caps->MaxPointSize;
307 caps->MaxPrimitiveCount = wined3d_caps->MaxPrimitiveCount;
308 caps->MaxVertexIndex = wined3d_caps->MaxVertexIndex;
309 caps->MaxStreams = wined3d_caps->MaxStreams;
310 caps->MaxStreamStride = wined3d_caps->MaxStreamStride;
311 caps->VertexShaderVersion = wined3d_caps->VertexShaderVersion;
312 caps->MaxVertexShaderConst = wined3d_caps->MaxVertexShaderConst;
313 caps->PixelShaderVersion = wined3d_caps->PixelShaderVersion;
314 caps->PixelShader1xMaxValue = wined3d_caps->PixelShader1xMaxValue;
315 caps->DevCaps2 = wined3d_caps->DevCaps2;
316 caps->MaxNpatchTessellationLevel = wined3d_caps->MaxNpatchTessellationLevel;
317 caps->MasterAdapterOrdinal = wined3d_caps->MasterAdapterOrdinal;
318 caps->AdapterOrdinalInGroup = wined3d_caps->AdapterOrdinalInGroup;
319 caps->NumberOfAdaptersInGroup = wined3d_caps->NumberOfAdaptersInGroup;
320 caps->DeclTypes = wined3d_caps->DeclTypes;
321 caps->NumSimultaneousRTs = wined3d_caps->NumSimultaneousRTs;
322 caps->StretchRectFilterCaps = wined3d_caps->StretchRectFilterCaps;
323 caps->VS20Caps.Caps = wined3d_caps->VS20Caps.caps;
324 caps->VS20Caps.DynamicFlowControlDepth = wined3d_caps->VS20Caps.dynamic_flow_control_depth;
325 caps->VS20Caps.NumTemps = wined3d_caps->VS20Caps.temp_count;
326 caps->VS20Caps.StaticFlowControlDepth = wined3d_caps->VS20Caps.static_flow_control_depth;
327 caps->PS20Caps.Caps = wined3d_caps->PS20Caps.caps;
328 caps->PS20Caps.DynamicFlowControlDepth = wined3d_caps->PS20Caps.dynamic_flow_control_depth;
329 caps->PS20Caps.NumTemps = wined3d_caps->PS20Caps.temp_count;
330 caps->PS20Caps.StaticFlowControlDepth = wined3d_caps->PS20Caps.static_flow_control_depth;
331 caps->PS20Caps.NumInstructionSlots = wined3d_caps->PS20Caps.instruction_slot_count;
332 caps->VertexTextureFilterCaps = wined3d_caps->VertexTextureFilterCaps;
333 caps->MaxVShaderInstructionsExecuted = wined3d_caps->MaxVShaderInstructionsExecuted;
334 caps->MaxPShaderInstructionsExecuted = wined3d_caps->MaxPShaderInstructionsExecuted;
335 caps->MaxVertexShader30InstructionSlots = wined3d_caps->MaxVertexShader30InstructionSlots;
336 caps->MaxPixelShader30InstructionSlots = wined3d_caps->MaxPixelShader30InstructionSlots;
337
338 /* Some functionality is implemented in d3d9.dll, not wined3d.dll. Add the needed caps. */
339 caps->DevCaps2 |= D3DDEVCAPS2_CAN_STRETCHRECT_FROM_TEXTURES;
340
341 /* Filter wined3d caps. */
342 caps->TextureFilterCaps &= texture_filter_caps;
343 caps->CubeTextureFilterCaps &= texture_filter_caps;
344 caps->VolumeTextureFilterCaps &= texture_filter_caps;
345
346 caps->DevCaps &=
347 D3DDEVCAPS_EXECUTESYSTEMMEMORY | D3DDEVCAPS_EXECUTEVIDEOMEMORY | D3DDEVCAPS_TLVERTEXSYSTEMMEMORY |
348 D3DDEVCAPS_TLVERTEXVIDEOMEMORY | D3DDEVCAPS_TEXTURESYSTEMMEMORY| D3DDEVCAPS_TEXTUREVIDEOMEMORY |
349 D3DDEVCAPS_DRAWPRIMTLVERTEX | D3DDEVCAPS_CANRENDERAFTERFLIP | D3DDEVCAPS_TEXTURENONLOCALVIDMEM|
350 D3DDEVCAPS_DRAWPRIMITIVES2 | D3DDEVCAPS_SEPARATETEXTUREMEMORIES |
351 D3DDEVCAPS_DRAWPRIMITIVES2EX | D3DDEVCAPS_HWTRANSFORMANDLIGHT| D3DDEVCAPS_CANBLTSYSTONONLOCAL |
352 D3DDEVCAPS_HWRASTERIZATION | D3DDEVCAPS_PUREDEVICE | D3DDEVCAPS_QUINTICRTPATCHES |
353 D3DDEVCAPS_RTPATCHES | D3DDEVCAPS_RTPATCHHANDLEZERO | D3DDEVCAPS_NPATCHES;
354
355 caps->ShadeCaps &=
356 D3DPSHADECAPS_COLORGOURAUDRGB | D3DPSHADECAPS_SPECULARGOURAUDRGB |
357 D3DPSHADECAPS_ALPHAGOURAUDBLEND | D3DPSHADECAPS_FOGGOURAUD;
358
359 caps->RasterCaps &=
360 D3DPRASTERCAPS_DITHER | D3DPRASTERCAPS_ZTEST | D3DPRASTERCAPS_FOGVERTEX |
361 D3DPRASTERCAPS_FOGTABLE | D3DPRASTERCAPS_MIPMAPLODBIAS | D3DPRASTERCAPS_ZBUFFERLESSHSR |
362 D3DPRASTERCAPS_FOGRANGE | D3DPRASTERCAPS_ANISOTROPY | D3DPRASTERCAPS_WBUFFER |
363 D3DPRASTERCAPS_WFOG | D3DPRASTERCAPS_ZFOG | D3DPRASTERCAPS_COLORPERSPECTIVE |
364 D3DPRASTERCAPS_SCISSORTEST | D3DPRASTERCAPS_SLOPESCALEDEPTHBIAS |
365 D3DPRASTERCAPS_DEPTHBIAS | D3DPRASTERCAPS_MULTISAMPLE_TOGGLE;
366
367 caps->DevCaps2 &=
368 D3DDEVCAPS2_STREAMOFFSET | D3DDEVCAPS2_DMAPNPATCH | D3DDEVCAPS2_ADAPTIVETESSRTPATCH |
369 D3DDEVCAPS2_ADAPTIVETESSNPATCH | D3DDEVCAPS2_CAN_STRETCHRECT_FROM_TEXTURES |
370 D3DDEVCAPS2_PRESAMPLEDDMAPNPATCH| D3DDEVCAPS2_VERTEXELEMENTSCANSHARESTREAMOFFSET;
371
372 caps->Caps2 &=
373 D3DCAPS2_FULLSCREENGAMMA | D3DCAPS2_CANCALIBRATEGAMMA | D3DCAPS2_RESERVED |
374 D3DCAPS2_CANMANAGERESOURCE | D3DCAPS2_DYNAMICTEXTURES | D3DCAPS2_CANAUTOGENMIPMAP;
375
376 caps->VertexProcessingCaps &=
377 D3DVTXPCAPS_TEXGEN | D3DVTXPCAPS_MATERIALSOURCE7 | D3DVTXPCAPS_DIRECTIONALLIGHTS |
378 D3DVTXPCAPS_POSITIONALLIGHTS | D3DVTXPCAPS_LOCALVIEWER | D3DVTXPCAPS_TWEENING |
379 D3DVTXPCAPS_TEXGEN_SPHEREMAP | D3DVTXPCAPS_NO_TEXGEN_NONLOCALVIEWER;
380
381 caps->TextureCaps &=
382 D3DPTEXTURECAPS_PERSPECTIVE | D3DPTEXTURECAPS_POW2 | D3DPTEXTURECAPS_ALPHA |
383 D3DPTEXTURECAPS_SQUAREONLY | D3DPTEXTURECAPS_TEXREPEATNOTSCALEDBYSIZE |
384 D3DPTEXTURECAPS_ALPHAPALETTE | D3DPTEXTURECAPS_NONPOW2CONDITIONAL |
385 D3DPTEXTURECAPS_PROJECTED | D3DPTEXTURECAPS_CUBEMAP | D3DPTEXTURECAPS_VOLUMEMAP |
386 D3DPTEXTURECAPS_MIPMAP | D3DPTEXTURECAPS_MIPVOLUMEMAP | D3DPTEXTURECAPS_MIPCUBEMAP |
387 D3DPTEXTURECAPS_CUBEMAP_POW2 | D3DPTEXTURECAPS_VOLUMEMAP_POW2| D3DPTEXTURECAPS_NOPROJECTEDBUMPENV;
388
389 caps->MaxVertexShaderConst = min(D3D9_MAX_VERTEX_SHADER_CONSTANTF, caps->MaxVertexShaderConst);
390 caps->NumSimultaneousRTs = min(D3D9_MAX_SIMULTANEOUS_RENDERTARGETS, caps->NumSimultaneousRTs);
391
392 if (caps->PixelShaderVersion > 3)
393 {
394 caps->PixelShaderVersion = D3DPS_VERSION(3, 0);
395 }
396 else
397 {
398 DWORD major = caps->PixelShaderVersion;
399 caps->PixelShaderVersion = D3DPS_VERSION(major, ps_minor_version[major]);
400 }
401
402 if (caps->VertexShaderVersion > 3)
403 {
404 caps->VertexShaderVersion = D3DVS_VERSION(3, 0);
405 }
406 else
407 {
408 DWORD major = caps->VertexShaderVersion;
409 caps->VertexShaderVersion = D3DVS_VERSION(major, vs_minor_version[major]);
410 }
411 }
412
413 static HRESULT WINAPI d3d9_device_QueryInterface(IDirect3DDevice9Ex *iface, REFIID riid, void **out)
414 {
415 TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
416
417 if (IsEqualGUID(riid, &IID_IDirect3DDevice9)
418 || IsEqualGUID(riid, &IID_IUnknown))
419 {
420 IDirect3DDevice9Ex_AddRef(iface);
421 *out = iface;
422 return S_OK;
423 }
424
425 if (IsEqualGUID(riid, &IID_IDirect3DDevice9Ex))
426 {
427 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
428
429 /* Find out if the creating d3d9 interface was created with Direct3DCreate9Ex.
430 * It doesn't matter with which function the device was created. */
431 if (!device->d3d_parent->extended)
432 {
433 WARN("IDirect3D9 instance wasn't created with CreateDirect3D9Ex, returning E_NOINTERFACE.\n");
434 *out = NULL;
435 return E_NOINTERFACE;
436 }
437
438 IDirect3DDevice9Ex_AddRef(iface);
439 *out = iface;
440 return S_OK;
441 }
442
443 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
444
445 *out = NULL;
446 return E_NOINTERFACE;
447 }
448
449 static ULONG WINAPI d3d9_device_AddRef(IDirect3DDevice9Ex *iface)
450 {
451 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
452 ULONG refcount = InterlockedIncrement(&device->refcount);
453
454 TRACE("%p increasing refcount to %u.\n", iface, refcount);
455
456 return refcount;
457 }
458
459 static ULONG WINAPI DECLSPEC_HOTPATCH d3d9_device_Release(IDirect3DDevice9Ex *iface)
460 {
461 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
462 ULONG refcount;
463
464 if (device->in_destruction)
465 return 0;
466
467 refcount = InterlockedDecrement(&device->refcount);
468
469 TRACE("%p decreasing refcount to %u.\n", iface, refcount);
470
471 if (!refcount)
472 {
473 unsigned i;
474 device->in_destruction = TRUE;
475
476 wined3d_mutex_lock();
477 for (i = 0; i < device->fvf_decl_count; ++i)
478 {
479 wined3d_vertex_declaration_decref(device->fvf_decls[i].decl);
480 }
481 HeapFree(GetProcessHeap(), 0, device->fvf_decls);
482
483 if (device->vertex_buffer)
484 wined3d_buffer_decref(device->vertex_buffer);
485 if (device->index_buffer)
486 wined3d_buffer_decref(device->index_buffer);
487
488 HeapFree(GetProcessHeap(), 0, device->implicit_swapchains);
489
490 wined3d_device_uninit_3d(device->wined3d_device);
491 wined3d_device_release_focus_window(device->wined3d_device);
492 wined3d_device_decref(device->wined3d_device);
493 wined3d_mutex_unlock();
494
495 IDirect3D9Ex_Release(&device->d3d_parent->IDirect3D9Ex_iface);
496
497 HeapFree(GetProcessHeap(), 0, device);
498 }
499
500 return refcount;
501 }
502
503 static HRESULT WINAPI d3d9_device_TestCooperativeLevel(IDirect3DDevice9Ex *iface)
504 {
505 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
506
507 TRACE("iface %p.\n", iface);
508
509 TRACE("device state: %#x.\n", device->device_state);
510
511 if (device->d3d_parent->extended)
512 return D3D_OK;
513
514 switch (device->device_state)
515 {
516 default:
517 case D3D9_DEVICE_STATE_OK:
518 return D3D_OK;
519 case D3D9_DEVICE_STATE_LOST:
520 return D3DERR_DEVICELOST;
521 case D3D9_DEVICE_STATE_NOT_RESET:
522 return D3DERR_DEVICENOTRESET;
523 }
524 }
525
526 static UINT WINAPI d3d9_device_GetAvailableTextureMem(IDirect3DDevice9Ex *iface)
527 {
528 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
529 UINT ret;
530
531 TRACE("iface %p.\n", iface);
532
533 wined3d_mutex_lock();
534 ret = wined3d_device_get_available_texture_mem(device->wined3d_device);
535 wined3d_mutex_unlock();
536
537 return ret;
538 }
539
540 static HRESULT WINAPI d3d9_device_EvictManagedResources(IDirect3DDevice9Ex *iface)
541 {
542 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
543
544 TRACE("iface %p.\n", iface);
545
546 wined3d_mutex_lock();
547 wined3d_device_evict_managed_resources(device->wined3d_device);
548 wined3d_mutex_unlock();
549
550 return D3D_OK;
551 }
552
553 static HRESULT WINAPI d3d9_device_GetDirect3D(IDirect3DDevice9Ex *iface, IDirect3D9 **d3d9)
554 {
555 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
556
557 TRACE("iface %p, d3d9 %p.\n", iface, d3d9);
558
559 if (!d3d9)
560 return D3DERR_INVALIDCALL;
561
562 return IDirect3D9Ex_QueryInterface(&device->d3d_parent->IDirect3D9Ex_iface, &IID_IDirect3D9, (void **)d3d9);
563 }
564
565 static HRESULT WINAPI d3d9_device_GetDeviceCaps(IDirect3DDevice9Ex *iface, D3DCAPS9 *caps)
566 {
567 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
568 WINED3DCAPS wined3d_caps;
569 HRESULT hr;
570
571 TRACE("iface %p, caps %p.\n", iface, caps);
572
573 if (!caps)
574 return D3DERR_INVALIDCALL;
575
576 memset(caps, 0, sizeof(*caps));
577
578 wined3d_mutex_lock();
579 hr = wined3d_device_get_device_caps(device->wined3d_device, &wined3d_caps);
580 wined3d_mutex_unlock();
581
582 d3dcaps_from_wined3dcaps(caps, &wined3d_caps);
583
584 return hr;
585 }
586
587 static HRESULT WINAPI d3d9_device_GetDisplayMode(IDirect3DDevice9Ex *iface, UINT swapchain, D3DDISPLAYMODE *mode)
588 {
589 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
590 struct wined3d_display_mode wined3d_mode;
591 HRESULT hr;
592
593 TRACE("iface %p, swapchain %u, mode %p.\n", iface, swapchain, mode);
594
595 wined3d_mutex_lock();
596 hr = wined3d_device_get_display_mode(device->wined3d_device, swapchain, &wined3d_mode, NULL);
597 wined3d_mutex_unlock();
598
599 if (SUCCEEDED(hr))
600 {
601 mode->Width = wined3d_mode.width;
602 mode->Height = wined3d_mode.height;
603 mode->RefreshRate = wined3d_mode.refresh_rate;
604 mode->Format = d3dformat_from_wined3dformat(wined3d_mode.format_id);
605 }
606
607 return hr;
608 }
609
610 static HRESULT WINAPI d3d9_device_GetCreationParameters(IDirect3DDevice9Ex *iface,
611 D3DDEVICE_CREATION_PARAMETERS *parameters)
612 {
613 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
614
615 TRACE("iface %p, parameters %p.\n", iface, parameters);
616
617 wined3d_mutex_lock();
618 wined3d_device_get_creation_parameters(device->wined3d_device,
619 (struct wined3d_device_creation_parameters *)parameters);
620 wined3d_mutex_unlock();
621
622 return D3D_OK;
623 }
624
625 static HRESULT WINAPI d3d9_device_SetCursorProperties(IDirect3DDevice9Ex *iface,
626 UINT hotspot_x, UINT hotspot_y, IDirect3DSurface9 *bitmap)
627 {
628 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
629 struct d3d9_surface *bitmap_impl = unsafe_impl_from_IDirect3DSurface9(bitmap);
630 HRESULT hr;
631
632 TRACE("iface %p, hotspot_x %u, hotspot_y %u, bitmap %p.\n",
633 iface, hotspot_x, hotspot_y, bitmap);
634
635 if (!bitmap)
636 {
637 WARN("No cursor bitmap, returning D3DERR_INVALIDCALL.\n");
638 return D3DERR_INVALIDCALL;
639 }
640
641 wined3d_mutex_lock();
642 hr = wined3d_device_set_cursor_properties(device->wined3d_device,
643 hotspot_x, hotspot_y, bitmap_impl->wined3d_texture, bitmap_impl->sub_resource_idx);
644 wined3d_mutex_unlock();
645
646 return hr;
647 }
648
649 static void WINAPI d3d9_device_SetCursorPosition(IDirect3DDevice9Ex *iface, int x, int y, DWORD flags)
650 {
651 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
652
653 TRACE("iface %p, x %u, y %u, flags %#x.\n", iface, x, y, flags);
654
655 wined3d_mutex_lock();
656 wined3d_device_set_cursor_position(device->wined3d_device, x, y, flags);
657 wined3d_mutex_unlock();
658 }
659
660 static BOOL WINAPI d3d9_device_ShowCursor(IDirect3DDevice9Ex *iface, BOOL show)
661 {
662 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
663 BOOL ret;
664
665 TRACE("iface %p, show %#x.\n", iface, show);
666
667 wined3d_mutex_lock();
668 ret = wined3d_device_show_cursor(device->wined3d_device, show);
669 wined3d_mutex_unlock();
670
671 return ret;
672 }
673
674 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_CreateAdditionalSwapChain(IDirect3DDevice9Ex *iface,
675 D3DPRESENT_PARAMETERS *present_parameters, IDirect3DSwapChain9 **swapchain)
676 {
677 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
678 struct wined3d_swapchain_desc desc;
679 struct d3d9_swapchain *object;
680 UINT i, count;
681 HRESULT hr;
682
683 TRACE("iface %p, present_parameters %p, swapchain %p.\n",
684 iface, present_parameters, swapchain);
685
686 if (!present_parameters->Windowed)
687 {
688 WARN("Trying to create an additional fullscreen swapchain, returning D3DERR_INVALIDCALL.\n");
689 return D3DERR_INVALIDCALL;
690 }
691
692 wined3d_mutex_lock();
693 count = wined3d_device_get_swapchain_count(device->wined3d_device);
694 for (i = 0; i < count; ++i)
695 {
696 struct wined3d_swapchain *wined3d_swapchain;
697
698 wined3d_swapchain = wined3d_device_get_swapchain(device->wined3d_device, i);
699 wined3d_swapchain_get_desc(wined3d_swapchain, &desc);
700
701 if (!desc.windowed)
702 {
703 wined3d_mutex_unlock();
704 WARN("Trying to create an additional swapchain in fullscreen mode, returning D3DERR_INVALIDCALL.\n");
705 return D3DERR_INVALIDCALL;
706 }
707 }
708 wined3d_mutex_unlock();
709
710 if (!wined3d_swapchain_desc_from_present_parameters(&desc, present_parameters,
711 device->d3d_parent->extended))
712 return D3DERR_INVALIDCALL;
713 if (SUCCEEDED(hr = d3d9_swapchain_create(device, &desc, &object)))
714 *swapchain = (IDirect3DSwapChain9 *)&object->IDirect3DSwapChain9Ex_iface;
715 present_parameters_from_wined3d_swapchain_desc(present_parameters, &desc);
716
717 return hr;
718 }
719
720 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_GetSwapChain(IDirect3DDevice9Ex *iface,
721 UINT swapchain_idx, IDirect3DSwapChain9 **swapchain)
722 {
723 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
724 HRESULT hr;
725
726 TRACE("iface %p, swapchain_idx %u, swapchain %p.\n", iface, swapchain_idx, swapchain);
727
728 wined3d_mutex_lock();
729 if (swapchain_idx < device->implicit_swapchain_count)
730 {
731 *swapchain = (IDirect3DSwapChain9 *)&device->implicit_swapchains[swapchain_idx]->IDirect3DSwapChain9Ex_iface;
732 IDirect3DSwapChain9Ex_AddRef(*swapchain);
733 hr = D3D_OK;
734 }
735 else
736 {
737 *swapchain = NULL;
738 hr = D3DERR_INVALIDCALL;
739 }
740 wined3d_mutex_unlock();
741
742 return hr;
743 }
744
745 static UINT WINAPI d3d9_device_GetNumberOfSwapChains(IDirect3DDevice9Ex *iface)
746 {
747 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
748 UINT count;
749
750 TRACE("iface %p.\n", iface);
751
752 wined3d_mutex_lock();
753 count = wined3d_device_get_swapchain_count(device->wined3d_device);
754 wined3d_mutex_unlock();
755
756 return count;
757 }
758
759 static HRESULT CDECL reset_enum_callback(struct wined3d_resource *resource)
760 {
761 struct wined3d_resource_desc desc;
762 IDirect3DBaseTexture9 *texture;
763 struct d3d9_surface *surface;
764 IUnknown *parent;
765
766 wined3d_resource_get_desc(resource, &desc);
767 if (desc.pool != WINED3D_POOL_DEFAULT)
768 return D3D_OK;
769
770 if (desc.resource_type != WINED3D_RTYPE_TEXTURE_2D)
771 {
772 WARN("Resource %p in pool D3DPOOL_DEFAULT blocks the Reset call.\n", resource);
773 return D3DERR_INVALIDCALL;
774 }
775
776 parent = wined3d_resource_get_parent(resource);
777 if (parent && SUCCEEDED(IUnknown_QueryInterface(parent, &IID_IDirect3DBaseTexture9, (void **)&texture)))
778 {
779 IDirect3DBaseTexture9_Release(texture);
780 WARN("Texture %p (resource %p) in pool D3DPOOL_DEFAULT blocks the Reset call.\n", texture, resource);
781 return D3DERR_INVALIDCALL;
782 }
783
784 surface = wined3d_texture_get_sub_resource_parent(wined3d_texture_from_resource(resource), 0);
785 if (!surface->resource.refcount)
786 return D3D_OK;
787
788 WARN("Surface %p in pool D3DPOOL_DEFAULT blocks the Reset call.\n", surface);
789 return D3DERR_INVALIDCALL;
790 }
791
792 static HRESULT d3d9_device_get_swapchains(struct d3d9_device *device)
793 {
794 UINT i, new_swapchain_count = wined3d_device_get_swapchain_count(device->wined3d_device);
795 struct wined3d_swapchain *wined3d_swapchain;
796
797 if (!(device->implicit_swapchains = HeapAlloc(GetProcessHeap(), 0,
798 new_swapchain_count * sizeof(*device->implicit_swapchains))))
799 return E_OUTOFMEMORY;
800
801 for (i = 0; i < new_swapchain_count; ++i)
802 {
803 wined3d_swapchain = wined3d_device_get_swapchain(device->wined3d_device, i);
804 device->implicit_swapchains[i] = wined3d_swapchain_get_parent(wined3d_swapchain);
805 }
806 device->implicit_swapchain_count = new_swapchain_count;
807
808 return D3D_OK;
809 }
810
811 static HRESULT d3d9_device_reset(struct d3d9_device *device,
812 D3DPRESENT_PARAMETERS *present_parameters, D3DDISPLAYMODEEX *mode)
813 {
814 struct wined3d_swapchain_desc swapchain_desc;
815 struct wined3d_display_mode wined3d_mode;
816 HRESULT hr;
817
818 if (!device->d3d_parent->extended && device->device_state == D3D9_DEVICE_STATE_LOST)
819 {
820 WARN("App not active, returning D3DERR_DEVICELOST.\n");
821 return D3DERR_DEVICELOST;
822 }
823
824 if (mode)
825 {
826 wined3d_mode.width = mode->Width;
827 wined3d_mode.height = mode->Height;
828 wined3d_mode.refresh_rate = mode->RefreshRate;
829 wined3d_mode.format_id = wined3dformat_from_d3dformat(mode->Format);
830 wined3d_mode.scanline_ordering = mode->ScanLineOrdering;
831 }
832
833 if (!wined3d_swapchain_desc_from_present_parameters(&swapchain_desc, present_parameters,
834 device->d3d_parent->extended))
835 return D3DERR_INVALIDCALL;
836
837 wined3d_mutex_lock();
838
839 if (device->vertex_buffer)
840 {
841 wined3d_buffer_decref(device->vertex_buffer);
842 device->vertex_buffer = NULL;
843 device->vertex_buffer_size = 0;
844 }
845
846 if (device->index_buffer)
847 {
848 wined3d_buffer_decref(device->index_buffer);
849 device->index_buffer = NULL;
850 device->index_buffer_size = 0;
851 }
852
853 if (SUCCEEDED(hr = wined3d_device_reset(device->wined3d_device, &swapchain_desc,
854 mode ? &wined3d_mode : NULL, reset_enum_callback, !device->d3d_parent->extended)))
855 {
856 HeapFree(GetProcessHeap(), 0, device->implicit_swapchains);
857
858 if (FAILED(hr = d3d9_device_get_swapchains(device)))
859 {
860 device->device_state = D3D9_DEVICE_STATE_NOT_RESET;
861 }
862 else
863 {
864 wined3d_swapchain_get_desc(device->implicit_swapchains[0]->wined3d_swapchain, &swapchain_desc);
865 present_parameters->BackBufferWidth = swapchain_desc.backbuffer_width;
866 present_parameters->BackBufferHeight = swapchain_desc.backbuffer_height;
867 present_parameters->BackBufferFormat = d3dformat_from_wined3dformat(swapchain_desc.backbuffer_format);
868 present_parameters->BackBufferCount = swapchain_desc.backbuffer_count;
869
870 device->device_state = D3D9_DEVICE_STATE_OK;
871 }
872 }
873 else if (!device->d3d_parent->extended)
874 {
875 device->device_state = D3D9_DEVICE_STATE_NOT_RESET;
876 }
877
878 wined3d_mutex_unlock();
879
880 return hr;
881 }
882
883 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_Reset(IDirect3DDevice9Ex *iface,
884 D3DPRESENT_PARAMETERS *present_parameters)
885 {
886 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
887
888 TRACE("iface %p, present_parameters %p.\n", iface, present_parameters);
889
890 return d3d9_device_reset(device, present_parameters, NULL);
891 }
892
893 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_Present(IDirect3DDevice9Ex *iface,
894 const RECT *src_rect, const RECT *dst_rect, HWND dst_window_override, const RGNDATA *dirty_region)
895 {
896 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
897 UINT i;
898 HRESULT hr;
899
900 TRACE("iface %p, src_rect %p, dst_rect %p, dst_window_override %p, dirty_region %p.\n",
901 iface, src_rect, dst_rect, dst_window_override, dirty_region);
902
903 if (device->device_state != D3D9_DEVICE_STATE_OK)
904 return device->d3d_parent->extended ? S_PRESENT_OCCLUDED : D3DERR_DEVICELOST;
905
906 if (dirty_region)
907 FIXME("Ignoring dirty_region %p.\n", dirty_region);
908
909 wined3d_mutex_lock();
910 for (i = 0; i < device->implicit_swapchain_count; ++i)
911 {
912 if (FAILED(hr = wined3d_swapchain_present(device->implicit_swapchains[i]->wined3d_swapchain,
913 src_rect, dst_rect, dst_window_override, 0)))
914 {
915 wined3d_mutex_unlock();
916 return hr;
917 }
918 }
919 wined3d_mutex_unlock();
920
921 return D3D_OK;
922 }
923
924 static HRESULT WINAPI d3d9_device_GetBackBuffer(IDirect3DDevice9Ex *iface, UINT swapchain,
925 UINT backbuffer_idx, D3DBACKBUFFER_TYPE backbuffer_type, IDirect3DSurface9 **backbuffer)
926 {
927 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
928 HRESULT hr;
929
930 TRACE("iface %p, swapchain %u, backbuffer_idx %u, backbuffer_type %#x, backbuffer %p.\n",
931 iface, swapchain, backbuffer_idx, backbuffer_type, backbuffer);
932
933 /* backbuffer_type is ignored by native. */
934
935 /* No need to check for backbuffer == NULL, Windows crashes in that case. */
936 *backbuffer = NULL;
937
938 wined3d_mutex_lock();
939 if (swapchain >= device->implicit_swapchain_count)
940 {
941 wined3d_mutex_unlock();
942 WARN("Swapchain index %u is out of range, returning D3DERR_INVALIDCALL.\n", swapchain);
943 return D3DERR_INVALIDCALL;
944 }
945
946 hr = IDirect3DSwapChain9Ex_GetBackBuffer(&device->implicit_swapchains[swapchain]->IDirect3DSwapChain9Ex_iface,
947 backbuffer_idx, backbuffer_type, backbuffer);
948 wined3d_mutex_unlock();
949
950 return hr;
951 }
952
953 static HRESULT WINAPI d3d9_device_GetRasterStatus(IDirect3DDevice9Ex *iface,
954 UINT swapchain, D3DRASTER_STATUS *raster_status)
955 {
956 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
957 HRESULT hr;
958
959 TRACE("iface %p, swapchain %u, raster_status %p.\n", iface, swapchain, raster_status);
960
961 wined3d_mutex_lock();
962 hr = wined3d_device_get_raster_status(device->wined3d_device,
963 swapchain, (struct wined3d_raster_status *)raster_status);
964 wined3d_mutex_unlock();
965
966 return hr;
967 }
968
969 static HRESULT WINAPI d3d9_device_SetDialogBoxMode(IDirect3DDevice9Ex *iface, BOOL enable)
970 {
971 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
972 HRESULT hr;
973
974 TRACE("iface %p, enable %#x.\n", iface, enable);
975
976 wined3d_mutex_lock();
977 hr = wined3d_device_set_dialog_box_mode(device->wined3d_device, enable);
978 wined3d_mutex_unlock();
979
980 return hr;
981 }
982
983 static void WINAPI d3d9_device_SetGammaRamp(IDirect3DDevice9Ex *iface,
984 UINT swapchain, DWORD flags, const D3DGAMMARAMP *ramp)
985 {
986 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
987
988 TRACE("iface %p, swapchain %u, flags %#x, ramp %p.\n", iface, swapchain, flags, ramp);
989
990 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
991 wined3d_mutex_lock();
992 wined3d_device_set_gamma_ramp(device->wined3d_device, swapchain, flags, (const struct wined3d_gamma_ramp *)ramp);
993 wined3d_mutex_unlock();
994 }
995
996 static void WINAPI d3d9_device_GetGammaRamp(IDirect3DDevice9Ex *iface, UINT swapchain, D3DGAMMARAMP *ramp)
997 {
998 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
999
1000 TRACE("iface %p, swapchain %u, ramp %p.\n", iface, swapchain, ramp);
1001
1002 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
1003 wined3d_mutex_lock();
1004 wined3d_device_get_gamma_ramp(device->wined3d_device, swapchain, (struct wined3d_gamma_ramp *)ramp);
1005 wined3d_mutex_unlock();
1006 }
1007
1008 static HRESULT WINAPI d3d9_device_CreateTexture(IDirect3DDevice9Ex *iface,
1009 UINT width, UINT height, UINT levels, DWORD usage, D3DFORMAT format,
1010 D3DPOOL pool, IDirect3DTexture9 **texture, HANDLE *shared_handle)
1011 {
1012 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1013 struct d3d9_texture *object;
1014 BOOL set_mem = FALSE;
1015 HRESULT hr;
1016
1017 TRACE("iface %p, width %u, height %u, levels %u, usage %#x, format %#x, pool %#x, texture %p, shared_handle %p.\n",
1018 iface, width, height, levels, usage, format, pool, texture, shared_handle);
1019
1020 *texture = NULL;
1021 if (shared_handle)
1022 {
1023 if (!device->d3d_parent->extended)
1024 {
1025 WARN("Trying to create a shared or user memory texture on a non-ex device.\n");
1026 return E_NOTIMPL;
1027 }
1028
1029 if (pool == D3DPOOL_SYSTEMMEM)
1030 {
1031 if (levels != 1)
1032 return D3DERR_INVALIDCALL;
1033 set_mem = TRUE;
1034 }
1035 else
1036 {
1037 if (pool != D3DPOOL_DEFAULT)
1038 {
1039 WARN("Trying to create a shared texture in pool %#x.\n", pool);
1040 return D3DERR_INVALIDCALL;
1041 }
1042 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1043 }
1044 }
1045
1046 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1047 if (!object)
1048 return D3DERR_OUTOFVIDEOMEMORY;
1049
1050 hr = texture_init(object, device, width, height, levels, usage, format, pool);
1051 if (FAILED(hr))
1052 {
1053 WARN("Failed to initialize texture, hr %#x.\n", hr);
1054 HeapFree(GetProcessHeap(), 0, object);
1055 return hr;
1056 }
1057
1058 if (set_mem)
1059 wined3d_texture_update_desc(object->wined3d_texture, width, height,
1060 wined3dformat_from_d3dformat(format), WINED3D_MULTISAMPLE_NONE, 0,
1061 *shared_handle, 0);
1062
1063 TRACE("Created texture %p.\n", object);
1064 *texture = (IDirect3DTexture9 *)&object->IDirect3DBaseTexture9_iface;
1065
1066 return D3D_OK;
1067 }
1068
1069 static HRESULT WINAPI d3d9_device_CreateVolumeTexture(IDirect3DDevice9Ex *iface,
1070 UINT width, UINT height, UINT depth, UINT levels, DWORD usage, D3DFORMAT format,
1071 D3DPOOL pool, IDirect3DVolumeTexture9 **texture, HANDLE *shared_handle)
1072 {
1073 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1074 struct d3d9_texture *object;
1075 HRESULT hr;
1076
1077 TRACE("iface %p, width %u, height %u, depth %u, levels %u, "
1078 "usage %#x, format %#x, pool %#x, texture %p, shared_handle %p.\n",
1079 iface, width, height, depth, levels,
1080 usage, format, pool, texture, shared_handle);
1081
1082 *texture = NULL;
1083 if (shared_handle)
1084 {
1085 if (!device->d3d_parent->extended)
1086 {
1087 WARN("Trying to create a shared volume texture on a non-ex device.\n");
1088 return E_NOTIMPL;
1089 }
1090
1091 if (pool != D3DPOOL_DEFAULT)
1092 {
1093 WARN("Trying to create a shared volume texture in pool %#x.\n", pool);
1094 return D3DERR_INVALIDCALL;
1095 }
1096 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1097 }
1098
1099 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1100 if (!object)
1101 return D3DERR_OUTOFVIDEOMEMORY;
1102
1103 hr = volumetexture_init(object, device, width, height, depth, levels, usage, format, pool);
1104 if (FAILED(hr))
1105 {
1106 WARN("Failed to initialize volume texture, hr %#x.\n", hr);
1107 HeapFree(GetProcessHeap(), 0, object);
1108 return hr;
1109 }
1110
1111 TRACE("Created volume texture %p.\n", object);
1112 *texture = (IDirect3DVolumeTexture9 *)&object->IDirect3DBaseTexture9_iface;
1113
1114 return D3D_OK;
1115 }
1116
1117 static HRESULT WINAPI d3d9_device_CreateCubeTexture(IDirect3DDevice9Ex *iface,
1118 UINT edge_length, UINT levels, DWORD usage, D3DFORMAT format, D3DPOOL pool,
1119 IDirect3DCubeTexture9 **texture, HANDLE *shared_handle)
1120 {
1121 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1122 struct d3d9_texture *object;
1123 HRESULT hr;
1124
1125 TRACE("iface %p, edge_length %u, levels %u, usage %#x, format %#x, pool %#x, texture %p, shared_handle %p.\n",
1126 iface, edge_length, levels, usage, format, pool, texture, shared_handle);
1127
1128 *texture = NULL;
1129 if (shared_handle)
1130 {
1131 if (!device->d3d_parent->extended)
1132 {
1133 WARN("Trying to create a shared cube texture on a non-ex device.\n");
1134 return E_NOTIMPL;
1135 }
1136
1137 if (pool != D3DPOOL_DEFAULT)
1138 {
1139 WARN("Trying to create a shared cube texture in pool %#x.\n", pool);
1140 return D3DERR_INVALIDCALL;
1141 }
1142 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1143 }
1144
1145 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1146 if (!object)
1147 return D3DERR_OUTOFVIDEOMEMORY;
1148
1149 hr = cubetexture_init(object, device, edge_length, levels, usage, format, pool);
1150 if (FAILED(hr))
1151 {
1152 WARN("Failed to initialize cube texture, hr %#x.\n", hr);
1153 HeapFree(GetProcessHeap(), 0, object);
1154 return hr;
1155 }
1156
1157 TRACE("Created cube texture %p.\n", object);
1158 *texture = (IDirect3DCubeTexture9 *)&object->IDirect3DBaseTexture9_iface;
1159
1160 return D3D_OK;
1161 }
1162
1163 static HRESULT WINAPI d3d9_device_CreateVertexBuffer(IDirect3DDevice9Ex *iface, UINT size,
1164 DWORD usage, DWORD fvf, D3DPOOL pool, IDirect3DVertexBuffer9 **buffer,
1165 HANDLE *shared_handle)
1166 {
1167 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1168 struct d3d9_vertexbuffer *object;
1169 HRESULT hr;
1170
1171 TRACE("iface %p, size %u, usage %#x, fvf %#x, pool %#x, buffer %p, shared_handle %p.\n",
1172 iface, size, usage, fvf, pool, buffer, shared_handle);
1173
1174 if (shared_handle)
1175 {
1176 if (!device->d3d_parent->extended)
1177 {
1178 WARN("Trying to create a shared vertex buffer on a non-ex device.\n");
1179 return E_NOTIMPL;
1180 }
1181
1182 if (pool != D3DPOOL_DEFAULT)
1183 {
1184 WARN("Trying to create a shared vertex buffer in pool %#x.\n", pool);
1185 return D3DERR_NOTAVAILABLE;
1186 }
1187 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1188 }
1189
1190 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1191 if (!object)
1192 return D3DERR_OUTOFVIDEOMEMORY;
1193
1194 hr = vertexbuffer_init(object, device, size, usage, fvf, pool);
1195 if (FAILED(hr))
1196 {
1197 WARN("Failed to initialize vertex buffer, hr %#x.\n", hr);
1198 HeapFree(GetProcessHeap(), 0, object);
1199 return hr;
1200 }
1201
1202 TRACE("Created vertex buffer %p.\n", object);
1203 *buffer = &object->IDirect3DVertexBuffer9_iface;
1204
1205 return D3D_OK;
1206 }
1207
1208 static HRESULT WINAPI d3d9_device_CreateIndexBuffer(IDirect3DDevice9Ex *iface, UINT size,
1209 DWORD usage, D3DFORMAT format, D3DPOOL pool, IDirect3DIndexBuffer9 **buffer,
1210 HANDLE *shared_handle)
1211 {
1212 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1213 struct d3d9_indexbuffer *object;
1214 HRESULT hr;
1215
1216 TRACE("iface %p, size %u, usage %#x, format %#x, pool %#x, buffer %p, shared_handle %p.\n",
1217 iface, size, usage, format, pool, buffer, shared_handle);
1218
1219 if (shared_handle)
1220 {
1221 if (!device->d3d_parent->extended)
1222 {
1223 WARN("Trying to create a shared index buffer on a non-ex device.\n");
1224 return E_NOTIMPL;
1225 }
1226
1227 if (pool != D3DPOOL_DEFAULT)
1228 {
1229 WARN("Trying to create a shared index buffer in pool %#x.\n", pool);
1230 return D3DERR_NOTAVAILABLE;
1231 }
1232 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1233 }
1234
1235 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1236 if (!object)
1237 return D3DERR_OUTOFVIDEOMEMORY;
1238
1239 hr = indexbuffer_init(object, device, size, usage, format, pool);
1240 if (FAILED(hr))
1241 {
1242 WARN("Failed to initialize index buffer, hr %#x.\n", hr);
1243 HeapFree(GetProcessHeap(), 0, object);
1244 return hr;
1245 }
1246
1247 TRACE("Created index buffer %p.\n", object);
1248 *buffer = &object->IDirect3DIndexBuffer9_iface;
1249
1250 return D3D_OK;
1251 }
1252
1253 static HRESULT d3d9_device_create_surface(struct d3d9_device *device, UINT width, UINT height,
1254 D3DFORMAT format, DWORD flags, IDirect3DSurface9 **surface, UINT usage, D3DPOOL pool,
1255 D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality, void *user_mem)
1256 {
1257 struct wined3d_resource_desc desc;
1258 struct d3d9_surface *surface_impl;
1259 struct wined3d_texture *texture;
1260 HRESULT hr;
1261
1262 TRACE("device %p, width %u, height %u, format %#x, flags %#x, surface %p.\n"
1263 "usage %#x, pool %#x, multisample_type %#x, multisample_quality %u.\n",
1264 device, width, height, format, flags, surface, usage, pool,
1265 multisample_type, multisample_quality);
1266
1267 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
1268 desc.format = wined3dformat_from_d3dformat(format);
1269 desc.multisample_type = multisample_type;
1270 desc.multisample_quality = multisample_quality;
1271 desc.usage = usage & WINED3DUSAGE_MASK;
1272 desc.pool = pool;
1273 desc.width = width;
1274 desc.height = height;
1275 desc.depth = 1;
1276 desc.size = 0;
1277
1278 if (is_gdi_compat_wined3dformat(desc.format))
1279 flags |= WINED3D_TEXTURE_CREATE_GET_DC;
1280
1281 wined3d_mutex_lock();
1282
1283 if (FAILED(hr = wined3d_texture_create(device->wined3d_device, &desc,
1284 1, 1, flags, NULL, NULL, &d3d9_null_wined3d_parent_ops, &texture)))
1285 {
1286 wined3d_mutex_unlock();
1287 WARN("Failed to create texture, hr %#x.\n", hr);
1288 if (hr == WINED3DERR_NOTAVAILABLE)
1289 hr = D3DERR_INVALIDCALL;
1290 return hr;
1291 }
1292
1293 surface_impl = wined3d_texture_get_sub_resource_parent(texture, 0);
1294 surface_impl->parent_device = &device->IDirect3DDevice9Ex_iface;
1295 *surface = &surface_impl->IDirect3DSurface9_iface;
1296 IDirect3DSurface9_AddRef(*surface);
1297
1298 if (user_mem)
1299 wined3d_texture_update_desc(texture, width, height,
1300 desc.format, multisample_type, multisample_quality, user_mem, 0);
1301
1302 wined3d_texture_decref(texture);
1303
1304 wined3d_mutex_unlock();
1305
1306 return D3D_OK;
1307 }
1308
1309 BOOL is_gdi_compat_wined3dformat(enum wined3d_format_id format)
1310 {
1311 switch (format)
1312 {
1313 case WINED3DFMT_B8G8R8A8_UNORM:
1314 case WINED3DFMT_B8G8R8X8_UNORM:
1315 case WINED3DFMT_B5G6R5_UNORM:
1316 case WINED3DFMT_B5G5R5X1_UNORM:
1317 case WINED3DFMT_B5G5R5A1_UNORM:
1318 case WINED3DFMT_B8G8R8_UNORM:
1319 return TRUE;
1320 default:
1321 return FALSE;
1322 }
1323 }
1324
1325 static HRESULT WINAPI d3d9_device_CreateRenderTarget(IDirect3DDevice9Ex *iface, UINT width, UINT height,
1326 D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
1327 BOOL lockable, IDirect3DSurface9 **surface, HANDLE *shared_handle)
1328 {
1329 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1330 DWORD flags = 0;
1331
1332 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u.\n"
1333 "lockable %#x, surface %p, shared_handle %p.\n",
1334 iface, width, height, format, multisample_type, multisample_quality,
1335 lockable, surface, shared_handle);
1336
1337 *surface = NULL;
1338 if (shared_handle)
1339 {
1340 if (!device->d3d_parent->extended)
1341 {
1342 WARN("Trying to create a shared render target on a non-ex device.\n");
1343 return E_NOTIMPL;
1344 }
1345
1346 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1347 }
1348
1349 if (lockable)
1350 flags |= WINED3D_TEXTURE_CREATE_MAPPABLE;
1351
1352 return d3d9_device_create_surface(device, width, height, format, flags, surface,
1353 D3DUSAGE_RENDERTARGET, D3DPOOL_DEFAULT, multisample_type, multisample_quality, NULL);
1354 }
1355
1356 static HRESULT WINAPI d3d9_device_CreateDepthStencilSurface(IDirect3DDevice9Ex *iface, UINT width, UINT height,
1357 D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
1358 BOOL discard, IDirect3DSurface9 **surface, HANDLE *shared_handle)
1359 {
1360 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1361 DWORD flags = WINED3D_TEXTURE_CREATE_MAPPABLE;
1362
1363 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u.\n"
1364 "discard %#x, surface %p, shared_handle %p.\n",
1365 iface, width, height, format, multisample_type, multisample_quality,
1366 discard, surface, shared_handle);
1367
1368 *surface = NULL;
1369 if (shared_handle)
1370 {
1371 if (!device->d3d_parent->extended)
1372 {
1373 WARN("Trying to create a shared depth stencil on a non-ex device.\n");
1374 return E_NOTIMPL;
1375 }
1376
1377 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1378 }
1379
1380 if (discard)
1381 flags |= WINED3D_TEXTURE_CREATE_DISCARD;
1382
1383 return d3d9_device_create_surface(device, width, height, format, flags, surface,
1384 D3DUSAGE_DEPTHSTENCIL, D3DPOOL_DEFAULT, multisample_type, multisample_quality, NULL);
1385 }
1386
1387
1388 static HRESULT WINAPI d3d9_device_UpdateSurface(IDirect3DDevice9Ex *iface,
1389 IDirect3DSurface9 *src_surface, const RECT *src_rect,
1390 IDirect3DSurface9 *dst_surface, const POINT *dst_point)
1391 {
1392 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1393 struct d3d9_surface *src = unsafe_impl_from_IDirect3DSurface9(src_surface);
1394 struct d3d9_surface *dst = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1395 struct wined3d_box src_box;
1396 HRESULT hr;
1397
1398 TRACE("iface %p, src_surface %p, src_rect %p, dst_surface %p, dst_point %p.\n",
1399 iface, src_surface, src_rect, dst_surface, dst_point);
1400
1401 if (src_rect)
1402 wined3d_box_set(&src_box, src_rect->left, src_rect->top, src_rect->right, src_rect->bottom, 0, 1);
1403
1404 wined3d_mutex_lock();
1405 hr = wined3d_device_copy_sub_resource_region(device->wined3d_device,
1406 wined3d_texture_get_resource(dst->wined3d_texture), dst->sub_resource_idx, dst_point ? dst_point->x : 0,
1407 dst_point ? dst_point->y : 0, 0, wined3d_texture_get_resource(src->wined3d_texture),
1408 src->sub_resource_idx, src_rect ? &src_box : NULL);
1409 wined3d_mutex_unlock();
1410
1411 if (FAILED(hr))
1412 return D3DERR_INVALIDCALL;
1413
1414 return hr;
1415 }
1416
1417 static HRESULT WINAPI d3d9_device_UpdateTexture(IDirect3DDevice9Ex *iface,
1418 IDirect3DBaseTexture9 *src_texture, IDirect3DBaseTexture9 *dst_texture)
1419 {
1420 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1421 struct d3d9_texture *src_impl, *dst_impl;
1422 HRESULT hr;
1423
1424 TRACE("iface %p, src_texture %p, dst_texture %p.\n", iface, src_texture, dst_texture);
1425
1426 src_impl = unsafe_impl_from_IDirect3DBaseTexture9(src_texture);
1427 dst_impl = unsafe_impl_from_IDirect3DBaseTexture9(dst_texture);
1428
1429 wined3d_mutex_lock();
1430 hr = wined3d_device_update_texture(device->wined3d_device,
1431 src_impl->wined3d_texture, dst_impl->wined3d_texture);
1432 wined3d_mutex_unlock();
1433
1434 return hr;
1435 }
1436
1437 static HRESULT WINAPI d3d9_device_GetRenderTargetData(IDirect3DDevice9Ex *iface,
1438 IDirect3DSurface9 *render_target, IDirect3DSurface9 *dst_surface)
1439 {
1440 struct d3d9_surface *rt_impl = unsafe_impl_from_IDirect3DSurface9(render_target);
1441 struct d3d9_surface *dst_impl = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1442 struct wined3d_sub_resource_desc wined3d_desc;
1443 RECT dst_rect, src_rect;
1444 HRESULT hr;
1445
1446 TRACE("iface %p, render_target %p, dst_surface %p.\n", iface, render_target, dst_surface);
1447
1448 if (!render_target || !dst_surface)
1449 return D3DERR_INVALIDCALL;
1450
1451 wined3d_mutex_lock();
1452 wined3d_texture_get_sub_resource_desc(dst_impl->wined3d_texture, dst_impl->sub_resource_idx, &wined3d_desc);
1453 SetRect(&dst_rect, 0, 0, wined3d_desc.width, wined3d_desc.height);
1454
1455 wined3d_texture_get_sub_resource_desc(rt_impl->wined3d_texture, rt_impl->sub_resource_idx, &wined3d_desc);
1456 SetRect(&src_rect, 0, 0, wined3d_desc.width, wined3d_desc.height);
1457
1458 /* TODO: Check surface sizes, pools, etc. */
1459 if (wined3d_desc.multisample_type)
1460 hr = D3DERR_INVALIDCALL;
1461 else
1462 hr = wined3d_texture_blt(dst_impl->wined3d_texture, dst_impl->sub_resource_idx, &dst_rect,
1463 rt_impl->wined3d_texture, rt_impl->sub_resource_idx, &src_rect, 0, NULL, WINED3D_TEXF_POINT);
1464 wined3d_mutex_unlock();
1465
1466 return hr;
1467 }
1468
1469 static HRESULT WINAPI d3d9_device_GetFrontBufferData(IDirect3DDevice9Ex *iface,
1470 UINT swapchain, IDirect3DSurface9 *dst_surface)
1471 {
1472 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1473 struct d3d9_surface *dst_impl = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1474 HRESULT hr = D3DERR_INVALIDCALL;
1475
1476 TRACE("iface %p, swapchain %u, dst_surface %p.\n", iface, swapchain, dst_surface);
1477
1478 wined3d_mutex_lock();
1479 if (swapchain < device->implicit_swapchain_count)
1480 hr = wined3d_swapchain_get_front_buffer_data(device->implicit_swapchains[swapchain]->wined3d_swapchain,
1481 dst_impl->wined3d_texture, dst_impl->sub_resource_idx);
1482 wined3d_mutex_unlock();
1483
1484 return hr;
1485 }
1486
1487 static HRESULT WINAPI d3d9_device_StretchRect(IDirect3DDevice9Ex *iface, IDirect3DSurface9 *src_surface,
1488 const RECT *src_rect, IDirect3DSurface9 *dst_surface, const RECT *dst_rect, D3DTEXTUREFILTERTYPE filter)
1489 {
1490 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1491 struct d3d9_surface *src = unsafe_impl_from_IDirect3DSurface9(src_surface);
1492 struct d3d9_surface *dst = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1493 struct wined3d_sub_resource_desc src_desc, dst_desc;
1494 HRESULT hr = D3DERR_INVALIDCALL;
1495 RECT d, s;
1496
1497 TRACE("iface %p, src_surface %p, src_rect %p, dst_surface %p, dst_rect %p, filter %#x.\n",
1498 iface, src_surface, src_rect, dst_surface, dst_rect, filter);
1499
1500 wined3d_mutex_lock();
1501 wined3d_texture_get_sub_resource_desc(dst->wined3d_texture, dst->sub_resource_idx, &dst_desc);
1502 if (!dst_rect)
1503 {
1504 SetRect(&d, 0, 0, dst_desc.width, dst_desc.height);
1505 dst_rect = &d;
1506 }
1507
1508 wined3d_texture_get_sub_resource_desc(src->wined3d_texture, src->sub_resource_idx, &src_desc);
1509 if (!src_rect)
1510 {
1511 SetRect(&s, 0, 0, src_desc.width, src_desc.height);
1512 src_rect = &s;
1513 }
1514
1515 if (src_desc.usage & WINED3DUSAGE_DEPTHSTENCIL)
1516 {
1517 if (device->in_scene)
1518 {
1519 WARN("Rejecting depth / stencil blit while in scene.\n");
1520 goto done;
1521 }
1522
1523 if (src_rect->left || src_rect->top || src_rect->right != src_desc.width
1524 || src_rect->bottom != src_desc.height)
1525 {
1526 WARN("Rejecting depth / stencil blit with invalid source rect %s.\n",
1527 wine_dbgstr_rect(src_rect));
1528 goto done;
1529 }
1530
1531 if (dst_rect->left || dst_rect->top || dst_rect->right != dst_desc.width
1532 || dst_rect->bottom != dst_desc.height)
1533 {
1534 WARN("Rejecting depth / stencil blit with invalid destination rect %s.\n",
1535 wine_dbgstr_rect(dst_rect));
1536 goto done;
1537 }
1538
1539 if (src_desc.width != dst_desc.width || src_desc.height != dst_desc.height)
1540 {
1541 WARN("Rejecting depth / stencil blit with mismatched surface sizes.\n");
1542 goto done;
1543 }
1544 }
1545
1546 hr = wined3d_texture_blt(dst->wined3d_texture, dst->sub_resource_idx, dst_rect,
1547 src->wined3d_texture, src->sub_resource_idx, src_rect, 0, NULL, filter);
1548 if (hr == WINEDDERR_INVALIDRECT)
1549 hr = D3DERR_INVALIDCALL;
1550
1551 done:
1552 wined3d_mutex_unlock();
1553 return hr;
1554 }
1555
1556 static HRESULT WINAPI d3d9_device_ColorFill(IDirect3DDevice9Ex *iface,
1557 IDirect3DSurface9 *surface, const RECT *rect, D3DCOLOR color)
1558 {
1559 const struct wined3d_color c =
1560 {
1561 ((color >> 16) & 0xff) / 255.0f,
1562 ((color >> 8) & 0xff) / 255.0f,
1563 (color & 0xff) / 255.0f,
1564 ((color >> 24) & 0xff) / 255.0f,
1565 };
1566 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1567 struct d3d9_surface *surface_impl = unsafe_impl_from_IDirect3DSurface9(surface);
1568 struct wined3d_sub_resource_desc desc;
1569 struct wined3d_rendertarget_view *rtv;
1570 HRESULT hr;
1571
1572 TRACE("iface %p, surface %p, rect %p, color 0x%08x.\n", iface, surface, rect, color);
1573
1574 wined3d_mutex_lock();
1575
1576 if (FAILED(wined3d_texture_get_sub_resource_desc(surface_impl->wined3d_texture,
1577 surface_impl->sub_resource_idx, &desc)))
1578 {
1579 wined3d_mutex_unlock();
1580 return D3DERR_INVALIDCALL;
1581 }
1582
1583 if (desc.pool != WINED3D_POOL_DEFAULT)
1584 {
1585 wined3d_mutex_unlock();
1586 WARN("Colorfill is not allowed on surfaces in pool %#x, returning D3DERR_INVALIDCALL.\n", desc.pool);
1587 return D3DERR_INVALIDCALL;
1588 }
1589 if ((desc.usage & (WINED3DUSAGE_RENDERTARGET | WINED3DUSAGE_TEXTURE)) == WINED3DUSAGE_TEXTURE)
1590 {
1591 wined3d_mutex_unlock();
1592 WARN("Colorfill is not allowed on non-RT textures, returning D3DERR_INVALIDCALL.\n");
1593 return D3DERR_INVALIDCALL;
1594 }
1595 if (desc.usage & WINED3DUSAGE_DEPTHSTENCIL)
1596 {
1597 wined3d_mutex_unlock();
1598 WARN("Colorfill is not allowed on depth stencil surfaces, returning D3DERR_INVALIDCALL.\n");
1599 return D3DERR_INVALIDCALL;
1600 }
1601
1602 rtv = d3d9_surface_acquire_rendertarget_view(surface_impl);
1603 hr = wined3d_device_clear_rendertarget_view(device->wined3d_device,
1604 rtv, rect, WINED3DCLEAR_TARGET, &c, 0.0f, 0);
1605 d3d9_surface_release_rendertarget_view(surface_impl, rtv);
1606
1607 wined3d_mutex_unlock();
1608
1609 return hr;
1610 }
1611
1612 static HRESULT WINAPI d3d9_device_CreateOffscreenPlainSurface(IDirect3DDevice9Ex *iface,
1613 UINT width, UINT height, D3DFORMAT format, D3DPOOL pool, IDirect3DSurface9 **surface,
1614 HANDLE *shared_handle)
1615 {
1616 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1617 void *user_mem = NULL;
1618
1619 TRACE("iface %p, width %u, height %u, format %#x, pool %#x, surface %p, shared_handle %p.\n",
1620 iface, width, height, format, pool, surface, shared_handle);
1621
1622 *surface = NULL;
1623 if (pool == D3DPOOL_MANAGED)
1624 {
1625 WARN("Attempting to create a managed offscreen plain surface.\n");
1626 return D3DERR_INVALIDCALL;
1627 }
1628
1629 if (shared_handle)
1630 {
1631 if (!device->d3d_parent->extended)
1632 {
1633 WARN("Trying to create a shared or user memory surface on a non-ex device.\n");
1634 return E_NOTIMPL;
1635 }
1636
1637 if (pool == D3DPOOL_SYSTEMMEM)
1638 user_mem = *shared_handle;
1639 else
1640 {
1641 if (pool != D3DPOOL_DEFAULT)
1642 {
1643 WARN("Trying to create a shared surface in pool %#x.\n", pool);
1644 return D3DERR_INVALIDCALL;
1645 }
1646 FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1647 }
1648 }
1649
1650 /* FIXME: Offscreen surfaces are supposed to be always lockable,
1651 * regardless of the pool they're created in. Should we set dynamic usage
1652 * here? */
1653 return d3d9_device_create_surface(device, width, height, format,
1654 WINED3D_TEXTURE_CREATE_MAPPABLE, surface, 0, pool, D3DMULTISAMPLE_NONE, 0, user_mem);
1655 }
1656
1657 static HRESULT WINAPI d3d9_device_SetRenderTarget(IDirect3DDevice9Ex *iface, DWORD idx, IDirect3DSurface9 *surface)
1658 {
1659 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1660 struct d3d9_surface *surface_impl = unsafe_impl_from_IDirect3DSurface9(surface);
1661 struct wined3d_rendertarget_view *rtv;
1662 HRESULT hr;
1663
1664 TRACE("iface %p, idx %u, surface %p.\n", iface, idx, surface);
1665
1666 if (idx >= D3D9_MAX_SIMULTANEOUS_RENDERTARGETS)
1667 {
1668 WARN("Invalid index %u specified.\n", idx);
1669 return D3DERR_INVALIDCALL;
1670 }
1671
1672 if (!idx && !surface_impl)
1673 {
1674 WARN("Trying to set render target 0 to NULL.\n");
1675 return D3DERR_INVALIDCALL;
1676 }
1677
1678 if (surface_impl && d3d9_surface_get_device(surface_impl) != device)
1679 {
1680 WARN("Render target surface does not match device.\n");
1681 return D3DERR_INVALIDCALL;
1682 }
1683
1684 wined3d_mutex_lock();
1685 rtv = surface_impl ? d3d9_surface_acquire_rendertarget_view(surface_impl) : NULL;
1686 hr = wined3d_device_set_rendertarget_view(device->wined3d_device, idx, rtv, TRUE);
1687 d3d9_surface_release_rendertarget_view(surface_impl, rtv);
1688 wined3d_mutex_unlock();
1689
1690 return hr;
1691 }
1692
1693 static HRESULT WINAPI d3d9_device_GetRenderTarget(IDirect3DDevice9Ex *iface, DWORD idx, IDirect3DSurface9 **surface)
1694 {
1695 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1696 struct wined3d_rendertarget_view *wined3d_rtv;
1697 struct d3d9_surface *surface_impl;
1698 HRESULT hr = D3D_OK;
1699
1700 TRACE("iface %p, idx %u, surface %p.\n", iface, idx, surface);
1701
1702 if (!surface)
1703 return D3DERR_INVALIDCALL;
1704
1705 if (idx >= D3D9_MAX_SIMULTANEOUS_RENDERTARGETS)
1706 {
1707 WARN("Invalid index %u specified.\n", idx);
1708 return D3DERR_INVALIDCALL;
1709 }
1710
1711 wined3d_mutex_lock();
1712 if ((wined3d_rtv = wined3d_device_get_rendertarget_view(device->wined3d_device, idx)))
1713 {
1714 /* We want the sub resource parent here, since the view itself may be
1715 * internal to wined3d and may not have a parent. */
1716 surface_impl = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_rtv);
1717 *surface = &surface_impl->IDirect3DSurface9_iface;
1718 IDirect3DSurface9_AddRef(*surface);
1719 }
1720 else
1721 {
1722 hr = D3DERR_NOTFOUND;
1723 *surface = NULL;
1724 }
1725 wined3d_mutex_unlock();
1726
1727 return hr;
1728 }
1729
1730 static HRESULT WINAPI d3d9_device_SetDepthStencilSurface(IDirect3DDevice9Ex *iface, IDirect3DSurface9 *depth_stencil)
1731 {
1732 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1733 struct d3d9_surface *ds_impl = unsafe_impl_from_IDirect3DSurface9(depth_stencil);
1734 struct wined3d_rendertarget_view *rtv;
1735
1736 TRACE("iface %p, depth_stencil %p.\n", iface, depth_stencil);
1737
1738 wined3d_mutex_lock();
1739 rtv = ds_impl ? d3d9_surface_acquire_rendertarget_view(ds_impl) : NULL;
1740 wined3d_device_set_depth_stencil_view(device->wined3d_device, rtv);
1741 d3d9_surface_release_rendertarget_view(ds_impl, rtv);
1742 wined3d_mutex_unlock();
1743
1744 return D3D_OK;
1745 }
1746
1747 static HRESULT WINAPI d3d9_device_GetDepthStencilSurface(IDirect3DDevice9Ex *iface, IDirect3DSurface9 **depth_stencil)
1748 {
1749 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1750 struct wined3d_rendertarget_view *wined3d_dsv;
1751 struct d3d9_surface *surface_impl;
1752 HRESULT hr = D3D_OK;
1753
1754 TRACE("iface %p, depth_stencil %p.\n", iface, depth_stencil);
1755
1756 if (!depth_stencil)
1757 return D3DERR_INVALIDCALL;
1758
1759 wined3d_mutex_lock();
1760 if ((wined3d_dsv = wined3d_device_get_depth_stencil_view(device->wined3d_device)))
1761 {
1762 /* We want the sub resource parent here, since the view itself may be
1763 * internal to wined3d and may not have a parent. */
1764 surface_impl = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_dsv);
1765 *depth_stencil = &surface_impl->IDirect3DSurface9_iface;
1766 IDirect3DSurface9_AddRef(*depth_stencil);
1767 }
1768 else
1769 {
1770 hr = D3DERR_NOTFOUND;
1771 *depth_stencil = NULL;
1772 }
1773 wined3d_mutex_unlock();
1774
1775 return hr;
1776 }
1777
1778 static HRESULT WINAPI d3d9_device_BeginScene(IDirect3DDevice9Ex *iface)
1779 {
1780 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1781 HRESULT hr;
1782
1783 TRACE("iface %p.\n", iface);
1784
1785 wined3d_mutex_lock();
1786 if (SUCCEEDED(hr = wined3d_device_begin_scene(device->wined3d_device)))
1787 device->in_scene = TRUE;
1788 wined3d_mutex_unlock();
1789
1790 return hr;
1791 }
1792
1793 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_EndScene(IDirect3DDevice9Ex *iface)
1794 {
1795 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1796 HRESULT hr;
1797
1798 TRACE("iface %p.\n", iface);
1799
1800 wined3d_mutex_lock();
1801 if (SUCCEEDED(hr = wined3d_device_end_scene(device->wined3d_device)))
1802 device->in_scene = FALSE;
1803 wined3d_mutex_unlock();
1804
1805 return hr;
1806 }
1807
1808 static HRESULT WINAPI d3d9_device_Clear(IDirect3DDevice9Ex *iface, DWORD rect_count,
1809 const D3DRECT *rects, DWORD flags, D3DCOLOR color, float z, DWORD stencil)
1810 {
1811 const struct wined3d_color c =
1812 {
1813 ((color >> 16) & 0xff) / 255.0f,
1814 ((color >> 8) & 0xff) / 255.0f,
1815 (color & 0xff) / 255.0f,
1816 ((color >> 24) & 0xff) / 255.0f,
1817 };
1818 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1819 HRESULT hr;
1820
1821 TRACE("iface %p, rect_count %u, rects %p, flags %#x, color 0x%08x, z %.8e, stencil %u.\n",
1822 iface, rect_count, rects, flags, color, z, stencil);
1823
1824 if (rect_count && !rects)
1825 {
1826 WARN("count %u with NULL rects.\n", rect_count);
1827 rect_count = 0;
1828 }
1829
1830 wined3d_mutex_lock();
1831 hr = wined3d_device_clear(device->wined3d_device, rect_count, (const RECT *)rects, flags, &c, z, stencil);
1832 wined3d_mutex_unlock();
1833
1834 return hr;
1835 }
1836
1837 static HRESULT WINAPI d3d9_device_SetTransform(IDirect3DDevice9Ex *iface,
1838 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
1839 {
1840 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1841
1842 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1843
1844 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1845 wined3d_mutex_lock();
1846 wined3d_device_set_transform(device->wined3d_device, state, (const struct wined3d_matrix *)matrix);
1847 wined3d_mutex_unlock();
1848
1849 return D3D_OK;
1850 }
1851
1852 static HRESULT WINAPI d3d9_device_GetTransform(IDirect3DDevice9Ex *iface,
1853 D3DTRANSFORMSTATETYPE state, D3DMATRIX *matrix)
1854 {
1855 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1856
1857 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1858
1859 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1860 wined3d_mutex_lock();
1861 wined3d_device_get_transform(device->wined3d_device, state, (struct wined3d_matrix *)matrix);
1862 wined3d_mutex_unlock();
1863
1864 return D3D_OK;
1865 }
1866
1867 static HRESULT WINAPI d3d9_device_MultiplyTransform(IDirect3DDevice9Ex *iface,
1868 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
1869 {
1870 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1871
1872 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1873
1874 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1875 wined3d_mutex_lock();
1876 wined3d_device_multiply_transform(device->wined3d_device, state, (const struct wined3d_matrix *)matrix);
1877 wined3d_mutex_unlock();
1878
1879 return D3D_OK;
1880 }
1881
1882 static HRESULT WINAPI d3d9_device_SetViewport(IDirect3DDevice9Ex *iface, const D3DVIEWPORT9 *viewport)
1883 {
1884 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1885 struct wined3d_viewport vp;
1886
1887 TRACE("iface %p, viewport %p.\n", iface, viewport);
1888
1889 vp.x = viewport->X;
1890 vp.y = viewport->Y;
1891 vp.width = viewport->Width;
1892 vp.height = viewport->Height;
1893 vp.min_z = viewport->MinZ;
1894 vp.max_z = viewport->MaxZ;
1895
1896 wined3d_mutex_lock();
1897 wined3d_device_set_viewport(device->wined3d_device, &vp);
1898 wined3d_mutex_unlock();
1899
1900 return D3D_OK;
1901 }
1902
1903 static HRESULT WINAPI d3d9_device_GetViewport(IDirect3DDevice9Ex *iface, D3DVIEWPORT9 *viewport)
1904 {
1905 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1906 struct wined3d_viewport wined3d_viewport;
1907
1908 TRACE("iface %p, viewport %p.\n", iface, viewport);
1909
1910 wined3d_mutex_lock();
1911 wined3d_device_get_viewport(device->wined3d_device, &wined3d_viewport);
1912 wined3d_mutex_unlock();
1913
1914 viewport->X = wined3d_viewport.x;
1915 viewport->Y = wined3d_viewport.y;
1916 viewport->Width = wined3d_viewport.width;
1917 viewport->Height = wined3d_viewport.height;
1918 viewport->MinZ = wined3d_viewport.min_z;
1919 viewport->MaxZ = wined3d_viewport.max_z;
1920
1921 return D3D_OK;
1922 }
1923
1924 static HRESULT WINAPI d3d9_device_SetMaterial(IDirect3DDevice9Ex *iface, const D3DMATERIAL9 *material)
1925 {
1926 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1927
1928 TRACE("iface %p, material %p.\n", iface, material);
1929
1930 /* Note: D3DMATERIAL9 is compatible with struct wined3d_material. */
1931 wined3d_mutex_lock();
1932 wined3d_device_set_material(device->wined3d_device, (const struct wined3d_material *)material);
1933 wined3d_mutex_unlock();
1934
1935 return D3D_OK;
1936 }
1937
1938 static HRESULT WINAPI d3d9_device_GetMaterial(IDirect3DDevice9Ex *iface, D3DMATERIAL9 *material)
1939 {
1940 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1941
1942 TRACE("iface %p, material %p.\n", iface, material);
1943
1944 /* Note: D3DMATERIAL9 is compatible with struct wined3d_material. */
1945 wined3d_mutex_lock();
1946 wined3d_device_get_material(device->wined3d_device, (struct wined3d_material *)material);
1947 wined3d_mutex_unlock();
1948
1949 return D3D_OK;
1950 }
1951
1952 static HRESULT WINAPI d3d9_device_SetLight(IDirect3DDevice9Ex *iface, DWORD index, const D3DLIGHT9 *light)
1953 {
1954 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1955 HRESULT hr;
1956
1957 TRACE("iface %p, index %u, light %p.\n", iface, index, light);
1958
1959 /* Note: D3DLIGHT9 is compatible with struct wined3d_light. */
1960 wined3d_mutex_lock();
1961 hr = wined3d_device_set_light(device->wined3d_device, index, (const struct wined3d_light *)light);
1962 wined3d_mutex_unlock();
1963
1964 return hr;
1965 }
1966
1967 static HRESULT WINAPI d3d9_device_GetLight(IDirect3DDevice9Ex *iface, DWORD index, D3DLIGHT9 *light)
1968 {
1969 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1970 HRESULT hr;
1971
1972 TRACE("iface %p, index %u, light %p.\n", iface, index, light);
1973
1974 /* Note: D3DLIGHT9 is compatible with struct wined3d_light. */
1975 wined3d_mutex_lock();
1976 hr = wined3d_device_get_light(device->wined3d_device, index, (struct wined3d_light *)light);
1977 wined3d_mutex_unlock();
1978
1979 return hr;
1980 }
1981
1982 static HRESULT WINAPI d3d9_device_LightEnable(IDirect3DDevice9Ex *iface, DWORD index, BOOL enable)
1983 {
1984 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1985 HRESULT hr;
1986
1987 TRACE("iface %p, index %u, enable %#x.\n", iface, index, enable);
1988
1989 wined3d_mutex_lock();
1990 hr = wined3d_device_set_light_enable(device->wined3d_device, index, enable);
1991 wined3d_mutex_unlock();
1992
1993 return hr;
1994 }
1995
1996 static HRESULT WINAPI d3d9_device_GetLightEnable(IDirect3DDevice9Ex *iface, DWORD index, BOOL *enable)
1997 {
1998 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
1999 HRESULT hr;
2000
2001 TRACE("iface %p, index %u, enable %p.\n", iface, index, enable);
2002
2003 wined3d_mutex_lock();
2004 hr = wined3d_device_get_light_enable(device->wined3d_device, index, enable);
2005 wined3d_mutex_unlock();
2006
2007 return hr;
2008 }
2009
2010 static HRESULT WINAPI d3d9_device_SetClipPlane(IDirect3DDevice9Ex *iface, DWORD index, const float *plane)
2011 {
2012 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2013 HRESULT hr;
2014
2015 TRACE("iface %p, index %u, plane %p.\n", iface, index, plane);
2016
2017 wined3d_mutex_lock();
2018 hr = wined3d_device_set_clip_plane(device->wined3d_device, index, (const struct wined3d_vec4 *)plane);
2019 wined3d_mutex_unlock();
2020
2021 return hr;
2022 }
2023
2024 static HRESULT WINAPI d3d9_device_GetClipPlane(IDirect3DDevice9Ex *iface, DWORD index, float *plane)
2025 {
2026 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2027 HRESULT hr;
2028
2029 TRACE("iface %p, index %u, plane %p.\n", iface, index, plane);
2030
2031 wined3d_mutex_lock();
2032 hr = wined3d_device_get_clip_plane(device->wined3d_device, index, (struct wined3d_vec4 *)plane);
2033 wined3d_mutex_unlock();
2034
2035 return hr;
2036 }
2037
2038 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_SetRenderState(IDirect3DDevice9Ex *iface,
2039 D3DRENDERSTATETYPE state, DWORD value)
2040 {
2041 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2042
2043 TRACE("iface %p, state %#x, value %#x.\n", iface, state, value);
2044
2045 wined3d_mutex_lock();
2046 wined3d_device_set_render_state(device->wined3d_device, state, value);
2047 wined3d_mutex_unlock();
2048
2049 return D3D_OK;
2050 }
2051
2052 static HRESULT WINAPI d3d9_device_GetRenderState(IDirect3DDevice9Ex *iface,
2053 D3DRENDERSTATETYPE state, DWORD *value)
2054 {
2055 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2056
2057 TRACE("iface %p, state %#x, value %p.\n", iface, state, value);
2058
2059 wined3d_mutex_lock();
2060 *value = wined3d_device_get_render_state(device->wined3d_device, state);
2061 wined3d_mutex_unlock();
2062
2063 return D3D_OK;
2064 }
2065
2066 static HRESULT WINAPI d3d9_device_CreateStateBlock(IDirect3DDevice9Ex *iface,
2067 D3DSTATEBLOCKTYPE type, IDirect3DStateBlock9 **stateblock)
2068 {
2069 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2070 struct d3d9_stateblock *object;
2071 HRESULT hr;
2072
2073 TRACE("iface %p, type %#x, stateblock %p.\n", iface, type, stateblock);
2074
2075 if (type != D3DSBT_ALL && type != D3DSBT_PIXELSTATE && type != D3DSBT_VERTEXSTATE)
2076 {
2077 WARN("Unexpected stateblock type, returning D3DERR_INVALIDCALL.\n");
2078 return D3DERR_INVALIDCALL;
2079 }
2080
2081 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2082 if (!object)
2083 return E_OUTOFMEMORY;
2084
2085 hr = stateblock_init(object, device, type, NULL);
2086 if (FAILED(hr))
2087 {
2088 WARN("Failed to initialize stateblock, hr %#x.\n", hr);
2089 HeapFree(GetProcessHeap(), 0, object);
2090 return hr;
2091 }
2092
2093 TRACE("Created stateblock %p.\n", object);
2094 *stateblock = &object->IDirect3DStateBlock9_iface;
2095
2096 return D3D_OK;
2097 }
2098
2099 static HRESULT WINAPI d3d9_device_BeginStateBlock(IDirect3DDevice9Ex *iface)
2100 {
2101 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2102 HRESULT hr;
2103
2104 TRACE("iface %p.\n", iface);
2105
2106 wined3d_mutex_lock();
2107 hr = wined3d_device_begin_stateblock(device->wined3d_device);
2108 wined3d_mutex_unlock();
2109
2110 return hr;
2111 }
2112
2113 static HRESULT WINAPI d3d9_device_EndStateBlock(IDirect3DDevice9Ex *iface, IDirect3DStateBlock9 **stateblock)
2114 {
2115 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2116 struct wined3d_stateblock *wined3d_stateblock;
2117 struct d3d9_stateblock *object;
2118 HRESULT hr;
2119
2120 TRACE("iface %p, stateblock %p.\n", iface, stateblock);
2121
2122 wined3d_mutex_lock();
2123 hr = wined3d_device_end_stateblock(device->wined3d_device, &wined3d_stateblock);
2124 wined3d_mutex_unlock();
2125 if (FAILED(hr))
2126 {
2127 WARN("Failed to end the state block, hr %#x.\n", hr);
2128 return hr;
2129 }
2130
2131 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2132 if (!object)
2133 {
2134 wined3d_mutex_lock();
2135 wined3d_stateblock_decref(wined3d_stateblock);
2136 wined3d_mutex_unlock();
2137 return E_OUTOFMEMORY;
2138 }
2139
2140 hr = stateblock_init(object, device, 0, wined3d_stateblock);
2141 if (FAILED(hr))
2142 {
2143 WARN("Failed to initialize stateblock, hr %#x.\n", hr);
2144 wined3d_mutex_lock();
2145 wined3d_stateblock_decref(wined3d_stateblock);
2146 wined3d_mutex_unlock();
2147 HeapFree(GetProcessHeap(), 0, object);
2148 return hr;
2149 }
2150
2151 TRACE("Created stateblock %p.\n", object);
2152 *stateblock = &object->IDirect3DStateBlock9_iface;
2153
2154 return D3D_OK;
2155 }
2156
2157 static HRESULT WINAPI d3d9_device_SetClipStatus(IDirect3DDevice9Ex *iface, const D3DCLIPSTATUS9 *clip_status)
2158 {
2159 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2160 HRESULT hr;
2161
2162 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
2163
2164 wined3d_mutex_lock();
2165 hr = wined3d_device_set_clip_status(device->wined3d_device, (const struct wined3d_clip_status *)clip_status);
2166 wined3d_mutex_unlock();
2167
2168 return hr;
2169 }
2170
2171 static HRESULT WINAPI d3d9_device_GetClipStatus(IDirect3DDevice9Ex *iface, D3DCLIPSTATUS9 *clip_status)
2172 {
2173 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2174 HRESULT hr;
2175
2176 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
2177
2178 wined3d_mutex_lock();
2179 hr = wined3d_device_get_clip_status(device->wined3d_device, (struct wined3d_clip_status *)clip_status);
2180 wined3d_mutex_unlock();
2181
2182 return hr;
2183 }
2184
2185 static HRESULT WINAPI d3d9_device_GetTexture(IDirect3DDevice9Ex *iface, DWORD stage, IDirect3DBaseTexture9 **texture)
2186 {
2187 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2188 struct wined3d_texture *wined3d_texture = NULL;
2189 struct d3d9_texture *texture_impl;
2190
2191 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
2192
2193 if (!texture)
2194 return D3DERR_INVALIDCALL;
2195
2196 wined3d_mutex_lock();
2197 if ((wined3d_texture = wined3d_device_get_texture(device->wined3d_device, stage)))
2198 {
2199 texture_impl = wined3d_texture_get_parent(wined3d_texture);
2200 *texture = &texture_impl->IDirect3DBaseTexture9_iface;
2201 IDirect3DBaseTexture9_AddRef(*texture);
2202 }
2203 else
2204 {
2205 *texture = NULL;
2206 }
2207 wined3d_mutex_unlock();
2208
2209 return D3D_OK;
2210 }
2211
2212 static HRESULT WINAPI d3d9_device_SetTexture(IDirect3DDevice9Ex *iface, DWORD stage, IDirect3DBaseTexture9 *texture)
2213 {
2214 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2215 struct d3d9_texture *texture_impl;
2216 HRESULT hr;
2217
2218 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
2219
2220 texture_impl = unsafe_impl_from_IDirect3DBaseTexture9(texture);
2221
2222 wined3d_mutex_lock();
2223 hr = wined3d_device_set_texture(device->wined3d_device, stage,
2224 texture_impl ? texture_impl->wined3d_texture : NULL);
2225 wined3d_mutex_unlock();
2226
2227 return hr;
2228 }
2229
2230 static const enum wined3d_texture_stage_state tss_lookup[] =
2231 {
2232 WINED3D_TSS_INVALID, /* 0, unused */
2233 WINED3D_TSS_COLOR_OP, /* 1, D3DTSS_COLOROP */
2234 WINED3D_TSS_COLOR_ARG1, /* 2, D3DTSS_COLORARG1 */
2235 WINED3D_TSS_COLOR_ARG2, /* 3, D3DTSS_COLORARG2 */
2236 WINED3D_TSS_ALPHA_OP, /* 4, D3DTSS_ALPHAOP */
2237 WINED3D_TSS_ALPHA_ARG1, /* 5, D3DTSS_ALPHAARG1 */
2238 WINED3D_TSS_ALPHA_ARG2, /* 6, D3DTSS_ALPHAARG2 */
2239 WINED3D_TSS_BUMPENV_MAT00, /* 7, D3DTSS_BUMPENVMAT00 */
2240 WINED3D_TSS_BUMPENV_MAT01, /* 8, D3DTSS_BUMPENVMAT01 */
2241 WINED3D_TSS_BUMPENV_MAT10, /* 9, D3DTSS_BUMPENVMAT10 */
2242 WINED3D_TSS_BUMPENV_MAT11, /* 10, D3DTSS_BUMPENVMAT11 */
2243 WINED3D_TSS_TEXCOORD_INDEX, /* 11, D3DTSS_TEXCOORDINDEX */
2244 WINED3D_TSS_INVALID, /* 12, unused */
2245 WINED3D_TSS_INVALID, /* 13, unused */
2246 WINED3D_TSS_INVALID, /* 14, unused */
2247 WINED3D_TSS_INVALID, /* 15, unused */
2248 WINED3D_TSS_INVALID, /* 16, unused */
2249 WINED3D_TSS_INVALID, /* 17, unused */
2250 WINED3D_TSS_INVALID, /* 18, unused */
2251 WINED3D_TSS_INVALID, /* 19, unused */
2252 WINED3D_TSS_INVALID, /* 20, unused */
2253 WINED3D_TSS_INVALID, /* 21, unused */
2254 WINED3D_TSS_BUMPENV_LSCALE, /* 22, D3DTSS_BUMPENVLSCALE */
2255 WINED3D_TSS_BUMPENV_LOFFSET, /* 23, D3DTSS_BUMPENVLOFFSET */
2256 WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS, /* 24, D3DTSS_TEXTURETRANSFORMFLAGS */
2257 WINED3D_TSS_INVALID, /* 25, unused */
2258 WINED3D_TSS_COLOR_ARG0, /* 26, D3DTSS_COLORARG0 */
2259 WINED3D_TSS_ALPHA_ARG0, /* 27, D3DTSS_ALPHAARG0 */
2260 WINED3D_TSS_RESULT_ARG, /* 28, D3DTSS_RESULTARG */
2261 WINED3D_TSS_INVALID, /* 29, unused */
2262 WINED3D_TSS_INVALID, /* 30, unused */
2263 WINED3D_TSS_INVALID, /* 31, unused */
2264 WINED3D_TSS_CONSTANT, /* 32, D3DTSS_CONSTANT */
2265 };
2266
2267 static HRESULT WINAPI d3d9_device_GetTextureStageState(IDirect3DDevice9Ex *iface,
2268 DWORD stage, D3DTEXTURESTAGESTATETYPE state, DWORD *value)
2269 {
2270 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2271
2272 TRACE("iface %p, stage %u, state %#x, value %p.\n", iface, stage, state, value);
2273
2274 if (state >= sizeof(tss_lookup) / sizeof(*tss_lookup))
2275 {
2276 WARN("Invalid state %#x passed.\n", state);
2277 return D3D_OK;
2278 }
2279
2280 wined3d_mutex_lock();
2281 *value = wined3d_device_get_texture_stage_state(device->wined3d_device, stage, tss_lookup[state]);
2282 wined3d_mutex_unlock();
2283
2284 return D3D_OK;
2285 }
2286
2287 static HRESULT WINAPI d3d9_device_SetTextureStageState(IDirect3DDevice9Ex *iface,
2288 DWORD stage, D3DTEXTURESTAGESTATETYPE state, DWORD value)
2289 {
2290 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2291
2292 TRACE("iface %p, stage %u, state %#x, value %#x.\n", iface, stage, state, value);
2293
2294 if (state >= sizeof(tss_lookup) / sizeof(*tss_lookup))
2295 {
2296 WARN("Invalid state %#x passed.\n", state);
2297 return D3D_OK;
2298 }
2299
2300 wined3d_mutex_lock();
2301 wined3d_device_set_texture_stage_state(device->wined3d_device, stage, tss_lookup[state], value);
2302 wined3d_mutex_unlock();
2303
2304 return D3D_OK;
2305 }
2306
2307 static HRESULT WINAPI d3d9_device_GetSamplerState(IDirect3DDevice9Ex *iface,
2308 DWORD sampler, D3DSAMPLERSTATETYPE state, DWORD *value)
2309 {
2310 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2311
2312 TRACE("iface %p, sampler %u, state %#x, value %p.\n", iface, sampler, state, value);
2313
2314 wined3d_mutex_lock();
2315 *value = wined3d_device_get_sampler_state(device->wined3d_device, sampler, state);
2316 wined3d_mutex_unlock();
2317
2318 return D3D_OK;
2319 }
2320
2321 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_device_SetSamplerState(IDirect3DDevice9Ex *iface,
2322 DWORD sampler, D3DSAMPLERSTATETYPE state, DWORD value)
2323 {
2324 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2325
2326 TRACE("iface %p, sampler %u, state %#x, value %#x.\n", iface, sampler, state, value);
2327
2328 wined3d_mutex_lock();
2329 wined3d_device_set_sampler_state(device->wined3d_device, sampler, state, value);
2330 wined3d_mutex_unlock();
2331
2332 return D3D_OK;
2333 }
2334
2335 static HRESULT WINAPI d3d9_device_ValidateDevice(IDirect3DDevice9Ex *iface, DWORD *pass_count)
2336 {
2337 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2338 HRESULT hr;
2339
2340 TRACE("iface %p, pass_count %p.\n", iface, pass_count);
2341
2342 wined3d_mutex_lock();
2343 hr = wined3d_device_validate_device(device->wined3d_device, pass_count);
2344 wined3d_mutex_unlock();
2345
2346 return hr;
2347 }
2348
2349 static HRESULT WINAPI d3d9_device_SetPaletteEntries(IDirect3DDevice9Ex *iface,
2350 UINT palette_idx, const PALETTEENTRY *entries)
2351 {
2352 WARN("iface %p, palette_idx %u, entries %p unimplemented.\n", iface, palette_idx, entries);
2353
2354 /* The d3d9 palette API is non-functional on Windows. Getters and setters are implemented,
2355 * and some drivers allow the creation of P8 surfaces. These surfaces can be copied to
2356 * other P8 surfaces with StretchRect, but cannot be converted to (A)RGB.
2357 *
2358 * Some older(dx7) cards may have support for P8 textures, but games cannot rely on this. */
2359 return D3D_OK;
2360 }
2361
2362 static HRESULT WINAPI d3d9_device_GetPaletteEntries(IDirect3DDevice9Ex *iface,
2363 UINT palette_idx, PALETTEENTRY *entries)
2364 {
2365 FIXME("iface %p, palette_idx %u, entries %p unimplemented.\n", iface, palette_idx, entries);
2366
2367 return D3DERR_INVALIDCALL;
2368 }
2369
2370 static HRESULT WINAPI d3d9_device_SetCurrentTexturePalette(IDirect3DDevice9Ex *iface, UINT palette_idx)
2371 {
2372 WARN("iface %p, palette_idx %u unimplemented.\n", iface, palette_idx);
2373
2374 return D3D_OK;
2375 }
2376
2377 static HRESULT WINAPI d3d9_device_GetCurrentTexturePalette(IDirect3DDevice9Ex *iface, UINT *palette_idx)
2378 {
2379 FIXME("iface %p, palette_idx %p.\n", iface, palette_idx);
2380
2381 return D3DERR_INVALIDCALL;
2382 }
2383
2384 static HRESULT WINAPI d3d9_device_SetScissorRect(IDirect3DDevice9Ex *iface, const RECT *rect)
2385 {
2386 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2387
2388 TRACE("iface %p, rect %p.\n", iface, rect);
2389
2390 wined3d_mutex_lock();
2391 wined3d_device_set_scissor_rect(device->wined3d_device, rect);
2392 wined3d_mutex_unlock();
2393
2394 return D3D_OK;
2395 }
2396
2397 static HRESULT WINAPI d3d9_device_GetScissorRect(IDirect3DDevice9Ex *iface, RECT *rect)
2398 {
2399 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2400
2401 TRACE("iface %p, rect %p.\n", iface, rect);
2402
2403 wined3d_mutex_lock();
2404 wined3d_device_get_scissor_rect(device->wined3d_device, rect);
2405 wined3d_mutex_unlock();
2406
2407 return D3D_OK;
2408 }
2409
2410 static HRESULT WINAPI d3d9_device_SetSoftwareVertexProcessing(IDirect3DDevice9Ex *iface, BOOL software)
2411 {
2412 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2413
2414 TRACE("iface %p, software %#x.\n", iface, software);
2415
2416 wined3d_mutex_lock();
2417 wined3d_device_set_software_vertex_processing(device->wined3d_device, software);
2418 wined3d_mutex_unlock();
2419
2420 return D3D_OK;
2421 }
2422
2423 static BOOL WINAPI d3d9_device_GetSoftwareVertexProcessing(IDirect3DDevice9Ex *iface)
2424 {
2425 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2426 BOOL ret;
2427
2428 TRACE("iface %p.\n", iface);
2429
2430 wined3d_mutex_lock();
2431 ret = wined3d_device_get_software_vertex_processing(device->wined3d_device);
2432 wined3d_mutex_unlock();
2433
2434 return ret;
2435 }
2436
2437 static HRESULT WINAPI d3d9_device_SetNPatchMode(IDirect3DDevice9Ex *iface, float segment_count)
2438 {
2439 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2440 HRESULT hr;
2441
2442 TRACE("iface %p, segment_count %.8e.\n", iface, segment_count);
2443
2444 wined3d_mutex_lock();
2445 hr = wined3d_device_set_npatch_mode(device->wined3d_device, segment_count);
2446 wined3d_mutex_unlock();
2447
2448 return hr;
2449 }
2450
2451 static float WINAPI d3d9_device_GetNPatchMode(IDirect3DDevice9Ex *iface)
2452 {
2453 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2454 float ret;
2455
2456 TRACE("iface %p.\n", iface);
2457
2458 wined3d_mutex_lock();
2459 ret = wined3d_device_get_npatch_mode(device->wined3d_device);
2460 wined3d_mutex_unlock();
2461
2462 return ret;
2463 }
2464
2465 static HRESULT WINAPI d3d9_device_DrawPrimitive(IDirect3DDevice9Ex *iface,
2466 D3DPRIMITIVETYPE primitive_type, UINT start_vertex, UINT primitive_count)
2467 {
2468 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2469 HRESULT hr;
2470
2471 TRACE("iface %p, primitive_type %#x, start_vertex %u, primitive_count %u.\n",
2472 iface, primitive_type, start_vertex, primitive_count);
2473
2474 wined3d_mutex_lock();
2475 if (!device->has_vertex_declaration)
2476 {
2477 wined3d_mutex_unlock();
2478 WARN("Called without a valid vertex declaration set.\n");
2479 return D3DERR_INVALIDCALL;
2480 }
2481 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type, 0);
2482 hr = wined3d_device_draw_primitive(device->wined3d_device, start_vertex,
2483 vertex_count_from_primitive_count(primitive_type, primitive_count));
2484 wined3d_mutex_unlock();
2485
2486 return hr;
2487 }
2488
2489 static HRESULT WINAPI d3d9_device_DrawIndexedPrimitive(IDirect3DDevice9Ex *iface,
2490 D3DPRIMITIVETYPE primitive_type, INT base_vertex_idx, UINT min_vertex_idx,
2491 UINT vertex_count, UINT start_idx, UINT primitive_count)
2492 {
2493 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2494 HRESULT hr;
2495
2496 TRACE("iface %p, primitive_type %#x, base_vertex_idx %u, min_vertex_idx %u, "
2497 "vertex_count %u, start_idx %u, primitive_count %u.\n",
2498 iface, primitive_type, base_vertex_idx, min_vertex_idx,
2499 vertex_count, start_idx, primitive_count);
2500
2501 wined3d_mutex_lock();
2502 if (!device->has_vertex_declaration)
2503 {
2504 wined3d_mutex_unlock();
2505 WARN("Called without a valid vertex declaration set.\n");
2506 return D3DERR_INVALIDCALL;
2507 }
2508 wined3d_device_set_base_vertex_index(device->wined3d_device, base_vertex_idx);
2509 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type, 0);
2510 hr = wined3d_device_draw_indexed_primitive(device->wined3d_device, start_idx,
2511 vertex_count_from_primitive_count(primitive_type, primitive_count));
2512 wined3d_mutex_unlock();
2513
2514 return hr;
2515 }
2516
2517 /* The caller is responsible for wined3d locking */
2518 static HRESULT d3d9_device_prepare_vertex_buffer(struct d3d9_device *device, UINT min_size)
2519 {
2520 HRESULT hr;
2521
2522 if (device->vertex_buffer_size < min_size || !device->vertex_buffer)
2523 {
2524 UINT size = max(device->vertex_buffer_size * 2, min_size);
2525 struct wined3d_buffer *buffer;
2526
2527 TRACE("Growing vertex buffer to %u bytes.\n", size);
2528
2529 hr = wined3d_buffer_create_vb(device->wined3d_device, size, WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_WRITEONLY,
2530 WINED3D_POOL_DEFAULT, NULL, &d3d9_null_wined3d_parent_ops, &buffer);
2531 if (FAILED(hr))
2532 {
2533 ERR("(%p) wined3d_buffer_create_vb failed with hr = %08x.\n", device, hr);
2534 return hr;
2535 }
2536
2537 if (device->vertex_buffer)
2538 wined3d_buffer_decref(device->vertex_buffer);
2539
2540 device->vertex_buffer = buffer;
2541 device->vertex_buffer_size = size;
2542 device->vertex_buffer_pos = 0;
2543 }
2544 return D3D_OK;
2545 }
2546
2547 static HRESULT WINAPI d3d9_device_DrawPrimitiveUP(IDirect3DDevice9Ex *iface,
2548 D3DPRIMITIVETYPE primitive_type, UINT primitive_count, const void *data, UINT stride)
2549 {
2550 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2551 HRESULT hr;
2552 UINT vtx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2553 struct wined3d_map_desc wined3d_map_desc;
2554 struct wined3d_box wined3d_box = {0};
2555 UINT size = vtx_count * stride;
2556 struct wined3d_resource *vb;
2557 UINT vb_pos, align;
2558
2559 TRACE("iface %p, primitive_type %#x, primitive_count %u, data %p, stride %u.\n",
2560 iface, primitive_type, primitive_count, data, stride);
2561
2562 if (!primitive_count)
2563 {
2564 WARN("primitive_count is 0, returning D3D_OK\n");
2565 return D3D_OK;
2566 }
2567
2568 wined3d_mutex_lock();
2569
2570 if (!device->has_vertex_declaration)
2571 {
2572 wined3d_mutex_unlock();
2573 WARN("Called without a valid vertex declaration set.\n");
2574 return D3DERR_INVALIDCALL;
2575 }
2576
2577 hr = d3d9_device_prepare_vertex_buffer(device, size);
2578 if (FAILED(hr))
2579 goto done;
2580
2581 vb_pos = device->vertex_buffer_pos;
2582 align = vb_pos % stride;
2583 if (align) align = stride - align;
2584 if (vb_pos + size + align > device->vertex_buffer_size)
2585 vb_pos = 0;
2586 else
2587 vb_pos += align;
2588
2589 wined3d_box.left = vb_pos;
2590 wined3d_box.right = vb_pos + size;
2591 vb = wined3d_buffer_get_resource(device->vertex_buffer);
2592 if (FAILED(hr = wined3d_resource_map(vb, 0, &wined3d_map_desc, &wined3d_box,
2593 vb_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD)))
2594 goto done;
2595 memcpy(wined3d_map_desc.data, data, size);
2596 wined3d_resource_unmap(vb, 0);
2597 device->vertex_buffer_pos = vb_pos + size;
2598
2599 hr = wined3d_device_set_stream_source(device->wined3d_device, 0, device->vertex_buffer, 0, stride);
2600 if (FAILED(hr))
2601 goto done;
2602
2603 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type, 0);
2604 hr = wined3d_device_draw_primitive(device->wined3d_device, vb_pos / stride, vtx_count);
2605 wined3d_device_set_stream_source(device->wined3d_device, 0, NULL, 0, 0);
2606
2607 done:
2608 wined3d_mutex_unlock();
2609 return hr;
2610 }
2611
2612 /* The caller is responsible for wined3d locking */
2613 static HRESULT d3d9_device_prepare_index_buffer(struct d3d9_device *device, UINT min_size)
2614 {
2615 HRESULT hr;
2616
2617 if (device->index_buffer_size < min_size || !device->index_buffer)
2618 {
2619 UINT size = max(device->index_buffer_size * 2, min_size);
2620 struct wined3d_buffer *buffer;
2621
2622 TRACE("Growing index buffer to %u bytes.\n", size);
2623
2624 hr = wined3d_buffer_create_ib(device->wined3d_device, size, WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_WRITEONLY,
2625 WINED3D_POOL_DEFAULT, NULL, &d3d9_null_wined3d_parent_ops, &buffer);
2626 if (FAILED(hr))
2627 {
2628 ERR("(%p) wined3d_buffer_create_ib failed with hr = %08x.\n", device, hr);
2629 return hr;
2630 }
2631
2632 if (device->index_buffer)
2633 wined3d_buffer_decref(device->index_buffer);
2634
2635 device->index_buffer = buffer;
2636 device->index_buffer_size = size;
2637 device->index_buffer_pos = 0;
2638 }
2639 return D3D_OK;
2640 }
2641
2642 static HRESULT WINAPI d3d9_device_DrawIndexedPrimitiveUP(IDirect3DDevice9Ex *iface,
2643 D3DPRIMITIVETYPE primitive_type, UINT min_vertex_idx, UINT vertex_count,
2644 UINT primitive_count, const void *index_data, D3DFORMAT index_format,
2645 const void *vertex_data, UINT vertex_stride)
2646 {
2647 UINT idx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2648 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2649 UINT idx_fmt_size = index_format == D3DFMT_INDEX16 ? 2 : 4;
2650 UINT vtx_size = vertex_count * vertex_stride;
2651 UINT idx_size = idx_count * idx_fmt_size;
2652 struct wined3d_map_desc wined3d_map_desc;
2653 struct wined3d_box wined3d_box = {0};
2654 struct wined3d_resource *ib, *vb;
2655 UINT vb_pos, ib_pos, align;
2656 HRESULT hr;
2657
2658 TRACE("iface %p, primitive_type %#x, min_vertex_idx %u, vertex_count %u, primitive_count %u, "
2659 "index_data %p, index_format %#x, vertex_data %p, vertex_stride %u.\n",
2660 iface, primitive_type, min_vertex_idx, vertex_count, primitive_count,
2661 index_data, index_format, vertex_data, vertex_stride);
2662
2663 if (!primitive_count)
2664 {
2665 WARN("primitive_count is 0, returning D3D_OK.\n");
2666 return D3D_OK;
2667 }
2668
2669 wined3d_mutex_lock();
2670
2671 if (!device->has_vertex_declaration)
2672 {
2673 wined3d_mutex_unlock();
2674 WARN("Called without a valid vertex declaration set.\n");
2675 return D3DERR_INVALIDCALL;
2676 }
2677
2678 hr = d3d9_device_prepare_vertex_buffer(device, vtx_size);
2679 if (FAILED(hr))
2680 goto done;
2681
2682 vb_pos = device->vertex_buffer_pos;
2683 align = vb_pos % vertex_stride;
2684 if (align) align = vertex_stride - align;
2685 if (vb_pos + vtx_size + align > device->vertex_buffer_size)
2686 vb_pos = 0;
2687 else
2688 vb_pos += align;
2689
2690 wined3d_box.left = vb_pos;
2691 wined3d_box.right = vb_pos + vtx_size;
2692 vb = wined3d_buffer_get_resource(device->vertex_buffer);
2693 if (FAILED(hr = wined3d_resource_map(vb, 0, &wined3d_map_desc, &wined3d_box,
2694 vb_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD)))
2695 goto done;
2696 memcpy(wined3d_map_desc.data, (char *)vertex_data + min_vertex_idx * vertex_stride, vtx_size);
2697 wined3d_resource_unmap(vb, 0);
2698 device->vertex_buffer_pos = vb_pos + vtx_size;
2699
2700 hr = d3d9_device_prepare_index_buffer(device, idx_size);
2701 if (FAILED(hr))
2702 goto done;
2703
2704 ib_pos = device->index_buffer_pos;
2705 align = ib_pos % idx_fmt_size;
2706 if (align) align = idx_fmt_size - align;
2707 if (ib_pos + idx_size + align > device->index_buffer_size)
2708 ib_pos = 0;
2709 else
2710 ib_pos += align;
2711
2712 wined3d_box.left = ib_pos;
2713 wined3d_box.right = ib_pos + idx_size;
2714 ib = wined3d_buffer_get_resource(device->index_buffer);
2715 if (FAILED(hr = wined3d_resource_map(ib, 0, &wined3d_map_desc, &wined3d_box,
2716 ib_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD)))
2717 goto done;
2718 memcpy(wined3d_map_desc.data, index_data, idx_size);
2719 wined3d_resource_unmap(ib, 0);
2720 device->index_buffer_pos = ib_pos + idx_size;
2721
2722 hr = wined3d_device_set_stream_source(device->wined3d_device, 0, device->vertex_buffer, 0, vertex_stride);
2723 if (FAILED(hr))
2724 goto done;
2725
2726 wined3d_device_set_index_buffer(device->wined3d_device, device->index_buffer,
2727 wined3dformat_from_d3dformat(index_format), 0);
2728 wined3d_device_set_base_vertex_index(device->wined3d_device, vb_pos / vertex_stride - min_vertex_idx);
2729
2730 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type, 0);
2731 hr = wined3d_device_draw_indexed_primitive(device->wined3d_device, ib_pos / idx_fmt_size, idx_count);
2732
2733 wined3d_device_set_stream_source(device->wined3d_device, 0, NULL, 0, 0);
2734 wined3d_device_set_index_buffer(device->wined3d_device, NULL, WINED3DFMT_UNKNOWN, 0);
2735 wined3d_device_set_base_vertex_index(device->wined3d_device, 0);
2736
2737 done:
2738 wined3d_mutex_unlock();
2739 return hr;
2740 }
2741
2742 static HRESULT WINAPI d3d9_device_ProcessVertices(IDirect3DDevice9Ex *iface,
2743 UINT src_start_idx, UINT dst_idx, UINT vertex_count, IDirect3DVertexBuffer9 *dst_buffer,
2744 IDirect3DVertexDeclaration9 *declaration, DWORD flags)
2745 {
2746 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2747 struct d3d9_vertexbuffer *dst_impl = unsafe_impl_from_IDirect3DVertexBuffer9(dst_buffer);
2748 struct d3d9_vertex_declaration *decl_impl = unsafe_impl_from_IDirect3DVertexDeclaration9(declaration);
2749 HRESULT hr;
2750
2751 TRACE("iface %p, src_start_idx %u, dst_idx %u, vertex_count %u, dst_buffer %p, declaration %p, flags %#x.\n",
2752 iface, src_start_idx, dst_idx, vertex_count, dst_buffer, declaration, flags);
2753
2754 wined3d_mutex_lock();
2755 hr = wined3d_device_process_vertices(device->wined3d_device, src_start_idx, dst_idx, vertex_count,
2756 dst_impl->wined3d_buffer, decl_impl ? decl_impl->wined3d_declaration : NULL,
2757 flags, dst_impl->fvf);
2758 wined3d_mutex_unlock();
2759
2760 return hr;
2761 }
2762
2763 static HRESULT WINAPI d3d9_device_CreateVertexDeclaration(IDirect3DDevice9Ex *iface,
2764 const D3DVERTEXELEMENT9 *elements, IDirect3DVertexDeclaration9 **declaration)
2765 {
2766 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2767 struct d3d9_vertex_declaration *object;
2768 HRESULT hr;
2769
2770 TRACE("iface %p, elements %p, declaration %p.\n", iface, elements, declaration);
2771
2772 if (!declaration)
2773 {
2774 WARN("Caller passed a NULL declaration, returning D3DERR_INVALIDCALL.\n");
2775 return D3DERR_INVALIDCALL;
2776 }
2777
2778 if (SUCCEEDED(hr = d3d9_vertex_declaration_create(device, elements, &object)))
2779 *declaration = &object->IDirect3DVertexDeclaration9_iface;
2780
2781 return hr;
2782 }
2783
2784 static HRESULT WINAPI d3d9_device_SetVertexDeclaration(IDirect3DDevice9Ex *iface,
2785 IDirect3DVertexDeclaration9 *declaration)
2786 {
2787 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2788 struct d3d9_vertex_declaration *decl_impl = unsafe_impl_from_IDirect3DVertexDeclaration9(declaration);
2789
2790 TRACE("iface %p, declaration %p.\n", iface, declaration);
2791
2792 wined3d_mutex_lock();
2793 wined3d_device_set_vertex_declaration(device->wined3d_device,
2794 decl_impl ? decl_impl->wined3d_declaration : NULL);
2795 device->has_vertex_declaration = !!decl_impl;
2796 wined3d_mutex_unlock();
2797
2798 return D3D_OK;
2799 }
2800
2801 static HRESULT WINAPI d3d9_device_GetVertexDeclaration(IDirect3DDevice9Ex *iface,
2802 IDirect3DVertexDeclaration9 **declaration)
2803 {
2804 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2805 struct wined3d_vertex_declaration *wined3d_declaration;
2806 struct d3d9_vertex_declaration *declaration_impl;
2807
2808 TRACE("iface %p, declaration %p.\n", iface, declaration);
2809
2810 if (!declaration) return D3DERR_INVALIDCALL;
2811
2812 wined3d_mutex_lock();
2813 if ((wined3d_declaration = wined3d_device_get_vertex_declaration(device->wined3d_device)))
2814 {
2815 declaration_impl = wined3d_vertex_declaration_get_parent(wined3d_declaration);
2816 *declaration = &declaration_impl->IDirect3DVertexDeclaration9_iface;
2817 IDirect3DVertexDeclaration9_AddRef(*declaration);
2818 }
2819 else
2820 {
2821 *declaration = NULL;
2822 }
2823 wined3d_mutex_unlock();
2824
2825 TRACE("Returning %p.\n", *declaration);
2826 return D3D_OK;
2827 }
2828
2829 static struct wined3d_vertex_declaration *device_get_fvf_declaration(struct d3d9_device *device, DWORD fvf)
2830 {
2831 struct wined3d_vertex_declaration *wined3d_declaration;
2832 struct fvf_declaration *fvf_decls = device->fvf_decls;
2833 struct d3d9_vertex_declaration *d3d9_declaration;
2834 D3DVERTEXELEMENT9 *elements;
2835 int p, low, high; /* deliberately signed */
2836 HRESULT hr;
2837
2838 TRACE("Searching for declaration for fvf %08x... ", fvf);
2839
2840 low = 0;
2841 high = device->fvf_decl_count - 1;
2842 while (low <= high)
2843 {
2844 p = (low + high) >> 1;
2845 TRACE("%d ", p);
2846
2847 if (fvf_decls[p].fvf == fvf)
2848 {
2849 TRACE("found %p.\n", fvf_decls[p].decl);
2850 return fvf_decls[p].decl;
2851 }
2852
2853 if (fvf_decls[p].fvf < fvf)
2854 low = p + 1;
2855 else
2856 high = p - 1;
2857 }
2858 TRACE("not found. Creating and inserting at position %d.\n", low);
2859
2860 if (FAILED(hr = vdecl_convert_fvf(fvf, &elements)))
2861 return NULL;
2862
2863 hr = d3d9_vertex_declaration_create(device, elements, &d3d9_declaration);
2864 HeapFree(GetProcessHeap(), 0, elements);
2865 if (FAILED(hr))
2866 return NULL;
2867
2868 if (device->fvf_decl_size == device->fvf_decl_count)
2869 {
2870 UINT grow = max(device->fvf_decl_size / 2, 8);
2871
2872 fvf_decls = HeapReAlloc(GetProcessHeap(), 0, fvf_decls, sizeof(*fvf_decls) * (device->fvf_decl_size + grow));
2873 if (!fvf_decls)
2874 {
2875 IDirect3DVertexDeclaration9_Release(&d3d9_declaration->IDirect3DVertexDeclaration9_iface);
2876 return NULL;
2877 }
2878 device->fvf_decls = fvf_decls;
2879 device->fvf_decl_size += grow;
2880 }
2881
2882 d3d9_declaration->fvf = fvf;
2883 wined3d_declaration = d3d9_declaration->wined3d_declaration;
2884 wined3d_vertex_declaration_incref(wined3d_declaration);
2885 IDirect3DVertexDeclaration9_Release(&d3d9_declaration->IDirect3DVertexDeclaration9_iface);
2886
2887 memmove(fvf_decls + low + 1, fvf_decls + low, sizeof(*fvf_decls) * (device->fvf_decl_count - low));
2888 fvf_decls[low].decl = wined3d_declaration;
2889 fvf_decls[low].fvf = fvf;
2890 ++device->fvf_decl_count;
2891
2892 TRACE("Returning %p. %u declarations in array.\n", wined3d_declaration, device->fvf_decl_count);
2893
2894 return wined3d_declaration;
2895 }
2896
2897 static HRESULT WINAPI d3d9_device_SetFVF(IDirect3DDevice9Ex *iface, DWORD fvf)
2898 {
2899 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2900 struct wined3d_vertex_declaration *decl;
2901
2902 TRACE("iface %p, fvf %#x.\n", iface, fvf);
2903
2904 if (!fvf)
2905 {
2906 WARN("%#x is not a valid FVF.\n", fvf);
2907 return D3D_OK;
2908 }
2909
2910 wined3d_mutex_lock();
2911 if (!(decl = device_get_fvf_declaration(device, fvf)))
2912 {
2913 wined3d_mutex_unlock();
2914 ERR("Failed to create a vertex declaration for fvf %#x.\n", fvf);
2915 return D3DERR_DRIVERINTERNALERROR;
2916 }
2917
2918 wined3d_device_set_vertex_declaration(device->wined3d_device, decl);
2919 device->has_vertex_declaration = TRUE;
2920 wined3d_mutex_unlock();
2921
2922 return D3D_OK;
2923 }
2924
2925 static HRESULT WINAPI d3d9_device_GetFVF(IDirect3DDevice9Ex *iface, DWORD *fvf)
2926 {
2927 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2928 struct wined3d_vertex_declaration *wined3d_declaration;
2929 struct d3d9_vertex_declaration *d3d9_declaration;
2930
2931 TRACE("iface %p, fvf %p.\n", iface, fvf);
2932
2933 wined3d_mutex_lock();
2934 if ((wined3d_declaration = wined3d_device_get_vertex_declaration(device->wined3d_device)))
2935 {
2936 d3d9_declaration = wined3d_vertex_declaration_get_parent(wined3d_declaration);
2937 *fvf = d3d9_declaration->fvf;
2938 }
2939 else
2940 {
2941 *fvf = 0;
2942 }
2943 wined3d_mutex_unlock();
2944
2945 TRACE("Returning FVF %#x.\n", *fvf);
2946
2947 return D3D_OK;
2948 }
2949
2950 static HRESULT WINAPI d3d9_device_CreateVertexShader(IDirect3DDevice9Ex *iface,
2951 const DWORD *byte_code, IDirect3DVertexShader9 **shader)
2952 {
2953 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2954 struct d3d9_vertexshader *object;
2955 HRESULT hr;
2956
2957 TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
2958
2959 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2960 if (!object)
2961 return E_OUTOFMEMORY;
2962
2963 hr = vertexshader_init(object, device, byte_code);
2964 if (FAILED(hr))
2965 {
2966 WARN("Failed to initialize vertex shader, hr %#x.\n", hr);
2967 HeapFree(GetProcessHeap(), 0, object);
2968 return hr;
2969 }
2970
2971 TRACE("Created vertex shader %p.\n", object);
2972 *shader = &object->IDirect3DVertexShader9_iface;
2973
2974 return D3D_OK;
2975 }
2976
2977 static HRESULT WINAPI d3d9_device_SetVertexShader(IDirect3DDevice9Ex *iface, IDirect3DVertexShader9 *shader)
2978 {
2979 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2980 struct d3d9_vertexshader *shader_obj = unsafe_impl_from_IDirect3DVertexShader9(shader);
2981
2982 TRACE("iface %p, shader %p.\n", iface, shader);
2983
2984 wined3d_mutex_lock();
2985 wined3d_device_set_vertex_shader(device->wined3d_device,
2986 shader_obj ? shader_obj->wined3d_shader : NULL);
2987 wined3d_mutex_unlock();
2988
2989 return D3D_OK;
2990 }
2991
2992 static HRESULT WINAPI d3d9_device_GetVertexShader(IDirect3DDevice9Ex *iface, IDirect3DVertexShader9 **shader)
2993 {
2994 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
2995 struct d3d9_vertexshader *shader_impl;
2996 struct wined3d_shader *wined3d_shader;
2997
2998 TRACE("iface %p, shader %p.\n", iface, shader);
2999
3000 wined3d_mutex_lock();
3001 if ((wined3d_shader = wined3d_device_get_vertex_shader(device->wined3d_device)))
3002 {
3003 shader_impl = wined3d_shader_get_parent(wined3d_shader);
3004 *shader = &shader_impl->IDirect3DVertexShader9_iface;
3005 IDirect3DVertexShader9_AddRef(*shader);
3006 }
3007 else
3008 {
3009 *shader = NULL;
3010 }
3011 wined3d_mutex_unlock();
3012
3013 TRACE("Returning %p.\n", *shader);
3014
3015 return D3D_OK;
3016 }
3017
3018 static HRESULT WINAPI d3d9_device_SetVertexShaderConstantF(IDirect3DDevice9Ex *iface,
3019 UINT reg_idx, const float *data, UINT count)
3020 {
3021 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3022 HRESULT hr;
3023
3024 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3025
3026 if (reg_idx + count > D3D9_MAX_VERTEX_SHADER_CONSTANTF)
3027 {
3028 WARN("Trying to access %u constants, but d3d9 only supports %u\n",
3029 reg_idx + count, D3D9_MAX_VERTEX_SHADER_CONSTANTF);
3030 return D3DERR_INVALIDCALL;
3031 }
3032
3033 wined3d_mutex_lock();
3034 hr = wined3d_device_set_vs_consts_f(device->wined3d_device,
3035 reg_idx, count, (const struct wined3d_vec4 *)data);
3036 wined3d_mutex_unlock();
3037
3038 return hr;
3039 }
3040
3041 static HRESULT WINAPI d3d9_device_GetVertexShaderConstantF(IDirect3DDevice9Ex *iface,
3042 UINT reg_idx, float *data, UINT count)
3043 {
3044 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3045 HRESULT hr;
3046
3047 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3048
3049 if (reg_idx + count > D3D9_MAX_VERTEX_SHADER_CONSTANTF)
3050 {
3051 WARN("Trying to access %u constants, but d3d9 only supports %u\n",
3052 reg_idx + count, D3D9_MAX_VERTEX_SHADER_CONSTANTF);
3053 return D3DERR_INVALIDCALL;
3054 }
3055
3056 wined3d_mutex_lock();
3057 hr = wined3d_device_get_vs_consts_f(device->wined3d_device,
3058 reg_idx, count, (struct wined3d_vec4 *)data);
3059 wined3d_mutex_unlock();
3060
3061 return hr;
3062 }
3063
3064 static HRESULT WINAPI d3d9_device_SetVertexShaderConstantI(IDirect3DDevice9Ex *iface,
3065 UINT reg_idx, const int *data, UINT count)
3066 {
3067 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3068 HRESULT hr;
3069
3070 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3071
3072 wined3d_mutex_lock();
3073 hr = wined3d_device_set_vs_consts_i(device->wined3d_device,
3074 reg_idx, count, (const struct wined3d_ivec4 *)data);
3075 wined3d_mutex_unlock();
3076
3077 return hr;
3078 }
3079
3080 static HRESULT WINAPI d3d9_device_GetVertexShaderConstantI(IDirect3DDevice9Ex *iface,
3081 UINT reg_idx, int *data, UINT count)
3082 {
3083 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
3084 HRESULT hr;
3085
3086 TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
3087
3088 wined3d_mutex_lock();
3089 hr = wined3d_device_get_vs_consts_i(device->wined3d_device,
3090 reg_idx, count, (struct wined3d_ivec4 *)data);
3091 wined3d_mutex_unlock();
3092
3093 return hr;
3094 }
3095
3096 static HRESULT WINAPI d3d9_device_SetVertexShaderConstantB(IDirect3DDevice9Ex *iface,
3097 UINT reg_idx, const BOOL *data, UINT count)
3098 {