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 #define WINED3D_TEXTURE_DYNAMIC_MAP_THRESHOLD 50
33 static BOOL
wined3d_texture_use_pbo(const struct wined3d_texture
*texture
, const struct wined3d_gl_info
*gl_info
)
35 return texture
->resource
.pool
== WINED3D_POOL_DEFAULT
36 && texture
->resource
.access_flags
& WINED3D_RESOURCE_ACCESS_CPU
37 && gl_info
->supported
[ARB_PIXEL_BUFFER_OBJECT
]
38 && !texture
->resource
.format
->convert
39 && !(texture
->flags
& (WINED3D_TEXTURE_PIN_SYSMEM
| WINED3D_TEXTURE_COND_NP2_EMULATED
));
42 GLenum
wined3d_texture_get_gl_buffer(const struct wined3d_texture
*texture
)
44 const struct wined3d_swapchain
*swapchain
= texture
->swapchain
;
46 TRACE("texture %p.\n", texture
);
50 ERR("Texture %p is not part of a swapchain.\n", texture
);
54 if (swapchain
->back_buffers
&& swapchain
->back_buffers
[0] == texture
)
56 if (swapchain
->render_to_fbo
)
58 TRACE("Returning GL_COLOR_ATTACHMENT0.\n");
59 return GL_COLOR_ATTACHMENT0
;
61 TRACE("Returning GL_BACK.\n");
64 else if (texture
== swapchain
->front_buffer
)
66 TRACE("Returning GL_FRONT.\n");
70 FIXME("Higher back buffer, returning GL_BACK.\n");
74 static void wined3d_texture_evict_sysmem(struct wined3d_texture
*texture
)
76 struct wined3d_texture_sub_resource
*sub_resource
;
77 unsigned int i
, sub_count
;
79 if (texture
->flags
& (WINED3D_TEXTURE_CONVERTED
| WINED3D_TEXTURE_PIN_SYSMEM
)
80 || texture
->download_count
> WINED3D_TEXTURE_DYNAMIC_MAP_THRESHOLD
)
82 TRACE("Not evicting system memory for texture %p.\n", texture
);
86 TRACE("Evicting system memory for texture %p.\n", texture
);
88 sub_count
= texture
->level_count
* texture
->layer_count
;
89 for (i
= 0; i
< sub_count
; ++i
)
91 sub_resource
= &texture
->sub_resources
[i
];
92 if (sub_resource
->locations
== WINED3D_LOCATION_SYSMEM
)
93 ERR("WINED3D_LOCATION_SYSMEM is the only location for sub-resource %u of texture %p.\n",
95 sub_resource
->locations
&= ~WINED3D_LOCATION_SYSMEM
;
96 wined3d_resource_free_sysmem(sub_resource
->resource
);
100 void wined3d_texture_validate_location(struct wined3d_texture
*texture
,
101 unsigned int sub_resource_idx
, DWORD location
)
103 struct wined3d_texture_sub_resource
*sub_resource
;
104 DWORD previous_locations
;
106 TRACE("texture %p, sub_resource_idx %u, location %s.\n",
107 texture
, sub_resource_idx
, wined3d_debug_location(location
));
109 sub_resource
= &texture
->sub_resources
[sub_resource_idx
];
110 previous_locations
= sub_resource
->locations
;
111 sub_resource
->locations
|= location
;
112 if (previous_locations
== WINED3D_LOCATION_SYSMEM
&& location
!= WINED3D_LOCATION_SYSMEM
113 && !--texture
->sysmem_count
)
114 wined3d_texture_evict_sysmem(texture
);
116 TRACE("New locations flags are %s.\n", wined3d_debug_location(sub_resource
->locations
));
119 void wined3d_texture_invalidate_location(struct wined3d_texture
*texture
,
120 unsigned int sub_resource_idx
, DWORD location
)
122 struct wined3d_texture_sub_resource
*sub_resource
;
124 TRACE("texture %p, sub_resource_idx %u, location %s.\n",
125 texture
, sub_resource_idx
, wined3d_debug_location(location
));
127 if (location
& (WINED3D_LOCATION_TEXTURE_RGB
| WINED3D_LOCATION_TEXTURE_SRGB
))
128 wined3d_texture_set_dirty(texture
);
130 sub_resource
= &texture
->sub_resources
[sub_resource_idx
];
131 sub_resource
->locations
&= ~location
;
132 if (sub_resource
->locations
== WINED3D_LOCATION_SYSMEM
)
133 ++texture
->sysmem_count
;
135 TRACE("New locations flags are %s.\n", wined3d_debug_location(sub_resource
->locations
));
137 if (!sub_resource
->locations
)
138 ERR("Sub-resource %u of texture %p does not have any up to date location.\n",
139 sub_resource_idx
, texture
);
142 /* Context activation is done by the caller. */
143 void *wined3d_texture_map_bo_address(const struct wined3d_bo_address
*data
, size_t size
,
144 const struct wined3d_gl_info
*gl_info
, GLenum binding
, DWORD flags
)
148 if (!data
->buffer_object
)
151 GL_EXTCALL(glBindBuffer(binding
, data
->buffer_object
));
153 if (gl_info
->supported
[ARB_MAP_BUFFER_RANGE
])
155 GLbitfield map_flags
= wined3d_resource_gl_map_flags(flags
) & ~GL_MAP_FLUSH_EXPLICIT_BIT
;
156 memory
= GL_EXTCALL(glMapBufferRange(binding
, (INT_PTR
)data
->addr
, size
, map_flags
));
160 memory
= GL_EXTCALL(glMapBuffer(binding
, wined3d_resource_gl_legacy_map_flags(flags
)));
161 memory
+= (INT_PTR
)data
->addr
;
164 GL_EXTCALL(glBindBuffer(binding
, 0));
165 checkGLcall("Map buffer object");
170 /* Context activation is done by the caller. */
171 void wined3d_texture_unmap_bo_address(const struct wined3d_bo_address
*data
,
172 const struct wined3d_gl_info
*gl_info
, GLenum binding
)
174 if (!data
->buffer_object
)
177 GL_EXTCALL(glBindBuffer(binding
, data
->buffer_object
));
178 GL_EXTCALL(glUnmapBuffer(binding
));
179 GL_EXTCALL(glBindBuffer(binding
, 0));
180 checkGLcall("Unmap buffer object");
183 void wined3d_texture_get_memory(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
,
184 struct wined3d_bo_address
*data
, DWORD locations
)
186 struct wined3d_texture_sub_resource
*sub_resource
;
188 TRACE("texture %p, sub_resource_idx %u, data %p, locations %s.\n",
189 texture
, sub_resource_idx
, data
, wined3d_debug_location(locations
));
191 sub_resource
= &texture
->sub_resources
[sub_resource_idx
];
192 if (locations
& WINED3D_LOCATION_BUFFER
)
195 data
->buffer_object
= sub_resource
->buffer_object
;
198 if (locations
& WINED3D_LOCATION_USER_MEMORY
)
200 data
->addr
= texture
->user_memory
;
201 data
->buffer_object
= 0;
204 if (locations
& WINED3D_LOCATION_SYSMEM
)
206 data
->addr
= sub_resource
->resource
->heap_memory
;
207 data
->buffer_object
= 0;
211 ERR("Unexpected locations %s.\n", wined3d_debug_location(locations
));
213 data
->buffer_object
= 0;
216 static HRESULT
wined3d_texture_init(struct wined3d_texture
*texture
, const struct wined3d_texture_ops
*texture_ops
,
217 UINT layer_count
, UINT level_count
, const struct wined3d_resource_desc
*desc
, DWORD flags
,
218 struct wined3d_device
*device
, void *parent
, const struct wined3d_parent_ops
*parent_ops
,
219 const struct wined3d_resource_ops
*resource_ops
)
221 const struct wined3d_format
*format
= wined3d_get_format(&device
->adapter
->gl_info
, desc
->format
);
224 TRACE("texture %p, texture_ops %p, layer_count %u, level_count %u, resource_type %s, format %s, "
225 "multisample_type %#x, multisample_quality %#x, usage %s, pool %s, width %u, height %u, depth %u, "
226 "flags %#x, device %p, parent %p, parent_ops %p, resource_ops %p.\n",
227 texture
, texture_ops
, layer_count
, level_count
, debug_d3dresourcetype(desc
->resource_type
),
228 debug_d3dformat(desc
->format
), desc
->multisample_type
, desc
->multisample_quality
,
229 debug_d3dusage(desc
->usage
), debug_d3dpool(desc
->pool
), desc
->width
, desc
->height
, desc
->depth
,
230 flags
, device
, parent
, parent_ops
, resource_ops
);
232 if (FAILED(hr
= resource_init(&texture
->resource
, device
, desc
->resource_type
, format
,
233 desc
->multisample_type
, desc
->multisample_quality
, desc
->usage
, desc
->pool
,
234 desc
->width
, desc
->height
, desc
->depth
, 0, parent
, parent_ops
, resource_ops
)))
236 static unsigned int once
;
238 /* DXTn 3D textures are not supported. Do not write the ERR for them. */
239 if ((desc
->format
== WINED3DFMT_DXT1
|| desc
->format
== WINED3DFMT_DXT2
|| desc
->format
== WINED3DFMT_DXT3
240 || desc
->format
== WINED3DFMT_DXT4
|| desc
->format
== WINED3DFMT_DXT5
)
241 && !(format
->flags
[WINED3D_GL_RES_TYPE_TEX_2D
] & WINED3DFMT_FLAG_TEXTURE
)
242 && desc
->resource_type
!= WINED3D_RTYPE_TEXTURE_3D
&& !once
++)
243 ERR_(winediag
)("The application tried to create a DXTn texture, but the driver does not support them.\n");
245 WARN("Failed to initialize resource, returning %#x\n", hr
);
248 wined3d_resource_update_draw_binding(&texture
->resource
);
249 if ((flags
& WINED3D_TEXTURE_CREATE_MAPPABLE
) || desc
->format
== WINED3DFMT_D16_LOCKABLE
)
250 texture
->resource
.access_flags
|= WINED3D_RESOURCE_ACCESS_CPU
;
252 texture
->texture_ops
= texture_ops
;
254 texture
->layer_count
= layer_count
;
255 texture
->level_count
= level_count
;
256 texture
->filter_type
= (desc
->usage
& WINED3DUSAGE_AUTOGENMIPMAP
) ? WINED3D_TEXF_LINEAR
: WINED3D_TEXF_NONE
;
258 texture
->flags
|= WINED3D_TEXTURE_POW2_MAT_IDENT
| WINED3D_TEXTURE_NORMALIZED_COORDS
;
259 if (flags
& WINED3D_TEXTURE_CREATE_GET_DC_LENIENT
)
260 texture
->flags
|= WINED3D_TEXTURE_PIN_SYSMEM
| WINED3D_TEXTURE_GET_DC_LENIENT
;
261 if (flags
& WINED3D_TEXTURE_CREATE_DISCARD
)
262 texture
->flags
|= WINED3D_TEXTURE_DISCARD
;
267 /* Context activation is done by the caller. */
268 static void wined3d_texture_remove_buffer_object(struct wined3d_texture
*texture
,
269 unsigned int sub_resource_idx
, const struct wined3d_gl_info
*gl_info
)
271 GLuint
*buffer_object
;
273 buffer_object
= &texture
->sub_resources
[sub_resource_idx
].buffer_object
;
274 GL_EXTCALL(glDeleteBuffers(1, buffer_object
));
275 checkGLcall("glDeleteBuffers");
276 wined3d_texture_invalidate_location(texture
, sub_resource_idx
, WINED3D_LOCATION_BUFFER
);
279 TRACE("Deleted buffer object %u for texture %p, sub-resource %u.\n",
280 *buffer_object
, texture
, sub_resource_idx
);
283 static void wined3d_texture_update_map_binding(struct wined3d_texture
*texture
)
285 unsigned int sub_count
= texture
->level_count
* texture
->layer_count
;
286 const struct wined3d_device
*device
= texture
->resource
.device
;
287 DWORD map_binding
= texture
->update_map_binding
;
288 struct wined3d_context
*context
= NULL
;
291 if (device
->d3d_initialized
)
292 context
= context_acquire(device
, NULL
);
294 for (i
= 0; i
< sub_count
; ++i
)
296 if (texture
->sub_resources
[i
].locations
== texture
->resource
.map_binding
297 && !texture
->texture_ops
->texture_load_location(texture
, i
, context
, map_binding
))
298 ERR("Failed to load location %s.\n", wined3d_debug_location(map_binding
));
299 if (texture
->resource
.map_binding
== WINED3D_LOCATION_BUFFER
)
300 wined3d_texture_remove_buffer_object(texture
, i
, context
->gl_info
);
304 context_release(context
);
306 texture
->resource
.map_binding
= map_binding
;
307 texture
->update_map_binding
= 0;
310 void wined3d_texture_set_map_binding(struct wined3d_texture
*texture
, DWORD map_binding
)
312 texture
->update_map_binding
= map_binding
;
313 if (!texture
->resource
.map_count
)
314 wined3d_texture_update_map_binding(texture
);
317 /* A GL context is provided by the caller */
318 static void gltexture_delete(struct wined3d_device
*device
, const struct wined3d_gl_info
*gl_info
,
319 struct gl_texture
*tex
)
321 context_gl_resource_released(device
, tex
->name
, FALSE
);
322 gl_info
->gl_ops
.gl
.p_glDeleteTextures(1, &tex
->name
);
326 static void wined3d_texture_unload_gl_texture(struct wined3d_texture
*texture
)
328 struct wined3d_device
*device
= texture
->resource
.device
;
329 const struct wined3d_gl_info
*gl_info
= NULL
;
330 struct wined3d_context
*context
= NULL
;
332 if (texture
->texture_rgb
.name
|| texture
->texture_srgb
.name
333 || texture
->rb_multisample
|| texture
->rb_resolved
)
335 context
= context_acquire(device
, NULL
);
336 gl_info
= context
->gl_info
;
339 if (texture
->texture_rgb
.name
)
340 gltexture_delete(device
, context
->gl_info
, &texture
->texture_rgb
);
342 if (texture
->texture_srgb
.name
)
343 gltexture_delete(device
, context
->gl_info
, &texture
->texture_srgb
);
345 if (texture
->rb_multisample
)
347 TRACE("Deleting multisample renderbuffer %u.\n", texture
->rb_multisample
);
348 context_gl_resource_released(device
, texture
->rb_multisample
, TRUE
);
349 gl_info
->fbo_ops
.glDeleteRenderbuffers(1, &texture
->rb_multisample
);
352 if (texture
->rb_resolved
)
354 TRACE("Deleting resolved renderbuffer %u.\n", texture
->rb_resolved
);
355 context_gl_resource_released(device
, texture
->rb_resolved
, TRUE
);
356 gl_info
->fbo_ops
.glDeleteRenderbuffers(1, &texture
->rb_resolved
);
359 if (context
) context_release(context
);
361 wined3d_texture_set_dirty(texture
);
363 resource_unload(&texture
->resource
);
366 static void wined3d_texture_cleanup(struct wined3d_texture
*texture
)
368 unsigned int sub_count
= texture
->level_count
* texture
->layer_count
;
369 struct wined3d_device
*device
= texture
->resource
.device
;
370 struct wined3d_context
*context
= NULL
;
371 const struct wined3d_gl_info
*gl_info
;
372 GLuint buffer_object
;
375 TRACE("texture %p.\n", texture
);
377 for (i
= 0; i
< sub_count
; ++i
)
379 if (!(buffer_object
= texture
->sub_resources
[i
].buffer_object
))
382 TRACE("Deleting buffer object %u.\n", buffer_object
);
384 /* We may not be able to get a context in wined3d_texture_cleanup() in
385 * general, but if a buffer object was previously created we can. */
388 context
= context_acquire(device
, NULL
);
389 gl_info
= context
->gl_info
;
392 GL_EXTCALL(glDeleteBuffers(1, &buffer_object
));
395 context_release(context
);
397 texture
->texture_ops
->texture_cleanup_sub_resources(texture
);
398 wined3d_texture_unload_gl_texture(texture
);
399 resource_cleanup(&texture
->resource
);
402 void wined3d_texture_set_swapchain(struct wined3d_texture
*texture
, struct wined3d_swapchain
*swapchain
)
404 texture
->swapchain
= swapchain
;
405 wined3d_resource_update_draw_binding(&texture
->resource
);
408 void wined3d_texture_set_dirty(struct wined3d_texture
*texture
)
410 texture
->flags
&= ~(WINED3D_TEXTURE_RGB_VALID
| WINED3D_TEXTURE_SRGB_VALID
);
413 /* Context activation is done by the caller. */
414 void wined3d_texture_bind(struct wined3d_texture
*texture
,
415 struct wined3d_context
*context
, BOOL srgb
)
417 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
418 const struct wined3d_format
*format
= texture
->resource
.format
;
419 const struct color_fixup_desc fixup
= format
->color_fixup
;
420 struct gl_texture
*gl_tex
;
423 TRACE("texture %p, context %p, srgb %#x.\n", texture
, context
, srgb
);
425 if (!needs_separate_srgb_gl_texture(context
))
428 /* sRGB mode cache for preload() calls outside drawprim. */
430 texture
->flags
|= WINED3D_TEXTURE_IS_SRGB
;
432 texture
->flags
&= ~WINED3D_TEXTURE_IS_SRGB
;
434 gl_tex
= wined3d_texture_get_gl_texture(texture
, srgb
);
435 target
= texture
->target
;
439 context_bind_texture(context
, target
, gl_tex
->name
);
443 gl_info
->gl_ops
.gl
.p_glGenTextures(1, &gl_tex
->name
);
444 checkGLcall("glGenTextures");
445 TRACE("Generated texture %d.\n", gl_tex
->name
);
449 ERR("Failed to generate a texture name.\n");
453 /* Initialise the state of the texture object to the OpenGL defaults, not
454 * the wined3d defaults. */
455 gl_tex
->sampler_desc
.address_u
= WINED3D_TADDRESS_WRAP
;
456 gl_tex
->sampler_desc
.address_v
= WINED3D_TADDRESS_WRAP
;
457 gl_tex
->sampler_desc
.address_w
= WINED3D_TADDRESS_WRAP
;
458 memset(gl_tex
->sampler_desc
.border_color
, 0, sizeof(gl_tex
->sampler_desc
.border_color
));
459 gl_tex
->sampler_desc
.mag_filter
= WINED3D_TEXF_LINEAR
;
460 gl_tex
->sampler_desc
.min_filter
= WINED3D_TEXF_POINT
; /* GL_NEAREST_MIPMAP_LINEAR */
461 gl_tex
->sampler_desc
.mip_filter
= WINED3D_TEXF_LINEAR
; /* GL_NEAREST_MIPMAP_LINEAR */
462 gl_tex
->sampler_desc
.lod_bias
= 0.0f
;
463 gl_tex
->sampler_desc
.min_lod
= -1000.0f
;
464 gl_tex
->sampler_desc
.max_lod
= 1000.0f
;
465 gl_tex
->sampler_desc
.max_anisotropy
= 1;
466 gl_tex
->sampler_desc
.compare
= FALSE
;
467 gl_tex
->sampler_desc
.comparison_func
= WINED3D_CMP_LESSEQUAL
;
468 if (context
->gl_info
->supported
[EXT_TEXTURE_SRGB_DECODE
])
469 gl_tex
->sampler_desc
.srgb_decode
= TRUE
;
471 gl_tex
->sampler_desc
.srgb_decode
= srgb
;
472 gl_tex
->base_level
= 0;
473 wined3d_texture_set_dirty(texture
);
475 context_bind_texture(context
, target
, gl_tex
->name
);
477 if (texture
->resource
.usage
& WINED3DUSAGE_AUTOGENMIPMAP
)
479 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_GENERATE_MIPMAP_SGIS
, GL_TRUE
);
480 checkGLcall("glTexParameteri(target, GL_GENERATE_MIPMAP_SGIS, GL_TRUE)");
483 /* For a new texture we have to set the texture levels after binding the
484 * texture. Beware that texture rectangles do not support mipmapping, but
485 * set the maxmiplevel if we're relying on the partial
486 * GL_ARB_texture_non_power_of_two emulation with texture rectangles.
487 * (I.e., do not care about cond_np2 here, just look for
488 * GL_TEXTURE_RECTANGLE_ARB.) */
489 if (target
!= GL_TEXTURE_RECTANGLE_ARB
)
491 TRACE("Setting GL_TEXTURE_MAX_LEVEL to %u.\n", texture
->level_count
- 1);
492 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, texture
->level_count
- 1);
493 checkGLcall("glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, texture->level_count)");
496 if (target
== GL_TEXTURE_CUBE_MAP_ARB
)
498 /* Cubemaps are always set to clamp, regardless of the sampler state. */
499 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
500 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
501 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_R
, GL_CLAMP_TO_EDGE
);
504 if (texture
->flags
& WINED3D_TEXTURE_COND_NP2
)
506 /* Conditinal non power of two textures use a different clamping
507 * default. If we're using the GL_WINE_normalized_texrect partial
508 * driver emulation, we're dealing with a GL_TEXTURE_2D texture which
509 * has the address mode set to repeat - something that prevents us
510 * from hitting the accelerated codepath. Thus manually set the GL
511 * state. The same applies to filtering. Even if the texture has only
512 * one mip level, the default LINEAR_MIPMAP_LINEAR filter causes a SW
513 * fallback on macos. */
514 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
515 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
516 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
517 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
518 checkGLcall("glTexParameteri");
519 gl_tex
->sampler_desc
.address_u
= WINED3D_TADDRESS_CLAMP
;
520 gl_tex
->sampler_desc
.address_v
= WINED3D_TADDRESS_CLAMP
;
521 gl_tex
->sampler_desc
.mag_filter
= WINED3D_TEXF_POINT
;
522 gl_tex
->sampler_desc
.min_filter
= WINED3D_TEXF_POINT
;
523 gl_tex
->sampler_desc
.mip_filter
= WINED3D_TEXF_NONE
;
526 if (gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
] && gl_info
->supported
[ARB_DEPTH_TEXTURE
])
528 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_DEPTH_TEXTURE_MODE_ARB
, GL_INTENSITY
);
529 checkGLcall("glTexParameteri(GL_DEPTH_TEXTURE_MODE_ARB, GL_INTENSITY)");
532 if (!is_identity_fixup(fixup
) && can_use_texture_swizzle(gl_info
, format
))
534 static const GLenum swizzle_source
[] =
536 GL_ZERO
, /* CHANNEL_SOURCE_ZERO */
537 GL_ONE
, /* CHANNEL_SOURCE_ONE */
538 GL_RED
, /* CHANNEL_SOURCE_X */
539 GL_GREEN
, /* CHANNEL_SOURCE_Y */
540 GL_BLUE
, /* CHANNEL_SOURCE_Z */
541 GL_ALPHA
, /* CHANNEL_SOURCE_W */
549 swizzle
.x
= swizzle_source
[fixup
.x_source
];
550 swizzle
.y
= swizzle_source
[fixup
.y_source
];
551 swizzle
.z
= swizzle_source
[fixup
.z_source
];
552 swizzle
.w
= swizzle_source
[fixup
.w_source
];
553 gl_info
->gl_ops
.gl
.p_glTexParameteriv(target
, GL_TEXTURE_SWIZZLE_RGBA
, &swizzle
.x
);
554 checkGLcall("glTexParameteriv(GL_TEXTURE_SWIZZLE_RGBA)");
558 /* Context activation is done by the caller. */
559 void wined3d_texture_bind_and_dirtify(struct wined3d_texture
*texture
,
560 struct wined3d_context
*context
, BOOL srgb
)
562 DWORD active_sampler
;
564 /* We don't need a specific texture unit, but after binding the texture
565 * the current unit is dirty. Read the unit back instead of switching to
566 * 0, this avoids messing around with the state manager's GL states. The
567 * current texture unit should always be a valid one.
569 * To be more specific, this is tricky because we can implicitly be
570 * called from sampler() in state.c. This means we can't touch anything
571 * other than whatever happens to be the currently active texture, or we
572 * would risk marking already applied sampler states dirty again. */
573 active_sampler
= context
->rev_tex_unit_map
[context
->active_texture
];
574 if (active_sampler
!= WINED3D_UNMAPPED_STAGE
)
575 context_invalidate_state(context
, STATE_SAMPLER(active_sampler
));
576 /* FIXME: Ideally we'd only do this when touching a binding that's used by
578 context_invalidate_state(context
, STATE_SHADER_RESOURCE_BINDING
);
580 wined3d_texture_bind(texture
, context
, srgb
);
583 /* Context activation is done by the caller (state handler). */
584 /* This function relies on the correct texture being bound and loaded. */
585 void wined3d_texture_apply_sampler_desc(struct wined3d_texture
*texture
,
586 const struct wined3d_sampler_desc
*sampler_desc
, const struct wined3d_context
*context
)
588 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
589 GLenum target
= texture
->target
;
590 struct gl_texture
*gl_tex
;
593 TRACE("texture %p, sampler_desc %p, context %p.\n", texture
, sampler_desc
, context
);
595 gl_tex
= wined3d_texture_get_gl_texture(texture
, texture
->flags
& WINED3D_TEXTURE_IS_SRGB
);
597 state
= sampler_desc
->address_u
;
598 if (state
!= gl_tex
->sampler_desc
.address_u
)
600 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_S
,
601 gl_info
->wrap_lookup
[state
- WINED3D_TADDRESS_WRAP
]);
602 gl_tex
->sampler_desc
.address_u
= state
;
605 state
= sampler_desc
->address_v
;
606 if (state
!= gl_tex
->sampler_desc
.address_v
)
608 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_T
,
609 gl_info
->wrap_lookup
[state
- WINED3D_TADDRESS_WRAP
]);
610 gl_tex
->sampler_desc
.address_v
= state
;
613 state
= sampler_desc
->address_w
;
614 if (state
!= gl_tex
->sampler_desc
.address_w
)
616 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_R
,
617 gl_info
->wrap_lookup
[state
- WINED3D_TADDRESS_WRAP
]);
618 gl_tex
->sampler_desc
.address_w
= state
;
621 if (memcmp(gl_tex
->sampler_desc
.border_color
, sampler_desc
->border_color
,
622 sizeof(gl_tex
->sampler_desc
.border_color
)))
624 gl_info
->gl_ops
.gl
.p_glTexParameterfv(target
, GL_TEXTURE_BORDER_COLOR
, &sampler_desc
->border_color
[0]);
625 memcpy(gl_tex
->sampler_desc
.border_color
, sampler_desc
->border_color
,
626 sizeof(gl_tex
->sampler_desc
.border_color
));
629 state
= sampler_desc
->mag_filter
;
630 if (state
!= gl_tex
->sampler_desc
.mag_filter
)
632 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MAG_FILTER
, wined3d_gl_mag_filter(state
));
633 gl_tex
->sampler_desc
.mag_filter
= state
;
636 if (sampler_desc
->min_filter
!= gl_tex
->sampler_desc
.min_filter
637 || sampler_desc
->mip_filter
!= gl_tex
->sampler_desc
.mip_filter
)
639 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MIN_FILTER
,
640 wined3d_gl_min_mip_filter(sampler_desc
->min_filter
, sampler_desc
->mip_filter
));
641 gl_tex
->sampler_desc
.min_filter
= sampler_desc
->min_filter
;
642 gl_tex
->sampler_desc
.mip_filter
= sampler_desc
->mip_filter
;
645 state
= sampler_desc
->max_anisotropy
;
646 if (state
!= gl_tex
->sampler_desc
.max_anisotropy
)
648 if (gl_info
->supported
[EXT_TEXTURE_FILTER_ANISOTROPIC
])
649 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MAX_ANISOTROPY_EXT
, state
);
651 WARN("Anisotropic filtering not supported.\n");
652 gl_tex
->sampler_desc
.max_anisotropy
= state
;
655 if (!sampler_desc
->srgb_decode
!= !gl_tex
->sampler_desc
.srgb_decode
656 && (context
->d3d_info
->wined3d_creation_flags
& WINED3D_SRGB_READ_WRITE_CONTROL
)
657 && gl_info
->supported
[EXT_TEXTURE_SRGB_DECODE
])
659 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_SRGB_DECODE_EXT
,
660 sampler_desc
->srgb_decode
? GL_DECODE_EXT
: GL_SKIP_DECODE_EXT
);
661 gl_tex
->sampler_desc
.srgb_decode
= sampler_desc
->srgb_decode
;
664 if (!sampler_desc
->compare
!= !gl_tex
->sampler_desc
.compare
)
666 if (sampler_desc
->compare
)
667 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_COMPARE_MODE_ARB
, GL_COMPARE_R_TO_TEXTURE_ARB
);
669 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_COMPARE_MODE_ARB
, GL_NONE
);
670 gl_tex
->sampler_desc
.compare
= sampler_desc
->compare
;
673 checkGLcall("Texture parameter application");
675 if (gl_info
->supported
[EXT_TEXTURE_LOD_BIAS
])
677 gl_info
->gl_ops
.gl
.p_glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT
,
678 GL_TEXTURE_LOD_BIAS_EXT
, sampler_desc
->lod_bias
);
679 checkGLcall("glTexEnvf(GL_TEXTURE_LOD_BIAS_EXT, ...)");
683 ULONG CDECL
wined3d_texture_incref(struct wined3d_texture
*texture
)
687 TRACE("texture %p, swapchain %p.\n", texture
, texture
->swapchain
);
689 if (texture
->swapchain
)
690 return wined3d_swapchain_incref(texture
->swapchain
);
692 refcount
= InterlockedIncrement(&texture
->resource
.ref
);
693 TRACE("%p increasing refcount to %u.\n", texture
, refcount
);
698 ULONG CDECL
wined3d_texture_decref(struct wined3d_texture
*texture
)
702 TRACE("texture %p, swapchain %p.\n", texture
, texture
->swapchain
);
704 if (texture
->swapchain
)
705 return wined3d_swapchain_decref(texture
->swapchain
);
707 refcount
= InterlockedDecrement(&texture
->resource
.ref
);
708 TRACE("%p decreasing refcount to %u.\n", texture
, refcount
);
712 wined3d_texture_cleanup(texture
);
713 texture
->resource
.parent_ops
->wined3d_object_destroyed(texture
->resource
.parent
);
714 HeapFree(GetProcessHeap(), 0, texture
);
720 struct wined3d_resource
* CDECL
wined3d_texture_get_resource(struct wined3d_texture
*texture
)
722 TRACE("texture %p.\n", texture
);
724 return &texture
->resource
;
727 static BOOL
color_key_equal(const struct wined3d_color_key
*c1
, struct wined3d_color_key
*c2
)
729 return c1
->color_space_low_value
== c2
->color_space_low_value
730 && c1
->color_space_high_value
== c2
->color_space_high_value
;
733 /* Context activation is done by the caller */
734 void wined3d_texture_load(struct wined3d_texture
*texture
,
735 struct wined3d_context
*context
, BOOL srgb
)
737 UINT sub_count
= texture
->level_count
* texture
->layer_count
;
738 const struct wined3d_d3d_info
*d3d_info
= context
->d3d_info
;
742 TRACE("texture %p, context %p, srgb %#x.\n", texture
, context
, srgb
);
744 if (!needs_separate_srgb_gl_texture(context
))
748 flag
= WINED3D_TEXTURE_SRGB_VALID
;
750 flag
= WINED3D_TEXTURE_RGB_VALID
;
752 if (!d3d_info
->shader_color_key
753 && (!(texture
->async
.flags
& WINED3D_TEXTURE_ASYNC_COLOR_KEY
)
754 != !(texture
->async
.color_key_flags
& WINED3D_CKEY_SRC_BLT
)
755 || (texture
->async
.flags
& WINED3D_TEXTURE_ASYNC_COLOR_KEY
756 && !color_key_equal(&texture
->async
.gl_color_key
, &texture
->async
.src_blt_color_key
))))
758 unsigned int sub_count
= texture
->level_count
* texture
->layer_count
;
761 TRACE("Reloading because of color key value change.\n");
762 for (i
= 0; i
< sub_count
; i
++)
764 if (!texture
->texture_ops
->texture_load_location(texture
, i
, context
, texture
->resource
.map_binding
))
765 ERR("Failed to load location %s.\n", wined3d_debug_location(texture
->resource
.map_binding
));
767 wined3d_texture_invalidate_location(texture
, i
, ~texture
->resource
.map_binding
);
770 texture
->async
.gl_color_key
= texture
->async
.src_blt_color_key
;
773 if (texture
->flags
& flag
)
775 TRACE("Texture %p not dirty, nothing to do.\n", texture
);
779 /* Reload the surfaces if the texture is marked dirty. */
780 for (i
= 0; i
< sub_count
; ++i
)
782 if (!texture
->texture_ops
->texture_load_location(texture
, i
, context
,
783 srgb
? WINED3D_LOCATION_TEXTURE_SRGB
: WINED3D_LOCATION_TEXTURE_RGB
))
784 ERR("Failed to load location (srgb %#x).\n", srgb
);
786 texture
->flags
|= flag
;
789 void CDECL
wined3d_texture_preload(struct wined3d_texture
*texture
)
791 struct wined3d_context
*context
;
792 context
= context_acquire(texture
->resource
.device
, NULL
);
793 wined3d_texture_load(texture
, context
, texture
->flags
& WINED3D_TEXTURE_IS_SRGB
);
794 context_release(context
);
797 void * CDECL
wined3d_texture_get_parent(const struct wined3d_texture
*texture
)
799 TRACE("texture %p.\n", texture
);
801 return texture
->resource
.parent
;
804 static BOOL
wined3d_texture_check_box_dimensions(const struct wined3d_texture
*texture
,
805 unsigned int level
, const struct wined3d_box
*box
)
807 if (box
->left
>= box
->right
808 || box
->top
>= box
->bottom
809 || box
->front
>= box
->back
)
812 if (box
->right
> wined3d_texture_get_level_width(texture
, level
)
813 || box
->bottom
> wined3d_texture_get_level_height(texture
, level
)
814 || box
->back
> wined3d_texture_get_level_depth(texture
, level
))
820 void CDECL
wined3d_texture_get_pitch(const struct wined3d_texture
*texture
,
821 unsigned int level
, unsigned int *row_pitch
, unsigned int *slice_pitch
)
823 const struct wined3d_resource
*resource
= &texture
->resource
;
824 unsigned int width
= wined3d_texture_get_level_width(texture
, level
);
825 unsigned int height
= wined3d_texture_get_level_height(texture
, level
);
827 if (texture
->row_pitch
)
829 *row_pitch
= texture
->row_pitch
;
830 *slice_pitch
= texture
->slice_pitch
;
834 wined3d_format_calculate_pitch(resource
->format
, resource
->device
->surface_alignment
,
835 width
, height
, row_pitch
, slice_pitch
);
838 DWORD CDECL
wined3d_texture_set_lod(struct wined3d_texture
*texture
, DWORD lod
)
840 DWORD old
= texture
->lod
;
842 TRACE("texture %p, lod %u.\n", texture
, lod
);
844 /* The d3d9:texture test shows that SetLOD is ignored on non-managed
845 * textures. The call always returns 0, and GetLOD always returns 0. */
846 if (texture
->resource
.pool
!= WINED3D_POOL_MANAGED
)
848 TRACE("Ignoring SetLOD on %s texture, returning 0.\n", debug_d3dpool(texture
->resource
.pool
));
852 if (lod
>= texture
->level_count
)
853 lod
= texture
->level_count
- 1;
855 if (texture
->lod
!= lod
)
859 texture
->texture_rgb
.base_level
= ~0u;
860 texture
->texture_srgb
.base_level
= ~0u;
861 if (texture
->resource
.bind_count
)
862 device_invalidate_state(texture
->resource
.device
, STATE_SAMPLER(texture
->sampler
));
868 DWORD CDECL
wined3d_texture_get_lod(const struct wined3d_texture
*texture
)
870 TRACE("texture %p, returning %u.\n", texture
, texture
->lod
);
875 DWORD CDECL
wined3d_texture_get_level_count(const struct wined3d_texture
*texture
)
877 TRACE("texture %p, returning %u.\n", texture
, texture
->level_count
);
879 return texture
->level_count
;
882 HRESULT CDECL
wined3d_texture_set_autogen_filter_type(struct wined3d_texture
*texture
,
883 enum wined3d_texture_filter_type filter_type
)
885 FIXME("texture %p, filter_type %s stub!\n", texture
, debug_d3dtexturefiltertype(filter_type
));
887 if (!(texture
->resource
.usage
& WINED3DUSAGE_AUTOGENMIPMAP
))
889 WARN("Texture doesn't have AUTOGENMIPMAP usage.\n");
890 return WINED3DERR_INVALIDCALL
;
893 texture
->filter_type
= filter_type
;
898 enum wined3d_texture_filter_type CDECL
wined3d_texture_get_autogen_filter_type(const struct wined3d_texture
*texture
)
900 TRACE("texture %p.\n", texture
);
902 return texture
->filter_type
;
905 HRESULT CDECL
wined3d_texture_set_color_key(struct wined3d_texture
*texture
,
906 DWORD flags
, const struct wined3d_color_key
*color_key
)
908 struct wined3d_device
*device
= texture
->resource
.device
;
909 static const DWORD all_flags
= WINED3D_CKEY_DST_BLT
| WINED3D_CKEY_DST_OVERLAY
910 | WINED3D_CKEY_SRC_BLT
| WINED3D_CKEY_SRC_OVERLAY
;
912 TRACE("texture %p, flags %#x, color_key %p.\n", texture
, flags
, color_key
);
914 if (flags
& ~all_flags
)
916 WARN("Invalid flags passed, returning WINED3DERR_INVALIDCALL.\n");
917 return WINED3DERR_INVALIDCALL
;
920 wined3d_cs_emit_set_color_key(device
->cs
, texture
, flags
, color_key
);
925 HRESULT CDECL
wined3d_texture_update_desc(struct wined3d_texture
*texture
, UINT width
, UINT height
,
926 enum wined3d_format_id format_id
, enum wined3d_multisample_type multisample_type
,
927 UINT multisample_quality
, void *mem
, UINT pitch
)
929 struct wined3d_device
*device
= texture
->resource
.device
;
930 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
931 const struct wined3d_format
*format
= wined3d_get_format(gl_info
, format_id
);
932 UINT resource_size
= wined3d_format_calculate_size(format
, device
->surface_alignment
, width
, height
, 1);
933 struct wined3d_texture_sub_resource
*sub_resource
;
934 struct wined3d_surface
*surface
;
935 DWORD valid_location
= 0;
936 BOOL create_dib
= FALSE
;
938 TRACE("texture %p, width %u, height %u, format %s, multisample_type %#x, multisample_quality %u, "
939 "mem %p, pitch %u.\n",
940 texture
, width
, height
, debug_d3dformat(format_id
), multisample_type
, multisample_quality
, mem
, pitch
);
943 return WINED3DERR_INVALIDCALL
;
945 if (texture
->level_count
* texture
->layer_count
> 1)
947 WARN("Texture has multiple sub-resources, not supported.\n");
948 return WINED3DERR_INVALIDCALL
;
951 if (texture
->resource
.type
== WINED3D_RTYPE_TEXTURE_3D
)
953 WARN("Not supported on 3D textures.\n");
954 return WINED3DERR_INVALIDCALL
;
957 if (texture
->resource
.map_count
)
959 WARN("Texture is mapped.\n");
960 return WINED3DERR_INVALIDCALL
;
963 /* We have no way of supporting a pitch that is not a multiple of the pixel
964 * byte width short of uploading the texture row-by-row.
965 * Fortunately that's not an issue since D3D9Ex doesn't allow a custom pitch
966 * for user-memory textures (it always expects packed data) while DirectDraw
967 * requires a 4-byte aligned pitch and doesn't support texture formats
968 * larger than 4 bytes per pixel nor any format using 3 bytes per pixel.
969 * This check is here to verify that the assumption holds. */
970 if (pitch
% texture
->resource
.format
->byte_count
)
972 WARN("Pitch unsupported, not a multiple of the texture format byte width.\n");
973 return WINED3DERR_INVALIDCALL
;
976 if (device
->d3d_initialized
)
977 texture
->resource
.resource_ops
->resource_unload(&texture
->resource
);
979 sub_resource
= &texture
->sub_resources
[0];
980 surface
= sub_resource
->u
.surface
;
983 wined3d_surface_destroy_dc(surface
);
987 wined3d_resource_free_sysmem(sub_resource
->resource
);
989 if ((texture
->row_pitch
= pitch
))
990 texture
->slice_pitch
= height
* pitch
;
992 /* User memory surfaces don't have the regular surface alignment. */
993 wined3d_format_calculate_pitch(format
, 1, width
, height
,
994 &texture
->row_pitch
, &texture
->slice_pitch
);
996 texture
->resource
.format
= format
;
997 texture
->resource
.multisample_type
= multisample_type
;
998 texture
->resource
.multisample_quality
= multisample_quality
;
999 texture
->resource
.width
= width
;
1000 texture
->resource
.height
= height
;
1002 sub_resource
->resource
->format
= format
;
1003 sub_resource
->resource
->multisample_type
= multisample_type
;
1004 sub_resource
->resource
->multisample_quality
= multisample_quality
;
1005 sub_resource
->resource
->width
= width
;
1006 sub_resource
->resource
->height
= height
;
1007 sub_resource
->resource
->size
= texture
->slice_pitch
;
1008 sub_resource
->locations
= WINED3D_LOCATION_DISCARDED
;
1010 if (((width
& (width
- 1)) || (height
& (height
- 1))) && !gl_info
->supported
[ARB_TEXTURE_NON_POWER_OF_TWO
]
1011 && !gl_info
->supported
[ARB_TEXTURE_RECTANGLE
] && !gl_info
->supported
[WINED3D_GL_NORMALIZED_TEXRECT
])
1013 texture
->flags
|= WINED3D_TEXTURE_COND_NP2_EMULATED
;
1014 texture
->pow2_width
= texture
->pow2_height
= 1;
1015 while (texture
->pow2_width
< width
)
1016 texture
->pow2_width
<<= 1;
1017 while (texture
->pow2_height
< height
)
1018 texture
->pow2_height
<<= 1;
1022 texture
->flags
&= ~WINED3D_TEXTURE_COND_NP2_EMULATED
;
1023 texture
->pow2_width
= width
;
1024 texture
->pow2_height
= height
;
1027 if ((texture
->user_memory
= mem
))
1029 texture
->resource
.map_binding
= WINED3D_LOCATION_USER_MEMORY
;
1030 valid_location
= WINED3D_LOCATION_USER_MEMORY
;
1034 wined3d_surface_prepare(surface
, NULL
, WINED3D_LOCATION_SYSMEM
);
1035 valid_location
= WINED3D_LOCATION_SYSMEM
;
1038 /* The format might be changed to a format that needs conversion.
1039 * If the surface didn't use PBOs previously but could now, don't
1040 * change it - whatever made us not use PBOs might come back, e.g.
1042 if (texture
->resource
.map_binding
== WINED3D_LOCATION_BUFFER
&& !wined3d_texture_use_pbo(texture
, gl_info
))
1043 texture
->resource
.map_binding
= WINED3D_LOCATION_SYSMEM
;
1045 wined3d_texture_validate_location(texture
, 0, valid_location
);
1046 wined3d_texture_invalidate_location(texture
, 0, ~valid_location
);
1049 wined3d_surface_create_dc(surface
);
1054 /* Context activation is done by the caller. */
1055 static void wined3d_texture_prepare_buffer_object(struct wined3d_texture
*texture
,
1056 unsigned int sub_resource_idx
, const struct wined3d_gl_info
*gl_info
)
1058 GLuint
*buffer_object
;
1060 buffer_object
= &texture
->sub_resources
[sub_resource_idx
].buffer_object
;
1064 GL_EXTCALL(glGenBuffers(1, buffer_object
));
1065 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER
, *buffer_object
));
1066 GL_EXTCALL(glBufferData(GL_PIXEL_UNPACK_BUFFER
,
1067 texture
->sub_resources
[sub_resource_idx
].resource
->size
, NULL
, GL_STREAM_DRAW
));
1068 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER
, 0));
1069 checkGLcall("Create buffer object");
1071 TRACE("Created buffer object %u for texture %p, sub-resource %u.\n",
1072 *buffer_object
, texture
, sub_resource_idx
);
1075 static void wined3d_texture_force_reload(struct wined3d_texture
*texture
)
1077 unsigned int sub_count
= texture
->level_count
* texture
->layer_count
;
1080 texture
->flags
&= ~(WINED3D_TEXTURE_RGB_ALLOCATED
| WINED3D_TEXTURE_SRGB_ALLOCATED
1081 | WINED3D_TEXTURE_CONVERTED
);
1082 texture
->async
.flags
&= ~WINED3D_TEXTURE_ASYNC_COLOR_KEY
;
1083 for (i
= 0; i
< sub_count
; ++i
)
1085 wined3d_texture_invalidate_location(texture
, i
,
1086 WINED3D_LOCATION_TEXTURE_RGB
| WINED3D_LOCATION_TEXTURE_SRGB
);
1090 void wined3d_texture_prepare_texture(struct wined3d_texture
*texture
, struct wined3d_context
*context
, BOOL srgb
)
1092 DWORD alloc_flag
= srgb
? WINED3D_TEXTURE_SRGB_ALLOCATED
: WINED3D_TEXTURE_RGB_ALLOCATED
;
1093 const struct wined3d_d3d_info
*d3d_info
= context
->d3d_info
;
1095 if (!d3d_info
->shader_color_key
1096 && !(texture
->async
.flags
& WINED3D_TEXTURE_ASYNC_COLOR_KEY
)
1097 != !(texture
->async
.color_key_flags
& WINED3D_CKEY_SRC_BLT
))
1099 wined3d_texture_force_reload(texture
);
1101 if (texture
->async
.color_key_flags
& WINED3D_CKEY_SRC_BLT
)
1102 texture
->async
.flags
|= WINED3D_TEXTURE_ASYNC_COLOR_KEY
;
1105 if (texture
->flags
& alloc_flag
)
1108 texture
->texture_ops
->texture_prepare_texture(texture
, context
, srgb
);
1109 texture
->flags
|= alloc_flag
;
1112 static void wined3d_texture_prepare_rb(struct wined3d_texture
*texture
,
1113 const struct wined3d_gl_info
*gl_info
, BOOL multisample
)
1115 const struct wined3d_format
*format
= texture
->resource
.format
;
1121 if (texture
->rb_multisample
)
1124 /* TODO: NVIDIA expose their Coverage Sample Anti-Aliasing (CSAA)
1125 * feature through type == MULTISAMPLE_XX and quality != 0. This could
1126 * be mapped to GL_NV_framebuffer_multisample_coverage.
1128 * AMD have a similar feature called Enhanced Quality Anti-Aliasing
1129 * (EQAA), but it does not have an equivalent OpenGL extension. */
1131 /* We advertise as many WINED3D_MULTISAMPLE_NON_MASKABLE quality
1132 * levels as the count of advertised multisample types for the texture
1134 if (texture
->resource
.multisample_type
== WINED3D_MULTISAMPLE_NON_MASKABLE
)
1136 unsigned int i
, count
= 0;
1138 for (i
= 0; i
< sizeof(format
->multisample_types
) * 8; ++i
)
1140 if (format
->multisample_types
& 1u << i
)
1142 if (texture
->resource
.multisample_quality
== count
++)
1150 samples
= texture
->resource
.multisample_type
;
1153 gl_info
->fbo_ops
.glGenRenderbuffers(1, &texture
->rb_multisample
);
1154 gl_info
->fbo_ops
.glBindRenderbuffer(GL_RENDERBUFFER
, texture
->rb_multisample
);
1155 gl_info
->fbo_ops
.glRenderbufferStorageMultisample(GL_RENDERBUFFER
, samples
,
1156 format
->glInternal
, texture
->resource
.width
, texture
->resource
.height
);
1157 checkGLcall("glRenderbufferStorageMultisample()");
1158 TRACE("Created multisample rb %u.\n", texture
->rb_multisample
);
1162 if (texture
->rb_resolved
)
1165 gl_info
->fbo_ops
.glGenRenderbuffers(1, &texture
->rb_resolved
);
1166 gl_info
->fbo_ops
.glBindRenderbuffer(GL_RENDERBUFFER
, texture
->rb_resolved
);
1167 gl_info
->fbo_ops
.glRenderbufferStorage(GL_RENDERBUFFER
, format
->glInternal
,
1168 texture
->resource
.width
, texture
->resource
.height
);
1169 checkGLcall("glRenderbufferStorage()");
1170 TRACE("Created resolved rb %u.\n", texture
->rb_resolved
);
1174 /* Context activation is done by the caller. Context may be NULL in
1175 * WINED3D_NO3D mode. */
1176 BOOL
wined3d_texture_prepare_location(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
,
1177 struct wined3d_context
*context
, DWORD location
)
1181 case WINED3D_LOCATION_SYSMEM
:
1182 if (texture
->sub_resources
[sub_resource_idx
].resource
->heap_memory
)
1185 if (!wined3d_resource_allocate_sysmem(texture
->sub_resources
[sub_resource_idx
].resource
))
1187 ERR("Failed to allocate system memory.\n");
1192 case WINED3D_LOCATION_USER_MEMORY
:
1193 if (!texture
->user_memory
)
1194 ERR("Map binding is set to WINED3D_LOCATION_USER_MEMORY but surface->user_memory is NULL.\n");
1197 case WINED3D_LOCATION_BUFFER
:
1198 wined3d_texture_prepare_buffer_object(texture
, sub_resource_idx
, context
->gl_info
);
1201 case WINED3D_LOCATION_TEXTURE_RGB
:
1202 wined3d_texture_prepare_texture(texture
, context
, FALSE
);
1205 case WINED3D_LOCATION_TEXTURE_SRGB
:
1206 wined3d_texture_prepare_texture(texture
, context
, TRUE
);
1209 case WINED3D_LOCATION_DRAWABLE
:
1210 if (!texture
->swapchain
)
1211 ERR("Texture %p does not have a drawable.\n", texture
);
1214 case WINED3D_LOCATION_RB_MULTISAMPLE
:
1215 wined3d_texture_prepare_rb(texture
, context
->gl_info
, TRUE
);
1218 case WINED3D_LOCATION_RB_RESOLVED
:
1219 wined3d_texture_prepare_rb(texture
, context
->gl_info
, FALSE
);
1223 ERR("Invalid location %s.\n", wined3d_debug_location(location
));
1228 void CDECL
wined3d_texture_generate_mipmaps(struct wined3d_texture
*texture
)
1230 /* TODO: Implement filters using GL_SGI_generate_mipmaps. */
1231 FIXME("texture %p stub!\n", texture
);
1234 struct wined3d_texture_sub_resource
*wined3d_texture_get_sub_resource(struct wined3d_texture
*texture
,
1235 unsigned int sub_resource_idx
)
1237 UINT sub_count
= texture
->level_count
* texture
->layer_count
;
1239 TRACE("texture %p, sub_resource_idx %u.\n", texture
, sub_resource_idx
);
1241 if (sub_resource_idx
>= sub_count
)
1243 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx
, sub_count
);
1247 return &texture
->sub_resources
[sub_resource_idx
];
1250 HRESULT CDECL
wined3d_texture_add_dirty_region(struct wined3d_texture
*texture
,
1251 UINT layer
, const struct wined3d_box
*dirty_region
)
1253 struct wined3d_context
*context
;
1254 unsigned int sub_resource_idx
;
1256 TRACE("texture %p, layer %u, dirty_region %s.\n", texture
, layer
, debug_box(dirty_region
));
1258 if (layer
>= texture
->layer_count
)
1260 WARN("Invalid layer %u specified.\n", layer
);
1261 return WINED3DERR_INVALIDCALL
;
1263 sub_resource_idx
= layer
* texture
->level_count
;
1266 FIXME("Ignoring dirty_region %s.\n", debug_box(dirty_region
));
1268 context
= context_acquire(texture
->resource
.device
, NULL
);
1269 if (!texture
->texture_ops
->texture_load_location(texture
, sub_resource_idx
,
1270 context
, texture
->resource
.map_binding
))
1272 ERR("Failed to load location %s.\n", wined3d_debug_location(texture
->resource
.map_binding
));
1273 context_release(context
);
1274 return E_OUTOFMEMORY
;
1276 wined3d_texture_invalidate_location(texture
, sub_resource_idx
, ~texture
->resource
.map_binding
);
1277 context_release(context
);
1282 static HRESULT
wined3d_texture_upload_data(struct wined3d_texture
*texture
,
1283 const struct wined3d_sub_resource_data
*data
)
1285 unsigned int sub_count
= texture
->level_count
* texture
->layer_count
;
1286 struct wined3d_context
*context
;
1289 for (i
= 0; i
< sub_count
; ++i
)
1293 WARN("Invalid sub-resource data specified for sub-resource %u.\n", i
);
1294 return E_INVALIDARG
;
1298 context
= context_acquire(texture
->resource
.device
, NULL
);
1300 wined3d_texture_prepare_texture(texture
, context
, FALSE
);
1301 wined3d_texture_bind_and_dirtify(texture
, context
, FALSE
);
1303 for (i
= 0; i
< sub_count
; ++i
)
1305 texture
->texture_ops
->texture_upload_data(texture
, i
, context
, &data
[i
]);
1306 wined3d_texture_validate_location(texture
, i
, WINED3D_LOCATION_TEXTURE_RGB
);
1307 wined3d_texture_invalidate_location(texture
, i
, ~WINED3D_LOCATION_TEXTURE_RGB
);
1310 context_release(context
);
1315 static void texture2d_upload_data(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
,
1316 const struct wined3d_context
*context
, const struct wined3d_sub_resource_data
*data
)
1318 static const POINT dst_point
= {0, 0};
1319 struct wined3d_const_bo_address addr
;
1320 unsigned int texture_level
;
1323 texture_level
= sub_resource_idx
% texture
->level_count
;
1324 SetRect(&src_rect
, 0, 0, wined3d_texture_get_level_width(texture
, texture_level
),
1325 wined3d_texture_get_level_height(texture
, texture_level
));
1327 addr
.buffer_object
= 0;
1328 addr
.addr
= data
->data
;
1330 wined3d_surface_upload_data(texture
->sub_resources
[sub_resource_idx
].u
.surface
, context
->gl_info
,
1331 texture
->resource
.format
, &src_rect
, data
->row_pitch
, &dst_point
, FALSE
, &addr
);
1334 static BOOL
texture2d_load_location(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
,
1335 struct wined3d_context
*context
, DWORD location
)
1337 return SUCCEEDED(surface_load_location(texture
->sub_resources
[sub_resource_idx
].u
.surface
, context
, location
));
1340 /* Context activation is done by the caller. */
1341 static void texture2d_prepare_texture(struct wined3d_texture
*texture
, struct wined3d_context
*context
, BOOL srgb
)
1343 UINT sub_count
= texture
->level_count
* texture
->layer_count
;
1344 const struct wined3d_format
*format
= texture
->resource
.format
;
1345 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1346 const struct wined3d_color_key_conversion
*conversion
;
1350 TRACE("texture %p, context %p, format %s.\n", texture
, context
, debug_d3dformat(format
->id
));
1352 if (format
->convert
)
1354 texture
->flags
|= WINED3D_TEXTURE_CONVERTED
;
1356 else if ((conversion
= wined3d_format_get_color_key_conversion(texture
, TRUE
)))
1358 texture
->flags
|= WINED3D_TEXTURE_CONVERTED
;
1359 format
= wined3d_get_format(gl_info
, conversion
->dst_format
);
1360 TRACE("Using format %s for color key conversion.\n", debug_d3dformat(format
->id
));
1363 wined3d_texture_bind_and_dirtify(texture
, context
, srgb
);
1366 internal
= format
->glGammaInternal
;
1367 else if (texture
->resource
.usage
& WINED3DUSAGE_RENDERTARGET
1368 && wined3d_resource_is_offscreen(&texture
->resource
))
1369 internal
= format
->rtInternal
;
1371 internal
= format
->glInternal
;
1374 FIXME("No GL internal format for format %s.\n", debug_d3dformat(format
->id
));
1376 TRACE("internal %#x, format %#x, type %#x.\n", internal
, format
->glFormat
, format
->glType
);
1378 for (i
= 0; i
< sub_count
; ++i
)
1380 struct wined3d_surface
*surface
= texture
->sub_resources
[i
].u
.surface
;
1381 GLsizei width
, height
;
1383 width
= wined3d_texture_get_level_pow2_width(texture
, surface
->texture_level
);
1384 height
= wined3d_texture_get_level_pow2_height(texture
, surface
->texture_level
);
1385 if (texture
->resource
.format_flags
& WINED3DFMT_FLAG_HEIGHT_SCALE
)
1387 height
*= format
->height_scale
.numerator
;
1388 height
/= format
->height_scale
.denominator
;
1391 TRACE("surface %p, target %#x, level %d, width %d, height %d.\n",
1392 surface
, surface
->texture_target
, surface
->texture_level
, width
, height
);
1394 gl_info
->gl_ops
.gl
.p_glTexImage2D(surface
->texture_target
, surface
->texture_level
,
1395 internal
, width
, height
, 0, format
->glFormat
, format
->glType
, NULL
);
1396 checkGLcall("glTexImage2D");
1400 static void texture2d_cleanup_sub_resources(struct wined3d_texture
*texture
)
1402 unsigned int sub_count
= texture
->level_count
* texture
->layer_count
;
1403 struct wined3d_surface
*surface
;
1406 for (i
= 0; i
< sub_count
; ++i
)
1408 if ((surface
= texture
->sub_resources
[i
].u
.surface
))
1410 TRACE("surface %p.\n", surface
);
1412 wined3d_surface_cleanup(surface
);
1413 surface
->resource
.parent_ops
->wined3d_object_destroyed(surface
->resource
.parent
);
1416 HeapFree(GetProcessHeap(), 0, texture
->sub_resources
[0].u
.surface
);
1419 static const struct wined3d_texture_ops texture2d_ops
=
1421 texture2d_upload_data
,
1422 texture2d_load_location
,
1423 texture2d_prepare_texture
,
1424 texture2d_cleanup_sub_resources
,
1427 static ULONG
texture_resource_incref(struct wined3d_resource
*resource
)
1429 return wined3d_texture_incref(wined3d_texture_from_resource(resource
));
1432 static ULONG
texture_resource_decref(struct wined3d_resource
*resource
)
1434 return wined3d_texture_decref(wined3d_texture_from_resource(resource
));
1437 static void wined3d_texture_unload(struct wined3d_resource
*resource
)
1439 struct wined3d_texture
*texture
= wined3d_texture_from_resource(resource
);
1440 UINT sub_count
= texture
->level_count
* texture
->layer_count
;
1441 struct wined3d_device
*device
= resource
->device
;
1442 const struct wined3d_gl_info
*gl_info
;
1443 struct wined3d_context
*context
;
1446 TRACE("texture %p.\n", texture
);
1448 context
= context_acquire(device
, NULL
);
1449 gl_info
= context
->gl_info
;
1451 for (i
= 0; i
< sub_count
; ++i
)
1453 struct wined3d_texture_sub_resource
*sub_resource
= &texture
->sub_resources
[i
];
1455 if (resource
->pool
!= WINED3D_POOL_DEFAULT
1456 && texture
->texture_ops
->texture_load_location(texture
, i
, context
, resource
->map_binding
))
1458 wined3d_texture_invalidate_location(texture
, i
, ~resource
->map_binding
);
1462 /* We should only get here on device reset/teardown for implicit
1464 if (resource
->pool
!= WINED3D_POOL_DEFAULT
|| resource
->type
!= WINED3D_RTYPE_TEXTURE_2D
)
1465 ERR("Discarding %s %p sub-resource %u in the %s pool.\n", debug_d3dresourcetype(resource
->type
),
1466 resource
, i
, debug_d3dpool(resource
->pool
));
1467 wined3d_texture_validate_location(texture
, i
, WINED3D_LOCATION_DISCARDED
);
1468 wined3d_texture_invalidate_location(texture
, i
, ~WINED3D_LOCATION_DISCARDED
);
1471 if (sub_resource
->buffer_object
)
1472 wined3d_texture_remove_buffer_object(texture
, i
, context
->gl_info
);
1474 if (resource
->type
== WINED3D_RTYPE_TEXTURE_2D
)
1476 struct wined3d_surface
*surface
= sub_resource
->u
.surface
;
1477 struct wined3d_renderbuffer_entry
*entry
, *entry2
;
1479 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, &surface
->renderbuffers
, struct wined3d_renderbuffer_entry
, entry
)
1481 context_gl_resource_released(device
, entry
->id
, TRUE
);
1482 gl_info
->fbo_ops
.glDeleteRenderbuffers(1, &entry
->id
);
1483 list_remove(&entry
->entry
);
1484 HeapFree(GetProcessHeap(), 0, entry
);
1486 list_init(&surface
->renderbuffers
);
1487 surface
->current_renderbuffer
= NULL
;
1490 resource_unload(sub_resource
->resource
);
1493 context_release(context
);
1495 wined3d_texture_force_reload(texture
);
1496 wined3d_texture_unload_gl_texture(texture
);
1499 static HRESULT
texture_resource_sub_resource_map(struct wined3d_resource
*resource
, unsigned int sub_resource_idx
,
1500 struct wined3d_map_desc
*map_desc
, const struct wined3d_box
*box
, DWORD flags
)
1502 const struct wined3d_format
*format
= resource
->format
;
1503 struct wined3d_texture_sub_resource
*sub_resource
;
1504 struct wined3d_device
*device
= resource
->device
;
1505 unsigned int fmt_flags
= resource
->format_flags
;
1506 const struct wined3d_gl_info
*gl_info
= NULL
;
1507 struct wined3d_context
*context
= NULL
;
1508 struct wined3d_texture
*texture
;
1509 struct wined3d_bo_address data
;
1510 unsigned int texture_level
;
1514 TRACE("resource %p, sub_resource_idx %u, map_desc %p, box %s, flags %#x.\n",
1515 resource
, sub_resource_idx
, map_desc
, debug_box(box
), flags
);
1517 texture
= wined3d_texture_from_resource(resource
);
1518 if (!(sub_resource
= wined3d_texture_get_sub_resource(texture
, sub_resource_idx
)))
1519 return E_INVALIDARG
;
1521 texture_level
= sub_resource_idx
% texture
->level_count
;
1522 if (box
&& !wined3d_texture_check_box_dimensions(texture
, texture_level
, box
))
1524 WARN("Map box is invalid.\n");
1525 if (resource
->type
!= WINED3D_RTYPE_TEXTURE_2D
)
1526 return WINED3DERR_INVALIDCALL
;
1529 if ((fmt_flags
& WINED3DFMT_FLAG_BLOCKS
) && box
1530 && !wined3d_texture_check_block_align(texture
, texture_level
, box
))
1532 WARN("Map box %s is misaligned for %ux%u blocks.\n",
1533 debug_box(box
), format
->block_width
, format
->block_height
);
1534 if (resource
->type
!= WINED3D_RTYPE_TEXTURE_2D
|| resource
->pool
== WINED3D_POOL_DEFAULT
)
1535 return WINED3DERR_INVALIDCALL
;
1538 if (!(resource
->access_flags
& WINED3D_RESOURCE_ACCESS_CPU
))
1540 WARN("Trying to map unmappable texture.\n");
1541 if (resource
->type
!= WINED3D_RTYPE_TEXTURE_2D
)
1542 return WINED3DERR_INVALIDCALL
;
1545 if (texture
->flags
& WINED3D_TEXTURE_DC_IN_USE
)
1547 WARN("DC is in use.\n");
1548 return WINED3DERR_INVALIDCALL
;
1551 if (sub_resource
->map_count
)
1553 WARN("Sub-resource is already mapped.\n");
1554 return WINED3DERR_INVALIDCALL
;
1557 flags
= wined3d_resource_sanitize_map_flags(resource
, flags
);
1559 if (device
->d3d_initialized
)
1561 context
= context_acquire(device
, NULL
);
1562 gl_info
= context
->gl_info
;
1565 if (flags
& WINED3D_MAP_DISCARD
)
1567 TRACE("WINED3D_MAP_DISCARD flag passed, marking %s as up to date.\n",
1568 wined3d_debug_location(texture
->resource
.map_binding
));
1569 if ((ret
= wined3d_texture_prepare_location(texture
, sub_resource_idx
,
1570 context
, texture
->resource
.map_binding
)))
1571 wined3d_texture_validate_location(texture
, sub_resource_idx
, texture
->resource
.map_binding
);
1575 if (resource
->usage
& WINED3DUSAGE_DYNAMIC
)
1576 WARN_(d3d_perf
)("Mapping a dynamic texture without WINED3D_MAP_DISCARD.\n");
1577 ret
= texture
->texture_ops
->texture_load_location(texture
,
1578 sub_resource_idx
, context
, texture
->resource
.map_binding
);
1583 ERR("Failed to prepare location.\n");
1584 context_release(context
);
1585 return E_OUTOFMEMORY
;
1588 if (!(flags
& (WINED3D_MAP_NO_DIRTY_UPDATE
| WINED3D_MAP_READONLY
)))
1589 wined3d_texture_invalidate_location(texture
, sub_resource_idx
, ~texture
->resource
.map_binding
);
1591 wined3d_texture_get_memory(texture
, sub_resource_idx
, &data
, texture
->resource
.map_binding
);
1592 base_memory
= wined3d_texture_map_bo_address(&data
, sub_resource
->resource
->size
,
1593 gl_info
, GL_PIXEL_UNPACK_BUFFER
, flags
);
1594 TRACE("Base memory pointer %p.\n", base_memory
);
1597 context_release(context
);
1599 if (fmt_flags
& WINED3DFMT_FLAG_BROKEN_PITCH
)
1601 map_desc
->row_pitch
= wined3d_texture_get_level_width(texture
, texture_level
) * format
->byte_count
;
1602 map_desc
->slice_pitch
= wined3d_texture_get_level_height(texture
, texture_level
) * map_desc
->row_pitch
;
1606 wined3d_texture_get_pitch(texture
, texture_level
, &map_desc
->row_pitch
, &map_desc
->slice_pitch
);
1611 map_desc
->data
= base_memory
;
1615 if ((fmt_flags
& (WINED3DFMT_FLAG_BLOCKS
| WINED3DFMT_FLAG_BROKEN_PITCH
)) == WINED3DFMT_FLAG_BLOCKS
)
1617 /* Compressed textures are block based, so calculate the offset of
1618 * the block that contains the top-left pixel of the mapped box. */
1619 map_desc
->data
= base_memory
1620 + (box
->front
* map_desc
->slice_pitch
)
1621 + ((box
->top
/ format
->block_height
) * map_desc
->row_pitch
)
1622 + ((box
->left
/ format
->block_width
) * format
->block_byte_count
);
1626 map_desc
->data
= base_memory
1627 + (box
->front
* map_desc
->slice_pitch
)
1628 + (box
->top
* map_desc
->row_pitch
)
1629 + (box
->left
* format
->byte_count
);
1633 if (texture
->swapchain
&& texture
->swapchain
->front_buffer
== texture
)
1635 RECT
*r
= &texture
->swapchain
->front_buffer_update
;
1638 SetRect(r
, 0, 0, resource
->width
, resource
->height
);
1640 SetRect(r
, box
->left
, box
->top
, box
->right
, box
->bottom
);
1641 TRACE("Mapped front buffer %s.\n", wine_dbgstr_rect(r
));
1644 ++resource
->map_count
;
1645 ++sub_resource
->map_count
;
1647 TRACE("Returning memory %p, row pitch %u, slice pitch %u.\n",
1648 map_desc
->data
, map_desc
->row_pitch
, map_desc
->slice_pitch
);
1653 static HRESULT
texture_resource_sub_resource_unmap(struct wined3d_resource
*resource
, unsigned int sub_resource_idx
)
1655 struct wined3d_texture_sub_resource
*sub_resource
;
1656 struct wined3d_device
*device
= resource
->device
;
1657 const struct wined3d_gl_info
*gl_info
= NULL
;
1658 struct wined3d_context
*context
= NULL
;
1659 struct wined3d_texture
*texture
;
1660 struct wined3d_bo_address data
;
1662 TRACE("resource %p, sub_resource_idx %u.\n", resource
, sub_resource_idx
);
1664 texture
= wined3d_texture_from_resource(resource
);
1665 if (!(sub_resource
= wined3d_texture_get_sub_resource(texture
, sub_resource_idx
)))
1666 return E_INVALIDARG
;
1668 if (!sub_resource
->map_count
)
1670 WARN("Trying to unmap unmapped sub-resource.\n");
1671 if (texture
->flags
& WINED3D_TEXTURE_DC_IN_USE
)
1673 return WINEDDERR_NOTLOCKED
;
1676 if (device
->d3d_initialized
)
1678 context
= context_acquire(device
, NULL
);
1679 gl_info
= context
->gl_info
;
1682 wined3d_texture_get_memory(texture
, sub_resource_idx
, &data
, texture
->resource
.map_binding
);
1683 wined3d_texture_unmap_bo_address(&data
, gl_info
, GL_PIXEL_UNPACK_BUFFER
);
1686 context_release(context
);
1688 if (texture
->swapchain
&& texture
->swapchain
->front_buffer
== texture
)
1690 if (!(sub_resource
->locations
& (WINED3D_LOCATION_DRAWABLE
| WINED3D_LOCATION_TEXTURE_RGB
)))
1691 texture
->swapchain
->swapchain_ops
->swapchain_frontbuffer_updated(texture
->swapchain
);
1693 else if (resource
->format_flags
& (WINED3DFMT_FLAG_DEPTH
| WINED3DFMT_FLAG_STENCIL
))
1695 FIXME("Depth / stencil buffer locking is not implemented.\n");
1698 --sub_resource
->map_count
;
1699 if (!--resource
->map_count
&& texture
->update_map_binding
)
1700 wined3d_texture_update_map_binding(texture
);
1705 static const struct wined3d_resource_ops texture_resource_ops
=
1707 texture_resource_incref
,
1708 texture_resource_decref
,
1709 wined3d_texture_unload
,
1710 texture_resource_sub_resource_map
,
1711 texture_resource_sub_resource_unmap
,
1714 static HRESULT
texture_init(struct wined3d_texture
*texture
, const struct wined3d_resource_desc
*desc
,
1715 unsigned int layer_count
, unsigned int level_count
, DWORD flags
, struct wined3d_device
*device
,
1716 void *parent
, const struct wined3d_parent_ops
*parent_ops
)
1718 struct wined3d_device_parent
*device_parent
= device
->device_parent
;
1719 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
1720 struct wined3d_resource_desc surface_desc
;
1721 struct wined3d_surface
*surfaces
;
1722 UINT pow2_width
, pow2_height
;
1726 /* TODO: It should only be possible to create textures for formats
1727 * that are reported as supported. */
1728 if (WINED3DFMT_UNKNOWN
>= desc
->format
)
1730 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture
);
1731 return WINED3DERR_INVALIDCALL
;
1734 if (desc
->usage
& WINED3DUSAGE_DYNAMIC
&& desc
->pool
== WINED3D_POOL_MANAGED
)
1735 FIXME("Trying to create a managed texture with dynamic usage.\n");
1736 if (!(desc
->usage
& (WINED3DUSAGE_DYNAMIC
| WINED3DUSAGE_RENDERTARGET
| WINED3DUSAGE_DEPTHSTENCIL
))
1737 && (flags
& WINED3D_TEXTURE_CREATE_MAPPABLE
))
1738 WARN("Creating a mappable texture in the default pool that doesn't specify dynamic usage.\n");
1739 if (desc
->usage
& WINED3DUSAGE_RENDERTARGET
&& desc
->pool
!= WINED3D_POOL_DEFAULT
)
1740 FIXME("Trying to create a render target that isn't in the default pool.\n");
1742 pow2_width
= desc
->width
;
1743 pow2_height
= desc
->height
;
1744 if (((desc
->width
& (desc
->width
- 1)) || (desc
->height
& (desc
->height
- 1)))
1745 && !gl_info
->supported
[ARB_TEXTURE_NON_POWER_OF_TWO
])
1747 /* level_count == 0 returns an error as well. */
1748 if (level_count
!= 1 || desc
->usage
& WINED3DUSAGE_LEGACY_CUBEMAP
)
1750 if (desc
->pool
!= WINED3D_POOL_SCRATCH
)
1752 WARN("Attempted to create a mipmapped/cube NPOT texture without unconditional NPOT support.\n");
1753 return WINED3DERR_INVALIDCALL
;
1756 WARN("Creating a scratch mipmapped/cube NPOT texture despite lack of HW support.\n");
1758 texture
->flags
|= WINED3D_TEXTURE_COND_NP2
;
1760 if (!gl_info
->supported
[ARB_TEXTURE_RECTANGLE
] && !gl_info
->supported
[WINED3D_GL_NORMALIZED_TEXRECT
])
1762 const struct wined3d_format
*format
= wined3d_get_format(gl_info
, desc
->format
);
1764 /* TODO: Add support for non-power-of-two compressed textures. */
1765 if (format
->flags
[WINED3D_GL_RES_TYPE_TEX_2D
]
1766 & (WINED3DFMT_FLAG_COMPRESSED
| WINED3DFMT_FLAG_HEIGHT_SCALE
))
1768 FIXME("Compressed or height scaled non-power-of-two (%ux%u) textures are not supported.\n",
1769 desc
->width
, desc
->height
);
1770 return WINED3DERR_NOTAVAILABLE
;
1773 /* Find the nearest pow2 match. */
1774 pow2_width
= pow2_height
= 1;
1775 while (pow2_width
< desc
->width
)
1777 while (pow2_height
< desc
->height
)
1779 texture
->flags
|= WINED3D_TEXTURE_COND_NP2_EMULATED
;
1782 texture
->pow2_width
= pow2_width
;
1783 texture
->pow2_height
= pow2_height
;
1785 if ((pow2_width
> gl_info
->limits
.texture_size
|| pow2_height
> gl_info
->limits
.texture_size
)
1786 && (desc
->usage
& WINED3DUSAGE_TEXTURE
))
1788 /* One of four options:
1789 * 1: Do the same as we do with NPOT and scale the texture. (Any
1790 * texture ops would require the texture to be scaled which is
1791 * potentially slow.)
1792 * 2: Set the texture to the maximum size (bad idea).
1793 * 3: WARN and return WINED3DERR_NOTAVAILABLE.
1794 * 4: Create the surface, but allow it to be used only for DirectDraw
1795 * Blts. Some apps (e.g. Swat 3) create textures with a height of
1796 * 16 and a width > 3000 and blt 16x16 letter areas from them to
1797 * the render target. */
1798 if (desc
->pool
== WINED3D_POOL_DEFAULT
|| desc
->pool
== WINED3D_POOL_MANAGED
)
1800 WARN("Dimensions (%ux%u) exceed the maximum texture size.\n", pow2_width
, pow2_height
);
1801 return WINED3DERR_NOTAVAILABLE
;
1804 /* We should never use this surface in combination with OpenGL. */
1805 TRACE("Creating an oversized (%ux%u) surface.\n", pow2_width
, pow2_height
);
1808 /* Calculate levels for mip mapping. */
1809 if (desc
->usage
& WINED3DUSAGE_AUTOGENMIPMAP
)
1811 if (!gl_info
->supported
[SGIS_GENERATE_MIPMAP
])
1813 WARN("No mipmap generation support, returning WINED3DERR_INVALIDCALL.\n");
1814 return WINED3DERR_INVALIDCALL
;
1817 if (level_count
!= 1)
1819 WARN("WINED3DUSAGE_AUTOGENMIPMAP is set, and level count != 1, returning WINED3DERR_INVALIDCALL.\n");
1820 return WINED3DERR_INVALIDCALL
;
1824 if (FAILED(hr
= wined3d_texture_init(texture
, &texture2d_ops
, layer_count
, level_count
, desc
,
1825 flags
, device
, parent
, parent_ops
, &texture_resource_ops
)))
1827 WARN("Failed to initialize texture, returning %#x.\n", hr
);
1831 /* Precalculated scaling for 'faked' non power of two texture coords. */
1832 if (texture
->resource
.gl_type
== WINED3D_GL_RES_TYPE_TEX_RECT
)
1834 texture
->pow2_matrix
[0] = (float)desc
->width
;
1835 texture
->pow2_matrix
[5] = (float)desc
->height
;
1836 texture
->flags
&= ~(WINED3D_TEXTURE_POW2_MAT_IDENT
| WINED3D_TEXTURE_NORMALIZED_COORDS
);
1837 texture
->target
= GL_TEXTURE_RECTANGLE_ARB
;
1841 if (texture
->flags
& WINED3D_TEXTURE_COND_NP2_EMULATED
)
1843 texture
->pow2_matrix
[0] = (((float)desc
->width
) / ((float)pow2_width
));
1844 texture
->pow2_matrix
[5] = (((float)desc
->height
) / ((float)pow2_height
));
1845 texture
->flags
&= ~WINED3D_TEXTURE_POW2_MAT_IDENT
;
1849 texture
->pow2_matrix
[0] = 1.0f
;
1850 texture
->pow2_matrix
[5] = 1.0f
;
1852 if (desc
->usage
& WINED3DUSAGE_LEGACY_CUBEMAP
)
1853 texture
->target
= GL_TEXTURE_CUBE_MAP_ARB
;
1855 texture
->target
= GL_TEXTURE_2D
;
1857 texture
->pow2_matrix
[10] = 1.0f
;
1858 texture
->pow2_matrix
[15] = 1.0f
;
1859 TRACE("x scale %.8e, y scale %.8e.\n", texture
->pow2_matrix
[0], texture
->pow2_matrix
[5]);
1861 if (wined3d_texture_use_pbo(texture
, gl_info
))
1862 texture
->resource
.map_binding
= WINED3D_LOCATION_BUFFER
;
1864 if (!(surfaces
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*surfaces
) * level_count
* layer_count
)))
1866 wined3d_texture_cleanup(texture
);
1867 return E_OUTOFMEMORY
;
1870 /* Generate all the surfaces. */
1871 surface_desc
= *desc
;
1872 surface_desc
.resource_type
= WINED3D_RTYPE_SURFACE
;
1873 for (i
= 0; i
< texture
->level_count
; ++i
)
1875 for (j
= 0; j
< texture
->layer_count
; ++j
)
1877 static const GLenum cube_targets
[6] =
1879 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
,
1880 GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
,
1881 GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
,
1882 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
,
1883 GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
,
1884 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
,
1886 GLenum target
= desc
->usage
& WINED3DUSAGE_LEGACY_CUBEMAP
? cube_targets
[j
] : texture
->target
;
1887 struct wined3d_texture_sub_resource
*sub_resource
;
1888 unsigned int idx
= j
* texture
->level_count
+ i
;
1889 struct wined3d_surface
*surface
;
1891 surface
= &surfaces
[idx
];
1892 if (FAILED(hr
= wined3d_surface_init(surface
, texture
, &surface_desc
, target
, i
, j
)))
1894 WARN("Failed to initialize surface, returning %#x.\n", hr
);
1895 wined3d_texture_cleanup(texture
);
1897 HeapFree(GetProcessHeap(), 0, surfaces
);
1901 sub_resource
= &texture
->sub_resources
[idx
];
1902 sub_resource
->locations
= WINED3D_LOCATION_DISCARDED
;
1903 sub_resource
->resource
= &surface
->resource
;
1904 sub_resource
->u
.surface
= surface
;
1905 if (!(texture
->resource
.usage
& WINED3DUSAGE_DEPTHSTENCIL
))
1907 wined3d_texture_validate_location(texture
, idx
, WINED3D_LOCATION_SYSMEM
);
1908 wined3d_texture_invalidate_location(texture
, idx
, ~WINED3D_LOCATION_SYSMEM
);
1911 if (FAILED(hr
= device_parent
->ops
->surface_created(device_parent
,
1912 texture
, idx
, &parent
, &parent_ops
)))
1914 WARN("Failed to create surface parent, hr %#x.\n", hr
);
1915 wined3d_surface_cleanup(surface
);
1916 wined3d_texture_cleanup(texture
);
1920 TRACE("parent %p, parent_ops %p.\n", parent
, parent_ops
);
1922 surface
->resource
.parent
= parent
;
1923 surface
->resource
.parent_ops
= parent_ops
;
1924 TRACE("Created surface level %u, layer %u @ %p.\n", i
, j
, surface
);
1926 if (((desc
->usage
& WINED3DUSAGE_OWNDC
) || (device
->wined3d
->flags
& WINED3D_NO3D
))
1927 && FAILED(hr
= wined3d_surface_create_dc(surface
)))
1929 wined3d_texture_cleanup(texture
);
1933 /* Calculate the next mipmap level. */
1934 surface_desc
.width
= max(1, surface_desc
.width
>> 1);
1935 surface_desc
.height
= max(1, surface_desc
.height
>> 1);
1941 static void texture3d_upload_data(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
,
1942 const struct wined3d_context
*context
, const struct wined3d_sub_resource_data
*data
)
1944 struct wined3d_const_bo_address addr
;
1945 unsigned int row_pitch
, slice_pitch
;
1947 wined3d_texture_get_pitch(texture
, sub_resource_idx
, &row_pitch
, &slice_pitch
);
1948 if (row_pitch
!= data
->row_pitch
|| slice_pitch
!= data
->slice_pitch
)
1949 FIXME("Ignoring row/slice pitch (%u/%u).\n", data
->row_pitch
, data
->slice_pitch
);
1951 addr
.buffer_object
= 0;
1952 addr
.addr
= data
->data
;
1954 wined3d_volume_upload_data(texture
->sub_resources
[sub_resource_idx
].u
.volume
, context
, &addr
);
1957 static BOOL
texture3d_load_location(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
,
1958 struct wined3d_context
*context
, DWORD location
)
1960 return wined3d_volume_load_location(texture
->sub_resources
[sub_resource_idx
].u
.volume
, context
, location
);
1963 static void texture3d_prepare_texture(struct wined3d_texture
*texture
, struct wined3d_context
*context
, BOOL srgb
)
1965 unsigned int sub_count
= texture
->level_count
* texture
->layer_count
;
1966 const struct wined3d_format
*format
= texture
->resource
.format
;
1967 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1970 wined3d_texture_bind_and_dirtify(texture
, context
, srgb
);
1972 for (i
= 0; i
< sub_count
; ++i
)
1974 struct wined3d_volume
*volume
= texture
->sub_resources
[i
].u
.volume
;
1976 GL_EXTCALL(glTexImage3D(GL_TEXTURE_3D
, volume
->texture_level
,
1977 srgb
? format
->glGammaInternal
: format
->glInternal
,
1978 wined3d_texture_get_level_width(texture
, volume
->texture_level
),
1979 wined3d_texture_get_level_height(texture
, volume
->texture_level
),
1980 wined3d_texture_get_level_depth(texture
, volume
->texture_level
),
1981 0, format
->glFormat
, format
->glType
, NULL
));
1982 checkGLcall("glTexImage3D");
1986 static void texture3d_cleanup_sub_resources(struct wined3d_texture
*texture
)
1988 unsigned int sub_count
= texture
->level_count
* texture
->layer_count
;
1989 struct wined3d_volume
*volume
;
1992 for (i
= 0; i
< sub_count
; ++i
)
1994 if ((volume
= texture
->sub_resources
[i
].u
.volume
))
1996 TRACE("volume %p.\n", volume
);
1998 wined3d_volume_cleanup(volume
);
1999 volume
->resource
.parent_ops
->wined3d_object_destroyed(volume
->resource
.parent
);
2002 HeapFree(GetProcessHeap(), 0, texture
->sub_resources
[0].u
.volume
);
2005 static const struct wined3d_texture_ops texture3d_ops
=
2007 texture3d_upload_data
,
2008 texture3d_load_location
,
2009 texture3d_prepare_texture
,
2010 texture3d_cleanup_sub_resources
,
2013 BOOL
wined3d_texture_check_block_align(const struct wined3d_texture
*texture
,
2014 unsigned int level
, const struct wined3d_box
*box
)
2016 const struct wined3d_format
*format
= texture
->resource
.format
;
2017 unsigned int height
= wined3d_texture_get_level_height(texture
, level
);
2018 unsigned int width
= wined3d_texture_get_level_width(texture
, level
);
2019 unsigned int width_mask
, height_mask
;
2021 if ((box
->left
>= box
->right
)
2022 || (box
->top
>= box
->bottom
)
2023 || (box
->right
> width
)
2024 || (box
->bottom
> height
))
2027 /* This assumes power of two block sizes, but NPOT block sizes would be
2030 * This also assumes that the format's block depth is 1. */
2031 width_mask
= format
->block_width
- 1;
2032 height_mask
= format
->block_height
- 1;
2034 if ((box
->left
& width_mask
) || (box
->top
& height_mask
)
2035 || (box
->right
& width_mask
&& box
->right
!= width
)
2036 || (box
->bottom
& height_mask
&& box
->bottom
!= height
))
2042 static HRESULT
volumetexture_init(struct wined3d_texture
*texture
, const struct wined3d_resource_desc
*desc
,
2043 UINT levels
, struct wined3d_device
*device
, void *parent
, const struct wined3d_parent_ops
*parent_ops
)
2045 struct wined3d_device_parent
*device_parent
= device
->device_parent
;
2046 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
2047 struct wined3d_resource_desc volume_desc
;
2048 struct wined3d_volume
*volumes
;
2052 /* TODO: It should only be possible to create textures for formats
2053 * that are reported as supported. */
2054 if (WINED3DFMT_UNKNOWN
>= desc
->format
)
2056 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture
);
2057 return WINED3DERR_INVALIDCALL
;
2060 if (!gl_info
->supported
[EXT_TEXTURE3D
])
2062 WARN("(%p) : Texture cannot be created - no volume texture support.\n", texture
);
2063 return WINED3DERR_INVALIDCALL
;
2066 /* Calculate levels for mip mapping. */
2067 if (desc
->usage
& WINED3DUSAGE_AUTOGENMIPMAP
)
2069 if (!gl_info
->supported
[SGIS_GENERATE_MIPMAP
])
2071 WARN("No mipmap generation support, returning D3DERR_INVALIDCALL.\n");
2072 return WINED3DERR_INVALIDCALL
;
2077 WARN("WINED3DUSAGE_AUTOGENMIPMAP is set, and level count != 1, returning D3DERR_INVALIDCALL.\n");
2078 return WINED3DERR_INVALIDCALL
;
2082 if (desc
->usage
& WINED3DUSAGE_DYNAMIC
&& (desc
->pool
== WINED3D_POOL_MANAGED
2083 || desc
->pool
== WINED3D_POOL_SCRATCH
))
2085 WARN("Attempted to create a DYNAMIC texture in pool %s.\n", debug_d3dpool(desc
->pool
));
2086 return WINED3DERR_INVALIDCALL
;
2089 if (!gl_info
->supported
[ARB_TEXTURE_NON_POWER_OF_TWO
])
2091 UINT pow2_w
, pow2_h
, pow2_d
;
2093 while (pow2_w
< desc
->width
)
2096 while (pow2_h
< desc
->height
)
2099 while (pow2_d
< desc
->depth
)
2102 if (pow2_w
!= desc
->width
|| pow2_h
!= desc
->height
|| pow2_d
!= desc
->depth
)
2104 if (desc
->pool
== WINED3D_POOL_SCRATCH
)
2106 WARN("Creating a scratch NPOT volume texture despite lack of HW support.\n");
2110 WARN("Attempted to create a NPOT volume texture (%u, %u, %u) without GL support.\n",
2111 desc
->width
, desc
->height
, desc
->depth
);
2112 return WINED3DERR_INVALIDCALL
;
2117 if (FAILED(hr
= wined3d_texture_init(texture
, &texture3d_ops
, 1, levels
, desc
,
2118 0, device
, parent
, parent_ops
, &texture_resource_ops
)))
2120 WARN("Failed to initialize texture, returning %#x.\n", hr
);
2124 texture
->pow2_matrix
[0] = 1.0f
;
2125 texture
->pow2_matrix
[5] = 1.0f
;
2126 texture
->pow2_matrix
[10] = 1.0f
;
2127 texture
->pow2_matrix
[15] = 1.0f
;
2128 texture
->target
= GL_TEXTURE_3D
;
2130 if (wined3d_texture_use_pbo(texture
, gl_info
))
2131 texture
->resource
.map_binding
= WINED3D_LOCATION_BUFFER
;
2133 if (!(volumes
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*volumes
) * levels
)))
2135 wined3d_texture_cleanup(texture
);
2136 return E_OUTOFMEMORY
;
2139 /* Generate all the surfaces. */
2140 volume_desc
= *desc
;
2141 volume_desc
.resource_type
= WINED3D_RTYPE_VOLUME
;
2142 for (i
= 0; i
< texture
->level_count
; ++i
)
2144 struct wined3d_texture_sub_resource
*sub_resource
;
2145 struct wined3d_volume
*volume
;
2147 volume
= &volumes
[i
];
2148 if (FAILED(hr
= wined3d_volume_init(volume
, texture
, &volume_desc
, i
)))
2150 WARN("Failed to initialize volume, returning %#x.\n", hr
);
2151 wined3d_texture_cleanup(texture
);
2153 HeapFree(GetProcessHeap(), 0, volumes
);
2157 sub_resource
= &texture
->sub_resources
[i
];
2158 sub_resource
->locations
= WINED3D_LOCATION_DISCARDED
;
2159 sub_resource
->resource
= &volume
->resource
;
2160 sub_resource
->u
.volume
= volume
;
2162 if (FAILED(hr
= device_parent
->ops
->volume_created(device_parent
,
2163 texture
, i
, &parent
, &parent_ops
)))
2165 WARN("Failed to create volume parent, hr %#x.\n", hr
);
2166 wined3d_volume_cleanup(volume
);
2167 wined3d_texture_cleanup(texture
);
2171 TRACE("parent %p, parent_ops %p.\n", parent
, parent_ops
);
2173 volume
->resource
.parent
= parent
;
2174 volume
->resource
.parent_ops
= parent_ops
;
2175 TRACE("Created volume level %u @ %p.\n", i
, volume
);
2177 /* Calculate the next mipmap level. */
2178 volume_desc
.width
= max(1, volume_desc
.width
>> 1);
2179 volume_desc
.height
= max(1, volume_desc
.height
>> 1);
2180 volume_desc
.depth
= max(1, volume_desc
.depth
>> 1);
2186 HRESULT CDECL
wined3d_texture_blt(struct wined3d_texture
*dst_texture
, unsigned int dst_sub_resource_idx
,
2187 const RECT
*dst_rect
, struct wined3d_texture
*src_texture
, unsigned int src_sub_resource_idx
,
2188 const RECT
*src_rect
, DWORD flags
, const struct wined3d_blt_fx
*fx
, enum wined3d_texture_filter_type filter
)
2190 struct wined3d_texture_sub_resource
*dst_resource
, *src_resource
= NULL
;
2192 TRACE("dst_texture %p, dst_sub_resource_idx %u, dst_rect %s, src_texture %p, "
2193 "src_sub_resource_idx %u, src_rect %s, flags %#x, fx %p, filter %s.\n",
2194 dst_texture
, dst_sub_resource_idx
, wine_dbgstr_rect(dst_rect
), src_texture
,
2195 src_sub_resource_idx
, wine_dbgstr_rect(src_rect
), flags
, fx
, debug_d3dtexturefiltertype(filter
));
2197 if (!(dst_resource
= wined3d_texture_get_sub_resource(dst_texture
, dst_sub_resource_idx
))
2198 || dst_texture
->resource
.type
!= WINED3D_RTYPE_TEXTURE_2D
)
2199 return WINED3DERR_INVALIDCALL
;
2203 if (!(src_resource
= wined3d_texture_get_sub_resource(src_texture
, src_sub_resource_idx
))
2204 || src_texture
->resource
.type
!= WINED3D_RTYPE_TEXTURE_2D
)
2205 return WINED3DERR_INVALIDCALL
;
2208 return wined3d_surface_blt(dst_resource
->u
.surface
, dst_rect
,
2209 src_resource
? src_resource
->u
.surface
: NULL
, src_rect
, flags
, fx
, filter
);
2212 HRESULT CDECL
wined3d_texture_get_overlay_position(const struct wined3d_texture
*texture
,
2213 unsigned int sub_resource_idx
, LONG
*x
, LONG
*y
)
2215 struct wined3d_surface
*surface
;
2217 TRACE("texture %p, sub_resource_idx %u, x %p, y %p.\n", texture
, sub_resource_idx
, x
, y
);
2219 if (!(texture
->resource
.usage
& WINED3DUSAGE_OVERLAY
) || texture
->resource
.type
!= WINED3D_RTYPE_TEXTURE_2D
2220 || sub_resource_idx
>= texture
->level_count
* texture
->layer_count
)
2222 WARN("Invalid sub-resource specified.\n");
2223 return WINEDDERR_NOTAOVERLAYSURFACE
;
2226 surface
= texture
->sub_resources
[sub_resource_idx
].u
.surface
;
2227 if (!surface
->overlay_dest
)
2229 TRACE("Overlay not visible.\n");
2232 return WINEDDERR_OVERLAYNOTVISIBLE
;
2235 *x
= surface
->overlay_destrect
.left
;
2236 *y
= surface
->overlay_destrect
.top
;
2238 TRACE("Returning position %d, %d.\n", *x
, *y
);
2243 HRESULT CDECL
wined3d_texture_set_overlay_position(struct wined3d_texture
*texture
,
2244 unsigned int sub_resource_idx
, LONG x
, LONG y
)
2246 struct wined3d_texture_sub_resource
*sub_resource
;
2247 struct wined3d_surface
*surface
;
2250 TRACE("texture %p, sub_resource_idx %u, x %d, y %d.\n", texture
, sub_resource_idx
, x
, y
);
2252 if (!(texture
->resource
.usage
& WINED3DUSAGE_OVERLAY
) || texture
->resource
.type
!= WINED3D_RTYPE_TEXTURE_2D
2253 || !(sub_resource
= wined3d_texture_get_sub_resource(texture
, sub_resource_idx
)))
2255 WARN("Invalid sub-resource specified.\n");
2256 return WINEDDERR_NOTAOVERLAYSURFACE
;
2259 surface
= sub_resource
->u
.surface
;
2260 w
= surface
->overlay_destrect
.right
- surface
->overlay_destrect
.left
;
2261 h
= surface
->overlay_destrect
.bottom
- surface
->overlay_destrect
.top
;
2262 surface
->overlay_destrect
.left
= x
;
2263 surface
->overlay_destrect
.top
= y
;
2264 surface
->overlay_destrect
.right
= x
+ w
;
2265 surface
->overlay_destrect
.bottom
= y
+ h
;
2270 HRESULT CDECL
wined3d_texture_update_overlay(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
,
2271 const RECT
*src_rect
, struct wined3d_texture
*dst_texture
, unsigned int dst_sub_resource_idx
,
2272 const RECT
*dst_rect
, DWORD flags
)
2274 struct wined3d_texture_sub_resource
*sub_resource
, *dst_sub_resource
;
2275 struct wined3d_surface
*surface
, *dst_surface
;
2277 TRACE("texture %p, sub_resource_idx %u, src_rect %s, dst_texture %p, "
2278 "dst_sub_resource_idx %u, dst_rect %s, flags %#x.\n",
2279 texture
, sub_resource_idx
, wine_dbgstr_rect(src_rect
), dst_texture
,
2280 dst_sub_resource_idx
, wine_dbgstr_rect(dst_rect
), flags
);
2282 if (!(texture
->resource
.usage
& WINED3DUSAGE_OVERLAY
) || texture
->resource
.type
!= WINED3D_RTYPE_TEXTURE_2D
2283 || !(sub_resource
= wined3d_texture_get_sub_resource(texture
, sub_resource_idx
)))
2285 WARN("Invalid sub-resource specified.\n");
2286 return WINEDDERR_NOTAOVERLAYSURFACE
;
2289 if (!dst_texture
|| dst_texture
->resource
.type
!= WINED3D_RTYPE_TEXTURE_2D
2290 || !(dst_sub_resource
= wined3d_texture_get_sub_resource(dst_texture
, dst_sub_resource_idx
)))
2292 WARN("Invalid destination sub-resource specified.\n");
2293 return WINED3DERR_INVALIDCALL
;
2296 surface
= sub_resource
->u
.surface
;
2298 surface
->overlay_srcrect
= *src_rect
;
2300 SetRect(&surface
->overlay_srcrect
, 0, 0,
2301 wined3d_texture_get_level_width(texture
, surface
->texture_level
),
2302 wined3d_texture_get_level_height(texture
, surface
->texture_level
));
2304 dst_surface
= dst_sub_resource
->u
.surface
;
2306 surface
->overlay_destrect
= *dst_rect
;
2308 SetRect(&surface
->overlay_destrect
, 0, 0,
2309 wined3d_texture_get_level_width(dst_texture
, dst_surface
->texture_level
),
2310 wined3d_texture_get_level_height(dst_texture
, dst_surface
->texture_level
));
2312 if (surface
->overlay_dest
&& (surface
->overlay_dest
!= dst_surface
|| flags
& WINEDDOVER_HIDE
))
2314 surface
->overlay_dest
= NULL
;
2315 list_remove(&surface
->overlay_entry
);
2318 if (flags
& WINEDDOVER_SHOW
)
2320 if (surface
->overlay_dest
!= dst_surface
)
2322 surface
->overlay_dest
= dst_surface
;
2323 list_add_tail(&dst_surface
->overlays
, &surface
->overlay_entry
);
2326 else if (flags
& WINEDDOVER_HIDE
)
2328 /* Tests show that the rectangles are erased on hide. */
2329 SetRectEmpty(&surface
->overlay_srcrect
);
2330 SetRectEmpty(&surface
->overlay_destrect
);
2331 surface
->overlay_dest
= NULL
;
2337 void * CDECL
wined3d_texture_get_sub_resource_parent(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
)
2339 unsigned int sub_count
= texture
->level_count
* texture
->layer_count
;
2341 TRACE("texture %p, sub_resource_idx %u.\n", texture
, sub_resource_idx
);
2343 if (sub_resource_idx
>= sub_count
)
2345 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx
, sub_count
);
2349 return texture
->sub_resources
[sub_resource_idx
].resource
->parent
;
2352 void CDECL
wined3d_texture_set_sub_resource_parent(struct wined3d_texture
*texture
,
2353 unsigned int sub_resource_idx
, void *parent
)
2355 unsigned int sub_count
= texture
->level_count
* texture
->layer_count
;
2357 TRACE("texture %p, sub_resource_idx %u, parent %p.\n", texture
, sub_resource_idx
, parent
);
2359 if (sub_resource_idx
>= sub_count
)
2361 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx
, sub_count
);
2365 texture
->sub_resources
[sub_resource_idx
].resource
->parent
= parent
;
2368 HRESULT CDECL
wined3d_texture_get_sub_resource_desc(const struct wined3d_texture
*texture
,
2369 unsigned int sub_resource_idx
, struct wined3d_sub_resource_desc
*desc
)
2371 unsigned int sub_count
= texture
->level_count
* texture
->layer_count
;
2372 const struct wined3d_resource
*resource
;
2373 unsigned int level_idx
;
2375 TRACE("texture %p, sub_resource_idx %u, desc %p.\n", texture
, sub_resource_idx
, desc
);
2377 if (sub_resource_idx
>= sub_count
)
2379 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx
, sub_count
);
2380 return WINED3DERR_INVALIDCALL
;
2383 resource
= &texture
->resource
;
2384 desc
->format
= resource
->format
->id
;
2385 desc
->multisample_type
= resource
->multisample_type
;
2386 desc
->multisample_quality
= resource
->multisample_quality
;
2387 desc
->usage
= resource
->usage
;
2388 desc
->pool
= resource
->pool
;
2390 level_idx
= sub_resource_idx
% texture
->level_count
;
2391 desc
->width
= wined3d_texture_get_level_width(texture
, level_idx
);
2392 desc
->height
= wined3d_texture_get_level_height(texture
, level_idx
);
2393 desc
->depth
= wined3d_texture_get_level_depth(texture
, level_idx
);
2394 desc
->size
= texture
->sub_resources
[sub_resource_idx
].resource
->size
;
2399 HRESULT CDECL
wined3d_texture_create(struct wined3d_device
*device
, const struct wined3d_resource_desc
*desc
,
2400 UINT level_count
, DWORD flags
, const struct wined3d_sub_resource_data
*data
, void *parent
,
2401 const struct wined3d_parent_ops
*parent_ops
, struct wined3d_texture
**texture
)
2403 unsigned int layer_count
= desc
->usage
& WINED3DUSAGE_LEGACY_CUBEMAP
? 6 : 1;
2404 struct wined3d_texture
*object
;
2407 TRACE("device %p, desc %p, level_count %u, flags %#x, data %p, parent %p, parent_ops %p, texture %p.\n",
2408 device
, desc
, level_count
, flags
, data
, parent
, parent_ops
, texture
);
2412 WARN("Invalid level count.\n");
2413 return WINED3DERR_INVALIDCALL
;
2416 if (desc
->multisample_type
!= WINED3D_MULTISAMPLE_NONE
)
2418 const struct wined3d_format
*format
= wined3d_get_format(&device
->adapter
->gl_info
, desc
->format
);
2420 if (desc
->multisample_type
== WINED3D_MULTISAMPLE_NON_MASKABLE
2421 && desc
->multisample_quality
>= wined3d_popcount(format
->multisample_types
))
2423 WARN("Unsupported quality level %u requested for WINED3D_MULTISAMPLE_NON_MASKABLE.\n",
2424 desc
->multisample_quality
);
2425 return WINED3DERR_NOTAVAILABLE
;
2427 if (desc
->multisample_type
!= WINED3D_MULTISAMPLE_NON_MASKABLE
2428 && (!(format
->multisample_types
& 1u << (desc
->multisample_type
- 1))
2429 || desc
->multisample_quality
))
2431 WARN("Unsupported multisample type %u quality %u requested.\n", desc
->multisample_type
,
2432 desc
->multisample_quality
);
2433 return WINED3DERR_NOTAVAILABLE
;
2437 if (!(object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
2438 FIELD_OFFSET(struct wined3d_texture
, sub_resources
[level_count
* layer_count
]))))
2439 return E_OUTOFMEMORY
;
2441 switch (desc
->resource_type
)
2443 case WINED3D_RTYPE_TEXTURE_2D
:
2444 hr
= texture_init(object
, desc
, layer_count
, level_count
, flags
, device
, parent
, parent_ops
);
2447 case WINED3D_RTYPE_TEXTURE_3D
:
2448 hr
= volumetexture_init(object
, desc
, level_count
, device
, parent
, parent_ops
);
2452 ERR("Invalid resource type %s.\n", debug_d3dresourcetype(desc
->resource_type
));
2453 hr
= WINED3DERR_INVALIDCALL
;
2459 WARN("Failed to initialize texture, returning %#x.\n", hr
);
2460 HeapFree(GetProcessHeap(), 0, object
);
2464 /* FIXME: We'd like to avoid ever allocating system memory for the texture
2466 if (data
&& FAILED(hr
= wined3d_texture_upload_data(object
, data
)))
2468 wined3d_texture_cleanup(object
);
2469 HeapFree(GetProcessHeap(), 0, object
);
2473 TRACE("Created texture %p.\n", object
);
2479 HRESULT CDECL
wined3d_texture_get_dc(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
, HDC
*dc
)
2481 struct wined3d_device
*device
= texture
->resource
.device
;
2482 struct wined3d_texture_sub_resource
*sub_resource
;
2483 struct wined3d_context
*context
= NULL
;
2484 struct wined3d_surface
*surface
;
2485 HRESULT hr
= WINED3D_OK
;
2487 TRACE("texture %p, sub_resource_idx %u, dc %p.\n", texture
, sub_resource_idx
, dc
);
2489 if (!(sub_resource
= wined3d_texture_get_sub_resource(texture
, sub_resource_idx
)))
2490 return WINED3DERR_INVALIDCALL
;
2492 if (texture
->resource
.type
!= WINED3D_RTYPE_TEXTURE_2D
)
2494 WARN("Not supported on %s resources.\n", debug_d3dresourcetype(texture
->resource
.type
));
2495 return WINED3DERR_INVALIDCALL
;
2498 surface
= sub_resource
->u
.surface
;
2500 if (texture
->resource
.map_count
&& !(texture
->flags
& WINED3D_TEXTURE_GET_DC_LENIENT
))
2501 return WINED3DERR_INVALIDCALL
;
2503 if (device
->d3d_initialized
)
2504 context
= context_acquire(device
, NULL
);
2506 surface_load_location(surface
, context
, texture
->resource
.map_binding
);
2507 wined3d_texture_invalidate_location(texture
, sub_resource_idx
, ~texture
->resource
.map_binding
);
2510 hr
= wined3d_surface_create_dc(surface
);
2512 context_release(context
);
2514 return WINED3DERR_INVALIDCALL
;
2516 if (!(texture
->flags
& WINED3D_TEXTURE_GET_DC_LENIENT
))
2517 texture
->flags
|= WINED3D_TEXTURE_DC_IN_USE
;
2518 ++texture
->resource
.map_count
;
2519 ++sub_resource
->map_count
;
2522 TRACE("Returning dc %p.\n", *dc
);
2527 HRESULT CDECL
wined3d_texture_release_dc(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
, HDC dc
)
2529 struct wined3d_device
*device
= texture
->resource
.device
;
2530 struct wined3d_texture_sub_resource
*sub_resource
;
2531 struct wined3d_surface
*surface
;
2533 TRACE("texture %p, sub_resource_idx %u, dc %p.\n", texture
, sub_resource_idx
, dc
);
2535 if (!(sub_resource
= wined3d_texture_get_sub_resource(texture
, sub_resource_idx
)))
2536 return WINED3DERR_INVALIDCALL
;
2538 if (texture
->resource
.type
!= WINED3D_RTYPE_TEXTURE_2D
)
2540 WARN("Not supported on %s resources.\n", debug_d3dresourcetype(texture
->resource
.type
));
2541 return WINED3DERR_INVALIDCALL
;
2544 surface
= sub_resource
->u
.surface
;
2546 if (!(texture
->flags
& (WINED3D_TEXTURE_GET_DC_LENIENT
| WINED3D_TEXTURE_DC_IN_USE
)))
2547 return WINED3DERR_INVALIDCALL
;
2549 if (surface
->dc
!= dc
)
2551 WARN("Application tries to release invalid DC %p, surface DC is %p.\n", dc
, surface
->dc
);
2552 return WINED3DERR_INVALIDCALL
;
2555 if (!(texture
->resource
.usage
& WINED3DUSAGE_OWNDC
) && !(device
->wined3d
->flags
& WINED3D_NO3D
))
2556 wined3d_surface_destroy_dc(surface
);
2558 --sub_resource
->map_count
;
2559 if (!--texture
->resource
.map_count
&& texture
->update_map_binding
)
2560 wined3d_texture_update_map_binding(texture
);
2561 if (!(texture
->flags
& WINED3D_TEXTURE_GET_DC_LENIENT
))
2562 texture
->flags
&= ~WINED3D_TEXTURE_DC_IN_USE
;