- Merge from trunk up to r45543
[reactos.git] / dll / directx / wine / d3d9 / stateblock.c
1 /*
2 * IDirect3DStateBlock9 implementation
3 *
4 * Copyright 2002-2003 Raphael Junqueira
5 * Copyright 2002-2003 Jason Edmeades
6 * Copyright 2005 Oliver Stieber
7 *
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.
12 *
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.
17 *
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
21 */
22
23 #include "config.h"
24 #include "d3d9_private.h"
25
26 WINE_DEFAULT_DEBUG_CHANNEL(d3d9);
27
28 /* IDirect3DStateBlock9 IUnknown parts follow: */
29 static HRESULT WINAPI IDirect3DStateBlock9Impl_QueryInterface(LPDIRECT3DSTATEBLOCK9 iface, REFIID riid, LPVOID* ppobj) {
30 IDirect3DStateBlock9Impl *This = (IDirect3DStateBlock9Impl *)iface;
31
32 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), ppobj);
33
34 if (IsEqualGUID(riid, &IID_IUnknown)
35 || IsEqualGUID(riid, &IID_IDirect3DStateBlock9)) {
36 IDirect3DStateBlock9_AddRef(iface);
37 *ppobj = This;
38 return S_OK;
39 }
40
41 WARN("(%p)->(%s,%p),not found\n", This, debugstr_guid(riid), ppobj);
42 *ppobj = NULL;
43 return E_NOINTERFACE;
44 }
45
46 static ULONG WINAPI IDirect3DStateBlock9Impl_AddRef(LPDIRECT3DSTATEBLOCK9 iface) {
47 IDirect3DStateBlock9Impl *This = (IDirect3DStateBlock9Impl *)iface;
48 ULONG ref = InterlockedIncrement(&This->ref);
49
50 TRACE("%p increasing refcount to %u.\n", iface, ref);
51
52 return ref;
53 }
54
55 static ULONG WINAPI IDirect3DStateBlock9Impl_Release(LPDIRECT3DSTATEBLOCK9 iface) {
56 IDirect3DStateBlock9Impl *This = (IDirect3DStateBlock9Impl *)iface;
57 ULONG ref = InterlockedDecrement(&This->ref);
58
59 TRACE("%p decreasing refcount to %u.\n", iface, ref);
60
61 if (ref == 0) {
62 wined3d_mutex_lock();
63 IWineD3DStateBlock_Release(This->wineD3DStateBlock);
64 wined3d_mutex_unlock();
65
66 IDirect3DDevice9Ex_Release(This->parentDevice);
67 HeapFree(GetProcessHeap(), 0, This);
68 }
69 return ref;
70 }
71
72 /* IDirect3DStateBlock9 Interface follow: */
73 static HRESULT WINAPI IDirect3DStateBlock9Impl_GetDevice(IDirect3DStateBlock9 *iface, IDirect3DDevice9 **device)
74 {
75 IDirect3DStateBlock9Impl *This = (IDirect3DStateBlock9Impl *)iface;
76
77 TRACE("iface %p, device %p.\n", iface, device);
78
79 *device = (IDirect3DDevice9 *)This->parentDevice;
80 IDirect3DDevice9_AddRef(*device);
81
82 TRACE("Returning device %p.\n", *device);
83
84 return D3D_OK;
85 }
86
87 static HRESULT WINAPI IDirect3DStateBlock9Impl_Capture(LPDIRECT3DSTATEBLOCK9 iface) {
88 IDirect3DStateBlock9Impl *This = (IDirect3DStateBlock9Impl *)iface;
89 HRESULT hr;
90
91 TRACE("iface %p.\n", iface);
92
93 wined3d_mutex_lock();
94 hr = IWineD3DStateBlock_Capture(This->wineD3DStateBlock);
95 wined3d_mutex_unlock();
96
97 return hr;
98 }
99
100 static HRESULT WINAPI IDirect3DStateBlock9Impl_Apply(LPDIRECT3DSTATEBLOCK9 iface) {
101 IDirect3DStateBlock9Impl *This = (IDirect3DStateBlock9Impl *)iface;
102 HRESULT hr;
103
104 TRACE("iface %p.\n", iface);
105
106 wined3d_mutex_lock();
107 hr = IWineD3DStateBlock_Apply(This->wineD3DStateBlock);
108 wined3d_mutex_unlock();
109
110 return hr;
111 }
112
113
114 static const IDirect3DStateBlock9Vtbl Direct3DStateBlock9_Vtbl =
115 {
116 /* IUnknown */
117 IDirect3DStateBlock9Impl_QueryInterface,
118 IDirect3DStateBlock9Impl_AddRef,
119 IDirect3DStateBlock9Impl_Release,
120 /* IDirect3DStateBlock9 */
121 IDirect3DStateBlock9Impl_GetDevice,
122 IDirect3DStateBlock9Impl_Capture,
123 IDirect3DStateBlock9Impl_Apply
124 };
125
126
127 /* IDirect3DDevice9 IDirect3DStateBlock9 Methods follow: */
128 HRESULT WINAPI IDirect3DDevice9Impl_CreateStateBlock(LPDIRECT3DDEVICE9EX iface, D3DSTATEBLOCKTYPE Type, IDirect3DStateBlock9** ppStateBlock) {
129 IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
130 IDirect3DStateBlock9Impl* object;
131 HRESULT hrc = D3D_OK;
132
133 TRACE("iface %p, type %#x, stateblock %p.\n", iface, Type, ppStateBlock);
134
135 if(Type != D3DSBT_ALL && Type != D3DSBT_PIXELSTATE &&
136 Type != D3DSBT_VERTEXSTATE ) {
137 WARN("Unexpected stateblock type, returning D3DERR_INVALIDCALL\n");
138 return D3DERR_INVALIDCALL;
139 }
140
141 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DStateBlock9Impl));
142 if (NULL == object) return E_OUTOFMEMORY;
143 object->lpVtbl = &Direct3DStateBlock9_Vtbl;
144 object->ref = 1;
145
146 wined3d_mutex_lock();
147 hrc = IWineD3DDevice_CreateStateBlock(This->WineD3DDevice, (WINED3DSTATEBLOCKTYPE)Type, &object->wineD3DStateBlock, (IUnknown*)object);
148 wined3d_mutex_unlock();
149
150 if(hrc != D3D_OK){
151 FIXME("(%p) Call to IWineD3DDevice_CreateStateBlock failed.\n", This);
152 HeapFree(GetProcessHeap(), 0, object);
153 } else {
154 IDirect3DDevice9Ex_AddRef(iface);
155 object->parentDevice = iface;
156 *ppStateBlock = (IDirect3DStateBlock9*)object;
157 TRACE("(%p) : Created stateblock %p\n", This, object);
158 }
159 TRACE("(%p) returning token (ptr to stateblock) of %p\n", This, object);
160 return hrc;
161 }
162
163 HRESULT WINAPI IDirect3DDevice9Impl_BeginStateBlock(IDirect3DDevice9Ex *iface)
164 {
165 IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
166 HRESULT hr;
167
168 TRACE("iface %p.\n", iface);
169
170 wined3d_mutex_lock();
171 hr = IWineD3DDevice_BeginStateBlock(This->WineD3DDevice);
172 wined3d_mutex_unlock();
173
174 return hr;
175 }
176
177 HRESULT WINAPI IDirect3DDevice9Impl_EndStateBlock(IDirect3DDevice9Ex *iface, IDirect3DStateBlock9 **ppSB)
178 {
179 IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
180 IWineD3DStateBlock *wineD3DStateBlock;
181 IDirect3DStateBlock9Impl *object;
182 HRESULT hr;
183
184 TRACE("iface %p, stateblock %p.\n", iface, ppSB);
185
186 /* Tell wineD3D to endstateblock before anything else (in case we run out
187 * of memory later and cause locking problems) */
188 wined3d_mutex_lock();
189 hr=IWineD3DDevice_EndStateBlock(This->WineD3DDevice,&wineD3DStateBlock);
190 wined3d_mutex_unlock();
191
192 if (hr!= D3D_OK)
193 {
194 WARN("IWineD3DDevice_EndStateBlock returned an error\n");
195 return hr;
196 }
197 /* allocate a new IDirectD3DStateBlock */
198 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DStateBlock9Impl));
199 if (!object) return E_OUTOFMEMORY;
200 object->ref = 1;
201 object->lpVtbl = &Direct3DStateBlock9_Vtbl;
202 object->wineD3DStateBlock = wineD3DStateBlock;
203
204 IDirect3DDevice9Ex_AddRef(iface);
205 object->parentDevice = iface;
206 *ppSB=(IDirect3DStateBlock9*)object;
207 TRACE("(%p) Returning *ppSB %p, wineD3DStateBlock %p\n", This, *ppSB, wineD3DStateBlock);
208 return D3D_OK;
209 }