2 * Copyright 2002-2005 Jason Edmeades
3 * Copyright 2002-2005 Raphael Junqueira
4 * Copyright 2005 Oliver Stieber
5 * Copyright 2007-2008 Stefan Dösinger for CodeWeavers
6 * Copyright 2009-2011 Henri Verbeet for CodeWeavers
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #include "wined3d_private.h"
26 WINE_DEFAULT_DEBUG_CHANNEL(d3d_texture
);
28 static HRESULT
wined3d_texture_init(struct wined3d_texture
*texture
, const struct wined3d_texture_ops
*texture_ops
,
29 UINT layer_count
, UINT level_count
, WINED3DRESOURCETYPE resource_type
, IWineD3DDeviceImpl
*device
,
30 DWORD usage
, const struct wined3d_format
*format
, WINED3DPOOL pool
, void *parent
,
31 const struct wined3d_parent_ops
*parent_ops
, const struct wined3d_resource_ops
*resource_ops
)
35 hr
= resource_init(&texture
->resource
, device
, resource_type
, format
,
36 WINED3DMULTISAMPLE_NONE
, 0, usage
, pool
, 0, 0, 0, 0,
37 parent
, parent_ops
, resource_ops
);
40 WARN("Failed to initialize resource, returning %#x\n", hr
);
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
)
49 ERR("Failed to allocate sub-resource array.\n");
50 resource_cleanup(&texture
->resource
);
54 texture
->layer_count
= layer_count
;
55 texture
->level_count
= level_count
;
56 texture
->filter_type
= (usage
& WINED3DUSAGE_AUTOGENMIPMAP
) ? WINED3DTEXF_LINEAR
: WINED3DTEXF_NONE
;
58 texture
->texture_rgb
.dirty
= TRUE
;
59 texture
->texture_srgb
.dirty
= TRUE
;
60 texture
->is_srgb
= FALSE
;
61 texture
->pow2_matrix_identity
= TRUE
;
63 if (texture
->resource
.format
->flags
& WINED3DFMT_FLAG_FILTERING
)
65 texture
->min_mip_lookup
= minMipLookup
;
66 texture
->mag_lookup
= magLookup
;
70 texture
->min_mip_lookup
= minMipLookup_noFilter
;
71 texture
->mag_lookup
= magLookup_noFilter
;
77 /* A GL context is provided by the caller */
78 static void gltexture_delete(struct gl_texture
*tex
)
81 glDeleteTextures(1, &tex
->name
);
86 static void wined3d_texture_unload(struct wined3d_texture
*texture
)
88 IWineD3DDeviceImpl
*device
= texture
->resource
.device
;
89 struct wined3d_context
*context
= NULL
;
91 if (texture
->texture_rgb
.name
|| texture
->texture_srgb
.name
)
93 context
= context_acquire(device
, NULL
);
96 if (texture
->texture_rgb
.name
)
97 gltexture_delete(&texture
->texture_rgb
);
99 if (texture
->texture_srgb
.name
)
100 gltexture_delete(&texture
->texture_srgb
);
102 if (context
) context_release(context
);
104 wined3d_texture_set_dirty(texture
, TRUE
);
106 resource_unload(&texture
->resource
);
109 static void wined3d_texture_cleanup(struct wined3d_texture
*texture
)
111 UINT sub_count
= texture
->level_count
* texture
->layer_count
;
114 TRACE("texture %p.\n", texture
);
116 for (i
= 0; i
< sub_count
; ++i
)
118 struct wined3d_resource
*sub_resource
= texture
->sub_resources
[i
];
121 texture
->texture_ops
->texture_sub_resource_cleanup(sub_resource
);
124 wined3d_texture_unload(texture
);
125 HeapFree(GetProcessHeap(), 0, texture
->sub_resources
);
126 resource_cleanup(&texture
->resource
);
129 void wined3d_texture_set_dirty(struct wined3d_texture
*texture
, BOOL dirty
)
131 texture
->texture_rgb
.dirty
= dirty
;
132 texture
->texture_srgb
.dirty
= dirty
;
135 /* Context activation is done by the caller. */
136 static HRESULT
wined3d_texture_bind(struct wined3d_texture
*texture
,
137 const struct wined3d_gl_info
*gl_info
, BOOL srgb
, BOOL
*set_surface_desc
)
139 struct gl_texture
*gl_tex
;
140 BOOL new_texture
= FALSE
;
141 HRESULT hr
= WINED3D_OK
;
144 TRACE("texture %p, srgb %#x, set_surface_desc %p.\n", texture
, srgb
, set_surface_desc
);
146 texture
->is_srgb
= srgb
; /* sRGB mode cache for preload() calls outside drawprim. */
147 gl_tex
= wined3d_texture_get_gl_texture(texture
, gl_info
, srgb
);
148 target
= texture
->target
;
151 /* Generate a texture name if we don't already have one. */
154 *set_surface_desc
= TRUE
;
155 glGenTextures(1, &gl_tex
->name
);
156 checkGLcall("glGenTextures");
157 TRACE("Generated texture %d.\n", gl_tex
->name
);
158 if (texture
->resource
.pool
== WINED3DPOOL_DEFAULT
)
160 /* Tell OpenGL to try and keep this texture in video ram (well mostly). */
162 glPrioritizeTextures(1, &gl_tex
->name
, &tmp
);
164 /* Initialise the state of the texture object to the OpenGL defaults,
165 * not the D3D defaults. */
166 gl_tex
->states
[WINED3DTEXSTA_ADDRESSU
] = WINED3DTADDRESS_WRAP
;
167 gl_tex
->states
[WINED3DTEXSTA_ADDRESSV
] = WINED3DTADDRESS_WRAP
;
168 gl_tex
->states
[WINED3DTEXSTA_ADDRESSW
] = WINED3DTADDRESS_WRAP
;
169 gl_tex
->states
[WINED3DTEXSTA_BORDERCOLOR
] = 0;
170 gl_tex
->states
[WINED3DTEXSTA_MAGFILTER
] = WINED3DTEXF_LINEAR
;
171 gl_tex
->states
[WINED3DTEXSTA_MINFILTER
] = WINED3DTEXF_POINT
; /* GL_NEAREST_MIPMAP_LINEAR */
172 gl_tex
->states
[WINED3DTEXSTA_MIPFILTER
] = WINED3DTEXF_LINEAR
; /* GL_NEAREST_MIPMAP_LINEAR */
173 gl_tex
->states
[WINED3DTEXSTA_MAXMIPLEVEL
] = 0;
174 gl_tex
->states
[WINED3DTEXSTA_MAXANISOTROPY
] = 1;
175 if (gl_info
->supported
[EXT_TEXTURE_SRGB_DECODE
])
176 gl_tex
->states
[WINED3DTEXSTA_SRGBTEXTURE
] = TRUE
;
178 gl_tex
->states
[WINED3DTEXSTA_SRGBTEXTURE
] = srgb
;
179 gl_tex
->states
[WINED3DTEXSTA_SHADOW
] = FALSE
;
180 wined3d_texture_set_dirty(texture
, TRUE
);
183 if (texture
->resource
.usage
& WINED3DUSAGE_AUTOGENMIPMAP
)
185 /* This means double binding the texture at creation, but keeps
186 * the code simpler all in all, and the run-time path free from
187 * additional checks. */
188 glBindTexture(target
, gl_tex
->name
);
189 checkGLcall("glBindTexture");
190 glTexParameteri(target
, GL_GENERATE_MIPMAP_SGIS
, GL_TRUE
);
191 checkGLcall("glTexParameteri(target, GL_GENERATE_MIPMAP_SGIS, GL_TRUE)");
196 *set_surface_desc
= FALSE
;
201 glBindTexture(target
, gl_tex
->name
);
202 checkGLcall("glBindTexture");
205 /* For a new texture we have to set the texture levels after
206 * binding the texture. Beware that texture rectangles do not
207 * support mipmapping, but set the maxmiplevel if we're relying
208 * on the partial GL_ARB_texture_non_power_of_two emulation with
209 * texture rectangles. (I.e., do not care about cond_np2 here,
210 * just look for GL_TEXTURE_RECTANGLE_ARB.) */
211 if (target
!= GL_TEXTURE_RECTANGLE_ARB
)
213 TRACE("Setting GL_TEXTURE_MAX_LEVEL to %u.\n", texture
->level_count
- 1);
214 glTexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, texture
->level_count
- 1);
215 checkGLcall("glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, texture->level_count)");
217 if (target
== GL_TEXTURE_CUBE_MAP_ARB
)
219 /* Cubemaps are always set to clamp, regardless of the sampler state. */
220 glTexParameteri(target
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
221 glTexParameteri(target
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
222 glTexParameteri(target
, GL_TEXTURE_WRAP_R
, GL_CLAMP_TO_EDGE
);
228 ERR("This texture doesn't have an OpenGL texture assigned to it.\n");
229 hr
= WINED3DERR_INVALIDCALL
;
236 /* GL locking is done by the caller */
237 static void apply_wrap(const struct wined3d_gl_info
*gl_info
, GLenum target
,
238 WINED3DTEXTUREADDRESS d3d_wrap
, GLenum param
, BOOL cond_np2
)
242 if (d3d_wrap
< WINED3DTADDRESS_WRAP
|| d3d_wrap
> WINED3DTADDRESS_MIRRORONCE
)
244 FIXME("Unrecognized or unsupported WINED3DTEXTUREADDRESS %#x.\n", d3d_wrap
);
248 /* Cubemaps are always set to clamp, regardless of the sampler state. */
249 if (target
== GL_TEXTURE_CUBE_MAP_ARB
250 || (cond_np2
&& d3d_wrap
== WINED3DTADDRESS_WRAP
))
251 gl_wrap
= GL_CLAMP_TO_EDGE
;
253 gl_wrap
= gl_info
->wrap_lookup
[d3d_wrap
- WINED3DTADDRESS_WRAP
];
255 TRACE("Setting param %#x to %#x for target %#x.\n", param
, gl_wrap
, target
);
256 glTexParameteri(target
, param
, gl_wrap
);
257 checkGLcall("glTexParameteri(target, param, gl_wrap)");
260 /* GL locking is done by the caller (state handler) */
261 void wined3d_texture_apply_state_changes(struct wined3d_texture
*texture
,
262 const DWORD sampler_states
[WINED3D_HIGHEST_SAMPLER_STATE
+ 1],
263 const struct wined3d_gl_info
*gl_info
)
265 BOOL cond_np2
= texture
->cond_np2
;
266 GLenum target
= texture
->target
;
267 struct gl_texture
*gl_tex
;
271 TRACE("texture %p, sampler_states %p.\n", texture
, sampler_states
);
273 gl_tex
= wined3d_texture_get_gl_texture(texture
, gl_info
, texture
->is_srgb
);
275 /* This function relies on the correct texture being bound and loaded. */
277 if (sampler_states
[WINED3DSAMP_ADDRESSU
] != gl_tex
->states
[WINED3DTEXSTA_ADDRESSU
])
279 state
= sampler_states
[WINED3DSAMP_ADDRESSU
];
280 apply_wrap(gl_info
, target
, state
, GL_TEXTURE_WRAP_S
, cond_np2
);
281 gl_tex
->states
[WINED3DTEXSTA_ADDRESSU
] = state
;
284 if (sampler_states
[WINED3DSAMP_ADDRESSV
] != gl_tex
->states
[WINED3DTEXSTA_ADDRESSV
])
286 state
= sampler_states
[WINED3DSAMP_ADDRESSV
];
287 apply_wrap(gl_info
, target
, state
, GL_TEXTURE_WRAP_T
, cond_np2
);
288 gl_tex
->states
[WINED3DTEXSTA_ADDRESSV
] = state
;
291 if (sampler_states
[WINED3DSAMP_ADDRESSW
] != gl_tex
->states
[WINED3DTEXSTA_ADDRESSW
])
293 state
= sampler_states
[WINED3DSAMP_ADDRESSW
];
294 apply_wrap(gl_info
, target
, state
, GL_TEXTURE_WRAP_R
, cond_np2
);
295 gl_tex
->states
[WINED3DTEXSTA_ADDRESSW
] = state
;
298 if (sampler_states
[WINED3DSAMP_BORDERCOLOR
] != gl_tex
->states
[WINED3DTEXSTA_BORDERCOLOR
])
302 state
= sampler_states
[WINED3DSAMP_BORDERCOLOR
];
303 D3DCOLORTOGLFLOAT4(state
, col
);
304 TRACE("Setting border color for %#x to %#x.\n", target
, state
);
305 glTexParameterfv(target
, GL_TEXTURE_BORDER_COLOR
, &col
[0]);
306 checkGLcall("glTexParameterfv(..., GL_TEXTURE_BORDER_COLOR, ...)");
307 gl_tex
->states
[WINED3DTEXSTA_BORDERCOLOR
] = state
;
310 if (sampler_states
[WINED3DSAMP_MAGFILTER
] != gl_tex
->states
[WINED3DTEXSTA_MAGFILTER
])
314 state
= sampler_states
[WINED3DSAMP_MAGFILTER
];
315 if (state
> WINED3DTEXF_ANISOTROPIC
)
316 FIXME("Unrecognized or unsupported MAGFILTER* value %d.\n", state
);
318 gl_value
= wined3d_gl_mag_filter(texture
->mag_lookup
,
319 min(max(state
, WINED3DTEXF_POINT
), WINED3DTEXF_LINEAR
));
320 TRACE("ValueMAG=%#x setting MAGFILTER to %#x.\n", state
, gl_value
);
321 glTexParameteri(target
, GL_TEXTURE_MAG_FILTER
, gl_value
);
323 gl_tex
->states
[WINED3DTEXSTA_MAGFILTER
] = state
;
326 if ((sampler_states
[WINED3DSAMP_MINFILTER
] != gl_tex
->states
[WINED3DTEXSTA_MINFILTER
]
327 || sampler_states
[WINED3DSAMP_MIPFILTER
] != gl_tex
->states
[WINED3DTEXSTA_MIPFILTER
]
328 || sampler_states
[WINED3DSAMP_MAXMIPLEVEL
] != gl_tex
->states
[WINED3DTEXSTA_MAXMIPLEVEL
]))
332 gl_tex
->states
[WINED3DTEXSTA_MIPFILTER
] = sampler_states
[WINED3DSAMP_MIPFILTER
];
333 gl_tex
->states
[WINED3DTEXSTA_MINFILTER
] = sampler_states
[WINED3DSAMP_MINFILTER
];
334 gl_tex
->states
[WINED3DTEXSTA_MAXMIPLEVEL
] = sampler_states
[WINED3DSAMP_MAXMIPLEVEL
];
336 if (gl_tex
->states
[WINED3DTEXSTA_MINFILTER
] > WINED3DTEXF_ANISOTROPIC
337 || gl_tex
->states
[WINED3DTEXSTA_MIPFILTER
] > WINED3DTEXF_ANISOTROPIC
)
339 FIXME("Unrecognized or unsupported D3DSAMP_MINFILTER value %#x D3DSAMP_MIPFILTER value %#x.\n",
340 gl_tex
->states
[WINED3DTEXSTA_MINFILTER
],
341 gl_tex
->states
[WINED3DTEXSTA_MIPFILTER
]);
343 gl_value
= wined3d_gl_min_mip_filter(texture
->min_mip_lookup
,
344 min(max(sampler_states
[WINED3DSAMP_MINFILTER
], WINED3DTEXF_POINT
), WINED3DTEXF_LINEAR
),
345 min(max(sampler_states
[WINED3DSAMP_MIPFILTER
], WINED3DTEXF_NONE
), WINED3DTEXF_LINEAR
));
347 TRACE("ValueMIN=%#x, ValueMIP=%#x, setting MINFILTER to %#x.\n",
348 sampler_states
[WINED3DSAMP_MINFILTER
],
349 sampler_states
[WINED3DSAMP_MIPFILTER
], gl_value
);
350 glTexParameteri(target
, GL_TEXTURE_MIN_FILTER
, gl_value
);
351 checkGLcall("glTexParameter GL_TEXTURE_MIN_FILTER, ...");
355 if (gl_tex
->states
[WINED3DTEXSTA_MIPFILTER
] == WINED3DTEXF_NONE
)
356 gl_value
= texture
->lod
;
357 else if (gl_tex
->states
[WINED3DTEXSTA_MAXMIPLEVEL
] >= texture
->level_count
)
358 gl_value
= texture
->level_count
- 1;
359 else if (gl_tex
->states
[WINED3DTEXSTA_MAXMIPLEVEL
] < texture
->lod
)
360 /* texture->lod is already clamped in the setter. */
361 gl_value
= texture
->lod
;
363 gl_value
= gl_tex
->states
[WINED3DTEXSTA_MAXMIPLEVEL
];
365 /* Note that WINED3DSAMP_MAXMIPLEVEL specifies the largest mipmap
366 * (default 0), while GL_TEXTURE_MAX_LEVEL specifies the smallest
367 * mimap used (default 1000). So WINED3DSAMP_MAXMIPLEVEL
368 * corresponds to GL_TEXTURE_BASE_LEVEL. */
369 glTexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, gl_value
);
373 if ((gl_tex
->states
[WINED3DTEXSTA_MAGFILTER
] != WINED3DTEXF_ANISOTROPIC
374 && gl_tex
->states
[WINED3DTEXSTA_MINFILTER
] != WINED3DTEXF_ANISOTROPIC
375 && gl_tex
->states
[WINED3DTEXSTA_MIPFILTER
] != WINED3DTEXF_ANISOTROPIC
)
379 aniso
= sampler_states
[WINED3DSAMP_MAXANISOTROPY
];
381 if (gl_tex
->states
[WINED3DTEXSTA_MAXANISOTROPY
] != aniso
)
383 if (gl_info
->supported
[EXT_TEXTURE_FILTER_ANISOTROPIC
])
385 glTexParameteri(target
, GL_TEXTURE_MAX_ANISOTROPY_EXT
, aniso
);
386 checkGLcall("glTexParameteri(GL_TEXTURE_MAX_ANISOTROPY_EXT, aniso)");
390 WARN("Anisotropic filtering not supported.\n");
392 gl_tex
->states
[WINED3DTEXSTA_MAXANISOTROPY
] = aniso
;
395 /* These should always be the same unless EXT_texture_sRGB_decode is supported. */
396 if (sampler_states
[WINED3DSAMP_SRGBTEXTURE
] != gl_tex
->states
[WINED3DTEXSTA_SRGBTEXTURE
])
398 glTexParameteri(target
, GL_TEXTURE_SRGB_DECODE_EXT
,
399 sampler_states
[WINED3DSAMP_SRGBTEXTURE
] ? GL_DECODE_EXT
: GL_SKIP_DECODE_EXT
);
400 checkGLcall("glTexParameteri(GL_TEXTURE_SRGB_DECODE_EXT)");
403 if (!(texture
->resource
.format
->flags
& WINED3DFMT_FLAG_SHADOW
)
404 != !gl_tex
->states
[WINED3DTEXSTA_SHADOW
])
406 if (texture
->resource
.format
->flags
& WINED3DFMT_FLAG_SHADOW
)
408 glTexParameteri(target
, GL_DEPTH_TEXTURE_MODE_ARB
, GL_LUMINANCE
);
409 glTexParameteri(target
, GL_TEXTURE_COMPARE_MODE_ARB
, GL_COMPARE_R_TO_TEXTURE_ARB
);
410 checkGLcall("glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE_ARB)");
411 gl_tex
->states
[WINED3DTEXSTA_SHADOW
] = TRUE
;
415 glTexParameteri(target
, GL_TEXTURE_COMPARE_MODE_ARB
, GL_NONE
);
416 checkGLcall("glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_NONE)");
417 gl_tex
->states
[WINED3DTEXSTA_SHADOW
] = FALSE
;
422 ULONG CDECL
wined3d_texture_incref(struct wined3d_texture
*texture
)
424 ULONG refcount
= InterlockedIncrement(&texture
->resource
.ref
);
426 TRACE("%p increasing refcount to %u.\n", texture
, refcount
);
431 /* Do not call while under the GL lock. */
432 ULONG CDECL
wined3d_texture_decref(struct wined3d_texture
*texture
)
434 ULONG refcount
= InterlockedDecrement(&texture
->resource
.ref
);
436 TRACE("%p decreasing refcount to %u.\n", texture
, refcount
);
440 wined3d_texture_cleanup(texture
);
441 texture
->resource
.parent_ops
->wined3d_object_destroyed(texture
->resource
.parent
);
442 HeapFree(GetProcessHeap(), 0, texture
);
448 HRESULT CDECL
wined3d_texture_set_private_data(struct wined3d_texture
*texture
,
449 REFGUID guid
, const void *data
, DWORD data_size
, DWORD flags
)
451 return resource_set_private_data(&texture
->resource
, guid
, data
, data_size
, flags
);
454 HRESULT CDECL
wined3d_texture_get_private_data(const struct wined3d_texture
*texture
,
455 REFGUID guid
, void *data
, DWORD
*data_size
)
457 return resource_get_private_data(&texture
->resource
, guid
, data
, data_size
);
460 HRESULT CDECL
wined3d_texture_free_private_data(struct wined3d_texture
*texture
, REFGUID guid
)
462 return resource_free_private_data(&texture
->resource
, guid
);
465 DWORD CDECL
wined3d_texture_set_priority(struct wined3d_texture
*texture
, DWORD priority
)
467 return resource_set_priority(&texture
->resource
, priority
);
470 DWORD CDECL
wined3d_texture_get_priority(const struct wined3d_texture
*texture
)
472 return resource_get_priority(&texture
->resource
);
475 /* Do not call while under the GL lock. */
476 void CDECL
wined3d_texture_preload(struct wined3d_texture
*texture
)
478 texture
->texture_ops
->texture_preload(texture
, SRGB_ANY
);
481 WINED3DRESOURCETYPE CDECL
wined3d_texture_get_type(const struct wined3d_texture
*texture
)
483 TRACE("texture %p.\n", texture
);
485 return texture
->resource
.resourceType
;
488 void * CDECL
wined3d_texture_get_parent(const struct wined3d_texture
*texture
)
490 TRACE("texture %p.\n", texture
);
492 return texture
->resource
.parent
;
495 DWORD CDECL
wined3d_texture_set_lod(struct wined3d_texture
*texture
, DWORD lod
)
497 DWORD old
= texture
->lod
;
499 TRACE("texture %p, lod %u.\n", texture
, lod
);
501 /* The d3d9:texture test shows that SetLOD is ignored on non-managed
502 * textures. The call always returns 0, and GetLOD always returns 0. */
503 if (texture
->resource
.pool
!= WINED3DPOOL_MANAGED
)
505 TRACE("Ignoring SetLOD on %s texture, returning 0.\n", debug_d3dpool(texture
->resource
.pool
));
509 if (lod
>= texture
->level_count
)
510 lod
= texture
->level_count
- 1;
512 if (texture
->lod
!= lod
)
516 texture
->texture_rgb
.states
[WINED3DTEXSTA_MAXMIPLEVEL
] = ~0U;
517 texture
->texture_srgb
.states
[WINED3DTEXSTA_MAXMIPLEVEL
] = ~0U;
518 if (texture
->bind_count
)
519 IWineD3DDeviceImpl_MarkStateDirty(texture
->resource
.device
, STATE_SAMPLER(texture
->sampler
));
525 DWORD CDECL
wined3d_texture_get_lod(const struct wined3d_texture
*texture
)
527 TRACE("texture %p, returning %u.\n", texture
, texture
->lod
);
532 DWORD CDECL
wined3d_texture_get_level_count(const struct wined3d_texture
*texture
)
534 TRACE("texture %p, returning %u.\n", texture
, texture
->level_count
);
536 return texture
->level_count
;
539 HRESULT CDECL
wined3d_texture_set_autogen_filter_type(struct wined3d_texture
*texture
,
540 WINED3DTEXTUREFILTERTYPE filter_type
)
542 TRACE("texture %p, filter_type %s.\n", texture
, debug_d3dtexturefiltertype(filter_type
));
544 if (!(texture
->resource
.usage
& WINED3DUSAGE_AUTOGENMIPMAP
))
546 WARN("Texture doesn't have AUTOGENMIPMAP usage.\n");
547 return WINED3DERR_INVALIDCALL
;
550 if (texture
->filter_type
!= filter_type
)
552 GLenum target
= texture
->target
;
553 struct wined3d_context
*context
;
555 context
= context_acquire(texture
->resource
.device
, NULL
);
558 glBindTexture(target
, texture
->texture_rgb
.name
);
559 checkGLcall("glBindTexture");
562 case WINED3DTEXF_NONE
:
563 case WINED3DTEXF_POINT
:
564 glTexParameteri(target
, GL_GENERATE_MIPMAP_HINT_SGIS
, GL_FASTEST
);
565 checkGLcall("glTexParameteri(target, GL_GENERATE_MIPMAP_HINT_SGIS, GL_FASTEST)");
568 case WINED3DTEXF_LINEAR
:
569 glTexParameteri(target
, GL_GENERATE_MIPMAP_HINT_SGIS
, GL_NICEST
);
570 checkGLcall("glTexParameteri(target, GL_GENERATE_MIPMAP_HINT_SGIS, GL_NICEST)");
574 WARN("Unexpected filter type %#x, setting to GL_NICEST.\n", filter_type
);
575 glTexParameteri(target
, GL_GENERATE_MIPMAP_HINT_SGIS
, GL_NICEST
);
576 checkGLcall("glTexParameteri(target, GL_GENERATE_MIPMAP_HINT_SGIS, GL_NICEST)");
581 context_release(context
);
583 texture
->filter_type
= filter_type
;
588 WINED3DTEXTUREFILTERTYPE CDECL
wined3d_texture_get_autogen_filter_type(const struct wined3d_texture
*texture
)
590 TRACE("texture %p.\n", texture
);
592 return texture
->filter_type
;
595 void CDECL
wined3d_texture_generate_mipmaps(struct wined3d_texture
*texture
)
597 /* TODO: Implement filters using GL_SGI_generate_mipmaps. */
598 FIXME("texture %p stub!\n", texture
);
601 struct wined3d_resource
* CDECL
wined3d_texture_get_sub_resource(struct wined3d_texture
*texture
,
602 UINT sub_resource_idx
)
604 UINT sub_count
= texture
->level_count
* texture
->layer_count
;
606 TRACE("texture %p, sub_resource_idx %u.\n", texture
, sub_resource_idx
);
608 if (sub_resource_idx
>= sub_count
)
610 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx
, sub_count
);
614 return texture
->sub_resources
[sub_resource_idx
];
617 HRESULT CDECL
wined3d_texture_add_dirty_region(struct wined3d_texture
*texture
,
618 UINT layer
, const WINED3DBOX
*dirty_region
)
620 struct wined3d_resource
*sub_resource
;
622 TRACE("texture %p, layer %u, dirty_region %p.\n", texture
, layer
, dirty_region
);
624 if (!(sub_resource
= wined3d_texture_get_sub_resource(texture
, layer
* texture
->level_count
)))
626 WARN("Failed to get sub-resource.\n");
627 return WINED3DERR_INVALIDCALL
;
630 wined3d_texture_set_dirty(texture
, TRUE
);
631 texture
->texture_ops
->texture_sub_resource_add_dirty_region(sub_resource
, dirty_region
);
636 /* Context activation is done by the caller. */
637 static HRESULT
texture2d_bind(struct wined3d_texture
*texture
,
638 const struct wined3d_gl_info
*gl_info
, BOOL srgb
)
640 BOOL set_gl_texture_desc
;
643 TRACE("texture %p, gl_info %p, srgb %#x.\n", texture
, gl_info
, srgb
);
645 hr
= wined3d_texture_bind(texture
, gl_info
, srgb
, &set_gl_texture_desc
);
646 if (set_gl_texture_desc
&& SUCCEEDED(hr
))
648 UINT sub_count
= texture
->level_count
* texture
->layer_count
;
649 BOOL srgb_tex
= !gl_info
->supported
[EXT_TEXTURE_SRGB_DECODE
] && texture
->is_srgb
;
650 struct gl_texture
*gl_tex
;
653 gl_tex
= wined3d_texture_get_gl_texture(texture
, gl_info
, srgb_tex
);
655 for (i
= 0; i
< sub_count
; ++i
)
657 IWineD3DSurfaceImpl
*surface
= surface_from_resource(texture
->sub_resources
[i
]);
658 surface_set_texture_name(surface
, gl_tex
->name
, srgb_tex
);
661 /* Conditinal non power of two textures use a different clamping
662 * default. If we're using the GL_WINE_normalized_texrect partial
663 * driver emulation, we're dealing with a GL_TEXTURE_2D texture which
664 * has the address mode set to repeat - something that prevents us
665 * from hitting the accelerated codepath. Thus manually set the GL
666 * state. The same applies to filtering. Even if the texture has only
667 * one mip level, the default LINEAR_MIPMAP_LINEAR filter causes a SW
668 * fallback on macos. */
669 if (texture
->cond_np2
)
671 GLenum target
= texture
->target
;
674 glTexParameteri(target
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
675 checkGLcall("glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)");
676 glTexParameteri(target
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
677 checkGLcall("glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)");
678 glTexParameteri(target
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
679 checkGLcall("glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST)");
680 glTexParameteri(target
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
681 checkGLcall("glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST)");
683 gl_tex
->states
[WINED3DTEXSTA_ADDRESSU
] = WINED3DTADDRESS_CLAMP
;
684 gl_tex
->states
[WINED3DTEXSTA_ADDRESSV
] = WINED3DTADDRESS_CLAMP
;
685 gl_tex
->states
[WINED3DTEXSTA_MAGFILTER
] = WINED3DTEXF_POINT
;
686 gl_tex
->states
[WINED3DTEXSTA_MINFILTER
] = WINED3DTEXF_POINT
;
687 gl_tex
->states
[WINED3DTEXSTA_MIPFILTER
] = WINED3DTEXF_NONE
;
694 /* Do not call while under the GL lock. */
695 static void texture2d_preload(struct wined3d_texture
*texture
, enum WINED3DSRGB srgb
)
697 UINT sub_count
= texture
->level_count
* texture
->layer_count
;
698 IWineD3DDeviceImpl
*device
= texture
->resource
.device
;
699 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
700 struct wined3d_context
*context
= NULL
;
701 struct gl_texture
*gl_tex
;
705 TRACE("texture %p, srgb %#x.\n", texture
, srgb
);
714 texture2d_preload(texture
, SRGB_RGB
);
721 srgb_mode
= texture
->is_srgb
;
725 gl_tex
= wined3d_texture_get_gl_texture(texture
, gl_info
, srgb_mode
);
727 if (!device
->isInDraw
)
729 /* No danger of recursive calls, context_acquire() sets isInDraw to TRUE
730 * when loading offscreen render targets into the texture. */
731 context
= context_acquire(device
, NULL
);
734 if (texture
->resource
.format
->id
== WINED3DFMT_P8_UINT
735 || texture
->resource
.format
->id
== WINED3DFMT_P8_UINT_A8_UNORM
)
737 for (i
= 0; i
< sub_count
; ++i
)
739 IWineD3DSurfaceImpl
*surface
= surface_from_resource(texture
->sub_resources
[i
]);
741 if (palette9_changed(surface
))
743 TRACE("Reloading surface %p because the d3d8/9 palette was changed.\n", surface
);
744 /* TODO: This is not necessarily needed with hw palettized texture support. */
745 surface_load_location(surface
, SFLAG_INSYSMEM
, NULL
);
746 /* Make sure the texture is reloaded because of the palette
747 * change, this kills performance though :( */
748 surface_modify_location(surface
, SFLAG_INTEXTURE
, FALSE
);
755 /* Reload the surfaces if the texture is marked dirty. */
756 for (i
= 0; i
< sub_count
; ++i
)
758 surface_load(surface_from_resource(texture
->sub_resources
[i
]), srgb_mode
);
763 TRACE("Texture %p not dirty, nothing to do.\n", texture
);
766 /* No longer dirty. */
767 gl_tex
->dirty
= FALSE
;
769 if (context
) context_release(context
);
772 static void texture2d_sub_resource_add_dirty_region(struct wined3d_resource
*sub_resource
,
773 const WINED3DBOX
*dirty_region
)
775 surface_add_dirty_rect(surface_from_resource(sub_resource
), dirty_region
);
778 static void texture2d_sub_resource_cleanup(struct wined3d_resource
*sub_resource
)
780 IWineD3DSurfaceImpl
*surface
= surface_from_resource(sub_resource
);
782 /* Clean out the texture name we gave to the surface so that the
783 * surface doesn't try and release it. */
784 surface_set_texture_name(surface
, 0, TRUE
);
785 surface_set_texture_name(surface
, 0, FALSE
);
786 surface_set_texture_target(surface
, 0);
787 surface_set_container(surface
, WINED3D_CONTAINER_NONE
, NULL
);
788 IWineD3DSurface_Release((IWineD3DSurface
*)surface
);
791 /* Do not call while under the GL lock. */
792 static void texture2d_unload(struct wined3d_resource
*resource
)
794 struct wined3d_texture
*texture
= wined3d_texture_from_resource(resource
);
795 UINT sub_count
= texture
->level_count
* texture
->layer_count
;
798 TRACE("texture %p.\n", texture
);
800 for (i
= 0; i
< sub_count
; ++i
)
802 struct wined3d_resource
*sub_resource
= texture
->sub_resources
[i
];
803 IWineD3DSurfaceImpl
*surface
= surface_from_resource(sub_resource
);
805 sub_resource
->resource_ops
->resource_unload(sub_resource
);
806 surface_set_texture_name(surface
, 0, FALSE
); /* Delete RGB name */
807 surface_set_texture_name(surface
, 0, TRUE
); /* Delete sRGB name */
810 wined3d_texture_unload(texture
);
813 static const struct wined3d_texture_ops texture2d_ops
=
817 texture2d_sub_resource_add_dirty_region
,
818 texture2d_sub_resource_cleanup
,
821 static const struct wined3d_resource_ops texture2d_resource_ops
=
826 HRESULT
cubetexture_init(struct wined3d_texture
*texture
, UINT edge_length
, UINT levels
,
827 IWineD3DDeviceImpl
*device
, DWORD usage
, enum wined3d_format_id format_id
, WINED3DPOOL pool
,
828 void *parent
, const struct wined3d_parent_ops
*parent_ops
)
830 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
831 const struct wined3d_format
*format
= wined3d_get_format(gl_info
, format_id
);
832 UINT pow2_edge_length
;
837 /* TODO: It should only be possible to create textures for formats
838 * that are reported as supported. */
839 if (WINED3DFMT_UNKNOWN
>= format_id
)
841 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture
);
842 return WINED3DERR_INVALIDCALL
;
845 if (!gl_info
->supported
[ARB_TEXTURE_CUBE_MAP
] && pool
!= WINED3DPOOL_SCRATCH
)
847 WARN("(%p) : Tried to create not supported cube texture.\n", texture
);
848 return WINED3DERR_INVALIDCALL
;
851 /* Calculate levels for mip mapping */
852 if (usage
& WINED3DUSAGE_AUTOGENMIPMAP
)
854 if (!gl_info
->supported
[SGIS_GENERATE_MIPMAP
])
856 WARN("No mipmap generation support, returning D3DERR_INVALIDCALL.\n");
857 return WINED3DERR_INVALIDCALL
;
862 WARN("D3DUSAGE_AUTOGENMIPMAP is set, and level count > 1, returning D3DERR_INVALIDCALL.\n");
863 return WINED3DERR_INVALIDCALL
;
870 levels
= wined3d_log2i(edge_length
) + 1;
871 TRACE("Calculated levels = %u.\n", levels
);
874 hr
= wined3d_texture_init(texture
, &texture2d_ops
, 6, levels
,
875 WINED3DRTYPE_CUBETEXTURE
, device
, usage
, format
, pool
,
876 parent
, parent_ops
, &texture2d_resource_ops
);
879 WARN("Failed to initialize texture, returning %#x\n", hr
);
883 /* Find the nearest pow2 match. */
884 pow2_edge_length
= 1;
885 while (pow2_edge_length
< edge_length
) pow2_edge_length
<<= 1;
887 if (gl_info
->supported
[ARB_TEXTURE_NON_POWER_OF_TWO
] || (edge_length
== pow2_edge_length
))
889 /* Precalculated scaling for 'faked' non power of two texture coords. */
890 texture
->pow2_matrix
[0] = 1.0f
;
891 texture
->pow2_matrix
[5] = 1.0f
;
892 texture
->pow2_matrix
[10] = 1.0f
;
893 texture
->pow2_matrix
[15] = 1.0f
;
897 /* Precalculated scaling for 'faked' non power of two texture coords. */
898 texture
->pow2_matrix
[0] = ((float)edge_length
) / ((float)pow2_edge_length
);
899 texture
->pow2_matrix
[5] = ((float)edge_length
) / ((float)pow2_edge_length
);
900 texture
->pow2_matrix
[10] = ((float)edge_length
) / ((float)pow2_edge_length
);
901 texture
->pow2_matrix
[15] = 1.0f
;
902 texture
->pow2_matrix_identity
= FALSE
;
904 texture
->target
= GL_TEXTURE_CUBE_MAP_ARB
;
906 /* Generate all the surfaces. */
908 for (i
= 0; i
< texture
->level_count
; ++i
)
910 /* Create the 6 faces. */
911 for (j
= 0; j
< 6; ++j
)
913 static const GLenum cube_targets
[6] =
915 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
,
916 GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
,
917 GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
,
918 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
,
919 GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
,
920 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
,
922 UINT idx
= j
* texture
->level_count
+ i
;
923 IWineD3DSurface
*surface
;
925 hr
= IWineD3DDeviceParent_CreateSurface(device
->device_parent
, parent
, tmp_w
, tmp_w
,
926 format_id
, usage
, pool
, i
/* Level */, j
, &surface
);
929 FIXME("(%p) Failed to create surface, hr %#x.\n", texture
, hr
);
930 wined3d_texture_cleanup(texture
);
934 surface_set_container((IWineD3DSurfaceImpl
*)surface
, WINED3D_CONTAINER_TEXTURE
, texture
);
935 surface_set_texture_target((IWineD3DSurfaceImpl
*)surface
, cube_targets
[j
]);
936 texture
->sub_resources
[idx
] = &((IWineD3DSurfaceImpl
*)surface
)->resource
;
937 TRACE("Created surface level %u @ %p.\n", i
, surface
);
939 tmp_w
= max(1, tmp_w
>> 1);
945 HRESULT
texture_init(struct wined3d_texture
*texture
, UINT width
, UINT height
, UINT levels
,
946 IWineD3DDeviceImpl
*device
, DWORD usage
, enum wined3d_format_id format_id
, WINED3DPOOL pool
,
947 void *parent
, const struct wined3d_parent_ops
*parent_ops
)
949 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
950 const struct wined3d_format
*format
= wined3d_get_format(gl_info
, format_id
);
951 UINT pow2_width
, pow2_height
;
956 /* TODO: It should only be possible to create textures for formats
957 * that are reported as supported. */
958 if (WINED3DFMT_UNKNOWN
>= format_id
)
960 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture
);
961 return WINED3DERR_INVALIDCALL
;
964 /* Non-power2 support. */
965 if (gl_info
->supported
[ARB_TEXTURE_NON_POWER_OF_TWO
])
968 pow2_height
= height
;
972 /* Find the nearest pow2 match. */
973 pow2_width
= pow2_height
= 1;
974 while (pow2_width
< width
) pow2_width
<<= 1;
975 while (pow2_height
< height
) pow2_height
<<= 1;
977 if (pow2_width
!= width
|| pow2_height
!= height
)
981 WARN("Attempted to create a mipmapped np2 texture without unconditional np2 support.\n");
982 return WINED3DERR_INVALIDCALL
;
988 /* Calculate levels for mip mapping. */
989 if (usage
& WINED3DUSAGE_AUTOGENMIPMAP
)
991 if (!gl_info
->supported
[SGIS_GENERATE_MIPMAP
])
993 WARN("No mipmap generation support, returning WINED3DERR_INVALIDCALL.\n");
994 return WINED3DERR_INVALIDCALL
;
999 WARN("D3DUSAGE_AUTOGENMIPMAP is set, and level count > 1, returning WINED3DERR_INVALIDCALL.\n");
1000 return WINED3DERR_INVALIDCALL
;
1007 levels
= wined3d_log2i(max(width
, height
)) + 1;
1008 TRACE("Calculated levels = %u.\n", levels
);
1011 hr
= wined3d_texture_init(texture
, &texture2d_ops
, 1, levels
,
1012 WINED3DRTYPE_TEXTURE
, device
, usage
, format
, pool
,
1013 parent
, parent_ops
, &texture2d_resource_ops
);
1016 WARN("Failed to initialize texture, returning %#x.\n", hr
);
1020 /* Precalculated scaling for 'faked' non power of two texture coords.
1021 * Second also don't use ARB_TEXTURE_RECTANGLE in case the surface format is P8 and EXT_PALETTED_TEXTURE
1022 * is used in combination with texture uploads (RTL_READTEX). The reason is that EXT_PALETTED_TEXTURE
1023 * doesn't work in combination with ARB_TEXTURE_RECTANGLE. */
1024 if (gl_info
->supported
[WINED3D_GL_NORMALIZED_TEXRECT
] && (width
!= pow2_width
|| height
!= pow2_height
))
1026 texture
->pow2_matrix
[0] = 1.0f
;
1027 texture
->pow2_matrix
[5] = 1.0f
;
1028 texture
->pow2_matrix
[10] = 1.0f
;
1029 texture
->pow2_matrix
[15] = 1.0f
;
1030 texture
->target
= GL_TEXTURE_2D
;
1031 texture
->cond_np2
= TRUE
;
1032 texture
->min_mip_lookup
= minMipLookup_noFilter
;
1034 else if (gl_info
->supported
[ARB_TEXTURE_RECTANGLE
] && (width
!= pow2_width
|| height
!= pow2_height
)
1035 && !(format
->id
== WINED3DFMT_P8_UINT
&& gl_info
->supported
[EXT_PALETTED_TEXTURE
]
1036 && wined3d_settings
.rendertargetlock_mode
== RTL_READTEX
))
1038 if (width
!= 1 || height
!= 1)
1039 texture
->pow2_matrix_identity
= FALSE
;
1041 texture
->pow2_matrix
[0] = (float)width
;
1042 texture
->pow2_matrix
[5] = (float)height
;
1043 texture
->pow2_matrix
[10] = 1.0f
;
1044 texture
->pow2_matrix
[15] = 1.0f
;
1045 texture
->target
= GL_TEXTURE_RECTANGLE_ARB
;
1046 texture
->cond_np2
= TRUE
;
1048 if (texture
->resource
.format
->flags
& WINED3DFMT_FLAG_FILTERING
)
1049 texture
->min_mip_lookup
= minMipLookup_noMip
;
1051 texture
->min_mip_lookup
= minMipLookup_noFilter
;
1055 if ((width
!= pow2_width
) || (height
!= pow2_height
))
1057 texture
->pow2_matrix_identity
= FALSE
;
1058 texture
->pow2_matrix
[0] = (((float)width
) / ((float)pow2_width
));
1059 texture
->pow2_matrix
[5] = (((float)height
) / ((float)pow2_height
));
1063 texture
->pow2_matrix
[0] = 1.0f
;
1064 texture
->pow2_matrix
[5] = 1.0f
;
1067 texture
->pow2_matrix
[10] = 1.0f
;
1068 texture
->pow2_matrix
[15] = 1.0f
;
1069 texture
->target
= GL_TEXTURE_2D
;
1070 texture
->cond_np2
= FALSE
;
1072 TRACE("xf(%f) yf(%f)\n", texture
->pow2_matrix
[0], texture
->pow2_matrix
[5]);
1074 /* Generate all the surfaces. */
1077 for (i
= 0; i
< texture
->level_count
; ++i
)
1079 IWineD3DSurface
*surface
;
1081 /* Use the callback to create the texture surface. */
1082 hr
= IWineD3DDeviceParent_CreateSurface(device
->device_parent
, parent
, tmp_w
, tmp_h
,
1083 format
->id
, usage
, pool
, i
, 0, &surface
);
1086 FIXME("Failed to create surface %p, hr %#x\n", texture
, hr
);
1087 wined3d_texture_cleanup(texture
);
1091 surface_set_container((IWineD3DSurfaceImpl
*)surface
, WINED3D_CONTAINER_TEXTURE
, texture
);
1092 surface_set_texture_target((IWineD3DSurfaceImpl
*)surface
, texture
->target
);
1093 texture
->sub_resources
[i
] = &((IWineD3DSurfaceImpl
*)surface
)->resource
;
1094 TRACE("Created surface level %u @ %p.\n", i
, surface
);
1095 /* Calculate the next mipmap level. */
1096 tmp_w
= max(1, tmp_w
>> 1);
1097 tmp_h
= max(1, tmp_h
>> 1);
1103 /* Context activation is done by the caller. */
1104 static HRESULT
texture3d_bind(struct wined3d_texture
*texture
,
1105 const struct wined3d_gl_info
*gl_info
, BOOL srgb
)
1109 TRACE("texture %p, gl_info %p, srgb %#x.\n", texture
, gl_info
, srgb
);
1111 return wined3d_texture_bind(texture
, gl_info
, srgb
, &dummy
);
1114 /* Do not call while under the GL lock. */
1115 static void texture3d_preload(struct wined3d_texture
*texture
, enum WINED3DSRGB srgb
)
1117 IWineD3DDeviceImpl
*device
= texture
->resource
.device
;
1118 struct wined3d_context
*context
= NULL
;
1119 BOOL srgb_mode
= texture
->is_srgb
;
1120 BOOL srgb_was_toggled
= FALSE
;
1123 TRACE("texture %p, srgb %#x.\n", texture
, srgb
);
1125 if (!device
->isInDraw
)
1126 context
= context_acquire(device
, NULL
);
1127 else if (texture
->bind_count
> 0)
1129 srgb_mode
= device
->stateBlock
->state
.sampler_states
[texture
->sampler
][WINED3DSAMP_SRGBTEXTURE
];
1130 srgb_was_toggled
= texture
->is_srgb
!= srgb_mode
;
1131 texture
->is_srgb
= srgb_mode
;
1134 /* If the texture is marked dirty or the sRGB sampler setting has changed
1135 * since the last load then reload the volumes. */
1136 if (texture
->texture_rgb
.dirty
)
1138 for (i
= 0; i
< texture
->level_count
; ++i
)
1140 volume_load(volume_from_resource(texture
->sub_resources
[i
]), i
, srgb_mode
);
1143 else if (srgb_was_toggled
)
1145 for (i
= 0; i
< texture
->level_count
; ++i
)
1147 IWineD3DVolumeImpl
*volume
= volume_from_resource(texture
->sub_resources
[i
]);
1148 volume_add_dirty_box(volume
, NULL
);
1149 volume_load(volume
, i
, srgb_mode
);
1154 TRACE("Texture %p not dirty, nothing to do.\n", texture
);
1158 context_release(context
);
1160 /* No longer dirty */
1161 texture
->texture_rgb
.dirty
= FALSE
;
1164 static void texture3d_sub_resource_add_dirty_region(struct wined3d_resource
*sub_resource
,
1165 const WINED3DBOX
*dirty_region
)
1167 volume_add_dirty_box(volume_from_resource(sub_resource
), dirty_region
);
1170 static void texture3d_sub_resource_cleanup(struct wined3d_resource
*sub_resource
)
1172 IWineD3DVolumeImpl
*volume
= volume_from_resource(sub_resource
);
1174 /* Cleanup the container. */
1175 volume_set_container(volume
, NULL
);
1176 IWineD3DVolume_Release((IWineD3DVolume
*)volume
);
1179 /* Do not call while under the GL lock. */
1180 static void texture3d_unload(struct wined3d_resource
*resource
)
1182 struct wined3d_texture
*texture
= wined3d_texture_from_resource(resource
);
1185 TRACE("texture %p.\n", texture
);
1187 for (i
= 0; i
< texture
->level_count
; ++i
)
1189 struct wined3d_resource
*sub_resource
= texture
->sub_resources
[i
];
1190 sub_resource
->resource_ops
->resource_unload(sub_resource
);
1193 wined3d_texture_unload(texture
);
1196 static const struct wined3d_texture_ops texture3d_ops
=
1200 texture3d_sub_resource_add_dirty_region
,
1201 texture3d_sub_resource_cleanup
,
1204 static const struct wined3d_resource_ops texture3d_resource_ops
=
1209 HRESULT
volumetexture_init(struct wined3d_texture
*texture
, UINT width
, UINT height
,
1210 UINT depth
, UINT levels
, IWineD3DDeviceImpl
*device
, DWORD usage
, enum wined3d_format_id format_id
,
1211 WINED3DPOOL pool
, void *parent
, const struct wined3d_parent_ops
*parent_ops
)
1213 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
1214 const struct wined3d_format
*format
= wined3d_get_format(gl_info
, format_id
);
1215 UINT tmp_w
, tmp_h
, tmp_d
;
1219 /* TODO: It should only be possible to create textures for formats
1220 * that are reported as supported. */
1221 if (WINED3DFMT_UNKNOWN
>= format_id
)
1223 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture
);
1224 return WINED3DERR_INVALIDCALL
;
1227 if (!gl_info
->supported
[EXT_TEXTURE3D
])
1229 WARN("(%p) : Texture cannot be created - no volume texture support.\n", texture
);
1230 return WINED3DERR_INVALIDCALL
;
1233 /* Calculate levels for mip mapping. */
1234 if (usage
& WINED3DUSAGE_AUTOGENMIPMAP
)
1236 if (!gl_info
->supported
[SGIS_GENERATE_MIPMAP
])
1238 WARN("No mipmap generation support, returning D3DERR_INVALIDCALL.\n");
1239 return WINED3DERR_INVALIDCALL
;
1244 WARN("D3DUSAGE_AUTOGENMIPMAP is set, and level count > 1, returning D3DERR_INVALIDCALL.\n");
1245 return WINED3DERR_INVALIDCALL
;
1252 levels
= wined3d_log2i(max(max(width
, height
), depth
)) + 1;
1253 TRACE("Calculated levels = %u.\n", levels
);
1256 hr
= wined3d_texture_init(texture
, &texture3d_ops
, 1, levels
,
1257 WINED3DRTYPE_VOLUMETEXTURE
, device
, usage
, format
, pool
,
1258 parent
, parent_ops
, &texture3d_resource_ops
);
1261 WARN("Failed to initialize texture, returning %#x.\n", hr
);
1265 /* Is NP2 support for volumes needed? */
1266 texture
->pow2_matrix
[0] = 1.0f
;
1267 texture
->pow2_matrix
[5] = 1.0f
;
1268 texture
->pow2_matrix
[10] = 1.0f
;
1269 texture
->pow2_matrix
[15] = 1.0f
;
1270 texture
->target
= GL_TEXTURE_3D
;
1272 /* Generate all the surfaces. */
1277 for (i
= 0; i
< texture
->level_count
; ++i
)
1279 IWineD3DVolume
*volume
;
1281 /* Create the volume. */
1282 hr
= IWineD3DDeviceParent_CreateVolume(device
->device_parent
, parent
,
1283 tmp_w
, tmp_h
, tmp_d
, format_id
, pool
, usage
, &volume
);
1286 ERR("Creating a volume for the volume texture failed, hr %#x.\n", hr
);
1287 wined3d_texture_cleanup(texture
);
1291 /* Set its container to this texture. */
1292 volume_set_container((IWineD3DVolumeImpl
*)volume
, texture
);
1293 texture
->sub_resources
[i
] = &((IWineD3DVolumeImpl
*)volume
)->resource
;
1295 /* Calculate the next mipmap level. */
1296 tmp_w
= max(1, tmp_w
>> 1);
1297 tmp_h
= max(1, tmp_h
>> 1);
1298 tmp_d
= max(1, tmp_d
>> 1);