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 /* Front buffer coordinates are always full screen coordinates, but our GL
52 * drawable is limited to the window's client area. The sysmem and texture
53 * copies do have the full screen size. Note that GL has a bottom-left
54 * origin, while D3D has a top-left origin. */
55 void wined3d_texture_translate_drawable_coords(const struct wined3d_texture
*texture
, HWND window
, RECT
*rect
)
57 unsigned int drawable_height
;
58 POINT offset
= {0, 0};
61 if (!texture
->swapchain
)
64 if (texture
== texture
->swapchain
->front_buffer
)
66 ScreenToClient(window
, &offset
);
67 OffsetRect(rect
, offset
.x
, offset
.y
);
70 GetClientRect(window
, &windowsize
);
71 drawable_height
= windowsize
.bottom
- windowsize
.top
;
73 rect
->top
= drawable_height
- rect
->top
;
74 rect
->bottom
= drawable_height
- rect
->bottom
;
77 GLenum
wined3d_texture_get_gl_buffer(const struct wined3d_texture
*texture
)
79 const struct wined3d_swapchain
*swapchain
= texture
->swapchain
;
81 TRACE("texture %p.\n", texture
);
85 ERR("Texture %p is not part of a swapchain.\n", texture
);
89 if (texture
== swapchain
->front_buffer
)
91 TRACE("Returning GL_FRONT.\n");
95 if (texture
== swapchain
->back_buffers
[0])
97 TRACE("Returning GL_BACK.\n");
101 FIXME("Higher back buffer, returning GL_BACK.\n");
105 static DWORD
wined3d_resource_access_from_location(DWORD location
)
109 case WINED3D_LOCATION_DISCARDED
:
112 case WINED3D_LOCATION_SYSMEM
:
113 case WINED3D_LOCATION_USER_MEMORY
:
114 return WINED3D_RESOURCE_ACCESS_CPU
;
116 case WINED3D_LOCATION_BUFFER
:
117 case WINED3D_LOCATION_DRAWABLE
:
118 case WINED3D_LOCATION_TEXTURE_RGB
:
119 case WINED3D_LOCATION_TEXTURE_SRGB
:
120 case WINED3D_LOCATION_RB_MULTISAMPLE
:
121 case WINED3D_LOCATION_RB_RESOLVED
:
122 return WINED3D_RESOURCE_ACCESS_GPU
;
125 FIXME("Unhandled location %#x.\n", location
);
130 static void wined3d_texture_evict_sysmem(struct wined3d_texture
*texture
)
132 struct wined3d_texture_sub_resource
*sub_resource
;
133 unsigned int i
, sub_count
;
135 if (texture
->flags
& (WINED3D_TEXTURE_CONVERTED
| WINED3D_TEXTURE_PIN_SYSMEM
)
136 || texture
->download_count
> WINED3D_TEXTURE_DYNAMIC_MAP_THRESHOLD
)
138 TRACE("Not evicting system memory for texture %p.\n", texture
);
142 TRACE("Evicting system memory for texture %p.\n", texture
);
144 sub_count
= texture
->level_count
* texture
->layer_count
;
145 for (i
= 0; i
< sub_count
; ++i
)
147 sub_resource
= &texture
->sub_resources
[i
];
148 if (sub_resource
->locations
== WINED3D_LOCATION_SYSMEM
)
149 ERR("WINED3D_LOCATION_SYSMEM is the only location for sub-resource %u of texture %p.\n",
151 sub_resource
->locations
&= ~WINED3D_LOCATION_SYSMEM
;
153 wined3d_resource_free_sysmem(&texture
->resource
);
156 void wined3d_texture_validate_location(struct wined3d_texture
*texture
,
157 unsigned int sub_resource_idx
, DWORD location
)
159 struct wined3d_texture_sub_resource
*sub_resource
;
160 DWORD previous_locations
;
162 TRACE("texture %p, sub_resource_idx %u, location %s.\n",
163 texture
, sub_resource_idx
, wined3d_debug_location(location
));
165 sub_resource
= &texture
->sub_resources
[sub_resource_idx
];
166 previous_locations
= sub_resource
->locations
;
167 sub_resource
->locations
|= location
;
168 if (previous_locations
== WINED3D_LOCATION_SYSMEM
&& location
!= WINED3D_LOCATION_SYSMEM
169 && !--texture
->sysmem_count
)
170 wined3d_texture_evict_sysmem(texture
);
172 TRACE("New locations flags are %s.\n", wined3d_debug_location(sub_resource
->locations
));
175 static void wined3d_texture_set_dirty(struct wined3d_texture
*texture
)
177 texture
->flags
&= ~(WINED3D_TEXTURE_RGB_VALID
| WINED3D_TEXTURE_SRGB_VALID
);
180 void wined3d_texture_invalidate_location(struct wined3d_texture
*texture
,
181 unsigned int sub_resource_idx
, DWORD location
)
183 struct wined3d_texture_sub_resource
*sub_resource
;
184 DWORD previous_locations
;
186 TRACE("texture %p, sub_resource_idx %u, location %s.\n",
187 texture
, sub_resource_idx
, wined3d_debug_location(location
));
189 if (location
& (WINED3D_LOCATION_TEXTURE_RGB
| WINED3D_LOCATION_TEXTURE_SRGB
))
190 wined3d_texture_set_dirty(texture
);
192 sub_resource
= &texture
->sub_resources
[sub_resource_idx
];
193 previous_locations
= sub_resource
->locations
;
194 sub_resource
->locations
&= ~location
;
195 if (previous_locations
!= WINED3D_LOCATION_SYSMEM
&& sub_resource
->locations
== WINED3D_LOCATION_SYSMEM
)
196 ++texture
->sysmem_count
;
198 TRACE("New locations flags are %s.\n", wined3d_debug_location(sub_resource
->locations
));
200 if (!sub_resource
->locations
)
201 ERR("Sub-resource %u of texture %p does not have any up to date location.\n",
202 sub_resource_idx
, texture
);
205 static BOOL
wined3d_texture_copy_sysmem_location(struct wined3d_texture
*texture
,
206 unsigned int sub_resource_idx
, struct wined3d_context
*context
, DWORD location
)
208 unsigned int size
= texture
->sub_resources
[sub_resource_idx
].size
;
209 struct wined3d_device
*device
= texture
->resource
.device
;
210 const struct wined3d_gl_info
*gl_info
;
211 struct wined3d_bo_address dst
, src
;
213 if (!wined3d_texture_prepare_location(texture
, sub_resource_idx
, context
, location
))
216 wined3d_texture_get_memory(texture
, sub_resource_idx
, &dst
, location
);
217 wined3d_texture_get_memory(texture
, sub_resource_idx
, &src
,
218 texture
->sub_resources
[sub_resource_idx
].locations
);
220 if (dst
.buffer_object
)
222 context
= context_acquire(device
, NULL
, 0);
223 gl_info
= context
->gl_info
;
224 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER
, dst
.buffer_object
));
225 GL_EXTCALL(glBufferSubData(GL_PIXEL_UNPACK_BUFFER
, 0, size
, src
.addr
));
226 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER
, 0));
227 checkGLcall("PBO upload");
228 context_release(context
);
232 if (src
.buffer_object
)
234 context
= context_acquire(device
, NULL
, 0);
235 gl_info
= context
->gl_info
;
236 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER
, src
.buffer_object
));
237 GL_EXTCALL(glGetBufferSubData(GL_PIXEL_PACK_BUFFER
, 0, size
, dst
.addr
));
238 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER
, 0));
239 checkGLcall("PBO download");
240 context_release(context
);
244 memcpy(dst
.addr
, src
.addr
, size
);
248 /* Context activation is done by the caller. Context may be NULL in
249 * WINED3D_NO3D mode. */
250 BOOL
wined3d_texture_load_location(struct wined3d_texture
*texture
,
251 unsigned int sub_resource_idx
, struct wined3d_context
*context
, DWORD location
)
253 static const DWORD sysmem_locations
= WINED3D_LOCATION_SYSMEM
| WINED3D_LOCATION_USER_MEMORY
254 | WINED3D_LOCATION_BUFFER
;
255 DWORD current
= texture
->sub_resources
[sub_resource_idx
].locations
;
258 TRACE("texture %p, sub_resource_idx %u, context %p, location %s.\n",
259 texture
, sub_resource_idx
, context
, wined3d_debug_location(location
));
261 TRACE("Current resource location %s.\n", wined3d_debug_location(current
));
263 if (current
& location
)
265 TRACE("Location %s is already up to date.\n", wined3d_debug_location(location
));
271 DWORD required_access
= wined3d_resource_access_from_location(location
);
272 if ((texture
->resource
.access
& required_access
) != required_access
)
273 WARN("Operation requires %#x access, but texture only has %#x.\n",
274 required_access
, texture
->resource
.access
);
277 if (current
& WINED3D_LOCATION_DISCARDED
)
279 TRACE("Sub-resource previously discarded, nothing to do.\n");
280 if (!wined3d_texture_prepare_location(texture
, sub_resource_idx
, context
, location
))
282 wined3d_texture_validate_location(texture
, sub_resource_idx
, location
);
283 wined3d_texture_invalidate_location(texture
, sub_resource_idx
, WINED3D_LOCATION_DISCARDED
);
289 ERR("Sub-resource %u of texture %p does not have any up to date location.\n",
290 sub_resource_idx
, texture
);
291 wined3d_texture_validate_location(texture
, sub_resource_idx
, WINED3D_LOCATION_DISCARDED
);
292 return wined3d_texture_load_location(texture
, sub_resource_idx
, context
, location
);
295 if ((location
& sysmem_locations
) && (current
& sysmem_locations
))
296 ret
= wined3d_texture_copy_sysmem_location(texture
, sub_resource_idx
, context
, location
);
298 ret
= texture
->texture_ops
->texture_load_location(texture
, sub_resource_idx
, context
, location
);
301 wined3d_texture_validate_location(texture
, sub_resource_idx
, location
);
306 void wined3d_texture_get_memory(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
,
307 struct wined3d_bo_address
*data
, DWORD locations
)
309 struct wined3d_texture_sub_resource
*sub_resource
;
311 TRACE("texture %p, sub_resource_idx %u, data %p, locations %s.\n",
312 texture
, sub_resource_idx
, data
, wined3d_debug_location(locations
));
314 sub_resource
= &texture
->sub_resources
[sub_resource_idx
];
315 if (locations
& WINED3D_LOCATION_BUFFER
)
318 data
->buffer_object
= sub_resource
->buffer_object
;
321 if (locations
& WINED3D_LOCATION_USER_MEMORY
)
323 data
->addr
= texture
->user_memory
;
324 data
->buffer_object
= 0;
327 if (locations
& WINED3D_LOCATION_SYSMEM
)
329 data
->addr
= texture
->resource
.heap_memory
;
330 data
->addr
+= sub_resource
->offset
;
331 data
->buffer_object
= 0;
335 ERR("Unexpected locations %s.\n", wined3d_debug_location(locations
));
337 data
->buffer_object
= 0;
340 static HRESULT
wined3d_texture_init(struct wined3d_texture
*texture
, const struct wined3d_texture_ops
*texture_ops
,
341 UINT layer_count
, UINT level_count
, const struct wined3d_resource_desc
*desc
, DWORD flags
,
342 struct wined3d_device
*device
, void *parent
, const struct wined3d_parent_ops
*parent_ops
,
343 const struct wined3d_resource_ops
*resource_ops
)
345 unsigned int i
, j
, size
, offset
= 0;
346 const struct wined3d_format
*format
;
349 TRACE("texture %p, texture_ops %p, layer_count %u, level_count %u, resource_type %s, format %s, "
350 "multisample_type %#x, multisample_quality %#x, usage %s, access %s, width %u, height %u, depth %u, "
351 "flags %#x, device %p, parent %p, parent_ops %p, resource_ops %p.\n",
352 texture
, texture_ops
, layer_count
, level_count
, debug_d3dresourcetype(desc
->resource_type
),
353 debug_d3dformat(desc
->format
), desc
->multisample_type
, desc
->multisample_quality
,
354 debug_d3dusage(desc
->usage
), wined3d_debug_resource_access(desc
->access
),
355 desc
->width
, desc
->height
, desc
->depth
, flags
, device
, parent
, parent_ops
, resource_ops
);
357 if (!desc
->width
|| !desc
->height
|| !desc
->depth
)
358 return WINED3DERR_INVALIDCALL
;
360 format
= wined3d_get_format(&device
->adapter
->gl_info
, desc
->format
, desc
->usage
);
362 for (i
= 0; i
< layer_count
; ++i
)
364 for (j
= 0; j
< level_count
; ++j
)
366 unsigned int idx
= i
* level_count
+ j
;
368 size
= wined3d_format_calculate_size(format
, device
->surface_alignment
,
369 max(1, desc
->width
>> j
), max(1, desc
->height
>> j
), max(1, desc
->depth
>> j
));
370 texture
->sub_resources
[idx
].offset
= offset
;
371 texture
->sub_resources
[idx
].size
= size
;
374 offset
= (offset
+ (RESOURCE_ALIGNMENT
- 1)) & ~(RESOURCE_ALIGNMENT
- 1);
378 return WINED3DERR_INVALIDCALL
;
380 if (FAILED(hr
= resource_init(&texture
->resource
, device
, desc
->resource_type
, format
,
381 desc
->multisample_type
, desc
->multisample_quality
, desc
->usage
, desc
->access
,
382 desc
->width
, desc
->height
, desc
->depth
, offset
, parent
, parent_ops
, resource_ops
)))
384 static unsigned int once
;
386 /* DXTn 3D textures are not supported. Do not write the ERR for them. */
387 if ((desc
->format
== WINED3DFMT_DXT1
|| desc
->format
== WINED3DFMT_DXT2
|| desc
->format
== WINED3DFMT_DXT3
388 || desc
->format
== WINED3DFMT_DXT4
|| desc
->format
== WINED3DFMT_DXT5
)
389 && !(format
->flags
[WINED3D_GL_RES_TYPE_TEX_2D
] & WINED3DFMT_FLAG_TEXTURE
)
390 && desc
->resource_type
!= WINED3D_RTYPE_TEXTURE_3D
&& !once
++)
391 ERR_(winediag
)("The application tried to create a DXTn texture, but the driver does not support them.\n");
393 WARN("Failed to initialize resource, returning %#x\n", hr
);
396 wined3d_resource_update_draw_binding(&texture
->resource
);
397 if ((flags
& WINED3D_TEXTURE_CREATE_MAPPABLE
) || desc
->format
== WINED3DFMT_D16_LOCKABLE
)
398 texture
->resource
.access
|= WINED3D_RESOURCE_ACCESS_MAP_R
| WINED3D_RESOURCE_ACCESS_MAP_W
;
400 texture
->texture_ops
= texture_ops
;
402 texture
->layer_count
= layer_count
;
403 texture
->level_count
= level_count
;
405 texture
->flags
|= WINED3D_TEXTURE_POW2_MAT_IDENT
| WINED3D_TEXTURE_NORMALIZED_COORDS
;
406 if (flags
& WINED3D_TEXTURE_CREATE_GET_DC_LENIENT
)
407 texture
->flags
|= WINED3D_TEXTURE_PIN_SYSMEM
| WINED3D_TEXTURE_GET_DC_LENIENT
;
408 if (flags
& (WINED3D_TEXTURE_CREATE_GET_DC
| WINED3D_TEXTURE_CREATE_GET_DC_LENIENT
))
409 texture
->flags
|= WINED3D_TEXTURE_GET_DC
;
410 if (flags
& WINED3D_TEXTURE_CREATE_DISCARD
)
411 texture
->flags
|= WINED3D_TEXTURE_DISCARD
;
412 if (flags
& WINED3D_TEXTURE_CREATE_GENERATE_MIPMAPS
)
414 if (!(texture
->resource
.format_flags
& WINED3DFMT_FLAG_GEN_MIPMAP
))
415 WARN("Format doesn't support mipmaps generation, "
416 "ignoring WINED3D_TEXTURE_CREATE_GENERATE_MIPMAPS flag.\n");
418 texture
->flags
|= WINED3D_TEXTURE_GENERATE_MIPMAPS
;
421 list_init(&texture
->renderbuffers
);
426 /* Context activation is done by the caller. */
427 static void wined3d_texture_remove_buffer_object(struct wined3d_texture
*texture
,
428 unsigned int sub_resource_idx
, const struct wined3d_gl_info
*gl_info
)
430 GLuint
*buffer_object
= &texture
->sub_resources
[sub_resource_idx
].buffer_object
;
432 GL_EXTCALL(glDeleteBuffers(1, buffer_object
));
433 checkGLcall("glDeleteBuffers");
435 TRACE("Deleted buffer object %u for texture %p, sub-resource %u.\n",
436 *buffer_object
, texture
, sub_resource_idx
);
438 wined3d_texture_invalidate_location(texture
, sub_resource_idx
, WINED3D_LOCATION_BUFFER
);
442 static void wined3d_texture_update_map_binding(struct wined3d_texture
*texture
)
444 unsigned int sub_count
= texture
->level_count
* texture
->layer_count
;
445 const struct wined3d_device
*device
= texture
->resource
.device
;
446 DWORD map_binding
= texture
->update_map_binding
;
447 struct wined3d_context
*context
= NULL
;
450 if (device
->d3d_initialized
)
451 context
= context_acquire(device
, NULL
, 0);
453 for (i
= 0; i
< sub_count
; ++i
)
455 if (texture
->sub_resources
[i
].locations
== texture
->resource
.map_binding
456 && !wined3d_texture_load_location(texture
, i
, context
, map_binding
))
457 ERR("Failed to load location %s.\n", wined3d_debug_location(map_binding
));
458 if (texture
->resource
.map_binding
== WINED3D_LOCATION_BUFFER
)
459 wined3d_texture_remove_buffer_object(texture
, i
, context
->gl_info
);
463 context_release(context
);
465 texture
->resource
.map_binding
= map_binding
;
466 texture
->update_map_binding
= 0;
469 void wined3d_texture_set_map_binding(struct wined3d_texture
*texture
, DWORD map_binding
)
471 texture
->update_map_binding
= map_binding
;
472 if (!texture
->resource
.map_count
)
473 wined3d_texture_update_map_binding(texture
);
476 /* A GL context is provided by the caller */
477 static void gltexture_delete(struct wined3d_device
*device
, const struct wined3d_gl_info
*gl_info
,
478 struct gl_texture
*tex
)
480 context_gl_resource_released(device
, tex
->name
, FALSE
);
481 gl_info
->gl_ops
.gl
.p_glDeleteTextures(1, &tex
->name
);
485 static unsigned int wined3d_texture_get_gl_sample_count(const struct wined3d_texture
*texture
)
487 const struct wined3d_format
*format
= texture
->resource
.format
;
489 /* TODO: NVIDIA expose their Coverage Sample Anti-Aliasing (CSAA)
490 * feature through type == MULTISAMPLE_XX and quality != 0. This could
491 * be mapped to GL_NV_framebuffer_multisample_coverage.
493 * AMD have a similar feature called Enhanced Quality Anti-Aliasing
494 * (EQAA), but it does not have an equivalent OpenGL extension. */
496 /* We advertise as many WINED3D_MULTISAMPLE_NON_MASKABLE quality
497 * levels as the count of advertised multisample types for the texture
499 if (texture
->resource
.multisample_type
== WINED3D_MULTISAMPLE_NON_MASKABLE
)
501 unsigned int i
, count
= 0;
503 for (i
= 0; i
< sizeof(format
->multisample_types
) * CHAR_BIT
; ++i
)
505 if (format
->multisample_types
& 1u << i
)
507 if (texture
->resource
.multisample_quality
== count
++)
514 return texture
->resource
.multisample_type
;
517 /* Context activation is done by the caller. */
518 /* The caller is responsible for binding the correct texture. */
519 static void wined3d_texture_allocate_gl_mutable_storage(struct wined3d_texture
*texture
,
520 GLenum gl_internal_format
, const struct wined3d_format
*format
,
521 const struct wined3d_gl_info
*gl_info
)
523 unsigned int level
, level_count
, layer
, layer_count
;
524 GLsizei width
, height
;
527 level_count
= texture
->level_count
;
528 layer_count
= texture
->target
== GL_TEXTURE_2D_ARRAY
? 1 : texture
->layer_count
;
530 for (layer
= 0; layer
< layer_count
; ++layer
)
532 target
= wined3d_texture_get_sub_resource_target(texture
, layer
* level_count
);
534 for (level
= 0; level
< level_count
; ++level
)
536 width
= wined3d_texture_get_level_pow2_width(texture
, level
);
537 height
= wined3d_texture_get_level_pow2_height(texture
, level
);
538 if (texture
->resource
.format_flags
& WINED3DFMT_FLAG_HEIGHT_SCALE
)
540 height
*= format
->height_scale
.numerator
;
541 height
/= format
->height_scale
.denominator
;
544 TRACE("texture %p, layer %u, level %u, target %#x, width %u, height %u.\n",
545 texture
, layer
, level
, target
, width
, height
);
547 if (texture
->target
== GL_TEXTURE_2D_ARRAY
)
549 GL_EXTCALL(glTexImage3D(target
, level
, gl_internal_format
, width
, height
,
550 texture
->layer_count
, 0, format
->glFormat
, format
->glType
, NULL
));
551 checkGLcall("glTexImage3D");
555 gl_info
->gl_ops
.gl
.p_glTexImage2D(target
, level
, gl_internal_format
,
556 width
, height
, 0, format
->glFormat
, format
->glType
, NULL
);
557 checkGLcall("glTexImage2D");
563 /* Context activation is done by the caller. */
564 /* The caller is responsible for binding the correct texture. */
565 static void wined3d_texture_allocate_gl_immutable_storage(struct wined3d_texture
*texture
,
566 GLenum gl_internal_format
, const struct wined3d_gl_info
*gl_info
)
568 unsigned int samples
= wined3d_texture_get_gl_sample_count(texture
);
569 GLsizei height
= wined3d_texture_get_level_pow2_height(texture
, 0);
570 GLsizei width
= wined3d_texture_get_level_pow2_width(texture
, 0);
572 switch (texture
->target
)
574 case GL_TEXTURE_2D_ARRAY
:
575 GL_EXTCALL(glTexStorage3D(texture
->target
, texture
->level_count
,
576 gl_internal_format
, width
, height
, texture
->layer_count
));
578 case GL_TEXTURE_2D_MULTISAMPLE
:
579 GL_EXTCALL(glTexStorage2DMultisample(texture
->target
, samples
,
580 gl_internal_format
, width
, height
, GL_FALSE
));
582 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
583 GL_EXTCALL(glTexStorage3DMultisample(texture
->target
, samples
,
584 gl_internal_format
, width
, height
, texture
->layer_count
, GL_FALSE
));
587 GL_EXTCALL(glTexStorage2D(texture
->target
, texture
->level_count
,
588 gl_internal_format
, width
, height
));
592 checkGLcall("allocate immutable storage");
595 static void wined3d_texture_unload_gl_texture(struct wined3d_texture
*texture
)
597 struct wined3d_device
*device
= texture
->resource
.device
;
598 const struct wined3d_gl_info
*gl_info
= NULL
;
599 struct wined3d_context
*context
= NULL
;
601 if (texture
->texture_rgb
.name
|| texture
->texture_srgb
.name
602 || texture
->rb_multisample
|| texture
->rb_resolved
)
604 context
= context_acquire(device
, NULL
, 0);
605 gl_info
= context
->gl_info
;
608 if (texture
->texture_rgb
.name
)
609 gltexture_delete(device
, context
->gl_info
, &texture
->texture_rgb
);
611 if (texture
->texture_srgb
.name
)
612 gltexture_delete(device
, context
->gl_info
, &texture
->texture_srgb
);
614 if (texture
->rb_multisample
)
616 TRACE("Deleting multisample renderbuffer %u.\n", texture
->rb_multisample
);
617 context_gl_resource_released(device
, texture
->rb_multisample
, TRUE
);
618 gl_info
->fbo_ops
.glDeleteRenderbuffers(1, &texture
->rb_multisample
);
619 texture
->rb_multisample
= 0;
622 if (texture
->rb_resolved
)
624 TRACE("Deleting resolved renderbuffer %u.\n", texture
->rb_resolved
);
625 context_gl_resource_released(device
, texture
->rb_resolved
, TRUE
);
626 gl_info
->fbo_ops
.glDeleteRenderbuffers(1, &texture
->rb_resolved
);
627 texture
->rb_resolved
= 0;
630 if (context
) context_release(context
);
632 wined3d_texture_set_dirty(texture
);
634 resource_unload(&texture
->resource
);
637 static void wined3d_texture_sub_resources_destroyed(struct wined3d_texture
*texture
)
639 unsigned int sub_count
= texture
->level_count
* texture
->layer_count
;
640 struct wined3d_texture_sub_resource
*sub_resource
;
643 for (i
= 0; i
< sub_count
; ++i
)
645 sub_resource
= &texture
->sub_resources
[i
];
646 if (sub_resource
->parent
)
648 TRACE("sub-resource %u.\n", i
);
649 sub_resource
->parent_ops
->wined3d_object_destroyed(sub_resource
->parent
);
650 sub_resource
->parent
= NULL
;
655 static void wined3d_texture_cleanup(struct wined3d_texture
*texture
)
657 unsigned int sub_count
= texture
->level_count
* texture
->layer_count
;
658 struct wined3d_device
*device
= texture
->resource
.device
;
659 struct wined3d_renderbuffer_entry
*entry
, *entry2
;
660 const struct wined3d_gl_info
*gl_info
= NULL
;
661 struct wined3d_context
*context
= NULL
;
662 GLuint buffer_object
;
665 TRACE("texture %p.\n", texture
);
667 for (i
= 0; i
< sub_count
; ++i
)
669 if (!(buffer_object
= texture
->sub_resources
[i
].buffer_object
))
672 TRACE("Deleting buffer object %u.\n", buffer_object
);
674 /* We may not be able to get a context in wined3d_texture_cleanup() in
675 * general, but if a buffer object was previously created we can. */
678 context
= context_acquire(device
, NULL
, 0);
679 gl_info
= context
->gl_info
;
682 GL_EXTCALL(glDeleteBuffers(1, &buffer_object
));
685 if (!context
&& !list_empty(&texture
->renderbuffers
))
687 context
= context_acquire(device
, NULL
, 0);
688 gl_info
= context
->gl_info
;
691 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, &texture
->renderbuffers
, struct wined3d_renderbuffer_entry
, entry
)
693 TRACE("Deleting renderbuffer %u.\n", entry
->id
);
694 context_gl_resource_released(device
, entry
->id
, TRUE
);
695 gl_info
->fbo_ops
.glDeleteRenderbuffers(1, &entry
->id
);
700 context_release(context
);
702 texture
->texture_ops
->texture_cleanup_sub_resources(texture
);
703 if (texture
->overlay_info
)
705 for (i
= 0; i
< sub_count
; ++i
)
707 struct wined3d_overlay_info
*info
= &texture
->overlay_info
[i
];
708 struct wined3d_overlay_info
*overlay
, *cur
;
710 list_remove(&info
->entry
);
711 LIST_FOR_EACH_ENTRY_SAFE(overlay
, cur
, &info
->overlays
, struct wined3d_overlay_info
, entry
)
713 list_remove(&overlay
->entry
);
716 heap_free(texture
->overlay_info
);
718 wined3d_texture_unload_gl_texture(texture
);
721 void wined3d_texture_set_swapchain(struct wined3d_texture
*texture
, struct wined3d_swapchain
*swapchain
)
723 texture
->swapchain
= swapchain
;
724 wined3d_resource_update_draw_binding(&texture
->resource
);
727 /* Context activation is done by the caller. */
728 void wined3d_texture_bind(struct wined3d_texture
*texture
,
729 struct wined3d_context
*context
, BOOL srgb
)
731 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
732 const struct wined3d_format
*format
= texture
->resource
.format
;
733 const struct color_fixup_desc fixup
= format
->color_fixup
;
734 struct gl_texture
*gl_tex
;
737 TRACE("texture %p, context %p, srgb %#x.\n", texture
, context
, srgb
);
739 if (!needs_separate_srgb_gl_texture(context
, texture
))
742 /* sRGB mode cache for preload() calls outside drawprim. */
744 texture
->flags
|= WINED3D_TEXTURE_IS_SRGB
;
746 texture
->flags
&= ~WINED3D_TEXTURE_IS_SRGB
;
748 gl_tex
= wined3d_texture_get_gl_texture(texture
, srgb
);
749 target
= texture
->target
;
753 context_bind_texture(context
, target
, gl_tex
->name
);
757 gl_info
->gl_ops
.gl
.p_glGenTextures(1, &gl_tex
->name
);
758 checkGLcall("glGenTextures");
759 TRACE("Generated texture %d.\n", gl_tex
->name
);
763 ERR("Failed to generate a texture name.\n");
767 /* Initialise the state of the texture object to the OpenGL defaults, not
768 * the wined3d defaults. */
769 gl_tex
->sampler_desc
.address_u
= WINED3D_TADDRESS_WRAP
;
770 gl_tex
->sampler_desc
.address_v
= WINED3D_TADDRESS_WRAP
;
771 gl_tex
->sampler_desc
.address_w
= WINED3D_TADDRESS_WRAP
;
772 memset(gl_tex
->sampler_desc
.border_color
, 0, sizeof(gl_tex
->sampler_desc
.border_color
));
773 gl_tex
->sampler_desc
.mag_filter
= WINED3D_TEXF_LINEAR
;
774 gl_tex
->sampler_desc
.min_filter
= WINED3D_TEXF_POINT
; /* GL_NEAREST_MIPMAP_LINEAR */
775 gl_tex
->sampler_desc
.mip_filter
= WINED3D_TEXF_LINEAR
; /* GL_NEAREST_MIPMAP_LINEAR */
776 gl_tex
->sampler_desc
.lod_bias
= 0.0f
;
777 gl_tex
->sampler_desc
.min_lod
= -1000.0f
;
778 gl_tex
->sampler_desc
.max_lod
= 1000.0f
;
779 gl_tex
->sampler_desc
.max_anisotropy
= 1;
780 gl_tex
->sampler_desc
.compare
= FALSE
;
781 gl_tex
->sampler_desc
.comparison_func
= WINED3D_CMP_LESSEQUAL
;
782 if (context
->gl_info
->supported
[EXT_TEXTURE_SRGB_DECODE
])
783 gl_tex
->sampler_desc
.srgb_decode
= TRUE
;
785 gl_tex
->sampler_desc
.srgb_decode
= srgb
;
786 gl_tex
->base_level
= 0;
787 wined3d_texture_set_dirty(texture
);
789 context_bind_texture(context
, target
, gl_tex
->name
);
791 /* For a new texture we have to set the texture levels after binding the
792 * texture. Beware that texture rectangles do not support mipmapping, but
793 * set the maxmiplevel if we're relying on the partial
794 * GL_ARB_texture_non_power_of_two emulation with texture rectangles.
795 * (I.e., do not care about cond_np2 here, just look for
796 * GL_TEXTURE_RECTANGLE_ARB.) */
797 if (target
!= GL_TEXTURE_RECTANGLE_ARB
)
799 TRACE("Setting GL_TEXTURE_MAX_LEVEL to %u.\n", texture
->level_count
- 1);
800 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, texture
->level_count
- 1);
801 checkGLcall("glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, texture->level_count)");
804 if (target
== GL_TEXTURE_CUBE_MAP_ARB
)
806 /* Cubemaps are always set to clamp, regardless of the sampler state. */
807 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
808 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
809 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_R
, GL_CLAMP_TO_EDGE
);
812 if (texture
->flags
& WINED3D_TEXTURE_COND_NP2
)
814 /* Conditinal non power of two textures use a different clamping
815 * default. If we're using the GL_WINE_normalized_texrect partial
816 * driver emulation, we're dealing with a GL_TEXTURE_2D texture which
817 * has the address mode set to repeat - something that prevents us
818 * from hitting the accelerated codepath. Thus manually set the GL
819 * state. The same applies to filtering. Even if the texture has only
820 * one mip level, the default LINEAR_MIPMAP_LINEAR filter causes a SW
821 * fallback on macos. */
822 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
823 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
824 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
825 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
826 checkGLcall("glTexParameteri");
827 gl_tex
->sampler_desc
.address_u
= WINED3D_TADDRESS_CLAMP
;
828 gl_tex
->sampler_desc
.address_v
= WINED3D_TADDRESS_CLAMP
;
829 gl_tex
->sampler_desc
.mag_filter
= WINED3D_TEXF_POINT
;
830 gl_tex
->sampler_desc
.min_filter
= WINED3D_TEXF_POINT
;
831 gl_tex
->sampler_desc
.mip_filter
= WINED3D_TEXF_NONE
;
834 if (gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
] && gl_info
->supported
[ARB_DEPTH_TEXTURE
])
836 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_DEPTH_TEXTURE_MODE_ARB
, GL_INTENSITY
);
837 checkGLcall("glTexParameteri(GL_DEPTH_TEXTURE_MODE_ARB, GL_INTENSITY)");
840 if (!is_identity_fixup(fixup
) && can_use_texture_swizzle(gl_info
, format
))
842 static const GLenum swizzle_source
[] =
844 GL_ZERO
, /* CHANNEL_SOURCE_ZERO */
845 GL_ONE
, /* CHANNEL_SOURCE_ONE */
846 GL_RED
, /* CHANNEL_SOURCE_X */
847 GL_GREEN
, /* CHANNEL_SOURCE_Y */
848 GL_BLUE
, /* CHANNEL_SOURCE_Z */
849 GL_ALPHA
, /* CHANNEL_SOURCE_W */
857 swizzle
.x
= swizzle_source
[fixup
.x_source
];
858 swizzle
.y
= swizzle_source
[fixup
.y_source
];
859 swizzle
.z
= swizzle_source
[fixup
.z_source
];
860 swizzle
.w
= swizzle_source
[fixup
.w_source
];
861 gl_info
->gl_ops
.gl
.p_glTexParameteriv(target
, GL_TEXTURE_SWIZZLE_RGBA
, &swizzle
.x
);
862 checkGLcall("glTexParameteriv(GL_TEXTURE_SWIZZLE_RGBA)");
866 /* Context activation is done by the caller. */
867 void wined3d_texture_bind_and_dirtify(struct wined3d_texture
*texture
,
868 struct wined3d_context
*context
, BOOL srgb
)
870 /* We don't need a specific texture unit, but after binding the texture
871 * the current unit is dirty. Read the unit back instead of switching to
872 * 0, this avoids messing around with the state manager's GL states. The
873 * current texture unit should always be a valid one.
875 * To be more specific, this is tricky because we can implicitly be
876 * called from sampler() in state.c. This means we can't touch anything
877 * other than whatever happens to be the currently active texture, or we
878 * would risk marking already applied sampler states dirty again. */
879 if (context
->active_texture
< ARRAY_SIZE(context
->rev_tex_unit_map
))
881 DWORD active_sampler
= context
->rev_tex_unit_map
[context
->active_texture
];
882 if (active_sampler
!= WINED3D_UNMAPPED_STAGE
)
883 context_invalidate_state(context
, STATE_SAMPLER(active_sampler
));
885 /* FIXME: Ideally we'd only do this when touching a binding that's used by
887 context_invalidate_compute_state(context
, STATE_COMPUTE_SHADER_RESOURCE_BINDING
);
888 context_invalidate_state(context
, STATE_GRAPHICS_SHADER_RESOURCE_BINDING
);
890 wined3d_texture_bind(texture
, context
, srgb
);
893 /* Context activation is done by the caller (state handler). */
894 /* This function relies on the correct texture being bound and loaded. */
895 void wined3d_texture_apply_sampler_desc(struct wined3d_texture
*texture
,
896 const struct wined3d_sampler_desc
*sampler_desc
, const struct wined3d_context
*context
)
898 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
899 GLenum target
= texture
->target
;
900 struct gl_texture
*gl_tex
;
903 TRACE("texture %p, sampler_desc %p, context %p.\n", texture
, sampler_desc
, context
);
905 gl_tex
= wined3d_texture_get_gl_texture(texture
, texture
->flags
& WINED3D_TEXTURE_IS_SRGB
);
907 state
= sampler_desc
->address_u
;
908 if (state
!= gl_tex
->sampler_desc
.address_u
)
910 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_S
,
911 gl_info
->wrap_lookup
[state
- WINED3D_TADDRESS_WRAP
]);
912 gl_tex
->sampler_desc
.address_u
= state
;
915 state
= sampler_desc
->address_v
;
916 if (state
!= gl_tex
->sampler_desc
.address_v
)
918 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_T
,
919 gl_info
->wrap_lookup
[state
- WINED3D_TADDRESS_WRAP
]);
920 gl_tex
->sampler_desc
.address_v
= state
;
923 state
= sampler_desc
->address_w
;
924 if (state
!= gl_tex
->sampler_desc
.address_w
)
926 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_R
,
927 gl_info
->wrap_lookup
[state
- WINED3D_TADDRESS_WRAP
]);
928 gl_tex
->sampler_desc
.address_w
= state
;
931 if (memcmp(gl_tex
->sampler_desc
.border_color
, sampler_desc
->border_color
,
932 sizeof(gl_tex
->sampler_desc
.border_color
)))
934 gl_info
->gl_ops
.gl
.p_glTexParameterfv(target
, GL_TEXTURE_BORDER_COLOR
, &sampler_desc
->border_color
[0]);
935 memcpy(gl_tex
->sampler_desc
.border_color
, sampler_desc
->border_color
,
936 sizeof(gl_tex
->sampler_desc
.border_color
));
939 state
= sampler_desc
->mag_filter
;
940 if (state
!= gl_tex
->sampler_desc
.mag_filter
)
942 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MAG_FILTER
, wined3d_gl_mag_filter(state
));
943 gl_tex
->sampler_desc
.mag_filter
= state
;
946 if (sampler_desc
->min_filter
!= gl_tex
->sampler_desc
.min_filter
947 || sampler_desc
->mip_filter
!= gl_tex
->sampler_desc
.mip_filter
)
949 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MIN_FILTER
,
950 wined3d_gl_min_mip_filter(sampler_desc
->min_filter
, sampler_desc
->mip_filter
));
951 gl_tex
->sampler_desc
.min_filter
= sampler_desc
->min_filter
;
952 gl_tex
->sampler_desc
.mip_filter
= sampler_desc
->mip_filter
;
955 state
= sampler_desc
->max_anisotropy
;
956 if (state
!= gl_tex
->sampler_desc
.max_anisotropy
)
958 if (gl_info
->supported
[ARB_TEXTURE_FILTER_ANISOTROPIC
])
959 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MAX_ANISOTROPY
, state
);
961 WARN("Anisotropic filtering not supported.\n");
962 gl_tex
->sampler_desc
.max_anisotropy
= state
;
965 if (!sampler_desc
->srgb_decode
!= !gl_tex
->sampler_desc
.srgb_decode
966 && (context
->d3d_info
->wined3d_creation_flags
& WINED3D_SRGB_READ_WRITE_CONTROL
)
967 && gl_info
->supported
[EXT_TEXTURE_SRGB_DECODE
])
969 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_SRGB_DECODE_EXT
,
970 sampler_desc
->srgb_decode
? GL_DECODE_EXT
: GL_SKIP_DECODE_EXT
);
971 gl_tex
->sampler_desc
.srgb_decode
= sampler_desc
->srgb_decode
;
974 if (!sampler_desc
->compare
!= !gl_tex
->sampler_desc
.compare
)
976 if (sampler_desc
->compare
)
977 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_COMPARE_MODE_ARB
, GL_COMPARE_R_TO_TEXTURE_ARB
);
979 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_COMPARE_MODE_ARB
, GL_NONE
);
980 gl_tex
->sampler_desc
.compare
= sampler_desc
->compare
;
983 checkGLcall("Texture parameter application");
985 if (gl_info
->supported
[EXT_TEXTURE_LOD_BIAS
])
987 gl_info
->gl_ops
.gl
.p_glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT
,
988 GL_TEXTURE_LOD_BIAS_EXT
, sampler_desc
->lod_bias
);
989 checkGLcall("glTexEnvf(GL_TEXTURE_LOD_BIAS_EXT, ...)");
993 ULONG CDECL
wined3d_texture_incref(struct wined3d_texture
*texture
)
997 TRACE("texture %p, swapchain %p.\n", texture
, texture
->swapchain
);
999 if (texture
->swapchain
)
1000 return wined3d_swapchain_incref(texture
->swapchain
);
1002 refcount
= InterlockedIncrement(&texture
->resource
.ref
);
1003 TRACE("%p increasing refcount to %u.\n", texture
, refcount
);
1008 static void wined3d_texture_cleanup_sync(struct wined3d_texture
*texture
)
1010 wined3d_texture_sub_resources_destroyed(texture
);
1011 resource_cleanup(&texture
->resource
);
1012 wined3d_resource_wait_idle(&texture
->resource
);
1013 wined3d_texture_cleanup(texture
);
1016 static void wined3d_texture_destroy_object(void *object
)
1018 wined3d_texture_cleanup(object
);
1022 ULONG CDECL
wined3d_texture_decref(struct wined3d_texture
*texture
)
1026 TRACE("texture %p, swapchain %p.\n", texture
, texture
->swapchain
);
1028 if (texture
->swapchain
)
1029 return wined3d_swapchain_decref(texture
->swapchain
);
1031 refcount
= InterlockedDecrement(&texture
->resource
.ref
);
1032 TRACE("%p decreasing refcount to %u.\n", texture
, refcount
);
1036 /* Wait for the texture to become idle if it's using user memory,
1037 * since the application is allowed to free that memory once the
1038 * texture is destroyed. Note that this implies that
1039 * wined3d_texture_destroy_object() can't access that memory either. */
1040 if (texture
->user_memory
)
1041 wined3d_resource_wait_idle(&texture
->resource
);
1042 wined3d_texture_sub_resources_destroyed(texture
);
1043 texture
->resource
.parent_ops
->wined3d_object_destroyed(texture
->resource
.parent
);
1044 resource_cleanup(&texture
->resource
);
1045 wined3d_cs_destroy_object(texture
->resource
.device
->cs
, wined3d_texture_destroy_object
, texture
);
1051 struct wined3d_resource
* CDECL
wined3d_texture_get_resource(struct wined3d_texture
*texture
)
1053 TRACE("texture %p.\n", texture
);
1055 return &texture
->resource
;
1058 static BOOL
color_key_equal(const struct wined3d_color_key
*c1
, struct wined3d_color_key
*c2
)
1060 return c1
->color_space_low_value
== c2
->color_space_low_value
1061 && c1
->color_space_high_value
== c2
->color_space_high_value
;
1064 /* Context activation is done by the caller */
1065 void wined3d_texture_load(struct wined3d_texture
*texture
,
1066 struct wined3d_context
*context
, BOOL srgb
)
1068 UINT sub_count
= texture
->level_count
* texture
->layer_count
;
1069 const struct wined3d_d3d_info
*d3d_info
= context
->d3d_info
;
1073 TRACE("texture %p, context %p, srgb %#x.\n", texture
, context
, srgb
);
1075 if (!needs_separate_srgb_gl_texture(context
, texture
))
1079 flag
= WINED3D_TEXTURE_SRGB_VALID
;
1081 flag
= WINED3D_TEXTURE_RGB_VALID
;
1083 if (!d3d_info
->shader_color_key
1084 && (!(texture
->async
.flags
& WINED3D_TEXTURE_ASYNC_COLOR_KEY
)
1085 != !(texture
->async
.color_key_flags
& WINED3D_CKEY_SRC_BLT
)
1086 || (texture
->async
.flags
& WINED3D_TEXTURE_ASYNC_COLOR_KEY
1087 && !color_key_equal(&texture
->async
.gl_color_key
, &texture
->async
.src_blt_color_key
))))
1089 unsigned int sub_count
= texture
->level_count
* texture
->layer_count
;
1092 TRACE("Reloading because of color key value change.\n");
1093 for (i
= 0; i
< sub_count
; i
++)
1095 if (!wined3d_texture_load_location(texture
, i
, context
, texture
->resource
.map_binding
))
1096 ERR("Failed to load location %s.\n", wined3d_debug_location(texture
->resource
.map_binding
));
1098 wined3d_texture_invalidate_location(texture
, i
, ~texture
->resource
.map_binding
);
1101 texture
->async
.gl_color_key
= texture
->async
.src_blt_color_key
;
1104 if (texture
->flags
& flag
)
1106 TRACE("Texture %p not dirty, nothing to do.\n", texture
);
1110 /* Reload the surfaces if the texture is marked dirty. */
1111 for (i
= 0; i
< sub_count
; ++i
)
1113 if (!wined3d_texture_load_location(texture
, i
, context
,
1114 srgb
? WINED3D_LOCATION_TEXTURE_SRGB
: WINED3D_LOCATION_TEXTURE_RGB
))
1115 ERR("Failed to load location (srgb %#x).\n", srgb
);
1117 texture
->flags
|= flag
;
1120 void * CDECL
wined3d_texture_get_parent(const struct wined3d_texture
*texture
)
1122 TRACE("texture %p.\n", texture
);
1124 return texture
->resource
.parent
;
1127 HRESULT
wined3d_texture_check_box_dimensions(const struct wined3d_texture
*texture
,
1128 unsigned int level
, const struct wined3d_box
*box
)
1130 const struct wined3d_format
*format
= texture
->resource
.format
;
1131 unsigned int width_mask
, height_mask
, width
, height
, depth
;
1133 width
= wined3d_texture_get_level_width(texture
, level
);
1134 height
= wined3d_texture_get_level_height(texture
, level
);
1135 depth
= wined3d_texture_get_level_depth(texture
, level
);
1137 if (box
->left
>= box
->right
|| box
->right
> width
1138 || box
->top
>= box
->bottom
|| box
->bottom
> height
1139 || box
->front
>= box
->back
|| box
->back
> depth
)
1141 WARN("Box %s is invalid.\n", debug_box(box
));
1142 return WINEDDERR_INVALIDRECT
;
1145 if (texture
->resource
.format_flags
& WINED3DFMT_FLAG_BLOCKS
)
1147 /* This assumes power of two block sizes, but NPOT block sizes would
1150 * This also assumes that the format's block depth is 1. */
1151 width_mask
= format
->block_width
- 1;
1152 height_mask
= format
->block_height
- 1;
1154 if ((box
->left
& width_mask
) || (box
->top
& height_mask
)
1155 || (box
->right
& width_mask
&& box
->right
!= width
)
1156 || (box
->bottom
& height_mask
&& box
->bottom
!= height
))
1158 WARN("Box %s is misaligned for %ux%u blocks.\n",
1159 debug_box(box
), format
->block_width
, format
->block_height
);
1160 return WINED3DERR_INVALIDCALL
;
1167 void CDECL
wined3d_texture_get_pitch(const struct wined3d_texture
*texture
,
1168 unsigned int level
, unsigned int *row_pitch
, unsigned int *slice_pitch
)
1170 const struct wined3d_resource
*resource
= &texture
->resource
;
1171 unsigned int width
= wined3d_texture_get_level_width(texture
, level
);
1172 unsigned int height
= wined3d_texture_get_level_height(texture
, level
);
1174 if (texture
->row_pitch
)
1176 *row_pitch
= texture
->row_pitch
;
1177 *slice_pitch
= texture
->slice_pitch
;
1181 wined3d_format_calculate_pitch(resource
->format
, resource
->device
->surface_alignment
,
1182 width
, height
, row_pitch
, slice_pitch
);
1185 DWORD CDECL
wined3d_texture_set_lod(struct wined3d_texture
*texture
, DWORD lod
)
1187 DWORD old
= texture
->lod
;
1189 TRACE("texture %p, lod %u.\n", texture
, lod
);
1191 /* The d3d9:texture test shows that SetLOD is ignored on non-managed
1192 * textures. The call always returns 0, and GetLOD always returns 0. */
1193 if (!wined3d_resource_access_is_managed(texture
->resource
.access
))
1195 TRACE("Ignoring LOD on texture with resource access %s.\n",
1196 wined3d_debug_resource_access(texture
->resource
.access
));
1200 if (lod
>= texture
->level_count
)
1201 lod
= texture
->level_count
- 1;
1203 if (texture
->lod
!= lod
)
1205 struct wined3d_device
*device
= texture
->resource
.device
;
1207 wined3d_resource_wait_idle(&texture
->resource
);
1210 texture
->texture_rgb
.base_level
= ~0u;
1211 texture
->texture_srgb
.base_level
= ~0u;
1212 if (texture
->resource
.bind_count
)
1213 wined3d_cs_emit_set_sampler_state(device
->cs
, texture
->sampler
, WINED3D_SAMP_MAX_MIP_LEVEL
,
1214 device
->state
.sampler_states
[texture
->sampler
][WINED3D_SAMP_MAX_MIP_LEVEL
]);
1220 DWORD CDECL
wined3d_texture_get_lod(const struct wined3d_texture
*texture
)
1222 TRACE("texture %p, returning %u.\n", texture
, texture
->lod
);
1224 return texture
->lod
;
1227 DWORD CDECL
wined3d_texture_get_level_count(const struct wined3d_texture
*texture
)
1229 TRACE("texture %p, returning %u.\n", texture
, texture
->level_count
);
1231 return texture
->level_count
;
1234 HRESULT CDECL
wined3d_texture_set_color_key(struct wined3d_texture
*texture
,
1235 DWORD flags
, const struct wined3d_color_key
*color_key
)
1237 struct wined3d_device
*device
= texture
->resource
.device
;
1238 static const DWORD all_flags
= WINED3D_CKEY_DST_BLT
| WINED3D_CKEY_DST_OVERLAY
1239 | WINED3D_CKEY_SRC_BLT
| WINED3D_CKEY_SRC_OVERLAY
;
1241 TRACE("texture %p, flags %#x, color_key %p.\n", texture
, flags
, color_key
);
1243 if (flags
& ~all_flags
)
1245 WARN("Invalid flags passed, returning WINED3DERR_INVALIDCALL.\n");
1246 return WINED3DERR_INVALIDCALL
;
1249 wined3d_cs_emit_set_color_key(device
->cs
, texture
, flags
, color_key
);
1254 /* In D3D the depth stencil dimensions have to be greater than or equal to the
1255 * render target dimensions. With FBOs, the dimensions have to be an exact match. */
1256 /* TODO: We should synchronize the renderbuffer's content with the texture's content. */
1257 /* Context activation is done by the caller. */
1258 void wined3d_texture_set_compatible_renderbuffer(struct wined3d_texture
*texture
,
1259 unsigned int level
, const struct wined3d_rendertarget_info
*rt
)
1261 struct wined3d_renderbuffer_entry
*entry
;
1262 const struct wined3d_gl_info
*gl_info
;
1263 unsigned int src_width
, src_height
;
1264 unsigned int width
, height
;
1265 GLuint renderbuffer
= 0;
1267 gl_info
= &texture
->resource
.device
->adapter
->gl_info
;
1268 if (gl_info
->supported
[ARB_FRAMEBUFFER_OBJECT
])
1271 if (rt
&& rt
->resource
->format
->id
!= WINED3DFMT_NULL
)
1273 struct wined3d_texture
*rt_texture
;
1274 unsigned int rt_level
;
1276 if (rt
->resource
->type
== WINED3D_RTYPE_BUFFER
)
1278 FIXME("Unsupported resource type %s.\n", debug_d3dresourcetype(rt
->resource
->type
));
1281 rt_texture
= wined3d_texture_from_resource(rt
->resource
);
1282 rt_level
= rt
->sub_resource_idx
% rt_texture
->level_count
;
1284 width
= wined3d_texture_get_level_pow2_width(rt_texture
, rt_level
);
1285 height
= wined3d_texture_get_level_pow2_height(rt_texture
, rt_level
);
1289 width
= wined3d_texture_get_level_pow2_width(texture
, level
);
1290 height
= wined3d_texture_get_level_pow2_height(texture
, level
);
1293 src_width
= wined3d_texture_get_level_pow2_width(texture
, level
);
1294 src_height
= wined3d_texture_get_level_pow2_height(texture
, level
);
1296 /* A depth stencil smaller than the render target is not valid */
1297 if (width
> src_width
|| height
> src_height
)
1300 /* Remove any renderbuffer set if the sizes match */
1301 if (width
== src_width
&& height
== src_height
)
1303 texture
->current_renderbuffer
= NULL
;
1307 /* Look if we've already got a renderbuffer of the correct dimensions */
1308 LIST_FOR_EACH_ENTRY(entry
, &texture
->renderbuffers
, struct wined3d_renderbuffer_entry
, entry
)
1310 if (entry
->width
== width
&& entry
->height
== height
)
1312 renderbuffer
= entry
->id
;
1313 texture
->current_renderbuffer
= entry
;
1320 gl_info
->fbo_ops
.glGenRenderbuffers(1, &renderbuffer
);
1321 gl_info
->fbo_ops
.glBindRenderbuffer(GL_RENDERBUFFER
, renderbuffer
);
1322 gl_info
->fbo_ops
.glRenderbufferStorage(GL_RENDERBUFFER
,
1323 texture
->resource
.format
->glInternal
, width
, height
);
1325 entry
= heap_alloc(sizeof(*entry
));
1326 entry
->width
= width
;
1327 entry
->height
= height
;
1328 entry
->id
= renderbuffer
;
1329 list_add_head(&texture
->renderbuffers
, &entry
->entry
);
1331 texture
->current_renderbuffer
= entry
;
1334 checkGLcall("set_compatible_renderbuffer");
1337 static void texture2d_create_dc(void *object
)
1339 struct wined3d_surface
*surface
= object
;
1340 struct wined3d_context
*context
= NULL
;
1341 unsigned int sub_resource_idx
, level
;
1342 const struct wined3d_format
*format
;
1343 unsigned int row_pitch
, slice_pitch
;
1344 struct wined3d_texture
*texture
;
1345 struct wined3d_bo_address data
;
1346 D3DKMT_CREATEDCFROMMEMORY desc
;
1347 struct wined3d_device
*device
;
1350 TRACE("surface %p.\n", surface
);
1352 texture
= surface
->container
;
1353 sub_resource_idx
= surface_get_sub_resource_idx(surface
);
1354 level
= sub_resource_idx
% texture
->level_count
;
1355 device
= texture
->resource
.device
;
1357 format
= texture
->resource
.format
;
1358 if (!format
->ddi_format
)
1360 WARN("Cannot create a DC for format %s.\n", debug_d3dformat(format
->id
));
1364 if (device
->d3d_initialized
)
1365 context
= context_acquire(device
, NULL
, 0);
1367 wined3d_texture_load_location(texture
, sub_resource_idx
, context
, texture
->resource
.map_binding
);
1368 wined3d_texture_invalidate_location(texture
, sub_resource_idx
, ~texture
->resource
.map_binding
);
1369 wined3d_texture_get_pitch(texture
, level
, &row_pitch
, &slice_pitch
);
1370 wined3d_texture_get_memory(texture
, sub_resource_idx
, &data
, texture
->resource
.map_binding
);
1371 desc
.pMemory
= context_map_bo_address(context
, &data
,
1372 texture
->sub_resources
[sub_resource_idx
].size
,
1373 GL_PIXEL_UNPACK_BUFFER
, WINED3D_MAP_READ
| WINED3D_MAP_WRITE
);
1376 context_release(context
);
1378 desc
.Format
= format
->ddi_format
;
1379 desc
.Width
= wined3d_texture_get_level_width(texture
, level
);
1380 desc
.Height
= wined3d_texture_get_level_height(texture
, level
);
1381 desc
.Pitch
= row_pitch
;
1382 desc
.hDeviceDc
= CreateCompatibleDC(NULL
);
1383 desc
.pColorTable
= NULL
;
1385 status
= D3DKMTCreateDCFromMemory(&desc
);
1386 DeleteDC(desc
.hDeviceDc
);
1389 WARN("Failed to create DC, status %#x.\n", status
);
1393 surface
->dc
= desc
.hDc
;
1394 surface
->bitmap
= desc
.hBitmap
;
1396 TRACE("Created DC %p, bitmap %p for surface %p.\n", surface
->dc
, surface
->bitmap
, surface
);
1399 static void texture2d_destroy_dc(void *object
)
1401 struct wined3d_surface
*surface
= object
;
1402 D3DKMT_DESTROYDCFROMMEMORY destroy_desc
;
1403 struct wined3d_context
*context
= NULL
;
1404 struct wined3d_texture
*texture
;
1405 struct wined3d_bo_address data
;
1406 unsigned int sub_resource_idx
;
1407 struct wined3d_device
*device
;
1410 texture
= surface
->container
;
1411 sub_resource_idx
= surface_get_sub_resource_idx(surface
);
1412 device
= texture
->resource
.device
;
1416 ERR("Surface %p has no DC.\n", surface
);
1420 TRACE("dc %p, bitmap %p.\n", surface
->dc
, surface
->bitmap
);
1422 destroy_desc
.hDc
= surface
->dc
;
1423 destroy_desc
.hBitmap
= surface
->bitmap
;
1424 if ((status
= D3DKMTDestroyDCFromMemory(&destroy_desc
)))
1425 ERR("Failed to destroy dc, status %#x.\n", status
);
1427 surface
->bitmap
= NULL
;
1429 if (device
->d3d_initialized
)
1430 context
= context_acquire(device
, NULL
, 0);
1432 wined3d_texture_get_memory(texture
, sub_resource_idx
, &data
, texture
->resource
.map_binding
);
1433 context_unmap_bo_address(context
, &data
, GL_PIXEL_UNPACK_BUFFER
);
1436 context_release(context
);
1439 HRESULT CDECL
wined3d_texture_update_desc(struct wined3d_texture
*texture
, UINT width
, UINT height
,
1440 enum wined3d_format_id format_id
, enum wined3d_multisample_type multisample_type
,
1441 UINT multisample_quality
, void *mem
, UINT pitch
)
1443 struct wined3d_device
*device
= texture
->resource
.device
;
1444 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
1445 const struct wined3d_format
*format
= wined3d_get_format(gl_info
, format_id
, texture
->resource
.usage
);
1446 UINT resource_size
= wined3d_format_calculate_size(format
, device
->surface_alignment
, width
, height
, 1);
1447 struct wined3d_texture_sub_resource
*sub_resource
;
1448 struct wined3d_surface
*surface
;
1449 DWORD valid_location
= 0;
1450 BOOL create_dib
= FALSE
;
1452 TRACE("texture %p, width %u, height %u, format %s, multisample_type %#x, multisample_quality %u, "
1453 "mem %p, pitch %u.\n",
1454 texture
, width
, height
, debug_d3dformat(format_id
), multisample_type
, multisample_quality
, mem
, pitch
);
1457 return WINED3DERR_INVALIDCALL
;
1459 if (texture
->level_count
* texture
->layer_count
> 1)
1461 WARN("Texture has multiple sub-resources, not supported.\n");
1462 return WINED3DERR_INVALIDCALL
;
1465 if (texture
->resource
.type
!= WINED3D_RTYPE_TEXTURE_2D
)
1467 WARN("Not supported on %s.\n", debug_d3dresourcetype(texture
->resource
.type
));
1468 return WINED3DERR_INVALIDCALL
;
1471 if (texture
->resource
.map_count
)
1473 WARN("Texture is mapped.\n");
1474 return WINED3DERR_INVALIDCALL
;
1477 /* We have no way of supporting a pitch that is not a multiple of the pixel
1478 * byte width short of uploading the texture row-by-row.
1479 * Fortunately that's not an issue since D3D9Ex doesn't allow a custom pitch
1480 * for user-memory textures (it always expects packed data) while DirectDraw
1481 * requires a 4-byte aligned pitch and doesn't support texture formats
1482 * larger than 4 bytes per pixel nor any format using 3 bytes per pixel.
1483 * This check is here to verify that the assumption holds. */
1484 if (pitch
% texture
->resource
.format
->byte_count
)
1486 WARN("Pitch unsupported, not a multiple of the texture format byte width.\n");
1487 return WINED3DERR_INVALIDCALL
;
1490 if (device
->d3d_initialized
)
1491 wined3d_cs_emit_unload_resource(device
->cs
, &texture
->resource
);
1492 wined3d_resource_wait_idle(&texture
->resource
);
1494 sub_resource
= &texture
->sub_resources
[0];
1495 surface
= sub_resource
->u
.surface
;
1498 wined3d_cs_destroy_object(device
->cs
, texture2d_destroy_dc
, surface
);
1499 device
->cs
->ops
->finish(device
->cs
, WINED3D_CS_QUEUE_DEFAULT
);
1503 wined3d_resource_free_sysmem(&texture
->resource
);
1505 if ((texture
->row_pitch
= pitch
))
1506 texture
->slice_pitch
= height
* pitch
;
1508 /* User memory surfaces don't have the regular surface alignment. */
1509 wined3d_format_calculate_pitch(format
, 1, width
, height
,
1510 &texture
->row_pitch
, &texture
->slice_pitch
);
1512 texture
->resource
.format
= format
;
1513 texture
->resource
.multisample_type
= multisample_type
;
1514 texture
->resource
.multisample_quality
= multisample_quality
;
1515 texture
->resource
.width
= width
;
1516 texture
->resource
.height
= height
;
1517 texture
->resource
.size
= texture
->slice_pitch
;
1518 sub_resource
->size
= texture
->slice_pitch
;
1519 sub_resource
->locations
= WINED3D_LOCATION_DISCARDED
;
1521 if (multisample_type
&& gl_info
->supported
[ARB_TEXTURE_MULTISAMPLE
])
1522 texture
->target
= GL_TEXTURE_2D_MULTISAMPLE
;
1524 texture
->target
= GL_TEXTURE_2D
;
1526 if (((width
& (width
- 1)) || (height
& (height
- 1))) && !gl_info
->supported
[ARB_TEXTURE_NON_POWER_OF_TWO
]
1527 && !gl_info
->supported
[ARB_TEXTURE_RECTANGLE
] && !gl_info
->supported
[WINED3D_GL_NORMALIZED_TEXRECT
])
1529 texture
->flags
|= WINED3D_TEXTURE_COND_NP2_EMULATED
;
1530 texture
->pow2_width
= texture
->pow2_height
= 1;
1531 while (texture
->pow2_width
< width
)
1532 texture
->pow2_width
<<= 1;
1533 while (texture
->pow2_height
< height
)
1534 texture
->pow2_height
<<= 1;
1538 texture
->flags
&= ~WINED3D_TEXTURE_COND_NP2_EMULATED
;
1539 texture
->pow2_width
= width
;
1540 texture
->pow2_height
= height
;
1543 if ((texture
->user_memory
= mem
))
1545 texture
->resource
.map_binding
= WINED3D_LOCATION_USER_MEMORY
;
1546 valid_location
= WINED3D_LOCATION_USER_MEMORY
;
1550 wined3d_texture_prepare_location(texture
, 0, NULL
, WINED3D_LOCATION_SYSMEM
);
1551 valid_location
= WINED3D_LOCATION_SYSMEM
;
1554 /* The format might be changed to a format that needs conversion.
1555 * If the surface didn't use PBOs previously but could now, don't
1556 * change it - whatever made us not use PBOs might come back, e.g.
1558 if (texture
->resource
.map_binding
== WINED3D_LOCATION_BUFFER
&& !wined3d_texture_use_pbo(texture
, gl_info
))
1559 texture
->resource
.map_binding
= WINED3D_LOCATION_SYSMEM
;
1561 wined3d_texture_validate_location(texture
, 0, valid_location
);
1562 wined3d_texture_invalidate_location(texture
, 0, ~valid_location
);
1566 wined3d_cs_init_object(device
->cs
, texture2d_create_dc
, surface
);
1567 device
->cs
->ops
->finish(device
->cs
, WINED3D_CS_QUEUE_DEFAULT
);
1573 /* Context activation is done by the caller. */
1574 static void wined3d_texture_prepare_buffer_object(struct wined3d_texture
*texture
,
1575 unsigned int sub_resource_idx
, const struct wined3d_gl_info
*gl_info
)
1577 struct wined3d_texture_sub_resource
*sub_resource
;
1579 sub_resource
= &texture
->sub_resources
[sub_resource_idx
];
1580 if (sub_resource
->buffer_object
)
1583 GL_EXTCALL(glGenBuffers(1, &sub_resource
->buffer_object
));
1584 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER
, sub_resource
->buffer_object
));
1585 GL_EXTCALL(glBufferData(GL_PIXEL_UNPACK_BUFFER
, sub_resource
->size
, NULL
, GL_STREAM_DRAW
));
1586 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER
, 0));
1587 checkGLcall("Create buffer object");
1589 TRACE("Created buffer object %u for texture %p, sub-resource %u.\n",
1590 sub_resource
->buffer_object
, texture
, sub_resource_idx
);
1593 static void wined3d_texture_force_reload(struct wined3d_texture
*texture
)
1595 unsigned int sub_count
= texture
->level_count
* texture
->layer_count
;
1598 texture
->flags
&= ~(WINED3D_TEXTURE_RGB_ALLOCATED
| WINED3D_TEXTURE_SRGB_ALLOCATED
1599 | WINED3D_TEXTURE_CONVERTED
);
1600 texture
->async
.flags
&= ~WINED3D_TEXTURE_ASYNC_COLOR_KEY
;
1601 for (i
= 0; i
< sub_count
; ++i
)
1603 wined3d_texture_invalidate_location(texture
, i
,
1604 WINED3D_LOCATION_TEXTURE_RGB
| WINED3D_LOCATION_TEXTURE_SRGB
);
1608 void wined3d_texture_prepare_texture(struct wined3d_texture
*texture
, struct wined3d_context
*context
, BOOL srgb
)
1610 DWORD alloc_flag
= srgb
? WINED3D_TEXTURE_SRGB_ALLOCATED
: WINED3D_TEXTURE_RGB_ALLOCATED
;
1611 const struct wined3d_d3d_info
*d3d_info
= context
->d3d_info
;
1613 if (!d3d_info
->shader_color_key
1614 && !(texture
->async
.flags
& WINED3D_TEXTURE_ASYNC_COLOR_KEY
)
1615 != !(texture
->async
.color_key_flags
& WINED3D_CKEY_SRC_BLT
))
1617 wined3d_texture_force_reload(texture
);
1619 if (texture
->async
.color_key_flags
& WINED3D_CKEY_SRC_BLT
)
1620 texture
->async
.flags
|= WINED3D_TEXTURE_ASYNC_COLOR_KEY
;
1623 if (texture
->flags
& alloc_flag
)
1626 texture
->texture_ops
->texture_prepare_texture(texture
, context
, srgb
);
1627 texture
->flags
|= alloc_flag
;
1630 static void wined3d_texture_prepare_rb(struct wined3d_texture
*texture
,
1631 const struct wined3d_gl_info
*gl_info
, BOOL multisample
)
1633 const struct wined3d_format
*format
= texture
->resource
.format
;
1639 if (texture
->rb_multisample
)
1642 samples
= wined3d_texture_get_gl_sample_count(texture
);
1644 gl_info
->fbo_ops
.glGenRenderbuffers(1, &texture
->rb_multisample
);
1645 gl_info
->fbo_ops
.glBindRenderbuffer(GL_RENDERBUFFER
, texture
->rb_multisample
);
1646 gl_info
->fbo_ops
.glRenderbufferStorageMultisample(GL_RENDERBUFFER
, samples
,
1647 format
->glInternal
, texture
->resource
.width
, texture
->resource
.height
);
1648 checkGLcall("glRenderbufferStorageMultisample()");
1649 TRACE("Created multisample rb %u.\n", texture
->rb_multisample
);
1653 if (texture
->rb_resolved
)
1656 gl_info
->fbo_ops
.glGenRenderbuffers(1, &texture
->rb_resolved
);
1657 gl_info
->fbo_ops
.glBindRenderbuffer(GL_RENDERBUFFER
, texture
->rb_resolved
);
1658 gl_info
->fbo_ops
.glRenderbufferStorage(GL_RENDERBUFFER
, format
->glInternal
,
1659 texture
->resource
.width
, texture
->resource
.height
);
1660 checkGLcall("glRenderbufferStorage()");
1661 TRACE("Created resolved rb %u.\n", texture
->rb_resolved
);
1665 /* Context activation is done by the caller. Context may be NULL in
1666 * WINED3D_NO3D mode. */
1667 BOOL
wined3d_texture_prepare_location(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
,
1668 struct wined3d_context
*context
, DWORD location
)
1672 case WINED3D_LOCATION_SYSMEM
:
1673 if (texture
->resource
.heap_memory
)
1676 if (!wined3d_resource_allocate_sysmem(&texture
->resource
))
1678 ERR("Failed to allocate system memory.\n");
1683 case WINED3D_LOCATION_USER_MEMORY
:
1684 if (!texture
->user_memory
)
1685 ERR("Map binding is set to WINED3D_LOCATION_USER_MEMORY but surface->user_memory is NULL.\n");
1688 case WINED3D_LOCATION_BUFFER
:
1689 wined3d_texture_prepare_buffer_object(texture
, sub_resource_idx
, context
->gl_info
);
1692 case WINED3D_LOCATION_TEXTURE_RGB
:
1693 wined3d_texture_prepare_texture(texture
, context
, FALSE
);
1696 case WINED3D_LOCATION_TEXTURE_SRGB
:
1697 wined3d_texture_prepare_texture(texture
, context
, TRUE
);
1700 case WINED3D_LOCATION_DRAWABLE
:
1701 if (!texture
->swapchain
&& wined3d_settings
.offscreen_rendering_mode
!= ORM_BACKBUFFER
)
1702 ERR("Texture %p does not have a drawable.\n", texture
);
1705 case WINED3D_LOCATION_RB_MULTISAMPLE
:
1706 wined3d_texture_prepare_rb(texture
, context
->gl_info
, TRUE
);
1709 case WINED3D_LOCATION_RB_RESOLVED
:
1710 wined3d_texture_prepare_rb(texture
, context
->gl_info
, FALSE
);
1714 ERR("Invalid location %s.\n", wined3d_debug_location(location
));
1719 static struct wined3d_texture_sub_resource
*wined3d_texture_get_sub_resource(struct wined3d_texture
*texture
,
1720 unsigned int sub_resource_idx
)
1722 UINT sub_count
= texture
->level_count
* texture
->layer_count
;
1724 TRACE("texture %p, sub_resource_idx %u.\n", texture
, sub_resource_idx
);
1726 if (sub_resource_idx
>= sub_count
)
1728 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx
, sub_count
);
1732 return &texture
->sub_resources
[sub_resource_idx
];
1735 HRESULT CDECL
wined3d_texture_add_dirty_region(struct wined3d_texture
*texture
,
1736 UINT layer
, const struct wined3d_box
*dirty_region
)
1738 TRACE("texture %p, layer %u, dirty_region %s.\n", texture
, layer
, debug_box(dirty_region
));
1740 if (layer
>= texture
->layer_count
)
1742 WARN("Invalid layer %u specified.\n", layer
);
1743 return WINED3DERR_INVALIDCALL
;
1747 FIXME("Ignoring dirty_region %s.\n", debug_box(dirty_region
));
1749 wined3d_cs_emit_add_dirty_texture_region(texture
->resource
.device
->cs
, texture
, layer
);
1754 void wined3d_texture_upload_data(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
,
1755 const struct wined3d_context
*context
, const struct wined3d_box
*box
,
1756 const struct wined3d_const_bo_address
*data
, unsigned int row_pitch
, unsigned int slice_pitch
)
1758 texture
->texture_ops
->texture_upload_data(texture
, sub_resource_idx
,
1759 context
, box
, data
, row_pitch
, slice_pitch
);
1762 static void texture2d_upload_data(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
,
1763 const struct wined3d_context
*context
, const struct wined3d_box
*box
,
1764 const struct wined3d_const_bo_address
*data
, unsigned int row_pitch
, unsigned int slice_pitch
)
1766 unsigned int texture_level
;
1774 dst_point
.x
= box
->left
;
1775 dst_point
.y
= box
->top
;
1776 src_rect
.right
= box
->right
- box
->left
;
1777 src_rect
.bottom
= box
->bottom
- box
->top
;
1781 dst_point
.x
= dst_point
.y
= 0;
1782 texture_level
= sub_resource_idx
% texture
->level_count
;
1783 src_rect
.right
= wined3d_texture_get_level_width(texture
, texture_level
);
1784 src_rect
.bottom
= wined3d_texture_get_level_height(texture
, texture_level
);
1787 wined3d_surface_upload_data(texture
, sub_resource_idx
, context
->gl_info
,
1788 texture
->resource
.format
, &src_rect
, row_pitch
, &dst_point
, FALSE
, data
);
1791 /* Context activation is done by the caller. Context may be NULL in ddraw-only mode. */
1792 static BOOL
texture2d_load_location(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
,
1793 struct wined3d_context
*context
, DWORD location
)
1795 struct wined3d_surface
*surface
;
1797 TRACE("texture %p, sub_resource_idx %u, context %p, location %s.\n",
1798 texture
, sub_resource_idx
, context
, wined3d_debug_location(location
));
1800 surface
= texture
->sub_resources
[sub_resource_idx
].u
.surface
;
1803 case WINED3D_LOCATION_USER_MEMORY
:
1804 case WINED3D_LOCATION_SYSMEM
:
1805 case WINED3D_LOCATION_BUFFER
:
1806 return surface_load_sysmem(surface
, context
, location
);
1808 case WINED3D_LOCATION_DRAWABLE
:
1809 return surface_load_drawable(surface
, context
);
1811 case WINED3D_LOCATION_RB_RESOLVED
:
1812 case WINED3D_LOCATION_RB_MULTISAMPLE
:
1813 return surface_load_renderbuffer(surface
, context
, location
);
1815 case WINED3D_LOCATION_TEXTURE_RGB
:
1816 case WINED3D_LOCATION_TEXTURE_SRGB
:
1817 return surface_load_texture(surface
, context
,
1818 location
== WINED3D_LOCATION_TEXTURE_SRGB
);
1821 ERR("Don't know how to handle location %#x.\n", location
);
1826 /* Context activation is done by the caller. */
1827 static void texture2d_prepare_texture(struct wined3d_texture
*texture
, struct wined3d_context
*context
, BOOL srgb
)
1829 const struct wined3d_format
*format
= texture
->resource
.format
;
1830 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1831 const struct wined3d_color_key_conversion
*conversion
;
1834 TRACE("texture %p, context %p, format %s.\n", texture
, context
, debug_d3dformat(format
->id
));
1836 if (format
->conv_byte_count
)
1838 texture
->flags
|= WINED3D_TEXTURE_CONVERTED
;
1840 else if ((conversion
= wined3d_format_get_color_key_conversion(texture
, TRUE
)))
1842 texture
->flags
|= WINED3D_TEXTURE_CONVERTED
;
1843 format
= wined3d_get_format(gl_info
, conversion
->dst_format
, texture
->resource
.usage
);
1844 TRACE("Using format %s for color key conversion.\n", debug_d3dformat(format
->id
));
1847 wined3d_texture_bind_and_dirtify(texture
, context
, srgb
);
1850 internal
= format
->glGammaInternal
;
1851 else if (texture
->resource
.usage
& WINED3DUSAGE_RENDERTARGET
1852 && wined3d_resource_is_offscreen(&texture
->resource
))
1853 internal
= format
->rtInternal
;
1855 internal
= format
->glInternal
;
1858 FIXME("No GL internal format for format %s.\n", debug_d3dformat(format
->id
));
1860 TRACE("internal %#x, format %#x, type %#x.\n", internal
, format
->glFormat
, format
->glType
);
1862 if (wined3d_texture_use_immutable_storage(texture
, gl_info
))
1863 wined3d_texture_allocate_gl_immutable_storage(texture
, internal
, gl_info
);
1865 wined3d_texture_allocate_gl_mutable_storage(texture
, internal
, format
, gl_info
);
1868 static void texture2d_cleanup_sub_resources(struct wined3d_texture
*texture
)
1870 unsigned int sub_count
= texture
->level_count
* texture
->layer_count
;
1871 struct wined3d_texture_sub_resource
*sub_resource
;
1872 struct wined3d_context
*context
= NULL
;
1873 struct wined3d_surface
*surface
;
1876 for (i
= 0; i
< sub_count
; ++i
)
1878 sub_resource
= &texture
->sub_resources
[i
];
1879 if (!(surface
= sub_resource
->u
.surface
))
1882 TRACE("surface %p.\n", surface
);
1885 texture2d_destroy_dc(surface
);
1888 context_release(context
);
1889 heap_free(texture
->sub_resources
[0].u
.surface
);
1892 static const struct wined3d_texture_ops texture2d_ops
=
1894 texture2d_upload_data
,
1895 texture2d_load_location
,
1896 texture2d_prepare_texture
,
1897 texture2d_cleanup_sub_resources
,
1900 struct wined3d_texture
* __cdecl
wined3d_texture_from_resource(struct wined3d_resource
*resource
)
1902 return texture_from_resource(resource
);
1905 static ULONG
texture_resource_incref(struct wined3d_resource
*resource
)
1907 return wined3d_texture_incref(texture_from_resource(resource
));
1910 static ULONG
texture_resource_decref(struct wined3d_resource
*resource
)
1912 return wined3d_texture_decref(texture_from_resource(resource
));
1915 static void texture_resource_preload(struct wined3d_resource
*resource
)
1917 struct wined3d_texture
*texture
= texture_from_resource(resource
);
1918 struct wined3d_context
*context
;
1920 context
= context_acquire(resource
->device
, NULL
, 0);
1921 wined3d_texture_load(texture
, context
, texture
->flags
& WINED3D_TEXTURE_IS_SRGB
);
1922 context_release(context
);
1925 static void wined3d_texture_unload(struct wined3d_resource
*resource
)
1927 struct wined3d_texture
*texture
= texture_from_resource(resource
);
1928 UINT sub_count
= texture
->level_count
* texture
->layer_count
;
1929 struct wined3d_renderbuffer_entry
*entry
, *entry2
;
1930 struct wined3d_device
*device
= resource
->device
;
1931 const struct wined3d_gl_info
*gl_info
;
1932 struct wined3d_context
*context
;
1935 TRACE("texture %p.\n", texture
);
1937 context
= context_acquire(device
, NULL
, 0);
1938 gl_info
= context
->gl_info
;
1940 for (i
= 0; i
< sub_count
; ++i
)
1942 struct wined3d_texture_sub_resource
*sub_resource
= &texture
->sub_resources
[i
];
1944 if (resource
->access
& WINED3D_RESOURCE_ACCESS_CPU
1945 && wined3d_texture_load_location(texture
, i
, context
, resource
->map_binding
))
1947 wined3d_texture_invalidate_location(texture
, i
, ~resource
->map_binding
);
1951 /* We should only get here on device reset/teardown for implicit
1953 if (resource
->access
& WINED3D_RESOURCE_ACCESS_CPU
1954 || resource
->type
!= WINED3D_RTYPE_TEXTURE_2D
)
1955 ERR("Discarding %s %p sub-resource %u with resource access %s.\n",
1956 debug_d3dresourcetype(resource
->type
), resource
, i
,
1957 wined3d_debug_resource_access(resource
->access
));
1958 wined3d_texture_validate_location(texture
, i
, WINED3D_LOCATION_DISCARDED
);
1959 wined3d_texture_invalidate_location(texture
, i
, ~WINED3D_LOCATION_DISCARDED
);
1962 if (sub_resource
->buffer_object
)
1963 wined3d_texture_remove_buffer_object(texture
, i
, context
->gl_info
);
1966 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, &texture
->renderbuffers
, struct wined3d_renderbuffer_entry
, entry
)
1968 context_gl_resource_released(device
, entry
->id
, TRUE
);
1969 gl_info
->fbo_ops
.glDeleteRenderbuffers(1, &entry
->id
);
1970 list_remove(&entry
->entry
);
1973 list_init(&texture
->renderbuffers
);
1974 texture
->current_renderbuffer
= NULL
;
1976 context_release(context
);
1978 wined3d_texture_force_reload(texture
);
1979 wined3d_texture_unload_gl_texture(texture
);
1982 static HRESULT
texture_resource_sub_resource_map(struct wined3d_resource
*resource
, unsigned int sub_resource_idx
,
1983 struct wined3d_map_desc
*map_desc
, const struct wined3d_box
*box
, DWORD flags
)
1985 const struct wined3d_format
*format
= resource
->format
;
1986 struct wined3d_texture_sub_resource
*sub_resource
;
1987 struct wined3d_device
*device
= resource
->device
;
1988 unsigned int fmt_flags
= resource
->format_flags
;
1989 struct wined3d_context
*context
= NULL
;
1990 struct wined3d_texture
*texture
;
1991 struct wined3d_bo_address data
;
1992 unsigned int texture_level
;
1996 TRACE("resource %p, sub_resource_idx %u, map_desc %p, box %s, flags %#x.\n",
1997 resource
, sub_resource_idx
, map_desc
, debug_box(box
), flags
);
1999 texture
= texture_from_resource(resource
);
2000 if (!(sub_resource
= wined3d_texture_get_sub_resource(texture
, sub_resource_idx
)))
2001 return E_INVALIDARG
;
2003 texture_level
= sub_resource_idx
% texture
->level_count
;
2004 if (box
&& FAILED(wined3d_texture_check_box_dimensions(texture
, texture_level
, box
)))
2006 WARN("Map box is invalid.\n");
2007 if (((fmt_flags
& WINED3DFMT_FLAG_BLOCKS
) && !(resource
->access
& WINED3D_RESOURCE_ACCESS_CPU
))
2008 || resource
->type
!= WINED3D_RTYPE_TEXTURE_2D
)
2009 return WINED3DERR_INVALIDCALL
;
2012 if (texture
->flags
& WINED3D_TEXTURE_DC_IN_USE
)
2014 WARN("DC is in use.\n");
2015 return WINED3DERR_INVALIDCALL
;
2018 if (sub_resource
->map_count
)
2020 WARN("Sub-resource is already mapped.\n");
2021 return WINED3DERR_INVALIDCALL
;
2024 if (device
->d3d_initialized
)
2025 context
= context_acquire(device
, NULL
, 0);
2027 if (flags
& WINED3D_MAP_DISCARD
)
2029 TRACE("WINED3D_MAP_DISCARD flag passed, marking %s as up to date.\n",
2030 wined3d_debug_location(resource
->map_binding
));
2031 if ((ret
= wined3d_texture_prepare_location(texture
, sub_resource_idx
, context
, resource
->map_binding
)))
2032 wined3d_texture_validate_location(texture
, sub_resource_idx
, resource
->map_binding
);
2036 if (resource
->usage
& WINED3DUSAGE_DYNAMIC
)
2037 WARN_(d3d_perf
)("Mapping a dynamic texture without WINED3D_MAP_DISCARD.\n");
2038 ret
= wined3d_texture_load_location(texture
, sub_resource_idx
, context
, resource
->map_binding
);
2043 ERR("Failed to prepare location.\n");
2044 context_release(context
);
2045 return E_OUTOFMEMORY
;
2048 if (flags
& WINED3D_MAP_WRITE
2049 && (!(flags
& WINED3D_MAP_NO_DIRTY_UPDATE
) || (resource
->usage
& WINED3DUSAGE_DYNAMIC
)))
2050 wined3d_texture_invalidate_location(texture
, sub_resource_idx
, ~resource
->map_binding
);
2052 wined3d_texture_get_memory(texture
, sub_resource_idx
, &data
, resource
->map_binding
);
2053 base_memory
= context_map_bo_address(context
, &data
, sub_resource
->size
, GL_PIXEL_UNPACK_BUFFER
, flags
);
2054 TRACE("Base memory pointer %p.\n", base_memory
);
2057 context_release(context
);
2059 if (fmt_flags
& WINED3DFMT_FLAG_BROKEN_PITCH
)
2061 map_desc
->row_pitch
= wined3d_texture_get_level_width(texture
, texture_level
) * format
->byte_count
;
2062 map_desc
->slice_pitch
= wined3d_texture_get_level_height(texture
, texture_level
) * map_desc
->row_pitch
;
2066 wined3d_texture_get_pitch(texture
, texture_level
, &map_desc
->row_pitch
, &map_desc
->slice_pitch
);
2071 map_desc
->data
= base_memory
;
2075 if ((fmt_flags
& (WINED3DFMT_FLAG_BLOCKS
| WINED3DFMT_FLAG_BROKEN_PITCH
)) == WINED3DFMT_FLAG_BLOCKS
)
2077 /* Compressed textures are block based, so calculate the offset of
2078 * the block that contains the top-left pixel of the mapped box. */
2079 map_desc
->data
= base_memory
2080 + (box
->front
* map_desc
->slice_pitch
)
2081 + ((box
->top
/ format
->block_height
) * map_desc
->row_pitch
)
2082 + ((box
->left
/ format
->block_width
) * format
->block_byte_count
);
2086 map_desc
->data
= base_memory
2087 + (box
->front
* map_desc
->slice_pitch
)
2088 + (box
->top
* map_desc
->row_pitch
)
2089 + (box
->left
* format
->byte_count
);
2093 if (texture
->swapchain
&& texture
->swapchain
->front_buffer
== texture
)
2095 RECT
*r
= &texture
->swapchain
->front_buffer_update
;
2098 SetRect(r
, 0, 0, resource
->width
, resource
->height
);
2100 SetRect(r
, box
->left
, box
->top
, box
->right
, box
->bottom
);
2101 TRACE("Mapped front buffer %s.\n", wine_dbgstr_rect(r
));
2104 ++resource
->map_count
;
2105 ++sub_resource
->map_count
;
2107 TRACE("Returning memory %p, row pitch %u, slice pitch %u.\n",
2108 map_desc
->data
, map_desc
->row_pitch
, map_desc
->slice_pitch
);
2113 static HRESULT
texture_resource_sub_resource_unmap(struct wined3d_resource
*resource
, unsigned int sub_resource_idx
)
2115 struct wined3d_texture_sub_resource
*sub_resource
;
2116 struct wined3d_device
*device
= resource
->device
;
2117 struct wined3d_context
*context
= NULL
;
2118 struct wined3d_texture
*texture
;
2119 struct wined3d_bo_address data
;
2121 TRACE("resource %p, sub_resource_idx %u.\n", resource
, sub_resource_idx
);
2123 texture
= texture_from_resource(resource
);
2124 if (!(sub_resource
= wined3d_texture_get_sub_resource(texture
, sub_resource_idx
)))
2125 return E_INVALIDARG
;
2127 if (!sub_resource
->map_count
)
2129 WARN("Trying to unmap unmapped sub-resource.\n");
2130 if (texture
->flags
& WINED3D_TEXTURE_DC_IN_USE
)
2132 return WINEDDERR_NOTLOCKED
;
2135 if (device
->d3d_initialized
)
2136 context
= context_acquire(device
, NULL
, 0);
2138 wined3d_texture_get_memory(texture
, sub_resource_idx
, &data
, texture
->resource
.map_binding
);
2139 context_unmap_bo_address(context
, &data
, GL_PIXEL_UNPACK_BUFFER
);
2142 context_release(context
);
2144 if (texture
->swapchain
&& texture
->swapchain
->front_buffer
== texture
)
2146 if (!(sub_resource
->locations
& (WINED3D_LOCATION_DRAWABLE
| WINED3D_LOCATION_TEXTURE_RGB
)))
2147 texture
->swapchain
->swapchain_ops
->swapchain_frontbuffer_updated(texture
->swapchain
);
2150 --sub_resource
->map_count
;
2151 if (!--resource
->map_count
&& texture
->update_map_binding
)
2152 wined3d_texture_update_map_binding(texture
);
2157 static const struct wined3d_resource_ops texture_resource_ops
=
2159 texture_resource_incref
,
2160 texture_resource_decref
,
2161 texture_resource_preload
,
2162 wined3d_texture_unload
,
2163 texture_resource_sub_resource_map
,
2164 texture_resource_sub_resource_unmap
,
2167 static HRESULT
texture_init(struct wined3d_texture
*texture
, const struct wined3d_resource_desc
*desc
,
2168 unsigned int layer_count
, unsigned int level_count
, DWORD flags
, struct wined3d_device
*device
,
2169 void *parent
, const struct wined3d_parent_ops
*parent_ops
)
2171 struct wined3d_device_parent
*device_parent
= device
->device_parent
;
2172 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
2173 struct wined3d_surface
*surfaces
;
2174 UINT pow2_width
, pow2_height
;
2175 unsigned int i
, j
, sub_count
;
2178 if (!(desc
->usage
& WINED3DUSAGE_LEGACY_CUBEMAP
) && layer_count
> 1
2179 && !gl_info
->supported
[EXT_TEXTURE_ARRAY
])
2181 WARN("OpenGL implementation does not support array textures.\n");
2182 return WINED3DERR_INVALIDCALL
;
2185 /* TODO: It should only be possible to create textures for formats
2186 * that are reported as supported. */
2187 if (WINED3DFMT_UNKNOWN
>= desc
->format
)
2189 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture
);
2190 return WINED3DERR_INVALIDCALL
;
2193 if (desc
->usage
& WINED3DUSAGE_DYNAMIC
&& wined3d_resource_access_is_managed(desc
->access
))
2194 FIXME("Trying to create a managed texture with dynamic usage.\n");
2195 if (!(desc
->usage
& (WINED3DUSAGE_DYNAMIC
| WINED3DUSAGE_RENDERTARGET
| WINED3DUSAGE_DEPTHSTENCIL
))
2196 && (flags
& WINED3D_TEXTURE_CREATE_MAPPABLE
))
2197 WARN("Creating a mappable texture that doesn't specify dynamic usage.\n");
2198 if (desc
->usage
& WINED3DUSAGE_RENDERTARGET
&& desc
->access
& WINED3D_RESOURCE_ACCESS_CPU
)
2199 FIXME("Trying to create a CPU accessible render target.\n");
2201 pow2_width
= desc
->width
;
2202 pow2_height
= desc
->height
;
2203 if (((desc
->width
& (desc
->width
- 1)) || (desc
->height
& (desc
->height
- 1)))
2204 && !gl_info
->supported
[ARB_TEXTURE_NON_POWER_OF_TWO
])
2206 /* level_count == 0 returns an error as well. */
2207 if (level_count
!= 1 || layer_count
!= 1)
2209 if (!(desc
->usage
& WINED3DUSAGE_SCRATCH
))
2211 WARN("Attempted to create a mipmapped/cube/array NPOT texture without unconditional NPOT support.\n");
2212 return WINED3DERR_INVALIDCALL
;
2215 WARN("Creating a scratch mipmapped/cube/array NPOT texture despite lack of HW support.\n");
2217 texture
->flags
|= WINED3D_TEXTURE_COND_NP2
;
2219 if (!gl_info
->supported
[ARB_TEXTURE_RECTANGLE
] && !gl_info
->supported
[WINED3D_GL_NORMALIZED_TEXRECT
])
2221 const struct wined3d_format
*format
= wined3d_get_format(gl_info
, desc
->format
, desc
->usage
);
2223 /* TODO: Add support for non-power-of-two compressed textures. */
2224 if (format
->flags
[WINED3D_GL_RES_TYPE_TEX_2D
]
2225 & (WINED3DFMT_FLAG_COMPRESSED
| WINED3DFMT_FLAG_HEIGHT_SCALE
))
2227 FIXME("Compressed or height scaled non-power-of-two (%ux%u) textures are not supported.\n",
2228 desc
->width
, desc
->height
);
2229 return WINED3DERR_NOTAVAILABLE
;
2232 /* Find the nearest pow2 match. */
2233 pow2_width
= pow2_height
= 1;
2234 while (pow2_width
< desc
->width
)
2236 while (pow2_height
< desc
->height
)
2238 texture
->flags
|= WINED3D_TEXTURE_COND_NP2_EMULATED
;
2241 texture
->pow2_width
= pow2_width
;
2242 texture
->pow2_height
= pow2_height
;
2244 if ((pow2_width
> gl_info
->limits
.texture_size
|| pow2_height
> gl_info
->limits
.texture_size
)
2245 && (desc
->usage
& WINED3DUSAGE_TEXTURE
))
2247 /* One of four options:
2248 * 1: Do the same as we do with NPOT and scale the texture. (Any
2249 * texture ops would require the texture to be scaled which is
2250 * potentially slow.)
2251 * 2: Set the texture to the maximum size (bad idea).
2252 * 3: WARN and return WINED3DERR_NOTAVAILABLE.
2253 * 4: Create the surface, but allow it to be used only for DirectDraw
2254 * Blts. Some apps (e.g. Swat 3) create textures with a height of
2255 * 16 and a width > 3000 and blt 16x16 letter areas from them to
2256 * the render target. */
2257 if (desc
->access
& WINED3D_RESOURCE_ACCESS_GPU
)
2259 WARN("Dimensions (%ux%u) exceed the maximum texture size.\n", pow2_width
, pow2_height
);
2260 return WINED3DERR_NOTAVAILABLE
;
2263 /* We should never use this surface in combination with OpenGL. */
2264 TRACE("Creating an oversized (%ux%u) surface.\n", pow2_width
, pow2_height
);
2267 if (FAILED(hr
= wined3d_texture_init(texture
, &texture2d_ops
, layer_count
, level_count
, desc
,
2268 flags
, device
, parent
, parent_ops
, &texture_resource_ops
)))
2270 WARN("Failed to initialize texture, returning %#x.\n", hr
);
2274 /* Precalculated scaling for 'faked' non power of two texture coords. */
2275 if (texture
->resource
.gl_type
== WINED3D_GL_RES_TYPE_TEX_RECT
)
2277 texture
->pow2_matrix
[0] = (float)desc
->width
;
2278 texture
->pow2_matrix
[5] = (float)desc
->height
;
2279 texture
->flags
&= ~(WINED3D_TEXTURE_POW2_MAT_IDENT
| WINED3D_TEXTURE_NORMALIZED_COORDS
);
2280 texture
->target
= GL_TEXTURE_RECTANGLE_ARB
;
2284 if (texture
->flags
& WINED3D_TEXTURE_COND_NP2_EMULATED
)
2286 texture
->pow2_matrix
[0] = (((float)desc
->width
) / ((float)pow2_width
));
2287 texture
->pow2_matrix
[5] = (((float)desc
->height
) / ((float)pow2_height
));
2288 texture
->flags
&= ~WINED3D_TEXTURE_POW2_MAT_IDENT
;
2292 texture
->pow2_matrix
[0] = 1.0f
;
2293 texture
->pow2_matrix
[5] = 1.0f
;
2295 if (desc
->usage
& WINED3DUSAGE_LEGACY_CUBEMAP
)
2297 texture
->target
= GL_TEXTURE_CUBE_MAP_ARB
;
2299 else if (desc
->multisample_type
&& gl_info
->supported
[ARB_TEXTURE_MULTISAMPLE
])
2301 if (layer_count
> 1)
2302 texture
->target
= GL_TEXTURE_2D_MULTISAMPLE_ARRAY
;
2304 texture
->target
= GL_TEXTURE_2D_MULTISAMPLE
;
2308 if (layer_count
> 1)
2309 texture
->target
= GL_TEXTURE_2D_ARRAY
;
2311 texture
->target
= GL_TEXTURE_2D
;
2314 texture
->pow2_matrix
[10] = 1.0f
;
2315 texture
->pow2_matrix
[15] = 1.0f
;
2316 TRACE("x scale %.8e, y scale %.8e.\n", texture
->pow2_matrix
[0], texture
->pow2_matrix
[5]);
2318 if (wined3d_texture_use_pbo(texture
, gl_info
))
2319 texture
->resource
.map_binding
= WINED3D_LOCATION_BUFFER
;
2321 sub_count
= level_count
* layer_count
;
2322 if (sub_count
/ layer_count
!= level_count
2323 || !(surfaces
= heap_calloc(sub_count
, sizeof(*surfaces
))))
2325 wined3d_texture_cleanup_sync(texture
);
2326 return E_OUTOFMEMORY
;
2329 if (desc
->usage
& WINED3DUSAGE_OVERLAY
)
2331 if (!(texture
->overlay_info
= heap_calloc(sub_count
, sizeof(*texture
->overlay_info
))))
2333 heap_free(surfaces
);
2334 wined3d_texture_cleanup_sync(texture
);
2335 return E_OUTOFMEMORY
;
2338 for (i
= 0; i
< sub_count
; ++i
)
2340 list_init(&texture
->overlay_info
[i
].entry
);
2341 list_init(&texture
->overlay_info
[i
].overlays
);
2345 /* Generate all the surfaces. */
2346 for (i
= 0; i
< texture
->level_count
; ++i
)
2348 for (j
= 0; j
< texture
->layer_count
; ++j
)
2350 struct wined3d_texture_sub_resource
*sub_resource
;
2351 unsigned int idx
= j
* texture
->level_count
+ i
;
2352 struct wined3d_surface
*surface
;
2354 surface
= &surfaces
[idx
];
2355 surface
->container
= texture
;
2356 surface
->texture_level
= i
;
2357 surface
->texture_layer
= j
;
2359 sub_resource
= &texture
->sub_resources
[idx
];
2360 sub_resource
->locations
= WINED3D_LOCATION_DISCARDED
;
2361 sub_resource
->u
.surface
= surface
;
2362 if (!(texture
->resource
.usage
& WINED3DUSAGE_DEPTHSTENCIL
))
2364 wined3d_texture_validate_location(texture
, idx
, WINED3D_LOCATION_SYSMEM
);
2365 wined3d_texture_invalidate_location(texture
, idx
, ~WINED3D_LOCATION_SYSMEM
);
2368 if (FAILED(hr
= device_parent
->ops
->surface_created(device_parent
,
2369 texture
, idx
, &sub_resource
->parent
, &sub_resource
->parent_ops
)))
2371 WARN("Failed to create surface parent, hr %#x.\n", hr
);
2372 sub_resource
->parent
= NULL
;
2373 wined3d_texture_cleanup_sync(texture
);
2377 TRACE("parent %p, parent_ops %p.\n", sub_resource
->parent
, sub_resource
->parent_ops
);
2379 TRACE("Created surface level %u, layer %u @ %p.\n", i
, j
, surface
);
2381 if ((desc
->usage
& WINED3DUSAGE_OWNDC
) || (device
->wined3d
->flags
& WINED3D_NO3D
))
2383 wined3d_cs_init_object(device
->cs
, texture2d_create_dc
, surface
);
2384 device
->cs
->ops
->finish(device
->cs
, WINED3D_CS_QUEUE_DEFAULT
);
2387 wined3d_texture_cleanup_sync(texture
);
2388 return WINED3DERR_INVALIDCALL
;
2397 /* This call just uploads data, the caller is responsible for binding the
2398 * correct texture. */
2399 /* Context activation is done by the caller. */
2400 static void texture3d_upload_data(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
,
2401 const struct wined3d_context
*context
, const struct wined3d_box
*box
,
2402 const struct wined3d_const_bo_address
*data
, unsigned int row_pitch
, unsigned int slice_pitch
)
2404 const struct wined3d_format
*format
= texture
->resource
.format
;
2405 unsigned int level
= sub_resource_idx
% texture
->level_count
;
2406 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2407 unsigned int x
, y
, z
, update_w
, update_h
, update_d
;
2408 unsigned int dst_row_pitch
, dst_slice_pitch
;
2409 unsigned int width
, height
, depth
;
2410 const void *mem
= data
->addr
;
2411 void *converted_mem
= NULL
;
2413 TRACE("texture %p, sub_resource_idx %u, context %p, box %s, data {%#x:%p}, row_pitch %#x, slice_pitch %#x.\n",
2414 texture
, sub_resource_idx
, context
, debug_box(box
),
2415 data
->buffer_object
, data
->addr
, row_pitch
, slice_pitch
);
2417 width
= wined3d_texture_get_level_width(texture
, level
);
2418 height
= wined3d_texture_get_level_height(texture
, level
);
2419 depth
= wined3d_texture_get_level_depth(texture
, level
);
2433 update_w
= box
->right
- box
->left
;
2434 update_h
= box
->bottom
- box
->top
;
2435 update_d
= box
->back
- box
->front
;
2438 if (format
->conv_byte_count
)
2440 if (data
->buffer_object
)
2441 ERR("Loading a converted texture from a PBO.\n");
2442 if (texture
->resource
.format_flags
& WINED3DFMT_FLAG_BLOCKS
)
2443 ERR("Converting a block-based format.\n");
2445 dst_row_pitch
= update_w
* format
->conv_byte_count
;
2446 dst_slice_pitch
= dst_row_pitch
* update_h
;
2448 converted_mem
= heap_calloc(update_d
, dst_slice_pitch
);
2449 format
->upload(data
->addr
, converted_mem
, row_pitch
, slice_pitch
,
2450 dst_row_pitch
, dst_slice_pitch
, update_w
, update_h
, update_d
);
2451 mem
= converted_mem
;
2455 wined3d_texture_get_pitch(texture
, sub_resource_idx
, &dst_row_pitch
, &dst_slice_pitch
);
2456 if (row_pitch
!= dst_row_pitch
|| slice_pitch
!= dst_slice_pitch
)
2457 FIXME("Ignoring row/slice pitch (%u/%u).\n", row_pitch
, slice_pitch
);
2460 if (data
->buffer_object
)
2462 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER
, data
->buffer_object
));
2463 checkGLcall("glBindBuffer");
2466 GL_EXTCALL(glTexSubImage3D(GL_TEXTURE_3D
, level
, x
, y
, z
,
2467 update_w
, update_h
, update_d
, format
->glFormat
, format
->glType
, mem
));
2468 checkGLcall("glTexSubImage3D");
2470 if (data
->buffer_object
)
2472 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER
, 0));
2473 checkGLcall("glBindBuffer");
2476 heap_free(converted_mem
);
2479 /* Context activation is done by the caller. */
2480 static void texture3d_download_data(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
,
2481 const struct wined3d_context
*context
, const struct wined3d_bo_address
*data
)
2483 const struct wined3d_format
*format
= texture
->resource
.format
;
2484 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2486 if (format
->conv_byte_count
)
2488 FIXME("Attempting to download a converted volume, format %s.\n",
2489 debug_d3dformat(format
->id
));
2493 if (data
->buffer_object
)
2495 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER
, data
->buffer_object
));
2496 checkGLcall("glBindBuffer");
2499 gl_info
->gl_ops
.gl
.p_glGetTexImage(GL_TEXTURE_3D
, sub_resource_idx
,
2500 format
->glFormat
, format
->glType
, data
->addr
);
2501 checkGLcall("glGetTexImage");
2503 if (data
->buffer_object
)
2505 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER
, 0));
2506 checkGLcall("glBindBuffer");
2511 /* Context activation is done by the caller. */
2512 static void texture3d_srgb_transfer(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
,
2513 struct wined3d_context
*context
, BOOL dest_is_srgb
)
2515 struct wined3d_texture_sub_resource
*sub_resource
= &texture
->sub_resources
[sub_resource_idx
];
2516 unsigned int row_pitch
, slice_pitch
;
2517 struct wined3d_bo_address data
;
2519 /* Optimisations are possible, but the effort should be put into either
2520 * implementing EXT_SRGB_DECODE in the driver or finding out why we
2521 * picked the wrong copy for the original upload and fixing that.
2523 * Also keep in mind that we want to avoid using resource.heap_memory
2524 * for DEFAULT pool surfaces. */
2525 WARN_(d3d_perf
)("Performing slow rgb/srgb volume transfer.\n");
2526 data
.buffer_object
= 0;
2527 if (!(data
.addr
= heap_alloc(sub_resource
->size
)))
2530 wined3d_texture_get_pitch(texture
, sub_resource_idx
, &row_pitch
, &slice_pitch
);
2531 wined3d_texture_bind_and_dirtify(texture
, context
, !dest_is_srgb
);
2532 texture3d_download_data(texture
, sub_resource_idx
, context
, &data
);
2533 wined3d_texture_bind_and_dirtify(texture
, context
, dest_is_srgb
);
2534 texture3d_upload_data(texture
, sub_resource_idx
, context
,
2535 NULL
, wined3d_const_bo_address(&data
), row_pitch
, slice_pitch
);
2537 heap_free(data
.addr
);
2540 /* Context activation is done by the caller. */
2541 static BOOL
texture3d_load_location(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
,
2542 struct wined3d_context
*context
, DWORD location
)
2544 struct wined3d_texture_sub_resource
*sub_resource
= &texture
->sub_resources
[sub_resource_idx
];
2545 unsigned int row_pitch
, slice_pitch
;
2547 if (!wined3d_texture_prepare_location(texture
, sub_resource_idx
, context
, location
))
2552 case WINED3D_LOCATION_TEXTURE_RGB
:
2553 case WINED3D_LOCATION_TEXTURE_SRGB
:
2554 if (sub_resource
->locations
& WINED3D_LOCATION_SYSMEM
)
2556 struct wined3d_const_bo_address data
= {0, texture
->resource
.heap_memory
};
2557 data
.addr
+= sub_resource
->offset
;
2558 wined3d_texture_bind_and_dirtify(texture
, context
,
2559 location
== WINED3D_LOCATION_TEXTURE_SRGB
);
2560 wined3d_texture_get_pitch(texture
, sub_resource_idx
, &row_pitch
, &slice_pitch
);
2561 texture3d_upload_data(texture
, sub_resource_idx
, context
, NULL
, &data
, row_pitch
, slice_pitch
);
2563 else if (sub_resource
->locations
& WINED3D_LOCATION_BUFFER
)
2565 struct wined3d_const_bo_address data
= {sub_resource
->buffer_object
, NULL
};
2566 wined3d_texture_bind_and_dirtify(texture
, context
,
2567 location
== WINED3D_LOCATION_TEXTURE_SRGB
);
2568 wined3d_texture_get_pitch(texture
, sub_resource_idx
, &row_pitch
, &slice_pitch
);
2569 texture3d_upload_data(texture
, sub_resource_idx
, context
, NULL
, &data
, row_pitch
, slice_pitch
);
2571 else if (sub_resource
->locations
& WINED3D_LOCATION_TEXTURE_RGB
)
2573 texture3d_srgb_transfer(texture
, sub_resource_idx
, context
, TRUE
);
2575 else if (sub_resource
->locations
& WINED3D_LOCATION_TEXTURE_SRGB
)
2577 texture3d_srgb_transfer(texture
, sub_resource_idx
, context
, FALSE
);
2581 FIXME("Implement texture loading from %s.\n", wined3d_debug_location(sub_resource
->locations
));
2586 case WINED3D_LOCATION_SYSMEM
:
2587 if (sub_resource
->locations
& (WINED3D_LOCATION_TEXTURE_RGB
| WINED3D_LOCATION_TEXTURE_SRGB
))
2589 struct wined3d_bo_address data
= {0, texture
->resource
.heap_memory
};
2591 data
.addr
+= sub_resource
->offset
;
2592 if (sub_resource
->locations
& WINED3D_LOCATION_TEXTURE_RGB
)
2593 wined3d_texture_bind_and_dirtify(texture
, context
, FALSE
);
2595 wined3d_texture_bind_and_dirtify(texture
, context
, TRUE
);
2597 texture3d_download_data(texture
, sub_resource_idx
, context
, &data
);
2598 ++texture
->download_count
;
2602 FIXME("Implement WINED3D_LOCATION_SYSMEM loading from %s.\n",
2603 wined3d_debug_location(sub_resource
->locations
));
2608 case WINED3D_LOCATION_BUFFER
:
2609 if (sub_resource
->locations
& (WINED3D_LOCATION_TEXTURE_RGB
| WINED3D_LOCATION_TEXTURE_SRGB
))
2611 struct wined3d_bo_address data
= {sub_resource
->buffer_object
, NULL
};
2613 if (sub_resource
->locations
& WINED3D_LOCATION_TEXTURE_RGB
)
2614 wined3d_texture_bind_and_dirtify(texture
, context
, FALSE
);
2616 wined3d_texture_bind_and_dirtify(texture
, context
, TRUE
);
2618 texture3d_download_data(texture
, sub_resource_idx
, context
, &data
);
2622 FIXME("Implement WINED3D_LOCATION_BUFFER loading from %s.\n",
2623 wined3d_debug_location(sub_resource
->locations
));
2629 FIXME("Implement %s loading from %s.\n", wined3d_debug_location(location
),
2630 wined3d_debug_location(sub_resource
->locations
));
2637 static void texture3d_prepare_texture(struct wined3d_texture
*texture
, struct wined3d_context
*context
, BOOL srgb
)
2639 const struct wined3d_format
*format
= texture
->resource
.format
;
2640 GLenum internal
= srgb
? format
->glGammaInternal
: format
->glInternal
;
2641 unsigned int sub_count
= texture
->level_count
* texture
->layer_count
;
2642 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2645 wined3d_texture_bind_and_dirtify(texture
, context
, srgb
);
2647 if (wined3d_texture_use_immutable_storage(texture
, gl_info
))
2649 GL_EXTCALL(glTexStorage3D(GL_TEXTURE_3D
, texture
->level_count
, internal
,
2650 wined3d_texture_get_level_width(texture
, 0),
2651 wined3d_texture_get_level_height(texture
, 0),
2652 wined3d_texture_get_level_depth(texture
, 0)));
2653 checkGLcall("glTexStorage3D");
2657 for (i
= 0; i
< sub_count
; ++i
)
2659 GL_EXTCALL(glTexImage3D(GL_TEXTURE_3D
, i
, internal
,
2660 wined3d_texture_get_level_width(texture
, i
),
2661 wined3d_texture_get_level_height(texture
, i
),
2662 wined3d_texture_get_level_depth(texture
, i
),
2663 0, format
->glFormat
, format
->glType
, NULL
));
2664 checkGLcall("glTexImage3D");
2669 static void texture3d_cleanup_sub_resources(struct wined3d_texture
*texture
)
2673 static const struct wined3d_texture_ops texture3d_ops
=
2675 texture3d_upload_data
,
2676 texture3d_load_location
,
2677 texture3d_prepare_texture
,
2678 texture3d_cleanup_sub_resources
,
2681 static HRESULT
volumetexture_init(struct wined3d_texture
*texture
, const struct wined3d_resource_desc
*desc
,
2682 UINT layer_count
, UINT level_count
, DWORD flags
, struct wined3d_device
*device
, void *parent
,
2683 const struct wined3d_parent_ops
*parent_ops
)
2685 struct wined3d_device_parent
*device_parent
= device
->device_parent
;
2686 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
2690 if (layer_count
!= 1)
2692 ERR("Invalid layer count for volume texture.\n");
2693 return E_INVALIDARG
;
2696 /* TODO: It should only be possible to create textures for formats
2697 * that are reported as supported. */
2698 if (WINED3DFMT_UNKNOWN
>= desc
->format
)
2700 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture
);
2701 return WINED3DERR_INVALIDCALL
;
2704 if (!gl_info
->supported
[EXT_TEXTURE3D
])
2706 WARN("(%p) : Texture cannot be created - no volume texture support.\n", texture
);
2707 return WINED3DERR_INVALIDCALL
;
2710 if (desc
->usage
& WINED3DUSAGE_DYNAMIC
&& (wined3d_resource_access_is_managed(desc
->access
)
2711 || desc
->usage
& WINED3DUSAGE_SCRATCH
))
2713 WARN("Attempted to create a DYNAMIC texture with access %s.\n",
2714 wined3d_debug_resource_access(desc
->access
));
2715 return WINED3DERR_INVALIDCALL
;
2718 if (!gl_info
->supported
[ARB_TEXTURE_NON_POWER_OF_TWO
])
2720 UINT pow2_w
, pow2_h
, pow2_d
;
2722 while (pow2_w
< desc
->width
)
2725 while (pow2_h
< desc
->height
)
2728 while (pow2_d
< desc
->depth
)
2731 if (pow2_w
!= desc
->width
|| pow2_h
!= desc
->height
|| pow2_d
!= desc
->depth
)
2733 if (desc
->usage
& WINED3DUSAGE_SCRATCH
)
2735 WARN("Creating a scratch NPOT volume texture despite lack of HW support.\n");
2739 WARN("Attempted to create a NPOT volume texture (%u, %u, %u) without GL support.\n",
2740 desc
->width
, desc
->height
, desc
->depth
);
2741 return WINED3DERR_INVALIDCALL
;
2746 if (FAILED(hr
= wined3d_texture_init(texture
, &texture3d_ops
, 1, level_count
, desc
,
2747 flags
, device
, parent
, parent_ops
, &texture_resource_ops
)))
2749 WARN("Failed to initialize texture, returning %#x.\n", hr
);
2753 texture
->pow2_matrix
[0] = 1.0f
;
2754 texture
->pow2_matrix
[5] = 1.0f
;
2755 texture
->pow2_matrix
[10] = 1.0f
;
2756 texture
->pow2_matrix
[15] = 1.0f
;
2757 texture
->target
= GL_TEXTURE_3D
;
2759 if (wined3d_texture_use_pbo(texture
, gl_info
))
2761 wined3d_resource_free_sysmem(&texture
->resource
);
2762 texture
->resource
.map_binding
= WINED3D_LOCATION_BUFFER
;
2765 /* Generate all the sub resources. */
2766 for (i
= 0; i
< texture
->level_count
; ++i
)
2768 struct wined3d_texture_sub_resource
*sub_resource
;
2770 sub_resource
= &texture
->sub_resources
[i
];
2771 sub_resource
->locations
= WINED3D_LOCATION_DISCARDED
;
2773 if (FAILED(hr
= device_parent
->ops
->volume_created(device_parent
,
2774 texture
, i
, &sub_resource
->parent
, &sub_resource
->parent_ops
)))
2776 WARN("Failed to create volume parent, hr %#x.\n", hr
);
2777 sub_resource
->parent
= NULL
;
2778 wined3d_texture_cleanup_sync(texture
);
2782 TRACE("parent %p, parent_ops %p.\n", parent
, parent_ops
);
2784 TRACE("Created volume level %u.\n", i
);
2790 HRESULT CDECL
wined3d_texture_blt(struct wined3d_texture
*dst_texture
, unsigned int dst_sub_resource_idx
,
2791 const RECT
*dst_rect
, struct wined3d_texture
*src_texture
, unsigned int src_sub_resource_idx
,
2792 const RECT
*src_rect
, DWORD flags
, const struct wined3d_blt_fx
*fx
, enum wined3d_texture_filter_type filter
)
2794 struct wined3d_box src_box
= {src_rect
->left
, src_rect
->top
, src_rect
->right
, src_rect
->bottom
, 0, 1};
2795 struct wined3d_box dst_box
= {dst_rect
->left
, dst_rect
->top
, dst_rect
->right
, dst_rect
->bottom
, 0, 1};
2796 unsigned int dst_format_flags
, src_format_flags
= 0;
2799 TRACE("dst_texture %p, dst_sub_resource_idx %u, dst_rect %s, src_texture %p, "
2800 "src_sub_resource_idx %u, src_rect %s, flags %#x, fx %p, filter %s.\n",
2801 dst_texture
, dst_sub_resource_idx
, wine_dbgstr_rect(dst_rect
), src_texture
,
2802 src_sub_resource_idx
, wine_dbgstr_rect(src_rect
), flags
, fx
, debug_d3dtexturefiltertype(filter
));
2804 if (dst_sub_resource_idx
>= dst_texture
->level_count
* dst_texture
->layer_count
2805 || dst_texture
->resource
.type
!= WINED3D_RTYPE_TEXTURE_2D
)
2806 return WINED3DERR_INVALIDCALL
;
2808 if (src_sub_resource_idx
>= src_texture
->level_count
* src_texture
->layer_count
2809 || src_texture
->resource
.type
!= WINED3D_RTYPE_TEXTURE_2D
)
2810 return WINED3DERR_INVALIDCALL
;
2812 dst_format_flags
= dst_texture
->resource
.format_flags
;
2813 if (FAILED(hr
= wined3d_texture_check_box_dimensions(dst_texture
,
2814 dst_sub_resource_idx
% dst_texture
->level_count
, &dst_box
)))
2817 src_format_flags
= src_texture
->resource
.format_flags
;
2818 if (FAILED(hr
= wined3d_texture_check_box_dimensions(src_texture
,
2819 src_sub_resource_idx
% src_texture
->level_count
, &src_box
)))
2822 if (dst_texture
->sub_resources
[dst_sub_resource_idx
].map_count
2823 || src_texture
->sub_resources
[src_sub_resource_idx
].map_count
)
2825 WARN("Sub-resource is busy, returning WINEDDERR_SURFACEBUSY.\n");
2826 return WINEDDERR_SURFACEBUSY
;
2829 if ((src_format_flags
& (WINED3DFMT_FLAG_DEPTH
| WINED3DFMT_FLAG_STENCIL
))
2830 != (dst_format_flags
& (WINED3DFMT_FLAG_DEPTH
| WINED3DFMT_FLAG_STENCIL
)))
2832 WARN("Rejecting depth/stencil blit between incompatible formats.\n");
2833 return WINED3DERR_INVALIDCALL
;
2836 wined3d_cs_emit_blt_sub_resource(dst_texture
->resource
.device
->cs
, &dst_texture
->resource
, dst_sub_resource_idx
,
2837 &dst_box
, &src_texture
->resource
, src_sub_resource_idx
, &src_box
, flags
, fx
, filter
);
2842 HRESULT CDECL
wined3d_texture_get_overlay_position(const struct wined3d_texture
*texture
,
2843 unsigned int sub_resource_idx
, LONG
*x
, LONG
*y
)
2845 struct wined3d_overlay_info
*overlay
;
2847 TRACE("texture %p, sub_resource_idx %u, x %p, y %p.\n", texture
, sub_resource_idx
, x
, y
);
2849 if (!(texture
->resource
.usage
& WINED3DUSAGE_OVERLAY
)
2850 || sub_resource_idx
>= texture
->level_count
* texture
->layer_count
)
2852 WARN("Invalid sub-resource specified.\n");
2853 return WINEDDERR_NOTAOVERLAYSURFACE
;
2856 overlay
= &texture
->overlay_info
[sub_resource_idx
];
2859 TRACE("Overlay not visible.\n");
2862 return WINEDDERR_OVERLAYNOTVISIBLE
;
2865 *x
= overlay
->dst_rect
.left
;
2866 *y
= overlay
->dst_rect
.top
;
2868 TRACE("Returning position %d, %d.\n", *x
, *y
);
2873 HRESULT CDECL
wined3d_texture_set_overlay_position(struct wined3d_texture
*texture
,
2874 unsigned int sub_resource_idx
, LONG x
, LONG y
)
2876 struct wined3d_overlay_info
*overlay
;
2879 TRACE("texture %p, sub_resource_idx %u, x %d, y %d.\n", texture
, sub_resource_idx
, x
, y
);
2881 if (!(texture
->resource
.usage
& WINED3DUSAGE_OVERLAY
)
2882 || sub_resource_idx
>= texture
->level_count
* texture
->layer_count
)
2884 WARN("Invalid sub-resource specified.\n");
2885 return WINEDDERR_NOTAOVERLAYSURFACE
;
2888 overlay
= &texture
->overlay_info
[sub_resource_idx
];
2889 w
= overlay
->dst_rect
.right
- overlay
->dst_rect
.left
;
2890 h
= overlay
->dst_rect
.bottom
- overlay
->dst_rect
.top
;
2891 SetRect(&overlay
->dst_rect
, x
, y
, x
+ w
, y
+ h
);
2896 HRESULT CDECL
wined3d_texture_update_overlay(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
,
2897 const RECT
*src_rect
, struct wined3d_texture
*dst_texture
, unsigned int dst_sub_resource_idx
,
2898 const RECT
*dst_rect
, DWORD flags
)
2900 struct wined3d_texture_sub_resource
*dst_sub_resource
;
2901 struct wined3d_overlay_info
*overlay
;
2902 struct wined3d_surface
*dst_surface
;
2903 unsigned int level
, dst_level
;
2905 TRACE("texture %p, sub_resource_idx %u, src_rect %s, dst_texture %p, "
2906 "dst_sub_resource_idx %u, dst_rect %s, flags %#x.\n",
2907 texture
, sub_resource_idx
, wine_dbgstr_rect(src_rect
), dst_texture
,
2908 dst_sub_resource_idx
, wine_dbgstr_rect(dst_rect
), flags
);
2910 if (!(texture
->resource
.usage
& WINED3DUSAGE_OVERLAY
) || texture
->resource
.type
!= WINED3D_RTYPE_TEXTURE_2D
2911 || sub_resource_idx
>= texture
->level_count
* texture
->layer_count
)
2913 WARN("Invalid sub-resource specified.\n");
2914 return WINEDDERR_NOTAOVERLAYSURFACE
;
2917 if (!dst_texture
|| dst_texture
->resource
.type
!= WINED3D_RTYPE_TEXTURE_2D
2918 || !(dst_sub_resource
= wined3d_texture_get_sub_resource(dst_texture
, dst_sub_resource_idx
)))
2920 WARN("Invalid destination sub-resource specified.\n");
2921 return WINED3DERR_INVALIDCALL
;
2924 overlay
= &texture
->overlay_info
[sub_resource_idx
];
2926 level
= sub_resource_idx
% texture
->level_count
;
2928 overlay
->src_rect
= *src_rect
;
2930 SetRect(&overlay
->src_rect
, 0, 0,
2931 wined3d_texture_get_level_width(texture
, level
),
2932 wined3d_texture_get_level_height(texture
, level
));
2934 dst_surface
= dst_sub_resource
->u
.surface
;
2935 dst_level
= dst_sub_resource_idx
% dst_texture
->level_count
;
2937 overlay
->dst_rect
= *dst_rect
;
2939 SetRect(&overlay
->dst_rect
, 0, 0,
2940 wined3d_texture_get_level_width(dst_texture
, dst_level
),
2941 wined3d_texture_get_level_height(dst_texture
, dst_level
));
2943 if (overlay
->dst
&& (overlay
->dst
!= dst_surface
|| flags
& WINEDDOVER_HIDE
))
2945 overlay
->dst
= NULL
;
2946 list_remove(&overlay
->entry
);
2949 if (flags
& WINEDDOVER_SHOW
)
2951 if (overlay
->dst
!= dst_surface
)
2953 overlay
->dst
= dst_surface
;
2954 list_add_tail(&texture
->overlay_info
[dst_sub_resource_idx
].overlays
, &overlay
->entry
);
2957 else if (flags
& WINEDDOVER_HIDE
)
2959 /* Tests show that the rectangles are erased on hide. */
2960 SetRectEmpty(&overlay
->src_rect
);
2961 SetRectEmpty(&overlay
->dst_rect
);
2962 overlay
->dst
= NULL
;
2968 void * CDECL
wined3d_texture_get_sub_resource_parent(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
)
2970 unsigned int sub_count
= texture
->level_count
* texture
->layer_count
;
2972 TRACE("texture %p, sub_resource_idx %u.\n", texture
, sub_resource_idx
);
2974 if (sub_resource_idx
>= sub_count
)
2976 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx
, sub_count
);
2980 return texture
->sub_resources
[sub_resource_idx
].parent
;
2983 void CDECL
wined3d_texture_set_sub_resource_parent(struct wined3d_texture
*texture
,
2984 unsigned int sub_resource_idx
, void *parent
)
2986 unsigned int sub_count
= texture
->level_count
* texture
->layer_count
;
2988 TRACE("texture %p, sub_resource_idx %u, parent %p.\n", texture
, sub_resource_idx
, parent
);
2990 if (sub_resource_idx
>= sub_count
)
2992 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx
, sub_count
);
2996 texture
->sub_resources
[sub_resource_idx
].parent
= parent
;
2999 HRESULT CDECL
wined3d_texture_get_sub_resource_desc(const struct wined3d_texture
*texture
,
3000 unsigned int sub_resource_idx
, struct wined3d_sub_resource_desc
*desc
)
3002 unsigned int sub_count
= texture
->level_count
* texture
->layer_count
;
3003 const struct wined3d_resource
*resource
;
3004 unsigned int level_idx
;
3006 TRACE("texture %p, sub_resource_idx %u, desc %p.\n", texture
, sub_resource_idx
, desc
);
3008 if (sub_resource_idx
>= sub_count
)
3010 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx
, sub_count
);
3011 return WINED3DERR_INVALIDCALL
;
3014 resource
= &texture
->resource
;
3015 desc
->format
= resource
->format
->id
;
3016 desc
->multisample_type
= resource
->multisample_type
;
3017 desc
->multisample_quality
= resource
->multisample_quality
;
3018 desc
->usage
= resource
->usage
;
3019 desc
->access
= resource
->access
;
3021 level_idx
= sub_resource_idx
% texture
->level_count
;
3022 desc
->width
= wined3d_texture_get_level_width(texture
, level_idx
);
3023 desc
->height
= wined3d_texture_get_level_height(texture
, level_idx
);
3024 desc
->depth
= wined3d_texture_get_level_depth(texture
, level_idx
);
3025 desc
->size
= texture
->sub_resources
[sub_resource_idx
].size
;
3030 HRESULT CDECL
wined3d_texture_create(struct wined3d_device
*device
, const struct wined3d_resource_desc
*desc
,
3031 UINT layer_count
, UINT level_count
, DWORD flags
, const struct wined3d_sub_resource_data
*data
,
3032 void *parent
, const struct wined3d_parent_ops
*parent_ops
, struct wined3d_texture
**texture
)
3034 struct wined3d_texture
*object
;
3037 TRACE("device %p, desc %p, layer_count %u, level_count %u, flags %#x, data %p, "
3038 "parent %p, parent_ops %p, texture %p.\n",
3039 device
, desc
, layer_count
, level_count
, flags
, data
, parent
, parent_ops
, texture
);
3043 WARN("Invalid layer count.\n");
3044 return E_INVALIDARG
;
3046 if ((desc
->usage
& WINED3DUSAGE_LEGACY_CUBEMAP
) && layer_count
!= 6)
3048 ERR("Invalid layer count %u for legacy cubemap.\n", layer_count
);
3054 WARN("Invalid level count.\n");
3055 return WINED3DERR_INVALIDCALL
;
3058 if (desc
->multisample_type
!= WINED3D_MULTISAMPLE_NONE
)
3060 const struct wined3d_format
*format
= wined3d_get_format(&device
->adapter
->gl_info
,
3061 desc
->format
, desc
->usage
);
3063 if (desc
->multisample_type
== WINED3D_MULTISAMPLE_NON_MASKABLE
3064 && desc
->multisample_quality
>= wined3d_popcount(format
->multisample_types
))
3066 WARN("Unsupported quality level %u requested for WINED3D_MULTISAMPLE_NON_MASKABLE.\n",
3067 desc
->multisample_quality
);
3068 return WINED3DERR_NOTAVAILABLE
;
3070 if (desc
->multisample_type
!= WINED3D_MULTISAMPLE_NON_MASKABLE
3071 && (!(format
->multisample_types
& 1u << (desc
->multisample_type
- 1))
3072 || desc
->multisample_quality
))
3074 WARN("Unsupported multisample type %u quality %u requested.\n", desc
->multisample_type
,
3075 desc
->multisample_quality
);
3076 return WINED3DERR_NOTAVAILABLE
;
3080 if (!(object
= heap_alloc_zero(FIELD_OFFSET(struct wined3d_texture
,
3081 sub_resources
[level_count
* layer_count
]))))
3082 return E_OUTOFMEMORY
;
3084 switch (desc
->resource_type
)
3086 case WINED3D_RTYPE_TEXTURE_2D
:
3087 hr
= texture_init(object
, desc
, layer_count
, level_count
, flags
, device
, parent
, parent_ops
);
3090 case WINED3D_RTYPE_TEXTURE_3D
:
3091 hr
= volumetexture_init(object
, desc
, layer_count
, level_count
, flags
, device
, parent
, parent_ops
);
3095 ERR("Invalid resource type %s.\n", debug_d3dresourcetype(desc
->resource_type
));
3096 hr
= WINED3DERR_INVALIDCALL
;
3102 WARN("Failed to initialize texture, returning %#x.\n", hr
);
3107 /* FIXME: We'd like to avoid ever allocating system memory for the texture
3111 unsigned int sub_count
= level_count
* layer_count
;
3114 for (i
= 0; i
< sub_count
; ++i
)
3118 WARN("Invalid sub-resource data specified for sub-resource %u.\n", i
);
3119 wined3d_texture_cleanup_sync(object
);
3121 return E_INVALIDARG
;
3125 for (i
= 0; i
< sub_count
; ++i
)
3127 wined3d_device_update_sub_resource(device
, &object
->resource
,
3128 i
, NULL
, data
[i
].data
, data
[i
].row_pitch
, data
[i
].slice_pitch
);
3132 TRACE("Created texture %p.\n", object
);
3138 HRESULT CDECL
wined3d_texture_get_dc(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
, HDC
*dc
)
3140 struct wined3d_device
*device
= texture
->resource
.device
;
3141 struct wined3d_texture_sub_resource
*sub_resource
;
3142 struct wined3d_surface
*surface
;
3144 TRACE("texture %p, sub_resource_idx %u, dc %p.\n", texture
, sub_resource_idx
, dc
);
3146 if (!(texture
->flags
& WINED3D_TEXTURE_GET_DC
))
3148 WARN("Texture does not support GetDC\n");
3149 /* Don't touch the DC */
3150 return WINED3DERR_INVALIDCALL
;
3153 if (!(sub_resource
= wined3d_texture_get_sub_resource(texture
, sub_resource_idx
)))
3154 return WINED3DERR_INVALIDCALL
;
3156 if (texture
->resource
.type
!= WINED3D_RTYPE_TEXTURE_2D
)
3158 WARN("Not supported on %s resources.\n", debug_d3dresourcetype(texture
->resource
.type
));
3159 return WINED3DERR_INVALIDCALL
;
3162 surface
= sub_resource
->u
.surface
;
3164 if (texture
->resource
.map_count
&& !(texture
->flags
& WINED3D_TEXTURE_GET_DC_LENIENT
))
3165 return WINED3DERR_INVALIDCALL
;
3169 wined3d_cs_init_object(device
->cs
, texture2d_create_dc
, surface
);
3170 device
->cs
->ops
->finish(device
->cs
, WINED3D_CS_QUEUE_DEFAULT
);
3173 return WINED3DERR_INVALIDCALL
;
3175 if (!(texture
->flags
& WINED3D_TEXTURE_GET_DC_LENIENT
))
3176 texture
->flags
|= WINED3D_TEXTURE_DC_IN_USE
;
3177 ++texture
->resource
.map_count
;
3178 ++sub_resource
->map_count
;
3181 TRACE("Returning dc %p.\n", *dc
);
3186 HRESULT CDECL
wined3d_texture_release_dc(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
, HDC dc
)
3188 struct wined3d_device
*device
= texture
->resource
.device
;
3189 struct wined3d_texture_sub_resource
*sub_resource
;
3190 struct wined3d_surface
*surface
;
3192 TRACE("texture %p, sub_resource_idx %u, dc %p.\n", texture
, sub_resource_idx
, dc
);
3194 if (!(sub_resource
= wined3d_texture_get_sub_resource(texture
, sub_resource_idx
)))
3195 return WINED3DERR_INVALIDCALL
;
3197 if (texture
->resource
.type
!= WINED3D_RTYPE_TEXTURE_2D
)
3199 WARN("Not supported on %s resources.\n", debug_d3dresourcetype(texture
->resource
.type
));
3200 return WINED3DERR_INVALIDCALL
;
3203 surface
= sub_resource
->u
.surface
;
3205 if (!(texture
->flags
& (WINED3D_TEXTURE_GET_DC_LENIENT
| WINED3D_TEXTURE_DC_IN_USE
)))
3206 return WINED3DERR_INVALIDCALL
;
3208 if (surface
->dc
!= dc
)
3210 WARN("Application tries to release invalid DC %p, surface DC is %p.\n", dc
, surface
->dc
);
3211 return WINED3DERR_INVALIDCALL
;
3214 if (!(texture
->resource
.usage
& WINED3DUSAGE_OWNDC
) && !(device
->wined3d
->flags
& WINED3D_NO3D
))
3216 wined3d_cs_destroy_object(device
->cs
, texture2d_destroy_dc
, surface
);
3217 device
->cs
->ops
->finish(device
->cs
, WINED3D_CS_QUEUE_DEFAULT
);
3220 --sub_resource
->map_count
;
3221 if (!--texture
->resource
.map_count
&& texture
->update_map_binding
)
3222 wined3d_texture_update_map_binding(texture
);
3223 if (!(texture
->flags
& WINED3D_TEXTURE_GET_DC_LENIENT
))
3224 texture
->flags
&= ~WINED3D_TEXTURE_DC_IN_USE
;