vertexdeclaration.c revision a3133ef29cdf3656735d569fd64e54c9286effc0
af062818b47340eef15700d2f0211576ba3506eevboxsync * IDirect3DVertexDeclaration9 implementation
af062818b47340eef15700d2f0211576ba3506eevboxsync * Copyright 2002-2003 Raphael Junqueira
af062818b47340eef15700d2f0211576ba3506eevboxsync * Jason Edmeades
af062818b47340eef15700d2f0211576ba3506eevboxsync * This library is free software; you can redistribute it and/or
af062818b47340eef15700d2f0211576ba3506eevboxsync * modify it under the terms of the GNU Lesser General Public
af062818b47340eef15700d2f0211576ba3506eevboxsync * License as published by the Free Software Foundation; either
af062818b47340eef15700d2f0211576ba3506eevboxsync * version 2.1 of the License, or (at your option) any later version.
af062818b47340eef15700d2f0211576ba3506eevboxsync * This library is distributed in the hope that it will be useful,
af062818b47340eef15700d2f0211576ba3506eevboxsync * but WITHOUT ANY WARRANTY; without even the implied warranty of
af062818b47340eef15700d2f0211576ba3506eevboxsync * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
af062818b47340eef15700d2f0211576ba3506eevboxsync * Lesser General Public License for more details.
af062818b47340eef15700d2f0211576ba3506eevboxsync * You should have received a copy of the GNU Lesser General Public
af062818b47340eef15700d2f0211576ba3506eevboxsync * License along with this library; if not, write to the Free Software
af062818b47340eef15700d2f0211576ba3506eevboxsync * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
b955672b950093ff7416d1269dd4d3b69983bd8fvboxsync * Sun LGPL Disclaimer: For the avoidance of doubt, except that if any license choice
b955672b950093ff7416d1269dd4d3b69983bd8fvboxsync * other than GPL or LGPL is available it will apply instead, Sun elects to use only
b955672b950093ff7416d1269dd4d3b69983bd8fvboxsync * the Lesser General Public License version 2.1 (LGPLv2) at this time for any software where
b955672b950093ff7416d1269dd4d3b69983bd8fvboxsync * a choice of LGPL license versions is made available with the language indicating
b955672b950093ff7416d1269dd4d3b69983bd8fvboxsync * that LGPLv2 or any later version may be used, or where a choice of which version
b955672b950093ff7416d1269dd4d3b69983bd8fvboxsync * of the LGPL is applied is otherwise unspecified.
af062818b47340eef15700d2f0211576ba3506eevboxsynctypedef struct _D3DDECLTYPE_INFO {
af062818b47340eef15700d2f0211576ba3506eevboxsyncstatic D3DDECLTYPE_INFO const d3d_dtype_lookup[D3DDECLTYPE_UNUSED] = {
589fd26cedb2b4ebbed14f2964cad03cc8ebbca2vboxsync {D3DDECLTYPE_FLOAT1, WINED3DFMT_R32_FLOAT, 1, sizeof(float)},
589fd26cedb2b4ebbed14f2964cad03cc8ebbca2vboxsync {D3DDECLTYPE_FLOAT2, WINED3DFMT_R32G32_FLOAT, 2, sizeof(float)},
589fd26cedb2b4ebbed14f2964cad03cc8ebbca2vboxsync {D3DDECLTYPE_FLOAT3, WINED3DFMT_R32G32B32_FLOAT, 3, sizeof(float)},
589fd26cedb2b4ebbed14f2964cad03cc8ebbca2vboxsync {D3DDECLTYPE_FLOAT4, WINED3DFMT_R32G32B32A32_FLOAT, 4, sizeof(float)},
a3133ef29cdf3656735d569fd64e54c9286effc0vboxsync {D3DDECLTYPE_D3DCOLOR, WINED3DFMT_B8G8R8A8_UNORM, 4, sizeof(BYTE)},
589fd26cedb2b4ebbed14f2964cad03cc8ebbca2vboxsync {D3DDECLTYPE_UBYTE4, WINED3DFMT_R8G8B8A8_UINT, 4, sizeof(BYTE)},
589fd26cedb2b4ebbed14f2964cad03cc8ebbca2vboxsync {D3DDECLTYPE_SHORT2, WINED3DFMT_R16G16_SINT, 2, sizeof(short int)},
589fd26cedb2b4ebbed14f2964cad03cc8ebbca2vboxsync {D3DDECLTYPE_SHORT4, WINED3DFMT_R16G16B16A16_SINT, 4, sizeof(short int)},
589fd26cedb2b4ebbed14f2964cad03cc8ebbca2vboxsync {D3DDECLTYPE_UBYTE4N, WINED3DFMT_R8G8B8A8_UNORM, 4, sizeof(BYTE)},
589fd26cedb2b4ebbed14f2964cad03cc8ebbca2vboxsync {D3DDECLTYPE_SHORT2N, WINED3DFMT_R16G16_SNORM, 2, sizeof(short int)},
589fd26cedb2b4ebbed14f2964cad03cc8ebbca2vboxsync {D3DDECLTYPE_SHORT4N, WINED3DFMT_R16G16B16A16_SNORM, 4, sizeof(short int)},
589fd26cedb2b4ebbed14f2964cad03cc8ebbca2vboxsync {D3DDECLTYPE_USHORT2N, WINED3DFMT_R16G16_UNORM, 2, sizeof(short int)},
589fd26cedb2b4ebbed14f2964cad03cc8ebbca2vboxsync {D3DDECLTYPE_USHORT4N, WINED3DFMT_R16G16B16A16_UNORM, 4, sizeof(short int)},
589fd26cedb2b4ebbed14f2964cad03cc8ebbca2vboxsync {D3DDECLTYPE_UDEC3, WINED3DFMT_R10G10B10A2_UINT, 3, sizeof(short int)},
589fd26cedb2b4ebbed14f2964cad03cc8ebbca2vboxsync {D3DDECLTYPE_DEC3N, WINED3DFMT_R10G10B10A2_SNORM, 3, sizeof(short int)},
589fd26cedb2b4ebbed14f2964cad03cc8ebbca2vboxsync {D3DDECLTYPE_FLOAT16_2, WINED3DFMT_R16G16_FLOAT, 2, sizeof(short int)},
589fd26cedb2b4ebbed14f2964cad03cc8ebbca2vboxsync {D3DDECLTYPE_FLOAT16_4, WINED3DFMT_R16G16B16A16_FLOAT, 4, sizeof(short int)}};
af062818b47340eef15700d2f0211576ba3506eevboxsync#define D3D_DECL_SIZE(type) d3d_dtype_lookup[type].size
af062818b47340eef15700d2f0211576ba3506eevboxsync#define D3D_DECL_TYPESIZE(type) d3d_dtype_lookup[type].typesize
af062818b47340eef15700d2f0211576ba3506eevboxsync unsigned int offset;
af062818b47340eef15700d2f0211576ba3506eevboxsync BOOL has_blend = (fvf & D3DFVF_XYZB5) > D3DFVF_XYZRHW;
af062818b47340eef15700d2f0211576ba3506eevboxsync DWORD num_textures = (fvf & D3DFVF_TEXCOUNT_MASK) >> D3DFVF_TEXCOUNT_SHIFT;
af062818b47340eef15700d2f0211576ba3506eevboxsync unsigned int size;
af062818b47340eef15700d2f0211576ba3506eevboxsync DWORD num_blends = 1 + (((fvf & D3DFVF_XYZB5) - D3DFVF_XYZB1) >> 1);
af062818b47340eef15700d2f0211576ba3506eevboxsync /* Compute declaration size */
af062818b47340eef15700d2f0211576ba3506eevboxsync size = has_pos + (has_blend && num_blends > 0) + has_blend_idx + has_normal +
af062818b47340eef15700d2f0211576ba3506eevboxsync has_psize + has_diffuse + has_specular + num_textures + 1;
af062818b47340eef15700d2f0211576ba3506eevboxsync /* convert the declaration */
af062818b47340eef15700d2f0211576ba3506eevboxsync elements = HeapAlloc(GetProcessHeap(), 0, size * sizeof(D3DVERTEXELEMENT9));
589fd26cedb2b4ebbed14f2964cad03cc8ebbca2vboxsync else if (!has_blend && (fvf & D3DFVF_XYZW) == D3DFVF_XYZW) {
af062818b47340eef15700d2f0211576ba3506eevboxsync if (((fvf & D3DFVF_XYZB5) == D3DFVF_XYZB2) && (fvf & D3DFVF_LASTBETA_D3DCOLOR))
af062818b47340eef15700d2f0211576ba3506eevboxsync case 1: elements[idx].Type = D3DDECLTYPE_FLOAT1; break;
af062818b47340eef15700d2f0211576ba3506eevboxsync case 2: elements[idx].Type = D3DDECLTYPE_FLOAT2; break;
af062818b47340eef15700d2f0211576ba3506eevboxsync case 3: elements[idx].Type = D3DDECLTYPE_FLOAT3; break;
af062818b47340eef15700d2f0211576ba3506eevboxsync case 4: elements[idx].Type = D3DDECLTYPE_FLOAT4; break;
af062818b47340eef15700d2f0211576ba3506eevboxsync ERR("Unexpected amount of blend values: %u\n", num_blends);
af062818b47340eef15700d2f0211576ba3506eevboxsync (((fvf & D3DFVF_XYZB5) == D3DFVF_XYZB2) && (fvf & D3DFVF_LASTBETA_D3DCOLOR)))
af062818b47340eef15700d2f0211576ba3506eevboxsync unsigned int numcoords = (texcoords >> (idx2*2)) & 0x03;
af062818b47340eef15700d2f0211576ba3506eevboxsync /* Now compute offsets, and initialize the rest of the fields */
af062818b47340eef15700d2f0211576ba3506eevboxsync offset += D3D_DECL_SIZE(elements[idx].Type) * D3D_DECL_TYPESIZE(elements[idx].Type);
af062818b47340eef15700d2f0211576ba3506eevboxsync/* IDirect3DVertexDeclaration9 IUnknown parts follow: */
af062818b47340eef15700d2f0211576ba3506eevboxsyncstatic HRESULT WINAPI IDirect3DVertexDeclaration9Impl_QueryInterface(LPDIRECT3DVERTEXDECLARATION9 iface, REFIID riid, LPVOID* ppobj) {
af062818b47340eef15700d2f0211576ba3506eevboxsync IDirect3DVertexDeclaration9Impl *This = (IDirect3DVertexDeclaration9Impl *)iface;
af062818b47340eef15700d2f0211576ba3506eevboxsync || IsEqualGUID(riid, &IID_IDirect3DVertexDeclaration9)) {
af062818b47340eef15700d2f0211576ba3506eevboxsync WARN("(%p)->(%s,%p),not found\n", This, debugstr_guid(riid), ppobj);
af062818b47340eef15700d2f0211576ba3506eevboxsyncstatic ULONG WINAPI IDirect3DVertexDeclaration9Impl_AddRef(LPDIRECT3DVERTEXDECLARATION9 iface) {
af062818b47340eef15700d2f0211576ba3506eevboxsync IDirect3DVertexDeclaration9Impl *This = (IDirect3DVertexDeclaration9Impl *)iface;
a3133ef29cdf3656735d569fd64e54c9286effc0vboxsync IWineD3DVertexDeclaration_AddRef(This->wineD3DVertexDeclaration);
af062818b47340eef15700d2f0211576ba3506eevboxsyncvoid IDirect3DVertexDeclaration9Impl_Destroy(LPDIRECT3DVERTEXDECLARATION9 iface) {
af062818b47340eef15700d2f0211576ba3506eevboxsync IDirect3DVertexDeclaration9Impl *This = (IDirect3DVertexDeclaration9Impl *)iface;
af062818b47340eef15700d2f0211576ba3506eevboxsync /* Should not happen unless wine has a bug or the application releases references it does not own */
af062818b47340eef15700d2f0211576ba3506eevboxsync IWineD3DVertexDeclaration_Release(This->wineD3DVertexDeclaration);
af062818b47340eef15700d2f0211576ba3506eevboxsyncstatic ULONG WINAPI IDirect3DVertexDeclaration9Impl_Release(LPDIRECT3DVERTEXDECLARATION9 iface) {
af062818b47340eef15700d2f0211576ba3506eevboxsync IDirect3DVertexDeclaration9Impl *This = (IDirect3DVertexDeclaration9Impl *)iface;
af062818b47340eef15700d2f0211576ba3506eevboxsync if (ref == 0) {
af062818b47340eef15700d2f0211576ba3506eevboxsync/* IDirect3DVertexDeclaration9 Interface follow: */
af062818b47340eef15700d2f0211576ba3506eevboxsyncstatic HRESULT WINAPI IDirect3DVertexDeclaration9Impl_GetDevice(LPDIRECT3DVERTEXDECLARATION9 iface, IDirect3DDevice9** ppDevice) {
af062818b47340eef15700d2f0211576ba3506eevboxsync IDirect3DVertexDeclaration9Impl *This = (IDirect3DVertexDeclaration9Impl *)iface;
af062818b47340eef15700d2f0211576ba3506eevboxsync hr = IWineD3DVertexDeclaration_GetDevice(This->wineD3DVertexDeclaration, &myDevice);
af062818b47340eef15700d2f0211576ba3506eevboxsync hr = IWineD3DDevice_GetParent(myDevice, (IUnknown **)ppDevice);
af062818b47340eef15700d2f0211576ba3506eevboxsyncstatic HRESULT WINAPI IDirect3DVertexDeclaration9Impl_GetDeclaration(LPDIRECT3DVERTEXDECLARATION9 iface, D3DVERTEXELEMENT9* pDecl, UINT* pNumElements) {
af062818b47340eef15700d2f0211576ba3506eevboxsync IDirect3DVertexDeclaration9Impl *This = (IDirect3DVertexDeclaration9Impl *)iface;
af062818b47340eef15700d2f0211576ba3506eevboxsync TRACE("(%p) : pDecl %p, pNumElements %p)\n", This, pDecl, pNumElements);
af062818b47340eef15700d2f0211576ba3506eevboxsync /* Passing a NULL pDecl is used to just retrieve the number of elements */
af062818b47340eef15700d2f0211576ba3506eevboxsync TRACE("Copying %p to %p\n", This->elements, pDecl);
af062818b47340eef15700d2f0211576ba3506eevboxsync CopyMemory(pDecl, This->elements, This->element_count * sizeof(D3DVERTEXELEMENT9));
af062818b47340eef15700d2f0211576ba3506eevboxsyncstatic const IDirect3DVertexDeclaration9Vtbl Direct3DVertexDeclaration9_Vtbl =
af062818b47340eef15700d2f0211576ba3506eevboxsync /* IUnknown */
af062818b47340eef15700d2f0211576ba3506eevboxsync /* IDirect3DVertexDeclaration9 */
a3133ef29cdf3656735d569fd64e54c9286effc0vboxsyncstatic void STDMETHODCALLTYPE d3d9_vertexdeclaration_wined3d_object_destroyed(void *parent)
a3133ef29cdf3656735d569fd64e54c9286effc0vboxsync IDirect3DVertexDeclaration9Impl *declaration = parent;
a3133ef29cdf3656735d569fd64e54c9286effc0vboxsync HeapFree(GetProcessHeap(), 0, declaration->elements);
a3133ef29cdf3656735d569fd64e54c9286effc0vboxsyncstatic const struct wined3d_parent_ops d3d9_vertexdeclaration_wined3d_parent_ops =
589fd26cedb2b4ebbed14f2964cad03cc8ebbca2vboxsyncstatic HRESULT convert_to_wined3d_declaration(const D3DVERTEXELEMENT9* d3d9_elements,
589fd26cedb2b4ebbed14f2964cad03cc8ebbca2vboxsync WINED3DVERTEXELEMENT **wined3d_elements, UINT *element_count)
af062818b47340eef15700d2f0211576ba3506eevboxsync TRACE("d3d9_elements %p, wined3d_elements %p\n", d3d9_elements, wined3d_elements);
589fd26cedb2b4ebbed14f2964cad03cc8ebbca2vboxsync while (element++->Stream != 0xff && count++ < 128);
589fd26cedb2b4ebbed14f2964cad03cc8ebbca2vboxsync /* Skip the END element */
589fd26cedb2b4ebbed14f2964cad03cc8ebbca2vboxsync *wined3d_elements = HeapAlloc(GetProcessHeap(), 0, count * sizeof(WINED3DVERTEXELEMENT));
589fd26cedb2b4ebbed14f2964cad03cc8ebbca2vboxsync for (i = 0; i < count; ++i)
589fd26cedb2b4ebbed14f2964cad03cc8ebbca2vboxsync if (d3d9_elements[i].Type >= (sizeof(d3d_dtype_lookup) / sizeof(*d3d_dtype_lookup)))
589fd26cedb2b4ebbed14f2964cad03cc8ebbca2vboxsync WARN("Invalid element type %#x.\n", d3d9_elements[i].Type);
589fd26cedb2b4ebbed14f2964cad03cc8ebbca2vboxsync (*wined3d_elements)[i].format = d3d_dtype_lookup[d3d9_elements[i].Type].format;
589fd26cedb2b4ebbed14f2964cad03cc8ebbca2vboxsync (*wined3d_elements)[i].input_slot = d3d9_elements[i].Stream;
589fd26cedb2b4ebbed14f2964cad03cc8ebbca2vboxsync (*wined3d_elements)[i].offset = d3d9_elements[i].Offset;
589fd26cedb2b4ebbed14f2964cad03cc8ebbca2vboxsync (*wined3d_elements)[i].method = d3d9_elements[i].Method;
589fd26cedb2b4ebbed14f2964cad03cc8ebbca2vboxsync (*wined3d_elements)[i].usage = d3d9_elements[i].Usage;
589fd26cedb2b4ebbed14f2964cad03cc8ebbca2vboxsync (*wined3d_elements)[i].usage_idx = d3d9_elements[i].UsageIndex;
a3133ef29cdf3656735d569fd64e54c9286effc0vboxsyncHRESULT vertexdeclaration_init(IDirect3DVertexDeclaration9Impl *declaration,
a3133ef29cdf3656735d569fd64e54c9286effc0vboxsync IDirect3DDevice9Impl *device, const D3DVERTEXELEMENT9 *elements)
a3133ef29cdf3656735d569fd64e54c9286effc0vboxsync hr = convert_to_wined3d_declaration(elements, &wined3d_elements, &wined3d_element_count);
a3133ef29cdf3656735d569fd64e54c9286effc0vboxsync WARN("Failed to create wined3d vertex declaration elements, hr %#x.\n", hr);
a3133ef29cdf3656735d569fd64e54c9286effc0vboxsync declaration->lpVtbl = &Direct3DVertexDeclaration9_Vtbl;
a3133ef29cdf3656735d569fd64e54c9286effc0vboxsync declaration->elements = HeapAlloc(GetProcessHeap(), 0, element_count * sizeof(*declaration->elements));
a3133ef29cdf3656735d569fd64e54c9286effc0vboxsync ERR("Failed to allocate vertex declaration elements memory.\n");
a3133ef29cdf3656735d569fd64e54c9286effc0vboxsync memcpy(declaration->elements, elements, element_count * sizeof(*elements));
a3133ef29cdf3656735d569fd64e54c9286effc0vboxsync hr = IWineD3DDevice_CreateVertexDeclaration(device->WineD3DDevice, &declaration->wineD3DVertexDeclaration,
a3133ef29cdf3656735d569fd64e54c9286effc0vboxsync (IUnknown *)declaration, &d3d9_vertexdeclaration_wined3d_parent_ops,
a3133ef29cdf3656735d569fd64e54c9286effc0vboxsync WARN("Failed to create wined3d vertex declaration, hr %#x.\n", hr);
a3133ef29cdf3656735d569fd64e54c9286effc0vboxsync declaration->parentDevice = (IDirect3DDevice9Ex *)device;
a3133ef29cdf3656735d569fd64e54c9286effc0vboxsync IDirect3DDevice9Ex_AddRef(declaration->parentDevice);
af062818b47340eef15700d2f0211576ba3506eevboxsyncHRESULT WINAPI IDirect3DDevice9Impl_SetVertexDeclaration(LPDIRECT3DDEVICE9EX iface, IDirect3DVertexDeclaration9* pDecl) {
af062818b47340eef15700d2f0211576ba3506eevboxsync IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
af062818b47340eef15700d2f0211576ba3506eevboxsync IDirect3DVertexDeclaration9Impl *pDeclImpl = (IDirect3DVertexDeclaration9Impl *)pDecl;
af062818b47340eef15700d2f0211576ba3506eevboxsync hr = IWineD3DDevice_SetVertexDeclaration(This->WineD3DDevice, pDeclImpl == NULL ? NULL : pDeclImpl->wineD3DVertexDeclaration);
af062818b47340eef15700d2f0211576ba3506eevboxsyncHRESULT WINAPI IDirect3DDevice9Impl_GetVertexDeclaration(LPDIRECT3DDEVICE9EX iface, IDirect3DVertexDeclaration9** ppDecl) {
af062818b47340eef15700d2f0211576ba3506eevboxsync IDirect3DDevice9Impl* This = (IDirect3DDevice9Impl*) iface;
af062818b47340eef15700d2f0211576ba3506eevboxsync hr = IWineD3DDevice_GetVertexDeclaration(This->WineD3DDevice, &pTest);