vbscript: Added beginning bytecode compiler implementation.
[wine] / dlls / wined3d / texture.c
1 /*
2  * Copyright 2002-2005 Jason Edmeades
3  * Copyright 2002-2005 Raphael Junqueira
4  * Copyright 2005 Oliver Stieber
5  * Copyright 2007-2008 Stefan Dösinger for CodeWeavers
6  * Copyright 2009-2011 Henri Verbeet for CodeWeavers
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 static HRESULT wined3d_texture_init(struct wined3d_texture *texture, const struct wined3d_texture_ops *texture_ops,
29         UINT layer_count, UINT level_count, WINED3DRESOURCETYPE resource_type, struct wined3d_device *device,
30         DWORD usage, const struct wined3d_format *format, WINED3DPOOL pool, void *parent,
31         const struct wined3d_parent_ops *parent_ops, const struct wined3d_resource_ops *resource_ops)
32 {
33     HRESULT hr;
34
35     hr = resource_init(&texture->resource, device, resource_type, format,
36             WINED3DMULTISAMPLE_NONE, 0, usage, pool, 0, 0, 0, 0,
37             parent, parent_ops, resource_ops);
38     if (FAILED(hr))
39     {
40         WARN("Failed to initialize resource, returning %#x\n", hr);
41         return hr;
42     }
43
44     texture->texture_ops = texture_ops;
45     texture->sub_resources = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
46             level_count * layer_count * sizeof(*texture->sub_resources));
47     if (!texture->sub_resources)
48     {
49         ERR("Failed to allocate sub-resource array.\n");
50         resource_cleanup(&texture->resource);
51         return E_OUTOFMEMORY;
52     }
53
54     texture->layer_count = layer_count;
55     texture->level_count = level_count;
56     texture->filter_type = (usage & WINED3DUSAGE_AUTOGENMIPMAP) ? WINED3DTEXF_LINEAR : WINED3DTEXF_NONE;
57     texture->lod = 0;
58     texture->texture_rgb.dirty = TRUE;
59     texture->texture_srgb.dirty = TRUE;
60     texture->flags = WINED3D_TEXTURE_POW2_MAT_IDENT;
61
62     if (texture->resource.format->flags & WINED3DFMT_FLAG_FILTERING)
63     {
64         texture->min_mip_lookup = minMipLookup;
65         texture->mag_lookup = magLookup;
66     }
67     else
68     {
69         texture->min_mip_lookup = minMipLookup_noFilter;
70         texture->mag_lookup = magLookup_noFilter;
71     }
72
73     return WINED3D_OK;
74 }
75
76 /* A GL context is provided by the caller */
77 static void gltexture_delete(struct gl_texture *tex)
78 {
79     ENTER_GL();
80     glDeleteTextures(1, &tex->name);
81     LEAVE_GL();
82     tex->name = 0;
83 }
84
85 static void wined3d_texture_unload(struct wined3d_texture *texture)
86 {
87     struct wined3d_device *device = texture->resource.device;
88     struct wined3d_context *context = NULL;
89
90     if (texture->texture_rgb.name || texture->texture_srgb.name)
91     {
92         context = context_acquire(device, NULL);
93     }
94
95     if (texture->texture_rgb.name)
96         gltexture_delete(&texture->texture_rgb);
97
98     if (texture->texture_srgb.name)
99         gltexture_delete(&texture->texture_srgb);
100
101     if (context) context_release(context);
102
103     wined3d_texture_set_dirty(texture, TRUE);
104
105     resource_unload(&texture->resource);
106 }
107
108 static void wined3d_texture_cleanup(struct wined3d_texture *texture)
109 {
110     UINT sub_count = texture->level_count * texture->layer_count;
111     UINT i;
112
113     TRACE("texture %p.\n", texture);
114
115     for (i = 0; i < sub_count; ++i)
116     {
117         struct wined3d_resource *sub_resource = texture->sub_resources[i];
118
119         if (sub_resource)
120             texture->texture_ops->texture_sub_resource_cleanup(sub_resource);
121     }
122
123     wined3d_texture_unload(texture);
124     HeapFree(GetProcessHeap(), 0, texture->sub_resources);
125     resource_cleanup(&texture->resource);
126 }
127
128 void wined3d_texture_set_dirty(struct wined3d_texture *texture, BOOL dirty)
129 {
130     texture->texture_rgb.dirty = dirty;
131     texture->texture_srgb.dirty = dirty;
132 }
133
134 /* Context activation is done by the caller. */
135 static HRESULT wined3d_texture_bind(struct wined3d_texture *texture,
136         struct wined3d_context *context, BOOL srgb, BOOL *set_surface_desc)
137 {
138     struct gl_texture *gl_tex;
139     BOOL new_texture = FALSE;
140     HRESULT hr = WINED3D_OK;
141     GLenum target;
142
143     TRACE("texture %p, context %p, srgb %#x, set_surface_desc %p.\n", texture, context, srgb, set_surface_desc);
144
145     /* sRGB mode cache for preload() calls outside drawprim. */
146     if (srgb)
147         texture->flags |= WINED3D_TEXTURE_IS_SRGB;
148     else
149         texture->flags &= ~WINED3D_TEXTURE_IS_SRGB;
150
151     gl_tex = wined3d_texture_get_gl_texture(texture, context->gl_info, srgb);
152     target = texture->target;
153
154     ENTER_GL();
155     /* Generate a texture name if we don't already have one. */
156     if (!gl_tex->name)
157     {
158         *set_surface_desc = TRUE;
159         glGenTextures(1, &gl_tex->name);
160         checkGLcall("glGenTextures");
161         TRACE("Generated texture %d.\n", gl_tex->name);
162         if (texture->resource.pool == WINED3DPOOL_DEFAULT)
163         {
164             /* Tell OpenGL to try and keep this texture in video ram (well mostly). */
165             GLclampf tmp = 0.9f;
166             glPrioritizeTextures(1, &gl_tex->name, &tmp);
167         }
168         /* Initialise the state of the texture object to the OpenGL defaults,
169          * not the D3D defaults. */
170         gl_tex->states[WINED3DTEXSTA_ADDRESSU] = WINED3DTADDRESS_WRAP;
171         gl_tex->states[WINED3DTEXSTA_ADDRESSV] = WINED3DTADDRESS_WRAP;
172         gl_tex->states[WINED3DTEXSTA_ADDRESSW] = WINED3DTADDRESS_WRAP;
173         gl_tex->states[WINED3DTEXSTA_BORDERCOLOR] = 0;
174         gl_tex->states[WINED3DTEXSTA_MAGFILTER] = WINED3DTEXF_LINEAR;
175         gl_tex->states[WINED3DTEXSTA_MINFILTER] = WINED3DTEXF_POINT; /* GL_NEAREST_MIPMAP_LINEAR */
176         gl_tex->states[WINED3DTEXSTA_MIPFILTER] = WINED3DTEXF_LINEAR; /* GL_NEAREST_MIPMAP_LINEAR */
177         gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL] = 0;
178         gl_tex->states[WINED3DTEXSTA_MAXANISOTROPY] = 1;
179         if (context->gl_info->supported[EXT_TEXTURE_SRGB_DECODE])
180             gl_tex->states[WINED3DTEXSTA_SRGBTEXTURE] = TRUE;
181         else
182             gl_tex->states[WINED3DTEXSTA_SRGBTEXTURE] = srgb;
183         gl_tex->states[WINED3DTEXSTA_SHADOW] = FALSE;
184         wined3d_texture_set_dirty(texture, TRUE);
185         new_texture = TRUE;
186
187         if (texture->resource.usage & WINED3DUSAGE_AUTOGENMIPMAP)
188         {
189             /* This means double binding the texture at creation, but keeps
190              * the code simpler all in all, and the run-time path free from
191              * additional checks. */
192             context_bind_texture(context, target, gl_tex->name);
193             glTexParameteri(target, GL_GENERATE_MIPMAP_SGIS, GL_TRUE);
194             checkGLcall("glTexParameteri(target, GL_GENERATE_MIPMAP_SGIS, GL_TRUE)");
195         }
196     }
197     else
198     {
199         *set_surface_desc = FALSE;
200     }
201
202     if (gl_tex->name)
203     {
204         context_bind_texture(context, target, gl_tex->name);
205         if (new_texture)
206         {
207             /* For a new texture we have to set the texture levels after
208              * binding the texture. Beware that texture rectangles do not
209              * support mipmapping, but set the maxmiplevel if we're relying
210              * on the partial GL_ARB_texture_non_power_of_two emulation with
211              * texture rectangles. (I.e., do not care about cond_np2 here,
212              * just look for GL_TEXTURE_RECTANGLE_ARB.) */
213             if (target != GL_TEXTURE_RECTANGLE_ARB)
214             {
215                 TRACE("Setting GL_TEXTURE_MAX_LEVEL to %u.\n", texture->level_count - 1);
216                 glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, texture->level_count - 1);
217                 checkGLcall("glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, texture->level_count)");
218             }
219             if (target == GL_TEXTURE_CUBE_MAP_ARB)
220             {
221                 /* Cubemaps are always set to clamp, regardless of the sampler state. */
222                 glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
223                 glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
224                 glTexParameteri(target, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
225             }
226         }
227     }
228     else
229     {
230         ERR("This texture doesn't have an OpenGL texture assigned to it.\n");
231         hr = WINED3DERR_INVALIDCALL;
232     }
233
234     LEAVE_GL();
235     return hr;
236 }
237
238 /* GL locking is done by the caller */
239 static void apply_wrap(const struct wined3d_gl_info *gl_info, GLenum target,
240         WINED3DTEXTUREADDRESS d3d_wrap, GLenum param, BOOL cond_np2)
241 {
242     GLint gl_wrap;
243
244     if (d3d_wrap < WINED3DTADDRESS_WRAP || d3d_wrap > WINED3DTADDRESS_MIRRORONCE)
245     {
246         FIXME("Unrecognized or unsupported WINED3DTEXTUREADDRESS %#x.\n", d3d_wrap);
247         return;
248     }
249
250     /* Cubemaps are always set to clamp, regardless of the sampler state. */
251     if (target == GL_TEXTURE_CUBE_MAP_ARB
252             || (cond_np2 && d3d_wrap == WINED3DTADDRESS_WRAP))
253         gl_wrap = GL_CLAMP_TO_EDGE;
254     else
255         gl_wrap = gl_info->wrap_lookup[d3d_wrap - WINED3DTADDRESS_WRAP];
256
257     TRACE("Setting param %#x to %#x for target %#x.\n", param, gl_wrap, target);
258     glTexParameteri(target, param, gl_wrap);
259     checkGLcall("glTexParameteri(target, param, gl_wrap)");
260 }
261
262 /* GL locking is done by the caller (state handler) */
263 void wined3d_texture_apply_state_changes(struct wined3d_texture *texture,
264         const DWORD sampler_states[WINED3D_HIGHEST_SAMPLER_STATE + 1],
265         const struct wined3d_gl_info *gl_info)
266 {
267     BOOL cond_np2 = texture->flags & WINED3D_TEXTURE_COND_NP2;
268     GLenum target = texture->target;
269     struct gl_texture *gl_tex;
270     DWORD state;
271     DWORD aniso;
272
273     TRACE("texture %p, sampler_states %p.\n", texture, sampler_states);
274
275     gl_tex = wined3d_texture_get_gl_texture(texture, gl_info,
276             texture->flags & WINED3D_TEXTURE_IS_SRGB);
277
278     /* This function relies on the correct texture being bound and loaded. */
279
280     if (sampler_states[WINED3DSAMP_ADDRESSU] != gl_tex->states[WINED3DTEXSTA_ADDRESSU])
281     {
282         state = sampler_states[WINED3DSAMP_ADDRESSU];
283         apply_wrap(gl_info, target, state, GL_TEXTURE_WRAP_S, cond_np2);
284         gl_tex->states[WINED3DTEXSTA_ADDRESSU] = state;
285     }
286
287     if (sampler_states[WINED3DSAMP_ADDRESSV] != gl_tex->states[WINED3DTEXSTA_ADDRESSV])
288     {
289         state = sampler_states[WINED3DSAMP_ADDRESSV];
290         apply_wrap(gl_info, target, state, GL_TEXTURE_WRAP_T, cond_np2);
291         gl_tex->states[WINED3DTEXSTA_ADDRESSV] = state;
292     }
293
294     if (sampler_states[WINED3DSAMP_ADDRESSW] != gl_tex->states[WINED3DTEXSTA_ADDRESSW])
295     {
296         state = sampler_states[WINED3DSAMP_ADDRESSW];
297         apply_wrap(gl_info, target, state, GL_TEXTURE_WRAP_R, cond_np2);
298         gl_tex->states[WINED3DTEXSTA_ADDRESSW] = state;
299     }
300
301     if (sampler_states[WINED3DSAMP_BORDERCOLOR] != gl_tex->states[WINED3DTEXSTA_BORDERCOLOR])
302     {
303         float col[4];
304
305         state = sampler_states[WINED3DSAMP_BORDERCOLOR];
306         D3DCOLORTOGLFLOAT4(state, col);
307         TRACE("Setting border color for %#x to %#x.\n", target, state);
308         glTexParameterfv(target, GL_TEXTURE_BORDER_COLOR, &col[0]);
309         checkGLcall("glTexParameterfv(..., GL_TEXTURE_BORDER_COLOR, ...)");
310         gl_tex->states[WINED3DTEXSTA_BORDERCOLOR] = state;
311     }
312
313     if (sampler_states[WINED3DSAMP_MAGFILTER] != gl_tex->states[WINED3DTEXSTA_MAGFILTER])
314     {
315         GLint gl_value;
316
317         state = sampler_states[WINED3DSAMP_MAGFILTER];
318         if (state > WINED3DTEXF_ANISOTROPIC)
319             FIXME("Unrecognized or unsupported MAGFILTER* value %d.\n", state);
320
321         gl_value = wined3d_gl_mag_filter(texture->mag_lookup,
322                 min(max(state, WINED3DTEXF_POINT), WINED3DTEXF_LINEAR));
323         TRACE("ValueMAG=%#x setting MAGFILTER to %#x.\n", state, gl_value);
324         glTexParameteri(target, GL_TEXTURE_MAG_FILTER, gl_value);
325
326         gl_tex->states[WINED3DTEXSTA_MAGFILTER] = state;
327     }
328
329     if ((sampler_states[WINED3DSAMP_MINFILTER] != gl_tex->states[WINED3DTEXSTA_MINFILTER]
330             || sampler_states[WINED3DSAMP_MIPFILTER] != gl_tex->states[WINED3DTEXSTA_MIPFILTER]
331             || sampler_states[WINED3DSAMP_MAXMIPLEVEL] != gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL]))
332     {
333         GLint gl_value;
334
335         gl_tex->states[WINED3DTEXSTA_MIPFILTER] = sampler_states[WINED3DSAMP_MIPFILTER];
336         gl_tex->states[WINED3DTEXSTA_MINFILTER] = sampler_states[WINED3DSAMP_MINFILTER];
337         gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL] = sampler_states[WINED3DSAMP_MAXMIPLEVEL];
338
339         if (gl_tex->states[WINED3DTEXSTA_MINFILTER] > WINED3DTEXF_ANISOTROPIC
340             || gl_tex->states[WINED3DTEXSTA_MIPFILTER] > WINED3DTEXF_ANISOTROPIC)
341         {
342             FIXME("Unrecognized or unsupported D3DSAMP_MINFILTER value %#x D3DSAMP_MIPFILTER value %#x.\n",
343                   gl_tex->states[WINED3DTEXSTA_MINFILTER],
344                   gl_tex->states[WINED3DTEXSTA_MIPFILTER]);
345         }
346         gl_value = wined3d_gl_min_mip_filter(texture->min_mip_lookup,
347                 min(max(sampler_states[WINED3DSAMP_MINFILTER], WINED3DTEXF_POINT), WINED3DTEXF_LINEAR),
348                 min(max(sampler_states[WINED3DSAMP_MIPFILTER], WINED3DTEXF_NONE), WINED3DTEXF_LINEAR));
349
350         TRACE("ValueMIN=%#x, ValueMIP=%#x, setting MINFILTER to %#x.\n",
351               sampler_states[WINED3DSAMP_MINFILTER],
352               sampler_states[WINED3DSAMP_MIPFILTER], gl_value);
353         glTexParameteri(target, GL_TEXTURE_MIN_FILTER, gl_value);
354         checkGLcall("glTexParameter GL_TEXTURE_MIN_FILTER, ...");
355
356         if (!cond_np2)
357         {
358             if (gl_tex->states[WINED3DTEXSTA_MIPFILTER] == WINED3DTEXF_NONE)
359                 gl_value = texture->lod;
360             else if (gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL] >= texture->level_count)
361                 gl_value = texture->level_count - 1;
362             else if (gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL] < texture->lod)
363                 /* texture->lod is already clamped in the setter. */
364                 gl_value = texture->lod;
365             else
366                 gl_value = gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL];
367
368             /* Note that WINED3DSAMP_MAXMIPLEVEL specifies the largest mipmap
369              * (default 0), while GL_TEXTURE_MAX_LEVEL specifies the smallest
370              * mimap used (default 1000). So WINED3DSAMP_MAXMIPLEVEL
371              * corresponds to GL_TEXTURE_BASE_LEVEL. */
372             glTexParameteri(target, GL_TEXTURE_BASE_LEVEL, gl_value);
373         }
374     }
375
376     if ((gl_tex->states[WINED3DTEXSTA_MAGFILTER] != WINED3DTEXF_ANISOTROPIC
377          && gl_tex->states[WINED3DTEXSTA_MINFILTER] != WINED3DTEXF_ANISOTROPIC
378          && gl_tex->states[WINED3DTEXSTA_MIPFILTER] != WINED3DTEXF_ANISOTROPIC)
379             || cond_np2)
380         aniso = 1;
381     else
382         aniso = sampler_states[WINED3DSAMP_MAXANISOTROPY];
383
384     if (gl_tex->states[WINED3DTEXSTA_MAXANISOTROPY] != aniso)
385     {
386         if (gl_info->supported[EXT_TEXTURE_FILTER_ANISOTROPIC])
387         {
388             glTexParameteri(target, GL_TEXTURE_MAX_ANISOTROPY_EXT, aniso);
389             checkGLcall("glTexParameteri(GL_TEXTURE_MAX_ANISOTROPY_EXT, aniso)");
390         }
391         else
392         {
393             WARN("Anisotropic filtering not supported.\n");
394         }
395         gl_tex->states[WINED3DTEXSTA_MAXANISOTROPY] = aniso;
396     }
397
398     /* These should always be the same unless EXT_texture_sRGB_decode is supported. */
399     if (sampler_states[WINED3DSAMP_SRGBTEXTURE] != gl_tex->states[WINED3DTEXSTA_SRGBTEXTURE])
400     {
401         glTexParameteri(target, GL_TEXTURE_SRGB_DECODE_EXT,
402                 sampler_states[WINED3DSAMP_SRGBTEXTURE] ? GL_DECODE_EXT : GL_SKIP_DECODE_EXT);
403         checkGLcall("glTexParameteri(GL_TEXTURE_SRGB_DECODE_EXT)");
404     }
405
406     if (!(texture->resource.format->flags & WINED3DFMT_FLAG_SHADOW)
407             != !gl_tex->states[WINED3DTEXSTA_SHADOW])
408     {
409         if (texture->resource.format->flags & WINED3DFMT_FLAG_SHADOW)
410         {
411             glTexParameteri(target, GL_DEPTH_TEXTURE_MODE_ARB, GL_LUMINANCE);
412             glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE_ARB);
413             checkGLcall("glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE_ARB)");
414             gl_tex->states[WINED3DTEXSTA_SHADOW] = TRUE;
415         }
416         else
417         {
418             glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_NONE);
419             checkGLcall("glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_NONE)");
420             gl_tex->states[WINED3DTEXSTA_SHADOW] = FALSE;
421         }
422     }
423 }
424
425 ULONG CDECL wined3d_texture_incref(struct wined3d_texture *texture)
426 {
427     ULONG refcount = InterlockedIncrement(&texture->resource.ref);
428
429     TRACE("%p increasing refcount to %u.\n", texture, refcount);
430
431     return refcount;
432 }
433
434 /* Do not call while under the GL lock. */
435 ULONG CDECL wined3d_texture_decref(struct wined3d_texture *texture)
436 {
437     ULONG refcount = InterlockedDecrement(&texture->resource.ref);
438
439     TRACE("%p decreasing refcount to %u.\n", texture, refcount);
440
441     if (!refcount)
442     {
443         wined3d_texture_cleanup(texture);
444         texture->resource.parent_ops->wined3d_object_destroyed(texture->resource.parent);
445         HeapFree(GetProcessHeap(), 0, texture);
446     }
447
448     return refcount;
449 }
450
451 struct wined3d_resource * CDECL wined3d_texture_get_resource(struct wined3d_texture *texture)
452 {
453     TRACE("texture %p.\n", texture);
454
455     return &texture->resource;
456 }
457
458 DWORD CDECL wined3d_texture_set_priority(struct wined3d_texture *texture, DWORD priority)
459 {
460     return resource_set_priority(&texture->resource, priority);
461 }
462
463 DWORD CDECL wined3d_texture_get_priority(const struct wined3d_texture *texture)
464 {
465     return resource_get_priority(&texture->resource);
466 }
467
468 /* Do not call while under the GL lock. */
469 void CDECL wined3d_texture_preload(struct wined3d_texture *texture)
470 {
471     texture->texture_ops->texture_preload(texture, SRGB_ANY);
472 }
473
474 void * CDECL wined3d_texture_get_parent(const struct wined3d_texture *texture)
475 {
476     TRACE("texture %p.\n", texture);
477
478     return texture->resource.parent;
479 }
480
481 DWORD CDECL wined3d_texture_set_lod(struct wined3d_texture *texture, DWORD lod)
482 {
483     DWORD old = texture->lod;
484
485     TRACE("texture %p, lod %u.\n", texture, lod);
486
487     /* The d3d9:texture test shows that SetLOD is ignored on non-managed
488      * textures. The call always returns 0, and GetLOD always returns 0. */
489     if (texture->resource.pool != WINED3DPOOL_MANAGED)
490     {
491         TRACE("Ignoring SetLOD on %s texture, returning 0.\n", debug_d3dpool(texture->resource.pool));
492         return 0;
493     }
494
495     if (lod >= texture->level_count)
496         lod = texture->level_count - 1;
497
498     if (texture->lod != lod)
499     {
500         texture->lod = lod;
501
502         texture->texture_rgb.states[WINED3DTEXSTA_MAXMIPLEVEL] = ~0U;
503         texture->texture_srgb.states[WINED3DTEXSTA_MAXMIPLEVEL] = ~0U;
504         if (texture->bind_count)
505             device_invalidate_state(texture->resource.device, STATE_SAMPLER(texture->sampler));
506     }
507
508     return old;
509 }
510
511 DWORD CDECL wined3d_texture_get_lod(const struct wined3d_texture *texture)
512 {
513     TRACE("texture %p, returning %u.\n", texture, texture->lod);
514
515     return texture->lod;
516 }
517
518 DWORD CDECL wined3d_texture_get_level_count(const struct wined3d_texture *texture)
519 {
520     TRACE("texture %p, returning %u.\n", texture, texture->level_count);
521
522     return texture->level_count;
523 }
524
525 HRESULT CDECL wined3d_texture_set_autogen_filter_type(struct wined3d_texture *texture,
526         WINED3DTEXTUREFILTERTYPE filter_type)
527 {
528     FIXME("texture %p, filter_type %s stub!\n", texture, debug_d3dtexturefiltertype(filter_type));
529
530     if (!(texture->resource.usage & WINED3DUSAGE_AUTOGENMIPMAP))
531     {
532         WARN("Texture doesn't have AUTOGENMIPMAP usage.\n");
533         return WINED3DERR_INVALIDCALL;
534     }
535
536     texture->filter_type = filter_type;
537
538     return WINED3D_OK;
539 }
540
541 WINED3DTEXTUREFILTERTYPE CDECL wined3d_texture_get_autogen_filter_type(const struct wined3d_texture *texture)
542 {
543     TRACE("texture %p.\n", texture);
544
545     return texture->filter_type;
546 }
547
548 void CDECL wined3d_texture_generate_mipmaps(struct wined3d_texture *texture)
549 {
550     /* TODO: Implement filters using GL_SGI_generate_mipmaps. */
551     FIXME("texture %p stub!\n", texture);
552 }
553
554 struct wined3d_resource * CDECL wined3d_texture_get_sub_resource(struct wined3d_texture *texture,
555         UINT sub_resource_idx)
556 {
557     UINT sub_count = texture->level_count * texture->layer_count;
558
559     TRACE("texture %p, sub_resource_idx %u.\n", texture, sub_resource_idx);
560
561     if (sub_resource_idx >= sub_count)
562     {
563         WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx, sub_count);
564         return NULL;
565     }
566
567     return texture->sub_resources[sub_resource_idx];
568 }
569
570 HRESULT CDECL wined3d_texture_add_dirty_region(struct wined3d_texture *texture,
571         UINT layer, const WINED3DBOX *dirty_region)
572 {
573     struct wined3d_resource *sub_resource;
574
575     TRACE("texture %p, layer %u, dirty_region %p.\n", texture, layer, dirty_region);
576
577     if (!(sub_resource = wined3d_texture_get_sub_resource(texture, layer * texture->level_count)))
578     {
579         WARN("Failed to get sub-resource.\n");
580         return WINED3DERR_INVALIDCALL;
581     }
582
583     wined3d_texture_set_dirty(texture, TRUE);
584     texture->texture_ops->texture_sub_resource_add_dirty_region(sub_resource, dirty_region);
585
586     return WINED3D_OK;
587 }
588
589 /* Context activation is done by the caller. */
590 static HRESULT texture2d_bind(struct wined3d_texture *texture,
591         struct wined3d_context *context, BOOL srgb)
592 {
593     BOOL set_gl_texture_desc;
594     HRESULT hr;
595
596     TRACE("texture %p, context %p, srgb %#x.\n", texture, context, srgb);
597
598     hr = wined3d_texture_bind(texture, context, srgb, &set_gl_texture_desc);
599     if (set_gl_texture_desc && SUCCEEDED(hr))
600     {
601         UINT sub_count = texture->level_count * texture->layer_count;
602         BOOL srgb_tex = !context->gl_info->supported[EXT_TEXTURE_SRGB_DECODE]
603                 && (texture->flags & WINED3D_TEXTURE_IS_SRGB);
604         struct gl_texture *gl_tex;
605         UINT i;
606
607         gl_tex = wined3d_texture_get_gl_texture(texture, context->gl_info, srgb_tex);
608
609         for (i = 0; i < sub_count; ++i)
610         {
611             struct wined3d_surface *surface = surface_from_resource(texture->sub_resources[i]);
612             surface_set_texture_name(surface, gl_tex->name, srgb_tex);
613         }
614
615         /* Conditinal non power of two textures use a different clamping
616          * default. If we're using the GL_WINE_normalized_texrect partial
617          * driver emulation, we're dealing with a GL_TEXTURE_2D texture which
618          * has the address mode set to repeat - something that prevents us
619          * from hitting the accelerated codepath. Thus manually set the GL
620          * state. The same applies to filtering. Even if the texture has only
621          * one mip level, the default LINEAR_MIPMAP_LINEAR filter causes a SW
622          * fallback on macos. */
623         if (texture->flags & WINED3D_TEXTURE_COND_NP2)
624         {
625             GLenum target = texture->target;
626
627             ENTER_GL();
628             glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
629             checkGLcall("glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)");
630             glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
631             checkGLcall("glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)");
632             glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
633             checkGLcall("glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST)");
634             glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
635             checkGLcall("glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST)");
636             LEAVE_GL();
637             gl_tex->states[WINED3DTEXSTA_ADDRESSU]      = WINED3DTADDRESS_CLAMP;
638             gl_tex->states[WINED3DTEXSTA_ADDRESSV]      = WINED3DTADDRESS_CLAMP;
639             gl_tex->states[WINED3DTEXSTA_MAGFILTER]     = WINED3DTEXF_POINT;
640             gl_tex->states[WINED3DTEXSTA_MINFILTER]     = WINED3DTEXF_POINT;
641             gl_tex->states[WINED3DTEXSTA_MIPFILTER]     = WINED3DTEXF_NONE;
642         }
643     }
644
645     return hr;
646 }
647
648 static BOOL texture_srgb_mode(const struct wined3d_texture *texture, enum WINED3DSRGB srgb)
649 {
650     switch (srgb)
651     {
652         case SRGB_RGB:
653             return FALSE;
654
655         case SRGB_SRGB:
656             return TRUE;
657
658         default:
659             return texture->flags & WINED3D_TEXTURE_IS_SRGB;
660     }
661 }
662
663 /* Do not call while under the GL lock. */
664 static void texture2d_preload(struct wined3d_texture *texture, enum WINED3DSRGB srgb)
665 {
666     UINT sub_count = texture->level_count * texture->layer_count;
667     struct wined3d_device *device = texture->resource.device;
668     const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
669     struct wined3d_context *context = NULL;
670     struct gl_texture *gl_tex;
671     BOOL srgb_mode;
672     UINT i;
673
674     TRACE("texture %p, srgb %#x.\n", texture, srgb);
675
676     srgb_mode = texture_srgb_mode(texture, srgb);
677     gl_tex = wined3d_texture_get_gl_texture(texture, gl_info, srgb_mode);
678
679     if (!device->isInDraw)
680     {
681         /* No danger of recursive calls, context_acquire() sets isInDraw to TRUE
682          * when loading offscreen render targets into the texture. */
683         context = context_acquire(device, NULL);
684     }
685
686     if (texture->resource.format->id == WINED3DFMT_P8_UINT
687             || texture->resource.format->id == WINED3DFMT_P8_UINT_A8_UNORM)
688     {
689         for (i = 0; i < sub_count; ++i)
690         {
691             struct wined3d_surface *surface = surface_from_resource(texture->sub_resources[i]);
692
693             if (palette9_changed(surface))
694             {
695                 TRACE("Reloading surface %p because the d3d8/9 palette was changed.\n", surface);
696                 /* TODO: This is not necessarily needed with hw palettized texture support. */
697                 surface_load_location(surface, SFLAG_INSYSMEM, NULL);
698                 /* Make sure the texture is reloaded because of the palette
699                  * change, this kills performance though :( */
700                 surface_modify_location(surface, SFLAG_INTEXTURE, FALSE);
701             }
702         }
703     }
704
705     if (gl_tex->dirty)
706     {
707         /* Reload the surfaces if the texture is marked dirty. */
708         for (i = 0; i < sub_count; ++i)
709         {
710             surface_load(surface_from_resource(texture->sub_resources[i]), srgb_mode);
711         }
712     }
713     else
714     {
715         TRACE("Texture %p not dirty, nothing to do.\n", texture);
716     }
717
718     /* No longer dirty. */
719     gl_tex->dirty = FALSE;
720
721     if (context) context_release(context);
722 }
723
724 static void texture2d_sub_resource_add_dirty_region(struct wined3d_resource *sub_resource,
725         const WINED3DBOX *dirty_region)
726 {
727     surface_add_dirty_rect(surface_from_resource(sub_resource), dirty_region);
728 }
729
730 static void texture2d_sub_resource_cleanup(struct wined3d_resource *sub_resource)
731 {
732     struct wined3d_surface *surface = surface_from_resource(sub_resource);
733
734     /* Clean out the texture name we gave to the surface so that the
735      * surface doesn't try and release it. */
736     surface_set_texture_name(surface, 0, TRUE);
737     surface_set_texture_name(surface, 0, FALSE);
738     surface_set_texture_target(surface, 0);
739     surface_set_container(surface, WINED3D_CONTAINER_NONE, NULL);
740     wined3d_surface_decref(surface);
741 }
742
743 /* Do not call while under the GL lock. */
744 static void texture2d_unload(struct wined3d_resource *resource)
745 {
746     struct wined3d_texture *texture = wined3d_texture_from_resource(resource);
747     UINT sub_count = texture->level_count * texture->layer_count;
748     UINT i;
749
750     TRACE("texture %p.\n", texture);
751
752     for (i = 0; i < sub_count; ++i)
753     {
754         struct wined3d_resource *sub_resource = texture->sub_resources[i];
755         struct wined3d_surface *surface = surface_from_resource(sub_resource);
756
757         sub_resource->resource_ops->resource_unload(sub_resource);
758         surface_set_texture_name(surface, 0, FALSE); /* Delete RGB name */
759         surface_set_texture_name(surface, 0, TRUE); /* Delete sRGB name */
760     }
761
762     wined3d_texture_unload(texture);
763 }
764
765 static const struct wined3d_texture_ops texture2d_ops =
766 {
767     texture2d_bind,
768     texture2d_preload,
769     texture2d_sub_resource_add_dirty_region,
770     texture2d_sub_resource_cleanup,
771 };
772
773 static const struct wined3d_resource_ops texture2d_resource_ops =
774 {
775     texture2d_unload,
776 };
777
778 static HRESULT cubetexture_init(struct wined3d_texture *texture, UINT edge_length, UINT levels,
779         struct wined3d_device *device, DWORD usage, enum wined3d_format_id format_id, WINED3DPOOL pool,
780         void *parent, const struct wined3d_parent_ops *parent_ops)
781 {
782     const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
783     const struct wined3d_format *format = wined3d_get_format(gl_info, format_id);
784     UINT pow2_edge_length;
785     unsigned int i, j;
786     UINT tmp_w;
787     HRESULT hr;
788
789     /* TODO: It should only be possible to create textures for formats
790      * that are reported as supported. */
791     if (WINED3DFMT_UNKNOWN >= format_id)
792     {
793         WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture);
794         return WINED3DERR_INVALIDCALL;
795     }
796
797     if (!gl_info->supported[ARB_TEXTURE_CUBE_MAP] && pool != WINED3DPOOL_SCRATCH)
798     {
799         WARN("(%p) : Tried to create not supported cube texture.\n", texture);
800         return WINED3DERR_INVALIDCALL;
801     }
802
803     /* Calculate levels for mip mapping */
804     if (usage & WINED3DUSAGE_AUTOGENMIPMAP)
805     {
806         if (!gl_info->supported[SGIS_GENERATE_MIPMAP])
807         {
808             WARN("No mipmap generation support, returning D3DERR_INVALIDCALL.\n");
809             return WINED3DERR_INVALIDCALL;
810         }
811
812         if (levels > 1)
813         {
814             WARN("D3DUSAGE_AUTOGENMIPMAP is set, and level count > 1, returning D3DERR_INVALIDCALL.\n");
815             return WINED3DERR_INVALIDCALL;
816         }
817
818         levels = 1;
819     }
820     else if (!levels)
821     {
822         levels = wined3d_log2i(edge_length) + 1;
823         TRACE("Calculated levels = %u.\n", levels);
824     }
825
826     hr = wined3d_texture_init(texture, &texture2d_ops, 6, levels,
827             WINED3DRTYPE_CUBETEXTURE, device, usage, format, pool,
828             parent, parent_ops, &texture2d_resource_ops);
829     if (FAILED(hr))
830     {
831         WARN("Failed to initialize texture, returning %#x\n", hr);
832         return hr;
833     }
834
835     /* Find the nearest pow2 match. */
836     pow2_edge_length = 1;
837     while (pow2_edge_length < edge_length) pow2_edge_length <<= 1;
838
839     if (gl_info->supported[ARB_TEXTURE_NON_POWER_OF_TWO] || (edge_length == pow2_edge_length))
840     {
841         /* Precalculated scaling for 'faked' non power of two texture coords. */
842         texture->pow2_matrix[0] = 1.0f;
843         texture->pow2_matrix[5] = 1.0f;
844         texture->pow2_matrix[10] = 1.0f;
845         texture->pow2_matrix[15] = 1.0f;
846     }
847     else
848     {
849         /* Precalculated scaling for 'faked' non power of two texture coords. */
850         texture->pow2_matrix[0] = ((float)edge_length) / ((float)pow2_edge_length);
851         texture->pow2_matrix[5] = ((float)edge_length) / ((float)pow2_edge_length);
852         texture->pow2_matrix[10] = ((float)edge_length) / ((float)pow2_edge_length);
853         texture->pow2_matrix[15] = 1.0f;
854         texture->flags &= ~WINED3D_TEXTURE_POW2_MAT_IDENT;
855     }
856     texture->target = GL_TEXTURE_CUBE_MAP_ARB;
857
858     /* Generate all the surfaces. */
859     tmp_w = edge_length;
860     for (i = 0; i < texture->level_count; ++i)
861     {
862         /* Create the 6 faces. */
863         for (j = 0; j < 6; ++j)
864         {
865             static const GLenum cube_targets[6] =
866             {
867                 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB,
868                 GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB,
869                 GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB,
870                 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB,
871                 GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB,
872                 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB,
873             };
874             UINT idx = j * texture->level_count + i;
875             struct wined3d_surface *surface;
876
877             hr = device->device_parent->ops->create_surface(device->device_parent, parent, tmp_w, tmp_w,
878                     format_id, usage, pool, i /* Level */, j, &surface);
879             if (FAILED(hr))
880             {
881                 FIXME("(%p) Failed to create surface, hr %#x.\n", texture, hr);
882                 wined3d_texture_cleanup(texture);
883                 return hr;
884             }
885
886             surface_set_container(surface, WINED3D_CONTAINER_TEXTURE, texture);
887             surface_set_texture_target(surface, cube_targets[j]);
888             texture->sub_resources[idx] = &surface->resource;
889             TRACE("Created surface level %u @ %p.\n", i, surface);
890         }
891         tmp_w = max(1, tmp_w >> 1);
892     }
893
894     return WINED3D_OK;
895 }
896
897 static HRESULT texture_init(struct wined3d_texture *texture, UINT width, UINT height, UINT levels,
898         struct wined3d_device *device, DWORD usage, enum wined3d_format_id format_id, WINED3DPOOL pool,
899         void *parent, const struct wined3d_parent_ops *parent_ops)
900 {
901     const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
902     const struct wined3d_format *format = wined3d_get_format(gl_info, format_id);
903     UINT pow2_width, pow2_height;
904     UINT tmp_w, tmp_h;
905     unsigned int i;
906     HRESULT hr;
907
908     /* TODO: It should only be possible to create textures for formats
909      * that are reported as supported. */
910     if (WINED3DFMT_UNKNOWN >= format_id)
911     {
912         WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture);
913         return WINED3DERR_INVALIDCALL;
914     }
915
916     /* Non-power2 support. */
917     if (gl_info->supported[ARB_TEXTURE_NON_POWER_OF_TWO])
918     {
919         pow2_width = width;
920         pow2_height = height;
921     }
922     else
923     {
924         /* Find the nearest pow2 match. */
925         pow2_width = pow2_height = 1;
926         while (pow2_width < width) pow2_width <<= 1;
927         while (pow2_height < height) pow2_height <<= 1;
928
929         if (pow2_width != width || pow2_height != height)
930         {
931             if (levels > 1)
932             {
933                 WARN("Attempted to create a mipmapped np2 texture without unconditional np2 support.\n");
934                 return WINED3DERR_INVALIDCALL;
935             }
936             levels = 1;
937         }
938     }
939
940     /* Calculate levels for mip mapping. */
941     if (usage & WINED3DUSAGE_AUTOGENMIPMAP)
942     {
943         if (!gl_info->supported[SGIS_GENERATE_MIPMAP])
944         {
945             WARN("No mipmap generation support, returning WINED3DERR_INVALIDCALL.\n");
946             return WINED3DERR_INVALIDCALL;
947         }
948
949         if (levels > 1)
950         {
951             WARN("D3DUSAGE_AUTOGENMIPMAP is set, and level count > 1, returning WINED3DERR_INVALIDCALL.\n");
952             return WINED3DERR_INVALIDCALL;
953         }
954
955         levels = 1;
956     }
957     else if (!levels)
958     {
959         levels = wined3d_log2i(max(width, height)) + 1;
960         TRACE("Calculated levels = %u.\n", levels);
961     }
962
963     hr = wined3d_texture_init(texture, &texture2d_ops, 1, levels,
964             WINED3DRTYPE_TEXTURE, device, usage, format, pool,
965             parent, parent_ops, &texture2d_resource_ops);
966     if (FAILED(hr))
967     {
968         WARN("Failed to initialize texture, returning %#x.\n", hr);
969         return hr;
970     }
971
972     /* Precalculated scaling for 'faked' non power of two texture coords.
973      * Second also don't use ARB_TEXTURE_RECTANGLE in case the surface format is P8 and EXT_PALETTED_TEXTURE
974      * is used in combination with texture uploads (RTL_READTEX). The reason is that EXT_PALETTED_TEXTURE
975      * doesn't work in combination with ARB_TEXTURE_RECTANGLE. */
976     if (gl_info->supported[WINED3D_GL_NORMALIZED_TEXRECT] && (width != pow2_width || height != pow2_height))
977     {
978         texture->pow2_matrix[0] = 1.0f;
979         texture->pow2_matrix[5] = 1.0f;
980         texture->pow2_matrix[10] = 1.0f;
981         texture->pow2_matrix[15] = 1.0f;
982         texture->target = GL_TEXTURE_2D;
983         texture->flags |= WINED3D_TEXTURE_COND_NP2;
984         texture->min_mip_lookup = minMipLookup_noFilter;
985     }
986     else if (gl_info->supported[ARB_TEXTURE_RECTANGLE] && (width != pow2_width || height != pow2_height)
987             && !(format->id == WINED3DFMT_P8_UINT && gl_info->supported[EXT_PALETTED_TEXTURE]
988             && wined3d_settings.rendertargetlock_mode == RTL_READTEX))
989     {
990         if (width != 1 || height != 1)
991             texture->flags &= ~WINED3D_TEXTURE_POW2_MAT_IDENT;
992
993         texture->pow2_matrix[0] = (float)width;
994         texture->pow2_matrix[5] = (float)height;
995         texture->pow2_matrix[10] = 1.0f;
996         texture->pow2_matrix[15] = 1.0f;
997         texture->target = GL_TEXTURE_RECTANGLE_ARB;
998         texture->flags |= WINED3D_TEXTURE_COND_NP2;
999
1000         if (texture->resource.format->flags & WINED3DFMT_FLAG_FILTERING)
1001             texture->min_mip_lookup = minMipLookup_noMip;
1002         else
1003             texture->min_mip_lookup = minMipLookup_noFilter;
1004     }
1005     else
1006     {
1007         if ((width != pow2_width) || (height != pow2_height))
1008         {
1009             texture->pow2_matrix[0] = (((float)width) / ((float)pow2_width));
1010             texture->pow2_matrix[5] = (((float)height) / ((float)pow2_height));
1011             texture->flags &= ~WINED3D_TEXTURE_POW2_MAT_IDENT;
1012         }
1013         else
1014         {
1015             texture->pow2_matrix[0] = 1.0f;
1016             texture->pow2_matrix[5] = 1.0f;
1017         }
1018
1019         texture->pow2_matrix[10] = 1.0f;
1020         texture->pow2_matrix[15] = 1.0f;
1021         texture->target = GL_TEXTURE_2D;
1022     }
1023     TRACE("xf(%f) yf(%f)\n", texture->pow2_matrix[0], texture->pow2_matrix[5]);
1024
1025     /* Generate all the surfaces. */
1026     tmp_w = width;
1027     tmp_h = height;
1028     for (i = 0; i < texture->level_count; ++i)
1029     {
1030         struct wined3d_surface *surface;
1031
1032         /* Use the callback to create the texture surface. */
1033         hr = device->device_parent->ops->create_surface(device->device_parent, parent, tmp_w, tmp_h,
1034                 format->id, usage, pool, i, 0, &surface);
1035         if (FAILED(hr))
1036         {
1037             FIXME("Failed to create surface %p, hr %#x\n", texture, hr);
1038             wined3d_texture_cleanup(texture);
1039             return hr;
1040         }
1041
1042         surface_set_container(surface, WINED3D_CONTAINER_TEXTURE, texture);
1043         surface_set_texture_target(surface, texture->target);
1044         texture->sub_resources[i] = &surface->resource;
1045         TRACE("Created surface level %u @ %p.\n", i, surface);
1046         /* Calculate the next mipmap level. */
1047         tmp_w = max(1, tmp_w >> 1);
1048         tmp_h = max(1, tmp_h >> 1);
1049     }
1050
1051     return WINED3D_OK;
1052 }
1053
1054 /* Context activation is done by the caller. */
1055 static HRESULT texture3d_bind(struct wined3d_texture *texture,
1056         struct wined3d_context *context, BOOL srgb)
1057 {
1058     BOOL dummy;
1059
1060     TRACE("texture %p, context %p, srgb %#x.\n", texture, context, srgb);
1061
1062     return wined3d_texture_bind(texture, context, srgb, &dummy);
1063 }
1064
1065 /* Do not call while under the GL lock. */
1066 static void texture3d_preload(struct wined3d_texture *texture, enum WINED3DSRGB srgb)
1067 {
1068     struct wined3d_device *device = texture->resource.device;
1069     struct wined3d_context *context;
1070     BOOL srgb_was_toggled = FALSE;
1071     unsigned int i;
1072
1073     TRACE("texture %p, srgb %#x.\n", texture, srgb);
1074
1075     /* TODO: Use already acquired context when possible. */
1076     context = context_acquire(device, NULL);
1077     if (texture->bind_count > 0)
1078     {
1079         BOOL texture_srgb = texture->flags & WINED3D_TEXTURE_IS_SRGB;
1080         BOOL sampler_srgb = texture_srgb_mode(texture, srgb);
1081         srgb_was_toggled = !texture_srgb != !sampler_srgb;
1082
1083         if (srgb_was_toggled)
1084         {
1085             if (sampler_srgb)
1086                 texture->flags |= WINED3D_TEXTURE_IS_SRGB;
1087             else
1088                 texture->flags &= ~WINED3D_TEXTURE_IS_SRGB;
1089         }
1090     }
1091
1092     /* If the texture is marked dirty or the sRGB sampler setting has changed
1093      * since the last load then reload the volumes. */
1094     if (texture->texture_rgb.dirty)
1095     {
1096         for (i = 0; i < texture->level_count; ++i)
1097         {
1098             volume_load(volume_from_resource(texture->sub_resources[i]), context, i,
1099                     texture->flags & WINED3D_TEXTURE_IS_SRGB);
1100         }
1101     }
1102     else if (srgb_was_toggled)
1103     {
1104         for (i = 0; i < texture->level_count; ++i)
1105         {
1106             struct wined3d_volume *volume = volume_from_resource(texture->sub_resources[i]);
1107             volume_add_dirty_box(volume, NULL);
1108             volume_load(volume, context, i, texture->flags & WINED3D_TEXTURE_IS_SRGB);
1109         }
1110     }
1111     else
1112     {
1113         TRACE("Texture %p not dirty, nothing to do.\n", texture);
1114     }
1115
1116     context_release(context);
1117
1118     /* No longer dirty */
1119     texture->texture_rgb.dirty = FALSE;
1120 }
1121
1122 static void texture3d_sub_resource_add_dirty_region(struct wined3d_resource *sub_resource,
1123         const WINED3DBOX *dirty_region)
1124 {
1125     volume_add_dirty_box(volume_from_resource(sub_resource), dirty_region);
1126 }
1127
1128 static void texture3d_sub_resource_cleanup(struct wined3d_resource *sub_resource)
1129 {
1130     struct wined3d_volume *volume = volume_from_resource(sub_resource);
1131
1132     /* Cleanup the container. */
1133     volume_set_container(volume, NULL);
1134     wined3d_volume_decref(volume);
1135 }
1136
1137 /* Do not call while under the GL lock. */
1138 static void texture3d_unload(struct wined3d_resource *resource)
1139 {
1140     struct wined3d_texture *texture = wined3d_texture_from_resource(resource);
1141     UINT i;
1142
1143     TRACE("texture %p.\n", texture);
1144
1145     for (i = 0; i < texture->level_count; ++i)
1146     {
1147         struct wined3d_resource *sub_resource = texture->sub_resources[i];
1148         sub_resource->resource_ops->resource_unload(sub_resource);
1149     }
1150
1151     wined3d_texture_unload(texture);
1152 }
1153
1154 static const struct wined3d_texture_ops texture3d_ops =
1155 {
1156     texture3d_bind,
1157     texture3d_preload,
1158     texture3d_sub_resource_add_dirty_region,
1159     texture3d_sub_resource_cleanup,
1160 };
1161
1162 static const struct wined3d_resource_ops texture3d_resource_ops =
1163 {
1164     texture3d_unload,
1165 };
1166
1167 static HRESULT volumetexture_init(struct wined3d_texture *texture, UINT width, UINT height,
1168         UINT depth, UINT levels, struct wined3d_device *device, DWORD usage, enum wined3d_format_id format_id,
1169         WINED3DPOOL pool, void *parent, const struct wined3d_parent_ops *parent_ops)
1170 {
1171     const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
1172     const struct wined3d_format *format = wined3d_get_format(gl_info, format_id);
1173     UINT tmp_w, tmp_h, tmp_d;
1174     unsigned int i;
1175     HRESULT hr;
1176
1177     /* TODO: It should only be possible to create textures for formats
1178      * that are reported as supported. */
1179     if (WINED3DFMT_UNKNOWN >= format_id)
1180     {
1181         WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture);
1182         return WINED3DERR_INVALIDCALL;
1183     }
1184
1185     if (!gl_info->supported[EXT_TEXTURE3D])
1186     {
1187         WARN("(%p) : Texture cannot be created - no volume texture support.\n", texture);
1188         return WINED3DERR_INVALIDCALL;
1189     }
1190
1191     /* Calculate levels for mip mapping. */
1192     if (usage & WINED3DUSAGE_AUTOGENMIPMAP)
1193     {
1194         if (!gl_info->supported[SGIS_GENERATE_MIPMAP])
1195         {
1196             WARN("No mipmap generation support, returning D3DERR_INVALIDCALL.\n");
1197             return WINED3DERR_INVALIDCALL;
1198         }
1199
1200         if (levels > 1)
1201         {
1202             WARN("D3DUSAGE_AUTOGENMIPMAP is set, and level count > 1, returning D3DERR_INVALIDCALL.\n");
1203             return WINED3DERR_INVALIDCALL;
1204         }
1205
1206         levels = 1;
1207     }
1208     else if (!levels)
1209     {
1210         levels = wined3d_log2i(max(max(width, height), depth)) + 1;
1211         TRACE("Calculated levels = %u.\n", levels);
1212     }
1213
1214     hr = wined3d_texture_init(texture, &texture3d_ops, 1, levels,
1215             WINED3DRTYPE_VOLUMETEXTURE, device, usage, format, pool,
1216             parent, parent_ops, &texture3d_resource_ops);
1217     if (FAILED(hr))
1218     {
1219         WARN("Failed to initialize texture, returning %#x.\n", hr);
1220         return hr;
1221     }
1222
1223     /* Is NP2 support for volumes needed? */
1224     texture->pow2_matrix[0] = 1.0f;
1225     texture->pow2_matrix[5] = 1.0f;
1226     texture->pow2_matrix[10] = 1.0f;
1227     texture->pow2_matrix[15] = 1.0f;
1228     texture->target = GL_TEXTURE_3D;
1229
1230     /* Generate all the surfaces. */
1231     tmp_w = width;
1232     tmp_h = height;
1233     tmp_d = depth;
1234
1235     for (i = 0; i < texture->level_count; ++i)
1236     {
1237         struct wined3d_volume *volume;
1238
1239         /* Create the volume. */
1240         hr = device->device_parent->ops->create_volume(device->device_parent, parent,
1241                 tmp_w, tmp_h, tmp_d, format_id, pool, usage, &volume);
1242         if (FAILED(hr))
1243         {
1244             ERR("Creating a volume for the volume texture failed, hr %#x.\n", hr);
1245             wined3d_texture_cleanup(texture);
1246             return hr;
1247         }
1248
1249         /* Set its container to this texture. */
1250         volume_set_container(volume, texture);
1251         texture->sub_resources[i] = &volume->resource;
1252
1253         /* Calculate the next mipmap level. */
1254         tmp_w = max(1, tmp_w >> 1);
1255         tmp_h = max(1, tmp_h >> 1);
1256         tmp_d = max(1, tmp_d >> 1);
1257     }
1258
1259     return WINED3D_OK;
1260 }
1261
1262 HRESULT CDECL wined3d_texture_create_2d(struct wined3d_device *device, UINT width, UINT height,
1263         UINT level_count, DWORD usage, enum wined3d_format_id format_id, WINED3DPOOL pool, void *parent,
1264         const struct wined3d_parent_ops *parent_ops, struct wined3d_texture **texture)
1265 {
1266     struct wined3d_texture *object;
1267     HRESULT hr;
1268
1269     TRACE("device %p, width %u, height %u, level_count %u, usage %#x\n",
1270             device, width, height, level_count, usage);
1271     TRACE("format %s, pool %#x, parent %p, parent_ops %p, texture %p.\n",
1272             debug_d3dformat(format_id), pool, parent, parent_ops, texture);
1273
1274     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1275     if (!object)
1276     {
1277         ERR("Out of memory.\n");
1278         *texture = NULL;
1279         return WINED3DERR_OUTOFVIDEOMEMORY;
1280     }
1281
1282     hr = texture_init(object, width, height, level_count,
1283             device, usage, format_id, pool, parent, parent_ops);
1284     if (FAILED(hr))
1285     {
1286         WARN("Failed to initialize texture, returning %#x.\n", hr);
1287         HeapFree(GetProcessHeap(), 0, object);
1288         *texture = NULL;
1289         return hr;
1290     }
1291
1292     TRACE("Created texture %p.\n", object);
1293     *texture = object;
1294
1295     return WINED3D_OK;
1296 }
1297
1298 HRESULT CDECL wined3d_texture_create_3d(struct wined3d_device *device, UINT width, UINT height, UINT depth,
1299         UINT level_count, DWORD usage, enum wined3d_format_id format_id, WINED3DPOOL pool, void *parent,
1300         const struct wined3d_parent_ops *parent_ops, struct wined3d_texture **texture)
1301 {
1302     struct wined3d_texture *object;
1303     HRESULT hr;
1304
1305     TRACE("device %p, width %u, height %u, depth %u, level_count %u, usage %#x\n",
1306             device, width, height, depth, level_count, usage);
1307     TRACE("format %s, pool %#x, parent %p, parent_ops %p, texture %p.\n",
1308             debug_d3dformat(format_id), pool, parent, parent_ops, texture);
1309
1310     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1311     if (!object)
1312     {
1313         ERR("Out of memory\n");
1314         *texture = NULL;
1315         return WINED3DERR_OUTOFVIDEOMEMORY;
1316     }
1317
1318     hr = volumetexture_init(object, width, height, depth, level_count,
1319             device, usage, format_id, pool, parent, parent_ops);
1320     if (FAILED(hr))
1321     {
1322         WARN("Failed to initialize volumetexture, returning %#x\n", hr);
1323         HeapFree(GetProcessHeap(), 0, object);
1324         *texture = NULL;
1325         return hr;
1326     }
1327
1328     TRACE("Created texture %p.\n", object);
1329     *texture = object;
1330
1331     return WINED3D_OK;
1332 }
1333
1334 HRESULT CDECL wined3d_texture_create_cube(struct wined3d_device *device, UINT edge_length,
1335         UINT level_count, DWORD usage, enum wined3d_format_id format_id, WINED3DPOOL pool, void *parent,
1336         const struct wined3d_parent_ops *parent_ops, struct wined3d_texture **texture)
1337 {
1338     struct wined3d_texture *object;
1339     HRESULT hr;
1340
1341     TRACE("device %p, edge_length %u, level_count %u, usage %#x\n",
1342             device, edge_length, level_count, usage);
1343     TRACE("format %s, pool %#x, parent %p, parent_ops %p, texture %p.\n",
1344             debug_d3dformat(format_id), pool, parent, parent_ops, texture);
1345
1346     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1347     if (!object)
1348     {
1349         ERR("Out of memory\n");
1350         *texture = NULL;
1351         return WINED3DERR_OUTOFVIDEOMEMORY;
1352     }
1353
1354     hr = cubetexture_init(object, edge_length, level_count,
1355             device, usage, format_id, pool, parent, parent_ops);
1356     if (FAILED(hr))
1357     {
1358         WARN("Failed to initialize cubetexture, returning %#x\n", hr);
1359         HeapFree(GetProcessHeap(), 0, object);
1360         *texture = NULL;
1361         return hr;
1362     }
1363
1364     TRACE("Created texture %p.\n", object);
1365     *texture = object;
1366
1367     return WINED3D_OK;
1368 }