d3d8: Store a struct d3d8 in struct d3d8_device.
[wine.git] / dlls / d3d8 / device.c
blobd3a88acc35d9ea59891dd7c6fbdd35bd15fc87f4
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_present_parameters(struct wined3d_swapchain_desc *swapchain_desc,
266 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->backbuffer_width = present_parameters->BackBufferWidth;
297 swapchain_desc->backbuffer_height = present_parameters->BackBufferHeight;
298 swapchain_desc->backbuffer_format = wined3dformat_from_d3dformat(present_parameters->BackBufferFormat);
299 swapchain_desc->backbuffer_count = max(1, present_parameters->BackBufferCount);
300 swapchain_desc->backbuffer_bind_flags = WINED3D_BIND_RENDER_TARGET;
301 swapchain_desc->multisample_type = wined3d_multisample_type_from_d3d(present_parameters->MultiSampleType);
302 swapchain_desc->multisample_quality = 0; /* d3d9 only */
303 swapchain_desc->swap_effect = wined3dswapeffect_from_d3dswapeffect(present_parameters->SwapEffect);
304 swapchain_desc->device_window = present_parameters->hDeviceWindow;
305 swapchain_desc->windowed = present_parameters->Windowed;
306 swapchain_desc->enable_auto_depth_stencil = present_parameters->EnableAutoDepthStencil;
307 swapchain_desc->auto_depth_stencil_format
308 = wined3dformat_from_d3dformat(present_parameters->AutoDepthStencilFormat);
309 swapchain_desc->flags
310 = (present_parameters->Flags & D3DPRESENTFLAGS_MASK) | WINED3D_SWAPCHAIN_ALLOW_MODE_SWITCH;
311 swapchain_desc->refresh_rate = present_parameters->FullScreen_RefreshRateInHz;
312 swapchain_desc->auto_restore_display_mode = TRUE;
314 if (present_parameters->Flags & ~D3DPRESENTFLAGS_MASK)
315 FIXME("Unhandled flags %#x.\n", present_parameters->Flags & ~D3DPRESENTFLAGS_MASK);
317 return TRUE;
320 void d3dcaps_from_wined3dcaps(D3DCAPS8 *caps, const struct wined3d_caps *wined3d_caps,
321 unsigned int adapter_ordinal)
323 caps->DeviceType = (D3DDEVTYPE)wined3d_caps->DeviceType;
324 caps->AdapterOrdinal = adapter_ordinal;
325 caps->Caps = wined3d_caps->Caps;
326 caps->Caps2 = wined3d_caps->Caps2;
327 caps->Caps3 = wined3d_caps->Caps3;
328 caps->PresentationIntervals = D3DPRESENT_INTERVAL_IMMEDIATE | D3DPRESENT_INTERVAL_ONE;
329 caps->CursorCaps = wined3d_caps->CursorCaps;
330 caps->DevCaps = wined3d_caps->DevCaps;
331 caps->PrimitiveMiscCaps = wined3d_caps->PrimitiveMiscCaps;
332 caps->RasterCaps = wined3d_caps->RasterCaps;
333 caps->ZCmpCaps = wined3d_caps->ZCmpCaps;
334 caps->SrcBlendCaps = wined3d_caps->SrcBlendCaps;
335 caps->DestBlendCaps = wined3d_caps->DestBlendCaps;
336 caps->AlphaCmpCaps = wined3d_caps->AlphaCmpCaps;
337 caps->ShadeCaps = wined3d_caps->ShadeCaps;
338 caps->TextureCaps = wined3d_caps->TextureCaps;
339 caps->TextureFilterCaps = wined3d_caps->TextureFilterCaps;
340 caps->CubeTextureFilterCaps = wined3d_caps->CubeTextureFilterCaps;
341 caps->VolumeTextureFilterCaps = wined3d_caps->VolumeTextureFilterCaps;
342 caps->TextureAddressCaps = wined3d_caps->TextureAddressCaps;
343 caps->VolumeTextureAddressCaps = wined3d_caps->VolumeTextureAddressCaps;
344 caps->LineCaps = wined3d_caps->LineCaps;
345 caps->MaxTextureWidth = wined3d_caps->MaxTextureWidth;
346 caps->MaxTextureHeight = wined3d_caps->MaxTextureHeight;
347 caps->MaxVolumeExtent = wined3d_caps->MaxVolumeExtent;
348 caps->MaxTextureRepeat = wined3d_caps->MaxTextureRepeat;
349 caps->MaxTextureAspectRatio = wined3d_caps->MaxTextureAspectRatio;
350 caps->MaxAnisotropy = wined3d_caps->MaxAnisotropy;
351 caps->MaxVertexW = wined3d_caps->MaxVertexW;
352 caps->GuardBandLeft = wined3d_caps->GuardBandLeft;
353 caps->GuardBandTop = wined3d_caps->GuardBandTop;
354 caps->GuardBandRight = wined3d_caps->GuardBandRight;
355 caps->GuardBandBottom = wined3d_caps->GuardBandBottom;
356 caps->ExtentsAdjust = wined3d_caps->ExtentsAdjust;
357 caps->StencilCaps = wined3d_caps->StencilCaps;
358 caps->FVFCaps = wined3d_caps->FVFCaps;
359 caps->TextureOpCaps = wined3d_caps->TextureOpCaps;
360 caps->MaxTextureBlendStages = wined3d_caps->MaxTextureBlendStages;
361 caps->MaxSimultaneousTextures = wined3d_caps->MaxSimultaneousTextures;
362 caps->VertexProcessingCaps = wined3d_caps->VertexProcessingCaps;
363 caps->MaxActiveLights = wined3d_caps->MaxActiveLights;
364 caps->MaxUserClipPlanes = wined3d_caps->MaxUserClipPlanes;
365 caps->MaxVertexBlendMatrices = wined3d_caps->MaxVertexBlendMatrices;
366 caps->MaxVertexBlendMatrixIndex = wined3d_caps->MaxVertexBlendMatrixIndex;
367 caps->MaxPointSize = wined3d_caps->MaxPointSize;
368 caps->MaxPrimitiveCount = wined3d_caps->MaxPrimitiveCount;
369 caps->MaxVertexIndex = wined3d_caps->MaxVertexIndex;
370 caps->MaxStreams = wined3d_caps->MaxStreams;
371 caps->MaxStreamStride = wined3d_caps->MaxStreamStride;
372 caps->VertexShaderVersion = wined3d_caps->VertexShaderVersion;
373 caps->MaxVertexShaderConst = wined3d_caps->MaxVertexShaderConst;
374 caps->PixelShaderVersion = wined3d_caps->PixelShaderVersion;
375 caps->MaxPixelShaderValue = wined3d_caps->PixelShader1xMaxValue;
377 caps->Caps2 &= D3DCAPS2_CANCALIBRATEGAMMA | D3DCAPS2_CANRENDERWINDOWED
378 | D3DCAPS2_CANMANAGERESOURCE | D3DCAPS2_DYNAMICTEXTURES | D3DCAPS2_FULLSCREENGAMMA
379 | D3DCAPS2_NO2DDURING3DSCENE | D3DCAPS2_RESERVED;
380 caps->Caps3 &= D3DCAPS3_ALPHA_FULLSCREEN_FLIP_OR_DISCARD | D3DCAPS3_RESERVED;
381 caps->PrimitiveMiscCaps &= D3DPMISCCAPS_MASKZ | D3DPMISCCAPS_LINEPATTERNREP
382 | D3DPMISCCAPS_CULLNONE | D3DPMISCCAPS_CULLCW | D3DPMISCCAPS_CULLCCW
383 | D3DPMISCCAPS_COLORWRITEENABLE | D3DPMISCCAPS_CLIPPLANESCALEDPOINTS
384 | D3DPMISCCAPS_CLIPTLVERTS | D3DPMISCCAPS_TSSARGTEMP | D3DPMISCCAPS_BLENDOP
385 | D3DPMISCCAPS_NULLREFERENCE;
386 caps->RasterCaps &= D3DPRASTERCAPS_DITHER | D3DPRASTERCAPS_PAT | D3DPRASTERCAPS_ZTEST
387 | D3DPRASTERCAPS_FOGVERTEX | D3DPRASTERCAPS_FOGTABLE | D3DPRASTERCAPS_ANTIALIASEDGES
388 | D3DPRASTERCAPS_MIPMAPLODBIAS | D3DPRASTERCAPS_ZBUFFERLESSHSR
389 | D3DPRASTERCAPS_FOGRANGE | D3DPRASTERCAPS_ANISOTROPY | D3DPRASTERCAPS_WBUFFER
390 | D3DPRASTERCAPS_WFOG | D3DPRASTERCAPS_ZFOG | D3DPRASTERCAPS_COLORPERSPECTIVE
391 | D3DPRASTERCAPS_STRETCHBLTMULTISAMPLE | WINED3DPRASTERCAPS_DEPTHBIAS;
392 if (caps->RasterCaps & WINED3DPRASTERCAPS_DEPTHBIAS)
393 caps->RasterCaps = (caps->RasterCaps | D3DPRASTERCAPS_ZBIAS) & ~WINED3DPRASTERCAPS_DEPTHBIAS;
394 caps->SrcBlendCaps &= D3DPBLENDCAPS_ZERO | D3DPBLENDCAPS_ONE | D3DPBLENDCAPS_SRCCOLOR
395 | D3DPBLENDCAPS_INVSRCCOLOR | D3DPBLENDCAPS_SRCALPHA | D3DPBLENDCAPS_INVSRCALPHA
396 | D3DPBLENDCAPS_DESTALPHA | D3DPBLENDCAPS_INVDESTALPHA | D3DPBLENDCAPS_DESTCOLOR
397 | D3DPBLENDCAPS_INVDESTCOLOR | D3DPBLENDCAPS_SRCALPHASAT | D3DPBLENDCAPS_BOTHSRCALPHA
398 | D3DPBLENDCAPS_BOTHINVSRCALPHA;
399 caps->DestBlendCaps &= D3DPBLENDCAPS_ZERO | D3DPBLENDCAPS_ONE | D3DPBLENDCAPS_SRCCOLOR
400 | D3DPBLENDCAPS_INVSRCCOLOR | D3DPBLENDCAPS_SRCALPHA | D3DPBLENDCAPS_INVSRCALPHA
401 | D3DPBLENDCAPS_DESTALPHA | D3DPBLENDCAPS_INVDESTALPHA | D3DPBLENDCAPS_DESTCOLOR
402 | D3DPBLENDCAPS_INVDESTCOLOR | D3DPBLENDCAPS_SRCALPHASAT | D3DPBLENDCAPS_BOTHSRCALPHA
403 | D3DPBLENDCAPS_BOTHINVSRCALPHA;
404 caps->TextureCaps &= D3DPTEXTURECAPS_PERSPECTIVE | D3DPTEXTURECAPS_POW2 | D3DPTEXTURECAPS_ALPHA
405 | D3DPTEXTURECAPS_SQUAREONLY | D3DPTEXTURECAPS_TEXREPEATNOTSCALEDBYSIZE
406 | D3DPTEXTURECAPS_ALPHAPALETTE | D3DPTEXTURECAPS_NONPOW2CONDITIONAL
407 | D3DPTEXTURECAPS_PROJECTED | D3DPTEXTURECAPS_CUBEMAP | D3DPTEXTURECAPS_VOLUMEMAP
408 | D3DPTEXTURECAPS_MIPMAP | D3DPTEXTURECAPS_MIPVOLUMEMAP | D3DPTEXTURECAPS_MIPCUBEMAP
409 | D3DPTEXTURECAPS_CUBEMAP_POW2 | D3DPTEXTURECAPS_VOLUMEMAP_POW2;
410 caps->TextureFilterCaps &= D3DPTFILTERCAPS_MINFPOINT | D3DPTFILTERCAPS_MINFLINEAR
411 | D3DPTFILTERCAPS_MINFANISOTROPIC | D3DPTFILTERCAPS_MIPFPOINT
412 | D3DPTFILTERCAPS_MIPFLINEAR | D3DPTFILTERCAPS_MAGFPOINT | D3DPTFILTERCAPS_MAGFLINEAR
413 | D3DPTFILTERCAPS_MAGFANISOTROPIC | D3DPTFILTERCAPS_MAGFAFLATCUBIC
414 | D3DPTFILTERCAPS_MAGFGAUSSIANCUBIC;
415 caps->CubeTextureFilterCaps &= D3DPTFILTERCAPS_MINFPOINT | D3DPTFILTERCAPS_MINFLINEAR
416 | D3DPTFILTERCAPS_MINFANISOTROPIC | D3DPTFILTERCAPS_MIPFPOINT
417 | D3DPTFILTERCAPS_MIPFLINEAR | D3DPTFILTERCAPS_MAGFPOINT | D3DPTFILTERCAPS_MAGFLINEAR
418 | D3DPTFILTERCAPS_MAGFANISOTROPIC | D3DPTFILTERCAPS_MAGFAFLATCUBIC
419 | D3DPTFILTERCAPS_MAGFGAUSSIANCUBIC;
420 caps->VolumeTextureFilterCaps &= D3DPTFILTERCAPS_MINFPOINT | D3DPTFILTERCAPS_MINFLINEAR
421 | D3DPTFILTERCAPS_MINFANISOTROPIC | D3DPTFILTERCAPS_MIPFPOINT
422 | D3DPTFILTERCAPS_MIPFLINEAR | D3DPTFILTERCAPS_MAGFPOINT | D3DPTFILTERCAPS_MAGFLINEAR
423 | D3DPTFILTERCAPS_MAGFANISOTROPIC | D3DPTFILTERCAPS_MAGFAFLATCUBIC
424 | D3DPTFILTERCAPS_MAGFGAUSSIANCUBIC;
425 if (caps->LineCaps & WINED3DLINECAPS_ANTIALIAS)
426 caps->RasterCaps |= D3DPRASTERCAPS_ANTIALIASEDGES;
427 caps->LineCaps &= ~WINED3DLINECAPS_ANTIALIAS;
428 caps->StencilCaps &= ~WINED3DSTENCILCAPS_TWOSIDED;
429 caps->VertexProcessingCaps &= D3DVTXPCAPS_TEXGEN | D3DVTXPCAPS_MATERIALSOURCE7
430 | D3DVTXPCAPS_DIRECTIONALLIGHTS | D3DVTXPCAPS_POSITIONALLIGHTS | D3DVTXPCAPS_LOCALVIEWER
431 | D3DVTXPCAPS_TWEENING | D3DVTXPCAPS_NO_VSDT_UBYTE4;
433 /* D3D8 doesn't support SM 2.0 or higher, so clamp to 1.x */
434 if (caps->PixelShaderVersion)
435 caps->PixelShaderVersion = D3DPS_VERSION(1, 4);
436 else
437 caps->PixelShaderVersion = D3DPS_VERSION(0, 0);
438 if (caps->VertexShaderVersion)
439 caps->VertexShaderVersion = D3DVS_VERSION(1, 1);
440 else
441 caps->VertexShaderVersion = D3DVS_VERSION(0, 0);
442 caps->MaxVertexShaderConst = min(D3D8_MAX_VERTEX_SHADER_CONSTANTF, caps->MaxVertexShaderConst);
445 static enum wined3d_transform_state wined3d_transform_state_from_d3d(D3DTRANSFORMSTATETYPE state)
447 return (enum wined3d_transform_state)state;
450 static enum wined3d_render_state wined3d_render_state_from_d3d(D3DRENDERSTATETYPE state)
452 switch (state)
454 case D3DRS_ZBIAS:
455 return WINED3D_RS_DEPTHBIAS;
456 case D3DRS_EDGEANTIALIAS:
457 return WINED3D_RS_ANTIALIASEDLINEENABLE;
458 default:
459 return (enum wined3d_render_state)state;
463 static enum wined3d_primitive_type wined3d_primitive_type_from_d3d(D3DPRIMITIVETYPE type)
465 return (enum wined3d_primitive_type)type;
468 /* Handle table functions */
469 static DWORD d3d8_allocate_handle(struct d3d8_handle_table *t, void *object, enum d3d8_handle_type type)
471 struct d3d8_handle_entry *entry;
473 if (t->free_entries)
475 DWORD index = t->free_entries - t->entries;
476 /* Use a free handle */
477 entry = t->free_entries;
478 if (entry->type != D3D8_HANDLE_FREE)
480 ERR("Handle %u(%p) is in the free list, but has type %#x.\n", index, entry, entry->type);
481 return D3D8_INVALID_HANDLE;
483 t->free_entries = entry->object;
484 entry->object = object;
485 entry->type = type;
487 return index;
490 if (!(t->entry_count < t->table_size))
492 /* Grow the table */
493 UINT new_size = t->table_size + (t->table_size >> 1);
494 struct d3d8_handle_entry *new_entries;
496 if (!(new_entries = heap_realloc(t->entries, new_size * sizeof(*t->entries))))
498 ERR("Failed to grow the handle table.\n");
499 return D3D8_INVALID_HANDLE;
501 t->entries = new_entries;
502 t->table_size = new_size;
505 entry = &t->entries[t->entry_count];
506 entry->object = object;
507 entry->type = type;
509 return t->entry_count++;
512 static void *d3d8_free_handle(struct d3d8_handle_table *t, DWORD handle, enum d3d8_handle_type type)
514 struct d3d8_handle_entry *entry;
515 void *object;
517 if (handle == D3D8_INVALID_HANDLE || handle >= t->entry_count)
519 WARN("Invalid handle %u passed.\n", handle);
520 return NULL;
523 entry = &t->entries[handle];
524 if (entry->type != type)
526 WARN("Handle %u(%p) is not of type %#x.\n", handle, entry, type);
527 return NULL;
530 object = entry->object;
531 entry->object = t->free_entries;
532 entry->type = D3D8_HANDLE_FREE;
533 t->free_entries = entry;
535 return object;
538 static void *d3d8_get_object(struct d3d8_handle_table *t, DWORD handle, enum d3d8_handle_type type)
540 struct d3d8_handle_entry *entry;
542 if (handle == D3D8_INVALID_HANDLE || handle >= t->entry_count)
544 WARN("Invalid handle %u passed.\n", handle);
545 return NULL;
548 entry = &t->entries[handle];
549 if (entry->type != type)
551 WARN("Handle %u(%p) is not of type %#x.\n", handle, entry, type);
552 return NULL;
555 return entry->object;
558 static void device_reset_viewport_state(struct d3d8_device *device)
560 struct wined3d_viewport vp;
561 RECT rect;
563 wined3d_device_get_viewports(device->wined3d_device, NULL, &vp);
564 wined3d_stateblock_set_viewport(device->state, &vp);
565 wined3d_device_get_scissor_rects(device->wined3d_device, NULL, &rect);
566 wined3d_stateblock_set_scissor_rect(device->state, &rect);
569 static HRESULT WINAPI d3d8_device_QueryInterface(IDirect3DDevice8 *iface, REFIID riid, void **out)
571 TRACE("iface %p, riid %s, out %p.\n",
572 iface, debugstr_guid(riid), out);
574 if (IsEqualGUID(riid, &IID_IDirect3DDevice8)
575 || IsEqualGUID(riid, &IID_IUnknown))
577 IDirect3DDevice8_AddRef(iface);
578 *out = iface;
579 return S_OK;
582 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
584 *out = NULL;
585 return E_NOINTERFACE;
588 static ULONG WINAPI d3d8_device_AddRef(IDirect3DDevice8 *iface)
590 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
591 ULONG ref = InterlockedIncrement(&device->ref);
593 TRACE("%p increasing refcount to %u.\n", iface, ref);
595 return ref;
598 static ULONG WINAPI d3d8_device_Release(IDirect3DDevice8 *iface)
600 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
601 ULONG ref;
603 if (device->in_destruction)
604 return 0;
606 ref = InterlockedDecrement(&device->ref);
608 TRACE("%p decreasing refcount to %u.\n", iface, ref);
610 if (!ref)
612 IDirect3D8 *parent = &device->d3d_parent->IDirect3D8_iface;
613 unsigned i;
615 TRACE("Releasing wined3d device %p.\n", device->wined3d_device);
617 wined3d_mutex_lock();
619 device->in_destruction = TRUE;
621 for (i = 0; i < device->numConvertedDecls; ++i)
623 d3d8_vertex_declaration_destroy(device->decls[i].declaration);
625 heap_free(device->decls);
627 if (device->vertex_buffer)
628 wined3d_buffer_decref(device->vertex_buffer);
629 if (device->index_buffer)
630 wined3d_buffer_decref(device->index_buffer);
632 if (device->recording)
633 wined3d_stateblock_decref(device->recording);
634 wined3d_stateblock_decref(device->state);
636 wined3d_swapchain_decref(device->implicit_swapchain);
637 wined3d_device_release_focus_window(device->wined3d_device);
638 wined3d_device_decref(device->wined3d_device);
639 heap_free(device->handle_table.entries);
640 heap_free(device);
642 wined3d_mutex_unlock();
644 IDirect3D8_Release(parent);
646 return ref;
649 static HRESULT WINAPI d3d8_device_TestCooperativeLevel(IDirect3DDevice8 *iface)
651 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
653 TRACE("iface %p.\n", iface);
655 TRACE("device state: %#x.\n", device->device_state);
657 switch (device->device_state)
659 default:
660 case D3D8_DEVICE_STATE_OK:
661 return D3D_OK;
662 case D3D8_DEVICE_STATE_LOST:
663 return D3DERR_DEVICELOST;
664 case D3D8_DEVICE_STATE_NOT_RESET:
665 return D3DERR_DEVICENOTRESET;
669 static UINT WINAPI d3d8_device_GetAvailableTextureMem(IDirect3DDevice8 *iface)
671 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
672 UINT ret;
674 TRACE("iface %p.\n", iface);
676 wined3d_mutex_lock();
677 ret = wined3d_device_get_available_texture_mem(device->wined3d_device);
678 wined3d_mutex_unlock();
680 return ret;
683 static HRESULT WINAPI d3d8_device_ResourceManagerDiscardBytes(IDirect3DDevice8 *iface, DWORD byte_count)
685 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
687 TRACE("iface %p, byte_count %u.\n", iface, byte_count);
689 if (byte_count)
690 FIXME("Byte count ignored.\n");
692 wined3d_mutex_lock();
693 wined3d_device_evict_managed_resources(device->wined3d_device);
694 wined3d_mutex_unlock();
696 return D3D_OK;
699 static HRESULT WINAPI d3d8_device_GetDirect3D(IDirect3DDevice8 *iface, IDirect3D8 **d3d8)
701 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
703 TRACE("iface %p, d3d8 %p.\n", iface, d3d8);
705 if (!d3d8)
706 return D3DERR_INVALIDCALL;
708 return IDirect3D8_QueryInterface(&device->d3d_parent->IDirect3D8_iface, &IID_IDirect3D8,
709 (void **)d3d8);
712 static HRESULT WINAPI d3d8_device_GetDeviceCaps(IDirect3DDevice8 *iface, D3DCAPS8 *caps)
714 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
715 struct wined3d_caps wined3d_caps;
716 HRESULT hr;
718 TRACE("iface %p, caps %p.\n", iface, caps);
720 if (!caps)
721 return D3DERR_INVALIDCALL;
723 wined3d_mutex_lock();
724 hr = wined3d_device_get_device_caps(device->wined3d_device, &wined3d_caps);
725 wined3d_mutex_unlock();
727 d3dcaps_from_wined3dcaps(caps, &wined3d_caps, device->adapter_ordinal);
729 return hr;
732 static HRESULT WINAPI d3d8_device_GetDisplayMode(IDirect3DDevice8 *iface, D3DDISPLAYMODE *mode)
734 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
735 struct wined3d_display_mode wined3d_mode;
736 HRESULT hr;
738 TRACE("iface %p, mode %p.\n", iface, mode);
740 wined3d_mutex_lock();
741 hr = wined3d_device_get_display_mode(device->wined3d_device, 0, &wined3d_mode, NULL);
742 wined3d_mutex_unlock();
744 if (SUCCEEDED(hr))
746 mode->Width = wined3d_mode.width;
747 mode->Height = wined3d_mode.height;
748 mode->RefreshRate = wined3d_mode.refresh_rate;
749 mode->Format = d3dformat_from_wined3dformat(wined3d_mode.format_id);
752 return hr;
755 static HRESULT WINAPI d3d8_device_GetCreationParameters(IDirect3DDevice8 *iface,
756 D3DDEVICE_CREATION_PARAMETERS *parameters)
758 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
760 TRACE("iface %p, parameters %p.\n", iface, parameters);
762 wined3d_mutex_lock();
763 wined3d_device_get_creation_parameters(device->wined3d_device,
764 (struct wined3d_device_creation_parameters *)parameters);
765 wined3d_mutex_unlock();
767 parameters->AdapterOrdinal = device->adapter_ordinal;
768 return D3D_OK;
771 static HRESULT WINAPI d3d8_device_SetCursorProperties(IDirect3DDevice8 *iface,
772 UINT hotspot_x, UINT hotspot_y, IDirect3DSurface8 *bitmap)
774 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
775 struct d3d8_surface *bitmap_impl = unsafe_impl_from_IDirect3DSurface8(bitmap);
776 D3DSURFACE_DESC surface_desc;
777 D3DDISPLAYMODE mode;
778 HRESULT hr;
780 TRACE("iface %p, hotspot_x %u, hotspot_y %u, bitmap %p.\n",
781 iface, hotspot_x, hotspot_y, bitmap);
783 if (!bitmap)
785 WARN("No cursor bitmap, returning D3DERR_INVALIDCALL.\n");
786 return D3DERR_INVALIDCALL;
789 if (FAILED(hr = IDirect3DSurface8_GetDesc(bitmap, &surface_desc)))
791 WARN("Failed to get surface description, hr %#x.\n", hr);
792 return hr;
795 if (FAILED(hr = IDirect3D8_GetAdapterDisplayMode(&device->d3d_parent->IDirect3D8_iface,
796 device->adapter_ordinal, &mode)))
798 WARN("Failed to get device display mode, hr %#x.\n", hr);
799 return hr;
802 if (surface_desc.Width > mode.Width || surface_desc.Height > mode.Height)
804 WARN("Surface dimension %ux%u exceeds display mode %ux%u.\n", surface_desc.Width,
805 surface_desc.Height, mode.Width, mode.Height);
806 return D3DERR_INVALIDCALL;
809 wined3d_mutex_lock();
810 hr = wined3d_device_set_cursor_properties(device->wined3d_device,
811 hotspot_x, hotspot_y, bitmap_impl->wined3d_texture, bitmap_impl->sub_resource_idx);
812 wined3d_mutex_unlock();
814 return hr;
817 static void WINAPI d3d8_device_SetCursorPosition(IDirect3DDevice8 *iface, UINT x, UINT y, DWORD flags)
819 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
821 TRACE("iface %p, x %u, y %u, flags %#x.\n", iface, x, y, flags);
823 wined3d_mutex_lock();
824 wined3d_device_set_cursor_position(device->wined3d_device, x, y, flags);
825 wined3d_mutex_unlock();
828 static BOOL WINAPI d3d8_device_ShowCursor(IDirect3DDevice8 *iface, BOOL show)
830 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
831 BOOL ret;
833 TRACE("iface %p, show %#x.\n", iface, show);
835 wined3d_mutex_lock();
836 ret = wined3d_device_show_cursor(device->wined3d_device, show);
837 wined3d_mutex_unlock();
839 return ret;
842 static HRESULT WINAPI d3d8_device_CreateAdditionalSwapChain(IDirect3DDevice8 *iface,
843 D3DPRESENT_PARAMETERS *present_parameters, IDirect3DSwapChain8 **swapchain)
845 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
846 struct wined3d_swapchain_desc desc;
847 struct d3d8_swapchain *object;
848 unsigned int swap_interval;
849 unsigned int i, count;
850 HRESULT hr;
852 TRACE("iface %p, present_parameters %p, swapchain %p.\n",
853 iface, present_parameters, swapchain);
855 if (!present_parameters->Windowed)
857 WARN("Trying to create an additional fullscreen swapchain, returning D3DERR_INVALIDCALL.\n");
858 return D3DERR_INVALIDCALL;
861 wined3d_mutex_lock();
862 count = wined3d_device_get_swapchain_count(device->wined3d_device);
863 for (i = 0; i < count; ++i)
865 struct wined3d_swapchain *wined3d_swapchain;
867 wined3d_swapchain = wined3d_device_get_swapchain(device->wined3d_device, i);
868 wined3d_swapchain_get_desc(wined3d_swapchain, &desc);
870 if (!desc.windowed)
872 wined3d_mutex_unlock();
873 WARN("Trying to create an additional swapchain in fullscreen mode, returning D3DERR_INVALIDCALL.\n");
874 return D3DERR_INVALIDCALL;
877 wined3d_mutex_unlock();
879 if (!wined3d_swapchain_desc_from_present_parameters(&desc, present_parameters))
880 return D3DERR_INVALIDCALL;
881 swap_interval = wined3dswapinterval_from_d3d(present_parameters->FullScreen_PresentationInterval);
882 if (SUCCEEDED(hr = d3d8_swapchain_create(device, &desc, swap_interval, &object)))
883 *swapchain = &object->IDirect3DSwapChain8_iface;
884 present_parameters_from_wined3d_swapchain_desc(present_parameters,
885 &desc, present_parameters->FullScreen_PresentationInterval);
887 return hr;
890 static HRESULT CDECL reset_enum_callback(struct wined3d_resource *resource)
892 struct d3d8_vertexbuffer *vertex_buffer;
893 struct d3d8_indexbuffer *index_buffer;
894 struct wined3d_resource_desc desc;
895 IDirect3DBaseTexture8 *texture;
896 struct d3d8_surface *surface;
897 IUnknown *parent;
899 wined3d_resource_get_desc(resource, &desc);
900 if (desc.access & WINED3D_RESOURCE_ACCESS_CPU)
901 return D3D_OK;
903 if (desc.resource_type != WINED3D_RTYPE_TEXTURE_2D)
905 if (desc.bind_flags & WINED3D_BIND_VERTEX_BUFFER)
907 vertex_buffer = wined3d_resource_get_parent(resource);
908 if (vertex_buffer && vertex_buffer->draw_buffer)
909 return D3D_OK;
911 if (desc.bind_flags & WINED3D_BIND_INDEX_BUFFER)
913 index_buffer = wined3d_resource_get_parent(resource);
914 if (index_buffer && index_buffer->draw_buffer)
915 return D3D_OK;
918 WARN("Resource %p in pool D3DPOOL_DEFAULT blocks the Reset call.\n", resource);
919 return D3DERR_DEVICELOST;
922 parent = wined3d_resource_get_parent(resource);
923 if (parent && SUCCEEDED(IUnknown_QueryInterface(parent, &IID_IDirect3DBaseTexture8, (void **)&texture)))
925 IDirect3DBaseTexture8_Release(texture);
926 WARN("Texture %p (resource %p) in pool D3DPOOL_DEFAULT blocks the Reset call.\n", texture, resource);
927 return D3DERR_DEVICELOST;
930 surface = wined3d_texture_get_sub_resource_parent(wined3d_texture_from_resource(resource), 0);
931 if (!surface->resource.refcount)
932 return D3D_OK;
934 WARN("Surface %p in pool D3DPOOL_DEFAULT blocks the Reset call.\n", surface);
935 return D3DERR_DEVICELOST;
938 static HRESULT WINAPI d3d8_device_Reset(IDirect3DDevice8 *iface,
939 D3DPRESENT_PARAMETERS *present_parameters)
941 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
942 struct wined3d_swapchain_desc swapchain_desc;
943 struct d3d8_swapchain *implicit_swapchain;
944 HRESULT hr;
946 TRACE("iface %p, present_parameters %p.\n", iface, present_parameters);
948 if (device->device_state == D3D8_DEVICE_STATE_LOST)
950 WARN("App not active, returning D3DERR_DEVICELOST.\n");
951 return D3DERR_DEVICELOST;
953 if (!wined3d_swapchain_desc_from_present_parameters(&swapchain_desc, present_parameters))
954 return D3DERR_INVALIDCALL;
955 swapchain_desc.flags |= WINED3D_SWAPCHAIN_IMPLICIT;
957 wined3d_mutex_lock();
959 if (device->vertex_buffer)
961 wined3d_buffer_decref(device->vertex_buffer);
962 device->vertex_buffer = NULL;
963 device->vertex_buffer_size = 0;
965 if (device->index_buffer)
967 wined3d_buffer_decref(device->index_buffer);
968 device->index_buffer = NULL;
969 device->index_buffer_size = 0;
972 if (device->recording)
973 wined3d_stateblock_decref(device->recording);
974 device->recording = NULL;
975 device->update_state = device->state;
976 wined3d_stateblock_reset(device->state);
978 if (SUCCEEDED(hr = wined3d_device_reset(device->wined3d_device, &swapchain_desc,
979 NULL, reset_enum_callback, TRUE)))
981 present_parameters->BackBufferCount = swapchain_desc.backbuffer_count;
982 implicit_swapchain = wined3d_swapchain_get_parent(device->implicit_swapchain);
983 implicit_swapchain->swap_interval
984 = wined3dswapinterval_from_d3d(present_parameters->FullScreen_PresentationInterval);
985 wined3d_stateblock_set_render_state(device->state, WINED3D_RS_POINTSIZE_MIN, 0);
986 wined3d_stateblock_set_render_state(device->state, WINED3D_RS_ZENABLE,
987 !!swapchain_desc.enable_auto_depth_stencil);
988 device_reset_viewport_state(device);
989 device->device_state = D3D8_DEVICE_STATE_OK;
991 else
993 device->device_state = D3D8_DEVICE_STATE_NOT_RESET;
995 wined3d_mutex_unlock();
997 return hr;
1000 static HRESULT WINAPI d3d8_device_Present(IDirect3DDevice8 *iface, const RECT *src_rect,
1001 const RECT *dst_rect, HWND dst_window_override, const RGNDATA *dirty_region)
1003 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1004 struct d3d8_swapchain *implicit_swapchain;
1006 TRACE("iface %p, src_rect %s, dst_rect %s, dst_window_override %p, dirty_region %p.\n",
1007 iface, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect), dst_window_override, dirty_region);
1009 /* Fraps does not hook IDirect3DDevice8::Present regardless of the hotpatch
1010 * attribute. It only hooks IDirect3DSwapChain8::Present. Yet it properly
1011 * shows a framerate on Windows in applications that only call the device
1012 * method, like e.g. the dx8 sdk samples. The conclusion is that native
1013 * calls the swapchain's public method from the device. */
1014 implicit_swapchain = wined3d_swapchain_get_parent(device->implicit_swapchain);
1015 return IDirect3DSwapChain8_Present(&implicit_swapchain->IDirect3DSwapChain8_iface,
1016 src_rect, dst_rect, dst_window_override, dirty_region);
1019 static HRESULT WINAPI d3d8_device_GetBackBuffer(IDirect3DDevice8 *iface,
1020 UINT backbuffer_idx, D3DBACKBUFFER_TYPE backbuffer_type, IDirect3DSurface8 **backbuffer)
1022 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1023 struct wined3d_texture *wined3d_texture;
1024 struct d3d8_surface *surface_impl;
1026 TRACE("iface %p, backbuffer_idx %u, backbuffer_type %#x, backbuffer %p.\n",
1027 iface, backbuffer_idx, backbuffer_type, backbuffer);
1029 /* backbuffer_type is ignored by native. */
1031 /* No need to check for backbuffer == NULL, Windows crashes in that case. */
1032 wined3d_mutex_lock();
1034 if (!(wined3d_texture = wined3d_swapchain_get_back_buffer(device->implicit_swapchain, backbuffer_idx)))
1036 wined3d_mutex_unlock();
1037 *backbuffer = NULL;
1038 return D3DERR_INVALIDCALL;
1041 surface_impl = wined3d_texture_get_sub_resource_parent(wined3d_texture, 0);
1042 *backbuffer = &surface_impl->IDirect3DSurface8_iface;
1043 IDirect3DSurface8_AddRef(*backbuffer);
1045 wined3d_mutex_unlock();
1046 return D3D_OK;
1049 static HRESULT WINAPI d3d8_device_GetRasterStatus(IDirect3DDevice8 *iface, D3DRASTER_STATUS *raster_status)
1051 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1052 HRESULT hr;
1054 TRACE("iface %p, raster_status %p.\n", iface, raster_status);
1056 wined3d_mutex_lock();
1057 hr = wined3d_device_get_raster_status(device->wined3d_device, 0, (struct wined3d_raster_status *)raster_status);
1058 wined3d_mutex_unlock();
1060 return hr;
1063 static void WINAPI d3d8_device_SetGammaRamp(IDirect3DDevice8 *iface, DWORD flags, const D3DGAMMARAMP *ramp)
1065 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1067 TRACE("iface %p, flags %#x, ramp %p.\n", iface, flags, ramp);
1069 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
1070 wined3d_mutex_lock();
1071 wined3d_device_set_gamma_ramp(device->wined3d_device, 0, flags, (const struct wined3d_gamma_ramp *)ramp);
1072 wined3d_mutex_unlock();
1075 static void WINAPI d3d8_device_GetGammaRamp(IDirect3DDevice8 *iface, D3DGAMMARAMP *ramp)
1077 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1079 TRACE("iface %p, ramp %p.\n", iface, ramp);
1081 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
1082 wined3d_mutex_lock();
1083 wined3d_device_get_gamma_ramp(device->wined3d_device, 0, (struct wined3d_gamma_ramp *)ramp);
1084 wined3d_mutex_unlock();
1087 static HRESULT WINAPI d3d8_device_CreateTexture(IDirect3DDevice8 *iface,
1088 UINT width, UINT height, UINT levels, DWORD usage, D3DFORMAT format,
1089 D3DPOOL pool, IDirect3DTexture8 **texture)
1091 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1092 struct d3d8_texture *object;
1093 HRESULT hr;
1095 TRACE("iface %p, width %u, height %u, levels %u, usage %#x, format %#x, pool %#x, texture %p.\n",
1096 iface, width, height, levels, usage, format, pool, texture);
1098 if (!format)
1099 return D3DERR_INVALIDCALL;
1101 *texture = NULL;
1102 if (!(object = heap_alloc_zero(sizeof(*object))))
1103 return D3DERR_OUTOFVIDEOMEMORY;
1105 hr = texture_init(object, device, width, height, levels, usage, format, pool);
1106 if (FAILED(hr))
1108 WARN("Failed to initialize texture, hr %#x.\n", hr);
1109 heap_free(object);
1110 return hr;
1113 TRACE("Created texture %p.\n", object);
1114 *texture = (IDirect3DTexture8 *)&object->IDirect3DBaseTexture8_iface;
1116 return D3D_OK;
1119 static HRESULT WINAPI d3d8_device_CreateVolumeTexture(IDirect3DDevice8 *iface,
1120 UINT width, UINT height, UINT depth, UINT levels, DWORD usage, D3DFORMAT format,
1121 D3DPOOL pool, IDirect3DVolumeTexture8 **texture)
1123 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1124 struct d3d8_texture *object;
1125 HRESULT hr;
1127 TRACE("iface %p, width %u, height %u, depth %u, levels %u, usage %#x, format %#x, pool %#x, texture %p.\n",
1128 iface, width, height, depth, levels, usage, format, pool, texture);
1130 if (!format)
1131 return D3DERR_INVALIDCALL;
1133 *texture = NULL;
1134 if (!(object = heap_alloc_zero(sizeof(*object))))
1135 return D3DERR_OUTOFVIDEOMEMORY;
1137 hr = volumetexture_init(object, device, width, height, depth, levels, usage, format, pool);
1138 if (FAILED(hr))
1140 WARN("Failed to initialize volume texture, hr %#x.\n", hr);
1141 heap_free(object);
1142 return hr;
1145 TRACE("Created volume texture %p.\n", object);
1146 *texture = (IDirect3DVolumeTexture8 *)&object->IDirect3DBaseTexture8_iface;
1148 return D3D_OK;
1151 static HRESULT WINAPI d3d8_device_CreateCubeTexture(IDirect3DDevice8 *iface, UINT edge_length,
1152 UINT levels, DWORD usage, D3DFORMAT format, D3DPOOL pool, IDirect3DCubeTexture8 **texture)
1154 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1155 struct d3d8_texture *object;
1156 HRESULT hr;
1158 TRACE("iface %p, edge_length %u, levels %u, usage %#x, format %#x, pool %#x, texture %p.\n",
1159 iface, edge_length, levels, usage, format, pool, texture);
1161 if (!format)
1162 return D3DERR_INVALIDCALL;
1164 *texture = NULL;
1165 if (!(object = heap_alloc_zero(sizeof(*object))))
1166 return D3DERR_OUTOFVIDEOMEMORY;
1168 hr = cubetexture_init(object, device, edge_length, levels, usage, format, pool);
1169 if (FAILED(hr))
1171 WARN("Failed to initialize cube texture, hr %#x.\n", hr);
1172 heap_free(object);
1173 return hr;
1176 TRACE("Created cube texture %p.\n", object);
1177 *texture = (IDirect3DCubeTexture8 *)&object->IDirect3DBaseTexture8_iface;
1179 return hr;
1182 static HRESULT WINAPI d3d8_device_CreateVertexBuffer(IDirect3DDevice8 *iface, UINT size,
1183 DWORD usage, DWORD fvf, D3DPOOL pool, IDirect3DVertexBuffer8 **buffer)
1185 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1186 struct d3d8_vertexbuffer *object;
1187 HRESULT hr;
1189 TRACE("iface %p, size %u, usage %#x, fvf %#x, pool %#x, buffer %p.\n",
1190 iface, size, usage, fvf, pool, buffer);
1192 if (!(object = heap_alloc_zero(sizeof(*object))))
1193 return D3DERR_OUTOFVIDEOMEMORY;
1195 hr = vertexbuffer_init(object, device, size, usage, fvf, pool);
1196 if (FAILED(hr))
1198 WARN("Failed to initialize vertex buffer, hr %#x.\n", hr);
1199 heap_free(object);
1200 return hr;
1203 TRACE("Created vertex buffer %p.\n", object);
1204 *buffer = &object->IDirect3DVertexBuffer8_iface;
1206 return D3D_OK;
1209 static HRESULT WINAPI d3d8_device_CreateIndexBuffer(IDirect3DDevice8 *iface, UINT size,
1210 DWORD usage, D3DFORMAT format, D3DPOOL pool, IDirect3DIndexBuffer8 **buffer)
1212 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1213 struct d3d8_indexbuffer *object;
1214 HRESULT hr;
1216 TRACE("iface %p, size %u, usage %#x, format %#x, pool %#x, buffer %p.\n",
1217 iface, size, usage, format, pool, buffer);
1219 if (!(object = heap_alloc_zero(sizeof(*object))))
1220 return D3DERR_OUTOFVIDEOMEMORY;
1222 hr = indexbuffer_init(object, device, size, usage, format, pool);
1223 if (FAILED(hr))
1225 WARN("Failed to initialize index buffer, hr %#x.\n", hr);
1226 heap_free(object);
1227 return hr;
1230 TRACE("Created index buffer %p.\n", object);
1231 *buffer = &object->IDirect3DIndexBuffer8_iface;
1233 return D3D_OK;
1236 static HRESULT d3d8_device_create_surface(struct d3d8_device *device, enum wined3d_format_id format,
1237 enum wined3d_multisample_type multisample_type, unsigned int bind_flags, unsigned int access,
1238 unsigned int width, unsigned int height, IDirect3DSurface8 **surface)
1240 struct wined3d_resource_desc desc;
1241 struct d3d8_surface *surface_impl;
1242 struct wined3d_texture *texture;
1243 HRESULT hr;
1245 TRACE("device %p, format %#x, multisample_type %#x, bind_flags %#x, "
1246 "access %#x, width %u, height %u, surface %p.\n",
1247 device, format, multisample_type, bind_flags, access, width, height, surface);
1249 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
1250 desc.format = format;
1251 desc.multisample_type = multisample_type;
1252 desc.multisample_quality = 0;
1253 desc.usage = 0;
1254 desc.bind_flags = bind_flags;
1255 desc.access = access;
1256 desc.width = width;
1257 desc.height = height;
1258 desc.depth = 1;
1259 desc.size = 0;
1261 wined3d_mutex_lock();
1263 if (FAILED(hr = wined3d_texture_create(device->wined3d_device, &desc,
1264 1, 1, 0, NULL, NULL, &d3d8_null_wined3d_parent_ops, &texture)))
1266 wined3d_mutex_unlock();
1267 WARN("Failed to create texture, hr %#x.\n", hr);
1268 return hr;
1271 surface_impl = wined3d_texture_get_sub_resource_parent(texture, 0);
1272 surface_impl->parent_device = &device->IDirect3DDevice8_iface;
1273 *surface = &surface_impl->IDirect3DSurface8_iface;
1274 IDirect3DSurface8_AddRef(*surface);
1275 wined3d_texture_decref(texture);
1277 wined3d_mutex_unlock();
1279 return D3D_OK;
1282 static HRESULT WINAPI d3d8_device_CreateRenderTarget(IDirect3DDevice8 *iface, UINT width,
1283 UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, BOOL lockable,
1284 IDirect3DSurface8 **surface)
1286 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1287 unsigned int access = WINED3D_RESOURCE_ACCESS_GPU;
1289 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, lockable %#x, surface %p.\n",
1290 iface, width, height, format, multisample_type, lockable, surface);
1292 if (!format)
1293 return D3DERR_INVALIDCALL;
1295 *surface = NULL;
1296 if (lockable)
1297 access |= WINED3D_RESOURCE_ACCESS_MAP_R | WINED3D_RESOURCE_ACCESS_MAP_W;
1299 return d3d8_device_create_surface(device, wined3dformat_from_d3dformat(format),
1300 wined3d_multisample_type_from_d3d(multisample_type),
1301 WINED3D_BIND_RENDER_TARGET, access, width, height, surface);
1304 static HRESULT WINAPI d3d8_device_CreateDepthStencilSurface(IDirect3DDevice8 *iface,
1305 UINT width, UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type,
1306 IDirect3DSurface8 **surface)
1308 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1310 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, surface %p.\n",
1311 iface, width, height, format, multisample_type, surface);
1313 if (!format)
1314 return D3DERR_INVALIDCALL;
1316 *surface = NULL;
1318 return d3d8_device_create_surface(device, wined3dformat_from_d3dformat(format),
1319 wined3d_multisample_type_from_d3d(multisample_type), WINED3D_BIND_DEPTH_STENCIL,
1320 WINED3D_RESOURCE_ACCESS_GPU, width, height, surface);
1323 /* IDirect3DDevice8Impl::CreateImageSurface returns surface with pool type SYSTEMMEM */
1324 static HRESULT WINAPI d3d8_device_CreateImageSurface(IDirect3DDevice8 *iface, UINT width,
1325 UINT height, D3DFORMAT format, IDirect3DSurface8 **surface)
1327 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1328 unsigned int access = WINED3D_RESOURCE_ACCESS_CPU
1329 | WINED3D_RESOURCE_ACCESS_MAP_R | WINED3D_RESOURCE_ACCESS_MAP_W;
1331 TRACE("iface %p, width %u, height %u, format %#x, surface %p.\n",
1332 iface, width, height, format, surface);
1334 *surface = NULL;
1336 return d3d8_device_create_surface(device, wined3dformat_from_d3dformat(format),
1337 WINED3D_MULTISAMPLE_NONE, 0, access, width, height, surface);
1340 static HRESULT WINAPI d3d8_device_CopyRects(IDirect3DDevice8 *iface,
1341 IDirect3DSurface8 *src_surface, const RECT *src_rects, UINT rect_count,
1342 IDirect3DSurface8 *dst_surface, const POINT *dst_points)
1344 struct d3d8_surface *src = unsafe_impl_from_IDirect3DSurface8(src_surface);
1345 struct d3d8_surface *dst = unsafe_impl_from_IDirect3DSurface8(dst_surface);
1346 enum wined3d_format_id src_format, dst_format;
1347 struct wined3d_sub_resource_desc wined3d_desc;
1348 UINT src_w, src_h;
1350 TRACE("iface %p, src_surface %p, src_rects %p, rect_count %u, dst_surface %p, dst_points %p.\n",
1351 iface, src_surface, src_rects, rect_count, dst_surface, dst_points);
1353 /* Check that the source texture is in WINED3D_POOL_SYSTEM_MEM and the
1354 * destination texture is in WINED3D_POOL_DEFAULT. */
1356 wined3d_mutex_lock();
1357 wined3d_texture_get_sub_resource_desc(src->wined3d_texture, src->sub_resource_idx, &wined3d_desc);
1358 if (wined3d_desc.bind_flags & WINED3D_BIND_DEPTH_STENCIL)
1360 WARN("Source %p is a depth stencil surface, returning D3DERR_INVALIDCALL.\n", src_surface);
1361 wined3d_mutex_unlock();
1362 return D3DERR_INVALIDCALL;
1364 src_format = wined3d_desc.format;
1365 src_w = wined3d_desc.width;
1366 src_h = wined3d_desc.height;
1368 wined3d_texture_get_sub_resource_desc(dst->wined3d_texture, dst->sub_resource_idx, &wined3d_desc);
1369 if (wined3d_desc.bind_flags & WINED3D_BIND_DEPTH_STENCIL)
1371 WARN("Destination %p is a depth stencil surface, returning D3DERR_INVALIDCALL.\n", dst_surface);
1372 wined3d_mutex_unlock();
1373 return D3DERR_INVALIDCALL;
1375 dst_format = wined3d_desc.format;
1377 /* Check that the source and destination formats match */
1378 if (src_format != dst_format)
1380 WARN("Source %p format must match the destination %p format, returning D3DERR_INVALIDCALL.\n",
1381 src_surface, dst_surface);
1382 wined3d_mutex_unlock();
1383 return D3DERR_INVALIDCALL;
1386 /* Quick if complete copy ... */
1387 if (!rect_count && !src_rects && !dst_points)
1389 RECT rect = {0, 0, src_w, src_h};
1390 wined3d_texture_blt(dst->wined3d_texture, dst->sub_resource_idx, &rect,
1391 src->wined3d_texture, src->sub_resource_idx, &rect, 0, NULL, WINED3D_TEXF_POINT);
1393 else
1395 unsigned int i;
1396 /* Copy rect by rect */
1397 if (src_rects && dst_points)
1399 for (i = 0; i < rect_count; ++i)
1401 UINT w = src_rects[i].right - src_rects[i].left;
1402 UINT h = src_rects[i].bottom - src_rects[i].top;
1403 RECT dst_rect = {dst_points[i].x, dst_points[i].y,
1404 dst_points[i].x + w, dst_points[i].y + h};
1406 wined3d_texture_blt(dst->wined3d_texture, dst->sub_resource_idx, &dst_rect,
1407 src->wined3d_texture, src->sub_resource_idx, &src_rects[i], 0, NULL, WINED3D_TEXF_POINT);
1410 else
1412 for (i = 0; i < rect_count; ++i)
1414 UINT w = src_rects[i].right - src_rects[i].left;
1415 UINT h = src_rects[i].bottom - src_rects[i].top;
1416 RECT dst_rect = {0, 0, w, h};
1418 wined3d_texture_blt(dst->wined3d_texture, dst->sub_resource_idx, &dst_rect,
1419 src->wined3d_texture, src->sub_resource_idx, &src_rects[i], 0, NULL, WINED3D_TEXF_POINT);
1423 wined3d_mutex_unlock();
1425 return WINED3D_OK;
1428 static HRESULT WINAPI d3d8_device_UpdateTexture(IDirect3DDevice8 *iface,
1429 IDirect3DBaseTexture8 *src_texture, IDirect3DBaseTexture8 *dst_texture)
1431 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1432 struct d3d8_texture *src_impl, *dst_impl;
1433 HRESULT hr;
1435 TRACE("iface %p, src_texture %p, dst_texture %p.\n", iface, src_texture, dst_texture);
1437 src_impl = unsafe_impl_from_IDirect3DBaseTexture8(src_texture);
1438 dst_impl = unsafe_impl_from_IDirect3DBaseTexture8(dst_texture);
1440 wined3d_mutex_lock();
1441 hr = wined3d_device_update_texture(device->wined3d_device,
1442 src_impl->wined3d_texture, dst_impl->wined3d_texture);
1443 wined3d_mutex_unlock();
1445 return hr;
1448 static HRESULT WINAPI d3d8_device_GetFrontBuffer(IDirect3DDevice8 *iface, IDirect3DSurface8 *dst_surface)
1450 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1451 struct d3d8_surface *dst_impl = unsafe_impl_from_IDirect3DSurface8(dst_surface);
1452 HRESULT hr;
1454 TRACE("iface %p, dst_surface %p.\n", iface, dst_surface);
1456 if (!dst_surface)
1458 WARN("Invalid destination surface passed.\n");
1459 return D3DERR_INVALIDCALL;
1462 wined3d_mutex_lock();
1463 hr = wined3d_swapchain_get_front_buffer_data(device->implicit_swapchain,
1464 dst_impl->wined3d_texture, dst_impl->sub_resource_idx);
1465 wined3d_mutex_unlock();
1467 return hr;
1470 static HRESULT WINAPI d3d8_device_SetRenderTarget(IDirect3DDevice8 *iface,
1471 IDirect3DSurface8 *render_target, IDirect3DSurface8 *depth_stencil)
1473 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1474 struct d3d8_surface *rt_impl = unsafe_impl_from_IDirect3DSurface8(render_target);
1475 struct d3d8_surface *ds_impl = unsafe_impl_from_IDirect3DSurface8(depth_stencil);
1476 struct wined3d_rendertarget_view *original_dsv, *rtv;
1477 HRESULT hr = D3D_OK;
1479 TRACE("iface %p, render_target %p, depth_stencil %p.\n", iface, render_target, depth_stencil);
1481 if (rt_impl && d3d8_surface_get_device(rt_impl) != device)
1483 WARN("Render target surface does not match device.\n");
1484 return D3DERR_INVALIDCALL;
1487 wined3d_mutex_lock();
1489 if (ds_impl)
1491 struct wined3d_sub_resource_desc ds_desc, rt_desc;
1492 struct wined3d_rendertarget_view *original_rtv;
1493 struct d3d8_surface *original_surface;
1495 /* If no render target is passed in check the size against the current RT */
1496 if (!render_target)
1498 if (!(original_rtv = wined3d_device_get_rendertarget_view(device->wined3d_device, 0)))
1500 wined3d_mutex_unlock();
1501 return D3DERR_NOTFOUND;
1503 original_surface = wined3d_rendertarget_view_get_sub_resource_parent(original_rtv);
1504 wined3d_texture_get_sub_resource_desc(original_surface->wined3d_texture,
1505 original_surface->sub_resource_idx, &rt_desc);
1507 else
1508 wined3d_texture_get_sub_resource_desc(rt_impl->wined3d_texture,
1509 rt_impl->sub_resource_idx, &rt_desc);
1511 wined3d_texture_get_sub_resource_desc(ds_impl->wined3d_texture, ds_impl->sub_resource_idx, &ds_desc);
1513 if (ds_desc.width < rt_desc.width || ds_desc.height < rt_desc.height)
1515 WARN("Depth stencil is smaller than the render target, returning D3DERR_INVALIDCALL\n");
1516 wined3d_mutex_unlock();
1517 return D3DERR_INVALIDCALL;
1519 if (ds_desc.multisample_type != rt_desc.multisample_type
1520 || ds_desc.multisample_quality != rt_desc.multisample_quality)
1522 WARN("Multisample settings do not match, returning D3DERR_INVALIDCALL\n");
1523 wined3d_mutex_unlock();
1524 return D3DERR_INVALIDCALL;
1528 original_dsv = wined3d_device_get_depth_stencil_view(device->wined3d_device);
1529 rtv = ds_impl ? d3d8_surface_acquire_rendertarget_view(ds_impl) : NULL;
1530 hr = wined3d_device_set_depth_stencil_view(device->wined3d_device, rtv);
1531 d3d8_surface_release_rendertarget_view(ds_impl, rtv);
1532 if (SUCCEEDED(hr))
1534 rtv = render_target ? d3d8_surface_acquire_rendertarget_view(rt_impl) : NULL;
1535 if (render_target)
1537 if (SUCCEEDED(hr = wined3d_device_set_rendertarget_view(device->wined3d_device, 0, rtv, TRUE)))
1538 device_reset_viewport_state(device);
1539 else
1540 wined3d_device_set_depth_stencil_view(device->wined3d_device, original_dsv);
1542 d3d8_surface_release_rendertarget_view(rt_impl, rtv);
1545 wined3d_mutex_unlock();
1547 return hr;
1550 static HRESULT WINAPI d3d8_device_GetRenderTarget(IDirect3DDevice8 *iface, IDirect3DSurface8 **render_target)
1552 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1553 struct wined3d_rendertarget_view *wined3d_rtv;
1554 struct d3d8_surface *surface_impl;
1555 HRESULT hr;
1557 TRACE("iface %p, render_target %p.\n", iface, render_target);
1559 if (!render_target)
1560 return D3DERR_INVALIDCALL;
1562 wined3d_mutex_lock();
1563 if ((wined3d_rtv = wined3d_device_get_rendertarget_view(device->wined3d_device, 0)))
1565 /* We want the sub resource parent here, since the view itself may be
1566 * internal to wined3d and may not have a parent. */
1567 surface_impl = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_rtv);
1568 *render_target = &surface_impl->IDirect3DSurface8_iface;
1569 IDirect3DSurface8_AddRef(*render_target);
1570 hr = D3D_OK;
1572 else
1574 ERR("Failed to get wined3d render target.\n");
1575 *render_target = NULL;
1576 hr = D3DERR_NOTFOUND;
1578 wined3d_mutex_unlock();
1580 return hr;
1583 static HRESULT WINAPI d3d8_device_GetDepthStencilSurface(IDirect3DDevice8 *iface, IDirect3DSurface8 **depth_stencil)
1585 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1586 struct wined3d_rendertarget_view *wined3d_dsv;
1587 struct d3d8_surface *surface_impl;
1588 HRESULT hr = D3D_OK;
1590 TRACE("iface %p, depth_stencil %p.\n", iface, depth_stencil);
1592 if (!depth_stencil)
1593 return D3DERR_INVALIDCALL;
1595 wined3d_mutex_lock();
1596 if ((wined3d_dsv = wined3d_device_get_depth_stencil_view(device->wined3d_device)))
1598 /* We want the sub resource parent here, since the view itself may be
1599 * internal to wined3d and may not have a parent. */
1600 surface_impl = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_dsv);
1601 *depth_stencil = &surface_impl->IDirect3DSurface8_iface;
1602 IDirect3DSurface8_AddRef(*depth_stencil);
1604 else
1606 hr = D3DERR_NOTFOUND;
1607 *depth_stencil = NULL;
1609 wined3d_mutex_unlock();
1611 return hr;
1614 static HRESULT WINAPI d3d8_device_BeginScene(IDirect3DDevice8 *iface)
1616 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1617 HRESULT hr;
1619 TRACE("iface %p.\n", iface);
1621 wined3d_mutex_lock();
1622 hr = wined3d_device_begin_scene(device->wined3d_device);
1623 wined3d_mutex_unlock();
1625 return hr;
1628 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d8_device_EndScene(IDirect3DDevice8 *iface)
1630 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1631 HRESULT hr;
1633 TRACE("iface %p.\n", iface);
1635 wined3d_mutex_lock();
1636 hr = wined3d_device_end_scene(device->wined3d_device);
1637 wined3d_mutex_unlock();
1639 return hr;
1642 static HRESULT WINAPI d3d8_device_Clear(IDirect3DDevice8 *iface, DWORD rect_count,
1643 const D3DRECT *rects, DWORD flags, D3DCOLOR color, float z, DWORD stencil)
1645 const struct wined3d_color c =
1647 ((color >> 16) & 0xff) / 255.0f,
1648 ((color >> 8) & 0xff) / 255.0f,
1649 (color & 0xff) / 255.0f,
1650 ((color >> 24) & 0xff) / 255.0f,
1652 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1653 HRESULT hr;
1655 TRACE("iface %p, rect_count %u, rects %p, flags %#x, color 0x%08x, z %.8e, stencil %u.\n",
1656 iface, rect_count, rects, flags, color, z, stencil);
1658 if (rect_count && !rects)
1660 WARN("count %u with NULL rects.\n", rect_count);
1661 rect_count = 0;
1664 wined3d_mutex_lock();
1665 wined3d_device_apply_stateblock(device->wined3d_device, device->state);
1666 hr = wined3d_device_clear(device->wined3d_device, rect_count, (const RECT *)rects, flags, &c, z, stencil);
1667 wined3d_mutex_unlock();
1669 return hr;
1672 static HRESULT WINAPI d3d8_device_SetTransform(IDirect3DDevice8 *iface,
1673 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
1675 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1677 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1679 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1680 wined3d_mutex_lock();
1681 wined3d_stateblock_set_transform(device->update_state,
1682 wined3d_transform_state_from_d3d(state), (const struct wined3d_matrix *)matrix);
1683 wined3d_mutex_unlock();
1685 return D3D_OK;
1688 static HRESULT WINAPI d3d8_device_GetTransform(IDirect3DDevice8 *iface,
1689 D3DTRANSFORMSTATETYPE state, D3DMATRIX *matrix)
1691 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1693 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1695 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1696 wined3d_mutex_lock();
1697 memcpy(matrix, &device->stateblock_state->transforms[state], sizeof(*matrix));
1698 wined3d_mutex_unlock();
1700 return D3D_OK;
1703 static HRESULT WINAPI d3d8_device_MultiplyTransform(IDirect3DDevice8 *iface,
1704 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
1706 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1708 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1710 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1711 wined3d_mutex_lock();
1712 wined3d_stateblock_multiply_transform(device->state,
1713 wined3d_transform_state_from_d3d(state), (const struct wined3d_matrix *)matrix);
1714 wined3d_mutex_unlock();
1716 return D3D_OK;
1719 static HRESULT WINAPI d3d8_device_SetViewport(IDirect3DDevice8 *iface, const D3DVIEWPORT8 *viewport)
1721 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1722 struct wined3d_sub_resource_desc rt_desc;
1723 struct wined3d_rendertarget_view *rtv;
1724 struct d3d8_surface *surface;
1725 struct wined3d_viewport vp;
1727 TRACE("iface %p, viewport %p.\n", iface, viewport);
1729 wined3d_mutex_lock();
1730 if (!(rtv = wined3d_device_get_rendertarget_view(device->wined3d_device, 0)))
1732 wined3d_mutex_unlock();
1733 return D3DERR_NOTFOUND;
1735 surface = wined3d_rendertarget_view_get_sub_resource_parent(rtv);
1736 wined3d_texture_get_sub_resource_desc(surface->wined3d_texture, surface->sub_resource_idx, &rt_desc);
1738 if (viewport->X > rt_desc.width || viewport->Width > rt_desc.width - viewport->X
1739 || viewport->Y > rt_desc.height || viewport->Height > rt_desc.height - viewport->Y)
1741 WARN("Invalid viewport, returning D3DERR_INVALIDCALL.\n");
1742 wined3d_mutex_unlock();
1743 return D3DERR_INVALIDCALL;
1746 vp.x = viewport->X;
1747 vp.y = viewport->Y;
1748 vp.width = viewport->Width;
1749 vp.height = viewport->Height;
1750 vp.min_z = viewport->MinZ;
1751 vp.max_z = viewport->MaxZ;
1753 wined3d_stateblock_set_viewport(device->update_state, &vp);
1754 wined3d_mutex_unlock();
1756 return D3D_OK;
1759 static HRESULT WINAPI d3d8_device_GetViewport(IDirect3DDevice8 *iface, D3DVIEWPORT8 *viewport)
1761 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1762 struct wined3d_viewport wined3d_viewport;
1764 TRACE("iface %p, viewport %p.\n", iface, viewport);
1766 wined3d_mutex_lock();
1767 wined3d_viewport = device->stateblock_state->viewport;
1768 wined3d_mutex_unlock();
1770 viewport->X = wined3d_viewport.x;
1771 viewport->Y = wined3d_viewport.y;
1772 viewport->Width = wined3d_viewport.width;
1773 viewport->Height = wined3d_viewport.height;
1774 viewport->MinZ = wined3d_viewport.min_z;
1775 viewport->MaxZ = wined3d_viewport.max_z;
1777 return D3D_OK;
1780 static HRESULT WINAPI d3d8_device_SetMaterial(IDirect3DDevice8 *iface, const D3DMATERIAL8 *material)
1782 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1784 TRACE("iface %p, material %p.\n", iface, material);
1786 /* Note: D3DMATERIAL8 is compatible with struct wined3d_material. */
1787 wined3d_mutex_lock();
1788 wined3d_stateblock_set_material(device->update_state, (const struct wined3d_material *)material);
1789 wined3d_mutex_unlock();
1791 return D3D_OK;
1794 static HRESULT WINAPI d3d8_device_GetMaterial(IDirect3DDevice8 *iface, D3DMATERIAL8 *material)
1796 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1798 TRACE("iface %p, material %p.\n", iface, material);
1800 /* Note: D3DMATERIAL8 is compatible with struct wined3d_material. */
1801 wined3d_mutex_lock();
1802 memcpy(material, &device->stateblock_state->material, sizeof(*material));
1803 wined3d_mutex_unlock();
1805 return D3D_OK;
1808 static HRESULT WINAPI d3d8_device_SetLight(IDirect3DDevice8 *iface, DWORD index, const D3DLIGHT8 *light)
1810 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1811 HRESULT hr;
1813 TRACE("iface %p, index %u, light %p.\n", iface, index, light);
1815 /* Note: D3DLIGHT8 is compatible with struct wined3d_light. */
1816 wined3d_mutex_lock();
1817 hr = wined3d_stateblock_set_light(device->update_state, index, (const struct wined3d_light *)light);
1818 wined3d_mutex_unlock();
1820 return hr;
1823 static HRESULT WINAPI d3d8_device_GetLight(IDirect3DDevice8 *iface, DWORD index, D3DLIGHT8 *light)
1825 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1826 BOOL enabled;
1827 HRESULT hr;
1829 TRACE("iface %p, index %u, light %p.\n", iface, index, light);
1831 /* Note: D3DLIGHT8 is compatible with struct wined3d_light. */
1832 wined3d_mutex_lock();
1833 hr = wined3d_stateblock_get_light(device->state, index, (struct wined3d_light *)light, &enabled);
1834 wined3d_mutex_unlock();
1836 return hr;
1839 static HRESULT WINAPI d3d8_device_LightEnable(IDirect3DDevice8 *iface, DWORD index, BOOL enable)
1841 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1842 HRESULT hr;
1844 TRACE("iface %p, index %u, enable %#x.\n", iface, index, enable);
1846 wined3d_mutex_lock();
1847 hr = wined3d_stateblock_set_light_enable(device->update_state, index, enable);
1848 wined3d_mutex_unlock();
1850 return hr;
1853 static HRESULT WINAPI d3d8_device_GetLightEnable(IDirect3DDevice8 *iface, DWORD index, BOOL *enabled)
1855 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1856 struct wined3d_light light;
1857 HRESULT hr;
1859 TRACE("iface %p, index %u, enabled %p.\n", iface, index, enabled);
1861 wined3d_mutex_lock();
1862 hr = wined3d_stateblock_get_light(device->state, index, &light, enabled);
1863 wined3d_mutex_unlock();
1865 return hr;
1868 static HRESULT WINAPI d3d8_device_SetClipPlane(IDirect3DDevice8 *iface, DWORD index, const float *plane)
1870 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1871 HRESULT hr;
1873 TRACE("iface %p, index %u, plane %p.\n", iface, index, plane);
1875 wined3d_mutex_lock();
1876 hr = wined3d_stateblock_set_clip_plane(device->update_state, index, (const struct wined3d_vec4 *)plane);
1877 wined3d_mutex_unlock();
1879 return hr;
1882 static HRESULT WINAPI d3d8_device_GetClipPlane(IDirect3DDevice8 *iface, DWORD index, float *plane)
1884 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1886 TRACE("iface %p, index %u, plane %p.\n", iface, index, plane);
1888 if (index >= device->max_user_clip_planes)
1890 TRACE("Plane %u requested, but only %u planes are supported.\n", index, device->max_user_clip_planes);
1891 return WINED3DERR_INVALIDCALL;
1894 wined3d_mutex_lock();
1895 memcpy(plane, &device->stateblock_state->clip_planes[index], sizeof(struct wined3d_vec4));
1896 wined3d_mutex_unlock();
1898 return D3D_OK;
1901 static void resolve_depth_buffer(struct d3d8_device *device)
1903 const struct wined3d_stateblock_state *state = device->stateblock_state;
1904 struct wined3d_rendertarget_view *wined3d_dsv;
1905 struct wined3d_resource *dst_resource;
1906 struct wined3d_texture *dst_texture;
1907 struct wined3d_resource_desc desc;
1908 struct d3d8_surface *d3d8_dsv;
1910 if (!(dst_texture = state->textures[0]))
1911 return;
1912 dst_resource = wined3d_texture_get_resource(dst_texture);
1913 wined3d_resource_get_desc(dst_resource, &desc);
1914 if (desc.format != WINED3DFMT_D24_UNORM_S8_UINT
1915 && desc.format != WINED3DFMT_X8D24_UNORM
1916 && desc.format != WINED3DFMT_DF16
1917 && desc.format != WINED3DFMT_DF24
1918 && desc.format != WINED3DFMT_INTZ)
1919 return;
1921 if (!(wined3d_dsv = wined3d_device_get_depth_stencil_view(device->wined3d_device)))
1922 return;
1923 d3d8_dsv = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_dsv);
1925 wined3d_device_resolve_sub_resource(device->wined3d_device, dst_resource, 0,
1926 wined3d_rendertarget_view_get_resource(wined3d_dsv), d3d8_dsv->sub_resource_idx, desc.format);
1929 static HRESULT WINAPI d3d8_device_SetRenderState(IDirect3DDevice8 *iface,
1930 D3DRENDERSTATETYPE state, DWORD value)
1932 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1934 TRACE("iface %p, state %#x, value %#x.\n", iface, state, value);
1936 wined3d_mutex_lock();
1937 wined3d_stateblock_set_render_state(device->update_state, wined3d_render_state_from_d3d(state), value);
1938 if (state == D3DRS_POINTSIZE && value == D3D8_RESZ_CODE)
1939 resolve_depth_buffer(device);
1940 wined3d_mutex_unlock();
1942 return D3D_OK;
1945 static HRESULT WINAPI d3d8_device_GetRenderState(IDirect3DDevice8 *iface,
1946 D3DRENDERSTATETYPE state, DWORD *value)
1948 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1949 const struct wined3d_stateblock_state *device_state;
1951 TRACE("iface %p, state %#x, value %p.\n", iface, state, value);
1953 wined3d_mutex_lock();
1954 device_state = device->stateblock_state;
1955 switch (state)
1957 case D3DRS_ZBIAS:
1958 *value = device_state->rs[WINED3D_RS_DEPTHBIAS];
1959 break;
1961 default:
1962 *value = device_state->rs[state];
1964 wined3d_mutex_unlock();
1966 return D3D_OK;
1969 static HRESULT WINAPI d3d8_device_BeginStateBlock(IDirect3DDevice8 *iface)
1971 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1972 struct wined3d_stateblock *stateblock;
1973 HRESULT hr;
1975 TRACE("iface %p.\n", iface);
1977 wined3d_mutex_lock();
1978 if (device->recording)
1980 wined3d_mutex_unlock();
1981 WARN("Trying to begin a stateblock while recording, returning D3DERR_INVALIDCALL.\n");
1982 return D3DERR_INVALIDCALL;
1985 if (SUCCEEDED(hr = wined3d_stateblock_create(device->wined3d_device, NULL, WINED3D_SBT_RECORDED, &stateblock)))
1986 device->update_state = device->recording = stateblock;
1987 wined3d_mutex_unlock();
1989 return hr;
1992 static HRESULT WINAPI d3d8_device_EndStateBlock(IDirect3DDevice8 *iface, DWORD *token)
1994 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1995 struct wined3d_stateblock *stateblock;
1997 TRACE("iface %p, token %p.\n", iface, token);
1999 /* Tell wineD3D to endstateblock before anything else (in case we run out
2000 * of memory later and cause locking problems)
2002 wined3d_mutex_lock();
2003 if (!device->recording)
2005 wined3d_mutex_unlock();
2006 WARN("Trying to end a stateblock, but no stateblock is being recorded.\n");
2007 return D3DERR_INVALIDCALL;
2009 stateblock = device->recording;
2010 wined3d_stateblock_init_contained_states(stateblock);
2011 device->recording = NULL;
2012 device->update_state = device->state;
2014 *token = d3d8_allocate_handle(&device->handle_table, stateblock, D3D8_HANDLE_SB);
2015 wined3d_mutex_unlock();
2017 if (*token == D3D8_INVALID_HANDLE)
2019 ERR("Failed to create a handle\n");
2020 wined3d_mutex_lock();
2021 wined3d_stateblock_decref(stateblock);
2022 wined3d_mutex_unlock();
2023 return E_FAIL;
2025 ++*token;
2027 TRACE("Returning %#x (%p).\n", *token, stateblock);
2029 return D3D_OK;
2032 static HRESULT WINAPI d3d8_device_ApplyStateBlock(IDirect3DDevice8 *iface, DWORD token)
2034 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2035 const struct wined3d_stateblock_state *state;
2036 struct wined3d_stateblock *stateblock;
2037 struct wined3d_buffer *wined3d_buffer;
2038 struct d3d8_vertexbuffer *buffer;
2039 unsigned int i;
2041 TRACE("iface %p, token %#x.\n", iface, token);
2043 if (!token)
2044 return D3D_OK;
2046 wined3d_mutex_lock();
2047 if (device->recording)
2049 wined3d_mutex_unlock();
2050 WARN("Trying to apply stateblock while recording, returning D3DERR_INVALIDCALL.\n");
2051 return D3DERR_INVALIDCALL;
2053 stateblock = d3d8_get_object(&device->handle_table, token - 1, D3D8_HANDLE_SB);
2054 if (!stateblock)
2056 WARN("Invalid handle (%#x) passed.\n", token);
2057 wined3d_mutex_unlock();
2058 return D3DERR_INVALIDCALL;
2060 wined3d_stateblock_apply(stateblock, device->state);
2061 device->sysmem_vb = 0;
2062 state = device->stateblock_state;
2063 for (i = 0; i < D3D8_MAX_STREAMS; ++i)
2065 wined3d_buffer = state->streams[i].buffer;
2066 if (!wined3d_buffer || !(buffer = wined3d_buffer_get_parent(wined3d_buffer)))
2067 continue;
2068 if (buffer->draw_buffer)
2069 device->sysmem_vb |= 1u << i;
2071 device->sysmem_ib = (wined3d_buffer = state->index_buffer)
2072 && (buffer = wined3d_buffer_get_parent(wined3d_buffer)) && buffer->draw_buffer;
2073 wined3d_mutex_unlock();
2075 return D3D_OK;
2078 static HRESULT WINAPI d3d8_device_CaptureStateBlock(IDirect3DDevice8 *iface, DWORD token)
2080 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2081 struct wined3d_stateblock *stateblock;
2083 TRACE("iface %p, token %#x.\n", iface, token);
2085 wined3d_mutex_lock();
2086 if (device->recording)
2088 wined3d_mutex_unlock();
2089 WARN("Trying to capture stateblock while recording, returning D3DERR_INVALIDCALL.\n");
2090 return D3DERR_INVALIDCALL;
2092 stateblock = d3d8_get_object(&device->handle_table, token - 1, D3D8_HANDLE_SB);
2093 if (!stateblock)
2095 WARN("Invalid handle (%#x) passed.\n", token);
2096 wined3d_mutex_unlock();
2097 return D3DERR_INVALIDCALL;
2099 wined3d_stateblock_capture(stateblock, device->state);
2100 wined3d_mutex_unlock();
2102 return D3D_OK;
2105 static HRESULT WINAPI d3d8_device_DeleteStateBlock(IDirect3DDevice8 *iface, DWORD token)
2107 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2108 struct wined3d_stateblock *stateblock;
2110 TRACE("iface %p, token %#x.\n", iface, token);
2112 wined3d_mutex_lock();
2113 stateblock = d3d8_free_handle(&device->handle_table, token - 1, D3D8_HANDLE_SB);
2115 if (!stateblock)
2117 WARN("Invalid handle (%#x) passed.\n", token);
2118 wined3d_mutex_unlock();
2119 return D3DERR_INVALIDCALL;
2122 if (wined3d_stateblock_decref(stateblock))
2124 ERR("Stateblock %p has references left, this shouldn't happen.\n", stateblock);
2126 wined3d_mutex_unlock();
2128 return D3D_OK;
2131 static HRESULT WINAPI d3d8_device_CreateStateBlock(IDirect3DDevice8 *iface,
2132 D3DSTATEBLOCKTYPE type, DWORD *handle)
2134 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2135 struct wined3d_stateblock *stateblock;
2136 HRESULT hr;
2138 TRACE("iface %p, type %#x, handle %p.\n", iface, type, handle);
2140 if (type != D3DSBT_ALL
2141 && type != D3DSBT_PIXELSTATE
2142 && type != D3DSBT_VERTEXSTATE)
2144 WARN("Unexpected stateblock type, returning D3DERR_INVALIDCALL\n");
2145 return D3DERR_INVALIDCALL;
2148 wined3d_mutex_lock();
2149 if (device->recording)
2151 wined3d_mutex_unlock();
2152 WARN("Trying to create a stateblock while recording, returning D3DERR_INVALIDCALL.\n");
2153 return D3DERR_INVALIDCALL;
2155 hr = wined3d_stateblock_create(device->wined3d_device, device->state, (enum wined3d_stateblock_type)type, &stateblock);
2156 if (FAILED(hr))
2158 wined3d_mutex_unlock();
2159 ERR("Failed to create the state block, hr %#x\n", hr);
2160 return hr;
2163 *handle = d3d8_allocate_handle(&device->handle_table, stateblock, D3D8_HANDLE_SB);
2164 wined3d_mutex_unlock();
2166 if (*handle == D3D8_INVALID_HANDLE)
2168 ERR("Failed to allocate a handle.\n");
2169 wined3d_mutex_lock();
2170 wined3d_stateblock_decref(stateblock);
2171 wined3d_mutex_unlock();
2172 return E_FAIL;
2174 ++*handle;
2176 TRACE("Returning %#x (%p).\n", *handle, stateblock);
2178 return hr;
2181 static HRESULT WINAPI d3d8_device_SetClipStatus(IDirect3DDevice8 *iface, const D3DCLIPSTATUS8 *clip_status)
2183 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2184 HRESULT hr;
2186 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
2187 /* FIXME: Verify that D3DCLIPSTATUS8 ~= struct wined3d_clip_status. */
2189 wined3d_mutex_lock();
2190 hr = wined3d_device_set_clip_status(device->wined3d_device, (const struct wined3d_clip_status *)clip_status);
2191 wined3d_mutex_unlock();
2193 return hr;
2196 static HRESULT WINAPI d3d8_device_GetClipStatus(IDirect3DDevice8 *iface, D3DCLIPSTATUS8 *clip_status)
2198 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2199 HRESULT hr;
2201 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
2203 wined3d_mutex_lock();
2204 hr = wined3d_device_get_clip_status(device->wined3d_device, (struct wined3d_clip_status *)clip_status);
2205 wined3d_mutex_unlock();
2207 return hr;
2210 static HRESULT WINAPI d3d8_device_GetTexture(IDirect3DDevice8 *iface, DWORD stage, IDirect3DBaseTexture8 **texture)
2212 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2213 struct wined3d_texture *wined3d_texture;
2214 struct d3d8_texture *texture_impl;
2216 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
2218 if (!texture)
2219 return D3DERR_INVALIDCALL;
2221 if (stage >= WINED3D_MAX_FRAGMENT_SAMPLERS)
2223 WARN("Ignoring invalid stage %u.\n", stage);
2224 *texture = NULL;
2225 return D3D_OK;
2228 wined3d_mutex_lock();
2229 if ((wined3d_texture = device->stateblock_state->textures[stage]))
2231 texture_impl = wined3d_texture_get_parent(wined3d_texture);
2232 *texture = &texture_impl->IDirect3DBaseTexture8_iface;
2233 IDirect3DBaseTexture8_AddRef(*texture);
2235 else
2237 *texture = NULL;
2239 wined3d_mutex_unlock();
2241 return D3D_OK;
2244 static HRESULT WINAPI d3d8_device_SetTexture(IDirect3DDevice8 *iface, DWORD stage, IDirect3DBaseTexture8 *texture)
2246 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2247 struct d3d8_texture *texture_impl;
2249 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
2251 texture_impl = unsafe_impl_from_IDirect3DBaseTexture8(texture);
2253 wined3d_mutex_lock();
2254 wined3d_stateblock_set_texture(device->update_state, stage,
2255 texture_impl ? texture_impl->wined3d_texture : NULL);
2256 wined3d_mutex_unlock();
2258 return D3D_OK;
2261 static const struct tss_lookup
2263 BOOL sampler_state;
2264 union
2266 enum wined3d_texture_stage_state texture_state;
2267 enum wined3d_sampler_state sampler_state;
2268 } u;
2270 tss_lookup[] =
2272 {FALSE, {WINED3D_TSS_INVALID}}, /* 0, unused */
2273 {FALSE, {WINED3D_TSS_COLOR_OP}}, /* 1, D3DTSS_COLOROP */
2274 {FALSE, {WINED3D_TSS_COLOR_ARG1}}, /* 2, D3DTSS_COLORARG1 */
2275 {FALSE, {WINED3D_TSS_COLOR_ARG2}}, /* 3, D3DTSS_COLORARG2 */
2276 {FALSE, {WINED3D_TSS_ALPHA_OP}}, /* 4, D3DTSS_ALPHAOP */
2277 {FALSE, {WINED3D_TSS_ALPHA_ARG1}}, /* 5, D3DTSS_ALPHAARG1 */
2278 {FALSE, {WINED3D_TSS_ALPHA_ARG2}}, /* 6, D3DTSS_ALPHAARG2 */
2279 {FALSE, {WINED3D_TSS_BUMPENV_MAT00}}, /* 7, D3DTSS_BUMPENVMAT00 */
2280 {FALSE, {WINED3D_TSS_BUMPENV_MAT01}}, /* 8, D3DTSS_BUMPENVMAT01 */
2281 {FALSE, {WINED3D_TSS_BUMPENV_MAT10}}, /* 9, D3DTSS_BUMPENVMAT10 */
2282 {FALSE, {WINED3D_TSS_BUMPENV_MAT11}}, /* 10, D3DTSS_BUMPENVMAT11 */
2283 {FALSE, {WINED3D_TSS_TEXCOORD_INDEX}}, /* 11, D3DTSS_TEXCOORDINDEX */
2284 {FALSE, {WINED3D_TSS_INVALID}}, /* 12, unused */
2285 {TRUE, {WINED3D_SAMP_ADDRESS_U}}, /* 13, D3DTSS_ADDRESSU */
2286 {TRUE, {WINED3D_SAMP_ADDRESS_V}}, /* 14, D3DTSS_ADDRESSV */
2287 {TRUE, {WINED3D_SAMP_BORDER_COLOR}}, /* 15, D3DTSS_BORDERCOLOR */
2288 {TRUE, {WINED3D_SAMP_MAG_FILTER}}, /* 16, D3DTSS_MAGFILTER */
2289 {TRUE, {WINED3D_SAMP_MIN_FILTER}}, /* 17, D3DTSS_MINFILTER */
2290 {TRUE, {WINED3D_SAMP_MIP_FILTER}}, /* 18, D3DTSS_MIPFILTER */
2291 {TRUE, {WINED3D_SAMP_MIPMAP_LOD_BIAS}}, /* 19, D3DTSS_MIPMAPLODBIAS */
2292 {TRUE, {WINED3D_SAMP_MAX_MIP_LEVEL}}, /* 20, D3DTSS_MAXMIPLEVEL */
2293 {TRUE, {WINED3D_SAMP_MAX_ANISOTROPY}}, /* 21, D3DTSS_MAXANISOTROPY */
2294 {FALSE, {WINED3D_TSS_BUMPENV_LSCALE}}, /* 22, D3DTSS_BUMPENVLSCALE */
2295 {FALSE, {WINED3D_TSS_BUMPENV_LOFFSET}}, /* 23, D3DTSS_BUMPENVLOFFSET */
2296 {FALSE, {WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS}}, /* 24, D3DTSS_TEXTURETRANSFORMFLAGS */
2297 {TRUE, {WINED3D_SAMP_ADDRESS_W}}, /* 25, D3DTSS_ADDRESSW */
2298 {FALSE, {WINED3D_TSS_COLOR_ARG0}}, /* 26, D3DTSS_COLORARG0 */
2299 {FALSE, {WINED3D_TSS_ALPHA_ARG0}}, /* 27, D3DTSS_ALPHAARG0 */
2300 {FALSE, {WINED3D_TSS_RESULT_ARG}}, /* 28, D3DTSS_RESULTARG */
2303 static HRESULT WINAPI d3d8_device_GetTextureStageState(IDirect3DDevice8 *iface,
2304 DWORD stage, D3DTEXTURESTAGESTATETYPE state, DWORD *value)
2306 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2307 const struct wined3d_stateblock_state *device_state;
2308 const struct tss_lookup *l;
2310 TRACE("iface %p, stage %u, state %#x, value %p.\n", iface, stage, state, value);
2312 if (state >= ARRAY_SIZE(tss_lookup))
2314 WARN("Invalid state %#x.\n", state);
2315 return D3D_OK;
2318 l = &tss_lookup[state];
2320 wined3d_mutex_lock();
2321 device_state = device->stateblock_state;
2322 if (l->sampler_state)
2323 *value = device_state->sampler_states[stage][l->u.sampler_state];
2324 else
2325 *value = device_state->texture_states[stage][l->u.texture_state];
2326 wined3d_mutex_unlock();
2328 return D3D_OK;
2331 static HRESULT WINAPI d3d8_device_SetTextureStageState(IDirect3DDevice8 *iface,
2332 DWORD stage, D3DTEXTURESTAGESTATETYPE type, DWORD value)
2334 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2335 const struct tss_lookup *l;
2337 TRACE("iface %p, stage %u, state %#x, value %#x.\n", iface, stage, type, value);
2339 if (type >= ARRAY_SIZE(tss_lookup))
2341 WARN("Invalid type %#x passed.\n", type);
2342 return D3D_OK;
2345 l = &tss_lookup[type];
2347 wined3d_mutex_lock();
2348 if (l->sampler_state)
2349 wined3d_stateblock_set_sampler_state(device->update_state, stage, l->u.sampler_state, value);
2350 else
2351 wined3d_stateblock_set_texture_stage_state(device->update_state, stage, l->u.texture_state, value);
2352 wined3d_mutex_unlock();
2354 return D3D_OK;
2357 static HRESULT WINAPI d3d8_device_ValidateDevice(IDirect3DDevice8 *iface, DWORD *pass_count)
2359 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2360 HRESULT hr;
2362 TRACE("iface %p, pass_count %p.\n", iface, pass_count);
2364 wined3d_mutex_lock();
2365 wined3d_device_apply_stateblock(device->wined3d_device, device->state);
2366 hr = wined3d_device_validate_device(device->wined3d_device, pass_count);
2367 wined3d_mutex_unlock();
2369 return hr;
2372 static HRESULT WINAPI d3d8_device_GetInfo(IDirect3DDevice8 *iface,
2373 DWORD info_id, void *info, DWORD info_size)
2375 TRACE("iface %p, info_id %#x, info %p, info_size %u.\n", iface, info_id, info, info_size);
2377 if (info_id < 4)
2378 return E_FAIL;
2379 return S_FALSE;
2382 static HRESULT WINAPI d3d8_device_SetPaletteEntries(IDirect3DDevice8 *iface,
2383 UINT palette_idx, const PALETTEENTRY *entries)
2385 WARN("iface %p, palette_idx %u, entries %p unimplemented\n", iface, palette_idx, entries);
2387 /* GPUs stopped supporting palettized textures with the Shader Model 1 generation. Wined3d
2388 * does not have a d3d8/9-style palette API */
2390 return D3D_OK;
2393 static HRESULT WINAPI d3d8_device_GetPaletteEntries(IDirect3DDevice8 *iface,
2394 UINT palette_idx, PALETTEENTRY *entries)
2396 FIXME("iface %p, palette_idx %u, entries %p unimplemented.\n", iface, palette_idx, entries);
2398 return D3DERR_INVALIDCALL;
2401 static HRESULT WINAPI d3d8_device_SetCurrentTexturePalette(IDirect3DDevice8 *iface, UINT palette_idx)
2403 WARN("iface %p, palette_idx %u unimplemented.\n", iface, palette_idx);
2405 return D3D_OK;
2408 static HRESULT WINAPI d3d8_device_GetCurrentTexturePalette(IDirect3DDevice8 *iface, UINT *palette_idx)
2410 FIXME("iface %p, palette_idx %p unimplemented.\n", iface, palette_idx);
2412 return D3DERR_INVALIDCALL;
2415 static void d3d8_device_upload_sysmem_vertex_buffers(struct d3d8_device *device,
2416 unsigned int start_vertex, unsigned int vertex_count)
2418 const struct wined3d_stateblock_state *state = device->stateblock_state;
2419 struct wined3d_vertex_declaration *wined3d_decl;
2420 struct wined3d_box box = {0, 0, 0, 1, 0, 1};
2421 const struct wined3d_stream_state *stream;
2422 struct d3d8_vertexbuffer *d3d8_buffer;
2423 struct wined3d_resource *dst_resource;
2424 struct d3d8_vertex_declaration *decl;
2425 struct wined3d_buffer *dst_buffer;
2426 struct wined3d_resource_desc desc;
2427 unsigned int stride, map;
2428 HRESULT hr;
2430 if (!device->sysmem_vb)
2431 return;
2433 if (!(wined3d_decl = state->vertex_declaration))
2434 return;
2436 decl = wined3d_vertex_declaration_get_parent(wined3d_decl);
2437 map = decl->stream_map & device->sysmem_vb;
2438 while (map)
2440 stream = &state->streams[wined3d_bit_scan(&map)];
2441 dst_buffer = stream->buffer;
2442 stride = stream->stride;
2444 d3d8_buffer = wined3d_buffer_get_parent(dst_buffer);
2445 dst_resource = wined3d_buffer_get_resource(dst_buffer);
2446 wined3d_resource_get_desc(dst_resource, &desc);
2447 box.left = stream->offset + start_vertex * stride;
2448 box.right = min(box.left + vertex_count * stride, desc.size);
2449 if (FAILED(hr = wined3d_device_copy_sub_resource_region(device->wined3d_device,
2450 dst_resource, 0, box.left, 0, 0,
2451 wined3d_buffer_get_resource(d3d8_buffer->wined3d_buffer), 0, &box, 0)))
2452 ERR("Failed to update buffer.\n");
2456 static void d3d8_device_upload_sysmem_index_buffer(struct d3d8_device *device,
2457 unsigned int start_idx, unsigned int idx_count)
2459 const struct wined3d_stateblock_state *state = device->stateblock_state;
2460 struct wined3d_box box = {0, 0, 0, 1, 0, 1};
2461 struct wined3d_resource *dst_resource;
2462 struct d3d8_indexbuffer *d3d8_buffer;
2463 struct wined3d_buffer *dst_buffer;
2464 struct wined3d_resource_desc desc;
2465 enum wined3d_format_id format;
2466 unsigned int idx_size;
2467 HRESULT hr;
2469 if (!device->sysmem_ib)
2470 return;
2472 dst_buffer = state->index_buffer;
2473 format = state->index_format;
2474 d3d8_buffer = wined3d_buffer_get_parent(dst_buffer);
2475 dst_resource = wined3d_buffer_get_resource(dst_buffer);
2476 wined3d_resource_get_desc(dst_resource, &desc);
2477 idx_size = format == WINED3DFMT_R16_UINT ? 2 : 4;
2478 box.left = start_idx * idx_size;
2479 box.right = min(box.left + idx_count * idx_size, desc.size);
2480 if (FAILED(hr = wined3d_device_copy_sub_resource_region(device->wined3d_device,
2481 dst_resource, 0, box.left, 0, 0,
2482 wined3d_buffer_get_resource(d3d8_buffer->wined3d_buffer), 0, &box, 0)))
2483 ERR("Failed to update buffer.\n");
2486 static HRESULT WINAPI d3d8_device_DrawPrimitive(IDirect3DDevice8 *iface,
2487 D3DPRIMITIVETYPE primitive_type, UINT start_vertex, UINT primitive_count)
2489 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2490 unsigned int vertex_count;
2491 HRESULT hr;
2493 TRACE("iface %p, primitive_type %#x, start_vertex %u, primitive_count %u.\n",
2494 iface, primitive_type, start_vertex, primitive_count);
2496 vertex_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2497 wined3d_mutex_lock();
2498 d3d8_device_upload_sysmem_vertex_buffers(device, start_vertex, vertex_count);
2499 wined3d_device_set_primitive_type(device->wined3d_device, wined3d_primitive_type_from_d3d(primitive_type), 0);
2500 wined3d_device_apply_stateblock(device->wined3d_device, device->state);
2501 hr = wined3d_device_draw_primitive(device->wined3d_device, start_vertex, vertex_count);
2502 wined3d_mutex_unlock();
2504 return hr;
2507 static HRESULT WINAPI d3d8_device_DrawIndexedPrimitive(IDirect3DDevice8 *iface,
2508 D3DPRIMITIVETYPE primitive_type, UINT min_vertex_idx, UINT vertex_count,
2509 UINT start_idx, UINT primitive_count)
2511 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2512 unsigned int index_count;
2513 int base_vertex_index;
2514 HRESULT hr;
2516 TRACE("iface %p, primitive_type %#x, min_vertex_idx %u, vertex_count %u, start_idx %u, primitive_count %u.\n",
2517 iface, primitive_type, min_vertex_idx, vertex_count, start_idx, primitive_count);
2519 index_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2520 wined3d_mutex_lock();
2521 base_vertex_index = device->stateblock_state->base_vertex_index;
2522 d3d8_device_upload_sysmem_vertex_buffers(device, base_vertex_index + min_vertex_idx, vertex_count);
2523 d3d8_device_upload_sysmem_index_buffer(device, start_idx, index_count);
2524 wined3d_device_set_primitive_type(device->wined3d_device, wined3d_primitive_type_from_d3d(primitive_type), 0);
2525 wined3d_device_apply_stateblock(device->wined3d_device, device->state);
2526 hr = wined3d_device_draw_indexed_primitive(device->wined3d_device, start_idx, index_count);
2527 wined3d_mutex_unlock();
2529 return hr;
2532 /* The caller is responsible for wined3d locking */
2533 static HRESULT d3d8_device_prepare_vertex_buffer(struct d3d8_device *device, UINT min_size)
2535 HRESULT hr;
2537 if (device->vertex_buffer_size < min_size || !device->vertex_buffer)
2539 UINT size = max(device->vertex_buffer_size * 2, min_size);
2540 struct wined3d_buffer_desc desc;
2541 struct wined3d_buffer *buffer;
2543 TRACE("Growing vertex buffer to %u bytes\n", size);
2545 desc.byte_width = size;
2546 desc.usage = WINED3DUSAGE_DYNAMIC;
2547 desc.bind_flags = WINED3D_BIND_VERTEX_BUFFER;
2548 desc.access = WINED3D_RESOURCE_ACCESS_GPU | WINED3D_RESOURCE_ACCESS_MAP_W;
2549 desc.misc_flags = 0;
2550 desc.structure_byte_stride = 0;
2552 if (FAILED(hr = wined3d_buffer_create(device->wined3d_device, &desc,
2553 NULL, NULL, &d3d8_null_wined3d_parent_ops, &buffer)))
2555 ERR("Failed to create vertex buffer, hr %#x.\n", hr);
2556 return hr;
2559 if (device->vertex_buffer)
2560 wined3d_buffer_decref(device->vertex_buffer);
2562 device->vertex_buffer = buffer;
2563 device->vertex_buffer_size = size;
2564 device->vertex_buffer_pos = 0;
2566 return D3D_OK;
2569 static HRESULT WINAPI d3d8_device_DrawPrimitiveUP(IDirect3DDevice8 *iface,
2570 D3DPRIMITIVETYPE primitive_type, UINT primitive_count, const void *data,
2571 UINT stride)
2573 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2574 HRESULT hr;
2575 UINT vtx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2576 struct wined3d_map_desc wined3d_map_desc;
2577 struct wined3d_box wined3d_box = {0};
2578 UINT size = vtx_count * stride;
2579 struct wined3d_resource *vb;
2580 UINT vb_pos, align;
2582 TRACE("iface %p, primitive_type %#x, primitive_count %u, data %p, stride %u.\n",
2583 iface, primitive_type, primitive_count, data, stride);
2585 if (!primitive_count || !stride)
2587 WARN("primitive_count or stride is 0, returning D3D_OK.\n");
2588 return D3D_OK;
2591 wined3d_mutex_lock();
2592 hr = d3d8_device_prepare_vertex_buffer(device, size);
2593 if (FAILED(hr))
2594 goto done;
2596 vb_pos = device->vertex_buffer_pos;
2597 align = vb_pos % stride;
2598 if (align) align = stride - align;
2599 if (vb_pos + size + align > device->vertex_buffer_size)
2600 vb_pos = 0;
2601 else
2602 vb_pos += align;
2604 wined3d_box.left = vb_pos;
2605 wined3d_box.right = vb_pos + size;
2606 vb = wined3d_buffer_get_resource(device->vertex_buffer);
2607 if (FAILED(wined3d_resource_map(vb, 0, &wined3d_map_desc, &wined3d_box,
2608 WINED3D_MAP_WRITE | (vb_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD))))
2609 goto done;
2610 memcpy(wined3d_map_desc.data, data, size);
2611 wined3d_resource_unmap(vb, 0);
2612 device->vertex_buffer_pos = vb_pos + size;
2614 hr = wined3d_stateblock_set_stream_source(device->state, 0, device->vertex_buffer, 0, stride);
2615 if (FAILED(hr))
2616 goto done;
2618 wined3d_device_set_primitive_type(device->wined3d_device, wined3d_primitive_type_from_d3d(primitive_type), 0);
2619 wined3d_device_apply_stateblock(device->wined3d_device, device->state);
2620 hr = wined3d_device_draw_primitive(device->wined3d_device, vb_pos / stride, vtx_count);
2621 wined3d_stateblock_set_stream_source(device->state, 0, NULL, 0, 0);
2623 done:
2624 wined3d_mutex_unlock();
2625 return hr;
2628 /* The caller is responsible for wined3d locking */
2629 static HRESULT d3d8_device_prepare_index_buffer(struct d3d8_device *device, UINT min_size)
2631 HRESULT hr;
2633 if (device->index_buffer_size < min_size || !device->index_buffer)
2635 UINT size = max(device->index_buffer_size * 2, min_size);
2636 struct wined3d_buffer_desc desc;
2637 struct wined3d_buffer *buffer;
2639 TRACE("Growing index buffer to %u bytes\n", size);
2641 desc.byte_width = size;
2642 desc.usage = WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_STATICDECL;
2643 desc.bind_flags = WINED3D_BIND_INDEX_BUFFER;
2644 desc.access = WINED3D_RESOURCE_ACCESS_GPU | WINED3D_RESOURCE_ACCESS_MAP_W;
2645 desc.misc_flags = 0;
2646 desc.structure_byte_stride = 0;
2648 if (FAILED(hr = wined3d_buffer_create(device->wined3d_device, &desc,
2649 NULL, NULL, &d3d8_null_wined3d_parent_ops, &buffer)))
2651 ERR("Failed to create index buffer, hr %#x.\n", hr);
2652 return hr;
2655 if (device->index_buffer)
2656 wined3d_buffer_decref(device->index_buffer);
2658 device->index_buffer = buffer;
2659 device->index_buffer_size = size;
2660 device->index_buffer_pos = 0;
2662 return D3D_OK;
2665 static HRESULT WINAPI d3d8_device_DrawIndexedPrimitiveUP(IDirect3DDevice8 *iface,
2666 D3DPRIMITIVETYPE primitive_type, UINT min_vertex_idx, UINT vertex_count,
2667 UINT primitive_count, const void *index_data, D3DFORMAT index_format,
2668 const void *vertex_data, UINT vertex_stride)
2670 UINT idx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2671 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2672 UINT idx_fmt_size = index_format == D3DFMT_INDEX16 ? 2 : 4;
2673 UINT vtx_size = vertex_count * vertex_stride;
2674 UINT idx_size = idx_count * idx_fmt_size;
2675 struct wined3d_map_desc wined3d_map_desc;
2676 struct wined3d_box wined3d_box = {0};
2677 struct wined3d_resource *ib, *vb;
2678 UINT vb_pos, ib_pos, align;
2679 HRESULT hr;
2681 TRACE("iface %p, primitive_type %#x, min_vertex_idx %u, vertex_count %u, primitive_count %u, "
2682 "index_data %p, index_format %#x, vertex_data %p, vertex_stride %u.\n",
2683 iface, primitive_type, min_vertex_idx, vertex_count, primitive_count,
2684 index_data, index_format, vertex_data, vertex_stride);
2686 if (!primitive_count || !vertex_stride)
2688 WARN("primitive_count or vertex_stride is 0, returning D3D_OK.\n");
2689 return D3D_OK;
2692 wined3d_mutex_lock();
2694 hr = d3d8_device_prepare_vertex_buffer(device, vtx_size);
2695 if (FAILED(hr))
2696 goto done;
2698 vb_pos = device->vertex_buffer_pos;
2699 align = vb_pos % vertex_stride;
2700 if (align) align = vertex_stride - align;
2701 if (vb_pos + vtx_size + align > device->vertex_buffer_size)
2702 vb_pos = 0;
2703 else
2704 vb_pos += align;
2706 wined3d_box.left = vb_pos;
2707 wined3d_box.right = vb_pos + vtx_size;
2708 vb = wined3d_buffer_get_resource(device->vertex_buffer);
2709 if (FAILED(hr = wined3d_resource_map(vb, 0, &wined3d_map_desc, &wined3d_box,
2710 WINED3D_MAP_WRITE | (vb_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD))))
2711 goto done;
2712 memcpy(wined3d_map_desc.data, (char *)vertex_data + min_vertex_idx * vertex_stride, vtx_size);
2713 wined3d_resource_unmap(vb, 0);
2714 device->vertex_buffer_pos = vb_pos + vtx_size;
2716 hr = d3d8_device_prepare_index_buffer(device, idx_size);
2717 if (FAILED(hr))
2718 goto done;
2720 ib_pos = device->index_buffer_pos;
2721 align = ib_pos % idx_fmt_size;
2722 if (align) align = idx_fmt_size - align;
2723 if (ib_pos + idx_size + align > device->index_buffer_size)
2724 ib_pos = 0;
2725 else
2726 ib_pos += align;
2728 wined3d_box.left = ib_pos;
2729 wined3d_box.right = ib_pos + idx_size;
2730 ib = wined3d_buffer_get_resource(device->index_buffer);
2731 if (FAILED(hr = wined3d_resource_map(ib, 0, &wined3d_map_desc, &wined3d_box,
2732 WINED3D_MAP_WRITE | (ib_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD))))
2733 goto done;
2734 memcpy(wined3d_map_desc.data, index_data, idx_size);
2735 wined3d_resource_unmap(ib, 0);
2736 device->index_buffer_pos = ib_pos + idx_size;
2738 hr = wined3d_stateblock_set_stream_source(device->state, 0, device->vertex_buffer, 0, vertex_stride);
2739 if (FAILED(hr))
2740 goto done;
2742 wined3d_stateblock_set_index_buffer(device->state, device->index_buffer,
2743 wined3dformat_from_d3dformat(index_format));
2744 wined3d_stateblock_set_base_vertex_index(device->state, vb_pos / vertex_stride - min_vertex_idx);
2746 wined3d_device_set_primitive_type(device->wined3d_device, wined3d_primitive_type_from_d3d(primitive_type), 0);
2747 wined3d_device_apply_stateblock(device->wined3d_device, device->state);
2748 hr = wined3d_device_draw_indexed_primitive(device->wined3d_device, ib_pos / idx_fmt_size, idx_count);
2750 wined3d_stateblock_set_stream_source(device->state, 0, NULL, 0, 0);
2751 wined3d_stateblock_set_index_buffer(device->state, NULL, WINED3DFMT_UNKNOWN);
2752 wined3d_stateblock_set_base_vertex_index(device->state, 0);
2754 done:
2755 wined3d_mutex_unlock();
2756 return hr;
2759 static HRESULT WINAPI d3d8_device_ProcessVertices(IDirect3DDevice8 *iface, UINT src_start_idx,
2760 UINT dst_idx, UINT vertex_count, IDirect3DVertexBuffer8 *dst_buffer, DWORD flags)
2762 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2763 struct d3d8_vertexbuffer *dst = unsafe_impl_from_IDirect3DVertexBuffer8(dst_buffer);
2764 const struct wined3d_stateblock_state *state;
2765 const struct wined3d_stream_state *stream;
2766 struct d3d8_vertexbuffer *d3d8_buffer;
2767 unsigned int i, map;
2768 HRESULT hr;
2770 TRACE("iface %p, src_start_idx %u, dst_idx %u, vertex_count %u, dst_buffer %p, flags %#x.\n",
2771 iface, src_start_idx, dst_idx, vertex_count, dst_buffer, flags);
2773 wined3d_mutex_lock();
2775 state = device->stateblock_state;
2777 /* Note that an alternative approach would be to simply create these
2778 * buffers with WINED3D_RESOURCE_ACCESS_MAP_R and update them here like we
2779 * do for draws. In some regards that would be easier, but it seems less
2780 * than optimal to upload data to the GPU only to subsequently download it
2781 * again. */
2782 map = device->sysmem_vb;
2783 while (map)
2785 i = wined3d_bit_scan(&map);
2786 stream = &state->streams[i];
2788 d3d8_buffer = wined3d_buffer_get_parent(stream->buffer);
2789 if (FAILED(wined3d_stateblock_set_stream_source(device->state,
2790 i, d3d8_buffer->wined3d_buffer, stream->offset, stream->stride)))
2791 ERR("Failed to set stream source.\n");
2794 wined3d_device_apply_stateblock(device->wined3d_device, device->state);
2795 hr = wined3d_device_process_vertices(device->wined3d_device, src_start_idx, dst_idx,
2796 vertex_count, dst->wined3d_buffer, NULL, flags, dst->fvf);
2798 map = device->sysmem_vb;
2799 while (map)
2801 i = wined3d_bit_scan(&map);
2802 stream = &state->streams[i];
2804 d3d8_buffer = wined3d_buffer_get_parent(stream->buffer);
2805 if (FAILED(wined3d_stateblock_set_stream_source(device->state,
2806 i, d3d8_buffer->draw_buffer, stream->offset, stream->stride)))
2807 ERR("Failed to set stream source.\n");
2810 wined3d_mutex_unlock();
2812 return hr;
2815 static HRESULT WINAPI d3d8_device_CreateVertexShader(IDirect3DDevice8 *iface,
2816 const DWORD *declaration, const DWORD *byte_code, DWORD *shader, DWORD usage)
2818 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2819 struct d3d8_vertex_shader *object;
2820 DWORD shader_handle;
2821 DWORD handle;
2822 HRESULT hr;
2824 TRACE("iface %p, declaration %p, byte_code %p, shader %p, usage %#x.\n",
2825 iface, declaration, byte_code, shader, usage);
2827 if (!(object = heap_alloc_zero(sizeof(*object))))
2829 *shader = 0;
2830 return E_OUTOFMEMORY;
2833 wined3d_mutex_lock();
2834 handle = d3d8_allocate_handle(&device->handle_table, object, D3D8_HANDLE_VS);
2835 wined3d_mutex_unlock();
2836 if (handle == D3D8_INVALID_HANDLE)
2838 ERR("Failed to allocate vertex shader handle.\n");
2839 heap_free(object);
2840 *shader = 0;
2841 return E_OUTOFMEMORY;
2844 shader_handle = handle + VS_HIGHESTFIXEDFXF + 1;
2846 hr = d3d8_vertex_shader_init(object, device, declaration, byte_code, shader_handle, usage);
2847 if (FAILED(hr))
2849 WARN("Failed to initialize vertex shader, hr %#x.\n", hr);
2850 wined3d_mutex_lock();
2851 d3d8_free_handle(&device->handle_table, handle, D3D8_HANDLE_VS);
2852 wined3d_mutex_unlock();
2853 heap_free(object);
2854 *shader = 0;
2855 return hr;
2858 TRACE("Created vertex shader %p (handle %#x).\n", object, shader_handle);
2859 *shader = shader_handle;
2861 return D3D_OK;
2864 static struct d3d8_vertex_declaration *d3d8_device_get_fvf_declaration(struct d3d8_device *device, DWORD fvf)
2866 struct d3d8_vertex_declaration *d3d8_declaration;
2867 struct FvfToDecl *convertedDecls = device->decls;
2868 int p, low, high; /* deliberately signed */
2869 HRESULT hr;
2871 TRACE("Searching for declaration for fvf %08x... ", fvf);
2873 low = 0;
2874 high = device->numConvertedDecls - 1;
2875 while (low <= high)
2877 p = (low + high) >> 1;
2878 TRACE("%d ", p);
2880 if (convertedDecls[p].fvf == fvf)
2882 TRACE("found %p\n", convertedDecls[p].declaration);
2883 return convertedDecls[p].declaration;
2886 if (convertedDecls[p].fvf < fvf)
2887 low = p + 1;
2888 else
2889 high = p - 1;
2891 TRACE("not found. Creating and inserting at position %d.\n", low);
2893 if (!(d3d8_declaration = heap_alloc(sizeof(*d3d8_declaration))))
2894 return NULL;
2896 if (FAILED(hr = d3d8_vertex_declaration_init_fvf(d3d8_declaration, device, fvf)))
2898 WARN("Failed to initialize vertex declaration, hr %#x.\n", hr);
2899 heap_free(d3d8_declaration);
2900 return NULL;
2903 if (device->declArraySize == device->numConvertedDecls)
2905 UINT grow = device->declArraySize / 2;
2907 if (!(convertedDecls = heap_realloc(convertedDecls,
2908 sizeof(*convertedDecls) * (device->numConvertedDecls + grow))))
2910 d3d8_vertex_declaration_destroy(d3d8_declaration);
2911 return NULL;
2913 device->decls = convertedDecls;
2914 device->declArraySize += grow;
2917 memmove(convertedDecls + low + 1, convertedDecls + low,
2918 sizeof(*convertedDecls) * (device->numConvertedDecls - low));
2919 convertedDecls[low].declaration = d3d8_declaration;
2920 convertedDecls[low].fvf = fvf;
2921 ++device->numConvertedDecls;
2923 TRACE("Returning %p. %u decls in array.\n", d3d8_declaration, device->numConvertedDecls);
2925 return d3d8_declaration;
2928 static HRESULT WINAPI d3d8_device_SetVertexShader(IDirect3DDevice8 *iface, DWORD shader)
2930 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2931 struct d3d8_vertex_shader *shader_impl;
2933 TRACE("iface %p, shader %#x.\n", iface, shader);
2935 if (VS_HIGHESTFIXEDFXF >= shader)
2937 TRACE("Setting FVF, %#x\n", shader);
2939 wined3d_mutex_lock();
2940 wined3d_stateblock_set_vertex_declaration(device->update_state,
2941 d3d8_device_get_fvf_declaration(device, shader)->wined3d_vertex_declaration);
2942 wined3d_stateblock_set_vertex_shader(device->update_state, NULL);
2943 wined3d_mutex_unlock();
2945 return D3D_OK;
2948 TRACE("Setting shader\n");
2950 wined3d_mutex_lock();
2951 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS)))
2953 WARN("Invalid handle (%#x) passed.\n", shader);
2954 wined3d_mutex_unlock();
2956 return D3DERR_INVALIDCALL;
2959 wined3d_stateblock_set_vertex_declaration(device->update_state,
2960 shader_impl->vertex_declaration->wined3d_vertex_declaration);
2961 wined3d_stateblock_set_vertex_shader(device->update_state, shader_impl->wined3d_shader);
2962 wined3d_mutex_unlock();
2964 return D3D_OK;
2967 static HRESULT WINAPI d3d8_device_GetVertexShader(IDirect3DDevice8 *iface, DWORD *shader)
2969 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2970 struct wined3d_vertex_declaration *wined3d_declaration;
2971 struct d3d8_vertex_declaration *d3d8_declaration;
2973 TRACE("iface %p, shader %p.\n", iface, shader);
2975 wined3d_mutex_lock();
2976 if ((wined3d_declaration = device->stateblock_state->vertex_declaration))
2978 d3d8_declaration = wined3d_vertex_declaration_get_parent(wined3d_declaration);
2979 *shader = d3d8_declaration->shader_handle;
2981 else
2983 *shader = 0;
2985 wined3d_mutex_unlock();
2987 TRACE("Returning %#x.\n", *shader);
2989 return D3D_OK;
2992 static HRESULT WINAPI d3d8_device_DeleteVertexShader(IDirect3DDevice8 *iface, DWORD shader)
2994 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2995 struct d3d8_vertex_shader *shader_impl;
2997 TRACE("iface %p, shader %#x.\n", iface, shader);
2999 wined3d_mutex_lock();
3000 if (!(shader_impl = d3d8_free_handle(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS)))
3002 WARN("Invalid handle (%#x) passed.\n", shader);
3003 wined3d_mutex_unlock();
3005 return D3DERR_INVALIDCALL;
3008 if (shader_impl->wined3d_shader
3009 && device->stateblock_state->vs == shader_impl->wined3d_shader)
3010 IDirect3DDevice8_SetVertexShader(iface, 0);
3012 wined3d_mutex_unlock();
3014 d3d8_vertex_shader_destroy(shader_impl);
3016 return D3D_OK;
3019 static HRESULT WINAPI d3d8_device_SetVertexShaderConstant(IDirect3DDevice8 *iface,
3020 DWORD start_register, const void *data, DWORD count)
3022 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3023 HRESULT hr;
3025 TRACE("iface %p, start_register %u, data %p, count %u.\n",
3026 iface, start_register, data, count);
3028 if (start_register + count > D3D8_MAX_VERTEX_SHADER_CONSTANTF)
3030 WARN("Trying to access %u constants, but d3d8 only supports %u\n",
3031 start_register + count, D3D8_MAX_VERTEX_SHADER_CONSTANTF);
3032 return D3DERR_INVALIDCALL;
3035 wined3d_mutex_lock();
3036 hr = wined3d_stateblock_set_vs_consts_f(device->update_state, start_register, count, data);
3037 wined3d_mutex_unlock();
3039 return hr;
3042 static HRESULT WINAPI d3d8_device_GetVertexShaderConstant(IDirect3DDevice8 *iface,
3043 DWORD start_idx, void *constants, DWORD count)
3045 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3046 const struct wined3d_vec4 *src;
3048 TRACE("iface %p, start_idx %u, constants %p, count %u.\n", iface, start_idx, constants, count);
3050 if (!constants)
3051 return D3DERR_INVALIDCALL;
3053 if (start_idx >= device->vs_uniform_count || count > device->vs_uniform_count - start_idx)
3055 WARN("Trying to access %u constants, but d3d8 only supports %u.\n",
3056 start_idx + count, device->vs_uniform_count);
3057 return D3DERR_INVALIDCALL;
3060 wined3d_mutex_lock();
3061 src = device->stateblock_state->vs_consts_f;
3062 memcpy(constants, &src[start_idx], count * sizeof(*src));
3063 wined3d_mutex_unlock();
3065 return D3D_OK;
3068 static HRESULT WINAPI d3d8_device_GetVertexShaderDeclaration(IDirect3DDevice8 *iface,
3069 DWORD shader, void *data, DWORD *data_size)
3071 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3072 struct d3d8_vertex_declaration *declaration;
3073 struct d3d8_vertex_shader *shader_impl;
3075 TRACE("iface %p, shader %#x, data %p, data_size %p.\n",
3076 iface, shader, data, data_size);
3078 wined3d_mutex_lock();
3079 shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS);
3080 wined3d_mutex_unlock();
3082 if (!shader_impl)
3084 WARN("Invalid handle (%#x) passed.\n", shader);
3085 return D3DERR_INVALIDCALL;
3087 declaration = shader_impl->vertex_declaration;
3089 if (!data)
3091 *data_size = declaration->elements_size;
3092 return D3D_OK;
3095 /* MSDN claims that if *data_size is smaller than the required size
3096 * we should write the required size and return D3DERR_MOREDATA.
3097 * That's not actually true. */
3098 if (*data_size < declaration->elements_size)
3099 return D3DERR_INVALIDCALL;
3101 memcpy(data, declaration->elements, declaration->elements_size);
3103 return D3D_OK;
3106 static HRESULT WINAPI d3d8_device_GetVertexShaderFunction(IDirect3DDevice8 *iface,
3107 DWORD shader, void *data, DWORD *data_size)
3109 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3110 struct d3d8_vertex_shader *shader_impl = NULL;
3111 HRESULT hr;
3113 TRACE("iface %p, shader %#x, data %p, data_size %p.\n",
3114 iface, shader, data, data_size);
3116 wined3d_mutex_lock();
3117 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS)))
3119 WARN("Invalid handle (%#x) passed.\n", shader);
3120 wined3d_mutex_unlock();
3122 return D3DERR_INVALIDCALL;
3125 if (!shader_impl->wined3d_shader)
3127 wined3d_mutex_unlock();
3128 *data_size = 0;
3129 return D3D_OK;
3132 hr = wined3d_shader_get_byte_code(shader_impl->wined3d_shader, data, data_size);
3133 wined3d_mutex_unlock();
3135 return hr;
3138 static HRESULT WINAPI d3d8_device_SetIndices(IDirect3DDevice8 *iface,
3139 IDirect3DIndexBuffer8 *buffer, UINT base_vertex_idx)
3141 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3142 struct d3d8_indexbuffer *ib = unsafe_impl_from_IDirect3DIndexBuffer8(buffer);
3143 struct wined3d_buffer *wined3d_buffer;
3145 TRACE("iface %p, buffer %p, base_vertex_idx %u.\n", iface, buffer, base_vertex_idx);
3147 if (!ib)
3148 wined3d_buffer = NULL;
3149 else if (ib->draw_buffer)
3150 wined3d_buffer = ib->draw_buffer;
3151 else
3152 wined3d_buffer = ib->wined3d_buffer;
3154 /* WineD3D takes an INT(due to d3d9), but d3d8 uses UINTs. Do I have to add a check here that
3155 * the UINT doesn't cause an overflow in the INT? It seems rather unlikely because such large
3156 * vertex buffers can't be created to address them with an index that requires the 32nd bit
3157 * (4 Byte minimum vertex size * 2^31-1 -> 8 gb buffer. The index sign would be the least
3158 * problem)
3160 wined3d_mutex_lock();
3161 wined3d_stateblock_set_base_vertex_index(device->update_state, base_vertex_idx);
3162 wined3d_stateblock_set_index_buffer(device->update_state, wined3d_buffer, ib ? ib->format : WINED3DFMT_UNKNOWN);
3163 if (!device->recording)
3164 device->sysmem_ib = ib && ib->draw_buffer;
3165 wined3d_mutex_unlock();
3167 return D3D_OK;
3170 static HRESULT WINAPI d3d8_device_GetIndices(IDirect3DDevice8 *iface,
3171 IDirect3DIndexBuffer8 **buffer, UINT *base_vertex_index)
3173 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3174 const struct wined3d_stateblock_state *state;
3175 struct wined3d_buffer *wined3d_buffer;
3176 struct d3d8_indexbuffer *buffer_impl;
3178 TRACE("iface %p, buffer %p, base_vertex_index %p.\n", iface, buffer, base_vertex_index);
3180 if (!buffer)
3181 return D3DERR_INVALIDCALL;
3183 /* The case from UINT to INT is safe because d3d8 will never set negative values */
3184 wined3d_mutex_lock();
3185 state = device->stateblock_state;
3186 *base_vertex_index = state->base_vertex_index;
3187 if ((wined3d_buffer = state->index_buffer))
3189 buffer_impl = wined3d_buffer_get_parent(wined3d_buffer);
3190 *buffer = &buffer_impl->IDirect3DIndexBuffer8_iface;
3191 IDirect3DIndexBuffer8_AddRef(*buffer);
3193 else
3195 *buffer = NULL;
3197 wined3d_mutex_unlock();
3199 return D3D_OK;
3202 static HRESULT WINAPI d3d8_device_CreatePixelShader(IDirect3DDevice8 *iface,
3203 const DWORD *byte_code, DWORD *shader)
3205 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3206 struct d3d8_pixel_shader *object;
3207 DWORD shader_handle;
3208 DWORD handle;
3209 HRESULT hr;
3211 TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
3213 if (!shader)
3214 return D3DERR_INVALIDCALL;
3216 if (!(object = heap_alloc_zero(sizeof(*object))))
3217 return E_OUTOFMEMORY;
3219 wined3d_mutex_lock();
3220 handle = d3d8_allocate_handle(&device->handle_table, object, D3D8_HANDLE_PS);
3221 wined3d_mutex_unlock();
3222 if (handle == D3D8_INVALID_HANDLE)
3224 ERR("Failed to allocate pixel shader handle.\n");
3225 heap_free(object);
3226 return E_OUTOFMEMORY;
3229 shader_handle = handle + VS_HIGHESTFIXEDFXF + 1;
3231 hr = d3d8_pixel_shader_init(object, device, byte_code, shader_handle);
3232 if (FAILED(hr))
3234 WARN("Failed to initialize pixel shader, hr %#x.\n", hr);
3235 wined3d_mutex_lock();
3236 d3d8_free_handle(&device->handle_table, handle, D3D8_HANDLE_PS);
3237 wined3d_mutex_unlock();
3238 heap_free(object);
3239 *shader = 0;
3240 return hr;
3243 TRACE("Created pixel shader %p (handle %#x).\n", object, shader_handle);
3244 *shader = shader_handle;
3246 return D3D_OK;
3249 static HRESULT WINAPI d3d8_device_SetPixelShader(IDirect3DDevice8 *iface, DWORD shader)
3251 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3252 struct d3d8_pixel_shader *shader_impl;
3254 TRACE("iface %p, shader %#x.\n", iface, shader);
3256 wined3d_mutex_lock();
3258 if (!shader)
3260 wined3d_stateblock_set_pixel_shader(device->update_state, NULL);
3261 wined3d_mutex_unlock();
3262 return D3D_OK;
3265 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS)))
3267 WARN("Invalid handle (%#x) passed.\n", shader);
3268 wined3d_mutex_unlock();
3269 return D3DERR_INVALIDCALL;
3272 TRACE("Setting shader %p.\n", shader_impl);
3273 wined3d_stateblock_set_pixel_shader(device->update_state, shader_impl->wined3d_shader);
3274 wined3d_mutex_unlock();
3276 return D3D_OK;
3279 static HRESULT WINAPI d3d8_device_GetPixelShader(IDirect3DDevice8 *iface, DWORD *shader)
3281 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3282 struct wined3d_shader *object;
3284 TRACE("iface %p, shader %p.\n", iface, shader);
3286 if (!shader)
3287 return D3DERR_INVALIDCALL;
3289 wined3d_mutex_lock();
3290 if ((object = device->stateblock_state->ps))
3292 struct d3d8_pixel_shader *d3d8_shader;
3293 d3d8_shader = wined3d_shader_get_parent(object);
3294 *shader = d3d8_shader->handle;
3296 else
3298 *shader = 0;
3300 wined3d_mutex_unlock();
3302 TRACE("Returning %#x.\n", *shader);
3304 return D3D_OK;
3307 static HRESULT WINAPI d3d8_device_DeletePixelShader(IDirect3DDevice8 *iface, DWORD shader)
3309 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3310 struct d3d8_pixel_shader *shader_impl;
3312 TRACE("iface %p, shader %#x.\n", iface, shader);
3314 wined3d_mutex_lock();
3316 if (!(shader_impl = d3d8_free_handle(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS)))
3318 WARN("Invalid handle (%#x) passed.\n", shader);
3319 wined3d_mutex_unlock();
3320 return D3DERR_INVALIDCALL;
3323 if (device->stateblock_state->ps == shader_impl->wined3d_shader)
3324 IDirect3DDevice8_SetPixelShader(iface, 0);
3326 wined3d_mutex_unlock();
3328 d3d8_pixel_shader_destroy(shader_impl);
3330 return D3D_OK;
3333 static HRESULT WINAPI d3d8_device_SetPixelShaderConstant(IDirect3DDevice8 *iface,
3334 DWORD start_register, const void *data, DWORD count)
3336 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3337 HRESULT hr;
3339 TRACE("iface %p, start_register %u, data %p, count %u.\n",
3340 iface, start_register, data, count);
3342 wined3d_mutex_lock();
3343 hr = wined3d_stateblock_set_ps_consts_f(device->update_state, start_register, count, data);
3344 wined3d_mutex_unlock();
3346 return hr;
3349 static HRESULT WINAPI d3d8_device_GetPixelShaderConstant(IDirect3DDevice8 *iface,
3350 DWORD start_idx, void *constants, DWORD count)
3352 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3353 const struct wined3d_vec4 *src;
3355 TRACE("iface %p, start_idx %u, constants %p, count %u.\n", iface, start_idx, constants, count);
3357 if (!constants || start_idx >= D3D8_MAX_PIXEL_SHADER_CONSTANTF
3358 || count > D3D8_MAX_PIXEL_SHADER_CONSTANTF - start_idx)
3359 return WINED3DERR_INVALIDCALL;
3361 wined3d_mutex_lock();
3362 src = device->stateblock_state->ps_consts_f;
3363 memcpy(constants, &src[start_idx], count * sizeof(*src));
3364 wined3d_mutex_unlock();
3366 return D3D_OK;
3369 static HRESULT WINAPI d3d8_device_GetPixelShaderFunction(IDirect3DDevice8 *iface,
3370 DWORD shader, void *data, DWORD *data_size)
3372 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3373 struct d3d8_pixel_shader *shader_impl = NULL;
3374 HRESULT hr;
3376 TRACE("iface %p, shader %#x, data %p, data_size %p.\n",
3377 iface, shader, data, data_size);
3379 wined3d_mutex_lock();
3380 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS)))
3382 WARN("Invalid handle (%#x) passed.\n", shader);
3383 wined3d_mutex_unlock();
3385 return D3DERR_INVALIDCALL;
3388 hr = wined3d_shader_get_byte_code(shader_impl->wined3d_shader, data, data_size);
3389 wined3d_mutex_unlock();
3391 return hr;
3394 static HRESULT WINAPI d3d8_device_DrawRectPatch(IDirect3DDevice8 *iface, UINT handle,
3395 const float *segment_count, const D3DRECTPATCH_INFO *patch_info)
3397 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
3398 iface, handle, segment_count, patch_info);
3399 return D3D_OK;
3402 static HRESULT WINAPI d3d8_device_DrawTriPatch(IDirect3DDevice8 *iface, UINT handle,
3403 const float *segment_count, const D3DTRIPATCH_INFO *patch_info)
3405 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
3406 iface, handle, segment_count, patch_info);
3407 return D3D_OK;
3410 static HRESULT WINAPI d3d8_device_DeletePatch(IDirect3DDevice8 *iface, UINT handle)
3412 FIXME("iface %p, handle %#x unimplemented.\n", iface, handle);
3413 return D3DERR_INVALIDCALL;
3416 static HRESULT WINAPI d3d8_device_SetStreamSource(IDirect3DDevice8 *iface,
3417 UINT stream_idx, IDirect3DVertexBuffer8 *buffer, UINT stride)
3419 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3420 struct d3d8_vertexbuffer *buffer_impl = unsafe_impl_from_IDirect3DVertexBuffer8(buffer);
3421 const struct wined3d_stateblock_state *state;
3422 struct wined3d_buffer *wined3d_buffer;
3423 HRESULT hr;
3425 TRACE("iface %p, stream_idx %u, buffer %p, stride %u.\n",
3426 iface, stream_idx, buffer, stride);
3428 if (stream_idx >= ARRAY_SIZE(state->streams))
3430 WARN("Stream index %u out of range.\n", stream_idx);
3431 return D3DERR_INVALIDCALL;
3434 wined3d_mutex_lock();
3436 if (!buffer_impl)
3438 wined3d_buffer = NULL;
3439 state = device->stateblock_state;
3440 stride = state->streams[stream_idx].stride;
3442 else if (buffer_impl->draw_buffer)
3443 wined3d_buffer = buffer_impl->draw_buffer;
3444 else
3445 wined3d_buffer = buffer_impl->wined3d_buffer;
3447 hr = wined3d_stateblock_set_stream_source(device->update_state, stream_idx, wined3d_buffer, 0, stride);
3448 if (SUCCEEDED(hr) && !device->recording)
3450 if (buffer_impl && buffer_impl->draw_buffer)
3451 device->sysmem_vb |= (1u << stream_idx);
3452 else
3453 device->sysmem_vb &= ~(1u << stream_idx);
3456 wined3d_mutex_unlock();
3458 return hr;
3461 static HRESULT WINAPI d3d8_device_GetStreamSource(IDirect3DDevice8 *iface,
3462 UINT stream_idx, IDirect3DVertexBuffer8 **buffer, UINT *stride)
3464 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3465 const struct wined3d_stateblock_state *state;
3466 const struct wined3d_stream_state *stream;
3467 struct d3d8_vertexbuffer *buffer_impl;
3469 TRACE("iface %p, stream_idx %u, buffer %p, stride %p.\n",
3470 iface, stream_idx, buffer, stride);
3472 if (!buffer)
3473 return D3DERR_INVALIDCALL;
3475 if (stream_idx >= ARRAY_SIZE(state->streams))
3477 WARN("Stream index %u out of range.\n", stream_idx);
3478 return D3DERR_INVALIDCALL;
3481 wined3d_mutex_lock();
3482 state = device->stateblock_state;
3483 stream = &state->streams[stream_idx];
3484 if (stream->buffer)
3486 buffer_impl = wined3d_buffer_get_parent(stream->buffer);
3487 *buffer = &buffer_impl->IDirect3DVertexBuffer8_iface;
3488 IDirect3DVertexBuffer8_AddRef(*buffer);
3490 else
3491 *buffer = NULL;
3492 *stride = stream->stride;
3493 wined3d_mutex_unlock();
3495 return D3D_OK;
3498 static const struct IDirect3DDevice8Vtbl d3d8_device_vtbl =
3500 d3d8_device_QueryInterface,
3501 d3d8_device_AddRef,
3502 d3d8_device_Release,
3503 d3d8_device_TestCooperativeLevel,
3504 d3d8_device_GetAvailableTextureMem,
3505 d3d8_device_ResourceManagerDiscardBytes,
3506 d3d8_device_GetDirect3D,
3507 d3d8_device_GetDeviceCaps,
3508 d3d8_device_GetDisplayMode,
3509 d3d8_device_GetCreationParameters,
3510 d3d8_device_SetCursorProperties,
3511 d3d8_device_SetCursorPosition,
3512 d3d8_device_ShowCursor,
3513 d3d8_device_CreateAdditionalSwapChain,
3514 d3d8_device_Reset,
3515 d3d8_device_Present,
3516 d3d8_device_GetBackBuffer,
3517 d3d8_device_GetRasterStatus,
3518 d3d8_device_SetGammaRamp,
3519 d3d8_device_GetGammaRamp,
3520 d3d8_device_CreateTexture,
3521 d3d8_device_CreateVolumeTexture,
3522 d3d8_device_CreateCubeTexture,
3523 d3d8_device_CreateVertexBuffer,
3524 d3d8_device_CreateIndexBuffer,
3525 d3d8_device_CreateRenderTarget,
3526 d3d8_device_CreateDepthStencilSurface,
3527 d3d8_device_CreateImageSurface,
3528 d3d8_device_CopyRects,
3529 d3d8_device_UpdateTexture,
3530 d3d8_device_GetFrontBuffer,
3531 d3d8_device_SetRenderTarget,
3532 d3d8_device_GetRenderTarget,
3533 d3d8_device_GetDepthStencilSurface,
3534 d3d8_device_BeginScene,
3535 d3d8_device_EndScene,
3536 d3d8_device_Clear,
3537 d3d8_device_SetTransform,
3538 d3d8_device_GetTransform,
3539 d3d8_device_MultiplyTransform,
3540 d3d8_device_SetViewport,
3541 d3d8_device_GetViewport,
3542 d3d8_device_SetMaterial,
3543 d3d8_device_GetMaterial,
3544 d3d8_device_SetLight,
3545 d3d8_device_GetLight,
3546 d3d8_device_LightEnable,
3547 d3d8_device_GetLightEnable,
3548 d3d8_device_SetClipPlane,
3549 d3d8_device_GetClipPlane,
3550 d3d8_device_SetRenderState,
3551 d3d8_device_GetRenderState,
3552 d3d8_device_BeginStateBlock,
3553 d3d8_device_EndStateBlock,
3554 d3d8_device_ApplyStateBlock,
3555 d3d8_device_CaptureStateBlock,
3556 d3d8_device_DeleteStateBlock,
3557 d3d8_device_CreateStateBlock,
3558 d3d8_device_SetClipStatus,
3559 d3d8_device_GetClipStatus,
3560 d3d8_device_GetTexture,
3561 d3d8_device_SetTexture,
3562 d3d8_device_GetTextureStageState,
3563 d3d8_device_SetTextureStageState,
3564 d3d8_device_ValidateDevice,
3565 d3d8_device_GetInfo,
3566 d3d8_device_SetPaletteEntries,
3567 d3d8_device_GetPaletteEntries,
3568 d3d8_device_SetCurrentTexturePalette,
3569 d3d8_device_GetCurrentTexturePalette,
3570 d3d8_device_DrawPrimitive,
3571 d3d8_device_DrawIndexedPrimitive,
3572 d3d8_device_DrawPrimitiveUP,
3573 d3d8_device_DrawIndexedPrimitiveUP,
3574 d3d8_device_ProcessVertices,
3575 d3d8_device_CreateVertexShader,
3576 d3d8_device_SetVertexShader,
3577 d3d8_device_GetVertexShader,
3578 d3d8_device_DeleteVertexShader,
3579 d3d8_device_SetVertexShaderConstant,
3580 d3d8_device_GetVertexShaderConstant,
3581 d3d8_device_GetVertexShaderDeclaration,
3582 d3d8_device_GetVertexShaderFunction,
3583 d3d8_device_SetStreamSource,
3584 d3d8_device_GetStreamSource,
3585 d3d8_device_SetIndices,
3586 d3d8_device_GetIndices,
3587 d3d8_device_CreatePixelShader,
3588 d3d8_device_SetPixelShader,
3589 d3d8_device_GetPixelShader,
3590 d3d8_device_DeletePixelShader,
3591 d3d8_device_SetPixelShaderConstant,
3592 d3d8_device_GetPixelShaderConstant,
3593 d3d8_device_GetPixelShaderFunction,
3594 d3d8_device_DrawRectPatch,
3595 d3d8_device_DrawTriPatch,
3596 d3d8_device_DeletePatch,
3599 static inline struct d3d8_device *device_from_device_parent(struct wined3d_device_parent *device_parent)
3601 return CONTAINING_RECORD(device_parent, struct d3d8_device, device_parent);
3604 static void CDECL device_parent_wined3d_device_created(struct wined3d_device_parent *device_parent,
3605 struct wined3d_device *device)
3607 TRACE("device_parent %p, device %p\n", device_parent, device);
3610 static void CDECL device_parent_mode_changed(struct wined3d_device_parent *device_parent)
3612 TRACE("device_parent %p.\n", device_parent);
3615 static void CDECL device_parent_activate(struct wined3d_device_parent *device_parent, BOOL activate)
3617 struct d3d8_device *device = device_from_device_parent(device_parent);
3619 TRACE("device_parent %p, activate %#x.\n", device_parent, activate);
3621 if (!activate)
3622 InterlockedCompareExchange(&device->device_state, D3D8_DEVICE_STATE_LOST, D3D8_DEVICE_STATE_OK);
3623 else
3624 InterlockedCompareExchange(&device->device_state, D3D8_DEVICE_STATE_NOT_RESET, D3D8_DEVICE_STATE_LOST);
3627 static HRESULT CDECL device_parent_texture_sub_resource_created(struct wined3d_device_parent *device_parent,
3628 enum wined3d_resource_type type, struct wined3d_texture *wined3d_texture, unsigned int sub_resource_idx,
3629 void **parent, const struct wined3d_parent_ops **parent_ops)
3631 TRACE("device_parent %p, type %#x, wined3d_texture %p, sub_resource_idx %u, parent %p, parent_ops %p.\n",
3632 device_parent, type, wined3d_texture, sub_resource_idx, parent, parent_ops);
3634 if (type == WINED3D_RTYPE_TEXTURE_2D)
3636 struct d3d8_surface *d3d_surface;
3638 if (!(d3d_surface = heap_alloc_zero(sizeof(*d3d_surface))))
3639 return E_OUTOFMEMORY;
3641 surface_init(d3d_surface, wined3d_texture, sub_resource_idx, parent_ops);
3642 *parent = d3d_surface;
3643 TRACE("Created surface %p.\n", d3d_surface);
3645 else if (type == WINED3D_RTYPE_TEXTURE_3D)
3647 struct d3d8_volume *d3d_volume;
3649 if (!(d3d_volume = heap_alloc_zero(sizeof(*d3d_volume))))
3650 return E_OUTOFMEMORY;
3652 volume_init(d3d_volume, wined3d_texture, sub_resource_idx, parent_ops);
3653 *parent = d3d_volume;
3654 TRACE("Created volume %p.\n", d3d_volume);
3656 else
3658 ERR("Unhandled resource type %#x.\n", type);
3659 return E_FAIL;
3662 return D3D_OK;
3665 static HRESULT CDECL device_parent_create_swapchain_texture(struct wined3d_device_parent *device_parent,
3666 void *container_parent, const struct wined3d_resource_desc *desc, DWORD texture_flags,
3667 struct wined3d_texture **texture)
3669 struct d3d8_device *device = device_from_device_parent(device_parent);
3670 struct d3d8_surface *d3d_surface;
3671 HRESULT hr;
3673 TRACE("device_parent %p, container_parent %p, desc %p, texture flags %#x, texture %p.\n",
3674 device_parent, container_parent, desc, texture_flags, texture);
3676 if (FAILED(hr = wined3d_texture_create(device->wined3d_device, desc, 1, 1, texture_flags,
3677 NULL, &device->IDirect3DDevice8_iface, &d3d8_null_wined3d_parent_ops, texture)))
3679 WARN("Failed to create texture, hr %#x.\n", hr);
3680 return hr;
3683 d3d_surface = wined3d_texture_get_sub_resource_parent(*texture, 0);
3684 d3d_surface->parent_device = &device->IDirect3DDevice8_iface;
3686 return hr;
3689 static const struct wined3d_device_parent_ops d3d8_wined3d_device_parent_ops =
3691 device_parent_wined3d_device_created,
3692 device_parent_mode_changed,
3693 device_parent_activate,
3694 device_parent_texture_sub_resource_created,
3695 device_parent_create_swapchain_texture,
3698 static void setup_fpu(void)
3700 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
3701 WORD cw;
3702 __asm__ volatile ("fnstcw %0" : "=m" (cw));
3703 cw = (cw & ~0xf3f) | 0x3f;
3704 __asm__ volatile ("fldcw %0" : : "m" (cw));
3705 #elif defined(__i386__) && defined(_MSC_VER)
3706 WORD cw;
3707 __asm fnstcw cw;
3708 cw = (cw & ~0xf3f) | 0x3f;
3709 __asm fldcw cw;
3710 #else
3711 FIXME("FPU setup not implemented for this platform.\n");
3712 #endif
3715 HRESULT device_init(struct d3d8_device *device, struct d3d8 *parent, struct wined3d *wined3d, UINT adapter,
3716 D3DDEVTYPE device_type, HWND focus_window, DWORD flags, D3DPRESENT_PARAMETERS *parameters)
3718 struct wined3d_swapchain_desc swapchain_desc;
3719 struct wined3d_swapchain *wined3d_swapchain;
3720 struct wined3d_adapter *wined3d_adapter;
3721 struct d3d8_swapchain *d3d_swapchain;
3722 struct wined3d_caps caps;
3723 unsigned int output_idx;
3724 HRESULT hr;
3726 static const enum wined3d_feature_level feature_levels[] =
3728 WINED3D_FEATURE_LEVEL_8,
3729 WINED3D_FEATURE_LEVEL_7,
3730 WINED3D_FEATURE_LEVEL_6,
3731 WINED3D_FEATURE_LEVEL_5,
3734 output_idx = adapter;
3735 if (output_idx >= parent->wined3d_output_count)
3736 return D3DERR_INVALIDCALL;
3738 device->IDirect3DDevice8_iface.lpVtbl = &d3d8_device_vtbl;
3739 device->device_parent.ops = &d3d8_wined3d_device_parent_ops;
3740 device->adapter_ordinal = adapter;
3741 device->ref = 1;
3742 if (!(device->handle_table.entries = heap_alloc_zero(D3D8_INITIAL_HANDLE_TABLE_SIZE
3743 * sizeof(*device->handle_table.entries))))
3745 ERR("Failed to allocate handle table memory.\n");
3746 return E_OUTOFMEMORY;
3748 device->handle_table.table_size = D3D8_INITIAL_HANDLE_TABLE_SIZE;
3750 if (!(flags & D3DCREATE_FPU_PRESERVE)) setup_fpu();
3752 wined3d_mutex_lock();
3753 wined3d_adapter = wined3d_output_get_adapter(parent->wined3d_outputs[output_idx]);
3754 if (FAILED(hr = wined3d_device_create(wined3d, wined3d_adapter, wined3d_device_type_from_d3d(device_type),
3755 focus_window, flags, 4, feature_levels, ARRAY_SIZE(feature_levels),
3756 &device->device_parent, &device->wined3d_device)))
3758 WARN("Failed to create wined3d device, hr %#x.\n", hr);
3759 wined3d_mutex_unlock();
3760 heap_free(device->handle_table.entries);
3761 return hr;
3764 wined3d_get_device_caps(wined3d_adapter, wined3d_device_type_from_d3d(device_type), &caps);
3765 device->max_user_clip_planes = caps.MaxUserClipPlanes;
3766 device->vs_uniform_count = caps.MaxVertexShaderConst;
3768 if (FAILED(hr = wined3d_stateblock_create(device->wined3d_device, NULL, WINED3D_SBT_PRIMARY, &device->state)))
3770 ERR("Failed to create primary stateblock, hr %#x.\n", hr);
3771 wined3d_device_decref(device->wined3d_device);
3772 wined3d_mutex_unlock();
3773 return hr;
3775 device->stateblock_state = wined3d_stateblock_get_state(device->state);
3776 device->update_state = device->state;
3778 if (!parameters->Windowed)
3780 if (!focus_window)
3781 focus_window = parameters->hDeviceWindow;
3782 if (FAILED(hr = wined3d_device_acquire_focus_window(device->wined3d_device, focus_window)))
3784 ERR("Failed to acquire focus window, hr %#x.\n", hr);
3785 wined3d_device_decref(device->wined3d_device);
3786 wined3d_mutex_unlock();
3787 heap_free(device->handle_table.entries);
3788 return hr;
3792 if (flags & D3DCREATE_MULTITHREADED)
3793 wined3d_device_set_multithreaded(device->wined3d_device);
3795 if (!wined3d_swapchain_desc_from_present_parameters(&swapchain_desc, parameters))
3797 wined3d_device_release_focus_window(device->wined3d_device);
3798 wined3d_device_decref(device->wined3d_device);
3799 wined3d_mutex_unlock();
3800 heap_free(device->handle_table.entries);
3801 return D3DERR_INVALIDCALL;
3803 swapchain_desc.flags |= WINED3D_SWAPCHAIN_IMPLICIT;
3805 if (FAILED(hr = d3d8_swapchain_create(device, &swapchain_desc,
3806 wined3dswapinterval_from_d3d(parameters->FullScreen_PresentationInterval), &d3d_swapchain)))
3808 WARN("Failed to create implicit swapchain, hr %#x.\n", hr);
3809 wined3d_device_release_focus_window(device->wined3d_device);
3810 wined3d_device_decref(device->wined3d_device);
3811 wined3d_mutex_unlock();
3812 heap_free(device->handle_table.entries);
3813 return hr;
3816 wined3d_swapchain = d3d_swapchain->wined3d_swapchain;
3817 wined3d_swapchain_incref(wined3d_swapchain);
3818 IDirect3DSwapChain8_Release(&d3d_swapchain->IDirect3DSwapChain8_iface);
3820 wined3d_stateblock_set_render_state(device->state, WINED3D_RS_ZENABLE,
3821 !!swapchain_desc.enable_auto_depth_stencil);
3822 wined3d_stateblock_set_render_state(device->state, WINED3D_RS_POINTSIZE_MIN, 0);
3823 device_reset_viewport_state(device);
3824 wined3d_mutex_unlock();
3826 present_parameters_from_wined3d_swapchain_desc(parameters,
3827 &swapchain_desc, parameters->FullScreen_PresentationInterval);
3829 device->declArraySize = 16;
3830 if (!(device->decls = heap_alloc(device->declArraySize * sizeof(*device->decls))))
3832 ERR("Failed to allocate FVF vertex declaration map memory.\n");
3833 hr = E_OUTOFMEMORY;
3834 goto err;
3837 device->implicit_swapchain = wined3d_swapchain;
3839 device->d3d_parent = parent;
3840 IDirect3D8_AddRef(&parent->IDirect3D8_iface);
3842 return D3D_OK;
3844 err:
3845 wined3d_mutex_lock();
3846 wined3d_swapchain_decref(wined3d_swapchain);
3847 wined3d_device_release_focus_window(device->wined3d_device);
3848 wined3d_device_decref(device->wined3d_device);
3849 wined3d_mutex_unlock();
3850 heap_free(device->handle_table.entries);
3851 return hr;