[AMSTREAM] We don't need to define WIDL_C_INLINE_WRAPPERS here anymore.
[reactos.git] / dll / directx / wine / d3dx9_36 / mesh.c
index ba97552..2f9615e 100644 (file)
 #include "dxfile.h"
 #include "rmxfguid.h"
 #include "rmxftmpl.h"
-#include "wine/list.h"
 
-#define fmax(a, b) ((a) > (b) ? (a) : (b))
+#include "wine/list.h"
 
-typedef struct ID3DXMeshImpl
+struct d3dx9_mesh
 {
     ID3DXMesh ID3DXMesh_iface;
     LONG ref;
@@ -59,9 +58,9 @@ typedef struct ID3DXMeshImpl
     int attrib_buffer_lock_count;
     DWORD attrib_table_size;
     D3DXATTRIBUTERANGE *attrib_table;
-} ID3DXMeshImpl;
+};
 
-const UINT d3dx_decltype_size[] =
+static const UINT d3dx_decltype_size[] =
 {
    /* D3DDECLTYPE_FLOAT1    */ sizeof(FLOAT),
    /* D3DDECLTYPE_FLOAT2    */ sizeof(D3DXVECTOR2),
@@ -82,21 +81,21 @@ const UINT d3dx_decltype_size[] =
    /* D3DDECLTYPE_FLOAT16_4 */ 4 * sizeof(D3DXFLOAT16),
 };
 
-static inline ID3DXMeshImpl *impl_from_ID3DXMesh(ID3DXMesh *iface)
+static inline struct d3dx9_mesh *impl_from_ID3DXMesh(ID3DXMesh *iface)
 {
-    return CONTAINING_RECORD(iface, ID3DXMeshImpl, ID3DXMesh_iface);
+    return CONTAINING_RECORD(iface, struct d3dx9_mesh, ID3DXMesh_iface);
 }
 
-static HRESULT WINAPI ID3DXMeshImpl_QueryInterface(ID3DXMesh *iface, REFIID riid, LPVOID *object)
+static HRESULT WINAPI d3dx9_mesh_QueryInterface(ID3DXMesh *iface, REFIID riid, void **out)
 {
-    TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), object);
+    TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
 
     if (IsEqualGUID(riid, &IID_IUnknown) ||
         IsEqualGUID(riid, &IID_ID3DXBaseMesh) ||
         IsEqualGUID(riid, &IID_ID3DXMesh))
     {
         iface->lpVtbl->AddRef(iface);
-        *object = iface;
+        *out = iface;
         return S_OK;
     }
 
@@ -105,47 +104,47 @@ static HRESULT WINAPI ID3DXMeshImpl_QueryInterface(ID3DXMesh *iface, REFIID riid
     return E_NOINTERFACE;
 }
 
-static ULONG WINAPI ID3DXMeshImpl_AddRef(ID3DXMesh *iface)
+static ULONG WINAPI d3dx9_mesh_AddRef(ID3DXMesh *iface)
 {
-    ID3DXMeshImpl *This = impl_from_ID3DXMesh(iface);
+    struct d3dx9_mesh *mesh = impl_from_ID3DXMesh(iface);
+    ULONG refcount = InterlockedIncrement(&mesh->ref);
 
-    TRACE("(%p)->(): AddRef from %d\n", This, This->ref);
+    TRACE("%p increasing refcount to %u.\n", mesh, refcount);
 
-    return InterlockedIncrement(&This->ref);
+    return refcount;
 }
 
-static ULONG WINAPI ID3DXMeshImpl_Release(ID3DXMesh *iface)
+static ULONG WINAPI d3dx9_mesh_Release(ID3DXMesh *iface)
 {
-    ID3DXMeshImpl *This = impl_from_ID3DXMesh(iface);
-    ULONG ref = InterlockedDecrement(&This->ref);
+    struct d3dx9_mesh *mesh = impl_from_ID3DXMesh(iface);
+    ULONG refcount = InterlockedDecrement(&mesh->ref);
 
-    TRACE("(%p)->(): Release from %d\n", This, ref + 1);
+    TRACE("%p decreasing refcount to %u.\n", mesh, refcount);
 
-    if (!ref)
+    if (!refcount)
     {
-        IDirect3DIndexBuffer9_Release(This->index_buffer);
-        IDirect3DVertexBuffer9_Release(This->vertex_buffer);
-        if (This->vertex_declaration)
-            IDirect3DVertexDeclaration9_Release(This->vertex_declaration);
-        IDirect3DDevice9_Release(This->device);
-        HeapFree(GetProcessHeap(), 0, This->attrib_buffer);
-        HeapFree(GetProcessHeap(), 0, This->attrib_table);
-        HeapFree(GetProcessHeap(), 0, This);
+        IDirect3DIndexBuffer9_Release(mesh->index_buffer);
+        IDirect3DVertexBuffer9_Release(mesh->vertex_buffer);
+        if (mesh->vertex_declaration)
+            IDirect3DVertexDeclaration9_Release(mesh->vertex_declaration);
+        IDirect3DDevice9_Release(mesh->device);
+        HeapFree(GetProcessHeap(), 0, mesh->attrib_buffer);
+        HeapFree(GetProcessHeap(), 0, mesh->attrib_table);
+        HeapFree(GetProcessHeap(), 0, mesh);
     }
 
-    return ref;
+    return refcount;
 }
 
-/*** ID3DXBaseMesh ***/
-static HRESULT WINAPI ID3DXMeshImpl_DrawSubset(ID3DXMesh *iface, DWORD attrib_id)
+static HRESULT WINAPI d3dx9_mesh_DrawSubset(ID3DXMesh *iface, DWORD attrib_id)
 {
-    ID3DXMeshImpl *This = impl_from_ID3DXMesh(iface);
+    struct d3dx9_mesh *This = impl_from_ID3DXMesh(iface);
     HRESULT hr;
     DWORD face_start;
     DWORD face_end = 0;
     DWORD vertex_size;
 
-    TRACE("(%p)->(%u)\n", This, attrib_id);
+    TRACE("iface %p, attrib_id %u.\n", iface, attrib_id);
 
     if (!This->vertex_declaration)
     {
@@ -185,31 +184,31 @@ static HRESULT WINAPI ID3DXMeshImpl_DrawSubset(ID3DXMesh *iface, DWORD attrib_id
     return D3D_OK;
 }
 
-static DWORD WINAPI ID3DXMeshImpl_GetNumFaces(ID3DXMesh *iface)
+static DWORD WINAPI d3dx9_mesh_GetNumFaces(ID3DXMesh *iface)
 {
-    ID3DXMeshImpl *This = impl_from_ID3DXMesh(iface);
+    struct d3dx9_mesh *mesh = impl_from_ID3DXMesh(iface);
 
-    TRACE("(%p)\n", This);
+    TRACE("iface %p.\n", iface);
 
-    return This->numfaces;
+    return mesh->numfaces;
 }
 
-static DWORD WINAPI ID3DXMeshImpl_GetNumVertices(ID3DXMesh *iface)
+static DWORD WINAPI d3dx9_mesh_GetNumVertices(ID3DXMesh *iface)
 {
-    ID3DXMeshImpl *This = impl_from_ID3DXMesh(iface);
+    struct d3dx9_mesh *mesh = impl_from_ID3DXMesh(iface);
 
-    TRACE("(%p)\n", This);
+    TRACE("iface %p.\n", iface);
 
-    return This->numvertices;
+    return mesh->numvertices;
 }
 
-static DWORD WINAPI ID3DXMeshImpl_GetFVF(ID3DXMesh *iface)
+static DWORD WINAPI d3dx9_mesh_GetFVF(ID3DXMesh *iface)
 {
-    ID3DXMeshImpl *This = impl_from_ID3DXMesh(iface);
+    struct d3dx9_mesh *mesh = impl_from_ID3DXMesh(iface);
 
-    TRACE("(%p)\n", This);
+    TRACE("iface %p.\n", iface);
 
-    return This->fvf;
+    return mesh->fvf;
 }
 
 static void copy_declaration(D3DVERTEXELEMENT9 *dst, const D3DVERTEXELEMENT9 *src, UINT num_elem)
@@ -217,61 +216,63 @@ static void copy_declaration(D3DVERTEXELEMENT9 *dst, const D3DVERTEXELEMENT9 *sr
     memcpy(dst, src, num_elem * sizeof(*src));
 }
 
-static HRESULT WINAPI ID3DXMeshImpl_GetDeclaration(ID3DXMesh *iface, D3DVERTEXELEMENT9 declaration[MAX_FVF_DECL_SIZE])
+static HRESULT WINAPI d3dx9_mesh_GetDeclaration(ID3DXMesh *iface, D3DVERTEXELEMENT9 declaration[MAX_FVF_DECL_SIZE])
 {
-    ID3DXMeshImpl *This = impl_from_ID3DXMesh(iface);
+    struct d3dx9_mesh *mesh = impl_from_ID3DXMesh(iface);
 
-    TRACE("(%p)\n", This);
+    TRACE("iface %p, declaration %p.\n", iface, declaration);
 
-    if (declaration == NULL) return D3DERR_INVALIDCALL;
+    if (!declaration)
+        return D3DERR_INVALIDCALL;
 
-    copy_declaration(declaration, This->cached_declaration, This->num_elem);
+    copy_declaration(declaration, mesh->cached_declaration, mesh->num_elem);
 
     return D3D_OK;
 }
 
-static DWORD WINAPI ID3DXMeshImpl_GetNumBytesPerVertex(ID3DXMesh *iface)
+static DWORD WINAPI d3dx9_mesh_GetNumBytesPerVertex(ID3DXMesh *iface)
 {
-    ID3DXMeshImpl *This = impl_from_ID3DXMesh(iface);
+    struct d3dx9_mesh *mesh = impl_from_ID3DXMesh(iface);
 
-    TRACE("iface (%p)\n", This);
+    TRACE("iface %p.\n", iface);
 
-    return This->vertex_declaration_size;
+    return mesh->vertex_declaration_size;
 }
 
-static DWORD WINAPI ID3DXMeshImpl_GetOptions(ID3DXMesh *iface)
+static DWORD WINAPI d3dx9_mesh_GetOptions(ID3DXMesh *iface)
 {
-    ID3DXMeshImpl *This = impl_from_ID3DXMesh(iface);
+    struct d3dx9_mesh *mesh = impl_from_ID3DXMesh(iface);
 
-    TRACE("(%p)\n", This);
+    TRACE("iface %p.\n", iface);
 
-    return This->options;
+    return mesh->options;
 }
 
-static HRESULT WINAPI ID3DXMeshImpl_GetDevice(struct ID3DXMesh *iface, struct IDirect3DDevice9 **device)
+static HRESULT WINAPI d3dx9_mesh_GetDevice(struct ID3DXMesh *iface, struct IDirect3DDevice9 **device)
 {
-    ID3DXMeshImpl *This = impl_from_ID3DXMesh(iface);
+    struct d3dx9_mesh *mesh = impl_from_ID3DXMesh(iface);
 
-    TRACE("(%p)->(%p)\n", This, device);
+    TRACE("iface %p, device %p.\n", iface, device);
 
-    if (device == NULL) return D3DERR_INVALIDCALL;
-    *device = This->device;
-    IDirect3DDevice9_AddRef(This->device);
+    if (!device)
+        return D3DERR_INVALIDCALL;
+    *device = mesh->device;
+    IDirect3DDevice9_AddRef(mesh->device);
 
     return D3D_OK;
 }
 
-static HRESULT WINAPI ID3DXMeshImpl_CloneMeshFVF(struct ID3DXMesh *iface, DWORD options, DWORD fvf,
+static HRESULT WINAPI d3dx9_mesh_CloneMeshFVF(struct ID3DXMesh *iface, DWORD options, DWORD fvf,
         struct IDirect3DDevice9 *device, struct ID3DXMesh **clone_mesh)
 {
-    ID3DXMeshImpl *This = impl_from_ID3DXMesh(iface);
     HRESULT hr;
     D3DVERTEXELEMENT9 declaration[MAX_FVF_DECL_SIZE];
 
-    TRACE("(%p)->(%x,%x,%p,%p)\n", This, options, fvf, device, clone_mesh);
+    TRACE("iface %p, options %#x, fvf %#x, device %p, clone_mesh %p.\n",
+            iface, options, fvf, device, clone_mesh);
 
-    hr = D3DXDeclaratorFromFVF(fvf, declaration);
-    if (FAILED(hr)) return hr;
+    if (FAILED(hr = D3DXDeclaratorFromFVF(fvf, declaration)))
+        return hr;
 
     return iface->lpVtbl->CloneMesh(iface, options, declaration, device, clone_mesh);
 }
@@ -336,7 +337,7 @@ static INT simple_round(FLOAT value)
     return res;
 }
 
-static void convert_float4(BYTE *dst, CONST D3DXVECTOR4 *src, D3DDECLTYPE type_dst)
+static void convert_float4(BYTE *dst, const D3DXVECTOR4 *src, D3DDECLTYPE type_dst)
 {
     BOOL fixme_once = FALSE;
 
@@ -650,7 +651,7 @@ cleanup:
     return hr;
 }
 
-static BOOL declaration_equals(CONST D3DVERTEXELEMENT9 *declaration1, CONST D3DVERTEXELEMENT9 *declaration2)
+static BOOL declaration_equals(const D3DVERTEXELEMENT9 *declaration1, const D3DVERTEXELEMENT9 *declaration2)
 {
     UINT size1 = 0, size2 = 0;
 
@@ -669,11 +670,11 @@ static BOOL declaration_equals(CONST D3DVERTEXELEMENT9 *declaration1, CONST D3DV
     return FALSE;
 }
 
-static HRESULT WINAPI ID3DXMeshImpl_CloneMesh(struct ID3DXMesh *iface, DWORD options,
+static HRESULT WINAPI d3dx9_mesh_CloneMesh(struct ID3DXMesh *iface, DWORD options,
         const D3DVERTEXELEMENT9 *declaration, struct IDirect3DDevice9 *device, struct ID3DXMesh **clone_mesh_out)
 {
-    ID3DXMeshImpl *This = impl_from_ID3DXMesh(iface);
-    ID3DXMeshImpl *cloned_this;
+    struct d3dx9_mesh *This = impl_from_ID3DXMesh(iface);
+    struct d3dx9_mesh *cloned_this;
     ID3DXMesh *clone_mesh;
     D3DVERTEXELEMENT9 orig_declaration[MAX_FVF_DECL_SIZE] = { D3DDECL_END() };
     void *data_in, *data_out;
@@ -681,7 +682,8 @@ static HRESULT WINAPI ID3DXMeshImpl_CloneMesh(struct ID3DXMesh *iface, DWORD opt
     HRESULT hr;
     BOOL same_declaration;
 
-    TRACE("(%p)->(%x,%p,%p,%p)\n", This, options, declaration, device, clone_mesh_out);
+    TRACE("iface %p, options %#x, declaration %p, device %p, clone_mesh_out %p.\n",
+            iface, options, declaration, device, clone_mesh_out);
 
     if (!clone_mesh_out)
         return D3DERR_INVALIDCALL;
@@ -765,81 +767,87 @@ error:
     return hr;
 }
 
-static HRESULT WINAPI ID3DXMeshImpl_GetVertexBuffer(struct ID3DXMesh *iface,
+static HRESULT WINAPI d3dx9_mesh_GetVertexBuffer(struct ID3DXMesh *iface,
         struct IDirect3DVertexBuffer9 **vertex_buffer)
 {
-    ID3DXMeshImpl *This = impl_from_ID3DXMesh(iface);
+    struct d3dx9_mesh *mesh = impl_from_ID3DXMesh(iface);
 
-    TRACE("(%p)->(%p)\n", This, vertex_buffer);
+    TRACE("iface %p, vertex_buffer %p.\n", iface, vertex_buffer);
 
-    if (vertex_buffer == NULL) return D3DERR_INVALIDCALL;
-    *vertex_buffer = This->vertex_buffer;
-    IDirect3DVertexBuffer9_AddRef(This->vertex_buffer);
+    if (!vertex_buffer)
+        return D3DERR_INVALIDCALL;
+    *vertex_buffer = mesh->vertex_buffer;
+    IDirect3DVertexBuffer9_AddRef(mesh->vertex_buffer);
 
     return D3D_OK;
 }
 
-static HRESULT WINAPI ID3DXMeshImpl_GetIndexBuffer(struct ID3DXMesh *iface,
+static HRESULT WINAPI d3dx9_mesh_GetIndexBuffer(struct ID3DXMesh *iface,
         struct IDirect3DIndexBuffer9 **index_buffer)
 {
-    ID3DXMeshImpl *This = impl_from_ID3DXMesh(iface);
+    struct d3dx9_mesh *mesh = impl_from_ID3DXMesh(iface);
 
-    TRACE("(%p)->(%p)\n", This, index_buffer);
+    TRACE("iface %p, index_buffer %p.\n", iface, index_buffer);
 
-    if (index_buffer == NULL) return D3DERR_INVALIDCALL;
-    *index_buffer = This->index_buffer;
-    IDirect3DIndexBuffer9_AddRef(This->index_buffer);
+    if (!index_buffer)
+        return D3DERR_INVALIDCALL;
+    *index_buffer = mesh->index_buffer;
+    IDirect3DIndexBuffer9_AddRef(mesh->index_buffer);
 
     return D3D_OK;
 }
 
-static HRESULT WINAPI ID3DXMeshImpl_LockVertexBuffer(ID3DXMesh *iface, DWORD flags, LPVOID *data)
+static HRESULT WINAPI d3dx9_mesh_LockVertexBuffer(ID3DXMesh *iface, DWORD flags, void **data)
 {
-    ID3DXMeshImpl *This = impl_from_ID3DXMesh(iface);
+    struct d3dx9_mesh *mesh = impl_from_ID3DXMesh(iface);
 
-    TRACE("(%p)->(%u,%p)\n", This, flags, data);
+    TRACE("iface %p, flags %#x, data %p.\n", iface, flags, data);
 
-    return IDirect3DVertexBuffer9_Lock(This->vertex_buffer, 0, 0, data, flags);
+    return IDirect3DVertexBuffer9_Lock(mesh->vertex_buffer, 0, 0, data, flags);
 }
 
-static HRESULT WINAPI ID3DXMeshImpl_UnlockVertexBuffer(ID3DXMesh *iface)
+static HRESULT WINAPI d3dx9_mesh_UnlockVertexBuffer(ID3DXMesh *iface)
 {
-    ID3DXMeshImpl *This = impl_from_ID3DXMesh(iface);
+    struct d3dx9_mesh *mesh = impl_from_ID3DXMesh(iface);
 
-    TRACE("(%p)\n", This);
+    TRACE("iface %p.\n", iface);
 
-    return IDirect3DVertexBuffer9_Unlock(This->vertex_buffer);
+    return IDirect3DVertexBuffer9_Unlock(mesh->vertex_buffer);
 }
 
-static HRESULT WINAPI ID3DXMeshImpl_LockIndexBuffer(ID3DXMesh *iface, DWORD flags, LPVOID *data)
+static HRESULT WINAPI d3dx9_mesh_LockIndexBuffer(ID3DXMesh *iface, DWORD flags, void **data)
 {
-    ID3DXMeshImpl *This = impl_from_ID3DXMesh(iface);
+    struct d3dx9_mesh *mesh = impl_from_ID3DXMesh(iface);
 
-    TRACE("(%p)->(%u,%p)\n", This, flags, data);
+    TRACE("iface %p, flags %#x, data %p.\n", iface, flags, data);
 
-    return IDirect3DIndexBuffer9_Lock(This->index_buffer, 0, 0, data, flags);
+    return IDirect3DIndexBuffer9_Lock(mesh->index_buffer, 0, 0, data, flags);
 }
 
-static HRESULT WINAPI ID3DXMeshImpl_UnlockIndexBuffer(ID3DXMesh *iface)
+static HRESULT WINAPI d3dx9_mesh_UnlockIndexBuffer(ID3DXMesh *iface)
 {
-    ID3DXMeshImpl *This = impl_from_ID3DXMesh(iface);
+    struct d3dx9_mesh *mesh = impl_from_ID3DXMesh(iface);
 
-    TRACE("(%p)\n", This);
+    TRACE("iface %p.\n", iface);
 
-    return IDirect3DIndexBuffer9_Unlock(This->index_buffer);
+    return IDirect3DIndexBuffer9_Unlock(mesh->index_buffer);
 }
 
-static HRESULT WINAPI ID3DXMeshImpl_GetAttributeTable(ID3DXMesh *iface, D3DXATTRIBUTERANGE *attrib_table, DWORD *attrib_table_size)
+/* FIXME: This looks just wrong, we never check *attrib_table_size before
+ * copying the data. */
+static HRESULT WINAPI d3dx9_mesh_GetAttributeTable(ID3DXMesh *iface,
+        D3DXATTRIBUTERANGE *attrib_table, DWORD *attrib_table_size)
 {
-    ID3DXMeshImpl *This = impl_from_ID3DXMesh(iface);
+    struct d3dx9_mesh *mesh = impl_from_ID3DXMesh(iface);
 
-    TRACE("(%p)->(%p,%p)\n", This, attrib_table, attrib_table_size);
+    TRACE("iface %p, attrib_table %p, attrib_table_size %p.\n",
+            iface, attrib_table, attrib_table_size);
 
     if (attrib_table_size)
-        *attrib_table_size = This->attrib_table_size;
+        *attrib_table_size = mesh->attrib_table_size;
 
     if (attrib_table)
-        CopyMemory(attrib_table, This->attrib_table, This->attrib_table_size * sizeof(*attrib_table));
+        memcpy(attrib_table, mesh->attrib_table, mesh->attrib_table_size * sizeof(*attrib_table));
 
     return D3D_OK;
 }
@@ -867,7 +875,8 @@ struct edge_face_map
  * is at most one edge to face mapping, i.e. an edge can only belong to one
  * face.
  */
-static HRESULT init_edge_face_map(struct edge_face_map *edge_face_map, CONST DWORD *index_buffer, CONST DWORD *point_reps, CONST DWORD num_faces)
+static HRESULT init_edge_face_map(struct edge_face_map *edge_face_map, const DWORD *index_buffer,
+        const DWORD *point_reps, DWORD num_faces)
 {
     DWORD face, edge;
     DWORD i;
@@ -907,7 +916,7 @@ static HRESULT init_edge_face_map(struct edge_face_map *edge_face_map, CONST DWO
     return D3D_OK;
 }
 
-static DWORD find_adjacent_face(struct edge_face_map *edge_face_map, DWORD vertex1, DWORD vertex2, CONST DWORD num_faces)
+static DWORD find_adjacent_face(struct edge_face_map *edge_face_map, DWORD vertex1, DWORD vertex2, DWORD num_faces)
 {
     struct edge_face *edge_face_ptr;
 
@@ -937,9 +946,9 @@ static DWORD *generate_identity_point_reps(DWORD num_vertices)
         return id_point_reps;
 }
 
-static HRESULT WINAPI ID3DXMeshImpl_ConvertPointRepsToAdjacency(ID3DXMesh *iface, CONST DWORD *point_reps, DWORD *adjacency)
+static HRESULT WINAPI d3dx9_mesh_ConvertPointRepsToAdjacency(ID3DXMesh *iface,
+        const DWORD *point_reps, DWORD *adjacency)
 {
-    ID3DXMeshImpl *This = impl_from_ID3DXMesh(iface);
     HRESULT hr;
     DWORD num_faces = iface->lpVtbl->GetNumFaces(iface);
     DWORD num_vertices = iface->lpVtbl->GetNumVertices(iface);
@@ -950,10 +959,10 @@ static HRESULT WINAPI ID3DXMeshImpl_ConvertPointRepsToAdjacency(ID3DXMesh *iface
     DWORD face;
     DWORD edge;
     struct edge_face_map edge_face_map = {0};
-    CONST DWORD *point_reps_ptr = NULL;
+    const DWORD *point_reps_ptr = NULL;
     DWORD *id_point_reps = NULL;
 
-    TRACE("(%p)->(%p,%p)\n", This, point_reps, adjacency);
+    TRACE("iface %p, point_reps %p, adjacency %p.\n", iface, point_reps, adjacency);
 
     if (!adjacency) return D3DERR_INVALIDCALL;
 
@@ -1038,9 +1047,8 @@ cleanup:
  * vertex 5 is replaced by vertex 3 then index 5 would contain 3. If no vertex
  * replaces it, then it contains the same number as the index itself, e.g.
  * index 5 would contain 5. */
-static HRESULT propagate_face_vertices(CONST DWORD *adjacency, DWORD *point_reps,
-                                    CONST DWORD *indices, DWORD *new_indices,
-                                    CONST DWORD face, CONST DWORD numfaces)
+static HRESULT propagate_face_vertices(const DWORD *adjacency, DWORD *point_reps,
+        const DWORD *indices, DWORD *new_indices, DWORD face, DWORD numfaces)
 {
     const unsigned int VERTS_PER_FACE = 3;
     DWORD edge, opp_edge;
@@ -1088,8 +1096,10 @@ static HRESULT propagate_face_vertices(CONST DWORD *adjacency, DWORD *point_reps
     return D3D_OK;
 }
 
-static HRESULT WINAPI ID3DXMeshImpl_ConvertAdjacencyToPointReps(ID3DXMesh *iface, CONST DWORD *adjacency, DWORD *point_reps)
+static HRESULT WINAPI d3dx9_mesh_ConvertAdjacencyToPointReps(ID3DXMesh *iface,
+        const DWORD *adjacency, DWORD *point_reps)
 {
+    struct d3dx9_mesh *This = impl_from_ID3DXMesh(iface);
     HRESULT hr;
     DWORD face;
     DWORD i;
@@ -1098,9 +1108,7 @@ static HRESULT WINAPI ID3DXMeshImpl_ConvertAdjacencyToPointReps(ID3DXMesh *iface
     DWORD *new_indices = NULL;
     const unsigned int VERTS_PER_FACE = 3;
 
-    ID3DXMeshImpl *This = impl_from_ID3DXMesh(iface);
-
-    TRACE("(%p)->(%p,%p)\n", This, adjacency, point_reps);
+    TRACE("iface %p, adjacency %p, point_reps %p.\n", iface, adjacency, point_reps);
 
     if (!adjacency)
     {
@@ -1209,9 +1217,9 @@ static int compare_vertex_keys(const void *a, const void *b)
     return left->key < right->key ? -1 : 1;
 }
 
-static HRESULT WINAPI ID3DXMeshImpl_GenerateAdjacency(ID3DXMesh *iface, FLOAT epsilon, DWORD *adjacency)
+static HRESULT WINAPI d3dx9_mesh_GenerateAdjacency(ID3DXMesh *iface, float epsilon, DWORD *adjacency)
 {
-    ID3DXMeshImpl *This = impl_from_ID3DXMesh(iface);
+    struct d3dx9_mesh *This = impl_from_ID3DXMesh(iface);
     HRESULT hr;
     BYTE *vertices = NULL;
     const DWORD *indices = NULL;
@@ -1225,7 +1233,7 @@ static HRESULT WINAPI ID3DXMeshImpl_GenerateAdjacency(ID3DXMesh *iface, FLOAT ep
     const FLOAT epsilon_sq = epsilon * epsilon;
     DWORD i;
 
-    TRACE("(%p)->(%f,%p)\n", This, epsilon, adjacency);
+    TRACE("iface %p, epsilon %.8e, adjacency %p.\n", iface, epsilon, adjacency);
 
     if (!adjacency)
         return D3DERR_INVALIDCALL;
@@ -1355,14 +1363,14 @@ cleanup:
     return hr;
 }
 
-static HRESULT WINAPI ID3DXMeshImpl_UpdateSemantics(ID3DXMesh *iface, D3DVERTEXELEMENT9 declaration[MAX_FVF_DECL_SIZE])
+static HRESULT WINAPI d3dx9_mesh_UpdateSemantics(ID3DXMesh *iface, D3DVERTEXELEMENT9 declaration[MAX_FVF_DECL_SIZE])
 {
+    struct d3dx9_mesh *This = impl_from_ID3DXMesh(iface);
     HRESULT hr;
-    ID3DXMeshImpl *This = impl_from_ID3DXMesh(iface);
     UINT vertex_declaration_size;
     int i;
 
-    TRACE("(%p)->(%p)\n", This, declaration);
+    TRACE("iface %p, declaration %p.\n", iface, declaration);
 
     if (!declaration)
     {
@@ -1414,53 +1422,54 @@ static HRESULT WINAPI ID3DXMeshImpl_UpdateSemantics(ID3DXMesh *iface, D3DVERTEXE
     return D3D_OK;
 }
 
-/*** ID3DXMesh ***/
-static HRESULT WINAPI ID3DXMeshImpl_LockAttributeBuffer(ID3DXMesh *iface, DWORD flags, DWORD **data)
+static HRESULT WINAPI d3dx9_mesh_LockAttributeBuffer(ID3DXMesh *iface, DWORD flags, DWORD **data)
 {
-    ID3DXMeshImpl *This = impl_from_ID3DXMesh(iface);
+    struct d3dx9_mesh *mesh = impl_from_ID3DXMesh(iface);
 
-    TRACE("(%p)->(%u,%p)\n", This, flags, data);
+    TRACE("iface %p, flags %#x, data %p.\n", iface, flags, data);
 
-    InterlockedIncrement(&This->attrib_buffer_lock_count);
+    InterlockedIncrement(&mesh->attrib_buffer_lock_count);
 
-    if (!(flags & D3DLOCK_READONLY)) {
-        D3DXATTRIBUTERANGE *attrib_table = This->attrib_table;
-        This->attrib_table_size = 0;
-        This->attrib_table = NULL;
+    if (!(flags & D3DLOCK_READONLY))
+    {
+        D3DXATTRIBUTERANGE *attrib_table = mesh->attrib_table;
+        mesh->attrib_table_size = 0;
+        mesh->attrib_table = NULL;
         HeapFree(GetProcessHeap(), 0, attrib_table);
     }
 
-    *data = This->attrib_buffer;
+    *data = mesh->attrib_buffer;
 
     return D3D_OK;
 }
 
-static HRESULT WINAPI ID3DXMeshImpl_UnlockAttributeBuffer(ID3DXMesh *iface)
+static HRESULT WINAPI d3dx9_mesh_UnlockAttributeBuffer(ID3DXMesh *iface)
 {
-    ID3DXMeshImpl *This = impl_from_ID3DXMesh(iface);
+    struct d3dx9_mesh *mesh = impl_from_ID3DXMesh(iface);
     int lock_count;
 
-    TRACE("(%p)\n", This);
-
-    lock_count = InterlockedDecrement(&This->attrib_buffer_lock_count);
+    TRACE("iface %p.\n", iface);
 
-    if (lock_count < 0) {
-        InterlockedIncrement(&This->attrib_buffer_lock_count);
+    lock_count = InterlockedDecrement(&mesh->attrib_buffer_lock_count);
+    if (lock_count < 0)
+    {
+        InterlockedIncrement(&mesh->attrib_buffer_lock_count);
         return D3DERR_INVALIDCALL;
     }
 
     return D3D_OK;
 }
 
-static HRESULT WINAPI ID3DXMeshImpl_Optimize(ID3DXMesh *iface, DWORD flags, const DWORD *adjacency_in,
+static HRESULT WINAPI d3dx9_mesh_Optimize(ID3DXMesh *iface, DWORD flags, const DWORD *adjacency_in,
         DWORD *adjacency_out, DWORD *face_remap, ID3DXBuffer **vertex_remap, ID3DXMesh **opt_mesh)
 {
-    ID3DXMeshImpl *This = impl_from_ID3DXMesh(iface);
+    struct d3dx9_mesh *mesh = impl_from_ID3DXMesh(iface);
     HRESULT hr;
     D3DVERTEXELEMENT9 declaration[MAX_FVF_DECL_SIZE] = { D3DDECL_END() };
     ID3DXMesh *optimized_mesh;
 
-    TRACE("(%p)->(%x,%p,%p,%p,%p,%p)\n", This, flags, adjacency_in, adjacency_out, face_remap, vertex_remap, opt_mesh);
+    TRACE("iface %p, flags %#x, adjacency_in %p, adjacency_out %p, face_remap %p, vertex_remap %p, opt_mesh %p.\n",
+            iface, flags, adjacency_in, adjacency_out, face_remap, vertex_remap, opt_mesh);
 
     if (!opt_mesh)
         return D3DERR_INVALIDCALL;
@@ -1468,8 +1477,8 @@ static HRESULT WINAPI ID3DXMeshImpl_Optimize(ID3DXMesh *iface, DWORD flags, cons
     hr = iface->lpVtbl->GetDeclaration(iface, declaration);
     if (FAILED(hr)) return hr;
 
-    hr = iface->lpVtbl->CloneMesh(iface, This->options, declaration, This->device, &optimized_mesh);
-    if (FAILED(hr)) return hr;
+    if (FAILED(hr = iface->lpVtbl->CloneMesh(iface, mesh->options, declaration, mesh->device, &optimized_mesh)))
+        return hr;
 
     hr = optimized_mesh->lpVtbl->OptimizeInplace(optimized_mesh, flags, adjacency_in, adjacency_out, face_remap, vertex_remap);
     if (SUCCEEDED(hr))
@@ -1481,7 +1490,8 @@ static HRESULT WINAPI ID3DXMeshImpl_Optimize(ID3DXMesh *iface, DWORD flags, cons
 
 /* Creates a vertex_remap that removes unused vertices.
  * Indices are updated according to the vertex_remap. */
-static HRESULT compact_mesh(ID3DXMeshImpl *This, DWORD *indices, DWORD *new_num_vertices, ID3DXBuffer **vertex_remap)
+static HRESULT compact_mesh(struct d3dx9_mesh *This, DWORD *indices,
+        DWORD *new_num_vertices, ID3DXBuffer **vertex_remap)
 {
     HRESULT hr;
     DWORD *vertex_remap_ptr;
@@ -1590,18 +1600,23 @@ static int attrib_entry_compare(const DWORD **a, const DWORD **b)
 }
 
 /* Create face_remap, a new attribute buffer for attribute sort optimization. */
-static HRESULT remap_faces_for_attrsort(ID3DXMeshImpl *This, const DWORD *indices,
-        const DWORD *attrib_buffer, DWORD **sorted_attrib_buffer, DWORD **face_remap)
+static HRESULT remap_faces_for_attrsort(struct d3dx9_mesh *This, const DWORD *indices,
+        DWORD *attrib_buffer, DWORD **sorted_attrib_buffer, DWORD **face_remap)
 {
-    const DWORD **sorted_attrib_ptr_buffer = NULL;
+    DWORD **sorted_attrib_ptr_buffer = NULL;
     DWORD i;
 
-    *face_remap = HeapAlloc(GetProcessHeap(), 0, This->numfaces * sizeof(**face_remap));
     sorted_attrib_ptr_buffer = HeapAlloc(GetProcessHeap(), 0, This->numfaces * sizeof(*sorted_attrib_ptr_buffer));
-    if (!*face_remap || !sorted_attrib_ptr_buffer) {
+    if (!sorted_attrib_ptr_buffer)
+        return E_OUTOFMEMORY;
+
+    *face_remap = HeapAlloc(GetProcessHeap(), 0, This->numfaces * sizeof(**face_remap));
+    if (!*face_remap)
+    {
         HeapFree(GetProcessHeap(), 0, sorted_attrib_ptr_buffer);
         return E_OUTOFMEMORY;
     }
+
     for (i = 0; i < This->numfaces; i++)
         sorted_attrib_ptr_buffer[i] = &attrib_buffer[i];
     qsort(sorted_attrib_ptr_buffer, This->numfaces, sizeof(*sorted_attrib_ptr_buffer),
@@ -1621,10 +1636,10 @@ static HRESULT remap_faces_for_attrsort(ID3DXMeshImpl *This, const DWORD *indice
     return D3D_OK;
 }
 
-static HRESULT WINAPI ID3DXMeshImpl_OptimizeInplace(ID3DXMesh *iface, DWORD flags, const DWORD *adjacency_in,
+static HRESULT WINAPI d3dx9_mesh_OptimizeInplace(ID3DXMesh *iface, DWORD flags, const DWORD *adjacency_in,
         DWORD *adjacency_out, DWORD *face_remap_out, ID3DXBuffer **vertex_remap_out)
 {
-    ID3DXMeshImpl *This = impl_from_ID3DXMesh(iface);
+    struct d3dx9_mesh *This = impl_from_ID3DXMesh(iface);
     void *indices = NULL;
     DWORD *attrib_buffer = NULL;
     HRESULT hr;
@@ -1637,7 +1652,8 @@ static HRESULT WINAPI ID3DXMeshImpl_OptimizeInplace(ID3DXMesh *iface, DWORD flag
     DWORD *sorted_attrib_buffer = NULL;
     DWORD i;
 
-    TRACE("(%p)->(%x,%p,%p,%p,%p)\n", This, flags, adjacency_in, adjacency_out, face_remap_out, vertex_remap_out);
+    TRACE("iface %p, flags %#x, adjacency_in %p, adjacency_out %p, face_remap_out %p, vertex_remap_out %p.\n",
+            iface, flags, adjacency_in, adjacency_out, face_remap_out, vertex_remap_out);
 
     if (!flags)
         return D3DERR_INVALIDCALL;
@@ -1675,11 +1691,7 @@ static HRESULT WINAPI ID3DXMeshImpl_OptimizeInplace(ID3DXMesh *iface, DWORD flag
         if (FAILED(hr)) goto cleanup;
     } else if (flags & D3DXMESHOPT_ATTRSORT) {
         if (!(flags & D3DXMESHOPT_IGNOREVERTS))
-        {
             FIXME("D3DXMESHOPT_ATTRSORT vertex reordering not implemented.\n");
-            hr = E_NOTIMPL;
-            goto cleanup;
-        }
 
         hr = iface->lpVtbl->LockAttributeBuffer(iface, 0, &attrib_buffer);
         if (FAILED(hr)) goto cleanup;
@@ -1818,12 +1830,13 @@ cleanup:
     return hr;
 }
 
-static HRESULT WINAPI ID3DXMeshImpl_SetAttributeTable(ID3DXMesh *iface, CONST D3DXATTRIBUTERANGE *attrib_table, DWORD attrib_table_size)
+static HRESULT WINAPI d3dx9_mesh_SetAttributeTable(ID3DXMesh *iface,
+        const D3DXATTRIBUTERANGE *attrib_table, DWORD attrib_table_size)
 {
-    ID3DXMeshImpl *This = impl_from_ID3DXMesh(iface);
+    struct d3dx9_mesh *mesh = impl_from_ID3DXMesh(iface);
     D3DXATTRIBUTERANGE *new_table = NULL;
 
-    TRACE("(%p)->(%p,%u)\n", This, attrib_table, attrib_table_size);
+    TRACE("iface %p, attrib_table %p, attrib_table_size %u.\n", iface, attrib_table, attrib_table_size);
 
     if (attrib_table_size) {
         size_t size = attrib_table_size * sizeof(*attrib_table);
@@ -1836,53 +1849,46 @@ static HRESULT WINAPI ID3DXMeshImpl_SetAttributeTable(ID3DXMesh *iface, CONST D3
     } else if (attrib_table) {
         return D3DERR_INVALIDCALL;
     }
-    HeapFree(GetProcessHeap(), 0, This->attrib_table);
-    This->attrib_table = new_table;
-    This->attrib_table_size = attrib_table_size;
+    HeapFree(GetProcessHeap(), 0, mesh->attrib_table);
+    mesh->attrib_table = new_table;
+    mesh->attrib_table_size = attrib_table_size;
 
     return D3D_OK;
 }
 
 static const struct ID3DXMeshVtbl D3DXMesh_Vtbl =
 {
-    /*** IUnknown methods ***/
-    ID3DXMeshImpl_QueryInterface,
-    ID3DXMeshImpl_AddRef,
-    ID3DXMeshImpl_Release,
-    /*** ID3DXBaseMesh ***/
-    ID3DXMeshImpl_DrawSubset,
-    ID3DXMeshImpl_GetNumFaces,
-    ID3DXMeshImpl_GetNumVertices,
-    ID3DXMeshImpl_GetFVF,
-    ID3DXMeshImpl_GetDeclaration,
-    ID3DXMeshImpl_GetNumBytesPerVertex,
-    ID3DXMeshImpl_GetOptions,
-    ID3DXMeshImpl_GetDevice,
-    ID3DXMeshImpl_CloneMeshFVF,
-    ID3DXMeshImpl_CloneMesh,
-    ID3DXMeshImpl_GetVertexBuffer,
-    ID3DXMeshImpl_GetIndexBuffer,
-    ID3DXMeshImpl_LockVertexBuffer,
-    ID3DXMeshImpl_UnlockVertexBuffer,
-    ID3DXMeshImpl_LockIndexBuffer,
-    ID3DXMeshImpl_UnlockIndexBuffer,
-    ID3DXMeshImpl_GetAttributeTable,
-    ID3DXMeshImpl_ConvertPointRepsToAdjacency,
-    ID3DXMeshImpl_ConvertAdjacencyToPointReps,
-    ID3DXMeshImpl_GenerateAdjacency,
-    ID3DXMeshImpl_UpdateSemantics,
-    /*** ID3DXMesh ***/
-    ID3DXMeshImpl_LockAttributeBuffer,
-    ID3DXMeshImpl_UnlockAttributeBuffer,
-    ID3DXMeshImpl_Optimize,
-    ID3DXMeshImpl_OptimizeInplace,
-    ID3DXMeshImpl_SetAttributeTable
+    d3dx9_mesh_QueryInterface,
+    d3dx9_mesh_AddRef,
+    d3dx9_mesh_Release,
+    d3dx9_mesh_DrawSubset,
+    d3dx9_mesh_GetNumFaces,
+    d3dx9_mesh_GetNumVertices,
+    d3dx9_mesh_GetFVF,
+    d3dx9_mesh_GetDeclaration,
+    d3dx9_mesh_GetNumBytesPerVertex,
+    d3dx9_mesh_GetOptions,
+    d3dx9_mesh_GetDevice,
+    d3dx9_mesh_CloneMeshFVF,
+    d3dx9_mesh_CloneMesh,
+    d3dx9_mesh_GetVertexBuffer,
+    d3dx9_mesh_GetIndexBuffer,
+    d3dx9_mesh_LockVertexBuffer,
+    d3dx9_mesh_UnlockVertexBuffer,
+    d3dx9_mesh_LockIndexBuffer,
+    d3dx9_mesh_UnlockIndexBuffer,
+    d3dx9_mesh_GetAttributeTable,
+    d3dx9_mesh_ConvertPointRepsToAdjacency,
+    d3dx9_mesh_ConvertAdjacencyToPointReps,
+    d3dx9_mesh_GenerateAdjacency,
+    d3dx9_mesh_UpdateSemantics,
+    d3dx9_mesh_LockAttributeBuffer,
+    d3dx9_mesh_UnlockAttributeBuffer,
+    d3dx9_mesh_Optimize,
+    d3dx9_mesh_OptimizeInplace,
+    d3dx9_mesh_SetAttributeTable,
 };
 
-/*************************************************************************
- * D3DXBoxBoundProbe
- */
-BOOL WINAPI D3DXBoxBoundProbe(CONST D3DXVECTOR3 *pmin, CONST D3DXVECTOR3 *pmax, CONST D3DXVECTOR3 *prayposition, CONST D3DXVECTOR3 *praydirection)
 
 /* Algorithm taken from the article: An Efficient and Robust Ray-Box Intersection Algorithm
 Amy Williams             University of Utah
@@ -1898,9 +1904,9 @@ This algorithm is free of patents or of copyrights, as confirmed by Peter Shirle
 
 Algorithm: Consider the box as the intersection of three slabs. Clip the ray
 against each slab, if there's anything left of the ray after we're
-done we've got an intersection of the ray with the box.
-*/
-
+done we've got an intersection of the ray with the box. */
+BOOL WINAPI D3DXBoxBoundProbe(const D3DXVECTOR3 *pmin, const D3DXVECTOR3 *pmax,
+        const D3DXVECTOR3 *prayposition, const D3DXVECTOR3 *praydirection)
 {
     FLOAT div, tmin, tmax, tymin, tymax, tzmin, tzmax;
 
@@ -1952,10 +1958,8 @@ done we've got an intersection of the ray with the box.
     return TRUE;
 }
 
-/*************************************************************************
- * D3DXComputeBoundingBox
- */
-HRESULT WINAPI D3DXComputeBoundingBox(CONST D3DXVECTOR3 *pfirstposition, DWORD numvertices, DWORD dwstride, D3DXVECTOR3 *pmin, D3DXVECTOR3 *pmax)
+HRESULT WINAPI D3DXComputeBoundingBox(const D3DXVECTOR3 *pfirstposition,
+        DWORD numvertices, DWORD dwstride, D3DXVECTOR3 *pmin, D3DXVECTOR3 *pmax)
 {
     D3DXVECTOR3 vec;
     unsigned int i;
@@ -1982,10 +1986,8 @@ HRESULT WINAPI D3DXComputeBoundingBox(CONST D3DXVECTOR3 *pfirstposition, DWORD n
     return D3D_OK;
 }
 
-/*************************************************************************
- * D3DXComputeBoundingSphere
- */
-HRESULT WINAPI D3DXComputeBoundingSphere(CONST D3DXVECTOR3* pfirstposition, DWORD numvertices, DWORD dwstride, D3DXVECTOR3 *pcenter, FLOAT *pradius)
+HRESULT WINAPI D3DXComputeBoundingSphere(const D3DXVECTOR3 *pfirstposition,
+        DWORD numvertices, DWORD dwstride, D3DXVECTOR3 *pcenter, float *pradius)
 {
     D3DXVECTOR3 temp;
     FLOAT d;
@@ -2359,20 +2361,21 @@ UINT WINAPI D3DXGetDeclLength(const D3DVERTEXELEMENT9 *decl)
     return element - decl;
 }
 
-/*************************************************************************
- * D3DXIntersectTri
- */
-BOOL WINAPI D3DXIntersectTri(CONST D3DXVECTOR3 *p0, CONST D3DXVECTOR3 *p1, CONST D3DXVECTOR3 *p2, CONST D3DXVECTOR3 *praypos, CONST D3DXVECTOR3 *praydir, FLOAT *pu, FLOAT *pv, FLOAT *pdist)
+BOOL WINAPI D3DXIntersectTri(const D3DXVECTOR3 *p0, const D3DXVECTOR3 *p1, const D3DXVECTOR3 *p2,
+        const D3DXVECTOR3 *praypos, const D3DXVECTOR3 *praydir, float *pu, float *pv, float *pdist)
 {
     D3DXMATRIX m;
     D3DXVECTOR4 vec;
 
+    TRACE("p0 %p, p1 %p, p2 %p, praypos %p, praydir %p, pu %p, pv %p, pdist %p.\n",
+            p0, p1, p2, praypos, praydir, pu, pv, pdist);
+
     m.u.m[0][0] = p1->x - p0->x;
     m.u.m[1][0] = p2->x - p0->x;
     m.u.m[2][0] = -praydir->x;
     m.u.m[3][0] = 0.0f;
-    m.u.m[0][1] = p1->y - p0->z;
-    m.u.m[1][1] = p2->y - p0->z;
+    m.u.m[0][1] = p1->y - p0->y;
+    m.u.m[1][1] = p2->y - p0->y;
     m.u.m[2][1] = -praydir->y;
     m.u.m[3][1] = 0.0f;
     m.u.m[0][2] = p1->z - p0->z;
@@ -2394,9 +2397,9 @@ BOOL WINAPI D3DXIntersectTri(CONST D3DXVECTOR3 *p0, CONST D3DXVECTOR3 *p1, CONST
         D3DXVec4Transform(&vec, &vec, &m);
         if ( (vec.x >= 0.0f) && (vec.y >= 0.0f) && (vec.x + vec.y <= 1.0f) && (vec.z >= 0.0f) )
         {
-            *pu = vec.x;
-            *pv = vec.y;
-            *pdist = fabs( vec.z );
+            if (pu) *pu = vec.x;
+            if (pv) *pv = vec.y;
+            if (pdist) *pdist = fabsf( vec.z );
             return TRUE;
         }
     }
@@ -2404,10 +2407,8 @@ BOOL WINAPI D3DXIntersectTri(CONST D3DXVECTOR3 *p0, CONST D3DXVECTOR3 *p1, CONST
     return FALSE;
 }
 
-/*************************************************************************
- * D3DXSphereBoundProbe
- */
-BOOL WINAPI D3DXSphereBoundProbe(CONST D3DXVECTOR3 *pcenter, FLOAT radius, CONST D3DXVECTOR3 *prayposition, CONST D3DXVECTOR3 *praydirection)
+BOOL WINAPI D3DXSphereBoundProbe(const D3DXVECTOR3 *pcenter, float radius,
+        const D3DXVECTOR3 *prayposition, const D3DXVECTOR3 *praydirection)
 {
     D3DXVECTOR3 difference;
     FLOAT a, b, c, d;
@@ -2436,7 +2437,7 @@ HRESULT WINAPI D3DXCreateMesh(DWORD numfaces, DWORD numvertices, DWORD options,
     IDirect3DVertexBuffer9 *vertex_buffer;
     IDirect3DIndexBuffer9 *index_buffer;
     DWORD *attrib_buffer;
-    ID3DXMeshImpl *object;
+    struct d3dx9_mesh *object;
     DWORD index_usage = 0;
     D3DPOOL index_pool = D3DPOOL_DEFAULT;
     D3DFORMAT index_format = D3DFMT_INDEX16;
@@ -2444,7 +2445,8 @@ HRESULT WINAPI D3DXCreateMesh(DWORD numfaces, DWORD numvertices, DWORD options,
     D3DPOOL vertex_pool = D3DPOOL_DEFAULT;
     int i;
 
-    TRACE("(%d, %d, %x, %p, %p, %p)\n", numfaces, numvertices, options, declaration, device, mesh);
+    TRACE("numfaces %u, numvertices %u, options %#x, declaration %p, device %p, mesh %p.\n",
+            numfaces, numvertices, options, declaration, device, mesh);
 
     if (numfaces == 0 || numvertices == 0 || declaration == NULL || device == NULL || mesh == NULL ||
         /* D3DXMESH_VB_SHARE is for cloning, and D3DXMESH_USEHWONLY is for ConvertToBlendedMesh */
@@ -2550,7 +2552,7 @@ HRESULT WINAPI D3DXCreateMesh(DWORD numfaces, DWORD numvertices, DWORD options,
     }
 
     attrib_buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, numfaces * sizeof(*attrib_buffer));
-    object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(ID3DXMeshImpl));
+    object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
     if (object == NULL || attrib_buffer == NULL)
     {
         HeapFree(GetProcessHeap(), 0, object);
@@ -2630,7 +2632,7 @@ struct mesh_data {
     DWORD nb_bones;
 };
 
-static HRESULT parse_texture_filename(ID3DXFileData *filedata, LPSTR *filename_out)
+static HRESULT parse_texture_filename(ID3DXFileData *filedata, char **filename_out)
 {
     HRESULT hr;
     SIZE_T data_size;
@@ -2650,12 +2652,13 @@ static HRESULT parse_texture_filename(ID3DXFileData *filedata, LPSTR *filename_o
     if (FAILED(hr)) return hr;
 
     /* FIXME: String must be retrieved directly instead of through a pointer once ID3DXFILE is fixed */
-    if (data_size < sizeof(LPSTR)) {
+    if (data_size < sizeof(filename_in))
+    {
         WARN("truncated data (%lu bytes)\n", data_size);
         filedata->lpVtbl->Unlock(filedata);
         return E_FAIL;
     }
-    filename_in = *(LPSTR*)data;
+    filename_in = *(char **)data;
 
     filename = HeapAlloc(GetProcessHeap(), 0, strlen(filename_in) + 1);
     if (!filename) {
@@ -2678,8 +2681,7 @@ static HRESULT parse_material(ID3DXFileData *filedata, D3DXMATERIAL *material)
     const BYTE *data;
     GUID type;
     ID3DXFileData *child;
-    SIZE_T nb_children;
-    int i;
+    SIZE_T i, nb_children;
 
     material->pTextureFilename = NULL;
 
@@ -2739,16 +2741,20 @@ static HRESULT parse_material(ID3DXFileData *filedata, D3DXMATERIAL *material)
             return hr;
         hr = child->lpVtbl->GetType(child, &type);
         if (FAILED(hr))
-            return hr;
+            goto err;
 
         if (IsEqualGUID(&type, &TID_D3DRMTextureFilename)) {
             hr = parse_texture_filename(child, &material->pTextureFilename);
             if (FAILED(hr))
-                return hr;
+                goto err;
         }
+        IUnknown_Release(child);
     }
-
     return D3D_OK;
+
+err:
+    IUnknown_Release(child);
+    return hr;
 }
 
 static void destroy_materials(struct mesh_data *mesh)
@@ -2769,7 +2775,7 @@ static HRESULT parse_material_list(ID3DXFileData *filedata, struct mesh_data *me
     SIZE_T data_size;
     const DWORD *data, *in_ptr;
     GUID type;
-    ID3DXFileData *child;
+    ID3DXFileData *child = NULL;
     DWORD num_materials;
     DWORD i;
     SIZE_T nb_children;
@@ -2852,6 +2858,9 @@ static HRESULT parse_material_list(ID3DXFileData *filedata, struct mesh_data *me
             if (FAILED(hr))
                 goto end;
         }
+
+        IUnknown_Release(child);
+        child = NULL;
     }
     if (num_materials != mesh->num_materials) {
         WARN("only %u of %u materials defined\n", num_materials, mesh->num_materials);
@@ -2859,6 +2868,8 @@ static HRESULT parse_material_list(ID3DXFileData *filedata, struct mesh_data *me
     }
 
 end:
+    if (child)
+        IUnknown_Release(child);
     filedata->lpVtbl->Unlock(filedata);
     return hr;
 }
@@ -3156,10 +3167,10 @@ static HRESULT parse_mesh(ID3DXFileData *filedata, struct mesh_data *mesh_data,
     const BYTE *data, *in_ptr;
     DWORD *index_out_ptr;
     GUID type;
-    ID3DXFileData *child;
+    ID3DXFileData *child = NULL;
     DWORD i;
     SIZE_T nb_children;
-    DWORD nb_skin_weigths_info = 0;
+    DWORD nb_skin_weights_info = 0;
 
     /*
      * template Mesh {
@@ -3273,9 +3284,6 @@ static HRESULT parse_mesh(ID3DXFileData *filedata, struct mesh_data *mesh_data,
             hr = parse_vertex_colors(child, mesh_data);
         } else if (IsEqualGUID(&type, &TID_D3DRMMeshTextureCoords)) {
             hr = parse_texture_coords(child, mesh_data);
-        hr = filedata->lpVtbl->GetChild(filedata, i, &child);
-        if (FAILED(hr))
-            goto end;
         } else if (IsEqualGUID(&type, &TID_D3DRMMeshMaterialList) &&
                    (provide_flags & PROVIDE_MATERIALS))
         {
@@ -3292,30 +3300,42 @@ static HRESULT parse_mesh(ID3DXFileData *filedata, struct mesh_data *mesh_data,
                     goto end;
             } else if (IsEqualGUID(&type, &DXFILEOBJ_SkinWeights)) {
                 if (!mesh_data->skin_info) {
-                    WARN("Skin weigths found but skin mesh header not encountered yet\n");
+                    WARN("Skin weights found but skin mesh header not encountered yet\n");
                     hr = E_FAIL;
                     goto end;
                 }
-                hr = parse_skin_mesh_info(child, mesh_data, nb_skin_weigths_info);
+                hr = parse_skin_mesh_info(child, mesh_data, nb_skin_weights_info);
                 if (FAILED(hr))
                     goto end;
-                nb_skin_weigths_info++;
+                nb_skin_weights_info++;
             }
         }
         if (FAILED(hr))
             goto end;
+
+        IUnknown_Release(child);
+        child = NULL;
     }
 
-    if (mesh_data->skin_info && (nb_skin_weigths_info != mesh_data->nb_bones)) {
+    if (mesh_data->skin_info && (nb_skin_weights_info != mesh_data->nb_bones)) {
         WARN("Mismatch between nb skin weights info %u encountered and nb bones %u from skin mesh header\n",
-             nb_skin_weigths_info, mesh_data->nb_bones);
+             nb_skin_weights_info, mesh_data->nb_bones);
         hr = E_FAIL;
         goto end;
     }
 
+    if ((provide_flags & PROVIDE_SKININFO) && !mesh_data->skin_info)
+    {
+        hr = create_dummy_skin(&mesh_data->skin_info);
+        if (FAILED(hr))
+            goto end;
+    }
+
     hr = D3D_OK;
 
 end:
+    if (child)
+        IUnknown_Release(child);
     filedata->lpVtbl->Unlock(filedata);
     return hr;
 }
@@ -3390,7 +3410,7 @@ static HRESULT generate_effects(ID3DXBuffer *materials, DWORD num_materials,
 
         for (j = 0; j < ARRAY_SIZE(material_effects); j++)
         {
-            defaults->pParamName = (LPSTR)out_ptr;
+            defaults->pParamName = (char *)out_ptr;
             strcpy(defaults->pParamName, material_effects[j].param_name);
             defaults->pValue = defaults->pParamName + material_effects[j].name_size;
             defaults->Type = D3DXEDT_FLOATS;
@@ -3400,8 +3420,9 @@ static HRESULT generate_effects(ID3DXBuffer *materials, DWORD num_materials,
             defaults++;
         }
 
-        if (material_ptr->pTextureFilename) {
-            defaults->pParamName = (LPSTR)out_ptr;
+        if (material_ptr->pTextureFilename)
+        {
+            defaults->pParamName = (char *)out_ptr;
             strcpy(defaults->pParamName, texture_paramname);
             defaults->pValue = defaults->pParamName + sizeof(texture_paramname);
             defaults->Type = D3DXEDT_STRING;
@@ -3665,12 +3686,14 @@ HRESULT WINAPI D3DXLoadMeshHierarchyFromXA(const char *filename, DWORD options,
         struct ID3DXAllocateHierarchy *alloc_hier, struct ID3DXLoadUserData *load_user_data,
         D3DXFRAME **frame_hierarchy, struct ID3DXAnimationController **anim_controller)
 {
+    WCHAR *filenameW;
     HRESULT hr;
     int len;
-    LPWSTR filenameW;
 
-    TRACE("(%s, %x, %p, %p, %p, %p, %p)\n", debugstr_a(filename), options,
-          device, alloc_hier, load_user_data, frame_hierarchy, anim_controller);
+    TRACE("filename %s, options %#x, device %p, alloc_hier %p, "
+            "load_user_data %p, frame_hierarchy %p, anim_controller %p.\n",
+            debugstr_a(filename), options, device, alloc_hier,
+            load_user_data, frame_hierarchy, anim_controller);
 
     if (!filename)
         return D3DERR_INVALIDCALL;
@@ -3691,12 +3714,14 @@ HRESULT WINAPI D3DXLoadMeshHierarchyFromXW(const WCHAR *filename, DWORD options,
         struct ID3DXAllocateHierarchy *alloc_hier, struct ID3DXLoadUserData *load_user_data,
         D3DXFRAME **frame_hierarchy, struct ID3DXAnimationController **anim_controller)
 {
+    void *buffer;
     HRESULT hr;
     DWORD size;
-    LPVOID buffer;
 
-    TRACE("(%s, %x, %p, %p, %p, %p, %p)\n", debugstr_w(filename), options,
-          device, alloc_hier, load_user_data, frame_hierarchy, anim_controller);
+    TRACE("filename %s, options %#x, device %p, alloc_hier %p, "
+            "load_user_data %p, frame_hierarchy %p, anim_controller %p.\n",
+            debugstr_w(filename), options, device, alloc_hier,
+            load_user_data, frame_hierarchy, anim_controller);
 
     if (!filename)
         return D3DERR_INVALIDCALL;
@@ -3814,8 +3839,7 @@ static HRESULT load_frame(struct ID3DXFileData *filedata, DWORD options, struct
     D3DXFRAME *frame = NULL;
     D3DXMESHCONTAINER **next_container;
     D3DXFRAME **next_child;
-    SIZE_T nb_children;
-    int i;
+    SIZE_T i, nb_children;
 
     hr = filedata_get_name(filedata, &name);
     if (FAILED(hr)) return hr;
@@ -3840,7 +3864,7 @@ static HRESULT load_frame(struct ID3DXFileData *filedata, DWORD options, struct
             return hr;
         hr = child->lpVtbl->GetType(child, &type);
         if (FAILED(hr))
-            return hr;
+            goto err;
 
         if (IsEqualGUID(&type, &TID_D3DRMMesh)) {
             hr = load_mesh_container(child, options, device, alloc_hier, next_container);
@@ -3854,10 +3878,15 @@ static HRESULT load_frame(struct ID3DXFileData *filedata, DWORD options, struct
                 next_child = &(*next_child)->pFrameSibling;
         }
         if (FAILED(hr))
-            return hr;
-    }
+            goto err;
 
+        IUnknown_Release(child);
+    }
     return D3D_OK;
+
+err:
+    IUnknown_Release(child);
+    return hr;
 }
 
 HRESULT WINAPI D3DXLoadMeshHierarchyFromXInMemory(const void *memory, DWORD memory_size, DWORD options,
@@ -3872,20 +3901,17 @@ HRESULT WINAPI D3DXLoadMeshHierarchyFromXInMemory(const void *memory, DWORD memo
     D3DXF_FILELOADMEMORY source;
     D3DXFRAME *first_frame = NULL;
     D3DXFRAME **next_frame = &first_frame;
-    SIZE_T nb_children;
+    SIZE_T i, nb_children;
     GUID guid;
-    int i;
 
     TRACE("(%p, %u, %x, %p, %p, %p, %p, %p)\n", memory, memory_size, options,
           device, alloc_hier, load_user_data, frame_hierarchy, anim_controller);
 
     if (!memory || !memory_size || !device || !frame_hierarchy || !alloc_hier)
         return D3DERR_INVALIDCALL;
-    if (load_user_data || anim_controller) {
-        if (load_user_data)
-            FIXME("Loading user data not implemented\n");
-        if (anim_controller)
-            FIXME("Animation controller creation not implemented\n");
+    if (load_user_data)
+    {
+        FIXME("Loading user data not implemented.\n");
         return E_NOTIMPL;
     }
 
@@ -3955,6 +3981,12 @@ HRESULT WINAPI D3DXLoadMeshHierarchyFromXInMemory(const void *memory, DWORD memo
         hr = D3D_OK;
     }
 
+    if (anim_controller)
+    {
+        *anim_controller = NULL;
+        FIXME("Animation controller creation not implemented.\n");
+    }
+
 cleanup:
     if (FAILED(hr) && first_frame) D3DXFrameDestroy(first_frame, alloc_hier);
     if (filedata) filedata->lpVtbl->Release(filedata);
@@ -4013,16 +4045,24 @@ HRESULT WINAPI D3DXFrameDestroy(D3DXFRAME *frame, ID3DXAllocateHierarchy *alloc_
     return D3D_OK;
 }
 
+D3DXFRAME* WINAPI D3DXFrameFind(const D3DXFRAME *frame_root, const char *name)
+{
+    FIXME("frame_root %p, name %s stub.\n", frame_root, debugstr_a(name));
+    return NULL;
+}
+
 HRESULT WINAPI D3DXLoadMeshFromXA(const char *filename, DWORD options, struct IDirect3DDevice9 *device,
         struct ID3DXBuffer **adjacency, struct ID3DXBuffer **materials, struct ID3DXBuffer **effect_instances,
         DWORD *num_materials, struct ID3DXMesh **mesh)
 {
+    WCHAR *filenameW;
     HRESULT hr;
     int len;
-    LPWSTR filenameW;
 
-    TRACE("(%s, %x, %p, %p, %p, %p, %p, %p)\n", debugstr_a(filename), options,
-          device, adjacency, materials, effect_instances, num_materials, mesh);
+    TRACE("filename %s, options %#x, device %p, adjacency %p, materials %p, "
+            "effect_instances %p, num_materials %p, mesh %p.\n",
+            debugstr_a(filename), options, device, adjacency, materials,
+            effect_instances, num_materials, mesh);
 
     if (!filename)
         return D3DERR_INVALIDCALL;
@@ -4043,12 +4083,14 @@ HRESULT WINAPI D3DXLoadMeshFromXW(const WCHAR *filename, DWORD options, struct I
         struct ID3DXBuffer **adjacency, struct ID3DXBuffer **materials, struct ID3DXBuffer **effect_instances,
         DWORD *num_materials, struct ID3DXMesh **mesh)
 {
+    void *buffer;
     HRESULT hr;
     DWORD size;
-    LPVOID buffer;
 
-    TRACE("(%s, %x, %p, %p, %p, %p, %p, %p)\n", debugstr_w(filename), options,
-          device, adjacency, materials, effect_instances, num_materials, mesh);
+    TRACE("filename %s, options %#x, device %p, adjacency %p, materials %p, "
+            "effect_instances %p, num_materials %p, mesh %p.\n",
+            debugstr_w(filename), options, device, adjacency, materials,
+            effect_instances, num_materials, mesh);
 
     if (!filename)
         return D3DERR_INVALIDCALL;
@@ -4071,12 +4113,13 @@ HRESULT WINAPI D3DXLoadMeshFromXResource(HMODULE module, const char *name, const
 {
     HRESULT hr;
     HRSRC resinfo;
+    void *buffer;
     DWORD size;
-    LPVOID buffer;
 
-    TRACE("(%p, %s, %s, %x, %p, %p, %p, %p, %p, %p)\n",
-          module, debugstr_a(name), debugstr_a(type), options, device,
-          adjacency, materials, effect_instances, num_materials, mesh);
+    TRACE("module %p, name %s, type %s, options %#x, device %p, adjacency %p, "
+            "materials %p, effect_instances %p, num_materials %p, mesh %p.\n",
+            module, debugstr_a(name), debugstr_a(type), options, device, adjacency,
+            materials, effect_instances, num_materials, mesh);
 
     resinfo = FindResourceA(module, name, type);
     if (!resinfo) return D3DXERR_INVALIDDATA;
@@ -4106,8 +4149,7 @@ static HRESULT parse_frame(struct ID3DXFileData *filedata, DWORD options, struct
     D3DXMATRIX transform = *parent_transform;
     ID3DXFileData *child;
     GUID type;
-    SIZE_T nb_children;
-    int i;
+    SIZE_T i, nb_children;
 
     hr = filedata->lpVtbl->GetChildren(filedata, &nb_children);
     if (FAILED(hr))
@@ -4120,12 +4162,15 @@ static HRESULT parse_frame(struct ID3DXFileData *filedata, DWORD options, struct
             return hr;
         hr = child->lpVtbl->GetType(child, &type);
         if (FAILED(hr))
-            return hr;
+            goto err;
 
         if (IsEqualGUID(&type, &TID_D3DRMMesh)) {
             struct mesh_container *container = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*container));
             if (!container)
-                return E_OUTOFMEMORY;
+            {
+                hr = E_OUTOFMEMORY;
+                goto err;
+            }
             list_add_tail(container_list, &container->entry);
             container->transform = transform;
 
@@ -4141,10 +4186,15 @@ static HRESULT parse_frame(struct ID3DXFileData *filedata, DWORD options, struct
             hr = parse_frame(child, options, device, &transform, container_list, provide_flags);
         }
         if (FAILED(hr))
-            return hr;
-    }
+            goto err;
 
+        IUnknown_Release(child);
+    }
     return D3D_OK;
+
+err:
+    IUnknown_Release(child);
+    return hr;
 }
 
 HRESULT WINAPI D3DXLoadMeshFromXInMemory(const void *memory, DWORD memory_size, DWORD options,
@@ -4172,9 +4222,8 @@ HRESULT WINAPI D3DXLoadMeshFromXInMemory(const void *memory, DWORD memory_size,
     void *concat_indices = NULL;
     DWORD index_offset;
     DWORD concat_vertex_size;
-    SIZE_T nb_children;
+    SIZE_T i, nb_children;
     GUID guid;
-    int i;
 
     TRACE("(%p, %u, %x, %p, %p, %p, %p, %p, %p)\n", memory, memory_size, options,
           device, adjacency_out, materials_out, effects_out, num_materials_out, mesh_out);
@@ -4507,20 +4556,194 @@ cleanup:
     return hr;
 }
 
-HRESULT WINAPI D3DXCreateBox(struct IDirect3DDevice9 *device, float width, float height,
-        float depth, struct ID3DXMesh **mesh, struct ID3DXBuffer **adjacency)
-{
-    FIXME("(%p, %f, %f, %f, %p, %p): stub\n", device, width, height, depth, mesh, adjacency);
-
-    return E_NOTIMPL;
-}
-
 struct vertex
 {
     D3DXVECTOR3 position;
     D3DXVECTOR3 normal;
 };
 
+HRESULT WINAPI D3DXCreatePolygon(struct IDirect3DDevice9 *device, float length, UINT sides,
+        struct ID3DXMesh **mesh, struct ID3DXBuffer **adjacency)
+{
+    HRESULT hr;
+    ID3DXMesh *polygon;
+    struct vertex *vertices;
+    WORD (*faces)[3];
+    DWORD (*adjacency_buf)[3];
+    float scale;
+    unsigned int i;
+
+    TRACE("device %p, length %f, sides %u, mesh %p, adjacency %p.\n",
+            device, length, sides, mesh, adjacency);
+
+    if (!device || length < 0.0f || sides < 3 || !mesh)
+        return D3DERR_INVALIDCALL;
+
+    if (FAILED(hr = D3DXCreateMeshFVF(sides, sides + 1, D3DXMESH_MANAGED,
+            D3DFVF_XYZ | D3DFVF_NORMAL, device, &polygon)))
+    {
+        return hr;
+    }
+
+    if (FAILED(hr = polygon->lpVtbl->LockVertexBuffer(polygon, 0, (void **)&vertices)))
+    {
+        polygon->lpVtbl->Release(polygon);
+        return hr;
+    }
+
+    if (FAILED(hr = polygon->lpVtbl->LockIndexBuffer(polygon, 0, (void **)&faces)))
+    {
+        polygon->lpVtbl->UnlockVertexBuffer(polygon);
+        polygon->lpVtbl->Release(polygon);
+        return hr;
+    }
+
+    scale = 0.5f * length / sinf(D3DX_PI / sides);
+
+    vertices[0].position.x = 0.0f;
+    vertices[0].position.y = 0.0f;
+    vertices[0].position.z = 0.0f;
+    vertices[0].normal.x = 0.0f;
+    vertices[0].normal.y = 0.0f;
+    vertices[0].normal.z = 1.0f;
+
+    for (i = 0; i < sides; ++i)
+    {
+        vertices[i + 1].position.x = cosf(2.0f * D3DX_PI * i / sides) * scale;
+        vertices[i + 1].position.y = sinf(2.0f * D3DX_PI * i / sides) * scale;
+        vertices[i + 1].position.z = 0.0f;
+        vertices[i + 1].normal.x = 0.0f;
+        vertices[i + 1].normal.y = 0.0f;
+        vertices[i + 1].normal.z = 1.0f;
+
+        faces[i][0] = 0;
+        faces[i][1] = i + 1;
+        faces[i][2] = i + 2;
+    }
+
+    faces[sides - 1][2] = 1;
+
+    polygon->lpVtbl->UnlockVertexBuffer(polygon);
+    polygon->lpVtbl->UnlockIndexBuffer(polygon);
+
+    if (adjacency)
+    {
+        if (FAILED(hr = D3DXCreateBuffer(sides * sizeof(DWORD) * 3, adjacency)))
+        {
+            polygon->lpVtbl->Release(polygon);
+            return hr;
+        }
+
+        adjacency_buf = ID3DXBuffer_GetBufferPointer(*adjacency);
+        for (i = 0; i < sides; ++i)
+        {
+            adjacency_buf[i][0] = i - 1;
+            adjacency_buf[i][1] = ~0U;
+            adjacency_buf[i][2] = i + 1;
+        }
+        adjacency_buf[0][0] = sides - 1;
+        adjacency_buf[sides - 1][2] = 0;
+    }
+
+    *mesh = polygon;
+
+    return D3D_OK;
+}
+
+HRESULT WINAPI D3DXCreateBox(struct IDirect3DDevice9 *device, float width, float height,
+        float depth, struct ID3DXMesh **mesh, struct ID3DXBuffer **adjacency)
+{
+    HRESULT hr;
+    ID3DXMesh *box;
+    struct vertex *vertices;
+    WORD (*faces)[3];
+    DWORD *adjacency_buf;
+    unsigned int i, face;
+    static const D3DXVECTOR3 unit_box[] =
+    {
+        {-0.5f, -0.5f, -0.5f}, {-0.5f, -0.5f,  0.5f}, {-0.5f,  0.5f,  0.5f}, {-0.5f,  0.5f, -0.5f},
+        {-0.5f,  0.5f, -0.5f}, {-0.5f,  0.5f,  0.5f}, { 0.5f,  0.5f,  0.5f}, { 0.5f,  0.5f, -0.5f},
+        { 0.5f,  0.5f, -0.5f}, { 0.5f,  0.5f,  0.5f}, { 0.5f, -0.5f,  0.5f}, { 0.5f, -0.5f, -0.5f},
+        {-0.5f, -0.5f,  0.5f}, {-0.5f, -0.5f, -0.5f}, { 0.5f, -0.5f, -0.5f}, { 0.5f, -0.5f,  0.5f},
+        {-0.5f, -0.5f,  0.5f}, { 0.5f, -0.5f,  0.5f}, { 0.5f,  0.5f,  0.5f}, {-0.5f,  0.5f,  0.5f},
+        {-0.5f, -0.5f, -0.5f}, {-0.5f,  0.5f, -0.5f}, { 0.5f,  0.5f, -0.5f}, { 0.5f, -0.5f, -0.5f}
+    };
+    static const D3DXVECTOR3 normals[] =
+    {
+        {-1.0f,  0.0f, 0.0f}, { 0.0f, 1.0f, 0.0f}, { 1.0f, 0.0f,  0.0f},
+        { 0.0f, -1.0f, 0.0f}, { 0.0f, 0.0f, 1.0f}, { 0.0f, 0.0f, -1.0f}
+    };
+    static const DWORD adjacency_table[] =
+    {
+        6, 9, 1, 2, 10, 0, 1,  9,  3, 4, 10,  2,
+        3, 8, 5, 7, 11, 4, 0, 11,  7, 5,  8,  6,
+        7, 4, 9, 2,  0, 8, 1,  3, 11, 5,  6, 10
+    };
+
+    TRACE("device %p, width %f, height %f, depth %f, mesh %p, adjacency %p\n",
+                    device, width, height, depth, mesh, adjacency);
+
+    if (!device || width < 0.0f || height < 0.0f || depth < 0.0f || !mesh)
+    {
+        return D3DERR_INVALIDCALL;
+    }
+
+    if (FAILED(hr = D3DXCreateMeshFVF(12, 24, D3DXMESH_MANAGED, D3DFVF_XYZ | D3DFVF_NORMAL, device, &box)))
+    {
+        return hr;
+    }
+
+    if (FAILED(hr = box->lpVtbl->LockVertexBuffer(box, 0, (void **)&vertices)))
+    {
+        box->lpVtbl->Release(box);
+        return hr;
+    }
+
+    if (FAILED(hr = box->lpVtbl->LockIndexBuffer(box, 0, (void **)&faces)))
+    {
+        box->lpVtbl->UnlockVertexBuffer(box);
+        box->lpVtbl->Release(box);
+        return hr;
+    }
+
+    for (i = 0; i < 24; i++)
+    {
+        vertices[i].position.x = width * unit_box[i].x;
+        vertices[i].position.y = height * unit_box[i].y;
+        vertices[i].position.z = depth * unit_box[i].z;
+        vertices[i].normal.x = normals[i / 4].x;
+        vertices[i].normal.y = normals[i / 4].y;
+        vertices[i].normal.z = normals[i / 4].z;
+    }
+
+    face = 0;
+    for (i = 0; i < 12; i++)
+    {
+        faces[i][0] = face++;
+        faces[i][1] = face++;
+        faces[i][2] = (i % 2) ? face - 4 : face;
+    }
+
+    box->lpVtbl->UnlockIndexBuffer(box);
+    box->lpVtbl->UnlockVertexBuffer(box);
+
+    if (adjacency)
+    {
+        if (FAILED(hr = D3DXCreateBuffer(sizeof(adjacency_table), adjacency)))
+        {
+            box->lpVtbl->Release(box);
+            return hr;
+        }
+
+        adjacency_buf = ID3DXBuffer_GetBufferPointer(*adjacency);
+        memcpy(adjacency_buf, adjacency_table, sizeof(adjacency_table));
+    }
+
+    *mesh = box;
+
+    return D3D_OK;
+}
+
 typedef WORD face[3];
 
 struct sincos_table
@@ -4556,8 +4779,8 @@ static BOOL compute_sincos_table(struct sincos_table *sincos_table, float angle_
     angle = angle_start;
     for (i = 0; i < n; i++)
     {
-        sincos_table->sin[i] = sin(angle);
-        sincos_table->cos[i] = cos(angle);
+        sincos_table->sin[i] = sinf(angle);
+        sincos_table->cos[i] = cosf(angle);
         angle += angle_step;
     }
 
@@ -4589,12 +4812,6 @@ HRESULT WINAPI D3DXCreateSphere(struct IDirect3DDevice9 *device, float radius, U
         return D3DERR_INVALIDCALL;
     }
 
-    if (adjacency)
-    {
-        FIXME("Case of adjacency != NULL not implemented.\n");
-        return E_NOTIMPL;
-    }
-
     number_of_vertices = 2 + slices * (stacks-1);
     number_of_faces = 2 * slices + (stacks - 2) * (2 * slices);
 
@@ -4605,15 +4822,13 @@ HRESULT WINAPI D3DXCreateSphere(struct IDirect3DDevice9 *device, float radius, U
         return hr;
     }
 
-    hr = sphere->lpVtbl->LockVertexBuffer(sphere, 0, (LPVOID *)&vertices);
-    if (FAILED(hr))
+    if (FAILED(hr = sphere->lpVtbl->LockVertexBuffer(sphere, 0, (void **)&vertices)))
     {
         sphere->lpVtbl->Release(sphere);
         return hr;
     }
 
-    hr = sphere->lpVtbl->LockIndexBuffer(sphere, 0, (LPVOID *)&faces);
-    if (FAILED(hr))
+    if (FAILED(hr = sphere->lpVtbl->LockIndexBuffer(sphere, 0, (void **)&faces)))
     {
         sphere->lpVtbl->UnlockVertexBuffer(sphere);
         sphere->lpVtbl->Release(sphere);
@@ -4621,8 +4836,8 @@ HRESULT WINAPI D3DXCreateSphere(struct IDirect3DDevice9 *device, float radius, U
     }
 
     /* phi = angle on xz plane wrt z axis */
-    phi_step = -2 * M_PI / slices;
-    phi_start = M_PI / 2;
+    phi_step = -2.0f * D3DX_PI / slices;
+    phi_start = D3DX_PI / 2.0f;
 
     if (!compute_sincos_table(&phi, phi_start, phi_step, slices))
     {
@@ -4633,7 +4848,7 @@ HRESULT WINAPI D3DXCreateSphere(struct IDirect3DDevice9 *device, float radius, U
     }
 
     /* theta = angle on xy plane wrt x axis */
-    theta_step = M_PI / stacks;
+    theta_step = D3DX_PI / stacks;
     theta = theta_step;
 
     vertex = 0;
@@ -4649,8 +4864,8 @@ HRESULT WINAPI D3DXCreateSphere(struct IDirect3DDevice9 *device, float radius, U
 
     for (stack = 0; stack < stacks - 1; stack++)
     {
-        sin_theta = sin(theta);
-        cos_theta = cos(theta);
+        sin_theta = sinf(theta);
+        cos_theta = cosf(theta);
 
         for (slice = 0; slice < slices; slice++)
         {
@@ -4734,6 +4949,24 @@ HRESULT WINAPI D3DXCreateSphere(struct IDirect3DDevice9 *device, float radius, U
     free_sincos_table(&phi);
     sphere->lpVtbl->UnlockIndexBuffer(sphere);
     sphere->lpVtbl->UnlockVertexBuffer(sphere);
+
+
+    if (adjacency)
+    {
+        if (FAILED(hr = D3DXCreateBuffer(number_of_faces * sizeof(DWORD) * 3, adjacency)))
+        {
+            sphere->lpVtbl->Release(sphere);
+            return hr;
+        }
+
+        if (FAILED(hr = sphere->lpVtbl->GenerateAdjacency(sphere, 0.0f, (*adjacency)->lpVtbl->GetBufferPointer(*adjacency))))
+        {
+            (*adjacency)->lpVtbl->Release(*adjacency);
+            sphere->lpVtbl->Release(sphere);
+            return hr;
+        }
+    }
+
     *mesh = sphere;
 
     return D3D_OK;
@@ -4760,12 +4993,6 @@ HRESULT WINAPI D3DXCreateCylinder(struct IDirect3DDevice9 *device, float radius1
         return D3DERR_INVALIDCALL;
     }
 
-    if (adjacency)
-    {
-        FIXME("Case of adjacency != NULL not implemented.\n");
-        return E_NOTIMPL;
-    }
-
     number_of_vertices = 2 + (slices * (3 + stacks));
     number_of_faces = 2 * slices + stacks * (2 * slices);
 
@@ -4776,15 +5003,13 @@ HRESULT WINAPI D3DXCreateCylinder(struct IDirect3DDevice9 *device, float radius1
         return hr;
     }
 
-    hr = cylinder->lpVtbl->LockVertexBuffer(cylinder, 0, (LPVOID *)&vertices);
-    if (FAILED(hr))
+    if (FAILED(hr = cylinder->lpVtbl->LockVertexBuffer(cylinder, 0, (void **)&vertices)))
     {
         cylinder->lpVtbl->Release(cylinder);
         return hr;
     }
 
-    hr = cylinder->lpVtbl->LockIndexBuffer(cylinder, 0, (LPVOID *)&faces);
-    if (FAILED(hr))
+    if (FAILED(hr = cylinder->lpVtbl->LockIndexBuffer(cylinder, 0, (void **)&faces)))
     {
         cylinder->lpVtbl->UnlockVertexBuffer(cylinder);
         cylinder->lpVtbl->Release(cylinder);
@@ -4792,8 +5017,8 @@ HRESULT WINAPI D3DXCreateCylinder(struct IDirect3DDevice9 *device, float radius1
     }
 
     /* theta = angle on xy plane wrt x axis */
-    theta_step = -2 * M_PI / slices;
-    theta_start = M_PI / 2;
+    theta_step = -2.0f * D3DX_PI / slices;
+    theta_start = D3DX_PI / 2.0f;
 
     if (!compute_sincos_table(&theta, theta_start, theta_step, slices))
     {
@@ -4919,6 +5144,23 @@ HRESULT WINAPI D3DXCreateCylinder(struct IDirect3DDevice9 *device, float radius1
     free_sincos_table(&theta);
     cylinder->lpVtbl->UnlockIndexBuffer(cylinder);
     cylinder->lpVtbl->UnlockVertexBuffer(cylinder);
+
+    if (adjacency)
+    {
+        if (FAILED(hr = D3DXCreateBuffer(number_of_faces * sizeof(DWORD) * 3, adjacency)))
+        {
+            cylinder->lpVtbl->Release(cylinder);
+            return hr;
+        }
+
+        if (FAILED(hr = cylinder->lpVtbl->GenerateAdjacency(cylinder, 0.0f, (*adjacency)->lpVtbl->GetBufferPointer(*adjacency))))
+        {
+            (*adjacency)->lpVtbl->Release(*adjacency);
+            cylinder->lpVtbl->Release(cylinder);
+            return hr;
+        }
+    }
+
     *mesh = cylinder;
 
     return D3D_OK;
@@ -4929,18 +5171,18 @@ HRESULT WINAPI D3DXCreateTeapot(struct IDirect3DDevice9 *device,
 {
     FIXME("(%p, %p, %p): stub\n", device, mesh, adjacency);
 
-    return E_NOTIMPL;
+    return D3DXCreateSphere(device, 1.0f, 4, 4, mesh, adjacency);
 }
 
 HRESULT WINAPI D3DXCreateTextA(struct IDirect3DDevice9 *device, HDC hdc, const char *text, float deviation,
         float extrusion, struct ID3DXMesh **mesh, struct ID3DXBuffer **adjacency, GLYPHMETRICSFLOAT *glyphmetrics)
 {
+    WCHAR *textW;
     HRESULT hr;
     int len;
-    LPWSTR textW;
 
-    TRACE("(%p, %p, %s, %f, %f, %p, %p, %p)\n", device, hdc,
-          debugstr_a(text), deviation, extrusion, mesh, adjacency, glyphmetrics);
+    TRACE("device %p, hdc %p, text %s, deviation %.8e, extrusion %.8e, mesh %p, adjacency %p, glyphmetrics %p.\n",
+            device, hdc, debugstr_a(text), deviation, extrusion, mesh, adjacency, glyphmetrics);
 
     if (!text)
         return D3DERR_INVALIDCALL;
@@ -4956,6 +5198,113 @@ HRESULT WINAPI D3DXCreateTextA(struct IDirect3DDevice9 *device, HDC hdc, const c
     return hr;
 }
 
+HRESULT WINAPI D3DXCreateTorus(struct IDirect3DDevice9 *device,
+        float innerradius, float outerradius, UINT sides, UINT rings, struct ID3DXMesh **mesh, ID3DXBuffer **adjacency)
+{
+    HRESULT hr;
+    ID3DXMesh *torus;
+    WORD (*faces)[3];
+    struct vertex *vertices;
+    float phi, phi_step, sin_phi, cos_phi;
+    float theta, theta_step, sin_theta, cos_theta;
+    unsigned int i, j, numvert, numfaces;
+
+    TRACE("device %p, innerradius %.8e, outerradius %.8e, sides %u, rings %u, mesh %p, adjacency %p.\n",
+            device, innerradius, outerradius, sides, rings, mesh, adjacency);
+
+    numvert = sides * rings;
+    numfaces = numvert * 2;
+
+    if (!device || innerradius < 0.0f || outerradius < 0.0f || sides < 3 || rings < 3 || !mesh)
+    {
+        WARN("Invalid arguments.\n");
+        return D3DERR_INVALIDCALL;
+    }
+
+    if (FAILED(hr = D3DXCreateMeshFVF(numfaces, numvert, D3DXMESH_MANAGED, D3DFVF_XYZ | D3DFVF_NORMAL, device, &torus)))
+        return hr;
+
+    if (FAILED(hr = torus->lpVtbl->LockVertexBuffer(torus, 0, (void **)&vertices)))
+    {
+        torus->lpVtbl->Release(torus);
+        return hr;
+    }
+
+    if (FAILED(hr = torus->lpVtbl->LockIndexBuffer(torus, 0, (void **)&faces)))
+    {
+        torus->lpVtbl->UnlockVertexBuffer(torus);
+        torus->lpVtbl->Release(torus);
+        return hr;
+    }
+
+    phi_step = D3DX_PI / sides * 2.0f;
+    theta_step = D3DX_PI / rings * -2.0f;
+
+    theta = 0.0f;
+
+    for (i = 0; i < rings; ++i)
+    {
+        phi = 0.0f;
+
+        sin_theta = sinf(theta);
+        cos_theta = cosf(theta);
+
+        for (j = 0; j < sides; ++j)
+        {
+            sin_phi = sinf(phi);
+            cos_phi = cosf(phi);
+
+            vertices[i * sides + j].position.x = (innerradius * cos_phi + outerradius) * cos_theta;
+            vertices[i * sides + j].position.y = (innerradius * cos_phi + outerradius) * sin_theta;
+            vertices[i * sides + j].position.z = innerradius * sin_phi;
+            vertices[i * sides + j].normal.x = cos_phi * cos_theta;
+            vertices[i * sides + j].normal.y = cos_phi * sin_theta;
+            vertices[i * sides + j].normal.z = sin_phi;
+
+            phi += phi_step;
+        }
+
+        theta += theta_step;
+    }
+
+    for (i = 0; i < numfaces - sides * 2; ++i)
+    {
+        faces[i][0] = i % 2 ? i / 2 + sides : i / 2;
+        faces[i][1] = (i / 2 + 1) % sides ? i / 2 + 1 : i / 2 + 1 - sides;
+        faces[i][2] = (i + 1) % (sides * 2) ? (i + 1) / 2 + sides : (i + 1) / 2;
+    }
+
+    for (j = 0; i < numfaces; ++i, ++j)
+    {
+        faces[i][0] = i % 2 ? j / 2 : i / 2;
+        faces[i][1] = (i / 2 + 1) % sides ? i / 2 + 1 : i / 2 + 1 - sides;
+        faces[i][2] = i == numfaces - 1 ? 0 : (j + 1) / 2;
+    }
+
+    torus->lpVtbl->UnlockIndexBuffer(torus);
+    torus->lpVtbl->UnlockVertexBuffer(torus);
+
+    if (adjacency)
+    {
+        if (FAILED(hr = D3DXCreateBuffer(numfaces * sizeof(DWORD) * 3, adjacency)))
+        {
+            torus->lpVtbl->Release(torus);
+            return hr;
+        }
+
+        if (FAILED(hr = torus->lpVtbl->GenerateAdjacency(torus, 0.0f, (*adjacency)->lpVtbl->GetBufferPointer(*adjacency))))
+        {
+            (*adjacency)->lpVtbl->Release(*adjacency);
+            torus->lpVtbl->Release(torus);
+            return hr;
+        }
+    }
+
+    *mesh = torus;
+
+    return D3D_OK;
+}
+
 enum pointtype {
     POINTTYPE_CURVE = 0,
     POINTTYPE_CORNER,
@@ -5114,7 +5463,7 @@ static HRESULT add_vertex_index(struct word_array *array, WORD vertex_index)
 C_ASSERT(sizeof(FIXED) == sizeof(float));
 C_ASSERT(sizeof(POINTFX) == sizeof(D3DXVECTOR2));
 
-static inline D3DXVECTOR2 *convert_fixed_to_float(POINTFX *pt, int count, float emsquare)
+static inline D3DXVECTOR2 *convert_fixed_to_float(POINTFX *pt, int count, unsigned int emsquare)
 {
     D3DXVECTOR2 *ret = (D3DXVECTOR2*)pt;
     while (count--) {
@@ -5219,7 +5568,8 @@ static BOOL attempt_line_merge(struct outline *outline,
 }
 
 static HRESULT create_outline(struct glyphinfo *glyph, void *raw_outline, int datasize,
-                              float max_deviation_sq, float emsquare, const struct cos_table *cos_table)
+                              float max_deviation_sq, unsigned int emsquare,
+                              const struct cos_table *cos_table)
 {
     TTPOLYGONHEADER *header = (TTPOLYGONHEADER *)raw_outline;
 
@@ -5317,7 +5667,6 @@ static HRESULT create_outline(struct glyphinfo *glyph, void *raw_outline, int da
                         pt->corner = POINTTYPE_CURVE_END;
                 }
                 outline->count--;
-                lastpt = &outline->items[outline->count - 1];
             } else {
                 /* outline closed with a line from end to start point */
                 attempt_line_merge(outline, outline->count - 1, &pt->pos, FALSE, cos_table);
@@ -5515,6 +5864,10 @@ static HRESULT triangulate(struct triangulation_array *triangulations)
     int i;
     struct point2d_index *idx_ptr;
 
+    /* Glyphs without outlines do not generate any vertices. */
+    if (!glyph->outlines.count)
+        return D3D_OK;
+
     for (i = 0; i < glyph->outlines.count; i++)
         nb_vertices += glyph->outlines.items[i].count;
 
@@ -5763,9 +6116,9 @@ HRESULT WINAPI D3DXCreateTextW(struct IDirect3DDevice9 *device, HDC hdc, const W
     face *face_ptr;
     float max_deviation_sq;
     const struct cos_table cos_table = {
-        cos(D3DXToRadian(0.5f)),
-        cos(D3DXToRadian(45.0f)),
-        cos(D3DXToRadian(90.0f)),
+        cosf(D3DXToRadian(0.5f)),
+        cosf(D3DXToRadian(45.0f)),
+        cosf(D3DXToRadian(90.0f)),
     };
     int f1, f2;
 
@@ -5883,12 +6236,10 @@ HRESULT WINAPI D3DXCreateTextW(struct IDirect3DDevice9 *device, HDC hdc, const W
     if (FAILED(hr))
         goto error;
 
-    hr = mesh->lpVtbl->LockVertexBuffer(mesh, 0, (LPVOID *)&vertices);
-    if (FAILED(hr))
+    if (FAILED(hr = mesh->lpVtbl->LockVertexBuffer(mesh, 0, (void **)&vertices)))
         goto error;
 
-    hr = mesh->lpVtbl->LockIndexBuffer(mesh, 0, (LPVOID *)&faces);
-    if (FAILED(hr))
+    if (FAILED(hr = mesh->lpVtbl->LockIndexBuffer(mesh, 0, (void **)&faces)))
         goto error;
 
     /* convert 2D vertices and faces into 3D mesh */
@@ -6147,7 +6498,7 @@ static BOOL weld_float4(void *to, void *from, FLOAT epsilon)
     FLOAT diff_y = fabsf(v1->y - v2->y);
     FLOAT diff_z = fabsf(v1->z - v2->z);
     FLOAT diff_w = fabsf(v1->w - v2->w);
-    FLOAT max_abs_diff = fmax(diff_x, diff_y);
+    FLOAT max_abs_diff = max(diff_x, diff_y);
     max_abs_diff = max(diff_z, max_abs_diff);
     max_abs_diff = max(diff_w, max_abs_diff);
 
@@ -6386,9 +6737,9 @@ static BOOL weld_float16_2(void *to, void *from, FLOAT epsilon)
     FLOAT diff_x;
     FLOAT diff_y;
     FLOAT max_abs_diff;
-    const UINT NUM_ELEM = 2;
-    FLOAT v1[2];
-    FLOAT v2[2];
+#define NUM_ELEM 2
+    FLOAT v1[NUM_ELEM];
+    FLOAT v2[NUM_ELEM];
 
     D3DXFloat16To32Array(v1, v1_float16, NUM_ELEM);
     D3DXFloat16To32Array(v2, v2_float16, NUM_ELEM);
@@ -6405,6 +6756,7 @@ static BOOL weld_float16_2(void *to, void *from, FLOAT epsilon)
     }
 
     return FALSE;
+#undef NUM_ELEM
 }
 
 static BOOL weld_float16_4(void *to, void *from, FLOAT epsilon)
@@ -6416,9 +6768,9 @@ static BOOL weld_float16_4(void *to, void *from, FLOAT epsilon)
     FLOAT diff_z;
     FLOAT diff_w;
     FLOAT max_abs_diff;
-    const UINT NUM_ELEM = 4;
-    FLOAT v1[4];
-    FLOAT v2[4];
+#define NUM_ELEM 4
+    FLOAT v1[NUM_ELEM];
+    FLOAT v2[NUM_ELEM];
 
     D3DXFloat16To32Array(v1, v1_float16, NUM_ELEM);
     D3DXFloat16To32Array(v2, v2_float16, NUM_ELEM);
@@ -6439,6 +6791,7 @@ static BOOL weld_float16_4(void *to, void *from, FLOAT epsilon)
     }
 
     return FALSE;
+#undef NUM_ELEM
 }
 
 /* Sets the vertex components to the same value if they are within epsilon. */
@@ -6663,17 +7016,16 @@ HRESULT WINAPI D3DXWeldVertices(ID3DXMesh *mesh, DWORD flags, const D3DXWELDEPSI
     BOOL indices_are_32bit = mesh->lpVtbl->GetOptions(mesh) & D3DXMESH_32BIT;
     DWORD optimize_flags;
     DWORD *point_reps = NULL;
-    ID3DXMeshImpl *This = impl_from_ID3DXMesh(mesh);
+    struct d3dx9_mesh *This = impl_from_ID3DXMesh(mesh);
     DWORD *vertex_face_map = NULL;
-    ID3DXBuffer *vertex_remap = NULL;
     BYTE *vertices = NULL;
 
-    TRACE("(%p, %x, %p, %p, %p, %p, %p)\n", mesh, flags, epsilons,
-           adjacency, adjacency_out, face_remap_out, vertex_remap_out);
+    TRACE("mesh %p, flags %#x, epsilons %p, adjacency %p, adjacency_out %p, face_remap_out %p, vertex_remap_out %p.\n",
+            mesh, flags, epsilons, adjacency, adjacency_out, face_remap_out, vertex_remap_out);
 
     if (flags == 0)
     {
-        WARN("No flags is undefined. Using D3DXWELDEPSILONS_WELDPARTIALMATCHES instead.\n");
+        WARN("No flags are undefined. Using D3DXWELDEPSILONS_WELDPARTIALMATCHES instead.\n");
         flags = D3DXWELDEPSILONS_WELDPARTIALMATCHES;
     }
 
@@ -6832,12 +7184,38 @@ cleanup:
     HeapFree(GetProcessHeap(), 0, vertex_face_map);
     if (attributes) mesh->lpVtbl->UnlockAttributeBuffer(mesh);
     if (indices) mesh->lpVtbl->UnlockIndexBuffer(mesh);
-    if (vertex_remap) ID3DXBuffer_Release(vertex_remap);
     if (vertices) mesh->lpVtbl->UnlockVertexBuffer(mesh);
 
     return hr;
 }
 
+
+/*************************************************************************
+ * D3DXOptimizeVertices    (D3DX9_36.@)
+ */
+HRESULT WINAPI D3DXOptimizeVertices(const void *indices, UINT num_faces,
+        UINT num_vertices, BOOL indices_are_32bit, DWORD *vertex_remap)
+{
+    UINT i;
+
+    FIXME("indices %p, num_faces %u, num_vertices %u, indices_are_32bit %#x, vertex_remap %p semi-stub.\n",
+            indices, num_faces, num_vertices, indices_are_32bit, vertex_remap);
+
+    if (!vertex_remap)
+    {
+        WARN("vertex remap pointer is NULL.\n");
+        return D3DERR_INVALIDCALL;
+    }
+
+    for (i = 0; i < num_vertices; i++)
+    {
+        vertex_remap[i] = i;
+    }
+
+    return D3D_OK;
+}
+
+
 /*************************************************************************
  * D3DXOptimizeFaces    (D3DX9_36.@)
  *
@@ -6858,20 +7236,17 @@ cleanup:
  *   The face re-ordering does not use the vertex cache optimally.
  *
  */
-HRESULT WINAPI D3DXOptimizeFaces(LPCVOID indices,
-                                 UINT num_faces,
-                                 UINT num_vertices,
-                                 BOOL indices_are_32bit,
-                                 DWORD *face_remap)
+HRESULT WINAPI D3DXOptimizeFaces(const void *indices, UINT num_faces,
+        UINT num_vertices, BOOL indices_are_32bit, DWORD *face_remap)
 {
     UINT i;
     UINT j = num_faces - 1;
     UINT limit_16_bit = 2 << 15; /* According to MSDN */
     HRESULT hr = D3D_OK;
 
-    FIXME("(%p, %u, %u, %s, %p): semi-stub. Face order will not be optimal.\n",
-          indices, num_faces, num_vertices,
-          indices_are_32bit ? "TRUE" : "FALSE", face_remap);
+    FIXME("indices %p, num_faces %u, num_vertices %u, indices_are_32bit %#x, face_remap %p semi-stub. "
+            "Face order will not be optimal.\n",
+            indices, num_faces, num_vertices, indices_are_32bit, face_remap);
 
     if (!indices_are_32bit && num_faces >= limit_16_bit)
     {
@@ -6905,3 +7280,340 @@ HRESULT WINAPI D3DXOptimizeFaces(LPCVOID indices,
 error:
     return hr;
 }
+
+static D3DXVECTOR3 *vertex_element_vec3(BYTE *vertices, const D3DVERTEXELEMENT9 *declaration,
+        DWORD vertex_stride, DWORD index)
+{
+    return (D3DXVECTOR3 *)(vertices + declaration->Offset + index * vertex_stride);
+}
+
+static D3DXVECTOR3 read_vec3(BYTE *vertices, const D3DVERTEXELEMENT9 *declaration,
+        DWORD vertex_stride, DWORD index)
+{
+    D3DXVECTOR3 vec3 = {0};
+    const D3DXVECTOR3 *src = vertex_element_vec3(vertices, declaration, vertex_stride, index);
+
+    switch (declaration->Type)
+    {
+        case D3DDECLTYPE_FLOAT1:
+            vec3.x = src->x;
+            break;
+        case D3DDECLTYPE_FLOAT2:
+            vec3.x = src->x;
+            vec3.y = src->y;
+            break;
+        case D3DDECLTYPE_FLOAT3:
+        case D3DDECLTYPE_FLOAT4:
+            vec3 = *src;
+            break;
+        default:
+            ERR("Cannot read vec3\n");
+            break;
+    }
+
+    return vec3;
+}
+
+/*************************************************************************
+ * D3DXComputeTangentFrameEx    (D3DX9_36.@)
+ */
+HRESULT WINAPI D3DXComputeTangentFrameEx(ID3DXMesh *mesh, DWORD texture_in_semantic, DWORD texture_in_index,
+        DWORD u_partial_out_semantic, DWORD u_partial_out_index, DWORD v_partial_out_semantic,
+        DWORD v_partial_out_index, DWORD normal_out_semantic, DWORD normal_out_index, DWORD options,
+        const DWORD *adjacency, float partial_edge_threshold, float singular_point_threshold,
+        float normal_edge_threshold, ID3DXMesh **mesh_out, ID3DXBuffer **vertex_mapping)
+{
+    HRESULT hr;
+    void *indices = NULL;
+    BYTE *vertices = NULL;
+    DWORD *point_reps = NULL;
+    size_t normal_size;
+    BOOL indices_are_32bit;
+    DWORD i, j, num_faces, num_vertices, vertex_stride;
+    D3DVERTEXELEMENT9 declaration[MAX_FVF_DECL_SIZE] = {D3DDECL_END()};
+    D3DVERTEXELEMENT9 *position_declaration = NULL, *normal_declaration = NULL;
+    DWORD weighting_method = options & (D3DXTANGENT_WEIGHT_EQUAL | D3DXTANGENT_WEIGHT_BY_AREA);
+
+    TRACE("mesh %p, texture_in_semantic %u, texture_in_index %u, u_partial_out_semantic %u, u_partial_out_index %u, "
+            "v_partial_out_semantic %u, v_partial_out_index %u, normal_out_semantic %u, normal_out_index %u, "
+            "options %#x, adjacency %p, partial_edge_threshold %f, singular_point_threshold %f, "
+            "normal_edge_threshold %f, mesh_out %p, vertex_mapping %p\n",
+            mesh, texture_in_semantic, texture_in_index, u_partial_out_semantic, u_partial_out_index,
+            v_partial_out_semantic, v_partial_out_index, normal_out_semantic, normal_out_index, options, adjacency,
+            partial_edge_threshold, singular_point_threshold, normal_edge_threshold, mesh_out, vertex_mapping);
+
+    if (!mesh)
+    {
+        WARN("mesh is NULL\n");
+        return D3DERR_INVALIDCALL;
+    }
+
+    if (weighting_method == (D3DXTANGENT_WEIGHT_EQUAL | D3DXTANGENT_WEIGHT_BY_AREA))
+    {
+        WARN("D3DXTANGENT_WEIGHT_BY_AREA and D3DXTANGENT_WEIGHT_EQUAL are mutally exclusive\n");
+        return D3DERR_INVALIDCALL;
+    }
+
+    if (u_partial_out_semantic != D3DX_DEFAULT)
+    {
+        FIXME("tangent vectors computation is not supported\n");
+        return E_NOTIMPL;
+    }
+
+    if (v_partial_out_semantic != D3DX_DEFAULT)
+    {
+        FIXME("binormal vectors computation is not supported\n");
+        return E_NOTIMPL;
+    }
+
+    if (options & ~(D3DXTANGENT_GENERATE_IN_PLACE | D3DXTANGENT_CALCULATE_NORMALS | D3DXTANGENT_WEIGHT_EQUAL | D3DXTANGENT_WEIGHT_BY_AREA))
+    {
+        FIXME("unsupported options %#x\n", options);
+        return E_NOTIMPL;
+    }
+
+    if (!(options & D3DXTANGENT_CALCULATE_NORMALS))
+    {
+        FIXME("only normals computation is supported\n");
+        return E_NOTIMPL;
+    }
+
+    if (!(options & D3DXTANGENT_GENERATE_IN_PLACE) || mesh_out || vertex_mapping)
+    {
+        FIXME("only D3DXTANGENT_GENERATE_IN_PLACE is supported\n");
+        return E_NOTIMPL;
+    }
+
+    if (FAILED(hr = mesh->lpVtbl->GetDeclaration(mesh, declaration)))
+        return hr;
+
+    for (i = 0; declaration[i].Stream != 0xff; i++)
+    {
+        if (declaration[i].Usage == D3DDECLUSAGE_POSITION && !declaration[i].UsageIndex)
+            position_declaration = &declaration[i];
+        if (declaration[i].Usage == normal_out_semantic && declaration[i].UsageIndex == normal_out_index)
+            normal_declaration = &declaration[i];
+    }
+
+    if (!position_declaration || !normal_declaration)
+        return D3DERR_INVALIDCALL;
+
+    if (normal_declaration->Type == D3DDECLTYPE_FLOAT3)
+    {
+        normal_size = sizeof(D3DXVECTOR3);
+    }
+    else if (normal_declaration->Type == D3DDECLTYPE_FLOAT4)
+    {
+        normal_size = sizeof(D3DXVECTOR4);
+    }
+    else
+    {
+        WARN("unsupported normals type %u\n", normal_declaration->Type);
+        return D3DERR_INVALIDCALL;
+    }
+
+    num_faces = mesh->lpVtbl->GetNumFaces(mesh);
+    num_vertices = mesh->lpVtbl->GetNumVertices(mesh);
+    vertex_stride = mesh->lpVtbl->GetNumBytesPerVertex(mesh);
+    indices_are_32bit = mesh->lpVtbl->GetOptions(mesh) & D3DXMESH_32BIT;
+
+    point_reps = HeapAlloc(GetProcessHeap(), 0, num_vertices * sizeof(*point_reps));
+    if (!point_reps)
+    {
+        hr = E_OUTOFMEMORY;
+        goto done;
+    }
+
+    if (adjacency)
+    {
+        if (FAILED(hr = mesh->lpVtbl->ConvertAdjacencyToPointReps(mesh, adjacency, point_reps)))
+            goto done;
+    }
+    else
+    {
+        for (i = 0; i < num_vertices; i++)
+            point_reps[i] = i;
+    }
+
+    if (FAILED(hr = mesh->lpVtbl->LockIndexBuffer(mesh, 0, &indices)))
+        goto done;
+
+    if (FAILED(hr = mesh->lpVtbl->LockVertexBuffer(mesh, 0, (void **)&vertices)))
+        goto done;
+
+    for (i = 0; i < num_vertices; i++)
+    {
+        static const D3DXVECTOR4 default_vector = {0.0f, 0.0f, 0.0f, 1.0f};
+        void *normal = vertices + normal_declaration->Offset + i * vertex_stride;
+
+        memcpy(normal, &default_vector, normal_size);
+    }
+
+    for (i = 0; i < num_faces; i++)
+    {
+        float denominator, weights[3];
+        D3DXVECTOR3 a, b, cross, face_normal;
+        const DWORD face_indices[3] =
+        {
+            read_ib(indices, indices_are_32bit, 3 * i + 0),
+            read_ib(indices, indices_are_32bit, 3 * i + 1),
+            read_ib(indices, indices_are_32bit, 3 * i + 2)
+        };
+        const D3DXVECTOR3 v0 = read_vec3(vertices, position_declaration, vertex_stride, face_indices[0]);
+        const D3DXVECTOR3 v1 = read_vec3(vertices, position_declaration, vertex_stride, face_indices[1]);
+        const D3DXVECTOR3 v2 = read_vec3(vertices, position_declaration, vertex_stride, face_indices[2]);
+
+        D3DXVec3Cross(&cross, D3DXVec3Subtract(&a, &v0, &v1), D3DXVec3Subtract(&b, &v0, &v2));
+
+        switch (weighting_method)
+        {
+            case D3DXTANGENT_WEIGHT_EQUAL:
+                weights[0] = weights[1] = weights[2] = 1.0f;
+                break;
+            case D3DXTANGENT_WEIGHT_BY_AREA:
+                weights[0] = weights[1] = weights[2] = D3DXVec3Length(&cross);
+                break;
+            default:
+                /* weight by angle */
+                denominator = D3DXVec3Length(&a) * D3DXVec3Length(&b);
+                if (!denominator)
+                    weights[0] = 0.0f;
+                else
+                    weights[0] = acosf(D3DXVec3Dot(&a, &b) / denominator);
+
+                D3DXVec3Subtract(&a, &v1, &v0);
+                D3DXVec3Subtract(&b, &v1, &v2);
+                denominator = D3DXVec3Length(&a) * D3DXVec3Length(&b);
+                if (!denominator)
+                    weights[1] = 0.0f;
+                else
+                    weights[1] = acosf(D3DXVec3Dot(&a, &b) / denominator);
+
+                D3DXVec3Subtract(&a, &v2, &v0);
+                D3DXVec3Subtract(&b, &v2, &v1);
+                denominator = D3DXVec3Length(&a) * D3DXVec3Length(&b);
+                if (!denominator)
+                    weights[2] = 0.0f;
+                else
+                    weights[2] = acosf(D3DXVec3Dot(&a, &b) / denominator);
+
+                break;
+        }
+
+        D3DXVec3Normalize(&face_normal, &cross);
+
+        for (j = 0; j < 3; j++)
+        {
+            D3DXVECTOR3 normal;
+            DWORD rep_index = point_reps[face_indices[j]];
+            D3DXVECTOR3 *rep_normal = vertex_element_vec3(vertices, normal_declaration, vertex_stride, rep_index);
+
+            D3DXVec3Scale(&normal, &face_normal, weights[j]);
+            D3DXVec3Add(rep_normal, rep_normal, &normal);
+        }
+    }
+
+    for (i = 0; i < num_vertices; i++)
+    {
+        DWORD rep_index = point_reps[i];
+        D3DXVECTOR3 *normal = vertex_element_vec3(vertices, normal_declaration, vertex_stride, i);
+        D3DXVECTOR3 *rep_normal = vertex_element_vec3(vertices, normal_declaration, vertex_stride, rep_index);
+
+        if (i == rep_index)
+            D3DXVec3Normalize(rep_normal, rep_normal);
+        else
+            *normal = *rep_normal;
+    }
+
+    hr = D3D_OK;
+
+done:
+    if (vertices)
+        mesh->lpVtbl->UnlockVertexBuffer(mesh);
+
+    if (indices)
+        mesh->lpVtbl->UnlockIndexBuffer(mesh);
+
+    HeapFree(GetProcessHeap(), 0, point_reps);
+
+    return hr;
+}
+
+/*************************************************************************
+ * D3DXComputeTangent    (D3DX9_36.@)
+ */
+HRESULT WINAPI D3DXComputeTangent(ID3DXMesh *mesh, DWORD stage_idx, DWORD tangent_idx,
+        DWORD binorm_idx, DWORD wrap, const DWORD *adjacency)
+{
+    TRACE("mesh %p, stage_idx %d, tangent_idx %d, binorm_idx %d, wrap %d, adjacency %p.\n",
+           mesh, stage_idx, tangent_idx, binorm_idx, wrap, adjacency);
+
+    return D3DXComputeTangentFrameEx( mesh, D3DDECLUSAGE_TEXCOORD, stage_idx,
+            ( binorm_idx == D3DX_DEFAULT ) ? D3DX_DEFAULT : D3DDECLUSAGE_BINORMAL,
+            binorm_idx,
+            ( tangent_idx == D3DX_DEFAULT ) ? D3DX_DEFAULT : D3DDECLUSAGE_TANGENT,
+            tangent_idx, D3DX_DEFAULT, 0,
+            ( wrap ? D3DXTANGENT_WRAP_UV : 0 ) | D3DXTANGENT_GENERATE_IN_PLACE | D3DXTANGENT_ORTHOGONALIZE_FROM_U,
+            adjacency, -1.01f, -0.01f, -1.01f, NULL, NULL);
+}
+
+/*************************************************************************
+ * D3DXComputeNormals    (D3DX9_36.@)
+ */
+HRESULT WINAPI D3DXComputeNormals(struct ID3DXBaseMesh *mesh, const DWORD *adjacency)
+{
+    TRACE("mesh %p, adjacency %p\n", mesh, adjacency);
+
+    if (mesh && (ID3DXMeshVtbl *)mesh->lpVtbl != &D3DXMesh_Vtbl)
+    {
+        ERR("Invalid virtual table\n");
+        return D3DERR_INVALIDCALL;
+    }
+
+    return D3DXComputeTangentFrameEx((ID3DXMesh *)mesh, D3DX_DEFAULT, 0,
+            D3DX_DEFAULT, 0, D3DX_DEFAULT, 0, D3DDECLUSAGE_NORMAL, 0,
+            D3DXTANGENT_GENERATE_IN_PLACE | D3DXTANGENT_CALCULATE_NORMALS,
+            adjacency, -1.01f, -0.01f, -1.01f, NULL, NULL);
+}
+
+/*************************************************************************
+ * D3DXComputeNormalMap    (D3DX9_36.@)
+ */
+HRESULT WINAPI D3DXComputeNormalMap(IDirect3DTexture9 *texture, IDirect3DTexture9 *src_texture,
+        const PALETTEENTRY *src_palette, DWORD flags, DWORD channel, FLOAT amplitude)
+{
+    FIXME("texture %p, src_texture %p, src_palette %p, flags %#x, channel %u, amplitude %f stub.\n",
+            texture, src_texture, src_palette, flags, channel, amplitude);
+
+    return D3D_OK;
+}
+
+/*************************************************************************
+ * D3DXIntersect    (D3DX9_36.@)
+ */
+HRESULT WINAPI D3DXIntersect(ID3DXBaseMesh *mesh, const D3DXVECTOR3 *ray_pos, const D3DXVECTOR3 *ray_dir,
+        BOOL *hit, DWORD *face_index, float *u, float *v, float *distance, ID3DXBuffer **all_hits, DWORD *count_of_hits)
+{
+    FIXME("mesh %p, ray_pos %p, ray_dir %p, hit %p, face_index %p, u %p, v %p, distance %p, all_hits %p, "
+            "count_of_hits %p stub!\n", mesh, ray_pos, ray_dir, hit, face_index, u, v, distance, all_hits, count_of_hits);
+
+    return E_NOTIMPL;
+}
+
+HRESULT WINAPI D3DXTessellateNPatches(ID3DXMesh *mesh, const DWORD *adjacency_in, float num_segs,
+        BOOL quadratic_normals, ID3DXMesh **mesh_out, ID3DXBuffer **adjacency_out)
+{
+    FIXME("mesh %p, adjacency_in %p, num_segs %f, quadratic_normals %d, mesh_out %p, adjacency_out %p stub.\n",
+            mesh, adjacency_in, num_segs, quadratic_normals, mesh_out, adjacency_out);
+
+    return E_NOTIMPL;
+}
+
+HRESULT WINAPI D3DXConvertMeshSubsetToSingleStrip(struct ID3DXBaseMesh *mesh_in, DWORD attribute_id,
+        DWORD ib_flags, struct IDirect3DIndexBuffer9 **index_buffer, DWORD *index_count)
+{
+    FIXME("mesh_in %p, attribute_id %u, ib_flags %u, index_buffer %p, index_count %p stub.\n",
+            mesh_in, attribute_id, ib_flags, index_buffer, index_count);
+
+    return E_NOTIMPL;
+}