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 (texture
== swapchain
->front_buffer
)
65 TRACE("Returning GL_FRONT.\n");
69 if (texture
== swapchain
->back_buffers
[0])
71 TRACE("Returning GL_BACK.\n");
75 FIXME("Higher back buffer, returning GL_BACK.\n");
79 static DWORD
wined3d_resource_access_from_location(DWORD location
)
83 case WINED3D_LOCATION_DISCARDED
:
86 case WINED3D_LOCATION_SYSMEM
:
87 case WINED3D_LOCATION_USER_MEMORY
:
88 return WINED3D_RESOURCE_ACCESS_CPU
;
90 case WINED3D_LOCATION_BUFFER
:
91 case WINED3D_LOCATION_DRAWABLE
:
92 case WINED3D_LOCATION_TEXTURE_RGB
:
93 case WINED3D_LOCATION_TEXTURE_SRGB
:
94 case WINED3D_LOCATION_RB_MULTISAMPLE
:
95 case WINED3D_LOCATION_RB_RESOLVED
:
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
;
158 DWORD previous_locations
;
160 TRACE("texture %p, sub_resource_idx %u, location %s.\n",
161 texture
, sub_resource_idx
, wined3d_debug_location(location
));
163 if (location
& (WINED3D_LOCATION_TEXTURE_RGB
| WINED3D_LOCATION_TEXTURE_SRGB
))
164 wined3d_texture_set_dirty(texture
);
166 sub_resource
= &texture
->sub_resources
[sub_resource_idx
];
167 previous_locations
= sub_resource
->locations
;
168 sub_resource
->locations
&= ~location
;
169 if (previous_locations
!= WINED3D_LOCATION_SYSMEM
&& sub_resource
->locations
== WINED3D_LOCATION_SYSMEM
)
170 ++texture
->sysmem_count
;
172 TRACE("New locations flags are %s.\n", wined3d_debug_location(sub_resource
->locations
));
174 if (!sub_resource
->locations
)
175 ERR("Sub-resource %u of texture %p does not have any up to date location.\n",
176 sub_resource_idx
, texture
);
179 static BOOL
wined3d_texture_copy_sysmem_location(struct wined3d_texture
*texture
,
180 unsigned int sub_resource_idx
, struct wined3d_context
*context
, DWORD location
)
182 unsigned int size
= texture
->sub_resources
[sub_resource_idx
].size
;
183 struct wined3d_device
*device
= texture
->resource
.device
;
184 const struct wined3d_gl_info
*gl_info
;
185 struct wined3d_bo_address dst
, src
;
187 if (!wined3d_texture_prepare_location(texture
, sub_resource_idx
, context
, location
))
190 wined3d_texture_get_memory(texture
, sub_resource_idx
, &dst
, location
);
191 wined3d_texture_get_memory(texture
, sub_resource_idx
, &src
,
192 texture
->sub_resources
[sub_resource_idx
].locations
);
194 if (dst
.buffer_object
)
196 context
= context_acquire(device
, NULL
, 0);
197 gl_info
= context
->gl_info
;
198 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER
, dst
.buffer_object
));
199 GL_EXTCALL(glBufferSubData(GL_PIXEL_UNPACK_BUFFER
, 0, size
, src
.addr
));
200 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER
, 0));
201 checkGLcall("PBO upload");
202 context_release(context
);
206 if (src
.buffer_object
)
208 context
= context_acquire(device
, NULL
, 0);
209 gl_info
= context
->gl_info
;
210 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER
, src
.buffer_object
));
211 GL_EXTCALL(glGetBufferSubData(GL_PIXEL_PACK_BUFFER
, 0, size
, dst
.addr
));
212 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER
, 0));
213 checkGLcall("PBO download");
214 context_release(context
);
218 memcpy(dst
.addr
, src
.addr
, size
);
222 /* Context activation is done by the caller. Context may be NULL in
223 * WINED3D_NO3D mode. */
224 BOOL
wined3d_texture_load_location(struct wined3d_texture
*texture
,
225 unsigned int sub_resource_idx
, struct wined3d_context
*context
, DWORD location
)
227 static const DWORD sysmem_locations
= WINED3D_LOCATION_SYSMEM
| WINED3D_LOCATION_USER_MEMORY
228 | WINED3D_LOCATION_BUFFER
;
229 DWORD current
= texture
->sub_resources
[sub_resource_idx
].locations
;
232 TRACE("texture %p, sub_resource_idx %u, context %p, location %s.\n",
233 texture
, sub_resource_idx
, context
, wined3d_debug_location(location
));
235 TRACE("Current resource location %s.\n", wined3d_debug_location(current
));
237 if (current
& location
)
239 TRACE("Location %s is already up to date.\n", wined3d_debug_location(location
));
245 DWORD required_access
= wined3d_resource_access_from_location(location
);
246 if ((texture
->resource
.access_flags
& required_access
) != required_access
)
247 WARN("Operation requires %#x access, but texture only has %#x.\n",
248 required_access
, texture
->resource
.access_flags
);
251 if (current
& WINED3D_LOCATION_DISCARDED
)
253 TRACE("Sub-resource previously discarded, nothing to do.\n");
254 if (!wined3d_texture_prepare_location(texture
, sub_resource_idx
, context
, location
))
256 wined3d_texture_validate_location(texture
, sub_resource_idx
, location
);
257 wined3d_texture_invalidate_location(texture
, sub_resource_idx
, WINED3D_LOCATION_DISCARDED
);
263 ERR("Sub-resource %u of texture %p does not have any up to date location.\n",
264 sub_resource_idx
, texture
);
265 wined3d_texture_validate_location(texture
, sub_resource_idx
, WINED3D_LOCATION_DISCARDED
);
266 return wined3d_texture_load_location(texture
, sub_resource_idx
, context
, location
);
269 if ((location
& sysmem_locations
) && (current
& sysmem_locations
))
270 ret
= wined3d_texture_copy_sysmem_location(texture
, sub_resource_idx
, context
, location
);
272 ret
= texture
->texture_ops
->texture_load_location(texture
, sub_resource_idx
, context
, location
);
275 wined3d_texture_validate_location(texture
, sub_resource_idx
, location
);
280 void wined3d_texture_get_memory(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
,
281 struct wined3d_bo_address
*data
, DWORD locations
)
283 struct wined3d_texture_sub_resource
*sub_resource
;
285 TRACE("texture %p, sub_resource_idx %u, data %p, locations %s.\n",
286 texture
, sub_resource_idx
, data
, wined3d_debug_location(locations
));
288 sub_resource
= &texture
->sub_resources
[sub_resource_idx
];
289 if (locations
& WINED3D_LOCATION_BUFFER
)
292 data
->buffer_object
= sub_resource
->buffer_object
;
295 if (locations
& WINED3D_LOCATION_USER_MEMORY
)
297 data
->addr
= texture
->user_memory
;
298 data
->buffer_object
= 0;
301 if (locations
& WINED3D_LOCATION_SYSMEM
)
303 data
->addr
= texture
->resource
.heap_memory
;
304 data
->addr
+= sub_resource
->offset
;
305 data
->buffer_object
= 0;
309 ERR("Unexpected locations %s.\n", wined3d_debug_location(locations
));
311 data
->buffer_object
= 0;
314 static HRESULT
wined3d_texture_init(struct wined3d_texture
*texture
, const struct wined3d_texture_ops
*texture_ops
,
315 UINT layer_count
, UINT level_count
, const struct wined3d_resource_desc
*desc
, DWORD flags
,
316 struct wined3d_device
*device
, void *parent
, const struct wined3d_parent_ops
*parent_ops
,
317 const struct wined3d_resource_ops
*resource_ops
)
319 unsigned int i
, j
, size
, offset
= 0;
320 const struct wined3d_format
*format
;
323 TRACE("texture %p, texture_ops %p, layer_count %u, level_count %u, resource_type %s, format %s, "
324 "multisample_type %#x, multisample_quality %#x, usage %s, pool %s, width %u, height %u, depth %u, "
325 "flags %#x, device %p, parent %p, parent_ops %p, resource_ops %p.\n",
326 texture
, texture_ops
, layer_count
, level_count
, debug_d3dresourcetype(desc
->resource_type
),
327 debug_d3dformat(desc
->format
), desc
->multisample_type
, desc
->multisample_quality
,
328 debug_d3dusage(desc
->usage
), debug_d3dpool(desc
->pool
), desc
->width
, desc
->height
, desc
->depth
,
329 flags
, device
, parent
, parent_ops
, resource_ops
);
331 if (!desc
->width
|| !desc
->height
|| !desc
->depth
)
332 return WINED3DERR_INVALIDCALL
;
334 format
= wined3d_get_format(&device
->adapter
->gl_info
, desc
->format
, desc
->usage
);
336 for (i
= 0; i
< layer_count
; ++i
)
338 for (j
= 0; j
< level_count
; ++j
)
340 unsigned int idx
= i
* level_count
+ j
;
342 size
= wined3d_format_calculate_size(format
, device
->surface_alignment
,
343 max(1, desc
->width
>> j
), max(1, desc
->height
>> j
), max(1, desc
->depth
>> j
));
344 texture
->sub_resources
[idx
].offset
= offset
;
345 texture
->sub_resources
[idx
].size
= size
;
348 offset
= (offset
+ (RESOURCE_ALIGNMENT
- 1)) & ~(RESOURCE_ALIGNMENT
- 1);
352 return WINED3DERR_INVALIDCALL
;
354 if (FAILED(hr
= resource_init(&texture
->resource
, device
, desc
->resource_type
, format
,
355 desc
->multisample_type
, desc
->multisample_quality
, desc
->usage
, desc
->pool
,
356 desc
->width
, desc
->height
, desc
->depth
, offset
, parent
, parent_ops
, resource_ops
)))
358 static unsigned int once
;
360 /* DXTn 3D textures are not supported. Do not write the ERR for them. */
361 if ((desc
->format
== WINED3DFMT_DXT1
|| desc
->format
== WINED3DFMT_DXT2
|| desc
->format
== WINED3DFMT_DXT3
362 || desc
->format
== WINED3DFMT_DXT4
|| desc
->format
== WINED3DFMT_DXT5
)
363 && !(format
->flags
[WINED3D_GL_RES_TYPE_TEX_2D
] & WINED3DFMT_FLAG_TEXTURE
)
364 && desc
->resource_type
!= WINED3D_RTYPE_TEXTURE_3D
&& !once
++)
365 ERR_(winediag
)("The application tried to create a DXTn texture, but the driver does not support them.\n");
367 WARN("Failed to initialize resource, returning %#x\n", hr
);
370 wined3d_resource_update_draw_binding(&texture
->resource
);
371 if ((flags
& WINED3D_TEXTURE_CREATE_MAPPABLE
) || desc
->format
== WINED3DFMT_D16_LOCKABLE
)
372 texture
->resource
.access_flags
|= WINED3D_RESOURCE_ACCESS_CPU
;
374 texture
->texture_ops
= texture_ops
;
376 texture
->layer_count
= layer_count
;
377 texture
->level_count
= level_count
;
378 texture
->filter_type
= (desc
->usage
& WINED3DUSAGE_AUTOGENMIPMAP
) ? WINED3D_TEXF_LINEAR
: WINED3D_TEXF_NONE
;
380 texture
->flags
|= WINED3D_TEXTURE_POW2_MAT_IDENT
| WINED3D_TEXTURE_NORMALIZED_COORDS
;
381 if (flags
& WINED3D_TEXTURE_CREATE_GET_DC_LENIENT
)
382 texture
->flags
|= WINED3D_TEXTURE_PIN_SYSMEM
| WINED3D_TEXTURE_GET_DC_LENIENT
;
383 if (flags
& (WINED3D_TEXTURE_CREATE_GET_DC
| WINED3D_TEXTURE_CREATE_GET_DC_LENIENT
))
384 texture
->flags
|= WINED3D_TEXTURE_GET_DC
;
385 if (flags
& WINED3D_TEXTURE_CREATE_DISCARD
)
386 texture
->flags
|= WINED3D_TEXTURE_DISCARD
;
387 if (flags
& WINED3D_TEXTURE_CREATE_GENERATE_MIPMAPS
)
389 if (~format
->flags
[WINED3D_GL_RES_TYPE_TEX_2D
]
390 & (WINED3DFMT_FLAG_RENDERTARGET
| WINED3DFMT_FLAG_FILTERING
))
391 WARN("Format doesn't support mipmaps generation, "
392 "ignoring WINED3D_TEXTURE_CREATE_GENERATE_MIPMAPS flag.\n");
394 texture
->flags
|= WINED3D_TEXTURE_GENERATE_MIPMAPS
;
400 /* Context activation is done by the caller. */
401 static void wined3d_texture_remove_buffer_object(struct wined3d_texture
*texture
,
402 unsigned int sub_resource_idx
, const struct wined3d_gl_info
*gl_info
)
404 GLuint
*buffer_object
= &texture
->sub_resources
[sub_resource_idx
].buffer_object
;
406 GL_EXTCALL(glDeleteBuffers(1, buffer_object
));
407 checkGLcall("glDeleteBuffers");
409 TRACE("Deleted buffer object %u for texture %p, sub-resource %u.\n",
410 *buffer_object
, texture
, sub_resource_idx
);
412 wined3d_texture_invalidate_location(texture
, sub_resource_idx
, WINED3D_LOCATION_BUFFER
);
416 static void wined3d_texture_update_map_binding(struct wined3d_texture
*texture
)
418 unsigned int sub_count
= texture
->level_count
* texture
->layer_count
;
419 const struct wined3d_device
*device
= texture
->resource
.device
;
420 DWORD map_binding
= texture
->update_map_binding
;
421 struct wined3d_context
*context
= NULL
;
424 if (device
->d3d_initialized
)
425 context
= context_acquire(device
, NULL
, 0);
427 for (i
= 0; i
< sub_count
; ++i
)
429 if (texture
->sub_resources
[i
].locations
== texture
->resource
.map_binding
430 && !wined3d_texture_load_location(texture
, i
, context
, map_binding
))
431 ERR("Failed to load location %s.\n", wined3d_debug_location(map_binding
));
432 if (texture
->resource
.map_binding
== WINED3D_LOCATION_BUFFER
)
433 wined3d_texture_remove_buffer_object(texture
, i
, context
->gl_info
);
437 context_release(context
);
439 texture
->resource
.map_binding
= map_binding
;
440 texture
->update_map_binding
= 0;
443 void wined3d_texture_set_map_binding(struct wined3d_texture
*texture
, DWORD map_binding
)
445 texture
->update_map_binding
= map_binding
;
446 if (!texture
->resource
.map_count
)
447 wined3d_texture_update_map_binding(texture
);
450 /* A GL context is provided by the caller */
451 static void gltexture_delete(struct wined3d_device
*device
, const struct wined3d_gl_info
*gl_info
,
452 struct gl_texture
*tex
)
454 context_gl_resource_released(device
, tex
->name
, FALSE
);
455 gl_info
->gl_ops
.gl
.p_glDeleteTextures(1, &tex
->name
);
459 /* Context activation is done by the caller. */
460 /* The caller is responsible for binding the correct texture. */
461 static void wined3d_texture_allocate_gl_mutable_storage(struct wined3d_texture
*texture
,
462 GLenum gl_internal_format
, const struct wined3d_format
*format
,
463 const struct wined3d_gl_info
*gl_info
)
465 unsigned int i
, sub_call_count
;
467 sub_call_count
= texture
->level_count
;
468 if (texture
->target
!= GL_TEXTURE_2D_ARRAY
)
469 sub_call_count
*= texture
->layer_count
;
471 for (i
= 0; i
< sub_call_count
; ++i
)
473 struct wined3d_surface
*surface
= texture
->sub_resources
[i
].u
.surface
;
474 GLsizei width
, height
;
476 width
= wined3d_texture_get_level_pow2_width(texture
, surface
->texture_level
);
477 height
= wined3d_texture_get_level_pow2_height(texture
, surface
->texture_level
);
478 if (texture
->resource
.format_flags
& WINED3DFMT_FLAG_HEIGHT_SCALE
)
480 height
*= format
->height_scale
.numerator
;
481 height
/= format
->height_scale
.denominator
;
484 TRACE("surface %p, target %#x, level %u, width %u, height %u.\n",
485 surface
, surface
->texture_target
, surface
->texture_level
, width
, height
);
487 if (texture
->target
== GL_TEXTURE_2D_ARRAY
)
489 GL_EXTCALL(glTexImage3D(surface
->texture_target
, surface
->texture_level
,
490 gl_internal_format
, width
, height
, texture
->layer_count
, 0,
491 format
->glFormat
, format
->glType
, NULL
));
492 checkGLcall("glTexImage3D");
496 gl_info
->gl_ops
.gl
.p_glTexImage2D(surface
->texture_target
, surface
->texture_level
,
497 gl_internal_format
, width
, height
, 0, format
->glFormat
, format
->glType
, NULL
);
498 checkGLcall("glTexImage2D");
503 /* Context activation is done by the caller. */
504 /* The caller is responsible for binding the correct texture. */
505 static void wined3d_texture_allocate_gl_immutable_storage(struct wined3d_texture
*texture
,
506 GLenum gl_internal_format
, const struct wined3d_gl_info
*gl_info
)
508 GLsizei width
= wined3d_texture_get_level_pow2_width(texture
, 0);
509 GLsizei height
= wined3d_texture_get_level_pow2_height(texture
, 0);
511 if (texture
->target
== GL_TEXTURE_2D_ARRAY
)
513 GL_EXTCALL(glTexStorage3D(texture
->target
, texture
->level_count
, gl_internal_format
,
514 width
, height
, texture
->layer_count
));
515 checkGLcall("glTexStorage3D");
519 GL_EXTCALL(glTexStorage2D(texture
->target
, texture
->level_count
, gl_internal_format
,
521 checkGLcall("glTexStorage2D");
525 static void wined3d_texture_unload_gl_texture(struct wined3d_texture
*texture
)
527 struct wined3d_device
*device
= texture
->resource
.device
;
528 const struct wined3d_gl_info
*gl_info
= NULL
;
529 struct wined3d_context
*context
= NULL
;
531 if (texture
->texture_rgb
.name
|| texture
->texture_srgb
.name
532 || texture
->rb_multisample
|| texture
->rb_resolved
)
534 context
= context_acquire(device
, NULL
, 0);
535 gl_info
= context
->gl_info
;
538 if (texture
->texture_rgb
.name
)
539 gltexture_delete(device
, context
->gl_info
, &texture
->texture_rgb
);
541 if (texture
->texture_srgb
.name
)
542 gltexture_delete(device
, context
->gl_info
, &texture
->texture_srgb
);
544 if (texture
->rb_multisample
)
546 TRACE("Deleting multisample renderbuffer %u.\n", texture
->rb_multisample
);
547 context_gl_resource_released(device
, texture
->rb_multisample
, TRUE
);
548 gl_info
->fbo_ops
.glDeleteRenderbuffers(1, &texture
->rb_multisample
);
549 texture
->rb_multisample
= 0;
552 if (texture
->rb_resolved
)
554 TRACE("Deleting resolved renderbuffer %u.\n", texture
->rb_resolved
);
555 context_gl_resource_released(device
, texture
->rb_resolved
, TRUE
);
556 gl_info
->fbo_ops
.glDeleteRenderbuffers(1, &texture
->rb_resolved
);
557 texture
->rb_resolved
= 0;
560 if (context
) context_release(context
);
562 wined3d_texture_set_dirty(texture
);
564 resource_unload(&texture
->resource
);
567 static void wined3d_texture_sub_resources_destroyed(struct wined3d_texture
*texture
)
569 unsigned int sub_count
= texture
->level_count
* texture
->layer_count
;
570 struct wined3d_texture_sub_resource
*sub_resource
;
573 for (i
= 0; i
< sub_count
; ++i
)
575 sub_resource
= &texture
->sub_resources
[i
];
576 if (sub_resource
->parent
)
578 TRACE("sub-resource %u.\n", i
);
579 sub_resource
->parent_ops
->wined3d_object_destroyed(sub_resource
->parent
);
580 sub_resource
->parent
= NULL
;
585 static void wined3d_texture_cleanup(struct wined3d_texture
*texture
)
587 unsigned int sub_count
= texture
->level_count
* texture
->layer_count
;
588 struct wined3d_device
*device
= texture
->resource
.device
;
589 struct wined3d_context
*context
= NULL
;
590 const struct wined3d_gl_info
*gl_info
;
591 GLuint buffer_object
;
594 TRACE("texture %p.\n", texture
);
596 for (i
= 0; i
< sub_count
; ++i
)
598 if (!(buffer_object
= texture
->sub_resources
[i
].buffer_object
))
601 TRACE("Deleting buffer object %u.\n", buffer_object
);
603 /* We may not be able to get a context in wined3d_texture_cleanup() in
604 * general, but if a buffer object was previously created we can. */
607 context
= context_acquire(device
, NULL
, 0);
608 gl_info
= context
->gl_info
;
611 GL_EXTCALL(glDeleteBuffers(1, &buffer_object
));
614 context_release(context
);
616 texture
->texture_ops
->texture_cleanup_sub_resources(texture
);
617 wined3d_texture_unload_gl_texture(texture
);
620 void wined3d_texture_set_swapchain(struct wined3d_texture
*texture
, struct wined3d_swapchain
*swapchain
)
622 texture
->swapchain
= swapchain
;
623 wined3d_resource_update_draw_binding(&texture
->resource
);
626 /* Context activation is done by the caller. */
627 void wined3d_texture_bind(struct wined3d_texture
*texture
,
628 struct wined3d_context
*context
, BOOL srgb
)
630 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
631 const struct wined3d_format
*format
= texture
->resource
.format
;
632 const struct color_fixup_desc fixup
= format
->color_fixup
;
633 struct gl_texture
*gl_tex
;
636 TRACE("texture %p, context %p, srgb %#x.\n", texture
, context
, srgb
);
638 if (!needs_separate_srgb_gl_texture(context
, texture
))
641 /* sRGB mode cache for preload() calls outside drawprim. */
643 texture
->flags
|= WINED3D_TEXTURE_IS_SRGB
;
645 texture
->flags
&= ~WINED3D_TEXTURE_IS_SRGB
;
647 gl_tex
= wined3d_texture_get_gl_texture(texture
, srgb
);
648 target
= texture
->target
;
652 context_bind_texture(context
, target
, gl_tex
->name
);
656 gl_info
->gl_ops
.gl
.p_glGenTextures(1, &gl_tex
->name
);
657 checkGLcall("glGenTextures");
658 TRACE("Generated texture %d.\n", gl_tex
->name
);
662 ERR("Failed to generate a texture name.\n");
666 /* Initialise the state of the texture object to the OpenGL defaults, not
667 * the wined3d defaults. */
668 gl_tex
->sampler_desc
.address_u
= WINED3D_TADDRESS_WRAP
;
669 gl_tex
->sampler_desc
.address_v
= WINED3D_TADDRESS_WRAP
;
670 gl_tex
->sampler_desc
.address_w
= WINED3D_TADDRESS_WRAP
;
671 memset(gl_tex
->sampler_desc
.border_color
, 0, sizeof(gl_tex
->sampler_desc
.border_color
));
672 gl_tex
->sampler_desc
.mag_filter
= WINED3D_TEXF_LINEAR
;
673 gl_tex
->sampler_desc
.min_filter
= WINED3D_TEXF_POINT
; /* GL_NEAREST_MIPMAP_LINEAR */
674 gl_tex
->sampler_desc
.mip_filter
= WINED3D_TEXF_LINEAR
; /* GL_NEAREST_MIPMAP_LINEAR */
675 gl_tex
->sampler_desc
.lod_bias
= 0.0f
;
676 gl_tex
->sampler_desc
.min_lod
= -1000.0f
;
677 gl_tex
->sampler_desc
.max_lod
= 1000.0f
;
678 gl_tex
->sampler_desc
.max_anisotropy
= 1;
679 gl_tex
->sampler_desc
.compare
= FALSE
;
680 gl_tex
->sampler_desc
.comparison_func
= WINED3D_CMP_LESSEQUAL
;
681 if (context
->gl_info
->supported
[EXT_TEXTURE_SRGB_DECODE
])
682 gl_tex
->sampler_desc
.srgb_decode
= TRUE
;
684 gl_tex
->sampler_desc
.srgb_decode
= srgb
;
685 gl_tex
->base_level
= 0;
686 wined3d_texture_set_dirty(texture
);
688 context_bind_texture(context
, target
, gl_tex
->name
);
690 if (texture
->resource
.usage
& WINED3DUSAGE_AUTOGENMIPMAP
)
692 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_GENERATE_MIPMAP_SGIS
, GL_TRUE
);
693 checkGLcall("glTexParameteri(target, GL_GENERATE_MIPMAP_SGIS, GL_TRUE)");
696 /* For a new texture we have to set the texture levels after binding the
697 * texture. Beware that texture rectangles do not support mipmapping, but
698 * set the maxmiplevel if we're relying on the partial
699 * GL_ARB_texture_non_power_of_two emulation with texture rectangles.
700 * (I.e., do not care about cond_np2 here, just look for
701 * GL_TEXTURE_RECTANGLE_ARB.) */
702 if (target
!= GL_TEXTURE_RECTANGLE_ARB
)
704 TRACE("Setting GL_TEXTURE_MAX_LEVEL to %u.\n", texture
->level_count
- 1);
705 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, texture
->level_count
- 1);
706 checkGLcall("glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, texture->level_count)");
709 if (target
== GL_TEXTURE_CUBE_MAP_ARB
)
711 /* Cubemaps are always set to clamp, regardless of the sampler state. */
712 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
713 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
714 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_R
, GL_CLAMP_TO_EDGE
);
717 if (texture
->flags
& WINED3D_TEXTURE_COND_NP2
)
719 /* Conditinal non power of two textures use a different clamping
720 * default. If we're using the GL_WINE_normalized_texrect partial
721 * driver emulation, we're dealing with a GL_TEXTURE_2D texture which
722 * has the address mode set to repeat - something that prevents us
723 * from hitting the accelerated codepath. Thus manually set the GL
724 * state. The same applies to filtering. Even if the texture has only
725 * one mip level, the default LINEAR_MIPMAP_LINEAR filter causes a SW
726 * fallback on macos. */
727 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
728 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
729 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
730 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
731 checkGLcall("glTexParameteri");
732 gl_tex
->sampler_desc
.address_u
= WINED3D_TADDRESS_CLAMP
;
733 gl_tex
->sampler_desc
.address_v
= WINED3D_TADDRESS_CLAMP
;
734 gl_tex
->sampler_desc
.mag_filter
= WINED3D_TEXF_POINT
;
735 gl_tex
->sampler_desc
.min_filter
= WINED3D_TEXF_POINT
;
736 gl_tex
->sampler_desc
.mip_filter
= WINED3D_TEXF_NONE
;
739 if (gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
] && gl_info
->supported
[ARB_DEPTH_TEXTURE
])
741 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_DEPTH_TEXTURE_MODE_ARB
, GL_INTENSITY
);
742 checkGLcall("glTexParameteri(GL_DEPTH_TEXTURE_MODE_ARB, GL_INTENSITY)");
745 if (!is_identity_fixup(fixup
) && can_use_texture_swizzle(gl_info
, format
))
747 static const GLenum swizzle_source
[] =
749 GL_ZERO
, /* CHANNEL_SOURCE_ZERO */
750 GL_ONE
, /* CHANNEL_SOURCE_ONE */
751 GL_RED
, /* CHANNEL_SOURCE_X */
752 GL_GREEN
, /* CHANNEL_SOURCE_Y */
753 GL_BLUE
, /* CHANNEL_SOURCE_Z */
754 GL_ALPHA
, /* CHANNEL_SOURCE_W */
762 swizzle
.x
= swizzle_source
[fixup
.x_source
];
763 swizzle
.y
= swizzle_source
[fixup
.y_source
];
764 swizzle
.z
= swizzle_source
[fixup
.z_source
];
765 swizzle
.w
= swizzle_source
[fixup
.w_source
];
766 gl_info
->gl_ops
.gl
.p_glTexParameteriv(target
, GL_TEXTURE_SWIZZLE_RGBA
, &swizzle
.x
);
767 checkGLcall("glTexParameteriv(GL_TEXTURE_SWIZZLE_RGBA)");
771 /* Context activation is done by the caller. */
772 void wined3d_texture_bind_and_dirtify(struct wined3d_texture
*texture
,
773 struct wined3d_context
*context
, BOOL srgb
)
775 /* We don't need a specific texture unit, but after binding the texture
776 * the current unit is dirty. Read the unit back instead of switching to
777 * 0, this avoids messing around with the state manager's GL states. The
778 * current texture unit should always be a valid one.
780 * To be more specific, this is tricky because we can implicitly be
781 * called from sampler() in state.c. This means we can't touch anything
782 * other than whatever happens to be the currently active texture, or we
783 * would risk marking already applied sampler states dirty again. */
784 if (context
->active_texture
< ARRAY_SIZE(context
->rev_tex_unit_map
))
786 DWORD active_sampler
= context
->rev_tex_unit_map
[context
->active_texture
];
787 if (active_sampler
!= WINED3D_UNMAPPED_STAGE
)
788 context_invalidate_state(context
, STATE_SAMPLER(active_sampler
));
790 /* FIXME: Ideally we'd only do this when touching a binding that's used by
792 context_invalidate_compute_state(context
, STATE_COMPUTE_SHADER_RESOURCE_BINDING
);
793 context_invalidate_state(context
, STATE_GRAPHICS_SHADER_RESOURCE_BINDING
);
795 wined3d_texture_bind(texture
, context
, srgb
);
798 /* Context activation is done by the caller (state handler). */
799 /* This function relies on the correct texture being bound and loaded. */
800 void wined3d_texture_apply_sampler_desc(struct wined3d_texture
*texture
,
801 const struct wined3d_sampler_desc
*sampler_desc
, const struct wined3d_context
*context
)
803 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
804 GLenum target
= texture
->target
;
805 struct gl_texture
*gl_tex
;
808 TRACE("texture %p, sampler_desc %p, context %p.\n", texture
, sampler_desc
, context
);
810 gl_tex
= wined3d_texture_get_gl_texture(texture
, texture
->flags
& WINED3D_TEXTURE_IS_SRGB
);
812 state
= sampler_desc
->address_u
;
813 if (state
!= gl_tex
->sampler_desc
.address_u
)
815 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_S
,
816 gl_info
->wrap_lookup
[state
- WINED3D_TADDRESS_WRAP
]);
817 gl_tex
->sampler_desc
.address_u
= state
;
820 state
= sampler_desc
->address_v
;
821 if (state
!= gl_tex
->sampler_desc
.address_v
)
823 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_T
,
824 gl_info
->wrap_lookup
[state
- WINED3D_TADDRESS_WRAP
]);
825 gl_tex
->sampler_desc
.address_v
= state
;
828 state
= sampler_desc
->address_w
;
829 if (state
!= gl_tex
->sampler_desc
.address_w
)
831 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_R
,
832 gl_info
->wrap_lookup
[state
- WINED3D_TADDRESS_WRAP
]);
833 gl_tex
->sampler_desc
.address_w
= state
;
836 if (memcmp(gl_tex
->sampler_desc
.border_color
, sampler_desc
->border_color
,
837 sizeof(gl_tex
->sampler_desc
.border_color
)))
839 gl_info
->gl_ops
.gl
.p_glTexParameterfv(target
, GL_TEXTURE_BORDER_COLOR
, &sampler_desc
->border_color
[0]);
840 memcpy(gl_tex
->sampler_desc
.border_color
, sampler_desc
->border_color
,
841 sizeof(gl_tex
->sampler_desc
.border_color
));
844 state
= sampler_desc
->mag_filter
;
845 if (state
!= gl_tex
->sampler_desc
.mag_filter
)
847 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MAG_FILTER
, wined3d_gl_mag_filter(state
));
848 gl_tex
->sampler_desc
.mag_filter
= state
;
851 if (sampler_desc
->min_filter
!= gl_tex
->sampler_desc
.min_filter
852 || sampler_desc
->mip_filter
!= gl_tex
->sampler_desc
.mip_filter
)
854 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MIN_FILTER
,
855 wined3d_gl_min_mip_filter(sampler_desc
->min_filter
, sampler_desc
->mip_filter
));
856 gl_tex
->sampler_desc
.min_filter
= sampler_desc
->min_filter
;
857 gl_tex
->sampler_desc
.mip_filter
= sampler_desc
->mip_filter
;
860 state
= sampler_desc
->max_anisotropy
;
861 if (state
!= gl_tex
->sampler_desc
.max_anisotropy
)
863 if (gl_info
->supported
[ARB_TEXTURE_FILTER_ANISOTROPIC
])
864 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MAX_ANISOTROPY
, state
);
866 WARN("Anisotropic filtering not supported.\n");
867 gl_tex
->sampler_desc
.max_anisotropy
= state
;
870 if (!sampler_desc
->srgb_decode
!= !gl_tex
->sampler_desc
.srgb_decode
871 && (context
->d3d_info
->wined3d_creation_flags
& WINED3D_SRGB_READ_WRITE_CONTROL
)
872 && gl_info
->supported
[EXT_TEXTURE_SRGB_DECODE
])
874 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_SRGB_DECODE_EXT
,
875 sampler_desc
->srgb_decode
? GL_DECODE_EXT
: GL_SKIP_DECODE_EXT
);
876 gl_tex
->sampler_desc
.srgb_decode
= sampler_desc
->srgb_decode
;
879 if (!sampler_desc
->compare
!= !gl_tex
->sampler_desc
.compare
)
881 if (sampler_desc
->compare
)
882 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_COMPARE_MODE_ARB
, GL_COMPARE_R_TO_TEXTURE_ARB
);
884 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_COMPARE_MODE_ARB
, GL_NONE
);
885 gl_tex
->sampler_desc
.compare
= sampler_desc
->compare
;
888 checkGLcall("Texture parameter application");
890 if (gl_info
->supported
[EXT_TEXTURE_LOD_BIAS
])
892 gl_info
->gl_ops
.gl
.p_glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT
,
893 GL_TEXTURE_LOD_BIAS_EXT
, sampler_desc
->lod_bias
);
894 checkGLcall("glTexEnvf(GL_TEXTURE_LOD_BIAS_EXT, ...)");
898 ULONG CDECL
wined3d_texture_incref(struct wined3d_texture
*texture
)
902 TRACE("texture %p, swapchain %p.\n", texture
, texture
->swapchain
);
904 if (texture
->swapchain
)
905 return wined3d_swapchain_incref(texture
->swapchain
);
907 refcount
= InterlockedIncrement(&texture
->resource
.ref
);
908 TRACE("%p increasing refcount to %u.\n", texture
, refcount
);
913 static void wined3d_texture_cleanup_sync(struct wined3d_texture
*texture
)
915 wined3d_texture_sub_resources_destroyed(texture
);
916 resource_cleanup(&texture
->resource
);
917 wined3d_resource_wait_idle(&texture
->resource
);
918 wined3d_texture_cleanup(texture
);
921 static void wined3d_texture_destroy_object(void *object
)
923 wined3d_texture_cleanup(object
);
924 HeapFree(GetProcessHeap(), 0, object
);
927 ULONG CDECL
wined3d_texture_decref(struct wined3d_texture
*texture
)
931 TRACE("texture %p, swapchain %p.\n", texture
, texture
->swapchain
);
933 if (texture
->swapchain
)
934 return wined3d_swapchain_decref(texture
->swapchain
);
936 refcount
= InterlockedDecrement(&texture
->resource
.ref
);
937 TRACE("%p decreasing refcount to %u.\n", texture
, refcount
);
941 /* Wait for the texture to become idle if it's using user memory,
942 * since the application is allowed to free that memory once the
943 * texture is destroyed. Note that this implies that
944 * wined3d_texture_destroy_object() can't access that memory either. */
945 if (texture
->user_memory
)
946 wined3d_resource_wait_idle(&texture
->resource
);
947 wined3d_texture_sub_resources_destroyed(texture
);
948 texture
->resource
.parent_ops
->wined3d_object_destroyed(texture
->resource
.parent
);
949 resource_cleanup(&texture
->resource
);
950 wined3d_cs_destroy_object(texture
->resource
.device
->cs
, wined3d_texture_destroy_object
, texture
);
956 struct wined3d_resource
* CDECL
wined3d_texture_get_resource(struct wined3d_texture
*texture
)
958 TRACE("texture %p.\n", texture
);
960 return &texture
->resource
;
963 static BOOL
color_key_equal(const struct wined3d_color_key
*c1
, struct wined3d_color_key
*c2
)
965 return c1
->color_space_low_value
== c2
->color_space_low_value
966 && c1
->color_space_high_value
== c2
->color_space_high_value
;
969 /* Context activation is done by the caller */
970 void wined3d_texture_load(struct wined3d_texture
*texture
,
971 struct wined3d_context
*context
, BOOL srgb
)
973 UINT sub_count
= texture
->level_count
* texture
->layer_count
;
974 const struct wined3d_d3d_info
*d3d_info
= context
->d3d_info
;
978 TRACE("texture %p, context %p, srgb %#x.\n", texture
, context
, srgb
);
980 if (!needs_separate_srgb_gl_texture(context
, texture
))
984 flag
= WINED3D_TEXTURE_SRGB_VALID
;
986 flag
= WINED3D_TEXTURE_RGB_VALID
;
988 if (!d3d_info
->shader_color_key
989 && (!(texture
->async
.flags
& WINED3D_TEXTURE_ASYNC_COLOR_KEY
)
990 != !(texture
->async
.color_key_flags
& WINED3D_CKEY_SRC_BLT
)
991 || (texture
->async
.flags
& WINED3D_TEXTURE_ASYNC_COLOR_KEY
992 && !color_key_equal(&texture
->async
.gl_color_key
, &texture
->async
.src_blt_color_key
))))
994 unsigned int sub_count
= texture
->level_count
* texture
->layer_count
;
997 TRACE("Reloading because of color key value change.\n");
998 for (i
= 0; i
< sub_count
; i
++)
1000 if (!wined3d_texture_load_location(texture
, i
, context
, texture
->resource
.map_binding
))
1001 ERR("Failed to load location %s.\n", wined3d_debug_location(texture
->resource
.map_binding
));
1003 wined3d_texture_invalidate_location(texture
, i
, ~texture
->resource
.map_binding
);
1006 texture
->async
.gl_color_key
= texture
->async
.src_blt_color_key
;
1009 if (texture
->flags
& flag
)
1011 TRACE("Texture %p not dirty, nothing to do.\n", texture
);
1015 /* Reload the surfaces if the texture is marked dirty. */
1016 for (i
= 0; i
< sub_count
; ++i
)
1018 if (!wined3d_texture_load_location(texture
, i
, context
,
1019 srgb
? WINED3D_LOCATION_TEXTURE_SRGB
: WINED3D_LOCATION_TEXTURE_RGB
))
1020 ERR("Failed to load location (srgb %#x).\n", srgb
);
1022 texture
->flags
|= flag
;
1025 void * CDECL
wined3d_texture_get_parent(const struct wined3d_texture
*texture
)
1027 TRACE("texture %p.\n", texture
);
1029 return texture
->resource
.parent
;
1032 HRESULT
wined3d_texture_check_box_dimensions(const struct wined3d_texture
*texture
,
1033 unsigned int level
, const struct wined3d_box
*box
)
1035 const struct wined3d_format
*format
= texture
->resource
.format
;
1036 unsigned int width_mask
, height_mask
, width
, height
, depth
;
1038 width
= wined3d_texture_get_level_width(texture
, level
);
1039 height
= wined3d_texture_get_level_height(texture
, level
);
1040 depth
= wined3d_texture_get_level_depth(texture
, level
);
1042 if (box
->left
>= box
->right
|| box
->right
> width
1043 || box
->top
>= box
->bottom
|| box
->bottom
> height
1044 || box
->front
>= box
->back
|| box
->back
> depth
)
1046 WARN("Box %s is invalid.\n", debug_box(box
));
1047 return WINEDDERR_INVALIDRECT
;
1050 if (texture
->resource
.format_flags
& WINED3DFMT_FLAG_BLOCKS
)
1052 /* This assumes power of two block sizes, but NPOT block sizes would
1055 * This also assumes that the format's block depth is 1. */
1056 width_mask
= format
->block_width
- 1;
1057 height_mask
= format
->block_height
- 1;
1059 if ((box
->left
& width_mask
) || (box
->top
& height_mask
)
1060 || (box
->right
& width_mask
&& box
->right
!= width
)
1061 || (box
->bottom
& height_mask
&& box
->bottom
!= height
))
1063 WARN("Box %s is misaligned for %ux%u blocks.\n",
1064 debug_box(box
), format
->block_width
, format
->block_height
);
1065 return WINED3DERR_INVALIDCALL
;
1072 void CDECL
wined3d_texture_get_pitch(const struct wined3d_texture
*texture
,
1073 unsigned int level
, unsigned int *row_pitch
, unsigned int *slice_pitch
)
1075 const struct wined3d_resource
*resource
= &texture
->resource
;
1076 unsigned int width
= wined3d_texture_get_level_width(texture
, level
);
1077 unsigned int height
= wined3d_texture_get_level_height(texture
, level
);
1079 if (texture
->row_pitch
)
1081 *row_pitch
= texture
->row_pitch
;
1082 *slice_pitch
= texture
->slice_pitch
;
1086 wined3d_format_calculate_pitch(resource
->format
, resource
->device
->surface_alignment
,
1087 width
, height
, row_pitch
, slice_pitch
);
1090 DWORD CDECL
wined3d_texture_set_lod(struct wined3d_texture
*texture
, DWORD lod
)
1092 DWORD old
= texture
->lod
;
1094 TRACE("texture %p, lod %u.\n", texture
, lod
);
1096 /* The d3d9:texture test shows that SetLOD is ignored on non-managed
1097 * textures. The call always returns 0, and GetLOD always returns 0. */
1098 if (texture
->resource
.pool
!= WINED3D_POOL_MANAGED
)
1100 TRACE("Ignoring SetLOD on %s texture, returning 0.\n", debug_d3dpool(texture
->resource
.pool
));
1104 if (lod
>= texture
->level_count
)
1105 lod
= texture
->level_count
- 1;
1107 if (texture
->lod
!= lod
)
1109 struct wined3d_device
*device
= texture
->resource
.device
;
1111 wined3d_resource_wait_idle(&texture
->resource
);
1114 texture
->texture_rgb
.base_level
= ~0u;
1115 texture
->texture_srgb
.base_level
= ~0u;
1116 if (texture
->resource
.bind_count
)
1117 wined3d_cs_emit_set_sampler_state(device
->cs
, texture
->sampler
, WINED3D_SAMP_MAX_MIP_LEVEL
,
1118 device
->state
.sampler_states
[texture
->sampler
][WINED3D_SAMP_MAX_MIP_LEVEL
]);
1124 DWORD CDECL
wined3d_texture_get_lod(const struct wined3d_texture
*texture
)
1126 TRACE("texture %p, returning %u.\n", texture
, texture
->lod
);
1128 return texture
->lod
;
1131 DWORD CDECL
wined3d_texture_get_level_count(const struct wined3d_texture
*texture
)
1133 TRACE("texture %p, returning %u.\n", texture
, texture
->level_count
);
1135 return texture
->level_count
;
1138 HRESULT CDECL
wined3d_texture_set_autogen_filter_type(struct wined3d_texture
*texture
,
1139 enum wined3d_texture_filter_type filter_type
)
1141 FIXME("texture %p, filter_type %s stub!\n", texture
, debug_d3dtexturefiltertype(filter_type
));
1143 if (!(texture
->resource
.usage
& WINED3DUSAGE_AUTOGENMIPMAP
))
1145 WARN("Texture doesn't have AUTOGENMIPMAP usage.\n");
1146 return WINED3DERR_INVALIDCALL
;
1149 texture
->filter_type
= filter_type
;
1154 enum wined3d_texture_filter_type CDECL
wined3d_texture_get_autogen_filter_type(const struct wined3d_texture
*texture
)
1156 TRACE("texture %p.\n", texture
);
1158 return texture
->filter_type
;
1161 HRESULT CDECL
wined3d_texture_set_color_key(struct wined3d_texture
*texture
,
1162 DWORD flags
, const struct wined3d_color_key
*color_key
)
1164 struct wined3d_device
*device
= texture
->resource
.device
;
1165 static const DWORD all_flags
= WINED3D_CKEY_DST_BLT
| WINED3D_CKEY_DST_OVERLAY
1166 | WINED3D_CKEY_SRC_BLT
| WINED3D_CKEY_SRC_OVERLAY
;
1168 TRACE("texture %p, flags %#x, color_key %p.\n", texture
, flags
, color_key
);
1170 if (flags
& ~all_flags
)
1172 WARN("Invalid flags passed, returning WINED3DERR_INVALIDCALL.\n");
1173 return WINED3DERR_INVALIDCALL
;
1176 wined3d_cs_emit_set_color_key(device
->cs
, texture
, flags
, color_key
);
1181 static void texture2d_create_dc(void *object
)
1183 struct wined3d_surface
*surface
= object
;
1184 struct wined3d_context
*context
= NULL
;
1185 const struct wined3d_format
*format
;
1186 unsigned int row_pitch
, slice_pitch
;
1187 struct wined3d_texture
*texture
;
1188 struct wined3d_bo_address data
;
1189 D3DKMT_CREATEDCFROMMEMORY desc
;
1190 unsigned int sub_resource_idx
;
1191 struct wined3d_device
*device
;
1194 TRACE("surface %p.\n", surface
);
1196 texture
= surface
->container
;
1197 sub_resource_idx
= surface_get_sub_resource_idx(surface
);
1198 device
= texture
->resource
.device
;
1200 format
= texture
->resource
.format
;
1201 if (!format
->ddi_format
)
1203 WARN("Cannot create a DC for format %s.\n", debug_d3dformat(format
->id
));
1207 if (device
->d3d_initialized
)
1208 context
= context_acquire(device
, NULL
, 0);
1210 wined3d_texture_load_location(texture
, sub_resource_idx
, context
, texture
->resource
.map_binding
);
1211 wined3d_texture_invalidate_location(texture
, sub_resource_idx
, ~texture
->resource
.map_binding
);
1212 wined3d_texture_get_pitch(texture
, surface
->texture_level
, &row_pitch
, &slice_pitch
);
1213 wined3d_texture_get_memory(texture
, sub_resource_idx
, &data
, texture
->resource
.map_binding
);
1214 desc
.pMemory
= context_map_bo_address(context
, &data
,
1215 texture
->sub_resources
[sub_resource_idx
].size
, GL_PIXEL_UNPACK_BUFFER
, 0);
1218 context_release(context
);
1220 desc
.Format
= format
->ddi_format
;
1221 desc
.Width
= wined3d_texture_get_level_width(texture
, surface
->texture_level
);
1222 desc
.Height
= wined3d_texture_get_level_height(texture
, surface
->texture_level
);
1223 desc
.Pitch
= row_pitch
;
1224 desc
.hDeviceDc
= CreateCompatibleDC(NULL
);
1225 desc
.pColorTable
= NULL
;
1227 status
= D3DKMTCreateDCFromMemory(&desc
);
1228 DeleteDC(desc
.hDeviceDc
);
1231 WARN("Failed to create DC, status %#x.\n", status
);
1235 surface
->dc
= desc
.hDc
;
1236 surface
->bitmap
= desc
.hBitmap
;
1238 TRACE("Created DC %p, bitmap %p for surface %p.\n", surface
->dc
, surface
->bitmap
, surface
);
1241 static void texture2d_destroy_dc(void *object
)
1243 struct wined3d_surface
*surface
= object
;
1244 D3DKMT_DESTROYDCFROMMEMORY destroy_desc
;
1245 struct wined3d_context
*context
= NULL
;
1246 struct wined3d_texture
*texture
;
1247 struct wined3d_bo_address data
;
1248 unsigned int sub_resource_idx
;
1249 struct wined3d_device
*device
;
1252 texture
= surface
->container
;
1253 sub_resource_idx
= surface_get_sub_resource_idx(surface
);
1254 device
= texture
->resource
.device
;
1258 ERR("Surface %p has no DC.\n", surface
);
1262 TRACE("dc %p, bitmap %p.\n", surface
->dc
, surface
->bitmap
);
1264 destroy_desc
.hDc
= surface
->dc
;
1265 destroy_desc
.hBitmap
= surface
->bitmap
;
1266 if ((status
= D3DKMTDestroyDCFromMemory(&destroy_desc
)))
1267 ERR("Failed to destroy dc, status %#x.\n", status
);
1269 surface
->bitmap
= NULL
;
1271 if (device
->d3d_initialized
)
1272 context
= context_acquire(device
, NULL
, 0);
1274 wined3d_texture_get_memory(texture
, sub_resource_idx
, &data
, texture
->resource
.map_binding
);
1275 context_unmap_bo_address(context
, &data
, GL_PIXEL_UNPACK_BUFFER
);
1278 context_release(context
);
1281 HRESULT CDECL
wined3d_texture_update_desc(struct wined3d_texture
*texture
, UINT width
, UINT height
,
1282 enum wined3d_format_id format_id
, enum wined3d_multisample_type multisample_type
,
1283 UINT multisample_quality
, void *mem
, UINT pitch
)
1285 struct wined3d_device
*device
= texture
->resource
.device
;
1286 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
1287 const struct wined3d_format
*format
= wined3d_get_format(gl_info
, format_id
, texture
->resource
.usage
);
1288 UINT resource_size
= wined3d_format_calculate_size(format
, device
->surface_alignment
, width
, height
, 1);
1289 struct wined3d_texture_sub_resource
*sub_resource
;
1290 struct wined3d_surface
*surface
;
1291 DWORD valid_location
= 0;
1292 BOOL create_dib
= FALSE
;
1294 TRACE("texture %p, width %u, height %u, format %s, multisample_type %#x, multisample_quality %u, "
1295 "mem %p, pitch %u.\n",
1296 texture
, width
, height
, debug_d3dformat(format_id
), multisample_type
, multisample_quality
, mem
, pitch
);
1299 return WINED3DERR_INVALIDCALL
;
1301 if (texture
->level_count
* texture
->layer_count
> 1)
1303 WARN("Texture has multiple sub-resources, not supported.\n");
1304 return WINED3DERR_INVALIDCALL
;
1307 if (texture
->resource
.type
== WINED3D_RTYPE_TEXTURE_3D
)
1309 WARN("Not supported on 3D textures.\n");
1310 return WINED3DERR_INVALIDCALL
;
1313 if (texture
->resource
.map_count
)
1315 WARN("Texture is mapped.\n");
1316 return WINED3DERR_INVALIDCALL
;
1319 /* We have no way of supporting a pitch that is not a multiple of the pixel
1320 * byte width short of uploading the texture row-by-row.
1321 * Fortunately that's not an issue since D3D9Ex doesn't allow a custom pitch
1322 * for user-memory textures (it always expects packed data) while DirectDraw
1323 * requires a 4-byte aligned pitch and doesn't support texture formats
1324 * larger than 4 bytes per pixel nor any format using 3 bytes per pixel.
1325 * This check is here to verify that the assumption holds. */
1326 if (pitch
% texture
->resource
.format
->byte_count
)
1328 WARN("Pitch unsupported, not a multiple of the texture format byte width.\n");
1329 return WINED3DERR_INVALIDCALL
;
1332 if (device
->d3d_initialized
)
1333 wined3d_cs_emit_unload_resource(device
->cs
, &texture
->resource
);
1334 wined3d_resource_wait_idle(&texture
->resource
);
1336 sub_resource
= &texture
->sub_resources
[0];
1337 surface
= sub_resource
->u
.surface
;
1340 wined3d_cs_destroy_object(device
->cs
, texture2d_destroy_dc
, surface
);
1341 device
->cs
->ops
->finish(device
->cs
, WINED3D_CS_QUEUE_DEFAULT
);
1345 wined3d_resource_free_sysmem(&texture
->resource
);
1347 if ((texture
->row_pitch
= pitch
))
1348 texture
->slice_pitch
= height
* pitch
;
1350 /* User memory surfaces don't have the regular surface alignment. */
1351 wined3d_format_calculate_pitch(format
, 1, width
, height
,
1352 &texture
->row_pitch
, &texture
->slice_pitch
);
1354 texture
->resource
.format
= format
;
1355 texture
->resource
.multisample_type
= multisample_type
;
1356 texture
->resource
.multisample_quality
= multisample_quality
;
1357 texture
->resource
.width
= width
;
1358 texture
->resource
.height
= height
;
1359 texture
->resource
.size
= texture
->slice_pitch
;
1360 sub_resource
->size
= texture
->slice_pitch
;
1361 sub_resource
->locations
= WINED3D_LOCATION_DISCARDED
;
1363 if (((width
& (width
- 1)) || (height
& (height
- 1))) && !gl_info
->supported
[ARB_TEXTURE_NON_POWER_OF_TWO
]
1364 && !gl_info
->supported
[ARB_TEXTURE_RECTANGLE
] && !gl_info
->supported
[WINED3D_GL_NORMALIZED_TEXRECT
])
1366 texture
->flags
|= WINED3D_TEXTURE_COND_NP2_EMULATED
;
1367 texture
->pow2_width
= texture
->pow2_height
= 1;
1368 while (texture
->pow2_width
< width
)
1369 texture
->pow2_width
<<= 1;
1370 while (texture
->pow2_height
< height
)
1371 texture
->pow2_height
<<= 1;
1375 texture
->flags
&= ~WINED3D_TEXTURE_COND_NP2_EMULATED
;
1376 texture
->pow2_width
= width
;
1377 texture
->pow2_height
= height
;
1380 if ((texture
->user_memory
= mem
))
1382 texture
->resource
.map_binding
= WINED3D_LOCATION_USER_MEMORY
;
1383 valid_location
= WINED3D_LOCATION_USER_MEMORY
;
1387 wined3d_texture_prepare_location(texture
, 0, NULL
, WINED3D_LOCATION_SYSMEM
);
1388 valid_location
= WINED3D_LOCATION_SYSMEM
;
1391 /* The format might be changed to a format that needs conversion.
1392 * If the surface didn't use PBOs previously but could now, don't
1393 * change it - whatever made us not use PBOs might come back, e.g.
1395 if (texture
->resource
.map_binding
== WINED3D_LOCATION_BUFFER
&& !wined3d_texture_use_pbo(texture
, gl_info
))
1396 texture
->resource
.map_binding
= WINED3D_LOCATION_SYSMEM
;
1398 wined3d_texture_validate_location(texture
, 0, valid_location
);
1399 wined3d_texture_invalidate_location(texture
, 0, ~valid_location
);
1403 wined3d_cs_init_object(device
->cs
, texture2d_create_dc
, surface
);
1404 device
->cs
->ops
->finish(device
->cs
, WINED3D_CS_QUEUE_DEFAULT
);
1410 /* Context activation is done by the caller. */
1411 static void wined3d_texture_prepare_buffer_object(struct wined3d_texture
*texture
,
1412 unsigned int sub_resource_idx
, const struct wined3d_gl_info
*gl_info
)
1414 struct wined3d_texture_sub_resource
*sub_resource
;
1416 sub_resource
= &texture
->sub_resources
[sub_resource_idx
];
1417 if (sub_resource
->buffer_object
)
1420 GL_EXTCALL(glGenBuffers(1, &sub_resource
->buffer_object
));
1421 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER
, sub_resource
->buffer_object
));
1422 GL_EXTCALL(glBufferData(GL_PIXEL_UNPACK_BUFFER
, sub_resource
->size
, NULL
, GL_STREAM_DRAW
));
1423 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER
, 0));
1424 checkGLcall("Create buffer object");
1426 TRACE("Created buffer object %u for texture %p, sub-resource %u.\n",
1427 sub_resource
->buffer_object
, texture
, sub_resource_idx
);
1430 static void wined3d_texture_force_reload(struct wined3d_texture
*texture
)
1432 unsigned int sub_count
= texture
->level_count
* texture
->layer_count
;
1435 texture
->flags
&= ~(WINED3D_TEXTURE_RGB_ALLOCATED
| WINED3D_TEXTURE_SRGB_ALLOCATED
1436 | WINED3D_TEXTURE_CONVERTED
);
1437 texture
->async
.flags
&= ~WINED3D_TEXTURE_ASYNC_COLOR_KEY
;
1438 for (i
= 0; i
< sub_count
; ++i
)
1440 wined3d_texture_invalidate_location(texture
, i
,
1441 WINED3D_LOCATION_TEXTURE_RGB
| WINED3D_LOCATION_TEXTURE_SRGB
);
1445 void wined3d_texture_prepare_texture(struct wined3d_texture
*texture
, struct wined3d_context
*context
, BOOL srgb
)
1447 DWORD alloc_flag
= srgb
? WINED3D_TEXTURE_SRGB_ALLOCATED
: WINED3D_TEXTURE_RGB_ALLOCATED
;
1448 const struct wined3d_d3d_info
*d3d_info
= context
->d3d_info
;
1450 if (!d3d_info
->shader_color_key
1451 && !(texture
->async
.flags
& WINED3D_TEXTURE_ASYNC_COLOR_KEY
)
1452 != !(texture
->async
.color_key_flags
& WINED3D_CKEY_SRC_BLT
))
1454 wined3d_texture_force_reload(texture
);
1456 if (texture
->async
.color_key_flags
& WINED3D_CKEY_SRC_BLT
)
1457 texture
->async
.flags
|= WINED3D_TEXTURE_ASYNC_COLOR_KEY
;
1460 if (texture
->flags
& alloc_flag
)
1463 texture
->texture_ops
->texture_prepare_texture(texture
, context
, srgb
);
1464 texture
->flags
|= alloc_flag
;
1467 static void wined3d_texture_prepare_rb(struct wined3d_texture
*texture
,
1468 const struct wined3d_gl_info
*gl_info
, BOOL multisample
)
1470 const struct wined3d_format
*format
= texture
->resource
.format
;
1476 if (texture
->rb_multisample
)
1479 /* TODO: NVIDIA expose their Coverage Sample Anti-Aliasing (CSAA)
1480 * feature through type == MULTISAMPLE_XX and quality != 0. This could
1481 * be mapped to GL_NV_framebuffer_multisample_coverage.
1483 * AMD have a similar feature called Enhanced Quality Anti-Aliasing
1484 * (EQAA), but it does not have an equivalent OpenGL extension. */
1486 /* We advertise as many WINED3D_MULTISAMPLE_NON_MASKABLE quality
1487 * levels as the count of advertised multisample types for the texture
1489 if (texture
->resource
.multisample_type
== WINED3D_MULTISAMPLE_NON_MASKABLE
)
1491 unsigned int i
, count
= 0;
1493 for (i
= 0; i
< sizeof(format
->multisample_types
) * 8; ++i
)
1495 if (format
->multisample_types
& 1u << i
)
1497 if (texture
->resource
.multisample_quality
== count
++)
1505 samples
= texture
->resource
.multisample_type
;
1508 gl_info
->fbo_ops
.glGenRenderbuffers(1, &texture
->rb_multisample
);
1509 gl_info
->fbo_ops
.glBindRenderbuffer(GL_RENDERBUFFER
, texture
->rb_multisample
);
1510 gl_info
->fbo_ops
.glRenderbufferStorageMultisample(GL_RENDERBUFFER
, samples
,
1511 format
->glInternal
, texture
->resource
.width
, texture
->resource
.height
);
1512 checkGLcall("glRenderbufferStorageMultisample()");
1513 TRACE("Created multisample rb %u.\n", texture
->rb_multisample
);
1517 if (texture
->rb_resolved
)
1520 gl_info
->fbo_ops
.glGenRenderbuffers(1, &texture
->rb_resolved
);
1521 gl_info
->fbo_ops
.glBindRenderbuffer(GL_RENDERBUFFER
, texture
->rb_resolved
);
1522 gl_info
->fbo_ops
.glRenderbufferStorage(GL_RENDERBUFFER
, format
->glInternal
,
1523 texture
->resource
.width
, texture
->resource
.height
);
1524 checkGLcall("glRenderbufferStorage()");
1525 TRACE("Created resolved rb %u.\n", texture
->rb_resolved
);
1529 /* Context activation is done by the caller. Context may be NULL in
1530 * WINED3D_NO3D mode. */
1531 BOOL
wined3d_texture_prepare_location(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
,
1532 struct wined3d_context
*context
, DWORD location
)
1536 case WINED3D_LOCATION_SYSMEM
:
1537 if (texture
->resource
.heap_memory
)
1540 if (!wined3d_resource_allocate_sysmem(&texture
->resource
))
1542 ERR("Failed to allocate system memory.\n");
1547 case WINED3D_LOCATION_USER_MEMORY
:
1548 if (!texture
->user_memory
)
1549 ERR("Map binding is set to WINED3D_LOCATION_USER_MEMORY but surface->user_memory is NULL.\n");
1552 case WINED3D_LOCATION_BUFFER
:
1553 wined3d_texture_prepare_buffer_object(texture
, sub_resource_idx
, context
->gl_info
);
1556 case WINED3D_LOCATION_TEXTURE_RGB
:
1557 wined3d_texture_prepare_texture(texture
, context
, FALSE
);
1560 case WINED3D_LOCATION_TEXTURE_SRGB
:
1561 wined3d_texture_prepare_texture(texture
, context
, TRUE
);
1564 case WINED3D_LOCATION_DRAWABLE
:
1565 if (!texture
->swapchain
&& wined3d_settings
.offscreen_rendering_mode
!= ORM_BACKBUFFER
)
1566 ERR("Texture %p does not have a drawable.\n", texture
);
1569 case WINED3D_LOCATION_RB_MULTISAMPLE
:
1570 wined3d_texture_prepare_rb(texture
, context
->gl_info
, TRUE
);
1573 case WINED3D_LOCATION_RB_RESOLVED
:
1574 wined3d_texture_prepare_rb(texture
, context
->gl_info
, FALSE
);
1578 ERR("Invalid location %s.\n", wined3d_debug_location(location
));
1583 static struct wined3d_texture_sub_resource
*wined3d_texture_get_sub_resource(struct wined3d_texture
*texture
,
1584 unsigned int sub_resource_idx
)
1586 UINT sub_count
= texture
->level_count
* texture
->layer_count
;
1588 TRACE("texture %p, sub_resource_idx %u.\n", texture
, sub_resource_idx
);
1590 if (sub_resource_idx
>= sub_count
)
1592 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx
, sub_count
);
1596 return &texture
->sub_resources
[sub_resource_idx
];
1599 HRESULT CDECL
wined3d_texture_add_dirty_region(struct wined3d_texture
*texture
,
1600 UINT layer
, const struct wined3d_box
*dirty_region
)
1602 TRACE("texture %p, layer %u, dirty_region %s.\n", texture
, layer
, debug_box(dirty_region
));
1604 if (layer
>= texture
->layer_count
)
1606 WARN("Invalid layer %u specified.\n", layer
);
1607 return WINED3DERR_INVALIDCALL
;
1611 FIXME("Ignoring dirty_region %s.\n", debug_box(dirty_region
));
1613 wined3d_cs_emit_add_dirty_texture_region(texture
->resource
.device
->cs
, texture
, layer
);
1618 void wined3d_texture_upload_data(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
,
1619 const struct wined3d_context
*context
, const struct wined3d_box
*box
,
1620 const struct wined3d_const_bo_address
*data
, unsigned int row_pitch
, unsigned int slice_pitch
)
1622 texture
->texture_ops
->texture_upload_data(texture
, sub_resource_idx
,
1623 context
, box
, data
, row_pitch
, slice_pitch
);
1626 static void texture2d_upload_data(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
,
1627 const struct wined3d_context
*context
, const struct wined3d_box
*box
,
1628 const struct wined3d_const_bo_address
*data
, unsigned int row_pitch
, unsigned int slice_pitch
)
1630 unsigned int texture_level
;
1638 dst_point
.x
= box
->left
;
1639 dst_point
.y
= box
->top
;
1640 src_rect
.right
= box
->right
- box
->left
;
1641 src_rect
.bottom
= box
->bottom
- box
->top
;
1645 dst_point
.x
= dst_point
.y
= 0;
1646 texture_level
= sub_resource_idx
% texture
->level_count
;
1647 src_rect
.right
= wined3d_texture_get_level_width(texture
, texture_level
);
1648 src_rect
.bottom
= wined3d_texture_get_level_height(texture
, texture_level
);
1651 wined3d_surface_upload_data(texture
->sub_resources
[sub_resource_idx
].u
.surface
, context
->gl_info
,
1652 texture
->resource
.format
, &src_rect
, row_pitch
, &dst_point
, FALSE
, data
);
1655 static BOOL
texture2d_load_location(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
,
1656 struct wined3d_context
*context
, DWORD location
)
1658 return surface_load_location(texture
->sub_resources
[sub_resource_idx
].u
.surface
, context
, location
);
1661 /* Context activation is done by the caller. */
1662 static void texture2d_prepare_texture(struct wined3d_texture
*texture
, struct wined3d_context
*context
, BOOL srgb
)
1664 const struct wined3d_format
*format
= texture
->resource
.format
;
1665 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1666 const struct wined3d_color_key_conversion
*conversion
;
1669 TRACE("texture %p, context %p, format %s.\n", texture
, context
, debug_d3dformat(format
->id
));
1671 if (format
->convert
)
1673 texture
->flags
|= WINED3D_TEXTURE_CONVERTED
;
1675 else if ((conversion
= wined3d_format_get_color_key_conversion(texture
, TRUE
)))
1677 texture
->flags
|= WINED3D_TEXTURE_CONVERTED
;
1678 format
= wined3d_get_format(gl_info
, conversion
->dst_format
, texture
->resource
.usage
);
1679 TRACE("Using format %s for color key conversion.\n", debug_d3dformat(format
->id
));
1682 wined3d_texture_bind_and_dirtify(texture
, context
, srgb
);
1685 internal
= format
->glGammaInternal
;
1686 else if (texture
->resource
.usage
& WINED3DUSAGE_RENDERTARGET
1687 && wined3d_resource_is_offscreen(&texture
->resource
))
1688 internal
= format
->rtInternal
;
1690 internal
= format
->glInternal
;
1693 FIXME("No GL internal format for format %s.\n", debug_d3dformat(format
->id
));
1695 TRACE("internal %#x, format %#x, type %#x.\n", internal
, format
->glFormat
, format
->glType
);
1697 if (wined3d_texture_use_immutable_storage(texture
, gl_info
))
1698 wined3d_texture_allocate_gl_immutable_storage(texture
, internal
, gl_info
);
1700 wined3d_texture_allocate_gl_mutable_storage(texture
, internal
, format
, gl_info
);
1703 static void texture2d_cleanup_sub_resources(struct wined3d_texture
*texture
)
1705 unsigned int sub_count
= texture
->level_count
* texture
->layer_count
;
1706 struct wined3d_device
*device
= texture
->resource
.device
;
1707 struct wined3d_texture_sub_resource
*sub_resource
;
1708 struct wined3d_renderbuffer_entry
*entry
, *entry2
;
1709 const struct wined3d_gl_info
*gl_info
= NULL
;
1710 struct wined3d_context
*context
= NULL
;
1711 struct wined3d_surface
*overlay
, *cur
;
1712 struct wined3d_surface
*surface
;
1715 for (i
= 0; i
< sub_count
; ++i
)
1717 sub_resource
= &texture
->sub_resources
[i
];
1718 if (!(surface
= sub_resource
->u
.surface
))
1721 TRACE("surface %p.\n", surface
);
1723 if (!context
&& !list_empty(&surface
->renderbuffers
))
1725 context
= context_acquire(device
, NULL
, 0);
1726 gl_info
= context
->gl_info
;
1729 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, &surface
->renderbuffers
, struct wined3d_renderbuffer_entry
, entry
)
1731 TRACE("Deleting renderbuffer %u.\n", entry
->id
);
1732 context_gl_resource_released(device
, entry
->id
, TRUE
);
1733 gl_info
->fbo_ops
.glDeleteRenderbuffers(1, &entry
->id
);
1734 HeapFree(GetProcessHeap(), 0, entry
);
1738 texture2d_destroy_dc(surface
);
1740 if (surface
->overlay_dest
)
1741 list_remove(&surface
->overlay_entry
);
1743 LIST_FOR_EACH_ENTRY_SAFE(overlay
, cur
, &surface
->overlays
, struct wined3d_surface
, overlay_entry
)
1745 list_remove(&overlay
->overlay_entry
);
1746 overlay
->overlay_dest
= NULL
;
1750 context_release(context
);
1751 HeapFree(GetProcessHeap(), 0, texture
->sub_resources
[0].u
.surface
);
1754 static const struct wined3d_texture_ops texture2d_ops
=
1756 texture2d_upload_data
,
1757 texture2d_load_location
,
1758 texture2d_prepare_texture
,
1759 texture2d_cleanup_sub_resources
,
1762 struct wined3d_texture
* __cdecl
wined3d_texture_from_resource(struct wined3d_resource
*resource
)
1764 return texture_from_resource(resource
);
1767 static ULONG
texture_resource_incref(struct wined3d_resource
*resource
)
1769 return wined3d_texture_incref(texture_from_resource(resource
));
1772 static ULONG
texture_resource_decref(struct wined3d_resource
*resource
)
1774 return wined3d_texture_decref(texture_from_resource(resource
));
1777 static void texture_resource_preload(struct wined3d_resource
*resource
)
1779 struct wined3d_texture
*texture
= texture_from_resource(resource
);
1780 struct wined3d_context
*context
;
1782 context
= context_acquire(resource
->device
, NULL
, 0);
1783 wined3d_texture_load(texture
, context
, texture
->flags
& WINED3D_TEXTURE_IS_SRGB
);
1784 context_release(context
);
1787 static void wined3d_texture_unload(struct wined3d_resource
*resource
)
1789 struct wined3d_texture
*texture
= texture_from_resource(resource
);
1790 UINT sub_count
= texture
->level_count
* texture
->layer_count
;
1791 struct wined3d_device
*device
= resource
->device
;
1792 const struct wined3d_gl_info
*gl_info
;
1793 struct wined3d_context
*context
;
1796 TRACE("texture %p.\n", texture
);
1798 context
= context_acquire(device
, NULL
, 0);
1799 gl_info
= context
->gl_info
;
1801 for (i
= 0; i
< sub_count
; ++i
)
1803 struct wined3d_texture_sub_resource
*sub_resource
= &texture
->sub_resources
[i
];
1805 if (resource
->pool
!= WINED3D_POOL_DEFAULT
1806 && wined3d_texture_load_location(texture
, i
, context
, resource
->map_binding
))
1808 wined3d_texture_invalidate_location(texture
, i
, ~resource
->map_binding
);
1812 /* We should only get here on device reset/teardown for implicit
1814 if (resource
->pool
!= WINED3D_POOL_DEFAULT
|| resource
->type
!= WINED3D_RTYPE_TEXTURE_2D
)
1815 ERR("Discarding %s %p sub-resource %u in the %s pool.\n", debug_d3dresourcetype(resource
->type
),
1816 resource
, i
, debug_d3dpool(resource
->pool
));
1817 wined3d_texture_validate_location(texture
, i
, WINED3D_LOCATION_DISCARDED
);
1818 wined3d_texture_invalidate_location(texture
, i
, ~WINED3D_LOCATION_DISCARDED
);
1821 if (sub_resource
->buffer_object
)
1822 wined3d_texture_remove_buffer_object(texture
, i
, context
->gl_info
);
1824 if (resource
->type
== WINED3D_RTYPE_TEXTURE_2D
)
1826 struct wined3d_surface
*surface
= sub_resource
->u
.surface
;
1827 struct wined3d_renderbuffer_entry
*entry
, *entry2
;
1829 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, &surface
->renderbuffers
, struct wined3d_renderbuffer_entry
, entry
)
1831 context_gl_resource_released(device
, entry
->id
, TRUE
);
1832 gl_info
->fbo_ops
.glDeleteRenderbuffers(1, &entry
->id
);
1833 list_remove(&entry
->entry
);
1834 HeapFree(GetProcessHeap(), 0, entry
);
1836 list_init(&surface
->renderbuffers
);
1837 surface
->current_renderbuffer
= NULL
;
1841 context_release(context
);
1843 wined3d_texture_force_reload(texture
);
1844 wined3d_texture_unload_gl_texture(texture
);
1847 static HRESULT
texture_resource_sub_resource_map(struct wined3d_resource
*resource
, unsigned int sub_resource_idx
,
1848 struct wined3d_map_desc
*map_desc
, const struct wined3d_box
*box
, DWORD flags
)
1850 const struct wined3d_format
*format
= resource
->format
;
1851 struct wined3d_texture_sub_resource
*sub_resource
;
1852 struct wined3d_device
*device
= resource
->device
;
1853 unsigned int fmt_flags
= resource
->format_flags
;
1854 struct wined3d_context
*context
= NULL
;
1855 struct wined3d_texture
*texture
;
1856 struct wined3d_bo_address data
;
1857 unsigned int texture_level
;
1861 TRACE("resource %p, sub_resource_idx %u, map_desc %p, box %s, flags %#x.\n",
1862 resource
, sub_resource_idx
, map_desc
, debug_box(box
), flags
);
1864 texture
= texture_from_resource(resource
);
1865 if (!(sub_resource
= wined3d_texture_get_sub_resource(texture
, sub_resource_idx
)))
1866 return E_INVALIDARG
;
1868 texture_level
= sub_resource_idx
% texture
->level_count
;
1869 if (box
&& FAILED(wined3d_texture_check_box_dimensions(texture
, texture_level
, box
)))
1871 WARN("Map box is invalid.\n");
1872 if (((fmt_flags
& WINED3DFMT_FLAG_BLOCKS
) && resource
->pool
== WINED3D_POOL_DEFAULT
)
1873 || resource
->type
!= WINED3D_RTYPE_TEXTURE_2D
)
1874 return WINED3DERR_INVALIDCALL
;
1877 if (!(resource
->access_flags
& WINED3D_RESOURCE_ACCESS_CPU
))
1879 WARN("Trying to map unmappable texture.\n");
1880 if (resource
->type
!= WINED3D_RTYPE_TEXTURE_2D
)
1881 return WINED3DERR_INVALIDCALL
;
1884 if (texture
->flags
& WINED3D_TEXTURE_DC_IN_USE
)
1886 WARN("DC is in use.\n");
1887 return WINED3DERR_INVALIDCALL
;
1890 if (sub_resource
->map_count
)
1892 WARN("Sub-resource is already mapped.\n");
1893 return WINED3DERR_INVALIDCALL
;
1896 if (device
->d3d_initialized
)
1897 context
= context_acquire(device
, NULL
, 0);
1899 if (flags
& WINED3D_MAP_DISCARD
)
1901 TRACE("WINED3D_MAP_DISCARD flag passed, marking %s as up to date.\n",
1902 wined3d_debug_location(resource
->map_binding
));
1903 if ((ret
= wined3d_texture_prepare_location(texture
, sub_resource_idx
, context
, resource
->map_binding
)))
1904 wined3d_texture_validate_location(texture
, sub_resource_idx
, resource
->map_binding
);
1908 if (resource
->usage
& WINED3DUSAGE_DYNAMIC
)
1909 WARN_(d3d_perf
)("Mapping a dynamic texture without WINED3D_MAP_DISCARD.\n");
1910 ret
= wined3d_texture_load_location(texture
, sub_resource_idx
, context
, resource
->map_binding
);
1915 ERR("Failed to prepare location.\n");
1916 context_release(context
);
1917 return E_OUTOFMEMORY
;
1920 if (!(flags
& WINED3D_MAP_READONLY
)
1921 && (!(flags
& WINED3D_MAP_NO_DIRTY_UPDATE
) || (resource
->usage
& WINED3DUSAGE_DYNAMIC
)))
1922 wined3d_texture_invalidate_location(texture
, sub_resource_idx
, ~resource
->map_binding
);
1924 wined3d_texture_get_memory(texture
, sub_resource_idx
, &data
, resource
->map_binding
);
1925 base_memory
= context_map_bo_address(context
, &data
, sub_resource
->size
, GL_PIXEL_UNPACK_BUFFER
, flags
);
1926 TRACE("Base memory pointer %p.\n", base_memory
);
1929 context_release(context
);
1931 if (fmt_flags
& WINED3DFMT_FLAG_BROKEN_PITCH
)
1933 map_desc
->row_pitch
= wined3d_texture_get_level_width(texture
, texture_level
) * format
->byte_count
;
1934 map_desc
->slice_pitch
= wined3d_texture_get_level_height(texture
, texture_level
) * map_desc
->row_pitch
;
1938 wined3d_texture_get_pitch(texture
, texture_level
, &map_desc
->row_pitch
, &map_desc
->slice_pitch
);
1943 map_desc
->data
= base_memory
;
1947 if ((fmt_flags
& (WINED3DFMT_FLAG_BLOCKS
| WINED3DFMT_FLAG_BROKEN_PITCH
)) == WINED3DFMT_FLAG_BLOCKS
)
1949 /* Compressed textures are block based, so calculate the offset of
1950 * the block that contains the top-left pixel of the mapped box. */
1951 map_desc
->data
= base_memory
1952 + (box
->front
* map_desc
->slice_pitch
)
1953 + ((box
->top
/ format
->block_height
) * map_desc
->row_pitch
)
1954 + ((box
->left
/ format
->block_width
) * format
->block_byte_count
);
1958 map_desc
->data
= base_memory
1959 + (box
->front
* map_desc
->slice_pitch
)
1960 + (box
->top
* map_desc
->row_pitch
)
1961 + (box
->left
* format
->byte_count
);
1965 if (texture
->swapchain
&& texture
->swapchain
->front_buffer
== texture
)
1967 RECT
*r
= &texture
->swapchain
->front_buffer_update
;
1970 SetRect(r
, 0, 0, resource
->width
, resource
->height
);
1972 SetRect(r
, box
->left
, box
->top
, box
->right
, box
->bottom
);
1973 TRACE("Mapped front buffer %s.\n", wine_dbgstr_rect(r
));
1976 ++resource
->map_count
;
1977 ++sub_resource
->map_count
;
1979 TRACE("Returning memory %p, row pitch %u, slice pitch %u.\n",
1980 map_desc
->data
, map_desc
->row_pitch
, map_desc
->slice_pitch
);
1985 static HRESULT
texture_resource_sub_resource_unmap(struct wined3d_resource
*resource
, unsigned int sub_resource_idx
)
1987 struct wined3d_texture_sub_resource
*sub_resource
;
1988 struct wined3d_device
*device
= resource
->device
;
1989 struct wined3d_context
*context
= NULL
;
1990 struct wined3d_texture
*texture
;
1991 struct wined3d_bo_address data
;
1993 TRACE("resource %p, sub_resource_idx %u.\n", resource
, sub_resource_idx
);
1995 texture
= texture_from_resource(resource
);
1996 if (!(sub_resource
= wined3d_texture_get_sub_resource(texture
, sub_resource_idx
)))
1997 return E_INVALIDARG
;
1999 if (!sub_resource
->map_count
)
2001 WARN("Trying to unmap unmapped sub-resource.\n");
2002 if (texture
->flags
& WINED3D_TEXTURE_DC_IN_USE
)
2004 return WINEDDERR_NOTLOCKED
;
2007 if (device
->d3d_initialized
)
2008 context
= context_acquire(device
, NULL
, 0);
2010 wined3d_texture_get_memory(texture
, sub_resource_idx
, &data
, texture
->resource
.map_binding
);
2011 context_unmap_bo_address(context
, &data
, GL_PIXEL_UNPACK_BUFFER
);
2014 context_release(context
);
2016 if (texture
->swapchain
&& texture
->swapchain
->front_buffer
== texture
)
2018 if (!(sub_resource
->locations
& (WINED3D_LOCATION_DRAWABLE
| WINED3D_LOCATION_TEXTURE_RGB
)))
2019 texture
->swapchain
->swapchain_ops
->swapchain_frontbuffer_updated(texture
->swapchain
);
2022 --sub_resource
->map_count
;
2023 if (!--resource
->map_count
&& texture
->update_map_binding
)
2024 wined3d_texture_update_map_binding(texture
);
2029 static const struct wined3d_resource_ops texture_resource_ops
=
2031 texture_resource_incref
,
2032 texture_resource_decref
,
2033 texture_resource_preload
,
2034 wined3d_texture_unload
,
2035 texture_resource_sub_resource_map
,
2036 texture_resource_sub_resource_unmap
,
2039 static HRESULT
texture_init(struct wined3d_texture
*texture
, const struct wined3d_resource_desc
*desc
,
2040 unsigned int layer_count
, unsigned int level_count
, DWORD flags
, struct wined3d_device
*device
,
2041 void *parent
, const struct wined3d_parent_ops
*parent_ops
)
2043 struct wined3d_device_parent
*device_parent
= device
->device_parent
;
2044 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
2045 struct wined3d_surface
*surfaces
;
2046 UINT pow2_width
, pow2_height
;
2050 if (!(desc
->usage
& WINED3DUSAGE_LEGACY_CUBEMAP
) && layer_count
> 1
2051 && !gl_info
->supported
[EXT_TEXTURE_ARRAY
])
2053 WARN("OpenGL implementation does not support array textures.\n");
2054 return WINED3DERR_INVALIDCALL
;
2057 /* TODO: It should only be possible to create textures for formats
2058 * that are reported as supported. */
2059 if (WINED3DFMT_UNKNOWN
>= desc
->format
)
2061 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture
);
2062 return WINED3DERR_INVALIDCALL
;
2065 if (desc
->usage
& WINED3DUSAGE_DYNAMIC
&& desc
->pool
== WINED3D_POOL_MANAGED
)
2066 FIXME("Trying to create a managed texture with dynamic usage.\n");
2067 if (!(desc
->usage
& (WINED3DUSAGE_DYNAMIC
| WINED3DUSAGE_RENDERTARGET
| WINED3DUSAGE_DEPTHSTENCIL
))
2068 && (flags
& WINED3D_TEXTURE_CREATE_MAPPABLE
))
2069 WARN("Creating a mappable texture in the default pool that doesn't specify dynamic usage.\n");
2070 if (desc
->usage
& WINED3DUSAGE_RENDERTARGET
&& desc
->pool
!= WINED3D_POOL_DEFAULT
)
2071 FIXME("Trying to create a render target that isn't in the default pool.\n");
2073 pow2_width
= desc
->width
;
2074 pow2_height
= desc
->height
;
2075 if (((desc
->width
& (desc
->width
- 1)) || (desc
->height
& (desc
->height
- 1)))
2076 && !gl_info
->supported
[ARB_TEXTURE_NON_POWER_OF_TWO
])
2078 /* level_count == 0 returns an error as well. */
2079 if (level_count
!= 1 || layer_count
!= 1)
2081 if (desc
->pool
!= WINED3D_POOL_SCRATCH
)
2083 WARN("Attempted to create a mipmapped/cube/array NPOT texture without unconditional NPOT support.\n");
2084 return WINED3DERR_INVALIDCALL
;
2087 WARN("Creating a scratch mipmapped/cube/array NPOT texture despite lack of HW support.\n");
2089 texture
->flags
|= WINED3D_TEXTURE_COND_NP2
;
2091 if (!gl_info
->supported
[ARB_TEXTURE_RECTANGLE
] && !gl_info
->supported
[WINED3D_GL_NORMALIZED_TEXRECT
])
2093 const struct wined3d_format
*format
= wined3d_get_format(gl_info
, desc
->format
, desc
->usage
);
2095 /* TODO: Add support for non-power-of-two compressed textures. */
2096 if (format
->flags
[WINED3D_GL_RES_TYPE_TEX_2D
]
2097 & (WINED3DFMT_FLAG_COMPRESSED
| WINED3DFMT_FLAG_HEIGHT_SCALE
))
2099 FIXME("Compressed or height scaled non-power-of-two (%ux%u) textures are not supported.\n",
2100 desc
->width
, desc
->height
);
2101 return WINED3DERR_NOTAVAILABLE
;
2104 /* Find the nearest pow2 match. */
2105 pow2_width
= pow2_height
= 1;
2106 while (pow2_width
< desc
->width
)
2108 while (pow2_height
< desc
->height
)
2110 texture
->flags
|= WINED3D_TEXTURE_COND_NP2_EMULATED
;
2113 texture
->pow2_width
= pow2_width
;
2114 texture
->pow2_height
= pow2_height
;
2116 if ((pow2_width
> gl_info
->limits
.texture_size
|| pow2_height
> gl_info
->limits
.texture_size
)
2117 && (desc
->usage
& WINED3DUSAGE_TEXTURE
))
2119 /* One of four options:
2120 * 1: Do the same as we do with NPOT and scale the texture. (Any
2121 * texture ops would require the texture to be scaled which is
2122 * potentially slow.)
2123 * 2: Set the texture to the maximum size (bad idea).
2124 * 3: WARN and return WINED3DERR_NOTAVAILABLE.
2125 * 4: Create the surface, but allow it to be used only for DirectDraw
2126 * Blts. Some apps (e.g. Swat 3) create textures with a height of
2127 * 16 and a width > 3000 and blt 16x16 letter areas from them to
2128 * the render target. */
2129 if (desc
->pool
== WINED3D_POOL_DEFAULT
|| desc
->pool
== WINED3D_POOL_MANAGED
)
2131 WARN("Dimensions (%ux%u) exceed the maximum texture size.\n", pow2_width
, pow2_height
);
2132 return WINED3DERR_NOTAVAILABLE
;
2135 /* We should never use this surface in combination with OpenGL. */
2136 TRACE("Creating an oversized (%ux%u) surface.\n", pow2_width
, pow2_height
);
2139 /* Calculate levels for mip mapping. */
2140 if (desc
->usage
& WINED3DUSAGE_AUTOGENMIPMAP
)
2142 if (!gl_info
->supported
[SGIS_GENERATE_MIPMAP
])
2144 WARN("No mipmap generation support, returning WINED3DERR_INVALIDCALL.\n");
2145 return WINED3DERR_INVALIDCALL
;
2148 if (level_count
!= 1)
2150 WARN("WINED3DUSAGE_AUTOGENMIPMAP is set, and level count != 1, returning WINED3DERR_INVALIDCALL.\n");
2151 return WINED3DERR_INVALIDCALL
;
2155 if (FAILED(hr
= wined3d_texture_init(texture
, &texture2d_ops
, layer_count
, level_count
, desc
,
2156 flags
, device
, parent
, parent_ops
, &texture_resource_ops
)))
2158 WARN("Failed to initialize texture, returning %#x.\n", hr
);
2162 /* Precalculated scaling for 'faked' non power of two texture coords. */
2163 if (texture
->resource
.gl_type
== WINED3D_GL_RES_TYPE_TEX_RECT
)
2165 texture
->pow2_matrix
[0] = (float)desc
->width
;
2166 texture
->pow2_matrix
[5] = (float)desc
->height
;
2167 texture
->flags
&= ~(WINED3D_TEXTURE_POW2_MAT_IDENT
| WINED3D_TEXTURE_NORMALIZED_COORDS
);
2168 texture
->target
= GL_TEXTURE_RECTANGLE_ARB
;
2172 if (texture
->flags
& WINED3D_TEXTURE_COND_NP2_EMULATED
)
2174 texture
->pow2_matrix
[0] = (((float)desc
->width
) / ((float)pow2_width
));
2175 texture
->pow2_matrix
[5] = (((float)desc
->height
) / ((float)pow2_height
));
2176 texture
->flags
&= ~WINED3D_TEXTURE_POW2_MAT_IDENT
;
2180 texture
->pow2_matrix
[0] = 1.0f
;
2181 texture
->pow2_matrix
[5] = 1.0f
;
2183 if (desc
->usage
& WINED3DUSAGE_LEGACY_CUBEMAP
)
2184 texture
->target
= GL_TEXTURE_CUBE_MAP_ARB
;
2185 else if (layer_count
> 1)
2186 texture
->target
= GL_TEXTURE_2D_ARRAY
;
2188 texture
->target
= GL_TEXTURE_2D
;
2190 texture
->pow2_matrix
[10] = 1.0f
;
2191 texture
->pow2_matrix
[15] = 1.0f
;
2192 TRACE("x scale %.8e, y scale %.8e.\n", texture
->pow2_matrix
[0], texture
->pow2_matrix
[5]);
2194 if (wined3d_texture_use_pbo(texture
, gl_info
))
2195 texture
->resource
.map_binding
= WINED3D_LOCATION_BUFFER
;
2197 if (level_count
> ~(SIZE_T
)0 / layer_count
2198 || !(surfaces
= wined3d_calloc(level_count
* layer_count
, sizeof(*surfaces
))))
2200 wined3d_texture_cleanup_sync(texture
);
2201 return E_OUTOFMEMORY
;
2204 /* Generate all the surfaces. */
2205 for (i
= 0; i
< texture
->level_count
; ++i
)
2207 for (j
= 0; j
< texture
->layer_count
; ++j
)
2209 static const GLenum cube_targets
[6] =
2211 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
,
2212 GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
,
2213 GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
,
2214 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
,
2215 GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
,
2216 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
,
2218 struct wined3d_texture_sub_resource
*sub_resource
;
2219 unsigned int idx
= j
* texture
->level_count
+ i
;
2220 struct wined3d_surface
*surface
;
2222 surface
= &surfaces
[idx
];
2223 surface
->container
= texture
;
2224 surface
->texture_target
= desc
->usage
& WINED3DUSAGE_LEGACY_CUBEMAP
? cube_targets
[j
] : texture
->target
;
2225 surface
->texture_level
= i
;
2226 surface
->texture_layer
= j
;
2227 list_init(&surface
->renderbuffers
);
2228 list_init(&surface
->overlays
);
2230 sub_resource
= &texture
->sub_resources
[idx
];
2231 sub_resource
->locations
= WINED3D_LOCATION_DISCARDED
;
2232 sub_resource
->u
.surface
= surface
;
2233 if (!(texture
->resource
.usage
& WINED3DUSAGE_DEPTHSTENCIL
))
2235 wined3d_texture_validate_location(texture
, idx
, WINED3D_LOCATION_SYSMEM
);
2236 wined3d_texture_invalidate_location(texture
, idx
, ~WINED3D_LOCATION_SYSMEM
);
2239 if (FAILED(hr
= device_parent
->ops
->surface_created(device_parent
,
2240 texture
, idx
, &sub_resource
->parent
, &sub_resource
->parent_ops
)))
2242 WARN("Failed to create surface parent, hr %#x.\n", hr
);
2243 sub_resource
->parent
= NULL
;
2244 wined3d_texture_cleanup_sync(texture
);
2248 TRACE("parent %p, parent_ops %p.\n", sub_resource
->parent
, sub_resource
->parent_ops
);
2250 TRACE("Created surface level %u, layer %u @ %p.\n", i
, j
, surface
);
2252 if ((desc
->usage
& WINED3DUSAGE_OWNDC
) || (device
->wined3d
->flags
& WINED3D_NO3D
))
2254 wined3d_cs_init_object(device
->cs
, texture2d_create_dc
, surface
);
2255 device
->cs
->ops
->finish(device
->cs
, WINED3D_CS_QUEUE_DEFAULT
);
2258 wined3d_texture_cleanup_sync(texture
);
2259 return WINED3DERR_INVALIDCALL
;
2268 /* This call just uploads data, the caller is responsible for binding the
2269 * correct texture. */
2270 /* Context activation is done by the caller. */
2271 static void texture3d_upload_data(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
,
2272 const struct wined3d_context
*context
, const struct wined3d_box
*box
,
2273 const struct wined3d_const_bo_address
*data
, unsigned int row_pitch
, unsigned int slice_pitch
)
2275 const struct wined3d_format
*format
= texture
->resource
.format
;
2276 unsigned int level
= sub_resource_idx
% texture
->level_count
;
2277 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2278 unsigned int x
, y
, z
, update_w
, update_h
, update_d
;
2279 unsigned int dst_row_pitch
, dst_slice_pitch
;
2280 unsigned int width
, height
, depth
;
2281 const void *mem
= data
->addr
;
2282 void *converted_mem
= NULL
;
2284 TRACE("texture %p, sub_resource_idx %u, context %p, box %s, data {%#x:%p}, row_pitch %#x, slice_pitch %#x.\n",
2285 texture
, sub_resource_idx
, context
, debug_box(box
),
2286 data
->buffer_object
, data
->addr
, row_pitch
, slice_pitch
);
2288 width
= wined3d_texture_get_level_width(texture
, level
);
2289 height
= wined3d_texture_get_level_height(texture
, level
);
2290 depth
= wined3d_texture_get_level_depth(texture
, level
);
2304 update_w
= box
->right
- box
->left
;
2305 update_h
= box
->bottom
- box
->top
;
2306 update_d
= box
->back
- box
->front
;
2309 if (format
->convert
)
2311 if (data
->buffer_object
)
2312 ERR("Loading a converted texture from a PBO.\n");
2313 if (texture
->resource
.format_flags
& WINED3DFMT_FLAG_BLOCKS
)
2314 ERR("Converting a block-based format.\n");
2316 dst_row_pitch
= update_w
* format
->conv_byte_count
;
2317 dst_slice_pitch
= dst_row_pitch
* update_h
;
2319 converted_mem
= wined3d_calloc(update_d
, dst_slice_pitch
);
2320 format
->convert(data
->addr
, converted_mem
, row_pitch
, slice_pitch
,
2321 dst_row_pitch
, dst_slice_pitch
, update_w
, update_h
, update_d
);
2322 mem
= converted_mem
;
2326 wined3d_texture_get_pitch(texture
, sub_resource_idx
, &dst_row_pitch
, &dst_slice_pitch
);
2327 if (row_pitch
!= dst_row_pitch
|| slice_pitch
!= dst_slice_pitch
)
2328 FIXME("Ignoring row/slice pitch (%u/%u).\n", row_pitch
, slice_pitch
);
2331 if (data
->buffer_object
)
2333 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER
, data
->buffer_object
));
2334 checkGLcall("glBindBuffer");
2337 GL_EXTCALL(glTexSubImage3D(GL_TEXTURE_3D
, level
, x
, y
, z
,
2338 update_w
, update_h
, update_d
, format
->glFormat
, format
->glType
, mem
));
2339 checkGLcall("glTexSubImage3D");
2341 if (data
->buffer_object
)
2343 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER
, 0));
2344 checkGLcall("glBindBuffer");
2347 HeapFree(GetProcessHeap(), 0, converted_mem
);
2350 /* Context activation is done by the caller. */
2351 static void texture3d_download_data(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
,
2352 const struct wined3d_context
*context
, const struct wined3d_bo_address
*data
)
2354 const struct wined3d_format
*format
= texture
->resource
.format
;
2355 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2357 if (format
->convert
)
2359 FIXME("Attempting to download a converted volume, format %s.\n",
2360 debug_d3dformat(format
->id
));
2364 if (data
->buffer_object
)
2366 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER
, data
->buffer_object
));
2367 checkGLcall("glBindBuffer");
2370 gl_info
->gl_ops
.gl
.p_glGetTexImage(GL_TEXTURE_3D
, sub_resource_idx
,
2371 format
->glFormat
, format
->glType
, data
->addr
);
2372 checkGLcall("glGetTexImage");
2374 if (data
->buffer_object
)
2376 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER
, 0));
2377 checkGLcall("glBindBuffer");
2382 /* Context activation is done by the caller. */
2383 static void texture3d_srgb_transfer(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
,
2384 struct wined3d_context
*context
, BOOL dest_is_srgb
)
2386 struct wined3d_texture_sub_resource
*sub_resource
= &texture
->sub_resources
[sub_resource_idx
];
2387 unsigned int row_pitch
, slice_pitch
;
2388 struct wined3d_bo_address data
;
2390 /* Optimisations are possible, but the effort should be put into either
2391 * implementing EXT_SRGB_DECODE in the driver or finding out why we
2392 * picked the wrong copy for the original upload and fixing that.
2394 * Also keep in mind that we want to avoid using resource.heap_memory
2395 * for DEFAULT pool surfaces. */
2396 WARN_(d3d_perf
)("Performing slow rgb/srgb volume transfer.\n");
2397 data
.buffer_object
= 0;
2398 if (!(data
.addr
= HeapAlloc(GetProcessHeap(), 0, sub_resource
->size
)))
2401 wined3d_texture_get_pitch(texture
, sub_resource_idx
, &row_pitch
, &slice_pitch
);
2402 wined3d_texture_bind_and_dirtify(texture
, context
, !dest_is_srgb
);
2403 texture3d_download_data(texture
, sub_resource_idx
, context
, &data
);
2404 wined3d_texture_bind_and_dirtify(texture
, context
, dest_is_srgb
);
2405 texture3d_upload_data(texture
, sub_resource_idx
, context
,
2406 NULL
, wined3d_const_bo_address(&data
), row_pitch
, slice_pitch
);
2408 HeapFree(GetProcessHeap(), 0, data
.addr
);
2411 /* Context activation is done by the caller. */
2412 static BOOL
texture3d_load_location(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
,
2413 struct wined3d_context
*context
, DWORD location
)
2415 struct wined3d_texture_sub_resource
*sub_resource
= &texture
->sub_resources
[sub_resource_idx
];
2416 unsigned int row_pitch
, slice_pitch
;
2418 if (!wined3d_texture_prepare_location(texture
, sub_resource_idx
, context
, location
))
2423 case WINED3D_LOCATION_TEXTURE_RGB
:
2424 case WINED3D_LOCATION_TEXTURE_SRGB
:
2425 if (sub_resource
->locations
& WINED3D_LOCATION_SYSMEM
)
2427 struct wined3d_const_bo_address data
= {0, texture
->resource
.heap_memory
};
2428 data
.addr
+= sub_resource
->offset
;
2429 wined3d_texture_bind_and_dirtify(texture
, context
,
2430 location
== WINED3D_LOCATION_TEXTURE_SRGB
);
2431 wined3d_texture_get_pitch(texture
, sub_resource_idx
, &row_pitch
, &slice_pitch
);
2432 texture3d_upload_data(texture
, sub_resource_idx
, context
, NULL
, &data
, row_pitch
, slice_pitch
);
2434 else if (sub_resource
->locations
& WINED3D_LOCATION_BUFFER
)
2436 struct wined3d_const_bo_address data
= {sub_resource
->buffer_object
, NULL
};
2437 wined3d_texture_bind_and_dirtify(texture
, context
,
2438 location
== WINED3D_LOCATION_TEXTURE_SRGB
);
2439 wined3d_texture_get_pitch(texture
, sub_resource_idx
, &row_pitch
, &slice_pitch
);
2440 texture3d_upload_data(texture
, sub_resource_idx
, context
, NULL
, &data
, row_pitch
, slice_pitch
);
2442 else if (sub_resource
->locations
& WINED3D_LOCATION_TEXTURE_RGB
)
2444 texture3d_srgb_transfer(texture
, sub_resource_idx
, context
, TRUE
);
2446 else if (sub_resource
->locations
& WINED3D_LOCATION_TEXTURE_SRGB
)
2448 texture3d_srgb_transfer(texture
, sub_resource_idx
, context
, FALSE
);
2452 FIXME("Implement texture loading from %s.\n", wined3d_debug_location(sub_resource
->locations
));
2457 case WINED3D_LOCATION_SYSMEM
:
2458 if (sub_resource
->locations
& (WINED3D_LOCATION_TEXTURE_RGB
| WINED3D_LOCATION_TEXTURE_SRGB
))
2460 struct wined3d_bo_address data
= {0, texture
->resource
.heap_memory
};
2462 data
.addr
+= sub_resource
->offset
;
2463 if (sub_resource
->locations
& WINED3D_LOCATION_TEXTURE_RGB
)
2464 wined3d_texture_bind_and_dirtify(texture
, context
, FALSE
);
2466 wined3d_texture_bind_and_dirtify(texture
, context
, TRUE
);
2468 texture3d_download_data(texture
, sub_resource_idx
, context
, &data
);
2469 ++texture
->download_count
;
2473 FIXME("Implement WINED3D_LOCATION_SYSMEM loading from %s.\n",
2474 wined3d_debug_location(sub_resource
->locations
));
2479 case WINED3D_LOCATION_BUFFER
:
2480 if (sub_resource
->locations
& (WINED3D_LOCATION_TEXTURE_RGB
| WINED3D_LOCATION_TEXTURE_SRGB
))
2482 struct wined3d_bo_address data
= {sub_resource
->buffer_object
, NULL
};
2484 if (sub_resource
->locations
& WINED3D_LOCATION_TEXTURE_RGB
)
2485 wined3d_texture_bind_and_dirtify(texture
, context
, FALSE
);
2487 wined3d_texture_bind_and_dirtify(texture
, context
, TRUE
);
2489 texture3d_download_data(texture
, sub_resource_idx
, context
, &data
);
2493 FIXME("Implement WINED3D_LOCATION_BUFFER loading from %s.\n",
2494 wined3d_debug_location(sub_resource
->locations
));
2500 FIXME("Implement %s loading from %s.\n", wined3d_debug_location(location
),
2501 wined3d_debug_location(sub_resource
->locations
));
2508 static void texture3d_prepare_texture(struct wined3d_texture
*texture
, struct wined3d_context
*context
, BOOL srgb
)
2510 const struct wined3d_format
*format
= texture
->resource
.format
;
2511 GLenum internal
= srgb
? format
->glGammaInternal
: format
->glInternal
;
2512 unsigned int sub_count
= texture
->level_count
* texture
->layer_count
;
2513 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2516 wined3d_texture_bind_and_dirtify(texture
, context
, srgb
);
2518 if (wined3d_texture_use_immutable_storage(texture
, gl_info
))
2520 GL_EXTCALL(glTexStorage3D(GL_TEXTURE_3D
, texture
->level_count
, internal
,
2521 wined3d_texture_get_level_width(texture
, 0),
2522 wined3d_texture_get_level_height(texture
, 0),
2523 wined3d_texture_get_level_depth(texture
, 0)));
2524 checkGLcall("glTexStorage3D");
2528 for (i
= 0; i
< sub_count
; ++i
)
2530 GL_EXTCALL(glTexImage3D(GL_TEXTURE_3D
, i
, internal
,
2531 wined3d_texture_get_level_width(texture
, i
),
2532 wined3d_texture_get_level_height(texture
, i
),
2533 wined3d_texture_get_level_depth(texture
, i
),
2534 0, format
->glFormat
, format
->glType
, NULL
));
2535 checkGLcall("glTexImage3D");
2540 static void texture3d_cleanup_sub_resources(struct wined3d_texture
*texture
)
2544 static const struct wined3d_texture_ops texture3d_ops
=
2546 texture3d_upload_data
,
2547 texture3d_load_location
,
2548 texture3d_prepare_texture
,
2549 texture3d_cleanup_sub_resources
,
2552 static HRESULT
volumetexture_init(struct wined3d_texture
*texture
, const struct wined3d_resource_desc
*desc
,
2553 UINT layer_count
, UINT level_count
, DWORD flags
, struct wined3d_device
*device
, void *parent
,
2554 const struct wined3d_parent_ops
*parent_ops
)
2556 struct wined3d_device_parent
*device_parent
= device
->device_parent
;
2557 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
2561 if (layer_count
!= 1)
2563 ERR("Invalid layer count for volume texture.\n");
2564 return E_INVALIDARG
;
2567 /* TODO: It should only be possible to create textures for formats
2568 * that are reported as supported. */
2569 if (WINED3DFMT_UNKNOWN
>= desc
->format
)
2571 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture
);
2572 return WINED3DERR_INVALIDCALL
;
2575 if (!gl_info
->supported
[EXT_TEXTURE3D
])
2577 WARN("(%p) : Texture cannot be created - no volume texture support.\n", texture
);
2578 return WINED3DERR_INVALIDCALL
;
2581 /* Calculate levels for mip mapping. */
2582 if (desc
->usage
& WINED3DUSAGE_AUTOGENMIPMAP
)
2584 if (!gl_info
->supported
[SGIS_GENERATE_MIPMAP
])
2586 WARN("No mipmap generation support, returning D3DERR_INVALIDCALL.\n");
2587 return WINED3DERR_INVALIDCALL
;
2590 if (level_count
!= 1)
2592 WARN("WINED3DUSAGE_AUTOGENMIPMAP is set, and level count != 1, returning D3DERR_INVALIDCALL.\n");
2593 return WINED3DERR_INVALIDCALL
;
2597 if (desc
->usage
& WINED3DUSAGE_DYNAMIC
&& (desc
->pool
== WINED3D_POOL_MANAGED
2598 || desc
->pool
== WINED3D_POOL_SCRATCH
))
2600 WARN("Attempted to create a DYNAMIC texture in pool %s.\n", debug_d3dpool(desc
->pool
));
2601 return WINED3DERR_INVALIDCALL
;
2604 if (!gl_info
->supported
[ARB_TEXTURE_NON_POWER_OF_TWO
])
2606 UINT pow2_w
, pow2_h
, pow2_d
;
2608 while (pow2_w
< desc
->width
)
2611 while (pow2_h
< desc
->height
)
2614 while (pow2_d
< desc
->depth
)
2617 if (pow2_w
!= desc
->width
|| pow2_h
!= desc
->height
|| pow2_d
!= desc
->depth
)
2619 if (desc
->pool
== WINED3D_POOL_SCRATCH
)
2621 WARN("Creating a scratch NPOT volume texture despite lack of HW support.\n");
2625 WARN("Attempted to create a NPOT volume texture (%u, %u, %u) without GL support.\n",
2626 desc
->width
, desc
->height
, desc
->depth
);
2627 return WINED3DERR_INVALIDCALL
;
2632 if (FAILED(hr
= wined3d_texture_init(texture
, &texture3d_ops
, 1, level_count
, desc
,
2633 flags
, device
, parent
, parent_ops
, &texture_resource_ops
)))
2635 WARN("Failed to initialize texture, returning %#x.\n", hr
);
2639 texture
->pow2_matrix
[0] = 1.0f
;
2640 texture
->pow2_matrix
[5] = 1.0f
;
2641 texture
->pow2_matrix
[10] = 1.0f
;
2642 texture
->pow2_matrix
[15] = 1.0f
;
2643 texture
->target
= GL_TEXTURE_3D
;
2645 if (wined3d_texture_use_pbo(texture
, gl_info
))
2647 wined3d_resource_free_sysmem(&texture
->resource
);
2648 texture
->resource
.map_binding
= WINED3D_LOCATION_BUFFER
;
2651 /* Generate all the surfaces. */
2652 for (i
= 0; i
< texture
->level_count
; ++i
)
2654 struct wined3d_texture_sub_resource
*sub_resource
;
2656 sub_resource
= &texture
->sub_resources
[i
];
2657 sub_resource
->locations
= WINED3D_LOCATION_DISCARDED
;
2659 if (FAILED(hr
= device_parent
->ops
->volume_created(device_parent
,
2660 texture
, i
, &sub_resource
->parent
, &sub_resource
->parent_ops
)))
2662 WARN("Failed to create volume parent, hr %#x.\n", hr
);
2663 sub_resource
->parent
= NULL
;
2664 wined3d_texture_cleanup_sync(texture
);
2668 TRACE("parent %p, parent_ops %p.\n", parent
, parent_ops
);
2670 TRACE("Created volume level %u.\n", i
);
2676 HRESULT CDECL
wined3d_texture_blt(struct wined3d_texture
*dst_texture
, unsigned int dst_sub_resource_idx
,
2677 const RECT
*dst_rect
, struct wined3d_texture
*src_texture
, unsigned int src_sub_resource_idx
,
2678 const RECT
*src_rect
, DWORD flags
, const struct wined3d_blt_fx
*fx
, enum wined3d_texture_filter_type filter
)
2680 struct wined3d_box src_box
= {src_rect
->left
, src_rect
->top
, src_rect
->right
, src_rect
->bottom
, 0, 1};
2681 struct wined3d_box dst_box
= {dst_rect
->left
, dst_rect
->top
, dst_rect
->right
, dst_rect
->bottom
, 0, 1};
2682 unsigned int dst_format_flags
, src_format_flags
= 0;
2685 TRACE("dst_texture %p, dst_sub_resource_idx %u, dst_rect %s, src_texture %p, "
2686 "src_sub_resource_idx %u, src_rect %s, flags %#x, fx %p, filter %s.\n",
2687 dst_texture
, dst_sub_resource_idx
, wine_dbgstr_rect(dst_rect
), src_texture
,
2688 src_sub_resource_idx
, wine_dbgstr_rect(src_rect
), flags
, fx
, debug_d3dtexturefiltertype(filter
));
2690 if (dst_sub_resource_idx
>= dst_texture
->level_count
* dst_texture
->layer_count
2691 || dst_texture
->resource
.type
!= WINED3D_RTYPE_TEXTURE_2D
)
2692 return WINED3DERR_INVALIDCALL
;
2694 if (src_sub_resource_idx
>= src_texture
->level_count
* src_texture
->layer_count
2695 || src_texture
->resource
.type
!= WINED3D_RTYPE_TEXTURE_2D
)
2696 return WINED3DERR_INVALIDCALL
;
2698 dst_format_flags
= dst_texture
->resource
.format_flags
;
2699 if (FAILED(hr
= wined3d_texture_check_box_dimensions(dst_texture
,
2700 dst_sub_resource_idx
% dst_texture
->level_count
, &dst_box
)))
2703 src_format_flags
= src_texture
->resource
.format_flags
;
2704 if (FAILED(hr
= wined3d_texture_check_box_dimensions(src_texture
,
2705 src_sub_resource_idx
% src_texture
->level_count
, &src_box
)))
2708 if (dst_texture
->sub_resources
[dst_sub_resource_idx
].map_count
2709 || src_texture
->sub_resources
[src_sub_resource_idx
].map_count
)
2711 WARN("Sub-resource is busy, returning WINEDDERR_SURFACEBUSY.\n");
2712 return WINEDDERR_SURFACEBUSY
;
2715 if ((src_format_flags
& (WINED3DFMT_FLAG_DEPTH
| WINED3DFMT_FLAG_STENCIL
))
2716 != (dst_format_flags
& (WINED3DFMT_FLAG_DEPTH
| WINED3DFMT_FLAG_STENCIL
)))
2718 WARN("Rejecting depth/stencil blit between incompatible formats.\n");
2719 return WINED3DERR_INVALIDCALL
;
2722 wined3d_cs_emit_blt_sub_resource(dst_texture
->resource
.device
->cs
, &dst_texture
->resource
, dst_sub_resource_idx
,
2723 &dst_box
, &src_texture
->resource
, src_sub_resource_idx
, &src_box
, flags
, fx
, filter
);
2728 HRESULT CDECL
wined3d_texture_get_overlay_position(const struct wined3d_texture
*texture
,
2729 unsigned int sub_resource_idx
, LONG
*x
, LONG
*y
)
2731 struct wined3d_surface
*surface
;
2733 TRACE("texture %p, sub_resource_idx %u, x %p, y %p.\n", texture
, sub_resource_idx
, x
, y
);
2735 if (!(texture
->resource
.usage
& WINED3DUSAGE_OVERLAY
) || texture
->resource
.type
!= WINED3D_RTYPE_TEXTURE_2D
2736 || sub_resource_idx
>= texture
->level_count
* texture
->layer_count
)
2738 WARN("Invalid sub-resource specified.\n");
2739 return WINEDDERR_NOTAOVERLAYSURFACE
;
2742 surface
= texture
->sub_resources
[sub_resource_idx
].u
.surface
;
2743 if (!surface
->overlay_dest
)
2745 TRACE("Overlay not visible.\n");
2748 return WINEDDERR_OVERLAYNOTVISIBLE
;
2751 *x
= surface
->overlay_destrect
.left
;
2752 *y
= surface
->overlay_destrect
.top
;
2754 TRACE("Returning position %d, %d.\n", *x
, *y
);
2759 HRESULT CDECL
wined3d_texture_set_overlay_position(struct wined3d_texture
*texture
,
2760 unsigned int sub_resource_idx
, LONG x
, LONG y
)
2762 struct wined3d_texture_sub_resource
*sub_resource
;
2763 struct wined3d_surface
*surface
;
2766 TRACE("texture %p, sub_resource_idx %u, x %d, y %d.\n", texture
, sub_resource_idx
, x
, y
);
2768 if (!(texture
->resource
.usage
& WINED3DUSAGE_OVERLAY
) || texture
->resource
.type
!= WINED3D_RTYPE_TEXTURE_2D
2769 || !(sub_resource
= wined3d_texture_get_sub_resource(texture
, sub_resource_idx
)))
2771 WARN("Invalid sub-resource specified.\n");
2772 return WINEDDERR_NOTAOVERLAYSURFACE
;
2775 surface
= sub_resource
->u
.surface
;
2776 w
= surface
->overlay_destrect
.right
- surface
->overlay_destrect
.left
;
2777 h
= surface
->overlay_destrect
.bottom
- surface
->overlay_destrect
.top
;
2778 SetRect(&surface
->overlay_destrect
, x
, y
, x
+ w
, y
+ h
);
2783 HRESULT CDECL
wined3d_texture_update_overlay(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
,
2784 const RECT
*src_rect
, struct wined3d_texture
*dst_texture
, unsigned int dst_sub_resource_idx
,
2785 const RECT
*dst_rect
, DWORD flags
)
2787 struct wined3d_texture_sub_resource
*sub_resource
, *dst_sub_resource
;
2788 struct wined3d_surface
*surface
, *dst_surface
;
2790 TRACE("texture %p, sub_resource_idx %u, src_rect %s, dst_texture %p, "
2791 "dst_sub_resource_idx %u, dst_rect %s, flags %#x.\n",
2792 texture
, sub_resource_idx
, wine_dbgstr_rect(src_rect
), dst_texture
,
2793 dst_sub_resource_idx
, wine_dbgstr_rect(dst_rect
), flags
);
2795 if (!(texture
->resource
.usage
& WINED3DUSAGE_OVERLAY
) || texture
->resource
.type
!= WINED3D_RTYPE_TEXTURE_2D
2796 || !(sub_resource
= wined3d_texture_get_sub_resource(texture
, sub_resource_idx
)))
2798 WARN("Invalid sub-resource specified.\n");
2799 return WINEDDERR_NOTAOVERLAYSURFACE
;
2802 if (!dst_texture
|| dst_texture
->resource
.type
!= WINED3D_RTYPE_TEXTURE_2D
2803 || !(dst_sub_resource
= wined3d_texture_get_sub_resource(dst_texture
, dst_sub_resource_idx
)))
2805 WARN("Invalid destination sub-resource specified.\n");
2806 return WINED3DERR_INVALIDCALL
;
2809 surface
= sub_resource
->u
.surface
;
2811 surface
->overlay_srcrect
= *src_rect
;
2813 SetRect(&surface
->overlay_srcrect
, 0, 0,
2814 wined3d_texture_get_level_width(texture
, surface
->texture_level
),
2815 wined3d_texture_get_level_height(texture
, surface
->texture_level
));
2817 dst_surface
= dst_sub_resource
->u
.surface
;
2819 surface
->overlay_destrect
= *dst_rect
;
2821 SetRect(&surface
->overlay_destrect
, 0, 0,
2822 wined3d_texture_get_level_width(dst_texture
, dst_surface
->texture_level
),
2823 wined3d_texture_get_level_height(dst_texture
, dst_surface
->texture_level
));
2825 if (surface
->overlay_dest
&& (surface
->overlay_dest
!= dst_surface
|| flags
& WINEDDOVER_HIDE
))
2827 surface
->overlay_dest
= NULL
;
2828 list_remove(&surface
->overlay_entry
);
2831 if (flags
& WINEDDOVER_SHOW
)
2833 if (surface
->overlay_dest
!= dst_surface
)
2835 surface
->overlay_dest
= dst_surface
;
2836 list_add_tail(&dst_surface
->overlays
, &surface
->overlay_entry
);
2839 else if (flags
& WINEDDOVER_HIDE
)
2841 /* Tests show that the rectangles are erased on hide. */
2842 SetRectEmpty(&surface
->overlay_srcrect
);
2843 SetRectEmpty(&surface
->overlay_destrect
);
2844 surface
->overlay_dest
= NULL
;
2850 void * CDECL
wined3d_texture_get_sub_resource_parent(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
)
2852 unsigned int sub_count
= texture
->level_count
* texture
->layer_count
;
2854 TRACE("texture %p, sub_resource_idx %u.\n", texture
, sub_resource_idx
);
2856 if (sub_resource_idx
>= sub_count
)
2858 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx
, sub_count
);
2862 return texture
->sub_resources
[sub_resource_idx
].parent
;
2865 void CDECL
wined3d_texture_set_sub_resource_parent(struct wined3d_texture
*texture
,
2866 unsigned int sub_resource_idx
, void *parent
)
2868 unsigned int sub_count
= texture
->level_count
* texture
->layer_count
;
2870 TRACE("texture %p, sub_resource_idx %u, parent %p.\n", texture
, sub_resource_idx
, parent
);
2872 if (sub_resource_idx
>= sub_count
)
2874 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx
, sub_count
);
2878 texture
->sub_resources
[sub_resource_idx
].parent
= parent
;
2881 HRESULT CDECL
wined3d_texture_get_sub_resource_desc(const struct wined3d_texture
*texture
,
2882 unsigned int sub_resource_idx
, struct wined3d_sub_resource_desc
*desc
)
2884 unsigned int sub_count
= texture
->level_count
* texture
->layer_count
;
2885 const struct wined3d_resource
*resource
;
2886 unsigned int level_idx
;
2888 TRACE("texture %p, sub_resource_idx %u, desc %p.\n", texture
, sub_resource_idx
, desc
);
2890 if (sub_resource_idx
>= sub_count
)
2892 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx
, sub_count
);
2893 return WINED3DERR_INVALIDCALL
;
2896 resource
= &texture
->resource
;
2897 desc
->format
= resource
->format
->id
;
2898 desc
->multisample_type
= resource
->multisample_type
;
2899 desc
->multisample_quality
= resource
->multisample_quality
;
2900 desc
->usage
= resource
->usage
;
2901 desc
->pool
= resource
->pool
;
2903 level_idx
= sub_resource_idx
% texture
->level_count
;
2904 desc
->width
= wined3d_texture_get_level_width(texture
, level_idx
);
2905 desc
->height
= wined3d_texture_get_level_height(texture
, level_idx
);
2906 desc
->depth
= wined3d_texture_get_level_depth(texture
, level_idx
);
2907 desc
->size
= texture
->sub_resources
[sub_resource_idx
].size
;
2912 HRESULT CDECL
wined3d_texture_create(struct wined3d_device
*device
, const struct wined3d_resource_desc
*desc
,
2913 UINT layer_count
, UINT level_count
, DWORD flags
, const struct wined3d_sub_resource_data
*data
,
2914 void *parent
, const struct wined3d_parent_ops
*parent_ops
, struct wined3d_texture
**texture
)
2916 struct wined3d_texture
*object
;
2919 TRACE("device %p, desc %p, layer_count %u, level_count %u, flags %#x, data %p, "
2920 "parent %p, parent_ops %p, texture %p.\n",
2921 device
, desc
, layer_count
, level_count
, flags
, data
, parent
, parent_ops
, texture
);
2925 WARN("Invalid layer count.\n");
2926 return E_INVALIDARG
;
2928 if ((desc
->usage
& WINED3DUSAGE_LEGACY_CUBEMAP
) && layer_count
!= 6)
2930 ERR("Invalid layer count %u for legacy cubemap.\n", layer_count
);
2936 WARN("Invalid level count.\n");
2937 return WINED3DERR_INVALIDCALL
;
2940 if (desc
->multisample_type
!= WINED3D_MULTISAMPLE_NONE
)
2942 const struct wined3d_format
*format
= wined3d_get_format(&device
->adapter
->gl_info
,
2943 desc
->format
, desc
->usage
);
2945 if (desc
->multisample_type
== WINED3D_MULTISAMPLE_NON_MASKABLE
2946 && desc
->multisample_quality
>= wined3d_popcount(format
->multisample_types
))
2948 WARN("Unsupported quality level %u requested for WINED3D_MULTISAMPLE_NON_MASKABLE.\n",
2949 desc
->multisample_quality
);
2950 return WINED3DERR_NOTAVAILABLE
;
2952 if (desc
->multisample_type
!= WINED3D_MULTISAMPLE_NON_MASKABLE
2953 && (!(format
->multisample_types
& 1u << (desc
->multisample_type
- 1))
2954 || desc
->multisample_quality
))
2956 WARN("Unsupported multisample type %u quality %u requested.\n", desc
->multisample_type
,
2957 desc
->multisample_quality
);
2958 return WINED3DERR_NOTAVAILABLE
;
2962 if (!(object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
2963 FIELD_OFFSET(struct wined3d_texture
, sub_resources
[level_count
* layer_count
]))))
2964 return E_OUTOFMEMORY
;
2966 switch (desc
->resource_type
)
2968 case WINED3D_RTYPE_TEXTURE_2D
:
2969 hr
= texture_init(object
, desc
, layer_count
, level_count
, flags
, device
, parent
, parent_ops
);
2972 case WINED3D_RTYPE_TEXTURE_3D
:
2973 hr
= volumetexture_init(object
, desc
, layer_count
, level_count
, flags
, device
, parent
, parent_ops
);
2977 ERR("Invalid resource type %s.\n", debug_d3dresourcetype(desc
->resource_type
));
2978 hr
= WINED3DERR_INVALIDCALL
;
2984 WARN("Failed to initialize texture, returning %#x.\n", hr
);
2985 HeapFree(GetProcessHeap(), 0, object
);
2989 /* FIXME: We'd like to avoid ever allocating system memory for the texture
2993 unsigned int sub_count
= level_count
* layer_count
;
2996 for (i
= 0; i
< sub_count
; ++i
)
3000 WARN("Invalid sub-resource data specified for sub-resource %u.\n", i
);
3001 wined3d_texture_cleanup_sync(object
);
3002 HeapFree(GetProcessHeap(), 0, object
);
3003 return E_INVALIDARG
;
3007 for (i
= 0; i
< sub_count
; ++i
)
3009 wined3d_device_update_sub_resource(device
, &object
->resource
,
3010 i
, NULL
, data
[i
].data
, data
[i
].row_pitch
, data
[i
].slice_pitch
);
3014 TRACE("Created texture %p.\n", object
);
3020 HRESULT CDECL
wined3d_texture_get_dc(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
, HDC
*dc
)
3022 struct wined3d_device
*device
= texture
->resource
.device
;
3023 struct wined3d_texture_sub_resource
*sub_resource
;
3024 struct wined3d_surface
*surface
;
3026 TRACE("texture %p, sub_resource_idx %u, dc %p.\n", texture
, sub_resource_idx
, dc
);
3028 if (!(texture
->flags
& WINED3D_TEXTURE_GET_DC
))
3030 WARN("Texture does not support GetDC\n");
3031 /* Don't touch the DC */
3032 return WINED3DERR_INVALIDCALL
;
3035 if (!(sub_resource
= wined3d_texture_get_sub_resource(texture
, sub_resource_idx
)))
3036 return WINED3DERR_INVALIDCALL
;
3038 if (texture
->resource
.type
!= WINED3D_RTYPE_TEXTURE_2D
)
3040 WARN("Not supported on %s resources.\n", debug_d3dresourcetype(texture
->resource
.type
));
3041 return WINED3DERR_INVALIDCALL
;
3044 surface
= sub_resource
->u
.surface
;
3046 if (texture
->resource
.map_count
&& !(texture
->flags
& WINED3D_TEXTURE_GET_DC_LENIENT
))
3047 return WINED3DERR_INVALIDCALL
;
3051 wined3d_cs_init_object(device
->cs
, texture2d_create_dc
, surface
);
3052 device
->cs
->ops
->finish(device
->cs
, WINED3D_CS_QUEUE_DEFAULT
);
3055 return WINED3DERR_INVALIDCALL
;
3057 if (!(texture
->flags
& WINED3D_TEXTURE_GET_DC_LENIENT
))
3058 texture
->flags
|= WINED3D_TEXTURE_DC_IN_USE
;
3059 ++texture
->resource
.map_count
;
3060 ++sub_resource
->map_count
;
3063 TRACE("Returning dc %p.\n", *dc
);
3068 HRESULT CDECL
wined3d_texture_release_dc(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
, HDC dc
)
3070 struct wined3d_device
*device
= texture
->resource
.device
;
3071 struct wined3d_texture_sub_resource
*sub_resource
;
3072 struct wined3d_surface
*surface
;
3074 TRACE("texture %p, sub_resource_idx %u, dc %p.\n", texture
, sub_resource_idx
, dc
);
3076 if (!(sub_resource
= wined3d_texture_get_sub_resource(texture
, sub_resource_idx
)))
3077 return WINED3DERR_INVALIDCALL
;
3079 if (texture
->resource
.type
!= WINED3D_RTYPE_TEXTURE_2D
)
3081 WARN("Not supported on %s resources.\n", debug_d3dresourcetype(texture
->resource
.type
));
3082 return WINED3DERR_INVALIDCALL
;
3085 surface
= sub_resource
->u
.surface
;
3087 if (!(texture
->flags
& (WINED3D_TEXTURE_GET_DC_LENIENT
| WINED3D_TEXTURE_DC_IN_USE
)))
3088 return WINED3DERR_INVALIDCALL
;
3090 if (surface
->dc
!= dc
)
3092 WARN("Application tries to release invalid DC %p, surface DC is %p.\n", dc
, surface
->dc
);
3093 return WINED3DERR_INVALIDCALL
;
3096 if (!(texture
->resource
.usage
& WINED3DUSAGE_OWNDC
) && !(device
->wined3d
->flags
& WINED3D_NO3D
))
3098 wined3d_cs_destroy_object(device
->cs
, texture2d_destroy_dc
, surface
);
3099 device
->cs
->ops
->finish(device
->cs
, WINED3D_CS_QUEUE_DEFAULT
);
3102 --sub_resource
->map_count
;
3103 if (!--texture
->resource
.map_count
&& texture
->update_map_binding
)
3104 wined3d_texture_update_map_binding(texture
);
3105 if (!(texture
->flags
& WINED3D_TEXTURE_GET_DC_LENIENT
))
3106 texture
->flags
&= ~WINED3D_TEXTURE_DC_IN_USE
;