crypt32/tests: Fix some test failures on Win9x.
[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 #define GLINFO_LOCATION This->wineD3DDevice->adapter->gl_info
30
31 static void dump_wined3dvertexelement(const WINED3DVERTEXELEMENT *element) {
32     TRACE("     Stream: %d\n", element->Stream);
33     TRACE("     Offset: %d\n", element->Offset);
34     TRACE("       Type: %s (%#x)\n", debug_d3ddecltype(element->Type), element->Type);
35     TRACE("     Method: %s (%#x)\n", debug_d3ddeclmethod(element->Method), element->Method);
36     TRACE("      Usage: %s (%#x)\n", debug_d3ddeclusage(element->Usage), element->Usage);
37     TRACE("Usage index: %d\n", element->UsageIndex);
38     TRACE("   Register: %d\n", element->Reg);
39 }
40
41 /* *******************************************
42    IWineD3DVertexDeclaration IUnknown parts follow
43    ******************************************* */
44 static HRESULT WINAPI IWineD3DVertexDeclarationImpl_QueryInterface(IWineD3DVertexDeclaration *iface, REFIID riid, LPVOID *ppobj)
45 {
46     IWineD3DVertexDeclarationImpl *This = (IWineD3DVertexDeclarationImpl *)iface;
47     TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppobj);
48     if (IsEqualGUID(riid, &IID_IUnknown)
49         || IsEqualGUID(riid, &IID_IWineD3DBase)
50         || IsEqualGUID(riid, &IID_IWineD3DVertexDeclaration)){
51         IUnknown_AddRef(iface);
52         *ppobj = This;
53         return S_OK;
54     }
55     *ppobj = NULL;
56     return E_NOINTERFACE;
57 }
58
59 static ULONG WINAPI IWineD3DVertexDeclarationImpl_AddRef(IWineD3DVertexDeclaration *iface) {
60     IWineD3DVertexDeclarationImpl *This = (IWineD3DVertexDeclarationImpl *)iface;
61     TRACE("(%p) : AddRef increasing from %d\n", This, This->ref);
62     return InterlockedIncrement(&This->ref);
63 }
64
65 static ULONG WINAPI IWineD3DVertexDeclarationImpl_Release(IWineD3DVertexDeclaration *iface) {
66     IWineD3DVertexDeclarationImpl *This = (IWineD3DVertexDeclarationImpl *)iface;
67     ULONG ref;
68     TRACE("(%p) : Releasing from %d\n", This, This->ref);
69     ref = InterlockedDecrement(&This->ref);
70     if (ref == 0) {
71         if(iface == This->wineD3DDevice->stateBlock->vertexDecl) {
72             /* See comment in PixelShader::Release */
73             IWineD3DDeviceImpl_MarkStateDirty(This->wineD3DDevice, STATE_VDECL);
74         }
75
76         HeapFree(GetProcessHeap(), 0, This->pDeclarationWine);
77         HeapFree(GetProcessHeap(), 0, This->ffp_valid);
78         HeapFree(GetProcessHeap(), 0, This);
79     }
80     return ref;
81 }
82
83 /* *******************************************
84    IWineD3DVertexDeclaration parts follow
85    ******************************************* */
86
87 static HRESULT WINAPI IWineD3DVertexDeclarationImpl_GetParent(IWineD3DVertexDeclaration *iface, IUnknown** parent){
88     IWineD3DVertexDeclarationImpl *This = (IWineD3DVertexDeclarationImpl *)iface;
89
90     *parent= This->parent;
91     IUnknown_AddRef(*parent);
92     TRACE("(%p) : returning %p\n", This, *parent);
93     return WINED3D_OK;
94 }
95
96 static HRESULT WINAPI IWineD3DVertexDeclarationImpl_GetDevice(IWineD3DVertexDeclaration *iface, IWineD3DDevice** ppDevice) {
97     IWineD3DVertexDeclarationImpl *This = (IWineD3DVertexDeclarationImpl *)iface;
98     TRACE("(%p) : returning %p\n", This, This->wineD3DDevice);
99
100     *ppDevice = (IWineD3DDevice *) This->wineD3DDevice;
101     IWineD3DDevice_AddRef(*ppDevice);
102
103     return WINED3D_OK;
104 }
105
106 static HRESULT WINAPI IWineD3DVertexDeclarationImpl_GetDeclaration(IWineD3DVertexDeclaration *iface,
107         WINED3DVERTEXELEMENT *elements, UINT *element_count) {
108     IWineD3DVertexDeclarationImpl *This = (IWineD3DVertexDeclarationImpl *)iface;
109     HRESULT hr = WINED3D_OK;
110
111     TRACE("(%p) : d3d version %d, elements %p, element_count %p\n",
112             This, ((IWineD3DImpl *)This->wineD3DDevice->wineD3D)->dxVersion, elements, element_count);
113
114     *element_count = This->declarationWNumElements;
115     if (elements) {
116         CopyMemory(elements, This->pDeclarationWine, This->declarationWNumElements * sizeof(WINED3DVERTEXELEMENT));
117     }
118
119     return hr;
120 }
121
122 static BOOL declaration_element_valid_ffp(const WINED3DVERTEXELEMENT *element)
123 {
124     switch(element->Usage)
125     {
126         case WINED3DDECLUSAGE_POSITION:
127         case WINED3DDECLUSAGE_POSITIONT:
128             switch(element->Type)
129             {
130                 case WINED3DDECLTYPE_FLOAT2:
131                 case WINED3DDECLTYPE_FLOAT3:
132                 case WINED3DDECLTYPE_FLOAT4:
133                 case WINED3DDECLTYPE_SHORT2:
134                 case WINED3DDECLTYPE_SHORT4:
135                 case WINED3DDECLTYPE_FLOAT16_2:
136                 case WINED3DDECLTYPE_FLOAT16_4:
137                     return TRUE;
138                 default:
139                     return FALSE;
140             }
141
142         case WINED3DDECLUSAGE_BLENDWEIGHT:
143             switch(element->Type)
144             {
145                 case WINED3DDECLTYPE_FLOAT1:
146                 case WINED3DDECLTYPE_FLOAT2:
147                 case WINED3DDECLTYPE_FLOAT3:
148                 case WINED3DDECLTYPE_FLOAT4:
149                 case WINED3DDECLTYPE_D3DCOLOR:
150                 case WINED3DDECLTYPE_UBYTE4:
151                 case WINED3DDECLTYPE_SHORT2:
152                 case WINED3DDECLTYPE_SHORT4:
153                 case WINED3DDECLTYPE_FLOAT16_2:
154                 case WINED3DDECLTYPE_FLOAT16_4:
155                     return TRUE;
156                 default:
157                     return FALSE;
158             }
159
160         case WINED3DDECLUSAGE_NORMAL:
161             switch(element->Type)
162             {
163                 case WINED3DDECLTYPE_FLOAT3:
164                 case WINED3DDECLTYPE_FLOAT4:
165                 case WINED3DDECLTYPE_SHORT4:
166                 case WINED3DDECLTYPE_FLOAT16_4:
167                     return TRUE;
168                 default:
169                     return FALSE;
170             }
171
172         case WINED3DDECLUSAGE_TEXCOORD:
173             switch(element->Type)
174             {
175                 case WINED3DDECLTYPE_FLOAT1:
176                 case WINED3DDECLTYPE_FLOAT2:
177                 case WINED3DDECLTYPE_FLOAT3:
178                 case WINED3DDECLTYPE_FLOAT4:
179                 case WINED3DDECLTYPE_SHORT2:
180                 case WINED3DDECLTYPE_SHORT4:
181                 case WINED3DDECLTYPE_FLOAT16_2:
182                 case WINED3DDECLTYPE_FLOAT16_4:
183                     return TRUE;
184                 default:
185                     return FALSE;
186             }
187
188         case WINED3DDECLUSAGE_COLOR:
189             switch(element->Type)
190             {
191                 case WINED3DDECLTYPE_FLOAT3:
192                 case WINED3DDECLTYPE_FLOAT4:
193                 case WINED3DDECLTYPE_D3DCOLOR:
194                 case WINED3DDECLTYPE_UBYTE4:
195                 case WINED3DDECLTYPE_SHORT4:
196                 case WINED3DDECLTYPE_UBYTE4N:
197                 case WINED3DDECLTYPE_SHORT4N:
198                 case WINED3DDECLTYPE_USHORT4N:
199                 case WINED3DDECLTYPE_FLOAT16_4:
200                     return TRUE;
201                 default:
202                     return FALSE;
203             }
204
205         default:
206             return FALSE;
207     }
208 }
209
210 static HRESULT WINAPI IWineD3DVertexDeclarationImpl_SetDeclaration(IWineD3DVertexDeclaration *iface,
211         const WINED3DVERTEXELEMENT *elements, UINT element_count) {
212     IWineD3DVertexDeclarationImpl *This = (IWineD3DVertexDeclarationImpl *)iface;
213     HRESULT hr = WINED3D_OK;
214     int i;
215     char isPreLoaded[MAX_STREAMS];
216
217     TRACE("(%p) : d3d version %d\n", This, ((IWineD3DImpl *)This->wineD3DDevice->wineD3D)->dxVersion);
218     memset(isPreLoaded, 0, sizeof(isPreLoaded));
219
220     if (TRACE_ON(d3d_decl)) {
221         for (i = 0; i < element_count; ++i) {
222             dump_wined3dvertexelement(elements+i);
223         }
224     }
225
226     This->declarationWNumElements = element_count;
227     This->pDeclarationWine = HeapAlloc(GetProcessHeap(), 0, sizeof(WINED3DVERTEXELEMENT) * element_count);
228     This->ffp_valid = HeapAlloc(GetProcessHeap(), 0, sizeof(*This->ffp_valid) * element_count);
229     if (!This->pDeclarationWine || !This->ffp_valid) {
230         ERR("Memory allocation failed\n");
231         return WINED3DERR_OUTOFVIDEOMEMORY;
232     } else {
233         CopyMemory(This->pDeclarationWine, elements, sizeof(WINED3DVERTEXELEMENT) * element_count);
234     }
235
236     /* Do some static analysis on the elements to make reading the declaration more comfortable
237      * for the drawing code
238      */
239     This->num_streams = 0;
240     This->position_transformed = FALSE;
241     for (i = 0; i < element_count; ++i) {
242         This->ffp_valid[i] = declaration_element_valid_ffp(&This->pDeclarationWine[i]);
243
244         if(This->pDeclarationWine[i].Usage == WINED3DDECLUSAGE_POSITIONT) {
245             This->position_transformed = TRUE;
246         }
247
248         /* Find the Streams used in the declaration. The vertex buffers have to be loaded
249          * when drawing, but filter tesselation pseudo streams
250          */
251         if(This->pDeclarationWine[i].Stream >= MAX_STREAMS) continue;
252
253         if(This->pDeclarationWine[i].Type == WINED3DDECLTYPE_UNUSED) {
254             WARN("The application tries to use WINED3DDECLTYPE_UNUSED, returning E_FAIL\n");
255             /* The caller will release the vdecl, which will free This->pDeclarationWine */
256             return E_FAIL;
257         }
258
259         if(This->pDeclarationWine[i].Offset & 0x3) {
260             WARN("Declaration element %d is not 4 byte aligned(%d), returning E_FAIL\n", i, This->pDeclarationWine[i].Offset);
261             return E_FAIL;
262         }
263
264         if(!isPreLoaded[This->pDeclarationWine[i].Stream]) {
265             This->streams[This->num_streams] = This->pDeclarationWine[i].Stream;
266             This->num_streams++;
267             isPreLoaded[This->pDeclarationWine[i].Stream] = 1;
268         }
269
270         if (This->pDeclarationWine[i].Type == WINED3DDECLTYPE_FLOAT16_2
271                 || This->pDeclarationWine[i].Type == WINED3DDECLTYPE_FLOAT16_4)
272         {
273             if (!GL_SUPPORT(NV_HALF_FLOAT)) This->half_float_conv_needed = TRUE;
274         }
275     }
276
277     TRACE("Returning\n");
278     return hr;
279 }
280
281 const IWineD3DVertexDeclarationVtbl IWineD3DVertexDeclaration_Vtbl =
282 {
283     /* IUnknown */
284     IWineD3DVertexDeclarationImpl_QueryInterface,
285     IWineD3DVertexDeclarationImpl_AddRef,
286     IWineD3DVertexDeclarationImpl_Release,
287     /* IWineD3DVertexDeclaration */
288     IWineD3DVertexDeclarationImpl_GetParent,
289     IWineD3DVertexDeclarationImpl_GetDevice,
290     IWineD3DVertexDeclarationImpl_GetDeclaration,
291     IWineD3DVertexDeclarationImpl_SetDeclaration
292 };