comctl32/monthcal: Implement handler for WM_ERASEBKGND.
[wine] / dlls / d3d9 / vertexshader.c
1 /*
2  * IDirect3DVertexShader9 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 /* IDirect3DVertexShader9 IUnknown parts follow: */
28 static HRESULT WINAPI IDirect3DVertexShader9Impl_QueryInterface(LPDIRECT3DVERTEXSHADER9 iface, REFIID riid, LPVOID* ppobj) {
29     IDirect3DVertexShader9Impl *This = (IDirect3DVertexShader9Impl *)iface;
30
31     if (IsEqualGUID(riid, &IID_IUnknown)
32         || IsEqualGUID(riid, &IID_IDirect3DVertexShader9)) {
33         IDirect3DVertexShader9_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 IDirect3DVertexShader9Impl_AddRef(LPDIRECT3DVERTEXSHADER9 iface) {
44     IDirect3DVertexShader9Impl *This = (IDirect3DVertexShader9Impl *)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         IWineD3DVertexShader_AddRef(This->wineD3DVertexShader);
54         wined3d_mutex_unlock();
55     }
56
57     return ref;
58 }
59
60 static ULONG WINAPI IDirect3DVertexShader9Impl_Release(LPDIRECT3DVERTEXSHADER9 iface) {
61     IDirect3DVertexShader9Impl *This = (IDirect3DVertexShader9Impl *)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         IWineD3DVertexShader_Release(This->wineD3DVertexShader);
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 /* IDirect3DVertexShader9 Interface follow: */
80 static HRESULT WINAPI IDirect3DVertexShader9Impl_GetDevice(LPDIRECT3DVERTEXSHADER9 iface, IDirect3DDevice9** ppDevice) {
81     IDirect3DVertexShader9Impl *This = (IDirect3DVertexShader9Impl *)iface;
82     IWineD3DDevice *myDevice = NULL;
83     HRESULT hr;
84     TRACE("(%p) : Relay\n", This);
85
86     wined3d_mutex_lock();
87     hr = IWineD3DVertexShader_GetDevice(This->wineD3DVertexShader, &myDevice);
88     if (WINED3D_OK == hr && myDevice != NULL) {
89         hr = IWineD3DDevice_GetParent(myDevice, (IUnknown **)ppDevice);
90         IWineD3DDevice_Release(myDevice);
91     } else {
92         *ppDevice = NULL;
93     }
94     wined3d_mutex_unlock();
95
96     TRACE("(%p) returning (%p)\n", This, *ppDevice);
97     return hr;
98 }
99
100 static HRESULT WINAPI IDirect3DVertexShader9Impl_GetFunction(LPDIRECT3DVERTEXSHADER9 iface, VOID* pData, UINT* pSizeOfData) {
101     IDirect3DVertexShader9Impl *This = (IDirect3DVertexShader9Impl *)iface;
102     HRESULT hr;
103     TRACE("(%p) : Relay\n", This);
104
105     wined3d_mutex_lock();
106     hr = IWineD3DVertexShader_GetFunction(This->wineD3DVertexShader, pData, pSizeOfData);
107     wined3d_mutex_unlock();
108
109     return hr;
110 }
111
112
113 static const IDirect3DVertexShader9Vtbl Direct3DVertexShader9_Vtbl =
114 {
115     /* IUnknown */
116     IDirect3DVertexShader9Impl_QueryInterface,
117     IDirect3DVertexShader9Impl_AddRef,
118     IDirect3DVertexShader9Impl_Release,
119     /* IDirect3DVertexShader9 */
120     IDirect3DVertexShader9Impl_GetDevice,
121     IDirect3DVertexShader9Impl_GetFunction
122 };
123
124 static void STDMETHODCALLTYPE d3d9_vertexshader_wined3d_object_destroyed(void *parent)
125 {
126     HeapFree(GetProcessHeap(), 0, parent);
127 }
128
129 static const struct wined3d_parent_ops d3d9_vertexshader_wined3d_parent_ops =
130 {
131     d3d9_vertexshader_wined3d_object_destroyed,
132 };
133
134 HRESULT vertexshader_init(IDirect3DVertexShader9Impl *shader, IDirect3DDevice9Impl *device, const DWORD *byte_code)
135 {
136     HRESULT hr;
137
138     shader->ref = 1;
139     shader->lpVtbl = &Direct3DVertexShader9_Vtbl;
140
141     wined3d_mutex_lock();
142     hr = IWineD3DDevice_CreateVertexShader(device->WineD3DDevice, byte_code,
143             NULL /* output signature */, &shader->wineD3DVertexShader,
144             (IUnknown *)shader, &d3d9_vertexshader_wined3d_parent_ops);
145     wined3d_mutex_unlock();
146     if (FAILED(hr))
147     {
148         WARN("Failed to create wined3d vertex shader, hr %#x.\n", hr);
149         return hr;
150     }
151
152     shader->parentDevice = (IDirect3DDevice9Ex *)device;
153     IDirect3DDevice9Ex_AddRef(shader->parentDevice);
154
155     return D3D_OK;
156 }
157
158 HRESULT WINAPI IDirect3DDevice9Impl_SetVertexShader(LPDIRECT3DDEVICE9EX iface, IDirect3DVertexShader9* pShader) {
159     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
160     HRESULT hrc = D3D_OK;
161
162     TRACE("(%p) : Relay\n", This);
163
164     wined3d_mutex_lock();
165     hrc =  IWineD3DDevice_SetVertexShader(This->WineD3DDevice, pShader==NULL?NULL:((IDirect3DVertexShader9Impl *)pShader)->wineD3DVertexShader);
166     wined3d_mutex_unlock();
167
168     TRACE("(%p) : returning hr(%u)\n", This, hrc);
169     return hrc;
170 }
171
172 HRESULT WINAPI IDirect3DDevice9Impl_GetVertexShader(LPDIRECT3DDEVICE9EX iface, IDirect3DVertexShader9** ppShader) {
173     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
174     IWineD3DVertexShader *pShader;
175     HRESULT hrc = D3D_OK;
176
177     TRACE("(%p) : Relay  device@%p\n", This, This->WineD3DDevice);
178
179     wined3d_mutex_lock();
180     hrc = IWineD3DDevice_GetVertexShader(This->WineD3DDevice, &pShader);
181     if (SUCCEEDED(hrc))
182     {
183         if (pShader)
184         {
185             hrc = IWineD3DVertexShader_GetParent(pShader, (IUnknown **)ppShader);
186             IWineD3DVertexShader_Release(pShader);
187         }
188         else
189         {
190             *ppShader = NULL;
191         }
192     }
193     else
194     {
195         WARN("(%p) : Call to IWineD3DDevice_GetVertexShader failed %u (device %p)\n", This, hrc, This->WineD3DDevice);
196     }
197     wined3d_mutex_unlock();
198
199     TRACE("(%p) : returning %p\n", This, *ppShader);
200     return hrc;
201 }
202
203 HRESULT WINAPI IDirect3DDevice9Impl_SetVertexShaderConstantF(LPDIRECT3DDEVICE9EX iface, UINT Register, CONST float* pConstantData, UINT Vector4fCount) {
204     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
205     HRESULT hr;
206     TRACE("(%p) : Relay\n", This);
207
208     if(Register + Vector4fCount > D3D9_MAX_VERTEX_SHADER_CONSTANTF) {
209         WARN("Trying to access %u constants, but d3d9 only supports %u\n",
210              Register + Vector4fCount, D3D9_MAX_VERTEX_SHADER_CONSTANTF);
211         return D3DERR_INVALIDCALL;
212     }
213
214     wined3d_mutex_lock();
215     hr = IWineD3DDevice_SetVertexShaderConstantF(This->WineD3DDevice, Register, pConstantData, Vector4fCount);
216     wined3d_mutex_unlock();
217
218     return hr;
219 }
220
221 HRESULT WINAPI IDirect3DDevice9Impl_GetVertexShaderConstantF(LPDIRECT3DDEVICE9EX iface, UINT Register, float* pConstantData, UINT Vector4fCount) {
222     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
223     HRESULT hr;
224
225     if(Register + Vector4fCount > D3D9_MAX_VERTEX_SHADER_CONSTANTF) {
226         WARN("Trying to access %u constants, but d3d9 only supports %u\n",
227              Register + Vector4fCount, D3D9_MAX_VERTEX_SHADER_CONSTANTF);
228         return D3DERR_INVALIDCALL;
229     }
230
231     TRACE("(%p) : Relay\n", This);
232
233     wined3d_mutex_lock();
234     hr = IWineD3DDevice_GetVertexShaderConstantF(This->WineD3DDevice, Register, pConstantData, Vector4fCount);
235     wined3d_mutex_unlock();
236
237     return hr;
238 }
239
240 HRESULT WINAPI IDirect3DDevice9Impl_SetVertexShaderConstantI(LPDIRECT3DDEVICE9EX iface, UINT Register, CONST int* pConstantData, UINT Vector4iCount) {
241     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
242     HRESULT hr;
243     TRACE("(%p) : Relay\n", This);
244
245     wined3d_mutex_lock();
246     hr = IWineD3DDevice_SetVertexShaderConstantI(This->WineD3DDevice, Register, pConstantData, Vector4iCount);
247     wined3d_mutex_unlock();
248
249     return hr;
250 }
251
252 HRESULT WINAPI IDirect3DDevice9Impl_GetVertexShaderConstantI(LPDIRECT3DDEVICE9EX iface, UINT Register, int* pConstantData, UINT Vector4iCount) {
253     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
254     HRESULT hr;
255     TRACE("(%p) : Relay\n", This);
256
257     wined3d_mutex_lock();
258     hr = IWineD3DDevice_GetVertexShaderConstantI(This->WineD3DDevice, Register, pConstantData, Vector4iCount);
259     wined3d_mutex_unlock();
260
261     return hr;
262 }
263
264 HRESULT WINAPI IDirect3DDevice9Impl_SetVertexShaderConstantB(LPDIRECT3DDEVICE9EX iface, UINT Register, CONST BOOL* pConstantData, UINT BoolCount) {
265     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
266     HRESULT hr;
267     TRACE("(%p) : Relay\n", This);
268
269     wined3d_mutex_lock();
270     hr = IWineD3DDevice_SetVertexShaderConstantB(This->WineD3DDevice, Register, pConstantData, BoolCount);
271     wined3d_mutex_unlock();
272
273     return hr;
274 }
275
276 HRESULT WINAPI IDirect3DDevice9Impl_GetVertexShaderConstantB(LPDIRECT3DDEVICE9EX iface, UINT Register, BOOL* pConstantData, UINT BoolCount) {
277     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
278     HRESULT hr;
279     TRACE("(%p) : Relay\n", This);
280
281     wined3d_mutex_lock();
282     hr = IWineD3DDevice_GetVertexShaderConstantB(This->WineD3DDevice, Register, pConstantData, BoolCount);
283     wined3d_mutex_unlock();
284
285     return hr;
286 }