gdi32: Don't overflow the buffer in GetGlyphOutline.
[wine/multimedia.git] / dlls / wined3d / texture.c
blob4f962f127f578878e891815bbb4e47c871664e07
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, DWORD surface_flags,
32 struct wined3d_device *device, void *parent, const struct wined3d_parent_ops *parent_ops,
33 const struct wined3d_resource_ops *resource_ops)
35 const struct wined3d_format *format = wined3d_get_format(&device->adapter->gl_info, desc->format);
36 HRESULT hr;
38 TRACE("texture %p, texture_ops %p, layer_count %u, level_count %u, resource_type %s, format %s, "
39 "multisample_type %#x, multisample_quality %#x, usage %s, pool %s, width %u, height %u, depth %u, "
40 "surface_flags %#x, device %p, parent %p, parent_ops %p, resource_ops %p.\n",
41 texture, texture_ops, layer_count, level_count, debug_d3dresourcetype(desc->resource_type),
42 debug_d3dformat(desc->format), desc->multisample_type, desc->multisample_quality,
43 debug_d3dusage(desc->usage), debug_d3dpool(desc->pool), desc->width, desc->height, desc->depth,
44 surface_flags, device, parent, parent_ops, resource_ops);
46 if ((format->flags & (WINED3DFMT_FLAG_BLOCKS | WINED3DFMT_FLAG_BLOCKS_NO_VERIFY)) == WINED3DFMT_FLAG_BLOCKS)
48 UINT width_mask = format->block_width - 1;
49 UINT height_mask = format->block_height - 1;
50 if (desc->width & width_mask || desc->height & height_mask)
51 return WINED3DERR_INVALIDCALL;
54 if (FAILED(hr = resource_init(&texture->resource, device, desc->resource_type, format,
55 desc->multisample_type, desc->multisample_quality, desc->usage, desc->pool,
56 desc->width, desc->height, desc->depth, 0, parent, parent_ops, resource_ops)))
58 static unsigned int once;
60 if ((desc->format == WINED3DFMT_DXT1 || desc->format == WINED3DFMT_DXT2 || desc->format == WINED3DFMT_DXT3
61 || desc->format == WINED3DFMT_DXT4 || desc->format == WINED3DFMT_DXT5)
62 && !(format->flags & WINED3DFMT_FLAG_TEXTURE) && !once++)
63 ERR_(winediag)("The application tried to create a DXTn texture, but the driver does not support them.\n");
65 WARN("Failed to initialize resource, returning %#x\n", hr);
66 return hr;
68 wined3d_resource_update_draw_binding(&texture->resource);
70 texture->texture_ops = texture_ops;
71 texture->sub_resources = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
72 level_count * layer_count * sizeof(*texture->sub_resources));
73 if (!texture->sub_resources)
75 ERR("Failed to allocate sub-resource array.\n");
76 resource_cleanup(&texture->resource);
77 return E_OUTOFMEMORY;
80 texture->layer_count = layer_count;
81 texture->level_count = level_count;
82 texture->filter_type = (desc->usage & WINED3DUSAGE_AUTOGENMIPMAP) ? WINED3D_TEXF_LINEAR : WINED3D_TEXF_NONE;
83 texture->lod = 0;
84 texture->flags = WINED3D_TEXTURE_POW2_MAT_IDENT | WINED3D_TEXTURE_NORMALIZED_COORDS;
85 if (surface_flags & WINED3D_SURFACE_PIN_SYSMEM)
86 texture->flags |= WINED3D_TEXTURE_PIN_SYSMEM;
88 if (texture->resource.format->flags & WINED3DFMT_FLAG_FILTERING)
90 texture->min_mip_lookup = minMipLookup;
91 texture->mag_lookup = magLookup;
93 else
95 texture->min_mip_lookup = minMipLookup_noFilter;
96 texture->mag_lookup = magLookup_noFilter;
99 return WINED3D_OK;
102 /* A GL context is provided by the caller */
103 static void gltexture_delete(const struct wined3d_gl_info *gl_info, struct gl_texture *tex)
105 gl_info->gl_ops.gl.p_glDeleteTextures(1, &tex->name);
106 tex->name = 0;
109 static void wined3d_texture_unload_gl_texture(struct wined3d_texture *texture)
111 struct wined3d_device *device = texture->resource.device;
112 struct wined3d_context *context = NULL;
114 if (texture->texture_rgb.name || texture->texture_srgb.name)
116 context = context_acquire(device, NULL);
119 if (texture->texture_rgb.name)
120 gltexture_delete(context->gl_info, &texture->texture_rgb);
122 if (texture->texture_srgb.name)
123 gltexture_delete(context->gl_info, &texture->texture_srgb);
125 if (context) context_release(context);
127 wined3d_texture_set_dirty(texture);
129 resource_unload(&texture->resource);
132 static void wined3d_texture_cleanup(struct wined3d_texture *texture)
134 UINT sub_count = texture->level_count * texture->layer_count;
135 UINT i;
137 TRACE("texture %p.\n", texture);
139 for (i = 0; i < sub_count; ++i)
141 struct wined3d_resource *sub_resource = texture->sub_resources[i];
143 if (sub_resource)
144 texture->texture_ops->texture_sub_resource_cleanup(sub_resource);
147 wined3d_texture_unload_gl_texture(texture);
148 HeapFree(GetProcessHeap(), 0, texture->sub_resources);
149 resource_cleanup(&texture->resource);
152 void wined3d_texture_set_swapchain(struct wined3d_texture *texture, struct wined3d_swapchain *swapchain)
154 texture->swapchain = swapchain;
155 wined3d_resource_update_draw_binding(&texture->resource);
158 void wined3d_texture_set_dirty(struct wined3d_texture *texture)
160 texture->flags &= ~(WINED3D_TEXTURE_RGB_VALID | WINED3D_TEXTURE_SRGB_VALID);
163 /* Context activation is done by the caller. */
164 void wined3d_texture_bind(struct wined3d_texture *texture,
165 struct wined3d_context *context, BOOL srgb)
167 const struct wined3d_gl_info *gl_info = context->gl_info;
168 struct gl_texture *gl_tex;
169 GLenum target;
171 TRACE("texture %p, context %p, srgb %#x.\n", texture, context, srgb);
173 if (gl_info->supported[EXT_TEXTURE_SRGB_DECODE])
174 srgb = FALSE;
176 /* sRGB mode cache for preload() calls outside drawprim. */
177 if (srgb)
178 texture->flags |= WINED3D_TEXTURE_IS_SRGB;
179 else
180 texture->flags &= ~WINED3D_TEXTURE_IS_SRGB;
182 gl_tex = wined3d_texture_get_gl_texture(texture, srgb);
183 target = texture->target;
185 if (gl_tex->name)
187 context_bind_texture(context, target, gl_tex->name);
188 return;
191 gl_info->gl_ops.gl.p_glGenTextures(1, &gl_tex->name);
192 checkGLcall("glGenTextures");
193 TRACE("Generated texture %d.\n", gl_tex->name);
195 if (!gl_tex->name)
197 ERR("Failed to generate a texture name.\n");
198 return;
201 if (texture->resource.pool == WINED3D_POOL_DEFAULT)
203 /* Tell OpenGL to try and keep this texture in video ram (well mostly). */
204 GLclampf tmp = 0.9f;
205 gl_info->gl_ops.gl.p_glPrioritizeTextures(1, &gl_tex->name, &tmp);
208 /* Initialise the state of the texture object to the OpenGL defaults, not
209 * the wined3d defaults. */
210 gl_tex->states[WINED3DTEXSTA_ADDRESSU] = WINED3D_TADDRESS_WRAP;
211 gl_tex->states[WINED3DTEXSTA_ADDRESSV] = WINED3D_TADDRESS_WRAP;
212 gl_tex->states[WINED3DTEXSTA_ADDRESSW] = WINED3D_TADDRESS_WRAP;
213 gl_tex->states[WINED3DTEXSTA_BORDERCOLOR] = 0;
214 gl_tex->states[WINED3DTEXSTA_MAGFILTER] = WINED3D_TEXF_LINEAR;
215 gl_tex->states[WINED3DTEXSTA_MINFILTER] = WINED3D_TEXF_POINT; /* GL_NEAREST_MIPMAP_LINEAR */
216 gl_tex->states[WINED3DTEXSTA_MIPFILTER] = WINED3D_TEXF_LINEAR; /* GL_NEAREST_MIPMAP_LINEAR */
217 gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL] = 0;
218 gl_tex->states[WINED3DTEXSTA_MAXANISOTROPY] = 1;
219 if (context->gl_info->supported[EXT_TEXTURE_SRGB_DECODE])
220 gl_tex->states[WINED3DTEXSTA_SRGBTEXTURE] = TRUE;
221 else
222 gl_tex->states[WINED3DTEXSTA_SRGBTEXTURE] = srgb;
223 gl_tex->states[WINED3DTEXSTA_SHADOW] = FALSE;
224 wined3d_texture_set_dirty(texture);
226 context_bind_texture(context, target, gl_tex->name);
228 if (texture->resource.usage & WINED3DUSAGE_AUTOGENMIPMAP)
230 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_GENERATE_MIPMAP_SGIS, GL_TRUE);
231 checkGLcall("glTexParameteri(target, GL_GENERATE_MIPMAP_SGIS, GL_TRUE)");
234 /* For a new texture we have to set the texture levels after binding the
235 * texture. Beware that texture rectangles do not support mipmapping, but
236 * set the maxmiplevel if we're relying on the partial
237 * GL_ARB_texture_non_power_of_two emulation with texture rectangles.
238 * (I.e., do not care about cond_np2 here, just look for
239 * GL_TEXTURE_RECTANGLE_ARB.) */
240 if (target != GL_TEXTURE_RECTANGLE_ARB)
242 TRACE("Setting GL_TEXTURE_MAX_LEVEL to %u.\n", texture->level_count - 1);
243 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, texture->level_count - 1);
244 checkGLcall("glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, texture->level_count)");
247 if (target == GL_TEXTURE_CUBE_MAP_ARB)
249 /* Cubemaps are always set to clamp, regardless of the sampler state. */
250 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
251 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
252 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
255 if (texture->flags & WINED3D_TEXTURE_COND_NP2)
257 /* Conditinal non power of two textures use a different clamping
258 * default. If we're using the GL_WINE_normalized_texrect partial
259 * driver emulation, we're dealing with a GL_TEXTURE_2D texture which
260 * has the address mode set to repeat - something that prevents us
261 * from hitting the accelerated codepath. Thus manually set the GL
262 * state. The same applies to filtering. Even if the texture has only
263 * one mip level, the default LINEAR_MIPMAP_LINEAR filter causes a SW
264 * fallback on macos. */
265 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
266 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
267 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
268 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
269 checkGLcall("glTexParameteri");
270 gl_tex->states[WINED3DTEXSTA_ADDRESSU] = WINED3D_TADDRESS_CLAMP;
271 gl_tex->states[WINED3DTEXSTA_ADDRESSV] = WINED3D_TADDRESS_CLAMP;
272 gl_tex->states[WINED3DTEXSTA_MAGFILTER] = WINED3D_TEXF_POINT;
273 gl_tex->states[WINED3DTEXSTA_MINFILTER] = WINED3D_TEXF_POINT;
274 gl_tex->states[WINED3DTEXSTA_MIPFILTER] = WINED3D_TEXF_NONE;
278 /* Context activation is done by the caller. */
279 void wined3d_texture_bind_and_dirtify(struct wined3d_texture *texture,
280 struct wined3d_context *context, BOOL srgb)
282 DWORD active_sampler;
284 /* We don't need a specific texture unit, but after binding the texture
285 * the current unit is dirty. Read the unit back instead of switching to
286 * 0, this avoids messing around with the state manager's GL states. The
287 * current texture unit should always be a valid one.
289 * To be more specific, this is tricky because we can implicitly be
290 * called from sampler() in state.c. This means we can't touch anything
291 * other than whatever happens to be the currently active texture, or we
292 * would risk marking already applied sampler states dirty again. */
293 active_sampler = context->rev_tex_unit_map[context->active_texture];
294 if (active_sampler != WINED3D_UNMAPPED_STAGE)
295 context_invalidate_state(context, STATE_SAMPLER(active_sampler));
297 wined3d_texture_bind(texture, context, srgb);
300 /* Context activation is done by the caller. */
301 static void apply_wrap(const struct wined3d_gl_info *gl_info, GLenum target,
302 enum wined3d_texture_address d3d_wrap, GLenum param, BOOL cond_np2)
304 GLint gl_wrap;
306 if (d3d_wrap < WINED3D_TADDRESS_WRAP || d3d_wrap > WINED3D_TADDRESS_MIRROR_ONCE)
308 FIXME("Unrecognized or unsupported texture address mode %#x.\n", d3d_wrap);
309 return;
312 /* Cubemaps are always set to clamp, regardless of the sampler state. */
313 if (target == GL_TEXTURE_CUBE_MAP_ARB
314 || (cond_np2 && d3d_wrap == WINED3D_TADDRESS_WRAP))
315 gl_wrap = GL_CLAMP_TO_EDGE;
316 else
317 gl_wrap = gl_info->wrap_lookup[d3d_wrap - WINED3D_TADDRESS_WRAP];
319 TRACE("Setting param %#x to %#x for target %#x.\n", param, gl_wrap, target);
320 gl_info->gl_ops.gl.p_glTexParameteri(target, param, gl_wrap);
321 checkGLcall("glTexParameteri(target, param, gl_wrap)");
324 /* Context activation is done by the caller (state handler). */
325 void wined3d_texture_apply_state_changes(struct wined3d_texture *texture,
326 const DWORD sampler_states[WINED3D_HIGHEST_SAMPLER_STATE + 1],
327 const struct wined3d_gl_info *gl_info)
329 BOOL cond_np2 = texture->flags & WINED3D_TEXTURE_COND_NP2;
330 GLenum target = texture->target;
331 struct gl_texture *gl_tex;
332 DWORD state;
333 DWORD aniso;
335 TRACE("texture %p, sampler_states %p.\n", texture, sampler_states);
337 gl_tex = wined3d_texture_get_gl_texture(texture, texture->flags & WINED3D_TEXTURE_IS_SRGB);
339 /* This function relies on the correct texture being bound and loaded. */
341 if (sampler_states[WINED3D_SAMP_ADDRESS_U] != gl_tex->states[WINED3DTEXSTA_ADDRESSU])
343 state = sampler_states[WINED3D_SAMP_ADDRESS_U];
344 apply_wrap(gl_info, target, state, GL_TEXTURE_WRAP_S, cond_np2);
345 gl_tex->states[WINED3DTEXSTA_ADDRESSU] = state;
348 if (sampler_states[WINED3D_SAMP_ADDRESS_V] != gl_tex->states[WINED3DTEXSTA_ADDRESSV])
350 state = sampler_states[WINED3D_SAMP_ADDRESS_V];
351 apply_wrap(gl_info, target, state, GL_TEXTURE_WRAP_T, cond_np2);
352 gl_tex->states[WINED3DTEXSTA_ADDRESSV] = state;
355 if (sampler_states[WINED3D_SAMP_ADDRESS_W] != gl_tex->states[WINED3DTEXSTA_ADDRESSW])
357 state = sampler_states[WINED3D_SAMP_ADDRESS_W];
358 apply_wrap(gl_info, target, state, GL_TEXTURE_WRAP_R, cond_np2);
359 gl_tex->states[WINED3DTEXSTA_ADDRESSW] = state;
362 if (sampler_states[WINED3D_SAMP_BORDER_COLOR] != gl_tex->states[WINED3DTEXSTA_BORDERCOLOR])
364 float col[4];
366 state = sampler_states[WINED3D_SAMP_BORDER_COLOR];
367 D3DCOLORTOGLFLOAT4(state, col);
368 TRACE("Setting border color for %#x to %#x.\n", target, state);
369 gl_info->gl_ops.gl.p_glTexParameterfv(target, GL_TEXTURE_BORDER_COLOR, &col[0]);
370 checkGLcall("glTexParameterfv(..., GL_TEXTURE_BORDER_COLOR, ...)");
371 gl_tex->states[WINED3DTEXSTA_BORDERCOLOR] = state;
374 if (sampler_states[WINED3D_SAMP_MAG_FILTER] != gl_tex->states[WINED3DTEXSTA_MAGFILTER])
376 GLint gl_value;
378 state = sampler_states[WINED3D_SAMP_MAG_FILTER];
379 if (state > WINED3D_TEXF_ANISOTROPIC)
380 FIXME("Unrecognized or unsupported MAGFILTER* value %d.\n", state);
382 gl_value = wined3d_gl_mag_filter(texture->mag_lookup,
383 min(max(state, WINED3D_TEXF_POINT), WINED3D_TEXF_LINEAR));
384 TRACE("ValueMAG=%#x setting MAGFILTER to %#x.\n", state, gl_value);
385 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAG_FILTER, gl_value);
387 gl_tex->states[WINED3DTEXSTA_MAGFILTER] = state;
390 if ((sampler_states[WINED3D_SAMP_MIN_FILTER] != gl_tex->states[WINED3DTEXSTA_MINFILTER]
391 || sampler_states[WINED3D_SAMP_MIP_FILTER] != gl_tex->states[WINED3DTEXSTA_MIPFILTER]
392 || sampler_states[WINED3D_SAMP_MAX_MIP_LEVEL] != gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL]))
394 GLint gl_value;
396 gl_tex->states[WINED3DTEXSTA_MIPFILTER] = sampler_states[WINED3D_SAMP_MIP_FILTER];
397 gl_tex->states[WINED3DTEXSTA_MINFILTER] = sampler_states[WINED3D_SAMP_MIN_FILTER];
398 gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL] = sampler_states[WINED3D_SAMP_MAX_MIP_LEVEL];
400 if (gl_tex->states[WINED3DTEXSTA_MINFILTER] > WINED3D_TEXF_ANISOTROPIC
401 || gl_tex->states[WINED3DTEXSTA_MIPFILTER] > WINED3D_TEXF_ANISOTROPIC)
403 FIXME("Unrecognized or unsupported MIN_FILTER value %#x MIP_FILTER value %#x.\n",
404 gl_tex->states[WINED3DTEXSTA_MINFILTER],
405 gl_tex->states[WINED3DTEXSTA_MIPFILTER]);
407 gl_value = wined3d_gl_min_mip_filter(texture->min_mip_lookup,
408 min(max(sampler_states[WINED3D_SAMP_MIN_FILTER], WINED3D_TEXF_POINT), WINED3D_TEXF_LINEAR),
409 min(max(sampler_states[WINED3D_SAMP_MIP_FILTER], WINED3D_TEXF_NONE), WINED3D_TEXF_LINEAR));
411 TRACE("ValueMIN=%#x, ValueMIP=%#x, setting MINFILTER to %#x.\n",
412 sampler_states[WINED3D_SAMP_MIN_FILTER],
413 sampler_states[WINED3D_SAMP_MIP_FILTER], gl_value);
414 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MIN_FILTER, gl_value);
415 checkGLcall("glTexParameter GL_TEXTURE_MIN_FILTER, ...");
417 if (!cond_np2)
419 if (gl_tex->states[WINED3DTEXSTA_MIPFILTER] == WINED3D_TEXF_NONE)
420 gl_value = texture->lod;
421 else if (gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL] >= texture->level_count)
422 gl_value = texture->level_count - 1;
423 else if (gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL] < texture->lod)
424 /* texture->lod is already clamped in the setter. */
425 gl_value = texture->lod;
426 else
427 gl_value = gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL];
429 /* Note that WINED3D_SAMP_MAX_MIP_LEVEL specifies the largest mipmap
430 * (default 0), while GL_TEXTURE_MAX_LEVEL specifies the smallest
431 * mimap used (default 1000). So WINED3D_SAMP_MAX_MIP_LEVEL
432 * corresponds to GL_TEXTURE_BASE_LEVEL. */
433 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_BASE_LEVEL, gl_value);
437 if ((gl_tex->states[WINED3DTEXSTA_MAGFILTER] != WINED3D_TEXF_ANISOTROPIC
438 && gl_tex->states[WINED3DTEXSTA_MINFILTER] != WINED3D_TEXF_ANISOTROPIC
439 && gl_tex->states[WINED3DTEXSTA_MIPFILTER] != WINED3D_TEXF_ANISOTROPIC)
440 || cond_np2)
441 aniso = 1;
442 else
443 aniso = sampler_states[WINED3D_SAMP_MAX_ANISOTROPY];
445 if (gl_tex->states[WINED3DTEXSTA_MAXANISOTROPY] != aniso)
447 if (gl_info->supported[EXT_TEXTURE_FILTER_ANISOTROPIC])
449 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAX_ANISOTROPY_EXT, aniso);
450 checkGLcall("glTexParameteri(GL_TEXTURE_MAX_ANISOTROPY_EXT, aniso)");
452 else
454 WARN("Anisotropic filtering not supported.\n");
456 gl_tex->states[WINED3DTEXSTA_MAXANISOTROPY] = aniso;
459 /* These should always be the same unless EXT_texture_sRGB_decode is supported. */
460 if (sampler_states[WINED3D_SAMP_SRGB_TEXTURE] != gl_tex->states[WINED3DTEXSTA_SRGBTEXTURE])
462 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_SRGB_DECODE_EXT,
463 sampler_states[WINED3D_SAMP_SRGB_TEXTURE] ? GL_DECODE_EXT : GL_SKIP_DECODE_EXT);
464 checkGLcall("glTexParameteri(GL_TEXTURE_SRGB_DECODE_EXT)");
465 gl_tex->states[WINED3DTEXSTA_SRGBTEXTURE] = sampler_states[WINED3D_SAMP_SRGB_TEXTURE];
468 if (!(texture->resource.format->flags & WINED3DFMT_FLAG_SHADOW)
469 != !gl_tex->states[WINED3DTEXSTA_SHADOW])
471 if (texture->resource.format->flags & WINED3DFMT_FLAG_SHADOW)
473 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_DEPTH_TEXTURE_MODE_ARB, GL_LUMINANCE);
474 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE_ARB);
475 checkGLcall("glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE_ARB)");
476 gl_tex->states[WINED3DTEXSTA_SHADOW] = TRUE;
478 else
480 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_NONE);
481 checkGLcall("glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_NONE)");
482 gl_tex->states[WINED3DTEXSTA_SHADOW] = FALSE;
487 ULONG CDECL wined3d_texture_incref(struct wined3d_texture *texture)
489 ULONG refcount;
491 TRACE("texture %p, swapchain %p.\n", texture, texture->swapchain);
493 if (texture->swapchain)
494 return wined3d_swapchain_incref(texture->swapchain);
496 refcount = InterlockedIncrement(&texture->resource.ref);
497 TRACE("%p increasing refcount to %u.\n", texture, refcount);
499 return refcount;
502 ULONG CDECL wined3d_texture_decref(struct wined3d_texture *texture)
504 ULONG refcount;
506 TRACE("texture %p, swapchain %p.\n", texture, texture->swapchain);
508 if (texture->swapchain)
509 return wined3d_swapchain_decref(texture->swapchain);
511 refcount = InterlockedDecrement(&texture->resource.ref);
512 TRACE("%p decreasing refcount to %u.\n", texture, refcount);
514 if (!refcount)
516 wined3d_texture_cleanup(texture);
517 texture->resource.parent_ops->wined3d_object_destroyed(texture->resource.parent);
518 HeapFree(GetProcessHeap(), 0, texture);
521 return refcount;
524 struct wined3d_resource * CDECL wined3d_texture_get_resource(struct wined3d_texture *texture)
526 TRACE("texture %p.\n", texture);
528 return &texture->resource;
531 /* Context activation is done by the caller */
532 void wined3d_texture_load(struct wined3d_texture *texture,
533 struct wined3d_context *context, BOOL srgb)
535 UINT sub_count = texture->level_count * texture->layer_count;
536 const struct wined3d_gl_info *gl_info = context->gl_info;
537 DWORD flag;
538 UINT i;
540 TRACE("texture %p, srgb %#x.\n", texture, srgb);
542 if (gl_info->supported[EXT_TEXTURE_SRGB_DECODE])
543 srgb = FALSE;
545 if (srgb)
546 flag = WINED3D_TEXTURE_SRGB_VALID;
547 else
548 flag = WINED3D_TEXTURE_RGB_VALID;
550 if (texture->flags & flag)
552 TRACE("Texture %p not dirty, nothing to do.\n", texture);
553 return;
556 /* Reload the surfaces if the texture is marked dirty. */
557 for (i = 0; i < sub_count; ++i)
559 texture->texture_ops->texture_sub_resource_load(texture->sub_resources[i], context, srgb);
561 texture->flags |= flag;
564 void CDECL wined3d_texture_preload(struct wined3d_texture *texture)
566 struct wined3d_context *context;
567 context = context_acquire(texture->resource.device, NULL);
568 wined3d_texture_load(texture, context, texture->flags & WINED3D_TEXTURE_IS_SRGB);
569 context_release(context);
572 void * CDECL wined3d_texture_get_parent(const struct wined3d_texture *texture)
574 TRACE("texture %p.\n", texture);
576 return texture->resource.parent;
579 DWORD CDECL wined3d_texture_set_lod(struct wined3d_texture *texture, DWORD lod)
581 DWORD old = texture->lod;
583 TRACE("texture %p, lod %u.\n", texture, lod);
585 /* The d3d9:texture test shows that SetLOD is ignored on non-managed
586 * textures. The call always returns 0, and GetLOD always returns 0. */
587 if (texture->resource.pool != WINED3D_POOL_MANAGED)
589 TRACE("Ignoring SetLOD on %s texture, returning 0.\n", debug_d3dpool(texture->resource.pool));
590 return 0;
593 if (lod >= texture->level_count)
594 lod = texture->level_count - 1;
596 if (texture->lod != lod)
598 texture->lod = lod;
600 texture->texture_rgb.states[WINED3DTEXSTA_MAXMIPLEVEL] = ~0U;
601 texture->texture_srgb.states[WINED3DTEXSTA_MAXMIPLEVEL] = ~0U;
602 if (texture->resource.bind_count)
603 device_invalidate_state(texture->resource.device, STATE_SAMPLER(texture->sampler));
606 return old;
609 DWORD CDECL wined3d_texture_get_lod(const struct wined3d_texture *texture)
611 TRACE("texture %p, returning %u.\n", texture, texture->lod);
613 return texture->lod;
616 DWORD CDECL wined3d_texture_get_level_count(const struct wined3d_texture *texture)
618 TRACE("texture %p, returning %u.\n", texture, texture->level_count);
620 return texture->level_count;
623 HRESULT CDECL wined3d_texture_set_autogen_filter_type(struct wined3d_texture *texture,
624 enum wined3d_texture_filter_type filter_type)
626 FIXME("texture %p, filter_type %s stub!\n", texture, debug_d3dtexturefiltertype(filter_type));
628 if (!(texture->resource.usage & WINED3DUSAGE_AUTOGENMIPMAP))
630 WARN("Texture doesn't have AUTOGENMIPMAP usage.\n");
631 return WINED3DERR_INVALIDCALL;
634 texture->filter_type = filter_type;
636 return WINED3D_OK;
639 enum wined3d_texture_filter_type CDECL wined3d_texture_get_autogen_filter_type(const struct wined3d_texture *texture)
641 TRACE("texture %p.\n", texture);
643 return texture->filter_type;
646 HRESULT CDECL wined3d_texture_set_color_key(struct wined3d_texture *texture,
647 DWORD flags, const struct wined3d_color_key *color_key)
649 TRACE("texture %p, flags %#x, color_key %p.\n", texture, flags, color_key);
651 if (flags & WINEDDCKEY_COLORSPACE)
653 FIXME("Unhandled flags %#x.\n", flags);
654 return WINED3DERR_INVALIDCALL;
657 if (color_key)
659 switch (flags & ~WINEDDCKEY_COLORSPACE)
661 case WINEDDCKEY_DESTBLT:
662 texture->dst_blt_color_key = *color_key;
663 texture->color_key_flags |= WINEDDSD_CKDESTBLT;
664 break;
666 case WINEDDCKEY_DESTOVERLAY:
667 texture->dst_overlay_color_key = *color_key;
668 texture->color_key_flags |= WINEDDSD_CKDESTOVERLAY;
669 break;
671 case WINEDDCKEY_SRCOVERLAY:
672 texture->src_overlay_color_key = *color_key;
673 texture->color_key_flags |= WINEDDSD_CKSRCOVERLAY;
674 break;
676 case WINEDDCKEY_SRCBLT:
677 texture->src_blt_color_key = *color_key;
678 texture->color_key_flags |= WINEDDSD_CKSRCBLT;
679 break;
682 else
684 switch (flags & ~WINEDDCKEY_COLORSPACE)
686 case WINEDDCKEY_DESTBLT:
687 texture->color_key_flags &= ~WINEDDSD_CKDESTBLT;
688 break;
690 case WINEDDCKEY_DESTOVERLAY:
691 texture->color_key_flags &= ~WINEDDSD_CKDESTOVERLAY;
692 break;
694 case WINEDDCKEY_SRCOVERLAY:
695 texture->color_key_flags &= ~WINEDDSD_CKSRCOVERLAY;
696 break;
698 case WINEDDCKEY_SRCBLT:
699 texture->color_key_flags &= ~WINEDDSD_CKSRCBLT;
700 break;
704 return WINED3D_OK;
707 HRESULT CDECL wined3d_texture_update_desc(struct wined3d_texture *texture, UINT width, UINT height,
708 enum wined3d_format_id format_id, enum wined3d_multisample_type multisample_type,
709 UINT multisample_quality, void *mem, UINT pitch)
711 struct wined3d_device *device = texture->resource.device;
712 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
713 const struct wined3d_format *format = wined3d_get_format(gl_info, format_id);
714 UINT resource_size = wined3d_format_calculate_size(format, device->surface_alignment, width, height, 1);
715 struct wined3d_surface *surface;
717 TRACE("texture %p, width %u, height %u, format %s, multisample_type %#x, multisample_quality %u, "
718 "mem %p, pitch %u.\n",
719 texture, width, height, debug_d3dformat(format_id), multisample_type, multisample_type, mem, pitch);
721 if (!resource_size)
722 return WINED3DERR_INVALIDCALL;
724 if (texture->level_count * texture->layer_count > 1)
726 WARN("Texture has multiple sub-resources, not supported.\n");
727 return WINED3DERR_INVALIDCALL;
730 if (texture->resource.type == WINED3D_RTYPE_VOLUME_TEXTURE)
732 WARN("Not supported on volume textures.\n");
733 return WINED3DERR_INVALIDCALL;
736 surface = surface_from_resource(texture->sub_resources[0]);
737 if (surface->resource.map_count || (surface->flags & SFLAG_DCINUSE))
739 WARN("Surface is mapped or the DC is in use.\n");
740 return WINED3DERR_INVALIDCALL;
743 if (device->d3d_initialized)
744 texture->resource.resource_ops->resource_unload(&texture->resource);
746 texture->resource.format = format;
747 texture->resource.multisample_type = multisample_type;
748 texture->resource.multisample_quality = multisample_quality;
749 texture->resource.width = width;
750 texture->resource.height = height;
752 return wined3d_surface_update_desc(surface, gl_info, mem, pitch);
755 void wined3d_texture_prepare_texture(struct wined3d_texture *texture, struct wined3d_context *context, BOOL srgb)
757 DWORD alloc_flag = srgb ? WINED3D_TEXTURE_SRGB_ALLOCATED : WINED3D_TEXTURE_RGB_ALLOCATED;
759 if (texture->flags & alloc_flag)
760 return;
762 texture->texture_ops->texture_prepare_texture(texture, context, srgb);
763 texture->flags |= alloc_flag;
766 void wined3d_texture_force_reload(struct wined3d_texture *texture)
768 unsigned int sub_count = texture->level_count * texture->layer_count;
769 unsigned int i;
771 texture->flags &= ~(WINED3D_TEXTURE_RGB_ALLOCATED | WINED3D_TEXTURE_SRGB_ALLOCATED | WINED3D_TEXTURE_CONVERTED);
772 for (i = 0; i < sub_count; ++i)
774 texture->texture_ops->texture_sub_resource_invalidate_location(texture->sub_resources[i],
775 WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB);
779 void CDECL wined3d_texture_generate_mipmaps(struct wined3d_texture *texture)
781 /* TODO: Implement filters using GL_SGI_generate_mipmaps. */
782 FIXME("texture %p stub!\n", texture);
785 struct wined3d_resource * CDECL wined3d_texture_get_sub_resource(struct wined3d_texture *texture,
786 UINT sub_resource_idx)
788 UINT sub_count = texture->level_count * texture->layer_count;
790 TRACE("texture %p, sub_resource_idx %u.\n", texture, sub_resource_idx);
792 if (sub_resource_idx >= sub_count)
794 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx, sub_count);
795 return NULL;
798 return texture->sub_resources[sub_resource_idx];
801 HRESULT CDECL wined3d_texture_add_dirty_region(struct wined3d_texture *texture,
802 UINT layer, const struct wined3d_box *dirty_region)
804 struct wined3d_resource *sub_resource;
806 TRACE("texture %p, layer %u, dirty_region %p.\n", texture, layer, dirty_region);
808 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, layer * texture->level_count)))
810 WARN("Failed to get sub-resource.\n");
811 return WINED3DERR_INVALIDCALL;
814 texture->texture_ops->texture_sub_resource_add_dirty_region(sub_resource, dirty_region);
816 return WINED3D_OK;
819 static void texture2d_sub_resource_load(struct wined3d_resource *sub_resource,
820 struct wined3d_context *context, BOOL srgb)
822 surface_load(surface_from_resource(sub_resource), srgb);
825 static void texture2d_sub_resource_add_dirty_region(struct wined3d_resource *sub_resource,
826 const struct wined3d_box *dirty_region)
828 struct wined3d_surface *surface = surface_from_resource(sub_resource);
830 surface_prepare_map_memory(surface);
831 surface_load_location(surface, surface->resource.map_binding);
832 surface_invalidate_location(surface, ~surface->resource.map_binding);
835 static void texture2d_sub_resource_cleanup(struct wined3d_resource *sub_resource)
837 struct wined3d_surface *surface = surface_from_resource(sub_resource);
839 wined3d_surface_destroy(surface);
842 static void texture2d_sub_resource_invalidate_location(struct wined3d_resource *sub_resource, DWORD location)
844 struct wined3d_surface *surface = surface_from_resource(sub_resource);
846 surface_invalidate_location(surface, location);
849 /* Context activation is done by the caller. */
850 static void texture2d_prepare_texture(struct wined3d_texture *texture, struct wined3d_context *context, BOOL srgb)
852 UINT sub_count = texture->level_count * texture->layer_count;
853 const struct wined3d_format *format = texture->resource.format;
854 const struct wined3d_gl_info *gl_info = context->gl_info;
855 const struct wined3d_color_key_conversion *conversion;
856 GLenum internal;
857 UINT i;
859 TRACE("texture %p, format %s.\n", texture, debug_d3dformat(format->id));
861 if (format->convert)
863 texture->flags |= WINED3D_TEXTURE_CONVERTED;
865 else if ((conversion = wined3d_format_get_color_key_conversion(texture, TRUE)))
867 texture->flags |= WINED3D_TEXTURE_CONVERTED;
868 format = wined3d_get_format(gl_info, conversion->dst_format);
869 TRACE("Using format %s for color key conversion.\n", debug_d3dformat(format->id));
872 wined3d_texture_bind_and_dirtify(texture, context, srgb);
874 if (srgb)
875 internal = format->glGammaInternal;
876 else if (texture->resource.usage & WINED3DUSAGE_RENDERTARGET
877 && wined3d_resource_is_offscreen(&texture->resource))
878 internal = format->rtInternal;
879 else
880 internal = format->glInternal;
882 if (!internal)
883 FIXME("No GL internal format for format %s.\n", debug_d3dformat(format->id));
885 TRACE("internal %#x, format %#x, type %#x.\n", internal, format->glFormat, format->glType);
887 for (i = 0; i < sub_count; ++i)
889 struct wined3d_surface *surface = surface_from_resource(texture->sub_resources[i]);
890 GLsizei height = surface->pow2Height;
891 GLsizei width = surface->pow2Width;
892 const BYTE *mem = NULL;
894 if (format->flags & WINED3DFMT_FLAG_HEIGHT_SCALE)
896 height *= format->height_scale.numerator;
897 height /= format->height_scale.denominator;
900 TRACE("surface %p, target %#x, level %d, width %d, height %d.\n",
901 surface, surface->texture_target, surface->texture_level, width, height);
903 if (gl_info->supported[APPLE_CLIENT_STORAGE])
905 if (surface->flags & (SFLAG_NONPOW2 | SFLAG_DIBSECTION)
906 || texture->flags & WINED3D_TEXTURE_CONVERTED
907 || !surface->resource.heap_memory)
909 /* In some cases we want to disable client storage.
910 * SFLAG_NONPOW2 has a bigger opengl texture than the client memory, and different pitches
911 * SFLAG_DIBSECTION: Dibsections may have read / write protections on the memory. Avoid issues...
912 * WINED3D_TEXTURE_CONVERTED: The conversion destination memory is freed after loading the surface
913 * heap_memory == NULL: Not defined in the extension. Seems to disable client storage effectively
915 surface->flags &= ~SFLAG_CLIENT;
917 else
919 surface->flags |= SFLAG_CLIENT;
920 mem = surface->resource.heap_memory;
922 gl_info->gl_ops.gl.p_glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE);
923 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE)");
927 if (format->flags & WINED3DFMT_FLAG_COMPRESSED && mem)
929 GL_EXTCALL(glCompressedTexImage2DARB(surface->texture_target, surface->texture_level,
930 internal, width, height, 0, surface->resource.size, mem));
931 checkGLcall("glCompressedTexImage2DARB");
933 else
935 gl_info->gl_ops.gl.p_glTexImage2D(surface->texture_target, surface->texture_level,
936 internal, width, height, 0, format->glFormat, format->glType, mem);
937 checkGLcall("glTexImage2D");
940 if (mem)
942 gl_info->gl_ops.gl.p_glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE);
943 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE)");
948 static const struct wined3d_texture_ops texture2d_ops =
950 texture2d_sub_resource_load,
951 texture2d_sub_resource_add_dirty_region,
952 texture2d_sub_resource_cleanup,
953 texture2d_sub_resource_invalidate_location,
954 texture2d_prepare_texture,
957 static ULONG texture_resource_incref(struct wined3d_resource *resource)
959 return wined3d_texture_incref(wined3d_texture_from_resource(resource));
962 static ULONG texture_resource_decref(struct wined3d_resource *resource)
964 return wined3d_texture_decref(wined3d_texture_from_resource(resource));
967 static void wined3d_texture_unload(struct wined3d_resource *resource)
969 struct wined3d_texture *texture = wined3d_texture_from_resource(resource);
970 UINT sub_count = texture->level_count * texture->layer_count;
971 UINT i;
973 TRACE("texture %p.\n", texture);
975 for (i = 0; i < sub_count; ++i)
977 struct wined3d_resource *sub_resource = texture->sub_resources[i];
979 sub_resource->resource_ops->resource_unload(sub_resource);
982 wined3d_texture_unload_gl_texture(texture);
985 static const struct wined3d_resource_ops texture_resource_ops =
987 texture_resource_incref,
988 texture_resource_decref,
989 wined3d_texture_unload,
992 static HRESULT cubetexture_init(struct wined3d_texture *texture, const struct wined3d_resource_desc *desc,
993 UINT levels, DWORD surface_flags, struct wined3d_device *device, void *parent,
994 const struct wined3d_parent_ops *parent_ops)
996 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
997 struct wined3d_resource_desc surface_desc;
998 unsigned int i, j;
999 HRESULT hr;
1001 /* TODO: It should only be possible to create textures for formats
1002 * that are reported as supported. */
1003 if (WINED3DFMT_UNKNOWN >= desc->format)
1005 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture);
1006 return WINED3DERR_INVALIDCALL;
1009 if (!gl_info->supported[ARB_TEXTURE_CUBE_MAP] && desc->pool != WINED3D_POOL_SCRATCH)
1011 WARN("(%p) : Tried to create not supported cube texture.\n", texture);
1012 return WINED3DERR_INVALIDCALL;
1015 /* Calculate levels for mip mapping */
1016 if (desc->usage & WINED3DUSAGE_AUTOGENMIPMAP)
1018 if (!gl_info->supported[SGIS_GENERATE_MIPMAP])
1020 WARN("No mipmap generation support, returning D3DERR_INVALIDCALL.\n");
1021 return WINED3DERR_INVALIDCALL;
1024 if (levels > 1)
1026 WARN("D3DUSAGE_AUTOGENMIPMAP is set, and level count > 1, returning D3DERR_INVALIDCALL.\n");
1027 return WINED3DERR_INVALIDCALL;
1030 levels = 1;
1032 else if (!levels)
1034 levels = wined3d_log2i(desc->width) + 1;
1035 TRACE("Calculated levels = %u.\n", levels);
1038 if (!gl_info->supported[ARB_TEXTURE_NON_POWER_OF_TWO])
1040 UINT pow2_edge_length = 1;
1041 while (pow2_edge_length < desc->width)
1042 pow2_edge_length <<= 1;
1044 if (desc->width != pow2_edge_length)
1046 if (desc->pool == WINED3D_POOL_SCRATCH)
1048 /* SCRATCH textures cannot be used for texturing */
1049 WARN("Creating a scratch NPOT cube texture despite lack of HW support.\n");
1051 else
1053 WARN("Attempted to create a NPOT cube texture (edge length %u) without GL support.\n", desc->width);
1054 return WINED3DERR_INVALIDCALL;
1059 if (FAILED(hr = wined3d_texture_init(texture, &texture2d_ops, 6, levels, desc,
1060 surface_flags, device, parent, parent_ops, &texture_resource_ops)))
1062 WARN("Failed to initialize texture, returning %#x\n", hr);
1063 return hr;
1066 texture->pow2_matrix[0] = 1.0f;
1067 texture->pow2_matrix[5] = 1.0f;
1068 texture->pow2_matrix[10] = 1.0f;
1069 texture->pow2_matrix[15] = 1.0f;
1070 texture->target = GL_TEXTURE_CUBE_MAP_ARB;
1072 /* Generate all the surfaces. */
1073 surface_desc = *desc;
1074 surface_desc.resource_type = WINED3D_RTYPE_SURFACE;
1075 for (i = 0; i < texture->level_count; ++i)
1077 /* Create the 6 faces. */
1078 for (j = 0; j < 6; ++j)
1080 static const GLenum cube_targets[6] =
1082 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB,
1083 GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB,
1084 GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB,
1085 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB,
1086 GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB,
1087 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB,
1089 UINT idx = j * texture->level_count + i;
1090 struct wined3d_surface *surface;
1092 if (FAILED(hr = wined3d_surface_create(texture, &surface_desc,
1093 cube_targets[j], i, j, surface_flags, &surface)))
1095 WARN("Failed to create surface, hr %#x.\n", hr);
1096 wined3d_texture_cleanup(texture);
1097 return hr;
1100 texture->sub_resources[idx] = &surface->resource;
1101 TRACE("Created surface level %u @ %p.\n", i, surface);
1103 surface_desc.width = max(1, surface_desc.width >> 1);
1104 surface_desc.height = surface_desc.width;
1107 return WINED3D_OK;
1110 static HRESULT texture_init(struct wined3d_texture *texture, const struct wined3d_resource_desc *desc,
1111 UINT levels, DWORD surface_flags, struct wined3d_device *device, void *parent,
1112 const struct wined3d_parent_ops *parent_ops)
1114 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
1115 struct wined3d_resource_desc surface_desc;
1116 UINT pow2_width, pow2_height;
1117 unsigned int i;
1118 HRESULT hr;
1120 /* TODO: It should only be possible to create textures for formats
1121 * that are reported as supported. */
1122 if (WINED3DFMT_UNKNOWN >= desc->format)
1124 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture);
1125 return WINED3DERR_INVALIDCALL;
1128 /* Non-power2 support. */
1129 if (gl_info->supported[ARB_TEXTURE_NON_POWER_OF_TWO])
1131 pow2_width = desc->width;
1132 pow2_height = desc->height;
1134 else
1136 /* Find the nearest pow2 match. */
1137 pow2_width = pow2_height = 1;
1138 while (pow2_width < desc->width)
1139 pow2_width <<= 1;
1140 while (pow2_height < desc->height)
1141 pow2_height <<= 1;
1143 if (pow2_width != desc->width || pow2_height != desc->height)
1145 /* levels == 0 returns an error as well */
1146 if (levels != 1)
1148 if (desc->pool == WINED3D_POOL_SCRATCH)
1150 WARN("Creating a scratch mipmapped NPOT texture despite lack of HW support.\n");
1152 else
1154 WARN("Attempted to create a mipmapped NPOT texture without unconditional NPOT support.\n");
1155 return WINED3DERR_INVALIDCALL;
1161 /* Calculate levels for mip mapping. */
1162 if (desc->usage & WINED3DUSAGE_AUTOGENMIPMAP)
1164 if (!gl_info->supported[SGIS_GENERATE_MIPMAP])
1166 WARN("No mipmap generation support, returning WINED3DERR_INVALIDCALL.\n");
1167 return WINED3DERR_INVALIDCALL;
1170 if (levels > 1)
1172 WARN("D3DUSAGE_AUTOGENMIPMAP is set, and level count > 1, returning WINED3DERR_INVALIDCALL.\n");
1173 return WINED3DERR_INVALIDCALL;
1176 levels = 1;
1178 else if (!levels)
1180 levels = wined3d_log2i(max(desc->width, desc->height)) + 1;
1181 TRACE("Calculated levels = %u.\n", levels);
1184 if (FAILED(hr = wined3d_texture_init(texture, &texture2d_ops, 1, levels, desc,
1185 surface_flags, device, parent, parent_ops, &texture_resource_ops)))
1187 WARN("Failed to initialize texture, returning %#x.\n", hr);
1188 return hr;
1191 /* Precalculated scaling for 'faked' non power of two texture coords. */
1192 if (gl_info->supported[WINED3D_GL_NORMALIZED_TEXRECT]
1193 && (desc->width != pow2_width || desc->height != pow2_height))
1195 texture->pow2_matrix[0] = 1.0f;
1196 texture->pow2_matrix[5] = 1.0f;
1197 texture->pow2_matrix[10] = 1.0f;
1198 texture->pow2_matrix[15] = 1.0f;
1199 texture->target = GL_TEXTURE_2D;
1200 texture->flags |= WINED3D_TEXTURE_COND_NP2;
1201 texture->min_mip_lookup = minMipLookup_noFilter;
1203 else if (gl_info->supported[ARB_TEXTURE_RECTANGLE]
1204 && (desc->width != pow2_width || desc->height != pow2_height))
1206 texture->pow2_matrix[0] = (float)desc->width;
1207 texture->pow2_matrix[5] = (float)desc->height;
1208 texture->pow2_matrix[10] = 1.0f;
1209 texture->pow2_matrix[15] = 1.0f;
1210 texture->target = GL_TEXTURE_RECTANGLE_ARB;
1211 texture->flags |= WINED3D_TEXTURE_COND_NP2;
1212 texture->flags &= ~(WINED3D_TEXTURE_POW2_MAT_IDENT | WINED3D_TEXTURE_NORMALIZED_COORDS);
1214 if (texture->resource.format->flags & WINED3DFMT_FLAG_FILTERING)
1215 texture->min_mip_lookup = minMipLookup_noMip;
1216 else
1217 texture->min_mip_lookup = minMipLookup_noFilter;
1219 else
1221 if ((desc->width != pow2_width) || (desc->height != pow2_height))
1223 texture->pow2_matrix[0] = (((float)desc->width) / ((float)pow2_width));
1224 texture->pow2_matrix[5] = (((float)desc->height) / ((float)pow2_height));
1225 texture->flags &= ~WINED3D_TEXTURE_POW2_MAT_IDENT;
1227 else
1229 texture->pow2_matrix[0] = 1.0f;
1230 texture->pow2_matrix[5] = 1.0f;
1233 texture->pow2_matrix[10] = 1.0f;
1234 texture->pow2_matrix[15] = 1.0f;
1235 texture->target = GL_TEXTURE_2D;
1237 TRACE("xf(%f) yf(%f)\n", texture->pow2_matrix[0], texture->pow2_matrix[5]);
1239 /* Generate all the surfaces. */
1240 surface_desc = *desc;
1241 surface_desc.resource_type = WINED3D_RTYPE_SURFACE;
1242 for (i = 0; i < texture->level_count; ++i)
1244 struct wined3d_surface *surface;
1246 if (FAILED(hr = wined3d_surface_create(texture, &surface_desc,
1247 texture->target, i, 0, surface_flags, &surface)))
1249 WARN("Failed to create surface, hr %#x.\n", hr);
1250 wined3d_texture_cleanup(texture);
1251 return hr;
1254 texture->sub_resources[i] = &surface->resource;
1255 TRACE("Created surface level %u @ %p.\n", i, surface);
1256 /* Calculate the next mipmap level. */
1257 surface_desc.width = max(1, surface_desc.width >> 1);
1258 surface_desc.height = max(1, surface_desc.height >> 1);
1261 return WINED3D_OK;
1264 static void texture3d_sub_resource_load(struct wined3d_resource *sub_resource,
1265 struct wined3d_context *context, BOOL srgb)
1267 wined3d_volume_load(volume_from_resource(sub_resource), context, srgb);
1270 static void texture3d_sub_resource_add_dirty_region(struct wined3d_resource *sub_resource,
1271 const struct wined3d_box *dirty_region)
1273 wined3d_texture_set_dirty(volume_from_resource(sub_resource)->container);
1276 static void texture3d_sub_resource_cleanup(struct wined3d_resource *sub_resource)
1278 struct wined3d_volume *volume = volume_from_resource(sub_resource);
1280 wined3d_volume_destroy(volume);
1283 static void texture3d_sub_resource_invalidate_location(struct wined3d_resource *sub_resource, DWORD location)
1285 struct wined3d_volume *volume = volume_from_resource(sub_resource);
1287 wined3d_volume_invalidate_location(volume, location);
1290 static void texture3d_prepare_texture(struct wined3d_texture *texture, struct wined3d_context *context, BOOL srgb)
1292 unsigned int sub_count = texture->level_count * texture->layer_count;
1293 const struct wined3d_format *format = texture->resource.format;
1294 const struct wined3d_gl_info *gl_info = context->gl_info;
1295 unsigned int i;
1297 wined3d_texture_bind_and_dirtify(texture, context, srgb);
1299 for (i = 0; i < sub_count; ++i)
1301 struct wined3d_volume *volume = volume_from_resource(texture->sub_resources[i]);
1302 void *mem = NULL;
1304 if (gl_info->supported[APPLE_CLIENT_STORAGE] && !format->convert
1305 && volume_prepare_system_memory(volume))
1307 TRACE("Enabling GL_UNPACK_CLIENT_STORAGE_APPLE for volume %p\n", volume);
1308 gl_info->gl_ops.gl.p_glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE);
1309 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE)");
1310 mem = volume->resource.heap_memory;
1311 volume->flags |= WINED3D_VFLAG_CLIENT_STORAGE;
1314 GL_EXTCALL(glTexImage3DEXT(GL_TEXTURE_3D, volume->texture_level,
1315 srgb ? format->glGammaInternal : format->glInternal,
1316 volume->resource.width, volume->resource.height, volume->resource.depth,
1317 0, format->glFormat, format->glType, mem));
1318 checkGLcall("glTexImage3D");
1320 if (mem)
1322 gl_info->gl_ops.gl.p_glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE);
1323 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE)");
1328 static const struct wined3d_texture_ops texture3d_ops =
1330 texture3d_sub_resource_load,
1331 texture3d_sub_resource_add_dirty_region,
1332 texture3d_sub_resource_cleanup,
1333 texture3d_sub_resource_invalidate_location,
1334 texture3d_prepare_texture,
1337 static HRESULT volumetexture_init(struct wined3d_texture *texture, const struct wined3d_resource_desc *desc,
1338 UINT levels, struct wined3d_device *device, void *parent, const struct wined3d_parent_ops *parent_ops)
1340 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
1341 struct wined3d_resource_desc volume_desc;
1342 unsigned int i;
1343 HRESULT hr;
1345 /* TODO: It should only be possible to create textures for formats
1346 * that are reported as supported. */
1347 if (WINED3DFMT_UNKNOWN >= desc->format)
1349 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture);
1350 return WINED3DERR_INVALIDCALL;
1353 if (!gl_info->supported[EXT_TEXTURE3D])
1355 WARN("(%p) : Texture cannot be created - no volume texture support.\n", texture);
1356 return WINED3DERR_INVALIDCALL;
1359 /* Calculate levels for mip mapping. */
1360 if (desc->usage & WINED3DUSAGE_AUTOGENMIPMAP)
1362 if (!gl_info->supported[SGIS_GENERATE_MIPMAP])
1364 WARN("No mipmap generation support, returning D3DERR_INVALIDCALL.\n");
1365 return WINED3DERR_INVALIDCALL;
1368 if (levels > 1)
1370 WARN("D3DUSAGE_AUTOGENMIPMAP is set, and level count > 1, returning D3DERR_INVALIDCALL.\n");
1371 return WINED3DERR_INVALIDCALL;
1374 levels = 1;
1376 else if (!levels)
1378 levels = wined3d_log2i(max(max(desc->width, desc->height), desc->depth)) + 1;
1379 TRACE("Calculated levels = %u.\n", levels);
1382 if (!gl_info->supported[ARB_TEXTURE_NON_POWER_OF_TWO])
1384 UINT pow2_w, pow2_h, pow2_d;
1385 pow2_w = 1;
1386 while (pow2_w < desc->width)
1387 pow2_w <<= 1;
1388 pow2_h = 1;
1389 while (pow2_h < desc->height)
1390 pow2_h <<= 1;
1391 pow2_d = 1;
1392 while (pow2_d < desc->depth)
1393 pow2_d <<= 1;
1395 if (pow2_w != desc->width || pow2_h != desc->height || pow2_d != desc->depth)
1397 if (desc->pool == WINED3D_POOL_SCRATCH)
1399 WARN("Creating a scratch NPOT volume texture despite lack of HW support.\n");
1401 else
1403 WARN("Attempted to create a NPOT volume texture (%u, %u, %u) without GL support.\n",
1404 desc->width, desc->height, desc->depth);
1405 return WINED3DERR_INVALIDCALL;
1410 if (FAILED(hr = wined3d_texture_init(texture, &texture3d_ops, 1, levels, desc,
1411 0, device, parent, parent_ops, &texture_resource_ops)))
1413 WARN("Failed to initialize texture, returning %#x.\n", hr);
1414 return hr;
1417 texture->pow2_matrix[0] = 1.0f;
1418 texture->pow2_matrix[5] = 1.0f;
1419 texture->pow2_matrix[10] = 1.0f;
1420 texture->pow2_matrix[15] = 1.0f;
1421 texture->target = GL_TEXTURE_3D;
1423 /* Generate all the surfaces. */
1424 volume_desc = *desc;
1425 volume_desc.resource_type = WINED3D_RTYPE_VOLUME;
1426 for (i = 0; i < texture->level_count; ++i)
1428 struct wined3d_volume *volume;
1430 if (FAILED(hr = wined3d_volume_create(texture, &volume_desc, i, &volume)))
1432 ERR("Creating a volume for the volume texture failed, hr %#x.\n", hr);
1433 wined3d_texture_cleanup(texture);
1434 return hr;
1437 texture->sub_resources[i] = &volume->resource;
1439 /* Calculate the next mipmap level. */
1440 volume_desc.width = max(1, volume_desc.width >> 1);
1441 volume_desc.height = max(1, volume_desc.height >> 1);
1442 volume_desc.depth = max(1, volume_desc.depth >> 1);
1445 return WINED3D_OK;
1448 HRESULT CDECL wined3d_texture_create(struct wined3d_device *device, const struct wined3d_resource_desc *desc,
1449 UINT level_count, DWORD surface_flags, void *parent, const struct wined3d_parent_ops *parent_ops,
1450 struct wined3d_texture **texture)
1452 struct wined3d_texture *object;
1453 HRESULT hr;
1455 TRACE("device %p, desc %p, level_count %u, surface_flags %#x, parent %p, parent_ops %p, texture %p.\n",
1456 device, desc, level_count, surface_flags, parent, parent_ops, texture);
1458 if (!(object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object))))
1459 return E_OUTOFMEMORY;
1461 switch (desc->resource_type)
1463 case WINED3D_RTYPE_TEXTURE:
1464 hr = texture_init(object, desc, level_count, surface_flags, device, parent, parent_ops);
1465 break;
1467 case WINED3D_RTYPE_VOLUME_TEXTURE:
1468 hr = volumetexture_init(object, desc, level_count, device, parent, parent_ops);
1469 break;
1471 case WINED3D_RTYPE_CUBE_TEXTURE:
1472 hr = cubetexture_init(object, desc, level_count, surface_flags, device, parent, parent_ops);
1473 break;
1475 default:
1476 ERR("Invalid resource type %s.\n", debug_d3dresourcetype(desc->resource_type));
1477 hr = WINED3DERR_INVALIDCALL;
1478 break;
1481 if (FAILED(hr))
1483 WARN("Failed to initialize texture, returning %#x.\n", hr);
1484 HeapFree(GetProcessHeap(), 0, object);
1485 return hr;
1488 TRACE("Created texture %p.\n", object);
1489 *texture = object;
1491 return WINED3D_OK;