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

#include "config.h"
24
#include "wine/port.h"
25 26
#include "d3d9_private.h"

27
WINE_DEFAULT_DEBUG_CHANNEL(d3d9);
28

29
static inline struct d3d9_stateblock *impl_from_IDirect3DStateBlock9(IDirect3DStateBlock9 *iface)
30
{
31
    return CONTAINING_RECORD(iface, struct d3d9_stateblock, IDirect3DStateBlock9_iface);
32 33
}

34
static HRESULT WINAPI d3d9_stateblock_QueryInterface(IDirect3DStateBlock9 *iface, REFIID riid, void **out)
35
{
36
    TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
Henri Verbeet's avatar
Henri Verbeet committed
37

38 39 40
    if (IsEqualGUID(riid, &IID_IDirect3DStateBlock9)
            || IsEqualGUID(riid, &IID_IUnknown))
    {
41
        IDirect3DStateBlock9_AddRef(iface);
42
        *out = iface;
H. Verbeet's avatar
H. Verbeet committed
43
        return S_OK;
44 45
    }

46 47
    WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));

48
    *out = NULL;
49 50 51
    return E_NOINTERFACE;
}

52
static ULONG WINAPI d3d9_stateblock_AddRef(IDirect3DStateBlock9 *iface)
53
{
54 55
    struct d3d9_stateblock *stateblock = impl_from_IDirect3DStateBlock9(iface);
    ULONG refcount = InterlockedIncrement(&stateblock->refcount);
56

57
    TRACE("%p increasing refcount to %u.\n", iface, refcount);
58

59
    return refcount;
60 61
}

62
static ULONG WINAPI d3d9_stateblock_Release(IDirect3DStateBlock9 *iface)
63
{
64 65
    struct d3d9_stateblock *stateblock = impl_from_IDirect3DStateBlock9(iface);
    ULONG refcount = InterlockedDecrement(&stateblock->refcount);
66

67
    TRACE("%p decreasing refcount to %u.\n", iface, refcount);
68

69 70
    if (!refcount)
    {
71
        wined3d_mutex_lock();
72
        wined3d_stateblock_decref(stateblock->wined3d_stateblock);
73 74
        wined3d_mutex_unlock();

75
        IDirect3DDevice9Ex_Release(stateblock->parent_device);
76
        heap_free(stateblock);
77
    }
78 79

    return refcount;
80 81
}

82
static HRESULT WINAPI d3d9_stateblock_GetDevice(IDirect3DStateBlock9 *iface, IDirect3DDevice9 **device)
83
{
84
    struct d3d9_stateblock *stateblock = impl_from_IDirect3DStateBlock9(iface);
Henri Verbeet's avatar
Henri Verbeet committed
85

86
    TRACE("iface %p, device %p.\n", iface, device);
87

88
    *device = (IDirect3DDevice9 *)stateblock->parent_device;
89 90 91
    IDirect3DDevice9_AddRef(*device);

    TRACE("Returning device %p.\n", *device);
92

93
    return D3D_OK;
94 95
}

96
static HRESULT WINAPI d3d9_stateblock_Capture(IDirect3DStateBlock9 *iface)
97
{
98
    struct d3d9_stateblock *stateblock = impl_from_IDirect3DStateBlock9(iface);
99
    struct d3d9_device *device;
Henri Verbeet's avatar
Henri Verbeet committed
100 101

    TRACE("iface %p.\n", iface);
102

103
    wined3d_mutex_lock();
104 105 106 107 108 109 110
    device = impl_from_IDirect3DDevice9Ex(stateblock->parent_device);
    if (device->recording)
    {
        wined3d_mutex_unlock();
        WARN("Trying to capture stateblock while recording, returning D3DERR_INVALIDCALL.\n");
        return D3DERR_INVALIDCALL;
    }
111
    wined3d_stateblock_capture(stateblock->wined3d_stateblock);
112 113
    wined3d_mutex_unlock();

114
    return D3D_OK;
115 116
}

117
static HRESULT WINAPI d3d9_stateblock_Apply(IDirect3DStateBlock9 *iface)
118
{
119
    struct d3d9_stateblock *stateblock = impl_from_IDirect3DStateBlock9(iface);
120 121
    struct wined3d_texture *wined3d_texture;
    unsigned int i, offset, stride, stage;
122 123
    struct wined3d_buffer *wined3d_buffer;
    struct d3d9_vertexbuffer *buffer;
124
    enum wined3d_format_id format;
125
    struct d3d9_texture *texture;
126 127
    struct d3d9_device *device;
    HRESULT hr;
Henri Verbeet's avatar
Henri Verbeet committed
128 129

    TRACE("iface %p.\n", iface);
130

131
    wined3d_mutex_lock();
132
    device = impl_from_IDirect3DDevice9Ex(stateblock->parent_device);
133 134 135 136 137 138 139
    if (device->recording)
    {
        wined3d_mutex_unlock();
        WARN("Trying to apply stateblock while recording, returning D3DERR_INVALIDCALL.\n");
        return D3DERR_INVALIDCALL;
    }
    wined3d_stateblock_apply(stateblock->wined3d_stateblock);
140 141 142 143 144 145 146 147 148 149 150
    device->sysmem_vb = 0;
    for (i = 0; i < D3D9_MAX_STREAMS; ++i)
    {
        if (FAILED(hr = wined3d_device_get_stream_source(device->wined3d_device,
                i, &wined3d_buffer, &offset, &stride)))
            continue;
        if (!wined3d_buffer || !(buffer = wined3d_buffer_get_parent(wined3d_buffer)))
            continue;
        if (buffer->draw_buffer)
            device->sysmem_vb |= 1u << i;
    }
151 152
    device->sysmem_ib = (wined3d_buffer = wined3d_device_get_index_buffer(device->wined3d_device, &format, &offset))
            && (buffer = wined3d_buffer_get_parent(wined3d_buffer)) && buffer->draw_buffer;
153 154 155 156 157 158 159 160 161 162 163 164
    device->auto_mipmaps = 0;
    for (i = 0; i < D3D9_MAX_TEXTURE_UNITS; ++i)
    {
        stage = i >= 16 ? i - 16 + D3DVERTEXTEXTURESAMPLER0 : i;

        if ((wined3d_texture = wined3d_device_get_texture(device->wined3d_device, stage))
                && (texture = wined3d_texture_get_parent(wined3d_texture))
                && texture->usage & D3DUSAGE_AUTOGENMIPMAP)
            device->auto_mipmaps |= 1u << i;
        else
            device->auto_mipmaps &= ~(1u << i);
    }
165 166
    wined3d_mutex_unlock();

167
    return D3D_OK;
168 169 170
}


171
static const struct IDirect3DStateBlock9Vtbl d3d9_stateblock_vtbl =
172
{
173
    /* IUnknown */
174 175 176
    d3d9_stateblock_QueryInterface,
    d3d9_stateblock_AddRef,
    d3d9_stateblock_Release,
177
    /* IDirect3DStateBlock9 */
178 179 180
    d3d9_stateblock_GetDevice,
    d3d9_stateblock_Capture,
    d3d9_stateblock_Apply,
181 182
};

183
HRESULT stateblock_init(struct d3d9_stateblock *stateblock, struct d3d9_device *device,
184
        D3DSTATEBLOCKTYPE type, struct wined3d_stateblock *wined3d_stateblock)
185
{
186
    HRESULT hr;
Henri Verbeet's avatar
Henri Verbeet committed
187

188 189
    stateblock->IDirect3DStateBlock9_iface.lpVtbl = &d3d9_stateblock_vtbl;
    stateblock->refcount = 1;
190

191
    if (wined3d_stateblock)
192
    {
193
        stateblock->wined3d_stateblock = wined3d_stateblock;
194
    }
195 196 197
    else
    {
        wined3d_mutex_lock();
198
        hr = wined3d_stateblock_create(device->wined3d_device,
199
                (enum wined3d_stateblock_type)type, &stateblock->wined3d_stateblock);
200 201 202 203 204 205 206 207
        wined3d_mutex_unlock();
        if (FAILED(hr))
        {
            WARN("Failed to create wined3d stateblock, hr %#x.\n", hr);
            return hr;
        }
    }

208 209
    stateblock->parent_device = &device->IDirect3DDevice9Ex_iface;
    IDirect3DDevice9Ex_AddRef(stateblock->parent_device);
210

211 212
    return D3D_OK;
}