[WINED3D]
[reactos.git] / reactos / dll / directx / wine / wined3d / texture.c
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
7 *
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.
12 *
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.
17 *
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
21 */
22
23 #include "wined3d_private.h"
24
25 WINE_DEFAULT_DEBUG_CHANNEL(d3d_texture);
26
27 static HRESULT wined3d_texture_init(struct wined3d_texture *texture, const struct wined3d_texture_ops *texture_ops,
28 UINT layer_count, UINT level_count, const struct wined3d_resource_desc *desc, struct wined3d_device *device,
29 void *parent, const struct wined3d_parent_ops *parent_ops, const struct wined3d_resource_ops *resource_ops)
30 {
31 const struct wined3d_format *format = wined3d_get_format(&device->adapter->gl_info, desc->format);
32 HRESULT hr;
33
34 TRACE("texture %p, texture_ops %p, layer_count %u, level_count %u, resource_type %s, format %s, "
35 "multisample_type %#x, multisample_quality %#x, usage %s, pool %s, width %u, height %u, depth %u, "
36 "device %p, parent %p, parent_ops %p, resource_ops %p.\n",
37 texture, texture_ops, layer_count, level_count, debug_d3dresourcetype(desc->resource_type),
38 debug_d3dformat(desc->format), desc->multisample_type, desc->multisample_quality,
39 debug_d3dusage(desc->usage), debug_d3dpool(desc->pool), desc->width, desc->height, desc->depth,
40 device, parent, parent_ops, resource_ops);
41
42 if (FAILED(hr = resource_init(&texture->resource, device, desc->resource_type, format,
43 desc->multisample_type, desc->multisample_quality, desc->usage, desc->pool,
44 desc->width, desc->height, desc->depth, 0, parent, parent_ops, resource_ops)))
45 {
46 WARN("Failed to initialize resource, returning %#x\n", hr);
47 return hr;
48 }
49
50 texture->texture_ops = texture_ops;
51 texture->sub_resources = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
52 level_count * layer_count * sizeof(*texture->sub_resources));
53 if (!texture->sub_resources)
54 {
55 ERR("Failed to allocate sub-resource array.\n");
56 resource_cleanup(&texture->resource);
57 return E_OUTOFMEMORY;
58 }
59
60 texture->layer_count = layer_count;
61 texture->level_count = level_count;
62 texture->filter_type = (desc->usage & WINED3DUSAGE_AUTOGENMIPMAP) ? WINED3D_TEXF_LINEAR : WINED3D_TEXF_NONE;
63 texture->lod = 0;
64 texture->texture_rgb.dirty = TRUE;
65 texture->texture_srgb.dirty = TRUE;
66 texture->flags = WINED3D_TEXTURE_POW2_MAT_IDENT;
67
68 if (texture->resource.format->flags & WINED3DFMT_FLAG_FILTERING)
69 {
70 texture->min_mip_lookup = minMipLookup;
71 texture->mag_lookup = magLookup;
72 }
73 else
74 {
75 texture->min_mip_lookup = minMipLookup_noFilter;
76 texture->mag_lookup = magLookup_noFilter;
77 }
78
79 return WINED3D_OK;
80 }
81
82 /* A GL context is provided by the caller */
83 static void gltexture_delete(const struct wined3d_gl_info *gl_info, struct gl_texture *tex)
84 {
85 gl_info->gl_ops.gl.p_glDeleteTextures(1, &tex->name);
86 tex->name = 0;
87 }
88
89 static void wined3d_texture_unload(struct wined3d_texture *texture)
90 {
91 struct wined3d_device *device = texture->resource.device;
92 struct wined3d_context *context = NULL;
93
94 if (texture->texture_rgb.name || texture->texture_srgb.name)
95 {
96 context = context_acquire(device, NULL);
97 }
98
99 if (texture->texture_rgb.name)
100 gltexture_delete(context->gl_info, &texture->texture_rgb);
101
102 if (texture->texture_srgb.name)
103 gltexture_delete(context->gl_info, &texture->texture_srgb);
104
105 if (context) context_release(context);
106
107 wined3d_texture_set_dirty(texture, TRUE);
108
109 resource_unload(&texture->resource);
110 }
111
112 static void wined3d_texture_cleanup(struct wined3d_texture *texture)
113 {
114 UINT sub_count = texture->level_count * texture->layer_count;
115 UINT i;
116
117 TRACE("texture %p.\n", texture);
118
119 for (i = 0; i < sub_count; ++i)
120 {
121 struct wined3d_resource *sub_resource = texture->sub_resources[i];
122
123 if (sub_resource)
124 texture->texture_ops->texture_sub_resource_cleanup(sub_resource);
125 }
126
127 wined3d_texture_unload(texture);
128 HeapFree(GetProcessHeap(), 0, texture->sub_resources);
129 resource_cleanup(&texture->resource);
130 }
131
132 void wined3d_texture_set_dirty(struct wined3d_texture *texture, BOOL dirty)
133 {
134 texture->texture_rgb.dirty = dirty;
135 texture->texture_srgb.dirty = dirty;
136 }
137
138 /* Context activation is done by the caller. */
139 static HRESULT wined3d_texture_bind(struct wined3d_texture *texture,
140 struct wined3d_context *context, BOOL srgb, BOOL *set_surface_desc)
141 {
142 const struct wined3d_gl_info *gl_info = context->gl_info;
143 struct gl_texture *gl_tex;
144 BOOL new_texture = FALSE;
145 HRESULT hr = WINED3D_OK;
146 GLenum target;
147
148 TRACE("texture %p, context %p, srgb %#x, set_surface_desc %p.\n", texture, context, srgb, set_surface_desc);
149
150 /* sRGB mode cache for preload() calls outside drawprim. */
151 if (srgb)
152 texture->flags |= WINED3D_TEXTURE_IS_SRGB;
153 else
154 texture->flags &= ~WINED3D_TEXTURE_IS_SRGB;
155
156 gl_tex = wined3d_texture_get_gl_texture(texture, context->gl_info, srgb);
157 target = texture->target;
158
159 /* Generate a texture name if we don't already have one. */
160 if (!gl_tex->name)
161 {
162 *set_surface_desc = TRUE;
163 gl_info->gl_ops.gl.p_glGenTextures(1, &gl_tex->name);
164 checkGLcall("glGenTextures");
165 TRACE("Generated texture %d.\n", gl_tex->name);
166 if (texture->resource.pool == WINED3D_POOL_DEFAULT)
167 {
168 /* Tell OpenGL to try and keep this texture in video ram (well mostly). */
169 GLclampf tmp = 0.9f;
170 gl_info->gl_ops.gl.p_glPrioritizeTextures(1, &gl_tex->name, &tmp);
171 }
172 /* Initialise the state of the texture object to the OpenGL defaults,
173 * not the D3D defaults. */
174 gl_tex->states[WINED3DTEXSTA_ADDRESSU] = WINED3D_TADDRESS_WRAP;
175 gl_tex->states[WINED3DTEXSTA_ADDRESSV] = WINED3D_TADDRESS_WRAP;
176 gl_tex->states[WINED3DTEXSTA_ADDRESSW] = WINED3D_TADDRESS_WRAP;
177 gl_tex->states[WINED3DTEXSTA_BORDERCOLOR] = 0;
178 gl_tex->states[WINED3DTEXSTA_MAGFILTER] = WINED3D_TEXF_LINEAR;
179 gl_tex->states[WINED3DTEXSTA_MINFILTER] = WINED3D_TEXF_POINT; /* GL_NEAREST_MIPMAP_LINEAR */
180 gl_tex->states[WINED3DTEXSTA_MIPFILTER] = WINED3D_TEXF_LINEAR; /* GL_NEAREST_MIPMAP_LINEAR */
181 gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL] = 0;
182 gl_tex->states[WINED3DTEXSTA_MAXANISOTROPY] = 1;
183 if (context->gl_info->supported[EXT_TEXTURE_SRGB_DECODE])
184 gl_tex->states[WINED3DTEXSTA_SRGBTEXTURE] = TRUE;
185 else
186 gl_tex->states[WINED3DTEXSTA_SRGBTEXTURE] = srgb;
187 gl_tex->states[WINED3DTEXSTA_SHADOW] = FALSE;
188 wined3d_texture_set_dirty(texture, TRUE);
189 new_texture = TRUE;
190
191 if (texture->resource.usage & WINED3DUSAGE_AUTOGENMIPMAP)
192 {
193 /* This means double binding the texture at creation, but keeps
194 * the code simpler all in all, and the run-time path free from
195 * additional checks. */
196 context_bind_texture(context, target, gl_tex->name);
197 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_GENERATE_MIPMAP_SGIS, GL_TRUE);
198 checkGLcall("glTexParameteri(target, GL_GENERATE_MIPMAP_SGIS, GL_TRUE)");
199 }
200 }
201 else
202 {
203 *set_surface_desc = FALSE;
204 }
205
206 if (gl_tex->name)
207 {
208 context_bind_texture(context, target, gl_tex->name);
209 if (new_texture)
210 {
211 /* For a new texture we have to set the texture levels after
212 * binding the texture. Beware that texture rectangles do not
213 * support mipmapping, but set the maxmiplevel if we're relying
214 * on the partial GL_ARB_texture_non_power_of_two emulation with
215 * texture rectangles. (I.e., do not care about cond_np2 here,
216 * just look for GL_TEXTURE_RECTANGLE_ARB.) */
217 if (target != GL_TEXTURE_RECTANGLE_ARB)
218 {
219 TRACE("Setting GL_TEXTURE_MAX_LEVEL to %u.\n", texture->level_count - 1);
220 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, texture->level_count - 1);
221 checkGLcall("glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, texture->level_count)");
222 }
223 if (target == GL_TEXTURE_CUBE_MAP_ARB)
224 {
225 /* Cubemaps are always set to clamp, regardless of the sampler state. */
226 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
227 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
228 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
229 }
230 }
231 }
232 else
233 {
234 ERR("This texture doesn't have an OpenGL texture assigned to it.\n");
235 hr = WINED3DERR_INVALIDCALL;
236 }
237
238 return hr;
239 }
240
241 /* Context activation is done by the caller. */
242 static void apply_wrap(const struct wined3d_gl_info *gl_info, GLenum target,
243 enum wined3d_texture_address d3d_wrap, GLenum param, BOOL cond_np2)
244 {
245 GLint gl_wrap;
246
247 if (d3d_wrap < WINED3D_TADDRESS_WRAP || d3d_wrap > WINED3D_TADDRESS_MIRROR_ONCE)
248 {
249 FIXME("Unrecognized or unsupported texture address mode %#x.\n", d3d_wrap);
250 return;
251 }
252
253 /* Cubemaps are always set to clamp, regardless of the sampler state. */
254 if (target == GL_TEXTURE_CUBE_MAP_ARB
255 || (cond_np2 && d3d_wrap == WINED3D_TADDRESS_WRAP))
256 gl_wrap = GL_CLAMP_TO_EDGE;
257 else
258 gl_wrap = gl_info->wrap_lookup[d3d_wrap - WINED3D_TADDRESS_WRAP];
259
260 TRACE("Setting param %#x to %#x for target %#x.\n", param, gl_wrap, target);
261 gl_info->gl_ops.gl.p_glTexParameteri(target, param, gl_wrap);
262 checkGLcall("glTexParameteri(target, param, gl_wrap)");
263 }
264
265 /* Context activation is done by the caller (state handler). */
266 void wined3d_texture_apply_state_changes(struct wined3d_texture *texture,
267 const DWORD sampler_states[WINED3D_HIGHEST_SAMPLER_STATE + 1],
268 const struct wined3d_gl_info *gl_info)
269 {
270 BOOL cond_np2 = texture->flags & WINED3D_TEXTURE_COND_NP2;
271 GLenum target = texture->target;
272 struct gl_texture *gl_tex;
273 DWORD state;
274 DWORD aniso;
275
276 TRACE("texture %p, sampler_states %p.\n", texture, sampler_states);
277
278 gl_tex = wined3d_texture_get_gl_texture(texture, gl_info,
279 texture->flags & WINED3D_TEXTURE_IS_SRGB);
280
281 /* This function relies on the correct texture being bound and loaded. */
282
283 if (sampler_states[WINED3D_SAMP_ADDRESS_U] != gl_tex->states[WINED3DTEXSTA_ADDRESSU])
284 {
285 state = sampler_states[WINED3D_SAMP_ADDRESS_U];
286 apply_wrap(gl_info, target, state, GL_TEXTURE_WRAP_S, cond_np2);
287 gl_tex->states[WINED3DTEXSTA_ADDRESSU] = state;
288 }
289
290 if (sampler_states[WINED3D_SAMP_ADDRESS_V] != gl_tex->states[WINED3DTEXSTA_ADDRESSV])
291 {
292 state = sampler_states[WINED3D_SAMP_ADDRESS_V];
293 apply_wrap(gl_info, target, state, GL_TEXTURE_WRAP_T, cond_np2);
294 gl_tex->states[WINED3DTEXSTA_ADDRESSV] = state;
295 }
296
297 if (sampler_states[WINED3D_SAMP_ADDRESS_W] != gl_tex->states[WINED3DTEXSTA_ADDRESSW])
298 {
299 state = sampler_states[WINED3D_SAMP_ADDRESS_W];
300 apply_wrap(gl_info, target, state, GL_TEXTURE_WRAP_R, cond_np2);
301 gl_tex->states[WINED3DTEXSTA_ADDRESSW] = state;
302 }
303
304 if (sampler_states[WINED3D_SAMP_BORDER_COLOR] != gl_tex->states[WINED3DTEXSTA_BORDERCOLOR])
305 {
306 float col[4];
307
308 state = sampler_states[WINED3D_SAMP_BORDER_COLOR];
309 D3DCOLORTOGLFLOAT4(state, col);
310 TRACE("Setting border color for %#x to %#x.\n", target, state);
311 gl_info->gl_ops.gl.p_glTexParameterfv(target, GL_TEXTURE_BORDER_COLOR, &col[0]);
312 checkGLcall("glTexParameterfv(..., GL_TEXTURE_BORDER_COLOR, ...)");
313 gl_tex->states[WINED3DTEXSTA_BORDERCOLOR] = state;
314 }
315
316 if (sampler_states[WINED3D_SAMP_MAG_FILTER] != gl_tex->states[WINED3DTEXSTA_MAGFILTER])
317 {
318 GLint gl_value;
319
320 state = sampler_states[WINED3D_SAMP_MAG_FILTER];
321 if (state > WINED3D_TEXF_ANISOTROPIC)
322 FIXME("Unrecognized or unsupported MAGFILTER* value %d.\n", state);
323
324 gl_value = wined3d_gl_mag_filter(texture->mag_lookup,
325 min(max(state, WINED3D_TEXF_POINT), WINED3D_TEXF_LINEAR));
326 TRACE("ValueMAG=%#x setting MAGFILTER to %#x.\n", state, gl_value);
327 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAG_FILTER, gl_value);
328
329 gl_tex->states[WINED3DTEXSTA_MAGFILTER] = state;
330 }
331
332 if ((sampler_states[WINED3D_SAMP_MIN_FILTER] != gl_tex->states[WINED3DTEXSTA_MINFILTER]
333 || sampler_states[WINED3D_SAMP_MIP_FILTER] != gl_tex->states[WINED3DTEXSTA_MIPFILTER]
334 || sampler_states[WINED3D_SAMP_MAX_MIP_LEVEL] != gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL]))
335 {
336 GLint gl_value;
337
338 gl_tex->states[WINED3DTEXSTA_MIPFILTER] = sampler_states[WINED3D_SAMP_MIP_FILTER];
339 gl_tex->states[WINED3DTEXSTA_MINFILTER] = sampler_states[WINED3D_SAMP_MIN_FILTER];
340 gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL] = sampler_states[WINED3D_SAMP_MAX_MIP_LEVEL];
341
342 if (gl_tex->states[WINED3DTEXSTA_MINFILTER] > WINED3D_TEXF_ANISOTROPIC
343 || gl_tex->states[WINED3DTEXSTA_MIPFILTER] > WINED3D_TEXF_ANISOTROPIC)
344 {
345 FIXME("Unrecognized or unsupported MIN_FILTER value %#x MIP_FILTER value %#x.\n",
346 gl_tex->states[WINED3DTEXSTA_MINFILTER],
347 gl_tex->states[WINED3DTEXSTA_MIPFILTER]);
348 }
349 gl_value = wined3d_gl_min_mip_filter(texture->min_mip_lookup,
350 min(max(sampler_states[WINED3D_SAMP_MIN_FILTER], WINED3D_TEXF_POINT), WINED3D_TEXF_LINEAR),
351 min(max(sampler_states[WINED3D_SAMP_MIP_FILTER], WINED3D_TEXF_NONE), WINED3D_TEXF_LINEAR));
352
353 TRACE("ValueMIN=%#x, ValueMIP=%#x, setting MINFILTER to %#x.\n",
354 sampler_states[WINED3D_SAMP_MIN_FILTER],
355 sampler_states[WINED3D_SAMP_MIP_FILTER], gl_value);
356 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MIN_FILTER, gl_value);
357 checkGLcall("glTexParameter GL_TEXTURE_MIN_FILTER, ...");
358
359 if (!cond_np2)
360 {
361 if (gl_tex->states[WINED3DTEXSTA_MIPFILTER] == WINED3D_TEXF_NONE)
362 gl_value = texture->lod;
363 else if (gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL] >= texture->level_count)
364 gl_value = texture->level_count - 1;
365 else if (gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL] < texture->lod)
366 /* texture->lod is already clamped in the setter. */
367 gl_value = texture->lod;
368 else
369 gl_value = gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL];
370
371 /* Note that WINED3D_SAMP_MAX_MIP_LEVEL specifies the largest mipmap
372 * (default 0), while GL_TEXTURE_MAX_LEVEL specifies the smallest
373 * mimap used (default 1000). So WINED3D_SAMP_MAX_MIP_LEVEL
374 * corresponds to GL_TEXTURE_BASE_LEVEL. */
375 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_BASE_LEVEL, gl_value);
376 }
377 }
378
379 if ((gl_tex->states[WINED3DTEXSTA_MAGFILTER] != WINED3D_TEXF_ANISOTROPIC
380 && gl_tex->states[WINED3DTEXSTA_MINFILTER] != WINED3D_TEXF_ANISOTROPIC
381 && gl_tex->states[WINED3DTEXSTA_MIPFILTER] != WINED3D_TEXF_ANISOTROPIC)
382 || cond_np2)
383 aniso = 1;
384 else
385 aniso = sampler_states[WINED3D_SAMP_MAX_ANISOTROPY];
386
387 if (gl_tex->states[WINED3DTEXSTA_MAXANISOTROPY] != aniso)
388 {
389 if (gl_info->supported[EXT_TEXTURE_FILTER_ANISOTROPIC])
390 {
391 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAX_ANISOTROPY_EXT, aniso);
392 checkGLcall("glTexParameteri(GL_TEXTURE_MAX_ANISOTROPY_EXT, aniso)");
393 }
394 else
395 {
396 WARN("Anisotropic filtering not supported.\n");
397 }
398 gl_tex->states[WINED3DTEXSTA_MAXANISOTROPY] = aniso;
399 }
400
401 /* These should always be the same unless EXT_texture_sRGB_decode is supported. */
402 if (sampler_states[WINED3D_SAMP_SRGB_TEXTURE] != gl_tex->states[WINED3DTEXSTA_SRGBTEXTURE])
403 {
404 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_SRGB_DECODE_EXT,
405 sampler_states[WINED3D_SAMP_SRGB_TEXTURE] ? GL_DECODE_EXT : GL_SKIP_DECODE_EXT);
406 checkGLcall("glTexParameteri(GL_TEXTURE_SRGB_DECODE_EXT)");
407 gl_tex->states[WINED3DTEXSTA_SRGBTEXTURE] = sampler_states[WINED3D_SAMP_SRGB_TEXTURE];
408 }
409
410 if (!(texture->resource.format->flags & WINED3DFMT_FLAG_SHADOW)
411 != !gl_tex->states[WINED3DTEXSTA_SHADOW])
412 {
413 if (texture->resource.format->flags & WINED3DFMT_FLAG_SHADOW)
414 {
415 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_DEPTH_TEXTURE_MODE_ARB, GL_LUMINANCE);
416 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE_ARB);
417 checkGLcall("glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE_ARB)");
418 gl_tex->states[WINED3DTEXSTA_SHADOW] = TRUE;
419 }
420 else
421 {
422 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_NONE);
423 checkGLcall("glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_NONE)");
424 gl_tex->states[WINED3DTEXSTA_SHADOW] = FALSE;
425 }
426 }
427 }
428
429 ULONG CDECL wined3d_texture_incref(struct wined3d_texture *texture)
430 {
431 ULONG refcount = InterlockedIncrement(&texture->resource.ref);
432
433 TRACE("%p increasing refcount to %u.\n", texture, refcount);
434
435 return refcount;
436 }
437
438 /* Do not call while under the GL lock. */
439 ULONG CDECL wined3d_texture_decref(struct wined3d_texture *texture)
440 {
441 ULONG refcount = InterlockedDecrement(&texture->resource.ref);
442
443 TRACE("%p decreasing refcount to %u.\n", texture, refcount);
444
445 if (!refcount)
446 {
447 wined3d_texture_cleanup(texture);
448 texture->resource.parent_ops->wined3d_object_destroyed(texture->resource.parent);
449 HeapFree(GetProcessHeap(), 0, texture);
450 }
451
452 return refcount;
453 }
454
455 struct wined3d_resource * CDECL wined3d_texture_get_resource(struct wined3d_texture *texture)
456 {
457 TRACE("texture %p.\n", texture);
458
459 return &texture->resource;
460 }
461
462 DWORD CDECL wined3d_texture_set_priority(struct wined3d_texture *texture, DWORD priority)
463 {
464 return resource_set_priority(&texture->resource, priority);
465 }
466
467 DWORD CDECL wined3d_texture_get_priority(const struct wined3d_texture *texture)
468 {
469 return resource_get_priority(&texture->resource);
470 }
471
472 /* Do not call while under the GL lock. */
473 void CDECL wined3d_texture_preload(struct wined3d_texture *texture)
474 {
475 texture->texture_ops->texture_preload(texture, SRGB_ANY);
476 }
477
478 void * CDECL wined3d_texture_get_parent(const struct wined3d_texture *texture)
479 {
480 TRACE("texture %p.\n", texture);
481
482 return texture->resource.parent;
483 }
484
485 DWORD CDECL wined3d_texture_set_lod(struct wined3d_texture *texture, DWORD lod)
486 {
487 DWORD old = texture->lod;
488
489 TRACE("texture %p, lod %u.\n", texture, lod);
490
491 /* The d3d9:texture test shows that SetLOD is ignored on non-managed
492 * textures. The call always returns 0, and GetLOD always returns 0. */
493 if (texture->resource.pool != WINED3D_POOL_MANAGED)
494 {
495 TRACE("Ignoring SetLOD on %s texture, returning 0.\n", debug_d3dpool(texture->resource.pool));
496 return 0;
497 }
498
499 if (lod >= texture->level_count)
500 lod = texture->level_count - 1;
501
502 if (texture->lod != lod)
503 {
504 texture->lod = lod;
505
506 texture->texture_rgb.states[WINED3DTEXSTA_MAXMIPLEVEL] = ~0U;
507 texture->texture_srgb.states[WINED3DTEXSTA_MAXMIPLEVEL] = ~0U;
508 if (texture->resource.bind_count)
509 device_invalidate_state(texture->resource.device, STATE_SAMPLER(texture->sampler));
510 }
511
512 return old;
513 }
514
515 DWORD CDECL wined3d_texture_get_lod(const struct wined3d_texture *texture)
516 {
517 TRACE("texture %p, returning %u.\n", texture, texture->lod);
518
519 return texture->lod;
520 }
521
522 DWORD CDECL wined3d_texture_get_level_count(const struct wined3d_texture *texture)
523 {
524 TRACE("texture %p, returning %u.\n", texture, texture->level_count);
525
526 return texture->level_count;
527 }
528
529 HRESULT CDECL wined3d_texture_set_autogen_filter_type(struct wined3d_texture *texture,
530 enum wined3d_texture_filter_type filter_type)
531 {
532 FIXME("texture %p, filter_type %s stub!\n", texture, debug_d3dtexturefiltertype(filter_type));
533
534 if (!(texture->resource.usage & WINED3DUSAGE_AUTOGENMIPMAP))
535 {
536 WARN("Texture doesn't have AUTOGENMIPMAP usage.\n");
537 return WINED3DERR_INVALIDCALL;
538 }
539
540 texture->filter_type = filter_type;
541
542 return WINED3D_OK;
543 }
544
545 enum wined3d_texture_filter_type CDECL wined3d_texture_get_autogen_filter_type(const struct wined3d_texture *texture)
546 {
547 TRACE("texture %p.\n", texture);
548
549 return texture->filter_type;
550 }
551
552 void CDECL wined3d_texture_generate_mipmaps(struct wined3d_texture *texture)
553 {
554 /* TODO: Implement filters using GL_SGI_generate_mipmaps. */
555 FIXME("texture %p stub!\n", texture);
556 }
557
558 struct wined3d_resource * CDECL wined3d_texture_get_sub_resource(struct wined3d_texture *texture,
559 UINT sub_resource_idx)
560 {
561 UINT sub_count = texture->level_count * texture->layer_count;
562
563 TRACE("texture %p, sub_resource_idx %u.\n", texture, sub_resource_idx);
564
565 if (sub_resource_idx >= sub_count)
566 {
567 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx, sub_count);
568 return NULL;
569 }
570
571 return texture->sub_resources[sub_resource_idx];
572 }
573
574 HRESULT CDECL wined3d_texture_add_dirty_region(struct wined3d_texture *texture,
575 UINT layer, const struct wined3d_box *dirty_region)
576 {
577 struct wined3d_resource *sub_resource;
578
579 TRACE("texture %p, layer %u, dirty_region %p.\n", texture, layer, dirty_region);
580
581 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, layer * texture->level_count)))
582 {
583 WARN("Failed to get sub-resource.\n");
584 return WINED3DERR_INVALIDCALL;
585 }
586
587 wined3d_texture_set_dirty(texture, TRUE);
588 texture->texture_ops->texture_sub_resource_add_dirty_region(sub_resource, dirty_region);
589
590 return WINED3D_OK;
591 }
592
593 /* Context activation is done by the caller. */
594 static HRESULT texture2d_bind(struct wined3d_texture *texture,
595 struct wined3d_context *context, BOOL srgb)
596 {
597 const struct wined3d_gl_info *gl_info = context->gl_info;
598 BOOL set_gl_texture_desc;
599 HRESULT hr;
600
601 TRACE("texture %p, context %p, srgb %#x.\n", texture, context, srgb);
602
603 hr = wined3d_texture_bind(texture, context, srgb, &set_gl_texture_desc);
604 if (set_gl_texture_desc && SUCCEEDED(hr))
605 {
606 UINT sub_count = texture->level_count * texture->layer_count;
607 BOOL srgb_tex = !context->gl_info->supported[EXT_TEXTURE_SRGB_DECODE]
608 && (texture->flags & WINED3D_TEXTURE_IS_SRGB);
609 struct gl_texture *gl_tex;
610 UINT i;
611
612 gl_tex = wined3d_texture_get_gl_texture(texture, context->gl_info, srgb_tex);
613
614 for (i = 0; i < sub_count; ++i)
615 {
616 struct wined3d_surface *surface = surface_from_resource(texture->sub_resources[i]);
617 surface_set_texture_name(surface, gl_tex->name, srgb_tex);
618 }
619
620 /* Conditinal non power of two textures use a different clamping
621 * default. If we're using the GL_WINE_normalized_texrect partial
622 * driver emulation, we're dealing with a GL_TEXTURE_2D texture which
623 * has the address mode set to repeat - something that prevents us
624 * from hitting the accelerated codepath. Thus manually set the GL
625 * state. The same applies to filtering. Even if the texture has only
626 * one mip level, the default LINEAR_MIPMAP_LINEAR filter causes a SW
627 * fallback on macos. */
628 if (texture->flags & WINED3D_TEXTURE_COND_NP2)
629 {
630 GLenum target = texture->target;
631
632 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
633 checkGLcall("glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)");
634 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
635 checkGLcall("glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)");
636 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
637 checkGLcall("glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST)");
638 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
639 checkGLcall("glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST)");
640 gl_tex->states[WINED3DTEXSTA_ADDRESSU] = WINED3D_TADDRESS_CLAMP;
641 gl_tex->states[WINED3DTEXSTA_ADDRESSV] = WINED3D_TADDRESS_CLAMP;
642 gl_tex->states[WINED3DTEXSTA_MAGFILTER] = WINED3D_TEXF_POINT;
643 gl_tex->states[WINED3DTEXSTA_MINFILTER] = WINED3D_TEXF_POINT;
644 gl_tex->states[WINED3DTEXSTA_MIPFILTER] = WINED3D_TEXF_NONE;
645 }
646 }
647
648 return hr;
649 }
650
651 static BOOL texture_srgb_mode(const struct wined3d_texture *texture, enum WINED3DSRGB srgb)
652 {
653 switch (srgb)
654 {
655 case SRGB_RGB:
656 return FALSE;
657
658 case SRGB_SRGB:
659 return TRUE;
660
661 default:
662 return texture->flags & WINED3D_TEXTURE_IS_SRGB;
663 }
664 }
665
666 /* Do not call while under the GL lock. */
667 static void texture2d_preload(struct wined3d_texture *texture, enum WINED3DSRGB srgb)
668 {
669 UINT sub_count = texture->level_count * texture->layer_count;
670 struct wined3d_device *device = texture->resource.device;
671 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
672 struct wined3d_context *context = NULL;
673 struct gl_texture *gl_tex;
674 BOOL srgb_mode;
675 UINT i;
676
677 TRACE("texture %p, srgb %#x.\n", texture, srgb);
678
679 srgb_mode = texture_srgb_mode(texture, srgb);
680 gl_tex = wined3d_texture_get_gl_texture(texture, gl_info, srgb_mode);
681
682 if (!device->isInDraw)
683 {
684 /* No danger of recursive calls, context_acquire() sets isInDraw to TRUE
685 * when loading offscreen render targets into the texture. */
686 context = context_acquire(device, NULL);
687 }
688
689 if (gl_tex->dirty)
690 {
691 /* Reload the surfaces if the texture is marked dirty. */
692 for (i = 0; i < sub_count; ++i)
693 {
694 surface_load(surface_from_resource(texture->sub_resources[i]), srgb_mode);
695 }
696 }
697 else
698 {
699 TRACE("Texture %p not dirty, nothing to do.\n", texture);
700 }
701
702 /* No longer dirty. */
703 gl_tex->dirty = FALSE;
704
705 if (context) context_release(context);
706 }
707
708 static void texture2d_sub_resource_add_dirty_region(struct wined3d_resource *sub_resource,
709 const struct wined3d_box *dirty_region)
710 {
711 surface_add_dirty_rect(surface_from_resource(sub_resource), dirty_region);
712 }
713
714 static void texture2d_sub_resource_cleanup(struct wined3d_resource *sub_resource)
715 {
716 struct wined3d_surface *surface = surface_from_resource(sub_resource);
717
718 /* Clean out the texture name we gave to the surface so that the
719 * surface doesn't try and release it. */
720 surface_set_texture_name(surface, 0, TRUE);
721 surface_set_texture_name(surface, 0, FALSE);
722 surface_set_texture_target(surface, 0, 0);
723 surface_set_container(surface, NULL);
724 wined3d_surface_decref(surface);
725 }
726
727 /* Do not call while under the GL lock. */
728 static void texture2d_unload(struct wined3d_resource *resource)
729 {
730 struct wined3d_texture *texture = wined3d_texture_from_resource(resource);
731 UINT sub_count = texture->level_count * texture->layer_count;
732 UINT i;
733
734 TRACE("texture %p.\n", texture);
735
736 for (i = 0; i < sub_count; ++i)
737 {
738 struct wined3d_resource *sub_resource = texture->sub_resources[i];
739 struct wined3d_surface *surface = surface_from_resource(sub_resource);
740
741 sub_resource->resource_ops->resource_unload(sub_resource);
742 surface_set_texture_name(surface, 0, FALSE); /* Delete RGB name */
743 surface_set_texture_name(surface, 0, TRUE); /* Delete sRGB name */
744 }
745
746 wined3d_texture_unload(texture);
747 }
748
749 static const struct wined3d_texture_ops texture2d_ops =
750 {
751 texture2d_bind,
752 texture2d_preload,
753 texture2d_sub_resource_add_dirty_region,
754 texture2d_sub_resource_cleanup,
755 };
756
757 static const struct wined3d_resource_ops texture2d_resource_ops =
758 {
759 texture2d_unload,
760 };
761
762 static HRESULT cubetexture_init(struct wined3d_texture *texture, const struct wined3d_resource_desc *desc,
763 UINT levels, DWORD surface_flags, struct wined3d_device *device, void *parent,
764 const struct wined3d_parent_ops *parent_ops)
765 {
766 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
767 struct wined3d_resource_desc surface_desc;
768 unsigned int i, j;
769 HRESULT hr;
770
771 /* TODO: It should only be possible to create textures for formats
772 * that are reported as supported. */
773 if (WINED3DFMT_UNKNOWN >= desc->format)
774 {
775 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture);
776 return WINED3DERR_INVALIDCALL;
777 }
778
779 if (!gl_info->supported[ARB_TEXTURE_CUBE_MAP] && desc->pool != WINED3D_POOL_SCRATCH)
780 {
781 WARN("(%p) : Tried to create not supported cube texture.\n", texture);
782 return WINED3DERR_INVALIDCALL;
783 }
784
785 /* Calculate levels for mip mapping */
786 if (desc->usage & WINED3DUSAGE_AUTOGENMIPMAP)
787 {
788 if (!gl_info->supported[SGIS_GENERATE_MIPMAP])
789 {
790 WARN("No mipmap generation support, returning D3DERR_INVALIDCALL.\n");
791 return WINED3DERR_INVALIDCALL;
792 }
793
794 if (levels > 1)
795 {
796 WARN("D3DUSAGE_AUTOGENMIPMAP is set, and level count > 1, returning D3DERR_INVALIDCALL.\n");
797 return WINED3DERR_INVALIDCALL;
798 }
799
800 levels = 1;
801 }
802 else if (!levels)
803 {
804 levels = wined3d_log2i(desc->width) + 1;
805 TRACE("Calculated levels = %u.\n", levels);
806 }
807
808 if (!gl_info->supported[ARB_TEXTURE_NON_POWER_OF_TWO])
809 {
810 UINT pow2_edge_length = 1;
811 while (pow2_edge_length < desc->width)
812 pow2_edge_length <<= 1;
813
814 if (desc->width != pow2_edge_length)
815 {
816 if (desc->pool == WINED3D_POOL_SCRATCH)
817 {
818 /* SCRATCH textures cannot be used for texturing */
819 WARN("Creating a scratch NPOT cube texture despite lack of HW support.\n");
820 }
821 else
822 {
823 WARN("Attempted to create a NPOT cube texture (edge length %u) without GL support.\n", desc->width);
824 return WINED3DERR_INVALIDCALL;
825 }
826 }
827 }
828
829 if (FAILED(hr = wined3d_texture_init(texture, &texture2d_ops, 6, levels,
830 desc, device, parent, parent_ops, &texture2d_resource_ops)))
831 {
832 WARN("Failed to initialize texture, returning %#x\n", hr);
833 return hr;
834 }
835
836 texture->pow2_matrix[0] = 1.0f;
837 texture->pow2_matrix[5] = 1.0f;
838 texture->pow2_matrix[10] = 1.0f;
839 texture->pow2_matrix[15] = 1.0f;
840 texture->target = GL_TEXTURE_CUBE_MAP_ARB;
841
842 /* Generate all the surfaces. */
843 surface_desc = *desc;
844 surface_desc.resource_type = WINED3D_RTYPE_SURFACE;
845 for (i = 0; i < texture->level_count; ++i)
846 {
847 /* Create the 6 faces. */
848 for (j = 0; j < 6; ++j)
849 {
850 static const GLenum cube_targets[6] =
851 {
852 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB,
853 GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB,
854 GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB,
855 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB,
856 GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB,
857 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB,
858 };
859 UINT idx = j * texture->level_count + i;
860 struct wined3d_surface *surface;
861
862 if (FAILED(hr = device->device_parent->ops->create_texture_surface(device->device_parent,
863 parent, &surface_desc, idx, surface_flags, &surface)))
864 {
865 FIXME("(%p) Failed to create surface, hr %#x.\n", texture, hr);
866 wined3d_texture_cleanup(texture);
867 return hr;
868 }
869
870 surface_set_container(surface, texture);
871 surface_set_texture_target(surface, cube_targets[j], i);
872 texture->sub_resources[idx] = &surface->resource;
873 TRACE("Created surface level %u @ %p.\n", i, surface);
874 }
875 surface_desc.width = max(1, surface_desc.width >> 1);
876 surface_desc.height = surface_desc.width;
877 }
878
879 return WINED3D_OK;
880 }
881
882 static HRESULT texture_init(struct wined3d_texture *texture, const struct wined3d_resource_desc *desc,
883 UINT levels, DWORD surface_flags, struct wined3d_device *device, void *parent,
884 const struct wined3d_parent_ops *parent_ops)
885 {
886 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
887 struct wined3d_resource_desc surface_desc;
888 UINT pow2_width, pow2_height;
889 unsigned int i;
890 HRESULT hr;
891
892 /* TODO: It should only be possible to create textures for formats
893 * that are reported as supported. */
894 if (WINED3DFMT_UNKNOWN >= desc->format)
895 {
896 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture);
897 return WINED3DERR_INVALIDCALL;
898 }
899
900 /* Non-power2 support. */
901 if (gl_info->supported[ARB_TEXTURE_NON_POWER_OF_TWO])
902 {
903 pow2_width = desc->width;
904 pow2_height = desc->height;
905 }
906 else
907 {
908 /* Find the nearest pow2 match. */
909 pow2_width = pow2_height = 1;
910 while (pow2_width < desc->width)
911 pow2_width <<= 1;
912 while (pow2_height < desc->height)
913 pow2_height <<= 1;
914
915 if (pow2_width != desc->width || pow2_height != desc->height)
916 {
917 /* levels == 0 returns an error as well */
918 if (levels != 1)
919 {
920 if (desc->pool == WINED3D_POOL_SCRATCH)
921 {
922 WARN("Creating a scratch mipmapped NPOT texture despite lack of HW support.\n");
923 }
924 else
925 {
926 WARN("Attempted to create a mipmapped NPOT texture without unconditional NPOT support.\n");
927 return WINED3DERR_INVALIDCALL;
928 }
929 }
930 }
931 }
932
933 /* Calculate levels for mip mapping. */
934 if (desc->usage & WINED3DUSAGE_AUTOGENMIPMAP)
935 {
936 if (!gl_info->supported[SGIS_GENERATE_MIPMAP])
937 {
938 WARN("No mipmap generation support, returning WINED3DERR_INVALIDCALL.\n");
939 return WINED3DERR_INVALIDCALL;
940 }
941
942 if (levels > 1)
943 {
944 WARN("D3DUSAGE_AUTOGENMIPMAP is set, and level count > 1, returning WINED3DERR_INVALIDCALL.\n");
945 return WINED3DERR_INVALIDCALL;
946 }
947
948 levels = 1;
949 }
950 else if (!levels)
951 {
952 levels = wined3d_log2i(max(desc->width, desc->height)) + 1;
953 TRACE("Calculated levels = %u.\n", levels);
954 }
955
956 if (FAILED(hr = wined3d_texture_init(texture, &texture2d_ops, 1, levels,
957 desc, device, parent, parent_ops, &texture2d_resource_ops)))
958 {
959 WARN("Failed to initialize texture, returning %#x.\n", hr);
960 return hr;
961 }
962
963 /* Precalculated scaling for 'faked' non power of two texture coords. */
964 if (gl_info->supported[WINED3D_GL_NORMALIZED_TEXRECT]
965 && (desc->width != pow2_width || desc->height != pow2_height))
966 {
967 texture->pow2_matrix[0] = 1.0f;
968 texture->pow2_matrix[5] = 1.0f;
969 texture->pow2_matrix[10] = 1.0f;
970 texture->pow2_matrix[15] = 1.0f;
971 texture->target = GL_TEXTURE_2D;
972 texture->flags |= WINED3D_TEXTURE_COND_NP2;
973 texture->min_mip_lookup = minMipLookup_noFilter;
974 }
975 else if (gl_info->supported[ARB_TEXTURE_RECTANGLE]
976 && (desc->width != pow2_width || desc->height != pow2_height))
977 {
978 texture->pow2_matrix[0] = (float)desc->width;
979 texture->pow2_matrix[5] = (float)desc->height;
980 texture->pow2_matrix[10] = 1.0f;
981 texture->pow2_matrix[15] = 1.0f;
982 texture->target = GL_TEXTURE_RECTANGLE_ARB;
983 texture->flags |= WINED3D_TEXTURE_COND_NP2;
984 texture->flags &= ~WINED3D_TEXTURE_POW2_MAT_IDENT;
985
986 if (texture->resource.format->flags & WINED3DFMT_FLAG_FILTERING)
987 texture->min_mip_lookup = minMipLookup_noMip;
988 else
989 texture->min_mip_lookup = minMipLookup_noFilter;
990 }
991 else
992 {
993 if ((desc->width != pow2_width) || (desc->height != pow2_height))
994 {
995 texture->pow2_matrix[0] = (((float)desc->width) / ((float)pow2_width));
996 texture->pow2_matrix[5] = (((float)desc->height) / ((float)pow2_height));
997 texture->flags &= ~WINED3D_TEXTURE_POW2_MAT_IDENT;
998 }
999 else
1000 {
1001 texture->pow2_matrix[0] = 1.0f;
1002 texture->pow2_matrix[5] = 1.0f;
1003 }
1004
1005 texture->pow2_matrix[10] = 1.0f;
1006 texture->pow2_matrix[15] = 1.0f;
1007 texture->target = GL_TEXTURE_2D;
1008 }
1009 TRACE("xf(%f) yf(%f)\n", texture->pow2_matrix[0], texture->pow2_matrix[5]);
1010
1011 /* Generate all the surfaces. */
1012 surface_desc = *desc;
1013 surface_desc.resource_type = WINED3D_RTYPE_SURFACE;
1014 for (i = 0; i < texture->level_count; ++i)
1015 {
1016 struct wined3d_surface *surface;
1017
1018 /* Use the callback to create the texture surface. */
1019 if (FAILED(hr = device->device_parent->ops->create_texture_surface(device->device_parent,
1020 parent, &surface_desc, i, surface_flags, &surface)))
1021 {
1022 FIXME("Failed to create surface %p, hr %#x\n", texture, hr);
1023 wined3d_texture_cleanup(texture);
1024 return hr;
1025 }
1026
1027 surface_set_container(surface, texture);
1028 surface_set_texture_target(surface, texture->target, i);
1029 texture->sub_resources[i] = &surface->resource;
1030 TRACE("Created surface level %u @ %p.\n", i, surface);
1031 /* Calculate the next mipmap level. */
1032 surface_desc.width = max(1, surface_desc.width >> 1);
1033 surface_desc.height = max(1, surface_desc.height >> 1);
1034 }
1035
1036 return WINED3D_OK;
1037 }
1038
1039 /* Context activation is done by the caller. */
1040 static HRESULT texture3d_bind(struct wined3d_texture *texture,
1041 struct wined3d_context *context, BOOL srgb)
1042 {
1043 BOOL dummy;
1044
1045 TRACE("texture %p, context %p, srgb %#x.\n", texture, context, srgb);
1046
1047 return wined3d_texture_bind(texture, context, srgb, &dummy);
1048 }
1049
1050 /* Do not call while under the GL lock. */
1051 static void texture3d_preload(struct wined3d_texture *texture, enum WINED3DSRGB srgb)
1052 {
1053 UINT sub_count = texture->level_count * texture->layer_count;
1054 struct wined3d_device *device = texture->resource.device;
1055 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
1056 struct wined3d_context *context = NULL;
1057 struct gl_texture *gl_tex;
1058 BOOL srgb_mode;
1059 UINT i;
1060
1061 TRACE("texture %p, srgb %#x.\n", texture, srgb);
1062
1063 srgb_mode = texture_srgb_mode(texture, srgb);
1064 gl_tex = wined3d_texture_get_gl_texture(texture, gl_info, srgb_mode);
1065
1066 if (gl_tex->dirty)
1067 {
1068 context = context_acquire(device, NULL);
1069
1070 /* Reload the surfaces if the texture is marked dirty. */
1071 for (i = 0; i < sub_count; ++i)
1072 {
1073 wined3d_volume_load(volume_from_resource(texture->sub_resources[i]), context,
1074 srgb_mode);
1075 }
1076
1077 context_release(context);
1078 }
1079 else
1080 {
1081 TRACE("Texture %p not dirty, nothing to do.\n", texture);
1082 }
1083
1084 /* No longer dirty. */
1085 gl_tex->dirty = FALSE;
1086 }
1087
1088 static void texture3d_sub_resource_add_dirty_region(struct wined3d_resource *sub_resource,
1089 const struct wined3d_box *dirty_region)
1090 {
1091 }
1092
1093 static void texture3d_sub_resource_cleanup(struct wined3d_resource *sub_resource)
1094 {
1095 struct wined3d_volume *volume = volume_from_resource(sub_resource);
1096
1097 /* Cleanup the container. */
1098 volume_set_container(volume, NULL);
1099 wined3d_volume_decref(volume);
1100 }
1101
1102 /* Do not call while under the GL lock. */
1103 static void texture3d_unload(struct wined3d_resource *resource)
1104 {
1105 struct wined3d_texture *texture = wined3d_texture_from_resource(resource);
1106 UINT i;
1107
1108 TRACE("texture %p.\n", texture);
1109
1110 for (i = 0; i < texture->level_count; ++i)
1111 {
1112 struct wined3d_resource *sub_resource = texture->sub_resources[i];
1113 sub_resource->resource_ops->resource_unload(sub_resource);
1114 }
1115
1116 wined3d_texture_unload(texture);
1117 }
1118
1119 static const struct wined3d_texture_ops texture3d_ops =
1120 {
1121 texture3d_bind,
1122 texture3d_preload,
1123 texture3d_sub_resource_add_dirty_region,
1124 texture3d_sub_resource_cleanup,
1125 };
1126
1127 static const struct wined3d_resource_ops texture3d_resource_ops =
1128 {
1129 texture3d_unload,
1130 };
1131
1132 static HRESULT volumetexture_init(struct wined3d_texture *texture, const struct wined3d_resource_desc *desc,
1133 UINT levels, struct wined3d_device *device, void *parent, const struct wined3d_parent_ops *parent_ops)
1134 {
1135 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
1136 UINT tmp_w, tmp_h, tmp_d;
1137 unsigned int i;
1138 HRESULT hr;
1139
1140 /* TODO: It should only be possible to create textures for formats
1141 * that are reported as supported. */
1142 if (WINED3DFMT_UNKNOWN >= desc->format)
1143 {
1144 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture);
1145 return WINED3DERR_INVALIDCALL;
1146 }
1147
1148 if (!gl_info->supported[EXT_TEXTURE3D])
1149 {
1150 WARN("(%p) : Texture cannot be created - no volume texture support.\n", texture);
1151 return WINED3DERR_INVALIDCALL;
1152 }
1153
1154 /* Calculate levels for mip mapping. */
1155 if (desc->usage & WINED3DUSAGE_AUTOGENMIPMAP)
1156 {
1157 if (!gl_info->supported[SGIS_GENERATE_MIPMAP])
1158 {
1159 WARN("No mipmap generation support, returning D3DERR_INVALIDCALL.\n");
1160 return WINED3DERR_INVALIDCALL;
1161 }
1162
1163 if (levels > 1)
1164 {
1165 WARN("D3DUSAGE_AUTOGENMIPMAP is set, and level count > 1, returning D3DERR_INVALIDCALL.\n");
1166 return WINED3DERR_INVALIDCALL;
1167 }
1168
1169 levels = 1;
1170 }
1171 else if (!levels)
1172 {
1173 levels = wined3d_log2i(max(max(desc->width, desc->height), desc->depth)) + 1;
1174 TRACE("Calculated levels = %u.\n", levels);
1175 }
1176
1177 if (!gl_info->supported[ARB_TEXTURE_NON_POWER_OF_TWO])
1178 {
1179 UINT pow2_w, pow2_h, pow2_d;
1180 pow2_w = 1;
1181 while (pow2_w < desc->width)
1182 pow2_w <<= 1;
1183 pow2_h = 1;
1184 while (pow2_h < desc->height)
1185 pow2_h <<= 1;
1186 pow2_d = 1;
1187 while (pow2_d < desc->depth)
1188 pow2_d <<= 1;
1189
1190 if (pow2_w != desc->width || pow2_h != desc->height || pow2_d != desc->depth)
1191 {
1192 if (desc->pool == WINED3D_POOL_SCRATCH)
1193 {
1194 WARN("Creating a scratch NPOT volume texture despite lack of HW support.\n");
1195 }
1196 else
1197 {
1198 WARN("Attempted to create a NPOT volume texture (%u, %u, %u) without GL support.\n",
1199 desc->width, desc->height, desc->depth);
1200 return WINED3DERR_INVALIDCALL;
1201 }
1202 }
1203 }
1204
1205 if (FAILED(hr = wined3d_texture_init(texture, &texture3d_ops, 1, levels,
1206 desc, device, parent, parent_ops, &texture3d_resource_ops)))
1207 {
1208 WARN("Failed to initialize texture, returning %#x.\n", hr);
1209 return hr;
1210 }
1211
1212 texture->pow2_matrix[0] = 1.0f;
1213 texture->pow2_matrix[5] = 1.0f;
1214 texture->pow2_matrix[10] = 1.0f;
1215 texture->pow2_matrix[15] = 1.0f;
1216 texture->target = GL_TEXTURE_3D;
1217
1218 /* Generate all the surfaces. */
1219 tmp_w = desc->width;
1220 tmp_h = desc->height;
1221 tmp_d = desc->depth;
1222
1223 for (i = 0; i < texture->level_count; ++i)
1224 {
1225 struct wined3d_volume *volume;
1226
1227 /* Create the volume. */
1228 hr = device->device_parent->ops->create_volume(device->device_parent, parent,
1229 tmp_w, tmp_h, tmp_d, i, desc->format, desc->pool, desc->usage, &volume);
1230 if (FAILED(hr))
1231 {
1232 ERR("Creating a volume for the volume texture failed, hr %#x.\n", hr);
1233 wined3d_texture_cleanup(texture);
1234 return hr;
1235 }
1236
1237 /* Set its container to this texture. */
1238 volume_set_container(volume, texture);
1239 texture->sub_resources[i] = &volume->resource;
1240
1241 /* Calculate the next mipmap level. */
1242 tmp_w = max(1, tmp_w >> 1);
1243 tmp_h = max(1, tmp_h >> 1);
1244 tmp_d = max(1, tmp_d >> 1);
1245 }
1246
1247 return WINED3D_OK;
1248 }
1249
1250 HRESULT CDECL wined3d_texture_create_2d(struct wined3d_device *device, const struct wined3d_resource_desc *desc,
1251 UINT level_count, DWORD surface_flags, void *parent, const struct wined3d_parent_ops *parent_ops,
1252 struct wined3d_texture **texture)
1253 {
1254 struct wined3d_texture *object;
1255 HRESULT hr;
1256
1257 TRACE("device %p, desc %p, level_count %u, surface_flags %#x, parent %p, parent_ops %p, texture %p.\n",
1258 device, desc, level_count, surface_flags, parent, parent_ops, texture);
1259
1260 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1261 if (!object)
1262 {
1263 *texture = NULL;
1264 return WINED3DERR_OUTOFVIDEOMEMORY;
1265 }
1266
1267 if (FAILED(hr = texture_init(object, desc, level_count, surface_flags, device, parent, parent_ops)))
1268 {
1269 WARN("Failed to initialize texture, returning %#x.\n", hr);
1270 HeapFree(GetProcessHeap(), 0, object);
1271 *texture = NULL;
1272 return hr;
1273 }
1274
1275 TRACE("Created texture %p.\n", object);
1276 *texture = object;
1277
1278 return WINED3D_OK;
1279 }
1280
1281 HRESULT CDECL wined3d_texture_create_3d(struct wined3d_device *device, const struct wined3d_resource_desc *desc,
1282 UINT level_count, void *parent, const struct wined3d_parent_ops *parent_ops, struct wined3d_texture **texture)
1283 {
1284 struct wined3d_texture *object;
1285 HRESULT hr;
1286
1287 TRACE("device %p, desc %p, level_count %u, parent %p, parent_ops %p, texture %p.\n",
1288 device, desc, level_count, parent, parent_ops, texture);
1289
1290 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1291 if (!object)
1292 {
1293 *texture = NULL;
1294 return WINED3DERR_OUTOFVIDEOMEMORY;
1295 }
1296
1297 if (FAILED(hr = volumetexture_init(object, desc, level_count, device, parent, parent_ops)))
1298 {
1299 WARN("Failed to initialize volumetexture, returning %#x\n", hr);
1300 HeapFree(GetProcessHeap(), 0, object);
1301 *texture = NULL;
1302 return hr;
1303 }
1304
1305 TRACE("Created texture %p.\n", object);
1306 *texture = object;
1307
1308 return WINED3D_OK;
1309 }
1310
1311 HRESULT CDECL wined3d_texture_create_cube(struct wined3d_device *device, const struct wined3d_resource_desc *desc,
1312 UINT level_count, DWORD surface_flags, void *parent, const struct wined3d_parent_ops *parent_ops,
1313 struct wined3d_texture **texture)
1314 {
1315 struct wined3d_texture *object;
1316 HRESULT hr;
1317
1318 TRACE("device %p, desc %p, level_count %u, surface_flags %#x, parent %p, parent_ops %p, texture %p.\n",
1319 device, desc, level_count, surface_flags, parent, parent_ops, texture);
1320
1321 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1322 if (!object)
1323 {
1324 *texture = NULL;
1325 return WINED3DERR_OUTOFVIDEOMEMORY;
1326 }
1327
1328 if (FAILED(hr = cubetexture_init(object, desc, level_count, surface_flags, device, parent, parent_ops)))
1329 {
1330 WARN("Failed to initialize cubetexture, returning %#x\n", hr);
1331 HeapFree(GetProcessHeap(), 0, object);
1332 *texture = NULL;
1333 return hr;
1334 }
1335
1336 TRACE("Created texture %p.\n", object);
1337 *texture = object;
1338
1339 return WINED3D_OK;
1340 }