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
.access
& WINED3D_RESOURCE_ACCESS_CPU
)
36 && texture
->resource
.usage
& WINED3DUSAGE_DYNAMIC
37 && gl_info
->supported
[ARB_PIXEL_BUFFER_OBJECT
]
38 && !texture
->resource
.format
->conv_byte_count
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 BOOL
is_power_of_two(UINT x
)
106 return (x
!= 0) && !(x
& (x
- 1));
109 static void wined3d_texture_evict_sysmem(struct wined3d_texture
*texture
)
111 struct wined3d_texture_sub_resource
*sub_resource
;
112 unsigned int i
, sub_count
;
114 if (texture
->flags
& (WINED3D_TEXTURE_CONVERTED
| WINED3D_TEXTURE_PIN_SYSMEM
)
115 || texture
->download_count
> WINED3D_TEXTURE_DYNAMIC_MAP_THRESHOLD
)
117 TRACE("Not evicting system memory for texture %p.\n", texture
);
121 TRACE("Evicting system memory for texture %p.\n", texture
);
123 sub_count
= texture
->level_count
* texture
->layer_count
;
124 for (i
= 0; i
< sub_count
; ++i
)
126 sub_resource
= &texture
->sub_resources
[i
];
127 if (sub_resource
->locations
== WINED3D_LOCATION_SYSMEM
)
128 ERR("WINED3D_LOCATION_SYSMEM is the only location for sub-resource %u of texture %p.\n",
130 sub_resource
->locations
&= ~WINED3D_LOCATION_SYSMEM
;
132 wined3d_resource_free_sysmem(&texture
->resource
);
135 void wined3d_texture_validate_location(struct wined3d_texture
*texture
,
136 unsigned int sub_resource_idx
, DWORD location
)
138 struct wined3d_texture_sub_resource
*sub_resource
;
139 DWORD previous_locations
;
141 TRACE("texture %p, sub_resource_idx %u, location %s.\n",
142 texture
, sub_resource_idx
, wined3d_debug_location(location
));
144 sub_resource
= &texture
->sub_resources
[sub_resource_idx
];
145 previous_locations
= sub_resource
->locations
;
146 sub_resource
->locations
|= location
;
147 if (previous_locations
== WINED3D_LOCATION_SYSMEM
&& location
!= WINED3D_LOCATION_SYSMEM
148 && !--texture
->sysmem_count
)
149 wined3d_texture_evict_sysmem(texture
);
151 TRACE("New locations flags are %s.\n", wined3d_debug_location(sub_resource
->locations
));
154 static void wined3d_texture_set_dirty(struct wined3d_texture
*texture
)
156 texture
->flags
&= ~(WINED3D_TEXTURE_RGB_VALID
| WINED3D_TEXTURE_SRGB_VALID
);
159 void wined3d_texture_invalidate_location(struct wined3d_texture
*texture
,
160 unsigned int sub_resource_idx
, DWORD location
)
162 struct wined3d_texture_sub_resource
*sub_resource
;
163 DWORD previous_locations
;
165 TRACE("texture %p, sub_resource_idx %u, location %s.\n",
166 texture
, sub_resource_idx
, wined3d_debug_location(location
));
168 if (location
& (WINED3D_LOCATION_TEXTURE_RGB
| WINED3D_LOCATION_TEXTURE_SRGB
))
169 wined3d_texture_set_dirty(texture
);
171 sub_resource
= &texture
->sub_resources
[sub_resource_idx
];
172 previous_locations
= sub_resource
->locations
;
173 sub_resource
->locations
&= ~location
;
174 if (previous_locations
!= WINED3D_LOCATION_SYSMEM
&& sub_resource
->locations
== WINED3D_LOCATION_SYSMEM
)
175 ++texture
->sysmem_count
;
177 TRACE("New locations flags are %s.\n", wined3d_debug_location(sub_resource
->locations
));
179 if (!sub_resource
->locations
)
180 ERR("Sub-resource %u of texture %p does not have any up to date location.\n",
181 sub_resource_idx
, texture
);
184 static BOOL
wined3d_texture_copy_sysmem_location(struct wined3d_texture
*texture
,
185 unsigned int sub_resource_idx
, struct wined3d_context
*context
, DWORD location
)
187 unsigned int size
= texture
->sub_resources
[sub_resource_idx
].size
;
188 struct wined3d_device
*device
= texture
->resource
.device
;
189 const struct wined3d_gl_info
*gl_info
;
190 struct wined3d_bo_address dst
, src
;
192 if (!wined3d_texture_prepare_location(texture
, sub_resource_idx
, context
, location
))
195 wined3d_texture_get_memory(texture
, sub_resource_idx
, &dst
, location
);
196 wined3d_texture_get_memory(texture
, sub_resource_idx
, &src
,
197 texture
->sub_resources
[sub_resource_idx
].locations
);
199 if (dst
.buffer_object
)
201 context
= context_acquire(device
, NULL
, 0);
202 gl_info
= context
->gl_info
;
203 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER
, dst
.buffer_object
));
204 GL_EXTCALL(glBufferSubData(GL_PIXEL_UNPACK_BUFFER
, 0, size
, src
.addr
));
205 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER
, 0));
206 checkGLcall("PBO upload");
207 context_release(context
);
211 if (src
.buffer_object
)
213 context
= context_acquire(device
, NULL
, 0);
214 gl_info
= context
->gl_info
;
215 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER
, src
.buffer_object
));
216 GL_EXTCALL(glGetBufferSubData(GL_PIXEL_PACK_BUFFER
, 0, size
, dst
.addr
));
217 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER
, 0));
218 checkGLcall("PBO download");
219 context_release(context
);
223 memcpy(dst
.addr
, src
.addr
, size
);
227 /* Context activation is done by the caller. Context may be NULL in
228 * WINED3D_NO3D mode. */
229 BOOL
wined3d_texture_load_location(struct wined3d_texture
*texture
,
230 unsigned int sub_resource_idx
, struct wined3d_context
*context
, DWORD location
)
232 static const DWORD sysmem_locations
= WINED3D_LOCATION_SYSMEM
| WINED3D_LOCATION_USER_MEMORY
233 | WINED3D_LOCATION_BUFFER
;
234 DWORD current
= texture
->sub_resources
[sub_resource_idx
].locations
;
237 TRACE("texture %p, sub_resource_idx %u, context %p, location %s.\n",
238 texture
, sub_resource_idx
, context
, wined3d_debug_location(location
));
240 TRACE("Current resource location %s.\n", wined3d_debug_location(current
));
242 if (current
& location
)
244 TRACE("Location %s is already up to date.\n", wined3d_debug_location(location
));
250 DWORD required_access
= wined3d_resource_access_from_location(location
);
251 if ((texture
->resource
.access
& required_access
) != required_access
)
252 WARN("Operation requires %#x access, but texture only has %#x.\n",
253 required_access
, texture
->resource
.access
);
256 if (current
& WINED3D_LOCATION_DISCARDED
)
258 TRACE("Sub-resource previously discarded, nothing to do.\n");
259 if (!wined3d_texture_prepare_location(texture
, sub_resource_idx
, context
, location
))
261 wined3d_texture_validate_location(texture
, sub_resource_idx
, location
);
262 wined3d_texture_invalidate_location(texture
, sub_resource_idx
, WINED3D_LOCATION_DISCARDED
);
268 ERR("Sub-resource %u of texture %p does not have any up to date location.\n",
269 sub_resource_idx
, texture
);
270 wined3d_texture_validate_location(texture
, sub_resource_idx
, WINED3D_LOCATION_DISCARDED
);
271 return wined3d_texture_load_location(texture
, sub_resource_idx
, context
, location
);
274 if ((location
& sysmem_locations
) && (current
& sysmem_locations
))
275 ret
= wined3d_texture_copy_sysmem_location(texture
, sub_resource_idx
, context
, location
);
277 ret
= texture
->texture_ops
->texture_load_location(texture
, sub_resource_idx
, context
, location
);
280 wined3d_texture_validate_location(texture
, sub_resource_idx
, location
);
285 void wined3d_texture_get_memory(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
,
286 struct wined3d_bo_address
*data
, DWORD locations
)
288 struct wined3d_texture_sub_resource
*sub_resource
;
290 TRACE("texture %p, sub_resource_idx %u, data %p, locations %s.\n",
291 texture
, sub_resource_idx
, data
, wined3d_debug_location(locations
));
293 sub_resource
= &texture
->sub_resources
[sub_resource_idx
];
294 if (locations
& WINED3D_LOCATION_BUFFER
)
297 #if !defined(STAGING_CSMT)
298 data
->buffer_object
= sub_resource
->buffer_object
;
299 #else /* STAGING_CSMT */
300 data
->buffer_object
= sub_resource
->buffer
->name
;
301 #endif /* STAGING_CSMT */
304 if (locations
& WINED3D_LOCATION_USER_MEMORY
)
306 data
->addr
= texture
->user_memory
;
307 data
->buffer_object
= 0;
310 if (locations
& WINED3D_LOCATION_SYSMEM
)
312 data
->addr
= texture
->resource
.heap_memory
;
313 data
->addr
+= sub_resource
->offset
;
314 data
->buffer_object
= 0;
318 ERR("Unexpected locations %s.\n", wined3d_debug_location(locations
));
320 data
->buffer_object
= 0;
323 static HRESULT
wined3d_texture_init(struct wined3d_texture
*texture
, const struct wined3d_texture_ops
*texture_ops
,
324 UINT layer_count
, UINT level_count
, const struct wined3d_resource_desc
*desc
, DWORD flags
,
325 struct wined3d_device
*device
, void *parent
, const struct wined3d_parent_ops
*parent_ops
,
326 const struct wined3d_resource_ops
*resource_ops
)
328 unsigned int i
, j
, size
, offset
= 0;
329 const struct wined3d_format
*format
;
332 TRACE("texture %p, texture_ops %p, layer_count %u, level_count %u, resource_type %s, format %s, "
333 "multisample_type %#x, multisample_quality %#x, usage %s, access %s, width %u, height %u, depth %u, "
334 "flags %#x, device %p, parent %p, parent_ops %p, resource_ops %p.\n",
335 texture
, texture_ops
, layer_count
, level_count
, debug_d3dresourcetype(desc
->resource_type
),
336 debug_d3dformat(desc
->format
), desc
->multisample_type
, desc
->multisample_quality
,
337 debug_d3dusage(desc
->usage
), wined3d_debug_resource_access(desc
->access
),
338 desc
->width
, desc
->height
, desc
->depth
, flags
, device
, parent
, parent_ops
, resource_ops
);
340 if (!desc
->width
|| !desc
->height
|| !desc
->depth
)
341 return WINED3DERR_INVALIDCALL
;
343 format
= wined3d_get_format(&device
->adapter
->gl_info
, desc
->format
, desc
->usage
);
345 for (i
= 0; i
< layer_count
; ++i
)
347 for (j
= 0; j
< level_count
; ++j
)
349 unsigned int idx
= i
* level_count
+ j
;
351 size
= wined3d_format_calculate_size(format
, device
->surface_alignment
,
352 max(1, desc
->width
>> j
), max(1, desc
->height
>> j
), max(1, desc
->depth
>> j
));
353 texture
->sub_resources
[idx
].offset
= offset
;
354 texture
->sub_resources
[idx
].size
= size
;
357 offset
= (offset
+ (RESOURCE_ALIGNMENT
- 1)) & ~(RESOURCE_ALIGNMENT
- 1);
361 return WINED3DERR_INVALIDCALL
;
363 if (FAILED(hr
= resource_init(&texture
->resource
, device
, desc
->resource_type
, format
,
364 desc
->multisample_type
, desc
->multisample_quality
, desc
->usage
, desc
->access
,
365 desc
->width
, desc
->height
, desc
->depth
, offset
, parent
, parent_ops
, resource_ops
)))
367 static unsigned int once
;
369 /* DXTn 3D textures are not supported. Do not write the ERR for them. */
370 if ((desc
->format
== WINED3DFMT_DXT1
|| desc
->format
== WINED3DFMT_DXT2
|| desc
->format
== WINED3DFMT_DXT3
371 || desc
->format
== WINED3DFMT_DXT4
|| desc
->format
== WINED3DFMT_DXT5
)
372 && !(format
->flags
[WINED3D_GL_RES_TYPE_TEX_2D
] & WINED3DFMT_FLAG_TEXTURE
)
373 && desc
->resource_type
!= WINED3D_RTYPE_TEXTURE_3D
&& !once
++)
374 ERR_(winediag
)("The application tried to create a DXTn texture, but the driver does not support them.\n");
376 WARN("Failed to initialize resource, returning %#x\n", hr
);
379 wined3d_resource_update_draw_binding(&texture
->resource
);
380 if ((flags
& WINED3D_TEXTURE_CREATE_MAPPABLE
) || desc
->format
== WINED3DFMT_D16_LOCKABLE
)
381 texture
->resource
.access
|= WINED3D_RESOURCE_ACCESS_MAP_R
| WINED3D_RESOURCE_ACCESS_MAP_W
;
383 texture
->texture_ops
= texture_ops
;
385 texture
->layer_count
= layer_count
;
386 texture
->level_count
= level_count
;
388 texture
->flags
|= WINED3D_TEXTURE_POW2_MAT_IDENT
| WINED3D_TEXTURE_NORMALIZED_COORDS
;
389 if (flags
& WINED3D_TEXTURE_CREATE_GET_DC_LENIENT
)
390 texture
->flags
|= WINED3D_TEXTURE_PIN_SYSMEM
| WINED3D_TEXTURE_GET_DC_LENIENT
;
391 if (flags
& (WINED3D_TEXTURE_CREATE_GET_DC
| WINED3D_TEXTURE_CREATE_GET_DC_LENIENT
))
392 texture
->flags
|= WINED3D_TEXTURE_GET_DC
;
393 if (flags
& WINED3D_TEXTURE_CREATE_DISCARD
)
394 texture
->flags
|= WINED3D_TEXTURE_DISCARD
;
395 if (flags
& WINED3D_TEXTURE_CREATE_GENERATE_MIPMAPS
)
397 if (!(texture
->resource
.format_flags
& WINED3DFMT_FLAG_GEN_MIPMAP
))
398 WARN("Format doesn't support mipmaps generation, "
399 "ignoring WINED3D_TEXTURE_CREATE_GENERATE_MIPMAPS flag.\n");
401 texture
->flags
|= WINED3D_TEXTURE_GENERATE_MIPMAPS
;
407 /* Context activation is done by the caller. */
408 static void wined3d_texture_remove_buffer_object(struct wined3d_texture
*texture
,
409 #if !defined(STAGING_CSMT)
410 unsigned int sub_resource_idx
, const struct wined3d_gl_info
*gl_info
)
412 GLuint
*buffer_object
= &texture
->sub_resources
[sub_resource_idx
].buffer_object
;
414 GL_EXTCALL(glDeleteBuffers(1, buffer_object
));
415 checkGLcall("glDeleteBuffers");
417 TRACE("Deleted buffer object %u for texture %p, sub-resource %u.\n",
418 *buffer_object
, texture
, sub_resource_idx
);
420 wined3d_texture_invalidate_location(texture
, sub_resource_idx
, WINED3D_LOCATION_BUFFER
);
422 #else /* STAGING_CSMT */
423 unsigned int sub_resource_idx
, struct wined3d_context
*context
)
425 struct wined3d_gl_bo
*buffer
= texture
->sub_resources
[sub_resource_idx
].buffer
;
426 GLuint name
= buffer
->name
;
428 wined3d_device_release_bo(texture
->resource
.device
, buffer
, context
);
429 texture
->sub_resources
[sub_resource_idx
].buffer
= NULL
;
430 wined3d_texture_invalidate_location(texture
, sub_resource_idx
, WINED3D_LOCATION_BUFFER
);
432 TRACE("Deleted buffer object %u for texture %p, sub-resource %u.\n",
433 name
, texture
, sub_resource_idx
);
434 #endif /* STAGING_CSMT */
437 static void wined3d_texture_update_map_binding(struct wined3d_texture
*texture
)
439 unsigned int sub_count
= texture
->level_count
* texture
->layer_count
;
440 const struct wined3d_device
*device
= texture
->resource
.device
;
441 DWORD map_binding
= texture
->update_map_binding
;
442 struct wined3d_context
*context
= NULL
;
445 if (device
->d3d_initialized
)
446 context
= context_acquire(device
, NULL
, 0);
448 for (i
= 0; i
< sub_count
; ++i
)
450 if (texture
->sub_resources
[i
].locations
== texture
->resource
.map_binding
451 && !wined3d_texture_load_location(texture
, i
, context
, map_binding
))
452 ERR("Failed to load location %s.\n", wined3d_debug_location(map_binding
));
453 if (texture
->resource
.map_binding
== WINED3D_LOCATION_BUFFER
)
454 #if !defined(STAGING_CSMT)
455 wined3d_texture_remove_buffer_object(texture
, i
, context
->gl_info
);
456 #else /* STAGING_CSMT */
457 wined3d_texture_remove_buffer_object(texture
, i
, context
);
458 #endif /* STAGING_CSMT */
462 context_release(context
);
464 texture
->resource
.map_binding
= map_binding
;
465 texture
->update_map_binding
= 0;
468 void wined3d_texture_set_map_binding(struct wined3d_texture
*texture
, DWORD map_binding
)
470 texture
->update_map_binding
= map_binding
;
471 if (!texture
->resource
.map_count
)
472 wined3d_texture_update_map_binding(texture
);
475 /* A GL context is provided by the caller */
476 static void gltexture_delete(struct wined3d_device
*device
, const struct wined3d_gl_info
*gl_info
,
477 struct gl_texture
*tex
)
479 context_gl_resource_released(device
, tex
->name
, FALSE
);
480 gl_info
->gl_ops
.gl
.p_glDeleteTextures(1, &tex
->name
);
484 static unsigned int wined3d_texture_get_gl_sample_count(const struct wined3d_texture
*texture
)
486 const struct wined3d_format
*format
= texture
->resource
.format
;
488 /* TODO: NVIDIA expose their Coverage Sample Anti-Aliasing (CSAA)
489 * feature through type == MULTISAMPLE_XX and quality != 0. This could
490 * be mapped to GL_NV_framebuffer_multisample_coverage.
492 * AMD have a similar feature called Enhanced Quality Anti-Aliasing
493 * (EQAA), but it does not have an equivalent OpenGL extension. */
495 /* We advertise as many WINED3D_MULTISAMPLE_NON_MASKABLE quality
496 * levels as the count of advertised multisample types for the texture
498 if (texture
->resource
.multisample_type
== WINED3D_MULTISAMPLE_NON_MASKABLE
)
500 unsigned int i
, count
= 0;
502 for (i
= 0; i
< sizeof(format
->multisample_types
) * CHAR_BIT
; ++i
)
504 if (format
->multisample_types
& 1u << i
)
506 if (texture
->resource
.multisample_quality
== count
++)
513 return texture
->resource
.multisample_type
;
516 /* Context activation is done by the caller. */
517 /* The caller is responsible for binding the correct texture. */
518 static void wined3d_texture_allocate_gl_mutable_storage(struct wined3d_texture
*texture
,
519 GLenum gl_internal_format
, const struct wined3d_format
*format
,
520 const struct wined3d_gl_info
*gl_info
)
522 unsigned int level
, level_count
, layer
, layer_count
;
523 GLsizei width
, height
;
526 level_count
= texture
->level_count
;
527 layer_count
= texture
->target
== GL_TEXTURE_2D_ARRAY
? 1 : texture
->layer_count
;
529 for (layer
= 0; layer
< layer_count
; ++layer
)
531 target
= wined3d_texture_get_sub_resource_target(texture
, layer
* level_count
);
533 for (level
= 0; level
< level_count
; ++level
)
535 width
= wined3d_texture_get_level_pow2_width(texture
, level
);
536 height
= wined3d_texture_get_level_pow2_height(texture
, level
);
537 if (texture
->resource
.format_flags
& WINED3DFMT_FLAG_HEIGHT_SCALE
)
539 height
*= format
->height_scale
.numerator
;
540 height
/= format
->height_scale
.denominator
;
543 TRACE("texture %p, layer %u, level %u, target %#x, width %u, height %u.\n",
544 texture
, layer
, level
, target
, width
, height
);
546 if (texture
->target
== GL_TEXTURE_2D_ARRAY
)
548 GL_EXTCALL(glTexImage3D(target
, level
, gl_internal_format
, width
, height
,
549 texture
->layer_count
, 0, format
->glFormat
, format
->glType
, NULL
));
550 checkGLcall("glTexImage3D");
554 gl_info
->gl_ops
.gl
.p_glTexImage2D(target
, level
, gl_internal_format
,
555 width
, height
, 0, format
->glFormat
, format
->glType
, NULL
);
556 checkGLcall("glTexImage2D");
562 /* Context activation is done by the caller. */
563 /* The caller is responsible for binding the correct texture. */
564 static void wined3d_texture_allocate_gl_immutable_storage(struct wined3d_texture
*texture
,
565 GLenum gl_internal_format
, const struct wined3d_gl_info
*gl_info
)
567 unsigned int samples
= wined3d_texture_get_gl_sample_count(texture
);
568 GLsizei height
= wined3d_texture_get_level_pow2_height(texture
, 0);
569 GLsizei width
= wined3d_texture_get_level_pow2_width(texture
, 0);
571 switch (texture
->target
)
573 case GL_TEXTURE_2D_ARRAY
:
574 GL_EXTCALL(glTexStorage3D(texture
->target
, texture
->level_count
,
575 gl_internal_format
, width
, height
, texture
->layer_count
));
577 case GL_TEXTURE_2D_MULTISAMPLE
:
578 GL_EXTCALL(glTexStorage2DMultisample(texture
->target
, samples
,
579 gl_internal_format
, width
, height
, GL_FALSE
));
581 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
582 GL_EXTCALL(glTexStorage3DMultisample(texture
->target
, samples
,
583 gl_internal_format
, width
, height
, texture
->layer_count
, GL_FALSE
));
586 GL_EXTCALL(glTexStorage2D(texture
->target
, texture
->level_count
,
587 gl_internal_format
, width
, height
));
591 checkGLcall("allocate immutable storage");
594 static void wined3d_texture_unload_gl_texture(struct wined3d_texture
*texture
)
596 struct wined3d_device
*device
= texture
->resource
.device
;
597 const struct wined3d_gl_info
*gl_info
= NULL
;
598 struct wined3d_context
*context
= NULL
;
600 if (texture
->texture_rgb
.name
|| texture
->texture_srgb
.name
601 || texture
->rb_multisample
|| texture
->rb_resolved
)
603 context
= context_acquire(device
, NULL
, 0);
604 gl_info
= context
->gl_info
;
607 if (texture
->texture_rgb
.name
)
608 gltexture_delete(device
, context
->gl_info
, &texture
->texture_rgb
);
610 if (texture
->texture_srgb
.name
)
611 gltexture_delete(device
, context
->gl_info
, &texture
->texture_srgb
);
613 if (texture
->rb_multisample
)
615 TRACE("Deleting multisample renderbuffer %u.\n", texture
->rb_multisample
);
616 context_gl_resource_released(device
, texture
->rb_multisample
, TRUE
);
617 gl_info
->fbo_ops
.glDeleteRenderbuffers(1, &texture
->rb_multisample
);
618 texture
->rb_multisample
= 0;
621 if (texture
->rb_resolved
)
623 TRACE("Deleting resolved renderbuffer %u.\n", texture
->rb_resolved
);
624 context_gl_resource_released(device
, texture
->rb_resolved
, TRUE
);
625 gl_info
->fbo_ops
.glDeleteRenderbuffers(1, &texture
->rb_resolved
);
626 texture
->rb_resolved
= 0;
629 if (context
) context_release(context
);
631 wined3d_texture_set_dirty(texture
);
633 resource_unload(&texture
->resource
);
636 static void wined3d_texture_sub_resources_destroyed(struct wined3d_texture
*texture
)
638 unsigned int sub_count
= texture
->level_count
* texture
->layer_count
;
639 struct wined3d_texture_sub_resource
*sub_resource
;
642 for (i
= 0; i
< sub_count
; ++i
)
644 sub_resource
= &texture
->sub_resources
[i
];
645 if (sub_resource
->parent
)
647 TRACE("sub-resource %u.\n", i
);
648 sub_resource
->parent_ops
->wined3d_object_destroyed(sub_resource
->parent
);
649 sub_resource
->parent
= NULL
;
654 static void wined3d_texture_cleanup(struct wined3d_texture
*texture
)
656 unsigned int sub_count
= texture
->level_count
* texture
->layer_count
;
657 struct wined3d_device
*device
= texture
->resource
.device
;
658 struct wined3d_context
*context
= NULL
;
659 #if !defined(STAGING_CSMT)
660 const struct wined3d_gl_info
*gl_info
;
661 GLuint buffer_object
;
662 #else /* STAGING_CSMT */
663 struct wined3d_gl_bo
*buffer
;
664 #endif /* STAGING_CSMT */
667 TRACE("texture %p.\n", texture
);
669 for (i
= 0; i
< sub_count
; ++i
)
671 #if !defined(STAGING_CSMT)
672 if (!(buffer_object
= texture
->sub_resources
[i
].buffer_object
))
675 TRACE("Deleting buffer object %u.\n", buffer_object
);
676 #else /* STAGING_CSMT */
677 if (!(buffer
= texture
->sub_resources
[i
].buffer
))
680 TRACE("Deleting buffer object %u.\n", buffer
->name
);
681 #endif /* STAGING_CSMT */
683 /* We may not be able to get a context in wined3d_texture_cleanup() in
684 * general, but if a buffer object was previously created we can. */
686 #if !defined(STAGING_CSMT)
688 context
= context_acquire(device
, NULL
, 0);
689 gl_info
= context
->gl_info
;
692 GL_EXTCALL(glDeleteBuffers(1, &buffer_object
));
693 #else /* STAGING_CSMT */
694 context
= context_acquire(device
, NULL
, 0);
696 wined3d_device_release_bo(device
, buffer
, context
);
697 texture
->sub_resources
[i
].buffer
= NULL
;
698 #endif /* STAGING_CSMT */
701 context_release(context
);
703 texture
->texture_ops
->texture_cleanup_sub_resources(texture
);
704 if (texture
->overlay_info
)
706 for (i
= 0; i
< sub_count
; ++i
)
708 struct wined3d_overlay_info
*info
= &texture
->overlay_info
[i
];
709 struct wined3d_overlay_info
*overlay
, *cur
;
711 list_remove(&info
->entry
);
712 LIST_FOR_EACH_ENTRY_SAFE(overlay
, cur
, &info
->overlays
, struct wined3d_overlay_info
, entry
)
714 list_remove(&overlay
->entry
);
717 heap_free(texture
->overlay_info
);
719 wined3d_texture_unload_gl_texture(texture
);
722 void wined3d_texture_set_swapchain(struct wined3d_texture
*texture
, struct wined3d_swapchain
*swapchain
)
724 texture
->swapchain
= swapchain
;
725 wined3d_resource_update_draw_binding(&texture
->resource
);
728 /* Context activation is done by the caller. */
729 void wined3d_texture_bind(struct wined3d_texture
*texture
,
730 struct wined3d_context
*context
, BOOL srgb
)
732 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
733 const struct wined3d_format
*format
= texture
->resource
.format
;
734 const struct color_fixup_desc fixup
= format
->color_fixup
;
735 struct gl_texture
*gl_tex
;
738 TRACE("texture %p, context %p, srgb %#x.\n", texture
, context
, srgb
);
740 if (!needs_separate_srgb_gl_texture(context
, texture
))
743 /* sRGB mode cache for preload() calls outside drawprim. */
745 texture
->flags
|= WINED3D_TEXTURE_IS_SRGB
;
747 texture
->flags
&= ~WINED3D_TEXTURE_IS_SRGB
;
749 gl_tex
= wined3d_texture_get_gl_texture(texture
, srgb
);
750 target
= texture
->target
;
754 context_bind_texture(context
, target
, gl_tex
->name
);
758 gl_info
->gl_ops
.gl
.p_glGenTextures(1, &gl_tex
->name
);
759 checkGLcall("glGenTextures");
760 TRACE("Generated texture %d.\n", gl_tex
->name
);
764 ERR("Failed to generate a texture name.\n");
768 /* Initialise the state of the texture object to the OpenGL defaults, not
769 * the wined3d defaults. */
770 gl_tex
->sampler_desc
.address_u
= WINED3D_TADDRESS_WRAP
;
771 gl_tex
->sampler_desc
.address_v
= WINED3D_TADDRESS_WRAP
;
772 gl_tex
->sampler_desc
.address_w
= WINED3D_TADDRESS_WRAP
;
773 memset(gl_tex
->sampler_desc
.border_color
, 0, sizeof(gl_tex
->sampler_desc
.border_color
));
774 gl_tex
->sampler_desc
.mag_filter
= WINED3D_TEXF_LINEAR
;
775 gl_tex
->sampler_desc
.min_filter
= WINED3D_TEXF_POINT
; /* GL_NEAREST_MIPMAP_LINEAR */
776 gl_tex
->sampler_desc
.mip_filter
= WINED3D_TEXF_LINEAR
; /* GL_NEAREST_MIPMAP_LINEAR */
777 gl_tex
->sampler_desc
.lod_bias
= 0.0f
;
778 gl_tex
->sampler_desc
.min_lod
= -1000.0f
;
779 gl_tex
->sampler_desc
.max_lod
= 1000.0f
;
780 gl_tex
->sampler_desc
.max_anisotropy
= 1;
781 gl_tex
->sampler_desc
.compare
= FALSE
;
782 gl_tex
->sampler_desc
.comparison_func
= WINED3D_CMP_LESSEQUAL
;
783 if (context
->gl_info
->supported
[EXT_TEXTURE_SRGB_DECODE
])
784 gl_tex
->sampler_desc
.srgb_decode
= TRUE
;
786 gl_tex
->sampler_desc
.srgb_decode
= srgb
;
787 gl_tex
->base_level
= 0;
788 wined3d_texture_set_dirty(texture
);
790 context_bind_texture(context
, target
, gl_tex
->name
);
792 /* For a new texture we have to set the texture levels after binding the
793 * texture. Beware that texture rectangles do not support mipmapping, but
794 * set the maxmiplevel if we're relying on the partial
795 * GL_ARB_texture_non_power_of_two emulation with texture rectangles.
796 * (I.e., do not care about cond_np2 here, just look for
797 * GL_TEXTURE_RECTANGLE_ARB.) */
798 if (target
!= GL_TEXTURE_RECTANGLE_ARB
)
800 TRACE("Setting GL_TEXTURE_MAX_LEVEL to %u.\n", texture
->level_count
- 1);
801 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, texture
->level_count
- 1);
802 checkGLcall("glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, texture->level_count)");
805 if (target
== GL_TEXTURE_CUBE_MAP_ARB
)
807 /* Cubemaps are always set to clamp, regardless of the sampler state. */
808 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
809 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
810 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_R
, GL_CLAMP_TO_EDGE
);
813 if (texture
->flags
& WINED3D_TEXTURE_COND_NP2
)
815 /* Conditinal non power of two textures use a different clamping
816 * default. If we're using the GL_WINE_normalized_texrect partial
817 * driver emulation, we're dealing with a GL_TEXTURE_2D texture which
818 * has the address mode set to repeat - something that prevents us
819 * from hitting the accelerated codepath. Thus manually set the GL
820 * state. The same applies to filtering. Even if the texture has only
821 * one mip level, the default LINEAR_MIPMAP_LINEAR filter causes a SW
822 * fallback on macos. */
823 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
824 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
825 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
826 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
827 checkGLcall("glTexParameteri");
828 gl_tex
->sampler_desc
.address_u
= WINED3D_TADDRESS_CLAMP
;
829 gl_tex
->sampler_desc
.address_v
= WINED3D_TADDRESS_CLAMP
;
830 gl_tex
->sampler_desc
.mag_filter
= WINED3D_TEXF_POINT
;
831 gl_tex
->sampler_desc
.min_filter
= WINED3D_TEXF_POINT
;
832 gl_tex
->sampler_desc
.mip_filter
= WINED3D_TEXF_NONE
;
835 if (gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
] && gl_info
->supported
[ARB_DEPTH_TEXTURE
])
837 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_DEPTH_TEXTURE_MODE_ARB
, GL_INTENSITY
);
838 checkGLcall("glTexParameteri(GL_DEPTH_TEXTURE_MODE_ARB, GL_INTENSITY)");
841 if (!is_identity_fixup(fixup
) && can_use_texture_swizzle(gl_info
, format
))
843 static const GLenum swizzle_source
[] =
845 GL_ZERO
, /* CHANNEL_SOURCE_ZERO */
846 GL_ONE
, /* CHANNEL_SOURCE_ONE */
847 GL_RED
, /* CHANNEL_SOURCE_X */
848 GL_GREEN
, /* CHANNEL_SOURCE_Y */
849 GL_BLUE
, /* CHANNEL_SOURCE_Z */
850 GL_ALPHA
, /* CHANNEL_SOURCE_W */
858 swizzle
.x
= swizzle_source
[fixup
.x_source
];
859 swizzle
.y
= swizzle_source
[fixup
.y_source
];
860 swizzle
.z
= swizzle_source
[fixup
.z_source
];
861 swizzle
.w
= swizzle_source
[fixup
.w_source
];
862 gl_info
->gl_ops
.gl
.p_glTexParameteriv(target
, GL_TEXTURE_SWIZZLE_RGBA
, &swizzle
.x
);
863 checkGLcall("glTexParameteriv(GL_TEXTURE_SWIZZLE_RGBA)");
867 /* Context activation is done by the caller. */
868 void wined3d_texture_bind_and_dirtify(struct wined3d_texture
*texture
,
869 struct wined3d_context
*context
, BOOL srgb
)
871 /* We don't need a specific texture unit, but after binding the texture
872 * the current unit is dirty. Read the unit back instead of switching to
873 * 0, this avoids messing around with the state manager's GL states. The
874 * current texture unit should always be a valid one.
876 * To be more specific, this is tricky because we can implicitly be
877 * called from sampler() in state.c. This means we can't touch anything
878 * other than whatever happens to be the currently active texture, or we
879 * would risk marking already applied sampler states dirty again. */
880 if (context
->active_texture
< ARRAY_SIZE(context
->rev_tex_unit_map
))
882 DWORD active_sampler
= context
->rev_tex_unit_map
[context
->active_texture
];
883 if (active_sampler
!= WINED3D_UNMAPPED_STAGE
)
884 context_invalidate_state(context
, STATE_SAMPLER(active_sampler
));
886 /* FIXME: Ideally we'd only do this when touching a binding that's used by
888 context_invalidate_compute_state(context
, STATE_COMPUTE_SHADER_RESOURCE_BINDING
);
889 context_invalidate_state(context
, STATE_GRAPHICS_SHADER_RESOURCE_BINDING
);
891 wined3d_texture_bind(texture
, context
, srgb
);
894 /* Context activation is done by the caller (state handler). */
895 /* This function relies on the correct texture being bound and loaded. */
896 void wined3d_texture_apply_sampler_desc(struct wined3d_texture
*texture
,
897 const struct wined3d_sampler_desc
*sampler_desc
, const struct wined3d_context
*context
)
899 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
900 GLenum target
= texture
->target
;
901 struct gl_texture
*gl_tex
;
904 TRACE("texture %p, sampler_desc %p, context %p.\n", texture
, sampler_desc
, context
);
906 gl_tex
= wined3d_texture_get_gl_texture(texture
, texture
->flags
& WINED3D_TEXTURE_IS_SRGB
);
908 state
= sampler_desc
->address_u
;
909 if (state
!= gl_tex
->sampler_desc
.address_u
)
911 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_S
,
912 gl_info
->wrap_lookup
[state
- WINED3D_TADDRESS_WRAP
]);
913 gl_tex
->sampler_desc
.address_u
= state
;
916 state
= sampler_desc
->address_v
;
917 if (state
!= gl_tex
->sampler_desc
.address_v
)
919 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_T
,
920 gl_info
->wrap_lookup
[state
- WINED3D_TADDRESS_WRAP
]);
921 gl_tex
->sampler_desc
.address_v
= state
;
924 state
= sampler_desc
->address_w
;
925 if (state
!= gl_tex
->sampler_desc
.address_w
)
927 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_R
,
928 gl_info
->wrap_lookup
[state
- WINED3D_TADDRESS_WRAP
]);
929 gl_tex
->sampler_desc
.address_w
= state
;
932 if (memcmp(gl_tex
->sampler_desc
.border_color
, sampler_desc
->border_color
,
933 sizeof(gl_tex
->sampler_desc
.border_color
)))
935 gl_info
->gl_ops
.gl
.p_glTexParameterfv(target
, GL_TEXTURE_BORDER_COLOR
, &sampler_desc
->border_color
[0]);
936 memcpy(gl_tex
->sampler_desc
.border_color
, sampler_desc
->border_color
,
937 sizeof(gl_tex
->sampler_desc
.border_color
));
940 state
= sampler_desc
->mag_filter
;
941 if (state
!= gl_tex
->sampler_desc
.mag_filter
)
943 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MAG_FILTER
, wined3d_gl_mag_filter(state
));
944 gl_tex
->sampler_desc
.mag_filter
= state
;
947 if (sampler_desc
->min_filter
!= gl_tex
->sampler_desc
.min_filter
948 || sampler_desc
->mip_filter
!= gl_tex
->sampler_desc
.mip_filter
)
950 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MIN_FILTER
,
951 wined3d_gl_min_mip_filter(sampler_desc
->min_filter
, sampler_desc
->mip_filter
));
952 gl_tex
->sampler_desc
.min_filter
= sampler_desc
->min_filter
;
953 gl_tex
->sampler_desc
.mip_filter
= sampler_desc
->mip_filter
;
956 state
= sampler_desc
->max_anisotropy
;
957 if (state
!= gl_tex
->sampler_desc
.max_anisotropy
)
959 if (gl_info
->supported
[ARB_TEXTURE_FILTER_ANISOTROPIC
])
960 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MAX_ANISOTROPY
, state
);
962 WARN("Anisotropic filtering not supported.\n");
963 gl_tex
->sampler_desc
.max_anisotropy
= state
;
966 if (!sampler_desc
->srgb_decode
!= !gl_tex
->sampler_desc
.srgb_decode
967 && (context
->d3d_info
->wined3d_creation_flags
& WINED3D_SRGB_READ_WRITE_CONTROL
)
968 && gl_info
->supported
[EXT_TEXTURE_SRGB_DECODE
])
970 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_SRGB_DECODE_EXT
,
971 sampler_desc
->srgb_decode
? GL_DECODE_EXT
: GL_SKIP_DECODE_EXT
);
972 gl_tex
->sampler_desc
.srgb_decode
= sampler_desc
->srgb_decode
;
975 if (!sampler_desc
->compare
!= !gl_tex
->sampler_desc
.compare
)
977 if (sampler_desc
->compare
)
978 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_COMPARE_MODE_ARB
, GL_COMPARE_R_TO_TEXTURE_ARB
);
980 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_COMPARE_MODE_ARB
, GL_NONE
);
981 gl_tex
->sampler_desc
.compare
= sampler_desc
->compare
;
984 checkGLcall("Texture parameter application");
986 if (gl_info
->supported
[EXT_TEXTURE_LOD_BIAS
])
988 gl_info
->gl_ops
.gl
.p_glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT
,
989 GL_TEXTURE_LOD_BIAS_EXT
, sampler_desc
->lod_bias
);
990 checkGLcall("glTexEnvf(GL_TEXTURE_LOD_BIAS_EXT, ...)");
994 ULONG CDECL
wined3d_texture_incref(struct wined3d_texture
*texture
)
998 TRACE("texture %p, swapchain %p.\n", texture
, texture
->swapchain
);
1000 if (texture
->swapchain
)
1001 return wined3d_swapchain_incref(texture
->swapchain
);
1003 refcount
= InterlockedIncrement(&texture
->resource
.ref
);
1004 TRACE("%p increasing refcount to %u.\n", texture
, refcount
);
1009 static void wined3d_texture_cleanup_sync(struct wined3d_texture
*texture
)
1011 wined3d_texture_sub_resources_destroyed(texture
);
1012 resource_cleanup(&texture
->resource
);
1013 wined3d_resource_wait_idle(&texture
->resource
);
1014 wined3d_texture_cleanup(texture
);
1017 static void wined3d_texture_destroy_object(void *object
)
1019 wined3d_texture_cleanup(object
);
1023 ULONG CDECL
wined3d_texture_decref(struct wined3d_texture
*texture
)
1027 TRACE("texture %p, swapchain %p.\n", texture
, texture
->swapchain
);
1029 if (texture
->swapchain
)
1030 return wined3d_swapchain_decref(texture
->swapchain
);
1032 refcount
= InterlockedDecrement(&texture
->resource
.ref
);
1033 TRACE("%p decreasing refcount to %u.\n", texture
, refcount
);
1037 /* Wait for the texture to become idle if it's using user memory,
1038 * since the application is allowed to free that memory once the
1039 * texture is destroyed. Note that this implies that
1040 * wined3d_texture_destroy_object() can't access that memory either. */
1041 if (texture
->user_memory
)
1042 wined3d_resource_wait_idle(&texture
->resource
);
1043 wined3d_texture_sub_resources_destroyed(texture
);
1044 texture
->resource
.parent_ops
->wined3d_object_destroyed(texture
->resource
.parent
);
1045 resource_cleanup(&texture
->resource
);
1046 wined3d_cs_destroy_object(texture
->resource
.device
->cs
, wined3d_texture_destroy_object
, texture
);
1052 struct wined3d_resource
* CDECL
wined3d_texture_get_resource(struct wined3d_texture
*texture
)
1054 TRACE("texture %p.\n", texture
);
1056 return &texture
->resource
;
1059 static BOOL
color_key_equal(const struct wined3d_color_key
*c1
, struct wined3d_color_key
*c2
)
1061 return c1
->color_space_low_value
== c2
->color_space_low_value
1062 && c1
->color_space_high_value
== c2
->color_space_high_value
;
1065 /* Context activation is done by the caller */
1066 void wined3d_texture_load(struct wined3d_texture
*texture
,
1067 struct wined3d_context
*context
, BOOL srgb
)
1069 UINT sub_count
= texture
->level_count
* texture
->layer_count
;
1070 const struct wined3d_d3d_info
*d3d_info
= context
->d3d_info
;
1074 TRACE("texture %p, context %p, srgb %#x.\n", texture
, context
, srgb
);
1076 if (!needs_separate_srgb_gl_texture(context
, texture
))
1080 flag
= WINED3D_TEXTURE_SRGB_VALID
;
1082 flag
= WINED3D_TEXTURE_RGB_VALID
;
1084 if (!d3d_info
->shader_color_key
1085 && (!(texture
->async
.flags
& WINED3D_TEXTURE_ASYNC_COLOR_KEY
)
1086 != !(texture
->async
.color_key_flags
& WINED3D_CKEY_SRC_BLT
)
1087 || (texture
->async
.flags
& WINED3D_TEXTURE_ASYNC_COLOR_KEY
1088 && !color_key_equal(&texture
->async
.gl_color_key
, &texture
->async
.src_blt_color_key
))))
1090 unsigned int sub_count
= texture
->level_count
* texture
->layer_count
;
1093 TRACE("Reloading because of color key value change.\n");
1094 for (i
= 0; i
< sub_count
; i
++)
1096 if (!wined3d_texture_load_location(texture
, i
, context
, texture
->resource
.map_binding
))
1097 ERR("Failed to load location %s.\n", wined3d_debug_location(texture
->resource
.map_binding
));
1099 wined3d_texture_invalidate_location(texture
, i
, ~texture
->resource
.map_binding
);
1102 texture
->async
.gl_color_key
= texture
->async
.src_blt_color_key
;
1105 if (texture
->flags
& flag
)
1107 TRACE("Texture %p not dirty, nothing to do.\n", texture
);
1111 /* Reload the surfaces if the texture is marked dirty. */
1112 for (i
= 0; i
< sub_count
; ++i
)
1114 if (!wined3d_texture_load_location(texture
, i
, context
,
1115 srgb
? WINED3D_LOCATION_TEXTURE_SRGB
: WINED3D_LOCATION_TEXTURE_RGB
))
1116 ERR("Failed to load location (srgb %#x).\n", srgb
);
1118 texture
->flags
|= flag
;
1121 void * CDECL
wined3d_texture_get_parent(const struct wined3d_texture
*texture
)
1123 TRACE("texture %p.\n", texture
);
1125 return texture
->resource
.parent
;
1128 HRESULT
wined3d_texture_check_box_dimensions(const struct wined3d_texture
*texture
,
1129 unsigned int level
, const struct wined3d_box
*box
)
1131 const struct wined3d_format
*format
= texture
->resource
.format
;
1132 unsigned int width_mask
, height_mask
, width
, height
, depth
;
1134 width
= wined3d_texture_get_level_width(texture
, level
);
1135 height
= wined3d_texture_get_level_height(texture
, level
);
1136 depth
= wined3d_texture_get_level_depth(texture
, level
);
1138 if (box
->left
>= box
->right
|| box
->right
> width
1139 || box
->top
>= box
->bottom
|| box
->bottom
> height
1140 || box
->front
>= box
->back
|| box
->back
> depth
)
1142 WARN("Box %s is invalid.\n", debug_box(box
));
1143 return WINEDDERR_INVALIDRECT
;
1146 if (texture
->resource
.format_flags
& WINED3DFMT_FLAG_BLOCKS
)
1148 /* This assumes power of two block sizes, but NPOT block sizes would
1151 * This also assumes that the format's block depth is 1. */
1152 width_mask
= format
->block_width
- 1;
1153 height_mask
= format
->block_height
- 1;
1155 if ((box
->left
& width_mask
) || (box
->top
& height_mask
)
1156 || (box
->right
& width_mask
&& box
->right
!= width
)
1157 || (box
->bottom
& height_mask
&& box
->bottom
!= height
))
1159 WARN("Box %s is misaligned for %ux%u blocks.\n",
1160 debug_box(box
), format
->block_width
, format
->block_height
);
1161 return WINED3DERR_INVALIDCALL
;
1168 void CDECL
wined3d_texture_get_pitch(const struct wined3d_texture
*texture
,
1169 unsigned int level
, unsigned int *row_pitch
, unsigned int *slice_pitch
)
1171 const struct wined3d_resource
*resource
= &texture
->resource
;
1172 unsigned int width
= wined3d_texture_get_level_width(texture
, level
);
1173 unsigned int height
= wined3d_texture_get_level_height(texture
, level
);
1175 if (texture
->row_pitch
)
1177 *row_pitch
= texture
->row_pitch
;
1178 *slice_pitch
= texture
->slice_pitch
;
1182 wined3d_format_calculate_pitch(resource
->format
, resource
->device
->surface_alignment
,
1183 width
, height
, row_pitch
, slice_pitch
);
1186 DWORD CDECL
wined3d_texture_set_lod(struct wined3d_texture
*texture
, DWORD lod
)
1188 DWORD old
= texture
->lod
;
1190 TRACE("texture %p, lod %u.\n", texture
, lod
);
1192 /* The d3d9:texture test shows that SetLOD is ignored on non-managed
1193 * textures. The call always returns 0, and GetLOD always returns 0. */
1194 if (!wined3d_resource_access_is_managed(texture
->resource
.access
))
1196 TRACE("Ignoring LOD on texture with resource access %s.\n",
1197 wined3d_debug_resource_access(texture
->resource
.access
));
1201 if (lod
>= texture
->level_count
)
1202 lod
= texture
->level_count
- 1;
1204 if (texture
->lod
!= lod
)
1206 struct wined3d_device
*device
= texture
->resource
.device
;
1208 wined3d_resource_wait_idle(&texture
->resource
);
1211 texture
->texture_rgb
.base_level
= ~0u;
1212 texture
->texture_srgb
.base_level
= ~0u;
1213 if (texture
->resource
.bind_count
)
1214 wined3d_cs_emit_set_sampler_state(device
->cs
, texture
->sampler
, WINED3D_SAMP_MAX_MIP_LEVEL
,
1215 device
->state
.sampler_states
[texture
->sampler
][WINED3D_SAMP_MAX_MIP_LEVEL
]);
1221 DWORD CDECL
wined3d_texture_get_lod(const struct wined3d_texture
*texture
)
1223 TRACE("texture %p, returning %u.\n", texture
, texture
->lod
);
1225 return texture
->lod
;
1228 DWORD CDECL
wined3d_texture_get_level_count(const struct wined3d_texture
*texture
)
1230 TRACE("texture %p, returning %u.\n", texture
, texture
->level_count
);
1232 return texture
->level_count
;
1235 HRESULT CDECL
wined3d_texture_set_color_key(struct wined3d_texture
*texture
,
1236 DWORD flags
, const struct wined3d_color_key
*color_key
)
1238 struct wined3d_device
*device
= texture
->resource
.device
;
1239 static const DWORD all_flags
= WINED3D_CKEY_DST_BLT
| WINED3D_CKEY_DST_OVERLAY
1240 | WINED3D_CKEY_SRC_BLT
| WINED3D_CKEY_SRC_OVERLAY
;
1242 TRACE("texture %p, flags %#x, color_key %p.\n", texture
, flags
, color_key
);
1244 if (flags
& ~all_flags
)
1246 WARN("Invalid flags passed, returning WINED3DERR_INVALIDCALL.\n");
1247 return WINED3DERR_INVALIDCALL
;
1250 wined3d_cs_emit_set_color_key(device
->cs
, texture
, flags
, color_key
);
1255 static void texture2d_create_dc(void *object
)
1257 struct wined3d_surface
*surface
= object
;
1258 struct wined3d_context
*context
= NULL
;
1259 const struct wined3d_format
*format
;
1260 unsigned int row_pitch
, slice_pitch
;
1261 struct wined3d_texture
*texture
;
1262 struct wined3d_bo_address data
;
1263 D3DKMT_CREATEDCFROMMEMORY desc
;
1264 unsigned int sub_resource_idx
;
1265 struct wined3d_device
*device
;
1268 TRACE("surface %p.\n", surface
);
1270 texture
= surface
->container
;
1271 sub_resource_idx
= surface_get_sub_resource_idx(surface
);
1272 device
= texture
->resource
.device
;
1274 format
= texture
->resource
.format
;
1275 if (!format
->ddi_format
)
1277 WARN("Cannot create a DC for format %s.\n", debug_d3dformat(format
->id
));
1281 if (device
->d3d_initialized
)
1282 context
= context_acquire(device
, NULL
, 0);
1284 wined3d_texture_load_location(texture
, sub_resource_idx
, context
, texture
->resource
.map_binding
);
1285 wined3d_texture_invalidate_location(texture
, sub_resource_idx
, ~texture
->resource
.map_binding
);
1286 wined3d_texture_get_pitch(texture
, surface
->texture_level
, &row_pitch
, &slice_pitch
);
1287 wined3d_texture_get_memory(texture
, sub_resource_idx
, &data
, texture
->resource
.map_binding
);
1288 desc
.pMemory
= context_map_bo_address(context
, &data
,
1289 texture
->sub_resources
[sub_resource_idx
].size
,
1290 GL_PIXEL_UNPACK_BUFFER
, WINED3D_MAP_READ
| WINED3D_MAP_WRITE
);
1293 context_release(context
);
1295 desc
.Format
= format
->ddi_format
;
1296 desc
.Width
= wined3d_texture_get_level_width(texture
, surface
->texture_level
);
1297 desc
.Height
= wined3d_texture_get_level_height(texture
, surface
->texture_level
);
1298 desc
.Pitch
= row_pitch
;
1299 desc
.hDeviceDc
= CreateCompatibleDC(NULL
);
1300 desc
.pColorTable
= NULL
;
1302 status
= D3DKMTCreateDCFromMemory(&desc
);
1303 DeleteDC(desc
.hDeviceDc
);
1306 WARN("Failed to create DC, status %#x.\n", status
);
1310 surface
->dc
= desc
.hDc
;
1311 surface
->bitmap
= desc
.hBitmap
;
1313 TRACE("Created DC %p, bitmap %p for surface %p.\n", surface
->dc
, surface
->bitmap
, surface
);
1316 static void texture2d_destroy_dc(void *object
)
1318 struct wined3d_surface
*surface
= object
;
1319 D3DKMT_DESTROYDCFROMMEMORY destroy_desc
;
1320 struct wined3d_context
*context
= NULL
;
1321 struct wined3d_texture
*texture
;
1322 struct wined3d_bo_address data
;
1323 unsigned int sub_resource_idx
;
1324 struct wined3d_device
*device
;
1327 texture
= surface
->container
;
1328 sub_resource_idx
= surface_get_sub_resource_idx(surface
);
1329 device
= texture
->resource
.device
;
1333 ERR("Surface %p has no DC.\n", surface
);
1337 TRACE("dc %p, bitmap %p.\n", surface
->dc
, surface
->bitmap
);
1339 destroy_desc
.hDc
= surface
->dc
;
1340 destroy_desc
.hBitmap
= surface
->bitmap
;
1341 if ((status
= D3DKMTDestroyDCFromMemory(&destroy_desc
)))
1342 ERR("Failed to destroy dc, status %#x.\n", status
);
1344 surface
->bitmap
= NULL
;
1346 if (device
->d3d_initialized
)
1347 context
= context_acquire(device
, NULL
, 0);
1349 wined3d_texture_get_memory(texture
, sub_resource_idx
, &data
, texture
->resource
.map_binding
);
1350 context_unmap_bo_address(context
, &data
, GL_PIXEL_UNPACK_BUFFER
);
1353 context_release(context
);
1356 HRESULT CDECL
wined3d_texture_update_desc(struct wined3d_texture
*texture
, UINT width
, UINT height
,
1357 enum wined3d_format_id format_id
, enum wined3d_multisample_type multisample_type
,
1358 UINT multisample_quality
, void *mem
, UINT pitch
)
1360 struct wined3d_device
*device
= texture
->resource
.device
;
1361 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
1362 const struct wined3d_format
*format
= wined3d_get_format(gl_info
, format_id
, texture
->resource
.usage
);
1363 UINT resource_size
= wined3d_format_calculate_size(format
, device
->surface_alignment
, width
, height
, 1);
1364 struct wined3d_texture_sub_resource
*sub_resource
;
1365 struct wined3d_surface
*surface
;
1366 DWORD valid_location
= 0;
1367 BOOL create_dib
= FALSE
;
1369 TRACE("texture %p, width %u, height %u, format %s, multisample_type %#x, multisample_quality %u, "
1370 "mem %p, pitch %u.\n",
1371 texture
, width
, height
, debug_d3dformat(format_id
), multisample_type
, multisample_quality
, mem
, pitch
);
1374 return WINED3DERR_INVALIDCALL
;
1376 if (texture
->level_count
* texture
->layer_count
> 1)
1378 WARN("Texture has multiple sub-resources, not supported.\n");
1379 return WINED3DERR_INVALIDCALL
;
1382 if (texture
->resource
.type
!= WINED3D_RTYPE_TEXTURE_2D
)
1384 WARN("Not supported on %s.\n", debug_d3dresourcetype(texture
->resource
.type
));
1385 return WINED3DERR_INVALIDCALL
;
1388 if (texture
->resource
.type
== WINED3D_RTYPE_TEXTURE_1D
)
1390 FIXME("Not yet supported for 1D textures.\n");
1391 return WINED3DERR_INVALIDCALL
;
1394 if (texture
->resource
.map_count
)
1396 WARN("Texture is mapped.\n");
1397 return WINED3DERR_INVALIDCALL
;
1400 /* We have no way of supporting a pitch that is not a multiple of the pixel
1401 * byte width short of uploading the texture row-by-row.
1402 * Fortunately that's not an issue since D3D9Ex doesn't allow a custom pitch
1403 * for user-memory textures (it always expects packed data) while DirectDraw
1404 * requires a 4-byte aligned pitch and doesn't support texture formats
1405 * larger than 4 bytes per pixel nor any format using 3 bytes per pixel.
1406 * This check is here to verify that the assumption holds. */
1407 if (pitch
% texture
->resource
.format
->byte_count
)
1409 WARN("Pitch unsupported, not a multiple of the texture format byte width.\n");
1410 return WINED3DERR_INVALIDCALL
;
1413 if (device
->d3d_initialized
)
1414 wined3d_cs_emit_unload_resource(device
->cs
, &texture
->resource
);
1415 wined3d_resource_wait_idle(&texture
->resource
);
1417 sub_resource
= &texture
->sub_resources
[0];
1418 surface
= sub_resource
->u
.surface
;
1421 wined3d_cs_destroy_object(device
->cs
, texture2d_destroy_dc
, surface
);
1422 device
->cs
->ops
->finish(device
->cs
, WINED3D_CS_QUEUE_DEFAULT
);
1426 wined3d_resource_free_sysmem(&texture
->resource
);
1428 if ((texture
->row_pitch
= pitch
))
1429 texture
->slice_pitch
= height
* pitch
;
1431 /* User memory surfaces don't have the regular surface alignment. */
1432 wined3d_format_calculate_pitch(format
, 1, width
, height
,
1433 &texture
->row_pitch
, &texture
->slice_pitch
);
1435 texture
->resource
.format
= format
;
1436 texture
->resource
.multisample_type
= multisample_type
;
1437 texture
->resource
.multisample_quality
= multisample_quality
;
1438 texture
->resource
.width
= width
;
1439 texture
->resource
.height
= height
;
1440 texture
->resource
.size
= texture
->slice_pitch
;
1441 sub_resource
->size
= texture
->slice_pitch
;
1442 sub_resource
->locations
= WINED3D_LOCATION_DISCARDED
;
1444 if (multisample_type
&& gl_info
->supported
[ARB_TEXTURE_MULTISAMPLE
])
1445 texture
->target
= GL_TEXTURE_2D_MULTISAMPLE
;
1447 texture
->target
= GL_TEXTURE_2D
;
1449 if ((!is_power_of_two(width
) || !is_power_of_two(height
)) && !gl_info
->supported
[ARB_TEXTURE_NON_POWER_OF_TWO
]
1450 && !gl_info
->supported
[ARB_TEXTURE_RECTANGLE
] && !gl_info
->supported
[WINED3D_GL_NORMALIZED_TEXRECT
])
1452 texture
->flags
|= WINED3D_TEXTURE_COND_NP2_EMULATED
;
1453 texture
->pow2_width
= texture
->pow2_height
= 1;
1454 while (texture
->pow2_width
< width
)
1455 texture
->pow2_width
<<= 1;
1456 while (texture
->pow2_height
< height
)
1457 texture
->pow2_height
<<= 1;
1461 texture
->flags
&= ~WINED3D_TEXTURE_COND_NP2_EMULATED
;
1462 texture
->pow2_width
= width
;
1463 texture
->pow2_height
= height
;
1466 if ((texture
->user_memory
= mem
))
1468 texture
->resource
.map_binding
= WINED3D_LOCATION_USER_MEMORY
;
1469 valid_location
= WINED3D_LOCATION_USER_MEMORY
;
1473 wined3d_texture_prepare_location(texture
, 0, NULL
, WINED3D_LOCATION_SYSMEM
);
1474 valid_location
= WINED3D_LOCATION_SYSMEM
;
1477 /* The format might be changed to a format that needs conversion.
1478 * If the surface didn't use PBOs previously but could now, don't
1479 * change it - whatever made us not use PBOs might come back, e.g.
1481 if (texture
->resource
.map_binding
== WINED3D_LOCATION_BUFFER
&& !wined3d_texture_use_pbo(texture
, gl_info
))
1482 texture
->resource
.map_binding
= WINED3D_LOCATION_SYSMEM
;
1484 wined3d_texture_validate_location(texture
, 0, valid_location
);
1485 wined3d_texture_invalidate_location(texture
, 0, ~valid_location
);
1489 wined3d_cs_init_object(device
->cs
, texture2d_create_dc
, surface
);
1490 device
->cs
->ops
->finish(device
->cs
, WINED3D_CS_QUEUE_DEFAULT
);
1496 /* Context activation is done by the caller. */
1497 static void wined3d_texture_prepare_buffer_object(struct wined3d_texture
*texture
,
1498 #if !defined(STAGING_CSMT)
1499 unsigned int sub_resource_idx
, const struct wined3d_gl_info
*gl_info
)
1500 #else /* STAGING_CSMT */
1501 unsigned int sub_resource_idx
, struct wined3d_context
*context
)
1502 #endif /* STAGING_CSMT */
1504 struct wined3d_texture_sub_resource
*sub_resource
;
1506 sub_resource
= &texture
->sub_resources
[sub_resource_idx
];
1507 #if !defined(STAGING_CSMT)
1508 if (sub_resource
->buffer_object
)
1511 GL_EXTCALL(glGenBuffers(1, &sub_resource
->buffer_object
));
1512 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER
, sub_resource
->buffer_object
));
1513 GL_EXTCALL(glBufferData(GL_PIXEL_UNPACK_BUFFER
, sub_resource
->size
, NULL
, GL_STREAM_DRAW
));
1514 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER
, 0));
1515 checkGLcall("Create buffer object");
1517 TRACE("Created buffer object %u for texture %p, sub-resource %u.\n",
1518 sub_resource
->buffer_object
, texture
, sub_resource_idx
);
1519 #else /* STAGING_CSMT */
1520 if (sub_resource
->buffer
)
1523 sub_resource
->buffer
= wined3d_device_get_bo(texture
->resource
.device
,
1524 sub_resource
->size
, GL_STREAM_DRAW
, GL_PIXEL_UNPACK_BUFFER
, context
);
1526 TRACE("Created buffer object %u for texture %p, sub-resource %u.\n",
1527 sub_resource
->buffer
->name
, texture
, sub_resource_idx
);
1528 #endif /* STAGING_CSMT */
1531 static void wined3d_texture_force_reload(struct wined3d_texture
*texture
)
1533 unsigned int sub_count
= texture
->level_count
* texture
->layer_count
;
1536 texture
->flags
&= ~(WINED3D_TEXTURE_RGB_ALLOCATED
| WINED3D_TEXTURE_SRGB_ALLOCATED
1537 | WINED3D_TEXTURE_CONVERTED
);
1538 texture
->async
.flags
&= ~WINED3D_TEXTURE_ASYNC_COLOR_KEY
;
1539 for (i
= 0; i
< sub_count
; ++i
)
1541 wined3d_texture_invalidate_location(texture
, i
,
1542 WINED3D_LOCATION_TEXTURE_RGB
| WINED3D_LOCATION_TEXTURE_SRGB
);
1546 void wined3d_texture_prepare_texture(struct wined3d_texture
*texture
, struct wined3d_context
*context
, BOOL srgb
)
1548 DWORD alloc_flag
= srgb
? WINED3D_TEXTURE_SRGB_ALLOCATED
: WINED3D_TEXTURE_RGB_ALLOCATED
;
1549 const struct wined3d_d3d_info
*d3d_info
= context
->d3d_info
;
1551 if (!d3d_info
->shader_color_key
1552 && !(texture
->async
.flags
& WINED3D_TEXTURE_ASYNC_COLOR_KEY
)
1553 != !(texture
->async
.color_key_flags
& WINED3D_CKEY_SRC_BLT
))
1555 wined3d_texture_force_reload(texture
);
1557 if (texture
->async
.color_key_flags
& WINED3D_CKEY_SRC_BLT
)
1558 texture
->async
.flags
|= WINED3D_TEXTURE_ASYNC_COLOR_KEY
;
1561 if (texture
->flags
& alloc_flag
)
1564 texture
->texture_ops
->texture_prepare_texture(texture
, context
, srgb
);
1565 texture
->flags
|= alloc_flag
;
1568 static void wined3d_texture_prepare_rb(struct wined3d_texture
*texture
,
1569 const struct wined3d_gl_info
*gl_info
, BOOL multisample
)
1571 const struct wined3d_format
*format
= texture
->resource
.format
;
1577 if (texture
->rb_multisample
)
1580 samples
= wined3d_texture_get_gl_sample_count(texture
);
1582 gl_info
->fbo_ops
.glGenRenderbuffers(1, &texture
->rb_multisample
);
1583 gl_info
->fbo_ops
.glBindRenderbuffer(GL_RENDERBUFFER
, texture
->rb_multisample
);
1584 gl_info
->fbo_ops
.glRenderbufferStorageMultisample(GL_RENDERBUFFER
, samples
,
1585 format
->glInternal
, texture
->resource
.width
, texture
->resource
.height
);
1586 checkGLcall("glRenderbufferStorageMultisample()");
1587 TRACE("Created multisample rb %u.\n", texture
->rb_multisample
);
1591 if (texture
->rb_resolved
)
1594 gl_info
->fbo_ops
.glGenRenderbuffers(1, &texture
->rb_resolved
);
1595 gl_info
->fbo_ops
.glBindRenderbuffer(GL_RENDERBUFFER
, texture
->rb_resolved
);
1596 gl_info
->fbo_ops
.glRenderbufferStorage(GL_RENDERBUFFER
, format
->glInternal
,
1597 texture
->resource
.width
, texture
->resource
.height
);
1598 checkGLcall("glRenderbufferStorage()");
1599 TRACE("Created resolved rb %u.\n", texture
->rb_resolved
);
1603 /* Context activation is done by the caller. Context may be NULL in
1604 * WINED3D_NO3D mode. */
1605 BOOL
wined3d_texture_prepare_location(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
,
1606 struct wined3d_context
*context
, DWORD location
)
1610 case WINED3D_LOCATION_SYSMEM
:
1611 if (texture
->resource
.heap_memory
)
1614 if (!wined3d_resource_allocate_sysmem(&texture
->resource
))
1616 ERR("Failed to allocate system memory.\n");
1621 case WINED3D_LOCATION_USER_MEMORY
:
1622 if (!texture
->user_memory
)
1623 ERR("Map binding is set to WINED3D_LOCATION_USER_MEMORY but surface->user_memory is NULL.\n");
1626 case WINED3D_LOCATION_BUFFER
:
1627 #if !defined(STAGING_CSMT)
1628 wined3d_texture_prepare_buffer_object(texture
, sub_resource_idx
, context
->gl_info
);
1629 #else /* STAGING_CSMT */
1630 wined3d_texture_prepare_buffer_object(texture
, sub_resource_idx
, context
);
1631 #endif /* STAGING_CSMT */
1634 case WINED3D_LOCATION_TEXTURE_RGB
:
1635 wined3d_texture_prepare_texture(texture
, context
, FALSE
);
1638 case WINED3D_LOCATION_TEXTURE_SRGB
:
1639 wined3d_texture_prepare_texture(texture
, context
, TRUE
);
1642 case WINED3D_LOCATION_DRAWABLE
:
1643 if (!texture
->swapchain
&& wined3d_settings
.offscreen_rendering_mode
!= ORM_BACKBUFFER
)
1644 ERR("Texture %p does not have a drawable.\n", texture
);
1647 case WINED3D_LOCATION_RB_MULTISAMPLE
:
1648 wined3d_texture_prepare_rb(texture
, context
->gl_info
, TRUE
);
1651 case WINED3D_LOCATION_RB_RESOLVED
:
1652 wined3d_texture_prepare_rb(texture
, context
->gl_info
, FALSE
);
1656 ERR("Invalid location %s.\n", wined3d_debug_location(location
));
1661 static struct wined3d_texture_sub_resource
*wined3d_texture_get_sub_resource(struct wined3d_texture
*texture
,
1662 unsigned int sub_resource_idx
)
1664 UINT sub_count
= texture
->level_count
* texture
->layer_count
;
1666 TRACE("texture %p, sub_resource_idx %u.\n", texture
, sub_resource_idx
);
1668 if (sub_resource_idx
>= sub_count
)
1670 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx
, sub_count
);
1674 return &texture
->sub_resources
[sub_resource_idx
];
1677 HRESULT CDECL
wined3d_texture_add_dirty_region(struct wined3d_texture
*texture
,
1678 UINT layer
, const struct wined3d_box
*dirty_region
)
1680 TRACE("texture %p, layer %u, dirty_region %s.\n", texture
, layer
, debug_box(dirty_region
));
1682 if (layer
>= texture
->layer_count
)
1684 WARN("Invalid layer %u specified.\n", layer
);
1685 return WINED3DERR_INVALIDCALL
;
1689 WARN("Ignoring dirty_region %s.\n", debug_box(dirty_region
));
1691 wined3d_cs_emit_add_dirty_texture_region(texture
->resource
.device
->cs
, texture
, layer
);
1696 void wined3d_texture_upload_data(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
,
1697 const struct wined3d_context
*context
, const struct wined3d_box
*box
,
1698 const struct wined3d_const_bo_address
*data
, unsigned int row_pitch
, unsigned int slice_pitch
)
1700 texture
->texture_ops
->texture_upload_data(texture
, sub_resource_idx
,
1701 context
, box
, data
, row_pitch
, slice_pitch
);
1705 /* This call just uploads data, the caller is responsible for binding the
1706 * correct texture. */
1707 /* Context activation is done by the caller. */
1708 static void texture1d_upload_data(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
,
1709 const struct wined3d_context
*context
, const struct wined3d_box
*box
, const struct wined3d_const_bo_address
*data
,
1710 unsigned int row_pitch
, unsigned int slice_pitch
)
1712 struct wined3d_surface
*surface
= texture
->sub_resources
[sub_resource_idx
].u
.surface
;
1713 const struct wined3d_format
*format
= texture
->resource
.format
;
1714 unsigned int level
= sub_resource_idx
% texture
->level_count
;
1715 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1716 const void *mem
= data
->addr
;
1717 void *converted_mem
= NULL
;
1718 unsigned int width
, x
, update_w
;
1721 TRACE("texture %p, sub_resource_idx %u, context %p, box %p, data {%#x:%p}, row_pitch %#x, slice_pitch %#x.\n",
1722 texture
, sub_resource_idx
, context
, box
, data
->buffer_object
, data
->addr
, row_pitch
, slice_pitch
);
1724 width
= wined3d_texture_get_level_width(texture
, level
);
1734 update_w
= box
->right
- box
->left
;
1739 unsigned int dst_row_pitch
;
1741 if (data
->buffer_object
)
1742 ERR("Loading a converted texture from a PBO.\n");
1743 if (texture
->resource
.format_flags
& WINED3DFMT_FLAG_BLOCKS
)
1744 ERR("Converting a block-based format.\n");
1746 dst_row_pitch
= update_w
* format
->conv_byte_count
;
1748 converted_mem
= HeapAlloc(GetProcessHeap(), 0, dst_row_pitch
);
1749 format
->upload(data
->addr
, converted_mem
, row_pitch
, slice_pitch
, dst_row_pitch
, dst_row_pitch
, update_w
, 1, 1);
1750 mem
= converted_mem
;
1753 if (data
->buffer_object
)
1755 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER
, data
->buffer_object
));
1756 checkGLcall("glBindBuffer");
1759 target
= wined3d_texture_get_sub_resource_target(texture
, sub_resource_idx
);
1760 if (target
== GL_TEXTURE_1D_ARRAY
)
1762 gl_info
->gl_ops
.gl
.p_glPixelStorei(GL_UNPACK_ROW_LENGTH
, row_pitch
/ format
->byte_count
);
1764 gl_info
->gl_ops
.gl
.p_glTexSubImage2D(target
, level
, x
, surface
->texture_layer
, update_w
, 1, format
->glFormat
, format
->glType
, mem
);
1765 checkGLcall("glTexSubImage2D");
1767 gl_info
->gl_ops
.gl
.p_glPixelStorei(GL_UNPACK_ROW_LENGTH
, 0);
1771 gl_info
->gl_ops
.gl
.p_glTexSubImage1D(target
, level
, x
, update_w
, format
->glFormat
, format
->glType
, mem
);
1772 checkGLcall("glTexSubImage1D");
1775 if (data
->buffer_object
)
1777 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER
, 0));
1778 checkGLcall("glBindBuffer");
1781 HeapFree(GetProcessHeap(), 0, converted_mem
);
1784 /* Context activation is done by the caller. */
1785 static void texture1d_download_data(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
,
1786 const struct wined3d_context
*context
, const struct wined3d_bo_address
*data
)
1788 struct wined3d_surface
*surface
= texture
->sub_resources
[sub_resource_idx
].u
.surface
;
1789 const struct wined3d_format
*format
= texture
->resource
.format
;
1790 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1791 struct wined3d_texture_sub_resource
*sub_resource
;
1792 BYTE
*temporary_mem
= NULL
;
1796 sub_resource
= &texture
->sub_resources
[sub_resource_idx
];
1798 if (format
->conv_byte_count
)
1800 FIXME("Attempting to download a converted 1d texture, format %s.\n",
1801 debug_d3dformat(format
->id
));
1805 target
= wined3d_texture_get_sub_resource_target(texture
, sub_resource_idx
);
1806 if (target
== GL_TEXTURE_1D_ARRAY
)
1808 WARN_(d3d_perf
)("Downloading all miplevel layers to get the surface data for a single sub-resource.\n");
1810 if (!(temporary_mem
= heap_calloc(texture
->layer_count
, sub_resource
->size
)))
1812 ERR("Out of memory.\n");
1816 mem
= temporary_mem
;
1818 else if (data
->buffer_object
)
1820 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER
, data
->buffer_object
));
1821 checkGLcall("glBindBuffer");
1827 gl_info
->gl_ops
.gl
.p_glGetTexImage(target
, sub_resource_idx
,
1828 format
->glFormat
, format
->glType
, mem
);
1829 checkGLcall("glGetTexImage");
1833 void *src_data
= temporary_mem
+ surface
->texture_layer
* sub_resource
->size
;
1834 if (data
->buffer_object
)
1836 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER
, data
->buffer_object
));
1837 checkGLcall("glBindBuffer");
1838 GL_EXTCALL(glBufferSubData(GL_PIXEL_PACK_BUFFER
, 0, sub_resource
->size
, src_data
));
1839 checkGLcall("glBufferSubData");
1843 memcpy(data
->addr
, src_data
, sub_resource
->size
);
1847 if (data
->buffer_object
)
1849 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER
, 0));
1850 checkGLcall("glBindBuffer");
1853 HeapFree(GetProcessHeap(), 0, temporary_mem
);
1856 /* Context activation is done by the caller. */
1857 static void texture1d_srgb_transfer(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
,
1858 struct wined3d_context
*context
, BOOL dest_is_srgb
)
1860 struct wined3d_texture_sub_resource
*sub_resource
= &texture
->sub_resources
[sub_resource_idx
];
1861 unsigned int row_pitch
, slice_pitch
;
1862 struct wined3d_bo_address data
;
1864 WARN_(d3d_perf
)("Performing slow rgb/srgb 1d texture transfer.\n");
1865 data
.buffer_object
= 0;
1866 if (!(data
.addr
= HeapAlloc(GetProcessHeap(), 0, sub_resource
->size
)))
1869 wined3d_texture_get_pitch(texture
, sub_resource_idx
, &row_pitch
, &slice_pitch
);
1870 wined3d_texture_bind_and_dirtify(texture
, context
, !dest_is_srgb
);
1871 texture1d_download_data(texture
, sub_resource_idx
, context
, &data
);
1872 wined3d_texture_bind_and_dirtify(texture
, context
, dest_is_srgb
);
1873 texture1d_upload_data(texture
, sub_resource_idx
, context
, NULL
,
1874 wined3d_const_bo_address(&data
), row_pitch
, slice_pitch
);
1876 HeapFree(GetProcessHeap(), 0, data
.addr
);
1879 /* Context activation is done by the caller. */
1880 static BOOL
texture1d_load_location(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
,
1881 struct wined3d_context
*context
, DWORD location
)
1883 struct wined3d_texture_sub_resource
*sub_resource
= &texture
->sub_resources
[sub_resource_idx
];
1884 DWORD required_access
= wined3d_resource_access_from_location(location
);
1885 unsigned int row_pitch
, slice_pitch
;
1887 TRACE("texture %p, sub_resource_idx %u, context %p, location %s.\n",
1888 texture
, sub_resource_idx
, context
, wined3d_debug_location(location
));
1890 TRACE("Current resource location %s.\n", wined3d_debug_location(sub_resource
->locations
));
1892 if ((sub_resource
->locations
& location
) == location
)
1894 TRACE("Location(s) already up to date.\n");
1898 if ((texture
->resource
.access
& required_access
) != required_access
)
1900 ERR("Operation requires %#x access, but 1d texture only has %#x.\n",
1901 required_access
, texture
->resource
.access
);
1905 if (!wined3d_texture_prepare_location(texture
, sub_resource_idx
, context
, location
))
1908 if (sub_resource
->locations
& WINED3D_LOCATION_DISCARDED
)
1910 TRACE("1d texture previously discarded, nothing to do.\n");
1911 wined3d_texture_validate_location(texture
, sub_resource_idx
, location
);
1912 wined3d_texture_invalidate_location(texture
, sub_resource_idx
, WINED3D_LOCATION_DISCARDED
);
1918 case WINED3D_LOCATION_TEXTURE_RGB
:
1919 case WINED3D_LOCATION_TEXTURE_SRGB
:
1920 if (sub_resource
->locations
& WINED3D_LOCATION_SYSMEM
)
1922 struct wined3d_const_bo_address data
= {0, texture
->resource
.heap_memory
};
1923 data
.addr
+= sub_resource
->offset
;
1924 wined3d_texture_bind_and_dirtify(texture
, context
, location
== WINED3D_LOCATION_TEXTURE_SRGB
);
1925 wined3d_texture_get_pitch(texture
, sub_resource_idx
, &row_pitch
, &slice_pitch
);
1926 texture1d_upload_data(texture
, sub_resource_idx
, context
, NULL
, &data
, row_pitch
, slice_pitch
);
1928 else if (sub_resource
->locations
& WINED3D_LOCATION_BUFFER
)
1930 #if !defined(STAGING_CSMT)
1931 struct wined3d_const_bo_address data
= {sub_resource
->buffer_object
, NULL
};
1932 #else /* STAGING_CSMT */
1933 struct wined3d_const_bo_address data
= {sub_resource
->buffer
->name
, NULL
};
1934 #endif /* STAGING_CSMT */
1935 wined3d_texture_bind_and_dirtify(texture
, context
, location
== WINED3D_LOCATION_TEXTURE_SRGB
);
1936 wined3d_texture_get_pitch(texture
, sub_resource_idx
, &row_pitch
, &slice_pitch
);
1937 texture1d_upload_data(texture
, sub_resource_idx
, context
, NULL
, &data
, row_pitch
, slice_pitch
);
1939 else if (sub_resource
->locations
& WINED3D_LOCATION_TEXTURE_RGB
)
1941 texture1d_srgb_transfer(texture
, sub_resource_idx
, context
, TRUE
);
1943 else if (sub_resource
->locations
& WINED3D_LOCATION_TEXTURE_SRGB
)
1945 texture1d_srgb_transfer(texture
, sub_resource_idx
, context
, FALSE
);
1949 FIXME("Implement 1d texture loading from %s.\n", wined3d_debug_location(sub_resource
->locations
));
1954 case WINED3D_LOCATION_SYSMEM
:
1955 if (sub_resource
->locations
& (WINED3D_LOCATION_TEXTURE_RGB
| WINED3D_LOCATION_TEXTURE_SRGB
))
1957 struct wined3d_bo_address data
= {0, texture
->resource
.heap_memory
};
1959 data
.addr
+= sub_resource
->offset
;
1960 if (sub_resource
->locations
& WINED3D_LOCATION_TEXTURE_RGB
)
1961 wined3d_texture_bind_and_dirtify(texture
, context
, FALSE
);
1963 wined3d_texture_bind_and_dirtify(texture
, context
, TRUE
);
1965 texture1d_download_data(texture
, sub_resource_idx
, context
, &data
);
1966 ++texture
->download_count
;
1970 FIXME("Implement WINED3D_LOCATION_SYSMEM loading from %s.\n",
1971 wined3d_debug_location(sub_resource
->locations
));
1976 case WINED3D_LOCATION_BUFFER
:
1977 if (sub_resource
->locations
& (WINED3D_LOCATION_TEXTURE_RGB
| WINED3D_LOCATION_TEXTURE_SRGB
))
1979 #if !defined(STAGING_CSMT)
1980 struct wined3d_bo_address data
= {sub_resource
->buffer_object
, NULL
};
1981 #else /* STAGING_CSMT */
1982 struct wined3d_bo_address data
= {sub_resource
->buffer
->name
, NULL
};
1983 #endif /* STAGING_CSMT */
1985 if (sub_resource
->locations
& WINED3D_LOCATION_TEXTURE_RGB
)
1986 wined3d_texture_bind_and_dirtify(texture
, context
, FALSE
);
1988 wined3d_texture_bind_and_dirtify(texture
, context
, TRUE
);
1990 texture1d_download_data(texture
, sub_resource_idx
, context
, &data
);
1994 FIXME("Implement WINED3D_LOCATION_BUFFER loading from %s.\n",
1995 wined3d_debug_location(sub_resource
->locations
));
2001 FIXME("Implement %s loading from %s.\n", wined3d_debug_location(location
),
2002 wined3d_debug_location(sub_resource
->locations
));
2007 wined3d_texture_validate_location(texture
, sub_resource_idx
, location
);
2012 static void texture1d_prepare_texture(struct wined3d_texture
*texture
, struct wined3d_context
*context
, BOOL srgb
)
2014 const struct wined3d_format
*format
= texture
->resource
.format
;
2015 unsigned int sub_count
= texture
->level_count
* texture
->layer_count
;
2016 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2020 wined3d_texture_bind_and_dirtify(texture
, context
, srgb
);
2023 internal
= format
->glGammaInternal
;
2024 else if (texture
->resource
.usage
& WINED3DUSAGE_RENDERTARGET
2025 && wined3d_resource_is_offscreen(&texture
->resource
))
2026 internal
= format
->rtInternal
;
2028 internal
= format
->glInternal
;
2030 if (wined3d_texture_use_immutable_storage(texture
, gl_info
))
2032 width
= wined3d_texture_get_level_width(texture
, 0);
2034 if (texture
->target
== GL_TEXTURE_1D_ARRAY
)
2036 GL_EXTCALL(glTexStorage2D(texture
->target
, texture
->level_count
, internal
, width
, texture
->layer_count
));
2037 checkGLcall("glTexStorage2D");
2041 GL_EXTCALL(glTexStorage1D(texture
->target
, texture
->level_count
, internal
, width
));
2042 checkGLcall("glTexStorage1D");
2049 for (i
= 0; i
< sub_count
; ++i
)
2052 struct wined3d_surface
*surface
= texture
->sub_resources
[i
].u
.surface
;
2053 width
= wined3d_texture_get_level_width(texture
, surface
->texture_level
);
2054 target
= wined3d_texture_get_sub_resource_target(texture
, i
);
2056 if (texture
->target
== GL_TEXTURE_1D_ARRAY
)
2058 gl_info
->gl_ops
.gl
.p_glTexImage2D(target
, surface
->texture_level
,
2059 internal
, width
, texture
->layer_count
, 0, format
->glFormat
, format
->glType
, NULL
);
2060 checkGLcall("glTexImage2D");
2064 gl_info
->gl_ops
.gl
.p_glTexImage1D(target
, surface
->texture_level
,
2065 internal
, width
, 0, format
->glFormat
, format
->glType
, NULL
);
2066 checkGLcall("glTexImage1D");
2072 static void texture1d_cleanup_sub_resources(struct wined3d_texture
*texture
)
2076 static const struct wined3d_texture_ops texture1d_ops
=
2078 texture1d_upload_data
,
2079 texture1d_load_location
,
2080 texture1d_prepare_texture
,
2081 texture1d_cleanup_sub_resources
,
2084 static void texture2d_upload_data(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
,
2085 const struct wined3d_context
*context
, const struct wined3d_box
*box
,
2086 const struct wined3d_const_bo_address
*data
, unsigned int row_pitch
, unsigned int slice_pitch
)
2088 unsigned int texture_level
;
2096 dst_point
.x
= box
->left
;
2097 dst_point
.y
= box
->top
;
2098 src_rect
.right
= box
->right
- box
->left
;
2099 src_rect
.bottom
= box
->bottom
- box
->top
;
2103 dst_point
.x
= dst_point
.y
= 0;
2104 texture_level
= sub_resource_idx
% texture
->level_count
;
2105 src_rect
.right
= wined3d_texture_get_level_width(texture
, texture_level
);
2106 src_rect
.bottom
= wined3d_texture_get_level_height(texture
, texture_level
);
2109 wined3d_surface_upload_data(texture
->sub_resources
[sub_resource_idx
].u
.surface
, context
->gl_info
,
2110 texture
->resource
.format
, &src_rect
, row_pitch
, &dst_point
, FALSE
, data
);
2113 static BOOL
texture2d_load_location(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
,
2114 struct wined3d_context
*context
, DWORD location
)
2116 return surface_load_location(texture
->sub_resources
[sub_resource_idx
].u
.surface
, context
, location
);
2119 /* Context activation is done by the caller. */
2120 static void texture2d_prepare_texture(struct wined3d_texture
*texture
, struct wined3d_context
*context
, BOOL srgb
)
2122 const struct wined3d_format
*format
= texture
->resource
.format
;
2123 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2124 const struct wined3d_color_key_conversion
*conversion
;
2127 TRACE("texture %p, context %p, format %s.\n", texture
, context
, debug_d3dformat(format
->id
));
2129 if (format
->conv_byte_count
)
2131 texture
->flags
|= WINED3D_TEXTURE_CONVERTED
;
2133 else if ((conversion
= wined3d_format_get_color_key_conversion(texture
, TRUE
)))
2135 texture
->flags
|= WINED3D_TEXTURE_CONVERTED
;
2136 format
= wined3d_get_format(gl_info
, conversion
->dst_format
, texture
->resource
.usage
);
2137 TRACE("Using format %s for color key conversion.\n", debug_d3dformat(format
->id
));
2140 wined3d_texture_bind_and_dirtify(texture
, context
, srgb
);
2143 internal
= format
->glGammaInternal
;
2144 else if (texture
->resource
.usage
& WINED3DUSAGE_RENDERTARGET
2145 && wined3d_resource_is_offscreen(&texture
->resource
))
2146 internal
= format
->rtInternal
;
2148 internal
= format
->glInternal
;
2151 FIXME("No GL internal format for format %s.\n", debug_d3dformat(format
->id
));
2153 TRACE("internal %#x, format %#x, type %#x.\n", internal
, format
->glFormat
, format
->glType
);
2155 if (wined3d_texture_use_immutable_storage(texture
, gl_info
))
2156 wined3d_texture_allocate_gl_immutable_storage(texture
, internal
, gl_info
);
2158 wined3d_texture_allocate_gl_mutable_storage(texture
, internal
, format
, gl_info
);
2161 static void texture2d_cleanup_sub_resources(struct wined3d_texture
*texture
)
2163 unsigned int sub_count
= texture
->level_count
* texture
->layer_count
;
2164 struct wined3d_device
*device
= texture
->resource
.device
;
2165 struct wined3d_texture_sub_resource
*sub_resource
;
2166 struct wined3d_renderbuffer_entry
*entry
, *entry2
;
2167 const struct wined3d_gl_info
*gl_info
= NULL
;
2168 struct wined3d_context
*context
= NULL
;
2169 struct wined3d_surface
*surface
;
2172 for (i
= 0; i
< sub_count
; ++i
)
2174 sub_resource
= &texture
->sub_resources
[i
];
2175 if (!(surface
= sub_resource
->u
.surface
))
2178 TRACE("surface %p.\n", surface
);
2180 if (!context
&& !list_empty(&surface
->renderbuffers
))
2182 context
= context_acquire(device
, NULL
, 0);
2183 gl_info
= context
->gl_info
;
2186 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, &surface
->renderbuffers
, struct wined3d_renderbuffer_entry
, entry
)
2188 TRACE("Deleting renderbuffer %u.\n", entry
->id
);
2189 context_gl_resource_released(device
, entry
->id
, TRUE
);
2190 gl_info
->fbo_ops
.glDeleteRenderbuffers(1, &entry
->id
);
2195 texture2d_destroy_dc(surface
);
2198 context_release(context
);
2199 heap_free(texture
->sub_resources
[0].u
.surface
);
2202 static const struct wined3d_texture_ops texture2d_ops
=
2204 texture2d_upload_data
,
2205 texture2d_load_location
,
2206 texture2d_prepare_texture
,
2207 texture2d_cleanup_sub_resources
,
2210 struct wined3d_texture
* __cdecl
wined3d_texture_from_resource(struct wined3d_resource
*resource
)
2212 return texture_from_resource(resource
);
2215 static ULONG
texture_resource_incref(struct wined3d_resource
*resource
)
2217 return wined3d_texture_incref(texture_from_resource(resource
));
2220 static ULONG
texture_resource_decref(struct wined3d_resource
*resource
)
2222 return wined3d_texture_decref(texture_from_resource(resource
));
2225 static void texture_resource_preload(struct wined3d_resource
*resource
)
2227 struct wined3d_texture
*texture
= texture_from_resource(resource
);
2228 struct wined3d_context
*context
;
2230 context
= context_acquire(resource
->device
, NULL
, 0);
2231 wined3d_texture_load(texture
, context
, texture
->flags
& WINED3D_TEXTURE_IS_SRGB
);
2232 context_release(context
);
2235 static void wined3d_texture_unload(struct wined3d_resource
*resource
)
2237 struct wined3d_texture
*texture
= texture_from_resource(resource
);
2238 UINT sub_count
= texture
->level_count
* texture
->layer_count
;
2239 struct wined3d_device
*device
= resource
->device
;
2240 const struct wined3d_gl_info
*gl_info
;
2241 struct wined3d_context
*context
;
2244 TRACE("texture %p.\n", texture
);
2246 context
= context_acquire(device
, NULL
, 0);
2247 gl_info
= context
->gl_info
;
2249 for (i
= 0; i
< sub_count
; ++i
)
2251 struct wined3d_texture_sub_resource
*sub_resource
= &texture
->sub_resources
[i
];
2253 if (resource
->access
& WINED3D_RESOURCE_ACCESS_CPU
2254 && wined3d_texture_load_location(texture
, i
, context
, resource
->map_binding
))
2256 wined3d_texture_invalidate_location(texture
, i
, ~resource
->map_binding
);
2260 /* We should only get here on device reset/teardown for implicit
2262 if (resource
->access
& WINED3D_RESOURCE_ACCESS_CPU
2263 || resource
->type
!= WINED3D_RTYPE_TEXTURE_2D
)
2264 ERR("Discarding %s %p sub-resource %u with resource access %s.\n",
2265 debug_d3dresourcetype(resource
->type
), resource
, i
,
2266 wined3d_debug_resource_access(resource
->access
));
2267 wined3d_texture_validate_location(texture
, i
, WINED3D_LOCATION_DISCARDED
);
2268 wined3d_texture_invalidate_location(texture
, i
, ~WINED3D_LOCATION_DISCARDED
);
2271 #if !defined(STAGING_CSMT)
2272 if (sub_resource
->buffer_object
)
2273 wined3d_texture_remove_buffer_object(texture
, i
, context
->gl_info
);
2274 #else /* STAGING_CSMT */
2275 if (sub_resource
->buffer
)
2276 wined3d_texture_remove_buffer_object(texture
, i
, context
);
2277 #endif /* STAGING_CSMT */
2279 if (resource
->type
== WINED3D_RTYPE_TEXTURE_2D
)
2281 struct wined3d_surface
*surface
= sub_resource
->u
.surface
;
2282 struct wined3d_renderbuffer_entry
*entry
, *entry2
;
2284 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, &surface
->renderbuffers
, struct wined3d_renderbuffer_entry
, entry
)
2286 context_gl_resource_released(device
, entry
->id
, TRUE
);
2287 gl_info
->fbo_ops
.glDeleteRenderbuffers(1, &entry
->id
);
2288 list_remove(&entry
->entry
);
2291 list_init(&surface
->renderbuffers
);
2292 surface
->current_renderbuffer
= NULL
;
2296 context_release(context
);
2298 wined3d_texture_force_reload(texture
);
2299 wined3d_texture_unload_gl_texture(texture
);
2302 static HRESULT
texture_resource_sub_resource_map(struct wined3d_resource
*resource
, unsigned int sub_resource_idx
,
2303 struct wined3d_map_desc
*map_desc
, const struct wined3d_box
*box
, DWORD flags
)
2305 const struct wined3d_format
*format
= resource
->format
;
2306 struct wined3d_texture_sub_resource
*sub_resource
;
2307 struct wined3d_device
*device
= resource
->device
;
2308 unsigned int fmt_flags
= resource
->format_flags
;
2309 struct wined3d_context
*context
= NULL
;
2310 struct wined3d_texture
*texture
;
2311 struct wined3d_bo_address data
;
2312 unsigned int texture_level
;
2316 TRACE("resource %p, sub_resource_idx %u, map_desc %p, box %s, flags %#x.\n",
2317 resource
, sub_resource_idx
, map_desc
, debug_box(box
), flags
);
2319 texture
= texture_from_resource(resource
);
2320 if (!(sub_resource
= wined3d_texture_get_sub_resource(texture
, sub_resource_idx
)))
2321 return E_INVALIDARG
;
2323 texture_level
= sub_resource_idx
% texture
->level_count
;
2324 if (box
&& FAILED(wined3d_texture_check_box_dimensions(texture
, texture_level
, box
)))
2326 WARN("Map box is invalid.\n");
2327 if (((fmt_flags
& WINED3DFMT_FLAG_BLOCKS
) && !(resource
->access
& WINED3D_RESOURCE_ACCESS_CPU
))
2328 || resource
->type
!= WINED3D_RTYPE_TEXTURE_2D
)
2329 return WINED3DERR_INVALIDCALL
;
2332 if (texture
->flags
& WINED3D_TEXTURE_DC_IN_USE
)
2334 WARN("DC is in use.\n");
2335 return WINED3DERR_INVALIDCALL
;
2338 if (sub_resource
->map_count
)
2340 WARN("Sub-resource is already mapped.\n");
2341 return WINED3DERR_INVALIDCALL
;
2344 if (device
->d3d_initialized
)
2345 context
= context_acquire(device
, NULL
, 0);
2347 if (flags
& WINED3D_MAP_DISCARD
)
2349 TRACE("WINED3D_MAP_DISCARD flag passed, marking %s as up to date.\n",
2350 wined3d_debug_location(resource
->map_binding
));
2351 if ((ret
= wined3d_texture_prepare_location(texture
, sub_resource_idx
, context
, resource
->map_binding
)))
2352 wined3d_texture_validate_location(texture
, sub_resource_idx
, resource
->map_binding
);
2356 if (resource
->usage
& WINED3DUSAGE_DYNAMIC
)
2357 WARN_(d3d_perf
)("Mapping a dynamic texture without WINED3D_MAP_DISCARD.\n");
2358 ret
= wined3d_texture_load_location(texture
, sub_resource_idx
, context
, resource
->map_binding
);
2363 ERR("Failed to prepare location.\n");
2364 context_release(context
);
2365 return E_OUTOFMEMORY
;
2368 if (flags
& WINED3D_MAP_WRITE
2369 && (!(flags
& WINED3D_MAP_NO_DIRTY_UPDATE
) || (resource
->usage
& WINED3DUSAGE_DYNAMIC
)))
2370 wined3d_texture_invalidate_location(texture
, sub_resource_idx
, ~resource
->map_binding
);
2372 wined3d_texture_get_memory(texture
, sub_resource_idx
, &data
, resource
->map_binding
);
2373 base_memory
= context_map_bo_address(context
, &data
, sub_resource
->size
, GL_PIXEL_UNPACK_BUFFER
, flags
);
2374 TRACE("Base memory pointer %p.\n", base_memory
);
2377 context_release(context
);
2379 if (fmt_flags
& WINED3DFMT_FLAG_BROKEN_PITCH
)
2381 map_desc
->row_pitch
= wined3d_texture_get_level_width(texture
, texture_level
) * format
->byte_count
;
2382 map_desc
->slice_pitch
= wined3d_texture_get_level_height(texture
, texture_level
) * map_desc
->row_pitch
;
2386 wined3d_texture_get_pitch(texture
, texture_level
, &map_desc
->row_pitch
, &map_desc
->slice_pitch
);
2391 map_desc
->data
= base_memory
;
2395 if ((fmt_flags
& (WINED3DFMT_FLAG_BLOCKS
| WINED3DFMT_FLAG_BROKEN_PITCH
)) == WINED3DFMT_FLAG_BLOCKS
)
2397 /* Compressed textures are block based, so calculate the offset of
2398 * the block that contains the top-left pixel of the mapped box. */
2399 map_desc
->data
= base_memory
2400 + (box
->front
* map_desc
->slice_pitch
)
2401 + ((box
->top
/ format
->block_height
) * map_desc
->row_pitch
)
2402 + ((box
->left
/ format
->block_width
) * format
->block_byte_count
);
2406 map_desc
->data
= base_memory
2407 + (box
->front
* map_desc
->slice_pitch
)
2408 + (box
->top
* map_desc
->row_pitch
)
2409 + (box
->left
* format
->byte_count
);
2413 if (texture
->swapchain
&& texture
->swapchain
->front_buffer
== texture
)
2415 RECT
*r
= &texture
->swapchain
->front_buffer_update
;
2418 SetRect(r
, 0, 0, resource
->width
, resource
->height
);
2420 SetRect(r
, box
->left
, box
->top
, box
->right
, box
->bottom
);
2421 TRACE("Mapped front buffer %s.\n", wine_dbgstr_rect(r
));
2424 ++resource
->map_count
;
2425 ++sub_resource
->map_count
;
2427 TRACE("Returning memory %p, row pitch %u, slice pitch %u.\n",
2428 map_desc
->data
, map_desc
->row_pitch
, map_desc
->slice_pitch
);
2433 static HRESULT
texture_resource_sub_resource_map_info(struct wined3d_resource
*resource
, unsigned int sub_resource_idx
,
2434 struct wined3d_map_info
*info
, DWORD flags
)
2436 const struct wined3d_format
*format
= resource
->format
;
2437 struct wined3d_texture_sub_resource
*sub_resource
;
2438 unsigned int fmt_flags
= resource
->format_flags
;
2439 struct wined3d_texture
*texture
;
2440 unsigned int texture_level
;
2442 texture
= texture_from_resource(resource
);
2443 if (!(sub_resource
= wined3d_texture_get_sub_resource(texture
, sub_resource_idx
)))
2444 return E_INVALIDARG
;
2446 texture_level
= sub_resource_idx
% texture
->level_count
;
2448 if (fmt_flags
& WINED3DFMT_FLAG_BROKEN_PITCH
)
2450 info
->row_pitch
= wined3d_texture_get_level_width(texture
, texture_level
) * format
->byte_count
;
2451 info
->slice_pitch
= wined3d_texture_get_level_height(texture
, texture_level
) * info
->row_pitch
;
2455 wined3d_texture_get_pitch(texture
, texture_level
, &info
->row_pitch
, &info
->slice_pitch
);
2458 info
->size
= info
->slice_pitch
* wined3d_texture_get_level_depth(texture
, texture_level
);
2463 static HRESULT
texture_resource_sub_resource_unmap(struct wined3d_resource
*resource
, unsigned int sub_resource_idx
)
2465 struct wined3d_texture_sub_resource
*sub_resource
;
2466 struct wined3d_device
*device
= resource
->device
;
2467 struct wined3d_context
*context
= NULL
;
2468 struct wined3d_texture
*texture
;
2469 struct wined3d_bo_address data
;
2471 TRACE("resource %p, sub_resource_idx %u.\n", resource
, sub_resource_idx
);
2473 texture
= texture_from_resource(resource
);
2474 if (!(sub_resource
= wined3d_texture_get_sub_resource(texture
, sub_resource_idx
)))
2475 return E_INVALIDARG
;
2477 if (!sub_resource
->map_count
)
2479 WARN("Trying to unmap unmapped sub-resource.\n");
2480 if (texture
->flags
& WINED3D_TEXTURE_DC_IN_USE
)
2482 return WINEDDERR_NOTLOCKED
;
2485 if (device
->d3d_initialized
)
2486 context
= context_acquire(device
, NULL
, 0);
2488 wined3d_texture_get_memory(texture
, sub_resource_idx
, &data
, texture
->resource
.map_binding
);
2489 context_unmap_bo_address(context
, &data
, GL_PIXEL_UNPACK_BUFFER
);
2492 context_release(context
);
2494 if (texture
->swapchain
&& texture
->swapchain
->front_buffer
== texture
)
2496 if (!(sub_resource
->locations
& (WINED3D_LOCATION_DRAWABLE
| WINED3D_LOCATION_TEXTURE_RGB
)))
2497 texture
->swapchain
->swapchain_ops
->swapchain_frontbuffer_updated(texture
->swapchain
);
2500 --sub_resource
->map_count
;
2501 if (!--resource
->map_count
&& texture
->update_map_binding
)
2502 wined3d_texture_update_map_binding(texture
);
2507 static const struct wined3d_resource_ops texture_resource_ops
=
2509 texture_resource_incref
,
2510 texture_resource_decref
,
2511 texture_resource_preload
,
2512 wined3d_texture_unload
,
2513 texture_resource_sub_resource_map
,
2514 texture_resource_sub_resource_map_info
,
2515 texture_resource_sub_resource_unmap
,
2518 static HRESULT
texture1d_init(struct wined3d_texture
*texture
, const struct wined3d_resource_desc
*desc
,
2519 UINT layer_count
, UINT level_count
, struct wined3d_device
*device
, void *parent
,
2520 const struct wined3d_parent_ops
*parent_ops
)
2522 struct wined3d_device_parent
*device_parent
= device
->device_parent
;
2523 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
2524 struct wined3d_surface
*surfaces
;
2528 if (layer_count
> 1 && !gl_info
->supported
[EXT_TEXTURE_ARRAY
])
2530 WARN("OpenGL implementation does not support array textures.\n");
2531 return WINED3DERR_INVALIDCALL
;
2534 /* TODO: It should only be possible to create textures for formats
2535 * that are reported as supported. */
2536 if (WINED3DFMT_UNKNOWN
>= desc
->format
)
2538 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture
);
2539 return WINED3DERR_INVALIDCALL
;
2542 if (desc
->usage
& WINED3DUSAGE_LEGACY_CUBEMAP
)
2544 WARN("1d textures can not be used for cube mapping, returning D3DERR_INVALIDCALL.\n");
2545 return WINED3DERR_INVALIDCALL
;
2548 if ((desc
->usage
& WINED3DUSAGE_DYNAMIC
&& wined3d_resource_access_is_managed(desc
->access
))
2549 || (desc
->usage
& WINED3DUSAGE_SCRATCH
))
2551 WARN("Attempted to create a DYNAMIC texture in pool %s.\n", wined3d_debug_resource_access(desc
->access
));
2552 return WINED3DERR_INVALIDCALL
;
2555 if (!gl_info
->supported
[ARB_TEXTURE_NON_POWER_OF_TWO
] && !is_power_of_two(desc
->width
))
2557 if (desc
->usage
& WINED3DUSAGE_SCRATCH
)
2559 WARN("Creating a scratch NPOT 1d texture despite lack of HW support.\n");
2563 WARN("Attempted to create a NPOT 1d texture (%u, %u, %u) without GL support.\n",
2564 desc
->width
, desc
->height
, desc
->depth
);
2565 return WINED3DERR_INVALIDCALL
;
2569 if (desc
->usage
& WINED3DUSAGE_QUERY_GENMIPMAP
)
2571 if (level_count
!= 1)
2573 WARN("WINED3DUSAGE_QUERY_GENMIPMAP is set, and level count != 1, returning WINED3DERR_INVALIDCALL.\n");
2574 return WINED3DERR_INVALIDCALL
;
2578 if (FAILED(hr
= wined3d_texture_init(texture
, &texture1d_ops
, layer_count
, level_count
, desc
,
2579 0, device
, parent
, parent_ops
, &texture_resource_ops
)))
2581 WARN("Failed to initialize texture, returning %#x.\n", hr
);
2585 texture
->pow2_matrix
[0] = 1.0f
;
2586 texture
->pow2_matrix
[5] = 1.0f
;
2587 texture
->pow2_matrix
[10] = 1.0f
;
2588 texture
->pow2_matrix
[15] = 1.0f
;
2589 texture
->target
= (layer_count
> 1) ? GL_TEXTURE_1D_ARRAY
: GL_TEXTURE_1D
;
2591 if (wined3d_texture_use_pbo(texture
, gl_info
))
2593 wined3d_resource_free_sysmem(&texture
->resource
);
2594 texture
->resource
.map_binding
= WINED3D_LOCATION_BUFFER
;
2597 if (level_count
> ~(SIZE_T
)0 / layer_count
2598 || !(surfaces
= heap_calloc(level_count
* layer_count
, sizeof(*surfaces
))))
2600 wined3d_texture_cleanup_sync(texture
);
2601 return E_OUTOFMEMORY
;
2604 /* Generate all the surfaces. */
2605 for (i
= 0; i
< texture
->level_count
; ++i
)
2607 for (j
= 0; j
< texture
->layer_count
; ++j
)
2609 struct wined3d_texture_sub_resource
*sub_resource
;
2610 unsigned int idx
= j
* texture
->level_count
+ i
;
2611 struct wined3d_surface
*surface
;
2613 surface
= &surfaces
[idx
];
2614 surface
->container
= texture
;
2615 surface
->texture_level
= i
;
2616 surface
->texture_layer
= j
;
2617 list_init(&surface
->renderbuffers
);
2619 sub_resource
= &texture
->sub_resources
[idx
];
2620 sub_resource
->locations
= WINED3D_LOCATION_DISCARDED
;
2621 sub_resource
->u
.surface
= surface
;
2623 if (FAILED(hr
= device_parent
->ops
->surface_created(device_parent
,
2624 texture
, idx
, &sub_resource
->parent
, &sub_resource
->parent_ops
)))
2626 WARN("Failed to create texture1d parent, hr %#x.\n", hr
);
2627 sub_resource
->parent
= NULL
;
2628 wined3d_texture_cleanup_sync(texture
);
2632 TRACE("parent %p, parent_ops %p.\n", parent
, parent_ops
);
2634 TRACE("Created 1d texture surface level %u, layer %u @ %p.\n", i
, j
, surface
);
2641 static HRESULT
texture_init(struct wined3d_texture
*texture
, const struct wined3d_resource_desc
*desc
,
2642 unsigned int layer_count
, unsigned int level_count
, DWORD flags
, struct wined3d_device
*device
,
2643 void *parent
, const struct wined3d_parent_ops
*parent_ops
)
2645 struct wined3d_device_parent
*device_parent
= device
->device_parent
;
2646 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
2647 struct wined3d_surface
*surfaces
;
2648 UINT pow2_width
, pow2_height
;
2649 unsigned int i
, j
, sub_count
;
2652 if (!(desc
->usage
& WINED3DUSAGE_LEGACY_CUBEMAP
) && layer_count
> 1
2653 && !gl_info
->supported
[EXT_TEXTURE_ARRAY
])
2655 WARN("OpenGL implementation does not support array textures.\n");
2656 return WINED3DERR_INVALIDCALL
;
2659 /* TODO: It should only be possible to create textures for formats
2660 * that are reported as supported. */
2661 if (WINED3DFMT_UNKNOWN
>= desc
->format
)
2663 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture
);
2664 return WINED3DERR_INVALIDCALL
;
2667 if (desc
->usage
& WINED3DUSAGE_DYNAMIC
&& wined3d_resource_access_is_managed(desc
->access
))
2668 FIXME("Trying to create a managed texture with dynamic usage.\n");
2669 if (!(desc
->usage
& (WINED3DUSAGE_DYNAMIC
| WINED3DUSAGE_RENDERTARGET
| WINED3DUSAGE_DEPTHSTENCIL
))
2670 && (flags
& WINED3D_TEXTURE_CREATE_MAPPABLE
))
2671 WARN("Creating a mappable texture that doesn't specify dynamic usage.\n");
2672 if (desc
->usage
& WINED3DUSAGE_RENDERTARGET
&& desc
->access
& WINED3D_RESOURCE_ACCESS_CPU
)
2673 FIXME("Trying to create a CPU accessible render target.\n");
2675 pow2_width
= desc
->width
;
2676 pow2_height
= desc
->height
;
2677 if (((desc
->width
& (desc
->width
- 1)) || (desc
->height
& (desc
->height
- 1)))
2678 && !gl_info
->supported
[ARB_TEXTURE_NON_POWER_OF_TWO
])
2680 /* level_count == 0 returns an error as well. */
2681 if (level_count
!= 1 || layer_count
!= 1)
2683 if (!(desc
->usage
& WINED3DUSAGE_SCRATCH
))
2685 WARN("Attempted to create a mipmapped/cube/array NPOT texture without unconditional NPOT support.\n");
2686 return WINED3DERR_INVALIDCALL
;
2689 WARN("Creating a scratch mipmapped/cube/array NPOT texture despite lack of HW support.\n");
2691 texture
->flags
|= WINED3D_TEXTURE_COND_NP2
;
2693 if (!gl_info
->supported
[ARB_TEXTURE_RECTANGLE
] && !gl_info
->supported
[WINED3D_GL_NORMALIZED_TEXRECT
])
2695 const struct wined3d_format
*format
= wined3d_get_format(gl_info
, desc
->format
, desc
->usage
);
2697 /* TODO: Add support for non-power-of-two compressed textures. */
2698 if (format
->flags
[WINED3D_GL_RES_TYPE_TEX_2D
]
2699 & (WINED3DFMT_FLAG_COMPRESSED
| WINED3DFMT_FLAG_HEIGHT_SCALE
))
2701 FIXME("Compressed or height scaled non-power-of-two (%ux%u) textures are not supported.\n",
2702 desc
->width
, desc
->height
);
2703 return WINED3DERR_NOTAVAILABLE
;
2706 /* Find the nearest pow2 match. */
2707 pow2_width
= pow2_height
= 1;
2708 while (pow2_width
< desc
->width
)
2710 while (pow2_height
< desc
->height
)
2712 texture
->flags
|= WINED3D_TEXTURE_COND_NP2_EMULATED
;
2715 texture
->pow2_width
= pow2_width
;
2716 texture
->pow2_height
= pow2_height
;
2718 if ((pow2_width
> gl_info
->limits
.texture_size
|| pow2_height
> gl_info
->limits
.texture_size
)
2719 && (desc
->usage
& WINED3DUSAGE_TEXTURE
))
2721 /* One of four options:
2722 * 1: Do the same as we do with NPOT and scale the texture. (Any
2723 * texture ops would require the texture to be scaled which is
2724 * potentially slow.)
2725 * 2: Set the texture to the maximum size (bad idea).
2726 * 3: WARN and return WINED3DERR_NOTAVAILABLE.
2727 * 4: Create the surface, but allow it to be used only for DirectDraw
2728 * Blts. Some apps (e.g. Swat 3) create textures with a height of
2729 * 16 and a width > 3000 and blt 16x16 letter areas from them to
2730 * the render target. */
2731 if (desc
->access
& WINED3D_RESOURCE_ACCESS_GPU
)
2733 WARN("Dimensions (%ux%u) exceed the maximum texture size.\n", pow2_width
, pow2_height
);
2734 return WINED3DERR_NOTAVAILABLE
;
2737 /* We should never use this surface in combination with OpenGL. */
2738 TRACE("Creating an oversized (%ux%u) surface.\n", pow2_width
, pow2_height
);
2741 if (FAILED(hr
= wined3d_texture_init(texture
, &texture2d_ops
, layer_count
, level_count
, desc
,
2742 flags
, device
, parent
, parent_ops
, &texture_resource_ops
)))
2744 WARN("Failed to initialize texture, returning %#x.\n", hr
);
2748 /* Precalculated scaling for 'faked' non power of two texture coords. */
2749 if (texture
->resource
.gl_type
== WINED3D_GL_RES_TYPE_TEX_RECT
)
2751 texture
->pow2_matrix
[0] = (float)desc
->width
;
2752 texture
->pow2_matrix
[5] = (float)desc
->height
;
2753 texture
->flags
&= ~(WINED3D_TEXTURE_POW2_MAT_IDENT
| WINED3D_TEXTURE_NORMALIZED_COORDS
);
2754 texture
->target
= GL_TEXTURE_RECTANGLE_ARB
;
2758 if (texture
->flags
& WINED3D_TEXTURE_COND_NP2_EMULATED
)
2760 texture
->pow2_matrix
[0] = (((float)desc
->width
) / ((float)pow2_width
));
2761 texture
->pow2_matrix
[5] = (((float)desc
->height
) / ((float)pow2_height
));
2762 texture
->flags
&= ~WINED3D_TEXTURE_POW2_MAT_IDENT
;
2766 texture
->pow2_matrix
[0] = 1.0f
;
2767 texture
->pow2_matrix
[5] = 1.0f
;
2769 if (desc
->usage
& WINED3DUSAGE_LEGACY_CUBEMAP
)
2771 texture
->target
= GL_TEXTURE_CUBE_MAP_ARB
;
2773 else if (desc
->multisample_type
&& gl_info
->supported
[ARB_TEXTURE_MULTISAMPLE
])
2775 if (layer_count
> 1)
2776 texture
->target
= GL_TEXTURE_2D_MULTISAMPLE_ARRAY
;
2778 texture
->target
= GL_TEXTURE_2D_MULTISAMPLE
;
2782 if (layer_count
> 1)
2783 texture
->target
= GL_TEXTURE_2D_ARRAY
;
2785 texture
->target
= GL_TEXTURE_2D
;
2788 texture
->pow2_matrix
[10] = 1.0f
;
2789 texture
->pow2_matrix
[15] = 1.0f
;
2790 TRACE("x scale %.8e, y scale %.8e.\n", texture
->pow2_matrix
[0], texture
->pow2_matrix
[5]);
2792 if (wined3d_texture_use_pbo(texture
, gl_info
))
2793 texture
->resource
.map_binding
= WINED3D_LOCATION_BUFFER
;
2795 sub_count
= level_count
* layer_count
;
2796 if (sub_count
/ layer_count
!= level_count
2797 || !(surfaces
= heap_calloc(sub_count
, sizeof(*surfaces
))))
2799 wined3d_texture_cleanup_sync(texture
);
2800 return E_OUTOFMEMORY
;
2803 if (desc
->usage
& WINED3DUSAGE_OVERLAY
)
2805 if (!(texture
->overlay_info
= heap_calloc(sub_count
, sizeof(*texture
->overlay_info
))))
2807 heap_free(surfaces
);
2808 wined3d_texture_cleanup_sync(texture
);
2809 return E_OUTOFMEMORY
;
2812 for (i
= 0; i
< sub_count
; ++i
)
2814 list_init(&texture
->overlay_info
[i
].entry
);
2815 list_init(&texture
->overlay_info
[i
].overlays
);
2819 /* Generate all the surfaces. */
2820 for (i
= 0; i
< texture
->level_count
; ++i
)
2822 for (j
= 0; j
< texture
->layer_count
; ++j
)
2824 struct wined3d_texture_sub_resource
*sub_resource
;
2825 unsigned int idx
= j
* texture
->level_count
+ i
;
2826 struct wined3d_surface
*surface
;
2828 surface
= &surfaces
[idx
];
2829 surface
->container
= texture
;
2830 surface
->texture_level
= i
;
2831 surface
->texture_layer
= j
;
2832 list_init(&surface
->renderbuffers
);
2834 sub_resource
= &texture
->sub_resources
[idx
];
2835 sub_resource
->locations
= WINED3D_LOCATION_DISCARDED
;
2836 sub_resource
->u
.surface
= surface
;
2837 if (!(texture
->resource
.usage
& WINED3DUSAGE_DEPTHSTENCIL
))
2839 wined3d_texture_validate_location(texture
, idx
, WINED3D_LOCATION_SYSMEM
);
2840 wined3d_texture_invalidate_location(texture
, idx
, ~WINED3D_LOCATION_SYSMEM
);
2843 if (FAILED(hr
= device_parent
->ops
->surface_created(device_parent
,
2844 texture
, idx
, &sub_resource
->parent
, &sub_resource
->parent_ops
)))
2846 WARN("Failed to create surface parent, hr %#x.\n", hr
);
2847 sub_resource
->parent
= NULL
;
2848 wined3d_texture_cleanup_sync(texture
);
2852 TRACE("parent %p, parent_ops %p.\n", sub_resource
->parent
, sub_resource
->parent_ops
);
2854 TRACE("Created surface level %u, layer %u @ %p.\n", i
, j
, surface
);
2856 if ((desc
->usage
& WINED3DUSAGE_OWNDC
) || (device
->wined3d
->flags
& WINED3D_NO3D
))
2858 wined3d_cs_init_object(device
->cs
, texture2d_create_dc
, surface
);
2859 device
->cs
->ops
->finish(device
->cs
, WINED3D_CS_QUEUE_DEFAULT
);
2862 wined3d_texture_cleanup_sync(texture
);
2863 return WINED3DERR_INVALIDCALL
;
2872 /* This call just uploads data, the caller is responsible for binding the
2873 * correct texture. */
2874 /* Context activation is done by the caller. */
2875 static void texture3d_upload_data(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
,
2876 const struct wined3d_context
*context
, const struct wined3d_box
*box
,
2877 const struct wined3d_const_bo_address
*data
, unsigned int row_pitch
, unsigned int slice_pitch
)
2879 const struct wined3d_format
*format
= texture
->resource
.format
;
2880 unsigned int level
= sub_resource_idx
% texture
->level_count
;
2881 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2882 unsigned int x
, y
, z
, update_w
, update_h
, update_d
;
2883 unsigned int dst_row_pitch
, dst_slice_pitch
;
2884 unsigned int width
, height
, depth
;
2885 const void *mem
= data
->addr
;
2886 void *converted_mem
= NULL
;
2888 TRACE("texture %p, sub_resource_idx %u, context %p, box %s, data {%#x:%p}, row_pitch %#x, slice_pitch %#x.\n",
2889 texture
, sub_resource_idx
, context
, debug_box(box
),
2890 data
->buffer_object
, data
->addr
, row_pitch
, slice_pitch
);
2892 width
= wined3d_texture_get_level_width(texture
, level
);
2893 height
= wined3d_texture_get_level_height(texture
, level
);
2894 depth
= wined3d_texture_get_level_depth(texture
, level
);
2908 update_w
= box
->right
- box
->left
;
2909 update_h
= box
->bottom
- box
->top
;
2910 update_d
= box
->back
- box
->front
;
2913 if (format
->conv_byte_count
)
2915 if (data
->buffer_object
)
2916 ERR("Loading a converted texture from a PBO.\n");
2917 if (texture
->resource
.format_flags
& WINED3DFMT_FLAG_BLOCKS
)
2918 ERR("Converting a block-based format.\n");
2920 dst_row_pitch
= update_w
* format
->conv_byte_count
;
2921 dst_slice_pitch
= dst_row_pitch
* update_h
;
2923 converted_mem
= heap_calloc(update_d
, dst_slice_pitch
);
2924 format
->upload(data
->addr
, converted_mem
, row_pitch
, slice_pitch
,
2925 dst_row_pitch
, dst_slice_pitch
, update_w
, update_h
, update_d
);
2926 mem
= converted_mem
;
2930 wined3d_texture_get_pitch(texture
, sub_resource_idx
, &dst_row_pitch
, &dst_slice_pitch
);
2931 if (row_pitch
!= dst_row_pitch
|| slice_pitch
!= dst_slice_pitch
)
2932 FIXME("Ignoring row/slice pitch (%u/%u).\n", row_pitch
, slice_pitch
);
2935 if (data
->buffer_object
)
2937 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER
, data
->buffer_object
));
2938 checkGLcall("glBindBuffer");
2941 GL_EXTCALL(glTexSubImage3D(GL_TEXTURE_3D
, level
, x
, y
, z
,
2942 update_w
, update_h
, update_d
, format
->glFormat
, format
->glType
, mem
));
2943 checkGLcall("glTexSubImage3D");
2945 if (data
->buffer_object
)
2947 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER
, 0));
2948 checkGLcall("glBindBuffer");
2951 heap_free(converted_mem
);
2954 /* Context activation is done by the caller. */
2955 static void texture3d_download_data(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
,
2956 const struct wined3d_context
*context
, const struct wined3d_bo_address
*data
)
2958 const struct wined3d_format
*format
= texture
->resource
.format
;
2959 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2961 if (format
->conv_byte_count
)
2963 FIXME("Attempting to download a converted volume, format %s.\n",
2964 debug_d3dformat(format
->id
));
2968 if (data
->buffer_object
)
2970 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER
, data
->buffer_object
));
2971 checkGLcall("glBindBuffer");
2974 gl_info
->gl_ops
.gl
.p_glGetTexImage(GL_TEXTURE_3D
, sub_resource_idx
,
2975 format
->glFormat
, format
->glType
, data
->addr
);
2976 checkGLcall("glGetTexImage");
2978 if (data
->buffer_object
)
2980 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER
, 0));
2981 checkGLcall("glBindBuffer");
2986 /* Context activation is done by the caller. */
2987 static void texture3d_srgb_transfer(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
,
2988 struct wined3d_context
*context
, BOOL dest_is_srgb
)
2990 struct wined3d_texture_sub_resource
*sub_resource
= &texture
->sub_resources
[sub_resource_idx
];
2991 unsigned int row_pitch
, slice_pitch
;
2992 struct wined3d_bo_address data
;
2994 /* Optimisations are possible, but the effort should be put into either
2995 * implementing EXT_SRGB_DECODE in the driver or finding out why we
2996 * picked the wrong copy for the original upload and fixing that.
2998 * Also keep in mind that we want to avoid using resource.heap_memory
2999 * for DEFAULT pool surfaces. */
3000 WARN_(d3d_perf
)("Performing slow rgb/srgb volume transfer.\n");
3001 data
.buffer_object
= 0;
3002 if (!(data
.addr
= heap_alloc(sub_resource
->size
)))
3005 wined3d_texture_get_pitch(texture
, sub_resource_idx
, &row_pitch
, &slice_pitch
);
3006 wined3d_texture_bind_and_dirtify(texture
, context
, !dest_is_srgb
);
3007 texture3d_download_data(texture
, sub_resource_idx
, context
, &data
);
3008 wined3d_texture_bind_and_dirtify(texture
, context
, dest_is_srgb
);
3009 texture3d_upload_data(texture
, sub_resource_idx
, context
,
3010 NULL
, wined3d_const_bo_address(&data
), row_pitch
, slice_pitch
);
3012 heap_free(data
.addr
);
3015 /* Context activation is done by the caller. */
3016 static BOOL
texture3d_load_location(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
,
3017 struct wined3d_context
*context
, DWORD location
)
3019 struct wined3d_texture_sub_resource
*sub_resource
= &texture
->sub_resources
[sub_resource_idx
];
3020 unsigned int row_pitch
, slice_pitch
;
3022 if (!wined3d_texture_prepare_location(texture
, sub_resource_idx
, context
, location
))
3027 case WINED3D_LOCATION_TEXTURE_RGB
:
3028 case WINED3D_LOCATION_TEXTURE_SRGB
:
3029 if (sub_resource
->locations
& WINED3D_LOCATION_SYSMEM
)
3031 struct wined3d_const_bo_address data
= {0, texture
->resource
.heap_memory
};
3032 data
.addr
+= sub_resource
->offset
;
3033 wined3d_texture_bind_and_dirtify(texture
, context
,
3034 location
== WINED3D_LOCATION_TEXTURE_SRGB
);
3035 wined3d_texture_get_pitch(texture
, sub_resource_idx
, &row_pitch
, &slice_pitch
);
3036 texture3d_upload_data(texture
, sub_resource_idx
, context
, NULL
, &data
, row_pitch
, slice_pitch
);
3038 else if (sub_resource
->locations
& WINED3D_LOCATION_BUFFER
)
3040 #if !defined(STAGING_CSMT)
3041 struct wined3d_const_bo_address data
= {sub_resource
->buffer_object
, NULL
};
3042 #else /* STAGING_CSMT */
3043 struct wined3d_const_bo_address data
= {sub_resource
->buffer
->name
, NULL
};
3044 #endif /* STAGING_CSMT */
3045 wined3d_texture_bind_and_dirtify(texture
, context
,
3046 location
== WINED3D_LOCATION_TEXTURE_SRGB
);
3047 wined3d_texture_get_pitch(texture
, sub_resource_idx
, &row_pitch
, &slice_pitch
);
3048 texture3d_upload_data(texture
, sub_resource_idx
, context
, NULL
, &data
, row_pitch
, slice_pitch
);
3050 else if (sub_resource
->locations
& WINED3D_LOCATION_TEXTURE_RGB
)
3052 texture3d_srgb_transfer(texture
, sub_resource_idx
, context
, TRUE
);
3054 else if (sub_resource
->locations
& WINED3D_LOCATION_TEXTURE_SRGB
)
3056 texture3d_srgb_transfer(texture
, sub_resource_idx
, context
, FALSE
);
3060 FIXME("Implement texture loading from %s.\n", wined3d_debug_location(sub_resource
->locations
));
3065 case WINED3D_LOCATION_SYSMEM
:
3066 if (sub_resource
->locations
& (WINED3D_LOCATION_TEXTURE_RGB
| WINED3D_LOCATION_TEXTURE_SRGB
))
3068 struct wined3d_bo_address data
= {0, texture
->resource
.heap_memory
};
3070 data
.addr
+= sub_resource
->offset
;
3071 if (sub_resource
->locations
& WINED3D_LOCATION_TEXTURE_RGB
)
3072 wined3d_texture_bind_and_dirtify(texture
, context
, FALSE
);
3074 wined3d_texture_bind_and_dirtify(texture
, context
, TRUE
);
3076 texture3d_download_data(texture
, sub_resource_idx
, context
, &data
);
3077 ++texture
->download_count
;
3081 FIXME("Implement WINED3D_LOCATION_SYSMEM loading from %s.\n",
3082 wined3d_debug_location(sub_resource
->locations
));
3087 case WINED3D_LOCATION_BUFFER
:
3088 if (sub_resource
->locations
& (WINED3D_LOCATION_TEXTURE_RGB
| WINED3D_LOCATION_TEXTURE_SRGB
))
3090 #if !defined(STAGING_CSMT)
3091 struct wined3d_bo_address data
= {sub_resource
->buffer_object
, NULL
};
3092 #else /* STAGING_CSMT */
3093 struct wined3d_bo_address data
= {sub_resource
->buffer
->name
, NULL
};
3094 #endif /* STAGING_CSMT */
3096 if (sub_resource
->locations
& WINED3D_LOCATION_TEXTURE_RGB
)
3097 wined3d_texture_bind_and_dirtify(texture
, context
, FALSE
);
3099 wined3d_texture_bind_and_dirtify(texture
, context
, TRUE
);
3101 texture3d_download_data(texture
, sub_resource_idx
, context
, &data
);
3105 FIXME("Implement WINED3D_LOCATION_BUFFER loading from %s.\n",
3106 wined3d_debug_location(sub_resource
->locations
));
3112 FIXME("Implement %s loading from %s.\n", wined3d_debug_location(location
),
3113 wined3d_debug_location(sub_resource
->locations
));
3120 static void texture3d_prepare_texture(struct wined3d_texture
*texture
, struct wined3d_context
*context
, BOOL srgb
)
3122 const struct wined3d_format
*format
= texture
->resource
.format
;
3123 GLenum internal
= srgb
? format
->glGammaInternal
: format
->glInternal
;
3124 unsigned int sub_count
= texture
->level_count
* texture
->layer_count
;
3125 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
3128 wined3d_texture_bind_and_dirtify(texture
, context
, srgb
);
3130 if (wined3d_texture_use_immutable_storage(texture
, gl_info
))
3132 GL_EXTCALL(glTexStorage3D(GL_TEXTURE_3D
, texture
->level_count
, internal
,
3133 wined3d_texture_get_level_width(texture
, 0),
3134 wined3d_texture_get_level_height(texture
, 0),
3135 wined3d_texture_get_level_depth(texture
, 0)));
3136 checkGLcall("glTexStorage3D");
3140 for (i
= 0; i
< sub_count
; ++i
)
3142 GL_EXTCALL(glTexImage3D(GL_TEXTURE_3D
, i
, internal
,
3143 wined3d_texture_get_level_width(texture
, i
),
3144 wined3d_texture_get_level_height(texture
, i
),
3145 wined3d_texture_get_level_depth(texture
, i
),
3146 0, format
->glFormat
, format
->glType
, NULL
));
3147 checkGLcall("glTexImage3D");
3152 static void texture3d_cleanup_sub_resources(struct wined3d_texture
*texture
)
3156 static const struct wined3d_texture_ops texture3d_ops
=
3158 texture3d_upload_data
,
3159 texture3d_load_location
,
3160 texture3d_prepare_texture
,
3161 texture3d_cleanup_sub_resources
,
3164 static HRESULT
volumetexture_init(struct wined3d_texture
*texture
, const struct wined3d_resource_desc
*desc
,
3165 UINT layer_count
, UINT level_count
, DWORD flags
, struct wined3d_device
*device
, void *parent
,
3166 const struct wined3d_parent_ops
*parent_ops
)
3168 struct wined3d_device_parent
*device_parent
= device
->device_parent
;
3169 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
3173 if (layer_count
!= 1)
3175 ERR("Invalid layer count for volume texture.\n");
3176 return E_INVALIDARG
;
3179 /* TODO: It should only be possible to create textures for formats
3180 * that are reported as supported. */
3181 if (WINED3DFMT_UNKNOWN
>= desc
->format
)
3183 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture
);
3184 return WINED3DERR_INVALIDCALL
;
3187 if (!gl_info
->supported
[EXT_TEXTURE3D
])
3189 WARN("(%p) : Texture cannot be created - no volume texture support.\n", texture
);
3190 return WINED3DERR_INVALIDCALL
;
3193 if (desc
->usage
& WINED3DUSAGE_DYNAMIC
&& (wined3d_resource_access_is_managed(desc
->access
)
3194 || desc
->usage
& WINED3DUSAGE_SCRATCH
))
3196 WARN("Attempted to create a DYNAMIC texture with access %s.\n",
3197 wined3d_debug_resource_access(desc
->access
));
3198 return WINED3DERR_INVALIDCALL
;
3201 if (!gl_info
->supported
[ARB_TEXTURE_NON_POWER_OF_TWO
])
3203 if (!is_power_of_two(desc
->width
) || !is_power_of_two(desc
->height
) || !is_power_of_two(desc
->depth
))
3205 if (desc
->usage
& WINED3DUSAGE_SCRATCH
)
3207 WARN("Creating a scratch NPOT volume texture despite lack of HW support.\n");
3211 WARN("Attempted to create a NPOT volume texture (%u, %u, %u) without GL support.\n",
3212 desc
->width
, desc
->height
, desc
->depth
);
3213 return WINED3DERR_INVALIDCALL
;
3218 if (FAILED(hr
= wined3d_texture_init(texture
, &texture3d_ops
, 1, level_count
, desc
,
3219 flags
, device
, parent
, parent_ops
, &texture_resource_ops
)))
3221 WARN("Failed to initialize texture, returning %#x.\n", hr
);
3225 texture
->pow2_matrix
[0] = 1.0f
;
3226 texture
->pow2_matrix
[5] = 1.0f
;
3227 texture
->pow2_matrix
[10] = 1.0f
;
3228 texture
->pow2_matrix
[15] = 1.0f
;
3229 texture
->target
= GL_TEXTURE_3D
;
3231 if (wined3d_texture_use_pbo(texture
, gl_info
))
3233 wined3d_resource_free_sysmem(&texture
->resource
);
3234 texture
->resource
.map_binding
= WINED3D_LOCATION_BUFFER
;
3237 /* Generate all the sub resources. */
3238 for (i
= 0; i
< texture
->level_count
; ++i
)
3240 struct wined3d_texture_sub_resource
*sub_resource
;
3242 sub_resource
= &texture
->sub_resources
[i
];
3243 sub_resource
->locations
= WINED3D_LOCATION_DISCARDED
;
3245 if (FAILED(hr
= device_parent
->ops
->volume_created(device_parent
,
3246 texture
, i
, &sub_resource
->parent
, &sub_resource
->parent_ops
)))
3248 WARN("Failed to create volume parent, hr %#x.\n", hr
);
3249 sub_resource
->parent
= NULL
;
3250 wined3d_texture_cleanup_sync(texture
);
3254 TRACE("parent %p, parent_ops %p.\n", parent
, parent_ops
);
3256 TRACE("Created volume level %u.\n", i
);
3262 HRESULT CDECL
wined3d_texture_blt(struct wined3d_texture
*dst_texture
, unsigned int dst_sub_resource_idx
,
3263 const RECT
*dst_rect
, struct wined3d_texture
*src_texture
, unsigned int src_sub_resource_idx
,
3264 const RECT
*src_rect
, DWORD flags
, const struct wined3d_blt_fx
*fx
, enum wined3d_texture_filter_type filter
)
3266 struct wined3d_box src_box
= {src_rect
->left
, src_rect
->top
, src_rect
->right
, src_rect
->bottom
, 0, 1};
3267 struct wined3d_box dst_box
= {dst_rect
->left
, dst_rect
->top
, dst_rect
->right
, dst_rect
->bottom
, 0, 1};
3268 unsigned int dst_format_flags
, src_format_flags
= 0;
3271 TRACE("dst_texture %p, dst_sub_resource_idx %u, dst_rect %s, src_texture %p, "
3272 "src_sub_resource_idx %u, src_rect %s, flags %#x, fx %p, filter %s.\n",
3273 dst_texture
, dst_sub_resource_idx
, wine_dbgstr_rect(dst_rect
), src_texture
,
3274 src_sub_resource_idx
, wine_dbgstr_rect(src_rect
), flags
, fx
, debug_d3dtexturefiltertype(filter
));
3276 if (dst_sub_resource_idx
>= dst_texture
->level_count
* dst_texture
->layer_count
3277 || dst_texture
->resource
.type
!= WINED3D_RTYPE_TEXTURE_2D
)
3278 return WINED3DERR_INVALIDCALL
;
3280 if (src_sub_resource_idx
>= src_texture
->level_count
* src_texture
->layer_count
3281 || src_texture
->resource
.type
!= WINED3D_RTYPE_TEXTURE_2D
)
3282 return WINED3DERR_INVALIDCALL
;
3284 dst_format_flags
= dst_texture
->resource
.format_flags
;
3285 if (FAILED(hr
= wined3d_texture_check_box_dimensions(dst_texture
,
3286 dst_sub_resource_idx
% dst_texture
->level_count
, &dst_box
)))
3289 src_format_flags
= src_texture
->resource
.format_flags
;
3290 if (FAILED(hr
= wined3d_texture_check_box_dimensions(src_texture
,
3291 src_sub_resource_idx
% src_texture
->level_count
, &src_box
)))
3294 if (dst_texture
->sub_resources
[dst_sub_resource_idx
].map_count
3295 || src_texture
->sub_resources
[src_sub_resource_idx
].map_count
)
3297 #if !defined(STAGING_CSMT)
3298 WARN("Sub-resource is busy, returning WINEDDERR_SURFACEBUSY.\n");
3299 return WINEDDERR_SURFACEBUSY
;
3300 #else /* STAGING_CSMT */
3301 struct wined3d_device
*device
= dst_texture
->resource
.device
;
3302 device
->cs
->ops
->finish(device
->cs
, WINED3D_CS_QUEUE_DEFAULT
);
3303 if (dst_texture
->sub_resources
[dst_sub_resource_idx
].map_count
3304 || (src_texture
&& src_texture
->sub_resources
[src_sub_resource_idx
].map_count
))
3306 WARN("Sub-resource is busy, returning WINEDDERR_SURFACEBUSY.\n");
3307 return WINEDDERR_SURFACEBUSY
;
3309 #endif /* STAGING_CSMT */
3312 if ((src_format_flags
& (WINED3DFMT_FLAG_DEPTH
| WINED3DFMT_FLAG_STENCIL
))
3313 != (dst_format_flags
& (WINED3DFMT_FLAG_DEPTH
| WINED3DFMT_FLAG_STENCIL
)))
3315 WARN("Rejecting depth/stencil blit between incompatible formats.\n");
3316 return WINED3DERR_INVALIDCALL
;
3319 wined3d_cs_emit_blt_sub_resource(dst_texture
->resource
.device
->cs
, &dst_texture
->resource
, dst_sub_resource_idx
,
3320 &dst_box
, &src_texture
->resource
, src_sub_resource_idx
, &src_box
, flags
, fx
, filter
);
3325 HRESULT CDECL
wined3d_texture_get_overlay_position(const struct wined3d_texture
*texture
,
3326 unsigned int sub_resource_idx
, LONG
*x
, LONG
*y
)
3328 struct wined3d_overlay_info
*overlay
;
3330 TRACE("texture %p, sub_resource_idx %u, x %p, y %p.\n", texture
, sub_resource_idx
, x
, y
);
3332 if (!(texture
->resource
.usage
& WINED3DUSAGE_OVERLAY
)
3333 || sub_resource_idx
>= texture
->level_count
* texture
->layer_count
)
3335 WARN("Invalid sub-resource specified.\n");
3336 return WINEDDERR_NOTAOVERLAYSURFACE
;
3339 overlay
= &texture
->overlay_info
[sub_resource_idx
];
3342 TRACE("Overlay not visible.\n");
3345 return WINEDDERR_OVERLAYNOTVISIBLE
;
3348 *x
= overlay
->dst_rect
.left
;
3349 *y
= overlay
->dst_rect
.top
;
3351 TRACE("Returning position %d, %d.\n", *x
, *y
);
3356 HRESULT CDECL
wined3d_texture_set_overlay_position(struct wined3d_texture
*texture
,
3357 unsigned int sub_resource_idx
, LONG x
, LONG y
)
3359 struct wined3d_overlay_info
*overlay
;
3362 TRACE("texture %p, sub_resource_idx %u, x %d, y %d.\n", texture
, sub_resource_idx
, x
, y
);
3364 if (!(texture
->resource
.usage
& WINED3DUSAGE_OVERLAY
)
3365 || sub_resource_idx
>= texture
->level_count
* texture
->layer_count
)
3367 WARN("Invalid sub-resource specified.\n");
3368 return WINEDDERR_NOTAOVERLAYSURFACE
;
3371 overlay
= &texture
->overlay_info
[sub_resource_idx
];
3372 w
= overlay
->dst_rect
.right
- overlay
->dst_rect
.left
;
3373 h
= overlay
->dst_rect
.bottom
- overlay
->dst_rect
.top
;
3374 SetRect(&overlay
->dst_rect
, x
, y
, x
+ w
, y
+ h
);
3379 HRESULT CDECL
wined3d_texture_update_overlay(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
,
3380 const RECT
*src_rect
, struct wined3d_texture
*dst_texture
, unsigned int dst_sub_resource_idx
,
3381 const RECT
*dst_rect
, DWORD flags
)
3383 struct wined3d_texture_sub_resource
*sub_resource
, *dst_sub_resource
;
3384 struct wined3d_surface
*surface
, *dst_surface
;
3385 struct wined3d_overlay_info
*overlay
;
3387 TRACE("texture %p, sub_resource_idx %u, src_rect %s, dst_texture %p, "
3388 "dst_sub_resource_idx %u, dst_rect %s, flags %#x.\n",
3389 texture
, sub_resource_idx
, wine_dbgstr_rect(src_rect
), dst_texture
,
3390 dst_sub_resource_idx
, wine_dbgstr_rect(dst_rect
), flags
);
3392 if (!(texture
->resource
.usage
& WINED3DUSAGE_OVERLAY
) || texture
->resource
.type
!= WINED3D_RTYPE_TEXTURE_2D
3393 || !(sub_resource
= wined3d_texture_get_sub_resource(texture
, sub_resource_idx
)))
3395 WARN("Invalid sub-resource specified.\n");
3396 return WINEDDERR_NOTAOVERLAYSURFACE
;
3399 if (!dst_texture
|| dst_texture
->resource
.type
!= WINED3D_RTYPE_TEXTURE_2D
3400 || !(dst_sub_resource
= wined3d_texture_get_sub_resource(dst_texture
, dst_sub_resource_idx
)))
3402 WARN("Invalid destination sub-resource specified.\n");
3403 return WINED3DERR_INVALIDCALL
;
3406 overlay
= &texture
->overlay_info
[sub_resource_idx
];
3408 surface
= sub_resource
->u
.surface
;
3410 overlay
->src_rect
= *src_rect
;
3412 SetRect(&overlay
->src_rect
, 0, 0,
3413 wined3d_texture_get_level_width(texture
, surface
->texture_level
),
3414 wined3d_texture_get_level_height(texture
, surface
->texture_level
));
3416 dst_surface
= dst_sub_resource
->u
.surface
;
3418 overlay
->dst_rect
= *dst_rect
;
3420 SetRect(&overlay
->dst_rect
, 0, 0,
3421 wined3d_texture_get_level_width(dst_texture
, dst_surface
->texture_level
),
3422 wined3d_texture_get_level_height(dst_texture
, dst_surface
->texture_level
));
3424 if (overlay
->dst
&& (overlay
->dst
!= dst_surface
|| flags
& WINEDDOVER_HIDE
))
3426 overlay
->dst
= NULL
;
3427 list_remove(&overlay
->entry
);
3430 if (flags
& WINEDDOVER_SHOW
)
3432 if (overlay
->dst
!= dst_surface
)
3434 overlay
->dst
= dst_surface
;
3435 list_add_tail(&texture
->overlay_info
[dst_sub_resource_idx
].overlays
, &overlay
->entry
);
3438 else if (flags
& WINEDDOVER_HIDE
)
3440 /* Tests show that the rectangles are erased on hide. */
3441 SetRectEmpty(&overlay
->src_rect
);
3442 SetRectEmpty(&overlay
->dst_rect
);
3443 overlay
->dst
= NULL
;
3449 void * CDECL
wined3d_texture_get_sub_resource_parent(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
)
3451 unsigned int sub_count
= texture
->level_count
* texture
->layer_count
;
3453 TRACE("texture %p, sub_resource_idx %u.\n", texture
, sub_resource_idx
);
3455 if (sub_resource_idx
>= sub_count
)
3457 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx
, sub_count
);
3461 return texture
->sub_resources
[sub_resource_idx
].parent
;
3464 void CDECL
wined3d_texture_set_sub_resource_parent(struct wined3d_texture
*texture
,
3465 unsigned int sub_resource_idx
, void *parent
)
3467 unsigned int sub_count
= texture
->level_count
* texture
->layer_count
;
3469 TRACE("texture %p, sub_resource_idx %u, parent %p.\n", texture
, sub_resource_idx
, parent
);
3471 if (sub_resource_idx
>= sub_count
)
3473 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx
, sub_count
);
3477 texture
->sub_resources
[sub_resource_idx
].parent
= parent
;
3480 HRESULT CDECL
wined3d_texture_get_sub_resource_desc(const struct wined3d_texture
*texture
,
3481 unsigned int sub_resource_idx
, struct wined3d_sub_resource_desc
*desc
)
3483 unsigned int sub_count
= texture
->level_count
* texture
->layer_count
;
3484 const struct wined3d_resource
*resource
;
3485 unsigned int level_idx
;
3487 TRACE("texture %p, sub_resource_idx %u, desc %p.\n", texture
, sub_resource_idx
, desc
);
3489 if (sub_resource_idx
>= sub_count
)
3491 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx
, sub_count
);
3492 return WINED3DERR_INVALIDCALL
;
3495 resource
= &texture
->resource
;
3496 desc
->format
= resource
->format
->id
;
3497 desc
->multisample_type
= resource
->multisample_type
;
3498 desc
->multisample_quality
= resource
->multisample_quality
;
3499 desc
->usage
= resource
->usage
;
3500 desc
->access
= resource
->access
;
3502 level_idx
= sub_resource_idx
% texture
->level_count
;
3503 desc
->width
= wined3d_texture_get_level_width(texture
, level_idx
);
3504 desc
->height
= wined3d_texture_get_level_height(texture
, level_idx
);
3505 desc
->depth
= wined3d_texture_get_level_depth(texture
, level_idx
);
3506 desc
->size
= texture
->sub_resources
[sub_resource_idx
].size
;
3511 HRESULT CDECL
wined3d_texture_create(struct wined3d_device
*device
, const struct wined3d_resource_desc
*desc
,
3512 UINT layer_count
, UINT level_count
, DWORD flags
, const struct wined3d_sub_resource_data
*data
,
3513 void *parent
, const struct wined3d_parent_ops
*parent_ops
, struct wined3d_texture
**texture
)
3515 struct wined3d_texture
*object
;
3518 TRACE("device %p, desc %p, layer_count %u, level_count %u, flags %#x, data %p, "
3519 "parent %p, parent_ops %p, texture %p.\n",
3520 device
, desc
, layer_count
, level_count
, flags
, data
, parent
, parent_ops
, texture
);
3524 WARN("Invalid layer count.\n");
3525 return E_INVALIDARG
;
3527 if ((desc
->usage
& WINED3DUSAGE_LEGACY_CUBEMAP
) && layer_count
!= 6)
3529 ERR("Invalid layer count %u for legacy cubemap.\n", layer_count
);
3535 WARN("Invalid level count.\n");
3536 return WINED3DERR_INVALIDCALL
;
3539 if (desc
->multisample_type
!= WINED3D_MULTISAMPLE_NONE
)
3541 const struct wined3d_format
*format
= wined3d_get_format(&device
->adapter
->gl_info
,
3542 desc
->format
, desc
->usage
);
3544 if (desc
->multisample_type
== WINED3D_MULTISAMPLE_NON_MASKABLE
3545 && desc
->multisample_quality
>= wined3d_popcount(format
->multisample_types
))
3547 WARN("Unsupported quality level %u requested for WINED3D_MULTISAMPLE_NON_MASKABLE.\n",
3548 desc
->multisample_quality
);
3549 return WINED3DERR_NOTAVAILABLE
;
3551 if (desc
->multisample_type
!= WINED3D_MULTISAMPLE_NON_MASKABLE
3552 && (!(format
->multisample_types
& 1u << (desc
->multisample_type
- 1))
3553 || desc
->multisample_quality
))
3555 WARN("Unsupported multisample type %u quality %u requested.\n", desc
->multisample_type
,
3556 desc
->multisample_quality
);
3557 return WINED3DERR_NOTAVAILABLE
;
3561 if (!(object
= heap_alloc_zero(FIELD_OFFSET(struct wined3d_texture
,
3562 sub_resources
[level_count
* layer_count
]))))
3563 return E_OUTOFMEMORY
;
3565 switch (desc
->resource_type
)
3567 case WINED3D_RTYPE_TEXTURE_1D
:
3568 hr
= texture1d_init(object
, desc
, layer_count
, level_count
, device
, parent
, parent_ops
);
3571 case WINED3D_RTYPE_TEXTURE_2D
:
3572 hr
= texture_init(object
, desc
, layer_count
, level_count
, flags
, device
, parent
, parent_ops
);
3575 case WINED3D_RTYPE_TEXTURE_3D
:
3576 hr
= volumetexture_init(object
, desc
, layer_count
, level_count
, flags
, device
, parent
, parent_ops
);
3580 ERR("Invalid resource type %s.\n", debug_d3dresourcetype(desc
->resource_type
));
3581 hr
= WINED3DERR_INVALIDCALL
;
3587 WARN("Failed to initialize texture, returning %#x.\n", hr
);
3592 /* FIXME: We'd like to avoid ever allocating system memory for the texture
3596 unsigned int sub_count
= level_count
* layer_count
;
3599 for (i
= 0; i
< sub_count
; ++i
)
3603 WARN("Invalid sub-resource data specified for sub-resource %u.\n", i
);
3604 wined3d_texture_cleanup_sync(object
);
3606 return E_INVALIDARG
;
3610 for (i
= 0; i
< sub_count
; ++i
)
3612 wined3d_device_update_sub_resource(device
, &object
->resource
,
3613 i
, NULL
, data
[i
].data
, data
[i
].row_pitch
, data
[i
].slice_pitch
);
3617 TRACE("Created texture %p.\n", object
);
3623 HRESULT CDECL
wined3d_texture_get_dc(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
, HDC
*dc
)
3625 struct wined3d_device
*device
= texture
->resource
.device
;
3626 struct wined3d_texture_sub_resource
*sub_resource
;
3627 struct wined3d_surface
*surface
;
3629 TRACE("texture %p, sub_resource_idx %u, dc %p.\n", texture
, sub_resource_idx
, dc
);
3631 if (!(texture
->flags
& WINED3D_TEXTURE_GET_DC
))
3633 WARN("Texture does not support GetDC\n");
3634 /* Don't touch the DC */
3635 return WINED3DERR_INVALIDCALL
;
3638 if (!(sub_resource
= wined3d_texture_get_sub_resource(texture
, sub_resource_idx
)))
3639 return WINED3DERR_INVALIDCALL
;
3641 if (texture
->resource
.type
!= WINED3D_RTYPE_TEXTURE_2D
)
3643 WARN("Not supported on %s resources.\n", debug_d3dresourcetype(texture
->resource
.type
));
3644 return WINED3DERR_INVALIDCALL
;
3647 surface
= sub_resource
->u
.surface
;
3649 if (texture
->resource
.map_count
&& !(texture
->flags
& WINED3D_TEXTURE_GET_DC_LENIENT
))
3650 return WINED3DERR_INVALIDCALL
;
3654 wined3d_cs_init_object(device
->cs
, texture2d_create_dc
, surface
);
3655 device
->cs
->ops
->finish(device
->cs
, WINED3D_CS_QUEUE_DEFAULT
);
3658 return WINED3DERR_INVALIDCALL
;
3660 if (!(texture
->flags
& WINED3D_TEXTURE_GET_DC_LENIENT
))
3661 texture
->flags
|= WINED3D_TEXTURE_DC_IN_USE
;
3662 ++texture
->resource
.map_count
;
3663 ++sub_resource
->map_count
;
3666 TRACE("Returning dc %p.\n", *dc
);
3671 HRESULT CDECL
wined3d_texture_release_dc(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
, HDC dc
)
3673 struct wined3d_device
*device
= texture
->resource
.device
;
3674 struct wined3d_texture_sub_resource
*sub_resource
;
3675 struct wined3d_surface
*surface
;
3677 TRACE("texture %p, sub_resource_idx %u, dc %p.\n", texture
, sub_resource_idx
, dc
);
3679 if (!(sub_resource
= wined3d_texture_get_sub_resource(texture
, sub_resource_idx
)))
3680 return WINED3DERR_INVALIDCALL
;
3682 if (texture
->resource
.type
!= WINED3D_RTYPE_TEXTURE_2D
)
3684 WARN("Not supported on %s resources.\n", debug_d3dresourcetype(texture
->resource
.type
));
3685 return WINED3DERR_INVALIDCALL
;
3688 surface
= sub_resource
->u
.surface
;
3690 if (!(texture
->flags
& (WINED3D_TEXTURE_GET_DC_LENIENT
| WINED3D_TEXTURE_DC_IN_USE
)))
3691 return WINED3DERR_INVALIDCALL
;
3693 if (surface
->dc
!= dc
)
3695 WARN("Application tries to release invalid DC %p, surface DC is %p.\n", dc
, surface
->dc
);
3696 return WINED3DERR_INVALIDCALL
;
3699 if (!(texture
->resource
.usage
& WINED3DUSAGE_OWNDC
) && !(device
->wined3d
->flags
& WINED3D_NO3D
))
3701 wined3d_cs_destroy_object(device
->cs
, texture2d_destroy_dc
, surface
);
3702 device
->cs
->ops
->finish(device
->cs
, WINED3D_CS_QUEUE_DEFAULT
);
3705 --sub_resource
->map_count
;
3706 if (!--texture
->resource
.map_count
&& texture
->update_map_binding
)
3707 wined3d_texture_update_map_binding(texture
);
3708 if (!(texture
->flags
& WINED3D_TEXTURE_GET_DC_LENIENT
))
3709 texture
->flags
&= ~WINED3D_TEXTURE_DC_IN_USE
;