comdlg32/tests: Use BOOL type where appropriate.
[wine.git] / dlls / d3dx9_36 / skin.c
blobaa5acf25ffbededd16b250dda79befd58141957c
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 struct d3dx9_skin_info
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;
47 static inline struct d3dx9_skin_info *impl_from_ID3DXSkinInfo(ID3DXSkinInfo *iface)
49 return CONTAINING_RECORD(iface, struct d3dx9_skin_info, ID3DXSkinInfo_iface);
52 static HRESULT WINAPI d3dx9_skin_info_QueryInterface(ID3DXSkinInfo *iface, REFIID riid, void **out)
54 TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
56 if (IsEqualGUID(riid, &IID_IUnknown) || IsEqualGUID(riid, &IID_ID3DXSkinInfo))
58 IUnknown_AddRef(iface);
59 *out = 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 d3dx9_skin_info_AddRef(ID3DXSkinInfo *iface)
70 struct d3dx9_skin_info *skin = impl_from_ID3DXSkinInfo(iface);
71 ULONG refcount = InterlockedIncrement(&skin->ref);
73 TRACE("%p increasing refcount to %u.\n", skin, refcount);
75 return refcount;
78 static ULONG WINAPI d3dx9_skin_info_Release(ID3DXSkinInfo *iface)
80 struct d3dx9_skin_info *skin = impl_from_ID3DXSkinInfo(iface);
81 ULONG refcount = InterlockedDecrement(&skin->ref);
83 TRACE("%p decreasing refcount to %u.\n", skin, refcount);
85 if (!refcount)
87 DWORD i;
89 for (i = 0; i < skin->num_bones; ++i)
91 HeapFree(GetProcessHeap(), 0, skin->bones[i].name);
92 HeapFree(GetProcessHeap(), 0, skin->bones[i].vertices);
93 HeapFree(GetProcessHeap(), 0, skin->bones[i].weights);
95 HeapFree(GetProcessHeap(), 0, skin->bones);
96 HeapFree(GetProcessHeap(), 0, skin);
99 return refcount;
102 static HRESULT WINAPI d3dx9_skin_info_SetBoneInfluence(ID3DXSkinInfo *iface,
103 DWORD bone_num, DWORD num_influences, const DWORD *vertices, const float *weights)
105 struct d3dx9_skin_info *skin = impl_from_ID3DXSkinInfo(iface);
106 struct bone *bone;
107 DWORD *new_vertices = NULL;
108 FLOAT *new_weights = NULL;
110 TRACE("iface %p, bone_num %u, num_influences %u, vertices %p, weights %p.\n",
111 iface, bone_num, num_influences, vertices, weights);
113 if (bone_num >= skin->num_bones || !vertices || !weights)
114 return D3DERR_INVALIDCALL;
116 if (num_influences) {
117 new_vertices = HeapAlloc(GetProcessHeap(), 0, num_influences * sizeof(*vertices));
118 if (!new_vertices)
119 return E_OUTOFMEMORY;
120 new_weights = HeapAlloc(GetProcessHeap(), 0, num_influences * sizeof(*weights));
121 if (!new_weights) {
122 HeapFree(GetProcessHeap(), 0, new_vertices);
123 return E_OUTOFMEMORY;
125 memcpy(new_vertices, vertices, num_influences * sizeof(*vertices));
126 memcpy(new_weights, weights, num_influences * sizeof(*weights));
128 bone = &skin->bones[bone_num];
129 bone->num_influences = num_influences;
130 HeapFree(GetProcessHeap(), 0, bone->vertices);
131 HeapFree(GetProcessHeap(), 0, bone->weights);
132 bone->vertices = new_vertices;
133 bone->weights = new_weights;
135 return D3D_OK;
138 static HRESULT WINAPI d3dx9_skin_info_SetBoneVertexInfluence(ID3DXSkinInfo *iface,
139 DWORD bone_num, DWORD influence_num, float weight)
141 FIXME("iface %p, bone_num %u, influence_num %u, weight %.8e stub!\n",
142 iface, bone_num, influence_num, weight);
144 return E_NOTIMPL;
147 static DWORD WINAPI d3dx9_skin_info_GetNumBoneInfluences(ID3DXSkinInfo *iface, DWORD bone_num)
149 struct d3dx9_skin_info *skin = impl_from_ID3DXSkinInfo(iface);
151 TRACE("iface %p, bone_num %u.\n", iface, bone_num);
153 if (bone_num >= skin->num_bones)
154 return 0;
156 return skin->bones[bone_num].num_influences;
159 static HRESULT WINAPI d3dx9_skin_info_GetBoneInfluence(ID3DXSkinInfo *iface,
160 DWORD bone_num, DWORD *vertices, float *weights)
162 struct d3dx9_skin_info *skin = impl_from_ID3DXSkinInfo(iface);
163 struct bone *bone;
165 TRACE("iface %p, bone_num %u, vertices %p, weights %p.\n",
166 iface, bone_num, vertices, weights);
168 if (bone_num >= skin->num_bones || !vertices)
169 return D3DERR_INVALIDCALL;
171 bone = &skin->bones[bone_num];
172 if (!bone->num_influences)
173 return D3D_OK;
175 memcpy(vertices, bone->vertices, bone->num_influences * sizeof(*vertices));
176 if (weights)
177 memcpy(weights, bone->weights, bone->num_influences * sizeof(*weights));
179 return D3D_OK;
182 static HRESULT WINAPI d3dx9_skin_info_GetBoneVertexInfluence(ID3DXSkinInfo *iface,
183 DWORD bone_num, DWORD influence_num, float *weight, DWORD *vertex_num)
185 FIXME("iface %p, bone_num %u, influence_num %u, weight %p, vertex_num %p stub!\n",
186 iface, bone_num, influence_num, weight, vertex_num);
188 return E_NOTIMPL;
191 static HRESULT WINAPI d3dx9_skin_info_GetMaxVertexInfluences(ID3DXSkinInfo *iface, DWORD *max_vertex_influences)
193 FIXME("iface %p, max_vertex_influences %p stub!\n", iface, max_vertex_influences);
195 return E_NOTIMPL;
198 static DWORD WINAPI d3dx9_skin_info_GetNumBones(ID3DXSkinInfo *iface)
200 struct d3dx9_skin_info *skin = impl_from_ID3DXSkinInfo(iface);
202 TRACE("iface %p.\n", iface);
204 return skin->num_bones;
207 static HRESULT WINAPI d3dx9_skin_info_FindBoneVertexInfluenceIndex(ID3DXSkinInfo *iface,
208 DWORD bone_num, DWORD vertex_num, DWORD *influence_index)
210 FIXME("iface %p, bone_num %u, vertex_num %u, influence_index %p stub!\n",
211 iface, bone_num, vertex_num, influence_index);
213 return E_NOTIMPL;
216 static HRESULT WINAPI d3dx9_skin_info_GetMaxFaceInfluences(struct ID3DXSkinInfo *iface,
217 struct IDirect3DIndexBuffer9 *index_buffer, DWORD num_faces, DWORD *max_face_influences)
219 FIXME("iface %p, index_buffer %p, num_faces %u, max_face_influences %p stub!\n",
220 iface, index_buffer, num_faces, max_face_influences);
222 return E_NOTIMPL;
225 static HRESULT WINAPI d3dx9_skin_info_SetMinBoneInfluence(ID3DXSkinInfo *iface, float min_influence)
227 FIXME("iface %p, min_influence %.8e stub!\n", iface, min_influence);
229 return E_NOTIMPL;
232 static float WINAPI d3dx9_skin_info_GetMinBoneInfluence(ID3DXSkinInfo *iface)
234 FIXME("iface %p stub!\n", iface);
236 return 0.0f;
239 static HRESULT WINAPI d3dx9_skin_info_SetBoneName(ID3DXSkinInfo *iface, DWORD bone_idx, const char *name)
241 struct d3dx9_skin_info *skin = impl_from_ID3DXSkinInfo(iface);
242 char *new_name;
243 size_t size;
245 TRACE("iface %p, bone_idx %u, name %s.\n", iface, bone_idx, debugstr_a(name));
247 if (bone_idx >= skin->num_bones || !name)
248 return D3DERR_INVALIDCALL;
250 size = strlen(name) + 1;
251 new_name = HeapAlloc(GetProcessHeap(), 0, size);
252 if (!new_name)
253 return E_OUTOFMEMORY;
254 memcpy(new_name, name, size);
255 HeapFree(GetProcessHeap(), 0, skin->bones[bone_idx].name);
256 skin->bones[bone_idx].name = new_name;
258 return D3D_OK;
261 static const char * WINAPI d3dx9_skin_info_GetBoneName(ID3DXSkinInfo *iface, DWORD bone_idx)
263 struct d3dx9_skin_info *skin = impl_from_ID3DXSkinInfo(iface);
265 TRACE("iface %p, bone_idx %u.\n", iface, bone_idx);
267 if (bone_idx >= skin->num_bones)
268 return NULL;
270 return skin->bones[bone_idx].name;
273 static HRESULT WINAPI d3dx9_skin_info_SetBoneOffsetMatrix(ID3DXSkinInfo *iface,
274 DWORD bone_num, const D3DXMATRIX *bone_transform)
276 struct d3dx9_skin_info *skin = impl_from_ID3DXSkinInfo(iface);
278 TRACE("iface %p, bone_num %u, bone_transform %p.\n", iface, bone_num, bone_transform);
280 if (bone_num >= skin->num_bones || !bone_transform)
281 return D3DERR_INVALIDCALL;
283 skin->bones[bone_num].transform = *bone_transform;
284 return D3D_OK;
287 static D3DXMATRIX * WINAPI d3dx9_skin_info_GetBoneOffsetMatrix(ID3DXSkinInfo *iface, DWORD bone_num)
289 struct d3dx9_skin_info *skin = impl_from_ID3DXSkinInfo(iface);
291 TRACE("iface %p, bone_num %u.\n", iface, bone_num);
293 if (bone_num >= skin->num_bones)
294 return NULL;
296 return &skin->bones[bone_num].transform;
299 static HRESULT WINAPI d3dx9_skin_info_Clone(ID3DXSkinInfo *iface, ID3DXSkinInfo **skin_info)
301 FIXME("iface %p, skin_info %p stub!\n", iface, skin_info);
303 return E_NOTIMPL;
306 static HRESULT WINAPI d3dx9_skin_info_Remap(ID3DXSkinInfo *iface, DWORD num_vertices, DWORD *vertex_remap)
308 FIXME("iface %p, num_vertices %u, vertex_remap %p stub!\n", iface, num_vertices, vertex_remap);
310 return E_NOTIMPL;
313 static HRESULT WINAPI d3dx9_skin_info_SetFVF(ID3DXSkinInfo *iface, DWORD fvf)
315 HRESULT hr;
316 D3DVERTEXELEMENT9 declaration[MAX_FVF_DECL_SIZE];
318 TRACE("iface %p, fvf %#x.\n", iface, fvf);
320 hr = D3DXDeclaratorFromFVF(fvf, declaration);
321 if (FAILED(hr)) return hr;
323 return iface->lpVtbl->SetDeclaration(iface, declaration);
326 static HRESULT WINAPI d3dx9_skin_info_SetDeclaration(ID3DXSkinInfo *iface, const D3DVERTEXELEMENT9 *declaration)
328 struct d3dx9_skin_info *skin = impl_from_ID3DXSkinInfo(iface);
329 HRESULT hr;
330 int count;
332 TRACE("iface %p, declaration %p.\n", iface, declaration);
334 if (!declaration)
335 return D3DERR_INVALIDCALL;
336 for (count = 0; declaration[count].Stream != 0xff; count++) {
337 if (declaration[count].Stream != 0) {
338 WARN("Invalid vertex element %u; contains non-zero stream %u\n",
339 count, declaration[count].Stream);
340 return D3DERR_INVALIDCALL;
343 count++;
345 memcpy(skin->vertex_declaration, declaration, count * sizeof(*declaration));
347 if (FAILED(hr = D3DXFVFFromDeclarator(skin->vertex_declaration, &skin->fvf)))
348 skin->fvf = 0;
350 return D3D_OK;
353 static DWORD WINAPI d3dx9_skin_info_GetFVF(ID3DXSkinInfo *iface)
355 struct d3dx9_skin_info *skin = impl_from_ID3DXSkinInfo(iface);
357 TRACE("iface %p.\n", iface);
359 return skin->fvf;
362 static HRESULT WINAPI d3dx9_skin_info_GetDeclaration(ID3DXSkinInfo *iface,
363 D3DVERTEXELEMENT9 declaration[MAX_FVF_DECL_SIZE])
365 struct d3dx9_skin_info *skin = impl_from_ID3DXSkinInfo(iface);
366 UINT count = 0;
368 TRACE("iface %p, declaration %p.\n", iface, declaration);
370 while (skin->vertex_declaration[count++].Stream != 0xff);
371 memcpy(declaration, skin->vertex_declaration, count * sizeof(declaration[0]));
372 return D3D_OK;
375 static HRESULT WINAPI d3dx9_skin_info_UpdateSkinnedMesh(ID3DXSkinInfo *iface, const D3DXMATRIX *bone_transforms,
376 const D3DXMATRIX *bone_inv_transpose_transforms, const void *src_vertices, void *dst_vertices)
378 FIXME("iface %p, bone_transforms %p, bone_inv_transpose_transforms %p, src_vertices %p, dst_vertices %p stub!\n",
379 iface, bone_transforms, bone_inv_transpose_transforms, src_vertices, dst_vertices);
381 return E_NOTIMPL;
384 static HRESULT WINAPI d3dx9_skin_info_ConvertToBlendedMesh(ID3DXSkinInfo *iface, ID3DXMesh *mesh_in,
385 DWORD options, const DWORD *adjacency_in, DWORD *adjacency_out, DWORD *face_remap,
386 ID3DXBuffer **vertex_remap, DWORD *max_face_infl, DWORD *num_bone_combinations,
387 ID3DXBuffer **bone_combination_table, ID3DXMesh **mesh_out)
389 FIXME("iface %p, mesh_in %p, options %#x, adjacency_in %p, adjacency_out %p, face_remap %p, vertex_remap %p, "
390 "max_face_infl %p, num_bone_combinations %p, bone_combination_table %p, mesh_out %p stub!\n",
391 iface, mesh_in, options, adjacency_in, adjacency_out, face_remap, vertex_remap,
392 max_face_infl, num_bone_combinations, bone_combination_table, mesh_out);
394 return E_NOTIMPL;
397 static HRESULT WINAPI d3dx9_skin_info_ConvertToIndexedBlendedMesh(ID3DXSkinInfo *iface, ID3DXMesh *mesh_in,
398 DWORD options, const DWORD *adjacency_in, DWORD *adjacency_out, DWORD *face_remap,
399 ID3DXBuffer **vertex_remap, DWORD *max_face_infl, DWORD *num_bone_combinations,
400 ID3DXBuffer **bone_combination_table, ID3DXMesh **mesh_out)
402 FIXME("iface %p, mesh_in %p, options %#x, adjacency_in %p, adjacency_out %p, face_remap %p, vertex_remap %p, "
403 "max_face_infl %p, num_bone_combinations %p, bone_combination_table %p, mesh_out %p stub!\n",
404 iface, mesh_in, options, adjacency_in, adjacency_out, face_remap, vertex_remap,
405 max_face_infl, num_bone_combinations, bone_combination_table, mesh_out);
407 return E_NOTIMPL;
410 static const struct ID3DXSkinInfoVtbl d3dx9_skin_info_vtbl =
412 d3dx9_skin_info_QueryInterface,
413 d3dx9_skin_info_AddRef,
414 d3dx9_skin_info_Release,
415 d3dx9_skin_info_SetBoneInfluence,
416 d3dx9_skin_info_SetBoneVertexInfluence,
417 d3dx9_skin_info_GetNumBoneInfluences,
418 d3dx9_skin_info_GetBoneInfluence,
419 d3dx9_skin_info_GetBoneVertexInfluence,
420 d3dx9_skin_info_GetMaxVertexInfluences,
421 d3dx9_skin_info_GetNumBones,
422 d3dx9_skin_info_FindBoneVertexInfluenceIndex,
423 d3dx9_skin_info_GetMaxFaceInfluences,
424 d3dx9_skin_info_SetMinBoneInfluence,
425 d3dx9_skin_info_GetMinBoneInfluence,
426 d3dx9_skin_info_SetBoneName,
427 d3dx9_skin_info_GetBoneName,
428 d3dx9_skin_info_SetBoneOffsetMatrix,
429 d3dx9_skin_info_GetBoneOffsetMatrix,
430 d3dx9_skin_info_Clone,
431 d3dx9_skin_info_Remap,
432 d3dx9_skin_info_SetFVF,
433 d3dx9_skin_info_SetDeclaration,
434 d3dx9_skin_info_GetFVF,
435 d3dx9_skin_info_GetDeclaration,
436 d3dx9_skin_info_UpdateSkinnedMesh,
437 d3dx9_skin_info_ConvertToBlendedMesh,
438 d3dx9_skin_info_ConvertToIndexedBlendedMesh,
441 HRESULT WINAPI D3DXCreateSkinInfo(DWORD num_vertices, const D3DVERTEXELEMENT9 *declaration,
442 DWORD num_bones, ID3DXSkinInfo **skin_info)
444 HRESULT hr;
445 static const D3DVERTEXELEMENT9 empty_declaration = D3DDECL_END();
446 struct d3dx9_skin_info *object = NULL;
448 TRACE("num_vertices %u, declaration %p, num_bones %u, skin_info %p.\n",
449 num_vertices, declaration, num_bones, skin_info);
451 if (!skin_info || !declaration)
452 return D3DERR_INVALIDCALL;
454 object = HeapAlloc(GetProcessHeap(), 0, sizeof(*object));
455 if (!object)
456 return E_OUTOFMEMORY;
458 object->ID3DXSkinInfo_iface.lpVtbl = &d3dx9_skin_info_vtbl;
459 object->ref = 1;
460 object->num_vertices = num_vertices;
461 object->num_bones = num_bones;
462 object->vertex_declaration[0] = empty_declaration;
463 object->fvf = 0;
465 object->bones = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, num_bones * sizeof(*object->bones));
466 if (!object->bones) {
467 hr = E_OUTOFMEMORY;
468 goto error;
471 if (FAILED(hr = d3dx9_skin_info_SetDeclaration(&object->ID3DXSkinInfo_iface, declaration)))
472 goto error;
474 *skin_info = &object->ID3DXSkinInfo_iface;
476 return D3D_OK;
477 error:
478 HeapFree(GetProcessHeap(), 0, object->bones);
479 HeapFree(GetProcessHeap(), 0, object);
480 return hr;
483 HRESULT WINAPI D3DXCreateSkinInfoFVF(DWORD num_vertices, DWORD fvf, DWORD num_bones, ID3DXSkinInfo **skin_info)
485 HRESULT hr;
486 D3DVERTEXELEMENT9 declaration[MAX_FVF_DECL_SIZE];
488 TRACE("(%u, %x, %u, %p)\n", num_vertices, fvf, num_bones, skin_info);
490 hr = D3DXDeclaratorFromFVF(fvf, declaration);
491 if (FAILED(hr))
492 return hr;
494 return D3DXCreateSkinInfo(num_vertices, declaration, num_bones, skin_info);