wined3d: Update Nvidia Geforce6+ driver version.
[wine] / dlls / wined3d / volumetexture.c
1 /*
2  * Copyright 2002-2005 Jason Edmeades
3  * Copyright 2002-2005 Raphael Junqueira
4  * Copyright 2005 Oliver Stieber
5  * Copyright 2009-2010 Henri Verbeet for CodeWeavers
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 #include "config.h"
23 #include "wined3d_private.h"
24
25 WINE_DEFAULT_DEBUG_CHANNEL(d3d_texture);
26
27 /* Context activation is done by the caller. */
28 static HRESULT volumetexture_bind(IWineD3DBaseTextureImpl *texture,
29         const struct wined3d_gl_info *gl_info, BOOL srgb)
30 {
31     BOOL dummy;
32
33     TRACE("texture %p, gl_info %p, srgb %#x.\n", texture, gl_info, srgb);
34
35     return basetexture_bind(texture, gl_info, srgb, &dummy);
36 }
37
38 /* Do not call while under the GL lock. */
39 static void volumetexture_preload(IWineD3DBaseTextureImpl *texture, enum WINED3DSRGB srgb)
40 {
41     IWineD3DDeviceImpl *device = texture->resource.device;
42     struct wined3d_context *context = NULL;
43     BOOL srgb_mode = texture->baseTexture.is_srgb;
44     BOOL srgb_was_toggled = FALSE;
45     unsigned int i;
46
47     TRACE("texture %p, srgb %#x.\n", texture, srgb);
48
49     if (!device->isInDraw) context = context_acquire(device, NULL);
50     else if (texture->baseTexture.bindCount > 0)
51     {
52         srgb_mode = device->stateBlock->state.sampler_states[texture->baseTexture.sampler][WINED3DSAMP_SRGBTEXTURE];
53         srgb_was_toggled = texture->baseTexture.is_srgb != srgb_mode;
54         texture->baseTexture.is_srgb = srgb_mode;
55     }
56
57     /* If the texture is marked dirty or the srgb sampler setting has changed
58      * since the last load then reload the volumes. */
59     if (texture->baseTexture.texture_rgb.dirty)
60     {
61         for (i = 0; i < texture->baseTexture.level_count; ++i)
62         {
63             volume_load(volume_from_resource(texture->baseTexture.sub_resources[i]), i, srgb_mode);
64         }
65     }
66     else if (srgb_was_toggled)
67     {
68         for (i = 0; i < texture->baseTexture.level_count; ++i)
69         {
70             IWineD3DVolumeImpl *volume = volume_from_resource(texture->baseTexture.sub_resources[i]);
71             volume_add_dirty_box(volume, NULL);
72             volume_load(volume, i, srgb_mode);
73         }
74     }
75     else
76     {
77         TRACE("Texture %p not dirty, nothing to do.\n", texture);
78     }
79
80     if (context) context_release(context);
81
82     /* No longer dirty */
83     texture->baseTexture.texture_rgb.dirty = FALSE;
84 }
85
86 /* Do not call while under the GL lock. */
87 static void volumetexture_unload(struct wined3d_resource *resource)
88 {
89     IWineD3DBaseTextureImpl *texture = basetexture_from_resource(resource);
90     unsigned int i;
91
92     TRACE("texture %p.\n", texture);
93
94     for (i = 0; i < texture->baseTexture.level_count; ++i)
95     {
96         struct wined3d_resource *sub_resource = texture->baseTexture.sub_resources[i];
97         sub_resource->resource_ops->resource_unload(sub_resource);
98     }
99
100     basetexture_unload(texture);
101 }
102
103 static const struct wined3d_texture_ops volumetexture_ops =
104 {
105     volumetexture_bind,
106     volumetexture_preload,
107 };
108
109 static const struct wined3d_resource_ops volumetexture_resource_ops =
110 {
111     volumetexture_unload,
112 };
113
114 static void volumetexture_cleanup(IWineD3DBaseTextureImpl *This)
115 {
116     unsigned int i;
117
118     TRACE("(%p) : Cleaning up.\n", This);
119
120     for (i = 0; i < This->baseTexture.level_count; ++i)
121     {
122         struct wined3d_resource *sub_resource = This->baseTexture.sub_resources[i];
123
124         if (sub_resource)
125         {
126             IWineD3DVolumeImpl *volume = volume_from_resource(sub_resource);
127
128             /* Cleanup the container. */
129             volume_set_container(volume, NULL);
130             IWineD3DVolume_Release((IWineD3DVolume *)volume);
131         }
132     }
133     basetexture_cleanup((IWineD3DBaseTextureImpl *)This);
134 }
135
136 /* *******************************************
137    IWineD3DTexture IUnknown parts follow
138    ******************************************* */
139
140 static HRESULT WINAPI IWineD3DVolumeTextureImpl_QueryInterface(IWineD3DBaseTexture *iface, REFIID riid, LPVOID *ppobj)
141 {
142     IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
143     TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppobj);
144     if (IsEqualGUID(riid, &IID_IUnknown)
145         || IsEqualGUID(riid, &IID_IWineD3DBase)
146         || IsEqualGUID(riid, &IID_IWineD3DResource)
147         || IsEqualGUID(riid, &IID_IWineD3DBaseTexture))
148     {
149         IUnknown_AddRef(iface);
150         *ppobj = This;
151         return S_OK;
152     }
153     *ppobj = NULL;
154     return E_NOINTERFACE;
155 }
156
157 static ULONG WINAPI IWineD3DVolumeTextureImpl_AddRef(IWineD3DBaseTexture *iface)
158 {
159     IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
160     TRACE("(%p) : AddRef increasing from %d\n", This, This->resource.ref);
161     return InterlockedIncrement(&This->resource.ref);
162 }
163
164 /* Do not call while under the GL lock. */
165 static ULONG WINAPI IWineD3DVolumeTextureImpl_Release(IWineD3DBaseTexture *iface)
166 {
167     IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
168     ULONG ref;
169     TRACE("(%p) : Releasing from %d\n", This, This->resource.ref);
170     ref = InterlockedDecrement(&This->resource.ref);
171     if (!ref)
172     {
173         volumetexture_cleanup(This);
174         This->resource.parent_ops->wined3d_object_destroyed(This->resource.parent);
175         HeapFree(GetProcessHeap(), 0, This);
176     }
177     return ref;
178 }
179
180 static HRESULT WINAPI IWineD3DVolumeTextureImpl_SetPrivateData(IWineD3DBaseTexture *iface,
181         REFGUID riid, const void *data, DWORD data_size, DWORD flags)
182 {
183     return resource_set_private_data(&((IWineD3DBaseTextureImpl *)iface)->resource, riid, data, data_size, flags);
184 }
185
186 static HRESULT WINAPI IWineD3DVolumeTextureImpl_GetPrivateData(IWineD3DBaseTexture *iface,
187         REFGUID guid, void *data, DWORD *data_size)
188 {
189     return resource_get_private_data(&((IWineD3DBaseTextureImpl *)iface)->resource, guid, data, data_size);
190 }
191
192 static HRESULT WINAPI IWineD3DVolumeTextureImpl_FreePrivateData(IWineD3DBaseTexture *iface, REFGUID refguid)
193 {
194     return resource_free_private_data(&((IWineD3DBaseTextureImpl *)iface)->resource, refguid);
195 }
196
197 static DWORD WINAPI IWineD3DVolumeTextureImpl_SetPriority(IWineD3DBaseTexture *iface, DWORD priority)
198 {
199     return resource_set_priority(&((IWineD3DBaseTextureImpl *)iface)->resource, priority);
200 }
201
202 static DWORD WINAPI IWineD3DVolumeTextureImpl_GetPriority(IWineD3DBaseTexture *iface)
203 {
204     return resource_get_priority(&((IWineD3DBaseTextureImpl *)iface)->resource);
205 }
206
207 static void WINAPI IWineD3DVolumeTextureImpl_PreLoad(IWineD3DBaseTexture *iface)
208 {
209     volumetexture_preload((IWineD3DBaseTextureImpl *)iface, SRGB_ANY);
210 }
211
212 static WINED3DRESOURCETYPE WINAPI IWineD3DVolumeTextureImpl_GetType(IWineD3DBaseTexture *iface)
213 {
214     return resource_get_type(&((IWineD3DBaseTextureImpl *)iface)->resource);
215 }
216
217 static void * WINAPI IWineD3DVolumeTextureImpl_GetParent(IWineD3DBaseTexture *iface)
218 {
219     TRACE("iface %p\n", iface);
220
221     return ((IWineD3DBaseTextureImpl *)iface)->resource.parent;
222 }
223
224 static DWORD WINAPI IWineD3DVolumeTextureImpl_SetLOD(IWineD3DBaseTexture *iface, DWORD LODNew)
225 {
226     return basetexture_set_lod((IWineD3DBaseTextureImpl *)iface, LODNew);
227 }
228
229 static DWORD WINAPI IWineD3DVolumeTextureImpl_GetLOD(IWineD3DBaseTexture *iface)
230 {
231     return basetexture_get_lod((IWineD3DBaseTextureImpl *)iface);
232 }
233
234 static DWORD WINAPI IWineD3DVolumeTextureImpl_GetLevelCount(IWineD3DBaseTexture *iface)
235 {
236     return basetexture_get_level_count((IWineD3DBaseTextureImpl *)iface);
237 }
238
239 static HRESULT WINAPI IWineD3DVolumeTextureImpl_SetAutoGenFilterType(IWineD3DBaseTexture *iface,
240         WINED3DTEXTUREFILTERTYPE FilterType)
241 {
242   return basetexture_set_autogen_filter_type((IWineD3DBaseTextureImpl *)iface, FilterType);
243 }
244
245 static WINED3DTEXTUREFILTERTYPE WINAPI IWineD3DVolumeTextureImpl_GetAutoGenFilterType(IWineD3DBaseTexture *iface)
246 {
247   return basetexture_get_autogen_filter_type((IWineD3DBaseTextureImpl *)iface);
248 }
249
250 static void WINAPI IWineD3DVolumeTextureImpl_GenerateMipSubLevels(IWineD3DBaseTexture *iface)
251 {
252     basetexture_generate_mipmaps((IWineD3DBaseTextureImpl *)iface);
253 }
254
255 static BOOL WINAPI IWineD3DVolumeTextureImpl_IsCondNP2(IWineD3DBaseTexture *iface)
256 {
257     TRACE("iface %p.\n", iface);
258
259     return FALSE;
260 }
261
262 static struct wined3d_resource * WINAPI IWineD3DVolumeTextureImpl_GetSubResource(IWineD3DBaseTexture *iface,
263         UINT sub_resource_idx)
264 {
265     IWineD3DBaseTextureImpl *texture = (IWineD3DBaseTextureImpl *)iface;
266
267     TRACE("iface %p, sub_resource_idx %u.\n", iface, sub_resource_idx);
268
269     return basetexture_get_sub_resource(texture, sub_resource_idx);
270 }
271
272 static HRESULT WINAPI IWineD3DVolumeTextureImpl_AddDirtyRegion(IWineD3DBaseTexture *iface,
273         UINT layer, const WINED3DBOX *dirty_region)
274 {
275     IWineD3DBaseTextureImpl *texture = (IWineD3DBaseTextureImpl *)iface;
276     struct wined3d_resource *sub_resource;
277
278     TRACE("iface %p, layer %u, dirty_region %p.\n", iface, layer, dirty_region);
279
280     if (!(sub_resource = basetexture_get_sub_resource(texture, layer * texture->baseTexture.level_count)))
281     {
282         WARN("Failed to get sub-resource.\n");
283         return WINED3DERR_INVALIDCALL;
284     }
285
286     basetexture_set_dirty(texture, TRUE);
287     volume_add_dirty_box(volume_from_resource(sub_resource), dirty_region);
288
289     return WINED3D_OK;
290 }
291
292 static const IWineD3DBaseTextureVtbl IWineD3DVolumeTexture_Vtbl =
293 {
294     /* IUnknown */
295     IWineD3DVolumeTextureImpl_QueryInterface,
296     IWineD3DVolumeTextureImpl_AddRef,
297     IWineD3DVolumeTextureImpl_Release,
298     /* resource */
299     IWineD3DVolumeTextureImpl_GetParent,
300     IWineD3DVolumeTextureImpl_SetPrivateData,
301     IWineD3DVolumeTextureImpl_GetPrivateData,
302     IWineD3DVolumeTextureImpl_FreePrivateData,
303     IWineD3DVolumeTextureImpl_SetPriority,
304     IWineD3DVolumeTextureImpl_GetPriority,
305     IWineD3DVolumeTextureImpl_PreLoad,
306     IWineD3DVolumeTextureImpl_GetType,
307     /* BaseTexture */
308     IWineD3DVolumeTextureImpl_SetLOD,
309     IWineD3DVolumeTextureImpl_GetLOD,
310     IWineD3DVolumeTextureImpl_GetLevelCount,
311     IWineD3DVolumeTextureImpl_SetAutoGenFilterType,
312     IWineD3DVolumeTextureImpl_GetAutoGenFilterType,
313     IWineD3DVolumeTextureImpl_GenerateMipSubLevels,
314     IWineD3DVolumeTextureImpl_IsCondNP2,
315     IWineD3DVolumeTextureImpl_GetSubResource,
316     IWineD3DVolumeTextureImpl_AddDirtyRegion,
317 };
318
319 HRESULT volumetexture_init(IWineD3DBaseTextureImpl *texture, UINT width, UINT height,
320         UINT depth, UINT levels, IWineD3DDeviceImpl *device, DWORD usage, enum wined3d_format_id format_id,
321         WINED3DPOOL pool, void *parent, const struct wined3d_parent_ops *parent_ops)
322 {
323     const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
324     const struct wined3d_format *format = wined3d_get_format(gl_info, format_id);
325     UINT tmp_w, tmp_h, tmp_d;
326     unsigned int i;
327     HRESULT hr;
328
329     /* TODO: It should only be possible to create textures for formats
330      * that are reported as supported. */
331     if (WINED3DFMT_UNKNOWN >= format_id)
332     {
333         WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture);
334         return WINED3DERR_INVALIDCALL;
335     }
336
337     if (!gl_info->supported[EXT_TEXTURE3D])
338     {
339         WARN("(%p) : Texture cannot be created - no volume texture support.\n", texture);
340         return WINED3DERR_INVALIDCALL;
341     }
342
343     /* Calculate levels for mip mapping. */
344     if (usage & WINED3DUSAGE_AUTOGENMIPMAP)
345     {
346         if (!gl_info->supported[SGIS_GENERATE_MIPMAP])
347         {
348             WARN("No mipmap generation support, returning D3DERR_INVALIDCALL.\n");
349             return WINED3DERR_INVALIDCALL;
350         }
351
352         if (levels > 1)
353         {
354             WARN("D3DUSAGE_AUTOGENMIPMAP is set, and level count > 1, returning D3DERR_INVALIDCALL.\n");
355             return WINED3DERR_INVALIDCALL;
356         }
357
358         levels = 1;
359     }
360     else if (!levels)
361     {
362         levels = wined3d_log2i(max(max(width, height), depth)) + 1;
363         TRACE("Calculated levels = %u.\n", levels);
364     }
365
366     texture->lpVtbl = &IWineD3DVolumeTexture_Vtbl;
367
368     hr = basetexture_init((IWineD3DBaseTextureImpl *)texture, &volumetexture_ops,
369             1, levels, WINED3DRTYPE_VOLUMETEXTURE, device, usage, format, pool,
370             parent, parent_ops, &volumetexture_resource_ops);
371     if (FAILED(hr))
372     {
373         WARN("Failed to initialize basetexture, returning %#x.\n", hr);
374         return hr;
375     }
376
377     /* Is NP2 support for volumes needed? */
378     texture->baseTexture.pow2Matrix[0] = 1.0f;
379     texture->baseTexture.pow2Matrix[5] = 1.0f;
380     texture->baseTexture.pow2Matrix[10] = 1.0f;
381     texture->baseTexture.pow2Matrix[15] = 1.0f;
382     texture->baseTexture.target = GL_TEXTURE_3D;
383
384     /* Generate all the surfaces. */
385     tmp_w = width;
386     tmp_h = height;
387     tmp_d = depth;
388
389     for (i = 0; i < texture->baseTexture.level_count; ++i)
390     {
391         IWineD3DVolume *volume;
392
393         /* Create the volume. */
394         hr = IWineD3DDeviceParent_CreateVolume(device->device_parent, parent,
395                 tmp_w, tmp_h, tmp_d, format_id, pool, usage, &volume);
396         if (FAILED(hr))
397         {
398             ERR("Creating a volume for the volume texture failed, hr %#x.\n", hr);
399             volumetexture_cleanup(texture);
400             return hr;
401         }
402
403         /* Set its container to this texture. */
404         volume_set_container((IWineD3DVolumeImpl *)volume, texture);
405         texture->baseTexture.sub_resources[i] = &((IWineD3DVolumeImpl *)volume)->resource;
406
407         /* Calculate the next mipmap level. */
408         tmp_w = max(1, tmp_w >> 1);
409         tmp_h = max(1, tmp_h >> 1);
410         tmp_d = max(1, tmp_d >> 1);
411     }
412
413     return WINED3D_OK;
414 }