wined3d: Remove wined3d_buffer_set/get_priority.
[wine.git] / dlls / wined3d / texture.c
blobb840b89eb20a76ace99b788eba0c92cd0fbbe852
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);
28 WINE_DECLARE_DEBUG_CHANNEL(winediag);
30 static HRESULT wined3d_texture_init(struct wined3d_texture *texture, const struct wined3d_texture_ops *texture_ops,
31 UINT layer_count, UINT level_count, const struct wined3d_resource_desc *desc, struct wined3d_device *device,
32 void *parent, const struct wined3d_parent_ops *parent_ops, const struct wined3d_resource_ops *resource_ops)
34 const struct wined3d_format *format = wined3d_get_format(&device->adapter->gl_info, desc->format);
35 HRESULT hr;
37 TRACE("texture %p, texture_ops %p, layer_count %u, level_count %u, resource_type %s, format %s, "
38 "multisample_type %#x, multisample_quality %#x, usage %s, pool %s, width %u, height %u, depth %u, "
39 "device %p, parent %p, parent_ops %p, resource_ops %p.\n",
40 texture, texture_ops, layer_count, level_count, debug_d3dresourcetype(desc->resource_type),
41 debug_d3dformat(desc->format), desc->multisample_type, desc->multisample_quality,
42 debug_d3dusage(desc->usage), debug_d3dpool(desc->pool), desc->width, desc->height, desc->depth,
43 device, parent, parent_ops, resource_ops);
45 if ((format->flags & (WINED3DFMT_FLAG_BLOCKS | WINED3DFMT_FLAG_BLOCKS_NO_VERIFY)) == WINED3DFMT_FLAG_BLOCKS)
47 UINT width_mask = format->block_width - 1;
48 UINT height_mask = format->block_height - 1;
49 if (desc->width & width_mask || desc->height & height_mask)
50 return WINED3DERR_INVALIDCALL;
53 if (FAILED(hr = resource_init(&texture->resource, device, desc->resource_type, format,
54 desc->multisample_type, desc->multisample_quality, desc->usage, desc->pool,
55 desc->width, desc->height, desc->depth, 0, parent, parent_ops, resource_ops)))
57 static unsigned int once;
59 if ((desc->format == WINED3DFMT_DXT1 || desc->format == WINED3DFMT_DXT2 || desc->format == WINED3DFMT_DXT3
60 || desc->format == WINED3DFMT_DXT4 || desc->format == WINED3DFMT_DXT5)
61 && !(format->flags & WINED3DFMT_FLAG_TEXTURE) && !once++)
62 ERR_(winediag)("The application tried to create a DXTn texture, but the driver does not support them.\n");
64 WARN("Failed to initialize resource, returning %#x\n", hr);
65 return hr;
68 texture->texture_ops = texture_ops;
69 texture->sub_resources = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
70 level_count * layer_count * sizeof(*texture->sub_resources));
71 if (!texture->sub_resources)
73 ERR("Failed to allocate sub-resource array.\n");
74 resource_cleanup(&texture->resource);
75 return E_OUTOFMEMORY;
78 texture->layer_count = layer_count;
79 texture->level_count = level_count;
80 texture->filter_type = (desc->usage & WINED3DUSAGE_AUTOGENMIPMAP) ? WINED3D_TEXF_LINEAR : WINED3D_TEXF_NONE;
81 texture->lod = 0;
82 texture->flags = WINED3D_TEXTURE_POW2_MAT_IDENT;
84 if (texture->resource.format->flags & WINED3DFMT_FLAG_FILTERING)
86 texture->min_mip_lookup = minMipLookup;
87 texture->mag_lookup = magLookup;
89 else
91 texture->min_mip_lookup = minMipLookup_noFilter;
92 texture->mag_lookup = magLookup_noFilter;
95 return WINED3D_OK;
98 /* A GL context is provided by the caller */
99 static void gltexture_delete(const struct wined3d_gl_info *gl_info, struct gl_texture *tex)
101 gl_info->gl_ops.gl.p_glDeleteTextures(1, &tex->name);
102 tex->name = 0;
105 static void wined3d_texture_unload_gl_texture(struct wined3d_texture *texture)
107 struct wined3d_device *device = texture->resource.device;
108 struct wined3d_context *context = NULL;
110 if (texture->texture_rgb.name || texture->texture_srgb.name)
112 context = context_acquire(device, NULL);
115 if (texture->texture_rgb.name)
116 gltexture_delete(context->gl_info, &texture->texture_rgb);
118 if (texture->texture_srgb.name)
119 gltexture_delete(context->gl_info, &texture->texture_srgb);
121 if (context) context_release(context);
123 wined3d_texture_set_dirty(texture);
125 resource_unload(&texture->resource);
128 static void wined3d_texture_cleanup(struct wined3d_texture *texture)
130 UINT sub_count = texture->level_count * texture->layer_count;
131 UINT i;
133 TRACE("texture %p.\n", texture);
135 for (i = 0; i < sub_count; ++i)
137 struct wined3d_resource *sub_resource = texture->sub_resources[i];
139 if (sub_resource)
140 texture->texture_ops->texture_sub_resource_cleanup(sub_resource);
143 wined3d_texture_unload_gl_texture(texture);
144 HeapFree(GetProcessHeap(), 0, texture->sub_resources);
145 resource_cleanup(&texture->resource);
148 void wined3d_texture_set_dirty(struct wined3d_texture *texture)
150 texture->flags &= ~(WINED3D_TEXTURE_RGB_VALID | WINED3D_TEXTURE_SRGB_VALID);
153 /* Context activation is done by the caller. */
154 void wined3d_texture_bind(struct wined3d_texture *texture,
155 struct wined3d_context *context, BOOL srgb)
157 const struct wined3d_gl_info *gl_info = context->gl_info;
158 struct gl_texture *gl_tex;
159 GLenum target;
161 TRACE("texture %p, context %p, srgb %#x.\n", texture, context, srgb);
163 if (gl_info->supported[EXT_TEXTURE_SRGB_DECODE])
164 srgb = FALSE;
166 /* sRGB mode cache for preload() calls outside drawprim. */
167 if (srgb)
168 texture->flags |= WINED3D_TEXTURE_IS_SRGB;
169 else
170 texture->flags &= ~WINED3D_TEXTURE_IS_SRGB;
172 gl_tex = wined3d_texture_get_gl_texture(texture, srgb);
173 target = texture->target;
175 if (gl_tex->name)
177 context_bind_texture(context, target, gl_tex->name);
178 return;
181 gl_info->gl_ops.gl.p_glGenTextures(1, &gl_tex->name);
182 checkGLcall("glGenTextures");
183 TRACE("Generated texture %d.\n", gl_tex->name);
185 if (!gl_tex->name)
187 ERR("Failed to generate a texture name.\n");
188 return;
191 if (texture->resource.pool == WINED3D_POOL_DEFAULT)
193 /* Tell OpenGL to try and keep this texture in video ram (well mostly). */
194 GLclampf tmp = 0.9f;
195 gl_info->gl_ops.gl.p_glPrioritizeTextures(1, &gl_tex->name, &tmp);
198 /* Initialise the state of the texture object to the OpenGL defaults, not
199 * the wined3d defaults. */
200 gl_tex->states[WINED3DTEXSTA_ADDRESSU] = WINED3D_TADDRESS_WRAP;
201 gl_tex->states[WINED3DTEXSTA_ADDRESSV] = WINED3D_TADDRESS_WRAP;
202 gl_tex->states[WINED3DTEXSTA_ADDRESSW] = WINED3D_TADDRESS_WRAP;
203 gl_tex->states[WINED3DTEXSTA_BORDERCOLOR] = 0;
204 gl_tex->states[WINED3DTEXSTA_MAGFILTER] = WINED3D_TEXF_LINEAR;
205 gl_tex->states[WINED3DTEXSTA_MINFILTER] = WINED3D_TEXF_POINT; /* GL_NEAREST_MIPMAP_LINEAR */
206 gl_tex->states[WINED3DTEXSTA_MIPFILTER] = WINED3D_TEXF_LINEAR; /* GL_NEAREST_MIPMAP_LINEAR */
207 gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL] = 0;
208 gl_tex->states[WINED3DTEXSTA_MAXANISOTROPY] = 1;
209 if (context->gl_info->supported[EXT_TEXTURE_SRGB_DECODE])
210 gl_tex->states[WINED3DTEXSTA_SRGBTEXTURE] = TRUE;
211 else
212 gl_tex->states[WINED3DTEXSTA_SRGBTEXTURE] = srgb;
213 gl_tex->states[WINED3DTEXSTA_SHADOW] = FALSE;
214 wined3d_texture_set_dirty(texture);
216 context_bind_texture(context, target, gl_tex->name);
218 if (texture->resource.usage & WINED3DUSAGE_AUTOGENMIPMAP)
220 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_GENERATE_MIPMAP_SGIS, GL_TRUE);
221 checkGLcall("glTexParameteri(target, GL_GENERATE_MIPMAP_SGIS, GL_TRUE)");
224 /* For a new texture we have to set the texture levels after binding the
225 * texture. Beware that texture rectangles do not support mipmapping, but
226 * set the maxmiplevel if we're relying on the partial
227 * GL_ARB_texture_non_power_of_two emulation with texture rectangles.
228 * (I.e., do not care about cond_np2 here, just look for
229 * GL_TEXTURE_RECTANGLE_ARB.) */
230 if (target != GL_TEXTURE_RECTANGLE_ARB)
232 TRACE("Setting GL_TEXTURE_MAX_LEVEL to %u.\n", texture->level_count - 1);
233 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, texture->level_count - 1);
234 checkGLcall("glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, texture->level_count)");
237 if (target == GL_TEXTURE_CUBE_MAP_ARB)
239 /* Cubemaps are always set to clamp, regardless of the sampler state. */
240 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
241 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
242 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
245 if (texture->flags & WINED3D_TEXTURE_COND_NP2)
247 /* Conditinal non power of two textures use a different clamping
248 * default. If we're using the GL_WINE_normalized_texrect partial
249 * driver emulation, we're dealing with a GL_TEXTURE_2D texture which
250 * has the address mode set to repeat - something that prevents us
251 * from hitting the accelerated codepath. Thus manually set the GL
252 * state. The same applies to filtering. Even if the texture has only
253 * one mip level, the default LINEAR_MIPMAP_LINEAR filter causes a SW
254 * fallback on macos. */
255 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
256 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
257 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
258 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
259 checkGLcall("glTexParameteri");
260 gl_tex->states[WINED3DTEXSTA_ADDRESSU] = WINED3D_TADDRESS_CLAMP;
261 gl_tex->states[WINED3DTEXSTA_ADDRESSV] = WINED3D_TADDRESS_CLAMP;
262 gl_tex->states[WINED3DTEXSTA_MAGFILTER] = WINED3D_TEXF_POINT;
263 gl_tex->states[WINED3DTEXSTA_MINFILTER] = WINED3D_TEXF_POINT;
264 gl_tex->states[WINED3DTEXSTA_MIPFILTER] = WINED3D_TEXF_NONE;
268 /* Context activation is done by the caller. */
269 void wined3d_texture_bind_and_dirtify(struct wined3d_texture *texture,
270 struct wined3d_context *context, BOOL srgb)
272 DWORD active_sampler;
274 /* We don't need a specific texture unit, but after binding the texture
275 * the current unit is dirty. Read the unit back instead of switching to
276 * 0, this avoids messing around with the state manager's GL states. The
277 * current texture unit should always be a valid one.
279 * To be more specific, this is tricky because we can implicitly be
280 * called from sampler() in state.c. This means we can't touch anything
281 * other than whatever happens to be the currently active texture, or we
282 * would risk marking already applied sampler states dirty again. */
283 active_sampler = context->rev_tex_unit_map[context->active_texture];
284 if (active_sampler != WINED3D_UNMAPPED_STAGE)
285 context_invalidate_state(context, STATE_SAMPLER(active_sampler));
287 wined3d_texture_bind(texture, context, srgb);
290 /* Context activation is done by the caller. */
291 static void apply_wrap(const struct wined3d_gl_info *gl_info, GLenum target,
292 enum wined3d_texture_address d3d_wrap, GLenum param, BOOL cond_np2)
294 GLint gl_wrap;
296 if (d3d_wrap < WINED3D_TADDRESS_WRAP || d3d_wrap > WINED3D_TADDRESS_MIRROR_ONCE)
298 FIXME("Unrecognized or unsupported texture address mode %#x.\n", d3d_wrap);
299 return;
302 /* Cubemaps are always set to clamp, regardless of the sampler state. */
303 if (target == GL_TEXTURE_CUBE_MAP_ARB
304 || (cond_np2 && d3d_wrap == WINED3D_TADDRESS_WRAP))
305 gl_wrap = GL_CLAMP_TO_EDGE;
306 else
307 gl_wrap = gl_info->wrap_lookup[d3d_wrap - WINED3D_TADDRESS_WRAP];
309 TRACE("Setting param %#x to %#x for target %#x.\n", param, gl_wrap, target);
310 gl_info->gl_ops.gl.p_glTexParameteri(target, param, gl_wrap);
311 checkGLcall("glTexParameteri(target, param, gl_wrap)");
314 /* Context activation is done by the caller (state handler). */
315 void wined3d_texture_apply_state_changes(struct wined3d_texture *texture,
316 const DWORD sampler_states[WINED3D_HIGHEST_SAMPLER_STATE + 1],
317 const struct wined3d_gl_info *gl_info)
319 BOOL cond_np2 = texture->flags & WINED3D_TEXTURE_COND_NP2;
320 GLenum target = texture->target;
321 struct gl_texture *gl_tex;
322 DWORD state;
323 DWORD aniso;
325 TRACE("texture %p, sampler_states %p.\n", texture, sampler_states);
327 gl_tex = wined3d_texture_get_gl_texture(texture, texture->flags & WINED3D_TEXTURE_IS_SRGB);
329 /* This function relies on the correct texture being bound and loaded. */
331 if (sampler_states[WINED3D_SAMP_ADDRESS_U] != gl_tex->states[WINED3DTEXSTA_ADDRESSU])
333 state = sampler_states[WINED3D_SAMP_ADDRESS_U];
334 apply_wrap(gl_info, target, state, GL_TEXTURE_WRAP_S, cond_np2);
335 gl_tex->states[WINED3DTEXSTA_ADDRESSU] = state;
338 if (sampler_states[WINED3D_SAMP_ADDRESS_V] != gl_tex->states[WINED3DTEXSTA_ADDRESSV])
340 state = sampler_states[WINED3D_SAMP_ADDRESS_V];
341 apply_wrap(gl_info, target, state, GL_TEXTURE_WRAP_T, cond_np2);
342 gl_tex->states[WINED3DTEXSTA_ADDRESSV] = state;
345 if (sampler_states[WINED3D_SAMP_ADDRESS_W] != gl_tex->states[WINED3DTEXSTA_ADDRESSW])
347 state = sampler_states[WINED3D_SAMP_ADDRESS_W];
348 apply_wrap(gl_info, target, state, GL_TEXTURE_WRAP_R, cond_np2);
349 gl_tex->states[WINED3DTEXSTA_ADDRESSW] = state;
352 if (sampler_states[WINED3D_SAMP_BORDER_COLOR] != gl_tex->states[WINED3DTEXSTA_BORDERCOLOR])
354 float col[4];
356 state = sampler_states[WINED3D_SAMP_BORDER_COLOR];
357 D3DCOLORTOGLFLOAT4(state, col);
358 TRACE("Setting border color for %#x to %#x.\n", target, state);
359 gl_info->gl_ops.gl.p_glTexParameterfv(target, GL_TEXTURE_BORDER_COLOR, &col[0]);
360 checkGLcall("glTexParameterfv(..., GL_TEXTURE_BORDER_COLOR, ...)");
361 gl_tex->states[WINED3DTEXSTA_BORDERCOLOR] = state;
364 if (sampler_states[WINED3D_SAMP_MAG_FILTER] != gl_tex->states[WINED3DTEXSTA_MAGFILTER])
366 GLint gl_value;
368 state = sampler_states[WINED3D_SAMP_MAG_FILTER];
369 if (state > WINED3D_TEXF_ANISOTROPIC)
370 FIXME("Unrecognized or unsupported MAGFILTER* value %d.\n", state);
372 gl_value = wined3d_gl_mag_filter(texture->mag_lookup,
373 min(max(state, WINED3D_TEXF_POINT), WINED3D_TEXF_LINEAR));
374 TRACE("ValueMAG=%#x setting MAGFILTER to %#x.\n", state, gl_value);
375 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAG_FILTER, gl_value);
377 gl_tex->states[WINED3DTEXSTA_MAGFILTER] = state;
380 if ((sampler_states[WINED3D_SAMP_MIN_FILTER] != gl_tex->states[WINED3DTEXSTA_MINFILTER]
381 || sampler_states[WINED3D_SAMP_MIP_FILTER] != gl_tex->states[WINED3DTEXSTA_MIPFILTER]
382 || sampler_states[WINED3D_SAMP_MAX_MIP_LEVEL] != gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL]))
384 GLint gl_value;
386 gl_tex->states[WINED3DTEXSTA_MIPFILTER] = sampler_states[WINED3D_SAMP_MIP_FILTER];
387 gl_tex->states[WINED3DTEXSTA_MINFILTER] = sampler_states[WINED3D_SAMP_MIN_FILTER];
388 gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL] = sampler_states[WINED3D_SAMP_MAX_MIP_LEVEL];
390 if (gl_tex->states[WINED3DTEXSTA_MINFILTER] > WINED3D_TEXF_ANISOTROPIC
391 || gl_tex->states[WINED3DTEXSTA_MIPFILTER] > WINED3D_TEXF_ANISOTROPIC)
393 FIXME("Unrecognized or unsupported MIN_FILTER value %#x MIP_FILTER value %#x.\n",
394 gl_tex->states[WINED3DTEXSTA_MINFILTER],
395 gl_tex->states[WINED3DTEXSTA_MIPFILTER]);
397 gl_value = wined3d_gl_min_mip_filter(texture->min_mip_lookup,
398 min(max(sampler_states[WINED3D_SAMP_MIN_FILTER], WINED3D_TEXF_POINT), WINED3D_TEXF_LINEAR),
399 min(max(sampler_states[WINED3D_SAMP_MIP_FILTER], WINED3D_TEXF_NONE), WINED3D_TEXF_LINEAR));
401 TRACE("ValueMIN=%#x, ValueMIP=%#x, setting MINFILTER to %#x.\n",
402 sampler_states[WINED3D_SAMP_MIN_FILTER],
403 sampler_states[WINED3D_SAMP_MIP_FILTER], gl_value);
404 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MIN_FILTER, gl_value);
405 checkGLcall("glTexParameter GL_TEXTURE_MIN_FILTER, ...");
407 if (!cond_np2)
409 if (gl_tex->states[WINED3DTEXSTA_MIPFILTER] == WINED3D_TEXF_NONE)
410 gl_value = texture->lod;
411 else if (gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL] >= texture->level_count)
412 gl_value = texture->level_count - 1;
413 else if (gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL] < texture->lod)
414 /* texture->lod is already clamped in the setter. */
415 gl_value = texture->lod;
416 else
417 gl_value = gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL];
419 /* Note that WINED3D_SAMP_MAX_MIP_LEVEL specifies the largest mipmap
420 * (default 0), while GL_TEXTURE_MAX_LEVEL specifies the smallest
421 * mimap used (default 1000). So WINED3D_SAMP_MAX_MIP_LEVEL
422 * corresponds to GL_TEXTURE_BASE_LEVEL. */
423 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_BASE_LEVEL, gl_value);
427 if ((gl_tex->states[WINED3DTEXSTA_MAGFILTER] != WINED3D_TEXF_ANISOTROPIC
428 && gl_tex->states[WINED3DTEXSTA_MINFILTER] != WINED3D_TEXF_ANISOTROPIC
429 && gl_tex->states[WINED3DTEXSTA_MIPFILTER] != WINED3D_TEXF_ANISOTROPIC)
430 || cond_np2)
431 aniso = 1;
432 else
433 aniso = sampler_states[WINED3D_SAMP_MAX_ANISOTROPY];
435 if (gl_tex->states[WINED3DTEXSTA_MAXANISOTROPY] != aniso)
437 if (gl_info->supported[EXT_TEXTURE_FILTER_ANISOTROPIC])
439 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAX_ANISOTROPY_EXT, aniso);
440 checkGLcall("glTexParameteri(GL_TEXTURE_MAX_ANISOTROPY_EXT, aniso)");
442 else
444 WARN("Anisotropic filtering not supported.\n");
446 gl_tex->states[WINED3DTEXSTA_MAXANISOTROPY] = aniso;
449 /* These should always be the same unless EXT_texture_sRGB_decode is supported. */
450 if (sampler_states[WINED3D_SAMP_SRGB_TEXTURE] != gl_tex->states[WINED3DTEXSTA_SRGBTEXTURE])
452 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_SRGB_DECODE_EXT,
453 sampler_states[WINED3D_SAMP_SRGB_TEXTURE] ? GL_DECODE_EXT : GL_SKIP_DECODE_EXT);
454 checkGLcall("glTexParameteri(GL_TEXTURE_SRGB_DECODE_EXT)");
455 gl_tex->states[WINED3DTEXSTA_SRGBTEXTURE] = sampler_states[WINED3D_SAMP_SRGB_TEXTURE];
458 if (!(texture->resource.format->flags & WINED3DFMT_FLAG_SHADOW)
459 != !gl_tex->states[WINED3DTEXSTA_SHADOW])
461 if (texture->resource.format->flags & WINED3DFMT_FLAG_SHADOW)
463 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_DEPTH_TEXTURE_MODE_ARB, GL_LUMINANCE);
464 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE_ARB);
465 checkGLcall("glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE_ARB)");
466 gl_tex->states[WINED3DTEXSTA_SHADOW] = TRUE;
468 else
470 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_NONE);
471 checkGLcall("glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_NONE)");
472 gl_tex->states[WINED3DTEXSTA_SHADOW] = FALSE;
477 ULONG CDECL wined3d_texture_incref(struct wined3d_texture *texture)
479 ULONG refcount = InterlockedIncrement(&texture->resource.ref);
481 TRACE("%p increasing refcount to %u.\n", texture, refcount);
483 return refcount;
486 ULONG CDECL wined3d_texture_decref(struct wined3d_texture *texture)
488 ULONG refcount = InterlockedDecrement(&texture->resource.ref);
490 TRACE("%p decreasing refcount to %u.\n", texture, refcount);
492 if (!refcount)
494 wined3d_texture_cleanup(texture);
495 texture->resource.parent_ops->wined3d_object_destroyed(texture->resource.parent);
496 HeapFree(GetProcessHeap(), 0, texture);
499 return refcount;
502 struct wined3d_resource * CDECL wined3d_texture_get_resource(struct wined3d_texture *texture)
504 TRACE("texture %p.\n", texture);
506 return &texture->resource;
509 DWORD CDECL wined3d_texture_set_priority(struct wined3d_texture *texture, DWORD priority)
511 return wined3d_resource_set_priority(&texture->resource, priority);
514 DWORD CDECL wined3d_texture_get_priority(const struct wined3d_texture *texture)
516 return wined3d_resource_get_priority(&texture->resource);
519 /* Context activation is done by the caller */
520 void wined3d_texture_load(struct wined3d_texture *texture,
521 struct wined3d_context *context, BOOL srgb)
523 UINT sub_count = texture->level_count * texture->layer_count;
524 const struct wined3d_gl_info *gl_info = context->gl_info;
525 DWORD flag;
526 UINT i;
528 TRACE("texture %p, srgb %#x.\n", texture, srgb);
530 if (gl_info->supported[EXT_TEXTURE_SRGB_DECODE])
531 srgb = FALSE;
533 if (srgb)
534 flag = WINED3D_TEXTURE_SRGB_VALID;
535 else
536 flag = WINED3D_TEXTURE_RGB_VALID;
538 if (texture->flags & flag)
540 TRACE("Texture %p not dirty, nothing to do.\n", texture);
541 return;
544 /* Reload the surfaces if the texture is marked dirty. */
545 for (i = 0; i < sub_count; ++i)
547 texture->texture_ops->texture_sub_resource_load(texture->sub_resources[i], context, srgb);
549 texture->flags |= flag;
552 void CDECL wined3d_texture_preload(struct wined3d_texture *texture)
554 struct wined3d_context *context;
555 context = context_acquire(texture->resource.device, NULL);
556 wined3d_texture_load(texture, context, texture->flags & WINED3D_TEXTURE_IS_SRGB);
557 context_release(context);
560 void * CDECL wined3d_texture_get_parent(const struct wined3d_texture *texture)
562 TRACE("texture %p.\n", texture);
564 return texture->resource.parent;
567 DWORD CDECL wined3d_texture_set_lod(struct wined3d_texture *texture, DWORD lod)
569 DWORD old = texture->lod;
571 TRACE("texture %p, lod %u.\n", texture, lod);
573 /* The d3d9:texture test shows that SetLOD is ignored on non-managed
574 * textures. The call always returns 0, and GetLOD always returns 0. */
575 if (texture->resource.pool != WINED3D_POOL_MANAGED)
577 TRACE("Ignoring SetLOD on %s texture, returning 0.\n", debug_d3dpool(texture->resource.pool));
578 return 0;
581 if (lod >= texture->level_count)
582 lod = texture->level_count - 1;
584 if (texture->lod != lod)
586 texture->lod = lod;
588 texture->texture_rgb.states[WINED3DTEXSTA_MAXMIPLEVEL] = ~0U;
589 texture->texture_srgb.states[WINED3DTEXSTA_MAXMIPLEVEL] = ~0U;
590 if (texture->resource.bind_count)
591 device_invalidate_state(texture->resource.device, STATE_SAMPLER(texture->sampler));
594 return old;
597 DWORD CDECL wined3d_texture_get_lod(const struct wined3d_texture *texture)
599 TRACE("texture %p, returning %u.\n", texture, texture->lod);
601 return texture->lod;
604 DWORD CDECL wined3d_texture_get_level_count(const struct wined3d_texture *texture)
606 TRACE("texture %p, returning %u.\n", texture, texture->level_count);
608 return texture->level_count;
611 HRESULT CDECL wined3d_texture_set_autogen_filter_type(struct wined3d_texture *texture,
612 enum wined3d_texture_filter_type filter_type)
614 FIXME("texture %p, filter_type %s stub!\n", texture, debug_d3dtexturefiltertype(filter_type));
616 if (!(texture->resource.usage & WINED3DUSAGE_AUTOGENMIPMAP))
618 WARN("Texture doesn't have AUTOGENMIPMAP usage.\n");
619 return WINED3DERR_INVALIDCALL;
622 texture->filter_type = filter_type;
624 return WINED3D_OK;
627 enum wined3d_texture_filter_type CDECL wined3d_texture_get_autogen_filter_type(const struct wined3d_texture *texture)
629 TRACE("texture %p.\n", texture);
631 return texture->filter_type;
634 HRESULT CDECL wined3d_texture_set_color_key(struct wined3d_texture *texture,
635 DWORD flags, const struct wined3d_color_key *color_key)
637 TRACE("texture %p, flags %#x, color_key %p.\n", texture, flags, color_key);
639 if (flags & WINEDDCKEY_COLORSPACE)
641 FIXME("Unhandled flags %#x.\n", flags);
642 return WINED3DERR_INVALIDCALL;
645 if (color_key)
647 switch (flags & ~WINEDDCKEY_COLORSPACE)
649 case WINEDDCKEY_DESTBLT:
650 texture->dst_blt_color_key = *color_key;
651 texture->color_key_flags |= WINEDDSD_CKDESTBLT;
652 break;
654 case WINEDDCKEY_DESTOVERLAY:
655 texture->dst_overlay_color_key = *color_key;
656 texture->color_key_flags |= WINEDDSD_CKDESTOVERLAY;
657 break;
659 case WINEDDCKEY_SRCOVERLAY:
660 texture->src_overlay_color_key = *color_key;
661 texture->color_key_flags |= WINEDDSD_CKSRCOVERLAY;
662 break;
664 case WINEDDCKEY_SRCBLT:
665 texture->src_blt_color_key = *color_key;
666 texture->color_key_flags |= WINEDDSD_CKSRCBLT;
667 break;
670 else
672 switch (flags & ~WINEDDCKEY_COLORSPACE)
674 case WINEDDCKEY_DESTBLT:
675 texture->color_key_flags &= ~WINEDDSD_CKDESTBLT;
676 break;
678 case WINEDDCKEY_DESTOVERLAY:
679 texture->color_key_flags &= ~WINEDDSD_CKDESTOVERLAY;
680 break;
682 case WINEDDCKEY_SRCOVERLAY:
683 texture->color_key_flags &= ~WINEDDSD_CKSRCOVERLAY;
684 break;
686 case WINEDDCKEY_SRCBLT:
687 texture->color_key_flags &= ~WINEDDSD_CKSRCBLT;
688 break;
692 return WINED3D_OK;
695 void CDECL wined3d_texture_generate_mipmaps(struct wined3d_texture *texture)
697 /* TODO: Implement filters using GL_SGI_generate_mipmaps. */
698 FIXME("texture %p stub!\n", texture);
701 struct wined3d_resource * CDECL wined3d_texture_get_sub_resource(struct wined3d_texture *texture,
702 UINT sub_resource_idx)
704 UINT sub_count = texture->level_count * texture->layer_count;
706 TRACE("texture %p, sub_resource_idx %u.\n", texture, sub_resource_idx);
708 if (sub_resource_idx >= sub_count)
710 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx, sub_count);
711 return NULL;
714 return texture->sub_resources[sub_resource_idx];
717 HRESULT CDECL wined3d_texture_add_dirty_region(struct wined3d_texture *texture,
718 UINT layer, const struct wined3d_box *dirty_region)
720 struct wined3d_resource *sub_resource;
722 TRACE("texture %p, layer %u, dirty_region %p.\n", texture, layer, dirty_region);
724 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, layer * texture->level_count)))
726 WARN("Failed to get sub-resource.\n");
727 return WINED3DERR_INVALIDCALL;
730 texture->texture_ops->texture_sub_resource_add_dirty_region(sub_resource, dirty_region);
732 return WINED3D_OK;
735 static void texture2d_sub_resource_load(struct wined3d_resource *sub_resource,
736 struct wined3d_context *context, BOOL srgb)
738 surface_load(surface_from_resource(sub_resource), srgb);
741 static void texture2d_sub_resource_add_dirty_region(struct wined3d_resource *sub_resource,
742 const struct wined3d_box *dirty_region)
744 struct wined3d_surface *surface = surface_from_resource(sub_resource);
746 surface_prepare_map_memory(surface);
747 surface_load_location(surface, surface->map_binding);
748 surface_invalidate_location(surface, ~surface->map_binding);
751 static void texture2d_sub_resource_cleanup(struct wined3d_resource *sub_resource)
753 struct wined3d_surface *surface = surface_from_resource(sub_resource);
755 surface_set_container(surface, NULL);
756 wined3d_surface_decref(surface);
759 static const struct wined3d_texture_ops texture2d_ops =
761 texture2d_sub_resource_load,
762 texture2d_sub_resource_add_dirty_region,
763 texture2d_sub_resource_cleanup,
766 static void wined3d_texture_unload(struct wined3d_resource *resource)
768 struct wined3d_texture *texture = wined3d_texture_from_resource(resource);
769 UINT sub_count = texture->level_count * texture->layer_count;
770 UINT i;
772 TRACE("texture %p.\n", texture);
774 for (i = 0; i < sub_count; ++i)
776 struct wined3d_resource *sub_resource = texture->sub_resources[i];
778 sub_resource->resource_ops->resource_unload(sub_resource);
781 wined3d_texture_unload_gl_texture(texture);
784 static const struct wined3d_resource_ops texture_resource_ops =
786 wined3d_texture_unload,
789 static HRESULT cubetexture_init(struct wined3d_texture *texture, const struct wined3d_resource_desc *desc,
790 UINT levels, DWORD surface_flags, struct wined3d_device *device, void *parent,
791 const struct wined3d_parent_ops *parent_ops)
793 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
794 struct wined3d_resource_desc surface_desc;
795 unsigned int i, j;
796 HRESULT hr;
798 /* TODO: It should only be possible to create textures for formats
799 * that are reported as supported. */
800 if (WINED3DFMT_UNKNOWN >= desc->format)
802 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture);
803 return WINED3DERR_INVALIDCALL;
806 if (!gl_info->supported[ARB_TEXTURE_CUBE_MAP] && desc->pool != WINED3D_POOL_SCRATCH)
808 WARN("(%p) : Tried to create not supported cube texture.\n", texture);
809 return WINED3DERR_INVALIDCALL;
812 /* Calculate levels for mip mapping */
813 if (desc->usage & WINED3DUSAGE_AUTOGENMIPMAP)
815 if (!gl_info->supported[SGIS_GENERATE_MIPMAP])
817 WARN("No mipmap generation support, returning D3DERR_INVALIDCALL.\n");
818 return WINED3DERR_INVALIDCALL;
821 if (levels > 1)
823 WARN("D3DUSAGE_AUTOGENMIPMAP is set, and level count > 1, returning D3DERR_INVALIDCALL.\n");
824 return WINED3DERR_INVALIDCALL;
827 levels = 1;
829 else if (!levels)
831 levels = wined3d_log2i(desc->width) + 1;
832 TRACE("Calculated levels = %u.\n", levels);
835 if (!gl_info->supported[ARB_TEXTURE_NON_POWER_OF_TWO])
837 UINT pow2_edge_length = 1;
838 while (pow2_edge_length < desc->width)
839 pow2_edge_length <<= 1;
841 if (desc->width != pow2_edge_length)
843 if (desc->pool == WINED3D_POOL_SCRATCH)
845 /* SCRATCH textures cannot be used for texturing */
846 WARN("Creating a scratch NPOT cube texture despite lack of HW support.\n");
848 else
850 WARN("Attempted to create a NPOT cube texture (edge length %u) without GL support.\n", desc->width);
851 return WINED3DERR_INVALIDCALL;
856 if (FAILED(hr = wined3d_texture_init(texture, &texture2d_ops, 6, levels,
857 desc, device, parent, parent_ops, &texture_resource_ops)))
859 WARN("Failed to initialize texture, returning %#x\n", hr);
860 return hr;
863 texture->pow2_matrix[0] = 1.0f;
864 texture->pow2_matrix[5] = 1.0f;
865 texture->pow2_matrix[10] = 1.0f;
866 texture->pow2_matrix[15] = 1.0f;
867 texture->target = GL_TEXTURE_CUBE_MAP_ARB;
869 /* Generate all the surfaces. */
870 surface_desc = *desc;
871 surface_desc.resource_type = WINED3D_RTYPE_SURFACE;
872 for (i = 0; i < texture->level_count; ++i)
874 /* Create the 6 faces. */
875 for (j = 0; j < 6; ++j)
877 static const GLenum cube_targets[6] =
879 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB,
880 GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB,
881 GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB,
882 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB,
883 GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB,
884 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB,
886 UINT idx = j * texture->level_count + i;
887 struct wined3d_surface *surface;
889 if (FAILED(hr = wined3d_surface_create(texture, &surface_desc,
890 cube_targets[j], i, surface_flags, &surface)))
892 WARN("Failed to create surface, hr %#x.\n", hr);
893 wined3d_texture_cleanup(texture);
894 return hr;
897 texture->sub_resources[idx] = &surface->resource;
898 TRACE("Created surface level %u @ %p.\n", i, surface);
900 surface_desc.width = max(1, surface_desc.width >> 1);
901 surface_desc.height = surface_desc.width;
904 return WINED3D_OK;
907 static HRESULT texture_init(struct wined3d_texture *texture, const struct wined3d_resource_desc *desc,
908 UINT levels, DWORD surface_flags, struct wined3d_device *device, void *parent,
909 const struct wined3d_parent_ops *parent_ops)
911 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
912 struct wined3d_resource_desc surface_desc;
913 UINT pow2_width, pow2_height;
914 unsigned int i;
915 HRESULT hr;
917 /* TODO: It should only be possible to create textures for formats
918 * that are reported as supported. */
919 if (WINED3DFMT_UNKNOWN >= desc->format)
921 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture);
922 return WINED3DERR_INVALIDCALL;
925 /* Non-power2 support. */
926 if (gl_info->supported[ARB_TEXTURE_NON_POWER_OF_TWO])
928 pow2_width = desc->width;
929 pow2_height = desc->height;
931 else
933 /* Find the nearest pow2 match. */
934 pow2_width = pow2_height = 1;
935 while (pow2_width < desc->width)
936 pow2_width <<= 1;
937 while (pow2_height < desc->height)
938 pow2_height <<= 1;
940 if (pow2_width != desc->width || pow2_height != desc->height)
942 /* levels == 0 returns an error as well */
943 if (levels != 1)
945 if (desc->pool == WINED3D_POOL_SCRATCH)
947 WARN("Creating a scratch mipmapped NPOT texture despite lack of HW support.\n");
949 else
951 WARN("Attempted to create a mipmapped NPOT texture without unconditional NPOT support.\n");
952 return WINED3DERR_INVALIDCALL;
958 /* Calculate levels for mip mapping. */
959 if (desc->usage & WINED3DUSAGE_AUTOGENMIPMAP)
961 if (!gl_info->supported[SGIS_GENERATE_MIPMAP])
963 WARN("No mipmap generation support, returning WINED3DERR_INVALIDCALL.\n");
964 return WINED3DERR_INVALIDCALL;
967 if (levels > 1)
969 WARN("D3DUSAGE_AUTOGENMIPMAP is set, and level count > 1, returning WINED3DERR_INVALIDCALL.\n");
970 return WINED3DERR_INVALIDCALL;
973 levels = 1;
975 else if (!levels)
977 levels = wined3d_log2i(max(desc->width, desc->height)) + 1;
978 TRACE("Calculated levels = %u.\n", levels);
981 if (FAILED(hr = wined3d_texture_init(texture, &texture2d_ops, 1, levels,
982 desc, device, parent, parent_ops, &texture_resource_ops)))
984 WARN("Failed to initialize texture, returning %#x.\n", hr);
985 return hr;
988 /* Precalculated scaling for 'faked' non power of two texture coords. */
989 if (gl_info->supported[WINED3D_GL_NORMALIZED_TEXRECT]
990 && (desc->width != pow2_width || desc->height != pow2_height))
992 texture->pow2_matrix[0] = 1.0f;
993 texture->pow2_matrix[5] = 1.0f;
994 texture->pow2_matrix[10] = 1.0f;
995 texture->pow2_matrix[15] = 1.0f;
996 texture->target = GL_TEXTURE_2D;
997 texture->flags |= WINED3D_TEXTURE_COND_NP2;
998 texture->min_mip_lookup = minMipLookup_noFilter;
1000 else if (gl_info->supported[ARB_TEXTURE_RECTANGLE]
1001 && (desc->width != pow2_width || desc->height != pow2_height))
1003 texture->pow2_matrix[0] = (float)desc->width;
1004 texture->pow2_matrix[5] = (float)desc->height;
1005 texture->pow2_matrix[10] = 1.0f;
1006 texture->pow2_matrix[15] = 1.0f;
1007 texture->target = GL_TEXTURE_RECTANGLE_ARB;
1008 texture->flags |= WINED3D_TEXTURE_COND_NP2;
1009 texture->flags &= ~WINED3D_TEXTURE_POW2_MAT_IDENT;
1011 if (texture->resource.format->flags & WINED3DFMT_FLAG_FILTERING)
1012 texture->min_mip_lookup = minMipLookup_noMip;
1013 else
1014 texture->min_mip_lookup = minMipLookup_noFilter;
1016 else
1018 if ((desc->width != pow2_width) || (desc->height != pow2_height))
1020 texture->pow2_matrix[0] = (((float)desc->width) / ((float)pow2_width));
1021 texture->pow2_matrix[5] = (((float)desc->height) / ((float)pow2_height));
1022 texture->flags &= ~WINED3D_TEXTURE_POW2_MAT_IDENT;
1024 else
1026 texture->pow2_matrix[0] = 1.0f;
1027 texture->pow2_matrix[5] = 1.0f;
1030 texture->pow2_matrix[10] = 1.0f;
1031 texture->pow2_matrix[15] = 1.0f;
1032 texture->target = GL_TEXTURE_2D;
1034 TRACE("xf(%f) yf(%f)\n", texture->pow2_matrix[0], texture->pow2_matrix[5]);
1036 /* Generate all the surfaces. */
1037 surface_desc = *desc;
1038 surface_desc.resource_type = WINED3D_RTYPE_SURFACE;
1039 for (i = 0; i < texture->level_count; ++i)
1041 struct wined3d_surface *surface;
1043 if (FAILED(hr = wined3d_surface_create(texture, &surface_desc,
1044 texture->target, i, surface_flags, &surface)))
1046 WARN("Failed to create surface, hr %#x.\n", hr);
1047 wined3d_texture_cleanup(texture);
1048 return hr;
1051 texture->sub_resources[i] = &surface->resource;
1052 TRACE("Created surface level %u @ %p.\n", i, surface);
1053 /* Calculate the next mipmap level. */
1054 surface_desc.width = max(1, surface_desc.width >> 1);
1055 surface_desc.height = max(1, surface_desc.height >> 1);
1058 return WINED3D_OK;
1061 static void texture3d_sub_resource_load(struct wined3d_resource *sub_resource,
1062 struct wined3d_context *context, BOOL srgb)
1064 wined3d_volume_load(volume_from_resource(sub_resource), context, srgb);
1067 static void texture3d_sub_resource_add_dirty_region(struct wined3d_resource *sub_resource,
1068 const struct wined3d_box *dirty_region)
1070 wined3d_texture_set_dirty(volume_from_resource(sub_resource)->container);
1073 static void texture3d_sub_resource_cleanup(struct wined3d_resource *sub_resource)
1075 struct wined3d_volume *volume = volume_from_resource(sub_resource);
1077 /* Cleanup the container. */
1078 volume_set_container(volume, NULL);
1079 wined3d_volume_decref(volume);
1082 static const struct wined3d_texture_ops texture3d_ops =
1084 texture3d_sub_resource_load,
1085 texture3d_sub_resource_add_dirty_region,
1086 texture3d_sub_resource_cleanup,
1089 static HRESULT volumetexture_init(struct wined3d_texture *texture, const struct wined3d_resource_desc *desc,
1090 UINT levels, struct wined3d_device *device, void *parent, const struct wined3d_parent_ops *parent_ops)
1092 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
1093 struct wined3d_resource_desc volume_desc;
1094 unsigned int i;
1095 HRESULT hr;
1097 /* TODO: It should only be possible to create textures for formats
1098 * that are reported as supported. */
1099 if (WINED3DFMT_UNKNOWN >= desc->format)
1101 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture);
1102 return WINED3DERR_INVALIDCALL;
1105 if (!gl_info->supported[EXT_TEXTURE3D])
1107 WARN("(%p) : Texture cannot be created - no volume texture support.\n", texture);
1108 return WINED3DERR_INVALIDCALL;
1111 /* Calculate levels for mip mapping. */
1112 if (desc->usage & WINED3DUSAGE_AUTOGENMIPMAP)
1114 if (!gl_info->supported[SGIS_GENERATE_MIPMAP])
1116 WARN("No mipmap generation support, returning D3DERR_INVALIDCALL.\n");
1117 return WINED3DERR_INVALIDCALL;
1120 if (levels > 1)
1122 WARN("D3DUSAGE_AUTOGENMIPMAP is set, and level count > 1, returning D3DERR_INVALIDCALL.\n");
1123 return WINED3DERR_INVALIDCALL;
1126 levels = 1;
1128 else if (!levels)
1130 levels = wined3d_log2i(max(max(desc->width, desc->height), desc->depth)) + 1;
1131 TRACE("Calculated levels = %u.\n", levels);
1134 if (!gl_info->supported[ARB_TEXTURE_NON_POWER_OF_TWO])
1136 UINT pow2_w, pow2_h, pow2_d;
1137 pow2_w = 1;
1138 while (pow2_w < desc->width)
1139 pow2_w <<= 1;
1140 pow2_h = 1;
1141 while (pow2_h < desc->height)
1142 pow2_h <<= 1;
1143 pow2_d = 1;
1144 while (pow2_d < desc->depth)
1145 pow2_d <<= 1;
1147 if (pow2_w != desc->width || pow2_h != desc->height || pow2_d != desc->depth)
1149 if (desc->pool == WINED3D_POOL_SCRATCH)
1151 WARN("Creating a scratch NPOT volume texture despite lack of HW support.\n");
1153 else
1155 WARN("Attempted to create a NPOT volume texture (%u, %u, %u) without GL support.\n",
1156 desc->width, desc->height, desc->depth);
1157 return WINED3DERR_INVALIDCALL;
1162 if (FAILED(hr = wined3d_texture_init(texture, &texture3d_ops, 1, levels,
1163 desc, device, parent, parent_ops, &texture_resource_ops)))
1165 WARN("Failed to initialize texture, returning %#x.\n", hr);
1166 return hr;
1169 texture->pow2_matrix[0] = 1.0f;
1170 texture->pow2_matrix[5] = 1.0f;
1171 texture->pow2_matrix[10] = 1.0f;
1172 texture->pow2_matrix[15] = 1.0f;
1173 texture->target = GL_TEXTURE_3D;
1175 /* Generate all the surfaces. */
1176 volume_desc = *desc;
1177 volume_desc.resource_type = WINED3D_RTYPE_VOLUME;
1178 for (i = 0; i < texture->level_count; ++i)
1180 struct wined3d_volume *volume;
1182 if (FAILED(hr = wined3d_volume_create(texture, &volume_desc, i, &volume)))
1184 ERR("Creating a volume for the volume texture failed, hr %#x.\n", hr);
1185 wined3d_texture_cleanup(texture);
1186 return hr;
1189 texture->sub_resources[i] = &volume->resource;
1191 /* Calculate the next mipmap level. */
1192 volume_desc.width = max(1, volume_desc.width >> 1);
1193 volume_desc.height = max(1, volume_desc.height >> 1);
1194 volume_desc.depth = max(1, volume_desc.depth >> 1);
1197 return WINED3D_OK;
1200 HRESULT CDECL wined3d_texture_create(struct wined3d_device *device, const struct wined3d_resource_desc *desc,
1201 UINT level_count, DWORD surface_flags, void *parent, const struct wined3d_parent_ops *parent_ops,
1202 struct wined3d_texture **texture)
1204 struct wined3d_texture *object;
1205 HRESULT hr;
1207 TRACE("device %p, desc %p, level_count %u, surface_flags %#x, parent %p, parent_ops %p, texture %p.\n",
1208 device, desc, level_count, surface_flags, parent, parent_ops, texture);
1210 if (!(object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object))))
1211 return E_OUTOFMEMORY;
1213 switch (desc->resource_type)
1215 case WINED3D_RTYPE_TEXTURE:
1216 hr = texture_init(object, desc, level_count, surface_flags, device, parent, parent_ops);
1217 break;
1219 case WINED3D_RTYPE_VOLUME_TEXTURE:
1220 hr = volumetexture_init(object, desc, level_count, device, parent, parent_ops);
1221 break;
1223 case WINED3D_RTYPE_CUBE_TEXTURE:
1224 hr = cubetexture_init(object, desc, level_count, surface_flags, device, parent, parent_ops);
1225 break;
1227 default:
1228 ERR("Invalid resource type %s.\n", debug_d3dresourcetype(desc->resource_type));
1229 hr = WINED3DERR_INVALIDCALL;
1230 break;
1233 if (FAILED(hr))
1235 WARN("Failed to initialize texture, returning %#x.\n", hr);
1236 HeapFree(GetProcessHeap(), 0, object);
1237 return hr;
1240 TRACE("Created texture %p.\n", object);
1241 *texture = object;
1243 return WINED3D_OK;