wined3d: Pass the destination pitch to format conversion functions.
[wine.git] / dlls / wined3d / texture.c
blob56d13f67d56a19f37d8b9424629294017aad7598
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 /* sRGB mode cache for preload() calls outside drawprim. */
158 if (srgb)
159 texture->flags |= WINED3D_TEXTURE_IS_SRGB;
160 else
161 texture->flags &= ~WINED3D_TEXTURE_IS_SRGB;
163 gl_tex = wined3d_texture_get_gl_texture(texture, context->gl_info, srgb);
164 target = texture->target;
166 /* Generate a texture name if we don't already have one. */
167 if (!gl_tex->name)
169 *set_surface_desc = TRUE;
170 gl_info->gl_ops.gl.p_glGenTextures(1, &gl_tex->name);
171 checkGLcall("glGenTextures");
172 TRACE("Generated texture %d.\n", gl_tex->name);
173 if (texture->resource.pool == WINED3D_POOL_DEFAULT)
175 /* Tell OpenGL to try and keep this texture in video ram (well mostly). */
176 GLclampf tmp = 0.9f;
177 gl_info->gl_ops.gl.p_glPrioritizeTextures(1, &gl_tex->name, &tmp);
179 /* Initialise the state of the texture object to the OpenGL defaults,
180 * not the D3D defaults. */
181 gl_tex->states[WINED3DTEXSTA_ADDRESSU] = WINED3D_TADDRESS_WRAP;
182 gl_tex->states[WINED3DTEXSTA_ADDRESSV] = WINED3D_TADDRESS_WRAP;
183 gl_tex->states[WINED3DTEXSTA_ADDRESSW] = WINED3D_TADDRESS_WRAP;
184 gl_tex->states[WINED3DTEXSTA_BORDERCOLOR] = 0;
185 gl_tex->states[WINED3DTEXSTA_MAGFILTER] = WINED3D_TEXF_LINEAR;
186 gl_tex->states[WINED3DTEXSTA_MINFILTER] = WINED3D_TEXF_POINT; /* GL_NEAREST_MIPMAP_LINEAR */
187 gl_tex->states[WINED3DTEXSTA_MIPFILTER] = WINED3D_TEXF_LINEAR; /* GL_NEAREST_MIPMAP_LINEAR */
188 gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL] = 0;
189 gl_tex->states[WINED3DTEXSTA_MAXANISOTROPY] = 1;
190 if (context->gl_info->supported[EXT_TEXTURE_SRGB_DECODE])
191 gl_tex->states[WINED3DTEXSTA_SRGBTEXTURE] = TRUE;
192 else
193 gl_tex->states[WINED3DTEXSTA_SRGBTEXTURE] = srgb;
194 gl_tex->states[WINED3DTEXSTA_SHADOW] = FALSE;
195 wined3d_texture_set_dirty(texture);
196 new_texture = TRUE;
198 if (texture->resource.usage & WINED3DUSAGE_AUTOGENMIPMAP)
200 /* This means double binding the texture at creation, but keeps
201 * the code simpler all in all, and the run-time path free from
202 * additional checks. */
203 context_bind_texture(context, target, gl_tex->name);
204 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_GENERATE_MIPMAP_SGIS, GL_TRUE);
205 checkGLcall("glTexParameteri(target, GL_GENERATE_MIPMAP_SGIS, GL_TRUE)");
208 else
210 *set_surface_desc = FALSE;
213 if (gl_tex->name)
215 context_bind_texture(context, target, gl_tex->name);
216 if (new_texture)
218 /* For a new texture we have to set the texture levels after
219 * binding the texture. Beware that texture rectangles do not
220 * support mipmapping, but set the maxmiplevel if we're relying
221 * on the partial GL_ARB_texture_non_power_of_two emulation with
222 * texture rectangles. (I.e., do not care about cond_np2 here,
223 * just look for GL_TEXTURE_RECTANGLE_ARB.) */
224 if (target != GL_TEXTURE_RECTANGLE_ARB)
226 TRACE("Setting GL_TEXTURE_MAX_LEVEL to %u.\n", texture->level_count - 1);
227 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, texture->level_count - 1);
228 checkGLcall("glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, texture->level_count)");
230 if (target == GL_TEXTURE_CUBE_MAP_ARB)
232 /* Cubemaps are always set to clamp, regardless of the sampler state. */
233 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
234 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
235 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
239 else
241 ERR("This texture doesn't have an OpenGL texture assigned to it.\n");
242 hr = WINED3DERR_INVALIDCALL;
245 return hr;
248 /* Context activation is done by the caller. */
249 static void apply_wrap(const struct wined3d_gl_info *gl_info, GLenum target,
250 enum wined3d_texture_address d3d_wrap, GLenum param, BOOL cond_np2)
252 GLint gl_wrap;
254 if (d3d_wrap < WINED3D_TADDRESS_WRAP || d3d_wrap > WINED3D_TADDRESS_MIRROR_ONCE)
256 FIXME("Unrecognized or unsupported texture address mode %#x.\n", d3d_wrap);
257 return;
260 /* Cubemaps are always set to clamp, regardless of the sampler state. */
261 if (target == GL_TEXTURE_CUBE_MAP_ARB
262 || (cond_np2 && d3d_wrap == WINED3D_TADDRESS_WRAP))
263 gl_wrap = GL_CLAMP_TO_EDGE;
264 else
265 gl_wrap = gl_info->wrap_lookup[d3d_wrap - WINED3D_TADDRESS_WRAP];
267 TRACE("Setting param %#x to %#x for target %#x.\n", param, gl_wrap, target);
268 gl_info->gl_ops.gl.p_glTexParameteri(target, param, gl_wrap);
269 checkGLcall("glTexParameteri(target, param, gl_wrap)");
272 /* Context activation is done by the caller (state handler). */
273 void wined3d_texture_apply_state_changes(struct wined3d_texture *texture,
274 const DWORD sampler_states[WINED3D_HIGHEST_SAMPLER_STATE + 1],
275 const struct wined3d_gl_info *gl_info)
277 BOOL cond_np2 = texture->flags & WINED3D_TEXTURE_COND_NP2;
278 GLenum target = texture->target;
279 struct gl_texture *gl_tex;
280 DWORD state;
281 DWORD aniso;
283 TRACE("texture %p, sampler_states %p.\n", texture, sampler_states);
285 gl_tex = wined3d_texture_get_gl_texture(texture, gl_info,
286 texture->flags & WINED3D_TEXTURE_IS_SRGB);
288 /* This function relies on the correct texture being bound and loaded. */
290 if (sampler_states[WINED3D_SAMP_ADDRESS_U] != gl_tex->states[WINED3DTEXSTA_ADDRESSU])
292 state = sampler_states[WINED3D_SAMP_ADDRESS_U];
293 apply_wrap(gl_info, target, state, GL_TEXTURE_WRAP_S, cond_np2);
294 gl_tex->states[WINED3DTEXSTA_ADDRESSU] = state;
297 if (sampler_states[WINED3D_SAMP_ADDRESS_V] != gl_tex->states[WINED3DTEXSTA_ADDRESSV])
299 state = sampler_states[WINED3D_SAMP_ADDRESS_V];
300 apply_wrap(gl_info, target, state, GL_TEXTURE_WRAP_T, cond_np2);
301 gl_tex->states[WINED3DTEXSTA_ADDRESSV] = state;
304 if (sampler_states[WINED3D_SAMP_ADDRESS_W] != gl_tex->states[WINED3DTEXSTA_ADDRESSW])
306 state = sampler_states[WINED3D_SAMP_ADDRESS_W];
307 apply_wrap(gl_info, target, state, GL_TEXTURE_WRAP_R, cond_np2);
308 gl_tex->states[WINED3DTEXSTA_ADDRESSW] = state;
311 if (sampler_states[WINED3D_SAMP_BORDER_COLOR] != gl_tex->states[WINED3DTEXSTA_BORDERCOLOR])
313 float col[4];
315 state = sampler_states[WINED3D_SAMP_BORDER_COLOR];
316 D3DCOLORTOGLFLOAT4(state, col);
317 TRACE("Setting border color for %#x to %#x.\n", target, state);
318 gl_info->gl_ops.gl.p_glTexParameterfv(target, GL_TEXTURE_BORDER_COLOR, &col[0]);
319 checkGLcall("glTexParameterfv(..., GL_TEXTURE_BORDER_COLOR, ...)");
320 gl_tex->states[WINED3DTEXSTA_BORDERCOLOR] = state;
323 if (sampler_states[WINED3D_SAMP_MAG_FILTER] != gl_tex->states[WINED3DTEXSTA_MAGFILTER])
325 GLint gl_value;
327 state = sampler_states[WINED3D_SAMP_MAG_FILTER];
328 if (state > WINED3D_TEXF_ANISOTROPIC)
329 FIXME("Unrecognized or unsupported MAGFILTER* value %d.\n", state);
331 gl_value = wined3d_gl_mag_filter(texture->mag_lookup,
332 min(max(state, WINED3D_TEXF_POINT), WINED3D_TEXF_LINEAR));
333 TRACE("ValueMAG=%#x setting MAGFILTER to %#x.\n", state, gl_value);
334 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAG_FILTER, gl_value);
336 gl_tex->states[WINED3DTEXSTA_MAGFILTER] = state;
339 if ((sampler_states[WINED3D_SAMP_MIN_FILTER] != gl_tex->states[WINED3DTEXSTA_MINFILTER]
340 || sampler_states[WINED3D_SAMP_MIP_FILTER] != gl_tex->states[WINED3DTEXSTA_MIPFILTER]
341 || sampler_states[WINED3D_SAMP_MAX_MIP_LEVEL] != gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL]))
343 GLint gl_value;
345 gl_tex->states[WINED3DTEXSTA_MIPFILTER] = sampler_states[WINED3D_SAMP_MIP_FILTER];
346 gl_tex->states[WINED3DTEXSTA_MINFILTER] = sampler_states[WINED3D_SAMP_MIN_FILTER];
347 gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL] = sampler_states[WINED3D_SAMP_MAX_MIP_LEVEL];
349 if (gl_tex->states[WINED3DTEXSTA_MINFILTER] > WINED3D_TEXF_ANISOTROPIC
350 || gl_tex->states[WINED3DTEXSTA_MIPFILTER] > WINED3D_TEXF_ANISOTROPIC)
352 FIXME("Unrecognized or unsupported MIN_FILTER value %#x MIP_FILTER value %#x.\n",
353 gl_tex->states[WINED3DTEXSTA_MINFILTER],
354 gl_tex->states[WINED3DTEXSTA_MIPFILTER]);
356 gl_value = wined3d_gl_min_mip_filter(texture->min_mip_lookup,
357 min(max(sampler_states[WINED3D_SAMP_MIN_FILTER], WINED3D_TEXF_POINT), WINED3D_TEXF_LINEAR),
358 min(max(sampler_states[WINED3D_SAMP_MIP_FILTER], WINED3D_TEXF_NONE), WINED3D_TEXF_LINEAR));
360 TRACE("ValueMIN=%#x, ValueMIP=%#x, setting MINFILTER to %#x.\n",
361 sampler_states[WINED3D_SAMP_MIN_FILTER],
362 sampler_states[WINED3D_SAMP_MIP_FILTER], gl_value);
363 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MIN_FILTER, gl_value);
364 checkGLcall("glTexParameter GL_TEXTURE_MIN_FILTER, ...");
366 if (!cond_np2)
368 if (gl_tex->states[WINED3DTEXSTA_MIPFILTER] == WINED3D_TEXF_NONE)
369 gl_value = texture->lod;
370 else if (gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL] >= texture->level_count)
371 gl_value = texture->level_count - 1;
372 else if (gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL] < texture->lod)
373 /* texture->lod is already clamped in the setter. */
374 gl_value = texture->lod;
375 else
376 gl_value = gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL];
378 /* Note that WINED3D_SAMP_MAX_MIP_LEVEL specifies the largest mipmap
379 * (default 0), while GL_TEXTURE_MAX_LEVEL specifies the smallest
380 * mimap used (default 1000). So WINED3D_SAMP_MAX_MIP_LEVEL
381 * corresponds to GL_TEXTURE_BASE_LEVEL. */
382 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_BASE_LEVEL, gl_value);
386 if ((gl_tex->states[WINED3DTEXSTA_MAGFILTER] != WINED3D_TEXF_ANISOTROPIC
387 && gl_tex->states[WINED3DTEXSTA_MINFILTER] != WINED3D_TEXF_ANISOTROPIC
388 && gl_tex->states[WINED3DTEXSTA_MIPFILTER] != WINED3D_TEXF_ANISOTROPIC)
389 || cond_np2)
390 aniso = 1;
391 else
392 aniso = sampler_states[WINED3D_SAMP_MAX_ANISOTROPY];
394 if (gl_tex->states[WINED3DTEXSTA_MAXANISOTROPY] != aniso)
396 if (gl_info->supported[EXT_TEXTURE_FILTER_ANISOTROPIC])
398 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAX_ANISOTROPY_EXT, aniso);
399 checkGLcall("glTexParameteri(GL_TEXTURE_MAX_ANISOTROPY_EXT, aniso)");
401 else
403 WARN("Anisotropic filtering not supported.\n");
405 gl_tex->states[WINED3DTEXSTA_MAXANISOTROPY] = aniso;
408 /* These should always be the same unless EXT_texture_sRGB_decode is supported. */
409 if (sampler_states[WINED3D_SAMP_SRGB_TEXTURE] != gl_tex->states[WINED3DTEXSTA_SRGBTEXTURE])
411 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_SRGB_DECODE_EXT,
412 sampler_states[WINED3D_SAMP_SRGB_TEXTURE] ? GL_DECODE_EXT : GL_SKIP_DECODE_EXT);
413 checkGLcall("glTexParameteri(GL_TEXTURE_SRGB_DECODE_EXT)");
414 gl_tex->states[WINED3DTEXSTA_SRGBTEXTURE] = sampler_states[WINED3D_SAMP_SRGB_TEXTURE];
417 if (!(texture->resource.format->flags & WINED3DFMT_FLAG_SHADOW)
418 != !gl_tex->states[WINED3DTEXSTA_SHADOW])
420 if (texture->resource.format->flags & WINED3DFMT_FLAG_SHADOW)
422 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_DEPTH_TEXTURE_MODE_ARB, GL_LUMINANCE);
423 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE_ARB);
424 checkGLcall("glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE_ARB)");
425 gl_tex->states[WINED3DTEXSTA_SHADOW] = TRUE;
427 else
429 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_NONE);
430 checkGLcall("glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_NONE)");
431 gl_tex->states[WINED3DTEXSTA_SHADOW] = FALSE;
436 ULONG CDECL wined3d_texture_incref(struct wined3d_texture *texture)
438 ULONG refcount = InterlockedIncrement(&texture->resource.ref);
440 TRACE("%p increasing refcount to %u.\n", texture, refcount);
442 return refcount;
445 ULONG CDECL wined3d_texture_decref(struct wined3d_texture *texture)
447 ULONG refcount = InterlockedDecrement(&texture->resource.ref);
449 TRACE("%p decreasing refcount to %u.\n", texture, refcount);
451 if (!refcount)
453 wined3d_texture_cleanup(texture);
454 texture->resource.parent_ops->wined3d_object_destroyed(texture->resource.parent);
455 HeapFree(GetProcessHeap(), 0, texture);
458 return refcount;
461 struct wined3d_resource * CDECL wined3d_texture_get_resource(struct wined3d_texture *texture)
463 TRACE("texture %p.\n", texture);
465 return &texture->resource;
468 DWORD CDECL wined3d_texture_set_priority(struct wined3d_texture *texture, DWORD priority)
470 return resource_set_priority(&texture->resource, priority);
473 DWORD CDECL wined3d_texture_get_priority(const struct wined3d_texture *texture)
475 return resource_get_priority(&texture->resource);
478 void CDECL wined3d_texture_preload(struct wined3d_texture *texture)
480 struct wined3d_context *context;
481 context = context_acquire(texture->resource.device, NULL);
482 texture->texture_ops->texture_preload(texture, context, SRGB_ANY);
483 context_release(context);
486 void * CDECL wined3d_texture_get_parent(const struct wined3d_texture *texture)
488 TRACE("texture %p.\n", texture);
490 return texture->resource.parent;
493 DWORD CDECL wined3d_texture_set_lod(struct wined3d_texture *texture, DWORD lod)
495 DWORD old = texture->lod;
497 TRACE("texture %p, lod %u.\n", texture, lod);
499 /* The d3d9:texture test shows that SetLOD is ignored on non-managed
500 * textures. The call always returns 0, and GetLOD always returns 0. */
501 if (texture->resource.pool != WINED3D_POOL_MANAGED)
503 TRACE("Ignoring SetLOD on %s texture, returning 0.\n", debug_d3dpool(texture->resource.pool));
504 return 0;
507 if (lod >= texture->level_count)
508 lod = texture->level_count - 1;
510 if (texture->lod != lod)
512 texture->lod = lod;
514 texture->texture_rgb.states[WINED3DTEXSTA_MAXMIPLEVEL] = ~0U;
515 texture->texture_srgb.states[WINED3DTEXSTA_MAXMIPLEVEL] = ~0U;
516 if (texture->resource.bind_count)
517 device_invalidate_state(texture->resource.device, STATE_SAMPLER(texture->sampler));
520 return old;
523 DWORD CDECL wined3d_texture_get_lod(const struct wined3d_texture *texture)
525 TRACE("texture %p, returning %u.\n", texture, texture->lod);
527 return texture->lod;
530 DWORD CDECL wined3d_texture_get_level_count(const struct wined3d_texture *texture)
532 TRACE("texture %p, returning %u.\n", texture, texture->level_count);
534 return texture->level_count;
537 HRESULT CDECL wined3d_texture_set_autogen_filter_type(struct wined3d_texture *texture,
538 enum wined3d_texture_filter_type filter_type)
540 FIXME("texture %p, filter_type %s stub!\n", texture, debug_d3dtexturefiltertype(filter_type));
542 if (!(texture->resource.usage & WINED3DUSAGE_AUTOGENMIPMAP))
544 WARN("Texture doesn't have AUTOGENMIPMAP usage.\n");
545 return WINED3DERR_INVALIDCALL;
548 texture->filter_type = filter_type;
550 return WINED3D_OK;
553 enum wined3d_texture_filter_type CDECL wined3d_texture_get_autogen_filter_type(const struct wined3d_texture *texture)
555 TRACE("texture %p.\n", texture);
557 return texture->filter_type;
560 void CDECL wined3d_texture_generate_mipmaps(struct wined3d_texture *texture)
562 /* TODO: Implement filters using GL_SGI_generate_mipmaps. */
563 FIXME("texture %p stub!\n", texture);
566 struct wined3d_resource * CDECL wined3d_texture_get_sub_resource(struct wined3d_texture *texture,
567 UINT sub_resource_idx)
569 UINT sub_count = texture->level_count * texture->layer_count;
571 TRACE("texture %p, sub_resource_idx %u.\n", texture, sub_resource_idx);
573 if (sub_resource_idx >= sub_count)
575 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx, sub_count);
576 return NULL;
579 return texture->sub_resources[sub_resource_idx];
582 HRESULT CDECL wined3d_texture_add_dirty_region(struct wined3d_texture *texture,
583 UINT layer, const struct wined3d_box *dirty_region)
585 struct wined3d_resource *sub_resource;
587 TRACE("texture %p, layer %u, dirty_region %p.\n", texture, layer, dirty_region);
589 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, layer * texture->level_count)))
591 WARN("Failed to get sub-resource.\n");
592 return WINED3DERR_INVALIDCALL;
595 wined3d_texture_set_dirty(texture);
596 texture->texture_ops->texture_sub_resource_add_dirty_region(sub_resource, dirty_region);
598 return WINED3D_OK;
601 /* Context activation is done by the caller. */
602 static HRESULT texture2d_bind(struct wined3d_texture *texture,
603 struct wined3d_context *context, BOOL srgb)
605 const struct wined3d_gl_info *gl_info = context->gl_info;
606 BOOL set_gl_texture_desc;
607 HRESULT hr;
609 TRACE("texture %p, context %p, srgb %#x.\n", texture, context, srgb);
611 hr = wined3d_texture_bind(texture, context, srgb, &set_gl_texture_desc);
612 if (set_gl_texture_desc && SUCCEEDED(hr))
614 UINT sub_count = texture->level_count * texture->layer_count;
615 BOOL srgb_tex = !context->gl_info->supported[EXT_TEXTURE_SRGB_DECODE]
616 && (texture->flags & WINED3D_TEXTURE_IS_SRGB);
617 struct gl_texture *gl_tex;
618 UINT i;
620 gl_tex = wined3d_texture_get_gl_texture(texture, context->gl_info, srgb_tex);
622 for (i = 0; i < sub_count; ++i)
624 struct wined3d_surface *surface = surface_from_resource(texture->sub_resources[i]);
625 surface_set_texture_name(surface, gl_tex->name, srgb_tex);
628 /* Conditinal non power of two textures use a different clamping
629 * default. If we're using the GL_WINE_normalized_texrect partial
630 * driver emulation, we're dealing with a GL_TEXTURE_2D texture which
631 * has the address mode set to repeat - something that prevents us
632 * from hitting the accelerated codepath. Thus manually set the GL
633 * state. The same applies to filtering. Even if the texture has only
634 * one mip level, the default LINEAR_MIPMAP_LINEAR filter causes a SW
635 * fallback on macos. */
636 if (texture->flags & WINED3D_TEXTURE_COND_NP2)
638 GLenum target = texture->target;
640 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
641 checkGLcall("glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)");
642 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
643 checkGLcall("glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)");
644 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
645 checkGLcall("glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST)");
646 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
647 checkGLcall("glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST)");
648 gl_tex->states[WINED3DTEXSTA_ADDRESSU] = WINED3D_TADDRESS_CLAMP;
649 gl_tex->states[WINED3DTEXSTA_ADDRESSV] = WINED3D_TADDRESS_CLAMP;
650 gl_tex->states[WINED3DTEXSTA_MAGFILTER] = WINED3D_TEXF_POINT;
651 gl_tex->states[WINED3DTEXSTA_MINFILTER] = WINED3D_TEXF_POINT;
652 gl_tex->states[WINED3DTEXSTA_MIPFILTER] = WINED3D_TEXF_NONE;
656 return hr;
659 static BOOL texture_srgb_mode(const struct wined3d_texture *texture, enum WINED3DSRGB srgb)
661 switch (srgb)
663 case SRGB_RGB:
664 return FALSE;
666 case SRGB_SRGB:
667 return TRUE;
669 default:
670 return texture->flags & WINED3D_TEXTURE_IS_SRGB;
674 /* Context activation is done by the caller */
675 static void texture2d_preload(struct wined3d_texture *texture,
676 struct wined3d_context *context, enum WINED3DSRGB srgb)
678 UINT sub_count = texture->level_count * texture->layer_count;
679 const struct wined3d_gl_info *gl_info = context->gl_info;
680 BOOL srgb_mode;
681 DWORD flag;
682 UINT i;
684 TRACE("texture %p, srgb %#x.\n", texture, srgb);
686 srgb_mode = texture_srgb_mode(texture, srgb);
687 if (srgb_mode && !gl_info->supported[EXT_TEXTURE_SRGB_DECODE])
688 flag = WINED3D_TEXTURE_SRGB_VALID;
689 else
690 flag = WINED3D_TEXTURE_RGB_VALID;
692 if (texture->flags & flag)
694 TRACE("Texture %p not dirty, nothing to do.\n", texture);
695 return;
698 /* Reload the surfaces if the texture is marked dirty. */
699 for (i = 0; i < sub_count; ++i)
701 surface_load(surface_from_resource(texture->sub_resources[i]), srgb_mode);
703 texture->flags |= flag;
706 static void texture2d_sub_resource_add_dirty_region(struct wined3d_resource *sub_resource,
707 const struct wined3d_box *dirty_region)
709 surface_add_dirty_rect(surface_from_resource(sub_resource), dirty_region);
712 static void texture2d_sub_resource_cleanup(struct wined3d_resource *sub_resource)
714 struct wined3d_surface *surface = surface_from_resource(sub_resource);
716 /* Clean out the texture name we gave to the surface so that the
717 * surface doesn't try and release it. */
718 surface_set_texture_name(surface, 0, TRUE);
719 surface_set_texture_name(surface, 0, FALSE);
720 surface_set_texture_target(surface, 0, 0);
721 surface_set_container(surface, NULL);
722 wined3d_surface_decref(surface);
725 static void texture2d_unload(struct wined3d_resource *resource)
727 struct wined3d_texture *texture = wined3d_texture_from_resource(resource);
728 UINT sub_count = texture->level_count * texture->layer_count;
729 UINT i;
731 TRACE("texture %p.\n", texture);
733 for (i = 0; i < sub_count; ++i)
735 struct wined3d_resource *sub_resource = texture->sub_resources[i];
736 struct wined3d_surface *surface = surface_from_resource(sub_resource);
738 sub_resource->resource_ops->resource_unload(sub_resource);
739 surface_set_texture_name(surface, 0, FALSE); /* Delete RGB name */
740 surface_set_texture_name(surface, 0, TRUE); /* Delete sRGB name */
743 wined3d_texture_unload(texture);
746 static const struct wined3d_texture_ops texture2d_ops =
748 texture2d_bind,
749 texture2d_preload,
750 texture2d_sub_resource_add_dirty_region,
751 texture2d_sub_resource_cleanup,
754 static const struct wined3d_resource_ops texture2d_resource_ops =
756 texture2d_unload,
759 static HRESULT cubetexture_init(struct wined3d_texture *texture, const struct wined3d_resource_desc *desc,
760 UINT levels, DWORD surface_flags, struct wined3d_device *device, void *parent,
761 const struct wined3d_parent_ops *parent_ops)
763 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
764 struct wined3d_resource_desc surface_desc;
765 unsigned int i, j;
766 HRESULT hr;
768 /* TODO: It should only be possible to create textures for formats
769 * that are reported as supported. */
770 if (WINED3DFMT_UNKNOWN >= desc->format)
772 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture);
773 return WINED3DERR_INVALIDCALL;
776 if (!gl_info->supported[ARB_TEXTURE_CUBE_MAP] && desc->pool != WINED3D_POOL_SCRATCH)
778 WARN("(%p) : Tried to create not supported cube texture.\n", texture);
779 return WINED3DERR_INVALIDCALL;
782 /* Calculate levels for mip mapping */
783 if (desc->usage & WINED3DUSAGE_AUTOGENMIPMAP)
785 if (!gl_info->supported[SGIS_GENERATE_MIPMAP])
787 WARN("No mipmap generation support, returning D3DERR_INVALIDCALL.\n");
788 return WINED3DERR_INVALIDCALL;
791 if (levels > 1)
793 WARN("D3DUSAGE_AUTOGENMIPMAP is set, and level count > 1, returning D3DERR_INVALIDCALL.\n");
794 return WINED3DERR_INVALIDCALL;
797 levels = 1;
799 else if (!levels)
801 levels = wined3d_log2i(desc->width) + 1;
802 TRACE("Calculated levels = %u.\n", levels);
805 if (!gl_info->supported[ARB_TEXTURE_NON_POWER_OF_TWO])
807 UINT pow2_edge_length = 1;
808 while (pow2_edge_length < desc->width)
809 pow2_edge_length <<= 1;
811 if (desc->width != pow2_edge_length)
813 if (desc->pool == WINED3D_POOL_SCRATCH)
815 /* SCRATCH textures cannot be used for texturing */
816 WARN("Creating a scratch NPOT cube texture despite lack of HW support.\n");
818 else
820 WARN("Attempted to create a NPOT cube texture (edge length %u) without GL support.\n", desc->width);
821 return WINED3DERR_INVALIDCALL;
826 if (FAILED(hr = wined3d_texture_init(texture, &texture2d_ops, 6, levels,
827 desc, device, parent, parent_ops, &texture2d_resource_ops)))
829 WARN("Failed to initialize texture, returning %#x\n", hr);
830 return hr;
833 texture->pow2_matrix[0] = 1.0f;
834 texture->pow2_matrix[5] = 1.0f;
835 texture->pow2_matrix[10] = 1.0f;
836 texture->pow2_matrix[15] = 1.0f;
837 texture->target = GL_TEXTURE_CUBE_MAP_ARB;
839 /* Generate all the surfaces. */
840 surface_desc = *desc;
841 surface_desc.resource_type = WINED3D_RTYPE_SURFACE;
842 for (i = 0; i < texture->level_count; ++i)
844 /* Create the 6 faces. */
845 for (j = 0; j < 6; ++j)
847 static const GLenum cube_targets[6] =
849 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB,
850 GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB,
851 GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB,
852 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB,
853 GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB,
854 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB,
856 UINT idx = j * texture->level_count + i;
857 struct wined3d_surface *surface;
859 if (FAILED(hr = device->device_parent->ops->create_texture_surface(device->device_parent,
860 parent, &surface_desc, idx, surface_flags, &surface)))
862 FIXME("(%p) Failed to create surface, hr %#x.\n", texture, hr);
863 wined3d_texture_cleanup(texture);
864 return hr;
867 surface_set_container(surface, texture);
868 surface_set_texture_target(surface, cube_targets[j], i);
869 texture->sub_resources[idx] = &surface->resource;
870 TRACE("Created surface level %u @ %p.\n", i, surface);
872 surface_desc.width = max(1, surface_desc.width >> 1);
873 surface_desc.height = surface_desc.width;
876 return WINED3D_OK;
879 static HRESULT texture_init(struct wined3d_texture *texture, const struct wined3d_resource_desc *desc,
880 UINT levels, DWORD surface_flags, struct wined3d_device *device, void *parent,
881 const struct wined3d_parent_ops *parent_ops)
883 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
884 struct wined3d_resource_desc surface_desc;
885 UINT pow2_width, pow2_height;
886 unsigned int i;
887 HRESULT hr;
889 /* TODO: It should only be possible to create textures for formats
890 * that are reported as supported. */
891 if (WINED3DFMT_UNKNOWN >= desc->format)
893 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture);
894 return WINED3DERR_INVALIDCALL;
897 /* Non-power2 support. */
898 if (gl_info->supported[ARB_TEXTURE_NON_POWER_OF_TWO])
900 pow2_width = desc->width;
901 pow2_height = desc->height;
903 else
905 /* Find the nearest pow2 match. */
906 pow2_width = pow2_height = 1;
907 while (pow2_width < desc->width)
908 pow2_width <<= 1;
909 while (pow2_height < desc->height)
910 pow2_height <<= 1;
912 if (pow2_width != desc->width || pow2_height != desc->height)
914 /* levels == 0 returns an error as well */
915 if (levels != 1)
917 if (desc->pool == WINED3D_POOL_SCRATCH)
919 WARN("Creating a scratch mipmapped NPOT texture despite lack of HW support.\n");
921 else
923 WARN("Attempted to create a mipmapped NPOT texture without unconditional NPOT support.\n");
924 return WINED3DERR_INVALIDCALL;
930 /* Calculate levels for mip mapping. */
931 if (desc->usage & WINED3DUSAGE_AUTOGENMIPMAP)
933 if (!gl_info->supported[SGIS_GENERATE_MIPMAP])
935 WARN("No mipmap generation support, returning WINED3DERR_INVALIDCALL.\n");
936 return WINED3DERR_INVALIDCALL;
939 if (levels > 1)
941 WARN("D3DUSAGE_AUTOGENMIPMAP is set, and level count > 1, returning WINED3DERR_INVALIDCALL.\n");
942 return WINED3DERR_INVALIDCALL;
945 levels = 1;
947 else if (!levels)
949 levels = wined3d_log2i(max(desc->width, desc->height)) + 1;
950 TRACE("Calculated levels = %u.\n", levels);
953 if (FAILED(hr = wined3d_texture_init(texture, &texture2d_ops, 1, levels,
954 desc, device, parent, parent_ops, &texture2d_resource_ops)))
956 WARN("Failed to initialize texture, returning %#x.\n", hr);
957 return hr;
960 /* Precalculated scaling for 'faked' non power of two texture coords. */
961 if (gl_info->supported[WINED3D_GL_NORMALIZED_TEXRECT]
962 && (desc->width != pow2_width || desc->height != pow2_height))
964 texture->pow2_matrix[0] = 1.0f;
965 texture->pow2_matrix[5] = 1.0f;
966 texture->pow2_matrix[10] = 1.0f;
967 texture->pow2_matrix[15] = 1.0f;
968 texture->target = GL_TEXTURE_2D;
969 texture->flags |= WINED3D_TEXTURE_COND_NP2;
970 texture->min_mip_lookup = minMipLookup_noFilter;
972 else if (gl_info->supported[ARB_TEXTURE_RECTANGLE]
973 && (desc->width != pow2_width || desc->height != pow2_height))
975 texture->pow2_matrix[0] = (float)desc->width;
976 texture->pow2_matrix[5] = (float)desc->height;
977 texture->pow2_matrix[10] = 1.0f;
978 texture->pow2_matrix[15] = 1.0f;
979 texture->target = GL_TEXTURE_RECTANGLE_ARB;
980 texture->flags |= WINED3D_TEXTURE_COND_NP2;
981 texture->flags &= ~WINED3D_TEXTURE_POW2_MAT_IDENT;
983 if (texture->resource.format->flags & WINED3DFMT_FLAG_FILTERING)
984 texture->min_mip_lookup = minMipLookup_noMip;
985 else
986 texture->min_mip_lookup = minMipLookup_noFilter;
988 else
990 if ((desc->width != pow2_width) || (desc->height != pow2_height))
992 texture->pow2_matrix[0] = (((float)desc->width) / ((float)pow2_width));
993 texture->pow2_matrix[5] = (((float)desc->height) / ((float)pow2_height));
994 texture->flags &= ~WINED3D_TEXTURE_POW2_MAT_IDENT;
996 else
998 texture->pow2_matrix[0] = 1.0f;
999 texture->pow2_matrix[5] = 1.0f;
1002 texture->pow2_matrix[10] = 1.0f;
1003 texture->pow2_matrix[15] = 1.0f;
1004 texture->target = GL_TEXTURE_2D;
1006 TRACE("xf(%f) yf(%f)\n", texture->pow2_matrix[0], texture->pow2_matrix[5]);
1008 /* Generate all the surfaces. */
1009 surface_desc = *desc;
1010 surface_desc.resource_type = WINED3D_RTYPE_SURFACE;
1011 for (i = 0; i < texture->level_count; ++i)
1013 struct wined3d_surface *surface;
1015 /* Use the callback to create the texture surface. */
1016 if (FAILED(hr = device->device_parent->ops->create_texture_surface(device->device_parent,
1017 parent, &surface_desc, i, surface_flags, &surface)))
1019 FIXME("Failed to create surface %p, hr %#x\n", texture, hr);
1020 wined3d_texture_cleanup(texture);
1021 return hr;
1024 surface_set_container(surface, texture);
1025 surface_set_texture_target(surface, texture->target, i);
1026 texture->sub_resources[i] = &surface->resource;
1027 TRACE("Created surface level %u @ %p.\n", i, surface);
1028 /* Calculate the next mipmap level. */
1029 surface_desc.width = max(1, surface_desc.width >> 1);
1030 surface_desc.height = max(1, surface_desc.height >> 1);
1033 return WINED3D_OK;
1036 /* Context activation is done by the caller. */
1037 static HRESULT texture3d_bind(struct wined3d_texture *texture,
1038 struct wined3d_context *context, BOOL srgb)
1040 BOOL dummy;
1042 TRACE("texture %p, context %p, srgb %#x.\n", texture, context, srgb);
1044 return wined3d_texture_bind(texture, context, srgb, &dummy);
1047 /* Context activation is done by the caller. */
1048 static void texture3d_preload(struct wined3d_texture *texture,
1049 struct wined3d_context *context, enum WINED3DSRGB srgb)
1051 UINT sub_count = texture->level_count * texture->layer_count;
1052 const struct wined3d_gl_info *gl_info = context->gl_info;
1053 BOOL srgb_mode;
1054 DWORD flag;
1055 UINT i;
1057 TRACE("texture %p, srgb %#x.\n", texture, srgb);
1059 srgb_mode = texture_srgb_mode(texture, srgb);
1060 if (srgb_mode && !gl_info->supported[EXT_TEXTURE_SRGB_DECODE])
1061 flag = WINED3D_TEXTURE_SRGB_VALID;
1062 else
1063 flag = WINED3D_TEXTURE_RGB_VALID;
1065 if (texture->flags & flag)
1067 TRACE("Texture %p not dirty, nothing to do.\n", texture);
1068 return;
1071 /* Reload the surfaces if the texture is marked dirty. */
1072 for (i = 0; i < sub_count; ++i)
1074 wined3d_volume_load(volume_from_resource(texture->sub_resources[i]), context, srgb_mode);
1076 texture->flags |= flag;
1079 static void texture3d_sub_resource_add_dirty_region(struct wined3d_resource *sub_resource,
1080 const struct wined3d_box *dirty_region)
1084 static void texture3d_sub_resource_cleanup(struct wined3d_resource *sub_resource)
1086 struct wined3d_volume *volume = volume_from_resource(sub_resource);
1088 /* Cleanup the container. */
1089 volume_set_container(volume, NULL);
1090 wined3d_volume_decref(volume);
1093 static void texture3d_unload(struct wined3d_resource *resource)
1095 struct wined3d_texture *texture = wined3d_texture_from_resource(resource);
1096 UINT i;
1098 TRACE("texture %p.\n", texture);
1100 for (i = 0; i < texture->level_count; ++i)
1102 struct wined3d_resource *sub_resource = texture->sub_resources[i];
1103 sub_resource->resource_ops->resource_unload(sub_resource);
1106 wined3d_texture_unload(texture);
1109 static const struct wined3d_texture_ops texture3d_ops =
1111 texture3d_bind,
1112 texture3d_preload,
1113 texture3d_sub_resource_add_dirty_region,
1114 texture3d_sub_resource_cleanup,
1117 static const struct wined3d_resource_ops texture3d_resource_ops =
1119 texture3d_unload,
1122 static HRESULT volumetexture_init(struct wined3d_texture *texture, const struct wined3d_resource_desc *desc,
1123 UINT levels, struct wined3d_device *device, void *parent, const struct wined3d_parent_ops *parent_ops)
1125 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
1126 UINT tmp_w, tmp_h, tmp_d;
1127 unsigned int i;
1128 HRESULT hr;
1130 /* TODO: It should only be possible to create textures for formats
1131 * that are reported as supported. */
1132 if (WINED3DFMT_UNKNOWN >= desc->format)
1134 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture);
1135 return WINED3DERR_INVALIDCALL;
1138 if (!gl_info->supported[EXT_TEXTURE3D])
1140 WARN("(%p) : Texture cannot be created - no volume texture support.\n", texture);
1141 return WINED3DERR_INVALIDCALL;
1144 /* Calculate levels for mip mapping. */
1145 if (desc->usage & WINED3DUSAGE_AUTOGENMIPMAP)
1147 if (!gl_info->supported[SGIS_GENERATE_MIPMAP])
1149 WARN("No mipmap generation support, returning D3DERR_INVALIDCALL.\n");
1150 return WINED3DERR_INVALIDCALL;
1153 if (levels > 1)
1155 WARN("D3DUSAGE_AUTOGENMIPMAP is set, and level count > 1, returning D3DERR_INVALIDCALL.\n");
1156 return WINED3DERR_INVALIDCALL;
1159 levels = 1;
1161 else if (!levels)
1163 levels = wined3d_log2i(max(max(desc->width, desc->height), desc->depth)) + 1;
1164 TRACE("Calculated levels = %u.\n", levels);
1167 if (!gl_info->supported[ARB_TEXTURE_NON_POWER_OF_TWO])
1169 UINT pow2_w, pow2_h, pow2_d;
1170 pow2_w = 1;
1171 while (pow2_w < desc->width)
1172 pow2_w <<= 1;
1173 pow2_h = 1;
1174 while (pow2_h < desc->height)
1175 pow2_h <<= 1;
1176 pow2_d = 1;
1177 while (pow2_d < desc->depth)
1178 pow2_d <<= 1;
1180 if (pow2_w != desc->width || pow2_h != desc->height || pow2_d != desc->depth)
1182 if (desc->pool == WINED3D_POOL_SCRATCH)
1184 WARN("Creating a scratch NPOT volume texture despite lack of HW support.\n");
1186 else
1188 WARN("Attempted to create a NPOT volume texture (%u, %u, %u) without GL support.\n",
1189 desc->width, desc->height, desc->depth);
1190 return WINED3DERR_INVALIDCALL;
1195 if (FAILED(hr = wined3d_texture_init(texture, &texture3d_ops, 1, levels,
1196 desc, device, parent, parent_ops, &texture3d_resource_ops)))
1198 WARN("Failed to initialize texture, returning %#x.\n", hr);
1199 return hr;
1202 texture->pow2_matrix[0] = 1.0f;
1203 texture->pow2_matrix[5] = 1.0f;
1204 texture->pow2_matrix[10] = 1.0f;
1205 texture->pow2_matrix[15] = 1.0f;
1206 texture->target = GL_TEXTURE_3D;
1208 /* Generate all the surfaces. */
1209 tmp_w = desc->width;
1210 tmp_h = desc->height;
1211 tmp_d = desc->depth;
1213 for (i = 0; i < texture->level_count; ++i)
1215 struct wined3d_volume *volume;
1217 /* Create the volume. */
1218 hr = device->device_parent->ops->create_volume(device->device_parent, parent,
1219 tmp_w, tmp_h, tmp_d, i, desc->format, desc->pool, desc->usage, &volume);
1220 if (FAILED(hr))
1222 ERR("Creating a volume for the volume texture failed, hr %#x.\n", hr);
1223 wined3d_texture_cleanup(texture);
1224 return hr;
1227 /* Set its container to this texture. */
1228 volume_set_container(volume, texture);
1229 texture->sub_resources[i] = &volume->resource;
1231 /* Calculate the next mipmap level. */
1232 tmp_w = max(1, tmp_w >> 1);
1233 tmp_h = max(1, tmp_h >> 1);
1234 tmp_d = max(1, tmp_d >> 1);
1237 return WINED3D_OK;
1240 HRESULT CDECL wined3d_texture_create_2d(struct wined3d_device *device, const struct wined3d_resource_desc *desc,
1241 UINT level_count, DWORD surface_flags, void *parent, const struct wined3d_parent_ops *parent_ops,
1242 struct wined3d_texture **texture)
1244 struct wined3d_texture *object;
1245 HRESULT hr;
1247 TRACE("device %p, desc %p, level_count %u, surface_flags %#x, parent %p, parent_ops %p, texture %p.\n",
1248 device, desc, level_count, surface_flags, parent, parent_ops, texture);
1250 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1251 if (!object)
1253 *texture = NULL;
1254 return WINED3DERR_OUTOFVIDEOMEMORY;
1257 if (FAILED(hr = texture_init(object, desc, level_count, surface_flags, device, parent, parent_ops)))
1259 WARN("Failed to initialize texture, returning %#x.\n", hr);
1260 HeapFree(GetProcessHeap(), 0, object);
1261 *texture = NULL;
1262 return hr;
1265 TRACE("Created texture %p.\n", object);
1266 *texture = object;
1268 return WINED3D_OK;
1271 HRESULT CDECL wined3d_texture_create_3d(struct wined3d_device *device, const struct wined3d_resource_desc *desc,
1272 UINT level_count, void *parent, const struct wined3d_parent_ops *parent_ops, struct wined3d_texture **texture)
1274 struct wined3d_texture *object;
1275 HRESULT hr;
1277 TRACE("device %p, desc %p, level_count %u, parent %p, parent_ops %p, texture %p.\n",
1278 device, desc, level_count, parent, parent_ops, texture);
1280 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1281 if (!object)
1283 *texture = NULL;
1284 return WINED3DERR_OUTOFVIDEOMEMORY;
1287 if (FAILED(hr = volumetexture_init(object, desc, level_count, device, parent, parent_ops)))
1289 WARN("Failed to initialize volumetexture, returning %#x\n", hr);
1290 HeapFree(GetProcessHeap(), 0, object);
1291 *texture = NULL;
1292 return hr;
1295 TRACE("Created texture %p.\n", object);
1296 *texture = object;
1298 return WINED3D_OK;
1301 HRESULT CDECL wined3d_texture_create_cube(struct wined3d_device *device, const struct wined3d_resource_desc *desc,
1302 UINT level_count, DWORD surface_flags, void *parent, const struct wined3d_parent_ops *parent_ops,
1303 struct wined3d_texture **texture)
1305 struct wined3d_texture *object;
1306 HRESULT hr;
1308 TRACE("device %p, desc %p, level_count %u, surface_flags %#x, parent %p, parent_ops %p, texture %p.\n",
1309 device, desc, level_count, surface_flags, parent, parent_ops, texture);
1311 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1312 if (!object)
1314 *texture = NULL;
1315 return WINED3DERR_OUTOFVIDEOMEMORY;
1318 if (FAILED(hr = cubetexture_init(object, desc, level_count, surface_flags, device, parent, parent_ops)))
1320 WARN("Failed to initialize cubetexture, returning %#x\n", hr);
1321 HeapFree(GetProcessHeap(), 0, object);
1322 *texture = NULL;
1323 return hr;
1326 TRACE("Created texture %p.\n", object);
1327 *texture = object;
1329 return WINED3D_OK;