d3d10: Implement ID3D10EffectVariable::AsVector().
[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 struct 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, NULL, 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 struct 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     /* Is NP2 support for volumes needed? */
154     texture->baseTexture.pow2Matrix[0] = 1.0f;
155     texture->baseTexture.pow2Matrix[5] = 1.0f;
156     texture->baseTexture.pow2Matrix[10] = 1.0f;
157     texture->baseTexture.pow2Matrix[15] = 1.0f;
158
159     /* Generate all the surfaces. */
160     tmp_w = width;
161     tmp_h = height;
162     tmp_d = depth;
163
164     for (i = 0; i < texture->baseTexture.levels; ++i)
165     {
166         /* Create the volume. */
167         hr = IWineD3DDeviceParent_CreateVolume(device->device_parent, parent,
168                 tmp_w, tmp_h, tmp_d, format, pool, usage, &texture->volumes[i]);
169         if (FAILED(hr))
170         {
171             ERR("Creating a volume for the volume texture failed, hr %#x.\n", hr);
172             texture->volumes[i] = NULL;
173             volumetexture_cleanup(texture, D3DCB_DefaultDestroyVolume);
174             return hr;
175         }
176
177         /* Set its container to this texture. */
178         IWineD3DVolume_SetContainer(texture->volumes[i], (IWineD3DBase *)texture);
179
180         /* Calculate the next mipmap level. */
181         tmp_w = max(1, tmp_w >> 1);
182         tmp_h = max(1, tmp_h >> 1);
183         tmp_d = max(1, tmp_d >> 1);
184     }
185     texture->baseTexture.internal_preload = volumetexture_internal_preload;
186
187     return WINED3D_OK;
188 }
189
190 #undef GLINFO_LOCATION
191
192 /* *******************************************
193    IWineD3DTexture IUnknown parts follow
194    ******************************************* */
195
196 #define GLINFO_LOCATION This->resource.wineD3DDevice->adapter->gl_info
197
198 static HRESULT WINAPI IWineD3DVolumeTextureImpl_QueryInterface(IWineD3DVolumeTexture *iface, REFIID riid, LPVOID *ppobj)
199 {
200     IWineD3DVolumeTextureImpl *This = (IWineD3DVolumeTextureImpl *)iface;
201     TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppobj);
202     if (IsEqualGUID(riid, &IID_IUnknown)
203         || IsEqualGUID(riid, &IID_IWineD3DBase)
204         || IsEqualGUID(riid, &IID_IWineD3DResource)
205         || IsEqualGUID(riid, &IID_IWineD3DBaseTexture)
206         || IsEqualGUID(riid, &IID_IWineD3DVolumeTexture)) {
207         IUnknown_AddRef(iface);
208         *ppobj = This;
209         return S_OK;
210     }
211     *ppobj = NULL;
212     return E_NOINTERFACE;
213 }
214
215 static ULONG WINAPI IWineD3DVolumeTextureImpl_AddRef(IWineD3DVolumeTexture *iface) {
216     IWineD3DVolumeTextureImpl *This = (IWineD3DVolumeTextureImpl *)iface;
217     TRACE("(%p) : AddRef increasing from %d\n", This, This->resource.ref);
218     return InterlockedIncrement(&This->resource.ref);
219 }
220
221 static ULONG WINAPI IWineD3DVolumeTextureImpl_Release(IWineD3DVolumeTexture *iface) {
222     IWineD3DVolumeTextureImpl *This = (IWineD3DVolumeTextureImpl *)iface;
223     ULONG ref;
224     TRACE("(%p) : Releasing from %d\n", This, This->resource.ref);
225     ref = InterlockedDecrement(&This->resource.ref);
226     if (ref == 0) {
227         IWineD3DVolumeTexture_Destroy(iface, D3DCB_DefaultDestroyVolume);
228     }
229     return ref;
230 }
231
232 /* ****************************************************
233    IWineD3DVolumeTexture IWineD3DResource parts follow
234    **************************************************** */
235 static HRESULT WINAPI IWineD3DVolumeTextureImpl_GetDevice(IWineD3DVolumeTexture *iface, IWineD3DDevice** ppDevice) {
236     return resource_get_device((IWineD3DResource *)iface, ppDevice);
237 }
238
239 static HRESULT WINAPI IWineD3DVolumeTextureImpl_SetPrivateData(IWineD3DVolumeTexture *iface, REFGUID refguid, CONST void* pData, DWORD SizeOfData, DWORD Flags) {
240     return resource_set_private_data((IWineD3DResource *)iface, refguid, pData, SizeOfData, Flags);
241 }
242
243 static HRESULT WINAPI IWineD3DVolumeTextureImpl_GetPrivateData(IWineD3DVolumeTexture *iface, REFGUID refguid, void* pData, DWORD* pSizeOfData) {
244     return resource_get_private_data((IWineD3DResource *)iface, refguid, pData, pSizeOfData);
245 }
246
247 static HRESULT WINAPI IWineD3DVolumeTextureImpl_FreePrivateData(IWineD3DVolumeTexture *iface, REFGUID refguid) {
248     return resource_free_private_data((IWineD3DResource *)iface, refguid);
249 }
250
251 static DWORD WINAPI IWineD3DVolumeTextureImpl_SetPriority(IWineD3DVolumeTexture *iface, DWORD PriorityNew) {
252     return resource_set_priority((IWineD3DResource *)iface, PriorityNew);
253 }
254
255 static DWORD WINAPI IWineD3DVolumeTextureImpl_GetPriority(IWineD3DVolumeTexture *iface) {
256     return resource_get_priority((IWineD3DResource *)iface);
257 }
258
259 static void WINAPI IWineD3DVolumeTextureImpl_PreLoad(IWineD3DVolumeTexture *iface) {
260     volumetexture_internal_preload((IWineD3DBaseTexture *) iface, SRGB_ANY);
261 }
262
263 static void WINAPI IWineD3DVolumeTextureImpl_UnLoad(IWineD3DVolumeTexture *iface) {
264     unsigned int i;
265     IWineD3DVolumeTextureImpl *This = (IWineD3DVolumeTextureImpl *)iface;
266     TRACE("(%p)\n", This);
267
268     /* Unload all the surfaces and reset the texture name. If UnLoad was called on the
269      * surface before, this one will be a NOP and vice versa. Unloading an unloaded
270      * surface is fine
271      */
272     for (i = 0; i < This->baseTexture.levels; i++) {
273         IWineD3DVolume_UnLoad(This->volumes[i]);
274     }
275
276     basetexture_unload((IWineD3DBaseTexture *)iface);
277 }
278
279 static WINED3DRESOURCETYPE WINAPI IWineD3DVolumeTextureImpl_GetType(IWineD3DVolumeTexture *iface) {
280     return resource_get_type((IWineD3DResource *)iface);
281 }
282
283 static HRESULT WINAPI IWineD3DVolumeTextureImpl_GetParent(IWineD3DVolumeTexture *iface, IUnknown **pParent) {
284     return resource_get_parent((IWineD3DResource *)iface, pParent);
285 }
286
287 /* ******************************************************
288    IWineD3DVolumeTexture IWineD3DBaseTexture parts follow
289    ****************************************************** */
290 static DWORD WINAPI IWineD3DVolumeTextureImpl_SetLOD(IWineD3DVolumeTexture *iface, DWORD LODNew) {
291     return basetexture_set_lod((IWineD3DBaseTexture *)iface, LODNew);
292 }
293
294 static DWORD WINAPI IWineD3DVolumeTextureImpl_GetLOD(IWineD3DVolumeTexture *iface) {
295     return basetexture_get_lod((IWineD3DBaseTexture *)iface);
296 }
297
298 static DWORD WINAPI IWineD3DVolumeTextureImpl_GetLevelCount(IWineD3DVolumeTexture *iface) {
299     return basetexture_get_level_count((IWineD3DBaseTexture *)iface);
300 }
301
302 static HRESULT WINAPI IWineD3DVolumeTextureImpl_SetAutoGenFilterType(IWineD3DVolumeTexture *iface, WINED3DTEXTUREFILTERTYPE FilterType) {
303   return basetexture_set_autogen_filter_type((IWineD3DBaseTexture *)iface, FilterType);
304 }
305
306 static WINED3DTEXTUREFILTERTYPE WINAPI IWineD3DVolumeTextureImpl_GetAutoGenFilterType(IWineD3DVolumeTexture *iface) {
307   return basetexture_get_autogen_filter_type((IWineD3DBaseTexture *)iface);
308 }
309
310 static void WINAPI IWineD3DVolumeTextureImpl_GenerateMipSubLevels(IWineD3DVolumeTexture *iface) {
311     basetexture_generate_mipmaps((IWineD3DBaseTexture *)iface);
312 }
313
314 /* Internal function, No d3d mapping */
315 static BOOL WINAPI IWineD3DVolumeTextureImpl_SetDirty(IWineD3DVolumeTexture *iface, BOOL dirty) {
316     return basetexture_set_dirty((IWineD3DBaseTexture *)iface, dirty);
317 }
318
319 static BOOL WINAPI IWineD3DVolumeTextureImpl_GetDirty(IWineD3DVolumeTexture *iface) {
320     return basetexture_get_dirty((IWineD3DBaseTexture *)iface);
321 }
322
323 /* Context activation is done by the caller. */
324 static HRESULT WINAPI IWineD3DVolumeTextureImpl_BindTexture(IWineD3DVolumeTexture *iface, BOOL srgb) {
325     IWineD3DVolumeTextureImpl *This = (IWineD3DVolumeTextureImpl *)iface;
326     BOOL dummy;
327     TRACE("(%p) : relay to BaseTexture\n", This);
328     return basetexture_bind((IWineD3DBaseTexture *)iface, srgb, &dummy);
329 }
330
331 static UINT WINAPI IWineD3DVolumeTextureImpl_GetTextureDimensions(IWineD3DVolumeTexture *iface) {
332     IWineD3DVolumeTextureImpl *This = (IWineD3DVolumeTextureImpl *)iface;
333     TRACE("(%p)\n", This);
334     return GL_TEXTURE_3D;
335 }
336
337 static BOOL WINAPI IWineD3DVolumeTextureImpl_IsCondNP2(IWineD3DVolumeTexture *iface) {
338     IWineD3DVolumeTextureImpl *This = (IWineD3DVolumeTextureImpl *)iface;
339     TRACE("(%p)\n", This);
340
341     return FALSE;
342 }
343
344 /* *******************************************
345    IWineD3DVolumeTexture IWineD3DVolumeTexture parts follow
346    ******************************************* */
347 static void WINAPI IWineD3DVolumeTextureImpl_Destroy(IWineD3DVolumeTexture *iface, D3DCB_DESTROYVOLUMEFN D3DCB_DestroyVolume) {
348     IWineD3DVolumeTextureImpl *This = (IWineD3DVolumeTextureImpl *)iface;
349
350     volumetexture_cleanup(This, D3DCB_DestroyVolume);
351
352     HeapFree(GetProcessHeap(), 0, This);
353 }
354
355 static HRESULT WINAPI IWineD3DVolumeTextureImpl_GetLevelDesc(IWineD3DVolumeTexture *iface, UINT Level,WINED3DVOLUME_DESC *pDesc) {
356     IWineD3DVolumeTextureImpl *This = (IWineD3DVolumeTextureImpl *)iface;
357     if (Level < This->baseTexture.levels) {
358         TRACE("(%p) Level (%d)\n", This, Level);
359         return IWineD3DVolume_GetDesc(This->volumes[Level], pDesc);
360     } else {
361         WARN("(%p) Level (%d)\n", This, Level);
362     }
363     return WINED3D_OK;
364 }
365 static HRESULT WINAPI IWineD3DVolumeTextureImpl_GetVolumeLevel(IWineD3DVolumeTexture *iface, UINT Level, IWineD3DVolume** ppVolumeLevel) {
366     IWineD3DVolumeTextureImpl *This = (IWineD3DVolumeTextureImpl *)iface;
367     if (Level < This->baseTexture.levels) {
368       *ppVolumeLevel = This->volumes[Level];
369       IWineD3DVolume_AddRef(*ppVolumeLevel);
370       TRACE("(%p) -> level(%d) returning volume@%p\n", This, Level, *ppVolumeLevel);
371     } else {
372       WARN("(%p) Level(%d) overflow Levels(%d)\n", This, Level, This->baseTexture.levels);
373       return WINED3DERR_INVALIDCALL;
374     }
375     return WINED3D_OK;
376
377 }
378 static HRESULT WINAPI IWineD3DVolumeTextureImpl_LockBox(IWineD3DVolumeTexture *iface, UINT Level, WINED3DLOCKED_BOX* pLockedVolume, CONST WINED3DBOX* pBox, DWORD Flags) {
379     HRESULT hr;
380     IWineD3DVolumeTextureImpl *This = (IWineD3DVolumeTextureImpl *)iface;
381
382     if (Level < This->baseTexture.levels) {
383       hr = IWineD3DVolume_LockBox(This->volumes[Level], pLockedVolume, pBox, Flags);
384       TRACE("(%p) Level (%d) success(%u)\n", This, Level, hr);
385
386     } else {
387       FIXME("(%p) level(%d) overflow Levels(%d)\n", This, Level, This->baseTexture.levels);
388       return WINED3DERR_INVALIDCALL;
389     }
390     return hr;
391 }
392
393 static HRESULT WINAPI IWineD3DVolumeTextureImpl_UnlockBox(IWineD3DVolumeTexture *iface, UINT Level) {
394     HRESULT hr;
395     IWineD3DVolumeTextureImpl *This = (IWineD3DVolumeTextureImpl *)iface;
396
397     if (Level < This->baseTexture.levels) {
398       hr = IWineD3DVolume_UnlockBox(This->volumes[Level]);
399       TRACE("(%p) -> level(%d) success(%u)\n", This, Level, hr);
400
401     } else {
402       FIXME("(%p) level(%d) overflow Levels(%d)\n", This, Level, This->baseTexture.levels);
403       return WINED3DERR_INVALIDCALL;
404     }
405     return hr;
406 }
407
408 static HRESULT WINAPI IWineD3DVolumeTextureImpl_AddDirtyBox(IWineD3DVolumeTexture *iface, CONST WINED3DBOX* pDirtyBox) {
409     IWineD3DVolumeTextureImpl *This = (IWineD3DVolumeTextureImpl *)iface;
410     This->baseTexture.dirty = TRUE;
411     TRACE("(%p) : dirtyfication of volume Level (0)\n", This);
412     volume_add_dirty_box(This->volumes[0], pDirtyBox);
413
414     return WINED3D_OK;
415 }
416
417 const IWineD3DVolumeTextureVtbl IWineD3DVolumeTexture_Vtbl =
418 {
419     /* IUnknown */
420     IWineD3DVolumeTextureImpl_QueryInterface,
421     IWineD3DVolumeTextureImpl_AddRef,
422     IWineD3DVolumeTextureImpl_Release,
423     /* resource */
424     IWineD3DVolumeTextureImpl_GetParent,
425     IWineD3DVolumeTextureImpl_GetDevice,
426     IWineD3DVolumeTextureImpl_SetPrivateData,
427     IWineD3DVolumeTextureImpl_GetPrivateData,
428     IWineD3DVolumeTextureImpl_FreePrivateData,
429     IWineD3DVolumeTextureImpl_SetPriority,
430     IWineD3DVolumeTextureImpl_GetPriority,
431     IWineD3DVolumeTextureImpl_PreLoad,
432     IWineD3DVolumeTextureImpl_UnLoad,
433     IWineD3DVolumeTextureImpl_GetType,
434     /* BaseTexture */
435     IWineD3DVolumeTextureImpl_SetLOD,
436     IWineD3DVolumeTextureImpl_GetLOD,
437     IWineD3DVolumeTextureImpl_GetLevelCount,
438     IWineD3DVolumeTextureImpl_SetAutoGenFilterType,
439     IWineD3DVolumeTextureImpl_GetAutoGenFilterType,
440     IWineD3DVolumeTextureImpl_GenerateMipSubLevels,
441     IWineD3DVolumeTextureImpl_SetDirty,
442     IWineD3DVolumeTextureImpl_GetDirty,
443     /* not in d3d */
444     IWineD3DVolumeTextureImpl_BindTexture,
445     IWineD3DVolumeTextureImpl_GetTextureDimensions,
446     IWineD3DVolumeTextureImpl_IsCondNP2,
447     /* volume texture */
448     IWineD3DVolumeTextureImpl_Destroy,
449     IWineD3DVolumeTextureImpl_GetLevelDesc,
450     IWineD3DVolumeTextureImpl_GetVolumeLevel,
451     IWineD3DVolumeTextureImpl_LockBox,
452     IWineD3DVolumeTextureImpl_UnlockBox,
453     IWineD3DVolumeTextureImpl_AddDirtyBox
454 };