msvcr100: Added _aligned_msize implementation.
[wine.git] / dlls / d3dx9_36 / skin.c
blob1525f0efb71bbb0fb9200d4ee0274c08c851e4b3
1 /*
2 * Skin Info operations specific to D3DX9.
4 * Copyright (C) 2011 Dylan Smith
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.
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.
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
21 #include "wine/debug.h"
22 #include "d3dx9_36_private.h"
24 WINE_DEFAULT_DEBUG_CHANNEL(d3dx);
26 struct bone
28 char *name;
29 D3DXMATRIX transform;
30 DWORD num_influences;
31 DWORD *vertices;
32 FLOAT *weights;
35 typedef struct ID3DXSkinInfoImpl
37 ID3DXSkinInfo ID3DXSkinInfo_iface;
38 LONG ref;
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;
47 static inline struct ID3DXSkinInfoImpl *impl_from_ID3DXSkinInfo(ID3DXSkinInfo *iface)
49 return CONTAINING_RECORD(iface, struct ID3DXSkinInfoImpl, ID3DXSkinInfo_iface);
52 static HRESULT WINAPI ID3DXSkinInfoImpl_QueryInterface(ID3DXSkinInfo *iface, REFIID riid, void **ppobj)
54 TRACE("(%p, %s, %p)\n", iface, debugstr_guid(riid), ppobj);
56 if (IsEqualGUID(riid, &IID_IUnknown) || IsEqualGUID(riid, &IID_ID3DXSkinInfo))
58 IUnknown_AddRef(iface);
59 *ppobj = iface;
60 return D3D_OK;
63 WARN("%s not implemented, returning E_NOINTERFACE\n", debugstr_guid(riid));
65 return E_NOINTERFACE;
68 static ULONG WINAPI ID3DXSkinInfoImpl_AddRef(ID3DXSkinInfo *iface)
70 struct ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
71 ULONG ref = InterlockedIncrement(&This->ref);
73 TRACE("%p increasing refcount to %u\n", This, ref);
75 return ref;
78 static ULONG WINAPI ID3DXSkinInfoImpl_Release(ID3DXSkinInfo *iface)
80 struct ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
81 ULONG ref = InterlockedDecrement(&This->ref);
83 TRACE("%p decreasing refcount to %u\n", This, ref);
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);
92 HeapFree(GetProcessHeap(), 0, This->bones);
93 HeapFree(GetProcessHeap(), 0, This);
96 return ref;
99 static HRESULT WINAPI ID3DXSkinInfoImpl_SetBoneInfluence(ID3DXSkinInfo *iface, DWORD bone_num, DWORD num_influences, CONST DWORD *vertices, CONST FLOAT *weights)
101 ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
102 struct bone *bone;
103 DWORD *new_vertices = NULL;
104 FLOAT *new_weights = NULL;
106 TRACE("(%p, %u, %u, %p, %p)\n", This, bone_num, num_influences, vertices, weights);
108 if (bone_num >= This->num_bones || !vertices || !weights)
109 return D3DERR_INVALIDCALL;
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;
120 memcpy(new_vertices, vertices, num_influences * sizeof(*vertices));
121 memcpy(new_weights, weights, num_influences * sizeof(*weights));
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;
130 return D3D_OK;
133 static HRESULT WINAPI ID3DXSkinInfoImpl_SetBoneVertexInfluence(ID3DXSkinInfo *iface, DWORD bone_num, DWORD influence_num, float weight)
135 ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
137 FIXME("(%p, %u, %u, %g): stub\n", This, bone_num, influence_num, weight);
139 return E_NOTIMPL;
142 static DWORD WINAPI ID3DXSkinInfoImpl_GetNumBoneInfluences(ID3DXSkinInfo *iface, DWORD bone_num)
144 ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
146 TRACE("(%p, %u)\n", This, bone_num);
148 if (bone_num >= This->num_bones)
149 return 0;
151 return This->bones[bone_num].num_influences;
154 static HRESULT WINAPI ID3DXSkinInfoImpl_GetBoneInfluence(ID3DXSkinInfo *iface, DWORD bone_num, DWORD *vertices, FLOAT *weights)
156 ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
157 struct bone *bone;
159 TRACE("(%p, %u, %p, %p)\n", This, bone_num, vertices, weights);
161 if (bone_num >= This->num_bones || !vertices)
162 return D3DERR_INVALIDCALL;
164 bone = &This->bones[bone_num];
165 if (!bone->num_influences)
166 return D3D_OK;
168 memcpy(vertices, bone->vertices, bone->num_influences * sizeof(*vertices));
169 if (weights)
170 memcpy(weights, bone->weights, bone->num_influences * sizeof(*weights));
172 return D3D_OK;
175 static HRESULT WINAPI ID3DXSkinInfoImpl_GetBoneVertexInfluence(ID3DXSkinInfo *iface, DWORD bone_num,
176 DWORD influence_num, float *weight, DWORD *vertex_num)
178 ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
180 FIXME("(%p, %u, %u, %p, %p): stub\n", This, bone_num, influence_num, weight, vertex_num);
182 return E_NOTIMPL;
185 static HRESULT WINAPI ID3DXSkinInfoImpl_GetMaxVertexInfluences(ID3DXSkinInfo *iface, DWORD *max_vertex_influences)
187 ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
189 FIXME("(%p, %p): stub\n", This, max_vertex_influences);
191 return E_NOTIMPL;
194 static DWORD WINAPI ID3DXSkinInfoImpl_GetNumBones(ID3DXSkinInfo *iface)
196 ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
198 TRACE("(%p)\n", This);
200 return This->num_bones;
203 static HRESULT WINAPI ID3DXSkinInfoImpl_FindBoneVertexInfluenceIndex(ID3DXSkinInfo *iface, DWORD bone_num,
204 DWORD vertex_num, DWORD *influence_index)
206 ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
208 FIXME("(%p, %u, %u, %p): stub\n", This, bone_num, vertex_num, influence_index);
210 return E_NOTIMPL;
213 static HRESULT WINAPI ID3DXSkinInfoImpl_GetMaxFaceInfluences(struct ID3DXSkinInfo *iface,
214 struct IDirect3DIndexBuffer9 *index_buffer, DWORD num_faces, DWORD *max_face_influences)
216 ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
218 FIXME("(%p, %p, %u, %p): stub\n", This, index_buffer, num_faces, max_face_influences);
220 return E_NOTIMPL;
223 static HRESULT WINAPI ID3DXSkinInfoImpl_SetMinBoneInfluence(ID3DXSkinInfo *iface, FLOAT min_influence)
225 ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
227 FIXME("(%p, %g): stub\n", This, min_influence);
229 return E_NOTIMPL;
232 static FLOAT WINAPI ID3DXSkinInfoImpl_GetMinBoneInfluence(ID3DXSkinInfo *iface)
234 ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
236 FIXME("(%p): stub\n", This);
238 return 0.0f;
241 static HRESULT WINAPI ID3DXSkinInfoImpl_SetBoneName(ID3DXSkinInfo *iface, DWORD bone_num, LPCSTR name)
243 ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
244 char *new_name;
245 size_t size;
247 TRACE("(%p, %u, %s)\n", This, bone_num, debugstr_a(name));
249 if (bone_num >= This->num_bones || !name)
250 return D3DERR_INVALIDCALL;
252 size = strlen(name) + 1;
253 new_name = HeapAlloc(GetProcessHeap(), 0, size);
254 if (!new_name)
255 return E_OUTOFMEMORY;
256 memcpy(new_name, name, size);
257 HeapFree(GetProcessHeap(), 0, This->bones[bone_num].name);
258 This->bones[bone_num].name = new_name;
260 return D3D_OK;
263 static LPCSTR WINAPI ID3DXSkinInfoImpl_GetBoneName(ID3DXSkinInfo *iface, DWORD bone_num)
265 ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
267 TRACE("(%p, %u)\n", This, bone_num);
269 if (bone_num >= This->num_bones)
270 return NULL;
272 return This->bones[bone_num].name;
275 static HRESULT WINAPI ID3DXSkinInfoImpl_SetBoneOffsetMatrix(ID3DXSkinInfo *iface, DWORD bone_num, CONST D3DXMATRIX *bone_transform)
277 ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
279 TRACE("(%p, %u, %p)\n", This, bone_num, bone_transform);
281 if (bone_num >= This->num_bones || !bone_transform)
282 return D3DERR_INVALIDCALL;
284 This->bones[bone_num].transform = *bone_transform;
285 return D3D_OK;
288 static LPD3DXMATRIX WINAPI ID3DXSkinInfoImpl_GetBoneOffsetMatrix(ID3DXSkinInfo *iface, DWORD bone_num)
290 ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
292 TRACE("(%p, %u)\n", This, bone_num);
294 if (bone_num >= This->num_bones)
295 return NULL;
297 return &This->bones[bone_num].transform;
300 static HRESULT WINAPI ID3DXSkinInfoImpl_Clone(ID3DXSkinInfo *iface, ID3DXSkinInfo **skin_info)
302 ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
304 FIXME("(%p, %p): stub\n", This, skin_info);
306 return E_NOTIMPL;
309 static HRESULT WINAPI ID3DXSkinInfoImpl_Remap(ID3DXSkinInfo *iface, DWORD num_vertices, DWORD *vertex_remap)
311 ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
313 FIXME("(%p, %u, %p): stub\n", This, num_vertices, vertex_remap);
315 return E_NOTIMPL;
318 static HRESULT WINAPI ID3DXSkinInfoImpl_SetFVF(ID3DXSkinInfo *iface, DWORD fvf)
320 ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
321 HRESULT hr;
322 D3DVERTEXELEMENT9 declaration[MAX_FVF_DECL_SIZE];
324 TRACE("(%p, %x)\n", This, fvf);
326 hr = D3DXDeclaratorFromFVF(fvf, declaration);
327 if (FAILED(hr)) return hr;
329 return iface->lpVtbl->SetDeclaration(iface, declaration);
332 static HRESULT WINAPI ID3DXSkinInfoImpl_SetDeclaration(ID3DXSkinInfo *iface, CONST D3DVERTEXELEMENT9 *declaration)
334 ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
335 HRESULT hr;
336 int count;
338 TRACE("(%p, %p)\n", This, declaration);
340 if (!declaration)
341 return D3DERR_INVALIDCALL;
342 for (count = 0; declaration[count].Stream != 0xff; count++) {
343 if (declaration[count].Stream != 0) {
344 WARN("Invalid vertex element %u; contains non-zero stream %u\n",
345 count, declaration[count].Stream);
346 return D3DERR_INVALIDCALL;
349 count++;
351 memcpy(This->vertex_declaration, declaration, count * sizeof(*declaration));
353 hr = D3DXFVFFromDeclarator(This->vertex_declaration, &This->fvf);
354 if (FAILED(hr))
355 This->fvf = 0;
357 return D3D_OK;
360 static DWORD WINAPI ID3DXSkinInfoImpl_GetFVF(ID3DXSkinInfo *iface)
362 ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
364 TRACE("(%p)\n", This);
366 return This->fvf;
369 static HRESULT WINAPI ID3DXSkinInfoImpl_GetDeclaration(ID3DXSkinInfo *iface, D3DVERTEXELEMENT9 declaration[MAX_FVF_DECL_SIZE])
371 ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
372 UINT count = 0;
374 TRACE("(%p)\n", This);
376 while (This->vertex_declaration[count++].Stream != 0xff);
377 memcpy(declaration, This->vertex_declaration, count * sizeof(declaration[0]));
378 return D3D_OK;
381 static HRESULT WINAPI ID3DXSkinInfoImpl_UpdateSkinnedMesh(ID3DXSkinInfo *iface, CONST D3DXMATRIX *bone_transforms,
382 CONST D3DXMATRIX *bone_inv_transpose_transforms, LPCVOID vertices_src, PVOID vertices_dest)
384 ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
386 FIXME("(%p, %p, %p, %p, %p): stub\n", This, bone_transforms, bone_inv_transpose_transforms, vertices_src, vertices_dest);
388 return E_NOTIMPL;
391 static HRESULT WINAPI ID3DXSkinInfoImpl_ConvertToBlendedMesh(ID3DXSkinInfo *iface, ID3DXMesh *mesh_in,
392 DWORD options, const DWORD *adjacency_in, DWORD *adjacency_out, DWORD *face_remap,
393 ID3DXBuffer **vertex_remap, DWORD *max_face_infl, DWORD *num_bone_combinations,
394 ID3DXBuffer **bone_combination_table, ID3DXMesh **mesh_out)
396 ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
398 FIXME("(%p, %p, %x, %p, %p, %p, %p, %p, %p, %p, %p): stub\n",
399 This, mesh_in, options, adjacency_in, adjacency_out, face_remap, vertex_remap,
400 max_face_infl, num_bone_combinations, bone_combination_table, mesh_out);
402 return E_NOTIMPL;
405 static HRESULT WINAPI ID3DXSkinInfoImpl_ConvertToIndexedBlendedMesh(ID3DXSkinInfo *iface, ID3DXMesh *mesh_in,
406 DWORD options, const DWORD *adjacency_in, DWORD *adjacency_out, DWORD *face_remap,
407 ID3DXBuffer **vertex_remap, DWORD *max_face_infl, DWORD *num_bone_combinations,
408 ID3DXBuffer **bone_combination_table, ID3DXMesh **mesh_out)
410 ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
412 FIXME("(%p, %p, %x, %p, %p, %p, %p, %p, %p, %p, %p): stub\n",
413 This, mesh_in, options, adjacency_in, adjacency_out, face_remap, vertex_remap,
414 max_face_infl, num_bone_combinations, bone_combination_table, mesh_out);
416 return E_NOTIMPL;
419 static const struct ID3DXSkinInfoVtbl ID3DXSkinInfoImpl_Vtbl =
421 /* IUnknown methods */
422 ID3DXSkinInfoImpl_QueryInterface,
423 ID3DXSkinInfoImpl_AddRef,
424 ID3DXSkinInfoImpl_Release,
425 /* ID3DXSkinInfo */
426 ID3DXSkinInfoImpl_SetBoneInfluence,
427 ID3DXSkinInfoImpl_SetBoneVertexInfluence,
428 ID3DXSkinInfoImpl_GetNumBoneInfluences,
429 ID3DXSkinInfoImpl_GetBoneInfluence,
430 ID3DXSkinInfoImpl_GetBoneVertexInfluence,
431 ID3DXSkinInfoImpl_GetMaxVertexInfluences,
432 ID3DXSkinInfoImpl_GetNumBones,
433 ID3DXSkinInfoImpl_FindBoneVertexInfluenceIndex,
434 ID3DXSkinInfoImpl_GetMaxFaceInfluences,
435 ID3DXSkinInfoImpl_SetMinBoneInfluence,
436 ID3DXSkinInfoImpl_GetMinBoneInfluence,
437 ID3DXSkinInfoImpl_SetBoneName,
438 ID3DXSkinInfoImpl_GetBoneName,
439 ID3DXSkinInfoImpl_SetBoneOffsetMatrix,
440 ID3DXSkinInfoImpl_GetBoneOffsetMatrix,
441 ID3DXSkinInfoImpl_Clone,
442 ID3DXSkinInfoImpl_Remap,
443 ID3DXSkinInfoImpl_SetFVF,
444 ID3DXSkinInfoImpl_SetDeclaration,
445 ID3DXSkinInfoImpl_GetFVF,
446 ID3DXSkinInfoImpl_GetDeclaration,
447 ID3DXSkinInfoImpl_UpdateSkinnedMesh,
448 ID3DXSkinInfoImpl_ConvertToBlendedMesh,
449 ID3DXSkinInfoImpl_ConvertToIndexedBlendedMesh
452 HRESULT WINAPI D3DXCreateSkinInfo(DWORD num_vertices, const D3DVERTEXELEMENT9 *declaration,
453 DWORD num_bones, ID3DXSkinInfo **skin_info)
455 HRESULT hr;
456 ID3DXSkinInfoImpl *object = NULL;
457 static const D3DVERTEXELEMENT9 empty_declaration = D3DDECL_END();
459 TRACE("(%u, %p, %u, %p)\n", num_vertices, declaration, num_bones, skin_info);
461 if (!skin_info || !declaration)
462 return D3DERR_INVALIDCALL;
464 object = HeapAlloc(GetProcessHeap(), 0, sizeof(*object));
465 if (!object)
466 return E_OUTOFMEMORY;
468 object->ID3DXSkinInfo_iface.lpVtbl = &ID3DXSkinInfoImpl_Vtbl;
469 object->ref = 1;
470 object->num_vertices = num_vertices;
471 object->num_bones = num_bones;
472 object->vertex_declaration[0] = empty_declaration;
473 object->fvf = 0;
475 object->bones = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, num_bones * sizeof(*object->bones));
476 if (!object->bones) {
477 hr = E_OUTOFMEMORY;
478 goto error;
481 hr = ID3DXSkinInfoImpl_SetDeclaration(&object->ID3DXSkinInfo_iface, declaration);
482 if (FAILED(hr)) goto error;
484 *skin_info = &object->ID3DXSkinInfo_iface;
486 return D3D_OK;
487 error:
488 HeapFree(GetProcessHeap(), 0, object->bones);
489 HeapFree(GetProcessHeap(), 0, object);
490 return hr;
493 HRESULT WINAPI D3DXCreateSkinInfoFVF(DWORD num_vertices, DWORD fvf, DWORD num_bones, ID3DXSkinInfo **skin_info)
495 HRESULT hr;
496 D3DVERTEXELEMENT9 declaration[MAX_FVF_DECL_SIZE];
498 TRACE("(%u, %x, %u, %p)\n", num_vertices, fvf, num_bones, skin_info);
500 hr = D3DXDeclaratorFromFVF(fvf, declaration);
501 if (FAILED(hr))
502 return hr;
504 return D3DXCreateSkinInfo(num_vertices, declaration, num_bones, skin_info);