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