user32: Clear %edi register when calling window procedure.
[wine.git] / dlls / d3d8 / device.c
blobb4d445d63d38f734e67846fc777a5315478fca31
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 "config.h"
23 #include "wine/port.h"
25 #include <math.h>
26 #include <stdarg.h>
28 #include "windef.h"
29 #include "winbase.h"
30 #include "winuser.h"
31 #include "wingdi.h"
32 #include "wine/debug.h"
34 #include "d3d8_private.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(d3d8);
38 static void STDMETHODCALLTYPE d3d8_null_wined3d_object_destroyed(void *parent) {}
40 const struct wined3d_parent_ops d3d8_null_wined3d_parent_ops =
42 d3d8_null_wined3d_object_destroyed,
45 D3DFORMAT d3dformat_from_wined3dformat(enum wined3d_format_id format)
47 BYTE *c = (BYTE *)&format;
49 /* Don't translate FOURCC formats */
50 if (isprint(c[0]) && isprint(c[1]) && isprint(c[2]) && isprint(c[3])) return format;
52 switch(format)
54 case WINED3DFMT_UNKNOWN: return D3DFMT_UNKNOWN;
55 case WINED3DFMT_B8G8R8_UNORM: return D3DFMT_R8G8B8;
56 case WINED3DFMT_B8G8R8A8_UNORM: return D3DFMT_A8R8G8B8;
57 case WINED3DFMT_B8G8R8X8_UNORM: return D3DFMT_X8R8G8B8;
58 case WINED3DFMT_B5G6R5_UNORM: return D3DFMT_R5G6B5;
59 case WINED3DFMT_B5G5R5X1_UNORM: return D3DFMT_X1R5G5B5;
60 case WINED3DFMT_B5G5R5A1_UNORM: return D3DFMT_A1R5G5B5;
61 case WINED3DFMT_B4G4R4A4_UNORM: return D3DFMT_A4R4G4B4;
62 case WINED3DFMT_B2G3R3_UNORM: return D3DFMT_R3G3B2;
63 case WINED3DFMT_A8_UNORM: return D3DFMT_A8;
64 case WINED3DFMT_B2G3R3A8_UNORM: return D3DFMT_A8R3G3B2;
65 case WINED3DFMT_B4G4R4X4_UNORM: return D3DFMT_X4R4G4B4;
66 case WINED3DFMT_R10G10B10A2_UNORM: return D3DFMT_A2B10G10R10;
67 case WINED3DFMT_R16G16_UNORM: return D3DFMT_G16R16;
68 case WINED3DFMT_P8_UINT_A8_UNORM: return D3DFMT_A8P8;
69 case WINED3DFMT_P8_UINT: return D3DFMT_P8;
70 case WINED3DFMT_L8_UNORM: return D3DFMT_L8;
71 case WINED3DFMT_L8A8_UNORM: return D3DFMT_A8L8;
72 case WINED3DFMT_L4A4_UNORM: return D3DFMT_A4L4;
73 case WINED3DFMT_R8G8_SNORM: return D3DFMT_V8U8;
74 case WINED3DFMT_R5G5_SNORM_L6_UNORM: return D3DFMT_L6V5U5;
75 case WINED3DFMT_R8G8_SNORM_L8X8_UNORM: return D3DFMT_X8L8V8U8;
76 case WINED3DFMT_R8G8B8A8_SNORM: return D3DFMT_Q8W8V8U8;
77 case WINED3DFMT_R16G16_SNORM: return D3DFMT_V16U16;
78 case WINED3DFMT_R10G11B11_SNORM: return D3DFMT_W11V11U10;
79 case WINED3DFMT_R10G10B10_SNORM_A2_UNORM: return D3DFMT_A2W10V10U10;
80 case WINED3DFMT_D16_LOCKABLE: return D3DFMT_D16_LOCKABLE;
81 case WINED3DFMT_D32_UNORM: return D3DFMT_D32;
82 case WINED3DFMT_S1_UINT_D15_UNORM: return D3DFMT_D15S1;
83 case WINED3DFMT_D24_UNORM_S8_UINT: return D3DFMT_D24S8;
84 case WINED3DFMT_X8D24_UNORM: return D3DFMT_D24X8;
85 case WINED3DFMT_S4X4_UINT_D24_UNORM: return D3DFMT_D24X4S4;
86 case WINED3DFMT_D16_UNORM: return D3DFMT_D16;
87 case WINED3DFMT_R16_UINT: return D3DFMT_INDEX16;
88 case WINED3DFMT_R32_UINT: return D3DFMT_INDEX32;
89 default:
90 FIXME("Unhandled wined3d format %#x.\n", format);
91 return D3DFMT_UNKNOWN;
95 enum wined3d_format_id wined3dformat_from_d3dformat(D3DFORMAT format)
97 BYTE *c = (BYTE *)&format;
99 /* Don't translate FOURCC formats */
100 if (isprint(c[0]) && isprint(c[1]) && isprint(c[2]) && isprint(c[3])) return format;
102 switch(format)
104 case D3DFMT_UNKNOWN: return WINED3DFMT_UNKNOWN;
105 case D3DFMT_R8G8B8: return WINED3DFMT_B8G8R8_UNORM;
106 case D3DFMT_A8R8G8B8: return WINED3DFMT_B8G8R8A8_UNORM;
107 case D3DFMT_X8R8G8B8: return WINED3DFMT_B8G8R8X8_UNORM;
108 case D3DFMT_R5G6B5: return WINED3DFMT_B5G6R5_UNORM;
109 case D3DFMT_X1R5G5B5: return WINED3DFMT_B5G5R5X1_UNORM;
110 case D3DFMT_A1R5G5B5: return WINED3DFMT_B5G5R5A1_UNORM;
111 case D3DFMT_A4R4G4B4: return WINED3DFMT_B4G4R4A4_UNORM;
112 case D3DFMT_R3G3B2: return WINED3DFMT_B2G3R3_UNORM;
113 case D3DFMT_A8: return WINED3DFMT_A8_UNORM;
114 case D3DFMT_A8R3G3B2: return WINED3DFMT_B2G3R3A8_UNORM;
115 case D3DFMT_X4R4G4B4: return WINED3DFMT_B4G4R4X4_UNORM;
116 case D3DFMT_A2B10G10R10: return WINED3DFMT_R10G10B10A2_UNORM;
117 case D3DFMT_G16R16: return WINED3DFMT_R16G16_UNORM;
118 case D3DFMT_A8P8: return WINED3DFMT_P8_UINT_A8_UNORM;
119 case D3DFMT_P8: return WINED3DFMT_P8_UINT;
120 case D3DFMT_L8: return WINED3DFMT_L8_UNORM;
121 case D3DFMT_A8L8: return WINED3DFMT_L8A8_UNORM;
122 case D3DFMT_A4L4: return WINED3DFMT_L4A4_UNORM;
123 case D3DFMT_V8U8: return WINED3DFMT_R8G8_SNORM;
124 case D3DFMT_L6V5U5: return WINED3DFMT_R5G5_SNORM_L6_UNORM;
125 case D3DFMT_X8L8V8U8: return WINED3DFMT_R8G8_SNORM_L8X8_UNORM;
126 case D3DFMT_Q8W8V8U8: return WINED3DFMT_R8G8B8A8_SNORM;
127 case D3DFMT_V16U16: return WINED3DFMT_R16G16_SNORM;
128 case D3DFMT_W11V11U10: return WINED3DFMT_R10G11B11_SNORM;
129 case D3DFMT_A2W10V10U10: return WINED3DFMT_R10G10B10_SNORM_A2_UNORM;
130 case D3DFMT_D16_LOCKABLE: return WINED3DFMT_D16_LOCKABLE;
131 case D3DFMT_D32: return WINED3DFMT_D32_UNORM;
132 case D3DFMT_D15S1: return WINED3DFMT_S1_UINT_D15_UNORM;
133 case D3DFMT_D24S8: return WINED3DFMT_D24_UNORM_S8_UINT;
134 case D3DFMT_D24X8: return WINED3DFMT_X8D24_UNORM;
135 case D3DFMT_D24X4S4: return WINED3DFMT_S4X4_UINT_D24_UNORM;
136 case D3DFMT_D16: return WINED3DFMT_D16_UNORM;
137 case D3DFMT_INDEX16: return WINED3DFMT_R16_UINT;
138 case D3DFMT_INDEX32: return WINED3DFMT_R32_UINT;
139 default:
140 FIXME("Unhandled D3DFORMAT %#x.\n", format);
141 return WINED3DFMT_UNKNOWN;
145 unsigned int wined3dmapflags_from_d3dmapflags(unsigned int flags)
147 static const unsigned int handled = D3DLOCK_NOSYSLOCK
148 | D3DLOCK_NOOVERWRITE
149 | D3DLOCK_DISCARD
150 | D3DLOCK_NO_DIRTY_UPDATE;
151 unsigned int wined3d_flags;
153 wined3d_flags = flags & handled;
154 if (!(flags & (D3DLOCK_NOOVERWRITE | D3DLOCK_DISCARD)))
155 wined3d_flags |= WINED3D_MAP_READ;
156 if (!(flags & D3DLOCK_READONLY))
157 wined3d_flags |= WINED3D_MAP_WRITE;
158 if (!(wined3d_flags & (WINED3D_MAP_READ | WINED3D_MAP_WRITE)))
159 wined3d_flags |= WINED3D_MAP_READ | WINED3D_MAP_WRITE;
160 flags &= ~(handled | D3DLOCK_READONLY);
162 if (flags)
163 FIXME("Unhandled flags %#x.\n", flags);
165 return wined3d_flags;
168 static UINT vertex_count_from_primitive_count(D3DPRIMITIVETYPE primitive_type, UINT primitive_count)
170 switch (primitive_type)
172 case D3DPT_POINTLIST:
173 return primitive_count;
175 case D3DPT_LINELIST:
176 return primitive_count * 2;
178 case D3DPT_LINESTRIP:
179 return primitive_count + 1;
181 case D3DPT_TRIANGLELIST:
182 return primitive_count * 3;
184 case D3DPT_TRIANGLESTRIP:
185 case D3DPT_TRIANGLEFAN:
186 return primitive_count + 2;
188 default:
189 FIXME("Unhandled primitive type %#x.\n", primitive_type);
190 return 0;
194 static D3DSWAPEFFECT d3dswapeffect_from_wined3dswapeffect(enum wined3d_swap_effect effect)
196 switch (effect)
198 case WINED3D_SWAP_EFFECT_DISCARD:
199 return D3DSWAPEFFECT_DISCARD;
200 case WINED3D_SWAP_EFFECT_SEQUENTIAL:
201 return D3DSWAPEFFECT_FLIP;
202 case WINED3D_SWAP_EFFECT_COPY:
203 return D3DSWAPEFFECT_COPY;
204 case WINED3D_SWAP_EFFECT_COPY_VSYNC:
205 return D3DSWAPEFFECT_COPY_VSYNC;
206 default:
207 FIXME("Unhandled swap effect %#x.\n", effect);
208 return D3DSWAPEFFECT_FLIP;
212 static void present_parameters_from_wined3d_swapchain_desc(D3DPRESENT_PARAMETERS *present_parameters,
213 const struct wined3d_swapchain_desc *swapchain_desc, DWORD presentation_interval)
215 present_parameters->BackBufferWidth = swapchain_desc->backbuffer_width;
216 present_parameters->BackBufferHeight = swapchain_desc->backbuffer_height;
217 present_parameters->BackBufferFormat = d3dformat_from_wined3dformat(swapchain_desc->backbuffer_format);
218 present_parameters->BackBufferCount = swapchain_desc->backbuffer_count;
219 present_parameters->MultiSampleType = swapchain_desc->multisample_type;
220 present_parameters->SwapEffect = d3dswapeffect_from_wined3dswapeffect(swapchain_desc->swap_effect);
221 present_parameters->hDeviceWindow = swapchain_desc->device_window;
222 present_parameters->Windowed = swapchain_desc->windowed;
223 present_parameters->EnableAutoDepthStencil = swapchain_desc->enable_auto_depth_stencil;
224 present_parameters->AutoDepthStencilFormat
225 = d3dformat_from_wined3dformat(swapchain_desc->auto_depth_stencil_format);
226 present_parameters->Flags = swapchain_desc->flags & D3DPRESENTFLAGS_MASK;
227 present_parameters->FullScreen_RefreshRateInHz = swapchain_desc->refresh_rate;
228 present_parameters->FullScreen_PresentationInterval = presentation_interval;
231 static enum wined3d_swap_effect wined3dswapeffect_from_d3dswapeffect(D3DSWAPEFFECT effect)
233 switch (effect)
235 case D3DSWAPEFFECT_DISCARD:
236 return WINED3D_SWAP_EFFECT_DISCARD;
237 case D3DSWAPEFFECT_FLIP:
238 return WINED3D_SWAP_EFFECT_SEQUENTIAL;
239 case D3DSWAPEFFECT_COPY:
240 return WINED3D_SWAP_EFFECT_COPY;
241 case D3DSWAPEFFECT_COPY_VSYNC:
242 return WINED3D_SWAP_EFFECT_COPY_VSYNC;
243 default:
244 FIXME("Unhandled swap effect %#x.\n", effect);
245 return WINED3D_SWAP_EFFECT_SEQUENTIAL;
249 static enum wined3d_swap_interval wined3dswapinterval_from_d3d(DWORD interval)
251 switch (interval)
253 case D3DPRESENT_INTERVAL_IMMEDIATE:
254 return WINED3D_SWAP_INTERVAL_IMMEDIATE;
255 case D3DPRESENT_INTERVAL_ONE:
256 return WINED3D_SWAP_INTERVAL_ONE;
257 case D3DPRESENT_INTERVAL_TWO:
258 return WINED3D_SWAP_INTERVAL_TWO;
259 case D3DPRESENT_INTERVAL_THREE:
260 return WINED3D_SWAP_INTERVAL_THREE;
261 case D3DPRESENT_INTERVAL_FOUR:
262 return WINED3D_SWAP_INTERVAL_FOUR;
263 default:
264 FIXME("Unhandled presentation interval %#x.\n", interval);
265 case D3DPRESENT_INTERVAL_DEFAULT:
266 return WINED3D_SWAP_INTERVAL_DEFAULT;
270 static BOOL wined3d_swapchain_desc_from_present_parameters(struct wined3d_swapchain_desc *swapchain_desc,
271 const D3DPRESENT_PARAMETERS *present_parameters)
273 if (!present_parameters->SwapEffect || present_parameters->SwapEffect > D3DSWAPEFFECT_COPY_VSYNC)
275 WARN("Invalid swap effect %u passed.\n", present_parameters->SwapEffect);
276 return FALSE;
278 if (present_parameters->BackBufferCount > 3
279 || ((present_parameters->SwapEffect == D3DSWAPEFFECT_COPY
280 || present_parameters->SwapEffect == D3DSWAPEFFECT_COPY_VSYNC)
281 && present_parameters->BackBufferCount > 1))
283 WARN("Invalid backbuffer count %u.\n", present_parameters->BackBufferCount);
284 return FALSE;
286 switch (present_parameters->FullScreen_PresentationInterval)
288 case D3DPRESENT_INTERVAL_DEFAULT:
289 case D3DPRESENT_INTERVAL_ONE:
290 case D3DPRESENT_INTERVAL_TWO:
291 case D3DPRESENT_INTERVAL_THREE:
292 case D3DPRESENT_INTERVAL_FOUR:
293 case D3DPRESENT_INTERVAL_IMMEDIATE:
294 break;
295 default:
296 WARN("Invalid presentation interval %#x.\n",
297 present_parameters->FullScreen_PresentationInterval);
298 return FALSE;
301 swapchain_desc->backbuffer_width = present_parameters->BackBufferWidth;
302 swapchain_desc->backbuffer_height = present_parameters->BackBufferHeight;
303 swapchain_desc->backbuffer_format = wined3dformat_from_d3dformat(present_parameters->BackBufferFormat);
304 swapchain_desc->backbuffer_count = max(1, present_parameters->BackBufferCount);
305 swapchain_desc->backbuffer_bind_flags = WINED3D_BIND_RENDER_TARGET;
306 swapchain_desc->multisample_type = present_parameters->MultiSampleType;
307 swapchain_desc->multisample_quality = 0; /* d3d9 only */
308 swapchain_desc->swap_effect = wined3dswapeffect_from_d3dswapeffect(present_parameters->SwapEffect);
309 swapchain_desc->device_window = present_parameters->hDeviceWindow;
310 swapchain_desc->windowed = present_parameters->Windowed;
311 swapchain_desc->enable_auto_depth_stencil = present_parameters->EnableAutoDepthStencil;
312 swapchain_desc->auto_depth_stencil_format
313 = wined3dformat_from_d3dformat(present_parameters->AutoDepthStencilFormat);
314 swapchain_desc->flags
315 = (present_parameters->Flags & D3DPRESENTFLAGS_MASK) | WINED3D_SWAPCHAIN_ALLOW_MODE_SWITCH;
316 swapchain_desc->refresh_rate = present_parameters->FullScreen_RefreshRateInHz;
317 swapchain_desc->auto_restore_display_mode = TRUE;
319 if (present_parameters->Flags & ~D3DPRESENTFLAGS_MASK)
320 FIXME("Unhandled flags %#x.\n", present_parameters->Flags & ~D3DPRESENTFLAGS_MASK);
322 return TRUE;
325 void d3dcaps_from_wined3dcaps(D3DCAPS8 *caps, const struct wined3d_caps *wined3d_caps)
327 caps->DeviceType = (D3DDEVTYPE)wined3d_caps->DeviceType;
328 caps->AdapterOrdinal = wined3d_caps->AdapterOrdinal;
329 caps->Caps = wined3d_caps->Caps;
330 caps->Caps2 = wined3d_caps->Caps2;
331 caps->Caps3 = wined3d_caps->Caps3;
332 caps->PresentationIntervals = D3DPRESENT_INTERVAL_IMMEDIATE | D3DPRESENT_INTERVAL_ONE;
333 caps->CursorCaps = wined3d_caps->CursorCaps;
334 caps->DevCaps = wined3d_caps->DevCaps;
335 caps->PrimitiveMiscCaps = wined3d_caps->PrimitiveMiscCaps;
336 caps->RasterCaps = wined3d_caps->RasterCaps;
337 caps->ZCmpCaps = wined3d_caps->ZCmpCaps;
338 caps->SrcBlendCaps = wined3d_caps->SrcBlendCaps;
339 caps->DestBlendCaps = wined3d_caps->DestBlendCaps;
340 caps->AlphaCmpCaps = wined3d_caps->AlphaCmpCaps;
341 caps->ShadeCaps = wined3d_caps->ShadeCaps;
342 caps->TextureCaps = wined3d_caps->TextureCaps;
343 caps->TextureFilterCaps = wined3d_caps->TextureFilterCaps;
344 caps->CubeTextureFilterCaps = wined3d_caps->CubeTextureFilterCaps;
345 caps->VolumeTextureFilterCaps = wined3d_caps->VolumeTextureFilterCaps;
346 caps->TextureAddressCaps = wined3d_caps->TextureAddressCaps;
347 caps->VolumeTextureAddressCaps = wined3d_caps->VolumeTextureAddressCaps;
348 caps->LineCaps = wined3d_caps->LineCaps;
349 caps->MaxTextureWidth = wined3d_caps->MaxTextureWidth;
350 caps->MaxTextureHeight = wined3d_caps->MaxTextureHeight;
351 caps->MaxVolumeExtent = wined3d_caps->MaxVolumeExtent;
352 caps->MaxTextureRepeat = wined3d_caps->MaxTextureRepeat;
353 caps->MaxTextureAspectRatio = wined3d_caps->MaxTextureAspectRatio;
354 caps->MaxAnisotropy = wined3d_caps->MaxAnisotropy;
355 caps->MaxVertexW = wined3d_caps->MaxVertexW;
356 caps->GuardBandLeft = wined3d_caps->GuardBandLeft;
357 caps->GuardBandTop = wined3d_caps->GuardBandTop;
358 caps->GuardBandRight = wined3d_caps->GuardBandRight;
359 caps->GuardBandBottom = wined3d_caps->GuardBandBottom;
360 caps->ExtentsAdjust = wined3d_caps->ExtentsAdjust;
361 caps->StencilCaps = wined3d_caps->StencilCaps;
362 caps->FVFCaps = wined3d_caps->FVFCaps;
363 caps->TextureOpCaps = wined3d_caps->TextureOpCaps;
364 caps->MaxTextureBlendStages = wined3d_caps->MaxTextureBlendStages;
365 caps->MaxSimultaneousTextures = wined3d_caps->MaxSimultaneousTextures;
366 caps->VertexProcessingCaps = wined3d_caps->VertexProcessingCaps;
367 caps->MaxActiveLights = wined3d_caps->MaxActiveLights;
368 caps->MaxUserClipPlanes = wined3d_caps->MaxUserClipPlanes;
369 caps->MaxVertexBlendMatrices = wined3d_caps->MaxVertexBlendMatrices;
370 caps->MaxVertexBlendMatrixIndex = wined3d_caps->MaxVertexBlendMatrixIndex;
371 caps->MaxPointSize = wined3d_caps->MaxPointSize;
372 caps->MaxPrimitiveCount = wined3d_caps->MaxPrimitiveCount;
373 caps->MaxVertexIndex = wined3d_caps->MaxVertexIndex;
374 caps->MaxStreams = wined3d_caps->MaxStreams;
375 caps->MaxStreamStride = wined3d_caps->MaxStreamStride;
376 caps->VertexShaderVersion = wined3d_caps->VertexShaderVersion;
377 caps->MaxVertexShaderConst = wined3d_caps->MaxVertexShaderConst;
378 caps->PixelShaderVersion = wined3d_caps->PixelShaderVersion;
379 caps->MaxPixelShaderValue = wined3d_caps->PixelShader1xMaxValue;
381 caps->Caps2 &= D3DCAPS2_CANCALIBRATEGAMMA | D3DCAPS2_CANRENDERWINDOWED
382 | D3DCAPS2_CANMANAGERESOURCE | D3DCAPS2_DYNAMICTEXTURES | D3DCAPS2_FULLSCREENGAMMA
383 | D3DCAPS2_NO2DDURING3DSCENE | D3DCAPS2_RESERVED;
384 caps->Caps3 &= D3DCAPS3_ALPHA_FULLSCREEN_FLIP_OR_DISCARD | D3DCAPS3_RESERVED;
385 caps->PrimitiveMiscCaps &= D3DPMISCCAPS_MASKZ | D3DPMISCCAPS_LINEPATTERNREP
386 | D3DPMISCCAPS_CULLNONE | D3DPMISCCAPS_CULLCW | D3DPMISCCAPS_CULLCCW
387 | D3DPMISCCAPS_COLORWRITEENABLE | D3DPMISCCAPS_CLIPPLANESCALEDPOINTS
388 | D3DPMISCCAPS_CLIPTLVERTS | D3DPMISCCAPS_TSSARGTEMP | D3DPMISCCAPS_BLENDOP
389 | D3DPMISCCAPS_NULLREFERENCE;
390 caps->RasterCaps &= D3DPRASTERCAPS_DITHER | D3DPRASTERCAPS_PAT | D3DPRASTERCAPS_ZTEST
391 | D3DPRASTERCAPS_FOGVERTEX | D3DPRASTERCAPS_FOGTABLE | D3DPRASTERCAPS_ANTIALIASEDGES
392 | D3DPRASTERCAPS_MIPMAPLODBIAS | D3DPRASTERCAPS_ZBIAS | D3DPRASTERCAPS_ZBUFFERLESSHSR
393 | D3DPRASTERCAPS_FOGRANGE | D3DPRASTERCAPS_ANISOTROPY | D3DPRASTERCAPS_WBUFFER
394 | D3DPRASTERCAPS_WFOG | D3DPRASTERCAPS_ZFOG | D3DPRASTERCAPS_COLORPERSPECTIVE
395 | D3DPRASTERCAPS_STRETCHBLTMULTISAMPLE;
396 caps->SrcBlendCaps &= D3DPBLENDCAPS_ZERO | D3DPBLENDCAPS_ONE | D3DPBLENDCAPS_SRCCOLOR
397 | D3DPBLENDCAPS_INVSRCCOLOR | D3DPBLENDCAPS_SRCALPHA | D3DPBLENDCAPS_INVSRCALPHA
398 | D3DPBLENDCAPS_DESTALPHA | D3DPBLENDCAPS_INVDESTALPHA | D3DPBLENDCAPS_DESTCOLOR
399 | D3DPBLENDCAPS_INVDESTCOLOR | D3DPBLENDCAPS_SRCALPHASAT | D3DPBLENDCAPS_BOTHSRCALPHA
400 | D3DPBLENDCAPS_BOTHINVSRCALPHA;
401 caps->DestBlendCaps &= D3DPBLENDCAPS_ZERO | D3DPBLENDCAPS_ONE | D3DPBLENDCAPS_SRCCOLOR
402 | D3DPBLENDCAPS_INVSRCCOLOR | D3DPBLENDCAPS_SRCALPHA | D3DPBLENDCAPS_INVSRCALPHA
403 | D3DPBLENDCAPS_DESTALPHA | D3DPBLENDCAPS_INVDESTALPHA | D3DPBLENDCAPS_DESTCOLOR
404 | D3DPBLENDCAPS_INVDESTCOLOR | D3DPBLENDCAPS_SRCALPHASAT | D3DPBLENDCAPS_BOTHSRCALPHA
405 | D3DPBLENDCAPS_BOTHINVSRCALPHA;
406 caps->TextureCaps &= D3DPTEXTURECAPS_PERSPECTIVE | D3DPTEXTURECAPS_POW2 | D3DPTEXTURECAPS_ALPHA
407 | D3DPTEXTURECAPS_SQUAREONLY | D3DPTEXTURECAPS_TEXREPEATNOTSCALEDBYSIZE
408 | D3DPTEXTURECAPS_ALPHAPALETTE | D3DPTEXTURECAPS_NONPOW2CONDITIONAL
409 | D3DPTEXTURECAPS_PROJECTED | D3DPTEXTURECAPS_CUBEMAP | D3DPTEXTURECAPS_VOLUMEMAP
410 | D3DPTEXTURECAPS_MIPMAP | D3DPTEXTURECAPS_MIPVOLUMEMAP | D3DPTEXTURECAPS_MIPCUBEMAP
411 | D3DPTEXTURECAPS_CUBEMAP_POW2 | D3DPTEXTURECAPS_VOLUMEMAP_POW2;
412 caps->TextureFilterCaps &= 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->CubeTextureFilterCaps &= D3DPTFILTERCAPS_MINFPOINT | D3DPTFILTERCAPS_MINFLINEAR
418 | D3DPTFILTERCAPS_MINFANISOTROPIC | D3DPTFILTERCAPS_MIPFPOINT
419 | D3DPTFILTERCAPS_MIPFLINEAR | D3DPTFILTERCAPS_MAGFPOINT | D3DPTFILTERCAPS_MAGFLINEAR
420 | D3DPTFILTERCAPS_MAGFANISOTROPIC | D3DPTFILTERCAPS_MAGFAFLATCUBIC
421 | D3DPTFILTERCAPS_MAGFGAUSSIANCUBIC;
422 caps->VolumeTextureFilterCaps &= D3DPTFILTERCAPS_MINFPOINT | D3DPTFILTERCAPS_MINFLINEAR
423 | D3DPTFILTERCAPS_MINFANISOTROPIC | D3DPTFILTERCAPS_MIPFPOINT
424 | D3DPTFILTERCAPS_MIPFLINEAR | D3DPTFILTERCAPS_MAGFPOINT | D3DPTFILTERCAPS_MAGFLINEAR
425 | D3DPTFILTERCAPS_MAGFANISOTROPIC | D3DPTFILTERCAPS_MAGFAFLATCUBIC
426 | D3DPTFILTERCAPS_MAGFGAUSSIANCUBIC;
427 caps->StencilCaps &= ~WINED3DSTENCILCAPS_TWOSIDED;
428 caps->VertexProcessingCaps &= D3DVTXPCAPS_TEXGEN | D3DVTXPCAPS_MATERIALSOURCE7
429 | D3DVTXPCAPS_DIRECTIONALLIGHTS | D3DVTXPCAPS_POSITIONALLIGHTS | D3DVTXPCAPS_LOCALVIEWER
430 | D3DVTXPCAPS_TWEENING | D3DVTXPCAPS_NO_VSDT_UBYTE4;
432 /* D3D8 doesn't support SM 2.0 or higher, so clamp to 1.x */
433 if (caps->PixelShaderVersion)
434 caps->PixelShaderVersion = D3DPS_VERSION(1, 4);
435 else
436 caps->PixelShaderVersion = D3DPS_VERSION(0, 0);
437 if (caps->VertexShaderVersion)
438 caps->VertexShaderVersion = D3DVS_VERSION(1, 1);
439 else
440 caps->VertexShaderVersion = D3DVS_VERSION(0, 0);
441 caps->MaxVertexShaderConst = min(D3D8_MAX_VERTEX_SHADER_CONSTANTF, caps->MaxVertexShaderConst);
444 /* Handle table functions */
445 static DWORD d3d8_allocate_handle(struct d3d8_handle_table *t, void *object, enum d3d8_handle_type type)
447 struct d3d8_handle_entry *entry;
449 if (t->free_entries)
451 DWORD index = t->free_entries - t->entries;
452 /* Use a free handle */
453 entry = t->free_entries;
454 if (entry->type != D3D8_HANDLE_FREE)
456 ERR("Handle %u(%p) is in the free list, but has type %#x.\n", index, entry, entry->type);
457 return D3D8_INVALID_HANDLE;
459 t->free_entries = entry->object;
460 entry->object = object;
461 entry->type = type;
463 return index;
466 if (!(t->entry_count < t->table_size))
468 /* Grow the table */
469 UINT new_size = t->table_size + (t->table_size >> 1);
470 struct d3d8_handle_entry *new_entries;
472 if (!(new_entries = heap_realloc(t->entries, new_size * sizeof(*t->entries))))
474 ERR("Failed to grow the handle table.\n");
475 return D3D8_INVALID_HANDLE;
477 t->entries = new_entries;
478 t->table_size = new_size;
481 entry = &t->entries[t->entry_count];
482 entry->object = object;
483 entry->type = type;
485 return t->entry_count++;
488 static void *d3d8_free_handle(struct d3d8_handle_table *t, DWORD handle, enum d3d8_handle_type type)
490 struct d3d8_handle_entry *entry;
491 void *object;
493 if (handle == D3D8_INVALID_HANDLE || handle >= t->entry_count)
495 WARN("Invalid handle %u passed.\n", handle);
496 return NULL;
499 entry = &t->entries[handle];
500 if (entry->type != type)
502 WARN("Handle %u(%p) is not of type %#x.\n", handle, entry, type);
503 return NULL;
506 object = entry->object;
507 entry->object = t->free_entries;
508 entry->type = D3D8_HANDLE_FREE;
509 t->free_entries = entry;
511 return object;
514 static void *d3d8_get_object(struct d3d8_handle_table *t, DWORD handle, enum d3d8_handle_type type)
516 struct d3d8_handle_entry *entry;
518 if (handle == D3D8_INVALID_HANDLE || handle >= t->entry_count)
520 WARN("Invalid handle %u passed.\n", handle);
521 return NULL;
524 entry = &t->entries[handle];
525 if (entry->type != type)
527 WARN("Handle %u(%p) is not of type %#x.\n", handle, entry, type);
528 return NULL;
531 return entry->object;
534 static HRESULT WINAPI d3d8_device_QueryInterface(IDirect3DDevice8 *iface, REFIID riid, void **out)
536 TRACE("iface %p, riid %s, out %p.\n",
537 iface, debugstr_guid(riid), out);
539 if (IsEqualGUID(riid, &IID_IDirect3DDevice8)
540 || IsEqualGUID(riid, &IID_IUnknown))
542 IDirect3DDevice8_AddRef(iface);
543 *out = iface;
544 return S_OK;
547 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
549 *out = NULL;
550 return E_NOINTERFACE;
553 static ULONG WINAPI d3d8_device_AddRef(IDirect3DDevice8 *iface)
555 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
556 ULONG ref = InterlockedIncrement(&device->ref);
558 TRACE("%p increasing refcount to %u.\n", iface, ref);
560 return ref;
563 static ULONG WINAPI d3d8_device_Release(IDirect3DDevice8 *iface)
565 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
566 ULONG ref;
568 if (device->in_destruction)
569 return 0;
571 ref = InterlockedDecrement(&device->ref);
573 TRACE("%p decreasing refcount to %u.\n", iface, ref);
575 if (!ref)
577 IDirect3D8 *parent = device->d3d_parent;
578 unsigned i;
580 TRACE("Releasing wined3d device %p.\n", device->wined3d_device);
582 wined3d_mutex_lock();
584 device->in_destruction = TRUE;
586 for (i = 0; i < device->numConvertedDecls; ++i)
588 d3d8_vertex_declaration_destroy(device->decls[i].declaration);
590 heap_free(device->decls);
592 if (device->vertex_buffer)
593 wined3d_buffer_decref(device->vertex_buffer);
594 if (device->index_buffer)
595 wined3d_buffer_decref(device->index_buffer);
597 wined3d_device_uninit_3d(device->wined3d_device);
598 wined3d_device_release_focus_window(device->wined3d_device);
599 wined3d_device_decref(device->wined3d_device);
600 heap_free(device->handle_table.entries);
601 heap_free(device);
603 wined3d_mutex_unlock();
605 IDirect3D8_Release(parent);
607 return ref;
610 static HRESULT WINAPI d3d8_device_TestCooperativeLevel(IDirect3DDevice8 *iface)
612 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
614 TRACE("iface %p.\n", iface);
616 TRACE("device state: %#x.\n", device->device_state);
618 switch (device->device_state)
620 default:
621 case D3D8_DEVICE_STATE_OK:
622 return D3D_OK;
623 case D3D8_DEVICE_STATE_LOST:
624 return D3DERR_DEVICELOST;
625 case D3D8_DEVICE_STATE_NOT_RESET:
626 return D3DERR_DEVICENOTRESET;
630 static UINT WINAPI d3d8_device_GetAvailableTextureMem(IDirect3DDevice8 *iface)
632 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
633 UINT ret;
635 TRACE("iface %p.\n", iface);
637 wined3d_mutex_lock();
638 ret = wined3d_device_get_available_texture_mem(device->wined3d_device);
639 wined3d_mutex_unlock();
641 return ret;
644 static HRESULT WINAPI d3d8_device_ResourceManagerDiscardBytes(IDirect3DDevice8 *iface, DWORD byte_count)
646 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
648 TRACE("iface %p, byte_count %u.\n", iface, byte_count);
650 if (byte_count)
651 FIXME("Byte count ignored.\n");
653 wined3d_mutex_lock();
654 wined3d_device_evict_managed_resources(device->wined3d_device);
655 wined3d_mutex_unlock();
657 return D3D_OK;
660 static HRESULT WINAPI d3d8_device_GetDirect3D(IDirect3DDevice8 *iface, IDirect3D8 **d3d8)
662 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
664 TRACE("iface %p, d3d8 %p.\n", iface, d3d8);
666 if (!d3d8)
667 return D3DERR_INVALIDCALL;
669 return IDirect3D8_QueryInterface(device->d3d_parent, &IID_IDirect3D8, (void **)d3d8);
672 static HRESULT WINAPI d3d8_device_GetDeviceCaps(IDirect3DDevice8 *iface, D3DCAPS8 *caps)
674 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
675 struct wined3d_caps wined3d_caps;
676 HRESULT hr;
678 TRACE("iface %p, caps %p.\n", iface, caps);
680 if (!caps)
681 return D3DERR_INVALIDCALL;
683 wined3d_mutex_lock();
684 hr = wined3d_device_get_device_caps(device->wined3d_device, &wined3d_caps);
685 wined3d_mutex_unlock();
687 d3dcaps_from_wined3dcaps(caps, &wined3d_caps);
689 return hr;
692 static HRESULT WINAPI d3d8_device_GetDisplayMode(IDirect3DDevice8 *iface, D3DDISPLAYMODE *mode)
694 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
695 struct wined3d_display_mode wined3d_mode;
696 HRESULT hr;
698 TRACE("iface %p, mode %p.\n", iface, mode);
700 wined3d_mutex_lock();
701 hr = wined3d_device_get_display_mode(device->wined3d_device, 0, &wined3d_mode, NULL);
702 wined3d_mutex_unlock();
704 if (SUCCEEDED(hr))
706 mode->Width = wined3d_mode.width;
707 mode->Height = wined3d_mode.height;
708 mode->RefreshRate = wined3d_mode.refresh_rate;
709 mode->Format = d3dformat_from_wined3dformat(wined3d_mode.format_id);
712 return hr;
715 static HRESULT WINAPI d3d8_device_GetCreationParameters(IDirect3DDevice8 *iface,
716 D3DDEVICE_CREATION_PARAMETERS *parameters)
718 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
720 TRACE("iface %p, parameters %p.\n", iface, parameters);
722 wined3d_mutex_lock();
723 wined3d_device_get_creation_parameters(device->wined3d_device,
724 (struct wined3d_device_creation_parameters *)parameters);
725 wined3d_mutex_unlock();
727 return D3D_OK;
730 static HRESULT WINAPI d3d8_device_SetCursorProperties(IDirect3DDevice8 *iface,
731 UINT hotspot_x, UINT hotspot_y, IDirect3DSurface8 *bitmap)
733 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
734 struct d3d8_surface *bitmap_impl = unsafe_impl_from_IDirect3DSurface8(bitmap);
735 HRESULT hr;
737 TRACE("iface %p, hotspot_x %u, hotspot_y %u, bitmap %p.\n",
738 iface, hotspot_x, hotspot_y, bitmap);
740 if (!bitmap)
742 WARN("No cursor bitmap, returning D3DERR_INVALIDCALL.\n");
743 return D3DERR_INVALIDCALL;
746 wined3d_mutex_lock();
747 hr = wined3d_device_set_cursor_properties(device->wined3d_device,
748 hotspot_x, hotspot_y, bitmap_impl->wined3d_texture, bitmap_impl->sub_resource_idx);
749 wined3d_mutex_unlock();
751 return hr;
754 static void WINAPI d3d8_device_SetCursorPosition(IDirect3DDevice8 *iface, UINT x, UINT y, DWORD flags)
756 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
758 TRACE("iface %p, x %u, y %u, flags %#x.\n", iface, x, y, flags);
760 wined3d_mutex_lock();
761 wined3d_device_set_cursor_position(device->wined3d_device, x, y, flags);
762 wined3d_mutex_unlock();
765 static BOOL WINAPI d3d8_device_ShowCursor(IDirect3DDevice8 *iface, BOOL show)
767 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
768 BOOL ret;
770 TRACE("iface %p, show %#x.\n", iface, show);
772 wined3d_mutex_lock();
773 ret = wined3d_device_show_cursor(device->wined3d_device, show);
774 wined3d_mutex_unlock();
776 return ret;
779 static HRESULT WINAPI d3d8_device_CreateAdditionalSwapChain(IDirect3DDevice8 *iface,
780 D3DPRESENT_PARAMETERS *present_parameters, IDirect3DSwapChain8 **swapchain)
782 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
783 struct wined3d_swapchain_desc desc;
784 struct d3d8_swapchain *object;
785 unsigned int swap_interval;
786 unsigned int i, count;
787 HRESULT hr;
789 TRACE("iface %p, present_parameters %p, swapchain %p.\n",
790 iface, present_parameters, swapchain);
792 if (!present_parameters->Windowed)
794 WARN("Trying to create an additional fullscreen swapchain, returning D3DERR_INVALIDCALL.\n");
795 return D3DERR_INVALIDCALL;
798 wined3d_mutex_lock();
799 count = wined3d_device_get_swapchain_count(device->wined3d_device);
800 for (i = 0; i < count; ++i)
802 struct wined3d_swapchain *wined3d_swapchain;
804 wined3d_swapchain = wined3d_device_get_swapchain(device->wined3d_device, i);
805 wined3d_swapchain_get_desc(wined3d_swapchain, &desc);
807 if (!desc.windowed)
809 wined3d_mutex_unlock();
810 WARN("Trying to create an additional swapchain in fullscreen mode, returning D3DERR_INVALIDCALL.\n");
811 return D3DERR_INVALIDCALL;
814 wined3d_mutex_unlock();
816 if (!wined3d_swapchain_desc_from_present_parameters(&desc, present_parameters))
817 return D3DERR_INVALIDCALL;
818 swap_interval = wined3dswapinterval_from_d3d(present_parameters->FullScreen_PresentationInterval);
819 if (SUCCEEDED(hr = d3d8_swapchain_create(device, &desc, swap_interval, &object)))
820 *swapchain = &object->IDirect3DSwapChain8_iface;
821 present_parameters_from_wined3d_swapchain_desc(present_parameters,
822 &desc, present_parameters->FullScreen_PresentationInterval);
824 return hr;
827 static HRESULT CDECL reset_enum_callback(struct wined3d_resource *resource)
829 struct d3d8_vertexbuffer *vertex_buffer;
830 struct d3d8_indexbuffer *index_buffer;
831 struct wined3d_resource_desc desc;
832 IDirect3DBaseTexture8 *texture;
833 struct d3d8_surface *surface;
834 IUnknown *parent;
836 wined3d_resource_get_desc(resource, &desc);
837 if (desc.access & WINED3D_RESOURCE_ACCESS_CPU)
838 return D3D_OK;
840 if (desc.resource_type != WINED3D_RTYPE_TEXTURE_2D)
842 if (desc.bind_flags & WINED3D_BIND_VERTEX_BUFFER)
844 vertex_buffer = wined3d_resource_get_parent(resource);
845 if (vertex_buffer && vertex_buffer->draw_buffer)
846 return D3D_OK;
848 if (desc.bind_flags & WINED3D_BIND_INDEX_BUFFER)
850 index_buffer = wined3d_resource_get_parent(resource);
851 if (index_buffer && index_buffer->draw_buffer)
852 return D3D_OK;
855 WARN("Resource %p in pool D3DPOOL_DEFAULT blocks the Reset call.\n", resource);
856 return D3DERR_DEVICELOST;
859 parent = wined3d_resource_get_parent(resource);
860 if (parent && SUCCEEDED(IUnknown_QueryInterface(parent, &IID_IDirect3DBaseTexture8, (void **)&texture)))
862 IDirect3DBaseTexture8_Release(texture);
863 WARN("Texture %p (resource %p) in pool D3DPOOL_DEFAULT blocks the Reset call.\n", texture, resource);
864 return D3DERR_DEVICELOST;
867 surface = wined3d_texture_get_sub_resource_parent(wined3d_texture_from_resource(resource), 0);
868 if (!surface->resource.refcount)
869 return D3D_OK;
871 WARN("Surface %p in pool D3DPOOL_DEFAULT blocks the Reset call.\n", surface);
872 return D3DERR_DEVICELOST;
875 static HRESULT WINAPI d3d8_device_Reset(IDirect3DDevice8 *iface,
876 D3DPRESENT_PARAMETERS *present_parameters)
878 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
879 struct wined3d_swapchain_desc swapchain_desc;
880 HRESULT hr;
882 TRACE("iface %p, present_parameters %p.\n", iface, present_parameters);
884 if (device->device_state == D3D8_DEVICE_STATE_LOST)
886 WARN("App not active, returning D3DERR_DEVICELOST.\n");
887 return D3DERR_DEVICELOST;
889 if (!wined3d_swapchain_desc_from_present_parameters(&swapchain_desc, present_parameters))
890 return D3DERR_INVALIDCALL;
892 wined3d_mutex_lock();
894 if (device->vertex_buffer)
896 wined3d_buffer_decref(device->vertex_buffer);
897 device->vertex_buffer = NULL;
898 device->vertex_buffer_size = 0;
900 if (device->index_buffer)
902 wined3d_buffer_decref(device->index_buffer);
903 device->index_buffer = NULL;
904 device->index_buffer_size = 0;
907 if (SUCCEEDED(hr = wined3d_device_reset(device->wined3d_device, &swapchain_desc,
908 NULL, reset_enum_callback, TRUE)))
910 device->recording = FALSE;
911 present_parameters->BackBufferCount = swapchain_desc.backbuffer_count;
912 device->implicit_swapchain->swap_interval
913 = wined3dswapinterval_from_d3d(present_parameters->FullScreen_PresentationInterval);
914 wined3d_device_set_render_state(device->wined3d_device, WINED3D_RS_POINTSIZE_MIN, 0);
915 wined3d_device_set_render_state(device->wined3d_device, WINED3D_RS_ZENABLE,
916 !!swapchain_desc.enable_auto_depth_stencil);
917 device->device_state = D3D8_DEVICE_STATE_OK;
919 else
921 device->device_state = D3D8_DEVICE_STATE_NOT_RESET;
923 wined3d_mutex_unlock();
925 return hr;
928 static HRESULT WINAPI d3d8_device_Present(IDirect3DDevice8 *iface, const RECT *src_rect,
929 const RECT *dst_rect, HWND dst_window_override, const RGNDATA *dirty_region)
931 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
933 TRACE("iface %p, src_rect %s, dst_rect %s, dst_window_override %p, dirty_region %p.\n",
934 iface, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect), dst_window_override, dirty_region);
936 /* Fraps does not hook IDirect3DDevice8::Present regardless of the hotpatch
937 * attribute. It only hooks IDirect3DSwapChain8::Present. Yet it properly
938 * shows a framerate on Windows in applications that only call the device
939 * method, like e.g. the dx8 sdk samples. The conclusion is that native
940 * calls the swapchain's public method from the device. */
941 return IDirect3DSwapChain8_Present(&device->implicit_swapchain->IDirect3DSwapChain8_iface,
942 src_rect, dst_rect, dst_window_override, dirty_region);
945 static HRESULT WINAPI d3d8_device_GetBackBuffer(IDirect3DDevice8 *iface,
946 UINT backbuffer_idx, D3DBACKBUFFER_TYPE backbuffer_type, IDirect3DSurface8 **backbuffer)
948 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
949 struct wined3d_swapchain *wined3d_swapchain;
950 struct wined3d_texture *wined3d_texture;
951 struct d3d8_surface *surface_impl;
953 TRACE("iface %p, backbuffer_idx %u, backbuffer_type %#x, backbuffer %p.\n",
954 iface, backbuffer_idx, backbuffer_type, backbuffer);
956 /* backbuffer_type is ignored by native. */
958 /* No need to check for backbuffer == NULL, Windows crashes in that case. */
959 wined3d_mutex_lock();
961 wined3d_swapchain = device->implicit_swapchain->wined3d_swapchain;
962 if (!(wined3d_texture = wined3d_swapchain_get_back_buffer(wined3d_swapchain, backbuffer_idx)))
964 wined3d_mutex_unlock();
965 *backbuffer = NULL;
966 return D3DERR_INVALIDCALL;
969 surface_impl = wined3d_texture_get_sub_resource_parent(wined3d_texture, 0);
970 *backbuffer = &surface_impl->IDirect3DSurface8_iface;
971 IDirect3DSurface8_AddRef(*backbuffer);
973 wined3d_mutex_unlock();
974 return D3D_OK;
977 static HRESULT WINAPI d3d8_device_GetRasterStatus(IDirect3DDevice8 *iface, D3DRASTER_STATUS *raster_status)
979 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
980 HRESULT hr;
982 TRACE("iface %p, raster_status %p.\n", iface, raster_status);
984 wined3d_mutex_lock();
985 hr = wined3d_device_get_raster_status(device->wined3d_device, 0, (struct wined3d_raster_status *)raster_status);
986 wined3d_mutex_unlock();
988 return hr;
991 static void WINAPI d3d8_device_SetGammaRamp(IDirect3DDevice8 *iface, DWORD flags, const D3DGAMMARAMP *ramp)
993 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
995 TRACE("iface %p, flags %#x, ramp %p.\n", iface, flags, ramp);
997 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
998 wined3d_mutex_lock();
999 wined3d_device_set_gamma_ramp(device->wined3d_device, 0, flags, (const struct wined3d_gamma_ramp *)ramp);
1000 wined3d_mutex_unlock();
1003 static void WINAPI d3d8_device_GetGammaRamp(IDirect3DDevice8 *iface, D3DGAMMARAMP *ramp)
1005 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1007 TRACE("iface %p, ramp %p.\n", iface, ramp);
1009 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
1010 wined3d_mutex_lock();
1011 wined3d_device_get_gamma_ramp(device->wined3d_device, 0, (struct wined3d_gamma_ramp *)ramp);
1012 wined3d_mutex_unlock();
1015 static HRESULT WINAPI d3d8_device_CreateTexture(IDirect3DDevice8 *iface,
1016 UINT width, UINT height, UINT levels, DWORD usage, D3DFORMAT format,
1017 D3DPOOL pool, IDirect3DTexture8 **texture)
1019 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1020 struct d3d8_texture *object;
1021 HRESULT hr;
1023 TRACE("iface %p, width %u, height %u, levels %u, usage %#x, format %#x, pool %#x, texture %p.\n",
1024 iface, width, height, levels, usage, format, pool, texture);
1026 if (!format)
1027 return D3DERR_INVALIDCALL;
1029 *texture = NULL;
1030 if (!(object = heap_alloc_zero(sizeof(*object))))
1031 return D3DERR_OUTOFVIDEOMEMORY;
1033 hr = texture_init(object, device, width, height, levels, usage, format, pool);
1034 if (FAILED(hr))
1036 WARN("Failed to initialize texture, hr %#x.\n", hr);
1037 heap_free(object);
1038 return hr;
1041 TRACE("Created texture %p.\n", object);
1042 *texture = (IDirect3DTexture8 *)&object->IDirect3DBaseTexture8_iface;
1044 return D3D_OK;
1047 static HRESULT WINAPI d3d8_device_CreateVolumeTexture(IDirect3DDevice8 *iface,
1048 UINT width, UINT height, UINT depth, UINT levels, DWORD usage, D3DFORMAT format,
1049 D3DPOOL pool, IDirect3DVolumeTexture8 **texture)
1051 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1052 struct d3d8_texture *object;
1053 HRESULT hr;
1055 TRACE("iface %p, width %u, height %u, depth %u, levels %u, usage %#x, format %#x, pool %#x, texture %p.\n",
1056 iface, width, height, depth, levels, usage, format, pool, texture);
1058 if (!format)
1059 return D3DERR_INVALIDCALL;
1061 *texture = NULL;
1062 if (!(object = heap_alloc_zero(sizeof(*object))))
1063 return D3DERR_OUTOFVIDEOMEMORY;
1065 hr = volumetexture_init(object, device, width, height, depth, levels, usage, format, pool);
1066 if (FAILED(hr))
1068 WARN("Failed to initialize volume texture, hr %#x.\n", hr);
1069 heap_free(object);
1070 return hr;
1073 TRACE("Created volume texture %p.\n", object);
1074 *texture = (IDirect3DVolumeTexture8 *)&object->IDirect3DBaseTexture8_iface;
1076 return D3D_OK;
1079 static HRESULT WINAPI d3d8_device_CreateCubeTexture(IDirect3DDevice8 *iface, UINT edge_length,
1080 UINT levels, DWORD usage, D3DFORMAT format, D3DPOOL pool, IDirect3DCubeTexture8 **texture)
1082 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1083 struct d3d8_texture *object;
1084 HRESULT hr;
1086 TRACE("iface %p, edge_length %u, levels %u, usage %#x, format %#x, pool %#x, texture %p.\n",
1087 iface, edge_length, levels, usage, format, pool, texture);
1089 if (!format)
1090 return D3DERR_INVALIDCALL;
1092 *texture = NULL;
1093 if (!(object = heap_alloc_zero(sizeof(*object))))
1094 return D3DERR_OUTOFVIDEOMEMORY;
1096 hr = cubetexture_init(object, device, edge_length, levels, usage, format, pool);
1097 if (FAILED(hr))
1099 WARN("Failed to initialize cube texture, hr %#x.\n", hr);
1100 heap_free(object);
1101 return hr;
1104 TRACE("Created cube texture %p.\n", object);
1105 *texture = (IDirect3DCubeTexture8 *)&object->IDirect3DBaseTexture8_iface;
1107 return hr;
1110 static HRESULT WINAPI d3d8_device_CreateVertexBuffer(IDirect3DDevice8 *iface, UINT size,
1111 DWORD usage, DWORD fvf, D3DPOOL pool, IDirect3DVertexBuffer8 **buffer)
1113 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1114 struct d3d8_vertexbuffer *object;
1115 HRESULT hr;
1117 TRACE("iface %p, size %u, usage %#x, fvf %#x, pool %#x, buffer %p.\n",
1118 iface, size, usage, fvf, pool, buffer);
1120 if (!(object = heap_alloc_zero(sizeof(*object))))
1121 return D3DERR_OUTOFVIDEOMEMORY;
1123 hr = vertexbuffer_init(object, device, size, usage, fvf, pool);
1124 if (FAILED(hr))
1126 WARN("Failed to initialize vertex buffer, hr %#x.\n", hr);
1127 heap_free(object);
1128 return hr;
1131 TRACE("Created vertex buffer %p.\n", object);
1132 *buffer = &object->IDirect3DVertexBuffer8_iface;
1134 return D3D_OK;
1137 static HRESULT WINAPI d3d8_device_CreateIndexBuffer(IDirect3DDevice8 *iface, UINT size,
1138 DWORD usage, D3DFORMAT format, D3DPOOL pool, IDirect3DIndexBuffer8 **buffer)
1140 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1141 struct d3d8_indexbuffer *object;
1142 HRESULT hr;
1144 TRACE("iface %p, size %u, usage %#x, format %#x, pool %#x, buffer %p.\n",
1145 iface, size, usage, format, pool, buffer);
1147 if (!(object = heap_alloc_zero(sizeof(*object))))
1148 return D3DERR_OUTOFVIDEOMEMORY;
1150 hr = indexbuffer_init(object, device, size, usage, format, pool);
1151 if (FAILED(hr))
1153 WARN("Failed to initialize index buffer, hr %#x.\n", hr);
1154 heap_free(object);
1155 return hr;
1158 TRACE("Created index buffer %p.\n", object);
1159 *buffer = &object->IDirect3DIndexBuffer8_iface;
1161 return D3D_OK;
1164 static HRESULT d3d8_device_create_surface(struct d3d8_device *device, enum wined3d_format_id format,
1165 enum wined3d_multisample_type multisample_type, unsigned int bind_flags, unsigned int access,
1166 unsigned int width, unsigned int height, IDirect3DSurface8 **surface)
1168 struct wined3d_resource_desc desc;
1169 struct d3d8_surface *surface_impl;
1170 struct wined3d_texture *texture;
1171 HRESULT hr;
1173 TRACE("device %p, format %#x, multisample_type %#x, bind_flags %#x, "
1174 "access %#x, width %u, height %u, surface %p.\n",
1175 device, format, multisample_type, bind_flags, access, width, height, surface);
1177 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
1178 desc.format = format;
1179 desc.multisample_type = multisample_type;
1180 desc.multisample_quality = 0;
1181 desc.usage = 0;
1182 desc.bind_flags = bind_flags;
1183 desc.access = access;
1184 desc.width = width;
1185 desc.height = height;
1186 desc.depth = 1;
1187 desc.size = 0;
1189 wined3d_mutex_lock();
1191 if (FAILED(hr = wined3d_texture_create(device->wined3d_device, &desc,
1192 1, 1, 0, NULL, NULL, &d3d8_null_wined3d_parent_ops, &texture)))
1194 wined3d_mutex_unlock();
1195 WARN("Failed to create texture, hr %#x.\n", hr);
1196 return hr;
1199 surface_impl = wined3d_texture_get_sub_resource_parent(texture, 0);
1200 surface_impl->parent_device = &device->IDirect3DDevice8_iface;
1201 *surface = &surface_impl->IDirect3DSurface8_iface;
1202 IDirect3DSurface8_AddRef(*surface);
1203 wined3d_texture_decref(texture);
1205 wined3d_mutex_unlock();
1207 return D3D_OK;
1210 static HRESULT WINAPI d3d8_device_CreateRenderTarget(IDirect3DDevice8 *iface, UINT width,
1211 UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, BOOL lockable,
1212 IDirect3DSurface8 **surface)
1214 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1215 unsigned int access = WINED3D_RESOURCE_ACCESS_GPU;
1217 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, lockable %#x, surface %p.\n",
1218 iface, width, height, format, multisample_type, lockable, surface);
1220 if (!format)
1221 return D3DERR_INVALIDCALL;
1223 *surface = NULL;
1224 if (lockable)
1225 access |= WINED3D_RESOURCE_ACCESS_MAP_R | WINED3D_RESOURCE_ACCESS_MAP_W;
1227 return d3d8_device_create_surface(device, wined3dformat_from_d3dformat(format),
1228 multisample_type, WINED3D_BIND_RENDER_TARGET, access, width, height, surface);
1231 static HRESULT WINAPI d3d8_device_CreateDepthStencilSurface(IDirect3DDevice8 *iface,
1232 UINT width, UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type,
1233 IDirect3DSurface8 **surface)
1235 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1237 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, surface %p.\n",
1238 iface, width, height, format, multisample_type, surface);
1240 if (!format)
1241 return D3DERR_INVALIDCALL;
1243 *surface = NULL;
1245 return d3d8_device_create_surface(device, wined3dformat_from_d3dformat(format),
1246 multisample_type, WINED3D_BIND_DEPTH_STENCIL,
1247 WINED3D_RESOURCE_ACCESS_GPU, width, height, surface);
1250 /* IDirect3DDevice8Impl::CreateImageSurface returns surface with pool type SYSTEMMEM */
1251 static HRESULT WINAPI d3d8_device_CreateImageSurface(IDirect3DDevice8 *iface, UINT width,
1252 UINT height, D3DFORMAT format, IDirect3DSurface8 **surface)
1254 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1255 unsigned int access = WINED3D_RESOURCE_ACCESS_CPU
1256 | WINED3D_RESOURCE_ACCESS_MAP_R | WINED3D_RESOURCE_ACCESS_MAP_W;
1258 TRACE("iface %p, width %u, height %u, format %#x, surface %p.\n",
1259 iface, width, height, format, surface);
1261 *surface = NULL;
1263 return d3d8_device_create_surface(device, wined3dformat_from_d3dformat(format),
1264 WINED3D_MULTISAMPLE_NONE, 0, access, width, height, surface);
1267 static HRESULT WINAPI d3d8_device_CopyRects(IDirect3DDevice8 *iface,
1268 IDirect3DSurface8 *src_surface, const RECT *src_rects, UINT rect_count,
1269 IDirect3DSurface8 *dst_surface, const POINT *dst_points)
1271 struct d3d8_surface *src = unsafe_impl_from_IDirect3DSurface8(src_surface);
1272 struct d3d8_surface *dst = unsafe_impl_from_IDirect3DSurface8(dst_surface);
1273 enum wined3d_format_id src_format, dst_format;
1274 struct wined3d_sub_resource_desc wined3d_desc;
1275 UINT src_w, src_h;
1277 TRACE("iface %p, src_surface %p, src_rects %p, rect_count %u, dst_surface %p, dst_points %p.\n",
1278 iface, src_surface, src_rects, rect_count, dst_surface, dst_points);
1280 /* Check that the source texture is in WINED3D_POOL_SYSTEM_MEM and the
1281 * destination texture is in WINED3D_POOL_DEFAULT. */
1283 wined3d_mutex_lock();
1284 wined3d_texture_get_sub_resource_desc(src->wined3d_texture, src->sub_resource_idx, &wined3d_desc);
1285 if (wined3d_desc.bind_flags & WINED3D_BIND_DEPTH_STENCIL)
1287 WARN("Source %p is a depth stencil surface, returning D3DERR_INVALIDCALL.\n", src_surface);
1288 wined3d_mutex_unlock();
1289 return D3DERR_INVALIDCALL;
1291 src_format = wined3d_desc.format;
1292 src_w = wined3d_desc.width;
1293 src_h = wined3d_desc.height;
1295 wined3d_texture_get_sub_resource_desc(dst->wined3d_texture, dst->sub_resource_idx, &wined3d_desc);
1296 if (wined3d_desc.bind_flags & WINED3D_BIND_DEPTH_STENCIL)
1298 WARN("Destination %p is a depth stencil surface, returning D3DERR_INVALIDCALL.\n", dst_surface);
1299 wined3d_mutex_unlock();
1300 return D3DERR_INVALIDCALL;
1302 dst_format = wined3d_desc.format;
1304 /* Check that the source and destination formats match */
1305 if (src_format != dst_format)
1307 WARN("Source %p format must match the destination %p format, returning D3DERR_INVALIDCALL.\n",
1308 src_surface, dst_surface);
1309 wined3d_mutex_unlock();
1310 return D3DERR_INVALIDCALL;
1313 /* Quick if complete copy ... */
1314 if (!rect_count && !src_rects && !dst_points)
1316 RECT rect = {0, 0, src_w, src_h};
1317 wined3d_texture_blt(dst->wined3d_texture, dst->sub_resource_idx, &rect,
1318 src->wined3d_texture, src->sub_resource_idx, &rect, 0, NULL, WINED3D_TEXF_POINT);
1320 else
1322 unsigned int i;
1323 /* Copy rect by rect */
1324 if (src_rects && dst_points)
1326 for (i = 0; i < rect_count; ++i)
1328 UINT w = src_rects[i].right - src_rects[i].left;
1329 UINT h = src_rects[i].bottom - src_rects[i].top;
1330 RECT dst_rect = {dst_points[i].x, dst_points[i].y,
1331 dst_points[i].x + w, dst_points[i].y + h};
1333 wined3d_texture_blt(dst->wined3d_texture, dst->sub_resource_idx, &dst_rect,
1334 src->wined3d_texture, src->sub_resource_idx, &src_rects[i], 0, NULL, WINED3D_TEXF_POINT);
1337 else
1339 for (i = 0; i < rect_count; ++i)
1341 UINT w = src_rects[i].right - src_rects[i].left;
1342 UINT h = src_rects[i].bottom - src_rects[i].top;
1343 RECT dst_rect = {0, 0, w, h};
1345 wined3d_texture_blt(dst->wined3d_texture, dst->sub_resource_idx, &dst_rect,
1346 src->wined3d_texture, src->sub_resource_idx, &src_rects[i], 0, NULL, WINED3D_TEXF_POINT);
1350 wined3d_mutex_unlock();
1352 return WINED3D_OK;
1355 static HRESULT WINAPI d3d8_device_UpdateTexture(IDirect3DDevice8 *iface,
1356 IDirect3DBaseTexture8 *src_texture, IDirect3DBaseTexture8 *dst_texture)
1358 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1359 struct d3d8_texture *src_impl, *dst_impl;
1360 HRESULT hr;
1362 TRACE("iface %p, src_texture %p, dst_texture %p.\n", iface, src_texture, dst_texture);
1364 src_impl = unsafe_impl_from_IDirect3DBaseTexture8(src_texture);
1365 dst_impl = unsafe_impl_from_IDirect3DBaseTexture8(dst_texture);
1367 wined3d_mutex_lock();
1368 hr = wined3d_device_update_texture(device->wined3d_device,
1369 src_impl->wined3d_texture, dst_impl->wined3d_texture);
1370 wined3d_mutex_unlock();
1372 return hr;
1375 static HRESULT WINAPI d3d8_device_GetFrontBuffer(IDirect3DDevice8 *iface, IDirect3DSurface8 *dst_surface)
1377 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1378 struct d3d8_surface *dst_impl = unsafe_impl_from_IDirect3DSurface8(dst_surface);
1379 HRESULT hr;
1381 TRACE("iface %p, dst_surface %p.\n", iface, dst_surface);
1383 if (!dst_surface)
1385 WARN("Invalid destination surface passed.\n");
1386 return D3DERR_INVALIDCALL;
1389 wined3d_mutex_lock();
1390 hr = wined3d_swapchain_get_front_buffer_data(device->implicit_swapchain->wined3d_swapchain,
1391 dst_impl->wined3d_texture, dst_impl->sub_resource_idx);
1392 wined3d_mutex_unlock();
1394 return hr;
1397 static HRESULT WINAPI d3d8_device_SetRenderTarget(IDirect3DDevice8 *iface,
1398 IDirect3DSurface8 *render_target, IDirect3DSurface8 *depth_stencil)
1400 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1401 struct d3d8_surface *rt_impl = unsafe_impl_from_IDirect3DSurface8(render_target);
1402 struct d3d8_surface *ds_impl = unsafe_impl_from_IDirect3DSurface8(depth_stencil);
1403 struct wined3d_rendertarget_view *original_dsv, *rtv;
1404 HRESULT hr = D3D_OK;
1406 TRACE("iface %p, render_target %p, depth_stencil %p.\n", iface, render_target, depth_stencil);
1408 if (rt_impl && d3d8_surface_get_device(rt_impl) != device)
1410 WARN("Render target surface does not match device.\n");
1411 return D3DERR_INVALIDCALL;
1414 wined3d_mutex_lock();
1416 if (ds_impl)
1418 struct wined3d_sub_resource_desc ds_desc, rt_desc;
1419 struct wined3d_rendertarget_view *original_rtv;
1420 struct d3d8_surface *original_surface;
1422 /* If no render target is passed in check the size against the current RT */
1423 if (!render_target)
1425 if (!(original_rtv = wined3d_device_get_rendertarget_view(device->wined3d_device, 0)))
1427 wined3d_mutex_unlock();
1428 return D3DERR_NOTFOUND;
1430 original_surface = wined3d_rendertarget_view_get_sub_resource_parent(original_rtv);
1431 wined3d_texture_get_sub_resource_desc(original_surface->wined3d_texture,
1432 original_surface->sub_resource_idx, &rt_desc);
1434 else
1435 wined3d_texture_get_sub_resource_desc(rt_impl->wined3d_texture,
1436 rt_impl->sub_resource_idx, &rt_desc);
1438 wined3d_texture_get_sub_resource_desc(ds_impl->wined3d_texture, ds_impl->sub_resource_idx, &ds_desc);
1440 if (ds_desc.width < rt_desc.width || ds_desc.height < rt_desc.height)
1442 WARN("Depth stencil is smaller than the render target, returning D3DERR_INVALIDCALL\n");
1443 wined3d_mutex_unlock();
1444 return D3DERR_INVALIDCALL;
1446 if (ds_desc.multisample_type != rt_desc.multisample_type
1447 || ds_desc.multisample_quality != rt_desc.multisample_quality)
1449 WARN("Multisample settings do not match, returning D3DERR_INVALIDCALL\n");
1450 wined3d_mutex_unlock();
1451 return D3DERR_INVALIDCALL;
1455 original_dsv = wined3d_device_get_depth_stencil_view(device->wined3d_device);
1456 rtv = ds_impl ? d3d8_surface_acquire_rendertarget_view(ds_impl) : NULL;
1457 hr = wined3d_device_set_depth_stencil_view(device->wined3d_device, rtv);
1458 d3d8_surface_release_rendertarget_view(ds_impl, rtv);
1459 if (SUCCEEDED(hr))
1461 rtv = render_target ? d3d8_surface_acquire_rendertarget_view(rt_impl) : NULL;
1462 if (render_target && FAILED(hr = wined3d_device_set_rendertarget_view(device->wined3d_device, 0, rtv, TRUE)))
1463 wined3d_device_set_depth_stencil_view(device->wined3d_device, original_dsv);
1464 d3d8_surface_release_rendertarget_view(rt_impl, rtv);
1467 wined3d_mutex_unlock();
1469 return hr;
1472 static HRESULT WINAPI d3d8_device_GetRenderTarget(IDirect3DDevice8 *iface, IDirect3DSurface8 **render_target)
1474 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1475 struct wined3d_rendertarget_view *wined3d_rtv;
1476 struct d3d8_surface *surface_impl;
1477 HRESULT hr;
1479 TRACE("iface %p, render_target %p.\n", iface, render_target);
1481 if (!render_target)
1482 return D3DERR_INVALIDCALL;
1484 wined3d_mutex_lock();
1485 if ((wined3d_rtv = wined3d_device_get_rendertarget_view(device->wined3d_device, 0)))
1487 /* We want the sub resource parent here, since the view itself may be
1488 * internal to wined3d and may not have a parent. */
1489 surface_impl = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_rtv);
1490 *render_target = &surface_impl->IDirect3DSurface8_iface;
1491 IDirect3DSurface8_AddRef(*render_target);
1492 hr = D3D_OK;
1494 else
1496 ERR("Failed to get wined3d render target.\n");
1497 *render_target = NULL;
1498 hr = D3DERR_NOTFOUND;
1500 wined3d_mutex_unlock();
1502 return hr;
1505 static HRESULT WINAPI d3d8_device_GetDepthStencilSurface(IDirect3DDevice8 *iface, IDirect3DSurface8 **depth_stencil)
1507 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1508 struct wined3d_rendertarget_view *wined3d_dsv;
1509 struct d3d8_surface *surface_impl;
1510 HRESULT hr = D3D_OK;
1512 TRACE("iface %p, depth_stencil %p.\n", iface, depth_stencil);
1514 if (!depth_stencil)
1515 return D3DERR_INVALIDCALL;
1517 wined3d_mutex_lock();
1518 if ((wined3d_dsv = wined3d_device_get_depth_stencil_view(device->wined3d_device)))
1520 /* We want the sub resource parent here, since the view itself may be
1521 * internal to wined3d and may not have a parent. */
1522 surface_impl = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_dsv);
1523 *depth_stencil = &surface_impl->IDirect3DSurface8_iface;
1524 IDirect3DSurface8_AddRef(*depth_stencil);
1526 else
1528 hr = D3DERR_NOTFOUND;
1529 *depth_stencil = NULL;
1531 wined3d_mutex_unlock();
1533 return hr;
1536 static HRESULT WINAPI d3d8_device_BeginScene(IDirect3DDevice8 *iface)
1538 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1539 HRESULT hr;
1541 TRACE("iface %p.\n", iface);
1543 wined3d_mutex_lock();
1544 hr = wined3d_device_begin_scene(device->wined3d_device);
1545 wined3d_mutex_unlock();
1547 return hr;
1550 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d8_device_EndScene(IDirect3DDevice8 *iface)
1552 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1553 HRESULT hr;
1555 TRACE("iface %p.\n", iface);
1557 wined3d_mutex_lock();
1558 hr = wined3d_device_end_scene(device->wined3d_device);
1559 wined3d_mutex_unlock();
1561 return hr;
1564 static HRESULT WINAPI d3d8_device_Clear(IDirect3DDevice8 *iface, DWORD rect_count,
1565 const D3DRECT *rects, DWORD flags, D3DCOLOR color, float z, DWORD stencil)
1567 const struct wined3d_color c =
1569 ((color >> 16) & 0xff) / 255.0f,
1570 ((color >> 8) & 0xff) / 255.0f,
1571 (color & 0xff) / 255.0f,
1572 ((color >> 24) & 0xff) / 255.0f,
1574 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1575 HRESULT hr;
1577 TRACE("iface %p, rect_count %u, rects %p, flags %#x, color 0x%08x, z %.8e, stencil %u.\n",
1578 iface, rect_count, rects, flags, color, z, stencil);
1580 if (rect_count && !rects)
1582 WARN("count %u with NULL rects.\n", rect_count);
1583 rect_count = 0;
1586 wined3d_mutex_lock();
1587 hr = wined3d_device_clear(device->wined3d_device, rect_count, (const RECT *)rects, flags, &c, z, stencil);
1588 wined3d_mutex_unlock();
1590 return hr;
1593 static HRESULT WINAPI d3d8_device_SetTransform(IDirect3DDevice8 *iface,
1594 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
1596 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1598 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1600 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1601 wined3d_mutex_lock();
1602 wined3d_device_set_transform(device->wined3d_device, state, (const struct wined3d_matrix *)matrix);
1603 wined3d_mutex_unlock();
1605 return D3D_OK;
1608 static HRESULT WINAPI d3d8_device_GetTransform(IDirect3DDevice8 *iface,
1609 D3DTRANSFORMSTATETYPE state, D3DMATRIX *matrix)
1611 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1613 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1615 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1616 wined3d_mutex_lock();
1617 wined3d_device_get_transform(device->wined3d_device, state, (struct wined3d_matrix *)matrix);
1618 wined3d_mutex_unlock();
1620 return D3D_OK;
1623 static HRESULT WINAPI d3d8_device_MultiplyTransform(IDirect3DDevice8 *iface,
1624 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
1626 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1628 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1630 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1631 wined3d_mutex_lock();
1632 wined3d_device_multiply_transform(device->wined3d_device, state, (const struct wined3d_matrix *)matrix);
1633 wined3d_mutex_unlock();
1635 return D3D_OK;
1638 static HRESULT WINAPI d3d8_device_SetViewport(IDirect3DDevice8 *iface, const D3DVIEWPORT8 *viewport)
1640 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1641 struct wined3d_sub_resource_desc rt_desc;
1642 struct wined3d_rendertarget_view *rtv;
1643 struct d3d8_surface *surface;
1644 struct wined3d_viewport vp;
1646 TRACE("iface %p, viewport %p.\n", iface, viewport);
1648 wined3d_mutex_lock();
1649 if (!(rtv = wined3d_device_get_rendertarget_view(device->wined3d_device, 0)))
1651 wined3d_mutex_unlock();
1652 return D3DERR_NOTFOUND;
1654 surface = wined3d_rendertarget_view_get_sub_resource_parent(rtv);
1655 wined3d_texture_get_sub_resource_desc(surface->wined3d_texture, surface->sub_resource_idx, &rt_desc);
1657 if (viewport->X > rt_desc.width || viewport->Width > rt_desc.width - viewport->X
1658 || viewport->Y > rt_desc.height || viewport->Height > rt_desc.height - viewport->Y)
1660 WARN("Invalid viewport, returning D3DERR_INVALIDCALL.\n");
1661 wined3d_mutex_unlock();
1662 return D3DERR_INVALIDCALL;
1665 vp.x = viewport->X;
1666 vp.y = viewport->Y;
1667 vp.width = viewport->Width;
1668 vp.height = viewport->Height;
1669 vp.min_z = viewport->MinZ;
1670 vp.max_z = viewport->MaxZ;
1672 wined3d_device_set_viewports(device->wined3d_device, 1, &vp);
1673 wined3d_mutex_unlock();
1675 return D3D_OK;
1678 static HRESULT WINAPI d3d8_device_GetViewport(IDirect3DDevice8 *iface, D3DVIEWPORT8 *viewport)
1680 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1681 struct wined3d_viewport wined3d_viewport;
1683 TRACE("iface %p, viewport %p.\n", iface, viewport);
1685 wined3d_mutex_lock();
1686 wined3d_device_get_viewports(device->wined3d_device, NULL, &wined3d_viewport);
1687 wined3d_mutex_unlock();
1689 viewport->X = wined3d_viewport.x;
1690 viewport->Y = wined3d_viewport.y;
1691 viewport->Width = wined3d_viewport.width;
1692 viewport->Height = wined3d_viewport.height;
1693 viewport->MinZ = wined3d_viewport.min_z;
1694 viewport->MaxZ = wined3d_viewport.max_z;
1696 return D3D_OK;
1699 static HRESULT WINAPI d3d8_device_SetMaterial(IDirect3DDevice8 *iface, const D3DMATERIAL8 *material)
1701 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1703 TRACE("iface %p, material %p.\n", iface, material);
1705 /* Note: D3DMATERIAL8 is compatible with struct wined3d_material. */
1706 wined3d_mutex_lock();
1707 wined3d_device_set_material(device->wined3d_device, (const struct wined3d_material *)material);
1708 wined3d_mutex_unlock();
1710 return D3D_OK;
1713 static HRESULT WINAPI d3d8_device_GetMaterial(IDirect3DDevice8 *iface, D3DMATERIAL8 *material)
1715 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1717 TRACE("iface %p, material %p.\n", iface, material);
1719 /* Note: D3DMATERIAL8 is compatible with struct wined3d_material. */
1720 wined3d_mutex_lock();
1721 wined3d_device_get_material(device->wined3d_device, (struct wined3d_material *)material);
1722 wined3d_mutex_unlock();
1724 return D3D_OK;
1727 static HRESULT WINAPI d3d8_device_SetLight(IDirect3DDevice8 *iface, DWORD index, const D3DLIGHT8 *light)
1729 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1730 HRESULT hr;
1732 TRACE("iface %p, index %u, light %p.\n", iface, index, light);
1734 /* Note: D3DLIGHT8 is compatible with struct wined3d_light. */
1735 wined3d_mutex_lock();
1736 hr = wined3d_device_set_light(device->wined3d_device, index, (const struct wined3d_light *)light);
1737 wined3d_mutex_unlock();
1739 return hr;
1742 static HRESULT WINAPI d3d8_device_GetLight(IDirect3DDevice8 *iface, DWORD index, D3DLIGHT8 *light)
1744 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1745 HRESULT hr;
1747 TRACE("iface %p, index %u, light %p.\n", iface, index, light);
1749 /* Note: D3DLIGHT8 is compatible with struct wined3d_light. */
1750 wined3d_mutex_lock();
1751 hr = wined3d_device_get_light(device->wined3d_device, index, (struct wined3d_light *)light);
1752 wined3d_mutex_unlock();
1754 return hr;
1757 static HRESULT WINAPI d3d8_device_LightEnable(IDirect3DDevice8 *iface, DWORD index, BOOL enable)
1759 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1760 HRESULT hr;
1762 TRACE("iface %p, index %u, enable %#x.\n", iface, index, enable);
1764 wined3d_mutex_lock();
1765 hr = wined3d_device_set_light_enable(device->wined3d_device, index, enable);
1766 wined3d_mutex_unlock();
1768 return hr;
1771 static HRESULT WINAPI d3d8_device_GetLightEnable(IDirect3DDevice8 *iface, DWORD index, BOOL *enable)
1773 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1774 HRESULT hr;
1776 TRACE("iface %p, index %u, enable %p.\n", iface, index, enable);
1778 wined3d_mutex_lock();
1779 hr = wined3d_device_get_light_enable(device->wined3d_device, index, enable);
1780 wined3d_mutex_unlock();
1782 return hr;
1785 static HRESULT WINAPI d3d8_device_SetClipPlane(IDirect3DDevice8 *iface, DWORD index, const float *plane)
1787 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1788 HRESULT hr;
1790 TRACE("iface %p, index %u, plane %p.\n", iface, index, plane);
1792 wined3d_mutex_lock();
1793 hr = wined3d_device_set_clip_plane(device->wined3d_device, index, (const struct wined3d_vec4 *)plane);
1794 wined3d_mutex_unlock();
1796 return hr;
1799 static HRESULT WINAPI d3d8_device_GetClipPlane(IDirect3DDevice8 *iface, DWORD index, float *plane)
1801 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1802 HRESULT hr;
1804 TRACE("iface %p, index %u, plane %p.\n", iface, index, plane);
1806 wined3d_mutex_lock();
1807 hr = wined3d_device_get_clip_plane(device->wined3d_device, index, (struct wined3d_vec4 *)plane);
1808 wined3d_mutex_unlock();
1810 return hr;
1813 static HRESULT WINAPI d3d8_device_SetRenderState(IDirect3DDevice8 *iface,
1814 D3DRENDERSTATETYPE state, DWORD value)
1816 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1818 TRACE("iface %p, state %#x, value %#x.\n", iface, state, value);
1820 wined3d_mutex_lock();
1821 switch (state)
1823 case D3DRS_ZBIAS:
1824 wined3d_device_set_render_state(device->wined3d_device, WINED3D_RS_DEPTHBIAS, value);
1825 break;
1827 default:
1828 wined3d_device_set_render_state(device->wined3d_device, state, value);
1830 wined3d_mutex_unlock();
1832 return D3D_OK;
1835 static HRESULT WINAPI d3d8_device_GetRenderState(IDirect3DDevice8 *iface,
1836 D3DRENDERSTATETYPE state, DWORD *value)
1838 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1840 TRACE("iface %p, state %#x, value %p.\n", iface, state, value);
1842 wined3d_mutex_lock();
1843 switch (state)
1845 case D3DRS_ZBIAS:
1846 *value = wined3d_device_get_render_state(device->wined3d_device, WINED3D_RS_DEPTHBIAS);
1847 break;
1849 default:
1850 *value = wined3d_device_get_render_state(device->wined3d_device, state);
1852 wined3d_mutex_unlock();
1854 return D3D_OK;
1857 static HRESULT WINAPI d3d8_device_BeginStateBlock(IDirect3DDevice8 *iface)
1859 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1860 HRESULT hr;
1862 TRACE("iface %p.\n", iface);
1864 wined3d_mutex_lock();
1865 if (SUCCEEDED(hr = wined3d_device_begin_stateblock(device->wined3d_device)))
1866 device->recording = TRUE;
1867 wined3d_mutex_unlock();
1869 return hr;
1872 static HRESULT WINAPI d3d8_device_EndStateBlock(IDirect3DDevice8 *iface, DWORD *token)
1874 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1875 struct wined3d_stateblock *stateblock;
1876 HRESULT hr;
1878 TRACE("iface %p, token %p.\n", iface, token);
1880 /* Tell wineD3D to endstateblock before anything else (in case we run out
1881 * of memory later and cause locking problems)
1883 wined3d_mutex_lock();
1884 hr = wined3d_device_end_stateblock(device->wined3d_device, &stateblock);
1885 if (FAILED(hr))
1887 WARN("Failed to end the state block, %#x.\n", hr);
1888 wined3d_mutex_unlock();
1889 return hr;
1891 device->recording = FALSE;
1893 *token = d3d8_allocate_handle(&device->handle_table, stateblock, D3D8_HANDLE_SB);
1894 wined3d_mutex_unlock();
1896 if (*token == D3D8_INVALID_HANDLE)
1898 ERR("Failed to create a handle\n");
1899 wined3d_mutex_lock();
1900 wined3d_stateblock_decref(stateblock);
1901 wined3d_mutex_unlock();
1902 return E_FAIL;
1904 ++*token;
1906 TRACE("Returning %#x (%p).\n", *token, stateblock);
1908 return hr;
1911 static HRESULT WINAPI d3d8_device_ApplyStateBlock(IDirect3DDevice8 *iface, DWORD token)
1913 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1914 struct wined3d_stateblock *stateblock;
1915 struct wined3d_buffer *wined3d_buffer;
1916 struct d3d8_vertexbuffer *buffer;
1917 unsigned int i, offset, stride;
1918 enum wined3d_format_id format;
1919 HRESULT hr;
1921 TRACE("iface %p, token %#x.\n", iface, token);
1923 if (!token)
1924 return D3D_OK;
1926 wined3d_mutex_lock();
1927 stateblock = d3d8_get_object(&device->handle_table, token - 1, D3D8_HANDLE_SB);
1928 if (!stateblock)
1930 WARN("Invalid handle (%#x) passed.\n", token);
1931 wined3d_mutex_unlock();
1932 return D3DERR_INVALIDCALL;
1934 wined3d_stateblock_apply(stateblock);
1935 device->sysmem_vb = 0;
1936 for (i = 0; i < D3D8_MAX_STREAMS; ++i)
1938 if (FAILED(hr = wined3d_device_get_stream_source(device->wined3d_device,
1939 i, &wined3d_buffer, &offset, &stride)))
1940 continue;
1941 if (!wined3d_buffer || !(buffer = wined3d_buffer_get_parent(wined3d_buffer)))
1942 continue;
1943 if (buffer->draw_buffer)
1944 device->sysmem_vb |= 1u << i;
1946 device->sysmem_ib = (wined3d_buffer = wined3d_device_get_index_buffer(device->wined3d_device, &format, &offset))
1947 && (buffer = wined3d_buffer_get_parent(wined3d_buffer)) && buffer->draw_buffer;
1948 wined3d_mutex_unlock();
1950 return D3D_OK;
1953 static HRESULT WINAPI d3d8_device_CaptureStateBlock(IDirect3DDevice8 *iface, DWORD token)
1955 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1956 struct wined3d_stateblock *stateblock;
1958 TRACE("iface %p, token %#x.\n", iface, token);
1960 wined3d_mutex_lock();
1961 stateblock = d3d8_get_object(&device->handle_table, token - 1, D3D8_HANDLE_SB);
1962 if (!stateblock)
1964 WARN("Invalid handle (%#x) passed.\n", token);
1965 wined3d_mutex_unlock();
1966 return D3DERR_INVALIDCALL;
1968 wined3d_stateblock_capture(stateblock);
1969 wined3d_mutex_unlock();
1971 return D3D_OK;
1974 static HRESULT WINAPI d3d8_device_DeleteStateBlock(IDirect3DDevice8 *iface, DWORD token)
1976 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1977 struct wined3d_stateblock *stateblock;
1979 TRACE("iface %p, token %#x.\n", iface, token);
1981 wined3d_mutex_lock();
1982 stateblock = d3d8_free_handle(&device->handle_table, token - 1, D3D8_HANDLE_SB);
1984 if (!stateblock)
1986 WARN("Invalid handle (%#x) passed.\n", token);
1987 wined3d_mutex_unlock();
1988 return D3DERR_INVALIDCALL;
1991 if (wined3d_stateblock_decref(stateblock))
1993 ERR("Stateblock %p has references left, this shouldn't happen.\n", stateblock);
1995 wined3d_mutex_unlock();
1997 return D3D_OK;
2000 static HRESULT WINAPI d3d8_device_CreateStateBlock(IDirect3DDevice8 *iface,
2001 D3DSTATEBLOCKTYPE type, DWORD *handle)
2003 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2004 struct wined3d_stateblock *stateblock;
2005 HRESULT hr;
2007 TRACE("iface %p, type %#x, handle %p.\n", iface, type, handle);
2009 if (type != D3DSBT_ALL
2010 && type != D3DSBT_PIXELSTATE
2011 && type != D3DSBT_VERTEXSTATE)
2013 WARN("Unexpected stateblock type, returning D3DERR_INVALIDCALL\n");
2014 return D3DERR_INVALIDCALL;
2017 wined3d_mutex_lock();
2018 hr = wined3d_stateblock_create(device->wined3d_device, (enum wined3d_stateblock_type)type, &stateblock);
2019 if (FAILED(hr))
2021 wined3d_mutex_unlock();
2022 ERR("Failed to create the state block, hr %#x\n", hr);
2023 return hr;
2026 *handle = d3d8_allocate_handle(&device->handle_table, stateblock, D3D8_HANDLE_SB);
2027 wined3d_mutex_unlock();
2029 if (*handle == D3D8_INVALID_HANDLE)
2031 ERR("Failed to allocate a handle.\n");
2032 wined3d_mutex_lock();
2033 wined3d_stateblock_decref(stateblock);
2034 wined3d_mutex_unlock();
2035 return E_FAIL;
2037 ++*handle;
2039 TRACE("Returning %#x (%p).\n", *handle, stateblock);
2041 return hr;
2044 static HRESULT WINAPI d3d8_device_SetClipStatus(IDirect3DDevice8 *iface, const D3DCLIPSTATUS8 *clip_status)
2046 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2047 HRESULT hr;
2049 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
2050 /* FIXME: Verify that D3DCLIPSTATUS8 ~= struct wined3d_clip_status. */
2052 wined3d_mutex_lock();
2053 hr = wined3d_device_set_clip_status(device->wined3d_device, (const struct wined3d_clip_status *)clip_status);
2054 wined3d_mutex_unlock();
2056 return hr;
2059 static HRESULT WINAPI d3d8_device_GetClipStatus(IDirect3DDevice8 *iface, D3DCLIPSTATUS8 *clip_status)
2061 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2062 HRESULT hr;
2064 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
2066 wined3d_mutex_lock();
2067 hr = wined3d_device_get_clip_status(device->wined3d_device, (struct wined3d_clip_status *)clip_status);
2068 wined3d_mutex_unlock();
2070 return hr;
2073 static HRESULT WINAPI d3d8_device_GetTexture(IDirect3DDevice8 *iface, DWORD stage, IDirect3DBaseTexture8 **texture)
2075 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2076 struct wined3d_texture *wined3d_texture;
2077 struct d3d8_texture *texture_impl;
2079 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
2081 if (!texture)
2082 return D3DERR_INVALIDCALL;
2084 wined3d_mutex_lock();
2085 if ((wined3d_texture = wined3d_device_get_texture(device->wined3d_device, stage)))
2087 texture_impl = wined3d_texture_get_parent(wined3d_texture);
2088 *texture = &texture_impl->IDirect3DBaseTexture8_iface;
2089 IDirect3DBaseTexture8_AddRef(*texture);
2091 else
2093 *texture = NULL;
2095 wined3d_mutex_unlock();
2097 return D3D_OK;
2100 static HRESULT WINAPI d3d8_device_SetTexture(IDirect3DDevice8 *iface, DWORD stage, IDirect3DBaseTexture8 *texture)
2102 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2103 struct d3d8_texture *texture_impl;
2104 HRESULT hr;
2106 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
2108 texture_impl = unsafe_impl_from_IDirect3DBaseTexture8(texture);
2110 wined3d_mutex_lock();
2111 hr = wined3d_device_set_texture(device->wined3d_device, stage,
2112 texture_impl ? texture_impl->wined3d_texture : NULL);
2113 wined3d_mutex_unlock();
2115 return hr;
2118 static const struct tss_lookup
2120 BOOL sampler_state;
2121 union
2123 enum wined3d_texture_stage_state texture_state;
2124 enum wined3d_sampler_state sampler_state;
2125 } u;
2127 tss_lookup[] =
2129 {FALSE, {WINED3D_TSS_INVALID}}, /* 0, unused */
2130 {FALSE, {WINED3D_TSS_COLOR_OP}}, /* 1, D3DTSS_COLOROP */
2131 {FALSE, {WINED3D_TSS_COLOR_ARG1}}, /* 2, D3DTSS_COLORARG1 */
2132 {FALSE, {WINED3D_TSS_COLOR_ARG2}}, /* 3, D3DTSS_COLORARG2 */
2133 {FALSE, {WINED3D_TSS_ALPHA_OP}}, /* 4, D3DTSS_ALPHAOP */
2134 {FALSE, {WINED3D_TSS_ALPHA_ARG1}}, /* 5, D3DTSS_ALPHAARG1 */
2135 {FALSE, {WINED3D_TSS_ALPHA_ARG2}}, /* 6, D3DTSS_ALPHAARG2 */
2136 {FALSE, {WINED3D_TSS_BUMPENV_MAT00}}, /* 7, D3DTSS_BUMPENVMAT00 */
2137 {FALSE, {WINED3D_TSS_BUMPENV_MAT01}}, /* 8, D3DTSS_BUMPENVMAT01 */
2138 {FALSE, {WINED3D_TSS_BUMPENV_MAT10}}, /* 9, D3DTSS_BUMPENVMAT10 */
2139 {FALSE, {WINED3D_TSS_BUMPENV_MAT11}}, /* 10, D3DTSS_BUMPENVMAT11 */
2140 {FALSE, {WINED3D_TSS_TEXCOORD_INDEX}}, /* 11, D3DTSS_TEXCOORDINDEX */
2141 {FALSE, {WINED3D_TSS_INVALID}}, /* 12, unused */
2142 {TRUE, {WINED3D_SAMP_ADDRESS_U}}, /* 13, D3DTSS_ADDRESSU */
2143 {TRUE, {WINED3D_SAMP_ADDRESS_V}}, /* 14, D3DTSS_ADDRESSV */
2144 {TRUE, {WINED3D_SAMP_BORDER_COLOR}}, /* 15, D3DTSS_BORDERCOLOR */
2145 {TRUE, {WINED3D_SAMP_MAG_FILTER}}, /* 16, D3DTSS_MAGFILTER */
2146 {TRUE, {WINED3D_SAMP_MIN_FILTER}}, /* 17, D3DTSS_MINFILTER */
2147 {TRUE, {WINED3D_SAMP_MIP_FILTER}}, /* 18, D3DTSS_MIPFILTER */
2148 {TRUE, {WINED3D_SAMP_MIPMAP_LOD_BIAS}}, /* 19, D3DTSS_MIPMAPLODBIAS */
2149 {TRUE, {WINED3D_SAMP_MAX_MIP_LEVEL}}, /* 20, D3DTSS_MAXMIPLEVEL */
2150 {TRUE, {WINED3D_SAMP_MAX_ANISOTROPY}}, /* 21, D3DTSS_MAXANISOTROPY */
2151 {FALSE, {WINED3D_TSS_BUMPENV_LSCALE}}, /* 22, D3DTSS_BUMPENVLSCALE */
2152 {FALSE, {WINED3D_TSS_BUMPENV_LOFFSET}}, /* 23, D3DTSS_BUMPENVLOFFSET */
2153 {FALSE, {WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS}}, /* 24, D3DTSS_TEXTURETRANSFORMFLAGS */
2154 {TRUE, {WINED3D_SAMP_ADDRESS_W}}, /* 25, D3DTSS_ADDRESSW */
2155 {FALSE, {WINED3D_TSS_COLOR_ARG0}}, /* 26, D3DTSS_COLORARG0 */
2156 {FALSE, {WINED3D_TSS_ALPHA_ARG0}}, /* 27, D3DTSS_ALPHAARG0 */
2157 {FALSE, {WINED3D_TSS_RESULT_ARG}}, /* 28, D3DTSS_RESULTARG */
2160 static HRESULT WINAPI d3d8_device_GetTextureStageState(IDirect3DDevice8 *iface,
2161 DWORD stage, D3DTEXTURESTAGESTATETYPE Type, DWORD *value)
2163 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2164 const struct tss_lookup *l;
2166 TRACE("iface %p, stage %u, state %#x, value %p.\n", iface, stage, Type, value);
2168 if (Type >= ARRAY_SIZE(tss_lookup))
2170 WARN("Invalid Type %#x passed.\n", Type);
2171 return D3D_OK;
2174 l = &tss_lookup[Type];
2176 wined3d_mutex_lock();
2177 if (l->sampler_state)
2178 *value = wined3d_device_get_sampler_state(device->wined3d_device, stage, l->u.sampler_state);
2179 else
2180 *value = wined3d_device_get_texture_stage_state(device->wined3d_device, stage, l->u.texture_state);
2181 wined3d_mutex_unlock();
2183 return D3D_OK;
2186 static HRESULT WINAPI d3d8_device_SetTextureStageState(IDirect3DDevice8 *iface,
2187 DWORD stage, D3DTEXTURESTAGESTATETYPE type, DWORD value)
2189 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2190 const struct tss_lookup *l;
2192 TRACE("iface %p, stage %u, state %#x, value %#x.\n", iface, stage, type, value);
2194 if (type >= ARRAY_SIZE(tss_lookup))
2196 WARN("Invalid type %#x passed.\n", type);
2197 return D3D_OK;
2200 l = &tss_lookup[type];
2202 wined3d_mutex_lock();
2203 if (l->sampler_state)
2204 wined3d_device_set_sampler_state(device->wined3d_device, stage, l->u.sampler_state, value);
2205 else
2206 wined3d_device_set_texture_stage_state(device->wined3d_device, stage, l->u.texture_state, value);
2207 wined3d_mutex_unlock();
2209 return D3D_OK;
2212 static HRESULT WINAPI d3d8_device_ValidateDevice(IDirect3DDevice8 *iface, DWORD *pass_count)
2214 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2215 HRESULT hr;
2217 TRACE("iface %p, pass_count %p.\n", iface, pass_count);
2219 wined3d_mutex_lock();
2220 hr = wined3d_device_validate_device(device->wined3d_device, pass_count);
2221 wined3d_mutex_unlock();
2223 return hr;
2226 static HRESULT WINAPI d3d8_device_GetInfo(IDirect3DDevice8 *iface,
2227 DWORD info_id, void *info, DWORD info_size)
2229 TRACE("iface %p, info_id %#x, info %p, info_size %u.\n", iface, info_id, info, info_size);
2231 if (info_id < 4)
2232 return E_FAIL;
2233 return S_FALSE;
2236 static HRESULT WINAPI d3d8_device_SetPaletteEntries(IDirect3DDevice8 *iface,
2237 UINT palette_idx, const PALETTEENTRY *entries)
2239 WARN("iface %p, palette_idx %u, entries %p unimplemented\n", iface, palette_idx, entries);
2241 /* GPUs stopped supporting palettized textures with the Shader Model 1 generation. Wined3d
2242 * does not have a d3d8/9-style palette API */
2244 return D3D_OK;
2247 static HRESULT WINAPI d3d8_device_GetPaletteEntries(IDirect3DDevice8 *iface,
2248 UINT palette_idx, PALETTEENTRY *entries)
2250 FIXME("iface %p, palette_idx %u, entries %p unimplemented.\n", iface, palette_idx, entries);
2252 return D3DERR_INVALIDCALL;
2255 static HRESULT WINAPI d3d8_device_SetCurrentTexturePalette(IDirect3DDevice8 *iface, UINT palette_idx)
2257 WARN("iface %p, palette_idx %u unimplemented.\n", iface, palette_idx);
2259 return D3D_OK;
2262 static HRESULT WINAPI d3d8_device_GetCurrentTexturePalette(IDirect3DDevice8 *iface, UINT *palette_idx)
2264 FIXME("iface %p, palette_idx %p unimplemented.\n", iface, palette_idx);
2266 return D3DERR_INVALIDCALL;
2269 static void d3d8_device_upload_sysmem_vertex_buffers(struct d3d8_device *device,
2270 unsigned int start_vertex, unsigned int vertex_count)
2272 struct wined3d_box box = {0, 0, 0, 1, 0, 1};
2273 struct d3d8_vertexbuffer *d3d8_buffer;
2274 unsigned int i, offset, stride, map;
2275 struct wined3d_buffer *dst_buffer;
2276 HRESULT hr;
2278 map = device->sysmem_vb;
2279 while (map)
2281 i = ffs(map) - 1;
2282 map ^= 1u << i;
2284 if (FAILED(hr = wined3d_device_get_stream_source(device->wined3d_device, i, &dst_buffer, &offset, &stride)))
2285 ERR("Failed to get stream source.\n");
2286 d3d8_buffer = wined3d_buffer_get_parent(dst_buffer);
2287 box.left = offset + start_vertex * stride;
2288 box.right = box.left + vertex_count * stride;
2289 if (FAILED(hr = wined3d_device_copy_sub_resource_region(device->wined3d_device,
2290 wined3d_buffer_get_resource(dst_buffer), 0, box.left, 0, 0,
2291 wined3d_buffer_get_resource(d3d8_buffer->wined3d_buffer), 0, &box, 0)))
2292 ERR("Failed to update buffer.\n");
2296 static void d3d8_device_upload_sysmem_index_buffer(struct d3d8_device *device,
2297 unsigned int start_idx, unsigned int idx_count)
2299 struct wined3d_box box = {0, 0, 0, 1, 0, 1};
2300 struct d3d8_vertexbuffer *d3d8_buffer;
2301 struct wined3d_buffer *dst_buffer;
2302 enum wined3d_format_id format;
2303 unsigned int offset, idx_size;
2304 HRESULT hr;
2306 if (!device->sysmem_ib)
2307 return;
2309 if (!(dst_buffer = wined3d_device_get_index_buffer(device->wined3d_device, &format, &offset)))
2310 ERR("Failed to get index buffer.\n");
2311 d3d8_buffer = wined3d_buffer_get_parent(dst_buffer);
2312 idx_size = format == WINED3DFMT_R16_UINT ? 2 : 4;
2313 box.left = offset + start_idx * idx_size;
2314 box.right = box.left + idx_count * idx_size;
2315 if (FAILED(hr = wined3d_device_copy_sub_resource_region(device->wined3d_device,
2316 wined3d_buffer_get_resource(dst_buffer), 0, box.left, 0, 0,
2317 wined3d_buffer_get_resource(d3d8_buffer->wined3d_buffer), 0, &box, 0)))
2318 ERR("Failed to update buffer.\n");
2321 static HRESULT WINAPI d3d8_device_DrawPrimitive(IDirect3DDevice8 *iface,
2322 D3DPRIMITIVETYPE primitive_type, UINT start_vertex, UINT primitive_count)
2324 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2325 unsigned int vertex_count;
2326 HRESULT hr;
2328 TRACE("iface %p, primitive_type %#x, start_vertex %u, primitive_count %u.\n",
2329 iface, primitive_type, start_vertex, primitive_count);
2331 vertex_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2332 wined3d_mutex_lock();
2333 d3d8_device_upload_sysmem_vertex_buffers(device, start_vertex, vertex_count);
2334 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type, 0);
2335 hr = wined3d_device_draw_primitive(device->wined3d_device, start_vertex, vertex_count);
2336 wined3d_mutex_unlock();
2338 return hr;
2341 static HRESULT WINAPI d3d8_device_DrawIndexedPrimitive(IDirect3DDevice8 *iface,
2342 D3DPRIMITIVETYPE primitive_type, UINT min_vertex_idx, UINT vertex_count,
2343 UINT start_idx, UINT primitive_count)
2345 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2346 unsigned int index_count;
2347 HRESULT hr;
2349 TRACE("iface %p, primitive_type %#x, min_vertex_idx %u, vertex_count %u, start_idx %u, primitive_count %u.\n",
2350 iface, primitive_type, min_vertex_idx, vertex_count, start_idx, primitive_count);
2352 index_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2353 wined3d_mutex_lock();
2354 d3d8_device_upload_sysmem_vertex_buffers(device, min_vertex_idx, vertex_count);
2355 d3d8_device_upload_sysmem_index_buffer(device, start_idx, index_count);
2356 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type, 0);
2357 hr = wined3d_device_draw_indexed_primitive(device->wined3d_device, start_idx, index_count);
2358 wined3d_mutex_unlock();
2360 return hr;
2363 /* The caller is responsible for wined3d locking */
2364 static HRESULT d3d8_device_prepare_vertex_buffer(struct d3d8_device *device, UINT min_size)
2366 HRESULT hr;
2368 if (device->vertex_buffer_size < min_size || !device->vertex_buffer)
2370 UINT size = max(device->vertex_buffer_size * 2, min_size);
2371 struct wined3d_buffer_desc desc;
2372 struct wined3d_buffer *buffer;
2374 TRACE("Growing vertex buffer to %u bytes\n", size);
2376 desc.byte_width = size;
2377 desc.usage = WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_WRITEONLY;
2378 desc.bind_flags = WINED3D_BIND_VERTEX_BUFFER;
2379 desc.access = WINED3D_RESOURCE_ACCESS_GPU | WINED3D_RESOURCE_ACCESS_MAP_R | WINED3D_RESOURCE_ACCESS_MAP_W;
2380 desc.misc_flags = 0;
2381 desc.structure_byte_stride = 0;
2383 if (FAILED(hr = wined3d_buffer_create(device->wined3d_device, &desc,
2384 NULL, NULL, &d3d8_null_wined3d_parent_ops, &buffer)))
2386 ERR("Failed to create vertex buffer, hr %#x.\n", hr);
2387 return hr;
2390 if (device->vertex_buffer)
2391 wined3d_buffer_decref(device->vertex_buffer);
2393 device->vertex_buffer = buffer;
2394 device->vertex_buffer_size = size;
2395 device->vertex_buffer_pos = 0;
2397 return D3D_OK;
2400 static HRESULT WINAPI d3d8_device_DrawPrimitiveUP(IDirect3DDevice8 *iface,
2401 D3DPRIMITIVETYPE primitive_type, UINT primitive_count, const void *data,
2402 UINT stride)
2404 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2405 HRESULT hr;
2406 UINT vtx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2407 struct wined3d_map_desc wined3d_map_desc;
2408 struct wined3d_box wined3d_box = {0};
2409 UINT size = vtx_count * stride;
2410 struct wined3d_resource *vb;
2411 UINT vb_pos, align;
2413 TRACE("iface %p, primitive_type %#x, primitive_count %u, data %p, stride %u.\n",
2414 iface, primitive_type, primitive_count, data, stride);
2416 if (!primitive_count)
2418 WARN("primitive_count is 0, returning D3D_OK\n");
2419 return D3D_OK;
2422 wined3d_mutex_lock();
2423 hr = d3d8_device_prepare_vertex_buffer(device, size);
2424 if (FAILED(hr))
2425 goto done;
2427 vb_pos = device->vertex_buffer_pos;
2428 align = vb_pos % stride;
2429 if (align) align = stride - align;
2430 if (vb_pos + size + align > device->vertex_buffer_size)
2431 vb_pos = 0;
2432 else
2433 vb_pos += align;
2435 wined3d_box.left = vb_pos;
2436 wined3d_box.right = vb_pos + size;
2437 vb = wined3d_buffer_get_resource(device->vertex_buffer);
2438 if (FAILED(wined3d_resource_map(vb, 0, &wined3d_map_desc, &wined3d_box,
2439 WINED3D_MAP_WRITE | (vb_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD))))
2440 goto done;
2441 memcpy(wined3d_map_desc.data, data, size);
2442 wined3d_resource_unmap(vb, 0);
2443 device->vertex_buffer_pos = vb_pos + size;
2445 hr = wined3d_device_set_stream_source(device->wined3d_device, 0, device->vertex_buffer, 0, stride);
2446 if (FAILED(hr))
2447 goto done;
2449 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type, 0);
2450 hr = wined3d_device_draw_primitive(device->wined3d_device, vb_pos / stride, vtx_count);
2451 wined3d_device_set_stream_source(device->wined3d_device, 0, NULL, 0, 0);
2453 done:
2454 wined3d_mutex_unlock();
2455 return hr;
2458 /* The caller is responsible for wined3d locking */
2459 static HRESULT d3d8_device_prepare_index_buffer(struct d3d8_device *device, UINT min_size)
2461 HRESULT hr;
2463 if (device->index_buffer_size < min_size || !device->index_buffer)
2465 UINT size = max(device->index_buffer_size * 2, min_size);
2466 struct wined3d_buffer_desc desc;
2467 struct wined3d_buffer *buffer;
2469 TRACE("Growing index buffer to %u bytes\n", size);
2471 desc.byte_width = size;
2472 desc.usage = WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_WRITEONLY | WINED3DUSAGE_STATICDECL;
2473 desc.bind_flags = WINED3D_BIND_INDEX_BUFFER;
2474 desc.access = WINED3D_RESOURCE_ACCESS_GPU | WINED3D_RESOURCE_ACCESS_MAP_R | WINED3D_RESOURCE_ACCESS_MAP_W;
2475 desc.misc_flags = 0;
2476 desc.structure_byte_stride = 0;
2478 if (FAILED(hr = wined3d_buffer_create(device->wined3d_device, &desc,
2479 NULL, NULL, &d3d8_null_wined3d_parent_ops, &buffer)))
2481 ERR("Failed to create index buffer, hr %#x.\n", hr);
2482 return hr;
2485 if (device->index_buffer)
2486 wined3d_buffer_decref(device->index_buffer);
2488 device->index_buffer = buffer;
2489 device->index_buffer_size = size;
2490 device->index_buffer_pos = 0;
2492 return D3D_OK;
2495 static HRESULT WINAPI d3d8_device_DrawIndexedPrimitiveUP(IDirect3DDevice8 *iface,
2496 D3DPRIMITIVETYPE primitive_type, UINT min_vertex_idx, UINT vertex_count,
2497 UINT primitive_count, const void *index_data, D3DFORMAT index_format,
2498 const void *vertex_data, UINT vertex_stride)
2500 UINT idx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2501 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2502 UINT idx_fmt_size = index_format == D3DFMT_INDEX16 ? 2 : 4;
2503 UINT vtx_size = vertex_count * vertex_stride;
2504 UINT idx_size = idx_count * idx_fmt_size;
2505 struct wined3d_map_desc wined3d_map_desc;
2506 struct wined3d_box wined3d_box = {0};
2507 struct wined3d_resource *ib, *vb;
2508 UINT vb_pos, ib_pos, align;
2509 HRESULT hr;
2511 TRACE("iface %p, primitive_type %#x, min_vertex_idx %u, vertex_count %u, primitive_count %u, "
2512 "index_data %p, index_format %#x, vertex_data %p, vertex_stride %u.\n",
2513 iface, primitive_type, min_vertex_idx, vertex_count, primitive_count,
2514 index_data, index_format, vertex_data, vertex_stride);
2516 if (!primitive_count)
2518 WARN("primitive_count is 0, returning D3D_OK\n");
2519 return D3D_OK;
2522 wined3d_mutex_lock();
2524 hr = d3d8_device_prepare_vertex_buffer(device, vtx_size);
2525 if (FAILED(hr))
2526 goto done;
2528 vb_pos = device->vertex_buffer_pos;
2529 align = vb_pos % vertex_stride;
2530 if (align) align = vertex_stride - align;
2531 if (vb_pos + vtx_size + align > device->vertex_buffer_size)
2532 vb_pos = 0;
2533 else
2534 vb_pos += align;
2536 wined3d_box.left = vb_pos;
2537 wined3d_box.right = vb_pos + vtx_size;
2538 vb = wined3d_buffer_get_resource(device->vertex_buffer);
2539 if (FAILED(hr = wined3d_resource_map(vb, 0, &wined3d_map_desc, &wined3d_box,
2540 WINED3D_MAP_WRITE | (vb_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD))))
2541 goto done;
2542 memcpy(wined3d_map_desc.data, (char *)vertex_data + min_vertex_idx * vertex_stride, vtx_size);
2543 wined3d_resource_unmap(vb, 0);
2544 device->vertex_buffer_pos = vb_pos + vtx_size;
2546 hr = d3d8_device_prepare_index_buffer(device, idx_size);
2547 if (FAILED(hr))
2548 goto done;
2550 ib_pos = device->index_buffer_pos;
2551 align = ib_pos % idx_fmt_size;
2552 if (align) align = idx_fmt_size - align;
2553 if (ib_pos + idx_size + align > device->index_buffer_size)
2554 ib_pos = 0;
2555 else
2556 ib_pos += align;
2558 wined3d_box.left = ib_pos;
2559 wined3d_box.right = ib_pos + idx_size;
2560 ib = wined3d_buffer_get_resource(device->index_buffer);
2561 if (FAILED(hr = wined3d_resource_map(ib, 0, &wined3d_map_desc, &wined3d_box,
2562 WINED3D_MAP_WRITE | (ib_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD))))
2563 goto done;
2564 memcpy(wined3d_map_desc.data, index_data, idx_size);
2565 wined3d_resource_unmap(ib, 0);
2566 device->index_buffer_pos = ib_pos + idx_size;
2568 hr = wined3d_device_set_stream_source(device->wined3d_device, 0, device->vertex_buffer, 0, vertex_stride);
2569 if (FAILED(hr))
2570 goto done;
2572 wined3d_device_set_index_buffer(device->wined3d_device, device->index_buffer,
2573 wined3dformat_from_d3dformat(index_format), 0);
2574 wined3d_device_set_base_vertex_index(device->wined3d_device, vb_pos / vertex_stride - min_vertex_idx);
2576 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type, 0);
2577 hr = wined3d_device_draw_indexed_primitive(device->wined3d_device, ib_pos / idx_fmt_size, idx_count);
2579 wined3d_device_set_stream_source(device->wined3d_device, 0, NULL, 0, 0);
2580 wined3d_device_set_index_buffer(device->wined3d_device, NULL, WINED3DFMT_UNKNOWN, 0);
2581 wined3d_device_set_base_vertex_index(device->wined3d_device, 0);
2583 done:
2584 wined3d_mutex_unlock();
2585 return hr;
2588 static HRESULT WINAPI d3d8_device_ProcessVertices(IDirect3DDevice8 *iface, UINT src_start_idx,
2589 UINT dst_idx, UINT vertex_count, IDirect3DVertexBuffer8 *dst_buffer, DWORD flags)
2591 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2592 struct d3d8_vertexbuffer *dst = unsafe_impl_from_IDirect3DVertexBuffer8(dst_buffer);
2593 HRESULT hr;
2595 TRACE("iface %p, src_start_idx %u, dst_idx %u, vertex_count %u, dst_buffer %p, flags %#x.\n",
2596 iface, src_start_idx, dst_idx, vertex_count, dst_buffer, flags);
2598 wined3d_mutex_lock();
2599 hr = wined3d_device_process_vertices(device->wined3d_device, src_start_idx, dst_idx,
2600 vertex_count, dst->wined3d_buffer, NULL, flags, dst->fvf);
2601 wined3d_mutex_unlock();
2603 return hr;
2606 static HRESULT WINAPI d3d8_device_CreateVertexShader(IDirect3DDevice8 *iface,
2607 const DWORD *declaration, const DWORD *byte_code, DWORD *shader, DWORD usage)
2609 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2610 struct d3d8_vertex_shader *object;
2611 DWORD shader_handle;
2612 DWORD handle;
2613 HRESULT hr;
2615 TRACE("iface %p, declaration %p, byte_code %p, shader %p, usage %#x.\n",
2616 iface, declaration, byte_code, shader, usage);
2618 if (!(object = heap_alloc_zero(sizeof(*object))))
2620 *shader = 0;
2621 return E_OUTOFMEMORY;
2624 wined3d_mutex_lock();
2625 handle = d3d8_allocate_handle(&device->handle_table, object, D3D8_HANDLE_VS);
2626 wined3d_mutex_unlock();
2627 if (handle == D3D8_INVALID_HANDLE)
2629 ERR("Failed to allocate vertex shader handle.\n");
2630 heap_free(object);
2631 *shader = 0;
2632 return E_OUTOFMEMORY;
2635 shader_handle = handle + VS_HIGHESTFIXEDFXF + 1;
2637 hr = d3d8_vertex_shader_init(object, device, declaration, byte_code, shader_handle, usage);
2638 if (FAILED(hr))
2640 WARN("Failed to initialize vertex shader, hr %#x.\n", hr);
2641 wined3d_mutex_lock();
2642 d3d8_free_handle(&device->handle_table, handle, D3D8_HANDLE_VS);
2643 wined3d_mutex_unlock();
2644 heap_free(object);
2645 *shader = 0;
2646 return hr;
2649 TRACE("Created vertex shader %p (handle %#x).\n", object, shader_handle);
2650 *shader = shader_handle;
2652 return D3D_OK;
2655 static struct d3d8_vertex_declaration *d3d8_device_get_fvf_declaration(struct d3d8_device *device, DWORD fvf)
2657 struct d3d8_vertex_declaration *d3d8_declaration;
2658 struct FvfToDecl *convertedDecls = device->decls;
2659 int p, low, high; /* deliberately signed */
2660 HRESULT hr;
2662 TRACE("Searching for declaration for fvf %08x... ", fvf);
2664 low = 0;
2665 high = device->numConvertedDecls - 1;
2666 while (low <= high)
2668 p = (low + high) >> 1;
2669 TRACE("%d ", p);
2671 if (convertedDecls[p].fvf == fvf)
2673 TRACE("found %p\n", convertedDecls[p].declaration);
2674 return convertedDecls[p].declaration;
2677 if (convertedDecls[p].fvf < fvf)
2678 low = p + 1;
2679 else
2680 high = p - 1;
2682 TRACE("not found. Creating and inserting at position %d.\n", low);
2684 if (!(d3d8_declaration = heap_alloc(sizeof(*d3d8_declaration))))
2685 return NULL;
2687 if (FAILED(hr = d3d8_vertex_declaration_init_fvf(d3d8_declaration, device, fvf)))
2689 WARN("Failed to initialize vertex declaration, hr %#x.\n", hr);
2690 heap_free(d3d8_declaration);
2691 return NULL;
2694 if (device->declArraySize == device->numConvertedDecls)
2696 UINT grow = device->declArraySize / 2;
2698 if (!(convertedDecls = heap_realloc(convertedDecls,
2699 sizeof(*convertedDecls) * (device->numConvertedDecls + grow))))
2701 d3d8_vertex_declaration_destroy(d3d8_declaration);
2702 return NULL;
2704 device->decls = convertedDecls;
2705 device->declArraySize += grow;
2708 memmove(convertedDecls + low + 1, convertedDecls + low,
2709 sizeof(*convertedDecls) * (device->numConvertedDecls - low));
2710 convertedDecls[low].declaration = d3d8_declaration;
2711 convertedDecls[low].fvf = fvf;
2712 ++device->numConvertedDecls;
2714 TRACE("Returning %p. %u decls in array.\n", d3d8_declaration, device->numConvertedDecls);
2716 return d3d8_declaration;
2719 static HRESULT WINAPI d3d8_device_SetVertexShader(IDirect3DDevice8 *iface, DWORD shader)
2721 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2722 struct d3d8_vertex_shader *shader_impl;
2724 TRACE("iface %p, shader %#x.\n", iface, shader);
2726 if (VS_HIGHESTFIXEDFXF >= shader)
2728 TRACE("Setting FVF, %#x\n", shader);
2730 wined3d_mutex_lock();
2731 wined3d_device_set_vertex_declaration(device->wined3d_device,
2732 d3d8_device_get_fvf_declaration(device, shader)->wined3d_vertex_declaration);
2733 wined3d_device_set_vertex_shader(device->wined3d_device, NULL);
2734 wined3d_mutex_unlock();
2736 return D3D_OK;
2739 TRACE("Setting shader\n");
2741 wined3d_mutex_lock();
2742 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS)))
2744 WARN("Invalid handle (%#x) passed.\n", shader);
2745 wined3d_mutex_unlock();
2747 return D3DERR_INVALIDCALL;
2750 wined3d_device_set_vertex_declaration(device->wined3d_device,
2751 shader_impl->vertex_declaration->wined3d_vertex_declaration);
2752 wined3d_device_set_vertex_shader(device->wined3d_device, shader_impl->wined3d_shader);
2753 wined3d_mutex_unlock();
2755 return D3D_OK;
2758 static HRESULT WINAPI d3d8_device_GetVertexShader(IDirect3DDevice8 *iface, DWORD *shader)
2760 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2761 struct wined3d_vertex_declaration *wined3d_declaration;
2762 struct d3d8_vertex_declaration *d3d8_declaration;
2764 TRACE("iface %p, shader %p.\n", iface, shader);
2766 wined3d_mutex_lock();
2767 if ((wined3d_declaration = wined3d_device_get_vertex_declaration(device->wined3d_device)))
2769 d3d8_declaration = wined3d_vertex_declaration_get_parent(wined3d_declaration);
2770 *shader = d3d8_declaration->shader_handle;
2772 else
2774 *shader = 0;
2776 wined3d_mutex_unlock();
2778 TRACE("Returning %#x.\n", *shader);
2780 return D3D_OK;
2783 static HRESULT WINAPI d3d8_device_DeleteVertexShader(IDirect3DDevice8 *iface, DWORD shader)
2785 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2786 struct d3d8_vertex_shader *shader_impl;
2788 TRACE("iface %p, shader %#x.\n", iface, shader);
2790 wined3d_mutex_lock();
2791 if (!(shader_impl = d3d8_free_handle(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS)))
2793 WARN("Invalid handle (%#x) passed.\n", shader);
2794 wined3d_mutex_unlock();
2796 return D3DERR_INVALIDCALL;
2799 if (shader_impl->wined3d_shader
2800 && wined3d_device_get_vertex_shader(device->wined3d_device) == shader_impl->wined3d_shader)
2801 IDirect3DDevice8_SetVertexShader(iface, 0);
2803 wined3d_mutex_unlock();
2805 d3d8_vertex_shader_destroy(shader_impl);
2807 return D3D_OK;
2810 static HRESULT WINAPI d3d8_device_SetVertexShaderConstant(IDirect3DDevice8 *iface,
2811 DWORD start_register, const void *data, DWORD count)
2813 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2814 HRESULT hr;
2816 TRACE("iface %p, start_register %u, data %p, count %u.\n",
2817 iface, start_register, data, count);
2819 if (start_register + count > D3D8_MAX_VERTEX_SHADER_CONSTANTF)
2821 WARN("Trying to access %u constants, but d3d8 only supports %u\n",
2822 start_register + count, D3D8_MAX_VERTEX_SHADER_CONSTANTF);
2823 return D3DERR_INVALIDCALL;
2826 wined3d_mutex_lock();
2827 hr = wined3d_device_set_vs_consts_f(device->wined3d_device, start_register, count, data);
2828 wined3d_mutex_unlock();
2830 return hr;
2833 static HRESULT WINAPI d3d8_device_GetVertexShaderConstant(IDirect3DDevice8 *iface,
2834 DWORD start_register, void *data, DWORD count)
2836 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2837 HRESULT hr;
2839 TRACE("iface %p, start_register %u, data %p, count %u.\n",
2840 iface, start_register, data, count);
2842 if (start_register + count > D3D8_MAX_VERTEX_SHADER_CONSTANTF)
2844 WARN("Trying to access %u constants, but d3d8 only supports %u\n",
2845 start_register + count, D3D8_MAX_VERTEX_SHADER_CONSTANTF);
2846 return D3DERR_INVALIDCALL;
2849 wined3d_mutex_lock();
2850 hr = wined3d_device_get_vs_consts_f(device->wined3d_device, start_register, count, data);
2851 wined3d_mutex_unlock();
2853 return hr;
2856 static HRESULT WINAPI d3d8_device_GetVertexShaderDeclaration(IDirect3DDevice8 *iface,
2857 DWORD shader, void *data, DWORD *data_size)
2859 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2860 struct d3d8_vertex_declaration *declaration;
2861 struct d3d8_vertex_shader *shader_impl;
2863 TRACE("iface %p, shader %#x, data %p, data_size %p.\n",
2864 iface, shader, data, data_size);
2866 wined3d_mutex_lock();
2867 shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS);
2868 wined3d_mutex_unlock();
2870 if (!shader_impl)
2872 WARN("Invalid handle (%#x) passed.\n", shader);
2873 return D3DERR_INVALIDCALL;
2875 declaration = shader_impl->vertex_declaration;
2877 if (!data)
2879 *data_size = declaration->elements_size;
2880 return D3D_OK;
2883 /* MSDN claims that if *data_size is smaller than the required size
2884 * we should write the required size and return D3DERR_MOREDATA.
2885 * That's not actually true. */
2886 if (*data_size < declaration->elements_size)
2887 return D3DERR_INVALIDCALL;
2889 memcpy(data, declaration->elements, declaration->elements_size);
2891 return D3D_OK;
2894 static HRESULT WINAPI d3d8_device_GetVertexShaderFunction(IDirect3DDevice8 *iface,
2895 DWORD shader, void *data, DWORD *data_size)
2897 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2898 struct d3d8_vertex_shader *shader_impl = NULL;
2899 HRESULT hr;
2901 TRACE("iface %p, shader %#x, data %p, data_size %p.\n",
2902 iface, shader, data, data_size);
2904 wined3d_mutex_lock();
2905 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS)))
2907 WARN("Invalid handle (%#x) passed.\n", shader);
2908 wined3d_mutex_unlock();
2910 return D3DERR_INVALIDCALL;
2913 if (!shader_impl->wined3d_shader)
2915 wined3d_mutex_unlock();
2916 *data_size = 0;
2917 return D3D_OK;
2920 hr = wined3d_shader_get_byte_code(shader_impl->wined3d_shader, data, data_size);
2921 wined3d_mutex_unlock();
2923 return hr;
2926 static HRESULT WINAPI d3d8_device_SetIndices(IDirect3DDevice8 *iface,
2927 IDirect3DIndexBuffer8 *buffer, UINT base_vertex_idx)
2929 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2930 struct d3d8_indexbuffer *ib = unsafe_impl_from_IDirect3DIndexBuffer8(buffer);
2931 struct wined3d_buffer *wined3d_buffer;
2933 TRACE("iface %p, buffer %p, base_vertex_idx %u.\n", iface, buffer, base_vertex_idx);
2935 if (!ib)
2936 wined3d_buffer = NULL;
2937 else if (ib->draw_buffer)
2938 wined3d_buffer = ib->draw_buffer;
2939 else
2940 wined3d_buffer = ib->wined3d_buffer;
2942 /* WineD3D takes an INT(due to d3d9), but d3d8 uses UINTs. Do I have to add a check here that
2943 * the UINT doesn't cause an overflow in the INT? It seems rather unlikely because such large
2944 * vertex buffers can't be created to address them with an index that requires the 32nd bit
2945 * (4 Byte minimum vertex size * 2^31-1 -> 8 gb buffer. The index sign would be the least
2946 * problem)
2948 wined3d_mutex_lock();
2949 wined3d_device_set_base_vertex_index(device->wined3d_device, base_vertex_idx);
2950 wined3d_device_set_index_buffer(device->wined3d_device, wined3d_buffer, ib ? ib->format : WINED3DFMT_UNKNOWN, 0);
2951 if (!device->recording)
2952 device->sysmem_ib = ib && ib->draw_buffer;
2953 wined3d_mutex_unlock();
2955 return D3D_OK;
2958 static HRESULT WINAPI d3d8_device_GetIndices(IDirect3DDevice8 *iface,
2959 IDirect3DIndexBuffer8 **buffer, UINT *base_vertex_index)
2961 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2962 enum wined3d_format_id wined3d_format;
2963 struct wined3d_buffer *wined3d_buffer;
2964 struct d3d8_indexbuffer *buffer_impl;
2966 TRACE("iface %p, buffer %p, base_vertex_index %p.\n", iface, buffer, base_vertex_index);
2968 if (!buffer)
2969 return D3DERR_INVALIDCALL;
2971 /* The case from UINT to INT is safe because d3d8 will never set negative values */
2972 wined3d_mutex_lock();
2973 *base_vertex_index = wined3d_device_get_base_vertex_index(device->wined3d_device);
2974 if ((wined3d_buffer = wined3d_device_get_index_buffer(device->wined3d_device, &wined3d_format, NULL)))
2976 buffer_impl = wined3d_buffer_get_parent(wined3d_buffer);
2977 *buffer = &buffer_impl->IDirect3DIndexBuffer8_iface;
2978 IDirect3DIndexBuffer8_AddRef(*buffer);
2980 else
2982 *buffer = NULL;
2984 wined3d_mutex_unlock();
2986 return D3D_OK;
2989 static HRESULT WINAPI d3d8_device_CreatePixelShader(IDirect3DDevice8 *iface,
2990 const DWORD *byte_code, DWORD *shader)
2992 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2993 struct d3d8_pixel_shader *object;
2994 DWORD shader_handle;
2995 DWORD handle;
2996 HRESULT hr;
2998 TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
3000 if (!shader)
3001 return D3DERR_INVALIDCALL;
3003 if (!(object = heap_alloc_zero(sizeof(*object))))
3004 return E_OUTOFMEMORY;
3006 wined3d_mutex_lock();
3007 handle = d3d8_allocate_handle(&device->handle_table, object, D3D8_HANDLE_PS);
3008 wined3d_mutex_unlock();
3009 if (handle == D3D8_INVALID_HANDLE)
3011 ERR("Failed to allocate pixel shader handle.\n");
3012 heap_free(object);
3013 return E_OUTOFMEMORY;
3016 shader_handle = handle + VS_HIGHESTFIXEDFXF + 1;
3018 hr = d3d8_pixel_shader_init(object, device, byte_code, shader_handle);
3019 if (FAILED(hr))
3021 WARN("Failed to initialize pixel shader, hr %#x.\n", hr);
3022 wined3d_mutex_lock();
3023 d3d8_free_handle(&device->handle_table, handle, D3D8_HANDLE_PS);
3024 wined3d_mutex_unlock();
3025 heap_free(object);
3026 *shader = 0;
3027 return hr;
3030 TRACE("Created pixel shader %p (handle %#x).\n", object, shader_handle);
3031 *shader = shader_handle;
3033 return D3D_OK;
3036 static HRESULT WINAPI d3d8_device_SetPixelShader(IDirect3DDevice8 *iface, DWORD shader)
3038 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3039 struct d3d8_pixel_shader *shader_impl;
3041 TRACE("iface %p, shader %#x.\n", iface, shader);
3043 wined3d_mutex_lock();
3045 if (!shader)
3047 wined3d_device_set_pixel_shader(device->wined3d_device, NULL);
3048 wined3d_mutex_unlock();
3049 return D3D_OK;
3052 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS)))
3054 WARN("Invalid handle (%#x) passed.\n", shader);
3055 wined3d_mutex_unlock();
3056 return D3DERR_INVALIDCALL;
3059 TRACE("Setting shader %p.\n", shader_impl);
3060 wined3d_device_set_pixel_shader(device->wined3d_device, shader_impl->wined3d_shader);
3061 wined3d_mutex_unlock();
3063 return D3D_OK;
3066 static HRESULT WINAPI d3d8_device_GetPixelShader(IDirect3DDevice8 *iface, DWORD *shader)
3068 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3069 struct wined3d_shader *object;
3071 TRACE("iface %p, shader %p.\n", iface, shader);
3073 if (!shader)
3074 return D3DERR_INVALIDCALL;
3076 wined3d_mutex_lock();
3077 if ((object = wined3d_device_get_pixel_shader(device->wined3d_device)))
3079 struct d3d8_pixel_shader *d3d8_shader;
3080 d3d8_shader = wined3d_shader_get_parent(object);
3081 *shader = d3d8_shader->handle;
3083 else
3085 *shader = 0;
3087 wined3d_mutex_unlock();
3089 TRACE("Returning %#x.\n", *shader);
3091 return D3D_OK;
3094 static HRESULT WINAPI d3d8_device_DeletePixelShader(IDirect3DDevice8 *iface, DWORD shader)
3096 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3097 struct d3d8_pixel_shader *shader_impl;
3099 TRACE("iface %p, shader %#x.\n", iface, shader);
3101 wined3d_mutex_lock();
3103 if (!(shader_impl = d3d8_free_handle(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS)))
3105 WARN("Invalid handle (%#x) passed.\n", shader);
3106 wined3d_mutex_unlock();
3107 return D3DERR_INVALIDCALL;
3110 if (wined3d_device_get_pixel_shader(device->wined3d_device) == shader_impl->wined3d_shader)
3111 IDirect3DDevice8_SetPixelShader(iface, 0);
3113 wined3d_mutex_unlock();
3115 d3d8_pixel_shader_destroy(shader_impl);
3117 return D3D_OK;
3120 static HRESULT WINAPI d3d8_device_SetPixelShaderConstant(IDirect3DDevice8 *iface,
3121 DWORD start_register, const void *data, DWORD count)
3123 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3124 HRESULT hr;
3126 TRACE("iface %p, start_register %u, data %p, count %u.\n",
3127 iface, start_register, data, count);
3129 wined3d_mutex_lock();
3130 hr = wined3d_device_set_ps_consts_f(device->wined3d_device, start_register, count, data);
3131 wined3d_mutex_unlock();
3133 return hr;
3136 static HRESULT WINAPI d3d8_device_GetPixelShaderConstant(IDirect3DDevice8 *iface,
3137 DWORD start_register, void *data, DWORD count)
3139 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3140 HRESULT hr;
3142 TRACE("iface %p, start_register %u, data %p, count %u.\n",
3143 iface, start_register, data, count);
3145 wined3d_mutex_lock();
3146 hr = wined3d_device_get_ps_consts_f(device->wined3d_device, start_register, count, data);
3147 wined3d_mutex_unlock();
3149 return hr;
3152 static HRESULT WINAPI d3d8_device_GetPixelShaderFunction(IDirect3DDevice8 *iface,
3153 DWORD shader, void *data, DWORD *data_size)
3155 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3156 struct d3d8_pixel_shader *shader_impl = NULL;
3157 HRESULT hr;
3159 TRACE("iface %p, shader %#x, data %p, data_size %p.\n",
3160 iface, shader, data, data_size);
3162 wined3d_mutex_lock();
3163 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS)))
3165 WARN("Invalid handle (%#x) passed.\n", shader);
3166 wined3d_mutex_unlock();
3168 return D3DERR_INVALIDCALL;
3171 hr = wined3d_shader_get_byte_code(shader_impl->wined3d_shader, data, data_size);
3172 wined3d_mutex_unlock();
3174 return hr;
3177 static HRESULT WINAPI d3d8_device_DrawRectPatch(IDirect3DDevice8 *iface, UINT handle,
3178 const float *segment_count, const D3DRECTPATCH_INFO *patch_info)
3180 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
3181 iface, handle, segment_count, patch_info);
3182 return D3D_OK;
3185 static HRESULT WINAPI d3d8_device_DrawTriPatch(IDirect3DDevice8 *iface, UINT handle,
3186 const float *segment_count, const D3DTRIPATCH_INFO *patch_info)
3188 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
3189 iface, handle, segment_count, patch_info);
3190 return D3D_OK;
3193 static HRESULT WINAPI d3d8_device_DeletePatch(IDirect3DDevice8 *iface, UINT handle)
3195 FIXME("iface %p, handle %#x unimplemented.\n", iface, handle);
3196 return D3DERR_INVALIDCALL;
3199 static HRESULT WINAPI d3d8_device_SetStreamSource(IDirect3DDevice8 *iface,
3200 UINT stream_idx, IDirect3DVertexBuffer8 *buffer, UINT stride)
3202 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3203 struct d3d8_vertexbuffer *buffer_impl = unsafe_impl_from_IDirect3DVertexBuffer8(buffer);
3204 struct wined3d_buffer *wined3d_buffer;
3205 HRESULT hr;
3207 TRACE("iface %p, stream_idx %u, buffer %p, stride %u.\n",
3208 iface, stream_idx, buffer, stride);
3210 wined3d_mutex_lock();
3211 if (!stride)
3213 unsigned int cur_offset;
3215 hr = wined3d_device_get_stream_source(device->wined3d_device, stream_idx, &wined3d_buffer,
3216 &cur_offset, &stride);
3219 if (!buffer_impl)
3220 wined3d_buffer = NULL;
3221 else if (buffer_impl->draw_buffer)
3222 wined3d_buffer = buffer_impl->draw_buffer;
3223 else
3224 wined3d_buffer = buffer_impl->wined3d_buffer;
3226 hr = wined3d_device_set_stream_source(device->wined3d_device, stream_idx, wined3d_buffer, 0, stride);
3227 if (SUCCEEDED(hr) && !device->recording)
3229 if (buffer_impl && buffer_impl->draw_buffer)
3230 device->sysmem_vb |= (1u << stream_idx);
3231 else
3232 device->sysmem_vb &= ~(1u << stream_idx);
3235 wined3d_mutex_unlock();
3237 return hr;
3240 static HRESULT WINAPI d3d8_device_GetStreamSource(IDirect3DDevice8 *iface,
3241 UINT stream_idx, IDirect3DVertexBuffer8 **buffer, UINT *stride)
3243 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3244 struct d3d8_vertexbuffer *buffer_impl;
3245 struct wined3d_buffer *wined3d_buffer = NULL;
3246 HRESULT hr;
3248 TRACE("iface %p, stream_idx %u, buffer %p, stride %p.\n",
3249 iface, stream_idx, buffer, stride);
3251 if (!buffer)
3252 return D3DERR_INVALIDCALL;
3254 wined3d_mutex_lock();
3255 hr = wined3d_device_get_stream_source(device->wined3d_device, stream_idx, &wined3d_buffer, 0, stride);
3256 if (SUCCEEDED(hr) && wined3d_buffer)
3258 buffer_impl = wined3d_buffer_get_parent(wined3d_buffer);
3259 *buffer = &buffer_impl->IDirect3DVertexBuffer8_iface;
3260 IDirect3DVertexBuffer8_AddRef(*buffer);
3262 else
3264 if (FAILED(hr))
3265 ERR("Failed to get wined3d stream source, hr %#x.\n", hr);
3266 *buffer = NULL;
3268 wined3d_mutex_unlock();
3270 return hr;
3273 static const struct IDirect3DDevice8Vtbl d3d8_device_vtbl =
3275 d3d8_device_QueryInterface,
3276 d3d8_device_AddRef,
3277 d3d8_device_Release,
3278 d3d8_device_TestCooperativeLevel,
3279 d3d8_device_GetAvailableTextureMem,
3280 d3d8_device_ResourceManagerDiscardBytes,
3281 d3d8_device_GetDirect3D,
3282 d3d8_device_GetDeviceCaps,
3283 d3d8_device_GetDisplayMode,
3284 d3d8_device_GetCreationParameters,
3285 d3d8_device_SetCursorProperties,
3286 d3d8_device_SetCursorPosition,
3287 d3d8_device_ShowCursor,
3288 d3d8_device_CreateAdditionalSwapChain,
3289 d3d8_device_Reset,
3290 d3d8_device_Present,
3291 d3d8_device_GetBackBuffer,
3292 d3d8_device_GetRasterStatus,
3293 d3d8_device_SetGammaRamp,
3294 d3d8_device_GetGammaRamp,
3295 d3d8_device_CreateTexture,
3296 d3d8_device_CreateVolumeTexture,
3297 d3d8_device_CreateCubeTexture,
3298 d3d8_device_CreateVertexBuffer,
3299 d3d8_device_CreateIndexBuffer,
3300 d3d8_device_CreateRenderTarget,
3301 d3d8_device_CreateDepthStencilSurface,
3302 d3d8_device_CreateImageSurface,
3303 d3d8_device_CopyRects,
3304 d3d8_device_UpdateTexture,
3305 d3d8_device_GetFrontBuffer,
3306 d3d8_device_SetRenderTarget,
3307 d3d8_device_GetRenderTarget,
3308 d3d8_device_GetDepthStencilSurface,
3309 d3d8_device_BeginScene,
3310 d3d8_device_EndScene,
3311 d3d8_device_Clear,
3312 d3d8_device_SetTransform,
3313 d3d8_device_GetTransform,
3314 d3d8_device_MultiplyTransform,
3315 d3d8_device_SetViewport,
3316 d3d8_device_GetViewport,
3317 d3d8_device_SetMaterial,
3318 d3d8_device_GetMaterial,
3319 d3d8_device_SetLight,
3320 d3d8_device_GetLight,
3321 d3d8_device_LightEnable,
3322 d3d8_device_GetLightEnable,
3323 d3d8_device_SetClipPlane,
3324 d3d8_device_GetClipPlane,
3325 d3d8_device_SetRenderState,
3326 d3d8_device_GetRenderState,
3327 d3d8_device_BeginStateBlock,
3328 d3d8_device_EndStateBlock,
3329 d3d8_device_ApplyStateBlock,
3330 d3d8_device_CaptureStateBlock,
3331 d3d8_device_DeleteStateBlock,
3332 d3d8_device_CreateStateBlock,
3333 d3d8_device_SetClipStatus,
3334 d3d8_device_GetClipStatus,
3335 d3d8_device_GetTexture,
3336 d3d8_device_SetTexture,
3337 d3d8_device_GetTextureStageState,
3338 d3d8_device_SetTextureStageState,
3339 d3d8_device_ValidateDevice,
3340 d3d8_device_GetInfo,
3341 d3d8_device_SetPaletteEntries,
3342 d3d8_device_GetPaletteEntries,
3343 d3d8_device_SetCurrentTexturePalette,
3344 d3d8_device_GetCurrentTexturePalette,
3345 d3d8_device_DrawPrimitive,
3346 d3d8_device_DrawIndexedPrimitive,
3347 d3d8_device_DrawPrimitiveUP,
3348 d3d8_device_DrawIndexedPrimitiveUP,
3349 d3d8_device_ProcessVertices,
3350 d3d8_device_CreateVertexShader,
3351 d3d8_device_SetVertexShader,
3352 d3d8_device_GetVertexShader,
3353 d3d8_device_DeleteVertexShader,
3354 d3d8_device_SetVertexShaderConstant,
3355 d3d8_device_GetVertexShaderConstant,
3356 d3d8_device_GetVertexShaderDeclaration,
3357 d3d8_device_GetVertexShaderFunction,
3358 d3d8_device_SetStreamSource,
3359 d3d8_device_GetStreamSource,
3360 d3d8_device_SetIndices,
3361 d3d8_device_GetIndices,
3362 d3d8_device_CreatePixelShader,
3363 d3d8_device_SetPixelShader,
3364 d3d8_device_GetPixelShader,
3365 d3d8_device_DeletePixelShader,
3366 d3d8_device_SetPixelShaderConstant,
3367 d3d8_device_GetPixelShaderConstant,
3368 d3d8_device_GetPixelShaderFunction,
3369 d3d8_device_DrawRectPatch,
3370 d3d8_device_DrawTriPatch,
3371 d3d8_device_DeletePatch,
3374 static inline struct d3d8_device *device_from_device_parent(struct wined3d_device_parent *device_parent)
3376 return CONTAINING_RECORD(device_parent, struct d3d8_device, device_parent);
3379 static void CDECL device_parent_wined3d_device_created(struct wined3d_device_parent *device_parent,
3380 struct wined3d_device *device)
3382 TRACE("device_parent %p, device %p\n", device_parent, device);
3385 static void CDECL device_parent_mode_changed(struct wined3d_device_parent *device_parent)
3387 TRACE("device_parent %p.\n", device_parent);
3390 static void CDECL device_parent_activate(struct wined3d_device_parent *device_parent, BOOL activate)
3392 struct d3d8_device *device = device_from_device_parent(device_parent);
3394 TRACE("device_parent %p, activate %#x.\n", device_parent, activate);
3396 if (!activate)
3397 InterlockedCompareExchange(&device->device_state, D3D8_DEVICE_STATE_LOST, D3D8_DEVICE_STATE_OK);
3398 else
3399 InterlockedCompareExchange(&device->device_state, D3D8_DEVICE_STATE_NOT_RESET, D3D8_DEVICE_STATE_LOST);
3402 static HRESULT CDECL device_parent_texture_sub_resource_created(struct wined3d_device_parent *device_parent,
3403 enum wined3d_resource_type type, struct wined3d_texture *wined3d_texture, unsigned int sub_resource_idx,
3404 void **parent, const struct wined3d_parent_ops **parent_ops)
3406 TRACE("device_parent %p, type %#x, wined3d_texture %p, sub_resource_idx %u, parent %p, parent_ops %p.\n",
3407 device_parent, type, wined3d_texture, sub_resource_idx, parent, parent_ops);
3409 if (type == WINED3D_RTYPE_TEXTURE_2D)
3411 struct d3d8_surface *d3d_surface;
3413 if (!(d3d_surface = heap_alloc_zero(sizeof(*d3d_surface))))
3414 return E_OUTOFMEMORY;
3416 surface_init(d3d_surface, wined3d_texture, sub_resource_idx, parent_ops);
3417 *parent = d3d_surface;
3418 TRACE("Created surface %p.\n", d3d_surface);
3420 else if (type == WINED3D_RTYPE_TEXTURE_3D)
3422 struct d3d8_volume *d3d_volume;
3424 if (!(d3d_volume = heap_alloc_zero(sizeof(*d3d_volume))))
3425 return E_OUTOFMEMORY;
3427 volume_init(d3d_volume, wined3d_texture, sub_resource_idx, parent_ops);
3428 *parent = d3d_volume;
3429 TRACE("Created volume %p.\n", d3d_volume);
3431 else
3433 ERR("Unhandled resource type %#x.\n", type);
3434 return E_FAIL;
3437 return D3D_OK;
3440 static HRESULT CDECL device_parent_create_swapchain_texture(struct wined3d_device_parent *device_parent,
3441 void *container_parent, const struct wined3d_resource_desc *desc, DWORD texture_flags,
3442 struct wined3d_texture **texture)
3444 struct d3d8_device *device = device_from_device_parent(device_parent);
3445 struct d3d8_surface *d3d_surface;
3446 HRESULT hr;
3448 TRACE("device_parent %p, container_parent %p, desc %p, texture flags %#x, texture %p.\n",
3449 device_parent, container_parent, desc, texture_flags, texture);
3451 if (FAILED(hr = wined3d_texture_create(device->wined3d_device, desc, 1, 1, texture_flags,
3452 NULL, &device->IDirect3DDevice8_iface, &d3d8_null_wined3d_parent_ops, texture)))
3454 WARN("Failed to create texture, hr %#x.\n", hr);
3455 return hr;
3458 d3d_surface = wined3d_texture_get_sub_resource_parent(*texture, 0);
3459 d3d_surface->parent_device = &device->IDirect3DDevice8_iface;
3461 return hr;
3464 static HRESULT CDECL device_parent_create_swapchain(struct wined3d_device_parent *device_parent,
3465 struct wined3d_swapchain_desc *desc, struct wined3d_swapchain **swapchain)
3467 struct d3d8_device *device = device_from_device_parent(device_parent);
3468 struct d3d8_swapchain *d3d_swapchain;
3469 HRESULT hr;
3471 TRACE("device_parent %p, desc %p, swapchain %p.\n", device_parent, desc, swapchain);
3473 if (FAILED(hr = d3d8_swapchain_create(device, desc, WINED3D_SWAP_INTERVAL_DEFAULT, &d3d_swapchain)))
3475 WARN("Failed to create swapchain, hr %#x.\n", hr);
3476 *swapchain = NULL;
3477 return hr;
3480 *swapchain = d3d_swapchain->wined3d_swapchain;
3481 wined3d_swapchain_incref(*swapchain);
3482 IDirect3DSwapChain8_Release(&d3d_swapchain->IDirect3DSwapChain8_iface);
3484 return hr;
3487 static const struct wined3d_device_parent_ops d3d8_wined3d_device_parent_ops =
3489 device_parent_wined3d_device_created,
3490 device_parent_mode_changed,
3491 device_parent_activate,
3492 device_parent_texture_sub_resource_created,
3493 device_parent_create_swapchain_texture,
3494 device_parent_create_swapchain,
3497 static void setup_fpu(void)
3499 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
3500 WORD cw;
3501 __asm__ volatile ("fnstcw %0" : "=m" (cw));
3502 cw = (cw & ~0xf3f) | 0x3f;
3503 __asm__ volatile ("fldcw %0" : : "m" (cw));
3504 #elif defined(__i386__) && defined(_MSC_VER)
3505 WORD cw;
3506 __asm fnstcw cw;
3507 cw = (cw & ~0xf3f) | 0x3f;
3508 __asm fldcw cw;
3509 #else
3510 FIXME("FPU setup not implemented for this platform.\n");
3511 #endif
3514 HRESULT device_init(struct d3d8_device *device, struct d3d8 *parent, struct wined3d *wined3d, UINT adapter,
3515 D3DDEVTYPE device_type, HWND focus_window, DWORD flags, D3DPRESENT_PARAMETERS *parameters)
3517 struct wined3d_swapchain_desc swapchain_desc;
3518 struct wined3d_swapchain *wined3d_swapchain;
3519 HRESULT hr;
3521 static const enum wined3d_feature_level feature_levels[] =
3523 WINED3D_FEATURE_LEVEL_8,
3524 WINED3D_FEATURE_LEVEL_7,
3525 WINED3D_FEATURE_LEVEL_6,
3526 WINED3D_FEATURE_LEVEL_5,
3529 device->IDirect3DDevice8_iface.lpVtbl = &d3d8_device_vtbl;
3530 device->device_parent.ops = &d3d8_wined3d_device_parent_ops;
3531 device->ref = 1;
3532 if (!(device->handle_table.entries = heap_alloc_zero(D3D8_INITIAL_HANDLE_TABLE_SIZE
3533 * sizeof(*device->handle_table.entries))))
3535 ERR("Failed to allocate handle table memory.\n");
3536 return E_OUTOFMEMORY;
3538 device->handle_table.table_size = D3D8_INITIAL_HANDLE_TABLE_SIZE;
3540 if (!(flags & D3DCREATE_FPU_PRESERVE)) setup_fpu();
3542 wined3d_mutex_lock();
3543 if (FAILED(hr = wined3d_device_create(wined3d, adapter, device_type,
3544 focus_window, flags, 4, feature_levels, ARRAY_SIZE(feature_levels),
3545 &device->device_parent, &device->wined3d_device)))
3547 WARN("Failed to create wined3d device, hr %#x.\n", hr);
3548 wined3d_mutex_unlock();
3549 heap_free(device->handle_table.entries);
3550 return hr;
3553 if (!parameters->Windowed)
3555 HWND device_window = parameters->hDeviceWindow;
3557 if (!focus_window)
3558 focus_window = device_window;
3559 if (FAILED(hr = wined3d_device_acquire_focus_window(device->wined3d_device, focus_window)))
3561 ERR("Failed to acquire focus window, hr %#x.\n", hr);
3562 wined3d_device_decref(device->wined3d_device);
3563 wined3d_mutex_unlock();
3564 heap_free(device->handle_table.entries);
3565 return hr;
3568 if (!device_window)
3569 device_window = focus_window;
3570 wined3d_device_setup_fullscreen_window(device->wined3d_device, device_window,
3571 parameters->BackBufferWidth,
3572 parameters->BackBufferHeight);
3575 if (flags & D3DCREATE_MULTITHREADED)
3576 wined3d_device_set_multithreaded(device->wined3d_device);
3578 if (!wined3d_swapchain_desc_from_present_parameters(&swapchain_desc, parameters))
3580 wined3d_device_release_focus_window(device->wined3d_device);
3581 wined3d_device_decref(device->wined3d_device);
3582 wined3d_mutex_unlock();
3583 heap_free(device->handle_table.entries);
3584 return D3DERR_INVALIDCALL;
3587 if (FAILED(hr = wined3d_device_init_3d(device->wined3d_device, &swapchain_desc)))
3589 WARN("Failed to initialize 3D, hr %#x.\n", hr);
3590 wined3d_device_release_focus_window(device->wined3d_device);
3591 wined3d_device_decref(device->wined3d_device);
3592 wined3d_mutex_unlock();
3593 heap_free(device->handle_table.entries);
3594 return hr;
3597 wined3d_device_set_render_state(device->wined3d_device,
3598 WINED3D_RS_ZENABLE, !!swapchain_desc.enable_auto_depth_stencil);
3599 wined3d_device_set_render_state(device->wined3d_device, WINED3D_RS_POINTSIZE_MIN, 0);
3600 wined3d_mutex_unlock();
3602 present_parameters_from_wined3d_swapchain_desc(parameters,
3603 &swapchain_desc, parameters->FullScreen_PresentationInterval);
3605 device->declArraySize = 16;
3606 if (!(device->decls = heap_alloc(device->declArraySize * sizeof(*device->decls))))
3608 ERR("Failed to allocate FVF vertex declaration map memory.\n");
3609 hr = E_OUTOFMEMORY;
3610 goto err;
3613 wined3d_swapchain = wined3d_device_get_swapchain(device->wined3d_device, 0);
3614 device->implicit_swapchain = wined3d_swapchain_get_parent(wined3d_swapchain);
3615 device->implicit_swapchain->swap_interval
3616 = wined3dswapinterval_from_d3d(parameters->FullScreen_PresentationInterval);
3618 device->d3d_parent = &parent->IDirect3D8_iface;
3619 IDirect3D8_AddRef(device->d3d_parent);
3621 return D3D_OK;
3623 err:
3624 wined3d_mutex_lock();
3625 wined3d_device_uninit_3d(device->wined3d_device);
3626 wined3d_device_release_focus_window(device->wined3d_device);
3627 wined3d_device_decref(device->wined3d_device);
3628 wined3d_mutex_unlock();
3629 heap_free(device->handle_table.entries);
3630 return hr;