msxml3/tests: Tests for IMXAttributes::clear().
[wine/multimedia.git] / dlls / d3dx9_36 / skin.c
blob3b03425290566689e4d716113d099e169477da6e
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(ID3DXSkinInfo *iface, LPDIRECT3DINDEXBUFFER9 index_buffer, DWORD num_faces, DWORD *max_face_influences)
215 ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
217 FIXME("(%p, %p, %u, %p): stub\n", This, index_buffer, num_faces, max_face_influences);
219 return E_NOTIMPL;
222 static HRESULT WINAPI ID3DXSkinInfoImpl_SetMinBoneInfluence(ID3DXSkinInfo *iface, FLOAT min_influence)
224 ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
226 FIXME("(%p, %g): stub\n", This, min_influence);
228 return E_NOTIMPL;
231 static FLOAT WINAPI ID3DXSkinInfoImpl_GetMinBoneInfluence(ID3DXSkinInfo *iface)
233 ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
235 FIXME("(%p): stub\n", This);
237 return 0.0f;
240 static HRESULT WINAPI ID3DXSkinInfoImpl_SetBoneName(ID3DXSkinInfo *iface, DWORD bone_num, LPCSTR name)
242 ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
243 char *new_name;
244 size_t size;
246 TRACE("(%p, %u, %s)\n", This, bone_num, debugstr_a(name));
248 if (bone_num >= This->num_bones || !name)
249 return D3DERR_INVALIDCALL;
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;
259 return D3D_OK;
262 static LPCSTR WINAPI ID3DXSkinInfoImpl_GetBoneName(ID3DXSkinInfo *iface, DWORD bone_num)
264 ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
266 TRACE("(%p, %u)\n", This, bone_num);
268 if (bone_num >= This->num_bones)
269 return NULL;
271 return This->bones[bone_num].name;
274 static HRESULT WINAPI ID3DXSkinInfoImpl_SetBoneOffsetMatrix(ID3DXSkinInfo *iface, DWORD bone_num, CONST D3DXMATRIX *bone_transform)
276 ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
278 TRACE("(%p, %u, %p)\n", This, bone_num, bone_transform);
280 if (bone_num >= This->num_bones || !bone_transform)
281 return D3DERR_INVALIDCALL;
283 This->bones[bone_num].transform = *bone_transform;
284 return D3D_OK;
287 static LPD3DXMATRIX WINAPI ID3DXSkinInfoImpl_GetBoneOffsetMatrix(ID3DXSkinInfo *iface, DWORD bone_num)
289 ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
291 TRACE("(%p, %u)\n", This, bone_num);
293 if (bone_num >= This->num_bones)
294 return NULL;
296 return &This->bones[bone_num].transform;
299 static HRESULT WINAPI ID3DXSkinInfoImpl_Clone(ID3DXSkinInfo *iface, LPD3DXSKININFO *skin_info)
301 ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
303 FIXME("(%p, %p): stub\n", This, skin_info);
305 return E_NOTIMPL;
308 static HRESULT WINAPI ID3DXSkinInfoImpl_Remap(ID3DXSkinInfo *iface, DWORD num_vertices, DWORD *vertex_remap)
310 ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
312 FIXME("(%p, %u, %p): stub\n", This, num_vertices, vertex_remap);
314 return E_NOTIMPL;
317 static HRESULT WINAPI ID3DXSkinInfoImpl_SetFVF(ID3DXSkinInfo *iface, DWORD fvf)
319 ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
320 HRESULT hr;
321 D3DVERTEXELEMENT9 declaration[MAX_FVF_DECL_SIZE];
323 TRACE("(%p, %x)\n", This, fvf);
325 hr = D3DXDeclaratorFromFVF(fvf, declaration);
326 if (FAILED(hr)) return hr;
328 return iface->lpVtbl->SetDeclaration(iface, declaration);
331 static HRESULT WINAPI ID3DXSkinInfoImpl_SetDeclaration(ID3DXSkinInfo *iface, CONST D3DVERTEXELEMENT9 *declaration)
333 ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
334 HRESULT hr;
335 int count;
337 TRACE("(%p, %p)\n", This, declaration);
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;
348 count++;
350 memcpy(This->vertex_declaration, declaration, count * sizeof(*declaration));
352 hr = D3DXFVFFromDeclarator(This->vertex_declaration, &This->fvf);
353 if (FAILED(hr))
354 This->fvf = 0;
356 return D3D_OK;
359 static DWORD WINAPI ID3DXSkinInfoImpl_GetFVF(ID3DXSkinInfo *iface)
361 ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
363 TRACE("(%p)\n", This);
365 return This->fvf;
368 static HRESULT WINAPI ID3DXSkinInfoImpl_GetDeclaration(ID3DXSkinInfo *iface, D3DVERTEXELEMENT9 declaration[MAX_FVF_DECL_SIZE])
370 ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
371 UINT count = 0;
373 TRACE("(%p)\n", This);
375 while (This->vertex_declaration[count++].Stream != 0xff);
376 memcpy(declaration, This->vertex_declaration, count * sizeof(declaration[0]));
377 return D3D_OK;
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)
383 ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
385 FIXME("(%p, %p, %p, %p, %p): stub\n", This, bone_transforms, bone_inv_transpose_transforms, vertices_src, vertices_dest);
387 return E_NOTIMPL;
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)
395 ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
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);
401 return E_NOTIMPL;
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)
409 ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
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);
415 return E_NOTIMPL;
418 static const struct ID3DXSkinInfoVtbl ID3DXSkinInfoImpl_Vtbl =
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
451 HRESULT WINAPI D3DXCreateSkinInfo(DWORD num_vertices, CONST D3DVERTEXELEMENT9 *declaration,
452 DWORD num_bones, LPD3DXSKININFO *skin_info)
454 HRESULT hr;
455 ID3DXSkinInfoImpl *object = NULL;
456 static const D3DVERTEXELEMENT9 empty_declaration = D3DDECL_END();
458 TRACE("(%u, %p, %u, %p)\n", num_vertices, declaration, num_bones, skin_info);
460 if (!skin_info || !declaration)
461 return D3DERR_INVALIDCALL;
463 object = HeapAlloc(GetProcessHeap(), 0, sizeof(*object));
464 if (!object)
465 return E_OUTOFMEMORY;
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;
474 object->bones = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, num_bones * sizeof(*object->bones));
475 if (!object->bones) {
476 hr = E_OUTOFMEMORY;
477 goto error;
480 hr = ID3DXSkinInfoImpl_SetDeclaration(&object->ID3DXSkinInfo_iface, declaration);
481 if (FAILED(hr)) goto error;
483 *skin_info = &object->ID3DXSkinInfo_iface;
485 return D3D_OK;
486 error:
487 HeapFree(GetProcessHeap(), 0, object->bones);
488 HeapFree(GetProcessHeap(), 0, object);
489 return hr;
492 HRESULT WINAPI D3DXCreateSkinInfoFVF(DWORD num_vertices, DWORD fvf, DWORD num_bones, LPD3DXSKININFO *skin_info)
494 HRESULT hr;
495 D3DVERTEXELEMENT9 declaration[MAX_FVF_DECL_SIZE];
497 TRACE("(%u, %x, %u, %p)\n", num_vertices, fvf, num_bones, skin_info);
499 hr = D3DXDeclaratorFromFVF(fvf, declaration);
500 if (FAILED(hr))
501 return hr;
503 return D3DXCreateSkinInfo(num_vertices, declaration, num_bones, skin_info);