vertexdeclaration.c 10.2 KB
Newer Older
1 2 3 4 5 6
/*
 * vertex declaration implementation
 *
 * Copyright 2002-2005 Raphael Junqueira
 * Copyright 2004 Jason Edmeades
 * Copyright 2004 Christian Costa
7
 * Copyright 2005 Oliver Stieber
8
 * Copyright 2009 Henri Verbeet for CodeWeavers
9 10 11 12 13 14 15 16 17 18 19 20 21
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
22
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 24 25 26 27 28 29
 */

#include "config.h"
#include "wined3d_private.h"

WINE_DEFAULT_DEBUG_CHANNEL(d3d_decl);

30
static void dump_wined3dvertexelement(const WINED3DVERTEXELEMENT *element) {
31 32 33 34 35 36 37
    TRACE("     format: %s (%#x)\n", debug_d3dformat(element->format), element->format);
    TRACE(" input_slot: %u\n", element->input_slot);
    TRACE("     offset: %u\n", element->offset);
    TRACE("output_slot: %u\n", element->output_slot);
    TRACE("     method: %s (%#x)\n", debug_d3ddeclmethod(element->method), element->method);
    TRACE("      usage: %s (%#x)\n", debug_d3ddeclusage(element->usage), element->usage);
    TRACE("  usage_idx: %u\n", element->usage_idx);
38 39
}

40 41 42
/* *******************************************
   IWineD3DVertexDeclaration IUnknown parts follow
   ******************************************* */
43
static HRESULT WINAPI IWineD3DVertexDeclarationImpl_QueryInterface(IWineD3DVertexDeclaration *iface, REFIID riid, LPVOID *ppobj)
44 45
{
    IWineD3DVertexDeclarationImpl *This = (IWineD3DVertexDeclarationImpl *)iface;
46 47
    TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppobj);
    if (IsEqualGUID(riid, &IID_IUnknown)
48
        || IsEqualGUID(riid, &IID_IWineD3DBase)
49 50 51
        || IsEqualGUID(riid, &IID_IWineD3DVertexDeclaration)){
        IUnknown_AddRef(iface);
        *ppobj = This;
52
        return S_OK;
53
    }
54
    *ppobj = NULL;
55 56 57
    return E_NOINTERFACE;
}

58
static ULONG WINAPI IWineD3DVertexDeclarationImpl_AddRef(IWineD3DVertexDeclaration *iface) {
59
    IWineD3DVertexDeclarationImpl *This = (IWineD3DVertexDeclarationImpl *)iface;
60
    TRACE("(%p) : AddRef increasing from %d\n", This, This->ref);
61 62 63
    return InterlockedIncrement(&This->ref);
}

64
static ULONG WINAPI IWineD3DVertexDeclarationImpl_Release(IWineD3DVertexDeclaration *iface) {
65 66
    IWineD3DVertexDeclarationImpl *This = (IWineD3DVertexDeclarationImpl *)iface;
    ULONG ref;
67
    TRACE("(%p) : Releasing from %d\n", This, This->ref);
68
    ref = InterlockedDecrement(&This->ref);
69 70
    if (!ref)
    {
71
        HeapFree(GetProcessHeap(), 0, This->elements);
72
        This->parent_ops->wined3d_object_destroyed(This->parent);
73
        HeapFree(GetProcessHeap(), 0, This);
74 75 76 77 78 79 80 81
    }
    return ref;
}

/* *******************************************
   IWineD3DVertexDeclaration parts follow
   ******************************************* */

82
static HRESULT WINAPI IWineD3DVertexDeclarationImpl_GetParent(IWineD3DVertexDeclaration *iface, IUnknown** parent){
83 84
    IWineD3DVertexDeclarationImpl *This = (IWineD3DVertexDeclarationImpl *)iface;

85
    *parent= This->parent;
86 87
    IUnknown_AddRef(*parent);
    TRACE("(%p) : returning %p\n", This, *parent);
88
    return WINED3D_OK;
89 90
}

91
static HRESULT WINAPI IWineD3DVertexDeclarationImpl_GetDevice(IWineD3DVertexDeclaration *iface, IWineD3DDevice** ppDevice) {
92 93
    IWineD3DVertexDeclarationImpl *This = (IWineD3DVertexDeclarationImpl *)iface;
    TRACE("(%p) : returning %p\n", This, This->wineD3DDevice);
94

95 96 97
    *ppDevice = (IWineD3DDevice *) This->wineD3DDevice;
    IWineD3DDevice_AddRef(*ppDevice);

98
    return WINED3D_OK;
99 100
}

101 102
static BOOL declaration_element_valid_ffp(const WINED3DVERTEXELEMENT *element)
{
103
    switch(element->usage)
104 105 106
    {
        case WINED3DDECLUSAGE_POSITION:
        case WINED3DDECLUSAGE_POSITIONT:
107
            switch(element->format)
108
            {
109 110 111 112 113 114 115
                case WINED3DFMT_R32G32_FLOAT:
                case WINED3DFMT_R32G32B32_FLOAT:
                case WINED3DFMT_R32G32B32A32_FLOAT:
                case WINED3DFMT_R16G16_SINT:
                case WINED3DFMT_R16G16B16A16_SINT:
                case WINED3DFMT_R16G16_FLOAT:
                case WINED3DFMT_R16G16B16A16_FLOAT:
116 117 118 119 120 121
                    return TRUE;
                default:
                    return FALSE;
            }

        case WINED3DDECLUSAGE_BLENDWEIGHT:
122
            switch(element->format)
123
            {
124 125 126 127
                case WINED3DFMT_R32_FLOAT:
                case WINED3DFMT_R32G32_FLOAT:
                case WINED3DFMT_R32G32B32_FLOAT:
                case WINED3DFMT_R32G32B32A32_FLOAT:
128
                case WINED3DFMT_B8G8R8A8_UNORM:
129 130 131 132 133
                case WINED3DFMT_R8G8B8A8_UINT:
                case WINED3DFMT_R16G16_SINT:
                case WINED3DFMT_R16G16B16A16_SINT:
                case WINED3DFMT_R16G16_FLOAT:
                case WINED3DFMT_R16G16B16A16_FLOAT:
134 135 136 137 138 139
                    return TRUE;
                default:
                    return FALSE;
            }

        case WINED3DDECLUSAGE_NORMAL:
140
            switch(element->format)
141
            {
142 143 144 145
                case WINED3DFMT_R32G32B32_FLOAT:
                case WINED3DFMT_R32G32B32A32_FLOAT:
                case WINED3DFMT_R16G16B16A16_SINT:
                case WINED3DFMT_R16G16B16A16_FLOAT:
146 147 148 149 150 151
                    return TRUE;
                default:
                    return FALSE;
            }

        case WINED3DDECLUSAGE_TEXCOORD:
152
            switch(element->format)
153
            {
154 155 156 157 158 159 160 161
                case WINED3DFMT_R32_FLOAT:
                case WINED3DFMT_R32G32_FLOAT:
                case WINED3DFMT_R32G32B32_FLOAT:
                case WINED3DFMT_R32G32B32A32_FLOAT:
                case WINED3DFMT_R16G16_SINT:
                case WINED3DFMT_R16G16B16A16_SINT:
                case WINED3DFMT_R16G16_FLOAT:
                case WINED3DFMT_R16G16B16A16_FLOAT:
162 163 164 165 166 167
                    return TRUE;
                default:
                    return FALSE;
            }

        case WINED3DDECLUSAGE_COLOR:
168
            switch(element->format)
169
            {
170 171
                case WINED3DFMT_R32G32B32_FLOAT:
                case WINED3DFMT_R32G32B32A32_FLOAT:
172
                case WINED3DFMT_B8G8R8A8_UNORM:
173 174 175 176 177 178
                case WINED3DFMT_R8G8B8A8_UINT:
                case WINED3DFMT_R16G16B16A16_SINT:
                case WINED3DFMT_R8G8B8A8_UNORM:
                case WINED3DFMT_R16G16B16A16_SNORM:
                case WINED3DFMT_R16G16B16A16_UNORM:
                case WINED3DFMT_R16G16B16A16_FLOAT:
179 180 181 182 183 184 185 186 187 188
                    return TRUE;
                default:
                    return FALSE;
            }

        default:
            return FALSE;
    }
}

189
static const IWineD3DVertexDeclarationVtbl IWineD3DVertexDeclaration_Vtbl =
190
{
191 192 193 194 195 196 197 198
    /* IUnknown */
    IWineD3DVertexDeclarationImpl_QueryInterface,
    IWineD3DVertexDeclarationImpl_AddRef,
    IWineD3DVertexDeclarationImpl_Release,
    /* IWineD3DVertexDeclaration */
    IWineD3DVertexDeclarationImpl_GetParent,
    IWineD3DVertexDeclarationImpl_GetDevice,
};
199

200
HRESULT vertexdeclaration_init(IWineD3DVertexDeclarationImpl *declaration, IWineD3DDeviceImpl *device,
201 202
        const WINED3DVERTEXELEMENT *elements, UINT element_count,
        IUnknown *parent, const struct wined3d_parent_ops *parent_ops)
203 204 205 206
{
    const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
    WORD preloaded = 0; /* MAX_STREAMS, 16 */
    unsigned int i;
207

208 209 210 211 212
    if (TRACE_ON(d3d_decl))
    {
        for (i = 0; i < element_count; ++i)
        {
            dump_wined3dvertexelement(elements + i);
213
        }
214
    }
215

216 217 218
    declaration->lpVtbl = &IWineD3DVertexDeclaration_Vtbl;
    declaration->ref = 1;
    declaration->parent = parent;
219
    declaration->parent_ops = parent_ops;
220 221 222
    declaration->wineD3DDevice = device;
    declaration->elements = HeapAlloc(GetProcessHeap(), 0, sizeof(*declaration->elements) * element_count);
    if (!declaration->elements)
223
    {
224 225
        ERR("Failed to allocate elements memory.\n");
        return E_OUTOFMEMORY;
226
    }
227
    declaration->element_count = element_count;
228

229 230 231 232 233
    /* Do some static analysis on the elements to make reading the
     * declaration more comfortable for the drawing code. */
    for (i = 0; i < element_count; ++i)
    {
        struct wined3d_vertex_declaration_element *e = &declaration->elements[i];
234

235
        e->format_desc = getFormatDescEntry(elements[i].format, gl_info);
236
        e->ffp_valid = declaration_element_valid_ffp(&elements[i]);
237 238 239 240 241 242
        e->input_slot = elements[i].input_slot;
        e->offset = elements[i].offset;
        e->output_slot = elements[i].output_slot;
        e->method = elements[i].method;
        e->usage = elements[i].usage;
        e->usage_idx = elements[i].usage_idx;
243

244
        if (e->usage == WINED3DDECLUSAGE_POSITIONT) declaration->position_transformed = TRUE;
245

246 247
        /* Find the streams used in the declaration. The vertex buffers have
         * to be loaded when drawing, but filter tesselation pseudo streams. */
248
        if (e->input_slot >= MAX_STREAMS) continue;
249

250
        if (!e->format_desc->gl_vtx_format)
251
        {
252
            FIXME("The application tries to use an unsupported format (%s), returning E_FAIL.\n",
253
                    debug_d3dformat(elements[i].format));
254
            HeapFree(GetProcessHeap(), 0, declaration->elements);
255 256 257
            return E_FAIL;
        }

258 259
        if (e->offset & 0x3)
        {
260 261
            WARN("Declaration element %u is not 4 byte aligned(%u), returning E_FAIL.\n", i, e->offset);
            HeapFree(GetProcessHeap(), 0, declaration->elements);
262 263 264
            return E_FAIL;
        }

265
        if (!(preloaded & (1 << e->input_slot)))
266
        {
267 268 269
            declaration->streams[declaration->num_streams] = e->input_slot;
            ++declaration->num_streams;
            preloaded |= 1 << e->input_slot;
270
        }
271

272
        if (elements[i].format == WINED3DFMT_R16G16_FLOAT || elements[i].format == WINED3DFMT_R16G16B16A16_FLOAT)
273
        {
274
            if (!gl_info->supported[ARB_HALF_FLOAT_VERTEX]) declaration->half_float_conv_needed = TRUE;
275
        }
276 277
    }

278
    return WINED3D_OK;
279
}