winemac.drv: Don't filter non active displays out.
[wine/zf.git] / dlls / d3d8 / device.c
blobb07c20f71a0110ae02f1c9f40488a0d57d64d058
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 "d3d8_private.h"
24 WINE_DEFAULT_DEBUG_CHANNEL(d3d8);
26 static void STDMETHODCALLTYPE d3d8_null_wined3d_object_destroyed(void *parent) {}
28 const struct wined3d_parent_ops d3d8_null_wined3d_parent_ops =
30 d3d8_null_wined3d_object_destroyed,
33 D3DFORMAT d3dformat_from_wined3dformat(enum wined3d_format_id format)
35 BYTE *c = (BYTE *)&format;
37 /* Don't translate FOURCC formats */
38 if (isprint(c[0]) && isprint(c[1]) && isprint(c[2]) && isprint(c[3]))
39 return (D3DFORMAT)format;
41 switch(format)
43 case WINED3DFMT_UNKNOWN: return D3DFMT_UNKNOWN;
44 case WINED3DFMT_B8G8R8_UNORM: return D3DFMT_R8G8B8;
45 case WINED3DFMT_B8G8R8A8_UNORM: return D3DFMT_A8R8G8B8;
46 case WINED3DFMT_B8G8R8X8_UNORM: return D3DFMT_X8R8G8B8;
47 case WINED3DFMT_B5G6R5_UNORM: return D3DFMT_R5G6B5;
48 case WINED3DFMT_B5G5R5X1_UNORM: return D3DFMT_X1R5G5B5;
49 case WINED3DFMT_B5G5R5A1_UNORM: return D3DFMT_A1R5G5B5;
50 case WINED3DFMT_B4G4R4A4_UNORM: return D3DFMT_A4R4G4B4;
51 case WINED3DFMT_B2G3R3_UNORM: return D3DFMT_R3G3B2;
52 case WINED3DFMT_A8_UNORM: return D3DFMT_A8;
53 case WINED3DFMT_B2G3R3A8_UNORM: return D3DFMT_A8R3G3B2;
54 case WINED3DFMT_B4G4R4X4_UNORM: return D3DFMT_X4R4G4B4;
55 case WINED3DFMT_R10G10B10A2_UNORM: return D3DFMT_A2B10G10R10;
56 case WINED3DFMT_R16G16_UNORM: return D3DFMT_G16R16;
57 case WINED3DFMT_P8_UINT_A8_UNORM: return D3DFMT_A8P8;
58 case WINED3DFMT_P8_UINT: return D3DFMT_P8;
59 case WINED3DFMT_L8_UNORM: return D3DFMT_L8;
60 case WINED3DFMT_L8A8_UNORM: return D3DFMT_A8L8;
61 case WINED3DFMT_L4A4_UNORM: return D3DFMT_A4L4;
62 case WINED3DFMT_R8G8_SNORM: return D3DFMT_V8U8;
63 case WINED3DFMT_R5G5_SNORM_L6_UNORM: return D3DFMT_L6V5U5;
64 case WINED3DFMT_R8G8_SNORM_L8X8_UNORM: return D3DFMT_X8L8V8U8;
65 case WINED3DFMT_R8G8B8A8_SNORM: return D3DFMT_Q8W8V8U8;
66 case WINED3DFMT_R16G16_SNORM: return D3DFMT_V16U16;
67 case WINED3DFMT_R10G11B11_SNORM: return D3DFMT_W11V11U10;
68 case WINED3DFMT_R10G10B10_SNORM_A2_UNORM: return D3DFMT_A2W10V10U10;
69 case WINED3DFMT_D16_LOCKABLE: return D3DFMT_D16_LOCKABLE;
70 case WINED3DFMT_D32_UNORM: return D3DFMT_D32;
71 case WINED3DFMT_S1_UINT_D15_UNORM: return D3DFMT_D15S1;
72 case WINED3DFMT_D24_UNORM_S8_UINT: return D3DFMT_D24S8;
73 case WINED3DFMT_X8D24_UNORM: return D3DFMT_D24X8;
74 case WINED3DFMT_S4X4_UINT_D24_UNORM: return D3DFMT_D24X4S4;
75 case WINED3DFMT_D16_UNORM: return D3DFMT_D16;
76 case WINED3DFMT_R16_UINT: return D3DFMT_INDEX16;
77 case WINED3DFMT_R32_UINT: return D3DFMT_INDEX32;
78 default:
79 FIXME("Unhandled wined3d format %#x.\n", format);
80 return D3DFMT_UNKNOWN;
84 enum wined3d_format_id wined3dformat_from_d3dformat(D3DFORMAT format)
86 BYTE *c = (BYTE *)&format;
88 /* Don't translate FOURCC formats */
89 if (isprint(c[0]) && isprint(c[1]) && isprint(c[2]) && isprint(c[3]))
90 return (enum wined3d_format_id)format;
92 switch(format)
94 case D3DFMT_UNKNOWN: return WINED3DFMT_UNKNOWN;
95 case D3DFMT_R8G8B8: return WINED3DFMT_B8G8R8_UNORM;
96 case D3DFMT_A8R8G8B8: return WINED3DFMT_B8G8R8A8_UNORM;
97 case D3DFMT_X8R8G8B8: return WINED3DFMT_B8G8R8X8_UNORM;
98 case D3DFMT_R5G6B5: return WINED3DFMT_B5G6R5_UNORM;
99 case D3DFMT_X1R5G5B5: return WINED3DFMT_B5G5R5X1_UNORM;
100 case D3DFMT_A1R5G5B5: return WINED3DFMT_B5G5R5A1_UNORM;
101 case D3DFMT_A4R4G4B4: return WINED3DFMT_B4G4R4A4_UNORM;
102 case D3DFMT_R3G3B2: return WINED3DFMT_B2G3R3_UNORM;
103 case D3DFMT_A8: return WINED3DFMT_A8_UNORM;
104 case D3DFMT_A8R3G3B2: return WINED3DFMT_B2G3R3A8_UNORM;
105 case D3DFMT_X4R4G4B4: return WINED3DFMT_B4G4R4X4_UNORM;
106 case D3DFMT_A2B10G10R10: return WINED3DFMT_R10G10B10A2_UNORM;
107 case D3DFMT_G16R16: return WINED3DFMT_R16G16_UNORM;
108 case D3DFMT_A8P8: return WINED3DFMT_P8_UINT_A8_UNORM;
109 case D3DFMT_P8: return WINED3DFMT_P8_UINT;
110 case D3DFMT_L8: return WINED3DFMT_L8_UNORM;
111 case D3DFMT_A8L8: return WINED3DFMT_L8A8_UNORM;
112 case D3DFMT_A4L4: return WINED3DFMT_L4A4_UNORM;
113 case D3DFMT_V8U8: return WINED3DFMT_R8G8_SNORM;
114 case D3DFMT_L6V5U5: return WINED3DFMT_R5G5_SNORM_L6_UNORM;
115 case D3DFMT_X8L8V8U8: return WINED3DFMT_R8G8_SNORM_L8X8_UNORM;
116 case D3DFMT_Q8W8V8U8: return WINED3DFMT_R8G8B8A8_SNORM;
117 case D3DFMT_V16U16: return WINED3DFMT_R16G16_SNORM;
118 case D3DFMT_W11V11U10: return WINED3DFMT_R10G11B11_SNORM;
119 case D3DFMT_A2W10V10U10: return WINED3DFMT_R10G10B10_SNORM_A2_UNORM;
120 case D3DFMT_D16_LOCKABLE: return WINED3DFMT_D16_LOCKABLE;
121 case D3DFMT_D32: return WINED3DFMT_D32_UNORM;
122 case D3DFMT_D15S1: return WINED3DFMT_S1_UINT_D15_UNORM;
123 case D3DFMT_D24S8: return WINED3DFMT_D24_UNORM_S8_UINT;
124 case D3DFMT_D24X8: return WINED3DFMT_X8D24_UNORM;
125 case D3DFMT_D24X4S4: return WINED3DFMT_S4X4_UINT_D24_UNORM;
126 case D3DFMT_D16: return WINED3DFMT_D16_UNORM;
127 case D3DFMT_INDEX16: return WINED3DFMT_R16_UINT;
128 case D3DFMT_INDEX32: return WINED3DFMT_R32_UINT;
129 default:
130 FIXME("Unhandled D3DFORMAT %#x.\n", format);
131 return WINED3DFMT_UNKNOWN;
135 unsigned int wined3dmapflags_from_d3dmapflags(unsigned int flags, unsigned int usage)
137 static const unsigned int handled = D3DLOCK_NOSYSLOCK
138 | D3DLOCK_NOOVERWRITE
139 | D3DLOCK_DISCARD
140 | D3DLOCK_NO_DIRTY_UPDATE;
141 unsigned int wined3d_flags;
143 wined3d_flags = flags & handled;
144 if (~usage & D3DUSAGE_WRITEONLY && !(flags & (D3DLOCK_NOOVERWRITE | D3DLOCK_DISCARD)))
145 wined3d_flags |= WINED3D_MAP_READ;
146 if (!(flags & D3DLOCK_READONLY))
147 wined3d_flags |= WINED3D_MAP_WRITE;
148 if (!(wined3d_flags & (WINED3D_MAP_READ | WINED3D_MAP_WRITE)))
149 wined3d_flags |= WINED3D_MAP_WRITE;
150 flags &= ~(handled | D3DLOCK_READONLY);
152 if (flags)
153 FIXME("Unhandled flags %#x.\n", flags);
155 return wined3d_flags;
158 static UINT vertex_count_from_primitive_count(D3DPRIMITIVETYPE primitive_type, UINT primitive_count)
160 switch (primitive_type)
162 case D3DPT_POINTLIST:
163 return primitive_count;
165 case D3DPT_LINELIST:
166 return primitive_count * 2;
168 case D3DPT_LINESTRIP:
169 return primitive_count + 1;
171 case D3DPT_TRIANGLELIST:
172 return primitive_count * 3;
174 case D3DPT_TRIANGLESTRIP:
175 case D3DPT_TRIANGLEFAN:
176 return primitive_count + 2;
178 default:
179 FIXME("Unhandled primitive type %#x.\n", primitive_type);
180 return 0;
184 static D3DSWAPEFFECT d3dswapeffect_from_wined3dswapeffect(enum wined3d_swap_effect effect)
186 switch (effect)
188 case WINED3D_SWAP_EFFECT_DISCARD:
189 return D3DSWAPEFFECT_DISCARD;
190 case WINED3D_SWAP_EFFECT_SEQUENTIAL:
191 return D3DSWAPEFFECT_FLIP;
192 case WINED3D_SWAP_EFFECT_COPY:
193 return D3DSWAPEFFECT_COPY;
194 case WINED3D_SWAP_EFFECT_COPY_VSYNC:
195 return D3DSWAPEFFECT_COPY_VSYNC;
196 default:
197 FIXME("Unhandled swap effect %#x.\n", effect);
198 return D3DSWAPEFFECT_FLIP;
202 static void present_parameters_from_wined3d_swapchain_desc(D3DPRESENT_PARAMETERS *present_parameters,
203 const struct wined3d_swapchain_desc *swapchain_desc, DWORD presentation_interval)
205 present_parameters->BackBufferWidth = swapchain_desc->backbuffer_width;
206 present_parameters->BackBufferHeight = swapchain_desc->backbuffer_height;
207 present_parameters->BackBufferFormat = d3dformat_from_wined3dformat(swapchain_desc->backbuffer_format);
208 present_parameters->BackBufferCount = swapchain_desc->backbuffer_count;
209 present_parameters->MultiSampleType = d3dmultisample_type_from_wined3d(swapchain_desc->multisample_type);
210 present_parameters->SwapEffect = d3dswapeffect_from_wined3dswapeffect(swapchain_desc->swap_effect);
211 present_parameters->hDeviceWindow = swapchain_desc->device_window;
212 present_parameters->Windowed = swapchain_desc->windowed;
213 present_parameters->EnableAutoDepthStencil = swapchain_desc->enable_auto_depth_stencil;
214 present_parameters->AutoDepthStencilFormat
215 = d3dformat_from_wined3dformat(swapchain_desc->auto_depth_stencil_format);
216 present_parameters->Flags = swapchain_desc->flags & D3DPRESENTFLAGS_MASK;
217 present_parameters->FullScreen_RefreshRateInHz = swapchain_desc->refresh_rate;
218 present_parameters->FullScreen_PresentationInterval = presentation_interval;
221 static enum wined3d_swap_effect wined3dswapeffect_from_d3dswapeffect(D3DSWAPEFFECT effect)
223 switch (effect)
225 case D3DSWAPEFFECT_DISCARD:
226 return WINED3D_SWAP_EFFECT_DISCARD;
227 case D3DSWAPEFFECT_FLIP:
228 return WINED3D_SWAP_EFFECT_SEQUENTIAL;
229 case D3DSWAPEFFECT_COPY:
230 return WINED3D_SWAP_EFFECT_COPY;
231 case D3DSWAPEFFECT_COPY_VSYNC:
232 return WINED3D_SWAP_EFFECT_COPY_VSYNC;
233 default:
234 FIXME("Unhandled swap effect %#x.\n", effect);
235 return WINED3D_SWAP_EFFECT_SEQUENTIAL;
239 static enum wined3d_swap_interval wined3dswapinterval_from_d3d(DWORD interval)
241 switch (interval)
243 case D3DPRESENT_INTERVAL_IMMEDIATE:
244 return WINED3D_SWAP_INTERVAL_IMMEDIATE;
245 case D3DPRESENT_INTERVAL_ONE:
246 return WINED3D_SWAP_INTERVAL_ONE;
247 case D3DPRESENT_INTERVAL_TWO:
248 return WINED3D_SWAP_INTERVAL_TWO;
249 case D3DPRESENT_INTERVAL_THREE:
250 return WINED3D_SWAP_INTERVAL_THREE;
251 case D3DPRESENT_INTERVAL_FOUR:
252 return WINED3D_SWAP_INTERVAL_FOUR;
253 default:
254 FIXME("Unhandled presentation interval %#x.\n", interval);
255 case D3DPRESENT_INTERVAL_DEFAULT:
256 return WINED3D_SWAP_INTERVAL_DEFAULT;
260 static enum wined3d_multisample_type wined3d_multisample_type_from_d3d(D3DMULTISAMPLE_TYPE type)
262 return (enum wined3d_multisample_type)type;
265 static BOOL wined3d_swapchain_desc_from_d3d8(struct wined3d_swapchain_desc *swapchain_desc,
266 struct wined3d_output *output, const D3DPRESENT_PARAMETERS *present_parameters)
268 if (!present_parameters->SwapEffect || present_parameters->SwapEffect > D3DSWAPEFFECT_COPY_VSYNC)
270 WARN("Invalid swap effect %u passed.\n", present_parameters->SwapEffect);
271 return FALSE;
273 if (present_parameters->BackBufferCount > 3
274 || ((present_parameters->SwapEffect == D3DSWAPEFFECT_COPY
275 || present_parameters->SwapEffect == D3DSWAPEFFECT_COPY_VSYNC)
276 && present_parameters->BackBufferCount > 1))
278 WARN("Invalid backbuffer count %u.\n", present_parameters->BackBufferCount);
279 return FALSE;
281 switch (present_parameters->FullScreen_PresentationInterval)
283 case D3DPRESENT_INTERVAL_DEFAULT:
284 case D3DPRESENT_INTERVAL_ONE:
285 case D3DPRESENT_INTERVAL_TWO:
286 case D3DPRESENT_INTERVAL_THREE:
287 case D3DPRESENT_INTERVAL_FOUR:
288 case D3DPRESENT_INTERVAL_IMMEDIATE:
289 break;
290 default:
291 WARN("Invalid presentation interval %#x.\n",
292 present_parameters->FullScreen_PresentationInterval);
293 return FALSE;
296 swapchain_desc->output = output;
297 swapchain_desc->backbuffer_width = present_parameters->BackBufferWidth;
298 swapchain_desc->backbuffer_height = present_parameters->BackBufferHeight;
299 swapchain_desc->backbuffer_format = wined3dformat_from_d3dformat(present_parameters->BackBufferFormat);
300 swapchain_desc->backbuffer_count = max(1, present_parameters->BackBufferCount);
301 swapchain_desc->backbuffer_bind_flags = WINED3D_BIND_RENDER_TARGET;
302 swapchain_desc->multisample_type = wined3d_multisample_type_from_d3d(present_parameters->MultiSampleType);
303 swapchain_desc->multisample_quality = 0; /* d3d9 only */
304 swapchain_desc->swap_effect = wined3dswapeffect_from_d3dswapeffect(present_parameters->SwapEffect);
305 swapchain_desc->device_window = present_parameters->hDeviceWindow;
306 swapchain_desc->windowed = present_parameters->Windowed;
307 swapchain_desc->enable_auto_depth_stencil = present_parameters->EnableAutoDepthStencil;
308 swapchain_desc->auto_depth_stencil_format
309 = wined3dformat_from_d3dformat(present_parameters->AutoDepthStencilFormat);
310 swapchain_desc->flags
311 = (present_parameters->Flags & D3DPRESENTFLAGS_MASK) | WINED3D_SWAPCHAIN_ALLOW_MODE_SWITCH;
312 swapchain_desc->refresh_rate = present_parameters->FullScreen_RefreshRateInHz;
313 swapchain_desc->auto_restore_display_mode = TRUE;
315 if (present_parameters->Flags & ~D3DPRESENTFLAGS_MASK)
316 FIXME("Unhandled flags %#x.\n", present_parameters->Flags & ~D3DPRESENTFLAGS_MASK);
318 return TRUE;
321 void d3dcaps_from_wined3dcaps(D3DCAPS8 *caps, const struct wined3d_caps *wined3d_caps,
322 unsigned int adapter_ordinal)
324 caps->DeviceType = (D3DDEVTYPE)wined3d_caps->DeviceType;
325 caps->AdapterOrdinal = adapter_ordinal;
326 caps->Caps = wined3d_caps->Caps;
327 caps->Caps2 = wined3d_caps->Caps2;
328 caps->Caps3 = wined3d_caps->Caps3;
329 caps->PresentationIntervals = D3DPRESENT_INTERVAL_IMMEDIATE | D3DPRESENT_INTERVAL_ONE;
330 caps->CursorCaps = wined3d_caps->CursorCaps;
331 caps->DevCaps = wined3d_caps->DevCaps;
332 caps->PrimitiveMiscCaps = wined3d_caps->PrimitiveMiscCaps;
333 caps->RasterCaps = wined3d_caps->RasterCaps;
334 caps->ZCmpCaps = wined3d_caps->ZCmpCaps;
335 caps->SrcBlendCaps = wined3d_caps->SrcBlendCaps;
336 caps->DestBlendCaps = wined3d_caps->DestBlendCaps;
337 caps->AlphaCmpCaps = wined3d_caps->AlphaCmpCaps;
338 caps->ShadeCaps = wined3d_caps->ShadeCaps;
339 caps->TextureCaps = wined3d_caps->TextureCaps;
340 caps->TextureFilterCaps = wined3d_caps->TextureFilterCaps;
341 caps->CubeTextureFilterCaps = wined3d_caps->CubeTextureFilterCaps;
342 caps->VolumeTextureFilterCaps = wined3d_caps->VolumeTextureFilterCaps;
343 caps->TextureAddressCaps = wined3d_caps->TextureAddressCaps;
344 caps->VolumeTextureAddressCaps = wined3d_caps->VolumeTextureAddressCaps;
345 caps->LineCaps = wined3d_caps->LineCaps;
346 caps->MaxTextureWidth = wined3d_caps->MaxTextureWidth;
347 caps->MaxTextureHeight = wined3d_caps->MaxTextureHeight;
348 caps->MaxVolumeExtent = wined3d_caps->MaxVolumeExtent;
349 caps->MaxTextureRepeat = wined3d_caps->MaxTextureRepeat;
350 caps->MaxTextureAspectRatio = wined3d_caps->MaxTextureAspectRatio;
351 caps->MaxAnisotropy = wined3d_caps->MaxAnisotropy;
352 caps->MaxVertexW = wined3d_caps->MaxVertexW;
353 caps->GuardBandLeft = wined3d_caps->GuardBandLeft;
354 caps->GuardBandTop = wined3d_caps->GuardBandTop;
355 caps->GuardBandRight = wined3d_caps->GuardBandRight;
356 caps->GuardBandBottom = wined3d_caps->GuardBandBottom;
357 caps->ExtentsAdjust = wined3d_caps->ExtentsAdjust;
358 caps->StencilCaps = wined3d_caps->StencilCaps;
359 caps->FVFCaps = wined3d_caps->FVFCaps;
360 caps->TextureOpCaps = wined3d_caps->TextureOpCaps;
361 caps->MaxTextureBlendStages = wined3d_caps->MaxTextureBlendStages;
362 caps->MaxSimultaneousTextures = wined3d_caps->MaxSimultaneousTextures;
363 caps->VertexProcessingCaps = wined3d_caps->VertexProcessingCaps;
364 caps->MaxActiveLights = wined3d_caps->MaxActiveLights;
365 caps->MaxUserClipPlanes = wined3d_caps->MaxUserClipPlanes;
366 caps->MaxVertexBlendMatrices = wined3d_caps->MaxVertexBlendMatrices;
367 caps->MaxVertexBlendMatrixIndex = wined3d_caps->MaxVertexBlendMatrixIndex;
368 caps->MaxPointSize = wined3d_caps->MaxPointSize;
369 caps->MaxPrimitiveCount = wined3d_caps->MaxPrimitiveCount;
370 caps->MaxVertexIndex = wined3d_caps->MaxVertexIndex;
371 caps->MaxStreams = wined3d_caps->MaxStreams;
372 caps->MaxStreamStride = wined3d_caps->MaxStreamStride;
373 caps->VertexShaderVersion = wined3d_caps->VertexShaderVersion;
374 caps->MaxVertexShaderConst = wined3d_caps->MaxVertexShaderConst;
375 caps->PixelShaderVersion = wined3d_caps->PixelShaderVersion;
376 caps->MaxPixelShaderValue = wined3d_caps->PixelShader1xMaxValue;
378 caps->Caps2 &= D3DCAPS2_CANCALIBRATEGAMMA | D3DCAPS2_CANRENDERWINDOWED
379 | D3DCAPS2_CANMANAGERESOURCE | D3DCAPS2_DYNAMICTEXTURES | D3DCAPS2_FULLSCREENGAMMA
380 | D3DCAPS2_NO2DDURING3DSCENE | D3DCAPS2_RESERVED;
381 caps->Caps3 &= D3DCAPS3_ALPHA_FULLSCREEN_FLIP_OR_DISCARD | D3DCAPS3_RESERVED;
382 caps->PrimitiveMiscCaps &= D3DPMISCCAPS_MASKZ | D3DPMISCCAPS_LINEPATTERNREP
383 | D3DPMISCCAPS_CULLNONE | D3DPMISCCAPS_CULLCW | D3DPMISCCAPS_CULLCCW
384 | D3DPMISCCAPS_COLORWRITEENABLE | D3DPMISCCAPS_CLIPPLANESCALEDPOINTS
385 | D3DPMISCCAPS_CLIPTLVERTS | D3DPMISCCAPS_TSSARGTEMP | D3DPMISCCAPS_BLENDOP
386 | D3DPMISCCAPS_NULLREFERENCE;
387 caps->RasterCaps &= D3DPRASTERCAPS_DITHER | D3DPRASTERCAPS_PAT | D3DPRASTERCAPS_ZTEST
388 | D3DPRASTERCAPS_FOGVERTEX | D3DPRASTERCAPS_FOGTABLE | D3DPRASTERCAPS_ANTIALIASEDGES
389 | D3DPRASTERCAPS_MIPMAPLODBIAS | D3DPRASTERCAPS_ZBUFFERLESSHSR
390 | D3DPRASTERCAPS_FOGRANGE | D3DPRASTERCAPS_ANISOTROPY | D3DPRASTERCAPS_WBUFFER
391 | D3DPRASTERCAPS_WFOG | D3DPRASTERCAPS_ZFOG | D3DPRASTERCAPS_COLORPERSPECTIVE
392 | D3DPRASTERCAPS_STRETCHBLTMULTISAMPLE | WINED3DPRASTERCAPS_DEPTHBIAS;
393 if (caps->RasterCaps & WINED3DPRASTERCAPS_DEPTHBIAS)
394 caps->RasterCaps = (caps->RasterCaps | D3DPRASTERCAPS_ZBIAS) & ~WINED3DPRASTERCAPS_DEPTHBIAS;
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 if (caps->LineCaps & WINED3DLINECAPS_ANTIALIAS)
427 caps->RasterCaps |= D3DPRASTERCAPS_ANTIALIASEDGES;
428 caps->LineCaps &= ~WINED3DLINECAPS_ANTIALIAS;
429 caps->StencilCaps &= ~WINED3DSTENCILCAPS_TWOSIDED;
430 caps->VertexProcessingCaps &= D3DVTXPCAPS_TEXGEN | D3DVTXPCAPS_MATERIALSOURCE7
431 | D3DVTXPCAPS_DIRECTIONALLIGHTS | D3DVTXPCAPS_POSITIONALLIGHTS | D3DVTXPCAPS_LOCALVIEWER
432 | D3DVTXPCAPS_TWEENING | D3DVTXPCAPS_NO_VSDT_UBYTE4;
434 /* D3D8 doesn't support SM 2.0 or higher, so clamp to 1.x */
435 if (caps->PixelShaderVersion)
436 caps->PixelShaderVersion = D3DPS_VERSION(1, 4);
437 else
438 caps->PixelShaderVersion = D3DPS_VERSION(0, 0);
439 if (caps->VertexShaderVersion)
440 caps->VertexShaderVersion = D3DVS_VERSION(1, 1);
441 else
442 caps->VertexShaderVersion = D3DVS_VERSION(0, 0);
443 caps->MaxVertexShaderConst = min(D3D8_MAX_VERTEX_SHADER_CONSTANTF, caps->MaxVertexShaderConst);
446 static enum wined3d_transform_state wined3d_transform_state_from_d3d(D3DTRANSFORMSTATETYPE state)
448 return (enum wined3d_transform_state)state;
451 static enum wined3d_render_state wined3d_render_state_from_d3d(D3DRENDERSTATETYPE state)
453 switch (state)
455 case D3DRS_ZBIAS:
456 return WINED3D_RS_DEPTHBIAS;
457 case D3DRS_EDGEANTIALIAS:
458 return WINED3D_RS_ANTIALIASEDLINEENABLE;
459 default:
460 return (enum wined3d_render_state)state;
464 static enum wined3d_primitive_type wined3d_primitive_type_from_d3d(D3DPRIMITIVETYPE type)
466 return (enum wined3d_primitive_type)type;
469 /* Handle table functions */
470 static DWORD d3d8_allocate_handle(struct d3d8_handle_table *t, void *object, enum d3d8_handle_type type)
472 struct d3d8_handle_entry *entry;
474 if (t->free_entries)
476 DWORD index = t->free_entries - t->entries;
477 /* Use a free handle */
478 entry = t->free_entries;
479 if (entry->type != D3D8_HANDLE_FREE)
481 ERR("Handle %u(%p) is in the free list, but has type %#x.\n", index, entry, entry->type);
482 return D3D8_INVALID_HANDLE;
484 t->free_entries = entry->object;
485 entry->object = object;
486 entry->type = type;
488 return index;
491 if (!(t->entry_count < t->table_size))
493 /* Grow the table */
494 UINT new_size = t->table_size + (t->table_size >> 1);
495 struct d3d8_handle_entry *new_entries;
497 if (!(new_entries = heap_realloc(t->entries, new_size * sizeof(*t->entries))))
499 ERR("Failed to grow the handle table.\n");
500 return D3D8_INVALID_HANDLE;
502 t->entries = new_entries;
503 t->table_size = new_size;
506 entry = &t->entries[t->entry_count];
507 entry->object = object;
508 entry->type = type;
510 return t->entry_count++;
513 static void *d3d8_free_handle(struct d3d8_handle_table *t, DWORD handle, enum d3d8_handle_type type)
515 struct d3d8_handle_entry *entry;
516 void *object;
518 if (handle == D3D8_INVALID_HANDLE || handle >= t->entry_count)
520 WARN("Invalid handle %u passed.\n", handle);
521 return NULL;
524 entry = &t->entries[handle];
525 if (entry->type != type)
527 WARN("Handle %u(%p) is not of type %#x.\n", handle, entry, type);
528 return NULL;
531 object = entry->object;
532 entry->object = t->free_entries;
533 entry->type = D3D8_HANDLE_FREE;
534 t->free_entries = entry;
536 return object;
539 static void *d3d8_get_object(struct d3d8_handle_table *t, DWORD handle, enum d3d8_handle_type type)
541 struct d3d8_handle_entry *entry;
543 if (handle == D3D8_INVALID_HANDLE || handle >= t->entry_count)
545 WARN("Invalid handle %u passed.\n", handle);
546 return NULL;
549 entry = &t->entries[handle];
550 if (entry->type != type)
552 WARN("Handle %u(%p) is not of type %#x.\n", handle, entry, type);
553 return NULL;
556 return entry->object;
559 static void device_reset_viewport_state(struct d3d8_device *device)
561 struct wined3d_viewport vp;
562 RECT rect;
564 wined3d_device_get_viewports(device->wined3d_device, NULL, &vp);
565 wined3d_stateblock_set_viewport(device->state, &vp);
566 wined3d_device_get_scissor_rects(device->wined3d_device, NULL, &rect);
567 wined3d_stateblock_set_scissor_rect(device->state, &rect);
570 static HRESULT WINAPI d3d8_device_QueryInterface(IDirect3DDevice8 *iface, REFIID riid, void **out)
572 TRACE("iface %p, riid %s, out %p.\n",
573 iface, debugstr_guid(riid), out);
575 if (IsEqualGUID(riid, &IID_IDirect3DDevice8)
576 || IsEqualGUID(riid, &IID_IUnknown))
578 IDirect3DDevice8_AddRef(iface);
579 *out = iface;
580 return S_OK;
583 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
585 *out = NULL;
586 return E_NOINTERFACE;
589 static ULONG WINAPI d3d8_device_AddRef(IDirect3DDevice8 *iface)
591 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
592 ULONG ref = InterlockedIncrement(&device->ref);
594 TRACE("%p increasing refcount to %u.\n", iface, ref);
596 return ref;
599 static ULONG WINAPI d3d8_device_Release(IDirect3DDevice8 *iface)
601 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
602 ULONG ref;
604 if (device->in_destruction)
605 return 0;
607 ref = InterlockedDecrement(&device->ref);
609 TRACE("%p decreasing refcount to %u.\n", iface, ref);
611 if (!ref)
613 IDirect3D8 *parent = &device->d3d_parent->IDirect3D8_iface;
614 unsigned i;
616 TRACE("Releasing wined3d device %p.\n", device->wined3d_device);
618 wined3d_mutex_lock();
620 device->in_destruction = TRUE;
622 for (i = 0; i < device->numConvertedDecls; ++i)
624 d3d8_vertex_declaration_destroy(device->decls[i].declaration);
626 heap_free(device->decls);
628 if (device->vertex_buffer)
629 wined3d_buffer_decref(device->vertex_buffer);
630 if (device->index_buffer)
631 wined3d_buffer_decref(device->index_buffer);
633 if (device->recording)
634 wined3d_stateblock_decref(device->recording);
635 wined3d_stateblock_decref(device->state);
637 wined3d_swapchain_decref(device->implicit_swapchain);
638 wined3d_device_release_focus_window(device->wined3d_device);
639 wined3d_device_decref(device->wined3d_device);
640 heap_free(device->handle_table.entries);
641 heap_free(device);
643 wined3d_mutex_unlock();
645 IDirect3D8_Release(parent);
647 return ref;
650 static HRESULT WINAPI d3d8_device_TestCooperativeLevel(IDirect3DDevice8 *iface)
652 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
654 TRACE("iface %p.\n", iface);
656 TRACE("device state: %#x.\n", device->device_state);
658 switch (device->device_state)
660 default:
661 case D3D8_DEVICE_STATE_OK:
662 return D3D_OK;
663 case D3D8_DEVICE_STATE_LOST:
664 return D3DERR_DEVICELOST;
665 case D3D8_DEVICE_STATE_NOT_RESET:
666 return D3DERR_DEVICENOTRESET;
670 static UINT WINAPI d3d8_device_GetAvailableTextureMem(IDirect3DDevice8 *iface)
672 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
673 UINT ret;
675 TRACE("iface %p.\n", iface);
677 wined3d_mutex_lock();
678 ret = wined3d_device_get_available_texture_mem(device->wined3d_device);
679 wined3d_mutex_unlock();
681 return ret;
684 static HRESULT WINAPI d3d8_device_ResourceManagerDiscardBytes(IDirect3DDevice8 *iface, DWORD byte_count)
686 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
688 TRACE("iface %p, byte_count %u.\n", iface, byte_count);
690 if (byte_count)
691 FIXME("Byte count ignored.\n");
693 wined3d_mutex_lock();
694 wined3d_device_evict_managed_resources(device->wined3d_device);
695 wined3d_mutex_unlock();
697 return D3D_OK;
700 static HRESULT WINAPI d3d8_device_GetDirect3D(IDirect3DDevice8 *iface, IDirect3D8 **d3d8)
702 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
704 TRACE("iface %p, d3d8 %p.\n", iface, d3d8);
706 if (!d3d8)
707 return D3DERR_INVALIDCALL;
709 return IDirect3D8_QueryInterface(&device->d3d_parent->IDirect3D8_iface, &IID_IDirect3D8,
710 (void **)d3d8);
713 static HRESULT WINAPI d3d8_device_GetDeviceCaps(IDirect3DDevice8 *iface, D3DCAPS8 *caps)
715 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
716 struct wined3d_caps wined3d_caps;
717 HRESULT hr;
719 TRACE("iface %p, caps %p.\n", iface, caps);
721 if (!caps)
722 return D3DERR_INVALIDCALL;
724 wined3d_mutex_lock();
725 hr = wined3d_device_get_device_caps(device->wined3d_device, &wined3d_caps);
726 wined3d_mutex_unlock();
728 d3dcaps_from_wined3dcaps(caps, &wined3d_caps, device->adapter_ordinal);
730 return hr;
733 static HRESULT WINAPI d3d8_device_GetDisplayMode(IDirect3DDevice8 *iface, D3DDISPLAYMODE *mode)
735 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
736 struct wined3d_display_mode wined3d_mode;
737 HRESULT hr;
739 TRACE("iface %p, mode %p.\n", iface, mode);
741 wined3d_mutex_lock();
742 hr = wined3d_device_get_display_mode(device->wined3d_device, 0, &wined3d_mode, NULL);
743 wined3d_mutex_unlock();
745 if (SUCCEEDED(hr))
747 mode->Width = wined3d_mode.width;
748 mode->Height = wined3d_mode.height;
749 mode->RefreshRate = wined3d_mode.refresh_rate;
750 mode->Format = d3dformat_from_wined3dformat(wined3d_mode.format_id);
753 return hr;
756 static HRESULT WINAPI d3d8_device_GetCreationParameters(IDirect3DDevice8 *iface,
757 D3DDEVICE_CREATION_PARAMETERS *parameters)
759 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
761 TRACE("iface %p, parameters %p.\n", iface, parameters);
763 wined3d_mutex_lock();
764 wined3d_device_get_creation_parameters(device->wined3d_device,
765 (struct wined3d_device_creation_parameters *)parameters);
766 wined3d_mutex_unlock();
768 parameters->AdapterOrdinal = device->adapter_ordinal;
769 return D3D_OK;
772 static HRESULT WINAPI d3d8_device_SetCursorProperties(IDirect3DDevice8 *iface,
773 UINT hotspot_x, UINT hotspot_y, IDirect3DSurface8 *bitmap)
775 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
776 struct d3d8_surface *bitmap_impl = unsafe_impl_from_IDirect3DSurface8(bitmap);
777 D3DSURFACE_DESC surface_desc;
778 D3DDISPLAYMODE mode;
779 HRESULT hr;
781 TRACE("iface %p, hotspot_x %u, hotspot_y %u, bitmap %p.\n",
782 iface, hotspot_x, hotspot_y, bitmap);
784 if (!bitmap)
786 WARN("No cursor bitmap, returning D3DERR_INVALIDCALL.\n");
787 return D3DERR_INVALIDCALL;
790 if (FAILED(hr = IDirect3DSurface8_GetDesc(bitmap, &surface_desc)))
792 WARN("Failed to get surface description, hr %#x.\n", hr);
793 return hr;
796 if (FAILED(hr = IDirect3D8_GetAdapterDisplayMode(&device->d3d_parent->IDirect3D8_iface,
797 device->adapter_ordinal, &mode)))
799 WARN("Failed to get device display mode, hr %#x.\n", hr);
800 return hr;
803 if (surface_desc.Width > mode.Width || surface_desc.Height > mode.Height)
805 WARN("Surface dimension %ux%u exceeds display mode %ux%u.\n", surface_desc.Width,
806 surface_desc.Height, mode.Width, mode.Height);
807 return D3DERR_INVALIDCALL;
810 wined3d_mutex_lock();
811 hr = wined3d_device_set_cursor_properties(device->wined3d_device,
812 hotspot_x, hotspot_y, bitmap_impl->wined3d_texture, bitmap_impl->sub_resource_idx);
813 wined3d_mutex_unlock();
815 return hr;
818 static void WINAPI d3d8_device_SetCursorPosition(IDirect3DDevice8 *iface, UINT x, UINT y, DWORD flags)
820 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
822 TRACE("iface %p, x %u, y %u, flags %#x.\n", iface, x, y, flags);
824 wined3d_mutex_lock();
825 wined3d_device_set_cursor_position(device->wined3d_device, x, y, flags);
826 wined3d_mutex_unlock();
829 static BOOL WINAPI d3d8_device_ShowCursor(IDirect3DDevice8 *iface, BOOL show)
831 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
832 BOOL ret;
834 TRACE("iface %p, show %#x.\n", iface, show);
836 wined3d_mutex_lock();
837 ret = wined3d_device_show_cursor(device->wined3d_device, show);
838 wined3d_mutex_unlock();
840 return ret;
843 static HRESULT WINAPI d3d8_device_CreateAdditionalSwapChain(IDirect3DDevice8 *iface,
844 D3DPRESENT_PARAMETERS *present_parameters, IDirect3DSwapChain8 **swapchain)
846 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
847 struct wined3d_swapchain_desc desc;
848 struct d3d8_swapchain *object;
849 unsigned int swap_interval;
850 unsigned int output_idx;
851 unsigned int i, count;
852 HRESULT hr;
854 TRACE("iface %p, present_parameters %p, swapchain %p.\n",
855 iface, present_parameters, swapchain);
857 if (!present_parameters->Windowed)
859 WARN("Trying to create an additional fullscreen swapchain, returning D3DERR_INVALIDCALL.\n");
860 return D3DERR_INVALIDCALL;
863 wined3d_mutex_lock();
864 count = wined3d_device_get_swapchain_count(device->wined3d_device);
865 for (i = 0; i < count; ++i)
867 struct wined3d_swapchain *wined3d_swapchain;
869 wined3d_swapchain = wined3d_device_get_swapchain(device->wined3d_device, i);
870 wined3d_swapchain_get_desc(wined3d_swapchain, &desc);
872 if (!desc.windowed)
874 wined3d_mutex_unlock();
875 WARN("Trying to create an additional swapchain in fullscreen mode, returning D3DERR_INVALIDCALL.\n");
876 return D3DERR_INVALIDCALL;
879 wined3d_mutex_unlock();
881 output_idx = device->adapter_ordinal;
882 if (!wined3d_swapchain_desc_from_d3d8(&desc, device->d3d_parent->wined3d_outputs[output_idx],
883 present_parameters))
884 return D3DERR_INVALIDCALL;
885 swap_interval = wined3dswapinterval_from_d3d(present_parameters->FullScreen_PresentationInterval);
886 if (SUCCEEDED(hr = d3d8_swapchain_create(device, &desc, swap_interval, &object)))
887 *swapchain = &object->IDirect3DSwapChain8_iface;
888 present_parameters_from_wined3d_swapchain_desc(present_parameters,
889 &desc, present_parameters->FullScreen_PresentationInterval);
891 return hr;
894 static HRESULT CDECL reset_enum_callback(struct wined3d_resource *resource)
896 struct d3d8_vertexbuffer *vertex_buffer;
897 struct d3d8_indexbuffer *index_buffer;
898 struct wined3d_resource_desc desc;
899 IDirect3DBaseTexture8 *texture;
900 struct d3d8_surface *surface;
901 IUnknown *parent;
903 wined3d_resource_get_desc(resource, &desc);
904 if (desc.access & WINED3D_RESOURCE_ACCESS_CPU)
905 return D3D_OK;
907 if (desc.resource_type != WINED3D_RTYPE_TEXTURE_2D)
909 if (desc.bind_flags & WINED3D_BIND_VERTEX_BUFFER)
911 vertex_buffer = wined3d_resource_get_parent(resource);
912 if (vertex_buffer && vertex_buffer->draw_buffer)
913 return D3D_OK;
915 if (desc.bind_flags & WINED3D_BIND_INDEX_BUFFER)
917 index_buffer = wined3d_resource_get_parent(resource);
918 if (index_buffer && index_buffer->draw_buffer)
919 return D3D_OK;
922 WARN("Resource %p in pool D3DPOOL_DEFAULT blocks the Reset call.\n", resource);
923 return D3DERR_DEVICELOST;
926 parent = wined3d_resource_get_parent(resource);
927 if (parent && SUCCEEDED(IUnknown_QueryInterface(parent, &IID_IDirect3DBaseTexture8, (void **)&texture)))
929 IDirect3DBaseTexture8_Release(texture);
930 WARN("Texture %p (resource %p) in pool D3DPOOL_DEFAULT blocks the Reset call.\n", texture, resource);
931 return D3DERR_DEVICELOST;
934 surface = wined3d_texture_get_sub_resource_parent(wined3d_texture_from_resource(resource), 0);
935 if (!surface->resource.refcount)
936 return D3D_OK;
938 WARN("Surface %p in pool D3DPOOL_DEFAULT blocks the Reset call.\n", surface);
939 return D3DERR_DEVICELOST;
942 static HRESULT WINAPI d3d8_device_Reset(IDirect3DDevice8 *iface,
943 D3DPRESENT_PARAMETERS *present_parameters)
945 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
946 struct wined3d_swapchain_desc swapchain_desc;
947 struct d3d8_swapchain *implicit_swapchain;
948 unsigned int output_idx;
949 HRESULT hr;
951 TRACE("iface %p, present_parameters %p.\n", iface, present_parameters);
953 if (device->device_state == D3D8_DEVICE_STATE_LOST)
955 WARN("App not active, returning D3DERR_DEVICELOST.\n");
956 return D3DERR_DEVICELOST;
958 output_idx = device->adapter_ordinal;
959 if (!wined3d_swapchain_desc_from_d3d8(&swapchain_desc,
960 device->d3d_parent->wined3d_outputs[output_idx], present_parameters))
961 return D3DERR_INVALIDCALL;
962 swapchain_desc.flags |= WINED3D_SWAPCHAIN_IMPLICIT;
964 wined3d_mutex_lock();
966 if (device->vertex_buffer)
968 wined3d_buffer_decref(device->vertex_buffer);
969 device->vertex_buffer = NULL;
970 device->vertex_buffer_size = 0;
972 if (device->index_buffer)
974 wined3d_buffer_decref(device->index_buffer);
975 device->index_buffer = NULL;
976 device->index_buffer_size = 0;
979 if (device->recording)
980 wined3d_stateblock_decref(device->recording);
981 device->recording = NULL;
982 device->update_state = device->state;
983 wined3d_stateblock_reset(device->state);
985 if (SUCCEEDED(hr = wined3d_device_reset(device->wined3d_device, &swapchain_desc,
986 NULL, reset_enum_callback, TRUE)))
988 present_parameters->BackBufferCount = swapchain_desc.backbuffer_count;
989 implicit_swapchain = wined3d_swapchain_get_parent(device->implicit_swapchain);
990 implicit_swapchain->swap_interval
991 = wined3dswapinterval_from_d3d(present_parameters->FullScreen_PresentationInterval);
992 wined3d_stateblock_set_render_state(device->state, WINED3D_RS_POINTSIZE_MIN, 0);
993 wined3d_stateblock_set_render_state(device->state, WINED3D_RS_ZENABLE,
994 !!swapchain_desc.enable_auto_depth_stencil);
995 device_reset_viewport_state(device);
996 device->device_state = D3D8_DEVICE_STATE_OK;
998 else
1000 device->device_state = D3D8_DEVICE_STATE_NOT_RESET;
1002 wined3d_mutex_unlock();
1004 return hr;
1007 static HRESULT WINAPI d3d8_device_Present(IDirect3DDevice8 *iface, const RECT *src_rect,
1008 const RECT *dst_rect, HWND dst_window_override, const RGNDATA *dirty_region)
1010 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1011 struct d3d8_swapchain *implicit_swapchain;
1013 TRACE("iface %p, src_rect %s, dst_rect %s, dst_window_override %p, dirty_region %p.\n",
1014 iface, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect), dst_window_override, dirty_region);
1016 /* Fraps does not hook IDirect3DDevice8::Present regardless of the hotpatch
1017 * attribute. It only hooks IDirect3DSwapChain8::Present. Yet it properly
1018 * shows a framerate on Windows in applications that only call the device
1019 * method, like e.g. the dx8 sdk samples. The conclusion is that native
1020 * calls the swapchain's public method from the device. */
1021 implicit_swapchain = wined3d_swapchain_get_parent(device->implicit_swapchain);
1022 return IDirect3DSwapChain8_Present(&implicit_swapchain->IDirect3DSwapChain8_iface,
1023 src_rect, dst_rect, dst_window_override, dirty_region);
1026 static HRESULT WINAPI d3d8_device_GetBackBuffer(IDirect3DDevice8 *iface,
1027 UINT backbuffer_idx, D3DBACKBUFFER_TYPE backbuffer_type, IDirect3DSurface8 **backbuffer)
1029 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1030 struct wined3d_texture *wined3d_texture;
1031 struct d3d8_surface *surface_impl;
1033 TRACE("iface %p, backbuffer_idx %u, backbuffer_type %#x, backbuffer %p.\n",
1034 iface, backbuffer_idx, backbuffer_type, backbuffer);
1036 /* backbuffer_type is ignored by native. */
1038 /* No need to check for backbuffer == NULL, Windows crashes in that case. */
1039 wined3d_mutex_lock();
1041 if (!(wined3d_texture = wined3d_swapchain_get_back_buffer(device->implicit_swapchain, backbuffer_idx)))
1043 wined3d_mutex_unlock();
1044 *backbuffer = NULL;
1045 return D3DERR_INVALIDCALL;
1048 surface_impl = wined3d_texture_get_sub_resource_parent(wined3d_texture, 0);
1049 *backbuffer = &surface_impl->IDirect3DSurface8_iface;
1050 IDirect3DSurface8_AddRef(*backbuffer);
1052 wined3d_mutex_unlock();
1053 return D3D_OK;
1056 static HRESULT WINAPI d3d8_device_GetRasterStatus(IDirect3DDevice8 *iface, D3DRASTER_STATUS *raster_status)
1058 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1059 HRESULT hr;
1061 TRACE("iface %p, raster_status %p.\n", iface, raster_status);
1063 wined3d_mutex_lock();
1064 hr = wined3d_device_get_raster_status(device->wined3d_device, 0, (struct wined3d_raster_status *)raster_status);
1065 wined3d_mutex_unlock();
1067 return hr;
1070 static void WINAPI d3d8_device_SetGammaRamp(IDirect3DDevice8 *iface, DWORD flags, const D3DGAMMARAMP *ramp)
1072 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1074 TRACE("iface %p, flags %#x, ramp %p.\n", iface, flags, ramp);
1076 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
1077 wined3d_mutex_lock();
1078 wined3d_device_set_gamma_ramp(device->wined3d_device, 0, flags, (const struct wined3d_gamma_ramp *)ramp);
1079 wined3d_mutex_unlock();
1082 static void WINAPI d3d8_device_GetGammaRamp(IDirect3DDevice8 *iface, D3DGAMMARAMP *ramp)
1084 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1086 TRACE("iface %p, ramp %p.\n", iface, ramp);
1088 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
1089 wined3d_mutex_lock();
1090 wined3d_device_get_gamma_ramp(device->wined3d_device, 0, (struct wined3d_gamma_ramp *)ramp);
1091 wined3d_mutex_unlock();
1094 static HRESULT WINAPI d3d8_device_CreateTexture(IDirect3DDevice8 *iface,
1095 UINT width, UINT height, UINT levels, DWORD usage, D3DFORMAT format,
1096 D3DPOOL pool, IDirect3DTexture8 **texture)
1098 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1099 struct d3d8_texture *object;
1100 HRESULT hr;
1102 TRACE("iface %p, width %u, height %u, levels %u, usage %#x, format %#x, pool %#x, texture %p.\n",
1103 iface, width, height, levels, usage, format, pool, texture);
1105 if (!format)
1106 return D3DERR_INVALIDCALL;
1108 *texture = NULL;
1109 if (!(object = heap_alloc_zero(sizeof(*object))))
1110 return D3DERR_OUTOFVIDEOMEMORY;
1112 hr = texture_init(object, device, width, height, levels, usage, format, pool);
1113 if (FAILED(hr))
1115 WARN("Failed to initialize texture, hr %#x.\n", hr);
1116 heap_free(object);
1117 return hr;
1120 TRACE("Created texture %p.\n", object);
1121 *texture = (IDirect3DTexture8 *)&object->IDirect3DBaseTexture8_iface;
1123 return D3D_OK;
1126 static HRESULT WINAPI d3d8_device_CreateVolumeTexture(IDirect3DDevice8 *iface,
1127 UINT width, UINT height, UINT depth, UINT levels, DWORD usage, D3DFORMAT format,
1128 D3DPOOL pool, IDirect3DVolumeTexture8 **texture)
1130 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1131 struct d3d8_texture *object;
1132 HRESULT hr;
1134 TRACE("iface %p, width %u, height %u, depth %u, levels %u, usage %#x, format %#x, pool %#x, texture %p.\n",
1135 iface, width, height, depth, levels, usage, format, pool, texture);
1137 if (!format)
1138 return D3DERR_INVALIDCALL;
1140 *texture = NULL;
1141 if (!(object = heap_alloc_zero(sizeof(*object))))
1142 return D3DERR_OUTOFVIDEOMEMORY;
1144 hr = volumetexture_init(object, device, width, height, depth, levels, usage, format, pool);
1145 if (FAILED(hr))
1147 WARN("Failed to initialize volume texture, hr %#x.\n", hr);
1148 heap_free(object);
1149 return hr;
1152 TRACE("Created volume texture %p.\n", object);
1153 *texture = (IDirect3DVolumeTexture8 *)&object->IDirect3DBaseTexture8_iface;
1155 return D3D_OK;
1158 static HRESULT WINAPI d3d8_device_CreateCubeTexture(IDirect3DDevice8 *iface, UINT edge_length,
1159 UINT levels, DWORD usage, D3DFORMAT format, D3DPOOL pool, IDirect3DCubeTexture8 **texture)
1161 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1162 struct d3d8_texture *object;
1163 HRESULT hr;
1165 TRACE("iface %p, edge_length %u, levels %u, usage %#x, format %#x, pool %#x, texture %p.\n",
1166 iface, edge_length, levels, usage, format, pool, texture);
1168 if (!format)
1169 return D3DERR_INVALIDCALL;
1171 *texture = NULL;
1172 if (!(object = heap_alloc_zero(sizeof(*object))))
1173 return D3DERR_OUTOFVIDEOMEMORY;
1175 hr = cubetexture_init(object, device, edge_length, levels, usage, format, pool);
1176 if (FAILED(hr))
1178 WARN("Failed to initialize cube texture, hr %#x.\n", hr);
1179 heap_free(object);
1180 return hr;
1183 TRACE("Created cube texture %p.\n", object);
1184 *texture = (IDirect3DCubeTexture8 *)&object->IDirect3DBaseTexture8_iface;
1186 return hr;
1189 static HRESULT WINAPI d3d8_device_CreateVertexBuffer(IDirect3DDevice8 *iface, UINT size,
1190 DWORD usage, DWORD fvf, D3DPOOL pool, IDirect3DVertexBuffer8 **buffer)
1192 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1193 struct d3d8_vertexbuffer *object;
1194 HRESULT hr;
1196 TRACE("iface %p, size %u, usage %#x, fvf %#x, pool %#x, buffer %p.\n",
1197 iface, size, usage, fvf, pool, buffer);
1199 if (!(object = heap_alloc_zero(sizeof(*object))))
1200 return D3DERR_OUTOFVIDEOMEMORY;
1202 hr = vertexbuffer_init(object, device, size, usage, fvf, pool);
1203 if (FAILED(hr))
1205 WARN("Failed to initialize vertex buffer, hr %#x.\n", hr);
1206 heap_free(object);
1207 return hr;
1210 TRACE("Created vertex buffer %p.\n", object);
1211 *buffer = &object->IDirect3DVertexBuffer8_iface;
1213 return D3D_OK;
1216 static HRESULT WINAPI d3d8_device_CreateIndexBuffer(IDirect3DDevice8 *iface, UINT size,
1217 DWORD usage, D3DFORMAT format, D3DPOOL pool, IDirect3DIndexBuffer8 **buffer)
1219 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1220 struct d3d8_indexbuffer *object;
1221 HRESULT hr;
1223 TRACE("iface %p, size %u, usage %#x, format %#x, pool %#x, buffer %p.\n",
1224 iface, size, usage, format, pool, buffer);
1226 if (!(object = heap_alloc_zero(sizeof(*object))))
1227 return D3DERR_OUTOFVIDEOMEMORY;
1229 hr = indexbuffer_init(object, device, size, usage, format, pool);
1230 if (FAILED(hr))
1232 WARN("Failed to initialize index buffer, hr %#x.\n", hr);
1233 heap_free(object);
1234 return hr;
1237 TRACE("Created index buffer %p.\n", object);
1238 *buffer = &object->IDirect3DIndexBuffer8_iface;
1240 return D3D_OK;
1243 static HRESULT d3d8_device_create_surface(struct d3d8_device *device, enum wined3d_format_id format,
1244 enum wined3d_multisample_type multisample_type, unsigned int bind_flags, unsigned int access,
1245 unsigned int width, unsigned int height, IDirect3DSurface8 **surface)
1247 struct wined3d_resource_desc desc;
1248 struct d3d8_surface *surface_impl;
1249 struct wined3d_texture *texture;
1250 HRESULT hr;
1252 TRACE("device %p, format %#x, multisample_type %#x, bind_flags %#x, "
1253 "access %#x, width %u, height %u, surface %p.\n",
1254 device, format, multisample_type, bind_flags, access, width, height, surface);
1256 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
1257 desc.format = format;
1258 desc.multisample_type = multisample_type;
1259 desc.multisample_quality = 0;
1260 desc.usage = 0;
1261 desc.bind_flags = bind_flags;
1262 desc.access = access;
1263 desc.width = width;
1264 desc.height = height;
1265 desc.depth = 1;
1266 desc.size = 0;
1268 wined3d_mutex_lock();
1270 if (FAILED(hr = wined3d_texture_create(device->wined3d_device, &desc,
1271 1, 1, 0, NULL, NULL, &d3d8_null_wined3d_parent_ops, &texture)))
1273 wined3d_mutex_unlock();
1274 WARN("Failed to create texture, hr %#x.\n", hr);
1275 return hr;
1278 surface_impl = wined3d_texture_get_sub_resource_parent(texture, 0);
1279 surface_impl->parent_device = &device->IDirect3DDevice8_iface;
1280 *surface = &surface_impl->IDirect3DSurface8_iface;
1281 IDirect3DSurface8_AddRef(*surface);
1282 wined3d_texture_decref(texture);
1284 wined3d_mutex_unlock();
1286 return D3D_OK;
1289 static HRESULT WINAPI d3d8_device_CreateRenderTarget(IDirect3DDevice8 *iface, UINT width,
1290 UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, BOOL lockable,
1291 IDirect3DSurface8 **surface)
1293 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1294 unsigned int access = WINED3D_RESOURCE_ACCESS_GPU;
1296 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, lockable %#x, surface %p.\n",
1297 iface, width, height, format, multisample_type, lockable, surface);
1299 if (!format)
1300 return D3DERR_INVALIDCALL;
1302 *surface = NULL;
1303 if (lockable)
1304 access |= WINED3D_RESOURCE_ACCESS_MAP_R | WINED3D_RESOURCE_ACCESS_MAP_W;
1306 return d3d8_device_create_surface(device, wined3dformat_from_d3dformat(format),
1307 wined3d_multisample_type_from_d3d(multisample_type),
1308 WINED3D_BIND_RENDER_TARGET, access, width, height, surface);
1311 static HRESULT WINAPI d3d8_device_CreateDepthStencilSurface(IDirect3DDevice8 *iface,
1312 UINT width, UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type,
1313 IDirect3DSurface8 **surface)
1315 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1317 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, surface %p.\n",
1318 iface, width, height, format, multisample_type, surface);
1320 if (!format)
1321 return D3DERR_INVALIDCALL;
1323 *surface = NULL;
1325 return d3d8_device_create_surface(device, wined3dformat_from_d3dformat(format),
1326 wined3d_multisample_type_from_d3d(multisample_type), WINED3D_BIND_DEPTH_STENCIL,
1327 WINED3D_RESOURCE_ACCESS_GPU, width, height, surface);
1330 /* IDirect3DDevice8Impl::CreateImageSurface returns surface with pool type SYSTEMMEM */
1331 static HRESULT WINAPI d3d8_device_CreateImageSurface(IDirect3DDevice8 *iface, UINT width,
1332 UINT height, D3DFORMAT format, IDirect3DSurface8 **surface)
1334 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1335 unsigned int access = WINED3D_RESOURCE_ACCESS_CPU
1336 | WINED3D_RESOURCE_ACCESS_MAP_R | WINED3D_RESOURCE_ACCESS_MAP_W;
1338 TRACE("iface %p, width %u, height %u, format %#x, surface %p.\n",
1339 iface, width, height, format, surface);
1341 *surface = NULL;
1343 return d3d8_device_create_surface(device, wined3dformat_from_d3dformat(format),
1344 WINED3D_MULTISAMPLE_NONE, 0, access, width, height, surface);
1347 static HRESULT WINAPI d3d8_device_CopyRects(IDirect3DDevice8 *iface,
1348 IDirect3DSurface8 *src_surface, const RECT *src_rects, UINT rect_count,
1349 IDirect3DSurface8 *dst_surface, const POINT *dst_points)
1351 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1352 struct d3d8_surface *src = unsafe_impl_from_IDirect3DSurface8(src_surface);
1353 struct d3d8_surface *dst = unsafe_impl_from_IDirect3DSurface8(dst_surface);
1354 enum wined3d_format_id src_format, dst_format;
1355 struct wined3d_sub_resource_desc wined3d_desc;
1356 UINT src_w, src_h;
1358 TRACE("iface %p, src_surface %p, src_rects %p, rect_count %u, dst_surface %p, dst_points %p.\n",
1359 iface, src_surface, src_rects, rect_count, dst_surface, dst_points);
1361 /* Check that the source texture is in WINED3D_POOL_SYSTEM_MEM and the
1362 * destination texture is in WINED3D_POOL_DEFAULT. */
1364 wined3d_mutex_lock();
1365 wined3d_texture_get_sub_resource_desc(src->wined3d_texture, src->sub_resource_idx, &wined3d_desc);
1366 if (wined3d_desc.bind_flags & WINED3D_BIND_DEPTH_STENCIL)
1368 WARN("Source %p is a depth stencil surface, returning D3DERR_INVALIDCALL.\n", src_surface);
1369 wined3d_mutex_unlock();
1370 return D3DERR_INVALIDCALL;
1372 src_format = wined3d_desc.format;
1373 src_w = wined3d_desc.width;
1374 src_h = wined3d_desc.height;
1376 wined3d_texture_get_sub_resource_desc(dst->wined3d_texture, dst->sub_resource_idx, &wined3d_desc);
1377 if (wined3d_desc.bind_flags & WINED3D_BIND_DEPTH_STENCIL)
1379 WARN("Destination %p is a depth stencil surface, returning D3DERR_INVALIDCALL.\n", dst_surface);
1380 wined3d_mutex_unlock();
1381 return D3DERR_INVALIDCALL;
1383 dst_format = wined3d_desc.format;
1385 /* Check that the source and destination formats match */
1386 if (src_format != dst_format)
1388 WARN("Source %p format must match the destination %p format, returning D3DERR_INVALIDCALL.\n",
1389 src_surface, dst_surface);
1390 wined3d_mutex_unlock();
1391 return D3DERR_INVALIDCALL;
1394 /* Quick if complete copy ... */
1395 if (!rect_count && !src_rects && !dst_points)
1397 RECT rect = {0, 0, src_w, src_h};
1398 wined3d_device_context_blt(device->immediate_context, dst->wined3d_texture, dst->sub_resource_idx, &rect,
1399 src->wined3d_texture, src->sub_resource_idx, &rect, 0, NULL, WINED3D_TEXF_POINT);
1401 else
1403 unsigned int i;
1404 /* Copy rect by rect */
1405 if (src_rects && dst_points)
1407 for (i = 0; i < rect_count; ++i)
1409 UINT w = src_rects[i].right - src_rects[i].left;
1410 UINT h = src_rects[i].bottom - src_rects[i].top;
1411 RECT dst_rect = {dst_points[i].x, dst_points[i].y,
1412 dst_points[i].x + w, dst_points[i].y + h};
1414 wined3d_device_context_blt(device->immediate_context,
1415 dst->wined3d_texture, dst->sub_resource_idx, &dst_rect,
1416 src->wined3d_texture, src->sub_resource_idx, &src_rects[i], 0, NULL, WINED3D_TEXF_POINT);
1419 else
1421 for (i = 0; i < rect_count; ++i)
1423 UINT w = src_rects[i].right - src_rects[i].left;
1424 UINT h = src_rects[i].bottom - src_rects[i].top;
1425 RECT dst_rect = {0, 0, w, h};
1427 wined3d_device_context_blt(device->immediate_context,
1428 dst->wined3d_texture, dst->sub_resource_idx, &dst_rect,
1429 src->wined3d_texture, src->sub_resource_idx, &src_rects[i], 0, NULL, WINED3D_TEXF_POINT);
1433 wined3d_mutex_unlock();
1435 return WINED3D_OK;
1438 static HRESULT WINAPI d3d8_device_UpdateTexture(IDirect3DDevice8 *iface,
1439 IDirect3DBaseTexture8 *src_texture, IDirect3DBaseTexture8 *dst_texture)
1441 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1442 struct d3d8_texture *src_impl, *dst_impl;
1443 HRESULT hr;
1445 TRACE("iface %p, src_texture %p, dst_texture %p.\n", iface, src_texture, dst_texture);
1447 src_impl = unsafe_impl_from_IDirect3DBaseTexture8(src_texture);
1448 dst_impl = unsafe_impl_from_IDirect3DBaseTexture8(dst_texture);
1450 wined3d_mutex_lock();
1451 hr = wined3d_device_update_texture(device->wined3d_device,
1452 src_impl->wined3d_texture, dst_impl->wined3d_texture);
1453 wined3d_mutex_unlock();
1455 return hr;
1458 static HRESULT WINAPI d3d8_device_GetFrontBuffer(IDirect3DDevice8 *iface, IDirect3DSurface8 *dst_surface)
1460 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1461 struct d3d8_surface *dst_impl = unsafe_impl_from_IDirect3DSurface8(dst_surface);
1462 HRESULT hr;
1464 TRACE("iface %p, dst_surface %p.\n", iface, dst_surface);
1466 if (!dst_surface)
1468 WARN("Invalid destination surface passed.\n");
1469 return D3DERR_INVALIDCALL;
1472 wined3d_mutex_lock();
1473 hr = wined3d_swapchain_get_front_buffer_data(device->implicit_swapchain,
1474 dst_impl->wined3d_texture, dst_impl->sub_resource_idx);
1475 wined3d_mutex_unlock();
1477 return hr;
1480 static HRESULT WINAPI d3d8_device_SetRenderTarget(IDirect3DDevice8 *iface,
1481 IDirect3DSurface8 *render_target, IDirect3DSurface8 *depth_stencil)
1483 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1484 struct d3d8_surface *rt_impl = unsafe_impl_from_IDirect3DSurface8(render_target);
1485 struct d3d8_surface *ds_impl = unsafe_impl_from_IDirect3DSurface8(depth_stencil);
1486 struct wined3d_rendertarget_view *original_dsv, *rtv;
1487 HRESULT hr = D3D_OK;
1489 TRACE("iface %p, render_target %p, depth_stencil %p.\n", iface, render_target, depth_stencil);
1491 if (rt_impl && d3d8_surface_get_device(rt_impl) != device)
1493 WARN("Render target surface does not match device.\n");
1494 return D3DERR_INVALIDCALL;
1497 wined3d_mutex_lock();
1499 if (ds_impl)
1501 struct wined3d_sub_resource_desc ds_desc, rt_desc;
1502 struct wined3d_rendertarget_view *original_rtv;
1503 struct d3d8_surface *original_surface;
1505 /* If no render target is passed in check the size against the current RT */
1506 if (!render_target)
1508 if (!(original_rtv = wined3d_device_get_rendertarget_view(device->wined3d_device, 0)))
1510 wined3d_mutex_unlock();
1511 return D3DERR_NOTFOUND;
1513 original_surface = wined3d_rendertarget_view_get_sub_resource_parent(original_rtv);
1514 wined3d_texture_get_sub_resource_desc(original_surface->wined3d_texture,
1515 original_surface->sub_resource_idx, &rt_desc);
1517 else
1518 wined3d_texture_get_sub_resource_desc(rt_impl->wined3d_texture,
1519 rt_impl->sub_resource_idx, &rt_desc);
1521 wined3d_texture_get_sub_resource_desc(ds_impl->wined3d_texture, ds_impl->sub_resource_idx, &ds_desc);
1523 if (ds_desc.width < rt_desc.width || ds_desc.height < rt_desc.height)
1525 WARN("Depth stencil is smaller than the render target, returning D3DERR_INVALIDCALL\n");
1526 wined3d_mutex_unlock();
1527 return D3DERR_INVALIDCALL;
1529 if (ds_desc.multisample_type != rt_desc.multisample_type
1530 || ds_desc.multisample_quality != rt_desc.multisample_quality)
1532 WARN("Multisample settings do not match, returning D3DERR_INVALIDCALL\n");
1533 wined3d_mutex_unlock();
1534 return D3DERR_INVALIDCALL;
1538 original_dsv = wined3d_device_get_depth_stencil_view(device->wined3d_device);
1539 rtv = ds_impl ? d3d8_surface_acquire_rendertarget_view(ds_impl) : NULL;
1540 hr = wined3d_device_context_set_depth_stencil_view(device->immediate_context, rtv);
1541 d3d8_surface_release_rendertarget_view(ds_impl, rtv);
1542 if (SUCCEEDED(hr))
1544 rtv = render_target ? d3d8_surface_acquire_rendertarget_view(rt_impl) : NULL;
1545 if (render_target)
1547 if (SUCCEEDED(hr = wined3d_device_context_set_rendertarget_view(device->immediate_context, 0, rtv, TRUE)))
1548 device_reset_viewport_state(device);
1549 else
1550 wined3d_device_context_set_depth_stencil_view(device->immediate_context, original_dsv);
1552 d3d8_surface_release_rendertarget_view(rt_impl, rtv);
1555 wined3d_mutex_unlock();
1557 return hr;
1560 static HRESULT WINAPI d3d8_device_GetRenderTarget(IDirect3DDevice8 *iface, IDirect3DSurface8 **render_target)
1562 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1563 struct wined3d_rendertarget_view *wined3d_rtv;
1564 struct d3d8_surface *surface_impl;
1565 HRESULT hr;
1567 TRACE("iface %p, render_target %p.\n", iface, render_target);
1569 if (!render_target)
1570 return D3DERR_INVALIDCALL;
1572 wined3d_mutex_lock();
1573 if ((wined3d_rtv = wined3d_device_get_rendertarget_view(device->wined3d_device, 0)))
1575 /* We want the sub resource parent here, since the view itself may be
1576 * internal to wined3d and may not have a parent. */
1577 surface_impl = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_rtv);
1578 *render_target = &surface_impl->IDirect3DSurface8_iface;
1579 IDirect3DSurface8_AddRef(*render_target);
1580 hr = D3D_OK;
1582 else
1584 ERR("Failed to get wined3d render target.\n");
1585 *render_target = NULL;
1586 hr = D3DERR_NOTFOUND;
1588 wined3d_mutex_unlock();
1590 return hr;
1593 static HRESULT WINAPI d3d8_device_GetDepthStencilSurface(IDirect3DDevice8 *iface, IDirect3DSurface8 **depth_stencil)
1595 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1596 struct wined3d_rendertarget_view *wined3d_dsv;
1597 struct d3d8_surface *surface_impl;
1598 HRESULT hr = D3D_OK;
1600 TRACE("iface %p, depth_stencil %p.\n", iface, depth_stencil);
1602 if (!depth_stencil)
1603 return D3DERR_INVALIDCALL;
1605 wined3d_mutex_lock();
1606 if ((wined3d_dsv = wined3d_device_get_depth_stencil_view(device->wined3d_device)))
1608 /* We want the sub resource parent here, since the view itself may be
1609 * internal to wined3d and may not have a parent. */
1610 surface_impl = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_dsv);
1611 *depth_stencil = &surface_impl->IDirect3DSurface8_iface;
1612 IDirect3DSurface8_AddRef(*depth_stencil);
1614 else
1616 hr = D3DERR_NOTFOUND;
1617 *depth_stencil = NULL;
1619 wined3d_mutex_unlock();
1621 return hr;
1624 static HRESULT WINAPI d3d8_device_BeginScene(IDirect3DDevice8 *iface)
1626 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1627 HRESULT hr;
1629 TRACE("iface %p.\n", iface);
1631 wined3d_mutex_lock();
1632 hr = wined3d_device_begin_scene(device->wined3d_device);
1633 wined3d_mutex_unlock();
1635 return hr;
1638 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d8_device_EndScene(IDirect3DDevice8 *iface)
1640 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1641 HRESULT hr;
1643 TRACE("iface %p.\n", iface);
1645 wined3d_mutex_lock();
1646 hr = wined3d_device_end_scene(device->wined3d_device);
1647 wined3d_mutex_unlock();
1649 return hr;
1652 static HRESULT WINAPI d3d8_device_Clear(IDirect3DDevice8 *iface, DWORD rect_count,
1653 const D3DRECT *rects, DWORD flags, D3DCOLOR color, float z, DWORD stencil)
1655 const struct wined3d_color c =
1657 ((color >> 16) & 0xff) / 255.0f,
1658 ((color >> 8) & 0xff) / 255.0f,
1659 (color & 0xff) / 255.0f,
1660 ((color >> 24) & 0xff) / 255.0f,
1662 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1663 HRESULT hr;
1665 TRACE("iface %p, rect_count %u, rects %p, flags %#x, color 0x%08x, z %.8e, stencil %u.\n",
1666 iface, rect_count, rects, flags, color, z, stencil);
1668 if (rect_count && !rects)
1670 WARN("count %u with NULL rects.\n", rect_count);
1671 rect_count = 0;
1674 wined3d_mutex_lock();
1675 wined3d_device_apply_stateblock(device->wined3d_device, device->state);
1676 hr = wined3d_device_clear(device->wined3d_device, rect_count, (const RECT *)rects, flags, &c, z, stencil);
1677 wined3d_mutex_unlock();
1679 return hr;
1682 static HRESULT WINAPI d3d8_device_SetTransform(IDirect3DDevice8 *iface,
1683 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
1685 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1687 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1689 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1690 wined3d_mutex_lock();
1691 wined3d_stateblock_set_transform(device->update_state,
1692 wined3d_transform_state_from_d3d(state), (const struct wined3d_matrix *)matrix);
1693 wined3d_mutex_unlock();
1695 return D3D_OK;
1698 static HRESULT WINAPI d3d8_device_GetTransform(IDirect3DDevice8 *iface,
1699 D3DTRANSFORMSTATETYPE state, D3DMATRIX *matrix)
1701 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1703 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1705 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1706 wined3d_mutex_lock();
1707 memcpy(matrix, &device->stateblock_state->transforms[state], sizeof(*matrix));
1708 wined3d_mutex_unlock();
1710 return D3D_OK;
1713 static HRESULT WINAPI d3d8_device_MultiplyTransform(IDirect3DDevice8 *iface,
1714 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
1716 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1718 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1720 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1721 wined3d_mutex_lock();
1722 wined3d_stateblock_multiply_transform(device->state,
1723 wined3d_transform_state_from_d3d(state), (const struct wined3d_matrix *)matrix);
1724 wined3d_mutex_unlock();
1726 return D3D_OK;
1729 static HRESULT WINAPI d3d8_device_SetViewport(IDirect3DDevice8 *iface, const D3DVIEWPORT8 *viewport)
1731 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1732 struct wined3d_sub_resource_desc rt_desc;
1733 struct wined3d_rendertarget_view *rtv;
1734 struct d3d8_surface *surface;
1735 struct wined3d_viewport vp;
1737 TRACE("iface %p, viewport %p.\n", iface, viewport);
1739 wined3d_mutex_lock();
1740 if (!(rtv = wined3d_device_get_rendertarget_view(device->wined3d_device, 0)))
1742 wined3d_mutex_unlock();
1743 return D3DERR_NOTFOUND;
1745 surface = wined3d_rendertarget_view_get_sub_resource_parent(rtv);
1746 wined3d_texture_get_sub_resource_desc(surface->wined3d_texture, surface->sub_resource_idx, &rt_desc);
1748 if (viewport->X > rt_desc.width || viewport->Width > rt_desc.width - viewport->X
1749 || viewport->Y > rt_desc.height || viewport->Height > rt_desc.height - viewport->Y)
1751 WARN("Invalid viewport, returning D3DERR_INVALIDCALL.\n");
1752 wined3d_mutex_unlock();
1753 return D3DERR_INVALIDCALL;
1756 vp.x = viewport->X;
1757 vp.y = viewport->Y;
1758 vp.width = viewport->Width;
1759 vp.height = viewport->Height;
1760 vp.min_z = viewport->MinZ;
1761 vp.max_z = viewport->MaxZ;
1763 wined3d_stateblock_set_viewport(device->update_state, &vp);
1764 wined3d_mutex_unlock();
1766 return D3D_OK;
1769 static HRESULT WINAPI d3d8_device_GetViewport(IDirect3DDevice8 *iface, D3DVIEWPORT8 *viewport)
1771 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1772 struct wined3d_viewport wined3d_viewport;
1774 TRACE("iface %p, viewport %p.\n", iface, viewport);
1776 wined3d_mutex_lock();
1777 wined3d_viewport = device->stateblock_state->viewport;
1778 wined3d_mutex_unlock();
1780 viewport->X = wined3d_viewport.x;
1781 viewport->Y = wined3d_viewport.y;
1782 viewport->Width = wined3d_viewport.width;
1783 viewport->Height = wined3d_viewport.height;
1784 viewport->MinZ = wined3d_viewport.min_z;
1785 viewport->MaxZ = wined3d_viewport.max_z;
1787 return D3D_OK;
1790 static HRESULT WINAPI d3d8_device_SetMaterial(IDirect3DDevice8 *iface, const D3DMATERIAL8 *material)
1792 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1794 TRACE("iface %p, material %p.\n", iface, material);
1796 /* Note: D3DMATERIAL8 is compatible with struct wined3d_material. */
1797 wined3d_mutex_lock();
1798 wined3d_stateblock_set_material(device->update_state, (const struct wined3d_material *)material);
1799 wined3d_mutex_unlock();
1801 return D3D_OK;
1804 static HRESULT WINAPI d3d8_device_GetMaterial(IDirect3DDevice8 *iface, D3DMATERIAL8 *material)
1806 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1808 TRACE("iface %p, material %p.\n", iface, material);
1810 /* Note: D3DMATERIAL8 is compatible with struct wined3d_material. */
1811 wined3d_mutex_lock();
1812 memcpy(material, &device->stateblock_state->material, sizeof(*material));
1813 wined3d_mutex_unlock();
1815 return D3D_OK;
1818 static HRESULT WINAPI d3d8_device_SetLight(IDirect3DDevice8 *iface, DWORD index, const D3DLIGHT8 *light)
1820 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1821 HRESULT hr;
1823 TRACE("iface %p, index %u, light %p.\n", iface, index, light);
1825 /* Note: D3DLIGHT8 is compatible with struct wined3d_light. */
1826 wined3d_mutex_lock();
1827 hr = wined3d_stateblock_set_light(device->update_state, index, (const struct wined3d_light *)light);
1828 wined3d_mutex_unlock();
1830 return hr;
1833 static HRESULT WINAPI d3d8_device_GetLight(IDirect3DDevice8 *iface, DWORD index, D3DLIGHT8 *light)
1835 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1836 BOOL enabled;
1837 HRESULT hr;
1839 TRACE("iface %p, index %u, light %p.\n", iface, index, light);
1841 /* Note: D3DLIGHT8 is compatible with struct wined3d_light. */
1842 wined3d_mutex_lock();
1843 hr = wined3d_stateblock_get_light(device->state, index, (struct wined3d_light *)light, &enabled);
1844 wined3d_mutex_unlock();
1846 return hr;
1849 static HRESULT WINAPI d3d8_device_LightEnable(IDirect3DDevice8 *iface, DWORD index, BOOL enable)
1851 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1852 HRESULT hr;
1854 TRACE("iface %p, index %u, enable %#x.\n", iface, index, enable);
1856 wined3d_mutex_lock();
1857 hr = wined3d_stateblock_set_light_enable(device->update_state, index, enable);
1858 wined3d_mutex_unlock();
1860 return hr;
1863 static HRESULT WINAPI d3d8_device_GetLightEnable(IDirect3DDevice8 *iface, DWORD index, BOOL *enabled)
1865 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1866 struct wined3d_light light;
1867 HRESULT hr;
1869 TRACE("iface %p, index %u, enabled %p.\n", iface, index, enabled);
1871 wined3d_mutex_lock();
1872 hr = wined3d_stateblock_get_light(device->state, index, &light, enabled);
1873 wined3d_mutex_unlock();
1875 return hr;
1878 static HRESULT WINAPI d3d8_device_SetClipPlane(IDirect3DDevice8 *iface, DWORD index, const float *plane)
1880 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1881 HRESULT hr;
1883 TRACE("iface %p, index %u, plane %p.\n", iface, index, plane);
1885 wined3d_mutex_lock();
1886 hr = wined3d_stateblock_set_clip_plane(device->update_state, index, (const struct wined3d_vec4 *)plane);
1887 wined3d_mutex_unlock();
1889 return hr;
1892 static HRESULT WINAPI d3d8_device_GetClipPlane(IDirect3DDevice8 *iface, DWORD index, float *plane)
1894 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1896 TRACE("iface %p, index %u, plane %p.\n", iface, index, plane);
1898 if (index >= device->max_user_clip_planes)
1900 TRACE("Plane %u requested, but only %u planes are supported.\n", index, device->max_user_clip_planes);
1901 return WINED3DERR_INVALIDCALL;
1904 wined3d_mutex_lock();
1905 memcpy(plane, &device->stateblock_state->clip_planes[index], sizeof(struct wined3d_vec4));
1906 wined3d_mutex_unlock();
1908 return D3D_OK;
1911 static void resolve_depth_buffer(struct d3d8_device *device)
1913 const struct wined3d_stateblock_state *state = device->stateblock_state;
1914 struct wined3d_rendertarget_view *wined3d_dsv;
1915 struct wined3d_resource *dst_resource;
1916 struct wined3d_texture *dst_texture;
1917 struct wined3d_resource_desc desc;
1918 struct d3d8_surface *d3d8_dsv;
1920 if (!(dst_texture = state->textures[0]))
1921 return;
1922 dst_resource = wined3d_texture_get_resource(dst_texture);
1923 wined3d_resource_get_desc(dst_resource, &desc);
1924 if (desc.format != WINED3DFMT_D24_UNORM_S8_UINT
1925 && desc.format != WINED3DFMT_X8D24_UNORM
1926 && desc.format != WINED3DFMT_DF16
1927 && desc.format != WINED3DFMT_DF24
1928 && desc.format != WINED3DFMT_INTZ)
1929 return;
1931 if (!(wined3d_dsv = wined3d_device_get_depth_stencil_view(device->wined3d_device)))
1932 return;
1933 d3d8_dsv = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_dsv);
1935 wined3d_device_context_resolve_sub_resource(device->immediate_context, dst_resource, 0,
1936 wined3d_rendertarget_view_get_resource(wined3d_dsv), d3d8_dsv->sub_resource_idx, desc.format);
1939 static HRESULT WINAPI d3d8_device_SetRenderState(IDirect3DDevice8 *iface,
1940 D3DRENDERSTATETYPE state, DWORD value)
1942 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1944 TRACE("iface %p, state %#x, value %#x.\n", iface, state, value);
1946 wined3d_mutex_lock();
1947 wined3d_stateblock_set_render_state(device->update_state, wined3d_render_state_from_d3d(state), value);
1948 if (state == D3DRS_POINTSIZE && value == D3D8_RESZ_CODE)
1949 resolve_depth_buffer(device);
1950 wined3d_mutex_unlock();
1952 return D3D_OK;
1955 static HRESULT WINAPI d3d8_device_GetRenderState(IDirect3DDevice8 *iface,
1956 D3DRENDERSTATETYPE state, DWORD *value)
1958 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1959 const struct wined3d_stateblock_state *device_state;
1961 TRACE("iface %p, state %#x, value %p.\n", iface, state, value);
1963 wined3d_mutex_lock();
1964 device_state = device->stateblock_state;
1965 switch (state)
1967 case D3DRS_ZBIAS:
1968 *value = device_state->rs[WINED3D_RS_DEPTHBIAS];
1969 break;
1971 default:
1972 *value = device_state->rs[state];
1974 wined3d_mutex_unlock();
1976 return D3D_OK;
1979 static HRESULT WINAPI d3d8_device_BeginStateBlock(IDirect3DDevice8 *iface)
1981 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1982 struct wined3d_stateblock *stateblock;
1983 HRESULT hr;
1985 TRACE("iface %p.\n", iface);
1987 wined3d_mutex_lock();
1988 if (device->recording)
1990 wined3d_mutex_unlock();
1991 WARN("Trying to begin a stateblock while recording, returning D3DERR_INVALIDCALL.\n");
1992 return D3DERR_INVALIDCALL;
1995 if (SUCCEEDED(hr = wined3d_stateblock_create(device->wined3d_device, NULL, WINED3D_SBT_RECORDED, &stateblock)))
1996 device->update_state = device->recording = stateblock;
1997 wined3d_mutex_unlock();
1999 return hr;
2002 static HRESULT WINAPI d3d8_device_EndStateBlock(IDirect3DDevice8 *iface, DWORD *token)
2004 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2005 struct wined3d_stateblock *stateblock;
2007 TRACE("iface %p, token %p.\n", iface, token);
2009 /* Tell wineD3D to endstateblock before anything else (in case we run out
2010 * of memory later and cause locking problems)
2012 wined3d_mutex_lock();
2013 if (!device->recording)
2015 wined3d_mutex_unlock();
2016 WARN("Trying to end a stateblock, but no stateblock is being recorded.\n");
2017 return D3DERR_INVALIDCALL;
2019 stateblock = device->recording;
2020 wined3d_stateblock_init_contained_states(stateblock);
2021 device->recording = NULL;
2022 device->update_state = device->state;
2024 *token = d3d8_allocate_handle(&device->handle_table, stateblock, D3D8_HANDLE_SB);
2025 wined3d_mutex_unlock();
2027 if (*token == D3D8_INVALID_HANDLE)
2029 ERR("Failed to create a handle\n");
2030 wined3d_mutex_lock();
2031 wined3d_stateblock_decref(stateblock);
2032 wined3d_mutex_unlock();
2033 return E_FAIL;
2035 ++*token;
2037 TRACE("Returning %#x (%p).\n", *token, stateblock);
2039 return D3D_OK;
2042 static HRESULT WINAPI d3d8_device_ApplyStateBlock(IDirect3DDevice8 *iface, DWORD token)
2044 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2045 const struct wined3d_stateblock_state *state;
2046 struct wined3d_stateblock *stateblock;
2047 struct wined3d_buffer *wined3d_buffer;
2048 struct d3d8_vertexbuffer *buffer;
2049 unsigned int i;
2051 TRACE("iface %p, token %#x.\n", iface, token);
2053 if (!token)
2054 return D3D_OK;
2056 wined3d_mutex_lock();
2057 if (device->recording)
2059 wined3d_mutex_unlock();
2060 WARN("Trying to apply stateblock while recording, returning D3DERR_INVALIDCALL.\n");
2061 return D3DERR_INVALIDCALL;
2063 stateblock = d3d8_get_object(&device->handle_table, token - 1, D3D8_HANDLE_SB);
2064 if (!stateblock)
2066 WARN("Invalid handle (%#x) passed.\n", token);
2067 wined3d_mutex_unlock();
2068 return D3DERR_INVALIDCALL;
2070 wined3d_stateblock_apply(stateblock, device->state);
2071 device->sysmem_vb = 0;
2072 state = device->stateblock_state;
2073 for (i = 0; i < D3D8_MAX_STREAMS; ++i)
2075 wined3d_buffer = state->streams[i].buffer;
2076 if (!wined3d_buffer || !(buffer = wined3d_buffer_get_parent(wined3d_buffer)))
2077 continue;
2078 if (buffer->draw_buffer)
2079 device->sysmem_vb |= 1u << i;
2081 device->sysmem_ib = (wined3d_buffer = state->index_buffer)
2082 && (buffer = wined3d_buffer_get_parent(wined3d_buffer)) && buffer->draw_buffer;
2083 wined3d_mutex_unlock();
2085 return D3D_OK;
2088 static HRESULT WINAPI d3d8_device_CaptureStateBlock(IDirect3DDevice8 *iface, DWORD token)
2090 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2091 struct wined3d_stateblock *stateblock;
2093 TRACE("iface %p, token %#x.\n", iface, token);
2095 wined3d_mutex_lock();
2096 if (device->recording)
2098 wined3d_mutex_unlock();
2099 WARN("Trying to capture stateblock while recording, returning D3DERR_INVALIDCALL.\n");
2100 return D3DERR_INVALIDCALL;
2102 stateblock = d3d8_get_object(&device->handle_table, token - 1, D3D8_HANDLE_SB);
2103 if (!stateblock)
2105 WARN("Invalid handle (%#x) passed.\n", token);
2106 wined3d_mutex_unlock();
2107 return D3DERR_INVALIDCALL;
2109 wined3d_stateblock_capture(stateblock, device->state);
2110 wined3d_mutex_unlock();
2112 return D3D_OK;
2115 static HRESULT WINAPI d3d8_device_DeleteStateBlock(IDirect3DDevice8 *iface, DWORD token)
2117 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2118 struct wined3d_stateblock *stateblock;
2120 TRACE("iface %p, token %#x.\n", iface, token);
2122 wined3d_mutex_lock();
2123 stateblock = d3d8_free_handle(&device->handle_table, token - 1, D3D8_HANDLE_SB);
2125 if (!stateblock)
2127 WARN("Invalid handle (%#x) passed.\n", token);
2128 wined3d_mutex_unlock();
2129 return D3DERR_INVALIDCALL;
2132 if (wined3d_stateblock_decref(stateblock))
2134 ERR("Stateblock %p has references left, this shouldn't happen.\n", stateblock);
2136 wined3d_mutex_unlock();
2138 return D3D_OK;
2141 static HRESULT WINAPI d3d8_device_CreateStateBlock(IDirect3DDevice8 *iface,
2142 D3DSTATEBLOCKTYPE type, DWORD *handle)
2144 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2145 struct wined3d_stateblock *stateblock;
2146 HRESULT hr;
2148 TRACE("iface %p, type %#x, handle %p.\n", iface, type, handle);
2150 if (type != D3DSBT_ALL
2151 && type != D3DSBT_PIXELSTATE
2152 && type != D3DSBT_VERTEXSTATE)
2154 WARN("Unexpected stateblock type, returning D3DERR_INVALIDCALL\n");
2155 return D3DERR_INVALIDCALL;
2158 wined3d_mutex_lock();
2159 if (device->recording)
2161 wined3d_mutex_unlock();
2162 WARN("Trying to create a stateblock while recording, returning D3DERR_INVALIDCALL.\n");
2163 return D3DERR_INVALIDCALL;
2165 hr = wined3d_stateblock_create(device->wined3d_device, device->state, (enum wined3d_stateblock_type)type, &stateblock);
2166 if (FAILED(hr))
2168 wined3d_mutex_unlock();
2169 ERR("Failed to create the state block, hr %#x\n", hr);
2170 return hr;
2173 *handle = d3d8_allocate_handle(&device->handle_table, stateblock, D3D8_HANDLE_SB);
2174 wined3d_mutex_unlock();
2176 if (*handle == D3D8_INVALID_HANDLE)
2178 ERR("Failed to allocate a handle.\n");
2179 wined3d_mutex_lock();
2180 wined3d_stateblock_decref(stateblock);
2181 wined3d_mutex_unlock();
2182 return E_FAIL;
2184 ++*handle;
2186 TRACE("Returning %#x (%p).\n", *handle, stateblock);
2188 return hr;
2191 static HRESULT WINAPI d3d8_device_SetClipStatus(IDirect3DDevice8 *iface, const D3DCLIPSTATUS8 *clip_status)
2193 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2194 HRESULT hr;
2196 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
2197 /* FIXME: Verify that D3DCLIPSTATUS8 ~= struct wined3d_clip_status. */
2199 wined3d_mutex_lock();
2200 hr = wined3d_device_set_clip_status(device->wined3d_device, (const struct wined3d_clip_status *)clip_status);
2201 wined3d_mutex_unlock();
2203 return hr;
2206 static HRESULT WINAPI d3d8_device_GetClipStatus(IDirect3DDevice8 *iface, D3DCLIPSTATUS8 *clip_status)
2208 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2209 HRESULT hr;
2211 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
2213 wined3d_mutex_lock();
2214 hr = wined3d_device_get_clip_status(device->wined3d_device, (struct wined3d_clip_status *)clip_status);
2215 wined3d_mutex_unlock();
2217 return hr;
2220 static HRESULT WINAPI d3d8_device_GetTexture(IDirect3DDevice8 *iface, DWORD stage, IDirect3DBaseTexture8 **texture)
2222 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2223 struct wined3d_texture *wined3d_texture;
2224 struct d3d8_texture *texture_impl;
2226 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
2228 if (!texture)
2229 return D3DERR_INVALIDCALL;
2231 if (stage >= WINED3D_MAX_FRAGMENT_SAMPLERS)
2233 WARN("Ignoring invalid stage %u.\n", stage);
2234 *texture = NULL;
2235 return D3D_OK;
2238 wined3d_mutex_lock();
2239 if ((wined3d_texture = device->stateblock_state->textures[stage]))
2241 texture_impl = wined3d_texture_get_parent(wined3d_texture);
2242 *texture = &texture_impl->IDirect3DBaseTexture8_iface;
2243 IDirect3DBaseTexture8_AddRef(*texture);
2245 else
2247 *texture = NULL;
2249 wined3d_mutex_unlock();
2251 return D3D_OK;
2254 static HRESULT WINAPI d3d8_device_SetTexture(IDirect3DDevice8 *iface, DWORD stage, IDirect3DBaseTexture8 *texture)
2256 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2257 struct d3d8_texture *texture_impl;
2259 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
2261 texture_impl = unsafe_impl_from_IDirect3DBaseTexture8(texture);
2263 wined3d_mutex_lock();
2264 wined3d_stateblock_set_texture(device->update_state, stage,
2265 texture_impl ? texture_impl->wined3d_texture : NULL);
2266 wined3d_mutex_unlock();
2268 return D3D_OK;
2271 static const struct tss_lookup
2273 BOOL sampler_state;
2274 union
2276 enum wined3d_texture_stage_state texture_state;
2277 enum wined3d_sampler_state sampler_state;
2278 } u;
2280 tss_lookup[] =
2282 {FALSE, {WINED3D_TSS_INVALID}}, /* 0, unused */
2283 {FALSE, {WINED3D_TSS_COLOR_OP}}, /* 1, D3DTSS_COLOROP */
2284 {FALSE, {WINED3D_TSS_COLOR_ARG1}}, /* 2, D3DTSS_COLORARG1 */
2285 {FALSE, {WINED3D_TSS_COLOR_ARG2}}, /* 3, D3DTSS_COLORARG2 */
2286 {FALSE, {WINED3D_TSS_ALPHA_OP}}, /* 4, D3DTSS_ALPHAOP */
2287 {FALSE, {WINED3D_TSS_ALPHA_ARG1}}, /* 5, D3DTSS_ALPHAARG1 */
2288 {FALSE, {WINED3D_TSS_ALPHA_ARG2}}, /* 6, D3DTSS_ALPHAARG2 */
2289 {FALSE, {WINED3D_TSS_BUMPENV_MAT00}}, /* 7, D3DTSS_BUMPENVMAT00 */
2290 {FALSE, {WINED3D_TSS_BUMPENV_MAT01}}, /* 8, D3DTSS_BUMPENVMAT01 */
2291 {FALSE, {WINED3D_TSS_BUMPENV_MAT10}}, /* 9, D3DTSS_BUMPENVMAT10 */
2292 {FALSE, {WINED3D_TSS_BUMPENV_MAT11}}, /* 10, D3DTSS_BUMPENVMAT11 */
2293 {FALSE, {WINED3D_TSS_TEXCOORD_INDEX}}, /* 11, D3DTSS_TEXCOORDINDEX */
2294 {FALSE, {WINED3D_TSS_INVALID}}, /* 12, unused */
2295 {TRUE, {WINED3D_SAMP_ADDRESS_U}}, /* 13, D3DTSS_ADDRESSU */
2296 {TRUE, {WINED3D_SAMP_ADDRESS_V}}, /* 14, D3DTSS_ADDRESSV */
2297 {TRUE, {WINED3D_SAMP_BORDER_COLOR}}, /* 15, D3DTSS_BORDERCOLOR */
2298 {TRUE, {WINED3D_SAMP_MAG_FILTER}}, /* 16, D3DTSS_MAGFILTER */
2299 {TRUE, {WINED3D_SAMP_MIN_FILTER}}, /* 17, D3DTSS_MINFILTER */
2300 {TRUE, {WINED3D_SAMP_MIP_FILTER}}, /* 18, D3DTSS_MIPFILTER */
2301 {TRUE, {WINED3D_SAMP_MIPMAP_LOD_BIAS}}, /* 19, D3DTSS_MIPMAPLODBIAS */
2302 {TRUE, {WINED3D_SAMP_MAX_MIP_LEVEL}}, /* 20, D3DTSS_MAXMIPLEVEL */
2303 {TRUE, {WINED3D_SAMP_MAX_ANISOTROPY}}, /* 21, D3DTSS_MAXANISOTROPY */
2304 {FALSE, {WINED3D_TSS_BUMPENV_LSCALE}}, /* 22, D3DTSS_BUMPENVLSCALE */
2305 {FALSE, {WINED3D_TSS_BUMPENV_LOFFSET}}, /* 23, D3DTSS_BUMPENVLOFFSET */
2306 {FALSE, {WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS}}, /* 24, D3DTSS_TEXTURETRANSFORMFLAGS */
2307 {TRUE, {WINED3D_SAMP_ADDRESS_W}}, /* 25, D3DTSS_ADDRESSW */
2308 {FALSE, {WINED3D_TSS_COLOR_ARG0}}, /* 26, D3DTSS_COLORARG0 */
2309 {FALSE, {WINED3D_TSS_ALPHA_ARG0}}, /* 27, D3DTSS_ALPHAARG0 */
2310 {FALSE, {WINED3D_TSS_RESULT_ARG}}, /* 28, D3DTSS_RESULTARG */
2313 static HRESULT WINAPI d3d8_device_GetTextureStageState(IDirect3DDevice8 *iface,
2314 DWORD stage, D3DTEXTURESTAGESTATETYPE state, DWORD *value)
2316 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2317 const struct wined3d_stateblock_state *device_state;
2318 const struct tss_lookup *l;
2320 TRACE("iface %p, stage %u, state %#x, value %p.\n", iface, stage, state, value);
2322 if (state >= ARRAY_SIZE(tss_lookup))
2324 WARN("Invalid state %#x.\n", state);
2325 return D3D_OK;
2328 l = &tss_lookup[state];
2330 wined3d_mutex_lock();
2331 device_state = device->stateblock_state;
2332 if (l->sampler_state)
2333 *value = device_state->sampler_states[stage][l->u.sampler_state];
2334 else
2335 *value = device_state->texture_states[stage][l->u.texture_state];
2336 wined3d_mutex_unlock();
2338 return D3D_OK;
2341 static HRESULT WINAPI d3d8_device_SetTextureStageState(IDirect3DDevice8 *iface,
2342 DWORD stage, D3DTEXTURESTAGESTATETYPE type, DWORD value)
2344 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2345 const struct tss_lookup *l;
2347 TRACE("iface %p, stage %u, state %#x, value %#x.\n", iface, stage, type, value);
2349 if (type >= ARRAY_SIZE(tss_lookup))
2351 WARN("Invalid type %#x passed.\n", type);
2352 return D3D_OK;
2355 l = &tss_lookup[type];
2357 wined3d_mutex_lock();
2358 if (l->sampler_state)
2359 wined3d_stateblock_set_sampler_state(device->update_state, stage, l->u.sampler_state, value);
2360 else
2361 wined3d_stateblock_set_texture_stage_state(device->update_state, stage, l->u.texture_state, value);
2362 wined3d_mutex_unlock();
2364 return D3D_OK;
2367 static HRESULT WINAPI d3d8_device_ValidateDevice(IDirect3DDevice8 *iface, DWORD *pass_count)
2369 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2370 HRESULT hr;
2372 TRACE("iface %p, pass_count %p.\n", iface, pass_count);
2374 wined3d_mutex_lock();
2375 wined3d_device_apply_stateblock(device->wined3d_device, device->state);
2376 hr = wined3d_device_validate_device(device->wined3d_device, pass_count);
2377 wined3d_mutex_unlock();
2379 return hr;
2382 static HRESULT WINAPI d3d8_device_GetInfo(IDirect3DDevice8 *iface,
2383 DWORD info_id, void *info, DWORD info_size)
2385 TRACE("iface %p, info_id %#x, info %p, info_size %u.\n", iface, info_id, info, info_size);
2387 if (info_id < 4)
2388 return E_FAIL;
2389 return S_FALSE;
2392 static HRESULT WINAPI d3d8_device_SetPaletteEntries(IDirect3DDevice8 *iface,
2393 UINT palette_idx, const PALETTEENTRY *entries)
2395 WARN("iface %p, palette_idx %u, entries %p unimplemented\n", iface, palette_idx, entries);
2397 /* GPUs stopped supporting palettized textures with the Shader Model 1 generation. Wined3d
2398 * does not have a d3d8/9-style palette API */
2400 return D3D_OK;
2403 static HRESULT WINAPI d3d8_device_GetPaletteEntries(IDirect3DDevice8 *iface,
2404 UINT palette_idx, PALETTEENTRY *entries)
2406 FIXME("iface %p, palette_idx %u, entries %p unimplemented.\n", iface, palette_idx, entries);
2408 return D3DERR_INVALIDCALL;
2411 static HRESULT WINAPI d3d8_device_SetCurrentTexturePalette(IDirect3DDevice8 *iface, UINT palette_idx)
2413 WARN("iface %p, palette_idx %u unimplemented.\n", iface, palette_idx);
2415 return D3D_OK;
2418 static HRESULT WINAPI d3d8_device_GetCurrentTexturePalette(IDirect3DDevice8 *iface, UINT *palette_idx)
2420 FIXME("iface %p, palette_idx %p unimplemented.\n", iface, palette_idx);
2422 return D3DERR_INVALIDCALL;
2425 static void d3d8_device_upload_sysmem_vertex_buffers(struct d3d8_device *device,
2426 unsigned int start_vertex, unsigned int vertex_count)
2428 const struct wined3d_stateblock_state *state = device->stateblock_state;
2429 struct wined3d_vertex_declaration *wined3d_decl;
2430 struct wined3d_box box = {0, 0, 0, 1, 0, 1};
2431 const struct wined3d_stream_state *stream;
2432 struct d3d8_vertexbuffer *d3d8_buffer;
2433 struct wined3d_resource *dst_resource;
2434 struct d3d8_vertex_declaration *decl;
2435 struct wined3d_buffer *dst_buffer;
2436 struct wined3d_resource_desc desc;
2437 unsigned int stride, map;
2438 HRESULT hr;
2440 if (!device->sysmem_vb)
2441 return;
2443 if (!(wined3d_decl = state->vertex_declaration))
2444 return;
2446 decl = wined3d_vertex_declaration_get_parent(wined3d_decl);
2447 map = decl->stream_map & device->sysmem_vb;
2448 while (map)
2450 stream = &state->streams[wined3d_bit_scan(&map)];
2451 dst_buffer = stream->buffer;
2452 stride = stream->stride;
2454 d3d8_buffer = wined3d_buffer_get_parent(dst_buffer);
2455 dst_resource = wined3d_buffer_get_resource(dst_buffer);
2456 wined3d_resource_get_desc(dst_resource, &desc);
2457 box.left = stream->offset + start_vertex * stride;
2458 box.right = min(box.left + vertex_count * stride, desc.size);
2459 if (FAILED(hr = wined3d_device_context_copy_sub_resource_region(device->immediate_context,
2460 dst_resource, 0, box.left, 0, 0,
2461 wined3d_buffer_get_resource(d3d8_buffer->wined3d_buffer), 0, &box, 0)))
2462 ERR("Failed to update buffer.\n");
2466 static void d3d8_device_upload_sysmem_index_buffer(struct d3d8_device *device,
2467 unsigned int start_idx, unsigned int idx_count)
2469 const struct wined3d_stateblock_state *state = device->stateblock_state;
2470 struct wined3d_box box = {0, 0, 0, 1, 0, 1};
2471 struct wined3d_resource *dst_resource;
2472 struct d3d8_indexbuffer *d3d8_buffer;
2473 struct wined3d_buffer *dst_buffer;
2474 struct wined3d_resource_desc desc;
2475 enum wined3d_format_id format;
2476 unsigned int idx_size;
2477 HRESULT hr;
2479 if (!device->sysmem_ib)
2480 return;
2482 dst_buffer = state->index_buffer;
2483 format = state->index_format;
2484 d3d8_buffer = wined3d_buffer_get_parent(dst_buffer);
2485 dst_resource = wined3d_buffer_get_resource(dst_buffer);
2486 wined3d_resource_get_desc(dst_resource, &desc);
2487 idx_size = format == WINED3DFMT_R16_UINT ? 2 : 4;
2488 box.left = start_idx * idx_size;
2489 box.right = min(box.left + idx_count * idx_size, desc.size);
2490 if (FAILED(hr = wined3d_device_context_copy_sub_resource_region(device->immediate_context,
2491 dst_resource, 0, box.left, 0, 0,
2492 wined3d_buffer_get_resource(d3d8_buffer->wined3d_buffer), 0, &box, 0)))
2493 ERR("Failed to update buffer.\n");
2496 static HRESULT WINAPI d3d8_device_DrawPrimitive(IDirect3DDevice8 *iface,
2497 D3DPRIMITIVETYPE primitive_type, UINT start_vertex, UINT primitive_count)
2499 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2500 unsigned int vertex_count;
2502 TRACE("iface %p, primitive_type %#x, start_vertex %u, primitive_count %u.\n",
2503 iface, primitive_type, start_vertex, primitive_count);
2505 vertex_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2506 wined3d_mutex_lock();
2507 d3d8_device_upload_sysmem_vertex_buffers(device, start_vertex, vertex_count);
2508 wined3d_device_set_primitive_type(device->wined3d_device, wined3d_primitive_type_from_d3d(primitive_type), 0);
2509 wined3d_device_apply_stateblock(device->wined3d_device, device->state);
2510 wined3d_device_context_draw(device->immediate_context, start_vertex, vertex_count, 0, 0);
2511 wined3d_mutex_unlock();
2513 return D3D_OK;
2516 static HRESULT WINAPI d3d8_device_DrawIndexedPrimitive(IDirect3DDevice8 *iface,
2517 D3DPRIMITIVETYPE primitive_type, UINT min_vertex_idx, UINT vertex_count,
2518 UINT start_idx, UINT primitive_count)
2520 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2521 unsigned int index_count;
2522 int base_vertex_index;
2524 TRACE("iface %p, primitive_type %#x, min_vertex_idx %u, vertex_count %u, start_idx %u, primitive_count %u.\n",
2525 iface, primitive_type, min_vertex_idx, vertex_count, start_idx, primitive_count);
2527 index_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2528 wined3d_mutex_lock();
2529 if (!device->stateblock_state->index_buffer)
2531 wined3d_mutex_unlock();
2532 WARN("Index buffer not set, returning D3D_OK.\n");
2533 return D3D_OK;
2535 base_vertex_index = device->stateblock_state->base_vertex_index;
2536 d3d8_device_upload_sysmem_vertex_buffers(device, base_vertex_index + min_vertex_idx, vertex_count);
2537 d3d8_device_upload_sysmem_index_buffer(device, start_idx, index_count);
2538 wined3d_device_set_primitive_type(device->wined3d_device, wined3d_primitive_type_from_d3d(primitive_type), 0);
2539 wined3d_device_apply_stateblock(device->wined3d_device, device->state);
2540 wined3d_device_context_draw_indexed(device->immediate_context, base_vertex_index, start_idx, index_count, 0, 0);
2541 wined3d_mutex_unlock();
2543 return D3D_OK;
2546 /* The caller is responsible for wined3d locking */
2547 static HRESULT d3d8_device_prepare_vertex_buffer(struct d3d8_device *device, UINT min_size)
2549 HRESULT hr;
2551 if (device->vertex_buffer_size < min_size || !device->vertex_buffer)
2553 UINT size = max(device->vertex_buffer_size * 2, min_size);
2554 struct wined3d_buffer_desc desc;
2555 struct wined3d_buffer *buffer;
2557 TRACE("Growing vertex buffer to %u bytes\n", size);
2559 desc.byte_width = size;
2560 desc.usage = WINED3DUSAGE_DYNAMIC;
2561 desc.bind_flags = WINED3D_BIND_VERTEX_BUFFER;
2562 desc.access = WINED3D_RESOURCE_ACCESS_GPU | WINED3D_RESOURCE_ACCESS_MAP_W;
2563 desc.misc_flags = 0;
2564 desc.structure_byte_stride = 0;
2566 if (FAILED(hr = wined3d_buffer_create(device->wined3d_device, &desc,
2567 NULL, NULL, &d3d8_null_wined3d_parent_ops, &buffer)))
2569 ERR("Failed to create vertex buffer, hr %#x.\n", hr);
2570 return hr;
2573 if (device->vertex_buffer)
2574 wined3d_buffer_decref(device->vertex_buffer);
2576 device->vertex_buffer = buffer;
2577 device->vertex_buffer_size = size;
2578 device->vertex_buffer_pos = 0;
2580 return D3D_OK;
2583 static HRESULT WINAPI d3d8_device_DrawPrimitiveUP(IDirect3DDevice8 *iface,
2584 D3DPRIMITIVETYPE primitive_type, UINT primitive_count, const void *data,
2585 UINT stride)
2587 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2588 HRESULT hr;
2589 UINT vtx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2590 struct wined3d_map_desc wined3d_map_desc;
2591 struct wined3d_box wined3d_box = {0};
2592 UINT size = vtx_count * stride;
2593 struct wined3d_resource *vb;
2594 UINT vb_pos, align;
2596 TRACE("iface %p, primitive_type %#x, primitive_count %u, data %p, stride %u.\n",
2597 iface, primitive_type, primitive_count, data, stride);
2599 if (!primitive_count || !stride)
2601 WARN("primitive_count or stride is 0, returning D3D_OK.\n");
2602 return D3D_OK;
2605 wined3d_mutex_lock();
2606 hr = d3d8_device_prepare_vertex_buffer(device, size);
2607 if (FAILED(hr))
2608 goto done;
2610 vb_pos = device->vertex_buffer_pos;
2611 align = vb_pos % stride;
2612 if (align) align = stride - align;
2613 if (vb_pos + size + align > device->vertex_buffer_size)
2614 vb_pos = 0;
2615 else
2616 vb_pos += align;
2618 wined3d_box.left = vb_pos;
2619 wined3d_box.right = vb_pos + size;
2620 vb = wined3d_buffer_get_resource(device->vertex_buffer);
2621 if (FAILED(wined3d_resource_map(vb, 0, &wined3d_map_desc, &wined3d_box,
2622 WINED3D_MAP_WRITE | (vb_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD))))
2623 goto done;
2624 memcpy(wined3d_map_desc.data, data, size);
2625 wined3d_resource_unmap(vb, 0);
2626 device->vertex_buffer_pos = vb_pos + size;
2628 hr = wined3d_stateblock_set_stream_source(device->state, 0, device->vertex_buffer, 0, stride);
2629 if (FAILED(hr))
2630 goto done;
2632 wined3d_device_set_primitive_type(device->wined3d_device, wined3d_primitive_type_from_d3d(primitive_type), 0);
2633 wined3d_device_apply_stateblock(device->wined3d_device, device->state);
2634 wined3d_device_context_draw(device->immediate_context, vb_pos / stride, vtx_count, 0, 0);
2635 wined3d_stateblock_set_stream_source(device->state, 0, NULL, 0, 0);
2637 done:
2638 wined3d_mutex_unlock();
2639 return hr;
2642 /* The caller is responsible for wined3d locking */
2643 static HRESULT d3d8_device_prepare_index_buffer(struct d3d8_device *device, UINT min_size)
2645 HRESULT hr;
2647 if (device->index_buffer_size < min_size || !device->index_buffer)
2649 UINT size = max(device->index_buffer_size * 2, min_size);
2650 struct wined3d_buffer_desc desc;
2651 struct wined3d_buffer *buffer;
2653 TRACE("Growing index buffer to %u bytes\n", size);
2655 desc.byte_width = size;
2656 desc.usage = WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_STATICDECL;
2657 desc.bind_flags = WINED3D_BIND_INDEX_BUFFER;
2658 desc.access = WINED3D_RESOURCE_ACCESS_GPU | WINED3D_RESOURCE_ACCESS_MAP_W;
2659 desc.misc_flags = 0;
2660 desc.structure_byte_stride = 0;
2662 if (FAILED(hr = wined3d_buffer_create(device->wined3d_device, &desc,
2663 NULL, NULL, &d3d8_null_wined3d_parent_ops, &buffer)))
2665 ERR("Failed to create index buffer, hr %#x.\n", hr);
2666 return hr;
2669 if (device->index_buffer)
2670 wined3d_buffer_decref(device->index_buffer);
2672 device->index_buffer = buffer;
2673 device->index_buffer_size = size;
2674 device->index_buffer_pos = 0;
2676 return D3D_OK;
2679 static HRESULT WINAPI d3d8_device_DrawIndexedPrimitiveUP(IDirect3DDevice8 *iface,
2680 D3DPRIMITIVETYPE primitive_type, UINT min_vertex_idx, UINT vertex_count,
2681 UINT primitive_count, const void *index_data, D3DFORMAT index_format,
2682 const void *vertex_data, UINT vertex_stride)
2684 UINT idx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2685 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2686 UINT idx_fmt_size = index_format == D3DFMT_INDEX16 ? 2 : 4;
2687 UINT vtx_size = vertex_count * vertex_stride;
2688 UINT idx_size = idx_count * idx_fmt_size;
2689 struct wined3d_map_desc wined3d_map_desc;
2690 struct wined3d_box wined3d_box = {0};
2691 struct wined3d_resource *ib, *vb;
2692 unsigned int base_vertex_idx;
2693 UINT vb_pos, ib_pos, align;
2694 HRESULT hr;
2696 TRACE("iface %p, primitive_type %#x, min_vertex_idx %u, vertex_count %u, primitive_count %u, "
2697 "index_data %p, index_format %#x, vertex_data %p, vertex_stride %u.\n",
2698 iface, primitive_type, min_vertex_idx, vertex_count, primitive_count,
2699 index_data, index_format, vertex_data, vertex_stride);
2701 if (!primitive_count || !vertex_stride)
2703 WARN("primitive_count or vertex_stride is 0, returning D3D_OK.\n");
2704 return D3D_OK;
2707 wined3d_mutex_lock();
2709 hr = d3d8_device_prepare_vertex_buffer(device, vtx_size);
2710 if (FAILED(hr))
2711 goto done;
2713 vb_pos = device->vertex_buffer_pos;
2714 align = vb_pos % vertex_stride;
2715 if (align) align = vertex_stride - align;
2716 if (vb_pos + vtx_size + align > device->vertex_buffer_size)
2717 vb_pos = 0;
2718 else
2719 vb_pos += align;
2721 wined3d_box.left = vb_pos;
2722 wined3d_box.right = vb_pos + vtx_size;
2723 vb = wined3d_buffer_get_resource(device->vertex_buffer);
2724 if (FAILED(hr = wined3d_resource_map(vb, 0, &wined3d_map_desc, &wined3d_box,
2725 WINED3D_MAP_WRITE | (vb_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD))))
2726 goto done;
2727 memcpy(wined3d_map_desc.data, (char *)vertex_data + min_vertex_idx * vertex_stride, vtx_size);
2728 wined3d_resource_unmap(vb, 0);
2729 device->vertex_buffer_pos = vb_pos + vtx_size;
2731 hr = d3d8_device_prepare_index_buffer(device, idx_size);
2732 if (FAILED(hr))
2733 goto done;
2735 ib_pos = device->index_buffer_pos;
2736 align = ib_pos % idx_fmt_size;
2737 if (align) align = idx_fmt_size - align;
2738 if (ib_pos + idx_size + align > device->index_buffer_size)
2739 ib_pos = 0;
2740 else
2741 ib_pos += align;
2743 wined3d_box.left = ib_pos;
2744 wined3d_box.right = ib_pos + idx_size;
2745 ib = wined3d_buffer_get_resource(device->index_buffer);
2746 if (FAILED(hr = wined3d_resource_map(ib, 0, &wined3d_map_desc, &wined3d_box,
2747 WINED3D_MAP_WRITE | (ib_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD))))
2748 goto done;
2749 memcpy(wined3d_map_desc.data, index_data, idx_size);
2750 wined3d_resource_unmap(ib, 0);
2751 device->index_buffer_pos = ib_pos + idx_size;
2753 hr = wined3d_stateblock_set_stream_source(device->state, 0, device->vertex_buffer, 0, vertex_stride);
2754 if (FAILED(hr))
2755 goto done;
2757 wined3d_stateblock_set_index_buffer(device->state, device->index_buffer,
2758 wined3dformat_from_d3dformat(index_format));
2759 base_vertex_idx = vb_pos / vertex_stride - min_vertex_idx;
2760 wined3d_stateblock_set_base_vertex_index(device->state, base_vertex_idx);
2762 wined3d_device_set_primitive_type(device->wined3d_device, wined3d_primitive_type_from_d3d(primitive_type), 0);
2763 wined3d_device_apply_stateblock(device->wined3d_device, device->state);
2764 wined3d_device_context_draw_indexed(device->immediate_context,
2765 base_vertex_idx, ib_pos / idx_fmt_size, idx_count, 0, 0);
2767 wined3d_stateblock_set_stream_source(device->state, 0, NULL, 0, 0);
2768 wined3d_stateblock_set_index_buffer(device->state, NULL, WINED3DFMT_UNKNOWN);
2769 wined3d_stateblock_set_base_vertex_index(device->state, 0);
2771 done:
2772 wined3d_mutex_unlock();
2773 return hr;
2776 static HRESULT WINAPI d3d8_device_ProcessVertices(IDirect3DDevice8 *iface, UINT src_start_idx,
2777 UINT dst_idx, UINT vertex_count, IDirect3DVertexBuffer8 *dst_buffer, DWORD flags)
2779 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2780 struct d3d8_vertexbuffer *dst = unsafe_impl_from_IDirect3DVertexBuffer8(dst_buffer);
2781 const struct wined3d_stateblock_state *state;
2782 const struct wined3d_stream_state *stream;
2783 struct d3d8_vertexbuffer *d3d8_buffer;
2784 unsigned int i, map;
2785 HRESULT hr;
2787 TRACE("iface %p, src_start_idx %u, dst_idx %u, vertex_count %u, dst_buffer %p, flags %#x.\n",
2788 iface, src_start_idx, dst_idx, vertex_count, dst_buffer, flags);
2790 wined3d_mutex_lock();
2792 state = device->stateblock_state;
2794 /* Note that an alternative approach would be to simply create these
2795 * buffers with WINED3D_RESOURCE_ACCESS_MAP_R and update them here like we
2796 * do for draws. In some regards that would be easier, but it seems less
2797 * than optimal to upload data to the GPU only to subsequently download it
2798 * again. */
2799 map = device->sysmem_vb;
2800 while (map)
2802 i = wined3d_bit_scan(&map);
2803 stream = &state->streams[i];
2805 d3d8_buffer = wined3d_buffer_get_parent(stream->buffer);
2806 if (FAILED(wined3d_stateblock_set_stream_source(device->state,
2807 i, d3d8_buffer->wined3d_buffer, stream->offset, stream->stride)))
2808 ERR("Failed to set stream source.\n");
2811 wined3d_device_apply_stateblock(device->wined3d_device, device->state);
2812 hr = wined3d_device_process_vertices(device->wined3d_device, src_start_idx, dst_idx,
2813 vertex_count, dst->wined3d_buffer, NULL, flags, dst->fvf);
2815 map = device->sysmem_vb;
2816 while (map)
2818 i = wined3d_bit_scan(&map);
2819 stream = &state->streams[i];
2821 d3d8_buffer = wined3d_buffer_get_parent(stream->buffer);
2822 if (FAILED(wined3d_stateblock_set_stream_source(device->state,
2823 i, d3d8_buffer->draw_buffer, stream->offset, stream->stride)))
2824 ERR("Failed to set stream source.\n");
2827 wined3d_mutex_unlock();
2829 return hr;
2832 static HRESULT WINAPI d3d8_device_CreateVertexShader(IDirect3DDevice8 *iface,
2833 const DWORD *declaration, const DWORD *byte_code, DWORD *shader, DWORD usage)
2835 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2836 struct d3d8_vertex_shader *object;
2837 DWORD shader_handle;
2838 DWORD handle;
2839 HRESULT hr;
2841 TRACE("iface %p, declaration %p, byte_code %p, shader %p, usage %#x.\n",
2842 iface, declaration, byte_code, shader, usage);
2844 if (!(object = heap_alloc_zero(sizeof(*object))))
2846 *shader = 0;
2847 return E_OUTOFMEMORY;
2850 wined3d_mutex_lock();
2851 handle = d3d8_allocate_handle(&device->handle_table, object, D3D8_HANDLE_VS);
2852 wined3d_mutex_unlock();
2853 if (handle == D3D8_INVALID_HANDLE)
2855 ERR("Failed to allocate vertex shader handle.\n");
2856 heap_free(object);
2857 *shader = 0;
2858 return E_OUTOFMEMORY;
2861 shader_handle = handle + VS_HIGHESTFIXEDFXF + 1;
2863 hr = d3d8_vertex_shader_init(object, device, declaration, byte_code, shader_handle, usage);
2864 if (FAILED(hr))
2866 WARN("Failed to initialize vertex shader, hr %#x.\n", hr);
2867 wined3d_mutex_lock();
2868 d3d8_free_handle(&device->handle_table, handle, D3D8_HANDLE_VS);
2869 wined3d_mutex_unlock();
2870 heap_free(object);
2871 *shader = 0;
2872 return hr;
2875 TRACE("Created vertex shader %p (handle %#x).\n", object, shader_handle);
2876 *shader = shader_handle;
2878 return D3D_OK;
2881 static struct d3d8_vertex_declaration *d3d8_device_get_fvf_declaration(struct d3d8_device *device, DWORD fvf)
2883 struct d3d8_vertex_declaration *d3d8_declaration;
2884 struct FvfToDecl *convertedDecls = device->decls;
2885 int p, low, high; /* deliberately signed */
2886 HRESULT hr;
2888 TRACE("Searching for declaration for fvf %08x... ", fvf);
2890 low = 0;
2891 high = device->numConvertedDecls - 1;
2892 while (low <= high)
2894 p = (low + high) >> 1;
2895 TRACE("%d ", p);
2897 if (convertedDecls[p].fvf == fvf)
2899 TRACE("found %p\n", convertedDecls[p].declaration);
2900 return convertedDecls[p].declaration;
2903 if (convertedDecls[p].fvf < fvf)
2904 low = p + 1;
2905 else
2906 high = p - 1;
2908 TRACE("not found. Creating and inserting at position %d.\n", low);
2910 if (!(d3d8_declaration = heap_alloc(sizeof(*d3d8_declaration))))
2911 return NULL;
2913 if (FAILED(hr = d3d8_vertex_declaration_init_fvf(d3d8_declaration, device, fvf)))
2915 WARN("Failed to initialize vertex declaration, hr %#x.\n", hr);
2916 heap_free(d3d8_declaration);
2917 return NULL;
2920 if (device->declArraySize == device->numConvertedDecls)
2922 UINT grow = device->declArraySize / 2;
2924 if (!(convertedDecls = heap_realloc(convertedDecls,
2925 sizeof(*convertedDecls) * (device->numConvertedDecls + grow))))
2927 d3d8_vertex_declaration_destroy(d3d8_declaration);
2928 return NULL;
2930 device->decls = convertedDecls;
2931 device->declArraySize += grow;
2934 memmove(convertedDecls + low + 1, convertedDecls + low,
2935 sizeof(*convertedDecls) * (device->numConvertedDecls - low));
2936 convertedDecls[low].declaration = d3d8_declaration;
2937 convertedDecls[low].fvf = fvf;
2938 ++device->numConvertedDecls;
2940 TRACE("Returning %p. %u decls in array.\n", d3d8_declaration, device->numConvertedDecls);
2942 return d3d8_declaration;
2945 static HRESULT WINAPI d3d8_device_SetVertexShader(IDirect3DDevice8 *iface, DWORD shader)
2947 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2948 struct d3d8_vertex_shader *shader_impl;
2950 TRACE("iface %p, shader %#x.\n", iface, shader);
2952 if (VS_HIGHESTFIXEDFXF >= shader)
2954 TRACE("Setting FVF, %#x\n", shader);
2956 wined3d_mutex_lock();
2957 wined3d_stateblock_set_vertex_declaration(device->update_state,
2958 d3d8_device_get_fvf_declaration(device, shader)->wined3d_vertex_declaration);
2959 wined3d_stateblock_set_vertex_shader(device->update_state, NULL);
2960 wined3d_mutex_unlock();
2962 return D3D_OK;
2965 TRACE("Setting shader\n");
2967 wined3d_mutex_lock();
2968 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS)))
2970 WARN("Invalid handle (%#x) passed.\n", shader);
2971 wined3d_mutex_unlock();
2973 return D3DERR_INVALIDCALL;
2976 wined3d_stateblock_set_vertex_declaration(device->update_state,
2977 shader_impl->vertex_declaration->wined3d_vertex_declaration);
2978 wined3d_stateblock_set_vertex_shader(device->update_state, shader_impl->wined3d_shader);
2979 wined3d_mutex_unlock();
2981 return D3D_OK;
2984 static HRESULT WINAPI d3d8_device_GetVertexShader(IDirect3DDevice8 *iface, DWORD *shader)
2986 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2987 struct wined3d_vertex_declaration *wined3d_declaration;
2988 struct d3d8_vertex_declaration *d3d8_declaration;
2990 TRACE("iface %p, shader %p.\n", iface, shader);
2992 wined3d_mutex_lock();
2993 if ((wined3d_declaration = device->stateblock_state->vertex_declaration))
2995 d3d8_declaration = wined3d_vertex_declaration_get_parent(wined3d_declaration);
2996 *shader = d3d8_declaration->shader_handle;
2998 else
3000 *shader = 0;
3002 wined3d_mutex_unlock();
3004 TRACE("Returning %#x.\n", *shader);
3006 return D3D_OK;
3009 static HRESULT WINAPI d3d8_device_DeleteVertexShader(IDirect3DDevice8 *iface, DWORD shader)
3011 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3012 struct d3d8_vertex_shader *shader_impl;
3014 TRACE("iface %p, shader %#x.\n", iface, shader);
3016 wined3d_mutex_lock();
3017 if (!(shader_impl = d3d8_free_handle(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS)))
3019 WARN("Invalid handle (%#x) passed.\n", shader);
3020 wined3d_mutex_unlock();
3022 return D3DERR_INVALIDCALL;
3025 if (shader_impl->wined3d_shader
3026 && device->stateblock_state->vs == shader_impl->wined3d_shader)
3027 IDirect3DDevice8_SetVertexShader(iface, 0);
3029 wined3d_mutex_unlock();
3031 d3d8_vertex_shader_destroy(shader_impl);
3033 return D3D_OK;
3036 static HRESULT WINAPI d3d8_device_SetVertexShaderConstant(IDirect3DDevice8 *iface,
3037 DWORD start_register, const void *data, DWORD count)
3039 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3040 HRESULT hr;
3042 TRACE("iface %p, start_register %u, data %p, count %u.\n",
3043 iface, start_register, data, count);
3045 if (start_register + count > D3D8_MAX_VERTEX_SHADER_CONSTANTF)
3047 WARN("Trying to access %u constants, but d3d8 only supports %u\n",
3048 start_register + count, D3D8_MAX_VERTEX_SHADER_CONSTANTF);
3049 return D3DERR_INVALIDCALL;
3052 wined3d_mutex_lock();
3053 hr = wined3d_stateblock_set_vs_consts_f(device->update_state, start_register, count, data);
3054 wined3d_mutex_unlock();
3056 return hr;
3059 static HRESULT WINAPI d3d8_device_GetVertexShaderConstant(IDirect3DDevice8 *iface,
3060 DWORD start_idx, void *constants, DWORD count)
3062 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3063 const struct wined3d_vec4 *src;
3065 TRACE("iface %p, start_idx %u, constants %p, count %u.\n", iface, start_idx, constants, count);
3067 if (!constants)
3068 return D3DERR_INVALIDCALL;
3070 if (start_idx >= device->vs_uniform_count || count > device->vs_uniform_count - start_idx)
3072 WARN("Trying to access %u constants, but d3d8 only supports %u.\n",
3073 start_idx + count, device->vs_uniform_count);
3074 return D3DERR_INVALIDCALL;
3077 wined3d_mutex_lock();
3078 src = device->stateblock_state->vs_consts_f;
3079 memcpy(constants, &src[start_idx], count * sizeof(*src));
3080 wined3d_mutex_unlock();
3082 return D3D_OK;
3085 static HRESULT WINAPI d3d8_device_GetVertexShaderDeclaration(IDirect3DDevice8 *iface,
3086 DWORD shader, void *data, DWORD *data_size)
3088 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3089 struct d3d8_vertex_declaration *declaration;
3090 struct d3d8_vertex_shader *shader_impl;
3092 TRACE("iface %p, shader %#x, data %p, data_size %p.\n",
3093 iface, shader, data, data_size);
3095 wined3d_mutex_lock();
3096 shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS);
3097 wined3d_mutex_unlock();
3099 if (!shader_impl)
3101 WARN("Invalid handle (%#x) passed.\n", shader);
3102 return D3DERR_INVALIDCALL;
3104 declaration = shader_impl->vertex_declaration;
3106 if (!data)
3108 *data_size = declaration->elements_size;
3109 return D3D_OK;
3112 /* MSDN claims that if *data_size is smaller than the required size
3113 * we should write the required size and return D3DERR_MOREDATA.
3114 * That's not actually true. */
3115 if (*data_size < declaration->elements_size)
3116 return D3DERR_INVALIDCALL;
3118 memcpy(data, declaration->elements, declaration->elements_size);
3120 return D3D_OK;
3123 static HRESULT WINAPI d3d8_device_GetVertexShaderFunction(IDirect3DDevice8 *iface,
3124 DWORD shader, void *data, DWORD *data_size)
3126 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3127 struct d3d8_vertex_shader *shader_impl = NULL;
3128 HRESULT hr;
3130 TRACE("iface %p, shader %#x, data %p, data_size %p.\n",
3131 iface, shader, data, data_size);
3133 wined3d_mutex_lock();
3134 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS)))
3136 WARN("Invalid handle (%#x) passed.\n", shader);
3137 wined3d_mutex_unlock();
3139 return D3DERR_INVALIDCALL;
3142 if (!shader_impl->wined3d_shader)
3144 wined3d_mutex_unlock();
3145 *data_size = 0;
3146 return D3D_OK;
3149 hr = wined3d_shader_get_byte_code(shader_impl->wined3d_shader, data, data_size);
3150 wined3d_mutex_unlock();
3152 return hr;
3155 static HRESULT WINAPI d3d8_device_SetIndices(IDirect3DDevice8 *iface,
3156 IDirect3DIndexBuffer8 *buffer, UINT base_vertex_idx)
3158 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3159 struct d3d8_indexbuffer *ib = unsafe_impl_from_IDirect3DIndexBuffer8(buffer);
3160 struct wined3d_buffer *wined3d_buffer;
3162 TRACE("iface %p, buffer %p, base_vertex_idx %u.\n", iface, buffer, base_vertex_idx);
3164 if (!ib)
3165 wined3d_buffer = NULL;
3166 else if (ib->draw_buffer)
3167 wined3d_buffer = ib->draw_buffer;
3168 else
3169 wined3d_buffer = ib->wined3d_buffer;
3171 /* WineD3D takes an INT(due to d3d9), but d3d8 uses UINTs. Do I have to add a check here that
3172 * the UINT doesn't cause an overflow in the INT? It seems rather unlikely because such large
3173 * vertex buffers can't be created to address them with an index that requires the 32nd bit
3174 * (4 Byte minimum vertex size * 2^31-1 -> 8 gb buffer. The index sign would be the least
3175 * problem)
3177 wined3d_mutex_lock();
3178 wined3d_stateblock_set_base_vertex_index(device->update_state, base_vertex_idx);
3179 wined3d_stateblock_set_index_buffer(device->update_state, wined3d_buffer, ib ? ib->format : WINED3DFMT_UNKNOWN);
3180 if (!device->recording)
3181 device->sysmem_ib = ib && ib->draw_buffer;
3182 wined3d_mutex_unlock();
3184 return D3D_OK;
3187 static HRESULT WINAPI d3d8_device_GetIndices(IDirect3DDevice8 *iface,
3188 IDirect3DIndexBuffer8 **buffer, UINT *base_vertex_index)
3190 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3191 const struct wined3d_stateblock_state *state;
3192 struct wined3d_buffer *wined3d_buffer;
3193 struct d3d8_indexbuffer *buffer_impl;
3195 TRACE("iface %p, buffer %p, base_vertex_index %p.\n", iface, buffer, base_vertex_index);
3197 if (!buffer)
3198 return D3DERR_INVALIDCALL;
3200 /* The case from UINT to INT is safe because d3d8 will never set negative values */
3201 wined3d_mutex_lock();
3202 state = device->stateblock_state;
3203 *base_vertex_index = state->base_vertex_index;
3204 if ((wined3d_buffer = state->index_buffer))
3206 buffer_impl = wined3d_buffer_get_parent(wined3d_buffer);
3207 *buffer = &buffer_impl->IDirect3DIndexBuffer8_iface;
3208 IDirect3DIndexBuffer8_AddRef(*buffer);
3210 else
3212 *buffer = NULL;
3214 wined3d_mutex_unlock();
3216 return D3D_OK;
3219 static HRESULT WINAPI d3d8_device_CreatePixelShader(IDirect3DDevice8 *iface,
3220 const DWORD *byte_code, DWORD *shader)
3222 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3223 struct d3d8_pixel_shader *object;
3224 DWORD shader_handle;
3225 DWORD handle;
3226 HRESULT hr;
3228 TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
3230 if (!shader)
3231 return D3DERR_INVALIDCALL;
3233 if (!(object = heap_alloc_zero(sizeof(*object))))
3234 return E_OUTOFMEMORY;
3236 wined3d_mutex_lock();
3237 handle = d3d8_allocate_handle(&device->handle_table, object, D3D8_HANDLE_PS);
3238 wined3d_mutex_unlock();
3239 if (handle == D3D8_INVALID_HANDLE)
3241 ERR("Failed to allocate pixel shader handle.\n");
3242 heap_free(object);
3243 return E_OUTOFMEMORY;
3246 shader_handle = handle + VS_HIGHESTFIXEDFXF + 1;
3248 hr = d3d8_pixel_shader_init(object, device, byte_code, shader_handle);
3249 if (FAILED(hr))
3251 WARN("Failed to initialize pixel shader, hr %#x.\n", hr);
3252 wined3d_mutex_lock();
3253 d3d8_free_handle(&device->handle_table, handle, D3D8_HANDLE_PS);
3254 wined3d_mutex_unlock();
3255 heap_free(object);
3256 *shader = 0;
3257 return hr;
3260 TRACE("Created pixel shader %p (handle %#x).\n", object, shader_handle);
3261 *shader = shader_handle;
3263 return D3D_OK;
3266 static HRESULT WINAPI d3d8_device_SetPixelShader(IDirect3DDevice8 *iface, DWORD shader)
3268 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3269 struct d3d8_pixel_shader *shader_impl;
3271 TRACE("iface %p, shader %#x.\n", iface, shader);
3273 wined3d_mutex_lock();
3275 if (!shader)
3277 wined3d_stateblock_set_pixel_shader(device->update_state, NULL);
3278 wined3d_mutex_unlock();
3279 return D3D_OK;
3282 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS)))
3284 WARN("Invalid handle (%#x) passed.\n", shader);
3285 wined3d_mutex_unlock();
3286 return D3DERR_INVALIDCALL;
3289 TRACE("Setting shader %p.\n", shader_impl);
3290 wined3d_stateblock_set_pixel_shader(device->update_state, shader_impl->wined3d_shader);
3291 wined3d_mutex_unlock();
3293 return D3D_OK;
3296 static HRESULT WINAPI d3d8_device_GetPixelShader(IDirect3DDevice8 *iface, DWORD *shader)
3298 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3299 struct wined3d_shader *object;
3301 TRACE("iface %p, shader %p.\n", iface, shader);
3303 if (!shader)
3304 return D3DERR_INVALIDCALL;
3306 wined3d_mutex_lock();
3307 if ((object = device->stateblock_state->ps))
3309 struct d3d8_pixel_shader *d3d8_shader;
3310 d3d8_shader = wined3d_shader_get_parent(object);
3311 *shader = d3d8_shader->handle;
3313 else
3315 *shader = 0;
3317 wined3d_mutex_unlock();
3319 TRACE("Returning %#x.\n", *shader);
3321 return D3D_OK;
3324 static HRESULT WINAPI d3d8_device_DeletePixelShader(IDirect3DDevice8 *iface, DWORD shader)
3326 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3327 struct d3d8_pixel_shader *shader_impl;
3329 TRACE("iface %p, shader %#x.\n", iface, shader);
3331 wined3d_mutex_lock();
3333 if (!(shader_impl = d3d8_free_handle(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS)))
3335 WARN("Invalid handle (%#x) passed.\n", shader);
3336 wined3d_mutex_unlock();
3337 return D3DERR_INVALIDCALL;
3340 if (device->stateblock_state->ps == shader_impl->wined3d_shader)
3341 IDirect3DDevice8_SetPixelShader(iface, 0);
3343 wined3d_mutex_unlock();
3345 d3d8_pixel_shader_destroy(shader_impl);
3347 return D3D_OK;
3350 static HRESULT WINAPI d3d8_device_SetPixelShaderConstant(IDirect3DDevice8 *iface,
3351 DWORD start_register, const void *data, DWORD count)
3353 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3354 HRESULT hr;
3356 TRACE("iface %p, start_register %u, data %p, count %u.\n",
3357 iface, start_register, data, count);
3359 wined3d_mutex_lock();
3360 hr = wined3d_stateblock_set_ps_consts_f(device->update_state, start_register, count, data);
3361 wined3d_mutex_unlock();
3363 return hr;
3366 static HRESULT WINAPI d3d8_device_GetPixelShaderConstant(IDirect3DDevice8 *iface,
3367 DWORD start_idx, void *constants, DWORD count)
3369 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3370 const struct wined3d_vec4 *src;
3372 TRACE("iface %p, start_idx %u, constants %p, count %u.\n", iface, start_idx, constants, count);
3374 if (!constants || start_idx >= D3D8_MAX_PIXEL_SHADER_CONSTANTF
3375 || count > D3D8_MAX_PIXEL_SHADER_CONSTANTF - start_idx)
3376 return WINED3DERR_INVALIDCALL;
3378 wined3d_mutex_lock();
3379 src = device->stateblock_state->ps_consts_f;
3380 memcpy(constants, &src[start_idx], count * sizeof(*src));
3381 wined3d_mutex_unlock();
3383 return D3D_OK;
3386 static HRESULT WINAPI d3d8_device_GetPixelShaderFunction(IDirect3DDevice8 *iface,
3387 DWORD shader, void *data, DWORD *data_size)
3389 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3390 struct d3d8_pixel_shader *shader_impl = NULL;
3391 HRESULT hr;
3393 TRACE("iface %p, shader %#x, data %p, data_size %p.\n",
3394 iface, shader, data, data_size);
3396 wined3d_mutex_lock();
3397 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS)))
3399 WARN("Invalid handle (%#x) passed.\n", shader);
3400 wined3d_mutex_unlock();
3402 return D3DERR_INVALIDCALL;
3405 hr = wined3d_shader_get_byte_code(shader_impl->wined3d_shader, data, data_size);
3406 wined3d_mutex_unlock();
3408 return hr;
3411 static HRESULT WINAPI d3d8_device_DrawRectPatch(IDirect3DDevice8 *iface, UINT handle,
3412 const float *segment_count, const D3DRECTPATCH_INFO *patch_info)
3414 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
3415 iface, handle, segment_count, patch_info);
3416 return D3D_OK;
3419 static HRESULT WINAPI d3d8_device_DrawTriPatch(IDirect3DDevice8 *iface, UINT handle,
3420 const float *segment_count, const D3DTRIPATCH_INFO *patch_info)
3422 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
3423 iface, handle, segment_count, patch_info);
3424 return D3D_OK;
3427 static HRESULT WINAPI d3d8_device_DeletePatch(IDirect3DDevice8 *iface, UINT handle)
3429 FIXME("iface %p, handle %#x unimplemented.\n", iface, handle);
3430 return D3DERR_INVALIDCALL;
3433 static HRESULT WINAPI d3d8_device_SetStreamSource(IDirect3DDevice8 *iface,
3434 UINT stream_idx, IDirect3DVertexBuffer8 *buffer, UINT stride)
3436 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3437 struct d3d8_vertexbuffer *buffer_impl = unsafe_impl_from_IDirect3DVertexBuffer8(buffer);
3438 const struct wined3d_stateblock_state *state;
3439 struct wined3d_buffer *wined3d_buffer;
3440 HRESULT hr;
3442 TRACE("iface %p, stream_idx %u, buffer %p, stride %u.\n",
3443 iface, stream_idx, buffer, stride);
3445 if (stream_idx >= ARRAY_SIZE(state->streams))
3447 WARN("Stream index %u out of range.\n", stream_idx);
3448 return D3DERR_INVALIDCALL;
3451 wined3d_mutex_lock();
3453 if (!buffer_impl)
3455 wined3d_buffer = NULL;
3456 state = device->stateblock_state;
3457 stride = state->streams[stream_idx].stride;
3459 else if (buffer_impl->draw_buffer)
3460 wined3d_buffer = buffer_impl->draw_buffer;
3461 else
3462 wined3d_buffer = buffer_impl->wined3d_buffer;
3464 hr = wined3d_stateblock_set_stream_source(device->update_state, stream_idx, wined3d_buffer, 0, stride);
3465 if (SUCCEEDED(hr) && !device->recording)
3467 if (buffer_impl && buffer_impl->draw_buffer)
3468 device->sysmem_vb |= (1u << stream_idx);
3469 else
3470 device->sysmem_vb &= ~(1u << stream_idx);
3473 wined3d_mutex_unlock();
3475 return hr;
3478 static HRESULT WINAPI d3d8_device_GetStreamSource(IDirect3DDevice8 *iface,
3479 UINT stream_idx, IDirect3DVertexBuffer8 **buffer, UINT *stride)
3481 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3482 const struct wined3d_stateblock_state *state;
3483 const struct wined3d_stream_state *stream;
3484 struct d3d8_vertexbuffer *buffer_impl;
3486 TRACE("iface %p, stream_idx %u, buffer %p, stride %p.\n",
3487 iface, stream_idx, buffer, stride);
3489 if (!buffer)
3490 return D3DERR_INVALIDCALL;
3492 if (stream_idx >= ARRAY_SIZE(state->streams))
3494 WARN("Stream index %u out of range.\n", stream_idx);
3495 return D3DERR_INVALIDCALL;
3498 wined3d_mutex_lock();
3499 state = device->stateblock_state;
3500 stream = &state->streams[stream_idx];
3501 if (stream->buffer)
3503 buffer_impl = wined3d_buffer_get_parent(stream->buffer);
3504 *buffer = &buffer_impl->IDirect3DVertexBuffer8_iface;
3505 IDirect3DVertexBuffer8_AddRef(*buffer);
3507 else
3508 *buffer = NULL;
3509 *stride = stream->stride;
3510 wined3d_mutex_unlock();
3512 return D3D_OK;
3515 static const struct IDirect3DDevice8Vtbl d3d8_device_vtbl =
3517 d3d8_device_QueryInterface,
3518 d3d8_device_AddRef,
3519 d3d8_device_Release,
3520 d3d8_device_TestCooperativeLevel,
3521 d3d8_device_GetAvailableTextureMem,
3522 d3d8_device_ResourceManagerDiscardBytes,
3523 d3d8_device_GetDirect3D,
3524 d3d8_device_GetDeviceCaps,
3525 d3d8_device_GetDisplayMode,
3526 d3d8_device_GetCreationParameters,
3527 d3d8_device_SetCursorProperties,
3528 d3d8_device_SetCursorPosition,
3529 d3d8_device_ShowCursor,
3530 d3d8_device_CreateAdditionalSwapChain,
3531 d3d8_device_Reset,
3532 d3d8_device_Present,
3533 d3d8_device_GetBackBuffer,
3534 d3d8_device_GetRasterStatus,
3535 d3d8_device_SetGammaRamp,
3536 d3d8_device_GetGammaRamp,
3537 d3d8_device_CreateTexture,
3538 d3d8_device_CreateVolumeTexture,
3539 d3d8_device_CreateCubeTexture,
3540 d3d8_device_CreateVertexBuffer,
3541 d3d8_device_CreateIndexBuffer,
3542 d3d8_device_CreateRenderTarget,
3543 d3d8_device_CreateDepthStencilSurface,
3544 d3d8_device_CreateImageSurface,
3545 d3d8_device_CopyRects,
3546 d3d8_device_UpdateTexture,
3547 d3d8_device_GetFrontBuffer,
3548 d3d8_device_SetRenderTarget,
3549 d3d8_device_GetRenderTarget,
3550 d3d8_device_GetDepthStencilSurface,
3551 d3d8_device_BeginScene,
3552 d3d8_device_EndScene,
3553 d3d8_device_Clear,
3554 d3d8_device_SetTransform,
3555 d3d8_device_GetTransform,
3556 d3d8_device_MultiplyTransform,
3557 d3d8_device_SetViewport,
3558 d3d8_device_GetViewport,
3559 d3d8_device_SetMaterial,
3560 d3d8_device_GetMaterial,
3561 d3d8_device_SetLight,
3562 d3d8_device_GetLight,
3563 d3d8_device_LightEnable,
3564 d3d8_device_GetLightEnable,
3565 d3d8_device_SetClipPlane,
3566 d3d8_device_GetClipPlane,
3567 d3d8_device_SetRenderState,
3568 d3d8_device_GetRenderState,
3569 d3d8_device_BeginStateBlock,
3570 d3d8_device_EndStateBlock,
3571 d3d8_device_ApplyStateBlock,
3572 d3d8_device_CaptureStateBlock,
3573 d3d8_device_DeleteStateBlock,
3574 d3d8_device_CreateStateBlock,
3575 d3d8_device_SetClipStatus,
3576 d3d8_device_GetClipStatus,
3577 d3d8_device_GetTexture,
3578 d3d8_device_SetTexture,
3579 d3d8_device_GetTextureStageState,
3580 d3d8_device_SetTextureStageState,
3581 d3d8_device_ValidateDevice,
3582 d3d8_device_GetInfo,
3583 d3d8_device_SetPaletteEntries,
3584 d3d8_device_GetPaletteEntries,
3585 d3d8_device_SetCurrentTexturePalette,
3586 d3d8_device_GetCurrentTexturePalette,
3587 d3d8_device_DrawPrimitive,
3588 d3d8_device_DrawIndexedPrimitive,
3589 d3d8_device_DrawPrimitiveUP,
3590 d3d8_device_DrawIndexedPrimitiveUP,
3591 d3d8_device_ProcessVertices,
3592 d3d8_device_CreateVertexShader,
3593 d3d8_device_SetVertexShader,
3594 d3d8_device_GetVertexShader,
3595 d3d8_device_DeleteVertexShader,
3596 d3d8_device_SetVertexShaderConstant,
3597 d3d8_device_GetVertexShaderConstant,
3598 d3d8_device_GetVertexShaderDeclaration,
3599 d3d8_device_GetVertexShaderFunction,
3600 d3d8_device_SetStreamSource,
3601 d3d8_device_GetStreamSource,
3602 d3d8_device_SetIndices,
3603 d3d8_device_GetIndices,
3604 d3d8_device_CreatePixelShader,
3605 d3d8_device_SetPixelShader,
3606 d3d8_device_GetPixelShader,
3607 d3d8_device_DeletePixelShader,
3608 d3d8_device_SetPixelShaderConstant,
3609 d3d8_device_GetPixelShaderConstant,
3610 d3d8_device_GetPixelShaderFunction,
3611 d3d8_device_DrawRectPatch,
3612 d3d8_device_DrawTriPatch,
3613 d3d8_device_DeletePatch,
3616 static inline struct d3d8_device *device_from_device_parent(struct wined3d_device_parent *device_parent)
3618 return CONTAINING_RECORD(device_parent, struct d3d8_device, device_parent);
3621 static void CDECL device_parent_wined3d_device_created(struct wined3d_device_parent *device_parent,
3622 struct wined3d_device *device)
3624 TRACE("device_parent %p, device %p\n", device_parent, device);
3627 static void CDECL device_parent_mode_changed(struct wined3d_device_parent *device_parent)
3629 TRACE("device_parent %p.\n", device_parent);
3632 static void CDECL device_parent_activate(struct wined3d_device_parent *device_parent, BOOL activate)
3634 struct d3d8_device *device = device_from_device_parent(device_parent);
3636 TRACE("device_parent %p, activate %#x.\n", device_parent, activate);
3638 if (!activate)
3639 InterlockedCompareExchange(&device->device_state, D3D8_DEVICE_STATE_LOST, D3D8_DEVICE_STATE_OK);
3640 else
3641 InterlockedCompareExchange(&device->device_state, D3D8_DEVICE_STATE_NOT_RESET, D3D8_DEVICE_STATE_LOST);
3644 static HRESULT CDECL device_parent_texture_sub_resource_created(struct wined3d_device_parent *device_parent,
3645 enum wined3d_resource_type type, struct wined3d_texture *wined3d_texture, unsigned int sub_resource_idx,
3646 void **parent, const struct wined3d_parent_ops **parent_ops)
3648 TRACE("device_parent %p, type %#x, wined3d_texture %p, sub_resource_idx %u, parent %p, parent_ops %p.\n",
3649 device_parent, type, wined3d_texture, sub_resource_idx, parent, parent_ops);
3651 if (type == WINED3D_RTYPE_TEXTURE_2D)
3653 struct d3d8_surface *d3d_surface;
3655 if (!(d3d_surface = heap_alloc_zero(sizeof(*d3d_surface))))
3656 return E_OUTOFMEMORY;
3658 surface_init(d3d_surface, wined3d_texture, sub_resource_idx, parent_ops);
3659 *parent = d3d_surface;
3660 TRACE("Created surface %p.\n", d3d_surface);
3662 else if (type == WINED3D_RTYPE_TEXTURE_3D)
3664 struct d3d8_volume *d3d_volume;
3666 if (!(d3d_volume = heap_alloc_zero(sizeof(*d3d_volume))))
3667 return E_OUTOFMEMORY;
3669 volume_init(d3d_volume, wined3d_texture, sub_resource_idx, parent_ops);
3670 *parent = d3d_volume;
3671 TRACE("Created volume %p.\n", d3d_volume);
3673 else
3675 ERR("Unhandled resource type %#x.\n", type);
3676 return E_FAIL;
3679 return D3D_OK;
3682 static HRESULT CDECL device_parent_create_swapchain_texture(struct wined3d_device_parent *device_parent,
3683 void *container_parent, const struct wined3d_resource_desc *desc, DWORD texture_flags,
3684 struct wined3d_texture **texture)
3686 struct d3d8_device *device = device_from_device_parent(device_parent);
3687 struct d3d8_surface *d3d_surface;
3688 HRESULT hr;
3690 TRACE("device_parent %p, container_parent %p, desc %p, texture flags %#x, texture %p.\n",
3691 device_parent, container_parent, desc, texture_flags, texture);
3693 if (FAILED(hr = wined3d_texture_create(device->wined3d_device, desc, 1, 1, texture_flags,
3694 NULL, &device->IDirect3DDevice8_iface, &d3d8_null_wined3d_parent_ops, texture)))
3696 WARN("Failed to create texture, hr %#x.\n", hr);
3697 return hr;
3700 d3d_surface = wined3d_texture_get_sub_resource_parent(*texture, 0);
3701 d3d_surface->parent_device = &device->IDirect3DDevice8_iface;
3703 return hr;
3706 static const struct wined3d_device_parent_ops d3d8_wined3d_device_parent_ops =
3708 device_parent_wined3d_device_created,
3709 device_parent_mode_changed,
3710 device_parent_activate,
3711 device_parent_texture_sub_resource_created,
3712 device_parent_create_swapchain_texture,
3715 static void setup_fpu(void)
3717 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
3718 WORD cw;
3719 __asm__ volatile ("fnstcw %0" : "=m" (cw));
3720 cw = (cw & ~0xf3f) | 0x3f;
3721 __asm__ volatile ("fldcw %0" : : "m" (cw));
3722 #elif defined(__i386__) && defined(_MSC_VER)
3723 WORD cw;
3724 __asm fnstcw cw;
3725 cw = (cw & ~0xf3f) | 0x3f;
3726 __asm fldcw cw;
3727 #else
3728 FIXME("FPU setup not implemented for this platform.\n");
3729 #endif
3732 HRESULT device_init(struct d3d8_device *device, struct d3d8 *parent, struct wined3d *wined3d, UINT adapter,
3733 D3DDEVTYPE device_type, HWND focus_window, DWORD flags, D3DPRESENT_PARAMETERS *parameters)
3735 struct wined3d_swapchain_desc swapchain_desc;
3736 struct wined3d_swapchain *wined3d_swapchain;
3737 struct wined3d_adapter *wined3d_adapter;
3738 struct wined3d_output *wined3d_output;
3739 struct d3d8_swapchain *d3d_swapchain;
3740 struct wined3d_caps caps;
3741 unsigned int output_idx;
3742 HRESULT hr;
3744 static const enum wined3d_feature_level feature_levels[] =
3746 WINED3D_FEATURE_LEVEL_8,
3747 WINED3D_FEATURE_LEVEL_7,
3748 WINED3D_FEATURE_LEVEL_6,
3749 WINED3D_FEATURE_LEVEL_5,
3752 output_idx = adapter;
3753 if (output_idx >= parent->wined3d_output_count)
3754 return D3DERR_INVALIDCALL;
3756 device->IDirect3DDevice8_iface.lpVtbl = &d3d8_device_vtbl;
3757 device->device_parent.ops = &d3d8_wined3d_device_parent_ops;
3758 device->adapter_ordinal = adapter;
3759 device->ref = 1;
3760 if (!(device->handle_table.entries = heap_alloc_zero(D3D8_INITIAL_HANDLE_TABLE_SIZE
3761 * sizeof(*device->handle_table.entries))))
3763 ERR("Failed to allocate handle table memory.\n");
3764 return E_OUTOFMEMORY;
3766 device->handle_table.table_size = D3D8_INITIAL_HANDLE_TABLE_SIZE;
3768 if (!(flags & D3DCREATE_FPU_PRESERVE)) setup_fpu();
3770 wined3d_mutex_lock();
3771 wined3d_output = parent->wined3d_outputs[output_idx];
3772 wined3d_adapter = wined3d_output_get_adapter(wined3d_output);
3773 if (FAILED(hr = wined3d_device_create(wined3d, wined3d_adapter, wined3d_device_type_from_d3d(device_type),
3774 focus_window, flags, 4, feature_levels, ARRAY_SIZE(feature_levels),
3775 &device->device_parent, &device->wined3d_device)))
3777 WARN("Failed to create wined3d device, hr %#x.\n", hr);
3778 wined3d_mutex_unlock();
3779 heap_free(device->handle_table.entries);
3780 return hr;
3783 device->immediate_context = wined3d_device_get_immediate_context(device->wined3d_device);
3784 wined3d_get_device_caps(wined3d_adapter, wined3d_device_type_from_d3d(device_type), &caps);
3785 device->max_user_clip_planes = caps.MaxUserClipPlanes;
3786 device->vs_uniform_count = caps.MaxVertexShaderConst;
3788 if (FAILED(hr = wined3d_stateblock_create(device->wined3d_device, NULL, WINED3D_SBT_PRIMARY, &device->state)))
3790 ERR("Failed to create primary stateblock, hr %#x.\n", hr);
3791 wined3d_device_decref(device->wined3d_device);
3792 wined3d_mutex_unlock();
3793 return hr;
3795 device->stateblock_state = wined3d_stateblock_get_state(device->state);
3796 device->update_state = device->state;
3798 if (!parameters->Windowed)
3800 if (!focus_window)
3801 focus_window = parameters->hDeviceWindow;
3802 if (FAILED(hr = wined3d_device_acquire_focus_window(device->wined3d_device, focus_window)))
3804 ERR("Failed to acquire focus window, hr %#x.\n", hr);
3805 wined3d_device_decref(device->wined3d_device);
3806 wined3d_mutex_unlock();
3807 heap_free(device->handle_table.entries);
3808 return hr;
3812 if (flags & D3DCREATE_MULTITHREADED)
3813 wined3d_device_set_multithreaded(device->wined3d_device);
3815 if (!wined3d_swapchain_desc_from_d3d8(&swapchain_desc, wined3d_output, parameters))
3817 wined3d_device_release_focus_window(device->wined3d_device);
3818 wined3d_device_decref(device->wined3d_device);
3819 wined3d_mutex_unlock();
3820 heap_free(device->handle_table.entries);
3821 return D3DERR_INVALIDCALL;
3823 swapchain_desc.flags |= WINED3D_SWAPCHAIN_IMPLICIT;
3825 if (FAILED(hr = d3d8_swapchain_create(device, &swapchain_desc,
3826 wined3dswapinterval_from_d3d(parameters->FullScreen_PresentationInterval), &d3d_swapchain)))
3828 WARN("Failed to create implicit swapchain, hr %#x.\n", hr);
3829 wined3d_device_release_focus_window(device->wined3d_device);
3830 wined3d_device_decref(device->wined3d_device);
3831 wined3d_mutex_unlock();
3832 heap_free(device->handle_table.entries);
3833 return hr;
3836 wined3d_swapchain = d3d_swapchain->wined3d_swapchain;
3837 wined3d_swapchain_incref(wined3d_swapchain);
3838 IDirect3DSwapChain8_Release(&d3d_swapchain->IDirect3DSwapChain8_iface);
3840 wined3d_stateblock_set_render_state(device->state, WINED3D_RS_ZENABLE,
3841 !!swapchain_desc.enable_auto_depth_stencil);
3842 wined3d_stateblock_set_render_state(device->state, WINED3D_RS_POINTSIZE_MIN, 0);
3843 device_reset_viewport_state(device);
3844 wined3d_mutex_unlock();
3846 present_parameters_from_wined3d_swapchain_desc(parameters,
3847 &swapchain_desc, parameters->FullScreen_PresentationInterval);
3849 device->declArraySize = 16;
3850 if (!(device->decls = heap_alloc(device->declArraySize * sizeof(*device->decls))))
3852 ERR("Failed to allocate FVF vertex declaration map memory.\n");
3853 hr = E_OUTOFMEMORY;
3854 goto err;
3857 device->implicit_swapchain = wined3d_swapchain;
3859 device->d3d_parent = parent;
3860 IDirect3D8_AddRef(&parent->IDirect3D8_iface);
3862 return D3D_OK;
3864 err:
3865 wined3d_mutex_lock();
3866 wined3d_swapchain_decref(wined3d_swapchain);
3867 wined3d_device_release_focus_window(device->wined3d_device);
3868 wined3d_device_decref(device->wined3d_device);
3869 wined3d_mutex_unlock();
3870 heap_free(device->handle_table.entries);
3871 return hr;