2 * Copyright 2002-2005 Jason Edmeades
3 * Copyright 2002-2005 Raphael Junqueira
4 * Copyright 2005 Oliver Stieber
5 * Copyright 2007-2009, 2013 Stefan Dösinger for CodeWeavers
6 * Copyright 2009-2011 Henri Verbeet for CodeWeavers
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #include "wine/port.h"
25 #include "wined3d_private.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(d3d_texture
);
29 static HRESULT
wined3d_texture_init(struct wined3d_texture
*texture
, const struct wined3d_texture_ops
*texture_ops
,
30 UINT layer_count
, UINT level_count
, const struct wined3d_resource_desc
*desc
, struct wined3d_device
*device
,
31 void *parent
, const struct wined3d_parent_ops
*parent_ops
, const struct wined3d_resource_ops
*resource_ops
)
33 const struct wined3d_format
*format
= wined3d_get_format(&device
->adapter
->gl_info
, desc
->format
);
36 TRACE("texture %p, texture_ops %p, layer_count %u, level_count %u, resource_type %s, format %s, "
37 "multisample_type %#x, multisample_quality %#x, usage %s, pool %s, width %u, height %u, depth %u, "
38 "device %p, parent %p, parent_ops %p, resource_ops %p.\n",
39 texture
, texture_ops
, layer_count
, level_count
, debug_d3dresourcetype(desc
->resource_type
),
40 debug_d3dformat(desc
->format
), desc
->multisample_type
, desc
->multisample_quality
,
41 debug_d3dusage(desc
->usage
), debug_d3dpool(desc
->pool
), desc
->width
, desc
->height
, desc
->depth
,
42 device
, parent
, parent_ops
, resource_ops
);
44 if (FAILED(hr
= resource_init(&texture
->resource
, device
, desc
->resource_type
, format
,
45 desc
->multisample_type
, desc
->multisample_quality
, desc
->usage
, desc
->pool
,
46 desc
->width
, desc
->height
, desc
->depth
, 0, parent
, parent_ops
, resource_ops
)))
48 WARN("Failed to initialize resource, returning %#x\n", hr
);
52 texture
->texture_ops
= texture_ops
;
53 texture
->sub_resources
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
54 level_count
* layer_count
* sizeof(*texture
->sub_resources
));
55 if (!texture
->sub_resources
)
57 ERR("Failed to allocate sub-resource array.\n");
58 resource_cleanup(&texture
->resource
);
62 texture
->layer_count
= layer_count
;
63 texture
->level_count
= level_count
;
64 texture
->filter_type
= (desc
->usage
& WINED3DUSAGE_AUTOGENMIPMAP
) ? WINED3D_TEXF_LINEAR
: WINED3D_TEXF_NONE
;
66 texture
->texture_rgb
.dirty
= TRUE
;
67 texture
->texture_srgb
.dirty
= TRUE
;
68 texture
->flags
= WINED3D_TEXTURE_POW2_MAT_IDENT
;
70 if (texture
->resource
.format
->flags
& WINED3DFMT_FLAG_FILTERING
)
72 texture
->min_mip_lookup
= minMipLookup
;
73 texture
->mag_lookup
= magLookup
;
77 texture
->min_mip_lookup
= minMipLookup_noFilter
;
78 texture
->mag_lookup
= magLookup_noFilter
;
84 /* A GL context is provided by the caller */
85 static void gltexture_delete(const struct wined3d_gl_info
*gl_info
, struct gl_texture
*tex
)
87 gl_info
->gl_ops
.gl
.p_glDeleteTextures(1, &tex
->name
);
91 static void wined3d_texture_unload(struct wined3d_texture
*texture
)
93 struct wined3d_device
*device
= texture
->resource
.device
;
94 struct wined3d_context
*context
= NULL
;
96 if (texture
->texture_rgb
.name
|| texture
->texture_srgb
.name
)
98 context
= context_acquire(device
, NULL
);
101 if (texture
->texture_rgb
.name
)
102 gltexture_delete(context
->gl_info
, &texture
->texture_rgb
);
104 if (texture
->texture_srgb
.name
)
105 gltexture_delete(context
->gl_info
, &texture
->texture_srgb
);
107 if (context
) context_release(context
);
109 wined3d_texture_set_dirty(texture
, TRUE
);
111 resource_unload(&texture
->resource
);
114 static void wined3d_texture_cleanup(struct wined3d_texture
*texture
)
116 UINT sub_count
= texture
->level_count
* texture
->layer_count
;
119 TRACE("texture %p.\n", texture
);
121 for (i
= 0; i
< sub_count
; ++i
)
123 struct wined3d_resource
*sub_resource
= texture
->sub_resources
[i
];
126 texture
->texture_ops
->texture_sub_resource_cleanup(sub_resource
);
129 wined3d_texture_unload(texture
);
130 HeapFree(GetProcessHeap(), 0, texture
->sub_resources
);
131 resource_cleanup(&texture
->resource
);
134 void wined3d_texture_set_dirty(struct wined3d_texture
*texture
, BOOL dirty
)
136 texture
->texture_rgb
.dirty
= dirty
;
137 texture
->texture_srgb
.dirty
= dirty
;
140 /* Context activation is done by the caller. */
141 static HRESULT
wined3d_texture_bind(struct wined3d_texture
*texture
,
142 struct wined3d_context
*context
, BOOL srgb
, BOOL
*set_surface_desc
)
144 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
145 struct gl_texture
*gl_tex
;
146 BOOL new_texture
= FALSE
;
147 HRESULT hr
= WINED3D_OK
;
150 TRACE("texture %p, context %p, srgb %#x, set_surface_desc %p.\n", texture
, context
, srgb
, set_surface_desc
);
152 /* sRGB mode cache for preload() calls outside drawprim. */
154 texture
->flags
|= WINED3D_TEXTURE_IS_SRGB
;
156 texture
->flags
&= ~WINED3D_TEXTURE_IS_SRGB
;
158 gl_tex
= wined3d_texture_get_gl_texture(texture
, context
->gl_info
, srgb
);
159 target
= texture
->target
;
161 /* Generate a texture name if we don't already have one. */
164 *set_surface_desc
= TRUE
;
165 gl_info
->gl_ops
.gl
.p_glGenTextures(1, &gl_tex
->name
);
166 checkGLcall("glGenTextures");
167 TRACE("Generated texture %d.\n", gl_tex
->name
);
168 if (texture
->resource
.pool
== WINED3D_POOL_DEFAULT
)
170 /* Tell OpenGL to try and keep this texture in video ram (well mostly). */
172 gl_info
->gl_ops
.gl
.p_glPrioritizeTextures(1, &gl_tex
->name
, &tmp
);
174 /* Initialise the state of the texture object to the OpenGL defaults,
175 * not the D3D defaults. */
176 gl_tex
->states
[WINED3DTEXSTA_ADDRESSU
] = WINED3D_TADDRESS_WRAP
;
177 gl_tex
->states
[WINED3DTEXSTA_ADDRESSV
] = WINED3D_TADDRESS_WRAP
;
178 gl_tex
->states
[WINED3DTEXSTA_ADDRESSW
] = WINED3D_TADDRESS_WRAP
;
179 gl_tex
->states
[WINED3DTEXSTA_BORDERCOLOR
] = 0;
180 gl_tex
->states
[WINED3DTEXSTA_MAGFILTER
] = WINED3D_TEXF_LINEAR
;
181 gl_tex
->states
[WINED3DTEXSTA_MINFILTER
] = WINED3D_TEXF_POINT
; /* GL_NEAREST_MIPMAP_LINEAR */
182 gl_tex
->states
[WINED3DTEXSTA_MIPFILTER
] = WINED3D_TEXF_LINEAR
; /* GL_NEAREST_MIPMAP_LINEAR */
183 gl_tex
->states
[WINED3DTEXSTA_MAXMIPLEVEL
] = 0;
184 gl_tex
->states
[WINED3DTEXSTA_MAXANISOTROPY
] = 1;
185 if (context
->gl_info
->supported
[EXT_TEXTURE_SRGB_DECODE
])
186 gl_tex
->states
[WINED3DTEXSTA_SRGBTEXTURE
] = TRUE
;
188 gl_tex
->states
[WINED3DTEXSTA_SRGBTEXTURE
] = srgb
;
189 gl_tex
->states
[WINED3DTEXSTA_SHADOW
] = FALSE
;
190 wined3d_texture_set_dirty(texture
, TRUE
);
193 if (texture
->resource
.usage
& WINED3DUSAGE_AUTOGENMIPMAP
)
195 /* This means double binding the texture at creation, but keeps
196 * the code simpler all in all, and the run-time path free from
197 * additional checks. */
198 context_bind_texture(context
, target
, gl_tex
->name
);
199 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_GENERATE_MIPMAP_SGIS
, GL_TRUE
);
200 checkGLcall("glTexParameteri(target, GL_GENERATE_MIPMAP_SGIS, GL_TRUE)");
205 *set_surface_desc
= FALSE
;
210 context_bind_texture(context
, target
, gl_tex
->name
);
213 /* For a new texture we have to set the texture levels after
214 * binding the texture. Beware that texture rectangles do not
215 * support mipmapping, but set the maxmiplevel if we're relying
216 * on the partial GL_ARB_texture_non_power_of_two emulation with
217 * texture rectangles. (I.e., do not care about cond_np2 here,
218 * just look for GL_TEXTURE_RECTANGLE_ARB.) */
219 if (target
!= GL_TEXTURE_RECTANGLE_ARB
)
221 TRACE("Setting GL_TEXTURE_MAX_LEVEL to %u.\n", texture
->level_count
- 1);
222 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, texture
->level_count
- 1);
223 checkGLcall("glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, texture->level_count)");
225 if (target
== GL_TEXTURE_CUBE_MAP_ARB
)
227 /* Cubemaps are always set to clamp, regardless of the sampler state. */
228 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
229 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
230 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_R
, GL_CLAMP_TO_EDGE
);
236 ERR("This texture doesn't have an OpenGL texture assigned to it.\n");
237 hr
= WINED3DERR_INVALIDCALL
;
243 /* Context activation is done by the caller. */
244 static void apply_wrap(const struct wined3d_gl_info
*gl_info
, GLenum target
,
245 enum wined3d_texture_address d3d_wrap
, GLenum param
, BOOL cond_np2
)
249 if (d3d_wrap
< WINED3D_TADDRESS_WRAP
|| d3d_wrap
> WINED3D_TADDRESS_MIRROR_ONCE
)
251 FIXME("Unrecognized or unsupported texture address mode %#x.\n", d3d_wrap
);
255 /* Cubemaps are always set to clamp, regardless of the sampler state. */
256 if (target
== GL_TEXTURE_CUBE_MAP_ARB
257 || (cond_np2
&& d3d_wrap
== WINED3D_TADDRESS_WRAP
))
258 gl_wrap
= GL_CLAMP_TO_EDGE
;
260 gl_wrap
= gl_info
->wrap_lookup
[d3d_wrap
- WINED3D_TADDRESS_WRAP
];
262 TRACE("Setting param %#x to %#x for target %#x.\n", param
, gl_wrap
, target
);
263 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, param
, gl_wrap
);
264 checkGLcall("glTexParameteri(target, param, gl_wrap)");
267 /* Context activation is done by the caller (state handler). */
268 void wined3d_texture_apply_state_changes(struct wined3d_texture
*texture
,
269 const DWORD sampler_states
[WINED3D_HIGHEST_SAMPLER_STATE
+ 1],
270 const struct wined3d_gl_info
*gl_info
)
272 BOOL cond_np2
= texture
->flags
& WINED3D_TEXTURE_COND_NP2
;
273 GLenum target
= texture
->target
;
274 struct gl_texture
*gl_tex
;
278 TRACE("texture %p, sampler_states %p.\n", texture
, sampler_states
);
280 gl_tex
= wined3d_texture_get_gl_texture(texture
, gl_info
,
281 texture
->flags
& WINED3D_TEXTURE_IS_SRGB
);
283 /* This function relies on the correct texture being bound and loaded. */
285 if (sampler_states
[WINED3D_SAMP_ADDRESS_U
] != gl_tex
->states
[WINED3DTEXSTA_ADDRESSU
])
287 state
= sampler_states
[WINED3D_SAMP_ADDRESS_U
];
288 apply_wrap(gl_info
, target
, state
, GL_TEXTURE_WRAP_S
, cond_np2
);
289 gl_tex
->states
[WINED3DTEXSTA_ADDRESSU
] = state
;
292 if (sampler_states
[WINED3D_SAMP_ADDRESS_V
] != gl_tex
->states
[WINED3DTEXSTA_ADDRESSV
])
294 state
= sampler_states
[WINED3D_SAMP_ADDRESS_V
];
295 apply_wrap(gl_info
, target
, state
, GL_TEXTURE_WRAP_T
, cond_np2
);
296 gl_tex
->states
[WINED3DTEXSTA_ADDRESSV
] = state
;
299 if (sampler_states
[WINED3D_SAMP_ADDRESS_W
] != gl_tex
->states
[WINED3DTEXSTA_ADDRESSW
])
301 state
= sampler_states
[WINED3D_SAMP_ADDRESS_W
];
302 apply_wrap(gl_info
, target
, state
, GL_TEXTURE_WRAP_R
, cond_np2
);
303 gl_tex
->states
[WINED3DTEXSTA_ADDRESSW
] = state
;
306 if (sampler_states
[WINED3D_SAMP_BORDER_COLOR
] != gl_tex
->states
[WINED3DTEXSTA_BORDERCOLOR
])
310 state
= sampler_states
[WINED3D_SAMP_BORDER_COLOR
];
311 D3DCOLORTOGLFLOAT4(state
, col
);
312 TRACE("Setting border color for %#x to %#x.\n", target
, state
);
313 gl_info
->gl_ops
.gl
.p_glTexParameterfv(target
, GL_TEXTURE_BORDER_COLOR
, &col
[0]);
314 checkGLcall("glTexParameterfv(..., GL_TEXTURE_BORDER_COLOR, ...)");
315 gl_tex
->states
[WINED3DTEXSTA_BORDERCOLOR
] = state
;
318 if (sampler_states
[WINED3D_SAMP_MAG_FILTER
] != gl_tex
->states
[WINED3DTEXSTA_MAGFILTER
])
322 state
= sampler_states
[WINED3D_SAMP_MAG_FILTER
];
323 if (state
> WINED3D_TEXF_ANISOTROPIC
)
324 FIXME("Unrecognized or unsupported MAGFILTER* value %d.\n", state
);
326 gl_value
= wined3d_gl_mag_filter(texture
->mag_lookup
,
327 min(max(state
, WINED3D_TEXF_POINT
), WINED3D_TEXF_LINEAR
));
328 TRACE("ValueMAG=%#x setting MAGFILTER to %#x.\n", state
, gl_value
);
329 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MAG_FILTER
, gl_value
);
331 gl_tex
->states
[WINED3DTEXSTA_MAGFILTER
] = state
;
334 if ((sampler_states
[WINED3D_SAMP_MIN_FILTER
] != gl_tex
->states
[WINED3DTEXSTA_MINFILTER
]
335 || sampler_states
[WINED3D_SAMP_MIP_FILTER
] != gl_tex
->states
[WINED3DTEXSTA_MIPFILTER
]
336 || sampler_states
[WINED3D_SAMP_MAX_MIP_LEVEL
] != gl_tex
->states
[WINED3DTEXSTA_MAXMIPLEVEL
]))
340 gl_tex
->states
[WINED3DTEXSTA_MIPFILTER
] = sampler_states
[WINED3D_SAMP_MIP_FILTER
];
341 gl_tex
->states
[WINED3DTEXSTA_MINFILTER
] = sampler_states
[WINED3D_SAMP_MIN_FILTER
];
342 gl_tex
->states
[WINED3DTEXSTA_MAXMIPLEVEL
] = sampler_states
[WINED3D_SAMP_MAX_MIP_LEVEL
];
344 if (gl_tex
->states
[WINED3DTEXSTA_MINFILTER
] > WINED3D_TEXF_ANISOTROPIC
345 || gl_tex
->states
[WINED3DTEXSTA_MIPFILTER
] > WINED3D_TEXF_ANISOTROPIC
)
347 FIXME("Unrecognized or unsupported MIN_FILTER value %#x MIP_FILTER value %#x.\n",
348 gl_tex
->states
[WINED3DTEXSTA_MINFILTER
],
349 gl_tex
->states
[WINED3DTEXSTA_MIPFILTER
]);
351 gl_value
= wined3d_gl_min_mip_filter(texture
->min_mip_lookup
,
352 min(max(sampler_states
[WINED3D_SAMP_MIN_FILTER
], WINED3D_TEXF_POINT
), WINED3D_TEXF_LINEAR
),
353 min(max(sampler_states
[WINED3D_SAMP_MIP_FILTER
], WINED3D_TEXF_NONE
), WINED3D_TEXF_LINEAR
));
355 TRACE("ValueMIN=%#x, ValueMIP=%#x, setting MINFILTER to %#x.\n",
356 sampler_states
[WINED3D_SAMP_MIN_FILTER
],
357 sampler_states
[WINED3D_SAMP_MIP_FILTER
], gl_value
);
358 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MIN_FILTER
, gl_value
);
359 checkGLcall("glTexParameter GL_TEXTURE_MIN_FILTER, ...");
363 if (gl_tex
->states
[WINED3DTEXSTA_MIPFILTER
] == WINED3D_TEXF_NONE
)
364 gl_value
= texture
->lod
;
365 else if (gl_tex
->states
[WINED3DTEXSTA_MAXMIPLEVEL
] >= texture
->level_count
)
366 gl_value
= texture
->level_count
- 1;
367 else if (gl_tex
->states
[WINED3DTEXSTA_MAXMIPLEVEL
] < texture
->lod
)
368 /* texture->lod is already clamped in the setter. */
369 gl_value
= texture
->lod
;
371 gl_value
= gl_tex
->states
[WINED3DTEXSTA_MAXMIPLEVEL
];
373 /* Note that WINED3D_SAMP_MAX_MIP_LEVEL specifies the largest mipmap
374 * (default 0), while GL_TEXTURE_MAX_LEVEL specifies the smallest
375 * mimap used (default 1000). So WINED3D_SAMP_MAX_MIP_LEVEL
376 * corresponds to GL_TEXTURE_BASE_LEVEL. */
377 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, gl_value
);
381 if ((gl_tex
->states
[WINED3DTEXSTA_MAGFILTER
] != WINED3D_TEXF_ANISOTROPIC
382 && gl_tex
->states
[WINED3DTEXSTA_MINFILTER
] != WINED3D_TEXF_ANISOTROPIC
383 && gl_tex
->states
[WINED3DTEXSTA_MIPFILTER
] != WINED3D_TEXF_ANISOTROPIC
)
387 aniso
= sampler_states
[WINED3D_SAMP_MAX_ANISOTROPY
];
389 if (gl_tex
->states
[WINED3DTEXSTA_MAXANISOTROPY
] != aniso
)
391 if (gl_info
->supported
[EXT_TEXTURE_FILTER_ANISOTROPIC
])
393 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MAX_ANISOTROPY_EXT
, aniso
);
394 checkGLcall("glTexParameteri(GL_TEXTURE_MAX_ANISOTROPY_EXT, aniso)");
398 WARN("Anisotropic filtering not supported.\n");
400 gl_tex
->states
[WINED3DTEXSTA_MAXANISOTROPY
] = aniso
;
403 /* These should always be the same unless EXT_texture_sRGB_decode is supported. */
404 if (sampler_states
[WINED3D_SAMP_SRGB_TEXTURE
] != gl_tex
->states
[WINED3DTEXSTA_SRGBTEXTURE
])
406 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_SRGB_DECODE_EXT
,
407 sampler_states
[WINED3D_SAMP_SRGB_TEXTURE
] ? GL_DECODE_EXT
: GL_SKIP_DECODE_EXT
);
408 checkGLcall("glTexParameteri(GL_TEXTURE_SRGB_DECODE_EXT)");
409 gl_tex
->states
[WINED3DTEXSTA_SRGBTEXTURE
] = sampler_states
[WINED3D_SAMP_SRGB_TEXTURE
];
412 if (!(texture
->resource
.format
->flags
& WINED3DFMT_FLAG_SHADOW
)
413 != !gl_tex
->states
[WINED3DTEXSTA_SHADOW
])
415 if (texture
->resource
.format
->flags
& WINED3DFMT_FLAG_SHADOW
)
417 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_DEPTH_TEXTURE_MODE_ARB
, GL_LUMINANCE
);
418 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_COMPARE_MODE_ARB
, GL_COMPARE_R_TO_TEXTURE_ARB
);
419 checkGLcall("glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE_ARB)");
420 gl_tex
->states
[WINED3DTEXSTA_SHADOW
] = TRUE
;
424 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_COMPARE_MODE_ARB
, GL_NONE
);
425 checkGLcall("glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_NONE)");
426 gl_tex
->states
[WINED3DTEXSTA_SHADOW
] = FALSE
;
431 ULONG CDECL
wined3d_texture_incref(struct wined3d_texture
*texture
)
433 ULONG refcount
= InterlockedIncrement(&texture
->resource
.ref
);
435 TRACE("%p increasing refcount to %u.\n", texture
, refcount
);
440 /* Do not call while under the GL lock. */
441 ULONG CDECL
wined3d_texture_decref(struct wined3d_texture
*texture
)
443 ULONG refcount
= InterlockedDecrement(&texture
->resource
.ref
);
445 TRACE("%p decreasing refcount to %u.\n", texture
, refcount
);
449 wined3d_texture_cleanup(texture
);
450 texture
->resource
.parent_ops
->wined3d_object_destroyed(texture
->resource
.parent
);
451 HeapFree(GetProcessHeap(), 0, texture
);
457 struct wined3d_resource
* CDECL
wined3d_texture_get_resource(struct wined3d_texture
*texture
)
459 TRACE("texture %p.\n", texture
);
461 return &texture
->resource
;
464 DWORD CDECL
wined3d_texture_set_priority(struct wined3d_texture
*texture
, DWORD priority
)
466 return resource_set_priority(&texture
->resource
, priority
);
469 DWORD CDECL
wined3d_texture_get_priority(const struct wined3d_texture
*texture
)
471 return resource_get_priority(&texture
->resource
);
474 /* Do not call while under the GL lock. */
475 void CDECL
wined3d_texture_preload(struct wined3d_texture
*texture
)
477 texture
->texture_ops
->texture_preload(texture
, SRGB_ANY
);
480 void * CDECL
wined3d_texture_get_parent(const struct wined3d_texture
*texture
)
482 TRACE("texture %p.\n", texture
);
484 return texture
->resource
.parent
;
487 DWORD CDECL
wined3d_texture_set_lod(struct wined3d_texture
*texture
, DWORD lod
)
489 DWORD old
= texture
->lod
;
491 TRACE("texture %p, lod %u.\n", texture
, lod
);
493 /* The d3d9:texture test shows that SetLOD is ignored on non-managed
494 * textures. The call always returns 0, and GetLOD always returns 0. */
495 if (texture
->resource
.pool
!= WINED3D_POOL_MANAGED
)
497 TRACE("Ignoring SetLOD on %s texture, returning 0.\n", debug_d3dpool(texture
->resource
.pool
));
501 if (lod
>= texture
->level_count
)
502 lod
= texture
->level_count
- 1;
504 if (texture
->lod
!= lod
)
508 texture
->texture_rgb
.states
[WINED3DTEXSTA_MAXMIPLEVEL
] = ~0U;
509 texture
->texture_srgb
.states
[WINED3DTEXSTA_MAXMIPLEVEL
] = ~0U;
510 if (texture
->resource
.bind_count
)
511 device_invalidate_state(texture
->resource
.device
, STATE_SAMPLER(texture
->sampler
));
517 DWORD CDECL
wined3d_texture_get_lod(const struct wined3d_texture
*texture
)
519 TRACE("texture %p, returning %u.\n", texture
, texture
->lod
);
524 DWORD CDECL
wined3d_texture_get_level_count(const struct wined3d_texture
*texture
)
526 TRACE("texture %p, returning %u.\n", texture
, texture
->level_count
);
528 return texture
->level_count
;
531 HRESULT CDECL
wined3d_texture_set_autogen_filter_type(struct wined3d_texture
*texture
,
532 enum wined3d_texture_filter_type filter_type
)
534 FIXME("texture %p, filter_type %s stub!\n", texture
, debug_d3dtexturefiltertype(filter_type
));
536 if (!(texture
->resource
.usage
& WINED3DUSAGE_AUTOGENMIPMAP
))
538 WARN("Texture doesn't have AUTOGENMIPMAP usage.\n");
539 return WINED3DERR_INVALIDCALL
;
542 texture
->filter_type
= filter_type
;
547 enum wined3d_texture_filter_type CDECL
wined3d_texture_get_autogen_filter_type(const struct wined3d_texture
*texture
)
549 TRACE("texture %p.\n", texture
);
551 return texture
->filter_type
;
554 void CDECL
wined3d_texture_generate_mipmaps(struct wined3d_texture
*texture
)
556 /* TODO: Implement filters using GL_SGI_generate_mipmaps. */
557 FIXME("texture %p stub!\n", texture
);
560 struct wined3d_resource
* CDECL
wined3d_texture_get_sub_resource(struct wined3d_texture
*texture
,
561 UINT sub_resource_idx
)
563 UINT sub_count
= texture
->level_count
* texture
->layer_count
;
565 TRACE("texture %p, sub_resource_idx %u.\n", texture
, sub_resource_idx
);
567 if (sub_resource_idx
>= sub_count
)
569 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx
, sub_count
);
573 return texture
->sub_resources
[sub_resource_idx
];
576 HRESULT CDECL
wined3d_texture_add_dirty_region(struct wined3d_texture
*texture
,
577 UINT layer
, const struct wined3d_box
*dirty_region
)
579 struct wined3d_resource
*sub_resource
;
581 TRACE("texture %p, layer %u, dirty_region %p.\n", texture
, layer
, dirty_region
);
583 if (!(sub_resource
= wined3d_texture_get_sub_resource(texture
, layer
* texture
->level_count
)))
585 WARN("Failed to get sub-resource.\n");
586 return WINED3DERR_INVALIDCALL
;
589 wined3d_texture_set_dirty(texture
, TRUE
);
590 texture
->texture_ops
->texture_sub_resource_add_dirty_region(sub_resource
, dirty_region
);
595 /* Context activation is done by the caller. */
596 static HRESULT
texture2d_bind(struct wined3d_texture
*texture
,
597 struct wined3d_context
*context
, BOOL srgb
)
599 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
600 BOOL set_gl_texture_desc
;
603 TRACE("texture %p, context %p, srgb %#x.\n", texture
, context
, srgb
);
605 hr
= wined3d_texture_bind(texture
, context
, srgb
, &set_gl_texture_desc
);
606 if (set_gl_texture_desc
&& SUCCEEDED(hr
))
608 UINT sub_count
= texture
->level_count
* texture
->layer_count
;
609 BOOL srgb_tex
= !context
->gl_info
->supported
[EXT_TEXTURE_SRGB_DECODE
]
610 && (texture
->flags
& WINED3D_TEXTURE_IS_SRGB
);
611 struct gl_texture
*gl_tex
;
614 gl_tex
= wined3d_texture_get_gl_texture(texture
, context
->gl_info
, srgb_tex
);
616 for (i
= 0; i
< sub_count
; ++i
)
618 struct wined3d_surface
*surface
= surface_from_resource(texture
->sub_resources
[i
]);
619 surface_set_texture_name(surface
, gl_tex
->name
, srgb_tex
);
622 /* Conditinal non power of two textures use a different clamping
623 * default. If we're using the GL_WINE_normalized_texrect partial
624 * driver emulation, we're dealing with a GL_TEXTURE_2D texture which
625 * has the address mode set to repeat - something that prevents us
626 * from hitting the accelerated codepath. Thus manually set the GL
627 * state. The same applies to filtering. Even if the texture has only
628 * one mip level, the default LINEAR_MIPMAP_LINEAR filter causes a SW
629 * fallback on macos. */
630 if (texture
->flags
& WINED3D_TEXTURE_COND_NP2
)
632 GLenum target
= texture
->target
;
634 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
635 checkGLcall("glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)");
636 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
637 checkGLcall("glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)");
638 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
639 checkGLcall("glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST)");
640 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
641 checkGLcall("glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST)");
642 gl_tex
->states
[WINED3DTEXSTA_ADDRESSU
] = WINED3D_TADDRESS_CLAMP
;
643 gl_tex
->states
[WINED3DTEXSTA_ADDRESSV
] = WINED3D_TADDRESS_CLAMP
;
644 gl_tex
->states
[WINED3DTEXSTA_MAGFILTER
] = WINED3D_TEXF_POINT
;
645 gl_tex
->states
[WINED3DTEXSTA_MINFILTER
] = WINED3D_TEXF_POINT
;
646 gl_tex
->states
[WINED3DTEXSTA_MIPFILTER
] = WINED3D_TEXF_NONE
;
653 static BOOL
texture_srgb_mode(const struct wined3d_texture
*texture
, enum WINED3DSRGB srgb
)
664 return texture
->flags
& WINED3D_TEXTURE_IS_SRGB
;
668 /* Do not call while under the GL lock. */
669 static void texture2d_preload(struct wined3d_texture
*texture
, enum WINED3DSRGB srgb
)
671 UINT sub_count
= texture
->level_count
* texture
->layer_count
;
672 struct wined3d_device
*device
= texture
->resource
.device
;
673 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
674 struct wined3d_context
*context
= NULL
;
675 struct gl_texture
*gl_tex
;
679 TRACE("texture %p, srgb %#x.\n", texture
, srgb
);
681 srgb_mode
= texture_srgb_mode(texture
, srgb
);
682 gl_tex
= wined3d_texture_get_gl_texture(texture
, gl_info
, srgb_mode
);
684 if (!device
->isInDraw
)
686 /* No danger of recursive calls, context_acquire() sets isInDraw to TRUE
687 * when loading offscreen render targets into the texture. */
688 context
= context_acquire(device
, NULL
);
693 /* Reload the surfaces if the texture is marked dirty. */
694 for (i
= 0; i
< sub_count
; ++i
)
696 surface_load(surface_from_resource(texture
->sub_resources
[i
]), srgb_mode
);
701 TRACE("Texture %p not dirty, nothing to do.\n", texture
);
704 /* No longer dirty. */
705 gl_tex
->dirty
= FALSE
;
707 if (context
) context_release(context
);
710 static void texture2d_sub_resource_add_dirty_region(struct wined3d_resource
*sub_resource
,
711 const struct wined3d_box
*dirty_region
)
713 surface_add_dirty_rect(surface_from_resource(sub_resource
), dirty_region
);
716 static void texture2d_sub_resource_cleanup(struct wined3d_resource
*sub_resource
)
718 struct wined3d_surface
*surface
= surface_from_resource(sub_resource
);
720 /* Clean out the texture name we gave to the surface so that the
721 * surface doesn't try and release it. */
722 surface_set_texture_name(surface
, 0, TRUE
);
723 surface_set_texture_name(surface
, 0, FALSE
);
724 surface_set_texture_target(surface
, 0, 0);
725 surface_set_container(surface
, NULL
);
726 wined3d_surface_decref(surface
);
729 /* Do not call while under the GL lock. */
730 static void texture2d_unload(struct wined3d_resource
*resource
)
732 struct wined3d_texture
*texture
= wined3d_texture_from_resource(resource
);
733 UINT sub_count
= texture
->level_count
* texture
->layer_count
;
736 TRACE("texture %p.\n", texture
);
738 for (i
= 0; i
< sub_count
; ++i
)
740 struct wined3d_resource
*sub_resource
= texture
->sub_resources
[i
];
741 struct wined3d_surface
*surface
= surface_from_resource(sub_resource
);
743 sub_resource
->resource_ops
->resource_unload(sub_resource
);
744 surface_set_texture_name(surface
, 0, FALSE
); /* Delete RGB name */
745 surface_set_texture_name(surface
, 0, TRUE
); /* Delete sRGB name */
748 wined3d_texture_unload(texture
);
751 static const struct wined3d_texture_ops texture2d_ops
=
755 texture2d_sub_resource_add_dirty_region
,
756 texture2d_sub_resource_cleanup
,
759 static const struct wined3d_resource_ops texture2d_resource_ops
=
764 static HRESULT
cubetexture_init(struct wined3d_texture
*texture
, const struct wined3d_resource_desc
*desc
,
765 UINT levels
, struct wined3d_device
*device
, void *parent
, const struct wined3d_parent_ops
*parent_ops
)
767 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
768 struct wined3d_resource_desc surface_desc
;
772 /* TODO: It should only be possible to create textures for formats
773 * that are reported as supported. */
774 if (WINED3DFMT_UNKNOWN
>= desc
->format
)
776 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture
);
777 return WINED3DERR_INVALIDCALL
;
780 if (!gl_info
->supported
[ARB_TEXTURE_CUBE_MAP
] && desc
->pool
!= WINED3D_POOL_SCRATCH
)
782 WARN("(%p) : Tried to create not supported cube texture.\n", texture
);
783 return WINED3DERR_INVALIDCALL
;
786 /* Calculate levels for mip mapping */
787 if (desc
->usage
& WINED3DUSAGE_AUTOGENMIPMAP
)
789 if (!gl_info
->supported
[SGIS_GENERATE_MIPMAP
])
791 WARN("No mipmap generation support, returning D3DERR_INVALIDCALL.\n");
792 return WINED3DERR_INVALIDCALL
;
797 WARN("D3DUSAGE_AUTOGENMIPMAP is set, and level count > 1, returning D3DERR_INVALIDCALL.\n");
798 return WINED3DERR_INVALIDCALL
;
805 levels
= wined3d_log2i(desc
->width
) + 1;
806 TRACE("Calculated levels = %u.\n", levels
);
809 if (!gl_info
->supported
[ARB_TEXTURE_NON_POWER_OF_TWO
])
811 UINT pow2_edge_length
= 1;
812 while (pow2_edge_length
< desc
->width
)
813 pow2_edge_length
<<= 1;
815 if (desc
->width
!= pow2_edge_length
)
817 if (desc
->pool
== WINED3D_POOL_SCRATCH
)
819 /* SCRATCH textures cannot be used for texturing */
820 WARN("Creating a scratch NPOT cube texture despite lack of HW support.\n");
824 WARN("Attempted to create a NPOT cube texture (edge length %u) without GL support.\n", desc
->width
);
825 return WINED3DERR_INVALIDCALL
;
830 if (FAILED(hr
= wined3d_texture_init(texture
, &texture2d_ops
, 6, levels
,
831 desc
, device
, parent
, parent_ops
, &texture2d_resource_ops
)))
833 WARN("Failed to initialize texture, returning %#x\n", hr
);
837 texture
->pow2_matrix
[0] = 1.0f
;
838 texture
->pow2_matrix
[5] = 1.0f
;
839 texture
->pow2_matrix
[10] = 1.0f
;
840 texture
->pow2_matrix
[15] = 1.0f
;
841 texture
->target
= GL_TEXTURE_CUBE_MAP_ARB
;
843 /* Generate all the surfaces. */
844 surface_desc
= *desc
;
845 surface_desc
.resource_type
= WINED3D_RTYPE_SURFACE
;
846 for (i
= 0; i
< texture
->level_count
; ++i
)
848 /* Create the 6 faces. */
849 for (j
= 0; j
< 6; ++j
)
851 static const GLenum cube_targets
[6] =
853 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
,
854 GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
,
855 GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
,
856 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
,
857 GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
,
858 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
,
860 UINT idx
= j
* texture
->level_count
+ i
;
861 struct wined3d_surface
*surface
;
863 if (FAILED(hr
= device
->device_parent
->ops
->create_texture_surface(device
->device_parent
,
864 parent
, &surface_desc
, idx
, &surface
)))
866 FIXME("(%p) Failed to create surface, hr %#x.\n", texture
, hr
);
867 wined3d_texture_cleanup(texture
);
871 surface_set_container(surface
, texture
);
872 surface_set_texture_target(surface
, cube_targets
[j
], i
);
873 texture
->sub_resources
[idx
] = &surface
->resource
;
874 TRACE("Created surface level %u @ %p.\n", i
, surface
);
876 surface_desc
.width
= max(1, surface_desc
.width
>> 1);
877 surface_desc
.height
= surface_desc
.width
;
883 static HRESULT
texture_init(struct wined3d_texture
*texture
, const struct wined3d_resource_desc
*desc
,
884 UINT levels
, struct wined3d_device
*device
, void *parent
, const struct wined3d_parent_ops
*parent_ops
)
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
;
892 /* TODO: It should only be possible to create textures for formats
893 * that are reported as supported. */
894 if (WINED3DFMT_UNKNOWN
>= desc
->format
)
896 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture
);
897 return WINED3DERR_INVALIDCALL
;
900 /* Non-power2 support. */
901 if (gl_info
->supported
[ARB_TEXTURE_NON_POWER_OF_TWO
])
903 pow2_width
= desc
->width
;
904 pow2_height
= desc
->height
;
908 /* Find the nearest pow2 match. */
909 pow2_width
= pow2_height
= 1;
910 while (pow2_width
< desc
->width
)
912 while (pow2_height
< desc
->height
)
915 if (pow2_width
!= desc
->width
|| pow2_height
!= desc
->height
)
917 /* levels == 0 returns an error as well */
920 if (desc
->pool
== WINED3D_POOL_SCRATCH
)
922 WARN("Creating a scratch mipmapped NPOT texture despite lack of HW support.\n");
926 WARN("Attempted to create a mipmapped NPOT texture without unconditional NPOT support.\n");
927 return WINED3DERR_INVALIDCALL
;
933 /* Calculate levels for mip mapping. */
934 if (desc
->usage
& WINED3DUSAGE_AUTOGENMIPMAP
)
936 if (!gl_info
->supported
[SGIS_GENERATE_MIPMAP
])
938 WARN("No mipmap generation support, returning WINED3DERR_INVALIDCALL.\n");
939 return WINED3DERR_INVALIDCALL
;
944 WARN("D3DUSAGE_AUTOGENMIPMAP is set, and level count > 1, returning WINED3DERR_INVALIDCALL.\n");
945 return WINED3DERR_INVALIDCALL
;
952 levels
= wined3d_log2i(max(desc
->width
, desc
->height
)) + 1;
953 TRACE("Calculated levels = %u.\n", levels
);
956 if (FAILED(hr
= wined3d_texture_init(texture
, &texture2d_ops
, 1, levels
,
957 desc
, device
, parent
, parent_ops
, &texture2d_resource_ops
)))
959 WARN("Failed to initialize texture, returning %#x.\n", hr
);
963 /* Precalculated scaling for 'faked' non power of two texture coords.
964 * Second also don't use ARB_TEXTURE_RECTANGLE in case the surface format is P8 and EXT_PALETTED_TEXTURE
965 * is used in combination with texture uploads (RTL_READTEX). The reason is that EXT_PALETTED_TEXTURE
966 * doesn't work in combination with ARB_TEXTURE_RECTANGLE. */
967 if (gl_info
->supported
[WINED3D_GL_NORMALIZED_TEXRECT
]
968 && (desc
->width
!= pow2_width
|| desc
->height
!= pow2_height
))
970 texture
->pow2_matrix
[0] = 1.0f
;
971 texture
->pow2_matrix
[5] = 1.0f
;
972 texture
->pow2_matrix
[10] = 1.0f
;
973 texture
->pow2_matrix
[15] = 1.0f
;
974 texture
->target
= GL_TEXTURE_2D
;
975 texture
->flags
|= WINED3D_TEXTURE_COND_NP2
;
976 texture
->min_mip_lookup
= minMipLookup_noFilter
;
978 else if (gl_info
->supported
[ARB_TEXTURE_RECTANGLE
] && (desc
->width
!= pow2_width
|| desc
->height
!= pow2_height
)
979 && !(desc
->format
== WINED3DFMT_P8_UINT
&& gl_info
->supported
[EXT_PALETTED_TEXTURE
]
980 && wined3d_settings
.rendertargetlock_mode
== RTL_READTEX
))
982 texture
->pow2_matrix
[0] = (float)desc
->width
;
983 texture
->pow2_matrix
[5] = (float)desc
->height
;
984 texture
->pow2_matrix
[10] = 1.0f
;
985 texture
->pow2_matrix
[15] = 1.0f
;
986 texture
->target
= GL_TEXTURE_RECTANGLE_ARB
;
987 texture
->flags
|= WINED3D_TEXTURE_COND_NP2
;
988 texture
->flags
&= ~WINED3D_TEXTURE_POW2_MAT_IDENT
;
990 if (texture
->resource
.format
->flags
& WINED3DFMT_FLAG_FILTERING
)
991 texture
->min_mip_lookup
= minMipLookup_noMip
;
993 texture
->min_mip_lookup
= minMipLookup_noFilter
;
997 if ((desc
->width
!= pow2_width
) || (desc
->height
!= pow2_height
))
999 texture
->pow2_matrix
[0] = (((float)desc
->width
) / ((float)pow2_width
));
1000 texture
->pow2_matrix
[5] = (((float)desc
->height
) / ((float)pow2_height
));
1001 texture
->flags
&= ~WINED3D_TEXTURE_POW2_MAT_IDENT
;
1005 texture
->pow2_matrix
[0] = 1.0f
;
1006 texture
->pow2_matrix
[5] = 1.0f
;
1009 texture
->pow2_matrix
[10] = 1.0f
;
1010 texture
->pow2_matrix
[15] = 1.0f
;
1011 texture
->target
= GL_TEXTURE_2D
;
1013 TRACE("xf(%f) yf(%f)\n", texture
->pow2_matrix
[0], texture
->pow2_matrix
[5]);
1015 /* Generate all the surfaces. */
1016 surface_desc
= *desc
;
1017 surface_desc
.resource_type
= WINED3D_RTYPE_SURFACE
;
1018 for (i
= 0; i
< texture
->level_count
; ++i
)
1020 struct wined3d_surface
*surface
;
1022 /* Use the callback to create the texture surface. */
1023 if (FAILED(hr
= device
->device_parent
->ops
->create_texture_surface(device
->device_parent
,
1024 parent
, &surface_desc
, i
, &surface
)))
1026 FIXME("Failed to create surface %p, hr %#x\n", texture
, hr
);
1027 wined3d_texture_cleanup(texture
);
1031 surface_set_container(surface
, texture
);
1032 surface_set_texture_target(surface
, texture
->target
, i
);
1033 texture
->sub_resources
[i
] = &surface
->resource
;
1034 TRACE("Created surface level %u @ %p.\n", i
, surface
);
1035 /* Calculate the next mipmap level. */
1036 surface_desc
.width
= max(1, surface_desc
.width
>> 1);
1037 surface_desc
.height
= max(1, surface_desc
.height
>> 1);
1043 /* Context activation is done by the caller. */
1044 static HRESULT
texture3d_bind(struct wined3d_texture
*texture
,
1045 struct wined3d_context
*context
, BOOL srgb
)
1049 TRACE("texture %p, context %p, srgb %#x.\n", texture
, context
, srgb
);
1051 return wined3d_texture_bind(texture
, context
, srgb
, &dummy
);
1054 /* Do not call while under the GL lock. */
1055 static void texture3d_preload(struct wined3d_texture
*texture
, enum WINED3DSRGB srgb
)
1057 struct wined3d_device
*device
= texture
->resource
.device
;
1058 struct wined3d_context
*context
;
1059 BOOL srgb_was_toggled
= FALSE
;
1062 TRACE("texture %p, srgb %#x.\n", texture
, srgb
);
1064 /* TODO: Use already acquired context when possible. */
1065 context
= context_acquire(device
, NULL
);
1066 if (texture
->resource
.bind_count
> 0)
1068 BOOL texture_srgb
= texture
->flags
& WINED3D_TEXTURE_IS_SRGB
;
1069 BOOL sampler_srgb
= texture_srgb_mode(texture
, srgb
);
1070 srgb_was_toggled
= !texture_srgb
!= !sampler_srgb
;
1072 if (srgb_was_toggled
)
1075 texture
->flags
|= WINED3D_TEXTURE_IS_SRGB
;
1077 texture
->flags
&= ~WINED3D_TEXTURE_IS_SRGB
;
1081 /* If the texture is marked dirty or the sRGB sampler setting has changed
1082 * since the last load then reload the volumes. */
1083 if (texture
->texture_rgb
.dirty
)
1085 for (i
= 0; i
< texture
->level_count
; ++i
)
1087 volume_load(volume_from_resource(texture
->sub_resources
[i
]), context
, i
,
1088 texture
->flags
& WINED3D_TEXTURE_IS_SRGB
);
1091 else if (srgb_was_toggled
)
1093 for (i
= 0; i
< texture
->level_count
; ++i
)
1095 struct wined3d_volume
*volume
= volume_from_resource(texture
->sub_resources
[i
]);
1096 volume_add_dirty_box(volume
, NULL
);
1097 volume_load(volume
, context
, i
, texture
->flags
& WINED3D_TEXTURE_IS_SRGB
);
1102 TRACE("Texture %p not dirty, nothing to do.\n", texture
);
1105 context_release(context
);
1107 /* No longer dirty */
1108 texture
->texture_rgb
.dirty
= FALSE
;
1111 static void texture3d_sub_resource_add_dirty_region(struct wined3d_resource
*sub_resource
,
1112 const struct wined3d_box
*dirty_region
)
1114 volume_add_dirty_box(volume_from_resource(sub_resource
), dirty_region
);
1117 static void texture3d_sub_resource_cleanup(struct wined3d_resource
*sub_resource
)
1119 struct wined3d_volume
*volume
= volume_from_resource(sub_resource
);
1121 /* Cleanup the container. */
1122 volume_set_container(volume
, NULL
);
1123 wined3d_volume_decref(volume
);
1126 /* Do not call while under the GL lock. */
1127 static void texture3d_unload(struct wined3d_resource
*resource
)
1129 struct wined3d_texture
*texture
= wined3d_texture_from_resource(resource
);
1132 TRACE("texture %p.\n", texture
);
1134 for (i
= 0; i
< texture
->level_count
; ++i
)
1136 struct wined3d_resource
*sub_resource
= texture
->sub_resources
[i
];
1137 sub_resource
->resource_ops
->resource_unload(sub_resource
);
1140 wined3d_texture_unload(texture
);
1143 static const struct wined3d_texture_ops texture3d_ops
=
1147 texture3d_sub_resource_add_dirty_region
,
1148 texture3d_sub_resource_cleanup
,
1151 static const struct wined3d_resource_ops texture3d_resource_ops
=
1156 static HRESULT
volumetexture_init(struct wined3d_texture
*texture
, UINT width
, UINT height
,
1157 UINT depth
, UINT levels
, struct wined3d_device
*device
, DWORD usage
, enum wined3d_format_id format_id
,
1158 enum wined3d_pool pool
, void *parent
, const struct wined3d_parent_ops
*parent_ops
)
1160 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
1161 struct wined3d_resource_desc desc
;
1162 UINT tmp_w
, tmp_h
, tmp_d
;
1166 /* TODO: It should only be possible to create textures for formats
1167 * that are reported as supported. */
1168 if (WINED3DFMT_UNKNOWN
>= format_id
)
1170 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture
);
1171 return WINED3DERR_INVALIDCALL
;
1174 if (!gl_info
->supported
[EXT_TEXTURE3D
])
1176 WARN("(%p) : Texture cannot be created - no volume texture support.\n", texture
);
1177 return WINED3DERR_INVALIDCALL
;
1180 /* Calculate levels for mip mapping. */
1181 if (usage
& WINED3DUSAGE_AUTOGENMIPMAP
)
1183 if (!gl_info
->supported
[SGIS_GENERATE_MIPMAP
])
1185 WARN("No mipmap generation support, returning D3DERR_INVALIDCALL.\n");
1186 return WINED3DERR_INVALIDCALL
;
1191 WARN("D3DUSAGE_AUTOGENMIPMAP is set, and level count > 1, returning D3DERR_INVALIDCALL.\n");
1192 return WINED3DERR_INVALIDCALL
;
1199 levels
= wined3d_log2i(max(max(width
, height
), depth
)) + 1;
1200 TRACE("Calculated levels = %u.\n", levels
);
1203 if (!gl_info
->supported
[ARB_TEXTURE_NON_POWER_OF_TWO
])
1205 UINT pow2_w
, pow2_h
, pow2_d
;
1207 while (pow2_w
< width
) pow2_w
<<= 1;
1209 while (pow2_h
< height
) pow2_h
<<= 1;
1211 while (pow2_d
< depth
) pow2_d
<<= 1;
1213 if (pow2_w
!= width
|| pow2_h
!= height
|| pow2_d
!= depth
)
1215 if (pool
== WINED3D_POOL_SCRATCH
)
1217 WARN("Creating a scratch NPOT volume texture despite lack of HW support.\n");
1221 WARN("Attempted to create a NPOT volume texture (%u,%u,%u) without GL support.\n",
1222 width
, height
, depth
);
1223 return WINED3DERR_INVALIDCALL
;
1228 desc
.resource_type
= WINED3D_RTYPE_VOLUME_TEXTURE
;
1229 desc
.format
= format_id
;
1230 desc
.multisample_type
= WINED3D_MULTISAMPLE_NONE
;
1231 desc
.multisample_quality
= 0;
1235 desc
.height
= height
;
1239 if (FAILED(hr
= wined3d_texture_init(texture
, &texture3d_ops
, 1, levels
,
1240 &desc
, device
, parent
, parent_ops
, &texture3d_resource_ops
)))
1242 WARN("Failed to initialize texture, returning %#x.\n", hr
);
1246 texture
->pow2_matrix
[0] = 1.0f
;
1247 texture
->pow2_matrix
[5] = 1.0f
;
1248 texture
->pow2_matrix
[10] = 1.0f
;
1249 texture
->pow2_matrix
[15] = 1.0f
;
1250 texture
->target
= GL_TEXTURE_3D
;
1252 /* Generate all the surfaces. */
1257 for (i
= 0; i
< texture
->level_count
; ++i
)
1259 struct wined3d_volume
*volume
;
1261 /* Create the volume. */
1262 hr
= device
->device_parent
->ops
->create_volume(device
->device_parent
, parent
,
1263 tmp_w
, tmp_h
, tmp_d
, format_id
, pool
, usage
, &volume
);
1266 ERR("Creating a volume for the volume texture failed, hr %#x.\n", hr
);
1267 wined3d_texture_cleanup(texture
);
1271 /* Set its container to this texture. */
1272 volume_set_container(volume
, texture
);
1273 texture
->sub_resources
[i
] = &volume
->resource
;
1275 /* Calculate the next mipmap level. */
1276 tmp_w
= max(1, tmp_w
>> 1);
1277 tmp_h
= max(1, tmp_h
>> 1);
1278 tmp_d
= max(1, tmp_d
>> 1);
1284 HRESULT CDECL
wined3d_texture_create_2d(struct wined3d_device
*device
, const struct wined3d_resource_desc
*desc
,
1285 UINT level_count
, void *parent
, const struct wined3d_parent_ops
*parent_ops
, struct wined3d_texture
**texture
)
1287 struct wined3d_texture
*object
;
1290 TRACE("device %p, desc %p, level_count %u, parent %p, parent_ops %p, texture %p.\n",
1291 device
, desc
, level_count
, parent
, parent_ops
, texture
);
1293 object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
));
1297 return WINED3DERR_OUTOFVIDEOMEMORY
;
1300 if (FAILED(hr
= texture_init(object
, desc
, level_count
, device
, parent
, parent_ops
)))
1302 WARN("Failed to initialize texture, returning %#x.\n", hr
);
1303 HeapFree(GetProcessHeap(), 0, object
);
1308 TRACE("Created texture %p.\n", object
);
1314 HRESULT CDECL
wined3d_texture_create_3d(struct wined3d_device
*device
, UINT width
, UINT height
, UINT depth
,
1315 UINT level_count
, DWORD usage
, enum wined3d_format_id format_id
, enum wined3d_pool pool
, void *parent
,
1316 const struct wined3d_parent_ops
*parent_ops
, struct wined3d_texture
**texture
)
1318 struct wined3d_texture
*object
;
1321 TRACE("device %p, width %u, height %u, depth %u, level_count %u, usage %#x\n",
1322 device
, width
, height
, depth
, level_count
, usage
);
1323 TRACE("format %s, pool %#x, parent %p, parent_ops %p, texture %p.\n",
1324 debug_d3dformat(format_id
), pool
, parent
, parent_ops
, texture
);
1326 object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
));
1330 return WINED3DERR_OUTOFVIDEOMEMORY
;
1333 hr
= volumetexture_init(object
, width
, height
, depth
, level_count
,
1334 device
, usage
, format_id
, pool
, parent
, parent_ops
);
1337 WARN("Failed to initialize volumetexture, returning %#x\n", hr
);
1338 HeapFree(GetProcessHeap(), 0, object
);
1343 TRACE("Created texture %p.\n", object
);
1349 HRESULT CDECL
wined3d_texture_create_cube(struct wined3d_device
*device
, const struct wined3d_resource_desc
*desc
,
1350 UINT level_count
, void *parent
, const struct wined3d_parent_ops
*parent_ops
, struct wined3d_texture
**texture
)
1352 struct wined3d_texture
*object
;
1355 TRACE("device %p, desc %p, level_count %u, parent %p, parent_ops %p, texture %p.\n",
1356 device
, desc
, level_count
, parent
, parent_ops
, texture
);
1358 object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
));
1362 return WINED3DERR_OUTOFVIDEOMEMORY
;
1365 if (FAILED(hr
= cubetexture_init(object
, desc
, level_count
, device
, parent
, parent_ops
)))
1367 WARN("Failed to initialize cubetexture, returning %#x\n", hr
);
1368 HeapFree(GetProcessHeap(), 0, object
);
1373 TRACE("Created texture %p.\n", object
);