iphlpapi/tests: Fix crash on Vista.
[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         IUnknown_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     return ref;
50 }
51
52 static ULONG WINAPI IDirect3DVertexShader9Impl_Release(LPDIRECT3DVERTEXSHADER9 iface) {
53     IDirect3DVertexShader9Impl *This = (IDirect3DVertexShader9Impl *)iface;
54     ULONG ref = InterlockedDecrement(&This->ref);
55
56     TRACE("(%p) : ReleaseRef to %d\n", This, ref);
57
58     if (ref == 0) {
59         EnterCriticalSection(&d3d9_cs);
60         IWineD3DVertexShader_Release(This->wineD3DVertexShader);
61         LeaveCriticalSection(&d3d9_cs);
62         IUnknown_Release(This->parentDevice);
63         HeapFree(GetProcessHeap(), 0, This);
64     }
65     return ref;
66 }
67
68 /* IDirect3DVertexShader9 Interface follow: */
69 static HRESULT WINAPI IDirect3DVertexShader9Impl_GetDevice(LPDIRECT3DVERTEXSHADER9 iface, IDirect3DDevice9** ppDevice) {
70     IDirect3DVertexShader9Impl *This = (IDirect3DVertexShader9Impl *)iface;
71     IWineD3DDevice *myDevice = NULL;
72     HRESULT hr = D3D_OK;
73     TRACE("(%p) : Relay\n", This);
74
75     EnterCriticalSection(&d3d9_cs);
76     if (D3D_OK == (hr = IWineD3DVertexShader_GetDevice(This->wineD3DVertexShader, &myDevice) && myDevice != NULL)) {
77         hr = IWineD3DDevice_GetParent(myDevice, (IUnknown **)ppDevice);
78         IWineD3DDevice_Release(myDevice);
79     } else {
80         *ppDevice = NULL;
81     }
82     LeaveCriticalSection(&d3d9_cs);
83     TRACE("(%p) returning (%p)\n", This, *ppDevice);
84     return hr;
85 }
86
87 static HRESULT WINAPI IDirect3DVertexShader9Impl_GetFunction(LPDIRECT3DVERTEXSHADER9 iface, VOID* pData, UINT* pSizeOfData) {
88     IDirect3DVertexShader9Impl *This = (IDirect3DVertexShader9Impl *)iface;
89     HRESULT hr;
90     TRACE("(%p) : Relay\n", This);
91
92     EnterCriticalSection(&d3d9_cs);
93     hr = IWineD3DVertexShader_GetFunction(This->wineD3DVertexShader, pData, pSizeOfData);
94     LeaveCriticalSection(&d3d9_cs);
95     return hr;
96 }
97
98
99 static const IDirect3DVertexShader9Vtbl Direct3DVertexShader9_Vtbl =
100 {
101     /* IUnknown */
102     IDirect3DVertexShader9Impl_QueryInterface,
103     IDirect3DVertexShader9Impl_AddRef,
104     IDirect3DVertexShader9Impl_Release,
105     /* IDirect3DVertexShader9 */
106     IDirect3DVertexShader9Impl_GetDevice,
107     IDirect3DVertexShader9Impl_GetFunction
108 };
109
110
111 /* IDirect3DDevice9 IDirect3DVertexShader9 Methods follow: */
112 HRESULT WINAPI IDirect3DDevice9Impl_CreateVertexShader(LPDIRECT3DDEVICE9EX iface, CONST DWORD* pFunction, IDirect3DVertexShader9** ppShader) {
113     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
114     HRESULT hrc = D3D_OK;
115     IDirect3DVertexShader9Impl *object;
116
117     /* Setup a stub object for now */
118     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
119     TRACE("(%p) : pFunction(%p), ppShader(%p)\n", This, pFunction, ppShader);
120     if (NULL == object) {
121         FIXME("Allocation of memory failed, returning D3DERR_OUTOFVIDEOMEMORY\n");
122         return D3DERR_OUTOFVIDEOMEMORY;
123     }
124
125     object->ref = 1;
126     object->lpVtbl = &Direct3DVertexShader9_Vtbl;
127     EnterCriticalSection(&d3d9_cs);
128     hrc= IWineD3DDevice_CreateVertexShader(This->WineD3DDevice, NULL /* declaration */, pFunction, &object->wineD3DVertexShader, (IUnknown *)object);
129     LeaveCriticalSection(&d3d9_cs);
130
131     if (FAILED(hrc)) {
132
133         /* free up object */
134         FIXME("Call to IWineD3DDevice_CreateVertexShader failed\n");
135         HeapFree(GetProcessHeap(), 0, object);
136     }else{
137         IUnknown_AddRef(iface);
138         object->parentDevice = iface;
139         *ppShader = (IDirect3DVertexShader9 *)object;
140         TRACE("(%p) : Created vertex shader %p\n", This, object);
141     }
142
143     TRACE("(%p) : returning %p\n", This, *ppShader);
144     return hrc;
145 }
146
147 HRESULT WINAPI IDirect3DDevice9Impl_SetVertexShader(LPDIRECT3DDEVICE9EX iface, IDirect3DVertexShader9* pShader) {
148     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
149     HRESULT hrc = D3D_OK;
150
151     TRACE("(%p) : Relay\n", This);
152     EnterCriticalSection(&d3d9_cs);
153     hrc =  IWineD3DDevice_SetVertexShader(This->WineD3DDevice, pShader==NULL?NULL:((IDirect3DVertexShader9Impl *)pShader)->wineD3DVertexShader);
154     LeaveCriticalSection(&d3d9_cs);
155
156     TRACE("(%p) : returning hr(%u)\n", This, hrc);
157     return hrc;
158 }
159
160 HRESULT WINAPI IDirect3DDevice9Impl_GetVertexShader(LPDIRECT3DDEVICE9EX iface, IDirect3DVertexShader9** ppShader) {
161     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
162     IWineD3DVertexShader *pShader;
163     HRESULT hrc = D3D_OK;
164
165     TRACE("(%p) : Relay  device@%p\n", This, This->WineD3DDevice);
166     EnterCriticalSection(&d3d9_cs);
167     hrc = IWineD3DDevice_GetVertexShader(This->WineD3DDevice, &pShader);
168     if(hrc == D3D_OK && pShader != NULL){
169        hrc = IWineD3DVertexShader_GetParent(pShader, (IUnknown **)ppShader);
170        IWineD3DVertexShader_Release(pShader);
171     } else {
172         WARN("(%p) : Call to IWineD3DDevice_GetVertexShader failed %u (device %p)\n", This, hrc, This->WineD3DDevice);
173     }
174     LeaveCriticalSection(&d3d9_cs);
175     TRACE("(%p) : returning %p\n", This, *ppShader);
176     return hrc;
177 }
178
179 HRESULT WINAPI IDirect3DDevice9Impl_SetVertexShaderConstantF(LPDIRECT3DDEVICE9EX iface, UINT Register, CONST float* pConstantData, UINT Vector4fCount) {
180     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
181     HRESULT hr;
182     TRACE("(%p) : Relay\n", This);
183
184     EnterCriticalSection(&d3d9_cs);
185     hr = IWineD3DDevice_SetVertexShaderConstantF(This->WineD3DDevice, Register, pConstantData, Vector4fCount);
186     LeaveCriticalSection(&d3d9_cs);
187     return hr;
188 }
189
190 HRESULT WINAPI IDirect3DDevice9Impl_GetVertexShaderConstantF(LPDIRECT3DDEVICE9EX iface, UINT Register, float* pConstantData, UINT Vector4fCount) {
191     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
192     HRESULT hr;
193
194     TRACE("(%p) : Relay\n", This);
195     EnterCriticalSection(&d3d9_cs);
196     hr = IWineD3DDevice_GetVertexShaderConstantF(This->WineD3DDevice, Register, pConstantData, Vector4fCount);
197     LeaveCriticalSection(&d3d9_cs);
198     return hr;
199 }
200
201 HRESULT WINAPI IDirect3DDevice9Impl_SetVertexShaderConstantI(LPDIRECT3DDEVICE9EX iface, UINT Register, CONST int* pConstantData, UINT Vector4iCount) {
202     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
203     HRESULT hr;
204     TRACE("(%p) : Relay\n", This);
205
206     EnterCriticalSection(&d3d9_cs);
207     hr = IWineD3DDevice_SetVertexShaderConstantI(This->WineD3DDevice, Register, pConstantData, Vector4iCount);
208     LeaveCriticalSection(&d3d9_cs);
209     return hr;
210 }
211
212 HRESULT WINAPI IDirect3DDevice9Impl_GetVertexShaderConstantI(LPDIRECT3DDEVICE9EX iface, UINT Register, int* pConstantData, UINT Vector4iCount) {
213     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
214     HRESULT hr;
215     TRACE("(%p) : Relay\n", This);
216
217     EnterCriticalSection(&d3d9_cs);
218     hr = IWineD3DDevice_GetVertexShaderConstantI(This->WineD3DDevice, Register, pConstantData, Vector4iCount);
219     LeaveCriticalSection(&d3d9_cs);
220     return hr;
221 }
222
223 HRESULT WINAPI IDirect3DDevice9Impl_SetVertexShaderConstantB(LPDIRECT3DDEVICE9EX iface, UINT Register, CONST BOOL* pConstantData, UINT BoolCount) {
224     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
225     HRESULT hr;
226     TRACE("(%p) : Relay\n", This);
227
228     EnterCriticalSection(&d3d9_cs);
229     hr = IWineD3DDevice_SetVertexShaderConstantB(This->WineD3DDevice, Register, pConstantData, BoolCount);
230     LeaveCriticalSection(&d3d9_cs);
231     return hr;
232 }
233
234 HRESULT WINAPI IDirect3DDevice9Impl_GetVertexShaderConstantB(LPDIRECT3DDEVICE9EX iface, UINT Register, BOOL* pConstantData, UINT BoolCount) {
235     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
236     HRESULT hr;
237     TRACE("(%p) : Relay\n", This);
238
239     EnterCriticalSection(&d3d9_cs);
240     hr = IWineD3DDevice_GetVertexShaderConstantB(This->WineD3DDevice, Register, pConstantData, BoolCount);
241     LeaveCriticalSection(&d3d9_cs);
242     return hr;
243 }