wined3d: Implement more GLSL instructions and a little cleanup.
[wine] / dlls / d3d9 / vertexdeclaration.c
1 /*
2  * IDirect3DVertexDeclaration9 implementation
3  *
4  * Copyright 2002-2003 Raphael Junqueira
5  *                     Jason Edmeades
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 /* IDirect3DVertexDeclaration9 IUnknown parts follow: */
28 static HRESULT WINAPI IDirect3DVertexDeclaration9Impl_QueryInterface(LPDIRECT3DVERTEXDECLARATION9 iface, REFIID riid, LPVOID* ppobj) {
29     IDirect3DVertexDeclaration9Impl *This = (IDirect3DVertexDeclaration9Impl *)iface;
30
31     if (IsEqualGUID(riid, &IID_IUnknown)
32         || IsEqualGUID(riid, &IID_IDirect3DVertexDeclaration9)) {
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 IDirect3DVertexDeclaration9Impl_AddRef(LPDIRECT3DVERTEXDECLARATION9 iface) {
44     IDirect3DVertexDeclaration9Impl *This = (IDirect3DVertexDeclaration9Impl *)iface;
45     ULONG ref = InterlockedIncrement(&This->ref);
46
47     TRACE("(%p) : AddRef from %ld\n", This, ref - 1);
48
49     return ref;
50 }
51
52 static ULONG WINAPI IDirect3DVertexDeclaration9Impl_Release(LPDIRECT3DVERTEXDECLARATION9 iface) {
53     IDirect3DVertexDeclaration9Impl *This = (IDirect3DVertexDeclaration9Impl *)iface;
54     ULONG ref = InterlockedDecrement(&This->ref);
55
56     TRACE("(%p) : ReleaseRef to %ld\n", This, ref);
57
58     if (ref == 0) {
59         IWineD3DVertexDeclaration_Release(This->wineD3DVertexDeclaration);
60         IUnknown_Release(This->parentDevice);
61         HeapFree(GetProcessHeap(), 0, This);
62     }
63     return ref;
64 }
65
66 /* IDirect3DVertexDeclaration9 Interface follow: */
67 static HRESULT WINAPI IDirect3DVertexDeclaration9Impl_GetDevice(LPDIRECT3DVERTEXDECLARATION9 iface, IDirect3DDevice9** ppDevice) {
68     IDirect3DVertexDeclaration9Impl *This = (IDirect3DVertexDeclaration9Impl *)iface;
69     IWineD3DDevice *myDevice = NULL;
70     HRESULT hr = D3D_OK;
71
72     TRACE("(%p) : Relay\n", iface);
73
74     hr = IWineD3DVertexDeclaration_GetDevice(This->wineD3DVertexDeclaration, &myDevice);
75     if (hr == D3D_OK && myDevice != NULL) {
76         hr = IWineD3DDevice_GetParent(myDevice, (IUnknown **)ppDevice);
77         IWineD3DDevice_Release(myDevice);
78     }
79     return hr;
80 }
81
82 static HRESULT WINAPI IDirect3DVertexDeclaration9Impl_GetDeclaration(LPDIRECT3DVERTEXDECLARATION9 iface, D3DVERTEXELEMENT9* pDecl, UINT* pNumElements) {
83     IDirect3DVertexDeclaration9Impl *This = (IDirect3DVertexDeclaration9Impl *)iface;
84     DWORD NumElements;
85     HRESULT hr;
86     TRACE("(%p) : Relay\n", iface);
87     hr = IWineD3DVertexDeclaration_GetDeclaration(This->wineD3DVertexDeclaration, pDecl, &NumElements);
88
89     *pNumElements = NumElements;
90     return hr;
91 }
92
93 static const IDirect3DVertexDeclaration9Vtbl Direct3DVertexDeclaration9_Vtbl =
94 {
95     /* IUnknown */
96     IDirect3DVertexDeclaration9Impl_QueryInterface,
97     IDirect3DVertexDeclaration9Impl_AddRef,
98     IDirect3DVertexDeclaration9Impl_Release,
99     /* IDirect3DVertexDeclaration9 */
100     IDirect3DVertexDeclaration9Impl_GetDevice,
101     IDirect3DVertexDeclaration9Impl_GetDeclaration
102 };
103
104
105 /* IDirect3DDevice9 IDirect3DVertexDeclaration9 Methods follow: */
106 HRESULT  WINAPI  IDirect3DDevice9Impl_CreateVertexDeclaration(LPDIRECT3DDEVICE9 iface, CONST D3DVERTEXELEMENT9* pVertexElements, IDirect3DVertexDeclaration9** ppDecl) {
107     
108     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
109     IDirect3DVertexDeclaration9Impl *object = NULL;
110     HRESULT hr = D3D_OK;
111
112     TRACE("(%p) : Relay\n", iface);
113     if (NULL == ppDecl) {
114         WARN("(%p) : Caller passed NULL As ppDecl, returning D3DERR_INVALIDCALL",This);
115         return D3DERR_INVALIDCALL;
116     }
117     /* Allocate the storage for the device */
118     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DVertexDeclaration9Impl));
119     if (NULL == object) {
120         FIXME("Allocation of memory failed, returning D3DERR_OUTOFVIDEOMEMORY\n");
121         return D3DERR_OUTOFVIDEOMEMORY;
122     }
123
124     object->lpVtbl = &Direct3DVertexDeclaration9_Vtbl;
125     object->ref = 1;
126     hr = IWineD3DDevice_CreateVertexDeclaration(This->WineD3DDevice, pVertexElements, &object->wineD3DVertexDeclaration, (IUnknown *)object);
127
128     if (FAILED(hr)) {
129
130         /* free up object */
131         FIXME("(%p) call to IWineD3DDevice_CreateVertexDeclaration failed\n", This);
132         HeapFree(GetProcessHeap(), 0, object);
133     } else {
134         IUnknown_AddRef(iface);
135         object->parentDevice = iface;
136         *ppDecl = (LPDIRECT3DVERTEXDECLARATION9) object;
137          TRACE("(%p) : Created vertex declatanio %p\n", This, object);
138     }
139     return hr;
140 }
141
142 HRESULT  WINAPI  IDirect3DDevice9Impl_SetVertexDeclaration(LPDIRECT3DDEVICE9 iface, IDirect3DVertexDeclaration9* pDecl) {
143     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
144     IDirect3DVertexDeclaration9Impl *pDeclImpl = (IDirect3DVertexDeclaration9Impl *)pDecl;
145     HRESULT hr = D3D_OK;
146
147     TRACE("(%p) : Relay\n", iface);
148
149     hr = IWineD3DDevice_SetVertexDeclaration(This->WineD3DDevice, pDeclImpl == NULL ? NULL : pDeclImpl->wineD3DVertexDeclaration);
150
151     return hr;
152 }
153
154 HRESULT  WINAPI  IDirect3DDevice9Impl_GetVertexDeclaration(LPDIRECT3DDEVICE9 iface, IDirect3DVertexDeclaration9** ppDecl) {
155     IDirect3DDevice9Impl* This = (IDirect3DDevice9Impl*) iface;
156     IWineD3DVertexDeclaration* pTest = NULL;
157     HRESULT hr = D3D_OK;
158
159     TRACE("(%p) : Relay+\n", iface);
160
161     if (NULL == ppDecl) {
162       return D3DERR_INVALIDCALL;
163     }
164
165     *ppDecl = NULL;
166     hr = IWineD3DDevice_GetVertexDeclaration(This->WineD3DDevice, &pTest);
167     if (hr == D3D_OK && NULL != pTest) {
168         IWineD3DResource_GetParent(pTest, (IUnknown **)ppDecl);
169         IWineD3DResource_Release(pTest);
170     } else {
171         *ppDecl = NULL;
172     }
173     TRACE("(%p) : returning %p\n", This, *ppDecl);
174     return hr;
175 }