d3dx9: Implement ID3DXSkinInfo bone influence getting and setting.
[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     DWORD num_influences;
29     DWORD *vertices;
30     FLOAT *weights;
31 };
32
33 typedef struct ID3DXSkinInfoImpl
34 {
35     ID3DXSkinInfo ID3DXSkinInfo_iface;
36     LONG ref;
37
38     DWORD fvf;
39     D3DVERTEXELEMENT9 vertex_declaration[MAX_FVF_DECL_SIZE];
40     DWORD num_vertices;
41     DWORD num_bones;
42     struct bone *bones;
43 } ID3DXSkinInfoImpl;
44
45 static inline struct ID3DXSkinInfoImpl *impl_from_ID3DXSkinInfo(ID3DXSkinInfo *iface)
46 {
47     return CONTAINING_RECORD(iface, struct ID3DXSkinInfoImpl, ID3DXSkinInfo_iface);
48 }
49
50 static HRESULT WINAPI ID3DXSkinInfoImpl_QueryInterface(ID3DXSkinInfo *iface, REFIID riid, void **ppobj)
51 {
52     TRACE("(%p, %s, %p)\n", iface, debugstr_guid(riid), ppobj);
53
54     if (IsEqualGUID(riid, &IID_IUnknown) || IsEqualGUID(riid, &IID_ID3DXSkinInfo))
55     {
56         IUnknown_AddRef(iface);
57         *ppobj = iface;
58         return D3D_OK;
59     }
60
61     WARN("%s not implemented, returning E_NOINTERFACE\n", debugstr_guid(riid));
62
63     return E_NOINTERFACE;
64 }
65
66 static ULONG WINAPI ID3DXSkinInfoImpl_AddRef(ID3DXSkinInfo *iface)
67 {
68     struct ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
69     ULONG ref = InterlockedIncrement(&This->ref);
70
71     TRACE("%p increasing refcount to %u\n", This, ref);
72
73     return ref;
74 }
75
76 static ULONG WINAPI ID3DXSkinInfoImpl_Release(ID3DXSkinInfo *iface)
77 {
78     struct ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
79     ULONG ref = InterlockedDecrement(&This->ref);
80
81     TRACE("%p decreasing refcount to %u\n", This, ref);
82
83     if (ref == 0) {
84         int i;
85         for (i = 0; i < This->num_bones; i++) {
86             HeapFree(GetProcessHeap(), 0, This->bones[i].vertices);
87             HeapFree(GetProcessHeap(), 0, This->bones[i].weights);
88         }
89         HeapFree(GetProcessHeap(), 0, This->bones);
90         HeapFree(GetProcessHeap(), 0, This);
91     }
92
93     return ref;
94 }
95
96 static HRESULT WINAPI ID3DXSkinInfoImpl_SetBoneInfluence(ID3DXSkinInfo *iface, DWORD bone_num, DWORD num_influences, CONST DWORD *vertices, CONST FLOAT *weights)
97 {
98     ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
99     struct bone *bone;
100     DWORD *new_vertices = NULL;
101     FLOAT *new_weights = NULL;
102
103     TRACE("(%p, %u, %u, %p, %p)\n", This, bone_num, num_influences, vertices, weights);
104
105     if (bone_num >= This->num_bones || !vertices || !weights)
106         return D3DERR_INVALIDCALL;
107
108     if (num_influences) {
109         new_vertices = HeapAlloc(GetProcessHeap(), 0, num_influences * sizeof(*vertices));
110         if (!new_vertices)
111             return E_OUTOFMEMORY;
112         new_weights = HeapAlloc(GetProcessHeap(), 0, num_influences * sizeof(*weights));
113         if (!new_weights) {
114             HeapFree(GetProcessHeap(), 0, new_vertices);
115             return E_OUTOFMEMORY;
116         }
117         memcpy(new_vertices, vertices, num_influences * sizeof(*vertices));
118         memcpy(new_weights, weights, num_influences * sizeof(*weights));
119     }
120     bone = &This->bones[bone_num];
121     bone->num_influences = num_influences;
122     HeapFree(GetProcessHeap(), 0, bone->vertices);
123     HeapFree(GetProcessHeap(), 0, bone->weights);
124     bone->vertices = new_vertices;
125     bone->weights = new_weights;
126
127     return D3D_OK;
128 }
129
130 static HRESULT WINAPI ID3DXSkinInfoImpl_SetBoneVertexInfluence(ID3DXSkinInfo *iface, DWORD bone_num, DWORD influence_num, float weight)
131 {
132     ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
133
134     FIXME("(%p, %u, %u, %g): stub\n", This, bone_num, influence_num, weight);
135
136     return E_NOTIMPL;
137 }
138
139 static DWORD WINAPI ID3DXSkinInfoImpl_GetNumBoneInfluences(ID3DXSkinInfo *iface, DWORD bone_num)
140 {
141     ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
142
143     TRACE("(%p, %u)\n", This, bone_num);
144
145     if (bone_num >= This->num_bones)
146         return 0;
147
148     return This->bones[bone_num].num_influences;
149 }
150
151 static HRESULT WINAPI ID3DXSkinInfoImpl_GetBoneInfluence(ID3DXSkinInfo *iface, DWORD bone_num, DWORD *vertices, FLOAT *weights)
152 {
153     ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
154     struct bone *bone;
155
156     TRACE("(%p, %u, %p, %p)\n", This, bone_num, vertices, weights);
157
158     if (bone_num >= This->num_bones || !vertices)
159         return D3DERR_INVALIDCALL;
160
161     bone = &This->bones[bone_num];
162     if (!bone->num_influences)
163         return D3D_OK;
164
165     memcpy(vertices, bone->vertices, bone->num_influences * sizeof(*vertices));
166     if (weights)
167         memcpy(weights, bone->weights, bone->num_influences * sizeof(*weights));
168
169     return D3D_OK;
170 }
171
172 static HRESULT WINAPI ID3DXSkinInfoImpl_GetBoneVertexInfluence(ID3DXSkinInfo *iface, DWORD bone_num,
173         DWORD influence_num, float *weight, DWORD *vertex_num)
174 {
175     ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
176
177     FIXME("(%p, %u, %u, %p, %p): stub\n", This, bone_num, influence_num, weight, vertex_num);
178
179     return E_NOTIMPL;
180 }
181
182 static HRESULT WINAPI ID3DXSkinInfoImpl_GetMaxVertexInfluences(ID3DXSkinInfo *iface, DWORD *max_vertex_influences)
183 {
184     ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
185
186     FIXME("(%p, %p): stub\n", This, max_vertex_influences);
187
188     return E_NOTIMPL;
189 }
190
191 static DWORD WINAPI ID3DXSkinInfoImpl_GetNumBones(ID3DXSkinInfo *iface)
192 {
193     ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
194
195     TRACE("(%p)\n", This);
196
197     return This->num_bones;
198 }
199
200 static HRESULT WINAPI ID3DXSkinInfoImpl_FindBoneVertexInfluenceIndex(ID3DXSkinInfo *iface, DWORD bone_num,
201         DWORD vertex_num, DWORD *influence_index)
202 {
203     ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
204
205     FIXME("(%p, %u, %u, %p): stub\n", This, bone_num, vertex_num, influence_index);
206
207     return E_NOTIMPL;
208 }
209
210 static HRESULT WINAPI ID3DXSkinInfoImpl_GetMaxFaceInfluences(ID3DXSkinInfo *iface, LPDIRECT3DINDEXBUFFER9 index_buffer, DWORD num_faces, DWORD *max_face_influences)
211 {
212     ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
213
214     FIXME("(%p, %p, %u, %p): stub\n", This, index_buffer, num_faces, max_face_influences);
215
216     return E_NOTIMPL;
217 }
218
219 static HRESULT WINAPI ID3DXSkinInfoImpl_SetMinBoneInfluence(ID3DXSkinInfo *iface, FLOAT min_influence)
220 {
221     ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
222
223     FIXME("(%p, %g): stub\n", This, min_influence);
224
225     return E_NOTIMPL;
226 }
227
228 static FLOAT WINAPI ID3DXSkinInfoImpl_GetMinBoneInfluence(ID3DXSkinInfo *iface)
229 {
230     ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
231
232     FIXME("(%p): stub\n", This);
233
234     return 0.0f;
235 }
236
237 static HRESULT WINAPI ID3DXSkinInfoImpl_SetBoneName(ID3DXSkinInfo *iface, DWORD bone_num, LPCSTR name)
238 {
239     ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
240
241     FIXME("(%p, %u, %s): stub\n", This, bone_num, debugstr_a(name));
242
243     return E_NOTIMPL;
244 }
245
246 static LPCSTR WINAPI ID3DXSkinInfoImpl_GetBoneName(ID3DXSkinInfo *iface, DWORD bone_num)
247 {
248     ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
249
250     FIXME("(%p, %u): stub\n", This, bone_num);
251
252     return NULL;
253 }
254
255 static HRESULT WINAPI ID3DXSkinInfoImpl_SetBoneOffsetMatrix(ID3DXSkinInfo *iface, DWORD bone_num, CONST D3DXMATRIX *bone_transform)
256 {
257     ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
258
259     FIXME("(%p, %u, %p): stub\n", This, bone_num, bone_transform);
260
261     return E_NOTIMPL;
262 }
263
264 static LPD3DXMATRIX WINAPI ID3DXSkinInfoImpl_GetBoneOffsetMatrix(ID3DXSkinInfo *iface, DWORD bone_num)
265 {
266     ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
267
268     FIXME("(%p, %u): stub\n", This, bone_num);
269
270     return NULL;
271 }
272
273 static HRESULT WINAPI ID3DXSkinInfoImpl_Clone(ID3DXSkinInfo *iface, LPD3DXSKININFO *skin_info)
274 {
275     ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
276
277     FIXME("(%p, %p): stub\n", This, skin_info);
278
279     return E_NOTIMPL;
280 }
281
282 static HRESULT WINAPI ID3DXSkinInfoImpl_Remap(ID3DXSkinInfo *iface, DWORD num_vertices, DWORD *vertex_remap)
283 {
284     ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
285
286     FIXME("(%p, %u, %p): stub\n", This, num_vertices, vertex_remap);
287
288     return E_NOTIMPL;
289 }
290
291 static HRESULT WINAPI ID3DXSkinInfoImpl_SetFVF(ID3DXSkinInfo *iface, DWORD fvf)
292 {
293     ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
294     HRESULT hr;
295     D3DVERTEXELEMENT9 declaration[MAX_FVF_DECL_SIZE];
296
297     TRACE("(%p, %x)\n", This, fvf);
298
299     hr = D3DXDeclaratorFromFVF(fvf, declaration);
300     if (FAILED(hr)) return hr;
301
302     return iface->lpVtbl->SetDeclaration(iface, declaration);
303 }
304
305 static HRESULT WINAPI ID3DXSkinInfoImpl_SetDeclaration(ID3DXSkinInfo *iface, CONST D3DVERTEXELEMENT9 *declaration)
306 {
307     ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
308     HRESULT hr;
309     int count;
310
311     TRACE("(%p, %p)\n", This, declaration);
312
313     if (!declaration)
314         return D3DERR_INVALIDCALL;
315     for (count = 0; declaration[count].Stream != 0xff; count++) {
316         if (declaration[count].Stream != 0) {
317             WARN("Invalid vertex element %u; contains non-zero stream %u\n",
318                  count, declaration[count].Stream);
319             return D3DERR_INVALIDCALL;
320         }
321     }
322     count++;
323
324     memcpy(This->vertex_declaration, declaration, count * sizeof(*declaration));
325
326     hr = D3DXFVFFromDeclarator(This->vertex_declaration, &This->fvf);
327     if (FAILED(hr))
328         This->fvf = 0;
329
330     return D3D_OK;
331 }
332
333 static DWORD WINAPI ID3DXSkinInfoImpl_GetFVF(ID3DXSkinInfo *iface)
334 {
335     ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
336
337     TRACE("(%p)\n", This);
338
339     return This->fvf;
340 }
341
342 static HRESULT WINAPI ID3DXSkinInfoImpl_GetDeclaration(ID3DXSkinInfo *iface, D3DVERTEXELEMENT9 declaration[MAX_FVF_DECL_SIZE])
343 {
344     ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
345     UINT count = 0;
346
347     TRACE("(%p)\n", This);
348
349     while (This->vertex_declaration[count++].Stream != 0xff);
350     memcpy(declaration, This->vertex_declaration, count * sizeof(declaration[0]));
351     return D3D_OK;
352 }
353
354 static HRESULT WINAPI ID3DXSkinInfoImpl_UpdateSkinnedMesh(ID3DXSkinInfo *iface, CONST D3DXMATRIX *bone_transforms,
355         CONST D3DXMATRIX *bone_inv_transpose_transforms, LPCVOID vertices_src, PVOID vertices_dest)
356 {
357     ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
358
359     FIXME("(%p, %p, %p, %p, %p): stub\n", This, bone_transforms, bone_inv_transpose_transforms, vertices_src, vertices_dest);
360
361     return E_NOTIMPL;
362 }
363
364 static HRESULT WINAPI ID3DXSkinInfoImpl_ConvertToBlendedMesh(ID3DXSkinInfo *iface, LPD3DXMESH mesh_in,
365         DWORD options, CONST DWORD *adjacency_in, LPDWORD adjacency_out, DWORD *face_remap,
366         LPD3DXBUFFER *vertex_remap, DWORD *max_face_infl, DWORD *num_bone_combinations,
367         LPD3DXBUFFER *bone_combination_table, LPD3DXMESH *mesh_out)
368 {
369     ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
370
371     FIXME("(%p, %p, %x, %p, %p, %p, %p, %p, %p, %p, %p): stub\n",
372           This, mesh_in, options, adjacency_in, adjacency_out, face_remap, vertex_remap,
373           max_face_infl, num_bone_combinations, bone_combination_table, mesh_out);
374
375     return E_NOTIMPL;
376 }
377
378 static HRESULT WINAPI ID3DXSkinInfoImpl_ConvertToIndexedBlendedMesh(ID3DXSkinInfo *iface, LPD3DXMESH mesh_in,
379         DWORD options, CONST DWORD *adjacency_in, LPDWORD adjacency_out, DWORD *face_remap,
380         LPD3DXBUFFER *vertex_remap, DWORD *max_face_infl, DWORD *num_bone_combinations,
381         LPD3DXBUFFER *bone_combination_table, LPD3DXMESH *mesh_out)
382 {
383     ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
384
385     FIXME("(%p, %p, %x, %p, %p, %p, %p, %p, %p, %p, %p): stub\n",
386           This, mesh_in, options, adjacency_in, adjacency_out, face_remap, vertex_remap,
387           max_face_infl, num_bone_combinations, bone_combination_table, mesh_out);
388
389     return E_NOTIMPL;
390 }
391
392 static const struct ID3DXSkinInfoVtbl ID3DXSkinInfoImpl_Vtbl =
393 {
394     /* IUnknown methods */
395     ID3DXSkinInfoImpl_QueryInterface,
396     ID3DXSkinInfoImpl_AddRef,
397     ID3DXSkinInfoImpl_Release,
398     /* ID3DXSkinInfo */
399     ID3DXSkinInfoImpl_SetBoneInfluence,
400     ID3DXSkinInfoImpl_SetBoneVertexInfluence,
401     ID3DXSkinInfoImpl_GetNumBoneInfluences,
402     ID3DXSkinInfoImpl_GetBoneInfluence,
403     ID3DXSkinInfoImpl_GetBoneVertexInfluence,
404     ID3DXSkinInfoImpl_GetMaxVertexInfluences,
405     ID3DXSkinInfoImpl_GetNumBones,
406     ID3DXSkinInfoImpl_FindBoneVertexInfluenceIndex,
407     ID3DXSkinInfoImpl_GetMaxFaceInfluences,
408     ID3DXSkinInfoImpl_SetMinBoneInfluence,
409     ID3DXSkinInfoImpl_GetMinBoneInfluence,
410     ID3DXSkinInfoImpl_SetBoneName,
411     ID3DXSkinInfoImpl_GetBoneName,
412     ID3DXSkinInfoImpl_SetBoneOffsetMatrix,
413     ID3DXSkinInfoImpl_GetBoneOffsetMatrix,
414     ID3DXSkinInfoImpl_Clone,
415     ID3DXSkinInfoImpl_Remap,
416     ID3DXSkinInfoImpl_SetFVF,
417     ID3DXSkinInfoImpl_SetDeclaration,
418     ID3DXSkinInfoImpl_GetFVF,
419     ID3DXSkinInfoImpl_GetDeclaration,
420     ID3DXSkinInfoImpl_UpdateSkinnedMesh,
421     ID3DXSkinInfoImpl_ConvertToBlendedMesh,
422     ID3DXSkinInfoImpl_ConvertToIndexedBlendedMesh
423 };
424
425 HRESULT WINAPI D3DXCreateSkinInfo(DWORD num_vertices, CONST D3DVERTEXELEMENT9 *declaration,
426                                   DWORD num_bones, LPD3DXSKININFO *skin_info)
427 {
428     HRESULT hr;
429     ID3DXSkinInfoImpl *object = NULL;
430     static const D3DVERTEXELEMENT9 empty_declaration = D3DDECL_END();
431
432     TRACE("(%u, %p, %u, %p)\n", num_vertices, declaration, num_bones, skin_info);
433
434     if (!skin_info || !declaration)
435         return D3DERR_INVALIDCALL;
436
437     object = HeapAlloc(GetProcessHeap(), 0, sizeof(*object));
438     if (!object)
439         return E_OUTOFMEMORY;
440
441     object->ID3DXSkinInfo_iface.lpVtbl = &ID3DXSkinInfoImpl_Vtbl;
442     object->ref = 1;
443     object->num_vertices = num_vertices;
444     object->num_bones = num_bones;
445     object->vertex_declaration[0] = empty_declaration;
446     object->fvf = 0;
447
448     object->bones = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, num_bones * sizeof(*object->bones));
449     if (!object->bones) {
450         hr = E_OUTOFMEMORY;
451         goto error;
452     }
453
454     hr = ID3DXSkinInfoImpl_SetDeclaration(&object->ID3DXSkinInfo_iface, declaration);
455     if (FAILED(hr)) goto error;
456
457     *skin_info = &object->ID3DXSkinInfo_iface;
458
459     return D3D_OK;
460 error:
461     HeapFree(GetProcessHeap(), 0, object->bones);
462     HeapFree(GetProcessHeap(), 0, object);
463     return hr;
464 }
465
466 HRESULT WINAPI D3DXCreateSkinInfoFVF(DWORD num_vertices, DWORD fvf, DWORD num_bones, LPD3DXSKININFO *skin_info)
467 {
468     HRESULT hr;
469     D3DVERTEXELEMENT9 declaration[MAX_FVF_DECL_SIZE];
470
471     TRACE("(%u, %x, %u, %p)\n", num_vertices, fvf, num_bones, skin_info);
472
473     hr = D3DXDeclaratorFromFVF(fvf, declaration);
474     if (FAILED(hr))
475         return hr;
476
477     return D3DXCreateSkinInfo(num_vertices, declaration, num_bones, skin_info);
478 }