d3d8: Pass the container to d3d8_surface_create().
[wine.git] / dlls / d3d8 / device.c
blob8ae6b5ce7b99307fe32919c9f9685e42637ab18c
1 /*
2 * IDirect3DDevice8 implementation
4 * Copyright 2002-2004 Jason Edmeades
5 * Copyright 2004 Christian Costa
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "d3d8_private.h"
24 WINE_DEFAULT_DEBUG_CHANNEL(d3d8);
26 static void STDMETHODCALLTYPE d3d8_null_wined3d_object_destroyed(void *parent) {}
28 const struct wined3d_parent_ops d3d8_null_wined3d_parent_ops =
30 d3d8_null_wined3d_object_destroyed,
33 D3DFORMAT d3dformat_from_wined3dformat(enum wined3d_format_id format)
35 BYTE *c = (BYTE *)&format;
37 /* Don't translate FOURCC formats */
38 if (isprint(c[0]) && isprint(c[1]) && isprint(c[2]) && isprint(c[3]))
39 return (D3DFORMAT)format;
41 switch(format)
43 case WINED3DFMT_UNKNOWN: return D3DFMT_UNKNOWN;
44 case WINED3DFMT_B8G8R8_UNORM: return D3DFMT_R8G8B8;
45 case WINED3DFMT_B8G8R8A8_UNORM: return D3DFMT_A8R8G8B8;
46 case WINED3DFMT_B8G8R8X8_UNORM: return D3DFMT_X8R8G8B8;
47 case WINED3DFMT_B5G6R5_UNORM: return D3DFMT_R5G6B5;
48 case WINED3DFMT_B5G5R5X1_UNORM: return D3DFMT_X1R5G5B5;
49 case WINED3DFMT_B5G5R5A1_UNORM: return D3DFMT_A1R5G5B5;
50 case WINED3DFMT_B4G4R4A4_UNORM: return D3DFMT_A4R4G4B4;
51 case WINED3DFMT_B2G3R3_UNORM: return D3DFMT_R3G3B2;
52 case WINED3DFMT_A8_UNORM: return D3DFMT_A8;
53 case WINED3DFMT_B2G3R3A8_UNORM: return D3DFMT_A8R3G3B2;
54 case WINED3DFMT_B4G4R4X4_UNORM: return D3DFMT_X4R4G4B4;
55 case WINED3DFMT_R10G10B10A2_UNORM: return D3DFMT_A2B10G10R10;
56 case WINED3DFMT_R16G16_UNORM: return D3DFMT_G16R16;
57 case WINED3DFMT_P8_UINT_A8_UNORM: return D3DFMT_A8P8;
58 case WINED3DFMT_P8_UINT: return D3DFMT_P8;
59 case WINED3DFMT_L8_UNORM: return D3DFMT_L8;
60 case WINED3DFMT_L8A8_UNORM: return D3DFMT_A8L8;
61 case WINED3DFMT_L4A4_UNORM: return D3DFMT_A4L4;
62 case WINED3DFMT_R8G8_SNORM: return D3DFMT_V8U8;
63 case WINED3DFMT_R5G5_SNORM_L6_UNORM: return D3DFMT_L6V5U5;
64 case WINED3DFMT_R8G8_SNORM_L8X8_UNORM: return D3DFMT_X8L8V8U8;
65 case WINED3DFMT_R8G8B8A8_SNORM: return D3DFMT_Q8W8V8U8;
66 case WINED3DFMT_R16G16_SNORM: return D3DFMT_V16U16;
67 case WINED3DFMT_R10G11B11_SNORM: return D3DFMT_W11V11U10;
68 case WINED3DFMT_R10G10B10_SNORM_A2_UNORM: return D3DFMT_A2W10V10U10;
69 case WINED3DFMT_D16_LOCKABLE: return D3DFMT_D16_LOCKABLE;
70 case WINED3DFMT_D32_UNORM: return D3DFMT_D32;
71 case WINED3DFMT_S1_UINT_D15_UNORM: return D3DFMT_D15S1;
72 case WINED3DFMT_D24_UNORM_S8_UINT: return D3DFMT_D24S8;
73 case WINED3DFMT_X8D24_UNORM: return D3DFMT_D24X8;
74 case WINED3DFMT_S4X4_UINT_D24_UNORM: return D3DFMT_D24X4S4;
75 case WINED3DFMT_D16_UNORM: return D3DFMT_D16;
76 case WINED3DFMT_R16_UINT: return D3DFMT_INDEX16;
77 case WINED3DFMT_R32_UINT: return D3DFMT_INDEX32;
78 default:
79 FIXME("Unhandled wined3d format %#x.\n", format);
80 return D3DFMT_UNKNOWN;
84 enum wined3d_format_id wined3dformat_from_d3dformat(D3DFORMAT format)
86 BYTE *c = (BYTE *)&format;
88 /* Don't translate FOURCC formats */
89 if (isprint(c[0]) && isprint(c[1]) && isprint(c[2]) && isprint(c[3]))
90 return (enum wined3d_format_id)format;
92 switch(format)
94 case D3DFMT_UNKNOWN: return WINED3DFMT_UNKNOWN;
95 case D3DFMT_R8G8B8: return WINED3DFMT_B8G8R8_UNORM;
96 case D3DFMT_A8R8G8B8: return WINED3DFMT_B8G8R8A8_UNORM;
97 case D3DFMT_X8R8G8B8: return WINED3DFMT_B8G8R8X8_UNORM;
98 case D3DFMT_R5G6B5: return WINED3DFMT_B5G6R5_UNORM;
99 case D3DFMT_X1R5G5B5: return WINED3DFMT_B5G5R5X1_UNORM;
100 case D3DFMT_A1R5G5B5: return WINED3DFMT_B5G5R5A1_UNORM;
101 case D3DFMT_A4R4G4B4: return WINED3DFMT_B4G4R4A4_UNORM;
102 case D3DFMT_R3G3B2: return WINED3DFMT_B2G3R3_UNORM;
103 case D3DFMT_A8: return WINED3DFMT_A8_UNORM;
104 case D3DFMT_A8R3G3B2: return WINED3DFMT_B2G3R3A8_UNORM;
105 case D3DFMT_X4R4G4B4: return WINED3DFMT_B4G4R4X4_UNORM;
106 case D3DFMT_A2B10G10R10: return WINED3DFMT_R10G10B10A2_UNORM;
107 case D3DFMT_G16R16: return WINED3DFMT_R16G16_UNORM;
108 case D3DFMT_A8P8: return WINED3DFMT_P8_UINT_A8_UNORM;
109 case D3DFMT_P8: return WINED3DFMT_P8_UINT;
110 case D3DFMT_L8: return WINED3DFMT_L8_UNORM;
111 case D3DFMT_A8L8: return WINED3DFMT_L8A8_UNORM;
112 case D3DFMT_A4L4: return WINED3DFMT_L4A4_UNORM;
113 case D3DFMT_V8U8: return WINED3DFMT_R8G8_SNORM;
114 case D3DFMT_L6V5U5: return WINED3DFMT_R5G5_SNORM_L6_UNORM;
115 case D3DFMT_X8L8V8U8: return WINED3DFMT_R8G8_SNORM_L8X8_UNORM;
116 case D3DFMT_Q8W8V8U8: return WINED3DFMT_R8G8B8A8_SNORM;
117 case D3DFMT_V16U16: return WINED3DFMT_R16G16_SNORM;
118 case D3DFMT_W11V11U10: return WINED3DFMT_R10G11B11_SNORM;
119 case D3DFMT_A2W10V10U10: return WINED3DFMT_R10G10B10_SNORM_A2_UNORM;
120 case D3DFMT_D16_LOCKABLE: return WINED3DFMT_D16_LOCKABLE;
121 case D3DFMT_D32: return WINED3DFMT_D32_UNORM;
122 case D3DFMT_D15S1: return WINED3DFMT_S1_UINT_D15_UNORM;
123 case D3DFMT_D24S8: return WINED3DFMT_D24_UNORM_S8_UINT;
124 case D3DFMT_D24X8: return WINED3DFMT_X8D24_UNORM;
125 case D3DFMT_D24X4S4: return WINED3DFMT_S4X4_UINT_D24_UNORM;
126 case D3DFMT_D16: return WINED3DFMT_D16_UNORM;
127 case D3DFMT_INDEX16: return WINED3DFMT_R16_UINT;
128 case D3DFMT_INDEX32: return WINED3DFMT_R32_UINT;
129 default:
130 FIXME("Unhandled D3DFORMAT %#x.\n", format);
131 return WINED3DFMT_UNKNOWN;
135 unsigned int wined3dmapflags_from_d3dmapflags(unsigned int flags, unsigned int usage)
137 static const unsigned int handled = D3DLOCK_NOSYSLOCK
138 | D3DLOCK_NOOVERWRITE
139 | D3DLOCK_DISCARD
140 | D3DLOCK_NO_DIRTY_UPDATE;
141 unsigned int wined3d_flags;
143 wined3d_flags = flags & handled;
144 if (~usage & D3DUSAGE_WRITEONLY && !(flags & (D3DLOCK_NOOVERWRITE | D3DLOCK_DISCARD)))
145 wined3d_flags |= WINED3D_MAP_READ;
146 if (!(flags & D3DLOCK_READONLY))
147 wined3d_flags |= WINED3D_MAP_WRITE;
148 if (!(wined3d_flags & (WINED3D_MAP_READ | WINED3D_MAP_WRITE)))
149 wined3d_flags |= WINED3D_MAP_WRITE;
150 flags &= ~(handled | D3DLOCK_READONLY);
152 if (flags)
153 FIXME("Unhandled flags %#x.\n", flags);
155 return wined3d_flags;
158 static UINT vertex_count_from_primitive_count(D3DPRIMITIVETYPE primitive_type, UINT primitive_count)
160 switch (primitive_type)
162 case D3DPT_POINTLIST:
163 return primitive_count;
165 case D3DPT_LINELIST:
166 return primitive_count * 2;
168 case D3DPT_LINESTRIP:
169 return primitive_count + 1;
171 case D3DPT_TRIANGLELIST:
172 return primitive_count * 3;
174 case D3DPT_TRIANGLESTRIP:
175 case D3DPT_TRIANGLEFAN:
176 return primitive_count + 2;
178 default:
179 FIXME("Unhandled primitive type %#x.\n", primitive_type);
180 return 0;
184 static D3DSWAPEFFECT d3dswapeffect_from_wined3dswapeffect(enum wined3d_swap_effect effect)
186 switch (effect)
188 case WINED3D_SWAP_EFFECT_DISCARD:
189 return D3DSWAPEFFECT_DISCARD;
190 case WINED3D_SWAP_EFFECT_SEQUENTIAL:
191 return D3DSWAPEFFECT_FLIP;
192 case WINED3D_SWAP_EFFECT_COPY:
193 return D3DSWAPEFFECT_COPY;
194 case WINED3D_SWAP_EFFECT_COPY_VSYNC:
195 return D3DSWAPEFFECT_COPY_VSYNC;
196 default:
197 FIXME("Unhandled swap effect %#x.\n", effect);
198 return D3DSWAPEFFECT_FLIP;
202 static void present_parameters_from_wined3d_swapchain_desc(D3DPRESENT_PARAMETERS *present_parameters,
203 const struct wined3d_swapchain_desc *swapchain_desc, DWORD presentation_interval)
205 present_parameters->BackBufferWidth = swapchain_desc->backbuffer_width;
206 present_parameters->BackBufferHeight = swapchain_desc->backbuffer_height;
207 present_parameters->BackBufferFormat = d3dformat_from_wined3dformat(swapchain_desc->backbuffer_format);
208 present_parameters->BackBufferCount = swapchain_desc->backbuffer_count;
209 present_parameters->MultiSampleType = d3dmultisample_type_from_wined3d(swapchain_desc->multisample_type);
210 present_parameters->SwapEffect = d3dswapeffect_from_wined3dswapeffect(swapchain_desc->swap_effect);
211 present_parameters->hDeviceWindow = swapchain_desc->device_window;
212 present_parameters->Windowed = swapchain_desc->windowed;
213 present_parameters->EnableAutoDepthStencil = swapchain_desc->enable_auto_depth_stencil;
214 present_parameters->AutoDepthStencilFormat
215 = d3dformat_from_wined3dformat(swapchain_desc->auto_depth_stencil_format);
216 present_parameters->Flags = swapchain_desc->flags & D3DPRESENTFLAGS_MASK;
217 present_parameters->FullScreen_RefreshRateInHz = swapchain_desc->refresh_rate;
218 present_parameters->FullScreen_PresentationInterval = presentation_interval;
221 static enum wined3d_swap_effect wined3dswapeffect_from_d3dswapeffect(D3DSWAPEFFECT effect)
223 switch (effect)
225 case D3DSWAPEFFECT_DISCARD:
226 return WINED3D_SWAP_EFFECT_DISCARD;
227 case D3DSWAPEFFECT_FLIP:
228 return WINED3D_SWAP_EFFECT_SEQUENTIAL;
229 case D3DSWAPEFFECT_COPY:
230 return WINED3D_SWAP_EFFECT_COPY;
231 case D3DSWAPEFFECT_COPY_VSYNC:
232 return WINED3D_SWAP_EFFECT_COPY_VSYNC;
233 default:
234 FIXME("Unhandled swap effect %#x.\n", effect);
235 return WINED3D_SWAP_EFFECT_SEQUENTIAL;
239 static enum wined3d_swap_interval wined3dswapinterval_from_d3d(DWORD interval)
241 switch (interval)
243 case D3DPRESENT_INTERVAL_IMMEDIATE:
244 return WINED3D_SWAP_INTERVAL_IMMEDIATE;
245 case D3DPRESENT_INTERVAL_ONE:
246 return WINED3D_SWAP_INTERVAL_ONE;
247 case D3DPRESENT_INTERVAL_TWO:
248 return WINED3D_SWAP_INTERVAL_TWO;
249 case D3DPRESENT_INTERVAL_THREE:
250 return WINED3D_SWAP_INTERVAL_THREE;
251 case D3DPRESENT_INTERVAL_FOUR:
252 return WINED3D_SWAP_INTERVAL_FOUR;
253 default:
254 FIXME("Unhandled presentation interval %#lx.\n", interval);
255 case D3DPRESENT_INTERVAL_DEFAULT:
256 return WINED3D_SWAP_INTERVAL_DEFAULT;
260 static enum wined3d_multisample_type wined3d_multisample_type_from_d3d(D3DMULTISAMPLE_TYPE type)
262 return (enum wined3d_multisample_type)type;
265 static BOOL wined3d_swapchain_desc_from_d3d8(struct wined3d_swapchain_desc *swapchain_desc,
266 struct wined3d_output *output, const D3DPRESENT_PARAMETERS *present_parameters)
268 if (!present_parameters->SwapEffect || present_parameters->SwapEffect > D3DSWAPEFFECT_COPY_VSYNC)
270 WARN("Invalid swap effect %u passed.\n", present_parameters->SwapEffect);
271 return FALSE;
273 if (present_parameters->BackBufferCount > 3
274 || ((present_parameters->SwapEffect == D3DSWAPEFFECT_COPY
275 || present_parameters->SwapEffect == D3DSWAPEFFECT_COPY_VSYNC)
276 && present_parameters->BackBufferCount > 1))
278 WARN("Invalid backbuffer count %u.\n", present_parameters->BackBufferCount);
279 return FALSE;
281 switch (present_parameters->FullScreen_PresentationInterval)
283 case D3DPRESENT_INTERVAL_DEFAULT:
284 case D3DPRESENT_INTERVAL_ONE:
285 case D3DPRESENT_INTERVAL_TWO:
286 case D3DPRESENT_INTERVAL_THREE:
287 case D3DPRESENT_INTERVAL_FOUR:
288 case D3DPRESENT_INTERVAL_IMMEDIATE:
289 break;
290 default:
291 WARN("Invalid presentation interval %#x.\n",
292 present_parameters->FullScreen_PresentationInterval);
293 return FALSE;
296 swapchain_desc->output = output;
297 swapchain_desc->backbuffer_width = present_parameters->BackBufferWidth;
298 swapchain_desc->backbuffer_height = present_parameters->BackBufferHeight;
299 swapchain_desc->backbuffer_format = wined3dformat_from_d3dformat(present_parameters->BackBufferFormat);
300 swapchain_desc->backbuffer_count = max(1, present_parameters->BackBufferCount);
301 swapchain_desc->backbuffer_bind_flags = WINED3D_BIND_RENDER_TARGET;
302 swapchain_desc->multisample_type = wined3d_multisample_type_from_d3d(present_parameters->MultiSampleType);
303 swapchain_desc->multisample_quality = 0; /* d3d9 only */
304 swapchain_desc->swap_effect = wined3dswapeffect_from_d3dswapeffect(present_parameters->SwapEffect);
305 swapchain_desc->device_window = present_parameters->hDeviceWindow;
306 swapchain_desc->windowed = present_parameters->Windowed;
307 swapchain_desc->enable_auto_depth_stencil = present_parameters->EnableAutoDepthStencil;
308 swapchain_desc->auto_depth_stencil_format
309 = wined3dformat_from_d3dformat(present_parameters->AutoDepthStencilFormat);
310 swapchain_desc->flags
311 = (present_parameters->Flags & D3DPRESENTFLAGS_MASK) | WINED3D_SWAPCHAIN_ALLOW_MODE_SWITCH;
312 swapchain_desc->refresh_rate = present_parameters->FullScreen_RefreshRateInHz;
313 swapchain_desc->auto_restore_display_mode = TRUE;
315 if (present_parameters->Flags & ~D3DPRESENTFLAGS_MASK)
316 FIXME("Unhandled flags %#lx.\n", present_parameters->Flags & ~D3DPRESENTFLAGS_MASK);
318 return TRUE;
321 void d3dcaps_from_wined3dcaps(D3DCAPS8 *caps, const struct wined3d_caps *wined3d_caps,
322 unsigned int adapter_ordinal)
324 caps->DeviceType = (D3DDEVTYPE)wined3d_caps->DeviceType;
325 caps->AdapterOrdinal = adapter_ordinal;
326 caps->Caps = wined3d_caps->Caps;
327 caps->Caps2 = wined3d_caps->Caps2;
328 caps->Caps3 = wined3d_caps->Caps3;
329 caps->PresentationIntervals = D3DPRESENT_INTERVAL_IMMEDIATE | D3DPRESENT_INTERVAL_ONE;
330 caps->CursorCaps = wined3d_caps->CursorCaps;
331 caps->DevCaps = wined3d_caps->DevCaps;
332 caps->PrimitiveMiscCaps = wined3d_caps->PrimitiveMiscCaps;
333 caps->RasterCaps = wined3d_caps->RasterCaps;
334 caps->ZCmpCaps = wined3d_caps->ZCmpCaps;
335 caps->SrcBlendCaps = wined3d_caps->SrcBlendCaps;
336 caps->DestBlendCaps = wined3d_caps->DestBlendCaps;
337 caps->AlphaCmpCaps = wined3d_caps->AlphaCmpCaps;
338 caps->ShadeCaps = wined3d_caps->ShadeCaps;
339 caps->TextureCaps = wined3d_caps->TextureCaps;
340 caps->TextureFilterCaps = wined3d_caps->TextureFilterCaps;
341 caps->CubeTextureFilterCaps = wined3d_caps->CubeTextureFilterCaps;
342 caps->VolumeTextureFilterCaps = wined3d_caps->VolumeTextureFilterCaps;
343 caps->TextureAddressCaps = wined3d_caps->TextureAddressCaps;
344 caps->VolumeTextureAddressCaps = wined3d_caps->VolumeTextureAddressCaps;
345 caps->LineCaps = wined3d_caps->LineCaps;
346 caps->MaxTextureWidth = wined3d_caps->MaxTextureWidth;
347 caps->MaxTextureHeight = wined3d_caps->MaxTextureHeight;
348 caps->MaxVolumeExtent = wined3d_caps->MaxVolumeExtent;
349 caps->MaxTextureRepeat = wined3d_caps->MaxTextureRepeat;
350 caps->MaxTextureAspectRatio = wined3d_caps->MaxTextureAspectRatio;
351 caps->MaxAnisotropy = wined3d_caps->MaxAnisotropy;
352 caps->MaxVertexW = wined3d_caps->MaxVertexW;
353 caps->GuardBandLeft = wined3d_caps->GuardBandLeft;
354 caps->GuardBandTop = wined3d_caps->GuardBandTop;
355 caps->GuardBandRight = wined3d_caps->GuardBandRight;
356 caps->GuardBandBottom = wined3d_caps->GuardBandBottom;
357 caps->ExtentsAdjust = wined3d_caps->ExtentsAdjust;
358 caps->StencilCaps = wined3d_caps->StencilCaps;
359 caps->FVFCaps = wined3d_caps->FVFCaps;
360 caps->TextureOpCaps = wined3d_caps->TextureOpCaps;
361 caps->MaxTextureBlendStages = wined3d_caps->MaxTextureBlendStages;
362 caps->MaxSimultaneousTextures = wined3d_caps->MaxSimultaneousTextures;
363 caps->VertexProcessingCaps = wined3d_caps->VertexProcessingCaps;
364 caps->MaxActiveLights = wined3d_caps->MaxActiveLights;
365 caps->MaxUserClipPlanes = wined3d_caps->MaxUserClipPlanes;
366 caps->MaxVertexBlendMatrices = wined3d_caps->MaxVertexBlendMatrices;
367 caps->MaxVertexBlendMatrixIndex = wined3d_caps->MaxVertexBlendMatrixIndex;
368 caps->MaxPointSize = wined3d_caps->MaxPointSize;
369 caps->MaxPrimitiveCount = wined3d_caps->MaxPrimitiveCount;
370 caps->MaxVertexIndex = wined3d_caps->MaxVertexIndex;
371 caps->MaxStreams = wined3d_caps->MaxStreams;
372 caps->MaxStreamStride = wined3d_caps->MaxStreamStride;
373 caps->VertexShaderVersion = wined3d_caps->VertexShaderVersion;
374 caps->MaxVertexShaderConst = wined3d_caps->MaxVertexShaderConst;
375 caps->PixelShaderVersion = wined3d_caps->PixelShaderVersion;
376 caps->MaxPixelShaderValue = wined3d_caps->PixelShader1xMaxValue;
378 caps->Caps2 &= D3DCAPS2_CANCALIBRATEGAMMA | D3DCAPS2_CANRENDERWINDOWED
379 | D3DCAPS2_CANMANAGERESOURCE | D3DCAPS2_DYNAMICTEXTURES | D3DCAPS2_FULLSCREENGAMMA
380 | D3DCAPS2_NO2DDURING3DSCENE | D3DCAPS2_RESERVED;
381 caps->Caps3 &= D3DCAPS3_ALPHA_FULLSCREEN_FLIP_OR_DISCARD | D3DCAPS3_RESERVED;
382 caps->PrimitiveMiscCaps &= D3DPMISCCAPS_MASKZ | D3DPMISCCAPS_LINEPATTERNREP
383 | D3DPMISCCAPS_CULLNONE | D3DPMISCCAPS_CULLCW | D3DPMISCCAPS_CULLCCW
384 | D3DPMISCCAPS_COLORWRITEENABLE | D3DPMISCCAPS_CLIPPLANESCALEDPOINTS
385 | D3DPMISCCAPS_CLIPTLVERTS | D3DPMISCCAPS_TSSARGTEMP | D3DPMISCCAPS_BLENDOP
386 | D3DPMISCCAPS_NULLREFERENCE;
387 caps->RasterCaps &= D3DPRASTERCAPS_DITHER | D3DPRASTERCAPS_PAT | D3DPRASTERCAPS_ZTEST
388 | D3DPRASTERCAPS_FOGVERTEX | D3DPRASTERCAPS_FOGTABLE | D3DPRASTERCAPS_ANTIALIASEDGES
389 | D3DPRASTERCAPS_MIPMAPLODBIAS | D3DPRASTERCAPS_ZBUFFERLESSHSR
390 | D3DPRASTERCAPS_FOGRANGE | D3DPRASTERCAPS_ANISOTROPY | D3DPRASTERCAPS_WBUFFER
391 | D3DPRASTERCAPS_WFOG | D3DPRASTERCAPS_ZFOG | D3DPRASTERCAPS_COLORPERSPECTIVE
392 | D3DPRASTERCAPS_STRETCHBLTMULTISAMPLE | WINED3DPRASTERCAPS_DEPTHBIAS;
393 if (caps->RasterCaps & WINED3DPRASTERCAPS_DEPTHBIAS)
394 caps->RasterCaps = (caps->RasterCaps | D3DPRASTERCAPS_ZBIAS) & ~WINED3DPRASTERCAPS_DEPTHBIAS;
395 caps->SrcBlendCaps &= D3DPBLENDCAPS_ZERO | D3DPBLENDCAPS_ONE | D3DPBLENDCAPS_SRCCOLOR
396 | D3DPBLENDCAPS_INVSRCCOLOR | D3DPBLENDCAPS_SRCALPHA | D3DPBLENDCAPS_INVSRCALPHA
397 | D3DPBLENDCAPS_DESTALPHA | D3DPBLENDCAPS_INVDESTALPHA | D3DPBLENDCAPS_DESTCOLOR
398 | D3DPBLENDCAPS_INVDESTCOLOR | D3DPBLENDCAPS_SRCALPHASAT | D3DPBLENDCAPS_BOTHSRCALPHA
399 | D3DPBLENDCAPS_BOTHINVSRCALPHA;
400 caps->DestBlendCaps &= D3DPBLENDCAPS_ZERO | D3DPBLENDCAPS_ONE | D3DPBLENDCAPS_SRCCOLOR
401 | D3DPBLENDCAPS_INVSRCCOLOR | D3DPBLENDCAPS_SRCALPHA | D3DPBLENDCAPS_INVSRCALPHA
402 | D3DPBLENDCAPS_DESTALPHA | D3DPBLENDCAPS_INVDESTALPHA | D3DPBLENDCAPS_DESTCOLOR
403 | D3DPBLENDCAPS_INVDESTCOLOR | D3DPBLENDCAPS_SRCALPHASAT | D3DPBLENDCAPS_BOTHSRCALPHA
404 | D3DPBLENDCAPS_BOTHINVSRCALPHA;
405 caps->TextureCaps &= D3DPTEXTURECAPS_PERSPECTIVE | D3DPTEXTURECAPS_POW2 | D3DPTEXTURECAPS_ALPHA
406 | D3DPTEXTURECAPS_SQUAREONLY | D3DPTEXTURECAPS_TEXREPEATNOTSCALEDBYSIZE
407 | D3DPTEXTURECAPS_ALPHAPALETTE | D3DPTEXTURECAPS_NONPOW2CONDITIONAL
408 | D3DPTEXTURECAPS_PROJECTED | D3DPTEXTURECAPS_CUBEMAP | D3DPTEXTURECAPS_VOLUMEMAP
409 | D3DPTEXTURECAPS_MIPMAP | D3DPTEXTURECAPS_MIPVOLUMEMAP | D3DPTEXTURECAPS_MIPCUBEMAP
410 | D3DPTEXTURECAPS_CUBEMAP_POW2 | D3DPTEXTURECAPS_VOLUMEMAP_POW2;
411 caps->TextureFilterCaps &= D3DPTFILTERCAPS_MINFPOINT | D3DPTFILTERCAPS_MINFLINEAR
412 | D3DPTFILTERCAPS_MINFANISOTROPIC | D3DPTFILTERCAPS_MIPFPOINT
413 | D3DPTFILTERCAPS_MIPFLINEAR | D3DPTFILTERCAPS_MAGFPOINT | D3DPTFILTERCAPS_MAGFLINEAR
414 | D3DPTFILTERCAPS_MAGFANISOTROPIC | D3DPTFILTERCAPS_MAGFAFLATCUBIC
415 | D3DPTFILTERCAPS_MAGFGAUSSIANCUBIC;
416 caps->CubeTextureFilterCaps &= D3DPTFILTERCAPS_MINFPOINT | D3DPTFILTERCAPS_MINFLINEAR
417 | D3DPTFILTERCAPS_MINFANISOTROPIC | D3DPTFILTERCAPS_MIPFPOINT
418 | D3DPTFILTERCAPS_MIPFLINEAR | D3DPTFILTERCAPS_MAGFPOINT | D3DPTFILTERCAPS_MAGFLINEAR
419 | D3DPTFILTERCAPS_MAGFANISOTROPIC | D3DPTFILTERCAPS_MAGFAFLATCUBIC
420 | D3DPTFILTERCAPS_MAGFGAUSSIANCUBIC;
421 caps->VolumeTextureFilterCaps &= D3DPTFILTERCAPS_MINFPOINT | D3DPTFILTERCAPS_MINFLINEAR
422 | D3DPTFILTERCAPS_MINFANISOTROPIC | D3DPTFILTERCAPS_MIPFPOINT
423 | D3DPTFILTERCAPS_MIPFLINEAR | D3DPTFILTERCAPS_MAGFPOINT | D3DPTFILTERCAPS_MAGFLINEAR
424 | D3DPTFILTERCAPS_MAGFANISOTROPIC | D3DPTFILTERCAPS_MAGFAFLATCUBIC
425 | D3DPTFILTERCAPS_MAGFGAUSSIANCUBIC;
426 if (caps->LineCaps & WINED3DLINECAPS_ANTIALIAS)
427 caps->RasterCaps |= D3DPRASTERCAPS_ANTIALIASEDGES;
428 caps->LineCaps &= ~WINED3DLINECAPS_ANTIALIAS;
429 caps->StencilCaps &= ~WINED3DSTENCILCAPS_TWOSIDED;
430 caps->VertexProcessingCaps &= D3DVTXPCAPS_TEXGEN | D3DVTXPCAPS_MATERIALSOURCE7
431 | D3DVTXPCAPS_DIRECTIONALLIGHTS | D3DVTXPCAPS_POSITIONALLIGHTS | D3DVTXPCAPS_LOCALVIEWER
432 | D3DVTXPCAPS_TWEENING | D3DVTXPCAPS_NO_VSDT_UBYTE4;
434 /* D3D8 doesn't support SM 2.0 or higher, so clamp to 1.x */
435 if (caps->PixelShaderVersion)
436 caps->PixelShaderVersion = D3DPS_VERSION(1, 4);
437 else
438 caps->PixelShaderVersion = D3DPS_VERSION(0, 0);
439 if (caps->VertexShaderVersion)
440 caps->VertexShaderVersion = D3DVS_VERSION(1, 1);
441 else
442 caps->VertexShaderVersion = D3DVS_VERSION(0, 0);
443 caps->MaxVertexShaderConst = min(D3D8_MAX_VERTEX_SHADER_CONSTANTF, caps->MaxVertexShaderConst);
446 static enum wined3d_transform_state wined3d_transform_state_from_d3d(D3DTRANSFORMSTATETYPE state)
448 return (enum wined3d_transform_state)state;
451 static enum wined3d_render_state wined3d_render_state_from_d3d(D3DRENDERSTATETYPE state)
453 switch (state)
455 case D3DRS_ZBIAS:
456 return WINED3D_RS_DEPTHBIAS;
457 case D3DRS_EDGEANTIALIAS:
458 return WINED3D_RS_ANTIALIASEDLINEENABLE;
459 default:
460 return (enum wined3d_render_state)state;
464 static enum wined3d_primitive_type wined3d_primitive_type_from_d3d(D3DPRIMITIVETYPE type)
466 return (enum wined3d_primitive_type)type;
469 /* Handle table functions */
470 static DWORD d3d8_allocate_handle(struct d3d8_handle_table *t, void *object, enum d3d8_handle_type type)
472 struct d3d8_handle_entry *entry;
474 if (t->free_entries)
476 DWORD index = t->free_entries - t->entries;
477 /* Use a free handle */
478 entry = t->free_entries;
479 if (entry->type != D3D8_HANDLE_FREE)
481 ERR("Handle %lu(%p) is in the free list, but has type %#x.\n", index, entry, entry->type);
482 return D3D8_INVALID_HANDLE;
484 t->free_entries = entry->object;
485 entry->object = object;
486 entry->type = type;
488 return index;
491 if (!(t->entry_count < t->table_size))
493 /* Grow the table */
494 UINT new_size = t->table_size + (t->table_size >> 1);
495 struct d3d8_handle_entry *new_entries;
497 if (!(new_entries = heap_realloc(t->entries, new_size * sizeof(*t->entries))))
499 ERR("Failed to grow the handle table.\n");
500 return D3D8_INVALID_HANDLE;
502 t->entries = new_entries;
503 t->table_size = new_size;
506 entry = &t->entries[t->entry_count];
507 entry->object = object;
508 entry->type = type;
510 return t->entry_count++;
513 static void *d3d8_free_handle(struct d3d8_handle_table *t, DWORD handle, enum d3d8_handle_type type)
515 struct d3d8_handle_entry *entry;
516 void *object;
518 if (handle == D3D8_INVALID_HANDLE || handle >= t->entry_count)
520 WARN("Invalid handle %lu passed.\n", handle);
521 return NULL;
524 entry = &t->entries[handle];
525 if (entry->type != type)
527 WARN("Handle %lu(%p) is not of type %#x.\n", handle, entry, type);
528 return NULL;
531 object = entry->object;
532 entry->object = t->free_entries;
533 entry->type = D3D8_HANDLE_FREE;
534 t->free_entries = entry;
536 return object;
539 static void *d3d8_get_object(struct d3d8_handle_table *t, DWORD handle, enum d3d8_handle_type type)
541 struct d3d8_handle_entry *entry;
543 if (handle == D3D8_INVALID_HANDLE || handle >= t->entry_count)
545 WARN("Invalid handle %lu passed.\n", handle);
546 return NULL;
549 entry = &t->entries[handle];
550 if (entry->type != type)
552 WARN("Handle %lu(%p) is not of type %#x.\n", handle, entry, type);
553 return NULL;
556 return entry->object;
559 static void device_reset_viewport_state(struct d3d8_device *device)
561 struct wined3d_viewport vp;
562 RECT rect;
564 wined3d_device_context_get_viewports(device->immediate_context, NULL, &vp);
565 wined3d_stateblock_set_viewport(device->state, &vp);
566 wined3d_device_context_get_scissor_rects(device->immediate_context, NULL, &rect);
567 wined3d_stateblock_set_scissor_rect(device->state, &rect);
570 static HRESULT WINAPI d3d8_device_QueryInterface(IDirect3DDevice8 *iface, REFIID riid, void **out)
572 TRACE("iface %p, riid %s, out %p.\n",
573 iface, debugstr_guid(riid), out);
575 if (IsEqualGUID(riid, &IID_IDirect3DDevice8)
576 || IsEqualGUID(riid, &IID_IUnknown))
578 IDirect3DDevice8_AddRef(iface);
579 *out = iface;
580 return S_OK;
583 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
585 *out = NULL;
586 return E_NOINTERFACE;
589 static ULONG WINAPI d3d8_device_AddRef(IDirect3DDevice8 *iface)
591 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
592 ULONG ref = InterlockedIncrement(&device->ref);
594 TRACE("%p increasing refcount to %lu.\n", iface, ref);
596 return ref;
599 static ULONG WINAPI d3d8_device_Release(IDirect3DDevice8 *iface)
601 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
602 ULONG ref;
604 if (device->in_destruction)
605 return 0;
607 ref = InterlockedDecrement(&device->ref);
609 TRACE("%p decreasing refcount to %lu.\n", iface, ref);
611 if (!ref)
613 IDirect3D8 *parent = &device->d3d_parent->IDirect3D8_iface;
614 unsigned i;
616 TRACE("Releasing wined3d device %p.\n", device->wined3d_device);
618 wined3d_mutex_lock();
620 device->in_destruction = TRUE;
622 for (i = 0; i < device->numConvertedDecls; ++i)
624 d3d8_vertex_declaration_destroy(device->decls[i].declaration);
626 heap_free(device->decls);
628 wined3d_streaming_buffer_cleanup(&device->vertex_buffer);
629 wined3d_streaming_buffer_cleanup(&device->index_buffer);
631 if (device->recording)
632 wined3d_stateblock_decref(device->recording);
633 wined3d_stateblock_decref(device->state);
635 wined3d_swapchain_decref(device->implicit_swapchain);
636 wined3d_device_release_focus_window(device->wined3d_device);
637 wined3d_device_decref(device->wined3d_device);
638 heap_free(device->handle_table.entries);
639 heap_free(device);
641 wined3d_mutex_unlock();
643 IDirect3D8_Release(parent);
645 return ref;
648 static HRESULT WINAPI d3d8_device_TestCooperativeLevel(IDirect3DDevice8 *iface)
650 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
652 TRACE("iface %p.\n", iface);
654 TRACE("device state: %#lx.\n", device->device_state);
656 switch (device->device_state)
658 default:
659 case D3D8_DEVICE_STATE_OK:
660 return D3D_OK;
661 case D3D8_DEVICE_STATE_LOST:
662 return D3DERR_DEVICELOST;
663 case D3D8_DEVICE_STATE_NOT_RESET:
664 return D3DERR_DEVICENOTRESET;
668 static UINT WINAPI d3d8_device_GetAvailableTextureMem(IDirect3DDevice8 *iface)
670 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
671 UINT ret;
673 TRACE("iface %p.\n", iface);
675 wined3d_mutex_lock();
676 ret = wined3d_device_get_available_texture_mem(device->wined3d_device);
677 wined3d_mutex_unlock();
679 return ret;
682 static HRESULT WINAPI d3d8_device_ResourceManagerDiscardBytes(IDirect3DDevice8 *iface, DWORD byte_count)
684 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
686 TRACE("iface %p, byte_count %lu.\n", iface, byte_count);
688 if (byte_count)
689 FIXME("Byte count ignored.\n");
691 wined3d_mutex_lock();
692 wined3d_device_evict_managed_resources(device->wined3d_device);
693 wined3d_mutex_unlock();
695 return D3D_OK;
698 static HRESULT WINAPI d3d8_device_GetDirect3D(IDirect3DDevice8 *iface, IDirect3D8 **d3d8)
700 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
702 TRACE("iface %p, d3d8 %p.\n", iface, d3d8);
704 if (!d3d8)
705 return D3DERR_INVALIDCALL;
707 return IDirect3D8_QueryInterface(&device->d3d_parent->IDirect3D8_iface, &IID_IDirect3D8,
708 (void **)d3d8);
711 static HRESULT WINAPI d3d8_device_GetDeviceCaps(IDirect3DDevice8 *iface, D3DCAPS8 *caps)
713 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
714 struct wined3d_caps wined3d_caps;
715 HRESULT hr;
717 TRACE("iface %p, caps %p.\n", iface, caps);
719 if (!caps)
720 return D3DERR_INVALIDCALL;
722 wined3d_mutex_lock();
723 hr = wined3d_device_get_device_caps(device->wined3d_device, &wined3d_caps);
724 wined3d_mutex_unlock();
726 d3dcaps_from_wined3dcaps(caps, &wined3d_caps, device->adapter_ordinal);
728 return hr;
731 static HRESULT WINAPI d3d8_device_GetDisplayMode(IDirect3DDevice8 *iface, D3DDISPLAYMODE *mode)
733 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
734 struct wined3d_display_mode wined3d_mode;
735 HRESULT hr;
737 TRACE("iface %p, mode %p.\n", iface, mode);
739 wined3d_mutex_lock();
740 hr = wined3d_device_get_display_mode(device->wined3d_device, 0, &wined3d_mode, NULL);
741 wined3d_mutex_unlock();
743 if (SUCCEEDED(hr))
745 mode->Width = wined3d_mode.width;
746 mode->Height = wined3d_mode.height;
747 mode->RefreshRate = wined3d_mode.refresh_rate;
748 mode->Format = d3dformat_from_wined3dformat(wined3d_mode.format_id);
751 return hr;
754 static HRESULT WINAPI d3d8_device_GetCreationParameters(IDirect3DDevice8 *iface,
755 D3DDEVICE_CREATION_PARAMETERS *parameters)
757 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
759 TRACE("iface %p, parameters %p.\n", iface, parameters);
761 wined3d_mutex_lock();
762 wined3d_device_get_creation_parameters(device->wined3d_device,
763 (struct wined3d_device_creation_parameters *)parameters);
764 wined3d_mutex_unlock();
766 parameters->AdapterOrdinal = device->adapter_ordinal;
767 return D3D_OK;
770 static HRESULT WINAPI d3d8_device_SetCursorProperties(IDirect3DDevice8 *iface,
771 UINT hotspot_x, UINT hotspot_y, IDirect3DSurface8 *bitmap)
773 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
774 struct d3d8_surface *bitmap_impl = unsafe_impl_from_IDirect3DSurface8(bitmap);
775 D3DSURFACE_DESC surface_desc;
776 D3DDISPLAYMODE mode;
777 HRESULT hr;
779 TRACE("iface %p, hotspot_x %u, hotspot_y %u, bitmap %p.\n",
780 iface, hotspot_x, hotspot_y, bitmap);
782 if (!bitmap)
784 WARN("No cursor bitmap, returning D3DERR_INVALIDCALL.\n");
785 return D3DERR_INVALIDCALL;
788 if (FAILED(hr = IDirect3DSurface8_GetDesc(bitmap, &surface_desc)))
790 WARN("Failed to get surface description, hr %#lx.\n", hr);
791 return hr;
794 if (FAILED(hr = IDirect3D8_GetAdapterDisplayMode(&device->d3d_parent->IDirect3D8_iface,
795 device->adapter_ordinal, &mode)))
797 WARN("Failed to get device display mode, hr %#lx.\n", hr);
798 return hr;
801 if (surface_desc.Width > mode.Width || surface_desc.Height > mode.Height)
803 WARN("Surface dimension %ux%u exceeds display mode %ux%u.\n", surface_desc.Width,
804 surface_desc.Height, mode.Width, mode.Height);
805 return D3DERR_INVALIDCALL;
808 wined3d_mutex_lock();
809 hr = wined3d_device_set_cursor_properties(device->wined3d_device,
810 hotspot_x, hotspot_y, bitmap_impl->wined3d_texture, bitmap_impl->sub_resource_idx);
811 wined3d_mutex_unlock();
813 return hr;
816 static void WINAPI d3d8_device_SetCursorPosition(IDirect3DDevice8 *iface, UINT x, UINT y, DWORD flags)
818 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
820 TRACE("iface %p, x %u, y %u, flags %#lx.\n", iface, x, y, flags);
822 wined3d_mutex_lock();
823 wined3d_device_set_cursor_position(device->wined3d_device, x, y, flags);
824 wined3d_mutex_unlock();
827 static BOOL WINAPI d3d8_device_ShowCursor(IDirect3DDevice8 *iface, BOOL show)
829 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
830 BOOL ret;
832 TRACE("iface %p, show %#x.\n", iface, show);
834 wined3d_mutex_lock();
835 ret = wined3d_device_show_cursor(device->wined3d_device, show);
836 wined3d_mutex_unlock();
838 return ret;
841 static HRESULT WINAPI d3d8_device_CreateAdditionalSwapChain(IDirect3DDevice8 *iface,
842 D3DPRESENT_PARAMETERS *present_parameters, IDirect3DSwapChain8 **swapchain)
844 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
845 struct wined3d_swapchain_desc desc;
846 struct d3d8_swapchain *object;
847 unsigned int swap_interval;
848 unsigned int output_idx;
849 unsigned int i, count;
850 HRESULT hr;
852 TRACE("iface %p, present_parameters %p, swapchain %p.\n",
853 iface, present_parameters, swapchain);
855 if (!present_parameters->Windowed)
857 WARN("Trying to create an additional fullscreen swapchain, returning D3DERR_INVALIDCALL.\n");
858 return D3DERR_INVALIDCALL;
861 wined3d_mutex_lock();
862 count = wined3d_device_get_swapchain_count(device->wined3d_device);
863 for (i = 0; i < count; ++i)
865 struct wined3d_swapchain *wined3d_swapchain;
867 wined3d_swapchain = wined3d_device_get_swapchain(device->wined3d_device, i);
868 wined3d_swapchain_get_desc(wined3d_swapchain, &desc);
870 if (!desc.windowed)
872 wined3d_mutex_unlock();
873 WARN("Trying to create an additional swapchain in fullscreen mode, returning D3DERR_INVALIDCALL.\n");
874 return D3DERR_INVALIDCALL;
877 wined3d_mutex_unlock();
879 output_idx = device->adapter_ordinal;
880 if (!wined3d_swapchain_desc_from_d3d8(&desc, device->d3d_parent->wined3d_outputs[output_idx],
881 present_parameters))
882 return D3DERR_INVALIDCALL;
883 swap_interval = wined3dswapinterval_from_d3d(present_parameters->FullScreen_PresentationInterval);
884 if (SUCCEEDED(hr = d3d8_swapchain_create(device, &desc, swap_interval, &object)))
885 *swapchain = &object->IDirect3DSwapChain8_iface;
886 present_parameters_from_wined3d_swapchain_desc(present_parameters,
887 &desc, present_parameters->FullScreen_PresentationInterval);
889 return hr;
892 static HRESULT CDECL reset_enum_callback(struct wined3d_resource *resource)
894 struct d3d8_vertexbuffer *vertex_buffer;
895 struct d3d8_indexbuffer *index_buffer;
896 struct wined3d_resource_desc desc;
897 IDirect3DBaseTexture8 *texture;
898 struct d3d8_surface *surface;
899 IUnknown *parent;
901 wined3d_resource_get_desc(resource, &desc);
902 if ((desc.access & WINED3D_RESOURCE_ACCESS_CPU) || (desc.usage & WINED3DUSAGE_MANAGED))
903 return D3D_OK;
905 if (desc.resource_type != WINED3D_RTYPE_TEXTURE_2D)
907 if (desc.bind_flags & WINED3D_BIND_VERTEX_BUFFER)
909 vertex_buffer = wined3d_resource_get_parent(resource);
910 if (vertex_buffer && vertex_buffer->draw_buffer)
911 return D3D_OK;
913 if (desc.bind_flags & WINED3D_BIND_INDEX_BUFFER)
915 index_buffer = wined3d_resource_get_parent(resource);
916 if (index_buffer && index_buffer->sysmem)
917 return D3D_OK;
920 WARN("Resource %p in pool D3DPOOL_DEFAULT blocks the Reset call.\n", resource);
921 return D3DERR_DEVICELOST;
924 parent = wined3d_resource_get_parent(resource);
925 if (parent && SUCCEEDED(IUnknown_QueryInterface(parent, &IID_IDirect3DBaseTexture8, (void **)&texture)))
927 IDirect3DBaseTexture8_Release(texture);
928 WARN("Texture %p (resource %p) in pool D3DPOOL_DEFAULT blocks the Reset call.\n", texture, resource);
929 return D3DERR_DEVICELOST;
932 surface = wined3d_texture_get_sub_resource_parent(wined3d_texture_from_resource(resource), 0);
933 if (!surface || !surface->resource.refcount)
934 return D3D_OK;
936 WARN("Surface %p in pool D3DPOOL_DEFAULT blocks the Reset call.\n", surface);
937 return D3DERR_DEVICELOST;
940 static HRESULT WINAPI d3d8_device_Reset(IDirect3DDevice8 *iface,
941 D3DPRESENT_PARAMETERS *present_parameters)
943 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
944 struct wined3d_swapchain_desc swapchain_desc;
945 struct d3d8_swapchain *implicit_swapchain;
946 unsigned int output_idx;
947 HRESULT hr;
949 TRACE("iface %p, present_parameters %p.\n", iface, present_parameters);
951 if (device->device_state == D3D8_DEVICE_STATE_LOST)
953 WARN("App not active, returning D3DERR_DEVICELOST.\n");
954 return D3DERR_DEVICELOST;
956 output_idx = device->adapter_ordinal;
957 if (!wined3d_swapchain_desc_from_d3d8(&swapchain_desc,
958 device->d3d_parent->wined3d_outputs[output_idx], present_parameters))
959 return D3DERR_INVALIDCALL;
960 swapchain_desc.flags |= WINED3D_SWAPCHAIN_IMPLICIT;
962 wined3d_mutex_lock();
964 wined3d_streaming_buffer_cleanup(&device->vertex_buffer);
965 wined3d_streaming_buffer_cleanup(&device->index_buffer);
967 if (device->recording)
968 wined3d_stateblock_decref(device->recording);
969 device->recording = NULL;
970 device->update_state = device->state;
971 wined3d_stateblock_reset(device->state);
973 if (SUCCEEDED(hr = wined3d_device_reset(device->wined3d_device, &swapchain_desc,
974 NULL, reset_enum_callback, TRUE)))
976 struct wined3d_rendertarget_view *rtv;
978 present_parameters->BackBufferCount = swapchain_desc.backbuffer_count;
979 implicit_swapchain = wined3d_swapchain_get_parent(device->implicit_swapchain);
980 implicit_swapchain->swap_interval
981 = wined3dswapinterval_from_d3d(present_parameters->FullScreen_PresentationInterval);
982 wined3d_stateblock_set_render_state(device->state, WINED3D_RS_POINTSIZE_MIN, 0);
983 wined3d_stateblock_set_render_state(device->state, WINED3D_RS_ZENABLE,
984 !!swapchain_desc.enable_auto_depth_stencil);
985 device_reset_viewport_state(device);
987 if ((rtv = wined3d_device_context_get_depth_stencil_view(device->immediate_context)))
989 struct wined3d_resource *resource = wined3d_rendertarget_view_get_resource(rtv);
990 struct d3d8_surface *surface;
992 if ((surface = d3d8_surface_create(wined3d_texture_from_resource(resource), 0,
993 (IUnknown *)&device->IDirect3DDevice8_iface)))
994 surface->parent_device = &device->IDirect3DDevice8_iface;
997 device->device_state = D3D8_DEVICE_STATE_OK;
999 else
1001 device->device_state = D3D8_DEVICE_STATE_NOT_RESET;
1003 wined3d_mutex_unlock();
1005 return hr;
1008 static HRESULT WINAPI d3d8_device_Present(IDirect3DDevice8 *iface, const RECT *src_rect,
1009 const RECT *dst_rect, HWND dst_window_override, const RGNDATA *dirty_region)
1011 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1012 struct d3d8_swapchain *implicit_swapchain;
1014 TRACE("iface %p, src_rect %s, dst_rect %s, dst_window_override %p, dirty_region %p.\n",
1015 iface, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect), dst_window_override, dirty_region);
1017 /* Fraps does not hook IDirect3DDevice8::Present regardless of the hotpatch
1018 * attribute. It only hooks IDirect3DSwapChain8::Present. Yet it properly
1019 * shows a framerate on Windows in applications that only call the device
1020 * method, like e.g. the dx8 sdk samples. The conclusion is that native
1021 * calls the swapchain's public method from the device. */
1022 implicit_swapchain = wined3d_swapchain_get_parent(device->implicit_swapchain);
1023 return IDirect3DSwapChain8_Present(&implicit_swapchain->IDirect3DSwapChain8_iface,
1024 src_rect, dst_rect, dst_window_override, dirty_region);
1027 static HRESULT WINAPI d3d8_device_GetBackBuffer(IDirect3DDevice8 *iface,
1028 UINT backbuffer_idx, D3DBACKBUFFER_TYPE backbuffer_type, IDirect3DSurface8 **backbuffer)
1030 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1031 struct wined3d_texture *wined3d_texture;
1032 struct d3d8_surface *surface_impl;
1034 TRACE("iface %p, backbuffer_idx %u, backbuffer_type %#x, backbuffer %p.\n",
1035 iface, backbuffer_idx, backbuffer_type, backbuffer);
1037 /* backbuffer_type is ignored by native. */
1039 /* No need to check for backbuffer == NULL, Windows crashes in that case. */
1040 wined3d_mutex_lock();
1042 if (!(wined3d_texture = wined3d_swapchain_get_back_buffer(device->implicit_swapchain, backbuffer_idx)))
1044 wined3d_mutex_unlock();
1045 *backbuffer = NULL;
1046 return D3DERR_INVALIDCALL;
1049 surface_impl = wined3d_texture_get_sub_resource_parent(wined3d_texture, 0);
1050 *backbuffer = &surface_impl->IDirect3DSurface8_iface;
1051 IDirect3DSurface8_AddRef(*backbuffer);
1053 wined3d_mutex_unlock();
1054 return D3D_OK;
1057 static HRESULT WINAPI d3d8_device_GetRasterStatus(IDirect3DDevice8 *iface, D3DRASTER_STATUS *raster_status)
1059 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1060 HRESULT hr;
1062 TRACE("iface %p, raster_status %p.\n", iface, raster_status);
1064 wined3d_mutex_lock();
1065 hr = wined3d_device_get_raster_status(device->wined3d_device, 0, (struct wined3d_raster_status *)raster_status);
1066 wined3d_mutex_unlock();
1068 return hr;
1071 static void WINAPI d3d8_device_SetGammaRamp(IDirect3DDevice8 *iface, DWORD flags, const D3DGAMMARAMP *ramp)
1073 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1075 TRACE("iface %p, flags %#lx, ramp %p.\n", iface, flags, ramp);
1077 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
1078 wined3d_mutex_lock();
1079 wined3d_device_set_gamma_ramp(device->wined3d_device, 0, flags, (const struct wined3d_gamma_ramp *)ramp);
1080 wined3d_mutex_unlock();
1083 static void WINAPI d3d8_device_GetGammaRamp(IDirect3DDevice8 *iface, D3DGAMMARAMP *ramp)
1085 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1087 TRACE("iface %p, ramp %p.\n", iface, ramp);
1089 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
1090 wined3d_mutex_lock();
1091 wined3d_device_get_gamma_ramp(device->wined3d_device, 0, (struct wined3d_gamma_ramp *)ramp);
1092 wined3d_mutex_unlock();
1095 static HRESULT WINAPI d3d8_device_CreateTexture(IDirect3DDevice8 *iface,
1096 UINT width, UINT height, UINT levels, DWORD usage, D3DFORMAT format,
1097 D3DPOOL pool, IDirect3DTexture8 **texture)
1099 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1100 struct d3d8_texture *object;
1101 unsigned int i;
1102 HRESULT hr;
1104 TRACE("iface %p, width %u, height %u, levels %u, usage %#lx, format %#x, pool %#x, texture %p.\n",
1105 iface, width, height, levels, usage, format, pool, texture);
1107 if (!format)
1108 return D3DERR_INVALIDCALL;
1110 *texture = NULL;
1111 if (!(object = heap_alloc_zero(sizeof(*object))))
1112 return D3DERR_OUTOFVIDEOMEMORY;
1114 hr = d3d8_texture_2d_init(object, device, width, height, levels, usage, format, pool);
1115 if (FAILED(hr))
1117 WARN("Failed to initialize texture, hr %#lx.\n", hr);
1118 heap_free(object);
1119 return hr;
1122 levels = wined3d_texture_get_level_count(object->wined3d_texture);
1123 for (i = 0; i < levels; ++i)
1125 if (!d3d8_surface_create(object->wined3d_texture, i, (IUnknown *)&object->IDirect3DBaseTexture8_iface))
1127 IDirect3DTexture8_Release(&object->IDirect3DBaseTexture8_iface);
1128 return E_OUTOFMEMORY;
1132 TRACE("Created texture %p.\n", object);
1133 *texture = (IDirect3DTexture8 *)&object->IDirect3DBaseTexture8_iface;
1135 return D3D_OK;
1138 static HRESULT WINAPI d3d8_device_CreateVolumeTexture(IDirect3DDevice8 *iface,
1139 UINT width, UINT height, UINT depth, UINT levels, DWORD usage, D3DFORMAT format,
1140 D3DPOOL pool, IDirect3DVolumeTexture8 **texture)
1142 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1143 struct d3d8_texture *object;
1144 HRESULT hr;
1146 TRACE("iface %p, width %u, height %u, depth %u, levels %u, usage %#lx, format %#x, pool %#x, texture %p.\n",
1147 iface, width, height, depth, levels, usage, format, pool, texture);
1149 if (!format)
1150 return D3DERR_INVALIDCALL;
1152 *texture = NULL;
1153 if (!(object = heap_alloc_zero(sizeof(*object))))
1154 return D3DERR_OUTOFVIDEOMEMORY;
1156 hr = d3d8_texture_3d_init(object, device, width, height, depth, levels, usage, format, pool);
1157 if (FAILED(hr))
1159 WARN("Failed to initialize volume texture, hr %#lx.\n", hr);
1160 heap_free(object);
1161 return hr;
1164 TRACE("Created volume texture %p.\n", object);
1165 *texture = (IDirect3DVolumeTexture8 *)&object->IDirect3DBaseTexture8_iface;
1167 return D3D_OK;
1170 static HRESULT WINAPI d3d8_device_CreateCubeTexture(IDirect3DDevice8 *iface, UINT edge_length,
1171 UINT levels, DWORD usage, D3DFORMAT format, D3DPOOL pool, IDirect3DCubeTexture8 **texture)
1173 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1174 struct d3d8_texture *object;
1175 unsigned int i;
1176 HRESULT hr;
1178 TRACE("iface %p, edge_length %u, levels %u, usage %#lx, format %#x, pool %#x, texture %p.\n",
1179 iface, edge_length, levels, usage, format, pool, texture);
1181 if (!format)
1182 return D3DERR_INVALIDCALL;
1184 *texture = NULL;
1185 if (!(object = heap_alloc_zero(sizeof(*object))))
1186 return D3DERR_OUTOFVIDEOMEMORY;
1188 hr = d3d8_texture_cube_init(object, device, edge_length, levels, usage, format, pool);
1189 if (FAILED(hr))
1191 WARN("Failed to initialize cube texture, hr %#lx.\n", hr);
1192 heap_free(object);
1193 return hr;
1196 levels = wined3d_texture_get_level_count(object->wined3d_texture);
1197 for (i = 0; i < levels * 6; ++i)
1199 if (!d3d8_surface_create(object->wined3d_texture, i, (IUnknown *)&object->IDirect3DBaseTexture8_iface))
1201 IDirect3DTexture8_Release(&object->IDirect3DBaseTexture8_iface);
1202 return E_OUTOFMEMORY;
1206 TRACE("Created cube texture %p.\n", object);
1207 *texture = (IDirect3DCubeTexture8 *)&object->IDirect3DBaseTexture8_iface;
1209 return hr;
1212 static HRESULT WINAPI d3d8_device_CreateVertexBuffer(IDirect3DDevice8 *iface, UINT size,
1213 DWORD usage, DWORD fvf, D3DPOOL pool, IDirect3DVertexBuffer8 **buffer)
1215 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1216 struct d3d8_vertexbuffer *object;
1217 HRESULT hr;
1219 TRACE("iface %p, size %u, usage %#lx, fvf %#lx, pool %#x, buffer %p.\n",
1220 iface, size, usage, fvf, pool, buffer);
1222 if (!(object = heap_alloc_zero(sizeof(*object))))
1223 return D3DERR_OUTOFVIDEOMEMORY;
1225 hr = vertexbuffer_init(object, device, size, usage, fvf, pool);
1226 if (FAILED(hr))
1228 WARN("Failed to initialize vertex buffer, hr %#lx.\n", hr);
1229 heap_free(object);
1230 return hr;
1233 TRACE("Created vertex buffer %p.\n", object);
1234 *buffer = &object->IDirect3DVertexBuffer8_iface;
1236 return D3D_OK;
1239 static HRESULT WINAPI d3d8_device_CreateIndexBuffer(IDirect3DDevice8 *iface, UINT size,
1240 DWORD usage, D3DFORMAT format, D3DPOOL pool, IDirect3DIndexBuffer8 **buffer)
1242 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1243 struct d3d8_indexbuffer *object;
1244 HRESULT hr;
1246 TRACE("iface %p, size %u, usage %#lx, format %#x, pool %#x, buffer %p.\n",
1247 iface, size, usage, format, pool, buffer);
1249 if (!(object = heap_alloc_zero(sizeof(*object))))
1250 return D3DERR_OUTOFVIDEOMEMORY;
1252 hr = indexbuffer_init(object, device, size, usage, format, pool);
1253 if (FAILED(hr))
1255 WARN("Failed to initialize index buffer, hr %#lx.\n", hr);
1256 heap_free(object);
1257 return hr;
1260 TRACE("Created index buffer %p.\n", object);
1261 *buffer = &object->IDirect3DIndexBuffer8_iface;
1263 return D3D_OK;
1266 static HRESULT d3d8_device_create_surface(struct d3d8_device *device, enum wined3d_format_id format,
1267 enum wined3d_multisample_type multisample_type, unsigned int bind_flags, unsigned int access,
1268 unsigned int width, unsigned int height, IDirect3DSurface8 **surface)
1270 struct wined3d_resource_desc desc;
1271 struct d3d8_surface *surface_impl;
1272 struct wined3d_texture *texture;
1273 HRESULT hr;
1275 TRACE("device %p, format %#x, multisample_type %#x, bind_flags %#x, "
1276 "access %#x, width %u, height %u, surface %p.\n",
1277 device, format, multisample_type, bind_flags, access, width, height, surface);
1279 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
1280 desc.format = format;
1281 desc.multisample_type = multisample_type;
1282 desc.multisample_quality = 0;
1283 desc.usage = 0;
1284 desc.bind_flags = bind_flags;
1285 desc.access = access;
1286 desc.width = width;
1287 desc.height = height;
1288 desc.depth = 1;
1289 desc.size = 0;
1291 wined3d_mutex_lock();
1293 if (FAILED(hr = wined3d_texture_create(device->wined3d_device, &desc,
1294 1, 1, 0, NULL, NULL, &d3d8_null_wined3d_parent_ops, &texture)))
1296 wined3d_mutex_unlock();
1297 WARN("Failed to create texture, hr %#lx.\n", hr);
1298 return hr;
1301 if (!(surface_impl = d3d8_surface_create(texture, 0, NULL)))
1303 wined3d_texture_decref(texture);
1304 wined3d_mutex_unlock();
1305 return E_OUTOFMEMORY;
1307 surface_impl->parent_device = &device->IDirect3DDevice8_iface;
1308 *surface = &surface_impl->IDirect3DSurface8_iface;
1309 IDirect3DSurface8_AddRef(*surface);
1310 wined3d_texture_decref(texture);
1312 wined3d_mutex_unlock();
1314 return D3D_OK;
1317 static HRESULT WINAPI d3d8_device_CreateRenderTarget(IDirect3DDevice8 *iface, UINT width,
1318 UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, BOOL lockable,
1319 IDirect3DSurface8 **surface)
1321 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1322 unsigned int access = WINED3D_RESOURCE_ACCESS_GPU;
1324 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, lockable %#x, surface %p.\n",
1325 iface, width, height, format, multisample_type, lockable, surface);
1327 if (!format)
1328 return D3DERR_INVALIDCALL;
1330 *surface = NULL;
1331 if (lockable)
1332 access |= WINED3D_RESOURCE_ACCESS_MAP_R | WINED3D_RESOURCE_ACCESS_MAP_W;
1334 return d3d8_device_create_surface(device, wined3dformat_from_d3dformat(format),
1335 wined3d_multisample_type_from_d3d(multisample_type),
1336 WINED3D_BIND_RENDER_TARGET, access, width, height, surface);
1339 static HRESULT WINAPI d3d8_device_CreateDepthStencilSurface(IDirect3DDevice8 *iface,
1340 UINT width, UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type,
1341 IDirect3DSurface8 **surface)
1343 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1345 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, surface %p.\n",
1346 iface, width, height, format, multisample_type, surface);
1348 if (!format)
1349 return D3DERR_INVALIDCALL;
1351 *surface = NULL;
1353 return d3d8_device_create_surface(device, wined3dformat_from_d3dformat(format),
1354 wined3d_multisample_type_from_d3d(multisample_type), WINED3D_BIND_DEPTH_STENCIL,
1355 WINED3D_RESOURCE_ACCESS_GPU, width, height, surface);
1358 /* IDirect3DDevice8Impl::CreateImageSurface returns surface with pool type SYSTEMMEM */
1359 static HRESULT WINAPI d3d8_device_CreateImageSurface(IDirect3DDevice8 *iface, UINT width,
1360 UINT height, D3DFORMAT format, IDirect3DSurface8 **surface)
1362 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1363 unsigned int access = WINED3D_RESOURCE_ACCESS_CPU
1364 | WINED3D_RESOURCE_ACCESS_MAP_R | WINED3D_RESOURCE_ACCESS_MAP_W;
1366 TRACE("iface %p, width %u, height %u, format %#x, surface %p.\n",
1367 iface, width, height, format, surface);
1369 *surface = NULL;
1371 return d3d8_device_create_surface(device, wined3dformat_from_d3dformat(format),
1372 WINED3D_MULTISAMPLE_NONE, 0, access, width, height, surface);
1375 static HRESULT WINAPI d3d8_device_CopyRects(IDirect3DDevice8 *iface,
1376 IDirect3DSurface8 *src_surface, const RECT *src_rects, UINT rect_count,
1377 IDirect3DSurface8 *dst_surface, const POINT *dst_points)
1379 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1380 struct d3d8_surface *src = unsafe_impl_from_IDirect3DSurface8(src_surface);
1381 struct d3d8_surface *dst = unsafe_impl_from_IDirect3DSurface8(dst_surface);
1382 enum wined3d_format_id src_format, dst_format;
1383 struct wined3d_sub_resource_desc wined3d_desc;
1384 UINT src_w, src_h;
1386 TRACE("iface %p, src_surface %p, src_rects %p, rect_count %u, dst_surface %p, dst_points %p.\n",
1387 iface, src_surface, src_rects, rect_count, dst_surface, dst_points);
1389 wined3d_mutex_lock();
1390 wined3d_texture_get_sub_resource_desc(src->wined3d_texture, src->sub_resource_idx, &wined3d_desc);
1391 if (wined3d_desc.bind_flags & WINED3D_BIND_DEPTH_STENCIL)
1393 WARN("Source %p is a depth stencil surface, returning D3DERR_INVALIDCALL.\n", src_surface);
1394 wined3d_mutex_unlock();
1395 return D3DERR_INVALIDCALL;
1397 src_format = wined3d_desc.format;
1398 src_w = wined3d_desc.width;
1399 src_h = wined3d_desc.height;
1401 wined3d_texture_get_sub_resource_desc(dst->wined3d_texture, dst->sub_resource_idx, &wined3d_desc);
1402 if (wined3d_desc.bind_flags & WINED3D_BIND_DEPTH_STENCIL)
1404 WARN("Destination %p is a depth stencil surface, returning D3DERR_INVALIDCALL.\n", dst_surface);
1405 wined3d_mutex_unlock();
1406 return D3DERR_INVALIDCALL;
1408 dst_format = wined3d_desc.format;
1410 /* Check that the source and destination formats match */
1411 if (src_format != dst_format)
1413 WARN("Source %p format must match the destination %p format, returning D3DERR_INVALIDCALL.\n",
1414 src_surface, dst_surface);
1415 wined3d_mutex_unlock();
1416 return D3DERR_INVALIDCALL;
1419 /* Quick if complete copy ... */
1420 if (!rect_count && !src_rects && !dst_points)
1422 RECT rect = {0, 0, src_w, src_h};
1423 wined3d_device_context_blt(device->immediate_context, dst->wined3d_texture, dst->sub_resource_idx, &rect,
1424 src->wined3d_texture, src->sub_resource_idx, &rect, 0, NULL, WINED3D_TEXF_POINT);
1426 else
1428 unsigned int i;
1429 /* Copy rect by rect */
1430 if (src_rects && dst_points)
1432 for (i = 0; i < rect_count; ++i)
1434 UINT w = src_rects[i].right - src_rects[i].left;
1435 UINT h = src_rects[i].bottom - src_rects[i].top;
1436 RECT dst_rect = {dst_points[i].x, dst_points[i].y,
1437 dst_points[i].x + w, dst_points[i].y + h};
1439 wined3d_device_context_blt(device->immediate_context,
1440 dst->wined3d_texture, dst->sub_resource_idx, &dst_rect,
1441 src->wined3d_texture, src->sub_resource_idx, &src_rects[i], 0, NULL, WINED3D_TEXF_POINT);
1444 else
1446 for (i = 0; i < rect_count; ++i)
1448 UINT w = src_rects[i].right - src_rects[i].left;
1449 UINT h = src_rects[i].bottom - src_rects[i].top;
1450 RECT dst_rect = {0, 0, w, h};
1452 wined3d_device_context_blt(device->immediate_context,
1453 dst->wined3d_texture, dst->sub_resource_idx, &dst_rect,
1454 src->wined3d_texture, src->sub_resource_idx, &src_rects[i], 0, NULL, WINED3D_TEXF_POINT);
1458 wined3d_mutex_unlock();
1460 return WINED3D_OK;
1463 static HRESULT WINAPI d3d8_device_UpdateTexture(IDirect3DDevice8 *iface,
1464 IDirect3DBaseTexture8 *src_texture, IDirect3DBaseTexture8 *dst_texture)
1466 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1467 struct d3d8_texture *src_impl, *dst_impl;
1468 HRESULT hr;
1470 TRACE("iface %p, src_texture %p, dst_texture %p.\n", iface, src_texture, dst_texture);
1472 src_impl = unsafe_impl_from_IDirect3DBaseTexture8(src_texture);
1473 dst_impl = unsafe_impl_from_IDirect3DBaseTexture8(dst_texture);
1475 if (src_impl->draw_texture || dst_impl->draw_texture)
1477 WARN("Source or destination is managed; returning D3DERR_INVALIDCALL.\n");
1478 return D3DERR_INVALIDCALL;
1481 wined3d_mutex_lock();
1482 hr = wined3d_device_update_texture(device->wined3d_device,
1483 src_impl->wined3d_texture, dst_impl->wined3d_texture);
1484 wined3d_mutex_unlock();
1486 return hr;
1489 static HRESULT WINAPI d3d8_device_GetFrontBuffer(IDirect3DDevice8 *iface, IDirect3DSurface8 *dst_surface)
1491 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1492 struct d3d8_surface *dst_impl = unsafe_impl_from_IDirect3DSurface8(dst_surface);
1493 HRESULT hr;
1495 TRACE("iface %p, dst_surface %p.\n", iface, dst_surface);
1497 if (!dst_surface)
1499 WARN("Invalid destination surface passed.\n");
1500 return D3DERR_INVALIDCALL;
1503 wined3d_mutex_lock();
1504 hr = wined3d_swapchain_get_front_buffer_data(device->implicit_swapchain,
1505 dst_impl->wined3d_texture, dst_impl->sub_resource_idx);
1506 wined3d_mutex_unlock();
1508 return hr;
1511 static HRESULT WINAPI d3d8_device_SetRenderTarget(IDirect3DDevice8 *iface,
1512 IDirect3DSurface8 *render_target, IDirect3DSurface8 *depth_stencil)
1514 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1515 struct d3d8_surface *rt_impl = unsafe_impl_from_IDirect3DSurface8(render_target);
1516 struct d3d8_surface *ds_impl = unsafe_impl_from_IDirect3DSurface8(depth_stencil);
1517 struct wined3d_rendertarget_view *original_dsv, *rtv;
1518 HRESULT hr = D3D_OK;
1520 TRACE("iface %p, render_target %p, depth_stencil %p.\n", iface, render_target, depth_stencil);
1522 if (rt_impl && d3d8_surface_get_device(rt_impl) != device)
1524 WARN("Render target surface does not match device.\n");
1525 return D3DERR_INVALIDCALL;
1528 wined3d_mutex_lock();
1530 if (ds_impl)
1532 struct wined3d_sub_resource_desc ds_desc, rt_desc;
1533 struct wined3d_rendertarget_view *original_rtv;
1534 struct d3d8_surface *original_surface;
1536 /* If no render target is passed in check the size against the current RT */
1537 if (!render_target)
1539 if (!(original_rtv = wined3d_device_context_get_rendertarget_view(device->immediate_context, 0)))
1541 wined3d_mutex_unlock();
1542 return D3DERR_NOTFOUND;
1544 original_surface = wined3d_rendertarget_view_get_sub_resource_parent(original_rtv);
1545 wined3d_texture_get_sub_resource_desc(original_surface->wined3d_texture,
1546 original_surface->sub_resource_idx, &rt_desc);
1548 else
1549 wined3d_texture_get_sub_resource_desc(rt_impl->wined3d_texture,
1550 rt_impl->sub_resource_idx, &rt_desc);
1552 wined3d_texture_get_sub_resource_desc(ds_impl->wined3d_texture, ds_impl->sub_resource_idx, &ds_desc);
1554 if (ds_desc.width < rt_desc.width || ds_desc.height < rt_desc.height)
1556 WARN("Depth stencil is smaller than the render target, returning D3DERR_INVALIDCALL\n");
1557 wined3d_mutex_unlock();
1558 return D3DERR_INVALIDCALL;
1560 if (ds_desc.multisample_type != rt_desc.multisample_type
1561 || ds_desc.multisample_quality != rt_desc.multisample_quality)
1563 WARN("Multisample settings do not match, returning D3DERR_INVALIDCALL\n");
1564 wined3d_mutex_unlock();
1565 return D3DERR_INVALIDCALL;
1569 original_dsv = wined3d_device_context_get_depth_stencil_view(device->immediate_context);
1570 rtv = ds_impl ? d3d8_surface_acquire_rendertarget_view(ds_impl) : NULL;
1571 hr = wined3d_device_context_set_depth_stencil_view(device->immediate_context, rtv);
1572 d3d8_surface_release_rendertarget_view(ds_impl, rtv);
1573 if (SUCCEEDED(hr))
1575 rtv = render_target ? d3d8_surface_acquire_rendertarget_view(rt_impl) : NULL;
1576 if (render_target)
1578 if (SUCCEEDED(hr = wined3d_device_context_set_rendertarget_views(device->immediate_context,
1579 0, 1, &rtv, TRUE)))
1580 device_reset_viewport_state(device);
1581 else
1582 wined3d_device_context_set_depth_stencil_view(device->immediate_context, original_dsv);
1584 d3d8_surface_release_rendertarget_view(rt_impl, rtv);
1587 wined3d_mutex_unlock();
1589 return hr;
1592 static HRESULT WINAPI d3d8_device_GetRenderTarget(IDirect3DDevice8 *iface, IDirect3DSurface8 **render_target)
1594 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1595 struct wined3d_rendertarget_view *wined3d_rtv;
1596 struct d3d8_surface *surface_impl;
1597 HRESULT hr;
1599 TRACE("iface %p, render_target %p.\n", iface, render_target);
1601 if (!render_target)
1602 return D3DERR_INVALIDCALL;
1604 wined3d_mutex_lock();
1605 if ((wined3d_rtv = wined3d_device_context_get_rendertarget_view(device->immediate_context, 0)))
1607 /* We want the sub resource parent here, since the view itself may be
1608 * internal to wined3d and may not have a parent. */
1609 surface_impl = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_rtv);
1610 *render_target = &surface_impl->IDirect3DSurface8_iface;
1611 IDirect3DSurface8_AddRef(*render_target);
1612 hr = D3D_OK;
1614 else
1616 ERR("Failed to get wined3d render target.\n");
1617 *render_target = NULL;
1618 hr = D3DERR_NOTFOUND;
1620 wined3d_mutex_unlock();
1622 return hr;
1625 static HRESULT WINAPI d3d8_device_GetDepthStencilSurface(IDirect3DDevice8 *iface, IDirect3DSurface8 **depth_stencil)
1627 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1628 struct wined3d_rendertarget_view *wined3d_dsv;
1629 struct d3d8_surface *surface_impl;
1630 HRESULT hr = D3D_OK;
1632 TRACE("iface %p, depth_stencil %p.\n", iface, depth_stencil);
1634 if (!depth_stencil)
1635 return D3DERR_INVALIDCALL;
1637 wined3d_mutex_lock();
1638 if ((wined3d_dsv = wined3d_device_context_get_depth_stencil_view(device->immediate_context)))
1640 /* We want the sub resource parent here, since the view itself may be
1641 * internal to wined3d and may not have a parent. */
1642 surface_impl = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_dsv);
1643 *depth_stencil = &surface_impl->IDirect3DSurface8_iface;
1644 IDirect3DSurface8_AddRef(*depth_stencil);
1646 else
1648 hr = D3DERR_NOTFOUND;
1649 *depth_stencil = NULL;
1651 wined3d_mutex_unlock();
1653 return hr;
1656 static HRESULT WINAPI d3d8_device_BeginScene(IDirect3DDevice8 *iface)
1658 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1659 HRESULT hr;
1661 TRACE("iface %p.\n", iface);
1663 wined3d_mutex_lock();
1664 hr = wined3d_device_begin_scene(device->wined3d_device);
1665 wined3d_mutex_unlock();
1667 return hr;
1670 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d8_device_EndScene(IDirect3DDevice8 *iface)
1672 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1673 HRESULT hr;
1675 TRACE("iface %p.\n", iface);
1677 wined3d_mutex_lock();
1678 hr = wined3d_device_end_scene(device->wined3d_device);
1679 wined3d_mutex_unlock();
1681 return hr;
1684 static HRESULT WINAPI d3d8_device_Clear(IDirect3DDevice8 *iface, DWORD rect_count,
1685 const D3DRECT *rects, DWORD flags, D3DCOLOR color, float z, DWORD stencil)
1687 const struct wined3d_color c =
1689 ((color >> 16) & 0xff) / 255.0f,
1690 ((color >> 8) & 0xff) / 255.0f,
1691 (color & 0xff) / 255.0f,
1692 ((color >> 24) & 0xff) / 255.0f,
1694 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1695 HRESULT hr;
1697 TRACE("iface %p, rect_count %lu, rects %p, flags %#lx, color 0x%08lx, z %.8e, stencil %lu.\n",
1698 iface, rect_count, rects, flags, color, z, stencil);
1700 if (rect_count && !rects)
1702 WARN("count %lu with NULL rects.\n", rect_count);
1703 rect_count = 0;
1706 wined3d_mutex_lock();
1707 wined3d_device_apply_stateblock(device->wined3d_device, device->state);
1708 hr = wined3d_device_clear(device->wined3d_device, rect_count, (const RECT *)rects, flags, &c, z, stencil);
1709 wined3d_mutex_unlock();
1711 return hr;
1714 static HRESULT WINAPI d3d8_device_SetTransform(IDirect3DDevice8 *iface,
1715 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
1717 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1719 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1721 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1722 wined3d_mutex_lock();
1723 wined3d_stateblock_set_transform(device->update_state,
1724 wined3d_transform_state_from_d3d(state), (const struct wined3d_matrix *)matrix);
1725 wined3d_mutex_unlock();
1727 return D3D_OK;
1730 static HRESULT WINAPI d3d8_device_GetTransform(IDirect3DDevice8 *iface,
1731 D3DTRANSFORMSTATETYPE state, D3DMATRIX *matrix)
1733 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1735 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1737 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1738 wined3d_mutex_lock();
1739 memcpy(matrix, &device->stateblock_state->transforms[state], sizeof(*matrix));
1740 wined3d_mutex_unlock();
1742 return D3D_OK;
1745 static HRESULT WINAPI d3d8_device_MultiplyTransform(IDirect3DDevice8 *iface,
1746 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
1748 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1750 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1752 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1753 wined3d_mutex_lock();
1754 wined3d_stateblock_multiply_transform(device->state,
1755 wined3d_transform_state_from_d3d(state), (const struct wined3d_matrix *)matrix);
1756 wined3d_mutex_unlock();
1758 return D3D_OK;
1761 static HRESULT WINAPI d3d8_device_SetViewport(IDirect3DDevice8 *iface, const D3DVIEWPORT8 *viewport)
1763 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1764 struct wined3d_sub_resource_desc rt_desc;
1765 struct wined3d_rendertarget_view *rtv;
1766 struct d3d8_surface *surface;
1767 struct wined3d_viewport vp;
1769 TRACE("iface %p, viewport %p.\n", iface, viewport);
1771 wined3d_mutex_lock();
1772 if (!(rtv = wined3d_device_context_get_rendertarget_view(device->immediate_context, 0)))
1774 wined3d_mutex_unlock();
1775 return D3DERR_NOTFOUND;
1777 surface = wined3d_rendertarget_view_get_sub_resource_parent(rtv);
1778 wined3d_texture_get_sub_resource_desc(surface->wined3d_texture, surface->sub_resource_idx, &rt_desc);
1780 if (viewport->X > rt_desc.width || viewport->Width > rt_desc.width - viewport->X
1781 || viewport->Y > rt_desc.height || viewport->Height > rt_desc.height - viewport->Y)
1783 WARN("Invalid viewport, returning D3DERR_INVALIDCALL.\n");
1784 wined3d_mutex_unlock();
1785 return D3DERR_INVALIDCALL;
1788 vp.x = viewport->X;
1789 vp.y = viewport->Y;
1790 vp.width = viewport->Width;
1791 vp.height = viewport->Height;
1792 vp.min_z = viewport->MinZ;
1793 vp.max_z = viewport->MaxZ;
1795 wined3d_stateblock_set_viewport(device->update_state, &vp);
1796 wined3d_mutex_unlock();
1798 return D3D_OK;
1801 static HRESULT WINAPI d3d8_device_GetViewport(IDirect3DDevice8 *iface, D3DVIEWPORT8 *viewport)
1803 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1804 struct wined3d_viewport wined3d_viewport;
1806 TRACE("iface %p, viewport %p.\n", iface, viewport);
1808 wined3d_mutex_lock();
1809 wined3d_viewport = device->stateblock_state->viewport;
1810 wined3d_mutex_unlock();
1812 viewport->X = wined3d_viewport.x;
1813 viewport->Y = wined3d_viewport.y;
1814 viewport->Width = wined3d_viewport.width;
1815 viewport->Height = wined3d_viewport.height;
1816 viewport->MinZ = wined3d_viewport.min_z;
1817 viewport->MaxZ = wined3d_viewport.max_z;
1819 return D3D_OK;
1822 static HRESULT WINAPI d3d8_device_SetMaterial(IDirect3DDevice8 *iface, const D3DMATERIAL8 *material)
1824 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1826 TRACE("iface %p, material %p.\n", iface, material);
1828 /* Note: D3DMATERIAL8 is compatible with struct wined3d_material. */
1829 wined3d_mutex_lock();
1830 wined3d_stateblock_set_material(device->update_state, (const struct wined3d_material *)material);
1831 wined3d_mutex_unlock();
1833 return D3D_OK;
1836 static HRESULT WINAPI d3d8_device_GetMaterial(IDirect3DDevice8 *iface, D3DMATERIAL8 *material)
1838 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1840 TRACE("iface %p, material %p.\n", iface, material);
1842 /* Note: D3DMATERIAL8 is compatible with struct wined3d_material. */
1843 wined3d_mutex_lock();
1844 memcpy(material, &device->stateblock_state->material, sizeof(*material));
1845 wined3d_mutex_unlock();
1847 return D3D_OK;
1850 static HRESULT WINAPI d3d8_device_SetLight(IDirect3DDevice8 *iface, DWORD index, const D3DLIGHT8 *light)
1852 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1853 HRESULT hr;
1855 TRACE("iface %p, index %lu, light %p.\n", iface, index, light);
1857 /* Note: D3DLIGHT8 is compatible with struct wined3d_light. */
1858 wined3d_mutex_lock();
1859 hr = wined3d_stateblock_set_light(device->update_state, index, (const struct wined3d_light *)light);
1860 wined3d_mutex_unlock();
1862 return hr;
1865 static HRESULT WINAPI d3d8_device_GetLight(IDirect3DDevice8 *iface, DWORD index, D3DLIGHT8 *light)
1867 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1868 BOOL enabled;
1869 HRESULT hr;
1871 TRACE("iface %p, index %lu, light %p.\n", iface, index, light);
1873 /* Note: D3DLIGHT8 is compatible with struct wined3d_light. */
1874 wined3d_mutex_lock();
1875 hr = wined3d_stateblock_get_light(device->state, index, (struct wined3d_light *)light, &enabled);
1876 wined3d_mutex_unlock();
1878 return hr;
1881 static HRESULT WINAPI d3d8_device_LightEnable(IDirect3DDevice8 *iface, DWORD index, BOOL enable)
1883 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1884 HRESULT hr;
1886 TRACE("iface %p, index %lu, enable %#x.\n", iface, index, enable);
1888 wined3d_mutex_lock();
1889 hr = wined3d_stateblock_set_light_enable(device->update_state, index, enable);
1890 wined3d_mutex_unlock();
1892 return hr;
1895 static HRESULT WINAPI d3d8_device_GetLightEnable(IDirect3DDevice8 *iface, DWORD index, BOOL *enabled)
1897 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1898 struct wined3d_light light;
1899 HRESULT hr;
1901 TRACE("iface %p, index %lu, enabled %p.\n", iface, index, enabled);
1903 wined3d_mutex_lock();
1904 hr = wined3d_stateblock_get_light(device->state, index, &light, enabled);
1905 wined3d_mutex_unlock();
1907 return hr;
1910 static HRESULT WINAPI d3d8_device_SetClipPlane(IDirect3DDevice8 *iface, DWORD index, const float *plane)
1912 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1913 HRESULT hr;
1915 TRACE("iface %p, index %lu, plane %p.\n", iface, index, plane);
1917 wined3d_mutex_lock();
1918 hr = wined3d_stateblock_set_clip_plane(device->update_state, index, (const struct wined3d_vec4 *)plane);
1919 wined3d_mutex_unlock();
1921 return hr;
1924 static HRESULT WINAPI d3d8_device_GetClipPlane(IDirect3DDevice8 *iface, DWORD index, float *plane)
1926 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1928 TRACE("iface %p, index %lu, plane %p.\n", iface, index, plane);
1930 if (index >= device->max_user_clip_planes)
1932 TRACE("Plane %lu requested, but only %u planes are supported.\n", index, device->max_user_clip_planes);
1933 return WINED3DERR_INVALIDCALL;
1936 wined3d_mutex_lock();
1937 memcpy(plane, &device->stateblock_state->clip_planes[index], sizeof(struct wined3d_vec4));
1938 wined3d_mutex_unlock();
1940 return D3D_OK;
1943 static void resolve_depth_buffer(struct d3d8_device *device)
1945 const struct wined3d_stateblock_state *state = device->stateblock_state;
1946 struct wined3d_rendertarget_view *wined3d_dsv;
1947 struct wined3d_resource *dst_resource;
1948 struct wined3d_texture *dst_texture;
1949 struct wined3d_resource_desc desc;
1950 struct d3d8_surface *d3d8_dsv;
1952 if (!(dst_texture = state->textures[0]))
1953 return;
1954 dst_resource = wined3d_texture_get_resource(dst_texture);
1955 wined3d_resource_get_desc(dst_resource, &desc);
1956 if (desc.format != WINED3DFMT_D24_UNORM_S8_UINT
1957 && desc.format != WINED3DFMT_X8D24_UNORM
1958 && desc.format != WINED3DFMT_DF16
1959 && desc.format != WINED3DFMT_DF24
1960 && desc.format != WINED3DFMT_INTZ)
1961 return;
1963 if (!(wined3d_dsv = wined3d_device_context_get_depth_stencil_view(device->immediate_context)))
1964 return;
1965 d3d8_dsv = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_dsv);
1967 wined3d_device_context_resolve_sub_resource(device->immediate_context, dst_resource, 0,
1968 wined3d_rendertarget_view_get_resource(wined3d_dsv), d3d8_dsv->sub_resource_idx, desc.format);
1971 static HRESULT WINAPI d3d8_device_SetRenderState(IDirect3DDevice8 *iface,
1972 D3DRENDERSTATETYPE state, DWORD value)
1974 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1976 TRACE("iface %p, state %#x, value %#lx.\n", iface, state, value);
1978 wined3d_mutex_lock();
1979 wined3d_stateblock_set_render_state(device->update_state, wined3d_render_state_from_d3d(state), value);
1980 if (state == D3DRS_POINTSIZE && value == D3D8_RESZ_CODE)
1981 resolve_depth_buffer(device);
1982 wined3d_mutex_unlock();
1984 return D3D_OK;
1987 static HRESULT WINAPI d3d8_device_GetRenderState(IDirect3DDevice8 *iface,
1988 D3DRENDERSTATETYPE state, DWORD *value)
1990 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1991 const struct wined3d_stateblock_state *device_state;
1993 TRACE("iface %p, state %#x, value %p.\n", iface, state, value);
1995 wined3d_mutex_lock();
1996 device_state = device->stateblock_state;
1997 switch (state)
1999 case D3DRS_ZBIAS:
2000 *value = device_state->rs[WINED3D_RS_DEPTHBIAS];
2001 break;
2003 default:
2004 *value = device_state->rs[state];
2006 wined3d_mutex_unlock();
2008 return D3D_OK;
2011 static HRESULT WINAPI d3d8_device_BeginStateBlock(IDirect3DDevice8 *iface)
2013 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2014 struct wined3d_stateblock *stateblock;
2015 HRESULT hr;
2017 TRACE("iface %p.\n", iface);
2019 wined3d_mutex_lock();
2020 if (device->recording)
2022 wined3d_mutex_unlock();
2023 WARN("Trying to begin a stateblock while recording, returning D3DERR_INVALIDCALL.\n");
2024 return D3DERR_INVALIDCALL;
2027 if (SUCCEEDED(hr = wined3d_stateblock_create(device->wined3d_device, NULL, WINED3D_SBT_RECORDED, &stateblock)))
2028 device->update_state = device->recording = stateblock;
2029 wined3d_mutex_unlock();
2031 return hr;
2034 static HRESULT WINAPI d3d8_device_EndStateBlock(IDirect3DDevice8 *iface, DWORD *token)
2036 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2037 struct wined3d_stateblock *stateblock;
2039 TRACE("iface %p, token %p.\n", iface, token);
2041 /* Tell wineD3D to endstateblock before anything else (in case we run out
2042 * of memory later and cause locking problems)
2044 wined3d_mutex_lock();
2045 if (!device->recording)
2047 wined3d_mutex_unlock();
2048 WARN("Trying to end a stateblock, but no stateblock is being recorded.\n");
2049 return D3DERR_INVALIDCALL;
2051 stateblock = device->recording;
2052 wined3d_stateblock_init_contained_states(stateblock);
2053 device->recording = NULL;
2054 device->update_state = device->state;
2056 *token = d3d8_allocate_handle(&device->handle_table, stateblock, D3D8_HANDLE_SB);
2057 wined3d_mutex_unlock();
2059 if (*token == D3D8_INVALID_HANDLE)
2061 ERR("Failed to create a handle\n");
2062 wined3d_mutex_lock();
2063 wined3d_stateblock_decref(stateblock);
2064 wined3d_mutex_unlock();
2065 return E_FAIL;
2067 ++*token;
2069 TRACE("Returning %#lx (%p).\n", *token, stateblock);
2071 return D3D_OK;
2074 static HRESULT WINAPI d3d8_device_ApplyStateBlock(IDirect3DDevice8 *iface, DWORD token)
2076 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2077 const struct wined3d_stateblock_state *state;
2078 struct d3d8_vertexbuffer *vertex_buffer;
2079 struct wined3d_stateblock *stateblock;
2080 struct d3d8_indexbuffer *index_buffer;
2081 struct wined3d_buffer *wined3d_buffer;
2082 unsigned int i;
2084 TRACE("iface %p, token %#lx.\n", iface, token);
2086 if (!token)
2087 return D3D_OK;
2089 wined3d_mutex_lock();
2090 if (device->recording)
2092 wined3d_mutex_unlock();
2093 WARN("Trying to apply stateblock while recording, returning D3DERR_INVALIDCALL.\n");
2094 return D3DERR_INVALIDCALL;
2096 stateblock = d3d8_get_object(&device->handle_table, token - 1, D3D8_HANDLE_SB);
2097 if (!stateblock)
2099 WARN("Invalid handle (%#lx) passed.\n", token);
2100 wined3d_mutex_unlock();
2101 return D3DERR_INVALIDCALL;
2103 wined3d_stateblock_apply(stateblock, device->state);
2104 device->sysmem_vb = 0;
2105 state = device->stateblock_state;
2106 for (i = 0; i < D3D8_MAX_STREAMS; ++i)
2108 wined3d_buffer = state->streams[i].buffer;
2109 if (!wined3d_buffer || !(vertex_buffer = wined3d_buffer_get_parent(wined3d_buffer)))
2110 continue;
2111 if (vertex_buffer->draw_buffer)
2112 device->sysmem_vb |= 1u << i;
2114 device->sysmem_ib = (wined3d_buffer = state->index_buffer)
2115 && (index_buffer = wined3d_buffer_get_parent(wined3d_buffer)) && index_buffer->sysmem;
2116 wined3d_mutex_unlock();
2118 return D3D_OK;
2121 static HRESULT WINAPI d3d8_device_CaptureStateBlock(IDirect3DDevice8 *iface, DWORD token)
2123 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2124 struct wined3d_stateblock *stateblock;
2126 TRACE("iface %p, token %#lx.\n", iface, token);
2128 wined3d_mutex_lock();
2129 if (device->recording)
2131 wined3d_mutex_unlock();
2132 WARN("Trying to capture stateblock while recording, returning D3DERR_INVALIDCALL.\n");
2133 return D3DERR_INVALIDCALL;
2135 stateblock = d3d8_get_object(&device->handle_table, token - 1, D3D8_HANDLE_SB);
2136 if (!stateblock)
2138 WARN("Invalid handle (%#lx) passed.\n", token);
2139 wined3d_mutex_unlock();
2140 return D3DERR_INVALIDCALL;
2142 wined3d_stateblock_capture(stateblock, device->state);
2143 wined3d_mutex_unlock();
2145 return D3D_OK;
2148 static HRESULT WINAPI d3d8_device_DeleteStateBlock(IDirect3DDevice8 *iface, DWORD token)
2150 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2151 struct wined3d_stateblock *stateblock;
2153 TRACE("iface %p, token %#lx.\n", iface, token);
2155 wined3d_mutex_lock();
2156 stateblock = d3d8_free_handle(&device->handle_table, token - 1, D3D8_HANDLE_SB);
2158 if (!stateblock)
2160 WARN("Invalid handle (%#lx) passed.\n", token);
2161 wined3d_mutex_unlock();
2162 return D3DERR_INVALIDCALL;
2165 if (wined3d_stateblock_decref(stateblock))
2167 ERR("Stateblock %p has references left, this shouldn't happen.\n", stateblock);
2169 wined3d_mutex_unlock();
2171 return D3D_OK;
2174 static HRESULT WINAPI d3d8_device_CreateStateBlock(IDirect3DDevice8 *iface,
2175 D3DSTATEBLOCKTYPE type, DWORD *handle)
2177 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2178 struct wined3d_stateblock *stateblock;
2179 HRESULT hr;
2181 TRACE("iface %p, type %#x, handle %p.\n", iface, type, handle);
2183 if (type != D3DSBT_ALL
2184 && type != D3DSBT_PIXELSTATE
2185 && type != D3DSBT_VERTEXSTATE)
2187 WARN("Unexpected stateblock type, returning D3DERR_INVALIDCALL\n");
2188 return D3DERR_INVALIDCALL;
2191 wined3d_mutex_lock();
2192 if (device->recording)
2194 wined3d_mutex_unlock();
2195 WARN("Trying to create a stateblock while recording, returning D3DERR_INVALIDCALL.\n");
2196 return D3DERR_INVALIDCALL;
2198 hr = wined3d_stateblock_create(device->wined3d_device, device->state, (enum wined3d_stateblock_type)type, &stateblock);
2199 if (FAILED(hr))
2201 wined3d_mutex_unlock();
2202 ERR("Failed to create the state block, hr %#lx.\n", hr);
2203 return hr;
2206 *handle = d3d8_allocate_handle(&device->handle_table, stateblock, D3D8_HANDLE_SB);
2207 wined3d_mutex_unlock();
2209 if (*handle == D3D8_INVALID_HANDLE)
2211 ERR("Failed to allocate a handle.\n");
2212 wined3d_mutex_lock();
2213 wined3d_stateblock_decref(stateblock);
2214 wined3d_mutex_unlock();
2215 return E_FAIL;
2217 ++*handle;
2219 TRACE("Returning %#lx (%p).\n", *handle, stateblock);
2221 return hr;
2224 static HRESULT WINAPI d3d8_device_SetClipStatus(IDirect3DDevice8 *iface, const D3DCLIPSTATUS8 *clip_status)
2226 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2227 HRESULT hr;
2229 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
2230 /* FIXME: Verify that D3DCLIPSTATUS8 ~= struct wined3d_clip_status. */
2232 wined3d_mutex_lock();
2233 hr = wined3d_device_set_clip_status(device->wined3d_device, (const struct wined3d_clip_status *)clip_status);
2234 wined3d_mutex_unlock();
2236 return hr;
2239 static HRESULT WINAPI d3d8_device_GetClipStatus(IDirect3DDevice8 *iface, D3DCLIPSTATUS8 *clip_status)
2241 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2242 HRESULT hr;
2244 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
2246 wined3d_mutex_lock();
2247 hr = wined3d_device_get_clip_status(device->wined3d_device, (struct wined3d_clip_status *)clip_status);
2248 wined3d_mutex_unlock();
2250 return hr;
2253 static HRESULT WINAPI d3d8_device_GetTexture(IDirect3DDevice8 *iface, DWORD stage, IDirect3DBaseTexture8 **texture)
2255 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2256 struct wined3d_texture *wined3d_texture;
2257 struct d3d8_texture *texture_impl;
2259 TRACE("iface %p, stage %lu, texture %p.\n", iface, stage, texture);
2261 if (!texture)
2262 return D3DERR_INVALIDCALL;
2264 if (stage >= WINED3D_MAX_FRAGMENT_SAMPLERS)
2266 WARN("Ignoring invalid stage %lu.\n", stage);
2267 *texture = NULL;
2268 return D3D_OK;
2271 wined3d_mutex_lock();
2272 if ((wined3d_texture = device->stateblock_state->textures[stage]))
2274 texture_impl = wined3d_texture_get_parent(wined3d_texture);
2275 *texture = &texture_impl->IDirect3DBaseTexture8_iface;
2276 IDirect3DBaseTexture8_AddRef(*texture);
2278 else
2280 *texture = NULL;
2282 wined3d_mutex_unlock();
2284 return D3D_OK;
2287 static HRESULT WINAPI d3d8_device_SetTexture(IDirect3DDevice8 *iface, DWORD stage, IDirect3DBaseTexture8 *texture)
2289 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2290 struct d3d8_texture *texture_impl;
2292 TRACE("iface %p, stage %lu, texture %p.\n", iface, stage, texture);
2294 texture_impl = unsafe_impl_from_IDirect3DBaseTexture8(texture);
2296 wined3d_mutex_lock();
2297 wined3d_stateblock_set_texture(device->update_state, stage,
2298 texture_impl ? d3d8_texture_get_draw_texture(texture_impl) : NULL);
2299 wined3d_mutex_unlock();
2301 return D3D_OK;
2304 static const struct tss_lookup
2306 BOOL sampler_state;
2307 union
2309 enum wined3d_texture_stage_state texture_state;
2310 enum wined3d_sampler_state sampler_state;
2311 } u;
2313 tss_lookup[] =
2315 {FALSE, {WINED3D_TSS_INVALID}}, /* 0, unused */
2316 {FALSE, {WINED3D_TSS_COLOR_OP}}, /* 1, D3DTSS_COLOROP */
2317 {FALSE, {WINED3D_TSS_COLOR_ARG1}}, /* 2, D3DTSS_COLORARG1 */
2318 {FALSE, {WINED3D_TSS_COLOR_ARG2}}, /* 3, D3DTSS_COLORARG2 */
2319 {FALSE, {WINED3D_TSS_ALPHA_OP}}, /* 4, D3DTSS_ALPHAOP */
2320 {FALSE, {WINED3D_TSS_ALPHA_ARG1}}, /* 5, D3DTSS_ALPHAARG1 */
2321 {FALSE, {WINED3D_TSS_ALPHA_ARG2}}, /* 6, D3DTSS_ALPHAARG2 */
2322 {FALSE, {WINED3D_TSS_BUMPENV_MAT00}}, /* 7, D3DTSS_BUMPENVMAT00 */
2323 {FALSE, {WINED3D_TSS_BUMPENV_MAT01}}, /* 8, D3DTSS_BUMPENVMAT01 */
2324 {FALSE, {WINED3D_TSS_BUMPENV_MAT10}}, /* 9, D3DTSS_BUMPENVMAT10 */
2325 {FALSE, {WINED3D_TSS_BUMPENV_MAT11}}, /* 10, D3DTSS_BUMPENVMAT11 */
2326 {FALSE, {WINED3D_TSS_TEXCOORD_INDEX}}, /* 11, D3DTSS_TEXCOORDINDEX */
2327 {FALSE, {WINED3D_TSS_INVALID}}, /* 12, unused */
2328 {TRUE, {WINED3D_SAMP_ADDRESS_U}}, /* 13, D3DTSS_ADDRESSU */
2329 {TRUE, {WINED3D_SAMP_ADDRESS_V}}, /* 14, D3DTSS_ADDRESSV */
2330 {TRUE, {WINED3D_SAMP_BORDER_COLOR}}, /* 15, D3DTSS_BORDERCOLOR */
2331 {TRUE, {WINED3D_SAMP_MAG_FILTER}}, /* 16, D3DTSS_MAGFILTER */
2332 {TRUE, {WINED3D_SAMP_MIN_FILTER}}, /* 17, D3DTSS_MINFILTER */
2333 {TRUE, {WINED3D_SAMP_MIP_FILTER}}, /* 18, D3DTSS_MIPFILTER */
2334 {TRUE, {WINED3D_SAMP_MIPMAP_LOD_BIAS}}, /* 19, D3DTSS_MIPMAPLODBIAS */
2335 {TRUE, {WINED3D_SAMP_MAX_MIP_LEVEL}}, /* 20, D3DTSS_MAXMIPLEVEL */
2336 {TRUE, {WINED3D_SAMP_MAX_ANISOTROPY}}, /* 21, D3DTSS_MAXANISOTROPY */
2337 {FALSE, {WINED3D_TSS_BUMPENV_LSCALE}}, /* 22, D3DTSS_BUMPENVLSCALE */
2338 {FALSE, {WINED3D_TSS_BUMPENV_LOFFSET}}, /* 23, D3DTSS_BUMPENVLOFFSET */
2339 {FALSE, {WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS}}, /* 24, D3DTSS_TEXTURETRANSFORMFLAGS */
2340 {TRUE, {WINED3D_SAMP_ADDRESS_W}}, /* 25, D3DTSS_ADDRESSW */
2341 {FALSE, {WINED3D_TSS_COLOR_ARG0}}, /* 26, D3DTSS_COLORARG0 */
2342 {FALSE, {WINED3D_TSS_ALPHA_ARG0}}, /* 27, D3DTSS_ALPHAARG0 */
2343 {FALSE, {WINED3D_TSS_RESULT_ARG}}, /* 28, D3DTSS_RESULTARG */
2346 static HRESULT WINAPI d3d8_device_GetTextureStageState(IDirect3DDevice8 *iface,
2347 DWORD stage, D3DTEXTURESTAGESTATETYPE state, DWORD *value)
2349 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2350 const struct wined3d_stateblock_state *device_state;
2351 const struct tss_lookup *l;
2353 TRACE("iface %p, stage %lu, state %#x, value %p.\n", iface, stage, state, value);
2355 if (state >= ARRAY_SIZE(tss_lookup))
2357 WARN("Invalid state %#x.\n", state);
2358 return D3D_OK;
2361 l = &tss_lookup[state];
2363 wined3d_mutex_lock();
2364 device_state = device->stateblock_state;
2365 if (l->sampler_state)
2366 *value = device_state->sampler_states[stage][l->u.sampler_state];
2367 else
2368 *value = device_state->texture_states[stage][l->u.texture_state];
2369 wined3d_mutex_unlock();
2371 return D3D_OK;
2374 static HRESULT WINAPI d3d8_device_SetTextureStageState(IDirect3DDevice8 *iface,
2375 DWORD stage, D3DTEXTURESTAGESTATETYPE type, DWORD value)
2377 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2378 const struct tss_lookup *l;
2380 TRACE("iface %p, stage %lu, state %#x, value %#lx.\n", iface, stage, type, value);
2382 if (type >= ARRAY_SIZE(tss_lookup))
2384 WARN("Invalid type %#x passed.\n", type);
2385 return D3D_OK;
2388 l = &tss_lookup[type];
2390 wined3d_mutex_lock();
2391 if (l->sampler_state)
2392 wined3d_stateblock_set_sampler_state(device->update_state, stage, l->u.sampler_state, value);
2393 else
2394 wined3d_stateblock_set_texture_stage_state(device->update_state, stage, l->u.texture_state, value);
2395 wined3d_mutex_unlock();
2397 return D3D_OK;
2400 static HRESULT WINAPI d3d8_device_ValidateDevice(IDirect3DDevice8 *iface, DWORD *pass_count)
2402 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2403 HRESULT hr;
2405 TRACE("iface %p, pass_count %p.\n", iface, pass_count);
2407 wined3d_mutex_lock();
2408 wined3d_device_apply_stateblock(device->wined3d_device, device->state);
2409 hr = wined3d_device_validate_device(device->wined3d_device, pass_count);
2410 wined3d_mutex_unlock();
2412 return hr;
2415 static HRESULT WINAPI d3d8_device_GetInfo(IDirect3DDevice8 *iface,
2416 DWORD info_id, void *info, DWORD info_size)
2418 TRACE("iface %p, info_id %#lx, info %p, info_size %lu.\n", iface, info_id, info, info_size);
2420 if (info_id < 4)
2421 return E_FAIL;
2422 return S_FALSE;
2425 static HRESULT WINAPI d3d8_device_SetPaletteEntries(IDirect3DDevice8 *iface,
2426 UINT palette_idx, const PALETTEENTRY *entries)
2428 WARN("iface %p, palette_idx %u, entries %p unimplemented\n", iface, palette_idx, entries);
2430 /* GPUs stopped supporting palettized textures with the Shader Model 1 generation. Wined3d
2431 * does not have a d3d8/9-style palette API */
2433 return D3D_OK;
2436 static HRESULT WINAPI d3d8_device_GetPaletteEntries(IDirect3DDevice8 *iface,
2437 UINT palette_idx, PALETTEENTRY *entries)
2439 FIXME("iface %p, palette_idx %u, entries %p unimplemented.\n", iface, palette_idx, entries);
2441 return D3DERR_INVALIDCALL;
2444 static HRESULT WINAPI d3d8_device_SetCurrentTexturePalette(IDirect3DDevice8 *iface, UINT palette_idx)
2446 WARN("iface %p, palette_idx %u unimplemented.\n", iface, palette_idx);
2448 return D3D_OK;
2451 static HRESULT WINAPI d3d8_device_GetCurrentTexturePalette(IDirect3DDevice8 *iface, UINT *palette_idx)
2453 FIXME("iface %p, palette_idx %p unimplemented.\n", iface, palette_idx);
2455 return D3DERR_INVALIDCALL;
2458 static void d3d8_device_upload_sysmem_vertex_buffers(struct d3d8_device *device,
2459 unsigned int start_vertex, unsigned int vertex_count)
2461 const struct wined3d_stateblock_state *state = device->stateblock_state;
2462 struct wined3d_vertex_declaration *wined3d_decl;
2463 struct wined3d_box box = {0, 0, 0, 1, 0, 1};
2464 const struct wined3d_stream_state *stream;
2465 struct d3d8_vertexbuffer *d3d8_buffer;
2466 struct wined3d_resource *dst_resource;
2467 struct d3d8_vertex_declaration *decl;
2468 struct wined3d_buffer *dst_buffer;
2469 struct wined3d_resource_desc desc;
2470 unsigned int stride, map;
2471 HRESULT hr;
2473 if (!device->sysmem_vb)
2474 return;
2476 if (!(wined3d_decl = state->vertex_declaration))
2477 return;
2479 decl = wined3d_vertex_declaration_get_parent(wined3d_decl);
2480 map = decl->stream_map & device->sysmem_vb;
2481 while (map)
2483 stream = &state->streams[wined3d_bit_scan(&map)];
2484 dst_buffer = stream->buffer;
2485 stride = stream->stride;
2487 d3d8_buffer = wined3d_buffer_get_parent(dst_buffer);
2488 dst_resource = wined3d_buffer_get_resource(dst_buffer);
2489 wined3d_resource_get_desc(dst_resource, &desc);
2490 box.left = stream->offset + start_vertex * stride;
2491 box.right = min(box.left + vertex_count * stride, desc.size);
2492 if (FAILED(hr = wined3d_device_context_copy_sub_resource_region(device->immediate_context,
2493 dst_resource, 0, box.left, 0, 0,
2494 wined3d_buffer_get_resource(d3d8_buffer->wined3d_buffer), 0, &box, 0)))
2495 ERR("Failed to update buffer.\n");
2499 static HRESULT d3d8_device_upload_sysmem_index_buffer(struct d3d8_device *device,
2500 unsigned int *start_idx, unsigned int idx_count)
2502 const struct wined3d_stateblock_state *state = device->stateblock_state;
2503 unsigned int src_offset, idx_size, buffer_size, pos;
2504 struct wined3d_resource_desc resource_desc;
2505 struct wined3d_resource *index_buffer;
2506 struct wined3d_map_desc map_desc;
2507 struct wined3d_box box;
2508 HRESULT hr;
2510 if (!device->sysmem_ib)
2511 return S_OK;
2513 index_buffer = wined3d_buffer_get_resource(state->index_buffer);
2514 wined3d_resource_get_desc(index_buffer, &resource_desc);
2515 idx_size = (state->index_format == WINED3DFMT_R16_UINT) ? 2 : 4;
2517 src_offset = (*start_idx) * idx_size;
2518 buffer_size = min(idx_count * idx_size, resource_desc.size - src_offset);
2520 wined3d_box_set(&box, src_offset, 0, buffer_size, 1, 0, 1);
2521 if (FAILED(hr = wined3d_resource_map(index_buffer, 0, &map_desc, &box, WINED3D_MAP_READ)))
2523 ERR("Failed to map index buffer, hr %#lx.\n", hr);
2524 return hr;
2526 wined3d_streaming_buffer_upload(device->wined3d_device, &device->index_buffer,
2527 map_desc.data, buffer_size, idx_size, &pos);
2528 wined3d_resource_unmap(index_buffer, 0);
2530 wined3d_device_context_set_index_buffer(device->immediate_context,
2531 device->index_buffer.buffer, state->index_format, pos);
2532 *start_idx = 0;
2533 return S_OK;
2536 static void d3d8_device_upload_managed_textures(struct d3d8_device *device)
2538 const struct wined3d_stateblock_state *state = device->stateblock_state;
2539 unsigned int i;
2541 for (i = 0; i < WINED3D_MAX_FRAGMENT_SAMPLERS; ++i)
2543 struct wined3d_texture *wined3d_texture = state->textures[i];
2544 struct d3d8_texture *d3d8_texture;
2546 if (!wined3d_texture)
2547 continue;
2548 d3d8_texture = wined3d_texture_get_parent(wined3d_texture);
2549 if (d3d8_texture->draw_texture)
2550 wined3d_device_update_texture(device->wined3d_device,
2551 d3d8_texture->wined3d_texture, d3d8_texture->draw_texture);
2555 static HRESULT WINAPI d3d8_device_DrawPrimitive(IDirect3DDevice8 *iface,
2556 D3DPRIMITIVETYPE primitive_type, UINT start_vertex, UINT primitive_count)
2558 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2559 struct d3d8_vertexbuffer *vb;
2560 unsigned int vertex_count, i;
2562 TRACE("iface %p, primitive_type %#x, start_vertex %u, primitive_count %u.\n",
2563 iface, primitive_type, start_vertex, primitive_count);
2565 vertex_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2566 wined3d_mutex_lock();
2567 d3d8_device_upload_managed_textures(device);
2568 d3d8_device_upload_sysmem_vertex_buffers(device, start_vertex, vertex_count);
2569 wined3d_device_context_set_primitive_type(device->immediate_context,
2570 wined3d_primitive_type_from_d3d(primitive_type), 0);
2571 wined3d_device_apply_stateblock(device->wined3d_device, device->state);
2572 wined3d_device_context_draw(device->immediate_context, start_vertex, vertex_count, 0, 0);
2574 for (i = 0; i < ARRAY_SIZE(device->stateblock_state->streams); ++i)
2576 if (device->stateblock_state->streams[i].buffer)
2578 vb = wined3d_buffer_get_parent(device->stateblock_state->streams[i].buffer);
2579 vb->discarded = false;
2583 wined3d_mutex_unlock();
2585 return D3D_OK;
2588 static HRESULT WINAPI d3d8_device_DrawIndexedPrimitive(IDirect3DDevice8 *iface,
2589 D3DPRIMITIVETYPE primitive_type, UINT min_vertex_idx, UINT vertex_count,
2590 UINT start_idx, UINT primitive_count)
2592 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2593 struct d3d8_vertexbuffer *vb;
2594 struct d3d8_indexbuffer *ib;
2595 unsigned int index_count, i;
2596 int base_vertex_index;
2597 HRESULT hr;
2599 TRACE("iface %p, primitive_type %#x, min_vertex_idx %u, vertex_count %u, start_idx %u, primitive_count %u.\n",
2600 iface, primitive_type, min_vertex_idx, vertex_count, start_idx, primitive_count);
2602 index_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2603 wined3d_mutex_lock();
2604 if (!device->stateblock_state->index_buffer)
2606 wined3d_mutex_unlock();
2607 WARN("Index buffer not set, returning D3D_OK.\n");
2608 return D3D_OK;
2610 base_vertex_index = device->stateblock_state->base_vertex_index;
2611 d3d8_device_upload_managed_textures(device);
2612 d3d8_device_upload_sysmem_vertex_buffers(device, base_vertex_index + min_vertex_idx, vertex_count);
2613 wined3d_device_context_set_primitive_type(device->immediate_context,
2614 wined3d_primitive_type_from_d3d(primitive_type), 0);
2615 wined3d_device_apply_stateblock(device->wined3d_device, device->state);
2616 if (FAILED(hr = d3d8_device_upload_sysmem_index_buffer(device, &start_idx, index_count)))
2618 wined3d_mutex_unlock();
2619 return hr;
2621 wined3d_device_context_draw_indexed(device->immediate_context, base_vertex_index, start_idx, index_count, 0, 0);
2623 ib = wined3d_buffer_get_parent(device->stateblock_state->index_buffer);
2624 ib->discarded = false;
2625 for (i = 0; i < ARRAY_SIZE(device->stateblock_state->streams); ++i)
2627 if (device->stateblock_state->streams[i].buffer)
2629 vb = wined3d_buffer_get_parent(device->stateblock_state->streams[i].buffer);
2630 vb->discarded = false;
2634 wined3d_mutex_unlock();
2636 return D3D_OK;
2639 static HRESULT WINAPI d3d8_device_DrawPrimitiveUP(IDirect3DDevice8 *iface,
2640 D3DPRIMITIVETYPE primitive_type, UINT primitive_count, const void *data,
2641 UINT stride)
2643 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2644 UINT vtx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2645 UINT size = vtx_count * stride;
2646 unsigned int vb_pos;
2647 HRESULT hr;
2649 TRACE("iface %p, primitive_type %#x, primitive_count %u, data %p, stride %u.\n",
2650 iface, primitive_type, primitive_count, data, stride);
2652 if (!primitive_count || !stride)
2654 WARN("primitive_count or stride is 0, returning D3D_OK.\n");
2655 return D3D_OK;
2658 wined3d_mutex_lock();
2660 if (FAILED(hr = wined3d_streaming_buffer_upload(device->wined3d_device,
2661 &device->vertex_buffer, data, size, stride, &vb_pos)))
2662 goto done;
2664 hr = wined3d_stateblock_set_stream_source(device->state, 0, device->vertex_buffer.buffer, 0, stride);
2665 if (FAILED(hr))
2666 goto done;
2668 d3d8_device_upload_managed_textures(device);
2669 wined3d_device_context_set_primitive_type(device->immediate_context,
2670 wined3d_primitive_type_from_d3d(primitive_type), 0);
2671 wined3d_device_apply_stateblock(device->wined3d_device, device->state);
2672 wined3d_device_context_draw(device->immediate_context, vb_pos / stride, vtx_count, 0, 0);
2673 wined3d_stateblock_set_stream_source(device->state, 0, NULL, 0, 0);
2675 done:
2676 wined3d_mutex_unlock();
2677 return hr;
2680 static HRESULT WINAPI d3d8_device_DrawIndexedPrimitiveUP(IDirect3DDevice8 *iface,
2681 D3DPRIMITIVETYPE primitive_type, UINT min_vertex_idx, UINT vertex_count,
2682 UINT primitive_count, const void *index_data, D3DFORMAT index_format,
2683 const void *vertex_data, UINT vertex_stride)
2685 UINT idx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2686 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2687 UINT idx_fmt_size = index_format == D3DFMT_INDEX16 ? 2 : 4;
2688 unsigned int base_vertex_idx, vb_pos, ib_pos;
2689 UINT vtx_size = vertex_count * vertex_stride;
2690 UINT idx_size = idx_count * idx_fmt_size;
2691 HRESULT hr;
2693 TRACE("iface %p, primitive_type %#x, min_vertex_idx %u, vertex_count %u, primitive_count %u, "
2694 "index_data %p, index_format %#x, vertex_data %p, vertex_stride %u.\n",
2695 iface, primitive_type, min_vertex_idx, vertex_count, primitive_count,
2696 index_data, index_format, vertex_data, vertex_stride);
2698 if (!primitive_count || !vertex_stride)
2700 WARN("primitive_count or vertex_stride is 0, returning D3D_OK.\n");
2701 return D3D_OK;
2704 wined3d_mutex_lock();
2706 if (FAILED(hr = wined3d_streaming_buffer_upload(device->wined3d_device, &device->vertex_buffer,
2707 (char *)vertex_data + min_vertex_idx * vertex_stride, vtx_size, vertex_stride, &vb_pos)))
2708 goto done;
2710 if (FAILED(hr = wined3d_streaming_buffer_upload(device->wined3d_device, &device->index_buffer,
2711 index_data, idx_size, idx_fmt_size, &ib_pos)))
2712 goto done;
2714 hr = wined3d_stateblock_set_stream_source(device->state, 0, device->vertex_buffer.buffer, 0, vertex_stride);
2715 if (FAILED(hr))
2716 goto done;
2718 wined3d_stateblock_set_index_buffer(device->state, device->index_buffer.buffer,
2719 wined3dformat_from_d3dformat(index_format));
2720 base_vertex_idx = vb_pos / vertex_stride - min_vertex_idx;
2721 wined3d_stateblock_set_base_vertex_index(device->state, base_vertex_idx);
2723 d3d8_device_upload_managed_textures(device);
2724 wined3d_device_context_set_primitive_type(device->immediate_context,
2725 wined3d_primitive_type_from_d3d(primitive_type), 0);
2726 wined3d_device_apply_stateblock(device->wined3d_device, device->state);
2727 wined3d_device_context_draw_indexed(device->immediate_context,
2728 base_vertex_idx, ib_pos / idx_fmt_size, idx_count, 0, 0);
2730 wined3d_stateblock_set_stream_source(device->state, 0, NULL, 0, 0);
2731 wined3d_stateblock_set_index_buffer(device->state, NULL, WINED3DFMT_UNKNOWN);
2732 wined3d_stateblock_set_base_vertex_index(device->state, 0);
2734 done:
2735 wined3d_mutex_unlock();
2736 return hr;
2739 static HRESULT WINAPI d3d8_device_ProcessVertices(IDirect3DDevice8 *iface, UINT src_start_idx,
2740 UINT dst_idx, UINT vertex_count, IDirect3DVertexBuffer8 *dst_buffer, DWORD flags)
2742 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2743 struct d3d8_vertexbuffer *dst = unsafe_impl_from_IDirect3DVertexBuffer8(dst_buffer);
2744 const struct wined3d_stateblock_state *state;
2745 const struct wined3d_stream_state *stream;
2746 struct d3d8_vertexbuffer *d3d8_buffer;
2747 unsigned int i, map;
2748 HRESULT hr;
2750 TRACE("iface %p, src_start_idx %u, dst_idx %u, vertex_count %u, dst_buffer %p, flags %#lx.\n",
2751 iface, src_start_idx, dst_idx, vertex_count, dst_buffer, flags);
2753 wined3d_mutex_lock();
2755 state = device->stateblock_state;
2757 /* Note that an alternative approach would be to simply create these
2758 * buffers with WINED3D_RESOURCE_ACCESS_MAP_R and update them here like we
2759 * do for draws. In some regards that would be easier, but it seems less
2760 * than optimal to upload data to the GPU only to subsequently download it
2761 * again. */
2762 map = device->sysmem_vb;
2763 while (map)
2765 i = wined3d_bit_scan(&map);
2766 stream = &state->streams[i];
2768 d3d8_buffer = wined3d_buffer_get_parent(stream->buffer);
2769 if (FAILED(wined3d_stateblock_set_stream_source(device->state,
2770 i, d3d8_buffer->wined3d_buffer, stream->offset, stream->stride)))
2771 ERR("Failed to set stream source.\n");
2774 wined3d_device_apply_stateblock(device->wined3d_device, device->state);
2775 hr = wined3d_device_process_vertices(device->wined3d_device, src_start_idx, dst_idx,
2776 vertex_count, dst->wined3d_buffer, NULL, flags, dst->fvf);
2778 map = device->sysmem_vb;
2779 while (map)
2781 i = wined3d_bit_scan(&map);
2782 stream = &state->streams[i];
2784 d3d8_buffer = wined3d_buffer_get_parent(stream->buffer);
2785 if (FAILED(wined3d_stateblock_set_stream_source(device->state,
2786 i, d3d8_buffer->draw_buffer, stream->offset, stream->stride)))
2787 ERR("Failed to set stream source.\n");
2790 wined3d_mutex_unlock();
2792 return hr;
2795 static HRESULT WINAPI d3d8_device_CreateVertexShader(IDirect3DDevice8 *iface,
2796 const DWORD *declaration, const DWORD *byte_code, DWORD *shader, DWORD usage)
2798 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2799 struct d3d8_vertex_shader *object;
2800 DWORD shader_handle;
2801 DWORD handle;
2802 HRESULT hr;
2804 TRACE("iface %p, declaration %p, byte_code %p, shader %p, usage %#lx.\n",
2805 iface, declaration, byte_code, shader, usage);
2807 if (!(object = heap_alloc_zero(sizeof(*object))))
2809 *shader = 0;
2810 return E_OUTOFMEMORY;
2813 wined3d_mutex_lock();
2814 handle = d3d8_allocate_handle(&device->handle_table, object, D3D8_HANDLE_VS);
2815 wined3d_mutex_unlock();
2816 if (handle == D3D8_INVALID_HANDLE)
2818 ERR("Failed to allocate vertex shader handle.\n");
2819 heap_free(object);
2820 *shader = 0;
2821 return E_OUTOFMEMORY;
2824 shader_handle = handle + VS_HIGHESTFIXEDFXF + 1;
2826 hr = d3d8_vertex_shader_init(object, device, declaration, byte_code, shader_handle, usage);
2827 if (FAILED(hr))
2829 WARN("Failed to initialize vertex shader, hr %#lx.\n", hr);
2830 wined3d_mutex_lock();
2831 d3d8_free_handle(&device->handle_table, handle, D3D8_HANDLE_VS);
2832 wined3d_mutex_unlock();
2833 heap_free(object);
2834 *shader = 0;
2835 return hr;
2838 TRACE("Created vertex shader %p (handle %#lx).\n", object, shader_handle);
2839 *shader = shader_handle;
2841 return D3D_OK;
2844 static struct d3d8_vertex_declaration *d3d8_device_get_fvf_declaration(struct d3d8_device *device, DWORD fvf)
2846 struct d3d8_vertex_declaration *d3d8_declaration;
2847 struct FvfToDecl *convertedDecls = device->decls;
2848 int p, low, high; /* deliberately signed */
2849 HRESULT hr;
2851 TRACE("Searching for declaration for fvf %#lx... ", fvf);
2853 low = 0;
2854 high = device->numConvertedDecls - 1;
2855 while (low <= high)
2857 p = (low + high) >> 1;
2858 TRACE("%d ", p);
2860 if (convertedDecls[p].fvf == fvf)
2862 TRACE("found %p\n", convertedDecls[p].declaration);
2863 return convertedDecls[p].declaration;
2866 if (convertedDecls[p].fvf < fvf)
2867 low = p + 1;
2868 else
2869 high = p - 1;
2871 TRACE("not found. Creating and inserting at position %d.\n", low);
2873 if (!(d3d8_declaration = heap_alloc(sizeof(*d3d8_declaration))))
2874 return NULL;
2876 if (FAILED(hr = d3d8_vertex_declaration_init_fvf(d3d8_declaration, device, fvf)))
2878 WARN("Failed to initialize vertex declaration, hr %#lx.\n", hr);
2879 heap_free(d3d8_declaration);
2880 return NULL;
2883 if (device->declArraySize == device->numConvertedDecls)
2885 UINT grow = device->declArraySize / 2;
2887 if (!(convertedDecls = heap_realloc(convertedDecls,
2888 sizeof(*convertedDecls) * (device->numConvertedDecls + grow))))
2890 d3d8_vertex_declaration_destroy(d3d8_declaration);
2891 return NULL;
2893 device->decls = convertedDecls;
2894 device->declArraySize += grow;
2897 memmove(convertedDecls + low + 1, convertedDecls + low,
2898 sizeof(*convertedDecls) * (device->numConvertedDecls - low));
2899 convertedDecls[low].declaration = d3d8_declaration;
2900 convertedDecls[low].fvf = fvf;
2901 ++device->numConvertedDecls;
2903 TRACE("Returning %p. %u decls in array.\n", d3d8_declaration, device->numConvertedDecls);
2905 return d3d8_declaration;
2908 static HRESULT WINAPI d3d8_device_SetVertexShader(IDirect3DDevice8 *iface, DWORD shader)
2910 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2911 struct d3d8_vertex_shader *shader_impl;
2913 TRACE("iface %p, shader %#lx.\n", iface, shader);
2915 if (VS_HIGHESTFIXEDFXF >= shader)
2917 TRACE("Setting FVF, %#lx\n", shader);
2919 wined3d_mutex_lock();
2920 wined3d_stateblock_set_vertex_declaration(device->update_state,
2921 d3d8_device_get_fvf_declaration(device, shader)->wined3d_vertex_declaration);
2922 wined3d_stateblock_set_vertex_shader(device->update_state, NULL);
2923 wined3d_mutex_unlock();
2925 return D3D_OK;
2928 TRACE("Setting shader\n");
2930 wined3d_mutex_lock();
2931 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS)))
2933 WARN("Invalid handle (%#lx) passed.\n", shader);
2934 wined3d_mutex_unlock();
2936 return D3DERR_INVALIDCALL;
2939 wined3d_stateblock_set_vertex_declaration(device->update_state,
2940 shader_impl->vertex_declaration->wined3d_vertex_declaration);
2941 wined3d_stateblock_set_vertex_shader(device->update_state, shader_impl->wined3d_shader);
2942 wined3d_mutex_unlock();
2944 return D3D_OK;
2947 static HRESULT WINAPI d3d8_device_GetVertexShader(IDirect3DDevice8 *iface, DWORD *shader)
2949 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2950 struct wined3d_vertex_declaration *wined3d_declaration;
2951 struct d3d8_vertex_declaration *d3d8_declaration;
2953 TRACE("iface %p, shader %p.\n", iface, shader);
2955 wined3d_mutex_lock();
2956 if ((wined3d_declaration = device->stateblock_state->vertex_declaration))
2958 d3d8_declaration = wined3d_vertex_declaration_get_parent(wined3d_declaration);
2959 *shader = d3d8_declaration->shader_handle;
2961 else
2963 *shader = 0;
2965 wined3d_mutex_unlock();
2967 TRACE("Returning %#lx.\n", *shader);
2969 return D3D_OK;
2972 static HRESULT WINAPI d3d8_device_DeleteVertexShader(IDirect3DDevice8 *iface, DWORD shader)
2974 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2975 struct d3d8_vertex_shader *shader_impl;
2977 TRACE("iface %p, shader %#lx.\n", iface, shader);
2979 wined3d_mutex_lock();
2980 if (!(shader_impl = d3d8_free_handle(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS)))
2982 WARN("Invalid handle (%#lx) passed.\n", shader);
2983 wined3d_mutex_unlock();
2985 return D3DERR_INVALIDCALL;
2988 if (shader_impl->wined3d_shader
2989 && device->stateblock_state->vs == shader_impl->wined3d_shader)
2990 IDirect3DDevice8_SetVertexShader(iface, 0);
2992 wined3d_mutex_unlock();
2994 d3d8_vertex_shader_destroy(shader_impl);
2996 return D3D_OK;
2999 static HRESULT WINAPI d3d8_device_SetVertexShaderConstant(IDirect3DDevice8 *iface,
3000 DWORD start_register, const void *data, DWORD count)
3002 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3003 HRESULT hr;
3005 TRACE("iface %p, start_register %lu, data %p, count %lu.\n",
3006 iface, start_register, data, count);
3008 if (start_register + count > D3D8_MAX_VERTEX_SHADER_CONSTANTF)
3010 WARN("Trying to access %lu constants, but d3d8 only supports %u.\n",
3011 start_register + count, D3D8_MAX_VERTEX_SHADER_CONSTANTF);
3012 return D3DERR_INVALIDCALL;
3015 wined3d_mutex_lock();
3016 hr = wined3d_stateblock_set_vs_consts_f(device->update_state, start_register, count, data);
3017 wined3d_mutex_unlock();
3019 return hr;
3022 static HRESULT WINAPI d3d8_device_GetVertexShaderConstant(IDirect3DDevice8 *iface,
3023 DWORD start_idx, void *constants, DWORD count)
3025 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3026 const struct wined3d_vec4 *src;
3028 TRACE("iface %p, start_idx %lu, constants %p, count %lu.\n", iface, start_idx, constants, count);
3030 if (!constants)
3031 return D3DERR_INVALIDCALL;
3033 if (!wined3d_bound_range(start_idx, count, device->vs_uniform_count))
3035 WARN("Trying to access %lu constants, but d3d8 only supports %u.\n",
3036 start_idx + count, device->vs_uniform_count);
3037 return D3DERR_INVALIDCALL;
3040 wined3d_mutex_lock();
3041 src = device->stateblock_state->vs_consts_f;
3042 memcpy(constants, &src[start_idx], count * sizeof(*src));
3043 wined3d_mutex_unlock();
3045 return D3D_OK;
3048 static HRESULT WINAPI d3d8_device_GetVertexShaderDeclaration(IDirect3DDevice8 *iface,
3049 DWORD shader, void *data, DWORD *data_size)
3051 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3052 struct d3d8_vertex_declaration *declaration;
3053 struct d3d8_vertex_shader *shader_impl;
3055 TRACE("iface %p, shader %#lx, data %p, data_size %p.\n", iface, shader, data, data_size);
3057 wined3d_mutex_lock();
3058 shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS);
3059 wined3d_mutex_unlock();
3061 if (!shader_impl)
3063 WARN("Invalid handle (%#lx) passed.\n", shader);
3064 return D3DERR_INVALIDCALL;
3066 declaration = shader_impl->vertex_declaration;
3068 if (!data)
3070 *data_size = declaration->elements_size;
3071 return D3D_OK;
3074 /* MSDN claims that if *data_size is smaller than the required size
3075 * we should write the required size and return D3DERR_MOREDATA.
3076 * That's not actually true. */
3077 if (*data_size < declaration->elements_size)
3078 return D3DERR_INVALIDCALL;
3080 memcpy(data, declaration->elements, declaration->elements_size);
3082 return D3D_OK;
3085 static HRESULT WINAPI d3d8_device_GetVertexShaderFunction(IDirect3DDevice8 *iface,
3086 DWORD shader, void *data, DWORD *data_size)
3088 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3089 struct d3d8_vertex_shader *shader_impl = NULL;
3090 HRESULT hr;
3092 TRACE("iface %p, shader %#lx, data %p, data_size %p.\n", iface, shader, data, data_size);
3094 wined3d_mutex_lock();
3095 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS)))
3097 WARN("Invalid handle (%#lx) passed.\n", shader);
3098 wined3d_mutex_unlock();
3100 return D3DERR_INVALIDCALL;
3103 if (!shader_impl->wined3d_shader)
3105 wined3d_mutex_unlock();
3106 *data_size = 0;
3107 return D3D_OK;
3110 hr = wined3d_shader_get_byte_code(shader_impl->wined3d_shader, data, (unsigned int *)data_size);
3111 wined3d_mutex_unlock();
3113 return hr;
3116 static HRESULT WINAPI d3d8_device_SetIndices(IDirect3DDevice8 *iface,
3117 IDirect3DIndexBuffer8 *buffer, UINT base_vertex_idx)
3119 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3120 struct d3d8_indexbuffer *ib = unsafe_impl_from_IDirect3DIndexBuffer8(buffer);
3121 struct wined3d_buffer *wined3d_buffer;
3123 TRACE("iface %p, buffer %p, base_vertex_idx %u.\n", iface, buffer, base_vertex_idx);
3125 if (!ib)
3126 wined3d_buffer = NULL;
3127 else
3128 wined3d_buffer = ib->wined3d_buffer;
3130 /* WineD3D takes an INT(due to d3d9), but d3d8 uses UINTs. Do I have to add a check here that
3131 * the UINT doesn't cause an overflow in the INT? It seems rather unlikely because such large
3132 * vertex buffers can't be created to address them with an index that requires the 32nd bit
3133 * (4 Byte minimum vertex size * 2^31-1 -> 8 gb buffer. The index sign would be the least
3134 * problem)
3136 wined3d_mutex_lock();
3137 wined3d_stateblock_set_base_vertex_index(device->update_state, base_vertex_idx);
3138 wined3d_stateblock_set_index_buffer(device->update_state, wined3d_buffer, ib ? ib->format : WINED3DFMT_UNKNOWN);
3139 if (!device->recording)
3140 device->sysmem_ib = ib && ib->sysmem;
3141 wined3d_mutex_unlock();
3143 return D3D_OK;
3146 static HRESULT WINAPI d3d8_device_GetIndices(IDirect3DDevice8 *iface,
3147 IDirect3DIndexBuffer8 **buffer, UINT *base_vertex_index)
3149 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3150 const struct wined3d_stateblock_state *state;
3151 struct wined3d_buffer *wined3d_buffer;
3152 struct d3d8_indexbuffer *buffer_impl;
3154 TRACE("iface %p, buffer %p, base_vertex_index %p.\n", iface, buffer, base_vertex_index);
3156 if (!buffer)
3157 return D3DERR_INVALIDCALL;
3159 /* The case from UINT to INT is safe because d3d8 will never set negative values */
3160 wined3d_mutex_lock();
3161 state = device->stateblock_state;
3162 *base_vertex_index = state->base_vertex_index;
3163 if ((wined3d_buffer = state->index_buffer))
3165 buffer_impl = wined3d_buffer_get_parent(wined3d_buffer);
3166 *buffer = &buffer_impl->IDirect3DIndexBuffer8_iface;
3167 IDirect3DIndexBuffer8_AddRef(*buffer);
3169 else
3171 *buffer = NULL;
3173 wined3d_mutex_unlock();
3175 return D3D_OK;
3178 static HRESULT WINAPI d3d8_device_CreatePixelShader(IDirect3DDevice8 *iface,
3179 const DWORD *byte_code, DWORD *shader)
3181 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3182 struct d3d8_pixel_shader *object;
3183 DWORD shader_handle;
3184 DWORD handle;
3185 HRESULT hr;
3187 TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
3189 if (!shader)
3190 return D3DERR_INVALIDCALL;
3192 if (!(object = heap_alloc_zero(sizeof(*object))))
3193 return E_OUTOFMEMORY;
3195 wined3d_mutex_lock();
3196 handle = d3d8_allocate_handle(&device->handle_table, object, D3D8_HANDLE_PS);
3197 wined3d_mutex_unlock();
3198 if (handle == D3D8_INVALID_HANDLE)
3200 ERR("Failed to allocate pixel shader handle.\n");
3201 heap_free(object);
3202 return E_OUTOFMEMORY;
3205 shader_handle = handle + VS_HIGHESTFIXEDFXF + 1;
3207 hr = d3d8_pixel_shader_init(object, device, byte_code, shader_handle);
3208 if (FAILED(hr))
3210 WARN("Failed to initialize pixel shader, hr %#lx.\n", hr);
3211 wined3d_mutex_lock();
3212 d3d8_free_handle(&device->handle_table, handle, D3D8_HANDLE_PS);
3213 wined3d_mutex_unlock();
3214 heap_free(object);
3215 *shader = 0;
3216 return hr;
3219 TRACE("Created pixel shader %p (handle %#lx).\n", object, shader_handle);
3220 *shader = shader_handle;
3222 return D3D_OK;
3225 static HRESULT WINAPI d3d8_device_SetPixelShader(IDirect3DDevice8 *iface, DWORD shader)
3227 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3228 struct d3d8_pixel_shader *shader_impl;
3230 TRACE("iface %p, shader %#lx.\n", iface, shader);
3232 wined3d_mutex_lock();
3234 if (!shader)
3236 wined3d_stateblock_set_pixel_shader(device->update_state, NULL);
3237 wined3d_mutex_unlock();
3238 return D3D_OK;
3241 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS)))
3243 WARN("Invalid handle (%#lx) passed.\n", shader);
3244 wined3d_mutex_unlock();
3245 return D3DERR_INVALIDCALL;
3248 TRACE("Setting shader %p.\n", shader_impl);
3249 wined3d_stateblock_set_pixel_shader(device->update_state, shader_impl->wined3d_shader);
3250 wined3d_mutex_unlock();
3252 return D3D_OK;
3255 static HRESULT WINAPI d3d8_device_GetPixelShader(IDirect3DDevice8 *iface, DWORD *shader)
3257 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3258 struct wined3d_shader *object;
3260 TRACE("iface %p, shader %p.\n", iface, shader);
3262 if (!shader)
3263 return D3DERR_INVALIDCALL;
3265 wined3d_mutex_lock();
3266 if ((object = device->stateblock_state->ps))
3268 struct d3d8_pixel_shader *d3d8_shader;
3269 d3d8_shader = wined3d_shader_get_parent(object);
3270 *shader = d3d8_shader->handle;
3272 else
3274 *shader = 0;
3276 wined3d_mutex_unlock();
3278 TRACE("Returning %#lx.\n", *shader);
3280 return D3D_OK;
3283 static HRESULT WINAPI d3d8_device_DeletePixelShader(IDirect3DDevice8 *iface, DWORD shader)
3285 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3286 struct d3d8_pixel_shader *shader_impl;
3288 TRACE("iface %p, shader %#lx.\n", iface, shader);
3290 wined3d_mutex_lock();
3292 if (!(shader_impl = d3d8_free_handle(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS)))
3294 WARN("Invalid handle (%#lx) passed.\n", shader);
3295 wined3d_mutex_unlock();
3296 return D3DERR_INVALIDCALL;
3299 if (device->stateblock_state->ps == shader_impl->wined3d_shader)
3300 IDirect3DDevice8_SetPixelShader(iface, 0);
3302 wined3d_mutex_unlock();
3304 d3d8_pixel_shader_destroy(shader_impl);
3306 return D3D_OK;
3309 static HRESULT WINAPI d3d8_device_SetPixelShaderConstant(IDirect3DDevice8 *iface,
3310 DWORD start_register, const void *data, DWORD count)
3312 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3313 HRESULT hr;
3315 TRACE("iface %p, start_register %lu, data %p, count %lu.\n", iface, start_register, data, count);
3317 wined3d_mutex_lock();
3318 hr = wined3d_stateblock_set_ps_consts_f(device->update_state, start_register, count, data);
3319 wined3d_mutex_unlock();
3321 return hr;
3324 static HRESULT WINAPI d3d8_device_GetPixelShaderConstant(IDirect3DDevice8 *iface,
3325 DWORD start_idx, void *constants, DWORD count)
3327 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3328 const struct wined3d_vec4 *src;
3330 TRACE("iface %p, start_idx %lu, constants %p, count %lu.\n", iface, start_idx, constants, count);
3332 if (!constants || !wined3d_bound_range(start_idx, count, D3D8_MAX_PIXEL_SHADER_CONSTANTF))
3333 return WINED3DERR_INVALIDCALL;
3335 wined3d_mutex_lock();
3336 src = device->stateblock_state->ps_consts_f;
3337 memcpy(constants, &src[start_idx], count * sizeof(*src));
3338 wined3d_mutex_unlock();
3340 return D3D_OK;
3343 static HRESULT WINAPI d3d8_device_GetPixelShaderFunction(IDirect3DDevice8 *iface,
3344 DWORD shader, void *data, DWORD *data_size)
3346 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3347 struct d3d8_pixel_shader *shader_impl = NULL;
3348 HRESULT hr;
3350 TRACE("iface %p, shader %#lx, data %p, data_size %p.\n", iface, shader, data, data_size);
3352 wined3d_mutex_lock();
3353 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS)))
3355 WARN("Invalid handle (%#lx) passed.\n", shader);
3356 wined3d_mutex_unlock();
3358 return D3DERR_INVALIDCALL;
3361 hr = wined3d_shader_get_byte_code(shader_impl->wined3d_shader, data, (unsigned int *)data_size);
3362 wined3d_mutex_unlock();
3364 return hr;
3367 static HRESULT WINAPI d3d8_device_DrawRectPatch(IDirect3DDevice8 *iface, UINT handle,
3368 const float *segment_count, const D3DRECTPATCH_INFO *patch_info)
3370 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
3371 iface, handle, segment_count, patch_info);
3372 return D3D_OK;
3375 static HRESULT WINAPI d3d8_device_DrawTriPatch(IDirect3DDevice8 *iface, UINT handle,
3376 const float *segment_count, const D3DTRIPATCH_INFO *patch_info)
3378 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
3379 iface, handle, segment_count, patch_info);
3380 return D3D_OK;
3383 static HRESULT WINAPI d3d8_device_DeletePatch(IDirect3DDevice8 *iface, UINT handle)
3385 FIXME("iface %p, handle %#x unimplemented.\n", iface, handle);
3386 return D3DERR_INVALIDCALL;
3389 static HRESULT WINAPI d3d8_device_SetStreamSource(IDirect3DDevice8 *iface,
3390 UINT stream_idx, IDirect3DVertexBuffer8 *buffer, UINT stride)
3392 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3393 struct d3d8_vertexbuffer *buffer_impl = unsafe_impl_from_IDirect3DVertexBuffer8(buffer);
3394 const struct wined3d_stateblock_state *state;
3395 struct wined3d_buffer *wined3d_buffer;
3396 HRESULT hr;
3398 TRACE("iface %p, stream_idx %u, buffer %p, stride %u.\n",
3399 iface, stream_idx, buffer, stride);
3401 if (stream_idx >= ARRAY_SIZE(state->streams))
3403 WARN("Stream index %u out of range.\n", stream_idx);
3404 return D3DERR_INVALIDCALL;
3407 wined3d_mutex_lock();
3409 if (!buffer_impl)
3411 wined3d_buffer = NULL;
3412 state = device->stateblock_state;
3413 stride = state->streams[stream_idx].stride;
3415 else if (buffer_impl->draw_buffer)
3416 wined3d_buffer = buffer_impl->draw_buffer;
3417 else
3418 wined3d_buffer = buffer_impl->wined3d_buffer;
3420 hr = wined3d_stateblock_set_stream_source(device->update_state, stream_idx, wined3d_buffer, 0, stride);
3421 if (SUCCEEDED(hr) && !device->recording)
3423 if (buffer_impl && buffer_impl->draw_buffer)
3424 device->sysmem_vb |= (1u << stream_idx);
3425 else
3426 device->sysmem_vb &= ~(1u << stream_idx);
3429 wined3d_mutex_unlock();
3431 return hr;
3434 static HRESULT WINAPI d3d8_device_GetStreamSource(IDirect3DDevice8 *iface,
3435 UINT stream_idx, IDirect3DVertexBuffer8 **buffer, UINT *stride)
3437 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3438 const struct wined3d_stateblock_state *state;
3439 const struct wined3d_stream_state *stream;
3440 struct d3d8_vertexbuffer *buffer_impl;
3442 TRACE("iface %p, stream_idx %u, buffer %p, stride %p.\n",
3443 iface, stream_idx, buffer, stride);
3445 if (!buffer)
3446 return D3DERR_INVALIDCALL;
3448 if (stream_idx >= ARRAY_SIZE(state->streams))
3450 WARN("Stream index %u out of range.\n", stream_idx);
3451 return D3DERR_INVALIDCALL;
3454 wined3d_mutex_lock();
3455 state = device->stateblock_state;
3456 stream = &state->streams[stream_idx];
3457 if (stream->buffer)
3459 buffer_impl = wined3d_buffer_get_parent(stream->buffer);
3460 *buffer = &buffer_impl->IDirect3DVertexBuffer8_iface;
3461 IDirect3DVertexBuffer8_AddRef(*buffer);
3463 else
3464 *buffer = NULL;
3465 *stride = stream->stride;
3466 wined3d_mutex_unlock();
3468 return D3D_OK;
3471 static const struct IDirect3DDevice8Vtbl d3d8_device_vtbl =
3473 d3d8_device_QueryInterface,
3474 d3d8_device_AddRef,
3475 d3d8_device_Release,
3476 d3d8_device_TestCooperativeLevel,
3477 d3d8_device_GetAvailableTextureMem,
3478 d3d8_device_ResourceManagerDiscardBytes,
3479 d3d8_device_GetDirect3D,
3480 d3d8_device_GetDeviceCaps,
3481 d3d8_device_GetDisplayMode,
3482 d3d8_device_GetCreationParameters,
3483 d3d8_device_SetCursorProperties,
3484 d3d8_device_SetCursorPosition,
3485 d3d8_device_ShowCursor,
3486 d3d8_device_CreateAdditionalSwapChain,
3487 d3d8_device_Reset,
3488 d3d8_device_Present,
3489 d3d8_device_GetBackBuffer,
3490 d3d8_device_GetRasterStatus,
3491 d3d8_device_SetGammaRamp,
3492 d3d8_device_GetGammaRamp,
3493 d3d8_device_CreateTexture,
3494 d3d8_device_CreateVolumeTexture,
3495 d3d8_device_CreateCubeTexture,
3496 d3d8_device_CreateVertexBuffer,
3497 d3d8_device_CreateIndexBuffer,
3498 d3d8_device_CreateRenderTarget,
3499 d3d8_device_CreateDepthStencilSurface,
3500 d3d8_device_CreateImageSurface,
3501 d3d8_device_CopyRects,
3502 d3d8_device_UpdateTexture,
3503 d3d8_device_GetFrontBuffer,
3504 d3d8_device_SetRenderTarget,
3505 d3d8_device_GetRenderTarget,
3506 d3d8_device_GetDepthStencilSurface,
3507 d3d8_device_BeginScene,
3508 d3d8_device_EndScene,
3509 d3d8_device_Clear,
3510 d3d8_device_SetTransform,
3511 d3d8_device_GetTransform,
3512 d3d8_device_MultiplyTransform,
3513 d3d8_device_SetViewport,
3514 d3d8_device_GetViewport,
3515 d3d8_device_SetMaterial,
3516 d3d8_device_GetMaterial,
3517 d3d8_device_SetLight,
3518 d3d8_device_GetLight,
3519 d3d8_device_LightEnable,
3520 d3d8_device_GetLightEnable,
3521 d3d8_device_SetClipPlane,
3522 d3d8_device_GetClipPlane,
3523 d3d8_device_SetRenderState,
3524 d3d8_device_GetRenderState,
3525 d3d8_device_BeginStateBlock,
3526 d3d8_device_EndStateBlock,
3527 d3d8_device_ApplyStateBlock,
3528 d3d8_device_CaptureStateBlock,
3529 d3d8_device_DeleteStateBlock,
3530 d3d8_device_CreateStateBlock,
3531 d3d8_device_SetClipStatus,
3532 d3d8_device_GetClipStatus,
3533 d3d8_device_GetTexture,
3534 d3d8_device_SetTexture,
3535 d3d8_device_GetTextureStageState,
3536 d3d8_device_SetTextureStageState,
3537 d3d8_device_ValidateDevice,
3538 d3d8_device_GetInfo,
3539 d3d8_device_SetPaletteEntries,
3540 d3d8_device_GetPaletteEntries,
3541 d3d8_device_SetCurrentTexturePalette,
3542 d3d8_device_GetCurrentTexturePalette,
3543 d3d8_device_DrawPrimitive,
3544 d3d8_device_DrawIndexedPrimitive,
3545 d3d8_device_DrawPrimitiveUP,
3546 d3d8_device_DrawIndexedPrimitiveUP,
3547 d3d8_device_ProcessVertices,
3548 d3d8_device_CreateVertexShader,
3549 d3d8_device_SetVertexShader,
3550 d3d8_device_GetVertexShader,
3551 d3d8_device_DeleteVertexShader,
3552 d3d8_device_SetVertexShaderConstant,
3553 d3d8_device_GetVertexShaderConstant,
3554 d3d8_device_GetVertexShaderDeclaration,
3555 d3d8_device_GetVertexShaderFunction,
3556 d3d8_device_SetStreamSource,
3557 d3d8_device_GetStreamSource,
3558 d3d8_device_SetIndices,
3559 d3d8_device_GetIndices,
3560 d3d8_device_CreatePixelShader,
3561 d3d8_device_SetPixelShader,
3562 d3d8_device_GetPixelShader,
3563 d3d8_device_DeletePixelShader,
3564 d3d8_device_SetPixelShaderConstant,
3565 d3d8_device_GetPixelShaderConstant,
3566 d3d8_device_GetPixelShaderFunction,
3567 d3d8_device_DrawRectPatch,
3568 d3d8_device_DrawTriPatch,
3569 d3d8_device_DeletePatch,
3572 static inline struct d3d8_device *device_from_device_parent(struct wined3d_device_parent *device_parent)
3574 return CONTAINING_RECORD(device_parent, struct d3d8_device, device_parent);
3577 static void CDECL device_parent_wined3d_device_created(struct wined3d_device_parent *device_parent,
3578 struct wined3d_device *device)
3580 TRACE("device_parent %p, device %p\n", device_parent, device);
3583 static void CDECL device_parent_mode_changed(struct wined3d_device_parent *device_parent)
3585 TRACE("device_parent %p.\n", device_parent);
3588 static void CDECL device_parent_activate(struct wined3d_device_parent *device_parent, BOOL activate)
3590 struct d3d8_device *device = device_from_device_parent(device_parent);
3592 TRACE("device_parent %p, activate %#x.\n", device_parent, activate);
3594 if (!activate)
3595 InterlockedCompareExchange(&device->device_state, D3D8_DEVICE_STATE_LOST, D3D8_DEVICE_STATE_OK);
3596 else
3597 InterlockedCompareExchange(&device->device_state, D3D8_DEVICE_STATE_NOT_RESET, D3D8_DEVICE_STATE_LOST);
3600 static HRESULT CDECL device_parent_texture_sub_resource_created(struct wined3d_device_parent *device_parent,
3601 enum wined3d_resource_type type, struct wined3d_texture *wined3d_texture, unsigned int sub_resource_idx,
3602 void **parent, const struct wined3d_parent_ops **parent_ops)
3604 TRACE("device_parent %p, type %#x, wined3d_texture %p, sub_resource_idx %u, parent %p, parent_ops %p.\n",
3605 device_parent, type, wined3d_texture, sub_resource_idx, parent, parent_ops);
3607 if (type == WINED3D_RTYPE_TEXTURE_3D)
3609 struct d3d8_volume *d3d_volume;
3611 if (!(d3d_volume = heap_alloc_zero(sizeof(*d3d_volume))))
3612 return E_OUTOFMEMORY;
3614 volume_init(d3d_volume, wined3d_texture, sub_resource_idx, parent_ops);
3615 *parent = d3d_volume;
3616 TRACE("Created volume %p.\n", d3d_volume);
3618 else if (type != WINED3D_RTYPE_TEXTURE_2D)
3620 ERR("Unhandled resource type %#x.\n", type);
3621 return E_FAIL;
3624 return D3D_OK;
3627 static HRESULT CDECL device_parent_create_swapchain_texture(struct wined3d_device_parent *device_parent,
3628 void *container_parent, const struct wined3d_resource_desc *desc, DWORD texture_flags,
3629 struct wined3d_texture **texture)
3631 struct d3d8_device *device = device_from_device_parent(device_parent);
3632 HRESULT hr;
3634 TRACE("device_parent %p, container_parent %p, desc %p, texture flags %#lx, texture %p.\n",
3635 device_parent, container_parent, desc, texture_flags, texture);
3637 if (FAILED(hr = wined3d_texture_create(device->wined3d_device, desc, 1, 1, texture_flags,
3638 NULL, NULL, &d3d8_null_wined3d_parent_ops, texture)))
3640 WARN("Failed to create texture, hr %#lx.\n", hr);
3641 return hr;
3644 return hr;
3647 static const struct wined3d_device_parent_ops d3d8_wined3d_device_parent_ops =
3649 device_parent_wined3d_device_created,
3650 device_parent_mode_changed,
3651 device_parent_activate,
3652 device_parent_texture_sub_resource_created,
3653 device_parent_create_swapchain_texture,
3656 static void setup_fpu(void)
3658 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
3659 WORD cw;
3660 __asm__ volatile ("fnstcw %0" : "=m" (cw));
3661 cw = (cw & ~0xf3f) | 0x3f;
3662 __asm__ volatile ("fldcw %0" : : "m" (cw));
3663 #elif defined(__i386__) && defined(_MSC_VER)
3664 WORD cw;
3665 __asm fnstcw cw;
3666 cw = (cw & ~0xf3f) | 0x3f;
3667 __asm fldcw cw;
3668 #else
3669 FIXME("FPU setup not implemented for this platform.\n");
3670 #endif
3673 HRESULT device_init(struct d3d8_device *device, struct d3d8 *parent, struct wined3d *wined3d, UINT adapter,
3674 D3DDEVTYPE device_type, HWND focus_window, DWORD flags, D3DPRESENT_PARAMETERS *parameters)
3676 struct wined3d_swapchain_desc swapchain_desc;
3677 struct wined3d_swapchain *wined3d_swapchain;
3678 struct wined3d_adapter *wined3d_adapter;
3679 struct wined3d_output *wined3d_output;
3680 struct d3d8_swapchain *d3d_swapchain;
3681 struct wined3d_caps caps;
3682 unsigned int output_idx;
3683 HRESULT hr;
3685 static const enum wined3d_feature_level feature_levels[] =
3687 WINED3D_FEATURE_LEVEL_8,
3688 WINED3D_FEATURE_LEVEL_7,
3689 WINED3D_FEATURE_LEVEL_6,
3690 WINED3D_FEATURE_LEVEL_5,
3693 output_idx = adapter;
3694 if (output_idx >= parent->wined3d_output_count)
3695 return D3DERR_INVALIDCALL;
3697 device->IDirect3DDevice8_iface.lpVtbl = &d3d8_device_vtbl;
3698 device->device_parent.ops = &d3d8_wined3d_device_parent_ops;
3699 device->adapter_ordinal = adapter;
3700 device->ref = 1;
3701 if (!(device->handle_table.entries = heap_alloc_zero(D3D8_INITIAL_HANDLE_TABLE_SIZE
3702 * sizeof(*device->handle_table.entries))))
3704 ERR("Failed to allocate handle table memory.\n");
3705 return E_OUTOFMEMORY;
3707 device->handle_table.table_size = D3D8_INITIAL_HANDLE_TABLE_SIZE;
3709 if (!(flags & D3DCREATE_FPU_PRESERVE)) setup_fpu();
3711 wined3d_mutex_lock();
3712 wined3d_output = parent->wined3d_outputs[output_idx];
3713 wined3d_adapter = wined3d_output_get_adapter(wined3d_output);
3714 if (FAILED(hr = wined3d_device_create(wined3d, wined3d_adapter, wined3d_device_type_from_d3d(device_type),
3715 focus_window, flags, 4, feature_levels, ARRAY_SIZE(feature_levels),
3716 &device->device_parent, &device->wined3d_device)))
3718 WARN("Failed to create wined3d device, hr %#lx.\n", hr);
3719 wined3d_mutex_unlock();
3720 heap_free(device->handle_table.entries);
3721 return hr;
3724 device->immediate_context = wined3d_device_get_immediate_context(device->wined3d_device);
3725 wined3d_get_device_caps(wined3d_adapter, wined3d_device_type_from_d3d(device_type), &caps);
3726 device->max_user_clip_planes = caps.MaxUserClipPlanes;
3727 device->vs_uniform_count = caps.MaxVertexShaderConst;
3729 if (FAILED(hr = wined3d_stateblock_create(device->wined3d_device, NULL, WINED3D_SBT_PRIMARY, &device->state)))
3731 ERR("Failed to create primary stateblock, hr %#lx.\n", hr);
3732 wined3d_device_decref(device->wined3d_device);
3733 wined3d_mutex_unlock();
3734 return hr;
3736 device->stateblock_state = wined3d_stateblock_get_state(device->state);
3737 device->update_state = device->state;
3739 wined3d_streaming_buffer_init(&device->vertex_buffer, WINED3D_BIND_VERTEX_BUFFER);
3740 wined3d_streaming_buffer_init(&device->index_buffer, WINED3D_BIND_INDEX_BUFFER);
3742 if (!parameters->Windowed)
3744 if (!focus_window)
3745 focus_window = parameters->hDeviceWindow;
3746 if (FAILED(hr = wined3d_device_acquire_focus_window(device->wined3d_device, focus_window)))
3748 ERR("Failed to acquire focus window, hr %#lx.\n", hr);
3749 wined3d_device_decref(device->wined3d_device);
3750 wined3d_mutex_unlock();
3751 heap_free(device->handle_table.entries);
3752 return hr;
3756 if (flags & D3DCREATE_MULTITHREADED)
3757 wined3d_device_set_multithreaded(device->wined3d_device);
3759 if (!wined3d_swapchain_desc_from_d3d8(&swapchain_desc, wined3d_output, parameters))
3761 wined3d_device_release_focus_window(device->wined3d_device);
3762 wined3d_device_decref(device->wined3d_device);
3763 wined3d_mutex_unlock();
3764 heap_free(device->handle_table.entries);
3765 return D3DERR_INVALIDCALL;
3767 swapchain_desc.flags |= WINED3D_SWAPCHAIN_IMPLICIT;
3769 if (FAILED(hr = d3d8_swapchain_create(device, &swapchain_desc,
3770 wined3dswapinterval_from_d3d(parameters->FullScreen_PresentationInterval), &d3d_swapchain)))
3772 WARN("Failed to create implicit swapchain, hr %#lx.\n", hr);
3773 wined3d_device_release_focus_window(device->wined3d_device);
3774 wined3d_device_decref(device->wined3d_device);
3775 wined3d_mutex_unlock();
3776 heap_free(device->handle_table.entries);
3777 return hr;
3780 wined3d_swapchain = d3d_swapchain->wined3d_swapchain;
3781 wined3d_swapchain_incref(wined3d_swapchain);
3782 IDirect3DSwapChain8_Release(&d3d_swapchain->IDirect3DSwapChain8_iface);
3784 wined3d_stateblock_set_render_state(device->state, WINED3D_RS_ZENABLE,
3785 !!swapchain_desc.enable_auto_depth_stencil);
3786 wined3d_stateblock_set_render_state(device->state, WINED3D_RS_POINTSIZE_MIN, 0);
3787 device_reset_viewport_state(device);
3788 wined3d_mutex_unlock();
3790 present_parameters_from_wined3d_swapchain_desc(parameters,
3791 &swapchain_desc, parameters->FullScreen_PresentationInterval);
3793 device->declArraySize = 16;
3794 if (!(device->decls = heap_alloc(device->declArraySize * sizeof(*device->decls))))
3796 ERR("Failed to allocate FVF vertex declaration map memory.\n");
3797 hr = E_OUTOFMEMORY;
3798 goto err;
3801 device->implicit_swapchain = wined3d_swapchain;
3803 device->d3d_parent = parent;
3804 IDirect3D8_AddRef(&parent->IDirect3D8_iface);
3806 return D3D_OK;
3808 err:
3809 wined3d_mutex_lock();
3810 wined3d_swapchain_decref(wined3d_swapchain);
3811 wined3d_device_release_focus_window(device->wined3d_device);
3812 wined3d_device_decref(device->wined3d_device);
3813 wined3d_mutex_unlock();
3814 heap_free(device->handle_table.entries);
3815 return hr;