shader.c 5.37 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * Copyright 2002-2003 Jason Edmeades
 *                     Raphael Junqueira
 *
 * 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
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */

#include "d3d8_private.h"

WINE_DEFAULT_DEBUG_CHANNEL(d3d8);

24
static void STDMETHODCALLTYPE d3d8_vertexshader_wined3d_object_destroyed(void *parent)
25
{
26 27
    struct d3d8_vertex_shader *shader = parent;
    d3d8_vertex_declaration_destroy(shader->vertex_declaration);
28
    heap_free(shader);
29 30
}

31
void d3d8_vertex_shader_destroy(struct d3d8_vertex_shader *shader)
32
{
33
    TRACE("shader %p.\n", shader);
34

35
    if (shader->wined3d_shader)
36 37
    {
        wined3d_mutex_lock();
38
        wined3d_shader_decref(shader->wined3d_shader);
39 40
        wined3d_mutex_unlock();
    }
41
    else
42
    {
43
        d3d8_vertexshader_wined3d_object_destroyed(shader);
44 45 46 47 48 49 50 51
    }
}

static const struct wined3d_parent_ops d3d8_vertexshader_wined3d_parent_ops =
{
    d3d8_vertexshader_wined3d_object_destroyed,
};

52
static HRESULT d3d8_vertexshader_create_vertexdeclaration(struct d3d8_device *device,
53
        const DWORD *declaration, DWORD shader_handle, struct d3d8_vertex_declaration **decl_ptr)
54
{
55
    struct d3d8_vertex_declaration *object;
56 57 58 59 60
    HRESULT hr;

    TRACE("device %p, declaration %p, shader_handle %#x, decl_ptr %p.\n",
            device, declaration, shader_handle, decl_ptr);

61
    if (!(object = heap_alloc_zero(sizeof(*object))))
62 63
        return E_OUTOFMEMORY;

64
    hr = d3d8_vertex_declaration_init(object, device, declaration, shader_handle);
65 66 67
    if (FAILED(hr))
    {
        WARN("Failed to initialize vertex declaration, hr %#x.\n", hr);
68
        heap_free(object);
69 70 71 72
        return hr;
    }

    TRACE("Created vertex declaration %p.\n", object);
73
    *decl_ptr = object;
74 75 76 77

    return D3D_OK;
}

78
HRESULT d3d8_vertex_shader_init(struct d3d8_vertex_shader *shader, struct d3d8_device *device,
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
        const DWORD *declaration, const DWORD *byte_code, DWORD shader_handle, DWORD usage)
{
    const DWORD *token = declaration;
    HRESULT hr;

    /* Test if the vertex declaration is valid. */
    while (D3DVSD_END() != *token)
    {
        D3DVSD_TOKENTYPE token_type = ((*token & D3DVSD_TOKENTYPEMASK) >> D3DVSD_TOKENTYPESHIFT);

        if (token_type == D3DVSD_TOKEN_STREAMDATA && !(token_type & 0x10000000))
        {
            DWORD type = ((*token & D3DVSD_DATATYPEMASK) >> D3DVSD_DATATYPESHIFT);
            DWORD reg  = ((*token & D3DVSD_VERTEXREGMASK) >> D3DVSD_VERTEXREGSHIFT);

            if (reg == D3DVSDE_NORMAL && type != D3DVSDT_FLOAT3 && !byte_code)
            {
96
                WARN("Attempt to use a non-FLOAT3 normal with the fixed-function function\n");
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
                return D3DERR_INVALIDCALL;
            }
        }
        token += parse_token(token);
    }

    hr = d3d8_vertexshader_create_vertexdeclaration(device, declaration, shader_handle, &shader->vertex_declaration);
    if (FAILED(hr))
    {
        WARN("Failed to create vertex declaration, hr %#x.\n", hr);
        return hr;
    }

    if (byte_code)
    {
112 113 114 115 116 117
        struct wined3d_shader_desc desc;

        if (usage)
            FIXME("Usage %#x not implemented.\n", usage);

        desc.byte_code = byte_code;
118
        desc.byte_code_size = ~(size_t)0;
119 120

        wined3d_mutex_lock();
121 122
        hr = wined3d_shader_create_vs(device->wined3d_device, &desc, shader,
                &d3d8_vertexshader_wined3d_parent_ops, &shader->wined3d_shader);
123 124 125 126
        wined3d_mutex_unlock();
        if (FAILED(hr))
        {
            WARN("Failed to create wined3d vertex shader, hr %#x.\n", hr);
127
            d3d8_vertex_declaration_destroy(shader->vertex_declaration);
128 129 130
            return hr;
        }

131
        load_local_constants(declaration, shader->wined3d_shader);
132 133 134 135 136
    }

    return D3D_OK;
}

137
static void STDMETHODCALLTYPE d3d8_pixelshader_wined3d_object_destroyed(void *parent)
138
{
139
    heap_free(parent);
140 141
}

142
void d3d8_pixel_shader_destroy(struct d3d8_pixel_shader *shader)
143
{
144
    TRACE("shader %p.\n", shader);
145

146 147 148
    wined3d_mutex_lock();
    wined3d_shader_decref(shader->wined3d_shader);
    wined3d_mutex_unlock();
149 150 151 152 153 154 155
}

static const struct wined3d_parent_ops d3d8_pixelshader_wined3d_parent_ops =
{
    d3d8_pixelshader_wined3d_object_destroyed,
};

156
HRESULT d3d8_pixel_shader_init(struct d3d8_pixel_shader *shader, struct d3d8_device *device,
157 158
        const DWORD *byte_code, DWORD shader_handle)
{
159
    struct wined3d_shader_desc desc;
160 161 162 163
    HRESULT hr;

    shader->handle = shader_handle;

164
    desc.byte_code = byte_code;
165
    desc.byte_code_size = ~(size_t)0;
166

167
    wined3d_mutex_lock();
168 169
    hr = wined3d_shader_create_ps(device->wined3d_device, &desc, shader,
            &d3d8_pixelshader_wined3d_parent_ops, &shader->wined3d_shader);
170 171 172 173 174 175 176 177 178
    wined3d_mutex_unlock();
    if (FAILED(hr))
    {
        WARN("Failed to create wined3d pixel shader, hr %#x.\n", hr);
        return hr;
    }

    return D3D_OK;
}