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
);
28 WINE_DECLARE_DEBUG_CHANNEL(winediag
);
30 static HRESULT
wined3d_texture_init(struct wined3d_texture
*texture
, const struct wined3d_texture_ops
*texture_ops
,
31 UINT layer_count
, UINT level_count
, const struct wined3d_resource_desc
*desc
, DWORD surface_flags
,
32 struct wined3d_device
*device
, void *parent
, const struct wined3d_parent_ops
*parent_ops
,
33 const struct wined3d_resource_ops
*resource_ops
)
35 const struct wined3d_format
*format
= wined3d_get_format(&device
->adapter
->gl_info
, desc
->format
);
38 TRACE("texture %p, texture_ops %p, layer_count %u, level_count %u, resource_type %s, format %s, "
39 "multisample_type %#x, multisample_quality %#x, usage %s, pool %s, width %u, height %u, depth %u, "
40 "surface_flags %#x, device %p, parent %p, parent_ops %p, resource_ops %p.\n",
41 texture
, texture_ops
, layer_count
, level_count
, debug_d3dresourcetype(desc
->resource_type
),
42 debug_d3dformat(desc
->format
), desc
->multisample_type
, desc
->multisample_quality
,
43 debug_d3dusage(desc
->usage
), debug_d3dpool(desc
->pool
), desc
->width
, desc
->height
, desc
->depth
,
44 surface_flags
, device
, parent
, parent_ops
, resource_ops
);
46 if ((format
->flags
& (WINED3DFMT_FLAG_BLOCKS
| WINED3DFMT_FLAG_BLOCKS_NO_VERIFY
)) == WINED3DFMT_FLAG_BLOCKS
)
48 UINT width_mask
= format
->block_width
- 1;
49 UINT height_mask
= format
->block_height
- 1;
50 if (desc
->width
& width_mask
|| desc
->height
& height_mask
)
51 return WINED3DERR_INVALIDCALL
;
54 if (FAILED(hr
= resource_init(&texture
->resource
, device
, desc
->resource_type
, format
,
55 desc
->multisample_type
, desc
->multisample_quality
, desc
->usage
, desc
->pool
,
56 desc
->width
, desc
->height
, desc
->depth
, 0, parent
, parent_ops
, resource_ops
)))
58 static unsigned int once
;
60 if ((desc
->format
== WINED3DFMT_DXT1
|| desc
->format
== WINED3DFMT_DXT2
|| desc
->format
== WINED3DFMT_DXT3
61 || desc
->format
== WINED3DFMT_DXT4
|| desc
->format
== WINED3DFMT_DXT5
)
62 && !(format
->flags
& WINED3DFMT_FLAG_TEXTURE
) && !once
++)
63 ERR_(winediag
)("The application tried to create a DXTn texture, but the driver does not support them.\n");
65 WARN("Failed to initialize resource, returning %#x\n", hr
);
68 wined3d_resource_update_draw_binding(&texture
->resource
);
70 texture
->texture_ops
= texture_ops
;
71 texture
->sub_resources
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
72 level_count
* layer_count
* sizeof(*texture
->sub_resources
));
73 if (!texture
->sub_resources
)
75 ERR("Failed to allocate sub-resource array.\n");
76 resource_cleanup(&texture
->resource
);
80 texture
->layer_count
= layer_count
;
81 texture
->level_count
= level_count
;
82 texture
->filter_type
= (desc
->usage
& WINED3DUSAGE_AUTOGENMIPMAP
) ? WINED3D_TEXF_LINEAR
: WINED3D_TEXF_NONE
;
84 texture
->flags
= WINED3D_TEXTURE_POW2_MAT_IDENT
| WINED3D_TEXTURE_NORMALIZED_COORDS
;
85 if (surface_flags
& WINED3D_SURFACE_PIN_SYSMEM
)
86 texture
->flags
|= WINED3D_TEXTURE_PIN_SYSMEM
;
91 /* A GL context is provided by the caller */
92 static void gltexture_delete(const struct wined3d_gl_info
*gl_info
, struct gl_texture
*tex
)
94 gl_info
->gl_ops
.gl
.p_glDeleteTextures(1, &tex
->name
);
98 static void wined3d_texture_unload_gl_texture(struct wined3d_texture
*texture
)
100 struct wined3d_device
*device
= texture
->resource
.device
;
101 struct wined3d_context
*context
= NULL
;
103 if (texture
->texture_rgb
.name
|| texture
->texture_srgb
.name
)
105 context
= context_acquire(device
, NULL
);
108 if (texture
->texture_rgb
.name
)
109 gltexture_delete(context
->gl_info
, &texture
->texture_rgb
);
111 if (texture
->texture_srgb
.name
)
112 gltexture_delete(context
->gl_info
, &texture
->texture_srgb
);
114 if (context
) context_release(context
);
116 wined3d_texture_set_dirty(texture
);
118 resource_unload(&texture
->resource
);
121 static void wined3d_texture_cleanup(struct wined3d_texture
*texture
)
123 UINT sub_count
= texture
->level_count
* texture
->layer_count
;
126 TRACE("texture %p.\n", texture
);
128 for (i
= 0; i
< sub_count
; ++i
)
130 struct wined3d_resource
*sub_resource
= texture
->sub_resources
[i
];
133 texture
->texture_ops
->texture_sub_resource_cleanup(sub_resource
);
136 wined3d_texture_unload_gl_texture(texture
);
137 HeapFree(GetProcessHeap(), 0, texture
->sub_resources
);
138 resource_cleanup(&texture
->resource
);
141 void wined3d_texture_set_swapchain(struct wined3d_texture
*texture
, struct wined3d_swapchain
*swapchain
)
143 texture
->swapchain
= swapchain
;
144 wined3d_resource_update_draw_binding(&texture
->resource
);
147 void wined3d_texture_set_dirty(struct wined3d_texture
*texture
)
149 texture
->flags
&= ~(WINED3D_TEXTURE_RGB_VALID
| WINED3D_TEXTURE_SRGB_VALID
);
152 /* Context activation is done by the caller. */
153 void wined3d_texture_bind(struct wined3d_texture
*texture
,
154 struct wined3d_context
*context
, BOOL srgb
)
156 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
157 struct gl_texture
*gl_tex
;
160 TRACE("texture %p, context %p, srgb %#x.\n", texture
, context
, srgb
);
162 if (gl_info
->supported
[EXT_TEXTURE_SRGB_DECODE
])
165 /* sRGB mode cache for preload() calls outside drawprim. */
167 texture
->flags
|= WINED3D_TEXTURE_IS_SRGB
;
169 texture
->flags
&= ~WINED3D_TEXTURE_IS_SRGB
;
171 gl_tex
= wined3d_texture_get_gl_texture(texture
, srgb
);
172 target
= texture
->target
;
176 context_bind_texture(context
, target
, gl_tex
->name
);
180 gl_info
->gl_ops
.gl
.p_glGenTextures(1, &gl_tex
->name
);
181 checkGLcall("glGenTextures");
182 TRACE("Generated texture %d.\n", gl_tex
->name
);
186 ERR("Failed to generate a texture name.\n");
190 if (texture
->resource
.pool
== WINED3D_POOL_DEFAULT
)
192 /* Tell OpenGL to try and keep this texture in video ram (well mostly). */
194 gl_info
->gl_ops
.gl
.p_glPrioritizeTextures(1, &gl_tex
->name
, &tmp
);
197 /* Initialise the state of the texture object to the OpenGL defaults, not
198 * the wined3d defaults. */
199 gl_tex
->sampler_desc
.address_u
= WINED3D_TADDRESS_WRAP
;
200 gl_tex
->sampler_desc
.address_v
= WINED3D_TADDRESS_WRAP
;
201 gl_tex
->sampler_desc
.address_w
= WINED3D_TADDRESS_WRAP
;
202 memset(gl_tex
->sampler_desc
.border_color
, 0, sizeof(gl_tex
->sampler_desc
.border_color
));
203 gl_tex
->sampler_desc
.mag_filter
= WINED3D_TEXF_LINEAR
;
204 gl_tex
->sampler_desc
.min_filter
= WINED3D_TEXF_POINT
; /* GL_NEAREST_MIPMAP_LINEAR */
205 gl_tex
->sampler_desc
.mip_filter
= WINED3D_TEXF_LINEAR
; /* GL_NEAREST_MIPMAP_LINEAR */
206 gl_tex
->sampler_desc
.lod_bias
= 0.0f
;
207 gl_tex
->sampler_desc
.min_lod
= -1000.0f
;
208 gl_tex
->sampler_desc
.max_lod
= 1000.0f
;
209 gl_tex
->sampler_desc
.max_anisotropy
= 1;
210 gl_tex
->sampler_desc
.compare
= FALSE
;
211 gl_tex
->sampler_desc
.comparison_func
= WINED3D_CMP_LESSEQUAL
;
212 if (context
->gl_info
->supported
[EXT_TEXTURE_SRGB_DECODE
])
213 gl_tex
->sampler_desc
.srgb_decode
= TRUE
;
215 gl_tex
->sampler_desc
.srgb_decode
= srgb
;
216 gl_tex
->base_level
= 0;
217 wined3d_texture_set_dirty(texture
);
219 context_bind_texture(context
, target
, gl_tex
->name
);
221 if (texture
->resource
.usage
& WINED3DUSAGE_AUTOGENMIPMAP
)
223 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_GENERATE_MIPMAP_SGIS
, GL_TRUE
);
224 checkGLcall("glTexParameteri(target, GL_GENERATE_MIPMAP_SGIS, GL_TRUE)");
227 /* For a new texture we have to set the texture levels after binding the
228 * texture. Beware that texture rectangles do not support mipmapping, but
229 * set the maxmiplevel if we're relying on the partial
230 * GL_ARB_texture_non_power_of_two emulation with texture rectangles.
231 * (I.e., do not care about cond_np2 here, just look for
232 * GL_TEXTURE_RECTANGLE_ARB.) */
233 if (target
!= GL_TEXTURE_RECTANGLE_ARB
)
235 TRACE("Setting GL_TEXTURE_MAX_LEVEL to %u.\n", texture
->level_count
- 1);
236 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, texture
->level_count
- 1);
237 checkGLcall("glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, texture->level_count)");
240 if (target
== GL_TEXTURE_CUBE_MAP_ARB
)
242 /* Cubemaps are always set to clamp, regardless of the sampler state. */
243 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
244 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
245 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_R
, GL_CLAMP_TO_EDGE
);
248 if (texture
->flags
& WINED3D_TEXTURE_COND_NP2
)
250 /* Conditinal non power of two textures use a different clamping
251 * default. If we're using the GL_WINE_normalized_texrect partial
252 * driver emulation, we're dealing with a GL_TEXTURE_2D texture which
253 * has the address mode set to repeat - something that prevents us
254 * from hitting the accelerated codepath. Thus manually set the GL
255 * state. The same applies to filtering. Even if the texture has only
256 * one mip level, the default LINEAR_MIPMAP_LINEAR filter causes a SW
257 * fallback on macos. */
258 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
259 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
260 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
261 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
262 checkGLcall("glTexParameteri");
263 gl_tex
->sampler_desc
.address_u
= WINED3D_TADDRESS_CLAMP
;
264 gl_tex
->sampler_desc
.address_v
= WINED3D_TADDRESS_CLAMP
;
265 gl_tex
->sampler_desc
.mag_filter
= WINED3D_TEXF_POINT
;
266 gl_tex
->sampler_desc
.min_filter
= WINED3D_TEXF_POINT
;
267 gl_tex
->sampler_desc
.mip_filter
= WINED3D_TEXF_NONE
;
271 /* Context activation is done by the caller. */
272 void wined3d_texture_bind_and_dirtify(struct wined3d_texture
*texture
,
273 struct wined3d_context
*context
, BOOL srgb
)
275 DWORD active_sampler
;
277 /* We don't need a specific texture unit, but after binding the texture
278 * the current unit is dirty. Read the unit back instead of switching to
279 * 0, this avoids messing around with the state manager's GL states. The
280 * current texture unit should always be a valid one.
282 * To be more specific, this is tricky because we can implicitly be
283 * called from sampler() in state.c. This means we can't touch anything
284 * other than whatever happens to be the currently active texture, or we
285 * would risk marking already applied sampler states dirty again. */
286 active_sampler
= context
->rev_tex_unit_map
[context
->active_texture
];
287 if (active_sampler
!= WINED3D_UNMAPPED_STAGE
)
288 context_invalidate_state(context
, STATE_SAMPLER(active_sampler
));
290 wined3d_texture_bind(texture
, context
, srgb
);
293 /* Context activation is done by the caller (state handler). */
294 /* This function relies on the correct texture being bound and loaded. */
295 void wined3d_texture_apply_sampler_desc(struct wined3d_texture
*texture
,
296 const struct wined3d_sampler_desc
*sampler_desc
, const struct wined3d_gl_info
*gl_info
)
298 GLenum target
= texture
->target
;
299 struct gl_texture
*gl_tex
;
302 TRACE("texture %p, sampler_desc %p, gl_info %p.\n", texture
, sampler_desc
, gl_info
);
304 gl_tex
= wined3d_texture_get_gl_texture(texture
, texture
->flags
& WINED3D_TEXTURE_IS_SRGB
);
306 state
= sampler_desc
->address_u
;
307 if (state
!= gl_tex
->sampler_desc
.address_u
)
309 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_S
,
310 gl_info
->wrap_lookup
[state
- WINED3D_TADDRESS_WRAP
]);
311 gl_tex
->sampler_desc
.address_u
= state
;
314 state
= sampler_desc
->address_v
;
315 if (state
!= gl_tex
->sampler_desc
.address_v
)
317 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_T
,
318 gl_info
->wrap_lookup
[state
- WINED3D_TADDRESS_WRAP
]);
319 gl_tex
->sampler_desc
.address_v
= state
;
322 state
= sampler_desc
->address_w
;
323 if (state
!= gl_tex
->sampler_desc
.address_w
)
325 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_R
,
326 gl_info
->wrap_lookup
[state
- WINED3D_TADDRESS_WRAP
]);
327 gl_tex
->sampler_desc
.address_w
= state
;
330 if (memcmp(gl_tex
->sampler_desc
.border_color
, sampler_desc
->border_color
,
331 sizeof(gl_tex
->sampler_desc
.border_color
)))
333 gl_info
->gl_ops
.gl
.p_glTexParameterfv(target
, GL_TEXTURE_BORDER_COLOR
, &sampler_desc
->border_color
[0]);
334 memcpy(gl_tex
->sampler_desc
.border_color
, sampler_desc
->border_color
,
335 sizeof(gl_tex
->sampler_desc
.border_color
));
338 state
= sampler_desc
->mag_filter
;
339 if (state
!= gl_tex
->sampler_desc
.mag_filter
)
341 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MAG_FILTER
, wined3d_gl_mag_filter(state
));
342 gl_tex
->sampler_desc
.mag_filter
= state
;
345 if (sampler_desc
->min_filter
!= gl_tex
->sampler_desc
.min_filter
346 || sampler_desc
->mip_filter
!= gl_tex
->sampler_desc
.mip_filter
)
348 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MIN_FILTER
,
349 wined3d_gl_min_mip_filter(sampler_desc
->min_filter
, sampler_desc
->mip_filter
));
350 gl_tex
->sampler_desc
.min_filter
= sampler_desc
->min_filter
;
351 gl_tex
->sampler_desc
.mip_filter
= sampler_desc
->mip_filter
;
354 state
= sampler_desc
->max_anisotropy
;
355 if (state
!= gl_tex
->sampler_desc
.max_anisotropy
)
357 if (gl_info
->supported
[EXT_TEXTURE_FILTER_ANISOTROPIC
])
358 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MAX_ANISOTROPY_EXT
, state
);
360 WARN("Anisotropic filtering not supported.\n");
361 gl_tex
->sampler_desc
.max_anisotropy
= state
;
364 /* These should always be the same unless EXT_texture_sRGB_decode is supported. */
365 if (!sampler_desc
->srgb_decode
!= !gl_tex
->sampler_desc
.srgb_decode
)
367 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_SRGB_DECODE_EXT
,
368 sampler_desc
->srgb_decode
? GL_DECODE_EXT
: GL_SKIP_DECODE_EXT
);
369 gl_tex
->sampler_desc
.srgb_decode
= sampler_desc
->srgb_decode
;
372 if (!sampler_desc
->compare
!= !gl_tex
->sampler_desc
.compare
)
374 if (sampler_desc
->compare
)
376 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_DEPTH_TEXTURE_MODE_ARB
, GL_LUMINANCE
);
377 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_COMPARE_MODE_ARB
, GL_COMPARE_R_TO_TEXTURE_ARB
);
381 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_COMPARE_MODE_ARB
, GL_NONE
);
383 gl_tex
->sampler_desc
.compare
= sampler_desc
->compare
;
386 checkGLcall("Texture parameter application");
388 if (gl_info
->supported
[EXT_TEXTURE_LOD_BIAS
])
390 gl_info
->gl_ops
.gl
.p_glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT
,
391 GL_TEXTURE_LOD_BIAS_EXT
, sampler_desc
->lod_bias
);
392 checkGLcall("glTexEnvf(GL_TEXTURE_LOD_BIAS_EXT, ...)");
396 ULONG CDECL
wined3d_texture_incref(struct wined3d_texture
*texture
)
400 TRACE("texture %p, swapchain %p.\n", texture
, texture
->swapchain
);
402 if (texture
->swapchain
)
403 return wined3d_swapchain_incref(texture
->swapchain
);
405 refcount
= InterlockedIncrement(&texture
->resource
.ref
);
406 TRACE("%p increasing refcount to %u.\n", texture
, refcount
);
411 ULONG CDECL
wined3d_texture_decref(struct wined3d_texture
*texture
)
415 TRACE("texture %p, swapchain %p.\n", texture
, texture
->swapchain
);
417 if (texture
->swapchain
)
418 return wined3d_swapchain_decref(texture
->swapchain
);
420 refcount
= InterlockedDecrement(&texture
->resource
.ref
);
421 TRACE("%p decreasing refcount to %u.\n", texture
, refcount
);
425 wined3d_texture_cleanup(texture
);
426 texture
->resource
.parent_ops
->wined3d_object_destroyed(texture
->resource
.parent
);
427 HeapFree(GetProcessHeap(), 0, texture
);
433 struct wined3d_resource
* CDECL
wined3d_texture_get_resource(struct wined3d_texture
*texture
)
435 TRACE("texture %p.\n", texture
);
437 return &texture
->resource
;
440 static BOOL
color_key_equal(const struct wined3d_color_key
*c1
, struct wined3d_color_key
*c2
)
442 return c1
->color_space_low_value
== c2
->color_space_low_value
443 && c1
->color_space_high_value
== c2
->color_space_high_value
;
446 /* Context activation is done by the caller */
447 void wined3d_texture_load(struct wined3d_texture
*texture
,
448 struct wined3d_context
*context
, BOOL srgb
)
450 UINT sub_count
= texture
->level_count
* texture
->layer_count
;
451 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
452 const struct wined3d_d3d_info
*d3d_info
= context
->d3d_info
;
456 TRACE("texture %p, srgb %#x.\n", texture
, srgb
);
458 if (gl_info
->supported
[EXT_TEXTURE_SRGB_DECODE
])
462 flag
= WINED3D_TEXTURE_SRGB_VALID
;
464 flag
= WINED3D_TEXTURE_RGB_VALID
;
466 if (!d3d_info
->shader_color_key
467 && (!(texture
->async
.flags
& WINED3D_TEXTURE_ASYNC_COLOR_KEY
)
468 != !(texture
->async
.color_key_flags
& WINED3D_CKEY_SRC_BLT
)
469 || (texture
->async
.flags
& WINED3D_TEXTURE_ASYNC_COLOR_KEY
470 && !color_key_equal(&texture
->async
.gl_color_key
, &texture
->async
.src_blt_color_key
))))
472 unsigned int sub_count
= texture
->level_count
* texture
->layer_count
;
475 TRACE("Reloading because of color key value change.\n");
476 for (i
= 0; i
< sub_count
; i
++)
477 texture
->texture_ops
->texture_sub_resource_add_dirty_region(texture
->sub_resources
[i
], NULL
);
478 wined3d_texture_set_dirty(texture
);
480 texture
->async
.gl_color_key
= texture
->async
.src_blt_color_key
;
483 if (texture
->flags
& flag
)
485 TRACE("Texture %p not dirty, nothing to do.\n", texture
);
489 /* Reload the surfaces if the texture is marked dirty. */
490 for (i
= 0; i
< sub_count
; ++i
)
492 texture
->texture_ops
->texture_sub_resource_load(texture
->sub_resources
[i
], context
, srgb
);
494 texture
->flags
|= flag
;
497 void CDECL
wined3d_texture_preload(struct wined3d_texture
*texture
)
499 struct wined3d_context
*context
;
500 context
= context_acquire(texture
->resource
.device
, NULL
);
501 wined3d_texture_load(texture
, context
, texture
->flags
& WINED3D_TEXTURE_IS_SRGB
);
502 context_release(context
);
505 void * CDECL
wined3d_texture_get_parent(const struct wined3d_texture
*texture
)
507 TRACE("texture %p.\n", texture
);
509 return texture
->resource
.parent
;
512 DWORD CDECL
wined3d_texture_set_lod(struct wined3d_texture
*texture
, DWORD lod
)
514 DWORD old
= texture
->lod
;
516 TRACE("texture %p, lod %u.\n", texture
, lod
);
518 /* The d3d9:texture test shows that SetLOD is ignored on non-managed
519 * textures. The call always returns 0, and GetLOD always returns 0. */
520 if (texture
->resource
.pool
!= WINED3D_POOL_MANAGED
)
522 TRACE("Ignoring SetLOD on %s texture, returning 0.\n", debug_d3dpool(texture
->resource
.pool
));
526 if (lod
>= texture
->level_count
)
527 lod
= texture
->level_count
- 1;
529 if (texture
->lod
!= lod
)
533 texture
->texture_rgb
.base_level
= ~0u;
534 texture
->texture_srgb
.base_level
= ~0u;
535 if (texture
->resource
.bind_count
)
536 device_invalidate_state(texture
->resource
.device
, STATE_SAMPLER(texture
->sampler
));
542 DWORD CDECL
wined3d_texture_get_lod(const struct wined3d_texture
*texture
)
544 TRACE("texture %p, returning %u.\n", texture
, texture
->lod
);
549 DWORD CDECL
wined3d_texture_get_level_count(const struct wined3d_texture
*texture
)
551 TRACE("texture %p, returning %u.\n", texture
, texture
->level_count
);
553 return texture
->level_count
;
556 HRESULT CDECL
wined3d_texture_set_autogen_filter_type(struct wined3d_texture
*texture
,
557 enum wined3d_texture_filter_type filter_type
)
559 FIXME("texture %p, filter_type %s stub!\n", texture
, debug_d3dtexturefiltertype(filter_type
));
561 if (!(texture
->resource
.usage
& WINED3DUSAGE_AUTOGENMIPMAP
))
563 WARN("Texture doesn't have AUTOGENMIPMAP usage.\n");
564 return WINED3DERR_INVALIDCALL
;
567 texture
->filter_type
= filter_type
;
572 enum wined3d_texture_filter_type CDECL
wined3d_texture_get_autogen_filter_type(const struct wined3d_texture
*texture
)
574 TRACE("texture %p.\n", texture
);
576 return texture
->filter_type
;
579 HRESULT CDECL
wined3d_texture_set_color_key(struct wined3d_texture
*texture
,
580 DWORD flags
, const struct wined3d_color_key
*color_key
)
582 struct wined3d_device
*device
= texture
->resource
.device
;
583 static const DWORD all_flags
= WINED3D_CKEY_COLORSPACE
| WINED3D_CKEY_DST_BLT
584 | WINED3D_CKEY_DST_OVERLAY
| WINED3D_CKEY_SRC_BLT
| WINED3D_CKEY_SRC_OVERLAY
;
586 TRACE("texture %p, flags %#x, color_key %p.\n", texture
, flags
, color_key
);
588 if (flags
& ~all_flags
)
590 WARN("Invalid flags passed, returning WINED3DERR_INVALIDCALL.\n");
591 return WINED3DERR_INVALIDCALL
;
594 if (flags
& WINED3D_CKEY_COLORSPACE
)
596 FIXME("Unhandled flags %#x.\n", flags
);
597 return WINED3DERR_INVALIDCALL
;
600 wined3d_cs_emit_set_color_key(device
->cs
, texture
, flags
, color_key
);
605 HRESULT CDECL
wined3d_texture_update_desc(struct wined3d_texture
*texture
, UINT width
, UINT height
,
606 enum wined3d_format_id format_id
, enum wined3d_multisample_type multisample_type
,
607 UINT multisample_quality
, void *mem
, UINT pitch
)
609 struct wined3d_device
*device
= texture
->resource
.device
;
610 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
611 const struct wined3d_format
*format
= wined3d_get_format(gl_info
, format_id
);
612 UINT resource_size
= wined3d_format_calculate_size(format
, device
->surface_alignment
, width
, height
, 1);
613 struct wined3d_surface
*surface
;
615 TRACE("texture %p, width %u, height %u, format %s, multisample_type %#x, multisample_quality %u, "
616 "mem %p, pitch %u.\n",
617 texture
, width
, height
, debug_d3dformat(format_id
), multisample_type
, multisample_type
, mem
, pitch
);
620 return WINED3DERR_INVALIDCALL
;
622 if (texture
->level_count
* texture
->layer_count
> 1)
624 WARN("Texture has multiple sub-resources, not supported.\n");
625 return WINED3DERR_INVALIDCALL
;
628 if (texture
->resource
.type
== WINED3D_RTYPE_VOLUME_TEXTURE
)
630 WARN("Not supported on volume textures.\n");
631 return WINED3DERR_INVALIDCALL
;
634 /* We have no way of supporting a pitch that is not a multiple of the pixel
635 * byte width short of uploading the texture row-by-row.
636 * Fortunately that's not an issue since D3D9Ex doesn't allow a custom pitch
637 * for user-memory textures (it always expects packed data) while DirectDraw
638 * requires a 4-byte aligned pitch and doesn't support texture formats
639 * larger than 4 bytes per pixel nor any format using 3 bytes per pixel.
640 * This check is here to verify that the assumption holds. */
641 if (pitch
% texture
->resource
.format
->byte_count
)
643 WARN("Pitch unsupported, not a multiple of the texture format byte width.\n");
644 return WINED3DERR_INVALIDCALL
;
647 surface
= surface_from_resource(texture
->sub_resources
[0]);
648 if (surface
->resource
.map_count
|| (surface
->flags
& SFLAG_DCINUSE
))
650 WARN("Surface is mapped or the DC is in use.\n");
651 return WINED3DERR_INVALIDCALL
;
654 if (device
->d3d_initialized
)
655 texture
->resource
.resource_ops
->resource_unload(&texture
->resource
);
657 texture
->resource
.format
= format
;
658 texture
->resource
.multisample_type
= multisample_type
;
659 texture
->resource
.multisample_quality
= multisample_quality
;
660 texture
->resource
.width
= width
;
661 texture
->resource
.height
= height
;
663 return wined3d_surface_update_desc(surface
, gl_info
, mem
, pitch
);
666 void wined3d_texture_prepare_texture(struct wined3d_texture
*texture
, struct wined3d_context
*context
, BOOL srgb
)
668 DWORD alloc_flag
= srgb
? WINED3D_TEXTURE_SRGB_ALLOCATED
: WINED3D_TEXTURE_RGB_ALLOCATED
;
669 const struct wined3d_d3d_info
*d3d_info
= context
->d3d_info
;
671 if (!d3d_info
->shader_color_key
672 && !(texture
->async
.flags
& WINED3D_TEXTURE_ASYNC_COLOR_KEY
)
673 != !(texture
->async
.color_key_flags
& WINED3D_CKEY_SRC_BLT
))
675 wined3d_texture_force_reload(texture
);
677 if (texture
->async
.color_key_flags
& WINED3D_CKEY_SRC_BLT
)
678 texture
->async
.flags
|= WINED3D_TEXTURE_ASYNC_COLOR_KEY
;
681 if (texture
->flags
& alloc_flag
)
684 texture
->texture_ops
->texture_prepare_texture(texture
, context
, srgb
);
685 texture
->flags
|= alloc_flag
;
688 void wined3d_texture_force_reload(struct wined3d_texture
*texture
)
690 unsigned int sub_count
= texture
->level_count
* texture
->layer_count
;
693 texture
->flags
&= ~(WINED3D_TEXTURE_RGB_ALLOCATED
| WINED3D_TEXTURE_SRGB_ALLOCATED
694 | WINED3D_TEXTURE_CONVERTED
);
695 texture
->async
.flags
&= ~WINED3D_TEXTURE_ASYNC_COLOR_KEY
;
696 for (i
= 0; i
< sub_count
; ++i
)
698 texture
->texture_ops
->texture_sub_resource_invalidate_location(texture
->sub_resources
[i
],
699 WINED3D_LOCATION_TEXTURE_RGB
| WINED3D_LOCATION_TEXTURE_SRGB
);
703 void CDECL
wined3d_texture_generate_mipmaps(struct wined3d_texture
*texture
)
705 /* TODO: Implement filters using GL_SGI_generate_mipmaps. */
706 FIXME("texture %p stub!\n", texture
);
709 struct wined3d_resource
* CDECL
wined3d_texture_get_sub_resource(struct wined3d_texture
*texture
,
710 UINT sub_resource_idx
)
712 UINT sub_count
= texture
->level_count
* texture
->layer_count
;
714 TRACE("texture %p, sub_resource_idx %u.\n", texture
, sub_resource_idx
);
716 if (sub_resource_idx
>= sub_count
)
718 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx
, sub_count
);
722 return texture
->sub_resources
[sub_resource_idx
];
725 HRESULT CDECL
wined3d_texture_add_dirty_region(struct wined3d_texture
*texture
,
726 UINT layer
, const struct wined3d_box
*dirty_region
)
728 struct wined3d_resource
*sub_resource
;
730 TRACE("texture %p, layer %u, dirty_region %p.\n", texture
, layer
, dirty_region
);
732 if (!(sub_resource
= wined3d_texture_get_sub_resource(texture
, layer
* texture
->level_count
)))
734 WARN("Failed to get sub-resource.\n");
735 return WINED3DERR_INVALIDCALL
;
738 texture
->texture_ops
->texture_sub_resource_add_dirty_region(sub_resource
, dirty_region
);
743 static void wined3d_texture_upload_data(struct wined3d_texture
*texture
, const struct wined3d_sub_resource_data
*data
)
745 unsigned int sub_count
= texture
->level_count
* texture
->layer_count
;
746 struct wined3d_context
*context
;
749 context
= context_acquire(texture
->resource
.device
, NULL
);
751 wined3d_texture_prepare_texture(texture
, context
, FALSE
);
752 wined3d_texture_bind(texture
, context
, FALSE
);
754 for (i
= 0; i
< sub_count
; ++i
)
756 struct wined3d_resource
*sub_resource
= texture
->sub_resources
[i
];
758 texture
->texture_ops
->texture_sub_resource_upload_data(sub_resource
, context
, &data
[i
]);
759 texture
->texture_ops
->texture_sub_resource_validate_location(sub_resource
, WINED3D_LOCATION_TEXTURE_RGB
);
760 texture
->texture_ops
->texture_sub_resource_invalidate_location(sub_resource
, ~WINED3D_LOCATION_TEXTURE_RGB
);
763 context_release(context
);
766 static void texture2d_sub_resource_load(struct wined3d_resource
*sub_resource
,
767 struct wined3d_context
*context
, BOOL srgb
)
769 surface_load(surface_from_resource(sub_resource
), srgb
);
772 static void texture2d_sub_resource_add_dirty_region(struct wined3d_resource
*sub_resource
,
773 const struct wined3d_box
*dirty_region
)
775 struct wined3d_surface
*surface
= surface_from_resource(sub_resource
);
777 surface_prepare_map_memory(surface
);
778 surface_load_location(surface
, surface
->resource
.map_binding
);
779 surface_invalidate_location(surface
, ~surface
->resource
.map_binding
);
782 static void texture2d_sub_resource_cleanup(struct wined3d_resource
*sub_resource
)
784 struct wined3d_surface
*surface
= surface_from_resource(sub_resource
);
786 wined3d_surface_destroy(surface
);
789 static void texture2d_sub_resource_invalidate_location(struct wined3d_resource
*sub_resource
, DWORD location
)
791 struct wined3d_surface
*surface
= surface_from_resource(sub_resource
);
793 surface_invalidate_location(surface
, location
);
796 static void texture2d_sub_resource_validate_location(struct wined3d_resource
*sub_resource
, DWORD location
)
798 struct wined3d_surface
*surface
= surface_from_resource(sub_resource
);
800 surface_validate_location(surface
, location
);
803 static void texture2d_sub_resource_upload_data(struct wined3d_resource
*sub_resource
,
804 const struct wined3d_context
*context
, const struct wined3d_sub_resource_data
*data
)
806 struct wined3d_surface
*surface
= surface_from_resource(sub_resource
);
807 static const POINT dst_point
= {0, 0};
808 struct wined3d_const_bo_address addr
;
813 src_rect
.right
= surface
->resource
.width
;
814 src_rect
.bottom
= surface
->resource
.height
;
816 addr
.buffer_object
= 0;
817 addr
.addr
= data
->data
;
819 wined3d_surface_upload_data(surface
, context
->gl_info
, surface
->container
->resource
.format
,
820 &src_rect
, data
->row_pitch
, &dst_point
, FALSE
, &addr
);
823 /* Context activation is done by the caller. */
824 static void texture2d_prepare_texture(struct wined3d_texture
*texture
, struct wined3d_context
*context
, BOOL srgb
)
826 UINT sub_count
= texture
->level_count
* texture
->layer_count
;
827 const struct wined3d_format
*format
= texture
->resource
.format
;
828 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
829 const struct wined3d_color_key_conversion
*conversion
;
833 TRACE("texture %p, format %s.\n", texture
, debug_d3dformat(format
->id
));
837 texture
->flags
|= WINED3D_TEXTURE_CONVERTED
;
839 else if ((conversion
= wined3d_format_get_color_key_conversion(texture
, TRUE
)))
841 texture
->flags
|= WINED3D_TEXTURE_CONVERTED
;
842 format
= wined3d_get_format(gl_info
, conversion
->dst_format
);
843 TRACE("Using format %s for color key conversion.\n", debug_d3dformat(format
->id
));
846 wined3d_texture_bind_and_dirtify(texture
, context
, srgb
);
849 internal
= format
->glGammaInternal
;
850 else if (texture
->resource
.usage
& WINED3DUSAGE_RENDERTARGET
851 && wined3d_resource_is_offscreen(&texture
->resource
))
852 internal
= format
->rtInternal
;
854 internal
= format
->glInternal
;
857 FIXME("No GL internal format for format %s.\n", debug_d3dformat(format
->id
));
859 TRACE("internal %#x, format %#x, type %#x.\n", internal
, format
->glFormat
, format
->glType
);
861 for (i
= 0; i
< sub_count
; ++i
)
863 struct wined3d_surface
*surface
= surface_from_resource(texture
->sub_resources
[i
]);
864 GLsizei height
= surface
->pow2Height
;
865 GLsizei width
= surface
->pow2Width
;
866 const BYTE
*mem
= NULL
;
868 if (format
->flags
& WINED3DFMT_FLAG_HEIGHT_SCALE
)
870 height
*= format
->height_scale
.numerator
;
871 height
/= format
->height_scale
.denominator
;
874 TRACE("surface %p, target %#x, level %d, width %d, height %d.\n",
875 surface
, surface
->texture_target
, surface
->texture_level
, width
, height
);
877 if (gl_info
->supported
[APPLE_CLIENT_STORAGE
])
879 if (surface
->flags
& (SFLAG_NONPOW2
| SFLAG_DIBSECTION
)
880 || texture
->flags
& WINED3D_TEXTURE_CONVERTED
881 || !surface
->resource
.heap_memory
)
883 /* In some cases we want to disable client storage.
884 * SFLAG_NONPOW2 has a bigger opengl texture than the client memory, and different pitches
885 * SFLAG_DIBSECTION: Dibsections may have read / write protections on the memory. Avoid issues...
886 * WINED3D_TEXTURE_CONVERTED: The conversion destination memory is freed after loading the surface
887 * heap_memory == NULL: Not defined in the extension. Seems to disable client storage effectively
889 surface
->flags
&= ~SFLAG_CLIENT
;
893 surface
->flags
|= SFLAG_CLIENT
;
894 mem
= surface
->resource
.heap_memory
;
896 gl_info
->gl_ops
.gl
.p_glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE
, GL_TRUE
);
897 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE)");
901 if (format
->flags
& WINED3DFMT_FLAG_COMPRESSED
&& mem
)
903 GL_EXTCALL(glCompressedTexImage2D(surface
->texture_target
, surface
->texture_level
,
904 internal
, width
, height
, 0, surface
->resource
.size
, mem
));
905 checkGLcall("glCompressedTexImage2D");
909 gl_info
->gl_ops
.gl
.p_glTexImage2D(surface
->texture_target
, surface
->texture_level
,
910 internal
, width
, height
, 0, format
->glFormat
, format
->glType
, mem
);
911 checkGLcall("glTexImage2D");
916 gl_info
->gl_ops
.gl
.p_glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE
, GL_FALSE
);
917 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE)");
922 static const struct wined3d_texture_ops texture2d_ops
=
924 texture2d_sub_resource_load
,
925 texture2d_sub_resource_add_dirty_region
,
926 texture2d_sub_resource_cleanup
,
927 texture2d_sub_resource_invalidate_location
,
928 texture2d_sub_resource_validate_location
,
929 texture2d_sub_resource_upload_data
,
930 texture2d_prepare_texture
,
933 static ULONG
texture_resource_incref(struct wined3d_resource
*resource
)
935 return wined3d_texture_incref(wined3d_texture_from_resource(resource
));
938 static ULONG
texture_resource_decref(struct wined3d_resource
*resource
)
940 return wined3d_texture_decref(wined3d_texture_from_resource(resource
));
943 static void wined3d_texture_unload(struct wined3d_resource
*resource
)
945 struct wined3d_texture
*texture
= wined3d_texture_from_resource(resource
);
946 UINT sub_count
= texture
->level_count
* texture
->layer_count
;
949 TRACE("texture %p.\n", texture
);
951 for (i
= 0; i
< sub_count
; ++i
)
953 struct wined3d_resource
*sub_resource
= texture
->sub_resources
[i
];
955 sub_resource
->resource_ops
->resource_unload(sub_resource
);
958 wined3d_texture_force_reload(texture
);
959 wined3d_texture_unload_gl_texture(texture
);
962 static const struct wined3d_resource_ops texture_resource_ops
=
964 texture_resource_incref
,
965 texture_resource_decref
,
966 wined3d_texture_unload
,
969 static HRESULT
cubetexture_init(struct wined3d_texture
*texture
, const struct wined3d_resource_desc
*desc
,
970 UINT levels
, DWORD surface_flags
, struct wined3d_device
*device
, void *parent
,
971 const struct wined3d_parent_ops
*parent_ops
)
973 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
974 struct wined3d_resource_desc surface_desc
;
978 /* TODO: It should only be possible to create textures for formats
979 * that are reported as supported. */
980 if (WINED3DFMT_UNKNOWN
>= desc
->format
)
982 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture
);
983 return WINED3DERR_INVALIDCALL
;
986 if (!gl_info
->supported
[ARB_TEXTURE_CUBE_MAP
] && desc
->pool
!= WINED3D_POOL_SCRATCH
)
988 WARN("(%p) : Tried to create not supported cube texture.\n", texture
);
989 return WINED3DERR_INVALIDCALL
;
992 /* Calculate levels for mip mapping */
993 if (desc
->usage
& WINED3DUSAGE_AUTOGENMIPMAP
)
995 if (!gl_info
->supported
[SGIS_GENERATE_MIPMAP
])
997 WARN("No mipmap generation support, returning D3DERR_INVALIDCALL.\n");
998 return WINED3DERR_INVALIDCALL
;
1003 WARN("WINED3DUSAGE_AUTOGENMIPMAP is set, and level count != 1, returning D3DERR_INVALIDCALL.\n");
1004 return WINED3DERR_INVALIDCALL
;
1008 if (!gl_info
->supported
[ARB_TEXTURE_NON_POWER_OF_TWO
])
1010 UINT pow2_edge_length
= 1;
1011 while (pow2_edge_length
< desc
->width
)
1012 pow2_edge_length
<<= 1;
1014 if (desc
->width
!= pow2_edge_length
)
1016 if (desc
->pool
== WINED3D_POOL_SCRATCH
)
1018 /* SCRATCH textures cannot be used for texturing */
1019 WARN("Creating a scratch NPOT cube texture despite lack of HW support.\n");
1023 WARN("Attempted to create a NPOT cube texture (edge length %u) without GL support.\n", desc
->width
);
1024 return WINED3DERR_INVALIDCALL
;
1029 if (FAILED(hr
= wined3d_texture_init(texture
, &texture2d_ops
, 6, levels
, desc
,
1030 surface_flags
, device
, parent
, parent_ops
, &texture_resource_ops
)))
1032 WARN("Failed to initialize texture, returning %#x\n", hr
);
1036 texture
->pow2_matrix
[0] = 1.0f
;
1037 texture
->pow2_matrix
[5] = 1.0f
;
1038 texture
->pow2_matrix
[10] = 1.0f
;
1039 texture
->pow2_matrix
[15] = 1.0f
;
1040 texture
->target
= GL_TEXTURE_CUBE_MAP_ARB
;
1042 /* Generate all the surfaces. */
1043 surface_desc
= *desc
;
1044 surface_desc
.resource_type
= WINED3D_RTYPE_SURFACE
;
1045 for (i
= 0; i
< texture
->level_count
; ++i
)
1047 /* Create the 6 faces. */
1048 for (j
= 0; j
< 6; ++j
)
1050 static const GLenum cube_targets
[6] =
1052 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
,
1053 GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
,
1054 GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
,
1055 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
,
1056 GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
,
1057 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
,
1059 UINT idx
= j
* texture
->level_count
+ i
;
1060 struct wined3d_surface
*surface
;
1062 if (FAILED(hr
= wined3d_surface_create(texture
, &surface_desc
,
1063 cube_targets
[j
], i
, j
, surface_flags
, &surface
)))
1065 WARN("Failed to create surface, hr %#x.\n", hr
);
1066 wined3d_texture_cleanup(texture
);
1070 texture
->sub_resources
[idx
] = &surface
->resource
;
1071 TRACE("Created surface level %u @ %p.\n", i
, surface
);
1073 surface_desc
.width
= max(1, surface_desc
.width
>> 1);
1074 surface_desc
.height
= surface_desc
.width
;
1080 static HRESULT
texture_init(struct wined3d_texture
*texture
, const struct wined3d_resource_desc
*desc
,
1081 UINT levels
, DWORD surface_flags
, struct wined3d_device
*device
, void *parent
,
1082 const struct wined3d_parent_ops
*parent_ops
)
1084 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
1085 struct wined3d_resource_desc surface_desc
;
1086 UINT pow2_width
, pow2_height
;
1090 /* TODO: It should only be possible to create textures for formats
1091 * that are reported as supported. */
1092 if (WINED3DFMT_UNKNOWN
>= desc
->format
)
1094 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture
);
1095 return WINED3DERR_INVALIDCALL
;
1098 /* Non-power2 support. */
1099 if (gl_info
->supported
[ARB_TEXTURE_NON_POWER_OF_TWO
])
1101 pow2_width
= desc
->width
;
1102 pow2_height
= desc
->height
;
1106 /* Find the nearest pow2 match. */
1107 pow2_width
= pow2_height
= 1;
1108 while (pow2_width
< desc
->width
)
1110 while (pow2_height
< desc
->height
)
1113 if (pow2_width
!= desc
->width
|| pow2_height
!= desc
->height
)
1115 /* levels == 0 returns an error as well */
1118 if (desc
->pool
== WINED3D_POOL_SCRATCH
)
1120 WARN("Creating a scratch mipmapped NPOT texture despite lack of HW support.\n");
1124 WARN("Attempted to create a mipmapped NPOT texture without unconditional NPOT support.\n");
1125 return WINED3DERR_INVALIDCALL
;
1131 /* Calculate levels for mip mapping. */
1132 if (desc
->usage
& WINED3DUSAGE_AUTOGENMIPMAP
)
1134 if (!gl_info
->supported
[SGIS_GENERATE_MIPMAP
])
1136 WARN("No mipmap generation support, returning WINED3DERR_INVALIDCALL.\n");
1137 return WINED3DERR_INVALIDCALL
;
1142 WARN("WINED3DUSAGE_AUTOGENMIPMAP is set, and level count != 1, returning WINED3DERR_INVALIDCALL.\n");
1143 return WINED3DERR_INVALIDCALL
;
1147 if (FAILED(hr
= wined3d_texture_init(texture
, &texture2d_ops
, 1, levels
, desc
,
1148 surface_flags
, device
, parent
, parent_ops
, &texture_resource_ops
)))
1150 WARN("Failed to initialize texture, returning %#x.\n", hr
);
1154 /* Precalculated scaling for 'faked' non power of two texture coords. */
1155 if (gl_info
->supported
[WINED3D_GL_NORMALIZED_TEXRECT
]
1156 && (desc
->width
!= pow2_width
|| desc
->height
!= pow2_height
))
1158 texture
->pow2_matrix
[0] = 1.0f
;
1159 texture
->pow2_matrix
[5] = 1.0f
;
1160 texture
->pow2_matrix
[10] = 1.0f
;
1161 texture
->pow2_matrix
[15] = 1.0f
;
1162 texture
->target
= GL_TEXTURE_2D
;
1163 texture
->flags
|= WINED3D_TEXTURE_COND_NP2
;
1165 else if (gl_info
->supported
[ARB_TEXTURE_RECTANGLE
]
1166 && (desc
->width
!= pow2_width
|| desc
->height
!= pow2_height
))
1168 texture
->pow2_matrix
[0] = (float)desc
->width
;
1169 texture
->pow2_matrix
[5] = (float)desc
->height
;
1170 texture
->pow2_matrix
[10] = 1.0f
;
1171 texture
->pow2_matrix
[15] = 1.0f
;
1172 texture
->target
= GL_TEXTURE_RECTANGLE_ARB
;
1173 texture
->flags
|= WINED3D_TEXTURE_COND_NP2
;
1174 texture
->flags
&= ~(WINED3D_TEXTURE_POW2_MAT_IDENT
| WINED3D_TEXTURE_NORMALIZED_COORDS
);
1178 if ((desc
->width
!= pow2_width
) || (desc
->height
!= pow2_height
))
1180 texture
->pow2_matrix
[0] = (((float)desc
->width
) / ((float)pow2_width
));
1181 texture
->pow2_matrix
[5] = (((float)desc
->height
) / ((float)pow2_height
));
1182 texture
->flags
&= ~WINED3D_TEXTURE_POW2_MAT_IDENT
;
1186 texture
->pow2_matrix
[0] = 1.0f
;
1187 texture
->pow2_matrix
[5] = 1.0f
;
1190 texture
->pow2_matrix
[10] = 1.0f
;
1191 texture
->pow2_matrix
[15] = 1.0f
;
1192 texture
->target
= GL_TEXTURE_2D
;
1194 TRACE("xf(%f) yf(%f)\n", texture
->pow2_matrix
[0], texture
->pow2_matrix
[5]);
1196 /* Generate all the surfaces. */
1197 surface_desc
= *desc
;
1198 surface_desc
.resource_type
= WINED3D_RTYPE_SURFACE
;
1199 for (i
= 0; i
< texture
->level_count
; ++i
)
1201 struct wined3d_surface
*surface
;
1203 if (FAILED(hr
= wined3d_surface_create(texture
, &surface_desc
,
1204 texture
->target
, i
, 0, surface_flags
, &surface
)))
1206 WARN("Failed to create surface, hr %#x.\n", hr
);
1207 wined3d_texture_cleanup(texture
);
1211 texture
->sub_resources
[i
] = &surface
->resource
;
1212 TRACE("Created surface level %u @ %p.\n", i
, surface
);
1213 /* Calculate the next mipmap level. */
1214 surface_desc
.width
= max(1, surface_desc
.width
>> 1);
1215 surface_desc
.height
= max(1, surface_desc
.height
>> 1);
1221 static void texture3d_sub_resource_load(struct wined3d_resource
*sub_resource
,
1222 struct wined3d_context
*context
, BOOL srgb
)
1224 wined3d_volume_load(volume_from_resource(sub_resource
), context
, srgb
);
1227 static void texture3d_sub_resource_add_dirty_region(struct wined3d_resource
*sub_resource
,
1228 const struct wined3d_box
*dirty_region
)
1230 wined3d_texture_set_dirty(volume_from_resource(sub_resource
)->container
);
1233 static void texture3d_sub_resource_cleanup(struct wined3d_resource
*sub_resource
)
1235 struct wined3d_volume
*volume
= volume_from_resource(sub_resource
);
1237 wined3d_volume_destroy(volume
);
1240 static void texture3d_sub_resource_invalidate_location(struct wined3d_resource
*sub_resource
, DWORD location
)
1242 struct wined3d_volume
*volume
= volume_from_resource(sub_resource
);
1244 wined3d_volume_invalidate_location(volume
, location
);
1247 static void texture3d_sub_resource_validate_location(struct wined3d_resource
*sub_resource
, DWORD location
)
1249 struct wined3d_volume
*volume
= volume_from_resource(sub_resource
);
1251 wined3d_volume_validate_location(volume
, location
);
1254 static void texture3d_sub_resource_upload_data(struct wined3d_resource
*sub_resource
,
1255 const struct wined3d_context
*context
, const struct wined3d_sub_resource_data
*data
)
1257 struct wined3d_volume
*volume
= volume_from_resource(sub_resource
);
1258 struct wined3d_const_bo_address addr
;
1259 unsigned int row_pitch
, slice_pitch
;
1261 wined3d_volume_get_pitch(volume
, &row_pitch
, &slice_pitch
);
1262 if (row_pitch
!= data
->row_pitch
|| slice_pitch
!= data
->slice_pitch
)
1263 FIXME("Ignoring row/slice pitch (%u/%u).\n", data
->row_pitch
, data
->slice_pitch
);
1265 addr
.buffer_object
= 0;
1266 addr
.addr
= data
->data
;
1268 wined3d_volume_upload_data(volume
, context
, &addr
);
1271 static void texture3d_prepare_texture(struct wined3d_texture
*texture
, struct wined3d_context
*context
, BOOL srgb
)
1273 unsigned int sub_count
= texture
->level_count
* texture
->layer_count
;
1274 const struct wined3d_format
*format
= texture
->resource
.format
;
1275 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1278 wined3d_texture_bind_and_dirtify(texture
, context
, srgb
);
1280 for (i
= 0; i
< sub_count
; ++i
)
1282 struct wined3d_volume
*volume
= volume_from_resource(texture
->sub_resources
[i
]);
1285 if (gl_info
->supported
[APPLE_CLIENT_STORAGE
] && !format
->convert
1286 && volume_prepare_system_memory(volume
))
1288 TRACE("Enabling GL_UNPACK_CLIENT_STORAGE_APPLE for volume %p\n", volume
);
1289 gl_info
->gl_ops
.gl
.p_glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE
, GL_TRUE
);
1290 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE)");
1291 mem
= volume
->resource
.heap_memory
;
1292 volume
->flags
|= WINED3D_VFLAG_CLIENT_STORAGE
;
1295 GL_EXTCALL(glTexImage3D(GL_TEXTURE_3D
, volume
->texture_level
,
1296 srgb
? format
->glGammaInternal
: format
->glInternal
,
1297 volume
->resource
.width
, volume
->resource
.height
, volume
->resource
.depth
,
1298 0, format
->glFormat
, format
->glType
, mem
));
1299 checkGLcall("glTexImage3D");
1303 gl_info
->gl_ops
.gl
.p_glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE
, GL_FALSE
);
1304 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE)");
1309 static const struct wined3d_texture_ops texture3d_ops
=
1311 texture3d_sub_resource_load
,
1312 texture3d_sub_resource_add_dirty_region
,
1313 texture3d_sub_resource_cleanup
,
1314 texture3d_sub_resource_invalidate_location
,
1315 texture3d_sub_resource_validate_location
,
1316 texture3d_sub_resource_upload_data
,
1317 texture3d_prepare_texture
,
1320 static HRESULT
volumetexture_init(struct wined3d_texture
*texture
, const struct wined3d_resource_desc
*desc
,
1321 UINT levels
, struct wined3d_device
*device
, void *parent
, const struct wined3d_parent_ops
*parent_ops
)
1323 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
1324 struct wined3d_resource_desc volume_desc
;
1328 /* TODO: It should only be possible to create textures for formats
1329 * that are reported as supported. */
1330 if (WINED3DFMT_UNKNOWN
>= desc
->format
)
1332 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture
);
1333 return WINED3DERR_INVALIDCALL
;
1336 if (!gl_info
->supported
[EXT_TEXTURE3D
])
1338 WARN("(%p) : Texture cannot be created - no volume texture support.\n", texture
);
1339 return WINED3DERR_INVALIDCALL
;
1342 /* Calculate levels for mip mapping. */
1343 if (desc
->usage
& WINED3DUSAGE_AUTOGENMIPMAP
)
1345 if (!gl_info
->supported
[SGIS_GENERATE_MIPMAP
])
1347 WARN("No mipmap generation support, returning D3DERR_INVALIDCALL.\n");
1348 return WINED3DERR_INVALIDCALL
;
1353 WARN("WINED3DUSAGE_AUTOGENMIPMAP is set, and level count != 1, returning D3DERR_INVALIDCALL.\n");
1354 return WINED3DERR_INVALIDCALL
;
1358 if (!gl_info
->supported
[ARB_TEXTURE_NON_POWER_OF_TWO
])
1360 UINT pow2_w
, pow2_h
, pow2_d
;
1362 while (pow2_w
< desc
->width
)
1365 while (pow2_h
< desc
->height
)
1368 while (pow2_d
< desc
->depth
)
1371 if (pow2_w
!= desc
->width
|| pow2_h
!= desc
->height
|| pow2_d
!= desc
->depth
)
1373 if (desc
->pool
== WINED3D_POOL_SCRATCH
)
1375 WARN("Creating a scratch NPOT volume texture despite lack of HW support.\n");
1379 WARN("Attempted to create a NPOT volume texture (%u, %u, %u) without GL support.\n",
1380 desc
->width
, desc
->height
, desc
->depth
);
1381 return WINED3DERR_INVALIDCALL
;
1386 if (FAILED(hr
= wined3d_texture_init(texture
, &texture3d_ops
, 1, levels
, desc
,
1387 0, device
, parent
, parent_ops
, &texture_resource_ops
)))
1389 WARN("Failed to initialize texture, returning %#x.\n", hr
);
1393 texture
->pow2_matrix
[0] = 1.0f
;
1394 texture
->pow2_matrix
[5] = 1.0f
;
1395 texture
->pow2_matrix
[10] = 1.0f
;
1396 texture
->pow2_matrix
[15] = 1.0f
;
1397 texture
->target
= GL_TEXTURE_3D
;
1399 /* Generate all the surfaces. */
1400 volume_desc
= *desc
;
1401 volume_desc
.resource_type
= WINED3D_RTYPE_VOLUME
;
1402 for (i
= 0; i
< texture
->level_count
; ++i
)
1404 struct wined3d_volume
*volume
;
1406 if (FAILED(hr
= wined3d_volume_create(texture
, &volume_desc
, i
, &volume
)))
1408 ERR("Creating a volume for the volume texture failed, hr %#x.\n", hr
);
1409 wined3d_texture_cleanup(texture
);
1413 texture
->sub_resources
[i
] = &volume
->resource
;
1415 /* Calculate the next mipmap level. */
1416 volume_desc
.width
= max(1, volume_desc
.width
>> 1);
1417 volume_desc
.height
= max(1, volume_desc
.height
>> 1);
1418 volume_desc
.depth
= max(1, volume_desc
.depth
>> 1);
1424 HRESULT CDECL
wined3d_texture_create(struct wined3d_device
*device
, const struct wined3d_resource_desc
*desc
,
1425 UINT level_count
, DWORD surface_flags
, const struct wined3d_sub_resource_data
*data
, void *parent
,
1426 const struct wined3d_parent_ops
*parent_ops
, struct wined3d_texture
**texture
)
1428 struct wined3d_texture
*object
;
1431 TRACE("device %p, desc %p, level_count %u, surface_flags %#x, data %p, parent %p, parent_ops %p, texture %p.\n",
1432 device
, desc
, level_count
, surface_flags
, data
, parent
, parent_ops
, texture
);
1436 WARN("Invalid level count.\n");
1437 return WINED3DERR_INVALIDCALL
;
1440 if (!(object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
))))
1441 return E_OUTOFMEMORY
;
1443 switch (desc
->resource_type
)
1445 case WINED3D_RTYPE_TEXTURE
:
1446 hr
= texture_init(object
, desc
, level_count
, surface_flags
, device
, parent
, parent_ops
);
1449 case WINED3D_RTYPE_VOLUME_TEXTURE
:
1450 hr
= volumetexture_init(object
, desc
, level_count
, device
, parent
, parent_ops
);
1453 case WINED3D_RTYPE_CUBE_TEXTURE
:
1454 hr
= cubetexture_init(object
, desc
, level_count
, surface_flags
, device
, parent
, parent_ops
);
1458 ERR("Invalid resource type %s.\n", debug_d3dresourcetype(desc
->resource_type
));
1459 hr
= WINED3DERR_INVALIDCALL
;
1465 WARN("Failed to initialize texture, returning %#x.\n", hr
);
1466 HeapFree(GetProcessHeap(), 0, object
);
1470 /* FIXME: We'd like to avoid ever allocating system memory for the texture
1473 wined3d_texture_upload_data(object
, data
);
1475 TRACE("Created texture %p.\n", object
);