wined3d: Validate that the view resource has depth/stencil bind flags in wined3d_devi...
[wine.git] / dlls / d3d8 / device.c
blob67996a78e703143bb3a30f4831570f56928594f0
1 /*
2 * IDirect3DDevice8 implementation
4 * Copyright 2002-2004 Jason Edmeades
5 * Copyright 2004 Christian Costa
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "config.h"
24 #include <math.h>
25 #include <stdarg.h>
27 #include "windef.h"
28 #include "winbase.h"
29 #include "winuser.h"
30 #include "wingdi.h"
31 #include "wine/debug.h"
33 #include "d3d8_private.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(d3d8);
37 static void STDMETHODCALLTYPE d3d8_null_wined3d_object_destroyed(void *parent) {}
39 static const struct wined3d_parent_ops d3d8_null_wined3d_parent_ops =
41 d3d8_null_wined3d_object_destroyed,
44 D3DFORMAT d3dformat_from_wined3dformat(enum wined3d_format_id format)
46 BYTE *c = (BYTE *)&format;
48 /* Don't translate FOURCC formats */
49 if (isprint(c[0]) && isprint(c[1]) && isprint(c[2]) && isprint(c[3])) return format;
51 switch(format)
53 case WINED3DFMT_UNKNOWN: return D3DFMT_UNKNOWN;
54 case WINED3DFMT_B8G8R8_UNORM: return D3DFMT_R8G8B8;
55 case WINED3DFMT_B8G8R8A8_UNORM: return D3DFMT_A8R8G8B8;
56 case WINED3DFMT_B8G8R8X8_UNORM: return D3DFMT_X8R8G8B8;
57 case WINED3DFMT_B5G6R5_UNORM: return D3DFMT_R5G6B5;
58 case WINED3DFMT_B5G5R5X1_UNORM: return D3DFMT_X1R5G5B5;
59 case WINED3DFMT_B5G5R5A1_UNORM: return D3DFMT_A1R5G5B5;
60 case WINED3DFMT_B4G4R4A4_UNORM: return D3DFMT_A4R4G4B4;
61 case WINED3DFMT_B2G3R3_UNORM: return D3DFMT_R3G3B2;
62 case WINED3DFMT_A8_UNORM: return D3DFMT_A8;
63 case WINED3DFMT_B2G3R3A8_UNORM: return D3DFMT_A8R3G3B2;
64 case WINED3DFMT_B4G4R4X4_UNORM: return D3DFMT_X4R4G4B4;
65 case WINED3DFMT_R10G10B10A2_UNORM: return D3DFMT_A2B10G10R10;
66 case WINED3DFMT_R16G16_UNORM: return D3DFMT_G16R16;
67 case WINED3DFMT_P8_UINT_A8_UNORM: return D3DFMT_A8P8;
68 case WINED3DFMT_P8_UINT: return D3DFMT_P8;
69 case WINED3DFMT_L8_UNORM: return D3DFMT_L8;
70 case WINED3DFMT_L8A8_UNORM: return D3DFMT_A8L8;
71 case WINED3DFMT_L4A4_UNORM: return D3DFMT_A4L4;
72 case WINED3DFMT_R8G8_SNORM: return D3DFMT_V8U8;
73 case WINED3DFMT_R5G5_SNORM_L6_UNORM: return D3DFMT_L6V5U5;
74 case WINED3DFMT_R8G8_SNORM_L8X8_UNORM: return D3DFMT_X8L8V8U8;
75 case WINED3DFMT_R8G8B8A8_SNORM: return D3DFMT_Q8W8V8U8;
76 case WINED3DFMT_R16G16_SNORM: return D3DFMT_V16U16;
77 case WINED3DFMT_R10G11B11_SNORM: return D3DFMT_W11V11U10;
78 case WINED3DFMT_R10G10B10_SNORM_A2_UNORM: return D3DFMT_A2W10V10U10;
79 case WINED3DFMT_D16_LOCKABLE: return D3DFMT_D16_LOCKABLE;
80 case WINED3DFMT_D32_UNORM: return D3DFMT_D32;
81 case WINED3DFMT_S1_UINT_D15_UNORM: return D3DFMT_D15S1;
82 case WINED3DFMT_D24_UNORM_S8_UINT: return D3DFMT_D24S8;
83 case WINED3DFMT_X8D24_UNORM: return D3DFMT_D24X8;
84 case WINED3DFMT_S4X4_UINT_D24_UNORM: return D3DFMT_D24X4S4;
85 case WINED3DFMT_D16_UNORM: return D3DFMT_D16;
86 case WINED3DFMT_R16_UINT: return D3DFMT_INDEX16;
87 case WINED3DFMT_R32_UINT: return D3DFMT_INDEX32;
88 default:
89 FIXME("Unhandled wined3d format %#x.\n", format);
90 return D3DFMT_UNKNOWN;
94 enum wined3d_format_id wined3dformat_from_d3dformat(D3DFORMAT format)
96 BYTE *c = (BYTE *)&format;
98 /* Don't translate FOURCC formats */
99 if (isprint(c[0]) && isprint(c[1]) && isprint(c[2]) && isprint(c[3])) return format;
101 switch(format)
103 case D3DFMT_UNKNOWN: return WINED3DFMT_UNKNOWN;
104 case D3DFMT_R8G8B8: return WINED3DFMT_B8G8R8_UNORM;
105 case D3DFMT_A8R8G8B8: return WINED3DFMT_B8G8R8A8_UNORM;
106 case D3DFMT_X8R8G8B8: return WINED3DFMT_B8G8R8X8_UNORM;
107 case D3DFMT_R5G6B5: return WINED3DFMT_B5G6R5_UNORM;
108 case D3DFMT_X1R5G5B5: return WINED3DFMT_B5G5R5X1_UNORM;
109 case D3DFMT_A1R5G5B5: return WINED3DFMT_B5G5R5A1_UNORM;
110 case D3DFMT_A4R4G4B4: return WINED3DFMT_B4G4R4A4_UNORM;
111 case D3DFMT_R3G3B2: return WINED3DFMT_B2G3R3_UNORM;
112 case D3DFMT_A8: return WINED3DFMT_A8_UNORM;
113 case D3DFMT_A8R3G3B2: return WINED3DFMT_B2G3R3A8_UNORM;
114 case D3DFMT_X4R4G4B4: return WINED3DFMT_B4G4R4X4_UNORM;
115 case D3DFMT_A2B10G10R10: return WINED3DFMT_R10G10B10A2_UNORM;
116 case D3DFMT_G16R16: return WINED3DFMT_R16G16_UNORM;
117 case D3DFMT_A8P8: return WINED3DFMT_P8_UINT_A8_UNORM;
118 case D3DFMT_P8: return WINED3DFMT_P8_UINT;
119 case D3DFMT_L8: return WINED3DFMT_L8_UNORM;
120 case D3DFMT_A8L8: return WINED3DFMT_L8A8_UNORM;
121 case D3DFMT_A4L4: return WINED3DFMT_L4A4_UNORM;
122 case D3DFMT_V8U8: return WINED3DFMT_R8G8_SNORM;
123 case D3DFMT_L6V5U5: return WINED3DFMT_R5G5_SNORM_L6_UNORM;
124 case D3DFMT_X8L8V8U8: return WINED3DFMT_R8G8_SNORM_L8X8_UNORM;
125 case D3DFMT_Q8W8V8U8: return WINED3DFMT_R8G8B8A8_SNORM;
126 case D3DFMT_V16U16: return WINED3DFMT_R16G16_SNORM;
127 case D3DFMT_W11V11U10: return WINED3DFMT_R10G11B11_SNORM;
128 case D3DFMT_A2W10V10U10: return WINED3DFMT_R10G10B10_SNORM_A2_UNORM;
129 case D3DFMT_D16_LOCKABLE: return WINED3DFMT_D16_LOCKABLE;
130 case D3DFMT_D32: return WINED3DFMT_D32_UNORM;
131 case D3DFMT_D15S1: return WINED3DFMT_S1_UINT_D15_UNORM;
132 case D3DFMT_D24S8: return WINED3DFMT_D24_UNORM_S8_UINT;
133 case D3DFMT_D24X8: return WINED3DFMT_X8D24_UNORM;
134 case D3DFMT_D24X4S4: return WINED3DFMT_S4X4_UINT_D24_UNORM;
135 case D3DFMT_D16: return WINED3DFMT_D16_UNORM;
136 case D3DFMT_INDEX16: return WINED3DFMT_R16_UINT;
137 case D3DFMT_INDEX32: return WINED3DFMT_R32_UINT;
138 default:
139 FIXME("Unhandled D3DFORMAT %#x.\n", format);
140 return WINED3DFMT_UNKNOWN;
144 unsigned int wined3dmapflags_from_d3dmapflags(unsigned int flags)
146 static const unsigned int handled = D3DLOCK_NOSYSLOCK
147 | D3DLOCK_NOOVERWRITE
148 | D3DLOCK_DISCARD
149 | D3DLOCK_NO_DIRTY_UPDATE;
150 unsigned int wined3d_flags;
152 wined3d_flags = flags & handled;
153 if (!(flags & (D3DLOCK_NOOVERWRITE | D3DLOCK_DISCARD)))
154 wined3d_flags |= WINED3D_MAP_READ;
155 if (!(flags & D3DLOCK_READONLY))
156 wined3d_flags |= WINED3D_MAP_WRITE;
157 if (!(wined3d_flags & (WINED3D_MAP_READ | WINED3D_MAP_WRITE)))
158 wined3d_flags |= WINED3D_MAP_READ | WINED3D_MAP_WRITE;
159 flags &= ~(handled | D3DLOCK_READONLY);
161 if (flags)
162 FIXME("Unhandled flags %#x.\n", flags);
164 return wined3d_flags;
167 static UINT vertex_count_from_primitive_count(D3DPRIMITIVETYPE primitive_type, UINT primitive_count)
169 switch (primitive_type)
171 case D3DPT_POINTLIST:
172 return primitive_count;
174 case D3DPT_LINELIST:
175 return primitive_count * 2;
177 case D3DPT_LINESTRIP:
178 return primitive_count + 1;
180 case D3DPT_TRIANGLELIST:
181 return primitive_count * 3;
183 case D3DPT_TRIANGLESTRIP:
184 case D3DPT_TRIANGLEFAN:
185 return primitive_count + 2;
187 default:
188 FIXME("Unhandled primitive type %#x.\n", primitive_type);
189 return 0;
193 static D3DSWAPEFFECT d3dswapeffect_from_wined3dswapeffect(enum wined3d_swap_effect effect)
195 switch (effect)
197 case WINED3D_SWAP_EFFECT_DISCARD:
198 return D3DSWAPEFFECT_DISCARD;
199 case WINED3D_SWAP_EFFECT_SEQUENTIAL:
200 return D3DSWAPEFFECT_FLIP;
201 case WINED3D_SWAP_EFFECT_COPY:
202 return D3DSWAPEFFECT_COPY;
203 case WINED3D_SWAP_EFFECT_COPY_VSYNC:
204 return D3DSWAPEFFECT_COPY_VSYNC;
205 default:
206 FIXME("Unhandled swap effect %#x.\n", effect);
207 return D3DSWAPEFFECT_FLIP;
211 static void present_parameters_from_wined3d_swapchain_desc(D3DPRESENT_PARAMETERS *present_parameters,
212 const struct wined3d_swapchain_desc *swapchain_desc, DWORD presentation_interval)
214 present_parameters->BackBufferWidth = swapchain_desc->backbuffer_width;
215 present_parameters->BackBufferHeight = swapchain_desc->backbuffer_height;
216 present_parameters->BackBufferFormat = d3dformat_from_wined3dformat(swapchain_desc->backbuffer_format);
217 present_parameters->BackBufferCount = swapchain_desc->backbuffer_count;
218 present_parameters->MultiSampleType = swapchain_desc->multisample_type;
219 present_parameters->SwapEffect = d3dswapeffect_from_wined3dswapeffect(swapchain_desc->swap_effect);
220 present_parameters->hDeviceWindow = swapchain_desc->device_window;
221 present_parameters->Windowed = swapchain_desc->windowed;
222 present_parameters->EnableAutoDepthStencil = swapchain_desc->enable_auto_depth_stencil;
223 present_parameters->AutoDepthStencilFormat
224 = d3dformat_from_wined3dformat(swapchain_desc->auto_depth_stencil_format);
225 present_parameters->Flags = swapchain_desc->flags & D3DPRESENTFLAGS_MASK;
226 present_parameters->FullScreen_RefreshRateInHz = swapchain_desc->refresh_rate;
227 present_parameters->FullScreen_PresentationInterval = presentation_interval;
230 static enum wined3d_swap_effect wined3dswapeffect_from_d3dswapeffect(D3DSWAPEFFECT effect)
232 switch (effect)
234 case D3DSWAPEFFECT_DISCARD:
235 return WINED3D_SWAP_EFFECT_DISCARD;
236 case D3DSWAPEFFECT_FLIP:
237 return WINED3D_SWAP_EFFECT_SEQUENTIAL;
238 case D3DSWAPEFFECT_COPY:
239 return WINED3D_SWAP_EFFECT_COPY;
240 case D3DSWAPEFFECT_COPY_VSYNC:
241 return WINED3D_SWAP_EFFECT_COPY_VSYNC;
242 default:
243 FIXME("Unhandled swap effect %#x.\n", effect);
244 return WINED3D_SWAP_EFFECT_SEQUENTIAL;
248 static enum wined3d_swap_interval wined3dswapinterval_from_d3d(DWORD interval)
250 switch (interval)
252 case D3DPRESENT_INTERVAL_IMMEDIATE:
253 return WINED3D_SWAP_INTERVAL_IMMEDIATE;
254 case D3DPRESENT_INTERVAL_ONE:
255 return WINED3D_SWAP_INTERVAL_ONE;
256 case D3DPRESENT_INTERVAL_TWO:
257 return WINED3D_SWAP_INTERVAL_TWO;
258 case D3DPRESENT_INTERVAL_THREE:
259 return WINED3D_SWAP_INTERVAL_THREE;
260 case D3DPRESENT_INTERVAL_FOUR:
261 return WINED3D_SWAP_INTERVAL_FOUR;
262 default:
263 FIXME("Unhandled presentation interval %#x.\n", interval);
264 case D3DPRESENT_INTERVAL_DEFAULT:
265 return WINED3D_SWAP_INTERVAL_DEFAULT;
269 static BOOL wined3d_swapchain_desc_from_present_parameters(struct wined3d_swapchain_desc *swapchain_desc,
270 const D3DPRESENT_PARAMETERS *present_parameters)
272 if (!present_parameters->SwapEffect || present_parameters->SwapEffect > D3DSWAPEFFECT_COPY_VSYNC)
274 WARN("Invalid swap effect %u passed.\n", present_parameters->SwapEffect);
275 return FALSE;
277 if (present_parameters->BackBufferCount > 3
278 || ((present_parameters->SwapEffect == D3DSWAPEFFECT_COPY
279 || present_parameters->SwapEffect == D3DSWAPEFFECT_COPY_VSYNC)
280 && present_parameters->BackBufferCount > 1))
282 WARN("Invalid backbuffer count %u.\n", present_parameters->BackBufferCount);
283 return FALSE;
285 switch (present_parameters->FullScreen_PresentationInterval)
287 case D3DPRESENT_INTERVAL_DEFAULT:
288 case D3DPRESENT_INTERVAL_ONE:
289 case D3DPRESENT_INTERVAL_TWO:
290 case D3DPRESENT_INTERVAL_THREE:
291 case D3DPRESENT_INTERVAL_FOUR:
292 case D3DPRESENT_INTERVAL_IMMEDIATE:
293 break;
294 default:
295 WARN("Invalid presentation interval %#x.\n",
296 present_parameters->FullScreen_PresentationInterval);
297 return FALSE;
300 swapchain_desc->backbuffer_width = present_parameters->BackBufferWidth;
301 swapchain_desc->backbuffer_height = present_parameters->BackBufferHeight;
302 swapchain_desc->backbuffer_format = wined3dformat_from_d3dformat(present_parameters->BackBufferFormat);
303 swapchain_desc->backbuffer_count = max(1, present_parameters->BackBufferCount);
304 swapchain_desc->backbuffer_bind_flags = WINED3D_BIND_RENDER_TARGET;
305 swapchain_desc->multisample_type = present_parameters->MultiSampleType;
306 swapchain_desc->multisample_quality = 0; /* d3d9 only */
307 swapchain_desc->swap_effect = wined3dswapeffect_from_d3dswapeffect(present_parameters->SwapEffect);
308 swapchain_desc->device_window = present_parameters->hDeviceWindow;
309 swapchain_desc->windowed = present_parameters->Windowed;
310 swapchain_desc->enable_auto_depth_stencil = present_parameters->EnableAutoDepthStencil;
311 swapchain_desc->auto_depth_stencil_format
312 = wined3dformat_from_d3dformat(present_parameters->AutoDepthStencilFormat);
313 swapchain_desc->flags
314 = (present_parameters->Flags & D3DPRESENTFLAGS_MASK) | WINED3D_SWAPCHAIN_ALLOW_MODE_SWITCH;
315 swapchain_desc->refresh_rate = present_parameters->FullScreen_RefreshRateInHz;
316 swapchain_desc->auto_restore_display_mode = TRUE;
318 if (present_parameters->Flags & ~D3DPRESENTFLAGS_MASK)
319 FIXME("Unhandled flags %#x.\n", present_parameters->Flags & ~D3DPRESENTFLAGS_MASK);
321 return TRUE;
324 void d3dcaps_from_wined3dcaps(D3DCAPS8 *caps, const struct wined3d_caps *wined3d_caps)
326 caps->DeviceType = (D3DDEVTYPE)wined3d_caps->DeviceType;
327 caps->AdapterOrdinal = wined3d_caps->AdapterOrdinal;
328 caps->Caps = wined3d_caps->Caps;
329 caps->Caps2 = wined3d_caps->Caps2;
330 caps->Caps3 = wined3d_caps->Caps3;
331 caps->PresentationIntervals = D3DPRESENT_INTERVAL_IMMEDIATE | D3DPRESENT_INTERVAL_ONE;
332 caps->CursorCaps = wined3d_caps->CursorCaps;
333 caps->DevCaps = wined3d_caps->DevCaps;
334 caps->PrimitiveMiscCaps = wined3d_caps->PrimitiveMiscCaps;
335 caps->RasterCaps = wined3d_caps->RasterCaps;
336 caps->ZCmpCaps = wined3d_caps->ZCmpCaps;
337 caps->SrcBlendCaps = wined3d_caps->SrcBlendCaps;
338 caps->DestBlendCaps = wined3d_caps->DestBlendCaps;
339 caps->AlphaCmpCaps = wined3d_caps->AlphaCmpCaps;
340 caps->ShadeCaps = wined3d_caps->ShadeCaps;
341 caps->TextureCaps = wined3d_caps->TextureCaps;
342 caps->TextureFilterCaps = wined3d_caps->TextureFilterCaps;
343 caps->CubeTextureFilterCaps = wined3d_caps->CubeTextureFilterCaps;
344 caps->VolumeTextureFilterCaps = wined3d_caps->VolumeTextureFilterCaps;
345 caps->TextureAddressCaps = wined3d_caps->TextureAddressCaps;
346 caps->VolumeTextureAddressCaps = wined3d_caps->VolumeTextureAddressCaps;
347 caps->LineCaps = wined3d_caps->LineCaps;
348 caps->MaxTextureWidth = wined3d_caps->MaxTextureWidth;
349 caps->MaxTextureHeight = wined3d_caps->MaxTextureHeight;
350 caps->MaxVolumeExtent = wined3d_caps->MaxVolumeExtent;
351 caps->MaxTextureRepeat = wined3d_caps->MaxTextureRepeat;
352 caps->MaxTextureAspectRatio = wined3d_caps->MaxTextureAspectRatio;
353 caps->MaxAnisotropy = wined3d_caps->MaxAnisotropy;
354 caps->MaxVertexW = wined3d_caps->MaxVertexW;
355 caps->GuardBandLeft = wined3d_caps->GuardBandLeft;
356 caps->GuardBandTop = wined3d_caps->GuardBandTop;
357 caps->GuardBandRight = wined3d_caps->GuardBandRight;
358 caps->GuardBandBottom = wined3d_caps->GuardBandBottom;
359 caps->ExtentsAdjust = wined3d_caps->ExtentsAdjust;
360 caps->StencilCaps = wined3d_caps->StencilCaps;
361 caps->FVFCaps = wined3d_caps->FVFCaps;
362 caps->TextureOpCaps = wined3d_caps->TextureOpCaps;
363 caps->MaxTextureBlendStages = wined3d_caps->MaxTextureBlendStages;
364 caps->MaxSimultaneousTextures = wined3d_caps->MaxSimultaneousTextures;
365 caps->VertexProcessingCaps = wined3d_caps->VertexProcessingCaps;
366 caps->MaxActiveLights = wined3d_caps->MaxActiveLights;
367 caps->MaxUserClipPlanes = wined3d_caps->MaxUserClipPlanes;
368 caps->MaxVertexBlendMatrices = wined3d_caps->MaxVertexBlendMatrices;
369 caps->MaxVertexBlendMatrixIndex = wined3d_caps->MaxVertexBlendMatrixIndex;
370 caps->MaxPointSize = wined3d_caps->MaxPointSize;
371 caps->MaxPrimitiveCount = wined3d_caps->MaxPrimitiveCount;
372 caps->MaxVertexIndex = wined3d_caps->MaxVertexIndex;
373 caps->MaxStreams = wined3d_caps->MaxStreams;
374 caps->MaxStreamStride = wined3d_caps->MaxStreamStride;
375 caps->VertexShaderVersion = wined3d_caps->VertexShaderVersion;
376 caps->MaxVertexShaderConst = wined3d_caps->MaxVertexShaderConst;
377 caps->PixelShaderVersion = wined3d_caps->PixelShaderVersion;
378 caps->MaxPixelShaderValue = wined3d_caps->PixelShader1xMaxValue;
380 caps->Caps2 &= D3DCAPS2_CANCALIBRATEGAMMA | D3DCAPS2_CANRENDERWINDOWED
381 | D3DCAPS2_CANMANAGERESOURCE | D3DCAPS2_DYNAMICTEXTURES | D3DCAPS2_FULLSCREENGAMMA
382 | D3DCAPS2_NO2DDURING3DSCENE | D3DCAPS2_RESERVED;
383 caps->Caps3 &= D3DCAPS3_ALPHA_FULLSCREEN_FLIP_OR_DISCARD | D3DCAPS3_RESERVED;
384 caps->PrimitiveMiscCaps &= D3DPMISCCAPS_MASKZ | D3DPMISCCAPS_LINEPATTERNREP
385 | D3DPMISCCAPS_CULLNONE | D3DPMISCCAPS_CULLCW | D3DPMISCCAPS_CULLCCW
386 | D3DPMISCCAPS_COLORWRITEENABLE | D3DPMISCCAPS_CLIPPLANESCALEDPOINTS
387 | D3DPMISCCAPS_CLIPTLVERTS | D3DPMISCCAPS_TSSARGTEMP | D3DPMISCCAPS_BLENDOP
388 | D3DPMISCCAPS_NULLREFERENCE;
389 caps->RasterCaps &= D3DPRASTERCAPS_DITHER | D3DPRASTERCAPS_PAT | D3DPRASTERCAPS_ZTEST
390 | D3DPRASTERCAPS_FOGVERTEX | D3DPRASTERCAPS_FOGTABLE | D3DPRASTERCAPS_ANTIALIASEDGES
391 | D3DPRASTERCAPS_MIPMAPLODBIAS | D3DPRASTERCAPS_ZBIAS | D3DPRASTERCAPS_ZBUFFERLESSHSR
392 | D3DPRASTERCAPS_FOGRANGE | D3DPRASTERCAPS_ANISOTROPY | D3DPRASTERCAPS_WBUFFER
393 | D3DPRASTERCAPS_WFOG | D3DPRASTERCAPS_ZFOG | D3DPRASTERCAPS_COLORPERSPECTIVE
394 | D3DPRASTERCAPS_STRETCHBLTMULTISAMPLE;
395 caps->SrcBlendCaps &= D3DPBLENDCAPS_ZERO | D3DPBLENDCAPS_ONE | D3DPBLENDCAPS_SRCCOLOR
396 | D3DPBLENDCAPS_INVSRCCOLOR | D3DPBLENDCAPS_SRCALPHA | D3DPBLENDCAPS_INVSRCALPHA
397 | D3DPBLENDCAPS_DESTALPHA | D3DPBLENDCAPS_INVDESTALPHA | D3DPBLENDCAPS_DESTCOLOR
398 | D3DPBLENDCAPS_INVDESTCOLOR | D3DPBLENDCAPS_SRCALPHASAT | D3DPBLENDCAPS_BOTHSRCALPHA
399 | D3DPBLENDCAPS_BOTHINVSRCALPHA;
400 caps->DestBlendCaps &= D3DPBLENDCAPS_ZERO | D3DPBLENDCAPS_ONE | D3DPBLENDCAPS_SRCCOLOR
401 | D3DPBLENDCAPS_INVSRCCOLOR | D3DPBLENDCAPS_SRCALPHA | D3DPBLENDCAPS_INVSRCALPHA
402 | D3DPBLENDCAPS_DESTALPHA | D3DPBLENDCAPS_INVDESTALPHA | D3DPBLENDCAPS_DESTCOLOR
403 | D3DPBLENDCAPS_INVDESTCOLOR | D3DPBLENDCAPS_SRCALPHASAT | D3DPBLENDCAPS_BOTHSRCALPHA
404 | D3DPBLENDCAPS_BOTHINVSRCALPHA;
405 caps->TextureCaps &= D3DPTEXTURECAPS_PERSPECTIVE | D3DPTEXTURECAPS_POW2 | D3DPTEXTURECAPS_ALPHA
406 | D3DPTEXTURECAPS_SQUAREONLY | D3DPTEXTURECAPS_TEXREPEATNOTSCALEDBYSIZE
407 | D3DPTEXTURECAPS_ALPHAPALETTE | D3DPTEXTURECAPS_NONPOW2CONDITIONAL
408 | D3DPTEXTURECAPS_PROJECTED | D3DPTEXTURECAPS_CUBEMAP | D3DPTEXTURECAPS_VOLUMEMAP
409 | D3DPTEXTURECAPS_MIPMAP | D3DPTEXTURECAPS_MIPVOLUMEMAP | D3DPTEXTURECAPS_MIPCUBEMAP
410 | D3DPTEXTURECAPS_CUBEMAP_POW2 | D3DPTEXTURECAPS_VOLUMEMAP_POW2;
411 caps->TextureFilterCaps &= D3DPTFILTERCAPS_MINFPOINT | D3DPTFILTERCAPS_MINFLINEAR
412 | D3DPTFILTERCAPS_MINFANISOTROPIC | D3DPTFILTERCAPS_MIPFPOINT
413 | D3DPTFILTERCAPS_MIPFLINEAR | D3DPTFILTERCAPS_MAGFPOINT | D3DPTFILTERCAPS_MAGFLINEAR
414 | D3DPTFILTERCAPS_MAGFANISOTROPIC | D3DPTFILTERCAPS_MAGFAFLATCUBIC
415 | D3DPTFILTERCAPS_MAGFGAUSSIANCUBIC;
416 caps->CubeTextureFilterCaps &= D3DPTFILTERCAPS_MINFPOINT | D3DPTFILTERCAPS_MINFLINEAR
417 | D3DPTFILTERCAPS_MINFANISOTROPIC | D3DPTFILTERCAPS_MIPFPOINT
418 | D3DPTFILTERCAPS_MIPFLINEAR | D3DPTFILTERCAPS_MAGFPOINT | D3DPTFILTERCAPS_MAGFLINEAR
419 | D3DPTFILTERCAPS_MAGFANISOTROPIC | D3DPTFILTERCAPS_MAGFAFLATCUBIC
420 | D3DPTFILTERCAPS_MAGFGAUSSIANCUBIC;
421 caps->VolumeTextureFilterCaps &= D3DPTFILTERCAPS_MINFPOINT | D3DPTFILTERCAPS_MINFLINEAR
422 | D3DPTFILTERCAPS_MINFANISOTROPIC | D3DPTFILTERCAPS_MIPFPOINT
423 | D3DPTFILTERCAPS_MIPFLINEAR | D3DPTFILTERCAPS_MAGFPOINT | D3DPTFILTERCAPS_MAGFLINEAR
424 | D3DPTFILTERCAPS_MAGFANISOTROPIC | D3DPTFILTERCAPS_MAGFAFLATCUBIC
425 | D3DPTFILTERCAPS_MAGFGAUSSIANCUBIC;
426 caps->StencilCaps &= ~WINED3DSTENCILCAPS_TWOSIDED;
427 caps->VertexProcessingCaps &= D3DVTXPCAPS_TEXGEN | D3DVTXPCAPS_MATERIALSOURCE7
428 | D3DVTXPCAPS_DIRECTIONALLIGHTS | D3DVTXPCAPS_POSITIONALLIGHTS | D3DVTXPCAPS_LOCALVIEWER
429 | D3DVTXPCAPS_TWEENING | D3DVTXPCAPS_NO_VSDT_UBYTE4;
431 /* D3D8 doesn't support SM 2.0 or higher, so clamp to 1.x */
432 if (caps->PixelShaderVersion)
433 caps->PixelShaderVersion = D3DPS_VERSION(1, 4);
434 else
435 caps->PixelShaderVersion = D3DPS_VERSION(0, 0);
436 if (caps->VertexShaderVersion)
437 caps->VertexShaderVersion = D3DVS_VERSION(1, 1);
438 else
439 caps->VertexShaderVersion = D3DVS_VERSION(0, 0);
440 caps->MaxVertexShaderConst = min(D3D8_MAX_VERTEX_SHADER_CONSTANTF, caps->MaxVertexShaderConst);
443 /* Handle table functions */
444 static DWORD d3d8_allocate_handle(struct d3d8_handle_table *t, void *object, enum d3d8_handle_type type)
446 struct d3d8_handle_entry *entry;
448 if (t->free_entries)
450 DWORD index = t->free_entries - t->entries;
451 /* Use a free handle */
452 entry = t->free_entries;
453 if (entry->type != D3D8_HANDLE_FREE)
455 ERR("Handle %u(%p) is in the free list, but has type %#x.\n", index, entry, entry->type);
456 return D3D8_INVALID_HANDLE;
458 t->free_entries = entry->object;
459 entry->object = object;
460 entry->type = type;
462 return index;
465 if (!(t->entry_count < t->table_size))
467 /* Grow the table */
468 UINT new_size = t->table_size + (t->table_size >> 1);
469 struct d3d8_handle_entry *new_entries;
471 if (!(new_entries = heap_realloc(t->entries, new_size * sizeof(*t->entries))))
473 ERR("Failed to grow the handle table.\n");
474 return D3D8_INVALID_HANDLE;
476 t->entries = new_entries;
477 t->table_size = new_size;
480 entry = &t->entries[t->entry_count];
481 entry->object = object;
482 entry->type = type;
484 return t->entry_count++;
487 static void *d3d8_free_handle(struct d3d8_handle_table *t, DWORD handle, enum d3d8_handle_type type)
489 struct d3d8_handle_entry *entry;
490 void *object;
492 if (handle == D3D8_INVALID_HANDLE || handle >= t->entry_count)
494 WARN("Invalid handle %u passed.\n", handle);
495 return NULL;
498 entry = &t->entries[handle];
499 if (entry->type != type)
501 WARN("Handle %u(%p) is not of type %#x.\n", handle, entry, type);
502 return NULL;
505 object = entry->object;
506 entry->object = t->free_entries;
507 entry->type = D3D8_HANDLE_FREE;
508 t->free_entries = entry;
510 return object;
513 static void *d3d8_get_object(struct d3d8_handle_table *t, DWORD handle, enum d3d8_handle_type type)
515 struct d3d8_handle_entry *entry;
517 if (handle == D3D8_INVALID_HANDLE || handle >= t->entry_count)
519 WARN("Invalid handle %u passed.\n", handle);
520 return NULL;
523 entry = &t->entries[handle];
524 if (entry->type != type)
526 WARN("Handle %u(%p) is not of type %#x.\n", handle, entry, type);
527 return NULL;
530 return entry->object;
533 static HRESULT WINAPI d3d8_device_QueryInterface(IDirect3DDevice8 *iface, REFIID riid, void **out)
535 TRACE("iface %p, riid %s, out %p.\n",
536 iface, debugstr_guid(riid), out);
538 if (IsEqualGUID(riid, &IID_IDirect3DDevice8)
539 || IsEqualGUID(riid, &IID_IUnknown))
541 IDirect3DDevice8_AddRef(iface);
542 *out = iface;
543 return S_OK;
546 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
548 *out = NULL;
549 return E_NOINTERFACE;
552 static ULONG WINAPI d3d8_device_AddRef(IDirect3DDevice8 *iface)
554 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
555 ULONG ref = InterlockedIncrement(&device->ref);
557 TRACE("%p increasing refcount to %u.\n", iface, ref);
559 return ref;
562 static ULONG WINAPI d3d8_device_Release(IDirect3DDevice8 *iface)
564 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
565 ULONG ref;
567 if (device->inDestruction)
568 return 0;
570 ref = InterlockedDecrement(&device->ref);
572 TRACE("%p decreasing refcount to %u.\n", iface, ref);
574 if (!ref)
576 IDirect3D8 *parent = device->d3d_parent;
577 unsigned i;
579 TRACE("Releasing wined3d device %p.\n", device->wined3d_device);
581 wined3d_mutex_lock();
583 device->inDestruction = TRUE;
585 for (i = 0; i < device->numConvertedDecls; ++i)
587 d3d8_vertex_declaration_destroy(device->decls[i].declaration);
589 heap_free(device->decls);
591 if (device->vertex_buffer)
592 wined3d_buffer_decref(device->vertex_buffer);
593 if (device->index_buffer)
594 wined3d_buffer_decref(device->index_buffer);
596 wined3d_device_uninit_3d(device->wined3d_device);
597 wined3d_device_release_focus_window(device->wined3d_device);
598 wined3d_device_decref(device->wined3d_device);
599 heap_free(device->handle_table.entries);
600 heap_free(device);
602 wined3d_mutex_unlock();
604 IDirect3D8_Release(parent);
606 return ref;
609 static HRESULT WINAPI d3d8_device_TestCooperativeLevel(IDirect3DDevice8 *iface)
611 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
613 TRACE("iface %p.\n", iface);
615 TRACE("device state: %#x.\n", device->device_state);
617 switch (device->device_state)
619 default:
620 case D3D8_DEVICE_STATE_OK:
621 return D3D_OK;
622 case D3D8_DEVICE_STATE_LOST:
623 return D3DERR_DEVICELOST;
624 case D3D8_DEVICE_STATE_NOT_RESET:
625 return D3DERR_DEVICENOTRESET;
629 static UINT WINAPI d3d8_device_GetAvailableTextureMem(IDirect3DDevice8 *iface)
631 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
632 UINT ret;
634 TRACE("iface %p.\n", iface);
636 wined3d_mutex_lock();
637 ret = wined3d_device_get_available_texture_mem(device->wined3d_device);
638 wined3d_mutex_unlock();
640 return ret;
643 static HRESULT WINAPI d3d8_device_ResourceManagerDiscardBytes(IDirect3DDevice8 *iface, DWORD byte_count)
645 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
647 TRACE("iface %p, byte_count %u.\n", iface, byte_count);
649 if (byte_count)
650 FIXME("Byte count ignored.\n");
652 wined3d_mutex_lock();
653 wined3d_device_evict_managed_resources(device->wined3d_device);
654 wined3d_mutex_unlock();
656 return D3D_OK;
659 static HRESULT WINAPI d3d8_device_GetDirect3D(IDirect3DDevice8 *iface, IDirect3D8 **d3d8)
661 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
663 TRACE("iface %p, d3d8 %p.\n", iface, d3d8);
665 if (!d3d8)
666 return D3DERR_INVALIDCALL;
668 return IDirect3D8_QueryInterface(device->d3d_parent, &IID_IDirect3D8, (void **)d3d8);
671 static HRESULT WINAPI d3d8_device_GetDeviceCaps(IDirect3DDevice8 *iface, D3DCAPS8 *caps)
673 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
674 struct wined3d_caps wined3d_caps;
675 HRESULT hr;
677 TRACE("iface %p, caps %p.\n", iface, caps);
679 if (!caps)
680 return D3DERR_INVALIDCALL;
682 wined3d_mutex_lock();
683 hr = wined3d_device_get_device_caps(device->wined3d_device, &wined3d_caps);
684 wined3d_mutex_unlock();
686 d3dcaps_from_wined3dcaps(caps, &wined3d_caps);
688 return hr;
691 static HRESULT WINAPI d3d8_device_GetDisplayMode(IDirect3DDevice8 *iface, D3DDISPLAYMODE *mode)
693 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
694 struct wined3d_display_mode wined3d_mode;
695 HRESULT hr;
697 TRACE("iface %p, mode %p.\n", iface, mode);
699 wined3d_mutex_lock();
700 hr = wined3d_device_get_display_mode(device->wined3d_device, 0, &wined3d_mode, NULL);
701 wined3d_mutex_unlock();
703 if (SUCCEEDED(hr))
705 mode->Width = wined3d_mode.width;
706 mode->Height = wined3d_mode.height;
707 mode->RefreshRate = wined3d_mode.refresh_rate;
708 mode->Format = d3dformat_from_wined3dformat(wined3d_mode.format_id);
711 return hr;
714 static HRESULT WINAPI d3d8_device_GetCreationParameters(IDirect3DDevice8 *iface,
715 D3DDEVICE_CREATION_PARAMETERS *parameters)
717 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
719 TRACE("iface %p, parameters %p.\n", iface, parameters);
721 wined3d_mutex_lock();
722 wined3d_device_get_creation_parameters(device->wined3d_device,
723 (struct wined3d_device_creation_parameters *)parameters);
724 wined3d_mutex_unlock();
726 return D3D_OK;
729 static HRESULT WINAPI d3d8_device_SetCursorProperties(IDirect3DDevice8 *iface,
730 UINT hotspot_x, UINT hotspot_y, IDirect3DSurface8 *bitmap)
732 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
733 struct d3d8_surface *bitmap_impl = unsafe_impl_from_IDirect3DSurface8(bitmap);
734 HRESULT hr;
736 TRACE("iface %p, hotspot_x %u, hotspot_y %u, bitmap %p.\n",
737 iface, hotspot_x, hotspot_y, bitmap);
739 if (!bitmap)
741 WARN("No cursor bitmap, returning D3DERR_INVALIDCALL.\n");
742 return D3DERR_INVALIDCALL;
745 wined3d_mutex_lock();
746 hr = wined3d_device_set_cursor_properties(device->wined3d_device,
747 hotspot_x, hotspot_y, bitmap_impl->wined3d_texture, bitmap_impl->sub_resource_idx);
748 wined3d_mutex_unlock();
750 return hr;
753 static void WINAPI d3d8_device_SetCursorPosition(IDirect3DDevice8 *iface, UINT x, UINT y, DWORD flags)
755 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
757 TRACE("iface %p, x %u, y %u, flags %#x.\n", iface, x, y, flags);
759 wined3d_mutex_lock();
760 wined3d_device_set_cursor_position(device->wined3d_device, x, y, flags);
761 wined3d_mutex_unlock();
764 static BOOL WINAPI d3d8_device_ShowCursor(IDirect3DDevice8 *iface, BOOL show)
766 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
767 BOOL ret;
769 TRACE("iface %p, show %#x.\n", iface, show);
771 wined3d_mutex_lock();
772 ret = wined3d_device_show_cursor(device->wined3d_device, show);
773 wined3d_mutex_unlock();
775 return ret;
778 static HRESULT WINAPI d3d8_device_CreateAdditionalSwapChain(IDirect3DDevice8 *iface,
779 D3DPRESENT_PARAMETERS *present_parameters, IDirect3DSwapChain8 **swapchain)
781 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
782 struct wined3d_swapchain_desc desc;
783 struct d3d8_swapchain *object;
784 unsigned int swap_interval;
785 unsigned int i, count;
786 HRESULT hr;
788 TRACE("iface %p, present_parameters %p, swapchain %p.\n",
789 iface, present_parameters, swapchain);
791 if (!present_parameters->Windowed)
793 WARN("Trying to create an additional fullscreen swapchain, returning D3DERR_INVALIDCALL.\n");
794 return D3DERR_INVALIDCALL;
797 wined3d_mutex_lock();
798 count = wined3d_device_get_swapchain_count(device->wined3d_device);
799 for (i = 0; i < count; ++i)
801 struct wined3d_swapchain *wined3d_swapchain;
803 wined3d_swapchain = wined3d_device_get_swapchain(device->wined3d_device, i);
804 wined3d_swapchain_get_desc(wined3d_swapchain, &desc);
806 if (!desc.windowed)
808 wined3d_mutex_unlock();
809 WARN("Trying to create an additional swapchain in fullscreen mode, returning D3DERR_INVALIDCALL.\n");
810 return D3DERR_INVALIDCALL;
813 wined3d_mutex_unlock();
815 if (!wined3d_swapchain_desc_from_present_parameters(&desc, present_parameters))
816 return D3DERR_INVALIDCALL;
817 swap_interval = wined3dswapinterval_from_d3d(present_parameters->FullScreen_PresentationInterval);
818 if (SUCCEEDED(hr = d3d8_swapchain_create(device, &desc, swap_interval, &object)))
819 *swapchain = &object->IDirect3DSwapChain8_iface;
820 present_parameters_from_wined3d_swapchain_desc(present_parameters,
821 &desc, present_parameters->FullScreen_PresentationInterval);
823 return hr;
826 static HRESULT CDECL reset_enum_callback(struct wined3d_resource *resource)
828 struct wined3d_resource_desc desc;
829 IDirect3DBaseTexture8 *texture;
830 struct d3d8_surface *surface;
831 IUnknown *parent;
833 wined3d_resource_get_desc(resource, &desc);
834 if (desc.access & WINED3D_RESOURCE_ACCESS_CPU)
835 return D3D_OK;
837 if (desc.resource_type != WINED3D_RTYPE_TEXTURE_2D)
839 WARN("Resource %p in pool D3DPOOL_DEFAULT blocks the Reset call.\n", resource);
840 return D3DERR_DEVICELOST;
843 parent = wined3d_resource_get_parent(resource);
844 if (parent && SUCCEEDED(IUnknown_QueryInterface(parent, &IID_IDirect3DBaseTexture8, (void **)&texture)))
846 IDirect3DBaseTexture8_Release(texture);
847 WARN("Texture %p (resource %p) in pool D3DPOOL_DEFAULT blocks the Reset call.\n", texture, resource);
848 return D3DERR_DEVICELOST;
851 surface = wined3d_texture_get_sub_resource_parent(wined3d_texture_from_resource(resource), 0);
852 if (!surface->resource.refcount)
853 return D3D_OK;
855 WARN("Surface %p in pool D3DPOOL_DEFAULT blocks the Reset call.\n", surface);
856 return D3DERR_DEVICELOST;
859 static HRESULT WINAPI d3d8_device_Reset(IDirect3DDevice8 *iface,
860 D3DPRESENT_PARAMETERS *present_parameters)
862 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
863 struct wined3d_swapchain_desc swapchain_desc;
864 HRESULT hr;
866 TRACE("iface %p, present_parameters %p.\n", iface, present_parameters);
868 if (device->device_state == D3D8_DEVICE_STATE_LOST)
870 WARN("App not active, returning D3DERR_DEVICELOST.\n");
871 return D3DERR_DEVICELOST;
873 if (!wined3d_swapchain_desc_from_present_parameters(&swapchain_desc, present_parameters))
874 return D3DERR_INVALIDCALL;
876 wined3d_mutex_lock();
878 if (device->vertex_buffer)
880 wined3d_buffer_decref(device->vertex_buffer);
881 device->vertex_buffer = NULL;
882 device->vertex_buffer_size = 0;
884 if (device->index_buffer)
886 wined3d_buffer_decref(device->index_buffer);
887 device->index_buffer = NULL;
888 device->index_buffer_size = 0;
891 if (SUCCEEDED(hr = wined3d_device_reset(device->wined3d_device, &swapchain_desc,
892 NULL, reset_enum_callback, TRUE)))
894 present_parameters->BackBufferCount = swapchain_desc.backbuffer_count;
895 device->implicit_swapchain->swap_interval
896 = wined3dswapinterval_from_d3d(present_parameters->FullScreen_PresentationInterval);
897 wined3d_device_set_render_state(device->wined3d_device, WINED3D_RS_POINTSIZE_MIN, 0);
898 wined3d_device_set_render_state(device->wined3d_device, WINED3D_RS_ZENABLE,
899 !!swapchain_desc.enable_auto_depth_stencil);
900 device->device_state = D3D8_DEVICE_STATE_OK;
902 else
904 device->device_state = D3D8_DEVICE_STATE_NOT_RESET;
906 wined3d_mutex_unlock();
908 return hr;
911 static HRESULT WINAPI d3d8_device_Present(IDirect3DDevice8 *iface, const RECT *src_rect,
912 const RECT *dst_rect, HWND dst_window_override, const RGNDATA *dirty_region)
914 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
916 TRACE("iface %p, src_rect %s, dst_rect %s, dst_window_override %p, dirty_region %p.\n",
917 iface, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect), dst_window_override, dirty_region);
919 /* Fraps does not hook IDirect3DDevice8::Present regardless of the hotpatch
920 * attribute. It only hooks IDirect3DSwapChain8::Present. Yet it properly
921 * shows a framerate on Windows in applications that only call the device
922 * method, like e.g. the dx8 sdk samples. The conclusion is that native
923 * calls the swapchain's public method from the device. */
924 return IDirect3DSwapChain8_Present(&device->implicit_swapchain->IDirect3DSwapChain8_iface,
925 src_rect, dst_rect, dst_window_override, dirty_region);
928 static HRESULT WINAPI d3d8_device_GetBackBuffer(IDirect3DDevice8 *iface,
929 UINT backbuffer_idx, D3DBACKBUFFER_TYPE backbuffer_type, IDirect3DSurface8 **backbuffer)
931 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
932 struct wined3d_swapchain *wined3d_swapchain;
933 struct wined3d_texture *wined3d_texture;
934 struct d3d8_surface *surface_impl;
936 TRACE("iface %p, backbuffer_idx %u, backbuffer_type %#x, backbuffer %p.\n",
937 iface, backbuffer_idx, backbuffer_type, backbuffer);
939 /* backbuffer_type is ignored by native. */
941 /* No need to check for backbuffer == NULL, Windows crashes in that case. */
942 wined3d_mutex_lock();
944 wined3d_swapchain = device->implicit_swapchain->wined3d_swapchain;
945 if (!(wined3d_texture = wined3d_swapchain_get_back_buffer(wined3d_swapchain, backbuffer_idx)))
947 wined3d_mutex_unlock();
948 *backbuffer = NULL;
949 return D3DERR_INVALIDCALL;
952 surface_impl = wined3d_texture_get_sub_resource_parent(wined3d_texture, 0);
953 *backbuffer = &surface_impl->IDirect3DSurface8_iface;
954 IDirect3DSurface8_AddRef(*backbuffer);
956 wined3d_mutex_unlock();
957 return D3D_OK;
960 static HRESULT WINAPI d3d8_device_GetRasterStatus(IDirect3DDevice8 *iface, D3DRASTER_STATUS *raster_status)
962 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
963 HRESULT hr;
965 TRACE("iface %p, raster_status %p.\n", iface, raster_status);
967 wined3d_mutex_lock();
968 hr = wined3d_device_get_raster_status(device->wined3d_device, 0, (struct wined3d_raster_status *)raster_status);
969 wined3d_mutex_unlock();
971 return hr;
974 static void WINAPI d3d8_device_SetGammaRamp(IDirect3DDevice8 *iface, DWORD flags, const D3DGAMMARAMP *ramp)
976 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
978 TRACE("iface %p, flags %#x, ramp %p.\n", iface, flags, ramp);
980 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
981 wined3d_mutex_lock();
982 wined3d_device_set_gamma_ramp(device->wined3d_device, 0, flags, (const struct wined3d_gamma_ramp *)ramp);
983 wined3d_mutex_unlock();
986 static void WINAPI d3d8_device_GetGammaRamp(IDirect3DDevice8 *iface, D3DGAMMARAMP *ramp)
988 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
990 TRACE("iface %p, ramp %p.\n", iface, ramp);
992 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
993 wined3d_mutex_lock();
994 wined3d_device_get_gamma_ramp(device->wined3d_device, 0, (struct wined3d_gamma_ramp *)ramp);
995 wined3d_mutex_unlock();
998 static HRESULT WINAPI d3d8_device_CreateTexture(IDirect3DDevice8 *iface,
999 UINT width, UINT height, UINT levels, DWORD usage, D3DFORMAT format,
1000 D3DPOOL pool, IDirect3DTexture8 **texture)
1002 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1003 struct d3d8_texture *object;
1004 HRESULT hr;
1006 TRACE("iface %p, width %u, height %u, levels %u, usage %#x, format %#x, pool %#x, texture %p.\n",
1007 iface, width, height, levels, usage, format, pool, texture);
1009 if (!format)
1010 return D3DERR_INVALIDCALL;
1012 *texture = NULL;
1013 if (!(object = heap_alloc_zero(sizeof(*object))))
1014 return D3DERR_OUTOFVIDEOMEMORY;
1016 hr = texture_init(object, device, width, height, levels, usage, format, pool);
1017 if (FAILED(hr))
1019 WARN("Failed to initialize texture, hr %#x.\n", hr);
1020 heap_free(object);
1021 return hr;
1024 TRACE("Created texture %p.\n", object);
1025 *texture = (IDirect3DTexture8 *)&object->IDirect3DBaseTexture8_iface;
1027 return D3D_OK;
1030 static HRESULT WINAPI d3d8_device_CreateVolumeTexture(IDirect3DDevice8 *iface,
1031 UINT width, UINT height, UINT depth, UINT levels, DWORD usage, D3DFORMAT format,
1032 D3DPOOL pool, IDirect3DVolumeTexture8 **texture)
1034 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1035 struct d3d8_texture *object;
1036 HRESULT hr;
1038 TRACE("iface %p, width %u, height %u, depth %u, levels %u, usage %#x, format %#x, pool %#x, texture %p.\n",
1039 iface, width, height, depth, levels, usage, format, pool, texture);
1041 if (!format)
1042 return D3DERR_INVALIDCALL;
1044 *texture = NULL;
1045 if (!(object = heap_alloc_zero(sizeof(*object))))
1046 return D3DERR_OUTOFVIDEOMEMORY;
1048 hr = volumetexture_init(object, device, width, height, depth, levels, usage, format, pool);
1049 if (FAILED(hr))
1051 WARN("Failed to initialize volume texture, hr %#x.\n", hr);
1052 heap_free(object);
1053 return hr;
1056 TRACE("Created volume texture %p.\n", object);
1057 *texture = (IDirect3DVolumeTexture8 *)&object->IDirect3DBaseTexture8_iface;
1059 return D3D_OK;
1062 static HRESULT WINAPI d3d8_device_CreateCubeTexture(IDirect3DDevice8 *iface, UINT edge_length,
1063 UINT levels, DWORD usage, D3DFORMAT format, D3DPOOL pool, IDirect3DCubeTexture8 **texture)
1065 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1066 struct d3d8_texture *object;
1067 HRESULT hr;
1069 TRACE("iface %p, edge_length %u, levels %u, usage %#x, format %#x, pool %#x, texture %p.\n",
1070 iface, edge_length, levels, usage, format, pool, texture);
1072 if (!format)
1073 return D3DERR_INVALIDCALL;
1075 *texture = NULL;
1076 if (!(object = heap_alloc_zero(sizeof(*object))))
1077 return D3DERR_OUTOFVIDEOMEMORY;
1079 hr = cubetexture_init(object, device, edge_length, levels, usage, format, pool);
1080 if (FAILED(hr))
1082 WARN("Failed to initialize cube texture, hr %#x.\n", hr);
1083 heap_free(object);
1084 return hr;
1087 TRACE("Created cube texture %p.\n", object);
1088 *texture = (IDirect3DCubeTexture8 *)&object->IDirect3DBaseTexture8_iface;
1090 return hr;
1093 static HRESULT WINAPI d3d8_device_CreateVertexBuffer(IDirect3DDevice8 *iface, UINT size,
1094 DWORD usage, DWORD fvf, D3DPOOL pool, IDirect3DVertexBuffer8 **buffer)
1096 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1097 struct d3d8_vertexbuffer *object;
1098 HRESULT hr;
1100 TRACE("iface %p, size %u, usage %#x, fvf %#x, pool %#x, buffer %p.\n",
1101 iface, size, usage, fvf, pool, buffer);
1103 if (!(object = heap_alloc_zero(sizeof(*object))))
1104 return D3DERR_OUTOFVIDEOMEMORY;
1106 hr = vertexbuffer_init(object, device, size, usage, fvf, pool);
1107 if (FAILED(hr))
1109 WARN("Failed to initialize vertex buffer, hr %#x.\n", hr);
1110 heap_free(object);
1111 return hr;
1114 TRACE("Created vertex buffer %p.\n", object);
1115 *buffer = &object->IDirect3DVertexBuffer8_iface;
1117 return D3D_OK;
1120 static HRESULT WINAPI d3d8_device_CreateIndexBuffer(IDirect3DDevice8 *iface, UINT size,
1121 DWORD usage, D3DFORMAT format, D3DPOOL pool, IDirect3DIndexBuffer8 **buffer)
1123 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1124 struct d3d8_indexbuffer *object;
1125 HRESULT hr;
1127 TRACE("iface %p, size %u, usage %#x, format %#x, pool %#x, buffer %p.\n",
1128 iface, size, usage, format, pool, buffer);
1130 if (!(object = heap_alloc_zero(sizeof(*object))))
1131 return D3DERR_OUTOFVIDEOMEMORY;
1133 hr = indexbuffer_init(object, device, size, usage, format, pool);
1134 if (FAILED(hr))
1136 WARN("Failed to initialize index buffer, hr %#x.\n", hr);
1137 heap_free(object);
1138 return hr;
1141 TRACE("Created index buffer %p.\n", object);
1142 *buffer = &object->IDirect3DIndexBuffer8_iface;
1144 return D3D_OK;
1147 static HRESULT d3d8_device_create_surface(struct d3d8_device *device, UINT width, UINT height,
1148 D3DFORMAT format, DWORD flags, IDirect3DSurface8 **surface, UINT usage, D3DPOOL pool,
1149 D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality)
1151 struct wined3d_resource_desc desc;
1152 struct d3d8_surface *surface_impl;
1153 struct wined3d_texture *texture;
1154 HRESULT hr;
1156 TRACE("device %p, width %u, height %u, format %#x, flags %#x, surface %p, "
1157 "usage %#x, pool %#x, multisample_type %#x, multisample_quality %u.\n",
1158 device, width, height, format, flags, surface,
1159 usage, pool, multisample_type, multisample_quality);
1161 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
1162 desc.format = wined3dformat_from_d3dformat(format);
1163 desc.multisample_type = multisample_type;
1164 desc.multisample_quality = multisample_quality;
1165 desc.usage = usage & WINED3DUSAGE_MASK;
1166 if (pool == D3DPOOL_SCRATCH)
1167 desc.usage |= WINED3DUSAGE_SCRATCH;
1168 desc.bind_flags = wined3d_bind_flags_from_d3d8_usage(usage);
1169 desc.access = wined3daccess_from_d3dpool(pool, usage);
1170 desc.width = width;
1171 desc.height = height;
1172 desc.depth = 1;
1173 desc.size = 0;
1175 wined3d_mutex_lock();
1177 if (FAILED(hr = wined3d_texture_create(device->wined3d_device, &desc,
1178 1, 1, flags, NULL, NULL, &d3d8_null_wined3d_parent_ops, &texture)))
1180 wined3d_mutex_unlock();
1181 WARN("Failed to create texture, hr %#x.\n", hr);
1182 return hr;
1185 surface_impl = wined3d_texture_get_sub_resource_parent(texture, 0);
1186 surface_impl->parent_device = &device->IDirect3DDevice8_iface;
1187 *surface = &surface_impl->IDirect3DSurface8_iface;
1188 IDirect3DSurface8_AddRef(*surface);
1189 wined3d_texture_decref(texture);
1191 wined3d_mutex_unlock();
1193 return D3D_OK;
1196 static HRESULT WINAPI d3d8_device_CreateRenderTarget(IDirect3DDevice8 *iface, UINT width,
1197 UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, BOOL lockable,
1198 IDirect3DSurface8 **surface)
1200 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1201 DWORD flags = 0;
1203 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, lockable %#x, surface %p.\n",
1204 iface, width, height, format, multisample_type, lockable, surface);
1206 if (!format)
1207 return D3DERR_INVALIDCALL;
1209 *surface = NULL;
1210 if (lockable)
1211 flags |= WINED3D_TEXTURE_CREATE_MAPPABLE;
1213 return d3d8_device_create_surface(device, width, height, format, flags, surface,
1214 D3DUSAGE_RENDERTARGET, D3DPOOL_DEFAULT, multisample_type, 0);
1217 static HRESULT WINAPI d3d8_device_CreateDepthStencilSurface(IDirect3DDevice8 *iface,
1218 UINT width, UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type,
1219 IDirect3DSurface8 **surface)
1221 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1223 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, surface %p.\n",
1224 iface, width, height, format, multisample_type, surface);
1226 if (!format)
1227 return D3DERR_INVALIDCALL;
1229 *surface = NULL;
1231 /* TODO: Verify that Discard is false */
1232 return d3d8_device_create_surface(device, width, height, format, 0,
1233 surface, D3DUSAGE_DEPTHSTENCIL, D3DPOOL_DEFAULT, multisample_type, 0);
1236 /* IDirect3DDevice8Impl::CreateImageSurface returns surface with pool type SYSTEMMEM */
1237 static HRESULT WINAPI d3d8_device_CreateImageSurface(IDirect3DDevice8 *iface, UINT width,
1238 UINT height, D3DFORMAT format, IDirect3DSurface8 **surface)
1240 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1242 TRACE("iface %p, width %u, height %u, format %#x, surface %p.\n",
1243 iface, width, height, format, surface);
1245 *surface = NULL;
1247 return d3d8_device_create_surface(device, width, height, format, 0,
1248 surface, 0, D3DPOOL_SYSTEMMEM, D3DMULTISAMPLE_NONE, 0);
1251 static HRESULT WINAPI d3d8_device_CopyRects(IDirect3DDevice8 *iface,
1252 IDirect3DSurface8 *src_surface, const RECT *src_rects, UINT rect_count,
1253 IDirect3DSurface8 *dst_surface, const POINT *dst_points)
1255 struct d3d8_surface *src = unsafe_impl_from_IDirect3DSurface8(src_surface);
1256 struct d3d8_surface *dst = unsafe_impl_from_IDirect3DSurface8(dst_surface);
1257 enum wined3d_format_id src_format, dst_format;
1258 struct wined3d_sub_resource_desc wined3d_desc;
1259 UINT src_w, src_h;
1261 TRACE("iface %p, src_surface %p, src_rects %p, rect_count %u, dst_surface %p, dst_points %p.\n",
1262 iface, src_surface, src_rects, rect_count, dst_surface, dst_points);
1264 /* Check that the source texture is in WINED3D_POOL_SYSTEM_MEM and the
1265 * destination texture is in WINED3D_POOL_DEFAULT. */
1267 wined3d_mutex_lock();
1268 wined3d_texture_get_sub_resource_desc(src->wined3d_texture, src->sub_resource_idx, &wined3d_desc);
1269 if (wined3d_desc.bind_flags & WINED3D_BIND_DEPTH_STENCIL)
1271 WARN("Source %p is a depth stencil surface, returning D3DERR_INVALIDCALL.\n", src_surface);
1272 wined3d_mutex_unlock();
1273 return D3DERR_INVALIDCALL;
1275 src_format = wined3d_desc.format;
1276 src_w = wined3d_desc.width;
1277 src_h = wined3d_desc.height;
1279 wined3d_texture_get_sub_resource_desc(dst->wined3d_texture, dst->sub_resource_idx, &wined3d_desc);
1280 if (wined3d_desc.bind_flags & WINED3D_BIND_DEPTH_STENCIL)
1282 WARN("Destination %p is a depth stencil surface, returning D3DERR_INVALIDCALL.\n", dst_surface);
1283 wined3d_mutex_unlock();
1284 return D3DERR_INVALIDCALL;
1286 dst_format = wined3d_desc.format;
1288 /* Check that the source and destination formats match */
1289 if (src_format != dst_format)
1291 WARN("Source %p format must match the destination %p format, returning D3DERR_INVALIDCALL.\n",
1292 src_surface, dst_surface);
1293 wined3d_mutex_unlock();
1294 return D3DERR_INVALIDCALL;
1297 /* Quick if complete copy ... */
1298 if (!rect_count && !src_rects && !dst_points)
1300 RECT rect = {0, 0, src_w, src_h};
1301 wined3d_texture_blt(dst->wined3d_texture, dst->sub_resource_idx, &rect,
1302 src->wined3d_texture, src->sub_resource_idx, &rect, 0, NULL, WINED3D_TEXF_POINT);
1304 else
1306 unsigned int i;
1307 /* Copy rect by rect */
1308 if (src_rects && dst_points)
1310 for (i = 0; i < rect_count; ++i)
1312 UINT w = src_rects[i].right - src_rects[i].left;
1313 UINT h = src_rects[i].bottom - src_rects[i].top;
1314 RECT dst_rect = {dst_points[i].x, dst_points[i].y,
1315 dst_points[i].x + w, dst_points[i].y + h};
1317 wined3d_texture_blt(dst->wined3d_texture, dst->sub_resource_idx, &dst_rect,
1318 src->wined3d_texture, src->sub_resource_idx, &src_rects[i], 0, NULL, WINED3D_TEXF_POINT);
1321 else
1323 for (i = 0; i < rect_count; ++i)
1325 UINT w = src_rects[i].right - src_rects[i].left;
1326 UINT h = src_rects[i].bottom - src_rects[i].top;
1327 RECT dst_rect = {0, 0, w, h};
1329 wined3d_texture_blt(dst->wined3d_texture, dst->sub_resource_idx, &dst_rect,
1330 src->wined3d_texture, src->sub_resource_idx, &src_rects[i], 0, NULL, WINED3D_TEXF_POINT);
1334 wined3d_mutex_unlock();
1336 return WINED3D_OK;
1339 static HRESULT WINAPI d3d8_device_UpdateTexture(IDirect3DDevice8 *iface,
1340 IDirect3DBaseTexture8 *src_texture, IDirect3DBaseTexture8 *dst_texture)
1342 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1343 struct d3d8_texture *src_impl, *dst_impl;
1344 HRESULT hr;
1346 TRACE("iface %p, src_texture %p, dst_texture %p.\n", iface, src_texture, dst_texture);
1348 src_impl = unsafe_impl_from_IDirect3DBaseTexture8(src_texture);
1349 dst_impl = unsafe_impl_from_IDirect3DBaseTexture8(dst_texture);
1351 wined3d_mutex_lock();
1352 hr = wined3d_device_update_texture(device->wined3d_device,
1353 src_impl->wined3d_texture, dst_impl->wined3d_texture);
1354 wined3d_mutex_unlock();
1356 return hr;
1359 static HRESULT WINAPI d3d8_device_GetFrontBuffer(IDirect3DDevice8 *iface, IDirect3DSurface8 *dst_surface)
1361 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1362 struct d3d8_surface *dst_impl = unsafe_impl_from_IDirect3DSurface8(dst_surface);
1363 HRESULT hr;
1365 TRACE("iface %p, dst_surface %p.\n", iface, dst_surface);
1367 if (!dst_surface)
1369 WARN("Invalid destination surface passed.\n");
1370 return D3DERR_INVALIDCALL;
1373 wined3d_mutex_lock();
1374 hr = wined3d_swapchain_get_front_buffer_data(device->implicit_swapchain->wined3d_swapchain,
1375 dst_impl->wined3d_texture, dst_impl->sub_resource_idx);
1376 wined3d_mutex_unlock();
1378 return hr;
1381 static HRESULT WINAPI d3d8_device_SetRenderTarget(IDirect3DDevice8 *iface,
1382 IDirect3DSurface8 *render_target, IDirect3DSurface8 *depth_stencil)
1384 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1385 struct d3d8_surface *rt_impl = unsafe_impl_from_IDirect3DSurface8(render_target);
1386 struct d3d8_surface *ds_impl = unsafe_impl_from_IDirect3DSurface8(depth_stencil);
1387 struct wined3d_rendertarget_view *original_dsv, *rtv;
1388 HRESULT hr = D3D_OK;
1390 TRACE("iface %p, render_target %p, depth_stencil %p.\n", iface, render_target, depth_stencil);
1392 if (rt_impl && d3d8_surface_get_device(rt_impl) != device)
1394 WARN("Render target surface does not match device.\n");
1395 return D3DERR_INVALIDCALL;
1398 wined3d_mutex_lock();
1400 if (ds_impl)
1402 struct wined3d_sub_resource_desc ds_desc, rt_desc;
1403 struct wined3d_rendertarget_view *original_rtv;
1404 struct d3d8_surface *original_surface;
1406 /* If no render target is passed in check the size against the current RT */
1407 if (!render_target)
1409 if (!(original_rtv = wined3d_device_get_rendertarget_view(device->wined3d_device, 0)))
1411 wined3d_mutex_unlock();
1412 return D3DERR_NOTFOUND;
1414 original_surface = wined3d_rendertarget_view_get_sub_resource_parent(original_rtv);
1415 wined3d_texture_get_sub_resource_desc(original_surface->wined3d_texture,
1416 original_surface->sub_resource_idx, &rt_desc);
1418 else
1419 wined3d_texture_get_sub_resource_desc(rt_impl->wined3d_texture,
1420 rt_impl->sub_resource_idx, &rt_desc);
1422 wined3d_texture_get_sub_resource_desc(ds_impl->wined3d_texture, ds_impl->sub_resource_idx, &ds_desc);
1424 if (ds_desc.width < rt_desc.width || ds_desc.height < rt_desc.height)
1426 WARN("Depth stencil is smaller than the render target, returning D3DERR_INVALIDCALL\n");
1427 wined3d_mutex_unlock();
1428 return D3DERR_INVALIDCALL;
1430 if (ds_desc.multisample_type != rt_desc.multisample_type
1431 || ds_desc.multisample_quality != rt_desc.multisample_quality)
1433 WARN("Multisample settings do not match, returning D3DERR_INVALIDCALL\n");
1434 wined3d_mutex_unlock();
1435 return D3DERR_INVALIDCALL;
1439 original_dsv = wined3d_device_get_depth_stencil_view(device->wined3d_device);
1440 rtv = ds_impl ? d3d8_surface_acquire_rendertarget_view(ds_impl) : NULL;
1441 hr = wined3d_device_set_depth_stencil_view(device->wined3d_device, rtv);
1442 d3d8_surface_release_rendertarget_view(ds_impl, rtv);
1443 if (SUCCEEDED(hr))
1445 rtv = render_target ? d3d8_surface_acquire_rendertarget_view(rt_impl) : NULL;
1446 if (render_target && FAILED(hr = wined3d_device_set_rendertarget_view(device->wined3d_device, 0, rtv, TRUE)))
1447 wined3d_device_set_depth_stencil_view(device->wined3d_device, original_dsv);
1448 d3d8_surface_release_rendertarget_view(rt_impl, rtv);
1451 wined3d_mutex_unlock();
1453 return hr;
1456 static HRESULT WINAPI d3d8_device_GetRenderTarget(IDirect3DDevice8 *iface, IDirect3DSurface8 **render_target)
1458 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1459 struct wined3d_rendertarget_view *wined3d_rtv;
1460 struct d3d8_surface *surface_impl;
1461 HRESULT hr;
1463 TRACE("iface %p, render_target %p.\n", iface, render_target);
1465 if (!render_target)
1466 return D3DERR_INVALIDCALL;
1468 wined3d_mutex_lock();
1469 if ((wined3d_rtv = wined3d_device_get_rendertarget_view(device->wined3d_device, 0)))
1471 /* We want the sub resource parent here, since the view itself may be
1472 * internal to wined3d and may not have a parent. */
1473 surface_impl = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_rtv);
1474 *render_target = &surface_impl->IDirect3DSurface8_iface;
1475 IDirect3DSurface8_AddRef(*render_target);
1476 hr = D3D_OK;
1478 else
1480 ERR("Failed to get wined3d render target.\n");
1481 *render_target = NULL;
1482 hr = D3DERR_NOTFOUND;
1484 wined3d_mutex_unlock();
1486 return hr;
1489 static HRESULT WINAPI d3d8_device_GetDepthStencilSurface(IDirect3DDevice8 *iface, IDirect3DSurface8 **depth_stencil)
1491 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1492 struct wined3d_rendertarget_view *wined3d_dsv;
1493 struct d3d8_surface *surface_impl;
1494 HRESULT hr = D3D_OK;
1496 TRACE("iface %p, depth_stencil %p.\n", iface, depth_stencil);
1498 if (!depth_stencil)
1499 return D3DERR_INVALIDCALL;
1501 wined3d_mutex_lock();
1502 if ((wined3d_dsv = wined3d_device_get_depth_stencil_view(device->wined3d_device)))
1504 /* We want the sub resource parent here, since the view itself may be
1505 * internal to wined3d and may not have a parent. */
1506 surface_impl = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_dsv);
1507 *depth_stencil = &surface_impl->IDirect3DSurface8_iface;
1508 IDirect3DSurface8_AddRef(*depth_stencil);
1510 else
1512 hr = D3DERR_NOTFOUND;
1513 *depth_stencil = NULL;
1515 wined3d_mutex_unlock();
1517 return hr;
1520 static HRESULT WINAPI d3d8_device_BeginScene(IDirect3DDevice8 *iface)
1522 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1523 HRESULT hr;
1525 TRACE("iface %p.\n", iface);
1527 wined3d_mutex_lock();
1528 hr = wined3d_device_begin_scene(device->wined3d_device);
1529 wined3d_mutex_unlock();
1531 return hr;
1534 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d8_device_EndScene(IDirect3DDevice8 *iface)
1536 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1537 HRESULT hr;
1539 TRACE("iface %p.\n", iface);
1541 wined3d_mutex_lock();
1542 hr = wined3d_device_end_scene(device->wined3d_device);
1543 wined3d_mutex_unlock();
1545 return hr;
1548 static HRESULT WINAPI d3d8_device_Clear(IDirect3DDevice8 *iface, DWORD rect_count,
1549 const D3DRECT *rects, DWORD flags, D3DCOLOR color, float z, DWORD stencil)
1551 const struct wined3d_color c =
1553 ((color >> 16) & 0xff) / 255.0f,
1554 ((color >> 8) & 0xff) / 255.0f,
1555 (color & 0xff) / 255.0f,
1556 ((color >> 24) & 0xff) / 255.0f,
1558 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1559 HRESULT hr;
1561 TRACE("iface %p, rect_count %u, rects %p, flags %#x, color 0x%08x, z %.8e, stencil %u.\n",
1562 iface, rect_count, rects, flags, color, z, stencil);
1564 if (rect_count && !rects)
1566 WARN("count %u with NULL rects.\n", rect_count);
1567 rect_count = 0;
1570 wined3d_mutex_lock();
1571 hr = wined3d_device_clear(device->wined3d_device, rect_count, (const RECT *)rects, flags, &c, z, stencil);
1572 wined3d_mutex_unlock();
1574 return hr;
1577 static HRESULT WINAPI d3d8_device_SetTransform(IDirect3DDevice8 *iface,
1578 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
1580 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1582 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1584 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1585 wined3d_mutex_lock();
1586 wined3d_device_set_transform(device->wined3d_device, state, (const struct wined3d_matrix *)matrix);
1587 wined3d_mutex_unlock();
1589 return D3D_OK;
1592 static HRESULT WINAPI d3d8_device_GetTransform(IDirect3DDevice8 *iface,
1593 D3DTRANSFORMSTATETYPE state, D3DMATRIX *matrix)
1595 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1597 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1599 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1600 wined3d_mutex_lock();
1601 wined3d_device_get_transform(device->wined3d_device, state, (struct wined3d_matrix *)matrix);
1602 wined3d_mutex_unlock();
1604 return D3D_OK;
1607 static HRESULT WINAPI d3d8_device_MultiplyTransform(IDirect3DDevice8 *iface,
1608 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
1610 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1612 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1614 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1615 wined3d_mutex_lock();
1616 wined3d_device_multiply_transform(device->wined3d_device, state, (const struct wined3d_matrix *)matrix);
1617 wined3d_mutex_unlock();
1619 return D3D_OK;
1622 static HRESULT WINAPI d3d8_device_SetViewport(IDirect3DDevice8 *iface, const D3DVIEWPORT8 *viewport)
1624 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1625 struct wined3d_sub_resource_desc rt_desc;
1626 struct wined3d_rendertarget_view *rtv;
1627 struct d3d8_surface *surface;
1628 struct wined3d_viewport vp;
1630 TRACE("iface %p, viewport %p.\n", iface, viewport);
1632 wined3d_mutex_lock();
1633 if (!(rtv = wined3d_device_get_rendertarget_view(device->wined3d_device, 0)))
1635 wined3d_mutex_unlock();
1636 return D3DERR_NOTFOUND;
1638 surface = wined3d_rendertarget_view_get_sub_resource_parent(rtv);
1639 wined3d_texture_get_sub_resource_desc(surface->wined3d_texture, surface->sub_resource_idx, &rt_desc);
1641 if (viewport->X > rt_desc.width || viewport->Width > rt_desc.width - viewport->X
1642 || viewport->Y > rt_desc.height || viewport->Height > rt_desc.height - viewport->Y)
1644 WARN("Invalid viewport, returning D3DERR_INVALIDCALL.\n");
1645 wined3d_mutex_unlock();
1646 return D3DERR_INVALIDCALL;
1649 vp.x = viewport->X;
1650 vp.y = viewport->Y;
1651 vp.width = viewport->Width;
1652 vp.height = viewport->Height;
1653 vp.min_z = viewport->MinZ;
1654 vp.max_z = viewport->MaxZ;
1656 wined3d_device_set_viewports(device->wined3d_device, 1, &vp);
1657 wined3d_mutex_unlock();
1659 return D3D_OK;
1662 static HRESULT WINAPI d3d8_device_GetViewport(IDirect3DDevice8 *iface, D3DVIEWPORT8 *viewport)
1664 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1665 struct wined3d_viewport wined3d_viewport;
1667 TRACE("iface %p, viewport %p.\n", iface, viewport);
1669 wined3d_mutex_lock();
1670 wined3d_device_get_viewports(device->wined3d_device, NULL, &wined3d_viewport);
1671 wined3d_mutex_unlock();
1673 viewport->X = wined3d_viewport.x;
1674 viewport->Y = wined3d_viewport.y;
1675 viewport->Width = wined3d_viewport.width;
1676 viewport->Height = wined3d_viewport.height;
1677 viewport->MinZ = wined3d_viewport.min_z;
1678 viewport->MaxZ = wined3d_viewport.max_z;
1680 return D3D_OK;
1683 static HRESULT WINAPI d3d8_device_SetMaterial(IDirect3DDevice8 *iface, const D3DMATERIAL8 *material)
1685 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1687 TRACE("iface %p, material %p.\n", iface, material);
1689 /* Note: D3DMATERIAL8 is compatible with struct wined3d_material. */
1690 wined3d_mutex_lock();
1691 wined3d_device_set_material(device->wined3d_device, (const struct wined3d_material *)material);
1692 wined3d_mutex_unlock();
1694 return D3D_OK;
1697 static HRESULT WINAPI d3d8_device_GetMaterial(IDirect3DDevice8 *iface, D3DMATERIAL8 *material)
1699 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1701 TRACE("iface %p, material %p.\n", iface, material);
1703 /* Note: D3DMATERIAL8 is compatible with struct wined3d_material. */
1704 wined3d_mutex_lock();
1705 wined3d_device_get_material(device->wined3d_device, (struct wined3d_material *)material);
1706 wined3d_mutex_unlock();
1708 return D3D_OK;
1711 static HRESULT WINAPI d3d8_device_SetLight(IDirect3DDevice8 *iface, DWORD index, const D3DLIGHT8 *light)
1713 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1714 HRESULT hr;
1716 TRACE("iface %p, index %u, light %p.\n", iface, index, light);
1718 /* Note: D3DLIGHT8 is compatible with struct wined3d_light. */
1719 wined3d_mutex_lock();
1720 hr = wined3d_device_set_light(device->wined3d_device, index, (const struct wined3d_light *)light);
1721 wined3d_mutex_unlock();
1723 return hr;
1726 static HRESULT WINAPI d3d8_device_GetLight(IDirect3DDevice8 *iface, DWORD index, D3DLIGHT8 *light)
1728 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1729 HRESULT hr;
1731 TRACE("iface %p, index %u, light %p.\n", iface, index, light);
1733 /* Note: D3DLIGHT8 is compatible with struct wined3d_light. */
1734 wined3d_mutex_lock();
1735 hr = wined3d_device_get_light(device->wined3d_device, index, (struct wined3d_light *)light);
1736 wined3d_mutex_unlock();
1738 return hr;
1741 static HRESULT WINAPI d3d8_device_LightEnable(IDirect3DDevice8 *iface, DWORD index, BOOL enable)
1743 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1744 HRESULT hr;
1746 TRACE("iface %p, index %u, enable %#x.\n", iface, index, enable);
1748 wined3d_mutex_lock();
1749 hr = wined3d_device_set_light_enable(device->wined3d_device, index, enable);
1750 wined3d_mutex_unlock();
1752 return hr;
1755 static HRESULT WINAPI d3d8_device_GetLightEnable(IDirect3DDevice8 *iface, DWORD index, BOOL *enable)
1757 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1758 HRESULT hr;
1760 TRACE("iface %p, index %u, enable %p.\n", iface, index, enable);
1762 wined3d_mutex_lock();
1763 hr = wined3d_device_get_light_enable(device->wined3d_device, index, enable);
1764 wined3d_mutex_unlock();
1766 return hr;
1769 static HRESULT WINAPI d3d8_device_SetClipPlane(IDirect3DDevice8 *iface, DWORD index, const float *plane)
1771 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1772 HRESULT hr;
1774 TRACE("iface %p, index %u, plane %p.\n", iface, index, plane);
1776 wined3d_mutex_lock();
1777 hr = wined3d_device_set_clip_plane(device->wined3d_device, index, (const struct wined3d_vec4 *)plane);
1778 wined3d_mutex_unlock();
1780 return hr;
1783 static HRESULT WINAPI d3d8_device_GetClipPlane(IDirect3DDevice8 *iface, DWORD index, float *plane)
1785 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1786 HRESULT hr;
1788 TRACE("iface %p, index %u, plane %p.\n", iface, index, plane);
1790 wined3d_mutex_lock();
1791 hr = wined3d_device_get_clip_plane(device->wined3d_device, index, (struct wined3d_vec4 *)plane);
1792 wined3d_mutex_unlock();
1794 return hr;
1797 static HRESULT WINAPI d3d8_device_SetRenderState(IDirect3DDevice8 *iface,
1798 D3DRENDERSTATETYPE state, DWORD value)
1800 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1802 TRACE("iface %p, state %#x, value %#x.\n", iface, state, value);
1804 wined3d_mutex_lock();
1805 switch (state)
1807 case D3DRS_ZBIAS:
1808 wined3d_device_set_render_state(device->wined3d_device, WINED3D_RS_DEPTHBIAS, value);
1809 break;
1811 default:
1812 wined3d_device_set_render_state(device->wined3d_device, state, value);
1814 wined3d_mutex_unlock();
1816 return D3D_OK;
1819 static HRESULT WINAPI d3d8_device_GetRenderState(IDirect3DDevice8 *iface,
1820 D3DRENDERSTATETYPE state, DWORD *value)
1822 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1824 TRACE("iface %p, state %#x, value %p.\n", iface, state, value);
1826 wined3d_mutex_lock();
1827 switch (state)
1829 case D3DRS_ZBIAS:
1830 *value = wined3d_device_get_render_state(device->wined3d_device, WINED3D_RS_DEPTHBIAS);
1831 break;
1833 default:
1834 *value = wined3d_device_get_render_state(device->wined3d_device, state);
1836 wined3d_mutex_unlock();
1838 return D3D_OK;
1841 static HRESULT WINAPI d3d8_device_BeginStateBlock(IDirect3DDevice8 *iface)
1843 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1844 HRESULT hr;
1846 TRACE("iface %p.\n", iface);
1848 wined3d_mutex_lock();
1849 hr = wined3d_device_begin_stateblock(device->wined3d_device);
1850 wined3d_mutex_unlock();
1852 return hr;
1855 static HRESULT WINAPI d3d8_device_EndStateBlock(IDirect3DDevice8 *iface, DWORD *token)
1857 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1858 struct wined3d_stateblock *stateblock;
1859 HRESULT hr;
1861 TRACE("iface %p, token %p.\n", iface, token);
1863 /* Tell wineD3D to endstateblock before anything else (in case we run out
1864 * of memory later and cause locking problems)
1866 wined3d_mutex_lock();
1867 hr = wined3d_device_end_stateblock(device->wined3d_device, &stateblock);
1868 if (FAILED(hr))
1870 WARN("Failed to end the state block, %#x.\n", hr);
1871 wined3d_mutex_unlock();
1872 return hr;
1875 *token = d3d8_allocate_handle(&device->handle_table, stateblock, D3D8_HANDLE_SB);
1876 wined3d_mutex_unlock();
1878 if (*token == D3D8_INVALID_HANDLE)
1880 ERR("Failed to create a handle\n");
1881 wined3d_mutex_lock();
1882 wined3d_stateblock_decref(stateblock);
1883 wined3d_mutex_unlock();
1884 return E_FAIL;
1886 ++*token;
1888 TRACE("Returning %#x (%p).\n", *token, stateblock);
1890 return hr;
1893 static HRESULT WINAPI d3d8_device_ApplyStateBlock(IDirect3DDevice8 *iface, DWORD token)
1895 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1896 struct wined3d_stateblock *stateblock;
1898 TRACE("iface %p, token %#x.\n", iface, token);
1900 if (!token)
1901 return D3D_OK;
1903 wined3d_mutex_lock();
1904 stateblock = d3d8_get_object(&device->handle_table, token - 1, D3D8_HANDLE_SB);
1905 if (!stateblock)
1907 WARN("Invalid handle (%#x) passed.\n", token);
1908 wined3d_mutex_unlock();
1909 return D3DERR_INVALIDCALL;
1911 wined3d_stateblock_apply(stateblock);
1912 wined3d_mutex_unlock();
1914 return D3D_OK;
1917 static HRESULT WINAPI d3d8_device_CaptureStateBlock(IDirect3DDevice8 *iface, DWORD token)
1919 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1920 struct wined3d_stateblock *stateblock;
1922 TRACE("iface %p, token %#x.\n", iface, token);
1924 wined3d_mutex_lock();
1925 stateblock = d3d8_get_object(&device->handle_table, token - 1, D3D8_HANDLE_SB);
1926 if (!stateblock)
1928 WARN("Invalid handle (%#x) passed.\n", token);
1929 wined3d_mutex_unlock();
1930 return D3DERR_INVALIDCALL;
1932 wined3d_stateblock_capture(stateblock);
1933 wined3d_mutex_unlock();
1935 return D3D_OK;
1938 static HRESULT WINAPI d3d8_device_DeleteStateBlock(IDirect3DDevice8 *iface, DWORD token)
1940 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1941 struct wined3d_stateblock *stateblock;
1943 TRACE("iface %p, token %#x.\n", iface, token);
1945 wined3d_mutex_lock();
1946 stateblock = d3d8_free_handle(&device->handle_table, token - 1, D3D8_HANDLE_SB);
1948 if (!stateblock)
1950 WARN("Invalid handle (%#x) passed.\n", token);
1951 wined3d_mutex_unlock();
1952 return D3DERR_INVALIDCALL;
1955 if (wined3d_stateblock_decref(stateblock))
1957 ERR("Stateblock %p has references left, this shouldn't happen.\n", stateblock);
1959 wined3d_mutex_unlock();
1961 return D3D_OK;
1964 static HRESULT WINAPI d3d8_device_CreateStateBlock(IDirect3DDevice8 *iface,
1965 D3DSTATEBLOCKTYPE type, DWORD *handle)
1967 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1968 struct wined3d_stateblock *stateblock;
1969 HRESULT hr;
1971 TRACE("iface %p, type %#x, handle %p.\n", iface, type, handle);
1973 if (type != D3DSBT_ALL
1974 && type != D3DSBT_PIXELSTATE
1975 && type != D3DSBT_VERTEXSTATE)
1977 WARN("Unexpected stateblock type, returning D3DERR_INVALIDCALL\n");
1978 return D3DERR_INVALIDCALL;
1981 wined3d_mutex_lock();
1982 hr = wined3d_stateblock_create(device->wined3d_device, (enum wined3d_stateblock_type)type, &stateblock);
1983 if (FAILED(hr))
1985 wined3d_mutex_unlock();
1986 ERR("Failed to create the state block, hr %#x\n", hr);
1987 return hr;
1990 *handle = d3d8_allocate_handle(&device->handle_table, stateblock, D3D8_HANDLE_SB);
1991 wined3d_mutex_unlock();
1993 if (*handle == D3D8_INVALID_HANDLE)
1995 ERR("Failed to allocate a handle.\n");
1996 wined3d_mutex_lock();
1997 wined3d_stateblock_decref(stateblock);
1998 wined3d_mutex_unlock();
1999 return E_FAIL;
2001 ++*handle;
2003 TRACE("Returning %#x (%p).\n", *handle, stateblock);
2005 return hr;
2008 static HRESULT WINAPI d3d8_device_SetClipStatus(IDirect3DDevice8 *iface, const D3DCLIPSTATUS8 *clip_status)
2010 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2011 HRESULT hr;
2013 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
2014 /* FIXME: Verify that D3DCLIPSTATUS8 ~= struct wined3d_clip_status. */
2016 wined3d_mutex_lock();
2017 hr = wined3d_device_set_clip_status(device->wined3d_device, (const struct wined3d_clip_status *)clip_status);
2018 wined3d_mutex_unlock();
2020 return hr;
2023 static HRESULT WINAPI d3d8_device_GetClipStatus(IDirect3DDevice8 *iface, D3DCLIPSTATUS8 *clip_status)
2025 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2026 HRESULT hr;
2028 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
2030 wined3d_mutex_lock();
2031 hr = wined3d_device_get_clip_status(device->wined3d_device, (struct wined3d_clip_status *)clip_status);
2032 wined3d_mutex_unlock();
2034 return hr;
2037 static HRESULT WINAPI d3d8_device_GetTexture(IDirect3DDevice8 *iface, DWORD stage, IDirect3DBaseTexture8 **texture)
2039 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2040 struct wined3d_texture *wined3d_texture;
2041 struct d3d8_texture *texture_impl;
2043 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
2045 if (!texture)
2046 return D3DERR_INVALIDCALL;
2048 wined3d_mutex_lock();
2049 if ((wined3d_texture = wined3d_device_get_texture(device->wined3d_device, stage)))
2051 texture_impl = wined3d_texture_get_parent(wined3d_texture);
2052 *texture = &texture_impl->IDirect3DBaseTexture8_iface;
2053 IDirect3DBaseTexture8_AddRef(*texture);
2055 else
2057 *texture = NULL;
2059 wined3d_mutex_unlock();
2061 return D3D_OK;
2064 static HRESULT WINAPI d3d8_device_SetTexture(IDirect3DDevice8 *iface, DWORD stage, IDirect3DBaseTexture8 *texture)
2066 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2067 struct d3d8_texture *texture_impl;
2068 HRESULT hr;
2070 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
2072 texture_impl = unsafe_impl_from_IDirect3DBaseTexture8(texture);
2074 wined3d_mutex_lock();
2075 hr = wined3d_device_set_texture(device->wined3d_device, stage,
2076 texture_impl ? texture_impl->wined3d_texture : NULL);
2077 wined3d_mutex_unlock();
2079 return hr;
2082 static const struct tss_lookup
2084 BOOL sampler_state;
2085 union
2087 enum wined3d_texture_stage_state texture_state;
2088 enum wined3d_sampler_state sampler_state;
2089 } u;
2091 tss_lookup[] =
2093 {FALSE, {WINED3D_TSS_INVALID}}, /* 0, unused */
2094 {FALSE, {WINED3D_TSS_COLOR_OP}}, /* 1, D3DTSS_COLOROP */
2095 {FALSE, {WINED3D_TSS_COLOR_ARG1}}, /* 2, D3DTSS_COLORARG1 */
2096 {FALSE, {WINED3D_TSS_COLOR_ARG2}}, /* 3, D3DTSS_COLORARG2 */
2097 {FALSE, {WINED3D_TSS_ALPHA_OP}}, /* 4, D3DTSS_ALPHAOP */
2098 {FALSE, {WINED3D_TSS_ALPHA_ARG1}}, /* 5, D3DTSS_ALPHAARG1 */
2099 {FALSE, {WINED3D_TSS_ALPHA_ARG2}}, /* 6, D3DTSS_ALPHAARG2 */
2100 {FALSE, {WINED3D_TSS_BUMPENV_MAT00}}, /* 7, D3DTSS_BUMPENVMAT00 */
2101 {FALSE, {WINED3D_TSS_BUMPENV_MAT01}}, /* 8, D3DTSS_BUMPENVMAT01 */
2102 {FALSE, {WINED3D_TSS_BUMPENV_MAT10}}, /* 9, D3DTSS_BUMPENVMAT10 */
2103 {FALSE, {WINED3D_TSS_BUMPENV_MAT11}}, /* 10, D3DTSS_BUMPENVMAT11 */
2104 {FALSE, {WINED3D_TSS_TEXCOORD_INDEX}}, /* 11, D3DTSS_TEXCOORDINDEX */
2105 {FALSE, {WINED3D_TSS_INVALID}}, /* 12, unused */
2106 {TRUE, {WINED3D_SAMP_ADDRESS_U}}, /* 13, D3DTSS_ADDRESSU */
2107 {TRUE, {WINED3D_SAMP_ADDRESS_V}}, /* 14, D3DTSS_ADDRESSV */
2108 {TRUE, {WINED3D_SAMP_BORDER_COLOR}}, /* 15, D3DTSS_BORDERCOLOR */
2109 {TRUE, {WINED3D_SAMP_MAG_FILTER}}, /* 16, D3DTSS_MAGFILTER */
2110 {TRUE, {WINED3D_SAMP_MIN_FILTER}}, /* 17, D3DTSS_MINFILTER */
2111 {TRUE, {WINED3D_SAMP_MIP_FILTER}}, /* 18, D3DTSS_MIPFILTER */
2112 {TRUE, {WINED3D_SAMP_MIPMAP_LOD_BIAS}}, /* 19, D3DTSS_MIPMAPLODBIAS */
2113 {TRUE, {WINED3D_SAMP_MAX_MIP_LEVEL}}, /* 20, D3DTSS_MAXMIPLEVEL */
2114 {TRUE, {WINED3D_SAMP_MAX_ANISOTROPY}}, /* 21, D3DTSS_MAXANISOTROPY */
2115 {FALSE, {WINED3D_TSS_BUMPENV_LSCALE}}, /* 22, D3DTSS_BUMPENVLSCALE */
2116 {FALSE, {WINED3D_TSS_BUMPENV_LOFFSET}}, /* 23, D3DTSS_BUMPENVLOFFSET */
2117 {FALSE, {WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS}}, /* 24, D3DTSS_TEXTURETRANSFORMFLAGS */
2118 {TRUE, {WINED3D_SAMP_ADDRESS_W}}, /* 25, D3DTSS_ADDRESSW */
2119 {FALSE, {WINED3D_TSS_COLOR_ARG0}}, /* 26, D3DTSS_COLORARG0 */
2120 {FALSE, {WINED3D_TSS_ALPHA_ARG0}}, /* 27, D3DTSS_ALPHAARG0 */
2121 {FALSE, {WINED3D_TSS_RESULT_ARG}}, /* 28, D3DTSS_RESULTARG */
2124 static HRESULT WINAPI d3d8_device_GetTextureStageState(IDirect3DDevice8 *iface,
2125 DWORD stage, D3DTEXTURESTAGESTATETYPE Type, DWORD *value)
2127 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2128 const struct tss_lookup *l;
2130 TRACE("iface %p, stage %u, state %#x, value %p.\n", iface, stage, Type, value);
2132 if (Type >= ARRAY_SIZE(tss_lookup))
2134 WARN("Invalid Type %#x passed.\n", Type);
2135 return D3D_OK;
2138 l = &tss_lookup[Type];
2140 wined3d_mutex_lock();
2141 if (l->sampler_state)
2142 *value = wined3d_device_get_sampler_state(device->wined3d_device, stage, l->u.sampler_state);
2143 else
2144 *value = wined3d_device_get_texture_stage_state(device->wined3d_device, stage, l->u.texture_state);
2145 wined3d_mutex_unlock();
2147 return D3D_OK;
2150 static HRESULT WINAPI d3d8_device_SetTextureStageState(IDirect3DDevice8 *iface,
2151 DWORD stage, D3DTEXTURESTAGESTATETYPE type, DWORD value)
2153 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2154 const struct tss_lookup *l;
2156 TRACE("iface %p, stage %u, state %#x, value %#x.\n", iface, stage, type, value);
2158 if (type >= ARRAY_SIZE(tss_lookup))
2160 WARN("Invalid type %#x passed.\n", type);
2161 return D3D_OK;
2164 l = &tss_lookup[type];
2166 wined3d_mutex_lock();
2167 if (l->sampler_state)
2168 wined3d_device_set_sampler_state(device->wined3d_device, stage, l->u.sampler_state, value);
2169 else
2170 wined3d_device_set_texture_stage_state(device->wined3d_device, stage, l->u.texture_state, value);
2171 wined3d_mutex_unlock();
2173 return D3D_OK;
2176 static HRESULT WINAPI d3d8_device_ValidateDevice(IDirect3DDevice8 *iface, DWORD *pass_count)
2178 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2179 HRESULT hr;
2181 TRACE("iface %p, pass_count %p.\n", iface, pass_count);
2183 wined3d_mutex_lock();
2184 hr = wined3d_device_validate_device(device->wined3d_device, pass_count);
2185 wined3d_mutex_unlock();
2187 return hr;
2190 static HRESULT WINAPI d3d8_device_GetInfo(IDirect3DDevice8 *iface,
2191 DWORD info_id, void *info, DWORD info_size)
2193 TRACE("iface %p, info_id %#x, info %p, info_size %u.\n", iface, info_id, info, info_size);
2195 if (info_id < 4)
2196 return E_FAIL;
2197 return S_FALSE;
2200 static HRESULT WINAPI d3d8_device_SetPaletteEntries(IDirect3DDevice8 *iface,
2201 UINT palette_idx, const PALETTEENTRY *entries)
2203 WARN("iface %p, palette_idx %u, entries %p unimplemented\n", iface, palette_idx, entries);
2205 /* GPUs stopped supporting palettized textures with the Shader Model 1 generation. Wined3d
2206 * does not have a d3d8/9-style palette API */
2208 return D3D_OK;
2211 static HRESULT WINAPI d3d8_device_GetPaletteEntries(IDirect3DDevice8 *iface,
2212 UINT palette_idx, PALETTEENTRY *entries)
2214 FIXME("iface %p, palette_idx %u, entries %p unimplemented.\n", iface, palette_idx, entries);
2216 return D3DERR_INVALIDCALL;
2219 static HRESULT WINAPI d3d8_device_SetCurrentTexturePalette(IDirect3DDevice8 *iface, UINT palette_idx)
2221 WARN("iface %p, palette_idx %u unimplemented.\n", iface, palette_idx);
2223 return D3D_OK;
2226 static HRESULT WINAPI d3d8_device_GetCurrentTexturePalette(IDirect3DDevice8 *iface, UINT *palette_idx)
2228 FIXME("iface %p, palette_idx %p unimplemented.\n", iface, palette_idx);
2230 return D3DERR_INVALIDCALL;
2233 static HRESULT WINAPI d3d8_device_DrawPrimitive(IDirect3DDevice8 *iface,
2234 D3DPRIMITIVETYPE primitive_type, UINT start_vertex, UINT primitive_count)
2236 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2237 HRESULT hr;
2239 TRACE("iface %p, primitive_type %#x, start_vertex %u, primitive_count %u.\n",
2240 iface, primitive_type, start_vertex, primitive_count);
2242 wined3d_mutex_lock();
2243 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type, 0);
2244 hr = wined3d_device_draw_primitive(device->wined3d_device, start_vertex,
2245 vertex_count_from_primitive_count(primitive_type, primitive_count));
2246 wined3d_mutex_unlock();
2248 return hr;
2251 static HRESULT WINAPI d3d8_device_DrawIndexedPrimitive(IDirect3DDevice8 *iface,
2252 D3DPRIMITIVETYPE primitive_type, UINT min_vertex_idx, UINT vertex_count,
2253 UINT start_idx, UINT primitive_count)
2255 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2256 HRESULT hr;
2258 TRACE("iface %p, primitive_type %#x, min_vertex_idx %u, vertex_count %u, start_idx %u, primitive_count %u.\n",
2259 iface, primitive_type, min_vertex_idx, vertex_count, start_idx, primitive_count);
2261 wined3d_mutex_lock();
2262 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type, 0);
2263 hr = wined3d_device_draw_indexed_primitive(device->wined3d_device, start_idx,
2264 vertex_count_from_primitive_count(primitive_type, primitive_count));
2265 wined3d_mutex_unlock();
2267 return hr;
2270 /* The caller is responsible for wined3d locking */
2271 static HRESULT d3d8_device_prepare_vertex_buffer(struct d3d8_device *device, UINT min_size)
2273 HRESULT hr;
2275 if (device->vertex_buffer_size < min_size || !device->vertex_buffer)
2277 UINT size = max(device->vertex_buffer_size * 2, min_size);
2278 struct wined3d_buffer_desc desc;
2279 struct wined3d_buffer *buffer;
2281 TRACE("Growing vertex buffer to %u bytes\n", size);
2283 desc.byte_width = size;
2284 desc.usage = WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_WRITEONLY;
2285 desc.bind_flags = WINED3D_BIND_VERTEX_BUFFER;
2286 desc.access = WINED3D_RESOURCE_ACCESS_GPU | WINED3D_RESOURCE_ACCESS_MAP_R | WINED3D_RESOURCE_ACCESS_MAP_W;
2287 desc.misc_flags = 0;
2288 desc.structure_byte_stride = 0;
2290 if (FAILED(hr = wined3d_buffer_create(device->wined3d_device, &desc,
2291 NULL, NULL, &d3d8_null_wined3d_parent_ops, &buffer)))
2293 ERR("Failed to create vertex buffer, hr %#x.\n", hr);
2294 return hr;
2297 if (device->vertex_buffer)
2298 wined3d_buffer_decref(device->vertex_buffer);
2300 device->vertex_buffer = buffer;
2301 device->vertex_buffer_size = size;
2302 device->vertex_buffer_pos = 0;
2304 return D3D_OK;
2307 static HRESULT WINAPI d3d8_device_DrawPrimitiveUP(IDirect3DDevice8 *iface,
2308 D3DPRIMITIVETYPE primitive_type, UINT primitive_count, const void *data,
2309 UINT stride)
2311 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2312 HRESULT hr;
2313 UINT vtx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2314 struct wined3d_map_desc wined3d_map_desc;
2315 struct wined3d_box wined3d_box = {0};
2316 UINT size = vtx_count * stride;
2317 struct wined3d_resource *vb;
2318 UINT vb_pos, align;
2320 TRACE("iface %p, primitive_type %#x, primitive_count %u, data %p, stride %u.\n",
2321 iface, primitive_type, primitive_count, data, stride);
2323 if (!primitive_count)
2325 WARN("primitive_count is 0, returning D3D_OK\n");
2326 return D3D_OK;
2329 wined3d_mutex_lock();
2330 hr = d3d8_device_prepare_vertex_buffer(device, size);
2331 if (FAILED(hr))
2332 goto done;
2334 vb_pos = device->vertex_buffer_pos;
2335 align = vb_pos % stride;
2336 if (align) align = stride - align;
2337 if (vb_pos + size + align > device->vertex_buffer_size)
2338 vb_pos = 0;
2339 else
2340 vb_pos += align;
2342 wined3d_box.left = vb_pos;
2343 wined3d_box.right = vb_pos + size;
2344 vb = wined3d_buffer_get_resource(device->vertex_buffer);
2345 if (FAILED(wined3d_resource_map(vb, 0, &wined3d_map_desc, &wined3d_box,
2346 WINED3D_MAP_WRITE | (vb_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD))))
2347 goto done;
2348 memcpy(wined3d_map_desc.data, data, size);
2349 wined3d_resource_unmap(vb, 0);
2350 device->vertex_buffer_pos = vb_pos + size;
2352 hr = wined3d_device_set_stream_source(device->wined3d_device, 0, device->vertex_buffer, 0, stride);
2353 if (FAILED(hr))
2354 goto done;
2356 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type, 0);
2357 hr = wined3d_device_draw_primitive(device->wined3d_device, vb_pos / stride, vtx_count);
2358 wined3d_device_set_stream_source(device->wined3d_device, 0, NULL, 0, 0);
2360 done:
2361 wined3d_mutex_unlock();
2362 return hr;
2365 /* The caller is responsible for wined3d locking */
2366 static HRESULT d3d8_device_prepare_index_buffer(struct d3d8_device *device, UINT min_size)
2368 HRESULT hr;
2370 if (device->index_buffer_size < min_size || !device->index_buffer)
2372 UINT size = max(device->index_buffer_size * 2, min_size);
2373 struct wined3d_buffer_desc desc;
2374 struct wined3d_buffer *buffer;
2376 TRACE("Growing index buffer to %u bytes\n", size);
2378 desc.byte_width = size;
2379 desc.usage = WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_WRITEONLY | WINED3DUSAGE_STATICDECL;
2380 desc.bind_flags = WINED3D_BIND_INDEX_BUFFER;
2381 desc.access = WINED3D_RESOURCE_ACCESS_GPU | WINED3D_RESOURCE_ACCESS_MAP_R | WINED3D_RESOURCE_ACCESS_MAP_W;
2382 desc.misc_flags = 0;
2383 desc.structure_byte_stride = 0;
2385 if (FAILED(hr = wined3d_buffer_create(device->wined3d_device, &desc,
2386 NULL, NULL, &d3d8_null_wined3d_parent_ops, &buffer)))
2388 ERR("Failed to create index buffer, hr %#x.\n", hr);
2389 return hr;
2392 if (device->index_buffer)
2393 wined3d_buffer_decref(device->index_buffer);
2395 device->index_buffer = buffer;
2396 device->index_buffer_size = size;
2397 device->index_buffer_pos = 0;
2399 return D3D_OK;
2402 static HRESULT WINAPI d3d8_device_DrawIndexedPrimitiveUP(IDirect3DDevice8 *iface,
2403 D3DPRIMITIVETYPE primitive_type, UINT min_vertex_idx, UINT vertex_count,
2404 UINT primitive_count, const void *index_data, D3DFORMAT index_format,
2405 const void *vertex_data, UINT vertex_stride)
2407 UINT idx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2408 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2409 UINT idx_fmt_size = index_format == D3DFMT_INDEX16 ? 2 : 4;
2410 UINT vtx_size = vertex_count * vertex_stride;
2411 UINT idx_size = idx_count * idx_fmt_size;
2412 struct wined3d_map_desc wined3d_map_desc;
2413 struct wined3d_box wined3d_box = {0};
2414 struct wined3d_resource *ib, *vb;
2415 UINT vb_pos, ib_pos, align;
2416 HRESULT hr;
2418 TRACE("iface %p, primitive_type %#x, min_vertex_idx %u, vertex_count %u, primitive_count %u, "
2419 "index_data %p, index_format %#x, vertex_data %p, vertex_stride %u.\n",
2420 iface, primitive_type, min_vertex_idx, vertex_count, primitive_count,
2421 index_data, index_format, vertex_data, vertex_stride);
2423 if (!primitive_count)
2425 WARN("primitive_count is 0, returning D3D_OK\n");
2426 return D3D_OK;
2429 wined3d_mutex_lock();
2431 hr = d3d8_device_prepare_vertex_buffer(device, vtx_size);
2432 if (FAILED(hr))
2433 goto done;
2435 vb_pos = device->vertex_buffer_pos;
2436 align = vb_pos % vertex_stride;
2437 if (align) align = vertex_stride - align;
2438 if (vb_pos + vtx_size + align > device->vertex_buffer_size)
2439 vb_pos = 0;
2440 else
2441 vb_pos += align;
2443 wined3d_box.left = vb_pos;
2444 wined3d_box.right = vb_pos + vtx_size;
2445 vb = wined3d_buffer_get_resource(device->vertex_buffer);
2446 if (FAILED(hr = wined3d_resource_map(vb, 0, &wined3d_map_desc, &wined3d_box,
2447 WINED3D_MAP_WRITE | (vb_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD))))
2448 goto done;
2449 memcpy(wined3d_map_desc.data, (char *)vertex_data + min_vertex_idx * vertex_stride, vtx_size);
2450 wined3d_resource_unmap(vb, 0);
2451 device->vertex_buffer_pos = vb_pos + vtx_size;
2453 hr = d3d8_device_prepare_index_buffer(device, idx_size);
2454 if (FAILED(hr))
2455 goto done;
2457 ib_pos = device->index_buffer_pos;
2458 align = ib_pos % idx_fmt_size;
2459 if (align) align = idx_fmt_size - align;
2460 if (ib_pos + idx_size + align > device->index_buffer_size)
2461 ib_pos = 0;
2462 else
2463 ib_pos += align;
2465 wined3d_box.left = ib_pos;
2466 wined3d_box.right = ib_pos + idx_size;
2467 ib = wined3d_buffer_get_resource(device->index_buffer);
2468 if (FAILED(hr = wined3d_resource_map(ib, 0, &wined3d_map_desc, &wined3d_box,
2469 WINED3D_MAP_WRITE | (ib_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD))))
2470 goto done;
2471 memcpy(wined3d_map_desc.data, index_data, idx_size);
2472 wined3d_resource_unmap(ib, 0);
2473 device->index_buffer_pos = ib_pos + idx_size;
2475 hr = wined3d_device_set_stream_source(device->wined3d_device, 0, device->vertex_buffer, 0, vertex_stride);
2476 if (FAILED(hr))
2477 goto done;
2479 wined3d_device_set_index_buffer(device->wined3d_device, device->index_buffer,
2480 wined3dformat_from_d3dformat(index_format), 0);
2481 wined3d_device_set_base_vertex_index(device->wined3d_device, vb_pos / vertex_stride - min_vertex_idx);
2483 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type, 0);
2484 hr = wined3d_device_draw_indexed_primitive(device->wined3d_device, ib_pos / idx_fmt_size, idx_count);
2486 wined3d_device_set_stream_source(device->wined3d_device, 0, NULL, 0, 0);
2487 wined3d_device_set_index_buffer(device->wined3d_device, NULL, WINED3DFMT_UNKNOWN, 0);
2488 wined3d_device_set_base_vertex_index(device->wined3d_device, 0);
2490 done:
2491 wined3d_mutex_unlock();
2492 return hr;
2495 static HRESULT WINAPI d3d8_device_ProcessVertices(IDirect3DDevice8 *iface, UINT src_start_idx,
2496 UINT dst_idx, UINT vertex_count, IDirect3DVertexBuffer8 *dst_buffer, DWORD flags)
2498 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2499 struct d3d8_vertexbuffer *dst = unsafe_impl_from_IDirect3DVertexBuffer8(dst_buffer);
2500 HRESULT hr;
2502 TRACE("iface %p, src_start_idx %u, dst_idx %u, vertex_count %u, dst_buffer %p, flags %#x.\n",
2503 iface, src_start_idx, dst_idx, vertex_count, dst_buffer, flags);
2505 wined3d_mutex_lock();
2506 hr = wined3d_device_process_vertices(device->wined3d_device, src_start_idx, dst_idx,
2507 vertex_count, dst->wined3d_buffer, NULL, flags, dst->fvf);
2508 wined3d_mutex_unlock();
2510 return hr;
2513 static HRESULT WINAPI d3d8_device_CreateVertexShader(IDirect3DDevice8 *iface,
2514 const DWORD *declaration, const DWORD *byte_code, DWORD *shader, DWORD usage)
2516 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2517 struct d3d8_vertex_shader *object;
2518 DWORD shader_handle;
2519 DWORD handle;
2520 HRESULT hr;
2522 TRACE("iface %p, declaration %p, byte_code %p, shader %p, usage %#x.\n",
2523 iface, declaration, byte_code, shader, usage);
2525 if (!(object = heap_alloc_zero(sizeof(*object))))
2527 *shader = 0;
2528 return E_OUTOFMEMORY;
2531 wined3d_mutex_lock();
2532 handle = d3d8_allocate_handle(&device->handle_table, object, D3D8_HANDLE_VS);
2533 wined3d_mutex_unlock();
2534 if (handle == D3D8_INVALID_HANDLE)
2536 ERR("Failed to allocate vertex shader handle.\n");
2537 heap_free(object);
2538 *shader = 0;
2539 return E_OUTOFMEMORY;
2542 shader_handle = handle + VS_HIGHESTFIXEDFXF + 1;
2544 hr = d3d8_vertex_shader_init(object, device, declaration, byte_code, shader_handle, usage);
2545 if (FAILED(hr))
2547 WARN("Failed to initialize vertex shader, hr %#x.\n", hr);
2548 wined3d_mutex_lock();
2549 d3d8_free_handle(&device->handle_table, handle, D3D8_HANDLE_VS);
2550 wined3d_mutex_unlock();
2551 heap_free(object);
2552 *shader = 0;
2553 return hr;
2556 TRACE("Created vertex shader %p (handle %#x).\n", object, shader_handle);
2557 *shader = shader_handle;
2559 return D3D_OK;
2562 static struct d3d8_vertex_declaration *d3d8_device_get_fvf_declaration(struct d3d8_device *device, DWORD fvf)
2564 struct d3d8_vertex_declaration *d3d8_declaration;
2565 struct FvfToDecl *convertedDecls = device->decls;
2566 int p, low, high; /* deliberately signed */
2567 HRESULT hr;
2569 TRACE("Searching for declaration for fvf %08x... ", fvf);
2571 low = 0;
2572 high = device->numConvertedDecls - 1;
2573 while (low <= high)
2575 p = (low + high) >> 1;
2576 TRACE("%d ", p);
2578 if (convertedDecls[p].fvf == fvf)
2580 TRACE("found %p\n", convertedDecls[p].declaration);
2581 return convertedDecls[p].declaration;
2584 if (convertedDecls[p].fvf < fvf)
2585 low = p + 1;
2586 else
2587 high = p - 1;
2589 TRACE("not found. Creating and inserting at position %d.\n", low);
2591 if (!(d3d8_declaration = heap_alloc(sizeof(*d3d8_declaration))))
2592 return NULL;
2594 if (FAILED(hr = d3d8_vertex_declaration_init_fvf(d3d8_declaration, device, fvf)))
2596 WARN("Failed to initialize vertex declaration, hr %#x.\n", hr);
2597 heap_free(d3d8_declaration);
2598 return NULL;
2601 if (device->declArraySize == device->numConvertedDecls)
2603 UINT grow = device->declArraySize / 2;
2605 if (!(convertedDecls = heap_realloc(convertedDecls,
2606 sizeof(*convertedDecls) * (device->numConvertedDecls + grow))))
2608 d3d8_vertex_declaration_destroy(d3d8_declaration);
2609 return NULL;
2611 device->decls = convertedDecls;
2612 device->declArraySize += grow;
2615 memmove(convertedDecls + low + 1, convertedDecls + low,
2616 sizeof(*convertedDecls) * (device->numConvertedDecls - low));
2617 convertedDecls[low].declaration = d3d8_declaration;
2618 convertedDecls[low].fvf = fvf;
2619 ++device->numConvertedDecls;
2621 TRACE("Returning %p. %u decls in array.\n", d3d8_declaration, device->numConvertedDecls);
2623 return d3d8_declaration;
2626 static HRESULT WINAPI d3d8_device_SetVertexShader(IDirect3DDevice8 *iface, DWORD shader)
2628 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2629 struct d3d8_vertex_shader *shader_impl;
2631 TRACE("iface %p, shader %#x.\n", iface, shader);
2633 if (VS_HIGHESTFIXEDFXF >= shader)
2635 TRACE("Setting FVF, %#x\n", shader);
2637 wined3d_mutex_lock();
2638 wined3d_device_set_vertex_declaration(device->wined3d_device,
2639 d3d8_device_get_fvf_declaration(device, shader)->wined3d_vertex_declaration);
2640 wined3d_device_set_vertex_shader(device->wined3d_device, NULL);
2641 wined3d_mutex_unlock();
2643 return D3D_OK;
2646 TRACE("Setting shader\n");
2648 wined3d_mutex_lock();
2649 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS)))
2651 WARN("Invalid handle (%#x) passed.\n", shader);
2652 wined3d_mutex_unlock();
2654 return D3DERR_INVALIDCALL;
2657 wined3d_device_set_vertex_declaration(device->wined3d_device,
2658 shader_impl->vertex_declaration->wined3d_vertex_declaration);
2659 wined3d_device_set_vertex_shader(device->wined3d_device, shader_impl->wined3d_shader);
2660 wined3d_mutex_unlock();
2662 return D3D_OK;
2665 static HRESULT WINAPI d3d8_device_GetVertexShader(IDirect3DDevice8 *iface, DWORD *shader)
2667 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2668 struct wined3d_vertex_declaration *wined3d_declaration;
2669 struct d3d8_vertex_declaration *d3d8_declaration;
2671 TRACE("iface %p, shader %p.\n", iface, shader);
2673 wined3d_mutex_lock();
2674 if ((wined3d_declaration = wined3d_device_get_vertex_declaration(device->wined3d_device)))
2676 d3d8_declaration = wined3d_vertex_declaration_get_parent(wined3d_declaration);
2677 *shader = d3d8_declaration->shader_handle;
2679 else
2681 *shader = 0;
2683 wined3d_mutex_unlock();
2685 TRACE("Returning %#x.\n", *shader);
2687 return D3D_OK;
2690 static HRESULT WINAPI d3d8_device_DeleteVertexShader(IDirect3DDevice8 *iface, DWORD shader)
2692 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2693 struct d3d8_vertex_shader *shader_impl;
2695 TRACE("iface %p, shader %#x.\n", iface, shader);
2697 wined3d_mutex_lock();
2698 if (!(shader_impl = d3d8_free_handle(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS)))
2700 WARN("Invalid handle (%#x) passed.\n", shader);
2701 wined3d_mutex_unlock();
2703 return D3DERR_INVALIDCALL;
2706 if (shader_impl->wined3d_shader
2707 && wined3d_device_get_vertex_shader(device->wined3d_device) == shader_impl->wined3d_shader)
2708 IDirect3DDevice8_SetVertexShader(iface, 0);
2710 wined3d_mutex_unlock();
2712 d3d8_vertex_shader_destroy(shader_impl);
2714 return D3D_OK;
2717 static HRESULT WINAPI d3d8_device_SetVertexShaderConstant(IDirect3DDevice8 *iface,
2718 DWORD start_register, const void *data, DWORD count)
2720 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2721 HRESULT hr;
2723 TRACE("iface %p, start_register %u, data %p, count %u.\n",
2724 iface, start_register, data, count);
2726 if (start_register + count > D3D8_MAX_VERTEX_SHADER_CONSTANTF)
2728 WARN("Trying to access %u constants, but d3d8 only supports %u\n",
2729 start_register + count, D3D8_MAX_VERTEX_SHADER_CONSTANTF);
2730 return D3DERR_INVALIDCALL;
2733 wined3d_mutex_lock();
2734 hr = wined3d_device_set_vs_consts_f(device->wined3d_device, start_register, count, data);
2735 wined3d_mutex_unlock();
2737 return hr;
2740 static HRESULT WINAPI d3d8_device_GetVertexShaderConstant(IDirect3DDevice8 *iface,
2741 DWORD start_register, void *data, DWORD count)
2743 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2744 HRESULT hr;
2746 TRACE("iface %p, start_register %u, data %p, count %u.\n",
2747 iface, start_register, data, count);
2749 if (start_register + count > D3D8_MAX_VERTEX_SHADER_CONSTANTF)
2751 WARN("Trying to access %u constants, but d3d8 only supports %u\n",
2752 start_register + count, D3D8_MAX_VERTEX_SHADER_CONSTANTF);
2753 return D3DERR_INVALIDCALL;
2756 wined3d_mutex_lock();
2757 hr = wined3d_device_get_vs_consts_f(device->wined3d_device, start_register, count, data);
2758 wined3d_mutex_unlock();
2760 return hr;
2763 static HRESULT WINAPI d3d8_device_GetVertexShaderDeclaration(IDirect3DDevice8 *iface,
2764 DWORD shader, void *data, DWORD *data_size)
2766 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2767 struct d3d8_vertex_declaration *declaration;
2768 struct d3d8_vertex_shader *shader_impl;
2770 TRACE("iface %p, shader %#x, data %p, data_size %p.\n",
2771 iface, shader, data, data_size);
2773 wined3d_mutex_lock();
2774 shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS);
2775 wined3d_mutex_unlock();
2777 if (!shader_impl)
2779 WARN("Invalid handle (%#x) passed.\n", shader);
2780 return D3DERR_INVALIDCALL;
2782 declaration = shader_impl->vertex_declaration;
2784 if (!data)
2786 *data_size = declaration->elements_size;
2787 return D3D_OK;
2790 /* MSDN claims that if *data_size is smaller than the required size
2791 * we should write the required size and return D3DERR_MOREDATA.
2792 * That's not actually true. */
2793 if (*data_size < declaration->elements_size)
2794 return D3DERR_INVALIDCALL;
2796 memcpy(data, declaration->elements, declaration->elements_size);
2798 return D3D_OK;
2801 static HRESULT WINAPI d3d8_device_GetVertexShaderFunction(IDirect3DDevice8 *iface,
2802 DWORD shader, void *data, DWORD *data_size)
2804 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2805 struct d3d8_vertex_shader *shader_impl = NULL;
2806 HRESULT hr;
2808 TRACE("iface %p, shader %#x, data %p, data_size %p.\n",
2809 iface, shader, data, data_size);
2811 wined3d_mutex_lock();
2812 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS)))
2814 WARN("Invalid handle (%#x) passed.\n", shader);
2815 wined3d_mutex_unlock();
2817 return D3DERR_INVALIDCALL;
2820 if (!shader_impl->wined3d_shader)
2822 wined3d_mutex_unlock();
2823 *data_size = 0;
2824 return D3D_OK;
2827 hr = wined3d_shader_get_byte_code(shader_impl->wined3d_shader, data, data_size);
2828 wined3d_mutex_unlock();
2830 return hr;
2833 static HRESULT WINAPI d3d8_device_SetIndices(IDirect3DDevice8 *iface,
2834 IDirect3DIndexBuffer8 *buffer, UINT base_vertex_idx)
2836 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2837 struct d3d8_indexbuffer *ib = unsafe_impl_from_IDirect3DIndexBuffer8(buffer);
2839 TRACE("iface %p, buffer %p, base_vertex_idx %u.\n", iface, buffer, base_vertex_idx);
2841 /* WineD3D takes an INT(due to d3d9), but d3d8 uses UINTs. Do I have to add a check here that
2842 * the UINT doesn't cause an overflow in the INT? It seems rather unlikely because such large
2843 * vertex buffers can't be created to address them with an index that requires the 32nd bit
2844 * (4 Byte minimum vertex size * 2^31-1 -> 8 gb buffer. The index sign would be the least
2845 * problem)
2847 wined3d_mutex_lock();
2848 wined3d_device_set_base_vertex_index(device->wined3d_device, base_vertex_idx);
2849 wined3d_device_set_index_buffer(device->wined3d_device,
2850 ib ? ib->wined3d_buffer : NULL, ib ? ib->format : WINED3DFMT_UNKNOWN, 0);
2851 wined3d_mutex_unlock();
2853 return D3D_OK;
2856 static HRESULT WINAPI d3d8_device_GetIndices(IDirect3DDevice8 *iface,
2857 IDirect3DIndexBuffer8 **buffer, UINT *base_vertex_index)
2859 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2860 enum wined3d_format_id wined3d_format;
2861 struct wined3d_buffer *wined3d_buffer;
2862 struct d3d8_indexbuffer *buffer_impl;
2864 TRACE("iface %p, buffer %p, base_vertex_index %p.\n", iface, buffer, base_vertex_index);
2866 if (!buffer)
2867 return D3DERR_INVALIDCALL;
2869 /* The case from UINT to INT is safe because d3d8 will never set negative values */
2870 wined3d_mutex_lock();
2871 *base_vertex_index = wined3d_device_get_base_vertex_index(device->wined3d_device);
2872 if ((wined3d_buffer = wined3d_device_get_index_buffer(device->wined3d_device, &wined3d_format, NULL)))
2874 buffer_impl = wined3d_buffer_get_parent(wined3d_buffer);
2875 *buffer = &buffer_impl->IDirect3DIndexBuffer8_iface;
2876 IDirect3DIndexBuffer8_AddRef(*buffer);
2878 else
2880 *buffer = NULL;
2882 wined3d_mutex_unlock();
2884 return D3D_OK;
2887 static HRESULT WINAPI d3d8_device_CreatePixelShader(IDirect3DDevice8 *iface,
2888 const DWORD *byte_code, DWORD *shader)
2890 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2891 struct d3d8_pixel_shader *object;
2892 DWORD shader_handle;
2893 DWORD handle;
2894 HRESULT hr;
2896 TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
2898 if (!shader)
2899 return D3DERR_INVALIDCALL;
2901 if (!(object = heap_alloc_zero(sizeof(*object))))
2902 return E_OUTOFMEMORY;
2904 wined3d_mutex_lock();
2905 handle = d3d8_allocate_handle(&device->handle_table, object, D3D8_HANDLE_PS);
2906 wined3d_mutex_unlock();
2907 if (handle == D3D8_INVALID_HANDLE)
2909 ERR("Failed to allocate pixel shader handle.\n");
2910 heap_free(object);
2911 return E_OUTOFMEMORY;
2914 shader_handle = handle + VS_HIGHESTFIXEDFXF + 1;
2916 hr = d3d8_pixel_shader_init(object, device, byte_code, shader_handle);
2917 if (FAILED(hr))
2919 WARN("Failed to initialize pixel shader, hr %#x.\n", hr);
2920 wined3d_mutex_lock();
2921 d3d8_free_handle(&device->handle_table, handle, D3D8_HANDLE_PS);
2922 wined3d_mutex_unlock();
2923 heap_free(object);
2924 *shader = 0;
2925 return hr;
2928 TRACE("Created pixel shader %p (handle %#x).\n", object, shader_handle);
2929 *shader = shader_handle;
2931 return D3D_OK;
2934 static HRESULT WINAPI d3d8_device_SetPixelShader(IDirect3DDevice8 *iface, DWORD shader)
2936 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2937 struct d3d8_pixel_shader *shader_impl;
2939 TRACE("iface %p, shader %#x.\n", iface, shader);
2941 wined3d_mutex_lock();
2943 if (!shader)
2945 wined3d_device_set_pixel_shader(device->wined3d_device, NULL);
2946 wined3d_mutex_unlock();
2947 return D3D_OK;
2950 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS)))
2952 WARN("Invalid handle (%#x) passed.\n", shader);
2953 wined3d_mutex_unlock();
2954 return D3DERR_INVALIDCALL;
2957 TRACE("Setting shader %p.\n", shader_impl);
2958 wined3d_device_set_pixel_shader(device->wined3d_device, shader_impl->wined3d_shader);
2959 wined3d_mutex_unlock();
2961 return D3D_OK;
2964 static HRESULT WINAPI d3d8_device_GetPixelShader(IDirect3DDevice8 *iface, DWORD *shader)
2966 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2967 struct wined3d_shader *object;
2969 TRACE("iface %p, shader %p.\n", iface, shader);
2971 if (!shader)
2972 return D3DERR_INVALIDCALL;
2974 wined3d_mutex_lock();
2975 if ((object = wined3d_device_get_pixel_shader(device->wined3d_device)))
2977 struct d3d8_pixel_shader *d3d8_shader;
2978 d3d8_shader = wined3d_shader_get_parent(object);
2979 *shader = d3d8_shader->handle;
2981 else
2983 *shader = 0;
2985 wined3d_mutex_unlock();
2987 TRACE("Returning %#x.\n", *shader);
2989 return D3D_OK;
2992 static HRESULT WINAPI d3d8_device_DeletePixelShader(IDirect3DDevice8 *iface, DWORD shader)
2994 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2995 struct d3d8_pixel_shader *shader_impl;
2997 TRACE("iface %p, shader %#x.\n", iface, shader);
2999 wined3d_mutex_lock();
3001 if (!(shader_impl = d3d8_free_handle(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS)))
3003 WARN("Invalid handle (%#x) passed.\n", shader);
3004 wined3d_mutex_unlock();
3005 return D3DERR_INVALIDCALL;
3008 if (wined3d_device_get_pixel_shader(device->wined3d_device) == shader_impl->wined3d_shader)
3009 IDirect3DDevice8_SetPixelShader(iface, 0);
3011 wined3d_mutex_unlock();
3013 d3d8_pixel_shader_destroy(shader_impl);
3015 return D3D_OK;
3018 static HRESULT WINAPI d3d8_device_SetPixelShaderConstant(IDirect3DDevice8 *iface,
3019 DWORD start_register, const void *data, DWORD count)
3021 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3022 HRESULT hr;
3024 TRACE("iface %p, start_register %u, data %p, count %u.\n",
3025 iface, start_register, data, count);
3027 wined3d_mutex_lock();
3028 hr = wined3d_device_set_ps_consts_f(device->wined3d_device, start_register, count, data);
3029 wined3d_mutex_unlock();
3031 return hr;
3034 static HRESULT WINAPI d3d8_device_GetPixelShaderConstant(IDirect3DDevice8 *iface,
3035 DWORD start_register, void *data, DWORD count)
3037 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3038 HRESULT hr;
3040 TRACE("iface %p, start_register %u, data %p, count %u.\n",
3041 iface, start_register, data, count);
3043 wined3d_mutex_lock();
3044 hr = wined3d_device_get_ps_consts_f(device->wined3d_device, start_register, count, data);
3045 wined3d_mutex_unlock();
3047 return hr;
3050 static HRESULT WINAPI d3d8_device_GetPixelShaderFunction(IDirect3DDevice8 *iface,
3051 DWORD shader, void *data, DWORD *data_size)
3053 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3054 struct d3d8_pixel_shader *shader_impl = NULL;
3055 HRESULT hr;
3057 TRACE("iface %p, shader %#x, data %p, data_size %p.\n",
3058 iface, shader, data, data_size);
3060 wined3d_mutex_lock();
3061 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS)))
3063 WARN("Invalid handle (%#x) passed.\n", shader);
3064 wined3d_mutex_unlock();
3066 return D3DERR_INVALIDCALL;
3069 hr = wined3d_shader_get_byte_code(shader_impl->wined3d_shader, data, data_size);
3070 wined3d_mutex_unlock();
3072 return hr;
3075 static HRESULT WINAPI d3d8_device_DrawRectPatch(IDirect3DDevice8 *iface, UINT handle,
3076 const float *segment_count, const D3DRECTPATCH_INFO *patch_info)
3078 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
3079 iface, handle, segment_count, patch_info);
3080 return D3D_OK;
3083 static HRESULT WINAPI d3d8_device_DrawTriPatch(IDirect3DDevice8 *iface, UINT handle,
3084 const float *segment_count, const D3DTRIPATCH_INFO *patch_info)
3086 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
3087 iface, handle, segment_count, patch_info);
3088 return D3D_OK;
3091 static HRESULT WINAPI d3d8_device_DeletePatch(IDirect3DDevice8 *iface, UINT handle)
3093 FIXME("iface %p, handle %#x unimplemented.\n", iface, handle);
3094 return D3DERR_INVALIDCALL;
3097 static HRESULT WINAPI d3d8_device_SetStreamSource(IDirect3DDevice8 *iface,
3098 UINT stream_idx, IDirect3DVertexBuffer8 *buffer, UINT stride)
3100 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3101 struct d3d8_vertexbuffer *buffer_impl = unsafe_impl_from_IDirect3DVertexBuffer8(buffer);
3102 HRESULT hr;
3104 TRACE("iface %p, stream_idx %u, buffer %p, stride %u.\n",
3105 iface, stream_idx, buffer, stride);
3107 wined3d_mutex_lock();
3108 if (!stride)
3110 struct wined3d_buffer *wined3d_buffer;
3111 unsigned int cur_offset;
3113 hr = wined3d_device_get_stream_source(device->wined3d_device, stream_idx, &wined3d_buffer,
3114 &cur_offset, &stride);
3116 hr = wined3d_device_set_stream_source(device->wined3d_device, stream_idx,
3117 buffer_impl ? buffer_impl->wined3d_buffer : NULL, 0, stride);
3118 wined3d_mutex_unlock();
3120 return hr;
3123 static HRESULT WINAPI d3d8_device_GetStreamSource(IDirect3DDevice8 *iface,
3124 UINT stream_idx, IDirect3DVertexBuffer8 **buffer, UINT *stride)
3126 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3127 struct d3d8_vertexbuffer *buffer_impl;
3128 struct wined3d_buffer *wined3d_buffer = NULL;
3129 HRESULT hr;
3131 TRACE("iface %p, stream_idx %u, buffer %p, stride %p.\n",
3132 iface, stream_idx, buffer, stride);
3134 if (!buffer)
3135 return D3DERR_INVALIDCALL;
3137 wined3d_mutex_lock();
3138 hr = wined3d_device_get_stream_source(device->wined3d_device, stream_idx, &wined3d_buffer, 0, stride);
3139 if (SUCCEEDED(hr) && wined3d_buffer)
3141 buffer_impl = wined3d_buffer_get_parent(wined3d_buffer);
3142 *buffer = &buffer_impl->IDirect3DVertexBuffer8_iface;
3143 IDirect3DVertexBuffer8_AddRef(*buffer);
3145 else
3147 if (FAILED(hr))
3148 ERR("Failed to get wined3d stream source, hr %#x.\n", hr);
3149 *buffer = NULL;
3151 wined3d_mutex_unlock();
3153 return hr;
3156 static const struct IDirect3DDevice8Vtbl d3d8_device_vtbl =
3158 d3d8_device_QueryInterface,
3159 d3d8_device_AddRef,
3160 d3d8_device_Release,
3161 d3d8_device_TestCooperativeLevel,
3162 d3d8_device_GetAvailableTextureMem,
3163 d3d8_device_ResourceManagerDiscardBytes,
3164 d3d8_device_GetDirect3D,
3165 d3d8_device_GetDeviceCaps,
3166 d3d8_device_GetDisplayMode,
3167 d3d8_device_GetCreationParameters,
3168 d3d8_device_SetCursorProperties,
3169 d3d8_device_SetCursorPosition,
3170 d3d8_device_ShowCursor,
3171 d3d8_device_CreateAdditionalSwapChain,
3172 d3d8_device_Reset,
3173 d3d8_device_Present,
3174 d3d8_device_GetBackBuffer,
3175 d3d8_device_GetRasterStatus,
3176 d3d8_device_SetGammaRamp,
3177 d3d8_device_GetGammaRamp,
3178 d3d8_device_CreateTexture,
3179 d3d8_device_CreateVolumeTexture,
3180 d3d8_device_CreateCubeTexture,
3181 d3d8_device_CreateVertexBuffer,
3182 d3d8_device_CreateIndexBuffer,
3183 d3d8_device_CreateRenderTarget,
3184 d3d8_device_CreateDepthStencilSurface,
3185 d3d8_device_CreateImageSurface,
3186 d3d8_device_CopyRects,
3187 d3d8_device_UpdateTexture,
3188 d3d8_device_GetFrontBuffer,
3189 d3d8_device_SetRenderTarget,
3190 d3d8_device_GetRenderTarget,
3191 d3d8_device_GetDepthStencilSurface,
3192 d3d8_device_BeginScene,
3193 d3d8_device_EndScene,
3194 d3d8_device_Clear,
3195 d3d8_device_SetTransform,
3196 d3d8_device_GetTransform,
3197 d3d8_device_MultiplyTransform,
3198 d3d8_device_SetViewport,
3199 d3d8_device_GetViewport,
3200 d3d8_device_SetMaterial,
3201 d3d8_device_GetMaterial,
3202 d3d8_device_SetLight,
3203 d3d8_device_GetLight,
3204 d3d8_device_LightEnable,
3205 d3d8_device_GetLightEnable,
3206 d3d8_device_SetClipPlane,
3207 d3d8_device_GetClipPlane,
3208 d3d8_device_SetRenderState,
3209 d3d8_device_GetRenderState,
3210 d3d8_device_BeginStateBlock,
3211 d3d8_device_EndStateBlock,
3212 d3d8_device_ApplyStateBlock,
3213 d3d8_device_CaptureStateBlock,
3214 d3d8_device_DeleteStateBlock,
3215 d3d8_device_CreateStateBlock,
3216 d3d8_device_SetClipStatus,
3217 d3d8_device_GetClipStatus,
3218 d3d8_device_GetTexture,
3219 d3d8_device_SetTexture,
3220 d3d8_device_GetTextureStageState,
3221 d3d8_device_SetTextureStageState,
3222 d3d8_device_ValidateDevice,
3223 d3d8_device_GetInfo,
3224 d3d8_device_SetPaletteEntries,
3225 d3d8_device_GetPaletteEntries,
3226 d3d8_device_SetCurrentTexturePalette,
3227 d3d8_device_GetCurrentTexturePalette,
3228 d3d8_device_DrawPrimitive,
3229 d3d8_device_DrawIndexedPrimitive,
3230 d3d8_device_DrawPrimitiveUP,
3231 d3d8_device_DrawIndexedPrimitiveUP,
3232 d3d8_device_ProcessVertices,
3233 d3d8_device_CreateVertexShader,
3234 d3d8_device_SetVertexShader,
3235 d3d8_device_GetVertexShader,
3236 d3d8_device_DeleteVertexShader,
3237 d3d8_device_SetVertexShaderConstant,
3238 d3d8_device_GetVertexShaderConstant,
3239 d3d8_device_GetVertexShaderDeclaration,
3240 d3d8_device_GetVertexShaderFunction,
3241 d3d8_device_SetStreamSource,
3242 d3d8_device_GetStreamSource,
3243 d3d8_device_SetIndices,
3244 d3d8_device_GetIndices,
3245 d3d8_device_CreatePixelShader,
3246 d3d8_device_SetPixelShader,
3247 d3d8_device_GetPixelShader,
3248 d3d8_device_DeletePixelShader,
3249 d3d8_device_SetPixelShaderConstant,
3250 d3d8_device_GetPixelShaderConstant,
3251 d3d8_device_GetPixelShaderFunction,
3252 d3d8_device_DrawRectPatch,
3253 d3d8_device_DrawTriPatch,
3254 d3d8_device_DeletePatch,
3257 static inline struct d3d8_device *device_from_device_parent(struct wined3d_device_parent *device_parent)
3259 return CONTAINING_RECORD(device_parent, struct d3d8_device, device_parent);
3262 static void CDECL device_parent_wined3d_device_created(struct wined3d_device_parent *device_parent,
3263 struct wined3d_device *device)
3265 TRACE("device_parent %p, device %p\n", device_parent, device);
3268 static void CDECL device_parent_mode_changed(struct wined3d_device_parent *device_parent)
3270 TRACE("device_parent %p.\n", device_parent);
3273 static void CDECL device_parent_activate(struct wined3d_device_parent *device_parent, BOOL activate)
3275 struct d3d8_device *device = device_from_device_parent(device_parent);
3277 TRACE("device_parent %p, activate %#x.\n", device_parent, activate);
3279 if (!activate)
3280 InterlockedCompareExchange(&device->device_state, D3D8_DEVICE_STATE_LOST, D3D8_DEVICE_STATE_OK);
3281 else
3282 InterlockedCompareExchange(&device->device_state, D3D8_DEVICE_STATE_NOT_RESET, D3D8_DEVICE_STATE_LOST);
3285 static HRESULT CDECL device_parent_texture_sub_resource_created(struct wined3d_device_parent *device_parent,
3286 enum wined3d_resource_type type, struct wined3d_texture *wined3d_texture, unsigned int sub_resource_idx,
3287 void **parent, const struct wined3d_parent_ops **parent_ops)
3289 TRACE("device_parent %p, type %#x, wined3d_texture %p, sub_resource_idx %u, parent %p, parent_ops %p.\n",
3290 device_parent, type, wined3d_texture, sub_resource_idx, parent, parent_ops);
3292 if (type == WINED3D_RTYPE_TEXTURE_2D)
3294 struct d3d8_surface *d3d_surface;
3296 if (!(d3d_surface = heap_alloc_zero(sizeof(*d3d_surface))))
3297 return E_OUTOFMEMORY;
3299 surface_init(d3d_surface, wined3d_texture, sub_resource_idx, parent_ops);
3300 *parent = d3d_surface;
3301 TRACE("Created surface %p.\n", d3d_surface);
3303 else if (type == WINED3D_RTYPE_TEXTURE_3D)
3305 struct d3d8_volume *d3d_volume;
3307 if (!(d3d_volume = heap_alloc_zero(sizeof(*d3d_volume))))
3308 return E_OUTOFMEMORY;
3310 volume_init(d3d_volume, wined3d_texture, sub_resource_idx, parent_ops);
3311 *parent = d3d_volume;
3312 TRACE("Created volume %p.\n", d3d_volume);
3314 else
3316 ERR("Unhandled resource type %#x.\n", type);
3317 return E_FAIL;
3320 return D3D_OK;
3323 static HRESULT CDECL device_parent_create_swapchain_texture(struct wined3d_device_parent *device_parent,
3324 void *container_parent, const struct wined3d_resource_desc *desc, DWORD texture_flags,
3325 struct wined3d_texture **texture)
3327 struct d3d8_device *device = device_from_device_parent(device_parent);
3328 struct d3d8_surface *d3d_surface;
3329 HRESULT hr;
3331 TRACE("device_parent %p, container_parent %p, desc %p, texture flags %#x, texture %p.\n",
3332 device_parent, container_parent, desc, texture_flags, texture);
3334 if (FAILED(hr = wined3d_texture_create(device->wined3d_device, desc, 1, 1, texture_flags,
3335 NULL, &device->IDirect3DDevice8_iface, &d3d8_null_wined3d_parent_ops, texture)))
3337 WARN("Failed to create texture, hr %#x.\n", hr);
3338 return hr;
3341 d3d_surface = wined3d_texture_get_sub_resource_parent(*texture, 0);
3342 d3d_surface->parent_device = &device->IDirect3DDevice8_iface;
3344 return hr;
3347 static HRESULT CDECL device_parent_create_swapchain(struct wined3d_device_parent *device_parent,
3348 struct wined3d_swapchain_desc *desc, struct wined3d_swapchain **swapchain)
3350 struct d3d8_device *device = device_from_device_parent(device_parent);
3351 struct d3d8_swapchain *d3d_swapchain;
3352 HRESULT hr;
3354 TRACE("device_parent %p, desc %p, swapchain %p.\n", device_parent, desc, swapchain);
3356 if (FAILED(hr = d3d8_swapchain_create(device, desc, WINED3D_SWAP_INTERVAL_DEFAULT, &d3d_swapchain)))
3358 WARN("Failed to create swapchain, hr %#x.\n", hr);
3359 *swapchain = NULL;
3360 return hr;
3363 *swapchain = d3d_swapchain->wined3d_swapchain;
3364 wined3d_swapchain_incref(*swapchain);
3365 IDirect3DSwapChain8_Release(&d3d_swapchain->IDirect3DSwapChain8_iface);
3367 return hr;
3370 static const struct wined3d_device_parent_ops d3d8_wined3d_device_parent_ops =
3372 device_parent_wined3d_device_created,
3373 device_parent_mode_changed,
3374 device_parent_activate,
3375 device_parent_texture_sub_resource_created,
3376 device_parent_create_swapchain_texture,
3377 device_parent_create_swapchain,
3380 static void setup_fpu(void)
3382 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
3383 WORD cw;
3384 __asm__ volatile ("fnstcw %0" : "=m" (cw));
3385 cw = (cw & ~0xf3f) | 0x3f;
3386 __asm__ volatile ("fldcw %0" : : "m" (cw));
3387 #elif defined(__i386__) && defined(_MSC_VER)
3388 WORD cw;
3389 __asm fnstcw cw;
3390 cw = (cw & ~0xf3f) | 0x3f;
3391 __asm fldcw cw;
3392 #else
3393 FIXME("FPU setup not implemented for this platform.\n");
3394 #endif
3397 HRESULT device_init(struct d3d8_device *device, struct d3d8 *parent, struct wined3d *wined3d, UINT adapter,
3398 D3DDEVTYPE device_type, HWND focus_window, DWORD flags, D3DPRESENT_PARAMETERS *parameters)
3400 struct wined3d_swapchain_desc swapchain_desc;
3401 struct wined3d_swapchain *wined3d_swapchain;
3402 HRESULT hr;
3404 static const enum wined3d_feature_level feature_levels[] =
3406 WINED3D_FEATURE_LEVEL_8,
3407 WINED3D_FEATURE_LEVEL_7,
3408 WINED3D_FEATURE_LEVEL_6,
3409 WINED3D_FEATURE_LEVEL_5,
3412 device->IDirect3DDevice8_iface.lpVtbl = &d3d8_device_vtbl;
3413 device->device_parent.ops = &d3d8_wined3d_device_parent_ops;
3414 device->ref = 1;
3415 if (!(device->handle_table.entries = heap_alloc_zero(D3D8_INITIAL_HANDLE_TABLE_SIZE
3416 * sizeof(*device->handle_table.entries))))
3418 ERR("Failed to allocate handle table memory.\n");
3419 return E_OUTOFMEMORY;
3421 device->handle_table.table_size = D3D8_INITIAL_HANDLE_TABLE_SIZE;
3423 if (!(flags & D3DCREATE_FPU_PRESERVE)) setup_fpu();
3425 wined3d_mutex_lock();
3426 if (FAILED(hr = wined3d_device_create(wined3d, adapter, device_type,
3427 focus_window, flags, 4, feature_levels, ARRAY_SIZE(feature_levels),
3428 &device->device_parent, &device->wined3d_device)))
3430 WARN("Failed to create wined3d device, hr %#x.\n", hr);
3431 wined3d_mutex_unlock();
3432 heap_free(device->handle_table.entries);
3433 return hr;
3436 if (!parameters->Windowed)
3438 HWND device_window = parameters->hDeviceWindow;
3440 if (!focus_window)
3441 focus_window = device_window;
3442 if (FAILED(hr = wined3d_device_acquire_focus_window(device->wined3d_device, focus_window)))
3444 ERR("Failed to acquire focus window, hr %#x.\n", hr);
3445 wined3d_device_decref(device->wined3d_device);
3446 wined3d_mutex_unlock();
3447 heap_free(device->handle_table.entries);
3448 return hr;
3451 if (!device_window)
3452 device_window = focus_window;
3453 wined3d_device_setup_fullscreen_window(device->wined3d_device, device_window,
3454 parameters->BackBufferWidth,
3455 parameters->BackBufferHeight);
3458 if (flags & D3DCREATE_MULTITHREADED)
3459 wined3d_device_set_multithreaded(device->wined3d_device);
3461 if (!wined3d_swapchain_desc_from_present_parameters(&swapchain_desc, parameters))
3463 wined3d_device_release_focus_window(device->wined3d_device);
3464 wined3d_device_decref(device->wined3d_device);
3465 wined3d_mutex_unlock();
3466 heap_free(device->handle_table.entries);
3467 return D3DERR_INVALIDCALL;
3470 if (FAILED(hr = wined3d_device_init_3d(device->wined3d_device, &swapchain_desc)))
3472 WARN("Failed to initialize 3D, hr %#x.\n", hr);
3473 wined3d_device_release_focus_window(device->wined3d_device);
3474 wined3d_device_decref(device->wined3d_device);
3475 wined3d_mutex_unlock();
3476 heap_free(device->handle_table.entries);
3477 return hr;
3480 wined3d_device_set_render_state(device->wined3d_device,
3481 WINED3D_RS_ZENABLE, !!swapchain_desc.enable_auto_depth_stencil);
3482 wined3d_device_set_render_state(device->wined3d_device, WINED3D_RS_POINTSIZE_MIN, 0);
3483 wined3d_mutex_unlock();
3485 present_parameters_from_wined3d_swapchain_desc(parameters,
3486 &swapchain_desc, parameters->FullScreen_PresentationInterval);
3488 device->declArraySize = 16;
3489 if (!(device->decls = heap_alloc(device->declArraySize * sizeof(*device->decls))))
3491 ERR("Failed to allocate FVF vertex declaration map memory.\n");
3492 hr = E_OUTOFMEMORY;
3493 goto err;
3496 wined3d_swapchain = wined3d_device_get_swapchain(device->wined3d_device, 0);
3497 device->implicit_swapchain = wined3d_swapchain_get_parent(wined3d_swapchain);
3498 device->implicit_swapchain->swap_interval
3499 = wined3dswapinterval_from_d3d(parameters->FullScreen_PresentationInterval);
3501 device->d3d_parent = &parent->IDirect3D8_iface;
3502 IDirect3D8_AddRef(device->d3d_parent);
3504 return D3D_OK;
3506 err:
3507 wined3d_mutex_lock();
3508 wined3d_device_uninit_3d(device->wined3d_device);
3509 wined3d_device_release_focus_window(device->wined3d_device);
3510 wined3d_device_decref(device->wined3d_device);
3511 wined3d_mutex_unlock();
3512 heap_free(device->handle_table.entries);
3513 return hr;