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
;
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
;
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
;
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
;
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
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
);
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
;
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;
179 FIXME("Unhandled primitive type %#x.\n", primitive_type
);
184 static D3DSWAPEFFECT
d3dswapeffect_from_wined3dswapeffect(enum wined3d_swap_effect 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
;
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
)
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
;
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
)
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
;
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
);
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
);
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
:
291 WARN("Invalid presentation interval %#x.\n",
292 present_parameters
->FullScreen_PresentationInterval
);
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
);
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);
438 caps
->PixelShaderVersion
= D3DPS_VERSION(0, 0);
439 if (caps
->VertexShaderVersion
)
440 caps
->VertexShaderVersion
= D3DVS_VERSION(1, 1);
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
)
456 return WINED3D_RS_DEPTHBIAS
;
457 case D3DRS_EDGEANTIALIAS
:
458 return WINED3D_RS_ANTIALIASEDLINEENABLE
;
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
;
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
;
491 if (!(t
->entry_count
< t
->table_size
))
494 UINT new_size
= t
->table_size
+ (t
->table_size
>> 1);
495 struct d3d8_handle_entry
*new_entries
;
497 if (!(new_entries
= heap_realloc(t
->entries
, new_size
* sizeof(*t
->entries
))))
499 ERR("Failed to grow the handle table.\n");
500 return D3D8_INVALID_HANDLE
;
502 t
->entries
= new_entries
;
503 t
->table_size
= new_size
;
506 entry
= &t
->entries
[t
->entry_count
];
507 entry
->object
= object
;
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
;
518 if (handle
== D3D8_INVALID_HANDLE
|| handle
>= t
->entry_count
)
520 WARN("Invalid handle %lu passed.\n", handle
);
524 entry
= &t
->entries
[handle
];
525 if (entry
->type
!= type
)
527 WARN("Handle %lu(%p) is not of type %#x.\n", handle
, entry
, type
);
531 object
= entry
->object
;
532 entry
->object
= t
->free_entries
;
533 entry
->type
= D3D8_HANDLE_FREE
;
534 t
->free_entries
= entry
;
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
);
549 entry
= &t
->entries
[handle
];
550 if (entry
->type
!= type
)
552 WARN("Handle %lu(%p) is not of type %#x.\n", handle
, entry
, type
);
556 return entry
->object
;
559 static void device_reset_viewport_state(struct d3d8_device
*device
)
561 struct wined3d_viewport vp
;
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
);
583 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid
));
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
);
599 static ULONG WINAPI
d3d8_device_Release(IDirect3DDevice8
*iface
)
601 struct d3d8_device
*device
= impl_from_IDirect3DDevice8(iface
);
604 if (device
->in_destruction
)
607 ref
= InterlockedDecrement(&device
->ref
);
609 TRACE("%p decreasing refcount to %lu.\n", iface
, ref
);
613 IDirect3D8
*parent
= &device
->d3d_parent
->IDirect3D8_iface
;
616 TRACE("Releasing wined3d device %p.\n", device
->wined3d_device
);
618 wined3d_mutex_lock();
620 device
->in_destruction
= TRUE
;
622 for (i
= 0; i
< device
->numConvertedDecls
; ++i
)
624 d3d8_vertex_declaration_destroy(device
->decls
[i
].declaration
);
626 heap_free(device
->decls
);
628 wined3d_streaming_buffer_cleanup(&device
->vertex_buffer
);
629 wined3d_streaming_buffer_cleanup(&device
->index_buffer
);
631 if (device
->recording
)
632 wined3d_stateblock_decref(device
->recording
);
633 wined3d_stateblock_decref(device
->state
);
635 wined3d_swapchain_decref(device
->implicit_swapchain
);
636 wined3d_device_release_focus_window(device
->wined3d_device
);
637 wined3d_device_decref(device
->wined3d_device
);
638 heap_free(device
->handle_table
.entries
);
641 wined3d_mutex_unlock();
643 IDirect3D8_Release(parent
);
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
)
659 case D3D8_DEVICE_STATE_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
);
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();
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
);
689 FIXME("Byte count ignored.\n");
691 wined3d_mutex_lock();
692 wined3d_device_evict_managed_resources(device
->wined3d_device
);
693 wined3d_mutex_unlock();
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
);
705 return D3DERR_INVALIDCALL
;
707 return IDirect3D8_QueryInterface(&device
->d3d_parent
->IDirect3D8_iface
, &IID_IDirect3D8
,
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
;
717 TRACE("iface %p, caps %p.\n", iface
, 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
);
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
;
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();
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
);
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
;
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
;
779 TRACE("iface %p, hotspot_x %u, hotspot_y %u, bitmap %p.\n",
780 iface
, hotspot_x
, hotspot_y
, 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
);
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
);
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();
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
);
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();
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
;
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
);
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
],
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
);
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
;
901 wined3d_resource_get_desc(resource
, &desc
);
902 if ((desc
.access
& WINED3D_RESOURCE_ACCESS_CPU
) || (desc
.usage
& WINED3DUSAGE_MANAGED
))
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
)
913 if (desc
.bind_flags
& WINED3D_BIND_INDEX_BUFFER
)
915 index_buffer
= wined3d_resource_get_parent(resource
);
916 if (index_buffer
&& index_buffer
->sysmem
)
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
)
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
;
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
;
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
;
1020 device
->device_state
= D3D8_DEVICE_STATE_NOT_RESET
;
1022 wined3d_mutex_unlock();
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();
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();
1076 static HRESULT WINAPI
d3d8_device_GetRasterStatus(IDirect3DDevice8
*iface
, D3DRASTER_STATUS
*raster_status
)
1078 struct d3d8_device
*device
= impl_from_IDirect3DDevice8(iface
);
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();
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
;
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
);
1127 return D3DERR_INVALIDCALL
;
1130 if (!(object
= heap_alloc_zero(sizeof(*object
))))
1131 return D3DERR_OUTOFVIDEOMEMORY
;
1133 hr
= d3d8_texture_2d_init(object
, device
, width
, height
, levels
, usage
, format
, pool
);
1136 WARN("Failed to initialize texture, hr %#lx.\n", 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
;
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
;
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
);
1169 return D3DERR_INVALIDCALL
;
1172 if (!(object
= heap_alloc_zero(sizeof(*object
))))
1173 return D3DERR_OUTOFVIDEOMEMORY
;
1175 hr
= d3d8_texture_3d_init(object
, device
, width
, height
, depth
, levels
, usage
, format
, pool
);
1178 WARN("Failed to initialize volume texture, hr %#lx.\n", hr
);
1183 TRACE("Created volume texture %p.\n", object
);
1184 *texture
= (IDirect3DVolumeTexture8
*)&object
->IDirect3DBaseTexture8_iface
;
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
;
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
);
1201 return D3DERR_INVALIDCALL
;
1204 if (!(object
= heap_alloc_zero(sizeof(*object
))))
1205 return D3DERR_OUTOFVIDEOMEMORY
;
1207 hr
= d3d8_texture_cube_init(object
, device
, edge_length
, levels
, usage
, format
, pool
);
1210 WARN("Failed to initialize cube texture, hr %#lx.\n", 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
;
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
;
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
= heap_alloc_zero(sizeof(*object
))))
1242 return D3DERR_OUTOFVIDEOMEMORY
;
1244 hr
= vertexbuffer_init(object
, device
, size
, usage
, fvf
, pool
);
1247 WARN("Failed to initialize vertex buffer, hr %#lx.\n", hr
);
1252 TRACE("Created vertex buffer %p.\n", object
);
1253 *buffer
= &object
->IDirect3DVertexBuffer8_iface
;
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
;
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
= heap_alloc_zero(sizeof(*object
))))
1269 return D3DERR_OUTOFVIDEOMEMORY
;
1271 hr
= indexbuffer_init(object
, device
, size
, usage
, format
, pool
);
1274 WARN("Failed to initialize index buffer, hr %#lx.\n", hr
);
1279 TRACE("Created index buffer %p.\n", object
);
1280 *buffer
= &object
->IDirect3DIndexBuffer8_iface
;
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
;
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;
1303 desc
.bind_flags
= bind_flags
;
1304 desc
.access
= access
;
1306 desc
.height
= height
;
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
);
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();
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
);
1347 return D3DERR_INVALIDCALL
;
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
);
1368 return D3DERR_INVALIDCALL
;
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
);
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
;
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
);
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
);
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();
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
;
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();
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
);
1514 TRACE("iface %p, dst_surface %p.\n", iface
, 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();
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();
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 */
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
);
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
);
1594 rtv
= render_target
? d3d8_surface_acquire_rendertarget_view(rt_impl
) : NULL
;
1597 if (SUCCEEDED(hr
= wined3d_device_context_set_rendertarget_views(device
->immediate_context
,
1599 device_reset_viewport_state(device
);
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();
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
;
1618 TRACE("iface %p, render_target %p.\n", iface
, 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
);
1635 ERR("Failed to get wined3d render target.\n");
1636 *render_target
= NULL
;
1637 hr
= D3DERR_NOTFOUND
;
1639 wined3d_mutex_unlock();
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
);
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
);
1667 hr
= D3DERR_NOTFOUND
;
1668 *depth_stencil
= NULL
;
1670 wined3d_mutex_unlock();
1675 static HRESULT WINAPI
d3d8_device_BeginScene(IDirect3DDevice8
*iface
)
1677 struct d3d8_device
*device
= impl_from_IDirect3DDevice8(iface
);
1680 TRACE("iface %p.\n", iface
);
1682 wined3d_mutex_lock();
1683 hr
= wined3d_device_begin_scene(device
->wined3d_device
);
1684 wined3d_mutex_unlock();
1689 static HRESULT WINAPI DECLSPEC_HOTPATCH
d3d8_device_EndScene(IDirect3DDevice8
*iface
)
1691 struct d3d8_device
*device
= impl_from_IDirect3DDevice8(iface
);
1694 TRACE("iface %p.\n", iface
);
1696 wined3d_mutex_lock();
1697 hr
= wined3d_device_end_scene(device
->wined3d_device
);
1698 wined3d_mutex_unlock();
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
);
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
);
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();
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();
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();
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();
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
;
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();
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
;
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();
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();
1869 static HRESULT WINAPI
d3d8_device_SetLight(IDirect3DDevice8
*iface
, DWORD index
, const D3DLIGHT8
*light
)
1871 struct d3d8_device
*device
= impl_from_IDirect3DDevice8(iface
);
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();
1884 static HRESULT WINAPI
d3d8_device_GetLight(IDirect3DDevice8
*iface
, DWORD index
, D3DLIGHT8
*light
)
1886 struct d3d8_device
*device
= impl_from_IDirect3DDevice8(iface
);
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();
1900 static HRESULT WINAPI
d3d8_device_LightEnable(IDirect3DDevice8
*iface
, DWORD index
, BOOL enable
)
1902 struct d3d8_device
*device
= impl_from_IDirect3DDevice8(iface
);
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();
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
;
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();
1929 static HRESULT WINAPI
d3d8_device_SetClipPlane(IDirect3DDevice8
*iface
, DWORD index
, const float *plane
)
1931 struct d3d8_device
*device
= impl_from_IDirect3DDevice8(iface
);
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();
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();
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]))
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
)
1982 if (!(wined3d_dsv
= wined3d_device_context_get_depth_stencil_view(device
->immediate_context
)))
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();
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
;
2019 *value
= device_state
->rs
[WINED3D_RS_DEPTHBIAS
];
2023 *value
= device_state
->rs
[state
];
2025 wined3d_mutex_unlock();
2030 static HRESULT WINAPI
d3d8_device_BeginStateBlock(IDirect3DDevice8
*iface
)
2032 struct d3d8_device
*device
= impl_from_IDirect3DDevice8(iface
);
2033 struct wined3d_stateblock
*stateblock
;
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();
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();
2088 TRACE("Returning %#lx (%p).\n", *token
, stateblock
);
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
;
2103 TRACE("iface %p, token %#lx.\n", iface
, token
);
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
);
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
)))
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();
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
);
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();
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
);
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();
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
;
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
);
2220 wined3d_mutex_unlock();
2221 ERR("Failed to create the state block, hr %#lx.\n", 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();
2238 TRACE("Returning %#lx (%p).\n", *handle
, stateblock
);
2243 static HRESULT WINAPI
d3d8_device_SetClipStatus(IDirect3DDevice8
*iface
, const D3DCLIPSTATUS8
*clip_status
)
2245 struct d3d8_device
*device
= impl_from_IDirect3DDevice8(iface
);
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();
2258 static HRESULT WINAPI
d3d8_device_GetClipStatus(IDirect3DDevice8
*iface
, D3DCLIPSTATUS8
*clip_status
)
2260 struct d3d8_device
*device
= impl_from_IDirect3DDevice8(iface
);
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();
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
);
2281 return D3DERR_INVALIDCALL
;
2283 if (stage
>= WINED3D_MAX_FRAGMENT_SAMPLERS
)
2285 WARN("Ignoring invalid stage %lu.\n", stage
);
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
);
2301 wined3d_mutex_unlock();
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();
2323 static const struct tss_lookup
2328 enum wined3d_texture_stage_state texture_state
;
2329 enum wined3d_sampler_state sampler_state
;
2334 {FALSE
, {WINED3D_TSS_INVALID
}}, /* 0, unused */
2335 {FALSE
, {WINED3D_TSS_COLOR_OP
}}, /* 1, D3DTSS_COLOROP */
2336 {FALSE
, {WINED3D_TSS_COLOR_ARG1
}}, /* 2, D3DTSS_COLORARG1 */
2337 {FALSE
, {WINED3D_TSS_COLOR_ARG2
}}, /* 3, D3DTSS_COLORARG2 */
2338 {FALSE
, {WINED3D_TSS_ALPHA_OP
}}, /* 4, D3DTSS_ALPHAOP */
2339 {FALSE
, {WINED3D_TSS_ALPHA_ARG1
}}, /* 5, D3DTSS_ALPHAARG1 */
2340 {FALSE
, {WINED3D_TSS_ALPHA_ARG2
}}, /* 6, D3DTSS_ALPHAARG2 */
2341 {FALSE
, {WINED3D_TSS_BUMPENV_MAT00
}}, /* 7, D3DTSS_BUMPENVMAT00 */
2342 {FALSE
, {WINED3D_TSS_BUMPENV_MAT01
}}, /* 8, D3DTSS_BUMPENVMAT01 */
2343 {FALSE
, {WINED3D_TSS_BUMPENV_MAT10
}}, /* 9, D3DTSS_BUMPENVMAT10 */
2344 {FALSE
, {WINED3D_TSS_BUMPENV_MAT11
}}, /* 10, D3DTSS_BUMPENVMAT11 */
2345 {FALSE
, {WINED3D_TSS_TEXCOORD_INDEX
}}, /* 11, D3DTSS_TEXCOORDINDEX */
2346 {FALSE
, {WINED3D_TSS_INVALID
}}, /* 12, unused */
2347 {TRUE
, {WINED3D_SAMP_ADDRESS_U
}}, /* 13, D3DTSS_ADDRESSU */
2348 {TRUE
, {WINED3D_SAMP_ADDRESS_V
}}, /* 14, D3DTSS_ADDRESSV */
2349 {TRUE
, {WINED3D_SAMP_BORDER_COLOR
}}, /* 15, D3DTSS_BORDERCOLOR */
2350 {TRUE
, {WINED3D_SAMP_MAG_FILTER
}}, /* 16, D3DTSS_MAGFILTER */
2351 {TRUE
, {WINED3D_SAMP_MIN_FILTER
}}, /* 17, D3DTSS_MINFILTER */
2352 {TRUE
, {WINED3D_SAMP_MIP_FILTER
}}, /* 18, D3DTSS_MIPFILTER */
2353 {TRUE
, {WINED3D_SAMP_MIPMAP_LOD_BIAS
}}, /* 19, D3DTSS_MIPMAPLODBIAS */
2354 {TRUE
, {WINED3D_SAMP_MAX_MIP_LEVEL
}}, /* 20, D3DTSS_MAXMIPLEVEL */
2355 {TRUE
, {WINED3D_SAMP_MAX_ANISOTROPY
}}, /* 21, D3DTSS_MAXANISOTROPY */
2356 {FALSE
, {WINED3D_TSS_BUMPENV_LSCALE
}}, /* 22, D3DTSS_BUMPENVLSCALE */
2357 {FALSE
, {WINED3D_TSS_BUMPENV_LOFFSET
}}, /* 23, D3DTSS_BUMPENVLOFFSET */
2358 {FALSE
, {WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
}}, /* 24, D3DTSS_TEXTURETRANSFORMFLAGS */
2359 {TRUE
, {WINED3D_SAMP_ADDRESS_W
}}, /* 25, D3DTSS_ADDRESSW */
2360 {FALSE
, {WINED3D_TSS_COLOR_ARG0
}}, /* 26, D3DTSS_COLORARG0 */
2361 {FALSE
, {WINED3D_TSS_ALPHA_ARG0
}}, /* 27, D3DTSS_ALPHAARG0 */
2362 {FALSE
, {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
);
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
];
2387 *value
= device_state
->texture_states
[stage
][l
->u
.texture_state
];
2388 wined3d_mutex_unlock();
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
);
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
);
2413 wined3d_stateblock_set_texture_stage_state(device
->update_state
, stage
, l
->u
.texture_state
, value
);
2414 wined3d_mutex_unlock();
2419 static HRESULT WINAPI
d3d8_device_ValidateDevice(IDirect3DDevice8
*iface
, DWORD
*pass_count
)
2421 struct d3d8_device
*device
= impl_from_IDirect3DDevice8(iface
);
2424 TRACE("iface %p, pass_count %p.\n", iface
, pass_count
);
2426 wined3d_mutex_lock();
2427 wined3d_device_apply_stateblock(device
->wined3d_device
, device
->state
);
2428 hr
= wined3d_device_validate_device(device
->wined3d_device
, pass_count
);
2429 wined3d_mutex_unlock();
2431 /* In d3d8, texture filters are not validated, so errors concerning
2432 * unsupported ones are ignored here. */
2433 if (hr
== WINED3DERR_UNSUPPORTEDTEXTUREFILTER
)
2435 WARN("Ignoring invalid texture filter settings.\n");
2443 static HRESULT WINAPI
d3d8_device_GetInfo(IDirect3DDevice8
*iface
,
2444 DWORD info_id
, void *info
, DWORD info_size
)
2446 TRACE("iface %p, info_id %#lx, info %p, info_size %lu.\n", iface
, info_id
, info
, info_size
);
2453 static HRESULT WINAPI
d3d8_device_SetPaletteEntries(IDirect3DDevice8
*iface
,
2454 UINT palette_idx
, const PALETTEENTRY
*entries
)
2456 WARN("iface %p, palette_idx %u, entries %p unimplemented\n", iface
, palette_idx
, entries
);
2458 /* GPUs stopped supporting palettized textures with the Shader Model 1 generation. Wined3d
2459 * does not have a d3d8/9-style palette API */
2464 static HRESULT WINAPI
d3d8_device_GetPaletteEntries(IDirect3DDevice8
*iface
,
2465 UINT palette_idx
, PALETTEENTRY
*entries
)
2467 FIXME("iface %p, palette_idx %u, entries %p unimplemented.\n", iface
, palette_idx
, entries
);
2469 return D3DERR_INVALIDCALL
;
2472 static HRESULT WINAPI
d3d8_device_SetCurrentTexturePalette(IDirect3DDevice8
*iface
, UINT palette_idx
)
2474 WARN("iface %p, palette_idx %u unimplemented.\n", iface
, palette_idx
);
2479 static HRESULT WINAPI
d3d8_device_GetCurrentTexturePalette(IDirect3DDevice8
*iface
, UINT
*palette_idx
)
2481 FIXME("iface %p, palette_idx %p unimplemented.\n", iface
, palette_idx
);
2483 return D3DERR_INVALIDCALL
;
2486 static void d3d8_device_upload_sysmem_vertex_buffers(struct d3d8_device
*device
,
2487 unsigned int start_vertex
, unsigned int vertex_count
)
2489 const struct wined3d_stateblock_state
*state
= device
->stateblock_state
;
2490 struct wined3d_vertex_declaration
*wined3d_decl
;
2491 struct wined3d_box box
= {0, 0, 0, 1, 0, 1};
2492 const struct wined3d_stream_state
*stream
;
2493 struct d3d8_vertexbuffer
*d3d8_buffer
;
2494 struct wined3d_resource
*dst_resource
;
2495 struct d3d8_vertex_declaration
*decl
;
2496 struct wined3d_buffer
*dst_buffer
;
2497 struct wined3d_resource_desc desc
;
2498 unsigned int stride
, map
;
2501 if (!device
->sysmem_vb
)
2504 if (!(wined3d_decl
= state
->vertex_declaration
))
2507 decl
= wined3d_vertex_declaration_get_parent(wined3d_decl
);
2508 map
= decl
->stream_map
& device
->sysmem_vb
;
2511 stream
= &state
->streams
[wined3d_bit_scan(&map
)];
2512 dst_buffer
= stream
->buffer
;
2513 stride
= stream
->stride
;
2515 d3d8_buffer
= wined3d_buffer_get_parent(dst_buffer
);
2516 dst_resource
= wined3d_buffer_get_resource(dst_buffer
);
2517 wined3d_resource_get_desc(dst_resource
, &desc
);
2518 box
.left
= stream
->offset
+ start_vertex
* stride
;
2519 box
.right
= min(box
.left
+ vertex_count
* stride
, desc
.size
);
2520 if (FAILED(hr
= wined3d_device_context_copy_sub_resource_region(device
->immediate_context
,
2521 dst_resource
, 0, box
.left
, 0, 0,
2522 wined3d_buffer_get_resource(d3d8_buffer
->wined3d_buffer
), 0, &box
, 0)))
2523 ERR("Failed to update buffer.\n");
2527 static HRESULT
d3d8_device_upload_sysmem_index_buffer(struct d3d8_device
*device
,
2528 unsigned int *start_idx
, unsigned int idx_count
)
2530 const struct wined3d_stateblock_state
*state
= device
->stateblock_state
;
2531 unsigned int src_offset
, idx_size
, buffer_size
, pos
;
2532 struct wined3d_resource_desc resource_desc
;
2533 struct wined3d_resource
*index_buffer
;
2534 struct wined3d_map_desc map_desc
;
2535 struct wined3d_box box
;
2538 if (!device
->sysmem_ib
)
2541 index_buffer
= wined3d_buffer_get_resource(state
->index_buffer
);
2542 wined3d_resource_get_desc(index_buffer
, &resource_desc
);
2543 idx_size
= (state
->index_format
== WINED3DFMT_R16_UINT
) ? 2 : 4;
2545 src_offset
= (*start_idx
) * idx_size
;
2546 buffer_size
= min(idx_count
* idx_size
, resource_desc
.size
- src_offset
);
2548 wined3d_box_set(&box
, src_offset
, 0, buffer_size
, 1, 0, 1);
2549 if (FAILED(hr
= wined3d_resource_map(index_buffer
, 0, &map_desc
, &box
, WINED3D_MAP_READ
)))
2551 ERR("Failed to map index buffer, hr %#lx.\n", hr
);
2554 wined3d_streaming_buffer_upload(device
->wined3d_device
, &device
->index_buffer
,
2555 map_desc
.data
, buffer_size
, idx_size
, &pos
);
2556 wined3d_resource_unmap(index_buffer
, 0);
2558 wined3d_device_context_set_index_buffer(device
->immediate_context
,
2559 device
->index_buffer
.buffer
, state
->index_format
, pos
);
2564 static void d3d8_device_upload_managed_textures(struct d3d8_device
*device
)
2566 const struct wined3d_stateblock_state
*state
= device
->stateblock_state
;
2569 for (i
= 0; i
< WINED3D_MAX_FRAGMENT_SAMPLERS
; ++i
)
2571 struct wined3d_texture
*wined3d_texture
= state
->textures
[i
];
2572 struct d3d8_texture
*d3d8_texture
;
2574 if (!wined3d_texture
)
2576 d3d8_texture
= wined3d_texture_get_parent(wined3d_texture
);
2577 if (d3d8_texture
->draw_texture
)
2578 wined3d_device_update_texture(device
->wined3d_device
,
2579 d3d8_texture
->wined3d_texture
, d3d8_texture
->draw_texture
);
2583 static HRESULT WINAPI
d3d8_device_DrawPrimitive(IDirect3DDevice8
*iface
,
2584 D3DPRIMITIVETYPE primitive_type
, UINT start_vertex
, UINT primitive_count
)
2586 struct d3d8_device
*device
= impl_from_IDirect3DDevice8(iface
);
2587 struct d3d8_vertexbuffer
*vb
;
2588 unsigned int vertex_count
, i
;
2590 TRACE("iface %p, primitive_type %#x, start_vertex %u, primitive_count %u.\n",
2591 iface
, primitive_type
, start_vertex
, primitive_count
);
2593 vertex_count
= vertex_count_from_primitive_count(primitive_type
, primitive_count
);
2594 wined3d_mutex_lock();
2595 d3d8_device_upload_managed_textures(device
);
2596 d3d8_device_upload_sysmem_vertex_buffers(device
, start_vertex
, vertex_count
);
2597 wined3d_device_context_set_primitive_type(device
->immediate_context
,
2598 wined3d_primitive_type_from_d3d(primitive_type
), 0);
2599 wined3d_device_apply_stateblock(device
->wined3d_device
, device
->state
);
2600 wined3d_device_context_draw(device
->immediate_context
, start_vertex
, vertex_count
, 0, 0);
2602 for (i
= 0; i
< ARRAY_SIZE(device
->stateblock_state
->streams
); ++i
)
2604 if (device
->stateblock_state
->streams
[i
].buffer
)
2606 vb
= wined3d_buffer_get_parent(device
->stateblock_state
->streams
[i
].buffer
);
2607 vb
->discarded
= false;
2611 wined3d_mutex_unlock();
2616 static HRESULT WINAPI
d3d8_device_DrawIndexedPrimitive(IDirect3DDevice8
*iface
,
2617 D3DPRIMITIVETYPE primitive_type
, UINT min_vertex_idx
, UINT vertex_count
,
2618 UINT start_idx
, UINT primitive_count
)
2620 struct d3d8_device
*device
= impl_from_IDirect3DDevice8(iface
);
2621 struct d3d8_vertexbuffer
*vb
;
2622 struct d3d8_indexbuffer
*ib
;
2623 unsigned int index_count
, i
;
2624 int base_vertex_index
;
2627 TRACE("iface %p, primitive_type %#x, min_vertex_idx %u, vertex_count %u, start_idx %u, primitive_count %u.\n",
2628 iface
, primitive_type
, min_vertex_idx
, vertex_count
, start_idx
, primitive_count
);
2630 index_count
= vertex_count_from_primitive_count(primitive_type
, primitive_count
);
2631 wined3d_mutex_lock();
2632 if (!device
->stateblock_state
->index_buffer
)
2634 wined3d_mutex_unlock();
2635 WARN("Index buffer not set, returning D3D_OK.\n");
2638 base_vertex_index
= device
->stateblock_state
->base_vertex_index
;
2639 d3d8_device_upload_managed_textures(device
);
2640 d3d8_device_upload_sysmem_vertex_buffers(device
, base_vertex_index
+ min_vertex_idx
, vertex_count
);
2641 wined3d_device_context_set_primitive_type(device
->immediate_context
,
2642 wined3d_primitive_type_from_d3d(primitive_type
), 0);
2643 wined3d_device_apply_stateblock(device
->wined3d_device
, device
->state
);
2644 if (FAILED(hr
= d3d8_device_upload_sysmem_index_buffer(device
, &start_idx
, index_count
)))
2646 wined3d_mutex_unlock();
2649 wined3d_device_context_draw_indexed(device
->immediate_context
, base_vertex_index
, start_idx
, index_count
, 0, 0);
2651 ib
= wined3d_buffer_get_parent(device
->stateblock_state
->index_buffer
);
2652 ib
->discarded
= false;
2653 for (i
= 0; i
< ARRAY_SIZE(device
->stateblock_state
->streams
); ++i
)
2655 if (device
->stateblock_state
->streams
[i
].buffer
)
2657 vb
= wined3d_buffer_get_parent(device
->stateblock_state
->streams
[i
].buffer
);
2658 vb
->discarded
= false;
2662 wined3d_mutex_unlock();
2667 static HRESULT WINAPI
d3d8_device_DrawPrimitiveUP(IDirect3DDevice8
*iface
,
2668 D3DPRIMITIVETYPE primitive_type
, UINT primitive_count
, const void *data
,
2671 struct d3d8_device
*device
= impl_from_IDirect3DDevice8(iface
);
2672 UINT vtx_count
= vertex_count_from_primitive_count(primitive_type
, primitive_count
);
2673 UINT size
= vtx_count
* stride
;
2674 unsigned int vb_pos
;
2677 TRACE("iface %p, primitive_type %#x, primitive_count %u, data %p, stride %u.\n",
2678 iface
, primitive_type
, primitive_count
, data
, stride
);
2680 if (!primitive_count
|| !stride
)
2682 WARN("primitive_count or stride is 0, returning D3D_OK.\n");
2686 wined3d_mutex_lock();
2688 if (FAILED(hr
= wined3d_streaming_buffer_upload(device
->wined3d_device
,
2689 &device
->vertex_buffer
, data
, size
, stride
, &vb_pos
)))
2692 hr
= wined3d_stateblock_set_stream_source(device
->state
, 0, device
->vertex_buffer
.buffer
, 0, stride
);
2696 d3d8_device_upload_managed_textures(device
);
2697 wined3d_device_context_set_primitive_type(device
->immediate_context
,
2698 wined3d_primitive_type_from_d3d(primitive_type
), 0);
2699 wined3d_device_apply_stateblock(device
->wined3d_device
, device
->state
);
2700 wined3d_device_context_draw(device
->immediate_context
, vb_pos
/ stride
, vtx_count
, 0, 0);
2701 wined3d_stateblock_set_stream_source(device
->state
, 0, NULL
, 0, 0);
2704 wined3d_mutex_unlock();
2708 static HRESULT WINAPI
d3d8_device_DrawIndexedPrimitiveUP(IDirect3DDevice8
*iface
,
2709 D3DPRIMITIVETYPE primitive_type
, UINT min_vertex_idx
, UINT vertex_count
,
2710 UINT primitive_count
, const void *index_data
, D3DFORMAT index_format
,
2711 const void *vertex_data
, UINT vertex_stride
)
2713 UINT idx_count
= vertex_count_from_primitive_count(primitive_type
, primitive_count
);
2714 struct d3d8_device
*device
= impl_from_IDirect3DDevice8(iface
);
2715 UINT idx_fmt_size
= index_format
== D3DFMT_INDEX16
? 2 : 4;
2716 unsigned int base_vertex_idx
, vb_pos
, ib_pos
;
2717 UINT vtx_size
= vertex_count
* vertex_stride
;
2718 UINT idx_size
= idx_count
* idx_fmt_size
;
2721 TRACE("iface %p, primitive_type %#x, min_vertex_idx %u, vertex_count %u, primitive_count %u, "
2722 "index_data %p, index_format %#x, vertex_data %p, vertex_stride %u.\n",
2723 iface
, primitive_type
, min_vertex_idx
, vertex_count
, primitive_count
,
2724 index_data
, index_format
, vertex_data
, vertex_stride
);
2726 if (!primitive_count
|| !vertex_stride
)
2728 WARN("primitive_count or vertex_stride is 0, returning D3D_OK.\n");
2732 wined3d_mutex_lock();
2734 if (FAILED(hr
= wined3d_streaming_buffer_upload(device
->wined3d_device
, &device
->vertex_buffer
,
2735 (char *)vertex_data
+ min_vertex_idx
* vertex_stride
, vtx_size
, vertex_stride
, &vb_pos
)))
2738 if (FAILED(hr
= wined3d_streaming_buffer_upload(device
->wined3d_device
, &device
->index_buffer
,
2739 index_data
, idx_size
, idx_fmt_size
, &ib_pos
)))
2742 hr
= wined3d_stateblock_set_stream_source(device
->state
, 0, device
->vertex_buffer
.buffer
, 0, vertex_stride
);
2746 wined3d_stateblock_set_index_buffer(device
->state
, device
->index_buffer
.buffer
,
2747 wined3dformat_from_d3dformat(index_format
));
2748 base_vertex_idx
= vb_pos
/ vertex_stride
- min_vertex_idx
;
2749 wined3d_stateblock_set_base_vertex_index(device
->state
, base_vertex_idx
);
2751 d3d8_device_upload_managed_textures(device
);
2752 wined3d_device_context_set_primitive_type(device
->immediate_context
,
2753 wined3d_primitive_type_from_d3d(primitive_type
), 0);
2754 wined3d_device_apply_stateblock(device
->wined3d_device
, device
->state
);
2755 wined3d_device_context_draw_indexed(device
->immediate_context
,
2756 base_vertex_idx
, ib_pos
/ idx_fmt_size
, idx_count
, 0, 0);
2758 wined3d_stateblock_set_stream_source(device
->state
, 0, NULL
, 0, 0);
2759 wined3d_stateblock_set_index_buffer(device
->state
, NULL
, WINED3DFMT_UNKNOWN
);
2760 wined3d_stateblock_set_base_vertex_index(device
->state
, 0);
2763 wined3d_mutex_unlock();
2767 static HRESULT WINAPI
d3d8_device_ProcessVertices(IDirect3DDevice8
*iface
, UINT src_start_idx
,
2768 UINT dst_idx
, UINT vertex_count
, IDirect3DVertexBuffer8
*dst_buffer
, DWORD flags
)
2770 struct d3d8_device
*device
= impl_from_IDirect3DDevice8(iface
);
2771 struct d3d8_vertexbuffer
*dst
= unsafe_impl_from_IDirect3DVertexBuffer8(dst_buffer
);
2772 const struct wined3d_stateblock_state
*state
;
2773 const struct wined3d_stream_state
*stream
;
2774 struct d3d8_vertexbuffer
*d3d8_buffer
;
2775 unsigned int i
, map
;
2778 TRACE("iface %p, src_start_idx %u, dst_idx %u, vertex_count %u, dst_buffer %p, flags %#lx.\n",
2779 iface
, src_start_idx
, dst_idx
, vertex_count
, dst_buffer
, flags
);
2781 wined3d_mutex_lock();
2783 state
= device
->stateblock_state
;
2785 /* Note that an alternative approach would be to simply create these
2786 * buffers with WINED3D_RESOURCE_ACCESS_MAP_R and update them here like we
2787 * do for draws. In some regards that would be easier, but it seems less
2788 * than optimal to upload data to the GPU only to subsequently download it
2790 map
= device
->sysmem_vb
;
2793 i
= wined3d_bit_scan(&map
);
2794 stream
= &state
->streams
[i
];
2796 d3d8_buffer
= wined3d_buffer_get_parent(stream
->buffer
);
2797 if (FAILED(wined3d_stateblock_set_stream_source(device
->state
,
2798 i
, d3d8_buffer
->wined3d_buffer
, stream
->offset
, stream
->stride
)))
2799 ERR("Failed to set stream source.\n");
2802 wined3d_device_apply_stateblock(device
->wined3d_device
, device
->state
);
2803 hr
= wined3d_device_process_vertices(device
->wined3d_device
, src_start_idx
, dst_idx
,
2804 vertex_count
, dst
->wined3d_buffer
, NULL
, flags
, dst
->fvf
);
2806 map
= device
->sysmem_vb
;
2809 i
= wined3d_bit_scan(&map
);
2810 stream
= &state
->streams
[i
];
2812 d3d8_buffer
= wined3d_buffer_get_parent(stream
->buffer
);
2813 if (FAILED(wined3d_stateblock_set_stream_source(device
->state
,
2814 i
, d3d8_buffer
->draw_buffer
, stream
->offset
, stream
->stride
)))
2815 ERR("Failed to set stream source.\n");
2818 wined3d_mutex_unlock();
2823 static HRESULT WINAPI
d3d8_device_CreateVertexShader(IDirect3DDevice8
*iface
,
2824 const DWORD
*declaration
, const DWORD
*byte_code
, DWORD
*shader
, DWORD usage
)
2826 struct d3d8_device
*device
= impl_from_IDirect3DDevice8(iface
);
2827 struct d3d8_vertex_shader
*object
;
2828 DWORD shader_handle
;
2832 TRACE("iface %p, declaration %p, byte_code %p, shader %p, usage %#lx.\n",
2833 iface
, declaration
, byte_code
, shader
, usage
);
2835 if (!(object
= heap_alloc_zero(sizeof(*object
))))
2838 return E_OUTOFMEMORY
;
2841 wined3d_mutex_lock();
2842 handle
= d3d8_allocate_handle(&device
->handle_table
, object
, D3D8_HANDLE_VS
);
2843 wined3d_mutex_unlock();
2844 if (handle
== D3D8_INVALID_HANDLE
)
2846 ERR("Failed to allocate vertex shader handle.\n");
2849 return E_OUTOFMEMORY
;
2852 shader_handle
= handle
+ VS_HIGHESTFIXEDFXF
+ 1;
2854 hr
= d3d8_vertex_shader_init(object
, device
, declaration
, byte_code
, shader_handle
, usage
);
2857 WARN("Failed to initialize vertex shader, hr %#lx.\n", hr
);
2858 wined3d_mutex_lock();
2859 d3d8_free_handle(&device
->handle_table
, handle
, D3D8_HANDLE_VS
);
2860 wined3d_mutex_unlock();
2866 TRACE("Created vertex shader %p (handle %#lx).\n", object
, shader_handle
);
2867 *shader
= shader_handle
;
2872 static struct d3d8_vertex_declaration
*d3d8_device_get_fvf_declaration(struct d3d8_device
*device
, DWORD fvf
)
2874 struct d3d8_vertex_declaration
*d3d8_declaration
;
2875 struct FvfToDecl
*convertedDecls
= device
->decls
;
2876 int p
, low
, high
; /* deliberately signed */
2879 TRACE("Searching for declaration for fvf %#lx... ", fvf
);
2882 high
= device
->numConvertedDecls
- 1;
2885 p
= (low
+ high
) >> 1;
2888 if (convertedDecls
[p
].fvf
== fvf
)
2890 TRACE("found %p\n", convertedDecls
[p
].declaration
);
2891 return convertedDecls
[p
].declaration
;
2894 if (convertedDecls
[p
].fvf
< fvf
)
2899 TRACE("not found. Creating and inserting at position %d.\n", low
);
2901 if (!(d3d8_declaration
= heap_alloc(sizeof(*d3d8_declaration
))))
2904 if (FAILED(hr
= d3d8_vertex_declaration_init_fvf(d3d8_declaration
, device
, fvf
)))
2906 WARN("Failed to initialize vertex declaration, hr %#lx.\n", hr
);
2907 heap_free(d3d8_declaration
);
2911 if (device
->declArraySize
== device
->numConvertedDecls
)
2913 UINT grow
= device
->declArraySize
/ 2;
2915 if (!(convertedDecls
= heap_realloc(convertedDecls
,
2916 sizeof(*convertedDecls
) * (device
->numConvertedDecls
+ grow
))))
2918 d3d8_vertex_declaration_destroy(d3d8_declaration
);
2921 device
->decls
= convertedDecls
;
2922 device
->declArraySize
+= grow
;
2925 memmove(convertedDecls
+ low
+ 1, convertedDecls
+ low
,
2926 sizeof(*convertedDecls
) * (device
->numConvertedDecls
- low
));
2927 convertedDecls
[low
].declaration
= d3d8_declaration
;
2928 convertedDecls
[low
].fvf
= fvf
;
2929 ++device
->numConvertedDecls
;
2931 TRACE("Returning %p. %u decls in array.\n", d3d8_declaration
, device
->numConvertedDecls
);
2933 return d3d8_declaration
;
2936 static HRESULT WINAPI
d3d8_device_SetVertexShader(IDirect3DDevice8
*iface
, DWORD shader
)
2938 struct d3d8_device
*device
= impl_from_IDirect3DDevice8(iface
);
2939 struct d3d8_vertex_shader
*shader_impl
;
2941 TRACE("iface %p, shader %#lx.\n", iface
, shader
);
2943 if (VS_HIGHESTFIXEDFXF
>= shader
)
2945 TRACE("Setting FVF, %#lx\n", shader
);
2947 wined3d_mutex_lock();
2948 wined3d_stateblock_set_vertex_declaration(device
->update_state
,
2949 d3d8_device_get_fvf_declaration(device
, shader
)->wined3d_vertex_declaration
);
2950 wined3d_stateblock_set_vertex_shader(device
->update_state
, NULL
);
2951 wined3d_mutex_unlock();
2956 TRACE("Setting shader\n");
2958 wined3d_mutex_lock();
2959 if (!(shader_impl
= d3d8_get_object(&device
->handle_table
, shader
- (VS_HIGHESTFIXEDFXF
+ 1), D3D8_HANDLE_VS
)))
2961 WARN("Invalid handle (%#lx) passed.\n", shader
);
2962 wined3d_mutex_unlock();
2964 return D3DERR_INVALIDCALL
;
2967 wined3d_stateblock_set_vertex_declaration(device
->update_state
,
2968 shader_impl
->vertex_declaration
->wined3d_vertex_declaration
);
2969 wined3d_stateblock_set_vertex_shader(device
->update_state
, shader_impl
->wined3d_shader
);
2970 wined3d_mutex_unlock();
2975 static HRESULT WINAPI
d3d8_device_GetVertexShader(IDirect3DDevice8
*iface
, DWORD
*shader
)
2977 struct d3d8_device
*device
= impl_from_IDirect3DDevice8(iface
);
2978 struct wined3d_vertex_declaration
*wined3d_declaration
;
2979 struct d3d8_vertex_declaration
*d3d8_declaration
;
2981 TRACE("iface %p, shader %p.\n", iface
, shader
);
2983 wined3d_mutex_lock();
2984 if ((wined3d_declaration
= device
->stateblock_state
->vertex_declaration
))
2986 d3d8_declaration
= wined3d_vertex_declaration_get_parent(wined3d_declaration
);
2987 *shader
= d3d8_declaration
->shader_handle
;
2993 wined3d_mutex_unlock();
2995 TRACE("Returning %#lx.\n", *shader
);
3000 static HRESULT WINAPI
d3d8_device_DeleteVertexShader(IDirect3DDevice8
*iface
, DWORD shader
)
3002 struct d3d8_device
*device
= impl_from_IDirect3DDevice8(iface
);
3003 struct d3d8_vertex_shader
*shader_impl
;
3005 TRACE("iface %p, shader %#lx.\n", iface
, shader
);
3007 wined3d_mutex_lock();
3008 if (!(shader_impl
= d3d8_free_handle(&device
->handle_table
, shader
- (VS_HIGHESTFIXEDFXF
+ 1), D3D8_HANDLE_VS
)))
3010 WARN("Invalid handle (%#lx) passed.\n", shader
);
3011 wined3d_mutex_unlock();
3013 return D3DERR_INVALIDCALL
;
3016 if (shader_impl
->wined3d_shader
3017 && device
->stateblock_state
->vs
== shader_impl
->wined3d_shader
)
3018 IDirect3DDevice8_SetVertexShader(iface
, 0);
3020 wined3d_mutex_unlock();
3022 d3d8_vertex_shader_destroy(shader_impl
);
3027 static HRESULT WINAPI
d3d8_device_SetVertexShaderConstant(IDirect3DDevice8
*iface
,
3028 DWORD start_register
, const void *data
, DWORD count
)
3030 struct d3d8_device
*device
= impl_from_IDirect3DDevice8(iface
);
3033 TRACE("iface %p, start_register %lu, data %p, count %lu.\n",
3034 iface
, start_register
, data
, count
);
3036 if (start_register
+ count
> D3D8_MAX_VERTEX_SHADER_CONSTANTF
)
3038 WARN("Trying to access %lu constants, but d3d8 only supports %u.\n",
3039 start_register
+ count
, D3D8_MAX_VERTEX_SHADER_CONSTANTF
);
3040 return D3DERR_INVALIDCALL
;
3043 wined3d_mutex_lock();
3044 hr
= wined3d_stateblock_set_vs_consts_f(device
->update_state
, start_register
, count
, data
);
3045 wined3d_mutex_unlock();
3050 static HRESULT WINAPI
d3d8_device_GetVertexShaderConstant(IDirect3DDevice8
*iface
,
3051 DWORD start_idx
, void *constants
, DWORD count
)
3053 struct d3d8_device
*device
= impl_from_IDirect3DDevice8(iface
);
3056 TRACE("iface %p, start_idx %lu, constants %p, count %lu.\n", iface
, start_idx
, constants
, count
);
3058 wined3d_mutex_lock();
3059 hr
= wined3d_stateblock_get_vs_consts_f(device
->state
, start_idx
, count
, constants
);
3060 wined3d_mutex_unlock();
3065 static HRESULT WINAPI
d3d8_device_GetVertexShaderDeclaration(IDirect3DDevice8
*iface
,
3066 DWORD shader
, void *data
, DWORD
*data_size
)
3068 struct d3d8_device
*device
= impl_from_IDirect3DDevice8(iface
);
3069 struct d3d8_vertex_declaration
*declaration
;
3070 struct d3d8_vertex_shader
*shader_impl
;
3072 TRACE("iface %p, shader %#lx, data %p, data_size %p.\n", iface
, shader
, data
, data_size
);
3074 wined3d_mutex_lock();
3075 shader_impl
= d3d8_get_object(&device
->handle_table
, shader
- (VS_HIGHESTFIXEDFXF
+ 1), D3D8_HANDLE_VS
);
3076 wined3d_mutex_unlock();
3080 WARN("Invalid handle (%#lx) passed.\n", shader
);
3081 return D3DERR_INVALIDCALL
;
3083 declaration
= shader_impl
->vertex_declaration
;
3087 *data_size
= declaration
->elements_size
;
3091 /* MSDN claims that if *data_size is smaller than the required size
3092 * we should write the required size and return D3DERR_MOREDATA.
3093 * That's not actually true. */
3094 if (*data_size
< declaration
->elements_size
)
3095 return D3DERR_INVALIDCALL
;
3097 memcpy(data
, declaration
->elements
, declaration
->elements_size
);
3102 static HRESULT WINAPI
d3d8_device_GetVertexShaderFunction(IDirect3DDevice8
*iface
,
3103 DWORD shader
, void *data
, DWORD
*data_size
)
3105 struct d3d8_device
*device
= impl_from_IDirect3DDevice8(iface
);
3106 struct d3d8_vertex_shader
*shader_impl
= NULL
;
3109 TRACE("iface %p, shader %#lx, data %p, data_size %p.\n", iface
, shader
, data
, data_size
);
3111 wined3d_mutex_lock();
3112 if (!(shader_impl
= d3d8_get_object(&device
->handle_table
, shader
- (VS_HIGHESTFIXEDFXF
+ 1), D3D8_HANDLE_VS
)))
3114 WARN("Invalid handle (%#lx) passed.\n", shader
);
3115 wined3d_mutex_unlock();
3117 return D3DERR_INVALIDCALL
;
3120 if (!shader_impl
->wined3d_shader
)
3122 wined3d_mutex_unlock();
3127 hr
= wined3d_shader_get_byte_code(shader_impl
->wined3d_shader
, data
, (unsigned int *)data_size
);
3128 wined3d_mutex_unlock();
3133 static HRESULT WINAPI
d3d8_device_SetIndices(IDirect3DDevice8
*iface
,
3134 IDirect3DIndexBuffer8
*buffer
, UINT base_vertex_idx
)
3136 struct d3d8_device
*device
= impl_from_IDirect3DDevice8(iface
);
3137 struct d3d8_indexbuffer
*ib
= unsafe_impl_from_IDirect3DIndexBuffer8(buffer
);
3138 struct wined3d_buffer
*wined3d_buffer
;
3140 TRACE("iface %p, buffer %p, base_vertex_idx %u.\n", iface
, buffer
, base_vertex_idx
);
3143 wined3d_buffer
= NULL
;
3145 wined3d_buffer
= ib
->wined3d_buffer
;
3147 /* WineD3D takes an INT(due to d3d9), but d3d8 uses UINTs. Do I have to add a check here that
3148 * the UINT doesn't cause an overflow in the INT? It seems rather unlikely because such large
3149 * vertex buffers can't be created to address them with an index that requires the 32nd bit
3150 * (4 Byte minimum vertex size * 2^31-1 -> 8 gb buffer. The index sign would be the least
3153 wined3d_mutex_lock();
3154 wined3d_stateblock_set_base_vertex_index(device
->update_state
, base_vertex_idx
);
3155 wined3d_stateblock_set_index_buffer(device
->update_state
, wined3d_buffer
, ib
? ib
->format
: WINED3DFMT_UNKNOWN
);
3156 if (!device
->recording
)
3157 device
->sysmem_ib
= ib
&& ib
->sysmem
;
3158 wined3d_mutex_unlock();
3163 static HRESULT WINAPI
d3d8_device_GetIndices(IDirect3DDevice8
*iface
,
3164 IDirect3DIndexBuffer8
**buffer
, UINT
*base_vertex_index
)
3166 struct d3d8_device
*device
= impl_from_IDirect3DDevice8(iface
);
3167 const struct wined3d_stateblock_state
*state
;
3168 struct wined3d_buffer
*wined3d_buffer
;
3169 struct d3d8_indexbuffer
*buffer_impl
;
3171 TRACE("iface %p, buffer %p, base_vertex_index %p.\n", iface
, buffer
, base_vertex_index
);
3174 return D3DERR_INVALIDCALL
;
3176 /* The case from UINT to INT is safe because d3d8 will never set negative values */
3177 wined3d_mutex_lock();
3178 state
= device
->stateblock_state
;
3179 *base_vertex_index
= state
->base_vertex_index
;
3180 if ((wined3d_buffer
= state
->index_buffer
))
3182 buffer_impl
= wined3d_buffer_get_parent(wined3d_buffer
);
3183 *buffer
= &buffer_impl
->IDirect3DIndexBuffer8_iface
;
3184 IDirect3DIndexBuffer8_AddRef(*buffer
);
3190 wined3d_mutex_unlock();
3195 static HRESULT WINAPI
d3d8_device_CreatePixelShader(IDirect3DDevice8
*iface
,
3196 const DWORD
*byte_code
, DWORD
*shader
)
3198 struct d3d8_device
*device
= impl_from_IDirect3DDevice8(iface
);
3199 struct d3d8_pixel_shader
*object
;
3200 DWORD shader_handle
;
3204 TRACE("iface %p, byte_code %p, shader %p.\n", iface
, byte_code
, shader
);
3207 return D3DERR_INVALIDCALL
;
3209 if (!(object
= heap_alloc_zero(sizeof(*object
))))
3210 return E_OUTOFMEMORY
;
3212 wined3d_mutex_lock();
3213 handle
= d3d8_allocate_handle(&device
->handle_table
, object
, D3D8_HANDLE_PS
);
3214 wined3d_mutex_unlock();
3215 if (handle
== D3D8_INVALID_HANDLE
)
3217 ERR("Failed to allocate pixel shader handle.\n");
3219 return E_OUTOFMEMORY
;
3222 shader_handle
= handle
+ VS_HIGHESTFIXEDFXF
+ 1;
3224 hr
= d3d8_pixel_shader_init(object
, device
, byte_code
, shader_handle
);
3227 WARN("Failed to initialize pixel shader, hr %#lx.\n", hr
);
3228 wined3d_mutex_lock();
3229 d3d8_free_handle(&device
->handle_table
, handle
, D3D8_HANDLE_PS
);
3230 wined3d_mutex_unlock();
3236 TRACE("Created pixel shader %p (handle %#lx).\n", object
, shader_handle
);
3237 *shader
= shader_handle
;
3242 static HRESULT WINAPI
d3d8_device_SetPixelShader(IDirect3DDevice8
*iface
, DWORD shader
)
3244 struct d3d8_device
*device
= impl_from_IDirect3DDevice8(iface
);
3245 struct d3d8_pixel_shader
*shader_impl
;
3247 TRACE("iface %p, shader %#lx.\n", iface
, shader
);
3249 wined3d_mutex_lock();
3253 wined3d_stateblock_set_pixel_shader(device
->update_state
, NULL
);
3254 wined3d_mutex_unlock();
3258 if (!(shader_impl
= d3d8_get_object(&device
->handle_table
, shader
- (VS_HIGHESTFIXEDFXF
+ 1), D3D8_HANDLE_PS
)))
3260 WARN("Invalid handle (%#lx) passed.\n", shader
);
3261 wined3d_mutex_unlock();
3262 return D3DERR_INVALIDCALL
;
3265 TRACE("Setting shader %p.\n", shader_impl
);
3266 wined3d_stateblock_set_pixel_shader(device
->update_state
, shader_impl
->wined3d_shader
);
3267 wined3d_mutex_unlock();
3272 static HRESULT WINAPI
d3d8_device_GetPixelShader(IDirect3DDevice8
*iface
, DWORD
*shader
)
3274 struct d3d8_device
*device
= impl_from_IDirect3DDevice8(iface
);
3275 struct wined3d_shader
*object
;
3277 TRACE("iface %p, shader %p.\n", iface
, shader
);
3280 return D3DERR_INVALIDCALL
;
3282 wined3d_mutex_lock();
3283 if ((object
= device
->stateblock_state
->ps
))
3285 struct d3d8_pixel_shader
*d3d8_shader
;
3286 d3d8_shader
= wined3d_shader_get_parent(object
);
3287 *shader
= d3d8_shader
->handle
;
3293 wined3d_mutex_unlock();
3295 TRACE("Returning %#lx.\n", *shader
);
3300 static HRESULT WINAPI
d3d8_device_DeletePixelShader(IDirect3DDevice8
*iface
, DWORD shader
)
3302 struct d3d8_device
*device
= impl_from_IDirect3DDevice8(iface
);
3303 struct d3d8_pixel_shader
*shader_impl
;
3305 TRACE("iface %p, shader %#lx.\n", iface
, shader
);
3307 wined3d_mutex_lock();
3309 if (!(shader_impl
= d3d8_free_handle(&device
->handle_table
, shader
- (VS_HIGHESTFIXEDFXF
+ 1), D3D8_HANDLE_PS
)))
3311 WARN("Invalid handle (%#lx) passed.\n", shader
);
3312 wined3d_mutex_unlock();
3313 return D3DERR_INVALIDCALL
;
3316 if (device
->stateblock_state
->ps
== shader_impl
->wined3d_shader
)
3317 IDirect3DDevice8_SetPixelShader(iface
, 0);
3319 wined3d_mutex_unlock();
3321 d3d8_pixel_shader_destroy(shader_impl
);
3326 static HRESULT WINAPI
d3d8_device_SetPixelShaderConstant(IDirect3DDevice8
*iface
,
3327 DWORD start_register
, const void *data
, DWORD count
)
3329 struct d3d8_device
*device
= impl_from_IDirect3DDevice8(iface
);
3332 TRACE("iface %p, start_register %lu, data %p, count %lu.\n", iface
, start_register
, data
, count
);
3334 wined3d_mutex_lock();
3335 hr
= wined3d_stateblock_set_ps_consts_f(device
->update_state
, start_register
, count
, data
);
3336 wined3d_mutex_unlock();
3341 static HRESULT WINAPI
d3d8_device_GetPixelShaderConstant(IDirect3DDevice8
*iface
,
3342 DWORD start_idx
, void *constants
, DWORD count
)
3344 struct d3d8_device
*device
= impl_from_IDirect3DDevice8(iface
);
3347 TRACE("iface %p, start_idx %lu, constants %p, count %lu.\n", iface
, start_idx
, constants
, count
);
3349 if (!wined3d_bound_range(start_idx
, count
, D3D8_MAX_PIXEL_SHADER_CONSTANTF
))
3350 return WINED3DERR_INVALIDCALL
;
3352 wined3d_mutex_lock();
3353 hr
= wined3d_stateblock_get_ps_consts_f(device
->state
, start_idx
, count
, constants
);
3354 wined3d_mutex_unlock();
3359 static HRESULT WINAPI
d3d8_device_GetPixelShaderFunction(IDirect3DDevice8
*iface
,
3360 DWORD shader
, void *data
, DWORD
*data_size
)
3362 struct d3d8_device
*device
= impl_from_IDirect3DDevice8(iface
);
3363 struct d3d8_pixel_shader
*shader_impl
= NULL
;
3366 TRACE("iface %p, shader %#lx, data %p, data_size %p.\n", iface
, shader
, data
, data_size
);
3368 wined3d_mutex_lock();
3369 if (!(shader_impl
= d3d8_get_object(&device
->handle_table
, shader
- (VS_HIGHESTFIXEDFXF
+ 1), D3D8_HANDLE_PS
)))
3371 WARN("Invalid handle (%#lx) passed.\n", shader
);
3372 wined3d_mutex_unlock();
3374 return D3DERR_INVALIDCALL
;
3377 hr
= wined3d_shader_get_byte_code(shader_impl
->wined3d_shader
, data
, (unsigned int *)data_size
);
3378 wined3d_mutex_unlock();
3383 static HRESULT WINAPI
d3d8_device_DrawRectPatch(IDirect3DDevice8
*iface
, UINT handle
,
3384 const float *segment_count
, const D3DRECTPATCH_INFO
*patch_info
)
3386 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
3387 iface
, handle
, segment_count
, patch_info
);
3391 static HRESULT WINAPI
d3d8_device_DrawTriPatch(IDirect3DDevice8
*iface
, UINT handle
,
3392 const float *segment_count
, const D3DTRIPATCH_INFO
*patch_info
)
3394 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
3395 iface
, handle
, segment_count
, patch_info
);
3399 static HRESULT WINAPI
d3d8_device_DeletePatch(IDirect3DDevice8
*iface
, UINT handle
)
3401 FIXME("iface %p, handle %#x unimplemented.\n", iface
, handle
);
3402 return D3DERR_INVALIDCALL
;
3405 static HRESULT WINAPI
d3d8_device_SetStreamSource(IDirect3DDevice8
*iface
,
3406 UINT stream_idx
, IDirect3DVertexBuffer8
*buffer
, UINT stride
)
3408 struct d3d8_device
*device
= impl_from_IDirect3DDevice8(iface
);
3409 struct d3d8_vertexbuffer
*buffer_impl
= unsafe_impl_from_IDirect3DVertexBuffer8(buffer
);
3410 const struct wined3d_stateblock_state
*state
;
3411 struct wined3d_buffer
*wined3d_buffer
;
3414 TRACE("iface %p, stream_idx %u, buffer %p, stride %u.\n",
3415 iface
, stream_idx
, buffer
, stride
);
3417 if (stream_idx
>= ARRAY_SIZE(state
->streams
))
3419 WARN("Stream index %u out of range.\n", stream_idx
);
3420 return D3DERR_INVALIDCALL
;
3423 wined3d_mutex_lock();
3427 wined3d_buffer
= NULL
;
3428 state
= device
->stateblock_state
;
3429 stride
= state
->streams
[stream_idx
].stride
;
3431 else if (buffer_impl
->draw_buffer
)
3432 wined3d_buffer
= buffer_impl
->draw_buffer
;
3434 wined3d_buffer
= buffer_impl
->wined3d_buffer
;
3436 hr
= wined3d_stateblock_set_stream_source(device
->update_state
, stream_idx
, wined3d_buffer
, 0, stride
);
3437 if (SUCCEEDED(hr
) && !device
->recording
)
3439 if (buffer_impl
&& buffer_impl
->draw_buffer
)
3440 device
->sysmem_vb
|= (1u << stream_idx
);
3442 device
->sysmem_vb
&= ~(1u << stream_idx
);
3445 wined3d_mutex_unlock();
3450 static HRESULT WINAPI
d3d8_device_GetStreamSource(IDirect3DDevice8
*iface
,
3451 UINT stream_idx
, IDirect3DVertexBuffer8
**buffer
, UINT
*stride
)
3453 struct d3d8_device
*device
= impl_from_IDirect3DDevice8(iface
);
3454 const struct wined3d_stateblock_state
*state
;
3455 const struct wined3d_stream_state
*stream
;
3456 struct d3d8_vertexbuffer
*buffer_impl
;
3458 TRACE("iface %p, stream_idx %u, buffer %p, stride %p.\n",
3459 iface
, stream_idx
, buffer
, stride
);
3462 return D3DERR_INVALIDCALL
;
3464 if (stream_idx
>= ARRAY_SIZE(state
->streams
))
3466 WARN("Stream index %u out of range.\n", stream_idx
);
3467 return D3DERR_INVALIDCALL
;
3470 wined3d_mutex_lock();
3471 state
= device
->stateblock_state
;
3472 stream
= &state
->streams
[stream_idx
];
3475 buffer_impl
= wined3d_buffer_get_parent(stream
->buffer
);
3476 *buffer
= &buffer_impl
->IDirect3DVertexBuffer8_iface
;
3477 IDirect3DVertexBuffer8_AddRef(*buffer
);
3481 *stride
= stream
->stride
;
3482 wined3d_mutex_unlock();
3487 static const struct IDirect3DDevice8Vtbl d3d8_device_vtbl
=
3489 d3d8_device_QueryInterface
,
3491 d3d8_device_Release
,
3492 d3d8_device_TestCooperativeLevel
,
3493 d3d8_device_GetAvailableTextureMem
,
3494 d3d8_device_ResourceManagerDiscardBytes
,
3495 d3d8_device_GetDirect3D
,
3496 d3d8_device_GetDeviceCaps
,
3497 d3d8_device_GetDisplayMode
,
3498 d3d8_device_GetCreationParameters
,
3499 d3d8_device_SetCursorProperties
,
3500 d3d8_device_SetCursorPosition
,
3501 d3d8_device_ShowCursor
,
3502 d3d8_device_CreateAdditionalSwapChain
,
3504 d3d8_device_Present
,
3505 d3d8_device_GetBackBuffer
,
3506 d3d8_device_GetRasterStatus
,
3507 d3d8_device_SetGammaRamp
,
3508 d3d8_device_GetGammaRamp
,
3509 d3d8_device_CreateTexture
,
3510 d3d8_device_CreateVolumeTexture
,
3511 d3d8_device_CreateCubeTexture
,
3512 d3d8_device_CreateVertexBuffer
,
3513 d3d8_device_CreateIndexBuffer
,
3514 d3d8_device_CreateRenderTarget
,
3515 d3d8_device_CreateDepthStencilSurface
,
3516 d3d8_device_CreateImageSurface
,
3517 d3d8_device_CopyRects
,
3518 d3d8_device_UpdateTexture
,
3519 d3d8_device_GetFrontBuffer
,
3520 d3d8_device_SetRenderTarget
,
3521 d3d8_device_GetRenderTarget
,
3522 d3d8_device_GetDepthStencilSurface
,
3523 d3d8_device_BeginScene
,
3524 d3d8_device_EndScene
,
3526 d3d8_device_SetTransform
,
3527 d3d8_device_GetTransform
,
3528 d3d8_device_MultiplyTransform
,
3529 d3d8_device_SetViewport
,
3530 d3d8_device_GetViewport
,
3531 d3d8_device_SetMaterial
,
3532 d3d8_device_GetMaterial
,
3533 d3d8_device_SetLight
,
3534 d3d8_device_GetLight
,
3535 d3d8_device_LightEnable
,
3536 d3d8_device_GetLightEnable
,
3537 d3d8_device_SetClipPlane
,
3538 d3d8_device_GetClipPlane
,
3539 d3d8_device_SetRenderState
,
3540 d3d8_device_GetRenderState
,
3541 d3d8_device_BeginStateBlock
,
3542 d3d8_device_EndStateBlock
,
3543 d3d8_device_ApplyStateBlock
,
3544 d3d8_device_CaptureStateBlock
,
3545 d3d8_device_DeleteStateBlock
,
3546 d3d8_device_CreateStateBlock
,
3547 d3d8_device_SetClipStatus
,
3548 d3d8_device_GetClipStatus
,
3549 d3d8_device_GetTexture
,
3550 d3d8_device_SetTexture
,
3551 d3d8_device_GetTextureStageState
,
3552 d3d8_device_SetTextureStageState
,
3553 d3d8_device_ValidateDevice
,
3554 d3d8_device_GetInfo
,
3555 d3d8_device_SetPaletteEntries
,
3556 d3d8_device_GetPaletteEntries
,
3557 d3d8_device_SetCurrentTexturePalette
,
3558 d3d8_device_GetCurrentTexturePalette
,
3559 d3d8_device_DrawPrimitive
,
3560 d3d8_device_DrawIndexedPrimitive
,
3561 d3d8_device_DrawPrimitiveUP
,
3562 d3d8_device_DrawIndexedPrimitiveUP
,
3563 d3d8_device_ProcessVertices
,
3564 d3d8_device_CreateVertexShader
,
3565 d3d8_device_SetVertexShader
,
3566 d3d8_device_GetVertexShader
,
3567 d3d8_device_DeleteVertexShader
,
3568 d3d8_device_SetVertexShaderConstant
,
3569 d3d8_device_GetVertexShaderConstant
,
3570 d3d8_device_GetVertexShaderDeclaration
,
3571 d3d8_device_GetVertexShaderFunction
,
3572 d3d8_device_SetStreamSource
,
3573 d3d8_device_GetStreamSource
,
3574 d3d8_device_SetIndices
,
3575 d3d8_device_GetIndices
,
3576 d3d8_device_CreatePixelShader
,
3577 d3d8_device_SetPixelShader
,
3578 d3d8_device_GetPixelShader
,
3579 d3d8_device_DeletePixelShader
,
3580 d3d8_device_SetPixelShaderConstant
,
3581 d3d8_device_GetPixelShaderConstant
,
3582 d3d8_device_GetPixelShaderFunction
,
3583 d3d8_device_DrawRectPatch
,
3584 d3d8_device_DrawTriPatch
,
3585 d3d8_device_DeletePatch
,
3588 static inline struct d3d8_device
*device_from_device_parent(struct wined3d_device_parent
*device_parent
)
3590 return CONTAINING_RECORD(device_parent
, struct d3d8_device
, device_parent
);
3593 static void CDECL
device_parent_wined3d_device_created(struct wined3d_device_parent
*device_parent
,
3594 struct wined3d_device
*device
)
3596 TRACE("device_parent %p, device %p\n", device_parent
, device
);
3599 static void CDECL
device_parent_mode_changed(struct wined3d_device_parent
*device_parent
)
3601 TRACE("device_parent %p.\n", device_parent
);
3604 static void CDECL
device_parent_activate(struct wined3d_device_parent
*device_parent
, BOOL activate
)
3606 struct d3d8_device
*device
= device_from_device_parent(device_parent
);
3608 TRACE("device_parent %p, activate %#x.\n", device_parent
, activate
);
3611 InterlockedCompareExchange(&device
->device_state
, D3D8_DEVICE_STATE_LOST
, D3D8_DEVICE_STATE_OK
);
3613 InterlockedCompareExchange(&device
->device_state
, D3D8_DEVICE_STATE_NOT_RESET
, D3D8_DEVICE_STATE_LOST
);
3616 static HRESULT CDECL
device_parent_texture_sub_resource_created(struct wined3d_device_parent
*device_parent
,
3617 enum wined3d_resource_type type
, struct wined3d_texture
*wined3d_texture
, unsigned int sub_resource_idx
,
3618 void **parent
, const struct wined3d_parent_ops
**parent_ops
)
3620 TRACE("device_parent %p, type %#x, wined3d_texture %p, sub_resource_idx %u, parent %p, parent_ops %p.\n",
3621 device_parent
, type
, wined3d_texture
, sub_resource_idx
, parent
, parent_ops
);
3623 if (type
== WINED3D_RTYPE_TEXTURE_3D
)
3625 struct d3d8_volume
*d3d_volume
;
3627 if (!(d3d_volume
= heap_alloc_zero(sizeof(*d3d_volume
))))
3628 return E_OUTOFMEMORY
;
3630 volume_init(d3d_volume
, wined3d_texture
, sub_resource_idx
, parent_ops
);
3631 *parent
= d3d_volume
;
3632 TRACE("Created volume %p.\n", d3d_volume
);
3634 else if (type
!= WINED3D_RTYPE_TEXTURE_2D
)
3636 ERR("Unhandled resource type %#x.\n", type
);
3643 static const struct wined3d_device_parent_ops d3d8_wined3d_device_parent_ops
=
3645 device_parent_wined3d_device_created
,
3646 device_parent_mode_changed
,
3647 device_parent_activate
,
3648 device_parent_texture_sub_resource_created
,
3651 static void setup_fpu(void)
3653 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
3655 __asm__
volatile ("fnstcw %0" : "=m" (cw
));
3656 cw
= (cw
& ~0xf3f) | 0x3f;
3657 __asm__
volatile ("fldcw %0" : : "m" (cw
));
3658 #elif defined(__i386__) && defined(_MSC_VER)
3661 cw
= (cw
& ~0xf3f) | 0x3f;
3664 FIXME("FPU setup not implemented for this platform.\n");
3668 HRESULT
device_init(struct d3d8_device
*device
, struct d3d8
*parent
, struct wined3d
*wined3d
, UINT adapter
,
3669 D3DDEVTYPE device_type
, HWND focus_window
, DWORD flags
, D3DPRESENT_PARAMETERS
*parameters
)
3671 struct wined3d_swapchain_desc swapchain_desc
;
3672 struct wined3d_swapchain
*wined3d_swapchain
;
3673 struct wined3d_adapter
*wined3d_adapter
;
3674 struct wined3d_output
*wined3d_output
;
3675 struct d3d8_swapchain
*d3d_swapchain
;
3676 struct wined3d_caps caps
;
3677 unsigned int output_idx
;
3680 static const enum wined3d_feature_level feature_levels
[] =
3682 WINED3D_FEATURE_LEVEL_8
,
3683 WINED3D_FEATURE_LEVEL_7
,
3684 WINED3D_FEATURE_LEVEL_6
,
3685 WINED3D_FEATURE_LEVEL_5
,
3688 output_idx
= adapter
;
3689 if (output_idx
>= parent
->wined3d_output_count
)
3690 return D3DERR_INVALIDCALL
;
3692 device
->IDirect3DDevice8_iface
.lpVtbl
= &d3d8_device_vtbl
;
3693 device
->device_parent
.ops
= &d3d8_wined3d_device_parent_ops
;
3694 device
->adapter_ordinal
= adapter
;
3696 if (!(device
->handle_table
.entries
= heap_alloc_zero(D3D8_INITIAL_HANDLE_TABLE_SIZE
3697 * sizeof(*device
->handle_table
.entries
))))
3699 ERR("Failed to allocate handle table memory.\n");
3700 return E_OUTOFMEMORY
;
3702 device
->handle_table
.table_size
= D3D8_INITIAL_HANDLE_TABLE_SIZE
;
3704 if (!(flags
& D3DCREATE_FPU_PRESERVE
)) setup_fpu();
3706 wined3d_mutex_lock();
3707 wined3d_output
= parent
->wined3d_outputs
[output_idx
];
3708 wined3d_adapter
= wined3d_output_get_adapter(wined3d_output
);
3709 if (FAILED(hr
= wined3d_device_create(wined3d
, wined3d_adapter
, wined3d_device_type_from_d3d(device_type
),
3710 focus_window
, flags
, 4, feature_levels
, ARRAY_SIZE(feature_levels
),
3711 &device
->device_parent
, &device
->wined3d_device
)))
3713 WARN("Failed to create wined3d device, hr %#lx.\n", hr
);
3714 wined3d_mutex_unlock();
3715 heap_free(device
->handle_table
.entries
);
3719 device
->immediate_context
= wined3d_device_get_immediate_context(device
->wined3d_device
);
3720 wined3d_get_device_caps(wined3d_adapter
, wined3d_device_type_from_d3d(device_type
), &caps
);
3721 device
->max_user_clip_planes
= caps
.MaxUserClipPlanes
;
3722 device
->vs_uniform_count
= caps
.MaxVertexShaderConst
;
3724 if (FAILED(hr
= wined3d_stateblock_create(device
->wined3d_device
, NULL
, WINED3D_SBT_PRIMARY
, &device
->state
)))
3726 ERR("Failed to create primary stateblock, hr %#lx.\n", hr
);
3727 wined3d_device_decref(device
->wined3d_device
);
3728 wined3d_mutex_unlock();
3731 device
->stateblock_state
= wined3d_stateblock_get_state(device
->state
);
3732 device
->update_state
= device
->state
;
3734 wined3d_streaming_buffer_init(&device
->vertex_buffer
, WINED3D_BIND_VERTEX_BUFFER
);
3735 wined3d_streaming_buffer_init(&device
->index_buffer
, WINED3D_BIND_INDEX_BUFFER
);
3737 if (!parameters
->Windowed
)
3740 focus_window
= parameters
->hDeviceWindow
;
3741 if (FAILED(hr
= wined3d_device_acquire_focus_window(device
->wined3d_device
, focus_window
)))
3743 ERR("Failed to acquire focus window, hr %#lx.\n", hr
);
3744 wined3d_device_decref(device
->wined3d_device
);
3745 wined3d_mutex_unlock();
3746 heap_free(device
->handle_table
.entries
);
3751 if (flags
& D3DCREATE_MULTITHREADED
)
3752 wined3d_device_set_multithreaded(device
->wined3d_device
);
3754 if (!wined3d_swapchain_desc_from_d3d8(&swapchain_desc
, wined3d_output
, parameters
))
3756 wined3d_device_release_focus_window(device
->wined3d_device
);
3757 wined3d_device_decref(device
->wined3d_device
);
3758 wined3d_mutex_unlock();
3759 heap_free(device
->handle_table
.entries
);
3760 return D3DERR_INVALIDCALL
;
3762 swapchain_desc
.flags
|= WINED3D_SWAPCHAIN_IMPLICIT
;
3764 if (FAILED(hr
= d3d8_swapchain_create(device
, &swapchain_desc
,
3765 wined3dswapinterval_from_d3d(parameters
->FullScreen_PresentationInterval
), &d3d_swapchain
)))
3767 WARN("Failed to create implicit swapchain, hr %#lx.\n", hr
);
3768 wined3d_device_release_focus_window(device
->wined3d_device
);
3769 wined3d_device_decref(device
->wined3d_device
);
3770 wined3d_mutex_unlock();
3771 heap_free(device
->handle_table
.entries
);
3775 wined3d_swapchain
= d3d_swapchain
->wined3d_swapchain
;
3776 wined3d_swapchain_incref(wined3d_swapchain
);
3777 IDirect3DSwapChain8_Release(&d3d_swapchain
->IDirect3DSwapChain8_iface
);
3779 wined3d_stateblock_set_render_state(device
->state
, WINED3D_RS_ZENABLE
,
3780 !!swapchain_desc
.enable_auto_depth_stencil
);
3781 wined3d_stateblock_set_render_state(device
->state
, WINED3D_RS_POINTSIZE_MIN
, 0);
3782 device_reset_viewport_state(device
);
3783 wined3d_mutex_unlock();
3785 present_parameters_from_wined3d_swapchain_desc(parameters
,
3786 &swapchain_desc
, parameters
->FullScreen_PresentationInterval
);
3788 device
->declArraySize
= 16;
3789 if (!(device
->decls
= heap_alloc(device
->declArraySize
* sizeof(*device
->decls
))))
3791 ERR("Failed to allocate FVF vertex declaration map memory.\n");
3796 device
->implicit_swapchain
= wined3d_swapchain
;
3798 device
->d3d_parent
= parent
;
3799 IDirect3D8_AddRef(&parent
->IDirect3D8_iface
);
3804 wined3d_mutex_lock();
3805 wined3d_swapchain_decref(wined3d_swapchain
);
3806 wined3d_device_release_focus_window(device
->wined3d_device
);
3807 wined3d_device_decref(device
->wined3d_device
);
3808 wined3d_mutex_unlock();
3809 heap_free(device
->handle_table
.entries
);