wined3d: Merge the texture implementations.
[wine.git] / dlls / wined3d / texture.c
blob960e1347663713f4b801e816dc6b883a4b541e74
1 /*
2 * Copyright 2002-2005 Jason Edmeades
3 * Copyright 2002-2005 Raphael Junqueira
4 * Copyright 2005 Oliver Stieber
5 * Copyright 2007-2008 Stefan Dösinger for CodeWeavers
6 * Copyright 2009-2011 Henri Verbeet for CodeWeavers
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "config.h"
24 #include "wined3d_private.h"
26 WINE_DEFAULT_DEBUG_CHANNEL(d3d_texture);
28 static HRESULT basetexture_init(IWineD3DBaseTextureImpl *texture, const struct wined3d_texture_ops *texture_ops,
29 UINT layer_count, UINT level_count, WINED3DRESOURCETYPE resource_type, IWineD3DDeviceImpl *device,
30 DWORD usage, const struct wined3d_format *format, WINED3DPOOL pool, void *parent,
31 const struct wined3d_parent_ops *parent_ops, const struct wined3d_resource_ops *resource_ops)
33 HRESULT hr;
35 hr = resource_init(&texture->resource, device, resource_type, format,
36 WINED3DMULTISAMPLE_NONE, 0, usage, pool, 0, 0, 0, 0,
37 parent, parent_ops, resource_ops);
38 if (FAILED(hr))
40 WARN("Failed to initialize resource, returning %#x\n", hr);
41 return hr;
44 texture->baseTexture.texture_ops = texture_ops;
45 texture->baseTexture.sub_resources = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
46 level_count * layer_count * sizeof(*texture->baseTexture.sub_resources));
47 if (!texture->baseTexture.sub_resources)
49 ERR("Failed to allocate sub-resource array.\n");
50 resource_cleanup(&texture->resource);
51 return E_OUTOFMEMORY;
54 texture->baseTexture.layer_count = layer_count;
55 texture->baseTexture.level_count = level_count;
56 texture->baseTexture.filterType = (usage & WINED3DUSAGE_AUTOGENMIPMAP) ? WINED3DTEXF_LINEAR : WINED3DTEXF_NONE;
57 texture->baseTexture.LOD = 0;
58 texture->baseTexture.texture_rgb.dirty = TRUE;
59 texture->baseTexture.texture_srgb.dirty = TRUE;
60 texture->baseTexture.is_srgb = FALSE;
61 texture->baseTexture.pow2Matrix_identity = TRUE;
63 if (texture->resource.format->flags & WINED3DFMT_FLAG_FILTERING)
65 texture->baseTexture.minMipLookup = minMipLookup;
66 texture->baseTexture.magLookup = magLookup;
68 else
70 texture->baseTexture.minMipLookup = minMipLookup_noFilter;
71 texture->baseTexture.magLookup = magLookup_noFilter;
74 return WINED3D_OK;
77 /* A GL context is provided by the caller */
78 static void gltexture_delete(struct gl_texture *tex)
80 ENTER_GL();
81 glDeleteTextures(1, &tex->name);
82 LEAVE_GL();
83 tex->name = 0;
86 static void basetexture_unload(IWineD3DBaseTextureImpl *texture)
88 IWineD3DDeviceImpl *device = texture->resource.device;
89 struct wined3d_context *context = NULL;
91 if (texture->baseTexture.texture_rgb.name || texture->baseTexture.texture_srgb.name)
93 context = context_acquire(device, NULL);
96 if (texture->baseTexture.texture_rgb.name)
97 gltexture_delete(&texture->baseTexture.texture_rgb);
99 if (texture->baseTexture.texture_srgb.name)
100 gltexture_delete(&texture->baseTexture.texture_srgb);
102 if (context) context_release(context);
104 basetexture_set_dirty(texture, TRUE);
106 resource_unload(&texture->resource);
109 static void basetexture_cleanup(IWineD3DBaseTextureImpl *texture)
111 UINT sub_count = texture->baseTexture.level_count * texture->baseTexture.layer_count;
112 UINT i;
114 TRACE("texture %p.\n", texture);
116 for (i = 0; i < sub_count; ++i)
118 struct wined3d_resource *sub_resource = texture->baseTexture.sub_resources[i];
120 if (sub_resource)
121 texture->baseTexture.texture_ops->texture_sub_resource_cleanup(sub_resource);
124 basetexture_unload(texture);
125 HeapFree(GetProcessHeap(), 0, texture->baseTexture.sub_resources);
126 resource_cleanup(&texture->resource);
129 struct wined3d_resource *basetexture_get_sub_resource(IWineD3DBaseTextureImpl *texture, UINT sub_resource_idx)
131 UINT sub_count = texture->baseTexture.level_count * texture->baseTexture.layer_count;
133 if (sub_resource_idx >= sub_count)
135 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx, sub_count);
136 return NULL;
139 return texture->baseTexture.sub_resources[sub_resource_idx];
142 void basetexture_set_dirty(IWineD3DBaseTextureImpl *texture, BOOL dirty)
144 texture->baseTexture.texture_rgb.dirty = dirty;
145 texture->baseTexture.texture_srgb.dirty = dirty;
148 /* Context activation is done by the caller. */
149 static HRESULT basetexture_bind(IWineD3DBaseTextureImpl *texture,
150 const struct wined3d_gl_info *gl_info, BOOL srgb, BOOL *set_surface_desc)
152 struct gl_texture *gl_tex;
153 BOOL new_texture = FALSE;
154 HRESULT hr = WINED3D_OK;
155 GLenum target;
157 TRACE("texture %p, srgb %#x, set_surface_desc %p.\n", texture, srgb, set_surface_desc);
159 texture->baseTexture.is_srgb = srgb; /* sRGB mode cache for preload() calls outside drawprim. */
160 gl_tex = basetexture_get_gl_texture(texture, gl_info, srgb);
161 target = texture->baseTexture.target;
163 ENTER_GL();
164 /* Generate a texture name if we don't already have one. */
165 if (!gl_tex->name)
167 *set_surface_desc = TRUE;
168 glGenTextures(1, &gl_tex->name);
169 checkGLcall("glGenTextures");
170 TRACE("Generated texture %d.\n", gl_tex->name);
171 if (texture->resource.pool == WINED3DPOOL_DEFAULT)
173 /* Tell OpenGL to try and keep this texture in video ram (well mostly). */
174 GLclampf tmp = 0.9f;
175 glPrioritizeTextures(1, &gl_tex->name, &tmp);
177 /* Initialise the state of the texture object to the OpenGL defaults,
178 * not the D3D defaults. */
179 gl_tex->states[WINED3DTEXSTA_ADDRESSU] = WINED3DTADDRESS_WRAP;
180 gl_tex->states[WINED3DTEXSTA_ADDRESSV] = WINED3DTADDRESS_WRAP;
181 gl_tex->states[WINED3DTEXSTA_ADDRESSW] = WINED3DTADDRESS_WRAP;
182 gl_tex->states[WINED3DTEXSTA_BORDERCOLOR] = 0;
183 gl_tex->states[WINED3DTEXSTA_MAGFILTER] = WINED3DTEXF_LINEAR;
184 gl_tex->states[WINED3DTEXSTA_MINFILTER] = WINED3DTEXF_POINT; /* GL_NEAREST_MIPMAP_LINEAR */
185 gl_tex->states[WINED3DTEXSTA_MIPFILTER] = WINED3DTEXF_LINEAR; /* GL_NEAREST_MIPMAP_LINEAR */
186 gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL] = 0;
187 gl_tex->states[WINED3DTEXSTA_MAXANISOTROPY] = 1;
188 if (gl_info->supported[EXT_TEXTURE_SRGB_DECODE])
189 gl_tex->states[WINED3DTEXSTA_SRGBTEXTURE] = TRUE;
190 else
191 gl_tex->states[WINED3DTEXSTA_SRGBTEXTURE] = srgb;
192 gl_tex->states[WINED3DTEXSTA_SHADOW] = FALSE;
193 basetexture_set_dirty(texture, TRUE);
194 new_texture = TRUE;
196 if (texture->resource.usage & WINED3DUSAGE_AUTOGENMIPMAP)
198 /* This means double binding the texture at creation, but keeps
199 * the code simpler all in all, and the run-time path free from
200 * additional checks. */
201 glBindTexture(target, gl_tex->name);
202 checkGLcall("glBindTexture");
203 glTexParameteri(target, GL_GENERATE_MIPMAP_SGIS, GL_TRUE);
204 checkGLcall("glTexParameteri(target, GL_GENERATE_MIPMAP_SGIS, GL_TRUE)");
207 else
209 *set_surface_desc = FALSE;
212 if (gl_tex->name)
214 glBindTexture(target, gl_tex->name);
215 checkGLcall("glBindTexture");
216 if (new_texture)
218 /* For a new texture we have to set the texture levels after
219 * binding the texture. Beware that texture rectangles do not
220 * support mipmapping, but set the maxmiplevel if we're relying
221 * on the partial GL_ARB_texture_non_power_of_two emulation with
222 * texture rectangles. (I.e., do not care about cond_np2 here,
223 * just look for GL_TEXTURE_RECTANGLE_ARB.) */
224 if (target != GL_TEXTURE_RECTANGLE_ARB)
226 TRACE("Setting GL_TEXTURE_MAX_LEVEL to %u.\n", texture->baseTexture.level_count - 1);
227 glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, texture->baseTexture.level_count - 1);
228 checkGLcall("glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, texture->baseTexture.level_count)");
230 if (target == GL_TEXTURE_CUBE_MAP_ARB)
232 /* Cubemaps are always set to clamp, regardless of the sampler state. */
233 glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
234 glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
235 glTexParameteri(target, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
239 else
241 ERR("This texture doesn't have an OpenGL texture assigned to it.\n");
242 hr = WINED3DERR_INVALIDCALL;
245 LEAVE_GL();
246 return hr;
249 /* GL locking is done by the caller */
250 static void apply_wrap(const struct wined3d_gl_info *gl_info, GLenum target,
251 WINED3DTEXTUREADDRESS d3d_wrap, GLenum param, BOOL cond_np2)
253 GLint gl_wrap;
255 if (d3d_wrap < WINED3DTADDRESS_WRAP || d3d_wrap > WINED3DTADDRESS_MIRRORONCE)
257 FIXME("Unrecognized or unsupported WINED3DTEXTUREADDRESS %#x.\n", d3d_wrap);
258 return;
261 /* Cubemaps are always set to clamp, regardless of the sampler state. */
262 if (target == GL_TEXTURE_CUBE_MAP_ARB
263 || (cond_np2 && d3d_wrap == WINED3DTADDRESS_WRAP))
264 gl_wrap = GL_CLAMP_TO_EDGE;
265 else
266 gl_wrap = gl_info->wrap_lookup[d3d_wrap - WINED3DTADDRESS_WRAP];
268 TRACE("Setting param %#x to %#x for target %#x.\n", param, gl_wrap, target);
269 glTexParameteri(target, param, gl_wrap);
270 checkGLcall("glTexParameteri(target, param, gl_wrap)");
273 /* GL locking is done by the caller (state handler) */
274 void basetexture_apply_state_changes(IWineD3DBaseTextureImpl *texture,
275 const DWORD sampler_states[WINED3D_HIGHEST_SAMPLER_STATE + 1],
276 const struct wined3d_gl_info *gl_info)
278 BOOL cond_np2 = texture->baseTexture.cond_np2;
279 GLenum target = texture->baseTexture.target;
280 struct gl_texture *gl_tex;
281 DWORD state;
282 DWORD aniso;
284 TRACE("texture %p, sampler_states %p.\n", texture, sampler_states);
286 gl_tex = basetexture_get_gl_texture(texture, gl_info, texture->baseTexture.is_srgb);
288 /* This function relies on the correct texture being bound and loaded. */
290 if (sampler_states[WINED3DSAMP_ADDRESSU] != gl_tex->states[WINED3DTEXSTA_ADDRESSU])
292 state = sampler_states[WINED3DSAMP_ADDRESSU];
293 apply_wrap(gl_info, target, state, GL_TEXTURE_WRAP_S, cond_np2);
294 gl_tex->states[WINED3DTEXSTA_ADDRESSU] = state;
297 if (sampler_states[WINED3DSAMP_ADDRESSV] != gl_tex->states[WINED3DTEXSTA_ADDRESSV])
299 state = sampler_states[WINED3DSAMP_ADDRESSV];
300 apply_wrap(gl_info, target, state, GL_TEXTURE_WRAP_T, cond_np2);
301 gl_tex->states[WINED3DTEXSTA_ADDRESSV] = state;
304 if (sampler_states[WINED3DSAMP_ADDRESSW] != gl_tex->states[WINED3DTEXSTA_ADDRESSW])
306 state = sampler_states[WINED3DSAMP_ADDRESSW];
307 apply_wrap(gl_info, target, state, GL_TEXTURE_WRAP_R, cond_np2);
308 gl_tex->states[WINED3DTEXSTA_ADDRESSW] = state;
311 if (sampler_states[WINED3DSAMP_BORDERCOLOR] != gl_tex->states[WINED3DTEXSTA_BORDERCOLOR])
313 float col[4];
315 state = sampler_states[WINED3DSAMP_BORDERCOLOR];
316 D3DCOLORTOGLFLOAT4(state, col);
317 TRACE("Setting border color for %#x to %#x.\n", target, state);
318 glTexParameterfv(target, GL_TEXTURE_BORDER_COLOR, &col[0]);
319 checkGLcall("glTexParameterfv(..., GL_TEXTURE_BORDER_COLOR, ...)");
320 gl_tex->states[WINED3DTEXSTA_BORDERCOLOR] = state;
323 if (sampler_states[WINED3DSAMP_MAGFILTER] != gl_tex->states[WINED3DTEXSTA_MAGFILTER])
325 GLint gl_value;
327 state = sampler_states[WINED3DSAMP_MAGFILTER];
328 if (state > WINED3DTEXF_ANISOTROPIC)
329 FIXME("Unrecognized or unsupported MAGFILTER* value %d.\n", state);
331 gl_value = wined3d_gl_mag_filter(texture->baseTexture.magLookup,
332 min(max(state, WINED3DTEXF_POINT), WINED3DTEXF_LINEAR));
333 TRACE("ValueMAG=%#x setting MAGFILTER to %#x.\n", state, gl_value);
334 glTexParameteri(target, GL_TEXTURE_MAG_FILTER, gl_value);
336 gl_tex->states[WINED3DTEXSTA_MAGFILTER] = state;
339 if ((sampler_states[WINED3DSAMP_MINFILTER] != gl_tex->states[WINED3DTEXSTA_MINFILTER]
340 || sampler_states[WINED3DSAMP_MIPFILTER] != gl_tex->states[WINED3DTEXSTA_MIPFILTER]
341 || sampler_states[WINED3DSAMP_MAXMIPLEVEL] != gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL]))
343 GLint gl_value;
345 gl_tex->states[WINED3DTEXSTA_MIPFILTER] = sampler_states[WINED3DSAMP_MIPFILTER];
346 gl_tex->states[WINED3DTEXSTA_MINFILTER] = sampler_states[WINED3DSAMP_MINFILTER];
347 gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL] = sampler_states[WINED3DSAMP_MAXMIPLEVEL];
349 if (gl_tex->states[WINED3DTEXSTA_MINFILTER] > WINED3DTEXF_ANISOTROPIC
350 || gl_tex->states[WINED3DTEXSTA_MIPFILTER] > WINED3DTEXF_ANISOTROPIC)
352 FIXME("Unrecognized or unsupported D3DSAMP_MINFILTER value %#x D3DSAMP_MIPFILTER value %#x.\n",
353 gl_tex->states[WINED3DTEXSTA_MINFILTER],
354 gl_tex->states[WINED3DTEXSTA_MIPFILTER]);
356 gl_value = wined3d_gl_min_mip_filter(texture->baseTexture.minMipLookup,
357 min(max(sampler_states[WINED3DSAMP_MINFILTER], WINED3DTEXF_POINT), WINED3DTEXF_LINEAR),
358 min(max(sampler_states[WINED3DSAMP_MIPFILTER], WINED3DTEXF_NONE), WINED3DTEXF_LINEAR));
360 TRACE("ValueMIN=%#x, ValueMIP=%#x, setting MINFILTER to %#x.\n",
361 sampler_states[WINED3DSAMP_MINFILTER],
362 sampler_states[WINED3DSAMP_MIPFILTER], gl_value);
363 glTexParameteri(target, GL_TEXTURE_MIN_FILTER, gl_value);
364 checkGLcall("glTexParameter GL_TEXTURE_MIN_FILTER, ...");
366 if (!cond_np2)
368 if (gl_tex->states[WINED3DTEXSTA_MIPFILTER] == WINED3DTEXF_NONE)
369 gl_value = texture->baseTexture.LOD;
370 else if (gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL] >= texture->baseTexture.level_count)
371 gl_value = texture->baseTexture.level_count - 1;
372 else if (gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL] < texture->baseTexture.LOD)
373 /* baseTexture.LOD is already clamped in the setter. */
374 gl_value = texture->baseTexture.LOD;
375 else
376 gl_value = gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL];
378 /* Note that WINED3DSAMP_MAXMIPLEVEL specifies the largest mipmap
379 * (default 0), while GL_TEXTURE_MAX_LEVEL specifies the smallest
380 * mimap used (default 1000). So WINED3DSAMP_MAXMIPLEVEL
381 * corresponds to GL_TEXTURE_BASE_LEVEL. */
382 glTexParameteri(target, GL_TEXTURE_BASE_LEVEL, gl_value);
386 if ((gl_tex->states[WINED3DTEXSTA_MAGFILTER] != WINED3DTEXF_ANISOTROPIC
387 && gl_tex->states[WINED3DTEXSTA_MINFILTER] != WINED3DTEXF_ANISOTROPIC
388 && gl_tex->states[WINED3DTEXSTA_MIPFILTER] != WINED3DTEXF_ANISOTROPIC)
389 || cond_np2)
390 aniso = 1;
391 else
392 aniso = sampler_states[WINED3DSAMP_MAXANISOTROPY];
394 if (gl_tex->states[WINED3DTEXSTA_MAXANISOTROPY] != aniso)
396 if (gl_info->supported[EXT_TEXTURE_FILTER_ANISOTROPIC])
398 glTexParameteri(target, GL_TEXTURE_MAX_ANISOTROPY_EXT, aniso);
399 checkGLcall("glTexParameteri(GL_TEXTURE_MAX_ANISOTROPY_EXT, aniso)");
401 else
403 WARN("Anisotropic filtering not supported.\n");
405 gl_tex->states[WINED3DTEXSTA_MAXANISOTROPY] = aniso;
408 /* These should always be the same unless EXT_texture_sRGB_decode is supported. */
409 if (sampler_states[WINED3DSAMP_SRGBTEXTURE] != gl_tex->states[WINED3DTEXSTA_SRGBTEXTURE])
411 glTexParameteri(target, GL_TEXTURE_SRGB_DECODE_EXT,
412 sampler_states[WINED3DSAMP_SRGBTEXTURE] ? GL_DECODE_EXT : GL_SKIP_DECODE_EXT);
413 checkGLcall("glTexParameteri(GL_TEXTURE_SRGB_DECODE_EXT)");
416 if (!(texture->resource.format->flags & WINED3DFMT_FLAG_SHADOW)
417 != !gl_tex->states[WINED3DTEXSTA_SHADOW])
419 if (texture->resource.format->flags & WINED3DFMT_FLAG_SHADOW)
421 glTexParameteri(target, GL_DEPTH_TEXTURE_MODE_ARB, GL_LUMINANCE);
422 glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE_ARB);
423 checkGLcall("glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE_ARB)");
424 gl_tex->states[WINED3DTEXSTA_SHADOW] = TRUE;
426 else
428 glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_NONE);
429 checkGLcall("glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_NONE)");
430 gl_tex->states[WINED3DTEXSTA_SHADOW] = FALSE;
435 static HRESULT WINAPI IWineD3DTextureImpl_QueryInterface(IWineD3DBaseTexture *iface, REFIID riid, void **object)
437 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
439 if (IsEqualGUID(riid, &IID_IWineD3DBaseTexture)
440 || IsEqualGUID(riid, &IID_IWineD3DResource)
441 || IsEqualGUID(riid, &IID_IWineD3DBase)
442 || IsEqualGUID(riid, &IID_IUnknown))
444 IUnknown_AddRef(iface);
445 *object = iface;
446 return S_OK;
449 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
451 *object = NULL;
452 return E_NOINTERFACE;
455 static ULONG WINAPI IWineD3DTextureImpl_AddRef(IWineD3DBaseTexture *iface)
457 IWineD3DBaseTextureImpl *texture = (IWineD3DBaseTextureImpl *)iface;
458 ULONG refcount = InterlockedIncrement(&texture->resource.ref);
460 TRACE("%p increasing refcount to %u.\n", texture, refcount);
462 return refcount;
465 /* Do not call while under the GL lock. */
466 static ULONG WINAPI IWineD3DTextureImpl_Release(IWineD3DBaseTexture *iface)
468 IWineD3DBaseTextureImpl *texture = (IWineD3DBaseTextureImpl *)iface;
469 ULONG refcount = InterlockedDecrement(&texture->resource.ref);
471 TRACE("%p decreasing refcount to %u.\n", texture, refcount);
473 if (!refcount)
475 basetexture_cleanup(texture);
476 texture->resource.parent_ops->wined3d_object_destroyed(texture->resource.parent);
477 HeapFree(GetProcessHeap(), 0, texture);
480 return refcount;
483 static HRESULT WINAPI IWineD3DTextureImpl_SetPrivateData(IWineD3DBaseTexture *iface,
484 REFGUID riid, const void *data, DWORD data_size, DWORD flags)
486 return resource_set_private_data(&((IWineD3DBaseTextureImpl *)iface)->resource, riid, data, data_size, flags);
489 static HRESULT WINAPI IWineD3DTextureImpl_GetPrivateData(IWineD3DBaseTexture *iface,
490 REFGUID guid, void *data, DWORD *data_size)
492 return resource_get_private_data(&((IWineD3DBaseTextureImpl *)iface)->resource, guid, data, data_size);
495 static HRESULT WINAPI IWineD3DTextureImpl_FreePrivateData(IWineD3DBaseTexture *iface, REFGUID refguid)
497 return resource_free_private_data(&((IWineD3DBaseTextureImpl *)iface)->resource, refguid);
500 static DWORD WINAPI IWineD3DTextureImpl_SetPriority(IWineD3DBaseTexture *iface, DWORD priority)
502 return resource_set_priority(&((IWineD3DBaseTextureImpl *)iface)->resource, priority);
505 static DWORD WINAPI IWineD3DTextureImpl_GetPriority(IWineD3DBaseTexture *iface)
507 return resource_get_priority(&((IWineD3DBaseTextureImpl *)iface)->resource);
510 /* Do not call while under the GL lock. */
511 static void WINAPI IWineD3DTextureImpl_PreLoad(IWineD3DBaseTexture *iface)
513 struct IWineD3DBaseTextureImpl *texture = (IWineD3DBaseTextureImpl *)iface;
515 texture->baseTexture.texture_ops->texture_preload(texture, SRGB_ANY);
518 static WINED3DRESOURCETYPE WINAPI IWineD3DTextureImpl_GetType(IWineD3DBaseTexture *iface)
520 return resource_get_type(&((IWineD3DBaseTextureImpl *)iface)->resource);
523 static void * WINAPI IWineD3DTextureImpl_GetParent(IWineD3DBaseTexture *iface)
525 TRACE("iface %p.\n", iface);
527 return ((IWineD3DBaseTextureImpl *)iface)->resource.parent;
530 static DWORD WINAPI IWineD3DTextureImpl_SetLOD(IWineD3DBaseTexture *iface, DWORD lod)
532 IWineD3DBaseTextureImpl *texture = (IWineD3DBaseTextureImpl *)iface;
533 DWORD old = texture->baseTexture.LOD;
535 TRACE("iface %p, lod %u.\n", iface, lod);
537 /* The d3d9:texture test shows that SetLOD is ignored on non-managed
538 * textures. The call always returns 0, and GetLOD always returns 0. */
539 if (texture->resource.pool != WINED3DPOOL_MANAGED)
541 TRACE("Ignoring SetLOD on %s texture, returning 0.\n", debug_d3dpool(texture->resource.pool));
542 return 0;
545 if (lod >= texture->baseTexture.level_count)
546 lod = texture->baseTexture.level_count - 1;
548 if (texture->baseTexture.LOD != lod)
550 texture->baseTexture.LOD = lod;
552 texture->baseTexture.texture_rgb.states[WINED3DTEXSTA_MAXMIPLEVEL] = ~0U;
553 texture->baseTexture.texture_srgb.states[WINED3DTEXSTA_MAXMIPLEVEL] = ~0U;
554 if (texture->baseTexture.bindCount)
555 IWineD3DDeviceImpl_MarkStateDirty(texture->resource.device, STATE_SAMPLER(texture->baseTexture.sampler));
558 return old;
561 static DWORD WINAPI IWineD3DTextureImpl_GetLOD(IWineD3DBaseTexture *iface)
563 IWineD3DBaseTextureImpl *texture = (IWineD3DBaseTextureImpl *)iface;
565 TRACE("iface %p, returning %u.\n", iface, texture->baseTexture.LOD);
567 return texture->baseTexture.LOD;
570 static DWORD WINAPI IWineD3DTextureImpl_GetLevelCount(IWineD3DBaseTexture *iface)
572 IWineD3DBaseTextureImpl *texture = (IWineD3DBaseTextureImpl *)iface;
574 TRACE("iface %p, returning %u.\n", iface, texture->baseTexture.level_count);
576 return texture->baseTexture.level_count;
579 static HRESULT WINAPI IWineD3DTextureImpl_SetAutoGenFilterType(IWineD3DBaseTexture *iface,
580 WINED3DTEXTUREFILTERTYPE filter_type)
582 IWineD3DBaseTextureImpl *texture = (IWineD3DBaseTextureImpl *)iface;
584 TRACE("iface %p, filter_type %s.\n", iface, debug_d3dtexturefiltertype(filter_type));
586 if (!(texture->resource.usage & WINED3DUSAGE_AUTOGENMIPMAP))
588 WARN("Texture doesn't have AUTOGENMIPMAP usage.\n");
589 return WINED3DERR_INVALIDCALL;
592 if (texture->baseTexture.filterType != filter_type)
594 GLenum target = texture->baseTexture.target;
595 struct wined3d_context *context;
597 context = context_acquire(texture->resource.device, NULL);
599 ENTER_GL();
600 glBindTexture(target, texture->baseTexture.texture_rgb.name);
601 checkGLcall("glBindTexture");
602 switch (filter_type)
604 case WINED3DTEXF_NONE:
605 case WINED3DTEXF_POINT:
606 glTexParameteri(target, GL_GENERATE_MIPMAP_HINT_SGIS, GL_FASTEST);
607 checkGLcall("glTexParameteri(target, GL_GENERATE_MIPMAP_HINT_SGIS, GL_FASTEST)");
608 break;
610 case WINED3DTEXF_LINEAR:
611 glTexParameteri(target, GL_GENERATE_MIPMAP_HINT_SGIS, GL_NICEST);
612 checkGLcall("glTexParameteri(target, GL_GENERATE_MIPMAP_HINT_SGIS, GL_NICEST)");
613 break;
615 default:
616 WARN("Unexpected filter type %#x, setting to GL_NICEST.\n", filter_type);
617 glTexParameteri(target, GL_GENERATE_MIPMAP_HINT_SGIS, GL_NICEST);
618 checkGLcall("glTexParameteri(target, GL_GENERATE_MIPMAP_HINT_SGIS, GL_NICEST)");
619 break;
621 LEAVE_GL();
623 context_release(context);
625 texture->baseTexture.filterType = filter_type;
627 return WINED3D_OK;
630 static WINED3DTEXTUREFILTERTYPE WINAPI IWineD3DTextureImpl_GetAutoGenFilterType(IWineD3DBaseTexture *iface)
632 IWineD3DBaseTextureImpl *texture = (IWineD3DBaseTextureImpl *)iface;
634 TRACE("iface %p.\n", iface);
636 return texture->baseTexture.filterType;
639 static void WINAPI IWineD3DTextureImpl_GenerateMipSubLevels(IWineD3DBaseTexture *iface)
641 /* TODO: Implement filters using GL_SGI_generate_mipmaps. */
642 FIXME("iface %p stub!\n", iface);
645 static struct wined3d_resource * WINAPI IWineD3DTextureImpl_GetSubResource(IWineD3DBaseTexture *iface,
646 UINT sub_resource_idx)
648 TRACE("iface %p, sub_resource_idx %u.\n", iface, sub_resource_idx);
650 return basetexture_get_sub_resource((IWineD3DBaseTextureImpl *)iface, sub_resource_idx);
653 static HRESULT WINAPI IWineD3DTextureImpl_AddDirtyRegion(IWineD3DBaseTexture *iface,
654 UINT layer, const WINED3DBOX *dirty_region)
656 IWineD3DBaseTextureImpl *texture = (IWineD3DBaseTextureImpl *)iface;
657 struct wined3d_resource *sub_resource;
659 TRACE("texture %p, layer %u, dirty_region %p.\n", texture, layer, dirty_region);
661 if (!(sub_resource = basetexture_get_sub_resource(texture, layer * texture->baseTexture.level_count)))
663 WARN("Failed to get sub-resource.\n");
664 return WINED3DERR_INVALIDCALL;
667 basetexture_set_dirty(texture, TRUE);
668 texture->baseTexture.texture_ops->texture_sub_resource_add_dirty_region(sub_resource, dirty_region);
670 return WINED3D_OK;
673 static const struct IWineD3DBaseTextureVtbl wined3d_texture_vtbl =
675 IWineD3DTextureImpl_QueryInterface,
676 IWineD3DTextureImpl_AddRef,
677 IWineD3DTextureImpl_Release,
678 IWineD3DTextureImpl_GetParent,
679 IWineD3DTextureImpl_SetPrivateData,
680 IWineD3DTextureImpl_GetPrivateData,
681 IWineD3DTextureImpl_FreePrivateData,
682 IWineD3DTextureImpl_SetPriority,
683 IWineD3DTextureImpl_GetPriority,
684 IWineD3DTextureImpl_PreLoad,
685 IWineD3DTextureImpl_GetType,
686 IWineD3DTextureImpl_SetLOD,
687 IWineD3DTextureImpl_GetLOD,
688 IWineD3DTextureImpl_GetLevelCount,
689 IWineD3DTextureImpl_SetAutoGenFilterType,
690 IWineD3DTextureImpl_GetAutoGenFilterType,
691 IWineD3DTextureImpl_GenerateMipSubLevels,
692 IWineD3DTextureImpl_GetSubResource,
693 IWineD3DTextureImpl_AddDirtyRegion,
696 /* Context activation is done by the caller. */
697 static HRESULT texture2d_bind(IWineD3DBaseTextureImpl *texture,
698 const struct wined3d_gl_info *gl_info, BOOL srgb)
700 BOOL set_gl_texture_desc;
701 HRESULT hr;
703 TRACE("texture %p, gl_info %p, srgb %#x.\n", texture, gl_info, srgb);
705 hr = basetexture_bind(texture, gl_info, srgb, &set_gl_texture_desc);
706 if (set_gl_texture_desc && SUCCEEDED(hr))
708 UINT sub_count = texture->baseTexture.level_count * texture->baseTexture.layer_count;
709 BOOL srgb_tex = !gl_info->supported[EXT_TEXTURE_SRGB_DECODE] && texture->baseTexture.is_srgb;
710 struct gl_texture *gl_tex;
711 UINT i;
713 gl_tex = basetexture_get_gl_texture(texture, gl_info, srgb_tex);
715 for (i = 0; i < sub_count; ++i)
717 IWineD3DSurfaceImpl *surface = surface_from_resource(texture->baseTexture.sub_resources[i]);
718 surface_set_texture_name(surface, gl_tex->name, srgb_tex);
721 /* Conditinal non power of two textures use a different clamping
722 * default. If we're using the GL_WINE_normalized_texrect partial
723 * driver emulation, we're dealing with a GL_TEXTURE_2D texture which
724 * has the address mode set to repeat - something that prevents us
725 * from hitting the accelerated codepath. Thus manually set the GL
726 * state. The same applies to filtering. Even if the texture has only
727 * one mip level, the default LINEAR_MIPMAP_LINEAR filter causes a SW
728 * fallback on macos. */
729 if (texture->baseTexture.cond_np2)
731 GLenum target = texture->baseTexture.target;
733 ENTER_GL();
734 glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
735 checkGLcall("glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)");
736 glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
737 checkGLcall("glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)");
738 glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
739 checkGLcall("glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST)");
740 glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
741 checkGLcall("glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST)");
742 LEAVE_GL();
743 gl_tex->states[WINED3DTEXSTA_ADDRESSU] = WINED3DTADDRESS_CLAMP;
744 gl_tex->states[WINED3DTEXSTA_ADDRESSV] = WINED3DTADDRESS_CLAMP;
745 gl_tex->states[WINED3DTEXSTA_MAGFILTER] = WINED3DTEXF_POINT;
746 gl_tex->states[WINED3DTEXSTA_MINFILTER] = WINED3DTEXF_POINT;
747 gl_tex->states[WINED3DTEXSTA_MIPFILTER] = WINED3DTEXF_NONE;
751 return hr;
754 /* Do not call while under the GL lock. */
755 static void texture2d_preload(IWineD3DBaseTextureImpl *texture, enum WINED3DSRGB srgb)
757 UINT sub_count = texture->baseTexture.level_count * texture->baseTexture.layer_count;
758 IWineD3DDeviceImpl *device = texture->resource.device;
759 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
760 struct wined3d_context *context = NULL;
761 struct gl_texture *gl_tex;
762 BOOL srgb_mode;
763 UINT i;
765 TRACE("texture %p, srgb %#x.\n", texture, srgb);
767 switch (srgb)
769 case SRGB_RGB:
770 srgb_mode = FALSE;
771 break;
773 case SRGB_BOTH:
774 texture2d_preload(texture, SRGB_RGB);
775 /* Fallthrough */
776 case SRGB_SRGB:
777 srgb_mode = TRUE;
778 break;
780 default:
781 srgb_mode = texture->baseTexture.is_srgb;
782 break;
785 gl_tex = basetexture_get_gl_texture(texture, gl_info, srgb_mode);
787 if (!device->isInDraw)
789 /* No danger of recursive calls, context_acquire() sets isInDraw to TRUE
790 * when loading offscreen render targets into the texture. */
791 context = context_acquire(device, NULL);
794 if (texture->resource.format->id == WINED3DFMT_P8_UINT
795 || texture->resource.format->id == WINED3DFMT_P8_UINT_A8_UNORM)
797 for (i = 0; i < sub_count; ++i)
799 IWineD3DSurfaceImpl *surface = surface_from_resource(texture->baseTexture.sub_resources[i]);
801 if (palette9_changed(surface))
803 TRACE("Reloading surface %p because the d3d8/9 palette was changed.\n", surface);
804 /* TODO: This is not necessarily needed with hw palettized texture support. */
805 surface_load_location(surface, SFLAG_INSYSMEM, NULL);
806 /* Make sure the texture is reloaded because of the palette
807 * change, this kills performance though :( */
808 surface_modify_location(surface, SFLAG_INTEXTURE, FALSE);
813 if (gl_tex->dirty)
815 /* Reload the surfaces if the texture is marked dirty. */
816 for (i = 0; i < sub_count; ++i)
818 surface_load(surface_from_resource(texture->baseTexture.sub_resources[i]), srgb_mode);
821 else
823 TRACE("Texture %p not dirty, nothing to do.\n", texture);
826 /* No longer dirty. */
827 gl_tex->dirty = FALSE;
829 if (context) context_release(context);
832 static void texture2d_sub_resource_add_dirty_region(struct wined3d_resource *sub_resource,
833 const WINED3DBOX *dirty_region)
835 surface_add_dirty_rect(surface_from_resource(sub_resource), dirty_region);
838 static void texture2d_sub_resource_cleanup(struct wined3d_resource *sub_resource)
840 IWineD3DSurfaceImpl *surface = surface_from_resource(sub_resource);
842 /* Clean out the texture name we gave to the surface so that the
843 * surface doesn't try and release it. */
844 surface_set_texture_name(surface, 0, TRUE);
845 surface_set_texture_name(surface, 0, FALSE);
846 surface_set_texture_target(surface, 0);
847 surface_set_container(surface, WINED3D_CONTAINER_NONE, NULL);
848 IWineD3DSurface_Release((IWineD3DSurface *)surface);
851 /* Do not call while under the GL lock. */
852 static void texture2d_unload(struct wined3d_resource *resource)
854 IWineD3DBaseTextureImpl *texture = basetexture_from_resource(resource);
855 UINT sub_count = texture->baseTexture.level_count * texture->baseTexture.layer_count;
856 UINT i;
858 TRACE("texture %p.\n", texture);
860 for (i = 0; i < sub_count; ++i)
862 struct wined3d_resource *sub_resource = texture->baseTexture.sub_resources[i];
863 IWineD3DSurfaceImpl *surface = surface_from_resource(sub_resource);
865 sub_resource->resource_ops->resource_unload(sub_resource);
866 surface_set_texture_name(surface, 0, FALSE); /* Delete RGB name */
867 surface_set_texture_name(surface, 0, TRUE); /* Delete sRGB name */
870 basetexture_unload(texture);
873 static const struct wined3d_texture_ops texture2d_ops =
875 texture2d_bind,
876 texture2d_preload,
877 texture2d_sub_resource_add_dirty_region,
878 texture2d_sub_resource_cleanup,
881 static const struct wined3d_resource_ops texture2d_resource_ops =
883 texture2d_unload,
886 HRESULT cubetexture_init(IWineD3DBaseTextureImpl *texture, UINT edge_length, UINT levels,
887 IWineD3DDeviceImpl *device, DWORD usage, enum wined3d_format_id format_id, WINED3DPOOL pool,
888 void *parent, const struct wined3d_parent_ops *parent_ops)
890 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
891 const struct wined3d_format *format = wined3d_get_format(gl_info, format_id);
892 UINT pow2_edge_length;
893 unsigned int i, j;
894 UINT tmp_w;
895 HRESULT hr;
897 /* TODO: It should only be possible to create textures for formats
898 * that are reported as supported. */
899 if (WINED3DFMT_UNKNOWN >= format_id)
901 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture);
902 return WINED3DERR_INVALIDCALL;
905 if (!gl_info->supported[ARB_TEXTURE_CUBE_MAP] && pool != WINED3DPOOL_SCRATCH)
907 WARN("(%p) : Tried to create not supported cube texture.\n", texture);
908 return WINED3DERR_INVALIDCALL;
911 /* Calculate levels for mip mapping */
912 if (usage & WINED3DUSAGE_AUTOGENMIPMAP)
914 if (!gl_info->supported[SGIS_GENERATE_MIPMAP])
916 WARN("No mipmap generation support, returning D3DERR_INVALIDCALL.\n");
917 return WINED3DERR_INVALIDCALL;
920 if (levels > 1)
922 WARN("D3DUSAGE_AUTOGENMIPMAP is set, and level count > 1, returning D3DERR_INVALIDCALL.\n");
923 return WINED3DERR_INVALIDCALL;
926 levels = 1;
928 else if (!levels)
930 levels = wined3d_log2i(edge_length) + 1;
931 TRACE("Calculated levels = %u.\n", levels);
934 texture->lpVtbl = &wined3d_texture_vtbl;
936 hr = basetexture_init((IWineD3DBaseTextureImpl *)texture, &texture2d_ops,
937 6, levels, WINED3DRTYPE_CUBETEXTURE, device, usage, format, pool,
938 parent, parent_ops, &texture2d_resource_ops);
939 if (FAILED(hr))
941 WARN("Failed to initialize basetexture, returning %#x\n", hr);
942 return hr;
945 /* Find the nearest pow2 match. */
946 pow2_edge_length = 1;
947 while (pow2_edge_length < edge_length) pow2_edge_length <<= 1;
949 if (gl_info->supported[ARB_TEXTURE_NON_POWER_OF_TWO] || (edge_length == pow2_edge_length))
951 /* Precalculated scaling for 'faked' non power of two texture coords. */
952 texture->baseTexture.pow2Matrix[0] = 1.0f;
953 texture->baseTexture.pow2Matrix[5] = 1.0f;
954 texture->baseTexture.pow2Matrix[10] = 1.0f;
955 texture->baseTexture.pow2Matrix[15] = 1.0f;
957 else
959 /* Precalculated scaling for 'faked' non power of two texture coords. */
960 texture->baseTexture.pow2Matrix[0] = ((float)edge_length) / ((float)pow2_edge_length);
961 texture->baseTexture.pow2Matrix[5] = ((float)edge_length) / ((float)pow2_edge_length);
962 texture->baseTexture.pow2Matrix[10] = ((float)edge_length) / ((float)pow2_edge_length);
963 texture->baseTexture.pow2Matrix[15] = 1.0f;
964 texture->baseTexture.pow2Matrix_identity = FALSE;
966 texture->baseTexture.target = GL_TEXTURE_CUBE_MAP_ARB;
968 /* Generate all the surfaces. */
969 tmp_w = edge_length;
970 for (i = 0; i < texture->baseTexture.level_count; ++i)
972 /* Create the 6 faces. */
973 for (j = 0; j < 6; ++j)
975 static const GLenum cube_targets[6] =
977 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB,
978 GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB,
979 GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB,
980 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB,
981 GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB,
982 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB,
984 UINT idx = j * texture->baseTexture.level_count + i;
985 IWineD3DSurface *surface;
987 hr = IWineD3DDeviceParent_CreateSurface(device->device_parent, parent, tmp_w, tmp_w,
988 format_id, usage, pool, i /* Level */, j, &surface);
989 if (FAILED(hr))
991 FIXME("(%p) Failed to create surface, hr %#x.\n", texture, hr);
992 basetexture_cleanup(texture);
993 return hr;
996 surface_set_container((IWineD3DSurfaceImpl *)surface, WINED3D_CONTAINER_TEXTURE, texture);
997 surface_set_texture_target((IWineD3DSurfaceImpl *)surface, cube_targets[j]);
998 texture->baseTexture.sub_resources[idx] = &((IWineD3DSurfaceImpl *)surface)->resource;
999 TRACE("Created surface level %u @ %p.\n", i, surface);
1001 tmp_w = max(1, tmp_w >> 1);
1004 return WINED3D_OK;
1007 HRESULT texture_init(IWineD3DBaseTextureImpl *texture, UINT width, UINT height, UINT levels,
1008 IWineD3DDeviceImpl *device, DWORD usage, enum wined3d_format_id format_id, WINED3DPOOL pool,
1009 void *parent, const struct wined3d_parent_ops *parent_ops)
1011 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
1012 const struct wined3d_format *format = wined3d_get_format(gl_info, format_id);
1013 UINT pow2_width, pow2_height;
1014 UINT tmp_w, tmp_h;
1015 unsigned int i;
1016 HRESULT hr;
1018 /* TODO: It should only be possible to create textures for formats
1019 * that are reported as supported. */
1020 if (WINED3DFMT_UNKNOWN >= format_id)
1022 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture);
1023 return WINED3DERR_INVALIDCALL;
1026 /* Non-power2 support. */
1027 if (gl_info->supported[ARB_TEXTURE_NON_POWER_OF_TWO])
1029 pow2_width = width;
1030 pow2_height = height;
1032 else
1034 /* Find the nearest pow2 match. */
1035 pow2_width = pow2_height = 1;
1036 while (pow2_width < width) pow2_width <<= 1;
1037 while (pow2_height < height) pow2_height <<= 1;
1039 if (pow2_width != width || pow2_height != height)
1041 if (levels > 1)
1043 WARN("Attempted to create a mipmapped np2 texture without unconditional np2 support.\n");
1044 return WINED3DERR_INVALIDCALL;
1046 levels = 1;
1050 /* Calculate levels for mip mapping. */
1051 if (usage & WINED3DUSAGE_AUTOGENMIPMAP)
1053 if (!gl_info->supported[SGIS_GENERATE_MIPMAP])
1055 WARN("No mipmap generation support, returning WINED3DERR_INVALIDCALL.\n");
1056 return WINED3DERR_INVALIDCALL;
1059 if (levels > 1)
1061 WARN("D3DUSAGE_AUTOGENMIPMAP is set, and level count > 1, returning WINED3DERR_INVALIDCALL.\n");
1062 return WINED3DERR_INVALIDCALL;
1065 levels = 1;
1067 else if (!levels)
1069 levels = wined3d_log2i(max(width, height)) + 1;
1070 TRACE("Calculated levels = %u.\n", levels);
1073 texture->lpVtbl = &wined3d_texture_vtbl;
1075 hr = basetexture_init((IWineD3DBaseTextureImpl *)texture, &texture2d_ops,
1076 1, levels, WINED3DRTYPE_TEXTURE, device, usage, format, pool,
1077 parent, parent_ops, &texture2d_resource_ops);
1078 if (FAILED(hr))
1080 WARN("Failed to initialize basetexture, returning %#x.\n", hr);
1081 return hr;
1084 /* Precalculated scaling for 'faked' non power of two texture coords.
1085 * Second also don't use ARB_TEXTURE_RECTANGLE in case the surface format is P8 and EXT_PALETTED_TEXTURE
1086 * is used in combination with texture uploads (RTL_READTEX). The reason is that EXT_PALETTED_TEXTURE
1087 * doesn't work in combination with ARB_TEXTURE_RECTANGLE. */
1088 if (gl_info->supported[WINED3D_GL_NORMALIZED_TEXRECT] && (width != pow2_width || height != pow2_height))
1090 texture->baseTexture.pow2Matrix[0] = 1.0f;
1091 texture->baseTexture.pow2Matrix[5] = 1.0f;
1092 texture->baseTexture.pow2Matrix[10] = 1.0f;
1093 texture->baseTexture.pow2Matrix[15] = 1.0f;
1094 texture->baseTexture.target = GL_TEXTURE_2D;
1095 texture->baseTexture.cond_np2 = TRUE;
1096 texture->baseTexture.minMipLookup = minMipLookup_noFilter;
1098 else if (gl_info->supported[ARB_TEXTURE_RECTANGLE] && (width != pow2_width || height != pow2_height)
1099 && !(format->id == WINED3DFMT_P8_UINT && gl_info->supported[EXT_PALETTED_TEXTURE]
1100 && wined3d_settings.rendertargetlock_mode == RTL_READTEX))
1102 if ((width != 1) || (height != 1)) texture->baseTexture.pow2Matrix_identity = FALSE;
1104 texture->baseTexture.pow2Matrix[0] = (float)width;
1105 texture->baseTexture.pow2Matrix[5] = (float)height;
1106 texture->baseTexture.pow2Matrix[10] = 1.0f;
1107 texture->baseTexture.pow2Matrix[15] = 1.0f;
1108 texture->baseTexture.target = GL_TEXTURE_RECTANGLE_ARB;
1109 texture->baseTexture.cond_np2 = TRUE;
1111 if (texture->resource.format->flags & WINED3DFMT_FLAG_FILTERING)
1113 texture->baseTexture.minMipLookup = minMipLookup_noMip;
1115 else
1117 texture->baseTexture.minMipLookup = minMipLookup_noFilter;
1120 else
1122 if ((width != pow2_width) || (height != pow2_height))
1124 texture->baseTexture.pow2Matrix_identity = FALSE;
1125 texture->baseTexture.pow2Matrix[0] = (((float)width) / ((float)pow2_width));
1126 texture->baseTexture.pow2Matrix[5] = (((float)height) / ((float)pow2_height));
1128 else
1130 texture->baseTexture.pow2Matrix[0] = 1.0f;
1131 texture->baseTexture.pow2Matrix[5] = 1.0f;
1134 texture->baseTexture.pow2Matrix[10] = 1.0f;
1135 texture->baseTexture.pow2Matrix[15] = 1.0f;
1136 texture->baseTexture.target = GL_TEXTURE_2D;
1137 texture->baseTexture.cond_np2 = FALSE;
1139 TRACE("xf(%f) yf(%f)\n", texture->baseTexture.pow2Matrix[0], texture->baseTexture.pow2Matrix[5]);
1141 /* Generate all the surfaces. */
1142 tmp_w = width;
1143 tmp_h = height;
1144 for (i = 0; i < texture->baseTexture.level_count; ++i)
1146 IWineD3DSurface *surface;
1148 /* Use the callback to create the texture surface. */
1149 hr = IWineD3DDeviceParent_CreateSurface(device->device_parent, parent, tmp_w, tmp_h,
1150 format->id, usage, pool, i, 0, &surface);
1151 if (FAILED(hr))
1153 FIXME("Failed to create surface %p, hr %#x\n", texture, hr);
1154 basetexture_cleanup(texture);
1155 return hr;
1158 surface_set_container((IWineD3DSurfaceImpl *)surface, WINED3D_CONTAINER_TEXTURE, texture);
1159 surface_set_texture_target((IWineD3DSurfaceImpl *)surface, texture->baseTexture.target);
1160 texture->baseTexture.sub_resources[i] = &((IWineD3DSurfaceImpl *)surface)->resource;
1161 TRACE("Created surface level %u @ %p.\n", i, surface);
1162 /* Calculate the next mipmap level. */
1163 tmp_w = max(1, tmp_w >> 1);
1164 tmp_h = max(1, tmp_h >> 1);
1167 return WINED3D_OK;
1170 /* Context activation is done by the caller. */
1171 static HRESULT texture3d_bind(IWineD3DBaseTextureImpl *texture,
1172 const struct wined3d_gl_info *gl_info, BOOL srgb)
1174 BOOL dummy;
1176 TRACE("texture %p, gl_info %p, srgb %#x.\n", texture, gl_info, srgb);
1178 return basetexture_bind(texture, gl_info, srgb, &dummy);
1181 /* Do not call while under the GL lock. */
1182 static void texture3d_preload(IWineD3DBaseTextureImpl *texture, enum WINED3DSRGB srgb)
1184 IWineD3DDeviceImpl *device = texture->resource.device;
1185 struct wined3d_context *context = NULL;
1186 BOOL srgb_mode = texture->baseTexture.is_srgb;
1187 BOOL srgb_was_toggled = FALSE;
1188 unsigned int i;
1190 TRACE("texture %p, srgb %#x.\n", texture, srgb);
1192 if (!device->isInDraw)
1193 context = context_acquire(device, NULL);
1194 else if (texture->baseTexture.bindCount > 0)
1196 srgb_mode = device->stateBlock->state.sampler_states[texture->baseTexture.sampler][WINED3DSAMP_SRGBTEXTURE];
1197 srgb_was_toggled = texture->baseTexture.is_srgb != srgb_mode;
1198 texture->baseTexture.is_srgb = srgb_mode;
1201 /* If the texture is marked dirty or the sRGB sampler setting has changed
1202 * since the last load then reload the volumes. */
1203 if (texture->baseTexture.texture_rgb.dirty)
1205 for (i = 0; i < texture->baseTexture.level_count; ++i)
1207 volume_load(volume_from_resource(texture->baseTexture.sub_resources[i]), i, srgb_mode);
1210 else if (srgb_was_toggled)
1212 for (i = 0; i < texture->baseTexture.level_count; ++i)
1214 IWineD3DVolumeImpl *volume = volume_from_resource(texture->baseTexture.sub_resources[i]);
1215 volume_add_dirty_box(volume, NULL);
1216 volume_load(volume, i, srgb_mode);
1219 else
1221 TRACE("Texture %p not dirty, nothing to do.\n", texture);
1224 if (context)
1225 context_release(context);
1227 /* No longer dirty */
1228 texture->baseTexture.texture_rgb.dirty = FALSE;
1231 static void texture3d_sub_resource_add_dirty_region(struct wined3d_resource *sub_resource,
1232 const WINED3DBOX *dirty_region)
1234 volume_add_dirty_box(volume_from_resource(sub_resource), dirty_region);
1237 static void texture3d_sub_resource_cleanup(struct wined3d_resource *sub_resource)
1239 IWineD3DVolumeImpl *volume = volume_from_resource(sub_resource);
1241 /* Cleanup the container. */
1242 volume_set_container(volume, NULL);
1243 IWineD3DVolume_Release((IWineD3DVolume *)volume);
1246 /* Do not call while under the GL lock. */
1247 static void texture3d_unload(struct wined3d_resource *resource)
1249 IWineD3DBaseTextureImpl *texture = basetexture_from_resource(resource);
1250 UINT i;
1252 TRACE("texture %p.\n", texture);
1254 for (i = 0; i < texture->baseTexture.level_count; ++i)
1256 struct wined3d_resource *sub_resource = texture->baseTexture.sub_resources[i];
1257 sub_resource->resource_ops->resource_unload(sub_resource);
1260 basetexture_unload(texture);
1263 static const struct wined3d_texture_ops texture3d_ops =
1265 texture3d_bind,
1266 texture3d_preload,
1267 texture3d_sub_resource_add_dirty_region,
1268 texture3d_sub_resource_cleanup,
1271 static const struct wined3d_resource_ops texture3d_resource_ops =
1273 texture3d_unload,
1276 HRESULT volumetexture_init(IWineD3DBaseTextureImpl *texture, UINT width, UINT height,
1277 UINT depth, UINT levels, IWineD3DDeviceImpl *device, DWORD usage, enum wined3d_format_id format_id,
1278 WINED3DPOOL pool, void *parent, const struct wined3d_parent_ops *parent_ops)
1280 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
1281 const struct wined3d_format *format = wined3d_get_format(gl_info, format_id);
1282 UINT tmp_w, tmp_h, tmp_d;
1283 unsigned int i;
1284 HRESULT hr;
1286 /* TODO: It should only be possible to create textures for formats
1287 * that are reported as supported. */
1288 if (WINED3DFMT_UNKNOWN >= format_id)
1290 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture);
1291 return WINED3DERR_INVALIDCALL;
1294 if (!gl_info->supported[EXT_TEXTURE3D])
1296 WARN("(%p) : Texture cannot be created - no volume texture support.\n", texture);
1297 return WINED3DERR_INVALIDCALL;
1300 /* Calculate levels for mip mapping. */
1301 if (usage & WINED3DUSAGE_AUTOGENMIPMAP)
1303 if (!gl_info->supported[SGIS_GENERATE_MIPMAP])
1305 WARN("No mipmap generation support, returning D3DERR_INVALIDCALL.\n");
1306 return WINED3DERR_INVALIDCALL;
1309 if (levels > 1)
1311 WARN("D3DUSAGE_AUTOGENMIPMAP is set, and level count > 1, returning D3DERR_INVALIDCALL.\n");
1312 return WINED3DERR_INVALIDCALL;
1315 levels = 1;
1317 else if (!levels)
1319 levels = wined3d_log2i(max(max(width, height), depth)) + 1;
1320 TRACE("Calculated levels = %u.\n", levels);
1323 texture->lpVtbl = &wined3d_texture_vtbl;
1325 hr = basetexture_init((IWineD3DBaseTextureImpl *)texture, &texture3d_ops,
1326 1, levels, WINED3DRTYPE_VOLUMETEXTURE, device, usage, format, pool,
1327 parent, parent_ops, &texture3d_resource_ops);
1328 if (FAILED(hr))
1330 WARN("Failed to initialize basetexture, returning %#x.\n", hr);
1331 return hr;
1334 /* Is NP2 support for volumes needed? */
1335 texture->baseTexture.pow2Matrix[0] = 1.0f;
1336 texture->baseTexture.pow2Matrix[5] = 1.0f;
1337 texture->baseTexture.pow2Matrix[10] = 1.0f;
1338 texture->baseTexture.pow2Matrix[15] = 1.0f;
1339 texture->baseTexture.target = GL_TEXTURE_3D;
1341 /* Generate all the surfaces. */
1342 tmp_w = width;
1343 tmp_h = height;
1344 tmp_d = depth;
1346 for (i = 0; i < texture->baseTexture.level_count; ++i)
1348 IWineD3DVolume *volume;
1350 /* Create the volume. */
1351 hr = IWineD3DDeviceParent_CreateVolume(device->device_parent, parent,
1352 tmp_w, tmp_h, tmp_d, format_id, pool, usage, &volume);
1353 if (FAILED(hr))
1355 ERR("Creating a volume for the volume texture failed, hr %#x.\n", hr);
1356 basetexture_cleanup(texture);
1357 return hr;
1360 /* Set its container to this texture. */
1361 volume_set_container((IWineD3DVolumeImpl *)volume, texture);
1362 texture->baseTexture.sub_resources[i] = &((IWineD3DVolumeImpl *)volume)->resource;
1364 /* Calculate the next mipmap level. */
1365 tmp_w = max(1, tmp_w >> 1);
1366 tmp_h = max(1, tmp_h >> 1);
1367 tmp_d = max(1, tmp_d >> 1);
1370 return WINED3D_OK;