wined3d: Tidy up pshader_hw_cnd.
[wine] / dlls / wined3d / volumetexture.c
1 /*
2  * IWineD3DVolumeTexture 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_texture);
27 #define GLINFO_LOCATION This->resource.wineD3DDevice->adapter->gl_info
28
29 /* *******************************************
30    IWineD3DTexture IUnknown parts follow
31    ******************************************* */
32 static HRESULT WINAPI IWineD3DVolumeTextureImpl_QueryInterface(IWineD3DVolumeTexture *iface, REFIID riid, LPVOID *ppobj)
33 {
34     IWineD3DVolumeTextureImpl *This = (IWineD3DVolumeTextureImpl *)iface;
35     TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppobj);
36     if (IsEqualGUID(riid, &IID_IUnknown)
37         || IsEqualGUID(riid, &IID_IWineD3DBase)
38         || IsEqualGUID(riid, &IID_IWineD3DResource)
39         || IsEqualGUID(riid, &IID_IWineD3DBaseTexture)
40         || IsEqualGUID(riid, &IID_IWineD3DVolumeTexture)) {
41         IUnknown_AddRef(iface);
42         *ppobj = This;
43         return S_OK;
44     }
45     *ppobj = NULL;
46     return E_NOINTERFACE;
47 }
48
49 static ULONG WINAPI IWineD3DVolumeTextureImpl_AddRef(IWineD3DVolumeTexture *iface) {
50     IWineD3DVolumeTextureImpl *This = (IWineD3DVolumeTextureImpl *)iface;
51     TRACE("(%p) : AddRef increasing from %d\n", This, This->resource.ref);
52     return InterlockedIncrement(&This->resource.ref);
53 }
54
55 static ULONG WINAPI IWineD3DVolumeTextureImpl_Release(IWineD3DVolumeTexture *iface) {
56     IWineD3DVolumeTextureImpl *This = (IWineD3DVolumeTextureImpl *)iface;
57     ULONG ref;
58     TRACE("(%p) : Releasing from %d\n", This, This->resource.ref);
59     ref = InterlockedDecrement(&This->resource.ref);
60     if (ref == 0) {
61         IWineD3DVolumeTexture_Destroy(iface, D3DCB_DefaultDestroyVolume);
62     }
63     return ref;
64 }
65
66 /* ****************************************************
67    IWineD3DVolumeTexture IWineD3DResource parts follow
68    **************************************************** */
69 static HRESULT WINAPI IWineD3DVolumeTextureImpl_GetDevice(IWineD3DVolumeTexture *iface, IWineD3DDevice** ppDevice) {
70     return resource_get_device((IWineD3DResource *)iface, ppDevice);
71 }
72
73 static HRESULT WINAPI IWineD3DVolumeTextureImpl_SetPrivateData(IWineD3DVolumeTexture *iface, REFGUID refguid, CONST void* pData, DWORD SizeOfData, DWORD Flags) {
74     return resource_set_private_data((IWineD3DResource *)iface, refguid, pData, SizeOfData, Flags);
75 }
76
77 static HRESULT WINAPI IWineD3DVolumeTextureImpl_GetPrivateData(IWineD3DVolumeTexture *iface, REFGUID refguid, void* pData, DWORD* pSizeOfData) {
78     return resource_get_private_data((IWineD3DResource *)iface, refguid, pData, pSizeOfData);
79 }
80
81 static HRESULT WINAPI IWineD3DVolumeTextureImpl_FreePrivateData(IWineD3DVolumeTexture *iface, REFGUID refguid) {
82     return resource_free_private_data((IWineD3DResource *)iface, refguid);
83 }
84
85 static DWORD WINAPI IWineD3DVolumeTextureImpl_SetPriority(IWineD3DVolumeTexture *iface, DWORD PriorityNew) {
86     return resource_set_priority((IWineD3DResource *)iface, PriorityNew);
87 }
88
89 static DWORD WINAPI IWineD3DVolumeTextureImpl_GetPriority(IWineD3DVolumeTexture *iface) {
90     return resource_get_priority((IWineD3DResource *)iface);
91 }
92
93 void volumetexture_internal_preload(IWineD3DBaseTexture *iface, enum WINED3DSRGB srgb) {
94     /* Overrider the IWineD3DResource Preload method */
95     unsigned int i;
96     IWineD3DVolumeTextureImpl *This = (IWineD3DVolumeTextureImpl *)iface;
97     IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
98     BOOL srgb_mode = This->baseTexture.is_srgb;
99     BOOL srgb_was_toggled = FALSE;
100
101     TRACE("(%p) : About to load texture\n", This);
102
103     if(!device->isInDraw) {
104         ActivateContext(device, device->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
105     } else if (GL_SUPPORT(EXT_TEXTURE_SRGB) && This->baseTexture.bindCount > 0) {
106         srgb_mode = device->stateBlock->samplerState[This->baseTexture.sampler][WINED3DSAMP_SRGBTEXTURE];
107         srgb_was_toggled = This->baseTexture.is_srgb != srgb_mode;
108         This->baseTexture.is_srgb = srgb_mode;
109     }
110
111     /* If the texture is marked dirty or the srgb sampler setting has changed since the last load then reload the surfaces */
112     if (This->baseTexture.dirty) {
113         for (i = 0; i < This->baseTexture.levels; i++)
114             IWineD3DVolume_LoadTexture(This->volumes[i], i, srgb_mode);
115     } else if (srgb_was_toggled) {
116         for (i = 0; i < This->baseTexture.levels; i++) {
117             volume_add_dirty_box(This->volumes[i], NULL);
118             IWineD3DVolume_LoadTexture(This->volumes[i], i, srgb_mode);
119         }
120     } else {
121         TRACE("(%p) Texture not dirty, nothing to do\n" , iface);
122     }
123
124     /* No longer dirty */
125     This->baseTexture.dirty = FALSE;
126
127     return ;
128 }
129
130 static void WINAPI IWineD3DVolumeTextureImpl_PreLoad(IWineD3DVolumeTexture *iface) {
131     volumetexture_internal_preload((IWineD3DBaseTexture *) iface, SRGB_ANY);
132 }
133
134 static void WINAPI IWineD3DVolumeTextureImpl_UnLoad(IWineD3DVolumeTexture *iface) {
135     unsigned int i;
136     IWineD3DVolumeTextureImpl *This = (IWineD3DVolumeTextureImpl *)iface;
137     TRACE("(%p)\n", This);
138
139     /* Unload all the surfaces and reset the texture name. If UnLoad was called on the
140      * surface before, this one will be a NOP and vice versa. Unloading an unloaded
141      * surface is fine
142      */
143     for (i = 0; i < This->baseTexture.levels; i++) {
144         IWineD3DVolume_UnLoad(This->volumes[i]);
145     }
146
147     basetexture_unload((IWineD3DBaseTexture *)iface);
148 }
149
150 static WINED3DRESOURCETYPE WINAPI IWineD3DVolumeTextureImpl_GetType(IWineD3DVolumeTexture *iface) {
151     return resource_get_type((IWineD3DResource *)iface);
152 }
153
154 static HRESULT WINAPI IWineD3DVolumeTextureImpl_GetParent(IWineD3DVolumeTexture *iface, IUnknown **pParent) {
155     return resource_get_parent((IWineD3DResource *)iface, pParent);
156 }
157
158 /* ******************************************************
159    IWineD3DVolumeTexture IWineD3DBaseTexture parts follow
160    ****************************************************** */
161 static DWORD WINAPI IWineD3DVolumeTextureImpl_SetLOD(IWineD3DVolumeTexture *iface, DWORD LODNew) {
162     return basetexture_set_lod((IWineD3DBaseTexture *)iface, LODNew);
163 }
164
165 static DWORD WINAPI IWineD3DVolumeTextureImpl_GetLOD(IWineD3DVolumeTexture *iface) {
166     return basetexture_get_lod((IWineD3DBaseTexture *)iface);
167 }
168
169 static DWORD WINAPI IWineD3DVolumeTextureImpl_GetLevelCount(IWineD3DVolumeTexture *iface) {
170     return basetexture_get_level_count((IWineD3DBaseTexture *)iface);
171 }
172
173 static HRESULT WINAPI IWineD3DVolumeTextureImpl_SetAutoGenFilterType(IWineD3DVolumeTexture *iface, WINED3DTEXTUREFILTERTYPE FilterType) {
174   return basetexture_set_autogen_filter_type((IWineD3DBaseTexture *)iface, FilterType);
175 }
176
177 static WINED3DTEXTUREFILTERTYPE WINAPI IWineD3DVolumeTextureImpl_GetAutoGenFilterType(IWineD3DVolumeTexture *iface) {
178   return basetexture_get_autogen_filter_type((IWineD3DBaseTexture *)iface);
179 }
180
181 static void WINAPI IWineD3DVolumeTextureImpl_GenerateMipSubLevels(IWineD3DVolumeTexture *iface) {
182     basetexture_generate_mipmaps((IWineD3DBaseTexture *)iface);
183 }
184
185 /* Internal function, No d3d mapping */
186 static BOOL WINAPI IWineD3DVolumeTextureImpl_SetDirty(IWineD3DVolumeTexture *iface, BOOL dirty) {
187     return basetexture_set_dirty((IWineD3DBaseTexture *)iface, dirty);
188 }
189
190 static BOOL WINAPI IWineD3DVolumeTextureImpl_GetDirty(IWineD3DVolumeTexture *iface) {
191     return basetexture_get_dirty((IWineD3DBaseTexture *)iface);
192 }
193
194 static HRESULT WINAPI IWineD3DVolumeTextureImpl_BindTexture(IWineD3DVolumeTexture *iface, BOOL srgb) {
195     IWineD3DVolumeTextureImpl *This = (IWineD3DVolumeTextureImpl *)iface;
196     BOOL dummy;
197     TRACE("(%p) : relay to BaseTexture\n", This);
198     return basetexture_bind((IWineD3DBaseTexture *)iface, srgb, &dummy);
199 }
200
201 static UINT WINAPI IWineD3DVolumeTextureImpl_GetTextureDimensions(IWineD3DVolumeTexture *iface) {
202     IWineD3DVolumeTextureImpl *This = (IWineD3DVolumeTextureImpl *)iface;
203     TRACE("(%p)\n", This);
204     return GL_TEXTURE_3D;
205 }
206
207 static BOOL WINAPI IWineD3DVolumeTextureImpl_IsCondNP2(IWineD3DVolumeTexture *iface) {
208     IWineD3DVolumeTextureImpl *This = (IWineD3DVolumeTextureImpl *)iface;
209     TRACE("(%p)\n", This);
210
211     return FALSE;
212 }
213
214 /* *******************************************
215    IWineD3DVolumeTexture IWineD3DVolumeTexture parts follow
216    ******************************************* */
217 static void WINAPI IWineD3DVolumeTextureImpl_Destroy(IWineD3DVolumeTexture *iface, D3DCB_DESTROYVOLUMEFN D3DCB_DestroyVolume) {
218     IWineD3DVolumeTextureImpl *This = (IWineD3DVolumeTextureImpl *)iface;
219     unsigned int i;
220     TRACE("(%p) : Cleaning up\n",This);
221     for (i = 0; i < This->baseTexture.levels; i++) {
222         if (This->volumes[i] != NULL) {
223             /* Cleanup the container */
224             IWineD3DVolume_SetContainer(This->volumes[i], 0);
225             D3DCB_DestroyVolume(This->volumes[i]);
226         }
227     }
228     basetexture_cleanup((IWineD3DBaseTexture *)iface);
229     HeapFree(GetProcessHeap(), 0, This);
230 }
231
232 static HRESULT WINAPI IWineD3DVolumeTextureImpl_GetLevelDesc(IWineD3DVolumeTexture *iface, UINT Level,WINED3DVOLUME_DESC *pDesc) {
233     IWineD3DVolumeTextureImpl *This = (IWineD3DVolumeTextureImpl *)iface;
234     if (Level < This->baseTexture.levels) {
235         TRACE("(%p) Level (%d)\n", This, Level);
236         return IWineD3DVolume_GetDesc(This->volumes[Level], pDesc);
237     } else {
238         WARN("(%p) Level (%d)\n", This, Level);
239     }
240     return WINED3D_OK;
241 }
242 static HRESULT WINAPI IWineD3DVolumeTextureImpl_GetVolumeLevel(IWineD3DVolumeTexture *iface, UINT Level, IWineD3DVolume** ppVolumeLevel) {
243     IWineD3DVolumeTextureImpl *This = (IWineD3DVolumeTextureImpl *)iface;
244     if (Level < This->baseTexture.levels) {
245       *ppVolumeLevel = This->volumes[Level];
246       IWineD3DVolume_AddRef(*ppVolumeLevel);
247       TRACE("(%p) -> level(%d) returning volume@%p\n", This, Level, *ppVolumeLevel);
248     } else {
249       WARN("(%p) Level(%d) overflow Levels(%d)\n", This, Level, This->baseTexture.levels);
250       return WINED3DERR_INVALIDCALL;
251     }
252     return WINED3D_OK;
253
254 }
255 static HRESULT WINAPI IWineD3DVolumeTextureImpl_LockBox(IWineD3DVolumeTexture *iface, UINT Level, WINED3DLOCKED_BOX* pLockedVolume, CONST WINED3DBOX* pBox, DWORD Flags) {
256     HRESULT hr;
257     IWineD3DVolumeTextureImpl *This = (IWineD3DVolumeTextureImpl *)iface;
258
259     if (Level < This->baseTexture.levels) {
260       hr = IWineD3DVolume_LockBox(This->volumes[Level], pLockedVolume, pBox, Flags);
261       TRACE("(%p) Level (%d) success(%u)\n", This, Level, hr);
262
263     } else {
264       FIXME("(%p) level(%d) overflow Levels(%d)\n", This, Level, This->baseTexture.levels);
265       return WINED3DERR_INVALIDCALL;
266     }
267     return hr;
268 }
269
270 static HRESULT WINAPI IWineD3DVolumeTextureImpl_UnlockBox(IWineD3DVolumeTexture *iface, UINT Level) {
271     HRESULT hr;
272     IWineD3DVolumeTextureImpl *This = (IWineD3DVolumeTextureImpl *)iface;
273
274     if (Level < This->baseTexture.levels) {
275       hr = IWineD3DVolume_UnlockBox(This->volumes[Level]);
276       TRACE("(%p) -> level(%d) success(%u)\n", This, Level, hr);
277
278     } else {
279       FIXME("(%p) level(%d) overflow Levels(%d)\n", This, Level, This->baseTexture.levels);
280       return WINED3DERR_INVALIDCALL;
281     }
282     return hr;
283 }
284
285 static HRESULT WINAPI IWineD3DVolumeTextureImpl_AddDirtyBox(IWineD3DVolumeTexture *iface, CONST WINED3DBOX* pDirtyBox) {
286     IWineD3DVolumeTextureImpl *This = (IWineD3DVolumeTextureImpl *)iface;
287     This->baseTexture.dirty = TRUE;
288     TRACE("(%p) : dirtyfication of volume Level (0)\n", This);
289     volume_add_dirty_box(This->volumes[0], pDirtyBox);
290
291     return WINED3D_OK;
292 }
293
294 const IWineD3DVolumeTextureVtbl IWineD3DVolumeTexture_Vtbl =
295 {
296     /* IUnknown */
297     IWineD3DVolumeTextureImpl_QueryInterface,
298     IWineD3DVolumeTextureImpl_AddRef,
299     IWineD3DVolumeTextureImpl_Release,
300     /* resource */
301     IWineD3DVolumeTextureImpl_GetParent,
302     IWineD3DVolumeTextureImpl_GetDevice,
303     IWineD3DVolumeTextureImpl_SetPrivateData,
304     IWineD3DVolumeTextureImpl_GetPrivateData,
305     IWineD3DVolumeTextureImpl_FreePrivateData,
306     IWineD3DVolumeTextureImpl_SetPriority,
307     IWineD3DVolumeTextureImpl_GetPriority,
308     IWineD3DVolumeTextureImpl_PreLoad,
309     IWineD3DVolumeTextureImpl_UnLoad,
310     IWineD3DVolumeTextureImpl_GetType,
311     /* BaseTexture */
312     IWineD3DVolumeTextureImpl_SetLOD,
313     IWineD3DVolumeTextureImpl_GetLOD,
314     IWineD3DVolumeTextureImpl_GetLevelCount,
315     IWineD3DVolumeTextureImpl_SetAutoGenFilterType,
316     IWineD3DVolumeTextureImpl_GetAutoGenFilterType,
317     IWineD3DVolumeTextureImpl_GenerateMipSubLevels,
318     IWineD3DVolumeTextureImpl_SetDirty,
319     IWineD3DVolumeTextureImpl_GetDirty,
320     /* not in d3d */
321     IWineD3DVolumeTextureImpl_BindTexture,
322     IWineD3DVolumeTextureImpl_GetTextureDimensions,
323     IWineD3DVolumeTextureImpl_IsCondNP2,
324     /* volume texture */
325     IWineD3DVolumeTextureImpl_Destroy,
326     IWineD3DVolumeTextureImpl_GetLevelDesc,
327     IWineD3DVolumeTextureImpl_GetVolumeLevel,
328     IWineD3DVolumeTextureImpl_LockBox,
329     IWineD3DVolumeTextureImpl_UnlockBox,
330     IWineD3DVolumeTextureImpl_AddDirtyBox
331 };