advapi32: Implement EnumServicesStatusExA/W.
[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  * Copyright 2009 Henri Verbeet for CodeWeavers
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this library; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24
25 #include "config.h"
26 #include "wined3d_private.h"
27
28 WINE_DEFAULT_DEBUG_CHANNEL(d3d_decl);
29
30 static void dump_wined3dvertexelement(const WINED3DVERTEXELEMENT *element) {
31     TRACE("     format: %s (%#x)\n", debug_d3dformat(element->format), element->format);
32     TRACE(" input_slot: %u\n", element->input_slot);
33     TRACE("     offset: %u\n", element->offset);
34     TRACE("output_slot: %u\n", element->output_slot);
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_idx: %u\n", element->usage_idx);
38 }
39
40 /* *******************************************
41    IWineD3DVertexDeclaration IUnknown parts follow
42    ******************************************* */
43 static HRESULT WINAPI IWineD3DVertexDeclarationImpl_QueryInterface(IWineD3DVertexDeclaration *iface, REFIID riid, LPVOID *ppobj)
44 {
45     IWineD3DVertexDeclarationImpl *This = (IWineD3DVertexDeclarationImpl *)iface;
46     TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppobj);
47     if (IsEqualGUID(riid, &IID_IUnknown)
48         || IsEqualGUID(riid, &IID_IWineD3DBase)
49         || IsEqualGUID(riid, &IID_IWineD3DVertexDeclaration)){
50         IUnknown_AddRef(iface);
51         *ppobj = This;
52         return S_OK;
53     }
54     *ppobj = NULL;
55     return E_NOINTERFACE;
56 }
57
58 static ULONG WINAPI IWineD3DVertexDeclarationImpl_AddRef(IWineD3DVertexDeclaration *iface) {
59     IWineD3DVertexDeclarationImpl *This = (IWineD3DVertexDeclarationImpl *)iface;
60     TRACE("(%p) : AddRef increasing from %d\n", This, This->ref);
61     return InterlockedIncrement(&This->ref);
62 }
63
64 static ULONG WINAPI IWineD3DVertexDeclarationImpl_Release(IWineD3DVertexDeclaration *iface) {
65     IWineD3DVertexDeclarationImpl *This = (IWineD3DVertexDeclarationImpl *)iface;
66     ULONG ref;
67     TRACE("(%p) : Releasing from %d\n", This, This->ref);
68     ref = InterlockedDecrement(&This->ref);
69     if (!ref)
70     {
71         HeapFree(GetProcessHeap(), 0, This->elements);
72         This->parent_ops->wined3d_object_destroyed(This->parent);
73         HeapFree(GetProcessHeap(), 0, This);
74     }
75     return ref;
76 }
77
78 /* *******************************************
79    IWineD3DVertexDeclaration parts follow
80    ******************************************* */
81
82 static void * WINAPI IWineD3DVertexDeclarationImpl_GetParent(IWineD3DVertexDeclaration *iface)
83 {
84     TRACE("iface %p.\n", iface);
85
86     return ((IWineD3DVertexDeclarationImpl *)iface)->parent;
87 }
88
89 static BOOL declaration_element_valid_ffp(const WINED3DVERTEXELEMENT *element)
90 {
91     switch(element->usage)
92     {
93         case WINED3DDECLUSAGE_POSITION:
94         case WINED3DDECLUSAGE_POSITIONT:
95             switch(element->format)
96             {
97                 case WINED3DFMT_R32G32_FLOAT:
98                 case WINED3DFMT_R32G32B32_FLOAT:
99                 case WINED3DFMT_R32G32B32A32_FLOAT:
100                 case WINED3DFMT_R16G16_SINT:
101                 case WINED3DFMT_R16G16B16A16_SINT:
102                 case WINED3DFMT_R16G16_FLOAT:
103                 case WINED3DFMT_R16G16B16A16_FLOAT:
104                     return TRUE;
105                 default:
106                     return FALSE;
107             }
108
109         case WINED3DDECLUSAGE_BLENDWEIGHT:
110             switch(element->format)
111             {
112                 case WINED3DFMT_R32_FLOAT:
113                 case WINED3DFMT_R32G32_FLOAT:
114                 case WINED3DFMT_R32G32B32_FLOAT:
115                 case WINED3DFMT_R32G32B32A32_FLOAT:
116                 case WINED3DFMT_B8G8R8A8_UNORM:
117                 case WINED3DFMT_R8G8B8A8_UINT:
118                 case WINED3DFMT_R16G16_SINT:
119                 case WINED3DFMT_R16G16B16A16_SINT:
120                 case WINED3DFMT_R16G16_FLOAT:
121                 case WINED3DFMT_R16G16B16A16_FLOAT:
122                     return TRUE;
123                 default:
124                     return FALSE;
125             }
126
127         case WINED3DDECLUSAGE_NORMAL:
128             switch(element->format)
129             {
130                 case WINED3DFMT_R32G32B32_FLOAT:
131                 case WINED3DFMT_R32G32B32A32_FLOAT:
132                 case WINED3DFMT_R16G16B16A16_SINT:
133                 case WINED3DFMT_R16G16B16A16_FLOAT:
134                     return TRUE;
135                 default:
136                     return FALSE;
137             }
138
139         case WINED3DDECLUSAGE_TEXCOORD:
140             switch(element->format)
141             {
142                 case WINED3DFMT_R32_FLOAT:
143                 case WINED3DFMT_R32G32_FLOAT:
144                 case WINED3DFMT_R32G32B32_FLOAT:
145                 case WINED3DFMT_R32G32B32A32_FLOAT:
146                 case WINED3DFMT_R16G16_SINT:
147                 case WINED3DFMT_R16G16B16A16_SINT:
148                 case WINED3DFMT_R16G16_FLOAT:
149                 case WINED3DFMT_R16G16B16A16_FLOAT:
150                     return TRUE;
151                 default:
152                     return FALSE;
153             }
154
155         case WINED3DDECLUSAGE_COLOR:
156             switch(element->format)
157             {
158                 case WINED3DFMT_R32G32B32_FLOAT:
159                 case WINED3DFMT_R32G32B32A32_FLOAT:
160                 case WINED3DFMT_B8G8R8A8_UNORM:
161                 case WINED3DFMT_R8G8B8A8_UINT:
162                 case WINED3DFMT_R16G16B16A16_SINT:
163                 case WINED3DFMT_R8G8B8A8_UNORM:
164                 case WINED3DFMT_R16G16B16A16_SNORM:
165                 case WINED3DFMT_R16G16B16A16_UNORM:
166                 case WINED3DFMT_R16G16B16A16_FLOAT:
167                     return TRUE;
168                 default:
169                     return FALSE;
170             }
171
172         default:
173             return FALSE;
174     }
175 }
176
177 static const IWineD3DVertexDeclarationVtbl IWineD3DVertexDeclaration_Vtbl =
178 {
179     /* IUnknown */
180     IWineD3DVertexDeclarationImpl_QueryInterface,
181     IWineD3DVertexDeclarationImpl_AddRef,
182     IWineD3DVertexDeclarationImpl_Release,
183     /* IWineD3DVertexDeclaration */
184     IWineD3DVertexDeclarationImpl_GetParent,
185 };
186
187 HRESULT vertexdeclaration_init(IWineD3DVertexDeclarationImpl *declaration, IWineD3DDeviceImpl *device,
188         const WINED3DVERTEXELEMENT *elements, UINT element_count,
189         void *parent, const struct wined3d_parent_ops *parent_ops)
190 {
191     const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
192     WORD preloaded = 0; /* MAX_STREAMS, 16 */
193     unsigned int i;
194
195     if (TRACE_ON(d3d_decl))
196     {
197         for (i = 0; i < element_count; ++i)
198         {
199             dump_wined3dvertexelement(elements + i);
200         }
201     }
202
203     declaration->lpVtbl = &IWineD3DVertexDeclaration_Vtbl;
204     declaration->ref = 1;
205     declaration->parent = parent;
206     declaration->parent_ops = parent_ops;
207     declaration->device = device;
208     declaration->elements = HeapAlloc(GetProcessHeap(), 0, sizeof(*declaration->elements) * element_count);
209     if (!declaration->elements)
210     {
211         ERR("Failed to allocate elements memory.\n");
212         return E_OUTOFMEMORY;
213     }
214     declaration->element_count = element_count;
215
216     /* Do some static analysis on the elements to make reading the
217      * declaration more comfortable for the drawing code. */
218     for (i = 0; i < element_count; ++i)
219     {
220         struct wined3d_vertex_declaration_element *e = &declaration->elements[i];
221
222         e->format = wined3d_get_format(gl_info, elements[i].format);
223         e->ffp_valid = declaration_element_valid_ffp(&elements[i]);
224         e->input_slot = elements[i].input_slot;
225         e->offset = elements[i].offset;
226         e->output_slot = elements[i].output_slot;
227         e->method = elements[i].method;
228         e->usage = elements[i].usage;
229         e->usage_idx = elements[i].usage_idx;
230
231         if (e->usage == WINED3DDECLUSAGE_POSITIONT) declaration->position_transformed = TRUE;
232
233         /* Find the streams used in the declaration. The vertex buffers have
234          * to be loaded when drawing, but filter tesselation pseudo streams. */
235         if (e->input_slot >= MAX_STREAMS) continue;
236
237         if (!e->format->gl_vtx_format)
238         {
239             FIXME("The application tries to use an unsupported format (%s), returning E_FAIL.\n",
240                     debug_d3dformat(elements[i].format));
241             HeapFree(GetProcessHeap(), 0, declaration->elements);
242             return E_FAIL;
243         }
244
245         if (e->offset & 0x3)
246         {
247             WARN("Declaration element %u is not 4 byte aligned(%u), returning E_FAIL.\n", i, e->offset);
248             HeapFree(GetProcessHeap(), 0, declaration->elements);
249             return E_FAIL;
250         }
251
252         if (!(preloaded & (1 << e->input_slot)))
253         {
254             declaration->streams[declaration->num_streams] = e->input_slot;
255             ++declaration->num_streams;
256             preloaded |= 1 << e->input_slot;
257         }
258
259         if (elements[i].format == WINED3DFMT_R16G16_FLOAT || elements[i].format == WINED3DFMT_R16G16B16A16_FLOAT)
260         {
261             if (!gl_info->supported[ARB_HALF_FLOAT_VERTEX]) declaration->half_float_conv_needed = TRUE;
262         }
263     }
264
265     return WINED3D_OK;
266 }