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