volume.c 13.6 KB
Newer Older
1 2 3 4
/*
 * IWineD3DVolume implementation
 *
 * Copyright 2002-2005 Jason Edmeades
5
 * Copyright 2002-2005 Raphael Junqueira
6
 * Copyright 2005 Oliver Stieber
7
 * Copyright 2009-2010 Henri Verbeet for CodeWeavers
8 9 10 11 12 13 14 15 16 17 18 19 20
 *
 * 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
21
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 23 24 25 26
 */

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

27
WINE_DEFAULT_DEBUG_CHANNEL(d3d_surface);
28

29
/* Context activation is done by the caller. */
30 31 32 33
static void volume_bind_and_dirtify(struct IWineD3DVolumeImpl *volume)
{
    const struct wined3d_gl_info *gl_info = &volume->resource.device->adapter->gl_info;
    IWineD3DBaseTextureImpl *container = (IWineD3DBaseTextureImpl *)volume->container;
34
    DWORD active_sampler;
35 36 37 38 39 40 41 42 43 44 45 46

    /* We don't need a specific texture unit, but after binding the texture the current unit is dirty.
     * Read the unit back instead of switching to 0, this avoids messing around with the state manager's
     * gl states. The current texture unit should always be a valid one.
     *
     * To be more specific, this is tricky because we can implicitly be called
     * from sampler() in state.c. This means we can't touch anything other than
     * whatever happens to be the currently active texture, or we would risk
     * marking already applied sampler states dirty again.
     *
     * TODO: Track the current active texture per GL context instead of using glGet
     */
47 48
    if (gl_info->supported[ARB_MULTITEXTURE])
    {
49 50 51 52
        GLint active_texture;
        ENTER_GL();
        glGetIntegerv(GL_ACTIVE_TEXTURE, &active_texture);
        LEAVE_GL();
53
        active_sampler = volume->resource.device->rev_tex_unit_map[active_texture - GL_TEXTURE0_ARB];
54 55 56 57
    } else {
        active_sampler = 0;
    }

58 59
    if (active_sampler != WINED3D_UNMAPPED_STAGE)
    {
60
        IWineD3DDeviceImpl_MarkStateDirty(volume->resource.device, STATE_SAMPLER(active_sampler));
61 62
    }

63
    container->baseTexture.texture_ops->texture_bind(container, FALSE);
64 65
}

66
void volume_add_dirty_box(struct IWineD3DVolumeImpl *volume, const WINED3DBOX *dirty_box)
67
{
68
    volume->dirty = TRUE;
69 70
    if (dirty_box)
    {
71 72 73 74 75 76
        volume->lockedBox.Left = min(volume->lockedBox.Left, dirty_box->Left);
        volume->lockedBox.Top = min(volume->lockedBox.Top, dirty_box->Top);
        volume->lockedBox.Front = min(volume->lockedBox.Front, dirty_box->Front);
        volume->lockedBox.Right = max(volume->lockedBox.Right, dirty_box->Right);
        volume->lockedBox.Bottom = max(volume->lockedBox.Bottom, dirty_box->Bottom);
        volume->lockedBox.Back = max(volume->lockedBox.Back, dirty_box->Back);
77 78 79
    }
    else
    {
80 81 82 83 84 85
        volume->lockedBox.Left = 0;
        volume->lockedBox.Top = 0;
        volume->lockedBox.Front = 0;
        volume->lockedBox.Right = volume->currentDesc.Width;
        volume->lockedBox.Bottom = volume->currentDesc.Height;
        volume->lockedBox.Back = volume->currentDesc.Depth;
86 87 88
    }
}

89
void volume_set_container(IWineD3DVolumeImpl *volume, struct IWineD3DVolumeTextureImpl *container)
90 91 92 93 94 95
{
    TRACE("volume %p, container %p.\n", volume, container);

    volume->container = container;
}

96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
/* Context activation is done by the caller. */
void volume_load(IWineD3DVolumeImpl *volume, UINT level, BOOL srgb_mode)
{
    const struct wined3d_gl_info *gl_info = &volume->resource.device->adapter->gl_info;
    const struct wined3d_format *format = volume->resource.format;

    TRACE("volume %p, level %u, srgb %#x, format %s (%#x).\n",
            volume, level, srgb_mode, debug_d3dformat(format->id), format->id);

    volume_bind_and_dirtify(volume);

    ENTER_GL();
    GL_EXTCALL(glTexImage3DEXT(GL_TEXTURE_3D, level, format->glInternal,
            volume->currentDesc.Width, volume->currentDesc.Height, volume->currentDesc.Depth,
            0, format->glFormat, format->glType, volume->resource.allocatedMemory));
    checkGLcall("glTexImage3D");
    LEAVE_GL();

    /* When adding code releasing volume->resource.allocatedMemory to save
     * data keep in mind that GL_UNPACK_CLIENT_STORAGE_APPLE is enabled by
     * default if supported(GL_APPLE_client_storage). Thus do not release
     * volume->resource.allocatedMemory if GL_APPLE_client_storage is
     * supported. */
}

121
/* Do not call while under the GL lock. */
122
static void volume_unload(struct wined3d_resource *resource)
123 124 125 126 127 128 129 130 131
{
    TRACE("texture %p.\n", resource);

    /* The whole content is shadowed on This->resource.allocatedMemory, and
     * the texture name is managed by the VolumeTexture container. */

    resource_unload(resource);
}

132 133 134
/* *******************************************
   IWineD3DVolume IUnknown parts follow
   ******************************************* */
135
static HRESULT WINAPI IWineD3DVolumeImpl_QueryInterface(IWineD3DVolume *iface, REFIID riid, void **object)
136
{
137 138 139 140 141 142 143
    TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);

    if (IsEqualGUID(riid, &IID_IWineD3DVolume)
            || IsEqualGUID(riid, &IID_IWineD3DResource)
            || IsEqualGUID(riid, &IID_IWineD3DBase)
            || IsEqualGUID(riid, &IID_IUnknown))
    {
144
        IUnknown_AddRef(iface);
145
        *object = iface;
146
        return S_OK;
147
    }
148 149 150 151

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

    *object = NULL;
152 153 154
    return E_NOINTERFACE;
}

155
static ULONG WINAPI IWineD3DVolumeImpl_AddRef(IWineD3DVolume *iface) {
156
    IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
157
    TRACE("(%p) : AddRef increasing from %d\n", This, This->resource.ref);
158
    return InterlockedIncrement(&This->resource.ref);
159 160
}

161
/* Do not call while under the GL lock. */
162
static ULONG WINAPI IWineD3DVolumeImpl_Release(IWineD3DVolume *iface) {
163 164
    IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
    ULONG ref;
165
    TRACE("(%p) : Releasing from %d\n", This, This->resource.ref);
166
    ref = InterlockedDecrement(&This->resource.ref);
167 168 169

    if (!ref)
    {
170
        resource_cleanup(&This->resource);
171
        This->resource.parent_ops->wined3d_object_destroyed(This->resource.parent);
172 173 174 175 176
        HeapFree(GetProcessHeap(), 0, This);
    }
    return ref;
}

177
/* ****************************************************
178
   IWineD3DVolume IWineD3DResource parts follow
179
   **************************************************** */
180 181 182 183 184
static void * WINAPI IWineD3DVolumeImpl_GetParent(IWineD3DVolume *iface)
{
    TRACE("iface %p.\n", iface);

    return ((IWineD3DVolumeImpl *)iface)->resource.parent;
185 186
}

187 188 189
static HRESULT WINAPI IWineD3DVolumeImpl_SetPrivateData(IWineD3DVolume *iface,
        REFGUID riid, const void *data, DWORD data_size, DWORD flags)
{
190
    return resource_set_private_data(&((IWineD3DVolumeImpl *)iface)->resource, riid, data, data_size, flags);
191 192
}

193 194 195
static HRESULT WINAPI IWineD3DVolumeImpl_GetPrivateData(IWineD3DVolume *iface,
        REFGUID guid, void *data, DWORD *data_size)
{
196
    return resource_get_private_data(&((IWineD3DVolumeImpl *)iface)->resource, guid, data, data_size);
197 198
}

199 200
static HRESULT WINAPI IWineD3DVolumeImpl_FreePrivateData(IWineD3DVolume *iface, REFGUID refguid)
{
201
    return resource_free_private_data(&((IWineD3DVolumeImpl *)iface)->resource, refguid);
202 203
}

204 205
static DWORD WINAPI IWineD3DVolumeImpl_SetPriority(IWineD3DVolume *iface, DWORD priority)
{
206
    return resource_set_priority(&((IWineD3DVolumeImpl *)iface)->resource, priority);
207 208
}

209 210
static DWORD WINAPI IWineD3DVolumeImpl_GetPriority(IWineD3DVolume *iface)
{
211
    return resource_get_priority(&((IWineD3DVolumeImpl *)iface)->resource);
212 213
}

214
/* Do not call while under the GL lock. */
215
static void WINAPI IWineD3DVolumeImpl_PreLoad(IWineD3DVolume *iface) {
216
    FIXME("iface %p stub!\n", iface);
217 218
}

219 220
static WINED3DRESOURCETYPE WINAPI IWineD3DVolumeImpl_GetType(IWineD3DVolume *iface)
{
221
    return resource_get_type(&((IWineD3DVolumeImpl *)iface)->resource);
222 223
}

224
static void WINAPI IWineD3DVolumeImpl_GetDesc(IWineD3DVolume *iface, WINED3DVOLUME_DESC *desc)
225
{
226 227 228 229 230 231 232 233 234 235 236 237
    IWineD3DVolumeImpl *volume = (IWineD3DVolumeImpl *)iface;

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

    desc->Format = volume->resource.format->id;
    desc->Type = volume->resource.resourceType;
    desc->Usage = volume->resource.usage;
    desc->Pool = volume->resource.pool;
    desc->Size = volume->resource.size; /* dx8 only */
    desc->Width = volume->currentDesc.Width;
    desc->Height = volume->currentDesc.Height;
    desc->Depth = volume->currentDesc.Depth;
238 239
}

240
static HRESULT WINAPI IWineD3DVolumeImpl_Map(IWineD3DVolume *iface,
241
        WINED3DLOCKED_BOX *pLockedVolume, const WINED3DBOX *pBox, DWORD flags)
242
{
243
    IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
244
    FIXME("(%p) : pBox=%p stub\n", This, pBox);
245

246 247 248 249
    if(!This->resource.allocatedMemory) {
        This->resource.allocatedMemory = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, This->resource.size);
    }

250
    /* fixme: should we really lock as such? */
251
    TRACE("(%p) : box=%p, output pbox=%p, allMem=%p\n", This, pBox, pLockedVolume, This->resource.allocatedMemory);
252

253 254
    pLockedVolume->RowPitch = This->resource.format->byte_count * This->currentDesc.Width; /* Bytes / row   */
    pLockedVolume->SlicePitch = This->resource.format->byte_count
255
            * This->currentDesc.Width * This->currentDesc.Height;                               /* Bytes / slice */
256 257
    if (!pBox) {
        TRACE("No box supplied - all is ok\n");
258
        pLockedVolume->pBits = This->resource.allocatedMemory;
259 260 261 262 263 264 265 266
        This->lockedBox.Left   = 0;
        This->lockedBox.Top    = 0;
        This->lockedBox.Front  = 0;
        This->lockedBox.Right  = This->currentDesc.Width;
        This->lockedBox.Bottom = This->currentDesc.Height;
        This->lockedBox.Back   = This->currentDesc.Depth;
    } else {
        TRACE("Lock Box (%p) = l %d, t %d, r %d, b %d, fr %d, ba %d\n", pBox, pBox->Left, pBox->Top, pBox->Right, pBox->Bottom, pBox->Front, pBox->Back);
267 268 269
        pLockedVolume->pBits = This->resource.allocatedMemory
                + (pLockedVolume->SlicePitch * pBox->Front)     /* FIXME: is front < back or vica versa? */
                + (pLockedVolume->RowPitch * pBox->Top)
270
                + (pBox->Left * This->resource.format->byte_count);
271 272 273 274 275 276 277
        This->lockedBox.Left   = pBox->Left;
        This->lockedBox.Top    = pBox->Top;
        This->lockedBox.Front  = pBox->Front;
        This->lockedBox.Right  = pBox->Right;
        This->lockedBox.Bottom = pBox->Bottom;
        This->lockedBox.Back   = pBox->Back;
    }
278

279
    if (!(flags & (WINED3DLOCK_NO_DIRTY_UPDATE | WINED3DLOCK_READONLY)))
280
    {
281
        volume_add_dirty_box(This, &This->lockedBox);
282 283
        This->container->baseTexture.texture_rgb.dirty = TRUE;
        This->container->baseTexture.texture_srgb.dirty = TRUE;
284 285 286 287
    }

    This->locked = TRUE;
    TRACE("returning memory@%p rpitch(%d) spitch(%d)\n", pLockedVolume->pBits, pLockedVolume->RowPitch, pLockedVolume->SlicePitch);
288
    return WINED3D_OK;
289 290
}

291 292
static HRESULT WINAPI IWineD3DVolumeImpl_Unmap(IWineD3DVolume *iface)
{
293
    IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
294 295 296 297
    if (!This->locked)
    {
        WARN("Trying to unlock unlocked volume %p.\n", iface);
        return WINED3DERR_INVALIDCALL;
298 299 300
    }
    TRACE("(%p) : unlocking volume\n", This);
    This->locked = FALSE;
301
    memset(&This->lockedBox, 0, sizeof(This->lockedBox));
302
    return WINED3D_OK;
303 304
}

305
static const IWineD3DVolumeVtbl IWineD3DVolume_Vtbl =
306
{
307
    /* IUnknown */
308 309 310
    IWineD3DVolumeImpl_QueryInterface,
    IWineD3DVolumeImpl_AddRef,
    IWineD3DVolumeImpl_Release,
311
    /* IWineD3DResource */
312 313 314 315
    IWineD3DVolumeImpl_GetParent,
    IWineD3DVolumeImpl_SetPrivateData,
    IWineD3DVolumeImpl_GetPrivateData,
    IWineD3DVolumeImpl_FreePrivateData,
316 317 318 319 320
    IWineD3DVolumeImpl_SetPriority,
    IWineD3DVolumeImpl_GetPriority,
    IWineD3DVolumeImpl_PreLoad,
    IWineD3DVolumeImpl_GetType,
    /* IWineD3DVolume */
321
    IWineD3DVolumeImpl_GetDesc,
322 323
    IWineD3DVolumeImpl_Map,
    IWineD3DVolumeImpl_Unmap,
324
};
325

326 327 328 329 330
static const struct wined3d_resource_ops volume_resource_ops =
{
    volume_unload,
};

331
HRESULT volume_init(IWineD3DVolumeImpl *volume, IWineD3DDeviceImpl *device, UINT width,
332
        UINT height, UINT depth, DWORD usage, enum wined3d_format_id format_id, WINED3DPOOL pool,
333
        void *parent, const struct wined3d_parent_ops *parent_ops)
334 335
{
    const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
336
    const struct wined3d_format *format = wined3d_get_format(gl_info, format_id);
337 338 339 340 341 342 343 344 345 346
    HRESULT hr;

    if (!gl_info->supported[EXT_TEXTURE3D])
    {
        WARN("Volume cannot be created - no volume texture support.\n");
        return WINED3DERR_INVALIDCALL;
    }

    volume->lpVtbl = &IWineD3DVolume_Vtbl;

347
    hr = resource_init(&volume->resource, WINED3DRTYPE_VOLUME, device,
348 349
            width * height * depth * format->byte_count, usage, format, pool,
            parent, parent_ops, &volume_resource_ops);
350 351 352 353 354 355 356 357 358 359 360 361 362 363
    if (FAILED(hr))
    {
        WARN("Failed to initialize resource, returning %#x.\n", hr);
        return hr;
    }

    volume->currentDesc.Width = width;
    volume->currentDesc.Height = height;
    volume->currentDesc.Depth = depth;
    volume->lockable = TRUE;
    volume->locked = FALSE;
    memset(&volume->lockedBox, 0, sizeof(volume->lockedBox));
    volume->dirty = TRUE;

364
    volume_add_dirty_box(volume, NULL);
365 366 367

    return WINED3D_OK;
}