volume.c 16 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 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
#define GLINFO_LOCATION This->resource.wineD3DDevice->adapter->gl_info
29

30
/* Context activation is done by the caller. */
31 32
static void volume_bind_and_dirtify(IWineD3DVolume *iface) {
    IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
33
    const struct wined3d_gl_info *gl_info = &This->resource.wineD3DDevice->adapter->gl_info;
34
    IWineD3DVolumeTexture *texture;
35
    DWORD active_sampler;
36 37 38 39 40 41 42 43 44 45 46 47

    /* 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
     */
48 49
    if (gl_info->supported[ARB_MULTITEXTURE])
    {
50 51 52 53 54 55 56 57 58
        GLint active_texture;
        ENTER_GL();
        glGetIntegerv(GL_ACTIVE_TEXTURE, &active_texture);
        LEAVE_GL();
        active_sampler = This->resource.wineD3DDevice->rev_tex_unit_map[active_texture - GL_TEXTURE0_ARB];
    } else {
        active_sampler = 0;
    }

59 60
    if (active_sampler != WINED3D_UNMAPPED_STAGE)
    {
61 62 63 64
        IWineD3DDeviceImpl_MarkStateDirty(This->resource.wineD3DDevice, STATE_SAMPLER(active_sampler));
    }

    if (SUCCEEDED(IWineD3DSurface_GetContainer(iface, &IID_IWineD3DVolumeTexture, (void **)&texture))) {
65
        IWineD3DVolumeTexture_BindTexture(texture, FALSE);
66 67 68 69 70 71
        IWineD3DVolumeTexture_Release(texture);
    } else {
        ERR("Volume should be part of a volume texture\n");
    }
}

72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
void volume_add_dirty_box(IWineD3DVolume *iface, const WINED3DBOX *dirty_box)
{
    IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;

    This->dirty = TRUE;
    if (dirty_box)
    {
        This->lockedBox.Left = min(This->lockedBox.Left, dirty_box->Left);
        This->lockedBox.Top = min(This->lockedBox.Top, dirty_box->Top);
        This->lockedBox.Front = min(This->lockedBox.Front, dirty_box->Front);
        This->lockedBox.Right = max(This->lockedBox.Right, dirty_box->Right);
        This->lockedBox.Bottom = max(This->lockedBox.Bottom, dirty_box->Bottom);
        This->lockedBox.Back = max(This->lockedBox.Back, dirty_box->Back);
    }
    else
    {
        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;
    }
}

97 98 99
/* *******************************************
   IWineD3DVolume IUnknown parts follow
   ******************************************* */
100
static HRESULT WINAPI IWineD3DVolumeImpl_QueryInterface(IWineD3DVolume *iface, REFIID riid, LPVOID *ppobj)
101 102
{
    IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
103 104
    TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppobj);
    if (IsEqualGUID(riid, &IID_IUnknown)
105
        || IsEqualGUID(riid, &IID_IWineD3DBase)
106 107 108
        || IsEqualGUID(riid, &IID_IWineD3DVolume)){
        IUnknown_AddRef(iface);
        *ppobj = This;
109
        return S_OK;
110
    }
111
    *ppobj = NULL;
112 113 114
    return E_NOINTERFACE;
}

115
static ULONG WINAPI IWineD3DVolumeImpl_AddRef(IWineD3DVolume *iface) {
116
    IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
117
    TRACE("(%p) : AddRef increasing from %d\n", This, This->resource.ref);
118
    return InterlockedIncrement(&This->resource.ref);
119 120
}

121
static ULONG WINAPI IWineD3DVolumeImpl_Release(IWineD3DVolume *iface) {
122 123
    IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
    ULONG ref;
124
    TRACE("(%p) : Releasing from %d\n", This, This->resource.ref);
125
    ref = InterlockedDecrement(&This->resource.ref);
126
    if (ref == 0) {
127
        resource_cleanup((IWineD3DResource *)iface);
128
        This->resource.parent_ops->wined3d_object_destroyed(This->resource.parent);
129 130 131 132 133
        HeapFree(GetProcessHeap(), 0, This);
    }
    return ref;
}

134
/* ****************************************************
135
   IWineD3DVolume IWineD3DResource parts follow
136
   **************************************************** */
137
static HRESULT WINAPI IWineD3DVolumeImpl_GetParent(IWineD3DVolume *iface, IUnknown **pParent) {
138
    return resource_get_parent((IWineD3DResource *)iface, pParent);
139 140
}

141
static HRESULT WINAPI IWineD3DVolumeImpl_GetDevice(IWineD3DVolume *iface, IWineD3DDevice** ppDevice) {
142
    return resource_get_device((IWineD3DResource *)iface, ppDevice);
143 144
}

145
static HRESULT WINAPI IWineD3DVolumeImpl_SetPrivateData(IWineD3DVolume *iface, REFGUID refguid, CONST void* pData, DWORD SizeOfData, DWORD Flags) {
146
    return resource_set_private_data((IWineD3DResource *)iface, refguid, pData, SizeOfData, Flags);
147 148
}

149
static HRESULT WINAPI IWineD3DVolumeImpl_GetPrivateData(IWineD3DVolume *iface, REFGUID  refguid, void* pData, DWORD* pSizeOfData) {
150
    return resource_get_private_data((IWineD3DResource *)iface, refguid, pData, pSizeOfData);
151 152
}

153
static HRESULT WINAPI IWineD3DVolumeImpl_FreePrivateData(IWineD3DVolume *iface, REFGUID refguid) {
154
    return resource_free_private_data((IWineD3DResource *)iface, refguid);
155 156
}

157
static DWORD WINAPI IWineD3DVolumeImpl_SetPriority(IWineD3DVolume *iface, DWORD PriorityNew) {
158
    return resource_set_priority((IWineD3DResource *)iface, PriorityNew);
159 160
}

161
static DWORD WINAPI IWineD3DVolumeImpl_GetPriority(IWineD3DVolume *iface) {
162
    return resource_get_priority((IWineD3DResource *)iface);
163 164
}

165
static void WINAPI IWineD3DVolumeImpl_PreLoad(IWineD3DVolume *iface) {
166
    FIXME("iface %p stub!\n", iface);
167 168
}

169
static void WINAPI IWineD3DVolumeImpl_UnLoad(IWineD3DVolume *iface) {
170 171 172 173
    /* The whole content is shadowed on This->resource.allocatedMemory, and the
     * texture name is managed by the VolumeTexture container
     */
    TRACE("(%p): Nothing to do\n", iface);
174 175
}

176
static WINED3DRESOURCETYPE WINAPI IWineD3DVolumeImpl_GetType(IWineD3DVolume *iface) {
177
    return resource_get_type((IWineD3DResource *)iface);
178 179 180 181 182
}

/* *******************************************
   IWineD3DVolume parts follow
   ******************************************* */
183
static HRESULT WINAPI IWineD3DVolumeImpl_GetContainer(IWineD3DVolume *iface, REFIID riid, void** ppContainer) {
184
    IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
185 186 187 188 189 190 191 192 193 194 195 196 197

    TRACE("(This %p, riid %s, ppContainer %p)\n", This, debugstr_guid(riid), ppContainer);

    if (!ppContainer) {
        ERR("Called without a valid ppContainer.\n");
    }

    /* Although surfaces can be standalone, volumes can't */
    if (!This->container) {
        ERR("Volume without an container. Should not happen.\n");
    }

    TRACE("Relaying to QueryInterface\n");
198
    return IUnknown_QueryInterface(This->container, riid, ppContainer);
199 200
}

201
static HRESULT WINAPI IWineD3DVolumeImpl_GetDesc(IWineD3DVolume *iface, WINED3DVOLUME_DESC* pDesc) {
202 203
    IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
    TRACE("(%p) : copying into %p\n", This, pDesc);
204

205 206 207 208 209 210 211 212 213
    pDesc->Format = This->resource.format_desc->format;
    pDesc->Type = This->resource.resourceType;
    pDesc->Usage = This->resource.usage;
    pDesc->Pool = This->resource.pool;
    pDesc->Size = This->resource.size; /* dx8 only */
    pDesc->Width = This->currentDesc.Width;
    pDesc->Height = This->currentDesc.Height;
    pDesc->Depth = This->currentDesc.Depth;

214
    return WINED3D_OK;
215 216
}

217
static HRESULT WINAPI IWineD3DVolumeImpl_LockBox(IWineD3DVolume *iface, WINED3DLOCKED_BOX* pLockedVolume, CONST WINED3DBOX* pBox, DWORD Flags) {
218
    IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
219
    FIXME("(%p) : pBox=%p stub\n", This, pBox);
220

221 222 223 224
    if(!This->resource.allocatedMemory) {
        This->resource.allocatedMemory = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, This->resource.size);
    }

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

228 229 230
    pLockedVolume->RowPitch = This->resource.format_desc->byte_count * This->currentDesc.Width; /* Bytes / row   */
    pLockedVolume->SlicePitch = This->resource.format_desc->byte_count
            * This->currentDesc.Width * This->currentDesc.Height;                               /* Bytes / slice */
231 232
    if (!pBox) {
        TRACE("No box supplied - all is ok\n");
233
        pLockedVolume->pBits = This->resource.allocatedMemory;
234 235 236 237 238 239 240 241
        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);
242 243 244 245
        pLockedVolume->pBits = This->resource.allocatedMemory
                + (pLockedVolume->SlicePitch * pBox->Front)     /* FIXME: is front < back or vica versa? */
                + (pLockedVolume->RowPitch * pBox->Top)
                + (pBox->Left * This->resource.format_desc->byte_count);
246 247 248 249 250 251 252
        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;
    }
253

254
    if (Flags & (WINED3DLOCK_NO_DIRTY_UPDATE | WINED3DLOCK_READONLY)) {
255 256 257 258 259 260
      /* Don't dirtify */
    } else {
      /**
       * Dirtify on lock
       * as seen in msdn docs
       */
261
      volume_add_dirty_box(iface, &This->lockedBox);
262 263 264 265

      /**  Dirtify Container if needed */
      if (NULL != This->container) {

266
        IWineD3DVolumeTexture *cont = (IWineD3DVolumeTexture*) This->container;
267
        WINED3DRESOURCETYPE containerType = IWineD3DBaseTexture_GetType((IWineD3DBaseTexture *) cont);
268

269
        if (containerType == WINED3DRTYPE_VOLUMETEXTURE) {
270
          IWineD3DBaseTextureImpl* pTexture = (IWineD3DBaseTextureImpl*) cont;
271 272
          pTexture->baseTexture.texture_rgb.dirty = TRUE;
          pTexture->baseTexture.texture_srgb.dirty = TRUE;
273 274 275 276 277 278 279 280
        } else {
          FIXME("Set dirty on container type %d\n", containerType);
        }
      }
    }

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

284
static HRESULT WINAPI IWineD3DVolumeImpl_UnlockBox(IWineD3DVolume *iface) {
285
    IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
286
    if (!This->locked) {
287
      ERR("trying to lock unlocked volume@%p\n", This);
288
      return WINED3DERR_INVALIDCALL;
289 290 291 292
    }
    TRACE("(%p) : unlocking volume\n", This);
    This->locked = FALSE;
    memset(&This->lockedBox, 0, sizeof(RECT));
293
    return WINED3D_OK;
294 295 296 297
}

/* Internal use functions follow : */

298
static HRESULT WINAPI IWineD3DVolumeImpl_SetContainer(IWineD3DVolume *iface, IWineD3DBase* container) {
H. Verbeet's avatar
H. Verbeet committed
299
    IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
300

H. Verbeet's avatar
H. Verbeet committed
301 302
    TRACE("This %p, container %p\n", This, container);

303
    /* We can't keep a reference to the container, since the container already keeps a reference to us. */
H. Verbeet's avatar
H. Verbeet committed
304 305 306 307

    TRACE("Setting container to %p from %p\n", container, This->container);
    This->container = container;

308
    return WINED3D_OK;
309 310
}

311
/* Context activation is done by the caller. */
312
static HRESULT WINAPI IWineD3DVolumeImpl_LoadTexture(IWineD3DVolume *iface, int gl_level, BOOL srgb_mode) {
313
    IWineD3DVolumeImpl *This     = (IWineD3DVolumeImpl *)iface;
314
    const struct GlPixelFormatDesc *glDesc = This->resource.format_desc;
315

316
    TRACE("(%p) : level %u, format %s (0x%08x)\n", This, gl_level, debug_d3dformat(glDesc->format), glDesc->format);
317

318 319
    volume_bind_and_dirtify(iface);

320 321 322 323 324 325 326 327 328 329 330
    TRACE("Calling glTexImage3D %x level=%d, intfmt=%x, w=%d, h=%d,d=%d, 0=%d, glFmt=%x, glType=%x, Mem=%p\n",
            GL_TEXTURE_3D,
            gl_level,
            glDesc->glInternal,
            This->currentDesc.Width,
            This->currentDesc.Height,
            This->currentDesc.Depth,
            0,
            glDesc->glFormat,
            glDesc->glType,
            This->resource.allocatedMemory);
331 332

    ENTER_GL();
333
    GL_EXTCALL(glTexImage3DEXT(GL_TEXTURE_3D,
334
                gl_level,
335
                glDesc->glInternal,
336 337 338 339
                This->currentDesc.Width,
                This->currentDesc.Height,
                This->currentDesc.Depth,
                0,
340 341
                glDesc->glFormat,
                glDesc->glType,
342 343
                This->resource.allocatedMemory));
    checkGLcall("glTexImage3D");
344
    LEAVE_GL();
345 346 347 348 349

    /* When adding code releasing This->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 This->resource.allocatedMemory if GL_APPLE_client_storage is supported.
     */
350
    return WINED3D_OK;
351

352 353
}

354
static const IWineD3DVolumeVtbl IWineD3DVolume_Vtbl =
355
{
356
    /* IUnknown */
357 358 359
    IWineD3DVolumeImpl_QueryInterface,
    IWineD3DVolumeImpl_AddRef,
    IWineD3DVolumeImpl_Release,
360
    /* IWineD3DResource */
361 362 363 364 365
    IWineD3DVolumeImpl_GetParent,
    IWineD3DVolumeImpl_GetDevice,
    IWineD3DVolumeImpl_SetPrivateData,
    IWineD3DVolumeImpl_GetPrivateData,
    IWineD3DVolumeImpl_FreePrivateData,
366 367 368
    IWineD3DVolumeImpl_SetPriority,
    IWineD3DVolumeImpl_GetPriority,
    IWineD3DVolumeImpl_PreLoad,
369
    IWineD3DVolumeImpl_UnLoad,
370 371
    IWineD3DVolumeImpl_GetType,
    /* IWineD3DVolume */
372 373 374 375
    IWineD3DVolumeImpl_GetContainer,
    IWineD3DVolumeImpl_GetDesc,
    IWineD3DVolumeImpl_LockBox,
    IWineD3DVolumeImpl_UnlockBox,
376
    /* Internal interface */
377 378
    IWineD3DVolumeImpl_LoadTexture,
    IWineD3DVolumeImpl_SetContainer
379
};
380

381 382 383
HRESULT volume_init(IWineD3DVolumeImpl *volume, IWineD3DDeviceImpl *device, UINT width,
        UINT height, UINT depth, DWORD usage, WINED3DFORMAT format, WINED3DPOOL pool,
        IUnknown *parent, const struct wined3d_parent_ops *parent_ops)
384 385 386 387 388 389 390 391 392 393 394 395 396 397
{
    const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
    const struct GlPixelFormatDesc *format_desc = getFormatDescEntry(format, gl_info);
    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;

    hr = resource_init((IWineD3DResource *)volume, WINED3DRTYPE_VOLUME, device,
398
            width * height * depth * format_desc->byte_count, usage, format_desc, pool, parent, parent_ops);
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
    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;

    volume_add_dirty_box((IWineD3DVolume *)volume, NULL);

    return WINED3D_OK;
}