d3d8: Hold the lock in IDirect3DVertexBuffer methods.
[wine] / dlls / wined3d / vertexdeclaration.c
1 /*
2  * vertex declaration implementation
3  *
4  * Copyright 2002-2005 Raphael Junqueira
5  * Copyright 2004 Jason Edmeades
6  * Copyright 2004 Christian Costa
7  * Copyright 2005 Oliver Stieber
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22  */
23
24 #include "config.h"
25 #include "wined3d_private.h"
26
27 WINE_DEFAULT_DEBUG_CHANNEL(d3d_decl);
28
29 /**
30  * DirectX9 SDK download
31  *  http://msdn.microsoft.com/library/default.asp?url=/downloads/list/directx.asp
32  *
33  * Exploring D3DX
34  *  http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndrive/html/directx07162002.asp
35  *
36  * Using Vertex Shaders
37  *  http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndrive/html/directx02192001.asp
38  *
39  * Dx9 New
40  *  http://msdn.microsoft.com/library/default.asp?url=/library/en-us/directx9_c/directx/graphics/whatsnew.asp
41  *
42  * Dx9 Shaders
43  *  http://msdn.microsoft.com/library/default.asp?url=/library/en-us/directx9_c/directx/graphics/reference/Shaders/VertexShader2_0/VertexShader2_0.asp
44  *  http://msdn.microsoft.com/library/default.asp?url=/library/en-us/directx9_c/directx/graphics/reference/Shaders/VertexShader2_0/Instructions/Instructions.asp
45  *  http://msdn.microsoft.com/library/default.asp?url=/library/en-us/directx9_c/directx/graphics/programmingguide/GettingStarted/VertexDeclaration/VertexDeclaration.asp
46  *  http://msdn.microsoft.com/library/default.asp?url=/library/en-us/directx9_c/directx/graphics/reference/Shaders/VertexShader3_0/VertexShader3_0.asp
47  *
48  * Dx9 D3DX
49  *  http://msdn.microsoft.com/library/default.asp?url=/library/en-us/directx9_c/directx/graphics/programmingguide/advancedtopics/VertexPipe/matrixstack/matrixstack.asp
50  *
51  * FVF
52  *  http://msdn.microsoft.com/library/default.asp?url=/library/en-us/directx9_c/directx/graphics/programmingguide/GettingStarted/VertexFormats/vformats.asp
53  *
54  * NVIDIA: DX8 Vertex Shader to NV Vertex Program
55  *  http://developer.nvidia.com/view.asp?IO=vstovp
56  *
57  * NVIDIA: Memory Management with VAR
58  *  http://developer.nvidia.com/view.asp?IO=var_memory_management
59  */
60
61 static void dump_wined3dvertexelement(const WINED3DVERTEXELEMENT *element) {
62     TRACE("     Stream: %d\n", element->Stream);
63     TRACE("     Offset: %d\n", element->Offset);
64     TRACE("       Type: %s (%#x)\n", debug_d3ddecltype(element->Type), element->Type);
65     TRACE("     Method: %s (%#x)\n", debug_d3ddeclmethod(element->Method), element->Method);
66     TRACE("      Usage: %s (%#x)\n", debug_d3ddeclusage(element->Usage), element->Usage);
67     TRACE("Usage index: %d\n", element->UsageIndex);
68     TRACE("   Register: %d\n", element->Reg);
69 }
70
71 /* *******************************************
72    IWineD3DVertexDeclaration IUnknown parts follow
73    ******************************************* */
74 static HRESULT WINAPI IWineD3DVertexDeclarationImpl_QueryInterface(IWineD3DVertexDeclaration *iface, REFIID riid, LPVOID *ppobj)
75 {
76     IWineD3DVertexDeclarationImpl *This = (IWineD3DVertexDeclarationImpl *)iface;
77     TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppobj);
78     if (IsEqualGUID(riid, &IID_IUnknown)
79         || IsEqualGUID(riid, &IID_IWineD3DBase)
80         || IsEqualGUID(riid, &IID_IWineD3DVertexDeclaration)){
81         IUnknown_AddRef(iface);
82         *ppobj = This;
83         return S_OK;
84     }
85     *ppobj = NULL;
86     return E_NOINTERFACE;
87 }
88
89 static ULONG WINAPI IWineD3DVertexDeclarationImpl_AddRef(IWineD3DVertexDeclaration *iface) {
90     IWineD3DVertexDeclarationImpl *This = (IWineD3DVertexDeclarationImpl *)iface;
91     TRACE("(%p) : AddRef increasing from %d\n", This, This->ref);
92     return InterlockedIncrement(&This->ref);
93 }
94
95 static ULONG WINAPI IWineD3DVertexDeclarationImpl_Release(IWineD3DVertexDeclaration *iface) {
96     IWineD3DVertexDeclarationImpl *This = (IWineD3DVertexDeclarationImpl *)iface;
97     ULONG ref;
98     TRACE("(%p) : Releasing from %d\n", This, This->ref);
99     ref = InterlockedDecrement(&This->ref);
100     if (ref == 0) {
101         if(iface == This->wineD3DDevice->stateBlock->vertexDecl) {
102             /* See comment in PixelShader::Release */
103             IWineD3DDeviceImpl_MarkStateDirty(This->wineD3DDevice, STATE_VDECL);
104         }
105
106         HeapFree(GetProcessHeap(), 0, This->pDeclarationWine);
107         HeapFree(GetProcessHeap(), 0, This);
108     }
109     return ref;
110 }
111
112 /* *******************************************
113    IWineD3DVertexDeclaration parts follow
114    ******************************************* */
115
116 static HRESULT WINAPI IWineD3DVertexDeclarationImpl_GetParent(IWineD3DVertexDeclaration *iface, IUnknown** parent){
117     IWineD3DVertexDeclarationImpl *This = (IWineD3DVertexDeclarationImpl *)iface;
118
119     *parent= This->parent;
120     IUnknown_AddRef(*parent);
121     TRACE("(%p) : returning %p\n", This, *parent);
122     return WINED3D_OK;
123 }
124
125 static HRESULT WINAPI IWineD3DVertexDeclarationImpl_GetDevice(IWineD3DVertexDeclaration *iface, IWineD3DDevice** ppDevice) {
126     IWineD3DVertexDeclarationImpl *This = (IWineD3DVertexDeclarationImpl *)iface;
127     TRACE("(%p) : returning %p\n", This, This->wineD3DDevice);
128
129     *ppDevice = (IWineD3DDevice *) This->wineD3DDevice;
130     IWineD3DDevice_AddRef(*ppDevice);
131
132     return WINED3D_OK;
133 }
134
135 static HRESULT WINAPI IWineD3DVertexDeclarationImpl_GetDeclaration(IWineD3DVertexDeclaration *iface,
136         WINED3DVERTEXELEMENT *elements, size_t *element_count) {
137     IWineD3DVertexDeclarationImpl *This = (IWineD3DVertexDeclarationImpl *)iface;
138     HRESULT hr = WINED3D_OK;
139
140     TRACE("(%p) : d3d version %d, elements %p, element_count %p\n",
141             This, ((IWineD3DImpl *)This->wineD3DDevice->wineD3D)->dxVersion, elements, element_count);
142
143     *element_count = This->declarationWNumElements;
144     if (elements) {
145         CopyMemory(elements, This->pDeclarationWine, This->declarationWNumElements * sizeof(WINED3DVERTEXELEMENT));
146     }
147
148     return hr;
149 }
150
151 static HRESULT WINAPI IWineD3DVertexDeclarationImpl_SetDeclaration(IWineD3DVertexDeclaration *iface,
152         const WINED3DVERTEXELEMENT *elements, size_t element_count) {
153     IWineD3DVertexDeclarationImpl *This = (IWineD3DVertexDeclarationImpl *)iface;
154     HRESULT hr = WINED3D_OK;
155
156     TRACE("(%p) : d3d version %d\n", This, ((IWineD3DImpl *)This->wineD3DDevice->wineD3D)->dxVersion);
157
158     if (TRACE_ON(d3d_decl)) {
159         int i;
160         for (i = 0; i < element_count; ++i) {
161             dump_wined3dvertexelement(elements+i);
162         }
163     }
164
165     This->declarationWNumElements = element_count;
166     This->pDeclarationWine = HeapAlloc(GetProcessHeap(), 0, sizeof(WINED3DVERTEXELEMENT) * element_count);
167     if (!This->pDeclarationWine) {
168         ERR("Memory allocation failed\n");
169         hr = WINED3DERR_OUTOFVIDEOMEMORY;
170     } else {
171         CopyMemory(This->pDeclarationWine, elements, sizeof(WINED3DVERTEXELEMENT) * element_count);
172     }
173
174     TRACE("Returning\n");
175     return hr;
176 }
177
178 const IWineD3DVertexDeclarationVtbl IWineD3DVertexDeclaration_Vtbl =
179 {
180     /* IUnknown */
181     IWineD3DVertexDeclarationImpl_QueryInterface,
182     IWineD3DVertexDeclarationImpl_AddRef,
183     IWineD3DVertexDeclarationImpl_Release,
184     /* IWineD3DVertexDeclaration */
185     IWineD3DVertexDeclarationImpl_GetParent,
186     IWineD3DVertexDeclarationImpl_GetDevice,
187     IWineD3DVertexDeclarationImpl_GetDeclaration,
188     IWineD3DVertexDeclarationImpl_SetDeclaration
189 };