29ff5b34d190310c8332e298999edcd8de2e937c
[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 if (IsEqualGUID(riid, &IID_IUnknown)
33 || IsEqualGUID(riid, &IID_IDirect3DStateBlock9)) {
34 IDirect3DStateBlock9_AddRef(iface);
35 *ppobj = This;
36 return S_OK;
37 }
38
39 WARN("(%p)->(%s,%p),not found\n", This, debugstr_guid(riid), ppobj);
40 *ppobj = NULL;
41 return E_NOINTERFACE;
42 }
43
44 static ULONG WINAPI IDirect3DStateBlock9Impl_AddRef(LPDIRECT3DSTATEBLOCK9 iface) {
45 IDirect3DStateBlock9Impl *This = (IDirect3DStateBlock9Impl *)iface;
46 ULONG ref = InterlockedIncrement(&This->ref);
47
48 TRACE("(%p) : AddRef from %d\n", This, ref - 1);
49
50 return ref;
51 }
52
53 static ULONG WINAPI IDirect3DStateBlock9Impl_Release(LPDIRECT3DSTATEBLOCK9 iface) {
54 IDirect3DStateBlock9Impl *This = (IDirect3DStateBlock9Impl *)iface;
55 ULONG ref = InterlockedDecrement(&This->ref);
56
57 TRACE("(%p) : ReleaseRef to %d\n", This, ref);
58
59 if (ref == 0) {
60 wined3d_mutex_lock();
61 IWineD3DStateBlock_Release(This->wineD3DStateBlock);
62 wined3d_mutex_unlock();
63
64 IDirect3DDevice9Ex_Release(This->parentDevice);
65 HeapFree(GetProcessHeap(), 0, This);
66 }
67 return ref;
68 }
69
70 /* IDirect3DStateBlock9 Interface follow: */
71 static HRESULT WINAPI IDirect3DStateBlock9Impl_GetDevice(LPDIRECT3DSTATEBLOCK9 iface, IDirect3DDevice9** ppDevice) {
72 IDirect3DStateBlock9Impl *This = (IDirect3DStateBlock9Impl *)iface;
73 IWineD3DDevice *wined3d_device;
74 HRESULT hr;
75 TRACE("(%p) Relay\n", This);
76
77 wined3d_mutex_lock();
78 hr = IWineD3DStateBlock_GetDevice(This->wineD3DStateBlock, &wined3d_device);
79 if (SUCCEEDED(hr))
80 {
81 IWineD3DDevice_GetParent(wined3d_device, (IUnknown **)ppDevice);
82 IWineD3DDevice_Release(wined3d_device);
83 }
84 wined3d_mutex_unlock();
85
86 return hr;
87 }
88
89 static HRESULT WINAPI IDirect3DStateBlock9Impl_Capture(LPDIRECT3DSTATEBLOCK9 iface) {
90 IDirect3DStateBlock9Impl *This = (IDirect3DStateBlock9Impl *)iface;
91 HRESULT hr;
92 TRACE("(%p) Relay\n", This);
93
94 wined3d_mutex_lock();
95 hr = IWineD3DStateBlock_Capture(This->wineD3DStateBlock);
96 wined3d_mutex_unlock();
97
98 return hr;
99 }
100
101 static HRESULT WINAPI IDirect3DStateBlock9Impl_Apply(LPDIRECT3DSTATEBLOCK9 iface) {
102 IDirect3DStateBlock9Impl *This = (IDirect3DStateBlock9Impl *)iface;
103 HRESULT hr;
104 TRACE("(%p) Relay\n", This);
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("(%p) Relay\n", This);
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 TRACE("(%p) Relay\n", This);
168
169 wined3d_mutex_lock();
170 hr = IWineD3DDevice_BeginStateBlock(This->WineD3DDevice);
171 wined3d_mutex_unlock();
172
173 return hr;
174 }
175
176 HRESULT WINAPI IDirect3DDevice9Impl_EndStateBlock(IDirect3DDevice9Ex *iface, IDirect3DStateBlock9 **ppSB)
177 {
178 IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
179 IWineD3DStateBlock *wineD3DStateBlock;
180 IDirect3DStateBlock9Impl *object;
181 HRESULT hr;
182
183 TRACE("(%p) Relay\n", This);
184
185 /* Tell wineD3D to endstateblock before anything else (in case we run out
186 * of memory later and cause locking problems) */
187 wined3d_mutex_lock();
188 hr=IWineD3DDevice_EndStateBlock(This->WineD3DDevice,&wineD3DStateBlock);
189 wined3d_mutex_unlock();
190
191 if (hr!= D3D_OK)
192 {
193 WARN("IWineD3DDevice_EndStateBlock returned an error\n");
194 return hr;
195 }
196 /* allocate a new IDirectD3DStateBlock */
197 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DStateBlock9Impl));
198 if (!object) return E_OUTOFMEMORY;
199 object->ref = 1;
200 object->lpVtbl = &Direct3DStateBlock9_Vtbl;
201 object->wineD3DStateBlock = wineD3DStateBlock;
202
203 IDirect3DDevice9Ex_AddRef(iface);
204 object->parentDevice = iface;
205 *ppSB=(IDirect3DStateBlock9*)object;
206 TRACE("(%p) Returning *ppSB %p, wineD3DStateBlock %p\n", This, *ppSB, wineD3DStateBlock);
207 return D3D_OK;
208 }