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 static BOOL
wined3d_texture_use_immutable_storage(const struct wined3d_texture
*texture
,
43 const struct wined3d_gl_info
*gl_info
)
45 /* We don't expect to create texture views for textures with height-scaled formats.
46 * Besides, ARB_texture_storage doesn't allow specifying exact sizes for all levels. */
47 return gl_info
->supported
[ARB_TEXTURE_STORAGE
]
48 && !(texture
->resource
.format_flags
& WINED3DFMT_FLAG_HEIGHT_SCALE
);
51 GLenum
wined3d_texture_get_gl_buffer(const struct wined3d_texture
*texture
)
53 const struct wined3d_swapchain
*swapchain
= texture
->swapchain
;
55 TRACE("texture %p.\n", texture
);
59 ERR("Texture %p is not part of a swapchain.\n", texture
);
63 if (swapchain
->back_buffers
&& swapchain
->back_buffers
[0] == texture
)
65 if (swapchain
->render_to_fbo
)
67 TRACE("Returning GL_COLOR_ATTACHMENT0.\n");
68 return GL_COLOR_ATTACHMENT0
;
70 TRACE("Returning GL_BACK.\n");
73 else if (texture
== swapchain
->front_buffer
)
75 TRACE("Returning GL_FRONT.\n");
79 FIXME("Higher back buffer, returning GL_BACK.\n");
83 static DWORD
wined3d_resource_access_from_location(DWORD location
)
87 case WINED3D_LOCATION_DISCARDED
:
90 case WINED3D_LOCATION_SYSMEM
:
91 return WINED3D_RESOURCE_ACCESS_CPU
;
93 case WINED3D_LOCATION_BUFFER
:
94 case WINED3D_LOCATION_TEXTURE_RGB
:
95 case WINED3D_LOCATION_TEXTURE_SRGB
:
96 return WINED3D_RESOURCE_ACCESS_GPU
;
99 FIXME("Unhandled location %#x.\n", location
);
104 static void wined3d_texture_evict_sysmem(struct wined3d_texture
*texture
)
106 struct wined3d_texture_sub_resource
*sub_resource
;
107 unsigned int i
, sub_count
;
109 if (texture
->flags
& (WINED3D_TEXTURE_CONVERTED
| WINED3D_TEXTURE_PIN_SYSMEM
)
110 || texture
->download_count
> WINED3D_TEXTURE_DYNAMIC_MAP_THRESHOLD
)
112 TRACE("Not evicting system memory for texture %p.\n", texture
);
116 TRACE("Evicting system memory for texture %p.\n", texture
);
118 sub_count
= texture
->level_count
* texture
->layer_count
;
119 for (i
= 0; i
< sub_count
; ++i
)
121 sub_resource
= &texture
->sub_resources
[i
];
122 if (sub_resource
->locations
== WINED3D_LOCATION_SYSMEM
)
123 ERR("WINED3D_LOCATION_SYSMEM is the only location for sub-resource %u of texture %p.\n",
125 sub_resource
->locations
&= ~WINED3D_LOCATION_SYSMEM
;
127 wined3d_resource_free_sysmem(&texture
->resource
);
130 void wined3d_texture_validate_location(struct wined3d_texture
*texture
,
131 unsigned int sub_resource_idx
, DWORD location
)
133 struct wined3d_texture_sub_resource
*sub_resource
;
134 DWORD previous_locations
;
136 TRACE("texture %p, sub_resource_idx %u, location %s.\n",
137 texture
, sub_resource_idx
, wined3d_debug_location(location
));
139 sub_resource
= &texture
->sub_resources
[sub_resource_idx
];
140 previous_locations
= sub_resource
->locations
;
141 sub_resource
->locations
|= location
;
142 if (previous_locations
== WINED3D_LOCATION_SYSMEM
&& location
!= WINED3D_LOCATION_SYSMEM
143 && !--texture
->sysmem_count
)
144 wined3d_texture_evict_sysmem(texture
);
146 TRACE("New locations flags are %s.\n", wined3d_debug_location(sub_resource
->locations
));
149 static void wined3d_texture_set_dirty(struct wined3d_texture
*texture
)
151 texture
->flags
&= ~(WINED3D_TEXTURE_RGB_VALID
| WINED3D_TEXTURE_SRGB_VALID
);
154 void wined3d_texture_invalidate_location(struct wined3d_texture
*texture
,
155 unsigned int sub_resource_idx
, DWORD location
)
157 struct wined3d_texture_sub_resource
*sub_resource
;
159 TRACE("texture %p, sub_resource_idx %u, location %s.\n",
160 texture
, sub_resource_idx
, wined3d_debug_location(location
));
162 if (location
& (WINED3D_LOCATION_TEXTURE_RGB
| WINED3D_LOCATION_TEXTURE_SRGB
))
163 wined3d_texture_set_dirty(texture
);
165 sub_resource
= &texture
->sub_resources
[sub_resource_idx
];
166 sub_resource
->locations
&= ~location
;
167 if (sub_resource
->locations
== WINED3D_LOCATION_SYSMEM
)
168 ++texture
->sysmem_count
;
170 TRACE("New locations flags are %s.\n", wined3d_debug_location(sub_resource
->locations
));
172 if (!sub_resource
->locations
)
173 ERR("Sub-resource %u of texture %p does not have any up to date location.\n",
174 sub_resource_idx
, texture
);
177 /* Context activation is done by the caller. Context may be NULL in
178 * WINED3D_NO3D mode. */
179 BOOL
wined3d_texture_load_location(struct wined3d_texture
*texture
,
180 unsigned int sub_resource_idx
, struct wined3d_context
*context
, DWORD location
)
182 return texture
->texture_ops
->texture_load_location(texture
, sub_resource_idx
, context
, location
);
185 /* Context activation is done by the caller. */
186 void *wined3d_texture_map_bo_address(const struct wined3d_bo_address
*data
, size_t size
,
187 const struct wined3d_gl_info
*gl_info
, GLenum binding
, DWORD flags
)
191 if (!data
->buffer_object
)
194 GL_EXTCALL(glBindBuffer(binding
, data
->buffer_object
));
196 if (gl_info
->supported
[ARB_MAP_BUFFER_RANGE
])
198 GLbitfield map_flags
= wined3d_resource_gl_map_flags(flags
) & ~GL_MAP_FLUSH_EXPLICIT_BIT
;
199 memory
= GL_EXTCALL(glMapBufferRange(binding
, (INT_PTR
)data
->addr
, size
, map_flags
));
203 memory
= GL_EXTCALL(glMapBuffer(binding
, wined3d_resource_gl_legacy_map_flags(flags
)));
204 memory
+= (INT_PTR
)data
->addr
;
207 GL_EXTCALL(glBindBuffer(binding
, 0));
208 checkGLcall("Map buffer object");
213 /* Context activation is done by the caller. */
214 void wined3d_texture_unmap_bo_address(const struct wined3d_bo_address
*data
,
215 const struct wined3d_gl_info
*gl_info
, GLenum binding
)
217 if (!data
->buffer_object
)
220 GL_EXTCALL(glBindBuffer(binding
, data
->buffer_object
));
221 GL_EXTCALL(glUnmapBuffer(binding
));
222 GL_EXTCALL(glBindBuffer(binding
, 0));
223 checkGLcall("Unmap buffer object");
226 void wined3d_texture_get_memory(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
,
227 struct wined3d_bo_address
*data
, DWORD locations
)
229 struct wined3d_texture_sub_resource
*sub_resource
;
231 TRACE("texture %p, sub_resource_idx %u, data %p, locations %s.\n",
232 texture
, sub_resource_idx
, data
, wined3d_debug_location(locations
));
234 sub_resource
= &texture
->sub_resources
[sub_resource_idx
];
235 if (locations
& WINED3D_LOCATION_BUFFER
)
238 data
->buffer_object
= sub_resource
->buffer_object
;
241 if (locations
& WINED3D_LOCATION_USER_MEMORY
)
243 data
->addr
= texture
->user_memory
;
244 data
->buffer_object
= 0;
247 if (locations
& WINED3D_LOCATION_SYSMEM
)
249 data
->addr
= texture
->resource
.heap_memory
;
250 data
->addr
+= sub_resource
->offset
;
251 data
->buffer_object
= 0;
255 ERR("Unexpected locations %s.\n", wined3d_debug_location(locations
));
257 data
->buffer_object
= 0;
260 static HRESULT
wined3d_texture_init(struct wined3d_texture
*texture
, const struct wined3d_texture_ops
*texture_ops
,
261 UINT layer_count
, UINT level_count
, const struct wined3d_resource_desc
*desc
, DWORD flags
,
262 struct wined3d_device
*device
, void *parent
, const struct wined3d_parent_ops
*parent_ops
,
263 const struct wined3d_resource_ops
*resource_ops
)
265 unsigned int i
, j
, size
, offset
= 0;
266 const struct wined3d_format
*format
;
269 TRACE("texture %p, texture_ops %p, layer_count %u, level_count %u, resource_type %s, format %s, "
270 "multisample_type %#x, multisample_quality %#x, usage %s, pool %s, width %u, height %u, depth %u, "
271 "flags %#x, device %p, parent %p, parent_ops %p, resource_ops %p.\n",
272 texture
, texture_ops
, layer_count
, level_count
, debug_d3dresourcetype(desc
->resource_type
),
273 debug_d3dformat(desc
->format
), desc
->multisample_type
, desc
->multisample_quality
,
274 debug_d3dusage(desc
->usage
), debug_d3dpool(desc
->pool
), desc
->width
, desc
->height
, desc
->depth
,
275 flags
, device
, parent
, parent_ops
, resource_ops
);
277 if (!desc
->width
|| !desc
->height
|| !desc
->depth
)
278 return WINED3DERR_INVALIDCALL
;
280 format
= wined3d_get_format(&device
->adapter
->gl_info
, desc
->format
, desc
->usage
);
282 for (i
= 0; i
< layer_count
; ++i
)
284 for (j
= 0; j
< level_count
; ++j
)
286 unsigned int idx
= i
* level_count
+ j
;
288 size
= wined3d_format_calculate_size(format
, device
->surface_alignment
,
289 max(1, desc
->width
>> j
), max(1, desc
->height
>> j
), max(1, desc
->depth
>> j
));
290 texture
->sub_resources
[idx
].offset
= offset
;
291 texture
->sub_resources
[idx
].size
= size
;
294 offset
= (offset
+ (RESOURCE_ALIGNMENT
- 1)) & ~(RESOURCE_ALIGNMENT
- 1);
298 return WINED3DERR_INVALIDCALL
;
300 if (FAILED(hr
= resource_init(&texture
->resource
, device
, desc
->resource_type
, format
,
301 desc
->multisample_type
, desc
->multisample_quality
, desc
->usage
, desc
->pool
,
302 desc
->width
, desc
->height
, desc
->depth
, offset
, parent
, parent_ops
, resource_ops
)))
304 static unsigned int once
;
306 /* DXTn 3D textures are not supported. Do not write the ERR for them. */
307 if ((desc
->format
== WINED3DFMT_DXT1
|| desc
->format
== WINED3DFMT_DXT2
|| desc
->format
== WINED3DFMT_DXT3
308 || desc
->format
== WINED3DFMT_DXT4
|| desc
->format
== WINED3DFMT_DXT5
)
309 && !(format
->flags
[WINED3D_GL_RES_TYPE_TEX_2D
] & WINED3DFMT_FLAG_TEXTURE
)
310 && desc
->resource_type
!= WINED3D_RTYPE_TEXTURE_3D
&& !once
++)
311 ERR_(winediag
)("The application tried to create a DXTn texture, but the driver does not support them.\n");
313 WARN("Failed to initialize resource, returning %#x\n", hr
);
316 wined3d_resource_update_draw_binding(&texture
->resource
);
317 if ((flags
& WINED3D_TEXTURE_CREATE_MAPPABLE
) || desc
->format
== WINED3DFMT_D16_LOCKABLE
)
318 texture
->resource
.access_flags
|= WINED3D_RESOURCE_ACCESS_CPU
;
320 texture
->texture_ops
= texture_ops
;
322 texture
->layer_count
= layer_count
;
323 texture
->level_count
= level_count
;
324 texture
->filter_type
= (desc
->usage
& WINED3DUSAGE_AUTOGENMIPMAP
) ? WINED3D_TEXF_LINEAR
: WINED3D_TEXF_NONE
;
326 texture
->flags
|= WINED3D_TEXTURE_POW2_MAT_IDENT
| WINED3D_TEXTURE_NORMALIZED_COORDS
;
327 if (flags
& WINED3D_TEXTURE_CREATE_GET_DC_LENIENT
)
328 texture
->flags
|= WINED3D_TEXTURE_PIN_SYSMEM
| WINED3D_TEXTURE_GET_DC_LENIENT
;
329 if (flags
& (WINED3D_TEXTURE_CREATE_GET_DC
| WINED3D_TEXTURE_CREATE_GET_DC_LENIENT
))
330 texture
->flags
|= WINED3D_TEXTURE_GET_DC
;
331 if (flags
& WINED3D_TEXTURE_CREATE_DISCARD
)
332 texture
->flags
|= WINED3D_TEXTURE_DISCARD
;
337 /* Context activation is done by the caller. */
338 static void wined3d_texture_remove_buffer_object(struct wined3d_texture
*texture
,
339 unsigned int sub_resource_idx
, const struct wined3d_gl_info
*gl_info
)
341 GLuint
*buffer_object
;
343 buffer_object
= &texture
->sub_resources
[sub_resource_idx
].buffer_object
;
344 GL_EXTCALL(glDeleteBuffers(1, buffer_object
));
345 checkGLcall("glDeleteBuffers");
346 wined3d_texture_invalidate_location(texture
, sub_resource_idx
, WINED3D_LOCATION_BUFFER
);
349 TRACE("Deleted buffer object %u for texture %p, sub-resource %u.\n",
350 *buffer_object
, texture
, sub_resource_idx
);
353 static void wined3d_texture_update_map_binding(struct wined3d_texture
*texture
)
355 unsigned int sub_count
= texture
->level_count
* texture
->layer_count
;
356 const struct wined3d_device
*device
= texture
->resource
.device
;
357 DWORD map_binding
= texture
->update_map_binding
;
358 struct wined3d_context
*context
= NULL
;
361 if (device
->d3d_initialized
)
362 context
= context_acquire(device
, NULL
);
364 for (i
= 0; i
< sub_count
; ++i
)
366 if (texture
->sub_resources
[i
].locations
== texture
->resource
.map_binding
367 && !wined3d_texture_load_location(texture
, i
, context
, map_binding
))
368 ERR("Failed to load location %s.\n", wined3d_debug_location(map_binding
));
369 if (texture
->resource
.map_binding
== WINED3D_LOCATION_BUFFER
)
370 wined3d_texture_remove_buffer_object(texture
, i
, context
->gl_info
);
374 context_release(context
);
376 texture
->resource
.map_binding
= map_binding
;
377 texture
->update_map_binding
= 0;
380 void wined3d_texture_set_map_binding(struct wined3d_texture
*texture
, DWORD map_binding
)
382 texture
->update_map_binding
= map_binding
;
383 if (!texture
->resource
.map_count
)
384 wined3d_texture_update_map_binding(texture
);
387 /* A GL context is provided by the caller */
388 static void gltexture_delete(struct wined3d_device
*device
, const struct wined3d_gl_info
*gl_info
,
389 struct gl_texture
*tex
)
391 context_gl_resource_released(device
, tex
->name
, FALSE
);
392 gl_info
->gl_ops
.gl
.p_glDeleteTextures(1, &tex
->name
);
396 /* Context activation is done by the caller. */
397 /* The caller is responsible for binding the correct texture. */
398 static void wined3d_texture_allocate_gl_mutable_storage(struct wined3d_texture
*texture
,
399 GLenum gl_internal_format
, const struct wined3d_format
*format
,
400 const struct wined3d_gl_info
*gl_info
)
402 unsigned int i
, sub_call_count
;
404 sub_call_count
= texture
->level_count
;
405 if (texture
->target
!= GL_TEXTURE_2D_ARRAY
)
406 sub_call_count
*= texture
->layer_count
;
408 for (i
= 0; i
< sub_call_count
; ++i
)
410 struct wined3d_surface
*surface
= texture
->sub_resources
[i
].u
.surface
;
411 GLsizei width
, height
;
413 width
= wined3d_texture_get_level_pow2_width(texture
, surface
->texture_level
);
414 height
= wined3d_texture_get_level_pow2_height(texture
, surface
->texture_level
);
415 if (texture
->resource
.format_flags
& WINED3DFMT_FLAG_HEIGHT_SCALE
)
417 height
*= format
->height_scale
.numerator
;
418 height
/= format
->height_scale
.denominator
;
421 TRACE("surface %p, target %#x, level %u, width %u, height %u.\n",
422 surface
, surface
->texture_target
, surface
->texture_level
, width
, height
);
424 if (texture
->target
== GL_TEXTURE_2D_ARRAY
)
426 GL_EXTCALL(glTexImage3D(surface
->texture_target
, surface
->texture_level
,
427 gl_internal_format
, width
, height
, texture
->layer_count
, 0,
428 format
->glFormat
, format
->glType
, NULL
));
429 checkGLcall("glTexImage3D");
433 gl_info
->gl_ops
.gl
.p_glTexImage2D(surface
->texture_target
, surface
->texture_level
,
434 gl_internal_format
, width
, height
, 0, format
->glFormat
, format
->glType
, NULL
);
435 checkGLcall("glTexImage2D");
440 /* Context activation is done by the caller. */
441 /* The caller is responsible for binding the correct texture. */
442 static void wined3d_texture_allocate_gl_immutable_storage(struct wined3d_texture
*texture
,
443 GLenum gl_internal_format
, const struct wined3d_gl_info
*gl_info
)
445 GLsizei width
= wined3d_texture_get_level_pow2_width(texture
, 0);
446 GLsizei height
= wined3d_texture_get_level_pow2_height(texture
, 0);
448 if (texture
->target
== GL_TEXTURE_2D_ARRAY
)
450 GL_EXTCALL(glTexStorage3D(texture
->target
, texture
->level_count
, gl_internal_format
,
451 width
, height
, texture
->layer_count
));
452 checkGLcall("glTexStorage3D");
456 GL_EXTCALL(glTexStorage2D(texture
->target
, texture
->level_count
, gl_internal_format
,
458 checkGLcall("glTexStorage2D");
462 static void wined3d_texture_unload_gl_texture(struct wined3d_texture
*texture
)
464 struct wined3d_device
*device
= texture
->resource
.device
;
465 const struct wined3d_gl_info
*gl_info
= NULL
;
466 struct wined3d_context
*context
= NULL
;
468 if (texture
->texture_rgb
.name
|| texture
->texture_srgb
.name
469 || texture
->rb_multisample
|| texture
->rb_resolved
)
471 context
= context_acquire(device
, NULL
);
472 gl_info
= context
->gl_info
;
475 if (texture
->texture_rgb
.name
)
476 gltexture_delete(device
, context
->gl_info
, &texture
->texture_rgb
);
478 if (texture
->texture_srgb
.name
)
479 gltexture_delete(device
, context
->gl_info
, &texture
->texture_srgb
);
481 if (texture
->rb_multisample
)
483 TRACE("Deleting multisample renderbuffer %u.\n", texture
->rb_multisample
);
484 context_gl_resource_released(device
, texture
->rb_multisample
, TRUE
);
485 gl_info
->fbo_ops
.glDeleteRenderbuffers(1, &texture
->rb_multisample
);
486 texture
->rb_multisample
= 0;
489 if (texture
->rb_resolved
)
491 TRACE("Deleting resolved renderbuffer %u.\n", texture
->rb_resolved
);
492 context_gl_resource_released(device
, texture
->rb_resolved
, TRUE
);
493 gl_info
->fbo_ops
.glDeleteRenderbuffers(1, &texture
->rb_resolved
);
494 texture
->rb_resolved
= 0;
497 if (context
) context_release(context
);
499 wined3d_texture_set_dirty(texture
);
501 resource_unload(&texture
->resource
);
504 static void wined3d_texture_sub_resources_destroyed(struct wined3d_texture
*texture
)
506 unsigned int sub_count
= texture
->level_count
* texture
->layer_count
;
507 struct wined3d_texture_sub_resource
*sub_resource
;
510 for (i
= 0; i
< sub_count
; ++i
)
512 sub_resource
= &texture
->sub_resources
[i
];
513 if (sub_resource
->parent
)
515 TRACE("sub-resource %u.\n", i
);
516 sub_resource
->parent_ops
->wined3d_object_destroyed(sub_resource
->parent
);
517 sub_resource
->parent
= NULL
;
522 static void wined3d_texture_cleanup(struct wined3d_texture
*texture
)
524 unsigned int sub_count
= texture
->level_count
* texture
->layer_count
;
525 struct wined3d_device
*device
= texture
->resource
.device
;
526 struct wined3d_context
*context
= NULL
;
527 const struct wined3d_gl_info
*gl_info
;
528 GLuint buffer_object
;
531 TRACE("texture %p.\n", texture
);
533 for (i
= 0; i
< sub_count
; ++i
)
535 if (!(buffer_object
= texture
->sub_resources
[i
].buffer_object
))
538 TRACE("Deleting buffer object %u.\n", buffer_object
);
540 /* We may not be able to get a context in wined3d_texture_cleanup() in
541 * general, but if a buffer object was previously created we can. */
544 context
= context_acquire(device
, NULL
);
545 gl_info
= context
->gl_info
;
548 GL_EXTCALL(glDeleteBuffers(1, &buffer_object
));
551 context_release(context
);
553 texture
->texture_ops
->texture_cleanup_sub_resources(texture
);
554 wined3d_texture_unload_gl_texture(texture
);
557 void wined3d_texture_set_swapchain(struct wined3d_texture
*texture
, struct wined3d_swapchain
*swapchain
)
559 texture
->swapchain
= swapchain
;
560 wined3d_resource_update_draw_binding(&texture
->resource
);
563 /* Context activation is done by the caller. */
564 void wined3d_texture_bind(struct wined3d_texture
*texture
,
565 struct wined3d_context
*context
, BOOL srgb
)
567 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
568 const struct wined3d_format
*format
= texture
->resource
.format
;
569 const struct color_fixup_desc fixup
= format
->color_fixup
;
570 struct gl_texture
*gl_tex
;
573 TRACE("texture %p, context %p, srgb %#x.\n", texture
, context
, srgb
);
575 if (!needs_separate_srgb_gl_texture(context
))
578 /* sRGB mode cache for preload() calls outside drawprim. */
580 texture
->flags
|= WINED3D_TEXTURE_IS_SRGB
;
582 texture
->flags
&= ~WINED3D_TEXTURE_IS_SRGB
;
584 gl_tex
= wined3d_texture_get_gl_texture(texture
, srgb
);
585 target
= texture
->target
;
589 context_bind_texture(context
, target
, gl_tex
->name
);
593 gl_info
->gl_ops
.gl
.p_glGenTextures(1, &gl_tex
->name
);
594 checkGLcall("glGenTextures");
595 TRACE("Generated texture %d.\n", gl_tex
->name
);
599 ERR("Failed to generate a texture name.\n");
603 /* Initialise the state of the texture object to the OpenGL defaults, not
604 * the wined3d defaults. */
605 gl_tex
->sampler_desc
.address_u
= WINED3D_TADDRESS_WRAP
;
606 gl_tex
->sampler_desc
.address_v
= WINED3D_TADDRESS_WRAP
;
607 gl_tex
->sampler_desc
.address_w
= WINED3D_TADDRESS_WRAP
;
608 memset(gl_tex
->sampler_desc
.border_color
, 0, sizeof(gl_tex
->sampler_desc
.border_color
));
609 gl_tex
->sampler_desc
.mag_filter
= WINED3D_TEXF_LINEAR
;
610 gl_tex
->sampler_desc
.min_filter
= WINED3D_TEXF_POINT
; /* GL_NEAREST_MIPMAP_LINEAR */
611 gl_tex
->sampler_desc
.mip_filter
= WINED3D_TEXF_LINEAR
; /* GL_NEAREST_MIPMAP_LINEAR */
612 gl_tex
->sampler_desc
.lod_bias
= 0.0f
;
613 gl_tex
->sampler_desc
.min_lod
= -1000.0f
;
614 gl_tex
->sampler_desc
.max_lod
= 1000.0f
;
615 gl_tex
->sampler_desc
.max_anisotropy
= 1;
616 gl_tex
->sampler_desc
.compare
= FALSE
;
617 gl_tex
->sampler_desc
.comparison_func
= WINED3D_CMP_LESSEQUAL
;
618 if (context
->gl_info
->supported
[EXT_TEXTURE_SRGB_DECODE
])
619 gl_tex
->sampler_desc
.srgb_decode
= TRUE
;
621 gl_tex
->sampler_desc
.srgb_decode
= srgb
;
622 gl_tex
->base_level
= 0;
623 wined3d_texture_set_dirty(texture
);
625 context_bind_texture(context
, target
, gl_tex
->name
);
627 if (texture
->resource
.usage
& WINED3DUSAGE_AUTOGENMIPMAP
)
629 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_GENERATE_MIPMAP_SGIS
, GL_TRUE
);
630 checkGLcall("glTexParameteri(target, GL_GENERATE_MIPMAP_SGIS, GL_TRUE)");
633 /* For a new texture we have to set the texture levels after binding the
634 * texture. Beware that texture rectangles do not support mipmapping, but
635 * set the maxmiplevel if we're relying on the partial
636 * GL_ARB_texture_non_power_of_two emulation with texture rectangles.
637 * (I.e., do not care about cond_np2 here, just look for
638 * GL_TEXTURE_RECTANGLE_ARB.) */
639 if (target
!= GL_TEXTURE_RECTANGLE_ARB
)
641 TRACE("Setting GL_TEXTURE_MAX_LEVEL to %u.\n", texture
->level_count
- 1);
642 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, texture
->level_count
- 1);
643 checkGLcall("glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, texture->level_count)");
646 if (target
== GL_TEXTURE_CUBE_MAP_ARB
)
648 /* Cubemaps are always set to clamp, regardless of the sampler state. */
649 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
650 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
651 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_R
, GL_CLAMP_TO_EDGE
);
654 if (texture
->flags
& WINED3D_TEXTURE_COND_NP2
)
656 /* Conditinal non power of two textures use a different clamping
657 * default. If we're using the GL_WINE_normalized_texrect partial
658 * driver emulation, we're dealing with a GL_TEXTURE_2D texture which
659 * has the address mode set to repeat - something that prevents us
660 * from hitting the accelerated codepath. Thus manually set the GL
661 * state. The same applies to filtering. Even if the texture has only
662 * one mip level, the default LINEAR_MIPMAP_LINEAR filter causes a SW
663 * fallback on macos. */
664 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
665 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
666 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
667 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
668 checkGLcall("glTexParameteri");
669 gl_tex
->sampler_desc
.address_u
= WINED3D_TADDRESS_CLAMP
;
670 gl_tex
->sampler_desc
.address_v
= WINED3D_TADDRESS_CLAMP
;
671 gl_tex
->sampler_desc
.mag_filter
= WINED3D_TEXF_POINT
;
672 gl_tex
->sampler_desc
.min_filter
= WINED3D_TEXF_POINT
;
673 gl_tex
->sampler_desc
.mip_filter
= WINED3D_TEXF_NONE
;
676 if (gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
] && gl_info
->supported
[ARB_DEPTH_TEXTURE
])
678 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_DEPTH_TEXTURE_MODE_ARB
, GL_INTENSITY
);
679 checkGLcall("glTexParameteri(GL_DEPTH_TEXTURE_MODE_ARB, GL_INTENSITY)");
682 if (!is_identity_fixup(fixup
) && can_use_texture_swizzle(gl_info
, format
))
684 static const GLenum swizzle_source
[] =
686 GL_ZERO
, /* CHANNEL_SOURCE_ZERO */
687 GL_ONE
, /* CHANNEL_SOURCE_ONE */
688 GL_RED
, /* CHANNEL_SOURCE_X */
689 GL_GREEN
, /* CHANNEL_SOURCE_Y */
690 GL_BLUE
, /* CHANNEL_SOURCE_Z */
691 GL_ALPHA
, /* CHANNEL_SOURCE_W */
699 swizzle
.x
= swizzle_source
[fixup
.x_source
];
700 swizzle
.y
= swizzle_source
[fixup
.y_source
];
701 swizzle
.z
= swizzle_source
[fixup
.z_source
];
702 swizzle
.w
= swizzle_source
[fixup
.w_source
];
703 gl_info
->gl_ops
.gl
.p_glTexParameteriv(target
, GL_TEXTURE_SWIZZLE_RGBA
, &swizzle
.x
);
704 checkGLcall("glTexParameteriv(GL_TEXTURE_SWIZZLE_RGBA)");
708 /* Context activation is done by the caller. */
709 void wined3d_texture_bind_and_dirtify(struct wined3d_texture
*texture
,
710 struct wined3d_context
*context
, BOOL srgb
)
712 DWORD active_sampler
;
714 /* We don't need a specific texture unit, but after binding the texture
715 * the current unit is dirty. Read the unit back instead of switching to
716 * 0, this avoids messing around with the state manager's GL states. The
717 * current texture unit should always be a valid one.
719 * To be more specific, this is tricky because we can implicitly be
720 * called from sampler() in state.c. This means we can't touch anything
721 * other than whatever happens to be the currently active texture, or we
722 * would risk marking already applied sampler states dirty again. */
723 active_sampler
= context
->rev_tex_unit_map
[context
->active_texture
];
724 if (active_sampler
!= WINED3D_UNMAPPED_STAGE
)
725 context_invalidate_state(context
, STATE_SAMPLER(active_sampler
));
726 /* FIXME: Ideally we'd only do this when touching a binding that's used by
728 context_invalidate_state(context
, STATE_SHADER_RESOURCE_BINDING
);
730 wined3d_texture_bind(texture
, context
, srgb
);
733 /* Context activation is done by the caller (state handler). */
734 /* This function relies on the correct texture being bound and loaded. */
735 void wined3d_texture_apply_sampler_desc(struct wined3d_texture
*texture
,
736 const struct wined3d_sampler_desc
*sampler_desc
, const struct wined3d_context
*context
)
738 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
739 GLenum target
= texture
->target
;
740 struct gl_texture
*gl_tex
;
743 TRACE("texture %p, sampler_desc %p, context %p.\n", texture
, sampler_desc
, context
);
745 gl_tex
= wined3d_texture_get_gl_texture(texture
, texture
->flags
& WINED3D_TEXTURE_IS_SRGB
);
747 state
= sampler_desc
->address_u
;
748 if (state
!= gl_tex
->sampler_desc
.address_u
)
750 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_S
,
751 gl_info
->wrap_lookup
[state
- WINED3D_TADDRESS_WRAP
]);
752 gl_tex
->sampler_desc
.address_u
= state
;
755 state
= sampler_desc
->address_v
;
756 if (state
!= gl_tex
->sampler_desc
.address_v
)
758 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_T
,
759 gl_info
->wrap_lookup
[state
- WINED3D_TADDRESS_WRAP
]);
760 gl_tex
->sampler_desc
.address_v
= state
;
763 state
= sampler_desc
->address_w
;
764 if (state
!= gl_tex
->sampler_desc
.address_w
)
766 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_R
,
767 gl_info
->wrap_lookup
[state
- WINED3D_TADDRESS_WRAP
]);
768 gl_tex
->sampler_desc
.address_w
= state
;
771 if (memcmp(gl_tex
->sampler_desc
.border_color
, sampler_desc
->border_color
,
772 sizeof(gl_tex
->sampler_desc
.border_color
)))
774 gl_info
->gl_ops
.gl
.p_glTexParameterfv(target
, GL_TEXTURE_BORDER_COLOR
, &sampler_desc
->border_color
[0]);
775 memcpy(gl_tex
->sampler_desc
.border_color
, sampler_desc
->border_color
,
776 sizeof(gl_tex
->sampler_desc
.border_color
));
779 state
= sampler_desc
->mag_filter
;
780 if (state
!= gl_tex
->sampler_desc
.mag_filter
)
782 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MAG_FILTER
, wined3d_gl_mag_filter(state
));
783 gl_tex
->sampler_desc
.mag_filter
= state
;
786 if (sampler_desc
->min_filter
!= gl_tex
->sampler_desc
.min_filter
787 || sampler_desc
->mip_filter
!= gl_tex
->sampler_desc
.mip_filter
)
789 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MIN_FILTER
,
790 wined3d_gl_min_mip_filter(sampler_desc
->min_filter
, sampler_desc
->mip_filter
));
791 gl_tex
->sampler_desc
.min_filter
= sampler_desc
->min_filter
;
792 gl_tex
->sampler_desc
.mip_filter
= sampler_desc
->mip_filter
;
795 state
= sampler_desc
->max_anisotropy
;
796 if (state
!= gl_tex
->sampler_desc
.max_anisotropy
)
798 if (gl_info
->supported
[EXT_TEXTURE_FILTER_ANISOTROPIC
])
799 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MAX_ANISOTROPY_EXT
, state
);
801 WARN("Anisotropic filtering not supported.\n");
802 gl_tex
->sampler_desc
.max_anisotropy
= state
;
805 if (!sampler_desc
->srgb_decode
!= !gl_tex
->sampler_desc
.srgb_decode
806 && (context
->d3d_info
->wined3d_creation_flags
& WINED3D_SRGB_READ_WRITE_CONTROL
)
807 && gl_info
->supported
[EXT_TEXTURE_SRGB_DECODE
])
809 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_SRGB_DECODE_EXT
,
810 sampler_desc
->srgb_decode
? GL_DECODE_EXT
: GL_SKIP_DECODE_EXT
);
811 gl_tex
->sampler_desc
.srgb_decode
= sampler_desc
->srgb_decode
;
814 if (!sampler_desc
->compare
!= !gl_tex
->sampler_desc
.compare
)
816 if (sampler_desc
->compare
)
817 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_COMPARE_MODE_ARB
, GL_COMPARE_R_TO_TEXTURE_ARB
);
819 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_COMPARE_MODE_ARB
, GL_NONE
);
820 gl_tex
->sampler_desc
.compare
= sampler_desc
->compare
;
823 checkGLcall("Texture parameter application");
825 if (gl_info
->supported
[EXT_TEXTURE_LOD_BIAS
])
827 gl_info
->gl_ops
.gl
.p_glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT
,
828 GL_TEXTURE_LOD_BIAS_EXT
, sampler_desc
->lod_bias
);
829 checkGLcall("glTexEnvf(GL_TEXTURE_LOD_BIAS_EXT, ...)");
833 ULONG CDECL
wined3d_texture_incref(struct wined3d_texture
*texture
)
837 TRACE("texture %p, swapchain %p.\n", texture
, texture
->swapchain
);
839 if (texture
->swapchain
)
840 return wined3d_swapchain_incref(texture
->swapchain
);
842 refcount
= InterlockedIncrement(&texture
->resource
.ref
);
843 TRACE("%p increasing refcount to %u.\n", texture
, refcount
);
848 static void wined3d_texture_cleanup_sync(struct wined3d_texture
*texture
)
850 wined3d_texture_sub_resources_destroyed(texture
);
851 resource_cleanup(&texture
->resource
);
852 wined3d_resource_wait_idle(&texture
->resource
);
853 wined3d_texture_cleanup(texture
);
856 static void wined3d_texture_destroy_object(void *object
)
858 wined3d_texture_cleanup(object
);
859 HeapFree(GetProcessHeap(), 0, object
);
862 ULONG CDECL
wined3d_texture_decref(struct wined3d_texture
*texture
)
866 TRACE("texture %p, swapchain %p.\n", texture
, texture
->swapchain
);
868 if (texture
->swapchain
)
869 return wined3d_swapchain_decref(texture
->swapchain
);
871 refcount
= InterlockedDecrement(&texture
->resource
.ref
);
872 TRACE("%p decreasing refcount to %u.\n", texture
, refcount
);
876 /* Wait for the texture to become idle if it's using user memory,
877 * since the application is allowed to free that memory once the
878 * texture is destroyed. Note that this implies that
879 * wined3d_texture_destroy_object() can't access that memory either. */
880 if (texture
->user_memory
)
881 wined3d_resource_wait_idle(&texture
->resource
);
882 wined3d_texture_sub_resources_destroyed(texture
);
883 texture
->resource
.parent_ops
->wined3d_object_destroyed(texture
->resource
.parent
);
884 resource_cleanup(&texture
->resource
);
885 wined3d_cs_emit_destroy_object(texture
->resource
.device
->cs
, wined3d_texture_destroy_object
, texture
);
891 struct wined3d_resource
* CDECL
wined3d_texture_get_resource(struct wined3d_texture
*texture
)
893 TRACE("texture %p.\n", texture
);
895 return &texture
->resource
;
898 static BOOL
color_key_equal(const struct wined3d_color_key
*c1
, struct wined3d_color_key
*c2
)
900 return c1
->color_space_low_value
== c2
->color_space_low_value
901 && c1
->color_space_high_value
== c2
->color_space_high_value
;
904 /* Context activation is done by the caller */
905 void wined3d_texture_load(struct wined3d_texture
*texture
,
906 struct wined3d_context
*context
, BOOL srgb
)
908 UINT sub_count
= texture
->level_count
* texture
->layer_count
;
909 const struct wined3d_d3d_info
*d3d_info
= context
->d3d_info
;
913 TRACE("texture %p, context %p, srgb %#x.\n", texture
, context
, srgb
);
915 if (!needs_separate_srgb_gl_texture(context
))
919 flag
= WINED3D_TEXTURE_SRGB_VALID
;
921 flag
= WINED3D_TEXTURE_RGB_VALID
;
923 if (!d3d_info
->shader_color_key
924 && (!(texture
->async
.flags
& WINED3D_TEXTURE_ASYNC_COLOR_KEY
)
925 != !(texture
->async
.color_key_flags
& WINED3D_CKEY_SRC_BLT
)
926 || (texture
->async
.flags
& WINED3D_TEXTURE_ASYNC_COLOR_KEY
927 && !color_key_equal(&texture
->async
.gl_color_key
, &texture
->async
.src_blt_color_key
))))
929 unsigned int sub_count
= texture
->level_count
* texture
->layer_count
;
932 TRACE("Reloading because of color key value change.\n");
933 for (i
= 0; i
< sub_count
; i
++)
935 if (!wined3d_texture_load_location(texture
, i
, context
, texture
->resource
.map_binding
))
936 ERR("Failed to load location %s.\n", wined3d_debug_location(texture
->resource
.map_binding
));
938 wined3d_texture_invalidate_location(texture
, i
, ~texture
->resource
.map_binding
);
941 texture
->async
.gl_color_key
= texture
->async
.src_blt_color_key
;
944 if (texture
->flags
& flag
)
946 TRACE("Texture %p not dirty, nothing to do.\n", texture
);
950 /* Reload the surfaces if the texture is marked dirty. */
951 for (i
= 0; i
< sub_count
; ++i
)
953 if (!wined3d_texture_load_location(texture
, i
, context
,
954 srgb
? WINED3D_LOCATION_TEXTURE_SRGB
: WINED3D_LOCATION_TEXTURE_RGB
))
955 ERR("Failed to load location (srgb %#x).\n", srgb
);
957 texture
->flags
|= flag
;
960 void * CDECL
wined3d_texture_get_parent(const struct wined3d_texture
*texture
)
962 TRACE("texture %p.\n", texture
);
964 return texture
->resource
.parent
;
967 static BOOL
wined3d_texture_check_box_dimensions(const struct wined3d_texture
*texture
,
968 unsigned int level
, const struct wined3d_box
*box
)
970 if (box
->left
>= box
->right
971 || box
->top
>= box
->bottom
972 || box
->front
>= box
->back
)
975 if (box
->right
> wined3d_texture_get_level_width(texture
, level
)
976 || box
->bottom
> wined3d_texture_get_level_height(texture
, level
)
977 || box
->back
> wined3d_texture_get_level_depth(texture
, level
))
983 void CDECL
wined3d_texture_get_pitch(const struct wined3d_texture
*texture
,
984 unsigned int level
, unsigned int *row_pitch
, unsigned int *slice_pitch
)
986 const struct wined3d_resource
*resource
= &texture
->resource
;
987 unsigned int width
= wined3d_texture_get_level_width(texture
, level
);
988 unsigned int height
= wined3d_texture_get_level_height(texture
, level
);
990 if (texture
->row_pitch
)
992 *row_pitch
= texture
->row_pitch
;
993 *slice_pitch
= texture
->slice_pitch
;
997 wined3d_format_calculate_pitch(resource
->format
, resource
->device
->surface_alignment
,
998 width
, height
, row_pitch
, slice_pitch
);
1001 DWORD CDECL
wined3d_texture_set_lod(struct wined3d_texture
*texture
, DWORD lod
)
1003 DWORD old
= texture
->lod
;
1005 TRACE("texture %p, lod %u.\n", texture
, lod
);
1007 /* The d3d9:texture test shows that SetLOD is ignored on non-managed
1008 * textures. The call always returns 0, and GetLOD always returns 0. */
1009 if (texture
->resource
.pool
!= WINED3D_POOL_MANAGED
)
1011 TRACE("Ignoring SetLOD on %s texture, returning 0.\n", debug_d3dpool(texture
->resource
.pool
));
1015 if (lod
>= texture
->level_count
)
1016 lod
= texture
->level_count
- 1;
1018 if (texture
->lod
!= lod
)
1020 wined3d_resource_wait_idle(&texture
->resource
);
1023 texture
->texture_rgb
.base_level
= ~0u;
1024 texture
->texture_srgb
.base_level
= ~0u;
1025 if (texture
->resource
.bind_count
)
1026 device_invalidate_state(texture
->resource
.device
, STATE_SAMPLER(texture
->sampler
));
1032 DWORD CDECL
wined3d_texture_get_lod(const struct wined3d_texture
*texture
)
1034 TRACE("texture %p, returning %u.\n", texture
, texture
->lod
);
1036 return texture
->lod
;
1039 DWORD CDECL
wined3d_texture_get_level_count(const struct wined3d_texture
*texture
)
1041 TRACE("texture %p, returning %u.\n", texture
, texture
->level_count
);
1043 return texture
->level_count
;
1046 HRESULT CDECL
wined3d_texture_set_autogen_filter_type(struct wined3d_texture
*texture
,
1047 enum wined3d_texture_filter_type filter_type
)
1049 FIXME("texture %p, filter_type %s stub!\n", texture
, debug_d3dtexturefiltertype(filter_type
));
1051 if (!(texture
->resource
.usage
& WINED3DUSAGE_AUTOGENMIPMAP
))
1053 WARN("Texture doesn't have AUTOGENMIPMAP usage.\n");
1054 return WINED3DERR_INVALIDCALL
;
1057 texture
->filter_type
= filter_type
;
1062 enum wined3d_texture_filter_type CDECL
wined3d_texture_get_autogen_filter_type(const struct wined3d_texture
*texture
)
1064 TRACE("texture %p.\n", texture
);
1066 return texture
->filter_type
;
1069 HRESULT CDECL
wined3d_texture_set_color_key(struct wined3d_texture
*texture
,
1070 DWORD flags
, const struct wined3d_color_key
*color_key
)
1072 struct wined3d_device
*device
= texture
->resource
.device
;
1073 static const DWORD all_flags
= WINED3D_CKEY_DST_BLT
| WINED3D_CKEY_DST_OVERLAY
1074 | WINED3D_CKEY_SRC_BLT
| WINED3D_CKEY_SRC_OVERLAY
;
1076 TRACE("texture %p, flags %#x, color_key %p.\n", texture
, flags
, color_key
);
1078 if (flags
& ~all_flags
)
1080 WARN("Invalid flags passed, returning WINED3DERR_INVALIDCALL.\n");
1081 return WINED3DERR_INVALIDCALL
;
1084 wined3d_cs_emit_set_color_key(device
->cs
, texture
, flags
, color_key
);
1089 HRESULT CDECL
wined3d_texture_update_desc(struct wined3d_texture
*texture
, UINT width
, UINT height
,
1090 enum wined3d_format_id format_id
, enum wined3d_multisample_type multisample_type
,
1091 UINT multisample_quality
, void *mem
, UINT pitch
)
1093 struct wined3d_device
*device
= texture
->resource
.device
;
1094 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
1095 const struct wined3d_format
*format
= wined3d_get_format(gl_info
, format_id
, texture
->resource
.usage
);
1096 UINT resource_size
= wined3d_format_calculate_size(format
, device
->surface_alignment
, width
, height
, 1);
1097 struct wined3d_texture_sub_resource
*sub_resource
;
1098 struct wined3d_surface
*surface
;
1099 DWORD valid_location
= 0;
1100 BOOL create_dib
= FALSE
;
1102 TRACE("texture %p, width %u, height %u, format %s, multisample_type %#x, multisample_quality %u, "
1103 "mem %p, pitch %u.\n",
1104 texture
, width
, height
, debug_d3dformat(format_id
), multisample_type
, multisample_quality
, mem
, pitch
);
1107 return WINED3DERR_INVALIDCALL
;
1109 if (texture
->level_count
* texture
->layer_count
> 1)
1111 WARN("Texture has multiple sub-resources, not supported.\n");
1112 return WINED3DERR_INVALIDCALL
;
1115 if (texture
->resource
.type
== WINED3D_RTYPE_TEXTURE_3D
)
1117 WARN("Not supported on 3D textures.\n");
1118 return WINED3DERR_INVALIDCALL
;
1121 if (texture
->resource
.map_count
)
1123 WARN("Texture is mapped.\n");
1124 return WINED3DERR_INVALIDCALL
;
1127 /* We have no way of supporting a pitch that is not a multiple of the pixel
1128 * byte width short of uploading the texture row-by-row.
1129 * Fortunately that's not an issue since D3D9Ex doesn't allow a custom pitch
1130 * for user-memory textures (it always expects packed data) while DirectDraw
1131 * requires a 4-byte aligned pitch and doesn't support texture formats
1132 * larger than 4 bytes per pixel nor any format using 3 bytes per pixel.
1133 * This check is here to verify that the assumption holds. */
1134 if (pitch
% texture
->resource
.format
->byte_count
)
1136 WARN("Pitch unsupported, not a multiple of the texture format byte width.\n");
1137 return WINED3DERR_INVALIDCALL
;
1140 if (device
->d3d_initialized
)
1141 wined3d_cs_emit_unload_resource(device
->cs
, &texture
->resource
);
1142 wined3d_resource_wait_idle(&texture
->resource
);
1144 sub_resource
= &texture
->sub_resources
[0];
1145 surface
= sub_resource
->u
.surface
;
1148 wined3d_surface_destroy_dc(surface
);
1152 wined3d_resource_free_sysmem(&texture
->resource
);
1154 if ((texture
->row_pitch
= pitch
))
1155 texture
->slice_pitch
= height
* pitch
;
1157 /* User memory surfaces don't have the regular surface alignment. */
1158 wined3d_format_calculate_pitch(format
, 1, width
, height
,
1159 &texture
->row_pitch
, &texture
->slice_pitch
);
1161 texture
->resource
.format
= format
;
1162 texture
->resource
.multisample_type
= multisample_type
;
1163 texture
->resource
.multisample_quality
= multisample_quality
;
1164 texture
->resource
.width
= width
;
1165 texture
->resource
.height
= height
;
1166 texture
->resource
.size
= texture
->slice_pitch
;
1167 sub_resource
->size
= texture
->slice_pitch
;
1168 sub_resource
->locations
= WINED3D_LOCATION_DISCARDED
;
1170 if (((width
& (width
- 1)) || (height
& (height
- 1))) && !gl_info
->supported
[ARB_TEXTURE_NON_POWER_OF_TWO
]
1171 && !gl_info
->supported
[ARB_TEXTURE_RECTANGLE
] && !gl_info
->supported
[WINED3D_GL_NORMALIZED_TEXRECT
])
1173 texture
->flags
|= WINED3D_TEXTURE_COND_NP2_EMULATED
;
1174 texture
->pow2_width
= texture
->pow2_height
= 1;
1175 while (texture
->pow2_width
< width
)
1176 texture
->pow2_width
<<= 1;
1177 while (texture
->pow2_height
< height
)
1178 texture
->pow2_height
<<= 1;
1182 texture
->flags
&= ~WINED3D_TEXTURE_COND_NP2_EMULATED
;
1183 texture
->pow2_width
= width
;
1184 texture
->pow2_height
= height
;
1187 if ((texture
->user_memory
= mem
))
1189 texture
->resource
.map_binding
= WINED3D_LOCATION_USER_MEMORY
;
1190 valid_location
= WINED3D_LOCATION_USER_MEMORY
;
1194 wined3d_texture_prepare_location(texture
, 0, NULL
, WINED3D_LOCATION_SYSMEM
);
1195 valid_location
= WINED3D_LOCATION_SYSMEM
;
1198 /* The format might be changed to a format that needs conversion.
1199 * If the surface didn't use PBOs previously but could now, don't
1200 * change it - whatever made us not use PBOs might come back, e.g.
1202 if (texture
->resource
.map_binding
== WINED3D_LOCATION_BUFFER
&& !wined3d_texture_use_pbo(texture
, gl_info
))
1203 texture
->resource
.map_binding
= WINED3D_LOCATION_SYSMEM
;
1205 wined3d_texture_validate_location(texture
, 0, valid_location
);
1206 wined3d_texture_invalidate_location(texture
, 0, ~valid_location
);
1209 wined3d_surface_create_dc(surface
);
1214 /* Context activation is done by the caller. */
1215 static void wined3d_texture_prepare_buffer_object(struct wined3d_texture
*texture
,
1216 unsigned int sub_resource_idx
, const struct wined3d_gl_info
*gl_info
)
1218 struct wined3d_texture_sub_resource
*sub_resource
;
1220 sub_resource
= &texture
->sub_resources
[sub_resource_idx
];
1221 if (sub_resource
->buffer_object
)
1224 GL_EXTCALL(glGenBuffers(1, &sub_resource
->buffer_object
));
1225 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER
, sub_resource
->buffer_object
));
1226 GL_EXTCALL(glBufferData(GL_PIXEL_UNPACK_BUFFER
, sub_resource
->size
, NULL
, GL_STREAM_DRAW
));
1227 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER
, 0));
1228 checkGLcall("Create buffer object");
1230 TRACE("Created buffer object %u for texture %p, sub-resource %u.\n",
1231 sub_resource
->buffer_object
, texture
, sub_resource_idx
);
1234 static void wined3d_texture_force_reload(struct wined3d_texture
*texture
)
1236 unsigned int sub_count
= texture
->level_count
* texture
->layer_count
;
1239 texture
->flags
&= ~(WINED3D_TEXTURE_RGB_ALLOCATED
| WINED3D_TEXTURE_SRGB_ALLOCATED
1240 | WINED3D_TEXTURE_CONVERTED
);
1241 texture
->async
.flags
&= ~WINED3D_TEXTURE_ASYNC_COLOR_KEY
;
1242 for (i
= 0; i
< sub_count
; ++i
)
1244 wined3d_texture_invalidate_location(texture
, i
,
1245 WINED3D_LOCATION_TEXTURE_RGB
| WINED3D_LOCATION_TEXTURE_SRGB
);
1249 void wined3d_texture_prepare_texture(struct wined3d_texture
*texture
, struct wined3d_context
*context
, BOOL srgb
)
1251 DWORD alloc_flag
= srgb
? WINED3D_TEXTURE_SRGB_ALLOCATED
: WINED3D_TEXTURE_RGB_ALLOCATED
;
1252 const struct wined3d_d3d_info
*d3d_info
= context
->d3d_info
;
1254 if (!d3d_info
->shader_color_key
1255 && !(texture
->async
.flags
& WINED3D_TEXTURE_ASYNC_COLOR_KEY
)
1256 != !(texture
->async
.color_key_flags
& WINED3D_CKEY_SRC_BLT
))
1258 wined3d_texture_force_reload(texture
);
1260 if (texture
->async
.color_key_flags
& WINED3D_CKEY_SRC_BLT
)
1261 texture
->async
.flags
|= WINED3D_TEXTURE_ASYNC_COLOR_KEY
;
1264 if (texture
->flags
& alloc_flag
)
1267 texture
->texture_ops
->texture_prepare_texture(texture
, context
, srgb
);
1268 texture
->flags
|= alloc_flag
;
1271 static void wined3d_texture_prepare_rb(struct wined3d_texture
*texture
,
1272 const struct wined3d_gl_info
*gl_info
, BOOL multisample
)
1274 const struct wined3d_format
*format
= texture
->resource
.format
;
1280 if (texture
->rb_multisample
)
1283 /* TODO: NVIDIA expose their Coverage Sample Anti-Aliasing (CSAA)
1284 * feature through type == MULTISAMPLE_XX and quality != 0. This could
1285 * be mapped to GL_NV_framebuffer_multisample_coverage.
1287 * AMD have a similar feature called Enhanced Quality Anti-Aliasing
1288 * (EQAA), but it does not have an equivalent OpenGL extension. */
1290 /* We advertise as many WINED3D_MULTISAMPLE_NON_MASKABLE quality
1291 * levels as the count of advertised multisample types for the texture
1293 if (texture
->resource
.multisample_type
== WINED3D_MULTISAMPLE_NON_MASKABLE
)
1295 unsigned int i
, count
= 0;
1297 for (i
= 0; i
< sizeof(format
->multisample_types
) * 8; ++i
)
1299 if (format
->multisample_types
& 1u << i
)
1301 if (texture
->resource
.multisample_quality
== count
++)
1309 samples
= texture
->resource
.multisample_type
;
1312 gl_info
->fbo_ops
.glGenRenderbuffers(1, &texture
->rb_multisample
);
1313 gl_info
->fbo_ops
.glBindRenderbuffer(GL_RENDERBUFFER
, texture
->rb_multisample
);
1314 gl_info
->fbo_ops
.glRenderbufferStorageMultisample(GL_RENDERBUFFER
, samples
,
1315 format
->glInternal
, texture
->resource
.width
, texture
->resource
.height
);
1316 checkGLcall("glRenderbufferStorageMultisample()");
1317 TRACE("Created multisample rb %u.\n", texture
->rb_multisample
);
1321 if (texture
->rb_resolved
)
1324 gl_info
->fbo_ops
.glGenRenderbuffers(1, &texture
->rb_resolved
);
1325 gl_info
->fbo_ops
.glBindRenderbuffer(GL_RENDERBUFFER
, texture
->rb_resolved
);
1326 gl_info
->fbo_ops
.glRenderbufferStorage(GL_RENDERBUFFER
, format
->glInternal
,
1327 texture
->resource
.width
, texture
->resource
.height
);
1328 checkGLcall("glRenderbufferStorage()");
1329 TRACE("Created resolved rb %u.\n", texture
->rb_resolved
);
1333 /* Context activation is done by the caller. Context may be NULL in
1334 * WINED3D_NO3D mode. */
1335 BOOL
wined3d_texture_prepare_location(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
,
1336 struct wined3d_context
*context
, DWORD location
)
1340 case WINED3D_LOCATION_SYSMEM
:
1341 if (texture
->resource
.heap_memory
)
1344 if (!wined3d_resource_allocate_sysmem(&texture
->resource
))
1346 ERR("Failed to allocate system memory.\n");
1351 case WINED3D_LOCATION_USER_MEMORY
:
1352 if (!texture
->user_memory
)
1353 ERR("Map binding is set to WINED3D_LOCATION_USER_MEMORY but surface->user_memory is NULL.\n");
1356 case WINED3D_LOCATION_BUFFER
:
1357 wined3d_texture_prepare_buffer_object(texture
, sub_resource_idx
, context
->gl_info
);
1360 case WINED3D_LOCATION_TEXTURE_RGB
:
1361 wined3d_texture_prepare_texture(texture
, context
, FALSE
);
1364 case WINED3D_LOCATION_TEXTURE_SRGB
:
1365 wined3d_texture_prepare_texture(texture
, context
, TRUE
);
1368 case WINED3D_LOCATION_DRAWABLE
:
1369 if (!texture
->swapchain
&& wined3d_settings
.always_offscreen
)
1370 ERR("Texture %p does not have a drawable.\n", texture
);
1373 case WINED3D_LOCATION_RB_MULTISAMPLE
:
1374 wined3d_texture_prepare_rb(texture
, context
->gl_info
, TRUE
);
1377 case WINED3D_LOCATION_RB_RESOLVED
:
1378 wined3d_texture_prepare_rb(texture
, context
->gl_info
, FALSE
);
1382 ERR("Invalid location %s.\n", wined3d_debug_location(location
));
1387 void CDECL
wined3d_texture_generate_mipmaps(struct wined3d_texture
*texture
)
1389 /* TODO: Implement filters using GL_SGI_generate_mipmaps. */
1390 FIXME("texture %p stub!\n", texture
);
1393 static struct wined3d_texture_sub_resource
*wined3d_texture_get_sub_resource(struct wined3d_texture
*texture
,
1394 unsigned int sub_resource_idx
)
1396 UINT sub_count
= texture
->level_count
* texture
->layer_count
;
1398 TRACE("texture %p, sub_resource_idx %u.\n", texture
, sub_resource_idx
);
1400 if (sub_resource_idx
>= sub_count
)
1402 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx
, sub_count
);
1406 return &texture
->sub_resources
[sub_resource_idx
];
1409 HRESULT CDECL
wined3d_texture_add_dirty_region(struct wined3d_texture
*texture
,
1410 UINT layer
, const struct wined3d_box
*dirty_region
)
1412 struct wined3d_context
*context
;
1413 unsigned int sub_resource_idx
;
1415 TRACE("texture %p, layer %u, dirty_region %s.\n", texture
, layer
, debug_box(dirty_region
));
1417 if (layer
>= texture
->layer_count
)
1419 WARN("Invalid layer %u specified.\n", layer
);
1420 return WINED3DERR_INVALIDCALL
;
1422 sub_resource_idx
= layer
* texture
->level_count
;
1425 FIXME("Ignoring dirty_region %s.\n", debug_box(dirty_region
));
1427 context
= context_acquire(texture
->resource
.device
, NULL
);
1428 if (!wined3d_texture_load_location(texture
, sub_resource_idx
, context
, texture
->resource
.map_binding
))
1430 ERR("Failed to load location %s.\n", wined3d_debug_location(texture
->resource
.map_binding
));
1431 context_release(context
);
1432 return E_OUTOFMEMORY
;
1434 wined3d_texture_invalidate_location(texture
, sub_resource_idx
, ~texture
->resource
.map_binding
);
1435 context_release(context
);
1440 void wined3d_texture_upload_data(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
,
1441 const struct wined3d_context
*context
, const struct wined3d_box
*box
,
1442 const struct wined3d_const_bo_address
*data
, unsigned int row_pitch
, unsigned int slice_pitch
)
1444 texture
->texture_ops
->texture_upload_data(texture
, sub_resource_idx
,
1445 context
, box
, data
, row_pitch
, slice_pitch
);
1448 static void texture2d_upload_data(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
,
1449 const struct wined3d_context
*context
, const struct wined3d_box
*box
,
1450 const struct wined3d_const_bo_address
*data
, unsigned int row_pitch
, unsigned int slice_pitch
)
1452 unsigned int texture_level
;
1460 dst_point
.x
= box
->left
;
1461 dst_point
.y
= box
->top
;
1462 src_rect
.right
= box
->right
- box
->left
;
1463 src_rect
.bottom
= box
->bottom
- box
->top
;
1467 dst_point
.x
= dst_point
.y
= 0;
1468 texture_level
= sub_resource_idx
% texture
->level_count
;
1469 src_rect
.right
= wined3d_texture_get_level_width(texture
, texture_level
);
1470 src_rect
.bottom
= wined3d_texture_get_level_height(texture
, texture_level
);
1473 wined3d_surface_upload_data(texture
->sub_resources
[sub_resource_idx
].u
.surface
, context
->gl_info
,
1474 texture
->resource
.format
, &src_rect
, row_pitch
, &dst_point
, FALSE
, data
);
1477 static BOOL
texture2d_load_location(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
,
1478 struct wined3d_context
*context
, DWORD location
)
1480 return SUCCEEDED(surface_load_location(texture
->sub_resources
[sub_resource_idx
].u
.surface
, context
, location
));
1483 /* Context activation is done by the caller. */
1484 static void texture2d_prepare_texture(struct wined3d_texture
*texture
, struct wined3d_context
*context
, BOOL srgb
)
1486 const struct wined3d_format
*format
= texture
->resource
.format
;
1487 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1488 const struct wined3d_color_key_conversion
*conversion
;
1491 TRACE("texture %p, context %p, format %s.\n", texture
, context
, debug_d3dformat(format
->id
));
1493 if (format
->convert
)
1495 texture
->flags
|= WINED3D_TEXTURE_CONVERTED
;
1497 else if ((conversion
= wined3d_format_get_color_key_conversion(texture
, TRUE
)))
1499 texture
->flags
|= WINED3D_TEXTURE_CONVERTED
;
1500 format
= wined3d_get_format(gl_info
, conversion
->dst_format
, texture
->resource
.usage
);
1501 TRACE("Using format %s for color key conversion.\n", debug_d3dformat(format
->id
));
1504 wined3d_texture_bind_and_dirtify(texture
, context
, srgb
);
1507 internal
= format
->glGammaInternal
;
1508 else if (texture
->resource
.usage
& WINED3DUSAGE_RENDERTARGET
1509 && wined3d_resource_is_offscreen(&texture
->resource
))
1510 internal
= format
->rtInternal
;
1512 internal
= format
->glInternal
;
1515 FIXME("No GL internal format for format %s.\n", debug_d3dformat(format
->id
));
1517 TRACE("internal %#x, format %#x, type %#x.\n", internal
, format
->glFormat
, format
->glType
);
1519 if (wined3d_texture_use_immutable_storage(texture
, gl_info
))
1520 wined3d_texture_allocate_gl_immutable_storage(texture
, internal
, gl_info
);
1522 wined3d_texture_allocate_gl_mutable_storage(texture
, internal
, format
, gl_info
);
1525 static void texture2d_cleanup_sub_resources(struct wined3d_texture
*texture
)
1527 unsigned int sub_count
= texture
->level_count
* texture
->layer_count
;
1528 struct wined3d_device
*device
= texture
->resource
.device
;
1529 struct wined3d_texture_sub_resource
*sub_resource
;
1530 struct wined3d_renderbuffer_entry
*entry
, *entry2
;
1531 const struct wined3d_gl_info
*gl_info
= NULL
;
1532 struct wined3d_context
*context
= NULL
;
1533 struct wined3d_surface
*overlay
, *cur
;
1534 struct wined3d_surface
*surface
;
1537 for (i
= 0; i
< sub_count
; ++i
)
1539 sub_resource
= &texture
->sub_resources
[i
];
1540 if (!(surface
= sub_resource
->u
.surface
))
1543 TRACE("surface %p.\n", surface
);
1545 if (!context
&& !list_empty(&surface
->renderbuffers
))
1547 context
= context_acquire(device
, NULL
);
1548 gl_info
= context
->gl_info
;
1551 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, &surface
->renderbuffers
, struct wined3d_renderbuffer_entry
, entry
)
1553 TRACE("Deleting renderbuffer %u.\n", entry
->id
);
1554 context_gl_resource_released(device
, entry
->id
, TRUE
);
1555 gl_info
->fbo_ops
.glDeleteRenderbuffers(1, &entry
->id
);
1556 HeapFree(GetProcessHeap(), 0, entry
);
1560 wined3d_surface_destroy_dc(surface
);
1562 if (surface
->overlay_dest
)
1563 list_remove(&surface
->overlay_entry
);
1565 LIST_FOR_EACH_ENTRY_SAFE(overlay
, cur
, &surface
->overlays
, struct wined3d_surface
, overlay_entry
)
1567 list_remove(&overlay
->overlay_entry
);
1568 overlay
->overlay_dest
= NULL
;
1572 context_release(context
);
1573 HeapFree(GetProcessHeap(), 0, texture
->sub_resources
[0].u
.surface
);
1576 static const struct wined3d_texture_ops texture2d_ops
=
1578 texture2d_upload_data
,
1579 texture2d_load_location
,
1580 texture2d_prepare_texture
,
1581 texture2d_cleanup_sub_resources
,
1584 struct wined3d_texture
* __cdecl
wined3d_texture_from_resource(struct wined3d_resource
*resource
)
1586 return texture_from_resource(resource
);
1589 static ULONG
texture_resource_incref(struct wined3d_resource
*resource
)
1591 return wined3d_texture_incref(texture_from_resource(resource
));
1594 static ULONG
texture_resource_decref(struct wined3d_resource
*resource
)
1596 return wined3d_texture_decref(texture_from_resource(resource
));
1599 static void texture_resource_preload(struct wined3d_resource
*resource
)
1601 struct wined3d_texture
*texture
= texture_from_resource(resource
);
1602 struct wined3d_context
*context
;
1604 context
= context_acquire(resource
->device
, NULL
);
1605 wined3d_texture_load(texture
, context
, texture
->flags
& WINED3D_TEXTURE_IS_SRGB
);
1606 context_release(context
);
1609 static void wined3d_texture_unload(struct wined3d_resource
*resource
)
1611 struct wined3d_texture
*texture
= texture_from_resource(resource
);
1612 UINT sub_count
= texture
->level_count
* texture
->layer_count
;
1613 struct wined3d_device
*device
= resource
->device
;
1614 const struct wined3d_gl_info
*gl_info
;
1615 struct wined3d_context
*context
;
1618 TRACE("texture %p.\n", texture
);
1620 context
= context_acquire(device
, NULL
);
1621 gl_info
= context
->gl_info
;
1623 for (i
= 0; i
< sub_count
; ++i
)
1625 struct wined3d_texture_sub_resource
*sub_resource
= &texture
->sub_resources
[i
];
1627 if (resource
->pool
!= WINED3D_POOL_DEFAULT
1628 && wined3d_texture_load_location(texture
, i
, context
, resource
->map_binding
))
1630 wined3d_texture_invalidate_location(texture
, i
, ~resource
->map_binding
);
1634 /* We should only get here on device reset/teardown for implicit
1636 if (resource
->pool
!= WINED3D_POOL_DEFAULT
|| resource
->type
!= WINED3D_RTYPE_TEXTURE_2D
)
1637 ERR("Discarding %s %p sub-resource %u in the %s pool.\n", debug_d3dresourcetype(resource
->type
),
1638 resource
, i
, debug_d3dpool(resource
->pool
));
1639 wined3d_texture_validate_location(texture
, i
, WINED3D_LOCATION_DISCARDED
);
1640 wined3d_texture_invalidate_location(texture
, i
, ~WINED3D_LOCATION_DISCARDED
);
1643 if (sub_resource
->buffer_object
)
1644 wined3d_texture_remove_buffer_object(texture
, i
, context
->gl_info
);
1646 if (resource
->type
== WINED3D_RTYPE_TEXTURE_2D
)
1648 struct wined3d_surface
*surface
= sub_resource
->u
.surface
;
1649 struct wined3d_renderbuffer_entry
*entry
, *entry2
;
1651 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, &surface
->renderbuffers
, struct wined3d_renderbuffer_entry
, entry
)
1653 context_gl_resource_released(device
, entry
->id
, TRUE
);
1654 gl_info
->fbo_ops
.glDeleteRenderbuffers(1, &entry
->id
);
1655 list_remove(&entry
->entry
);
1656 HeapFree(GetProcessHeap(), 0, entry
);
1658 list_init(&surface
->renderbuffers
);
1659 surface
->current_renderbuffer
= NULL
;
1663 context_release(context
);
1665 wined3d_texture_force_reload(texture
);
1666 wined3d_texture_unload_gl_texture(texture
);
1669 static HRESULT
texture_resource_sub_resource_map(struct wined3d_resource
*resource
, unsigned int sub_resource_idx
,
1670 struct wined3d_map_desc
*map_desc
, const struct wined3d_box
*box
, DWORD flags
)
1672 const struct wined3d_format
*format
= resource
->format
;
1673 struct wined3d_texture_sub_resource
*sub_resource
;
1674 struct wined3d_device
*device
= resource
->device
;
1675 unsigned int fmt_flags
= resource
->format_flags
;
1676 const struct wined3d_gl_info
*gl_info
= NULL
;
1677 struct wined3d_context
*context
= NULL
;
1678 struct wined3d_texture
*texture
;
1679 struct wined3d_bo_address data
;
1680 unsigned int texture_level
;
1684 TRACE("resource %p, sub_resource_idx %u, map_desc %p, box %s, flags %#x.\n",
1685 resource
, sub_resource_idx
, map_desc
, debug_box(box
), flags
);
1687 texture
= texture_from_resource(resource
);
1688 if (!(sub_resource
= wined3d_texture_get_sub_resource(texture
, sub_resource_idx
)))
1689 return E_INVALIDARG
;
1691 texture_level
= sub_resource_idx
% texture
->level_count
;
1692 if (box
&& !wined3d_texture_check_box_dimensions(texture
, texture_level
, box
))
1694 WARN("Map box is invalid.\n");
1695 if (resource
->type
!= WINED3D_RTYPE_TEXTURE_2D
)
1696 return WINED3DERR_INVALIDCALL
;
1699 if ((fmt_flags
& WINED3DFMT_FLAG_BLOCKS
) && box
1700 && !wined3d_texture_check_block_align(texture
, texture_level
, box
))
1702 WARN("Map box %s is misaligned for %ux%u blocks.\n",
1703 debug_box(box
), format
->block_width
, format
->block_height
);
1704 if (resource
->type
!= WINED3D_RTYPE_TEXTURE_2D
|| resource
->pool
== WINED3D_POOL_DEFAULT
)
1705 return WINED3DERR_INVALIDCALL
;
1708 if (!(resource
->access_flags
& WINED3D_RESOURCE_ACCESS_CPU
))
1710 WARN("Trying to map unmappable texture.\n");
1711 if (resource
->type
!= WINED3D_RTYPE_TEXTURE_2D
)
1712 return WINED3DERR_INVALIDCALL
;
1715 if (texture
->flags
& WINED3D_TEXTURE_DC_IN_USE
)
1717 WARN("DC is in use.\n");
1718 return WINED3DERR_INVALIDCALL
;
1721 if (sub_resource
->map_count
)
1723 WARN("Sub-resource is already mapped.\n");
1724 return WINED3DERR_INVALIDCALL
;
1727 if (device
->d3d_initialized
)
1729 context
= context_acquire(device
, NULL
);
1730 gl_info
= context
->gl_info
;
1733 if (flags
& WINED3D_MAP_DISCARD
)
1735 TRACE("WINED3D_MAP_DISCARD flag passed, marking %s as up to date.\n",
1736 wined3d_debug_location(texture
->resource
.map_binding
));
1737 if ((ret
= wined3d_texture_prepare_location(texture
, sub_resource_idx
,
1738 context
, texture
->resource
.map_binding
)))
1739 wined3d_texture_validate_location(texture
, sub_resource_idx
, texture
->resource
.map_binding
);
1743 if (resource
->usage
& WINED3DUSAGE_DYNAMIC
)
1744 WARN_(d3d_perf
)("Mapping a dynamic texture without WINED3D_MAP_DISCARD.\n");
1745 ret
= wined3d_texture_load_location(texture
, sub_resource_idx
, context
, texture
->resource
.map_binding
);
1750 ERR("Failed to prepare location.\n");
1751 context_release(context
);
1752 return E_OUTOFMEMORY
;
1755 if (!(flags
& (WINED3D_MAP_NO_DIRTY_UPDATE
| WINED3D_MAP_READONLY
)))
1756 wined3d_texture_invalidate_location(texture
, sub_resource_idx
, ~texture
->resource
.map_binding
);
1758 wined3d_texture_get_memory(texture
, sub_resource_idx
, &data
, texture
->resource
.map_binding
);
1759 base_memory
= wined3d_texture_map_bo_address(&data
, sub_resource
->size
,
1760 gl_info
, GL_PIXEL_UNPACK_BUFFER
, flags
);
1761 TRACE("Base memory pointer %p.\n", base_memory
);
1764 context_release(context
);
1766 if (fmt_flags
& WINED3DFMT_FLAG_BROKEN_PITCH
)
1768 map_desc
->row_pitch
= wined3d_texture_get_level_width(texture
, texture_level
) * format
->byte_count
;
1769 map_desc
->slice_pitch
= wined3d_texture_get_level_height(texture
, texture_level
) * map_desc
->row_pitch
;
1773 wined3d_texture_get_pitch(texture
, texture_level
, &map_desc
->row_pitch
, &map_desc
->slice_pitch
);
1778 map_desc
->data
= base_memory
;
1782 if ((fmt_flags
& (WINED3DFMT_FLAG_BLOCKS
| WINED3DFMT_FLAG_BROKEN_PITCH
)) == WINED3DFMT_FLAG_BLOCKS
)
1784 /* Compressed textures are block based, so calculate the offset of
1785 * the block that contains the top-left pixel of the mapped box. */
1786 map_desc
->data
= base_memory
1787 + (box
->front
* map_desc
->slice_pitch
)
1788 + ((box
->top
/ format
->block_height
) * map_desc
->row_pitch
)
1789 + ((box
->left
/ format
->block_width
) * format
->block_byte_count
);
1793 map_desc
->data
= base_memory
1794 + (box
->front
* map_desc
->slice_pitch
)
1795 + (box
->top
* map_desc
->row_pitch
)
1796 + (box
->left
* format
->byte_count
);
1800 if (texture
->swapchain
&& texture
->swapchain
->front_buffer
== texture
)
1802 RECT
*r
= &texture
->swapchain
->front_buffer_update
;
1805 SetRect(r
, 0, 0, resource
->width
, resource
->height
);
1807 SetRect(r
, box
->left
, box
->top
, box
->right
, box
->bottom
);
1808 TRACE("Mapped front buffer %s.\n", wine_dbgstr_rect(r
));
1811 ++resource
->map_count
;
1812 ++sub_resource
->map_count
;
1814 TRACE("Returning memory %p, row pitch %u, slice pitch %u.\n",
1815 map_desc
->data
, map_desc
->row_pitch
, map_desc
->slice_pitch
);
1820 static HRESULT
texture_resource_sub_resource_unmap(struct wined3d_resource
*resource
, unsigned int sub_resource_idx
)
1822 struct wined3d_texture_sub_resource
*sub_resource
;
1823 struct wined3d_device
*device
= resource
->device
;
1824 const struct wined3d_gl_info
*gl_info
= NULL
;
1825 struct wined3d_context
*context
= NULL
;
1826 struct wined3d_texture
*texture
;
1827 struct wined3d_bo_address data
;
1829 TRACE("resource %p, sub_resource_idx %u.\n", resource
, sub_resource_idx
);
1831 texture
= texture_from_resource(resource
);
1832 if (!(sub_resource
= wined3d_texture_get_sub_resource(texture
, sub_resource_idx
)))
1833 return E_INVALIDARG
;
1835 if (!sub_resource
->map_count
)
1837 WARN("Trying to unmap unmapped sub-resource.\n");
1838 if (texture
->flags
& WINED3D_TEXTURE_DC_IN_USE
)
1840 return WINEDDERR_NOTLOCKED
;
1843 if (device
->d3d_initialized
)
1845 context
= context_acquire(device
, NULL
);
1846 gl_info
= context
->gl_info
;
1849 wined3d_texture_get_memory(texture
, sub_resource_idx
, &data
, texture
->resource
.map_binding
);
1850 wined3d_texture_unmap_bo_address(&data
, gl_info
, GL_PIXEL_UNPACK_BUFFER
);
1853 context_release(context
);
1855 if (texture
->swapchain
&& texture
->swapchain
->front_buffer
== texture
)
1857 if (!(sub_resource
->locations
& (WINED3D_LOCATION_DRAWABLE
| WINED3D_LOCATION_TEXTURE_RGB
)))
1858 texture
->swapchain
->swapchain_ops
->swapchain_frontbuffer_updated(texture
->swapchain
);
1860 else if (resource
->format_flags
& (WINED3DFMT_FLAG_DEPTH
| WINED3DFMT_FLAG_STENCIL
))
1862 FIXME("Depth / stencil buffer locking is not implemented.\n");
1865 --sub_resource
->map_count
;
1866 if (!--resource
->map_count
&& texture
->update_map_binding
)
1867 wined3d_texture_update_map_binding(texture
);
1872 static const struct wined3d_resource_ops texture_resource_ops
=
1874 texture_resource_incref
,
1875 texture_resource_decref
,
1876 texture_resource_preload
,
1877 wined3d_texture_unload
,
1878 texture_resource_sub_resource_map
,
1879 texture_resource_sub_resource_unmap
,
1882 static HRESULT
texture_init(struct wined3d_texture
*texture
, const struct wined3d_resource_desc
*desc
,
1883 unsigned int layer_count
, unsigned int level_count
, DWORD flags
, struct wined3d_device
*device
,
1884 void *parent
, const struct wined3d_parent_ops
*parent_ops
)
1886 struct wined3d_device_parent
*device_parent
= device
->device_parent
;
1887 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
1888 struct wined3d_surface
*surfaces
;
1889 UINT pow2_width
, pow2_height
;
1893 if (!(desc
->usage
& WINED3DUSAGE_LEGACY_CUBEMAP
) && layer_count
> 1
1894 && !gl_info
->supported
[EXT_TEXTURE_ARRAY
])
1896 WARN("OpenGL implementation does not support array textures.\n");
1897 return WINED3DERR_INVALIDCALL
;
1900 /* TODO: It should only be possible to create textures for formats
1901 * that are reported as supported. */
1902 if (WINED3DFMT_UNKNOWN
>= desc
->format
)
1904 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture
);
1905 return WINED3DERR_INVALIDCALL
;
1908 if (desc
->usage
& WINED3DUSAGE_DYNAMIC
&& desc
->pool
== WINED3D_POOL_MANAGED
)
1909 FIXME("Trying to create a managed texture with dynamic usage.\n");
1910 if (!(desc
->usage
& (WINED3DUSAGE_DYNAMIC
| WINED3DUSAGE_RENDERTARGET
| WINED3DUSAGE_DEPTHSTENCIL
))
1911 && (flags
& WINED3D_TEXTURE_CREATE_MAPPABLE
))
1912 WARN("Creating a mappable texture in the default pool that doesn't specify dynamic usage.\n");
1913 if (desc
->usage
& WINED3DUSAGE_RENDERTARGET
&& desc
->pool
!= WINED3D_POOL_DEFAULT
)
1914 FIXME("Trying to create a render target that isn't in the default pool.\n");
1916 pow2_width
= desc
->width
;
1917 pow2_height
= desc
->height
;
1918 if (((desc
->width
& (desc
->width
- 1)) || (desc
->height
& (desc
->height
- 1)))
1919 && !gl_info
->supported
[ARB_TEXTURE_NON_POWER_OF_TWO
])
1921 /* level_count == 0 returns an error as well. */
1922 if (level_count
!= 1 || layer_count
!= 1)
1924 if (desc
->pool
!= WINED3D_POOL_SCRATCH
)
1926 WARN("Attempted to create a mipmapped/cube/array NPOT texture without unconditional NPOT support.\n");
1927 return WINED3DERR_INVALIDCALL
;
1930 WARN("Creating a scratch mipmapped/cube/array NPOT texture despite lack of HW support.\n");
1932 texture
->flags
|= WINED3D_TEXTURE_COND_NP2
;
1934 if (!gl_info
->supported
[ARB_TEXTURE_RECTANGLE
] && !gl_info
->supported
[WINED3D_GL_NORMALIZED_TEXRECT
])
1936 const struct wined3d_format
*format
= wined3d_get_format(gl_info
, desc
->format
, desc
->usage
);
1938 /* TODO: Add support for non-power-of-two compressed textures. */
1939 if (format
->flags
[WINED3D_GL_RES_TYPE_TEX_2D
]
1940 & (WINED3DFMT_FLAG_COMPRESSED
| WINED3DFMT_FLAG_HEIGHT_SCALE
))
1942 FIXME("Compressed or height scaled non-power-of-two (%ux%u) textures are not supported.\n",
1943 desc
->width
, desc
->height
);
1944 return WINED3DERR_NOTAVAILABLE
;
1947 /* Find the nearest pow2 match. */
1948 pow2_width
= pow2_height
= 1;
1949 while (pow2_width
< desc
->width
)
1951 while (pow2_height
< desc
->height
)
1953 texture
->flags
|= WINED3D_TEXTURE_COND_NP2_EMULATED
;
1956 texture
->pow2_width
= pow2_width
;
1957 texture
->pow2_height
= pow2_height
;
1959 if ((pow2_width
> gl_info
->limits
.texture_size
|| pow2_height
> gl_info
->limits
.texture_size
)
1960 && (desc
->usage
& WINED3DUSAGE_TEXTURE
))
1962 /* One of four options:
1963 * 1: Do the same as we do with NPOT and scale the texture. (Any
1964 * texture ops would require the texture to be scaled which is
1965 * potentially slow.)
1966 * 2: Set the texture to the maximum size (bad idea).
1967 * 3: WARN and return WINED3DERR_NOTAVAILABLE.
1968 * 4: Create the surface, but allow it to be used only for DirectDraw
1969 * Blts. Some apps (e.g. Swat 3) create textures with a height of
1970 * 16 and a width > 3000 and blt 16x16 letter areas from them to
1971 * the render target. */
1972 if (desc
->pool
== WINED3D_POOL_DEFAULT
|| desc
->pool
== WINED3D_POOL_MANAGED
)
1974 WARN("Dimensions (%ux%u) exceed the maximum texture size.\n", pow2_width
, pow2_height
);
1975 return WINED3DERR_NOTAVAILABLE
;
1978 /* We should never use this surface in combination with OpenGL. */
1979 TRACE("Creating an oversized (%ux%u) surface.\n", pow2_width
, pow2_height
);
1982 /* Calculate levels for mip mapping. */
1983 if (desc
->usage
& WINED3DUSAGE_AUTOGENMIPMAP
)
1985 if (!gl_info
->supported
[SGIS_GENERATE_MIPMAP
])
1987 WARN("No mipmap generation support, returning WINED3DERR_INVALIDCALL.\n");
1988 return WINED3DERR_INVALIDCALL
;
1991 if (level_count
!= 1)
1993 WARN("WINED3DUSAGE_AUTOGENMIPMAP is set, and level count != 1, returning WINED3DERR_INVALIDCALL.\n");
1994 return WINED3DERR_INVALIDCALL
;
1998 if (FAILED(hr
= wined3d_texture_init(texture
, &texture2d_ops
, layer_count
, level_count
, desc
,
1999 flags
, device
, parent
, parent_ops
, &texture_resource_ops
)))
2001 WARN("Failed to initialize texture, returning %#x.\n", hr
);
2005 /* Precalculated scaling for 'faked' non power of two texture coords. */
2006 if (texture
->resource
.gl_type
== WINED3D_GL_RES_TYPE_TEX_RECT
)
2008 texture
->pow2_matrix
[0] = (float)desc
->width
;
2009 texture
->pow2_matrix
[5] = (float)desc
->height
;
2010 texture
->flags
&= ~(WINED3D_TEXTURE_POW2_MAT_IDENT
| WINED3D_TEXTURE_NORMALIZED_COORDS
);
2011 texture
->target
= GL_TEXTURE_RECTANGLE_ARB
;
2015 if (texture
->flags
& WINED3D_TEXTURE_COND_NP2_EMULATED
)
2017 texture
->pow2_matrix
[0] = (((float)desc
->width
) / ((float)pow2_width
));
2018 texture
->pow2_matrix
[5] = (((float)desc
->height
) / ((float)pow2_height
));
2019 texture
->flags
&= ~WINED3D_TEXTURE_POW2_MAT_IDENT
;
2023 texture
->pow2_matrix
[0] = 1.0f
;
2024 texture
->pow2_matrix
[5] = 1.0f
;
2026 if (desc
->usage
& WINED3DUSAGE_LEGACY_CUBEMAP
)
2027 texture
->target
= GL_TEXTURE_CUBE_MAP_ARB
;
2028 else if (layer_count
> 1)
2029 texture
->target
= GL_TEXTURE_2D_ARRAY
;
2031 texture
->target
= GL_TEXTURE_2D
;
2033 texture
->pow2_matrix
[10] = 1.0f
;
2034 texture
->pow2_matrix
[15] = 1.0f
;
2035 TRACE("x scale %.8e, y scale %.8e.\n", texture
->pow2_matrix
[0], texture
->pow2_matrix
[5]);
2037 if (wined3d_texture_use_pbo(texture
, gl_info
))
2038 texture
->resource
.map_binding
= WINED3D_LOCATION_BUFFER
;
2040 if (level_count
> ~(SIZE_T
)0 / layer_count
2041 || !(surfaces
= wined3d_calloc(level_count
* layer_count
, sizeof(*surfaces
))))
2043 wined3d_texture_cleanup_sync(texture
);
2044 return E_OUTOFMEMORY
;
2047 /* Generate all the surfaces. */
2048 for (i
= 0; i
< texture
->level_count
; ++i
)
2050 for (j
= 0; j
< texture
->layer_count
; ++j
)
2052 static const GLenum cube_targets
[6] =
2054 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
,
2055 GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
,
2056 GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
,
2057 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
,
2058 GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
,
2059 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
,
2061 struct wined3d_texture_sub_resource
*sub_resource
;
2062 unsigned int idx
= j
* texture
->level_count
+ i
;
2063 struct wined3d_surface
*surface
;
2065 surface
= &surfaces
[idx
];
2066 surface
->container
= texture
;
2067 surface
->texture_target
= desc
->usage
& WINED3DUSAGE_LEGACY_CUBEMAP
? cube_targets
[j
] : texture
->target
;
2068 surface
->texture_level
= i
;
2069 surface
->texture_layer
= j
;
2070 list_init(&surface
->renderbuffers
);
2071 list_init(&surface
->overlays
);
2073 sub_resource
= &texture
->sub_resources
[idx
];
2074 sub_resource
->locations
= WINED3D_LOCATION_DISCARDED
;
2075 sub_resource
->u
.surface
= surface
;
2076 if (!(texture
->resource
.usage
& WINED3DUSAGE_DEPTHSTENCIL
))
2078 wined3d_texture_validate_location(texture
, idx
, WINED3D_LOCATION_SYSMEM
);
2079 wined3d_texture_invalidate_location(texture
, idx
, ~WINED3D_LOCATION_SYSMEM
);
2082 if (FAILED(hr
= device_parent
->ops
->surface_created(device_parent
,
2083 texture
, idx
, &sub_resource
->parent
, &sub_resource
->parent_ops
)))
2085 WARN("Failed to create surface parent, hr %#x.\n", hr
);
2086 sub_resource
->parent
= NULL
;
2087 wined3d_texture_cleanup_sync(texture
);
2091 TRACE("parent %p, parent_ops %p.\n", sub_resource
->parent
, sub_resource
->parent_ops
);
2093 TRACE("Created surface level %u, layer %u @ %p.\n", i
, j
, surface
);
2095 if (((desc
->usage
& WINED3DUSAGE_OWNDC
) || (device
->wined3d
->flags
& WINED3D_NO3D
))
2096 && FAILED(hr
= wined3d_surface_create_dc(surface
)))
2098 wined3d_texture_cleanup_sync(texture
);
2107 /* This call just uploads data, the caller is responsible for binding the
2108 * correct texture. */
2109 /* Context activation is done by the caller. */
2110 static void texture3d_upload_data(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
,
2111 const struct wined3d_context
*context
, const struct wined3d_box
*box
,
2112 const struct wined3d_const_bo_address
*data
, unsigned int row_pitch
, unsigned int slice_pitch
)
2114 const struct wined3d_format
*format
= texture
->resource
.format
;
2115 unsigned int level
= sub_resource_idx
% texture
->level_count
;
2116 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2117 unsigned int x
, y
, z
, update_w
, update_h
, update_d
;
2118 unsigned int dst_row_pitch
, dst_slice_pitch
;
2119 unsigned int width
, height
, depth
;
2120 const void *mem
= data
->addr
;
2121 void *converted_mem
= NULL
;
2123 TRACE("texture %p, sub_resource_idx %u, context %p, box %s, data {%#x:%p}, row_pitch %#x, slice_pitch %#x.\n",
2124 texture
, sub_resource_idx
, context
, debug_box(box
),
2125 data
->buffer_object
, data
->addr
, row_pitch
, slice_pitch
);
2127 width
= wined3d_texture_get_level_width(texture
, level
);
2128 height
= wined3d_texture_get_level_height(texture
, level
);
2129 depth
= wined3d_texture_get_level_depth(texture
, level
);
2143 update_w
= box
->right
- box
->left
;
2144 update_h
= box
->bottom
- box
->top
;
2145 update_d
= box
->back
- box
->front
;
2148 if (format
->convert
)
2150 if (data
->buffer_object
)
2151 ERR("Loading a converted texture from a PBO.\n");
2152 if (texture
->resource
.format_flags
& WINED3DFMT_FLAG_BLOCKS
)
2153 ERR("Converting a block-based format.\n");
2155 dst_row_pitch
= update_w
* format
->conv_byte_count
;
2156 dst_slice_pitch
= dst_row_pitch
* update_h
;
2158 converted_mem
= wined3d_calloc(update_d
, dst_slice_pitch
);
2159 format
->convert(data
->addr
, converted_mem
, row_pitch
, slice_pitch
,
2160 dst_row_pitch
, dst_slice_pitch
, update_w
, update_h
, update_d
);
2161 mem
= converted_mem
;
2165 wined3d_texture_get_pitch(texture
, sub_resource_idx
, &dst_row_pitch
, &dst_slice_pitch
);
2166 if (row_pitch
!= dst_row_pitch
|| slice_pitch
!= dst_slice_pitch
)
2167 FIXME("Ignoring row/slice pitch (%u/%u).\n", row_pitch
, slice_pitch
);
2170 if (data
->buffer_object
)
2172 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER
, data
->buffer_object
));
2173 checkGLcall("glBindBuffer");
2176 GL_EXTCALL(glTexSubImage3D(GL_TEXTURE_3D
, level
, x
, y
, z
,
2177 update_w
, update_h
, update_d
, format
->glFormat
, format
->glType
, mem
));
2178 checkGLcall("glTexSubImage3D");
2180 if (data
->buffer_object
)
2182 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER
, 0));
2183 checkGLcall("glBindBuffer");
2186 HeapFree(GetProcessHeap(), 0, converted_mem
);
2189 /* Context activation is done by the caller. */
2190 static void texture3d_download_data(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
,
2191 const struct wined3d_context
*context
, const struct wined3d_bo_address
*data
)
2193 const struct wined3d_format
*format
= texture
->resource
.format
;
2194 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2196 if (format
->convert
)
2198 FIXME("Attempting to download a converted volume, format %s.\n",
2199 debug_d3dformat(format
->id
));
2203 if (data
->buffer_object
)
2205 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER
, data
->buffer_object
));
2206 checkGLcall("glBindBuffer");
2209 gl_info
->gl_ops
.gl
.p_glGetTexImage(GL_TEXTURE_3D
, sub_resource_idx
,
2210 format
->glFormat
, format
->glType
, data
->addr
);
2211 checkGLcall("glGetTexImage");
2213 if (data
->buffer_object
)
2215 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER
, 0));
2216 checkGLcall("glBindBuffer");
2221 /* Context activation is done by the caller. */
2222 static void texture3d_srgb_transfer(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
,
2223 struct wined3d_context
*context
, BOOL dest_is_srgb
)
2225 struct wined3d_texture_sub_resource
*sub_resource
= &texture
->sub_resources
[sub_resource_idx
];
2226 unsigned int row_pitch
, slice_pitch
;
2227 struct wined3d_bo_address data
;
2229 /* Optimisations are possible, but the effort should be put into either
2230 * implementing EXT_SRGB_DECODE in the driver or finding out why we
2231 * picked the wrong copy for the original upload and fixing that.
2233 * Also keep in mind that we want to avoid using resource.heap_memory
2234 * for DEFAULT pool surfaces. */
2235 WARN_(d3d_perf
)("Performing slow rgb/srgb volume transfer.\n");
2236 data
.buffer_object
= 0;
2237 if (!(data
.addr
= HeapAlloc(GetProcessHeap(), 0, sub_resource
->size
)))
2240 wined3d_texture_get_pitch(texture
, sub_resource_idx
, &row_pitch
, &slice_pitch
);
2241 wined3d_texture_bind_and_dirtify(texture
, context
, !dest_is_srgb
);
2242 texture3d_download_data(texture
, sub_resource_idx
, context
, &data
);
2243 wined3d_texture_bind_and_dirtify(texture
, context
, dest_is_srgb
);
2244 texture3d_upload_data(texture
, sub_resource_idx
, context
,
2245 NULL
, wined3d_const_bo_address(&data
), row_pitch
, slice_pitch
);
2247 HeapFree(GetProcessHeap(), 0, data
.addr
);
2250 /* Context activation is done by the caller. */
2251 static BOOL
texture3d_load_location(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
,
2252 struct wined3d_context
*context
, DWORD location
)
2254 struct wined3d_texture_sub_resource
*sub_resource
= &texture
->sub_resources
[sub_resource_idx
];
2255 DWORD required_access
= wined3d_resource_access_from_location(location
);
2256 unsigned int row_pitch
, slice_pitch
;
2258 TRACE("texture %p, sub_resource_idx %u, context %p, location %s.\n",
2259 texture
, sub_resource_idx
, context
, wined3d_debug_location(location
));
2261 TRACE("Current resource location %s.\n", wined3d_debug_location(sub_resource
->locations
));
2263 if ((sub_resource
->locations
& location
) == location
)
2265 TRACE("Location(s) already up to date.\n");
2269 if ((texture
->resource
.access_flags
& required_access
) != required_access
)
2271 ERR("Operation requires %#x access, but volume only has %#x.\n",
2272 required_access
, texture
->resource
.access_flags
);
2276 if (!wined3d_texture_prepare_location(texture
, sub_resource_idx
, context
, location
))
2279 if (sub_resource
->locations
& WINED3D_LOCATION_DISCARDED
)
2281 TRACE("Volume previously discarded, nothing to do.\n");
2282 wined3d_texture_validate_location(texture
, sub_resource_idx
, location
);
2283 wined3d_texture_invalidate_location(texture
, sub_resource_idx
, WINED3D_LOCATION_DISCARDED
);
2289 case WINED3D_LOCATION_TEXTURE_RGB
:
2290 case WINED3D_LOCATION_TEXTURE_SRGB
:
2291 if (sub_resource
->locations
& WINED3D_LOCATION_SYSMEM
)
2293 struct wined3d_const_bo_address data
= {0, texture
->resource
.heap_memory
};
2294 data
.addr
+= sub_resource
->offset
;
2295 wined3d_texture_bind_and_dirtify(texture
, context
,
2296 location
== WINED3D_LOCATION_TEXTURE_SRGB
);
2297 wined3d_texture_get_pitch(texture
, sub_resource_idx
, &row_pitch
, &slice_pitch
);
2298 texture3d_upload_data(texture
, sub_resource_idx
, context
, NULL
, &data
, row_pitch
, slice_pitch
);
2300 else if (sub_resource
->locations
& WINED3D_LOCATION_BUFFER
)
2302 struct wined3d_const_bo_address data
= {sub_resource
->buffer_object
, NULL
};
2303 wined3d_texture_bind_and_dirtify(texture
, context
,
2304 location
== WINED3D_LOCATION_TEXTURE_SRGB
);
2305 wined3d_texture_get_pitch(texture
, sub_resource_idx
, &row_pitch
, &slice_pitch
);
2306 texture3d_upload_data(texture
, sub_resource_idx
, context
, NULL
, &data
, row_pitch
, slice_pitch
);
2308 else if (sub_resource
->locations
& WINED3D_LOCATION_TEXTURE_RGB
)
2310 texture3d_srgb_transfer(texture
, sub_resource_idx
, context
, TRUE
);
2312 else if (sub_resource
->locations
& WINED3D_LOCATION_TEXTURE_SRGB
)
2314 texture3d_srgb_transfer(texture
, sub_resource_idx
, context
, FALSE
);
2318 FIXME("Implement texture loading from %s.\n", wined3d_debug_location(sub_resource
->locations
));
2323 case WINED3D_LOCATION_SYSMEM
:
2324 if (sub_resource
->locations
& (WINED3D_LOCATION_TEXTURE_RGB
| WINED3D_LOCATION_TEXTURE_SRGB
))
2326 struct wined3d_bo_address data
= {0, texture
->resource
.heap_memory
};
2328 data
.addr
+= sub_resource
->offset
;
2329 if (sub_resource
->locations
& WINED3D_LOCATION_TEXTURE_RGB
)
2330 wined3d_texture_bind_and_dirtify(texture
, context
, FALSE
);
2332 wined3d_texture_bind_and_dirtify(texture
, context
, TRUE
);
2334 texture3d_download_data(texture
, sub_resource_idx
, context
, &data
);
2335 ++texture
->download_count
;
2339 FIXME("Implement WINED3D_LOCATION_SYSMEM loading from %s.\n",
2340 wined3d_debug_location(sub_resource
->locations
));
2345 case WINED3D_LOCATION_BUFFER
:
2346 if (sub_resource
->locations
& (WINED3D_LOCATION_TEXTURE_RGB
| WINED3D_LOCATION_TEXTURE_SRGB
))
2348 struct wined3d_bo_address data
= {sub_resource
->buffer_object
, NULL
};
2350 if (sub_resource
->locations
& WINED3D_LOCATION_TEXTURE_RGB
)
2351 wined3d_texture_bind_and_dirtify(texture
, context
, FALSE
);
2353 wined3d_texture_bind_and_dirtify(texture
, context
, TRUE
);
2355 texture3d_download_data(texture
, sub_resource_idx
, context
, &data
);
2359 FIXME("Implement WINED3D_LOCATION_BUFFER loading from %s.\n",
2360 wined3d_debug_location(sub_resource
->locations
));
2366 FIXME("Implement %s loading from %s.\n", wined3d_debug_location(location
),
2367 wined3d_debug_location(sub_resource
->locations
));
2372 wined3d_texture_validate_location(texture
, sub_resource_idx
, location
);
2377 static void texture3d_prepare_texture(struct wined3d_texture
*texture
, struct wined3d_context
*context
, BOOL srgb
)
2379 const struct wined3d_format
*format
= texture
->resource
.format
;
2380 GLenum internal
= srgb
? format
->glGammaInternal
: format
->glInternal
;
2381 unsigned int sub_count
= texture
->level_count
* texture
->layer_count
;
2382 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2385 wined3d_texture_bind_and_dirtify(texture
, context
, srgb
);
2387 if (wined3d_texture_use_immutable_storage(texture
, gl_info
))
2389 GL_EXTCALL(glTexStorage3D(GL_TEXTURE_3D
, texture
->level_count
, internal
,
2390 wined3d_texture_get_level_width(texture
, 0),
2391 wined3d_texture_get_level_height(texture
, 0),
2392 wined3d_texture_get_level_depth(texture
, 0)));
2393 checkGLcall("glTexStorage3D");
2397 for (i
= 0; i
< sub_count
; ++i
)
2399 GL_EXTCALL(glTexImage3D(GL_TEXTURE_3D
, i
, internal
,
2400 wined3d_texture_get_level_width(texture
, i
),
2401 wined3d_texture_get_level_height(texture
, i
),
2402 wined3d_texture_get_level_depth(texture
, i
),
2403 0, format
->glFormat
, format
->glType
, NULL
));
2404 checkGLcall("glTexImage3D");
2409 static void texture3d_cleanup_sub_resources(struct wined3d_texture
*texture
)
2413 static const struct wined3d_texture_ops texture3d_ops
=
2415 texture3d_upload_data
,
2416 texture3d_load_location
,
2417 texture3d_prepare_texture
,
2418 texture3d_cleanup_sub_resources
,
2421 BOOL
wined3d_texture_check_block_align(const struct wined3d_texture
*texture
,
2422 unsigned int level
, const struct wined3d_box
*box
)
2424 const struct wined3d_format
*format
= texture
->resource
.format
;
2425 unsigned int height
= wined3d_texture_get_level_height(texture
, level
);
2426 unsigned int width
= wined3d_texture_get_level_width(texture
, level
);
2427 unsigned int width_mask
, height_mask
;
2429 if ((box
->left
>= box
->right
)
2430 || (box
->top
>= box
->bottom
)
2431 || (box
->right
> width
)
2432 || (box
->bottom
> height
))
2435 /* This assumes power of two block sizes, but NPOT block sizes would be
2438 * This also assumes that the format's block depth is 1. */
2439 width_mask
= format
->block_width
- 1;
2440 height_mask
= format
->block_height
- 1;
2442 if ((box
->left
& width_mask
) || (box
->top
& height_mask
)
2443 || (box
->right
& width_mask
&& box
->right
!= width
)
2444 || (box
->bottom
& height_mask
&& box
->bottom
!= height
))
2450 static HRESULT
volumetexture_init(struct wined3d_texture
*texture
, const struct wined3d_resource_desc
*desc
,
2451 UINT layer_count
, UINT level_count
, struct wined3d_device
*device
, void *parent
,
2452 const struct wined3d_parent_ops
*parent_ops
)
2454 struct wined3d_device_parent
*device_parent
= device
->device_parent
;
2455 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
2459 if (layer_count
!= 1)
2461 ERR("Invalid layer count for volume texture.\n");
2462 return E_INVALIDARG
;
2465 /* TODO: It should only be possible to create textures for formats
2466 * that are reported as supported. */
2467 if (WINED3DFMT_UNKNOWN
>= desc
->format
)
2469 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture
);
2470 return WINED3DERR_INVALIDCALL
;
2473 if (!gl_info
->supported
[EXT_TEXTURE3D
])
2475 WARN("(%p) : Texture cannot be created - no volume texture support.\n", texture
);
2476 return WINED3DERR_INVALIDCALL
;
2479 /* Calculate levels for mip mapping. */
2480 if (desc
->usage
& WINED3DUSAGE_AUTOGENMIPMAP
)
2482 if (!gl_info
->supported
[SGIS_GENERATE_MIPMAP
])
2484 WARN("No mipmap generation support, returning D3DERR_INVALIDCALL.\n");
2485 return WINED3DERR_INVALIDCALL
;
2488 if (level_count
!= 1)
2490 WARN("WINED3DUSAGE_AUTOGENMIPMAP is set, and level count != 1, returning D3DERR_INVALIDCALL.\n");
2491 return WINED3DERR_INVALIDCALL
;
2495 if (desc
->usage
& WINED3DUSAGE_DYNAMIC
&& (desc
->pool
== WINED3D_POOL_MANAGED
2496 || desc
->pool
== WINED3D_POOL_SCRATCH
))
2498 WARN("Attempted to create a DYNAMIC texture in pool %s.\n", debug_d3dpool(desc
->pool
));
2499 return WINED3DERR_INVALIDCALL
;
2502 if (!gl_info
->supported
[ARB_TEXTURE_NON_POWER_OF_TWO
])
2504 UINT pow2_w
, pow2_h
, pow2_d
;
2506 while (pow2_w
< desc
->width
)
2509 while (pow2_h
< desc
->height
)
2512 while (pow2_d
< desc
->depth
)
2515 if (pow2_w
!= desc
->width
|| pow2_h
!= desc
->height
|| pow2_d
!= desc
->depth
)
2517 if (desc
->pool
== WINED3D_POOL_SCRATCH
)
2519 WARN("Creating a scratch NPOT volume texture despite lack of HW support.\n");
2523 WARN("Attempted to create a NPOT volume texture (%u, %u, %u) without GL support.\n",
2524 desc
->width
, desc
->height
, desc
->depth
);
2525 return WINED3DERR_INVALIDCALL
;
2530 if (FAILED(hr
= wined3d_texture_init(texture
, &texture3d_ops
, 1, level_count
, desc
,
2531 0, device
, parent
, parent_ops
, &texture_resource_ops
)))
2533 WARN("Failed to initialize texture, returning %#x.\n", hr
);
2537 texture
->pow2_matrix
[0] = 1.0f
;
2538 texture
->pow2_matrix
[5] = 1.0f
;
2539 texture
->pow2_matrix
[10] = 1.0f
;
2540 texture
->pow2_matrix
[15] = 1.0f
;
2541 texture
->target
= GL_TEXTURE_3D
;
2543 if (wined3d_texture_use_pbo(texture
, gl_info
))
2545 wined3d_resource_free_sysmem(&texture
->resource
);
2546 texture
->resource
.map_binding
= WINED3D_LOCATION_BUFFER
;
2549 /* Generate all the surfaces. */
2550 for (i
= 0; i
< texture
->level_count
; ++i
)
2552 struct wined3d_texture_sub_resource
*sub_resource
;
2554 sub_resource
= &texture
->sub_resources
[i
];
2555 sub_resource
->locations
= WINED3D_LOCATION_DISCARDED
;
2557 if (FAILED(hr
= device_parent
->ops
->volume_created(device_parent
,
2558 texture
, i
, &sub_resource
->parent
, &sub_resource
->parent_ops
)))
2560 WARN("Failed to create volume parent, hr %#x.\n", hr
);
2561 sub_resource
->parent
= NULL
;
2562 wined3d_texture_cleanup_sync(texture
);
2566 TRACE("parent %p, parent_ops %p.\n", parent
, parent_ops
);
2568 TRACE("Created volume level %u.\n", i
);
2574 HRESULT CDECL
wined3d_texture_blt(struct wined3d_texture
*dst_texture
, unsigned int dst_sub_resource_idx
,
2575 const RECT
*dst_rect
, struct wined3d_texture
*src_texture
, unsigned int src_sub_resource_idx
,
2576 const RECT
*src_rect
, DWORD flags
, const struct wined3d_blt_fx
*fx
, enum wined3d_texture_filter_type filter
)
2578 struct wined3d_texture_sub_resource
*dst_resource
, *src_resource
= NULL
;
2580 TRACE("dst_texture %p, dst_sub_resource_idx %u, dst_rect %s, src_texture %p, "
2581 "src_sub_resource_idx %u, src_rect %s, flags %#x, fx %p, filter %s.\n",
2582 dst_texture
, dst_sub_resource_idx
, wine_dbgstr_rect(dst_rect
), src_texture
,
2583 src_sub_resource_idx
, wine_dbgstr_rect(src_rect
), flags
, fx
, debug_d3dtexturefiltertype(filter
));
2585 if (!(dst_resource
= wined3d_texture_get_sub_resource(dst_texture
, dst_sub_resource_idx
))
2586 || dst_texture
->resource
.type
!= WINED3D_RTYPE_TEXTURE_2D
)
2587 return WINED3DERR_INVALIDCALL
;
2591 if (!(src_resource
= wined3d_texture_get_sub_resource(src_texture
, src_sub_resource_idx
))
2592 || src_texture
->resource
.type
!= WINED3D_RTYPE_TEXTURE_2D
)
2593 return WINED3DERR_INVALIDCALL
;
2596 return wined3d_surface_blt(dst_resource
->u
.surface
, dst_rect
,
2597 src_resource
? src_resource
->u
.surface
: NULL
, src_rect
, flags
, fx
, filter
);
2600 HRESULT CDECL
wined3d_texture_get_overlay_position(const struct wined3d_texture
*texture
,
2601 unsigned int sub_resource_idx
, LONG
*x
, LONG
*y
)
2603 struct wined3d_surface
*surface
;
2605 TRACE("texture %p, sub_resource_idx %u, x %p, y %p.\n", texture
, sub_resource_idx
, x
, y
);
2607 if (!(texture
->resource
.usage
& WINED3DUSAGE_OVERLAY
) || texture
->resource
.type
!= WINED3D_RTYPE_TEXTURE_2D
2608 || sub_resource_idx
>= texture
->level_count
* texture
->layer_count
)
2610 WARN("Invalid sub-resource specified.\n");
2611 return WINEDDERR_NOTAOVERLAYSURFACE
;
2614 surface
= texture
->sub_resources
[sub_resource_idx
].u
.surface
;
2615 if (!surface
->overlay_dest
)
2617 TRACE("Overlay not visible.\n");
2620 return WINEDDERR_OVERLAYNOTVISIBLE
;
2623 *x
= surface
->overlay_destrect
.left
;
2624 *y
= surface
->overlay_destrect
.top
;
2626 TRACE("Returning position %d, %d.\n", *x
, *y
);
2631 HRESULT CDECL
wined3d_texture_set_overlay_position(struct wined3d_texture
*texture
,
2632 unsigned int sub_resource_idx
, LONG x
, LONG y
)
2634 struct wined3d_texture_sub_resource
*sub_resource
;
2635 struct wined3d_surface
*surface
;
2638 TRACE("texture %p, sub_resource_idx %u, x %d, y %d.\n", texture
, sub_resource_idx
, x
, y
);
2640 if (!(texture
->resource
.usage
& WINED3DUSAGE_OVERLAY
) || texture
->resource
.type
!= WINED3D_RTYPE_TEXTURE_2D
2641 || !(sub_resource
= wined3d_texture_get_sub_resource(texture
, sub_resource_idx
)))
2643 WARN("Invalid sub-resource specified.\n");
2644 return WINEDDERR_NOTAOVERLAYSURFACE
;
2647 surface
= sub_resource
->u
.surface
;
2648 w
= surface
->overlay_destrect
.right
- surface
->overlay_destrect
.left
;
2649 h
= surface
->overlay_destrect
.bottom
- surface
->overlay_destrect
.top
;
2650 SetRect(&surface
->overlay_destrect
, x
, y
, x
+ w
, y
+ h
);
2655 HRESULT CDECL
wined3d_texture_update_overlay(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
,
2656 const RECT
*src_rect
, struct wined3d_texture
*dst_texture
, unsigned int dst_sub_resource_idx
,
2657 const RECT
*dst_rect
, DWORD flags
)
2659 struct wined3d_texture_sub_resource
*sub_resource
, *dst_sub_resource
;
2660 struct wined3d_surface
*surface
, *dst_surface
;
2662 TRACE("texture %p, sub_resource_idx %u, src_rect %s, dst_texture %p, "
2663 "dst_sub_resource_idx %u, dst_rect %s, flags %#x.\n",
2664 texture
, sub_resource_idx
, wine_dbgstr_rect(src_rect
), dst_texture
,
2665 dst_sub_resource_idx
, wine_dbgstr_rect(dst_rect
), flags
);
2667 if (!(texture
->resource
.usage
& WINED3DUSAGE_OVERLAY
) || texture
->resource
.type
!= WINED3D_RTYPE_TEXTURE_2D
2668 || !(sub_resource
= wined3d_texture_get_sub_resource(texture
, sub_resource_idx
)))
2670 WARN("Invalid sub-resource specified.\n");
2671 return WINEDDERR_NOTAOVERLAYSURFACE
;
2674 if (!dst_texture
|| dst_texture
->resource
.type
!= WINED3D_RTYPE_TEXTURE_2D
2675 || !(dst_sub_resource
= wined3d_texture_get_sub_resource(dst_texture
, dst_sub_resource_idx
)))
2677 WARN("Invalid destination sub-resource specified.\n");
2678 return WINED3DERR_INVALIDCALL
;
2681 surface
= sub_resource
->u
.surface
;
2683 surface
->overlay_srcrect
= *src_rect
;
2685 SetRect(&surface
->overlay_srcrect
, 0, 0,
2686 wined3d_texture_get_level_width(texture
, surface
->texture_level
),
2687 wined3d_texture_get_level_height(texture
, surface
->texture_level
));
2689 dst_surface
= dst_sub_resource
->u
.surface
;
2691 surface
->overlay_destrect
= *dst_rect
;
2693 SetRect(&surface
->overlay_destrect
, 0, 0,
2694 wined3d_texture_get_level_width(dst_texture
, dst_surface
->texture_level
),
2695 wined3d_texture_get_level_height(dst_texture
, dst_surface
->texture_level
));
2697 if (surface
->overlay_dest
&& (surface
->overlay_dest
!= dst_surface
|| flags
& WINEDDOVER_HIDE
))
2699 surface
->overlay_dest
= NULL
;
2700 list_remove(&surface
->overlay_entry
);
2703 if (flags
& WINEDDOVER_SHOW
)
2705 if (surface
->overlay_dest
!= dst_surface
)
2707 surface
->overlay_dest
= dst_surface
;
2708 list_add_tail(&dst_surface
->overlays
, &surface
->overlay_entry
);
2711 else if (flags
& WINEDDOVER_HIDE
)
2713 /* Tests show that the rectangles are erased on hide. */
2714 SetRectEmpty(&surface
->overlay_srcrect
);
2715 SetRectEmpty(&surface
->overlay_destrect
);
2716 surface
->overlay_dest
= NULL
;
2722 void * CDECL
wined3d_texture_get_sub_resource_parent(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
)
2724 unsigned int sub_count
= texture
->level_count
* texture
->layer_count
;
2726 TRACE("texture %p, sub_resource_idx %u.\n", texture
, sub_resource_idx
);
2728 if (sub_resource_idx
>= sub_count
)
2730 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx
, sub_count
);
2734 return texture
->sub_resources
[sub_resource_idx
].parent
;
2737 void CDECL
wined3d_texture_set_sub_resource_parent(struct wined3d_texture
*texture
,
2738 unsigned int sub_resource_idx
, void *parent
)
2740 unsigned int sub_count
= texture
->level_count
* texture
->layer_count
;
2742 TRACE("texture %p, sub_resource_idx %u, parent %p.\n", texture
, sub_resource_idx
, parent
);
2744 if (sub_resource_idx
>= sub_count
)
2746 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx
, sub_count
);
2750 texture
->sub_resources
[sub_resource_idx
].parent
= parent
;
2753 HRESULT CDECL
wined3d_texture_get_sub_resource_desc(const struct wined3d_texture
*texture
,
2754 unsigned int sub_resource_idx
, struct wined3d_sub_resource_desc
*desc
)
2756 unsigned int sub_count
= texture
->level_count
* texture
->layer_count
;
2757 const struct wined3d_resource
*resource
;
2758 unsigned int level_idx
;
2760 TRACE("texture %p, sub_resource_idx %u, desc %p.\n", texture
, sub_resource_idx
, desc
);
2762 if (sub_resource_idx
>= sub_count
)
2764 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx
, sub_count
);
2765 return WINED3DERR_INVALIDCALL
;
2768 resource
= &texture
->resource
;
2769 desc
->format
= resource
->format
->id
;
2770 desc
->multisample_type
= resource
->multisample_type
;
2771 desc
->multisample_quality
= resource
->multisample_quality
;
2772 desc
->usage
= resource
->usage
;
2773 desc
->pool
= resource
->pool
;
2775 level_idx
= sub_resource_idx
% texture
->level_count
;
2776 desc
->width
= wined3d_texture_get_level_width(texture
, level_idx
);
2777 desc
->height
= wined3d_texture_get_level_height(texture
, level_idx
);
2778 desc
->depth
= wined3d_texture_get_level_depth(texture
, level_idx
);
2779 desc
->size
= texture
->sub_resources
[sub_resource_idx
].size
;
2784 HRESULT CDECL
wined3d_texture_create(struct wined3d_device
*device
, const struct wined3d_resource_desc
*desc
,
2785 UINT layer_count
, UINT level_count
, DWORD flags
, const struct wined3d_sub_resource_data
*data
,
2786 void *parent
, const struct wined3d_parent_ops
*parent_ops
, struct wined3d_texture
**texture
)
2788 struct wined3d_texture
*object
;
2791 TRACE("device %p, desc %p, layer_count %u, level_count %u, flags %#x, data %p, "
2792 "parent %p, parent_ops %p, texture %p.\n",
2793 device
, desc
, layer_count
, level_count
, flags
, data
, parent
, parent_ops
, texture
);
2797 WARN("Invalid layer count.\n");
2798 return E_INVALIDARG
;
2800 if ((desc
->usage
& WINED3DUSAGE_LEGACY_CUBEMAP
) && layer_count
!= 6)
2802 ERR("Invalid layer count %u for legacy cubemap.\n", layer_count
);
2808 WARN("Invalid level count.\n");
2809 return WINED3DERR_INVALIDCALL
;
2812 if (desc
->multisample_type
!= WINED3D_MULTISAMPLE_NONE
)
2814 const struct wined3d_format
*format
= wined3d_get_format(&device
->adapter
->gl_info
,
2815 desc
->format
, desc
->usage
);
2817 if (desc
->multisample_type
== WINED3D_MULTISAMPLE_NON_MASKABLE
2818 && desc
->multisample_quality
>= wined3d_popcount(format
->multisample_types
))
2820 WARN("Unsupported quality level %u requested for WINED3D_MULTISAMPLE_NON_MASKABLE.\n",
2821 desc
->multisample_quality
);
2822 return WINED3DERR_NOTAVAILABLE
;
2824 if (desc
->multisample_type
!= WINED3D_MULTISAMPLE_NON_MASKABLE
2825 && (!(format
->multisample_types
& 1u << (desc
->multisample_type
- 1))
2826 || desc
->multisample_quality
))
2828 WARN("Unsupported multisample type %u quality %u requested.\n", desc
->multisample_type
,
2829 desc
->multisample_quality
);
2830 return WINED3DERR_NOTAVAILABLE
;
2834 if (!(object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
2835 FIELD_OFFSET(struct wined3d_texture
, sub_resources
[level_count
* layer_count
]))))
2836 return E_OUTOFMEMORY
;
2838 switch (desc
->resource_type
)
2840 case WINED3D_RTYPE_TEXTURE_2D
:
2841 hr
= texture_init(object
, desc
, layer_count
, level_count
, flags
, device
, parent
, parent_ops
);
2844 case WINED3D_RTYPE_TEXTURE_3D
:
2845 hr
= volumetexture_init(object
, desc
, layer_count
, level_count
, device
, parent
, parent_ops
);
2849 ERR("Invalid resource type %s.\n", debug_d3dresourcetype(desc
->resource_type
));
2850 hr
= WINED3DERR_INVALIDCALL
;
2856 WARN("Failed to initialize texture, returning %#x.\n", hr
);
2857 HeapFree(GetProcessHeap(), 0, object
);
2861 /* FIXME: We'd like to avoid ever allocating system memory for the texture
2865 unsigned int sub_count
= level_count
* layer_count
;
2868 for (i
= 0; i
< sub_count
; ++i
)
2872 WARN("Invalid sub-resource data specified for sub-resource %u.\n", i
);
2873 wined3d_texture_cleanup_sync(object
);
2874 HeapFree(GetProcessHeap(), 0, object
);
2875 return E_INVALIDARG
;
2879 for (i
= 0; i
< sub_count
; ++i
)
2881 wined3d_device_update_sub_resource(device
, &object
->resource
,
2882 i
, NULL
, data
[i
].data
, data
[i
].row_pitch
, data
[i
].slice_pitch
);
2886 TRACE("Created texture %p.\n", object
);
2892 HRESULT CDECL
wined3d_texture_get_dc(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
, HDC
*dc
)
2894 struct wined3d_device
*device
= texture
->resource
.device
;
2895 struct wined3d_texture_sub_resource
*sub_resource
;
2896 struct wined3d_context
*context
= NULL
;
2897 struct wined3d_surface
*surface
;
2898 HRESULT hr
= WINED3D_OK
;
2900 TRACE("texture %p, sub_resource_idx %u, dc %p.\n", texture
, sub_resource_idx
, dc
);
2902 if (!(texture
->flags
& WINED3D_TEXTURE_GET_DC
))
2904 WARN("Texture does not support GetDC\n");
2905 /* Don't touch the DC */
2906 return WINED3DERR_INVALIDCALL
;
2909 if (!(sub_resource
= wined3d_texture_get_sub_resource(texture
, sub_resource_idx
)))
2910 return WINED3DERR_INVALIDCALL
;
2912 if (texture
->resource
.type
!= WINED3D_RTYPE_TEXTURE_2D
)
2914 WARN("Not supported on %s resources.\n", debug_d3dresourcetype(texture
->resource
.type
));
2915 return WINED3DERR_INVALIDCALL
;
2918 surface
= sub_resource
->u
.surface
;
2920 if (texture
->resource
.map_count
&& !(texture
->flags
& WINED3D_TEXTURE_GET_DC_LENIENT
))
2921 return WINED3DERR_INVALIDCALL
;
2923 if (device
->d3d_initialized
)
2924 context
= context_acquire(device
, NULL
);
2926 wined3d_texture_load_location(texture
, sub_resource_idx
, context
, texture
->resource
.map_binding
);
2927 wined3d_texture_invalidate_location(texture
, sub_resource_idx
, ~texture
->resource
.map_binding
);
2930 hr
= wined3d_surface_create_dc(surface
);
2932 context_release(context
);
2934 return WINED3DERR_INVALIDCALL
;
2936 if (!(texture
->flags
& WINED3D_TEXTURE_GET_DC_LENIENT
))
2937 texture
->flags
|= WINED3D_TEXTURE_DC_IN_USE
;
2938 ++texture
->resource
.map_count
;
2939 ++sub_resource
->map_count
;
2942 TRACE("Returning dc %p.\n", *dc
);
2947 HRESULT CDECL
wined3d_texture_release_dc(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
, HDC dc
)
2949 struct wined3d_device
*device
= texture
->resource
.device
;
2950 struct wined3d_texture_sub_resource
*sub_resource
;
2951 struct wined3d_surface
*surface
;
2953 TRACE("texture %p, sub_resource_idx %u, dc %p.\n", texture
, sub_resource_idx
, dc
);
2955 if (!(sub_resource
= wined3d_texture_get_sub_resource(texture
, sub_resource_idx
)))
2956 return WINED3DERR_INVALIDCALL
;
2958 if (texture
->resource
.type
!= WINED3D_RTYPE_TEXTURE_2D
)
2960 WARN("Not supported on %s resources.\n", debug_d3dresourcetype(texture
->resource
.type
));
2961 return WINED3DERR_INVALIDCALL
;
2964 surface
= sub_resource
->u
.surface
;
2966 if (!(texture
->flags
& (WINED3D_TEXTURE_GET_DC_LENIENT
| WINED3D_TEXTURE_DC_IN_USE
)))
2967 return WINED3DERR_INVALIDCALL
;
2969 if (surface
->dc
!= dc
)
2971 WARN("Application tries to release invalid DC %p, surface DC is %p.\n", dc
, surface
->dc
);
2972 return WINED3DERR_INVALIDCALL
;
2975 if (!(texture
->resource
.usage
& WINED3DUSAGE_OWNDC
) && !(device
->wined3d
->flags
& WINED3D_NO3D
))
2976 wined3d_surface_destroy_dc(surface
);
2978 --sub_resource
->map_count
;
2979 if (!--texture
->resource
.map_count
&& texture
->update_map_binding
)
2980 wined3d_texture_update_map_binding(texture
);
2981 if (!(texture
->flags
& WINED3D_TEXTURE_GET_DC_LENIENT
))
2982 texture
->flags
&= ~WINED3D_TEXTURE_DC_IN_USE
;