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 (FAILED(hr
= resource_init(&texture
->resource
, device
, desc
->resource_type
, format
,
47 desc
->multisample_type
, desc
->multisample_quality
, desc
->usage
, desc
->pool
,
48 desc
->width
, desc
->height
, desc
->depth
, 0, parent
, parent_ops
, resource_ops
)))
50 static unsigned int once
;
52 /* DXTn 3D textures are not supported. Do not write the ERR for them. */
53 if ((desc
->format
== WINED3DFMT_DXT1
|| desc
->format
== WINED3DFMT_DXT2
|| desc
->format
== WINED3DFMT_DXT3
54 || desc
->format
== WINED3DFMT_DXT4
|| desc
->format
== WINED3DFMT_DXT5
)
55 && !(format
->flags
[WINED3D_GL_RES_TYPE_TEX_2D
] & WINED3DFMT_FLAG_TEXTURE
)
56 && desc
->resource_type
!= WINED3D_RTYPE_VOLUME_TEXTURE
&& !once
++)
57 ERR_(winediag
)("The application tried to create a DXTn texture, but the driver does not support them.\n");
59 WARN("Failed to initialize resource, returning %#x\n", hr
);
62 wined3d_resource_update_draw_binding(&texture
->resource
);
64 texture
->texture_ops
= texture_ops
;
65 texture
->sub_resources
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
66 level_count
* layer_count
* sizeof(*texture
->sub_resources
));
67 if (!texture
->sub_resources
)
69 ERR("Failed to allocate sub-resource array.\n");
70 resource_cleanup(&texture
->resource
);
74 texture
->layer_count
= layer_count
;
75 texture
->level_count
= level_count
;
76 texture
->filter_type
= (desc
->usage
& WINED3DUSAGE_AUTOGENMIPMAP
) ? WINED3D_TEXF_LINEAR
: WINED3D_TEXF_NONE
;
78 texture
->flags
= WINED3D_TEXTURE_POW2_MAT_IDENT
| WINED3D_TEXTURE_NORMALIZED_COORDS
;
79 if (surface_flags
& WINED3D_SURFACE_PIN_SYSMEM
)
80 texture
->flags
|= WINED3D_TEXTURE_PIN_SYSMEM
;
85 /* A GL context is provided by the caller */
86 static void gltexture_delete(const struct wined3d_gl_info
*gl_info
, struct gl_texture
*tex
)
88 gl_info
->gl_ops
.gl
.p_glDeleteTextures(1, &tex
->name
);
92 static void wined3d_texture_unload_gl_texture(struct wined3d_texture
*texture
)
94 struct wined3d_device
*device
= texture
->resource
.device
;
95 struct wined3d_context
*context
= NULL
;
97 if (texture
->texture_rgb
.name
|| texture
->texture_srgb
.name
)
99 context
= context_acquire(device
, NULL
);
102 if (texture
->texture_rgb
.name
)
103 gltexture_delete(context
->gl_info
, &texture
->texture_rgb
);
105 if (texture
->texture_srgb
.name
)
106 gltexture_delete(context
->gl_info
, &texture
->texture_srgb
);
108 if (context
) context_release(context
);
110 wined3d_texture_set_dirty(texture
);
112 resource_unload(&texture
->resource
);
115 static void wined3d_texture_cleanup(struct wined3d_texture
*texture
)
117 UINT sub_count
= texture
->level_count
* texture
->layer_count
;
120 TRACE("texture %p.\n", texture
);
122 for (i
= 0; i
< sub_count
; ++i
)
124 struct wined3d_resource
*sub_resource
= texture
->sub_resources
[i
];
127 texture
->texture_ops
->texture_sub_resource_cleanup(sub_resource
);
130 wined3d_texture_unload_gl_texture(texture
);
131 HeapFree(GetProcessHeap(), 0, texture
->sub_resources
);
132 resource_cleanup(&texture
->resource
);
135 void wined3d_texture_set_swapchain(struct wined3d_texture
*texture
, struct wined3d_swapchain
*swapchain
)
137 texture
->swapchain
= swapchain
;
138 wined3d_resource_update_draw_binding(&texture
->resource
);
141 void wined3d_texture_set_dirty(struct wined3d_texture
*texture
)
143 texture
->flags
&= ~(WINED3D_TEXTURE_RGB_VALID
| WINED3D_TEXTURE_SRGB_VALID
);
146 /* Context activation is done by the caller. */
147 void wined3d_texture_bind(struct wined3d_texture
*texture
,
148 struct wined3d_context
*context
, BOOL srgb
)
150 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
151 struct gl_texture
*gl_tex
;
154 TRACE("texture %p, context %p, srgb %#x.\n", texture
, context
, srgb
);
156 if (gl_info
->supported
[EXT_TEXTURE_SRGB_DECODE
])
159 /* sRGB mode cache for preload() calls outside drawprim. */
161 texture
->flags
|= WINED3D_TEXTURE_IS_SRGB
;
163 texture
->flags
&= ~WINED3D_TEXTURE_IS_SRGB
;
165 gl_tex
= wined3d_texture_get_gl_texture(texture
, srgb
);
166 target
= texture
->target
;
170 context_bind_texture(context
, target
, gl_tex
->name
);
174 gl_info
->gl_ops
.gl
.p_glGenTextures(1, &gl_tex
->name
);
175 checkGLcall("glGenTextures");
176 TRACE("Generated texture %d.\n", gl_tex
->name
);
180 ERR("Failed to generate a texture name.\n");
184 /* Initialise the state of the texture object to the OpenGL defaults, not
185 * the wined3d defaults. */
186 gl_tex
->sampler_desc
.address_u
= WINED3D_TADDRESS_WRAP
;
187 gl_tex
->sampler_desc
.address_v
= WINED3D_TADDRESS_WRAP
;
188 gl_tex
->sampler_desc
.address_w
= WINED3D_TADDRESS_WRAP
;
189 memset(gl_tex
->sampler_desc
.border_color
, 0, sizeof(gl_tex
->sampler_desc
.border_color
));
190 gl_tex
->sampler_desc
.mag_filter
= WINED3D_TEXF_LINEAR
;
191 gl_tex
->sampler_desc
.min_filter
= WINED3D_TEXF_POINT
; /* GL_NEAREST_MIPMAP_LINEAR */
192 gl_tex
->sampler_desc
.mip_filter
= WINED3D_TEXF_LINEAR
; /* GL_NEAREST_MIPMAP_LINEAR */
193 gl_tex
->sampler_desc
.lod_bias
= 0.0f
;
194 gl_tex
->sampler_desc
.min_lod
= -1000.0f
;
195 gl_tex
->sampler_desc
.max_lod
= 1000.0f
;
196 gl_tex
->sampler_desc
.max_anisotropy
= 1;
197 gl_tex
->sampler_desc
.compare
= FALSE
;
198 gl_tex
->sampler_desc
.comparison_func
= WINED3D_CMP_LESSEQUAL
;
199 if (context
->gl_info
->supported
[EXT_TEXTURE_SRGB_DECODE
])
200 gl_tex
->sampler_desc
.srgb_decode
= TRUE
;
202 gl_tex
->sampler_desc
.srgb_decode
= srgb
;
203 gl_tex
->base_level
= 0;
204 wined3d_texture_set_dirty(texture
);
206 context_bind_texture(context
, target
, gl_tex
->name
);
208 if (texture
->resource
.usage
& WINED3DUSAGE_AUTOGENMIPMAP
)
210 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_GENERATE_MIPMAP_SGIS
, GL_TRUE
);
211 checkGLcall("glTexParameteri(target, GL_GENERATE_MIPMAP_SGIS, GL_TRUE)");
214 /* For a new texture we have to set the texture levels after binding the
215 * texture. Beware that texture rectangles do not support mipmapping, but
216 * set the maxmiplevel if we're relying on the partial
217 * GL_ARB_texture_non_power_of_two emulation with texture rectangles.
218 * (I.e., do not care about cond_np2 here, just look for
219 * GL_TEXTURE_RECTANGLE_ARB.) */
220 if (target
!= GL_TEXTURE_RECTANGLE_ARB
)
222 TRACE("Setting GL_TEXTURE_MAX_LEVEL to %u.\n", texture
->level_count
- 1);
223 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, texture
->level_count
- 1);
224 checkGLcall("glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, texture->level_count)");
227 if (target
== GL_TEXTURE_CUBE_MAP_ARB
)
229 /* Cubemaps are always set to clamp, regardless of the sampler state. */
230 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
231 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
232 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_R
, GL_CLAMP_TO_EDGE
);
235 if (texture
->flags
& WINED3D_TEXTURE_COND_NP2
)
237 /* Conditinal non power of two textures use a different clamping
238 * default. If we're using the GL_WINE_normalized_texrect partial
239 * driver emulation, we're dealing with a GL_TEXTURE_2D texture which
240 * has the address mode set to repeat - something that prevents us
241 * from hitting the accelerated codepath. Thus manually set the GL
242 * state. The same applies to filtering. Even if the texture has only
243 * one mip level, the default LINEAR_MIPMAP_LINEAR filter causes a SW
244 * fallback on macos. */
245 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
246 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
247 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
248 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
249 checkGLcall("glTexParameteri");
250 gl_tex
->sampler_desc
.address_u
= WINED3D_TADDRESS_CLAMP
;
251 gl_tex
->sampler_desc
.address_v
= WINED3D_TADDRESS_CLAMP
;
252 gl_tex
->sampler_desc
.mag_filter
= WINED3D_TEXF_POINT
;
253 gl_tex
->sampler_desc
.min_filter
= WINED3D_TEXF_POINT
;
254 gl_tex
->sampler_desc
.mip_filter
= WINED3D_TEXF_NONE
;
258 /* Context activation is done by the caller. */
259 void wined3d_texture_bind_and_dirtify(struct wined3d_texture
*texture
,
260 struct wined3d_context
*context
, BOOL srgb
)
262 DWORD active_sampler
;
264 /* We don't need a specific texture unit, but after binding the texture
265 * the current unit is dirty. Read the unit back instead of switching to
266 * 0, this avoids messing around with the state manager's GL states. The
267 * current texture unit should always be a valid one.
269 * To be more specific, this is tricky because we can implicitly be
270 * called from sampler() in state.c. This means we can't touch anything
271 * other than whatever happens to be the currently active texture, or we
272 * would risk marking already applied sampler states dirty again. */
273 active_sampler
= context
->rev_tex_unit_map
[context
->active_texture
];
274 if (active_sampler
!= WINED3D_UNMAPPED_STAGE
)
275 context_invalidate_state(context
, STATE_SAMPLER(active_sampler
));
277 wined3d_texture_bind(texture
, context
, srgb
);
280 /* Context activation is done by the caller (state handler). */
281 /* This function relies on the correct texture being bound and loaded. */
282 void wined3d_texture_apply_sampler_desc(struct wined3d_texture
*texture
,
283 const struct wined3d_sampler_desc
*sampler_desc
, const struct wined3d_gl_info
*gl_info
)
285 GLenum target
= texture
->target
;
286 struct gl_texture
*gl_tex
;
289 TRACE("texture %p, sampler_desc %p, gl_info %p.\n", texture
, sampler_desc
, gl_info
);
291 gl_tex
= wined3d_texture_get_gl_texture(texture
, texture
->flags
& WINED3D_TEXTURE_IS_SRGB
);
293 state
= sampler_desc
->address_u
;
294 if (state
!= gl_tex
->sampler_desc
.address_u
)
296 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_S
,
297 gl_info
->wrap_lookup
[state
- WINED3D_TADDRESS_WRAP
]);
298 gl_tex
->sampler_desc
.address_u
= state
;
301 state
= sampler_desc
->address_v
;
302 if (state
!= gl_tex
->sampler_desc
.address_v
)
304 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_T
,
305 gl_info
->wrap_lookup
[state
- WINED3D_TADDRESS_WRAP
]);
306 gl_tex
->sampler_desc
.address_v
= state
;
309 state
= sampler_desc
->address_w
;
310 if (state
!= gl_tex
->sampler_desc
.address_w
)
312 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_R
,
313 gl_info
->wrap_lookup
[state
- WINED3D_TADDRESS_WRAP
]);
314 gl_tex
->sampler_desc
.address_w
= state
;
317 if (memcmp(gl_tex
->sampler_desc
.border_color
, sampler_desc
->border_color
,
318 sizeof(gl_tex
->sampler_desc
.border_color
)))
320 gl_info
->gl_ops
.gl
.p_glTexParameterfv(target
, GL_TEXTURE_BORDER_COLOR
, &sampler_desc
->border_color
[0]);
321 memcpy(gl_tex
->sampler_desc
.border_color
, sampler_desc
->border_color
,
322 sizeof(gl_tex
->sampler_desc
.border_color
));
325 state
= sampler_desc
->mag_filter
;
326 if (state
!= gl_tex
->sampler_desc
.mag_filter
)
328 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MAG_FILTER
, wined3d_gl_mag_filter(state
));
329 gl_tex
->sampler_desc
.mag_filter
= state
;
332 if (sampler_desc
->min_filter
!= gl_tex
->sampler_desc
.min_filter
333 || sampler_desc
->mip_filter
!= gl_tex
->sampler_desc
.mip_filter
)
335 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MIN_FILTER
,
336 wined3d_gl_min_mip_filter(sampler_desc
->min_filter
, sampler_desc
->mip_filter
));
337 gl_tex
->sampler_desc
.min_filter
= sampler_desc
->min_filter
;
338 gl_tex
->sampler_desc
.mip_filter
= sampler_desc
->mip_filter
;
341 state
= sampler_desc
->max_anisotropy
;
342 if (state
!= gl_tex
->sampler_desc
.max_anisotropy
)
344 if (gl_info
->supported
[EXT_TEXTURE_FILTER_ANISOTROPIC
])
345 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MAX_ANISOTROPY_EXT
, state
);
347 WARN("Anisotropic filtering not supported.\n");
348 gl_tex
->sampler_desc
.max_anisotropy
= state
;
351 /* These should always be the same unless EXT_texture_sRGB_decode is supported. */
352 if (!sampler_desc
->srgb_decode
!= !gl_tex
->sampler_desc
.srgb_decode
)
354 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_SRGB_DECODE_EXT
,
355 sampler_desc
->srgb_decode
? GL_DECODE_EXT
: GL_SKIP_DECODE_EXT
);
356 gl_tex
->sampler_desc
.srgb_decode
= sampler_desc
->srgb_decode
;
359 if (!sampler_desc
->compare
!= !gl_tex
->sampler_desc
.compare
)
361 if (sampler_desc
->compare
)
363 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_DEPTH_TEXTURE_MODE_ARB
, GL_LUMINANCE
);
364 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_COMPARE_MODE_ARB
, GL_COMPARE_R_TO_TEXTURE_ARB
);
368 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_COMPARE_MODE_ARB
, GL_NONE
);
370 gl_tex
->sampler_desc
.compare
= sampler_desc
->compare
;
373 checkGLcall("Texture parameter application");
375 if (gl_info
->supported
[EXT_TEXTURE_LOD_BIAS
])
377 gl_info
->gl_ops
.gl
.p_glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT
,
378 GL_TEXTURE_LOD_BIAS_EXT
, sampler_desc
->lod_bias
);
379 checkGLcall("glTexEnvf(GL_TEXTURE_LOD_BIAS_EXT, ...)");
383 ULONG CDECL
wined3d_texture_incref(struct wined3d_texture
*texture
)
387 TRACE("texture %p, swapchain %p.\n", texture
, texture
->swapchain
);
389 if (texture
->swapchain
)
390 return wined3d_swapchain_incref(texture
->swapchain
);
392 refcount
= InterlockedIncrement(&texture
->resource
.ref
);
393 TRACE("%p increasing refcount to %u.\n", texture
, refcount
);
398 ULONG CDECL
wined3d_texture_decref(struct wined3d_texture
*texture
)
402 TRACE("texture %p, swapchain %p.\n", texture
, texture
->swapchain
);
404 if (texture
->swapchain
)
405 return wined3d_swapchain_decref(texture
->swapchain
);
407 refcount
= InterlockedDecrement(&texture
->resource
.ref
);
408 TRACE("%p decreasing refcount to %u.\n", texture
, refcount
);
412 wined3d_texture_cleanup(texture
);
413 texture
->resource
.parent_ops
->wined3d_object_destroyed(texture
->resource
.parent
);
414 HeapFree(GetProcessHeap(), 0, texture
);
420 struct wined3d_resource
* CDECL
wined3d_texture_get_resource(struct wined3d_texture
*texture
)
422 TRACE("texture %p.\n", texture
);
424 return &texture
->resource
;
427 static BOOL
color_key_equal(const struct wined3d_color_key
*c1
, struct wined3d_color_key
*c2
)
429 return c1
->color_space_low_value
== c2
->color_space_low_value
430 && c1
->color_space_high_value
== c2
->color_space_high_value
;
433 /* Context activation is done by the caller */
434 void wined3d_texture_load(struct wined3d_texture
*texture
,
435 struct wined3d_context
*context
, BOOL srgb
)
437 UINT sub_count
= texture
->level_count
* texture
->layer_count
;
438 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
439 const struct wined3d_d3d_info
*d3d_info
= context
->d3d_info
;
443 TRACE("texture %p, srgb %#x.\n", texture
, srgb
);
445 if (gl_info
->supported
[EXT_TEXTURE_SRGB_DECODE
])
449 flag
= WINED3D_TEXTURE_SRGB_VALID
;
451 flag
= WINED3D_TEXTURE_RGB_VALID
;
453 if (!d3d_info
->shader_color_key
454 && (!(texture
->async
.flags
& WINED3D_TEXTURE_ASYNC_COLOR_KEY
)
455 != !(texture
->async
.color_key_flags
& WINED3D_CKEY_SRC_BLT
)
456 || (texture
->async
.flags
& WINED3D_TEXTURE_ASYNC_COLOR_KEY
457 && !color_key_equal(&texture
->async
.gl_color_key
, &texture
->async
.src_blt_color_key
))))
459 unsigned int sub_count
= texture
->level_count
* texture
->layer_count
;
462 TRACE("Reloading because of color key value change.\n");
463 for (i
= 0; i
< sub_count
; i
++)
464 texture
->texture_ops
->texture_sub_resource_add_dirty_region(texture
->sub_resources
[i
], NULL
);
465 wined3d_texture_set_dirty(texture
);
467 texture
->async
.gl_color_key
= texture
->async
.src_blt_color_key
;
470 if (texture
->flags
& flag
)
472 TRACE("Texture %p not dirty, nothing to do.\n", texture
);
476 /* Reload the surfaces if the texture is marked dirty. */
477 for (i
= 0; i
< sub_count
; ++i
)
479 texture
->texture_ops
->texture_sub_resource_load(texture
->sub_resources
[i
], context
, srgb
);
481 texture
->flags
|= flag
;
484 void CDECL
wined3d_texture_preload(struct wined3d_texture
*texture
)
486 struct wined3d_context
*context
;
487 context
= context_acquire(texture
->resource
.device
, NULL
);
488 wined3d_texture_load(texture
, context
, texture
->flags
& WINED3D_TEXTURE_IS_SRGB
);
489 context_release(context
);
492 void * CDECL
wined3d_texture_get_parent(const struct wined3d_texture
*texture
)
494 TRACE("texture %p.\n", texture
);
496 return texture
->resource
.parent
;
499 DWORD CDECL
wined3d_texture_set_lod(struct wined3d_texture
*texture
, DWORD lod
)
501 DWORD old
= texture
->lod
;
503 TRACE("texture %p, lod %u.\n", texture
, lod
);
505 /* The d3d9:texture test shows that SetLOD is ignored on non-managed
506 * textures. The call always returns 0, and GetLOD always returns 0. */
507 if (texture
->resource
.pool
!= WINED3D_POOL_MANAGED
)
509 TRACE("Ignoring SetLOD on %s texture, returning 0.\n", debug_d3dpool(texture
->resource
.pool
));
513 if (lod
>= texture
->level_count
)
514 lod
= texture
->level_count
- 1;
516 if (texture
->lod
!= lod
)
520 texture
->texture_rgb
.base_level
= ~0u;
521 texture
->texture_srgb
.base_level
= ~0u;
522 if (texture
->resource
.bind_count
)
523 device_invalidate_state(texture
->resource
.device
, STATE_SAMPLER(texture
->sampler
));
529 DWORD CDECL
wined3d_texture_get_lod(const struct wined3d_texture
*texture
)
531 TRACE("texture %p, returning %u.\n", texture
, texture
->lod
);
536 DWORD CDECL
wined3d_texture_get_level_count(const struct wined3d_texture
*texture
)
538 TRACE("texture %p, returning %u.\n", texture
, texture
->level_count
);
540 return texture
->level_count
;
543 HRESULT CDECL
wined3d_texture_set_autogen_filter_type(struct wined3d_texture
*texture
,
544 enum wined3d_texture_filter_type filter_type
)
546 FIXME("texture %p, filter_type %s stub!\n", texture
, debug_d3dtexturefiltertype(filter_type
));
548 if (!(texture
->resource
.usage
& WINED3DUSAGE_AUTOGENMIPMAP
))
550 WARN("Texture doesn't have AUTOGENMIPMAP usage.\n");
551 return WINED3DERR_INVALIDCALL
;
554 texture
->filter_type
= filter_type
;
559 enum wined3d_texture_filter_type CDECL
wined3d_texture_get_autogen_filter_type(const struct wined3d_texture
*texture
)
561 TRACE("texture %p.\n", texture
);
563 return texture
->filter_type
;
566 HRESULT CDECL
wined3d_texture_set_color_key(struct wined3d_texture
*texture
,
567 DWORD flags
, const struct wined3d_color_key
*color_key
)
569 struct wined3d_device
*device
= texture
->resource
.device
;
570 static const DWORD all_flags
= WINED3D_CKEY_COLORSPACE
| WINED3D_CKEY_DST_BLT
571 | WINED3D_CKEY_DST_OVERLAY
| WINED3D_CKEY_SRC_BLT
| WINED3D_CKEY_SRC_OVERLAY
;
573 TRACE("texture %p, flags %#x, color_key %p.\n", texture
, flags
, color_key
);
575 if (flags
& ~all_flags
)
577 WARN("Invalid flags passed, returning WINED3DERR_INVALIDCALL.\n");
578 return WINED3DERR_INVALIDCALL
;
581 if (flags
& WINED3D_CKEY_COLORSPACE
)
583 FIXME("Unhandled flags %#x.\n", flags
);
584 return WINED3DERR_INVALIDCALL
;
587 wined3d_cs_emit_set_color_key(device
->cs
, texture
, flags
, color_key
);
592 HRESULT CDECL
wined3d_texture_update_desc(struct wined3d_texture
*texture
, UINT width
, UINT height
,
593 enum wined3d_format_id format_id
, enum wined3d_multisample_type multisample_type
,
594 UINT multisample_quality
, void *mem
, UINT pitch
)
596 struct wined3d_device
*device
= texture
->resource
.device
;
597 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
598 const struct wined3d_format
*format
= wined3d_get_format(gl_info
, format_id
);
599 UINT resource_size
= wined3d_format_calculate_size(format
, device
->surface_alignment
, width
, height
, 1);
600 struct wined3d_surface
*surface
;
602 TRACE("texture %p, width %u, height %u, format %s, multisample_type %#x, multisample_quality %u, "
603 "mem %p, pitch %u.\n",
604 texture
, width
, height
, debug_d3dformat(format_id
), multisample_type
, multisample_type
, mem
, pitch
);
607 return WINED3DERR_INVALIDCALL
;
609 if (texture
->level_count
* texture
->layer_count
> 1)
611 WARN("Texture has multiple sub-resources, not supported.\n");
612 return WINED3DERR_INVALIDCALL
;
615 if (texture
->resource
.type
== WINED3D_RTYPE_VOLUME_TEXTURE
)
617 WARN("Not supported on volume textures.\n");
618 return WINED3DERR_INVALIDCALL
;
621 /* We have no way of supporting a pitch that is not a multiple of the pixel
622 * byte width short of uploading the texture row-by-row.
623 * Fortunately that's not an issue since D3D9Ex doesn't allow a custom pitch
624 * for user-memory textures (it always expects packed data) while DirectDraw
625 * requires a 4-byte aligned pitch and doesn't support texture formats
626 * larger than 4 bytes per pixel nor any format using 3 bytes per pixel.
627 * This check is here to verify that the assumption holds. */
628 if (pitch
% texture
->resource
.format
->byte_count
)
630 WARN("Pitch unsupported, not a multiple of the texture format byte width.\n");
631 return WINED3DERR_INVALIDCALL
;
634 surface
= surface_from_resource(texture
->sub_resources
[0]);
635 if (surface
->resource
.map_count
|| (surface
->flags
& SFLAG_DCINUSE
))
637 WARN("Surface is mapped or the DC is in use.\n");
638 return WINED3DERR_INVALIDCALL
;
641 if (device
->d3d_initialized
)
642 texture
->resource
.resource_ops
->resource_unload(&texture
->resource
);
644 texture
->resource
.format
= format
;
645 texture
->resource
.multisample_type
= multisample_type
;
646 texture
->resource
.multisample_quality
= multisample_quality
;
647 texture
->resource
.width
= width
;
648 texture
->resource
.height
= height
;
650 return wined3d_surface_update_desc(surface
, gl_info
, mem
, pitch
);
653 void wined3d_texture_prepare_texture(struct wined3d_texture
*texture
, struct wined3d_context
*context
, BOOL srgb
)
655 DWORD alloc_flag
= srgb
? WINED3D_TEXTURE_SRGB_ALLOCATED
: WINED3D_TEXTURE_RGB_ALLOCATED
;
656 const struct wined3d_d3d_info
*d3d_info
= context
->d3d_info
;
658 if (!d3d_info
->shader_color_key
659 && !(texture
->async
.flags
& WINED3D_TEXTURE_ASYNC_COLOR_KEY
)
660 != !(texture
->async
.color_key_flags
& WINED3D_CKEY_SRC_BLT
))
662 wined3d_texture_force_reload(texture
);
664 if (texture
->async
.color_key_flags
& WINED3D_CKEY_SRC_BLT
)
665 texture
->async
.flags
|= WINED3D_TEXTURE_ASYNC_COLOR_KEY
;
668 if (texture
->flags
& alloc_flag
)
671 texture
->texture_ops
->texture_prepare_texture(texture
, context
, srgb
);
672 texture
->flags
|= alloc_flag
;
675 void wined3d_texture_force_reload(struct wined3d_texture
*texture
)
677 unsigned int sub_count
= texture
->level_count
* texture
->layer_count
;
680 texture
->flags
&= ~(WINED3D_TEXTURE_RGB_ALLOCATED
| WINED3D_TEXTURE_SRGB_ALLOCATED
681 | WINED3D_TEXTURE_CONVERTED
);
682 texture
->async
.flags
&= ~WINED3D_TEXTURE_ASYNC_COLOR_KEY
;
683 for (i
= 0; i
< sub_count
; ++i
)
685 texture
->texture_ops
->texture_sub_resource_invalidate_location(texture
->sub_resources
[i
],
686 WINED3D_LOCATION_TEXTURE_RGB
| WINED3D_LOCATION_TEXTURE_SRGB
);
690 void CDECL
wined3d_texture_generate_mipmaps(struct wined3d_texture
*texture
)
692 /* TODO: Implement filters using GL_SGI_generate_mipmaps. */
693 FIXME("texture %p stub!\n", texture
);
696 struct wined3d_resource
* CDECL
wined3d_texture_get_sub_resource(struct wined3d_texture
*texture
,
697 UINT sub_resource_idx
)
699 UINT sub_count
= texture
->level_count
* texture
->layer_count
;
701 TRACE("texture %p, sub_resource_idx %u.\n", texture
, sub_resource_idx
);
703 if (sub_resource_idx
>= sub_count
)
705 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx
, sub_count
);
709 return texture
->sub_resources
[sub_resource_idx
];
712 HRESULT CDECL
wined3d_texture_add_dirty_region(struct wined3d_texture
*texture
,
713 UINT layer
, const struct wined3d_box
*dirty_region
)
715 struct wined3d_resource
*sub_resource
;
717 TRACE("texture %p, layer %u, dirty_region %p.\n", texture
, layer
, dirty_region
);
719 if (!(sub_resource
= wined3d_texture_get_sub_resource(texture
, layer
* texture
->level_count
)))
721 WARN("Failed to get sub-resource.\n");
722 return WINED3DERR_INVALIDCALL
;
725 texture
->texture_ops
->texture_sub_resource_add_dirty_region(sub_resource
, dirty_region
);
730 static void wined3d_texture_upload_data(struct wined3d_texture
*texture
, const struct wined3d_sub_resource_data
*data
)
732 unsigned int sub_count
= texture
->level_count
* texture
->layer_count
;
733 struct wined3d_context
*context
;
736 context
= context_acquire(texture
->resource
.device
, NULL
);
738 wined3d_texture_prepare_texture(texture
, context
, FALSE
);
739 wined3d_texture_bind_and_dirtify(texture
, context
, FALSE
);
741 for (i
= 0; i
< sub_count
; ++i
)
743 struct wined3d_resource
*sub_resource
= texture
->sub_resources
[i
];
745 texture
->texture_ops
->texture_sub_resource_upload_data(sub_resource
, context
, &data
[i
]);
746 texture
->texture_ops
->texture_sub_resource_validate_location(sub_resource
, WINED3D_LOCATION_TEXTURE_RGB
);
747 texture
->texture_ops
->texture_sub_resource_invalidate_location(sub_resource
, ~WINED3D_LOCATION_TEXTURE_RGB
);
750 context_release(context
);
753 static void texture2d_sub_resource_load(struct wined3d_resource
*sub_resource
,
754 struct wined3d_context
*context
, BOOL srgb
)
756 surface_load(surface_from_resource(sub_resource
), srgb
);
759 static void texture2d_sub_resource_add_dirty_region(struct wined3d_resource
*sub_resource
,
760 const struct wined3d_box
*dirty_region
)
762 struct wined3d_surface
*surface
= surface_from_resource(sub_resource
);
764 surface_prepare_map_memory(surface
);
765 surface_load_location(surface
, surface
->resource
.map_binding
);
766 surface_invalidate_location(surface
, ~surface
->resource
.map_binding
);
769 static void texture2d_sub_resource_cleanup(struct wined3d_resource
*sub_resource
)
771 struct wined3d_surface
*surface
= surface_from_resource(sub_resource
);
773 wined3d_surface_destroy(surface
);
776 static void texture2d_sub_resource_invalidate_location(struct wined3d_resource
*sub_resource
, DWORD location
)
778 struct wined3d_surface
*surface
= surface_from_resource(sub_resource
);
780 surface_invalidate_location(surface
, location
);
783 static void texture2d_sub_resource_validate_location(struct wined3d_resource
*sub_resource
, DWORD location
)
785 struct wined3d_surface
*surface
= surface_from_resource(sub_resource
);
787 surface_validate_location(surface
, location
);
790 static void texture2d_sub_resource_upload_data(struct wined3d_resource
*sub_resource
,
791 const struct wined3d_context
*context
, const struct wined3d_sub_resource_data
*data
)
793 struct wined3d_surface
*surface
= surface_from_resource(sub_resource
);
794 static const POINT dst_point
= {0, 0};
795 struct wined3d_const_bo_address addr
;
800 src_rect
.right
= surface
->resource
.width
;
801 src_rect
.bottom
= surface
->resource
.height
;
803 addr
.buffer_object
= 0;
804 addr
.addr
= data
->data
;
806 wined3d_surface_upload_data(surface
, context
->gl_info
, surface
->container
->resource
.format
,
807 &src_rect
, data
->row_pitch
, &dst_point
, FALSE
, &addr
);
810 /* Context activation is done by the caller. */
811 static void texture2d_prepare_texture(struct wined3d_texture
*texture
, struct wined3d_context
*context
, BOOL srgb
)
813 UINT sub_count
= texture
->level_count
* texture
->layer_count
;
814 const struct wined3d_format
*format
= texture
->resource
.format
;
815 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
816 const struct wined3d_color_key_conversion
*conversion
;
820 TRACE("texture %p, format %s.\n", texture
, debug_d3dformat(format
->id
));
824 texture
->flags
|= WINED3D_TEXTURE_CONVERTED
;
826 else if ((conversion
= wined3d_format_get_color_key_conversion(texture
, TRUE
)))
828 texture
->flags
|= WINED3D_TEXTURE_CONVERTED
;
829 format
= wined3d_get_format(gl_info
, conversion
->dst_format
);
830 TRACE("Using format %s for color key conversion.\n", debug_d3dformat(format
->id
));
833 wined3d_texture_bind_and_dirtify(texture
, context
, srgb
);
836 internal
= format
->glGammaInternal
;
837 else if (texture
->resource
.usage
& WINED3DUSAGE_RENDERTARGET
838 && wined3d_resource_is_offscreen(&texture
->resource
))
839 internal
= format
->rtInternal
;
841 internal
= format
->glInternal
;
844 FIXME("No GL internal format for format %s.\n", debug_d3dformat(format
->id
));
846 TRACE("internal %#x, format %#x, type %#x.\n", internal
, format
->glFormat
, format
->glType
);
848 for (i
= 0; i
< sub_count
; ++i
)
850 struct wined3d_surface
*surface
= surface_from_resource(texture
->sub_resources
[i
]);
851 GLsizei height
= surface
->pow2Height
;
852 GLsizei width
= surface
->pow2Width
;
853 const BYTE
*mem
= NULL
;
855 if (texture
->resource
.format_flags
& WINED3DFMT_FLAG_HEIGHT_SCALE
)
857 height
*= format
->height_scale
.numerator
;
858 height
/= format
->height_scale
.denominator
;
861 TRACE("surface %p, target %#x, level %d, width %d, height %d.\n",
862 surface
, surface
->texture_target
, surface
->texture_level
, width
, height
);
864 if (gl_info
->supported
[APPLE_CLIENT_STORAGE
])
866 if (surface
->flags
& (SFLAG_NONPOW2
| SFLAG_DIBSECTION
)
867 || texture
->flags
& WINED3D_TEXTURE_CONVERTED
868 || !surface
->resource
.heap_memory
)
870 /* In some cases we want to disable client storage.
871 * SFLAG_NONPOW2 has a bigger opengl texture than the client memory, and different pitches
872 * SFLAG_DIBSECTION: Dibsections may have read / write protections on the memory. Avoid issues...
873 * WINED3D_TEXTURE_CONVERTED: The conversion destination memory is freed after loading the surface
874 * heap_memory == NULL: Not defined in the extension. Seems to disable client storage effectively
876 surface
->flags
&= ~SFLAG_CLIENT
;
880 surface
->flags
|= SFLAG_CLIENT
;
881 mem
= surface
->resource
.heap_memory
;
883 gl_info
->gl_ops
.gl
.p_glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE
, GL_TRUE
);
884 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE)");
888 if (texture
->resource
.format_flags
& WINED3DFMT_FLAG_COMPRESSED
&& mem
)
890 GL_EXTCALL(glCompressedTexImage2D(surface
->texture_target
, surface
->texture_level
,
891 internal
, width
, height
, 0, surface
->resource
.size
, mem
));
892 checkGLcall("glCompressedTexImage2D");
896 gl_info
->gl_ops
.gl
.p_glTexImage2D(surface
->texture_target
, surface
->texture_level
,
897 internal
, width
, height
, 0, format
->glFormat
, format
->glType
, mem
);
898 checkGLcall("glTexImage2D");
903 gl_info
->gl_ops
.gl
.p_glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE
, GL_FALSE
);
904 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE)");
909 static const struct wined3d_texture_ops texture2d_ops
=
911 texture2d_sub_resource_load
,
912 texture2d_sub_resource_add_dirty_region
,
913 texture2d_sub_resource_cleanup
,
914 texture2d_sub_resource_invalidate_location
,
915 texture2d_sub_resource_validate_location
,
916 texture2d_sub_resource_upload_data
,
917 texture2d_prepare_texture
,
920 static ULONG
texture_resource_incref(struct wined3d_resource
*resource
)
922 return wined3d_texture_incref(wined3d_texture_from_resource(resource
));
925 static ULONG
texture_resource_decref(struct wined3d_resource
*resource
)
927 return wined3d_texture_decref(wined3d_texture_from_resource(resource
));
930 static void wined3d_texture_unload(struct wined3d_resource
*resource
)
932 struct wined3d_texture
*texture
= wined3d_texture_from_resource(resource
);
933 UINT sub_count
= texture
->level_count
* texture
->layer_count
;
936 TRACE("texture %p.\n", texture
);
938 for (i
= 0; i
< sub_count
; ++i
)
940 struct wined3d_resource
*sub_resource
= texture
->sub_resources
[i
];
942 sub_resource
->resource_ops
->resource_unload(sub_resource
);
945 wined3d_texture_force_reload(texture
);
946 wined3d_texture_unload_gl_texture(texture
);
949 static const struct wined3d_resource_ops texture_resource_ops
=
951 texture_resource_incref
,
952 texture_resource_decref
,
953 wined3d_texture_unload
,
956 static HRESULT
cubetexture_init(struct wined3d_texture
*texture
, const struct wined3d_resource_desc
*desc
,
957 UINT levels
, DWORD surface_flags
, struct wined3d_device
*device
, void *parent
,
958 const struct wined3d_parent_ops
*parent_ops
)
960 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
961 struct wined3d_resource_desc surface_desc
;
965 /* TODO: It should only be possible to create textures for formats
966 * that are reported as supported. */
967 if (WINED3DFMT_UNKNOWN
>= desc
->format
)
969 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture
);
970 return WINED3DERR_INVALIDCALL
;
973 /* Calculate levels for mip mapping */
974 if (desc
->usage
& WINED3DUSAGE_AUTOGENMIPMAP
)
976 if (!gl_info
->supported
[SGIS_GENERATE_MIPMAP
])
978 WARN("No mipmap generation support, returning D3DERR_INVALIDCALL.\n");
979 return WINED3DERR_INVALIDCALL
;
984 WARN("WINED3DUSAGE_AUTOGENMIPMAP is set, and level count != 1, returning D3DERR_INVALIDCALL.\n");
985 return WINED3DERR_INVALIDCALL
;
989 if (!gl_info
->supported
[ARB_TEXTURE_NON_POWER_OF_TWO
])
991 UINT pow2_edge_length
= 1;
992 while (pow2_edge_length
< desc
->width
)
993 pow2_edge_length
<<= 1;
995 if (desc
->width
!= pow2_edge_length
)
997 if (desc
->pool
== WINED3D_POOL_SCRATCH
)
999 /* SCRATCH textures cannot be used for texturing */
1000 WARN("Creating a scratch NPOT cube texture despite lack of HW support.\n");
1004 WARN("Attempted to create a NPOT cube texture (edge length %u) without GL support.\n", desc
->width
);
1005 return WINED3DERR_INVALIDCALL
;
1010 if (FAILED(hr
= wined3d_texture_init(texture
, &texture2d_ops
, 6, levels
, desc
,
1011 surface_flags
, device
, parent
, parent_ops
, &texture_resource_ops
)))
1013 WARN("Failed to initialize texture, returning %#x\n", hr
);
1017 texture
->pow2_matrix
[0] = 1.0f
;
1018 texture
->pow2_matrix
[5] = 1.0f
;
1019 texture
->pow2_matrix
[10] = 1.0f
;
1020 texture
->pow2_matrix
[15] = 1.0f
;
1021 texture
->target
= GL_TEXTURE_CUBE_MAP_ARB
;
1023 /* Generate all the surfaces. */
1024 surface_desc
= *desc
;
1025 surface_desc
.resource_type
= WINED3D_RTYPE_SURFACE
;
1026 for (i
= 0; i
< texture
->level_count
; ++i
)
1028 /* Create the 6 faces. */
1029 for (j
= 0; j
< 6; ++j
)
1031 static const GLenum cube_targets
[6] =
1033 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
,
1034 GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
,
1035 GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
,
1036 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
,
1037 GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
,
1038 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
,
1040 UINT idx
= j
* texture
->level_count
+ i
;
1041 struct wined3d_surface
*surface
;
1043 if (FAILED(hr
= wined3d_surface_create(texture
, &surface_desc
,
1044 cube_targets
[j
], i
, j
, surface_flags
, &surface
)))
1046 WARN("Failed to create surface, hr %#x.\n", hr
);
1047 wined3d_texture_cleanup(texture
);
1051 texture
->sub_resources
[idx
] = &surface
->resource
;
1052 TRACE("Created surface level %u @ %p.\n", i
, surface
);
1054 surface_desc
.width
= max(1, surface_desc
.width
>> 1);
1055 surface_desc
.height
= surface_desc
.width
;
1061 static HRESULT
texture_init(struct wined3d_texture
*texture
, const struct wined3d_resource_desc
*desc
,
1062 UINT levels
, DWORD surface_flags
, struct wined3d_device
*device
, void *parent
,
1063 const struct wined3d_parent_ops
*parent_ops
)
1065 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
1066 struct wined3d_resource_desc surface_desc
;
1067 UINT pow2_width
, pow2_height
;
1071 /* TODO: It should only be possible to create textures for formats
1072 * that are reported as supported. */
1073 if (WINED3DFMT_UNKNOWN
>= desc
->format
)
1075 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture
);
1076 return WINED3DERR_INVALIDCALL
;
1079 /* Non-power2 support. */
1080 if (gl_info
->supported
[ARB_TEXTURE_NON_POWER_OF_TWO
])
1082 pow2_width
= desc
->width
;
1083 pow2_height
= desc
->height
;
1087 /* Find the nearest pow2 match. */
1088 pow2_width
= pow2_height
= 1;
1089 while (pow2_width
< desc
->width
)
1091 while (pow2_height
< desc
->height
)
1094 if (pow2_width
!= desc
->width
|| pow2_height
!= desc
->height
)
1096 /* levels == 0 returns an error as well */
1099 if (desc
->pool
== WINED3D_POOL_SCRATCH
)
1101 WARN("Creating a scratch mipmapped NPOT texture despite lack of HW support.\n");
1105 WARN("Attempted to create a mipmapped NPOT texture without unconditional NPOT support.\n");
1106 return WINED3DERR_INVALIDCALL
;
1112 /* Calculate levels for mip mapping. */
1113 if (desc
->usage
& WINED3DUSAGE_AUTOGENMIPMAP
)
1115 if (!gl_info
->supported
[SGIS_GENERATE_MIPMAP
])
1117 WARN("No mipmap generation support, returning WINED3DERR_INVALIDCALL.\n");
1118 return WINED3DERR_INVALIDCALL
;
1123 WARN("WINED3DUSAGE_AUTOGENMIPMAP is set, and level count != 1, returning WINED3DERR_INVALIDCALL.\n");
1124 return WINED3DERR_INVALIDCALL
;
1128 if (FAILED(hr
= wined3d_texture_init(texture
, &texture2d_ops
, 1, levels
, desc
,
1129 surface_flags
, device
, parent
, parent_ops
, &texture_resource_ops
)))
1131 WARN("Failed to initialize texture, returning %#x.\n", hr
);
1135 /* Precalculated scaling for 'faked' non power of two texture coords. */
1136 if (texture
->resource
.gl_type
== WINED3D_GL_RES_TYPE_TEX_RECT
)
1138 texture
->pow2_matrix
[0] = (float)desc
->width
;
1139 texture
->pow2_matrix
[5] = (float)desc
->height
;
1140 texture
->pow2_matrix
[10] = 1.0f
;
1141 texture
->pow2_matrix
[15] = 1.0f
;
1142 texture
->target
= GL_TEXTURE_RECTANGLE_ARB
;
1143 texture
->flags
|= WINED3D_TEXTURE_COND_NP2
;
1144 texture
->flags
&= ~(WINED3D_TEXTURE_POW2_MAT_IDENT
| WINED3D_TEXTURE_NORMALIZED_COORDS
);
1148 texture
->target
= GL_TEXTURE_2D
;
1149 if (desc
->width
== pow2_width
&& desc
->height
== pow2_height
)
1151 texture
->pow2_matrix
[0] = 1.0f
;
1152 texture
->pow2_matrix
[5] = 1.0f
;
1154 else if (gl_info
->supported
[WINED3D_GL_NORMALIZED_TEXRECT
])
1156 texture
->pow2_matrix
[0] = 1.0f
;
1157 texture
->pow2_matrix
[5] = 1.0f
;
1158 texture
->flags
|= WINED3D_TEXTURE_COND_NP2
;
1162 texture
->pow2_matrix
[0] = (((float)desc
->width
) / ((float)pow2_width
));
1163 texture
->pow2_matrix
[5] = (((float)desc
->height
) / ((float)pow2_height
));
1164 texture
->flags
&= ~WINED3D_TEXTURE_POW2_MAT_IDENT
;
1166 texture
->pow2_matrix
[10] = 1.0f
;
1167 texture
->pow2_matrix
[15] = 1.0f
;
1169 TRACE("xf(%f) yf(%f)\n", texture
->pow2_matrix
[0], texture
->pow2_matrix
[5]);
1171 /* Generate all the surfaces. */
1172 surface_desc
= *desc
;
1173 surface_desc
.resource_type
= WINED3D_RTYPE_SURFACE
;
1174 for (i
= 0; i
< texture
->level_count
; ++i
)
1176 struct wined3d_surface
*surface
;
1178 if (FAILED(hr
= wined3d_surface_create(texture
, &surface_desc
,
1179 texture
->target
, i
, 0, surface_flags
, &surface
)))
1181 WARN("Failed to create surface, hr %#x.\n", hr
);
1182 wined3d_texture_cleanup(texture
);
1186 texture
->sub_resources
[i
] = &surface
->resource
;
1187 TRACE("Created surface level %u @ %p.\n", i
, surface
);
1188 /* Calculate the next mipmap level. */
1189 surface_desc
.width
= max(1, surface_desc
.width
>> 1);
1190 surface_desc
.height
= max(1, surface_desc
.height
>> 1);
1196 static void texture3d_sub_resource_load(struct wined3d_resource
*sub_resource
,
1197 struct wined3d_context
*context
, BOOL srgb
)
1199 wined3d_volume_load(volume_from_resource(sub_resource
), context
, srgb
);
1202 static void texture3d_sub_resource_add_dirty_region(struct wined3d_resource
*sub_resource
,
1203 const struct wined3d_box
*dirty_region
)
1205 wined3d_texture_set_dirty(volume_from_resource(sub_resource
)->container
);
1208 static void texture3d_sub_resource_cleanup(struct wined3d_resource
*sub_resource
)
1210 struct wined3d_volume
*volume
= volume_from_resource(sub_resource
);
1212 wined3d_volume_destroy(volume
);
1215 static void texture3d_sub_resource_invalidate_location(struct wined3d_resource
*sub_resource
, DWORD location
)
1217 struct wined3d_volume
*volume
= volume_from_resource(sub_resource
);
1219 wined3d_volume_invalidate_location(volume
, location
);
1222 static void texture3d_sub_resource_validate_location(struct wined3d_resource
*sub_resource
, DWORD location
)
1224 struct wined3d_volume
*volume
= volume_from_resource(sub_resource
);
1226 wined3d_volume_validate_location(volume
, location
);
1229 static void texture3d_sub_resource_upload_data(struct wined3d_resource
*sub_resource
,
1230 const struct wined3d_context
*context
, const struct wined3d_sub_resource_data
*data
)
1232 struct wined3d_volume
*volume
= volume_from_resource(sub_resource
);
1233 struct wined3d_const_bo_address addr
;
1234 unsigned int row_pitch
, slice_pitch
;
1236 wined3d_volume_get_pitch(volume
, &row_pitch
, &slice_pitch
);
1237 if (row_pitch
!= data
->row_pitch
|| slice_pitch
!= data
->slice_pitch
)
1238 FIXME("Ignoring row/slice pitch (%u/%u).\n", data
->row_pitch
, data
->slice_pitch
);
1240 addr
.buffer_object
= 0;
1241 addr
.addr
= data
->data
;
1243 wined3d_volume_upload_data(volume
, context
, &addr
);
1246 static void texture3d_prepare_texture(struct wined3d_texture
*texture
, struct wined3d_context
*context
, BOOL srgb
)
1248 unsigned int sub_count
= texture
->level_count
* texture
->layer_count
;
1249 const struct wined3d_format
*format
= texture
->resource
.format
;
1250 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1253 wined3d_texture_bind_and_dirtify(texture
, context
, srgb
);
1255 for (i
= 0; i
< sub_count
; ++i
)
1257 struct wined3d_volume
*volume
= volume_from_resource(texture
->sub_resources
[i
]);
1260 if (gl_info
->supported
[APPLE_CLIENT_STORAGE
] && !format
->convert
1261 && volume_prepare_system_memory(volume
))
1263 TRACE("Enabling GL_UNPACK_CLIENT_STORAGE_APPLE for volume %p\n", volume
);
1264 gl_info
->gl_ops
.gl
.p_glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE
, GL_TRUE
);
1265 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE)");
1266 mem
= volume
->resource
.heap_memory
;
1267 volume
->flags
|= WINED3D_VFLAG_CLIENT_STORAGE
;
1270 GL_EXTCALL(glTexImage3D(GL_TEXTURE_3D
, volume
->texture_level
,
1271 srgb
? format
->glGammaInternal
: format
->glInternal
,
1272 volume
->resource
.width
, volume
->resource
.height
, volume
->resource
.depth
,
1273 0, format
->glFormat
, format
->glType
, mem
));
1274 checkGLcall("glTexImage3D");
1278 gl_info
->gl_ops
.gl
.p_glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE
, GL_FALSE
);
1279 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE)");
1284 static const struct wined3d_texture_ops texture3d_ops
=
1286 texture3d_sub_resource_load
,
1287 texture3d_sub_resource_add_dirty_region
,
1288 texture3d_sub_resource_cleanup
,
1289 texture3d_sub_resource_invalidate_location
,
1290 texture3d_sub_resource_validate_location
,
1291 texture3d_sub_resource_upload_data
,
1292 texture3d_prepare_texture
,
1295 static HRESULT
volumetexture_init(struct wined3d_texture
*texture
, const struct wined3d_resource_desc
*desc
,
1296 UINT levels
, struct wined3d_device
*device
, void *parent
, const struct wined3d_parent_ops
*parent_ops
)
1298 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
1299 struct wined3d_resource_desc volume_desc
;
1303 /* TODO: It should only be possible to create textures for formats
1304 * that are reported as supported. */
1305 if (WINED3DFMT_UNKNOWN
>= desc
->format
)
1307 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture
);
1308 return WINED3DERR_INVALIDCALL
;
1311 if (!gl_info
->supported
[EXT_TEXTURE3D
])
1313 WARN("(%p) : Texture cannot be created - no volume texture support.\n", texture
);
1314 return WINED3DERR_INVALIDCALL
;
1317 /* Calculate levels for mip mapping. */
1318 if (desc
->usage
& WINED3DUSAGE_AUTOGENMIPMAP
)
1320 if (!gl_info
->supported
[SGIS_GENERATE_MIPMAP
])
1322 WARN("No mipmap generation support, returning D3DERR_INVALIDCALL.\n");
1323 return WINED3DERR_INVALIDCALL
;
1328 WARN("WINED3DUSAGE_AUTOGENMIPMAP is set, and level count != 1, returning D3DERR_INVALIDCALL.\n");
1329 return WINED3DERR_INVALIDCALL
;
1333 if (!gl_info
->supported
[ARB_TEXTURE_NON_POWER_OF_TWO
])
1335 UINT pow2_w
, pow2_h
, pow2_d
;
1337 while (pow2_w
< desc
->width
)
1340 while (pow2_h
< desc
->height
)
1343 while (pow2_d
< desc
->depth
)
1346 if (pow2_w
!= desc
->width
|| pow2_h
!= desc
->height
|| pow2_d
!= desc
->depth
)
1348 if (desc
->pool
== WINED3D_POOL_SCRATCH
)
1350 WARN("Creating a scratch NPOT volume texture despite lack of HW support.\n");
1354 WARN("Attempted to create a NPOT volume texture (%u, %u, %u) without GL support.\n",
1355 desc
->width
, desc
->height
, desc
->depth
);
1356 return WINED3DERR_INVALIDCALL
;
1361 if (FAILED(hr
= wined3d_texture_init(texture
, &texture3d_ops
, 1, levels
, desc
,
1362 0, device
, parent
, parent_ops
, &texture_resource_ops
)))
1364 WARN("Failed to initialize texture, returning %#x.\n", hr
);
1368 texture
->pow2_matrix
[0] = 1.0f
;
1369 texture
->pow2_matrix
[5] = 1.0f
;
1370 texture
->pow2_matrix
[10] = 1.0f
;
1371 texture
->pow2_matrix
[15] = 1.0f
;
1372 texture
->target
= GL_TEXTURE_3D
;
1374 /* Generate all the surfaces. */
1375 volume_desc
= *desc
;
1376 volume_desc
.resource_type
= WINED3D_RTYPE_VOLUME
;
1377 for (i
= 0; i
< texture
->level_count
; ++i
)
1379 struct wined3d_volume
*volume
;
1381 if (FAILED(hr
= wined3d_volume_create(texture
, &volume_desc
, i
, &volume
)))
1383 ERR("Creating a volume for the volume texture failed, hr %#x.\n", hr
);
1384 wined3d_texture_cleanup(texture
);
1388 texture
->sub_resources
[i
] = &volume
->resource
;
1390 /* Calculate the next mipmap level. */
1391 volume_desc
.width
= max(1, volume_desc
.width
>> 1);
1392 volume_desc
.height
= max(1, volume_desc
.height
>> 1);
1393 volume_desc
.depth
= max(1, volume_desc
.depth
>> 1);
1399 HRESULT CDECL
wined3d_texture_create(struct wined3d_device
*device
, const struct wined3d_resource_desc
*desc
,
1400 UINT level_count
, DWORD surface_flags
, const struct wined3d_sub_resource_data
*data
, void *parent
,
1401 const struct wined3d_parent_ops
*parent_ops
, struct wined3d_texture
**texture
)
1403 struct wined3d_texture
*object
;
1406 TRACE("device %p, desc %p, level_count %u, surface_flags %#x, data %p, parent %p, parent_ops %p, texture %p.\n",
1407 device
, desc
, level_count
, surface_flags
, data
, parent
, parent_ops
, texture
);
1411 WARN("Invalid level count.\n");
1412 return WINED3DERR_INVALIDCALL
;
1415 if (!(object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
))))
1416 return E_OUTOFMEMORY
;
1418 switch (desc
->resource_type
)
1420 case WINED3D_RTYPE_TEXTURE
:
1421 hr
= texture_init(object
, desc
, level_count
, surface_flags
, device
, parent
, parent_ops
);
1424 case WINED3D_RTYPE_VOLUME_TEXTURE
:
1425 hr
= volumetexture_init(object
, desc
, level_count
, device
, parent
, parent_ops
);
1428 case WINED3D_RTYPE_CUBE_TEXTURE
:
1429 hr
= cubetexture_init(object
, desc
, level_count
, surface_flags
, device
, parent
, parent_ops
);
1433 ERR("Invalid resource type %s.\n", debug_d3dresourcetype(desc
->resource_type
));
1434 hr
= WINED3DERR_INVALIDCALL
;
1440 WARN("Failed to initialize texture, returning %#x.\n", hr
);
1441 HeapFree(GetProcessHeap(), 0, object
);
1445 /* FIXME: We'd like to avoid ever allocating system memory for the texture
1448 wined3d_texture_upload_data(object
, data
);
1450 TRACE("Created texture %p.\n", object
);