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 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 "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 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_TEXTURE_3D
&& !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 (flags
& WINED3D_TEXTURE_CREATE_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 (!needs_separate_srgb_gl_texture(context
))
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
;
257 if (gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
] && gl_info
->supported
[ARB_DEPTH_TEXTURE
])
259 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_DEPTH_TEXTURE_MODE_ARB
, GL_INTENSITY
);
260 checkGLcall("glTexParameteri(GL_DEPTH_TEXTURE_MODE_ARB, GL_INTENSITY)");
264 /* Context activation is done by the caller. */
265 void wined3d_texture_bind_and_dirtify(struct wined3d_texture
*texture
,
266 struct wined3d_context
*context
, BOOL srgb
)
268 DWORD active_sampler
;
270 /* We don't need a specific texture unit, but after binding the texture
271 * the current unit is dirty. Read the unit back instead of switching to
272 * 0, this avoids messing around with the state manager's GL states. The
273 * current texture unit should always be a valid one.
275 * To be more specific, this is tricky because we can implicitly be
276 * called from sampler() in state.c. This means we can't touch anything
277 * other than whatever happens to be the currently active texture, or we
278 * would risk marking already applied sampler states dirty again. */
279 active_sampler
= context
->rev_tex_unit_map
[context
->active_texture
];
280 if (active_sampler
!= WINED3D_UNMAPPED_STAGE
)
281 context_invalidate_state(context
, STATE_SAMPLER(active_sampler
));
282 /* FIXME: Ideally we'd only do this when touching a binding that's used by
284 context_invalidate_state(context
, STATE_SHADER_RESOURCE_BINDING
);
286 wined3d_texture_bind(texture
, context
, srgb
);
289 /* Context activation is done by the caller (state handler). */
290 /* This function relies on the correct texture being bound and loaded. */
291 void wined3d_texture_apply_sampler_desc(struct wined3d_texture
*texture
,
292 const struct wined3d_sampler_desc
*sampler_desc
, const struct wined3d_context
*context
)
294 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
295 GLenum target
= texture
->target
;
296 struct gl_texture
*gl_tex
;
299 TRACE("texture %p, sampler_desc %p, context %p.\n", texture
, sampler_desc
, context
);
301 gl_tex
= wined3d_texture_get_gl_texture(texture
, texture
->flags
& WINED3D_TEXTURE_IS_SRGB
);
303 state
= sampler_desc
->address_u
;
304 if (state
!= gl_tex
->sampler_desc
.address_u
)
306 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_S
,
307 gl_info
->wrap_lookup
[state
- WINED3D_TADDRESS_WRAP
]);
308 gl_tex
->sampler_desc
.address_u
= state
;
311 state
= sampler_desc
->address_v
;
312 if (state
!= gl_tex
->sampler_desc
.address_v
)
314 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_T
,
315 gl_info
->wrap_lookup
[state
- WINED3D_TADDRESS_WRAP
]);
316 gl_tex
->sampler_desc
.address_v
= state
;
319 state
= sampler_desc
->address_w
;
320 if (state
!= gl_tex
->sampler_desc
.address_w
)
322 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_R
,
323 gl_info
->wrap_lookup
[state
- WINED3D_TADDRESS_WRAP
]);
324 gl_tex
->sampler_desc
.address_w
= state
;
327 if (memcmp(gl_tex
->sampler_desc
.border_color
, sampler_desc
->border_color
,
328 sizeof(gl_tex
->sampler_desc
.border_color
)))
330 gl_info
->gl_ops
.gl
.p_glTexParameterfv(target
, GL_TEXTURE_BORDER_COLOR
, &sampler_desc
->border_color
[0]);
331 memcpy(gl_tex
->sampler_desc
.border_color
, sampler_desc
->border_color
,
332 sizeof(gl_tex
->sampler_desc
.border_color
));
335 state
= sampler_desc
->mag_filter
;
336 if (state
!= gl_tex
->sampler_desc
.mag_filter
)
338 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MAG_FILTER
, wined3d_gl_mag_filter(state
));
339 gl_tex
->sampler_desc
.mag_filter
= state
;
342 if (sampler_desc
->min_filter
!= gl_tex
->sampler_desc
.min_filter
343 || sampler_desc
->mip_filter
!= gl_tex
->sampler_desc
.mip_filter
)
345 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MIN_FILTER
,
346 wined3d_gl_min_mip_filter(sampler_desc
->min_filter
, sampler_desc
->mip_filter
));
347 gl_tex
->sampler_desc
.min_filter
= sampler_desc
->min_filter
;
348 gl_tex
->sampler_desc
.mip_filter
= sampler_desc
->mip_filter
;
351 state
= sampler_desc
->max_anisotropy
;
352 if (state
!= gl_tex
->sampler_desc
.max_anisotropy
)
354 if (gl_info
->supported
[EXT_TEXTURE_FILTER_ANISOTROPIC
])
355 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MAX_ANISOTROPY_EXT
, state
);
357 WARN("Anisotropic filtering not supported.\n");
358 gl_tex
->sampler_desc
.max_anisotropy
= state
;
361 if (!sampler_desc
->srgb_decode
!= !gl_tex
->sampler_desc
.srgb_decode
362 && (context
->d3d_info
->wined3d_creation_flags
& WINED3D_SRGB_READ_WRITE_CONTROL
)
363 && gl_info
->supported
[EXT_TEXTURE_SRGB_DECODE
])
365 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_SRGB_DECODE_EXT
,
366 sampler_desc
->srgb_decode
? GL_DECODE_EXT
: GL_SKIP_DECODE_EXT
);
367 gl_tex
->sampler_desc
.srgb_decode
= sampler_desc
->srgb_decode
;
370 if (!sampler_desc
->compare
!= !gl_tex
->sampler_desc
.compare
)
372 if (sampler_desc
->compare
)
373 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_COMPARE_MODE_ARB
, GL_COMPARE_R_TO_TEXTURE_ARB
);
375 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_COMPARE_MODE_ARB
, GL_NONE
);
376 gl_tex
->sampler_desc
.compare
= sampler_desc
->compare
;
379 checkGLcall("Texture parameter application");
381 if (gl_info
->supported
[EXT_TEXTURE_LOD_BIAS
])
383 gl_info
->gl_ops
.gl
.p_glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT
,
384 GL_TEXTURE_LOD_BIAS_EXT
, sampler_desc
->lod_bias
);
385 checkGLcall("glTexEnvf(GL_TEXTURE_LOD_BIAS_EXT, ...)");
389 ULONG CDECL
wined3d_texture_incref(struct wined3d_texture
*texture
)
393 TRACE("texture %p, swapchain %p.\n", texture
, texture
->swapchain
);
395 if (texture
->swapchain
)
396 return wined3d_swapchain_incref(texture
->swapchain
);
398 refcount
= InterlockedIncrement(&texture
->resource
.ref
);
399 TRACE("%p increasing refcount to %u.\n", texture
, refcount
);
404 ULONG CDECL
wined3d_texture_decref(struct wined3d_texture
*texture
)
408 TRACE("texture %p, swapchain %p.\n", texture
, texture
->swapchain
);
410 if (texture
->swapchain
)
411 return wined3d_swapchain_decref(texture
->swapchain
);
413 refcount
= InterlockedDecrement(&texture
->resource
.ref
);
414 TRACE("%p decreasing refcount to %u.\n", texture
, refcount
);
418 wined3d_texture_cleanup(texture
);
419 texture
->resource
.parent_ops
->wined3d_object_destroyed(texture
->resource
.parent
);
420 HeapFree(GetProcessHeap(), 0, texture
);
426 struct wined3d_resource
* CDECL
wined3d_texture_get_resource(struct wined3d_texture
*texture
)
428 TRACE("texture %p.\n", texture
);
430 return &texture
->resource
;
433 static BOOL
color_key_equal(const struct wined3d_color_key
*c1
, struct wined3d_color_key
*c2
)
435 return c1
->color_space_low_value
== c2
->color_space_low_value
436 && c1
->color_space_high_value
== c2
->color_space_high_value
;
439 /* Context activation is done by the caller */
440 void wined3d_texture_load(struct wined3d_texture
*texture
,
441 struct wined3d_context
*context
, BOOL srgb
)
443 UINT sub_count
= texture
->level_count
* texture
->layer_count
;
444 const struct wined3d_d3d_info
*d3d_info
= context
->d3d_info
;
448 TRACE("texture %p, context %p, srgb %#x.\n", texture
, context
, srgb
);
450 if (!needs_separate_srgb_gl_texture(context
))
454 flag
= WINED3D_TEXTURE_SRGB_VALID
;
456 flag
= WINED3D_TEXTURE_RGB_VALID
;
458 if (!d3d_info
->shader_color_key
459 && (!(texture
->async
.flags
& WINED3D_TEXTURE_ASYNC_COLOR_KEY
)
460 != !(texture
->async
.color_key_flags
& WINED3D_CKEY_SRC_BLT
)
461 || (texture
->async
.flags
& WINED3D_TEXTURE_ASYNC_COLOR_KEY
462 && !color_key_equal(&texture
->async
.gl_color_key
, &texture
->async
.src_blt_color_key
))))
464 unsigned int sub_count
= texture
->level_count
* texture
->layer_count
;
467 TRACE("Reloading because of color key value change.\n");
468 for (i
= 0; i
< sub_count
; i
++)
469 texture
->texture_ops
->texture_sub_resource_add_dirty_region(texture
->sub_resources
[i
], NULL
);
470 wined3d_texture_set_dirty(texture
);
472 texture
->async
.gl_color_key
= texture
->async
.src_blt_color_key
;
475 if (texture
->flags
& flag
)
477 TRACE("Texture %p not dirty, nothing to do.\n", texture
);
481 /* Reload the surfaces if the texture is marked dirty. */
482 for (i
= 0; i
< sub_count
; ++i
)
484 texture
->texture_ops
->texture_sub_resource_load(texture
->sub_resources
[i
], context
, srgb
);
486 texture
->flags
|= flag
;
489 void CDECL
wined3d_texture_preload(struct wined3d_texture
*texture
)
491 struct wined3d_context
*context
;
492 context
= context_acquire(texture
->resource
.device
, NULL
);
493 wined3d_texture_load(texture
, context
, texture
->flags
& WINED3D_TEXTURE_IS_SRGB
);
494 context_release(context
);
497 void * CDECL
wined3d_texture_get_parent(const struct wined3d_texture
*texture
)
499 TRACE("texture %p.\n", texture
);
501 return texture
->resource
.parent
;
504 void CDECL
wined3d_texture_get_pitch(const struct wined3d_texture
*texture
,
505 unsigned int level
, unsigned int *row_pitch
, unsigned int *slice_pitch
)
507 const struct wined3d_resource
*resource
= &texture
->resource
;
508 unsigned int width
= max(1, texture
->resource
.width
>> level
);
509 unsigned int height
= max(1, texture
->resource
.height
>> level
);
511 if (texture
->row_pitch
)
513 *row_pitch
= texture
->row_pitch
;
514 *slice_pitch
= texture
->slice_pitch
;
518 wined3d_format_calculate_pitch(resource
->format
, resource
->device
->surface_alignment
,
519 width
, height
, row_pitch
, slice_pitch
);
522 DWORD CDECL
wined3d_texture_set_lod(struct wined3d_texture
*texture
, DWORD lod
)
524 DWORD old
= texture
->lod
;
526 TRACE("texture %p, lod %u.\n", texture
, lod
);
528 /* The d3d9:texture test shows that SetLOD is ignored on non-managed
529 * textures. The call always returns 0, and GetLOD always returns 0. */
530 if (texture
->resource
.pool
!= WINED3D_POOL_MANAGED
)
532 TRACE("Ignoring SetLOD on %s texture, returning 0.\n", debug_d3dpool(texture
->resource
.pool
));
536 if (lod
>= texture
->level_count
)
537 lod
= texture
->level_count
- 1;
539 if (texture
->lod
!= lod
)
543 texture
->texture_rgb
.base_level
= ~0u;
544 texture
->texture_srgb
.base_level
= ~0u;
545 if (texture
->resource
.bind_count
)
546 device_invalidate_state(texture
->resource
.device
, STATE_SAMPLER(texture
->sampler
));
552 DWORD CDECL
wined3d_texture_get_lod(const struct wined3d_texture
*texture
)
554 TRACE("texture %p, returning %u.\n", texture
, texture
->lod
);
559 DWORD CDECL
wined3d_texture_get_level_count(const struct wined3d_texture
*texture
)
561 TRACE("texture %p, returning %u.\n", texture
, texture
->level_count
);
563 return texture
->level_count
;
566 HRESULT CDECL
wined3d_texture_set_autogen_filter_type(struct wined3d_texture
*texture
,
567 enum wined3d_texture_filter_type filter_type
)
569 FIXME("texture %p, filter_type %s stub!\n", texture
, debug_d3dtexturefiltertype(filter_type
));
571 if (!(texture
->resource
.usage
& WINED3DUSAGE_AUTOGENMIPMAP
))
573 WARN("Texture doesn't have AUTOGENMIPMAP usage.\n");
574 return WINED3DERR_INVALIDCALL
;
577 texture
->filter_type
= filter_type
;
582 enum wined3d_texture_filter_type CDECL
wined3d_texture_get_autogen_filter_type(const struct wined3d_texture
*texture
)
584 TRACE("texture %p.\n", texture
);
586 return texture
->filter_type
;
589 HRESULT CDECL
wined3d_texture_set_color_key(struct wined3d_texture
*texture
,
590 DWORD flags
, const struct wined3d_color_key
*color_key
)
592 struct wined3d_device
*device
= texture
->resource
.device
;
593 static const DWORD all_flags
= WINED3D_CKEY_DST_BLT
| WINED3D_CKEY_DST_OVERLAY
594 | WINED3D_CKEY_SRC_BLT
| WINED3D_CKEY_SRC_OVERLAY
;
596 TRACE("texture %p, flags %#x, color_key %p.\n", texture
, flags
, color_key
);
598 if (flags
& ~all_flags
)
600 WARN("Invalid flags passed, returning WINED3DERR_INVALIDCALL.\n");
601 return WINED3DERR_INVALIDCALL
;
604 wined3d_cs_emit_set_color_key(device
->cs
, texture
, flags
, color_key
);
609 HRESULT CDECL
wined3d_texture_update_desc(struct wined3d_texture
*texture
, UINT width
, UINT height
,
610 enum wined3d_format_id format_id
, enum wined3d_multisample_type multisample_type
,
611 UINT multisample_quality
, void *mem
, UINT pitch
)
613 struct wined3d_device
*device
= texture
->resource
.device
;
614 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
615 const struct wined3d_format
*format
= wined3d_get_format(gl_info
, format_id
);
616 UINT resource_size
= wined3d_format_calculate_size(format
, device
->surface_alignment
, width
, height
, 1);
617 struct wined3d_surface
*surface
;
619 TRACE("texture %p, width %u, height %u, format %s, multisample_type %#x, multisample_quality %u, "
620 "mem %p, pitch %u.\n",
621 texture
, width
, height
, debug_d3dformat(format_id
), multisample_type
, multisample_type
, mem
, pitch
);
624 return WINED3DERR_INVALIDCALL
;
626 if (texture
->level_count
* texture
->layer_count
> 1)
628 WARN("Texture has multiple sub-resources, not supported.\n");
629 return WINED3DERR_INVALIDCALL
;
632 if (texture
->resource
.type
== WINED3D_RTYPE_TEXTURE_3D
)
634 WARN("Not supported on 3D textures.\n");
635 return WINED3DERR_INVALIDCALL
;
638 /* We have no way of supporting a pitch that is not a multiple of the pixel
639 * byte width short of uploading the texture row-by-row.
640 * Fortunately that's not an issue since D3D9Ex doesn't allow a custom pitch
641 * for user-memory textures (it always expects packed data) while DirectDraw
642 * requires a 4-byte aligned pitch and doesn't support texture formats
643 * larger than 4 bytes per pixel nor any format using 3 bytes per pixel.
644 * This check is here to verify that the assumption holds. */
645 if (pitch
% texture
->resource
.format
->byte_count
)
647 WARN("Pitch unsupported, not a multiple of the texture format byte width.\n");
648 return WINED3DERR_INVALIDCALL
;
651 surface
= surface_from_resource(texture
->sub_resources
[0]);
652 if (surface
->resource
.map_count
|| (surface
->flags
& SFLAG_DCINUSE
))
654 WARN("Surface is mapped or the DC is in use.\n");
655 return WINED3DERR_INVALIDCALL
;
658 if (device
->d3d_initialized
)
659 texture
->resource
.resource_ops
->resource_unload(&texture
->resource
);
661 texture
->resource
.format
= format
;
662 texture
->resource
.multisample_type
= multisample_type
;
663 texture
->resource
.multisample_quality
= multisample_quality
;
664 texture
->resource
.width
= width
;
665 texture
->resource
.height
= height
;
667 texture
->user_memory
= mem
;
668 if ((texture
->row_pitch
= pitch
))
669 texture
->slice_pitch
= height
* pitch
;
671 /* User memory surfaces don't have the regular surface alignment. */
672 wined3d_format_calculate_pitch(format
, 1, width
, height
,
673 &texture
->row_pitch
, &texture
->slice_pitch
);
675 return wined3d_surface_update_desc(surface
, gl_info
);
678 void wined3d_texture_prepare_texture(struct wined3d_texture
*texture
, struct wined3d_context
*context
, BOOL srgb
)
680 DWORD alloc_flag
= srgb
? WINED3D_TEXTURE_SRGB_ALLOCATED
: WINED3D_TEXTURE_RGB_ALLOCATED
;
681 const struct wined3d_d3d_info
*d3d_info
= context
->d3d_info
;
683 if (!d3d_info
->shader_color_key
684 && !(texture
->async
.flags
& WINED3D_TEXTURE_ASYNC_COLOR_KEY
)
685 != !(texture
->async
.color_key_flags
& WINED3D_CKEY_SRC_BLT
))
687 wined3d_texture_force_reload(texture
);
689 if (texture
->async
.color_key_flags
& WINED3D_CKEY_SRC_BLT
)
690 texture
->async
.flags
|= WINED3D_TEXTURE_ASYNC_COLOR_KEY
;
693 if (texture
->flags
& alloc_flag
)
696 texture
->texture_ops
->texture_prepare_texture(texture
, context
, srgb
);
697 texture
->flags
|= alloc_flag
;
700 void wined3d_texture_force_reload(struct wined3d_texture
*texture
)
702 unsigned int sub_count
= texture
->level_count
* texture
->layer_count
;
705 texture
->flags
&= ~(WINED3D_TEXTURE_RGB_ALLOCATED
| WINED3D_TEXTURE_SRGB_ALLOCATED
706 | WINED3D_TEXTURE_CONVERTED
);
707 texture
->async
.flags
&= ~WINED3D_TEXTURE_ASYNC_COLOR_KEY
;
708 for (i
= 0; i
< sub_count
; ++i
)
710 texture
->texture_ops
->texture_sub_resource_invalidate_location(texture
->sub_resources
[i
],
711 WINED3D_LOCATION_TEXTURE_RGB
| WINED3D_LOCATION_TEXTURE_SRGB
);
715 void CDECL
wined3d_texture_generate_mipmaps(struct wined3d_texture
*texture
)
717 /* TODO: Implement filters using GL_SGI_generate_mipmaps. */
718 FIXME("texture %p stub!\n", texture
);
721 struct wined3d_resource
* CDECL
wined3d_texture_get_sub_resource(const struct wined3d_texture
*texture
,
722 UINT sub_resource_idx
)
724 UINT sub_count
= texture
->level_count
* texture
->layer_count
;
726 TRACE("texture %p, sub_resource_idx %u.\n", texture
, sub_resource_idx
);
728 if (sub_resource_idx
>= sub_count
)
730 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx
, sub_count
);
734 return texture
->sub_resources
[sub_resource_idx
];
737 HRESULT CDECL
wined3d_texture_add_dirty_region(struct wined3d_texture
*texture
,
738 UINT layer
, const struct wined3d_box
*dirty_region
)
740 struct wined3d_resource
*sub_resource
;
742 TRACE("texture %p, layer %u, dirty_region %s.\n", texture
, layer
, debug_box(dirty_region
));
744 if (!(sub_resource
= wined3d_texture_get_sub_resource(texture
, layer
* texture
->level_count
)))
746 WARN("Failed to get sub-resource.\n");
747 return WINED3DERR_INVALIDCALL
;
750 texture
->texture_ops
->texture_sub_resource_add_dirty_region(sub_resource
, dirty_region
);
755 static HRESULT
wined3d_texture_upload_data(struct wined3d_texture
*texture
,
756 const struct wined3d_sub_resource_data
*data
)
758 unsigned int sub_count
= texture
->level_count
* texture
->layer_count
;
759 struct wined3d_context
*context
;
762 for (i
= 0; i
< sub_count
; ++i
)
766 WARN("Invalid sub-resource data specified for sub-resource %u.\n", i
);
771 context
= context_acquire(texture
->resource
.device
, NULL
);
773 wined3d_texture_prepare_texture(texture
, context
, FALSE
);
774 wined3d_texture_bind_and_dirtify(texture
, context
, FALSE
);
776 for (i
= 0; i
< sub_count
; ++i
)
778 struct wined3d_resource
*sub_resource
= texture
->sub_resources
[i
];
780 texture
->texture_ops
->texture_sub_resource_upload_data(sub_resource
, context
, &data
[i
]);
781 texture
->texture_ops
->texture_sub_resource_validate_location(sub_resource
, WINED3D_LOCATION_TEXTURE_RGB
);
782 texture
->texture_ops
->texture_sub_resource_invalidate_location(sub_resource
, ~WINED3D_LOCATION_TEXTURE_RGB
);
785 context_release(context
);
790 static void texture2d_sub_resource_load(struct wined3d_resource
*sub_resource
,
791 struct wined3d_context
*context
, BOOL srgb
)
793 surface_load(surface_from_resource(sub_resource
), context
, srgb
);
796 static void texture2d_sub_resource_add_dirty_region(struct wined3d_resource
*sub_resource
,
797 const struct wined3d_box
*dirty_region
)
799 struct wined3d_surface
*surface
= surface_from_resource(sub_resource
);
800 struct wined3d_context
*context
;
802 surface_prepare_map_memory(surface
);
803 context
= context_acquire(surface
->resource
.device
, NULL
);
804 surface_load_location(surface
, context
, surface
->resource
.map_binding
);
805 context_release(context
);
806 surface_invalidate_location(surface
, ~surface
->resource
.map_binding
);
809 static void texture2d_sub_resource_cleanup(struct wined3d_resource
*sub_resource
)
811 struct wined3d_surface
*surface
= surface_from_resource(sub_resource
);
813 wined3d_surface_destroy(surface
);
816 static void texture2d_sub_resource_invalidate_location(struct wined3d_resource
*sub_resource
, DWORD location
)
818 struct wined3d_surface
*surface
= surface_from_resource(sub_resource
);
820 surface_invalidate_location(surface
, location
);
823 static void texture2d_sub_resource_validate_location(struct wined3d_resource
*sub_resource
, DWORD location
)
825 struct wined3d_surface
*surface
= surface_from_resource(sub_resource
);
827 surface_validate_location(surface
, location
);
830 static void texture2d_sub_resource_upload_data(struct wined3d_resource
*sub_resource
,
831 const struct wined3d_context
*context
, const struct wined3d_sub_resource_data
*data
)
833 struct wined3d_surface
*surface
= surface_from_resource(sub_resource
);
834 static const POINT dst_point
= {0, 0};
835 struct wined3d_const_bo_address addr
;
840 src_rect
.right
= surface
->resource
.width
;
841 src_rect
.bottom
= surface
->resource
.height
;
843 addr
.buffer_object
= 0;
844 addr
.addr
= data
->data
;
846 wined3d_surface_upload_data(surface
, context
->gl_info
, surface
->container
->resource
.format
,
847 &src_rect
, data
->row_pitch
, &dst_point
, FALSE
, &addr
);
850 /* Context activation is done by the caller. */
851 static void texture2d_prepare_texture(struct wined3d_texture
*texture
, struct wined3d_context
*context
, BOOL srgb
)
853 UINT sub_count
= texture
->level_count
* texture
->layer_count
;
854 const struct wined3d_format
*format
= texture
->resource
.format
;
855 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
856 const struct wined3d_color_key_conversion
*conversion
;
860 TRACE("texture %p, context %p, format %s.\n", texture
, context
, debug_d3dformat(format
->id
));
864 texture
->flags
|= WINED3D_TEXTURE_CONVERTED
;
866 else if ((conversion
= wined3d_format_get_color_key_conversion(texture
, TRUE
)))
868 texture
->flags
|= WINED3D_TEXTURE_CONVERTED
;
869 format
= wined3d_get_format(gl_info
, conversion
->dst_format
);
870 TRACE("Using format %s for color key conversion.\n", debug_d3dformat(format
->id
));
873 wined3d_texture_bind_and_dirtify(texture
, context
, srgb
);
876 internal
= format
->glGammaInternal
;
877 else if (texture
->resource
.usage
& WINED3DUSAGE_RENDERTARGET
878 && wined3d_resource_is_offscreen(&texture
->resource
))
879 internal
= format
->rtInternal
;
881 internal
= format
->glInternal
;
884 FIXME("No GL internal format for format %s.\n", debug_d3dformat(format
->id
));
886 TRACE("internal %#x, format %#x, type %#x.\n", internal
, format
->glFormat
, format
->glType
);
888 for (i
= 0; i
< sub_count
; ++i
)
890 struct wined3d_surface
*surface
= surface_from_resource(texture
->sub_resources
[i
]);
891 GLsizei height
= surface
->pow2Height
;
892 GLsizei width
= surface
->pow2Width
;
894 if (texture
->resource
.format_flags
& WINED3DFMT_FLAG_HEIGHT_SCALE
)
896 height
*= format
->height_scale
.numerator
;
897 height
/= format
->height_scale
.denominator
;
900 TRACE("surface %p, target %#x, level %d, width %d, height %d.\n",
901 surface
, surface
->texture_target
, surface
->texture_level
, width
, height
);
903 gl_info
->gl_ops
.gl
.p_glTexImage2D(surface
->texture_target
, surface
->texture_level
,
904 internal
, width
, height
, 0, format
->glFormat
, format
->glType
, NULL
);
905 checkGLcall("glTexImage2D");
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 HRESULT
texture2d_resource_sub_resource_map(struct wined3d_resource
*resource
, unsigned int sub_resource_idx
,
950 struct wined3d_map_desc
*map_desc
, const struct wined3d_box
*box
, DWORD flags
)
952 struct wined3d_resource
*sub_resource
;
954 if (!(sub_resource
= wined3d_texture_get_sub_resource(wined3d_texture_from_resource(resource
), sub_resource_idx
)))
957 return wined3d_surface_map(surface_from_resource(sub_resource
), map_desc
, box
, flags
);
960 static HRESULT
texture2d_resource_sub_resource_unmap(struct wined3d_resource
*resource
, unsigned int sub_resource_idx
)
962 struct wined3d_resource
*sub_resource
;
964 if (!(sub_resource
= wined3d_texture_get_sub_resource(wined3d_texture_from_resource(resource
), sub_resource_idx
)))
967 return wined3d_surface_unmap(surface_from_resource(sub_resource
));
970 static const struct wined3d_resource_ops texture2d_resource_ops
=
972 texture_resource_incref
,
973 texture_resource_decref
,
974 wined3d_texture_unload
,
975 texture2d_resource_sub_resource_map
,
976 texture2d_resource_sub_resource_unmap
,
979 static HRESULT
texture_init(struct wined3d_texture
*texture
, const struct wined3d_resource_desc
*desc
,
980 UINT level_count
, DWORD flags
, struct wined3d_device
*device
, void *parent
,
981 const struct wined3d_parent_ops
*parent_ops
)
983 unsigned int layer_count
= desc
->usage
& WINED3DUSAGE_LEGACY_CUBEMAP
? 6 : 1;
984 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
985 struct wined3d_resource_desc surface_desc
;
986 UINT pow2_width
, pow2_height
;
990 /* TODO: It should only be possible to create textures for formats
991 * that are reported as supported. */
992 if (WINED3DFMT_UNKNOWN
>= desc
->format
)
994 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture
);
995 return WINED3DERR_INVALIDCALL
;
998 /* Non-power2 support. */
999 if (gl_info
->supported
[ARB_TEXTURE_NON_POWER_OF_TWO
])
1001 pow2_width
= desc
->width
;
1002 pow2_height
= desc
->height
;
1006 /* Find the nearest pow2 match. */
1007 pow2_width
= pow2_height
= 1;
1008 while (pow2_width
< desc
->width
)
1010 while (pow2_height
< desc
->height
)
1013 if (pow2_width
!= desc
->width
|| pow2_height
!= desc
->height
)
1015 /* level_count == 0 returns an error as well */
1016 if (level_count
!= 1 || desc
->usage
& WINED3DUSAGE_LEGACY_CUBEMAP
)
1018 if (desc
->pool
== WINED3D_POOL_SCRATCH
)
1020 WARN("Creating a scratch mipmapped/cube NPOT texture despite lack of HW support.\n");
1024 WARN("Attempted to create a mipmapped/cube NPOT texture without unconditional NPOT support.\n");
1025 return WINED3DERR_INVALIDCALL
;
1031 /* Calculate levels for mip mapping. */
1032 if (desc
->usage
& WINED3DUSAGE_AUTOGENMIPMAP
)
1034 if (!gl_info
->supported
[SGIS_GENERATE_MIPMAP
])
1036 WARN("No mipmap generation support, returning WINED3DERR_INVALIDCALL.\n");
1037 return WINED3DERR_INVALIDCALL
;
1040 if (level_count
!= 1)
1042 WARN("WINED3DUSAGE_AUTOGENMIPMAP is set, and level count != 1, returning WINED3DERR_INVALIDCALL.\n");
1043 return WINED3DERR_INVALIDCALL
;
1047 if (FAILED(hr
= wined3d_texture_init(texture
, &texture2d_ops
, layer_count
, level_count
, desc
,
1048 flags
, device
, parent
, parent_ops
, &texture2d_resource_ops
)))
1050 WARN("Failed to initialize texture, returning %#x.\n", hr
);
1054 /* Precalculated scaling for 'faked' non power of two texture coords. */
1055 if (texture
->resource
.gl_type
== WINED3D_GL_RES_TYPE_TEX_RECT
)
1057 texture
->pow2_matrix
[0] = (float)desc
->width
;
1058 texture
->pow2_matrix
[5] = (float)desc
->height
;
1059 texture
->pow2_matrix
[10] = 1.0f
;
1060 texture
->pow2_matrix
[15] = 1.0f
;
1061 texture
->target
= GL_TEXTURE_RECTANGLE_ARB
;
1062 texture
->flags
|= WINED3D_TEXTURE_COND_NP2
;
1063 texture
->flags
&= ~(WINED3D_TEXTURE_POW2_MAT_IDENT
| WINED3D_TEXTURE_NORMALIZED_COORDS
);
1067 if (desc
->usage
& WINED3DUSAGE_LEGACY_CUBEMAP
)
1068 texture
->target
= GL_TEXTURE_CUBE_MAP_ARB
;
1070 texture
->target
= GL_TEXTURE_2D
;
1071 if (desc
->width
== pow2_width
&& desc
->height
== pow2_height
)
1073 texture
->pow2_matrix
[0] = 1.0f
;
1074 texture
->pow2_matrix
[5] = 1.0f
;
1076 else if (gl_info
->supported
[WINED3D_GL_NORMALIZED_TEXRECT
])
1078 texture
->pow2_matrix
[0] = 1.0f
;
1079 texture
->pow2_matrix
[5] = 1.0f
;
1080 texture
->flags
|= WINED3D_TEXTURE_COND_NP2
;
1084 texture
->pow2_matrix
[0] = (((float)desc
->width
) / ((float)pow2_width
));
1085 texture
->pow2_matrix
[5] = (((float)desc
->height
) / ((float)pow2_height
));
1086 texture
->flags
&= ~WINED3D_TEXTURE_POW2_MAT_IDENT
;
1088 texture
->pow2_matrix
[10] = 1.0f
;
1089 texture
->pow2_matrix
[15] = 1.0f
;
1091 TRACE("xf(%f) yf(%f)\n", texture
->pow2_matrix
[0], texture
->pow2_matrix
[5]);
1093 /* Generate all the surfaces. */
1094 surface_desc
= *desc
;
1095 surface_desc
.resource_type
= WINED3D_RTYPE_SURFACE
;
1096 for (i
= 0; i
< texture
->level_count
; ++i
)
1098 for (j
= 0; j
< texture
->layer_count
; ++j
)
1100 static const GLenum cube_targets
[6] =
1102 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
,
1103 GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
,
1104 GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
,
1105 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
,
1106 GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
,
1107 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
,
1109 GLenum target
= desc
->usage
& WINED3DUSAGE_LEGACY_CUBEMAP
? cube_targets
[j
] : texture
->target
;
1110 unsigned int idx
= j
* texture
->level_count
+ i
;
1111 struct wined3d_surface
*surface
;
1113 if (FAILED(hr
= wined3d_surface_create(texture
, &surface_desc
,
1114 target
, i
, j
, flags
, &surface
)))
1116 WARN("Failed to create surface, hr %#x.\n", hr
);
1117 wined3d_texture_cleanup(texture
);
1121 texture
->sub_resources
[idx
] = &surface
->resource
;
1122 TRACE("Created surface level %u @ %p.\n", i
, surface
);
1124 /* Calculate the next mipmap level. */
1125 surface_desc
.width
= max(1, surface_desc
.width
>> 1);
1126 surface_desc
.height
= max(1, surface_desc
.height
>> 1);
1132 static void texture3d_sub_resource_load(struct wined3d_resource
*sub_resource
,
1133 struct wined3d_context
*context
, BOOL srgb
)
1135 wined3d_volume_load(volume_from_resource(sub_resource
), context
, srgb
);
1138 static void texture3d_sub_resource_add_dirty_region(struct wined3d_resource
*sub_resource
,
1139 const struct wined3d_box
*dirty_region
)
1141 wined3d_texture_set_dirty(volume_from_resource(sub_resource
)->container
);
1144 static void texture3d_sub_resource_cleanup(struct wined3d_resource
*sub_resource
)
1146 struct wined3d_volume
*volume
= volume_from_resource(sub_resource
);
1148 wined3d_volume_destroy(volume
);
1151 static void texture3d_sub_resource_invalidate_location(struct wined3d_resource
*sub_resource
, DWORD location
)
1153 struct wined3d_volume
*volume
= volume_from_resource(sub_resource
);
1155 wined3d_volume_invalidate_location(volume
, location
);
1158 static void texture3d_sub_resource_validate_location(struct wined3d_resource
*sub_resource
, DWORD location
)
1160 struct wined3d_volume
*volume
= volume_from_resource(sub_resource
);
1162 wined3d_volume_validate_location(volume
, location
);
1165 static void texture3d_sub_resource_upload_data(struct wined3d_resource
*sub_resource
,
1166 const struct wined3d_context
*context
, const struct wined3d_sub_resource_data
*data
)
1168 struct wined3d_volume
*volume
= volume_from_resource(sub_resource
);
1169 struct wined3d_const_bo_address addr
;
1170 unsigned int row_pitch
, slice_pitch
;
1172 wined3d_texture_get_pitch(volume
->container
, volume
->texture_level
, &row_pitch
, &slice_pitch
);
1173 if (row_pitch
!= data
->row_pitch
|| slice_pitch
!= data
->slice_pitch
)
1174 FIXME("Ignoring row/slice pitch (%u/%u).\n", data
->row_pitch
, data
->slice_pitch
);
1176 addr
.buffer_object
= 0;
1177 addr
.addr
= data
->data
;
1179 wined3d_volume_upload_data(volume
, context
, &addr
);
1182 static void texture3d_prepare_texture(struct wined3d_texture
*texture
, struct wined3d_context
*context
, BOOL srgb
)
1184 unsigned int sub_count
= texture
->level_count
* texture
->layer_count
;
1185 const struct wined3d_format
*format
= texture
->resource
.format
;
1186 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1189 wined3d_texture_bind_and_dirtify(texture
, context
, srgb
);
1191 for (i
= 0; i
< sub_count
; ++i
)
1193 struct wined3d_volume
*volume
= volume_from_resource(texture
->sub_resources
[i
]);
1195 GL_EXTCALL(glTexImage3D(GL_TEXTURE_3D
, volume
->texture_level
,
1196 srgb
? format
->glGammaInternal
: format
->glInternal
,
1197 volume
->resource
.width
, volume
->resource
.height
, volume
->resource
.depth
,
1198 0, format
->glFormat
, format
->glType
, NULL
));
1199 checkGLcall("glTexImage3D");
1203 static const struct wined3d_texture_ops texture3d_ops
=
1205 texture3d_sub_resource_load
,
1206 texture3d_sub_resource_add_dirty_region
,
1207 texture3d_sub_resource_cleanup
,
1208 texture3d_sub_resource_invalidate_location
,
1209 texture3d_sub_resource_validate_location
,
1210 texture3d_sub_resource_upload_data
,
1211 texture3d_prepare_texture
,
1214 static HRESULT
texture3d_resource_sub_resource_map(struct wined3d_resource
*resource
, unsigned int sub_resource_idx
,
1215 struct wined3d_map_desc
*map_desc
, const struct wined3d_box
*box
, DWORD flags
)
1217 struct wined3d_resource
*sub_resource
;
1219 if (!(sub_resource
= wined3d_texture_get_sub_resource(wined3d_texture_from_resource(resource
), sub_resource_idx
)))
1220 return E_INVALIDARG
;
1222 return wined3d_volume_map(volume_from_resource(sub_resource
), map_desc
, box
, flags
);
1225 static HRESULT
texture3d_resource_sub_resource_unmap(struct wined3d_resource
*resource
, unsigned int sub_resource_idx
)
1227 struct wined3d_resource
*sub_resource
;
1229 if (!(sub_resource
= wined3d_texture_get_sub_resource(wined3d_texture_from_resource(resource
), sub_resource_idx
)))
1230 return E_INVALIDARG
;
1232 return wined3d_volume_unmap(volume_from_resource(sub_resource
));
1235 static const struct wined3d_resource_ops texture3d_resource_ops
=
1237 texture_resource_incref
,
1238 texture_resource_decref
,
1239 wined3d_texture_unload
,
1240 texture3d_resource_sub_resource_map
,
1241 texture3d_resource_sub_resource_unmap
,
1244 static HRESULT
volumetexture_init(struct wined3d_texture
*texture
, const struct wined3d_resource_desc
*desc
,
1245 UINT levels
, struct wined3d_device
*device
, void *parent
, const struct wined3d_parent_ops
*parent_ops
)
1247 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
1248 struct wined3d_resource_desc volume_desc
;
1252 /* TODO: It should only be possible to create textures for formats
1253 * that are reported as supported. */
1254 if (WINED3DFMT_UNKNOWN
>= desc
->format
)
1256 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture
);
1257 return WINED3DERR_INVALIDCALL
;
1260 if (!gl_info
->supported
[EXT_TEXTURE3D
])
1262 WARN("(%p) : Texture cannot be created - no volume texture support.\n", texture
);
1263 return WINED3DERR_INVALIDCALL
;
1266 /* Calculate levels for mip mapping. */
1267 if (desc
->usage
& WINED3DUSAGE_AUTOGENMIPMAP
)
1269 if (!gl_info
->supported
[SGIS_GENERATE_MIPMAP
])
1271 WARN("No mipmap generation support, returning D3DERR_INVALIDCALL.\n");
1272 return WINED3DERR_INVALIDCALL
;
1277 WARN("WINED3DUSAGE_AUTOGENMIPMAP is set, and level count != 1, returning D3DERR_INVALIDCALL.\n");
1278 return WINED3DERR_INVALIDCALL
;
1282 if (!gl_info
->supported
[ARB_TEXTURE_NON_POWER_OF_TWO
])
1284 UINT pow2_w
, pow2_h
, pow2_d
;
1286 while (pow2_w
< desc
->width
)
1289 while (pow2_h
< desc
->height
)
1292 while (pow2_d
< desc
->depth
)
1295 if (pow2_w
!= desc
->width
|| pow2_h
!= desc
->height
|| pow2_d
!= desc
->depth
)
1297 if (desc
->pool
== WINED3D_POOL_SCRATCH
)
1299 WARN("Creating a scratch NPOT volume texture despite lack of HW support.\n");
1303 WARN("Attempted to create a NPOT volume texture (%u, %u, %u) without GL support.\n",
1304 desc
->width
, desc
->height
, desc
->depth
);
1305 return WINED3DERR_INVALIDCALL
;
1310 if (FAILED(hr
= wined3d_texture_init(texture
, &texture3d_ops
, 1, levels
, desc
,
1311 0, device
, parent
, parent_ops
, &texture3d_resource_ops
)))
1313 WARN("Failed to initialize texture, returning %#x.\n", hr
);
1317 texture
->pow2_matrix
[0] = 1.0f
;
1318 texture
->pow2_matrix
[5] = 1.0f
;
1319 texture
->pow2_matrix
[10] = 1.0f
;
1320 texture
->pow2_matrix
[15] = 1.0f
;
1321 texture
->target
= GL_TEXTURE_3D
;
1323 /* Generate all the surfaces. */
1324 volume_desc
= *desc
;
1325 volume_desc
.resource_type
= WINED3D_RTYPE_VOLUME
;
1326 for (i
= 0; i
< texture
->level_count
; ++i
)
1328 struct wined3d_volume
*volume
;
1330 if (FAILED(hr
= wined3d_volume_create(texture
, &volume_desc
, i
, &volume
)))
1332 ERR("Creating a volume for the volume texture failed, hr %#x.\n", hr
);
1333 wined3d_texture_cleanup(texture
);
1337 texture
->sub_resources
[i
] = &volume
->resource
;
1339 /* Calculate the next mipmap level. */
1340 volume_desc
.width
= max(1, volume_desc
.width
>> 1);
1341 volume_desc
.height
= max(1, volume_desc
.height
>> 1);
1342 volume_desc
.depth
= max(1, volume_desc
.depth
>> 1);
1348 HRESULT CDECL
wined3d_texture_blt(struct wined3d_texture
*dst_texture
, unsigned int dst_sub_resource_idx
,
1349 const RECT
*dst_rect
, struct wined3d_texture
*src_texture
, unsigned int src_sub_resource_idx
,
1350 const RECT
*src_rect
, DWORD flags
, const WINEDDBLTFX
*fx
, enum wined3d_texture_filter_type filter
)
1352 struct wined3d_resource
*dst_resource
, *src_resource
= NULL
;
1354 TRACE("dst_texture %p, dst_sub_resource_idx %u, dst_rect %s, src_texture %p, "
1355 "src_sub_resource_idx %u, src_rect %s, flags %#x, fx %p, filter %s.\n",
1356 dst_texture
, dst_sub_resource_idx
, wine_dbgstr_rect(dst_rect
), src_texture
,
1357 src_sub_resource_idx
, wine_dbgstr_rect(src_rect
), flags
, fx
, debug_d3dtexturefiltertype(filter
));
1359 if (!(dst_resource
= wined3d_texture_get_sub_resource(dst_texture
, dst_sub_resource_idx
))
1360 || dst_resource
->type
!= WINED3D_RTYPE_SURFACE
)
1361 return WINED3DERR_INVALIDCALL
;
1365 if (!(src_resource
= wined3d_texture_get_sub_resource(src_texture
, src_sub_resource_idx
))
1366 || src_resource
->type
!= WINED3D_RTYPE_SURFACE
)
1367 return WINED3DERR_INVALIDCALL
;
1370 return wined3d_surface_blt(surface_from_resource(dst_resource
), dst_rect
,
1371 src_resource
? surface_from_resource(src_resource
) : NULL
, src_rect
, flags
, fx
, filter
);
1374 HRESULT CDECL
wined3d_texture_get_overlay_position(const struct wined3d_texture
*texture
,
1375 unsigned int sub_resource_idx
, LONG
*x
, LONG
*y
)
1377 struct wined3d_resource
*sub_resource
;
1378 struct wined3d_surface
*surface
;
1380 TRACE("texture %p, sub_resource_idx %u, x %p, y %p.\n", texture
, sub_resource_idx
, x
, y
);
1382 if (!(texture
->resource
.usage
& WINED3DUSAGE_OVERLAY
) || texture
->resource
.type
!= WINED3D_RTYPE_TEXTURE_2D
1383 || !(sub_resource
= wined3d_texture_get_sub_resource(texture
, sub_resource_idx
)))
1385 WARN("Invalid sub-resource specified.\n");
1386 return WINEDDERR_NOTAOVERLAYSURFACE
;
1389 surface
= surface_from_resource(sub_resource
);
1390 if (!surface
->overlay_dest
)
1392 TRACE("Overlay not visible.\n");
1395 return WINEDDERR_OVERLAYNOTVISIBLE
;
1398 *x
= surface
->overlay_destrect
.left
;
1399 *y
= surface
->overlay_destrect
.top
;
1401 TRACE("Returning position %d, %d.\n", *x
, *y
);
1406 HRESULT CDECL
wined3d_texture_set_overlay_position(struct wined3d_texture
*texture
,
1407 unsigned int sub_resource_idx
, LONG x
, LONG y
)
1409 struct wined3d_resource
*sub_resource
;
1410 struct wined3d_surface
*surface
;
1413 TRACE("texture %p, sub_resource_idx %u, x %d, y %d.\n", texture
, sub_resource_idx
, x
, y
);
1415 if (!(texture
->resource
.usage
& WINED3DUSAGE_OVERLAY
) || texture
->resource
.type
!= WINED3D_RTYPE_TEXTURE_2D
1416 || !(sub_resource
= wined3d_texture_get_sub_resource(texture
, sub_resource_idx
)))
1418 WARN("Invalid sub-resource specified.\n");
1419 return WINEDDERR_NOTAOVERLAYSURFACE
;
1422 surface
= surface_from_resource(sub_resource
);
1423 w
= surface
->overlay_destrect
.right
- surface
->overlay_destrect
.left
;
1424 h
= surface
->overlay_destrect
.bottom
- surface
->overlay_destrect
.top
;
1425 surface
->overlay_destrect
.left
= x
;
1426 surface
->overlay_destrect
.top
= y
;
1427 surface
->overlay_destrect
.right
= x
+ w
;
1428 surface
->overlay_destrect
.bottom
= y
+ h
;
1433 HRESULT CDECL
wined3d_texture_update_overlay(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
,
1434 const RECT
*src_rect
, struct wined3d_texture
*dst_texture
, unsigned int dst_sub_resource_idx
,
1435 const RECT
*dst_rect
, DWORD flags
, const WINEDDOVERLAYFX
*fx
)
1437 struct wined3d_resource
*sub_resource
, *dst_sub_resource
;
1438 struct wined3d_surface
*surface
, *dst_surface
;
1440 TRACE("texture %p, sub_resource_idx %u, src_rect %s, dst_texture %p, "
1441 "dst_sub_resource_idx %u, dst_rect %s, flags %#x, fx %p.\n",
1442 texture
, sub_resource_idx
, wine_dbgstr_rect(src_rect
), dst_texture
,
1443 dst_sub_resource_idx
, wine_dbgstr_rect(dst_rect
), flags
, fx
);
1445 if (!(texture
->resource
.usage
& WINED3DUSAGE_OVERLAY
) || texture
->resource
.type
!= WINED3D_RTYPE_TEXTURE_2D
1446 || !(sub_resource
= wined3d_texture_get_sub_resource(texture
, sub_resource_idx
)))
1448 WARN("Invalid sub-resource specified.\n");
1449 return WINEDDERR_NOTAOVERLAYSURFACE
;
1452 if (!dst_texture
|| dst_texture
->resource
.type
!= WINED3D_RTYPE_TEXTURE_2D
1453 || !(dst_sub_resource
= wined3d_texture_get_sub_resource(dst_texture
, dst_sub_resource_idx
)))
1455 WARN("Invalid destination sub-resource specified.\n");
1456 return WINED3DERR_INVALIDCALL
;
1459 surface
= surface_from_resource(sub_resource
);
1461 surface
->overlay_srcrect
= *src_rect
;
1463 SetRect(&surface
->overlay_srcrect
, 0, 0, surface
->resource
.width
, surface
->resource
.height
);
1465 dst_surface
= surface_from_resource(dst_sub_resource
);
1467 surface
->overlay_destrect
= *dst_rect
;
1469 SetRect(&surface
->overlay_destrect
, 0, 0, dst_surface
->resource
.width
, dst_surface
->resource
.height
);
1471 if (surface
->overlay_dest
&& (surface
->overlay_dest
!= dst_surface
|| flags
& WINEDDOVER_HIDE
))
1473 surface
->overlay_dest
= NULL
;
1474 list_remove(&surface
->overlay_entry
);
1477 if (flags
& WINEDDOVER_SHOW
)
1479 if (surface
->overlay_dest
!= dst_surface
)
1481 surface
->overlay_dest
= dst_surface
;
1482 list_add_tail(&dst_surface
->overlays
, &surface
->overlay_entry
);
1485 else if (flags
& WINEDDOVER_HIDE
)
1487 /* Tests show that the rectangles are erased on hide. */
1488 surface
->overlay_srcrect
.left
= 0; surface
->overlay_srcrect
.top
= 0;
1489 surface
->overlay_srcrect
.right
= 0; surface
->overlay_srcrect
.bottom
= 0;
1490 surface
->overlay_destrect
.left
= 0; surface
->overlay_destrect
.top
= 0;
1491 surface
->overlay_destrect
.right
= 0; surface
->overlay_destrect
.bottom
= 0;
1492 surface
->overlay_dest
= NULL
;
1498 HRESULT CDECL
wined3d_texture_create(struct wined3d_device
*device
, const struct wined3d_resource_desc
*desc
,
1499 UINT level_count
, DWORD flags
, const struct wined3d_sub_resource_data
*data
, void *parent
,
1500 const struct wined3d_parent_ops
*parent_ops
, struct wined3d_texture
**texture
)
1502 struct wined3d_texture
*object
;
1505 TRACE("device %p, desc %p, level_count %u, flags %#x, data %p, parent %p, parent_ops %p, texture %p.\n",
1506 device
, desc
, level_count
, flags
, data
, parent
, parent_ops
, texture
);
1510 WARN("Invalid level count.\n");
1511 return WINED3DERR_INVALIDCALL
;
1514 if (desc
->multisample_type
!= WINED3D_MULTISAMPLE_NONE
)
1516 const struct wined3d_format
*format
= wined3d_get_format(&device
->adapter
->gl_info
, desc
->format
);
1518 if (desc
->multisample_type
== WINED3D_MULTISAMPLE_NON_MASKABLE
1519 && desc
->multisample_quality
>= wined3d_popcount(format
->multisample_types
))
1521 WARN("Unsupported quality level %u requested for WINED3D_MULTISAMPLE_NON_MASKABLE.\n",
1522 desc
->multisample_quality
);
1523 return WINED3DERR_NOTAVAILABLE
;
1525 if (desc
->multisample_type
!= WINED3D_MULTISAMPLE_NON_MASKABLE
1526 && (!(format
->multisample_types
& 1u << (desc
->multisample_type
- 1))
1527 || desc
->multisample_quality
))
1529 WARN("Unsupported multisample type %u quality %u requested.\n", desc
->multisample_type
,
1530 desc
->multisample_quality
);
1531 return WINED3DERR_NOTAVAILABLE
;
1535 if (!(object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
))))
1536 return E_OUTOFMEMORY
;
1538 switch (desc
->resource_type
)
1540 case WINED3D_RTYPE_TEXTURE_2D
:
1541 hr
= texture_init(object
, desc
, level_count
, flags
, device
, parent
, parent_ops
);
1544 case WINED3D_RTYPE_TEXTURE_3D
:
1545 hr
= volumetexture_init(object
, desc
, level_count
, device
, parent
, parent_ops
);
1549 ERR("Invalid resource type %s.\n", debug_d3dresourcetype(desc
->resource_type
));
1550 hr
= WINED3DERR_INVALIDCALL
;
1556 WARN("Failed to initialize texture, returning %#x.\n", hr
);
1557 HeapFree(GetProcessHeap(), 0, object
);
1561 /* FIXME: We'd like to avoid ever allocating system memory for the texture
1563 if (data
&& FAILED(hr
= wined3d_texture_upload_data(object
, data
)))
1565 wined3d_texture_cleanup(object
);
1566 HeapFree(GetProcessHeap(), 0, object
);
1570 TRACE("Created texture %p.\n", object
);
1576 HRESULT CDECL
wined3d_texture_get_dc(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
, HDC
*dc
)
1578 struct wined3d_device
*device
= texture
->resource
.device
;
1579 struct wined3d_context
*context
= NULL
;
1580 struct wined3d_resource
*sub_resource
;
1581 struct wined3d_surface
*surface
;
1584 TRACE("texture %p, sub_resource_idx %u, dc %p.\n", texture
, sub_resource_idx
, dc
);
1586 if (!(sub_resource
= wined3d_texture_get_sub_resource(texture
, sub_resource_idx
)))
1587 return WINED3DERR_INVALIDCALL
;
1589 if (sub_resource
->type
!= WINED3D_RTYPE_SURFACE
)
1591 WARN("Not supported on %s resources.\n", debug_d3dresourcetype(texture
->resource
.type
));
1592 return WINED3DERR_INVALIDCALL
;
1595 surface
= surface_from_resource(sub_resource
);
1597 /* Give more detailed info for ddraw. */
1598 if (surface
->flags
& SFLAG_DCINUSE
)
1599 return WINEDDERR_DCALREADYCREATED
;
1601 /* Can't GetDC if the surface is locked. */
1602 if (surface
->resource
.map_count
)
1603 return WINED3DERR_INVALIDCALL
;
1605 if (device
->d3d_initialized
)
1606 context
= context_acquire(device
, NULL
);
1608 /* Create a DIB section if there isn't a dc yet. */
1611 if (FAILED(hr
= surface_create_dib_section(surface
)))
1614 context_release(context
);
1615 return WINED3DERR_INVALIDCALL
;
1617 if (!(surface
->resource
.map_binding
== WINED3D_LOCATION_USER_MEMORY
1618 || surface
->container
->flags
& WINED3D_TEXTURE_PIN_SYSMEM
1620 surface
->resource
.map_binding
= WINED3D_LOCATION_DIB
;
1623 surface_load_location(surface
, context
, WINED3D_LOCATION_DIB
);
1624 surface_invalidate_location(surface
, ~WINED3D_LOCATION_DIB
);
1627 context_release(context
);
1629 surface
->flags
|= SFLAG_DCINUSE
;
1630 surface
->resource
.map_count
++;
1633 TRACE("Returning dc %p.\n", *dc
);
1638 HRESULT CDECL
wined3d_texture_release_dc(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
, HDC dc
)
1640 struct wined3d_device
*device
= texture
->resource
.device
;
1641 struct wined3d_context
*context
= NULL
;
1642 struct wined3d_resource
*sub_resource
;
1643 struct wined3d_surface
*surface
;
1645 TRACE("texture %p, sub_resource_idx %u, dc %p.\n", texture
, sub_resource_idx
, dc
);
1647 if (!(sub_resource
= wined3d_texture_get_sub_resource(texture
, sub_resource_idx
)))
1648 return WINED3DERR_INVALIDCALL
;
1650 if (sub_resource
->type
!= WINED3D_RTYPE_SURFACE
)
1652 WARN("Not supported on %s resources.\n", debug_d3dresourcetype(texture
->resource
.type
));
1653 return WINED3DERR_INVALIDCALL
;
1656 surface
= surface_from_resource(sub_resource
);
1658 if (!(surface
->flags
& SFLAG_DCINUSE
))
1659 return WINEDDERR_NODC
;
1661 if (surface
->hDC
!= dc
)
1663 WARN("Application tries to release invalid DC %p, surface DC is %p.\n",
1665 return WINEDDERR_NODC
;
1668 surface
->resource
.map_count
--;
1669 surface
->flags
&= ~SFLAG_DCINUSE
;
1671 if (surface
->resource
.map_binding
== WINED3D_LOCATION_USER_MEMORY
1672 || (surface
->container
->flags
& WINED3D_TEXTURE_PIN_SYSMEM
1673 && surface
->resource
.map_binding
!= WINED3D_LOCATION_DIB
))
1675 /* The game Salammbo modifies the surface contents without mapping the surface between
1676 * a GetDC/ReleaseDC operation and flipping the surface. If the DIB remains the active
1677 * copy and is copied to the screen, this update, which draws the mouse pointer, is lost.
1678 * Do not only copy the DIB to the map location, but also make sure the map location is
1679 * copied back to the DIB in the next getdc call.
1681 * The same consideration applies to user memory surfaces. */
1683 if (device
->d3d_initialized
)
1684 context
= context_acquire(device
, NULL
);
1686 surface_load_location(surface
, context
, surface
->resource
.map_binding
);
1687 surface_invalidate_location(surface
, WINED3D_LOCATION_DIB
);
1689 context_release(context
);