d3dx9: Don't return a pointer to the implementation in ID3DXConstantTableImpl_QueryIn...
[wine] / dlls / d3dx9_36 / skin.c
1 /*
2  * Skin Info operations specific to D3DX9.
3  *
4  * Copyright (C) 2011 Dylan Smith
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include "wine/debug.h"
22 #include "d3dx9_36_private.h"
23
24 WINE_DEFAULT_DEBUG_CHANNEL(d3dx);
25
26 struct bone
27 {
28     char *name;
29     D3DXMATRIX transform;
30     DWORD num_influences;
31     DWORD *vertices;
32     FLOAT *weights;
33 };
34
35 typedef struct ID3DXSkinInfoImpl
36 {
37     ID3DXSkinInfo ID3DXSkinInfo_iface;
38     LONG ref;
39
40     DWORD fvf;
41     D3DVERTEXELEMENT9 vertex_declaration[MAX_FVF_DECL_SIZE];
42     DWORD num_vertices;
43     DWORD num_bones;
44     struct bone *bones;
45 } ID3DXSkinInfoImpl;
46
47 static inline struct ID3DXSkinInfoImpl *impl_from_ID3DXSkinInfo(ID3DXSkinInfo *iface)
48 {
49     return CONTAINING_RECORD(iface, struct ID3DXSkinInfoImpl, ID3DXSkinInfo_iface);
50 }
51
52 static HRESULT WINAPI ID3DXSkinInfoImpl_QueryInterface(ID3DXSkinInfo *iface, REFIID riid, void **ppobj)
53 {
54     TRACE("(%p, %s, %p)\n", iface, debugstr_guid(riid), ppobj);
55
56     if (IsEqualGUID(riid, &IID_IUnknown) || IsEqualGUID(riid, &IID_ID3DXSkinInfo))
57     {
58         IUnknown_AddRef(iface);
59         *ppobj = iface;
60         return D3D_OK;
61     }
62
63     WARN("%s not implemented, returning E_NOINTERFACE\n", debugstr_guid(riid));
64
65     return E_NOINTERFACE;
66 }
67
68 static ULONG WINAPI ID3DXSkinInfoImpl_AddRef(ID3DXSkinInfo *iface)
69 {
70     struct ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
71     ULONG ref = InterlockedIncrement(&This->ref);
72
73     TRACE("%p increasing refcount to %u\n", This, ref);
74
75     return ref;
76 }
77
78 static ULONG WINAPI ID3DXSkinInfoImpl_Release(ID3DXSkinInfo *iface)
79 {
80     struct ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
81     ULONG ref = InterlockedDecrement(&This->ref);
82
83     TRACE("%p decreasing refcount to %u\n", This, ref);
84
85     if (ref == 0) {
86         int i;
87         for (i = 0; i < This->num_bones; i++) {
88             HeapFree(GetProcessHeap(), 0, This->bones[i].name);
89             HeapFree(GetProcessHeap(), 0, This->bones[i].vertices);
90             HeapFree(GetProcessHeap(), 0, This->bones[i].weights);
91         }
92         HeapFree(GetProcessHeap(), 0, This->bones);
93         HeapFree(GetProcessHeap(), 0, This);
94     }
95
96     return ref;
97 }
98
99 static HRESULT WINAPI ID3DXSkinInfoImpl_SetBoneInfluence(ID3DXSkinInfo *iface, DWORD bone_num, DWORD num_influences, CONST DWORD *vertices, CONST FLOAT *weights)
100 {
101     ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
102     struct bone *bone;
103     DWORD *new_vertices = NULL;
104     FLOAT *new_weights = NULL;
105
106     TRACE("(%p, %u, %u, %p, %p)\n", This, bone_num, num_influences, vertices, weights);
107
108     if (bone_num >= This->num_bones || !vertices || !weights)
109         return D3DERR_INVALIDCALL;
110
111     if (num_influences) {
112         new_vertices = HeapAlloc(GetProcessHeap(), 0, num_influences * sizeof(*vertices));
113         if (!new_vertices)
114             return E_OUTOFMEMORY;
115         new_weights = HeapAlloc(GetProcessHeap(), 0, num_influences * sizeof(*weights));
116         if (!new_weights) {
117             HeapFree(GetProcessHeap(), 0, new_vertices);
118             return E_OUTOFMEMORY;
119         }
120         memcpy(new_vertices, vertices, num_influences * sizeof(*vertices));
121         memcpy(new_weights, weights, num_influences * sizeof(*weights));
122     }
123     bone = &This->bones[bone_num];
124     bone->num_influences = num_influences;
125     HeapFree(GetProcessHeap(), 0, bone->vertices);
126     HeapFree(GetProcessHeap(), 0, bone->weights);
127     bone->vertices = new_vertices;
128     bone->weights = new_weights;
129
130     return D3D_OK;
131 }
132
133 static HRESULT WINAPI ID3DXSkinInfoImpl_SetBoneVertexInfluence(ID3DXSkinInfo *iface, DWORD bone_num, DWORD influence_num, float weight)
134 {
135     ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
136
137     FIXME("(%p, %u, %u, %g): stub\n", This, bone_num, influence_num, weight);
138
139     return E_NOTIMPL;
140 }
141
142 static DWORD WINAPI ID3DXSkinInfoImpl_GetNumBoneInfluences(ID3DXSkinInfo *iface, DWORD bone_num)
143 {
144     ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
145
146     TRACE("(%p, %u)\n", This, bone_num);
147
148     if (bone_num >= This->num_bones)
149         return 0;
150
151     return This->bones[bone_num].num_influences;
152 }
153
154 static HRESULT WINAPI ID3DXSkinInfoImpl_GetBoneInfluence(ID3DXSkinInfo *iface, DWORD bone_num, DWORD *vertices, FLOAT *weights)
155 {
156     ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
157     struct bone *bone;
158
159     TRACE("(%p, %u, %p, %p)\n", This, bone_num, vertices, weights);
160
161     if (bone_num >= This->num_bones || !vertices)
162         return D3DERR_INVALIDCALL;
163
164     bone = &This->bones[bone_num];
165     if (!bone->num_influences)
166         return D3D_OK;
167
168     memcpy(vertices, bone->vertices, bone->num_influences * sizeof(*vertices));
169     if (weights)
170         memcpy(weights, bone->weights, bone->num_influences * sizeof(*weights));
171
172     return D3D_OK;
173 }
174
175 static HRESULT WINAPI ID3DXSkinInfoImpl_GetBoneVertexInfluence(ID3DXSkinInfo *iface, DWORD bone_num,
176         DWORD influence_num, float *weight, DWORD *vertex_num)
177 {
178     ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
179
180     FIXME("(%p, %u, %u, %p, %p): stub\n", This, bone_num, influence_num, weight, vertex_num);
181
182     return E_NOTIMPL;
183 }
184
185 static HRESULT WINAPI ID3DXSkinInfoImpl_GetMaxVertexInfluences(ID3DXSkinInfo *iface, DWORD *max_vertex_influences)
186 {
187     ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
188
189     FIXME("(%p, %p): stub\n", This, max_vertex_influences);
190
191     return E_NOTIMPL;
192 }
193
194 static DWORD WINAPI ID3DXSkinInfoImpl_GetNumBones(ID3DXSkinInfo *iface)
195 {
196     ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
197
198     TRACE("(%p)\n", This);
199
200     return This->num_bones;
201 }
202
203 static HRESULT WINAPI ID3DXSkinInfoImpl_FindBoneVertexInfluenceIndex(ID3DXSkinInfo *iface, DWORD bone_num,
204         DWORD vertex_num, DWORD *influence_index)
205 {
206     ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
207
208     FIXME("(%p, %u, %u, %p): stub\n", This, bone_num, vertex_num, influence_index);
209
210     return E_NOTIMPL;
211 }
212
213 static HRESULT WINAPI ID3DXSkinInfoImpl_GetMaxFaceInfluences(ID3DXSkinInfo *iface, LPDIRECT3DINDEXBUFFER9 index_buffer, DWORD num_faces, DWORD *max_face_influences)
214 {
215     ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
216
217     FIXME("(%p, %p, %u, %p): stub\n", This, index_buffer, num_faces, max_face_influences);
218
219     return E_NOTIMPL;
220 }
221
222 static HRESULT WINAPI ID3DXSkinInfoImpl_SetMinBoneInfluence(ID3DXSkinInfo *iface, FLOAT min_influence)
223 {
224     ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
225
226     FIXME("(%p, %g): stub\n", This, min_influence);
227
228     return E_NOTIMPL;
229 }
230
231 static FLOAT WINAPI ID3DXSkinInfoImpl_GetMinBoneInfluence(ID3DXSkinInfo *iface)
232 {
233     ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
234
235     FIXME("(%p): stub\n", This);
236
237     return 0.0f;
238 }
239
240 static HRESULT WINAPI ID3DXSkinInfoImpl_SetBoneName(ID3DXSkinInfo *iface, DWORD bone_num, LPCSTR name)
241 {
242     ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
243     char *new_name;
244     size_t size;
245
246     TRACE("(%p, %u, %s)\n", This, bone_num, debugstr_a(name));
247
248     if (bone_num >= This->num_bones || !name)
249         return D3DERR_INVALIDCALL;
250
251     size = strlen(name) + 1;
252     new_name = HeapAlloc(GetProcessHeap(), 0, size);
253     if (!new_name)
254         return E_OUTOFMEMORY;
255     memcpy(new_name, name, size);
256     HeapFree(GetProcessHeap(), 0, This->bones[bone_num].name);
257     This->bones[bone_num].name = new_name;
258
259     return D3D_OK;
260 }
261
262 static LPCSTR WINAPI ID3DXSkinInfoImpl_GetBoneName(ID3DXSkinInfo *iface, DWORD bone_num)
263 {
264     ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
265
266     TRACE("(%p, %u)\n", This, bone_num);
267
268     if (bone_num >= This->num_bones)
269         return NULL;
270
271     return This->bones[bone_num].name;
272 }
273
274 static HRESULT WINAPI ID3DXSkinInfoImpl_SetBoneOffsetMatrix(ID3DXSkinInfo *iface, DWORD bone_num, CONST D3DXMATRIX *bone_transform)
275 {
276     ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
277
278     TRACE("(%p, %u, %p)\n", This, bone_num, bone_transform);
279
280     if (bone_num >= This->num_bones || !bone_transform)
281         return D3DERR_INVALIDCALL;
282
283     This->bones[bone_num].transform = *bone_transform;
284     return D3D_OK;
285 }
286
287 static LPD3DXMATRIX WINAPI ID3DXSkinInfoImpl_GetBoneOffsetMatrix(ID3DXSkinInfo *iface, DWORD bone_num)
288 {
289     ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
290
291     TRACE("(%p, %u)\n", This, bone_num);
292
293     if (bone_num >= This->num_bones)
294         return NULL;
295
296     return &This->bones[bone_num].transform;
297 }
298
299 static HRESULT WINAPI ID3DXSkinInfoImpl_Clone(ID3DXSkinInfo *iface, LPD3DXSKININFO *skin_info)
300 {
301     ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
302
303     FIXME("(%p, %p): stub\n", This, skin_info);
304
305     return E_NOTIMPL;
306 }
307
308 static HRESULT WINAPI ID3DXSkinInfoImpl_Remap(ID3DXSkinInfo *iface, DWORD num_vertices, DWORD *vertex_remap)
309 {
310     ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
311
312     FIXME("(%p, %u, %p): stub\n", This, num_vertices, vertex_remap);
313
314     return E_NOTIMPL;
315 }
316
317 static HRESULT WINAPI ID3DXSkinInfoImpl_SetFVF(ID3DXSkinInfo *iface, DWORD fvf)
318 {
319     ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
320     HRESULT hr;
321     D3DVERTEXELEMENT9 declaration[MAX_FVF_DECL_SIZE];
322
323     TRACE("(%p, %x)\n", This, fvf);
324
325     hr = D3DXDeclaratorFromFVF(fvf, declaration);
326     if (FAILED(hr)) return hr;
327
328     return iface->lpVtbl->SetDeclaration(iface, declaration);
329 }
330
331 static HRESULT WINAPI ID3DXSkinInfoImpl_SetDeclaration(ID3DXSkinInfo *iface, CONST D3DVERTEXELEMENT9 *declaration)
332 {
333     ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
334     HRESULT hr;
335     int count;
336
337     TRACE("(%p, %p)\n", This, declaration);
338
339     if (!declaration)
340         return D3DERR_INVALIDCALL;
341     for (count = 0; declaration[count].Stream != 0xff; count++) {
342         if (declaration[count].Stream != 0) {
343             WARN("Invalid vertex element %u; contains non-zero stream %u\n",
344                  count, declaration[count].Stream);
345             return D3DERR_INVALIDCALL;
346         }
347     }
348     count++;
349
350     memcpy(This->vertex_declaration, declaration, count * sizeof(*declaration));
351
352     hr = D3DXFVFFromDeclarator(This->vertex_declaration, &This->fvf);
353     if (FAILED(hr))
354         This->fvf = 0;
355
356     return D3D_OK;
357 }
358
359 static DWORD WINAPI ID3DXSkinInfoImpl_GetFVF(ID3DXSkinInfo *iface)
360 {
361     ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
362
363     TRACE("(%p)\n", This);
364
365     return This->fvf;
366 }
367
368 static HRESULT WINAPI ID3DXSkinInfoImpl_GetDeclaration(ID3DXSkinInfo *iface, D3DVERTEXELEMENT9 declaration[MAX_FVF_DECL_SIZE])
369 {
370     ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
371     UINT count = 0;
372
373     TRACE("(%p)\n", This);
374
375     while (This->vertex_declaration[count++].Stream != 0xff);
376     memcpy(declaration, This->vertex_declaration, count * sizeof(declaration[0]));
377     return D3D_OK;
378 }
379
380 static HRESULT WINAPI ID3DXSkinInfoImpl_UpdateSkinnedMesh(ID3DXSkinInfo *iface, CONST D3DXMATRIX *bone_transforms,
381         CONST D3DXMATRIX *bone_inv_transpose_transforms, LPCVOID vertices_src, PVOID vertices_dest)
382 {
383     ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
384
385     FIXME("(%p, %p, %p, %p, %p): stub\n", This, bone_transforms, bone_inv_transpose_transforms, vertices_src, vertices_dest);
386
387     return E_NOTIMPL;
388 }
389
390 static HRESULT WINAPI ID3DXSkinInfoImpl_ConvertToBlendedMesh(ID3DXSkinInfo *iface, LPD3DXMESH mesh_in,
391         DWORD options, CONST DWORD *adjacency_in, LPDWORD adjacency_out, DWORD *face_remap,
392         LPD3DXBUFFER *vertex_remap, DWORD *max_face_infl, DWORD *num_bone_combinations,
393         LPD3DXBUFFER *bone_combination_table, LPD3DXMESH *mesh_out)
394 {
395     ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
396
397     FIXME("(%p, %p, %x, %p, %p, %p, %p, %p, %p, %p, %p): stub\n",
398           This, mesh_in, options, adjacency_in, adjacency_out, face_remap, vertex_remap,
399           max_face_infl, num_bone_combinations, bone_combination_table, mesh_out);
400
401     return E_NOTIMPL;
402 }
403
404 static HRESULT WINAPI ID3DXSkinInfoImpl_ConvertToIndexedBlendedMesh(ID3DXSkinInfo *iface, LPD3DXMESH mesh_in,
405         DWORD options, CONST DWORD *adjacency_in, LPDWORD adjacency_out, DWORD *face_remap,
406         LPD3DXBUFFER *vertex_remap, DWORD *max_face_infl, DWORD *num_bone_combinations,
407         LPD3DXBUFFER *bone_combination_table, LPD3DXMESH *mesh_out)
408 {
409     ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
410
411     FIXME("(%p, %p, %x, %p, %p, %p, %p, %p, %p, %p, %p): stub\n",
412           This, mesh_in, options, adjacency_in, adjacency_out, face_remap, vertex_remap,
413           max_face_infl, num_bone_combinations, bone_combination_table, mesh_out);
414
415     return E_NOTIMPL;
416 }
417
418 static const struct ID3DXSkinInfoVtbl ID3DXSkinInfoImpl_Vtbl =
419 {
420     /* IUnknown methods */
421     ID3DXSkinInfoImpl_QueryInterface,
422     ID3DXSkinInfoImpl_AddRef,
423     ID3DXSkinInfoImpl_Release,
424     /* ID3DXSkinInfo */
425     ID3DXSkinInfoImpl_SetBoneInfluence,
426     ID3DXSkinInfoImpl_SetBoneVertexInfluence,
427     ID3DXSkinInfoImpl_GetNumBoneInfluences,
428     ID3DXSkinInfoImpl_GetBoneInfluence,
429     ID3DXSkinInfoImpl_GetBoneVertexInfluence,
430     ID3DXSkinInfoImpl_GetMaxVertexInfluences,
431     ID3DXSkinInfoImpl_GetNumBones,
432     ID3DXSkinInfoImpl_FindBoneVertexInfluenceIndex,
433     ID3DXSkinInfoImpl_GetMaxFaceInfluences,
434     ID3DXSkinInfoImpl_SetMinBoneInfluence,
435     ID3DXSkinInfoImpl_GetMinBoneInfluence,
436     ID3DXSkinInfoImpl_SetBoneName,
437     ID3DXSkinInfoImpl_GetBoneName,
438     ID3DXSkinInfoImpl_SetBoneOffsetMatrix,
439     ID3DXSkinInfoImpl_GetBoneOffsetMatrix,
440     ID3DXSkinInfoImpl_Clone,
441     ID3DXSkinInfoImpl_Remap,
442     ID3DXSkinInfoImpl_SetFVF,
443     ID3DXSkinInfoImpl_SetDeclaration,
444     ID3DXSkinInfoImpl_GetFVF,
445     ID3DXSkinInfoImpl_GetDeclaration,
446     ID3DXSkinInfoImpl_UpdateSkinnedMesh,
447     ID3DXSkinInfoImpl_ConvertToBlendedMesh,
448     ID3DXSkinInfoImpl_ConvertToIndexedBlendedMesh
449 };
450
451 HRESULT WINAPI D3DXCreateSkinInfo(DWORD num_vertices, CONST D3DVERTEXELEMENT9 *declaration,
452                                   DWORD num_bones, LPD3DXSKININFO *skin_info)
453 {
454     HRESULT hr;
455     ID3DXSkinInfoImpl *object = NULL;
456     static const D3DVERTEXELEMENT9 empty_declaration = D3DDECL_END();
457
458     TRACE("(%u, %p, %u, %p)\n", num_vertices, declaration, num_bones, skin_info);
459
460     if (!skin_info || !declaration)
461         return D3DERR_INVALIDCALL;
462
463     object = HeapAlloc(GetProcessHeap(), 0, sizeof(*object));
464     if (!object)
465         return E_OUTOFMEMORY;
466
467     object->ID3DXSkinInfo_iface.lpVtbl = &ID3DXSkinInfoImpl_Vtbl;
468     object->ref = 1;
469     object->num_vertices = num_vertices;
470     object->num_bones = num_bones;
471     object->vertex_declaration[0] = empty_declaration;
472     object->fvf = 0;
473
474     object->bones = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, num_bones * sizeof(*object->bones));
475     if (!object->bones) {
476         hr = E_OUTOFMEMORY;
477         goto error;
478     }
479
480     hr = ID3DXSkinInfoImpl_SetDeclaration(&object->ID3DXSkinInfo_iface, declaration);
481     if (FAILED(hr)) goto error;
482
483     *skin_info = &object->ID3DXSkinInfo_iface;
484
485     return D3D_OK;
486 error:
487     HeapFree(GetProcessHeap(), 0, object->bones);
488     HeapFree(GetProcessHeap(), 0, object);
489     return hr;
490 }
491
492 HRESULT WINAPI D3DXCreateSkinInfoFVF(DWORD num_vertices, DWORD fvf, DWORD num_bones, LPD3DXSKININFO *skin_info)
493 {
494     HRESULT hr;
495     D3DVERTEXELEMENT9 declaration[MAX_FVF_DECL_SIZE];
496
497     TRACE("(%u, %x, %u, %p)\n", num_vertices, fvf, num_bones, skin_info);
498
499     hr = D3DXDeclaratorFromFVF(fvf, declaration);
500     if (FAILED(hr))
501         return hr;
502
503     return D3DXCreateSkinInfo(num_vertices, declaration, num_bones, skin_info);
504 }