wined3d: Allow shader_buffer_init() to fail.
[wine] / dlls / wined3d / volume.c
1 /*
2  * IWineD3DVolume implementation
3  *
4  * Copyright 2002-2005 Jason Edmeades
5  * Copyright 2002-2005 Raphael Junqueira
6  * Copyright 2005 Oliver Stieber
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * 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 "wined3d_private.h"
25
26 WINE_DEFAULT_DEBUG_CHANNEL(d3d_surface);
27 #define GLINFO_LOCATION This->resource.wineD3DDevice->adapter->gl_info
28
29 /* Context activation is done by the caller. */
30 static void volume_bind_and_dirtify(IWineD3DVolume *iface) {
31     IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
32     IWineD3DVolumeTexture *texture;
33     int active_sampler;
34
35     /* We don't need a specific texture unit, but after binding the texture the current unit is dirty.
36      * Read the unit back instead of switching to 0, this avoids messing around with the state manager's
37      * gl states. The current texture unit should always be a valid one.
38      *
39      * To be more specific, this is tricky because we can implicitly be called
40      * from sampler() in state.c. This means we can't touch anything other than
41      * whatever happens to be the currently active texture, or we would risk
42      * marking already applied sampler states dirty again.
43      *
44      * TODO: Track the current active texture per GL context instead of using glGet
45      */
46     if (GL_SUPPORT(ARB_MULTITEXTURE)) {
47         GLint active_texture;
48         ENTER_GL();
49         glGetIntegerv(GL_ACTIVE_TEXTURE, &active_texture);
50         LEAVE_GL();
51         active_sampler = This->resource.wineD3DDevice->rev_tex_unit_map[active_texture - GL_TEXTURE0_ARB];
52     } else {
53         active_sampler = 0;
54     }
55
56     if (active_sampler != -1) {
57         IWineD3DDeviceImpl_MarkStateDirty(This->resource.wineD3DDevice, STATE_SAMPLER(active_sampler));
58     }
59
60     if (SUCCEEDED(IWineD3DSurface_GetContainer(iface, &IID_IWineD3DVolumeTexture, (void **)&texture))) {
61         IWineD3DVolumeTexture_BindTexture(texture, FALSE);
62         IWineD3DVolumeTexture_Release(texture);
63     } else {
64         ERR("Volume should be part of a volume texture\n");
65     }
66 }
67
68 void volume_add_dirty_box(IWineD3DVolume *iface, const WINED3DBOX *dirty_box)
69 {
70     IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
71
72     This->dirty = TRUE;
73     if (dirty_box)
74     {
75         This->lockedBox.Left = min(This->lockedBox.Left, dirty_box->Left);
76         This->lockedBox.Top = min(This->lockedBox.Top, dirty_box->Top);
77         This->lockedBox.Front = min(This->lockedBox.Front, dirty_box->Front);
78         This->lockedBox.Right = max(This->lockedBox.Right, dirty_box->Right);
79         This->lockedBox.Bottom = max(This->lockedBox.Bottom, dirty_box->Bottom);
80         This->lockedBox.Back = max(This->lockedBox.Back, dirty_box->Back);
81     }
82     else
83     {
84         This->lockedBox.Left = 0;
85         This->lockedBox.Top = 0;
86         This->lockedBox.Front = 0;
87         This->lockedBox.Right = This->currentDesc.Width;
88         This->lockedBox.Bottom = This->currentDesc.Height;
89         This->lockedBox.Back = This->currentDesc.Depth;
90     }
91 }
92
93 /* *******************************************
94    IWineD3DVolume IUnknown parts follow
95    ******************************************* */
96 static HRESULT WINAPI IWineD3DVolumeImpl_QueryInterface(IWineD3DVolume *iface, REFIID riid, LPVOID *ppobj)
97 {
98     IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
99     TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppobj);
100     if (IsEqualGUID(riid, &IID_IUnknown)
101         || IsEqualGUID(riid, &IID_IWineD3DBase)
102         || IsEqualGUID(riid, &IID_IWineD3DVolume)){
103         IUnknown_AddRef(iface);
104         *ppobj = This;
105         return S_OK;
106     }
107     *ppobj = NULL;
108     return E_NOINTERFACE;
109 }
110
111 static ULONG WINAPI IWineD3DVolumeImpl_AddRef(IWineD3DVolume *iface) {
112     IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
113     TRACE("(%p) : AddRef increasing from %d\n", This, This->resource.ref);
114     return InterlockedIncrement(&This->resource.ref);
115 }
116
117 static ULONG WINAPI IWineD3DVolumeImpl_Release(IWineD3DVolume *iface) {
118     IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
119     ULONG ref;
120     TRACE("(%p) : Releasing from %d\n", This, This->resource.ref);
121     ref = InterlockedDecrement(&This->resource.ref);
122     if (ref == 0) {
123         resource_cleanup((IWineD3DResource *)iface);
124         HeapFree(GetProcessHeap(), 0, This);
125     }
126     return ref;
127 }
128
129 /* ****************************************************
130    IWineD3DVolume IWineD3DResource parts follow
131    **************************************************** */
132 static HRESULT WINAPI IWineD3DVolumeImpl_GetParent(IWineD3DVolume *iface, IUnknown **pParent) {
133     return resource_get_parent((IWineD3DResource *)iface, pParent);
134 }
135
136 static HRESULT WINAPI IWineD3DVolumeImpl_GetDevice(IWineD3DVolume *iface, IWineD3DDevice** ppDevice) {
137     return resource_get_device((IWineD3DResource *)iface, ppDevice);
138 }
139
140 static HRESULT WINAPI IWineD3DVolumeImpl_SetPrivateData(IWineD3DVolume *iface, REFGUID refguid, CONST void* pData, DWORD SizeOfData, DWORD Flags) {
141     return resource_set_private_data((IWineD3DResource *)iface, refguid, pData, SizeOfData, Flags);
142 }
143
144 static HRESULT WINAPI IWineD3DVolumeImpl_GetPrivateData(IWineD3DVolume *iface, REFGUID  refguid, void* pData, DWORD* pSizeOfData) {
145     return resource_get_private_data((IWineD3DResource *)iface, refguid, pData, pSizeOfData);
146 }
147
148 static HRESULT WINAPI IWineD3DVolumeImpl_FreePrivateData(IWineD3DVolume *iface, REFGUID refguid) {
149     return resource_free_private_data((IWineD3DResource *)iface, refguid);
150 }
151
152 static DWORD WINAPI IWineD3DVolumeImpl_SetPriority(IWineD3DVolume *iface, DWORD PriorityNew) {
153     return resource_set_priority((IWineD3DResource *)iface, PriorityNew);
154 }
155
156 static DWORD WINAPI IWineD3DVolumeImpl_GetPriority(IWineD3DVolume *iface) {
157     return resource_get_priority((IWineD3DResource *)iface);
158 }
159
160 static void WINAPI IWineD3DVolumeImpl_PreLoad(IWineD3DVolume *iface) {
161     FIXME("iface %p stub!\n", iface);
162 }
163
164 static void WINAPI IWineD3DVolumeImpl_UnLoad(IWineD3DVolume *iface) {
165     /* The whole content is shadowed on This->resource.allocatedMemory, and the
166      * texture name is managed by the VolumeTexture container
167      */
168     TRACE("(%p): Nothing to do\n", iface);
169 }
170
171 static WINED3DRESOURCETYPE WINAPI IWineD3DVolumeImpl_GetType(IWineD3DVolume *iface) {
172     return resource_get_type((IWineD3DResource *)iface);
173 }
174
175 /* *******************************************
176    IWineD3DVolume parts follow
177    ******************************************* */
178 static HRESULT WINAPI IWineD3DVolumeImpl_GetContainer(IWineD3DVolume *iface, REFIID riid, void** ppContainer) {
179     IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
180
181     TRACE("(This %p, riid %s, ppContainer %p)\n", This, debugstr_guid(riid), ppContainer);
182
183     if (!ppContainer) {
184         ERR("Called without a valid ppContainer.\n");
185     }
186
187     /* Although surfaces can be standalone, volumes can't */
188     if (!This->container) {
189         ERR("Volume without an container. Should not happen.\n");
190     }
191
192     TRACE("Relaying to QueryInterface\n");
193     return IUnknown_QueryInterface(This->container, riid, ppContainer);
194 }
195
196 static HRESULT WINAPI IWineD3DVolumeImpl_GetDesc(IWineD3DVolume *iface, WINED3DVOLUME_DESC* pDesc) {
197     IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
198     TRACE("(%p) : copying into %p\n", This, pDesc);
199
200     pDesc->Format = This->resource.format_desc->format;
201     pDesc->Type = This->resource.resourceType;
202     pDesc->Usage = This->resource.usage;
203     pDesc->Pool = This->resource.pool;
204     pDesc->Size = This->resource.size; /* dx8 only */
205     pDesc->Width = This->currentDesc.Width;
206     pDesc->Height = This->currentDesc.Height;
207     pDesc->Depth = This->currentDesc.Depth;
208
209     return WINED3D_OK;
210 }
211
212 static HRESULT WINAPI IWineD3DVolumeImpl_LockBox(IWineD3DVolume *iface, WINED3DLOCKED_BOX* pLockedVolume, CONST WINED3DBOX* pBox, DWORD Flags) {
213     IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
214     FIXME("(%p) : pBox=%p stub\n", This, pBox);
215
216     if(!This->resource.allocatedMemory) {
217         This->resource.allocatedMemory = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, This->resource.size);
218     }
219
220     /* fixme: should we really lock as such? */
221     TRACE("(%p) : box=%p, output pbox=%p, allMem=%p\n", This, pBox, pLockedVolume, This->resource.allocatedMemory);
222
223     pLockedVolume->RowPitch = This->resource.format_desc->byte_count * This->currentDesc.Width; /* Bytes / row   */
224     pLockedVolume->SlicePitch = This->resource.format_desc->byte_count
225             * This->currentDesc.Width * This->currentDesc.Height;                               /* Bytes / slice */
226     if (!pBox) {
227         TRACE("No box supplied - all is ok\n");
228         pLockedVolume->pBits = This->resource.allocatedMemory;
229         This->lockedBox.Left   = 0;
230         This->lockedBox.Top    = 0;
231         This->lockedBox.Front  = 0;
232         This->lockedBox.Right  = This->currentDesc.Width;
233         This->lockedBox.Bottom = This->currentDesc.Height;
234         This->lockedBox.Back   = This->currentDesc.Depth;
235     } else {
236         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);
237         pLockedVolume->pBits = This->resource.allocatedMemory
238                 + (pLockedVolume->SlicePitch * pBox->Front)     /* FIXME: is front < back or vica versa? */
239                 + (pLockedVolume->RowPitch * pBox->Top)
240                 + (pBox->Left * This->resource.format_desc->byte_count);
241         This->lockedBox.Left   = pBox->Left;
242         This->lockedBox.Top    = pBox->Top;
243         This->lockedBox.Front  = pBox->Front;
244         This->lockedBox.Right  = pBox->Right;
245         This->lockedBox.Bottom = pBox->Bottom;
246         This->lockedBox.Back   = pBox->Back;
247     }
248
249     if (Flags & (WINED3DLOCK_NO_DIRTY_UPDATE | WINED3DLOCK_READONLY)) {
250       /* Don't dirtify */
251     } else {
252       /**
253        * Dirtify on lock
254        * as seen in msdn docs
255        */
256       volume_add_dirty_box(iface, &This->lockedBox);
257
258       /**  Dirtify Container if needed */
259       if (NULL != This->container) {
260
261         IWineD3DVolumeTexture *cont = (IWineD3DVolumeTexture*) This->container;
262         WINED3DRESOURCETYPE containerType = IWineD3DBaseTexture_GetType((IWineD3DBaseTexture *) cont);
263
264         if (containerType == WINED3DRTYPE_VOLUMETEXTURE) {
265           IWineD3DBaseTextureImpl* pTexture = (IWineD3DBaseTextureImpl*) cont;
266           pTexture->baseTexture.dirty = TRUE;
267         } else {
268           FIXME("Set dirty on container type %d\n", containerType);
269         }
270       }
271     }
272
273     This->locked = TRUE;
274     TRACE("returning memory@%p rpitch(%d) spitch(%d)\n", pLockedVolume->pBits, pLockedVolume->RowPitch, pLockedVolume->SlicePitch);
275     return WINED3D_OK;
276 }
277
278 static HRESULT WINAPI IWineD3DVolumeImpl_UnlockBox(IWineD3DVolume *iface) {
279     IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
280     if (!This->locked) {
281       ERR("trying to lock unlocked volume@%p\n", This);
282       return WINED3DERR_INVALIDCALL;
283     }
284     TRACE("(%p) : unlocking volume\n", This);
285     This->locked = FALSE;
286     memset(&This->lockedBox, 0, sizeof(RECT));
287     return WINED3D_OK;
288 }
289
290 /* Internal use functions follow : */
291
292 static HRESULT WINAPI IWineD3DVolumeImpl_SetContainer(IWineD3DVolume *iface, IWineD3DBase* container) {
293     IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
294
295     TRACE("This %p, container %p\n", This, container);
296
297     /* We can't keep a reference to the container, since the container already keeps a reference to us. */
298
299     TRACE("Setting container to %p from %p\n", container, This->container);
300     This->container = container;
301
302     return WINED3D_OK;
303 }
304
305 /* Context activation is done by the caller. */
306 static HRESULT WINAPI IWineD3DVolumeImpl_LoadTexture(IWineD3DVolume *iface, int gl_level, BOOL srgb_mode) {
307     IWineD3DVolumeImpl *This     = (IWineD3DVolumeImpl *)iface;
308     const struct GlPixelFormatDesc *glDesc = This->resource.format_desc;
309
310     TRACE("(%p) : level %u, format %s (0x%08x)\n", This, gl_level, debug_d3dformat(glDesc->format), glDesc->format);
311
312     volume_bind_and_dirtify(iface);
313
314     TRACE("Calling glTexImage3D %x level=%d, intfmt=%x, w=%d, h=%d,d=%d, 0=%d, glFmt=%x, glType=%x, Mem=%p\n",
315             GL_TEXTURE_3D,
316             gl_level,
317             glDesc->glInternal,
318             This->currentDesc.Width,
319             This->currentDesc.Height,
320             This->currentDesc.Depth,
321             0,
322             glDesc->glFormat,
323             glDesc->glType,
324             This->resource.allocatedMemory);
325
326     ENTER_GL();
327     GL_EXTCALL(glTexImage3DEXT(GL_TEXTURE_3D,
328                 gl_level,
329                 glDesc->glInternal,
330                 This->currentDesc.Width,
331                 This->currentDesc.Height,
332                 This->currentDesc.Depth,
333                 0,
334                 glDesc->glFormat,
335                 glDesc->glType,
336                 This->resource.allocatedMemory));
337     checkGLcall("glTexImage3D");
338     LEAVE_GL();
339
340     /* When adding code releasing This->resource.allocatedMemory to save data keep in mind that
341      * GL_UNPACK_CLIENT_STORAGE_APPLE is enabled by default if supported(GL_APPLE_client_storage).
342      * Thus do not release This->resource.allocatedMemory if GL_APPLE_client_storage is supported.
343      */
344     return WINED3D_OK;
345
346 }
347
348 const IWineD3DVolumeVtbl IWineD3DVolume_Vtbl =
349 {
350     /* IUnknown */
351     IWineD3DVolumeImpl_QueryInterface,
352     IWineD3DVolumeImpl_AddRef,
353     IWineD3DVolumeImpl_Release,
354     /* IWineD3DResource */
355     IWineD3DVolumeImpl_GetParent,
356     IWineD3DVolumeImpl_GetDevice,
357     IWineD3DVolumeImpl_SetPrivateData,
358     IWineD3DVolumeImpl_GetPrivateData,
359     IWineD3DVolumeImpl_FreePrivateData,
360     IWineD3DVolumeImpl_SetPriority,
361     IWineD3DVolumeImpl_GetPriority,
362     IWineD3DVolumeImpl_PreLoad,
363     IWineD3DVolumeImpl_UnLoad,
364     IWineD3DVolumeImpl_GetType,
365     /* IWineD3DVolume */
366     IWineD3DVolumeImpl_GetContainer,
367     IWineD3DVolumeImpl_GetDesc,
368     IWineD3DVolumeImpl_LockBox,
369     IWineD3DVolumeImpl_UnlockBox,
370     /* Internal interface */
371     IWineD3DVolumeImpl_LoadTexture,
372     IWineD3DVolumeImpl_SetContainer
373 };