2 * IDirect3DStateBlock9 implementation
4 * Copyright 2002-2003 Raphael Junqueira
5 * Copyright 2002-2003 Jason Edmeades
6 * Copyright 2005 Oliver Stieber
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #include "wine/port.h"
25 #include "d3d9_private.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(d3d9
);
29 static inline struct d3d9_stateblock
*impl_from_IDirect3DStateBlock9(IDirect3DStateBlock9
*iface
)
31 return CONTAINING_RECORD(iface
, struct d3d9_stateblock
, IDirect3DStateBlock9_iface
);
34 static HRESULT WINAPI
d3d9_stateblock_QueryInterface(IDirect3DStateBlock9
*iface
, REFIID riid
, void **out
)
36 TRACE("iface %p, riid %s, out %p.\n", iface
, debugstr_guid(riid
), out
);
38 if (IsEqualGUID(riid
, &IID_IDirect3DStateBlock9
)
39 || IsEqualGUID(riid
, &IID_IUnknown
))
41 IDirect3DStateBlock9_AddRef(iface
);
46 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid
));
52 static ULONG WINAPI
d3d9_stateblock_AddRef(IDirect3DStateBlock9
*iface
)
54 struct d3d9_stateblock
*stateblock
= impl_from_IDirect3DStateBlock9(iface
);
55 ULONG refcount
= InterlockedIncrement(&stateblock
->refcount
);
57 TRACE("%p increasing refcount to %u.\n", iface
, refcount
);
62 static ULONG WINAPI
d3d9_stateblock_Release(IDirect3DStateBlock9
*iface
)
64 struct d3d9_stateblock
*stateblock
= impl_from_IDirect3DStateBlock9(iface
);
65 ULONG refcount
= InterlockedDecrement(&stateblock
->refcount
);
67 TRACE("%p decreasing refcount to %u.\n", iface
, refcount
);
72 wined3d_stateblock_decref(stateblock
->wined3d_stateblock
);
73 wined3d_mutex_unlock();
75 IDirect3DDevice9Ex_Release(stateblock
->parent_device
);
76 heap_free(stateblock
);
82 static HRESULT WINAPI
d3d9_stateblock_GetDevice(IDirect3DStateBlock9
*iface
, IDirect3DDevice9
**device
)
84 struct d3d9_stateblock
*stateblock
= impl_from_IDirect3DStateBlock9(iface
);
86 TRACE("iface %p, device %p.\n", iface
, device
);
88 *device
= (IDirect3DDevice9
*)stateblock
->parent_device
;
89 IDirect3DDevice9_AddRef(*device
);
91 TRACE("Returning device %p.\n", *device
);
96 static HRESULT WINAPI
d3d9_stateblock_Capture(IDirect3DStateBlock9
*iface
)
98 struct d3d9_stateblock
*stateblock
= impl_from_IDirect3DStateBlock9(iface
);
99 struct d3d9_device
*device
;
101 TRACE("iface %p.\n", iface
);
103 wined3d_mutex_lock();
104 device
= impl_from_IDirect3DDevice9Ex(stateblock
->parent_device
);
105 if (device
->recording
)
107 wined3d_mutex_unlock();
108 WARN("Trying to capture stateblock while recording, returning D3DERR_INVALIDCALL.\n");
109 return D3DERR_INVALIDCALL
;
111 wined3d_stateblock_capture(stateblock
->wined3d_stateblock
);
112 wined3d_mutex_unlock();
117 static HRESULT WINAPI
d3d9_stateblock_Apply(IDirect3DStateBlock9
*iface
)
119 struct d3d9_stateblock
*stateblock
= impl_from_IDirect3DStateBlock9(iface
);
120 struct wined3d_texture
*wined3d_texture
;
121 unsigned int i
, offset
, stride
, stage
;
122 struct wined3d_buffer
*wined3d_buffer
;
123 struct d3d9_vertexbuffer
*buffer
;
124 enum wined3d_format_id format
;
125 struct d3d9_texture
*texture
;
126 struct d3d9_device
*device
;
129 TRACE("iface %p.\n", iface
);
131 wined3d_mutex_lock();
132 device
= impl_from_IDirect3DDevice9Ex(stateblock
->parent_device
);
133 if (device
->recording
)
135 wined3d_mutex_unlock();
136 WARN("Trying to apply stateblock while recording, returning D3DERR_INVALIDCALL.\n");
137 return D3DERR_INVALIDCALL
;
139 wined3d_stateblock_apply(stateblock
->wined3d_stateblock
);
140 device
->sysmem_vb
= 0;
141 for (i
= 0; i
< D3D9_MAX_STREAMS
; ++i
)
143 if (FAILED(hr
= wined3d_device_get_stream_source(device
->wined3d_device
,
144 i
, &wined3d_buffer
, &offset
, &stride
)))
146 if (!wined3d_buffer
|| !(buffer
= wined3d_buffer_get_parent(wined3d_buffer
)))
148 if (buffer
->draw_buffer
)
149 device
->sysmem_vb
|= 1u << i
;
151 device
->sysmem_ib
= (wined3d_buffer
= wined3d_device_get_index_buffer(device
->wined3d_device
, &format
, &offset
))
152 && (buffer
= wined3d_buffer_get_parent(wined3d_buffer
)) && buffer
->draw_buffer
;
153 device
->auto_mipmaps
= 0;
154 for (i
= 0; i
< D3D9_MAX_TEXTURE_UNITS
; ++i
)
156 stage
= i
>= 16 ? i
- 16 + D3DVERTEXTEXTURESAMPLER0
: i
;
158 if ((wined3d_texture
= wined3d_device_get_texture(device
->wined3d_device
, stage
))
159 && (texture
= wined3d_texture_get_parent(wined3d_texture
))
160 && texture
->usage
& D3DUSAGE_AUTOGENMIPMAP
)
161 device
->auto_mipmaps
|= 1u << i
;
163 device
->auto_mipmaps
&= ~(1u << i
);
165 wined3d_mutex_unlock();
171 static const struct IDirect3DStateBlock9Vtbl d3d9_stateblock_vtbl
=
174 d3d9_stateblock_QueryInterface
,
175 d3d9_stateblock_AddRef
,
176 d3d9_stateblock_Release
,
177 /* IDirect3DStateBlock9 */
178 d3d9_stateblock_GetDevice
,
179 d3d9_stateblock_Capture
,
180 d3d9_stateblock_Apply
,
183 HRESULT
stateblock_init(struct d3d9_stateblock
*stateblock
, struct d3d9_device
*device
,
184 D3DSTATEBLOCKTYPE type
, struct wined3d_stateblock
*wined3d_stateblock
)
188 stateblock
->IDirect3DStateBlock9_iface
.lpVtbl
= &d3d9_stateblock_vtbl
;
189 stateblock
->refcount
= 1;
191 if (wined3d_stateblock
)
193 stateblock
->wined3d_stateblock
= wined3d_stateblock
;
197 wined3d_mutex_lock();
198 hr
= wined3d_stateblock_create(device
->wined3d_device
,
199 (enum wined3d_stateblock_type
)type
, &stateblock
->wined3d_stateblock
);
200 wined3d_mutex_unlock();
203 WARN("Failed to create wined3d stateblock, hr %#x.\n", hr
);
208 stateblock
->parent_device
= &device
->IDirect3DDevice9Ex_iface
;
209 IDirect3DDevice9Ex_AddRef(stateblock
->parent_device
);