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(d3d_perf
);
29 WINE_DECLARE_DEBUG_CHANNEL(winediag
);
31 static HRESULT
wined3d_texture_init(struct wined3d_texture
*texture
, const struct wined3d_texture_ops
*texture_ops
,
32 UINT layer_count
, UINT level_count
, const struct wined3d_resource_desc
*desc
, DWORD flags
,
33 struct wined3d_device
*device
, void *parent
, const struct wined3d_parent_ops
*parent_ops
,
34 const struct wined3d_resource_ops
*resource_ops
)
36 const struct wined3d_format
*format
= wined3d_get_format(&device
->adapter
->gl_info
, desc
->format
);
39 TRACE("texture %p, texture_ops %p, layer_count %u, level_count %u, resource_type %s, format %s, "
40 "multisample_type %#x, multisample_quality %#x, usage %s, pool %s, width %u, height %u, depth %u, "
41 "flags %#x, device %p, parent %p, parent_ops %p, resource_ops %p.\n",
42 texture
, texture_ops
, layer_count
, level_count
, debug_d3dresourcetype(desc
->resource_type
),
43 debug_d3dformat(desc
->format
), desc
->multisample_type
, desc
->multisample_quality
,
44 debug_d3dusage(desc
->usage
), debug_d3dpool(desc
->pool
), desc
->width
, desc
->height
, desc
->depth
,
45 flags
, device
, parent
, parent_ops
, resource_ops
);
47 if (FAILED(hr
= resource_init(&texture
->resource
, device
, desc
->resource_type
, format
,
48 desc
->multisample_type
, desc
->multisample_quality
, desc
->usage
, desc
->pool
,
49 desc
->width
, desc
->height
, desc
->depth
, 0, parent
, parent_ops
, resource_ops
)))
51 static unsigned int once
;
53 /* DXTn 3D textures are not supported. Do not write the ERR for them. */
54 if ((desc
->format
== WINED3DFMT_DXT1
|| desc
->format
== WINED3DFMT_DXT2
|| desc
->format
== WINED3DFMT_DXT3
55 || desc
->format
== WINED3DFMT_DXT4
|| desc
->format
== WINED3DFMT_DXT5
)
56 && !(format
->flags
[WINED3D_GL_RES_TYPE_TEX_2D
] & WINED3DFMT_FLAG_TEXTURE
)
57 && desc
->resource_type
!= WINED3D_RTYPE_TEXTURE_3D
&& !once
++)
58 ERR_(winediag
)("The application tried to create a DXTn texture, but the driver does not support them.\n");
60 WARN("Failed to initialize resource, returning %#x\n", hr
);
63 wined3d_resource_update_draw_binding(&texture
->resource
);
65 texture
->texture_ops
= texture_ops
;
67 texture
->layer_count
= layer_count
;
68 texture
->level_count
= level_count
;
69 texture
->filter_type
= (desc
->usage
& WINED3DUSAGE_AUTOGENMIPMAP
) ? WINED3D_TEXF_LINEAR
: WINED3D_TEXF_NONE
;
71 texture
->flags
= WINED3D_TEXTURE_POW2_MAT_IDENT
| WINED3D_TEXTURE_NORMALIZED_COORDS
;
72 if (flags
& WINED3D_TEXTURE_CREATE_PIN_SYSMEM
)
73 texture
->flags
|= WINED3D_TEXTURE_PIN_SYSMEM
;
78 /* Context activation is done by the caller. */
79 void wined3d_texture_remove_buffer_object(struct wined3d_texture
*texture
,
80 unsigned int sub_resource_idx
, const struct wined3d_gl_info
*gl_info
)
82 GLuint
*buffer_object
;
84 buffer_object
= &texture
->sub_resources
[sub_resource_idx
].buffer_object
;
85 GL_EXTCALL(glDeleteBuffers(1, buffer_object
));
86 checkGLcall("glDeleteBuffers");
87 texture
->texture_ops
->texture_sub_resource_invalidate_location(
88 texture
->sub_resources
[sub_resource_idx
].resource
, WINED3D_LOCATION_BUFFER
);
91 TRACE("Deleted buffer object %u for texture %p, sub-resource %u.\n",
92 *buffer_object
, texture
, sub_resource_idx
);
95 /* A GL context is provided by the caller */
96 static void gltexture_delete(struct wined3d_device
*device
, const struct wined3d_gl_info
*gl_info
,
97 struct gl_texture
*tex
)
99 context_gl_resource_released(device
, tex
->name
, FALSE
);
100 gl_info
->gl_ops
.gl
.p_glDeleteTextures(1, &tex
->name
);
104 static void wined3d_texture_unload_gl_texture(struct wined3d_texture
*texture
)
106 struct wined3d_device
*device
= texture
->resource
.device
;
107 struct wined3d_context
*context
= NULL
;
109 if (texture
->texture_rgb
.name
|| texture
->texture_srgb
.name
)
111 context
= context_acquire(device
, NULL
);
114 if (texture
->texture_rgb
.name
)
115 gltexture_delete(device
, context
->gl_info
, &texture
->texture_rgb
);
117 if (texture
->texture_srgb
.name
)
118 gltexture_delete(device
, context
->gl_info
, &texture
->texture_srgb
);
120 if (context
) context_release(context
);
122 wined3d_texture_set_dirty(texture
);
124 resource_unload(&texture
->resource
);
127 static void wined3d_texture_cleanup(struct wined3d_texture
*texture
)
129 unsigned int sub_count
= texture
->level_count
* texture
->layer_count
;
130 struct wined3d_device
*device
= texture
->resource
.device
;
131 struct wined3d_context
*context
= NULL
;
132 const struct wined3d_gl_info
*gl_info
;
133 GLuint buffer_object
;
136 TRACE("texture %p.\n", texture
);
138 for (i
= 0; i
< sub_count
; ++i
)
140 if (!(buffer_object
= texture
->sub_resources
[i
].buffer_object
))
143 TRACE("Deleting buffer object %u.\n", buffer_object
);
145 /* We may not be able to get a context in wined3d_texture_cleanup() in
146 * general, but if a buffer object was previously created we can. */
149 context
= context_acquire(device
, NULL
);
150 gl_info
= context
->gl_info
;
153 GL_EXTCALL(glDeleteBuffers(1, &buffer_object
));
156 context_release(context
);
158 texture
->texture_ops
->texture_cleanup_sub_resources(texture
);
159 wined3d_texture_unload_gl_texture(texture
);
160 resource_cleanup(&texture
->resource
);
163 void wined3d_texture_set_swapchain(struct wined3d_texture
*texture
, struct wined3d_swapchain
*swapchain
)
165 texture
->swapchain
= swapchain
;
166 wined3d_resource_update_draw_binding(&texture
->resource
);
169 void wined3d_texture_set_dirty(struct wined3d_texture
*texture
)
171 texture
->flags
&= ~(WINED3D_TEXTURE_RGB_VALID
| WINED3D_TEXTURE_SRGB_VALID
);
174 /* Context activation is done by the caller. */
175 void wined3d_texture_bind(struct wined3d_texture
*texture
,
176 struct wined3d_context
*context
, BOOL srgb
)
178 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
179 struct gl_texture
*gl_tex
;
182 TRACE("texture %p, context %p, srgb %#x.\n", texture
, context
, srgb
);
184 if (!needs_separate_srgb_gl_texture(context
))
187 /* sRGB mode cache for preload() calls outside drawprim. */
189 texture
->flags
|= WINED3D_TEXTURE_IS_SRGB
;
191 texture
->flags
&= ~WINED3D_TEXTURE_IS_SRGB
;
193 gl_tex
= wined3d_texture_get_gl_texture(texture
, srgb
);
194 target
= texture
->target
;
198 context_bind_texture(context
, target
, gl_tex
->name
);
202 gl_info
->gl_ops
.gl
.p_glGenTextures(1, &gl_tex
->name
);
203 checkGLcall("glGenTextures");
204 TRACE("Generated texture %d.\n", gl_tex
->name
);
208 ERR("Failed to generate a texture name.\n");
212 /* Initialise the state of the texture object to the OpenGL defaults, not
213 * the wined3d defaults. */
214 gl_tex
->sampler_desc
.address_u
= WINED3D_TADDRESS_WRAP
;
215 gl_tex
->sampler_desc
.address_v
= WINED3D_TADDRESS_WRAP
;
216 gl_tex
->sampler_desc
.address_w
= WINED3D_TADDRESS_WRAP
;
217 memset(gl_tex
->sampler_desc
.border_color
, 0, sizeof(gl_tex
->sampler_desc
.border_color
));
218 gl_tex
->sampler_desc
.mag_filter
= WINED3D_TEXF_LINEAR
;
219 gl_tex
->sampler_desc
.min_filter
= WINED3D_TEXF_POINT
; /* GL_NEAREST_MIPMAP_LINEAR */
220 gl_tex
->sampler_desc
.mip_filter
= WINED3D_TEXF_LINEAR
; /* GL_NEAREST_MIPMAP_LINEAR */
221 gl_tex
->sampler_desc
.lod_bias
= 0.0f
;
222 gl_tex
->sampler_desc
.min_lod
= -1000.0f
;
223 gl_tex
->sampler_desc
.max_lod
= 1000.0f
;
224 gl_tex
->sampler_desc
.max_anisotropy
= 1;
225 gl_tex
->sampler_desc
.compare
= FALSE
;
226 gl_tex
->sampler_desc
.comparison_func
= WINED3D_CMP_LESSEQUAL
;
227 if (context
->gl_info
->supported
[EXT_TEXTURE_SRGB_DECODE
])
228 gl_tex
->sampler_desc
.srgb_decode
= TRUE
;
230 gl_tex
->sampler_desc
.srgb_decode
= srgb
;
231 gl_tex
->base_level
= 0;
232 wined3d_texture_set_dirty(texture
);
234 context_bind_texture(context
, target
, gl_tex
->name
);
236 if (texture
->resource
.usage
& WINED3DUSAGE_AUTOGENMIPMAP
)
238 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_GENERATE_MIPMAP_SGIS
, GL_TRUE
);
239 checkGLcall("glTexParameteri(target, GL_GENERATE_MIPMAP_SGIS, GL_TRUE)");
242 /* For a new texture we have to set the texture levels after binding the
243 * texture. Beware that texture rectangles do not support mipmapping, but
244 * set the maxmiplevel if we're relying on the partial
245 * GL_ARB_texture_non_power_of_two emulation with texture rectangles.
246 * (I.e., do not care about cond_np2 here, just look for
247 * GL_TEXTURE_RECTANGLE_ARB.) */
248 if (target
!= GL_TEXTURE_RECTANGLE_ARB
)
250 TRACE("Setting GL_TEXTURE_MAX_LEVEL to %u.\n", texture
->level_count
- 1);
251 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, texture
->level_count
- 1);
252 checkGLcall("glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, texture->level_count)");
255 if (target
== GL_TEXTURE_CUBE_MAP_ARB
)
257 /* Cubemaps are always set to clamp, regardless of the sampler state. */
258 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
259 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
260 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_R
, GL_CLAMP_TO_EDGE
);
263 if (texture
->flags
& WINED3D_TEXTURE_COND_NP2
)
265 /* Conditinal non power of two textures use a different clamping
266 * default. If we're using the GL_WINE_normalized_texrect partial
267 * driver emulation, we're dealing with a GL_TEXTURE_2D texture which
268 * has the address mode set to repeat - something that prevents us
269 * from hitting the accelerated codepath. Thus manually set the GL
270 * state. The same applies to filtering. Even if the texture has only
271 * one mip level, the default LINEAR_MIPMAP_LINEAR filter causes a SW
272 * fallback on macos. */
273 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
274 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
275 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
276 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
277 checkGLcall("glTexParameteri");
278 gl_tex
->sampler_desc
.address_u
= WINED3D_TADDRESS_CLAMP
;
279 gl_tex
->sampler_desc
.address_v
= WINED3D_TADDRESS_CLAMP
;
280 gl_tex
->sampler_desc
.mag_filter
= WINED3D_TEXF_POINT
;
281 gl_tex
->sampler_desc
.min_filter
= WINED3D_TEXF_POINT
;
282 gl_tex
->sampler_desc
.mip_filter
= WINED3D_TEXF_NONE
;
285 if (gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
] && gl_info
->supported
[ARB_DEPTH_TEXTURE
])
287 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_DEPTH_TEXTURE_MODE_ARB
, GL_INTENSITY
);
288 checkGLcall("glTexParameteri(GL_DEPTH_TEXTURE_MODE_ARB, GL_INTENSITY)");
292 /* Context activation is done by the caller. */
293 void wined3d_texture_bind_and_dirtify(struct wined3d_texture
*texture
,
294 struct wined3d_context
*context
, BOOL srgb
)
296 DWORD active_sampler
;
298 /* We don't need a specific texture unit, but after binding the texture
299 * the current unit is dirty. Read the unit back instead of switching to
300 * 0, this avoids messing around with the state manager's GL states. The
301 * current texture unit should always be a valid one.
303 * To be more specific, this is tricky because we can implicitly be
304 * called from sampler() in state.c. This means we can't touch anything
305 * other than whatever happens to be the currently active texture, or we
306 * would risk marking already applied sampler states dirty again. */
307 active_sampler
= context
->rev_tex_unit_map
[context
->active_texture
];
308 if (active_sampler
!= WINED3D_UNMAPPED_STAGE
)
309 context_invalidate_state(context
, STATE_SAMPLER(active_sampler
));
310 /* FIXME: Ideally we'd only do this when touching a binding that's used by
312 context_invalidate_state(context
, STATE_SHADER_RESOURCE_BINDING
);
314 wined3d_texture_bind(texture
, context
, srgb
);
317 /* Context activation is done by the caller (state handler). */
318 /* This function relies on the correct texture being bound and loaded. */
319 void wined3d_texture_apply_sampler_desc(struct wined3d_texture
*texture
,
320 const struct wined3d_sampler_desc
*sampler_desc
, const struct wined3d_context
*context
)
322 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
323 GLenum target
= texture
->target
;
324 struct gl_texture
*gl_tex
;
327 TRACE("texture %p, sampler_desc %p, context %p.\n", texture
, sampler_desc
, context
);
329 gl_tex
= wined3d_texture_get_gl_texture(texture
, texture
->flags
& WINED3D_TEXTURE_IS_SRGB
);
331 state
= sampler_desc
->address_u
;
332 if (state
!= gl_tex
->sampler_desc
.address_u
)
334 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_S
,
335 gl_info
->wrap_lookup
[state
- WINED3D_TADDRESS_WRAP
]);
336 gl_tex
->sampler_desc
.address_u
= state
;
339 state
= sampler_desc
->address_v
;
340 if (state
!= gl_tex
->sampler_desc
.address_v
)
342 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_T
,
343 gl_info
->wrap_lookup
[state
- WINED3D_TADDRESS_WRAP
]);
344 gl_tex
->sampler_desc
.address_v
= state
;
347 state
= sampler_desc
->address_w
;
348 if (state
!= gl_tex
->sampler_desc
.address_w
)
350 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_R
,
351 gl_info
->wrap_lookup
[state
- WINED3D_TADDRESS_WRAP
]);
352 gl_tex
->sampler_desc
.address_w
= state
;
355 if (memcmp(gl_tex
->sampler_desc
.border_color
, sampler_desc
->border_color
,
356 sizeof(gl_tex
->sampler_desc
.border_color
)))
358 gl_info
->gl_ops
.gl
.p_glTexParameterfv(target
, GL_TEXTURE_BORDER_COLOR
, &sampler_desc
->border_color
[0]);
359 memcpy(gl_tex
->sampler_desc
.border_color
, sampler_desc
->border_color
,
360 sizeof(gl_tex
->sampler_desc
.border_color
));
363 state
= sampler_desc
->mag_filter
;
364 if (state
!= gl_tex
->sampler_desc
.mag_filter
)
366 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MAG_FILTER
, wined3d_gl_mag_filter(state
));
367 gl_tex
->sampler_desc
.mag_filter
= state
;
370 if (sampler_desc
->min_filter
!= gl_tex
->sampler_desc
.min_filter
371 || sampler_desc
->mip_filter
!= gl_tex
->sampler_desc
.mip_filter
)
373 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MIN_FILTER
,
374 wined3d_gl_min_mip_filter(sampler_desc
->min_filter
, sampler_desc
->mip_filter
));
375 gl_tex
->sampler_desc
.min_filter
= sampler_desc
->min_filter
;
376 gl_tex
->sampler_desc
.mip_filter
= sampler_desc
->mip_filter
;
379 state
= sampler_desc
->max_anisotropy
;
380 if (state
!= gl_tex
->sampler_desc
.max_anisotropy
)
382 if (gl_info
->supported
[EXT_TEXTURE_FILTER_ANISOTROPIC
])
383 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MAX_ANISOTROPY_EXT
, state
);
385 WARN("Anisotropic filtering not supported.\n");
386 gl_tex
->sampler_desc
.max_anisotropy
= state
;
389 if (!sampler_desc
->srgb_decode
!= !gl_tex
->sampler_desc
.srgb_decode
390 && (context
->d3d_info
->wined3d_creation_flags
& WINED3D_SRGB_READ_WRITE_CONTROL
)
391 && gl_info
->supported
[EXT_TEXTURE_SRGB_DECODE
])
393 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_SRGB_DECODE_EXT
,
394 sampler_desc
->srgb_decode
? GL_DECODE_EXT
: GL_SKIP_DECODE_EXT
);
395 gl_tex
->sampler_desc
.srgb_decode
= sampler_desc
->srgb_decode
;
398 if (!sampler_desc
->compare
!= !gl_tex
->sampler_desc
.compare
)
400 if (sampler_desc
->compare
)
401 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_COMPARE_MODE_ARB
, GL_COMPARE_R_TO_TEXTURE_ARB
);
403 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_COMPARE_MODE_ARB
, GL_NONE
);
404 gl_tex
->sampler_desc
.compare
= sampler_desc
->compare
;
407 checkGLcall("Texture parameter application");
409 if (gl_info
->supported
[EXT_TEXTURE_LOD_BIAS
])
411 gl_info
->gl_ops
.gl
.p_glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT
,
412 GL_TEXTURE_LOD_BIAS_EXT
, sampler_desc
->lod_bias
);
413 checkGLcall("glTexEnvf(GL_TEXTURE_LOD_BIAS_EXT, ...)");
417 ULONG CDECL
wined3d_texture_incref(struct wined3d_texture
*texture
)
421 TRACE("texture %p, swapchain %p.\n", texture
, texture
->swapchain
);
423 if (texture
->swapchain
)
424 return wined3d_swapchain_incref(texture
->swapchain
);
426 refcount
= InterlockedIncrement(&texture
->resource
.ref
);
427 TRACE("%p increasing refcount to %u.\n", texture
, refcount
);
432 ULONG CDECL
wined3d_texture_decref(struct wined3d_texture
*texture
)
436 TRACE("texture %p, swapchain %p.\n", texture
, texture
->swapchain
);
438 if (texture
->swapchain
)
439 return wined3d_swapchain_decref(texture
->swapchain
);
441 refcount
= InterlockedDecrement(&texture
->resource
.ref
);
442 TRACE("%p decreasing refcount to %u.\n", texture
, refcount
);
446 wined3d_texture_cleanup(texture
);
447 texture
->resource
.parent_ops
->wined3d_object_destroyed(texture
->resource
.parent
);
448 HeapFree(GetProcessHeap(), 0, texture
);
454 struct wined3d_resource
* CDECL
wined3d_texture_get_resource(struct wined3d_texture
*texture
)
456 TRACE("texture %p.\n", texture
);
458 return &texture
->resource
;
461 static BOOL
color_key_equal(const struct wined3d_color_key
*c1
, struct wined3d_color_key
*c2
)
463 return c1
->color_space_low_value
== c2
->color_space_low_value
464 && c1
->color_space_high_value
== c2
->color_space_high_value
;
467 /* Context activation is done by the caller */
468 void wined3d_texture_load(struct wined3d_texture
*texture
,
469 struct wined3d_context
*context
, BOOL srgb
)
471 UINT sub_count
= texture
->level_count
* texture
->layer_count
;
472 const struct wined3d_d3d_info
*d3d_info
= context
->d3d_info
;
476 TRACE("texture %p, context %p, srgb %#x.\n", texture
, context
, srgb
);
478 if (!needs_separate_srgb_gl_texture(context
))
482 flag
= WINED3D_TEXTURE_SRGB_VALID
;
484 flag
= WINED3D_TEXTURE_RGB_VALID
;
486 if (!d3d_info
->shader_color_key
487 && (!(texture
->async
.flags
& WINED3D_TEXTURE_ASYNC_COLOR_KEY
)
488 != !(texture
->async
.color_key_flags
& WINED3D_CKEY_SRC_BLT
)
489 || (texture
->async
.flags
& WINED3D_TEXTURE_ASYNC_COLOR_KEY
490 && !color_key_equal(&texture
->async
.gl_color_key
, &texture
->async
.src_blt_color_key
))))
492 unsigned int sub_count
= texture
->level_count
* texture
->layer_count
;
495 TRACE("Reloading because of color key value change.\n");
496 for (i
= 0; i
< sub_count
; i
++)
497 texture
->texture_ops
->texture_sub_resource_add_dirty_region(texture
->sub_resources
[i
].resource
, NULL
);
498 wined3d_texture_set_dirty(texture
);
500 texture
->async
.gl_color_key
= texture
->async
.src_blt_color_key
;
503 if (texture
->flags
& flag
)
505 TRACE("Texture %p not dirty, nothing to do.\n", texture
);
509 /* Reload the surfaces if the texture is marked dirty. */
510 for (i
= 0; i
< sub_count
; ++i
)
512 texture
->texture_ops
->texture_sub_resource_load(texture
->sub_resources
[i
].resource
, context
, srgb
);
514 texture
->flags
|= flag
;
517 void CDECL
wined3d_texture_preload(struct wined3d_texture
*texture
)
519 struct wined3d_context
*context
;
520 context
= context_acquire(texture
->resource
.device
, NULL
);
521 wined3d_texture_load(texture
, context
, texture
->flags
& WINED3D_TEXTURE_IS_SRGB
);
522 context_release(context
);
525 void * CDECL
wined3d_texture_get_parent(const struct wined3d_texture
*texture
)
527 TRACE("texture %p.\n", texture
);
529 return texture
->resource
.parent
;
532 static BOOL
wined3d_texture_check_box_dimensions(const struct wined3d_texture
*texture
,
533 unsigned int level
, const struct wined3d_box
*box
)
535 if (box
->left
>= box
->right
536 || box
->top
>= box
->bottom
537 || box
->front
>= box
->back
)
540 if (box
->right
> wined3d_texture_get_level_width(texture
, level
)
541 || box
->bottom
> wined3d_texture_get_level_height(texture
, level
)
542 || box
->back
> wined3d_texture_get_level_depth(texture
, level
))
548 void CDECL
wined3d_texture_get_pitch(const struct wined3d_texture
*texture
,
549 unsigned int level
, unsigned int *row_pitch
, unsigned int *slice_pitch
)
551 const struct wined3d_resource
*resource
= &texture
->resource
;
552 unsigned int width
= wined3d_texture_get_level_width(texture
, level
);
553 unsigned int height
= wined3d_texture_get_level_height(texture
, level
);
555 if (texture
->row_pitch
)
557 *row_pitch
= texture
->row_pitch
;
558 *slice_pitch
= texture
->slice_pitch
;
562 wined3d_format_calculate_pitch(resource
->format
, resource
->device
->surface_alignment
,
563 width
, height
, row_pitch
, slice_pitch
);
566 DWORD CDECL
wined3d_texture_set_lod(struct wined3d_texture
*texture
, DWORD lod
)
568 DWORD old
= texture
->lod
;
570 TRACE("texture %p, lod %u.\n", texture
, lod
);
572 /* The d3d9:texture test shows that SetLOD is ignored on non-managed
573 * textures. The call always returns 0, and GetLOD always returns 0. */
574 if (texture
->resource
.pool
!= WINED3D_POOL_MANAGED
)
576 TRACE("Ignoring SetLOD on %s texture, returning 0.\n", debug_d3dpool(texture
->resource
.pool
));
580 if (lod
>= texture
->level_count
)
581 lod
= texture
->level_count
- 1;
583 if (texture
->lod
!= lod
)
587 texture
->texture_rgb
.base_level
= ~0u;
588 texture
->texture_srgb
.base_level
= ~0u;
589 if (texture
->resource
.bind_count
)
590 device_invalidate_state(texture
->resource
.device
, STATE_SAMPLER(texture
->sampler
));
596 DWORD CDECL
wined3d_texture_get_lod(const struct wined3d_texture
*texture
)
598 TRACE("texture %p, returning %u.\n", texture
, texture
->lod
);
603 DWORD CDECL
wined3d_texture_get_level_count(const struct wined3d_texture
*texture
)
605 TRACE("texture %p, returning %u.\n", texture
, texture
->level_count
);
607 return texture
->level_count
;
610 HRESULT CDECL
wined3d_texture_set_autogen_filter_type(struct wined3d_texture
*texture
,
611 enum wined3d_texture_filter_type filter_type
)
613 FIXME("texture %p, filter_type %s stub!\n", texture
, debug_d3dtexturefiltertype(filter_type
));
615 if (!(texture
->resource
.usage
& WINED3DUSAGE_AUTOGENMIPMAP
))
617 WARN("Texture doesn't have AUTOGENMIPMAP usage.\n");
618 return WINED3DERR_INVALIDCALL
;
621 texture
->filter_type
= filter_type
;
626 enum wined3d_texture_filter_type CDECL
wined3d_texture_get_autogen_filter_type(const struct wined3d_texture
*texture
)
628 TRACE("texture %p.\n", texture
);
630 return texture
->filter_type
;
633 HRESULT CDECL
wined3d_texture_set_color_key(struct wined3d_texture
*texture
,
634 DWORD flags
, const struct wined3d_color_key
*color_key
)
636 struct wined3d_device
*device
= texture
->resource
.device
;
637 static const DWORD all_flags
= WINED3D_CKEY_DST_BLT
| WINED3D_CKEY_DST_OVERLAY
638 | WINED3D_CKEY_SRC_BLT
| WINED3D_CKEY_SRC_OVERLAY
;
640 TRACE("texture %p, flags %#x, color_key %p.\n", texture
, flags
, color_key
);
642 if (flags
& ~all_flags
)
644 WARN("Invalid flags passed, returning WINED3DERR_INVALIDCALL.\n");
645 return WINED3DERR_INVALIDCALL
;
648 wined3d_cs_emit_set_color_key(device
->cs
, texture
, flags
, color_key
);
653 HRESULT CDECL
wined3d_texture_update_desc(struct wined3d_texture
*texture
, UINT width
, UINT height
,
654 enum wined3d_format_id format_id
, enum wined3d_multisample_type multisample_type
,
655 UINT multisample_quality
, void *mem
, UINT pitch
)
657 struct wined3d_device
*device
= texture
->resource
.device
;
658 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
659 const struct wined3d_format
*format
= wined3d_get_format(gl_info
, format_id
);
660 UINT resource_size
= wined3d_format_calculate_size(format
, device
->surface_alignment
, width
, height
, 1);
661 struct wined3d_surface
*surface
;
663 TRACE("texture %p, width %u, height %u, format %s, multisample_type %#x, multisample_quality %u, "
664 "mem %p, pitch %u.\n",
665 texture
, width
, height
, debug_d3dformat(format_id
), multisample_type
, multisample_quality
, mem
, pitch
);
668 return WINED3DERR_INVALIDCALL
;
670 if (texture
->level_count
* texture
->layer_count
> 1)
672 WARN("Texture has multiple sub-resources, not supported.\n");
673 return WINED3DERR_INVALIDCALL
;
676 if (texture
->resource
.type
== WINED3D_RTYPE_TEXTURE_3D
)
678 WARN("Not supported on 3D textures.\n");
679 return WINED3DERR_INVALIDCALL
;
682 /* We have no way of supporting a pitch that is not a multiple of the pixel
683 * byte width short of uploading the texture row-by-row.
684 * Fortunately that's not an issue since D3D9Ex doesn't allow a custom pitch
685 * for user-memory textures (it always expects packed data) while DirectDraw
686 * requires a 4-byte aligned pitch and doesn't support texture formats
687 * larger than 4 bytes per pixel nor any format using 3 bytes per pixel.
688 * This check is here to verify that the assumption holds. */
689 if (pitch
% texture
->resource
.format
->byte_count
)
691 WARN("Pitch unsupported, not a multiple of the texture format byte width.\n");
692 return WINED3DERR_INVALIDCALL
;
695 surface
= texture
->sub_resources
[0].u
.surface
;
696 if (surface
->resource
.map_count
|| (surface
->flags
& SFLAG_DCINUSE
))
698 WARN("Surface is mapped or the DC is in use.\n");
699 return WINED3DERR_INVALIDCALL
;
702 if (device
->d3d_initialized
)
703 texture
->resource
.resource_ops
->resource_unload(&texture
->resource
);
705 texture
->resource
.format
= format
;
706 texture
->resource
.multisample_type
= multisample_type
;
707 texture
->resource
.multisample_quality
= multisample_quality
;
708 texture
->resource
.width
= width
;
709 texture
->resource
.height
= height
;
711 texture
->user_memory
= mem
;
712 if ((texture
->row_pitch
= pitch
))
713 texture
->slice_pitch
= height
* pitch
;
715 /* User memory surfaces don't have the regular surface alignment. */
716 wined3d_format_calculate_pitch(format
, 1, width
, height
,
717 &texture
->row_pitch
, &texture
->slice_pitch
);
719 texture
->flags
&= ~WINED3D_TEXTURE_COND_NP2_EMULATED
;
720 if (((width
& (width
- 1)) || (height
& (height
- 1))) && !gl_info
->supported
[ARB_TEXTURE_NON_POWER_OF_TWO
]
721 && !gl_info
->supported
[ARB_TEXTURE_RECTANGLE
] && !gl_info
->supported
[WINED3D_GL_NORMALIZED_TEXRECT
])
722 texture
->flags
|= WINED3D_TEXTURE_COND_NP2_EMULATED
;
724 return wined3d_surface_update_desc(surface
, gl_info
);
727 /* Context activation is done by the caller. */
728 void wined3d_texture_prepare_buffer_object(struct wined3d_texture
*texture
,
729 unsigned int sub_resource_idx
, const struct wined3d_gl_info
*gl_info
)
731 GLuint
*buffer_object
;
733 buffer_object
= &texture
->sub_resources
[sub_resource_idx
].buffer_object
;
737 GL_EXTCALL(glGenBuffers(1, buffer_object
));
738 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER
, *buffer_object
));
739 GL_EXTCALL(glBufferData(GL_PIXEL_UNPACK_BUFFER
,
740 texture
->sub_resources
[sub_resource_idx
].resource
->size
, NULL
, GL_STREAM_DRAW
));
741 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER
, 0));
742 checkGLcall("Create buffer object");
744 TRACE("Created buffer object %u for texture %p, sub-resource %u.\n",
745 *buffer_object
, texture
, sub_resource_idx
);
748 void wined3d_texture_prepare_texture(struct wined3d_texture
*texture
, struct wined3d_context
*context
, BOOL srgb
)
750 DWORD alloc_flag
= srgb
? WINED3D_TEXTURE_SRGB_ALLOCATED
: WINED3D_TEXTURE_RGB_ALLOCATED
;
751 const struct wined3d_d3d_info
*d3d_info
= context
->d3d_info
;
753 if (!d3d_info
->shader_color_key
754 && !(texture
->async
.flags
& WINED3D_TEXTURE_ASYNC_COLOR_KEY
)
755 != !(texture
->async
.color_key_flags
& WINED3D_CKEY_SRC_BLT
))
757 wined3d_texture_force_reload(texture
);
759 if (texture
->async
.color_key_flags
& WINED3D_CKEY_SRC_BLT
)
760 texture
->async
.flags
|= WINED3D_TEXTURE_ASYNC_COLOR_KEY
;
763 if (texture
->flags
& alloc_flag
)
766 texture
->texture_ops
->texture_prepare_texture(texture
, context
, srgb
);
767 texture
->flags
|= alloc_flag
;
770 void wined3d_texture_force_reload(struct wined3d_texture
*texture
)
772 unsigned int sub_count
= texture
->level_count
* texture
->layer_count
;
775 texture
->flags
&= ~(WINED3D_TEXTURE_RGB_ALLOCATED
| WINED3D_TEXTURE_SRGB_ALLOCATED
776 | WINED3D_TEXTURE_CONVERTED
);
777 texture
->async
.flags
&= ~WINED3D_TEXTURE_ASYNC_COLOR_KEY
;
778 for (i
= 0; i
< sub_count
; ++i
)
780 texture
->texture_ops
->texture_sub_resource_invalidate_location(texture
->sub_resources
[i
].resource
,
781 WINED3D_LOCATION_TEXTURE_RGB
| WINED3D_LOCATION_TEXTURE_SRGB
);
785 void CDECL
wined3d_texture_generate_mipmaps(struct wined3d_texture
*texture
)
787 /* TODO: Implement filters using GL_SGI_generate_mipmaps. */
788 FIXME("texture %p stub!\n", texture
);
791 struct wined3d_resource
*wined3d_texture_get_sub_resource(const struct wined3d_texture
*texture
,
792 UINT sub_resource_idx
)
794 UINT sub_count
= texture
->level_count
* texture
->layer_count
;
796 TRACE("texture %p, sub_resource_idx %u.\n", texture
, sub_resource_idx
);
798 if (sub_resource_idx
>= sub_count
)
800 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx
, sub_count
);
804 return texture
->sub_resources
[sub_resource_idx
].resource
;
807 HRESULT CDECL
wined3d_texture_add_dirty_region(struct wined3d_texture
*texture
,
808 UINT layer
, const struct wined3d_box
*dirty_region
)
810 struct wined3d_resource
*sub_resource
;
812 TRACE("texture %p, layer %u, dirty_region %s.\n", texture
, layer
, debug_box(dirty_region
));
814 if (!(sub_resource
= wined3d_texture_get_sub_resource(texture
, layer
* texture
->level_count
)))
816 WARN("Failed to get sub-resource.\n");
817 return WINED3DERR_INVALIDCALL
;
820 texture
->texture_ops
->texture_sub_resource_add_dirty_region(sub_resource
, dirty_region
);
825 static HRESULT
wined3d_texture_upload_data(struct wined3d_texture
*texture
,
826 const struct wined3d_sub_resource_data
*data
)
828 unsigned int sub_count
= texture
->level_count
* texture
->layer_count
;
829 struct wined3d_context
*context
;
832 for (i
= 0; i
< sub_count
; ++i
)
836 WARN("Invalid sub-resource data specified for sub-resource %u.\n", i
);
841 context
= context_acquire(texture
->resource
.device
, NULL
);
843 wined3d_texture_prepare_texture(texture
, context
, FALSE
);
844 wined3d_texture_bind_and_dirtify(texture
, context
, FALSE
);
846 for (i
= 0; i
< sub_count
; ++i
)
848 struct wined3d_resource
*sub_resource
= texture
->sub_resources
[i
].resource
;
850 texture
->texture_ops
->texture_sub_resource_upload_data(sub_resource
, context
, &data
[i
]);
851 texture
->texture_ops
->texture_sub_resource_validate_location(sub_resource
, WINED3D_LOCATION_TEXTURE_RGB
);
852 texture
->texture_ops
->texture_sub_resource_invalidate_location(sub_resource
, ~WINED3D_LOCATION_TEXTURE_RGB
);
855 context_release(context
);
860 static void texture2d_sub_resource_load(struct wined3d_resource
*sub_resource
,
861 struct wined3d_context
*context
, BOOL srgb
)
863 surface_load(surface_from_resource(sub_resource
), context
, srgb
);
866 static void texture2d_sub_resource_add_dirty_region(struct wined3d_resource
*sub_resource
,
867 const struct wined3d_box
*dirty_region
)
869 struct wined3d_surface
*surface
= surface_from_resource(sub_resource
);
870 struct wined3d_context
*context
;
872 context
= context_acquire(surface
->container
->resource
.device
, NULL
);
873 surface_load_location(surface
, context
, sub_resource
->map_binding
);
874 context_release(context
);
875 surface_invalidate_location(surface
, ~sub_resource
->map_binding
);
878 static void texture2d_sub_resource_invalidate_location(struct wined3d_resource
*sub_resource
, DWORD location
)
880 struct wined3d_surface
*surface
= surface_from_resource(sub_resource
);
882 surface_invalidate_location(surface
, location
);
885 static void texture2d_sub_resource_validate_location(struct wined3d_resource
*sub_resource
, DWORD location
)
887 struct wined3d_surface
*surface
= surface_from_resource(sub_resource
);
889 surface_validate_location(surface
, location
);
892 static void texture2d_sub_resource_upload_data(struct wined3d_resource
*sub_resource
,
893 const struct wined3d_context
*context
, const struct wined3d_sub_resource_data
*data
)
895 struct wined3d_surface
*surface
= surface_from_resource(sub_resource
);
896 static const POINT dst_point
= {0, 0};
897 struct wined3d_const_bo_address addr
;
900 SetRect(&src_rect
, 0, 0, surface
->resource
.width
, surface
->resource
.height
);
902 addr
.buffer_object
= 0;
903 addr
.addr
= data
->data
;
905 wined3d_surface_upload_data(surface
, context
->gl_info
, surface
->container
->resource
.format
,
906 &src_rect
, data
->row_pitch
, &dst_point
, FALSE
, &addr
);
909 static BOOL
texture2d_load_location(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
,
910 struct wined3d_context
*context
, DWORD location
)
912 return SUCCEEDED(surface_load_location(texture
->sub_resources
[sub_resource_idx
].u
.surface
, context
, location
));
915 static BOOL
texture2d_prepare_location(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
,
916 struct wined3d_context
*context
, DWORD location
)
918 wined3d_surface_prepare(texture
->sub_resources
[sub_resource_idx
].u
.surface
, context
, location
);
922 /* Context activation is done by the caller. */
923 static void texture2d_prepare_texture(struct wined3d_texture
*texture
, struct wined3d_context
*context
, BOOL srgb
)
925 UINT sub_count
= texture
->level_count
* texture
->layer_count
;
926 const struct wined3d_format
*format
= texture
->resource
.format
;
927 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
928 const struct wined3d_color_key_conversion
*conversion
;
932 TRACE("texture %p, context %p, format %s.\n", texture
, context
, debug_d3dformat(format
->id
));
936 texture
->flags
|= WINED3D_TEXTURE_CONVERTED
;
938 else if ((conversion
= wined3d_format_get_color_key_conversion(texture
, TRUE
)))
940 texture
->flags
|= WINED3D_TEXTURE_CONVERTED
;
941 format
= wined3d_get_format(gl_info
, conversion
->dst_format
);
942 TRACE("Using format %s for color key conversion.\n", debug_d3dformat(format
->id
));
945 wined3d_texture_bind_and_dirtify(texture
, context
, srgb
);
948 internal
= format
->glGammaInternal
;
949 else if (texture
->resource
.usage
& WINED3DUSAGE_RENDERTARGET
950 && wined3d_resource_is_offscreen(&texture
->resource
))
951 internal
= format
->rtInternal
;
953 internal
= format
->glInternal
;
956 FIXME("No GL internal format for format %s.\n", debug_d3dformat(format
->id
));
958 TRACE("internal %#x, format %#x, type %#x.\n", internal
, format
->glFormat
, format
->glType
);
960 for (i
= 0; i
< sub_count
; ++i
)
962 struct wined3d_surface
*surface
= texture
->sub_resources
[i
].u
.surface
;
963 GLsizei height
= surface
->pow2Height
;
964 GLsizei width
= surface
->pow2Width
;
966 if (texture
->resource
.format_flags
& WINED3DFMT_FLAG_HEIGHT_SCALE
)
968 height
*= format
->height_scale
.numerator
;
969 height
/= format
->height_scale
.denominator
;
972 TRACE("surface %p, target %#x, level %d, width %d, height %d.\n",
973 surface
, surface
->texture_target
, surface
->texture_level
, width
, height
);
975 gl_info
->gl_ops
.gl
.p_glTexImage2D(surface
->texture_target
, surface
->texture_level
,
976 internal
, width
, height
, 0, format
->glFormat
, format
->glType
, NULL
);
977 checkGLcall("glTexImage2D");
981 static void texture2d_cleanup_sub_resources(struct wined3d_texture
*texture
)
983 unsigned int sub_count
= texture
->level_count
* texture
->layer_count
;
984 struct wined3d_surface
*surface
;
987 for (i
= 0; i
< sub_count
; ++i
)
989 if ((surface
= texture
->sub_resources
[i
].u
.surface
))
991 TRACE("surface %p.\n", surface
);
993 wined3d_surface_cleanup(surface
);
994 surface
->resource
.parent_ops
->wined3d_object_destroyed(surface
->resource
.parent
);
997 HeapFree(GetProcessHeap(), 0, texture
->sub_resources
[0].u
.surface
);
1000 static const struct wined3d_texture_ops texture2d_ops
=
1002 texture2d_sub_resource_load
,
1003 texture2d_sub_resource_add_dirty_region
,
1004 texture2d_sub_resource_invalidate_location
,
1005 texture2d_sub_resource_validate_location
,
1006 texture2d_sub_resource_upload_data
,
1007 texture2d_load_location
,
1008 texture2d_prepare_location
,
1009 texture2d_prepare_texture
,
1010 texture2d_cleanup_sub_resources
,
1013 static ULONG
texture_resource_incref(struct wined3d_resource
*resource
)
1015 return wined3d_texture_incref(wined3d_texture_from_resource(resource
));
1018 static ULONG
texture_resource_decref(struct wined3d_resource
*resource
)
1020 return wined3d_texture_decref(wined3d_texture_from_resource(resource
));
1023 static void wined3d_texture_unload(struct wined3d_resource
*resource
)
1025 struct wined3d_texture
*texture
= wined3d_texture_from_resource(resource
);
1026 UINT sub_count
= texture
->level_count
* texture
->layer_count
;
1027 struct wined3d_context
*context
= NULL
;
1030 TRACE("texture %p.\n", texture
);
1032 for (i
= 0; i
< sub_count
; ++i
)
1034 struct wined3d_resource
*sub_resource
= texture
->sub_resources
[i
].resource
;
1036 sub_resource
->resource_ops
->resource_unload(sub_resource
);
1038 if (texture
->sub_resources
[i
].buffer_object
)
1041 context
= context_acquire(texture
->resource
.device
, NULL
);
1042 wined3d_texture_remove_buffer_object(texture
, i
, context
->gl_info
);
1046 context_release(context
);
1048 wined3d_texture_force_reload(texture
);
1049 wined3d_texture_unload_gl_texture(texture
);
1052 static HRESULT
texture_resource_sub_resource_map(struct wined3d_resource
*resource
, unsigned int sub_resource_idx
,
1053 struct wined3d_map_desc
*map_desc
, const struct wined3d_box
*box
, DWORD flags
)
1055 const struct wined3d_format
*format
= resource
->format
;
1056 struct wined3d_device
*device
= resource
->device
;
1057 unsigned int fmt_flags
= resource
->format_flags
;
1058 const struct wined3d_gl_info
*gl_info
= NULL
;
1059 struct wined3d_context
*context
= NULL
;
1060 struct wined3d_resource
*sub_resource
;
1061 struct wined3d_texture
*texture
;
1062 unsigned int texture_level
;
1066 TRACE("resource %p, sub_resource_idx %u, map_desc %p, box %s, flags %#x.\n",
1067 resource
, sub_resource_idx
, map_desc
, debug_box(box
), flags
);
1069 texture
= wined3d_texture_from_resource(resource
);
1070 if (!(sub_resource
= wined3d_texture_get_sub_resource(texture
, sub_resource_idx
)))
1071 return E_INVALIDARG
;
1073 texture_level
= sub_resource_idx
% texture
->level_count
;
1074 if (box
&& !wined3d_texture_check_box_dimensions(texture
, texture_level
, box
))
1076 WARN("Map box is invalid.\n");
1077 if (resource
->type
!= WINED3D_RTYPE_TEXTURE_2D
)
1078 return WINED3DERR_INVALIDCALL
;
1081 if ((fmt_flags
& WINED3DFMT_FLAG_BLOCKS
) && box
1082 && !wined3d_texture_check_block_align(texture
, texture_level
, box
))
1084 WARN("Map box %s is misaligned for %ux%u blocks.\n",
1085 debug_box(box
), format
->block_width
, format
->block_height
);
1086 if (resource
->type
!= WINED3D_RTYPE_TEXTURE_2D
|| resource
->pool
== WINED3D_POOL_DEFAULT
)
1087 return WINED3DERR_INVALIDCALL
;
1090 if (!(resource
->access_flags
& WINED3D_RESOURCE_ACCESS_CPU
))
1092 WARN("Trying to map unmappable texture.\n");
1093 if (resource
->type
!= WINED3D_RTYPE_TEXTURE_2D
)
1094 return WINED3DERR_INVALIDCALL
;
1097 if (sub_resource
->map_count
)
1099 WARN("Sub-resource is already mapped.\n");
1100 return WINED3DERR_INVALIDCALL
;
1103 flags
= wined3d_resource_sanitize_map_flags(resource
, flags
);
1105 if (device
->d3d_initialized
)
1107 context
= context_acquire(device
, NULL
);
1108 gl_info
= context
->gl_info
;
1111 if (flags
& WINED3D_MAP_DISCARD
)
1113 TRACE("WINED3D_MAP_DISCARD flag passed, marking %s as up to date.\n",
1114 wined3d_debug_location(sub_resource
->map_binding
));
1115 if ((ret
= texture
->texture_ops
->texture_prepare_location(texture
,
1116 sub_resource_idx
, context
, sub_resource
->map_binding
)))
1117 texture
->texture_ops
->texture_sub_resource_validate_location(sub_resource
, sub_resource
->map_binding
);
1121 if (resource
->usage
& WINED3DUSAGE_DYNAMIC
)
1122 WARN_(d3d_perf
)("Mapping a dynamic texture without WINED3D_MAP_DISCARD.\n");
1123 ret
= texture
->texture_ops
->texture_load_location(texture
,
1124 sub_resource_idx
, context
, sub_resource
->map_binding
);
1129 ERR("Failed to prepare location.\n");
1130 context_release(context
);
1131 return E_OUTOFMEMORY
;
1134 if (!(flags
& (WINED3D_MAP_NO_DIRTY_UPDATE
| WINED3D_MAP_READONLY
)))
1135 texture
->texture_ops
->texture_sub_resource_invalidate_location(sub_resource
, ~sub_resource
->map_binding
);
1137 switch (sub_resource
->map_binding
)
1139 case WINED3D_LOCATION_SYSMEM
:
1140 base_memory
= sub_resource
->heap_memory
;
1143 case WINED3D_LOCATION_USER_MEMORY
:
1144 base_memory
= texture
->user_memory
;
1147 case WINED3D_LOCATION_DIB
:
1148 if (resource
->type
!= WINED3D_RTYPE_TEXTURE_2D
)
1149 ERR("Invalid map binding %#x for resource type %#x.\n",
1150 sub_resource
->map_binding
, resource
->type
);
1151 base_memory
= texture
->sub_resources
[sub_resource_idx
].u
.surface
->dib
.bitmap_data
;
1154 case WINED3D_LOCATION_BUFFER
:
1155 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER
,
1156 texture
->sub_resources
[sub_resource_idx
].buffer_object
));
1158 if (gl_info
->supported
[ARB_MAP_BUFFER_RANGE
])
1160 GLbitfield map_flags
= wined3d_resource_gl_map_flags(flags
);
1161 map_flags
&= ~GL_MAP_FLUSH_EXPLICIT_BIT
;
1162 base_memory
= GL_EXTCALL(glMapBufferRange(GL_PIXEL_UNPACK_BUFFER
,
1163 0, sub_resource
->size
, map_flags
));
1167 GLenum access
= wined3d_resource_gl_legacy_map_flags(flags
);
1168 base_memory
= GL_EXTCALL(glMapBuffer(GL_PIXEL_UNPACK_BUFFER
, access
));
1171 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER
, 0));
1172 checkGLcall("Map PBO");
1176 ERR("Unexpected map binding %s.\n", wined3d_debug_location(sub_resource
->map_binding
));
1182 context_release(context
);
1184 TRACE("Base memory pointer %p.\n", base_memory
);
1186 if (fmt_flags
& WINED3DFMT_FLAG_BROKEN_PITCH
)
1188 map_desc
->row_pitch
= wined3d_texture_get_level_width(texture
, texture_level
) * format
->byte_count
;
1189 map_desc
->slice_pitch
= wined3d_texture_get_level_height(texture
, texture_level
) * map_desc
->row_pitch
;
1193 wined3d_texture_get_pitch(texture
, texture_level
, &map_desc
->row_pitch
, &map_desc
->slice_pitch
);
1198 map_desc
->data
= base_memory
;
1202 if ((fmt_flags
& (WINED3DFMT_FLAG_BLOCKS
| WINED3DFMT_FLAG_BROKEN_PITCH
)) == WINED3DFMT_FLAG_BLOCKS
)
1204 /* Compressed textures are block based, so calculate the offset of
1205 * the block that contains the top-left pixel of the mapped box. */
1206 map_desc
->data
= base_memory
1207 + (box
->front
* map_desc
->slice_pitch
)
1208 + ((box
->top
/ format
->block_height
) * map_desc
->row_pitch
)
1209 + ((box
->left
/ format
->block_width
) * format
->block_byte_count
);
1213 map_desc
->data
= base_memory
1214 + (box
->front
* map_desc
->slice_pitch
)
1215 + (box
->top
* map_desc
->row_pitch
)
1216 + (box
->left
* format
->byte_count
);
1220 if (texture
->swapchain
&& texture
->swapchain
->front_buffer
== texture
)
1222 RECT
*r
= &texture
->swapchain
->front_buffer_update
;
1225 SetRect(r
, 0, 0, resource
->width
, resource
->height
);
1227 SetRect(r
, box
->left
, box
->top
, box
->right
, box
->bottom
);
1228 TRACE("Mapped front buffer %s.\n", wine_dbgstr_rect(r
));
1231 ++sub_resource
->map_count
;
1233 TRACE("Returning memory %p, row pitch %u, slice pitch %u.\n",
1234 map_desc
->data
, map_desc
->row_pitch
, map_desc
->slice_pitch
);
1239 static HRESULT
texture_resource_sub_resource_unmap(struct wined3d_resource
*resource
, unsigned int sub_resource_idx
)
1241 const struct wined3d_gl_info
*gl_info
;
1242 struct wined3d_resource
*sub_resource
;
1243 struct wined3d_texture
*texture
;
1244 struct wined3d_context
*context
;
1246 TRACE("resource %p, sub_resource_idx %u.\n", resource
, sub_resource_idx
);
1248 texture
= wined3d_texture_from_resource(resource
);
1249 if (!(sub_resource
= wined3d_texture_get_sub_resource(texture
, sub_resource_idx
)))
1250 return E_INVALIDARG
;
1252 if (!sub_resource
->map_count
)
1254 WARN("Trying to unmap unmapped sub-resource.\n");
1255 return WINEDDERR_NOTLOCKED
;
1258 switch (sub_resource
->map_binding
)
1260 case WINED3D_LOCATION_SYSMEM
:
1261 case WINED3D_LOCATION_USER_MEMORY
:
1262 case WINED3D_LOCATION_DIB
:
1265 case WINED3D_LOCATION_BUFFER
:
1266 context
= context_acquire(resource
->device
, NULL
);
1267 gl_info
= context
->gl_info
;
1269 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER
,
1270 texture
->sub_resources
[sub_resource_idx
].buffer_object
));
1271 GL_EXTCALL(glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER
));
1272 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER
, 0));
1273 checkGLcall("glUnmapBuffer");
1274 context_release(context
);
1278 ERR("Unexpected map binding %s.\n", wined3d_debug_location(sub_resource
->map_binding
));
1282 if (texture
->swapchain
&& texture
->swapchain
->front_buffer
== texture
)
1284 struct wined3d_surface
*surface
= texture
->sub_resources
[sub_resource_idx
].u
.surface
;
1286 if (!(surface
->locations
& (WINED3D_LOCATION_DRAWABLE
| WINED3D_LOCATION_TEXTURE_RGB
)))
1287 texture
->swapchain
->swapchain_ops
->swapchain_frontbuffer_updated(texture
->swapchain
);
1289 else if (resource
->format_flags
& (WINED3DFMT_FLAG_DEPTH
| WINED3DFMT_FLAG_STENCIL
))
1291 FIXME("Depth / stencil buffer locking is not implemented.\n");
1294 --sub_resource
->map_count
;
1299 static const struct wined3d_resource_ops texture_resource_ops
=
1301 texture_resource_incref
,
1302 texture_resource_decref
,
1303 wined3d_texture_unload
,
1304 texture_resource_sub_resource_map
,
1305 texture_resource_sub_resource_unmap
,
1308 static HRESULT
texture_init(struct wined3d_texture
*texture
, const struct wined3d_resource_desc
*desc
,
1309 unsigned int layer_count
, unsigned int level_count
, DWORD flags
, struct wined3d_device
*device
,
1310 void *parent
, const struct wined3d_parent_ops
*parent_ops
)
1312 struct wined3d_device_parent
*device_parent
= device
->device_parent
;
1313 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
1314 struct wined3d_resource_desc surface_desc
;
1315 struct wined3d_surface
*surfaces
;
1316 UINT pow2_width
, pow2_height
;
1320 /* TODO: It should only be possible to create textures for formats
1321 * that are reported as supported. */
1322 if (WINED3DFMT_UNKNOWN
>= desc
->format
)
1324 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture
);
1325 return WINED3DERR_INVALIDCALL
;
1328 /* Non-power2 support. */
1329 if (gl_info
->supported
[ARB_TEXTURE_NON_POWER_OF_TWO
])
1331 pow2_width
= desc
->width
;
1332 pow2_height
= desc
->height
;
1336 /* Find the nearest pow2 match. */
1337 pow2_width
= pow2_height
= 1;
1338 while (pow2_width
< desc
->width
)
1340 while (pow2_height
< desc
->height
)
1343 if (pow2_width
!= desc
->width
|| pow2_height
!= desc
->height
)
1345 /* level_count == 0 returns an error as well */
1346 if (level_count
!= 1 || desc
->usage
& WINED3DUSAGE_LEGACY_CUBEMAP
)
1348 if (desc
->pool
== WINED3D_POOL_SCRATCH
)
1350 WARN("Creating a scratch mipmapped/cube NPOT texture despite lack of HW support.\n");
1354 WARN("Attempted to create a mipmapped/cube NPOT texture without unconditional NPOT support.\n");
1355 return WINED3DERR_INVALIDCALL
;
1361 /* Calculate levels for mip mapping. */
1362 if (desc
->usage
& WINED3DUSAGE_AUTOGENMIPMAP
)
1364 if (!gl_info
->supported
[SGIS_GENERATE_MIPMAP
])
1366 WARN("No mipmap generation support, returning WINED3DERR_INVALIDCALL.\n");
1367 return WINED3DERR_INVALIDCALL
;
1370 if (level_count
!= 1)
1372 WARN("WINED3DUSAGE_AUTOGENMIPMAP is set, and level count != 1, returning WINED3DERR_INVALIDCALL.\n");
1373 return WINED3DERR_INVALIDCALL
;
1377 if (FAILED(hr
= wined3d_texture_init(texture
, &texture2d_ops
, layer_count
, level_count
, desc
,
1378 flags
, device
, parent
, parent_ops
, &texture_resource_ops
)))
1380 WARN("Failed to initialize texture, returning %#x.\n", hr
);
1384 /* Precalculated scaling for 'faked' non power of two texture coords. */
1385 if (texture
->resource
.gl_type
== WINED3D_GL_RES_TYPE_TEX_RECT
)
1387 texture
->pow2_matrix
[0] = (float)desc
->width
;
1388 texture
->pow2_matrix
[5] = (float)desc
->height
;
1389 texture
->pow2_matrix
[10] = 1.0f
;
1390 texture
->pow2_matrix
[15] = 1.0f
;
1391 texture
->target
= GL_TEXTURE_RECTANGLE_ARB
;
1392 texture
->flags
|= WINED3D_TEXTURE_COND_NP2
;
1393 texture
->flags
&= ~(WINED3D_TEXTURE_POW2_MAT_IDENT
| WINED3D_TEXTURE_NORMALIZED_COORDS
);
1397 if (desc
->usage
& WINED3DUSAGE_LEGACY_CUBEMAP
)
1398 texture
->target
= GL_TEXTURE_CUBE_MAP_ARB
;
1400 texture
->target
= GL_TEXTURE_2D
;
1401 if (desc
->width
== pow2_width
&& desc
->height
== pow2_height
)
1403 texture
->pow2_matrix
[0] = 1.0f
;
1404 texture
->pow2_matrix
[5] = 1.0f
;
1406 else if (gl_info
->supported
[WINED3D_GL_NORMALIZED_TEXRECT
])
1408 texture
->pow2_matrix
[0] = 1.0f
;
1409 texture
->pow2_matrix
[5] = 1.0f
;
1410 texture
->flags
|= WINED3D_TEXTURE_COND_NP2
;
1414 texture
->pow2_matrix
[0] = (((float)desc
->width
) / ((float)pow2_width
));
1415 texture
->pow2_matrix
[5] = (((float)desc
->height
) / ((float)pow2_height
));
1416 texture
->flags
&= ~WINED3D_TEXTURE_POW2_MAT_IDENT
;
1417 texture
->flags
|= WINED3D_TEXTURE_COND_NP2_EMULATED
;
1419 texture
->pow2_matrix
[10] = 1.0f
;
1420 texture
->pow2_matrix
[15] = 1.0f
;
1422 TRACE("xf(%f) yf(%f)\n", texture
->pow2_matrix
[0], texture
->pow2_matrix
[5]);
1424 if (!(surfaces
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*surfaces
) * level_count
* layer_count
)))
1426 wined3d_texture_cleanup(texture
);
1427 return E_OUTOFMEMORY
;
1430 /* Generate all the surfaces. */
1431 surface_desc
= *desc
;
1432 surface_desc
.resource_type
= WINED3D_RTYPE_SURFACE
;
1433 for (i
= 0; i
< texture
->level_count
; ++i
)
1435 for (j
= 0; j
< texture
->layer_count
; ++j
)
1437 static const GLenum cube_targets
[6] =
1439 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
,
1440 GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
,
1441 GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
,
1442 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
,
1443 GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
,
1444 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
,
1446 GLenum target
= desc
->usage
& WINED3DUSAGE_LEGACY_CUBEMAP
? cube_targets
[j
] : texture
->target
;
1447 unsigned int idx
= j
* texture
->level_count
+ i
;
1448 struct wined3d_surface
*surface
;
1450 surface
= &surfaces
[idx
];
1451 if (FAILED(hr
= wined3d_surface_init(surface
, texture
, &surface_desc
, target
, i
, j
, flags
)))
1453 WARN("Failed to initialize surface, returning %#x.\n", hr
);
1454 wined3d_texture_cleanup(texture
);
1456 HeapFree(GetProcessHeap(), 0, surfaces
);
1460 if (FAILED(hr
= device_parent
->ops
->surface_created(device_parent
,
1461 texture
, idx
, &parent
, &parent_ops
)))
1463 WARN("Failed to create surface parent, hr %#x.\n", hr
);
1464 wined3d_surface_cleanup(surface
);
1465 wined3d_texture_cleanup(texture
);
1469 TRACE("parent %p, parent_ops %p.\n", parent
, parent_ops
);
1471 surface
->resource
.parent
= parent
;
1472 surface
->resource
.parent_ops
= parent_ops
;
1473 texture
->sub_resources
[idx
].resource
= &surface
->resource
;
1474 texture
->sub_resources
[idx
].u
.surface
= surface
;
1475 TRACE("Created surface level %u, layer %u @ %p.\n", i
, j
, surface
);
1477 /* Calculate the next mipmap level. */
1478 surface_desc
.width
= max(1, surface_desc
.width
>> 1);
1479 surface_desc
.height
= max(1, surface_desc
.height
>> 1);
1485 static void texture3d_sub_resource_load(struct wined3d_resource
*sub_resource
,
1486 struct wined3d_context
*context
, BOOL srgb
)
1488 wined3d_volume_load(volume_from_resource(sub_resource
), context
, srgb
);
1491 static void texture3d_sub_resource_add_dirty_region(struct wined3d_resource
*sub_resource
,
1492 const struct wined3d_box
*dirty_region
)
1494 wined3d_texture_set_dirty(volume_from_resource(sub_resource
)->container
);
1497 static void texture3d_sub_resource_invalidate_location(struct wined3d_resource
*sub_resource
, DWORD location
)
1499 struct wined3d_volume
*volume
= volume_from_resource(sub_resource
);
1501 wined3d_volume_invalidate_location(volume
, location
);
1504 static void texture3d_sub_resource_validate_location(struct wined3d_resource
*sub_resource
, DWORD location
)
1506 struct wined3d_volume
*volume
= volume_from_resource(sub_resource
);
1508 wined3d_volume_validate_location(volume
, location
);
1511 static void texture3d_sub_resource_upload_data(struct wined3d_resource
*sub_resource
,
1512 const struct wined3d_context
*context
, const struct wined3d_sub_resource_data
*data
)
1514 struct wined3d_volume
*volume
= volume_from_resource(sub_resource
);
1515 struct wined3d_const_bo_address addr
;
1516 unsigned int row_pitch
, slice_pitch
;
1518 wined3d_texture_get_pitch(volume
->container
, volume
->texture_level
, &row_pitch
, &slice_pitch
);
1519 if (row_pitch
!= data
->row_pitch
|| slice_pitch
!= data
->slice_pitch
)
1520 FIXME("Ignoring row/slice pitch (%u/%u).\n", data
->row_pitch
, data
->slice_pitch
);
1522 addr
.buffer_object
= 0;
1523 addr
.addr
= data
->data
;
1525 wined3d_volume_upload_data(volume
, context
, &addr
);
1528 static BOOL
texture3d_load_location(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
,
1529 struct wined3d_context
*context
, DWORD location
)
1531 return wined3d_volume_load_location(texture
->sub_resources
[sub_resource_idx
].u
.volume
, context
, location
);
1534 static BOOL
texture3d_prepare_location(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
,
1535 struct wined3d_context
*context
, DWORD location
)
1537 return wined3d_volume_prepare_location(texture
->sub_resources
[sub_resource_idx
].u
.volume
, context
, location
);
1540 static void texture3d_prepare_texture(struct wined3d_texture
*texture
, struct wined3d_context
*context
, BOOL srgb
)
1542 unsigned int sub_count
= texture
->level_count
* texture
->layer_count
;
1543 const struct wined3d_format
*format
= texture
->resource
.format
;
1544 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1547 wined3d_texture_bind_and_dirtify(texture
, context
, srgb
);
1549 for (i
= 0; i
< sub_count
; ++i
)
1551 struct wined3d_volume
*volume
= texture
->sub_resources
[i
].u
.volume
;
1553 GL_EXTCALL(glTexImage3D(GL_TEXTURE_3D
, volume
->texture_level
,
1554 srgb
? format
->glGammaInternal
: format
->glInternal
,
1555 volume
->resource
.width
, volume
->resource
.height
, volume
->resource
.depth
,
1556 0, format
->glFormat
, format
->glType
, NULL
));
1557 checkGLcall("glTexImage3D");
1561 static void texture3d_cleanup_sub_resources(struct wined3d_texture
*texture
)
1563 unsigned int sub_count
= texture
->level_count
* texture
->layer_count
;
1564 struct wined3d_volume
*volume
;
1567 for (i
= 0; i
< sub_count
; ++i
)
1569 if ((volume
= texture
->sub_resources
[i
].u
.volume
))
1571 TRACE("volume %p.\n", volume
);
1573 wined3d_volume_cleanup(volume
);
1574 volume
->resource
.parent_ops
->wined3d_object_destroyed(volume
->resource
.parent
);
1577 HeapFree(GetProcessHeap(), 0, texture
->sub_resources
[0].u
.volume
);
1580 static const struct wined3d_texture_ops texture3d_ops
=
1582 texture3d_sub_resource_load
,
1583 texture3d_sub_resource_add_dirty_region
,
1584 texture3d_sub_resource_invalidate_location
,
1585 texture3d_sub_resource_validate_location
,
1586 texture3d_sub_resource_upload_data
,
1587 texture3d_load_location
,
1588 texture3d_prepare_location
,
1589 texture3d_prepare_texture
,
1590 texture3d_cleanup_sub_resources
,
1593 BOOL
wined3d_texture_check_block_align(const struct wined3d_texture
*texture
,
1594 unsigned int level
, const struct wined3d_box
*box
)
1596 const struct wined3d_format
*format
= texture
->resource
.format
;
1597 unsigned int height
= wined3d_texture_get_level_height(texture
, level
);
1598 unsigned int width
= wined3d_texture_get_level_width(texture
, level
);
1599 unsigned int width_mask
, height_mask
;
1601 if ((box
->left
>= box
->right
)
1602 || (box
->top
>= box
->bottom
)
1603 || (box
->right
> width
)
1604 || (box
->bottom
> height
))
1607 /* This assumes power of two block sizes, but NPOT block sizes would be
1610 * This also assumes that the format's block depth is 1. */
1611 width_mask
= format
->block_width
- 1;
1612 height_mask
= format
->block_height
- 1;
1614 if ((box
->left
& width_mask
) || (box
->top
& height_mask
)
1615 || (box
->right
& width_mask
&& box
->right
!= width
)
1616 || (box
->bottom
& height_mask
&& box
->bottom
!= height
))
1622 static HRESULT
volumetexture_init(struct wined3d_texture
*texture
, const struct wined3d_resource_desc
*desc
,
1623 UINT levels
, struct wined3d_device
*device
, void *parent
, const struct wined3d_parent_ops
*parent_ops
)
1625 struct wined3d_device_parent
*device_parent
= device
->device_parent
;
1626 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
1627 struct wined3d_resource_desc volume_desc
;
1628 struct wined3d_volume
*volumes
;
1632 /* TODO: It should only be possible to create textures for formats
1633 * that are reported as supported. */
1634 if (WINED3DFMT_UNKNOWN
>= desc
->format
)
1636 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture
);
1637 return WINED3DERR_INVALIDCALL
;
1640 if (!gl_info
->supported
[EXT_TEXTURE3D
])
1642 WARN("(%p) : Texture cannot be created - no volume texture support.\n", texture
);
1643 return WINED3DERR_INVALIDCALL
;
1646 /* Calculate levels for mip mapping. */
1647 if (desc
->usage
& WINED3DUSAGE_AUTOGENMIPMAP
)
1649 if (!gl_info
->supported
[SGIS_GENERATE_MIPMAP
])
1651 WARN("No mipmap generation support, returning D3DERR_INVALIDCALL.\n");
1652 return WINED3DERR_INVALIDCALL
;
1657 WARN("WINED3DUSAGE_AUTOGENMIPMAP is set, and level count != 1, returning D3DERR_INVALIDCALL.\n");
1658 return WINED3DERR_INVALIDCALL
;
1662 if (!gl_info
->supported
[ARB_TEXTURE_NON_POWER_OF_TWO
])
1664 UINT pow2_w
, pow2_h
, pow2_d
;
1666 while (pow2_w
< desc
->width
)
1669 while (pow2_h
< desc
->height
)
1672 while (pow2_d
< desc
->depth
)
1675 if (pow2_w
!= desc
->width
|| pow2_h
!= desc
->height
|| pow2_d
!= desc
->depth
)
1677 if (desc
->pool
== WINED3D_POOL_SCRATCH
)
1679 WARN("Creating a scratch NPOT volume texture despite lack of HW support.\n");
1683 WARN("Attempted to create a NPOT volume texture (%u, %u, %u) without GL support.\n",
1684 desc
->width
, desc
->height
, desc
->depth
);
1685 return WINED3DERR_INVALIDCALL
;
1690 if (FAILED(hr
= wined3d_texture_init(texture
, &texture3d_ops
, 1, levels
, desc
,
1691 0, device
, parent
, parent_ops
, &texture_resource_ops
)))
1693 WARN("Failed to initialize texture, returning %#x.\n", hr
);
1697 texture
->pow2_matrix
[0] = 1.0f
;
1698 texture
->pow2_matrix
[5] = 1.0f
;
1699 texture
->pow2_matrix
[10] = 1.0f
;
1700 texture
->pow2_matrix
[15] = 1.0f
;
1701 texture
->target
= GL_TEXTURE_3D
;
1703 if (!(volumes
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*volumes
) * levels
)))
1705 wined3d_texture_cleanup(texture
);
1706 return E_OUTOFMEMORY
;
1709 /* Generate all the surfaces. */
1710 volume_desc
= *desc
;
1711 volume_desc
.resource_type
= WINED3D_RTYPE_VOLUME
;
1712 for (i
= 0; i
< texture
->level_count
; ++i
)
1714 struct wined3d_volume
*volume
;
1716 volume
= &volumes
[i
];
1717 if (FAILED(hr
= wined3d_volume_init(volume
, texture
, &volume_desc
, i
)))
1719 WARN("Failed to initialize volume, returning %#x.\n", hr
);
1720 wined3d_texture_cleanup(texture
);
1722 HeapFree(GetProcessHeap(), 0, volumes
);
1726 if (FAILED(hr
= device_parent
->ops
->volume_created(device_parent
,
1727 texture
, i
, &parent
, &parent_ops
)))
1729 WARN("Failed to create volume parent, hr %#x.\n", hr
);
1730 wined3d_volume_cleanup(volume
);
1731 wined3d_texture_cleanup(texture
);
1735 TRACE("parent %p, parent_ops %p.\n", parent
, parent_ops
);
1737 volume
->resource
.parent
= parent
;
1738 volume
->resource
.parent_ops
= parent_ops
;
1739 texture
->sub_resources
[i
].resource
= &volume
->resource
;
1740 texture
->sub_resources
[i
].u
.volume
= volume
;
1741 TRACE("Created volume level %u @ %p.\n", i
, volume
);
1743 /* Calculate the next mipmap level. */
1744 volume_desc
.width
= max(1, volume_desc
.width
>> 1);
1745 volume_desc
.height
= max(1, volume_desc
.height
>> 1);
1746 volume_desc
.depth
= max(1, volume_desc
.depth
>> 1);
1752 HRESULT CDECL
wined3d_texture_blt(struct wined3d_texture
*dst_texture
, unsigned int dst_sub_resource_idx
,
1753 const RECT
*dst_rect
, struct wined3d_texture
*src_texture
, unsigned int src_sub_resource_idx
,
1754 const RECT
*src_rect
, DWORD flags
, const struct wined3d_blt_fx
*fx
, enum wined3d_texture_filter_type filter
)
1756 struct wined3d_resource
*dst_resource
, *src_resource
= NULL
;
1758 TRACE("dst_texture %p, dst_sub_resource_idx %u, dst_rect %s, src_texture %p, "
1759 "src_sub_resource_idx %u, src_rect %s, flags %#x, fx %p, filter %s.\n",
1760 dst_texture
, dst_sub_resource_idx
, wine_dbgstr_rect(dst_rect
), src_texture
,
1761 src_sub_resource_idx
, wine_dbgstr_rect(src_rect
), flags
, fx
, debug_d3dtexturefiltertype(filter
));
1763 if (!(dst_resource
= wined3d_texture_get_sub_resource(dst_texture
, dst_sub_resource_idx
))
1764 || dst_resource
->type
!= WINED3D_RTYPE_SURFACE
)
1765 return WINED3DERR_INVALIDCALL
;
1769 if (!(src_resource
= wined3d_texture_get_sub_resource(src_texture
, src_sub_resource_idx
))
1770 || src_resource
->type
!= WINED3D_RTYPE_SURFACE
)
1771 return WINED3DERR_INVALIDCALL
;
1774 return wined3d_surface_blt(surface_from_resource(dst_resource
), dst_rect
,
1775 src_resource
? surface_from_resource(src_resource
) : NULL
, src_rect
, flags
, fx
, filter
);
1778 HRESULT CDECL
wined3d_texture_get_overlay_position(const struct wined3d_texture
*texture
,
1779 unsigned int sub_resource_idx
, LONG
*x
, LONG
*y
)
1781 struct wined3d_resource
*sub_resource
;
1782 struct wined3d_surface
*surface
;
1784 TRACE("texture %p, sub_resource_idx %u, x %p, y %p.\n", texture
, sub_resource_idx
, x
, y
);
1786 if (!(texture
->resource
.usage
& WINED3DUSAGE_OVERLAY
) || texture
->resource
.type
!= WINED3D_RTYPE_TEXTURE_2D
1787 || !(sub_resource
= wined3d_texture_get_sub_resource(texture
, sub_resource_idx
)))
1789 WARN("Invalid sub-resource specified.\n");
1790 return WINEDDERR_NOTAOVERLAYSURFACE
;
1793 surface
= surface_from_resource(sub_resource
);
1794 if (!surface
->overlay_dest
)
1796 TRACE("Overlay not visible.\n");
1799 return WINEDDERR_OVERLAYNOTVISIBLE
;
1802 *x
= surface
->overlay_destrect
.left
;
1803 *y
= surface
->overlay_destrect
.top
;
1805 TRACE("Returning position %d, %d.\n", *x
, *y
);
1810 HRESULT CDECL
wined3d_texture_set_overlay_position(struct wined3d_texture
*texture
,
1811 unsigned int sub_resource_idx
, LONG x
, LONG y
)
1813 struct wined3d_resource
*sub_resource
;
1814 struct wined3d_surface
*surface
;
1817 TRACE("texture %p, sub_resource_idx %u, x %d, y %d.\n", texture
, sub_resource_idx
, x
, y
);
1819 if (!(texture
->resource
.usage
& WINED3DUSAGE_OVERLAY
) || texture
->resource
.type
!= WINED3D_RTYPE_TEXTURE_2D
1820 || !(sub_resource
= wined3d_texture_get_sub_resource(texture
, sub_resource_idx
)))
1822 WARN("Invalid sub-resource specified.\n");
1823 return WINEDDERR_NOTAOVERLAYSURFACE
;
1826 surface
= surface_from_resource(sub_resource
);
1827 w
= surface
->overlay_destrect
.right
- surface
->overlay_destrect
.left
;
1828 h
= surface
->overlay_destrect
.bottom
- surface
->overlay_destrect
.top
;
1829 surface
->overlay_destrect
.left
= x
;
1830 surface
->overlay_destrect
.top
= y
;
1831 surface
->overlay_destrect
.right
= x
+ w
;
1832 surface
->overlay_destrect
.bottom
= y
+ h
;
1837 HRESULT CDECL
wined3d_texture_update_overlay(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
,
1838 const RECT
*src_rect
, struct wined3d_texture
*dst_texture
, unsigned int dst_sub_resource_idx
,
1839 const RECT
*dst_rect
, DWORD flags
)
1841 struct wined3d_resource
*sub_resource
, *dst_sub_resource
;
1842 struct wined3d_surface
*surface
, *dst_surface
;
1844 TRACE("texture %p, sub_resource_idx %u, src_rect %s, dst_texture %p, "
1845 "dst_sub_resource_idx %u, dst_rect %s, flags %#x.\n",
1846 texture
, sub_resource_idx
, wine_dbgstr_rect(src_rect
), dst_texture
,
1847 dst_sub_resource_idx
, wine_dbgstr_rect(dst_rect
), flags
);
1849 if (!(texture
->resource
.usage
& WINED3DUSAGE_OVERLAY
) || texture
->resource
.type
!= WINED3D_RTYPE_TEXTURE_2D
1850 || !(sub_resource
= wined3d_texture_get_sub_resource(texture
, sub_resource_idx
)))
1852 WARN("Invalid sub-resource specified.\n");
1853 return WINEDDERR_NOTAOVERLAYSURFACE
;
1856 if (!dst_texture
|| dst_texture
->resource
.type
!= WINED3D_RTYPE_TEXTURE_2D
1857 || !(dst_sub_resource
= wined3d_texture_get_sub_resource(dst_texture
, dst_sub_resource_idx
)))
1859 WARN("Invalid destination sub-resource specified.\n");
1860 return WINED3DERR_INVALIDCALL
;
1863 surface
= surface_from_resource(sub_resource
);
1865 surface
->overlay_srcrect
= *src_rect
;
1867 SetRect(&surface
->overlay_srcrect
, 0, 0, surface
->resource
.width
, surface
->resource
.height
);
1869 dst_surface
= surface_from_resource(dst_sub_resource
);
1871 surface
->overlay_destrect
= *dst_rect
;
1873 SetRect(&surface
->overlay_destrect
, 0, 0, dst_surface
->resource
.width
, dst_surface
->resource
.height
);
1875 if (surface
->overlay_dest
&& (surface
->overlay_dest
!= dst_surface
|| flags
& WINEDDOVER_HIDE
))
1877 surface
->overlay_dest
= NULL
;
1878 list_remove(&surface
->overlay_entry
);
1881 if (flags
& WINEDDOVER_SHOW
)
1883 if (surface
->overlay_dest
!= dst_surface
)
1885 surface
->overlay_dest
= dst_surface
;
1886 list_add_tail(&dst_surface
->overlays
, &surface
->overlay_entry
);
1889 else if (flags
& WINEDDOVER_HIDE
)
1891 /* Tests show that the rectangles are erased on hide. */
1892 SetRectEmpty(&surface
->overlay_srcrect
);
1893 SetRectEmpty(&surface
->overlay_destrect
);
1894 surface
->overlay_dest
= NULL
;
1900 void * CDECL
wined3d_texture_get_sub_resource_parent(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
)
1902 unsigned int sub_count
= texture
->level_count
* texture
->layer_count
;
1904 TRACE("texture %p, sub_resource_idx %u.\n", texture
, sub_resource_idx
);
1906 if (sub_resource_idx
>= sub_count
)
1908 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx
, sub_count
);
1912 return texture
->sub_resources
[sub_resource_idx
].resource
->parent
;
1915 void CDECL
wined3d_texture_set_sub_resource_parent(struct wined3d_texture
*texture
,
1916 unsigned int sub_resource_idx
, void *parent
)
1918 unsigned int sub_count
= texture
->level_count
* texture
->layer_count
;
1920 TRACE("texture %p, sub_resource_idx %u, parent %p.\n", texture
, sub_resource_idx
, parent
);
1922 if (sub_resource_idx
>= sub_count
)
1924 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx
, sub_count
);
1928 texture
->sub_resources
[sub_resource_idx
].resource
->parent
= parent
;
1931 HRESULT CDECL
wined3d_texture_get_sub_resource_desc(const struct wined3d_texture
*texture
,
1932 unsigned int sub_resource_idx
, struct wined3d_sub_resource_desc
*desc
)
1934 unsigned int sub_count
= texture
->level_count
* texture
->layer_count
;
1935 const struct wined3d_resource
*resource
;
1936 unsigned int level_idx
;
1938 TRACE("texture %p, sub_resource_idx %u, desc %p.\n", texture
, sub_resource_idx
, desc
);
1940 if (sub_resource_idx
>= sub_count
)
1942 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx
, sub_count
);
1943 return WINED3DERR_INVALIDCALL
;
1946 resource
= &texture
->resource
;
1947 desc
->format
= resource
->format
->id
;
1948 desc
->multisample_type
= resource
->multisample_type
;
1949 desc
->multisample_quality
= resource
->multisample_quality
;
1950 desc
->usage
= resource
->usage
;
1951 desc
->pool
= resource
->pool
;
1953 level_idx
= sub_resource_idx
% texture
->level_count
;
1954 desc
->width
= wined3d_texture_get_level_width(texture
, level_idx
);
1955 desc
->height
= wined3d_texture_get_level_height(texture
, level_idx
);
1956 desc
->depth
= wined3d_texture_get_level_depth(texture
, level_idx
);
1957 desc
->size
= texture
->sub_resources
[sub_resource_idx
].resource
->size
;
1962 HRESULT CDECL
wined3d_texture_create(struct wined3d_device
*device
, const struct wined3d_resource_desc
*desc
,
1963 UINT level_count
, DWORD flags
, const struct wined3d_sub_resource_data
*data
, void *parent
,
1964 const struct wined3d_parent_ops
*parent_ops
, struct wined3d_texture
**texture
)
1966 unsigned int layer_count
= desc
->usage
& WINED3DUSAGE_LEGACY_CUBEMAP
? 6 : 1;
1967 struct wined3d_texture
*object
;
1970 TRACE("device %p, desc %p, level_count %u, flags %#x, data %p, parent %p, parent_ops %p, texture %p.\n",
1971 device
, desc
, level_count
, flags
, data
, parent
, parent_ops
, texture
);
1975 WARN("Invalid level count.\n");
1976 return WINED3DERR_INVALIDCALL
;
1979 if (desc
->multisample_type
!= WINED3D_MULTISAMPLE_NONE
)
1981 const struct wined3d_format
*format
= wined3d_get_format(&device
->adapter
->gl_info
, desc
->format
);
1983 if (desc
->multisample_type
== WINED3D_MULTISAMPLE_NON_MASKABLE
1984 && desc
->multisample_quality
>= wined3d_popcount(format
->multisample_types
))
1986 WARN("Unsupported quality level %u requested for WINED3D_MULTISAMPLE_NON_MASKABLE.\n",
1987 desc
->multisample_quality
);
1988 return WINED3DERR_NOTAVAILABLE
;
1990 if (desc
->multisample_type
!= WINED3D_MULTISAMPLE_NON_MASKABLE
1991 && (!(format
->multisample_types
& 1u << (desc
->multisample_type
- 1))
1992 || desc
->multisample_quality
))
1994 WARN("Unsupported multisample type %u quality %u requested.\n", desc
->multisample_type
,
1995 desc
->multisample_quality
);
1996 return WINED3DERR_NOTAVAILABLE
;
2000 if (!(object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
2001 FIELD_OFFSET(struct wined3d_texture
, sub_resources
[level_count
* layer_count
]))))
2002 return E_OUTOFMEMORY
;
2004 switch (desc
->resource_type
)
2006 case WINED3D_RTYPE_TEXTURE_2D
:
2007 hr
= texture_init(object
, desc
, layer_count
, level_count
, flags
, device
, parent
, parent_ops
);
2010 case WINED3D_RTYPE_TEXTURE_3D
:
2011 hr
= volumetexture_init(object
, desc
, level_count
, device
, parent
, parent_ops
);
2015 ERR("Invalid resource type %s.\n", debug_d3dresourcetype(desc
->resource_type
));
2016 hr
= WINED3DERR_INVALIDCALL
;
2022 WARN("Failed to initialize texture, returning %#x.\n", hr
);
2023 HeapFree(GetProcessHeap(), 0, object
);
2027 /* FIXME: We'd like to avoid ever allocating system memory for the texture
2029 if (data
&& FAILED(hr
= wined3d_texture_upload_data(object
, data
)))
2031 wined3d_texture_cleanup(object
);
2032 HeapFree(GetProcessHeap(), 0, object
);
2036 TRACE("Created texture %p.\n", object
);
2042 HRESULT CDECL
wined3d_texture_get_dc(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
, HDC
*dc
)
2044 struct wined3d_device
*device
= texture
->resource
.device
;
2045 struct wined3d_context
*context
= NULL
;
2046 struct wined3d_resource
*sub_resource
;
2047 struct wined3d_surface
*surface
;
2050 TRACE("texture %p, sub_resource_idx %u, dc %p.\n", texture
, sub_resource_idx
, dc
);
2052 if (!(sub_resource
= wined3d_texture_get_sub_resource(texture
, sub_resource_idx
)))
2053 return WINED3DERR_INVALIDCALL
;
2055 if (sub_resource
->type
!= WINED3D_RTYPE_SURFACE
)
2057 WARN("Not supported on %s resources.\n", debug_d3dresourcetype(texture
->resource
.type
));
2058 return WINED3DERR_INVALIDCALL
;
2061 surface
= surface_from_resource(sub_resource
);
2063 /* Give more detailed info for ddraw. */
2064 if (surface
->flags
& SFLAG_DCINUSE
)
2065 return WINEDDERR_DCALREADYCREATED
;
2067 /* Can't GetDC if the surface is locked. */
2068 if (surface
->resource
.map_count
)
2069 return WINED3DERR_INVALIDCALL
;
2071 if (device
->d3d_initialized
)
2072 context
= context_acquire(device
, NULL
);
2074 /* Create a DIB section if there isn't a dc yet. */
2077 if (FAILED(hr
= surface_create_dib_section(surface
)))
2080 context_release(context
);
2081 return WINED3DERR_INVALIDCALL
;
2083 if (!(surface
->resource
.map_binding
== WINED3D_LOCATION_USER_MEMORY
2084 || texture
->flags
& WINED3D_TEXTURE_PIN_SYSMEM
2085 || texture
->sub_resources
[sub_resource_idx
].buffer_object
))
2086 surface
->resource
.map_binding
= WINED3D_LOCATION_DIB
;
2089 surface_load_location(surface
, context
, WINED3D_LOCATION_DIB
);
2090 surface_invalidate_location(surface
, ~WINED3D_LOCATION_DIB
);
2093 context_release(context
);
2095 surface
->flags
|= SFLAG_DCINUSE
;
2096 surface
->resource
.map_count
++;
2099 TRACE("Returning dc %p.\n", *dc
);
2104 HRESULT CDECL
wined3d_texture_release_dc(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
, HDC dc
)
2106 struct wined3d_device
*device
= texture
->resource
.device
;
2107 struct wined3d_context
*context
= NULL
;
2108 struct wined3d_resource
*sub_resource
;
2109 struct wined3d_surface
*surface
;
2111 TRACE("texture %p, sub_resource_idx %u, dc %p.\n", texture
, sub_resource_idx
, dc
);
2113 if (!(sub_resource
= wined3d_texture_get_sub_resource(texture
, sub_resource_idx
)))
2114 return WINED3DERR_INVALIDCALL
;
2116 if (sub_resource
->type
!= WINED3D_RTYPE_SURFACE
)
2118 WARN("Not supported on %s resources.\n", debug_d3dresourcetype(texture
->resource
.type
));
2119 return WINED3DERR_INVALIDCALL
;
2122 surface
= surface_from_resource(sub_resource
);
2124 if (!(surface
->flags
& SFLAG_DCINUSE
))
2125 return WINEDDERR_NODC
;
2127 if (surface
->hDC
!= dc
)
2129 WARN("Application tries to release invalid DC %p, surface DC is %p.\n",
2131 return WINEDDERR_NODC
;
2134 surface
->resource
.map_count
--;
2135 surface
->flags
&= ~SFLAG_DCINUSE
;
2137 if (surface
->resource
.map_binding
== WINED3D_LOCATION_USER_MEMORY
2138 || (surface
->container
->flags
& WINED3D_TEXTURE_PIN_SYSMEM
2139 && surface
->resource
.map_binding
!= WINED3D_LOCATION_DIB
))
2141 /* The game Salammbo modifies the surface contents without mapping the surface between
2142 * a GetDC/ReleaseDC operation and flipping the surface. If the DIB remains the active
2143 * copy and is copied to the screen, this update, which draws the mouse pointer, is lost.
2144 * Do not only copy the DIB to the map location, but also make sure the map location is
2145 * copied back to the DIB in the next getdc call.
2147 * The same consideration applies to user memory surfaces. */
2149 if (device
->d3d_initialized
)
2150 context
= context_acquire(device
, NULL
);
2152 surface_load_location(surface
, context
, surface
->resource
.map_binding
);
2153 surface_invalidate_location(surface
, WINED3D_LOCATION_DIB
);
2155 context_release(context
);