d3d8: Create sub-resource surfaces manually.
[wine.git] / dlls / d3d8 / device.c
blob981bd76cee97307709ba550e3a12b7d34fa97f6e
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 surface->parent_device = &device->IDirect3DDevice8_iface;
996 device->device_state = D3D8_DEVICE_STATE_OK;
998 else
1000 device->device_state = D3D8_DEVICE_STATE_NOT_RESET;
1002 wined3d_mutex_unlock();
1004 return hr;
1007 static HRESULT WINAPI d3d8_device_Present(IDirect3DDevice8 *iface, const RECT *src_rect,
1008 const RECT *dst_rect, HWND dst_window_override, const RGNDATA *dirty_region)
1010 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1011 struct d3d8_swapchain *implicit_swapchain;
1013 TRACE("iface %p, src_rect %s, dst_rect %s, dst_window_override %p, dirty_region %p.\n",
1014 iface, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect), dst_window_override, dirty_region);
1016 /* Fraps does not hook IDirect3DDevice8::Present regardless of the hotpatch
1017 * attribute. It only hooks IDirect3DSwapChain8::Present. Yet it properly
1018 * shows a framerate on Windows in applications that only call the device
1019 * method, like e.g. the dx8 sdk samples. The conclusion is that native
1020 * calls the swapchain's public method from the device. */
1021 implicit_swapchain = wined3d_swapchain_get_parent(device->implicit_swapchain);
1022 return IDirect3DSwapChain8_Present(&implicit_swapchain->IDirect3DSwapChain8_iface,
1023 src_rect, dst_rect, dst_window_override, dirty_region);
1026 static HRESULT WINAPI d3d8_device_GetBackBuffer(IDirect3DDevice8 *iface,
1027 UINT backbuffer_idx, D3DBACKBUFFER_TYPE backbuffer_type, IDirect3DSurface8 **backbuffer)
1029 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1030 struct wined3d_texture *wined3d_texture;
1031 struct d3d8_surface *surface_impl;
1033 TRACE("iface %p, backbuffer_idx %u, backbuffer_type %#x, backbuffer %p.\n",
1034 iface, backbuffer_idx, backbuffer_type, backbuffer);
1036 /* backbuffer_type is ignored by native. */
1038 /* No need to check for backbuffer == NULL, Windows crashes in that case. */
1039 wined3d_mutex_lock();
1041 if (!(wined3d_texture = wined3d_swapchain_get_back_buffer(device->implicit_swapchain, backbuffer_idx)))
1043 wined3d_mutex_unlock();
1044 *backbuffer = NULL;
1045 return D3DERR_INVALIDCALL;
1048 surface_impl = wined3d_texture_get_sub_resource_parent(wined3d_texture, 0);
1049 *backbuffer = &surface_impl->IDirect3DSurface8_iface;
1050 IDirect3DSurface8_AddRef(*backbuffer);
1052 wined3d_mutex_unlock();
1053 return D3D_OK;
1056 static HRESULT WINAPI d3d8_device_GetRasterStatus(IDirect3DDevice8 *iface, D3DRASTER_STATUS *raster_status)
1058 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1059 HRESULT hr;
1061 TRACE("iface %p, raster_status %p.\n", iface, raster_status);
1063 wined3d_mutex_lock();
1064 hr = wined3d_device_get_raster_status(device->wined3d_device, 0, (struct wined3d_raster_status *)raster_status);
1065 wined3d_mutex_unlock();
1067 return hr;
1070 static void WINAPI d3d8_device_SetGammaRamp(IDirect3DDevice8 *iface, DWORD flags, const D3DGAMMARAMP *ramp)
1072 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1074 TRACE("iface %p, flags %#lx, ramp %p.\n", iface, flags, ramp);
1076 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
1077 wined3d_mutex_lock();
1078 wined3d_device_set_gamma_ramp(device->wined3d_device, 0, flags, (const struct wined3d_gamma_ramp *)ramp);
1079 wined3d_mutex_unlock();
1082 static void WINAPI d3d8_device_GetGammaRamp(IDirect3DDevice8 *iface, D3DGAMMARAMP *ramp)
1084 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1086 TRACE("iface %p, ramp %p.\n", iface, ramp);
1088 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
1089 wined3d_mutex_lock();
1090 wined3d_device_get_gamma_ramp(device->wined3d_device, 0, (struct wined3d_gamma_ramp *)ramp);
1091 wined3d_mutex_unlock();
1094 static HRESULT WINAPI d3d8_device_CreateTexture(IDirect3DDevice8 *iface,
1095 UINT width, UINT height, UINT levels, DWORD usage, D3DFORMAT format,
1096 D3DPOOL pool, IDirect3DTexture8 **texture)
1098 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1099 struct d3d8_texture *object;
1100 unsigned int i;
1101 HRESULT hr;
1103 TRACE("iface %p, width %u, height %u, levels %u, usage %#lx, format %#x, pool %#x, texture %p.\n",
1104 iface, width, height, levels, usage, format, pool, texture);
1106 if (!format)
1107 return D3DERR_INVALIDCALL;
1109 *texture = NULL;
1110 if (!(object = heap_alloc_zero(sizeof(*object))))
1111 return D3DERR_OUTOFVIDEOMEMORY;
1113 hr = d3d8_texture_2d_init(object, device, width, height, levels, usage, format, pool);
1114 if (FAILED(hr))
1116 WARN("Failed to initialize texture, hr %#lx.\n", hr);
1117 heap_free(object);
1118 return hr;
1121 levels = wined3d_texture_get_level_count(object->wined3d_texture);
1122 for (i = 0; i < levels; ++i)
1124 if (!d3d8_surface_create(object->wined3d_texture, i))
1126 IDirect3DTexture8_Release(&object->IDirect3DBaseTexture8_iface);
1127 return E_OUTOFMEMORY;
1131 TRACE("Created texture %p.\n", object);
1132 *texture = (IDirect3DTexture8 *)&object->IDirect3DBaseTexture8_iface;
1134 return D3D_OK;
1137 static HRESULT WINAPI d3d8_device_CreateVolumeTexture(IDirect3DDevice8 *iface,
1138 UINT width, UINT height, UINT depth, UINT levels, DWORD usage, D3DFORMAT format,
1139 D3DPOOL pool, IDirect3DVolumeTexture8 **texture)
1141 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1142 struct d3d8_texture *object;
1143 HRESULT hr;
1145 TRACE("iface %p, width %u, height %u, depth %u, levels %u, usage %#lx, format %#x, pool %#x, texture %p.\n",
1146 iface, width, height, depth, levels, usage, format, pool, texture);
1148 if (!format)
1149 return D3DERR_INVALIDCALL;
1151 *texture = NULL;
1152 if (!(object = heap_alloc_zero(sizeof(*object))))
1153 return D3DERR_OUTOFVIDEOMEMORY;
1155 hr = d3d8_texture_3d_init(object, device, width, height, depth, levels, usage, format, pool);
1156 if (FAILED(hr))
1158 WARN("Failed to initialize volume texture, hr %#lx.\n", hr);
1159 heap_free(object);
1160 return hr;
1163 TRACE("Created volume texture %p.\n", object);
1164 *texture = (IDirect3DVolumeTexture8 *)&object->IDirect3DBaseTexture8_iface;
1166 return D3D_OK;
1169 static HRESULT WINAPI d3d8_device_CreateCubeTexture(IDirect3DDevice8 *iface, UINT edge_length,
1170 UINT levels, DWORD usage, D3DFORMAT format, D3DPOOL pool, IDirect3DCubeTexture8 **texture)
1172 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1173 struct d3d8_texture *object;
1174 unsigned int i;
1175 HRESULT hr;
1177 TRACE("iface %p, edge_length %u, levels %u, usage %#lx, format %#x, pool %#x, texture %p.\n",
1178 iface, edge_length, levels, usage, format, pool, texture);
1180 if (!format)
1181 return D3DERR_INVALIDCALL;
1183 *texture = NULL;
1184 if (!(object = heap_alloc_zero(sizeof(*object))))
1185 return D3DERR_OUTOFVIDEOMEMORY;
1187 hr = d3d8_texture_cube_init(object, device, edge_length, levels, usage, format, pool);
1188 if (FAILED(hr))
1190 WARN("Failed to initialize cube texture, hr %#lx.\n", hr);
1191 heap_free(object);
1192 return hr;
1195 levels = wined3d_texture_get_level_count(object->wined3d_texture);
1196 for (i = 0; i < levels * 6; ++i)
1198 if (!d3d8_surface_create(object->wined3d_texture, i))
1200 IDirect3DTexture8_Release(&object->IDirect3DBaseTexture8_iface);
1201 return E_OUTOFMEMORY;
1205 TRACE("Created cube texture %p.\n", object);
1206 *texture = (IDirect3DCubeTexture8 *)&object->IDirect3DBaseTexture8_iface;
1208 return hr;
1211 static HRESULT WINAPI d3d8_device_CreateVertexBuffer(IDirect3DDevice8 *iface, UINT size,
1212 DWORD usage, DWORD fvf, D3DPOOL pool, IDirect3DVertexBuffer8 **buffer)
1214 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1215 struct d3d8_vertexbuffer *object;
1216 HRESULT hr;
1218 TRACE("iface %p, size %u, usage %#lx, fvf %#lx, pool %#x, buffer %p.\n",
1219 iface, size, usage, fvf, pool, buffer);
1221 if (!(object = heap_alloc_zero(sizeof(*object))))
1222 return D3DERR_OUTOFVIDEOMEMORY;
1224 hr = vertexbuffer_init(object, device, size, usage, fvf, pool);
1225 if (FAILED(hr))
1227 WARN("Failed to initialize vertex buffer, hr %#lx.\n", hr);
1228 heap_free(object);
1229 return hr;
1232 TRACE("Created vertex buffer %p.\n", object);
1233 *buffer = &object->IDirect3DVertexBuffer8_iface;
1235 return D3D_OK;
1238 static HRESULT WINAPI d3d8_device_CreateIndexBuffer(IDirect3DDevice8 *iface, UINT size,
1239 DWORD usage, D3DFORMAT format, D3DPOOL pool, IDirect3DIndexBuffer8 **buffer)
1241 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1242 struct d3d8_indexbuffer *object;
1243 HRESULT hr;
1245 TRACE("iface %p, size %u, usage %#lx, format %#x, pool %#x, buffer %p.\n",
1246 iface, size, usage, format, pool, buffer);
1248 if (!(object = heap_alloc_zero(sizeof(*object))))
1249 return D3DERR_OUTOFVIDEOMEMORY;
1251 hr = indexbuffer_init(object, device, size, usage, format, pool);
1252 if (FAILED(hr))
1254 WARN("Failed to initialize index buffer, hr %#lx.\n", hr);
1255 heap_free(object);
1256 return hr;
1259 TRACE("Created index buffer %p.\n", object);
1260 *buffer = &object->IDirect3DIndexBuffer8_iface;
1262 return D3D_OK;
1265 static HRESULT d3d8_device_create_surface(struct d3d8_device *device, enum wined3d_format_id format,
1266 enum wined3d_multisample_type multisample_type, unsigned int bind_flags, unsigned int access,
1267 unsigned int width, unsigned int height, IDirect3DSurface8 **surface)
1269 struct wined3d_resource_desc desc;
1270 struct d3d8_surface *surface_impl;
1271 struct wined3d_texture *texture;
1272 HRESULT hr;
1274 TRACE("device %p, format %#x, multisample_type %#x, bind_flags %#x, "
1275 "access %#x, width %u, height %u, surface %p.\n",
1276 device, format, multisample_type, bind_flags, access, width, height, surface);
1278 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
1279 desc.format = format;
1280 desc.multisample_type = multisample_type;
1281 desc.multisample_quality = 0;
1282 desc.usage = 0;
1283 desc.bind_flags = bind_flags;
1284 desc.access = access;
1285 desc.width = width;
1286 desc.height = height;
1287 desc.depth = 1;
1288 desc.size = 0;
1290 wined3d_mutex_lock();
1292 if (FAILED(hr = wined3d_texture_create(device->wined3d_device, &desc,
1293 1, 1, 0, NULL, NULL, &d3d8_null_wined3d_parent_ops, &texture)))
1295 wined3d_mutex_unlock();
1296 WARN("Failed to create texture, hr %#lx.\n", hr);
1297 return hr;
1300 if (!(surface_impl = d3d8_surface_create(texture, 0)))
1302 wined3d_texture_decref(texture);
1303 wined3d_mutex_unlock();
1304 return E_OUTOFMEMORY;
1306 surface_impl->parent_device = &device->IDirect3DDevice8_iface;
1307 *surface = &surface_impl->IDirect3DSurface8_iface;
1308 IDirect3DSurface8_AddRef(*surface);
1309 wined3d_texture_decref(texture);
1311 wined3d_mutex_unlock();
1313 return D3D_OK;
1316 static HRESULT WINAPI d3d8_device_CreateRenderTarget(IDirect3DDevice8 *iface, UINT width,
1317 UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, BOOL lockable,
1318 IDirect3DSurface8 **surface)
1320 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1321 unsigned int access = WINED3D_RESOURCE_ACCESS_GPU;
1323 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, lockable %#x, surface %p.\n",
1324 iface, width, height, format, multisample_type, lockable, surface);
1326 if (!format)
1327 return D3DERR_INVALIDCALL;
1329 *surface = NULL;
1330 if (lockable)
1331 access |= WINED3D_RESOURCE_ACCESS_MAP_R | WINED3D_RESOURCE_ACCESS_MAP_W;
1333 return d3d8_device_create_surface(device, wined3dformat_from_d3dformat(format),
1334 wined3d_multisample_type_from_d3d(multisample_type),
1335 WINED3D_BIND_RENDER_TARGET, access, width, height, surface);
1338 static HRESULT WINAPI d3d8_device_CreateDepthStencilSurface(IDirect3DDevice8 *iface,
1339 UINT width, UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type,
1340 IDirect3DSurface8 **surface)
1342 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1344 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, surface %p.\n",
1345 iface, width, height, format, multisample_type, surface);
1347 if (!format)
1348 return D3DERR_INVALIDCALL;
1350 *surface = NULL;
1352 return d3d8_device_create_surface(device, wined3dformat_from_d3dformat(format),
1353 wined3d_multisample_type_from_d3d(multisample_type), WINED3D_BIND_DEPTH_STENCIL,
1354 WINED3D_RESOURCE_ACCESS_GPU, width, height, surface);
1357 /* IDirect3DDevice8Impl::CreateImageSurface returns surface with pool type SYSTEMMEM */
1358 static HRESULT WINAPI d3d8_device_CreateImageSurface(IDirect3DDevice8 *iface, UINT width,
1359 UINT height, D3DFORMAT format, IDirect3DSurface8 **surface)
1361 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1362 unsigned int access = WINED3D_RESOURCE_ACCESS_CPU
1363 | WINED3D_RESOURCE_ACCESS_MAP_R | WINED3D_RESOURCE_ACCESS_MAP_W;
1365 TRACE("iface %p, width %u, height %u, format %#x, surface %p.\n",
1366 iface, width, height, format, surface);
1368 *surface = NULL;
1370 return d3d8_device_create_surface(device, wined3dformat_from_d3dformat(format),
1371 WINED3D_MULTISAMPLE_NONE, 0, access, width, height, surface);
1374 static HRESULT WINAPI d3d8_device_CopyRects(IDirect3DDevice8 *iface,
1375 IDirect3DSurface8 *src_surface, const RECT *src_rects, UINT rect_count,
1376 IDirect3DSurface8 *dst_surface, const POINT *dst_points)
1378 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1379 struct d3d8_surface *src = unsafe_impl_from_IDirect3DSurface8(src_surface);
1380 struct d3d8_surface *dst = unsafe_impl_from_IDirect3DSurface8(dst_surface);
1381 enum wined3d_format_id src_format, dst_format;
1382 struct wined3d_sub_resource_desc wined3d_desc;
1383 UINT src_w, src_h;
1385 TRACE("iface %p, src_surface %p, src_rects %p, rect_count %u, dst_surface %p, dst_points %p.\n",
1386 iface, src_surface, src_rects, rect_count, dst_surface, dst_points);
1388 wined3d_mutex_lock();
1389 wined3d_texture_get_sub_resource_desc(src->wined3d_texture, src->sub_resource_idx, &wined3d_desc);
1390 if (wined3d_desc.bind_flags & WINED3D_BIND_DEPTH_STENCIL)
1392 WARN("Source %p is a depth stencil surface, returning D3DERR_INVALIDCALL.\n", src_surface);
1393 wined3d_mutex_unlock();
1394 return D3DERR_INVALIDCALL;
1396 src_format = wined3d_desc.format;
1397 src_w = wined3d_desc.width;
1398 src_h = wined3d_desc.height;
1400 wined3d_texture_get_sub_resource_desc(dst->wined3d_texture, dst->sub_resource_idx, &wined3d_desc);
1401 if (wined3d_desc.bind_flags & WINED3D_BIND_DEPTH_STENCIL)
1403 WARN("Destination %p is a depth stencil surface, returning D3DERR_INVALIDCALL.\n", dst_surface);
1404 wined3d_mutex_unlock();
1405 return D3DERR_INVALIDCALL;
1407 dst_format = wined3d_desc.format;
1409 /* Check that the source and destination formats match */
1410 if (src_format != dst_format)
1412 WARN("Source %p format must match the destination %p format, returning D3DERR_INVALIDCALL.\n",
1413 src_surface, dst_surface);
1414 wined3d_mutex_unlock();
1415 return D3DERR_INVALIDCALL;
1418 /* Quick if complete copy ... */
1419 if (!rect_count && !src_rects && !dst_points)
1421 RECT rect = {0, 0, src_w, src_h};
1422 wined3d_device_context_blt(device->immediate_context, dst->wined3d_texture, dst->sub_resource_idx, &rect,
1423 src->wined3d_texture, src->sub_resource_idx, &rect, 0, NULL, WINED3D_TEXF_POINT);
1425 else
1427 unsigned int i;
1428 /* Copy rect by rect */
1429 if (src_rects && dst_points)
1431 for (i = 0; i < rect_count; ++i)
1433 UINT w = src_rects[i].right - src_rects[i].left;
1434 UINT h = src_rects[i].bottom - src_rects[i].top;
1435 RECT dst_rect = {dst_points[i].x, dst_points[i].y,
1436 dst_points[i].x + w, dst_points[i].y + h};
1438 wined3d_device_context_blt(device->immediate_context,
1439 dst->wined3d_texture, dst->sub_resource_idx, &dst_rect,
1440 src->wined3d_texture, src->sub_resource_idx, &src_rects[i], 0, NULL, WINED3D_TEXF_POINT);
1443 else
1445 for (i = 0; i < rect_count; ++i)
1447 UINT w = src_rects[i].right - src_rects[i].left;
1448 UINT h = src_rects[i].bottom - src_rects[i].top;
1449 RECT dst_rect = {0, 0, w, h};
1451 wined3d_device_context_blt(device->immediate_context,
1452 dst->wined3d_texture, dst->sub_resource_idx, &dst_rect,
1453 src->wined3d_texture, src->sub_resource_idx, &src_rects[i], 0, NULL, WINED3D_TEXF_POINT);
1457 wined3d_mutex_unlock();
1459 return WINED3D_OK;
1462 static HRESULT WINAPI d3d8_device_UpdateTexture(IDirect3DDevice8 *iface,
1463 IDirect3DBaseTexture8 *src_texture, IDirect3DBaseTexture8 *dst_texture)
1465 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1466 struct d3d8_texture *src_impl, *dst_impl;
1467 HRESULT hr;
1469 TRACE("iface %p, src_texture %p, dst_texture %p.\n", iface, src_texture, dst_texture);
1471 src_impl = unsafe_impl_from_IDirect3DBaseTexture8(src_texture);
1472 dst_impl = unsafe_impl_from_IDirect3DBaseTexture8(dst_texture);
1474 if (src_impl->draw_texture || dst_impl->draw_texture)
1476 WARN("Source or destination is managed; returning D3DERR_INVALIDCALL.\n");
1477 return D3DERR_INVALIDCALL;
1480 wined3d_mutex_lock();
1481 hr = wined3d_device_update_texture(device->wined3d_device,
1482 src_impl->wined3d_texture, dst_impl->wined3d_texture);
1483 wined3d_mutex_unlock();
1485 return hr;
1488 static HRESULT WINAPI d3d8_device_GetFrontBuffer(IDirect3DDevice8 *iface, IDirect3DSurface8 *dst_surface)
1490 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1491 struct d3d8_surface *dst_impl = unsafe_impl_from_IDirect3DSurface8(dst_surface);
1492 HRESULT hr;
1494 TRACE("iface %p, dst_surface %p.\n", iface, dst_surface);
1496 if (!dst_surface)
1498 WARN("Invalid destination surface passed.\n");
1499 return D3DERR_INVALIDCALL;
1502 wined3d_mutex_lock();
1503 hr = wined3d_swapchain_get_front_buffer_data(device->implicit_swapchain,
1504 dst_impl->wined3d_texture, dst_impl->sub_resource_idx);
1505 wined3d_mutex_unlock();
1507 return hr;
1510 static HRESULT WINAPI d3d8_device_SetRenderTarget(IDirect3DDevice8 *iface,
1511 IDirect3DSurface8 *render_target, IDirect3DSurface8 *depth_stencil)
1513 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1514 struct d3d8_surface *rt_impl = unsafe_impl_from_IDirect3DSurface8(render_target);
1515 struct d3d8_surface *ds_impl = unsafe_impl_from_IDirect3DSurface8(depth_stencil);
1516 struct wined3d_rendertarget_view *original_dsv, *rtv;
1517 HRESULT hr = D3D_OK;
1519 TRACE("iface %p, render_target %p, depth_stencil %p.\n", iface, render_target, depth_stencil);
1521 if (rt_impl && d3d8_surface_get_device(rt_impl) != device)
1523 WARN("Render target surface does not match device.\n");
1524 return D3DERR_INVALIDCALL;
1527 wined3d_mutex_lock();
1529 if (ds_impl)
1531 struct wined3d_sub_resource_desc ds_desc, rt_desc;
1532 struct wined3d_rendertarget_view *original_rtv;
1533 struct d3d8_surface *original_surface;
1535 /* If no render target is passed in check the size against the current RT */
1536 if (!render_target)
1538 if (!(original_rtv = wined3d_device_context_get_rendertarget_view(device->immediate_context, 0)))
1540 wined3d_mutex_unlock();
1541 return D3DERR_NOTFOUND;
1543 original_surface = wined3d_rendertarget_view_get_sub_resource_parent(original_rtv);
1544 wined3d_texture_get_sub_resource_desc(original_surface->wined3d_texture,
1545 original_surface->sub_resource_idx, &rt_desc);
1547 else
1548 wined3d_texture_get_sub_resource_desc(rt_impl->wined3d_texture,
1549 rt_impl->sub_resource_idx, &rt_desc);
1551 wined3d_texture_get_sub_resource_desc(ds_impl->wined3d_texture, ds_impl->sub_resource_idx, &ds_desc);
1553 if (ds_desc.width < rt_desc.width || ds_desc.height < rt_desc.height)
1555 WARN("Depth stencil is smaller than the render target, returning D3DERR_INVALIDCALL\n");
1556 wined3d_mutex_unlock();
1557 return D3DERR_INVALIDCALL;
1559 if (ds_desc.multisample_type != rt_desc.multisample_type
1560 || ds_desc.multisample_quality != rt_desc.multisample_quality)
1562 WARN("Multisample settings do not match, returning D3DERR_INVALIDCALL\n");
1563 wined3d_mutex_unlock();
1564 return D3DERR_INVALIDCALL;
1568 original_dsv = wined3d_device_context_get_depth_stencil_view(device->immediate_context);
1569 rtv = ds_impl ? d3d8_surface_acquire_rendertarget_view(ds_impl) : NULL;
1570 hr = wined3d_device_context_set_depth_stencil_view(device->immediate_context, rtv);
1571 d3d8_surface_release_rendertarget_view(ds_impl, rtv);
1572 if (SUCCEEDED(hr))
1574 rtv = render_target ? d3d8_surface_acquire_rendertarget_view(rt_impl) : NULL;
1575 if (render_target)
1577 if (SUCCEEDED(hr = wined3d_device_context_set_rendertarget_views(device->immediate_context,
1578 0, 1, &rtv, TRUE)))
1579 device_reset_viewport_state(device);
1580 else
1581 wined3d_device_context_set_depth_stencil_view(device->immediate_context, original_dsv);
1583 d3d8_surface_release_rendertarget_view(rt_impl, rtv);
1586 wined3d_mutex_unlock();
1588 return hr;
1591 static HRESULT WINAPI d3d8_device_GetRenderTarget(IDirect3DDevice8 *iface, IDirect3DSurface8 **render_target)
1593 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1594 struct wined3d_rendertarget_view *wined3d_rtv;
1595 struct d3d8_surface *surface_impl;
1596 HRESULT hr;
1598 TRACE("iface %p, render_target %p.\n", iface, render_target);
1600 if (!render_target)
1601 return D3DERR_INVALIDCALL;
1603 wined3d_mutex_lock();
1604 if ((wined3d_rtv = wined3d_device_context_get_rendertarget_view(device->immediate_context, 0)))
1606 /* We want the sub resource parent here, since the view itself may be
1607 * internal to wined3d and may not have a parent. */
1608 surface_impl = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_rtv);
1609 *render_target = &surface_impl->IDirect3DSurface8_iface;
1610 IDirect3DSurface8_AddRef(*render_target);
1611 hr = D3D_OK;
1613 else
1615 ERR("Failed to get wined3d render target.\n");
1616 *render_target = NULL;
1617 hr = D3DERR_NOTFOUND;
1619 wined3d_mutex_unlock();
1621 return hr;
1624 static HRESULT WINAPI d3d8_device_GetDepthStencilSurface(IDirect3DDevice8 *iface, IDirect3DSurface8 **depth_stencil)
1626 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1627 struct wined3d_rendertarget_view *wined3d_dsv;
1628 struct d3d8_surface *surface_impl;
1629 HRESULT hr = D3D_OK;
1631 TRACE("iface %p, depth_stencil %p.\n", iface, depth_stencil);
1633 if (!depth_stencil)
1634 return D3DERR_INVALIDCALL;
1636 wined3d_mutex_lock();
1637 if ((wined3d_dsv = wined3d_device_context_get_depth_stencil_view(device->immediate_context)))
1639 /* We want the sub resource parent here, since the view itself may be
1640 * internal to wined3d and may not have a parent. */
1641 surface_impl = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_dsv);
1642 *depth_stencil = &surface_impl->IDirect3DSurface8_iface;
1643 IDirect3DSurface8_AddRef(*depth_stencil);
1645 else
1647 hr = D3DERR_NOTFOUND;
1648 *depth_stencil = NULL;
1650 wined3d_mutex_unlock();
1652 return hr;
1655 static HRESULT WINAPI d3d8_device_BeginScene(IDirect3DDevice8 *iface)
1657 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1658 HRESULT hr;
1660 TRACE("iface %p.\n", iface);
1662 wined3d_mutex_lock();
1663 hr = wined3d_device_begin_scene(device->wined3d_device);
1664 wined3d_mutex_unlock();
1666 return hr;
1669 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d8_device_EndScene(IDirect3DDevice8 *iface)
1671 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1672 HRESULT hr;
1674 TRACE("iface %p.\n", iface);
1676 wined3d_mutex_lock();
1677 hr = wined3d_device_end_scene(device->wined3d_device);
1678 wined3d_mutex_unlock();
1680 return hr;
1683 static HRESULT WINAPI d3d8_device_Clear(IDirect3DDevice8 *iface, DWORD rect_count,
1684 const D3DRECT *rects, DWORD flags, D3DCOLOR color, float z, DWORD stencil)
1686 const struct wined3d_color c =
1688 ((color >> 16) & 0xff) / 255.0f,
1689 ((color >> 8) & 0xff) / 255.0f,
1690 (color & 0xff) / 255.0f,
1691 ((color >> 24) & 0xff) / 255.0f,
1693 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1694 HRESULT hr;
1696 TRACE("iface %p, rect_count %lu, rects %p, flags %#lx, color 0x%08lx, z %.8e, stencil %lu.\n",
1697 iface, rect_count, rects, flags, color, z, stencil);
1699 if (rect_count && !rects)
1701 WARN("count %lu with NULL rects.\n", rect_count);
1702 rect_count = 0;
1705 wined3d_mutex_lock();
1706 wined3d_device_apply_stateblock(device->wined3d_device, device->state);
1707 hr = wined3d_device_clear(device->wined3d_device, rect_count, (const RECT *)rects, flags, &c, z, stencil);
1708 wined3d_mutex_unlock();
1710 return hr;
1713 static HRESULT WINAPI d3d8_device_SetTransform(IDirect3DDevice8 *iface,
1714 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
1716 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1718 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1720 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1721 wined3d_mutex_lock();
1722 wined3d_stateblock_set_transform(device->update_state,
1723 wined3d_transform_state_from_d3d(state), (const struct wined3d_matrix *)matrix);
1724 wined3d_mutex_unlock();
1726 return D3D_OK;
1729 static HRESULT WINAPI d3d8_device_GetTransform(IDirect3DDevice8 *iface,
1730 D3DTRANSFORMSTATETYPE state, D3DMATRIX *matrix)
1732 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1734 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1736 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1737 wined3d_mutex_lock();
1738 memcpy(matrix, &device->stateblock_state->transforms[state], sizeof(*matrix));
1739 wined3d_mutex_unlock();
1741 return D3D_OK;
1744 static HRESULT WINAPI d3d8_device_MultiplyTransform(IDirect3DDevice8 *iface,
1745 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
1747 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1749 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1751 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1752 wined3d_mutex_lock();
1753 wined3d_stateblock_multiply_transform(device->state,
1754 wined3d_transform_state_from_d3d(state), (const struct wined3d_matrix *)matrix);
1755 wined3d_mutex_unlock();
1757 return D3D_OK;
1760 static HRESULT WINAPI d3d8_device_SetViewport(IDirect3DDevice8 *iface, const D3DVIEWPORT8 *viewport)
1762 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1763 struct wined3d_sub_resource_desc rt_desc;
1764 struct wined3d_rendertarget_view *rtv;
1765 struct d3d8_surface *surface;
1766 struct wined3d_viewport vp;
1768 TRACE("iface %p, viewport %p.\n", iface, viewport);
1770 wined3d_mutex_lock();
1771 if (!(rtv = wined3d_device_context_get_rendertarget_view(device->immediate_context, 0)))
1773 wined3d_mutex_unlock();
1774 return D3DERR_NOTFOUND;
1776 surface = wined3d_rendertarget_view_get_sub_resource_parent(rtv);
1777 wined3d_texture_get_sub_resource_desc(surface->wined3d_texture, surface->sub_resource_idx, &rt_desc);
1779 if (viewport->X > rt_desc.width || viewport->Width > rt_desc.width - viewport->X
1780 || viewport->Y > rt_desc.height || viewport->Height > rt_desc.height - viewport->Y)
1782 WARN("Invalid viewport, returning D3DERR_INVALIDCALL.\n");
1783 wined3d_mutex_unlock();
1784 return D3DERR_INVALIDCALL;
1787 vp.x = viewport->X;
1788 vp.y = viewport->Y;
1789 vp.width = viewport->Width;
1790 vp.height = viewport->Height;
1791 vp.min_z = viewport->MinZ;
1792 vp.max_z = viewport->MaxZ;
1794 wined3d_stateblock_set_viewport(device->update_state, &vp);
1795 wined3d_mutex_unlock();
1797 return D3D_OK;
1800 static HRESULT WINAPI d3d8_device_GetViewport(IDirect3DDevice8 *iface, D3DVIEWPORT8 *viewport)
1802 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1803 struct wined3d_viewport wined3d_viewport;
1805 TRACE("iface %p, viewport %p.\n", iface, viewport);
1807 wined3d_mutex_lock();
1808 wined3d_viewport = device->stateblock_state->viewport;
1809 wined3d_mutex_unlock();
1811 viewport->X = wined3d_viewport.x;
1812 viewport->Y = wined3d_viewport.y;
1813 viewport->Width = wined3d_viewport.width;
1814 viewport->Height = wined3d_viewport.height;
1815 viewport->MinZ = wined3d_viewport.min_z;
1816 viewport->MaxZ = wined3d_viewport.max_z;
1818 return D3D_OK;
1821 static HRESULT WINAPI d3d8_device_SetMaterial(IDirect3DDevice8 *iface, const D3DMATERIAL8 *material)
1823 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1825 TRACE("iface %p, material %p.\n", iface, material);
1827 /* Note: D3DMATERIAL8 is compatible with struct wined3d_material. */
1828 wined3d_mutex_lock();
1829 wined3d_stateblock_set_material(device->update_state, (const struct wined3d_material *)material);
1830 wined3d_mutex_unlock();
1832 return D3D_OK;
1835 static HRESULT WINAPI d3d8_device_GetMaterial(IDirect3DDevice8 *iface, D3DMATERIAL8 *material)
1837 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1839 TRACE("iface %p, material %p.\n", iface, material);
1841 /* Note: D3DMATERIAL8 is compatible with struct wined3d_material. */
1842 wined3d_mutex_lock();
1843 memcpy(material, &device->stateblock_state->material, sizeof(*material));
1844 wined3d_mutex_unlock();
1846 return D3D_OK;
1849 static HRESULT WINAPI d3d8_device_SetLight(IDirect3DDevice8 *iface, DWORD index, const D3DLIGHT8 *light)
1851 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1852 HRESULT hr;
1854 TRACE("iface %p, index %lu, light %p.\n", iface, index, light);
1856 /* Note: D3DLIGHT8 is compatible with struct wined3d_light. */
1857 wined3d_mutex_lock();
1858 hr = wined3d_stateblock_set_light(device->update_state, index, (const struct wined3d_light *)light);
1859 wined3d_mutex_unlock();
1861 return hr;
1864 static HRESULT WINAPI d3d8_device_GetLight(IDirect3DDevice8 *iface, DWORD index, D3DLIGHT8 *light)
1866 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1867 BOOL enabled;
1868 HRESULT hr;
1870 TRACE("iface %p, index %lu, light %p.\n", iface, index, light);
1872 /* Note: D3DLIGHT8 is compatible with struct wined3d_light. */
1873 wined3d_mutex_lock();
1874 hr = wined3d_stateblock_get_light(device->state, index, (struct wined3d_light *)light, &enabled);
1875 wined3d_mutex_unlock();
1877 return hr;
1880 static HRESULT WINAPI d3d8_device_LightEnable(IDirect3DDevice8 *iface, DWORD index, BOOL enable)
1882 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1883 HRESULT hr;
1885 TRACE("iface %p, index %lu, enable %#x.\n", iface, index, enable);
1887 wined3d_mutex_lock();
1888 hr = wined3d_stateblock_set_light_enable(device->update_state, index, enable);
1889 wined3d_mutex_unlock();
1891 return hr;
1894 static HRESULT WINAPI d3d8_device_GetLightEnable(IDirect3DDevice8 *iface, DWORD index, BOOL *enabled)
1896 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1897 struct wined3d_light light;
1898 HRESULT hr;
1900 TRACE("iface %p, index %lu, enabled %p.\n", iface, index, enabled);
1902 wined3d_mutex_lock();
1903 hr = wined3d_stateblock_get_light(device->state, index, &light, enabled);
1904 wined3d_mutex_unlock();
1906 return hr;
1909 static HRESULT WINAPI d3d8_device_SetClipPlane(IDirect3DDevice8 *iface, DWORD index, const float *plane)
1911 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1912 HRESULT hr;
1914 TRACE("iface %p, index %lu, plane %p.\n", iface, index, plane);
1916 wined3d_mutex_lock();
1917 hr = wined3d_stateblock_set_clip_plane(device->update_state, index, (const struct wined3d_vec4 *)plane);
1918 wined3d_mutex_unlock();
1920 return hr;
1923 static HRESULT WINAPI d3d8_device_GetClipPlane(IDirect3DDevice8 *iface, DWORD index, float *plane)
1925 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1927 TRACE("iface %p, index %lu, plane %p.\n", iface, index, plane);
1929 if (index >= device->max_user_clip_planes)
1931 TRACE("Plane %lu requested, but only %u planes are supported.\n", index, device->max_user_clip_planes);
1932 return WINED3DERR_INVALIDCALL;
1935 wined3d_mutex_lock();
1936 memcpy(plane, &device->stateblock_state->clip_planes[index], sizeof(struct wined3d_vec4));
1937 wined3d_mutex_unlock();
1939 return D3D_OK;
1942 static void resolve_depth_buffer(struct d3d8_device *device)
1944 const struct wined3d_stateblock_state *state = device->stateblock_state;
1945 struct wined3d_rendertarget_view *wined3d_dsv;
1946 struct wined3d_resource *dst_resource;
1947 struct wined3d_texture *dst_texture;
1948 struct wined3d_resource_desc desc;
1949 struct d3d8_surface *d3d8_dsv;
1951 if (!(dst_texture = state->textures[0]))
1952 return;
1953 dst_resource = wined3d_texture_get_resource(dst_texture);
1954 wined3d_resource_get_desc(dst_resource, &desc);
1955 if (desc.format != WINED3DFMT_D24_UNORM_S8_UINT
1956 && desc.format != WINED3DFMT_X8D24_UNORM
1957 && desc.format != WINED3DFMT_DF16
1958 && desc.format != WINED3DFMT_DF24
1959 && desc.format != WINED3DFMT_INTZ)
1960 return;
1962 if (!(wined3d_dsv = wined3d_device_context_get_depth_stencil_view(device->immediate_context)))
1963 return;
1964 d3d8_dsv = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_dsv);
1966 wined3d_device_context_resolve_sub_resource(device->immediate_context, dst_resource, 0,
1967 wined3d_rendertarget_view_get_resource(wined3d_dsv), d3d8_dsv->sub_resource_idx, desc.format);
1970 static HRESULT WINAPI d3d8_device_SetRenderState(IDirect3DDevice8 *iface,
1971 D3DRENDERSTATETYPE state, DWORD value)
1973 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1975 TRACE("iface %p, state %#x, value %#lx.\n", iface, state, value);
1977 wined3d_mutex_lock();
1978 wined3d_stateblock_set_render_state(device->update_state, wined3d_render_state_from_d3d(state), value);
1979 if (state == D3DRS_POINTSIZE && value == D3D8_RESZ_CODE)
1980 resolve_depth_buffer(device);
1981 wined3d_mutex_unlock();
1983 return D3D_OK;
1986 static HRESULT WINAPI d3d8_device_GetRenderState(IDirect3DDevice8 *iface,
1987 D3DRENDERSTATETYPE state, DWORD *value)
1989 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1990 const struct wined3d_stateblock_state *device_state;
1992 TRACE("iface %p, state %#x, value %p.\n", iface, state, value);
1994 wined3d_mutex_lock();
1995 device_state = device->stateblock_state;
1996 switch (state)
1998 case D3DRS_ZBIAS:
1999 *value = device_state->rs[WINED3D_RS_DEPTHBIAS];
2000 break;
2002 default:
2003 *value = device_state->rs[state];
2005 wined3d_mutex_unlock();
2007 return D3D_OK;
2010 static HRESULT WINAPI d3d8_device_BeginStateBlock(IDirect3DDevice8 *iface)
2012 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2013 struct wined3d_stateblock *stateblock;
2014 HRESULT hr;
2016 TRACE("iface %p.\n", iface);
2018 wined3d_mutex_lock();
2019 if (device->recording)
2021 wined3d_mutex_unlock();
2022 WARN("Trying to begin a stateblock while recording, returning D3DERR_INVALIDCALL.\n");
2023 return D3DERR_INVALIDCALL;
2026 if (SUCCEEDED(hr = wined3d_stateblock_create(device->wined3d_device, NULL, WINED3D_SBT_RECORDED, &stateblock)))
2027 device->update_state = device->recording = stateblock;
2028 wined3d_mutex_unlock();
2030 return hr;
2033 static HRESULT WINAPI d3d8_device_EndStateBlock(IDirect3DDevice8 *iface, DWORD *token)
2035 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2036 struct wined3d_stateblock *stateblock;
2038 TRACE("iface %p, token %p.\n", iface, token);
2040 /* Tell wineD3D to endstateblock before anything else (in case we run out
2041 * of memory later and cause locking problems)
2043 wined3d_mutex_lock();
2044 if (!device->recording)
2046 wined3d_mutex_unlock();
2047 WARN("Trying to end a stateblock, but no stateblock is being recorded.\n");
2048 return D3DERR_INVALIDCALL;
2050 stateblock = device->recording;
2051 wined3d_stateblock_init_contained_states(stateblock);
2052 device->recording = NULL;
2053 device->update_state = device->state;
2055 *token = d3d8_allocate_handle(&device->handle_table, stateblock, D3D8_HANDLE_SB);
2056 wined3d_mutex_unlock();
2058 if (*token == D3D8_INVALID_HANDLE)
2060 ERR("Failed to create a handle\n");
2061 wined3d_mutex_lock();
2062 wined3d_stateblock_decref(stateblock);
2063 wined3d_mutex_unlock();
2064 return E_FAIL;
2066 ++*token;
2068 TRACE("Returning %#lx (%p).\n", *token, stateblock);
2070 return D3D_OK;
2073 static HRESULT WINAPI d3d8_device_ApplyStateBlock(IDirect3DDevice8 *iface, DWORD token)
2075 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2076 const struct wined3d_stateblock_state *state;
2077 struct d3d8_vertexbuffer *vertex_buffer;
2078 struct wined3d_stateblock *stateblock;
2079 struct d3d8_indexbuffer *index_buffer;
2080 struct wined3d_buffer *wined3d_buffer;
2081 unsigned int i;
2083 TRACE("iface %p, token %#lx.\n", iface, token);
2085 if (!token)
2086 return D3D_OK;
2088 wined3d_mutex_lock();
2089 if (device->recording)
2091 wined3d_mutex_unlock();
2092 WARN("Trying to apply stateblock while recording, returning D3DERR_INVALIDCALL.\n");
2093 return D3DERR_INVALIDCALL;
2095 stateblock = d3d8_get_object(&device->handle_table, token - 1, D3D8_HANDLE_SB);
2096 if (!stateblock)
2098 WARN("Invalid handle (%#lx) passed.\n", token);
2099 wined3d_mutex_unlock();
2100 return D3DERR_INVALIDCALL;
2102 wined3d_stateblock_apply(stateblock, device->state);
2103 device->sysmem_vb = 0;
2104 state = device->stateblock_state;
2105 for (i = 0; i < D3D8_MAX_STREAMS; ++i)
2107 wined3d_buffer = state->streams[i].buffer;
2108 if (!wined3d_buffer || !(vertex_buffer = wined3d_buffer_get_parent(wined3d_buffer)))
2109 continue;
2110 if (vertex_buffer->draw_buffer)
2111 device->sysmem_vb |= 1u << i;
2113 device->sysmem_ib = (wined3d_buffer = state->index_buffer)
2114 && (index_buffer = wined3d_buffer_get_parent(wined3d_buffer)) && index_buffer->sysmem;
2115 wined3d_mutex_unlock();
2117 return D3D_OK;
2120 static HRESULT WINAPI d3d8_device_CaptureStateBlock(IDirect3DDevice8 *iface, DWORD token)
2122 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2123 struct wined3d_stateblock *stateblock;
2125 TRACE("iface %p, token %#lx.\n", iface, token);
2127 wined3d_mutex_lock();
2128 if (device->recording)
2130 wined3d_mutex_unlock();
2131 WARN("Trying to capture stateblock while recording, returning D3DERR_INVALIDCALL.\n");
2132 return D3DERR_INVALIDCALL;
2134 stateblock = d3d8_get_object(&device->handle_table, token - 1, D3D8_HANDLE_SB);
2135 if (!stateblock)
2137 WARN("Invalid handle (%#lx) passed.\n", token);
2138 wined3d_mutex_unlock();
2139 return D3DERR_INVALIDCALL;
2141 wined3d_stateblock_capture(stateblock, device->state);
2142 wined3d_mutex_unlock();
2144 return D3D_OK;
2147 static HRESULT WINAPI d3d8_device_DeleteStateBlock(IDirect3DDevice8 *iface, DWORD token)
2149 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2150 struct wined3d_stateblock *stateblock;
2152 TRACE("iface %p, token %#lx.\n", iface, token);
2154 wined3d_mutex_lock();
2155 stateblock = d3d8_free_handle(&device->handle_table, token - 1, D3D8_HANDLE_SB);
2157 if (!stateblock)
2159 WARN("Invalid handle (%#lx) passed.\n", token);
2160 wined3d_mutex_unlock();
2161 return D3DERR_INVALIDCALL;
2164 if (wined3d_stateblock_decref(stateblock))
2166 ERR("Stateblock %p has references left, this shouldn't happen.\n", stateblock);
2168 wined3d_mutex_unlock();
2170 return D3D_OK;
2173 static HRESULT WINAPI d3d8_device_CreateStateBlock(IDirect3DDevice8 *iface,
2174 D3DSTATEBLOCKTYPE type, DWORD *handle)
2176 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2177 struct wined3d_stateblock *stateblock;
2178 HRESULT hr;
2180 TRACE("iface %p, type %#x, handle %p.\n", iface, type, handle);
2182 if (type != D3DSBT_ALL
2183 && type != D3DSBT_PIXELSTATE
2184 && type != D3DSBT_VERTEXSTATE)
2186 WARN("Unexpected stateblock type, returning D3DERR_INVALIDCALL\n");
2187 return D3DERR_INVALIDCALL;
2190 wined3d_mutex_lock();
2191 if (device->recording)
2193 wined3d_mutex_unlock();
2194 WARN("Trying to create a stateblock while recording, returning D3DERR_INVALIDCALL.\n");
2195 return D3DERR_INVALIDCALL;
2197 hr = wined3d_stateblock_create(device->wined3d_device, device->state, (enum wined3d_stateblock_type)type, &stateblock);
2198 if (FAILED(hr))
2200 wined3d_mutex_unlock();
2201 ERR("Failed to create the state block, hr %#lx.\n", hr);
2202 return hr;
2205 *handle = d3d8_allocate_handle(&device->handle_table, stateblock, D3D8_HANDLE_SB);
2206 wined3d_mutex_unlock();
2208 if (*handle == D3D8_INVALID_HANDLE)
2210 ERR("Failed to allocate a handle.\n");
2211 wined3d_mutex_lock();
2212 wined3d_stateblock_decref(stateblock);
2213 wined3d_mutex_unlock();
2214 return E_FAIL;
2216 ++*handle;
2218 TRACE("Returning %#lx (%p).\n", *handle, stateblock);
2220 return hr;
2223 static HRESULT WINAPI d3d8_device_SetClipStatus(IDirect3DDevice8 *iface, const D3DCLIPSTATUS8 *clip_status)
2225 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2226 HRESULT hr;
2228 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
2229 /* FIXME: Verify that D3DCLIPSTATUS8 ~= struct wined3d_clip_status. */
2231 wined3d_mutex_lock();
2232 hr = wined3d_device_set_clip_status(device->wined3d_device, (const struct wined3d_clip_status *)clip_status);
2233 wined3d_mutex_unlock();
2235 return hr;
2238 static HRESULT WINAPI d3d8_device_GetClipStatus(IDirect3DDevice8 *iface, D3DCLIPSTATUS8 *clip_status)
2240 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2241 HRESULT hr;
2243 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
2245 wined3d_mutex_lock();
2246 hr = wined3d_device_get_clip_status(device->wined3d_device, (struct wined3d_clip_status *)clip_status);
2247 wined3d_mutex_unlock();
2249 return hr;
2252 static HRESULT WINAPI d3d8_device_GetTexture(IDirect3DDevice8 *iface, DWORD stage, IDirect3DBaseTexture8 **texture)
2254 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2255 struct wined3d_texture *wined3d_texture;
2256 struct d3d8_texture *texture_impl;
2258 TRACE("iface %p, stage %lu, texture %p.\n", iface, stage, texture);
2260 if (!texture)
2261 return D3DERR_INVALIDCALL;
2263 if (stage >= WINED3D_MAX_FRAGMENT_SAMPLERS)
2265 WARN("Ignoring invalid stage %lu.\n", stage);
2266 *texture = NULL;
2267 return D3D_OK;
2270 wined3d_mutex_lock();
2271 if ((wined3d_texture = device->stateblock_state->textures[stage]))
2273 texture_impl = wined3d_texture_get_parent(wined3d_texture);
2274 *texture = &texture_impl->IDirect3DBaseTexture8_iface;
2275 IDirect3DBaseTexture8_AddRef(*texture);
2277 else
2279 *texture = NULL;
2281 wined3d_mutex_unlock();
2283 return D3D_OK;
2286 static HRESULT WINAPI d3d8_device_SetTexture(IDirect3DDevice8 *iface, DWORD stage, IDirect3DBaseTexture8 *texture)
2288 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2289 struct d3d8_texture *texture_impl;
2291 TRACE("iface %p, stage %lu, texture %p.\n", iface, stage, texture);
2293 texture_impl = unsafe_impl_from_IDirect3DBaseTexture8(texture);
2295 wined3d_mutex_lock();
2296 wined3d_stateblock_set_texture(device->update_state, stage,
2297 texture_impl ? d3d8_texture_get_draw_texture(texture_impl) : NULL);
2298 wined3d_mutex_unlock();
2300 return D3D_OK;
2303 static const struct tss_lookup
2305 BOOL sampler_state;
2306 union
2308 enum wined3d_texture_stage_state texture_state;
2309 enum wined3d_sampler_state sampler_state;
2310 } u;
2312 tss_lookup[] =
2314 {FALSE, {WINED3D_TSS_INVALID}}, /* 0, unused */
2315 {FALSE, {WINED3D_TSS_COLOR_OP}}, /* 1, D3DTSS_COLOROP */
2316 {FALSE, {WINED3D_TSS_COLOR_ARG1}}, /* 2, D3DTSS_COLORARG1 */
2317 {FALSE, {WINED3D_TSS_COLOR_ARG2}}, /* 3, D3DTSS_COLORARG2 */
2318 {FALSE, {WINED3D_TSS_ALPHA_OP}}, /* 4, D3DTSS_ALPHAOP */
2319 {FALSE, {WINED3D_TSS_ALPHA_ARG1}}, /* 5, D3DTSS_ALPHAARG1 */
2320 {FALSE, {WINED3D_TSS_ALPHA_ARG2}}, /* 6, D3DTSS_ALPHAARG2 */
2321 {FALSE, {WINED3D_TSS_BUMPENV_MAT00}}, /* 7, D3DTSS_BUMPENVMAT00 */
2322 {FALSE, {WINED3D_TSS_BUMPENV_MAT01}}, /* 8, D3DTSS_BUMPENVMAT01 */
2323 {FALSE, {WINED3D_TSS_BUMPENV_MAT10}}, /* 9, D3DTSS_BUMPENVMAT10 */
2324 {FALSE, {WINED3D_TSS_BUMPENV_MAT11}}, /* 10, D3DTSS_BUMPENVMAT11 */
2325 {FALSE, {WINED3D_TSS_TEXCOORD_INDEX}}, /* 11, D3DTSS_TEXCOORDINDEX */
2326 {FALSE, {WINED3D_TSS_INVALID}}, /* 12, unused */
2327 {TRUE, {WINED3D_SAMP_ADDRESS_U}}, /* 13, D3DTSS_ADDRESSU */
2328 {TRUE, {WINED3D_SAMP_ADDRESS_V}}, /* 14, D3DTSS_ADDRESSV */
2329 {TRUE, {WINED3D_SAMP_BORDER_COLOR}}, /* 15, D3DTSS_BORDERCOLOR */
2330 {TRUE, {WINED3D_SAMP_MAG_FILTER}}, /* 16, D3DTSS_MAGFILTER */
2331 {TRUE, {WINED3D_SAMP_MIN_FILTER}}, /* 17, D3DTSS_MINFILTER */
2332 {TRUE, {WINED3D_SAMP_MIP_FILTER}}, /* 18, D3DTSS_MIPFILTER */
2333 {TRUE, {WINED3D_SAMP_MIPMAP_LOD_BIAS}}, /* 19, D3DTSS_MIPMAPLODBIAS */
2334 {TRUE, {WINED3D_SAMP_MAX_MIP_LEVEL}}, /* 20, D3DTSS_MAXMIPLEVEL */
2335 {TRUE, {WINED3D_SAMP_MAX_ANISOTROPY}}, /* 21, D3DTSS_MAXANISOTROPY */
2336 {FALSE, {WINED3D_TSS_BUMPENV_LSCALE}}, /* 22, D3DTSS_BUMPENVLSCALE */
2337 {FALSE, {WINED3D_TSS_BUMPENV_LOFFSET}}, /* 23, D3DTSS_BUMPENVLOFFSET */
2338 {FALSE, {WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS}}, /* 24, D3DTSS_TEXTURETRANSFORMFLAGS */
2339 {TRUE, {WINED3D_SAMP_ADDRESS_W}}, /* 25, D3DTSS_ADDRESSW */
2340 {FALSE, {WINED3D_TSS_COLOR_ARG0}}, /* 26, D3DTSS_COLORARG0 */
2341 {FALSE, {WINED3D_TSS_ALPHA_ARG0}}, /* 27, D3DTSS_ALPHAARG0 */
2342 {FALSE, {WINED3D_TSS_RESULT_ARG}}, /* 28, D3DTSS_RESULTARG */
2345 static HRESULT WINAPI d3d8_device_GetTextureStageState(IDirect3DDevice8 *iface,
2346 DWORD stage, D3DTEXTURESTAGESTATETYPE state, DWORD *value)
2348 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2349 const struct wined3d_stateblock_state *device_state;
2350 const struct tss_lookup *l;
2352 TRACE("iface %p, stage %lu, state %#x, value %p.\n", iface, stage, state, value);
2354 if (state >= ARRAY_SIZE(tss_lookup))
2356 WARN("Invalid state %#x.\n", state);
2357 return D3D_OK;
2360 l = &tss_lookup[state];
2362 wined3d_mutex_lock();
2363 device_state = device->stateblock_state;
2364 if (l->sampler_state)
2365 *value = device_state->sampler_states[stage][l->u.sampler_state];
2366 else
2367 *value = device_state->texture_states[stage][l->u.texture_state];
2368 wined3d_mutex_unlock();
2370 return D3D_OK;
2373 static HRESULT WINAPI d3d8_device_SetTextureStageState(IDirect3DDevice8 *iface,
2374 DWORD stage, D3DTEXTURESTAGESTATETYPE type, DWORD value)
2376 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2377 const struct tss_lookup *l;
2379 TRACE("iface %p, stage %lu, state %#x, value %#lx.\n", iface, stage, type, value);
2381 if (type >= ARRAY_SIZE(tss_lookup))
2383 WARN("Invalid type %#x passed.\n", type);
2384 return D3D_OK;
2387 l = &tss_lookup[type];
2389 wined3d_mutex_lock();
2390 if (l->sampler_state)
2391 wined3d_stateblock_set_sampler_state(device->update_state, stage, l->u.sampler_state, value);
2392 else
2393 wined3d_stateblock_set_texture_stage_state(device->update_state, stage, l->u.texture_state, value);
2394 wined3d_mutex_unlock();
2396 return D3D_OK;
2399 static HRESULT WINAPI d3d8_device_ValidateDevice(IDirect3DDevice8 *iface, DWORD *pass_count)
2401 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2402 HRESULT hr;
2404 TRACE("iface %p, pass_count %p.\n", iface, pass_count);
2406 wined3d_mutex_lock();
2407 wined3d_device_apply_stateblock(device->wined3d_device, device->state);
2408 hr = wined3d_device_validate_device(device->wined3d_device, pass_count);
2409 wined3d_mutex_unlock();
2411 return hr;
2414 static HRESULT WINAPI d3d8_device_GetInfo(IDirect3DDevice8 *iface,
2415 DWORD info_id, void *info, DWORD info_size)
2417 TRACE("iface %p, info_id %#lx, info %p, info_size %lu.\n", iface, info_id, info, info_size);
2419 if (info_id < 4)
2420 return E_FAIL;
2421 return S_FALSE;
2424 static HRESULT WINAPI d3d8_device_SetPaletteEntries(IDirect3DDevice8 *iface,
2425 UINT palette_idx, const PALETTEENTRY *entries)
2427 WARN("iface %p, palette_idx %u, entries %p unimplemented\n", iface, palette_idx, entries);
2429 /* GPUs stopped supporting palettized textures with the Shader Model 1 generation. Wined3d
2430 * does not have a d3d8/9-style palette API */
2432 return D3D_OK;
2435 static HRESULT WINAPI d3d8_device_GetPaletteEntries(IDirect3DDevice8 *iface,
2436 UINT palette_idx, PALETTEENTRY *entries)
2438 FIXME("iface %p, palette_idx %u, entries %p unimplemented.\n", iface, palette_idx, entries);
2440 return D3DERR_INVALIDCALL;
2443 static HRESULT WINAPI d3d8_device_SetCurrentTexturePalette(IDirect3DDevice8 *iface, UINT palette_idx)
2445 WARN("iface %p, palette_idx %u unimplemented.\n", iface, palette_idx);
2447 return D3D_OK;
2450 static HRESULT WINAPI d3d8_device_GetCurrentTexturePalette(IDirect3DDevice8 *iface, UINT *palette_idx)
2452 FIXME("iface %p, palette_idx %p unimplemented.\n", iface, palette_idx);
2454 return D3DERR_INVALIDCALL;
2457 static void d3d8_device_upload_sysmem_vertex_buffers(struct d3d8_device *device,
2458 unsigned int start_vertex, unsigned int vertex_count)
2460 const struct wined3d_stateblock_state *state = device->stateblock_state;
2461 struct wined3d_vertex_declaration *wined3d_decl;
2462 struct wined3d_box box = {0, 0, 0, 1, 0, 1};
2463 const struct wined3d_stream_state *stream;
2464 struct d3d8_vertexbuffer *d3d8_buffer;
2465 struct wined3d_resource *dst_resource;
2466 struct d3d8_vertex_declaration *decl;
2467 struct wined3d_buffer *dst_buffer;
2468 struct wined3d_resource_desc desc;
2469 unsigned int stride, map;
2470 HRESULT hr;
2472 if (!device->sysmem_vb)
2473 return;
2475 if (!(wined3d_decl = state->vertex_declaration))
2476 return;
2478 decl = wined3d_vertex_declaration_get_parent(wined3d_decl);
2479 map = decl->stream_map & device->sysmem_vb;
2480 while (map)
2482 stream = &state->streams[wined3d_bit_scan(&map)];
2483 dst_buffer = stream->buffer;
2484 stride = stream->stride;
2486 d3d8_buffer = wined3d_buffer_get_parent(dst_buffer);
2487 dst_resource = wined3d_buffer_get_resource(dst_buffer);
2488 wined3d_resource_get_desc(dst_resource, &desc);
2489 box.left = stream->offset + start_vertex * stride;
2490 box.right = min(box.left + vertex_count * stride, desc.size);
2491 if (FAILED(hr = wined3d_device_context_copy_sub_resource_region(device->immediate_context,
2492 dst_resource, 0, box.left, 0, 0,
2493 wined3d_buffer_get_resource(d3d8_buffer->wined3d_buffer), 0, &box, 0)))
2494 ERR("Failed to update buffer.\n");
2498 static HRESULT d3d8_device_upload_sysmem_index_buffer(struct d3d8_device *device,
2499 unsigned int *start_idx, unsigned int idx_count)
2501 const struct wined3d_stateblock_state *state = device->stateblock_state;
2502 unsigned int src_offset, idx_size, buffer_size, pos;
2503 struct wined3d_resource_desc resource_desc;
2504 struct wined3d_resource *index_buffer;
2505 struct wined3d_map_desc map_desc;
2506 struct wined3d_box box;
2507 HRESULT hr;
2509 if (!device->sysmem_ib)
2510 return S_OK;
2512 index_buffer = wined3d_buffer_get_resource(state->index_buffer);
2513 wined3d_resource_get_desc(index_buffer, &resource_desc);
2514 idx_size = (state->index_format == WINED3DFMT_R16_UINT) ? 2 : 4;
2516 src_offset = (*start_idx) * idx_size;
2517 buffer_size = min(idx_count * idx_size, resource_desc.size - src_offset);
2519 wined3d_box_set(&box, src_offset, 0, buffer_size, 1, 0, 1);
2520 if (FAILED(hr = wined3d_resource_map(index_buffer, 0, &map_desc, &box, WINED3D_MAP_READ)))
2522 ERR("Failed to map index buffer, hr %#lx.\n", hr);
2523 return hr;
2525 wined3d_streaming_buffer_upload(device->wined3d_device, &device->index_buffer,
2526 map_desc.data, buffer_size, idx_size, &pos);
2527 wined3d_resource_unmap(index_buffer, 0);
2529 wined3d_device_context_set_index_buffer(device->immediate_context,
2530 device->index_buffer.buffer, state->index_format, pos);
2531 *start_idx = 0;
2532 return S_OK;
2535 static void d3d8_device_upload_managed_textures(struct d3d8_device *device)
2537 const struct wined3d_stateblock_state *state = device->stateblock_state;
2538 unsigned int i;
2540 for (i = 0; i < WINED3D_MAX_FRAGMENT_SAMPLERS; ++i)
2542 struct wined3d_texture *wined3d_texture = state->textures[i];
2543 struct d3d8_texture *d3d8_texture;
2545 if (!wined3d_texture)
2546 continue;
2547 d3d8_texture = wined3d_texture_get_parent(wined3d_texture);
2548 if (d3d8_texture->draw_texture)
2549 wined3d_device_update_texture(device->wined3d_device,
2550 d3d8_texture->wined3d_texture, d3d8_texture->draw_texture);
2554 static HRESULT WINAPI d3d8_device_DrawPrimitive(IDirect3DDevice8 *iface,
2555 D3DPRIMITIVETYPE primitive_type, UINT start_vertex, UINT primitive_count)
2557 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2558 struct d3d8_vertexbuffer *vb;
2559 unsigned int vertex_count, i;
2561 TRACE("iface %p, primitive_type %#x, start_vertex %u, primitive_count %u.\n",
2562 iface, primitive_type, start_vertex, primitive_count);
2564 vertex_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2565 wined3d_mutex_lock();
2566 d3d8_device_upload_managed_textures(device);
2567 d3d8_device_upload_sysmem_vertex_buffers(device, start_vertex, vertex_count);
2568 wined3d_device_context_set_primitive_type(device->immediate_context,
2569 wined3d_primitive_type_from_d3d(primitive_type), 0);
2570 wined3d_device_apply_stateblock(device->wined3d_device, device->state);
2571 wined3d_device_context_draw(device->immediate_context, start_vertex, vertex_count, 0, 0);
2573 for (i = 0; i < ARRAY_SIZE(device->stateblock_state->streams); ++i)
2575 if (device->stateblock_state->streams[i].buffer)
2577 vb = wined3d_buffer_get_parent(device->stateblock_state->streams[i].buffer);
2578 vb->discarded = false;
2582 wined3d_mutex_unlock();
2584 return D3D_OK;
2587 static HRESULT WINAPI d3d8_device_DrawIndexedPrimitive(IDirect3DDevice8 *iface,
2588 D3DPRIMITIVETYPE primitive_type, UINT min_vertex_idx, UINT vertex_count,
2589 UINT start_idx, UINT primitive_count)
2591 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2592 struct d3d8_vertexbuffer *vb;
2593 struct d3d8_indexbuffer *ib;
2594 unsigned int index_count, i;
2595 int base_vertex_index;
2596 HRESULT hr;
2598 TRACE("iface %p, primitive_type %#x, min_vertex_idx %u, vertex_count %u, start_idx %u, primitive_count %u.\n",
2599 iface, primitive_type, min_vertex_idx, vertex_count, start_idx, primitive_count);
2601 index_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2602 wined3d_mutex_lock();
2603 if (!device->stateblock_state->index_buffer)
2605 wined3d_mutex_unlock();
2606 WARN("Index buffer not set, returning D3D_OK.\n");
2607 return D3D_OK;
2609 base_vertex_index = device->stateblock_state->base_vertex_index;
2610 d3d8_device_upload_managed_textures(device);
2611 d3d8_device_upload_sysmem_vertex_buffers(device, base_vertex_index + min_vertex_idx, vertex_count);
2612 wined3d_device_context_set_primitive_type(device->immediate_context,
2613 wined3d_primitive_type_from_d3d(primitive_type), 0);
2614 wined3d_device_apply_stateblock(device->wined3d_device, device->state);
2615 if (FAILED(hr = d3d8_device_upload_sysmem_index_buffer(device, &start_idx, index_count)))
2617 wined3d_mutex_unlock();
2618 return hr;
2620 wined3d_device_context_draw_indexed(device->immediate_context, base_vertex_index, start_idx, index_count, 0, 0);
2622 ib = wined3d_buffer_get_parent(device->stateblock_state->index_buffer);
2623 ib->discarded = false;
2624 for (i = 0; i < ARRAY_SIZE(device->stateblock_state->streams); ++i)
2626 if (device->stateblock_state->streams[i].buffer)
2628 vb = wined3d_buffer_get_parent(device->stateblock_state->streams[i].buffer);
2629 vb->discarded = false;
2633 wined3d_mutex_unlock();
2635 return D3D_OK;
2638 static HRESULT WINAPI d3d8_device_DrawPrimitiveUP(IDirect3DDevice8 *iface,
2639 D3DPRIMITIVETYPE primitive_type, UINT primitive_count, const void *data,
2640 UINT stride)
2642 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2643 UINT vtx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2644 UINT size = vtx_count * stride;
2645 unsigned int vb_pos;
2646 HRESULT hr;
2648 TRACE("iface %p, primitive_type %#x, primitive_count %u, data %p, stride %u.\n",
2649 iface, primitive_type, primitive_count, data, stride);
2651 if (!primitive_count || !stride)
2653 WARN("primitive_count or stride is 0, returning D3D_OK.\n");
2654 return D3D_OK;
2657 wined3d_mutex_lock();
2659 if (FAILED(hr = wined3d_streaming_buffer_upload(device->wined3d_device,
2660 &device->vertex_buffer, data, size, stride, &vb_pos)))
2661 goto done;
2663 hr = wined3d_stateblock_set_stream_source(device->state, 0, device->vertex_buffer.buffer, 0, stride);
2664 if (FAILED(hr))
2665 goto done;
2667 d3d8_device_upload_managed_textures(device);
2668 wined3d_device_context_set_primitive_type(device->immediate_context,
2669 wined3d_primitive_type_from_d3d(primitive_type), 0);
2670 wined3d_device_apply_stateblock(device->wined3d_device, device->state);
2671 wined3d_device_context_draw(device->immediate_context, vb_pos / stride, vtx_count, 0, 0);
2672 wined3d_stateblock_set_stream_source(device->state, 0, NULL, 0, 0);
2674 done:
2675 wined3d_mutex_unlock();
2676 return hr;
2679 static HRESULT WINAPI d3d8_device_DrawIndexedPrimitiveUP(IDirect3DDevice8 *iface,
2680 D3DPRIMITIVETYPE primitive_type, UINT min_vertex_idx, UINT vertex_count,
2681 UINT primitive_count, const void *index_data, D3DFORMAT index_format,
2682 const void *vertex_data, UINT vertex_stride)
2684 UINT idx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2685 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2686 UINT idx_fmt_size = index_format == D3DFMT_INDEX16 ? 2 : 4;
2687 unsigned int base_vertex_idx, vb_pos, ib_pos;
2688 UINT vtx_size = vertex_count * vertex_stride;
2689 UINT idx_size = idx_count * idx_fmt_size;
2690 HRESULT hr;
2692 TRACE("iface %p, primitive_type %#x, min_vertex_idx %u, vertex_count %u, primitive_count %u, "
2693 "index_data %p, index_format %#x, vertex_data %p, vertex_stride %u.\n",
2694 iface, primitive_type, min_vertex_idx, vertex_count, primitive_count,
2695 index_data, index_format, vertex_data, vertex_stride);
2697 if (!primitive_count || !vertex_stride)
2699 WARN("primitive_count or vertex_stride is 0, returning D3D_OK.\n");
2700 return D3D_OK;
2703 wined3d_mutex_lock();
2705 if (FAILED(hr = wined3d_streaming_buffer_upload(device->wined3d_device, &device->vertex_buffer,
2706 (char *)vertex_data + min_vertex_idx * vertex_stride, vtx_size, vertex_stride, &vb_pos)))
2707 goto done;
2709 if (FAILED(hr = wined3d_streaming_buffer_upload(device->wined3d_device, &device->index_buffer,
2710 index_data, idx_size, idx_fmt_size, &ib_pos)))
2711 goto done;
2713 hr = wined3d_stateblock_set_stream_source(device->state, 0, device->vertex_buffer.buffer, 0, vertex_stride);
2714 if (FAILED(hr))
2715 goto done;
2717 wined3d_stateblock_set_index_buffer(device->state, device->index_buffer.buffer,
2718 wined3dformat_from_d3dformat(index_format));
2719 base_vertex_idx = vb_pos / vertex_stride - min_vertex_idx;
2720 wined3d_stateblock_set_base_vertex_index(device->state, base_vertex_idx);
2722 d3d8_device_upload_managed_textures(device);
2723 wined3d_device_context_set_primitive_type(device->immediate_context,
2724 wined3d_primitive_type_from_d3d(primitive_type), 0);
2725 wined3d_device_apply_stateblock(device->wined3d_device, device->state);
2726 wined3d_device_context_draw_indexed(device->immediate_context,
2727 base_vertex_idx, ib_pos / idx_fmt_size, idx_count, 0, 0);
2729 wined3d_stateblock_set_stream_source(device->state, 0, NULL, 0, 0);
2730 wined3d_stateblock_set_index_buffer(device->state, NULL, WINED3DFMT_UNKNOWN);
2731 wined3d_stateblock_set_base_vertex_index(device->state, 0);
2733 done:
2734 wined3d_mutex_unlock();
2735 return hr;
2738 static HRESULT WINAPI d3d8_device_ProcessVertices(IDirect3DDevice8 *iface, UINT src_start_idx,
2739 UINT dst_idx, UINT vertex_count, IDirect3DVertexBuffer8 *dst_buffer, DWORD flags)
2741 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2742 struct d3d8_vertexbuffer *dst = unsafe_impl_from_IDirect3DVertexBuffer8(dst_buffer);
2743 const struct wined3d_stateblock_state *state;
2744 const struct wined3d_stream_state *stream;
2745 struct d3d8_vertexbuffer *d3d8_buffer;
2746 unsigned int i, map;
2747 HRESULT hr;
2749 TRACE("iface %p, src_start_idx %u, dst_idx %u, vertex_count %u, dst_buffer %p, flags %#lx.\n",
2750 iface, src_start_idx, dst_idx, vertex_count, dst_buffer, flags);
2752 wined3d_mutex_lock();
2754 state = device->stateblock_state;
2756 /* Note that an alternative approach would be to simply create these
2757 * buffers with WINED3D_RESOURCE_ACCESS_MAP_R and update them here like we
2758 * do for draws. In some regards that would be easier, but it seems less
2759 * than optimal to upload data to the GPU only to subsequently download it
2760 * again. */
2761 map = device->sysmem_vb;
2762 while (map)
2764 i = wined3d_bit_scan(&map);
2765 stream = &state->streams[i];
2767 d3d8_buffer = wined3d_buffer_get_parent(stream->buffer);
2768 if (FAILED(wined3d_stateblock_set_stream_source(device->state,
2769 i, d3d8_buffer->wined3d_buffer, stream->offset, stream->stride)))
2770 ERR("Failed to set stream source.\n");
2773 wined3d_device_apply_stateblock(device->wined3d_device, device->state);
2774 hr = wined3d_device_process_vertices(device->wined3d_device, src_start_idx, dst_idx,
2775 vertex_count, dst->wined3d_buffer, NULL, flags, dst->fvf);
2777 map = device->sysmem_vb;
2778 while (map)
2780 i = wined3d_bit_scan(&map);
2781 stream = &state->streams[i];
2783 d3d8_buffer = wined3d_buffer_get_parent(stream->buffer);
2784 if (FAILED(wined3d_stateblock_set_stream_source(device->state,
2785 i, d3d8_buffer->draw_buffer, stream->offset, stream->stride)))
2786 ERR("Failed to set stream source.\n");
2789 wined3d_mutex_unlock();
2791 return hr;
2794 static HRESULT WINAPI d3d8_device_CreateVertexShader(IDirect3DDevice8 *iface,
2795 const DWORD *declaration, const DWORD *byte_code, DWORD *shader, DWORD usage)
2797 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2798 struct d3d8_vertex_shader *object;
2799 DWORD shader_handle;
2800 DWORD handle;
2801 HRESULT hr;
2803 TRACE("iface %p, declaration %p, byte_code %p, shader %p, usage %#lx.\n",
2804 iface, declaration, byte_code, shader, usage);
2806 if (!(object = heap_alloc_zero(sizeof(*object))))
2808 *shader = 0;
2809 return E_OUTOFMEMORY;
2812 wined3d_mutex_lock();
2813 handle = d3d8_allocate_handle(&device->handle_table, object, D3D8_HANDLE_VS);
2814 wined3d_mutex_unlock();
2815 if (handle == D3D8_INVALID_HANDLE)
2817 ERR("Failed to allocate vertex shader handle.\n");
2818 heap_free(object);
2819 *shader = 0;
2820 return E_OUTOFMEMORY;
2823 shader_handle = handle + VS_HIGHESTFIXEDFXF + 1;
2825 hr = d3d8_vertex_shader_init(object, device, declaration, byte_code, shader_handle, usage);
2826 if (FAILED(hr))
2828 WARN("Failed to initialize vertex shader, hr %#lx.\n", hr);
2829 wined3d_mutex_lock();
2830 d3d8_free_handle(&device->handle_table, handle, D3D8_HANDLE_VS);
2831 wined3d_mutex_unlock();
2832 heap_free(object);
2833 *shader = 0;
2834 return hr;
2837 TRACE("Created vertex shader %p (handle %#lx).\n", object, shader_handle);
2838 *shader = shader_handle;
2840 return D3D_OK;
2843 static struct d3d8_vertex_declaration *d3d8_device_get_fvf_declaration(struct d3d8_device *device, DWORD fvf)
2845 struct d3d8_vertex_declaration *d3d8_declaration;
2846 struct FvfToDecl *convertedDecls = device->decls;
2847 int p, low, high; /* deliberately signed */
2848 HRESULT hr;
2850 TRACE("Searching for declaration for fvf %#lx... ", fvf);
2852 low = 0;
2853 high = device->numConvertedDecls - 1;
2854 while (low <= high)
2856 p = (low + high) >> 1;
2857 TRACE("%d ", p);
2859 if (convertedDecls[p].fvf == fvf)
2861 TRACE("found %p\n", convertedDecls[p].declaration);
2862 return convertedDecls[p].declaration;
2865 if (convertedDecls[p].fvf < fvf)
2866 low = p + 1;
2867 else
2868 high = p - 1;
2870 TRACE("not found. Creating and inserting at position %d.\n", low);
2872 if (!(d3d8_declaration = heap_alloc(sizeof(*d3d8_declaration))))
2873 return NULL;
2875 if (FAILED(hr = d3d8_vertex_declaration_init_fvf(d3d8_declaration, device, fvf)))
2877 WARN("Failed to initialize vertex declaration, hr %#lx.\n", hr);
2878 heap_free(d3d8_declaration);
2879 return NULL;
2882 if (device->declArraySize == device->numConvertedDecls)
2884 UINT grow = device->declArraySize / 2;
2886 if (!(convertedDecls = heap_realloc(convertedDecls,
2887 sizeof(*convertedDecls) * (device->numConvertedDecls + grow))))
2889 d3d8_vertex_declaration_destroy(d3d8_declaration);
2890 return NULL;
2892 device->decls = convertedDecls;
2893 device->declArraySize += grow;
2896 memmove(convertedDecls + low + 1, convertedDecls + low,
2897 sizeof(*convertedDecls) * (device->numConvertedDecls - low));
2898 convertedDecls[low].declaration = d3d8_declaration;
2899 convertedDecls[low].fvf = fvf;
2900 ++device->numConvertedDecls;
2902 TRACE("Returning %p. %u decls in array.\n", d3d8_declaration, device->numConvertedDecls);
2904 return d3d8_declaration;
2907 static HRESULT WINAPI d3d8_device_SetVertexShader(IDirect3DDevice8 *iface, DWORD shader)
2909 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2910 struct d3d8_vertex_shader *shader_impl;
2912 TRACE("iface %p, shader %#lx.\n", iface, shader);
2914 if (VS_HIGHESTFIXEDFXF >= shader)
2916 TRACE("Setting FVF, %#lx\n", shader);
2918 wined3d_mutex_lock();
2919 wined3d_stateblock_set_vertex_declaration(device->update_state,
2920 d3d8_device_get_fvf_declaration(device, shader)->wined3d_vertex_declaration);
2921 wined3d_stateblock_set_vertex_shader(device->update_state, NULL);
2922 wined3d_mutex_unlock();
2924 return D3D_OK;
2927 TRACE("Setting shader\n");
2929 wined3d_mutex_lock();
2930 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS)))
2932 WARN("Invalid handle (%#lx) passed.\n", shader);
2933 wined3d_mutex_unlock();
2935 return D3DERR_INVALIDCALL;
2938 wined3d_stateblock_set_vertex_declaration(device->update_state,
2939 shader_impl->vertex_declaration->wined3d_vertex_declaration);
2940 wined3d_stateblock_set_vertex_shader(device->update_state, shader_impl->wined3d_shader);
2941 wined3d_mutex_unlock();
2943 return D3D_OK;
2946 static HRESULT WINAPI d3d8_device_GetVertexShader(IDirect3DDevice8 *iface, DWORD *shader)
2948 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2949 struct wined3d_vertex_declaration *wined3d_declaration;
2950 struct d3d8_vertex_declaration *d3d8_declaration;
2952 TRACE("iface %p, shader %p.\n", iface, shader);
2954 wined3d_mutex_lock();
2955 if ((wined3d_declaration = device->stateblock_state->vertex_declaration))
2957 d3d8_declaration = wined3d_vertex_declaration_get_parent(wined3d_declaration);
2958 *shader = d3d8_declaration->shader_handle;
2960 else
2962 *shader = 0;
2964 wined3d_mutex_unlock();
2966 TRACE("Returning %#lx.\n", *shader);
2968 return D3D_OK;
2971 static HRESULT WINAPI d3d8_device_DeleteVertexShader(IDirect3DDevice8 *iface, DWORD shader)
2973 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2974 struct d3d8_vertex_shader *shader_impl;
2976 TRACE("iface %p, shader %#lx.\n", iface, shader);
2978 wined3d_mutex_lock();
2979 if (!(shader_impl = d3d8_free_handle(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS)))
2981 WARN("Invalid handle (%#lx) passed.\n", shader);
2982 wined3d_mutex_unlock();
2984 return D3DERR_INVALIDCALL;
2987 if (shader_impl->wined3d_shader
2988 && device->stateblock_state->vs == shader_impl->wined3d_shader)
2989 IDirect3DDevice8_SetVertexShader(iface, 0);
2991 wined3d_mutex_unlock();
2993 d3d8_vertex_shader_destroy(shader_impl);
2995 return D3D_OK;
2998 static HRESULT WINAPI d3d8_device_SetVertexShaderConstant(IDirect3DDevice8 *iface,
2999 DWORD start_register, const void *data, DWORD count)
3001 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3002 HRESULT hr;
3004 TRACE("iface %p, start_register %lu, data %p, count %lu.\n",
3005 iface, start_register, data, count);
3007 if (start_register + count > D3D8_MAX_VERTEX_SHADER_CONSTANTF)
3009 WARN("Trying to access %lu constants, but d3d8 only supports %u.\n",
3010 start_register + count, D3D8_MAX_VERTEX_SHADER_CONSTANTF);
3011 return D3DERR_INVALIDCALL;
3014 wined3d_mutex_lock();
3015 hr = wined3d_stateblock_set_vs_consts_f(device->update_state, start_register, count, data);
3016 wined3d_mutex_unlock();
3018 return hr;
3021 static HRESULT WINAPI d3d8_device_GetVertexShaderConstant(IDirect3DDevice8 *iface,
3022 DWORD start_idx, void *constants, DWORD count)
3024 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3025 const struct wined3d_vec4 *src;
3027 TRACE("iface %p, start_idx %lu, constants %p, count %lu.\n", iface, start_idx, constants, count);
3029 if (!constants)
3030 return D3DERR_INVALIDCALL;
3032 if (!wined3d_bound_range(start_idx, count, device->vs_uniform_count))
3034 WARN("Trying to access %lu constants, but d3d8 only supports %u.\n",
3035 start_idx + count, device->vs_uniform_count);
3036 return D3DERR_INVALIDCALL;
3039 wined3d_mutex_lock();
3040 src = device->stateblock_state->vs_consts_f;
3041 memcpy(constants, &src[start_idx], count * sizeof(*src));
3042 wined3d_mutex_unlock();
3044 return D3D_OK;
3047 static HRESULT WINAPI d3d8_device_GetVertexShaderDeclaration(IDirect3DDevice8 *iface,
3048 DWORD shader, void *data, DWORD *data_size)
3050 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3051 struct d3d8_vertex_declaration *declaration;
3052 struct d3d8_vertex_shader *shader_impl;
3054 TRACE("iface %p, shader %#lx, data %p, data_size %p.\n", iface, shader, data, data_size);
3056 wined3d_mutex_lock();
3057 shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS);
3058 wined3d_mutex_unlock();
3060 if (!shader_impl)
3062 WARN("Invalid handle (%#lx) passed.\n", shader);
3063 return D3DERR_INVALIDCALL;
3065 declaration = shader_impl->vertex_declaration;
3067 if (!data)
3069 *data_size = declaration->elements_size;
3070 return D3D_OK;
3073 /* MSDN claims that if *data_size is smaller than the required size
3074 * we should write the required size and return D3DERR_MOREDATA.
3075 * That's not actually true. */
3076 if (*data_size < declaration->elements_size)
3077 return D3DERR_INVALIDCALL;
3079 memcpy(data, declaration->elements, declaration->elements_size);
3081 return D3D_OK;
3084 static HRESULT WINAPI d3d8_device_GetVertexShaderFunction(IDirect3DDevice8 *iface,
3085 DWORD shader, void *data, DWORD *data_size)
3087 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3088 struct d3d8_vertex_shader *shader_impl = NULL;
3089 HRESULT hr;
3091 TRACE("iface %p, shader %#lx, data %p, data_size %p.\n", iface, shader, data, data_size);
3093 wined3d_mutex_lock();
3094 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS)))
3096 WARN("Invalid handle (%#lx) passed.\n", shader);
3097 wined3d_mutex_unlock();
3099 return D3DERR_INVALIDCALL;
3102 if (!shader_impl->wined3d_shader)
3104 wined3d_mutex_unlock();
3105 *data_size = 0;
3106 return D3D_OK;
3109 hr = wined3d_shader_get_byte_code(shader_impl->wined3d_shader, data, (unsigned int *)data_size);
3110 wined3d_mutex_unlock();
3112 return hr;
3115 static HRESULT WINAPI d3d8_device_SetIndices(IDirect3DDevice8 *iface,
3116 IDirect3DIndexBuffer8 *buffer, UINT base_vertex_idx)
3118 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3119 struct d3d8_indexbuffer *ib = unsafe_impl_from_IDirect3DIndexBuffer8(buffer);
3120 struct wined3d_buffer *wined3d_buffer;
3122 TRACE("iface %p, buffer %p, base_vertex_idx %u.\n", iface, buffer, base_vertex_idx);
3124 if (!ib)
3125 wined3d_buffer = NULL;
3126 else
3127 wined3d_buffer = ib->wined3d_buffer;
3129 /* WineD3D takes an INT(due to d3d9), but d3d8 uses UINTs. Do I have to add a check here that
3130 * the UINT doesn't cause an overflow in the INT? It seems rather unlikely because such large
3131 * vertex buffers can't be created to address them with an index that requires the 32nd bit
3132 * (4 Byte minimum vertex size * 2^31-1 -> 8 gb buffer. The index sign would be the least
3133 * problem)
3135 wined3d_mutex_lock();
3136 wined3d_stateblock_set_base_vertex_index(device->update_state, base_vertex_idx);
3137 wined3d_stateblock_set_index_buffer(device->update_state, wined3d_buffer, ib ? ib->format : WINED3DFMT_UNKNOWN);
3138 if (!device->recording)
3139 device->sysmem_ib = ib && ib->sysmem;
3140 wined3d_mutex_unlock();
3142 return D3D_OK;
3145 static HRESULT WINAPI d3d8_device_GetIndices(IDirect3DDevice8 *iface,
3146 IDirect3DIndexBuffer8 **buffer, UINT *base_vertex_index)
3148 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3149 const struct wined3d_stateblock_state *state;
3150 struct wined3d_buffer *wined3d_buffer;
3151 struct d3d8_indexbuffer *buffer_impl;
3153 TRACE("iface %p, buffer %p, base_vertex_index %p.\n", iface, buffer, base_vertex_index);
3155 if (!buffer)
3156 return D3DERR_INVALIDCALL;
3158 /* The case from UINT to INT is safe because d3d8 will never set negative values */
3159 wined3d_mutex_lock();
3160 state = device->stateblock_state;
3161 *base_vertex_index = state->base_vertex_index;
3162 if ((wined3d_buffer = state->index_buffer))
3164 buffer_impl = wined3d_buffer_get_parent(wined3d_buffer);
3165 *buffer = &buffer_impl->IDirect3DIndexBuffer8_iface;
3166 IDirect3DIndexBuffer8_AddRef(*buffer);
3168 else
3170 *buffer = NULL;
3172 wined3d_mutex_unlock();
3174 return D3D_OK;
3177 static HRESULT WINAPI d3d8_device_CreatePixelShader(IDirect3DDevice8 *iface,
3178 const DWORD *byte_code, DWORD *shader)
3180 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3181 struct d3d8_pixel_shader *object;
3182 DWORD shader_handle;
3183 DWORD handle;
3184 HRESULT hr;
3186 TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
3188 if (!shader)
3189 return D3DERR_INVALIDCALL;
3191 if (!(object = heap_alloc_zero(sizeof(*object))))
3192 return E_OUTOFMEMORY;
3194 wined3d_mutex_lock();
3195 handle = d3d8_allocate_handle(&device->handle_table, object, D3D8_HANDLE_PS);
3196 wined3d_mutex_unlock();
3197 if (handle == D3D8_INVALID_HANDLE)
3199 ERR("Failed to allocate pixel shader handle.\n");
3200 heap_free(object);
3201 return E_OUTOFMEMORY;
3204 shader_handle = handle + VS_HIGHESTFIXEDFXF + 1;
3206 hr = d3d8_pixel_shader_init(object, device, byte_code, shader_handle);
3207 if (FAILED(hr))
3209 WARN("Failed to initialize pixel shader, hr %#lx.\n", hr);
3210 wined3d_mutex_lock();
3211 d3d8_free_handle(&device->handle_table, handle, D3D8_HANDLE_PS);
3212 wined3d_mutex_unlock();
3213 heap_free(object);
3214 *shader = 0;
3215 return hr;
3218 TRACE("Created pixel shader %p (handle %#lx).\n", object, shader_handle);
3219 *shader = shader_handle;
3221 return D3D_OK;
3224 static HRESULT WINAPI d3d8_device_SetPixelShader(IDirect3DDevice8 *iface, DWORD shader)
3226 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3227 struct d3d8_pixel_shader *shader_impl;
3229 TRACE("iface %p, shader %#lx.\n", iface, shader);
3231 wined3d_mutex_lock();
3233 if (!shader)
3235 wined3d_stateblock_set_pixel_shader(device->update_state, NULL);
3236 wined3d_mutex_unlock();
3237 return D3D_OK;
3240 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS)))
3242 WARN("Invalid handle (%#lx) passed.\n", shader);
3243 wined3d_mutex_unlock();
3244 return D3DERR_INVALIDCALL;
3247 TRACE("Setting shader %p.\n", shader_impl);
3248 wined3d_stateblock_set_pixel_shader(device->update_state, shader_impl->wined3d_shader);
3249 wined3d_mutex_unlock();
3251 return D3D_OK;
3254 static HRESULT WINAPI d3d8_device_GetPixelShader(IDirect3DDevice8 *iface, DWORD *shader)
3256 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3257 struct wined3d_shader *object;
3259 TRACE("iface %p, shader %p.\n", iface, shader);
3261 if (!shader)
3262 return D3DERR_INVALIDCALL;
3264 wined3d_mutex_lock();
3265 if ((object = device->stateblock_state->ps))
3267 struct d3d8_pixel_shader *d3d8_shader;
3268 d3d8_shader = wined3d_shader_get_parent(object);
3269 *shader = d3d8_shader->handle;
3271 else
3273 *shader = 0;
3275 wined3d_mutex_unlock();
3277 TRACE("Returning %#lx.\n", *shader);
3279 return D3D_OK;
3282 static HRESULT WINAPI d3d8_device_DeletePixelShader(IDirect3DDevice8 *iface, DWORD shader)
3284 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3285 struct d3d8_pixel_shader *shader_impl;
3287 TRACE("iface %p, shader %#lx.\n", iface, shader);
3289 wined3d_mutex_lock();
3291 if (!(shader_impl = d3d8_free_handle(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS)))
3293 WARN("Invalid handle (%#lx) passed.\n", shader);
3294 wined3d_mutex_unlock();
3295 return D3DERR_INVALIDCALL;
3298 if (device->stateblock_state->ps == shader_impl->wined3d_shader)
3299 IDirect3DDevice8_SetPixelShader(iface, 0);
3301 wined3d_mutex_unlock();
3303 d3d8_pixel_shader_destroy(shader_impl);
3305 return D3D_OK;
3308 static HRESULT WINAPI d3d8_device_SetPixelShaderConstant(IDirect3DDevice8 *iface,
3309 DWORD start_register, const void *data, DWORD count)
3311 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3312 HRESULT hr;
3314 TRACE("iface %p, start_register %lu, data %p, count %lu.\n", iface, start_register, data, count);
3316 wined3d_mutex_lock();
3317 hr = wined3d_stateblock_set_ps_consts_f(device->update_state, start_register, count, data);
3318 wined3d_mutex_unlock();
3320 return hr;
3323 static HRESULT WINAPI d3d8_device_GetPixelShaderConstant(IDirect3DDevice8 *iface,
3324 DWORD start_idx, void *constants, DWORD count)
3326 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3327 const struct wined3d_vec4 *src;
3329 TRACE("iface %p, start_idx %lu, constants %p, count %lu.\n", iface, start_idx, constants, count);
3331 if (!constants || !wined3d_bound_range(start_idx, count, D3D8_MAX_PIXEL_SHADER_CONSTANTF))
3332 return WINED3DERR_INVALIDCALL;
3334 wined3d_mutex_lock();
3335 src = device->stateblock_state->ps_consts_f;
3336 memcpy(constants, &src[start_idx], count * sizeof(*src));
3337 wined3d_mutex_unlock();
3339 return D3D_OK;
3342 static HRESULT WINAPI d3d8_device_GetPixelShaderFunction(IDirect3DDevice8 *iface,
3343 DWORD shader, void *data, DWORD *data_size)
3345 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3346 struct d3d8_pixel_shader *shader_impl = NULL;
3347 HRESULT hr;
3349 TRACE("iface %p, shader %#lx, data %p, data_size %p.\n", iface, shader, data, data_size);
3351 wined3d_mutex_lock();
3352 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS)))
3354 WARN("Invalid handle (%#lx) passed.\n", shader);
3355 wined3d_mutex_unlock();
3357 return D3DERR_INVALIDCALL;
3360 hr = wined3d_shader_get_byte_code(shader_impl->wined3d_shader, data, (unsigned int *)data_size);
3361 wined3d_mutex_unlock();
3363 return hr;
3366 static HRESULT WINAPI d3d8_device_DrawRectPatch(IDirect3DDevice8 *iface, UINT handle,
3367 const float *segment_count, const D3DRECTPATCH_INFO *patch_info)
3369 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
3370 iface, handle, segment_count, patch_info);
3371 return D3D_OK;
3374 static HRESULT WINAPI d3d8_device_DrawTriPatch(IDirect3DDevice8 *iface, UINT handle,
3375 const float *segment_count, const D3DTRIPATCH_INFO *patch_info)
3377 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
3378 iface, handle, segment_count, patch_info);
3379 return D3D_OK;
3382 static HRESULT WINAPI d3d8_device_DeletePatch(IDirect3DDevice8 *iface, UINT handle)
3384 FIXME("iface %p, handle %#x unimplemented.\n", iface, handle);
3385 return D3DERR_INVALIDCALL;
3388 static HRESULT WINAPI d3d8_device_SetStreamSource(IDirect3DDevice8 *iface,
3389 UINT stream_idx, IDirect3DVertexBuffer8 *buffer, UINT stride)
3391 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3392 struct d3d8_vertexbuffer *buffer_impl = unsafe_impl_from_IDirect3DVertexBuffer8(buffer);
3393 const struct wined3d_stateblock_state *state;
3394 struct wined3d_buffer *wined3d_buffer;
3395 HRESULT hr;
3397 TRACE("iface %p, stream_idx %u, buffer %p, stride %u.\n",
3398 iface, stream_idx, buffer, stride);
3400 if (stream_idx >= ARRAY_SIZE(state->streams))
3402 WARN("Stream index %u out of range.\n", stream_idx);
3403 return D3DERR_INVALIDCALL;
3406 wined3d_mutex_lock();
3408 if (!buffer_impl)
3410 wined3d_buffer = NULL;
3411 state = device->stateblock_state;
3412 stride = state->streams[stream_idx].stride;
3414 else if (buffer_impl->draw_buffer)
3415 wined3d_buffer = buffer_impl->draw_buffer;
3416 else
3417 wined3d_buffer = buffer_impl->wined3d_buffer;
3419 hr = wined3d_stateblock_set_stream_source(device->update_state, stream_idx, wined3d_buffer, 0, stride);
3420 if (SUCCEEDED(hr) && !device->recording)
3422 if (buffer_impl && buffer_impl->draw_buffer)
3423 device->sysmem_vb |= (1u << stream_idx);
3424 else
3425 device->sysmem_vb &= ~(1u << stream_idx);
3428 wined3d_mutex_unlock();
3430 return hr;
3433 static HRESULT WINAPI d3d8_device_GetStreamSource(IDirect3DDevice8 *iface,
3434 UINT stream_idx, IDirect3DVertexBuffer8 **buffer, UINT *stride)
3436 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3437 const struct wined3d_stateblock_state *state;
3438 const struct wined3d_stream_state *stream;
3439 struct d3d8_vertexbuffer *buffer_impl;
3441 TRACE("iface %p, stream_idx %u, buffer %p, stride %p.\n",
3442 iface, stream_idx, buffer, stride);
3444 if (!buffer)
3445 return D3DERR_INVALIDCALL;
3447 if (stream_idx >= ARRAY_SIZE(state->streams))
3449 WARN("Stream index %u out of range.\n", stream_idx);
3450 return D3DERR_INVALIDCALL;
3453 wined3d_mutex_lock();
3454 state = device->stateblock_state;
3455 stream = &state->streams[stream_idx];
3456 if (stream->buffer)
3458 buffer_impl = wined3d_buffer_get_parent(stream->buffer);
3459 *buffer = &buffer_impl->IDirect3DVertexBuffer8_iface;
3460 IDirect3DVertexBuffer8_AddRef(*buffer);
3462 else
3463 *buffer = NULL;
3464 *stride = stream->stride;
3465 wined3d_mutex_unlock();
3467 return D3D_OK;
3470 static const struct IDirect3DDevice8Vtbl d3d8_device_vtbl =
3472 d3d8_device_QueryInterface,
3473 d3d8_device_AddRef,
3474 d3d8_device_Release,
3475 d3d8_device_TestCooperativeLevel,
3476 d3d8_device_GetAvailableTextureMem,
3477 d3d8_device_ResourceManagerDiscardBytes,
3478 d3d8_device_GetDirect3D,
3479 d3d8_device_GetDeviceCaps,
3480 d3d8_device_GetDisplayMode,
3481 d3d8_device_GetCreationParameters,
3482 d3d8_device_SetCursorProperties,
3483 d3d8_device_SetCursorPosition,
3484 d3d8_device_ShowCursor,
3485 d3d8_device_CreateAdditionalSwapChain,
3486 d3d8_device_Reset,
3487 d3d8_device_Present,
3488 d3d8_device_GetBackBuffer,
3489 d3d8_device_GetRasterStatus,
3490 d3d8_device_SetGammaRamp,
3491 d3d8_device_GetGammaRamp,
3492 d3d8_device_CreateTexture,
3493 d3d8_device_CreateVolumeTexture,
3494 d3d8_device_CreateCubeTexture,
3495 d3d8_device_CreateVertexBuffer,
3496 d3d8_device_CreateIndexBuffer,
3497 d3d8_device_CreateRenderTarget,
3498 d3d8_device_CreateDepthStencilSurface,
3499 d3d8_device_CreateImageSurface,
3500 d3d8_device_CopyRects,
3501 d3d8_device_UpdateTexture,
3502 d3d8_device_GetFrontBuffer,
3503 d3d8_device_SetRenderTarget,
3504 d3d8_device_GetRenderTarget,
3505 d3d8_device_GetDepthStencilSurface,
3506 d3d8_device_BeginScene,
3507 d3d8_device_EndScene,
3508 d3d8_device_Clear,
3509 d3d8_device_SetTransform,
3510 d3d8_device_GetTransform,
3511 d3d8_device_MultiplyTransform,
3512 d3d8_device_SetViewport,
3513 d3d8_device_GetViewport,
3514 d3d8_device_SetMaterial,
3515 d3d8_device_GetMaterial,
3516 d3d8_device_SetLight,
3517 d3d8_device_GetLight,
3518 d3d8_device_LightEnable,
3519 d3d8_device_GetLightEnable,
3520 d3d8_device_SetClipPlane,
3521 d3d8_device_GetClipPlane,
3522 d3d8_device_SetRenderState,
3523 d3d8_device_GetRenderState,
3524 d3d8_device_BeginStateBlock,
3525 d3d8_device_EndStateBlock,
3526 d3d8_device_ApplyStateBlock,
3527 d3d8_device_CaptureStateBlock,
3528 d3d8_device_DeleteStateBlock,
3529 d3d8_device_CreateStateBlock,
3530 d3d8_device_SetClipStatus,
3531 d3d8_device_GetClipStatus,
3532 d3d8_device_GetTexture,
3533 d3d8_device_SetTexture,
3534 d3d8_device_GetTextureStageState,
3535 d3d8_device_SetTextureStageState,
3536 d3d8_device_ValidateDevice,
3537 d3d8_device_GetInfo,
3538 d3d8_device_SetPaletteEntries,
3539 d3d8_device_GetPaletteEntries,
3540 d3d8_device_SetCurrentTexturePalette,
3541 d3d8_device_GetCurrentTexturePalette,
3542 d3d8_device_DrawPrimitive,
3543 d3d8_device_DrawIndexedPrimitive,
3544 d3d8_device_DrawPrimitiveUP,
3545 d3d8_device_DrawIndexedPrimitiveUP,
3546 d3d8_device_ProcessVertices,
3547 d3d8_device_CreateVertexShader,
3548 d3d8_device_SetVertexShader,
3549 d3d8_device_GetVertexShader,
3550 d3d8_device_DeleteVertexShader,
3551 d3d8_device_SetVertexShaderConstant,
3552 d3d8_device_GetVertexShaderConstant,
3553 d3d8_device_GetVertexShaderDeclaration,
3554 d3d8_device_GetVertexShaderFunction,
3555 d3d8_device_SetStreamSource,
3556 d3d8_device_GetStreamSource,
3557 d3d8_device_SetIndices,
3558 d3d8_device_GetIndices,
3559 d3d8_device_CreatePixelShader,
3560 d3d8_device_SetPixelShader,
3561 d3d8_device_GetPixelShader,
3562 d3d8_device_DeletePixelShader,
3563 d3d8_device_SetPixelShaderConstant,
3564 d3d8_device_GetPixelShaderConstant,
3565 d3d8_device_GetPixelShaderFunction,
3566 d3d8_device_DrawRectPatch,
3567 d3d8_device_DrawTriPatch,
3568 d3d8_device_DeletePatch,
3571 static inline struct d3d8_device *device_from_device_parent(struct wined3d_device_parent *device_parent)
3573 return CONTAINING_RECORD(device_parent, struct d3d8_device, device_parent);
3576 static void CDECL device_parent_wined3d_device_created(struct wined3d_device_parent *device_parent,
3577 struct wined3d_device *device)
3579 TRACE("device_parent %p, device %p\n", device_parent, device);
3582 static void CDECL device_parent_mode_changed(struct wined3d_device_parent *device_parent)
3584 TRACE("device_parent %p.\n", device_parent);
3587 static void CDECL device_parent_activate(struct wined3d_device_parent *device_parent, BOOL activate)
3589 struct d3d8_device *device = device_from_device_parent(device_parent);
3591 TRACE("device_parent %p, activate %#x.\n", device_parent, activate);
3593 if (!activate)
3594 InterlockedCompareExchange(&device->device_state, D3D8_DEVICE_STATE_LOST, D3D8_DEVICE_STATE_OK);
3595 else
3596 InterlockedCompareExchange(&device->device_state, D3D8_DEVICE_STATE_NOT_RESET, D3D8_DEVICE_STATE_LOST);
3599 static HRESULT CDECL device_parent_texture_sub_resource_created(struct wined3d_device_parent *device_parent,
3600 enum wined3d_resource_type type, struct wined3d_texture *wined3d_texture, unsigned int sub_resource_idx,
3601 void **parent, const struct wined3d_parent_ops **parent_ops)
3603 TRACE("device_parent %p, type %#x, wined3d_texture %p, sub_resource_idx %u, parent %p, parent_ops %p.\n",
3604 device_parent, type, wined3d_texture, sub_resource_idx, parent, parent_ops);
3606 if (type == WINED3D_RTYPE_TEXTURE_3D)
3608 struct d3d8_volume *d3d_volume;
3610 if (!(d3d_volume = heap_alloc_zero(sizeof(*d3d_volume))))
3611 return E_OUTOFMEMORY;
3613 volume_init(d3d_volume, wined3d_texture, sub_resource_idx, parent_ops);
3614 *parent = d3d_volume;
3615 TRACE("Created volume %p.\n", d3d_volume);
3617 else if (type != WINED3D_RTYPE_TEXTURE_2D)
3619 ERR("Unhandled resource type %#x.\n", type);
3620 return E_FAIL;
3623 return D3D_OK;
3626 static HRESULT CDECL device_parent_create_swapchain_texture(struct wined3d_device_parent *device_parent,
3627 void *container_parent, const struct wined3d_resource_desc *desc, DWORD texture_flags,
3628 struct wined3d_texture **texture)
3630 struct d3d8_device *device = device_from_device_parent(device_parent);
3631 HRESULT hr;
3633 TRACE("device_parent %p, container_parent %p, desc %p, texture flags %#lx, texture %p.\n",
3634 device_parent, container_parent, desc, texture_flags, texture);
3636 if (FAILED(hr = wined3d_texture_create(device->wined3d_device, desc, 1, 1, texture_flags,
3637 NULL, &device->IDirect3DDevice8_iface, &d3d8_null_wined3d_parent_ops, texture)))
3639 WARN("Failed to create texture, hr %#lx.\n", hr);
3640 return hr;
3643 return hr;
3646 static const struct wined3d_device_parent_ops d3d8_wined3d_device_parent_ops =
3648 device_parent_wined3d_device_created,
3649 device_parent_mode_changed,
3650 device_parent_activate,
3651 device_parent_texture_sub_resource_created,
3652 device_parent_create_swapchain_texture,
3655 static void setup_fpu(void)
3657 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
3658 WORD cw;
3659 __asm__ volatile ("fnstcw %0" : "=m" (cw));
3660 cw = (cw & ~0xf3f) | 0x3f;
3661 __asm__ volatile ("fldcw %0" : : "m" (cw));
3662 #elif defined(__i386__) && defined(_MSC_VER)
3663 WORD cw;
3664 __asm fnstcw cw;
3665 cw = (cw & ~0xf3f) | 0x3f;
3666 __asm fldcw cw;
3667 #else
3668 FIXME("FPU setup not implemented for this platform.\n");
3669 #endif
3672 HRESULT device_init(struct d3d8_device *device, struct d3d8 *parent, struct wined3d *wined3d, UINT adapter,
3673 D3DDEVTYPE device_type, HWND focus_window, DWORD flags, D3DPRESENT_PARAMETERS *parameters)
3675 struct wined3d_swapchain_desc swapchain_desc;
3676 struct wined3d_swapchain *wined3d_swapchain;
3677 struct wined3d_adapter *wined3d_adapter;
3678 struct wined3d_output *wined3d_output;
3679 struct d3d8_swapchain *d3d_swapchain;
3680 struct wined3d_caps caps;
3681 unsigned int output_idx;
3682 HRESULT hr;
3684 static const enum wined3d_feature_level feature_levels[] =
3686 WINED3D_FEATURE_LEVEL_8,
3687 WINED3D_FEATURE_LEVEL_7,
3688 WINED3D_FEATURE_LEVEL_6,
3689 WINED3D_FEATURE_LEVEL_5,
3692 output_idx = adapter;
3693 if (output_idx >= parent->wined3d_output_count)
3694 return D3DERR_INVALIDCALL;
3696 device->IDirect3DDevice8_iface.lpVtbl = &d3d8_device_vtbl;
3697 device->device_parent.ops = &d3d8_wined3d_device_parent_ops;
3698 device->adapter_ordinal = adapter;
3699 device->ref = 1;
3700 if (!(device->handle_table.entries = heap_alloc_zero(D3D8_INITIAL_HANDLE_TABLE_SIZE
3701 * sizeof(*device->handle_table.entries))))
3703 ERR("Failed to allocate handle table memory.\n");
3704 return E_OUTOFMEMORY;
3706 device->handle_table.table_size = D3D8_INITIAL_HANDLE_TABLE_SIZE;
3708 if (!(flags & D3DCREATE_FPU_PRESERVE)) setup_fpu();
3710 wined3d_mutex_lock();
3711 wined3d_output = parent->wined3d_outputs[output_idx];
3712 wined3d_adapter = wined3d_output_get_adapter(wined3d_output);
3713 if (FAILED(hr = wined3d_device_create(wined3d, wined3d_adapter, wined3d_device_type_from_d3d(device_type),
3714 focus_window, flags, 4, feature_levels, ARRAY_SIZE(feature_levels),
3715 &device->device_parent, &device->wined3d_device)))
3717 WARN("Failed to create wined3d device, hr %#lx.\n", hr);
3718 wined3d_mutex_unlock();
3719 heap_free(device->handle_table.entries);
3720 return hr;
3723 device->immediate_context = wined3d_device_get_immediate_context(device->wined3d_device);
3724 wined3d_get_device_caps(wined3d_adapter, wined3d_device_type_from_d3d(device_type), &caps);
3725 device->max_user_clip_planes = caps.MaxUserClipPlanes;
3726 device->vs_uniform_count = caps.MaxVertexShaderConst;
3728 if (FAILED(hr = wined3d_stateblock_create(device->wined3d_device, NULL, WINED3D_SBT_PRIMARY, &device->state)))
3730 ERR("Failed to create primary stateblock, hr %#lx.\n", hr);
3731 wined3d_device_decref(device->wined3d_device);
3732 wined3d_mutex_unlock();
3733 return hr;
3735 device->stateblock_state = wined3d_stateblock_get_state(device->state);
3736 device->update_state = device->state;
3738 wined3d_streaming_buffer_init(&device->vertex_buffer, WINED3D_BIND_VERTEX_BUFFER);
3739 wined3d_streaming_buffer_init(&device->index_buffer, WINED3D_BIND_INDEX_BUFFER);
3741 if (!parameters->Windowed)
3743 if (!focus_window)
3744 focus_window = parameters->hDeviceWindow;
3745 if (FAILED(hr = wined3d_device_acquire_focus_window(device->wined3d_device, focus_window)))
3747 ERR("Failed to acquire focus window, hr %#lx.\n", hr);
3748 wined3d_device_decref(device->wined3d_device);
3749 wined3d_mutex_unlock();
3750 heap_free(device->handle_table.entries);
3751 return hr;
3755 if (flags & D3DCREATE_MULTITHREADED)
3756 wined3d_device_set_multithreaded(device->wined3d_device);
3758 if (!wined3d_swapchain_desc_from_d3d8(&swapchain_desc, wined3d_output, parameters))
3760 wined3d_device_release_focus_window(device->wined3d_device);
3761 wined3d_device_decref(device->wined3d_device);
3762 wined3d_mutex_unlock();
3763 heap_free(device->handle_table.entries);
3764 return D3DERR_INVALIDCALL;
3766 swapchain_desc.flags |= WINED3D_SWAPCHAIN_IMPLICIT;
3768 if (FAILED(hr = d3d8_swapchain_create(device, &swapchain_desc,
3769 wined3dswapinterval_from_d3d(parameters->FullScreen_PresentationInterval), &d3d_swapchain)))
3771 WARN("Failed to create implicit swapchain, hr %#lx.\n", hr);
3772 wined3d_device_release_focus_window(device->wined3d_device);
3773 wined3d_device_decref(device->wined3d_device);
3774 wined3d_mutex_unlock();
3775 heap_free(device->handle_table.entries);
3776 return hr;
3779 wined3d_swapchain = d3d_swapchain->wined3d_swapchain;
3780 wined3d_swapchain_incref(wined3d_swapchain);
3781 IDirect3DSwapChain8_Release(&d3d_swapchain->IDirect3DSwapChain8_iface);
3783 wined3d_stateblock_set_render_state(device->state, WINED3D_RS_ZENABLE,
3784 !!swapchain_desc.enable_auto_depth_stencil);
3785 wined3d_stateblock_set_render_state(device->state, WINED3D_RS_POINTSIZE_MIN, 0);
3786 device_reset_viewport_state(device);
3787 wined3d_mutex_unlock();
3789 present_parameters_from_wined3d_swapchain_desc(parameters,
3790 &swapchain_desc, parameters->FullScreen_PresentationInterval);
3792 device->declArraySize = 16;
3793 if (!(device->decls = heap_alloc(device->declArraySize * sizeof(*device->decls))))
3795 ERR("Failed to allocate FVF vertex declaration map memory.\n");
3796 hr = E_OUTOFMEMORY;
3797 goto err;
3800 device->implicit_swapchain = wined3d_swapchain;
3802 device->d3d_parent = parent;
3803 IDirect3D8_AddRef(&parent->IDirect3D8_iface);
3805 return D3D_OK;
3807 err:
3808 wined3d_mutex_lock();
3809 wined3d_swapchain_decref(wined3d_swapchain);
3810 wined3d_device_release_focus_window(device->wined3d_device);
3811 wined3d_device_decref(device->wined3d_device);
3812 wined3d_mutex_unlock();
3813 heap_free(device->handle_table.entries);
3814 return hr;