winecoreaudio: Fix a potential leak. (Clang).
[wine/wine-gecko.git] / dlls / wined3d / texture.c
blob69e7010948ecafd85b1c8c5f1dc9c88561a10a7a
1 /*
2 * Copyright 2002-2005 Jason Edmeades
3 * Copyright 2002-2005 Raphael Junqueira
4 * Copyright 2005 Oliver Stieber
5 * Copyright 2007-2009, 2013 Stefan Dösinger for CodeWeavers
6 * Copyright 2009-2011 Henri Verbeet for CodeWeavers
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.
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.
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
23 #include "config.h"
24 #include "wine/port.h"
25 #include "wined3d_private.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(d3d_texture);
29 static HRESULT wined3d_texture_init(struct wined3d_texture *texture, const struct wined3d_texture_ops *texture_ops,
30 UINT layer_count, UINT level_count, const struct wined3d_resource_desc *desc, struct wined3d_device *device,
31 void *parent, const struct wined3d_parent_ops *parent_ops, const struct wined3d_resource_ops *resource_ops)
33 const struct wined3d_format *format = wined3d_get_format(&device->adapter->gl_info, desc->format);
34 HRESULT hr;
36 TRACE("texture %p, texture_ops %p, layer_count %u, level_count %u, resource_type %s, format %s, "
37 "multisample_type %#x, multisample_quality %#x, usage %s, pool %s, width %u, height %u, depth %u, "
38 "device %p, parent %p, parent_ops %p, resource_ops %p.\n",
39 texture, texture_ops, layer_count, level_count, debug_d3dresourcetype(desc->resource_type),
40 debug_d3dformat(desc->format), desc->multisample_type, desc->multisample_quality,
41 debug_d3dusage(desc->usage), debug_d3dpool(desc->pool), desc->width, desc->height, desc->depth,
42 device, parent, parent_ops, resource_ops);
44 if ((format->flags & (WINED3DFMT_FLAG_BLOCKS | WINED3DFMT_FLAG_BLOCKS_NO_VERIFY)) == WINED3DFMT_FLAG_BLOCKS)
46 UINT width_mask = format->block_width - 1;
47 UINT height_mask = format->block_height - 1;
48 if (desc->width & width_mask || desc->height & height_mask)
49 return WINED3DERR_INVALIDCALL;
52 if (FAILED(hr = resource_init(&texture->resource, device, desc->resource_type, format,
53 desc->multisample_type, desc->multisample_quality, desc->usage, desc->pool,
54 desc->width, desc->height, desc->depth, 0, parent, parent_ops, resource_ops)))
56 WARN("Failed to initialize resource, returning %#x\n", hr);
57 return hr;
60 texture->texture_ops = texture_ops;
61 texture->sub_resources = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
62 level_count * layer_count * sizeof(*texture->sub_resources));
63 if (!texture->sub_resources)
65 ERR("Failed to allocate sub-resource array.\n");
66 resource_cleanup(&texture->resource);
67 return E_OUTOFMEMORY;
70 texture->layer_count = layer_count;
71 texture->level_count = level_count;
72 texture->filter_type = (desc->usage & WINED3DUSAGE_AUTOGENMIPMAP) ? WINED3D_TEXF_LINEAR : WINED3D_TEXF_NONE;
73 texture->lod = 0;
74 texture->flags = WINED3D_TEXTURE_POW2_MAT_IDENT;
76 if (texture->resource.format->flags & WINED3DFMT_FLAG_FILTERING)
78 texture->min_mip_lookup = minMipLookup;
79 texture->mag_lookup = magLookup;
81 else
83 texture->min_mip_lookup = minMipLookup_noFilter;
84 texture->mag_lookup = magLookup_noFilter;
87 return WINED3D_OK;
90 /* A GL context is provided by the caller */
91 static void gltexture_delete(const struct wined3d_gl_info *gl_info, struct gl_texture *tex)
93 gl_info->gl_ops.gl.p_glDeleteTextures(1, &tex->name);
94 tex->name = 0;
97 static void wined3d_texture_unload(struct wined3d_texture *texture)
99 struct wined3d_device *device = texture->resource.device;
100 struct wined3d_context *context = NULL;
102 if (texture->texture_rgb.name || texture->texture_srgb.name)
104 context = context_acquire(device, NULL);
107 if (texture->texture_rgb.name)
108 gltexture_delete(context->gl_info, &texture->texture_rgb);
110 if (texture->texture_srgb.name)
111 gltexture_delete(context->gl_info, &texture->texture_srgb);
113 if (context) context_release(context);
115 wined3d_texture_set_dirty(texture);
117 resource_unload(&texture->resource);
120 static void wined3d_texture_cleanup(struct wined3d_texture *texture)
122 UINT sub_count = texture->level_count * texture->layer_count;
123 UINT i;
125 TRACE("texture %p.\n", texture);
127 for (i = 0; i < sub_count; ++i)
129 struct wined3d_resource *sub_resource = texture->sub_resources[i];
131 if (sub_resource)
132 texture->texture_ops->texture_sub_resource_cleanup(sub_resource);
135 wined3d_texture_unload(texture);
136 HeapFree(GetProcessHeap(), 0, texture->sub_resources);
137 resource_cleanup(&texture->resource);
140 void wined3d_texture_set_dirty(struct wined3d_texture *texture)
142 texture->flags &= ~(WINED3D_TEXTURE_RGB_VALID | WINED3D_TEXTURE_SRGB_VALID);
145 /* Context activation is done by the caller. */
146 static HRESULT wined3d_texture_bind(struct wined3d_texture *texture,
147 struct wined3d_context *context, BOOL srgb, BOOL *set_surface_desc)
149 const struct wined3d_gl_info *gl_info = context->gl_info;
150 struct gl_texture *gl_tex;
151 BOOL new_texture = FALSE;
152 HRESULT hr = WINED3D_OK;
153 GLenum target;
155 TRACE("texture %p, context %p, srgb %#x, set_surface_desc %p.\n", texture, context, srgb, set_surface_desc);
157 if (gl_info->supported[EXT_TEXTURE_SRGB_DECODE])
158 srgb = FALSE;
160 /* sRGB mode cache for preload() calls outside drawprim. */
161 if (srgb)
162 texture->flags |= WINED3D_TEXTURE_IS_SRGB;
163 else
164 texture->flags &= ~WINED3D_TEXTURE_IS_SRGB;
166 gl_tex = wined3d_texture_get_gl_texture(texture, srgb);
167 target = texture->target;
169 /* Generate a texture name if we don't already have one. */
170 if (!gl_tex->name)
172 *set_surface_desc = TRUE;
173 gl_info->gl_ops.gl.p_glGenTextures(1, &gl_tex->name);
174 checkGLcall("glGenTextures");
175 TRACE("Generated texture %d.\n", gl_tex->name);
176 if (texture->resource.pool == WINED3D_POOL_DEFAULT)
178 /* Tell OpenGL to try and keep this texture in video ram (well mostly). */
179 GLclampf tmp = 0.9f;
180 gl_info->gl_ops.gl.p_glPrioritizeTextures(1, &gl_tex->name, &tmp);
182 /* Initialise the state of the texture object to the OpenGL defaults,
183 * not the D3D defaults. */
184 gl_tex->states[WINED3DTEXSTA_ADDRESSU] = WINED3D_TADDRESS_WRAP;
185 gl_tex->states[WINED3DTEXSTA_ADDRESSV] = WINED3D_TADDRESS_WRAP;
186 gl_tex->states[WINED3DTEXSTA_ADDRESSW] = WINED3D_TADDRESS_WRAP;
187 gl_tex->states[WINED3DTEXSTA_BORDERCOLOR] = 0;
188 gl_tex->states[WINED3DTEXSTA_MAGFILTER] = WINED3D_TEXF_LINEAR;
189 gl_tex->states[WINED3DTEXSTA_MINFILTER] = WINED3D_TEXF_POINT; /* GL_NEAREST_MIPMAP_LINEAR */
190 gl_tex->states[WINED3DTEXSTA_MIPFILTER] = WINED3D_TEXF_LINEAR; /* GL_NEAREST_MIPMAP_LINEAR */
191 gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL] = 0;
192 gl_tex->states[WINED3DTEXSTA_MAXANISOTROPY] = 1;
193 if (context->gl_info->supported[EXT_TEXTURE_SRGB_DECODE])
194 gl_tex->states[WINED3DTEXSTA_SRGBTEXTURE] = TRUE;
195 else
196 gl_tex->states[WINED3DTEXSTA_SRGBTEXTURE] = srgb;
197 gl_tex->states[WINED3DTEXSTA_SHADOW] = FALSE;
198 wined3d_texture_set_dirty(texture);
199 new_texture = TRUE;
201 if (texture->resource.usage & WINED3DUSAGE_AUTOGENMIPMAP)
203 /* This means double binding the texture at creation, but keeps
204 * the code simpler all in all, and the run-time path free from
205 * additional checks. */
206 context_bind_texture(context, target, gl_tex->name);
207 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_GENERATE_MIPMAP_SGIS, GL_TRUE);
208 checkGLcall("glTexParameteri(target, GL_GENERATE_MIPMAP_SGIS, GL_TRUE)");
211 else
213 *set_surface_desc = FALSE;
216 if (gl_tex->name)
218 context_bind_texture(context, target, gl_tex->name);
219 if (new_texture)
221 /* For a new texture we have to set the texture levels after
222 * binding the texture. Beware that texture rectangles do not
223 * support mipmapping, but set the maxmiplevel if we're relying
224 * on the partial GL_ARB_texture_non_power_of_two emulation with
225 * texture rectangles. (I.e., do not care about cond_np2 here,
226 * just look for GL_TEXTURE_RECTANGLE_ARB.) */
227 if (target != GL_TEXTURE_RECTANGLE_ARB)
229 TRACE("Setting GL_TEXTURE_MAX_LEVEL to %u.\n", texture->level_count - 1);
230 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, texture->level_count - 1);
231 checkGLcall("glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, texture->level_count)");
233 if (target == GL_TEXTURE_CUBE_MAP_ARB)
235 /* Cubemaps are always set to clamp, regardless of the sampler state. */
236 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
237 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
238 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
242 else
244 ERR("This texture doesn't have an OpenGL texture assigned to it.\n");
245 hr = WINED3DERR_INVALIDCALL;
248 return hr;
251 /* Context activation is done by the caller. */
252 static void apply_wrap(const struct wined3d_gl_info *gl_info, GLenum target,
253 enum wined3d_texture_address d3d_wrap, GLenum param, BOOL cond_np2)
255 GLint gl_wrap;
257 if (d3d_wrap < WINED3D_TADDRESS_WRAP || d3d_wrap > WINED3D_TADDRESS_MIRROR_ONCE)
259 FIXME("Unrecognized or unsupported texture address mode %#x.\n", d3d_wrap);
260 return;
263 /* Cubemaps are always set to clamp, regardless of the sampler state. */
264 if (target == GL_TEXTURE_CUBE_MAP_ARB
265 || (cond_np2 && d3d_wrap == WINED3D_TADDRESS_WRAP))
266 gl_wrap = GL_CLAMP_TO_EDGE;
267 else
268 gl_wrap = gl_info->wrap_lookup[d3d_wrap - WINED3D_TADDRESS_WRAP];
270 TRACE("Setting param %#x to %#x for target %#x.\n", param, gl_wrap, target);
271 gl_info->gl_ops.gl.p_glTexParameteri(target, param, gl_wrap);
272 checkGLcall("glTexParameteri(target, param, gl_wrap)");
275 /* Context activation is done by the caller (state handler). */
276 void wined3d_texture_apply_state_changes(struct wined3d_texture *texture,
277 const DWORD sampler_states[WINED3D_HIGHEST_SAMPLER_STATE + 1],
278 const struct wined3d_gl_info *gl_info)
280 BOOL cond_np2 = texture->flags & WINED3D_TEXTURE_COND_NP2;
281 GLenum target = texture->target;
282 struct gl_texture *gl_tex;
283 DWORD state;
284 DWORD aniso;
286 TRACE("texture %p, sampler_states %p.\n", texture, sampler_states);
288 gl_tex = wined3d_texture_get_gl_texture(texture, texture->flags & WINED3D_TEXTURE_IS_SRGB);
290 /* This function relies on the correct texture being bound and loaded. */
292 if (sampler_states[WINED3D_SAMP_ADDRESS_U] != gl_tex->states[WINED3DTEXSTA_ADDRESSU])
294 state = sampler_states[WINED3D_SAMP_ADDRESS_U];
295 apply_wrap(gl_info, target, state, GL_TEXTURE_WRAP_S, cond_np2);
296 gl_tex->states[WINED3DTEXSTA_ADDRESSU] = state;
299 if (sampler_states[WINED3D_SAMP_ADDRESS_V] != gl_tex->states[WINED3DTEXSTA_ADDRESSV])
301 state = sampler_states[WINED3D_SAMP_ADDRESS_V];
302 apply_wrap(gl_info, target, state, GL_TEXTURE_WRAP_T, cond_np2);
303 gl_tex->states[WINED3DTEXSTA_ADDRESSV] = state;
306 if (sampler_states[WINED3D_SAMP_ADDRESS_W] != gl_tex->states[WINED3DTEXSTA_ADDRESSW])
308 state = sampler_states[WINED3D_SAMP_ADDRESS_W];
309 apply_wrap(gl_info, target, state, GL_TEXTURE_WRAP_R, cond_np2);
310 gl_tex->states[WINED3DTEXSTA_ADDRESSW] = state;
313 if (sampler_states[WINED3D_SAMP_BORDER_COLOR] != gl_tex->states[WINED3DTEXSTA_BORDERCOLOR])
315 float col[4];
317 state = sampler_states[WINED3D_SAMP_BORDER_COLOR];
318 D3DCOLORTOGLFLOAT4(state, col);
319 TRACE("Setting border color for %#x to %#x.\n", target, state);
320 gl_info->gl_ops.gl.p_glTexParameterfv(target, GL_TEXTURE_BORDER_COLOR, &col[0]);
321 checkGLcall("glTexParameterfv(..., GL_TEXTURE_BORDER_COLOR, ...)");
322 gl_tex->states[WINED3DTEXSTA_BORDERCOLOR] = state;
325 if (sampler_states[WINED3D_SAMP_MAG_FILTER] != gl_tex->states[WINED3DTEXSTA_MAGFILTER])
327 GLint gl_value;
329 state = sampler_states[WINED3D_SAMP_MAG_FILTER];
330 if (state > WINED3D_TEXF_ANISOTROPIC)
331 FIXME("Unrecognized or unsupported MAGFILTER* value %d.\n", state);
333 gl_value = wined3d_gl_mag_filter(texture->mag_lookup,
334 min(max(state, WINED3D_TEXF_POINT), WINED3D_TEXF_LINEAR));
335 TRACE("ValueMAG=%#x setting MAGFILTER to %#x.\n", state, gl_value);
336 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAG_FILTER, gl_value);
338 gl_tex->states[WINED3DTEXSTA_MAGFILTER] = state;
341 if ((sampler_states[WINED3D_SAMP_MIN_FILTER] != gl_tex->states[WINED3DTEXSTA_MINFILTER]
342 || sampler_states[WINED3D_SAMP_MIP_FILTER] != gl_tex->states[WINED3DTEXSTA_MIPFILTER]
343 || sampler_states[WINED3D_SAMP_MAX_MIP_LEVEL] != gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL]))
345 GLint gl_value;
347 gl_tex->states[WINED3DTEXSTA_MIPFILTER] = sampler_states[WINED3D_SAMP_MIP_FILTER];
348 gl_tex->states[WINED3DTEXSTA_MINFILTER] = sampler_states[WINED3D_SAMP_MIN_FILTER];
349 gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL] = sampler_states[WINED3D_SAMP_MAX_MIP_LEVEL];
351 if (gl_tex->states[WINED3DTEXSTA_MINFILTER] > WINED3D_TEXF_ANISOTROPIC
352 || gl_tex->states[WINED3DTEXSTA_MIPFILTER] > WINED3D_TEXF_ANISOTROPIC)
354 FIXME("Unrecognized or unsupported MIN_FILTER value %#x MIP_FILTER value %#x.\n",
355 gl_tex->states[WINED3DTEXSTA_MINFILTER],
356 gl_tex->states[WINED3DTEXSTA_MIPFILTER]);
358 gl_value = wined3d_gl_min_mip_filter(texture->min_mip_lookup,
359 min(max(sampler_states[WINED3D_SAMP_MIN_FILTER], WINED3D_TEXF_POINT), WINED3D_TEXF_LINEAR),
360 min(max(sampler_states[WINED3D_SAMP_MIP_FILTER], WINED3D_TEXF_NONE), WINED3D_TEXF_LINEAR));
362 TRACE("ValueMIN=%#x, ValueMIP=%#x, setting MINFILTER to %#x.\n",
363 sampler_states[WINED3D_SAMP_MIN_FILTER],
364 sampler_states[WINED3D_SAMP_MIP_FILTER], gl_value);
365 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MIN_FILTER, gl_value);
366 checkGLcall("glTexParameter GL_TEXTURE_MIN_FILTER, ...");
368 if (!cond_np2)
370 if (gl_tex->states[WINED3DTEXSTA_MIPFILTER] == WINED3D_TEXF_NONE)
371 gl_value = texture->lod;
372 else if (gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL] >= texture->level_count)
373 gl_value = texture->level_count - 1;
374 else if (gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL] < texture->lod)
375 /* texture->lod is already clamped in the setter. */
376 gl_value = texture->lod;
377 else
378 gl_value = gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL];
380 /* Note that WINED3D_SAMP_MAX_MIP_LEVEL specifies the largest mipmap
381 * (default 0), while GL_TEXTURE_MAX_LEVEL specifies the smallest
382 * mimap used (default 1000). So WINED3D_SAMP_MAX_MIP_LEVEL
383 * corresponds to GL_TEXTURE_BASE_LEVEL. */
384 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_BASE_LEVEL, gl_value);
388 if ((gl_tex->states[WINED3DTEXSTA_MAGFILTER] != WINED3D_TEXF_ANISOTROPIC
389 && gl_tex->states[WINED3DTEXSTA_MINFILTER] != WINED3D_TEXF_ANISOTROPIC
390 && gl_tex->states[WINED3DTEXSTA_MIPFILTER] != WINED3D_TEXF_ANISOTROPIC)
391 || cond_np2)
392 aniso = 1;
393 else
394 aniso = sampler_states[WINED3D_SAMP_MAX_ANISOTROPY];
396 if (gl_tex->states[WINED3DTEXSTA_MAXANISOTROPY] != aniso)
398 if (gl_info->supported[EXT_TEXTURE_FILTER_ANISOTROPIC])
400 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAX_ANISOTROPY_EXT, aniso);
401 checkGLcall("glTexParameteri(GL_TEXTURE_MAX_ANISOTROPY_EXT, aniso)");
403 else
405 WARN("Anisotropic filtering not supported.\n");
407 gl_tex->states[WINED3DTEXSTA_MAXANISOTROPY] = aniso;
410 /* These should always be the same unless EXT_texture_sRGB_decode is supported. */
411 if (sampler_states[WINED3D_SAMP_SRGB_TEXTURE] != gl_tex->states[WINED3DTEXSTA_SRGBTEXTURE])
413 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_SRGB_DECODE_EXT,
414 sampler_states[WINED3D_SAMP_SRGB_TEXTURE] ? GL_DECODE_EXT : GL_SKIP_DECODE_EXT);
415 checkGLcall("glTexParameteri(GL_TEXTURE_SRGB_DECODE_EXT)");
416 gl_tex->states[WINED3DTEXSTA_SRGBTEXTURE] = sampler_states[WINED3D_SAMP_SRGB_TEXTURE];
419 if (!(texture->resource.format->flags & WINED3DFMT_FLAG_SHADOW)
420 != !gl_tex->states[WINED3DTEXSTA_SHADOW])
422 if (texture->resource.format->flags & WINED3DFMT_FLAG_SHADOW)
424 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_DEPTH_TEXTURE_MODE_ARB, GL_LUMINANCE);
425 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE_ARB);
426 checkGLcall("glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE_ARB)");
427 gl_tex->states[WINED3DTEXSTA_SHADOW] = TRUE;
429 else
431 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_NONE);
432 checkGLcall("glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_NONE)");
433 gl_tex->states[WINED3DTEXSTA_SHADOW] = FALSE;
438 ULONG CDECL wined3d_texture_incref(struct wined3d_texture *texture)
440 ULONG refcount = InterlockedIncrement(&texture->resource.ref);
442 TRACE("%p increasing refcount to %u.\n", texture, refcount);
444 return refcount;
447 ULONG CDECL wined3d_texture_decref(struct wined3d_texture *texture)
449 ULONG refcount = InterlockedDecrement(&texture->resource.ref);
451 TRACE("%p decreasing refcount to %u.\n", texture, refcount);
453 if (!refcount)
455 wined3d_texture_cleanup(texture);
456 texture->resource.parent_ops->wined3d_object_destroyed(texture->resource.parent);
457 HeapFree(GetProcessHeap(), 0, texture);
460 return refcount;
463 struct wined3d_resource * CDECL wined3d_texture_get_resource(struct wined3d_texture *texture)
465 TRACE("texture %p.\n", texture);
467 return &texture->resource;
470 DWORD CDECL wined3d_texture_set_priority(struct wined3d_texture *texture, DWORD priority)
472 return resource_set_priority(&texture->resource, priority);
475 DWORD CDECL wined3d_texture_get_priority(const struct wined3d_texture *texture)
477 return resource_get_priority(&texture->resource);
480 void CDECL wined3d_texture_preload(struct wined3d_texture *texture)
482 struct wined3d_context *context;
483 context = context_acquire(texture->resource.device, NULL);
484 texture->texture_ops->texture_preload(texture, context, SRGB_ANY);
485 context_release(context);
488 void * CDECL wined3d_texture_get_parent(const struct wined3d_texture *texture)
490 TRACE("texture %p.\n", texture);
492 return texture->resource.parent;
495 DWORD CDECL wined3d_texture_set_lod(struct wined3d_texture *texture, DWORD lod)
497 DWORD old = texture->lod;
499 TRACE("texture %p, lod %u.\n", texture, lod);
501 /* The d3d9:texture test shows that SetLOD is ignored on non-managed
502 * textures. The call always returns 0, and GetLOD always returns 0. */
503 if (texture->resource.pool != WINED3D_POOL_MANAGED)
505 TRACE("Ignoring SetLOD on %s texture, returning 0.\n", debug_d3dpool(texture->resource.pool));
506 return 0;
509 if (lod >= texture->level_count)
510 lod = texture->level_count - 1;
512 if (texture->lod != lod)
514 texture->lod = lod;
516 texture->texture_rgb.states[WINED3DTEXSTA_MAXMIPLEVEL] = ~0U;
517 texture->texture_srgb.states[WINED3DTEXSTA_MAXMIPLEVEL] = ~0U;
518 if (texture->resource.bind_count)
519 device_invalidate_state(texture->resource.device, STATE_SAMPLER(texture->sampler));
522 return old;
525 DWORD CDECL wined3d_texture_get_lod(const struct wined3d_texture *texture)
527 TRACE("texture %p, returning %u.\n", texture, texture->lod);
529 return texture->lod;
532 DWORD CDECL wined3d_texture_get_level_count(const struct wined3d_texture *texture)
534 TRACE("texture %p, returning %u.\n", texture, texture->level_count);
536 return texture->level_count;
539 HRESULT CDECL wined3d_texture_set_autogen_filter_type(struct wined3d_texture *texture,
540 enum wined3d_texture_filter_type filter_type)
542 FIXME("texture %p, filter_type %s stub!\n", texture, debug_d3dtexturefiltertype(filter_type));
544 if (!(texture->resource.usage & WINED3DUSAGE_AUTOGENMIPMAP))
546 WARN("Texture doesn't have AUTOGENMIPMAP usage.\n");
547 return WINED3DERR_INVALIDCALL;
550 texture->filter_type = filter_type;
552 return WINED3D_OK;
555 enum wined3d_texture_filter_type CDECL wined3d_texture_get_autogen_filter_type(const struct wined3d_texture *texture)
557 TRACE("texture %p.\n", texture);
559 return texture->filter_type;
562 void CDECL wined3d_texture_generate_mipmaps(struct wined3d_texture *texture)
564 /* TODO: Implement filters using GL_SGI_generate_mipmaps. */
565 FIXME("texture %p stub!\n", texture);
568 struct wined3d_resource * CDECL wined3d_texture_get_sub_resource(struct wined3d_texture *texture,
569 UINT sub_resource_idx)
571 UINT sub_count = texture->level_count * texture->layer_count;
573 TRACE("texture %p, sub_resource_idx %u.\n", texture, sub_resource_idx);
575 if (sub_resource_idx >= sub_count)
577 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx, sub_count);
578 return NULL;
581 return texture->sub_resources[sub_resource_idx];
584 HRESULT CDECL wined3d_texture_add_dirty_region(struct wined3d_texture *texture,
585 UINT layer, const struct wined3d_box *dirty_region)
587 struct wined3d_resource *sub_resource;
589 TRACE("texture %p, layer %u, dirty_region %p.\n", texture, layer, dirty_region);
591 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, layer * texture->level_count)))
593 WARN("Failed to get sub-resource.\n");
594 return WINED3DERR_INVALIDCALL;
597 wined3d_texture_set_dirty(texture);
598 texture->texture_ops->texture_sub_resource_add_dirty_region(sub_resource, dirty_region);
600 return WINED3D_OK;
603 /* Context activation is done by the caller. */
604 static HRESULT texture2d_bind(struct wined3d_texture *texture,
605 struct wined3d_context *context, BOOL srgb)
607 const struct wined3d_gl_info *gl_info = context->gl_info;
608 BOOL set_gl_texture_desc;
609 HRESULT hr;
611 TRACE("texture %p, context %p, srgb %#x.\n", texture, context, srgb);
613 hr = wined3d_texture_bind(texture, context, srgb, &set_gl_texture_desc);
614 if (set_gl_texture_desc && SUCCEEDED(hr))
616 UINT sub_count = texture->level_count * texture->layer_count;
617 BOOL srgb_tex = texture->flags & WINED3D_TEXTURE_IS_SRGB;
618 struct gl_texture *gl_tex;
619 UINT i;
621 gl_tex = wined3d_texture_get_gl_texture(texture, srgb_tex);
623 for (i = 0; i < sub_count; ++i)
625 struct wined3d_surface *surface = surface_from_resource(texture->sub_resources[i]);
626 surface_set_texture_name(surface, gl_tex->name, srgb_tex);
629 /* Conditinal non power of two textures use a different clamping
630 * default. If we're using the GL_WINE_normalized_texrect partial
631 * driver emulation, we're dealing with a GL_TEXTURE_2D texture which
632 * has the address mode set to repeat - something that prevents us
633 * from hitting the accelerated codepath. Thus manually set the GL
634 * state. The same applies to filtering. Even if the texture has only
635 * one mip level, the default LINEAR_MIPMAP_LINEAR filter causes a SW
636 * fallback on macos. */
637 if (texture->flags & WINED3D_TEXTURE_COND_NP2)
639 GLenum target = texture->target;
641 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
642 checkGLcall("glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)");
643 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
644 checkGLcall("glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)");
645 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
646 checkGLcall("glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST)");
647 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
648 checkGLcall("glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST)");
649 gl_tex->states[WINED3DTEXSTA_ADDRESSU] = WINED3D_TADDRESS_CLAMP;
650 gl_tex->states[WINED3DTEXSTA_ADDRESSV] = WINED3D_TADDRESS_CLAMP;
651 gl_tex->states[WINED3DTEXSTA_MAGFILTER] = WINED3D_TEXF_POINT;
652 gl_tex->states[WINED3DTEXSTA_MINFILTER] = WINED3D_TEXF_POINT;
653 gl_tex->states[WINED3DTEXSTA_MIPFILTER] = WINED3D_TEXF_NONE;
657 return hr;
660 static BOOL texture_srgb_mode(const struct wined3d_texture *texture, enum WINED3DSRGB srgb)
662 switch (srgb)
664 case SRGB_RGB:
665 return FALSE;
667 case SRGB_SRGB:
668 return TRUE;
670 default:
671 return texture->flags & WINED3D_TEXTURE_IS_SRGB;
675 /* Context activation is done by the caller */
676 static void texture2d_preload(struct wined3d_texture *texture,
677 struct wined3d_context *context, enum WINED3DSRGB srgb)
679 UINT sub_count = texture->level_count * texture->layer_count;
680 const struct wined3d_gl_info *gl_info = context->gl_info;
681 BOOL srgb_mode;
682 DWORD flag;
683 UINT i;
685 TRACE("texture %p, srgb %#x.\n", texture, srgb);
687 if (gl_info->supported[EXT_TEXTURE_SRGB_DECODE])
688 srgb = SRGB_RGB;
690 srgb_mode = texture_srgb_mode(texture, srgb);
691 if (srgb_mode)
692 flag = WINED3D_TEXTURE_SRGB_VALID;
693 else
694 flag = WINED3D_TEXTURE_RGB_VALID;
696 if (texture->flags & flag)
698 TRACE("Texture %p not dirty, nothing to do.\n", texture);
699 return;
702 /* Reload the surfaces if the texture is marked dirty. */
703 for (i = 0; i < sub_count; ++i)
705 surface_load(surface_from_resource(texture->sub_resources[i]), srgb_mode);
707 texture->flags |= flag;
710 static void texture2d_sub_resource_add_dirty_region(struct wined3d_resource *sub_resource,
711 const struct wined3d_box *dirty_region)
713 surface_add_dirty_rect(surface_from_resource(sub_resource), dirty_region);
716 static void texture2d_sub_resource_cleanup(struct wined3d_resource *sub_resource)
718 struct wined3d_surface *surface = surface_from_resource(sub_resource);
720 /* Clean out the texture name we gave to the surface so that the
721 * surface doesn't try and release it. */
722 surface_set_texture_name(surface, 0, TRUE);
723 surface_set_texture_name(surface, 0, FALSE);
724 surface_set_texture_target(surface, 0, 0);
725 surface_set_container(surface, NULL);
726 wined3d_surface_decref(surface);
729 static void texture2d_unload(struct wined3d_resource *resource)
731 struct wined3d_texture *texture = wined3d_texture_from_resource(resource);
732 UINT sub_count = texture->level_count * texture->layer_count;
733 UINT i;
735 TRACE("texture %p.\n", texture);
737 for (i = 0; i < sub_count; ++i)
739 struct wined3d_resource *sub_resource = texture->sub_resources[i];
740 struct wined3d_surface *surface = surface_from_resource(sub_resource);
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 */
747 wined3d_texture_unload(texture);
750 static const struct wined3d_texture_ops texture2d_ops =
752 texture2d_bind,
753 texture2d_preload,
754 texture2d_sub_resource_add_dirty_region,
755 texture2d_sub_resource_cleanup,
758 static const struct wined3d_resource_ops texture2d_resource_ops =
760 texture2d_unload,
763 static HRESULT cubetexture_init(struct wined3d_texture *texture, const struct wined3d_resource_desc *desc,
764 UINT levels, DWORD surface_flags, struct wined3d_device *device, void *parent,
765 const struct wined3d_parent_ops *parent_ops)
767 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
768 struct wined3d_resource_desc surface_desc;
769 unsigned int i, j;
770 HRESULT hr;
772 /* TODO: It should only be possible to create textures for formats
773 * that are reported as supported. */
774 if (WINED3DFMT_UNKNOWN >= desc->format)
776 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture);
777 return WINED3DERR_INVALIDCALL;
780 if (!gl_info->supported[ARB_TEXTURE_CUBE_MAP] && desc->pool != WINED3D_POOL_SCRATCH)
782 WARN("(%p) : Tried to create not supported cube texture.\n", texture);
783 return WINED3DERR_INVALIDCALL;
786 /* Calculate levels for mip mapping */
787 if (desc->usage & WINED3DUSAGE_AUTOGENMIPMAP)
789 if (!gl_info->supported[SGIS_GENERATE_MIPMAP])
791 WARN("No mipmap generation support, returning D3DERR_INVALIDCALL.\n");
792 return WINED3DERR_INVALIDCALL;
795 if (levels > 1)
797 WARN("D3DUSAGE_AUTOGENMIPMAP is set, and level count > 1, returning D3DERR_INVALIDCALL.\n");
798 return WINED3DERR_INVALIDCALL;
801 levels = 1;
803 else if (!levels)
805 levels = wined3d_log2i(desc->width) + 1;
806 TRACE("Calculated levels = %u.\n", levels);
809 if (!gl_info->supported[ARB_TEXTURE_NON_POWER_OF_TWO])
811 UINT pow2_edge_length = 1;
812 while (pow2_edge_length < desc->width)
813 pow2_edge_length <<= 1;
815 if (desc->width != pow2_edge_length)
817 if (desc->pool == WINED3D_POOL_SCRATCH)
819 /* SCRATCH textures cannot be used for texturing */
820 WARN("Creating a scratch NPOT cube texture despite lack of HW support.\n");
822 else
824 WARN("Attempted to create a NPOT cube texture (edge length %u) without GL support.\n", desc->width);
825 return WINED3DERR_INVALIDCALL;
830 if (FAILED(hr = wined3d_texture_init(texture, &texture2d_ops, 6, levels,
831 desc, device, parent, parent_ops, &texture2d_resource_ops)))
833 WARN("Failed to initialize texture, returning %#x\n", hr);
834 return hr;
837 texture->pow2_matrix[0] = 1.0f;
838 texture->pow2_matrix[5] = 1.0f;
839 texture->pow2_matrix[10] = 1.0f;
840 texture->pow2_matrix[15] = 1.0f;
841 texture->target = GL_TEXTURE_CUBE_MAP_ARB;
843 /* Generate all the surfaces. */
844 surface_desc = *desc;
845 surface_desc.resource_type = WINED3D_RTYPE_SURFACE;
846 for (i = 0; i < texture->level_count; ++i)
848 /* Create the 6 faces. */
849 for (j = 0; j < 6; ++j)
851 static const GLenum cube_targets[6] =
853 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB,
854 GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB,
855 GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB,
856 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB,
857 GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB,
858 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB,
860 UINT idx = j * texture->level_count + i;
861 struct wined3d_surface *surface;
863 if (FAILED(hr = device->device_parent->ops->create_texture_surface(device->device_parent,
864 parent, &surface_desc, idx, surface_flags, &surface)))
866 FIXME("(%p) Failed to create surface, hr %#x.\n", texture, hr);
867 wined3d_texture_cleanup(texture);
868 return hr;
871 surface_set_container(surface, texture);
872 surface_set_texture_target(surface, cube_targets[j], i);
873 texture->sub_resources[idx] = &surface->resource;
874 TRACE("Created surface level %u @ %p.\n", i, surface);
876 surface_desc.width = max(1, surface_desc.width >> 1);
877 surface_desc.height = surface_desc.width;
880 return WINED3D_OK;
883 static HRESULT texture_init(struct wined3d_texture *texture, const struct wined3d_resource_desc *desc,
884 UINT levels, DWORD surface_flags, struct wined3d_device *device, void *parent,
885 const struct wined3d_parent_ops *parent_ops)
887 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
888 struct wined3d_resource_desc surface_desc;
889 UINT pow2_width, pow2_height;
890 unsigned int i;
891 HRESULT hr;
893 /* TODO: It should only be possible to create textures for formats
894 * that are reported as supported. */
895 if (WINED3DFMT_UNKNOWN >= desc->format)
897 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture);
898 return WINED3DERR_INVALIDCALL;
901 /* Non-power2 support. */
902 if (gl_info->supported[ARB_TEXTURE_NON_POWER_OF_TWO])
904 pow2_width = desc->width;
905 pow2_height = desc->height;
907 else
909 /* Find the nearest pow2 match. */
910 pow2_width = pow2_height = 1;
911 while (pow2_width < desc->width)
912 pow2_width <<= 1;
913 while (pow2_height < desc->height)
914 pow2_height <<= 1;
916 if (pow2_width != desc->width || pow2_height != desc->height)
918 /* levels == 0 returns an error as well */
919 if (levels != 1)
921 if (desc->pool == WINED3D_POOL_SCRATCH)
923 WARN("Creating a scratch mipmapped NPOT texture despite lack of HW support.\n");
925 else
927 WARN("Attempted to create a mipmapped NPOT texture without unconditional NPOT support.\n");
928 return WINED3DERR_INVALIDCALL;
934 /* Calculate levels for mip mapping. */
935 if (desc->usage & WINED3DUSAGE_AUTOGENMIPMAP)
937 if (!gl_info->supported[SGIS_GENERATE_MIPMAP])
939 WARN("No mipmap generation support, returning WINED3DERR_INVALIDCALL.\n");
940 return WINED3DERR_INVALIDCALL;
943 if (levels > 1)
945 WARN("D3DUSAGE_AUTOGENMIPMAP is set, and level count > 1, returning WINED3DERR_INVALIDCALL.\n");
946 return WINED3DERR_INVALIDCALL;
949 levels = 1;
951 else if (!levels)
953 levels = wined3d_log2i(max(desc->width, desc->height)) + 1;
954 TRACE("Calculated levels = %u.\n", levels);
957 if (FAILED(hr = wined3d_texture_init(texture, &texture2d_ops, 1, levels,
958 desc, device, parent, parent_ops, &texture2d_resource_ops)))
960 WARN("Failed to initialize texture, returning %#x.\n", hr);
961 return hr;
964 /* Precalculated scaling for 'faked' non power of two texture coords. */
965 if (gl_info->supported[WINED3D_GL_NORMALIZED_TEXRECT]
966 && (desc->width != pow2_width || desc->height != pow2_height))
968 texture->pow2_matrix[0] = 1.0f;
969 texture->pow2_matrix[5] = 1.0f;
970 texture->pow2_matrix[10] = 1.0f;
971 texture->pow2_matrix[15] = 1.0f;
972 texture->target = GL_TEXTURE_2D;
973 texture->flags |= WINED3D_TEXTURE_COND_NP2;
974 texture->min_mip_lookup = minMipLookup_noFilter;
976 else if (gl_info->supported[ARB_TEXTURE_RECTANGLE]
977 && (desc->width != pow2_width || desc->height != pow2_height))
979 texture->pow2_matrix[0] = (float)desc->width;
980 texture->pow2_matrix[5] = (float)desc->height;
981 texture->pow2_matrix[10] = 1.0f;
982 texture->pow2_matrix[15] = 1.0f;
983 texture->target = GL_TEXTURE_RECTANGLE_ARB;
984 texture->flags |= WINED3D_TEXTURE_COND_NP2;
985 texture->flags &= ~WINED3D_TEXTURE_POW2_MAT_IDENT;
987 if (texture->resource.format->flags & WINED3DFMT_FLAG_FILTERING)
988 texture->min_mip_lookup = minMipLookup_noMip;
989 else
990 texture->min_mip_lookup = minMipLookup_noFilter;
992 else
994 if ((desc->width != pow2_width) || (desc->height != pow2_height))
996 texture->pow2_matrix[0] = (((float)desc->width) / ((float)pow2_width));
997 texture->pow2_matrix[5] = (((float)desc->height) / ((float)pow2_height));
998 texture->flags &= ~WINED3D_TEXTURE_POW2_MAT_IDENT;
1000 else
1002 texture->pow2_matrix[0] = 1.0f;
1003 texture->pow2_matrix[5] = 1.0f;
1006 texture->pow2_matrix[10] = 1.0f;
1007 texture->pow2_matrix[15] = 1.0f;
1008 texture->target = GL_TEXTURE_2D;
1010 TRACE("xf(%f) yf(%f)\n", texture->pow2_matrix[0], texture->pow2_matrix[5]);
1012 /* Generate all the surfaces. */
1013 surface_desc = *desc;
1014 surface_desc.resource_type = WINED3D_RTYPE_SURFACE;
1015 for (i = 0; i < texture->level_count; ++i)
1017 struct wined3d_surface *surface;
1019 /* Use the callback to create the texture surface. */
1020 if (FAILED(hr = device->device_parent->ops->create_texture_surface(device->device_parent,
1021 parent, &surface_desc, i, surface_flags, &surface)))
1023 FIXME("Failed to create surface %p, hr %#x\n", texture, hr);
1024 wined3d_texture_cleanup(texture);
1025 return hr;
1028 surface_set_container(surface, texture);
1029 surface_set_texture_target(surface, texture->target, i);
1030 texture->sub_resources[i] = &surface->resource;
1031 TRACE("Created surface level %u @ %p.\n", i, surface);
1032 /* Calculate the next mipmap level. */
1033 surface_desc.width = max(1, surface_desc.width >> 1);
1034 surface_desc.height = max(1, surface_desc.height >> 1);
1037 return WINED3D_OK;
1040 /* Context activation is done by the caller. */
1041 static HRESULT texture3d_bind(struct wined3d_texture *texture,
1042 struct wined3d_context *context, BOOL srgb)
1044 BOOL dummy;
1046 TRACE("texture %p, context %p, srgb %#x.\n", texture, context, srgb);
1048 return wined3d_texture_bind(texture, context, srgb, &dummy);
1051 /* Context activation is done by the caller. */
1052 static void texture3d_preload(struct wined3d_texture *texture,
1053 struct wined3d_context *context, enum WINED3DSRGB srgb)
1055 UINT sub_count = texture->level_count * texture->layer_count;
1056 const struct wined3d_gl_info *gl_info = context->gl_info;
1057 BOOL srgb_mode;
1058 DWORD flag;
1059 UINT i;
1061 TRACE("texture %p, srgb %#x.\n", texture, srgb);
1063 if (gl_info->supported[EXT_TEXTURE_SRGB_DECODE])
1064 srgb = SRGB_RGB;
1066 srgb_mode = texture_srgb_mode(texture, srgb);
1067 if (srgb_mode)
1068 flag = WINED3D_TEXTURE_SRGB_VALID;
1069 else
1070 flag = WINED3D_TEXTURE_RGB_VALID;
1072 if (texture->flags & flag)
1074 TRACE("Texture %p not dirty, nothing to do.\n", texture);
1075 return;
1078 /* Reload the surfaces if the texture is marked dirty. */
1079 for (i = 0; i < sub_count; ++i)
1081 wined3d_volume_load(volume_from_resource(texture->sub_resources[i]), context, srgb_mode);
1083 texture->flags |= flag;
1086 static void texture3d_sub_resource_add_dirty_region(struct wined3d_resource *sub_resource,
1087 const struct wined3d_box *dirty_region)
1091 static void texture3d_sub_resource_cleanup(struct wined3d_resource *sub_resource)
1093 struct wined3d_volume *volume = volume_from_resource(sub_resource);
1095 /* Cleanup the container. */
1096 volume_set_container(volume, NULL);
1097 wined3d_volume_decref(volume);
1100 static void texture3d_unload(struct wined3d_resource *resource)
1102 struct wined3d_texture *texture = wined3d_texture_from_resource(resource);
1103 UINT i;
1105 TRACE("texture %p.\n", texture);
1107 for (i = 0; i < texture->level_count; ++i)
1109 struct wined3d_resource *sub_resource = texture->sub_resources[i];
1110 sub_resource->resource_ops->resource_unload(sub_resource);
1113 wined3d_texture_unload(texture);
1116 static const struct wined3d_texture_ops texture3d_ops =
1118 texture3d_bind,
1119 texture3d_preload,
1120 texture3d_sub_resource_add_dirty_region,
1121 texture3d_sub_resource_cleanup,
1124 static const struct wined3d_resource_ops texture3d_resource_ops =
1126 texture3d_unload,
1129 static HRESULT volumetexture_init(struct wined3d_texture *texture, const struct wined3d_resource_desc *desc,
1130 UINT levels, struct wined3d_device *device, void *parent, const struct wined3d_parent_ops *parent_ops)
1132 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
1133 UINT tmp_w, tmp_h, tmp_d;
1134 unsigned int i;
1135 HRESULT hr;
1137 /* TODO: It should only be possible to create textures for formats
1138 * that are reported as supported. */
1139 if (WINED3DFMT_UNKNOWN >= desc->format)
1141 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture);
1142 return WINED3DERR_INVALIDCALL;
1145 if (!gl_info->supported[EXT_TEXTURE3D])
1147 WARN("(%p) : Texture cannot be created - no volume texture support.\n", texture);
1148 return WINED3DERR_INVALIDCALL;
1151 /* Calculate levels for mip mapping. */
1152 if (desc->usage & WINED3DUSAGE_AUTOGENMIPMAP)
1154 if (!gl_info->supported[SGIS_GENERATE_MIPMAP])
1156 WARN("No mipmap generation support, returning D3DERR_INVALIDCALL.\n");
1157 return WINED3DERR_INVALIDCALL;
1160 if (levels > 1)
1162 WARN("D3DUSAGE_AUTOGENMIPMAP is set, and level count > 1, returning D3DERR_INVALIDCALL.\n");
1163 return WINED3DERR_INVALIDCALL;
1166 levels = 1;
1168 else if (!levels)
1170 levels = wined3d_log2i(max(max(desc->width, desc->height), desc->depth)) + 1;
1171 TRACE("Calculated levels = %u.\n", levels);
1174 if (!gl_info->supported[ARB_TEXTURE_NON_POWER_OF_TWO])
1176 UINT pow2_w, pow2_h, pow2_d;
1177 pow2_w = 1;
1178 while (pow2_w < desc->width)
1179 pow2_w <<= 1;
1180 pow2_h = 1;
1181 while (pow2_h < desc->height)
1182 pow2_h <<= 1;
1183 pow2_d = 1;
1184 while (pow2_d < desc->depth)
1185 pow2_d <<= 1;
1187 if (pow2_w != desc->width || pow2_h != desc->height || pow2_d != desc->depth)
1189 if (desc->pool == WINED3D_POOL_SCRATCH)
1191 WARN("Creating a scratch NPOT volume texture despite lack of HW support.\n");
1193 else
1195 WARN("Attempted to create a NPOT volume texture (%u, %u, %u) without GL support.\n",
1196 desc->width, desc->height, desc->depth);
1197 return WINED3DERR_INVALIDCALL;
1202 if (FAILED(hr = wined3d_texture_init(texture, &texture3d_ops, 1, levels,
1203 desc, device, parent, parent_ops, &texture3d_resource_ops)))
1205 WARN("Failed to initialize texture, returning %#x.\n", hr);
1206 return hr;
1209 texture->pow2_matrix[0] = 1.0f;
1210 texture->pow2_matrix[5] = 1.0f;
1211 texture->pow2_matrix[10] = 1.0f;
1212 texture->pow2_matrix[15] = 1.0f;
1213 texture->target = GL_TEXTURE_3D;
1215 /* Generate all the surfaces. */
1216 tmp_w = desc->width;
1217 tmp_h = desc->height;
1218 tmp_d = desc->depth;
1220 for (i = 0; i < texture->level_count; ++i)
1222 struct wined3d_volume *volume;
1224 /* Create the volume. */
1225 hr = device->device_parent->ops->create_volume(device->device_parent, parent,
1226 tmp_w, tmp_h, tmp_d, i, desc->format, desc->pool, desc->usage, &volume);
1227 if (FAILED(hr))
1229 ERR("Creating a volume for the volume texture failed, hr %#x.\n", hr);
1230 wined3d_texture_cleanup(texture);
1231 return hr;
1234 /* Set its container to this texture. */
1235 volume_set_container(volume, texture);
1236 texture->sub_resources[i] = &volume->resource;
1238 /* Calculate the next mipmap level. */
1239 tmp_w = max(1, tmp_w >> 1);
1240 tmp_h = max(1, tmp_h >> 1);
1241 tmp_d = max(1, tmp_d >> 1);
1244 return WINED3D_OK;
1247 HRESULT CDECL wined3d_texture_create_2d(struct wined3d_device *device, const struct wined3d_resource_desc *desc,
1248 UINT level_count, DWORD surface_flags, void *parent, const struct wined3d_parent_ops *parent_ops,
1249 struct wined3d_texture **texture)
1251 struct wined3d_texture *object;
1252 HRESULT hr;
1254 TRACE("device %p, desc %p, level_count %u, surface_flags %#x, parent %p, parent_ops %p, texture %p.\n",
1255 device, desc, level_count, surface_flags, parent, parent_ops, texture);
1257 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1258 if (!object)
1260 *texture = NULL;
1261 return WINED3DERR_OUTOFVIDEOMEMORY;
1264 if (FAILED(hr = texture_init(object, desc, level_count, surface_flags, device, parent, parent_ops)))
1266 WARN("Failed to initialize texture, returning %#x.\n", hr);
1267 HeapFree(GetProcessHeap(), 0, object);
1268 *texture = NULL;
1269 return hr;
1272 TRACE("Created texture %p.\n", object);
1273 *texture = object;
1275 return WINED3D_OK;
1278 HRESULT CDECL wined3d_texture_create_3d(struct wined3d_device *device, const struct wined3d_resource_desc *desc,
1279 UINT level_count, void *parent, const struct wined3d_parent_ops *parent_ops, struct wined3d_texture **texture)
1281 struct wined3d_texture *object;
1282 HRESULT hr;
1284 TRACE("device %p, desc %p, level_count %u, parent %p, parent_ops %p, texture %p.\n",
1285 device, desc, level_count, parent, parent_ops, texture);
1287 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1288 if (!object)
1290 *texture = NULL;
1291 return WINED3DERR_OUTOFVIDEOMEMORY;
1294 if (FAILED(hr = volumetexture_init(object, desc, level_count, device, parent, parent_ops)))
1296 WARN("Failed to initialize volumetexture, returning %#x\n", hr);
1297 HeapFree(GetProcessHeap(), 0, object);
1298 *texture = NULL;
1299 return hr;
1302 TRACE("Created texture %p.\n", object);
1303 *texture = object;
1305 return WINED3D_OK;
1308 HRESULT CDECL wined3d_texture_create_cube(struct wined3d_device *device, const struct wined3d_resource_desc *desc,
1309 UINT level_count, DWORD surface_flags, void *parent, const struct wined3d_parent_ops *parent_ops,
1310 struct wined3d_texture **texture)
1312 struct wined3d_texture *object;
1313 HRESULT hr;
1315 TRACE("device %p, desc %p, level_count %u, surface_flags %#x, parent %p, parent_ops %p, texture %p.\n",
1316 device, desc, level_count, surface_flags, parent, parent_ops, texture);
1318 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1319 if (!object)
1321 *texture = NULL;
1322 return WINED3DERR_OUTOFVIDEOMEMORY;
1325 if (FAILED(hr = cubetexture_init(object, desc, level_count, surface_flags, device, parent, parent_ops)))
1327 WARN("Failed to initialize cubetexture, returning %#x\n", hr);
1328 HeapFree(GetProcessHeap(), 0, object);
1329 *texture = NULL;
1330 return hr;
1333 TRACE("Created texture %p.\n", object);
1334 *texture = object;
1336 return WINED3D_OK;