wined3d: Document GL context dependencies.
[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
28 #define GLINFO_LOCATION (*gl_info)
29
30 static void volumetexture_internal_preload(IWineD3DBaseTexture *iface, enum WINED3DSRGB srgb)
31 {
32     /* Override the IWineD3DResource Preload method. */
33     IWineD3DVolumeTextureImpl *This = (IWineD3DVolumeTextureImpl *)iface;
34     IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
35     const WineD3D_GL_Info *gl_info = &device->adapter->gl_info;
36     BOOL srgb_mode = This->baseTexture.is_srgb;
37     BOOL srgb_was_toggled = FALSE;
38     unsigned int i;
39
40     TRACE("(%p) : About to load texture.\n", This);
41
42     if (!device->isInDraw)
43     {
44         ActivateContext(device, device->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
45     }
46     else if (GL_SUPPORT(EXT_TEXTURE_SRGB) && This->baseTexture.bindCount > 0)
47     {
48         srgb_mode = device->stateBlock->samplerState[This->baseTexture.sampler][WINED3DSAMP_SRGBTEXTURE];
49         srgb_was_toggled = This->baseTexture.is_srgb != srgb_mode;
50         This->baseTexture.is_srgb = srgb_mode;
51     }
52
53     /* If the texture is marked dirty or the srgb sampler setting has changed
54      * since the last load then reload the volumes. */
55     if (This->baseTexture.dirty)
56     {
57         for (i = 0; i < This->baseTexture.levels; ++i)
58         {
59             IWineD3DVolume_LoadTexture(This->volumes[i], i, srgb_mode);
60         }
61     }
62     else if (srgb_was_toggled)
63     {
64         for (i = 0; i < This->baseTexture.levels; ++i)
65         {
66             volume_add_dirty_box(This->volumes[i], NULL);
67             IWineD3DVolume_LoadTexture(This->volumes[i], i, srgb_mode);
68         }
69     }
70     else
71     {
72         TRACE("(%p) Texture not dirty, nothing to do.\n", iface);
73     }
74
75     /* No longer dirty */
76     This->baseTexture.dirty = FALSE;
77 }
78
79 static void volumetexture_cleanup(IWineD3DVolumeTextureImpl *This, D3DCB_DESTROYVOLUMEFN volume_destroy_cb)
80 {
81     unsigned int i;
82
83     TRACE("(%p) : Cleaning up.\n", This);
84
85     for (i = 0; i < This->baseTexture.levels; ++i)
86     {
87         IWineD3DVolume *volume = This->volumes[i];
88
89         if (volume)
90         {
91             /* Cleanup the container. */
92             IWineD3DVolume_SetContainer(volume, NULL);
93             volume_destroy_cb(volume);
94         }
95     }
96     basetexture_cleanup((IWineD3DBaseTexture *)This);
97 }
98
99 HRESULT volumetexture_init(IWineD3DVolumeTextureImpl *texture, UINT width, UINT height, UINT depth, UINT levels,
100         IWineD3DDeviceImpl *device, DWORD usage, WINED3DFORMAT format, WINED3DPOOL pool, IUnknown *parent)
101 {
102     const WineD3D_GL_Info *gl_info = &device->adapter->gl_info;
103     const struct GlPixelFormatDesc *format_desc = getFormatDescEntry(format, gl_info);
104     UINT tmp_w, tmp_h, tmp_d;
105     unsigned int i;
106     HRESULT hr;
107
108     /* TODO: It should only be possible to create textures for formats
109      * that are reported as supported. */
110     if (WINED3DFMT_UNKNOWN >= format)
111     {
112         WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture);
113         return WINED3DERR_INVALIDCALL;
114     }
115
116     if (!GL_SUPPORT(EXT_TEXTURE3D))
117     {
118         WARN("(%p) : Texture cannot be created - no volume texture support.\n", texture);
119         return WINED3DERR_INVALIDCALL;
120     }
121
122     /* Calculate levels for mip mapping. */
123     if (usage & WINED3DUSAGE_AUTOGENMIPMAP)
124     {
125         if (!GL_SUPPORT(SGIS_GENERATE_MIPMAP))
126         {
127             WARN("No mipmap generation support, returning D3DERR_INVALIDCALL.\n");
128             return WINED3DERR_INVALIDCALL;
129         }
130
131         if (levels > 1)
132         {
133             WARN("D3DUSAGE_AUTOGENMIPMAP is set, and level count > 1, returning D3DERR_INVALIDCALL.\n");
134             return WINED3DERR_INVALIDCALL;
135         }
136
137         levels = 1;
138     }
139     else if (!levels)
140     {
141         levels = wined3d_log2i(max(max(width, height), depth)) + 1;
142         TRACE("Calculated levels = %u.\n", levels);
143     }
144
145     hr = basetexture_init((IWineD3DBaseTextureImpl *)texture, levels,
146             WINED3DRTYPE_VOLUMETEXTURE, device, 0, usage, format_desc, pool, parent);
147     if (FAILED(hr))
148     {
149         WARN("Failed to initialize basetexture, returning %#x.\n", hr);
150         return hr;
151     }
152
153     if (texture->resource.format_desc->Flags & WINED3DFMT_FLAG_FILTERING)
154     {
155         texture->baseTexture.minMipLookup = minMipLookup;
156         texture->baseTexture.magLookup = magLookup;
157     }
158     else
159     {
160         texture->baseTexture.minMipLookup = minMipLookup_noFilter;
161         texture->baseTexture.magLookup = magLookup_noFilter;
162     }
163
164     /* Is NP2 support for volumes needed? */
165     texture->baseTexture.pow2Matrix[0] = 1.0;
166     texture->baseTexture.pow2Matrix[5] = 1.0;
167     texture->baseTexture.pow2Matrix[10] = 1.0;
168     texture->baseTexture.pow2Matrix[15] = 1.0;
169
170     /* Generate all the surfaces. */
171     tmp_w = width;
172     tmp_h = height;
173     tmp_d = depth;
174
175     for (i = 0; i < texture->baseTexture.levels; ++i)
176     {
177         /* Create the volume. */
178         hr = IWineD3DDeviceParent_CreateVolume(device->device_parent, parent,
179                 tmp_w, tmp_h, tmp_d, format, pool, usage, &texture->volumes[i]);
180         if (FAILED(hr))
181         {
182             ERR("Creating a volume for the volume texture failed, hr %#x.\n", hr);
183             texture->volumes[i] = NULL;
184             volumetexture_cleanup(texture, D3DCB_DefaultDestroyVolume);
185             return hr;
186         }
187
188         /* Set its container to this texture. */
189         IWineD3DVolume_SetContainer(texture->volumes[i], (IWineD3DBase *)texture);
190
191         /* Calculate the next mipmap level. */
192         tmp_w = max(1, tmp_w >> 1);
193         tmp_h = max(1, tmp_h >> 1);
194         tmp_d = max(1, tmp_d >> 1);
195     }
196     texture->baseTexture.internal_preload = volumetexture_internal_preload;
197
198     return WINED3D_OK;
199 }
200
201 #undef GLINFO_LOCATION
202
203 /* *******************************************
204    IWineD3DTexture IUnknown parts follow
205    ******************************************* */
206
207 #define GLINFO_LOCATION This->resource.wineD3DDevice->adapter->gl_info
208
209 static HRESULT WINAPI IWineD3DVolumeTextureImpl_QueryInterface(IWineD3DVolumeTexture *iface, REFIID riid, LPVOID *ppobj)
210 {
211     IWineD3DVolumeTextureImpl *This = (IWineD3DVolumeTextureImpl *)iface;
212     TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppobj);
213     if (IsEqualGUID(riid, &IID_IUnknown)
214         || IsEqualGUID(riid, &IID_IWineD3DBase)
215         || IsEqualGUID(riid, &IID_IWineD3DResource)
216         || IsEqualGUID(riid, &IID_IWineD3DBaseTexture)
217         || IsEqualGUID(riid, &IID_IWineD3DVolumeTexture)) {
218         IUnknown_AddRef(iface);
219         *ppobj = This;
220         return S_OK;
221     }
222     *ppobj = NULL;
223     return E_NOINTERFACE;
224 }
225
226 static ULONG WINAPI IWineD3DVolumeTextureImpl_AddRef(IWineD3DVolumeTexture *iface) {
227     IWineD3DVolumeTextureImpl *This = (IWineD3DVolumeTextureImpl *)iface;
228     TRACE("(%p) : AddRef increasing from %d\n", This, This->resource.ref);
229     return InterlockedIncrement(&This->resource.ref);
230 }
231
232 static ULONG WINAPI IWineD3DVolumeTextureImpl_Release(IWineD3DVolumeTexture *iface) {
233     IWineD3DVolumeTextureImpl *This = (IWineD3DVolumeTextureImpl *)iface;
234     ULONG ref;
235     TRACE("(%p) : Releasing from %d\n", This, This->resource.ref);
236     ref = InterlockedDecrement(&This->resource.ref);
237     if (ref == 0) {
238         IWineD3DVolumeTexture_Destroy(iface, D3DCB_DefaultDestroyVolume);
239     }
240     return ref;
241 }
242
243 /* ****************************************************
244    IWineD3DVolumeTexture IWineD3DResource parts follow
245    **************************************************** */
246 static HRESULT WINAPI IWineD3DVolumeTextureImpl_GetDevice(IWineD3DVolumeTexture *iface, IWineD3DDevice** ppDevice) {
247     return resource_get_device((IWineD3DResource *)iface, ppDevice);
248 }
249
250 static HRESULT WINAPI IWineD3DVolumeTextureImpl_SetPrivateData(IWineD3DVolumeTexture *iface, REFGUID refguid, CONST void* pData, DWORD SizeOfData, DWORD Flags) {
251     return resource_set_private_data((IWineD3DResource *)iface, refguid, pData, SizeOfData, Flags);
252 }
253
254 static HRESULT WINAPI IWineD3DVolumeTextureImpl_GetPrivateData(IWineD3DVolumeTexture *iface, REFGUID refguid, void* pData, DWORD* pSizeOfData) {
255     return resource_get_private_data((IWineD3DResource *)iface, refguid, pData, pSizeOfData);
256 }
257
258 static HRESULT WINAPI IWineD3DVolumeTextureImpl_FreePrivateData(IWineD3DVolumeTexture *iface, REFGUID refguid) {
259     return resource_free_private_data((IWineD3DResource *)iface, refguid);
260 }
261
262 static DWORD WINAPI IWineD3DVolumeTextureImpl_SetPriority(IWineD3DVolumeTexture *iface, DWORD PriorityNew) {
263     return resource_set_priority((IWineD3DResource *)iface, PriorityNew);
264 }
265
266 static DWORD WINAPI IWineD3DVolumeTextureImpl_GetPriority(IWineD3DVolumeTexture *iface) {
267     return resource_get_priority((IWineD3DResource *)iface);
268 }
269
270 static void WINAPI IWineD3DVolumeTextureImpl_PreLoad(IWineD3DVolumeTexture *iface) {
271     volumetexture_internal_preload((IWineD3DBaseTexture *) iface, SRGB_ANY);
272 }
273
274 static void WINAPI IWineD3DVolumeTextureImpl_UnLoad(IWineD3DVolumeTexture *iface) {
275     unsigned int i;
276     IWineD3DVolumeTextureImpl *This = (IWineD3DVolumeTextureImpl *)iface;
277     TRACE("(%p)\n", This);
278
279     /* Unload all the surfaces and reset the texture name. If UnLoad was called on the
280      * surface before, this one will be a NOP and vice versa. Unloading an unloaded
281      * surface is fine
282      */
283     for (i = 0; i < This->baseTexture.levels; i++) {
284         IWineD3DVolume_UnLoad(This->volumes[i]);
285     }
286
287     basetexture_unload((IWineD3DBaseTexture *)iface);
288 }
289
290 static WINED3DRESOURCETYPE WINAPI IWineD3DVolumeTextureImpl_GetType(IWineD3DVolumeTexture *iface) {
291     return resource_get_type((IWineD3DResource *)iface);
292 }
293
294 static HRESULT WINAPI IWineD3DVolumeTextureImpl_GetParent(IWineD3DVolumeTexture *iface, IUnknown **pParent) {
295     return resource_get_parent((IWineD3DResource *)iface, pParent);
296 }
297
298 /* ******************************************************
299    IWineD3DVolumeTexture IWineD3DBaseTexture parts follow
300    ****************************************************** */
301 static DWORD WINAPI IWineD3DVolumeTextureImpl_SetLOD(IWineD3DVolumeTexture *iface, DWORD LODNew) {
302     return basetexture_set_lod((IWineD3DBaseTexture *)iface, LODNew);
303 }
304
305 static DWORD WINAPI IWineD3DVolumeTextureImpl_GetLOD(IWineD3DVolumeTexture *iface) {
306     return basetexture_get_lod((IWineD3DBaseTexture *)iface);
307 }
308
309 static DWORD WINAPI IWineD3DVolumeTextureImpl_GetLevelCount(IWineD3DVolumeTexture *iface) {
310     return basetexture_get_level_count((IWineD3DBaseTexture *)iface);
311 }
312
313 static HRESULT WINAPI IWineD3DVolumeTextureImpl_SetAutoGenFilterType(IWineD3DVolumeTexture *iface, WINED3DTEXTUREFILTERTYPE FilterType) {
314   return basetexture_set_autogen_filter_type((IWineD3DBaseTexture *)iface, FilterType);
315 }
316
317 static WINED3DTEXTUREFILTERTYPE WINAPI IWineD3DVolumeTextureImpl_GetAutoGenFilterType(IWineD3DVolumeTexture *iface) {
318   return basetexture_get_autogen_filter_type((IWineD3DBaseTexture *)iface);
319 }
320
321 static void WINAPI IWineD3DVolumeTextureImpl_GenerateMipSubLevels(IWineD3DVolumeTexture *iface) {
322     basetexture_generate_mipmaps((IWineD3DBaseTexture *)iface);
323 }
324
325 /* Internal function, No d3d mapping */
326 static BOOL WINAPI IWineD3DVolumeTextureImpl_SetDirty(IWineD3DVolumeTexture *iface, BOOL dirty) {
327     return basetexture_set_dirty((IWineD3DBaseTexture *)iface, dirty);
328 }
329
330 static BOOL WINAPI IWineD3DVolumeTextureImpl_GetDirty(IWineD3DVolumeTexture *iface) {
331     return basetexture_get_dirty((IWineD3DBaseTexture *)iface);
332 }
333
334 /* Context activation is done by the caller. */
335 static HRESULT WINAPI IWineD3DVolumeTextureImpl_BindTexture(IWineD3DVolumeTexture *iface, BOOL srgb) {
336     IWineD3DVolumeTextureImpl *This = (IWineD3DVolumeTextureImpl *)iface;
337     BOOL dummy;
338     TRACE("(%p) : relay to BaseTexture\n", This);
339     return basetexture_bind((IWineD3DBaseTexture *)iface, srgb, &dummy);
340 }
341
342 static UINT WINAPI IWineD3DVolumeTextureImpl_GetTextureDimensions(IWineD3DVolumeTexture *iface) {
343     IWineD3DVolumeTextureImpl *This = (IWineD3DVolumeTextureImpl *)iface;
344     TRACE("(%p)\n", This);
345     return GL_TEXTURE_3D;
346 }
347
348 static BOOL WINAPI IWineD3DVolumeTextureImpl_IsCondNP2(IWineD3DVolumeTexture *iface) {
349     IWineD3DVolumeTextureImpl *This = (IWineD3DVolumeTextureImpl *)iface;
350     TRACE("(%p)\n", This);
351
352     return FALSE;
353 }
354
355 /* *******************************************
356    IWineD3DVolumeTexture IWineD3DVolumeTexture parts follow
357    ******************************************* */
358 static void WINAPI IWineD3DVolumeTextureImpl_Destroy(IWineD3DVolumeTexture *iface, D3DCB_DESTROYVOLUMEFN D3DCB_DestroyVolume) {
359     IWineD3DVolumeTextureImpl *This = (IWineD3DVolumeTextureImpl *)iface;
360
361     volumetexture_cleanup(This, D3DCB_DestroyVolume);
362
363     HeapFree(GetProcessHeap(), 0, This);
364 }
365
366 static HRESULT WINAPI IWineD3DVolumeTextureImpl_GetLevelDesc(IWineD3DVolumeTexture *iface, UINT Level,WINED3DVOLUME_DESC *pDesc) {
367     IWineD3DVolumeTextureImpl *This = (IWineD3DVolumeTextureImpl *)iface;
368     if (Level < This->baseTexture.levels) {
369         TRACE("(%p) Level (%d)\n", This, Level);
370         return IWineD3DVolume_GetDesc(This->volumes[Level], pDesc);
371     } else {
372         WARN("(%p) Level (%d)\n", This, Level);
373     }
374     return WINED3D_OK;
375 }
376 static HRESULT WINAPI IWineD3DVolumeTextureImpl_GetVolumeLevel(IWineD3DVolumeTexture *iface, UINT Level, IWineD3DVolume** ppVolumeLevel) {
377     IWineD3DVolumeTextureImpl *This = (IWineD3DVolumeTextureImpl *)iface;
378     if (Level < This->baseTexture.levels) {
379       *ppVolumeLevel = This->volumes[Level];
380       IWineD3DVolume_AddRef(*ppVolumeLevel);
381       TRACE("(%p) -> level(%d) returning volume@%p\n", This, Level, *ppVolumeLevel);
382     } else {
383       WARN("(%p) Level(%d) overflow Levels(%d)\n", This, Level, This->baseTexture.levels);
384       return WINED3DERR_INVALIDCALL;
385     }
386     return WINED3D_OK;
387
388 }
389 static HRESULT WINAPI IWineD3DVolumeTextureImpl_LockBox(IWineD3DVolumeTexture *iface, UINT Level, WINED3DLOCKED_BOX* pLockedVolume, CONST WINED3DBOX* pBox, DWORD Flags) {
390     HRESULT hr;
391     IWineD3DVolumeTextureImpl *This = (IWineD3DVolumeTextureImpl *)iface;
392
393     if (Level < This->baseTexture.levels) {
394       hr = IWineD3DVolume_LockBox(This->volumes[Level], pLockedVolume, pBox, Flags);
395       TRACE("(%p) Level (%d) success(%u)\n", This, Level, hr);
396
397     } else {
398       FIXME("(%p) level(%d) overflow Levels(%d)\n", This, Level, This->baseTexture.levels);
399       return WINED3DERR_INVALIDCALL;
400     }
401     return hr;
402 }
403
404 static HRESULT WINAPI IWineD3DVolumeTextureImpl_UnlockBox(IWineD3DVolumeTexture *iface, UINT Level) {
405     HRESULT hr;
406     IWineD3DVolumeTextureImpl *This = (IWineD3DVolumeTextureImpl *)iface;
407
408     if (Level < This->baseTexture.levels) {
409       hr = IWineD3DVolume_UnlockBox(This->volumes[Level]);
410       TRACE("(%p) -> level(%d) success(%u)\n", This, Level, hr);
411
412     } else {
413       FIXME("(%p) level(%d) overflow Levels(%d)\n", This, Level, This->baseTexture.levels);
414       return WINED3DERR_INVALIDCALL;
415     }
416     return hr;
417 }
418
419 static HRESULT WINAPI IWineD3DVolumeTextureImpl_AddDirtyBox(IWineD3DVolumeTexture *iface, CONST WINED3DBOX* pDirtyBox) {
420     IWineD3DVolumeTextureImpl *This = (IWineD3DVolumeTextureImpl *)iface;
421     This->baseTexture.dirty = TRUE;
422     TRACE("(%p) : dirtyfication of volume Level (0)\n", This);
423     volume_add_dirty_box(This->volumes[0], pDirtyBox);
424
425     return WINED3D_OK;
426 }
427
428 const IWineD3DVolumeTextureVtbl IWineD3DVolumeTexture_Vtbl =
429 {
430     /* IUnknown */
431     IWineD3DVolumeTextureImpl_QueryInterface,
432     IWineD3DVolumeTextureImpl_AddRef,
433     IWineD3DVolumeTextureImpl_Release,
434     /* resource */
435     IWineD3DVolumeTextureImpl_GetParent,
436     IWineD3DVolumeTextureImpl_GetDevice,
437     IWineD3DVolumeTextureImpl_SetPrivateData,
438     IWineD3DVolumeTextureImpl_GetPrivateData,
439     IWineD3DVolumeTextureImpl_FreePrivateData,
440     IWineD3DVolumeTextureImpl_SetPriority,
441     IWineD3DVolumeTextureImpl_GetPriority,
442     IWineD3DVolumeTextureImpl_PreLoad,
443     IWineD3DVolumeTextureImpl_UnLoad,
444     IWineD3DVolumeTextureImpl_GetType,
445     /* BaseTexture */
446     IWineD3DVolumeTextureImpl_SetLOD,
447     IWineD3DVolumeTextureImpl_GetLOD,
448     IWineD3DVolumeTextureImpl_GetLevelCount,
449     IWineD3DVolumeTextureImpl_SetAutoGenFilterType,
450     IWineD3DVolumeTextureImpl_GetAutoGenFilterType,
451     IWineD3DVolumeTextureImpl_GenerateMipSubLevels,
452     IWineD3DVolumeTextureImpl_SetDirty,
453     IWineD3DVolumeTextureImpl_GetDirty,
454     /* not in d3d */
455     IWineD3DVolumeTextureImpl_BindTexture,
456     IWineD3DVolumeTextureImpl_GetTextureDimensions,
457     IWineD3DVolumeTextureImpl_IsCondNP2,
458     /* volume texture */
459     IWineD3DVolumeTextureImpl_Destroy,
460     IWineD3DVolumeTextureImpl_GetLevelDesc,
461     IWineD3DVolumeTextureImpl_GetVolumeLevel,
462     IWineD3DVolumeTextureImpl_LockBox,
463     IWineD3DVolumeTextureImpl_UnlockBox,
464     IWineD3DVolumeTextureImpl_AddDirtyBox
465 };