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