ntdll: Buffer pagemap reads in fill_working_set_info().
[wine.git] / dlls / d3d8 / device.c
blobe9cdfa9b4ffce35f4973cfa04731b43e4394dee2
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 = 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 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 free(device->handle_table.entries);
639 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 wined3d_swapchain_desc swapchain_desc, old_swapchain_desc;
944 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
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 wined3d_swapchain_get_desc(device->implicit_swapchain, &old_swapchain_desc);
969 if (device->recording)
970 wined3d_stateblock_decref(device->recording);
971 device->recording = NULL;
972 device->update_state = device->state;
973 wined3d_stateblock_reset(device->state);
975 /* wined3d_device_reset() may recreate swapchain textures.
976 * We do not need to remove the reference to the wined3d swapchain from the
977 * old d3d8 surfaces: we will fail the reset if they have 0 references,
978 * and therefore they are not actually holding a reference to the wined3d
979 * swapchain, and will not do anything with it when they are destroyed. */
981 if (SUCCEEDED(hr = wined3d_device_reset(device->wined3d_device, &swapchain_desc,
982 NULL, reset_enum_callback, TRUE)))
984 struct wined3d_rendertarget_view *rtv;
985 struct d3d8_surface *surface;
986 unsigned int i;
988 present_parameters->BackBufferCount = swapchain_desc.backbuffer_count;
989 implicit_swapchain = wined3d_swapchain_get_parent(device->implicit_swapchain);
990 implicit_swapchain->swap_interval
991 = wined3dswapinterval_from_d3d(present_parameters->FullScreen_PresentationInterval);
992 wined3d_stateblock_set_render_state(device->state, WINED3D_RS_POINTSIZE_MIN, 0);
993 wined3d_stateblock_set_render_state(device->state, WINED3D_RS_ZENABLE,
994 !!swapchain_desc.enable_auto_depth_stencil);
995 device_reset_viewport_state(device);
997 /* FIXME: This should be the new backbuffer count, but we don't support
998 * changing the backbuffer count in wined3d yet. */
999 for (i = 0; i < old_swapchain_desc.backbuffer_count; ++i)
1001 struct wined3d_texture *backbuffer = wined3d_swapchain_get_back_buffer(device->implicit_swapchain, i);
1003 if ((surface = d3d8_surface_create(backbuffer, 0, (IUnknown *)&device->IDirect3DDevice8_iface)))
1004 surface->parent_device = &device->IDirect3DDevice8_iface;
1007 if ((rtv = wined3d_device_context_get_depth_stencil_view(device->immediate_context)))
1009 struct wined3d_resource *resource = wined3d_rendertarget_view_get_resource(rtv);
1011 if ((surface = d3d8_surface_create(wined3d_texture_from_resource(resource), 0,
1012 (IUnknown *)&device->IDirect3DDevice8_iface)))
1013 surface->parent_device = &device->IDirect3DDevice8_iface;
1016 device->device_state = D3D8_DEVICE_STATE_OK;
1018 else
1020 device->device_state = D3D8_DEVICE_STATE_NOT_RESET;
1022 wined3d_mutex_unlock();
1024 return hr;
1027 static HRESULT WINAPI d3d8_device_Present(IDirect3DDevice8 *iface, const RECT *src_rect,
1028 const RECT *dst_rect, HWND dst_window_override, const RGNDATA *dirty_region)
1030 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1031 struct d3d8_swapchain *implicit_swapchain;
1033 TRACE("iface %p, src_rect %s, dst_rect %s, dst_window_override %p, dirty_region %p.\n",
1034 iface, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect), dst_window_override, dirty_region);
1036 /* Fraps does not hook IDirect3DDevice8::Present regardless of the hotpatch
1037 * attribute. It only hooks IDirect3DSwapChain8::Present. Yet it properly
1038 * shows a framerate on Windows in applications that only call the device
1039 * method, like e.g. the dx8 sdk samples. The conclusion is that native
1040 * calls the swapchain's public method from the device. */
1041 implicit_swapchain = wined3d_swapchain_get_parent(device->implicit_swapchain);
1042 return IDirect3DSwapChain8_Present(&implicit_swapchain->IDirect3DSwapChain8_iface,
1043 src_rect, dst_rect, dst_window_override, dirty_region);
1046 static HRESULT WINAPI d3d8_device_GetBackBuffer(IDirect3DDevice8 *iface,
1047 UINT backbuffer_idx, D3DBACKBUFFER_TYPE backbuffer_type, IDirect3DSurface8 **backbuffer)
1049 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1050 struct wined3d_texture *wined3d_texture;
1051 struct d3d8_surface *surface_impl;
1053 TRACE("iface %p, backbuffer_idx %u, backbuffer_type %#x, backbuffer %p.\n",
1054 iface, backbuffer_idx, backbuffer_type, backbuffer);
1056 /* backbuffer_type is ignored by native. */
1058 /* No need to check for backbuffer == NULL, Windows crashes in that case. */
1059 wined3d_mutex_lock();
1061 if (!(wined3d_texture = wined3d_swapchain_get_back_buffer(device->implicit_swapchain, backbuffer_idx)))
1063 wined3d_mutex_unlock();
1064 *backbuffer = NULL;
1065 return D3DERR_INVALIDCALL;
1068 surface_impl = wined3d_texture_get_sub_resource_parent(wined3d_texture, 0);
1069 *backbuffer = &surface_impl->IDirect3DSurface8_iface;
1070 IDirect3DSurface8_AddRef(*backbuffer);
1072 wined3d_mutex_unlock();
1073 return D3D_OK;
1076 static HRESULT WINAPI d3d8_device_GetRasterStatus(IDirect3DDevice8 *iface, D3DRASTER_STATUS *raster_status)
1078 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1079 HRESULT hr;
1081 TRACE("iface %p, raster_status %p.\n", iface, raster_status);
1083 wined3d_mutex_lock();
1084 hr = wined3d_device_get_raster_status(device->wined3d_device, 0, (struct wined3d_raster_status *)raster_status);
1085 wined3d_mutex_unlock();
1087 return hr;
1090 static void WINAPI d3d8_device_SetGammaRamp(IDirect3DDevice8 *iface, DWORD flags, const D3DGAMMARAMP *ramp)
1092 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1094 TRACE("iface %p, flags %#lx, ramp %p.\n", iface, flags, ramp);
1096 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
1097 wined3d_mutex_lock();
1098 wined3d_device_set_gamma_ramp(device->wined3d_device, 0, flags, (const struct wined3d_gamma_ramp *)ramp);
1099 wined3d_mutex_unlock();
1102 static void WINAPI d3d8_device_GetGammaRamp(IDirect3DDevice8 *iface, D3DGAMMARAMP *ramp)
1104 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1106 TRACE("iface %p, ramp %p.\n", iface, ramp);
1108 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
1109 wined3d_mutex_lock();
1110 wined3d_device_get_gamma_ramp(device->wined3d_device, 0, (struct wined3d_gamma_ramp *)ramp);
1111 wined3d_mutex_unlock();
1114 static HRESULT WINAPI d3d8_device_CreateTexture(IDirect3DDevice8 *iface,
1115 UINT width, UINT height, UINT levels, DWORD usage, D3DFORMAT format,
1116 D3DPOOL pool, IDirect3DTexture8 **texture)
1118 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1119 struct d3d8_texture *object;
1120 unsigned int i;
1121 HRESULT hr;
1123 TRACE("iface %p, width %u, height %u, levels %u, usage %#lx, format %#x, pool %#x, texture %p.\n",
1124 iface, width, height, levels, usage, format, pool, texture);
1126 if (!format)
1127 return D3DERR_INVALIDCALL;
1129 *texture = NULL;
1130 if (!(object = calloc(1, sizeof(*object))))
1131 return D3DERR_OUTOFVIDEOMEMORY;
1133 hr = d3d8_texture_2d_init(object, device, width, height, levels, usage, format, pool);
1134 if (FAILED(hr))
1136 WARN("Failed to initialize texture, hr %#lx.\n", hr);
1137 free(object);
1138 return hr;
1141 levels = wined3d_texture_get_level_count(object->wined3d_texture);
1142 for (i = 0; i < levels; ++i)
1144 if (!d3d8_surface_create(object->wined3d_texture, i, (IUnknown *)&object->IDirect3DBaseTexture8_iface))
1146 IDirect3DTexture8_Release(&object->IDirect3DBaseTexture8_iface);
1147 return E_OUTOFMEMORY;
1151 TRACE("Created texture %p.\n", object);
1152 *texture = (IDirect3DTexture8 *)&object->IDirect3DBaseTexture8_iface;
1154 return D3D_OK;
1157 static HRESULT WINAPI d3d8_device_CreateVolumeTexture(IDirect3DDevice8 *iface,
1158 UINT width, UINT height, UINT depth, UINT levels, DWORD usage, D3DFORMAT format,
1159 D3DPOOL pool, IDirect3DVolumeTexture8 **texture)
1161 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1162 struct d3d8_texture *object;
1163 HRESULT hr;
1165 TRACE("iface %p, width %u, height %u, depth %u, levels %u, usage %#lx, format %#x, pool %#x, texture %p.\n",
1166 iface, width, height, depth, levels, usage, format, pool, texture);
1168 if (!format)
1169 return D3DERR_INVALIDCALL;
1171 *texture = NULL;
1172 if (!(object = calloc(1, sizeof(*object))))
1173 return D3DERR_OUTOFVIDEOMEMORY;
1175 hr = d3d8_texture_3d_init(object, device, width, height, depth, levels, usage, format, pool);
1176 if (FAILED(hr))
1178 WARN("Failed to initialize volume texture, hr %#lx.\n", hr);
1179 free(object);
1180 return hr;
1183 TRACE("Created volume texture %p.\n", object);
1184 *texture = (IDirect3DVolumeTexture8 *)&object->IDirect3DBaseTexture8_iface;
1186 return D3D_OK;
1189 static HRESULT WINAPI d3d8_device_CreateCubeTexture(IDirect3DDevice8 *iface, UINT edge_length,
1190 UINT levels, DWORD usage, D3DFORMAT format, D3DPOOL pool, IDirect3DCubeTexture8 **texture)
1192 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1193 struct d3d8_texture *object;
1194 unsigned int i;
1195 HRESULT hr;
1197 TRACE("iface %p, edge_length %u, levels %u, usage %#lx, format %#x, pool %#x, texture %p.\n",
1198 iface, edge_length, levels, usage, format, pool, texture);
1200 if (!format)
1201 return D3DERR_INVALIDCALL;
1203 *texture = NULL;
1204 if (!(object = calloc(1, sizeof(*object))))
1205 return D3DERR_OUTOFVIDEOMEMORY;
1207 hr = d3d8_texture_cube_init(object, device, edge_length, levels, usage, format, pool);
1208 if (FAILED(hr))
1210 WARN("Failed to initialize cube texture, hr %#lx.\n", hr);
1211 free(object);
1212 return hr;
1215 levels = wined3d_texture_get_level_count(object->wined3d_texture);
1216 for (i = 0; i < levels * 6; ++i)
1218 if (!d3d8_surface_create(object->wined3d_texture, i, (IUnknown *)&object->IDirect3DBaseTexture8_iface))
1220 IDirect3DTexture8_Release(&object->IDirect3DBaseTexture8_iface);
1221 return E_OUTOFMEMORY;
1225 TRACE("Created cube texture %p.\n", object);
1226 *texture = (IDirect3DCubeTexture8 *)&object->IDirect3DBaseTexture8_iface;
1228 return hr;
1231 static HRESULT WINAPI d3d8_device_CreateVertexBuffer(IDirect3DDevice8 *iface, UINT size,
1232 DWORD usage, DWORD fvf, D3DPOOL pool, IDirect3DVertexBuffer8 **buffer)
1234 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1235 struct d3d8_vertexbuffer *object;
1236 HRESULT hr;
1238 TRACE("iface %p, size %u, usage %#lx, fvf %#lx, pool %#x, buffer %p.\n",
1239 iface, size, usage, fvf, pool, buffer);
1241 if (!(object = calloc(1, sizeof(*object))))
1242 return D3DERR_OUTOFVIDEOMEMORY;
1244 hr = vertexbuffer_init(object, device, size, usage, fvf, pool);
1245 if (FAILED(hr))
1247 WARN("Failed to initialize vertex buffer, hr %#lx.\n", hr);
1248 free(object);
1249 return hr;
1252 TRACE("Created vertex buffer %p.\n", object);
1253 *buffer = &object->IDirect3DVertexBuffer8_iface;
1255 return D3D_OK;
1258 static HRESULT WINAPI d3d8_device_CreateIndexBuffer(IDirect3DDevice8 *iface, UINT size,
1259 DWORD usage, D3DFORMAT format, D3DPOOL pool, IDirect3DIndexBuffer8 **buffer)
1261 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1262 struct d3d8_indexbuffer *object;
1263 HRESULT hr;
1265 TRACE("iface %p, size %u, usage %#lx, format %#x, pool %#x, buffer %p.\n",
1266 iface, size, usage, format, pool, buffer);
1268 if (!(object = calloc(1, sizeof(*object))))
1269 return D3DERR_OUTOFVIDEOMEMORY;
1271 hr = indexbuffer_init(object, device, size, usage, format, pool);
1272 if (FAILED(hr))
1274 WARN("Failed to initialize index buffer, hr %#lx.\n", hr);
1275 free(object);
1276 return hr;
1279 TRACE("Created index buffer %p.\n", object);
1280 *buffer = &object->IDirect3DIndexBuffer8_iface;
1282 return D3D_OK;
1285 static HRESULT d3d8_device_create_surface(struct d3d8_device *device, enum wined3d_format_id format,
1286 enum wined3d_multisample_type multisample_type, unsigned int bind_flags, unsigned int access,
1287 unsigned int width, unsigned int height, IDirect3DSurface8 **surface)
1289 struct wined3d_resource_desc desc;
1290 struct d3d8_surface *surface_impl;
1291 struct wined3d_texture *texture;
1292 HRESULT hr;
1294 TRACE("device %p, format %#x, multisample_type %#x, bind_flags %#x, "
1295 "access %#x, width %u, height %u, surface %p.\n",
1296 device, format, multisample_type, bind_flags, access, width, height, surface);
1298 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
1299 desc.format = format;
1300 desc.multisample_type = multisample_type;
1301 desc.multisample_quality = 0;
1302 desc.usage = 0;
1303 desc.bind_flags = bind_flags;
1304 desc.access = access;
1305 desc.width = width;
1306 desc.height = height;
1307 desc.depth = 1;
1308 desc.size = 0;
1310 wined3d_mutex_lock();
1312 if (FAILED(hr = wined3d_texture_create(device->wined3d_device, &desc,
1313 1, 1, 0, NULL, NULL, &d3d8_null_wined3d_parent_ops, &texture)))
1315 wined3d_mutex_unlock();
1316 WARN("Failed to create texture, hr %#lx.\n", hr);
1317 return hr;
1320 if (!(surface_impl = d3d8_surface_create(texture, 0, NULL)))
1322 wined3d_texture_decref(texture);
1323 wined3d_mutex_unlock();
1324 return E_OUTOFMEMORY;
1326 surface_impl->parent_device = &device->IDirect3DDevice8_iface;
1327 *surface = &surface_impl->IDirect3DSurface8_iface;
1328 IDirect3DSurface8_AddRef(*surface);
1329 wined3d_texture_decref(texture);
1331 wined3d_mutex_unlock();
1333 return D3D_OK;
1336 static HRESULT WINAPI d3d8_device_CreateRenderTarget(IDirect3DDevice8 *iface, UINT width,
1337 UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, BOOL lockable,
1338 IDirect3DSurface8 **surface)
1340 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1341 unsigned int access = WINED3D_RESOURCE_ACCESS_GPU;
1343 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, lockable %#x, surface %p.\n",
1344 iface, width, height, format, multisample_type, lockable, surface);
1346 if (!format)
1347 return D3DERR_INVALIDCALL;
1349 *surface = NULL;
1350 if (lockable)
1351 access |= WINED3D_RESOURCE_ACCESS_MAP_R | WINED3D_RESOURCE_ACCESS_MAP_W;
1353 return d3d8_device_create_surface(device, wined3dformat_from_d3dformat(format),
1354 wined3d_multisample_type_from_d3d(multisample_type),
1355 WINED3D_BIND_RENDER_TARGET, access, width, height, surface);
1358 static HRESULT WINAPI d3d8_device_CreateDepthStencilSurface(IDirect3DDevice8 *iface,
1359 UINT width, UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type,
1360 IDirect3DSurface8 **surface)
1362 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1364 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, surface %p.\n",
1365 iface, width, height, format, multisample_type, surface);
1367 if (!format)
1368 return D3DERR_INVALIDCALL;
1370 *surface = NULL;
1372 return d3d8_device_create_surface(device, wined3dformat_from_d3dformat(format),
1373 wined3d_multisample_type_from_d3d(multisample_type), WINED3D_BIND_DEPTH_STENCIL,
1374 WINED3D_RESOURCE_ACCESS_GPU, width, height, surface);
1377 /* IDirect3DDevice8Impl::CreateImageSurface returns surface with pool type SYSTEMMEM */
1378 static HRESULT WINAPI d3d8_device_CreateImageSurface(IDirect3DDevice8 *iface, UINT width,
1379 UINT height, D3DFORMAT format, IDirect3DSurface8 **surface)
1381 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1382 unsigned int access = WINED3D_RESOURCE_ACCESS_CPU
1383 | WINED3D_RESOURCE_ACCESS_MAP_R | WINED3D_RESOURCE_ACCESS_MAP_W;
1385 TRACE("iface %p, width %u, height %u, format %#x, surface %p.\n",
1386 iface, width, height, format, surface);
1388 *surface = NULL;
1390 return d3d8_device_create_surface(device, wined3dformat_from_d3dformat(format),
1391 WINED3D_MULTISAMPLE_NONE, 0, access, width, height, surface);
1394 static HRESULT WINAPI d3d8_device_CopyRects(IDirect3DDevice8 *iface,
1395 IDirect3DSurface8 *src_surface, const RECT *src_rects, UINT rect_count,
1396 IDirect3DSurface8 *dst_surface, const POINT *dst_points)
1398 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1399 struct d3d8_surface *src = unsafe_impl_from_IDirect3DSurface8(src_surface);
1400 struct d3d8_surface *dst = unsafe_impl_from_IDirect3DSurface8(dst_surface);
1401 enum wined3d_format_id src_format, dst_format;
1402 struct wined3d_sub_resource_desc wined3d_desc;
1403 UINT src_w, src_h;
1405 TRACE("iface %p, src_surface %p, src_rects %p, rect_count %u, dst_surface %p, dst_points %p.\n",
1406 iface, src_surface, src_rects, rect_count, dst_surface, dst_points);
1408 wined3d_mutex_lock();
1409 wined3d_texture_get_sub_resource_desc(src->wined3d_texture, src->sub_resource_idx, &wined3d_desc);
1410 if (wined3d_desc.bind_flags & WINED3D_BIND_DEPTH_STENCIL)
1412 WARN("Source %p is a depth stencil surface, returning D3DERR_INVALIDCALL.\n", src_surface);
1413 wined3d_mutex_unlock();
1414 return D3DERR_INVALIDCALL;
1416 src_format = wined3d_desc.format;
1417 src_w = wined3d_desc.width;
1418 src_h = wined3d_desc.height;
1420 wined3d_texture_get_sub_resource_desc(dst->wined3d_texture, dst->sub_resource_idx, &wined3d_desc);
1421 if (wined3d_desc.bind_flags & WINED3D_BIND_DEPTH_STENCIL)
1423 WARN("Destination %p is a depth stencil surface, returning D3DERR_INVALIDCALL.\n", dst_surface);
1424 wined3d_mutex_unlock();
1425 return D3DERR_INVALIDCALL;
1427 dst_format = wined3d_desc.format;
1429 /* Check that the source and destination formats match */
1430 if (src_format != dst_format)
1432 WARN("Source %p format must match the destination %p format, returning D3DERR_INVALIDCALL.\n",
1433 src_surface, dst_surface);
1434 wined3d_mutex_unlock();
1435 return D3DERR_INVALIDCALL;
1438 /* Quick if complete copy ... */
1439 if (!rect_count && !src_rects && !dst_points)
1441 RECT rect = {0, 0, src_w, src_h};
1442 wined3d_device_context_blt(device->immediate_context, dst->wined3d_texture, dst->sub_resource_idx, &rect,
1443 src->wined3d_texture, src->sub_resource_idx, &rect, 0, NULL, WINED3D_TEXF_POINT);
1445 else
1447 unsigned int i;
1448 /* Copy rect by rect */
1449 if (src_rects && dst_points)
1451 for (i = 0; i < rect_count; ++i)
1453 UINT w = src_rects[i].right - src_rects[i].left;
1454 UINT h = src_rects[i].bottom - src_rects[i].top;
1455 RECT dst_rect = {dst_points[i].x, dst_points[i].y,
1456 dst_points[i].x + w, dst_points[i].y + h};
1458 wined3d_device_context_blt(device->immediate_context,
1459 dst->wined3d_texture, dst->sub_resource_idx, &dst_rect,
1460 src->wined3d_texture, src->sub_resource_idx, &src_rects[i], 0, NULL, WINED3D_TEXF_POINT);
1463 else
1465 for (i = 0; i < rect_count; ++i)
1467 UINT w = src_rects[i].right - src_rects[i].left;
1468 UINT h = src_rects[i].bottom - src_rects[i].top;
1469 RECT dst_rect = {0, 0, w, h};
1471 wined3d_device_context_blt(device->immediate_context,
1472 dst->wined3d_texture, dst->sub_resource_idx, &dst_rect,
1473 src->wined3d_texture, src->sub_resource_idx, &src_rects[i], 0, NULL, WINED3D_TEXF_POINT);
1477 wined3d_mutex_unlock();
1479 return WINED3D_OK;
1482 static HRESULT WINAPI d3d8_device_UpdateTexture(IDirect3DDevice8 *iface,
1483 IDirect3DBaseTexture8 *src_texture, IDirect3DBaseTexture8 *dst_texture)
1485 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1486 struct d3d8_texture *src_impl, *dst_impl;
1487 HRESULT hr;
1489 TRACE("iface %p, src_texture %p, dst_texture %p.\n", iface, src_texture, dst_texture);
1491 src_impl = unsafe_impl_from_IDirect3DBaseTexture8(src_texture);
1492 dst_impl = unsafe_impl_from_IDirect3DBaseTexture8(dst_texture);
1494 if (src_impl->draw_texture || dst_impl->draw_texture)
1496 WARN("Source or destination is managed; returning D3DERR_INVALIDCALL.\n");
1497 return D3DERR_INVALIDCALL;
1500 wined3d_mutex_lock();
1501 hr = wined3d_device_update_texture(device->wined3d_device,
1502 src_impl->wined3d_texture, dst_impl->wined3d_texture);
1503 wined3d_mutex_unlock();
1505 return hr;
1508 static HRESULT WINAPI d3d8_device_GetFrontBuffer(IDirect3DDevice8 *iface, IDirect3DSurface8 *dst_surface)
1510 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1511 struct d3d8_surface *dst_impl = unsafe_impl_from_IDirect3DSurface8(dst_surface);
1512 HRESULT hr;
1514 TRACE("iface %p, dst_surface %p.\n", iface, dst_surface);
1516 if (!dst_surface)
1518 WARN("Invalid destination surface passed.\n");
1519 return D3DERR_INVALIDCALL;
1522 wined3d_mutex_lock();
1523 hr = wined3d_swapchain_get_front_buffer_data(device->implicit_swapchain,
1524 dst_impl->wined3d_texture, dst_impl->sub_resource_idx);
1525 wined3d_mutex_unlock();
1527 return hr;
1530 static HRESULT WINAPI d3d8_device_SetRenderTarget(IDirect3DDevice8 *iface,
1531 IDirect3DSurface8 *render_target, IDirect3DSurface8 *depth_stencil)
1533 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1534 struct d3d8_surface *rt_impl = unsafe_impl_from_IDirect3DSurface8(render_target);
1535 struct d3d8_surface *ds_impl = unsafe_impl_from_IDirect3DSurface8(depth_stencil);
1536 struct wined3d_rendertarget_view *original_dsv, *rtv;
1537 HRESULT hr = D3D_OK;
1539 TRACE("iface %p, render_target %p, depth_stencil %p.\n", iface, render_target, depth_stencil);
1541 if (rt_impl && d3d8_surface_get_device(rt_impl) != device)
1543 WARN("Render target surface does not match device.\n");
1544 return D3DERR_INVALIDCALL;
1547 wined3d_mutex_lock();
1549 if (ds_impl)
1551 struct wined3d_sub_resource_desc ds_desc, rt_desc;
1552 struct wined3d_rendertarget_view *original_rtv;
1553 struct d3d8_surface *original_surface;
1555 /* If no render target is passed in check the size against the current RT */
1556 if (!render_target)
1558 if (!(original_rtv = wined3d_device_context_get_rendertarget_view(device->immediate_context, 0)))
1560 wined3d_mutex_unlock();
1561 return D3DERR_NOTFOUND;
1563 original_surface = wined3d_rendertarget_view_get_sub_resource_parent(original_rtv);
1564 wined3d_texture_get_sub_resource_desc(original_surface->wined3d_texture,
1565 original_surface->sub_resource_idx, &rt_desc);
1567 else
1568 wined3d_texture_get_sub_resource_desc(rt_impl->wined3d_texture,
1569 rt_impl->sub_resource_idx, &rt_desc);
1571 wined3d_texture_get_sub_resource_desc(ds_impl->wined3d_texture, ds_impl->sub_resource_idx, &ds_desc);
1573 if (ds_desc.width < rt_desc.width || ds_desc.height < rt_desc.height)
1575 WARN("Depth stencil is smaller than the render target, returning D3DERR_INVALIDCALL\n");
1576 wined3d_mutex_unlock();
1577 return D3DERR_INVALIDCALL;
1579 if (ds_desc.multisample_type != rt_desc.multisample_type
1580 || ds_desc.multisample_quality != rt_desc.multisample_quality)
1582 WARN("Multisample settings do not match, returning D3DERR_INVALIDCALL\n");
1583 wined3d_mutex_unlock();
1584 return D3DERR_INVALIDCALL;
1588 original_dsv = wined3d_device_context_get_depth_stencil_view(device->immediate_context);
1589 rtv = ds_impl ? d3d8_surface_acquire_rendertarget_view(ds_impl) : NULL;
1590 hr = wined3d_device_context_set_depth_stencil_view(device->immediate_context, rtv);
1591 d3d8_surface_release_rendertarget_view(ds_impl, rtv);
1592 if (SUCCEEDED(hr))
1594 rtv = render_target ? d3d8_surface_acquire_rendertarget_view(rt_impl) : NULL;
1595 if (render_target)
1597 if (SUCCEEDED(hr = wined3d_device_context_set_rendertarget_views(device->immediate_context,
1598 0, 1, &rtv, TRUE)))
1599 device_reset_viewport_state(device);
1600 else
1601 wined3d_device_context_set_depth_stencil_view(device->immediate_context, original_dsv);
1603 d3d8_surface_release_rendertarget_view(rt_impl, rtv);
1606 wined3d_mutex_unlock();
1608 return hr;
1611 static HRESULT WINAPI d3d8_device_GetRenderTarget(IDirect3DDevice8 *iface, IDirect3DSurface8 **render_target)
1613 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1614 struct wined3d_rendertarget_view *wined3d_rtv;
1615 struct d3d8_surface *surface_impl;
1616 HRESULT hr;
1618 TRACE("iface %p, render_target %p.\n", iface, render_target);
1620 if (!render_target)
1621 return D3DERR_INVALIDCALL;
1623 wined3d_mutex_lock();
1624 if ((wined3d_rtv = wined3d_device_context_get_rendertarget_view(device->immediate_context, 0)))
1626 /* We want the sub resource parent here, since the view itself may be
1627 * internal to wined3d and may not have a parent. */
1628 surface_impl = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_rtv);
1629 *render_target = &surface_impl->IDirect3DSurface8_iface;
1630 IDirect3DSurface8_AddRef(*render_target);
1631 hr = D3D_OK;
1633 else
1635 ERR("Failed to get wined3d render target.\n");
1636 *render_target = NULL;
1637 hr = D3DERR_NOTFOUND;
1639 wined3d_mutex_unlock();
1641 return hr;
1644 static HRESULT WINAPI d3d8_device_GetDepthStencilSurface(IDirect3DDevice8 *iface, IDirect3DSurface8 **depth_stencil)
1646 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1647 struct wined3d_rendertarget_view *wined3d_dsv;
1648 struct d3d8_surface *surface_impl;
1649 HRESULT hr = D3D_OK;
1651 TRACE("iface %p, depth_stencil %p.\n", iface, depth_stencil);
1653 if (!depth_stencil)
1654 return D3DERR_INVALIDCALL;
1656 wined3d_mutex_lock();
1657 if ((wined3d_dsv = wined3d_device_context_get_depth_stencil_view(device->immediate_context)))
1659 /* We want the sub resource parent here, since the view itself may be
1660 * internal to wined3d and may not have a parent. */
1661 surface_impl = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_dsv);
1662 *depth_stencil = &surface_impl->IDirect3DSurface8_iface;
1663 IDirect3DSurface8_AddRef(*depth_stencil);
1665 else
1667 hr = D3DERR_NOTFOUND;
1668 *depth_stencil = NULL;
1670 wined3d_mutex_unlock();
1672 return hr;
1675 static HRESULT WINAPI d3d8_device_BeginScene(IDirect3DDevice8 *iface)
1677 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1678 HRESULT hr;
1680 TRACE("iface %p.\n", iface);
1682 wined3d_mutex_lock();
1683 hr = wined3d_device_begin_scene(device->wined3d_device);
1684 wined3d_mutex_unlock();
1686 return hr;
1689 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d8_device_EndScene(IDirect3DDevice8 *iface)
1691 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1692 HRESULT hr;
1694 TRACE("iface %p.\n", iface);
1696 wined3d_mutex_lock();
1697 hr = wined3d_device_end_scene(device->wined3d_device);
1698 wined3d_mutex_unlock();
1700 return hr;
1703 static HRESULT WINAPI d3d8_device_Clear(IDirect3DDevice8 *iface, DWORD rect_count,
1704 const D3DRECT *rects, DWORD flags, D3DCOLOR color, float z, DWORD stencil)
1706 const struct wined3d_color c =
1708 ((color >> 16) & 0xff) / 255.0f,
1709 ((color >> 8) & 0xff) / 255.0f,
1710 (color & 0xff) / 255.0f,
1711 ((color >> 24) & 0xff) / 255.0f,
1713 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1714 HRESULT hr;
1716 TRACE("iface %p, rect_count %lu, rects %p, flags %#lx, color 0x%08lx, z %.8e, stencil %lu.\n",
1717 iface, rect_count, rects, flags, color, z, stencil);
1719 if (rect_count && !rects)
1721 WARN("count %lu with NULL rects.\n", rect_count);
1722 rect_count = 0;
1725 wined3d_mutex_lock();
1726 wined3d_device_apply_stateblock(device->wined3d_device, device->state);
1727 hr = wined3d_device_clear(device->wined3d_device, rect_count, (const RECT *)rects, flags, &c, z, stencil);
1728 wined3d_mutex_unlock();
1730 return hr;
1733 static HRESULT WINAPI d3d8_device_SetTransform(IDirect3DDevice8 *iface,
1734 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
1736 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1738 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1740 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1741 wined3d_mutex_lock();
1742 wined3d_stateblock_set_transform(device->update_state,
1743 wined3d_transform_state_from_d3d(state), (const struct wined3d_matrix *)matrix);
1744 wined3d_mutex_unlock();
1746 return D3D_OK;
1749 static HRESULT WINAPI d3d8_device_GetTransform(IDirect3DDevice8 *iface,
1750 D3DTRANSFORMSTATETYPE state, D3DMATRIX *matrix)
1752 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1754 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1756 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1757 wined3d_mutex_lock();
1758 memcpy(matrix, &device->stateblock_state->transforms[state], sizeof(*matrix));
1759 wined3d_mutex_unlock();
1761 return D3D_OK;
1764 static HRESULT WINAPI d3d8_device_MultiplyTransform(IDirect3DDevice8 *iface,
1765 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
1767 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1769 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1771 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1772 wined3d_mutex_lock();
1773 wined3d_stateblock_multiply_transform(device->state,
1774 wined3d_transform_state_from_d3d(state), (const struct wined3d_matrix *)matrix);
1775 wined3d_mutex_unlock();
1777 return D3D_OK;
1780 static HRESULT WINAPI d3d8_device_SetViewport(IDirect3DDevice8 *iface, const D3DVIEWPORT8 *viewport)
1782 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1783 struct wined3d_sub_resource_desc rt_desc;
1784 struct wined3d_rendertarget_view *rtv;
1785 struct d3d8_surface *surface;
1786 struct wined3d_viewport vp;
1788 TRACE("iface %p, viewport %p.\n", iface, viewport);
1790 wined3d_mutex_lock();
1791 if (!(rtv = wined3d_device_context_get_rendertarget_view(device->immediate_context, 0)))
1793 wined3d_mutex_unlock();
1794 return D3DERR_NOTFOUND;
1796 surface = wined3d_rendertarget_view_get_sub_resource_parent(rtv);
1797 wined3d_texture_get_sub_resource_desc(surface->wined3d_texture, surface->sub_resource_idx, &rt_desc);
1799 if (viewport->X > rt_desc.width || viewport->Width > rt_desc.width - viewport->X
1800 || viewport->Y > rt_desc.height || viewport->Height > rt_desc.height - viewport->Y)
1802 WARN("Invalid viewport, returning D3DERR_INVALIDCALL.\n");
1803 wined3d_mutex_unlock();
1804 return D3DERR_INVALIDCALL;
1807 vp.x = viewport->X;
1808 vp.y = viewport->Y;
1809 vp.width = viewport->Width;
1810 vp.height = viewport->Height;
1811 vp.min_z = viewport->MinZ;
1812 vp.max_z = viewport->MaxZ;
1814 wined3d_stateblock_set_viewport(device->update_state, &vp);
1815 wined3d_mutex_unlock();
1817 return D3D_OK;
1820 static HRESULT WINAPI d3d8_device_GetViewport(IDirect3DDevice8 *iface, D3DVIEWPORT8 *viewport)
1822 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1823 struct wined3d_viewport wined3d_viewport;
1825 TRACE("iface %p, viewport %p.\n", iface, viewport);
1827 wined3d_mutex_lock();
1828 wined3d_viewport = device->stateblock_state->viewport;
1829 wined3d_mutex_unlock();
1831 viewport->X = wined3d_viewport.x;
1832 viewport->Y = wined3d_viewport.y;
1833 viewport->Width = wined3d_viewport.width;
1834 viewport->Height = wined3d_viewport.height;
1835 viewport->MinZ = wined3d_viewport.min_z;
1836 viewport->MaxZ = wined3d_viewport.max_z;
1838 return D3D_OK;
1841 static HRESULT WINAPI d3d8_device_SetMaterial(IDirect3DDevice8 *iface, const D3DMATERIAL8 *material)
1843 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1845 TRACE("iface %p, material %p.\n", iface, material);
1847 /* Note: D3DMATERIAL8 is compatible with struct wined3d_material. */
1848 wined3d_mutex_lock();
1849 wined3d_stateblock_set_material(device->update_state, (const struct wined3d_material *)material);
1850 wined3d_mutex_unlock();
1852 return D3D_OK;
1855 static HRESULT WINAPI d3d8_device_GetMaterial(IDirect3DDevice8 *iface, D3DMATERIAL8 *material)
1857 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1859 TRACE("iface %p, material %p.\n", iface, material);
1861 /* Note: D3DMATERIAL8 is compatible with struct wined3d_material. */
1862 wined3d_mutex_lock();
1863 memcpy(material, &device->stateblock_state->material, sizeof(*material));
1864 wined3d_mutex_unlock();
1866 return D3D_OK;
1869 static HRESULT WINAPI d3d8_device_SetLight(IDirect3DDevice8 *iface, DWORD index, const D3DLIGHT8 *light)
1871 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1872 HRESULT hr;
1874 TRACE("iface %p, index %lu, light %p.\n", iface, index, light);
1876 /* Note: D3DLIGHT8 is compatible with struct wined3d_light. */
1877 wined3d_mutex_lock();
1878 hr = wined3d_stateblock_set_light(device->update_state, index, (const struct wined3d_light *)light);
1879 wined3d_mutex_unlock();
1881 return hr;
1884 static HRESULT WINAPI d3d8_device_GetLight(IDirect3DDevice8 *iface, DWORD index, D3DLIGHT8 *light)
1886 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1887 BOOL enabled;
1888 HRESULT hr;
1890 TRACE("iface %p, index %lu, light %p.\n", iface, index, light);
1892 /* Note: D3DLIGHT8 is compatible with struct wined3d_light. */
1893 wined3d_mutex_lock();
1894 hr = wined3d_stateblock_get_light(device->state, index, (struct wined3d_light *)light, &enabled);
1895 wined3d_mutex_unlock();
1897 return hr;
1900 static HRESULT WINAPI d3d8_device_LightEnable(IDirect3DDevice8 *iface, DWORD index, BOOL enable)
1902 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1903 HRESULT hr;
1905 TRACE("iface %p, index %lu, enable %#x.\n", iface, index, enable);
1907 wined3d_mutex_lock();
1908 hr = wined3d_stateblock_set_light_enable(device->update_state, index, enable);
1909 wined3d_mutex_unlock();
1911 return hr;
1914 static HRESULT WINAPI d3d8_device_GetLightEnable(IDirect3DDevice8 *iface, DWORD index, BOOL *enabled)
1916 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1917 struct wined3d_light light;
1918 HRESULT hr;
1920 TRACE("iface %p, index %lu, enabled %p.\n", iface, index, enabled);
1922 wined3d_mutex_lock();
1923 hr = wined3d_stateblock_get_light(device->state, index, &light, enabled);
1924 wined3d_mutex_unlock();
1926 return hr;
1929 static HRESULT WINAPI d3d8_device_SetClipPlane(IDirect3DDevice8 *iface, DWORD index, const float *plane)
1931 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1932 HRESULT hr;
1934 TRACE("iface %p, index %lu, plane %p.\n", iface, index, plane);
1936 wined3d_mutex_lock();
1937 hr = wined3d_stateblock_set_clip_plane(device->update_state, index, (const struct wined3d_vec4 *)plane);
1938 wined3d_mutex_unlock();
1940 return hr;
1943 static HRESULT WINAPI d3d8_device_GetClipPlane(IDirect3DDevice8 *iface, DWORD index, float *plane)
1945 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1947 TRACE("iface %p, index %lu, plane %p.\n", iface, index, plane);
1949 if (index >= device->max_user_clip_planes)
1951 TRACE("Plane %lu requested, but only %u planes are supported.\n", index, device->max_user_clip_planes);
1952 return WINED3DERR_INVALIDCALL;
1955 wined3d_mutex_lock();
1956 memcpy(plane, &device->stateblock_state->clip_planes[index], sizeof(struct wined3d_vec4));
1957 wined3d_mutex_unlock();
1959 return D3D_OK;
1962 static void resolve_depth_buffer(struct d3d8_device *device)
1964 const struct wined3d_stateblock_state *state = device->stateblock_state;
1965 struct wined3d_rendertarget_view *wined3d_dsv;
1966 struct wined3d_resource *dst_resource;
1967 struct wined3d_texture *dst_texture;
1968 struct wined3d_resource_desc desc;
1969 struct d3d8_surface *d3d8_dsv;
1971 if (!(dst_texture = state->textures[0]))
1972 return;
1973 dst_resource = wined3d_texture_get_resource(dst_texture);
1974 wined3d_resource_get_desc(dst_resource, &desc);
1975 if (desc.format != WINED3DFMT_D24_UNORM_S8_UINT
1976 && desc.format != WINED3DFMT_X8D24_UNORM
1977 && desc.format != WINED3DFMT_DF16
1978 && desc.format != WINED3DFMT_DF24
1979 && desc.format != WINED3DFMT_INTZ)
1980 return;
1982 if (!(wined3d_dsv = wined3d_device_context_get_depth_stencil_view(device->immediate_context)))
1983 return;
1984 d3d8_dsv = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_dsv);
1986 wined3d_device_context_resolve_sub_resource(device->immediate_context, dst_resource, 0,
1987 wined3d_rendertarget_view_get_resource(wined3d_dsv), d3d8_dsv->sub_resource_idx, desc.format);
1990 static HRESULT WINAPI d3d8_device_SetRenderState(IDirect3DDevice8 *iface,
1991 D3DRENDERSTATETYPE state, DWORD value)
1993 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1995 TRACE("iface %p, state %#x, value %#lx.\n", iface, state, value);
1997 wined3d_mutex_lock();
1998 wined3d_stateblock_set_render_state(device->update_state, wined3d_render_state_from_d3d(state), value);
1999 if (state == D3DRS_POINTSIZE && value == D3D8_RESZ_CODE)
2000 resolve_depth_buffer(device);
2001 wined3d_mutex_unlock();
2003 return D3D_OK;
2006 static HRESULT WINAPI d3d8_device_GetRenderState(IDirect3DDevice8 *iface,
2007 D3DRENDERSTATETYPE state, DWORD *value)
2009 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2010 const struct wined3d_stateblock_state *device_state;
2012 TRACE("iface %p, state %#x, value %p.\n", iface, state, value);
2014 wined3d_mutex_lock();
2015 device_state = device->stateblock_state;
2016 switch (state)
2018 case D3DRS_ZBIAS:
2019 *value = device_state->rs[WINED3D_RS_DEPTHBIAS];
2020 break;
2022 default:
2023 *value = device_state->rs[state];
2025 wined3d_mutex_unlock();
2027 return D3D_OK;
2030 static HRESULT WINAPI d3d8_device_BeginStateBlock(IDirect3DDevice8 *iface)
2032 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2033 struct wined3d_stateblock *stateblock;
2034 HRESULT hr;
2036 TRACE("iface %p.\n", iface);
2038 wined3d_mutex_lock();
2039 if (device->recording)
2041 wined3d_mutex_unlock();
2042 WARN("Trying to begin a stateblock while recording, returning D3DERR_INVALIDCALL.\n");
2043 return D3DERR_INVALIDCALL;
2046 if (SUCCEEDED(hr = wined3d_stateblock_create(device->wined3d_device, NULL, WINED3D_SBT_RECORDED, &stateblock)))
2047 device->update_state = device->recording = stateblock;
2048 wined3d_mutex_unlock();
2050 return hr;
2053 static HRESULT WINAPI d3d8_device_EndStateBlock(IDirect3DDevice8 *iface, DWORD *token)
2055 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2056 struct wined3d_stateblock *stateblock;
2058 TRACE("iface %p, token %p.\n", iface, token);
2060 /* Tell wineD3D to endstateblock before anything else (in case we run out
2061 * of memory later and cause locking problems)
2063 wined3d_mutex_lock();
2064 if (!device->recording)
2066 wined3d_mutex_unlock();
2067 WARN("Trying to end a stateblock, but no stateblock is being recorded.\n");
2068 return D3DERR_INVALIDCALL;
2070 stateblock = device->recording;
2071 wined3d_stateblock_init_contained_states(stateblock);
2072 device->recording = NULL;
2073 device->update_state = device->state;
2075 *token = d3d8_allocate_handle(&device->handle_table, stateblock, D3D8_HANDLE_SB);
2076 wined3d_mutex_unlock();
2078 if (*token == D3D8_INVALID_HANDLE)
2080 ERR("Failed to create a handle\n");
2081 wined3d_mutex_lock();
2082 wined3d_stateblock_decref(stateblock);
2083 wined3d_mutex_unlock();
2084 return E_FAIL;
2086 ++*token;
2088 TRACE("Returning %#lx (%p).\n", *token, stateblock);
2090 return D3D_OK;
2093 static HRESULT WINAPI d3d8_device_ApplyStateBlock(IDirect3DDevice8 *iface, DWORD token)
2095 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2096 const struct wined3d_stateblock_state *state;
2097 struct d3d8_vertexbuffer *vertex_buffer;
2098 struct wined3d_stateblock *stateblock;
2099 struct d3d8_indexbuffer *index_buffer;
2100 struct wined3d_buffer *wined3d_buffer;
2101 unsigned int i;
2103 TRACE("iface %p, token %#lx.\n", iface, token);
2105 if (!token)
2106 return D3D_OK;
2108 wined3d_mutex_lock();
2109 if (device->recording)
2111 wined3d_mutex_unlock();
2112 WARN("Trying to apply stateblock while recording, returning D3DERR_INVALIDCALL.\n");
2113 return D3DERR_INVALIDCALL;
2115 stateblock = d3d8_get_object(&device->handle_table, token - 1, D3D8_HANDLE_SB);
2116 if (!stateblock)
2118 WARN("Invalid handle (%#lx) passed.\n", token);
2119 wined3d_mutex_unlock();
2120 return D3DERR_INVALIDCALL;
2122 wined3d_stateblock_apply(stateblock, device->state);
2123 device->sysmem_vb = 0;
2124 state = device->stateblock_state;
2125 for (i = 0; i < D3D8_MAX_STREAMS; ++i)
2127 wined3d_buffer = state->streams[i].buffer;
2128 if (!wined3d_buffer || !(vertex_buffer = wined3d_buffer_get_parent(wined3d_buffer)))
2129 continue;
2130 if (vertex_buffer->draw_buffer)
2131 device->sysmem_vb |= 1u << i;
2133 device->sysmem_ib = (wined3d_buffer = state->index_buffer)
2134 && (index_buffer = wined3d_buffer_get_parent(wined3d_buffer)) && index_buffer->sysmem;
2135 wined3d_mutex_unlock();
2137 return D3D_OK;
2140 static HRESULT WINAPI d3d8_device_CaptureStateBlock(IDirect3DDevice8 *iface, DWORD token)
2142 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2143 struct wined3d_stateblock *stateblock;
2145 TRACE("iface %p, token %#lx.\n", iface, token);
2147 wined3d_mutex_lock();
2148 if (device->recording)
2150 wined3d_mutex_unlock();
2151 WARN("Trying to capture stateblock while recording, returning D3DERR_INVALIDCALL.\n");
2152 return D3DERR_INVALIDCALL;
2154 stateblock = d3d8_get_object(&device->handle_table, token - 1, D3D8_HANDLE_SB);
2155 if (!stateblock)
2157 WARN("Invalid handle (%#lx) passed.\n", token);
2158 wined3d_mutex_unlock();
2159 return D3DERR_INVALIDCALL;
2161 wined3d_stateblock_capture(stateblock, device->state);
2162 wined3d_mutex_unlock();
2164 return D3D_OK;
2167 static HRESULT WINAPI d3d8_device_DeleteStateBlock(IDirect3DDevice8 *iface, DWORD token)
2169 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2170 struct wined3d_stateblock *stateblock;
2172 TRACE("iface %p, token %#lx.\n", iface, token);
2174 wined3d_mutex_lock();
2175 stateblock = d3d8_free_handle(&device->handle_table, token - 1, D3D8_HANDLE_SB);
2177 if (!stateblock)
2179 WARN("Invalid handle (%#lx) passed.\n", token);
2180 wined3d_mutex_unlock();
2181 return D3DERR_INVALIDCALL;
2184 if (wined3d_stateblock_decref(stateblock))
2186 ERR("Stateblock %p has references left, this shouldn't happen.\n", stateblock);
2188 wined3d_mutex_unlock();
2190 return D3D_OK;
2193 static HRESULT WINAPI d3d8_device_CreateStateBlock(IDirect3DDevice8 *iface,
2194 D3DSTATEBLOCKTYPE type, DWORD *handle)
2196 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2197 struct wined3d_stateblock *stateblock;
2198 HRESULT hr;
2200 TRACE("iface %p, type %#x, handle %p.\n", iface, type, handle);
2202 if (type != D3DSBT_ALL
2203 && type != D3DSBT_PIXELSTATE
2204 && type != D3DSBT_VERTEXSTATE)
2206 WARN("Unexpected stateblock type, returning D3DERR_INVALIDCALL\n");
2207 return D3DERR_INVALIDCALL;
2210 wined3d_mutex_lock();
2211 if (device->recording)
2213 wined3d_mutex_unlock();
2214 WARN("Trying to create a stateblock while recording, returning D3DERR_INVALIDCALL.\n");
2215 return D3DERR_INVALIDCALL;
2217 hr = wined3d_stateblock_create(device->wined3d_device, device->state, (enum wined3d_stateblock_type)type, &stateblock);
2218 if (FAILED(hr))
2220 wined3d_mutex_unlock();
2221 ERR("Failed to create the state block, hr %#lx.\n", hr);
2222 return hr;
2225 *handle = d3d8_allocate_handle(&device->handle_table, stateblock, D3D8_HANDLE_SB);
2226 wined3d_mutex_unlock();
2228 if (*handle == D3D8_INVALID_HANDLE)
2230 ERR("Failed to allocate a handle.\n");
2231 wined3d_mutex_lock();
2232 wined3d_stateblock_decref(stateblock);
2233 wined3d_mutex_unlock();
2234 return E_FAIL;
2236 ++*handle;
2238 TRACE("Returning %#lx (%p).\n", *handle, stateblock);
2240 return hr;
2243 static HRESULT WINAPI d3d8_device_SetClipStatus(IDirect3DDevice8 *iface, const D3DCLIPSTATUS8 *clip_status)
2245 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2246 HRESULT hr;
2248 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
2249 /* FIXME: Verify that D3DCLIPSTATUS8 ~= struct wined3d_clip_status. */
2251 wined3d_mutex_lock();
2252 hr = wined3d_device_set_clip_status(device->wined3d_device, (const struct wined3d_clip_status *)clip_status);
2253 wined3d_mutex_unlock();
2255 return hr;
2258 static HRESULT WINAPI d3d8_device_GetClipStatus(IDirect3DDevice8 *iface, D3DCLIPSTATUS8 *clip_status)
2260 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2261 HRESULT hr;
2263 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
2265 wined3d_mutex_lock();
2266 hr = wined3d_device_get_clip_status(device->wined3d_device, (struct wined3d_clip_status *)clip_status);
2267 wined3d_mutex_unlock();
2269 return hr;
2272 static HRESULT WINAPI d3d8_device_GetTexture(IDirect3DDevice8 *iface, DWORD stage, IDirect3DBaseTexture8 **texture)
2274 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2275 struct wined3d_texture *wined3d_texture;
2276 struct d3d8_texture *texture_impl;
2278 TRACE("iface %p, stage %lu, texture %p.\n", iface, stage, texture);
2280 if (!texture)
2281 return D3DERR_INVALIDCALL;
2283 if (stage >= WINED3D_MAX_FRAGMENT_SAMPLERS)
2285 WARN("Ignoring invalid stage %lu.\n", stage);
2286 *texture = NULL;
2287 return D3D_OK;
2290 wined3d_mutex_lock();
2291 if ((wined3d_texture = device->stateblock_state->textures[stage]))
2293 texture_impl = wined3d_texture_get_parent(wined3d_texture);
2294 *texture = &texture_impl->IDirect3DBaseTexture8_iface;
2295 IDirect3DBaseTexture8_AddRef(*texture);
2297 else
2299 *texture = NULL;
2301 wined3d_mutex_unlock();
2303 return D3D_OK;
2306 static HRESULT WINAPI d3d8_device_SetTexture(IDirect3DDevice8 *iface, DWORD stage, IDirect3DBaseTexture8 *texture)
2308 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2309 struct d3d8_texture *texture_impl;
2311 TRACE("iface %p, stage %lu, texture %p.\n", iface, stage, texture);
2313 texture_impl = unsafe_impl_from_IDirect3DBaseTexture8(texture);
2315 wined3d_mutex_lock();
2316 wined3d_stateblock_set_texture(device->update_state, stage,
2317 texture_impl ? d3d8_texture_get_draw_texture(texture_impl) : NULL);
2318 wined3d_mutex_unlock();
2320 return D3D_OK;
2323 static const struct tss_lookup
2325 BOOL sampler_state;
2326 union
2328 enum wined3d_texture_stage_state texture_state;
2329 enum wined3d_sampler_state sampler_state;
2330 } u;
2332 tss_lookup[] =
2334 {FALSE, .u.texture_state = WINED3D_TSS_INVALID}, /* 0, unused */
2335 {FALSE, .u.texture_state = WINED3D_TSS_COLOR_OP}, /* 1, D3DTSS_COLOROP */
2336 {FALSE, .u.texture_state = WINED3D_TSS_COLOR_ARG1}, /* 2, D3DTSS_COLORARG1 */
2337 {FALSE, .u.texture_state = WINED3D_TSS_COLOR_ARG2}, /* 3, D3DTSS_COLORARG2 */
2338 {FALSE, .u.texture_state = WINED3D_TSS_ALPHA_OP}, /* 4, D3DTSS_ALPHAOP */
2339 {FALSE, .u.texture_state = WINED3D_TSS_ALPHA_ARG1}, /* 5, D3DTSS_ALPHAARG1 */
2340 {FALSE, .u.texture_state = WINED3D_TSS_ALPHA_ARG2}, /* 6, D3DTSS_ALPHAARG2 */
2341 {FALSE, .u.texture_state = WINED3D_TSS_BUMPENV_MAT00}, /* 7, D3DTSS_BUMPENVMAT00 */
2342 {FALSE, .u.texture_state = WINED3D_TSS_BUMPENV_MAT01}, /* 8, D3DTSS_BUMPENVMAT01 */
2343 {FALSE, .u.texture_state = WINED3D_TSS_BUMPENV_MAT10}, /* 9, D3DTSS_BUMPENVMAT10 */
2344 {FALSE, .u.texture_state = WINED3D_TSS_BUMPENV_MAT11}, /* 10, D3DTSS_BUMPENVMAT11 */
2345 {FALSE, .u.texture_state = WINED3D_TSS_TEXCOORD_INDEX}, /* 11, D3DTSS_TEXCOORDINDEX */
2346 {FALSE, .u.texture_state = WINED3D_TSS_INVALID}, /* 12, unused */
2347 {TRUE, .u.sampler_state = WINED3D_SAMP_ADDRESS_U}, /* 13, D3DTSS_ADDRESSU */
2348 {TRUE, .u.sampler_state = WINED3D_SAMP_ADDRESS_V}, /* 14, D3DTSS_ADDRESSV */
2349 {TRUE, .u.sampler_state = WINED3D_SAMP_BORDER_COLOR}, /* 15, D3DTSS_BORDERCOLOR */
2350 {TRUE, .u.sampler_state = WINED3D_SAMP_MAG_FILTER}, /* 16, D3DTSS_MAGFILTER */
2351 {TRUE, .u.sampler_state = WINED3D_SAMP_MIN_FILTER}, /* 17, D3DTSS_MINFILTER */
2352 {TRUE, .u.sampler_state = WINED3D_SAMP_MIP_FILTER}, /* 18, D3DTSS_MIPFILTER */
2353 {TRUE, .u.sampler_state = WINED3D_SAMP_MIPMAP_LOD_BIAS}, /* 19, D3DTSS_MIPMAPLODBIAS */
2354 {TRUE, .u.sampler_state = WINED3D_SAMP_MAX_MIP_LEVEL}, /* 20, D3DTSS_MAXMIPLEVEL */
2355 {TRUE, .u.sampler_state = WINED3D_SAMP_MAX_ANISOTROPY}, /* 21, D3DTSS_MAXANISOTROPY */
2356 {FALSE, .u.texture_state = WINED3D_TSS_BUMPENV_LSCALE}, /* 22, D3DTSS_BUMPENVLSCALE */
2357 {FALSE, .u.texture_state = WINED3D_TSS_BUMPENV_LOFFSET}, /* 23, D3DTSS_BUMPENVLOFFSET */
2358 {FALSE, .u.texture_state = WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS}, /* 24, D3DTSS_TEXTURETRANSFORMFLAGS */
2359 {TRUE, .u.sampler_state = WINED3D_SAMP_ADDRESS_W}, /* 25, D3DTSS_ADDRESSW */
2360 {FALSE, .u.texture_state = WINED3D_TSS_COLOR_ARG0}, /* 26, D3DTSS_COLORARG0 */
2361 {FALSE, .u.texture_state = WINED3D_TSS_ALPHA_ARG0}, /* 27, D3DTSS_ALPHAARG0 */
2362 {FALSE, .u.texture_state = WINED3D_TSS_RESULT_ARG}, /* 28, D3DTSS_RESULTARG */
2365 static HRESULT WINAPI d3d8_device_GetTextureStageState(IDirect3DDevice8 *iface,
2366 DWORD stage, D3DTEXTURESTAGESTATETYPE state, DWORD *value)
2368 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2369 const struct wined3d_stateblock_state *device_state;
2370 const struct tss_lookup *l;
2372 TRACE("iface %p, stage %lu, state %#x, value %p.\n", iface, stage, state, value);
2374 if (state >= ARRAY_SIZE(tss_lookup))
2376 WARN("Invalid state %#x.\n", state);
2377 return D3D_OK;
2380 l = &tss_lookup[state];
2382 wined3d_mutex_lock();
2383 device_state = device->stateblock_state;
2384 if (l->sampler_state)
2385 *value = device_state->sampler_states[stage][l->u.sampler_state];
2386 else
2387 *value = device_state->texture_states[stage][l->u.texture_state];
2388 wined3d_mutex_unlock();
2390 return D3D_OK;
2393 static HRESULT WINAPI d3d8_device_SetTextureStageState(IDirect3DDevice8 *iface,
2394 DWORD stage, D3DTEXTURESTAGESTATETYPE type, DWORD value)
2396 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2397 const struct tss_lookup *l;
2399 TRACE("iface %p, stage %lu, state %#x, value %#lx.\n", iface, stage, type, value);
2401 if (type >= ARRAY_SIZE(tss_lookup))
2403 WARN("Invalid type %#x passed.\n", type);
2404 return D3D_OK;
2407 l = &tss_lookup[type];
2409 wined3d_mutex_lock();
2410 if (l->sampler_state)
2411 wined3d_stateblock_set_sampler_state(device->update_state, stage, l->u.sampler_state, value);
2412 else
2413 wined3d_stateblock_set_texture_stage_state(device->update_state, stage, l->u.texture_state, value);
2414 wined3d_mutex_unlock();
2416 return D3D_OK;
2419 static HRESULT WINAPI d3d8_device_ValidateDevice(IDirect3DDevice8 *iface, DWORD *pass_count)
2421 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2422 HRESULT hr;
2424 TRACE("iface %p, pass_count %p.\n", iface, pass_count);
2426 wined3d_mutex_lock();
2427 hr = wined3d_device_validate_device(device->wined3d_device, device->stateblock_state, pass_count);
2428 wined3d_mutex_unlock();
2430 /* In d3d8, texture filters are not validated, so errors concerning
2431 * unsupported ones are ignored here. */
2432 if (hr == WINED3DERR_UNSUPPORTEDTEXTUREFILTER)
2434 WARN("Ignoring invalid texture filter settings.\n");
2435 *pass_count = 1;
2436 return D3D_OK;
2439 return hr;
2442 static HRESULT WINAPI d3d8_device_GetInfo(IDirect3DDevice8 *iface,
2443 DWORD info_id, void *info, DWORD info_size)
2445 TRACE("iface %p, info_id %#lx, info %p, info_size %lu.\n", iface, info_id, info, info_size);
2447 if (info_id < 4)
2448 return E_FAIL;
2449 return S_FALSE;
2452 static HRESULT WINAPI d3d8_device_SetPaletteEntries(IDirect3DDevice8 *iface,
2453 UINT palette_idx, const PALETTEENTRY *entries)
2455 WARN("iface %p, palette_idx %u, entries %p unimplemented\n", iface, palette_idx, entries);
2457 /* GPUs stopped supporting palettized textures with the Shader Model 1 generation. Wined3d
2458 * does not have a d3d8/9-style palette API */
2460 return D3D_OK;
2463 static HRESULT WINAPI d3d8_device_GetPaletteEntries(IDirect3DDevice8 *iface,
2464 UINT palette_idx, PALETTEENTRY *entries)
2466 FIXME("iface %p, palette_idx %u, entries %p unimplemented.\n", iface, palette_idx, entries);
2468 return D3DERR_INVALIDCALL;
2471 static HRESULT WINAPI d3d8_device_SetCurrentTexturePalette(IDirect3DDevice8 *iface, UINT palette_idx)
2473 WARN("iface %p, palette_idx %u unimplemented.\n", iface, palette_idx);
2475 return D3D_OK;
2478 static HRESULT WINAPI d3d8_device_GetCurrentTexturePalette(IDirect3DDevice8 *iface, UINT *palette_idx)
2480 FIXME("iface %p, palette_idx %p unimplemented.\n", iface, palette_idx);
2482 return D3DERR_INVALIDCALL;
2485 static void d3d8_device_upload_sysmem_vertex_buffers(struct d3d8_device *device,
2486 unsigned int start_vertex, unsigned int vertex_count)
2488 const struct wined3d_stateblock_state *state = device->stateblock_state;
2489 struct wined3d_vertex_declaration *wined3d_decl;
2490 struct wined3d_box box = {0, 0, 0, 1, 0, 1};
2491 const struct wined3d_stream_state *stream;
2492 struct d3d8_vertexbuffer *d3d8_buffer;
2493 struct wined3d_resource *dst_resource;
2494 struct d3d8_vertex_declaration *decl;
2495 struct wined3d_buffer *dst_buffer;
2496 struct wined3d_resource_desc desc;
2497 unsigned int stride, map;
2498 HRESULT hr;
2500 if (!device->sysmem_vb)
2501 return;
2503 if (!(wined3d_decl = state->vertex_declaration))
2504 return;
2506 decl = wined3d_vertex_declaration_get_parent(wined3d_decl);
2507 map = decl->stream_map & device->sysmem_vb;
2508 while (map)
2510 stream = &state->streams[wined3d_bit_scan(&map)];
2511 dst_buffer = stream->buffer;
2512 stride = stream->stride;
2514 d3d8_buffer = wined3d_buffer_get_parent(dst_buffer);
2515 dst_resource = wined3d_buffer_get_resource(dst_buffer);
2516 wined3d_resource_get_desc(dst_resource, &desc);
2517 box.left = stream->offset + start_vertex * stride;
2518 box.right = min(box.left + vertex_count * stride, desc.size);
2519 if (FAILED(hr = wined3d_device_context_copy_sub_resource_region(device->immediate_context,
2520 dst_resource, 0, box.left, 0, 0,
2521 wined3d_buffer_get_resource(d3d8_buffer->wined3d_buffer), 0, &box, 0)))
2522 ERR("Failed to update buffer.\n");
2526 static HRESULT d3d8_device_upload_sysmem_index_buffer(struct d3d8_device *device,
2527 unsigned int *start_idx, unsigned int idx_count)
2529 const struct wined3d_stateblock_state *state = device->stateblock_state;
2530 unsigned int src_offset, idx_size, buffer_size, pos;
2531 struct wined3d_resource_desc resource_desc;
2532 struct wined3d_resource *index_buffer;
2533 struct wined3d_map_desc map_desc;
2534 struct wined3d_box box;
2535 HRESULT hr;
2537 if (!device->sysmem_ib)
2538 return S_OK;
2540 index_buffer = wined3d_buffer_get_resource(state->index_buffer);
2541 wined3d_resource_get_desc(index_buffer, &resource_desc);
2542 idx_size = (state->index_format == WINED3DFMT_R16_UINT) ? 2 : 4;
2544 src_offset = (*start_idx) * idx_size;
2545 buffer_size = min(idx_count * idx_size, resource_desc.size - src_offset);
2547 wined3d_box_set(&box, src_offset, 0, buffer_size, 1, 0, 1);
2548 if (FAILED(hr = wined3d_resource_map(index_buffer, 0, &map_desc, &box, WINED3D_MAP_READ)))
2550 ERR("Failed to map index buffer, hr %#lx.\n", hr);
2551 return hr;
2553 wined3d_streaming_buffer_upload(device->wined3d_device, &device->index_buffer,
2554 map_desc.data, buffer_size, idx_size, &pos);
2555 wined3d_resource_unmap(index_buffer, 0);
2557 wined3d_device_context_set_index_buffer(device->immediate_context,
2558 device->index_buffer.buffer, state->index_format, pos);
2559 *start_idx = 0;
2560 return S_OK;
2563 static void d3d8_device_upload_managed_textures(struct d3d8_device *device)
2565 const struct wined3d_stateblock_state *state = device->stateblock_state;
2566 unsigned int i;
2568 for (i = 0; i < WINED3D_MAX_FRAGMENT_SAMPLERS; ++i)
2570 struct wined3d_texture *wined3d_texture = state->textures[i];
2571 struct d3d8_texture *d3d8_texture;
2573 if (!wined3d_texture)
2574 continue;
2575 d3d8_texture = wined3d_texture_get_parent(wined3d_texture);
2576 if (d3d8_texture->draw_texture)
2577 wined3d_device_update_texture(device->wined3d_device,
2578 d3d8_texture->wined3d_texture, d3d8_texture->draw_texture);
2582 static HRESULT WINAPI d3d8_device_DrawPrimitive(IDirect3DDevice8 *iface,
2583 D3DPRIMITIVETYPE primitive_type, UINT start_vertex, UINT primitive_count)
2585 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2586 struct d3d8_vertexbuffer *vb;
2587 unsigned int vertex_count, i;
2589 TRACE("iface %p, primitive_type %#x, start_vertex %u, primitive_count %u.\n",
2590 iface, primitive_type, start_vertex, primitive_count);
2592 vertex_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2593 wined3d_mutex_lock();
2594 d3d8_device_upload_managed_textures(device);
2595 d3d8_device_upload_sysmem_vertex_buffers(device, start_vertex, vertex_count);
2596 wined3d_device_context_set_primitive_type(device->immediate_context,
2597 wined3d_primitive_type_from_d3d(primitive_type), 0);
2598 wined3d_device_apply_stateblock(device->wined3d_device, device->state);
2599 wined3d_device_context_draw(device->immediate_context, start_vertex, vertex_count, 0, 0);
2601 for (i = 0; i < ARRAY_SIZE(device->stateblock_state->streams); ++i)
2603 if (device->stateblock_state->streams[i].buffer)
2605 vb = wined3d_buffer_get_parent(device->stateblock_state->streams[i].buffer);
2606 vb->discarded = false;
2610 wined3d_mutex_unlock();
2612 return D3D_OK;
2615 static HRESULT WINAPI d3d8_device_DrawIndexedPrimitive(IDirect3DDevice8 *iface,
2616 D3DPRIMITIVETYPE primitive_type, UINT min_vertex_idx, UINT vertex_count,
2617 UINT start_idx, UINT primitive_count)
2619 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2620 struct d3d8_vertexbuffer *vb;
2621 struct d3d8_indexbuffer *ib;
2622 unsigned int index_count, i;
2623 int base_vertex_index;
2624 HRESULT hr;
2626 TRACE("iface %p, primitive_type %#x, min_vertex_idx %u, vertex_count %u, start_idx %u, primitive_count %u.\n",
2627 iface, primitive_type, min_vertex_idx, vertex_count, start_idx, primitive_count);
2629 index_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2630 wined3d_mutex_lock();
2631 if (!device->stateblock_state->index_buffer)
2633 wined3d_mutex_unlock();
2634 WARN("Index buffer not set, returning D3D_OK.\n");
2635 return D3D_OK;
2637 base_vertex_index = device->stateblock_state->base_vertex_index;
2638 d3d8_device_upload_managed_textures(device);
2639 d3d8_device_upload_sysmem_vertex_buffers(device, base_vertex_index + min_vertex_idx, vertex_count);
2640 wined3d_device_context_set_primitive_type(device->immediate_context,
2641 wined3d_primitive_type_from_d3d(primitive_type), 0);
2642 wined3d_device_apply_stateblock(device->wined3d_device, device->state);
2643 if (FAILED(hr = d3d8_device_upload_sysmem_index_buffer(device, &start_idx, index_count)))
2645 wined3d_mutex_unlock();
2646 return hr;
2648 wined3d_device_context_draw_indexed(device->immediate_context, base_vertex_index, start_idx, index_count, 0, 0);
2650 ib = wined3d_buffer_get_parent(device->stateblock_state->index_buffer);
2651 ib->discarded = false;
2652 for (i = 0; i < ARRAY_SIZE(device->stateblock_state->streams); ++i)
2654 if (device->stateblock_state->streams[i].buffer)
2656 vb = wined3d_buffer_get_parent(device->stateblock_state->streams[i].buffer);
2657 vb->discarded = false;
2661 wined3d_mutex_unlock();
2663 return D3D_OK;
2666 static HRESULT WINAPI d3d8_device_DrawPrimitiveUP(IDirect3DDevice8 *iface,
2667 D3DPRIMITIVETYPE primitive_type, UINT primitive_count, const void *data,
2668 UINT stride)
2670 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2671 UINT vtx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2672 UINT size = vtx_count * stride;
2673 unsigned int vb_pos;
2674 HRESULT hr;
2676 TRACE("iface %p, primitive_type %#x, primitive_count %u, data %p, stride %u.\n",
2677 iface, primitive_type, primitive_count, data, stride);
2679 if (!primitive_count || !stride)
2681 WARN("primitive_count or stride is 0, returning D3D_OK.\n");
2682 return D3D_OK;
2685 wined3d_mutex_lock();
2687 if (FAILED(hr = wined3d_streaming_buffer_upload(device->wined3d_device,
2688 &device->vertex_buffer, data, size, stride, &vb_pos)))
2689 goto done;
2691 hr = wined3d_stateblock_set_stream_source(device->state, 0, device->vertex_buffer.buffer, 0, stride);
2692 if (FAILED(hr))
2693 goto done;
2695 d3d8_device_upload_managed_textures(device);
2696 wined3d_device_context_set_primitive_type(device->immediate_context,
2697 wined3d_primitive_type_from_d3d(primitive_type), 0);
2698 wined3d_device_apply_stateblock(device->wined3d_device, device->state);
2699 wined3d_device_context_draw(device->immediate_context, vb_pos / stride, vtx_count, 0, 0);
2700 wined3d_stateblock_set_stream_source(device->state, 0, NULL, 0, 0);
2702 done:
2703 wined3d_mutex_unlock();
2704 return hr;
2707 static HRESULT WINAPI d3d8_device_DrawIndexedPrimitiveUP(IDirect3DDevice8 *iface,
2708 D3DPRIMITIVETYPE primitive_type, UINT min_vertex_idx, UINT vertex_count,
2709 UINT primitive_count, const void *index_data, D3DFORMAT index_format,
2710 const void *vertex_data, UINT vertex_stride)
2712 UINT idx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2713 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2714 UINT idx_fmt_size = index_format == D3DFMT_INDEX16 ? 2 : 4;
2715 unsigned int base_vertex_idx, vb_pos, ib_pos;
2716 UINT vtx_size = vertex_count * vertex_stride;
2717 UINT idx_size = idx_count * idx_fmt_size;
2718 HRESULT hr;
2720 TRACE("iface %p, primitive_type %#x, min_vertex_idx %u, vertex_count %u, primitive_count %u, "
2721 "index_data %p, index_format %#x, vertex_data %p, vertex_stride %u.\n",
2722 iface, primitive_type, min_vertex_idx, vertex_count, primitive_count,
2723 index_data, index_format, vertex_data, vertex_stride);
2725 if (!primitive_count || !vertex_stride)
2727 WARN("primitive_count or vertex_stride is 0, returning D3D_OK.\n");
2728 return D3D_OK;
2731 wined3d_mutex_lock();
2733 if (FAILED(hr = wined3d_streaming_buffer_upload(device->wined3d_device, &device->vertex_buffer,
2734 (char *)vertex_data + min_vertex_idx * vertex_stride, vtx_size, vertex_stride, &vb_pos)))
2735 goto done;
2737 if (FAILED(hr = wined3d_streaming_buffer_upload(device->wined3d_device, &device->index_buffer,
2738 index_data, idx_size, idx_fmt_size, &ib_pos)))
2739 goto done;
2741 hr = wined3d_stateblock_set_stream_source(device->state, 0, device->vertex_buffer.buffer, 0, vertex_stride);
2742 if (FAILED(hr))
2743 goto done;
2745 wined3d_stateblock_set_index_buffer(device->state, device->index_buffer.buffer,
2746 wined3dformat_from_d3dformat(index_format));
2747 base_vertex_idx = vb_pos / vertex_stride - min_vertex_idx;
2748 wined3d_stateblock_set_base_vertex_index(device->state, base_vertex_idx);
2750 d3d8_device_upload_managed_textures(device);
2751 wined3d_device_context_set_primitive_type(device->immediate_context,
2752 wined3d_primitive_type_from_d3d(primitive_type), 0);
2753 wined3d_device_apply_stateblock(device->wined3d_device, device->state);
2754 wined3d_device_context_draw_indexed(device->immediate_context,
2755 base_vertex_idx, ib_pos / idx_fmt_size, idx_count, 0, 0);
2757 wined3d_stateblock_set_stream_source(device->state, 0, NULL, 0, 0);
2758 wined3d_stateblock_set_index_buffer(device->state, NULL, WINED3DFMT_UNKNOWN);
2759 wined3d_stateblock_set_base_vertex_index(device->state, 0);
2761 done:
2762 wined3d_mutex_unlock();
2763 return hr;
2766 static HRESULT WINAPI d3d8_device_ProcessVertices(IDirect3DDevice8 *iface, UINT src_start_idx,
2767 UINT dst_idx, UINT vertex_count, IDirect3DVertexBuffer8 *dst_buffer, DWORD flags)
2769 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2770 struct d3d8_vertexbuffer *dst = unsafe_impl_from_IDirect3DVertexBuffer8(dst_buffer);
2771 const struct wined3d_stateblock_state *state;
2772 const struct wined3d_stream_state *stream;
2773 struct d3d8_vertexbuffer *d3d8_buffer;
2774 unsigned int i, map;
2775 HRESULT hr;
2777 TRACE("iface %p, src_start_idx %u, dst_idx %u, vertex_count %u, dst_buffer %p, flags %#lx.\n",
2778 iface, src_start_idx, dst_idx, vertex_count, dst_buffer, flags);
2780 wined3d_mutex_lock();
2782 state = device->stateblock_state;
2784 /* Note that an alternative approach would be to simply create these
2785 * buffers with WINED3D_RESOURCE_ACCESS_MAP_R and update them here like we
2786 * do for draws. In some regards that would be easier, but it seems less
2787 * than optimal to upload data to the GPU only to subsequently download it
2788 * again. */
2789 map = device->sysmem_vb;
2790 while (map)
2792 i = wined3d_bit_scan(&map);
2793 stream = &state->streams[i];
2795 d3d8_buffer = wined3d_buffer_get_parent(stream->buffer);
2796 if (FAILED(wined3d_stateblock_set_stream_source(device->state,
2797 i, d3d8_buffer->wined3d_buffer, stream->offset, stream->stride)))
2798 ERR("Failed to set stream source.\n");
2801 hr = wined3d_device_process_vertices(device->wined3d_device, device->state, src_start_idx, dst_idx,
2802 vertex_count, dst->wined3d_buffer, NULL, flags, dst->fvf);
2804 map = device->sysmem_vb;
2805 while (map)
2807 i = wined3d_bit_scan(&map);
2808 stream = &state->streams[i];
2810 d3d8_buffer = wined3d_buffer_get_parent(stream->buffer);
2811 if (FAILED(wined3d_stateblock_set_stream_source(device->state,
2812 i, d3d8_buffer->draw_buffer, stream->offset, stream->stride)))
2813 ERR("Failed to set stream source.\n");
2816 wined3d_mutex_unlock();
2818 return hr;
2821 static HRESULT WINAPI d3d8_device_CreateVertexShader(IDirect3DDevice8 *iface,
2822 const DWORD *declaration, const DWORD *byte_code, DWORD *shader, DWORD usage)
2824 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2825 struct d3d8_vertex_shader *object;
2826 DWORD shader_handle;
2827 DWORD handle;
2828 HRESULT hr;
2830 TRACE("iface %p, declaration %p, byte_code %p, shader %p, usage %#lx.\n",
2831 iface, declaration, byte_code, shader, usage);
2833 if (!(object = calloc(1, sizeof(*object))))
2835 *shader = 0;
2836 return E_OUTOFMEMORY;
2839 wined3d_mutex_lock();
2840 handle = d3d8_allocate_handle(&device->handle_table, object, D3D8_HANDLE_VS);
2841 wined3d_mutex_unlock();
2842 if (handle == D3D8_INVALID_HANDLE)
2844 ERR("Failed to allocate vertex shader handle.\n");
2845 free(object);
2846 *shader = 0;
2847 return E_OUTOFMEMORY;
2850 shader_handle = handle + VS_HIGHESTFIXEDFXF + 1;
2852 hr = d3d8_vertex_shader_init(object, device, declaration, byte_code, shader_handle, usage);
2853 if (FAILED(hr))
2855 WARN("Failed to initialize vertex shader, hr %#lx.\n", hr);
2856 wined3d_mutex_lock();
2857 d3d8_free_handle(&device->handle_table, handle, D3D8_HANDLE_VS);
2858 wined3d_mutex_unlock();
2859 free(object);
2860 *shader = 0;
2861 return hr;
2864 TRACE("Created vertex shader %p (handle %#lx).\n", object, shader_handle);
2865 *shader = shader_handle;
2867 return D3D_OK;
2870 static struct d3d8_vertex_declaration *d3d8_device_get_fvf_declaration(struct d3d8_device *device, DWORD fvf)
2872 struct d3d8_vertex_declaration *d3d8_declaration;
2873 struct FvfToDecl *convertedDecls = device->decls;
2874 int p, low, high; /* deliberately signed */
2875 HRESULT hr;
2877 TRACE("Searching for declaration for fvf %#lx... ", fvf);
2879 low = 0;
2880 high = device->numConvertedDecls - 1;
2881 while (low <= high)
2883 p = (low + high) >> 1;
2884 TRACE("%d ", p);
2886 if (convertedDecls[p].fvf == fvf)
2888 TRACE("found %p\n", convertedDecls[p].declaration);
2889 return convertedDecls[p].declaration;
2892 if (convertedDecls[p].fvf < fvf)
2893 low = p + 1;
2894 else
2895 high = p - 1;
2897 TRACE("not found. Creating and inserting at position %d.\n", low);
2899 if (!(d3d8_declaration = malloc(sizeof(*d3d8_declaration))))
2900 return NULL;
2902 if (FAILED(hr = d3d8_vertex_declaration_init_fvf(d3d8_declaration, device, fvf)))
2904 WARN("Failed to initialize vertex declaration, hr %#lx.\n", hr);
2905 free(d3d8_declaration);
2906 return NULL;
2909 if (device->declArraySize == device->numConvertedDecls)
2911 UINT grow = device->declArraySize / 2;
2913 if (!(convertedDecls = realloc(convertedDecls,
2914 sizeof(*convertedDecls) * (device->numConvertedDecls + grow))))
2916 d3d8_vertex_declaration_destroy(d3d8_declaration);
2917 return NULL;
2919 device->decls = convertedDecls;
2920 device->declArraySize += grow;
2923 memmove(convertedDecls + low + 1, convertedDecls + low,
2924 sizeof(*convertedDecls) * (device->numConvertedDecls - low));
2925 convertedDecls[low].declaration = d3d8_declaration;
2926 convertedDecls[low].fvf = fvf;
2927 ++device->numConvertedDecls;
2929 TRACE("Returning %p. %u decls in array.\n", d3d8_declaration, device->numConvertedDecls);
2931 return d3d8_declaration;
2934 static HRESULT WINAPI d3d8_device_SetVertexShader(IDirect3DDevice8 *iface, DWORD shader)
2936 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2937 struct d3d8_vertex_shader *shader_impl;
2939 TRACE("iface %p, shader %#lx.\n", iface, shader);
2941 if (VS_HIGHESTFIXEDFXF >= shader)
2943 TRACE("Setting FVF, %#lx\n", shader);
2945 wined3d_mutex_lock();
2946 wined3d_stateblock_set_vertex_declaration(device->update_state,
2947 d3d8_device_get_fvf_declaration(device, shader)->wined3d_vertex_declaration);
2948 wined3d_stateblock_set_vertex_shader(device->update_state, NULL);
2949 wined3d_mutex_unlock();
2951 return D3D_OK;
2954 TRACE("Setting shader\n");
2956 wined3d_mutex_lock();
2957 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS)))
2959 WARN("Invalid handle (%#lx) passed.\n", shader);
2960 wined3d_mutex_unlock();
2962 return D3DERR_INVALIDCALL;
2965 wined3d_stateblock_set_vertex_declaration(device->update_state,
2966 shader_impl->vertex_declaration->wined3d_vertex_declaration);
2967 wined3d_stateblock_set_vertex_shader(device->update_state, shader_impl->wined3d_shader);
2968 wined3d_mutex_unlock();
2970 return D3D_OK;
2973 static HRESULT WINAPI d3d8_device_GetVertexShader(IDirect3DDevice8 *iface, DWORD *shader)
2975 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2976 struct wined3d_vertex_declaration *wined3d_declaration;
2977 struct d3d8_vertex_declaration *d3d8_declaration;
2979 TRACE("iface %p, shader %p.\n", iface, shader);
2981 wined3d_mutex_lock();
2982 if ((wined3d_declaration = device->stateblock_state->vertex_declaration))
2984 d3d8_declaration = wined3d_vertex_declaration_get_parent(wined3d_declaration);
2985 *shader = d3d8_declaration->shader_handle;
2987 else
2989 *shader = 0;
2991 wined3d_mutex_unlock();
2993 TRACE("Returning %#lx.\n", *shader);
2995 return D3D_OK;
2998 static HRESULT WINAPI d3d8_device_DeleteVertexShader(IDirect3DDevice8 *iface, DWORD shader)
3000 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3001 struct d3d8_vertex_shader *shader_impl;
3003 TRACE("iface %p, shader %#lx.\n", iface, shader);
3005 wined3d_mutex_lock();
3006 if (!(shader_impl = d3d8_free_handle(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS)))
3008 WARN("Invalid handle (%#lx) passed.\n", shader);
3009 wined3d_mutex_unlock();
3011 return D3DERR_INVALIDCALL;
3014 if (shader_impl->wined3d_shader
3015 && device->stateblock_state->vs == shader_impl->wined3d_shader)
3016 IDirect3DDevice8_SetVertexShader(iface, 0);
3018 wined3d_mutex_unlock();
3020 d3d8_vertex_shader_destroy(shader_impl);
3022 return D3D_OK;
3025 static HRESULT WINAPI d3d8_device_SetVertexShaderConstant(IDirect3DDevice8 *iface,
3026 DWORD start_register, const void *data, DWORD count)
3028 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3029 HRESULT hr;
3031 TRACE("iface %p, start_register %lu, data %p, count %lu.\n",
3032 iface, start_register, data, count);
3034 if (start_register + count > D3D8_MAX_VERTEX_SHADER_CONSTANTF)
3036 WARN("Trying to access %lu constants, but d3d8 only supports %u.\n",
3037 start_register + count, D3D8_MAX_VERTEX_SHADER_CONSTANTF);
3038 return D3DERR_INVALIDCALL;
3041 wined3d_mutex_lock();
3042 hr = wined3d_stateblock_set_vs_consts_f(device->update_state, start_register, count, data);
3043 wined3d_mutex_unlock();
3045 return hr;
3048 static HRESULT WINAPI d3d8_device_GetVertexShaderConstant(IDirect3DDevice8 *iface,
3049 DWORD start_idx, void *constants, DWORD count)
3051 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3052 HRESULT hr;
3054 TRACE("iface %p, start_idx %lu, constants %p, count %lu.\n", iface, start_idx, constants, count);
3056 wined3d_mutex_lock();
3057 hr = wined3d_stateblock_get_vs_consts_f(device->state, start_idx, count, constants);
3058 wined3d_mutex_unlock();
3060 return hr;
3063 static HRESULT WINAPI d3d8_device_GetVertexShaderDeclaration(IDirect3DDevice8 *iface,
3064 DWORD shader, void *data, DWORD *data_size)
3066 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3067 struct d3d8_vertex_declaration *declaration;
3068 struct d3d8_vertex_shader *shader_impl;
3070 TRACE("iface %p, shader %#lx, data %p, data_size %p.\n", iface, shader, data, data_size);
3072 wined3d_mutex_lock();
3073 shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS);
3074 wined3d_mutex_unlock();
3076 if (!shader_impl)
3078 WARN("Invalid handle (%#lx) passed.\n", shader);
3079 return D3DERR_INVALIDCALL;
3081 declaration = shader_impl->vertex_declaration;
3083 if (!data)
3085 *data_size = declaration->elements_size;
3086 return D3D_OK;
3089 /* MSDN claims that if *data_size is smaller than the required size
3090 * we should write the required size and return D3DERR_MOREDATA.
3091 * That's not actually true. */
3092 if (*data_size < declaration->elements_size)
3093 return D3DERR_INVALIDCALL;
3095 memcpy(data, declaration->elements, declaration->elements_size);
3097 return D3D_OK;
3100 static HRESULT WINAPI d3d8_device_GetVertexShaderFunction(IDirect3DDevice8 *iface,
3101 DWORD shader, void *data, DWORD *data_size)
3103 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3104 struct d3d8_vertex_shader *shader_impl = NULL;
3105 HRESULT hr;
3107 TRACE("iface %p, shader %#lx, data %p, data_size %p.\n", iface, shader, data, data_size);
3109 wined3d_mutex_lock();
3110 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS)))
3112 WARN("Invalid handle (%#lx) passed.\n", shader);
3113 wined3d_mutex_unlock();
3115 return D3DERR_INVALIDCALL;
3118 if (!shader_impl->wined3d_shader)
3120 wined3d_mutex_unlock();
3121 *data_size = 0;
3122 return D3D_OK;
3125 hr = wined3d_shader_get_byte_code(shader_impl->wined3d_shader, data, (unsigned int *)data_size);
3126 wined3d_mutex_unlock();
3128 return hr;
3131 static HRESULT WINAPI d3d8_device_SetIndices(IDirect3DDevice8 *iface,
3132 IDirect3DIndexBuffer8 *buffer, UINT base_vertex_idx)
3134 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3135 struct d3d8_indexbuffer *ib = unsafe_impl_from_IDirect3DIndexBuffer8(buffer);
3136 struct wined3d_buffer *wined3d_buffer;
3138 TRACE("iface %p, buffer %p, base_vertex_idx %u.\n", iface, buffer, base_vertex_idx);
3140 if (!ib)
3141 wined3d_buffer = NULL;
3142 else
3143 wined3d_buffer = ib->wined3d_buffer;
3145 /* WineD3D takes an INT(due to d3d9), but d3d8 uses UINTs. Do I have to add a check here that
3146 * the UINT doesn't cause an overflow in the INT? It seems rather unlikely because such large
3147 * vertex buffers can't be created to address them with an index that requires the 32nd bit
3148 * (4 Byte minimum vertex size * 2^31-1 -> 8 gb buffer. The index sign would be the least
3149 * problem)
3151 wined3d_mutex_lock();
3152 wined3d_stateblock_set_base_vertex_index(device->update_state, base_vertex_idx);
3153 wined3d_stateblock_set_index_buffer(device->update_state, wined3d_buffer, ib ? ib->format : WINED3DFMT_UNKNOWN);
3154 if (!device->recording)
3155 device->sysmem_ib = ib && ib->sysmem;
3156 wined3d_mutex_unlock();
3158 return D3D_OK;
3161 static HRESULT WINAPI d3d8_device_GetIndices(IDirect3DDevice8 *iface,
3162 IDirect3DIndexBuffer8 **buffer, UINT *base_vertex_index)
3164 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3165 const struct wined3d_stateblock_state *state;
3166 struct wined3d_buffer *wined3d_buffer;
3167 struct d3d8_indexbuffer *buffer_impl;
3169 TRACE("iface %p, buffer %p, base_vertex_index %p.\n", iface, buffer, base_vertex_index);
3171 if (!buffer)
3172 return D3DERR_INVALIDCALL;
3174 /* The case from UINT to INT is safe because d3d8 will never set negative values */
3175 wined3d_mutex_lock();
3176 state = device->stateblock_state;
3177 *base_vertex_index = state->base_vertex_index;
3178 if ((wined3d_buffer = state->index_buffer))
3180 buffer_impl = wined3d_buffer_get_parent(wined3d_buffer);
3181 *buffer = &buffer_impl->IDirect3DIndexBuffer8_iface;
3182 IDirect3DIndexBuffer8_AddRef(*buffer);
3184 else
3186 *buffer = NULL;
3188 wined3d_mutex_unlock();
3190 return D3D_OK;
3193 static HRESULT WINAPI d3d8_device_CreatePixelShader(IDirect3DDevice8 *iface,
3194 const DWORD *byte_code, DWORD *shader)
3196 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3197 struct d3d8_pixel_shader *object;
3198 DWORD shader_handle;
3199 DWORD handle;
3200 HRESULT hr;
3202 TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
3204 if (!shader)
3205 return D3DERR_INVALIDCALL;
3207 if (!(object = calloc(1, sizeof(*object))))
3208 return E_OUTOFMEMORY;
3210 wined3d_mutex_lock();
3211 handle = d3d8_allocate_handle(&device->handle_table, object, D3D8_HANDLE_PS);
3212 wined3d_mutex_unlock();
3213 if (handle == D3D8_INVALID_HANDLE)
3215 ERR("Failed to allocate pixel shader handle.\n");
3216 free(object);
3217 return E_OUTOFMEMORY;
3220 shader_handle = handle + VS_HIGHESTFIXEDFXF + 1;
3222 hr = d3d8_pixel_shader_init(object, device, byte_code, shader_handle);
3223 if (FAILED(hr))
3225 WARN("Failed to initialize pixel shader, hr %#lx.\n", hr);
3226 wined3d_mutex_lock();
3227 d3d8_free_handle(&device->handle_table, handle, D3D8_HANDLE_PS);
3228 wined3d_mutex_unlock();
3229 free(object);
3230 *shader = 0;
3231 return hr;
3234 TRACE("Created pixel shader %p (handle %#lx).\n", object, shader_handle);
3235 *shader = shader_handle;
3237 return D3D_OK;
3240 static HRESULT WINAPI d3d8_device_SetPixelShader(IDirect3DDevice8 *iface, DWORD shader)
3242 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3243 struct d3d8_pixel_shader *shader_impl;
3245 TRACE("iface %p, shader %#lx.\n", iface, shader);
3247 wined3d_mutex_lock();
3249 if (!shader)
3251 wined3d_stateblock_set_pixel_shader(device->update_state, NULL);
3252 wined3d_mutex_unlock();
3253 return D3D_OK;
3256 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS)))
3258 WARN("Invalid handle (%#lx) passed.\n", shader);
3259 wined3d_mutex_unlock();
3260 return D3DERR_INVALIDCALL;
3263 TRACE("Setting shader %p.\n", shader_impl);
3264 wined3d_stateblock_set_pixel_shader(device->update_state, shader_impl->wined3d_shader);
3265 wined3d_mutex_unlock();
3267 return D3D_OK;
3270 static HRESULT WINAPI d3d8_device_GetPixelShader(IDirect3DDevice8 *iface, DWORD *shader)
3272 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3273 struct wined3d_shader *object;
3275 TRACE("iface %p, shader %p.\n", iface, shader);
3277 if (!shader)
3278 return D3DERR_INVALIDCALL;
3280 wined3d_mutex_lock();
3281 if ((object = device->stateblock_state->ps))
3283 struct d3d8_pixel_shader *d3d8_shader;
3284 d3d8_shader = wined3d_shader_get_parent(object);
3285 *shader = d3d8_shader->handle;
3287 else
3289 *shader = 0;
3291 wined3d_mutex_unlock();
3293 TRACE("Returning %#lx.\n", *shader);
3295 return D3D_OK;
3298 static HRESULT WINAPI d3d8_device_DeletePixelShader(IDirect3DDevice8 *iface, DWORD shader)
3300 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3301 struct d3d8_pixel_shader *shader_impl;
3303 TRACE("iface %p, shader %#lx.\n", iface, shader);
3305 wined3d_mutex_lock();
3307 if (!(shader_impl = d3d8_free_handle(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS)))
3309 WARN("Invalid handle (%#lx) passed.\n", shader);
3310 wined3d_mutex_unlock();
3311 return D3DERR_INVALIDCALL;
3314 if (device->stateblock_state->ps == shader_impl->wined3d_shader)
3315 IDirect3DDevice8_SetPixelShader(iface, 0);
3317 wined3d_mutex_unlock();
3319 d3d8_pixel_shader_destroy(shader_impl);
3321 return D3D_OK;
3324 static HRESULT WINAPI d3d8_device_SetPixelShaderConstant(IDirect3DDevice8 *iface,
3325 DWORD start_register, const void *data, DWORD count)
3327 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3328 HRESULT hr;
3330 TRACE("iface %p, start_register %lu, data %p, count %lu.\n", iface, start_register, data, count);
3332 wined3d_mutex_lock();
3333 hr = wined3d_stateblock_set_ps_consts_f(device->update_state, start_register, count, data);
3334 wined3d_mutex_unlock();
3336 return hr;
3339 static HRESULT WINAPI d3d8_device_GetPixelShaderConstant(IDirect3DDevice8 *iface,
3340 DWORD start_idx, void *constants, DWORD count)
3342 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3343 HRESULT hr;
3345 TRACE("iface %p, start_idx %lu, constants %p, count %lu.\n", iface, start_idx, constants, count);
3347 if (!wined3d_bound_range(start_idx, count, D3D8_MAX_PIXEL_SHADER_CONSTANTF))
3348 return WINED3DERR_INVALIDCALL;
3350 wined3d_mutex_lock();
3351 hr = wined3d_stateblock_get_ps_consts_f(device->state, start_idx, count, constants);
3352 wined3d_mutex_unlock();
3354 return hr;
3357 static HRESULT WINAPI d3d8_device_GetPixelShaderFunction(IDirect3DDevice8 *iface,
3358 DWORD shader, void *data, DWORD *data_size)
3360 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3361 struct d3d8_pixel_shader *shader_impl = NULL;
3362 HRESULT hr;
3364 TRACE("iface %p, shader %#lx, data %p, data_size %p.\n", iface, shader, data, data_size);
3366 wined3d_mutex_lock();
3367 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS)))
3369 WARN("Invalid handle (%#lx) passed.\n", shader);
3370 wined3d_mutex_unlock();
3372 return D3DERR_INVALIDCALL;
3375 hr = wined3d_shader_get_byte_code(shader_impl->wined3d_shader, data, (unsigned int *)data_size);
3376 wined3d_mutex_unlock();
3378 return hr;
3381 static HRESULT WINAPI d3d8_device_DrawRectPatch(IDirect3DDevice8 *iface, UINT handle,
3382 const float *segment_count, const D3DRECTPATCH_INFO *patch_info)
3384 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
3385 iface, handle, segment_count, patch_info);
3386 return D3D_OK;
3389 static HRESULT WINAPI d3d8_device_DrawTriPatch(IDirect3DDevice8 *iface, UINT handle,
3390 const float *segment_count, const D3DTRIPATCH_INFO *patch_info)
3392 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
3393 iface, handle, segment_count, patch_info);
3394 return D3D_OK;
3397 static HRESULT WINAPI d3d8_device_DeletePatch(IDirect3DDevice8 *iface, UINT handle)
3399 FIXME("iface %p, handle %#x unimplemented.\n", iface, handle);
3400 return D3DERR_INVALIDCALL;
3403 static HRESULT WINAPI d3d8_device_SetStreamSource(IDirect3DDevice8 *iface,
3404 UINT stream_idx, IDirect3DVertexBuffer8 *buffer, UINT stride)
3406 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3407 struct d3d8_vertexbuffer *buffer_impl = unsafe_impl_from_IDirect3DVertexBuffer8(buffer);
3408 const struct wined3d_stateblock_state *state;
3409 struct wined3d_buffer *wined3d_buffer;
3410 HRESULT hr;
3412 TRACE("iface %p, stream_idx %u, buffer %p, stride %u.\n",
3413 iface, stream_idx, buffer, stride);
3415 if (stream_idx >= ARRAY_SIZE(state->streams))
3417 WARN("Stream index %u out of range.\n", stream_idx);
3418 return D3DERR_INVALIDCALL;
3421 wined3d_mutex_lock();
3423 if (!buffer_impl)
3425 wined3d_buffer = NULL;
3426 state = device->stateblock_state;
3427 stride = state->streams[stream_idx].stride;
3429 else if (buffer_impl->draw_buffer)
3430 wined3d_buffer = buffer_impl->draw_buffer;
3431 else
3432 wined3d_buffer = buffer_impl->wined3d_buffer;
3434 hr = wined3d_stateblock_set_stream_source(device->update_state, stream_idx, wined3d_buffer, 0, stride);
3435 if (SUCCEEDED(hr) && !device->recording)
3437 if (buffer_impl && buffer_impl->draw_buffer)
3438 device->sysmem_vb |= (1u << stream_idx);
3439 else
3440 device->sysmem_vb &= ~(1u << stream_idx);
3443 wined3d_mutex_unlock();
3445 return hr;
3448 static HRESULT WINAPI d3d8_device_GetStreamSource(IDirect3DDevice8 *iface,
3449 UINT stream_idx, IDirect3DVertexBuffer8 **buffer, UINT *stride)
3451 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
3452 const struct wined3d_stateblock_state *state;
3453 const struct wined3d_stream_state *stream;
3454 struct d3d8_vertexbuffer *buffer_impl;
3456 TRACE("iface %p, stream_idx %u, buffer %p, stride %p.\n",
3457 iface, stream_idx, buffer, stride);
3459 if (!buffer)
3460 return D3DERR_INVALIDCALL;
3462 if (stream_idx >= ARRAY_SIZE(state->streams))
3464 WARN("Stream index %u out of range.\n", stream_idx);
3465 return D3DERR_INVALIDCALL;
3468 wined3d_mutex_lock();
3469 state = device->stateblock_state;
3470 stream = &state->streams[stream_idx];
3471 if (stream->buffer)
3473 buffer_impl = wined3d_buffer_get_parent(stream->buffer);
3474 *buffer = &buffer_impl->IDirect3DVertexBuffer8_iface;
3475 IDirect3DVertexBuffer8_AddRef(*buffer);
3477 else
3478 *buffer = NULL;
3479 *stride = stream->stride;
3480 wined3d_mutex_unlock();
3482 return D3D_OK;
3485 static const struct IDirect3DDevice8Vtbl d3d8_device_vtbl =
3487 d3d8_device_QueryInterface,
3488 d3d8_device_AddRef,
3489 d3d8_device_Release,
3490 d3d8_device_TestCooperativeLevel,
3491 d3d8_device_GetAvailableTextureMem,
3492 d3d8_device_ResourceManagerDiscardBytes,
3493 d3d8_device_GetDirect3D,
3494 d3d8_device_GetDeviceCaps,
3495 d3d8_device_GetDisplayMode,
3496 d3d8_device_GetCreationParameters,
3497 d3d8_device_SetCursorProperties,
3498 d3d8_device_SetCursorPosition,
3499 d3d8_device_ShowCursor,
3500 d3d8_device_CreateAdditionalSwapChain,
3501 d3d8_device_Reset,
3502 d3d8_device_Present,
3503 d3d8_device_GetBackBuffer,
3504 d3d8_device_GetRasterStatus,
3505 d3d8_device_SetGammaRamp,
3506 d3d8_device_GetGammaRamp,
3507 d3d8_device_CreateTexture,
3508 d3d8_device_CreateVolumeTexture,
3509 d3d8_device_CreateCubeTexture,
3510 d3d8_device_CreateVertexBuffer,
3511 d3d8_device_CreateIndexBuffer,
3512 d3d8_device_CreateRenderTarget,
3513 d3d8_device_CreateDepthStencilSurface,
3514 d3d8_device_CreateImageSurface,
3515 d3d8_device_CopyRects,
3516 d3d8_device_UpdateTexture,
3517 d3d8_device_GetFrontBuffer,
3518 d3d8_device_SetRenderTarget,
3519 d3d8_device_GetRenderTarget,
3520 d3d8_device_GetDepthStencilSurface,
3521 d3d8_device_BeginScene,
3522 d3d8_device_EndScene,
3523 d3d8_device_Clear,
3524 d3d8_device_SetTransform,
3525 d3d8_device_GetTransform,
3526 d3d8_device_MultiplyTransform,
3527 d3d8_device_SetViewport,
3528 d3d8_device_GetViewport,
3529 d3d8_device_SetMaterial,
3530 d3d8_device_GetMaterial,
3531 d3d8_device_SetLight,
3532 d3d8_device_GetLight,
3533 d3d8_device_LightEnable,
3534 d3d8_device_GetLightEnable,
3535 d3d8_device_SetClipPlane,
3536 d3d8_device_GetClipPlane,
3537 d3d8_device_SetRenderState,
3538 d3d8_device_GetRenderState,
3539 d3d8_device_BeginStateBlock,
3540 d3d8_device_EndStateBlock,
3541 d3d8_device_ApplyStateBlock,
3542 d3d8_device_CaptureStateBlock,
3543 d3d8_device_DeleteStateBlock,
3544 d3d8_device_CreateStateBlock,
3545 d3d8_device_SetClipStatus,
3546 d3d8_device_GetClipStatus,
3547 d3d8_device_GetTexture,
3548 d3d8_device_SetTexture,
3549 d3d8_device_GetTextureStageState,
3550 d3d8_device_SetTextureStageState,
3551 d3d8_device_ValidateDevice,
3552 d3d8_device_GetInfo,
3553 d3d8_device_SetPaletteEntries,
3554 d3d8_device_GetPaletteEntries,
3555 d3d8_device_SetCurrentTexturePalette,
3556 d3d8_device_GetCurrentTexturePalette,
3557 d3d8_device_DrawPrimitive,
3558 d3d8_device_DrawIndexedPrimitive,
3559 d3d8_device_DrawPrimitiveUP,
3560 d3d8_device_DrawIndexedPrimitiveUP,
3561 d3d8_device_ProcessVertices,
3562 d3d8_device_CreateVertexShader,
3563 d3d8_device_SetVertexShader,
3564 d3d8_device_GetVertexShader,
3565 d3d8_device_DeleteVertexShader,
3566 d3d8_device_SetVertexShaderConstant,
3567 d3d8_device_GetVertexShaderConstant,
3568 d3d8_device_GetVertexShaderDeclaration,
3569 d3d8_device_GetVertexShaderFunction,
3570 d3d8_device_SetStreamSource,
3571 d3d8_device_GetStreamSource,
3572 d3d8_device_SetIndices,
3573 d3d8_device_GetIndices,
3574 d3d8_device_CreatePixelShader,
3575 d3d8_device_SetPixelShader,
3576 d3d8_device_GetPixelShader,
3577 d3d8_device_DeletePixelShader,
3578 d3d8_device_SetPixelShaderConstant,
3579 d3d8_device_GetPixelShaderConstant,
3580 d3d8_device_GetPixelShaderFunction,
3581 d3d8_device_DrawRectPatch,
3582 d3d8_device_DrawTriPatch,
3583 d3d8_device_DeletePatch,
3586 static inline struct d3d8_device *device_from_device_parent(struct wined3d_device_parent *device_parent)
3588 return CONTAINING_RECORD(device_parent, struct d3d8_device, device_parent);
3591 static void CDECL device_parent_wined3d_device_created(struct wined3d_device_parent *device_parent,
3592 struct wined3d_device *device)
3594 TRACE("device_parent %p, device %p\n", device_parent, device);
3597 static void CDECL device_parent_mode_changed(struct wined3d_device_parent *device_parent)
3599 TRACE("device_parent %p.\n", device_parent);
3602 static void CDECL device_parent_activate(struct wined3d_device_parent *device_parent, BOOL activate)
3604 struct d3d8_device *device = device_from_device_parent(device_parent);
3606 TRACE("device_parent %p, activate %#x.\n", device_parent, activate);
3608 if (!activate)
3609 InterlockedCompareExchange(&device->device_state, D3D8_DEVICE_STATE_LOST, D3D8_DEVICE_STATE_OK);
3610 else
3611 InterlockedCompareExchange(&device->device_state, D3D8_DEVICE_STATE_NOT_RESET, D3D8_DEVICE_STATE_LOST);
3614 static HRESULT CDECL device_parent_texture_sub_resource_created(struct wined3d_device_parent *device_parent,
3615 enum wined3d_resource_type type, struct wined3d_texture *wined3d_texture, unsigned int sub_resource_idx,
3616 void **parent, const struct wined3d_parent_ops **parent_ops)
3618 TRACE("device_parent %p, type %#x, wined3d_texture %p, sub_resource_idx %u, parent %p, parent_ops %p.\n",
3619 device_parent, type, wined3d_texture, sub_resource_idx, parent, parent_ops);
3621 if (type == WINED3D_RTYPE_TEXTURE_3D)
3623 struct d3d8_volume *d3d_volume;
3625 if (!(d3d_volume = calloc(1, sizeof(*d3d_volume))))
3626 return E_OUTOFMEMORY;
3628 volume_init(d3d_volume, wined3d_texture, sub_resource_idx, parent_ops);
3629 *parent = d3d_volume;
3630 TRACE("Created volume %p.\n", d3d_volume);
3632 else if (type != WINED3D_RTYPE_TEXTURE_2D)
3634 ERR("Unhandled resource type %#x.\n", type);
3635 return E_FAIL;
3638 return D3D_OK;
3641 static const struct wined3d_device_parent_ops d3d8_wined3d_device_parent_ops =
3643 device_parent_wined3d_device_created,
3644 device_parent_mode_changed,
3645 device_parent_activate,
3646 device_parent_texture_sub_resource_created,
3649 static void setup_fpu(void)
3651 #if defined(__i386__) && defined(_MSC_VER)
3652 WORD cw;
3653 __asm fnstcw cw;
3654 cw = (cw & ~0xf3f) | 0x3f;
3655 __asm fldcw cw;
3656 #elif defined(__i386__) || (defined(__x86_64__) && !defined(__arm64ec__) && (defined(__GNUC__) || defined(__clang__)))
3657 WORD cw;
3658 __asm__ volatile ("fnstcw %0" : "=m" (cw));
3659 cw = (cw & ~0xf3f) | 0x3f;
3660 __asm__ volatile ("fldcw %0" : : "m" (cw));
3661 #else
3662 FIXME("FPU setup not implemented for this platform.\n");
3663 #endif
3666 HRESULT device_init(struct d3d8_device *device, struct d3d8 *parent, struct wined3d *wined3d, UINT adapter,
3667 D3DDEVTYPE device_type, HWND focus_window, DWORD flags, D3DPRESENT_PARAMETERS *parameters)
3669 struct wined3d_swapchain_desc swapchain_desc;
3670 struct wined3d_swapchain *wined3d_swapchain;
3671 struct wined3d_adapter *wined3d_adapter;
3672 struct wined3d_output *wined3d_output;
3673 struct d3d8_swapchain *d3d_swapchain;
3674 struct wined3d_caps caps;
3675 unsigned int output_idx;
3676 HRESULT hr;
3678 static const enum wined3d_feature_level feature_levels[] =
3680 WINED3D_FEATURE_LEVEL_8,
3681 WINED3D_FEATURE_LEVEL_7,
3682 WINED3D_FEATURE_LEVEL_6,
3683 WINED3D_FEATURE_LEVEL_5,
3686 output_idx = adapter;
3687 if (output_idx >= parent->wined3d_output_count)
3688 return D3DERR_INVALIDCALL;
3690 device->IDirect3DDevice8_iface.lpVtbl = &d3d8_device_vtbl;
3691 device->device_parent.ops = &d3d8_wined3d_device_parent_ops;
3692 device->adapter_ordinal = adapter;
3693 device->ref = 1;
3694 if (!(device->handle_table.entries = calloc(D3D8_INITIAL_HANDLE_TABLE_SIZE,
3695 sizeof(*device->handle_table.entries))))
3697 ERR("Failed to allocate handle table memory.\n");
3698 return E_OUTOFMEMORY;
3700 device->handle_table.table_size = D3D8_INITIAL_HANDLE_TABLE_SIZE;
3702 if (!(flags & D3DCREATE_FPU_PRESERVE)) setup_fpu();
3704 wined3d_mutex_lock();
3705 wined3d_output = parent->wined3d_outputs[output_idx];
3706 wined3d_adapter = wined3d_output_get_adapter(wined3d_output);
3707 if (FAILED(hr = wined3d_device_create(wined3d, wined3d_adapter, wined3d_device_type_from_d3d(device_type),
3708 focus_window, flags, 4, feature_levels, ARRAY_SIZE(feature_levels),
3709 &device->device_parent, &device->wined3d_device)))
3711 WARN("Failed to create wined3d device, hr %#lx.\n", hr);
3712 wined3d_mutex_unlock();
3713 free(device->handle_table.entries);
3714 return hr;
3717 device->immediate_context = wined3d_device_get_immediate_context(device->wined3d_device);
3718 wined3d_get_device_caps(wined3d_adapter, wined3d_device_type_from_d3d(device_type), &caps);
3719 device->max_user_clip_planes = caps.MaxUserClipPlanes;
3720 device->vs_uniform_count = caps.MaxVertexShaderConst;
3722 if (FAILED(hr = wined3d_stateblock_create(device->wined3d_device, NULL, WINED3D_SBT_PRIMARY, &device->state)))
3724 ERR("Failed to create primary stateblock, hr %#lx.\n", hr);
3725 wined3d_device_decref(device->wined3d_device);
3726 wined3d_mutex_unlock();
3727 return hr;
3729 device->stateblock_state = wined3d_stateblock_get_state(device->state);
3730 device->update_state = device->state;
3732 wined3d_streaming_buffer_init(&device->vertex_buffer, WINED3D_BIND_VERTEX_BUFFER);
3733 wined3d_streaming_buffer_init(&device->index_buffer, WINED3D_BIND_INDEX_BUFFER);
3735 if (!parameters->Windowed)
3737 if (!focus_window)
3738 focus_window = parameters->hDeviceWindow;
3739 if (FAILED(hr = wined3d_device_acquire_focus_window(device->wined3d_device, focus_window)))
3741 ERR("Failed to acquire focus window, hr %#lx.\n", hr);
3742 wined3d_device_decref(device->wined3d_device);
3743 wined3d_mutex_unlock();
3744 free(device->handle_table.entries);
3745 return hr;
3749 if (flags & D3DCREATE_MULTITHREADED)
3750 wined3d_device_set_multithreaded(device->wined3d_device);
3752 if (!wined3d_swapchain_desc_from_d3d8(&swapchain_desc, wined3d_output, parameters))
3754 wined3d_device_release_focus_window(device->wined3d_device);
3755 wined3d_device_decref(device->wined3d_device);
3756 wined3d_mutex_unlock();
3757 free(device->handle_table.entries);
3758 return D3DERR_INVALIDCALL;
3760 swapchain_desc.flags |= WINED3D_SWAPCHAIN_IMPLICIT;
3762 if (FAILED(hr = d3d8_swapchain_create(device, &swapchain_desc,
3763 wined3dswapinterval_from_d3d(parameters->FullScreen_PresentationInterval), &d3d_swapchain)))
3765 WARN("Failed to create implicit swapchain, hr %#lx.\n", hr);
3766 wined3d_device_release_focus_window(device->wined3d_device);
3767 wined3d_device_decref(device->wined3d_device);
3768 wined3d_mutex_unlock();
3769 free(device->handle_table.entries);
3770 return hr;
3773 wined3d_swapchain = d3d_swapchain->wined3d_swapchain;
3774 wined3d_swapchain_incref(wined3d_swapchain);
3775 IDirect3DSwapChain8_Release(&d3d_swapchain->IDirect3DSwapChain8_iface);
3777 wined3d_stateblock_set_render_state(device->state, WINED3D_RS_ZENABLE,
3778 !!swapchain_desc.enable_auto_depth_stencil);
3779 wined3d_stateblock_set_render_state(device->state, WINED3D_RS_POINTSIZE_MIN, 0);
3780 device_reset_viewport_state(device);
3781 wined3d_mutex_unlock();
3783 present_parameters_from_wined3d_swapchain_desc(parameters,
3784 &swapchain_desc, parameters->FullScreen_PresentationInterval);
3786 device->declArraySize = 16;
3787 if (!(device->decls = malloc(device->declArraySize * sizeof(*device->decls))))
3789 ERR("Failed to allocate FVF vertex declaration map memory.\n");
3790 hr = E_OUTOFMEMORY;
3791 goto err;
3794 device->implicit_swapchain = wined3d_swapchain;
3796 device->d3d_parent = parent;
3797 IDirect3D8_AddRef(&parent->IDirect3D8_iface);
3799 return D3D_OK;
3801 err:
3802 wined3d_mutex_lock();
3803 wined3d_swapchain_decref(wined3d_swapchain);
3804 wined3d_device_release_focus_window(device->wined3d_device);
3805 wined3d_device_decref(device->wined3d_device);
3806 wined3d_mutex_unlock();
3807 free(device->handle_table.entries);
3808 return hr;