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