ole32: We do need to implement Commit/Revert for non-root storage.
[wine] / dlls / d3d9 / pixelshader.c
1 /*
2  * IDirect3DPixelShader9 implementation
3  *
4  * Copyright 2002-2003 Jason Edmeades
5  *                     Raphael Junqueira
6  *
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.
11  *
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.
16  *
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
20  */
21
22 #include "config.h"
23 #include "d3d9_private.h"
24
25 WINE_DEFAULT_DEBUG_CHANNEL(d3d9);
26
27 /* IDirect3DPixelShader9 IUnknown parts follow: */
28 static HRESULT WINAPI IDirect3DPixelShader9Impl_QueryInterface(LPDIRECT3DPIXELSHADER9 iface, REFIID riid, LPVOID* ppobj) {
29     IDirect3DPixelShader9Impl *This = (IDirect3DPixelShader9Impl *)iface;
30
31     if (IsEqualGUID(riid, &IID_IUnknown)
32         || IsEqualGUID(riid, &IID_IDirect3DPixelShader9)) {
33         IDirect3DPixelShader9_AddRef(iface);
34         *ppobj = This;
35         return S_OK;
36     }
37
38     WARN("(%p)->(%s,%p),not found\n", This, debugstr_guid(riid), ppobj);
39     *ppobj = NULL;
40     return E_NOINTERFACE;
41 }
42
43 static ULONG WINAPI IDirect3DPixelShader9Impl_AddRef(LPDIRECT3DPIXELSHADER9 iface) {
44     IDirect3DPixelShader9Impl *This = (IDirect3DPixelShader9Impl *)iface;
45     ULONG ref = InterlockedIncrement(&This->ref);
46
47     TRACE("(%p) : AddRef from %d\n", This, ref - 1);
48
49     if (ref == 1)
50     {
51         IDirect3DDevice9Ex_AddRef(This->parentDevice);
52         wined3d_mutex_lock();
53         IWineD3DPixelShader_AddRef(This->wineD3DPixelShader);
54         wined3d_mutex_unlock();
55     }
56
57     return ref;
58 }
59
60 static ULONG WINAPI IDirect3DPixelShader9Impl_Release(LPDIRECT3DPIXELSHADER9 iface) {
61     IDirect3DPixelShader9Impl *This = (IDirect3DPixelShader9Impl *)iface;
62     ULONG ref = InterlockedDecrement(&This->ref);
63
64     TRACE("(%p) : ReleaseRef to %d\n", This, ref);
65
66     if (ref == 0) {
67         IDirect3DDevice9Ex *parentDevice = This->parentDevice;
68
69         wined3d_mutex_lock();
70         IWineD3DPixelShader_Release(This->wineD3DPixelShader);
71         wined3d_mutex_unlock();
72
73         /* Release the device last, as it may cause the device to be destroyed. */
74         IDirect3DDevice9Ex_Release(parentDevice);
75     }
76     return ref;
77 }
78
79 /* IDirect3DPixelShader9 Interface follow: */
80 static HRESULT WINAPI IDirect3DPixelShader9Impl_GetDevice(LPDIRECT3DPIXELSHADER9 iface, IDirect3DDevice9** ppDevice) {
81     IDirect3DPixelShader9Impl *This = (IDirect3DPixelShader9Impl *)iface;
82     IWineD3DDevice *myDevice = NULL;
83
84     TRACE("(%p) : Relay\n", This);
85
86     wined3d_mutex_lock();
87     IWineD3DPixelShader_GetDevice(This->wineD3DPixelShader, &myDevice);
88     IWineD3DDevice_GetParent(myDevice, (IUnknown **)ppDevice);
89     IWineD3DDevice_Release(myDevice);
90     wined3d_mutex_unlock();
91
92     TRACE("(%p) returning (%p)\n", This, *ppDevice);
93     return D3D_OK;
94 }
95
96 static HRESULT WINAPI IDirect3DPixelShader9Impl_GetFunction(LPDIRECT3DPIXELSHADER9 iface, VOID* pData, UINT* pSizeOfData) {
97     IDirect3DPixelShader9Impl *This = (IDirect3DPixelShader9Impl *)iface;
98     HRESULT hr;
99     TRACE("(%p) Relay\n", This);
100
101     wined3d_mutex_lock();
102     hr = IWineD3DPixelShader_GetFunction(This->wineD3DPixelShader, pData, pSizeOfData);
103     wined3d_mutex_unlock();
104
105     return hr;
106 }
107
108
109 static const IDirect3DPixelShader9Vtbl Direct3DPixelShader9_Vtbl =
110 {
111     /* IUnknown */
112     IDirect3DPixelShader9Impl_QueryInterface,
113     IDirect3DPixelShader9Impl_AddRef,
114     IDirect3DPixelShader9Impl_Release,
115     /* IDirect3DPixelShader9 */
116     IDirect3DPixelShader9Impl_GetDevice,
117     IDirect3DPixelShader9Impl_GetFunction
118 };
119
120 static void STDMETHODCALLTYPE d3d9_pixelshader_wined3d_object_destroyed(void *parent)
121 {
122     HeapFree(GetProcessHeap(), 0, parent);
123 }
124
125 static const struct wined3d_parent_ops d3d9_pixelshader_wined3d_parent_ops =
126 {
127     d3d9_pixelshader_wined3d_object_destroyed,
128 };
129
130 HRESULT pixelshader_init(IDirect3DPixelShader9Impl *shader, IDirect3DDevice9Impl *device, const DWORD *byte_code)
131 {
132     HRESULT hr;
133
134     shader->ref = 1;
135     shader->lpVtbl = &Direct3DPixelShader9_Vtbl;
136
137     wined3d_mutex_lock();
138     hr = IWineD3DDevice_CreatePixelShader(device->WineD3DDevice, byte_code,
139             NULL, &shader->wineD3DPixelShader, (IUnknown *)shader,
140             &d3d9_pixelshader_wined3d_parent_ops);
141     wined3d_mutex_unlock();
142     if (FAILED(hr))
143     {
144         WARN("Failed to created wined3d pixel shader, hr %#x.\n", hr);
145         return hr;
146     }
147
148     shader->parentDevice = (IDirect3DDevice9Ex *)device;
149     IDirect3DDevice9Ex_AddRef(shader->parentDevice);
150
151     return D3D_OK;
152 }
153
154 HRESULT WINAPI IDirect3DDevice9Impl_SetPixelShader(LPDIRECT3DDEVICE9EX iface, IDirect3DPixelShader9* pShader) {
155     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
156     IDirect3DPixelShader9Impl *shader = (IDirect3DPixelShader9Impl *)pShader;
157     TRACE("(%p) Relay\n", This);
158
159     wined3d_mutex_lock();
160     IWineD3DDevice_SetPixelShader(This->WineD3DDevice, shader == NULL ? NULL :shader->wineD3DPixelShader);
161     wined3d_mutex_unlock();
162
163     return D3D_OK;
164 }
165
166 HRESULT WINAPI IDirect3DDevice9Impl_GetPixelShader(LPDIRECT3DDEVICE9EX iface, IDirect3DPixelShader9** ppShader) {
167     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
168     IWineD3DPixelShader *object;
169
170     HRESULT hrc = D3D_OK;
171     TRACE("(%p) Relay\n", This);
172     if (ppShader == NULL) {
173         TRACE("(%p) Invalid call\n", This);
174         return D3DERR_INVALIDCALL;
175     }
176
177     wined3d_mutex_lock();
178     hrc = IWineD3DDevice_GetPixelShader(This->WineD3DDevice, &object);
179     if (SUCCEEDED(hrc))
180     {
181         if (object)
182         {
183             hrc = IWineD3DPixelShader_GetParent(object, (IUnknown **)ppShader);
184             IWineD3DPixelShader_Release(object);
185         }
186         else
187         {
188             *ppShader = NULL;
189         }
190     }
191     else
192     {
193         WARN("(%p) : Call to IWineD3DDevice_GetPixelShader failed %u (device %p)\n", This, hrc, This->WineD3DDevice);
194     }
195     wined3d_mutex_unlock();
196
197     TRACE("(%p) : returning %p\n", This, *ppShader);
198     return hrc;
199 }
200
201 HRESULT WINAPI IDirect3DDevice9Impl_SetPixelShaderConstantF(LPDIRECT3DDEVICE9EX iface, UINT Register, CONST float* pConstantData, UINT Vector4fCount) {
202    IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
203     HRESULT hr;
204     TRACE("(%p) Relay\n", This);
205
206     wined3d_mutex_lock();
207     hr = IWineD3DDevice_SetPixelShaderConstantF(This->WineD3DDevice, Register, pConstantData, Vector4fCount);
208     wined3d_mutex_unlock();
209
210     return hr;
211 }
212
213 HRESULT WINAPI IDirect3DDevice9Impl_GetPixelShaderConstantF(LPDIRECT3DDEVICE9EX iface, UINT Register, float* pConstantData, UINT Vector4fCount) {
214     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
215     HRESULT hr;
216
217     TRACE("(%p) Relay\n", This);
218
219     wined3d_mutex_lock();
220     hr = IWineD3DDevice_GetPixelShaderConstantF(This->WineD3DDevice, Register, pConstantData, Vector4fCount);
221     wined3d_mutex_unlock();
222
223     return hr;
224 }
225
226 HRESULT WINAPI IDirect3DDevice9Impl_SetPixelShaderConstantI(LPDIRECT3DDEVICE9EX iface, UINT Register, CONST int* pConstantData, UINT Vector4iCount) {
227     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
228     HRESULT hr;
229     TRACE("(%p) Relay\n", This);
230
231     wined3d_mutex_lock();
232     hr = IWineD3DDevice_SetPixelShaderConstantI(This->WineD3DDevice, Register, pConstantData, Vector4iCount);
233     wined3d_mutex_unlock();
234
235     return hr;
236 }
237
238 HRESULT WINAPI IDirect3DDevice9Impl_GetPixelShaderConstantI(LPDIRECT3DDEVICE9EX iface, UINT Register, int* pConstantData, UINT Vector4iCount) {
239     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
240     HRESULT hr;
241     TRACE("(%p) Relay\n", This);
242
243     wined3d_mutex_lock();
244     hr = IWineD3DDevice_GetPixelShaderConstantI(This->WineD3DDevice, Register, pConstantData, Vector4iCount);
245     wined3d_mutex_unlock();
246
247     return hr;
248 }
249
250 HRESULT WINAPI IDirect3DDevice9Impl_SetPixelShaderConstantB(LPDIRECT3DDEVICE9EX iface, UINT Register, CONST BOOL* pConstantData, UINT BoolCount) {
251     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
252     HRESULT hr;
253     TRACE("(%p) Relay\n", This);
254
255     wined3d_mutex_lock();
256     hr = IWineD3DDevice_SetPixelShaderConstantB(This->WineD3DDevice, Register, pConstantData, BoolCount);
257     wined3d_mutex_unlock();
258
259     return hr;
260 }
261
262 HRESULT WINAPI IDirect3DDevice9Impl_GetPixelShaderConstantB(LPDIRECT3DDEVICE9EX iface, UINT Register, BOOL* pConstantData, UINT BoolCount) {
263     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
264     HRESULT hr;
265     TRACE("(%p) Relay\n", This);
266
267     wined3d_mutex_lock();
268     hr = IWineD3DDevice_GetPixelShaderConstantB(This->WineD3DDevice, Register, pConstantData, BoolCount);
269     wined3d_mutex_unlock();
270
271     return hr;
272 }