ntdll: Rename local variables in heap_reallocate.
[wine.git] / dlls / d3d8 / device.c
blob6c1646a1893d8c1a8fd6247ded349aae59a37be4
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_context_get_viewports(device->immediate_context, NULL, &vp);
565 wined3d_stateblock_set_viewport(device->state, &vp);
566 wined3d_device_context_get_scissor_rects(device->immediate_context, 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_context_get_rendertarget_view(device->immediate_context, 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_context_get_depth_stencil_view(device->immediate_context);
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_views(device->immediate_context,
1548 0, 1, &rtv, TRUE)))
1549 device_reset_viewport_state(device);
1550 else
1551 wined3d_device_context_set_depth_stencil_view(device->immediate_context, original_dsv);
1553 d3d8_surface_release_rendertarget_view(rt_impl, rtv);
1556 wined3d_mutex_unlock();
1558 return hr;
1561 static HRESULT WINAPI d3d8_device_GetRenderTarget(IDirect3DDevice8 *iface, IDirect3DSurface8 **render_target)
1563 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1564 struct wined3d_rendertarget_view *wined3d_rtv;
1565 struct d3d8_surface *surface_impl;
1566 HRESULT hr;
1568 TRACE("iface %p, render_target %p.\n", iface, render_target);
1570 if (!render_target)
1571 return D3DERR_INVALIDCALL;
1573 wined3d_mutex_lock();
1574 if ((wined3d_rtv = wined3d_device_context_get_rendertarget_view(device->immediate_context, 0)))
1576 /* We want the sub resource parent here, since the view itself may be
1577 * internal to wined3d and may not have a parent. */
1578 surface_impl = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_rtv);
1579 *render_target = &surface_impl->IDirect3DSurface8_iface;
1580 IDirect3DSurface8_AddRef(*render_target);
1581 hr = D3D_OK;
1583 else
1585 ERR("Failed to get wined3d render target.\n");
1586 *render_target = NULL;
1587 hr = D3DERR_NOTFOUND;
1589 wined3d_mutex_unlock();
1591 return hr;
1594 static HRESULT WINAPI d3d8_device_GetDepthStencilSurface(IDirect3DDevice8 *iface, IDirect3DSurface8 **depth_stencil)
1596 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1597 struct wined3d_rendertarget_view *wined3d_dsv;
1598 struct d3d8_surface *surface_impl;
1599 HRESULT hr = D3D_OK;
1601 TRACE("iface %p, depth_stencil %p.\n", iface, depth_stencil);
1603 if (!depth_stencil)
1604 return D3DERR_INVALIDCALL;
1606 wined3d_mutex_lock();
1607 if ((wined3d_dsv = wined3d_device_context_get_depth_stencil_view(device->immediate_context)))
1609 /* We want the sub resource parent here, since the view itself may be
1610 * internal to wined3d and may not have a parent. */
1611 surface_impl = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_dsv);
1612 *depth_stencil = &surface_impl->IDirect3DSurface8_iface;
1613 IDirect3DSurface8_AddRef(*depth_stencil);
1615 else
1617 hr = D3DERR_NOTFOUND;
1618 *depth_stencil = NULL;
1620 wined3d_mutex_unlock();
1622 return hr;
1625 static HRESULT WINAPI d3d8_device_BeginScene(IDirect3DDevice8 *iface)
1627 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1628 HRESULT hr;
1630 TRACE("iface %p.\n", iface);
1632 wined3d_mutex_lock();
1633 hr = wined3d_device_begin_scene(device->wined3d_device);
1634 wined3d_mutex_unlock();
1636 return hr;
1639 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d8_device_EndScene(IDirect3DDevice8 *iface)
1641 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1642 HRESULT hr;
1644 TRACE("iface %p.\n", iface);
1646 wined3d_mutex_lock();
1647 hr = wined3d_device_end_scene(device->wined3d_device);
1648 wined3d_mutex_unlock();
1650 return hr;
1653 static HRESULT WINAPI d3d8_device_Clear(IDirect3DDevice8 *iface, DWORD rect_count,
1654 const D3DRECT *rects, DWORD flags, D3DCOLOR color, float z, DWORD stencil)
1656 const struct wined3d_color c =
1658 ((color >> 16) & 0xff) / 255.0f,
1659 ((color >> 8) & 0xff) / 255.0f,
1660 (color & 0xff) / 255.0f,
1661 ((color >> 24) & 0xff) / 255.0f,
1663 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1664 HRESULT hr;
1666 TRACE("iface %p, rect_count %u, rects %p, flags %#x, color 0x%08x, z %.8e, stencil %u.\n",
1667 iface, rect_count, rects, flags, color, z, stencil);
1669 if (rect_count && !rects)
1671 WARN("count %u with NULL rects.\n", rect_count);
1672 rect_count = 0;
1675 wined3d_mutex_lock();
1676 wined3d_device_apply_stateblock(device->wined3d_device, device->state);
1677 hr = wined3d_device_clear(device->wined3d_device, rect_count, (const RECT *)rects, flags, &c, z, stencil);
1678 wined3d_mutex_unlock();
1680 return hr;
1683 static HRESULT WINAPI d3d8_device_SetTransform(IDirect3DDevice8 *iface,
1684 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
1686 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1688 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1690 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1691 wined3d_mutex_lock();
1692 wined3d_stateblock_set_transform(device->update_state,
1693 wined3d_transform_state_from_d3d(state), (const struct wined3d_matrix *)matrix);
1694 wined3d_mutex_unlock();
1696 return D3D_OK;
1699 static HRESULT WINAPI d3d8_device_GetTransform(IDirect3DDevice8 *iface,
1700 D3DTRANSFORMSTATETYPE state, D3DMATRIX *matrix)
1702 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1704 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1706 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1707 wined3d_mutex_lock();
1708 memcpy(matrix, &device->stateblock_state->transforms[state], sizeof(*matrix));
1709 wined3d_mutex_unlock();
1711 return D3D_OK;
1714 static HRESULT WINAPI d3d8_device_MultiplyTransform(IDirect3DDevice8 *iface,
1715 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
1717 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1719 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1721 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1722 wined3d_mutex_lock();
1723 wined3d_stateblock_multiply_transform(device->state,
1724 wined3d_transform_state_from_d3d(state), (const struct wined3d_matrix *)matrix);
1725 wined3d_mutex_unlock();
1727 return D3D_OK;
1730 static HRESULT WINAPI d3d8_device_SetViewport(IDirect3DDevice8 *iface, const D3DVIEWPORT8 *viewport)
1732 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1733 struct wined3d_sub_resource_desc rt_desc;
1734 struct wined3d_rendertarget_view *rtv;
1735 struct d3d8_surface *surface;
1736 struct wined3d_viewport vp;
1738 TRACE("iface %p, viewport %p.\n", iface, viewport);
1740 wined3d_mutex_lock();
1741 if (!(rtv = wined3d_device_context_get_rendertarget_view(device->immediate_context, 0)))
1743 wined3d_mutex_unlock();
1744 return D3DERR_NOTFOUND;
1746 surface = wined3d_rendertarget_view_get_sub_resource_parent(rtv);
1747 wined3d_texture_get_sub_resource_desc(surface->wined3d_texture, surface->sub_resource_idx, &rt_desc);
1749 if (viewport->X > rt_desc.width || viewport->Width > rt_desc.width - viewport->X
1750 || viewport->Y > rt_desc.height || viewport->Height > rt_desc.height - viewport->Y)
1752 WARN("Invalid viewport, returning D3DERR_INVALIDCALL.\n");
1753 wined3d_mutex_unlock();
1754 return D3DERR_INVALIDCALL;
1757 vp.x = viewport->X;
1758 vp.y = viewport->Y;
1759 vp.width = viewport->Width;
1760 vp.height = viewport->Height;
1761 vp.min_z = viewport->MinZ;
1762 vp.max_z = viewport->MaxZ;
1764 wined3d_stateblock_set_viewport(device->update_state, &vp);
1765 wined3d_mutex_unlock();
1767 return D3D_OK;
1770 static HRESULT WINAPI d3d8_device_GetViewport(IDirect3DDevice8 *iface, D3DVIEWPORT8 *viewport)
1772 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1773 struct wined3d_viewport wined3d_viewport;
1775 TRACE("iface %p, viewport %p.\n", iface, viewport);
1777 wined3d_mutex_lock();
1778 wined3d_viewport = device->stateblock_state->viewport;
1779 wined3d_mutex_unlock();
1781 viewport->X = wined3d_viewport.x;
1782 viewport->Y = wined3d_viewport.y;
1783 viewport->Width = wined3d_viewport.width;
1784 viewport->Height = wined3d_viewport.height;
1785 viewport->MinZ = wined3d_viewport.min_z;
1786 viewport->MaxZ = wined3d_viewport.max_z;
1788 return D3D_OK;
1791 static HRESULT WINAPI d3d8_device_SetMaterial(IDirect3DDevice8 *iface, const D3DMATERIAL8 *material)
1793 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1795 TRACE("iface %p, material %p.\n", iface, material);
1797 /* Note: D3DMATERIAL8 is compatible with struct wined3d_material. */
1798 wined3d_mutex_lock();
1799 wined3d_stateblock_set_material(device->update_state, (const struct wined3d_material *)material);
1800 wined3d_mutex_unlock();
1802 return D3D_OK;
1805 static HRESULT WINAPI d3d8_device_GetMaterial(IDirect3DDevice8 *iface, D3DMATERIAL8 *material)
1807 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1809 TRACE("iface %p, material %p.\n", iface, material);
1811 /* Note: D3DMATERIAL8 is compatible with struct wined3d_material. */
1812 wined3d_mutex_lock();
1813 memcpy(material, &device->stateblock_state->material, sizeof(*material));
1814 wined3d_mutex_unlock();
1816 return D3D_OK;
1819 static HRESULT WINAPI d3d8_device_SetLight(IDirect3DDevice8 *iface, DWORD index, const D3DLIGHT8 *light)
1821 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1822 HRESULT hr;
1824 TRACE("iface %p, index %u, light %p.\n", iface, index, light);
1826 /* Note: D3DLIGHT8 is compatible with struct wined3d_light. */
1827 wined3d_mutex_lock();
1828 hr = wined3d_stateblock_set_light(device->update_state, index, (const struct wined3d_light *)light);
1829 wined3d_mutex_unlock();
1831 return hr;
1834 static HRESULT WINAPI d3d8_device_GetLight(IDirect3DDevice8 *iface, DWORD index, D3DLIGHT8 *light)
1836 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1837 BOOL enabled;
1838 HRESULT hr;
1840 TRACE("iface %p, index %u, light %p.\n", iface, index, light);
1842 /* Note: D3DLIGHT8 is compatible with struct wined3d_light. */
1843 wined3d_mutex_lock();
1844 hr = wined3d_stateblock_get_light(device->state, index, (struct wined3d_light *)light, &enabled);
1845 wined3d_mutex_unlock();
1847 return hr;
1850 static HRESULT WINAPI d3d8_device_LightEnable(IDirect3DDevice8 *iface, DWORD index, BOOL enable)
1852 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1853 HRESULT hr;
1855 TRACE("iface %p, index %u, enable %#x.\n", iface, index, enable);
1857 wined3d_mutex_lock();
1858 hr = wined3d_stateblock_set_light_enable(device->update_state, index, enable);
1859 wined3d_mutex_unlock();
1861 return hr;
1864 static HRESULT WINAPI d3d8_device_GetLightEnable(IDirect3DDevice8 *iface, DWORD index, BOOL *enabled)
1866 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1867 struct wined3d_light light;
1868 HRESULT hr;
1870 TRACE("iface %p, index %u, enabled %p.\n", iface, index, enabled);
1872 wined3d_mutex_lock();
1873 hr = wined3d_stateblock_get_light(device->state, index, &light, enabled);
1874 wined3d_mutex_unlock();
1876 return hr;
1879 static HRESULT WINAPI d3d8_device_SetClipPlane(IDirect3DDevice8 *iface, DWORD index, const float *plane)
1881 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1882 HRESULT hr;
1884 TRACE("iface %p, index %u, plane %p.\n", iface, index, plane);
1886 wined3d_mutex_lock();
1887 hr = wined3d_stateblock_set_clip_plane(device->update_state, index, (const struct wined3d_vec4 *)plane);
1888 wined3d_mutex_unlock();
1890 return hr;
1893 static HRESULT WINAPI d3d8_device_GetClipPlane(IDirect3DDevice8 *iface, DWORD index, float *plane)
1895 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1897 TRACE("iface %p, index %u, plane %p.\n", iface, index, plane);
1899 if (index >= device->max_user_clip_planes)
1901 TRACE("Plane %u requested, but only %u planes are supported.\n", index, device->max_user_clip_planes);
1902 return WINED3DERR_INVALIDCALL;
1905 wined3d_mutex_lock();
1906 memcpy(plane, &device->stateblock_state->clip_planes[index], sizeof(struct wined3d_vec4));
1907 wined3d_mutex_unlock();
1909 return D3D_OK;
1912 static void resolve_depth_buffer(struct d3d8_device *device)
1914 const struct wined3d_stateblock_state *state = device->stateblock_state;
1915 struct wined3d_rendertarget_view *wined3d_dsv;
1916 struct wined3d_resource *dst_resource;
1917 struct wined3d_texture *dst_texture;
1918 struct wined3d_resource_desc desc;
1919 struct d3d8_surface *d3d8_dsv;
1921 if (!(dst_texture = state->textures[0]))
1922 return;
1923 dst_resource = wined3d_texture_get_resource(dst_texture);
1924 wined3d_resource_get_desc(dst_resource, &desc);
1925 if (desc.format != WINED3DFMT_D24_UNORM_S8_UINT
1926 && desc.format != WINED3DFMT_X8D24_UNORM
1927 && desc.format != WINED3DFMT_DF16
1928 && desc.format != WINED3DFMT_DF24
1929 && desc.format != WINED3DFMT_INTZ)
1930 return;
1932 if (!(wined3d_dsv = wined3d_device_context_get_depth_stencil_view(device->immediate_context)))
1933 return;
1934 d3d8_dsv = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_dsv);
1936 wined3d_device_context_resolve_sub_resource(device->immediate_context, dst_resource, 0,
1937 wined3d_rendertarget_view_get_resource(wined3d_dsv), d3d8_dsv->sub_resource_idx, desc.format);
1940 static HRESULT WINAPI d3d8_device_SetRenderState(IDirect3DDevice8 *iface,
1941 D3DRENDERSTATETYPE state, DWORD value)
1943 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1945 TRACE("iface %p, state %#x, value %#x.\n", iface, state, value);
1947 wined3d_mutex_lock();
1948 wined3d_stateblock_set_render_state(device->update_state, wined3d_render_state_from_d3d(state), value);
1949 if (state == D3DRS_POINTSIZE && value == D3D8_RESZ_CODE)
1950 resolve_depth_buffer(device);
1951 wined3d_mutex_unlock();
1953 return D3D_OK;
1956 static HRESULT WINAPI d3d8_device_GetRenderState(IDirect3DDevice8 *iface,
1957 D3DRENDERSTATETYPE state, DWORD *value)
1959 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1960 const struct wined3d_stateblock_state *device_state;
1962 TRACE("iface %p, state %#x, value %p.\n", iface, state, value);
1964 wined3d_mutex_lock();
1965 device_state = device->stateblock_state;
1966 switch (state)
1968 case D3DRS_ZBIAS:
1969 *value = device_state->rs[WINED3D_RS_DEPTHBIAS];
1970 break;
1972 default:
1973 *value = device_state->rs[state];
1975 wined3d_mutex_unlock();
1977 return D3D_OK;
1980 static HRESULT WINAPI d3d8_device_BeginStateBlock(IDirect3DDevice8 *iface)
1982 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1983 struct wined3d_stateblock *stateblock;
1984 HRESULT hr;
1986 TRACE("iface %p.\n", iface);
1988 wined3d_mutex_lock();
1989 if (device->recording)
1991 wined3d_mutex_unlock();
1992 WARN("Trying to begin a stateblock while recording, returning D3DERR_INVALIDCALL.\n");
1993 return D3DERR_INVALIDCALL;
1996 if (SUCCEEDED(hr = wined3d_stateblock_create(device->wined3d_device, NULL, WINED3D_SBT_RECORDED, &stateblock)))
1997 device->update_state = device->recording = stateblock;
1998 wined3d_mutex_unlock();
2000 return hr;
2003 static HRESULT WINAPI d3d8_device_EndStateBlock(IDirect3DDevice8 *iface, DWORD *token)
2005 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2006 struct wined3d_stateblock *stateblock;
2008 TRACE("iface %p, token %p.\n", iface, token);
2010 /* Tell wineD3D to endstateblock before anything else (in case we run out
2011 * of memory later and cause locking problems)
2013 wined3d_mutex_lock();
2014 if (!device->recording)
2016 wined3d_mutex_unlock();
2017 WARN("Trying to end a stateblock, but no stateblock is being recorded.\n");
2018 return D3DERR_INVALIDCALL;
2020 stateblock = device->recording;
2021 wined3d_stateblock_init_contained_states(stateblock);
2022 device->recording = NULL;
2023 device->update_state = device->state;
2025 *token = d3d8_allocate_handle(&device->handle_table, stateblock, D3D8_HANDLE_SB);
2026 wined3d_mutex_unlock();
2028 if (*token == D3D8_INVALID_HANDLE)
2030 ERR("Failed to create a handle\n");
2031 wined3d_mutex_lock();
2032 wined3d_stateblock_decref(stateblock);
2033 wined3d_mutex_unlock();
2034 return E_FAIL;
2036 ++*token;
2038 TRACE("Returning %#x (%p).\n", *token, stateblock);
2040 return D3D_OK;
2043 static HRESULT WINAPI d3d8_device_ApplyStateBlock(IDirect3DDevice8 *iface, DWORD token)
2045 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2046 const struct wined3d_stateblock_state *state;
2047 struct wined3d_stateblock *stateblock;
2048 struct wined3d_buffer *wined3d_buffer;
2049 struct d3d8_vertexbuffer *buffer;
2050 unsigned int i;
2052 TRACE("iface %p, token %#x.\n", iface, token);
2054 if (!token)
2055 return D3D_OK;
2057 wined3d_mutex_lock();
2058 if (device->recording)
2060 wined3d_mutex_unlock();
2061 WARN("Trying to apply stateblock while recording, returning D3DERR_INVALIDCALL.\n");
2062 return D3DERR_INVALIDCALL;
2064 stateblock = d3d8_get_object(&device->handle_table, token - 1, D3D8_HANDLE_SB);
2065 if (!stateblock)
2067 WARN("Invalid handle (%#x) passed.\n", token);
2068 wined3d_mutex_unlock();
2069 return D3DERR_INVALIDCALL;
2071 wined3d_stateblock_apply(stateblock, device->state);
2072 device->sysmem_vb = 0;
2073 state = device->stateblock_state;
2074 for (i = 0; i < D3D8_MAX_STREAMS; ++i)
2076 wined3d_buffer = state->streams[i].buffer;
2077 if (!wined3d_buffer || !(buffer = wined3d_buffer_get_parent(wined3d_buffer)))
2078 continue;
2079 if (buffer->draw_buffer)
2080 device->sysmem_vb |= 1u << i;
2082 device->sysmem_ib = (wined3d_buffer = state->index_buffer)
2083 && (buffer = wined3d_buffer_get_parent(wined3d_buffer)) && buffer->draw_buffer;
2084 wined3d_mutex_unlock();
2086 return D3D_OK;
2089 static HRESULT WINAPI d3d8_device_CaptureStateBlock(IDirect3DDevice8 *iface, DWORD token)
2091 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2092 struct wined3d_stateblock *stateblock;
2094 TRACE("iface %p, token %#x.\n", iface, token);
2096 wined3d_mutex_lock();
2097 if (device->recording)
2099 wined3d_mutex_unlock();
2100 WARN("Trying to capture stateblock while recording, returning D3DERR_INVALIDCALL.\n");
2101 return D3DERR_INVALIDCALL;
2103 stateblock = d3d8_get_object(&device->handle_table, token - 1, D3D8_HANDLE_SB);
2104 if (!stateblock)
2106 WARN("Invalid handle (%#x) passed.\n", token);
2107 wined3d_mutex_unlock();
2108 return D3DERR_INVALIDCALL;
2110 wined3d_stateblock_capture(stateblock, device->state);
2111 wined3d_mutex_unlock();
2113 return D3D_OK;
2116 static HRESULT WINAPI d3d8_device_DeleteStateBlock(IDirect3DDevice8 *iface, DWORD token)
2118 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2119 struct wined3d_stateblock *stateblock;
2121 TRACE("iface %p, token %#x.\n", iface, token);
2123 wined3d_mutex_lock();
2124 stateblock = d3d8_free_handle(&device->handle_table, token - 1, D3D8_HANDLE_SB);
2126 if (!stateblock)
2128 WARN("Invalid handle (%#x) passed.\n", token);
2129 wined3d_mutex_unlock();
2130 return D3DERR_INVALIDCALL;
2133 if (wined3d_stateblock_decref(stateblock))
2135 ERR("Stateblock %p has references left, this shouldn't happen.\n", stateblock);
2137 wined3d_mutex_unlock();
2139 return D3D_OK;
2142 static HRESULT WINAPI d3d8_device_CreateStateBlock(IDirect3DDevice8 *iface,
2143 D3DSTATEBLOCKTYPE type, DWORD *handle)
2145 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2146 struct wined3d_stateblock *stateblock;
2147 HRESULT hr;
2149 TRACE("iface %p, type %#x, handle %p.\n", iface, type, handle);
2151 if (type != D3DSBT_ALL
2152 && type != D3DSBT_PIXELSTATE
2153 && type != D3DSBT_VERTEXSTATE)
2155 WARN("Unexpected stateblock type, returning D3DERR_INVALIDCALL\n");
2156 return D3DERR_INVALIDCALL;
2159 wined3d_mutex_lock();
2160 if (device->recording)
2162 wined3d_mutex_unlock();
2163 WARN("Trying to create a stateblock while recording, returning D3DERR_INVALIDCALL.\n");
2164 return D3DERR_INVALIDCALL;
2166 hr = wined3d_stateblock_create(device->wined3d_device, device->state, (enum wined3d_stateblock_type)type, &stateblock);
2167 if (FAILED(hr))
2169 wined3d_mutex_unlock();
2170 ERR("Failed to create the state block, hr %#x\n", hr);
2171 return hr;
2174 *handle = d3d8_allocate_handle(&device->handle_table, stateblock, D3D8_HANDLE_SB);
2175 wined3d_mutex_unlock();
2177 if (*handle == D3D8_INVALID_HANDLE)
2179 ERR("Failed to allocate a handle.\n");
2180 wined3d_mutex_lock();
2181 wined3d_stateblock_decref(stateblock);
2182 wined3d_mutex_unlock();
2183 return E_FAIL;
2185 ++*handle;
2187 TRACE("Returning %#x (%p).\n", *handle, stateblock);
2189 return hr;
2192 static HRESULT WINAPI d3d8_device_SetClipStatus(IDirect3DDevice8 *iface, const D3DCLIPSTATUS8 *clip_status)
2194 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2195 HRESULT hr;
2197 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
2198 /* FIXME: Verify that D3DCLIPSTATUS8 ~= struct wined3d_clip_status. */
2200 wined3d_mutex_lock();
2201 hr = wined3d_device_set_clip_status(device->wined3d_device, (const struct wined3d_clip_status *)clip_status);
2202 wined3d_mutex_unlock();
2204 return hr;
2207 static HRESULT WINAPI d3d8_device_GetClipStatus(IDirect3DDevice8 *iface, D3DCLIPSTATUS8 *clip_status)
2209 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2210 HRESULT hr;
2212 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
2214 wined3d_mutex_lock();
2215 hr = wined3d_device_get_clip_status(device->wined3d_device, (struct wined3d_clip_status *)clip_status);
2216 wined3d_mutex_unlock();
2218 return hr;
2221 static HRESULT WINAPI d3d8_device_GetTexture(IDirect3DDevice8 *iface, DWORD stage, IDirect3DBaseTexture8 **texture)
2223 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2224 struct wined3d_texture *wined3d_texture;
2225 struct d3d8_texture *texture_impl;
2227 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
2229 if (!texture)
2230 return D3DERR_INVALIDCALL;
2232 if (stage >= WINED3D_MAX_FRAGMENT_SAMPLERS)
2234 WARN("Ignoring invalid stage %u.\n", stage);
2235 *texture = NULL;
2236 return D3D_OK;
2239 wined3d_mutex_lock();
2240 if ((wined3d_texture = device->stateblock_state->textures[stage]))
2242 texture_impl = wined3d_texture_get_parent(wined3d_texture);
2243 *texture = &texture_impl->IDirect3DBaseTexture8_iface;
2244 IDirect3DBaseTexture8_AddRef(*texture);
2246 else
2248 *texture = NULL;
2250 wined3d_mutex_unlock();
2252 return D3D_OK;
2255 static HRESULT WINAPI d3d8_device_SetTexture(IDirect3DDevice8 *iface, DWORD stage, IDirect3DBaseTexture8 *texture)
2257 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2258 struct d3d8_texture *texture_impl;
2260 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
2262 texture_impl = unsafe_impl_from_IDirect3DBaseTexture8(texture);
2264 wined3d_mutex_lock();
2265 wined3d_stateblock_set_texture(device->update_state, stage,
2266 texture_impl ? texture_impl->wined3d_texture : NULL);
2267 wined3d_mutex_unlock();
2269 return D3D_OK;
2272 static const struct tss_lookup
2274 BOOL sampler_state;
2275 union
2277 enum wined3d_texture_stage_state texture_state;
2278 enum wined3d_sampler_state sampler_state;
2279 } u;
2281 tss_lookup[] =
2283 {FALSE, {WINED3D_TSS_INVALID}}, /* 0, unused */
2284 {FALSE, {WINED3D_TSS_COLOR_OP}}, /* 1, D3DTSS_COLOROP */
2285 {FALSE, {WINED3D_TSS_COLOR_ARG1}}, /* 2, D3DTSS_COLORARG1 */
2286 {FALSE, {WINED3D_TSS_COLOR_ARG2}}, /* 3, D3DTSS_COLORARG2 */
2287 {FALSE, {WINED3D_TSS_ALPHA_OP}}, /* 4, D3DTSS_ALPHAOP */
2288 {FALSE, {WINED3D_TSS_ALPHA_ARG1}}, /* 5, D3DTSS_ALPHAARG1 */
2289 {FALSE, {WINED3D_TSS_ALPHA_ARG2}}, /* 6, D3DTSS_ALPHAARG2 */
2290 {FALSE, {WINED3D_TSS_BUMPENV_MAT00}}, /* 7, D3DTSS_BUMPENVMAT00 */
2291 {FALSE, {WINED3D_TSS_BUMPENV_MAT01}}, /* 8, D3DTSS_BUMPENVMAT01 */
2292 {FALSE, {WINED3D_TSS_BUMPENV_MAT10}}, /* 9, D3DTSS_BUMPENVMAT10 */
2293 {FALSE, {WINED3D_TSS_BUMPENV_MAT11}}, /* 10, D3DTSS_BUMPENVMAT11 */
2294 {FALSE, {WINED3D_TSS_TEXCOORD_INDEX}}, /* 11, D3DTSS_TEXCOORDINDEX */
2295 {FALSE, {WINED3D_TSS_INVALID}}, /* 12, unused */
2296 {TRUE, {WINED3D_SAMP_ADDRESS_U}}, /* 13, D3DTSS_ADDRESSU */
2297 {TRUE, {WINED3D_SAMP_ADDRESS_V}}, /* 14, D3DTSS_ADDRESSV */
2298 {TRUE, {WINED3D_SAMP_BORDER_COLOR}}, /* 15, D3DTSS_BORDERCOLOR */
2299 {TRUE, {WINED3D_SAMP_MAG_FILTER}}, /* 16, D3DTSS_MAGFILTER */
2300 {TRUE, {WINED3D_SAMP_MIN_FILTER}}, /* 17, D3DTSS_MINFILTER */
2301 {TRUE, {WINED3D_SAMP_MIP_FILTER}}, /* 18, D3DTSS_MIPFILTER */
2302 {TRUE, {WINED3D_SAMP_MIPMAP_LOD_BIAS}}, /* 19, D3DTSS_MIPMAPLODBIAS */
2303 {TRUE, {WINED3D_SAMP_MAX_MIP_LEVEL}}, /* 20, D3DTSS_MAXMIPLEVEL */
2304 {TRUE, {WINED3D_SAMP_MAX_ANISOTROPY}}, /* 21, D3DTSS_MAXANISOTROPY */
2305 {FALSE, {WINED3D_TSS_BUMPENV_LSCALE}}, /* 22, D3DTSS_BUMPENVLSCALE */
2306 {FALSE, {WINED3D_TSS_BUMPENV_LOFFSET}}, /* 23, D3DTSS_BUMPENVLOFFSET */
2307 {FALSE, {WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS}}, /* 24, D3DTSS_TEXTURETRANSFORMFLAGS */
2308 {TRUE, {WINED3D_SAMP_ADDRESS_W}}, /* 25, D3DTSS_ADDRESSW */
2309 {FALSE, {WINED3D_TSS_COLOR_ARG0}}, /* 26, D3DTSS_COLORARG0 */
2310 {FALSE, {WINED3D_TSS_ALPHA_ARG0}}, /* 27, D3DTSS_ALPHAARG0 */
2311 {FALSE, {WINED3D_TSS_RESULT_ARG}}, /* 28, D3DTSS_RESULTARG */
2314 static HRESULT WINAPI d3d8_device_GetTextureStageState(IDirect3DDevice8 *iface,
2315 DWORD stage, D3DTEXTURESTAGESTATETYPE state, DWORD *value)
2317 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2318 const struct wined3d_stateblock_state *device_state;
2319 const struct tss_lookup *l;
2321 TRACE("iface %p, stage %u, state %#x, value %p.\n", iface, stage, state, value);
2323 if (state >= ARRAY_SIZE(tss_lookup))
2325 WARN("Invalid state %#x.\n", state);
2326 return D3D_OK;
2329 l = &tss_lookup[state];
2331 wined3d_mutex_lock();
2332 device_state = device->stateblock_state;
2333 if (l->sampler_state)
2334 *value = device_state->sampler_states[stage][l->u.sampler_state];
2335 else
2336 *value = device_state->texture_states[stage][l->u.texture_state];
2337 wined3d_mutex_unlock();
2339 return D3D_OK;
2342 static HRESULT WINAPI d3d8_device_SetTextureStageState(IDirect3DDevice8 *iface,
2343 DWORD stage, D3DTEXTURESTAGESTATETYPE type, DWORD value)
2345 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2346 const struct tss_lookup *l;
2348 TRACE("iface %p, stage %u, state %#x, value %#x.\n", iface, stage, type, value);
2350 if (type >= ARRAY_SIZE(tss_lookup))
2352 WARN("Invalid type %#x passed.\n", type);
2353 return D3D_OK;
2356 l = &tss_lookup[type];
2358 wined3d_mutex_lock();
2359 if (l->sampler_state)
2360 wined3d_stateblock_set_sampler_state(device->update_state, stage, l->u.sampler_state, value);
2361 else
2362 wined3d_stateblock_set_texture_stage_state(device->update_state, stage, l->u.texture_state, value);
2363 wined3d_mutex_unlock();
2365 return D3D_OK;
2368 static HRESULT WINAPI d3d8_device_ValidateDevice(IDirect3DDevice8 *iface, DWORD *pass_count)
2370 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2371 HRESULT hr;
2373 TRACE("iface %p, pass_count %p.\n", iface, pass_count);
2375 wined3d_mutex_lock();
2376 wined3d_device_apply_stateblock(device->wined3d_device, device->state);
2377 hr = wined3d_device_validate_device(device->wined3d_device, pass_count);
2378 wined3d_mutex_unlock();
2380 return hr;
2383 static HRESULT WINAPI d3d8_device_GetInfo(IDirect3DDevice8 *iface,
2384 DWORD info_id, void *info, DWORD info_size)
2386 TRACE("iface %p, info_id %#x, info %p, info_size %u.\n", iface, info_id, info, info_size);
2388 if (info_id < 4)
2389 return E_FAIL;
2390 return S_FALSE;
2393 static HRESULT WINAPI d3d8_device_SetPaletteEntries(IDirect3DDevice8 *iface,
2394 UINT palette_idx, const PALETTEENTRY *entries)
2396 WARN("iface %p, palette_idx %u, entries %p unimplemented\n", iface, palette_idx, entries);
2398 /* GPUs stopped supporting palettized textures with the Shader Model 1 generation. Wined3d
2399 * does not have a d3d8/9-style palette API */
2401 return D3D_OK;
2404 static HRESULT WINAPI d3d8_device_GetPaletteEntries(IDirect3DDevice8 *iface,
2405 UINT palette_idx, PALETTEENTRY *entries)
2407 FIXME("iface %p, palette_idx %u, entries %p unimplemented.\n", iface, palette_idx, entries);
2409 return D3DERR_INVALIDCALL;
2412 static HRESULT WINAPI d3d8_device_SetCurrentTexturePalette(IDirect3DDevice8 *iface, UINT palette_idx)
2414 WARN("iface %p, palette_idx %u unimplemented.\n", iface, palette_idx);
2416 return D3D_OK;
2419 static HRESULT WINAPI d3d8_device_GetCurrentTexturePalette(IDirect3DDevice8 *iface, UINT *palette_idx)
2421 FIXME("iface %p, palette_idx %p unimplemented.\n", iface, palette_idx);
2423 return D3DERR_INVALIDCALL;
2426 static void d3d8_device_upload_sysmem_vertex_buffers(struct d3d8_device *device,
2427 unsigned int start_vertex, unsigned int vertex_count)
2429 const struct wined3d_stateblock_state *state = device->stateblock_state;
2430 struct wined3d_vertex_declaration *wined3d_decl;
2431 struct wined3d_box box = {0, 0, 0, 1, 0, 1};
2432 const struct wined3d_stream_state *stream;
2433 struct d3d8_vertexbuffer *d3d8_buffer;
2434 struct wined3d_resource *dst_resource;
2435 struct d3d8_vertex_declaration *decl;
2436 struct wined3d_buffer *dst_buffer;
2437 struct wined3d_resource_desc desc;
2438 unsigned int stride, map;
2439 HRESULT hr;
2441 if (!device->sysmem_vb)
2442 return;
2444 if (!(wined3d_decl = state->vertex_declaration))
2445 return;
2447 decl = wined3d_vertex_declaration_get_parent(wined3d_decl);
2448 map = decl->stream_map & device->sysmem_vb;
2449 while (map)
2451 stream = &state->streams[wined3d_bit_scan(&map)];
2452 dst_buffer = stream->buffer;
2453 stride = stream->stride;
2455 d3d8_buffer = wined3d_buffer_get_parent(dst_buffer);
2456 dst_resource = wined3d_buffer_get_resource(dst_buffer);
2457 wined3d_resource_get_desc(dst_resource, &desc);
2458 box.left = stream->offset + start_vertex * stride;
2459 box.right = min(box.left + vertex_count * stride, desc.size);
2460 if (FAILED(hr = wined3d_device_context_copy_sub_resource_region(device->immediate_context,
2461 dst_resource, 0, box.left, 0, 0,
2462 wined3d_buffer_get_resource(d3d8_buffer->wined3d_buffer), 0, &box, 0)))
2463 ERR("Failed to update buffer.\n");
2467 static void d3d8_device_upload_sysmem_index_buffer(struct d3d8_device *device,
2468 unsigned int start_idx, unsigned int idx_count)
2470 const struct wined3d_stateblock_state *state = device->stateblock_state;
2471 struct wined3d_box box = {0, 0, 0, 1, 0, 1};
2472 struct wined3d_resource *dst_resource;
2473 struct d3d8_indexbuffer *d3d8_buffer;
2474 struct wined3d_buffer *dst_buffer;
2475 struct wined3d_resource_desc desc;
2476 enum wined3d_format_id format;
2477 unsigned int idx_size;
2478 HRESULT hr;
2480 if (!device->sysmem_ib)
2481 return;
2483 dst_buffer = state->index_buffer;
2484 format = state->index_format;
2485 d3d8_buffer = wined3d_buffer_get_parent(dst_buffer);
2486 dst_resource = wined3d_buffer_get_resource(dst_buffer);
2487 wined3d_resource_get_desc(dst_resource, &desc);
2488 idx_size = format == WINED3DFMT_R16_UINT ? 2 : 4;
2489 box.left = start_idx * idx_size;
2490 box.right = min(box.left + idx_count * idx_size, desc.size);
2491 if (FAILED(hr = wined3d_device_context_copy_sub_resource_region(device->immediate_context,
2492 dst_resource, 0, box.left, 0, 0,
2493 wined3d_buffer_get_resource(d3d8_buffer->wined3d_buffer), 0, &box, 0)))
2494 ERR("Failed to update buffer.\n");
2497 static HRESULT WINAPI d3d8_device_DrawPrimitive(IDirect3DDevice8 *iface,
2498 D3DPRIMITIVETYPE primitive_type, UINT start_vertex, UINT primitive_count)
2500 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2501 unsigned int vertex_count;
2503 TRACE("iface %p, primitive_type %#x, start_vertex %u, primitive_count %u.\n",
2504 iface, primitive_type, start_vertex, primitive_count);
2506 vertex_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2507 wined3d_mutex_lock();
2508 d3d8_device_upload_sysmem_vertex_buffers(device, start_vertex, vertex_count);
2509 wined3d_device_context_set_primitive_type(device->immediate_context,
2510 wined3d_primitive_type_from_d3d(primitive_type), 0);
2511 wined3d_device_apply_stateblock(device->wined3d_device, device->state);
2512 wined3d_device_context_draw(device->immediate_context, start_vertex, vertex_count, 0, 0);
2513 wined3d_mutex_unlock();
2515 return D3D_OK;
2518 static HRESULT WINAPI d3d8_device_DrawIndexedPrimitive(IDirect3DDevice8 *iface,
2519 D3DPRIMITIVETYPE primitive_type, UINT min_vertex_idx, UINT vertex_count,
2520 UINT start_idx, UINT primitive_count)
2522 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2523 unsigned int index_count;
2524 int base_vertex_index;
2526 TRACE("iface %p, primitive_type %#x, min_vertex_idx %u, vertex_count %u, start_idx %u, primitive_count %u.\n",
2527 iface, primitive_type, min_vertex_idx, vertex_count, start_idx, primitive_count);
2529 index_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2530 wined3d_mutex_lock();
2531 if (!device->stateblock_state->index_buffer)
2533 wined3d_mutex_unlock();
2534 WARN("Index buffer not set, returning D3D_OK.\n");
2535 return D3D_OK;
2537 base_vertex_index = device->stateblock_state->base_vertex_index;
2538 d3d8_device_upload_sysmem_vertex_buffers(device, base_vertex_index + min_vertex_idx, vertex_count);
2539 d3d8_device_upload_sysmem_index_buffer(device, start_idx, index_count);
2540 wined3d_device_context_set_primitive_type(device->immediate_context,
2541 wined3d_primitive_type_from_d3d(primitive_type), 0);
2542 wined3d_device_apply_stateblock(device->wined3d_device, device->state);
2543 wined3d_device_context_draw_indexed(device->immediate_context, base_vertex_index, start_idx, index_count, 0, 0);
2544 wined3d_mutex_unlock();
2546 return D3D_OK;
2549 /* The caller is responsible for wined3d locking */
2550 static HRESULT d3d8_device_prepare_vertex_buffer(struct d3d8_device *device, UINT min_size)
2552 HRESULT hr;
2554 if (device->vertex_buffer_size < min_size || !device->vertex_buffer)
2556 UINT size = max(device->vertex_buffer_size * 2, min_size);
2557 struct wined3d_buffer_desc desc;
2558 struct wined3d_buffer *buffer;
2560 TRACE("Growing vertex buffer to %u bytes\n", size);
2562 desc.byte_width = size;
2563 desc.usage = WINED3DUSAGE_DYNAMIC;
2564 desc.bind_flags = WINED3D_BIND_VERTEX_BUFFER;
2565 desc.access = WINED3D_RESOURCE_ACCESS_GPU | WINED3D_RESOURCE_ACCESS_MAP_W;
2566 desc.misc_flags = 0;
2567 desc.structure_byte_stride = 0;
2569 if (FAILED(hr = wined3d_buffer_create(device->wined3d_device, &desc,
2570 NULL, NULL, &d3d8_null_wined3d_parent_ops, &buffer)))
2572 ERR("Failed to create vertex buffer, hr %#x.\n", hr);
2573 return hr;
2576 if (device->vertex_buffer)
2577 wined3d_buffer_decref(device->vertex_buffer);
2579 device->vertex_buffer = buffer;
2580 device->vertex_buffer_size = size;
2581 device->vertex_buffer_pos = 0;
2583 return D3D_OK;
2586 static HRESULT WINAPI d3d8_device_DrawPrimitiveUP(IDirect3DDevice8 *iface,
2587 D3DPRIMITIVETYPE primitive_type, UINT primitive_count, const void *data,
2588 UINT stride)
2590 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2591 HRESULT hr;
2592 UINT vtx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2593 struct wined3d_map_desc wined3d_map_desc;
2594 struct wined3d_box wined3d_box = {0};
2595 UINT size = vtx_count * stride;
2596 struct wined3d_resource *vb;
2597 UINT vb_pos, align;
2599 TRACE("iface %p, primitive_type %#x, primitive_count %u, data %p, stride %u.\n",
2600 iface, primitive_type, primitive_count, data, stride);
2602 if (!primitive_count || !stride)
2604 WARN("primitive_count or stride is 0, returning D3D_OK.\n");
2605 return D3D_OK;
2608 wined3d_mutex_lock();
2609 hr = d3d8_device_prepare_vertex_buffer(device, size);
2610 if (FAILED(hr))
2611 goto done;
2613 vb_pos = device->vertex_buffer_pos;
2614 align = vb_pos % stride;
2615 if (align) align = stride - align;
2616 if (vb_pos + size + align > device->vertex_buffer_size)
2617 vb_pos = 0;
2618 else
2619 vb_pos += align;
2621 wined3d_box.left = vb_pos;
2622 wined3d_box.right = vb_pos + size;
2623 vb = wined3d_buffer_get_resource(device->vertex_buffer);
2624 if (FAILED(wined3d_resource_map(vb, 0, &wined3d_map_desc, &wined3d_box,
2625 WINED3D_MAP_WRITE | (vb_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD))))
2626 goto done;
2627 memcpy(wined3d_map_desc.data, data, size);
2628 wined3d_resource_unmap(vb, 0);
2629 device->vertex_buffer_pos = vb_pos + size;
2631 hr = wined3d_stateblock_set_stream_source(device->state, 0, device->vertex_buffer, 0, stride);
2632 if (FAILED(hr))
2633 goto done;
2635 wined3d_device_context_set_primitive_type(device->immediate_context,
2636 wined3d_primitive_type_from_d3d(primitive_type), 0);
2637 wined3d_device_apply_stateblock(device->wined3d_device, device->state);
2638 wined3d_device_context_draw(device->immediate_context, vb_pos / stride, vtx_count, 0, 0);
2639 wined3d_stateblock_set_stream_source(device->state, 0, NULL, 0, 0);
2641 done:
2642 wined3d_mutex_unlock();
2643 return hr;
2646 /* The caller is responsible for wined3d locking */
2647 static HRESULT d3d8_device_prepare_index_buffer(struct d3d8_device *device, UINT min_size)
2649 HRESULT hr;
2651 if (device->index_buffer_size < min_size || !device->index_buffer)
2653 UINT size = max(device->index_buffer_size * 2, min_size);
2654 struct wined3d_buffer_desc desc;
2655 struct wined3d_buffer *buffer;
2657 TRACE("Growing index buffer to %u bytes\n", size);
2659 desc.byte_width = size;
2660 desc.usage = WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_STATICDECL;
2661 desc.bind_flags = WINED3D_BIND_INDEX_BUFFER;
2662 desc.access = WINED3D_RESOURCE_ACCESS_GPU | WINED3D_RESOURCE_ACCESS_MAP_W;
2663 desc.misc_flags = 0;
2664 desc.structure_byte_stride = 0;
2666 if (FAILED(hr = wined3d_buffer_create(device->wined3d_device, &desc,
2667 NULL, NULL, &d3d8_null_wined3d_parent_ops, &buffer)))
2669 ERR("Failed to create index buffer, hr %#x.\n", hr);
2670 return hr;
2673 if (device->index_buffer)
2674 wined3d_buffer_decref(device->index_buffer);
2676 device->index_buffer = buffer;
2677 device->index_buffer_size = size;
2678 device->index_buffer_pos = 0;
2680 return D3D_OK;
2683 static HRESULT WINAPI d3d8_device_DrawIndexedPrimitiveUP(IDirect3DDevice8 *iface,
2684 D3DPRIMITIVETYPE primitive_type, UINT min_vertex_idx, UINT vertex_count,
2685 UINT primitive_count, const void *index_data, D3DFORMAT index_format,
2686 const void *vertex_data, UINT vertex_stride)
2688 UINT idx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2689 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2690 UINT idx_fmt_size = index_format == D3DFMT_INDEX16 ? 2 : 4;
2691 UINT vtx_size = vertex_count * vertex_stride;
2692 UINT idx_size = idx_count * idx_fmt_size;
2693 struct wined3d_map_desc wined3d_map_desc;
2694 struct wined3d_box wined3d_box = {0};
2695 struct wined3d_resource *ib, *vb;
2696 unsigned int base_vertex_idx;
2697 UINT vb_pos, ib_pos, align;
2698 HRESULT hr;
2700 TRACE("iface %p, primitive_type %#x, min_vertex_idx %u, vertex_count %u, primitive_count %u, "
2701 "index_data %p, index_format %#x, vertex_data %p, vertex_stride %u.\n",
2702 iface, primitive_type, min_vertex_idx, vertex_count, primitive_count,
2703 index_data, index_format, vertex_data, vertex_stride);
2705 if (!primitive_count || !vertex_stride)
2707 WARN("primitive_count or vertex_stride is 0, returning D3D_OK.\n");
2708 return D3D_OK;
2711 wined3d_mutex_lock();
2713 hr = d3d8_device_prepare_vertex_buffer(device, vtx_size);
2714 if (FAILED(hr))
2715 goto done;
2717 vb_pos = device->vertex_buffer_pos;
2718 align = vb_pos % vertex_stride;
2719 if (align) align = vertex_stride - align;
2720 if (vb_pos + vtx_size + align > device->vertex_buffer_size)
2721 vb_pos = 0;
2722 else
2723 vb_pos += align;
2725 wined3d_box.left = vb_pos;
2726 wined3d_box.right = vb_pos + vtx_size;
2727 vb = wined3d_buffer_get_resource(device->vertex_buffer);
2728 if (FAILED(hr = wined3d_resource_map(vb, 0, &wined3d_map_desc, &wined3d_box,
2729 WINED3D_MAP_WRITE | (vb_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD))))
2730 goto done;
2731 memcpy(wined3d_map_desc.data, (char *)vertex_data + min_vertex_idx * vertex_stride, vtx_size);
2732 wined3d_resource_unmap(vb, 0);
2733 device->vertex_buffer_pos = vb_pos + vtx_size;
2735 hr = d3d8_device_prepare_index_buffer(device, idx_size);
2736 if (FAILED(hr))
2737 goto done;
2739 ib_pos = device->index_buffer_pos;
2740 align = ib_pos % idx_fmt_size;
2741 if (align) align = idx_fmt_size - align;
2742 if (ib_pos + idx_size + align > device->index_buffer_size)
2743 ib_pos = 0;
2744 else
2745 ib_pos += align;
2747 wined3d_box.left = ib_pos;
2748 wined3d_box.right = ib_pos + idx_size;
2749 ib = wined3d_buffer_get_resource(device->index_buffer);
2750 if (FAILED(hr = wined3d_resource_map(ib, 0, &wined3d_map_desc, &wined3d_box,
2751 WINED3D_MAP_WRITE | (ib_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD))))
2752 goto done;
2753 memcpy(wined3d_map_desc.data, index_data, idx_size);
2754 wined3d_resource_unmap(ib, 0);
2755 device->index_buffer_pos = ib_pos + idx_size;
2757 hr = wined3d_stateblock_set_stream_source(device->state, 0, device->vertex_buffer, 0, vertex_stride);
2758 if (FAILED(hr))
2759 goto done;
2761 wined3d_stateblock_set_index_buffer(device->state, device->index_buffer,
2762 wined3dformat_from_d3dformat(index_format));
2763 base_vertex_idx = vb_pos / vertex_stride - min_vertex_idx;
2764 wined3d_stateblock_set_base_vertex_index(device->state, base_vertex_idx);
2766 wined3d_device_context_set_primitive_type(device->immediate_context,
2767 wined3d_primitive_type_from_d3d(primitive_type), 0);
2768 wined3d_device_apply_stateblock(device->wined3d_device, device->state);
2769 wined3d_device_context_draw_indexed(device->immediate_context,
2770 base_vertex_idx, ib_pos / idx_fmt_size, idx_count, 0, 0);
2772 wined3d_stateblock_set_stream_source(device->state, 0, NULL, 0, 0);
2773 wined3d_stateblock_set_index_buffer(device->state, NULL, WINED3DFMT_UNKNOWN);
2774 wined3d_stateblock_set_base_vertex_index(device->state, 0);
2776 done:
2777 wined3d_mutex_unlock();
2778 return hr;
2781 static HRESULT WINAPI d3d8_device_ProcessVertices(IDirect3DDevice8 *iface, UINT src_start_idx,
2782 UINT dst_idx, UINT vertex_count, IDirect3DVertexBuffer8 *dst_buffer, DWORD flags)
2784 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2785 struct d3d8_vertexbuffer *dst = unsafe_impl_from_IDirect3DVertexBuffer8(dst_buffer);
2786 const struct wined3d_stateblock_state *state;
2787 const struct wined3d_stream_state *stream;
2788 struct d3d8_vertexbuffer *d3d8_buffer;
2789 unsigned int i, map;
2790 HRESULT hr;
2792 TRACE("iface %p, src_start_idx %u, dst_idx %u, vertex_count %u, dst_buffer %p, flags %#x.\n",
2793 iface, src_start_idx, dst_idx, vertex_count, dst_buffer, flags);
2795 wined3d_mutex_lock();
2797 state = device->stateblock_state;
2799 /* Note that an alternative approach would be to simply create these
2800 * buffers with WINED3D_RESOURCE_ACCESS_MAP_R and update them here like we
2801 * do for draws. In some regards that would be easier, but it seems less
2802 * than optimal to upload data to the GPU only to subsequently download it
2803 * again. */
2804 map = device->sysmem_vb;
2805 while (map)
2807 i = wined3d_bit_scan(&map);
2808 stream = &state->streams[i];
2810 d3d8_buffer = wined3d_buffer_get_parent(stream->buffer);
2811 if (FAILED(wined3d_stateblock_set_stream_source(device->state,
2812 i, d3d8_buffer->wined3d_buffer, stream->offset, stream->stride)))
2813 ERR("Failed to set stream source.\n");
2816 wined3d_device_apply_stateblock(device->wined3d_device, device->state);
2817 hr = wined3d_device_process_vertices(device->wined3d_device, src_start_idx, dst_idx,
2818 vertex_count, dst->wined3d_buffer, NULL, flags, dst->fvf);
2820 map = device->sysmem_vb;
2821 while (map)
2823 i = wined3d_bit_scan(&map);
2824 stream = &state->streams[i];
2826 d3d8_buffer = wined3d_buffer_get_parent(stream->buffer);
2827 if (FAILED(wined3d_stateblock_set_stream_source(device->state,
2828 i, d3d8_buffer->draw_buffer, stream->offset, stream->stride)))
2829 ERR("Failed to set stream source.\n");
2832 wined3d_mutex_unlock();
2834 return hr;
2837 static HRESULT WINAPI d3d8_device_CreateVertexShader(IDirect3DDevice8 *iface,
2838 const DWORD *declaration, const DWORD *byte_code, DWORD *shader, DWORD usage)
2840 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2841 struct d3d8_vertex_shader *object;
2842 DWORD shader_handle;
2843 DWORD handle;
2844 HRESULT hr;
2846 TRACE("iface %p, declaration %p, byte_code %p, shader %p, usage %#x.\n",
2847 iface, declaration, byte_code, shader, usage);
2849 if (!(object = heap_alloc_zero(sizeof(*object))))
2851 *shader = 0;
2852 return E_OUTOFMEMORY;
2855 wined3d_mutex_lock();
2856 handle = d3d8_allocate_handle(&device->handle_table, object, D3D8_HANDLE_VS);
2857 wined3d_mutex_unlock();
2858 if (handle == D3D8_INVALID_HANDLE)
2860 ERR("Failed to allocate vertex shader handle.\n");
2861 heap_free(object);
2862 *shader = 0;
2863 return E_OUTOFMEMORY;
2866 shader_handle = handle + VS_HIGHESTFIXEDFXF + 1;
2868 hr = d3d8_vertex_shader_init(object, device, declaration, byte_code, shader_handle, usage);
2869 if (FAILED(hr))
2871 WARN("Failed to initialize vertex shader, hr %#x.\n", hr);
2872 wined3d_mutex_lock();
2873 d3d8_free_handle(&device->handle_table, handle, D3D8_HANDLE_VS);
2874 wined3d_mutex_unlock();
2875 heap_free(object);
2876 *shader = 0;
2877 return hr;
2880 TRACE("Created vertex shader %p (handle %#x).\n", object, shader_handle);
2881 *shader = shader_handle;
2883 return D3D_OK;
2886 static struct d3d8_vertex_declaration *d3d8_device_get_fvf_declaration(struct d3d8_device *device, DWORD fvf)
2888 struct d3d8_vertex_declaration *d3d8_declaration;
2889 struct FvfToDecl *convertedDecls = device->decls;
2890 int p, low, high; /* deliberately signed */
2891 HRESULT hr;
2893 TRACE("Searching for declaration for fvf %08x... ", fvf);
2895 low = 0;
2896 high = device->numConvertedDecls - 1;
2897 while (low <= high)
2899 p = (low + high) >> 1;
2900 TRACE("%d ", p);
2902 if (convertedDecls[p].fvf == fvf)
2904 TRACE("found %p\n", convertedDecls[p].declaration);
2905 return convertedDecls[p].declaration;
2908 if (convertedDecls[p].fvf < fvf)
2909 low = p + 1;
2910 else
2911 high = p - 1;
2913 TRACE("not found. Creating and inserting at position %d.\n", low);
2915 if (!(d3d8_declaration = heap_alloc(sizeof(*d3d8_declaration))))
2916 return NULL;
2918 if (FAILED(hr = d3d8_vertex_declaration_init_fvf(d3d8_declaration, device, fvf)))
2920 WARN("Failed to initialize vertex declaration, hr %#x.\n", hr);
2921 heap_free(d3d8_declaration);
2922 return NULL;
2925 if (device->declArraySize == device->numConvertedDecls)
2927 UINT grow = device->declArraySize / 2;
2929 if (!(convertedDecls = heap_realloc(convertedDecls,
2930 sizeof(*convertedDecls) * (device->numConvertedDecls + grow))))
2932 d3d8_vertex_declaration_destroy(d3d8_declaration);
2933 return NULL;
2935 device->decls = convertedDecls;
2936 device->declArraySize += grow;
2939 memmove(convertedDecls + low + 1, convertedDecls + low,
2940 sizeof(*convertedDecls) * (device->numConvertedDecls - low));
2941 convertedDecls[low].declaration = d3d8_declaration;
2942 convertedDecls[low].fvf = fvf;
2943 ++device->numConvertedDecls;
2945 TRACE("Returning %p. %u decls in array.\n", d3d8_declaration, device->numConvertedDecls);
2947 return d3d8_declaration;
2950 static HRESULT WINAPI d3d8_device_SetVertexShader(IDirect3DDevice8 *iface, DWORD shader)
2952 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2953 struct d3d8_vertex_shader *shader_impl;
2955 TRACE("iface %p, shader %#x.\n", iface, shader);
2957 if (VS_HIGHESTFIXEDFXF >= shader)
2959 TRACE("Setting FVF, %#x\n", shader);
2961 wined3d_mutex_lock();
2962 wined3d_stateblock_set_vertex_declaration(device->update_state,
2963 d3d8_device_get_fvf_declaration(device, shader)->wined3d_vertex_declaration);
2964 wined3d_stateblock_set_vertex_shader(device->update_state, NULL);
2965 wined3d_mutex_unlock();
2967 return D3D_OK;
2970 TRACE("Setting shader\n");
2972 wined3d_mutex_lock();
2973 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS)))
2975 WARN("Invalid handle (%#x) passed.\n", shader);
2976 wined3d_mutex_unlock();
2978 return D3DERR_INVALIDCALL;
2981 wined3d_stateblock_set_vertex_declaration(device->update_state,
2982 shader_impl->vertex_declaration->wined3d_vertex_declaration);
2983 wined3d_stateblock_set_vertex_shader(device->update_state, shader_impl->wined3d_shader);
2984 wined3d_mutex_unlock();
2986 return D3D_OK;
2989 static HRESULT WINAPI d3d8_device_GetVertexShader(IDirect3DDevice8 *iface, DWORD *shader)
2991 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2992 struct wined3d_vertex_declaration *wined3d_declaration;
2993 struct d3d8_vertex_declaration *d3d8_declaration;
2995 TRACE("iface %p, shader %p.\n", iface, shader);
2997 wined3d_mutex_lock();
2998 if ((wined3d_declaration = device->stateblock_state->vertex_declaration))
3000 d3d8_declaration = wined3d_vertex_declaration_get_parent(wined3d_declaration);
3001 *shader = d3d8_declaration->shader_handle;
3003 else
3005 *shader = 0;
3007 wined3d_mutex_unlock();
3009 TRACE("Returning %#x.\n", *shader);
3011 return D3D_OK;
3014 static HRESULT WINAPI d3d8_device_DeleteVertexShader(IDirect3DDevice8 *iface, DWORD shader)
3016 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3017 struct d3d8_vertex_shader *shader_impl;
3019 TRACE("iface %p, shader %#x.\n", iface, shader);
3021 wined3d_mutex_lock();
3022 if (!(shader_impl = d3d8_free_handle(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS)))
3024 WARN("Invalid handle (%#x) passed.\n", shader);
3025 wined3d_mutex_unlock();
3027 return D3DERR_INVALIDCALL;
3030 if (shader_impl->wined3d_shader
3031 && device->stateblock_state->vs == shader_impl->wined3d_shader)
3032 IDirect3DDevice8_SetVertexShader(iface, 0);
3034 wined3d_mutex_unlock();
3036 d3d8_vertex_shader_destroy(shader_impl);
3038 return D3D_OK;
3041 static HRESULT WINAPI d3d8_device_SetVertexShaderConstant(IDirect3DDevice8 *iface,
3042 DWORD start_register, const void *data, DWORD count)
3044 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3045 HRESULT hr;
3047 TRACE("iface %p, start_register %u, data %p, count %u.\n",
3048 iface, start_register, data, count);
3050 if (start_register + count > D3D8_MAX_VERTEX_SHADER_CONSTANTF)
3052 WARN("Trying to access %u constants, but d3d8 only supports %u\n",
3053 start_register + count, D3D8_MAX_VERTEX_SHADER_CONSTANTF);
3054 return D3DERR_INVALIDCALL;
3057 wined3d_mutex_lock();
3058 hr = wined3d_stateblock_set_vs_consts_f(device->update_state, start_register, count, data);
3059 wined3d_mutex_unlock();
3061 return hr;
3064 static HRESULT WINAPI d3d8_device_GetVertexShaderConstant(IDirect3DDevice8 *iface,
3065 DWORD start_idx, void *constants, DWORD count)
3067 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3068 const struct wined3d_vec4 *src;
3070 TRACE("iface %p, start_idx %u, constants %p, count %u.\n", iface, start_idx, constants, count);
3072 if (!constants)
3073 return D3DERR_INVALIDCALL;
3075 if (!wined3d_bound_range(start_idx, count, device->vs_uniform_count))
3077 WARN("Trying to access %u constants, but d3d8 only supports %u.\n",
3078 start_idx + count, device->vs_uniform_count);
3079 return D3DERR_INVALIDCALL;
3082 wined3d_mutex_lock();
3083 src = device->stateblock_state->vs_consts_f;
3084 memcpy(constants, &src[start_idx], count * sizeof(*src));
3085 wined3d_mutex_unlock();
3087 return D3D_OK;
3090 static HRESULT WINAPI d3d8_device_GetVertexShaderDeclaration(IDirect3DDevice8 *iface,
3091 DWORD shader, void *data, DWORD *data_size)
3093 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3094 struct d3d8_vertex_declaration *declaration;
3095 struct d3d8_vertex_shader *shader_impl;
3097 TRACE("iface %p, shader %#x, data %p, data_size %p.\n",
3098 iface, shader, data, data_size);
3100 wined3d_mutex_lock();
3101 shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS);
3102 wined3d_mutex_unlock();
3104 if (!shader_impl)
3106 WARN("Invalid handle (%#x) passed.\n", shader);
3107 return D3DERR_INVALIDCALL;
3109 declaration = shader_impl->vertex_declaration;
3111 if (!data)
3113 *data_size = declaration->elements_size;
3114 return D3D_OK;
3117 /* MSDN claims that if *data_size is smaller than the required size
3118 * we should write the required size and return D3DERR_MOREDATA.
3119 * That's not actually true. */
3120 if (*data_size < declaration->elements_size)
3121 return D3DERR_INVALIDCALL;
3123 memcpy(data, declaration->elements, declaration->elements_size);
3125 return D3D_OK;
3128 static HRESULT WINAPI d3d8_device_GetVertexShaderFunction(IDirect3DDevice8 *iface,
3129 DWORD shader, void *data, DWORD *data_size)
3131 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3132 struct d3d8_vertex_shader *shader_impl = NULL;
3133 HRESULT hr;
3135 TRACE("iface %p, shader %#x, data %p, data_size %p.\n",
3136 iface, shader, data, data_size);
3138 wined3d_mutex_lock();
3139 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS)))
3141 WARN("Invalid handle (%#x) passed.\n", shader);
3142 wined3d_mutex_unlock();
3144 return D3DERR_INVALIDCALL;
3147 if (!shader_impl->wined3d_shader)
3149 wined3d_mutex_unlock();
3150 *data_size = 0;
3151 return D3D_OK;
3154 hr = wined3d_shader_get_byte_code(shader_impl->wined3d_shader, data, data_size);
3155 wined3d_mutex_unlock();
3157 return hr;
3160 static HRESULT WINAPI d3d8_device_SetIndices(IDirect3DDevice8 *iface,
3161 IDirect3DIndexBuffer8 *buffer, UINT base_vertex_idx)
3163 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3164 struct d3d8_indexbuffer *ib = unsafe_impl_from_IDirect3DIndexBuffer8(buffer);
3165 struct wined3d_buffer *wined3d_buffer;
3167 TRACE("iface %p, buffer %p, base_vertex_idx %u.\n", iface, buffer, base_vertex_idx);
3169 if (!ib)
3170 wined3d_buffer = NULL;
3171 else if (ib->draw_buffer)
3172 wined3d_buffer = ib->draw_buffer;
3173 else
3174 wined3d_buffer = ib->wined3d_buffer;
3176 /* WineD3D takes an INT(due to d3d9), but d3d8 uses UINTs. Do I have to add a check here that
3177 * the UINT doesn't cause an overflow in the INT? It seems rather unlikely because such large
3178 * vertex buffers can't be created to address them with an index that requires the 32nd bit
3179 * (4 Byte minimum vertex size * 2^31-1 -> 8 gb buffer. The index sign would be the least
3180 * problem)
3182 wined3d_mutex_lock();
3183 wined3d_stateblock_set_base_vertex_index(device->update_state, base_vertex_idx);
3184 wined3d_stateblock_set_index_buffer(device->update_state, wined3d_buffer, ib ? ib->format : WINED3DFMT_UNKNOWN);
3185 if (!device->recording)
3186 device->sysmem_ib = ib && ib->draw_buffer;
3187 wined3d_mutex_unlock();
3189 return D3D_OK;
3192 static HRESULT WINAPI d3d8_device_GetIndices(IDirect3DDevice8 *iface,
3193 IDirect3DIndexBuffer8 **buffer, UINT *base_vertex_index)
3195 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3196 const struct wined3d_stateblock_state *state;
3197 struct wined3d_buffer *wined3d_buffer;
3198 struct d3d8_indexbuffer *buffer_impl;
3200 TRACE("iface %p, buffer %p, base_vertex_index %p.\n", iface, buffer, base_vertex_index);
3202 if (!buffer)
3203 return D3DERR_INVALIDCALL;
3205 /* The case from UINT to INT is safe because d3d8 will never set negative values */
3206 wined3d_mutex_lock();
3207 state = device->stateblock_state;
3208 *base_vertex_index = state->base_vertex_index;
3209 if ((wined3d_buffer = state->index_buffer))
3211 buffer_impl = wined3d_buffer_get_parent(wined3d_buffer);
3212 *buffer = &buffer_impl->IDirect3DIndexBuffer8_iface;
3213 IDirect3DIndexBuffer8_AddRef(*buffer);
3215 else
3217 *buffer = NULL;
3219 wined3d_mutex_unlock();
3221 return D3D_OK;
3224 static HRESULT WINAPI d3d8_device_CreatePixelShader(IDirect3DDevice8 *iface,
3225 const DWORD *byte_code, DWORD *shader)
3227 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3228 struct d3d8_pixel_shader *object;
3229 DWORD shader_handle;
3230 DWORD handle;
3231 HRESULT hr;
3233 TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
3235 if (!shader)
3236 return D3DERR_INVALIDCALL;
3238 if (!(object = heap_alloc_zero(sizeof(*object))))
3239 return E_OUTOFMEMORY;
3241 wined3d_mutex_lock();
3242 handle = d3d8_allocate_handle(&device->handle_table, object, D3D8_HANDLE_PS);
3243 wined3d_mutex_unlock();
3244 if (handle == D3D8_INVALID_HANDLE)
3246 ERR("Failed to allocate pixel shader handle.\n");
3247 heap_free(object);
3248 return E_OUTOFMEMORY;
3251 shader_handle = handle + VS_HIGHESTFIXEDFXF + 1;
3253 hr = d3d8_pixel_shader_init(object, device, byte_code, shader_handle);
3254 if (FAILED(hr))
3256 WARN("Failed to initialize pixel shader, hr %#x.\n", hr);
3257 wined3d_mutex_lock();
3258 d3d8_free_handle(&device->handle_table, handle, D3D8_HANDLE_PS);
3259 wined3d_mutex_unlock();
3260 heap_free(object);
3261 *shader = 0;
3262 return hr;
3265 TRACE("Created pixel shader %p (handle %#x).\n", object, shader_handle);
3266 *shader = shader_handle;
3268 return D3D_OK;
3271 static HRESULT WINAPI d3d8_device_SetPixelShader(IDirect3DDevice8 *iface, DWORD shader)
3273 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3274 struct d3d8_pixel_shader *shader_impl;
3276 TRACE("iface %p, shader %#x.\n", iface, shader);
3278 wined3d_mutex_lock();
3280 if (!shader)
3282 wined3d_stateblock_set_pixel_shader(device->update_state, NULL);
3283 wined3d_mutex_unlock();
3284 return D3D_OK;
3287 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS)))
3289 WARN("Invalid handle (%#x) passed.\n", shader);
3290 wined3d_mutex_unlock();
3291 return D3DERR_INVALIDCALL;
3294 TRACE("Setting shader %p.\n", shader_impl);
3295 wined3d_stateblock_set_pixel_shader(device->update_state, shader_impl->wined3d_shader);
3296 wined3d_mutex_unlock();
3298 return D3D_OK;
3301 static HRESULT WINAPI d3d8_device_GetPixelShader(IDirect3DDevice8 *iface, DWORD *shader)
3303 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3304 struct wined3d_shader *object;
3306 TRACE("iface %p, shader %p.\n", iface, shader);
3308 if (!shader)
3309 return D3DERR_INVALIDCALL;
3311 wined3d_mutex_lock();
3312 if ((object = device->stateblock_state->ps))
3314 struct d3d8_pixel_shader *d3d8_shader;
3315 d3d8_shader = wined3d_shader_get_parent(object);
3316 *shader = d3d8_shader->handle;
3318 else
3320 *shader = 0;
3322 wined3d_mutex_unlock();
3324 TRACE("Returning %#x.\n", *shader);
3326 return D3D_OK;
3329 static HRESULT WINAPI d3d8_device_DeletePixelShader(IDirect3DDevice8 *iface, DWORD shader)
3331 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3332 struct d3d8_pixel_shader *shader_impl;
3334 TRACE("iface %p, shader %#x.\n", iface, shader);
3336 wined3d_mutex_lock();
3338 if (!(shader_impl = d3d8_free_handle(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS)))
3340 WARN("Invalid handle (%#x) passed.\n", shader);
3341 wined3d_mutex_unlock();
3342 return D3DERR_INVALIDCALL;
3345 if (device->stateblock_state->ps == shader_impl->wined3d_shader)
3346 IDirect3DDevice8_SetPixelShader(iface, 0);
3348 wined3d_mutex_unlock();
3350 d3d8_pixel_shader_destroy(shader_impl);
3352 return D3D_OK;
3355 static HRESULT WINAPI d3d8_device_SetPixelShaderConstant(IDirect3DDevice8 *iface,
3356 DWORD start_register, const void *data, DWORD count)
3358 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3359 HRESULT hr;
3361 TRACE("iface %p, start_register %u, data %p, count %u.\n",
3362 iface, start_register, data, count);
3364 wined3d_mutex_lock();
3365 hr = wined3d_stateblock_set_ps_consts_f(device->update_state, start_register, count, data);
3366 wined3d_mutex_unlock();
3368 return hr;
3371 static HRESULT WINAPI d3d8_device_GetPixelShaderConstant(IDirect3DDevice8 *iface,
3372 DWORD start_idx, void *constants, DWORD count)
3374 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3375 const struct wined3d_vec4 *src;
3377 TRACE("iface %p, start_idx %u, constants %p, count %u.\n", iface, start_idx, constants, count);
3379 if (!constants || !wined3d_bound_range(start_idx, count, D3D8_MAX_PIXEL_SHADER_CONSTANTF))
3380 return WINED3DERR_INVALIDCALL;
3382 wined3d_mutex_lock();
3383 src = device->stateblock_state->ps_consts_f;
3384 memcpy(constants, &src[start_idx], count * sizeof(*src));
3385 wined3d_mutex_unlock();
3387 return D3D_OK;
3390 static HRESULT WINAPI d3d8_device_GetPixelShaderFunction(IDirect3DDevice8 *iface,
3391 DWORD shader, void *data, DWORD *data_size)
3393 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3394 struct d3d8_pixel_shader *shader_impl = NULL;
3395 HRESULT hr;
3397 TRACE("iface %p, shader %#x, data %p, data_size %p.\n",
3398 iface, shader, data, data_size);
3400 wined3d_mutex_lock();
3401 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS)))
3403 WARN("Invalid handle (%#x) passed.\n", shader);
3404 wined3d_mutex_unlock();
3406 return D3DERR_INVALIDCALL;
3409 hr = wined3d_shader_get_byte_code(shader_impl->wined3d_shader, data, data_size);
3410 wined3d_mutex_unlock();
3412 return hr;
3415 static HRESULT WINAPI d3d8_device_DrawRectPatch(IDirect3DDevice8 *iface, UINT handle,
3416 const float *segment_count, const D3DRECTPATCH_INFO *patch_info)
3418 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
3419 iface, handle, segment_count, patch_info);
3420 return D3D_OK;
3423 static HRESULT WINAPI d3d8_device_DrawTriPatch(IDirect3DDevice8 *iface, UINT handle,
3424 const float *segment_count, const D3DTRIPATCH_INFO *patch_info)
3426 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
3427 iface, handle, segment_count, patch_info);
3428 return D3D_OK;
3431 static HRESULT WINAPI d3d8_device_DeletePatch(IDirect3DDevice8 *iface, UINT handle)
3433 FIXME("iface %p, handle %#x unimplemented.\n", iface, handle);
3434 return D3DERR_INVALIDCALL;
3437 static HRESULT WINAPI d3d8_device_SetStreamSource(IDirect3DDevice8 *iface,
3438 UINT stream_idx, IDirect3DVertexBuffer8 *buffer, UINT stride)
3440 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3441 struct d3d8_vertexbuffer *buffer_impl = unsafe_impl_from_IDirect3DVertexBuffer8(buffer);
3442 const struct wined3d_stateblock_state *state;
3443 struct wined3d_buffer *wined3d_buffer;
3444 HRESULT hr;
3446 TRACE("iface %p, stream_idx %u, buffer %p, stride %u.\n",
3447 iface, stream_idx, buffer, stride);
3449 if (stream_idx >= ARRAY_SIZE(state->streams))
3451 WARN("Stream index %u out of range.\n", stream_idx);
3452 return D3DERR_INVALIDCALL;
3455 wined3d_mutex_lock();
3457 if (!buffer_impl)
3459 wined3d_buffer = NULL;
3460 state = device->stateblock_state;
3461 stride = state->streams[stream_idx].stride;
3463 else if (buffer_impl->draw_buffer)
3464 wined3d_buffer = buffer_impl->draw_buffer;
3465 else
3466 wined3d_buffer = buffer_impl->wined3d_buffer;
3468 hr = wined3d_stateblock_set_stream_source(device->update_state, stream_idx, wined3d_buffer, 0, stride);
3469 if (SUCCEEDED(hr) && !device->recording)
3471 if (buffer_impl && buffer_impl->draw_buffer)
3472 device->sysmem_vb |= (1u << stream_idx);
3473 else
3474 device->sysmem_vb &= ~(1u << stream_idx);
3477 wined3d_mutex_unlock();
3479 return hr;
3482 static HRESULT WINAPI d3d8_device_GetStreamSource(IDirect3DDevice8 *iface,
3483 UINT stream_idx, IDirect3DVertexBuffer8 **buffer, UINT *stride)
3485 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3486 const struct wined3d_stateblock_state *state;
3487 const struct wined3d_stream_state *stream;
3488 struct d3d8_vertexbuffer *buffer_impl;
3490 TRACE("iface %p, stream_idx %u, buffer %p, stride %p.\n",
3491 iface, stream_idx, buffer, stride);
3493 if (!buffer)
3494 return D3DERR_INVALIDCALL;
3496 if (stream_idx >= ARRAY_SIZE(state->streams))
3498 WARN("Stream index %u out of range.\n", stream_idx);
3499 return D3DERR_INVALIDCALL;
3502 wined3d_mutex_lock();
3503 state = device->stateblock_state;
3504 stream = &state->streams[stream_idx];
3505 if (stream->buffer)
3507 buffer_impl = wined3d_buffer_get_parent(stream->buffer);
3508 *buffer = &buffer_impl->IDirect3DVertexBuffer8_iface;
3509 IDirect3DVertexBuffer8_AddRef(*buffer);
3511 else
3512 *buffer = NULL;
3513 *stride = stream->stride;
3514 wined3d_mutex_unlock();
3516 return D3D_OK;
3519 static const struct IDirect3DDevice8Vtbl d3d8_device_vtbl =
3521 d3d8_device_QueryInterface,
3522 d3d8_device_AddRef,
3523 d3d8_device_Release,
3524 d3d8_device_TestCooperativeLevel,
3525 d3d8_device_GetAvailableTextureMem,
3526 d3d8_device_ResourceManagerDiscardBytes,
3527 d3d8_device_GetDirect3D,
3528 d3d8_device_GetDeviceCaps,
3529 d3d8_device_GetDisplayMode,
3530 d3d8_device_GetCreationParameters,
3531 d3d8_device_SetCursorProperties,
3532 d3d8_device_SetCursorPosition,
3533 d3d8_device_ShowCursor,
3534 d3d8_device_CreateAdditionalSwapChain,
3535 d3d8_device_Reset,
3536 d3d8_device_Present,
3537 d3d8_device_GetBackBuffer,
3538 d3d8_device_GetRasterStatus,
3539 d3d8_device_SetGammaRamp,
3540 d3d8_device_GetGammaRamp,
3541 d3d8_device_CreateTexture,
3542 d3d8_device_CreateVolumeTexture,
3543 d3d8_device_CreateCubeTexture,
3544 d3d8_device_CreateVertexBuffer,
3545 d3d8_device_CreateIndexBuffer,
3546 d3d8_device_CreateRenderTarget,
3547 d3d8_device_CreateDepthStencilSurface,
3548 d3d8_device_CreateImageSurface,
3549 d3d8_device_CopyRects,
3550 d3d8_device_UpdateTexture,
3551 d3d8_device_GetFrontBuffer,
3552 d3d8_device_SetRenderTarget,
3553 d3d8_device_GetRenderTarget,
3554 d3d8_device_GetDepthStencilSurface,
3555 d3d8_device_BeginScene,
3556 d3d8_device_EndScene,
3557 d3d8_device_Clear,
3558 d3d8_device_SetTransform,
3559 d3d8_device_GetTransform,
3560 d3d8_device_MultiplyTransform,
3561 d3d8_device_SetViewport,
3562 d3d8_device_GetViewport,
3563 d3d8_device_SetMaterial,
3564 d3d8_device_GetMaterial,
3565 d3d8_device_SetLight,
3566 d3d8_device_GetLight,
3567 d3d8_device_LightEnable,
3568 d3d8_device_GetLightEnable,
3569 d3d8_device_SetClipPlane,
3570 d3d8_device_GetClipPlane,
3571 d3d8_device_SetRenderState,
3572 d3d8_device_GetRenderState,
3573 d3d8_device_BeginStateBlock,
3574 d3d8_device_EndStateBlock,
3575 d3d8_device_ApplyStateBlock,
3576 d3d8_device_CaptureStateBlock,
3577 d3d8_device_DeleteStateBlock,
3578 d3d8_device_CreateStateBlock,
3579 d3d8_device_SetClipStatus,
3580 d3d8_device_GetClipStatus,
3581 d3d8_device_GetTexture,
3582 d3d8_device_SetTexture,
3583 d3d8_device_GetTextureStageState,
3584 d3d8_device_SetTextureStageState,
3585 d3d8_device_ValidateDevice,
3586 d3d8_device_GetInfo,
3587 d3d8_device_SetPaletteEntries,
3588 d3d8_device_GetPaletteEntries,
3589 d3d8_device_SetCurrentTexturePalette,
3590 d3d8_device_GetCurrentTexturePalette,
3591 d3d8_device_DrawPrimitive,
3592 d3d8_device_DrawIndexedPrimitive,
3593 d3d8_device_DrawPrimitiveUP,
3594 d3d8_device_DrawIndexedPrimitiveUP,
3595 d3d8_device_ProcessVertices,
3596 d3d8_device_CreateVertexShader,
3597 d3d8_device_SetVertexShader,
3598 d3d8_device_GetVertexShader,
3599 d3d8_device_DeleteVertexShader,
3600 d3d8_device_SetVertexShaderConstant,
3601 d3d8_device_GetVertexShaderConstant,
3602 d3d8_device_GetVertexShaderDeclaration,
3603 d3d8_device_GetVertexShaderFunction,
3604 d3d8_device_SetStreamSource,
3605 d3d8_device_GetStreamSource,
3606 d3d8_device_SetIndices,
3607 d3d8_device_GetIndices,
3608 d3d8_device_CreatePixelShader,
3609 d3d8_device_SetPixelShader,
3610 d3d8_device_GetPixelShader,
3611 d3d8_device_DeletePixelShader,
3612 d3d8_device_SetPixelShaderConstant,
3613 d3d8_device_GetPixelShaderConstant,
3614 d3d8_device_GetPixelShaderFunction,
3615 d3d8_device_DrawRectPatch,
3616 d3d8_device_DrawTriPatch,
3617 d3d8_device_DeletePatch,
3620 static inline struct d3d8_device *device_from_device_parent(struct wined3d_device_parent *device_parent)
3622 return CONTAINING_RECORD(device_parent, struct d3d8_device, device_parent);
3625 static void CDECL device_parent_wined3d_device_created(struct wined3d_device_parent *device_parent,
3626 struct wined3d_device *device)
3628 TRACE("device_parent %p, device %p\n", device_parent, device);
3631 static void CDECL device_parent_mode_changed(struct wined3d_device_parent *device_parent)
3633 TRACE("device_parent %p.\n", device_parent);
3636 static void CDECL device_parent_activate(struct wined3d_device_parent *device_parent, BOOL activate)
3638 struct d3d8_device *device = device_from_device_parent(device_parent);
3640 TRACE("device_parent %p, activate %#x.\n", device_parent, activate);
3642 if (!activate)
3643 InterlockedCompareExchange(&device->device_state, D3D8_DEVICE_STATE_LOST, D3D8_DEVICE_STATE_OK);
3644 else
3645 InterlockedCompareExchange(&device->device_state, D3D8_DEVICE_STATE_NOT_RESET, D3D8_DEVICE_STATE_LOST);
3648 static HRESULT CDECL device_parent_texture_sub_resource_created(struct wined3d_device_parent *device_parent,
3649 enum wined3d_resource_type type, struct wined3d_texture *wined3d_texture, unsigned int sub_resource_idx,
3650 void **parent, const struct wined3d_parent_ops **parent_ops)
3652 TRACE("device_parent %p, type %#x, wined3d_texture %p, sub_resource_idx %u, parent %p, parent_ops %p.\n",
3653 device_parent, type, wined3d_texture, sub_resource_idx, parent, parent_ops);
3655 if (type == WINED3D_RTYPE_TEXTURE_2D)
3657 struct d3d8_surface *d3d_surface;
3659 if (!(d3d_surface = heap_alloc_zero(sizeof(*d3d_surface))))
3660 return E_OUTOFMEMORY;
3662 surface_init(d3d_surface, wined3d_texture, sub_resource_idx, parent_ops);
3663 *parent = d3d_surface;
3664 TRACE("Created surface %p.\n", d3d_surface);
3666 else if (type == WINED3D_RTYPE_TEXTURE_3D)
3668 struct d3d8_volume *d3d_volume;
3670 if (!(d3d_volume = heap_alloc_zero(sizeof(*d3d_volume))))
3671 return E_OUTOFMEMORY;
3673 volume_init(d3d_volume, wined3d_texture, sub_resource_idx, parent_ops);
3674 *parent = d3d_volume;
3675 TRACE("Created volume %p.\n", d3d_volume);
3677 else
3679 ERR("Unhandled resource type %#x.\n", type);
3680 return E_FAIL;
3683 return D3D_OK;
3686 static HRESULT CDECL device_parent_create_swapchain_texture(struct wined3d_device_parent *device_parent,
3687 void *container_parent, const struct wined3d_resource_desc *desc, DWORD texture_flags,
3688 struct wined3d_texture **texture)
3690 struct d3d8_device *device = device_from_device_parent(device_parent);
3691 struct d3d8_surface *d3d_surface;
3692 HRESULT hr;
3694 TRACE("device_parent %p, container_parent %p, desc %p, texture flags %#x, texture %p.\n",
3695 device_parent, container_parent, desc, texture_flags, texture);
3697 if (FAILED(hr = wined3d_texture_create(device->wined3d_device, desc, 1, 1, texture_flags,
3698 NULL, &device->IDirect3DDevice8_iface, &d3d8_null_wined3d_parent_ops, texture)))
3700 WARN("Failed to create texture, hr %#x.\n", hr);
3701 return hr;
3704 d3d_surface = wined3d_texture_get_sub_resource_parent(*texture, 0);
3705 d3d_surface->parent_device = &device->IDirect3DDevice8_iface;
3707 return hr;
3710 static const struct wined3d_device_parent_ops d3d8_wined3d_device_parent_ops =
3712 device_parent_wined3d_device_created,
3713 device_parent_mode_changed,
3714 device_parent_activate,
3715 device_parent_texture_sub_resource_created,
3716 device_parent_create_swapchain_texture,
3719 static void setup_fpu(void)
3721 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
3722 WORD cw;
3723 __asm__ volatile ("fnstcw %0" : "=m" (cw));
3724 cw = (cw & ~0xf3f) | 0x3f;
3725 __asm__ volatile ("fldcw %0" : : "m" (cw));
3726 #elif defined(__i386__) && defined(_MSC_VER)
3727 WORD cw;
3728 __asm fnstcw cw;
3729 cw = (cw & ~0xf3f) | 0x3f;
3730 __asm fldcw cw;
3731 #else
3732 FIXME("FPU setup not implemented for this platform.\n");
3733 #endif
3736 HRESULT device_init(struct d3d8_device *device, struct d3d8 *parent, struct wined3d *wined3d, UINT adapter,
3737 D3DDEVTYPE device_type, HWND focus_window, DWORD flags, D3DPRESENT_PARAMETERS *parameters)
3739 struct wined3d_swapchain_desc swapchain_desc;
3740 struct wined3d_swapchain *wined3d_swapchain;
3741 struct wined3d_adapter *wined3d_adapter;
3742 struct wined3d_output *wined3d_output;
3743 struct d3d8_swapchain *d3d_swapchain;
3744 struct wined3d_caps caps;
3745 unsigned int output_idx;
3746 HRESULT hr;
3748 static const enum wined3d_feature_level feature_levels[] =
3750 WINED3D_FEATURE_LEVEL_8,
3751 WINED3D_FEATURE_LEVEL_7,
3752 WINED3D_FEATURE_LEVEL_6,
3753 WINED3D_FEATURE_LEVEL_5,
3756 output_idx = adapter;
3757 if (output_idx >= parent->wined3d_output_count)
3758 return D3DERR_INVALIDCALL;
3760 device->IDirect3DDevice8_iface.lpVtbl = &d3d8_device_vtbl;
3761 device->device_parent.ops = &d3d8_wined3d_device_parent_ops;
3762 device->adapter_ordinal = adapter;
3763 device->ref = 1;
3764 if (!(device->handle_table.entries = heap_alloc_zero(D3D8_INITIAL_HANDLE_TABLE_SIZE
3765 * sizeof(*device->handle_table.entries))))
3767 ERR("Failed to allocate handle table memory.\n");
3768 return E_OUTOFMEMORY;
3770 device->handle_table.table_size = D3D8_INITIAL_HANDLE_TABLE_SIZE;
3772 if (!(flags & D3DCREATE_FPU_PRESERVE)) setup_fpu();
3774 wined3d_mutex_lock();
3775 wined3d_output = parent->wined3d_outputs[output_idx];
3776 wined3d_adapter = wined3d_output_get_adapter(wined3d_output);
3777 if (FAILED(hr = wined3d_device_create(wined3d, wined3d_adapter, wined3d_device_type_from_d3d(device_type),
3778 focus_window, flags, 4, feature_levels, ARRAY_SIZE(feature_levels),
3779 &device->device_parent, &device->wined3d_device)))
3781 WARN("Failed to create wined3d device, hr %#x.\n", hr);
3782 wined3d_mutex_unlock();
3783 heap_free(device->handle_table.entries);
3784 return hr;
3787 device->immediate_context = wined3d_device_get_immediate_context(device->wined3d_device);
3788 wined3d_get_device_caps(wined3d_adapter, wined3d_device_type_from_d3d(device_type), &caps);
3789 device->max_user_clip_planes = caps.MaxUserClipPlanes;
3790 device->vs_uniform_count = caps.MaxVertexShaderConst;
3792 if (FAILED(hr = wined3d_stateblock_create(device->wined3d_device, NULL, WINED3D_SBT_PRIMARY, &device->state)))
3794 ERR("Failed to create primary stateblock, hr %#x.\n", hr);
3795 wined3d_device_decref(device->wined3d_device);
3796 wined3d_mutex_unlock();
3797 return hr;
3799 device->stateblock_state = wined3d_stateblock_get_state(device->state);
3800 device->update_state = device->state;
3802 if (!parameters->Windowed)
3804 if (!focus_window)
3805 focus_window = parameters->hDeviceWindow;
3806 if (FAILED(hr = wined3d_device_acquire_focus_window(device->wined3d_device, focus_window)))
3808 ERR("Failed to acquire focus window, hr %#x.\n", hr);
3809 wined3d_device_decref(device->wined3d_device);
3810 wined3d_mutex_unlock();
3811 heap_free(device->handle_table.entries);
3812 return hr;
3816 if (flags & D3DCREATE_MULTITHREADED)
3817 wined3d_device_set_multithreaded(device->wined3d_device);
3819 if (!wined3d_swapchain_desc_from_d3d8(&swapchain_desc, wined3d_output, parameters))
3821 wined3d_device_release_focus_window(device->wined3d_device);
3822 wined3d_device_decref(device->wined3d_device);
3823 wined3d_mutex_unlock();
3824 heap_free(device->handle_table.entries);
3825 return D3DERR_INVALIDCALL;
3827 swapchain_desc.flags |= WINED3D_SWAPCHAIN_IMPLICIT;
3829 if (FAILED(hr = d3d8_swapchain_create(device, &swapchain_desc,
3830 wined3dswapinterval_from_d3d(parameters->FullScreen_PresentationInterval), &d3d_swapchain)))
3832 WARN("Failed to create implicit swapchain, hr %#x.\n", hr);
3833 wined3d_device_release_focus_window(device->wined3d_device);
3834 wined3d_device_decref(device->wined3d_device);
3835 wined3d_mutex_unlock();
3836 heap_free(device->handle_table.entries);
3837 return hr;
3840 wined3d_swapchain = d3d_swapchain->wined3d_swapchain;
3841 wined3d_swapchain_incref(wined3d_swapchain);
3842 IDirect3DSwapChain8_Release(&d3d_swapchain->IDirect3DSwapChain8_iface);
3844 wined3d_stateblock_set_render_state(device->state, WINED3D_RS_ZENABLE,
3845 !!swapchain_desc.enable_auto_depth_stencil);
3846 wined3d_stateblock_set_render_state(device->state, WINED3D_RS_POINTSIZE_MIN, 0);
3847 device_reset_viewport_state(device);
3848 wined3d_mutex_unlock();
3850 present_parameters_from_wined3d_swapchain_desc(parameters,
3851 &swapchain_desc, parameters->FullScreen_PresentationInterval);
3853 device->declArraySize = 16;
3854 if (!(device->decls = heap_alloc(device->declArraySize * sizeof(*device->decls))))
3856 ERR("Failed to allocate FVF vertex declaration map memory.\n");
3857 hr = E_OUTOFMEMORY;
3858 goto err;
3861 device->implicit_swapchain = wined3d_swapchain;
3863 device->d3d_parent = parent;
3864 IDirect3D8_AddRef(&parent->IDirect3D8_iface);
3866 return D3D_OK;
3868 err:
3869 wined3d_mutex_lock();
3870 wined3d_swapchain_decref(wined3d_swapchain);
3871 wined3d_device_release_focus_window(device->wined3d_device);
3872 wined3d_device_decref(device->wined3d_device);
3873 wined3d_mutex_unlock();
3874 heap_free(device->handle_table.entries);
3875 return hr;