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
);
28 WINE_DECLARE_DEBUG_CHANNEL(d3d_perf
);
29 WINE_DECLARE_DEBUG_CHANNEL(winediag
);
31 BOOL
wined3d_texture_use_pbo(const struct wined3d_texture
*texture
, const struct wined3d_gl_info
*gl_info
)
33 return texture
->resource
.pool
== WINED3D_POOL_DEFAULT
34 && texture
->resource
.access_flags
& WINED3D_RESOURCE_ACCESS_CPU
35 && gl_info
->supported
[ARB_PIXEL_BUFFER_OBJECT
]
36 && !texture
->resource
.format
->convert
37 && !(texture
->flags
& (WINED3D_TEXTURE_PIN_SYSMEM
| WINED3D_TEXTURE_COND_NP2_EMULATED
));
40 GLenum
wined3d_texture_get_gl_buffer(const struct wined3d_texture
*texture
)
42 const struct wined3d_swapchain
*swapchain
= texture
->swapchain
;
44 TRACE("texture %p.\n", texture
);
48 ERR("Texture %p is not part of a swapchain.\n", texture
);
52 if (swapchain
->back_buffers
&& swapchain
->back_buffers
[0] == texture
)
54 if (swapchain
->render_to_fbo
)
56 TRACE("Returning GL_COLOR_ATTACHMENT0.\n");
57 return GL_COLOR_ATTACHMENT0
;
59 TRACE("Returning GL_BACK.\n");
62 else if (texture
== swapchain
->front_buffer
)
64 TRACE("Returning GL_FRONT.\n");
68 FIXME("Higher back buffer, returning GL_BACK.\n");
72 void wined3d_texture_validate_location(struct wined3d_texture
*texture
,
73 unsigned int sub_resource_idx
, DWORD location
)
75 struct wined3d_texture_sub_resource
*sub_resource
;
77 TRACE("texture %p, sub_resource_idx %u, location %s.\n",
78 texture
, sub_resource_idx
, wined3d_debug_location(location
));
80 sub_resource
= &texture
->sub_resources
[sub_resource_idx
];
81 sub_resource
->locations
|= location
;
83 TRACE("New locations flags are %s.\n", wined3d_debug_location(sub_resource
->locations
));
86 void wined3d_texture_invalidate_location(struct wined3d_texture
*texture
,
87 unsigned int sub_resource_idx
, DWORD location
)
89 struct wined3d_texture_sub_resource
*sub_resource
;
91 TRACE("texture %p, sub_resource_idx %u, location %s.\n",
92 texture
, sub_resource_idx
, wined3d_debug_location(location
));
94 if (location
& (WINED3D_LOCATION_TEXTURE_RGB
| WINED3D_LOCATION_TEXTURE_SRGB
))
95 wined3d_texture_set_dirty(texture
);
97 sub_resource
= &texture
->sub_resources
[sub_resource_idx
];
98 sub_resource
->locations
&= ~location
;
100 TRACE("New locations flags are %s.\n", wined3d_debug_location(sub_resource
->locations
));
102 if (!sub_resource
->locations
)
103 ERR("Sub-resource %u of texture %p does not have any up to date location.\n",
104 sub_resource_idx
, texture
);
107 void wined3d_texture_get_memory(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
,
108 struct wined3d_bo_address
*data
, DWORD locations
)
110 struct wined3d_texture_sub_resource
*sub_resource
;
112 TRACE("texture %p, sub_resource_idx %u, data %p, locations %s.\n",
113 texture
, sub_resource_idx
, data
, wined3d_debug_location(locations
));
115 sub_resource
= &texture
->sub_resources
[sub_resource_idx
];
116 if (locations
& WINED3D_LOCATION_BUFFER
)
119 data
->buffer_object
= sub_resource
->buffer_object
;
122 if (locations
& WINED3D_LOCATION_USER_MEMORY
)
124 data
->addr
= texture
->user_memory
;
125 data
->buffer_object
= 0;
128 if (locations
& WINED3D_LOCATION_DIB
)
130 if (texture
->resource
.type
== WINED3D_RTYPE_TEXTURE_2D
)
132 data
->addr
= sub_resource
->u
.surface
->dib
.bitmap_data
;
133 data
->buffer_object
= 0;
136 ERR("Invalid location WINED3D_LOCATION_DIB for resource type %s.\n",
137 debug_d3dresourcetype(texture
->resource
.type
));
139 if (locations
& WINED3D_LOCATION_SYSMEM
)
141 data
->addr
= sub_resource
->resource
->heap_memory
;
142 data
->buffer_object
= 0;
146 ERR("Unexpected locations %s.\n", wined3d_debug_location(locations
));
148 data
->buffer_object
= 0;
151 static HRESULT
wined3d_texture_init(struct wined3d_texture
*texture
, const struct wined3d_texture_ops
*texture_ops
,
152 UINT layer_count
, UINT level_count
, const struct wined3d_resource_desc
*desc
, DWORD flags
,
153 struct wined3d_device
*device
, void *parent
, const struct wined3d_parent_ops
*parent_ops
,
154 const struct wined3d_resource_ops
*resource_ops
)
156 const struct wined3d_format
*format
= wined3d_get_format(&device
->adapter
->gl_info
, desc
->format
);
159 TRACE("texture %p, texture_ops %p, layer_count %u, level_count %u, resource_type %s, format %s, "
160 "multisample_type %#x, multisample_quality %#x, usage %s, pool %s, width %u, height %u, depth %u, "
161 "flags %#x, device %p, parent %p, parent_ops %p, resource_ops %p.\n",
162 texture
, texture_ops
, layer_count
, level_count
, debug_d3dresourcetype(desc
->resource_type
),
163 debug_d3dformat(desc
->format
), desc
->multisample_type
, desc
->multisample_quality
,
164 debug_d3dusage(desc
->usage
), debug_d3dpool(desc
->pool
), desc
->width
, desc
->height
, desc
->depth
,
165 flags
, device
, parent
, parent_ops
, resource_ops
);
167 if (FAILED(hr
= resource_init(&texture
->resource
, device
, desc
->resource_type
, format
,
168 desc
->multisample_type
, desc
->multisample_quality
, desc
->usage
, desc
->pool
,
169 desc
->width
, desc
->height
, desc
->depth
, 0, parent
, parent_ops
, resource_ops
)))
171 static unsigned int once
;
173 /* DXTn 3D textures are not supported. Do not write the ERR for them. */
174 if ((desc
->format
== WINED3DFMT_DXT1
|| desc
->format
== WINED3DFMT_DXT2
|| desc
->format
== WINED3DFMT_DXT3
175 || desc
->format
== WINED3DFMT_DXT4
|| desc
->format
== WINED3DFMT_DXT5
)
176 && !(format
->flags
[WINED3D_GL_RES_TYPE_TEX_2D
] & WINED3DFMT_FLAG_TEXTURE
)
177 && desc
->resource_type
!= WINED3D_RTYPE_TEXTURE_3D
&& !once
++)
178 ERR_(winediag
)("The application tried to create a DXTn texture, but the driver does not support them.\n");
180 WARN("Failed to initialize resource, returning %#x\n", hr
);
183 wined3d_resource_update_draw_binding(&texture
->resource
);
184 if ((flags
& WINED3D_TEXTURE_CREATE_MAPPABLE
) || desc
->format
== WINED3DFMT_D16_LOCKABLE
)
185 texture
->resource
.access_flags
|= WINED3D_RESOURCE_ACCESS_CPU
;
187 texture
->texture_ops
= texture_ops
;
189 texture
->layer_count
= layer_count
;
190 texture
->level_count
= level_count
;
191 texture
->filter_type
= (desc
->usage
& WINED3DUSAGE_AUTOGENMIPMAP
) ? WINED3D_TEXF_LINEAR
: WINED3D_TEXF_NONE
;
193 texture
->flags
|= WINED3D_TEXTURE_POW2_MAT_IDENT
| WINED3D_TEXTURE_NORMALIZED_COORDS
;
194 if (flags
& WINED3D_TEXTURE_CREATE_PIN_SYSMEM
)
195 texture
->flags
|= WINED3D_TEXTURE_PIN_SYSMEM
;
200 /* Context activation is done by the caller. */
201 void wined3d_texture_remove_buffer_object(struct wined3d_texture
*texture
,
202 unsigned int sub_resource_idx
, const struct wined3d_gl_info
*gl_info
)
204 GLuint
*buffer_object
;
206 buffer_object
= &texture
->sub_resources
[sub_resource_idx
].buffer_object
;
207 GL_EXTCALL(glDeleteBuffers(1, buffer_object
));
208 checkGLcall("glDeleteBuffers");
209 wined3d_texture_invalidate_location(texture
, sub_resource_idx
, WINED3D_LOCATION_BUFFER
);
212 TRACE("Deleted buffer object %u for texture %p, sub-resource %u.\n",
213 *buffer_object
, texture
, sub_resource_idx
);
216 /* A GL context is provided by the caller */
217 static void gltexture_delete(struct wined3d_device
*device
, const struct wined3d_gl_info
*gl_info
,
218 struct gl_texture
*tex
)
220 context_gl_resource_released(device
, tex
->name
, FALSE
);
221 gl_info
->gl_ops
.gl
.p_glDeleteTextures(1, &tex
->name
);
225 static void wined3d_texture_unload_gl_texture(struct wined3d_texture
*texture
)
227 struct wined3d_device
*device
= texture
->resource
.device
;
228 struct wined3d_context
*context
= NULL
;
230 if (texture
->texture_rgb
.name
|| texture
->texture_srgb
.name
)
232 context
= context_acquire(device
, NULL
);
235 if (texture
->texture_rgb
.name
)
236 gltexture_delete(device
, context
->gl_info
, &texture
->texture_rgb
);
238 if (texture
->texture_srgb
.name
)
239 gltexture_delete(device
, context
->gl_info
, &texture
->texture_srgb
);
241 if (context
) context_release(context
);
243 wined3d_texture_set_dirty(texture
);
245 resource_unload(&texture
->resource
);
248 static void wined3d_texture_cleanup(struct wined3d_texture
*texture
)
250 unsigned int sub_count
= texture
->level_count
* texture
->layer_count
;
251 struct wined3d_device
*device
= texture
->resource
.device
;
252 struct wined3d_context
*context
= NULL
;
253 const struct wined3d_gl_info
*gl_info
;
254 GLuint buffer_object
;
257 TRACE("texture %p.\n", texture
);
259 for (i
= 0; i
< sub_count
; ++i
)
261 if (!(buffer_object
= texture
->sub_resources
[i
].buffer_object
))
264 TRACE("Deleting buffer object %u.\n", buffer_object
);
266 /* We may not be able to get a context in wined3d_texture_cleanup() in
267 * general, but if a buffer object was previously created we can. */
270 context
= context_acquire(device
, NULL
);
271 gl_info
= context
->gl_info
;
274 GL_EXTCALL(glDeleteBuffers(1, &buffer_object
));
277 context_release(context
);
279 texture
->texture_ops
->texture_cleanup_sub_resources(texture
);
280 wined3d_texture_unload_gl_texture(texture
);
281 resource_cleanup(&texture
->resource
);
284 void wined3d_texture_set_swapchain(struct wined3d_texture
*texture
, struct wined3d_swapchain
*swapchain
)
286 texture
->swapchain
= swapchain
;
287 wined3d_resource_update_draw_binding(&texture
->resource
);
290 void wined3d_texture_set_dirty(struct wined3d_texture
*texture
)
292 texture
->flags
&= ~(WINED3D_TEXTURE_RGB_VALID
| WINED3D_TEXTURE_SRGB_VALID
);
295 /* Context activation is done by the caller. */
296 void wined3d_texture_bind(struct wined3d_texture
*texture
,
297 struct wined3d_context
*context
, BOOL srgb
)
299 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
300 const struct wined3d_format
*format
= texture
->resource
.format
;
301 const struct color_fixup_desc fixup
= format
->color_fixup
;
302 struct gl_texture
*gl_tex
;
305 TRACE("texture %p, context %p, srgb %#x.\n", texture
, context
, srgb
);
307 if (!needs_separate_srgb_gl_texture(context
))
310 /* sRGB mode cache for preload() calls outside drawprim. */
312 texture
->flags
|= WINED3D_TEXTURE_IS_SRGB
;
314 texture
->flags
&= ~WINED3D_TEXTURE_IS_SRGB
;
316 gl_tex
= wined3d_texture_get_gl_texture(texture
, srgb
);
317 target
= texture
->target
;
321 context_bind_texture(context
, target
, gl_tex
->name
);
325 gl_info
->gl_ops
.gl
.p_glGenTextures(1, &gl_tex
->name
);
326 checkGLcall("glGenTextures");
327 TRACE("Generated texture %d.\n", gl_tex
->name
);
331 ERR("Failed to generate a texture name.\n");
335 /* Initialise the state of the texture object to the OpenGL defaults, not
336 * the wined3d defaults. */
337 gl_tex
->sampler_desc
.address_u
= WINED3D_TADDRESS_WRAP
;
338 gl_tex
->sampler_desc
.address_v
= WINED3D_TADDRESS_WRAP
;
339 gl_tex
->sampler_desc
.address_w
= WINED3D_TADDRESS_WRAP
;
340 memset(gl_tex
->sampler_desc
.border_color
, 0, sizeof(gl_tex
->sampler_desc
.border_color
));
341 gl_tex
->sampler_desc
.mag_filter
= WINED3D_TEXF_LINEAR
;
342 gl_tex
->sampler_desc
.min_filter
= WINED3D_TEXF_POINT
; /* GL_NEAREST_MIPMAP_LINEAR */
343 gl_tex
->sampler_desc
.mip_filter
= WINED3D_TEXF_LINEAR
; /* GL_NEAREST_MIPMAP_LINEAR */
344 gl_tex
->sampler_desc
.lod_bias
= 0.0f
;
345 gl_tex
->sampler_desc
.min_lod
= -1000.0f
;
346 gl_tex
->sampler_desc
.max_lod
= 1000.0f
;
347 gl_tex
->sampler_desc
.max_anisotropy
= 1;
348 gl_tex
->sampler_desc
.compare
= FALSE
;
349 gl_tex
->sampler_desc
.comparison_func
= WINED3D_CMP_LESSEQUAL
;
350 if (context
->gl_info
->supported
[EXT_TEXTURE_SRGB_DECODE
])
351 gl_tex
->sampler_desc
.srgb_decode
= TRUE
;
353 gl_tex
->sampler_desc
.srgb_decode
= srgb
;
354 gl_tex
->base_level
= 0;
355 wined3d_texture_set_dirty(texture
);
357 context_bind_texture(context
, target
, gl_tex
->name
);
359 if (texture
->resource
.usage
& WINED3DUSAGE_AUTOGENMIPMAP
)
361 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_GENERATE_MIPMAP_SGIS
, GL_TRUE
);
362 checkGLcall("glTexParameteri(target, GL_GENERATE_MIPMAP_SGIS, GL_TRUE)");
365 /* For a new texture we have to set the texture levels after binding the
366 * texture. Beware that texture rectangles do not support mipmapping, but
367 * set the maxmiplevel if we're relying on the partial
368 * GL_ARB_texture_non_power_of_two emulation with texture rectangles.
369 * (I.e., do not care about cond_np2 here, just look for
370 * GL_TEXTURE_RECTANGLE_ARB.) */
371 if (target
!= GL_TEXTURE_RECTANGLE_ARB
)
373 TRACE("Setting GL_TEXTURE_MAX_LEVEL to %u.\n", texture
->level_count
- 1);
374 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, texture
->level_count
- 1);
375 checkGLcall("glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, texture->level_count)");
378 if (target
== GL_TEXTURE_CUBE_MAP_ARB
)
380 /* Cubemaps are always set to clamp, regardless of the sampler state. */
381 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
382 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
383 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_R
, GL_CLAMP_TO_EDGE
);
386 if (texture
->flags
& WINED3D_TEXTURE_COND_NP2
)
388 /* Conditinal non power of two textures use a different clamping
389 * default. If we're using the GL_WINE_normalized_texrect partial
390 * driver emulation, we're dealing with a GL_TEXTURE_2D texture which
391 * has the address mode set to repeat - something that prevents us
392 * from hitting the accelerated codepath. Thus manually set the GL
393 * state. The same applies to filtering. Even if the texture has only
394 * one mip level, the default LINEAR_MIPMAP_LINEAR filter causes a SW
395 * fallback on macos. */
396 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
397 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
398 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
399 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
400 checkGLcall("glTexParameteri");
401 gl_tex
->sampler_desc
.address_u
= WINED3D_TADDRESS_CLAMP
;
402 gl_tex
->sampler_desc
.address_v
= WINED3D_TADDRESS_CLAMP
;
403 gl_tex
->sampler_desc
.mag_filter
= WINED3D_TEXF_POINT
;
404 gl_tex
->sampler_desc
.min_filter
= WINED3D_TEXF_POINT
;
405 gl_tex
->sampler_desc
.mip_filter
= WINED3D_TEXF_NONE
;
408 if (gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
] && gl_info
->supported
[ARB_DEPTH_TEXTURE
])
410 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_DEPTH_TEXTURE_MODE_ARB
, GL_INTENSITY
);
411 checkGLcall("glTexParameteri(GL_DEPTH_TEXTURE_MODE_ARB, GL_INTENSITY)");
414 if (!is_identity_fixup(fixup
) && can_use_texture_swizzle(gl_info
, format
))
416 static const GLenum swizzle_source
[] =
418 GL_ZERO
, /* CHANNEL_SOURCE_ZERO */
419 GL_ONE
, /* CHANNEL_SOURCE_ONE */
420 GL_RED
, /* CHANNEL_SOURCE_X */
421 GL_GREEN
, /* CHANNEL_SOURCE_Y */
422 GL_BLUE
, /* CHANNEL_SOURCE_Z */
423 GL_ALPHA
, /* CHANNEL_SOURCE_W */
431 swizzle
.x
= swizzle_source
[fixup
.x_source
];
432 swizzle
.y
= swizzle_source
[fixup
.y_source
];
433 swizzle
.z
= swizzle_source
[fixup
.z_source
];
434 swizzle
.w
= swizzle_source
[fixup
.w_source
];
435 gl_info
->gl_ops
.gl
.p_glTexParameteriv(target
, GL_TEXTURE_SWIZZLE_RGBA
, &swizzle
.x
);
436 checkGLcall("glTexParameteriv(GL_TEXTURE_SWIZZLE_RGBA)");
440 /* Context activation is done by the caller. */
441 void wined3d_texture_bind_and_dirtify(struct wined3d_texture
*texture
,
442 struct wined3d_context
*context
, BOOL srgb
)
444 DWORD active_sampler
;
446 /* We don't need a specific texture unit, but after binding the texture
447 * the current unit is dirty. Read the unit back instead of switching to
448 * 0, this avoids messing around with the state manager's GL states. The
449 * current texture unit should always be a valid one.
451 * To be more specific, this is tricky because we can implicitly be
452 * called from sampler() in state.c. This means we can't touch anything
453 * other than whatever happens to be the currently active texture, or we
454 * would risk marking already applied sampler states dirty again. */
455 active_sampler
= context
->rev_tex_unit_map
[context
->active_texture
];
456 if (active_sampler
!= WINED3D_UNMAPPED_STAGE
)
457 context_invalidate_state(context
, STATE_SAMPLER(active_sampler
));
458 /* FIXME: Ideally we'd only do this when touching a binding that's used by
460 context_invalidate_state(context
, STATE_SHADER_RESOURCE_BINDING
);
462 wined3d_texture_bind(texture
, context
, srgb
);
465 /* Context activation is done by the caller (state handler). */
466 /* This function relies on the correct texture being bound and loaded. */
467 void wined3d_texture_apply_sampler_desc(struct wined3d_texture
*texture
,
468 const struct wined3d_sampler_desc
*sampler_desc
, const struct wined3d_context
*context
)
470 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
471 GLenum target
= texture
->target
;
472 struct gl_texture
*gl_tex
;
475 TRACE("texture %p, sampler_desc %p, context %p.\n", texture
, sampler_desc
, context
);
477 gl_tex
= wined3d_texture_get_gl_texture(texture
, texture
->flags
& WINED3D_TEXTURE_IS_SRGB
);
479 state
= sampler_desc
->address_u
;
480 if (state
!= gl_tex
->sampler_desc
.address_u
)
482 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_S
,
483 gl_info
->wrap_lookup
[state
- WINED3D_TADDRESS_WRAP
]);
484 gl_tex
->sampler_desc
.address_u
= state
;
487 state
= sampler_desc
->address_v
;
488 if (state
!= gl_tex
->sampler_desc
.address_v
)
490 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_T
,
491 gl_info
->wrap_lookup
[state
- WINED3D_TADDRESS_WRAP
]);
492 gl_tex
->sampler_desc
.address_v
= state
;
495 state
= sampler_desc
->address_w
;
496 if (state
!= gl_tex
->sampler_desc
.address_w
)
498 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_R
,
499 gl_info
->wrap_lookup
[state
- WINED3D_TADDRESS_WRAP
]);
500 gl_tex
->sampler_desc
.address_w
= state
;
503 if (memcmp(gl_tex
->sampler_desc
.border_color
, sampler_desc
->border_color
,
504 sizeof(gl_tex
->sampler_desc
.border_color
)))
506 gl_info
->gl_ops
.gl
.p_glTexParameterfv(target
, GL_TEXTURE_BORDER_COLOR
, &sampler_desc
->border_color
[0]);
507 memcpy(gl_tex
->sampler_desc
.border_color
, sampler_desc
->border_color
,
508 sizeof(gl_tex
->sampler_desc
.border_color
));
511 state
= sampler_desc
->mag_filter
;
512 if (state
!= gl_tex
->sampler_desc
.mag_filter
)
514 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MAG_FILTER
, wined3d_gl_mag_filter(state
));
515 gl_tex
->sampler_desc
.mag_filter
= state
;
518 if (sampler_desc
->min_filter
!= gl_tex
->sampler_desc
.min_filter
519 || sampler_desc
->mip_filter
!= gl_tex
->sampler_desc
.mip_filter
)
521 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MIN_FILTER
,
522 wined3d_gl_min_mip_filter(sampler_desc
->min_filter
, sampler_desc
->mip_filter
));
523 gl_tex
->sampler_desc
.min_filter
= sampler_desc
->min_filter
;
524 gl_tex
->sampler_desc
.mip_filter
= sampler_desc
->mip_filter
;
527 state
= sampler_desc
->max_anisotropy
;
528 if (state
!= gl_tex
->sampler_desc
.max_anisotropy
)
530 if (gl_info
->supported
[EXT_TEXTURE_FILTER_ANISOTROPIC
])
531 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MAX_ANISOTROPY_EXT
, state
);
533 WARN("Anisotropic filtering not supported.\n");
534 gl_tex
->sampler_desc
.max_anisotropy
= state
;
537 if (!sampler_desc
->srgb_decode
!= !gl_tex
->sampler_desc
.srgb_decode
538 && (context
->d3d_info
->wined3d_creation_flags
& WINED3D_SRGB_READ_WRITE_CONTROL
)
539 && gl_info
->supported
[EXT_TEXTURE_SRGB_DECODE
])
541 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_SRGB_DECODE_EXT
,
542 sampler_desc
->srgb_decode
? GL_DECODE_EXT
: GL_SKIP_DECODE_EXT
);
543 gl_tex
->sampler_desc
.srgb_decode
= sampler_desc
->srgb_decode
;
546 if (!sampler_desc
->compare
!= !gl_tex
->sampler_desc
.compare
)
548 if (sampler_desc
->compare
)
549 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_COMPARE_MODE_ARB
, GL_COMPARE_R_TO_TEXTURE_ARB
);
551 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_COMPARE_MODE_ARB
, GL_NONE
);
552 gl_tex
->sampler_desc
.compare
= sampler_desc
->compare
;
555 checkGLcall("Texture parameter application");
557 if (gl_info
->supported
[EXT_TEXTURE_LOD_BIAS
])
559 gl_info
->gl_ops
.gl
.p_glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT
,
560 GL_TEXTURE_LOD_BIAS_EXT
, sampler_desc
->lod_bias
);
561 checkGLcall("glTexEnvf(GL_TEXTURE_LOD_BIAS_EXT, ...)");
565 ULONG CDECL
wined3d_texture_incref(struct wined3d_texture
*texture
)
569 TRACE("texture %p, swapchain %p.\n", texture
, texture
->swapchain
);
571 if (texture
->swapchain
)
572 return wined3d_swapchain_incref(texture
->swapchain
);
574 refcount
= InterlockedIncrement(&texture
->resource
.ref
);
575 TRACE("%p increasing refcount to %u.\n", texture
, refcount
);
580 ULONG CDECL
wined3d_texture_decref(struct wined3d_texture
*texture
)
584 TRACE("texture %p, swapchain %p.\n", texture
, texture
->swapchain
);
586 if (texture
->swapchain
)
587 return wined3d_swapchain_decref(texture
->swapchain
);
589 refcount
= InterlockedDecrement(&texture
->resource
.ref
);
590 TRACE("%p decreasing refcount to %u.\n", texture
, refcount
);
594 wined3d_texture_cleanup(texture
);
595 texture
->resource
.parent_ops
->wined3d_object_destroyed(texture
->resource
.parent
);
596 HeapFree(GetProcessHeap(), 0, texture
);
602 struct wined3d_resource
* CDECL
wined3d_texture_get_resource(struct wined3d_texture
*texture
)
604 TRACE("texture %p.\n", texture
);
606 return &texture
->resource
;
609 static BOOL
color_key_equal(const struct wined3d_color_key
*c1
, struct wined3d_color_key
*c2
)
611 return c1
->color_space_low_value
== c2
->color_space_low_value
612 && c1
->color_space_high_value
== c2
->color_space_high_value
;
615 /* Context activation is done by the caller */
616 void wined3d_texture_load(struct wined3d_texture
*texture
,
617 struct wined3d_context
*context
, BOOL srgb
)
619 UINT sub_count
= texture
->level_count
* texture
->layer_count
;
620 const struct wined3d_d3d_info
*d3d_info
= context
->d3d_info
;
624 TRACE("texture %p, context %p, srgb %#x.\n", texture
, context
, srgb
);
626 if (!needs_separate_srgb_gl_texture(context
))
630 flag
= WINED3D_TEXTURE_SRGB_VALID
;
632 flag
= WINED3D_TEXTURE_RGB_VALID
;
634 if (!d3d_info
->shader_color_key
635 && (!(texture
->async
.flags
& WINED3D_TEXTURE_ASYNC_COLOR_KEY
)
636 != !(texture
->async
.color_key_flags
& WINED3D_CKEY_SRC_BLT
)
637 || (texture
->async
.flags
& WINED3D_TEXTURE_ASYNC_COLOR_KEY
638 && !color_key_equal(&texture
->async
.gl_color_key
, &texture
->async
.src_blt_color_key
))))
640 unsigned int sub_count
= texture
->level_count
* texture
->layer_count
;
643 TRACE("Reloading because of color key value change.\n");
644 for (i
= 0; i
< sub_count
; i
++)
646 struct wined3d_resource
*sub_resource
= texture
->sub_resources
[i
].resource
;
648 if (!texture
->texture_ops
->texture_load_location(texture
, i
, context
, sub_resource
->map_binding
))
649 ERR("Failed to load location %s.\n", wined3d_debug_location(sub_resource
->map_binding
));
651 wined3d_texture_invalidate_location(texture
, i
, ~sub_resource
->map_binding
);
654 texture
->async
.gl_color_key
= texture
->async
.src_blt_color_key
;
657 if (texture
->flags
& flag
)
659 TRACE("Texture %p not dirty, nothing to do.\n", texture
);
663 /* Reload the surfaces if the texture is marked dirty. */
664 for (i
= 0; i
< sub_count
; ++i
)
666 if (!texture
->texture_ops
->texture_load_location(texture
, i
, context
,
667 srgb
? WINED3D_LOCATION_TEXTURE_SRGB
: WINED3D_LOCATION_TEXTURE_RGB
))
668 ERR("Failed to load location (srgb %#x).\n", srgb
);
670 texture
->flags
|= flag
;
673 void CDECL
wined3d_texture_preload(struct wined3d_texture
*texture
)
675 struct wined3d_context
*context
;
676 context
= context_acquire(texture
->resource
.device
, NULL
);
677 wined3d_texture_load(texture
, context
, texture
->flags
& WINED3D_TEXTURE_IS_SRGB
);
678 context_release(context
);
681 void * CDECL
wined3d_texture_get_parent(const struct wined3d_texture
*texture
)
683 TRACE("texture %p.\n", texture
);
685 return texture
->resource
.parent
;
688 static BOOL
wined3d_texture_check_box_dimensions(const struct wined3d_texture
*texture
,
689 unsigned int level
, const struct wined3d_box
*box
)
691 if (box
->left
>= box
->right
692 || box
->top
>= box
->bottom
693 || box
->front
>= box
->back
)
696 if (box
->right
> wined3d_texture_get_level_width(texture
, level
)
697 || box
->bottom
> wined3d_texture_get_level_height(texture
, level
)
698 || box
->back
> wined3d_texture_get_level_depth(texture
, level
))
704 void CDECL
wined3d_texture_get_pitch(const struct wined3d_texture
*texture
,
705 unsigned int level
, unsigned int *row_pitch
, unsigned int *slice_pitch
)
707 const struct wined3d_resource
*resource
= &texture
->resource
;
708 unsigned int width
= wined3d_texture_get_level_width(texture
, level
);
709 unsigned int height
= wined3d_texture_get_level_height(texture
, level
);
711 if (texture
->row_pitch
)
713 *row_pitch
= texture
->row_pitch
;
714 *slice_pitch
= texture
->slice_pitch
;
718 wined3d_format_calculate_pitch(resource
->format
, resource
->device
->surface_alignment
,
719 width
, height
, row_pitch
, slice_pitch
);
722 DWORD CDECL
wined3d_texture_set_lod(struct wined3d_texture
*texture
, DWORD lod
)
724 DWORD old
= texture
->lod
;
726 TRACE("texture %p, lod %u.\n", texture
, lod
);
728 /* The d3d9:texture test shows that SetLOD is ignored on non-managed
729 * textures. The call always returns 0, and GetLOD always returns 0. */
730 if (texture
->resource
.pool
!= WINED3D_POOL_MANAGED
)
732 TRACE("Ignoring SetLOD on %s texture, returning 0.\n", debug_d3dpool(texture
->resource
.pool
));
736 if (lod
>= texture
->level_count
)
737 lod
= texture
->level_count
- 1;
739 if (texture
->lod
!= lod
)
743 texture
->texture_rgb
.base_level
= ~0u;
744 texture
->texture_srgb
.base_level
= ~0u;
745 if (texture
->resource
.bind_count
)
746 device_invalidate_state(texture
->resource
.device
, STATE_SAMPLER(texture
->sampler
));
752 DWORD CDECL
wined3d_texture_get_lod(const struct wined3d_texture
*texture
)
754 TRACE("texture %p, returning %u.\n", texture
, texture
->lod
);
759 DWORD CDECL
wined3d_texture_get_level_count(const struct wined3d_texture
*texture
)
761 TRACE("texture %p, returning %u.\n", texture
, texture
->level_count
);
763 return texture
->level_count
;
766 HRESULT CDECL
wined3d_texture_set_autogen_filter_type(struct wined3d_texture
*texture
,
767 enum wined3d_texture_filter_type filter_type
)
769 FIXME("texture %p, filter_type %s stub!\n", texture
, debug_d3dtexturefiltertype(filter_type
));
771 if (!(texture
->resource
.usage
& WINED3DUSAGE_AUTOGENMIPMAP
))
773 WARN("Texture doesn't have AUTOGENMIPMAP usage.\n");
774 return WINED3DERR_INVALIDCALL
;
777 texture
->filter_type
= filter_type
;
782 enum wined3d_texture_filter_type CDECL
wined3d_texture_get_autogen_filter_type(const struct wined3d_texture
*texture
)
784 TRACE("texture %p.\n", texture
);
786 return texture
->filter_type
;
789 HRESULT CDECL
wined3d_texture_set_color_key(struct wined3d_texture
*texture
,
790 DWORD flags
, const struct wined3d_color_key
*color_key
)
792 struct wined3d_device
*device
= texture
->resource
.device
;
793 static const DWORD all_flags
= WINED3D_CKEY_DST_BLT
| WINED3D_CKEY_DST_OVERLAY
794 | WINED3D_CKEY_SRC_BLT
| WINED3D_CKEY_SRC_OVERLAY
;
796 TRACE("texture %p, flags %#x, color_key %p.\n", texture
, flags
, color_key
);
798 if (flags
& ~all_flags
)
800 WARN("Invalid flags passed, returning WINED3DERR_INVALIDCALL.\n");
801 return WINED3DERR_INVALIDCALL
;
804 wined3d_cs_emit_set_color_key(device
->cs
, texture
, flags
, color_key
);
809 HRESULT CDECL
wined3d_texture_update_desc(struct wined3d_texture
*texture
, UINT width
, UINT height
,
810 enum wined3d_format_id format_id
, enum wined3d_multisample_type multisample_type
,
811 UINT multisample_quality
, void *mem
, UINT pitch
)
813 struct wined3d_device
*device
= texture
->resource
.device
;
814 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
815 const struct wined3d_format
*format
= wined3d_get_format(gl_info
, format_id
);
816 UINT resource_size
= wined3d_format_calculate_size(format
, device
->surface_alignment
, width
, height
, 1);
817 struct wined3d_texture_sub_resource
*sub_resource
;
818 struct wined3d_surface
*surface
;
819 DWORD valid_location
= 0;
820 BOOL create_dib
= FALSE
;
822 TRACE("texture %p, width %u, height %u, format %s, multisample_type %#x, multisample_quality %u, "
823 "mem %p, pitch %u.\n",
824 texture
, width
, height
, debug_d3dformat(format_id
), multisample_type
, multisample_quality
, mem
, pitch
);
827 return WINED3DERR_INVALIDCALL
;
829 if (texture
->level_count
* texture
->layer_count
> 1)
831 WARN("Texture has multiple sub-resources, not supported.\n");
832 return WINED3DERR_INVALIDCALL
;
835 if (texture
->resource
.type
== WINED3D_RTYPE_TEXTURE_3D
)
837 WARN("Not supported on 3D textures.\n");
838 return WINED3DERR_INVALIDCALL
;
841 /* We have no way of supporting a pitch that is not a multiple of the pixel
842 * byte width short of uploading the texture row-by-row.
843 * Fortunately that's not an issue since D3D9Ex doesn't allow a custom pitch
844 * for user-memory textures (it always expects packed data) while DirectDraw
845 * requires a 4-byte aligned pitch and doesn't support texture formats
846 * larger than 4 bytes per pixel nor any format using 3 bytes per pixel.
847 * This check is here to verify that the assumption holds. */
848 if (pitch
% texture
->resource
.format
->byte_count
)
850 WARN("Pitch unsupported, not a multiple of the texture format byte width.\n");
851 return WINED3DERR_INVALIDCALL
;
854 sub_resource
= &texture
->sub_resources
[0];
855 surface
= sub_resource
->u
.surface
;
856 if (sub_resource
->resource
->map_count
|| (surface
->flags
& SFLAG_DCINUSE
))
858 WARN("Surface is mapped or the DC is in use.\n");
859 return WINED3DERR_INVALIDCALL
;
862 if (device
->d3d_initialized
)
863 texture
->resource
.resource_ops
->resource_unload(&texture
->resource
);
865 if (surface
->flags
& SFLAG_DIBSECTION
)
867 DeleteDC(surface
->hDC
);
869 DeleteObject(surface
->dib
.DIBsection
);
870 surface
->dib
.DIBsection
= NULL
;
871 surface
->dib
.bitmap_data
= NULL
;
872 surface
->flags
&= ~SFLAG_DIBSECTION
;
876 wined3d_resource_free_sysmem(sub_resource
->resource
);
878 if ((texture
->row_pitch
= pitch
))
879 texture
->slice_pitch
= height
* pitch
;
881 /* User memory surfaces don't have the regular surface alignment. */
882 wined3d_format_calculate_pitch(format
, 1, width
, height
,
883 &texture
->row_pitch
, &texture
->slice_pitch
);
885 texture
->resource
.format
= format
;
886 texture
->resource
.multisample_type
= multisample_type
;
887 texture
->resource
.multisample_quality
= multisample_quality
;
888 texture
->resource
.width
= width
;
889 texture
->resource
.height
= height
;
891 sub_resource
->resource
->format
= format
;
892 sub_resource
->resource
->multisample_type
= multisample_type
;
893 sub_resource
->resource
->multisample_quality
= multisample_quality
;
894 sub_resource
->resource
->width
= width
;
895 sub_resource
->resource
->height
= height
;
896 sub_resource
->resource
->size
= texture
->slice_pitch
;
898 if (((width
& (width
- 1)) || (height
& (height
- 1))) && !gl_info
->supported
[ARB_TEXTURE_NON_POWER_OF_TWO
]
899 && !gl_info
->supported
[ARB_TEXTURE_RECTANGLE
] && !gl_info
->supported
[WINED3D_GL_NORMALIZED_TEXRECT
])
901 texture
->flags
|= WINED3D_TEXTURE_COND_NP2_EMULATED
;
902 surface
->pow2Width
= surface
->pow2Height
= 1;
903 while (surface
->pow2Width
< width
)
904 surface
->pow2Width
<<= 1;
905 while (surface
->pow2Height
< height
)
906 surface
->pow2Height
<<= 1;
910 texture
->flags
&= ~WINED3D_TEXTURE_COND_NP2_EMULATED
;
911 surface
->pow2Width
= width
;
912 surface
->pow2Height
= height
;
915 sub_resource
->locations
= 0;
917 if ((texture
->user_memory
= mem
))
919 sub_resource
->resource
->map_binding
= WINED3D_LOCATION_USER_MEMORY
;
920 valid_location
= WINED3D_LOCATION_USER_MEMORY
;
922 else if (create_dib
&& SUCCEEDED(surface_create_dib_section(surface
)))
924 valid_location
= WINED3D_LOCATION_DIB
;
928 wined3d_surface_prepare(surface
, NULL
, WINED3D_LOCATION_SYSMEM
);
929 valid_location
= WINED3D_LOCATION_SYSMEM
;
932 /* The format might be changed to a format that needs conversion.
933 * If the surface didn't use PBOs previously but could now, don't
934 * change it - whatever made us not use PBOs might come back, e.g.
936 if (sub_resource
->resource
->map_binding
== WINED3D_LOCATION_BUFFER
&& !wined3d_texture_use_pbo(texture
, gl_info
))
937 sub_resource
->resource
->map_binding
= surface
->dib
.DIBsection
? WINED3D_LOCATION_DIB
: WINED3D_LOCATION_SYSMEM
;
939 wined3d_texture_validate_location(texture
, 0, valid_location
);
944 /* Context activation is done by the caller. */
945 void wined3d_texture_prepare_buffer_object(struct wined3d_texture
*texture
,
946 unsigned int sub_resource_idx
, const struct wined3d_gl_info
*gl_info
)
948 GLuint
*buffer_object
;
950 buffer_object
= &texture
->sub_resources
[sub_resource_idx
].buffer_object
;
954 GL_EXTCALL(glGenBuffers(1, buffer_object
));
955 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER
, *buffer_object
));
956 GL_EXTCALL(glBufferData(GL_PIXEL_UNPACK_BUFFER
,
957 texture
->sub_resources
[sub_resource_idx
].resource
->size
, NULL
, GL_STREAM_DRAW
));
958 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER
, 0));
959 checkGLcall("Create buffer object");
961 TRACE("Created buffer object %u for texture %p, sub-resource %u.\n",
962 *buffer_object
, texture
, sub_resource_idx
);
965 static void wined3d_texture_force_reload(struct wined3d_texture
*texture
)
967 unsigned int sub_count
= texture
->level_count
* texture
->layer_count
;
970 texture
->flags
&= ~(WINED3D_TEXTURE_RGB_ALLOCATED
| WINED3D_TEXTURE_SRGB_ALLOCATED
971 | WINED3D_TEXTURE_CONVERTED
);
972 texture
->async
.flags
&= ~WINED3D_TEXTURE_ASYNC_COLOR_KEY
;
973 for (i
= 0; i
< sub_count
; ++i
)
975 wined3d_texture_invalidate_location(texture
, i
,
976 WINED3D_LOCATION_TEXTURE_RGB
| WINED3D_LOCATION_TEXTURE_SRGB
);
980 void wined3d_texture_prepare_texture(struct wined3d_texture
*texture
, struct wined3d_context
*context
, BOOL srgb
)
982 DWORD alloc_flag
= srgb
? WINED3D_TEXTURE_SRGB_ALLOCATED
: WINED3D_TEXTURE_RGB_ALLOCATED
;
983 const struct wined3d_d3d_info
*d3d_info
= context
->d3d_info
;
985 if (!d3d_info
->shader_color_key
986 && !(texture
->async
.flags
& WINED3D_TEXTURE_ASYNC_COLOR_KEY
)
987 != !(texture
->async
.color_key_flags
& WINED3D_CKEY_SRC_BLT
))
989 wined3d_texture_force_reload(texture
);
991 if (texture
->async
.color_key_flags
& WINED3D_CKEY_SRC_BLT
)
992 texture
->async
.flags
|= WINED3D_TEXTURE_ASYNC_COLOR_KEY
;
995 if (texture
->flags
& alloc_flag
)
998 texture
->texture_ops
->texture_prepare_texture(texture
, context
, srgb
);
999 texture
->flags
|= alloc_flag
;
1002 void CDECL
wined3d_texture_generate_mipmaps(struct wined3d_texture
*texture
)
1004 /* TODO: Implement filters using GL_SGI_generate_mipmaps. */
1005 FIXME("texture %p stub!\n", texture
);
1008 struct wined3d_texture_sub_resource
*wined3d_texture_get_sub_resource(struct wined3d_texture
*texture
,
1009 unsigned int sub_resource_idx
)
1011 UINT sub_count
= texture
->level_count
* texture
->layer_count
;
1013 TRACE("texture %p, sub_resource_idx %u.\n", texture
, sub_resource_idx
);
1015 if (sub_resource_idx
>= sub_count
)
1017 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx
, sub_count
);
1021 return &texture
->sub_resources
[sub_resource_idx
];
1024 HRESULT CDECL
wined3d_texture_add_dirty_region(struct wined3d_texture
*texture
,
1025 UINT layer
, const struct wined3d_box
*dirty_region
)
1027 struct wined3d_texture_sub_resource
*sub_resource
;
1028 struct wined3d_context
*context
;
1029 unsigned int sub_resource_idx
;
1031 TRACE("texture %p, layer %u, dirty_region %s.\n", texture
, layer
, debug_box(dirty_region
));
1033 sub_resource_idx
= layer
* texture
->level_count
;
1034 if (!(sub_resource
= wined3d_texture_get_sub_resource(texture
, sub_resource_idx
)))
1036 WARN("Failed to get sub-resource.\n");
1037 return WINED3DERR_INVALIDCALL
;
1041 FIXME("Ignoring dirty_region %s.\n", debug_box(dirty_region
));
1043 context
= context_acquire(texture
->resource
.device
, NULL
);
1044 if (!texture
->texture_ops
->texture_load_location(texture
, sub_resource_idx
,
1045 context
, sub_resource
->resource
->map_binding
))
1047 ERR("Failed to load location %s.\n", wined3d_debug_location(sub_resource
->resource
->map_binding
));
1048 context_release(context
);
1049 return E_OUTOFMEMORY
;
1051 wined3d_texture_invalidate_location(texture
, sub_resource_idx
, ~sub_resource
->resource
->map_binding
);
1052 context_release(context
);
1057 static HRESULT
wined3d_texture_upload_data(struct wined3d_texture
*texture
,
1058 const struct wined3d_sub_resource_data
*data
)
1060 unsigned int sub_count
= texture
->level_count
* texture
->layer_count
;
1061 struct wined3d_context
*context
;
1064 for (i
= 0; i
< sub_count
; ++i
)
1068 WARN("Invalid sub-resource data specified for sub-resource %u.\n", i
);
1069 return E_INVALIDARG
;
1073 context
= context_acquire(texture
->resource
.device
, NULL
);
1075 wined3d_texture_prepare_texture(texture
, context
, FALSE
);
1076 wined3d_texture_bind_and_dirtify(texture
, context
, FALSE
);
1078 for (i
= 0; i
< sub_count
; ++i
)
1080 texture
->texture_ops
->texture_upload_data(texture
, i
, context
, &data
[i
]);
1081 wined3d_texture_validate_location(texture
, i
, WINED3D_LOCATION_TEXTURE_RGB
);
1082 wined3d_texture_invalidate_location(texture
, i
, ~WINED3D_LOCATION_TEXTURE_RGB
);
1085 context_release(context
);
1090 static void texture2d_upload_data(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
,
1091 const struct wined3d_context
*context
, const struct wined3d_sub_resource_data
*data
)
1093 static const POINT dst_point
= {0, 0};
1094 struct wined3d_const_bo_address addr
;
1095 unsigned int texture_level
;
1098 texture_level
= sub_resource_idx
% texture
->level_count
;
1099 SetRect(&src_rect
, 0, 0, wined3d_texture_get_level_width(texture
, texture_level
),
1100 wined3d_texture_get_level_height(texture
, texture_level
));
1102 addr
.buffer_object
= 0;
1103 addr
.addr
= data
->data
;
1105 wined3d_surface_upload_data(texture
->sub_resources
[sub_resource_idx
].u
.surface
, context
->gl_info
,
1106 texture
->resource
.format
, &src_rect
, data
->row_pitch
, &dst_point
, FALSE
, &addr
);
1109 static BOOL
texture2d_load_location(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
,
1110 struct wined3d_context
*context
, DWORD location
)
1112 return SUCCEEDED(surface_load_location(texture
->sub_resources
[sub_resource_idx
].u
.surface
, context
, location
));
1115 static BOOL
texture2d_prepare_location(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
,
1116 struct wined3d_context
*context
, DWORD location
)
1118 wined3d_surface_prepare(texture
->sub_resources
[sub_resource_idx
].u
.surface
, context
, location
);
1122 /* Context activation is done by the caller. */
1123 static void texture2d_prepare_texture(struct wined3d_texture
*texture
, struct wined3d_context
*context
, BOOL srgb
)
1125 UINT sub_count
= texture
->level_count
* texture
->layer_count
;
1126 const struct wined3d_format
*format
= texture
->resource
.format
;
1127 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1128 const struct wined3d_color_key_conversion
*conversion
;
1132 TRACE("texture %p, context %p, format %s.\n", texture
, context
, debug_d3dformat(format
->id
));
1134 if (format
->convert
)
1136 texture
->flags
|= WINED3D_TEXTURE_CONVERTED
;
1138 else if ((conversion
= wined3d_format_get_color_key_conversion(texture
, TRUE
)))
1140 texture
->flags
|= WINED3D_TEXTURE_CONVERTED
;
1141 format
= wined3d_get_format(gl_info
, conversion
->dst_format
);
1142 TRACE("Using format %s for color key conversion.\n", debug_d3dformat(format
->id
));
1145 wined3d_texture_bind_and_dirtify(texture
, context
, srgb
);
1148 internal
= format
->glGammaInternal
;
1149 else if (texture
->resource
.usage
& WINED3DUSAGE_RENDERTARGET
1150 && wined3d_resource_is_offscreen(&texture
->resource
))
1151 internal
= format
->rtInternal
;
1153 internal
= format
->glInternal
;
1156 FIXME("No GL internal format for format %s.\n", debug_d3dformat(format
->id
));
1158 TRACE("internal %#x, format %#x, type %#x.\n", internal
, format
->glFormat
, format
->glType
);
1160 for (i
= 0; i
< sub_count
; ++i
)
1162 struct wined3d_surface
*surface
= texture
->sub_resources
[i
].u
.surface
;
1163 GLsizei height
= surface
->pow2Height
;
1164 GLsizei width
= surface
->pow2Width
;
1166 if (texture
->resource
.format_flags
& WINED3DFMT_FLAG_HEIGHT_SCALE
)
1168 height
*= format
->height_scale
.numerator
;
1169 height
/= format
->height_scale
.denominator
;
1172 TRACE("surface %p, target %#x, level %d, width %d, height %d.\n",
1173 surface
, surface
->texture_target
, surface
->texture_level
, width
, height
);
1175 gl_info
->gl_ops
.gl
.p_glTexImage2D(surface
->texture_target
, surface
->texture_level
,
1176 internal
, width
, height
, 0, format
->glFormat
, format
->glType
, NULL
);
1177 checkGLcall("glTexImage2D");
1181 static void texture2d_cleanup_sub_resources(struct wined3d_texture
*texture
)
1183 unsigned int sub_count
= texture
->level_count
* texture
->layer_count
;
1184 struct wined3d_surface
*surface
;
1187 for (i
= 0; i
< sub_count
; ++i
)
1189 if ((surface
= texture
->sub_resources
[i
].u
.surface
))
1191 TRACE("surface %p.\n", surface
);
1193 wined3d_surface_cleanup(surface
);
1194 surface
->resource
.parent_ops
->wined3d_object_destroyed(surface
->resource
.parent
);
1197 HeapFree(GetProcessHeap(), 0, texture
->sub_resources
[0].u
.surface
);
1200 static const struct wined3d_texture_ops texture2d_ops
=
1202 texture2d_upload_data
,
1203 texture2d_load_location
,
1204 texture2d_prepare_location
,
1205 texture2d_prepare_texture
,
1206 texture2d_cleanup_sub_resources
,
1209 static ULONG
texture_resource_incref(struct wined3d_resource
*resource
)
1211 return wined3d_texture_incref(wined3d_texture_from_resource(resource
));
1214 static ULONG
texture_resource_decref(struct wined3d_resource
*resource
)
1216 return wined3d_texture_decref(wined3d_texture_from_resource(resource
));
1219 static void wined3d_texture_unload(struct wined3d_resource
*resource
)
1221 struct wined3d_texture
*texture
= wined3d_texture_from_resource(resource
);
1222 UINT sub_count
= texture
->level_count
* texture
->layer_count
;
1223 struct wined3d_context
*context
= NULL
;
1226 TRACE("texture %p.\n", texture
);
1228 for (i
= 0; i
< sub_count
; ++i
)
1230 struct wined3d_resource
*sub_resource
= texture
->sub_resources
[i
].resource
;
1232 sub_resource
->resource_ops
->resource_unload(sub_resource
);
1234 if (texture
->sub_resources
[i
].buffer_object
)
1237 context
= context_acquire(texture
->resource
.device
, NULL
);
1238 wined3d_texture_remove_buffer_object(texture
, i
, context
->gl_info
);
1242 context_release(context
);
1244 wined3d_texture_force_reload(texture
);
1245 wined3d_texture_unload_gl_texture(texture
);
1248 static HRESULT
texture_resource_sub_resource_map(struct wined3d_resource
*resource
, unsigned int sub_resource_idx
,
1249 struct wined3d_map_desc
*map_desc
, const struct wined3d_box
*box
, DWORD flags
)
1251 const struct wined3d_format
*format
= resource
->format
;
1252 struct wined3d_texture_sub_resource
*sub_resource
;
1253 struct wined3d_device
*device
= resource
->device
;
1254 unsigned int fmt_flags
= resource
->format_flags
;
1255 const struct wined3d_gl_info
*gl_info
= NULL
;
1256 struct wined3d_context
*context
= NULL
;
1257 struct wined3d_texture
*texture
;
1258 struct wined3d_bo_address data
;
1259 unsigned int texture_level
;
1263 TRACE("resource %p, sub_resource_idx %u, map_desc %p, box %s, flags %#x.\n",
1264 resource
, sub_resource_idx
, map_desc
, debug_box(box
), flags
);
1266 texture
= wined3d_texture_from_resource(resource
);
1267 if (!(sub_resource
= wined3d_texture_get_sub_resource(texture
, sub_resource_idx
)))
1268 return E_INVALIDARG
;
1270 texture_level
= sub_resource_idx
% texture
->level_count
;
1271 if (box
&& !wined3d_texture_check_box_dimensions(texture
, texture_level
, box
))
1273 WARN("Map box is invalid.\n");
1274 if (resource
->type
!= WINED3D_RTYPE_TEXTURE_2D
)
1275 return WINED3DERR_INVALIDCALL
;
1278 if ((fmt_flags
& WINED3DFMT_FLAG_BLOCKS
) && box
1279 && !wined3d_texture_check_block_align(texture
, texture_level
, box
))
1281 WARN("Map box %s is misaligned for %ux%u blocks.\n",
1282 debug_box(box
), format
->block_width
, format
->block_height
);
1283 if (resource
->type
!= WINED3D_RTYPE_TEXTURE_2D
|| resource
->pool
== WINED3D_POOL_DEFAULT
)
1284 return WINED3DERR_INVALIDCALL
;
1287 if (!(resource
->access_flags
& WINED3D_RESOURCE_ACCESS_CPU
))
1289 WARN("Trying to map unmappable texture.\n");
1290 if (resource
->type
!= WINED3D_RTYPE_TEXTURE_2D
)
1291 return WINED3DERR_INVALIDCALL
;
1294 if (sub_resource
->resource
->map_count
)
1296 WARN("Sub-resource is already mapped.\n");
1297 return WINED3DERR_INVALIDCALL
;
1300 flags
= wined3d_resource_sanitize_map_flags(resource
, flags
);
1302 if (device
->d3d_initialized
)
1304 context
= context_acquire(device
, NULL
);
1305 gl_info
= context
->gl_info
;
1308 if (flags
& WINED3D_MAP_DISCARD
)
1310 TRACE("WINED3D_MAP_DISCARD flag passed, marking %s as up to date.\n",
1311 wined3d_debug_location(sub_resource
->resource
->map_binding
));
1312 if ((ret
= texture
->texture_ops
->texture_prepare_location(texture
,
1313 sub_resource_idx
, context
, sub_resource
->resource
->map_binding
)))
1314 wined3d_texture_validate_location(texture
, sub_resource_idx
, sub_resource
->resource
->map_binding
);
1318 if (resource
->usage
& WINED3DUSAGE_DYNAMIC
)
1319 WARN_(d3d_perf
)("Mapping a dynamic texture without WINED3D_MAP_DISCARD.\n");
1320 ret
= texture
->texture_ops
->texture_load_location(texture
,
1321 sub_resource_idx
, context
, sub_resource
->resource
->map_binding
);
1326 ERR("Failed to prepare location.\n");
1327 context_release(context
);
1328 return E_OUTOFMEMORY
;
1331 if (!(flags
& (WINED3D_MAP_NO_DIRTY_UPDATE
| WINED3D_MAP_READONLY
)))
1332 wined3d_texture_invalidate_location(texture
, sub_resource_idx
, ~sub_resource
->resource
->map_binding
);
1334 wined3d_texture_get_memory(texture
, sub_resource_idx
, &data
, sub_resource
->resource
->map_binding
);
1335 if (!data
.buffer_object
)
1337 base_memory
= data
.addr
;
1341 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER
, data
.buffer_object
));
1343 if (gl_info
->supported
[ARB_MAP_BUFFER_RANGE
])
1345 GLbitfield map_flags
= wined3d_resource_gl_map_flags(flags
);
1346 map_flags
&= ~GL_MAP_FLUSH_EXPLICIT_BIT
;
1347 base_memory
= GL_EXTCALL(glMapBufferRange(GL_PIXEL_UNPACK_BUFFER
,
1348 (INT_PTR
)data
.addr
, sub_resource
->resource
->size
, map_flags
));
1352 GLenum access
= wined3d_resource_gl_legacy_map_flags(flags
);
1353 base_memory
= GL_EXTCALL(glMapBuffer(GL_PIXEL_UNPACK_BUFFER
, access
));
1354 base_memory
+= (INT_PTR
)data
.addr
;
1357 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER
, 0));
1358 checkGLcall("Map PBO");
1362 context_release(context
);
1364 TRACE("Base memory pointer %p.\n", base_memory
);
1366 if (fmt_flags
& WINED3DFMT_FLAG_BROKEN_PITCH
)
1368 map_desc
->row_pitch
= wined3d_texture_get_level_width(texture
, texture_level
) * format
->byte_count
;
1369 map_desc
->slice_pitch
= wined3d_texture_get_level_height(texture
, texture_level
) * map_desc
->row_pitch
;
1373 wined3d_texture_get_pitch(texture
, texture_level
, &map_desc
->row_pitch
, &map_desc
->slice_pitch
);
1378 map_desc
->data
= base_memory
;
1382 if ((fmt_flags
& (WINED3DFMT_FLAG_BLOCKS
| WINED3DFMT_FLAG_BROKEN_PITCH
)) == WINED3DFMT_FLAG_BLOCKS
)
1384 /* Compressed textures are block based, so calculate the offset of
1385 * the block that contains the top-left pixel of the mapped box. */
1386 map_desc
->data
= base_memory
1387 + (box
->front
* map_desc
->slice_pitch
)
1388 + ((box
->top
/ format
->block_height
) * map_desc
->row_pitch
)
1389 + ((box
->left
/ format
->block_width
) * format
->block_byte_count
);
1393 map_desc
->data
= base_memory
1394 + (box
->front
* map_desc
->slice_pitch
)
1395 + (box
->top
* map_desc
->row_pitch
)
1396 + (box
->left
* format
->byte_count
);
1400 if (texture
->swapchain
&& texture
->swapchain
->front_buffer
== texture
)
1402 RECT
*r
= &texture
->swapchain
->front_buffer_update
;
1405 SetRect(r
, 0, 0, resource
->width
, resource
->height
);
1407 SetRect(r
, box
->left
, box
->top
, box
->right
, box
->bottom
);
1408 TRACE("Mapped front buffer %s.\n", wine_dbgstr_rect(r
));
1411 ++resource
->map_count
;
1412 ++sub_resource
->resource
->map_count
;
1414 TRACE("Returning memory %p, row pitch %u, slice pitch %u.\n",
1415 map_desc
->data
, map_desc
->row_pitch
, map_desc
->slice_pitch
);
1420 static HRESULT
texture_resource_sub_resource_unmap(struct wined3d_resource
*resource
, unsigned int sub_resource_idx
)
1422 struct wined3d_texture_sub_resource
*sub_resource
;
1423 const struct wined3d_gl_info
*gl_info
;
1424 struct wined3d_texture
*texture
;
1425 struct wined3d_context
*context
;
1427 TRACE("resource %p, sub_resource_idx %u.\n", resource
, sub_resource_idx
);
1429 texture
= wined3d_texture_from_resource(resource
);
1430 if (!(sub_resource
= wined3d_texture_get_sub_resource(texture
, sub_resource_idx
)))
1431 return E_INVALIDARG
;
1433 if (!sub_resource
->resource
->map_count
)
1435 WARN("Trying to unmap unmapped sub-resource.\n");
1436 return WINEDDERR_NOTLOCKED
;
1439 switch (sub_resource
->resource
->map_binding
)
1441 case WINED3D_LOCATION_SYSMEM
:
1442 case WINED3D_LOCATION_USER_MEMORY
:
1443 case WINED3D_LOCATION_DIB
:
1446 case WINED3D_LOCATION_BUFFER
:
1447 context
= context_acquire(resource
->device
, NULL
);
1448 gl_info
= context
->gl_info
;
1450 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER
,
1451 texture
->sub_resources
[sub_resource_idx
].buffer_object
));
1452 GL_EXTCALL(glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER
));
1453 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER
, 0));
1454 checkGLcall("glUnmapBuffer");
1455 context_release(context
);
1459 ERR("Unexpected map binding %s.\n", wined3d_debug_location(sub_resource
->resource
->map_binding
));
1463 if (texture
->swapchain
&& texture
->swapchain
->front_buffer
== texture
)
1465 struct wined3d_surface
*surface
= texture
->sub_resources
[sub_resource_idx
].u
.surface
;
1467 if (!(surface_get_sub_resource(surface
)->locations
1468 & (WINED3D_LOCATION_DRAWABLE
| WINED3D_LOCATION_TEXTURE_RGB
)))
1469 texture
->swapchain
->swapchain_ops
->swapchain_frontbuffer_updated(texture
->swapchain
);
1471 else if (resource
->format_flags
& (WINED3DFMT_FLAG_DEPTH
| WINED3DFMT_FLAG_STENCIL
))
1473 FIXME("Depth / stencil buffer locking is not implemented.\n");
1476 --sub_resource
->resource
->map_count
;
1477 --resource
->map_count
;
1482 static const struct wined3d_resource_ops texture_resource_ops
=
1484 texture_resource_incref
,
1485 texture_resource_decref
,
1486 wined3d_texture_unload
,
1487 texture_resource_sub_resource_map
,
1488 texture_resource_sub_resource_unmap
,
1491 static HRESULT
texture_init(struct wined3d_texture
*texture
, const struct wined3d_resource_desc
*desc
,
1492 unsigned int layer_count
, unsigned int level_count
, DWORD flags
, struct wined3d_device
*device
,
1493 void *parent
, const struct wined3d_parent_ops
*parent_ops
)
1495 struct wined3d_device_parent
*device_parent
= device
->device_parent
;
1496 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
1497 struct wined3d_resource_desc surface_desc
;
1498 struct wined3d_surface
*surfaces
;
1499 UINT pow2_width
, pow2_height
;
1503 /* TODO: It should only be possible to create textures for formats
1504 * that are reported as supported. */
1505 if (WINED3DFMT_UNKNOWN
>= desc
->format
)
1507 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture
);
1508 return WINED3DERR_INVALIDCALL
;
1511 pow2_width
= desc
->width
;
1512 pow2_height
= desc
->height
;
1513 if (((desc
->width
& (desc
->width
- 1)) || (desc
->height
& (desc
->height
- 1)))
1514 && !gl_info
->supported
[ARB_TEXTURE_NON_POWER_OF_TWO
])
1516 /* level_count == 0 returns an error as well. */
1517 if (level_count
!= 1 || desc
->usage
& WINED3DUSAGE_LEGACY_CUBEMAP
)
1519 if (desc
->pool
!= WINED3D_POOL_SCRATCH
)
1521 WARN("Attempted to create a mipmapped/cube NPOT texture without unconditional NPOT support.\n");
1522 return WINED3DERR_INVALIDCALL
;
1525 WARN("Creating a scratch mipmapped/cube NPOT texture despite lack of HW support.\n");
1527 texture
->flags
|= WINED3D_TEXTURE_COND_NP2
;
1529 if (!gl_info
->supported
[ARB_TEXTURE_RECTANGLE
] && !gl_info
->supported
[WINED3D_GL_NORMALIZED_TEXRECT
])
1531 const struct wined3d_format
*format
= wined3d_get_format(gl_info
, desc
->format
);
1533 /* TODO: Add support for non-power-of-two compressed textures. */
1534 if (format
->flags
[WINED3D_GL_RES_TYPE_TEX_2D
]
1535 & (WINED3DFMT_FLAG_COMPRESSED
| WINED3DFMT_FLAG_HEIGHT_SCALE
))
1537 FIXME("Compressed or height scaled non-power-of-two (%ux%u) textures are not supported.\n",
1538 desc
->width
, desc
->height
);
1539 return WINED3DERR_NOTAVAILABLE
;
1542 /* Find the nearest pow2 match. */
1543 pow2_width
= pow2_height
= 1;
1544 while (pow2_width
< desc
->width
)
1546 while (pow2_height
< desc
->height
)
1548 texture
->flags
|= WINED3D_TEXTURE_COND_NP2_EMULATED
;
1552 if ((pow2_width
> gl_info
->limits
.texture_size
|| pow2_height
> gl_info
->limits
.texture_size
)
1553 && (desc
->usage
& WINED3DUSAGE_TEXTURE
))
1555 /* One of four options:
1556 * 1: Do the same as we do with NPOT and scale the texture. (Any
1557 * texture ops would require the texture to be scaled which is
1558 * potentially slow.)
1559 * 2: Set the texture to the maximum size (bad idea).
1560 * 3: WARN and return WINED3DERR_NOTAVAILABLE.
1561 * 4: Create the surface, but allow it to be used only for DirectDraw
1562 * Blts. Some apps (e.g. Swat 3) create textures with a height of
1563 * 16 and a width > 3000 and blt 16x16 letter areas from them to
1564 * the render target. */
1565 if (desc
->pool
== WINED3D_POOL_DEFAULT
|| desc
->pool
== WINED3D_POOL_MANAGED
)
1567 WARN("Dimensions (%ux%u) exceed the maximum texture size.\n", pow2_width
, pow2_height
);
1568 return WINED3DERR_NOTAVAILABLE
;
1571 /* We should never use this surface in combination with OpenGL. */
1572 TRACE("Creating an oversized (%ux%u) surface.\n", pow2_width
, pow2_height
);
1575 /* Calculate levels for mip mapping. */
1576 if (desc
->usage
& WINED3DUSAGE_AUTOGENMIPMAP
)
1578 if (!gl_info
->supported
[SGIS_GENERATE_MIPMAP
])
1580 WARN("No mipmap generation support, returning WINED3DERR_INVALIDCALL.\n");
1581 return WINED3DERR_INVALIDCALL
;
1584 if (level_count
!= 1)
1586 WARN("WINED3DUSAGE_AUTOGENMIPMAP is set, and level count != 1, returning WINED3DERR_INVALIDCALL.\n");
1587 return WINED3DERR_INVALIDCALL
;
1591 if (FAILED(hr
= wined3d_texture_init(texture
, &texture2d_ops
, layer_count
, level_count
, desc
,
1592 flags
, device
, parent
, parent_ops
, &texture_resource_ops
)))
1594 WARN("Failed to initialize texture, returning %#x.\n", hr
);
1598 /* Precalculated scaling for 'faked' non power of two texture coords. */
1599 if (texture
->resource
.gl_type
== WINED3D_GL_RES_TYPE_TEX_RECT
)
1601 texture
->pow2_matrix
[0] = (float)desc
->width
;
1602 texture
->pow2_matrix
[5] = (float)desc
->height
;
1603 texture
->flags
&= ~(WINED3D_TEXTURE_POW2_MAT_IDENT
| WINED3D_TEXTURE_NORMALIZED_COORDS
);
1604 texture
->target
= GL_TEXTURE_RECTANGLE_ARB
;
1608 if (texture
->flags
& WINED3D_TEXTURE_COND_NP2_EMULATED
)
1610 texture
->pow2_matrix
[0] = (((float)desc
->width
) / ((float)pow2_width
));
1611 texture
->pow2_matrix
[5] = (((float)desc
->height
) / ((float)pow2_height
));
1612 texture
->flags
&= ~WINED3D_TEXTURE_POW2_MAT_IDENT
;
1616 texture
->pow2_matrix
[0] = 1.0f
;
1617 texture
->pow2_matrix
[5] = 1.0f
;
1619 if (desc
->usage
& WINED3DUSAGE_LEGACY_CUBEMAP
)
1620 texture
->target
= GL_TEXTURE_CUBE_MAP_ARB
;
1622 texture
->target
= GL_TEXTURE_2D
;
1624 texture
->pow2_matrix
[10] = 1.0f
;
1625 texture
->pow2_matrix
[15] = 1.0f
;
1626 TRACE("x scale %.8e, y scale %.8e.\n", texture
->pow2_matrix
[0], texture
->pow2_matrix
[5]);
1628 if (!(surfaces
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*surfaces
) * level_count
* layer_count
)))
1630 wined3d_texture_cleanup(texture
);
1631 return E_OUTOFMEMORY
;
1634 /* Generate all the surfaces. */
1635 surface_desc
= *desc
;
1636 surface_desc
.resource_type
= WINED3D_RTYPE_SURFACE
;
1637 for (i
= 0; i
< texture
->level_count
; ++i
)
1639 for (j
= 0; j
< texture
->layer_count
; ++j
)
1641 static const GLenum cube_targets
[6] =
1643 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
,
1644 GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
,
1645 GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
,
1646 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
,
1647 GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
,
1648 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
,
1650 GLenum target
= desc
->usage
& WINED3DUSAGE_LEGACY_CUBEMAP
? cube_targets
[j
] : texture
->target
;
1651 unsigned int idx
= j
* texture
->level_count
+ i
;
1652 struct wined3d_surface
*surface
;
1654 surface
= &surfaces
[idx
];
1655 if (FAILED(hr
= wined3d_surface_init(surface
, texture
, &surface_desc
, target
, i
, j
, flags
)))
1657 WARN("Failed to initialize surface, returning %#x.\n", hr
);
1658 wined3d_texture_cleanup(texture
);
1660 HeapFree(GetProcessHeap(), 0, surfaces
);
1664 if (FAILED(hr
= device_parent
->ops
->surface_created(device_parent
,
1665 texture
, idx
, &parent
, &parent_ops
)))
1667 WARN("Failed to create surface parent, hr %#x.\n", hr
);
1668 wined3d_surface_cleanup(surface
);
1669 wined3d_texture_cleanup(texture
);
1673 TRACE("parent %p, parent_ops %p.\n", parent
, parent_ops
);
1675 surface
->resource
.parent
= parent
;
1676 surface
->resource
.parent_ops
= parent_ops
;
1677 texture
->sub_resources
[idx
].resource
= &surface
->resource
;
1678 texture
->sub_resources
[idx
].u
.surface
= surface
;
1679 TRACE("Created surface level %u, layer %u @ %p.\n", i
, j
, surface
);
1681 /* Calculate the next mipmap level. */
1682 surface_desc
.width
= max(1, surface_desc
.width
>> 1);
1683 surface_desc
.height
= max(1, surface_desc
.height
>> 1);
1689 static void texture3d_upload_data(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
,
1690 const struct wined3d_context
*context
, const struct wined3d_sub_resource_data
*data
)
1692 struct wined3d_const_bo_address addr
;
1693 unsigned int row_pitch
, slice_pitch
;
1695 wined3d_texture_get_pitch(texture
, sub_resource_idx
, &row_pitch
, &slice_pitch
);
1696 if (row_pitch
!= data
->row_pitch
|| slice_pitch
!= data
->slice_pitch
)
1697 FIXME("Ignoring row/slice pitch (%u/%u).\n", data
->row_pitch
, data
->slice_pitch
);
1699 addr
.buffer_object
= 0;
1700 addr
.addr
= data
->data
;
1702 wined3d_volume_upload_data(texture
->sub_resources
[sub_resource_idx
].u
.volume
, context
, &addr
);
1705 static BOOL
texture3d_load_location(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
,
1706 struct wined3d_context
*context
, DWORD location
)
1708 return wined3d_volume_load_location(texture
->sub_resources
[sub_resource_idx
].u
.volume
, context
, location
);
1711 static BOOL
texture3d_prepare_location(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
,
1712 struct wined3d_context
*context
, DWORD location
)
1714 return wined3d_volume_prepare_location(texture
->sub_resources
[sub_resource_idx
].u
.volume
, context
, location
);
1717 static void texture3d_prepare_texture(struct wined3d_texture
*texture
, struct wined3d_context
*context
, BOOL srgb
)
1719 unsigned int sub_count
= texture
->level_count
* texture
->layer_count
;
1720 const struct wined3d_format
*format
= texture
->resource
.format
;
1721 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1724 wined3d_texture_bind_and_dirtify(texture
, context
, srgb
);
1726 for (i
= 0; i
< sub_count
; ++i
)
1728 struct wined3d_volume
*volume
= texture
->sub_resources
[i
].u
.volume
;
1730 GL_EXTCALL(glTexImage3D(GL_TEXTURE_3D
, volume
->texture_level
,
1731 srgb
? format
->glGammaInternal
: format
->glInternal
,
1732 volume
->resource
.width
, volume
->resource
.height
, volume
->resource
.depth
,
1733 0, format
->glFormat
, format
->glType
, NULL
));
1734 checkGLcall("glTexImage3D");
1738 static void texture3d_cleanup_sub_resources(struct wined3d_texture
*texture
)
1740 unsigned int sub_count
= texture
->level_count
* texture
->layer_count
;
1741 struct wined3d_volume
*volume
;
1744 for (i
= 0; i
< sub_count
; ++i
)
1746 if ((volume
= texture
->sub_resources
[i
].u
.volume
))
1748 TRACE("volume %p.\n", volume
);
1750 wined3d_volume_cleanup(volume
);
1751 volume
->resource
.parent_ops
->wined3d_object_destroyed(volume
->resource
.parent
);
1754 HeapFree(GetProcessHeap(), 0, texture
->sub_resources
[0].u
.volume
);
1757 static const struct wined3d_texture_ops texture3d_ops
=
1759 texture3d_upload_data
,
1760 texture3d_load_location
,
1761 texture3d_prepare_location
,
1762 texture3d_prepare_texture
,
1763 texture3d_cleanup_sub_resources
,
1766 BOOL
wined3d_texture_check_block_align(const struct wined3d_texture
*texture
,
1767 unsigned int level
, const struct wined3d_box
*box
)
1769 const struct wined3d_format
*format
= texture
->resource
.format
;
1770 unsigned int height
= wined3d_texture_get_level_height(texture
, level
);
1771 unsigned int width
= wined3d_texture_get_level_width(texture
, level
);
1772 unsigned int width_mask
, height_mask
;
1774 if ((box
->left
>= box
->right
)
1775 || (box
->top
>= box
->bottom
)
1776 || (box
->right
> width
)
1777 || (box
->bottom
> height
))
1780 /* This assumes power of two block sizes, but NPOT block sizes would be
1783 * This also assumes that the format's block depth is 1. */
1784 width_mask
= format
->block_width
- 1;
1785 height_mask
= format
->block_height
- 1;
1787 if ((box
->left
& width_mask
) || (box
->top
& height_mask
)
1788 || (box
->right
& width_mask
&& box
->right
!= width
)
1789 || (box
->bottom
& height_mask
&& box
->bottom
!= height
))
1795 static HRESULT
volumetexture_init(struct wined3d_texture
*texture
, const struct wined3d_resource_desc
*desc
,
1796 UINT levels
, struct wined3d_device
*device
, void *parent
, const struct wined3d_parent_ops
*parent_ops
)
1798 struct wined3d_device_parent
*device_parent
= device
->device_parent
;
1799 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
1800 struct wined3d_resource_desc volume_desc
;
1801 struct wined3d_volume
*volumes
;
1805 /* TODO: It should only be possible to create textures for formats
1806 * that are reported as supported. */
1807 if (WINED3DFMT_UNKNOWN
>= desc
->format
)
1809 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture
);
1810 return WINED3DERR_INVALIDCALL
;
1813 if (!gl_info
->supported
[EXT_TEXTURE3D
])
1815 WARN("(%p) : Texture cannot be created - no volume texture support.\n", texture
);
1816 return WINED3DERR_INVALIDCALL
;
1819 /* Calculate levels for mip mapping. */
1820 if (desc
->usage
& WINED3DUSAGE_AUTOGENMIPMAP
)
1822 if (!gl_info
->supported
[SGIS_GENERATE_MIPMAP
])
1824 WARN("No mipmap generation support, returning D3DERR_INVALIDCALL.\n");
1825 return WINED3DERR_INVALIDCALL
;
1830 WARN("WINED3DUSAGE_AUTOGENMIPMAP is set, and level count != 1, returning D3DERR_INVALIDCALL.\n");
1831 return WINED3DERR_INVALIDCALL
;
1835 if (!gl_info
->supported
[ARB_TEXTURE_NON_POWER_OF_TWO
])
1837 UINT pow2_w
, pow2_h
, pow2_d
;
1839 while (pow2_w
< desc
->width
)
1842 while (pow2_h
< desc
->height
)
1845 while (pow2_d
< desc
->depth
)
1848 if (pow2_w
!= desc
->width
|| pow2_h
!= desc
->height
|| pow2_d
!= desc
->depth
)
1850 if (desc
->pool
== WINED3D_POOL_SCRATCH
)
1852 WARN("Creating a scratch NPOT volume texture despite lack of HW support.\n");
1856 WARN("Attempted to create a NPOT volume texture (%u, %u, %u) without GL support.\n",
1857 desc
->width
, desc
->height
, desc
->depth
);
1858 return WINED3DERR_INVALIDCALL
;
1863 if (FAILED(hr
= wined3d_texture_init(texture
, &texture3d_ops
, 1, levels
, desc
,
1864 0, device
, parent
, parent_ops
, &texture_resource_ops
)))
1866 WARN("Failed to initialize texture, returning %#x.\n", hr
);
1870 texture
->pow2_matrix
[0] = 1.0f
;
1871 texture
->pow2_matrix
[5] = 1.0f
;
1872 texture
->pow2_matrix
[10] = 1.0f
;
1873 texture
->pow2_matrix
[15] = 1.0f
;
1874 texture
->target
= GL_TEXTURE_3D
;
1876 if (!(volumes
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*volumes
) * levels
)))
1878 wined3d_texture_cleanup(texture
);
1879 return E_OUTOFMEMORY
;
1882 /* Generate all the surfaces. */
1883 volume_desc
= *desc
;
1884 volume_desc
.resource_type
= WINED3D_RTYPE_VOLUME
;
1885 for (i
= 0; i
< texture
->level_count
; ++i
)
1887 struct wined3d_volume
*volume
;
1889 volume
= &volumes
[i
];
1890 if (FAILED(hr
= wined3d_volume_init(volume
, texture
, &volume_desc
, i
)))
1892 WARN("Failed to initialize volume, returning %#x.\n", hr
);
1893 wined3d_texture_cleanup(texture
);
1895 HeapFree(GetProcessHeap(), 0, volumes
);
1899 if (FAILED(hr
= device_parent
->ops
->volume_created(device_parent
,
1900 texture
, i
, &parent
, &parent_ops
)))
1902 WARN("Failed to create volume parent, hr %#x.\n", hr
);
1903 wined3d_volume_cleanup(volume
);
1904 wined3d_texture_cleanup(texture
);
1908 TRACE("parent %p, parent_ops %p.\n", parent
, parent_ops
);
1910 volume
->resource
.parent
= parent
;
1911 volume
->resource
.parent_ops
= parent_ops
;
1912 texture
->sub_resources
[i
].resource
= &volume
->resource
;
1913 texture
->sub_resources
[i
].u
.volume
= volume
;
1914 TRACE("Created volume level %u @ %p.\n", i
, volume
);
1916 /* Calculate the next mipmap level. */
1917 volume_desc
.width
= max(1, volume_desc
.width
>> 1);
1918 volume_desc
.height
= max(1, volume_desc
.height
>> 1);
1919 volume_desc
.depth
= max(1, volume_desc
.depth
>> 1);
1925 HRESULT CDECL
wined3d_texture_blt(struct wined3d_texture
*dst_texture
, unsigned int dst_sub_resource_idx
,
1926 const RECT
*dst_rect
, struct wined3d_texture
*src_texture
, unsigned int src_sub_resource_idx
,
1927 const RECT
*src_rect
, DWORD flags
, const struct wined3d_blt_fx
*fx
, enum wined3d_texture_filter_type filter
)
1929 struct wined3d_texture_sub_resource
*dst_resource
, *src_resource
= NULL
;
1931 TRACE("dst_texture %p, dst_sub_resource_idx %u, dst_rect %s, src_texture %p, "
1932 "src_sub_resource_idx %u, src_rect %s, flags %#x, fx %p, filter %s.\n",
1933 dst_texture
, dst_sub_resource_idx
, wine_dbgstr_rect(dst_rect
), src_texture
,
1934 src_sub_resource_idx
, wine_dbgstr_rect(src_rect
), flags
, fx
, debug_d3dtexturefiltertype(filter
));
1936 if (!(dst_resource
= wined3d_texture_get_sub_resource(dst_texture
, dst_sub_resource_idx
))
1937 || dst_texture
->resource
.type
!= WINED3D_RTYPE_TEXTURE_2D
)
1938 return WINED3DERR_INVALIDCALL
;
1942 if (!(src_resource
= wined3d_texture_get_sub_resource(src_texture
, src_sub_resource_idx
))
1943 || src_texture
->resource
.type
!= WINED3D_RTYPE_TEXTURE_2D
)
1944 return WINED3DERR_INVALIDCALL
;
1947 return wined3d_surface_blt(dst_resource
->u
.surface
, dst_rect
,
1948 src_resource
? src_resource
->u
.surface
: NULL
, src_rect
, flags
, fx
, filter
);
1951 HRESULT CDECL
wined3d_texture_get_overlay_position(const struct wined3d_texture
*texture
,
1952 unsigned int sub_resource_idx
, LONG
*x
, LONG
*y
)
1954 struct wined3d_surface
*surface
;
1956 TRACE("texture %p, sub_resource_idx %u, x %p, y %p.\n", texture
, sub_resource_idx
, x
, y
);
1958 if (!(texture
->resource
.usage
& WINED3DUSAGE_OVERLAY
) || texture
->resource
.type
!= WINED3D_RTYPE_TEXTURE_2D
1959 || sub_resource_idx
>= texture
->level_count
* texture
->layer_count
)
1961 WARN("Invalid sub-resource specified.\n");
1962 return WINEDDERR_NOTAOVERLAYSURFACE
;
1965 surface
= texture
->sub_resources
[sub_resource_idx
].u
.surface
;
1966 if (!surface
->overlay_dest
)
1968 TRACE("Overlay not visible.\n");
1971 return WINEDDERR_OVERLAYNOTVISIBLE
;
1974 *x
= surface
->overlay_destrect
.left
;
1975 *y
= surface
->overlay_destrect
.top
;
1977 TRACE("Returning position %d, %d.\n", *x
, *y
);
1982 HRESULT CDECL
wined3d_texture_set_overlay_position(struct wined3d_texture
*texture
,
1983 unsigned int sub_resource_idx
, LONG x
, LONG y
)
1985 struct wined3d_texture_sub_resource
*sub_resource
;
1986 struct wined3d_surface
*surface
;
1989 TRACE("texture %p, sub_resource_idx %u, x %d, y %d.\n", texture
, sub_resource_idx
, x
, y
);
1991 if (!(texture
->resource
.usage
& WINED3DUSAGE_OVERLAY
) || texture
->resource
.type
!= WINED3D_RTYPE_TEXTURE_2D
1992 || !(sub_resource
= wined3d_texture_get_sub_resource(texture
, sub_resource_idx
)))
1994 WARN("Invalid sub-resource specified.\n");
1995 return WINEDDERR_NOTAOVERLAYSURFACE
;
1998 surface
= sub_resource
->u
.surface
;
1999 w
= surface
->overlay_destrect
.right
- surface
->overlay_destrect
.left
;
2000 h
= surface
->overlay_destrect
.bottom
- surface
->overlay_destrect
.top
;
2001 surface
->overlay_destrect
.left
= x
;
2002 surface
->overlay_destrect
.top
= y
;
2003 surface
->overlay_destrect
.right
= x
+ w
;
2004 surface
->overlay_destrect
.bottom
= y
+ h
;
2009 HRESULT CDECL
wined3d_texture_update_overlay(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
,
2010 const RECT
*src_rect
, struct wined3d_texture
*dst_texture
, unsigned int dst_sub_resource_idx
,
2011 const RECT
*dst_rect
, DWORD flags
)
2013 struct wined3d_texture_sub_resource
*sub_resource
, *dst_sub_resource
;
2014 struct wined3d_surface
*surface
, *dst_surface
;
2016 TRACE("texture %p, sub_resource_idx %u, src_rect %s, dst_texture %p, "
2017 "dst_sub_resource_idx %u, dst_rect %s, flags %#x.\n",
2018 texture
, sub_resource_idx
, wine_dbgstr_rect(src_rect
), dst_texture
,
2019 dst_sub_resource_idx
, wine_dbgstr_rect(dst_rect
), flags
);
2021 if (!(texture
->resource
.usage
& WINED3DUSAGE_OVERLAY
) || texture
->resource
.type
!= WINED3D_RTYPE_TEXTURE_2D
2022 || !(sub_resource
= wined3d_texture_get_sub_resource(texture
, sub_resource_idx
)))
2024 WARN("Invalid sub-resource specified.\n");
2025 return WINEDDERR_NOTAOVERLAYSURFACE
;
2028 if (!dst_texture
|| dst_texture
->resource
.type
!= WINED3D_RTYPE_TEXTURE_2D
2029 || !(dst_sub_resource
= wined3d_texture_get_sub_resource(dst_texture
, dst_sub_resource_idx
)))
2031 WARN("Invalid destination sub-resource specified.\n");
2032 return WINED3DERR_INVALIDCALL
;
2035 surface
= sub_resource
->u
.surface
;
2037 surface
->overlay_srcrect
= *src_rect
;
2039 SetRect(&surface
->overlay_srcrect
, 0, 0, surface
->resource
.width
, surface
->resource
.height
);
2041 dst_surface
= dst_sub_resource
->u
.surface
;
2043 surface
->overlay_destrect
= *dst_rect
;
2045 SetRect(&surface
->overlay_destrect
, 0, 0, dst_surface
->resource
.width
, dst_surface
->resource
.height
);
2047 if (surface
->overlay_dest
&& (surface
->overlay_dest
!= dst_surface
|| flags
& WINEDDOVER_HIDE
))
2049 surface
->overlay_dest
= NULL
;
2050 list_remove(&surface
->overlay_entry
);
2053 if (flags
& WINEDDOVER_SHOW
)
2055 if (surface
->overlay_dest
!= dst_surface
)
2057 surface
->overlay_dest
= dst_surface
;
2058 list_add_tail(&dst_surface
->overlays
, &surface
->overlay_entry
);
2061 else if (flags
& WINEDDOVER_HIDE
)
2063 /* Tests show that the rectangles are erased on hide. */
2064 SetRectEmpty(&surface
->overlay_srcrect
);
2065 SetRectEmpty(&surface
->overlay_destrect
);
2066 surface
->overlay_dest
= NULL
;
2072 void * CDECL
wined3d_texture_get_sub_resource_parent(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
)
2074 unsigned int sub_count
= texture
->level_count
* texture
->layer_count
;
2076 TRACE("texture %p, sub_resource_idx %u.\n", texture
, sub_resource_idx
);
2078 if (sub_resource_idx
>= sub_count
)
2080 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx
, sub_count
);
2084 return texture
->sub_resources
[sub_resource_idx
].resource
->parent
;
2087 void CDECL
wined3d_texture_set_sub_resource_parent(struct wined3d_texture
*texture
,
2088 unsigned int sub_resource_idx
, void *parent
)
2090 unsigned int sub_count
= texture
->level_count
* texture
->layer_count
;
2092 TRACE("texture %p, sub_resource_idx %u, parent %p.\n", texture
, sub_resource_idx
, parent
);
2094 if (sub_resource_idx
>= sub_count
)
2096 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx
, sub_count
);
2100 texture
->sub_resources
[sub_resource_idx
].resource
->parent
= parent
;
2103 HRESULT CDECL
wined3d_texture_get_sub_resource_desc(const struct wined3d_texture
*texture
,
2104 unsigned int sub_resource_idx
, struct wined3d_sub_resource_desc
*desc
)
2106 unsigned int sub_count
= texture
->level_count
* texture
->layer_count
;
2107 const struct wined3d_resource
*resource
;
2108 unsigned int level_idx
;
2110 TRACE("texture %p, sub_resource_idx %u, desc %p.\n", texture
, sub_resource_idx
, desc
);
2112 if (sub_resource_idx
>= sub_count
)
2114 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx
, sub_count
);
2115 return WINED3DERR_INVALIDCALL
;
2118 resource
= &texture
->resource
;
2119 desc
->format
= resource
->format
->id
;
2120 desc
->multisample_type
= resource
->multisample_type
;
2121 desc
->multisample_quality
= resource
->multisample_quality
;
2122 desc
->usage
= resource
->usage
;
2123 desc
->pool
= resource
->pool
;
2125 level_idx
= sub_resource_idx
% texture
->level_count
;
2126 desc
->width
= wined3d_texture_get_level_width(texture
, level_idx
);
2127 desc
->height
= wined3d_texture_get_level_height(texture
, level_idx
);
2128 desc
->depth
= wined3d_texture_get_level_depth(texture
, level_idx
);
2129 desc
->size
= texture
->sub_resources
[sub_resource_idx
].resource
->size
;
2134 HRESULT CDECL
wined3d_texture_create(struct wined3d_device
*device
, const struct wined3d_resource_desc
*desc
,
2135 UINT level_count
, DWORD flags
, const struct wined3d_sub_resource_data
*data
, void *parent
,
2136 const struct wined3d_parent_ops
*parent_ops
, struct wined3d_texture
**texture
)
2138 unsigned int layer_count
= desc
->usage
& WINED3DUSAGE_LEGACY_CUBEMAP
? 6 : 1;
2139 struct wined3d_texture
*object
;
2142 TRACE("device %p, desc %p, level_count %u, flags %#x, data %p, parent %p, parent_ops %p, texture %p.\n",
2143 device
, desc
, level_count
, flags
, data
, parent
, parent_ops
, texture
);
2147 WARN("Invalid level count.\n");
2148 return WINED3DERR_INVALIDCALL
;
2151 if (desc
->multisample_type
!= WINED3D_MULTISAMPLE_NONE
)
2153 const struct wined3d_format
*format
= wined3d_get_format(&device
->adapter
->gl_info
, desc
->format
);
2155 if (desc
->multisample_type
== WINED3D_MULTISAMPLE_NON_MASKABLE
2156 && desc
->multisample_quality
>= wined3d_popcount(format
->multisample_types
))
2158 WARN("Unsupported quality level %u requested for WINED3D_MULTISAMPLE_NON_MASKABLE.\n",
2159 desc
->multisample_quality
);
2160 return WINED3DERR_NOTAVAILABLE
;
2162 if (desc
->multisample_type
!= WINED3D_MULTISAMPLE_NON_MASKABLE
2163 && (!(format
->multisample_types
& 1u << (desc
->multisample_type
- 1))
2164 || desc
->multisample_quality
))
2166 WARN("Unsupported multisample type %u quality %u requested.\n", desc
->multisample_type
,
2167 desc
->multisample_quality
);
2168 return WINED3DERR_NOTAVAILABLE
;
2172 if (!(object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
2173 FIELD_OFFSET(struct wined3d_texture
, sub_resources
[level_count
* layer_count
]))))
2174 return E_OUTOFMEMORY
;
2176 switch (desc
->resource_type
)
2178 case WINED3D_RTYPE_TEXTURE_2D
:
2179 hr
= texture_init(object
, desc
, layer_count
, level_count
, flags
, device
, parent
, parent_ops
);
2182 case WINED3D_RTYPE_TEXTURE_3D
:
2183 hr
= volumetexture_init(object
, desc
, level_count
, device
, parent
, parent_ops
);
2187 ERR("Invalid resource type %s.\n", debug_d3dresourcetype(desc
->resource_type
));
2188 hr
= WINED3DERR_INVALIDCALL
;
2194 WARN("Failed to initialize texture, returning %#x.\n", hr
);
2195 HeapFree(GetProcessHeap(), 0, object
);
2199 /* FIXME: We'd like to avoid ever allocating system memory for the texture
2201 if (data
&& FAILED(hr
= wined3d_texture_upload_data(object
, data
)))
2203 wined3d_texture_cleanup(object
);
2204 HeapFree(GetProcessHeap(), 0, object
);
2208 TRACE("Created texture %p.\n", object
);
2214 HRESULT CDECL
wined3d_texture_get_dc(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
, HDC
*dc
)
2216 struct wined3d_device
*device
= texture
->resource
.device
;
2217 struct wined3d_texture_sub_resource
*sub_resource
;
2218 struct wined3d_context
*context
= NULL
;
2219 struct wined3d_surface
*surface
;
2222 TRACE("texture %p, sub_resource_idx %u, dc %p.\n", texture
, sub_resource_idx
, dc
);
2224 if (!(sub_resource
= wined3d_texture_get_sub_resource(texture
, sub_resource_idx
)))
2225 return WINED3DERR_INVALIDCALL
;
2227 if (texture
->resource
.type
!= WINED3D_RTYPE_TEXTURE_2D
)
2229 WARN("Not supported on %s resources.\n", debug_d3dresourcetype(texture
->resource
.type
));
2230 return WINED3DERR_INVALIDCALL
;
2233 surface
= sub_resource
->u
.surface
;
2235 /* Give more detailed info for ddraw. */
2236 if (surface
->flags
& SFLAG_DCINUSE
)
2237 return WINEDDERR_DCALREADYCREATED
;
2239 /* Can't GetDC if the surface is locked. */
2240 if (surface
->resource
.map_count
)
2241 return WINED3DERR_INVALIDCALL
;
2243 if (device
->d3d_initialized
)
2244 context
= context_acquire(device
, NULL
);
2246 /* Create a DIB section if there isn't a dc yet. */
2249 if (FAILED(hr
= surface_create_dib_section(surface
)))
2252 context_release(context
);
2253 return WINED3DERR_INVALIDCALL
;
2255 if (!(surface
->resource
.map_binding
== WINED3D_LOCATION_USER_MEMORY
2256 || texture
->flags
& WINED3D_TEXTURE_PIN_SYSMEM
2257 || texture
->sub_resources
[sub_resource_idx
].buffer_object
))
2258 surface
->resource
.map_binding
= WINED3D_LOCATION_DIB
;
2261 surface_load_location(surface
, context
, WINED3D_LOCATION_DIB
);
2262 wined3d_texture_invalidate_location(texture
, sub_resource_idx
, ~WINED3D_LOCATION_DIB
);
2265 context_release(context
);
2267 surface
->flags
|= SFLAG_DCINUSE
;
2268 ++texture
->resource
.map_count
;
2269 surface
->resource
.map_count
++;
2272 TRACE("Returning dc %p.\n", *dc
);
2277 HRESULT CDECL
wined3d_texture_release_dc(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
, HDC dc
)
2279 struct wined3d_device
*device
= texture
->resource
.device
;
2280 struct wined3d_texture_sub_resource
*sub_resource
;
2281 struct wined3d_context
*context
= NULL
;
2282 struct wined3d_surface
*surface
;
2284 TRACE("texture %p, sub_resource_idx %u, dc %p.\n", texture
, sub_resource_idx
, dc
);
2286 if (!(sub_resource
= wined3d_texture_get_sub_resource(texture
, sub_resource_idx
)))
2287 return WINED3DERR_INVALIDCALL
;
2289 if (texture
->resource
.type
!= WINED3D_RTYPE_TEXTURE_2D
)
2291 WARN("Not supported on %s resources.\n", debug_d3dresourcetype(texture
->resource
.type
));
2292 return WINED3DERR_INVALIDCALL
;
2295 surface
= sub_resource
->u
.surface
;
2297 if (!(surface
->flags
& SFLAG_DCINUSE
))
2298 return WINEDDERR_NODC
;
2300 if (surface
->hDC
!= dc
)
2302 WARN("Application tries to release invalid DC %p, surface DC is %p.\n",
2304 return WINEDDERR_NODC
;
2307 surface
->resource
.map_count
--;
2308 --texture
->resource
.map_count
;
2309 surface
->flags
&= ~SFLAG_DCINUSE
;
2311 if (surface
->resource
.map_binding
== WINED3D_LOCATION_USER_MEMORY
2312 || (surface
->container
->flags
& WINED3D_TEXTURE_PIN_SYSMEM
2313 && surface
->resource
.map_binding
!= WINED3D_LOCATION_DIB
))
2315 /* The game Salammbo modifies the surface contents without mapping the surface between
2316 * a GetDC/ReleaseDC operation and flipping the surface. If the DIB remains the active
2317 * copy and is copied to the screen, this update, which draws the mouse pointer, is lost.
2318 * Do not only copy the DIB to the map location, but also make sure the map location is
2319 * copied back to the DIB in the next getdc call.
2321 * The same consideration applies to user memory surfaces. */
2323 if (device
->d3d_initialized
)
2324 context
= context_acquire(device
, NULL
);
2326 surface_load_location(surface
, context
, surface
->resource
.map_binding
);
2327 wined3d_texture_invalidate_location(texture
, sub_resource_idx
, WINED3D_LOCATION_DIB
);
2329 context_release(context
);