winhttp/tests: Make sure proxy settings are restored.
[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     char *name;
29     D3DXMATRIX transform;
30     DWORD num_influences;
31     DWORD *vertices;
32     FLOAT *weights;
33 };
34
35 typedef struct ID3DXSkinInfoImpl
36 {
37     ID3DXSkinInfo ID3DXSkinInfo_iface;
38     LONG ref;
39
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;
46
47 static inline struct ID3DXSkinInfoImpl *impl_from_ID3DXSkinInfo(ID3DXSkinInfo *iface)
48 {
49     return CONTAINING_RECORD(iface, struct ID3DXSkinInfoImpl, ID3DXSkinInfo_iface);
50 }
51
52 static HRESULT WINAPI ID3DXSkinInfoImpl_QueryInterface(ID3DXSkinInfo *iface, REFIID riid, void **ppobj)
53 {
54     TRACE("(%p, %s, %p)\n", iface, debugstr_guid(riid), ppobj);
55
56     if (IsEqualGUID(riid, &IID_IUnknown) || IsEqualGUID(riid, &IID_ID3DXSkinInfo))
57     {
58         IUnknown_AddRef(iface);
59         *ppobj = iface;
60         return D3D_OK;
61     }
62
63     WARN("%s not implemented, returning E_NOINTERFACE\n", debugstr_guid(riid));
64
65     return E_NOINTERFACE;
66 }
67
68 static ULONG WINAPI ID3DXSkinInfoImpl_AddRef(ID3DXSkinInfo *iface)
69 {
70     struct ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
71     ULONG ref = InterlockedIncrement(&This->ref);
72
73     TRACE("%p increasing refcount to %u\n", This, ref);
74
75     return ref;
76 }
77
78 static ULONG WINAPI ID3DXSkinInfoImpl_Release(ID3DXSkinInfo *iface)
79 {
80     struct ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
81     ULONG ref = InterlockedDecrement(&This->ref);
82
83     TRACE("%p decreasing refcount to %u\n", This, ref);
84
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);
91         }
92         HeapFree(GetProcessHeap(), 0, This->bones);
93         HeapFree(GetProcessHeap(), 0, This);
94     }
95
96     return ref;
97 }
98
99 static HRESULT WINAPI ID3DXSkinInfoImpl_SetBoneInfluence(ID3DXSkinInfo *iface, DWORD bone_num, DWORD num_influences, CONST DWORD *vertices, CONST FLOAT *weights)
100 {
101     ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
102     struct bone *bone;
103     DWORD *new_vertices = NULL;
104     FLOAT *new_weights = NULL;
105
106     TRACE("(%p, %u, %u, %p, %p)\n", This, bone_num, num_influences, vertices, weights);
107
108     if (bone_num >= This->num_bones || !vertices || !weights)
109         return D3DERR_INVALIDCALL;
110
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;
119         }
120         memcpy(new_vertices, vertices, num_influences * sizeof(*vertices));
121         memcpy(new_weights, weights, num_influences * sizeof(*weights));
122     }
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;
129
130     return D3D_OK;
131 }
132
133 static HRESULT WINAPI ID3DXSkinInfoImpl_SetBoneVertexInfluence(ID3DXSkinInfo *iface, DWORD bone_num, DWORD influence_num, float weight)
134 {
135     ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
136
137     FIXME("(%p, %u, %u, %g): stub\n", This, bone_num, influence_num, weight);
138
139     return E_NOTIMPL;
140 }
141
142 static DWORD WINAPI ID3DXSkinInfoImpl_GetNumBoneInfluences(ID3DXSkinInfo *iface, DWORD bone_num)
143 {
144     ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
145
146     TRACE("(%p, %u)\n", This, bone_num);
147
148     if (bone_num >= This->num_bones)
149         return 0;
150
151     return This->bones[bone_num].num_influences;
152 }
153
154 static HRESULT WINAPI ID3DXSkinInfoImpl_GetBoneInfluence(ID3DXSkinInfo *iface, DWORD bone_num, DWORD *vertices, FLOAT *weights)
155 {
156     ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
157     struct bone *bone;
158
159     TRACE("(%p, %u, %p, %p)\n", This, bone_num, vertices, weights);
160
161     if (bone_num >= This->num_bones || !vertices)
162         return D3DERR_INVALIDCALL;
163
164     bone = &This->bones[bone_num];
165     if (!bone->num_influences)
166         return D3D_OK;
167
168     memcpy(vertices, bone->vertices, bone->num_influences * sizeof(*vertices));
169     if (weights)
170         memcpy(weights, bone->weights, bone->num_influences * sizeof(*weights));
171
172     return D3D_OK;
173 }
174
175 static HRESULT WINAPI ID3DXSkinInfoImpl_GetBoneVertexInfluence(ID3DXSkinInfo *iface, DWORD bone_num,
176         DWORD influence_num, float *weight, DWORD *vertex_num)
177 {
178     ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
179
180     FIXME("(%p, %u, %u, %p, %p): stub\n", This, bone_num, influence_num, weight, vertex_num);
181
182     return E_NOTIMPL;
183 }
184
185 static HRESULT WINAPI ID3DXSkinInfoImpl_GetMaxVertexInfluences(ID3DXSkinInfo *iface, DWORD *max_vertex_influences)
186 {
187     ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
188
189     FIXME("(%p, %p): stub\n", This, max_vertex_influences);
190
191     return E_NOTIMPL;
192 }
193
194 static DWORD WINAPI ID3DXSkinInfoImpl_GetNumBones(ID3DXSkinInfo *iface)
195 {
196     ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
197
198     TRACE("(%p)\n", This);
199
200     return This->num_bones;
201 }
202
203 static HRESULT WINAPI ID3DXSkinInfoImpl_FindBoneVertexInfluenceIndex(ID3DXSkinInfo *iface, DWORD bone_num,
204         DWORD vertex_num, DWORD *influence_index)
205 {
206     ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
207
208     FIXME("(%p, %u, %u, %p): stub\n", This, bone_num, vertex_num, influence_index);
209
210     return E_NOTIMPL;
211 }
212
213 static HRESULT WINAPI ID3DXSkinInfoImpl_GetMaxFaceInfluences(struct ID3DXSkinInfo *iface,
214         struct IDirect3DIndexBuffer9 *index_buffer, DWORD num_faces, DWORD *max_face_influences)
215 {
216     ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
217
218     FIXME("(%p, %p, %u, %p): stub\n", This, index_buffer, num_faces, max_face_influences);
219
220     return E_NOTIMPL;
221 }
222
223 static HRESULT WINAPI ID3DXSkinInfoImpl_SetMinBoneInfluence(ID3DXSkinInfo *iface, FLOAT min_influence)
224 {
225     ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
226
227     FIXME("(%p, %g): stub\n", This, min_influence);
228
229     return E_NOTIMPL;
230 }
231
232 static FLOAT WINAPI ID3DXSkinInfoImpl_GetMinBoneInfluence(ID3DXSkinInfo *iface)
233 {
234     ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
235
236     FIXME("(%p): stub\n", This);
237
238     return 0.0f;
239 }
240
241 static HRESULT WINAPI ID3DXSkinInfoImpl_SetBoneName(ID3DXSkinInfo *iface, DWORD bone_num, LPCSTR name)
242 {
243     ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
244     char *new_name;
245     size_t size;
246
247     TRACE("(%p, %u, %s)\n", This, bone_num, debugstr_a(name));
248
249     if (bone_num >= This->num_bones || !name)
250         return D3DERR_INVALIDCALL;
251
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;
259
260     return D3D_OK;
261 }
262
263 static LPCSTR WINAPI ID3DXSkinInfoImpl_GetBoneName(ID3DXSkinInfo *iface, DWORD bone_num)
264 {
265     ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
266
267     TRACE("(%p, %u)\n", This, bone_num);
268
269     if (bone_num >= This->num_bones)
270         return NULL;
271
272     return This->bones[bone_num].name;
273 }
274
275 static HRESULT WINAPI ID3DXSkinInfoImpl_SetBoneOffsetMatrix(ID3DXSkinInfo *iface, DWORD bone_num, CONST D3DXMATRIX *bone_transform)
276 {
277     ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
278
279     TRACE("(%p, %u, %p)\n", This, bone_num, bone_transform);
280
281     if (bone_num >= This->num_bones || !bone_transform)
282         return D3DERR_INVALIDCALL;
283
284     This->bones[bone_num].transform = *bone_transform;
285     return D3D_OK;
286 }
287
288 static LPD3DXMATRIX WINAPI ID3DXSkinInfoImpl_GetBoneOffsetMatrix(ID3DXSkinInfo *iface, DWORD bone_num)
289 {
290     ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
291
292     TRACE("(%p, %u)\n", This, bone_num);
293
294     if (bone_num >= This->num_bones)
295         return NULL;
296
297     return &This->bones[bone_num].transform;
298 }
299
300 static HRESULT WINAPI ID3DXSkinInfoImpl_Clone(ID3DXSkinInfo *iface, ID3DXSkinInfo **skin_info)
301 {
302     ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
303
304     FIXME("(%p, %p): stub\n", This, skin_info);
305
306     return E_NOTIMPL;
307 }
308
309 static HRESULT WINAPI ID3DXSkinInfoImpl_Remap(ID3DXSkinInfo *iface, DWORD num_vertices, DWORD *vertex_remap)
310 {
311     ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
312
313     FIXME("(%p, %u, %p): stub\n", This, num_vertices, vertex_remap);
314
315     return E_NOTIMPL;
316 }
317
318 static HRESULT WINAPI ID3DXSkinInfoImpl_SetFVF(ID3DXSkinInfo *iface, DWORD fvf)
319 {
320     ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
321     HRESULT hr;
322     D3DVERTEXELEMENT9 declaration[MAX_FVF_DECL_SIZE];
323
324     TRACE("(%p, %x)\n", This, fvf);
325
326     hr = D3DXDeclaratorFromFVF(fvf, declaration);
327     if (FAILED(hr)) return hr;
328
329     return iface->lpVtbl->SetDeclaration(iface, declaration);
330 }
331
332 static HRESULT WINAPI ID3DXSkinInfoImpl_SetDeclaration(ID3DXSkinInfo *iface, CONST D3DVERTEXELEMENT9 *declaration)
333 {
334     ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
335     HRESULT hr;
336     int count;
337
338     TRACE("(%p, %p)\n", This, declaration);
339
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;
347         }
348     }
349     count++;
350
351     memcpy(This->vertex_declaration, declaration, count * sizeof(*declaration));
352
353     hr = D3DXFVFFromDeclarator(This->vertex_declaration, &This->fvf);
354     if (FAILED(hr))
355         This->fvf = 0;
356
357     return D3D_OK;
358 }
359
360 static DWORD WINAPI ID3DXSkinInfoImpl_GetFVF(ID3DXSkinInfo *iface)
361 {
362     ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
363
364     TRACE("(%p)\n", This);
365
366     return This->fvf;
367 }
368
369 static HRESULT WINAPI ID3DXSkinInfoImpl_GetDeclaration(ID3DXSkinInfo *iface, D3DVERTEXELEMENT9 declaration[MAX_FVF_DECL_SIZE])
370 {
371     ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
372     UINT count = 0;
373
374     TRACE("(%p)\n", This);
375
376     while (This->vertex_declaration[count++].Stream != 0xff);
377     memcpy(declaration, This->vertex_declaration, count * sizeof(declaration[0]));
378     return D3D_OK;
379 }
380
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)
383 {
384     ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
385
386     FIXME("(%p, %p, %p, %p, %p): stub\n", This, bone_transforms, bone_inv_transpose_transforms, vertices_src, vertices_dest);
387
388     return E_NOTIMPL;
389 }
390
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)
395 {
396     ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
397
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);
401
402     return E_NOTIMPL;
403 }
404
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)
409 {
410     ID3DXSkinInfoImpl *This = impl_from_ID3DXSkinInfo(iface);
411
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);
415
416     return E_NOTIMPL;
417 }
418
419 static const struct ID3DXSkinInfoVtbl ID3DXSkinInfoImpl_Vtbl =
420 {
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
450 };
451
452 HRESULT WINAPI D3DXCreateSkinInfo(DWORD num_vertices, const D3DVERTEXELEMENT9 *declaration,
453         DWORD num_bones, ID3DXSkinInfo **skin_info)
454 {
455     HRESULT hr;
456     ID3DXSkinInfoImpl *object = NULL;
457     static const D3DVERTEXELEMENT9 empty_declaration = D3DDECL_END();
458
459     TRACE("(%u, %p, %u, %p)\n", num_vertices, declaration, num_bones, skin_info);
460
461     if (!skin_info || !declaration)
462         return D3DERR_INVALIDCALL;
463
464     object = HeapAlloc(GetProcessHeap(), 0, sizeof(*object));
465     if (!object)
466         return E_OUTOFMEMORY;
467
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;
474
475     object->bones = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, num_bones * sizeof(*object->bones));
476     if (!object->bones) {
477         hr = E_OUTOFMEMORY;
478         goto error;
479     }
480
481     hr = ID3DXSkinInfoImpl_SetDeclaration(&object->ID3DXSkinInfo_iface, declaration);
482     if (FAILED(hr)) goto error;
483
484     *skin_info = &object->ID3DXSkinInfo_iface;
485
486     return D3D_OK;
487 error:
488     HeapFree(GetProcessHeap(), 0, object->bones);
489     HeapFree(GetProcessHeap(), 0, object);
490     return hr;
491 }
492
493 HRESULT WINAPI D3DXCreateSkinInfoFVF(DWORD num_vertices, DWORD fvf, DWORD num_bones, ID3DXSkinInfo **skin_info)
494 {
495     HRESULT hr;
496     D3DVERTEXELEMENT9 declaration[MAX_FVF_DECL_SIZE];
497
498     TRACE("(%u, %x, %u, %p)\n", num_vertices, fvf, num_bones, skin_info);
499
500     hr = D3DXDeclaratorFromFVF(fvf, declaration);
501     if (FAILED(hr))
502         return hr;
503
504     return D3DXCreateSkinInfo(num_vertices, declaration, num_bones, skin_info);
505 }