windowscodecs: Test pixel format of the loaded TIFF image.
[wine.git] / dlls / wined3d / texture.c
blob9d14beb637f7aa26e74a60c3b0525a0e071ae1da
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
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, 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)
34 HRESULT hr;
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))
41 WARN("Failed to initialize resource, returning %#x\n", hr);
42 return hr;
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)
50 ERR("Failed to allocate sub-resource array.\n");
51 resource_cleanup(&texture->resource);
52 return E_OUTOFMEMORY;
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;
63 if (texture->resource.format->flags & WINED3DFMT_FLAG_FILTERING)
65 texture->min_mip_lookup = minMipLookup;
66 texture->mag_lookup = magLookup;
68 else
70 texture->min_mip_lookup = minMipLookup_noFilter;
71 texture->mag_lookup = magLookup_noFilter;
74 return WINED3D_OK;
77 /* A GL context is provided by the caller */
78 static void gltexture_delete(struct gl_texture *tex)
80 ENTER_GL();
81 glDeleteTextures(1, &tex->name);
82 LEAVE_GL();
83 tex->name = 0;
86 static void wined3d_texture_unload(struct wined3d_texture *texture)
88 struct wined3d_device *device = texture->resource.device;
89 struct wined3d_context *context = NULL;
91 if (texture->texture_rgb.name || texture->texture_srgb.name)
93 context = context_acquire(device, NULL);
96 if (texture->texture_rgb.name)
97 gltexture_delete(&texture->texture_rgb);
99 if (texture->texture_srgb.name)
100 gltexture_delete(&texture->texture_srgb);
102 if (context) context_release(context);
104 wined3d_texture_set_dirty(texture, TRUE);
106 resource_unload(&texture->resource);
109 static void wined3d_texture_cleanup(struct wined3d_texture *texture)
111 UINT sub_count = texture->level_count * texture->layer_count;
112 UINT i;
114 TRACE("texture %p.\n", texture);
116 for (i = 0; i < sub_count; ++i)
118 struct wined3d_resource *sub_resource = texture->sub_resources[i];
120 if (sub_resource)
121 texture->texture_ops->texture_sub_resource_cleanup(sub_resource);
124 wined3d_texture_unload(texture);
125 HeapFree(GetProcessHeap(), 0, texture->sub_resources);
126 resource_cleanup(&texture->resource);
129 void wined3d_texture_set_dirty(struct wined3d_texture *texture, BOOL dirty)
131 texture->texture_rgb.dirty = dirty;
132 texture->texture_srgb.dirty = dirty;
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)
139 struct gl_texture *gl_tex;
140 BOOL new_texture = FALSE;
141 HRESULT hr = WINED3D_OK;
142 GLenum target;
144 TRACE("texture %p, context %p, srgb %#x, set_surface_desc %p.\n", texture, context, srgb, set_surface_desc);
146 /* sRGB mode cache for preload() calls outside drawprim. */
147 if (srgb)
148 texture->flags |= WINED3D_TEXTURE_IS_SRGB;
149 else
150 texture->flags &= ~WINED3D_TEXTURE_IS_SRGB;
152 gl_tex = wined3d_texture_get_gl_texture(texture, context->gl_info, srgb);
153 target = texture->target;
155 ENTER_GL();
156 /* Generate a texture name if we don't already have one. */
157 if (!gl_tex->name)
159 *set_surface_desc = TRUE;
160 glGenTextures(1, &gl_tex->name);
161 checkGLcall("glGenTextures");
162 TRACE("Generated texture %d.\n", gl_tex->name);
163 if (texture->resource.pool == WINED3D_POOL_DEFAULT)
165 /* Tell OpenGL to try and keep this texture in video ram (well mostly). */
166 GLclampf tmp = 0.9f;
167 glPrioritizeTextures(1, &gl_tex->name, &tmp);
169 /* Initialise the state of the texture object to the OpenGL defaults,
170 * not the D3D defaults. */
171 gl_tex->states[WINED3DTEXSTA_ADDRESSU] = WINED3D_TADDRESS_WRAP;
172 gl_tex->states[WINED3DTEXSTA_ADDRESSV] = WINED3D_TADDRESS_WRAP;
173 gl_tex->states[WINED3DTEXSTA_ADDRESSW] = WINED3D_TADDRESS_WRAP;
174 gl_tex->states[WINED3DTEXSTA_BORDERCOLOR] = 0;
175 gl_tex->states[WINED3DTEXSTA_MAGFILTER] = WINED3D_TEXF_LINEAR;
176 gl_tex->states[WINED3DTEXSTA_MINFILTER] = WINED3D_TEXF_POINT; /* GL_NEAREST_MIPMAP_LINEAR */
177 gl_tex->states[WINED3DTEXSTA_MIPFILTER] = WINED3D_TEXF_LINEAR; /* GL_NEAREST_MIPMAP_LINEAR */
178 gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL] = 0;
179 gl_tex->states[WINED3DTEXSTA_MAXANISOTROPY] = 1;
180 if (context->gl_info->supported[EXT_TEXTURE_SRGB_DECODE])
181 gl_tex->states[WINED3DTEXSTA_SRGBTEXTURE] = TRUE;
182 else
183 gl_tex->states[WINED3DTEXSTA_SRGBTEXTURE] = srgb;
184 gl_tex->states[WINED3DTEXSTA_SHADOW] = FALSE;
185 wined3d_texture_set_dirty(texture, TRUE);
186 new_texture = TRUE;
188 if (texture->resource.usage & WINED3DUSAGE_AUTOGENMIPMAP)
190 /* This means double binding the texture at creation, but keeps
191 * the code simpler all in all, and the run-time path free from
192 * additional checks. */
193 context_bind_texture(context, target, gl_tex->name);
194 glTexParameteri(target, GL_GENERATE_MIPMAP_SGIS, GL_TRUE);
195 checkGLcall("glTexParameteri(target, GL_GENERATE_MIPMAP_SGIS, GL_TRUE)");
198 else
200 *set_surface_desc = FALSE;
203 if (gl_tex->name)
205 context_bind_texture(context, target, gl_tex->name);
206 if (new_texture)
208 /* For a new texture we have to set the texture levels after
209 * binding the texture. Beware that texture rectangles do not
210 * support mipmapping, but set the maxmiplevel if we're relying
211 * on the partial GL_ARB_texture_non_power_of_two emulation with
212 * texture rectangles. (I.e., do not care about cond_np2 here,
213 * just look for GL_TEXTURE_RECTANGLE_ARB.) */
214 if (target != GL_TEXTURE_RECTANGLE_ARB)
216 TRACE("Setting GL_TEXTURE_MAX_LEVEL to %u.\n", texture->level_count - 1);
217 glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, texture->level_count - 1);
218 checkGLcall("glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, texture->level_count)");
220 if (target == GL_TEXTURE_CUBE_MAP_ARB)
222 /* Cubemaps are always set to clamp, regardless of the sampler state. */
223 glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
224 glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
225 glTexParameteri(target, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
229 else
231 ERR("This texture doesn't have an OpenGL texture assigned to it.\n");
232 hr = WINED3DERR_INVALIDCALL;
235 LEAVE_GL();
236 return hr;
239 /* GL locking is done by the caller */
240 static void apply_wrap(const struct wined3d_gl_info *gl_info, GLenum target,
241 enum wined3d_texture_address d3d_wrap, GLenum param, BOOL cond_np2)
243 GLint gl_wrap;
245 if (d3d_wrap < WINED3D_TADDRESS_WRAP || d3d_wrap > WINED3D_TADDRESS_MIRROR_ONCE)
247 FIXME("Unrecognized or unsupported texture address mode %#x.\n", d3d_wrap);
248 return;
251 /* Cubemaps are always set to clamp, regardless of the sampler state. */
252 if (target == GL_TEXTURE_CUBE_MAP_ARB
253 || (cond_np2 && d3d_wrap == WINED3D_TADDRESS_WRAP))
254 gl_wrap = GL_CLAMP_TO_EDGE;
255 else
256 gl_wrap = gl_info->wrap_lookup[d3d_wrap - WINED3D_TADDRESS_WRAP];
258 TRACE("Setting param %#x to %#x for target %#x.\n", param, gl_wrap, target);
259 glTexParameteri(target, param, gl_wrap);
260 checkGLcall("glTexParameteri(target, param, gl_wrap)");
263 /* GL locking is done by the caller (state handler) */
264 void wined3d_texture_apply_state_changes(struct wined3d_texture *texture,
265 const DWORD sampler_states[WINED3D_HIGHEST_SAMPLER_STATE + 1],
266 const struct wined3d_gl_info *gl_info)
268 BOOL cond_np2 = texture->flags & WINED3D_TEXTURE_COND_NP2;
269 GLenum target = texture->target;
270 struct gl_texture *gl_tex;
271 DWORD state;
272 DWORD aniso;
274 TRACE("texture %p, sampler_states %p.\n", texture, sampler_states);
276 gl_tex = wined3d_texture_get_gl_texture(texture, gl_info,
277 texture->flags & WINED3D_TEXTURE_IS_SRGB);
279 /* This function relies on the correct texture being bound and loaded. */
281 if (sampler_states[WINED3D_SAMP_ADDRESS_U] != gl_tex->states[WINED3DTEXSTA_ADDRESSU])
283 state = sampler_states[WINED3D_SAMP_ADDRESS_U];
284 apply_wrap(gl_info, target, state, GL_TEXTURE_WRAP_S, cond_np2);
285 gl_tex->states[WINED3DTEXSTA_ADDRESSU] = state;
288 if (sampler_states[WINED3D_SAMP_ADDRESS_V] != gl_tex->states[WINED3DTEXSTA_ADDRESSV])
290 state = sampler_states[WINED3D_SAMP_ADDRESS_V];
291 apply_wrap(gl_info, target, state, GL_TEXTURE_WRAP_T, cond_np2);
292 gl_tex->states[WINED3DTEXSTA_ADDRESSV] = state;
295 if (sampler_states[WINED3D_SAMP_ADDRESS_W] != gl_tex->states[WINED3DTEXSTA_ADDRESSW])
297 state = sampler_states[WINED3D_SAMP_ADDRESS_W];
298 apply_wrap(gl_info, target, state, GL_TEXTURE_WRAP_R, cond_np2);
299 gl_tex->states[WINED3DTEXSTA_ADDRESSW] = state;
302 if (sampler_states[WINED3D_SAMP_BORDER_COLOR] != gl_tex->states[WINED3DTEXSTA_BORDERCOLOR])
304 float col[4];
306 state = sampler_states[WINED3D_SAMP_BORDER_COLOR];
307 D3DCOLORTOGLFLOAT4(state, col);
308 TRACE("Setting border color for %#x to %#x.\n", target, state);
309 glTexParameterfv(target, GL_TEXTURE_BORDER_COLOR, &col[0]);
310 checkGLcall("glTexParameterfv(..., GL_TEXTURE_BORDER_COLOR, ...)");
311 gl_tex->states[WINED3DTEXSTA_BORDERCOLOR] = state;
314 if (sampler_states[WINED3D_SAMP_MAG_FILTER] != gl_tex->states[WINED3DTEXSTA_MAGFILTER])
316 GLint gl_value;
318 state = sampler_states[WINED3D_SAMP_MAG_FILTER];
319 if (state > WINED3D_TEXF_ANISOTROPIC)
320 FIXME("Unrecognized or unsupported MAGFILTER* value %d.\n", state);
322 gl_value = wined3d_gl_mag_filter(texture->mag_lookup,
323 min(max(state, WINED3D_TEXF_POINT), WINED3D_TEXF_LINEAR));
324 TRACE("ValueMAG=%#x setting MAGFILTER to %#x.\n", state, gl_value);
325 glTexParameteri(target, GL_TEXTURE_MAG_FILTER, gl_value);
327 gl_tex->states[WINED3DTEXSTA_MAGFILTER] = state;
330 if ((sampler_states[WINED3D_SAMP_MIN_FILTER] != gl_tex->states[WINED3DTEXSTA_MINFILTER]
331 || sampler_states[WINED3D_SAMP_MIP_FILTER] != gl_tex->states[WINED3DTEXSTA_MIPFILTER]
332 || sampler_states[WINED3D_SAMP_MAX_MIP_LEVEL] != gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL]))
334 GLint gl_value;
336 gl_tex->states[WINED3DTEXSTA_MIPFILTER] = sampler_states[WINED3D_SAMP_MIP_FILTER];
337 gl_tex->states[WINED3DTEXSTA_MINFILTER] = sampler_states[WINED3D_SAMP_MIN_FILTER];
338 gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL] = sampler_states[WINED3D_SAMP_MAX_MIP_LEVEL];
340 if (gl_tex->states[WINED3DTEXSTA_MINFILTER] > WINED3D_TEXF_ANISOTROPIC
341 || gl_tex->states[WINED3DTEXSTA_MIPFILTER] > WINED3D_TEXF_ANISOTROPIC)
343 FIXME("Unrecognized or unsupported MIN_FILTER value %#x MIP_FILTER value %#x.\n",
344 gl_tex->states[WINED3DTEXSTA_MINFILTER],
345 gl_tex->states[WINED3DTEXSTA_MIPFILTER]);
347 gl_value = wined3d_gl_min_mip_filter(texture->min_mip_lookup,
348 min(max(sampler_states[WINED3D_SAMP_MIN_FILTER], WINED3D_TEXF_POINT), WINED3D_TEXF_LINEAR),
349 min(max(sampler_states[WINED3D_SAMP_MIP_FILTER], WINED3D_TEXF_NONE), WINED3D_TEXF_LINEAR));
351 TRACE("ValueMIN=%#x, ValueMIP=%#x, setting MINFILTER to %#x.\n",
352 sampler_states[WINED3D_SAMP_MIN_FILTER],
353 sampler_states[WINED3D_SAMP_MIP_FILTER], gl_value);
354 glTexParameteri(target, GL_TEXTURE_MIN_FILTER, gl_value);
355 checkGLcall("glTexParameter GL_TEXTURE_MIN_FILTER, ...");
357 if (!cond_np2)
359 if (gl_tex->states[WINED3DTEXSTA_MIPFILTER] == WINED3D_TEXF_NONE)
360 gl_value = texture->lod;
361 else if (gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL] >= texture->level_count)
362 gl_value = texture->level_count - 1;
363 else if (gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL] < texture->lod)
364 /* texture->lod is already clamped in the setter. */
365 gl_value = texture->lod;
366 else
367 gl_value = gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL];
369 /* Note that WINED3D_SAMP_MAX_MIP_LEVEL specifies the largest mipmap
370 * (default 0), while GL_TEXTURE_MAX_LEVEL specifies the smallest
371 * mimap used (default 1000). So WINED3D_SAMP_MAX_MIP_LEVEL
372 * corresponds to GL_TEXTURE_BASE_LEVEL. */
373 glTexParameteri(target, GL_TEXTURE_BASE_LEVEL, gl_value);
377 if ((gl_tex->states[WINED3DTEXSTA_MAGFILTER] != WINED3D_TEXF_ANISOTROPIC
378 && gl_tex->states[WINED3DTEXSTA_MINFILTER] != WINED3D_TEXF_ANISOTROPIC
379 && gl_tex->states[WINED3DTEXSTA_MIPFILTER] != WINED3D_TEXF_ANISOTROPIC)
380 || cond_np2)
381 aniso = 1;
382 else
383 aniso = sampler_states[WINED3D_SAMP_MAX_ANISOTROPY];
385 if (gl_tex->states[WINED3DTEXSTA_MAXANISOTROPY] != aniso)
387 if (gl_info->supported[EXT_TEXTURE_FILTER_ANISOTROPIC])
389 glTexParameteri(target, GL_TEXTURE_MAX_ANISOTROPY_EXT, aniso);
390 checkGLcall("glTexParameteri(GL_TEXTURE_MAX_ANISOTROPY_EXT, aniso)");
392 else
394 WARN("Anisotropic filtering not supported.\n");
396 gl_tex->states[WINED3DTEXSTA_MAXANISOTROPY] = aniso;
399 /* These should always be the same unless EXT_texture_sRGB_decode is supported. */
400 if (sampler_states[WINED3D_SAMP_SRGB_TEXTURE] != gl_tex->states[WINED3DTEXSTA_SRGBTEXTURE])
402 glTexParameteri(target, GL_TEXTURE_SRGB_DECODE_EXT,
403 sampler_states[WINED3D_SAMP_SRGB_TEXTURE] ? GL_DECODE_EXT : GL_SKIP_DECODE_EXT);
404 checkGLcall("glTexParameteri(GL_TEXTURE_SRGB_DECODE_EXT)");
405 gl_tex->states[WINED3DTEXSTA_SRGBTEXTURE] = sampler_states[WINED3D_SAMP_SRGB_TEXTURE];
408 if (!(texture->resource.format->flags & WINED3DFMT_FLAG_SHADOW)
409 != !gl_tex->states[WINED3DTEXSTA_SHADOW])
411 if (texture->resource.format->flags & WINED3DFMT_FLAG_SHADOW)
413 glTexParameteri(target, GL_DEPTH_TEXTURE_MODE_ARB, GL_LUMINANCE);
414 glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE_ARB);
415 checkGLcall("glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE_ARB)");
416 gl_tex->states[WINED3DTEXSTA_SHADOW] = TRUE;
418 else
420 glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_NONE);
421 checkGLcall("glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_NONE)");
422 gl_tex->states[WINED3DTEXSTA_SHADOW] = FALSE;
427 ULONG CDECL wined3d_texture_incref(struct wined3d_texture *texture)
429 ULONG refcount = InterlockedIncrement(&texture->resource.ref);
431 TRACE("%p increasing refcount to %u.\n", texture, refcount);
433 return refcount;
436 /* Do not call while under the GL lock. */
437 ULONG CDECL wined3d_texture_decref(struct wined3d_texture *texture)
439 ULONG refcount = InterlockedDecrement(&texture->resource.ref);
441 TRACE("%p decreasing refcount to %u.\n", texture, refcount);
443 if (!refcount)
445 wined3d_texture_cleanup(texture);
446 texture->resource.parent_ops->wined3d_object_destroyed(texture->resource.parent);
447 HeapFree(GetProcessHeap(), 0, texture);
450 return refcount;
453 struct wined3d_resource * CDECL wined3d_texture_get_resource(struct wined3d_texture *texture)
455 TRACE("texture %p.\n", texture);
457 return &texture->resource;
460 DWORD CDECL wined3d_texture_set_priority(struct wined3d_texture *texture, DWORD priority)
462 return resource_set_priority(&texture->resource, priority);
465 DWORD CDECL wined3d_texture_get_priority(const struct wined3d_texture *texture)
467 return resource_get_priority(&texture->resource);
470 /* Do not call while under the GL lock. */
471 void CDECL wined3d_texture_preload(struct wined3d_texture *texture)
473 texture->texture_ops->texture_preload(texture, SRGB_ANY);
476 void * CDECL wined3d_texture_get_parent(const struct wined3d_texture *texture)
478 TRACE("texture %p.\n", texture);
480 return texture->resource.parent;
483 DWORD CDECL wined3d_texture_set_lod(struct wined3d_texture *texture, DWORD lod)
485 DWORD old = texture->lod;
487 TRACE("texture %p, lod %u.\n", texture, lod);
489 /* The d3d9:texture test shows that SetLOD is ignored on non-managed
490 * textures. The call always returns 0, and GetLOD always returns 0. */
491 if (texture->resource.pool != WINED3D_POOL_MANAGED)
493 TRACE("Ignoring SetLOD on %s texture, returning 0.\n", debug_d3dpool(texture->resource.pool));
494 return 0;
497 if (lod >= texture->level_count)
498 lod = texture->level_count - 1;
500 if (texture->lod != lod)
502 texture->lod = lod;
504 texture->texture_rgb.states[WINED3DTEXSTA_MAXMIPLEVEL] = ~0U;
505 texture->texture_srgb.states[WINED3DTEXSTA_MAXMIPLEVEL] = ~0U;
506 if (texture->resource.bind_count)
507 device_invalidate_state(texture->resource.device, STATE_SAMPLER(texture->sampler));
510 return old;
513 DWORD CDECL wined3d_texture_get_lod(const struct wined3d_texture *texture)
515 TRACE("texture %p, returning %u.\n", texture, texture->lod);
517 return texture->lod;
520 DWORD CDECL wined3d_texture_get_level_count(const struct wined3d_texture *texture)
522 TRACE("texture %p, returning %u.\n", texture, texture->level_count);
524 return texture->level_count;
527 HRESULT CDECL wined3d_texture_set_autogen_filter_type(struct wined3d_texture *texture,
528 enum wined3d_texture_filter_type filter_type)
530 FIXME("texture %p, filter_type %s stub!\n", texture, debug_d3dtexturefiltertype(filter_type));
532 if (!(texture->resource.usage & WINED3DUSAGE_AUTOGENMIPMAP))
534 WARN("Texture doesn't have AUTOGENMIPMAP usage.\n");
535 return WINED3DERR_INVALIDCALL;
538 texture->filter_type = filter_type;
540 return WINED3D_OK;
543 enum wined3d_texture_filter_type CDECL wined3d_texture_get_autogen_filter_type(const struct wined3d_texture *texture)
545 TRACE("texture %p.\n", texture);
547 return texture->filter_type;
550 void CDECL wined3d_texture_generate_mipmaps(struct wined3d_texture *texture)
552 /* TODO: Implement filters using GL_SGI_generate_mipmaps. */
553 FIXME("texture %p stub!\n", texture);
556 struct wined3d_resource * CDECL wined3d_texture_get_sub_resource(struct wined3d_texture *texture,
557 UINT sub_resource_idx)
559 UINT sub_count = texture->level_count * texture->layer_count;
561 TRACE("texture %p, sub_resource_idx %u.\n", texture, sub_resource_idx);
563 if (sub_resource_idx >= sub_count)
565 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx, sub_count);
566 return NULL;
569 return texture->sub_resources[sub_resource_idx];
572 HRESULT CDECL wined3d_texture_add_dirty_region(struct wined3d_texture *texture,
573 UINT layer, const struct wined3d_box *dirty_region)
575 struct wined3d_resource *sub_resource;
577 TRACE("texture %p, layer %u, dirty_region %p.\n", texture, layer, dirty_region);
579 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, layer * texture->level_count)))
581 WARN("Failed to get sub-resource.\n");
582 return WINED3DERR_INVALIDCALL;
585 wined3d_texture_set_dirty(texture, TRUE);
586 texture->texture_ops->texture_sub_resource_add_dirty_region(sub_resource, dirty_region);
588 return WINED3D_OK;
591 /* Context activation is done by the caller. */
592 static HRESULT texture2d_bind(struct wined3d_texture *texture,
593 struct wined3d_context *context, BOOL srgb)
595 BOOL set_gl_texture_desc;
596 HRESULT hr;
598 TRACE("texture %p, context %p, srgb %#x.\n", texture, context, srgb);
600 hr = wined3d_texture_bind(texture, context, srgb, &set_gl_texture_desc);
601 if (set_gl_texture_desc && SUCCEEDED(hr))
603 UINT sub_count = texture->level_count * texture->layer_count;
604 BOOL srgb_tex = !context->gl_info->supported[EXT_TEXTURE_SRGB_DECODE]
605 && (texture->flags & WINED3D_TEXTURE_IS_SRGB);
606 struct gl_texture *gl_tex;
607 UINT i;
609 gl_tex = wined3d_texture_get_gl_texture(texture, context->gl_info, srgb_tex);
611 for (i = 0; i < sub_count; ++i)
613 struct wined3d_surface *surface = surface_from_resource(texture->sub_resources[i]);
614 surface_set_texture_name(surface, gl_tex->name, srgb_tex);
617 /* Conditinal non power of two textures use a different clamping
618 * default. If we're using the GL_WINE_normalized_texrect partial
619 * driver emulation, we're dealing with a GL_TEXTURE_2D texture which
620 * has the address mode set to repeat - something that prevents us
621 * from hitting the accelerated codepath. Thus manually set the GL
622 * state. The same applies to filtering. Even if the texture has only
623 * one mip level, the default LINEAR_MIPMAP_LINEAR filter causes a SW
624 * fallback on macos. */
625 if (texture->flags & WINED3D_TEXTURE_COND_NP2)
627 GLenum target = texture->target;
629 ENTER_GL();
630 glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
631 checkGLcall("glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)");
632 glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
633 checkGLcall("glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)");
634 glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
635 checkGLcall("glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST)");
636 glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
637 checkGLcall("glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST)");
638 LEAVE_GL();
639 gl_tex->states[WINED3DTEXSTA_ADDRESSU] = WINED3D_TADDRESS_CLAMP;
640 gl_tex->states[WINED3DTEXSTA_ADDRESSV] = WINED3D_TADDRESS_CLAMP;
641 gl_tex->states[WINED3DTEXSTA_MAGFILTER] = WINED3D_TEXF_POINT;
642 gl_tex->states[WINED3DTEXSTA_MINFILTER] = WINED3D_TEXF_POINT;
643 gl_tex->states[WINED3DTEXSTA_MIPFILTER] = WINED3D_TEXF_NONE;
647 return hr;
650 static BOOL texture_srgb_mode(const struct wined3d_texture *texture, enum WINED3DSRGB srgb)
652 switch (srgb)
654 case SRGB_RGB:
655 return FALSE;
657 case SRGB_SRGB:
658 return TRUE;
660 default:
661 return texture->flags & WINED3D_TEXTURE_IS_SRGB;
665 /* Do not call while under the GL lock. */
666 static void texture2d_preload(struct wined3d_texture *texture, enum WINED3DSRGB srgb)
668 UINT sub_count = texture->level_count * texture->layer_count;
669 struct wined3d_device *device = texture->resource.device;
670 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
671 struct wined3d_context *context = NULL;
672 struct gl_texture *gl_tex;
673 BOOL srgb_mode;
674 UINT i;
676 TRACE("texture %p, srgb %#x.\n", texture, srgb);
678 srgb_mode = texture_srgb_mode(texture, srgb);
679 gl_tex = wined3d_texture_get_gl_texture(texture, gl_info, srgb_mode);
681 if (!device->isInDraw)
683 /* No danger of recursive calls, context_acquire() sets isInDraw to TRUE
684 * when loading offscreen render targets into the texture. */
685 context = context_acquire(device, NULL);
688 if (gl_tex->dirty)
690 /* Reload the surfaces if the texture is marked dirty. */
691 for (i = 0; i < sub_count; ++i)
693 surface_load(surface_from_resource(texture->sub_resources[i]), srgb_mode);
696 else
698 TRACE("Texture %p not dirty, nothing to do.\n", texture);
701 /* No longer dirty. */
702 gl_tex->dirty = FALSE;
704 if (context) context_release(context);
707 static void texture2d_sub_resource_add_dirty_region(struct wined3d_resource *sub_resource,
708 const struct wined3d_box *dirty_region)
710 surface_add_dirty_rect(surface_from_resource(sub_resource), dirty_region);
713 static void texture2d_sub_resource_cleanup(struct wined3d_resource *sub_resource)
715 struct wined3d_surface *surface = surface_from_resource(sub_resource);
717 /* Clean out the texture name we gave to the surface so that the
718 * surface doesn't try and release it. */
719 surface_set_texture_name(surface, 0, TRUE);
720 surface_set_texture_name(surface, 0, FALSE);
721 surface_set_texture_target(surface, 0);
722 surface_set_container(surface, WINED3D_CONTAINER_NONE, NULL);
723 wined3d_surface_decref(surface);
726 /* Do not call while under the GL lock. */
727 static void texture2d_unload(struct wined3d_resource *resource)
729 struct wined3d_texture *texture = wined3d_texture_from_resource(resource);
730 UINT sub_count = texture->level_count * texture->layer_count;
731 UINT i;
733 TRACE("texture %p.\n", texture);
735 for (i = 0; i < sub_count; ++i)
737 struct wined3d_resource *sub_resource = texture->sub_resources[i];
738 struct wined3d_surface *surface = surface_from_resource(sub_resource);
740 sub_resource->resource_ops->resource_unload(sub_resource);
741 surface_set_texture_name(surface, 0, FALSE); /* Delete RGB name */
742 surface_set_texture_name(surface, 0, TRUE); /* Delete sRGB name */
745 wined3d_texture_unload(texture);
748 static const struct wined3d_texture_ops texture2d_ops =
750 texture2d_bind,
751 texture2d_preload,
752 texture2d_sub_resource_add_dirty_region,
753 texture2d_sub_resource_cleanup,
756 static const struct wined3d_resource_ops texture2d_resource_ops =
758 texture2d_unload,
761 static HRESULT cubetexture_init(struct wined3d_texture *texture, UINT edge_length, UINT levels,
762 struct wined3d_device *device, DWORD usage, enum wined3d_format_id format_id, enum wined3d_pool pool,
763 void *parent, const struct wined3d_parent_ops *parent_ops)
765 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
766 const struct wined3d_format *format = wined3d_get_format(gl_info, format_id);
767 UINT pow2_edge_length;
768 unsigned int i, j;
769 UINT tmp_w;
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 >= format_id)
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] && 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 (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(edge_length) + 1;
806 TRACE("Calculated levels = %u.\n", levels);
809 hr = wined3d_texture_init(texture, &texture2d_ops, 6, levels,
810 WINED3D_RTYPE_CUBE_TEXTURE, device, usage, format, pool,
811 parent, parent_ops, &texture2d_resource_ops);
812 if (FAILED(hr))
814 WARN("Failed to initialize texture, returning %#x\n", hr);
815 return hr;
818 /* Find the nearest pow2 match. */
819 pow2_edge_length = 1;
820 while (pow2_edge_length < edge_length) pow2_edge_length <<= 1;
822 if (gl_info->supported[ARB_TEXTURE_NON_POWER_OF_TWO] || (edge_length == pow2_edge_length))
824 /* Precalculated scaling for 'faked' non power of two texture coords. */
825 texture->pow2_matrix[0] = 1.0f;
826 texture->pow2_matrix[5] = 1.0f;
827 texture->pow2_matrix[10] = 1.0f;
828 texture->pow2_matrix[15] = 1.0f;
830 else
832 /* Precalculated scaling for 'faked' non power of two texture coords. */
833 texture->pow2_matrix[0] = ((float)edge_length) / ((float)pow2_edge_length);
834 texture->pow2_matrix[5] = ((float)edge_length) / ((float)pow2_edge_length);
835 texture->pow2_matrix[10] = ((float)edge_length) / ((float)pow2_edge_length);
836 texture->pow2_matrix[15] = 1.0f;
837 texture->flags &= ~WINED3D_TEXTURE_POW2_MAT_IDENT;
839 texture->target = GL_TEXTURE_CUBE_MAP_ARB;
841 /* Generate all the surfaces. */
842 tmp_w = edge_length;
843 for (i = 0; i < texture->level_count; ++i)
845 /* Create the 6 faces. */
846 for (j = 0; j < 6; ++j)
848 static const GLenum cube_targets[6] =
850 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB,
851 GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB,
852 GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB,
853 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB,
854 GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB,
855 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB,
857 UINT idx = j * texture->level_count + i;
858 struct wined3d_surface *surface;
860 if (FAILED(hr = device->device_parent->ops->create_texture_surface(device->device_parent, parent,
861 tmp_w, tmp_w, format_id, usage, pool, i /* Level */, j, &surface)))
863 FIXME("(%p) Failed to create surface, hr %#x.\n", texture, hr);
864 wined3d_texture_cleanup(texture);
865 return hr;
868 surface_set_container(surface, WINED3D_CONTAINER_TEXTURE, texture);
869 surface_set_texture_target(surface, cube_targets[j]);
870 texture->sub_resources[idx] = &surface->resource;
871 TRACE("Created surface level %u @ %p.\n", i, surface);
873 tmp_w = max(1, tmp_w >> 1);
876 return WINED3D_OK;
879 static HRESULT texture_init(struct wined3d_texture *texture, UINT width, UINT height, UINT levels,
880 struct wined3d_device *device, DWORD usage, enum wined3d_format_id format_id, enum wined3d_pool pool,
881 void *parent, const struct wined3d_parent_ops *parent_ops)
883 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
884 const struct wined3d_format *format = wined3d_get_format(gl_info, format_id);
885 UINT pow2_width, pow2_height;
886 UINT tmp_w, tmp_h;
887 unsigned int i;
888 HRESULT hr;
890 /* TODO: It should only be possible to create textures for formats
891 * that are reported as supported. */
892 if (WINED3DFMT_UNKNOWN >= format_id)
894 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture);
895 return WINED3DERR_INVALIDCALL;
898 /* Non-power2 support. */
899 if (gl_info->supported[ARB_TEXTURE_NON_POWER_OF_TWO])
901 pow2_width = width;
902 pow2_height = height;
904 else
906 /* Find the nearest pow2 match. */
907 pow2_width = pow2_height = 1;
908 while (pow2_width < width) pow2_width <<= 1;
909 while (pow2_height < height) pow2_height <<= 1;
911 if (pow2_width != width || pow2_height != height)
913 if (levels > 1)
915 WARN("Attempted to create a mipmapped np2 texture without unconditional np2 support.\n");
916 return WINED3DERR_INVALIDCALL;
918 levels = 1;
922 /* Calculate levels for mip mapping. */
923 if (usage & WINED3DUSAGE_AUTOGENMIPMAP)
925 if (!gl_info->supported[SGIS_GENERATE_MIPMAP])
927 WARN("No mipmap generation support, returning WINED3DERR_INVALIDCALL.\n");
928 return WINED3DERR_INVALIDCALL;
931 if (levels > 1)
933 WARN("D3DUSAGE_AUTOGENMIPMAP is set, and level count > 1, returning WINED3DERR_INVALIDCALL.\n");
934 return WINED3DERR_INVALIDCALL;
937 levels = 1;
939 else if (!levels)
941 levels = wined3d_log2i(max(width, height)) + 1;
942 TRACE("Calculated levels = %u.\n", levels);
945 hr = wined3d_texture_init(texture, &texture2d_ops, 1, levels,
946 WINED3D_RTYPE_TEXTURE, device, usage, format, pool,
947 parent, parent_ops, &texture2d_resource_ops);
948 if (FAILED(hr))
950 WARN("Failed to initialize texture, returning %#x.\n", hr);
951 return hr;
954 /* Precalculated scaling for 'faked' non power of two texture coords.
955 * Second also don't use ARB_TEXTURE_RECTANGLE in case the surface format is P8 and EXT_PALETTED_TEXTURE
956 * is used in combination with texture uploads (RTL_READTEX). The reason is that EXT_PALETTED_TEXTURE
957 * doesn't work in combination with ARB_TEXTURE_RECTANGLE. */
958 if (gl_info->supported[WINED3D_GL_NORMALIZED_TEXRECT] && (width != pow2_width || height != pow2_height))
960 texture->pow2_matrix[0] = 1.0f;
961 texture->pow2_matrix[5] = 1.0f;
962 texture->pow2_matrix[10] = 1.0f;
963 texture->pow2_matrix[15] = 1.0f;
964 texture->target = GL_TEXTURE_2D;
965 texture->flags |= WINED3D_TEXTURE_COND_NP2;
966 texture->min_mip_lookup = minMipLookup_noFilter;
968 else if (gl_info->supported[ARB_TEXTURE_RECTANGLE] && (width != pow2_width || height != pow2_height)
969 && !(format->id == WINED3DFMT_P8_UINT && gl_info->supported[EXT_PALETTED_TEXTURE]
970 && wined3d_settings.rendertargetlock_mode == RTL_READTEX))
972 if (width != 1 || height != 1)
973 texture->flags &= ~WINED3D_TEXTURE_POW2_MAT_IDENT;
975 texture->pow2_matrix[0] = (float)width;
976 texture->pow2_matrix[5] = (float)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;
982 if (texture->resource.format->flags & WINED3DFMT_FLAG_FILTERING)
983 texture->min_mip_lookup = minMipLookup_noMip;
984 else
985 texture->min_mip_lookup = minMipLookup_noFilter;
987 else
989 if ((width != pow2_width) || (height != pow2_height))
991 texture->pow2_matrix[0] = (((float)width) / ((float)pow2_width));
992 texture->pow2_matrix[5] = (((float)height) / ((float)pow2_height));
993 texture->flags &= ~WINED3D_TEXTURE_POW2_MAT_IDENT;
995 else
997 texture->pow2_matrix[0] = 1.0f;
998 texture->pow2_matrix[5] = 1.0f;
1001 texture->pow2_matrix[10] = 1.0f;
1002 texture->pow2_matrix[15] = 1.0f;
1003 texture->target = GL_TEXTURE_2D;
1005 TRACE("xf(%f) yf(%f)\n", texture->pow2_matrix[0], texture->pow2_matrix[5]);
1007 /* Generate all the surfaces. */
1008 tmp_w = width;
1009 tmp_h = height;
1010 for (i = 0; i < texture->level_count; ++i)
1012 struct wined3d_surface *surface;
1014 /* Use the callback to create the texture surface. */
1015 if (FAILED(hr = device->device_parent->ops->create_texture_surface(device->device_parent, parent,
1016 tmp_w, tmp_h, format->id, usage, pool, i, 0, &surface)))
1018 FIXME("Failed to create surface %p, hr %#x\n", texture, hr);
1019 wined3d_texture_cleanup(texture);
1020 return hr;
1023 surface_set_container(surface, WINED3D_CONTAINER_TEXTURE, texture);
1024 surface_set_texture_target(surface, texture->target);
1025 texture->sub_resources[i] = &surface->resource;
1026 TRACE("Created surface level %u @ %p.\n", i, surface);
1027 /* Calculate the next mipmap level. */
1028 tmp_w = max(1, tmp_w >> 1);
1029 tmp_h = max(1, tmp_h >> 1);
1032 return WINED3D_OK;
1035 /* Context activation is done by the caller. */
1036 static HRESULT texture3d_bind(struct wined3d_texture *texture,
1037 struct wined3d_context *context, BOOL srgb)
1039 BOOL dummy;
1041 TRACE("texture %p, context %p, srgb %#x.\n", texture, context, srgb);
1043 return wined3d_texture_bind(texture, context, srgb, &dummy);
1046 /* Do not call while under the GL lock. */
1047 static void texture3d_preload(struct wined3d_texture *texture, enum WINED3DSRGB srgb)
1049 struct wined3d_device *device = texture->resource.device;
1050 struct wined3d_context *context;
1051 BOOL srgb_was_toggled = FALSE;
1052 unsigned int i;
1054 TRACE("texture %p, srgb %#x.\n", texture, srgb);
1056 /* TODO: Use already acquired context when possible. */
1057 context = context_acquire(device, NULL);
1058 if (texture->resource.bind_count > 0)
1060 BOOL texture_srgb = texture->flags & WINED3D_TEXTURE_IS_SRGB;
1061 BOOL sampler_srgb = texture_srgb_mode(texture, srgb);
1062 srgb_was_toggled = !texture_srgb != !sampler_srgb;
1064 if (srgb_was_toggled)
1066 if (sampler_srgb)
1067 texture->flags |= WINED3D_TEXTURE_IS_SRGB;
1068 else
1069 texture->flags &= ~WINED3D_TEXTURE_IS_SRGB;
1073 /* If the texture is marked dirty or the sRGB sampler setting has changed
1074 * since the last load then reload the volumes. */
1075 if (texture->texture_rgb.dirty)
1077 for (i = 0; i < texture->level_count; ++i)
1079 volume_load(volume_from_resource(texture->sub_resources[i]), context, i,
1080 texture->flags & WINED3D_TEXTURE_IS_SRGB);
1083 else if (srgb_was_toggled)
1085 for (i = 0; i < texture->level_count; ++i)
1087 struct wined3d_volume *volume = volume_from_resource(texture->sub_resources[i]);
1088 volume_add_dirty_box(volume, NULL);
1089 volume_load(volume, context, i, texture->flags & WINED3D_TEXTURE_IS_SRGB);
1092 else
1094 TRACE("Texture %p not dirty, nothing to do.\n", texture);
1097 context_release(context);
1099 /* No longer dirty */
1100 texture->texture_rgb.dirty = FALSE;
1103 static void texture3d_sub_resource_add_dirty_region(struct wined3d_resource *sub_resource,
1104 const struct wined3d_box *dirty_region)
1106 volume_add_dirty_box(volume_from_resource(sub_resource), dirty_region);
1109 static void texture3d_sub_resource_cleanup(struct wined3d_resource *sub_resource)
1111 struct wined3d_volume *volume = volume_from_resource(sub_resource);
1113 /* Cleanup the container. */
1114 volume_set_container(volume, NULL);
1115 wined3d_volume_decref(volume);
1118 /* Do not call while under the GL lock. */
1119 static void texture3d_unload(struct wined3d_resource *resource)
1121 struct wined3d_texture *texture = wined3d_texture_from_resource(resource);
1122 UINT i;
1124 TRACE("texture %p.\n", texture);
1126 for (i = 0; i < texture->level_count; ++i)
1128 struct wined3d_resource *sub_resource = texture->sub_resources[i];
1129 sub_resource->resource_ops->resource_unload(sub_resource);
1132 wined3d_texture_unload(texture);
1135 static const struct wined3d_texture_ops texture3d_ops =
1137 texture3d_bind,
1138 texture3d_preload,
1139 texture3d_sub_resource_add_dirty_region,
1140 texture3d_sub_resource_cleanup,
1143 static const struct wined3d_resource_ops texture3d_resource_ops =
1145 texture3d_unload,
1148 static HRESULT volumetexture_init(struct wined3d_texture *texture, UINT width, UINT height,
1149 UINT depth, UINT levels, struct wined3d_device *device, DWORD usage, enum wined3d_format_id format_id,
1150 enum wined3d_pool pool, void *parent, const struct wined3d_parent_ops *parent_ops)
1152 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
1153 const struct wined3d_format *format = wined3d_get_format(gl_info, format_id);
1154 UINT tmp_w, tmp_h, tmp_d;
1155 unsigned int i;
1156 HRESULT hr;
1158 /* TODO: It should only be possible to create textures for formats
1159 * that are reported as supported. */
1160 if (WINED3DFMT_UNKNOWN >= format_id)
1162 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture);
1163 return WINED3DERR_INVALIDCALL;
1166 if (!gl_info->supported[EXT_TEXTURE3D])
1168 WARN("(%p) : Texture cannot be created - no volume texture support.\n", texture);
1169 return WINED3DERR_INVALIDCALL;
1172 /* Calculate levels for mip mapping. */
1173 if (usage & WINED3DUSAGE_AUTOGENMIPMAP)
1175 if (!gl_info->supported[SGIS_GENERATE_MIPMAP])
1177 WARN("No mipmap generation support, returning D3DERR_INVALIDCALL.\n");
1178 return WINED3DERR_INVALIDCALL;
1181 if (levels > 1)
1183 WARN("D3DUSAGE_AUTOGENMIPMAP is set, and level count > 1, returning D3DERR_INVALIDCALL.\n");
1184 return WINED3DERR_INVALIDCALL;
1187 levels = 1;
1189 else if (!levels)
1191 levels = wined3d_log2i(max(max(width, height), depth)) + 1;
1192 TRACE("Calculated levels = %u.\n", levels);
1195 hr = wined3d_texture_init(texture, &texture3d_ops, 1, levels,
1196 WINED3D_RTYPE_VOLUME_TEXTURE, device, usage, format, pool,
1197 parent, parent_ops, &texture3d_resource_ops);
1198 if (FAILED(hr))
1200 WARN("Failed to initialize texture, returning %#x.\n", hr);
1201 return hr;
1204 /* Is NP2 support for volumes needed? */
1205 texture->pow2_matrix[0] = 1.0f;
1206 texture->pow2_matrix[5] = 1.0f;
1207 texture->pow2_matrix[10] = 1.0f;
1208 texture->pow2_matrix[15] = 1.0f;
1209 texture->target = GL_TEXTURE_3D;
1211 /* Generate all the surfaces. */
1212 tmp_w = width;
1213 tmp_h = height;
1214 tmp_d = depth;
1216 for (i = 0; i < texture->level_count; ++i)
1218 struct wined3d_volume *volume;
1220 /* Create the volume. */
1221 hr = device->device_parent->ops->create_volume(device->device_parent, parent,
1222 tmp_w, tmp_h, tmp_d, format_id, pool, usage, &volume);
1223 if (FAILED(hr))
1225 ERR("Creating a volume for the volume texture failed, hr %#x.\n", hr);
1226 wined3d_texture_cleanup(texture);
1227 return hr;
1230 /* Set its container to this texture. */
1231 volume_set_container(volume, texture);
1232 texture->sub_resources[i] = &volume->resource;
1234 /* Calculate the next mipmap level. */
1235 tmp_w = max(1, tmp_w >> 1);
1236 tmp_h = max(1, tmp_h >> 1);
1237 tmp_d = max(1, tmp_d >> 1);
1240 return WINED3D_OK;
1243 HRESULT CDECL wined3d_texture_create_2d(struct wined3d_device *device, UINT width, UINT height,
1244 UINT level_count, DWORD usage, enum wined3d_format_id format_id, enum wined3d_pool pool, void *parent,
1245 const struct wined3d_parent_ops *parent_ops, struct wined3d_texture **texture)
1247 struct wined3d_texture *object;
1248 HRESULT hr;
1250 TRACE("device %p, width %u, height %u, level_count %u, usage %#x\n",
1251 device, width, height, level_count, usage);
1252 TRACE("format %s, pool %#x, parent %p, parent_ops %p, texture %p.\n",
1253 debug_d3dformat(format_id), pool, parent, parent_ops, texture);
1255 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1256 if (!object)
1258 ERR("Out of memory.\n");
1259 *texture = NULL;
1260 return WINED3DERR_OUTOFVIDEOMEMORY;
1263 hr = texture_init(object, width, height, level_count,
1264 device, usage, format_id, pool, parent, parent_ops);
1265 if (FAILED(hr))
1267 WARN("Failed to initialize texture, returning %#x.\n", hr);
1268 HeapFree(GetProcessHeap(), 0, object);
1269 *texture = NULL;
1270 return hr;
1273 TRACE("Created texture %p.\n", object);
1274 *texture = object;
1276 return WINED3D_OK;
1279 HRESULT CDECL wined3d_texture_create_3d(struct wined3d_device *device, UINT width, UINT height, UINT depth,
1280 UINT level_count, DWORD usage, enum wined3d_format_id format_id, enum wined3d_pool pool, void *parent,
1281 const struct wined3d_parent_ops *parent_ops, struct wined3d_texture **texture)
1283 struct wined3d_texture *object;
1284 HRESULT hr;
1286 TRACE("device %p, width %u, height %u, depth %u, level_count %u, usage %#x\n",
1287 device, width, height, depth, level_count, usage);
1288 TRACE("format %s, pool %#x, parent %p, parent_ops %p, texture %p.\n",
1289 debug_d3dformat(format_id), pool, parent, parent_ops, texture);
1291 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1292 if (!object)
1294 ERR("Out of memory\n");
1295 *texture = NULL;
1296 return WINED3DERR_OUTOFVIDEOMEMORY;
1299 hr = volumetexture_init(object, width, height, depth, level_count,
1300 device, usage, format_id, pool, parent, parent_ops);
1301 if (FAILED(hr))
1303 WARN("Failed to initialize volumetexture, returning %#x\n", hr);
1304 HeapFree(GetProcessHeap(), 0, object);
1305 *texture = NULL;
1306 return hr;
1309 TRACE("Created texture %p.\n", object);
1310 *texture = object;
1312 return WINED3D_OK;
1315 HRESULT CDECL wined3d_texture_create_cube(struct wined3d_device *device, UINT edge_length,
1316 UINT level_count, DWORD usage, enum wined3d_format_id format_id, enum wined3d_pool pool, void *parent,
1317 const struct wined3d_parent_ops *parent_ops, struct wined3d_texture **texture)
1319 struct wined3d_texture *object;
1320 HRESULT hr;
1322 TRACE("device %p, edge_length %u, level_count %u, usage %#x\n",
1323 device, edge_length, level_count, usage);
1324 TRACE("format %s, pool %#x, parent %p, parent_ops %p, texture %p.\n",
1325 debug_d3dformat(format_id), pool, parent, parent_ops, texture);
1327 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1328 if (!object)
1330 ERR("Out of memory\n");
1331 *texture = NULL;
1332 return WINED3DERR_OUTOFVIDEOMEMORY;
1335 hr = cubetexture_init(object, edge_length, level_count,
1336 device, usage, format_id, pool, parent, parent_ops);
1337 if (FAILED(hr))
1339 WARN("Failed to initialize cubetexture, returning %#x\n", hr);
1340 HeapFree(GetProcessHeap(), 0, object);
1341 *texture = NULL;
1342 return hr;
1345 TRACE("Created texture %p.\n", object);
1346 *texture = object;
1348 return WINED3D_OK;