mstask: Use wide-char string literals.
[wine.git] / dlls / d3d8 / device.c
blobf4bdb7482754d76d936fb29375cc30bb0de4fb0d
1 /*
2 * IDirect3DDevice8 implementation
4 * Copyright 2002-2004 Jason Edmeades
5 * Copyright 2004 Christian Costa
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "d3d8_private.h"
24 WINE_DEFAULT_DEBUG_CHANNEL(d3d8);
26 static void STDMETHODCALLTYPE d3d8_null_wined3d_object_destroyed(void *parent) {}
28 const struct wined3d_parent_ops d3d8_null_wined3d_parent_ops =
30 d3d8_null_wined3d_object_destroyed,
33 D3DFORMAT d3dformat_from_wined3dformat(enum wined3d_format_id format)
35 BYTE *c = (BYTE *)&format;
37 /* Don't translate FOURCC formats */
38 if (isprint(c[0]) && isprint(c[1]) && isprint(c[2]) && isprint(c[3]))
39 return (D3DFORMAT)format;
41 switch(format)
43 case WINED3DFMT_UNKNOWN: return D3DFMT_UNKNOWN;
44 case WINED3DFMT_B8G8R8_UNORM: return D3DFMT_R8G8B8;
45 case WINED3DFMT_B8G8R8A8_UNORM: return D3DFMT_A8R8G8B8;
46 case WINED3DFMT_B8G8R8X8_UNORM: return D3DFMT_X8R8G8B8;
47 case WINED3DFMT_B5G6R5_UNORM: return D3DFMT_R5G6B5;
48 case WINED3DFMT_B5G5R5X1_UNORM: return D3DFMT_X1R5G5B5;
49 case WINED3DFMT_B5G5R5A1_UNORM: return D3DFMT_A1R5G5B5;
50 case WINED3DFMT_B4G4R4A4_UNORM: return D3DFMT_A4R4G4B4;
51 case WINED3DFMT_B2G3R3_UNORM: return D3DFMT_R3G3B2;
52 case WINED3DFMT_A8_UNORM: return D3DFMT_A8;
53 case WINED3DFMT_B2G3R3A8_UNORM: return D3DFMT_A8R3G3B2;
54 case WINED3DFMT_B4G4R4X4_UNORM: return D3DFMT_X4R4G4B4;
55 case WINED3DFMT_R10G10B10A2_UNORM: return D3DFMT_A2B10G10R10;
56 case WINED3DFMT_R16G16_UNORM: return D3DFMT_G16R16;
57 case WINED3DFMT_P8_UINT_A8_UNORM: return D3DFMT_A8P8;
58 case WINED3DFMT_P8_UINT: return D3DFMT_P8;
59 case WINED3DFMT_L8_UNORM: return D3DFMT_L8;
60 case WINED3DFMT_L8A8_UNORM: return D3DFMT_A8L8;
61 case WINED3DFMT_L4A4_UNORM: return D3DFMT_A4L4;
62 case WINED3DFMT_R8G8_SNORM: return D3DFMT_V8U8;
63 case WINED3DFMT_R5G5_SNORM_L6_UNORM: return D3DFMT_L6V5U5;
64 case WINED3DFMT_R8G8_SNORM_L8X8_UNORM: return D3DFMT_X8L8V8U8;
65 case WINED3DFMT_R8G8B8A8_SNORM: return D3DFMT_Q8W8V8U8;
66 case WINED3DFMT_R16G16_SNORM: return D3DFMT_V16U16;
67 case WINED3DFMT_R10G11B11_SNORM: return D3DFMT_W11V11U10;
68 case WINED3DFMT_R10G10B10_SNORM_A2_UNORM: return D3DFMT_A2W10V10U10;
69 case WINED3DFMT_D16_LOCKABLE: return D3DFMT_D16_LOCKABLE;
70 case WINED3DFMT_D32_UNORM: return D3DFMT_D32;
71 case WINED3DFMT_S1_UINT_D15_UNORM: return D3DFMT_D15S1;
72 case WINED3DFMT_D24_UNORM_S8_UINT: return D3DFMT_D24S8;
73 case WINED3DFMT_X8D24_UNORM: return D3DFMT_D24X8;
74 case WINED3DFMT_S4X4_UINT_D24_UNORM: return D3DFMT_D24X4S4;
75 case WINED3DFMT_D16_UNORM: return D3DFMT_D16;
76 case WINED3DFMT_R16_UINT: return D3DFMT_INDEX16;
77 case WINED3DFMT_R32_UINT: return D3DFMT_INDEX32;
78 default:
79 FIXME("Unhandled wined3d format %#x.\n", format);
80 return D3DFMT_UNKNOWN;
84 enum wined3d_format_id wined3dformat_from_d3dformat(D3DFORMAT format)
86 BYTE *c = (BYTE *)&format;
88 /* Don't translate FOURCC formats */
89 if (isprint(c[0]) && isprint(c[1]) && isprint(c[2]) && isprint(c[3]))
90 return (enum wined3d_format_id)format;
92 switch(format)
94 case D3DFMT_UNKNOWN: return WINED3DFMT_UNKNOWN;
95 case D3DFMT_R8G8B8: return WINED3DFMT_B8G8R8_UNORM;
96 case D3DFMT_A8R8G8B8: return WINED3DFMT_B8G8R8A8_UNORM;
97 case D3DFMT_X8R8G8B8: return WINED3DFMT_B8G8R8X8_UNORM;
98 case D3DFMT_R5G6B5: return WINED3DFMT_B5G6R5_UNORM;
99 case D3DFMT_X1R5G5B5: return WINED3DFMT_B5G5R5X1_UNORM;
100 case D3DFMT_A1R5G5B5: return WINED3DFMT_B5G5R5A1_UNORM;
101 case D3DFMT_A4R4G4B4: return WINED3DFMT_B4G4R4A4_UNORM;
102 case D3DFMT_R3G3B2: return WINED3DFMT_B2G3R3_UNORM;
103 case D3DFMT_A8: return WINED3DFMT_A8_UNORM;
104 case D3DFMT_A8R3G3B2: return WINED3DFMT_B2G3R3A8_UNORM;
105 case D3DFMT_X4R4G4B4: return WINED3DFMT_B4G4R4X4_UNORM;
106 case D3DFMT_A2B10G10R10: return WINED3DFMT_R10G10B10A2_UNORM;
107 case D3DFMT_G16R16: return WINED3DFMT_R16G16_UNORM;
108 case D3DFMT_A8P8: return WINED3DFMT_P8_UINT_A8_UNORM;
109 case D3DFMT_P8: return WINED3DFMT_P8_UINT;
110 case D3DFMT_L8: return WINED3DFMT_L8_UNORM;
111 case D3DFMT_A8L8: return WINED3DFMT_L8A8_UNORM;
112 case D3DFMT_A4L4: return WINED3DFMT_L4A4_UNORM;
113 case D3DFMT_V8U8: return WINED3DFMT_R8G8_SNORM;
114 case D3DFMT_L6V5U5: return WINED3DFMT_R5G5_SNORM_L6_UNORM;
115 case D3DFMT_X8L8V8U8: return WINED3DFMT_R8G8_SNORM_L8X8_UNORM;
116 case D3DFMT_Q8W8V8U8: return WINED3DFMT_R8G8B8A8_SNORM;
117 case D3DFMT_V16U16: return WINED3DFMT_R16G16_SNORM;
118 case D3DFMT_W11V11U10: return WINED3DFMT_R10G11B11_SNORM;
119 case D3DFMT_A2W10V10U10: return WINED3DFMT_R10G10B10_SNORM_A2_UNORM;
120 case D3DFMT_D16_LOCKABLE: return WINED3DFMT_D16_LOCKABLE;
121 case D3DFMT_D32: return WINED3DFMT_D32_UNORM;
122 case D3DFMT_D15S1: return WINED3DFMT_S1_UINT_D15_UNORM;
123 case D3DFMT_D24S8: return WINED3DFMT_D24_UNORM_S8_UINT;
124 case D3DFMT_D24X8: return WINED3DFMT_X8D24_UNORM;
125 case D3DFMT_D24X4S4: return WINED3DFMT_S4X4_UINT_D24_UNORM;
126 case D3DFMT_D16: return WINED3DFMT_D16_UNORM;
127 case D3DFMT_INDEX16: return WINED3DFMT_R16_UINT;
128 case D3DFMT_INDEX32: return WINED3DFMT_R32_UINT;
129 default:
130 FIXME("Unhandled D3DFORMAT %#x.\n", format);
131 return WINED3DFMT_UNKNOWN;
135 unsigned int wined3dmapflags_from_d3dmapflags(unsigned int flags, unsigned int usage)
137 static const unsigned int handled = D3DLOCK_NOSYSLOCK
138 | D3DLOCK_NOOVERWRITE
139 | D3DLOCK_DISCARD
140 | D3DLOCK_NO_DIRTY_UPDATE;
141 unsigned int wined3d_flags;
143 wined3d_flags = flags & handled;
144 if (~usage & D3DUSAGE_WRITEONLY && !(flags & (D3DLOCK_NOOVERWRITE | D3DLOCK_DISCARD)))
145 wined3d_flags |= WINED3D_MAP_READ;
146 if (!(flags & D3DLOCK_READONLY))
147 wined3d_flags |= WINED3D_MAP_WRITE;
148 if (!(wined3d_flags & (WINED3D_MAP_READ | WINED3D_MAP_WRITE)))
149 wined3d_flags |= WINED3D_MAP_WRITE;
150 flags &= ~(handled | D3DLOCK_READONLY);
152 if (flags)
153 FIXME("Unhandled flags %#x.\n", flags);
155 return wined3d_flags;
158 static UINT vertex_count_from_primitive_count(D3DPRIMITIVETYPE primitive_type, UINT primitive_count)
160 switch (primitive_type)
162 case D3DPT_POINTLIST:
163 return primitive_count;
165 case D3DPT_LINELIST:
166 return primitive_count * 2;
168 case D3DPT_LINESTRIP:
169 return primitive_count + 1;
171 case D3DPT_TRIANGLELIST:
172 return primitive_count * 3;
174 case D3DPT_TRIANGLESTRIP:
175 case D3DPT_TRIANGLEFAN:
176 return primitive_count + 2;
178 default:
179 FIXME("Unhandled primitive type %#x.\n", primitive_type);
180 return 0;
184 static D3DSWAPEFFECT d3dswapeffect_from_wined3dswapeffect(enum wined3d_swap_effect effect)
186 switch (effect)
188 case WINED3D_SWAP_EFFECT_DISCARD:
189 return D3DSWAPEFFECT_DISCARD;
190 case WINED3D_SWAP_EFFECT_SEQUENTIAL:
191 return D3DSWAPEFFECT_FLIP;
192 case WINED3D_SWAP_EFFECT_COPY:
193 return D3DSWAPEFFECT_COPY;
194 case WINED3D_SWAP_EFFECT_COPY_VSYNC:
195 return D3DSWAPEFFECT_COPY_VSYNC;
196 default:
197 FIXME("Unhandled swap effect %#x.\n", effect);
198 return D3DSWAPEFFECT_FLIP;
202 static void present_parameters_from_wined3d_swapchain_desc(D3DPRESENT_PARAMETERS *present_parameters,
203 const struct wined3d_swapchain_desc *swapchain_desc, DWORD presentation_interval)
205 present_parameters->BackBufferWidth = swapchain_desc->backbuffer_width;
206 present_parameters->BackBufferHeight = swapchain_desc->backbuffer_height;
207 present_parameters->BackBufferFormat = d3dformat_from_wined3dformat(swapchain_desc->backbuffer_format);
208 present_parameters->BackBufferCount = swapchain_desc->backbuffer_count;
209 present_parameters->MultiSampleType = d3dmultisample_type_from_wined3d(swapchain_desc->multisample_type);
210 present_parameters->SwapEffect = d3dswapeffect_from_wined3dswapeffect(swapchain_desc->swap_effect);
211 present_parameters->hDeviceWindow = swapchain_desc->device_window;
212 present_parameters->Windowed = swapchain_desc->windowed;
213 present_parameters->EnableAutoDepthStencil = swapchain_desc->enable_auto_depth_stencil;
214 present_parameters->AutoDepthStencilFormat
215 = d3dformat_from_wined3dformat(swapchain_desc->auto_depth_stencil_format);
216 present_parameters->Flags = swapchain_desc->flags & D3DPRESENTFLAGS_MASK;
217 present_parameters->FullScreen_RefreshRateInHz = swapchain_desc->refresh_rate;
218 present_parameters->FullScreen_PresentationInterval = presentation_interval;
221 static enum wined3d_swap_effect wined3dswapeffect_from_d3dswapeffect(D3DSWAPEFFECT effect)
223 switch (effect)
225 case D3DSWAPEFFECT_DISCARD:
226 return WINED3D_SWAP_EFFECT_DISCARD;
227 case D3DSWAPEFFECT_FLIP:
228 return WINED3D_SWAP_EFFECT_SEQUENTIAL;
229 case D3DSWAPEFFECT_COPY:
230 return WINED3D_SWAP_EFFECT_COPY;
231 case D3DSWAPEFFECT_COPY_VSYNC:
232 return WINED3D_SWAP_EFFECT_COPY_VSYNC;
233 default:
234 FIXME("Unhandled swap effect %#x.\n", effect);
235 return WINED3D_SWAP_EFFECT_SEQUENTIAL;
239 static enum wined3d_swap_interval wined3dswapinterval_from_d3d(DWORD interval)
241 switch (interval)
243 case D3DPRESENT_INTERVAL_IMMEDIATE:
244 return WINED3D_SWAP_INTERVAL_IMMEDIATE;
245 case D3DPRESENT_INTERVAL_ONE:
246 return WINED3D_SWAP_INTERVAL_ONE;
247 case D3DPRESENT_INTERVAL_TWO:
248 return WINED3D_SWAP_INTERVAL_TWO;
249 case D3DPRESENT_INTERVAL_THREE:
250 return WINED3D_SWAP_INTERVAL_THREE;
251 case D3DPRESENT_INTERVAL_FOUR:
252 return WINED3D_SWAP_INTERVAL_FOUR;
253 default:
254 FIXME("Unhandled presentation interval %#x.\n", interval);
255 case D3DPRESENT_INTERVAL_DEFAULT:
256 return WINED3D_SWAP_INTERVAL_DEFAULT;
260 static enum wined3d_multisample_type wined3d_multisample_type_from_d3d(D3DMULTISAMPLE_TYPE type)
262 return (enum wined3d_multisample_type)type;
265 static BOOL wined3d_swapchain_desc_from_d3d8(struct wined3d_swapchain_desc *swapchain_desc,
266 struct wined3d_output *output, const D3DPRESENT_PARAMETERS *present_parameters)
268 if (!present_parameters->SwapEffect || present_parameters->SwapEffect > D3DSWAPEFFECT_COPY_VSYNC)
270 WARN("Invalid swap effect %u passed.\n", present_parameters->SwapEffect);
271 return FALSE;
273 if (present_parameters->BackBufferCount > 3
274 || ((present_parameters->SwapEffect == D3DSWAPEFFECT_COPY
275 || present_parameters->SwapEffect == D3DSWAPEFFECT_COPY_VSYNC)
276 && present_parameters->BackBufferCount > 1))
278 WARN("Invalid backbuffer count %u.\n", present_parameters->BackBufferCount);
279 return FALSE;
281 switch (present_parameters->FullScreen_PresentationInterval)
283 case D3DPRESENT_INTERVAL_DEFAULT:
284 case D3DPRESENT_INTERVAL_ONE:
285 case D3DPRESENT_INTERVAL_TWO:
286 case D3DPRESENT_INTERVAL_THREE:
287 case D3DPRESENT_INTERVAL_FOUR:
288 case D3DPRESENT_INTERVAL_IMMEDIATE:
289 break;
290 default:
291 WARN("Invalid presentation interval %#x.\n",
292 present_parameters->FullScreen_PresentationInterval);
293 return FALSE;
296 swapchain_desc->output = output;
297 swapchain_desc->backbuffer_width = present_parameters->BackBufferWidth;
298 swapchain_desc->backbuffer_height = present_parameters->BackBufferHeight;
299 swapchain_desc->backbuffer_format = wined3dformat_from_d3dformat(present_parameters->BackBufferFormat);
300 swapchain_desc->backbuffer_count = max(1, present_parameters->BackBufferCount);
301 swapchain_desc->backbuffer_bind_flags = WINED3D_BIND_RENDER_TARGET;
302 swapchain_desc->multisample_type = wined3d_multisample_type_from_d3d(present_parameters->MultiSampleType);
303 swapchain_desc->multisample_quality = 0; /* d3d9 only */
304 swapchain_desc->swap_effect = wined3dswapeffect_from_d3dswapeffect(present_parameters->SwapEffect);
305 swapchain_desc->device_window = present_parameters->hDeviceWindow;
306 swapchain_desc->windowed = present_parameters->Windowed;
307 swapchain_desc->enable_auto_depth_stencil = present_parameters->EnableAutoDepthStencil;
308 swapchain_desc->auto_depth_stencil_format
309 = wined3dformat_from_d3dformat(present_parameters->AutoDepthStencilFormat);
310 swapchain_desc->flags
311 = (present_parameters->Flags & D3DPRESENTFLAGS_MASK) | WINED3D_SWAPCHAIN_ALLOW_MODE_SWITCH;
312 swapchain_desc->refresh_rate = present_parameters->FullScreen_RefreshRateInHz;
313 swapchain_desc->auto_restore_display_mode = TRUE;
315 if (present_parameters->Flags & ~D3DPRESENTFLAGS_MASK)
316 FIXME("Unhandled flags %#x.\n", present_parameters->Flags & ~D3DPRESENTFLAGS_MASK);
318 return TRUE;
321 void d3dcaps_from_wined3dcaps(D3DCAPS8 *caps, const struct wined3d_caps *wined3d_caps,
322 unsigned int adapter_ordinal)
324 caps->DeviceType = (D3DDEVTYPE)wined3d_caps->DeviceType;
325 caps->AdapterOrdinal = adapter_ordinal;
326 caps->Caps = wined3d_caps->Caps;
327 caps->Caps2 = wined3d_caps->Caps2;
328 caps->Caps3 = wined3d_caps->Caps3;
329 caps->PresentationIntervals = D3DPRESENT_INTERVAL_IMMEDIATE | D3DPRESENT_INTERVAL_ONE;
330 caps->CursorCaps = wined3d_caps->CursorCaps;
331 caps->DevCaps = wined3d_caps->DevCaps;
332 caps->PrimitiveMiscCaps = wined3d_caps->PrimitiveMiscCaps;
333 caps->RasterCaps = wined3d_caps->RasterCaps;
334 caps->ZCmpCaps = wined3d_caps->ZCmpCaps;
335 caps->SrcBlendCaps = wined3d_caps->SrcBlendCaps;
336 caps->DestBlendCaps = wined3d_caps->DestBlendCaps;
337 caps->AlphaCmpCaps = wined3d_caps->AlphaCmpCaps;
338 caps->ShadeCaps = wined3d_caps->ShadeCaps;
339 caps->TextureCaps = wined3d_caps->TextureCaps;
340 caps->TextureFilterCaps = wined3d_caps->TextureFilterCaps;
341 caps->CubeTextureFilterCaps = wined3d_caps->CubeTextureFilterCaps;
342 caps->VolumeTextureFilterCaps = wined3d_caps->VolumeTextureFilterCaps;
343 caps->TextureAddressCaps = wined3d_caps->TextureAddressCaps;
344 caps->VolumeTextureAddressCaps = wined3d_caps->VolumeTextureAddressCaps;
345 caps->LineCaps = wined3d_caps->LineCaps;
346 caps->MaxTextureWidth = wined3d_caps->MaxTextureWidth;
347 caps->MaxTextureHeight = wined3d_caps->MaxTextureHeight;
348 caps->MaxVolumeExtent = wined3d_caps->MaxVolumeExtent;
349 caps->MaxTextureRepeat = wined3d_caps->MaxTextureRepeat;
350 caps->MaxTextureAspectRatio = wined3d_caps->MaxTextureAspectRatio;
351 caps->MaxAnisotropy = wined3d_caps->MaxAnisotropy;
352 caps->MaxVertexW = wined3d_caps->MaxVertexW;
353 caps->GuardBandLeft = wined3d_caps->GuardBandLeft;
354 caps->GuardBandTop = wined3d_caps->GuardBandTop;
355 caps->GuardBandRight = wined3d_caps->GuardBandRight;
356 caps->GuardBandBottom = wined3d_caps->GuardBandBottom;
357 caps->ExtentsAdjust = wined3d_caps->ExtentsAdjust;
358 caps->StencilCaps = wined3d_caps->StencilCaps;
359 caps->FVFCaps = wined3d_caps->FVFCaps;
360 caps->TextureOpCaps = wined3d_caps->TextureOpCaps;
361 caps->MaxTextureBlendStages = wined3d_caps->MaxTextureBlendStages;
362 caps->MaxSimultaneousTextures = wined3d_caps->MaxSimultaneousTextures;
363 caps->VertexProcessingCaps = wined3d_caps->VertexProcessingCaps;
364 caps->MaxActiveLights = wined3d_caps->MaxActiveLights;
365 caps->MaxUserClipPlanes = wined3d_caps->MaxUserClipPlanes;
366 caps->MaxVertexBlendMatrices = wined3d_caps->MaxVertexBlendMatrices;
367 caps->MaxVertexBlendMatrixIndex = wined3d_caps->MaxVertexBlendMatrixIndex;
368 caps->MaxPointSize = wined3d_caps->MaxPointSize;
369 caps->MaxPrimitiveCount = wined3d_caps->MaxPrimitiveCount;
370 caps->MaxVertexIndex = wined3d_caps->MaxVertexIndex;
371 caps->MaxStreams = wined3d_caps->MaxStreams;
372 caps->MaxStreamStride = wined3d_caps->MaxStreamStride;
373 caps->VertexShaderVersion = wined3d_caps->VertexShaderVersion;
374 caps->MaxVertexShaderConst = wined3d_caps->MaxVertexShaderConst;
375 caps->PixelShaderVersion = wined3d_caps->PixelShaderVersion;
376 caps->MaxPixelShaderValue = wined3d_caps->PixelShader1xMaxValue;
378 caps->Caps2 &= D3DCAPS2_CANCALIBRATEGAMMA | D3DCAPS2_CANRENDERWINDOWED
379 | D3DCAPS2_CANMANAGERESOURCE | D3DCAPS2_DYNAMICTEXTURES | D3DCAPS2_FULLSCREENGAMMA
380 | D3DCAPS2_NO2DDURING3DSCENE | D3DCAPS2_RESERVED;
381 caps->Caps3 &= D3DCAPS3_ALPHA_FULLSCREEN_FLIP_OR_DISCARD | D3DCAPS3_RESERVED;
382 caps->PrimitiveMiscCaps &= D3DPMISCCAPS_MASKZ | D3DPMISCCAPS_LINEPATTERNREP
383 | D3DPMISCCAPS_CULLNONE | D3DPMISCCAPS_CULLCW | D3DPMISCCAPS_CULLCCW
384 | D3DPMISCCAPS_COLORWRITEENABLE | D3DPMISCCAPS_CLIPPLANESCALEDPOINTS
385 | D3DPMISCCAPS_CLIPTLVERTS | D3DPMISCCAPS_TSSARGTEMP | D3DPMISCCAPS_BLENDOP
386 | D3DPMISCCAPS_NULLREFERENCE;
387 caps->RasterCaps &= D3DPRASTERCAPS_DITHER | D3DPRASTERCAPS_PAT | D3DPRASTERCAPS_ZTEST
388 | D3DPRASTERCAPS_FOGVERTEX | D3DPRASTERCAPS_FOGTABLE | D3DPRASTERCAPS_ANTIALIASEDGES
389 | D3DPRASTERCAPS_MIPMAPLODBIAS | D3DPRASTERCAPS_ZBUFFERLESSHSR
390 | D3DPRASTERCAPS_FOGRANGE | D3DPRASTERCAPS_ANISOTROPY | D3DPRASTERCAPS_WBUFFER
391 | D3DPRASTERCAPS_WFOG | D3DPRASTERCAPS_ZFOG | D3DPRASTERCAPS_COLORPERSPECTIVE
392 | D3DPRASTERCAPS_STRETCHBLTMULTISAMPLE | WINED3DPRASTERCAPS_DEPTHBIAS;
393 if (caps->RasterCaps & WINED3DPRASTERCAPS_DEPTHBIAS)
394 caps->RasterCaps = (caps->RasterCaps | D3DPRASTERCAPS_ZBIAS) & ~WINED3DPRASTERCAPS_DEPTHBIAS;
395 caps->SrcBlendCaps &= D3DPBLENDCAPS_ZERO | D3DPBLENDCAPS_ONE | D3DPBLENDCAPS_SRCCOLOR
396 | D3DPBLENDCAPS_INVSRCCOLOR | D3DPBLENDCAPS_SRCALPHA | D3DPBLENDCAPS_INVSRCALPHA
397 | D3DPBLENDCAPS_DESTALPHA | D3DPBLENDCAPS_INVDESTALPHA | D3DPBLENDCAPS_DESTCOLOR
398 | D3DPBLENDCAPS_INVDESTCOLOR | D3DPBLENDCAPS_SRCALPHASAT | D3DPBLENDCAPS_BOTHSRCALPHA
399 | D3DPBLENDCAPS_BOTHINVSRCALPHA;
400 caps->DestBlendCaps &= D3DPBLENDCAPS_ZERO | D3DPBLENDCAPS_ONE | D3DPBLENDCAPS_SRCCOLOR
401 | D3DPBLENDCAPS_INVSRCCOLOR | D3DPBLENDCAPS_SRCALPHA | D3DPBLENDCAPS_INVSRCALPHA
402 | D3DPBLENDCAPS_DESTALPHA | D3DPBLENDCAPS_INVDESTALPHA | D3DPBLENDCAPS_DESTCOLOR
403 | D3DPBLENDCAPS_INVDESTCOLOR | D3DPBLENDCAPS_SRCALPHASAT | D3DPBLENDCAPS_BOTHSRCALPHA
404 | D3DPBLENDCAPS_BOTHINVSRCALPHA;
405 caps->TextureCaps &= D3DPTEXTURECAPS_PERSPECTIVE | D3DPTEXTURECAPS_POW2 | D3DPTEXTURECAPS_ALPHA
406 | D3DPTEXTURECAPS_SQUAREONLY | D3DPTEXTURECAPS_TEXREPEATNOTSCALEDBYSIZE
407 | D3DPTEXTURECAPS_ALPHAPALETTE | D3DPTEXTURECAPS_NONPOW2CONDITIONAL
408 | D3DPTEXTURECAPS_PROJECTED | D3DPTEXTURECAPS_CUBEMAP | D3DPTEXTURECAPS_VOLUMEMAP
409 | D3DPTEXTURECAPS_MIPMAP | D3DPTEXTURECAPS_MIPVOLUMEMAP | D3DPTEXTURECAPS_MIPCUBEMAP
410 | D3DPTEXTURECAPS_CUBEMAP_POW2 | D3DPTEXTURECAPS_VOLUMEMAP_POW2;
411 caps->TextureFilterCaps &= D3DPTFILTERCAPS_MINFPOINT | D3DPTFILTERCAPS_MINFLINEAR
412 | D3DPTFILTERCAPS_MINFANISOTROPIC | D3DPTFILTERCAPS_MIPFPOINT
413 | D3DPTFILTERCAPS_MIPFLINEAR | D3DPTFILTERCAPS_MAGFPOINT | D3DPTFILTERCAPS_MAGFLINEAR
414 | D3DPTFILTERCAPS_MAGFANISOTROPIC | D3DPTFILTERCAPS_MAGFAFLATCUBIC
415 | D3DPTFILTERCAPS_MAGFGAUSSIANCUBIC;
416 caps->CubeTextureFilterCaps &= D3DPTFILTERCAPS_MINFPOINT | D3DPTFILTERCAPS_MINFLINEAR
417 | D3DPTFILTERCAPS_MINFANISOTROPIC | D3DPTFILTERCAPS_MIPFPOINT
418 | D3DPTFILTERCAPS_MIPFLINEAR | D3DPTFILTERCAPS_MAGFPOINT | D3DPTFILTERCAPS_MAGFLINEAR
419 | D3DPTFILTERCAPS_MAGFANISOTROPIC | D3DPTFILTERCAPS_MAGFAFLATCUBIC
420 | D3DPTFILTERCAPS_MAGFGAUSSIANCUBIC;
421 caps->VolumeTextureFilterCaps &= D3DPTFILTERCAPS_MINFPOINT | D3DPTFILTERCAPS_MINFLINEAR
422 | D3DPTFILTERCAPS_MINFANISOTROPIC | D3DPTFILTERCAPS_MIPFPOINT
423 | D3DPTFILTERCAPS_MIPFLINEAR | D3DPTFILTERCAPS_MAGFPOINT | D3DPTFILTERCAPS_MAGFLINEAR
424 | D3DPTFILTERCAPS_MAGFANISOTROPIC | D3DPTFILTERCAPS_MAGFAFLATCUBIC
425 | D3DPTFILTERCAPS_MAGFGAUSSIANCUBIC;
426 if (caps->LineCaps & WINED3DLINECAPS_ANTIALIAS)
427 caps->RasterCaps |= D3DPRASTERCAPS_ANTIALIASEDGES;
428 caps->LineCaps &= ~WINED3DLINECAPS_ANTIALIAS;
429 caps->StencilCaps &= ~WINED3DSTENCILCAPS_TWOSIDED;
430 caps->VertexProcessingCaps &= D3DVTXPCAPS_TEXGEN | D3DVTXPCAPS_MATERIALSOURCE7
431 | D3DVTXPCAPS_DIRECTIONALLIGHTS | D3DVTXPCAPS_POSITIONALLIGHTS | D3DVTXPCAPS_LOCALVIEWER
432 | D3DVTXPCAPS_TWEENING | D3DVTXPCAPS_NO_VSDT_UBYTE4;
434 /* D3D8 doesn't support SM 2.0 or higher, so clamp to 1.x */
435 if (caps->PixelShaderVersion)
436 caps->PixelShaderVersion = D3DPS_VERSION(1, 4);
437 else
438 caps->PixelShaderVersion = D3DPS_VERSION(0, 0);
439 if (caps->VertexShaderVersion)
440 caps->VertexShaderVersion = D3DVS_VERSION(1, 1);
441 else
442 caps->VertexShaderVersion = D3DVS_VERSION(0, 0);
443 caps->MaxVertexShaderConst = min(D3D8_MAX_VERTEX_SHADER_CONSTANTF, caps->MaxVertexShaderConst);
446 static enum wined3d_transform_state wined3d_transform_state_from_d3d(D3DTRANSFORMSTATETYPE state)
448 return (enum wined3d_transform_state)state;
451 static enum wined3d_render_state wined3d_render_state_from_d3d(D3DRENDERSTATETYPE state)
453 switch (state)
455 case D3DRS_ZBIAS:
456 return WINED3D_RS_DEPTHBIAS;
457 case D3DRS_EDGEANTIALIAS:
458 return WINED3D_RS_ANTIALIASEDLINEENABLE;
459 default:
460 return (enum wined3d_render_state)state;
464 static enum wined3d_primitive_type wined3d_primitive_type_from_d3d(D3DPRIMITIVETYPE type)
466 return (enum wined3d_primitive_type)type;
469 /* Handle table functions */
470 static DWORD d3d8_allocate_handle(struct d3d8_handle_table *t, void *object, enum d3d8_handle_type type)
472 struct d3d8_handle_entry *entry;
474 if (t->free_entries)
476 DWORD index = t->free_entries - t->entries;
477 /* Use a free handle */
478 entry = t->free_entries;
479 if (entry->type != D3D8_HANDLE_FREE)
481 ERR("Handle %u(%p) is in the free list, but has type %#x.\n", index, entry, entry->type);
482 return D3D8_INVALID_HANDLE;
484 t->free_entries = entry->object;
485 entry->object = object;
486 entry->type = type;
488 return index;
491 if (!(t->entry_count < t->table_size))
493 /* Grow the table */
494 UINT new_size = t->table_size + (t->table_size >> 1);
495 struct d3d8_handle_entry *new_entries;
497 if (!(new_entries = heap_realloc(t->entries, new_size * sizeof(*t->entries))))
499 ERR("Failed to grow the handle table.\n");
500 return D3D8_INVALID_HANDLE;
502 t->entries = new_entries;
503 t->table_size = new_size;
506 entry = &t->entries[t->entry_count];
507 entry->object = object;
508 entry->type = type;
510 return t->entry_count++;
513 static void *d3d8_free_handle(struct d3d8_handle_table *t, DWORD handle, enum d3d8_handle_type type)
515 struct d3d8_handle_entry *entry;
516 void *object;
518 if (handle == D3D8_INVALID_HANDLE || handle >= t->entry_count)
520 WARN("Invalid handle %u passed.\n", handle);
521 return NULL;
524 entry = &t->entries[handle];
525 if (entry->type != type)
527 WARN("Handle %u(%p) is not of type %#x.\n", handle, entry, type);
528 return NULL;
531 object = entry->object;
532 entry->object = t->free_entries;
533 entry->type = D3D8_HANDLE_FREE;
534 t->free_entries = entry;
536 return object;
539 static void *d3d8_get_object(struct d3d8_handle_table *t, DWORD handle, enum d3d8_handle_type type)
541 struct d3d8_handle_entry *entry;
543 if (handle == D3D8_INVALID_HANDLE || handle >= t->entry_count)
545 WARN("Invalid handle %u passed.\n", handle);
546 return NULL;
549 entry = &t->entries[handle];
550 if (entry->type != type)
552 WARN("Handle %u(%p) is not of type %#x.\n", handle, entry, type);
553 return NULL;
556 return entry->object;
559 static void device_reset_viewport_state(struct d3d8_device *device)
561 struct wined3d_viewport vp;
562 RECT rect;
564 wined3d_device_get_viewports(device->wined3d_device, NULL, &vp);
565 wined3d_stateblock_set_viewport(device->state, &vp);
566 wined3d_device_get_scissor_rects(device->wined3d_device, NULL, &rect);
567 wined3d_stateblock_set_scissor_rect(device->state, &rect);
570 static HRESULT WINAPI d3d8_device_QueryInterface(IDirect3DDevice8 *iface, REFIID riid, void **out)
572 TRACE("iface %p, riid %s, out %p.\n",
573 iface, debugstr_guid(riid), out);
575 if (IsEqualGUID(riid, &IID_IDirect3DDevice8)
576 || IsEqualGUID(riid, &IID_IUnknown))
578 IDirect3DDevice8_AddRef(iface);
579 *out = iface;
580 return S_OK;
583 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
585 *out = NULL;
586 return E_NOINTERFACE;
589 static ULONG WINAPI d3d8_device_AddRef(IDirect3DDevice8 *iface)
591 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
592 ULONG ref = InterlockedIncrement(&device->ref);
594 TRACE("%p increasing refcount to %u.\n", iface, ref);
596 return ref;
599 static ULONG WINAPI d3d8_device_Release(IDirect3DDevice8 *iface)
601 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
602 ULONG ref;
604 if (device->in_destruction)
605 return 0;
607 ref = InterlockedDecrement(&device->ref);
609 TRACE("%p decreasing refcount to %u.\n", iface, ref);
611 if (!ref)
613 IDirect3D8 *parent = &device->d3d_parent->IDirect3D8_iface;
614 unsigned i;
616 TRACE("Releasing wined3d device %p.\n", device->wined3d_device);
618 wined3d_mutex_lock();
620 device->in_destruction = TRUE;
622 for (i = 0; i < device->numConvertedDecls; ++i)
624 d3d8_vertex_declaration_destroy(device->decls[i].declaration);
626 heap_free(device->decls);
628 if (device->vertex_buffer)
629 wined3d_buffer_decref(device->vertex_buffer);
630 if (device->index_buffer)
631 wined3d_buffer_decref(device->index_buffer);
633 if (device->recording)
634 wined3d_stateblock_decref(device->recording);
635 wined3d_stateblock_decref(device->state);
637 wined3d_swapchain_decref(device->implicit_swapchain);
638 wined3d_device_release_focus_window(device->wined3d_device);
639 wined3d_device_decref(device->wined3d_device);
640 heap_free(device->handle_table.entries);
641 heap_free(device);
643 wined3d_mutex_unlock();
645 IDirect3D8_Release(parent);
647 return ref;
650 static HRESULT WINAPI d3d8_device_TestCooperativeLevel(IDirect3DDevice8 *iface)
652 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
654 TRACE("iface %p.\n", iface);
656 TRACE("device state: %#x.\n", device->device_state);
658 switch (device->device_state)
660 default:
661 case D3D8_DEVICE_STATE_OK:
662 return D3D_OK;
663 case D3D8_DEVICE_STATE_LOST:
664 return D3DERR_DEVICELOST;
665 case D3D8_DEVICE_STATE_NOT_RESET:
666 return D3DERR_DEVICENOTRESET;
670 static UINT WINAPI d3d8_device_GetAvailableTextureMem(IDirect3DDevice8 *iface)
672 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
673 UINT ret;
675 TRACE("iface %p.\n", iface);
677 wined3d_mutex_lock();
678 ret = wined3d_device_get_available_texture_mem(device->wined3d_device);
679 wined3d_mutex_unlock();
681 return ret;
684 static HRESULT WINAPI d3d8_device_ResourceManagerDiscardBytes(IDirect3DDevice8 *iface, DWORD byte_count)
686 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
688 TRACE("iface %p, byte_count %u.\n", iface, byte_count);
690 if (byte_count)
691 FIXME("Byte count ignored.\n");
693 wined3d_mutex_lock();
694 wined3d_device_evict_managed_resources(device->wined3d_device);
695 wined3d_mutex_unlock();
697 return D3D_OK;
700 static HRESULT WINAPI d3d8_device_GetDirect3D(IDirect3DDevice8 *iface, IDirect3D8 **d3d8)
702 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
704 TRACE("iface %p, d3d8 %p.\n", iface, d3d8);
706 if (!d3d8)
707 return D3DERR_INVALIDCALL;
709 return IDirect3D8_QueryInterface(&device->d3d_parent->IDirect3D8_iface, &IID_IDirect3D8,
710 (void **)d3d8);
713 static HRESULT WINAPI d3d8_device_GetDeviceCaps(IDirect3DDevice8 *iface, D3DCAPS8 *caps)
715 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
716 struct wined3d_caps wined3d_caps;
717 HRESULT hr;
719 TRACE("iface %p, caps %p.\n", iface, caps);
721 if (!caps)
722 return D3DERR_INVALIDCALL;
724 wined3d_mutex_lock();
725 hr = wined3d_device_get_device_caps(device->wined3d_device, &wined3d_caps);
726 wined3d_mutex_unlock();
728 d3dcaps_from_wined3dcaps(caps, &wined3d_caps, device->adapter_ordinal);
730 return hr;
733 static HRESULT WINAPI d3d8_device_GetDisplayMode(IDirect3DDevice8 *iface, D3DDISPLAYMODE *mode)
735 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
736 struct wined3d_display_mode wined3d_mode;
737 HRESULT hr;
739 TRACE("iface %p, mode %p.\n", iface, mode);
741 wined3d_mutex_lock();
742 hr = wined3d_device_get_display_mode(device->wined3d_device, 0, &wined3d_mode, NULL);
743 wined3d_mutex_unlock();
745 if (SUCCEEDED(hr))
747 mode->Width = wined3d_mode.width;
748 mode->Height = wined3d_mode.height;
749 mode->RefreshRate = wined3d_mode.refresh_rate;
750 mode->Format = d3dformat_from_wined3dformat(wined3d_mode.format_id);
753 return hr;
756 static HRESULT WINAPI d3d8_device_GetCreationParameters(IDirect3DDevice8 *iface,
757 D3DDEVICE_CREATION_PARAMETERS *parameters)
759 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
761 TRACE("iface %p, parameters %p.\n", iface, parameters);
763 wined3d_mutex_lock();
764 wined3d_device_get_creation_parameters(device->wined3d_device,
765 (struct wined3d_device_creation_parameters *)parameters);
766 wined3d_mutex_unlock();
768 parameters->AdapterOrdinal = device->adapter_ordinal;
769 return D3D_OK;
772 static HRESULT WINAPI d3d8_device_SetCursorProperties(IDirect3DDevice8 *iface,
773 UINT hotspot_x, UINT hotspot_y, IDirect3DSurface8 *bitmap)
775 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
776 struct d3d8_surface *bitmap_impl = unsafe_impl_from_IDirect3DSurface8(bitmap);
777 D3DSURFACE_DESC surface_desc;
778 D3DDISPLAYMODE mode;
779 HRESULT hr;
781 TRACE("iface %p, hotspot_x %u, hotspot_y %u, bitmap %p.\n",
782 iface, hotspot_x, hotspot_y, bitmap);
784 if (!bitmap)
786 WARN("No cursor bitmap, returning D3DERR_INVALIDCALL.\n");
787 return D3DERR_INVALIDCALL;
790 if (FAILED(hr = IDirect3DSurface8_GetDesc(bitmap, &surface_desc)))
792 WARN("Failed to get surface description, hr %#x.\n", hr);
793 return hr;
796 if (FAILED(hr = IDirect3D8_GetAdapterDisplayMode(&device->d3d_parent->IDirect3D8_iface,
797 device->adapter_ordinal, &mode)))
799 WARN("Failed to get device display mode, hr %#x.\n", hr);
800 return hr;
803 if (surface_desc.Width > mode.Width || surface_desc.Height > mode.Height)
805 WARN("Surface dimension %ux%u exceeds display mode %ux%u.\n", surface_desc.Width,
806 surface_desc.Height, mode.Width, mode.Height);
807 return D3DERR_INVALIDCALL;
810 wined3d_mutex_lock();
811 hr = wined3d_device_set_cursor_properties(device->wined3d_device,
812 hotspot_x, hotspot_y, bitmap_impl->wined3d_texture, bitmap_impl->sub_resource_idx);
813 wined3d_mutex_unlock();
815 return hr;
818 static void WINAPI d3d8_device_SetCursorPosition(IDirect3DDevice8 *iface, UINT x, UINT y, DWORD flags)
820 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
822 TRACE("iface %p, x %u, y %u, flags %#x.\n", iface, x, y, flags);
824 wined3d_mutex_lock();
825 wined3d_device_set_cursor_position(device->wined3d_device, x, y, flags);
826 wined3d_mutex_unlock();
829 static BOOL WINAPI d3d8_device_ShowCursor(IDirect3DDevice8 *iface, BOOL show)
831 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
832 BOOL ret;
834 TRACE("iface %p, show %#x.\n", iface, show);
836 wined3d_mutex_lock();
837 ret = wined3d_device_show_cursor(device->wined3d_device, show);
838 wined3d_mutex_unlock();
840 return ret;
843 static HRESULT WINAPI d3d8_device_CreateAdditionalSwapChain(IDirect3DDevice8 *iface,
844 D3DPRESENT_PARAMETERS *present_parameters, IDirect3DSwapChain8 **swapchain)
846 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
847 struct wined3d_swapchain_desc desc;
848 struct d3d8_swapchain *object;
849 unsigned int swap_interval;
850 unsigned int output_idx;
851 unsigned int i, count;
852 HRESULT hr;
854 TRACE("iface %p, present_parameters %p, swapchain %p.\n",
855 iface, present_parameters, swapchain);
857 if (!present_parameters->Windowed)
859 WARN("Trying to create an additional fullscreen swapchain, returning D3DERR_INVALIDCALL.\n");
860 return D3DERR_INVALIDCALL;
863 wined3d_mutex_lock();
864 count = wined3d_device_get_swapchain_count(device->wined3d_device);
865 for (i = 0; i < count; ++i)
867 struct wined3d_swapchain *wined3d_swapchain;
869 wined3d_swapchain = wined3d_device_get_swapchain(device->wined3d_device, i);
870 wined3d_swapchain_get_desc(wined3d_swapchain, &desc);
872 if (!desc.windowed)
874 wined3d_mutex_unlock();
875 WARN("Trying to create an additional swapchain in fullscreen mode, returning D3DERR_INVALIDCALL.\n");
876 return D3DERR_INVALIDCALL;
879 wined3d_mutex_unlock();
881 output_idx = device->adapter_ordinal;
882 if (!wined3d_swapchain_desc_from_d3d8(&desc, device->d3d_parent->wined3d_outputs[output_idx],
883 present_parameters))
884 return D3DERR_INVALIDCALL;
885 swap_interval = wined3dswapinterval_from_d3d(present_parameters->FullScreen_PresentationInterval);
886 if (SUCCEEDED(hr = d3d8_swapchain_create(device, &desc, swap_interval, &object)))
887 *swapchain = &object->IDirect3DSwapChain8_iface;
888 present_parameters_from_wined3d_swapchain_desc(present_parameters,
889 &desc, present_parameters->FullScreen_PresentationInterval);
891 return hr;
894 static HRESULT CDECL reset_enum_callback(struct wined3d_resource *resource)
896 struct d3d8_vertexbuffer *vertex_buffer;
897 struct d3d8_indexbuffer *index_buffer;
898 struct wined3d_resource_desc desc;
899 IDirect3DBaseTexture8 *texture;
900 struct d3d8_surface *surface;
901 IUnknown *parent;
903 wined3d_resource_get_desc(resource, &desc);
904 if (desc.access & WINED3D_RESOURCE_ACCESS_CPU)
905 return D3D_OK;
907 if (desc.resource_type != WINED3D_RTYPE_TEXTURE_2D)
909 if (desc.bind_flags & WINED3D_BIND_VERTEX_BUFFER)
911 vertex_buffer = wined3d_resource_get_parent(resource);
912 if (vertex_buffer && vertex_buffer->draw_buffer)
913 return D3D_OK;
915 if (desc.bind_flags & WINED3D_BIND_INDEX_BUFFER)
917 index_buffer = wined3d_resource_get_parent(resource);
918 if (index_buffer && index_buffer->draw_buffer)
919 return D3D_OK;
922 WARN("Resource %p in pool D3DPOOL_DEFAULT blocks the Reset call.\n", resource);
923 return D3DERR_DEVICELOST;
926 parent = wined3d_resource_get_parent(resource);
927 if (parent && SUCCEEDED(IUnknown_QueryInterface(parent, &IID_IDirect3DBaseTexture8, (void **)&texture)))
929 IDirect3DBaseTexture8_Release(texture);
930 WARN("Texture %p (resource %p) in pool D3DPOOL_DEFAULT blocks the Reset call.\n", texture, resource);
931 return D3DERR_DEVICELOST;
934 surface = wined3d_texture_get_sub_resource_parent(wined3d_texture_from_resource(resource), 0);
935 if (!surface->resource.refcount)
936 return D3D_OK;
938 WARN("Surface %p in pool D3DPOOL_DEFAULT blocks the Reset call.\n", surface);
939 return D3DERR_DEVICELOST;
942 static HRESULT WINAPI d3d8_device_Reset(IDirect3DDevice8 *iface,
943 D3DPRESENT_PARAMETERS *present_parameters)
945 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
946 struct wined3d_swapchain_desc swapchain_desc;
947 struct d3d8_swapchain *implicit_swapchain;
948 unsigned int output_idx;
949 HRESULT hr;
951 TRACE("iface %p, present_parameters %p.\n", iface, present_parameters);
953 if (device->device_state == D3D8_DEVICE_STATE_LOST)
955 WARN("App not active, returning D3DERR_DEVICELOST.\n");
956 return D3DERR_DEVICELOST;
958 output_idx = device->adapter_ordinal;
959 if (!wined3d_swapchain_desc_from_d3d8(&swapchain_desc,
960 device->d3d_parent->wined3d_outputs[output_idx], present_parameters))
961 return D3DERR_INVALIDCALL;
962 swapchain_desc.flags |= WINED3D_SWAPCHAIN_IMPLICIT;
964 wined3d_mutex_lock();
966 if (device->vertex_buffer)
968 wined3d_buffer_decref(device->vertex_buffer);
969 device->vertex_buffer = NULL;
970 device->vertex_buffer_size = 0;
972 if (device->index_buffer)
974 wined3d_buffer_decref(device->index_buffer);
975 device->index_buffer = NULL;
976 device->index_buffer_size = 0;
979 if (device->recording)
980 wined3d_stateblock_decref(device->recording);
981 device->recording = NULL;
982 device->update_state = device->state;
983 wined3d_stateblock_reset(device->state);
985 if (SUCCEEDED(hr = wined3d_device_reset(device->wined3d_device, &swapchain_desc,
986 NULL, reset_enum_callback, TRUE)))
988 present_parameters->BackBufferCount = swapchain_desc.backbuffer_count;
989 implicit_swapchain = wined3d_swapchain_get_parent(device->implicit_swapchain);
990 implicit_swapchain->swap_interval
991 = wined3dswapinterval_from_d3d(present_parameters->FullScreen_PresentationInterval);
992 wined3d_stateblock_set_render_state(device->state, WINED3D_RS_POINTSIZE_MIN, 0);
993 wined3d_stateblock_set_render_state(device->state, WINED3D_RS_ZENABLE,
994 !!swapchain_desc.enable_auto_depth_stencil);
995 device_reset_viewport_state(device);
996 device->device_state = D3D8_DEVICE_STATE_OK;
998 else
1000 device->device_state = D3D8_DEVICE_STATE_NOT_RESET;
1002 wined3d_mutex_unlock();
1004 return hr;
1007 static HRESULT WINAPI d3d8_device_Present(IDirect3DDevice8 *iface, const RECT *src_rect,
1008 const RECT *dst_rect, HWND dst_window_override, const RGNDATA *dirty_region)
1010 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1011 struct d3d8_swapchain *implicit_swapchain;
1013 TRACE("iface %p, src_rect %s, dst_rect %s, dst_window_override %p, dirty_region %p.\n",
1014 iface, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect), dst_window_override, dirty_region);
1016 /* Fraps does not hook IDirect3DDevice8::Present regardless of the hotpatch
1017 * attribute. It only hooks IDirect3DSwapChain8::Present. Yet it properly
1018 * shows a framerate on Windows in applications that only call the device
1019 * method, like e.g. the dx8 sdk samples. The conclusion is that native
1020 * calls the swapchain's public method from the device. */
1021 implicit_swapchain = wined3d_swapchain_get_parent(device->implicit_swapchain);
1022 return IDirect3DSwapChain8_Present(&implicit_swapchain->IDirect3DSwapChain8_iface,
1023 src_rect, dst_rect, dst_window_override, dirty_region);
1026 static HRESULT WINAPI d3d8_device_GetBackBuffer(IDirect3DDevice8 *iface,
1027 UINT backbuffer_idx, D3DBACKBUFFER_TYPE backbuffer_type, IDirect3DSurface8 **backbuffer)
1029 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1030 struct wined3d_texture *wined3d_texture;
1031 struct d3d8_surface *surface_impl;
1033 TRACE("iface %p, backbuffer_idx %u, backbuffer_type %#x, backbuffer %p.\n",
1034 iface, backbuffer_idx, backbuffer_type, backbuffer);
1036 /* backbuffer_type is ignored by native. */
1038 /* No need to check for backbuffer == NULL, Windows crashes in that case. */
1039 wined3d_mutex_lock();
1041 if (!(wined3d_texture = wined3d_swapchain_get_back_buffer(device->implicit_swapchain, backbuffer_idx)))
1043 wined3d_mutex_unlock();
1044 *backbuffer = NULL;
1045 return D3DERR_INVALIDCALL;
1048 surface_impl = wined3d_texture_get_sub_resource_parent(wined3d_texture, 0);
1049 *backbuffer = &surface_impl->IDirect3DSurface8_iface;
1050 IDirect3DSurface8_AddRef(*backbuffer);
1052 wined3d_mutex_unlock();
1053 return D3D_OK;
1056 static HRESULT WINAPI d3d8_device_GetRasterStatus(IDirect3DDevice8 *iface, D3DRASTER_STATUS *raster_status)
1058 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1059 HRESULT hr;
1061 TRACE("iface %p, raster_status %p.\n", iface, raster_status);
1063 wined3d_mutex_lock();
1064 hr = wined3d_device_get_raster_status(device->wined3d_device, 0, (struct wined3d_raster_status *)raster_status);
1065 wined3d_mutex_unlock();
1067 return hr;
1070 static void WINAPI d3d8_device_SetGammaRamp(IDirect3DDevice8 *iface, DWORD flags, const D3DGAMMARAMP *ramp)
1072 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1074 TRACE("iface %p, flags %#x, ramp %p.\n", iface, flags, ramp);
1076 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
1077 wined3d_mutex_lock();
1078 wined3d_device_set_gamma_ramp(device->wined3d_device, 0, flags, (const struct wined3d_gamma_ramp *)ramp);
1079 wined3d_mutex_unlock();
1082 static void WINAPI d3d8_device_GetGammaRamp(IDirect3DDevice8 *iface, D3DGAMMARAMP *ramp)
1084 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1086 TRACE("iface %p, ramp %p.\n", iface, ramp);
1088 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
1089 wined3d_mutex_lock();
1090 wined3d_device_get_gamma_ramp(device->wined3d_device, 0, (struct wined3d_gamma_ramp *)ramp);
1091 wined3d_mutex_unlock();
1094 static HRESULT WINAPI d3d8_device_CreateTexture(IDirect3DDevice8 *iface,
1095 UINT width, UINT height, UINT levels, DWORD usage, D3DFORMAT format,
1096 D3DPOOL pool, IDirect3DTexture8 **texture)
1098 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1099 struct d3d8_texture *object;
1100 HRESULT hr;
1102 TRACE("iface %p, width %u, height %u, levels %u, usage %#x, format %#x, pool %#x, texture %p.\n",
1103 iface, width, height, levels, usage, format, pool, texture);
1105 if (!format)
1106 return D3DERR_INVALIDCALL;
1108 *texture = NULL;
1109 if (!(object = heap_alloc_zero(sizeof(*object))))
1110 return D3DERR_OUTOFVIDEOMEMORY;
1112 hr = texture_init(object, device, width, height, levels, usage, format, pool);
1113 if (FAILED(hr))
1115 WARN("Failed to initialize texture, hr %#x.\n", hr);
1116 heap_free(object);
1117 return hr;
1120 TRACE("Created texture %p.\n", object);
1121 *texture = (IDirect3DTexture8 *)&object->IDirect3DBaseTexture8_iface;
1123 return D3D_OK;
1126 static HRESULT WINAPI d3d8_device_CreateVolumeTexture(IDirect3DDevice8 *iface,
1127 UINT width, UINT height, UINT depth, UINT levels, DWORD usage, D3DFORMAT format,
1128 D3DPOOL pool, IDirect3DVolumeTexture8 **texture)
1130 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1131 struct d3d8_texture *object;
1132 HRESULT hr;
1134 TRACE("iface %p, width %u, height %u, depth %u, levels %u, usage %#x, format %#x, pool %#x, texture %p.\n",
1135 iface, width, height, depth, levels, usage, format, pool, texture);
1137 if (!format)
1138 return D3DERR_INVALIDCALL;
1140 *texture = NULL;
1141 if (!(object = heap_alloc_zero(sizeof(*object))))
1142 return D3DERR_OUTOFVIDEOMEMORY;
1144 hr = volumetexture_init(object, device, width, height, depth, levels, usage, format, pool);
1145 if (FAILED(hr))
1147 WARN("Failed to initialize volume texture, hr %#x.\n", hr);
1148 heap_free(object);
1149 return hr;
1152 TRACE("Created volume texture %p.\n", object);
1153 *texture = (IDirect3DVolumeTexture8 *)&object->IDirect3DBaseTexture8_iface;
1155 return D3D_OK;
1158 static HRESULT WINAPI d3d8_device_CreateCubeTexture(IDirect3DDevice8 *iface, UINT edge_length,
1159 UINT levels, DWORD usage, D3DFORMAT format, D3DPOOL pool, IDirect3DCubeTexture8 **texture)
1161 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1162 struct d3d8_texture *object;
1163 HRESULT hr;
1165 TRACE("iface %p, edge_length %u, levels %u, usage %#x, format %#x, pool %#x, texture %p.\n",
1166 iface, edge_length, levels, usage, format, pool, texture);
1168 if (!format)
1169 return D3DERR_INVALIDCALL;
1171 *texture = NULL;
1172 if (!(object = heap_alloc_zero(sizeof(*object))))
1173 return D3DERR_OUTOFVIDEOMEMORY;
1175 hr = cubetexture_init(object, device, edge_length, levels, usage, format, pool);
1176 if (FAILED(hr))
1178 WARN("Failed to initialize cube texture, hr %#x.\n", hr);
1179 heap_free(object);
1180 return hr;
1183 TRACE("Created cube texture %p.\n", object);
1184 *texture = (IDirect3DCubeTexture8 *)&object->IDirect3DBaseTexture8_iface;
1186 return hr;
1189 static HRESULT WINAPI d3d8_device_CreateVertexBuffer(IDirect3DDevice8 *iface, UINT size,
1190 DWORD usage, DWORD fvf, D3DPOOL pool, IDirect3DVertexBuffer8 **buffer)
1192 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1193 struct d3d8_vertexbuffer *object;
1194 HRESULT hr;
1196 TRACE("iface %p, size %u, usage %#x, fvf %#x, pool %#x, buffer %p.\n",
1197 iface, size, usage, fvf, pool, buffer);
1199 if (!(object = heap_alloc_zero(sizeof(*object))))
1200 return D3DERR_OUTOFVIDEOMEMORY;
1202 hr = vertexbuffer_init(object, device, size, usage, fvf, pool);
1203 if (FAILED(hr))
1205 WARN("Failed to initialize vertex buffer, hr %#x.\n", hr);
1206 heap_free(object);
1207 return hr;
1210 TRACE("Created vertex buffer %p.\n", object);
1211 *buffer = &object->IDirect3DVertexBuffer8_iface;
1213 return D3D_OK;
1216 static HRESULT WINAPI d3d8_device_CreateIndexBuffer(IDirect3DDevice8 *iface, UINT size,
1217 DWORD usage, D3DFORMAT format, D3DPOOL pool, IDirect3DIndexBuffer8 **buffer)
1219 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1220 struct d3d8_indexbuffer *object;
1221 HRESULT hr;
1223 TRACE("iface %p, size %u, usage %#x, format %#x, pool %#x, buffer %p.\n",
1224 iface, size, usage, format, pool, buffer);
1226 if (!(object = heap_alloc_zero(sizeof(*object))))
1227 return D3DERR_OUTOFVIDEOMEMORY;
1229 hr = indexbuffer_init(object, device, size, usage, format, pool);
1230 if (FAILED(hr))
1232 WARN("Failed to initialize index buffer, hr %#x.\n", hr);
1233 heap_free(object);
1234 return hr;
1237 TRACE("Created index buffer %p.\n", object);
1238 *buffer = &object->IDirect3DIndexBuffer8_iface;
1240 return D3D_OK;
1243 static HRESULT d3d8_device_create_surface(struct d3d8_device *device, enum wined3d_format_id format,
1244 enum wined3d_multisample_type multisample_type, unsigned int bind_flags, unsigned int access,
1245 unsigned int width, unsigned int height, IDirect3DSurface8 **surface)
1247 struct wined3d_resource_desc desc;
1248 struct d3d8_surface *surface_impl;
1249 struct wined3d_texture *texture;
1250 HRESULT hr;
1252 TRACE("device %p, format %#x, multisample_type %#x, bind_flags %#x, "
1253 "access %#x, width %u, height %u, surface %p.\n",
1254 device, format, multisample_type, bind_flags, access, width, height, surface);
1256 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
1257 desc.format = format;
1258 desc.multisample_type = multisample_type;
1259 desc.multisample_quality = 0;
1260 desc.usage = 0;
1261 desc.bind_flags = bind_flags;
1262 desc.access = access;
1263 desc.width = width;
1264 desc.height = height;
1265 desc.depth = 1;
1266 desc.size = 0;
1268 wined3d_mutex_lock();
1270 if (FAILED(hr = wined3d_texture_create(device->wined3d_device, &desc,
1271 1, 1, 0, NULL, NULL, &d3d8_null_wined3d_parent_ops, &texture)))
1273 wined3d_mutex_unlock();
1274 WARN("Failed to create texture, hr %#x.\n", hr);
1275 return hr;
1278 surface_impl = wined3d_texture_get_sub_resource_parent(texture, 0);
1279 surface_impl->parent_device = &device->IDirect3DDevice8_iface;
1280 *surface = &surface_impl->IDirect3DSurface8_iface;
1281 IDirect3DSurface8_AddRef(*surface);
1282 wined3d_texture_decref(texture);
1284 wined3d_mutex_unlock();
1286 return D3D_OK;
1289 static HRESULT WINAPI d3d8_device_CreateRenderTarget(IDirect3DDevice8 *iface, UINT width,
1290 UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, BOOL lockable,
1291 IDirect3DSurface8 **surface)
1293 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1294 unsigned int access = WINED3D_RESOURCE_ACCESS_GPU;
1296 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, lockable %#x, surface %p.\n",
1297 iface, width, height, format, multisample_type, lockable, surface);
1299 if (!format)
1300 return D3DERR_INVALIDCALL;
1302 *surface = NULL;
1303 if (lockable)
1304 access |= WINED3D_RESOURCE_ACCESS_MAP_R | WINED3D_RESOURCE_ACCESS_MAP_W;
1306 return d3d8_device_create_surface(device, wined3dformat_from_d3dformat(format),
1307 wined3d_multisample_type_from_d3d(multisample_type),
1308 WINED3D_BIND_RENDER_TARGET, access, width, height, surface);
1311 static HRESULT WINAPI d3d8_device_CreateDepthStencilSurface(IDirect3DDevice8 *iface,
1312 UINT width, UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type,
1313 IDirect3DSurface8 **surface)
1315 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1317 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, surface %p.\n",
1318 iface, width, height, format, multisample_type, surface);
1320 if (!format)
1321 return D3DERR_INVALIDCALL;
1323 *surface = NULL;
1325 return d3d8_device_create_surface(device, wined3dformat_from_d3dformat(format),
1326 wined3d_multisample_type_from_d3d(multisample_type), WINED3D_BIND_DEPTH_STENCIL,
1327 WINED3D_RESOURCE_ACCESS_GPU, width, height, surface);
1330 /* IDirect3DDevice8Impl::CreateImageSurface returns surface with pool type SYSTEMMEM */
1331 static HRESULT WINAPI d3d8_device_CreateImageSurface(IDirect3DDevice8 *iface, UINT width,
1332 UINT height, D3DFORMAT format, IDirect3DSurface8 **surface)
1334 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1335 unsigned int access = WINED3D_RESOURCE_ACCESS_CPU
1336 | WINED3D_RESOURCE_ACCESS_MAP_R | WINED3D_RESOURCE_ACCESS_MAP_W;
1338 TRACE("iface %p, width %u, height %u, format %#x, surface %p.\n",
1339 iface, width, height, format, surface);
1341 *surface = NULL;
1343 return d3d8_device_create_surface(device, wined3dformat_from_d3dformat(format),
1344 WINED3D_MULTISAMPLE_NONE, 0, access, width, height, surface);
1347 static HRESULT WINAPI d3d8_device_CopyRects(IDirect3DDevice8 *iface,
1348 IDirect3DSurface8 *src_surface, const RECT *src_rects, UINT rect_count,
1349 IDirect3DSurface8 *dst_surface, const POINT *dst_points)
1351 struct d3d8_surface *src = unsafe_impl_from_IDirect3DSurface8(src_surface);
1352 struct d3d8_surface *dst = unsafe_impl_from_IDirect3DSurface8(dst_surface);
1353 enum wined3d_format_id src_format, dst_format;
1354 struct wined3d_sub_resource_desc wined3d_desc;
1355 UINT src_w, src_h;
1357 TRACE("iface %p, src_surface %p, src_rects %p, rect_count %u, dst_surface %p, dst_points %p.\n",
1358 iface, src_surface, src_rects, rect_count, dst_surface, dst_points);
1360 /* Check that the source texture is in WINED3D_POOL_SYSTEM_MEM and the
1361 * destination texture is in WINED3D_POOL_DEFAULT. */
1363 wined3d_mutex_lock();
1364 wined3d_texture_get_sub_resource_desc(src->wined3d_texture, src->sub_resource_idx, &wined3d_desc);
1365 if (wined3d_desc.bind_flags & WINED3D_BIND_DEPTH_STENCIL)
1367 WARN("Source %p is a depth stencil surface, returning D3DERR_INVALIDCALL.\n", src_surface);
1368 wined3d_mutex_unlock();
1369 return D3DERR_INVALIDCALL;
1371 src_format = wined3d_desc.format;
1372 src_w = wined3d_desc.width;
1373 src_h = wined3d_desc.height;
1375 wined3d_texture_get_sub_resource_desc(dst->wined3d_texture, dst->sub_resource_idx, &wined3d_desc);
1376 if (wined3d_desc.bind_flags & WINED3D_BIND_DEPTH_STENCIL)
1378 WARN("Destination %p is a depth stencil surface, returning D3DERR_INVALIDCALL.\n", dst_surface);
1379 wined3d_mutex_unlock();
1380 return D3DERR_INVALIDCALL;
1382 dst_format = wined3d_desc.format;
1384 /* Check that the source and destination formats match */
1385 if (src_format != dst_format)
1387 WARN("Source %p format must match the destination %p format, returning D3DERR_INVALIDCALL.\n",
1388 src_surface, dst_surface);
1389 wined3d_mutex_unlock();
1390 return D3DERR_INVALIDCALL;
1393 /* Quick if complete copy ... */
1394 if (!rect_count && !src_rects && !dst_points)
1396 RECT rect = {0, 0, src_w, src_h};
1397 wined3d_texture_blt(dst->wined3d_texture, dst->sub_resource_idx, &rect,
1398 src->wined3d_texture, src->sub_resource_idx, &rect, 0, NULL, WINED3D_TEXF_POINT);
1400 else
1402 unsigned int i;
1403 /* Copy rect by rect */
1404 if (src_rects && dst_points)
1406 for (i = 0; i < rect_count; ++i)
1408 UINT w = src_rects[i].right - src_rects[i].left;
1409 UINT h = src_rects[i].bottom - src_rects[i].top;
1410 RECT dst_rect = {dst_points[i].x, dst_points[i].y,
1411 dst_points[i].x + w, dst_points[i].y + h};
1413 wined3d_texture_blt(dst->wined3d_texture, dst->sub_resource_idx, &dst_rect,
1414 src->wined3d_texture, src->sub_resource_idx, &src_rects[i], 0, NULL, WINED3D_TEXF_POINT);
1417 else
1419 for (i = 0; i < rect_count; ++i)
1421 UINT w = src_rects[i].right - src_rects[i].left;
1422 UINT h = src_rects[i].bottom - src_rects[i].top;
1423 RECT dst_rect = {0, 0, w, h};
1425 wined3d_texture_blt(dst->wined3d_texture, dst->sub_resource_idx, &dst_rect,
1426 src->wined3d_texture, src->sub_resource_idx, &src_rects[i], 0, NULL, WINED3D_TEXF_POINT);
1430 wined3d_mutex_unlock();
1432 return WINED3D_OK;
1435 static HRESULT WINAPI d3d8_device_UpdateTexture(IDirect3DDevice8 *iface,
1436 IDirect3DBaseTexture8 *src_texture, IDirect3DBaseTexture8 *dst_texture)
1438 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1439 struct d3d8_texture *src_impl, *dst_impl;
1440 HRESULT hr;
1442 TRACE("iface %p, src_texture %p, dst_texture %p.\n", iface, src_texture, dst_texture);
1444 src_impl = unsafe_impl_from_IDirect3DBaseTexture8(src_texture);
1445 dst_impl = unsafe_impl_from_IDirect3DBaseTexture8(dst_texture);
1447 wined3d_mutex_lock();
1448 hr = wined3d_device_update_texture(device->wined3d_device,
1449 src_impl->wined3d_texture, dst_impl->wined3d_texture);
1450 wined3d_mutex_unlock();
1452 return hr;
1455 static HRESULT WINAPI d3d8_device_GetFrontBuffer(IDirect3DDevice8 *iface, IDirect3DSurface8 *dst_surface)
1457 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1458 struct d3d8_surface *dst_impl = unsafe_impl_from_IDirect3DSurface8(dst_surface);
1459 HRESULT hr;
1461 TRACE("iface %p, dst_surface %p.\n", iface, dst_surface);
1463 if (!dst_surface)
1465 WARN("Invalid destination surface passed.\n");
1466 return D3DERR_INVALIDCALL;
1469 wined3d_mutex_lock();
1470 hr = wined3d_swapchain_get_front_buffer_data(device->implicit_swapchain,
1471 dst_impl->wined3d_texture, dst_impl->sub_resource_idx);
1472 wined3d_mutex_unlock();
1474 return hr;
1477 static HRESULT WINAPI d3d8_device_SetRenderTarget(IDirect3DDevice8 *iface,
1478 IDirect3DSurface8 *render_target, IDirect3DSurface8 *depth_stencil)
1480 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1481 struct d3d8_surface *rt_impl = unsafe_impl_from_IDirect3DSurface8(render_target);
1482 struct d3d8_surface *ds_impl = unsafe_impl_from_IDirect3DSurface8(depth_stencil);
1483 struct wined3d_rendertarget_view *original_dsv, *rtv;
1484 HRESULT hr = D3D_OK;
1486 TRACE("iface %p, render_target %p, depth_stencil %p.\n", iface, render_target, depth_stencil);
1488 if (rt_impl && d3d8_surface_get_device(rt_impl) != device)
1490 WARN("Render target surface does not match device.\n");
1491 return D3DERR_INVALIDCALL;
1494 wined3d_mutex_lock();
1496 if (ds_impl)
1498 struct wined3d_sub_resource_desc ds_desc, rt_desc;
1499 struct wined3d_rendertarget_view *original_rtv;
1500 struct d3d8_surface *original_surface;
1502 /* If no render target is passed in check the size against the current RT */
1503 if (!render_target)
1505 if (!(original_rtv = wined3d_device_get_rendertarget_view(device->wined3d_device, 0)))
1507 wined3d_mutex_unlock();
1508 return D3DERR_NOTFOUND;
1510 original_surface = wined3d_rendertarget_view_get_sub_resource_parent(original_rtv);
1511 wined3d_texture_get_sub_resource_desc(original_surface->wined3d_texture,
1512 original_surface->sub_resource_idx, &rt_desc);
1514 else
1515 wined3d_texture_get_sub_resource_desc(rt_impl->wined3d_texture,
1516 rt_impl->sub_resource_idx, &rt_desc);
1518 wined3d_texture_get_sub_resource_desc(ds_impl->wined3d_texture, ds_impl->sub_resource_idx, &ds_desc);
1520 if (ds_desc.width < rt_desc.width || ds_desc.height < rt_desc.height)
1522 WARN("Depth stencil is smaller than the render target, returning D3DERR_INVALIDCALL\n");
1523 wined3d_mutex_unlock();
1524 return D3DERR_INVALIDCALL;
1526 if (ds_desc.multisample_type != rt_desc.multisample_type
1527 || ds_desc.multisample_quality != rt_desc.multisample_quality)
1529 WARN("Multisample settings do not match, returning D3DERR_INVALIDCALL\n");
1530 wined3d_mutex_unlock();
1531 return D3DERR_INVALIDCALL;
1535 original_dsv = wined3d_device_get_depth_stencil_view(device->wined3d_device);
1536 rtv = ds_impl ? d3d8_surface_acquire_rendertarget_view(ds_impl) : NULL;
1537 hr = wined3d_device_set_depth_stencil_view(device->wined3d_device, rtv);
1538 d3d8_surface_release_rendertarget_view(ds_impl, rtv);
1539 if (SUCCEEDED(hr))
1541 rtv = render_target ? d3d8_surface_acquire_rendertarget_view(rt_impl) : NULL;
1542 if (render_target)
1544 if (SUCCEEDED(hr = wined3d_device_set_rendertarget_view(device->wined3d_device, 0, rtv, TRUE)))
1545 device_reset_viewport_state(device);
1546 else
1547 wined3d_device_set_depth_stencil_view(device->wined3d_device, original_dsv);
1549 d3d8_surface_release_rendertarget_view(rt_impl, rtv);
1552 wined3d_mutex_unlock();
1554 return hr;
1557 static HRESULT WINAPI d3d8_device_GetRenderTarget(IDirect3DDevice8 *iface, IDirect3DSurface8 **render_target)
1559 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1560 struct wined3d_rendertarget_view *wined3d_rtv;
1561 struct d3d8_surface *surface_impl;
1562 HRESULT hr;
1564 TRACE("iface %p, render_target %p.\n", iface, render_target);
1566 if (!render_target)
1567 return D3DERR_INVALIDCALL;
1569 wined3d_mutex_lock();
1570 if ((wined3d_rtv = wined3d_device_get_rendertarget_view(device->wined3d_device, 0)))
1572 /* We want the sub resource parent here, since the view itself may be
1573 * internal to wined3d and may not have a parent. */
1574 surface_impl = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_rtv);
1575 *render_target = &surface_impl->IDirect3DSurface8_iface;
1576 IDirect3DSurface8_AddRef(*render_target);
1577 hr = D3D_OK;
1579 else
1581 ERR("Failed to get wined3d render target.\n");
1582 *render_target = NULL;
1583 hr = D3DERR_NOTFOUND;
1585 wined3d_mutex_unlock();
1587 return hr;
1590 static HRESULT WINAPI d3d8_device_GetDepthStencilSurface(IDirect3DDevice8 *iface, IDirect3DSurface8 **depth_stencil)
1592 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1593 struct wined3d_rendertarget_view *wined3d_dsv;
1594 struct d3d8_surface *surface_impl;
1595 HRESULT hr = D3D_OK;
1597 TRACE("iface %p, depth_stencil %p.\n", iface, depth_stencil);
1599 if (!depth_stencil)
1600 return D3DERR_INVALIDCALL;
1602 wined3d_mutex_lock();
1603 if ((wined3d_dsv = wined3d_device_get_depth_stencil_view(device->wined3d_device)))
1605 /* We want the sub resource parent here, since the view itself may be
1606 * internal to wined3d and may not have a parent. */
1607 surface_impl = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_dsv);
1608 *depth_stencil = &surface_impl->IDirect3DSurface8_iface;
1609 IDirect3DSurface8_AddRef(*depth_stencil);
1611 else
1613 hr = D3DERR_NOTFOUND;
1614 *depth_stencil = NULL;
1616 wined3d_mutex_unlock();
1618 return hr;
1621 static HRESULT WINAPI d3d8_device_BeginScene(IDirect3DDevice8 *iface)
1623 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1624 HRESULT hr;
1626 TRACE("iface %p.\n", iface);
1628 wined3d_mutex_lock();
1629 hr = wined3d_device_begin_scene(device->wined3d_device);
1630 wined3d_mutex_unlock();
1632 return hr;
1635 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d8_device_EndScene(IDirect3DDevice8 *iface)
1637 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1638 HRESULT hr;
1640 TRACE("iface %p.\n", iface);
1642 wined3d_mutex_lock();
1643 hr = wined3d_device_end_scene(device->wined3d_device);
1644 wined3d_mutex_unlock();
1646 return hr;
1649 static HRESULT WINAPI d3d8_device_Clear(IDirect3DDevice8 *iface, DWORD rect_count,
1650 const D3DRECT *rects, DWORD flags, D3DCOLOR color, float z, DWORD stencil)
1652 const struct wined3d_color c =
1654 ((color >> 16) & 0xff) / 255.0f,
1655 ((color >> 8) & 0xff) / 255.0f,
1656 (color & 0xff) / 255.0f,
1657 ((color >> 24) & 0xff) / 255.0f,
1659 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1660 HRESULT hr;
1662 TRACE("iface %p, rect_count %u, rects %p, flags %#x, color 0x%08x, z %.8e, stencil %u.\n",
1663 iface, rect_count, rects, flags, color, z, stencil);
1665 if (rect_count && !rects)
1667 WARN("count %u with NULL rects.\n", rect_count);
1668 rect_count = 0;
1671 wined3d_mutex_lock();
1672 wined3d_device_apply_stateblock(device->wined3d_device, device->state);
1673 hr = wined3d_device_clear(device->wined3d_device, rect_count, (const RECT *)rects, flags, &c, z, stencil);
1674 wined3d_mutex_unlock();
1676 return hr;
1679 static HRESULT WINAPI d3d8_device_SetTransform(IDirect3DDevice8 *iface,
1680 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
1682 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1684 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1686 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1687 wined3d_mutex_lock();
1688 wined3d_stateblock_set_transform(device->update_state,
1689 wined3d_transform_state_from_d3d(state), (const struct wined3d_matrix *)matrix);
1690 wined3d_mutex_unlock();
1692 return D3D_OK;
1695 static HRESULT WINAPI d3d8_device_GetTransform(IDirect3DDevice8 *iface,
1696 D3DTRANSFORMSTATETYPE state, D3DMATRIX *matrix)
1698 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1700 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1702 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1703 wined3d_mutex_lock();
1704 memcpy(matrix, &device->stateblock_state->transforms[state], sizeof(*matrix));
1705 wined3d_mutex_unlock();
1707 return D3D_OK;
1710 static HRESULT WINAPI d3d8_device_MultiplyTransform(IDirect3DDevice8 *iface,
1711 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
1713 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1715 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1717 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1718 wined3d_mutex_lock();
1719 wined3d_stateblock_multiply_transform(device->state,
1720 wined3d_transform_state_from_d3d(state), (const struct wined3d_matrix *)matrix);
1721 wined3d_mutex_unlock();
1723 return D3D_OK;
1726 static HRESULT WINAPI d3d8_device_SetViewport(IDirect3DDevice8 *iface, const D3DVIEWPORT8 *viewport)
1728 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1729 struct wined3d_sub_resource_desc rt_desc;
1730 struct wined3d_rendertarget_view *rtv;
1731 struct d3d8_surface *surface;
1732 struct wined3d_viewport vp;
1734 TRACE("iface %p, viewport %p.\n", iface, viewport);
1736 wined3d_mutex_lock();
1737 if (!(rtv = wined3d_device_get_rendertarget_view(device->wined3d_device, 0)))
1739 wined3d_mutex_unlock();
1740 return D3DERR_NOTFOUND;
1742 surface = wined3d_rendertarget_view_get_sub_resource_parent(rtv);
1743 wined3d_texture_get_sub_resource_desc(surface->wined3d_texture, surface->sub_resource_idx, &rt_desc);
1745 if (viewport->X > rt_desc.width || viewport->Width > rt_desc.width - viewport->X
1746 || viewport->Y > rt_desc.height || viewport->Height > rt_desc.height - viewport->Y)
1748 WARN("Invalid viewport, returning D3DERR_INVALIDCALL.\n");
1749 wined3d_mutex_unlock();
1750 return D3DERR_INVALIDCALL;
1753 vp.x = viewport->X;
1754 vp.y = viewport->Y;
1755 vp.width = viewport->Width;
1756 vp.height = viewport->Height;
1757 vp.min_z = viewport->MinZ;
1758 vp.max_z = viewport->MaxZ;
1760 wined3d_stateblock_set_viewport(device->update_state, &vp);
1761 wined3d_mutex_unlock();
1763 return D3D_OK;
1766 static HRESULT WINAPI d3d8_device_GetViewport(IDirect3DDevice8 *iface, D3DVIEWPORT8 *viewport)
1768 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1769 struct wined3d_viewport wined3d_viewport;
1771 TRACE("iface %p, viewport %p.\n", iface, viewport);
1773 wined3d_mutex_lock();
1774 wined3d_viewport = device->stateblock_state->viewport;
1775 wined3d_mutex_unlock();
1777 viewport->X = wined3d_viewport.x;
1778 viewport->Y = wined3d_viewport.y;
1779 viewport->Width = wined3d_viewport.width;
1780 viewport->Height = wined3d_viewport.height;
1781 viewport->MinZ = wined3d_viewport.min_z;
1782 viewport->MaxZ = wined3d_viewport.max_z;
1784 return D3D_OK;
1787 static HRESULT WINAPI d3d8_device_SetMaterial(IDirect3DDevice8 *iface, const D3DMATERIAL8 *material)
1789 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1791 TRACE("iface %p, material %p.\n", iface, material);
1793 /* Note: D3DMATERIAL8 is compatible with struct wined3d_material. */
1794 wined3d_mutex_lock();
1795 wined3d_stateblock_set_material(device->update_state, (const struct wined3d_material *)material);
1796 wined3d_mutex_unlock();
1798 return D3D_OK;
1801 static HRESULT WINAPI d3d8_device_GetMaterial(IDirect3DDevice8 *iface, D3DMATERIAL8 *material)
1803 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1805 TRACE("iface %p, material %p.\n", iface, material);
1807 /* Note: D3DMATERIAL8 is compatible with struct wined3d_material. */
1808 wined3d_mutex_lock();
1809 memcpy(material, &device->stateblock_state->material, sizeof(*material));
1810 wined3d_mutex_unlock();
1812 return D3D_OK;
1815 static HRESULT WINAPI d3d8_device_SetLight(IDirect3DDevice8 *iface, DWORD index, const D3DLIGHT8 *light)
1817 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1818 HRESULT hr;
1820 TRACE("iface %p, index %u, light %p.\n", iface, index, light);
1822 /* Note: D3DLIGHT8 is compatible with struct wined3d_light. */
1823 wined3d_mutex_lock();
1824 hr = wined3d_stateblock_set_light(device->update_state, index, (const struct wined3d_light *)light);
1825 wined3d_mutex_unlock();
1827 return hr;
1830 static HRESULT WINAPI d3d8_device_GetLight(IDirect3DDevice8 *iface, DWORD index, D3DLIGHT8 *light)
1832 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1833 BOOL enabled;
1834 HRESULT hr;
1836 TRACE("iface %p, index %u, light %p.\n", iface, index, light);
1838 /* Note: D3DLIGHT8 is compatible with struct wined3d_light. */
1839 wined3d_mutex_lock();
1840 hr = wined3d_stateblock_get_light(device->state, index, (struct wined3d_light *)light, &enabled);
1841 wined3d_mutex_unlock();
1843 return hr;
1846 static HRESULT WINAPI d3d8_device_LightEnable(IDirect3DDevice8 *iface, DWORD index, BOOL enable)
1848 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1849 HRESULT hr;
1851 TRACE("iface %p, index %u, enable %#x.\n", iface, index, enable);
1853 wined3d_mutex_lock();
1854 hr = wined3d_stateblock_set_light_enable(device->update_state, index, enable);
1855 wined3d_mutex_unlock();
1857 return hr;
1860 static HRESULT WINAPI d3d8_device_GetLightEnable(IDirect3DDevice8 *iface, DWORD index, BOOL *enabled)
1862 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1863 struct wined3d_light light;
1864 HRESULT hr;
1866 TRACE("iface %p, index %u, enabled %p.\n", iface, index, enabled);
1868 wined3d_mutex_lock();
1869 hr = wined3d_stateblock_get_light(device->state, index, &light, enabled);
1870 wined3d_mutex_unlock();
1872 return hr;
1875 static HRESULT WINAPI d3d8_device_SetClipPlane(IDirect3DDevice8 *iface, DWORD index, const float *plane)
1877 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1878 HRESULT hr;
1880 TRACE("iface %p, index %u, plane %p.\n", iface, index, plane);
1882 wined3d_mutex_lock();
1883 hr = wined3d_stateblock_set_clip_plane(device->update_state, index, (const struct wined3d_vec4 *)plane);
1884 wined3d_mutex_unlock();
1886 return hr;
1889 static HRESULT WINAPI d3d8_device_GetClipPlane(IDirect3DDevice8 *iface, DWORD index, float *plane)
1891 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1893 TRACE("iface %p, index %u, plane %p.\n", iface, index, plane);
1895 if (index >= device->max_user_clip_planes)
1897 TRACE("Plane %u requested, but only %u planes are supported.\n", index, device->max_user_clip_planes);
1898 return WINED3DERR_INVALIDCALL;
1901 wined3d_mutex_lock();
1902 memcpy(plane, &device->stateblock_state->clip_planes[index], sizeof(struct wined3d_vec4));
1903 wined3d_mutex_unlock();
1905 return D3D_OK;
1908 static void resolve_depth_buffer(struct d3d8_device *device)
1910 const struct wined3d_stateblock_state *state = device->stateblock_state;
1911 struct wined3d_rendertarget_view *wined3d_dsv;
1912 struct wined3d_resource *dst_resource;
1913 struct wined3d_texture *dst_texture;
1914 struct wined3d_resource_desc desc;
1915 struct d3d8_surface *d3d8_dsv;
1917 if (!(dst_texture = state->textures[0]))
1918 return;
1919 dst_resource = wined3d_texture_get_resource(dst_texture);
1920 wined3d_resource_get_desc(dst_resource, &desc);
1921 if (desc.format != WINED3DFMT_D24_UNORM_S8_UINT
1922 && desc.format != WINED3DFMT_X8D24_UNORM
1923 && desc.format != WINED3DFMT_DF16
1924 && desc.format != WINED3DFMT_DF24
1925 && desc.format != WINED3DFMT_INTZ)
1926 return;
1928 if (!(wined3d_dsv = wined3d_device_get_depth_stencil_view(device->wined3d_device)))
1929 return;
1930 d3d8_dsv = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_dsv);
1932 wined3d_device_resolve_sub_resource(device->wined3d_device, dst_resource, 0,
1933 wined3d_rendertarget_view_get_resource(wined3d_dsv), d3d8_dsv->sub_resource_idx, desc.format);
1936 static HRESULT WINAPI d3d8_device_SetRenderState(IDirect3DDevice8 *iface,
1937 D3DRENDERSTATETYPE state, DWORD value)
1939 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1941 TRACE("iface %p, state %#x, value %#x.\n", iface, state, value);
1943 wined3d_mutex_lock();
1944 wined3d_stateblock_set_render_state(device->update_state, wined3d_render_state_from_d3d(state), value);
1945 if (state == D3DRS_POINTSIZE && value == D3D8_RESZ_CODE)
1946 resolve_depth_buffer(device);
1947 wined3d_mutex_unlock();
1949 return D3D_OK;
1952 static HRESULT WINAPI d3d8_device_GetRenderState(IDirect3DDevice8 *iface,
1953 D3DRENDERSTATETYPE state, DWORD *value)
1955 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1956 const struct wined3d_stateblock_state *device_state;
1958 TRACE("iface %p, state %#x, value %p.\n", iface, state, value);
1960 wined3d_mutex_lock();
1961 device_state = device->stateblock_state;
1962 switch (state)
1964 case D3DRS_ZBIAS:
1965 *value = device_state->rs[WINED3D_RS_DEPTHBIAS];
1966 break;
1968 default:
1969 *value = device_state->rs[state];
1971 wined3d_mutex_unlock();
1973 return D3D_OK;
1976 static HRESULT WINAPI d3d8_device_BeginStateBlock(IDirect3DDevice8 *iface)
1978 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1979 struct wined3d_stateblock *stateblock;
1980 HRESULT hr;
1982 TRACE("iface %p.\n", iface);
1984 wined3d_mutex_lock();
1985 if (device->recording)
1987 wined3d_mutex_unlock();
1988 WARN("Trying to begin a stateblock while recording, returning D3DERR_INVALIDCALL.\n");
1989 return D3DERR_INVALIDCALL;
1992 if (SUCCEEDED(hr = wined3d_stateblock_create(device->wined3d_device, NULL, WINED3D_SBT_RECORDED, &stateblock)))
1993 device->update_state = device->recording = stateblock;
1994 wined3d_mutex_unlock();
1996 return hr;
1999 static HRESULT WINAPI d3d8_device_EndStateBlock(IDirect3DDevice8 *iface, DWORD *token)
2001 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2002 struct wined3d_stateblock *stateblock;
2004 TRACE("iface %p, token %p.\n", iface, token);
2006 /* Tell wineD3D to endstateblock before anything else (in case we run out
2007 * of memory later and cause locking problems)
2009 wined3d_mutex_lock();
2010 if (!device->recording)
2012 wined3d_mutex_unlock();
2013 WARN("Trying to end a stateblock, but no stateblock is being recorded.\n");
2014 return D3DERR_INVALIDCALL;
2016 stateblock = device->recording;
2017 wined3d_stateblock_init_contained_states(stateblock);
2018 device->recording = NULL;
2019 device->update_state = device->state;
2021 *token = d3d8_allocate_handle(&device->handle_table, stateblock, D3D8_HANDLE_SB);
2022 wined3d_mutex_unlock();
2024 if (*token == D3D8_INVALID_HANDLE)
2026 ERR("Failed to create a handle\n");
2027 wined3d_mutex_lock();
2028 wined3d_stateblock_decref(stateblock);
2029 wined3d_mutex_unlock();
2030 return E_FAIL;
2032 ++*token;
2034 TRACE("Returning %#x (%p).\n", *token, stateblock);
2036 return D3D_OK;
2039 static HRESULT WINAPI d3d8_device_ApplyStateBlock(IDirect3DDevice8 *iface, DWORD token)
2041 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2042 const struct wined3d_stateblock_state *state;
2043 struct wined3d_stateblock *stateblock;
2044 struct wined3d_buffer *wined3d_buffer;
2045 struct d3d8_vertexbuffer *buffer;
2046 unsigned int i;
2048 TRACE("iface %p, token %#x.\n", iface, token);
2050 if (!token)
2051 return D3D_OK;
2053 wined3d_mutex_lock();
2054 if (device->recording)
2056 wined3d_mutex_unlock();
2057 WARN("Trying to apply stateblock while recording, returning D3DERR_INVALIDCALL.\n");
2058 return D3DERR_INVALIDCALL;
2060 stateblock = d3d8_get_object(&device->handle_table, token - 1, D3D8_HANDLE_SB);
2061 if (!stateblock)
2063 WARN("Invalid handle (%#x) passed.\n", token);
2064 wined3d_mutex_unlock();
2065 return D3DERR_INVALIDCALL;
2067 wined3d_stateblock_apply(stateblock, device->state);
2068 device->sysmem_vb = 0;
2069 state = device->stateblock_state;
2070 for (i = 0; i < D3D8_MAX_STREAMS; ++i)
2072 wined3d_buffer = state->streams[i].buffer;
2073 if (!wined3d_buffer || !(buffer = wined3d_buffer_get_parent(wined3d_buffer)))
2074 continue;
2075 if (buffer->draw_buffer)
2076 device->sysmem_vb |= 1u << i;
2078 device->sysmem_ib = (wined3d_buffer = state->index_buffer)
2079 && (buffer = wined3d_buffer_get_parent(wined3d_buffer)) && buffer->draw_buffer;
2080 wined3d_mutex_unlock();
2082 return D3D_OK;
2085 static HRESULT WINAPI d3d8_device_CaptureStateBlock(IDirect3DDevice8 *iface, DWORD token)
2087 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2088 struct wined3d_stateblock *stateblock;
2090 TRACE("iface %p, token %#x.\n", iface, token);
2092 wined3d_mutex_lock();
2093 if (device->recording)
2095 wined3d_mutex_unlock();
2096 WARN("Trying to capture stateblock while recording, returning D3DERR_INVALIDCALL.\n");
2097 return D3DERR_INVALIDCALL;
2099 stateblock = d3d8_get_object(&device->handle_table, token - 1, D3D8_HANDLE_SB);
2100 if (!stateblock)
2102 WARN("Invalid handle (%#x) passed.\n", token);
2103 wined3d_mutex_unlock();
2104 return D3DERR_INVALIDCALL;
2106 wined3d_stateblock_capture(stateblock, device->state);
2107 wined3d_mutex_unlock();
2109 return D3D_OK;
2112 static HRESULT WINAPI d3d8_device_DeleteStateBlock(IDirect3DDevice8 *iface, DWORD token)
2114 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2115 struct wined3d_stateblock *stateblock;
2117 TRACE("iface %p, token %#x.\n", iface, token);
2119 wined3d_mutex_lock();
2120 stateblock = d3d8_free_handle(&device->handle_table, token - 1, D3D8_HANDLE_SB);
2122 if (!stateblock)
2124 WARN("Invalid handle (%#x) passed.\n", token);
2125 wined3d_mutex_unlock();
2126 return D3DERR_INVALIDCALL;
2129 if (wined3d_stateblock_decref(stateblock))
2131 ERR("Stateblock %p has references left, this shouldn't happen.\n", stateblock);
2133 wined3d_mutex_unlock();
2135 return D3D_OK;
2138 static HRESULT WINAPI d3d8_device_CreateStateBlock(IDirect3DDevice8 *iface,
2139 D3DSTATEBLOCKTYPE type, DWORD *handle)
2141 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2142 struct wined3d_stateblock *stateblock;
2143 HRESULT hr;
2145 TRACE("iface %p, type %#x, handle %p.\n", iface, type, handle);
2147 if (type != D3DSBT_ALL
2148 && type != D3DSBT_PIXELSTATE
2149 && type != D3DSBT_VERTEXSTATE)
2151 WARN("Unexpected stateblock type, returning D3DERR_INVALIDCALL\n");
2152 return D3DERR_INVALIDCALL;
2155 wined3d_mutex_lock();
2156 if (device->recording)
2158 wined3d_mutex_unlock();
2159 WARN("Trying to create a stateblock while recording, returning D3DERR_INVALIDCALL.\n");
2160 return D3DERR_INVALIDCALL;
2162 hr = wined3d_stateblock_create(device->wined3d_device, device->state, (enum wined3d_stateblock_type)type, &stateblock);
2163 if (FAILED(hr))
2165 wined3d_mutex_unlock();
2166 ERR("Failed to create the state block, hr %#x\n", hr);
2167 return hr;
2170 *handle = d3d8_allocate_handle(&device->handle_table, stateblock, D3D8_HANDLE_SB);
2171 wined3d_mutex_unlock();
2173 if (*handle == D3D8_INVALID_HANDLE)
2175 ERR("Failed to allocate a handle.\n");
2176 wined3d_mutex_lock();
2177 wined3d_stateblock_decref(stateblock);
2178 wined3d_mutex_unlock();
2179 return E_FAIL;
2181 ++*handle;
2183 TRACE("Returning %#x (%p).\n", *handle, stateblock);
2185 return hr;
2188 static HRESULT WINAPI d3d8_device_SetClipStatus(IDirect3DDevice8 *iface, const D3DCLIPSTATUS8 *clip_status)
2190 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2191 HRESULT hr;
2193 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
2194 /* FIXME: Verify that D3DCLIPSTATUS8 ~= struct wined3d_clip_status. */
2196 wined3d_mutex_lock();
2197 hr = wined3d_device_set_clip_status(device->wined3d_device, (const struct wined3d_clip_status *)clip_status);
2198 wined3d_mutex_unlock();
2200 return hr;
2203 static HRESULT WINAPI d3d8_device_GetClipStatus(IDirect3DDevice8 *iface, D3DCLIPSTATUS8 *clip_status)
2205 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2206 HRESULT hr;
2208 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
2210 wined3d_mutex_lock();
2211 hr = wined3d_device_get_clip_status(device->wined3d_device, (struct wined3d_clip_status *)clip_status);
2212 wined3d_mutex_unlock();
2214 return hr;
2217 static HRESULT WINAPI d3d8_device_GetTexture(IDirect3DDevice8 *iface, DWORD stage, IDirect3DBaseTexture8 **texture)
2219 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2220 struct wined3d_texture *wined3d_texture;
2221 struct d3d8_texture *texture_impl;
2223 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
2225 if (!texture)
2226 return D3DERR_INVALIDCALL;
2228 if (stage >= WINED3D_MAX_FRAGMENT_SAMPLERS)
2230 WARN("Ignoring invalid stage %u.\n", stage);
2231 *texture = NULL;
2232 return D3D_OK;
2235 wined3d_mutex_lock();
2236 if ((wined3d_texture = device->stateblock_state->textures[stage]))
2238 texture_impl = wined3d_texture_get_parent(wined3d_texture);
2239 *texture = &texture_impl->IDirect3DBaseTexture8_iface;
2240 IDirect3DBaseTexture8_AddRef(*texture);
2242 else
2244 *texture = NULL;
2246 wined3d_mutex_unlock();
2248 return D3D_OK;
2251 static HRESULT WINAPI d3d8_device_SetTexture(IDirect3DDevice8 *iface, DWORD stage, IDirect3DBaseTexture8 *texture)
2253 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2254 struct d3d8_texture *texture_impl;
2256 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
2258 texture_impl = unsafe_impl_from_IDirect3DBaseTexture8(texture);
2260 wined3d_mutex_lock();
2261 wined3d_stateblock_set_texture(device->update_state, stage,
2262 texture_impl ? texture_impl->wined3d_texture : NULL);
2263 wined3d_mutex_unlock();
2265 return D3D_OK;
2268 static const struct tss_lookup
2270 BOOL sampler_state;
2271 union
2273 enum wined3d_texture_stage_state texture_state;
2274 enum wined3d_sampler_state sampler_state;
2275 } u;
2277 tss_lookup[] =
2279 {FALSE, {WINED3D_TSS_INVALID}}, /* 0, unused */
2280 {FALSE, {WINED3D_TSS_COLOR_OP}}, /* 1, D3DTSS_COLOROP */
2281 {FALSE, {WINED3D_TSS_COLOR_ARG1}}, /* 2, D3DTSS_COLORARG1 */
2282 {FALSE, {WINED3D_TSS_COLOR_ARG2}}, /* 3, D3DTSS_COLORARG2 */
2283 {FALSE, {WINED3D_TSS_ALPHA_OP}}, /* 4, D3DTSS_ALPHAOP */
2284 {FALSE, {WINED3D_TSS_ALPHA_ARG1}}, /* 5, D3DTSS_ALPHAARG1 */
2285 {FALSE, {WINED3D_TSS_ALPHA_ARG2}}, /* 6, D3DTSS_ALPHAARG2 */
2286 {FALSE, {WINED3D_TSS_BUMPENV_MAT00}}, /* 7, D3DTSS_BUMPENVMAT00 */
2287 {FALSE, {WINED3D_TSS_BUMPENV_MAT01}}, /* 8, D3DTSS_BUMPENVMAT01 */
2288 {FALSE, {WINED3D_TSS_BUMPENV_MAT10}}, /* 9, D3DTSS_BUMPENVMAT10 */
2289 {FALSE, {WINED3D_TSS_BUMPENV_MAT11}}, /* 10, D3DTSS_BUMPENVMAT11 */
2290 {FALSE, {WINED3D_TSS_TEXCOORD_INDEX}}, /* 11, D3DTSS_TEXCOORDINDEX */
2291 {FALSE, {WINED3D_TSS_INVALID}}, /* 12, unused */
2292 {TRUE, {WINED3D_SAMP_ADDRESS_U}}, /* 13, D3DTSS_ADDRESSU */
2293 {TRUE, {WINED3D_SAMP_ADDRESS_V}}, /* 14, D3DTSS_ADDRESSV */
2294 {TRUE, {WINED3D_SAMP_BORDER_COLOR}}, /* 15, D3DTSS_BORDERCOLOR */
2295 {TRUE, {WINED3D_SAMP_MAG_FILTER}}, /* 16, D3DTSS_MAGFILTER */
2296 {TRUE, {WINED3D_SAMP_MIN_FILTER}}, /* 17, D3DTSS_MINFILTER */
2297 {TRUE, {WINED3D_SAMP_MIP_FILTER}}, /* 18, D3DTSS_MIPFILTER */
2298 {TRUE, {WINED3D_SAMP_MIPMAP_LOD_BIAS}}, /* 19, D3DTSS_MIPMAPLODBIAS */
2299 {TRUE, {WINED3D_SAMP_MAX_MIP_LEVEL}}, /* 20, D3DTSS_MAXMIPLEVEL */
2300 {TRUE, {WINED3D_SAMP_MAX_ANISOTROPY}}, /* 21, D3DTSS_MAXANISOTROPY */
2301 {FALSE, {WINED3D_TSS_BUMPENV_LSCALE}}, /* 22, D3DTSS_BUMPENVLSCALE */
2302 {FALSE, {WINED3D_TSS_BUMPENV_LOFFSET}}, /* 23, D3DTSS_BUMPENVLOFFSET */
2303 {FALSE, {WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS}}, /* 24, D3DTSS_TEXTURETRANSFORMFLAGS */
2304 {TRUE, {WINED3D_SAMP_ADDRESS_W}}, /* 25, D3DTSS_ADDRESSW */
2305 {FALSE, {WINED3D_TSS_COLOR_ARG0}}, /* 26, D3DTSS_COLORARG0 */
2306 {FALSE, {WINED3D_TSS_ALPHA_ARG0}}, /* 27, D3DTSS_ALPHAARG0 */
2307 {FALSE, {WINED3D_TSS_RESULT_ARG}}, /* 28, D3DTSS_RESULTARG */
2310 static HRESULT WINAPI d3d8_device_GetTextureStageState(IDirect3DDevice8 *iface,
2311 DWORD stage, D3DTEXTURESTAGESTATETYPE state, DWORD *value)
2313 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2314 const struct wined3d_stateblock_state *device_state;
2315 const struct tss_lookup *l;
2317 TRACE("iface %p, stage %u, state %#x, value %p.\n", iface, stage, state, value);
2319 if (state >= ARRAY_SIZE(tss_lookup))
2321 WARN("Invalid state %#x.\n", state);
2322 return D3D_OK;
2325 l = &tss_lookup[state];
2327 wined3d_mutex_lock();
2328 device_state = device->stateblock_state;
2329 if (l->sampler_state)
2330 *value = device_state->sampler_states[stage][l->u.sampler_state];
2331 else
2332 *value = device_state->texture_states[stage][l->u.texture_state];
2333 wined3d_mutex_unlock();
2335 return D3D_OK;
2338 static HRESULT WINAPI d3d8_device_SetTextureStageState(IDirect3DDevice8 *iface,
2339 DWORD stage, D3DTEXTURESTAGESTATETYPE type, DWORD value)
2341 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2342 const struct tss_lookup *l;
2344 TRACE("iface %p, stage %u, state %#x, value %#x.\n", iface, stage, type, value);
2346 if (type >= ARRAY_SIZE(tss_lookup))
2348 WARN("Invalid type %#x passed.\n", type);
2349 return D3D_OK;
2352 l = &tss_lookup[type];
2354 wined3d_mutex_lock();
2355 if (l->sampler_state)
2356 wined3d_stateblock_set_sampler_state(device->update_state, stage, l->u.sampler_state, value);
2357 else
2358 wined3d_stateblock_set_texture_stage_state(device->update_state, stage, l->u.texture_state, value);
2359 wined3d_mutex_unlock();
2361 return D3D_OK;
2364 static HRESULT WINAPI d3d8_device_ValidateDevice(IDirect3DDevice8 *iface, DWORD *pass_count)
2366 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2367 HRESULT hr;
2369 TRACE("iface %p, pass_count %p.\n", iface, pass_count);
2371 wined3d_mutex_lock();
2372 wined3d_device_apply_stateblock(device->wined3d_device, device->state);
2373 hr = wined3d_device_validate_device(device->wined3d_device, pass_count);
2374 wined3d_mutex_unlock();
2376 return hr;
2379 static HRESULT WINAPI d3d8_device_GetInfo(IDirect3DDevice8 *iface,
2380 DWORD info_id, void *info, DWORD info_size)
2382 TRACE("iface %p, info_id %#x, info %p, info_size %u.\n", iface, info_id, info, info_size);
2384 if (info_id < 4)
2385 return E_FAIL;
2386 return S_FALSE;
2389 static HRESULT WINAPI d3d8_device_SetPaletteEntries(IDirect3DDevice8 *iface,
2390 UINT palette_idx, const PALETTEENTRY *entries)
2392 WARN("iface %p, palette_idx %u, entries %p unimplemented\n", iface, palette_idx, entries);
2394 /* GPUs stopped supporting palettized textures with the Shader Model 1 generation. Wined3d
2395 * does not have a d3d8/9-style palette API */
2397 return D3D_OK;
2400 static HRESULT WINAPI d3d8_device_GetPaletteEntries(IDirect3DDevice8 *iface,
2401 UINT palette_idx, PALETTEENTRY *entries)
2403 FIXME("iface %p, palette_idx %u, entries %p unimplemented.\n", iface, palette_idx, entries);
2405 return D3DERR_INVALIDCALL;
2408 static HRESULT WINAPI d3d8_device_SetCurrentTexturePalette(IDirect3DDevice8 *iface, UINT palette_idx)
2410 WARN("iface %p, palette_idx %u unimplemented.\n", iface, palette_idx);
2412 return D3D_OK;
2415 static HRESULT WINAPI d3d8_device_GetCurrentTexturePalette(IDirect3DDevice8 *iface, UINT *palette_idx)
2417 FIXME("iface %p, palette_idx %p unimplemented.\n", iface, palette_idx);
2419 return D3DERR_INVALIDCALL;
2422 static void d3d8_device_upload_sysmem_vertex_buffers(struct d3d8_device *device,
2423 unsigned int start_vertex, unsigned int vertex_count)
2425 const struct wined3d_stateblock_state *state = device->stateblock_state;
2426 struct wined3d_vertex_declaration *wined3d_decl;
2427 struct wined3d_box box = {0, 0, 0, 1, 0, 1};
2428 const struct wined3d_stream_state *stream;
2429 struct d3d8_vertexbuffer *d3d8_buffer;
2430 struct wined3d_resource *dst_resource;
2431 struct d3d8_vertex_declaration *decl;
2432 struct wined3d_buffer *dst_buffer;
2433 struct wined3d_resource_desc desc;
2434 unsigned int stride, map;
2435 HRESULT hr;
2437 if (!device->sysmem_vb)
2438 return;
2440 if (!(wined3d_decl = state->vertex_declaration))
2441 return;
2443 decl = wined3d_vertex_declaration_get_parent(wined3d_decl);
2444 map = decl->stream_map & device->sysmem_vb;
2445 while (map)
2447 stream = &state->streams[wined3d_bit_scan(&map)];
2448 dst_buffer = stream->buffer;
2449 stride = stream->stride;
2451 d3d8_buffer = wined3d_buffer_get_parent(dst_buffer);
2452 dst_resource = wined3d_buffer_get_resource(dst_buffer);
2453 wined3d_resource_get_desc(dst_resource, &desc);
2454 box.left = stream->offset + start_vertex * stride;
2455 box.right = min(box.left + vertex_count * stride, desc.size);
2456 if (FAILED(hr = wined3d_device_copy_sub_resource_region(device->wined3d_device,
2457 dst_resource, 0, box.left, 0, 0,
2458 wined3d_buffer_get_resource(d3d8_buffer->wined3d_buffer), 0, &box, 0)))
2459 ERR("Failed to update buffer.\n");
2463 static void d3d8_device_upload_sysmem_index_buffer(struct d3d8_device *device,
2464 unsigned int start_idx, unsigned int idx_count)
2466 const struct wined3d_stateblock_state *state = device->stateblock_state;
2467 struct wined3d_box box = {0, 0, 0, 1, 0, 1};
2468 struct wined3d_resource *dst_resource;
2469 struct d3d8_indexbuffer *d3d8_buffer;
2470 struct wined3d_buffer *dst_buffer;
2471 struct wined3d_resource_desc desc;
2472 enum wined3d_format_id format;
2473 unsigned int idx_size;
2474 HRESULT hr;
2476 if (!device->sysmem_ib)
2477 return;
2479 dst_buffer = state->index_buffer;
2480 format = state->index_format;
2481 d3d8_buffer = wined3d_buffer_get_parent(dst_buffer);
2482 dst_resource = wined3d_buffer_get_resource(dst_buffer);
2483 wined3d_resource_get_desc(dst_resource, &desc);
2484 idx_size = format == WINED3DFMT_R16_UINT ? 2 : 4;
2485 box.left = start_idx * idx_size;
2486 box.right = min(box.left + idx_count * idx_size, desc.size);
2487 if (FAILED(hr = wined3d_device_copy_sub_resource_region(device->wined3d_device,
2488 dst_resource, 0, box.left, 0, 0,
2489 wined3d_buffer_get_resource(d3d8_buffer->wined3d_buffer), 0, &box, 0)))
2490 ERR("Failed to update buffer.\n");
2493 static HRESULT WINAPI d3d8_device_DrawPrimitive(IDirect3DDevice8 *iface,
2494 D3DPRIMITIVETYPE primitive_type, UINT start_vertex, UINT primitive_count)
2496 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2497 unsigned int vertex_count;
2498 HRESULT hr;
2500 TRACE("iface %p, primitive_type %#x, start_vertex %u, primitive_count %u.\n",
2501 iface, primitive_type, start_vertex, primitive_count);
2503 vertex_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2504 wined3d_mutex_lock();
2505 d3d8_device_upload_sysmem_vertex_buffers(device, start_vertex, vertex_count);
2506 wined3d_device_set_primitive_type(device->wined3d_device, wined3d_primitive_type_from_d3d(primitive_type), 0);
2507 wined3d_device_apply_stateblock(device->wined3d_device, device->state);
2508 hr = wined3d_device_draw_primitive(device->wined3d_device, start_vertex, vertex_count);
2509 wined3d_mutex_unlock();
2511 return hr;
2514 static HRESULT WINAPI d3d8_device_DrawIndexedPrimitive(IDirect3DDevice8 *iface,
2515 D3DPRIMITIVETYPE primitive_type, UINT min_vertex_idx, UINT vertex_count,
2516 UINT start_idx, UINT primitive_count)
2518 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2519 unsigned int index_count;
2520 int base_vertex_index;
2521 HRESULT hr;
2523 TRACE("iface %p, primitive_type %#x, min_vertex_idx %u, vertex_count %u, start_idx %u, primitive_count %u.\n",
2524 iface, primitive_type, min_vertex_idx, vertex_count, start_idx, primitive_count);
2526 index_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2527 wined3d_mutex_lock();
2528 base_vertex_index = device->stateblock_state->base_vertex_index;
2529 d3d8_device_upload_sysmem_vertex_buffers(device, base_vertex_index + min_vertex_idx, vertex_count);
2530 d3d8_device_upload_sysmem_index_buffer(device, start_idx, index_count);
2531 wined3d_device_set_primitive_type(device->wined3d_device, wined3d_primitive_type_from_d3d(primitive_type), 0);
2532 wined3d_device_apply_stateblock(device->wined3d_device, device->state);
2533 hr = wined3d_device_draw_indexed_primitive(device->wined3d_device, start_idx, index_count);
2534 wined3d_mutex_unlock();
2536 return hr;
2539 /* The caller is responsible for wined3d locking */
2540 static HRESULT d3d8_device_prepare_vertex_buffer(struct d3d8_device *device, UINT min_size)
2542 HRESULT hr;
2544 if (device->vertex_buffer_size < min_size || !device->vertex_buffer)
2546 UINT size = max(device->vertex_buffer_size * 2, min_size);
2547 struct wined3d_buffer_desc desc;
2548 struct wined3d_buffer *buffer;
2550 TRACE("Growing vertex buffer to %u bytes\n", size);
2552 desc.byte_width = size;
2553 desc.usage = WINED3DUSAGE_DYNAMIC;
2554 desc.bind_flags = WINED3D_BIND_VERTEX_BUFFER;
2555 desc.access = WINED3D_RESOURCE_ACCESS_GPU | WINED3D_RESOURCE_ACCESS_MAP_W;
2556 desc.misc_flags = 0;
2557 desc.structure_byte_stride = 0;
2559 if (FAILED(hr = wined3d_buffer_create(device->wined3d_device, &desc,
2560 NULL, NULL, &d3d8_null_wined3d_parent_ops, &buffer)))
2562 ERR("Failed to create vertex buffer, hr %#x.\n", hr);
2563 return hr;
2566 if (device->vertex_buffer)
2567 wined3d_buffer_decref(device->vertex_buffer);
2569 device->vertex_buffer = buffer;
2570 device->vertex_buffer_size = size;
2571 device->vertex_buffer_pos = 0;
2573 return D3D_OK;
2576 static HRESULT WINAPI d3d8_device_DrawPrimitiveUP(IDirect3DDevice8 *iface,
2577 D3DPRIMITIVETYPE primitive_type, UINT primitive_count, const void *data,
2578 UINT stride)
2580 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2581 HRESULT hr;
2582 UINT vtx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2583 struct wined3d_map_desc wined3d_map_desc;
2584 struct wined3d_box wined3d_box = {0};
2585 UINT size = vtx_count * stride;
2586 struct wined3d_resource *vb;
2587 UINT vb_pos, align;
2589 TRACE("iface %p, primitive_type %#x, primitive_count %u, data %p, stride %u.\n",
2590 iface, primitive_type, primitive_count, data, stride);
2592 if (!primitive_count || !stride)
2594 WARN("primitive_count or stride is 0, returning D3D_OK.\n");
2595 return D3D_OK;
2598 wined3d_mutex_lock();
2599 hr = d3d8_device_prepare_vertex_buffer(device, size);
2600 if (FAILED(hr))
2601 goto done;
2603 vb_pos = device->vertex_buffer_pos;
2604 align = vb_pos % stride;
2605 if (align) align = stride - align;
2606 if (vb_pos + size + align > device->vertex_buffer_size)
2607 vb_pos = 0;
2608 else
2609 vb_pos += align;
2611 wined3d_box.left = vb_pos;
2612 wined3d_box.right = vb_pos + size;
2613 vb = wined3d_buffer_get_resource(device->vertex_buffer);
2614 if (FAILED(wined3d_resource_map(vb, 0, &wined3d_map_desc, &wined3d_box,
2615 WINED3D_MAP_WRITE | (vb_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD))))
2616 goto done;
2617 memcpy(wined3d_map_desc.data, data, size);
2618 wined3d_resource_unmap(vb, 0);
2619 device->vertex_buffer_pos = vb_pos + size;
2621 hr = wined3d_stateblock_set_stream_source(device->state, 0, device->vertex_buffer, 0, stride);
2622 if (FAILED(hr))
2623 goto done;
2625 wined3d_device_set_primitive_type(device->wined3d_device, wined3d_primitive_type_from_d3d(primitive_type), 0);
2626 wined3d_device_apply_stateblock(device->wined3d_device, device->state);
2627 hr = wined3d_device_draw_primitive(device->wined3d_device, vb_pos / stride, vtx_count);
2628 wined3d_stateblock_set_stream_source(device->state, 0, NULL, 0, 0);
2630 done:
2631 wined3d_mutex_unlock();
2632 return hr;
2635 /* The caller is responsible for wined3d locking */
2636 static HRESULT d3d8_device_prepare_index_buffer(struct d3d8_device *device, UINT min_size)
2638 HRESULT hr;
2640 if (device->index_buffer_size < min_size || !device->index_buffer)
2642 UINT size = max(device->index_buffer_size * 2, min_size);
2643 struct wined3d_buffer_desc desc;
2644 struct wined3d_buffer *buffer;
2646 TRACE("Growing index buffer to %u bytes\n", size);
2648 desc.byte_width = size;
2649 desc.usage = WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_STATICDECL;
2650 desc.bind_flags = WINED3D_BIND_INDEX_BUFFER;
2651 desc.access = WINED3D_RESOURCE_ACCESS_GPU | WINED3D_RESOURCE_ACCESS_MAP_W;
2652 desc.misc_flags = 0;
2653 desc.structure_byte_stride = 0;
2655 if (FAILED(hr = wined3d_buffer_create(device->wined3d_device, &desc,
2656 NULL, NULL, &d3d8_null_wined3d_parent_ops, &buffer)))
2658 ERR("Failed to create index buffer, hr %#x.\n", hr);
2659 return hr;
2662 if (device->index_buffer)
2663 wined3d_buffer_decref(device->index_buffer);
2665 device->index_buffer = buffer;
2666 device->index_buffer_size = size;
2667 device->index_buffer_pos = 0;
2669 return D3D_OK;
2672 static HRESULT WINAPI d3d8_device_DrawIndexedPrimitiveUP(IDirect3DDevice8 *iface,
2673 D3DPRIMITIVETYPE primitive_type, UINT min_vertex_idx, UINT vertex_count,
2674 UINT primitive_count, const void *index_data, D3DFORMAT index_format,
2675 const void *vertex_data, UINT vertex_stride)
2677 UINT idx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2678 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2679 UINT idx_fmt_size = index_format == D3DFMT_INDEX16 ? 2 : 4;
2680 UINT vtx_size = vertex_count * vertex_stride;
2681 UINT idx_size = idx_count * idx_fmt_size;
2682 struct wined3d_map_desc wined3d_map_desc;
2683 struct wined3d_box wined3d_box = {0};
2684 struct wined3d_resource *ib, *vb;
2685 UINT vb_pos, ib_pos, align;
2686 HRESULT hr;
2688 TRACE("iface %p, primitive_type %#x, min_vertex_idx %u, vertex_count %u, primitive_count %u, "
2689 "index_data %p, index_format %#x, vertex_data %p, vertex_stride %u.\n",
2690 iface, primitive_type, min_vertex_idx, vertex_count, primitive_count,
2691 index_data, index_format, vertex_data, vertex_stride);
2693 if (!primitive_count || !vertex_stride)
2695 WARN("primitive_count or vertex_stride is 0, returning D3D_OK.\n");
2696 return D3D_OK;
2699 wined3d_mutex_lock();
2701 hr = d3d8_device_prepare_vertex_buffer(device, vtx_size);
2702 if (FAILED(hr))
2703 goto done;
2705 vb_pos = device->vertex_buffer_pos;
2706 align = vb_pos % vertex_stride;
2707 if (align) align = vertex_stride - align;
2708 if (vb_pos + vtx_size + align > device->vertex_buffer_size)
2709 vb_pos = 0;
2710 else
2711 vb_pos += align;
2713 wined3d_box.left = vb_pos;
2714 wined3d_box.right = vb_pos + vtx_size;
2715 vb = wined3d_buffer_get_resource(device->vertex_buffer);
2716 if (FAILED(hr = wined3d_resource_map(vb, 0, &wined3d_map_desc, &wined3d_box,
2717 WINED3D_MAP_WRITE | (vb_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD))))
2718 goto done;
2719 memcpy(wined3d_map_desc.data, (char *)vertex_data + min_vertex_idx * vertex_stride, vtx_size);
2720 wined3d_resource_unmap(vb, 0);
2721 device->vertex_buffer_pos = vb_pos + vtx_size;
2723 hr = d3d8_device_prepare_index_buffer(device, idx_size);
2724 if (FAILED(hr))
2725 goto done;
2727 ib_pos = device->index_buffer_pos;
2728 align = ib_pos % idx_fmt_size;
2729 if (align) align = idx_fmt_size - align;
2730 if (ib_pos + idx_size + align > device->index_buffer_size)
2731 ib_pos = 0;
2732 else
2733 ib_pos += align;
2735 wined3d_box.left = ib_pos;
2736 wined3d_box.right = ib_pos + idx_size;
2737 ib = wined3d_buffer_get_resource(device->index_buffer);
2738 if (FAILED(hr = wined3d_resource_map(ib, 0, &wined3d_map_desc, &wined3d_box,
2739 WINED3D_MAP_WRITE | (ib_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD))))
2740 goto done;
2741 memcpy(wined3d_map_desc.data, index_data, idx_size);
2742 wined3d_resource_unmap(ib, 0);
2743 device->index_buffer_pos = ib_pos + idx_size;
2745 hr = wined3d_stateblock_set_stream_source(device->state, 0, device->vertex_buffer, 0, vertex_stride);
2746 if (FAILED(hr))
2747 goto done;
2749 wined3d_stateblock_set_index_buffer(device->state, device->index_buffer,
2750 wined3dformat_from_d3dformat(index_format));
2751 wined3d_stateblock_set_base_vertex_index(device->state, vb_pos / vertex_stride - min_vertex_idx);
2753 wined3d_device_set_primitive_type(device->wined3d_device, wined3d_primitive_type_from_d3d(primitive_type), 0);
2754 wined3d_device_apply_stateblock(device->wined3d_device, device->state);
2755 hr = wined3d_device_draw_indexed_primitive(device->wined3d_device, ib_pos / idx_fmt_size, idx_count);
2757 wined3d_stateblock_set_stream_source(device->state, 0, NULL, 0, 0);
2758 wined3d_stateblock_set_index_buffer(device->state, NULL, WINED3DFMT_UNKNOWN);
2759 wined3d_stateblock_set_base_vertex_index(device->state, 0);
2761 done:
2762 wined3d_mutex_unlock();
2763 return hr;
2766 static HRESULT WINAPI d3d8_device_ProcessVertices(IDirect3DDevice8 *iface, UINT src_start_idx,
2767 UINT dst_idx, UINT vertex_count, IDirect3DVertexBuffer8 *dst_buffer, DWORD flags)
2769 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2770 struct d3d8_vertexbuffer *dst = unsafe_impl_from_IDirect3DVertexBuffer8(dst_buffer);
2771 const struct wined3d_stateblock_state *state;
2772 const struct wined3d_stream_state *stream;
2773 struct d3d8_vertexbuffer *d3d8_buffer;
2774 unsigned int i, map;
2775 HRESULT hr;
2777 TRACE("iface %p, src_start_idx %u, dst_idx %u, vertex_count %u, dst_buffer %p, flags %#x.\n",
2778 iface, src_start_idx, dst_idx, vertex_count, dst_buffer, flags);
2780 wined3d_mutex_lock();
2782 state = device->stateblock_state;
2784 /* Note that an alternative approach would be to simply create these
2785 * buffers with WINED3D_RESOURCE_ACCESS_MAP_R and update them here like we
2786 * do for draws. In some regards that would be easier, but it seems less
2787 * than optimal to upload data to the GPU only to subsequently download it
2788 * again. */
2789 map = device->sysmem_vb;
2790 while (map)
2792 i = wined3d_bit_scan(&map);
2793 stream = &state->streams[i];
2795 d3d8_buffer = wined3d_buffer_get_parent(stream->buffer);
2796 if (FAILED(wined3d_stateblock_set_stream_source(device->state,
2797 i, d3d8_buffer->wined3d_buffer, stream->offset, stream->stride)))
2798 ERR("Failed to set stream source.\n");
2801 wined3d_device_apply_stateblock(device->wined3d_device, device->state);
2802 hr = wined3d_device_process_vertices(device->wined3d_device, src_start_idx, dst_idx,
2803 vertex_count, dst->wined3d_buffer, NULL, flags, dst->fvf);
2805 map = device->sysmem_vb;
2806 while (map)
2808 i = wined3d_bit_scan(&map);
2809 stream = &state->streams[i];
2811 d3d8_buffer = wined3d_buffer_get_parent(stream->buffer);
2812 if (FAILED(wined3d_stateblock_set_stream_source(device->state,
2813 i, d3d8_buffer->draw_buffer, stream->offset, stream->stride)))
2814 ERR("Failed to set stream source.\n");
2817 wined3d_mutex_unlock();
2819 return hr;
2822 static HRESULT WINAPI d3d8_device_CreateVertexShader(IDirect3DDevice8 *iface,
2823 const DWORD *declaration, const DWORD *byte_code, DWORD *shader, DWORD usage)
2825 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2826 struct d3d8_vertex_shader *object;
2827 DWORD shader_handle;
2828 DWORD handle;
2829 HRESULT hr;
2831 TRACE("iface %p, declaration %p, byte_code %p, shader %p, usage %#x.\n",
2832 iface, declaration, byte_code, shader, usage);
2834 if (!(object = heap_alloc_zero(sizeof(*object))))
2836 *shader = 0;
2837 return E_OUTOFMEMORY;
2840 wined3d_mutex_lock();
2841 handle = d3d8_allocate_handle(&device->handle_table, object, D3D8_HANDLE_VS);
2842 wined3d_mutex_unlock();
2843 if (handle == D3D8_INVALID_HANDLE)
2845 ERR("Failed to allocate vertex shader handle.\n");
2846 heap_free(object);
2847 *shader = 0;
2848 return E_OUTOFMEMORY;
2851 shader_handle = handle + VS_HIGHESTFIXEDFXF + 1;
2853 hr = d3d8_vertex_shader_init(object, device, declaration, byte_code, shader_handle, usage);
2854 if (FAILED(hr))
2856 WARN("Failed to initialize vertex shader, hr %#x.\n", hr);
2857 wined3d_mutex_lock();
2858 d3d8_free_handle(&device->handle_table, handle, D3D8_HANDLE_VS);
2859 wined3d_mutex_unlock();
2860 heap_free(object);
2861 *shader = 0;
2862 return hr;
2865 TRACE("Created vertex shader %p (handle %#x).\n", object, shader_handle);
2866 *shader = shader_handle;
2868 return D3D_OK;
2871 static struct d3d8_vertex_declaration *d3d8_device_get_fvf_declaration(struct d3d8_device *device, DWORD fvf)
2873 struct d3d8_vertex_declaration *d3d8_declaration;
2874 struct FvfToDecl *convertedDecls = device->decls;
2875 int p, low, high; /* deliberately signed */
2876 HRESULT hr;
2878 TRACE("Searching for declaration for fvf %08x... ", fvf);
2880 low = 0;
2881 high = device->numConvertedDecls - 1;
2882 while (low <= high)
2884 p = (low + high) >> 1;
2885 TRACE("%d ", p);
2887 if (convertedDecls[p].fvf == fvf)
2889 TRACE("found %p\n", convertedDecls[p].declaration);
2890 return convertedDecls[p].declaration;
2893 if (convertedDecls[p].fvf < fvf)
2894 low = p + 1;
2895 else
2896 high = p - 1;
2898 TRACE("not found. Creating and inserting at position %d.\n", low);
2900 if (!(d3d8_declaration = heap_alloc(sizeof(*d3d8_declaration))))
2901 return NULL;
2903 if (FAILED(hr = d3d8_vertex_declaration_init_fvf(d3d8_declaration, device, fvf)))
2905 WARN("Failed to initialize vertex declaration, hr %#x.\n", hr);
2906 heap_free(d3d8_declaration);
2907 return NULL;
2910 if (device->declArraySize == device->numConvertedDecls)
2912 UINT grow = device->declArraySize / 2;
2914 if (!(convertedDecls = heap_realloc(convertedDecls,
2915 sizeof(*convertedDecls) * (device->numConvertedDecls + grow))))
2917 d3d8_vertex_declaration_destroy(d3d8_declaration);
2918 return NULL;
2920 device->decls = convertedDecls;
2921 device->declArraySize += grow;
2924 memmove(convertedDecls + low + 1, convertedDecls + low,
2925 sizeof(*convertedDecls) * (device->numConvertedDecls - low));
2926 convertedDecls[low].declaration = d3d8_declaration;
2927 convertedDecls[low].fvf = fvf;
2928 ++device->numConvertedDecls;
2930 TRACE("Returning %p. %u decls in array.\n", d3d8_declaration, device->numConvertedDecls);
2932 return d3d8_declaration;
2935 static HRESULT WINAPI d3d8_device_SetVertexShader(IDirect3DDevice8 *iface, DWORD shader)
2937 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2938 struct d3d8_vertex_shader *shader_impl;
2940 TRACE("iface %p, shader %#x.\n", iface, shader);
2942 if (VS_HIGHESTFIXEDFXF >= shader)
2944 TRACE("Setting FVF, %#x\n", shader);
2946 wined3d_mutex_lock();
2947 wined3d_stateblock_set_vertex_declaration(device->update_state,
2948 d3d8_device_get_fvf_declaration(device, shader)->wined3d_vertex_declaration);
2949 wined3d_stateblock_set_vertex_shader(device->update_state, NULL);
2950 wined3d_mutex_unlock();
2952 return D3D_OK;
2955 TRACE("Setting shader\n");
2957 wined3d_mutex_lock();
2958 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS)))
2960 WARN("Invalid handle (%#x) passed.\n", shader);
2961 wined3d_mutex_unlock();
2963 return D3DERR_INVALIDCALL;
2966 wined3d_stateblock_set_vertex_declaration(device->update_state,
2967 shader_impl->vertex_declaration->wined3d_vertex_declaration);
2968 wined3d_stateblock_set_vertex_shader(device->update_state, shader_impl->wined3d_shader);
2969 wined3d_mutex_unlock();
2971 return D3D_OK;
2974 static HRESULT WINAPI d3d8_device_GetVertexShader(IDirect3DDevice8 *iface, DWORD *shader)
2976 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2977 struct wined3d_vertex_declaration *wined3d_declaration;
2978 struct d3d8_vertex_declaration *d3d8_declaration;
2980 TRACE("iface %p, shader %p.\n", iface, shader);
2982 wined3d_mutex_lock();
2983 if ((wined3d_declaration = device->stateblock_state->vertex_declaration))
2985 d3d8_declaration = wined3d_vertex_declaration_get_parent(wined3d_declaration);
2986 *shader = d3d8_declaration->shader_handle;
2988 else
2990 *shader = 0;
2992 wined3d_mutex_unlock();
2994 TRACE("Returning %#x.\n", *shader);
2996 return D3D_OK;
2999 static HRESULT WINAPI d3d8_device_DeleteVertexShader(IDirect3DDevice8 *iface, DWORD shader)
3001 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3002 struct d3d8_vertex_shader *shader_impl;
3004 TRACE("iface %p, shader %#x.\n", iface, shader);
3006 wined3d_mutex_lock();
3007 if (!(shader_impl = d3d8_free_handle(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS)))
3009 WARN("Invalid handle (%#x) passed.\n", shader);
3010 wined3d_mutex_unlock();
3012 return D3DERR_INVALIDCALL;
3015 if (shader_impl->wined3d_shader
3016 && device->stateblock_state->vs == shader_impl->wined3d_shader)
3017 IDirect3DDevice8_SetVertexShader(iface, 0);
3019 wined3d_mutex_unlock();
3021 d3d8_vertex_shader_destroy(shader_impl);
3023 return D3D_OK;
3026 static HRESULT WINAPI d3d8_device_SetVertexShaderConstant(IDirect3DDevice8 *iface,
3027 DWORD start_register, const void *data, DWORD count)
3029 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3030 HRESULT hr;
3032 TRACE("iface %p, start_register %u, data %p, count %u.\n",
3033 iface, start_register, data, count);
3035 if (start_register + count > D3D8_MAX_VERTEX_SHADER_CONSTANTF)
3037 WARN("Trying to access %u constants, but d3d8 only supports %u\n",
3038 start_register + count, D3D8_MAX_VERTEX_SHADER_CONSTANTF);
3039 return D3DERR_INVALIDCALL;
3042 wined3d_mutex_lock();
3043 hr = wined3d_stateblock_set_vs_consts_f(device->update_state, start_register, count, data);
3044 wined3d_mutex_unlock();
3046 return hr;
3049 static HRESULT WINAPI d3d8_device_GetVertexShaderConstant(IDirect3DDevice8 *iface,
3050 DWORD start_idx, void *constants, DWORD count)
3052 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3053 const struct wined3d_vec4 *src;
3055 TRACE("iface %p, start_idx %u, constants %p, count %u.\n", iface, start_idx, constants, count);
3057 if (!constants)
3058 return D3DERR_INVALIDCALL;
3060 if (start_idx >= device->vs_uniform_count || count > device->vs_uniform_count - start_idx)
3062 WARN("Trying to access %u constants, but d3d8 only supports %u.\n",
3063 start_idx + count, device->vs_uniform_count);
3064 return D3DERR_INVALIDCALL;
3067 wined3d_mutex_lock();
3068 src = device->stateblock_state->vs_consts_f;
3069 memcpy(constants, &src[start_idx], count * sizeof(*src));
3070 wined3d_mutex_unlock();
3072 return D3D_OK;
3075 static HRESULT WINAPI d3d8_device_GetVertexShaderDeclaration(IDirect3DDevice8 *iface,
3076 DWORD shader, void *data, DWORD *data_size)
3078 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3079 struct d3d8_vertex_declaration *declaration;
3080 struct d3d8_vertex_shader *shader_impl;
3082 TRACE("iface %p, shader %#x, data %p, data_size %p.\n",
3083 iface, shader, data, data_size);
3085 wined3d_mutex_lock();
3086 shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS);
3087 wined3d_mutex_unlock();
3089 if (!shader_impl)
3091 WARN("Invalid handle (%#x) passed.\n", shader);
3092 return D3DERR_INVALIDCALL;
3094 declaration = shader_impl->vertex_declaration;
3096 if (!data)
3098 *data_size = declaration->elements_size;
3099 return D3D_OK;
3102 /* MSDN claims that if *data_size is smaller than the required size
3103 * we should write the required size and return D3DERR_MOREDATA.
3104 * That's not actually true. */
3105 if (*data_size < declaration->elements_size)
3106 return D3DERR_INVALIDCALL;
3108 memcpy(data, declaration->elements, declaration->elements_size);
3110 return D3D_OK;
3113 static HRESULT WINAPI d3d8_device_GetVertexShaderFunction(IDirect3DDevice8 *iface,
3114 DWORD shader, void *data, DWORD *data_size)
3116 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3117 struct d3d8_vertex_shader *shader_impl = NULL;
3118 HRESULT hr;
3120 TRACE("iface %p, shader %#x, data %p, data_size %p.\n",
3121 iface, shader, data, data_size);
3123 wined3d_mutex_lock();
3124 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS)))
3126 WARN("Invalid handle (%#x) passed.\n", shader);
3127 wined3d_mutex_unlock();
3129 return D3DERR_INVALIDCALL;
3132 if (!shader_impl->wined3d_shader)
3134 wined3d_mutex_unlock();
3135 *data_size = 0;
3136 return D3D_OK;
3139 hr = wined3d_shader_get_byte_code(shader_impl->wined3d_shader, data, data_size);
3140 wined3d_mutex_unlock();
3142 return hr;
3145 static HRESULT WINAPI d3d8_device_SetIndices(IDirect3DDevice8 *iface,
3146 IDirect3DIndexBuffer8 *buffer, UINT base_vertex_idx)
3148 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3149 struct d3d8_indexbuffer *ib = unsafe_impl_from_IDirect3DIndexBuffer8(buffer);
3150 struct wined3d_buffer *wined3d_buffer;
3152 TRACE("iface %p, buffer %p, base_vertex_idx %u.\n", iface, buffer, base_vertex_idx);
3154 if (!ib)
3155 wined3d_buffer = NULL;
3156 else if (ib->draw_buffer)
3157 wined3d_buffer = ib->draw_buffer;
3158 else
3159 wined3d_buffer = ib->wined3d_buffer;
3161 /* WineD3D takes an INT(due to d3d9), but d3d8 uses UINTs. Do I have to add a check here that
3162 * the UINT doesn't cause an overflow in the INT? It seems rather unlikely because such large
3163 * vertex buffers can't be created to address them with an index that requires the 32nd bit
3164 * (4 Byte minimum vertex size * 2^31-1 -> 8 gb buffer. The index sign would be the least
3165 * problem)
3167 wined3d_mutex_lock();
3168 wined3d_stateblock_set_base_vertex_index(device->update_state, base_vertex_idx);
3169 wined3d_stateblock_set_index_buffer(device->update_state, wined3d_buffer, ib ? ib->format : WINED3DFMT_UNKNOWN);
3170 if (!device->recording)
3171 device->sysmem_ib = ib && ib->draw_buffer;
3172 wined3d_mutex_unlock();
3174 return D3D_OK;
3177 static HRESULT WINAPI d3d8_device_GetIndices(IDirect3DDevice8 *iface,
3178 IDirect3DIndexBuffer8 **buffer, UINT *base_vertex_index)
3180 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3181 const struct wined3d_stateblock_state *state;
3182 struct wined3d_buffer *wined3d_buffer;
3183 struct d3d8_indexbuffer *buffer_impl;
3185 TRACE("iface %p, buffer %p, base_vertex_index %p.\n", iface, buffer, base_vertex_index);
3187 if (!buffer)
3188 return D3DERR_INVALIDCALL;
3190 /* The case from UINT to INT is safe because d3d8 will never set negative values */
3191 wined3d_mutex_lock();
3192 state = device->stateblock_state;
3193 *base_vertex_index = state->base_vertex_index;
3194 if ((wined3d_buffer = state->index_buffer))
3196 buffer_impl = wined3d_buffer_get_parent(wined3d_buffer);
3197 *buffer = &buffer_impl->IDirect3DIndexBuffer8_iface;
3198 IDirect3DIndexBuffer8_AddRef(*buffer);
3200 else
3202 *buffer = NULL;
3204 wined3d_mutex_unlock();
3206 return D3D_OK;
3209 static HRESULT WINAPI d3d8_device_CreatePixelShader(IDirect3DDevice8 *iface,
3210 const DWORD *byte_code, DWORD *shader)
3212 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3213 struct d3d8_pixel_shader *object;
3214 DWORD shader_handle;
3215 DWORD handle;
3216 HRESULT hr;
3218 TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
3220 if (!shader)
3221 return D3DERR_INVALIDCALL;
3223 if (!(object = heap_alloc_zero(sizeof(*object))))
3224 return E_OUTOFMEMORY;
3226 wined3d_mutex_lock();
3227 handle = d3d8_allocate_handle(&device->handle_table, object, D3D8_HANDLE_PS);
3228 wined3d_mutex_unlock();
3229 if (handle == D3D8_INVALID_HANDLE)
3231 ERR("Failed to allocate pixel shader handle.\n");
3232 heap_free(object);
3233 return E_OUTOFMEMORY;
3236 shader_handle = handle + VS_HIGHESTFIXEDFXF + 1;
3238 hr = d3d8_pixel_shader_init(object, device, byte_code, shader_handle);
3239 if (FAILED(hr))
3241 WARN("Failed to initialize pixel shader, hr %#x.\n", hr);
3242 wined3d_mutex_lock();
3243 d3d8_free_handle(&device->handle_table, handle, D3D8_HANDLE_PS);
3244 wined3d_mutex_unlock();
3245 heap_free(object);
3246 *shader = 0;
3247 return hr;
3250 TRACE("Created pixel shader %p (handle %#x).\n", object, shader_handle);
3251 *shader = shader_handle;
3253 return D3D_OK;
3256 static HRESULT WINAPI d3d8_device_SetPixelShader(IDirect3DDevice8 *iface, DWORD shader)
3258 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3259 struct d3d8_pixel_shader *shader_impl;
3261 TRACE("iface %p, shader %#x.\n", iface, shader);
3263 wined3d_mutex_lock();
3265 if (!shader)
3267 wined3d_stateblock_set_pixel_shader(device->update_state, NULL);
3268 wined3d_mutex_unlock();
3269 return D3D_OK;
3272 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS)))
3274 WARN("Invalid handle (%#x) passed.\n", shader);
3275 wined3d_mutex_unlock();
3276 return D3DERR_INVALIDCALL;
3279 TRACE("Setting shader %p.\n", shader_impl);
3280 wined3d_stateblock_set_pixel_shader(device->update_state, shader_impl->wined3d_shader);
3281 wined3d_mutex_unlock();
3283 return D3D_OK;
3286 static HRESULT WINAPI d3d8_device_GetPixelShader(IDirect3DDevice8 *iface, DWORD *shader)
3288 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3289 struct wined3d_shader *object;
3291 TRACE("iface %p, shader %p.\n", iface, shader);
3293 if (!shader)
3294 return D3DERR_INVALIDCALL;
3296 wined3d_mutex_lock();
3297 if ((object = device->stateblock_state->ps))
3299 struct d3d8_pixel_shader *d3d8_shader;
3300 d3d8_shader = wined3d_shader_get_parent(object);
3301 *shader = d3d8_shader->handle;
3303 else
3305 *shader = 0;
3307 wined3d_mutex_unlock();
3309 TRACE("Returning %#x.\n", *shader);
3311 return D3D_OK;
3314 static HRESULT WINAPI d3d8_device_DeletePixelShader(IDirect3DDevice8 *iface, DWORD shader)
3316 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3317 struct d3d8_pixel_shader *shader_impl;
3319 TRACE("iface %p, shader %#x.\n", iface, shader);
3321 wined3d_mutex_lock();
3323 if (!(shader_impl = d3d8_free_handle(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS)))
3325 WARN("Invalid handle (%#x) passed.\n", shader);
3326 wined3d_mutex_unlock();
3327 return D3DERR_INVALIDCALL;
3330 if (device->stateblock_state->ps == shader_impl->wined3d_shader)
3331 IDirect3DDevice8_SetPixelShader(iface, 0);
3333 wined3d_mutex_unlock();
3335 d3d8_pixel_shader_destroy(shader_impl);
3337 return D3D_OK;
3340 static HRESULT WINAPI d3d8_device_SetPixelShaderConstant(IDirect3DDevice8 *iface,
3341 DWORD start_register, const void *data, DWORD count)
3343 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3344 HRESULT hr;
3346 TRACE("iface %p, start_register %u, data %p, count %u.\n",
3347 iface, start_register, data, count);
3349 wined3d_mutex_lock();
3350 hr = wined3d_stateblock_set_ps_consts_f(device->update_state, start_register, count, data);
3351 wined3d_mutex_unlock();
3353 return hr;
3356 static HRESULT WINAPI d3d8_device_GetPixelShaderConstant(IDirect3DDevice8 *iface,
3357 DWORD start_idx, void *constants, DWORD count)
3359 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3360 const struct wined3d_vec4 *src;
3362 TRACE("iface %p, start_idx %u, constants %p, count %u.\n", iface, start_idx, constants, count);
3364 if (!constants || start_idx >= D3D8_MAX_PIXEL_SHADER_CONSTANTF
3365 || count > D3D8_MAX_PIXEL_SHADER_CONSTANTF - start_idx)
3366 return WINED3DERR_INVALIDCALL;
3368 wined3d_mutex_lock();
3369 src = device->stateblock_state->ps_consts_f;
3370 memcpy(constants, &src[start_idx], count * sizeof(*src));
3371 wined3d_mutex_unlock();
3373 return D3D_OK;
3376 static HRESULT WINAPI d3d8_device_GetPixelShaderFunction(IDirect3DDevice8 *iface,
3377 DWORD shader, void *data, DWORD *data_size)
3379 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3380 struct d3d8_pixel_shader *shader_impl = NULL;
3381 HRESULT hr;
3383 TRACE("iface %p, shader %#x, data %p, data_size %p.\n",
3384 iface, shader, data, data_size);
3386 wined3d_mutex_lock();
3387 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS)))
3389 WARN("Invalid handle (%#x) passed.\n", shader);
3390 wined3d_mutex_unlock();
3392 return D3DERR_INVALIDCALL;
3395 hr = wined3d_shader_get_byte_code(shader_impl->wined3d_shader, data, data_size);
3396 wined3d_mutex_unlock();
3398 return hr;
3401 static HRESULT WINAPI d3d8_device_DrawRectPatch(IDirect3DDevice8 *iface, UINT handle,
3402 const float *segment_count, const D3DRECTPATCH_INFO *patch_info)
3404 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
3405 iface, handle, segment_count, patch_info);
3406 return D3D_OK;
3409 static HRESULT WINAPI d3d8_device_DrawTriPatch(IDirect3DDevice8 *iface, UINT handle,
3410 const float *segment_count, const D3DTRIPATCH_INFO *patch_info)
3412 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
3413 iface, handle, segment_count, patch_info);
3414 return D3D_OK;
3417 static HRESULT WINAPI d3d8_device_DeletePatch(IDirect3DDevice8 *iface, UINT handle)
3419 FIXME("iface %p, handle %#x unimplemented.\n", iface, handle);
3420 return D3DERR_INVALIDCALL;
3423 static HRESULT WINAPI d3d8_device_SetStreamSource(IDirect3DDevice8 *iface,
3424 UINT stream_idx, IDirect3DVertexBuffer8 *buffer, UINT stride)
3426 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3427 struct d3d8_vertexbuffer *buffer_impl = unsafe_impl_from_IDirect3DVertexBuffer8(buffer);
3428 const struct wined3d_stateblock_state *state;
3429 struct wined3d_buffer *wined3d_buffer;
3430 HRESULT hr;
3432 TRACE("iface %p, stream_idx %u, buffer %p, stride %u.\n",
3433 iface, stream_idx, buffer, stride);
3435 if (stream_idx >= ARRAY_SIZE(state->streams))
3437 WARN("Stream index %u out of range.\n", stream_idx);
3438 return D3DERR_INVALIDCALL;
3441 wined3d_mutex_lock();
3443 if (!buffer_impl)
3445 wined3d_buffer = NULL;
3446 state = device->stateblock_state;
3447 stride = state->streams[stream_idx].stride;
3449 else if (buffer_impl->draw_buffer)
3450 wined3d_buffer = buffer_impl->draw_buffer;
3451 else
3452 wined3d_buffer = buffer_impl->wined3d_buffer;
3454 hr = wined3d_stateblock_set_stream_source(device->update_state, stream_idx, wined3d_buffer, 0, stride);
3455 if (SUCCEEDED(hr) && !device->recording)
3457 if (buffer_impl && buffer_impl->draw_buffer)
3458 device->sysmem_vb |= (1u << stream_idx);
3459 else
3460 device->sysmem_vb &= ~(1u << stream_idx);
3463 wined3d_mutex_unlock();
3465 return hr;
3468 static HRESULT WINAPI d3d8_device_GetStreamSource(IDirect3DDevice8 *iface,
3469 UINT stream_idx, IDirect3DVertexBuffer8 **buffer, UINT *stride)
3471 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3472 const struct wined3d_stateblock_state *state;
3473 const struct wined3d_stream_state *stream;
3474 struct d3d8_vertexbuffer *buffer_impl;
3476 TRACE("iface %p, stream_idx %u, buffer %p, stride %p.\n",
3477 iface, stream_idx, buffer, stride);
3479 if (!buffer)
3480 return D3DERR_INVALIDCALL;
3482 if (stream_idx >= ARRAY_SIZE(state->streams))
3484 WARN("Stream index %u out of range.\n", stream_idx);
3485 return D3DERR_INVALIDCALL;
3488 wined3d_mutex_lock();
3489 state = device->stateblock_state;
3490 stream = &state->streams[stream_idx];
3491 if (stream->buffer)
3493 buffer_impl = wined3d_buffer_get_parent(stream->buffer);
3494 *buffer = &buffer_impl->IDirect3DVertexBuffer8_iface;
3495 IDirect3DVertexBuffer8_AddRef(*buffer);
3497 else
3498 *buffer = NULL;
3499 *stride = stream->stride;
3500 wined3d_mutex_unlock();
3502 return D3D_OK;
3505 static const struct IDirect3DDevice8Vtbl d3d8_device_vtbl =
3507 d3d8_device_QueryInterface,
3508 d3d8_device_AddRef,
3509 d3d8_device_Release,
3510 d3d8_device_TestCooperativeLevel,
3511 d3d8_device_GetAvailableTextureMem,
3512 d3d8_device_ResourceManagerDiscardBytes,
3513 d3d8_device_GetDirect3D,
3514 d3d8_device_GetDeviceCaps,
3515 d3d8_device_GetDisplayMode,
3516 d3d8_device_GetCreationParameters,
3517 d3d8_device_SetCursorProperties,
3518 d3d8_device_SetCursorPosition,
3519 d3d8_device_ShowCursor,
3520 d3d8_device_CreateAdditionalSwapChain,
3521 d3d8_device_Reset,
3522 d3d8_device_Present,
3523 d3d8_device_GetBackBuffer,
3524 d3d8_device_GetRasterStatus,
3525 d3d8_device_SetGammaRamp,
3526 d3d8_device_GetGammaRamp,
3527 d3d8_device_CreateTexture,
3528 d3d8_device_CreateVolumeTexture,
3529 d3d8_device_CreateCubeTexture,
3530 d3d8_device_CreateVertexBuffer,
3531 d3d8_device_CreateIndexBuffer,
3532 d3d8_device_CreateRenderTarget,
3533 d3d8_device_CreateDepthStencilSurface,
3534 d3d8_device_CreateImageSurface,
3535 d3d8_device_CopyRects,
3536 d3d8_device_UpdateTexture,
3537 d3d8_device_GetFrontBuffer,
3538 d3d8_device_SetRenderTarget,
3539 d3d8_device_GetRenderTarget,
3540 d3d8_device_GetDepthStencilSurface,
3541 d3d8_device_BeginScene,
3542 d3d8_device_EndScene,
3543 d3d8_device_Clear,
3544 d3d8_device_SetTransform,
3545 d3d8_device_GetTransform,
3546 d3d8_device_MultiplyTransform,
3547 d3d8_device_SetViewport,
3548 d3d8_device_GetViewport,
3549 d3d8_device_SetMaterial,
3550 d3d8_device_GetMaterial,
3551 d3d8_device_SetLight,
3552 d3d8_device_GetLight,
3553 d3d8_device_LightEnable,
3554 d3d8_device_GetLightEnable,
3555 d3d8_device_SetClipPlane,
3556 d3d8_device_GetClipPlane,
3557 d3d8_device_SetRenderState,
3558 d3d8_device_GetRenderState,
3559 d3d8_device_BeginStateBlock,
3560 d3d8_device_EndStateBlock,
3561 d3d8_device_ApplyStateBlock,
3562 d3d8_device_CaptureStateBlock,
3563 d3d8_device_DeleteStateBlock,
3564 d3d8_device_CreateStateBlock,
3565 d3d8_device_SetClipStatus,
3566 d3d8_device_GetClipStatus,
3567 d3d8_device_GetTexture,
3568 d3d8_device_SetTexture,
3569 d3d8_device_GetTextureStageState,
3570 d3d8_device_SetTextureStageState,
3571 d3d8_device_ValidateDevice,
3572 d3d8_device_GetInfo,
3573 d3d8_device_SetPaletteEntries,
3574 d3d8_device_GetPaletteEntries,
3575 d3d8_device_SetCurrentTexturePalette,
3576 d3d8_device_GetCurrentTexturePalette,
3577 d3d8_device_DrawPrimitive,
3578 d3d8_device_DrawIndexedPrimitive,
3579 d3d8_device_DrawPrimitiveUP,
3580 d3d8_device_DrawIndexedPrimitiveUP,
3581 d3d8_device_ProcessVertices,
3582 d3d8_device_CreateVertexShader,
3583 d3d8_device_SetVertexShader,
3584 d3d8_device_GetVertexShader,
3585 d3d8_device_DeleteVertexShader,
3586 d3d8_device_SetVertexShaderConstant,
3587 d3d8_device_GetVertexShaderConstant,
3588 d3d8_device_GetVertexShaderDeclaration,
3589 d3d8_device_GetVertexShaderFunction,
3590 d3d8_device_SetStreamSource,
3591 d3d8_device_GetStreamSource,
3592 d3d8_device_SetIndices,
3593 d3d8_device_GetIndices,
3594 d3d8_device_CreatePixelShader,
3595 d3d8_device_SetPixelShader,
3596 d3d8_device_GetPixelShader,
3597 d3d8_device_DeletePixelShader,
3598 d3d8_device_SetPixelShaderConstant,
3599 d3d8_device_GetPixelShaderConstant,
3600 d3d8_device_GetPixelShaderFunction,
3601 d3d8_device_DrawRectPatch,
3602 d3d8_device_DrawTriPatch,
3603 d3d8_device_DeletePatch,
3606 static inline struct d3d8_device *device_from_device_parent(struct wined3d_device_parent *device_parent)
3608 return CONTAINING_RECORD(device_parent, struct d3d8_device, device_parent);
3611 static void CDECL device_parent_wined3d_device_created(struct wined3d_device_parent *device_parent,
3612 struct wined3d_device *device)
3614 TRACE("device_parent %p, device %p\n", device_parent, device);
3617 static void CDECL device_parent_mode_changed(struct wined3d_device_parent *device_parent)
3619 TRACE("device_parent %p.\n", device_parent);
3622 static void CDECL device_parent_activate(struct wined3d_device_parent *device_parent, BOOL activate)
3624 struct d3d8_device *device = device_from_device_parent(device_parent);
3626 TRACE("device_parent %p, activate %#x.\n", device_parent, activate);
3628 if (!activate)
3629 InterlockedCompareExchange(&device->device_state, D3D8_DEVICE_STATE_LOST, D3D8_DEVICE_STATE_OK);
3630 else
3631 InterlockedCompareExchange(&device->device_state, D3D8_DEVICE_STATE_NOT_RESET, D3D8_DEVICE_STATE_LOST);
3634 static HRESULT CDECL device_parent_texture_sub_resource_created(struct wined3d_device_parent *device_parent,
3635 enum wined3d_resource_type type, struct wined3d_texture *wined3d_texture, unsigned int sub_resource_idx,
3636 void **parent, const struct wined3d_parent_ops **parent_ops)
3638 TRACE("device_parent %p, type %#x, wined3d_texture %p, sub_resource_idx %u, parent %p, parent_ops %p.\n",
3639 device_parent, type, wined3d_texture, sub_resource_idx, parent, parent_ops);
3641 if (type == WINED3D_RTYPE_TEXTURE_2D)
3643 struct d3d8_surface *d3d_surface;
3645 if (!(d3d_surface = heap_alloc_zero(sizeof(*d3d_surface))))
3646 return E_OUTOFMEMORY;
3648 surface_init(d3d_surface, wined3d_texture, sub_resource_idx, parent_ops);
3649 *parent = d3d_surface;
3650 TRACE("Created surface %p.\n", d3d_surface);
3652 else if (type == WINED3D_RTYPE_TEXTURE_3D)
3654 struct d3d8_volume *d3d_volume;
3656 if (!(d3d_volume = heap_alloc_zero(sizeof(*d3d_volume))))
3657 return E_OUTOFMEMORY;
3659 volume_init(d3d_volume, wined3d_texture, sub_resource_idx, parent_ops);
3660 *parent = d3d_volume;
3661 TRACE("Created volume %p.\n", d3d_volume);
3663 else
3665 ERR("Unhandled resource type %#x.\n", type);
3666 return E_FAIL;
3669 return D3D_OK;
3672 static HRESULT CDECL device_parent_create_swapchain_texture(struct wined3d_device_parent *device_parent,
3673 void *container_parent, const struct wined3d_resource_desc *desc, DWORD texture_flags,
3674 struct wined3d_texture **texture)
3676 struct d3d8_device *device = device_from_device_parent(device_parent);
3677 struct d3d8_surface *d3d_surface;
3678 HRESULT hr;
3680 TRACE("device_parent %p, container_parent %p, desc %p, texture flags %#x, texture %p.\n",
3681 device_parent, container_parent, desc, texture_flags, texture);
3683 if (FAILED(hr = wined3d_texture_create(device->wined3d_device, desc, 1, 1, texture_flags,
3684 NULL, &device->IDirect3DDevice8_iface, &d3d8_null_wined3d_parent_ops, texture)))
3686 WARN("Failed to create texture, hr %#x.\n", hr);
3687 return hr;
3690 d3d_surface = wined3d_texture_get_sub_resource_parent(*texture, 0);
3691 d3d_surface->parent_device = &device->IDirect3DDevice8_iface;
3693 return hr;
3696 static const struct wined3d_device_parent_ops d3d8_wined3d_device_parent_ops =
3698 device_parent_wined3d_device_created,
3699 device_parent_mode_changed,
3700 device_parent_activate,
3701 device_parent_texture_sub_resource_created,
3702 device_parent_create_swapchain_texture,
3705 static void setup_fpu(void)
3707 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
3708 WORD cw;
3709 __asm__ volatile ("fnstcw %0" : "=m" (cw));
3710 cw = (cw & ~0xf3f) | 0x3f;
3711 __asm__ volatile ("fldcw %0" : : "m" (cw));
3712 #elif defined(__i386__) && defined(_MSC_VER)
3713 WORD cw;
3714 __asm fnstcw cw;
3715 cw = (cw & ~0xf3f) | 0x3f;
3716 __asm fldcw cw;
3717 #else
3718 FIXME("FPU setup not implemented for this platform.\n");
3719 #endif
3722 HRESULT device_init(struct d3d8_device *device, struct d3d8 *parent, struct wined3d *wined3d, UINT adapter,
3723 D3DDEVTYPE device_type, HWND focus_window, DWORD flags, D3DPRESENT_PARAMETERS *parameters)
3725 struct wined3d_swapchain_desc swapchain_desc;
3726 struct wined3d_swapchain *wined3d_swapchain;
3727 struct wined3d_adapter *wined3d_adapter;
3728 struct wined3d_output *wined3d_output;
3729 struct d3d8_swapchain *d3d_swapchain;
3730 struct wined3d_caps caps;
3731 unsigned int output_idx;
3732 HRESULT hr;
3734 static const enum wined3d_feature_level feature_levels[] =
3736 WINED3D_FEATURE_LEVEL_8,
3737 WINED3D_FEATURE_LEVEL_7,
3738 WINED3D_FEATURE_LEVEL_6,
3739 WINED3D_FEATURE_LEVEL_5,
3742 output_idx = adapter;
3743 if (output_idx >= parent->wined3d_output_count)
3744 return D3DERR_INVALIDCALL;
3746 device->IDirect3DDevice8_iface.lpVtbl = &d3d8_device_vtbl;
3747 device->device_parent.ops = &d3d8_wined3d_device_parent_ops;
3748 device->adapter_ordinal = adapter;
3749 device->ref = 1;
3750 if (!(device->handle_table.entries = heap_alloc_zero(D3D8_INITIAL_HANDLE_TABLE_SIZE
3751 * sizeof(*device->handle_table.entries))))
3753 ERR("Failed to allocate handle table memory.\n");
3754 return E_OUTOFMEMORY;
3756 device->handle_table.table_size = D3D8_INITIAL_HANDLE_TABLE_SIZE;
3758 if (!(flags & D3DCREATE_FPU_PRESERVE)) setup_fpu();
3760 wined3d_mutex_lock();
3761 wined3d_output = parent->wined3d_outputs[output_idx];
3762 wined3d_adapter = wined3d_output_get_adapter(wined3d_output);
3763 if (FAILED(hr = wined3d_device_create(wined3d, wined3d_adapter, wined3d_device_type_from_d3d(device_type),
3764 focus_window, flags, 4, feature_levels, ARRAY_SIZE(feature_levels),
3765 &device->device_parent, &device->wined3d_device)))
3767 WARN("Failed to create wined3d device, hr %#x.\n", hr);
3768 wined3d_mutex_unlock();
3769 heap_free(device->handle_table.entries);
3770 return hr;
3773 wined3d_get_device_caps(wined3d_adapter, wined3d_device_type_from_d3d(device_type), &caps);
3774 device->max_user_clip_planes = caps.MaxUserClipPlanes;
3775 device->vs_uniform_count = caps.MaxVertexShaderConst;
3777 if (FAILED(hr = wined3d_stateblock_create(device->wined3d_device, NULL, WINED3D_SBT_PRIMARY, &device->state)))
3779 ERR("Failed to create primary stateblock, hr %#x.\n", hr);
3780 wined3d_device_decref(device->wined3d_device);
3781 wined3d_mutex_unlock();
3782 return hr;
3784 device->stateblock_state = wined3d_stateblock_get_state(device->state);
3785 device->update_state = device->state;
3787 if (!parameters->Windowed)
3789 if (!focus_window)
3790 focus_window = parameters->hDeviceWindow;
3791 if (FAILED(hr = wined3d_device_acquire_focus_window(device->wined3d_device, focus_window)))
3793 ERR("Failed to acquire focus window, hr %#x.\n", hr);
3794 wined3d_device_decref(device->wined3d_device);
3795 wined3d_mutex_unlock();
3796 heap_free(device->handle_table.entries);
3797 return hr;
3801 if (flags & D3DCREATE_MULTITHREADED)
3802 wined3d_device_set_multithreaded(device->wined3d_device);
3804 if (!wined3d_swapchain_desc_from_d3d8(&swapchain_desc, wined3d_output, parameters))
3806 wined3d_device_release_focus_window(device->wined3d_device);
3807 wined3d_device_decref(device->wined3d_device);
3808 wined3d_mutex_unlock();
3809 heap_free(device->handle_table.entries);
3810 return D3DERR_INVALIDCALL;
3812 swapchain_desc.flags |= WINED3D_SWAPCHAIN_IMPLICIT;
3814 if (FAILED(hr = d3d8_swapchain_create(device, &swapchain_desc,
3815 wined3dswapinterval_from_d3d(parameters->FullScreen_PresentationInterval), &d3d_swapchain)))
3817 WARN("Failed to create implicit swapchain, hr %#x.\n", hr);
3818 wined3d_device_release_focus_window(device->wined3d_device);
3819 wined3d_device_decref(device->wined3d_device);
3820 wined3d_mutex_unlock();
3821 heap_free(device->handle_table.entries);
3822 return hr;
3825 wined3d_swapchain = d3d_swapchain->wined3d_swapchain;
3826 wined3d_swapchain_incref(wined3d_swapchain);
3827 IDirect3DSwapChain8_Release(&d3d_swapchain->IDirect3DSwapChain8_iface);
3829 wined3d_stateblock_set_render_state(device->state, WINED3D_RS_ZENABLE,
3830 !!swapchain_desc.enable_auto_depth_stencil);
3831 wined3d_stateblock_set_render_state(device->state, WINED3D_RS_POINTSIZE_MIN, 0);
3832 device_reset_viewport_state(device);
3833 wined3d_mutex_unlock();
3835 present_parameters_from_wined3d_swapchain_desc(parameters,
3836 &swapchain_desc, parameters->FullScreen_PresentationInterval);
3838 device->declArraySize = 16;
3839 if (!(device->decls = heap_alloc(device->declArraySize * sizeof(*device->decls))))
3841 ERR("Failed to allocate FVF vertex declaration map memory.\n");
3842 hr = E_OUTOFMEMORY;
3843 goto err;
3846 device->implicit_swapchain = wined3d_swapchain;
3848 device->d3d_parent = parent;
3849 IDirect3D8_AddRef(&parent->IDirect3D8_iface);
3851 return D3D_OK;
3853 err:
3854 wined3d_mutex_lock();
3855 wined3d_swapchain_decref(wined3d_swapchain);
3856 wined3d_device_release_focus_window(device->wined3d_device);
3857 wined3d_device_decref(device->wined3d_device);
3858 wined3d_mutex_unlock();
3859 heap_free(device->handle_table.entries);
3860 return hr;