iphlpapi: Remove unnecessary memcpy from build_udp6_table.
[wine.git] / dlls / d3d8 / device.c
blob71717852bb61f84f269a86a3cc5263f694bad7ec
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])) return format;
40 switch(format)
42 case WINED3DFMT_UNKNOWN: return D3DFMT_UNKNOWN;
43 case WINED3DFMT_B8G8R8_UNORM: return D3DFMT_R8G8B8;
44 case WINED3DFMT_B8G8R8A8_UNORM: return D3DFMT_A8R8G8B8;
45 case WINED3DFMT_B8G8R8X8_UNORM: return D3DFMT_X8R8G8B8;
46 case WINED3DFMT_B5G6R5_UNORM: return D3DFMT_R5G6B5;
47 case WINED3DFMT_B5G5R5X1_UNORM: return D3DFMT_X1R5G5B5;
48 case WINED3DFMT_B5G5R5A1_UNORM: return D3DFMT_A1R5G5B5;
49 case WINED3DFMT_B4G4R4A4_UNORM: return D3DFMT_A4R4G4B4;
50 case WINED3DFMT_B2G3R3_UNORM: return D3DFMT_R3G3B2;
51 case WINED3DFMT_A8_UNORM: return D3DFMT_A8;
52 case WINED3DFMT_B2G3R3A8_UNORM: return D3DFMT_A8R3G3B2;
53 case WINED3DFMT_B4G4R4X4_UNORM: return D3DFMT_X4R4G4B4;
54 case WINED3DFMT_R10G10B10A2_UNORM: return D3DFMT_A2B10G10R10;
55 case WINED3DFMT_R16G16_UNORM: return D3DFMT_G16R16;
56 case WINED3DFMT_P8_UINT_A8_UNORM: return D3DFMT_A8P8;
57 case WINED3DFMT_P8_UINT: return D3DFMT_P8;
58 case WINED3DFMT_L8_UNORM: return D3DFMT_L8;
59 case WINED3DFMT_L8A8_UNORM: return D3DFMT_A8L8;
60 case WINED3DFMT_L4A4_UNORM: return D3DFMT_A4L4;
61 case WINED3DFMT_R8G8_SNORM: return D3DFMT_V8U8;
62 case WINED3DFMT_R5G5_SNORM_L6_UNORM: return D3DFMT_L6V5U5;
63 case WINED3DFMT_R8G8_SNORM_L8X8_UNORM: return D3DFMT_X8L8V8U8;
64 case WINED3DFMT_R8G8B8A8_SNORM: return D3DFMT_Q8W8V8U8;
65 case WINED3DFMT_R16G16_SNORM: return D3DFMT_V16U16;
66 case WINED3DFMT_R10G11B11_SNORM: return D3DFMT_W11V11U10;
67 case WINED3DFMT_R10G10B10_SNORM_A2_UNORM: return D3DFMT_A2W10V10U10;
68 case WINED3DFMT_D16_LOCKABLE: return D3DFMT_D16_LOCKABLE;
69 case WINED3DFMT_D32_UNORM: return D3DFMT_D32;
70 case WINED3DFMT_S1_UINT_D15_UNORM: return D3DFMT_D15S1;
71 case WINED3DFMT_D24_UNORM_S8_UINT: return D3DFMT_D24S8;
72 case WINED3DFMT_X8D24_UNORM: return D3DFMT_D24X8;
73 case WINED3DFMT_S4X4_UINT_D24_UNORM: return D3DFMT_D24X4S4;
74 case WINED3DFMT_D16_UNORM: return D3DFMT_D16;
75 case WINED3DFMT_R16_UINT: return D3DFMT_INDEX16;
76 case WINED3DFMT_R32_UINT: return D3DFMT_INDEX32;
77 default:
78 FIXME("Unhandled wined3d format %#x.\n", format);
79 return D3DFMT_UNKNOWN;
83 enum wined3d_format_id wined3dformat_from_d3dformat(D3DFORMAT format)
85 BYTE *c = (BYTE *)&format;
87 /* Don't translate FOURCC formats */
88 if (isprint(c[0]) && isprint(c[1]) && isprint(c[2]) && isprint(c[3])) return format;
90 switch(format)
92 case D3DFMT_UNKNOWN: return WINED3DFMT_UNKNOWN;
93 case D3DFMT_R8G8B8: return WINED3DFMT_B8G8R8_UNORM;
94 case D3DFMT_A8R8G8B8: return WINED3DFMT_B8G8R8A8_UNORM;
95 case D3DFMT_X8R8G8B8: return WINED3DFMT_B8G8R8X8_UNORM;
96 case D3DFMT_R5G6B5: return WINED3DFMT_B5G6R5_UNORM;
97 case D3DFMT_X1R5G5B5: return WINED3DFMT_B5G5R5X1_UNORM;
98 case D3DFMT_A1R5G5B5: return WINED3DFMT_B5G5R5A1_UNORM;
99 case D3DFMT_A4R4G4B4: return WINED3DFMT_B4G4R4A4_UNORM;
100 case D3DFMT_R3G3B2: return WINED3DFMT_B2G3R3_UNORM;
101 case D3DFMT_A8: return WINED3DFMT_A8_UNORM;
102 case D3DFMT_A8R3G3B2: return WINED3DFMT_B2G3R3A8_UNORM;
103 case D3DFMT_X4R4G4B4: return WINED3DFMT_B4G4R4X4_UNORM;
104 case D3DFMT_A2B10G10R10: return WINED3DFMT_R10G10B10A2_UNORM;
105 case D3DFMT_G16R16: return WINED3DFMT_R16G16_UNORM;
106 case D3DFMT_A8P8: return WINED3DFMT_P8_UINT_A8_UNORM;
107 case D3DFMT_P8: return WINED3DFMT_P8_UINT;
108 case D3DFMT_L8: return WINED3DFMT_L8_UNORM;
109 case D3DFMT_A8L8: return WINED3DFMT_L8A8_UNORM;
110 case D3DFMT_A4L4: return WINED3DFMT_L4A4_UNORM;
111 case D3DFMT_V8U8: return WINED3DFMT_R8G8_SNORM;
112 case D3DFMT_L6V5U5: return WINED3DFMT_R5G5_SNORM_L6_UNORM;
113 case D3DFMT_X8L8V8U8: return WINED3DFMT_R8G8_SNORM_L8X8_UNORM;
114 case D3DFMT_Q8W8V8U8: return WINED3DFMT_R8G8B8A8_SNORM;
115 case D3DFMT_V16U16: return WINED3DFMT_R16G16_SNORM;
116 case D3DFMT_W11V11U10: return WINED3DFMT_R10G11B11_SNORM;
117 case D3DFMT_A2W10V10U10: return WINED3DFMT_R10G10B10_SNORM_A2_UNORM;
118 case D3DFMT_D16_LOCKABLE: return WINED3DFMT_D16_LOCKABLE;
119 case D3DFMT_D32: return WINED3DFMT_D32_UNORM;
120 case D3DFMT_D15S1: return WINED3DFMT_S1_UINT_D15_UNORM;
121 case D3DFMT_D24S8: return WINED3DFMT_D24_UNORM_S8_UINT;
122 case D3DFMT_D24X8: return WINED3DFMT_X8D24_UNORM;
123 case D3DFMT_D24X4S4: return WINED3DFMT_S4X4_UINT_D24_UNORM;
124 case D3DFMT_D16: return WINED3DFMT_D16_UNORM;
125 case D3DFMT_INDEX16: return WINED3DFMT_R16_UINT;
126 case D3DFMT_INDEX32: return WINED3DFMT_R32_UINT;
127 default:
128 FIXME("Unhandled D3DFORMAT %#x.\n", format);
129 return WINED3DFMT_UNKNOWN;
133 unsigned int wined3dmapflags_from_d3dmapflags(unsigned int flags, unsigned int usage)
135 static const unsigned int handled = D3DLOCK_NOSYSLOCK
136 | D3DLOCK_NOOVERWRITE
137 | D3DLOCK_DISCARD
138 | D3DLOCK_NO_DIRTY_UPDATE;
139 unsigned int wined3d_flags;
141 wined3d_flags = flags & handled;
142 if (~usage & D3DUSAGE_WRITEONLY && !(flags & (D3DLOCK_NOOVERWRITE | D3DLOCK_DISCARD)))
143 wined3d_flags |= WINED3D_MAP_READ;
144 if (!(flags & D3DLOCK_READONLY))
145 wined3d_flags |= WINED3D_MAP_WRITE;
146 if (!(wined3d_flags & (WINED3D_MAP_READ | WINED3D_MAP_WRITE)))
147 wined3d_flags |= WINED3D_MAP_WRITE;
148 flags &= ~(handled | D3DLOCK_READONLY);
150 if (flags)
151 FIXME("Unhandled flags %#x.\n", flags);
153 return wined3d_flags;
156 static UINT vertex_count_from_primitive_count(D3DPRIMITIVETYPE primitive_type, UINT primitive_count)
158 switch (primitive_type)
160 case D3DPT_POINTLIST:
161 return primitive_count;
163 case D3DPT_LINELIST:
164 return primitive_count * 2;
166 case D3DPT_LINESTRIP:
167 return primitive_count + 1;
169 case D3DPT_TRIANGLELIST:
170 return primitive_count * 3;
172 case D3DPT_TRIANGLESTRIP:
173 case D3DPT_TRIANGLEFAN:
174 return primitive_count + 2;
176 default:
177 FIXME("Unhandled primitive type %#x.\n", primitive_type);
178 return 0;
182 static D3DSWAPEFFECT d3dswapeffect_from_wined3dswapeffect(enum wined3d_swap_effect effect)
184 switch (effect)
186 case WINED3D_SWAP_EFFECT_DISCARD:
187 return D3DSWAPEFFECT_DISCARD;
188 case WINED3D_SWAP_EFFECT_SEQUENTIAL:
189 return D3DSWAPEFFECT_FLIP;
190 case WINED3D_SWAP_EFFECT_COPY:
191 return D3DSWAPEFFECT_COPY;
192 case WINED3D_SWAP_EFFECT_COPY_VSYNC:
193 return D3DSWAPEFFECT_COPY_VSYNC;
194 default:
195 FIXME("Unhandled swap effect %#x.\n", effect);
196 return D3DSWAPEFFECT_FLIP;
200 static void present_parameters_from_wined3d_swapchain_desc(D3DPRESENT_PARAMETERS *present_parameters,
201 const struct wined3d_swapchain_desc *swapchain_desc, DWORD presentation_interval)
203 present_parameters->BackBufferWidth = swapchain_desc->backbuffer_width;
204 present_parameters->BackBufferHeight = swapchain_desc->backbuffer_height;
205 present_parameters->BackBufferFormat = d3dformat_from_wined3dformat(swapchain_desc->backbuffer_format);
206 present_parameters->BackBufferCount = swapchain_desc->backbuffer_count;
207 present_parameters->MultiSampleType = swapchain_desc->multisample_type;
208 present_parameters->SwapEffect = d3dswapeffect_from_wined3dswapeffect(swapchain_desc->swap_effect);
209 present_parameters->hDeviceWindow = swapchain_desc->device_window;
210 present_parameters->Windowed = swapchain_desc->windowed;
211 present_parameters->EnableAutoDepthStencil = swapchain_desc->enable_auto_depth_stencil;
212 present_parameters->AutoDepthStencilFormat
213 = d3dformat_from_wined3dformat(swapchain_desc->auto_depth_stencil_format);
214 present_parameters->Flags = swapchain_desc->flags & D3DPRESENTFLAGS_MASK;
215 present_parameters->FullScreen_RefreshRateInHz = swapchain_desc->refresh_rate;
216 present_parameters->FullScreen_PresentationInterval = presentation_interval;
219 static enum wined3d_swap_effect wined3dswapeffect_from_d3dswapeffect(D3DSWAPEFFECT effect)
221 switch (effect)
223 case D3DSWAPEFFECT_DISCARD:
224 return WINED3D_SWAP_EFFECT_DISCARD;
225 case D3DSWAPEFFECT_FLIP:
226 return WINED3D_SWAP_EFFECT_SEQUENTIAL;
227 case D3DSWAPEFFECT_COPY:
228 return WINED3D_SWAP_EFFECT_COPY;
229 case D3DSWAPEFFECT_COPY_VSYNC:
230 return WINED3D_SWAP_EFFECT_COPY_VSYNC;
231 default:
232 FIXME("Unhandled swap effect %#x.\n", effect);
233 return WINED3D_SWAP_EFFECT_SEQUENTIAL;
237 static enum wined3d_swap_interval wined3dswapinterval_from_d3d(DWORD interval)
239 switch (interval)
241 case D3DPRESENT_INTERVAL_IMMEDIATE:
242 return WINED3D_SWAP_INTERVAL_IMMEDIATE;
243 case D3DPRESENT_INTERVAL_ONE:
244 return WINED3D_SWAP_INTERVAL_ONE;
245 case D3DPRESENT_INTERVAL_TWO:
246 return WINED3D_SWAP_INTERVAL_TWO;
247 case D3DPRESENT_INTERVAL_THREE:
248 return WINED3D_SWAP_INTERVAL_THREE;
249 case D3DPRESENT_INTERVAL_FOUR:
250 return WINED3D_SWAP_INTERVAL_FOUR;
251 default:
252 FIXME("Unhandled presentation interval %#x.\n", interval);
253 case D3DPRESENT_INTERVAL_DEFAULT:
254 return WINED3D_SWAP_INTERVAL_DEFAULT;
258 static BOOL wined3d_swapchain_desc_from_present_parameters(struct wined3d_swapchain_desc *swapchain_desc,
259 const D3DPRESENT_PARAMETERS *present_parameters)
261 if (!present_parameters->SwapEffect || present_parameters->SwapEffect > D3DSWAPEFFECT_COPY_VSYNC)
263 WARN("Invalid swap effect %u passed.\n", present_parameters->SwapEffect);
264 return FALSE;
266 if (present_parameters->BackBufferCount > 3
267 || ((present_parameters->SwapEffect == D3DSWAPEFFECT_COPY
268 || present_parameters->SwapEffect == D3DSWAPEFFECT_COPY_VSYNC)
269 && present_parameters->BackBufferCount > 1))
271 WARN("Invalid backbuffer count %u.\n", present_parameters->BackBufferCount);
272 return FALSE;
274 switch (present_parameters->FullScreen_PresentationInterval)
276 case D3DPRESENT_INTERVAL_DEFAULT:
277 case D3DPRESENT_INTERVAL_ONE:
278 case D3DPRESENT_INTERVAL_TWO:
279 case D3DPRESENT_INTERVAL_THREE:
280 case D3DPRESENT_INTERVAL_FOUR:
281 case D3DPRESENT_INTERVAL_IMMEDIATE:
282 break;
283 default:
284 WARN("Invalid presentation interval %#x.\n",
285 present_parameters->FullScreen_PresentationInterval);
286 return FALSE;
289 swapchain_desc->backbuffer_width = present_parameters->BackBufferWidth;
290 swapchain_desc->backbuffer_height = present_parameters->BackBufferHeight;
291 swapchain_desc->backbuffer_format = wined3dformat_from_d3dformat(present_parameters->BackBufferFormat);
292 swapchain_desc->backbuffer_count = max(1, present_parameters->BackBufferCount);
293 swapchain_desc->backbuffer_bind_flags = WINED3D_BIND_RENDER_TARGET;
294 swapchain_desc->multisample_type = present_parameters->MultiSampleType;
295 swapchain_desc->multisample_quality = 0; /* d3d9 only */
296 swapchain_desc->swap_effect = wined3dswapeffect_from_d3dswapeffect(present_parameters->SwapEffect);
297 swapchain_desc->device_window = present_parameters->hDeviceWindow;
298 swapchain_desc->windowed = present_parameters->Windowed;
299 swapchain_desc->enable_auto_depth_stencil = present_parameters->EnableAutoDepthStencil;
300 swapchain_desc->auto_depth_stencil_format
301 = wined3dformat_from_d3dformat(present_parameters->AutoDepthStencilFormat);
302 swapchain_desc->flags
303 = (present_parameters->Flags & D3DPRESENTFLAGS_MASK) | WINED3D_SWAPCHAIN_ALLOW_MODE_SWITCH;
304 swapchain_desc->refresh_rate = present_parameters->FullScreen_RefreshRateInHz;
305 swapchain_desc->auto_restore_display_mode = TRUE;
307 if (present_parameters->Flags & ~D3DPRESENTFLAGS_MASK)
308 FIXME("Unhandled flags %#x.\n", present_parameters->Flags & ~D3DPRESENTFLAGS_MASK);
310 return TRUE;
313 void d3dcaps_from_wined3dcaps(D3DCAPS8 *caps, const struct wined3d_caps *wined3d_caps)
315 caps->DeviceType = (D3DDEVTYPE)wined3d_caps->DeviceType;
316 caps->AdapterOrdinal = wined3d_caps->AdapterOrdinal;
317 caps->Caps = wined3d_caps->Caps;
318 caps->Caps2 = wined3d_caps->Caps2;
319 caps->Caps3 = wined3d_caps->Caps3;
320 caps->PresentationIntervals = D3DPRESENT_INTERVAL_IMMEDIATE | D3DPRESENT_INTERVAL_ONE;
321 caps->CursorCaps = wined3d_caps->CursorCaps;
322 caps->DevCaps = wined3d_caps->DevCaps;
323 caps->PrimitiveMiscCaps = wined3d_caps->PrimitiveMiscCaps;
324 caps->RasterCaps = wined3d_caps->RasterCaps;
325 caps->ZCmpCaps = wined3d_caps->ZCmpCaps;
326 caps->SrcBlendCaps = wined3d_caps->SrcBlendCaps;
327 caps->DestBlendCaps = wined3d_caps->DestBlendCaps;
328 caps->AlphaCmpCaps = wined3d_caps->AlphaCmpCaps;
329 caps->ShadeCaps = wined3d_caps->ShadeCaps;
330 caps->TextureCaps = wined3d_caps->TextureCaps;
331 caps->TextureFilterCaps = wined3d_caps->TextureFilterCaps;
332 caps->CubeTextureFilterCaps = wined3d_caps->CubeTextureFilterCaps;
333 caps->VolumeTextureFilterCaps = wined3d_caps->VolumeTextureFilterCaps;
334 caps->TextureAddressCaps = wined3d_caps->TextureAddressCaps;
335 caps->VolumeTextureAddressCaps = wined3d_caps->VolumeTextureAddressCaps;
336 caps->LineCaps = wined3d_caps->LineCaps;
337 caps->MaxTextureWidth = wined3d_caps->MaxTextureWidth;
338 caps->MaxTextureHeight = wined3d_caps->MaxTextureHeight;
339 caps->MaxVolumeExtent = wined3d_caps->MaxVolumeExtent;
340 caps->MaxTextureRepeat = wined3d_caps->MaxTextureRepeat;
341 caps->MaxTextureAspectRatio = wined3d_caps->MaxTextureAspectRatio;
342 caps->MaxAnisotropy = wined3d_caps->MaxAnisotropy;
343 caps->MaxVertexW = wined3d_caps->MaxVertexW;
344 caps->GuardBandLeft = wined3d_caps->GuardBandLeft;
345 caps->GuardBandTop = wined3d_caps->GuardBandTop;
346 caps->GuardBandRight = wined3d_caps->GuardBandRight;
347 caps->GuardBandBottom = wined3d_caps->GuardBandBottom;
348 caps->ExtentsAdjust = wined3d_caps->ExtentsAdjust;
349 caps->StencilCaps = wined3d_caps->StencilCaps;
350 caps->FVFCaps = wined3d_caps->FVFCaps;
351 caps->TextureOpCaps = wined3d_caps->TextureOpCaps;
352 caps->MaxTextureBlendStages = wined3d_caps->MaxTextureBlendStages;
353 caps->MaxSimultaneousTextures = wined3d_caps->MaxSimultaneousTextures;
354 caps->VertexProcessingCaps = wined3d_caps->VertexProcessingCaps;
355 caps->MaxActiveLights = wined3d_caps->MaxActiveLights;
356 caps->MaxUserClipPlanes = wined3d_caps->MaxUserClipPlanes;
357 caps->MaxVertexBlendMatrices = wined3d_caps->MaxVertexBlendMatrices;
358 caps->MaxVertexBlendMatrixIndex = wined3d_caps->MaxVertexBlendMatrixIndex;
359 caps->MaxPointSize = wined3d_caps->MaxPointSize;
360 caps->MaxPrimitiveCount = wined3d_caps->MaxPrimitiveCount;
361 caps->MaxVertexIndex = wined3d_caps->MaxVertexIndex;
362 caps->MaxStreams = wined3d_caps->MaxStreams;
363 caps->MaxStreamStride = wined3d_caps->MaxStreamStride;
364 caps->VertexShaderVersion = wined3d_caps->VertexShaderVersion;
365 caps->MaxVertexShaderConst = wined3d_caps->MaxVertexShaderConst;
366 caps->PixelShaderVersion = wined3d_caps->PixelShaderVersion;
367 caps->MaxPixelShaderValue = wined3d_caps->PixelShader1xMaxValue;
369 caps->Caps2 &= D3DCAPS2_CANCALIBRATEGAMMA | D3DCAPS2_CANRENDERWINDOWED
370 | D3DCAPS2_CANMANAGERESOURCE | D3DCAPS2_DYNAMICTEXTURES | D3DCAPS2_FULLSCREENGAMMA
371 | D3DCAPS2_NO2DDURING3DSCENE | D3DCAPS2_RESERVED;
372 caps->Caps3 &= D3DCAPS3_ALPHA_FULLSCREEN_FLIP_OR_DISCARD | D3DCAPS3_RESERVED;
373 caps->PrimitiveMiscCaps &= D3DPMISCCAPS_MASKZ | D3DPMISCCAPS_LINEPATTERNREP
374 | D3DPMISCCAPS_CULLNONE | D3DPMISCCAPS_CULLCW | D3DPMISCCAPS_CULLCCW
375 | D3DPMISCCAPS_COLORWRITEENABLE | D3DPMISCCAPS_CLIPPLANESCALEDPOINTS
376 | D3DPMISCCAPS_CLIPTLVERTS | D3DPMISCCAPS_TSSARGTEMP | D3DPMISCCAPS_BLENDOP
377 | D3DPMISCCAPS_NULLREFERENCE;
378 caps->RasterCaps &= D3DPRASTERCAPS_DITHER | D3DPRASTERCAPS_PAT | D3DPRASTERCAPS_ZTEST
379 | D3DPRASTERCAPS_FOGVERTEX | D3DPRASTERCAPS_FOGTABLE | D3DPRASTERCAPS_ANTIALIASEDGES
380 | D3DPRASTERCAPS_MIPMAPLODBIAS | D3DPRASTERCAPS_ZBUFFERLESSHSR
381 | D3DPRASTERCAPS_FOGRANGE | D3DPRASTERCAPS_ANISOTROPY | D3DPRASTERCAPS_WBUFFER
382 | D3DPRASTERCAPS_WFOG | D3DPRASTERCAPS_ZFOG | D3DPRASTERCAPS_COLORPERSPECTIVE
383 | D3DPRASTERCAPS_STRETCHBLTMULTISAMPLE | WINED3DPRASTERCAPS_DEPTHBIAS;
384 if (caps->RasterCaps & WINED3DPRASTERCAPS_DEPTHBIAS)
385 caps->RasterCaps = (caps->RasterCaps | D3DPRASTERCAPS_ZBIAS) & ~WINED3DPRASTERCAPS_DEPTHBIAS;
386 caps->SrcBlendCaps &= D3DPBLENDCAPS_ZERO | D3DPBLENDCAPS_ONE | D3DPBLENDCAPS_SRCCOLOR
387 | D3DPBLENDCAPS_INVSRCCOLOR | D3DPBLENDCAPS_SRCALPHA | D3DPBLENDCAPS_INVSRCALPHA
388 | D3DPBLENDCAPS_DESTALPHA | D3DPBLENDCAPS_INVDESTALPHA | D3DPBLENDCAPS_DESTCOLOR
389 | D3DPBLENDCAPS_INVDESTCOLOR | D3DPBLENDCAPS_SRCALPHASAT | D3DPBLENDCAPS_BOTHSRCALPHA
390 | D3DPBLENDCAPS_BOTHINVSRCALPHA;
391 caps->DestBlendCaps &= D3DPBLENDCAPS_ZERO | D3DPBLENDCAPS_ONE | D3DPBLENDCAPS_SRCCOLOR
392 | D3DPBLENDCAPS_INVSRCCOLOR | D3DPBLENDCAPS_SRCALPHA | D3DPBLENDCAPS_INVSRCALPHA
393 | D3DPBLENDCAPS_DESTALPHA | D3DPBLENDCAPS_INVDESTALPHA | D3DPBLENDCAPS_DESTCOLOR
394 | D3DPBLENDCAPS_INVDESTCOLOR | D3DPBLENDCAPS_SRCALPHASAT | D3DPBLENDCAPS_BOTHSRCALPHA
395 | D3DPBLENDCAPS_BOTHINVSRCALPHA;
396 caps->TextureCaps &= D3DPTEXTURECAPS_PERSPECTIVE | D3DPTEXTURECAPS_POW2 | D3DPTEXTURECAPS_ALPHA
397 | D3DPTEXTURECAPS_SQUAREONLY | D3DPTEXTURECAPS_TEXREPEATNOTSCALEDBYSIZE
398 | D3DPTEXTURECAPS_ALPHAPALETTE | D3DPTEXTURECAPS_NONPOW2CONDITIONAL
399 | D3DPTEXTURECAPS_PROJECTED | D3DPTEXTURECAPS_CUBEMAP | D3DPTEXTURECAPS_VOLUMEMAP
400 | D3DPTEXTURECAPS_MIPMAP | D3DPTEXTURECAPS_MIPVOLUMEMAP | D3DPTEXTURECAPS_MIPCUBEMAP
401 | D3DPTEXTURECAPS_CUBEMAP_POW2 | D3DPTEXTURECAPS_VOLUMEMAP_POW2;
402 caps->TextureFilterCaps &= D3DPTFILTERCAPS_MINFPOINT | D3DPTFILTERCAPS_MINFLINEAR
403 | D3DPTFILTERCAPS_MINFANISOTROPIC | D3DPTFILTERCAPS_MIPFPOINT
404 | D3DPTFILTERCAPS_MIPFLINEAR | D3DPTFILTERCAPS_MAGFPOINT | D3DPTFILTERCAPS_MAGFLINEAR
405 | D3DPTFILTERCAPS_MAGFANISOTROPIC | D3DPTFILTERCAPS_MAGFAFLATCUBIC
406 | D3DPTFILTERCAPS_MAGFGAUSSIANCUBIC;
407 caps->CubeTextureFilterCaps &= D3DPTFILTERCAPS_MINFPOINT | D3DPTFILTERCAPS_MINFLINEAR
408 | D3DPTFILTERCAPS_MINFANISOTROPIC | D3DPTFILTERCAPS_MIPFPOINT
409 | D3DPTFILTERCAPS_MIPFLINEAR | D3DPTFILTERCAPS_MAGFPOINT | D3DPTFILTERCAPS_MAGFLINEAR
410 | D3DPTFILTERCAPS_MAGFANISOTROPIC | D3DPTFILTERCAPS_MAGFAFLATCUBIC
411 | D3DPTFILTERCAPS_MAGFGAUSSIANCUBIC;
412 caps->VolumeTextureFilterCaps &= D3DPTFILTERCAPS_MINFPOINT | D3DPTFILTERCAPS_MINFLINEAR
413 | D3DPTFILTERCAPS_MINFANISOTROPIC | D3DPTFILTERCAPS_MIPFPOINT
414 | D3DPTFILTERCAPS_MIPFLINEAR | D3DPTFILTERCAPS_MAGFPOINT | D3DPTFILTERCAPS_MAGFLINEAR
415 | D3DPTFILTERCAPS_MAGFANISOTROPIC | D3DPTFILTERCAPS_MAGFAFLATCUBIC
416 | D3DPTFILTERCAPS_MAGFGAUSSIANCUBIC;
417 caps->StencilCaps &= ~WINED3DSTENCILCAPS_TWOSIDED;
418 caps->VertexProcessingCaps &= D3DVTXPCAPS_TEXGEN | D3DVTXPCAPS_MATERIALSOURCE7
419 | D3DVTXPCAPS_DIRECTIONALLIGHTS | D3DVTXPCAPS_POSITIONALLIGHTS | D3DVTXPCAPS_LOCALVIEWER
420 | D3DVTXPCAPS_TWEENING | D3DVTXPCAPS_NO_VSDT_UBYTE4;
422 /* D3D8 doesn't support SM 2.0 or higher, so clamp to 1.x */
423 if (caps->PixelShaderVersion)
424 caps->PixelShaderVersion = D3DPS_VERSION(1, 4);
425 else
426 caps->PixelShaderVersion = D3DPS_VERSION(0, 0);
427 if (caps->VertexShaderVersion)
428 caps->VertexShaderVersion = D3DVS_VERSION(1, 1);
429 else
430 caps->VertexShaderVersion = D3DVS_VERSION(0, 0);
431 caps->MaxVertexShaderConst = min(D3D8_MAX_VERTEX_SHADER_CONSTANTF, caps->MaxVertexShaderConst);
434 /* Handle table functions */
435 static DWORD d3d8_allocate_handle(struct d3d8_handle_table *t, void *object, enum d3d8_handle_type type)
437 struct d3d8_handle_entry *entry;
439 if (t->free_entries)
441 DWORD index = t->free_entries - t->entries;
442 /* Use a free handle */
443 entry = t->free_entries;
444 if (entry->type != D3D8_HANDLE_FREE)
446 ERR("Handle %u(%p) is in the free list, but has type %#x.\n", index, entry, entry->type);
447 return D3D8_INVALID_HANDLE;
449 t->free_entries = entry->object;
450 entry->object = object;
451 entry->type = type;
453 return index;
456 if (!(t->entry_count < t->table_size))
458 /* Grow the table */
459 UINT new_size = t->table_size + (t->table_size >> 1);
460 struct d3d8_handle_entry *new_entries;
462 if (!(new_entries = heap_realloc(t->entries, new_size * sizeof(*t->entries))))
464 ERR("Failed to grow the handle table.\n");
465 return D3D8_INVALID_HANDLE;
467 t->entries = new_entries;
468 t->table_size = new_size;
471 entry = &t->entries[t->entry_count];
472 entry->object = object;
473 entry->type = type;
475 return t->entry_count++;
478 static void *d3d8_free_handle(struct d3d8_handle_table *t, DWORD handle, enum d3d8_handle_type type)
480 struct d3d8_handle_entry *entry;
481 void *object;
483 if (handle == D3D8_INVALID_HANDLE || handle >= t->entry_count)
485 WARN("Invalid handle %u passed.\n", handle);
486 return NULL;
489 entry = &t->entries[handle];
490 if (entry->type != type)
492 WARN("Handle %u(%p) is not of type %#x.\n", handle, entry, type);
493 return NULL;
496 object = entry->object;
497 entry->object = t->free_entries;
498 entry->type = D3D8_HANDLE_FREE;
499 t->free_entries = entry;
501 return object;
504 static void *d3d8_get_object(struct d3d8_handle_table *t, DWORD handle, enum d3d8_handle_type type)
506 struct d3d8_handle_entry *entry;
508 if (handle == D3D8_INVALID_HANDLE || handle >= t->entry_count)
510 WARN("Invalid handle %u passed.\n", handle);
511 return NULL;
514 entry = &t->entries[handle];
515 if (entry->type != type)
517 WARN("Handle %u(%p) is not of type %#x.\n", handle, entry, type);
518 return NULL;
521 return entry->object;
524 static void device_reset_viewport_state(struct d3d8_device *device)
526 struct wined3d_viewport vp;
527 RECT rect;
529 wined3d_device_get_viewports(device->wined3d_device, NULL, &vp);
530 wined3d_stateblock_set_viewport(device->state, &vp);
531 wined3d_device_get_scissor_rects(device->wined3d_device, NULL, &rect);
532 wined3d_stateblock_set_scissor_rect(device->state, &rect);
535 static HRESULT WINAPI d3d8_device_QueryInterface(IDirect3DDevice8 *iface, REFIID riid, void **out)
537 TRACE("iface %p, riid %s, out %p.\n",
538 iface, debugstr_guid(riid), out);
540 if (IsEqualGUID(riid, &IID_IDirect3DDevice8)
541 || IsEqualGUID(riid, &IID_IUnknown))
543 IDirect3DDevice8_AddRef(iface);
544 *out = iface;
545 return S_OK;
548 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
550 *out = NULL;
551 return E_NOINTERFACE;
554 static ULONG WINAPI d3d8_device_AddRef(IDirect3DDevice8 *iface)
556 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
557 ULONG ref = InterlockedIncrement(&device->ref);
559 TRACE("%p increasing refcount to %u.\n", iface, ref);
561 return ref;
564 static ULONG WINAPI d3d8_device_Release(IDirect3DDevice8 *iface)
566 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
567 ULONG ref;
569 if (device->in_destruction)
570 return 0;
572 ref = InterlockedDecrement(&device->ref);
574 TRACE("%p decreasing refcount to %u.\n", iface, ref);
576 if (!ref)
578 IDirect3D8 *parent = device->d3d_parent;
579 unsigned i;
581 TRACE("Releasing wined3d device %p.\n", device->wined3d_device);
583 wined3d_mutex_lock();
585 device->in_destruction = TRUE;
587 for (i = 0; i < device->numConvertedDecls; ++i)
589 d3d8_vertex_declaration_destroy(device->decls[i].declaration);
591 heap_free(device->decls);
593 if (device->vertex_buffer)
594 wined3d_buffer_decref(device->vertex_buffer);
595 if (device->index_buffer)
596 wined3d_buffer_decref(device->index_buffer);
598 if (device->recording)
599 wined3d_stateblock_decref(device->recording);
600 wined3d_stateblock_decref(device->state);
602 wined3d_swapchain_decref(device->implicit_swapchain);
603 wined3d_device_release_focus_window(device->wined3d_device);
604 wined3d_device_decref(device->wined3d_device);
605 heap_free(device->handle_table.entries);
606 heap_free(device);
608 wined3d_mutex_unlock();
610 IDirect3D8_Release(parent);
612 return ref;
615 static HRESULT WINAPI d3d8_device_TestCooperativeLevel(IDirect3DDevice8 *iface)
617 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
619 TRACE("iface %p.\n", iface);
621 TRACE("device state: %#x.\n", device->device_state);
623 switch (device->device_state)
625 default:
626 case D3D8_DEVICE_STATE_OK:
627 return D3D_OK;
628 case D3D8_DEVICE_STATE_LOST:
629 return D3DERR_DEVICELOST;
630 case D3D8_DEVICE_STATE_NOT_RESET:
631 return D3DERR_DEVICENOTRESET;
635 static UINT WINAPI d3d8_device_GetAvailableTextureMem(IDirect3DDevice8 *iface)
637 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
638 UINT ret;
640 TRACE("iface %p.\n", iface);
642 wined3d_mutex_lock();
643 ret = wined3d_device_get_available_texture_mem(device->wined3d_device);
644 wined3d_mutex_unlock();
646 return ret;
649 static HRESULT WINAPI d3d8_device_ResourceManagerDiscardBytes(IDirect3DDevice8 *iface, DWORD byte_count)
651 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
653 TRACE("iface %p, byte_count %u.\n", iface, byte_count);
655 if (byte_count)
656 FIXME("Byte count ignored.\n");
658 wined3d_mutex_lock();
659 wined3d_device_evict_managed_resources(device->wined3d_device);
660 wined3d_mutex_unlock();
662 return D3D_OK;
665 static HRESULT WINAPI d3d8_device_GetDirect3D(IDirect3DDevice8 *iface, IDirect3D8 **d3d8)
667 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
669 TRACE("iface %p, d3d8 %p.\n", iface, d3d8);
671 if (!d3d8)
672 return D3DERR_INVALIDCALL;
674 return IDirect3D8_QueryInterface(device->d3d_parent, &IID_IDirect3D8, (void **)d3d8);
677 static HRESULT WINAPI d3d8_device_GetDeviceCaps(IDirect3DDevice8 *iface, D3DCAPS8 *caps)
679 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
680 struct wined3d_caps wined3d_caps;
681 HRESULT hr;
683 TRACE("iface %p, caps %p.\n", iface, caps);
685 if (!caps)
686 return D3DERR_INVALIDCALL;
688 wined3d_mutex_lock();
689 hr = wined3d_device_get_device_caps(device->wined3d_device, &wined3d_caps);
690 wined3d_mutex_unlock();
692 d3dcaps_from_wined3dcaps(caps, &wined3d_caps);
694 return hr;
697 static HRESULT WINAPI d3d8_device_GetDisplayMode(IDirect3DDevice8 *iface, D3DDISPLAYMODE *mode)
699 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
700 struct wined3d_display_mode wined3d_mode;
701 HRESULT hr;
703 TRACE("iface %p, mode %p.\n", iface, mode);
705 wined3d_mutex_lock();
706 hr = wined3d_device_get_display_mode(device->wined3d_device, 0, &wined3d_mode, NULL);
707 wined3d_mutex_unlock();
709 if (SUCCEEDED(hr))
711 mode->Width = wined3d_mode.width;
712 mode->Height = wined3d_mode.height;
713 mode->RefreshRate = wined3d_mode.refresh_rate;
714 mode->Format = d3dformat_from_wined3dformat(wined3d_mode.format_id);
717 return hr;
720 static HRESULT WINAPI d3d8_device_GetCreationParameters(IDirect3DDevice8 *iface,
721 D3DDEVICE_CREATION_PARAMETERS *parameters)
723 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
725 TRACE("iface %p, parameters %p.\n", iface, parameters);
727 wined3d_mutex_lock();
728 wined3d_device_get_creation_parameters(device->wined3d_device,
729 (struct wined3d_device_creation_parameters *)parameters);
730 wined3d_mutex_unlock();
732 return D3D_OK;
735 static HRESULT WINAPI d3d8_device_SetCursorProperties(IDirect3DDevice8 *iface,
736 UINT hotspot_x, UINT hotspot_y, IDirect3DSurface8 *bitmap)
738 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
739 struct d3d8_surface *bitmap_impl = unsafe_impl_from_IDirect3DSurface8(bitmap);
740 HRESULT hr;
742 TRACE("iface %p, hotspot_x %u, hotspot_y %u, bitmap %p.\n",
743 iface, hotspot_x, hotspot_y, bitmap);
745 if (!bitmap)
747 WARN("No cursor bitmap, returning D3DERR_INVALIDCALL.\n");
748 return D3DERR_INVALIDCALL;
751 wined3d_mutex_lock();
752 hr = wined3d_device_set_cursor_properties(device->wined3d_device,
753 hotspot_x, hotspot_y, bitmap_impl->wined3d_texture, bitmap_impl->sub_resource_idx);
754 wined3d_mutex_unlock();
756 return hr;
759 static void WINAPI d3d8_device_SetCursorPosition(IDirect3DDevice8 *iface, UINT x, UINT y, DWORD flags)
761 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
763 TRACE("iface %p, x %u, y %u, flags %#x.\n", iface, x, y, flags);
765 wined3d_mutex_lock();
766 wined3d_device_set_cursor_position(device->wined3d_device, x, y, flags);
767 wined3d_mutex_unlock();
770 static BOOL WINAPI d3d8_device_ShowCursor(IDirect3DDevice8 *iface, BOOL show)
772 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
773 BOOL ret;
775 TRACE("iface %p, show %#x.\n", iface, show);
777 wined3d_mutex_lock();
778 ret = wined3d_device_show_cursor(device->wined3d_device, show);
779 wined3d_mutex_unlock();
781 return ret;
784 static HRESULT WINAPI d3d8_device_CreateAdditionalSwapChain(IDirect3DDevice8 *iface,
785 D3DPRESENT_PARAMETERS *present_parameters, IDirect3DSwapChain8 **swapchain)
787 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
788 struct wined3d_swapchain_desc desc;
789 struct d3d8_swapchain *object;
790 unsigned int swap_interval;
791 unsigned int i, count;
792 HRESULT hr;
794 TRACE("iface %p, present_parameters %p, swapchain %p.\n",
795 iface, present_parameters, swapchain);
797 if (!present_parameters->Windowed)
799 WARN("Trying to create an additional fullscreen swapchain, returning D3DERR_INVALIDCALL.\n");
800 return D3DERR_INVALIDCALL;
803 wined3d_mutex_lock();
804 count = wined3d_device_get_swapchain_count(device->wined3d_device);
805 for (i = 0; i < count; ++i)
807 struct wined3d_swapchain *wined3d_swapchain;
809 wined3d_swapchain = wined3d_device_get_swapchain(device->wined3d_device, i);
810 wined3d_swapchain_get_desc(wined3d_swapchain, &desc);
812 if (!desc.windowed)
814 wined3d_mutex_unlock();
815 WARN("Trying to create an additional swapchain in fullscreen mode, returning D3DERR_INVALIDCALL.\n");
816 return D3DERR_INVALIDCALL;
819 wined3d_mutex_unlock();
821 if (!wined3d_swapchain_desc_from_present_parameters(&desc, present_parameters))
822 return D3DERR_INVALIDCALL;
823 swap_interval = wined3dswapinterval_from_d3d(present_parameters->FullScreen_PresentationInterval);
824 if (SUCCEEDED(hr = d3d8_swapchain_create(device, &desc, swap_interval, &object)))
825 *swapchain = &object->IDirect3DSwapChain8_iface;
826 present_parameters_from_wined3d_swapchain_desc(present_parameters,
827 &desc, present_parameters->FullScreen_PresentationInterval);
829 return hr;
832 static HRESULT CDECL reset_enum_callback(struct wined3d_resource *resource)
834 struct d3d8_vertexbuffer *vertex_buffer;
835 struct d3d8_indexbuffer *index_buffer;
836 struct wined3d_resource_desc desc;
837 IDirect3DBaseTexture8 *texture;
838 struct d3d8_surface *surface;
839 IUnknown *parent;
841 wined3d_resource_get_desc(resource, &desc);
842 if (desc.access & WINED3D_RESOURCE_ACCESS_CPU)
843 return D3D_OK;
845 if (desc.resource_type != WINED3D_RTYPE_TEXTURE_2D)
847 if (desc.bind_flags & WINED3D_BIND_VERTEX_BUFFER)
849 vertex_buffer = wined3d_resource_get_parent(resource);
850 if (vertex_buffer && vertex_buffer->draw_buffer)
851 return D3D_OK;
853 if (desc.bind_flags & WINED3D_BIND_INDEX_BUFFER)
855 index_buffer = wined3d_resource_get_parent(resource);
856 if (index_buffer && index_buffer->draw_buffer)
857 return D3D_OK;
860 WARN("Resource %p in pool D3DPOOL_DEFAULT blocks the Reset call.\n", resource);
861 return D3DERR_DEVICELOST;
864 parent = wined3d_resource_get_parent(resource);
865 if (parent && SUCCEEDED(IUnknown_QueryInterface(parent, &IID_IDirect3DBaseTexture8, (void **)&texture)))
867 IDirect3DBaseTexture8_Release(texture);
868 WARN("Texture %p (resource %p) in pool D3DPOOL_DEFAULT blocks the Reset call.\n", texture, resource);
869 return D3DERR_DEVICELOST;
872 surface = wined3d_texture_get_sub_resource_parent(wined3d_texture_from_resource(resource), 0);
873 if (!surface->resource.refcount)
874 return D3D_OK;
876 WARN("Surface %p in pool D3DPOOL_DEFAULT blocks the Reset call.\n", surface);
877 return D3DERR_DEVICELOST;
880 static HRESULT WINAPI d3d8_device_Reset(IDirect3DDevice8 *iface,
881 D3DPRESENT_PARAMETERS *present_parameters)
883 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
884 struct wined3d_swapchain_desc swapchain_desc;
885 struct d3d8_swapchain *implicit_swapchain;
886 HRESULT hr;
888 TRACE("iface %p, present_parameters %p.\n", iface, present_parameters);
890 if (device->device_state == D3D8_DEVICE_STATE_LOST)
892 WARN("App not active, returning D3DERR_DEVICELOST.\n");
893 return D3DERR_DEVICELOST;
895 if (!wined3d_swapchain_desc_from_present_parameters(&swapchain_desc, present_parameters))
896 return D3DERR_INVALIDCALL;
897 swapchain_desc.flags |= WINED3D_SWAPCHAIN_IMPLICIT;
899 wined3d_mutex_lock();
901 if (device->vertex_buffer)
903 wined3d_buffer_decref(device->vertex_buffer);
904 device->vertex_buffer = NULL;
905 device->vertex_buffer_size = 0;
907 if (device->index_buffer)
909 wined3d_buffer_decref(device->index_buffer);
910 device->index_buffer = NULL;
911 device->index_buffer_size = 0;
914 if (device->recording)
915 wined3d_stateblock_decref(device->recording);
916 device->recording = NULL;
917 device->update_state = device->state;
918 wined3d_stateblock_reset(device->state);
920 if (SUCCEEDED(hr = wined3d_device_reset(device->wined3d_device, &swapchain_desc,
921 NULL, reset_enum_callback, TRUE)))
923 present_parameters->BackBufferCount = swapchain_desc.backbuffer_count;
924 implicit_swapchain = wined3d_swapchain_get_parent(device->implicit_swapchain);
925 implicit_swapchain->swap_interval
926 = wined3dswapinterval_from_d3d(present_parameters->FullScreen_PresentationInterval);
927 wined3d_stateblock_set_render_state(device->state, WINED3D_RS_POINTSIZE_MIN, 0);
928 wined3d_stateblock_set_render_state(device->state, WINED3D_RS_ZENABLE,
929 !!swapchain_desc.enable_auto_depth_stencil);
930 device_reset_viewport_state(device);
931 device->device_state = D3D8_DEVICE_STATE_OK;
933 else
935 device->device_state = D3D8_DEVICE_STATE_NOT_RESET;
937 wined3d_mutex_unlock();
939 return hr;
942 static HRESULT WINAPI d3d8_device_Present(IDirect3DDevice8 *iface, const RECT *src_rect,
943 const RECT *dst_rect, HWND dst_window_override, const RGNDATA *dirty_region)
945 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
946 struct d3d8_swapchain *implicit_swapchain;
948 TRACE("iface %p, src_rect %s, dst_rect %s, dst_window_override %p, dirty_region %p.\n",
949 iface, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect), dst_window_override, dirty_region);
951 /* Fraps does not hook IDirect3DDevice8::Present regardless of the hotpatch
952 * attribute. It only hooks IDirect3DSwapChain8::Present. Yet it properly
953 * shows a framerate on Windows in applications that only call the device
954 * method, like e.g. the dx8 sdk samples. The conclusion is that native
955 * calls the swapchain's public method from the device. */
956 implicit_swapchain = wined3d_swapchain_get_parent(device->implicit_swapchain);
957 return IDirect3DSwapChain8_Present(&implicit_swapchain->IDirect3DSwapChain8_iface,
958 src_rect, dst_rect, dst_window_override, dirty_region);
961 static HRESULT WINAPI d3d8_device_GetBackBuffer(IDirect3DDevice8 *iface,
962 UINT backbuffer_idx, D3DBACKBUFFER_TYPE backbuffer_type, IDirect3DSurface8 **backbuffer)
964 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
965 struct wined3d_texture *wined3d_texture;
966 struct d3d8_surface *surface_impl;
968 TRACE("iface %p, backbuffer_idx %u, backbuffer_type %#x, backbuffer %p.\n",
969 iface, backbuffer_idx, backbuffer_type, backbuffer);
971 /* backbuffer_type is ignored by native. */
973 /* No need to check for backbuffer == NULL, Windows crashes in that case. */
974 wined3d_mutex_lock();
976 if (!(wined3d_texture = wined3d_swapchain_get_back_buffer(device->implicit_swapchain, backbuffer_idx)))
978 wined3d_mutex_unlock();
979 *backbuffer = NULL;
980 return D3DERR_INVALIDCALL;
983 surface_impl = wined3d_texture_get_sub_resource_parent(wined3d_texture, 0);
984 *backbuffer = &surface_impl->IDirect3DSurface8_iface;
985 IDirect3DSurface8_AddRef(*backbuffer);
987 wined3d_mutex_unlock();
988 return D3D_OK;
991 static HRESULT WINAPI d3d8_device_GetRasterStatus(IDirect3DDevice8 *iface, D3DRASTER_STATUS *raster_status)
993 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
994 HRESULT hr;
996 TRACE("iface %p, raster_status %p.\n", iface, raster_status);
998 wined3d_mutex_lock();
999 hr = wined3d_device_get_raster_status(device->wined3d_device, 0, (struct wined3d_raster_status *)raster_status);
1000 wined3d_mutex_unlock();
1002 return hr;
1005 static void WINAPI d3d8_device_SetGammaRamp(IDirect3DDevice8 *iface, DWORD flags, const D3DGAMMARAMP *ramp)
1007 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1009 TRACE("iface %p, flags %#x, ramp %p.\n", iface, flags, ramp);
1011 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
1012 wined3d_mutex_lock();
1013 wined3d_device_set_gamma_ramp(device->wined3d_device, 0, flags, (const struct wined3d_gamma_ramp *)ramp);
1014 wined3d_mutex_unlock();
1017 static void WINAPI d3d8_device_GetGammaRamp(IDirect3DDevice8 *iface, D3DGAMMARAMP *ramp)
1019 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1021 TRACE("iface %p, ramp %p.\n", iface, ramp);
1023 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
1024 wined3d_mutex_lock();
1025 wined3d_device_get_gamma_ramp(device->wined3d_device, 0, (struct wined3d_gamma_ramp *)ramp);
1026 wined3d_mutex_unlock();
1029 static HRESULT WINAPI d3d8_device_CreateTexture(IDirect3DDevice8 *iface,
1030 UINT width, UINT height, UINT levels, DWORD usage, D3DFORMAT format,
1031 D3DPOOL pool, IDirect3DTexture8 **texture)
1033 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1034 struct d3d8_texture *object;
1035 HRESULT hr;
1037 TRACE("iface %p, width %u, height %u, levels %u, usage %#x, format %#x, pool %#x, texture %p.\n",
1038 iface, width, height, levels, usage, format, pool, texture);
1040 if (!format)
1041 return D3DERR_INVALIDCALL;
1043 *texture = NULL;
1044 if (!(object = heap_alloc_zero(sizeof(*object))))
1045 return D3DERR_OUTOFVIDEOMEMORY;
1047 hr = texture_init(object, device, width, height, levels, usage, format, pool);
1048 if (FAILED(hr))
1050 WARN("Failed to initialize texture, hr %#x.\n", hr);
1051 heap_free(object);
1052 return hr;
1055 TRACE("Created texture %p.\n", object);
1056 *texture = (IDirect3DTexture8 *)&object->IDirect3DBaseTexture8_iface;
1058 return D3D_OK;
1061 static HRESULT WINAPI d3d8_device_CreateVolumeTexture(IDirect3DDevice8 *iface,
1062 UINT width, UINT height, UINT depth, UINT levels, DWORD usage, D3DFORMAT format,
1063 D3DPOOL pool, IDirect3DVolumeTexture8 **texture)
1065 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1066 struct d3d8_texture *object;
1067 HRESULT hr;
1069 TRACE("iface %p, width %u, height %u, depth %u, levels %u, usage %#x, format %#x, pool %#x, texture %p.\n",
1070 iface, width, height, depth, levels, usage, format, pool, texture);
1072 if (!format)
1073 return D3DERR_INVALIDCALL;
1075 *texture = NULL;
1076 if (!(object = heap_alloc_zero(sizeof(*object))))
1077 return D3DERR_OUTOFVIDEOMEMORY;
1079 hr = volumetexture_init(object, device, width, height, depth, levels, usage, format, pool);
1080 if (FAILED(hr))
1082 WARN("Failed to initialize volume texture, hr %#x.\n", hr);
1083 heap_free(object);
1084 return hr;
1087 TRACE("Created volume texture %p.\n", object);
1088 *texture = (IDirect3DVolumeTexture8 *)&object->IDirect3DBaseTexture8_iface;
1090 return D3D_OK;
1093 static HRESULT WINAPI d3d8_device_CreateCubeTexture(IDirect3DDevice8 *iface, UINT edge_length,
1094 UINT levels, DWORD usage, D3DFORMAT format, D3DPOOL pool, IDirect3DCubeTexture8 **texture)
1096 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1097 struct d3d8_texture *object;
1098 HRESULT hr;
1100 TRACE("iface %p, edge_length %u, levels %u, usage %#x, format %#x, pool %#x, texture %p.\n",
1101 iface, edge_length, levels, usage, format, pool, texture);
1103 if (!format)
1104 return D3DERR_INVALIDCALL;
1106 *texture = NULL;
1107 if (!(object = heap_alloc_zero(sizeof(*object))))
1108 return D3DERR_OUTOFVIDEOMEMORY;
1110 hr = cubetexture_init(object, device, edge_length, levels, usage, format, pool);
1111 if (FAILED(hr))
1113 WARN("Failed to initialize cube texture, hr %#x.\n", hr);
1114 heap_free(object);
1115 return hr;
1118 TRACE("Created cube texture %p.\n", object);
1119 *texture = (IDirect3DCubeTexture8 *)&object->IDirect3DBaseTexture8_iface;
1121 return hr;
1124 static HRESULT WINAPI d3d8_device_CreateVertexBuffer(IDirect3DDevice8 *iface, UINT size,
1125 DWORD usage, DWORD fvf, D3DPOOL pool, IDirect3DVertexBuffer8 **buffer)
1127 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1128 struct d3d8_vertexbuffer *object;
1129 HRESULT hr;
1131 TRACE("iface %p, size %u, usage %#x, fvf %#x, pool %#x, buffer %p.\n",
1132 iface, size, usage, fvf, pool, buffer);
1134 if (!(object = heap_alloc_zero(sizeof(*object))))
1135 return D3DERR_OUTOFVIDEOMEMORY;
1137 hr = vertexbuffer_init(object, device, size, usage, fvf, pool);
1138 if (FAILED(hr))
1140 WARN("Failed to initialize vertex buffer, hr %#x.\n", hr);
1141 heap_free(object);
1142 return hr;
1145 TRACE("Created vertex buffer %p.\n", object);
1146 *buffer = &object->IDirect3DVertexBuffer8_iface;
1148 return D3D_OK;
1151 static HRESULT WINAPI d3d8_device_CreateIndexBuffer(IDirect3DDevice8 *iface, UINT size,
1152 DWORD usage, D3DFORMAT format, D3DPOOL pool, IDirect3DIndexBuffer8 **buffer)
1154 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1155 struct d3d8_indexbuffer *object;
1156 HRESULT hr;
1158 TRACE("iface %p, size %u, usage %#x, format %#x, pool %#x, buffer %p.\n",
1159 iface, size, usage, format, pool, buffer);
1161 if (!(object = heap_alloc_zero(sizeof(*object))))
1162 return D3DERR_OUTOFVIDEOMEMORY;
1164 hr = indexbuffer_init(object, device, size, usage, format, pool);
1165 if (FAILED(hr))
1167 WARN("Failed to initialize index buffer, hr %#x.\n", hr);
1168 heap_free(object);
1169 return hr;
1172 TRACE("Created index buffer %p.\n", object);
1173 *buffer = &object->IDirect3DIndexBuffer8_iface;
1175 return D3D_OK;
1178 static HRESULT d3d8_device_create_surface(struct d3d8_device *device, enum wined3d_format_id format,
1179 enum wined3d_multisample_type multisample_type, unsigned int bind_flags, unsigned int access,
1180 unsigned int width, unsigned int height, IDirect3DSurface8 **surface)
1182 struct wined3d_resource_desc desc;
1183 struct d3d8_surface *surface_impl;
1184 struct wined3d_texture *texture;
1185 HRESULT hr;
1187 TRACE("device %p, format %#x, multisample_type %#x, bind_flags %#x, "
1188 "access %#x, width %u, height %u, surface %p.\n",
1189 device, format, multisample_type, bind_flags, access, width, height, surface);
1191 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
1192 desc.format = format;
1193 desc.multisample_type = multisample_type;
1194 desc.multisample_quality = 0;
1195 desc.usage = 0;
1196 desc.bind_flags = bind_flags;
1197 desc.access = access;
1198 desc.width = width;
1199 desc.height = height;
1200 desc.depth = 1;
1201 desc.size = 0;
1203 wined3d_mutex_lock();
1205 if (FAILED(hr = wined3d_texture_create(device->wined3d_device, &desc,
1206 1, 1, 0, NULL, NULL, &d3d8_null_wined3d_parent_ops, &texture)))
1208 wined3d_mutex_unlock();
1209 WARN("Failed to create texture, hr %#x.\n", hr);
1210 return hr;
1213 surface_impl = wined3d_texture_get_sub_resource_parent(texture, 0);
1214 surface_impl->parent_device = &device->IDirect3DDevice8_iface;
1215 *surface = &surface_impl->IDirect3DSurface8_iface;
1216 IDirect3DSurface8_AddRef(*surface);
1217 wined3d_texture_decref(texture);
1219 wined3d_mutex_unlock();
1221 return D3D_OK;
1224 static HRESULT WINAPI d3d8_device_CreateRenderTarget(IDirect3DDevice8 *iface, UINT width,
1225 UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, BOOL lockable,
1226 IDirect3DSurface8 **surface)
1228 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1229 unsigned int access = WINED3D_RESOURCE_ACCESS_GPU;
1231 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, lockable %#x, surface %p.\n",
1232 iface, width, height, format, multisample_type, lockable, surface);
1234 if (!format)
1235 return D3DERR_INVALIDCALL;
1237 *surface = NULL;
1238 if (lockable)
1239 access |= WINED3D_RESOURCE_ACCESS_MAP_R | WINED3D_RESOURCE_ACCESS_MAP_W;
1241 return d3d8_device_create_surface(device, wined3dformat_from_d3dformat(format),
1242 multisample_type, WINED3D_BIND_RENDER_TARGET, access, width, height, surface);
1245 static HRESULT WINAPI d3d8_device_CreateDepthStencilSurface(IDirect3DDevice8 *iface,
1246 UINT width, UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type,
1247 IDirect3DSurface8 **surface)
1249 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1251 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, surface %p.\n",
1252 iface, width, height, format, multisample_type, surface);
1254 if (!format)
1255 return D3DERR_INVALIDCALL;
1257 *surface = NULL;
1259 return d3d8_device_create_surface(device, wined3dformat_from_d3dformat(format),
1260 multisample_type, WINED3D_BIND_DEPTH_STENCIL,
1261 WINED3D_RESOURCE_ACCESS_GPU, width, height, surface);
1264 /* IDirect3DDevice8Impl::CreateImageSurface returns surface with pool type SYSTEMMEM */
1265 static HRESULT WINAPI d3d8_device_CreateImageSurface(IDirect3DDevice8 *iface, UINT width,
1266 UINT height, D3DFORMAT format, IDirect3DSurface8 **surface)
1268 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1269 unsigned int access = WINED3D_RESOURCE_ACCESS_CPU
1270 | WINED3D_RESOURCE_ACCESS_MAP_R | WINED3D_RESOURCE_ACCESS_MAP_W;
1272 TRACE("iface %p, width %u, height %u, format %#x, surface %p.\n",
1273 iface, width, height, format, surface);
1275 *surface = NULL;
1277 return d3d8_device_create_surface(device, wined3dformat_from_d3dformat(format),
1278 WINED3D_MULTISAMPLE_NONE, 0, access, width, height, surface);
1281 static HRESULT WINAPI d3d8_device_CopyRects(IDirect3DDevice8 *iface,
1282 IDirect3DSurface8 *src_surface, const RECT *src_rects, UINT rect_count,
1283 IDirect3DSurface8 *dst_surface, const POINT *dst_points)
1285 struct d3d8_surface *src = unsafe_impl_from_IDirect3DSurface8(src_surface);
1286 struct d3d8_surface *dst = unsafe_impl_from_IDirect3DSurface8(dst_surface);
1287 enum wined3d_format_id src_format, dst_format;
1288 struct wined3d_sub_resource_desc wined3d_desc;
1289 UINT src_w, src_h;
1291 TRACE("iface %p, src_surface %p, src_rects %p, rect_count %u, dst_surface %p, dst_points %p.\n",
1292 iface, src_surface, src_rects, rect_count, dst_surface, dst_points);
1294 /* Check that the source texture is in WINED3D_POOL_SYSTEM_MEM and the
1295 * destination texture is in WINED3D_POOL_DEFAULT. */
1297 wined3d_mutex_lock();
1298 wined3d_texture_get_sub_resource_desc(src->wined3d_texture, src->sub_resource_idx, &wined3d_desc);
1299 if (wined3d_desc.bind_flags & WINED3D_BIND_DEPTH_STENCIL)
1301 WARN("Source %p is a depth stencil surface, returning D3DERR_INVALIDCALL.\n", src_surface);
1302 wined3d_mutex_unlock();
1303 return D3DERR_INVALIDCALL;
1305 src_format = wined3d_desc.format;
1306 src_w = wined3d_desc.width;
1307 src_h = wined3d_desc.height;
1309 wined3d_texture_get_sub_resource_desc(dst->wined3d_texture, dst->sub_resource_idx, &wined3d_desc);
1310 if (wined3d_desc.bind_flags & WINED3D_BIND_DEPTH_STENCIL)
1312 WARN("Destination %p is a depth stencil surface, returning D3DERR_INVALIDCALL.\n", dst_surface);
1313 wined3d_mutex_unlock();
1314 return D3DERR_INVALIDCALL;
1316 dst_format = wined3d_desc.format;
1318 /* Check that the source and destination formats match */
1319 if (src_format != dst_format)
1321 WARN("Source %p format must match the destination %p format, returning D3DERR_INVALIDCALL.\n",
1322 src_surface, dst_surface);
1323 wined3d_mutex_unlock();
1324 return D3DERR_INVALIDCALL;
1327 /* Quick if complete copy ... */
1328 if (!rect_count && !src_rects && !dst_points)
1330 RECT rect = {0, 0, src_w, src_h};
1331 wined3d_texture_blt(dst->wined3d_texture, dst->sub_resource_idx, &rect,
1332 src->wined3d_texture, src->sub_resource_idx, &rect, 0, NULL, WINED3D_TEXF_POINT);
1334 else
1336 unsigned int i;
1337 /* Copy rect by rect */
1338 if (src_rects && dst_points)
1340 for (i = 0; i < rect_count; ++i)
1342 UINT w = src_rects[i].right - src_rects[i].left;
1343 UINT h = src_rects[i].bottom - src_rects[i].top;
1344 RECT dst_rect = {dst_points[i].x, dst_points[i].y,
1345 dst_points[i].x + w, dst_points[i].y + h};
1347 wined3d_texture_blt(dst->wined3d_texture, dst->sub_resource_idx, &dst_rect,
1348 src->wined3d_texture, src->sub_resource_idx, &src_rects[i], 0, NULL, WINED3D_TEXF_POINT);
1351 else
1353 for (i = 0; i < rect_count; ++i)
1355 UINT w = src_rects[i].right - src_rects[i].left;
1356 UINT h = src_rects[i].bottom - src_rects[i].top;
1357 RECT dst_rect = {0, 0, w, h};
1359 wined3d_texture_blt(dst->wined3d_texture, dst->sub_resource_idx, &dst_rect,
1360 src->wined3d_texture, src->sub_resource_idx, &src_rects[i], 0, NULL, WINED3D_TEXF_POINT);
1364 wined3d_mutex_unlock();
1366 return WINED3D_OK;
1369 static HRESULT WINAPI d3d8_device_UpdateTexture(IDirect3DDevice8 *iface,
1370 IDirect3DBaseTexture8 *src_texture, IDirect3DBaseTexture8 *dst_texture)
1372 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1373 struct d3d8_texture *src_impl, *dst_impl;
1374 HRESULT hr;
1376 TRACE("iface %p, src_texture %p, dst_texture %p.\n", iface, src_texture, dst_texture);
1378 src_impl = unsafe_impl_from_IDirect3DBaseTexture8(src_texture);
1379 dst_impl = unsafe_impl_from_IDirect3DBaseTexture8(dst_texture);
1381 wined3d_mutex_lock();
1382 hr = wined3d_device_update_texture(device->wined3d_device,
1383 src_impl->wined3d_texture, dst_impl->wined3d_texture);
1384 wined3d_mutex_unlock();
1386 return hr;
1389 static HRESULT WINAPI d3d8_device_GetFrontBuffer(IDirect3DDevice8 *iface, IDirect3DSurface8 *dst_surface)
1391 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1392 struct d3d8_surface *dst_impl = unsafe_impl_from_IDirect3DSurface8(dst_surface);
1393 HRESULT hr;
1395 TRACE("iface %p, dst_surface %p.\n", iface, dst_surface);
1397 if (!dst_surface)
1399 WARN("Invalid destination surface passed.\n");
1400 return D3DERR_INVALIDCALL;
1403 wined3d_mutex_lock();
1404 hr = wined3d_swapchain_get_front_buffer_data(device->implicit_swapchain,
1405 dst_impl->wined3d_texture, dst_impl->sub_resource_idx);
1406 wined3d_mutex_unlock();
1408 return hr;
1411 static HRESULT WINAPI d3d8_device_SetRenderTarget(IDirect3DDevice8 *iface,
1412 IDirect3DSurface8 *render_target, IDirect3DSurface8 *depth_stencil)
1414 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1415 struct d3d8_surface *rt_impl = unsafe_impl_from_IDirect3DSurface8(render_target);
1416 struct d3d8_surface *ds_impl = unsafe_impl_from_IDirect3DSurface8(depth_stencil);
1417 struct wined3d_rendertarget_view *original_dsv, *rtv;
1418 HRESULT hr = D3D_OK;
1420 TRACE("iface %p, render_target %p, depth_stencil %p.\n", iface, render_target, depth_stencil);
1422 if (rt_impl && d3d8_surface_get_device(rt_impl) != device)
1424 WARN("Render target surface does not match device.\n");
1425 return D3DERR_INVALIDCALL;
1428 wined3d_mutex_lock();
1430 if (ds_impl)
1432 struct wined3d_sub_resource_desc ds_desc, rt_desc;
1433 struct wined3d_rendertarget_view *original_rtv;
1434 struct d3d8_surface *original_surface;
1436 /* If no render target is passed in check the size against the current RT */
1437 if (!render_target)
1439 if (!(original_rtv = wined3d_device_get_rendertarget_view(device->wined3d_device, 0)))
1441 wined3d_mutex_unlock();
1442 return D3DERR_NOTFOUND;
1444 original_surface = wined3d_rendertarget_view_get_sub_resource_parent(original_rtv);
1445 wined3d_texture_get_sub_resource_desc(original_surface->wined3d_texture,
1446 original_surface->sub_resource_idx, &rt_desc);
1448 else
1449 wined3d_texture_get_sub_resource_desc(rt_impl->wined3d_texture,
1450 rt_impl->sub_resource_idx, &rt_desc);
1452 wined3d_texture_get_sub_resource_desc(ds_impl->wined3d_texture, ds_impl->sub_resource_idx, &ds_desc);
1454 if (ds_desc.width < rt_desc.width || ds_desc.height < rt_desc.height)
1456 WARN("Depth stencil is smaller than the render target, returning D3DERR_INVALIDCALL\n");
1457 wined3d_mutex_unlock();
1458 return D3DERR_INVALIDCALL;
1460 if (ds_desc.multisample_type != rt_desc.multisample_type
1461 || ds_desc.multisample_quality != rt_desc.multisample_quality)
1463 WARN("Multisample settings do not match, returning D3DERR_INVALIDCALL\n");
1464 wined3d_mutex_unlock();
1465 return D3DERR_INVALIDCALL;
1469 original_dsv = wined3d_device_get_depth_stencil_view(device->wined3d_device);
1470 rtv = ds_impl ? d3d8_surface_acquire_rendertarget_view(ds_impl) : NULL;
1471 hr = wined3d_device_set_depth_stencil_view(device->wined3d_device, rtv);
1472 d3d8_surface_release_rendertarget_view(ds_impl, rtv);
1473 if (SUCCEEDED(hr))
1475 rtv = render_target ? d3d8_surface_acquire_rendertarget_view(rt_impl) : NULL;
1476 if (render_target)
1478 if (SUCCEEDED(hr = wined3d_device_set_rendertarget_view(device->wined3d_device, 0, rtv, TRUE)))
1479 device_reset_viewport_state(device);
1480 else
1481 wined3d_device_set_depth_stencil_view(device->wined3d_device, original_dsv);
1483 d3d8_surface_release_rendertarget_view(rt_impl, rtv);
1486 wined3d_mutex_unlock();
1488 return hr;
1491 static HRESULT WINAPI d3d8_device_GetRenderTarget(IDirect3DDevice8 *iface, IDirect3DSurface8 **render_target)
1493 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1494 struct wined3d_rendertarget_view *wined3d_rtv;
1495 struct d3d8_surface *surface_impl;
1496 HRESULT hr;
1498 TRACE("iface %p, render_target %p.\n", iface, render_target);
1500 if (!render_target)
1501 return D3DERR_INVALIDCALL;
1503 wined3d_mutex_lock();
1504 if ((wined3d_rtv = wined3d_device_get_rendertarget_view(device->wined3d_device, 0)))
1506 /* We want the sub resource parent here, since the view itself may be
1507 * internal to wined3d and may not have a parent. */
1508 surface_impl = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_rtv);
1509 *render_target = &surface_impl->IDirect3DSurface8_iface;
1510 IDirect3DSurface8_AddRef(*render_target);
1511 hr = D3D_OK;
1513 else
1515 ERR("Failed to get wined3d render target.\n");
1516 *render_target = NULL;
1517 hr = D3DERR_NOTFOUND;
1519 wined3d_mutex_unlock();
1521 return hr;
1524 static HRESULT WINAPI d3d8_device_GetDepthStencilSurface(IDirect3DDevice8 *iface, IDirect3DSurface8 **depth_stencil)
1526 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1527 struct wined3d_rendertarget_view *wined3d_dsv;
1528 struct d3d8_surface *surface_impl;
1529 HRESULT hr = D3D_OK;
1531 TRACE("iface %p, depth_stencil %p.\n", iface, depth_stencil);
1533 if (!depth_stencil)
1534 return D3DERR_INVALIDCALL;
1536 wined3d_mutex_lock();
1537 if ((wined3d_dsv = wined3d_device_get_depth_stencil_view(device->wined3d_device)))
1539 /* We want the sub resource parent here, since the view itself may be
1540 * internal to wined3d and may not have a parent. */
1541 surface_impl = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_dsv);
1542 *depth_stencil = &surface_impl->IDirect3DSurface8_iface;
1543 IDirect3DSurface8_AddRef(*depth_stencil);
1545 else
1547 hr = D3DERR_NOTFOUND;
1548 *depth_stencil = NULL;
1550 wined3d_mutex_unlock();
1552 return hr;
1555 static HRESULT WINAPI d3d8_device_BeginScene(IDirect3DDevice8 *iface)
1557 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1558 HRESULT hr;
1560 TRACE("iface %p.\n", iface);
1562 wined3d_mutex_lock();
1563 hr = wined3d_device_begin_scene(device->wined3d_device);
1564 wined3d_mutex_unlock();
1566 return hr;
1569 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d8_device_EndScene(IDirect3DDevice8 *iface)
1571 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1572 HRESULT hr;
1574 TRACE("iface %p.\n", iface);
1576 wined3d_mutex_lock();
1577 hr = wined3d_device_end_scene(device->wined3d_device);
1578 wined3d_mutex_unlock();
1580 return hr;
1583 static HRESULT WINAPI d3d8_device_Clear(IDirect3DDevice8 *iface, DWORD rect_count,
1584 const D3DRECT *rects, DWORD flags, D3DCOLOR color, float z, DWORD stencil)
1586 const struct wined3d_color c =
1588 ((color >> 16) & 0xff) / 255.0f,
1589 ((color >> 8) & 0xff) / 255.0f,
1590 (color & 0xff) / 255.0f,
1591 ((color >> 24) & 0xff) / 255.0f,
1593 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1594 HRESULT hr;
1596 TRACE("iface %p, rect_count %u, rects %p, flags %#x, color 0x%08x, z %.8e, stencil %u.\n",
1597 iface, rect_count, rects, flags, color, z, stencil);
1599 if (rect_count && !rects)
1601 WARN("count %u with NULL rects.\n", rect_count);
1602 rect_count = 0;
1605 wined3d_mutex_lock();
1606 wined3d_device_apply_stateblock(device->wined3d_device, device->state);
1607 hr = wined3d_device_clear(device->wined3d_device, rect_count, (const RECT *)rects, flags, &c, z, stencil);
1608 wined3d_mutex_unlock();
1610 return hr;
1613 static HRESULT WINAPI d3d8_device_SetTransform(IDirect3DDevice8 *iface,
1614 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
1616 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1618 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1620 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1621 wined3d_mutex_lock();
1622 wined3d_stateblock_set_transform(device->update_state, state, (const struct wined3d_matrix *)matrix);
1623 if (!device->recording)
1624 wined3d_device_set_transform(device->wined3d_device, state, (const struct wined3d_matrix *)matrix);
1625 wined3d_mutex_unlock();
1627 return D3D_OK;
1630 static HRESULT WINAPI d3d8_device_GetTransform(IDirect3DDevice8 *iface,
1631 D3DTRANSFORMSTATETYPE state, D3DMATRIX *matrix)
1633 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1635 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1637 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1638 wined3d_mutex_lock();
1639 wined3d_device_get_transform(device->wined3d_device, state, (struct wined3d_matrix *)matrix);
1640 wined3d_mutex_unlock();
1642 return D3D_OK;
1645 static HRESULT WINAPI d3d8_device_MultiplyTransform(IDirect3DDevice8 *iface,
1646 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
1648 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1650 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1652 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1653 wined3d_mutex_lock();
1654 wined3d_stateblock_multiply_transform(device->state, state, (const struct wined3d_matrix *)matrix);
1655 wined3d_device_multiply_transform(device->wined3d_device, state, (const struct wined3d_matrix *)matrix);
1656 wined3d_mutex_unlock();
1658 return D3D_OK;
1661 static HRESULT WINAPI d3d8_device_SetViewport(IDirect3DDevice8 *iface, const D3DVIEWPORT8 *viewport)
1663 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1664 struct wined3d_sub_resource_desc rt_desc;
1665 struct wined3d_rendertarget_view *rtv;
1666 struct d3d8_surface *surface;
1667 struct wined3d_viewport vp;
1669 TRACE("iface %p, viewport %p.\n", iface, viewport);
1671 wined3d_mutex_lock();
1672 if (!(rtv = wined3d_device_get_rendertarget_view(device->wined3d_device, 0)))
1674 wined3d_mutex_unlock();
1675 return D3DERR_NOTFOUND;
1677 surface = wined3d_rendertarget_view_get_sub_resource_parent(rtv);
1678 wined3d_texture_get_sub_resource_desc(surface->wined3d_texture, surface->sub_resource_idx, &rt_desc);
1680 if (viewport->X > rt_desc.width || viewport->Width > rt_desc.width - viewport->X
1681 || viewport->Y > rt_desc.height || viewport->Height > rt_desc.height - viewport->Y)
1683 WARN("Invalid viewport, returning D3DERR_INVALIDCALL.\n");
1684 wined3d_mutex_unlock();
1685 return D3DERR_INVALIDCALL;
1688 vp.x = viewport->X;
1689 vp.y = viewport->Y;
1690 vp.width = viewport->Width;
1691 vp.height = viewport->Height;
1692 vp.min_z = viewport->MinZ;
1693 vp.max_z = viewport->MaxZ;
1695 wined3d_stateblock_set_viewport(device->update_state, &vp);
1696 if (!device->recording)
1697 wined3d_device_set_viewports(device->wined3d_device, 1, &vp);
1698 wined3d_mutex_unlock();
1700 return D3D_OK;
1703 static HRESULT WINAPI d3d8_device_GetViewport(IDirect3DDevice8 *iface, D3DVIEWPORT8 *viewport)
1705 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1706 struct wined3d_viewport wined3d_viewport;
1708 TRACE("iface %p, viewport %p.\n", iface, viewport);
1710 wined3d_mutex_lock();
1711 wined3d_device_get_viewports(device->wined3d_device, NULL, &wined3d_viewport);
1712 wined3d_mutex_unlock();
1714 viewport->X = wined3d_viewport.x;
1715 viewport->Y = wined3d_viewport.y;
1716 viewport->Width = wined3d_viewport.width;
1717 viewport->Height = wined3d_viewport.height;
1718 viewport->MinZ = wined3d_viewport.min_z;
1719 viewport->MaxZ = wined3d_viewport.max_z;
1721 return D3D_OK;
1724 static HRESULT WINAPI d3d8_device_SetMaterial(IDirect3DDevice8 *iface, const D3DMATERIAL8 *material)
1726 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1728 TRACE("iface %p, material %p.\n", iface, material);
1730 /* Note: D3DMATERIAL8 is compatible with struct wined3d_material. */
1731 wined3d_mutex_lock();
1732 wined3d_stateblock_set_material(device->update_state, (const struct wined3d_material *)material);
1733 if (!device->recording)
1734 wined3d_device_set_material(device->wined3d_device, (const struct wined3d_material *)material);
1735 wined3d_mutex_unlock();
1737 return D3D_OK;
1740 static HRESULT WINAPI d3d8_device_GetMaterial(IDirect3DDevice8 *iface, D3DMATERIAL8 *material)
1742 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1744 TRACE("iface %p, material %p.\n", iface, material);
1746 /* Note: D3DMATERIAL8 is compatible with struct wined3d_material. */
1747 wined3d_mutex_lock();
1748 wined3d_device_get_material(device->wined3d_device, (struct wined3d_material *)material);
1749 wined3d_mutex_unlock();
1751 return D3D_OK;
1754 static HRESULT WINAPI d3d8_device_SetLight(IDirect3DDevice8 *iface, DWORD index, const D3DLIGHT8 *light)
1756 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1757 HRESULT hr;
1759 TRACE("iface %p, index %u, light %p.\n", iface, index, light);
1761 /* Note: D3DLIGHT8 is compatible with struct wined3d_light. */
1762 wined3d_mutex_lock();
1763 hr = wined3d_stateblock_set_light(device->update_state, index, (const struct wined3d_light *)light);
1764 if (SUCCEEDED(hr) && !device->recording)
1765 hr = wined3d_device_set_light(device->wined3d_device, index, (const struct wined3d_light *)light);
1766 wined3d_mutex_unlock();
1768 return hr;
1771 static HRESULT WINAPI d3d8_device_GetLight(IDirect3DDevice8 *iface, DWORD index, D3DLIGHT8 *light)
1773 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1774 HRESULT hr;
1776 TRACE("iface %p, index %u, light %p.\n", iface, index, light);
1778 /* Note: D3DLIGHT8 is compatible with struct wined3d_light. */
1779 wined3d_mutex_lock();
1780 hr = wined3d_device_get_light(device->wined3d_device, index, (struct wined3d_light *)light);
1781 wined3d_mutex_unlock();
1783 return hr;
1786 static HRESULT WINAPI d3d8_device_LightEnable(IDirect3DDevice8 *iface, DWORD index, BOOL enable)
1788 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1789 HRESULT hr;
1791 TRACE("iface %p, index %u, enable %#x.\n", iface, index, enable);
1793 wined3d_mutex_lock();
1794 hr = wined3d_stateblock_set_light_enable(device->update_state, index, enable);
1795 if (SUCCEEDED(hr) && !device->recording)
1796 hr = wined3d_device_set_light_enable(device->wined3d_device, index, enable);
1797 wined3d_mutex_unlock();
1799 return hr;
1802 static HRESULT WINAPI d3d8_device_GetLightEnable(IDirect3DDevice8 *iface, DWORD index, BOOL *enable)
1804 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1805 HRESULT hr;
1807 TRACE("iface %p, index %u, enable %p.\n", iface, index, enable);
1809 wined3d_mutex_lock();
1810 hr = wined3d_device_get_light_enable(device->wined3d_device, index, enable);
1811 wined3d_mutex_unlock();
1813 return hr;
1816 static HRESULT WINAPI d3d8_device_SetClipPlane(IDirect3DDevice8 *iface, DWORD index, const float *plane)
1818 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1819 HRESULT hr;
1821 TRACE("iface %p, index %u, plane %p.\n", iface, index, plane);
1823 wined3d_mutex_lock();
1824 hr = wined3d_stateblock_set_clip_plane(device->update_state, index, (const struct wined3d_vec4 *)plane);
1825 if (SUCCEEDED(hr) && !device->recording)
1826 hr = wined3d_device_set_clip_plane(device->wined3d_device, index, (const struct wined3d_vec4 *)plane);
1827 wined3d_mutex_unlock();
1829 return hr;
1832 static HRESULT WINAPI d3d8_device_GetClipPlane(IDirect3DDevice8 *iface, DWORD index, float *plane)
1834 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1835 HRESULT hr;
1837 TRACE("iface %p, index %u, plane %p.\n", iface, index, plane);
1839 wined3d_mutex_lock();
1840 hr = wined3d_device_get_clip_plane(device->wined3d_device, index, (struct wined3d_vec4 *)plane);
1841 wined3d_mutex_unlock();
1843 return hr;
1846 static void resolve_depth_buffer(struct d3d8_device *device)
1848 const struct wined3d_stateblock_state *state = wined3d_stateblock_get_state(device->state);
1849 struct wined3d_rendertarget_view *wined3d_dsv;
1850 struct wined3d_resource *dst_resource;
1851 struct wined3d_texture *dst_texture;
1852 struct wined3d_resource_desc desc;
1853 struct d3d8_surface *d3d8_dsv;
1855 if (!(dst_texture = state->textures[0]))
1856 return;
1857 dst_resource = wined3d_texture_get_resource(dst_texture);
1858 wined3d_resource_get_desc(dst_resource, &desc);
1859 if (desc.format != WINED3DFMT_D24_UNORM_S8_UINT
1860 && desc.format != WINED3DFMT_X8D24_UNORM
1861 && desc.format != WINED3DFMT_DF16
1862 && desc.format != WINED3DFMT_DF24
1863 && desc.format != WINED3DFMT_INTZ)
1864 return;
1866 if (!(wined3d_dsv = wined3d_device_get_depth_stencil_view(device->wined3d_device)))
1867 return;
1868 d3d8_dsv = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_dsv);
1870 wined3d_device_resolve_sub_resource(device->wined3d_device, dst_resource, 0,
1871 wined3d_rendertarget_view_get_resource(wined3d_dsv), d3d8_dsv->sub_resource_idx, desc.format);
1874 static HRESULT WINAPI d3d8_device_SetRenderState(IDirect3DDevice8 *iface,
1875 D3DRENDERSTATETYPE state, DWORD value)
1877 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1879 TRACE("iface %p, state %#x, value %#x.\n", iface, state, value);
1881 wined3d_mutex_lock();
1882 if (state == D3DRS_ZBIAS)
1883 state = WINED3D_RS_DEPTHBIAS;
1884 wined3d_stateblock_set_render_state(device->update_state, state, value);
1885 if (state == D3DRS_POINTSIZE && value == D3D8_RESZ_CODE)
1886 resolve_depth_buffer(device);
1887 wined3d_mutex_unlock();
1889 return D3D_OK;
1892 static HRESULT WINAPI d3d8_device_GetRenderState(IDirect3DDevice8 *iface,
1893 D3DRENDERSTATETYPE state, DWORD *value)
1895 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1896 const struct wined3d_stateblock_state *device_state;
1898 TRACE("iface %p, state %#x, value %p.\n", iface, state, value);
1900 wined3d_mutex_lock();
1901 device_state = wined3d_stateblock_get_state(device->state);
1902 switch (state)
1904 case D3DRS_ZBIAS:
1905 *value = device_state->rs[WINED3D_RS_DEPTHBIAS];
1906 break;
1908 default:
1909 *value = device_state->rs[state];
1911 wined3d_mutex_unlock();
1913 return D3D_OK;
1916 static HRESULT WINAPI d3d8_device_BeginStateBlock(IDirect3DDevice8 *iface)
1918 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1919 struct wined3d_stateblock *stateblock;
1920 HRESULT hr;
1922 TRACE("iface %p.\n", iface);
1924 wined3d_mutex_lock();
1925 if (device->recording)
1927 wined3d_mutex_unlock();
1928 WARN("Trying to begin a stateblock while recording, returning D3DERR_INVALIDCALL.\n");
1929 return D3DERR_INVALIDCALL;
1932 if (SUCCEEDED(hr = wined3d_stateblock_create(device->wined3d_device, NULL, WINED3D_SBT_RECORDED, &stateblock)))
1933 device->update_state = device->recording = stateblock;
1934 wined3d_mutex_unlock();
1936 return hr;
1939 static HRESULT WINAPI d3d8_device_EndStateBlock(IDirect3DDevice8 *iface, DWORD *token)
1941 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1942 struct wined3d_stateblock *stateblock;
1944 TRACE("iface %p, token %p.\n", iface, token);
1946 /* Tell wineD3D to endstateblock before anything else (in case we run out
1947 * of memory later and cause locking problems)
1949 wined3d_mutex_lock();
1950 if (!device->recording)
1952 wined3d_mutex_unlock();
1953 WARN("Trying to end a stateblock, but no stateblock is being recorded.\n");
1954 return D3DERR_INVALIDCALL;
1956 stateblock = device->recording;
1957 wined3d_stateblock_init_contained_states(stateblock);
1958 device->recording = NULL;
1959 device->update_state = device->state;
1961 *token = d3d8_allocate_handle(&device->handle_table, stateblock, D3D8_HANDLE_SB);
1962 wined3d_mutex_unlock();
1964 if (*token == D3D8_INVALID_HANDLE)
1966 ERR("Failed to create a handle\n");
1967 wined3d_mutex_lock();
1968 wined3d_stateblock_decref(stateblock);
1969 wined3d_mutex_unlock();
1970 return E_FAIL;
1972 ++*token;
1974 TRACE("Returning %#x (%p).\n", *token, stateblock);
1976 return D3D_OK;
1979 static HRESULT WINAPI d3d8_device_ApplyStateBlock(IDirect3DDevice8 *iface, DWORD token)
1981 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1982 struct wined3d_stateblock *stateblock;
1983 struct wined3d_buffer *wined3d_buffer;
1984 struct d3d8_vertexbuffer *buffer;
1985 unsigned int i, offset, stride;
1986 enum wined3d_format_id format;
1987 HRESULT hr;
1989 TRACE("iface %p, token %#x.\n", iface, token);
1991 if (!token)
1992 return D3D_OK;
1994 wined3d_mutex_lock();
1995 if (device->recording)
1997 wined3d_mutex_unlock();
1998 WARN("Trying to apply stateblock while recording, returning D3DERR_INVALIDCALL.\n");
1999 return D3DERR_INVALIDCALL;
2001 stateblock = d3d8_get_object(&device->handle_table, token - 1, D3D8_HANDLE_SB);
2002 if (!stateblock)
2004 WARN("Invalid handle (%#x) passed.\n", token);
2005 wined3d_mutex_unlock();
2006 return D3DERR_INVALIDCALL;
2008 wined3d_stateblock_apply(stateblock, device->state);
2009 device->sysmem_vb = 0;
2010 for (i = 0; i < D3D8_MAX_STREAMS; ++i)
2012 if (FAILED(hr = wined3d_device_get_stream_source(device->wined3d_device,
2013 i, &wined3d_buffer, &offset, &stride)))
2014 continue;
2015 if (!wined3d_buffer || !(buffer = wined3d_buffer_get_parent(wined3d_buffer)))
2016 continue;
2017 if (buffer->draw_buffer)
2018 device->sysmem_vb |= 1u << i;
2020 device->sysmem_ib = (wined3d_buffer = wined3d_device_get_index_buffer(device->wined3d_device, &format, &offset))
2021 && (buffer = wined3d_buffer_get_parent(wined3d_buffer)) && buffer->draw_buffer;
2022 wined3d_mutex_unlock();
2024 return D3D_OK;
2027 static HRESULT WINAPI d3d8_device_CaptureStateBlock(IDirect3DDevice8 *iface, DWORD token)
2029 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2030 struct wined3d_stateblock *stateblock;
2032 TRACE("iface %p, token %#x.\n", iface, token);
2034 wined3d_mutex_lock();
2035 if (device->recording)
2037 wined3d_mutex_unlock();
2038 WARN("Trying to capture stateblock while recording, returning D3DERR_INVALIDCALL.\n");
2039 return D3DERR_INVALIDCALL;
2041 stateblock = d3d8_get_object(&device->handle_table, token - 1, D3D8_HANDLE_SB);
2042 if (!stateblock)
2044 WARN("Invalid handle (%#x) passed.\n", token);
2045 wined3d_mutex_unlock();
2046 return D3DERR_INVALIDCALL;
2048 wined3d_stateblock_capture(stateblock, device->state);
2049 wined3d_mutex_unlock();
2051 return D3D_OK;
2054 static HRESULT WINAPI d3d8_device_DeleteStateBlock(IDirect3DDevice8 *iface, DWORD token)
2056 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2057 struct wined3d_stateblock *stateblock;
2059 TRACE("iface %p, token %#x.\n", iface, token);
2061 wined3d_mutex_lock();
2062 stateblock = d3d8_free_handle(&device->handle_table, token - 1, D3D8_HANDLE_SB);
2064 if (!stateblock)
2066 WARN("Invalid handle (%#x) passed.\n", token);
2067 wined3d_mutex_unlock();
2068 return D3DERR_INVALIDCALL;
2071 if (wined3d_stateblock_decref(stateblock))
2073 ERR("Stateblock %p has references left, this shouldn't happen.\n", stateblock);
2075 wined3d_mutex_unlock();
2077 return D3D_OK;
2080 static HRESULT WINAPI d3d8_device_CreateStateBlock(IDirect3DDevice8 *iface,
2081 D3DSTATEBLOCKTYPE type, DWORD *handle)
2083 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2084 struct wined3d_stateblock *stateblock;
2085 HRESULT hr;
2087 TRACE("iface %p, type %#x, handle %p.\n", iface, type, handle);
2089 if (type != D3DSBT_ALL
2090 && type != D3DSBT_PIXELSTATE
2091 && type != D3DSBT_VERTEXSTATE)
2093 WARN("Unexpected stateblock type, returning D3DERR_INVALIDCALL\n");
2094 return D3DERR_INVALIDCALL;
2097 wined3d_mutex_lock();
2098 if (device->recording)
2100 wined3d_mutex_unlock();
2101 WARN("Trying to create a stateblock while recording, returning D3DERR_INVALIDCALL.\n");
2102 return D3DERR_INVALIDCALL;
2104 hr = wined3d_stateblock_create(device->wined3d_device, device->state, (enum wined3d_stateblock_type)type, &stateblock);
2105 if (FAILED(hr))
2107 wined3d_mutex_unlock();
2108 ERR("Failed to create the state block, hr %#x\n", hr);
2109 return hr;
2112 *handle = d3d8_allocate_handle(&device->handle_table, stateblock, D3D8_HANDLE_SB);
2113 wined3d_mutex_unlock();
2115 if (*handle == D3D8_INVALID_HANDLE)
2117 ERR("Failed to allocate a handle.\n");
2118 wined3d_mutex_lock();
2119 wined3d_stateblock_decref(stateblock);
2120 wined3d_mutex_unlock();
2121 return E_FAIL;
2123 ++*handle;
2125 TRACE("Returning %#x (%p).\n", *handle, stateblock);
2127 return hr;
2130 static HRESULT WINAPI d3d8_device_SetClipStatus(IDirect3DDevice8 *iface, const D3DCLIPSTATUS8 *clip_status)
2132 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2133 HRESULT hr;
2135 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
2136 /* FIXME: Verify that D3DCLIPSTATUS8 ~= struct wined3d_clip_status. */
2138 wined3d_mutex_lock();
2139 hr = wined3d_device_set_clip_status(device->wined3d_device, (const struct wined3d_clip_status *)clip_status);
2140 wined3d_mutex_unlock();
2142 return hr;
2145 static HRESULT WINAPI d3d8_device_GetClipStatus(IDirect3DDevice8 *iface, D3DCLIPSTATUS8 *clip_status)
2147 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2148 HRESULT hr;
2150 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
2152 wined3d_mutex_lock();
2153 hr = wined3d_device_get_clip_status(device->wined3d_device, (struct wined3d_clip_status *)clip_status);
2154 wined3d_mutex_unlock();
2156 return hr;
2159 static HRESULT WINAPI d3d8_device_GetTexture(IDirect3DDevice8 *iface, DWORD stage, IDirect3DBaseTexture8 **texture)
2161 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2162 struct wined3d_texture *wined3d_texture;
2163 struct d3d8_texture *texture_impl;
2165 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
2167 if (!texture)
2168 return D3DERR_INVALIDCALL;
2170 wined3d_mutex_lock();
2171 if ((wined3d_texture = wined3d_device_get_texture(device->wined3d_device, stage)))
2173 texture_impl = wined3d_texture_get_parent(wined3d_texture);
2174 *texture = &texture_impl->IDirect3DBaseTexture8_iface;
2175 IDirect3DBaseTexture8_AddRef(*texture);
2177 else
2179 *texture = NULL;
2181 wined3d_mutex_unlock();
2183 return D3D_OK;
2186 static HRESULT WINAPI d3d8_device_SetTexture(IDirect3DDevice8 *iface, DWORD stage, IDirect3DBaseTexture8 *texture)
2188 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2189 struct d3d8_texture *texture_impl;
2191 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
2193 texture_impl = unsafe_impl_from_IDirect3DBaseTexture8(texture);
2195 wined3d_mutex_lock();
2196 wined3d_stateblock_set_texture(device->update_state, stage,
2197 texture_impl ? texture_impl->wined3d_texture : NULL);
2198 if (!device->recording)
2199 wined3d_device_set_texture(device->wined3d_device, stage,
2200 texture_impl ? texture_impl->wined3d_texture : NULL);
2201 wined3d_mutex_unlock();
2203 return D3D_OK;
2206 static const struct tss_lookup
2208 BOOL sampler_state;
2209 union
2211 enum wined3d_texture_stage_state texture_state;
2212 enum wined3d_sampler_state sampler_state;
2213 } u;
2215 tss_lookup[] =
2217 {FALSE, {WINED3D_TSS_INVALID}}, /* 0, unused */
2218 {FALSE, {WINED3D_TSS_COLOR_OP}}, /* 1, D3DTSS_COLOROP */
2219 {FALSE, {WINED3D_TSS_COLOR_ARG1}}, /* 2, D3DTSS_COLORARG1 */
2220 {FALSE, {WINED3D_TSS_COLOR_ARG2}}, /* 3, D3DTSS_COLORARG2 */
2221 {FALSE, {WINED3D_TSS_ALPHA_OP}}, /* 4, D3DTSS_ALPHAOP */
2222 {FALSE, {WINED3D_TSS_ALPHA_ARG1}}, /* 5, D3DTSS_ALPHAARG1 */
2223 {FALSE, {WINED3D_TSS_ALPHA_ARG2}}, /* 6, D3DTSS_ALPHAARG2 */
2224 {FALSE, {WINED3D_TSS_BUMPENV_MAT00}}, /* 7, D3DTSS_BUMPENVMAT00 */
2225 {FALSE, {WINED3D_TSS_BUMPENV_MAT01}}, /* 8, D3DTSS_BUMPENVMAT01 */
2226 {FALSE, {WINED3D_TSS_BUMPENV_MAT10}}, /* 9, D3DTSS_BUMPENVMAT10 */
2227 {FALSE, {WINED3D_TSS_BUMPENV_MAT11}}, /* 10, D3DTSS_BUMPENVMAT11 */
2228 {FALSE, {WINED3D_TSS_TEXCOORD_INDEX}}, /* 11, D3DTSS_TEXCOORDINDEX */
2229 {FALSE, {WINED3D_TSS_INVALID}}, /* 12, unused */
2230 {TRUE, {WINED3D_SAMP_ADDRESS_U}}, /* 13, D3DTSS_ADDRESSU */
2231 {TRUE, {WINED3D_SAMP_ADDRESS_V}}, /* 14, D3DTSS_ADDRESSV */
2232 {TRUE, {WINED3D_SAMP_BORDER_COLOR}}, /* 15, D3DTSS_BORDERCOLOR */
2233 {TRUE, {WINED3D_SAMP_MAG_FILTER}}, /* 16, D3DTSS_MAGFILTER */
2234 {TRUE, {WINED3D_SAMP_MIN_FILTER}}, /* 17, D3DTSS_MINFILTER */
2235 {TRUE, {WINED3D_SAMP_MIP_FILTER}}, /* 18, D3DTSS_MIPFILTER */
2236 {TRUE, {WINED3D_SAMP_MIPMAP_LOD_BIAS}}, /* 19, D3DTSS_MIPMAPLODBIAS */
2237 {TRUE, {WINED3D_SAMP_MAX_MIP_LEVEL}}, /* 20, D3DTSS_MAXMIPLEVEL */
2238 {TRUE, {WINED3D_SAMP_MAX_ANISOTROPY}}, /* 21, D3DTSS_MAXANISOTROPY */
2239 {FALSE, {WINED3D_TSS_BUMPENV_LSCALE}}, /* 22, D3DTSS_BUMPENVLSCALE */
2240 {FALSE, {WINED3D_TSS_BUMPENV_LOFFSET}}, /* 23, D3DTSS_BUMPENVLOFFSET */
2241 {FALSE, {WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS}}, /* 24, D3DTSS_TEXTURETRANSFORMFLAGS */
2242 {TRUE, {WINED3D_SAMP_ADDRESS_W}}, /* 25, D3DTSS_ADDRESSW */
2243 {FALSE, {WINED3D_TSS_COLOR_ARG0}}, /* 26, D3DTSS_COLORARG0 */
2244 {FALSE, {WINED3D_TSS_ALPHA_ARG0}}, /* 27, D3DTSS_ALPHAARG0 */
2245 {FALSE, {WINED3D_TSS_RESULT_ARG}}, /* 28, D3DTSS_RESULTARG */
2248 static HRESULT WINAPI d3d8_device_GetTextureStageState(IDirect3DDevice8 *iface,
2249 DWORD stage, D3DTEXTURESTAGESTATETYPE Type, DWORD *value)
2251 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2252 const struct tss_lookup *l;
2254 TRACE("iface %p, stage %u, state %#x, value %p.\n", iface, stage, Type, value);
2256 if (Type >= ARRAY_SIZE(tss_lookup))
2258 WARN("Invalid Type %#x passed.\n", Type);
2259 return D3D_OK;
2262 l = &tss_lookup[Type];
2264 wined3d_mutex_lock();
2265 if (l->sampler_state)
2266 *value = wined3d_device_get_sampler_state(device->wined3d_device, stage, l->u.sampler_state);
2267 else
2268 *value = wined3d_device_get_texture_stage_state(device->wined3d_device, stage, l->u.texture_state);
2269 wined3d_mutex_unlock();
2271 return D3D_OK;
2274 static HRESULT WINAPI d3d8_device_SetTextureStageState(IDirect3DDevice8 *iface,
2275 DWORD stage, D3DTEXTURESTAGESTATETYPE type, DWORD value)
2277 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2278 const struct tss_lookup *l;
2280 TRACE("iface %p, stage %u, state %#x, value %#x.\n", iface, stage, type, value);
2282 if (type >= ARRAY_SIZE(tss_lookup))
2284 WARN("Invalid type %#x passed.\n", type);
2285 return D3D_OK;
2288 l = &tss_lookup[type];
2290 wined3d_mutex_lock();
2291 if (l->sampler_state)
2293 wined3d_stateblock_set_sampler_state(device->update_state, stage, l->u.sampler_state, value);
2294 if (!device->recording)
2295 wined3d_device_set_sampler_state(device->wined3d_device, stage, l->u.sampler_state, value);
2297 else
2299 wined3d_stateblock_set_texture_stage_state(device->update_state, stage, l->u.texture_state, value);
2300 if (!device->recording)
2301 wined3d_device_set_texture_stage_state(device->wined3d_device, stage, l->u.texture_state, value);
2303 wined3d_mutex_unlock();
2305 return D3D_OK;
2308 static HRESULT WINAPI d3d8_device_ValidateDevice(IDirect3DDevice8 *iface, DWORD *pass_count)
2310 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2311 HRESULT hr;
2313 TRACE("iface %p, pass_count %p.\n", iface, pass_count);
2315 wined3d_mutex_lock();
2316 wined3d_device_apply_stateblock(device->wined3d_device, device->state);
2317 hr = wined3d_device_validate_device(device->wined3d_device, pass_count);
2318 wined3d_mutex_unlock();
2320 return hr;
2323 static HRESULT WINAPI d3d8_device_GetInfo(IDirect3DDevice8 *iface,
2324 DWORD info_id, void *info, DWORD info_size)
2326 TRACE("iface %p, info_id %#x, info %p, info_size %u.\n", iface, info_id, info, info_size);
2328 if (info_id < 4)
2329 return E_FAIL;
2330 return S_FALSE;
2333 static HRESULT WINAPI d3d8_device_SetPaletteEntries(IDirect3DDevice8 *iface,
2334 UINT palette_idx, const PALETTEENTRY *entries)
2336 WARN("iface %p, palette_idx %u, entries %p unimplemented\n", iface, palette_idx, entries);
2338 /* GPUs stopped supporting palettized textures with the Shader Model 1 generation. Wined3d
2339 * does not have a d3d8/9-style palette API */
2341 return D3D_OK;
2344 static HRESULT WINAPI d3d8_device_GetPaletteEntries(IDirect3DDevice8 *iface,
2345 UINT palette_idx, PALETTEENTRY *entries)
2347 FIXME("iface %p, palette_idx %u, entries %p unimplemented.\n", iface, palette_idx, entries);
2349 return D3DERR_INVALIDCALL;
2352 static HRESULT WINAPI d3d8_device_SetCurrentTexturePalette(IDirect3DDevice8 *iface, UINT palette_idx)
2354 WARN("iface %p, palette_idx %u unimplemented.\n", iface, palette_idx);
2356 return D3D_OK;
2359 static HRESULT WINAPI d3d8_device_GetCurrentTexturePalette(IDirect3DDevice8 *iface, UINT *palette_idx)
2361 FIXME("iface %p, palette_idx %p unimplemented.\n", iface, palette_idx);
2363 return D3DERR_INVALIDCALL;
2366 static void d3d8_device_upload_sysmem_vertex_buffers(struct d3d8_device *device,
2367 unsigned int start_vertex, unsigned int vertex_count)
2369 struct wined3d_vertex_declaration *wined3d_decl;
2370 struct wined3d_box box = {0, 0, 0, 1, 0, 1};
2371 struct d3d8_vertexbuffer *d3d8_buffer;
2372 struct wined3d_resource *dst_resource;
2373 struct d3d8_vertex_declaration *decl;
2374 unsigned int i, offset, stride, map;
2375 struct wined3d_buffer *dst_buffer;
2376 struct wined3d_resource_desc desc;
2377 HRESULT hr;
2379 if (!device->sysmem_vb)
2380 return;
2382 wined3d_decl = wined3d_device_get_vertex_declaration(device->wined3d_device);
2383 if (!wined3d_decl)
2384 return;
2386 decl = wined3d_vertex_declaration_get_parent(wined3d_decl);
2387 map = decl->stream_map & device->sysmem_vb;
2388 while (map)
2390 i = wined3d_bit_scan(&map);
2392 if (FAILED(hr = wined3d_device_get_stream_source(device->wined3d_device, i, &dst_buffer, &offset, &stride)))
2393 ERR("Failed to get stream source.\n");
2394 d3d8_buffer = wined3d_buffer_get_parent(dst_buffer);
2395 dst_resource = wined3d_buffer_get_resource(dst_buffer);
2396 wined3d_resource_get_desc(dst_resource, &desc);
2397 box.left = offset + start_vertex * stride;
2398 box.right = min(box.left + vertex_count * stride, desc.size);
2399 if (FAILED(hr = wined3d_device_copy_sub_resource_region(device->wined3d_device,
2400 dst_resource, 0, box.left, 0, 0,
2401 wined3d_buffer_get_resource(d3d8_buffer->wined3d_buffer), 0, &box, 0)))
2402 ERR("Failed to update buffer.\n");
2406 static void d3d8_device_upload_sysmem_index_buffer(struct d3d8_device *device,
2407 unsigned int start_idx, unsigned int idx_count)
2409 struct wined3d_box box = {0, 0, 0, 1, 0, 1};
2410 struct wined3d_resource *dst_resource;
2411 struct d3d8_indexbuffer *d3d8_buffer;
2412 struct wined3d_buffer *dst_buffer;
2413 struct wined3d_resource_desc desc;
2414 enum wined3d_format_id format;
2415 unsigned int offset, idx_size;
2416 HRESULT hr;
2418 if (!device->sysmem_ib)
2419 return;
2421 if (!(dst_buffer = wined3d_device_get_index_buffer(device->wined3d_device, &format, &offset)))
2422 ERR("Failed to get index buffer.\n");
2423 d3d8_buffer = wined3d_buffer_get_parent(dst_buffer);
2424 dst_resource = wined3d_buffer_get_resource(dst_buffer);
2425 wined3d_resource_get_desc(dst_resource, &desc);
2426 idx_size = format == WINED3DFMT_R16_UINT ? 2 : 4;
2427 box.left = offset + start_idx * idx_size;
2428 box.right = min(box.left + idx_count * idx_size, desc.size);
2429 if (FAILED(hr = wined3d_device_copy_sub_resource_region(device->wined3d_device,
2430 dst_resource, 0, box.left, 0, 0,
2431 wined3d_buffer_get_resource(d3d8_buffer->wined3d_buffer), 0, &box, 0)))
2432 ERR("Failed to update buffer.\n");
2435 static HRESULT WINAPI d3d8_device_DrawPrimitive(IDirect3DDevice8 *iface,
2436 D3DPRIMITIVETYPE primitive_type, UINT start_vertex, UINT primitive_count)
2438 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2439 unsigned int vertex_count;
2440 HRESULT hr;
2442 TRACE("iface %p, primitive_type %#x, start_vertex %u, primitive_count %u.\n",
2443 iface, primitive_type, start_vertex, primitive_count);
2445 vertex_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2446 wined3d_mutex_lock();
2447 wined3d_device_apply_stateblock(device->wined3d_device, device->state);
2448 d3d8_device_upload_sysmem_vertex_buffers(device, start_vertex, vertex_count);
2449 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type, 0);
2450 hr = wined3d_device_draw_primitive(device->wined3d_device, start_vertex, vertex_count);
2451 wined3d_mutex_unlock();
2453 return hr;
2456 static HRESULT WINAPI d3d8_device_DrawIndexedPrimitive(IDirect3DDevice8 *iface,
2457 D3DPRIMITIVETYPE primitive_type, UINT min_vertex_idx, UINT vertex_count,
2458 UINT start_idx, UINT primitive_count)
2460 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2461 unsigned int index_count;
2462 int base_vertex_index;
2463 HRESULT hr;
2465 TRACE("iface %p, primitive_type %#x, min_vertex_idx %u, vertex_count %u, start_idx %u, primitive_count %u.\n",
2466 iface, primitive_type, min_vertex_idx, vertex_count, start_idx, primitive_count);
2468 index_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2469 wined3d_mutex_lock();
2470 wined3d_device_apply_stateblock(device->wined3d_device, device->state);
2471 base_vertex_index = wined3d_device_get_base_vertex_index(device->wined3d_device);
2472 d3d8_device_upload_sysmem_vertex_buffers(device, base_vertex_index + min_vertex_idx, vertex_count);
2473 d3d8_device_upload_sysmem_index_buffer(device, start_idx, index_count);
2474 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type, 0);
2475 hr = wined3d_device_draw_indexed_primitive(device->wined3d_device, start_idx, index_count);
2476 wined3d_mutex_unlock();
2478 return hr;
2481 /* The caller is responsible for wined3d locking */
2482 static HRESULT d3d8_device_prepare_vertex_buffer(struct d3d8_device *device, UINT min_size)
2484 HRESULT hr;
2486 if (device->vertex_buffer_size < min_size || !device->vertex_buffer)
2488 UINT size = max(device->vertex_buffer_size * 2, min_size);
2489 struct wined3d_buffer_desc desc;
2490 struct wined3d_buffer *buffer;
2492 TRACE("Growing vertex buffer to %u bytes\n", size);
2494 desc.byte_width = size;
2495 desc.usage = WINED3DUSAGE_DYNAMIC;
2496 desc.bind_flags = WINED3D_BIND_VERTEX_BUFFER;
2497 desc.access = WINED3D_RESOURCE_ACCESS_GPU | WINED3D_RESOURCE_ACCESS_MAP_W;
2498 desc.misc_flags = 0;
2499 desc.structure_byte_stride = 0;
2501 if (FAILED(hr = wined3d_buffer_create(device->wined3d_device, &desc,
2502 NULL, NULL, &d3d8_null_wined3d_parent_ops, &buffer)))
2504 ERR("Failed to create vertex buffer, hr %#x.\n", hr);
2505 return hr;
2508 if (device->vertex_buffer)
2509 wined3d_buffer_decref(device->vertex_buffer);
2511 device->vertex_buffer = buffer;
2512 device->vertex_buffer_size = size;
2513 device->vertex_buffer_pos = 0;
2515 return D3D_OK;
2518 static HRESULT WINAPI d3d8_device_DrawPrimitiveUP(IDirect3DDevice8 *iface,
2519 D3DPRIMITIVETYPE primitive_type, UINT primitive_count, const void *data,
2520 UINT stride)
2522 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2523 HRESULT hr;
2524 UINT vtx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2525 struct wined3d_map_desc wined3d_map_desc;
2526 struct wined3d_box wined3d_box = {0};
2527 UINT size = vtx_count * stride;
2528 struct wined3d_resource *vb;
2529 UINT vb_pos, align;
2531 TRACE("iface %p, primitive_type %#x, primitive_count %u, data %p, stride %u.\n",
2532 iface, primitive_type, primitive_count, data, stride);
2534 if (!primitive_count || !stride)
2536 WARN("primitive_count or stride is 0, returning D3D_OK.\n");
2537 return D3D_OK;
2540 wined3d_mutex_lock();
2541 wined3d_device_apply_stateblock(device->wined3d_device, device->state);
2542 hr = d3d8_device_prepare_vertex_buffer(device, size);
2543 if (FAILED(hr))
2544 goto done;
2546 vb_pos = device->vertex_buffer_pos;
2547 align = vb_pos % stride;
2548 if (align) align = stride - align;
2549 if (vb_pos + size + align > device->vertex_buffer_size)
2550 vb_pos = 0;
2551 else
2552 vb_pos += align;
2554 wined3d_box.left = vb_pos;
2555 wined3d_box.right = vb_pos + size;
2556 vb = wined3d_buffer_get_resource(device->vertex_buffer);
2557 if (FAILED(wined3d_resource_map(vb, 0, &wined3d_map_desc, &wined3d_box,
2558 WINED3D_MAP_WRITE | (vb_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD))))
2559 goto done;
2560 memcpy(wined3d_map_desc.data, data, size);
2561 wined3d_resource_unmap(vb, 0);
2562 device->vertex_buffer_pos = vb_pos + size;
2564 hr = wined3d_device_set_stream_source(device->wined3d_device, 0, device->vertex_buffer, 0, stride);
2565 if (FAILED(hr))
2566 goto done;
2568 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type, 0);
2569 hr = wined3d_device_draw_primitive(device->wined3d_device, vb_pos / stride, vtx_count);
2570 wined3d_device_set_stream_source(device->wined3d_device, 0, NULL, 0, 0);
2572 done:
2573 wined3d_mutex_unlock();
2574 return hr;
2577 /* The caller is responsible for wined3d locking */
2578 static HRESULT d3d8_device_prepare_index_buffer(struct d3d8_device *device, UINT min_size)
2580 HRESULT hr;
2582 if (device->index_buffer_size < min_size || !device->index_buffer)
2584 UINT size = max(device->index_buffer_size * 2, min_size);
2585 struct wined3d_buffer_desc desc;
2586 struct wined3d_buffer *buffer;
2588 TRACE("Growing index buffer to %u bytes\n", size);
2590 desc.byte_width = size;
2591 desc.usage = WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_STATICDECL;
2592 desc.bind_flags = WINED3D_BIND_INDEX_BUFFER;
2593 desc.access = WINED3D_RESOURCE_ACCESS_GPU | WINED3D_RESOURCE_ACCESS_MAP_W;
2594 desc.misc_flags = 0;
2595 desc.structure_byte_stride = 0;
2597 if (FAILED(hr = wined3d_buffer_create(device->wined3d_device, &desc,
2598 NULL, NULL, &d3d8_null_wined3d_parent_ops, &buffer)))
2600 ERR("Failed to create index buffer, hr %#x.\n", hr);
2601 return hr;
2604 if (device->index_buffer)
2605 wined3d_buffer_decref(device->index_buffer);
2607 device->index_buffer = buffer;
2608 device->index_buffer_size = size;
2609 device->index_buffer_pos = 0;
2611 return D3D_OK;
2614 static HRESULT WINAPI d3d8_device_DrawIndexedPrimitiveUP(IDirect3DDevice8 *iface,
2615 D3DPRIMITIVETYPE primitive_type, UINT min_vertex_idx, UINT vertex_count,
2616 UINT primitive_count, const void *index_data, D3DFORMAT index_format,
2617 const void *vertex_data, UINT vertex_stride)
2619 UINT idx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2620 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2621 UINT idx_fmt_size = index_format == D3DFMT_INDEX16 ? 2 : 4;
2622 UINT vtx_size = vertex_count * vertex_stride;
2623 UINT idx_size = idx_count * idx_fmt_size;
2624 struct wined3d_map_desc wined3d_map_desc;
2625 struct wined3d_box wined3d_box = {0};
2626 struct wined3d_resource *ib, *vb;
2627 UINT vb_pos, ib_pos, align;
2628 HRESULT hr;
2630 TRACE("iface %p, primitive_type %#x, min_vertex_idx %u, vertex_count %u, primitive_count %u, "
2631 "index_data %p, index_format %#x, vertex_data %p, vertex_stride %u.\n",
2632 iface, primitive_type, min_vertex_idx, vertex_count, primitive_count,
2633 index_data, index_format, vertex_data, vertex_stride);
2635 if (!primitive_count || !vertex_stride)
2637 WARN("primitive_count or vertex_stride is 0, returning D3D_OK.\n");
2638 return D3D_OK;
2641 wined3d_mutex_lock();
2643 wined3d_device_apply_stateblock(device->wined3d_device, device->state);
2644 hr = d3d8_device_prepare_vertex_buffer(device, vtx_size);
2645 if (FAILED(hr))
2646 goto done;
2648 vb_pos = device->vertex_buffer_pos;
2649 align = vb_pos % vertex_stride;
2650 if (align) align = vertex_stride - align;
2651 if (vb_pos + vtx_size + align > device->vertex_buffer_size)
2652 vb_pos = 0;
2653 else
2654 vb_pos += align;
2656 wined3d_box.left = vb_pos;
2657 wined3d_box.right = vb_pos + vtx_size;
2658 vb = wined3d_buffer_get_resource(device->vertex_buffer);
2659 if (FAILED(hr = wined3d_resource_map(vb, 0, &wined3d_map_desc, &wined3d_box,
2660 WINED3D_MAP_WRITE | (vb_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD))))
2661 goto done;
2662 memcpy(wined3d_map_desc.data, (char *)vertex_data + min_vertex_idx * vertex_stride, vtx_size);
2663 wined3d_resource_unmap(vb, 0);
2664 device->vertex_buffer_pos = vb_pos + vtx_size;
2666 hr = d3d8_device_prepare_index_buffer(device, idx_size);
2667 if (FAILED(hr))
2668 goto done;
2670 ib_pos = device->index_buffer_pos;
2671 align = ib_pos % idx_fmt_size;
2672 if (align) align = idx_fmt_size - align;
2673 if (ib_pos + idx_size + align > device->index_buffer_size)
2674 ib_pos = 0;
2675 else
2676 ib_pos += align;
2678 wined3d_box.left = ib_pos;
2679 wined3d_box.right = ib_pos + idx_size;
2680 ib = wined3d_buffer_get_resource(device->index_buffer);
2681 if (FAILED(hr = wined3d_resource_map(ib, 0, &wined3d_map_desc, &wined3d_box,
2682 WINED3D_MAP_WRITE | (ib_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD))))
2683 goto done;
2684 memcpy(wined3d_map_desc.data, index_data, idx_size);
2685 wined3d_resource_unmap(ib, 0);
2686 device->index_buffer_pos = ib_pos + idx_size;
2688 hr = wined3d_device_set_stream_source(device->wined3d_device, 0, device->vertex_buffer, 0, vertex_stride);
2689 if (FAILED(hr))
2690 goto done;
2692 wined3d_device_set_index_buffer(device->wined3d_device, device->index_buffer,
2693 wined3dformat_from_d3dformat(index_format), 0);
2694 wined3d_device_set_base_vertex_index(device->wined3d_device, vb_pos / vertex_stride - min_vertex_idx);
2696 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type, 0);
2697 hr = wined3d_device_draw_indexed_primitive(device->wined3d_device, ib_pos / idx_fmt_size, idx_count);
2699 wined3d_device_set_stream_source(device->wined3d_device, 0, NULL, 0, 0);
2700 wined3d_device_set_index_buffer(device->wined3d_device, NULL, WINED3DFMT_UNKNOWN, 0);
2701 wined3d_device_set_base_vertex_index(device->wined3d_device, 0);
2703 done:
2704 wined3d_mutex_unlock();
2705 return hr;
2708 static HRESULT WINAPI d3d8_device_ProcessVertices(IDirect3DDevice8 *iface, UINT src_start_idx,
2709 UINT dst_idx, UINT vertex_count, IDirect3DVertexBuffer8 *dst_buffer, DWORD flags)
2711 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2712 struct d3d8_vertexbuffer *dst = unsafe_impl_from_IDirect3DVertexBuffer8(dst_buffer);
2713 struct d3d8_vertexbuffer *d3d8_buffer;
2714 struct wined3d_buffer *wined3d_buffer;
2715 unsigned int i, offset, stride, map;
2716 HRESULT hr;
2718 TRACE("iface %p, src_start_idx %u, dst_idx %u, vertex_count %u, dst_buffer %p, flags %#x.\n",
2719 iface, src_start_idx, dst_idx, vertex_count, dst_buffer, flags);
2721 wined3d_mutex_lock();
2723 wined3d_device_apply_stateblock(device->wined3d_device, device->state);
2725 /* Note that an alternative approach would be to simply create these
2726 * buffers with WINED3D_RESOURCE_ACCESS_MAP_R and update them here like we
2727 * do for draws. In some regards that would be easier, but it seems less
2728 * than optimal to upload data to the GPU only to subsequently download it
2729 * again. */
2730 map = device->sysmem_vb;
2731 while (map)
2733 i = wined3d_bit_scan(&map);
2735 if (FAILED(wined3d_device_get_stream_source(device->wined3d_device,
2736 i, &wined3d_buffer, &offset, &stride)))
2737 ERR("Failed to get stream source.\n");
2738 d3d8_buffer = wined3d_buffer_get_parent(wined3d_buffer);
2739 if (FAILED(wined3d_device_set_stream_source(device->wined3d_device,
2740 i, d3d8_buffer->wined3d_buffer, offset, stride)))
2741 ERR("Failed to set stream source.\n");
2744 hr = wined3d_device_process_vertices(device->wined3d_device, src_start_idx, dst_idx,
2745 vertex_count, dst->wined3d_buffer, NULL, flags, dst->fvf);
2747 map = device->sysmem_vb;
2748 while (map)
2750 i = wined3d_bit_scan(&map);
2752 if (FAILED(wined3d_device_get_stream_source(device->wined3d_device,
2753 i, &wined3d_buffer, &offset, &stride)))
2754 ERR("Failed to get stream source.\n");
2755 d3d8_buffer = wined3d_buffer_get_parent(wined3d_buffer);
2756 if (FAILED(wined3d_device_set_stream_source(device->wined3d_device,
2757 i, d3d8_buffer->draw_buffer, offset, stride)))
2758 ERR("Failed to set stream source.\n");
2761 wined3d_mutex_unlock();
2763 return hr;
2766 static HRESULT WINAPI d3d8_device_CreateVertexShader(IDirect3DDevice8 *iface,
2767 const DWORD *declaration, const DWORD *byte_code, DWORD *shader, DWORD usage)
2769 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2770 struct d3d8_vertex_shader *object;
2771 DWORD shader_handle;
2772 DWORD handle;
2773 HRESULT hr;
2775 TRACE("iface %p, declaration %p, byte_code %p, shader %p, usage %#x.\n",
2776 iface, declaration, byte_code, shader, usage);
2778 if (!(object = heap_alloc_zero(sizeof(*object))))
2780 *shader = 0;
2781 return E_OUTOFMEMORY;
2784 wined3d_mutex_lock();
2785 handle = d3d8_allocate_handle(&device->handle_table, object, D3D8_HANDLE_VS);
2786 wined3d_mutex_unlock();
2787 if (handle == D3D8_INVALID_HANDLE)
2789 ERR("Failed to allocate vertex shader handle.\n");
2790 heap_free(object);
2791 *shader = 0;
2792 return E_OUTOFMEMORY;
2795 shader_handle = handle + VS_HIGHESTFIXEDFXF + 1;
2797 hr = d3d8_vertex_shader_init(object, device, declaration, byte_code, shader_handle, usage);
2798 if (FAILED(hr))
2800 WARN("Failed to initialize vertex shader, hr %#x.\n", hr);
2801 wined3d_mutex_lock();
2802 d3d8_free_handle(&device->handle_table, handle, D3D8_HANDLE_VS);
2803 wined3d_mutex_unlock();
2804 heap_free(object);
2805 *shader = 0;
2806 return hr;
2809 TRACE("Created vertex shader %p (handle %#x).\n", object, shader_handle);
2810 *shader = shader_handle;
2812 return D3D_OK;
2815 static struct d3d8_vertex_declaration *d3d8_device_get_fvf_declaration(struct d3d8_device *device, DWORD fvf)
2817 struct d3d8_vertex_declaration *d3d8_declaration;
2818 struct FvfToDecl *convertedDecls = device->decls;
2819 int p, low, high; /* deliberately signed */
2820 HRESULT hr;
2822 TRACE("Searching for declaration for fvf %08x... ", fvf);
2824 low = 0;
2825 high = device->numConvertedDecls - 1;
2826 while (low <= high)
2828 p = (low + high) >> 1;
2829 TRACE("%d ", p);
2831 if (convertedDecls[p].fvf == fvf)
2833 TRACE("found %p\n", convertedDecls[p].declaration);
2834 return convertedDecls[p].declaration;
2837 if (convertedDecls[p].fvf < fvf)
2838 low = p + 1;
2839 else
2840 high = p - 1;
2842 TRACE("not found. Creating and inserting at position %d.\n", low);
2844 if (!(d3d8_declaration = heap_alloc(sizeof(*d3d8_declaration))))
2845 return NULL;
2847 if (FAILED(hr = d3d8_vertex_declaration_init_fvf(d3d8_declaration, device, fvf)))
2849 WARN("Failed to initialize vertex declaration, hr %#x.\n", hr);
2850 heap_free(d3d8_declaration);
2851 return NULL;
2854 if (device->declArraySize == device->numConvertedDecls)
2856 UINT grow = device->declArraySize / 2;
2858 if (!(convertedDecls = heap_realloc(convertedDecls,
2859 sizeof(*convertedDecls) * (device->numConvertedDecls + grow))))
2861 d3d8_vertex_declaration_destroy(d3d8_declaration);
2862 return NULL;
2864 device->decls = convertedDecls;
2865 device->declArraySize += grow;
2868 memmove(convertedDecls + low + 1, convertedDecls + low,
2869 sizeof(*convertedDecls) * (device->numConvertedDecls - low));
2870 convertedDecls[low].declaration = d3d8_declaration;
2871 convertedDecls[low].fvf = fvf;
2872 ++device->numConvertedDecls;
2874 TRACE("Returning %p. %u decls in array.\n", d3d8_declaration, device->numConvertedDecls);
2876 return d3d8_declaration;
2879 static HRESULT WINAPI d3d8_device_SetVertexShader(IDirect3DDevice8 *iface, DWORD shader)
2881 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2882 struct d3d8_vertex_shader *shader_impl;
2884 TRACE("iface %p, shader %#x.\n", iface, shader);
2886 if (VS_HIGHESTFIXEDFXF >= shader)
2888 TRACE("Setting FVF, %#x\n", shader);
2890 wined3d_mutex_lock();
2891 wined3d_stateblock_set_vertex_declaration(device->update_state,
2892 d3d8_device_get_fvf_declaration(device, shader)->wined3d_vertex_declaration);
2893 wined3d_stateblock_set_vertex_shader(device->update_state, NULL);
2894 if (!device->recording)
2896 wined3d_device_set_vertex_declaration(device->wined3d_device,
2897 d3d8_device_get_fvf_declaration(device, shader)->wined3d_vertex_declaration);
2898 wined3d_device_set_vertex_shader(device->wined3d_device, NULL);
2900 wined3d_mutex_unlock();
2902 return D3D_OK;
2905 TRACE("Setting shader\n");
2907 wined3d_mutex_lock();
2908 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS)))
2910 WARN("Invalid handle (%#x) passed.\n", shader);
2911 wined3d_mutex_unlock();
2913 return D3DERR_INVALIDCALL;
2916 wined3d_stateblock_set_vertex_declaration(device->update_state,
2917 shader_impl->vertex_declaration->wined3d_vertex_declaration);
2918 wined3d_stateblock_set_vertex_shader(device->update_state, shader_impl->wined3d_shader);
2919 if (!device->recording)
2921 wined3d_device_set_vertex_declaration(device->wined3d_device,
2922 shader_impl->vertex_declaration->wined3d_vertex_declaration);
2923 wined3d_device_set_vertex_shader(device->wined3d_device, shader_impl->wined3d_shader);
2925 wined3d_mutex_unlock();
2927 return D3D_OK;
2930 static HRESULT WINAPI d3d8_device_GetVertexShader(IDirect3DDevice8 *iface, DWORD *shader)
2932 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2933 struct wined3d_vertex_declaration *wined3d_declaration;
2934 struct d3d8_vertex_declaration *d3d8_declaration;
2936 TRACE("iface %p, shader %p.\n", iface, shader);
2938 wined3d_mutex_lock();
2939 if ((wined3d_declaration = wined3d_device_get_vertex_declaration(device->wined3d_device)))
2941 d3d8_declaration = wined3d_vertex_declaration_get_parent(wined3d_declaration);
2942 *shader = d3d8_declaration->shader_handle;
2944 else
2946 *shader = 0;
2948 wined3d_mutex_unlock();
2950 TRACE("Returning %#x.\n", *shader);
2952 return D3D_OK;
2955 static HRESULT WINAPI d3d8_device_DeleteVertexShader(IDirect3DDevice8 *iface, DWORD shader)
2957 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2958 struct d3d8_vertex_shader *shader_impl;
2960 TRACE("iface %p, shader %#x.\n", iface, shader);
2962 wined3d_mutex_lock();
2963 if (!(shader_impl = d3d8_free_handle(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS)))
2965 WARN("Invalid handle (%#x) passed.\n", shader);
2966 wined3d_mutex_unlock();
2968 return D3DERR_INVALIDCALL;
2971 if (shader_impl->wined3d_shader
2972 && wined3d_device_get_vertex_shader(device->wined3d_device) == shader_impl->wined3d_shader)
2973 IDirect3DDevice8_SetVertexShader(iface, 0);
2975 wined3d_mutex_unlock();
2977 d3d8_vertex_shader_destroy(shader_impl);
2979 return D3D_OK;
2982 static HRESULT WINAPI d3d8_device_SetVertexShaderConstant(IDirect3DDevice8 *iface,
2983 DWORD start_register, const void *data, DWORD count)
2985 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2986 HRESULT hr;
2988 TRACE("iface %p, start_register %u, data %p, count %u.\n",
2989 iface, start_register, data, count);
2991 if (start_register + count > D3D8_MAX_VERTEX_SHADER_CONSTANTF)
2993 WARN("Trying to access %u constants, but d3d8 only supports %u\n",
2994 start_register + count, D3D8_MAX_VERTEX_SHADER_CONSTANTF);
2995 return D3DERR_INVALIDCALL;
2998 wined3d_mutex_lock();
2999 hr = wined3d_stateblock_set_vs_consts_f(device->update_state, start_register, count, data);
3000 if (SUCCEEDED(hr) && !device->recording)
3001 hr = wined3d_device_set_vs_consts_f(device->wined3d_device, start_register, count, data);
3002 wined3d_mutex_unlock();
3004 return hr;
3007 static HRESULT WINAPI d3d8_device_GetVertexShaderConstant(IDirect3DDevice8 *iface,
3008 DWORD start_register, void *data, DWORD count)
3010 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3011 HRESULT hr;
3013 TRACE("iface %p, start_register %u, data %p, count %u.\n",
3014 iface, start_register, data, count);
3016 if (start_register + count > D3D8_MAX_VERTEX_SHADER_CONSTANTF)
3018 WARN("Trying to access %u constants, but d3d8 only supports %u\n",
3019 start_register + count, D3D8_MAX_VERTEX_SHADER_CONSTANTF);
3020 return D3DERR_INVALIDCALL;
3023 wined3d_mutex_lock();
3024 hr = wined3d_device_get_vs_consts_f(device->wined3d_device, start_register, count, data);
3025 wined3d_mutex_unlock();
3027 return hr;
3030 static HRESULT WINAPI d3d8_device_GetVertexShaderDeclaration(IDirect3DDevice8 *iface,
3031 DWORD shader, void *data, DWORD *data_size)
3033 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3034 struct d3d8_vertex_declaration *declaration;
3035 struct d3d8_vertex_shader *shader_impl;
3037 TRACE("iface %p, shader %#x, data %p, data_size %p.\n",
3038 iface, shader, data, data_size);
3040 wined3d_mutex_lock();
3041 shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS);
3042 wined3d_mutex_unlock();
3044 if (!shader_impl)
3046 WARN("Invalid handle (%#x) passed.\n", shader);
3047 return D3DERR_INVALIDCALL;
3049 declaration = shader_impl->vertex_declaration;
3051 if (!data)
3053 *data_size = declaration->elements_size;
3054 return D3D_OK;
3057 /* MSDN claims that if *data_size is smaller than the required size
3058 * we should write the required size and return D3DERR_MOREDATA.
3059 * That's not actually true. */
3060 if (*data_size < declaration->elements_size)
3061 return D3DERR_INVALIDCALL;
3063 memcpy(data, declaration->elements, declaration->elements_size);
3065 return D3D_OK;
3068 static HRESULT WINAPI d3d8_device_GetVertexShaderFunction(IDirect3DDevice8 *iface,
3069 DWORD shader, void *data, DWORD *data_size)
3071 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3072 struct d3d8_vertex_shader *shader_impl = NULL;
3073 HRESULT hr;
3075 TRACE("iface %p, shader %#x, data %p, data_size %p.\n",
3076 iface, shader, data, data_size);
3078 wined3d_mutex_lock();
3079 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS)))
3081 WARN("Invalid handle (%#x) passed.\n", shader);
3082 wined3d_mutex_unlock();
3084 return D3DERR_INVALIDCALL;
3087 if (!shader_impl->wined3d_shader)
3089 wined3d_mutex_unlock();
3090 *data_size = 0;
3091 return D3D_OK;
3094 hr = wined3d_shader_get_byte_code(shader_impl->wined3d_shader, data, data_size);
3095 wined3d_mutex_unlock();
3097 return hr;
3100 static HRESULT WINAPI d3d8_device_SetIndices(IDirect3DDevice8 *iface,
3101 IDirect3DIndexBuffer8 *buffer, UINT base_vertex_idx)
3103 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3104 struct d3d8_indexbuffer *ib = unsafe_impl_from_IDirect3DIndexBuffer8(buffer);
3105 struct wined3d_buffer *wined3d_buffer;
3107 TRACE("iface %p, buffer %p, base_vertex_idx %u.\n", iface, buffer, base_vertex_idx);
3109 if (!ib)
3110 wined3d_buffer = NULL;
3111 else if (ib->draw_buffer)
3112 wined3d_buffer = ib->draw_buffer;
3113 else
3114 wined3d_buffer = ib->wined3d_buffer;
3116 /* WineD3D takes an INT(due to d3d9), but d3d8 uses UINTs. Do I have to add a check here that
3117 * the UINT doesn't cause an overflow in the INT? It seems rather unlikely because such large
3118 * vertex buffers can't be created to address them with an index that requires the 32nd bit
3119 * (4 Byte minimum vertex size * 2^31-1 -> 8 gb buffer. The index sign would be the least
3120 * problem)
3122 wined3d_mutex_lock();
3123 wined3d_stateblock_set_base_vertex_index(device->update_state, base_vertex_idx);
3124 wined3d_stateblock_set_index_buffer(device->update_state, wined3d_buffer, ib ? ib->format : WINED3DFMT_UNKNOWN);
3125 if (!device->recording)
3127 wined3d_device_set_base_vertex_index(device->wined3d_device, base_vertex_idx);
3128 wined3d_device_set_index_buffer(device->wined3d_device, wined3d_buffer, ib ? ib->format : WINED3DFMT_UNKNOWN, 0);
3129 device->sysmem_ib = ib && ib->draw_buffer;
3131 wined3d_mutex_unlock();
3133 return D3D_OK;
3136 static HRESULT WINAPI d3d8_device_GetIndices(IDirect3DDevice8 *iface,
3137 IDirect3DIndexBuffer8 **buffer, UINT *base_vertex_index)
3139 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3140 enum wined3d_format_id wined3d_format;
3141 struct wined3d_buffer *wined3d_buffer;
3142 struct d3d8_indexbuffer *buffer_impl;
3144 TRACE("iface %p, buffer %p, base_vertex_index %p.\n", iface, buffer, base_vertex_index);
3146 if (!buffer)
3147 return D3DERR_INVALIDCALL;
3149 /* The case from UINT to INT is safe because d3d8 will never set negative values */
3150 wined3d_mutex_lock();
3151 *base_vertex_index = wined3d_device_get_base_vertex_index(device->wined3d_device);
3152 if ((wined3d_buffer = wined3d_device_get_index_buffer(device->wined3d_device, &wined3d_format, NULL)))
3154 buffer_impl = wined3d_buffer_get_parent(wined3d_buffer);
3155 *buffer = &buffer_impl->IDirect3DIndexBuffer8_iface;
3156 IDirect3DIndexBuffer8_AddRef(*buffer);
3158 else
3160 *buffer = NULL;
3162 wined3d_mutex_unlock();
3164 return D3D_OK;
3167 static HRESULT WINAPI d3d8_device_CreatePixelShader(IDirect3DDevice8 *iface,
3168 const DWORD *byte_code, DWORD *shader)
3170 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3171 struct d3d8_pixel_shader *object;
3172 DWORD shader_handle;
3173 DWORD handle;
3174 HRESULT hr;
3176 TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
3178 if (!shader)
3179 return D3DERR_INVALIDCALL;
3181 if (!(object = heap_alloc_zero(sizeof(*object))))
3182 return E_OUTOFMEMORY;
3184 wined3d_mutex_lock();
3185 handle = d3d8_allocate_handle(&device->handle_table, object, D3D8_HANDLE_PS);
3186 wined3d_mutex_unlock();
3187 if (handle == D3D8_INVALID_HANDLE)
3189 ERR("Failed to allocate pixel shader handle.\n");
3190 heap_free(object);
3191 return E_OUTOFMEMORY;
3194 shader_handle = handle + VS_HIGHESTFIXEDFXF + 1;
3196 hr = d3d8_pixel_shader_init(object, device, byte_code, shader_handle);
3197 if (FAILED(hr))
3199 WARN("Failed to initialize pixel shader, hr %#x.\n", hr);
3200 wined3d_mutex_lock();
3201 d3d8_free_handle(&device->handle_table, handle, D3D8_HANDLE_PS);
3202 wined3d_mutex_unlock();
3203 heap_free(object);
3204 *shader = 0;
3205 return hr;
3208 TRACE("Created pixel shader %p (handle %#x).\n", object, shader_handle);
3209 *shader = shader_handle;
3211 return D3D_OK;
3214 static HRESULT WINAPI d3d8_device_SetPixelShader(IDirect3DDevice8 *iface, DWORD shader)
3216 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3217 struct d3d8_pixel_shader *shader_impl;
3219 TRACE("iface %p, shader %#x.\n", iface, shader);
3221 wined3d_mutex_lock();
3223 if (!shader)
3225 wined3d_stateblock_set_pixel_shader(device->update_state, NULL);
3226 if (!device->recording)
3227 wined3d_device_set_pixel_shader(device->wined3d_device, NULL);
3228 wined3d_mutex_unlock();
3229 return D3D_OK;
3232 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS)))
3234 WARN("Invalid handle (%#x) passed.\n", shader);
3235 wined3d_mutex_unlock();
3236 return D3DERR_INVALIDCALL;
3239 TRACE("Setting shader %p.\n", shader_impl);
3240 wined3d_stateblock_set_pixel_shader(device->update_state, shader_impl->wined3d_shader);
3241 if (!device->recording)
3242 wined3d_device_set_pixel_shader(device->wined3d_device, shader_impl->wined3d_shader);
3243 wined3d_mutex_unlock();
3245 return D3D_OK;
3248 static HRESULT WINAPI d3d8_device_GetPixelShader(IDirect3DDevice8 *iface, DWORD *shader)
3250 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3251 struct wined3d_shader *object;
3253 TRACE("iface %p, shader %p.\n", iface, shader);
3255 if (!shader)
3256 return D3DERR_INVALIDCALL;
3258 wined3d_mutex_lock();
3259 if ((object = wined3d_device_get_pixel_shader(device->wined3d_device)))
3261 struct d3d8_pixel_shader *d3d8_shader;
3262 d3d8_shader = wined3d_shader_get_parent(object);
3263 *shader = d3d8_shader->handle;
3265 else
3267 *shader = 0;
3269 wined3d_mutex_unlock();
3271 TRACE("Returning %#x.\n", *shader);
3273 return D3D_OK;
3276 static HRESULT WINAPI d3d8_device_DeletePixelShader(IDirect3DDevice8 *iface, DWORD shader)
3278 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3279 struct d3d8_pixel_shader *shader_impl;
3281 TRACE("iface %p, shader %#x.\n", iface, shader);
3283 wined3d_mutex_lock();
3285 if (!(shader_impl = d3d8_free_handle(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS)))
3287 WARN("Invalid handle (%#x) passed.\n", shader);
3288 wined3d_mutex_unlock();
3289 return D3DERR_INVALIDCALL;
3292 if (wined3d_device_get_pixel_shader(device->wined3d_device) == shader_impl->wined3d_shader)
3293 IDirect3DDevice8_SetPixelShader(iface, 0);
3295 wined3d_mutex_unlock();
3297 d3d8_pixel_shader_destroy(shader_impl);
3299 return D3D_OK;
3302 static HRESULT WINAPI d3d8_device_SetPixelShaderConstant(IDirect3DDevice8 *iface,
3303 DWORD start_register, const void *data, DWORD count)
3305 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3306 HRESULT hr;
3308 TRACE("iface %p, start_register %u, data %p, count %u.\n",
3309 iface, start_register, data, count);
3311 wined3d_mutex_lock();
3312 hr = wined3d_stateblock_set_ps_consts_f(device->update_state, start_register, count, data);
3313 if (SUCCEEDED(hr) && !device->recording)
3314 hr = wined3d_device_set_ps_consts_f(device->wined3d_device, start_register, count, data);
3315 wined3d_mutex_unlock();
3317 return hr;
3320 static HRESULT WINAPI d3d8_device_GetPixelShaderConstant(IDirect3DDevice8 *iface,
3321 DWORD start_register, void *data, DWORD count)
3323 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3324 HRESULT hr;
3326 TRACE("iface %p, start_register %u, data %p, count %u.\n",
3327 iface, start_register, data, count);
3329 wined3d_mutex_lock();
3330 hr = wined3d_device_get_ps_consts_f(device->wined3d_device, start_register, count, data);
3331 wined3d_mutex_unlock();
3333 return hr;
3336 static HRESULT WINAPI d3d8_device_GetPixelShaderFunction(IDirect3DDevice8 *iface,
3337 DWORD shader, void *data, DWORD *data_size)
3339 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3340 struct d3d8_pixel_shader *shader_impl = NULL;
3341 HRESULT hr;
3343 TRACE("iface %p, shader %#x, data %p, data_size %p.\n",
3344 iface, shader, data, data_size);
3346 wined3d_mutex_lock();
3347 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS)))
3349 WARN("Invalid handle (%#x) passed.\n", shader);
3350 wined3d_mutex_unlock();
3352 return D3DERR_INVALIDCALL;
3355 hr = wined3d_shader_get_byte_code(shader_impl->wined3d_shader, data, data_size);
3356 wined3d_mutex_unlock();
3358 return hr;
3361 static HRESULT WINAPI d3d8_device_DrawRectPatch(IDirect3DDevice8 *iface, UINT handle,
3362 const float *segment_count, const D3DRECTPATCH_INFO *patch_info)
3364 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
3365 iface, handle, segment_count, patch_info);
3366 return D3D_OK;
3369 static HRESULT WINAPI d3d8_device_DrawTriPatch(IDirect3DDevice8 *iface, UINT handle,
3370 const float *segment_count, const D3DTRIPATCH_INFO *patch_info)
3372 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
3373 iface, handle, segment_count, patch_info);
3374 return D3D_OK;
3377 static HRESULT WINAPI d3d8_device_DeletePatch(IDirect3DDevice8 *iface, UINT handle)
3379 FIXME("iface %p, handle %#x unimplemented.\n", iface, handle);
3380 return D3DERR_INVALIDCALL;
3383 static HRESULT WINAPI d3d8_device_SetStreamSource(IDirect3DDevice8 *iface,
3384 UINT stream_idx, IDirect3DVertexBuffer8 *buffer, UINT stride)
3386 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3387 struct d3d8_vertexbuffer *buffer_impl = unsafe_impl_from_IDirect3DVertexBuffer8(buffer);
3388 struct wined3d_buffer *wined3d_buffer;
3389 HRESULT hr;
3391 TRACE("iface %p, stream_idx %u, buffer %p, stride %u.\n",
3392 iface, stream_idx, buffer, stride);
3394 wined3d_mutex_lock();
3396 if (!buffer_impl)
3398 wined3d_device_get_stream_source(device->wined3d_device, stream_idx, &wined3d_buffer,
3399 NULL, &stride);
3400 wined3d_buffer = NULL;
3402 else if (buffer_impl->draw_buffer)
3403 wined3d_buffer = buffer_impl->draw_buffer;
3404 else
3405 wined3d_buffer = buffer_impl->wined3d_buffer;
3407 hr = wined3d_stateblock_set_stream_source(device->update_state, stream_idx, wined3d_buffer, 0, stride);
3408 if (SUCCEEDED(hr) && !device->recording)
3410 hr = wined3d_device_set_stream_source(device->wined3d_device, stream_idx, wined3d_buffer, 0, stride);
3411 if (SUCCEEDED(hr))
3413 if (buffer_impl && buffer_impl->draw_buffer)
3414 device->sysmem_vb |= (1u << stream_idx);
3415 else
3416 device->sysmem_vb &= ~(1u << stream_idx);
3420 wined3d_mutex_unlock();
3422 return hr;
3425 static HRESULT WINAPI d3d8_device_GetStreamSource(IDirect3DDevice8 *iface,
3426 UINT stream_idx, IDirect3DVertexBuffer8 **buffer, UINT *stride)
3428 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3429 struct d3d8_vertexbuffer *buffer_impl;
3430 struct wined3d_buffer *wined3d_buffer = NULL;
3431 HRESULT hr;
3433 TRACE("iface %p, stream_idx %u, buffer %p, stride %p.\n",
3434 iface, stream_idx, buffer, stride);
3436 if (!buffer)
3437 return D3DERR_INVALIDCALL;
3439 wined3d_mutex_lock();
3440 hr = wined3d_device_get_stream_source(device->wined3d_device, stream_idx, &wined3d_buffer, 0, stride);
3441 if (SUCCEEDED(hr) && wined3d_buffer)
3443 buffer_impl = wined3d_buffer_get_parent(wined3d_buffer);
3444 *buffer = &buffer_impl->IDirect3DVertexBuffer8_iface;
3445 IDirect3DVertexBuffer8_AddRef(*buffer);
3447 else
3449 if (FAILED(hr))
3450 ERR("Failed to get wined3d stream source, hr %#x.\n", hr);
3451 *buffer = NULL;
3453 wined3d_mutex_unlock();
3455 return hr;
3458 static const struct IDirect3DDevice8Vtbl d3d8_device_vtbl =
3460 d3d8_device_QueryInterface,
3461 d3d8_device_AddRef,
3462 d3d8_device_Release,
3463 d3d8_device_TestCooperativeLevel,
3464 d3d8_device_GetAvailableTextureMem,
3465 d3d8_device_ResourceManagerDiscardBytes,
3466 d3d8_device_GetDirect3D,
3467 d3d8_device_GetDeviceCaps,
3468 d3d8_device_GetDisplayMode,
3469 d3d8_device_GetCreationParameters,
3470 d3d8_device_SetCursorProperties,
3471 d3d8_device_SetCursorPosition,
3472 d3d8_device_ShowCursor,
3473 d3d8_device_CreateAdditionalSwapChain,
3474 d3d8_device_Reset,
3475 d3d8_device_Present,
3476 d3d8_device_GetBackBuffer,
3477 d3d8_device_GetRasterStatus,
3478 d3d8_device_SetGammaRamp,
3479 d3d8_device_GetGammaRamp,
3480 d3d8_device_CreateTexture,
3481 d3d8_device_CreateVolumeTexture,
3482 d3d8_device_CreateCubeTexture,
3483 d3d8_device_CreateVertexBuffer,
3484 d3d8_device_CreateIndexBuffer,
3485 d3d8_device_CreateRenderTarget,
3486 d3d8_device_CreateDepthStencilSurface,
3487 d3d8_device_CreateImageSurface,
3488 d3d8_device_CopyRects,
3489 d3d8_device_UpdateTexture,
3490 d3d8_device_GetFrontBuffer,
3491 d3d8_device_SetRenderTarget,
3492 d3d8_device_GetRenderTarget,
3493 d3d8_device_GetDepthStencilSurface,
3494 d3d8_device_BeginScene,
3495 d3d8_device_EndScene,
3496 d3d8_device_Clear,
3497 d3d8_device_SetTransform,
3498 d3d8_device_GetTransform,
3499 d3d8_device_MultiplyTransform,
3500 d3d8_device_SetViewport,
3501 d3d8_device_GetViewport,
3502 d3d8_device_SetMaterial,
3503 d3d8_device_GetMaterial,
3504 d3d8_device_SetLight,
3505 d3d8_device_GetLight,
3506 d3d8_device_LightEnable,
3507 d3d8_device_GetLightEnable,
3508 d3d8_device_SetClipPlane,
3509 d3d8_device_GetClipPlane,
3510 d3d8_device_SetRenderState,
3511 d3d8_device_GetRenderState,
3512 d3d8_device_BeginStateBlock,
3513 d3d8_device_EndStateBlock,
3514 d3d8_device_ApplyStateBlock,
3515 d3d8_device_CaptureStateBlock,
3516 d3d8_device_DeleteStateBlock,
3517 d3d8_device_CreateStateBlock,
3518 d3d8_device_SetClipStatus,
3519 d3d8_device_GetClipStatus,
3520 d3d8_device_GetTexture,
3521 d3d8_device_SetTexture,
3522 d3d8_device_GetTextureStageState,
3523 d3d8_device_SetTextureStageState,
3524 d3d8_device_ValidateDevice,
3525 d3d8_device_GetInfo,
3526 d3d8_device_SetPaletteEntries,
3527 d3d8_device_GetPaletteEntries,
3528 d3d8_device_SetCurrentTexturePalette,
3529 d3d8_device_GetCurrentTexturePalette,
3530 d3d8_device_DrawPrimitive,
3531 d3d8_device_DrawIndexedPrimitive,
3532 d3d8_device_DrawPrimitiveUP,
3533 d3d8_device_DrawIndexedPrimitiveUP,
3534 d3d8_device_ProcessVertices,
3535 d3d8_device_CreateVertexShader,
3536 d3d8_device_SetVertexShader,
3537 d3d8_device_GetVertexShader,
3538 d3d8_device_DeleteVertexShader,
3539 d3d8_device_SetVertexShaderConstant,
3540 d3d8_device_GetVertexShaderConstant,
3541 d3d8_device_GetVertexShaderDeclaration,
3542 d3d8_device_GetVertexShaderFunction,
3543 d3d8_device_SetStreamSource,
3544 d3d8_device_GetStreamSource,
3545 d3d8_device_SetIndices,
3546 d3d8_device_GetIndices,
3547 d3d8_device_CreatePixelShader,
3548 d3d8_device_SetPixelShader,
3549 d3d8_device_GetPixelShader,
3550 d3d8_device_DeletePixelShader,
3551 d3d8_device_SetPixelShaderConstant,
3552 d3d8_device_GetPixelShaderConstant,
3553 d3d8_device_GetPixelShaderFunction,
3554 d3d8_device_DrawRectPatch,
3555 d3d8_device_DrawTriPatch,
3556 d3d8_device_DeletePatch,
3559 static inline struct d3d8_device *device_from_device_parent(struct wined3d_device_parent *device_parent)
3561 return CONTAINING_RECORD(device_parent, struct d3d8_device, device_parent);
3564 static void CDECL device_parent_wined3d_device_created(struct wined3d_device_parent *device_parent,
3565 struct wined3d_device *device)
3567 TRACE("device_parent %p, device %p\n", device_parent, device);
3570 static void CDECL device_parent_mode_changed(struct wined3d_device_parent *device_parent)
3572 TRACE("device_parent %p.\n", device_parent);
3575 static void CDECL device_parent_activate(struct wined3d_device_parent *device_parent, BOOL activate)
3577 struct d3d8_device *device = device_from_device_parent(device_parent);
3579 TRACE("device_parent %p, activate %#x.\n", device_parent, activate);
3581 if (!activate)
3582 InterlockedCompareExchange(&device->device_state, D3D8_DEVICE_STATE_LOST, D3D8_DEVICE_STATE_OK);
3583 else
3584 InterlockedCompareExchange(&device->device_state, D3D8_DEVICE_STATE_NOT_RESET, D3D8_DEVICE_STATE_LOST);
3587 static HRESULT CDECL device_parent_texture_sub_resource_created(struct wined3d_device_parent *device_parent,
3588 enum wined3d_resource_type type, struct wined3d_texture *wined3d_texture, unsigned int sub_resource_idx,
3589 void **parent, const struct wined3d_parent_ops **parent_ops)
3591 TRACE("device_parent %p, type %#x, wined3d_texture %p, sub_resource_idx %u, parent %p, parent_ops %p.\n",
3592 device_parent, type, wined3d_texture, sub_resource_idx, parent, parent_ops);
3594 if (type == WINED3D_RTYPE_TEXTURE_2D)
3596 struct d3d8_surface *d3d_surface;
3598 if (!(d3d_surface = heap_alloc_zero(sizeof(*d3d_surface))))
3599 return E_OUTOFMEMORY;
3601 surface_init(d3d_surface, wined3d_texture, sub_resource_idx, parent_ops);
3602 *parent = d3d_surface;
3603 TRACE("Created surface %p.\n", d3d_surface);
3605 else if (type == WINED3D_RTYPE_TEXTURE_3D)
3607 struct d3d8_volume *d3d_volume;
3609 if (!(d3d_volume = heap_alloc_zero(sizeof(*d3d_volume))))
3610 return E_OUTOFMEMORY;
3612 volume_init(d3d_volume, wined3d_texture, sub_resource_idx, parent_ops);
3613 *parent = d3d_volume;
3614 TRACE("Created volume %p.\n", d3d_volume);
3616 else
3618 ERR("Unhandled resource type %#x.\n", type);
3619 return E_FAIL;
3622 return D3D_OK;
3625 static HRESULT CDECL device_parent_create_swapchain_texture(struct wined3d_device_parent *device_parent,
3626 void *container_parent, const struct wined3d_resource_desc *desc, DWORD texture_flags,
3627 struct wined3d_texture **texture)
3629 struct d3d8_device *device = device_from_device_parent(device_parent);
3630 struct d3d8_surface *d3d_surface;
3631 HRESULT hr;
3633 TRACE("device_parent %p, container_parent %p, desc %p, texture flags %#x, texture %p.\n",
3634 device_parent, container_parent, desc, texture_flags, texture);
3636 if (FAILED(hr = wined3d_texture_create(device->wined3d_device, desc, 1, 1, texture_flags,
3637 NULL, &device->IDirect3DDevice8_iface, &d3d8_null_wined3d_parent_ops, texture)))
3639 WARN("Failed to create texture, hr %#x.\n", hr);
3640 return hr;
3643 d3d_surface = wined3d_texture_get_sub_resource_parent(*texture, 0);
3644 d3d_surface->parent_device = &device->IDirect3DDevice8_iface;
3646 return hr;
3649 static const struct wined3d_device_parent_ops d3d8_wined3d_device_parent_ops =
3651 device_parent_wined3d_device_created,
3652 device_parent_mode_changed,
3653 device_parent_activate,
3654 device_parent_texture_sub_resource_created,
3655 device_parent_create_swapchain_texture,
3658 static void setup_fpu(void)
3660 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
3661 WORD cw;
3662 __asm__ volatile ("fnstcw %0" : "=m" (cw));
3663 cw = (cw & ~0xf3f) | 0x3f;
3664 __asm__ volatile ("fldcw %0" : : "m" (cw));
3665 #elif defined(__i386__) && defined(_MSC_VER)
3666 WORD cw;
3667 __asm fnstcw cw;
3668 cw = (cw & ~0xf3f) | 0x3f;
3669 __asm fldcw cw;
3670 #else
3671 FIXME("FPU setup not implemented for this platform.\n");
3672 #endif
3675 HRESULT device_init(struct d3d8_device *device, struct d3d8 *parent, struct wined3d *wined3d, UINT adapter,
3676 D3DDEVTYPE device_type, HWND focus_window, DWORD flags, D3DPRESENT_PARAMETERS *parameters)
3678 struct wined3d_swapchain_desc swapchain_desc;
3679 struct wined3d_swapchain *wined3d_swapchain;
3680 struct d3d8_swapchain *d3d_swapchain;
3681 HRESULT hr;
3683 static const enum wined3d_feature_level feature_levels[] =
3685 WINED3D_FEATURE_LEVEL_8,
3686 WINED3D_FEATURE_LEVEL_7,
3687 WINED3D_FEATURE_LEVEL_6,
3688 WINED3D_FEATURE_LEVEL_5,
3691 device->IDirect3DDevice8_iface.lpVtbl = &d3d8_device_vtbl;
3692 device->device_parent.ops = &d3d8_wined3d_device_parent_ops;
3693 device->ref = 1;
3694 if (!(device->handle_table.entries = heap_alloc_zero(D3D8_INITIAL_HANDLE_TABLE_SIZE
3695 * sizeof(*device->handle_table.entries))))
3697 ERR("Failed to allocate handle table memory.\n");
3698 return E_OUTOFMEMORY;
3700 device->handle_table.table_size = D3D8_INITIAL_HANDLE_TABLE_SIZE;
3702 if (!(flags & D3DCREATE_FPU_PRESERVE)) setup_fpu();
3704 wined3d_mutex_lock();
3705 if (FAILED(hr = wined3d_device_create(wined3d, adapter, device_type,
3706 focus_window, flags, 4, feature_levels, ARRAY_SIZE(feature_levels),
3707 &device->device_parent, &device->wined3d_device)))
3709 WARN("Failed to create wined3d device, hr %#x.\n", hr);
3710 wined3d_mutex_unlock();
3711 heap_free(device->handle_table.entries);
3712 return hr;
3715 if (FAILED(hr = wined3d_stateblock_create(device->wined3d_device, NULL, WINED3D_SBT_PRIMARY, &device->state)))
3717 ERR("Failed to create primary stateblock, hr %#x.\n", hr);
3718 wined3d_device_decref(device->wined3d_device);
3719 wined3d_mutex_unlock();
3720 return hr;
3722 device->update_state = device->state;
3724 if (!parameters->Windowed)
3726 if (!focus_window)
3727 focus_window = parameters->hDeviceWindow;
3728 if (FAILED(hr = wined3d_device_acquire_focus_window(device->wined3d_device, focus_window)))
3730 ERR("Failed to acquire focus window, hr %#x.\n", hr);
3731 wined3d_device_decref(device->wined3d_device);
3732 wined3d_mutex_unlock();
3733 heap_free(device->handle_table.entries);
3734 return hr;
3738 if (flags & D3DCREATE_MULTITHREADED)
3739 wined3d_device_set_multithreaded(device->wined3d_device);
3741 if (!wined3d_swapchain_desc_from_present_parameters(&swapchain_desc, parameters))
3743 wined3d_device_release_focus_window(device->wined3d_device);
3744 wined3d_device_decref(device->wined3d_device);
3745 wined3d_mutex_unlock();
3746 heap_free(device->handle_table.entries);
3747 return D3DERR_INVALIDCALL;
3749 swapchain_desc.flags |= WINED3D_SWAPCHAIN_IMPLICIT;
3751 if (FAILED(hr = d3d8_swapchain_create(device, &swapchain_desc,
3752 wined3dswapinterval_from_d3d(parameters->FullScreen_PresentationInterval), &d3d_swapchain)))
3754 WARN("Failed to create implicit swapchain, hr %#x.\n", hr);
3755 wined3d_device_release_focus_window(device->wined3d_device);
3756 wined3d_device_decref(device->wined3d_device);
3757 wined3d_mutex_unlock();
3758 heap_free(device->handle_table.entries);
3759 return hr;
3762 wined3d_swapchain = d3d_swapchain->wined3d_swapchain;
3763 wined3d_swapchain_incref(wined3d_swapchain);
3764 IDirect3DSwapChain8_Release(&d3d_swapchain->IDirect3DSwapChain8_iface);
3766 wined3d_stateblock_set_render_state(device->state, WINED3D_RS_ZENABLE,
3767 !!swapchain_desc.enable_auto_depth_stencil);
3768 wined3d_stateblock_set_render_state(device->state, WINED3D_RS_POINTSIZE_MIN, 0);
3769 device_reset_viewport_state(device);
3770 wined3d_mutex_unlock();
3772 present_parameters_from_wined3d_swapchain_desc(parameters,
3773 &swapchain_desc, parameters->FullScreen_PresentationInterval);
3775 device->declArraySize = 16;
3776 if (!(device->decls = heap_alloc(device->declArraySize * sizeof(*device->decls))))
3778 ERR("Failed to allocate FVF vertex declaration map memory.\n");
3779 hr = E_OUTOFMEMORY;
3780 goto err;
3783 device->implicit_swapchain = wined3d_swapchain;
3785 device->d3d_parent = &parent->IDirect3D8_iface;
3786 IDirect3D8_AddRef(device->d3d_parent);
3788 return D3D_OK;
3790 err:
3791 wined3d_mutex_lock();
3792 wined3d_swapchain_decref(wined3d_swapchain);
3793 wined3d_device_release_focus_window(device->wined3d_device);
3794 wined3d_device_decref(device->wined3d_device);
3795 wined3d_mutex_unlock();
3796 heap_free(device->handle_table.entries);
3797 return hr;