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 struct wined3d_texture_idx
35 struct wined3d_texture
*texture
;
36 unsigned int sub_resource_idx
;
47 static BOOL
wined3d_texture_use_pbo(const struct wined3d_texture
*texture
, const struct wined3d_gl_info
*gl_info
)
49 if (!gl_info
->supported
[ARB_PIXEL_BUFFER_OBJECT
]
50 || texture
->resource
.format
->conv_byte_count
51 || (texture
->flags
& (WINED3D_TEXTURE_PIN_SYSMEM
| WINED3D_TEXTURE_COND_NP2_EMULATED
)))
54 /* Use a PBO for dynamic textures and read-only staging textures. */
55 return (!(texture
->resource
.access
& WINED3D_RESOURCE_ACCESS_CPU
)
56 && texture
->resource
.usage
& WINED3DUSAGE_DYNAMIC
)
57 || texture
->resource
.access
== (WINED3D_RESOURCE_ACCESS_CPU
| WINED3D_RESOURCE_ACCESS_MAP_R
);
60 static BOOL
wined3d_texture_use_immutable_storage(const struct wined3d_texture
*texture
,
61 const struct wined3d_gl_info
*gl_info
)
63 /* We don't expect to create texture views for textures with height-scaled formats.
64 * Besides, ARB_texture_storage doesn't allow specifying exact sizes for all levels. */
65 return gl_info
->supported
[ARB_TEXTURE_STORAGE
]
66 && !(texture
->resource
.format_flags
& WINED3DFMT_FLAG_HEIGHT_SCALE
);
69 /* Front buffer coordinates are always full screen coordinates, but our GL
70 * drawable is limited to the window's client area. The sysmem and texture
71 * copies do have the full screen size. Note that GL has a bottom-left
72 * origin, while D3D has a top-left origin. */
73 void wined3d_texture_translate_drawable_coords(const struct wined3d_texture
*texture
, HWND window
, RECT
*rect
)
75 unsigned int drawable_height
;
76 POINT offset
= {0, 0};
79 if (!texture
->swapchain
)
82 if (texture
== texture
->swapchain
->front_buffer
)
84 ScreenToClient(window
, &offset
);
85 OffsetRect(rect
, offset
.x
, offset
.y
);
88 GetClientRect(window
, &windowsize
);
89 drawable_height
= windowsize
.bottom
- windowsize
.top
;
91 rect
->top
= drawable_height
- rect
->top
;
92 rect
->bottom
= drawable_height
- rect
->bottom
;
95 GLenum
wined3d_texture_get_gl_buffer(const struct wined3d_texture
*texture
)
97 const struct wined3d_swapchain
*swapchain
= texture
->swapchain
;
99 TRACE("texture %p.\n", texture
);
103 ERR("Texture %p is not part of a swapchain.\n", texture
);
107 if (texture
== swapchain
->front_buffer
)
109 TRACE("Returning GL_FRONT.\n");
113 if (texture
== swapchain
->back_buffers
[0])
115 TRACE("Returning GL_BACK.\n");
119 FIXME("Higher back buffer, returning GL_BACK.\n");
123 static DWORD
wined3d_resource_access_from_location(DWORD location
)
127 case WINED3D_LOCATION_DISCARDED
:
130 case WINED3D_LOCATION_SYSMEM
:
131 case WINED3D_LOCATION_USER_MEMORY
:
132 return WINED3D_RESOURCE_ACCESS_CPU
;
134 case WINED3D_LOCATION_BUFFER
:
135 case WINED3D_LOCATION_DRAWABLE
:
136 case WINED3D_LOCATION_TEXTURE_RGB
:
137 case WINED3D_LOCATION_TEXTURE_SRGB
:
138 case WINED3D_LOCATION_RB_MULTISAMPLE
:
139 case WINED3D_LOCATION_RB_RESOLVED
:
140 return WINED3D_RESOURCE_ACCESS_GPU
;
143 FIXME("Unhandled location %#x.\n", location
);
148 static inline void cube_coords_float(const RECT
*r
, UINT w
, UINT h
, struct wined3d_rect_f
*f
)
150 f
->l
= ((r
->left
* 2.0f
) / w
) - 1.0f
;
151 f
->t
= ((r
->top
* 2.0f
) / h
) - 1.0f
;
152 f
->r
= ((r
->right
* 2.0f
) / w
) - 1.0f
;
153 f
->b
= ((r
->bottom
* 2.0f
) / h
) - 1.0f
;
156 void texture2d_get_blt_info(const struct wined3d_texture
*texture
,
157 unsigned int sub_resource_idx
, const RECT
*rect
, struct wined3d_blt_info
*info
)
159 struct wined3d_vec3
*coords
= info
->texcoords
;
160 struct wined3d_rect_f f
;
165 level
= sub_resource_idx
% texture
->level_count
;
166 w
= wined3d_texture_get_level_pow2_width(texture
, level
);
167 h
= wined3d_texture_get_level_pow2_height(texture
, level
);
168 target
= wined3d_texture_get_sub_resource_target(texture
, sub_resource_idx
);
173 FIXME("Unsupported texture target %#x.\n", target
);
174 /* Fall back to GL_TEXTURE_2D */
176 info
->bind_target
= GL_TEXTURE_2D
;
177 coords
[0].x
= (float)rect
->left
/ w
;
178 coords
[0].y
= (float)rect
->top
/ h
;
181 coords
[1].x
= (float)rect
->right
/ w
;
182 coords
[1].y
= (float)rect
->top
/ h
;
185 coords
[2].x
= (float)rect
->left
/ w
;
186 coords
[2].y
= (float)rect
->bottom
/ h
;
189 coords
[3].x
= (float)rect
->right
/ w
;
190 coords
[3].y
= (float)rect
->bottom
/ h
;
194 case GL_TEXTURE_RECTANGLE_ARB
:
195 info
->bind_target
= GL_TEXTURE_RECTANGLE_ARB
;
196 coords
[0].x
= rect
->left
; coords
[0].y
= rect
->top
; coords
[0].z
= 0.0f
;
197 coords
[1].x
= rect
->right
; coords
[1].y
= rect
->top
; coords
[1].z
= 0.0f
;
198 coords
[2].x
= rect
->left
; coords
[2].y
= rect
->bottom
; coords
[2].z
= 0.0f
;
199 coords
[3].x
= rect
->right
; coords
[3].y
= rect
->bottom
; coords
[3].z
= 0.0f
;
202 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
203 info
->bind_target
= GL_TEXTURE_CUBE_MAP_ARB
;
204 cube_coords_float(rect
, w
, h
, &f
);
206 coords
[0].x
= 1.0f
; coords
[0].y
= -f
.t
; coords
[0].z
= -f
.l
;
207 coords
[1].x
= 1.0f
; coords
[1].y
= -f
.t
; coords
[1].z
= -f
.r
;
208 coords
[2].x
= 1.0f
; coords
[2].y
= -f
.b
; coords
[2].z
= -f
.l
;
209 coords
[3].x
= 1.0f
; coords
[3].y
= -f
.b
; coords
[3].z
= -f
.r
;
212 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
213 info
->bind_target
= GL_TEXTURE_CUBE_MAP_ARB
;
214 cube_coords_float(rect
, w
, h
, &f
);
216 coords
[0].x
= -1.0f
; coords
[0].y
= -f
.t
; coords
[0].z
= f
.l
;
217 coords
[1].x
= -1.0f
; coords
[1].y
= -f
.t
; coords
[1].z
= f
.r
;
218 coords
[2].x
= -1.0f
; coords
[2].y
= -f
.b
; coords
[2].z
= f
.l
;
219 coords
[3].x
= -1.0f
; coords
[3].y
= -f
.b
; coords
[3].z
= f
.r
;
222 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
223 info
->bind_target
= GL_TEXTURE_CUBE_MAP_ARB
;
224 cube_coords_float(rect
, w
, h
, &f
);
226 coords
[0].x
= f
.l
; coords
[0].y
= 1.0f
; coords
[0].z
= f
.t
;
227 coords
[1].x
= f
.r
; coords
[1].y
= 1.0f
; coords
[1].z
= f
.t
;
228 coords
[2].x
= f
.l
; coords
[2].y
= 1.0f
; coords
[2].z
= f
.b
;
229 coords
[3].x
= f
.r
; coords
[3].y
= 1.0f
; coords
[3].z
= f
.b
;
232 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
233 info
->bind_target
= GL_TEXTURE_CUBE_MAP_ARB
;
234 cube_coords_float(rect
, w
, h
, &f
);
236 coords
[0].x
= f
.l
; coords
[0].y
= -1.0f
; coords
[0].z
= -f
.t
;
237 coords
[1].x
= f
.r
; coords
[1].y
= -1.0f
; coords
[1].z
= -f
.t
;
238 coords
[2].x
= f
.l
; coords
[2].y
= -1.0f
; coords
[2].z
= -f
.b
;
239 coords
[3].x
= f
.r
; coords
[3].y
= -1.0f
; coords
[3].z
= -f
.b
;
242 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
243 info
->bind_target
= GL_TEXTURE_CUBE_MAP_ARB
;
244 cube_coords_float(rect
, w
, h
, &f
);
246 coords
[0].x
= f
.l
; coords
[0].y
= -f
.t
; coords
[0].z
= 1.0f
;
247 coords
[1].x
= f
.r
; coords
[1].y
= -f
.t
; coords
[1].z
= 1.0f
;
248 coords
[2].x
= f
.l
; coords
[2].y
= -f
.b
; coords
[2].z
= 1.0f
;
249 coords
[3].x
= f
.r
; coords
[3].y
= -f
.b
; coords
[3].z
= 1.0f
;
252 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
253 info
->bind_target
= GL_TEXTURE_CUBE_MAP_ARB
;
254 cube_coords_float(rect
, w
, h
, &f
);
256 coords
[0].x
= -f
.l
; coords
[0].y
= -f
.t
; coords
[0].z
= -1.0f
;
257 coords
[1].x
= -f
.r
; coords
[1].y
= -f
.t
; coords
[1].z
= -1.0f
;
258 coords
[2].x
= -f
.l
; coords
[2].y
= -f
.b
; coords
[2].z
= -1.0f
;
259 coords
[3].x
= -f
.r
; coords
[3].y
= -f
.b
; coords
[3].z
= -1.0f
;
264 static void wined3d_texture_evict_sysmem(struct wined3d_texture
*texture
)
266 struct wined3d_texture_sub_resource
*sub_resource
;
267 unsigned int i
, sub_count
;
269 if (texture
->flags
& (WINED3D_TEXTURE_CONVERTED
| WINED3D_TEXTURE_PIN_SYSMEM
)
270 || texture
->download_count
> WINED3D_TEXTURE_DYNAMIC_MAP_THRESHOLD
)
272 TRACE("Not evicting system memory for texture %p.\n", texture
);
276 TRACE("Evicting system memory for texture %p.\n", texture
);
278 sub_count
= texture
->level_count
* texture
->layer_count
;
279 for (i
= 0; i
< sub_count
; ++i
)
281 sub_resource
= &texture
->sub_resources
[i
];
282 if (sub_resource
->locations
== WINED3D_LOCATION_SYSMEM
)
283 ERR("WINED3D_LOCATION_SYSMEM is the only location for sub-resource %u of texture %p.\n",
285 sub_resource
->locations
&= ~WINED3D_LOCATION_SYSMEM
;
287 wined3d_resource_free_sysmem(&texture
->resource
);
290 void wined3d_texture_validate_location(struct wined3d_texture
*texture
,
291 unsigned int sub_resource_idx
, DWORD location
)
293 struct wined3d_texture_sub_resource
*sub_resource
;
294 DWORD previous_locations
;
296 TRACE("texture %p, sub_resource_idx %u, location %s.\n",
297 texture
, sub_resource_idx
, wined3d_debug_location(location
));
299 sub_resource
= &texture
->sub_resources
[sub_resource_idx
];
300 previous_locations
= sub_resource
->locations
;
301 sub_resource
->locations
|= location
;
302 if (previous_locations
== WINED3D_LOCATION_SYSMEM
&& location
!= WINED3D_LOCATION_SYSMEM
303 && !--texture
->sysmem_count
)
304 wined3d_texture_evict_sysmem(texture
);
306 TRACE("New locations flags are %s.\n", wined3d_debug_location(sub_resource
->locations
));
309 static void wined3d_texture_set_dirty(struct wined3d_texture
*texture
)
311 texture
->flags
&= ~(WINED3D_TEXTURE_RGB_VALID
| WINED3D_TEXTURE_SRGB_VALID
);
314 void wined3d_texture_invalidate_location(struct wined3d_texture
*texture
,
315 unsigned int sub_resource_idx
, DWORD location
)
317 struct wined3d_texture_sub_resource
*sub_resource
;
318 DWORD previous_locations
;
320 TRACE("texture %p, sub_resource_idx %u, location %s.\n",
321 texture
, sub_resource_idx
, wined3d_debug_location(location
));
323 if (location
& (WINED3D_LOCATION_TEXTURE_RGB
| WINED3D_LOCATION_TEXTURE_SRGB
))
324 wined3d_texture_set_dirty(texture
);
326 sub_resource
= &texture
->sub_resources
[sub_resource_idx
];
327 previous_locations
= sub_resource
->locations
;
328 sub_resource
->locations
&= ~location
;
329 if (previous_locations
!= WINED3D_LOCATION_SYSMEM
&& sub_resource
->locations
== WINED3D_LOCATION_SYSMEM
)
330 ++texture
->sysmem_count
;
332 TRACE("New locations flags are %s.\n", wined3d_debug_location(sub_resource
->locations
));
334 if (!sub_resource
->locations
)
335 ERR("Sub-resource %u of texture %p does not have any up to date location.\n",
336 sub_resource_idx
, texture
);
339 static BOOL
wined3d_texture_copy_sysmem_location(struct wined3d_texture
*texture
,
340 unsigned int sub_resource_idx
, struct wined3d_context
*context
, DWORD location
)
342 unsigned int size
= texture
->sub_resources
[sub_resource_idx
].size
;
343 struct wined3d_device
*device
= texture
->resource
.device
;
344 const struct wined3d_gl_info
*gl_info
;
345 struct wined3d_bo_address dst
, src
;
347 if (!wined3d_texture_prepare_location(texture
, sub_resource_idx
, context
, location
))
350 wined3d_texture_get_memory(texture
, sub_resource_idx
, &dst
, location
);
351 wined3d_texture_get_memory(texture
, sub_resource_idx
, &src
,
352 texture
->sub_resources
[sub_resource_idx
].locations
);
354 if (dst
.buffer_object
)
356 context
= context_acquire(device
, NULL
, 0);
357 gl_info
= context
->gl_info
;
358 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER
, dst
.buffer_object
));
359 GL_EXTCALL(glBufferSubData(GL_PIXEL_UNPACK_BUFFER
, 0, size
, src
.addr
));
360 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER
, 0));
361 checkGLcall("PBO upload");
362 context_release(context
);
366 if (src
.buffer_object
)
368 context
= context_acquire(device
, NULL
, 0);
369 gl_info
= context
->gl_info
;
370 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER
, src
.buffer_object
));
371 GL_EXTCALL(glGetBufferSubData(GL_PIXEL_PACK_BUFFER
, 0, size
, dst
.addr
));
372 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER
, 0));
373 checkGLcall("PBO download");
374 context_release(context
);
378 memcpy(dst
.addr
, src
.addr
, size
);
382 /* Context activation is done by the caller. Context may be NULL in
383 * WINED3D_NO3D mode. */
384 BOOL
wined3d_texture_load_location(struct wined3d_texture
*texture
,
385 unsigned int sub_resource_idx
, struct wined3d_context
*context
, DWORD location
)
387 static const DWORD sysmem_locations
= WINED3D_LOCATION_SYSMEM
| WINED3D_LOCATION_USER_MEMORY
388 | WINED3D_LOCATION_BUFFER
;
389 DWORD current
= texture
->sub_resources
[sub_resource_idx
].locations
;
392 TRACE("texture %p, sub_resource_idx %u, context %p, location %s.\n",
393 texture
, sub_resource_idx
, context
, wined3d_debug_location(location
));
395 TRACE("Current resource location %s.\n", wined3d_debug_location(current
));
397 if (current
& location
)
399 TRACE("Location %s is already up to date.\n", wined3d_debug_location(location
));
405 DWORD required_access
= wined3d_resource_access_from_location(location
);
406 if ((texture
->resource
.access
& required_access
) != required_access
)
407 WARN("Operation requires %#x access, but texture only has %#x.\n",
408 required_access
, texture
->resource
.access
);
411 if (current
& WINED3D_LOCATION_DISCARDED
)
413 TRACE("Sub-resource previously discarded, nothing to do.\n");
414 if (!wined3d_texture_prepare_location(texture
, sub_resource_idx
, context
, location
))
416 wined3d_texture_validate_location(texture
, sub_resource_idx
, location
);
417 wined3d_texture_invalidate_location(texture
, sub_resource_idx
, WINED3D_LOCATION_DISCARDED
);
423 ERR("Sub-resource %u of texture %p does not have any up to date location.\n",
424 sub_resource_idx
, texture
);
425 wined3d_texture_validate_location(texture
, sub_resource_idx
, WINED3D_LOCATION_DISCARDED
);
426 return wined3d_texture_load_location(texture
, sub_resource_idx
, context
, location
);
429 if ((location
& sysmem_locations
) && (current
& sysmem_locations
))
430 ret
= wined3d_texture_copy_sysmem_location(texture
, sub_resource_idx
, context
, location
);
432 ret
= texture
->texture_ops
->texture_load_location(texture
, sub_resource_idx
, context
, location
);
435 wined3d_texture_validate_location(texture
, sub_resource_idx
, location
);
440 void wined3d_texture_get_memory(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
,
441 struct wined3d_bo_address
*data
, DWORD locations
)
443 struct wined3d_texture_sub_resource
*sub_resource
;
445 TRACE("texture %p, sub_resource_idx %u, data %p, locations %s.\n",
446 texture
, sub_resource_idx
, data
, wined3d_debug_location(locations
));
448 sub_resource
= &texture
->sub_resources
[sub_resource_idx
];
449 if (locations
& WINED3D_LOCATION_BUFFER
)
452 data
->buffer_object
= sub_resource
->buffer_object
;
455 if (locations
& WINED3D_LOCATION_USER_MEMORY
)
457 data
->addr
= texture
->user_memory
;
458 data
->buffer_object
= 0;
461 if (locations
& WINED3D_LOCATION_SYSMEM
)
463 data
->addr
= texture
->resource
.heap_memory
;
464 data
->addr
+= sub_resource
->offset
;
465 data
->buffer_object
= 0;
469 ERR("Unexpected locations %s.\n", wined3d_debug_location(locations
));
471 data
->buffer_object
= 0;
474 /* Context activation is done by the caller. */
475 static void wined3d_texture_remove_buffer_object(struct wined3d_texture
*texture
,
476 unsigned int sub_resource_idx
, const struct wined3d_gl_info
*gl_info
)
478 GLuint
*buffer_object
= &texture
->sub_resources
[sub_resource_idx
].buffer_object
;
480 GL_EXTCALL(glDeleteBuffers(1, buffer_object
));
481 checkGLcall("glDeleteBuffers");
483 TRACE("Deleted buffer object %u for texture %p, sub-resource %u.\n",
484 *buffer_object
, texture
, sub_resource_idx
);
486 wined3d_texture_invalidate_location(texture
, sub_resource_idx
, WINED3D_LOCATION_BUFFER
);
490 static void wined3d_texture_update_map_binding(struct wined3d_texture
*texture
)
492 unsigned int sub_count
= texture
->level_count
* texture
->layer_count
;
493 const struct wined3d_device
*device
= texture
->resource
.device
;
494 DWORD map_binding
= texture
->update_map_binding
;
495 struct wined3d_context
*context
= NULL
;
498 if (device
->d3d_initialized
)
499 context
= context_acquire(device
, NULL
, 0);
501 for (i
= 0; i
< sub_count
; ++i
)
503 if (texture
->sub_resources
[i
].locations
== texture
->resource
.map_binding
504 && !wined3d_texture_load_location(texture
, i
, context
, map_binding
))
505 ERR("Failed to load location %s.\n", wined3d_debug_location(map_binding
));
506 if (texture
->resource
.map_binding
== WINED3D_LOCATION_BUFFER
)
507 wined3d_texture_remove_buffer_object(texture
, i
, context
->gl_info
);
511 context_release(context
);
513 texture
->resource
.map_binding
= map_binding
;
514 texture
->update_map_binding
= 0;
517 void wined3d_texture_set_map_binding(struct wined3d_texture
*texture
, DWORD map_binding
)
519 texture
->update_map_binding
= map_binding
;
520 if (!texture
->resource
.map_count
)
521 wined3d_texture_update_map_binding(texture
);
524 /* A GL context is provided by the caller */
525 static void gltexture_delete(struct wined3d_device
*device
, const struct wined3d_gl_info
*gl_info
,
526 struct gl_texture
*tex
)
528 context_gl_resource_released(device
, tex
->name
, FALSE
);
529 gl_info
->gl_ops
.gl
.p_glDeleteTextures(1, &tex
->name
);
533 static unsigned int wined3d_texture_get_gl_sample_count(const struct wined3d_texture
*texture
)
535 const struct wined3d_format
*format
= texture
->resource
.format
;
537 /* TODO: NVIDIA expose their Coverage Sample Anti-Aliasing (CSAA)
538 * feature through type == MULTISAMPLE_XX and quality != 0. This could
539 * be mapped to GL_NV_framebuffer_multisample_coverage.
541 * AMD have a similar feature called Enhanced Quality Anti-Aliasing
542 * (EQAA), but it does not have an equivalent OpenGL extension. */
544 /* We advertise as many WINED3D_MULTISAMPLE_NON_MASKABLE quality
545 * levels as the count of advertised multisample types for the texture
547 if (texture
->resource
.multisample_type
== WINED3D_MULTISAMPLE_NON_MASKABLE
)
549 unsigned int i
, count
= 0;
551 for (i
= 0; i
< sizeof(format
->multisample_types
) * CHAR_BIT
; ++i
)
553 if (format
->multisample_types
& 1u << i
)
555 if (texture
->resource
.multisample_quality
== count
++)
562 return texture
->resource
.multisample_type
;
565 /* Context activation is done by the caller. */
566 /* The caller is responsible for binding the correct texture. */
567 static void wined3d_texture_allocate_gl_mutable_storage(struct wined3d_texture
*texture
,
568 GLenum gl_internal_format
, const struct wined3d_format_gl
*format
,
569 const struct wined3d_gl_info
*gl_info
)
571 unsigned int level
, level_count
, layer
, layer_count
;
572 GLsizei width
, height
, depth
;
575 level_count
= texture
->level_count
;
576 if (texture
->target
== GL_TEXTURE_1D_ARRAY
|| texture
->target
== GL_TEXTURE_2D_ARRAY
)
579 layer_count
= texture
->layer_count
;
581 for (layer
= 0; layer
< layer_count
; ++layer
)
583 target
= wined3d_texture_get_sub_resource_target(texture
, layer
* level_count
);
585 for (level
= 0; level
< level_count
; ++level
)
587 width
= wined3d_texture_get_level_pow2_width(texture
, level
);
588 height
= wined3d_texture_get_level_pow2_height(texture
, level
);
589 if (texture
->resource
.format_flags
& WINED3DFMT_FLAG_HEIGHT_SCALE
)
591 height
*= format
->f
.height_scale
.numerator
;
592 height
/= format
->f
.height_scale
.denominator
;
595 TRACE("texture %p, layer %u, level %u, target %#x, width %u, height %u.\n",
596 texture
, layer
, level
, target
, width
, height
);
598 if (target
== GL_TEXTURE_3D
|| target
== GL_TEXTURE_2D_ARRAY
)
600 depth
= wined3d_texture_get_level_depth(texture
, level
);
601 GL_EXTCALL(glTexImage3D(target
, level
, gl_internal_format
, width
, height
,
602 target
== GL_TEXTURE_2D_ARRAY
? texture
->layer_count
: depth
, 0,
603 format
->format
, format
->type
, NULL
));
604 checkGLcall("glTexImage3D");
606 else if (target
== GL_TEXTURE_1D
)
608 gl_info
->gl_ops
.gl
.p_glTexImage1D(target
, level
, gl_internal_format
,
609 width
, 0, format
->format
, format
->type
, NULL
);
613 gl_info
->gl_ops
.gl
.p_glTexImage2D(target
, level
, gl_internal_format
, width
,
614 target
== GL_TEXTURE_1D_ARRAY
? texture
->layer_count
: height
, 0,
615 format
->format
, format
->type
, NULL
);
616 checkGLcall("glTexImage2D");
622 /* Context activation is done by the caller. */
623 /* The caller is responsible for binding the correct texture. */
624 static void wined3d_texture_allocate_gl_immutable_storage(struct wined3d_texture
*texture
,
625 GLenum gl_internal_format
, const struct wined3d_gl_info
*gl_info
)
627 unsigned int samples
= wined3d_texture_get_gl_sample_count(texture
);
628 GLsizei height
= wined3d_texture_get_level_pow2_height(texture
, 0);
629 GLsizei width
= wined3d_texture_get_level_pow2_width(texture
, 0);
631 switch (texture
->target
)
634 GL_EXTCALL(glTexStorage3D(texture
->target
, texture
->level_count
,
635 gl_internal_format
, width
, height
, wined3d_texture_get_level_depth(texture
, 0)));
637 case GL_TEXTURE_2D_ARRAY
:
638 GL_EXTCALL(glTexStorage3D(texture
->target
, texture
->level_count
,
639 gl_internal_format
, width
, height
, texture
->layer_count
));
641 case GL_TEXTURE_2D_MULTISAMPLE
:
642 GL_EXTCALL(glTexStorage2DMultisample(texture
->target
, samples
,
643 gl_internal_format
, width
, height
, GL_FALSE
));
645 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
646 GL_EXTCALL(glTexStorage3DMultisample(texture
->target
, samples
,
647 gl_internal_format
, width
, height
, texture
->layer_count
, GL_FALSE
));
649 case GL_TEXTURE_1D_ARRAY
:
650 GL_EXTCALL(glTexStorage2D(texture
->target
, texture
->level_count
,
651 gl_internal_format
, width
, texture
->layer_count
));
654 GL_EXTCALL(glTexStorage1D(texture
->target
, texture
->level_count
, gl_internal_format
, width
));
657 GL_EXTCALL(glTexStorage2D(texture
->target
, texture
->level_count
,
658 gl_internal_format
, width
, height
));
662 checkGLcall("allocate immutable storage");
665 static void wined3d_texture_unload_gl_texture(struct wined3d_texture
*texture
)
667 struct wined3d_device
*device
= texture
->resource
.device
;
668 const struct wined3d_gl_info
*gl_info
= NULL
;
669 struct wined3d_context
*context
= NULL
;
671 if (texture
->texture_rgb
.name
|| texture
->texture_srgb
.name
672 || texture
->rb_multisample
|| texture
->rb_resolved
)
674 context
= context_acquire(device
, NULL
, 0);
675 gl_info
= context
->gl_info
;
678 if (texture
->texture_rgb
.name
)
679 gltexture_delete(device
, context
->gl_info
, &texture
->texture_rgb
);
681 if (texture
->texture_srgb
.name
)
682 gltexture_delete(device
, context
->gl_info
, &texture
->texture_srgb
);
684 if (texture
->rb_multisample
)
686 TRACE("Deleting multisample renderbuffer %u.\n", texture
->rb_multisample
);
687 context_gl_resource_released(device
, texture
->rb_multisample
, TRUE
);
688 gl_info
->fbo_ops
.glDeleteRenderbuffers(1, &texture
->rb_multisample
);
689 texture
->rb_multisample
= 0;
692 if (texture
->rb_resolved
)
694 TRACE("Deleting resolved renderbuffer %u.\n", texture
->rb_resolved
);
695 context_gl_resource_released(device
, texture
->rb_resolved
, TRUE
);
696 gl_info
->fbo_ops
.glDeleteRenderbuffers(1, &texture
->rb_resolved
);
697 texture
->rb_resolved
= 0;
700 if (context
) context_release(context
);
702 wined3d_texture_set_dirty(texture
);
704 resource_unload(&texture
->resource
);
707 static void wined3d_texture_sub_resources_destroyed(struct wined3d_texture
*texture
)
709 unsigned int sub_count
= texture
->level_count
* texture
->layer_count
;
710 struct wined3d_texture_sub_resource
*sub_resource
;
713 for (i
= 0; i
< sub_count
; ++i
)
715 sub_resource
= &texture
->sub_resources
[i
];
716 if (sub_resource
->parent
)
718 TRACE("sub-resource %u.\n", i
);
719 sub_resource
->parent_ops
->wined3d_object_destroyed(sub_resource
->parent
);
720 sub_resource
->parent
= NULL
;
725 static void wined3d_texture_create_dc(void *object
)
727 const struct wined3d_texture_idx
*idx
= object
;
728 struct wined3d_context
*context
= NULL
;
729 unsigned int sub_resource_idx
, level
;
730 const struct wined3d_format
*format
;
731 unsigned int row_pitch
, slice_pitch
;
732 struct wined3d_texture
*texture
;
733 struct wined3d_dc_info
*dc_info
;
734 struct wined3d_bo_address data
;
735 D3DKMT_CREATEDCFROMMEMORY desc
;
736 struct wined3d_device
*device
;
739 TRACE("texture %p, sub_resource_idx %u.\n", idx
->texture
, idx
->sub_resource_idx
);
741 texture
= idx
->texture
;
742 sub_resource_idx
= idx
->sub_resource_idx
;
743 level
= sub_resource_idx
% texture
->level_count
;
744 device
= texture
->resource
.device
;
746 format
= texture
->resource
.format
;
747 if (!format
->ddi_format
)
749 WARN("Cannot create a DC for format %s.\n", debug_d3dformat(format
->id
));
753 if (!texture
->dc_info
)
755 unsigned int sub_count
= texture
->level_count
* texture
->layer_count
;
757 if (!(texture
->dc_info
= heap_calloc(sub_count
, sizeof(*texture
->dc_info
))))
759 ERR("Failed to allocate DC info.\n");
764 if (device
->d3d_initialized
)
765 context
= context_acquire(device
, NULL
, 0);
767 wined3d_texture_load_location(texture
, sub_resource_idx
, context
, texture
->resource
.map_binding
);
768 wined3d_texture_invalidate_location(texture
, sub_resource_idx
, ~texture
->resource
.map_binding
);
769 wined3d_texture_get_pitch(texture
, level
, &row_pitch
, &slice_pitch
);
770 wined3d_texture_get_memory(texture
, sub_resource_idx
, &data
, texture
->resource
.map_binding
);
771 desc
.pMemory
= context_map_bo_address(context
, &data
,
772 texture
->sub_resources
[sub_resource_idx
].size
,
773 GL_PIXEL_UNPACK_BUFFER
, WINED3D_MAP_READ
| WINED3D_MAP_WRITE
);
776 context_release(context
);
778 desc
.Format
= format
->ddi_format
;
779 desc
.Width
= wined3d_texture_get_level_width(texture
, level
);
780 desc
.Height
= wined3d_texture_get_level_height(texture
, level
);
781 desc
.Pitch
= row_pitch
;
782 desc
.hDeviceDc
= CreateCompatibleDC(NULL
);
783 desc
.pColorTable
= NULL
;
785 status
= D3DKMTCreateDCFromMemory(&desc
);
786 DeleteDC(desc
.hDeviceDc
);
789 WARN("Failed to create DC, status %#x.\n", status
);
793 dc_info
= &texture
->dc_info
[sub_resource_idx
];
794 dc_info
->dc
= desc
.hDc
;
795 dc_info
->bitmap
= desc
.hBitmap
;
797 TRACE("Created DC %p, bitmap %p for texture %p, %u.\n", dc_info
->dc
, dc_info
->bitmap
, texture
, sub_resource_idx
);
800 static void wined3d_texture_destroy_dc(void *object
)
802 const struct wined3d_texture_idx
*idx
= object
;
803 D3DKMT_DESTROYDCFROMMEMORY destroy_desc
;
804 struct wined3d_context
*context
= NULL
;
805 struct wined3d_texture
*texture
;
806 struct wined3d_dc_info
*dc_info
;
807 struct wined3d_bo_address data
;
808 unsigned int sub_resource_idx
;
809 struct wined3d_device
*device
;
812 texture
= idx
->texture
;
813 sub_resource_idx
= idx
->sub_resource_idx
;
814 device
= texture
->resource
.device
;
815 dc_info
= &texture
->dc_info
[sub_resource_idx
];
819 ERR("Sub-resource {%p, %u} has no DC.\n", texture
, sub_resource_idx
);
823 TRACE("dc %p, bitmap %p.\n", dc_info
->dc
, dc_info
->bitmap
);
825 destroy_desc
.hDc
= dc_info
->dc
;
826 destroy_desc
.hBitmap
= dc_info
->bitmap
;
827 if ((status
= D3DKMTDestroyDCFromMemory(&destroy_desc
)))
828 ERR("Failed to destroy dc, status %#x.\n", status
);
830 dc_info
->bitmap
= NULL
;
832 if (device
->d3d_initialized
)
833 context
= context_acquire(device
, NULL
, 0);
835 wined3d_texture_get_memory(texture
, sub_resource_idx
, &data
, texture
->resource
.map_binding
);
836 context_unmap_bo_address(context
, &data
, GL_PIXEL_UNPACK_BUFFER
);
839 context_release(context
);
842 static void wined3d_texture_cleanup(struct wined3d_texture
*texture
)
844 unsigned int sub_count
= texture
->level_count
* texture
->layer_count
;
845 struct wined3d_device
*device
= texture
->resource
.device
;
846 struct wined3d_renderbuffer_entry
*entry
, *entry2
;
847 const struct wined3d_gl_info
*gl_info
= NULL
;
848 struct wined3d_context
*context
= NULL
;
849 struct wined3d_dc_info
*dc_info
;
850 GLuint buffer_object
;
853 TRACE("texture %p.\n", texture
);
855 for (i
= 0; i
< sub_count
; ++i
)
857 if (!(buffer_object
= texture
->sub_resources
[i
].buffer_object
))
860 TRACE("Deleting buffer object %u.\n", buffer_object
);
862 /* We may not be able to get a context in wined3d_texture_cleanup() in
863 * general, but if a buffer object was previously created we can. */
866 context
= context_acquire(device
, NULL
, 0);
867 gl_info
= context
->gl_info
;
870 GL_EXTCALL(glDeleteBuffers(1, &buffer_object
));
873 if (!context
&& !list_empty(&texture
->renderbuffers
))
875 context
= context_acquire(device
, NULL
, 0);
876 gl_info
= context
->gl_info
;
879 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, &texture
->renderbuffers
, struct wined3d_renderbuffer_entry
, entry
)
881 TRACE("Deleting renderbuffer %u.\n", entry
->id
);
882 context_gl_resource_released(device
, entry
->id
, TRUE
);
883 gl_info
->fbo_ops
.glDeleteRenderbuffers(1, &entry
->id
);
888 context_release(context
);
890 if ((dc_info
= texture
->dc_info
))
892 for (i
= 0; i
< sub_count
; ++i
)
896 struct wined3d_texture_idx texture_idx
= {texture
, i
};
898 wined3d_texture_destroy_dc(&texture_idx
);
904 if (texture
->overlay_info
)
906 for (i
= 0; i
< sub_count
; ++i
)
908 struct wined3d_overlay_info
*info
= &texture
->overlay_info
[i
];
909 struct wined3d_overlay_info
*overlay
, *cur
;
911 list_remove(&info
->entry
);
912 LIST_FOR_EACH_ENTRY_SAFE(overlay
, cur
, &info
->overlays
, struct wined3d_overlay_info
, entry
)
914 list_remove(&overlay
->entry
);
917 heap_free(texture
->overlay_info
);
919 wined3d_texture_unload_gl_texture(texture
);
922 void wined3d_texture_set_swapchain(struct wined3d_texture
*texture
, struct wined3d_swapchain
*swapchain
)
924 texture
->swapchain
= swapchain
;
925 wined3d_resource_update_draw_binding(&texture
->resource
);
928 /* Context activation is done by the caller. */
929 void wined3d_texture_bind(struct wined3d_texture
*texture
,
930 struct wined3d_context
*context
, BOOL srgb
)
932 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
933 const struct wined3d_format
*format
= texture
->resource
.format
;
934 const struct color_fixup_desc fixup
= format
->color_fixup
;
935 struct gl_texture
*gl_tex
;
938 TRACE("texture %p, context %p, srgb %#x.\n", texture
, context
, srgb
);
940 if (!needs_separate_srgb_gl_texture(context
, texture
))
943 /* sRGB mode cache for preload() calls outside drawprim. */
945 texture
->flags
|= WINED3D_TEXTURE_IS_SRGB
;
947 texture
->flags
&= ~WINED3D_TEXTURE_IS_SRGB
;
949 gl_tex
= wined3d_texture_get_gl_texture(texture
, srgb
);
950 target
= texture
->target
;
954 context_bind_texture(context
, target
, gl_tex
->name
);
958 gl_info
->gl_ops
.gl
.p_glGenTextures(1, &gl_tex
->name
);
959 checkGLcall("glGenTextures");
960 TRACE("Generated texture %d.\n", gl_tex
->name
);
964 ERR("Failed to generate a texture name.\n");
968 /* Initialise the state of the texture object to the OpenGL defaults, not
969 * the wined3d defaults. */
970 gl_tex
->sampler_desc
.address_u
= WINED3D_TADDRESS_WRAP
;
971 gl_tex
->sampler_desc
.address_v
= WINED3D_TADDRESS_WRAP
;
972 gl_tex
->sampler_desc
.address_w
= WINED3D_TADDRESS_WRAP
;
973 memset(gl_tex
->sampler_desc
.border_color
, 0, sizeof(gl_tex
->sampler_desc
.border_color
));
974 gl_tex
->sampler_desc
.mag_filter
= WINED3D_TEXF_LINEAR
;
975 gl_tex
->sampler_desc
.min_filter
= WINED3D_TEXF_POINT
; /* GL_NEAREST_MIPMAP_LINEAR */
976 gl_tex
->sampler_desc
.mip_filter
= WINED3D_TEXF_LINEAR
; /* GL_NEAREST_MIPMAP_LINEAR */
977 gl_tex
->sampler_desc
.lod_bias
= 0.0f
;
978 gl_tex
->sampler_desc
.min_lod
= -1000.0f
;
979 gl_tex
->sampler_desc
.max_lod
= 1000.0f
;
980 gl_tex
->sampler_desc
.max_anisotropy
= 1;
981 gl_tex
->sampler_desc
.compare
= FALSE
;
982 gl_tex
->sampler_desc
.comparison_func
= WINED3D_CMP_LESSEQUAL
;
983 if (context
->gl_info
->supported
[EXT_TEXTURE_SRGB_DECODE
])
984 gl_tex
->sampler_desc
.srgb_decode
= TRUE
;
986 gl_tex
->sampler_desc
.srgb_decode
= srgb
;
987 gl_tex
->base_level
= 0;
988 wined3d_texture_set_dirty(texture
);
990 context_bind_texture(context
, target
, gl_tex
->name
);
992 /* For a new texture we have to set the texture levels after binding the
993 * texture. Beware that texture rectangles do not support mipmapping, but
994 * set the maxmiplevel if we're relying on the partial
995 * GL_ARB_texture_non_power_of_two emulation with texture rectangles.
996 * (I.e., do not care about cond_np2 here, just look for
997 * GL_TEXTURE_RECTANGLE_ARB.) */
998 if (target
!= GL_TEXTURE_RECTANGLE_ARB
)
1000 TRACE("Setting GL_TEXTURE_MAX_LEVEL to %u.\n", texture
->level_count
- 1);
1001 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, texture
->level_count
- 1);
1002 checkGLcall("glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, texture->level_count)");
1005 if (target
== GL_TEXTURE_CUBE_MAP_ARB
)
1007 /* Cubemaps are always set to clamp, regardless of the sampler state. */
1008 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
1009 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
1010 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_R
, GL_CLAMP_TO_EDGE
);
1013 if (texture
->flags
& WINED3D_TEXTURE_COND_NP2
)
1015 /* Conditinal non power of two textures use a different clamping
1016 * default. If we're using the GL_WINE_normalized_texrect partial
1017 * driver emulation, we're dealing with a GL_TEXTURE_2D texture which
1018 * has the address mode set to repeat - something that prevents us
1019 * from hitting the accelerated codepath. Thus manually set the GL
1020 * state. The same applies to filtering. Even if the texture has only
1021 * one mip level, the default LINEAR_MIPMAP_LINEAR filter causes a SW
1022 * fallback on macos. */
1023 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
1024 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
1025 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
1026 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
1027 checkGLcall("glTexParameteri");
1028 gl_tex
->sampler_desc
.address_u
= WINED3D_TADDRESS_CLAMP
;
1029 gl_tex
->sampler_desc
.address_v
= WINED3D_TADDRESS_CLAMP
;
1030 gl_tex
->sampler_desc
.mag_filter
= WINED3D_TEXF_POINT
;
1031 gl_tex
->sampler_desc
.min_filter
= WINED3D_TEXF_POINT
;
1032 gl_tex
->sampler_desc
.mip_filter
= WINED3D_TEXF_NONE
;
1035 if (gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
] && gl_info
->supported
[ARB_DEPTH_TEXTURE
])
1037 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_DEPTH_TEXTURE_MODE_ARB
, GL_INTENSITY
);
1038 checkGLcall("glTexParameteri(GL_DEPTH_TEXTURE_MODE_ARB, GL_INTENSITY)");
1041 if (!is_identity_fixup(fixup
) && can_use_texture_swizzle(gl_info
, format
))
1043 static const GLenum swizzle_source
[] =
1045 GL_ZERO
, /* CHANNEL_SOURCE_ZERO */
1046 GL_ONE
, /* CHANNEL_SOURCE_ONE */
1047 GL_RED
, /* CHANNEL_SOURCE_X */
1048 GL_GREEN
, /* CHANNEL_SOURCE_Y */
1049 GL_BLUE
, /* CHANNEL_SOURCE_Z */
1050 GL_ALPHA
, /* CHANNEL_SOURCE_W */
1058 swizzle
.x
= swizzle_source
[fixup
.x_source
];
1059 swizzle
.y
= swizzle_source
[fixup
.y_source
];
1060 swizzle
.z
= swizzle_source
[fixup
.z_source
];
1061 swizzle
.w
= swizzle_source
[fixup
.w_source
];
1062 gl_info
->gl_ops
.gl
.p_glTexParameteriv(target
, GL_TEXTURE_SWIZZLE_RGBA
, &swizzle
.x
);
1063 checkGLcall("glTexParameteriv(GL_TEXTURE_SWIZZLE_RGBA)");
1067 /* Context activation is done by the caller. */
1068 void wined3d_texture_bind_and_dirtify(struct wined3d_texture
*texture
,
1069 struct wined3d_context
*context
, BOOL srgb
)
1071 /* We don't need a specific texture unit, but after binding the texture
1072 * the current unit is dirty. Read the unit back instead of switching to
1073 * 0, this avoids messing around with the state manager's GL states. The
1074 * current texture unit should always be a valid one.
1076 * To be more specific, this is tricky because we can implicitly be
1077 * called from sampler() in state.c. This means we can't touch anything
1078 * other than whatever happens to be the currently active texture, or we
1079 * would risk marking already applied sampler states dirty again. */
1080 if (context
->active_texture
< ARRAY_SIZE(context
->rev_tex_unit_map
))
1082 DWORD active_sampler
= context
->rev_tex_unit_map
[context
->active_texture
];
1083 if (active_sampler
!= WINED3D_UNMAPPED_STAGE
)
1084 context_invalidate_state(context
, STATE_SAMPLER(active_sampler
));
1086 /* FIXME: Ideally we'd only do this when touching a binding that's used by
1088 context_invalidate_compute_state(context
, STATE_COMPUTE_SHADER_RESOURCE_BINDING
);
1089 context_invalidate_state(context
, STATE_GRAPHICS_SHADER_RESOURCE_BINDING
);
1091 wined3d_texture_bind(texture
, context
, srgb
);
1094 /* Context activation is done by the caller (state handler). */
1095 /* This function relies on the correct texture being bound and loaded. */
1096 void wined3d_texture_apply_sampler_desc(struct wined3d_texture
*texture
,
1097 const struct wined3d_sampler_desc
*sampler_desc
, const struct wined3d_context
*context
)
1099 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1100 GLenum target
= texture
->target
;
1101 struct gl_texture
*gl_tex
;
1104 TRACE("texture %p, sampler_desc %p, context %p.\n", texture
, sampler_desc
, context
);
1106 gl_tex
= wined3d_texture_get_gl_texture(texture
, texture
->flags
& WINED3D_TEXTURE_IS_SRGB
);
1108 state
= sampler_desc
->address_u
;
1109 if (state
!= gl_tex
->sampler_desc
.address_u
)
1111 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_S
,
1112 gl_info
->wrap_lookup
[state
- WINED3D_TADDRESS_WRAP
]);
1113 gl_tex
->sampler_desc
.address_u
= state
;
1116 state
= sampler_desc
->address_v
;
1117 if (state
!= gl_tex
->sampler_desc
.address_v
)
1119 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_T
,
1120 gl_info
->wrap_lookup
[state
- WINED3D_TADDRESS_WRAP
]);
1121 gl_tex
->sampler_desc
.address_v
= state
;
1124 state
= sampler_desc
->address_w
;
1125 if (state
!= gl_tex
->sampler_desc
.address_w
)
1127 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_R
,
1128 gl_info
->wrap_lookup
[state
- WINED3D_TADDRESS_WRAP
]);
1129 gl_tex
->sampler_desc
.address_w
= state
;
1132 if (memcmp(gl_tex
->sampler_desc
.border_color
, sampler_desc
->border_color
,
1133 sizeof(gl_tex
->sampler_desc
.border_color
)))
1135 gl_info
->gl_ops
.gl
.p_glTexParameterfv(target
, GL_TEXTURE_BORDER_COLOR
, &sampler_desc
->border_color
[0]);
1136 memcpy(gl_tex
->sampler_desc
.border_color
, sampler_desc
->border_color
,
1137 sizeof(gl_tex
->sampler_desc
.border_color
));
1140 state
= sampler_desc
->mag_filter
;
1141 if (state
!= gl_tex
->sampler_desc
.mag_filter
)
1143 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MAG_FILTER
, wined3d_gl_mag_filter(state
));
1144 gl_tex
->sampler_desc
.mag_filter
= state
;
1147 if (sampler_desc
->min_filter
!= gl_tex
->sampler_desc
.min_filter
1148 || sampler_desc
->mip_filter
!= gl_tex
->sampler_desc
.mip_filter
)
1150 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MIN_FILTER
,
1151 wined3d_gl_min_mip_filter(sampler_desc
->min_filter
, sampler_desc
->mip_filter
));
1152 gl_tex
->sampler_desc
.min_filter
= sampler_desc
->min_filter
;
1153 gl_tex
->sampler_desc
.mip_filter
= sampler_desc
->mip_filter
;
1156 state
= sampler_desc
->max_anisotropy
;
1157 if (state
!= gl_tex
->sampler_desc
.max_anisotropy
)
1159 if (gl_info
->supported
[ARB_TEXTURE_FILTER_ANISOTROPIC
])
1160 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MAX_ANISOTROPY
, state
);
1162 WARN("Anisotropic filtering not supported.\n");
1163 gl_tex
->sampler_desc
.max_anisotropy
= state
;
1166 if (!sampler_desc
->srgb_decode
!= !gl_tex
->sampler_desc
.srgb_decode
1167 && (context
->d3d_info
->wined3d_creation_flags
& WINED3D_SRGB_READ_WRITE_CONTROL
)
1168 && gl_info
->supported
[EXT_TEXTURE_SRGB_DECODE
])
1170 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_SRGB_DECODE_EXT
,
1171 sampler_desc
->srgb_decode
? GL_DECODE_EXT
: GL_SKIP_DECODE_EXT
);
1172 gl_tex
->sampler_desc
.srgb_decode
= sampler_desc
->srgb_decode
;
1175 if (!sampler_desc
->compare
!= !gl_tex
->sampler_desc
.compare
)
1177 if (sampler_desc
->compare
)
1178 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_COMPARE_MODE_ARB
, GL_COMPARE_R_TO_TEXTURE_ARB
);
1180 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_COMPARE_MODE_ARB
, GL_NONE
);
1181 gl_tex
->sampler_desc
.compare
= sampler_desc
->compare
;
1184 checkGLcall("Texture parameter application");
1186 if (gl_info
->supported
[EXT_TEXTURE_LOD_BIAS
])
1188 gl_info
->gl_ops
.gl
.p_glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT
,
1189 GL_TEXTURE_LOD_BIAS_EXT
, sampler_desc
->lod_bias
);
1190 checkGLcall("glTexEnvf(GL_TEXTURE_LOD_BIAS_EXT, ...)");
1194 ULONG CDECL
wined3d_texture_incref(struct wined3d_texture
*texture
)
1198 TRACE("texture %p, swapchain %p.\n", texture
, texture
->swapchain
);
1200 if (texture
->swapchain
)
1201 return wined3d_swapchain_incref(texture
->swapchain
);
1203 refcount
= InterlockedIncrement(&texture
->resource
.ref
);
1204 TRACE("%p increasing refcount to %u.\n", texture
, refcount
);
1209 static void wined3d_texture_cleanup_sync(struct wined3d_texture
*texture
)
1211 wined3d_texture_sub_resources_destroyed(texture
);
1212 resource_cleanup(&texture
->resource
);
1213 wined3d_resource_wait_idle(&texture
->resource
);
1214 wined3d_texture_cleanup(texture
);
1217 static void wined3d_texture_destroy_object(void *object
)
1219 wined3d_texture_cleanup(object
);
1223 ULONG CDECL
wined3d_texture_decref(struct wined3d_texture
*texture
)
1227 TRACE("texture %p, swapchain %p.\n", texture
, texture
->swapchain
);
1229 if (texture
->swapchain
)
1230 return wined3d_swapchain_decref(texture
->swapchain
);
1232 refcount
= InterlockedDecrement(&texture
->resource
.ref
);
1233 TRACE("%p decreasing refcount to %u.\n", texture
, refcount
);
1237 /* Wait for the texture to become idle if it's using user memory,
1238 * since the application is allowed to free that memory once the
1239 * texture is destroyed. Note that this implies that
1240 * wined3d_texture_destroy_object() can't access that memory either. */
1241 if (texture
->user_memory
)
1242 wined3d_resource_wait_idle(&texture
->resource
);
1243 wined3d_texture_sub_resources_destroyed(texture
);
1244 texture
->resource
.parent_ops
->wined3d_object_destroyed(texture
->resource
.parent
);
1245 resource_cleanup(&texture
->resource
);
1246 wined3d_cs_destroy_object(texture
->resource
.device
->cs
, wined3d_texture_destroy_object
, texture
);
1252 struct wined3d_resource
* CDECL
wined3d_texture_get_resource(struct wined3d_texture
*texture
)
1254 TRACE("texture %p.\n", texture
);
1256 return &texture
->resource
;
1259 static BOOL
color_key_equal(const struct wined3d_color_key
*c1
, struct wined3d_color_key
*c2
)
1261 return c1
->color_space_low_value
== c2
->color_space_low_value
1262 && c1
->color_space_high_value
== c2
->color_space_high_value
;
1265 /* Context activation is done by the caller */
1266 void wined3d_texture_load(struct wined3d_texture
*texture
,
1267 struct wined3d_context
*context
, BOOL srgb
)
1269 UINT sub_count
= texture
->level_count
* texture
->layer_count
;
1270 const struct wined3d_d3d_info
*d3d_info
= context
->d3d_info
;
1274 TRACE("texture %p, context %p, srgb %#x.\n", texture
, context
, srgb
);
1276 if (!needs_separate_srgb_gl_texture(context
, texture
))
1280 flag
= WINED3D_TEXTURE_SRGB_VALID
;
1282 flag
= WINED3D_TEXTURE_RGB_VALID
;
1284 if (!d3d_info
->shader_color_key
1285 && (!(texture
->async
.flags
& WINED3D_TEXTURE_ASYNC_COLOR_KEY
)
1286 != !(texture
->async
.color_key_flags
& WINED3D_CKEY_SRC_BLT
)
1287 || (texture
->async
.flags
& WINED3D_TEXTURE_ASYNC_COLOR_KEY
1288 && !color_key_equal(&texture
->async
.gl_color_key
, &texture
->async
.src_blt_color_key
))))
1290 unsigned int sub_count
= texture
->level_count
* texture
->layer_count
;
1293 TRACE("Reloading because of color key value change.\n");
1294 for (i
= 0; i
< sub_count
; i
++)
1296 if (!wined3d_texture_load_location(texture
, i
, context
, texture
->resource
.map_binding
))
1297 ERR("Failed to load location %s.\n", wined3d_debug_location(texture
->resource
.map_binding
));
1299 wined3d_texture_invalidate_location(texture
, i
, ~texture
->resource
.map_binding
);
1302 texture
->async
.gl_color_key
= texture
->async
.src_blt_color_key
;
1305 if (texture
->flags
& flag
)
1307 TRACE("Texture %p not dirty, nothing to do.\n", texture
);
1311 /* Reload the surfaces if the texture is marked dirty. */
1312 for (i
= 0; i
< sub_count
; ++i
)
1314 if (!wined3d_texture_load_location(texture
, i
, context
,
1315 srgb
? WINED3D_LOCATION_TEXTURE_SRGB
: WINED3D_LOCATION_TEXTURE_RGB
))
1316 ERR("Failed to load location (srgb %#x).\n", srgb
);
1318 texture
->flags
|= flag
;
1321 void * CDECL
wined3d_texture_get_parent(const struct wined3d_texture
*texture
)
1323 TRACE("texture %p.\n", texture
);
1325 return texture
->resource
.parent
;
1328 HRESULT
wined3d_texture_check_box_dimensions(const struct wined3d_texture
*texture
,
1329 unsigned int level
, const struct wined3d_box
*box
)
1331 const struct wined3d_format
*format
= texture
->resource
.format
;
1332 unsigned int width_mask
, height_mask
, width
, height
, depth
;
1334 width
= wined3d_texture_get_level_width(texture
, level
);
1335 height
= wined3d_texture_get_level_height(texture
, level
);
1336 depth
= wined3d_texture_get_level_depth(texture
, level
);
1338 if (box
->left
>= box
->right
|| box
->right
> width
1339 || box
->top
>= box
->bottom
|| box
->bottom
> height
1340 || box
->front
>= box
->back
|| box
->back
> depth
)
1342 WARN("Box %s is invalid.\n", debug_box(box
));
1343 return WINEDDERR_INVALIDRECT
;
1346 if (texture
->resource
.format_flags
& WINED3DFMT_FLAG_BLOCKS
)
1348 /* This assumes power of two block sizes, but NPOT block sizes would
1351 * This also assumes that the format's block depth is 1. */
1352 width_mask
= format
->block_width
- 1;
1353 height_mask
= format
->block_height
- 1;
1355 if ((box
->left
& width_mask
) || (box
->top
& height_mask
)
1356 || (box
->right
& width_mask
&& box
->right
!= width
)
1357 || (box
->bottom
& height_mask
&& box
->bottom
!= height
))
1359 WARN("Box %s is misaligned for %ux%u blocks.\n",
1360 debug_box(box
), format
->block_width
, format
->block_height
);
1361 return WINED3DERR_INVALIDCALL
;
1368 void CDECL
wined3d_texture_get_pitch(const struct wined3d_texture
*texture
,
1369 unsigned int level
, unsigned int *row_pitch
, unsigned int *slice_pitch
)
1371 const struct wined3d_resource
*resource
= &texture
->resource
;
1372 unsigned int width
= wined3d_texture_get_level_width(texture
, level
);
1373 unsigned int height
= wined3d_texture_get_level_height(texture
, level
);
1375 if (texture
->row_pitch
)
1377 *row_pitch
= texture
->row_pitch
;
1378 *slice_pitch
= texture
->slice_pitch
;
1382 wined3d_format_calculate_pitch(resource
->format
, resource
->device
->surface_alignment
,
1383 width
, height
, row_pitch
, slice_pitch
);
1386 DWORD CDECL
wined3d_texture_set_lod(struct wined3d_texture
*texture
, DWORD lod
)
1388 DWORD old
= texture
->lod
;
1390 TRACE("texture %p, lod %u.\n", texture
, lod
);
1392 /* The d3d9:texture test shows that SetLOD is ignored on non-managed
1393 * textures. The call always returns 0, and GetLOD always returns 0. */
1394 if (!wined3d_resource_access_is_managed(texture
->resource
.access
))
1396 TRACE("Ignoring LOD on texture with resource access %s.\n",
1397 wined3d_debug_resource_access(texture
->resource
.access
));
1401 if (lod
>= texture
->level_count
)
1402 lod
= texture
->level_count
- 1;
1404 if (texture
->lod
!= lod
)
1406 struct wined3d_device
*device
= texture
->resource
.device
;
1408 wined3d_resource_wait_idle(&texture
->resource
);
1411 texture
->texture_rgb
.base_level
= ~0u;
1412 texture
->texture_srgb
.base_level
= ~0u;
1413 if (texture
->resource
.bind_count
)
1414 wined3d_cs_emit_set_sampler_state(device
->cs
, texture
->sampler
, WINED3D_SAMP_MAX_MIP_LEVEL
,
1415 device
->state
.sampler_states
[texture
->sampler
][WINED3D_SAMP_MAX_MIP_LEVEL
]);
1421 DWORD CDECL
wined3d_texture_get_lod(const struct wined3d_texture
*texture
)
1423 TRACE("texture %p, returning %u.\n", texture
, texture
->lod
);
1425 return texture
->lod
;
1428 DWORD CDECL
wined3d_texture_get_level_count(const struct wined3d_texture
*texture
)
1430 TRACE("texture %p, returning %u.\n", texture
, texture
->level_count
);
1432 return texture
->level_count
;
1435 HRESULT CDECL
wined3d_texture_set_color_key(struct wined3d_texture
*texture
,
1436 DWORD flags
, const struct wined3d_color_key
*color_key
)
1438 struct wined3d_device
*device
= texture
->resource
.device
;
1439 static const DWORD all_flags
= WINED3D_CKEY_DST_BLT
| WINED3D_CKEY_DST_OVERLAY
1440 | WINED3D_CKEY_SRC_BLT
| WINED3D_CKEY_SRC_OVERLAY
;
1442 TRACE("texture %p, flags %#x, color_key %p.\n", texture
, flags
, color_key
);
1444 if (flags
& ~all_flags
)
1446 WARN("Invalid flags passed, returning WINED3DERR_INVALIDCALL.\n");
1447 return WINED3DERR_INVALIDCALL
;
1450 wined3d_cs_emit_set_color_key(device
->cs
, texture
, flags
, color_key
);
1455 /* In D3D the depth stencil dimensions have to be greater than or equal to the
1456 * render target dimensions. With FBOs, the dimensions have to be an exact match. */
1457 /* TODO: We should synchronize the renderbuffer's content with the texture's content. */
1458 /* Context activation is done by the caller. */
1459 void wined3d_texture_set_compatible_renderbuffer(struct wined3d_texture
*texture
,
1460 struct wined3d_context
*context
, unsigned int level
, const struct wined3d_rendertarget_info
*rt
)
1462 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1463 struct wined3d_renderbuffer_entry
*entry
;
1464 unsigned int src_width
, src_height
;
1465 unsigned int width
, height
;
1466 GLuint renderbuffer
= 0;
1468 if (gl_info
->supported
[ARB_FRAMEBUFFER_OBJECT
])
1471 if (rt
&& rt
->resource
->format
->id
!= WINED3DFMT_NULL
)
1473 struct wined3d_texture
*rt_texture
;
1474 unsigned int rt_level
;
1476 if (rt
->resource
->type
== WINED3D_RTYPE_BUFFER
)
1478 FIXME("Unsupported resource type %s.\n", debug_d3dresourcetype(rt
->resource
->type
));
1481 rt_texture
= wined3d_texture_from_resource(rt
->resource
);
1482 rt_level
= rt
->sub_resource_idx
% rt_texture
->level_count
;
1484 width
= wined3d_texture_get_level_pow2_width(rt_texture
, rt_level
);
1485 height
= wined3d_texture_get_level_pow2_height(rt_texture
, rt_level
);
1489 width
= wined3d_texture_get_level_pow2_width(texture
, level
);
1490 height
= wined3d_texture_get_level_pow2_height(texture
, level
);
1493 src_width
= wined3d_texture_get_level_pow2_width(texture
, level
);
1494 src_height
= wined3d_texture_get_level_pow2_height(texture
, level
);
1496 /* A depth stencil smaller than the render target is not valid */
1497 if (width
> src_width
|| height
> src_height
)
1500 /* Remove any renderbuffer set if the sizes match */
1501 if (width
== src_width
&& height
== src_height
)
1503 texture
->current_renderbuffer
= NULL
;
1507 /* Look if we've already got a renderbuffer of the correct dimensions */
1508 LIST_FOR_EACH_ENTRY(entry
, &texture
->renderbuffers
, struct wined3d_renderbuffer_entry
, entry
)
1510 if (entry
->width
== width
&& entry
->height
== height
)
1512 renderbuffer
= entry
->id
;
1513 texture
->current_renderbuffer
= entry
;
1520 const struct wined3d_format_gl
*format_gl
;
1522 format_gl
= wined3d_format_gl(texture
->resource
.format
);
1523 gl_info
->fbo_ops
.glGenRenderbuffers(1, &renderbuffer
);
1524 gl_info
->fbo_ops
.glBindRenderbuffer(GL_RENDERBUFFER
, renderbuffer
);
1525 gl_info
->fbo_ops
.glRenderbufferStorage(GL_RENDERBUFFER
, format_gl
->internal
, width
, height
);
1527 entry
= heap_alloc(sizeof(*entry
));
1528 entry
->width
= width
;
1529 entry
->height
= height
;
1530 entry
->id
= renderbuffer
;
1531 list_add_head(&texture
->renderbuffers
, &entry
->entry
);
1533 texture
->current_renderbuffer
= entry
;
1536 checkGLcall("set compatible renderbuffer");
1539 HRESULT CDECL
wined3d_texture_update_desc(struct wined3d_texture
*texture
, UINT width
, UINT height
,
1540 enum wined3d_format_id format_id
, enum wined3d_multisample_type multisample_type
,
1541 UINT multisample_quality
, void *mem
, UINT pitch
)
1543 struct wined3d_texture_sub_resource
*sub_resource
;
1544 const struct wined3d_d3d_info
*d3d_info
;
1545 const struct wined3d_gl_info
*gl_info
;
1546 const struct wined3d_format
*format
;
1547 struct wined3d_device
*device
;
1548 unsigned int resource_size
;
1549 DWORD valid_location
= 0;
1550 BOOL create_dib
= FALSE
;
1552 TRACE("texture %p, width %u, height %u, format %s, multisample_type %#x, multisample_quality %u, "
1553 "mem %p, pitch %u.\n",
1554 texture
, width
, height
, debug_d3dformat(format_id
), multisample_type
, multisample_quality
, mem
, pitch
);
1556 device
= texture
->resource
.device
;
1557 gl_info
= &device
->adapter
->gl_info
;
1558 d3d_info
= &device
->adapter
->d3d_info
;
1559 format
= wined3d_get_format(device
->adapter
, format_id
, texture
->resource
.usage
);
1560 resource_size
= wined3d_format_calculate_size(format
, device
->surface_alignment
, width
, height
, 1);
1563 return WINED3DERR_INVALIDCALL
;
1565 if (texture
->level_count
* texture
->layer_count
> 1)
1567 WARN("Texture has multiple sub-resources, not supported.\n");
1568 return WINED3DERR_INVALIDCALL
;
1571 if (texture
->resource
.type
!= WINED3D_RTYPE_TEXTURE_2D
)
1573 WARN("Not supported on %s.\n", debug_d3dresourcetype(texture
->resource
.type
));
1574 return WINED3DERR_INVALIDCALL
;
1577 if (texture
->resource
.map_count
)
1579 WARN("Texture is mapped.\n");
1580 return WINED3DERR_INVALIDCALL
;
1583 /* We have no way of supporting a pitch that is not a multiple of the pixel
1584 * byte width short of uploading the texture row-by-row.
1585 * Fortunately that's not an issue since D3D9Ex doesn't allow a custom pitch
1586 * for user-memory textures (it always expects packed data) while DirectDraw
1587 * requires a 4-byte aligned pitch and doesn't support texture formats
1588 * larger than 4 bytes per pixel nor any format using 3 bytes per pixel.
1589 * This check is here to verify that the assumption holds. */
1590 if (pitch
% texture
->resource
.format
->byte_count
)
1592 WARN("Pitch unsupported, not a multiple of the texture format byte width.\n");
1593 return WINED3DERR_INVALIDCALL
;
1596 if (device
->d3d_initialized
)
1597 wined3d_cs_emit_unload_resource(device
->cs
, &texture
->resource
);
1598 wined3d_resource_wait_idle(&texture
->resource
);
1600 sub_resource
= &texture
->sub_resources
[0];
1601 if (texture
->dc_info
&& texture
->dc_info
[0].dc
)
1603 struct wined3d_texture_idx texture_idx
= {texture
, 0};
1605 wined3d_cs_destroy_object(device
->cs
, wined3d_texture_destroy_dc
, &texture_idx
);
1606 device
->cs
->ops
->finish(device
->cs
, WINED3D_CS_QUEUE_DEFAULT
);
1610 wined3d_resource_free_sysmem(&texture
->resource
);
1612 if ((texture
->row_pitch
= pitch
))
1613 texture
->slice_pitch
= height
* pitch
;
1615 /* User memory surfaces don't have the regular surface alignment. */
1616 wined3d_format_calculate_pitch(format
, 1, width
, height
,
1617 &texture
->row_pitch
, &texture
->slice_pitch
);
1619 texture
->resource
.format
= format
;
1620 texture
->resource
.multisample_type
= multisample_type
;
1621 texture
->resource
.multisample_quality
= multisample_quality
;
1622 texture
->resource
.width
= width
;
1623 texture
->resource
.height
= height
;
1624 texture
->resource
.size
= texture
->slice_pitch
;
1625 sub_resource
->size
= texture
->slice_pitch
;
1626 sub_resource
->locations
= WINED3D_LOCATION_DISCARDED
;
1628 if (multisample_type
&& gl_info
->supported
[ARB_TEXTURE_MULTISAMPLE
])
1629 texture
->target
= GL_TEXTURE_2D_MULTISAMPLE
;
1631 texture
->target
= GL_TEXTURE_2D
;
1633 if (((width
& (width
- 1)) || (height
& (height
- 1))) && !d3d_info
->texture_npot
1634 && !d3d_info
->texture_npot_conditional
)
1636 texture
->flags
|= WINED3D_TEXTURE_COND_NP2_EMULATED
;
1637 texture
->pow2_width
= texture
->pow2_height
= 1;
1638 while (texture
->pow2_width
< width
)
1639 texture
->pow2_width
<<= 1;
1640 while (texture
->pow2_height
< height
)
1641 texture
->pow2_height
<<= 1;
1645 texture
->flags
&= ~WINED3D_TEXTURE_COND_NP2_EMULATED
;
1646 texture
->pow2_width
= width
;
1647 texture
->pow2_height
= height
;
1650 if ((texture
->user_memory
= mem
))
1652 texture
->resource
.map_binding
= WINED3D_LOCATION_USER_MEMORY
;
1653 valid_location
= WINED3D_LOCATION_USER_MEMORY
;
1657 wined3d_texture_prepare_location(texture
, 0, NULL
, WINED3D_LOCATION_SYSMEM
);
1658 valid_location
= WINED3D_LOCATION_SYSMEM
;
1661 /* The format might be changed to a format that needs conversion.
1662 * If the surface didn't use PBOs previously but could now, don't
1663 * change it - whatever made us not use PBOs might come back, e.g.
1665 if (texture
->resource
.map_binding
== WINED3D_LOCATION_BUFFER
&& !wined3d_texture_use_pbo(texture
, gl_info
))
1666 texture
->resource
.map_binding
= WINED3D_LOCATION_SYSMEM
;
1668 wined3d_texture_validate_location(texture
, 0, valid_location
);
1669 wined3d_texture_invalidate_location(texture
, 0, ~valid_location
);
1673 struct wined3d_texture_idx texture_idx
= {texture
, 0};
1675 wined3d_cs_init_object(device
->cs
, wined3d_texture_create_dc
, &texture_idx
);
1676 device
->cs
->ops
->finish(device
->cs
, WINED3D_CS_QUEUE_DEFAULT
);
1682 /* Context activation is done by the caller. */
1683 static void wined3d_texture_prepare_buffer_object(struct wined3d_texture
*texture
,
1684 unsigned int sub_resource_idx
, const struct wined3d_gl_info
*gl_info
)
1686 struct wined3d_texture_sub_resource
*sub_resource
;
1688 sub_resource
= &texture
->sub_resources
[sub_resource_idx
];
1689 if (sub_resource
->buffer_object
)
1692 GL_EXTCALL(glGenBuffers(1, &sub_resource
->buffer_object
));
1693 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER
, sub_resource
->buffer_object
));
1694 GL_EXTCALL(glBufferData(GL_PIXEL_UNPACK_BUFFER
, sub_resource
->size
, NULL
, GL_STREAM_DRAW
));
1695 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER
, 0));
1696 checkGLcall("Create buffer object");
1698 TRACE("Created buffer object %u for texture %p, sub-resource %u.\n",
1699 sub_resource
->buffer_object
, texture
, sub_resource_idx
);
1702 static void wined3d_texture_force_reload(struct wined3d_texture
*texture
)
1704 unsigned int sub_count
= texture
->level_count
* texture
->layer_count
;
1707 texture
->flags
&= ~(WINED3D_TEXTURE_RGB_ALLOCATED
| WINED3D_TEXTURE_SRGB_ALLOCATED
1708 | WINED3D_TEXTURE_CONVERTED
);
1709 texture
->async
.flags
&= ~WINED3D_TEXTURE_ASYNC_COLOR_KEY
;
1710 for (i
= 0; i
< sub_count
; ++i
)
1712 wined3d_texture_invalidate_location(texture
, i
,
1713 WINED3D_LOCATION_TEXTURE_RGB
| WINED3D_LOCATION_TEXTURE_SRGB
);
1717 /* Context activation is done by the caller. */
1718 void wined3d_texture_prepare_texture(struct wined3d_texture
*texture
, struct wined3d_context
*context
, BOOL srgb
)
1720 DWORD alloc_flag
= srgb
? WINED3D_TEXTURE_SRGB_ALLOCATED
: WINED3D_TEXTURE_RGB_ALLOCATED
;
1721 const struct wined3d_d3d_info
*d3d_info
= context
->d3d_info
;
1722 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1723 struct wined3d_resource
*resource
= &texture
->resource
;
1724 const struct wined3d_device
*device
= resource
->device
;
1725 const struct wined3d_format
*format
= resource
->format
;
1726 const struct wined3d_color_key_conversion
*conversion
;
1727 const struct wined3d_format_gl
*format_gl
;
1730 TRACE("texture %p, context %p, format %s.\n", texture
, context
, debug_d3dformat(format
->id
));
1732 if (!d3d_info
->shader_color_key
1733 && !(texture
->async
.flags
& WINED3D_TEXTURE_ASYNC_COLOR_KEY
)
1734 != !(texture
->async
.color_key_flags
& WINED3D_CKEY_SRC_BLT
))
1736 wined3d_texture_force_reload(texture
);
1738 if (texture
->async
.color_key_flags
& WINED3D_CKEY_SRC_BLT
)
1739 texture
->async
.flags
|= WINED3D_TEXTURE_ASYNC_COLOR_KEY
;
1742 if (texture
->flags
& alloc_flag
)
1745 if (resource
->format_flags
& WINED3DFMT_FLAG_DECOMPRESS
)
1747 TRACE("WINED3DFMT_FLAG_DECOMPRESS set.\n");
1748 texture
->flags
|= WINED3D_TEXTURE_CONVERTED
;
1749 format
= wined3d_resource_get_decompress_format(resource
);
1751 else if (format
->conv_byte_count
)
1753 texture
->flags
|= WINED3D_TEXTURE_CONVERTED
;
1755 else if ((conversion
= wined3d_format_get_color_key_conversion(texture
, TRUE
)))
1757 texture
->flags
|= WINED3D_TEXTURE_CONVERTED
;
1758 format
= wined3d_get_format(device
->adapter
, conversion
->dst_format
, resource
->usage
);
1759 TRACE("Using format %s for color key conversion.\n", debug_d3dformat(format
->id
));
1761 format_gl
= wined3d_format_gl(format
);
1763 wined3d_texture_bind_and_dirtify(texture
, context
, srgb
);
1766 internal
= format_gl
->srgb_internal
;
1767 else if (resource
->usage
& WINED3DUSAGE_RENDERTARGET
&& wined3d_resource_is_offscreen(resource
))
1768 internal
= format_gl
->rt_internal
;
1770 internal
= format_gl
->internal
;
1773 FIXME("No GL internal format for format %s.\n", debug_d3dformat(format
->id
));
1775 TRACE("internal %#x, format %#x, type %#x.\n", internal
, format_gl
->format
, format_gl
->type
);
1777 if (wined3d_texture_use_immutable_storage(texture
, gl_info
))
1778 wined3d_texture_allocate_gl_immutable_storage(texture
, internal
, gl_info
);
1780 wined3d_texture_allocate_gl_mutable_storage(texture
, internal
, format_gl
, gl_info
);
1781 texture
->flags
|= alloc_flag
;
1784 static void wined3d_texture_prepare_rb(struct wined3d_texture
*texture
,
1785 const struct wined3d_gl_info
*gl_info
, BOOL multisample
)
1787 const struct wined3d_format_gl
*format_gl
;
1789 format_gl
= wined3d_format_gl(texture
->resource
.format
);
1794 if (texture
->rb_multisample
)
1797 samples
= wined3d_texture_get_gl_sample_count(texture
);
1799 gl_info
->fbo_ops
.glGenRenderbuffers(1, &texture
->rb_multisample
);
1800 gl_info
->fbo_ops
.glBindRenderbuffer(GL_RENDERBUFFER
, texture
->rb_multisample
);
1801 gl_info
->fbo_ops
.glRenderbufferStorageMultisample(GL_RENDERBUFFER
, samples
,
1802 format_gl
->internal
, texture
->resource
.width
, texture
->resource
.height
);
1803 checkGLcall("glRenderbufferStorageMultisample()");
1804 TRACE("Created multisample rb %u.\n", texture
->rb_multisample
);
1808 if (texture
->rb_resolved
)
1811 gl_info
->fbo_ops
.glGenRenderbuffers(1, &texture
->rb_resolved
);
1812 gl_info
->fbo_ops
.glBindRenderbuffer(GL_RENDERBUFFER
, texture
->rb_resolved
);
1813 gl_info
->fbo_ops
.glRenderbufferStorage(GL_RENDERBUFFER
, format_gl
->internal
,
1814 texture
->resource
.width
, texture
->resource
.height
);
1815 checkGLcall("glRenderbufferStorage()");
1816 TRACE("Created resolved rb %u.\n", texture
->rb_resolved
);
1820 /* Context activation is done by the caller. Context may be NULL in
1821 * WINED3D_NO3D mode. */
1822 BOOL
wined3d_texture_prepare_location(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
,
1823 struct wined3d_context
*context
, DWORD location
)
1827 case WINED3D_LOCATION_SYSMEM
:
1828 if (texture
->resource
.heap_memory
)
1831 if (!wined3d_resource_allocate_sysmem(&texture
->resource
))
1835 case WINED3D_LOCATION_USER_MEMORY
:
1836 if (!texture
->user_memory
)
1837 ERR("Map binding is set to WINED3D_LOCATION_USER_MEMORY but surface->user_memory is NULL.\n");
1840 case WINED3D_LOCATION_BUFFER
:
1841 wined3d_texture_prepare_buffer_object(texture
, sub_resource_idx
, context
->gl_info
);
1844 case WINED3D_LOCATION_TEXTURE_RGB
:
1845 wined3d_texture_prepare_texture(texture
, context
, FALSE
);
1848 case WINED3D_LOCATION_TEXTURE_SRGB
:
1849 wined3d_texture_prepare_texture(texture
, context
, TRUE
);
1852 case WINED3D_LOCATION_DRAWABLE
:
1853 if (!texture
->swapchain
&& wined3d_settings
.offscreen_rendering_mode
!= ORM_BACKBUFFER
)
1854 ERR("Texture %p does not have a drawable.\n", texture
);
1857 case WINED3D_LOCATION_RB_MULTISAMPLE
:
1858 wined3d_texture_prepare_rb(texture
, context
->gl_info
, TRUE
);
1861 case WINED3D_LOCATION_RB_RESOLVED
:
1862 wined3d_texture_prepare_rb(texture
, context
->gl_info
, FALSE
);
1866 ERR("Invalid location %s.\n", wined3d_debug_location(location
));
1871 static struct wined3d_texture_sub_resource
*wined3d_texture_get_sub_resource(struct wined3d_texture
*texture
,
1872 unsigned int sub_resource_idx
)
1874 UINT sub_count
= texture
->level_count
* texture
->layer_count
;
1876 TRACE("texture %p, sub_resource_idx %u.\n", texture
, sub_resource_idx
);
1878 if (sub_resource_idx
>= sub_count
)
1880 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx
, sub_count
);
1884 return &texture
->sub_resources
[sub_resource_idx
];
1887 HRESULT CDECL
wined3d_texture_add_dirty_region(struct wined3d_texture
*texture
,
1888 UINT layer
, const struct wined3d_box
*dirty_region
)
1890 TRACE("texture %p, layer %u, dirty_region %s.\n", texture
, layer
, debug_box(dirty_region
));
1892 if (layer
>= texture
->layer_count
)
1894 WARN("Invalid layer %u specified.\n", layer
);
1895 return WINED3DERR_INVALIDCALL
;
1899 FIXME("Ignoring dirty_region %s.\n", debug_box(dirty_region
));
1901 wined3d_cs_emit_add_dirty_texture_region(texture
->resource
.device
->cs
, texture
, layer
);
1906 /* This call just uploads data, the caller is responsible for binding the
1907 * correct texture. */
1908 /* Context activation is done by the caller. */
1909 void wined3d_texture_upload_data(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
,
1910 struct wined3d_context
*context
, const struct wined3d_format
*format
, const struct wined3d_box
*src_box
,
1911 const struct wined3d_const_bo_address
*data
, unsigned int src_row_pitch
, unsigned int src_slice_pitch
,
1912 unsigned int dst_x
, unsigned int dst_y
, unsigned int dst_z
, BOOL srgb
)
1914 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1915 unsigned int update_w
= src_box
->right
- src_box
->left
;
1916 unsigned int update_h
= src_box
->bottom
- src_box
->top
;
1917 unsigned int update_d
= src_box
->back
- src_box
->front
;
1918 const struct wined3d_format_gl
*format_gl
;
1919 struct wined3d_bo_address bo
;
1920 void *converted_mem
= NULL
;
1921 struct wined3d_format_gl f
;
1926 TRACE("texture %p, sub_resource_idx %u, context %p, format %s, src_box %s, data {%#x:%p}, "
1927 "src_row_pitch %#x, src_slice_pitch %#x, dst_x %u, dst_y %u, dst_z %u, srgb %#x.\n",
1928 texture
, sub_resource_idx
, context
, debug_d3dformat(format
->id
), debug_box(src_box
),
1929 data
->buffer_object
, data
->addr
, src_row_pitch
, src_slice_pitch
, dst_x
, dst_y
, dst_z
, srgb
);
1931 if (texture
->sub_resources
[sub_resource_idx
].map_count
)
1933 WARN("Uploading a texture that is currently mapped, setting WINED3D_TEXTURE_PIN_SYSMEM.\n");
1934 texture
->flags
|= WINED3D_TEXTURE_PIN_SYSMEM
;
1937 if (format
->flags
[WINED3D_GL_RES_TYPE_TEX_2D
] & WINED3DFMT_FLAG_HEIGHT_SCALE
)
1939 update_h
*= format
->height_scale
.numerator
;
1940 update_h
/= format
->height_scale
.denominator
;
1943 target
= wined3d_texture_get_sub_resource_target(texture
, sub_resource_idx
);
1944 level
= sub_resource_idx
% texture
->level_count
;
1948 case GL_TEXTURE_1D_ARRAY
:
1949 dst_y
= sub_resource_idx
/ texture
->level_count
;
1952 case GL_TEXTURE_2D_ARRAY
:
1953 dst_z
= sub_resource_idx
/ texture
->level_count
;
1956 case GL_TEXTURE_2D_MULTISAMPLE
:
1957 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
1958 FIXME("Not supported for multisample textures.\n");
1962 bo
.buffer_object
= data
->buffer_object
;
1963 bo
.addr
= (BYTE
*)data
->addr
+ src_box
->front
* src_slice_pitch
;
1964 if (texture
->resource
.format_flags
& WINED3DFMT_FLAG_BLOCKS
)
1966 bo
.addr
+= (src_box
->top
/ format
->block_height
) * src_row_pitch
;
1967 bo
.addr
+= (src_box
->left
/ format
->block_width
) * format
->block_byte_count
;
1971 bo
.addr
+= src_box
->top
* src_row_pitch
;
1972 bo
.addr
+= src_box
->left
* format
->byte_count
;
1975 decompress
= texture
->resource
.format_flags
& WINED3DFMT_FLAG_DECOMPRESS
;
1976 if (format
->upload
|| decompress
)
1978 const struct wined3d_format
*compressed_format
= format
;
1979 unsigned int dst_row_pitch
, dst_slice_pitch
;
1984 format
= wined3d_resource_get_decompress_format(&texture
->resource
);
1988 if (texture
->resource
.format_flags
& WINED3DFMT_FLAG_BLOCKS
)
1989 ERR("Converting a block-based format.\n");
1991 f
= *wined3d_format_gl(format
);
1992 f
.f
.byte_count
= format
->conv_byte_count
;
1996 wined3d_format_calculate_pitch(format
, 1, update_w
, update_h
, &dst_row_pitch
, &dst_slice_pitch
);
1998 /* Note that uploading 3D textures may require quite some address
1999 * space; it may make sense to upload them per-slice instead. */
2000 if (!(converted_mem
= heap_calloc(update_d
, dst_slice_pitch
)))
2002 ERR("Failed to allocate upload buffer.\n");
2006 src_mem
= context_map_bo_address(context
, &bo
, src_slice_pitch
,
2007 GL_PIXEL_UNPACK_BUFFER
, WINED3D_MAP_READ
);
2009 compressed_format
->decompress(src_mem
, converted_mem
, src_row_pitch
, src_slice_pitch
,
2010 dst_row_pitch
, dst_slice_pitch
, update_w
, update_h
, update_d
);
2012 format
->upload(src_mem
, converted_mem
, src_row_pitch
, src_slice_pitch
,
2013 dst_row_pitch
, dst_slice_pitch
, update_w
, update_h
, update_d
);
2014 context_unmap_bo_address(context
, &bo
, GL_PIXEL_UNPACK_BUFFER
);
2016 bo
.buffer_object
= 0;
2017 bo
.addr
= converted_mem
;
2018 src_row_pitch
= dst_row_pitch
;
2019 src_slice_pitch
= dst_slice_pitch
;
2022 if (bo
.buffer_object
)
2024 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER
, bo
.buffer_object
));
2025 checkGLcall("glBindBuffer");
2028 format_gl
= wined3d_format_gl(format
);
2029 if (format
->flags
[WINED3D_GL_RES_TYPE_TEX_2D
] & WINED3DFMT_FLAG_COMPRESSED
)
2031 unsigned int dst_row_pitch
, dst_slice_pitch
;
2032 const BYTE
*addr
= bo
.addr
;
2036 internal
= format_gl
->srgb_internal
;
2037 else if (texture
->resource
.usage
& WINED3DUSAGE_RENDERTARGET
2038 && wined3d_resource_is_offscreen(&texture
->resource
))
2039 internal
= format_gl
->rt_internal
;
2041 internal
= format_gl
->internal
;
2043 wined3d_format_calculate_pitch(format
, 1, update_w
, update_h
, &dst_row_pitch
, &dst_slice_pitch
);
2045 TRACE("Uploading compressed data, target %#x, level %u, x %u, y %u, z %u, "
2046 "w %u, h %u, d %u, format %#x, image_size %#x, addr %p.\n",
2047 target
, level
, dst_x
, dst_y
, dst_z
, update_w
, update_h
,
2048 update_d
, internal
, dst_slice_pitch
, addr
);
2050 if (target
== GL_TEXTURE_1D
)
2052 GL_EXTCALL(glCompressedTexSubImage1D(target
, level
, dst_x
,
2053 update_w
, internal
, dst_row_pitch
, addr
));
2055 else if (dst_row_pitch
== src_row_pitch
)
2057 if (target
== GL_TEXTURE_2D_ARRAY
|| target
== GL_TEXTURE_3D
)
2059 GL_EXTCALL(glCompressedTexSubImage3D(target
, level
, dst_x
, dst_y
, dst_z
,
2060 update_w
, update_h
, update_d
, internal
, dst_slice_pitch
* update_d
, addr
));
2064 GL_EXTCALL(glCompressedTexSubImage2D(target
, level
, dst_x
, dst_y
,
2065 update_w
, update_h
, internal
, dst_slice_pitch
, addr
));
2070 unsigned int row_count
= (update_h
+ format
->block_height
- 1) / format
->block_height
;
2071 unsigned int row
, y
, z
;
2073 /* glCompressedTexSubImage2D() ignores pixel store state, so we
2074 * can't use the unpack row length like for glTexSubImage2D. */
2075 for (z
= dst_z
; z
< dst_z
+ update_d
; ++z
)
2077 for (row
= 0, y
= dst_y
; row
< row_count
; ++row
)
2079 if (target
== GL_TEXTURE_2D_ARRAY
|| target
== GL_TEXTURE_3D
)
2081 GL_EXTCALL(glCompressedTexSubImage3D(target
, level
, dst_x
, y
, z
,
2082 update_w
, format
->block_height
, 1, internal
, dst_row_pitch
, addr
));
2086 GL_EXTCALL(glCompressedTexSubImage2D(target
, level
, dst_x
, y
,
2087 update_w
, format
->block_height
, internal
, dst_row_pitch
, addr
));
2090 y
+= format
->block_height
;
2091 addr
+= src_row_pitch
;
2095 checkGLcall("Upload compressed texture data");
2099 TRACE("Uploading data, target %#x, level %u, x %u, y %u, z %u, "
2100 "w %u, h %u, d %u, format %#x, type %#x, addr %p.\n",
2101 target
, level
, dst_x
, dst_y
, dst_z
, update_w
, update_h
,
2102 update_d
, format_gl
->format
, format_gl
->type
, bo
.addr
);
2104 gl_info
->gl_ops
.gl
.p_glPixelStorei(GL_UNPACK_ROW_LENGTH
, src_row_pitch
/ format
->byte_count
);
2105 if (target
== GL_TEXTURE_2D_ARRAY
|| target
== GL_TEXTURE_3D
)
2107 GL_EXTCALL(glTexSubImage3D(target
, level
, dst_x
, dst_y
, dst_z
,
2108 update_w
, update_h
, update_d
, format_gl
->format
, format_gl
->type
, bo
.addr
));
2110 else if (target
== GL_TEXTURE_1D
)
2112 gl_info
->gl_ops
.gl
.p_glTexSubImage1D(target
, level
, dst_x
,
2113 update_w
, format_gl
->format
, format_gl
->type
, bo
.addr
);
2117 gl_info
->gl_ops
.gl
.p_glTexSubImage2D(target
, level
, dst_x
, dst_y
,
2118 update_w
, update_h
, format_gl
->format
, format_gl
->type
, bo
.addr
);
2120 gl_info
->gl_ops
.gl
.p_glPixelStorei(GL_UNPACK_ROW_LENGTH
, 0);
2121 checkGLcall("Upload texture data");
2124 if (bo
.buffer_object
)
2126 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER
, 0));
2127 checkGLcall("glBindBuffer");
2129 heap_free(converted_mem
);
2131 if (gl_info
->quirks
& WINED3D_QUIRK_FBO_TEX_UPDATE
)
2133 struct wined3d_device
*device
= texture
->resource
.device
;
2136 for (i
= 0; i
< device
->context_count
; ++i
)
2138 context_texture_update(device
->contexts
[i
], texture
);
2143 static void texture2d_download_data(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
,
2144 struct wined3d_context
*context
, const struct wined3d_bo_address
*data
)
2146 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2147 struct wined3d_texture_sub_resource
*sub_resource
;
2148 unsigned int dst_row_pitch
, dst_slice_pitch
;
2149 unsigned int src_row_pitch
, src_slice_pitch
;
2150 const struct wined3d_format_gl
*format_gl
;
2151 BYTE
*temporary_mem
= NULL
;
2156 format_gl
= wined3d_format_gl(texture
->resource
.format
);
2158 /* Only support read back of converted P8 textures. */
2159 if (texture
->flags
& WINED3D_TEXTURE_CONVERTED
&& format_gl
->f
.id
!= WINED3DFMT_P8_UINT
&& !format_gl
->f
.download
)
2161 ERR("Trying to read back converted texture %p, %u with format %s.\n",
2162 texture
, sub_resource_idx
, debug_d3dformat(format_gl
->f
.id
));
2166 sub_resource
= &texture
->sub_resources
[sub_resource_idx
];
2167 target
= wined3d_texture_get_sub_resource_target(texture
, sub_resource_idx
);
2168 level
= sub_resource_idx
% texture
->level_count
;
2170 if (target
== GL_TEXTURE_2D_ARRAY
)
2172 if (format_gl
->f
.download
)
2174 FIXME("Reading back converted array texture %p is not supported.\n", texture
);
2178 /* NP2 emulation is not allowed on array textures. */
2179 if (texture
->flags
& WINED3D_TEXTURE_COND_NP2_EMULATED
)
2180 ERR("Array texture %p uses NP2 emulation.\n", texture
);
2182 WARN_(d3d_perf
)("Downloading all miplevel layers to get the data for a single sub-resource.\n");
2184 if (!(temporary_mem
= heap_calloc(texture
->layer_count
, sub_resource
->size
)))
2186 ERR("Out of memory.\n");
2191 if (texture
->flags
& WINED3D_TEXTURE_COND_NP2_EMULATED
)
2193 if (format_gl
->f
.download
)
2195 FIXME("Reading back converted texture %p with NP2 emulation is not supported.\n", texture
);
2199 wined3d_texture_get_pitch(texture
, level
, &dst_row_pitch
, &dst_slice_pitch
);
2200 wined3d_format_calculate_pitch(&format_gl
->f
, texture
->resource
.device
->surface_alignment
,
2201 wined3d_texture_get_level_pow2_width(texture
, level
),
2202 wined3d_texture_get_level_pow2_height(texture
, level
),
2203 &src_row_pitch
, &src_slice_pitch
);
2204 if (!(temporary_mem
= heap_alloc(src_slice_pitch
)))
2206 ERR("Out of memory.\n");
2210 if (data
->buffer_object
)
2211 ERR("NP2 emulated texture uses PBO unexpectedly.\n");
2212 if (texture
->resource
.format_flags
& WINED3DFMT_FLAG_COMPRESSED
)
2213 ERR("Unexpected compressed format for NP2 emulated texture.\n");
2216 if (format_gl
->f
.download
)
2218 struct wined3d_format f
;
2220 if (data
->buffer_object
)
2221 ERR("Converted texture %p uses PBO unexpectedly.\n", texture
);
2223 WARN_(d3d_perf
)("Downloading converted texture %p, %u with format %s.\n",
2224 texture
, sub_resource_idx
, debug_d3dformat(format_gl
->f
.id
));
2227 f
.byte_count
= format_gl
->f
.conv_byte_count
;
2228 wined3d_texture_get_pitch(texture
, level
, &dst_row_pitch
, &dst_slice_pitch
);
2229 wined3d_format_calculate_pitch(&f
, texture
->resource
.device
->surface_alignment
,
2230 wined3d_texture_get_level_width(texture
, level
),
2231 wined3d_texture_get_level_height(texture
, level
),
2232 &src_row_pitch
, &src_slice_pitch
);
2234 if (!(temporary_mem
= heap_alloc(src_slice_pitch
)))
2236 ERR("Failed to allocate memory.\n");
2243 mem
= temporary_mem
;
2245 else if (data
->buffer_object
)
2247 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER
, data
->buffer_object
));
2248 checkGLcall("glBindBuffer");
2256 if (texture
->resource
.format_flags
& WINED3DFMT_FLAG_COMPRESSED
)
2258 TRACE("Downloading compressed texture %p, %u, level %u, format %#x, type %#x, data %p.\n",
2259 texture
, sub_resource_idx
, level
, format_gl
->format
, format_gl
->type
, mem
);
2261 GL_EXTCALL(glGetCompressedTexImage(target
, level
, mem
));
2262 checkGLcall("glGetCompressedTexImage");
2266 TRACE("Downloading texture %p, %u, level %u, format %#x, type %#x, data %p.\n",
2267 texture
, sub_resource_idx
, level
, format_gl
->format
, format_gl
->type
, mem
);
2269 gl_info
->gl_ops
.gl
.p_glGetTexImage(target
, level
, format_gl
->format
, format_gl
->type
, mem
);
2270 checkGLcall("glGetTexImage");
2273 if (format_gl
->f
.download
)
2275 format_gl
->f
.download(mem
, data
->addr
, src_row_pitch
, src_slice_pitch
, dst_row_pitch
, dst_slice_pitch
,
2276 wined3d_texture_get_level_width(texture
, level
),
2277 wined3d_texture_get_level_height(texture
, level
), 1);
2279 else if (texture
->flags
& WINED3D_TEXTURE_COND_NP2_EMULATED
)
2281 const BYTE
*src_data
;
2284 /* Some games (e.g. Warhammer 40,000) don't properly handle texture
2285 * pitches, preventing us from using the texture pitch to box NPOT
2286 * textures. Instead, we repack the texture's CPU copy so that its
2287 * pitch equals bpp * width instead of bpp * pow2width.
2289 * Instead of boxing the texture:
2291 * │<── texture width ──>│ pow2 width ──>│
2292 * ├─────────────────────┼───────────────┼─
2293 * │111111111111111111111│ │ʌ
2294 * │222222222222222222222│ ││
2295 * │333333333333333333333│ padding │texture height
2296 * │444444444444444444444│ ││
2297 * │555555555555555555555│ │v
2298 * ├─────────────────────┘ ├─
2299 * │ │pow2 height
2300 * │ padding padding ││
2302 * └─────────────────────────────────────┴─
2304 * we're repacking the data to the expected texture width
2306 * │<── texture width ──>│ pow2 width ──>│
2307 * ├─────────────────────┴───────────────┼─
2308 * │1111111111111111111112222222222222222│ʌ
2309 * │2222233333333333333333333344444444444││
2310 * │4444444444555555555555555555555 │texture height
2312 * │ padding padding │v
2314 * │ │pow2 height
2315 * │ padding padding ││
2317 * └─────────────────────────────────────┴─
2321 * │<── texture width ──>│
2322 * ├─────────────────────┼─
2323 * │111111111111111111111│ʌ
2324 * │222222222222222222222││
2325 * │333333333333333333333│texture height
2326 * │444444444444444444444││
2327 * │555555555555555555555│v
2328 * └─────────────────────┴─
2330 * This also means that any references to surface memory should work
2331 * with the data as if it were a standard texture with a NPOT width
2332 * instead of a texture boxed up to be a power-of-two texture. */
2334 dst_data
= data
->addr
;
2335 TRACE("Repacking the surface data from pitch %u to pitch %u.\n", src_row_pitch
, dst_row_pitch
);
2336 h
= wined3d_texture_get_level_height(texture
, level
);
2337 for (y
= 0; y
< h
; ++y
)
2339 memcpy(dst_data
, src_data
, dst_row_pitch
);
2340 src_data
+= src_row_pitch
;
2341 dst_data
+= dst_row_pitch
;
2344 else if (temporary_mem
)
2346 unsigned int layer
= sub_resource_idx
/ texture
->level_count
;
2347 void *src_data
= temporary_mem
+ layer
* sub_resource
->size
;
2348 if (data
->buffer_object
)
2350 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER
, data
->buffer_object
));
2351 checkGLcall("glBindBuffer");
2352 GL_EXTCALL(glBufferSubData(GL_PIXEL_PACK_BUFFER
, 0, sub_resource
->size
, src_data
));
2353 checkGLcall("glBufferSubData");
2357 memcpy(data
->addr
, src_data
, sub_resource
->size
);
2361 if (data
->buffer_object
)
2363 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER
, 0));
2364 checkGLcall("glBindBuffer");
2367 heap_free(temporary_mem
);
2370 /* This call just downloads data, the caller is responsible for binding the
2371 * correct texture. Partial downloads are not supported. */
2372 /* Context activation is done by the caller. */
2373 void wined3d_texture_download_data(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
,
2374 struct wined3d_context
*context
, const struct wined3d_bo_address
*data
)
2376 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2377 const struct wined3d_format_gl
*format_gl
;
2381 format_gl
= wined3d_format_gl(texture
->resource
.format
);
2382 target
= wined3d_texture_get_sub_resource_target(texture
, sub_resource_idx
);
2383 level
= sub_resource_idx
% texture
->level_count
;
2385 if (texture
->resource
.type
== WINED3D_RTYPE_TEXTURE_2D
2386 && (target
== GL_TEXTURE_2D_ARRAY
|| format_gl
->f
.conv_byte_count
2387 || texture
->flags
& (WINED3D_TEXTURE_CONVERTED
| WINED3D_TEXTURE_COND_NP2_EMULATED
)))
2389 /* 2D-specific special cases. */
2390 texture2d_download_data(texture
, sub_resource_idx
, context
, data
);
2394 if (format_gl
->f
.conv_byte_count
)
2396 FIXME("Attempting to download a converted texture, type %s format %s.\n",
2397 debug_d3dresourcetype(texture
->resource
.type
),
2398 debug_d3dformat(format_gl
->f
.id
));
2402 if (data
->buffer_object
)
2404 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER
, data
->buffer_object
));
2405 checkGLcall("glBindBuffer");
2408 if (texture
->resource
.format_flags
& WINED3DFMT_FLAG_COMPRESSED
)
2410 TRACE("Downloading compressed texture %p, %u, level %u, format %#x, type %#x, data %p.\n",
2411 texture
, sub_resource_idx
, level
, format_gl
->format
, format_gl
->type
, data
->addr
);
2413 GL_EXTCALL(glGetCompressedTexImage(target
, level
, data
->addr
));
2414 checkGLcall("glGetCompressedTexImage");
2418 TRACE("Downloading texture %p, %u, level %u, format %#x, type %#x, data %p.\n",
2419 texture
, sub_resource_idx
, level
, format_gl
->format
, format_gl
->type
, data
->addr
);
2421 gl_info
->gl_ops
.gl
.p_glGetTexImage(target
, level
, format_gl
->format
, format_gl
->type
, data
->addr
);
2422 checkGLcall("glGetTexImage");
2425 if (data
->buffer_object
)
2427 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER
, 0));
2428 checkGLcall("glBindBuffer");
2432 /* Context activation is done by the caller. Context may be NULL in ddraw-only mode. */
2433 static BOOL
texture2d_load_location(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
,
2434 struct wined3d_context
*context
, DWORD location
)
2436 TRACE("texture %p, sub_resource_idx %u, context %p, location %s.\n",
2437 texture
, sub_resource_idx
, context
, wined3d_debug_location(location
));
2441 case WINED3D_LOCATION_USER_MEMORY
:
2442 case WINED3D_LOCATION_SYSMEM
:
2443 case WINED3D_LOCATION_BUFFER
:
2444 return texture2d_load_sysmem(texture
, sub_resource_idx
, context
, location
);
2446 case WINED3D_LOCATION_DRAWABLE
:
2447 return texture2d_load_drawable(texture
, sub_resource_idx
, context
);
2449 case WINED3D_LOCATION_RB_RESOLVED
:
2450 case WINED3D_LOCATION_RB_MULTISAMPLE
:
2451 return texture2d_load_renderbuffer(texture
, sub_resource_idx
, context
, location
);
2453 case WINED3D_LOCATION_TEXTURE_RGB
:
2454 case WINED3D_LOCATION_TEXTURE_SRGB
:
2455 return texture2d_load_texture(texture
, sub_resource_idx
, context
,
2456 location
== WINED3D_LOCATION_TEXTURE_SRGB
);
2459 ERR("Don't know how to handle location %#x.\n", location
);
2464 static const struct wined3d_texture_ops texture2d_ops
=
2466 texture2d_load_location
,
2469 struct wined3d_texture
* __cdecl
wined3d_texture_from_resource(struct wined3d_resource
*resource
)
2471 return texture_from_resource(resource
);
2474 static ULONG
texture_resource_incref(struct wined3d_resource
*resource
)
2476 return wined3d_texture_incref(texture_from_resource(resource
));
2479 static ULONG
texture_resource_decref(struct wined3d_resource
*resource
)
2481 return wined3d_texture_decref(texture_from_resource(resource
));
2484 static void texture_resource_preload(struct wined3d_resource
*resource
)
2486 struct wined3d_texture
*texture
= texture_from_resource(resource
);
2487 struct wined3d_context
*context
;
2489 context
= context_acquire(resource
->device
, NULL
, 0);
2490 wined3d_texture_load(texture
, context
, texture
->flags
& WINED3D_TEXTURE_IS_SRGB
);
2491 context_release(context
);
2494 static void wined3d_texture_unload(struct wined3d_resource
*resource
)
2496 struct wined3d_texture
*texture
= texture_from_resource(resource
);
2497 UINT sub_count
= texture
->level_count
* texture
->layer_count
;
2498 struct wined3d_renderbuffer_entry
*entry
, *entry2
;
2499 struct wined3d_device
*device
= resource
->device
;
2500 const struct wined3d_gl_info
*gl_info
;
2501 struct wined3d_context
*context
;
2504 TRACE("texture %p.\n", texture
);
2506 context
= context_acquire(device
, NULL
, 0);
2507 gl_info
= context
->gl_info
;
2509 for (i
= 0; i
< sub_count
; ++i
)
2511 struct wined3d_texture_sub_resource
*sub_resource
= &texture
->sub_resources
[i
];
2513 if (resource
->access
& WINED3D_RESOURCE_ACCESS_CPU
2514 && wined3d_texture_load_location(texture
, i
, context
, resource
->map_binding
))
2516 wined3d_texture_invalidate_location(texture
, i
, ~resource
->map_binding
);
2520 /* We should only get here on device reset/teardown for implicit
2522 if (resource
->access
& WINED3D_RESOURCE_ACCESS_CPU
2523 || resource
->type
!= WINED3D_RTYPE_TEXTURE_2D
)
2524 ERR("Discarding %s %p sub-resource %u with resource access %s.\n",
2525 debug_d3dresourcetype(resource
->type
), resource
, i
,
2526 wined3d_debug_resource_access(resource
->access
));
2527 wined3d_texture_validate_location(texture
, i
, WINED3D_LOCATION_DISCARDED
);
2528 wined3d_texture_invalidate_location(texture
, i
, ~WINED3D_LOCATION_DISCARDED
);
2531 if (sub_resource
->buffer_object
)
2532 wined3d_texture_remove_buffer_object(texture
, i
, context
->gl_info
);
2535 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, &texture
->renderbuffers
, struct wined3d_renderbuffer_entry
, entry
)
2537 context_gl_resource_released(device
, entry
->id
, TRUE
);
2538 gl_info
->fbo_ops
.glDeleteRenderbuffers(1, &entry
->id
);
2539 list_remove(&entry
->entry
);
2542 list_init(&texture
->renderbuffers
);
2543 texture
->current_renderbuffer
= NULL
;
2545 context_release(context
);
2547 wined3d_texture_force_reload(texture
);
2548 wined3d_texture_unload_gl_texture(texture
);
2551 static HRESULT
texture_resource_sub_resource_map(struct wined3d_resource
*resource
, unsigned int sub_resource_idx
,
2552 struct wined3d_map_desc
*map_desc
, const struct wined3d_box
*box
, DWORD flags
)
2554 const struct wined3d_format
*format
= resource
->format
;
2555 struct wined3d_texture_sub_resource
*sub_resource
;
2556 struct wined3d_device
*device
= resource
->device
;
2557 unsigned int fmt_flags
= resource
->format_flags
;
2558 struct wined3d_context
*context
= NULL
;
2559 struct wined3d_texture
*texture
;
2560 struct wined3d_bo_address data
;
2561 unsigned int texture_level
;
2565 TRACE("resource %p, sub_resource_idx %u, map_desc %p, box %s, flags %#x.\n",
2566 resource
, sub_resource_idx
, map_desc
, debug_box(box
), flags
);
2568 texture
= texture_from_resource(resource
);
2569 if (!(sub_resource
= wined3d_texture_get_sub_resource(texture
, sub_resource_idx
)))
2570 return E_INVALIDARG
;
2572 texture_level
= sub_resource_idx
% texture
->level_count
;
2573 if (box
&& FAILED(wined3d_texture_check_box_dimensions(texture
, texture_level
, box
)))
2575 WARN("Map box is invalid.\n");
2576 if (((fmt_flags
& WINED3DFMT_FLAG_BLOCKS
) && !(resource
->access
& WINED3D_RESOURCE_ACCESS_CPU
))
2577 || resource
->type
!= WINED3D_RTYPE_TEXTURE_2D
)
2578 return WINED3DERR_INVALIDCALL
;
2581 if (texture
->flags
& WINED3D_TEXTURE_DC_IN_USE
)
2583 WARN("DC is in use.\n");
2584 return WINED3DERR_INVALIDCALL
;
2587 if (sub_resource
->map_count
)
2589 WARN("Sub-resource is already mapped.\n");
2590 return WINED3DERR_INVALIDCALL
;
2593 if (device
->d3d_initialized
)
2594 context
= context_acquire(device
, NULL
, 0);
2596 if (flags
& WINED3D_MAP_DISCARD
)
2598 TRACE("WINED3D_MAP_DISCARD flag passed, marking %s as up to date.\n",
2599 wined3d_debug_location(resource
->map_binding
));
2600 if ((ret
= wined3d_texture_prepare_location(texture
, sub_resource_idx
, context
, resource
->map_binding
)))
2601 wined3d_texture_validate_location(texture
, sub_resource_idx
, resource
->map_binding
);
2605 if (resource
->usage
& WINED3DUSAGE_DYNAMIC
)
2606 WARN_(d3d_perf
)("Mapping a dynamic texture without WINED3D_MAP_DISCARD.\n");
2607 ret
= wined3d_texture_load_location(texture
, sub_resource_idx
, context
, resource
->map_binding
);
2612 ERR("Failed to prepare location.\n");
2613 context_release(context
);
2614 return E_OUTOFMEMORY
;
2617 if (flags
& WINED3D_MAP_WRITE
2618 && (!(flags
& WINED3D_MAP_NO_DIRTY_UPDATE
) || (resource
->usage
& WINED3DUSAGE_DYNAMIC
)))
2619 wined3d_texture_invalidate_location(texture
, sub_resource_idx
, ~resource
->map_binding
);
2621 wined3d_texture_get_memory(texture
, sub_resource_idx
, &data
, resource
->map_binding
);
2622 base_memory
= context_map_bo_address(context
, &data
, sub_resource
->size
, GL_PIXEL_UNPACK_BUFFER
, flags
);
2623 TRACE("Base memory pointer %p.\n", base_memory
);
2626 context_release(context
);
2628 if (fmt_flags
& WINED3DFMT_FLAG_BROKEN_PITCH
)
2630 map_desc
->row_pitch
= wined3d_texture_get_level_width(texture
, texture_level
) * format
->byte_count
;
2631 map_desc
->slice_pitch
= wined3d_texture_get_level_height(texture
, texture_level
) * map_desc
->row_pitch
;
2635 wined3d_texture_get_pitch(texture
, texture_level
, &map_desc
->row_pitch
, &map_desc
->slice_pitch
);
2640 map_desc
->data
= base_memory
;
2644 if ((fmt_flags
& (WINED3DFMT_FLAG_BLOCKS
| WINED3DFMT_FLAG_BROKEN_PITCH
)) == WINED3DFMT_FLAG_BLOCKS
)
2646 /* Compressed textures are block based, so calculate the offset of
2647 * the block that contains the top-left pixel of the mapped box. */
2648 map_desc
->data
= base_memory
2649 + (box
->front
* map_desc
->slice_pitch
)
2650 + ((box
->top
/ format
->block_height
) * map_desc
->row_pitch
)
2651 + ((box
->left
/ format
->block_width
) * format
->block_byte_count
);
2655 map_desc
->data
= base_memory
2656 + (box
->front
* map_desc
->slice_pitch
)
2657 + (box
->top
* map_desc
->row_pitch
)
2658 + (box
->left
* format
->byte_count
);
2662 if (texture
->swapchain
&& texture
->swapchain
->front_buffer
== texture
)
2664 RECT
*r
= &texture
->swapchain
->front_buffer_update
;
2667 SetRect(r
, 0, 0, resource
->width
, resource
->height
);
2669 SetRect(r
, box
->left
, box
->top
, box
->right
, box
->bottom
);
2670 TRACE("Mapped front buffer %s.\n", wine_dbgstr_rect(r
));
2673 ++resource
->map_count
;
2674 ++sub_resource
->map_count
;
2676 TRACE("Returning memory %p, row pitch %u, slice pitch %u.\n",
2677 map_desc
->data
, map_desc
->row_pitch
, map_desc
->slice_pitch
);
2682 static HRESULT
texture_resource_sub_resource_unmap(struct wined3d_resource
*resource
, unsigned int sub_resource_idx
)
2684 struct wined3d_texture_sub_resource
*sub_resource
;
2685 struct wined3d_device
*device
= resource
->device
;
2686 struct wined3d_context
*context
= NULL
;
2687 struct wined3d_texture
*texture
;
2688 struct wined3d_bo_address data
;
2690 TRACE("resource %p, sub_resource_idx %u.\n", resource
, sub_resource_idx
);
2692 texture
= texture_from_resource(resource
);
2693 if (!(sub_resource
= wined3d_texture_get_sub_resource(texture
, sub_resource_idx
)))
2694 return E_INVALIDARG
;
2696 if (!sub_resource
->map_count
)
2698 WARN("Trying to unmap unmapped sub-resource.\n");
2699 if (texture
->flags
& WINED3D_TEXTURE_DC_IN_USE
)
2701 return WINEDDERR_NOTLOCKED
;
2704 if (device
->d3d_initialized
)
2705 context
= context_acquire(device
, NULL
, 0);
2707 wined3d_texture_get_memory(texture
, sub_resource_idx
, &data
, texture
->resource
.map_binding
);
2708 context_unmap_bo_address(context
, &data
, GL_PIXEL_UNPACK_BUFFER
);
2711 context_release(context
);
2713 if (texture
->swapchain
&& texture
->swapchain
->front_buffer
== texture
)
2715 if (!(sub_resource
->locations
& (WINED3D_LOCATION_DRAWABLE
| WINED3D_LOCATION_TEXTURE_RGB
)))
2716 texture
->swapchain
->swapchain_ops
->swapchain_frontbuffer_updated(texture
->swapchain
);
2719 --sub_resource
->map_count
;
2720 if (!--resource
->map_count
&& texture
->update_map_binding
)
2721 wined3d_texture_update_map_binding(texture
);
2726 static const struct wined3d_resource_ops texture_resource_ops
=
2728 texture_resource_incref
,
2729 texture_resource_decref
,
2730 texture_resource_preload
,
2731 wined3d_texture_unload
,
2732 texture_resource_sub_resource_map
,
2733 texture_resource_sub_resource_unmap
,
2736 /* Context activation is done by the caller. */
2737 static BOOL
texture1d_load_location(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
,
2738 struct wined3d_context
*context
, DWORD location
)
2740 struct wined3d_texture_sub_resource
*sub_resource
= &texture
->sub_resources
[sub_resource_idx
];
2741 unsigned int row_pitch
, slice_pitch
;
2743 TRACE("texture %p, sub_resource_idx %u, context %p, location %s.\n",
2744 texture
, sub_resource_idx
, context
, wined3d_debug_location(location
));
2746 if (!wined3d_texture_prepare_location(texture
, sub_resource_idx
, context
, location
))
2751 case WINED3D_LOCATION_TEXTURE_RGB
:
2752 case WINED3D_LOCATION_TEXTURE_SRGB
:
2753 if (sub_resource
->locations
& WINED3D_LOCATION_SYSMEM
)
2755 struct wined3d_const_bo_address data
= {0, texture
->resource
.heap_memory
};
2756 struct wined3d_box src_box
;
2758 data
.addr
+= sub_resource
->offset
;
2759 wined3d_texture_bind_and_dirtify(texture
, context
,
2760 location
== WINED3D_LOCATION_TEXTURE_SRGB
);
2761 wined3d_texture_get_pitch(texture
, sub_resource_idx
, &row_pitch
, &slice_pitch
);
2762 wined3d_texture_get_level_box(texture
, sub_resource_idx
% texture
->level_count
, &src_box
);
2763 wined3d_texture_upload_data(texture
, sub_resource_idx
, context
, texture
->resource
.format
,
2764 &src_box
, &data
, row_pitch
, slice_pitch
, 0, 0, 0, FALSE
);
2766 else if (sub_resource
->locations
& WINED3D_LOCATION_BUFFER
)
2768 struct wined3d_const_bo_address data
= {sub_resource
->buffer_object
, NULL
};
2769 struct wined3d_box src_box
;
2771 wined3d_texture_bind_and_dirtify(texture
, context
,
2772 location
== WINED3D_LOCATION_TEXTURE_SRGB
);
2773 wined3d_texture_get_pitch(texture
, sub_resource_idx
, &row_pitch
, &slice_pitch
);
2774 wined3d_texture_get_level_box(texture
, sub_resource_idx
% texture
->level_count
, &src_box
);
2775 wined3d_texture_upload_data(texture
, sub_resource_idx
, context
, texture
->resource
.format
,
2776 &src_box
, &data
, row_pitch
, slice_pitch
, 0, 0, 0, FALSE
);
2780 FIXME("Implement texture loading from %s.\n", wined3d_debug_location(sub_resource
->locations
));
2785 case WINED3D_LOCATION_SYSMEM
:
2786 if (sub_resource
->locations
& (WINED3D_LOCATION_TEXTURE_RGB
| WINED3D_LOCATION_TEXTURE_SRGB
))
2788 struct wined3d_bo_address data
= {0, texture
->resource
.heap_memory
};
2790 data
.addr
+= sub_resource
->offset
;
2791 if (sub_resource
->locations
& WINED3D_LOCATION_TEXTURE_RGB
)
2792 wined3d_texture_bind_and_dirtify(texture
, context
, FALSE
);
2794 wined3d_texture_bind_and_dirtify(texture
, context
, TRUE
);
2796 wined3d_texture_download_data(texture
, sub_resource_idx
, context
, &data
);
2797 ++texture
->download_count
;
2801 FIXME("Implement WINED3D_LOCATION_SYSMEM loading from %s.\n",
2802 wined3d_debug_location(sub_resource
->locations
));
2807 case WINED3D_LOCATION_BUFFER
:
2808 if (sub_resource
->locations
& (WINED3D_LOCATION_TEXTURE_RGB
| WINED3D_LOCATION_TEXTURE_SRGB
))
2810 struct wined3d_bo_address data
= {sub_resource
->buffer_object
, NULL
};
2812 if (sub_resource
->locations
& WINED3D_LOCATION_TEXTURE_RGB
)
2813 wined3d_texture_bind_and_dirtify(texture
, context
, FALSE
);
2815 wined3d_texture_bind_and_dirtify(texture
, context
, TRUE
);
2817 wined3d_texture_download_data(texture
, sub_resource_idx
, context
, &data
);
2821 FIXME("Implement WINED3D_LOCATION_BUFFER loading from %s.\n",
2822 wined3d_debug_location(sub_resource
->locations
));
2828 FIXME("Implement %s loading from %s.\n", wined3d_debug_location(location
),
2829 wined3d_debug_location(sub_resource
->locations
));
2836 static const struct wined3d_texture_ops texture1d_ops
=
2838 texture1d_load_location
,
2841 static HRESULT
wined3d_texture_init(struct wined3d_texture
*texture
, const struct wined3d_resource_desc
*desc
,
2842 unsigned int layer_count
, unsigned int level_count
, DWORD flags
, struct wined3d_device
*device
,
2843 void *parent
, const struct wined3d_parent_ops
*parent_ops
, const struct wined3d_texture_ops
*texture_ops
)
2845 const struct wined3d_d3d_info
*d3d_info
= &device
->adapter
->d3d_info
;
2846 struct wined3d_device_parent
*device_parent
= device
->device_parent
;
2847 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
2848 unsigned int sub_count
, i
, j
, size
, offset
= 0;
2849 unsigned int pow2_width
, pow2_height
;
2850 const struct wined3d_format
*format
;
2853 TRACE("texture %p, resource_type %s, format %s, multisample_type %#x, multisample_quality %#x, "
2854 "usage %s, access %s, width %u, height %u, depth %u, layer_count %u, level_count %u, "
2855 "flags %#x, device %p, parent %p, parent_ops %p, texture_ops %p.\n",
2856 texture
, debug_d3dresourcetype(desc
->resource_type
), debug_d3dformat(desc
->format
),
2857 desc
->multisample_type
, desc
->multisample_quality
, debug_d3dusage(desc
->usage
),
2858 wined3d_debug_resource_access(desc
->access
), desc
->width
, desc
->height
, desc
->depth
,
2859 layer_count
, level_count
, flags
, device
, parent
, parent_ops
, texture_ops
);
2861 if (!desc
->width
|| !desc
->height
|| !desc
->depth
)
2862 return WINED3DERR_INVALIDCALL
;
2864 if (desc
->resource_type
== WINED3D_RTYPE_TEXTURE_3D
)
2866 if (layer_count
!= 1)
2868 ERR("Invalid layer count for volume texture.\n");
2869 return E_INVALIDARG
;
2872 if (!gl_info
->supported
[EXT_TEXTURE3D
])
2874 WARN("OpenGL implementation does not support 3D textures.\n");
2875 return WINED3DERR_INVALIDCALL
;
2879 if (!(desc
->usage
& WINED3DUSAGE_LEGACY_CUBEMAP
) && layer_count
> 1
2880 && !gl_info
->supported
[EXT_TEXTURE_ARRAY
])
2882 WARN("OpenGL implementation does not support array textures.\n");
2883 return WINED3DERR_INVALIDCALL
;
2886 /* TODO: It should only be possible to create textures for formats
2887 * that are reported as supported. */
2888 if (WINED3DFMT_UNKNOWN
>= desc
->format
)
2890 WARN("Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n");
2891 return WINED3DERR_INVALIDCALL
;
2893 format
= wined3d_get_format(device
->adapter
, desc
->format
, desc
->usage
);
2895 if (desc
->usage
& WINED3DUSAGE_DYNAMIC
&& (wined3d_resource_access_is_managed(desc
->access
)
2896 || desc
->usage
& WINED3DUSAGE_SCRATCH
))
2898 WARN("Attempted to create a dynamic texture with access %s and usage %s.\n",
2899 wined3d_debug_resource_access(desc
->access
), debug_d3dusage(desc
->usage
));
2900 return WINED3DERR_INVALIDCALL
;
2903 if (!(desc
->usage
& (WINED3DUSAGE_DYNAMIC
| WINED3DUSAGE_RENDERTARGET
| WINED3DUSAGE_DEPTHSTENCIL
))
2904 && (flags
& WINED3D_TEXTURE_CREATE_MAPPABLE
))
2905 WARN("Creating a mappable texture that doesn't specify dynamic usage.\n");
2906 if (desc
->usage
& WINED3DUSAGE_RENDERTARGET
&& desc
->access
& WINED3D_RESOURCE_ACCESS_CPU
)
2907 FIXME("Trying to create a CPU accessible render target.\n");
2909 pow2_width
= desc
->width
;
2910 pow2_height
= desc
->height
;
2911 if (((desc
->width
& (desc
->width
- 1)) || (desc
->height
& (desc
->height
- 1)) || (desc
->depth
& (desc
->depth
- 1)))
2912 && !d3d_info
->texture_npot
)
2914 /* level_count == 0 returns an error as well. */
2915 if (level_count
!= 1 || layer_count
!= 1 || desc
->resource_type
== WINED3D_RTYPE_TEXTURE_3D
)
2917 if (!(desc
->usage
& WINED3DUSAGE_SCRATCH
))
2919 WARN("Attempted to create a mipmapped/cube/array/volume NPOT "
2920 "texture without unconditional NPOT support.\n");
2921 return WINED3DERR_INVALIDCALL
;
2924 WARN("Creating a scratch mipmapped/cube/array NPOT texture despite lack of HW support.\n");
2926 texture
->flags
|= WINED3D_TEXTURE_COND_NP2
;
2928 if (desc
->resource_type
!= WINED3D_RTYPE_TEXTURE_3D
&& !d3d_info
->texture_npot_conditional
)
2930 /* TODO: Add support for non-power-of-two compressed textures. */
2931 if (format
->flags
[WINED3D_GL_RES_TYPE_TEX_2D
]
2932 & (WINED3DFMT_FLAG_COMPRESSED
| WINED3DFMT_FLAG_HEIGHT_SCALE
))
2934 FIXME("Compressed or height scaled non-power-of-two (%ux%u) textures are not supported.\n",
2935 desc
->width
, desc
->height
);
2936 return WINED3DERR_NOTAVAILABLE
;
2939 /* Find the nearest pow2 match. */
2940 pow2_width
= pow2_height
= 1;
2941 while (pow2_width
< desc
->width
)
2943 while (pow2_height
< desc
->height
)
2945 texture
->flags
|= WINED3D_TEXTURE_COND_NP2_EMULATED
;
2948 texture
->pow2_width
= pow2_width
;
2949 texture
->pow2_height
= pow2_height
;
2951 if ((pow2_width
> d3d_info
->limits
.texture_size
|| pow2_height
> d3d_info
->limits
.texture_size
)
2952 && (desc
->usage
& WINED3DUSAGE_TEXTURE
))
2954 /* One of four options:
2955 * 1: Do the same as we do with NPOT and scale the texture. (Any
2956 * texture ops would require the texture to be scaled which is
2957 * potentially slow.)
2958 * 2: Set the texture to the maximum size (bad idea).
2959 * 3: WARN and return WINED3DERR_NOTAVAILABLE.
2960 * 4: Create the surface, but allow it to be used only for DirectDraw
2961 * Blts. Some apps (e.g. Swat 3) create textures with a height of
2962 * 16 and a width > 3000 and blt 16x16 letter areas from them to
2963 * the render target. */
2964 if (desc
->access
& WINED3D_RESOURCE_ACCESS_GPU
)
2966 WARN("Dimensions (%ux%u) exceed the maximum texture size.\n", pow2_width
, pow2_height
);
2967 return WINED3DERR_NOTAVAILABLE
;
2970 /* We should never use this surface in combination with OpenGL. */
2971 TRACE("Creating an oversized (%ux%u) surface.\n", pow2_width
, pow2_height
);
2974 for (i
= 0; i
< layer_count
; ++i
)
2976 for (j
= 0; j
< level_count
; ++j
)
2978 unsigned int idx
= i
* level_count
+ j
;
2980 size
= wined3d_format_calculate_size(format
, device
->surface_alignment
,
2981 max(1, desc
->width
>> j
), max(1, desc
->height
>> j
), max(1, desc
->depth
>> j
));
2982 texture
->sub_resources
[idx
].offset
= offset
;
2983 texture
->sub_resources
[idx
].size
= size
;
2986 offset
= (offset
+ (RESOURCE_ALIGNMENT
- 1)) & ~(RESOURCE_ALIGNMENT
- 1);
2990 return WINED3DERR_INVALIDCALL
;
2992 if (FAILED(hr
= resource_init(&texture
->resource
, device
, desc
->resource_type
, format
,
2993 desc
->multisample_type
, desc
->multisample_quality
, desc
->usage
, desc
->access
,
2994 desc
->width
, desc
->height
, desc
->depth
, offset
, parent
, parent_ops
, &texture_resource_ops
)))
2996 static unsigned int once
;
2998 /* DXTn 3D textures are not supported. Do not write the ERR for them. */
2999 if ((desc
->format
== WINED3DFMT_DXT1
|| desc
->format
== WINED3DFMT_DXT2
|| desc
->format
== WINED3DFMT_DXT3
3000 || desc
->format
== WINED3DFMT_DXT4
|| desc
->format
== WINED3DFMT_DXT5
)
3001 && !(format
->flags
[WINED3D_GL_RES_TYPE_TEX_2D
] & WINED3DFMT_FLAG_TEXTURE
)
3002 && desc
->resource_type
!= WINED3D_RTYPE_TEXTURE_3D
&& !once
++)
3003 ERR_(winediag
)("The application tried to create a DXTn texture, but the driver does not support them.\n");
3005 WARN("Failed to initialize resource, returning %#x\n", hr
);
3008 wined3d_resource_update_draw_binding(&texture
->resource
);
3009 if ((flags
& WINED3D_TEXTURE_CREATE_MAPPABLE
) || desc
->format
== WINED3DFMT_D16_LOCKABLE
)
3010 texture
->resource
.access
|= WINED3D_RESOURCE_ACCESS_MAP_R
| WINED3D_RESOURCE_ACCESS_MAP_W
;
3012 texture
->texture_ops
= texture_ops
;
3014 texture
->layer_count
= layer_count
;
3015 texture
->level_count
= level_count
;
3017 texture
->flags
|= WINED3D_TEXTURE_POW2_MAT_IDENT
| WINED3D_TEXTURE_NORMALIZED_COORDS
;
3018 if (flags
& WINED3D_TEXTURE_CREATE_GET_DC_LENIENT
)
3019 texture
->flags
|= WINED3D_TEXTURE_PIN_SYSMEM
| WINED3D_TEXTURE_GET_DC_LENIENT
;
3020 if (flags
& (WINED3D_TEXTURE_CREATE_GET_DC
| WINED3D_TEXTURE_CREATE_GET_DC_LENIENT
))
3021 texture
->flags
|= WINED3D_TEXTURE_GET_DC
;
3022 if (flags
& WINED3D_TEXTURE_CREATE_DISCARD
)
3023 texture
->flags
|= WINED3D_TEXTURE_DISCARD
;
3024 if (flags
& WINED3D_TEXTURE_CREATE_GENERATE_MIPMAPS
)
3026 if (!(texture
->resource
.format_flags
& WINED3DFMT_FLAG_GEN_MIPMAP
))
3027 WARN("Format doesn't support mipmaps generation, "
3028 "ignoring WINED3D_TEXTURE_CREATE_GENERATE_MIPMAPS flag.\n");
3030 texture
->flags
|= WINED3D_TEXTURE_GENERATE_MIPMAPS
;
3033 list_init(&texture
->renderbuffers
);
3035 switch (desc
->resource_type
)
3037 case WINED3D_RTYPE_TEXTURE_1D
:
3038 if (layer_count
> 1)
3039 texture
->target
= GL_TEXTURE_1D_ARRAY
;
3041 texture
->target
= GL_TEXTURE_1D
;
3044 case WINED3D_RTYPE_TEXTURE_2D
:
3045 if (desc
->usage
& WINED3DUSAGE_LEGACY_CUBEMAP
)
3047 texture
->target
= GL_TEXTURE_CUBE_MAP_ARB
;
3049 else if (desc
->multisample_type
&& gl_info
->supported
[ARB_TEXTURE_MULTISAMPLE
])
3051 if (layer_count
> 1)
3052 texture
->target
= GL_TEXTURE_2D_MULTISAMPLE_ARRAY
;
3054 texture
->target
= GL_TEXTURE_2D_MULTISAMPLE
;
3058 if (layer_count
> 1)
3059 texture
->target
= GL_TEXTURE_2D_ARRAY
;
3061 texture
->target
= GL_TEXTURE_2D
;
3065 case WINED3D_RTYPE_TEXTURE_3D
:
3066 texture
->target
= GL_TEXTURE_3D
;
3070 ERR("Invalid resource type %s.\n", debug_d3dresourcetype(desc
->resource_type
));
3071 wined3d_texture_cleanup_sync(texture
);
3072 return WINED3DERR_INVALIDCALL
;
3075 /* Precalculated scaling for 'faked' non power of two texture coords. */
3076 if (texture
->resource
.gl_type
== WINED3D_GL_RES_TYPE_TEX_RECT
)
3078 texture
->pow2_matrix
[0] = (float)desc
->width
;
3079 texture
->pow2_matrix
[5] = (float)desc
->height
;
3080 texture
->flags
&= ~(WINED3D_TEXTURE_POW2_MAT_IDENT
| WINED3D_TEXTURE_NORMALIZED_COORDS
);
3081 texture
->target
= GL_TEXTURE_RECTANGLE_ARB
;
3083 else if (texture
->flags
& WINED3D_TEXTURE_COND_NP2_EMULATED
)
3085 texture
->pow2_matrix
[0] = (((float)desc
->width
) / ((float)pow2_width
));
3086 texture
->pow2_matrix
[5] = (((float)desc
->height
) / ((float)pow2_height
));
3087 texture
->flags
&= ~WINED3D_TEXTURE_POW2_MAT_IDENT
;
3091 texture
->pow2_matrix
[0] = 1.0f
;
3092 texture
->pow2_matrix
[5] = 1.0f
;
3094 texture
->pow2_matrix
[10] = 1.0f
;
3095 texture
->pow2_matrix
[15] = 1.0f
;
3096 TRACE("x scale %.8e, y scale %.8e.\n", texture
->pow2_matrix
[0], texture
->pow2_matrix
[5]);
3098 if (wined3d_texture_use_pbo(texture
, gl_info
))
3099 texture
->resource
.map_binding
= WINED3D_LOCATION_BUFFER
;
3101 if (desc
->resource_type
!= WINED3D_RTYPE_TEXTURE_3D
3102 || !wined3d_texture_use_pbo(texture
, gl_info
))
3104 if (!wined3d_resource_allocate_sysmem(&texture
->resource
))
3106 wined3d_texture_cleanup_sync(texture
);
3107 return E_OUTOFMEMORY
;
3111 sub_count
= level_count
* layer_count
;
3112 if (sub_count
/ layer_count
!= level_count
)
3114 wined3d_texture_cleanup_sync(texture
);
3115 return E_OUTOFMEMORY
;
3118 if (desc
->usage
& WINED3DUSAGE_OVERLAY
)
3120 if (!(texture
->overlay_info
= heap_calloc(sub_count
, sizeof(*texture
->overlay_info
))))
3122 wined3d_texture_cleanup_sync(texture
);
3123 return E_OUTOFMEMORY
;
3126 for (i
= 0; i
< sub_count
; ++i
)
3128 list_init(&texture
->overlay_info
[i
].entry
);
3129 list_init(&texture
->overlay_info
[i
].overlays
);
3133 /* Generate all sub-resources. */
3134 for (i
= 0; i
< sub_count
; ++i
)
3136 struct wined3d_texture_sub_resource
*sub_resource
;
3138 sub_resource
= &texture
->sub_resources
[i
];
3139 sub_resource
->locations
= WINED3D_LOCATION_DISCARDED
;
3140 if (desc
->resource_type
!= WINED3D_RTYPE_TEXTURE_3D
)
3142 wined3d_texture_validate_location(texture
, i
, WINED3D_LOCATION_SYSMEM
);
3143 wined3d_texture_invalidate_location(texture
, i
, ~WINED3D_LOCATION_SYSMEM
);
3146 if (FAILED(hr
= device_parent
->ops
->texture_sub_resource_created(device_parent
,
3147 desc
->resource_type
, texture
, i
, &sub_resource
->parent
, &sub_resource
->parent_ops
)))
3149 WARN("Failed to create sub-resource parent, hr %#x.\n", hr
);
3150 sub_resource
->parent
= NULL
;
3151 wined3d_texture_cleanup_sync(texture
);
3155 TRACE("parent %p, parent_ops %p.\n", sub_resource
->parent
, sub_resource
->parent_ops
);
3157 TRACE("Created sub-resource %u (level %u, layer %u).\n",
3158 i
, i
% texture
->level_count
, i
/ texture
->level_count
);
3160 if (desc
->usage
& WINED3DUSAGE_OWNDC
)
3162 struct wined3d_texture_idx texture_idx
= {texture
, i
};
3164 wined3d_cs_init_object(device
->cs
, wined3d_texture_create_dc
, &texture_idx
);
3165 device
->cs
->ops
->finish(device
->cs
, WINED3D_CS_QUEUE_DEFAULT
);
3166 if (!texture
->dc_info
|| !texture
->dc_info
[i
].dc
)
3168 wined3d_texture_cleanup_sync(texture
);
3169 return WINED3DERR_INVALIDCALL
;
3177 /* Context activation is done by the caller. */
3178 static void texture3d_srgb_transfer(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
,
3179 struct wined3d_context
*context
, BOOL dest_is_srgb
)
3181 struct wined3d_texture_sub_resource
*sub_resource
= &texture
->sub_resources
[sub_resource_idx
];
3182 unsigned int row_pitch
, slice_pitch
;
3183 struct wined3d_bo_address data
;
3184 struct wined3d_box src_box
;
3186 /* Optimisations are possible, but the effort should be put into either
3187 * implementing EXT_SRGB_DECODE in the driver or finding out why we
3188 * picked the wrong copy for the original upload and fixing that.
3190 * Also keep in mind that we want to avoid using resource.heap_memory
3191 * for DEFAULT pool surfaces. */
3192 WARN_(d3d_perf
)("Performing slow rgb/srgb volume transfer.\n");
3193 data
.buffer_object
= 0;
3194 if (!(data
.addr
= heap_alloc(sub_resource
->size
)))
3197 wined3d_texture_get_pitch(texture
, sub_resource_idx
, &row_pitch
, &slice_pitch
);
3198 wined3d_texture_get_level_box(texture
, sub_resource_idx
% texture
->level_count
, &src_box
);
3199 wined3d_texture_bind_and_dirtify(texture
, context
, !dest_is_srgb
);
3200 wined3d_texture_download_data(texture
, sub_resource_idx
, context
, &data
);
3201 wined3d_texture_bind_and_dirtify(texture
, context
, dest_is_srgb
);
3202 wined3d_texture_upload_data(texture
, sub_resource_idx
, context
, texture
->resource
.format
,
3203 &src_box
, wined3d_const_bo_address(&data
), row_pitch
, slice_pitch
, 0, 0, 0, FALSE
);
3205 heap_free(data
.addr
);
3208 /* Context activation is done by the caller. */
3209 static BOOL
texture3d_load_location(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
,
3210 struct wined3d_context
*context
, DWORD location
)
3212 struct wined3d_texture_sub_resource
*sub_resource
= &texture
->sub_resources
[sub_resource_idx
];
3213 unsigned int row_pitch
, slice_pitch
;
3215 if (!wined3d_texture_prepare_location(texture
, sub_resource_idx
, context
, location
))
3220 case WINED3D_LOCATION_TEXTURE_RGB
:
3221 case WINED3D_LOCATION_TEXTURE_SRGB
:
3222 if (sub_resource
->locations
& WINED3D_LOCATION_SYSMEM
)
3224 struct wined3d_const_bo_address data
= {0, texture
->resource
.heap_memory
};
3225 struct wined3d_box src_box
;
3227 data
.addr
+= sub_resource
->offset
;
3228 wined3d_texture_bind_and_dirtify(texture
, context
,
3229 location
== WINED3D_LOCATION_TEXTURE_SRGB
);
3230 wined3d_texture_get_pitch(texture
, sub_resource_idx
, &row_pitch
, &slice_pitch
);
3231 wined3d_texture_get_level_box(texture
, sub_resource_idx
% texture
->level_count
, &src_box
);
3232 wined3d_texture_upload_data(texture
, sub_resource_idx
, context
, texture
->resource
.format
,
3233 &src_box
, &data
, row_pitch
, slice_pitch
, 0, 0, 0, FALSE
);
3235 else if (sub_resource
->locations
& WINED3D_LOCATION_BUFFER
)
3237 struct wined3d_const_bo_address data
= {sub_resource
->buffer_object
, NULL
};
3238 struct wined3d_box src_box
;
3240 wined3d_texture_bind_and_dirtify(texture
, context
,
3241 location
== WINED3D_LOCATION_TEXTURE_SRGB
);
3242 wined3d_texture_get_pitch(texture
, sub_resource_idx
, &row_pitch
, &slice_pitch
);
3243 wined3d_texture_get_level_box(texture
, sub_resource_idx
% texture
->level_count
, &src_box
);
3244 wined3d_texture_upload_data(texture
, sub_resource_idx
, context
, texture
->resource
.format
,
3245 &src_box
, &data
, row_pitch
, slice_pitch
, 0, 0, 0, FALSE
);
3247 else if (sub_resource
->locations
& WINED3D_LOCATION_TEXTURE_RGB
)
3249 texture3d_srgb_transfer(texture
, sub_resource_idx
, context
, TRUE
);
3251 else if (sub_resource
->locations
& WINED3D_LOCATION_TEXTURE_SRGB
)
3253 texture3d_srgb_transfer(texture
, sub_resource_idx
, context
, FALSE
);
3257 FIXME("Implement texture loading from %s.\n", wined3d_debug_location(sub_resource
->locations
));
3262 case WINED3D_LOCATION_SYSMEM
:
3263 if (sub_resource
->locations
& (WINED3D_LOCATION_TEXTURE_RGB
| WINED3D_LOCATION_TEXTURE_SRGB
))
3265 struct wined3d_bo_address data
= {0, texture
->resource
.heap_memory
};
3267 data
.addr
+= sub_resource
->offset
;
3268 if (sub_resource
->locations
& WINED3D_LOCATION_TEXTURE_RGB
)
3269 wined3d_texture_bind_and_dirtify(texture
, context
, FALSE
);
3271 wined3d_texture_bind_and_dirtify(texture
, context
, TRUE
);
3273 wined3d_texture_download_data(texture
, sub_resource_idx
, context
, &data
);
3274 ++texture
->download_count
;
3278 FIXME("Implement WINED3D_LOCATION_SYSMEM loading from %s.\n",
3279 wined3d_debug_location(sub_resource
->locations
));
3284 case WINED3D_LOCATION_BUFFER
:
3285 if (sub_resource
->locations
& (WINED3D_LOCATION_TEXTURE_RGB
| WINED3D_LOCATION_TEXTURE_SRGB
))
3287 struct wined3d_bo_address data
= {sub_resource
->buffer_object
, NULL
};
3289 if (sub_resource
->locations
& WINED3D_LOCATION_TEXTURE_RGB
)
3290 wined3d_texture_bind_and_dirtify(texture
, context
, FALSE
);
3292 wined3d_texture_bind_and_dirtify(texture
, context
, TRUE
);
3294 wined3d_texture_download_data(texture
, sub_resource_idx
, context
, &data
);
3298 FIXME("Implement WINED3D_LOCATION_BUFFER loading from %s.\n",
3299 wined3d_debug_location(sub_resource
->locations
));
3305 FIXME("Implement %s loading from %s.\n", wined3d_debug_location(location
),
3306 wined3d_debug_location(sub_resource
->locations
));
3313 static const struct wined3d_texture_ops texture3d_ops
=
3315 texture3d_load_location
,
3318 HRESULT CDECL
wined3d_texture_blt(struct wined3d_texture
*dst_texture
, unsigned int dst_sub_resource_idx
,
3319 const RECT
*dst_rect
, struct wined3d_texture
*src_texture
, unsigned int src_sub_resource_idx
,
3320 const RECT
*src_rect
, DWORD flags
, const struct wined3d_blt_fx
*fx
, enum wined3d_texture_filter_type filter
)
3322 struct wined3d_box src_box
= {src_rect
->left
, src_rect
->top
, src_rect
->right
, src_rect
->bottom
, 0, 1};
3323 struct wined3d_box dst_box
= {dst_rect
->left
, dst_rect
->top
, dst_rect
->right
, dst_rect
->bottom
, 0, 1};
3324 unsigned int dst_format_flags
, src_format_flags
= 0;
3327 TRACE("dst_texture %p, dst_sub_resource_idx %u, dst_rect %s, src_texture %p, "
3328 "src_sub_resource_idx %u, src_rect %s, flags %#x, fx %p, filter %s.\n",
3329 dst_texture
, dst_sub_resource_idx
, wine_dbgstr_rect(dst_rect
), src_texture
,
3330 src_sub_resource_idx
, wine_dbgstr_rect(src_rect
), flags
, fx
, debug_d3dtexturefiltertype(filter
));
3332 if (dst_sub_resource_idx
>= dst_texture
->level_count
* dst_texture
->layer_count
3333 || dst_texture
->resource
.type
!= WINED3D_RTYPE_TEXTURE_2D
)
3334 return WINED3DERR_INVALIDCALL
;
3336 if (src_sub_resource_idx
>= src_texture
->level_count
* src_texture
->layer_count
3337 || src_texture
->resource
.type
!= WINED3D_RTYPE_TEXTURE_2D
)
3338 return WINED3DERR_INVALIDCALL
;
3340 dst_format_flags
= dst_texture
->resource
.format_flags
;
3341 if (FAILED(hr
= wined3d_texture_check_box_dimensions(dst_texture
,
3342 dst_sub_resource_idx
% dst_texture
->level_count
, &dst_box
)))
3345 src_format_flags
= src_texture
->resource
.format_flags
;
3346 if (FAILED(hr
= wined3d_texture_check_box_dimensions(src_texture
,
3347 src_sub_resource_idx
% src_texture
->level_count
, &src_box
)))
3350 if (dst_texture
->sub_resources
[dst_sub_resource_idx
].map_count
3351 || src_texture
->sub_resources
[src_sub_resource_idx
].map_count
)
3353 WARN("Sub-resource is busy, returning WINEDDERR_SURFACEBUSY.\n");
3354 return WINEDDERR_SURFACEBUSY
;
3357 if ((src_format_flags
& (WINED3DFMT_FLAG_DEPTH
| WINED3DFMT_FLAG_STENCIL
))
3358 != (dst_format_flags
& (WINED3DFMT_FLAG_DEPTH
| WINED3DFMT_FLAG_STENCIL
)))
3360 WARN("Rejecting depth/stencil blit between incompatible formats.\n");
3361 return WINED3DERR_INVALIDCALL
;
3364 if (dst_texture
->resource
.device
!= src_texture
->resource
.device
)
3366 FIXME("Rejecting cross-device blit.\n");
3370 wined3d_cs_emit_blt_sub_resource(dst_texture
->resource
.device
->cs
, &dst_texture
->resource
, dst_sub_resource_idx
,
3371 &dst_box
, &src_texture
->resource
, src_sub_resource_idx
, &src_box
, flags
, fx
, filter
);
3376 HRESULT CDECL
wined3d_texture_get_overlay_position(const struct wined3d_texture
*texture
,
3377 unsigned int sub_resource_idx
, LONG
*x
, LONG
*y
)
3379 struct wined3d_overlay_info
*overlay
;
3381 TRACE("texture %p, sub_resource_idx %u, x %p, y %p.\n", texture
, sub_resource_idx
, x
, y
);
3383 if (!(texture
->resource
.usage
& WINED3DUSAGE_OVERLAY
)
3384 || sub_resource_idx
>= texture
->level_count
* texture
->layer_count
)
3386 WARN("Invalid sub-resource specified.\n");
3387 return WINEDDERR_NOTAOVERLAYSURFACE
;
3390 overlay
= &texture
->overlay_info
[sub_resource_idx
];
3391 if (!overlay
->dst_texture
)
3393 TRACE("Overlay not visible.\n");
3396 return WINEDDERR_OVERLAYNOTVISIBLE
;
3399 *x
= overlay
->dst_rect
.left
;
3400 *y
= overlay
->dst_rect
.top
;
3402 TRACE("Returning position %d, %d.\n", *x
, *y
);
3407 HRESULT CDECL
wined3d_texture_set_overlay_position(struct wined3d_texture
*texture
,
3408 unsigned int sub_resource_idx
, LONG x
, LONG y
)
3410 struct wined3d_overlay_info
*overlay
;
3413 TRACE("texture %p, sub_resource_idx %u, x %d, y %d.\n", texture
, sub_resource_idx
, x
, y
);
3415 if (!(texture
->resource
.usage
& WINED3DUSAGE_OVERLAY
)
3416 || sub_resource_idx
>= texture
->level_count
* texture
->layer_count
)
3418 WARN("Invalid sub-resource specified.\n");
3419 return WINEDDERR_NOTAOVERLAYSURFACE
;
3422 overlay
= &texture
->overlay_info
[sub_resource_idx
];
3423 w
= overlay
->dst_rect
.right
- overlay
->dst_rect
.left
;
3424 h
= overlay
->dst_rect
.bottom
- overlay
->dst_rect
.top
;
3425 SetRect(&overlay
->dst_rect
, x
, y
, x
+ w
, y
+ h
);
3430 HRESULT CDECL
wined3d_texture_update_overlay(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
,
3431 const RECT
*src_rect
, struct wined3d_texture
*dst_texture
, unsigned int dst_sub_resource_idx
,
3432 const RECT
*dst_rect
, DWORD flags
)
3434 struct wined3d_overlay_info
*overlay
;
3435 unsigned int level
, dst_level
;
3437 TRACE("texture %p, sub_resource_idx %u, src_rect %s, dst_texture %p, "
3438 "dst_sub_resource_idx %u, dst_rect %s, flags %#x.\n",
3439 texture
, sub_resource_idx
, wine_dbgstr_rect(src_rect
), dst_texture
,
3440 dst_sub_resource_idx
, wine_dbgstr_rect(dst_rect
), flags
);
3442 if (!(texture
->resource
.usage
& WINED3DUSAGE_OVERLAY
) || texture
->resource
.type
!= WINED3D_RTYPE_TEXTURE_2D
3443 || sub_resource_idx
>= texture
->level_count
* texture
->layer_count
)
3445 WARN("Invalid sub-resource specified.\n");
3446 return WINEDDERR_NOTAOVERLAYSURFACE
;
3449 if (!dst_texture
|| dst_texture
->resource
.type
!= WINED3D_RTYPE_TEXTURE_2D
3450 || dst_sub_resource_idx
>= dst_texture
->level_count
* dst_texture
->layer_count
)
3452 WARN("Invalid destination sub-resource specified.\n");
3453 return WINED3DERR_INVALIDCALL
;
3456 overlay
= &texture
->overlay_info
[sub_resource_idx
];
3458 level
= sub_resource_idx
% texture
->level_count
;
3460 overlay
->src_rect
= *src_rect
;
3462 SetRect(&overlay
->src_rect
, 0, 0,
3463 wined3d_texture_get_level_width(texture
, level
),
3464 wined3d_texture_get_level_height(texture
, level
));
3466 dst_level
= dst_sub_resource_idx
% dst_texture
->level_count
;
3468 overlay
->dst_rect
= *dst_rect
;
3470 SetRect(&overlay
->dst_rect
, 0, 0,
3471 wined3d_texture_get_level_width(dst_texture
, dst_level
),
3472 wined3d_texture_get_level_height(dst_texture
, dst_level
));
3474 if (overlay
->dst_texture
&& (overlay
->dst_texture
!= dst_texture
3475 || overlay
->dst_sub_resource_idx
!= dst_sub_resource_idx
|| flags
& WINEDDOVER_HIDE
))
3477 overlay
->dst_texture
= NULL
;
3478 list_remove(&overlay
->entry
);
3481 if (flags
& WINEDDOVER_SHOW
)
3483 if (overlay
->dst_texture
!= dst_texture
|| overlay
->dst_sub_resource_idx
!= dst_sub_resource_idx
)
3485 overlay
->dst_texture
= dst_texture
;
3486 overlay
->dst_sub_resource_idx
= dst_sub_resource_idx
;
3487 list_add_tail(&texture
->overlay_info
[dst_sub_resource_idx
].overlays
, &overlay
->entry
);
3490 else if (flags
& WINEDDOVER_HIDE
)
3492 /* Tests show that the rectangles are erased on hide. */
3493 SetRectEmpty(&overlay
->src_rect
);
3494 SetRectEmpty(&overlay
->dst_rect
);
3495 overlay
->dst_texture
= NULL
;
3501 void * CDECL
wined3d_texture_get_sub_resource_parent(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
)
3503 unsigned int sub_count
= texture
->level_count
* texture
->layer_count
;
3505 TRACE("texture %p, sub_resource_idx %u.\n", texture
, sub_resource_idx
);
3507 if (sub_resource_idx
>= sub_count
)
3509 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx
, sub_count
);
3513 return texture
->sub_resources
[sub_resource_idx
].parent
;
3516 void CDECL
wined3d_texture_set_sub_resource_parent(struct wined3d_texture
*texture
,
3517 unsigned int sub_resource_idx
, void *parent
)
3519 unsigned int sub_count
= texture
->level_count
* texture
->layer_count
;
3521 TRACE("texture %p, sub_resource_idx %u, parent %p.\n", texture
, sub_resource_idx
, parent
);
3523 if (sub_resource_idx
>= sub_count
)
3525 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx
, sub_count
);
3529 texture
->sub_resources
[sub_resource_idx
].parent
= parent
;
3532 HRESULT CDECL
wined3d_texture_get_sub_resource_desc(const struct wined3d_texture
*texture
,
3533 unsigned int sub_resource_idx
, struct wined3d_sub_resource_desc
*desc
)
3535 unsigned int sub_count
= texture
->level_count
* texture
->layer_count
;
3536 const struct wined3d_resource
*resource
;
3537 unsigned int level_idx
;
3539 TRACE("texture %p, sub_resource_idx %u, desc %p.\n", texture
, sub_resource_idx
, desc
);
3541 if (sub_resource_idx
>= sub_count
)
3543 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx
, sub_count
);
3544 return WINED3DERR_INVALIDCALL
;
3547 resource
= &texture
->resource
;
3548 desc
->format
= resource
->format
->id
;
3549 desc
->multisample_type
= resource
->multisample_type
;
3550 desc
->multisample_quality
= resource
->multisample_quality
;
3551 desc
->usage
= resource
->usage
;
3552 desc
->access
= resource
->access
;
3554 level_idx
= sub_resource_idx
% texture
->level_count
;
3555 desc
->width
= wined3d_texture_get_level_width(texture
, level_idx
);
3556 desc
->height
= wined3d_texture_get_level_height(texture
, level_idx
);
3557 desc
->depth
= wined3d_texture_get_level_depth(texture
, level_idx
);
3558 desc
->size
= texture
->sub_resources
[sub_resource_idx
].size
;
3563 HRESULT CDECL
wined3d_texture_create(struct wined3d_device
*device
, const struct wined3d_resource_desc
*desc
,
3564 UINT layer_count
, UINT level_count
, DWORD flags
, const struct wined3d_sub_resource_data
*data
,
3565 void *parent
, const struct wined3d_parent_ops
*parent_ops
, struct wined3d_texture
**texture
)
3567 const struct wined3d_texture_ops
*texture_ops
;
3568 struct wined3d_texture
*object
;
3571 TRACE("device %p, desc %p, layer_count %u, level_count %u, flags %#x, data %p, "
3572 "parent %p, parent_ops %p, texture %p.\n",
3573 device
, desc
, layer_count
, level_count
, flags
, data
, parent
, parent_ops
, texture
);
3577 WARN("Invalid layer count.\n");
3578 return E_INVALIDARG
;
3580 if ((desc
->usage
& WINED3DUSAGE_LEGACY_CUBEMAP
) && layer_count
!= 6)
3582 ERR("Invalid layer count %u for legacy cubemap.\n", layer_count
);
3588 WARN("Invalid level count.\n");
3589 return WINED3DERR_INVALIDCALL
;
3592 if (desc
->multisample_type
!= WINED3D_MULTISAMPLE_NONE
)
3594 const struct wined3d_format
*format
= wined3d_get_format(device
->adapter
, desc
->format
, desc
->usage
);
3596 if (desc
->multisample_type
== WINED3D_MULTISAMPLE_NON_MASKABLE
3597 && desc
->multisample_quality
>= wined3d_popcount(format
->multisample_types
))
3599 WARN("Unsupported quality level %u requested for WINED3D_MULTISAMPLE_NON_MASKABLE.\n",
3600 desc
->multisample_quality
);
3601 return WINED3DERR_NOTAVAILABLE
;
3603 if (desc
->multisample_type
!= WINED3D_MULTISAMPLE_NON_MASKABLE
3604 && (!(format
->multisample_types
& 1u << (desc
->multisample_type
- 1))
3605 || desc
->multisample_quality
))
3607 WARN("Unsupported multisample type %u quality %u requested.\n", desc
->multisample_type
,
3608 desc
->multisample_quality
);
3609 return WINED3DERR_NOTAVAILABLE
;
3613 switch (desc
->resource_type
)
3615 case WINED3D_RTYPE_TEXTURE_1D
:
3616 texture_ops
= &texture1d_ops
;
3618 case WINED3D_RTYPE_TEXTURE_2D
:
3619 texture_ops
= &texture2d_ops
;
3621 case WINED3D_RTYPE_TEXTURE_3D
:
3622 texture_ops
= &texture3d_ops
;
3625 ERR("Invalid resource type %s.\n", debug_d3dresourcetype(desc
->resource_type
));
3626 return WINED3DERR_INVALIDCALL
;
3629 if (!(object
= heap_alloc_zero(FIELD_OFFSET(struct wined3d_texture
,
3630 sub_resources
[level_count
* layer_count
]))))
3631 return E_OUTOFMEMORY
;
3633 if (FAILED(hr
= wined3d_texture_init(object
, desc
, layer_count
,
3634 level_count
, flags
, device
, parent
, parent_ops
, texture_ops
)))
3636 WARN("Failed to initialize texture, returning %#x.\n", hr
);
3641 /* FIXME: We'd like to avoid ever allocating system memory for the texture
3645 unsigned int sub_count
= level_count
* layer_count
;
3648 for (i
= 0; i
< sub_count
; ++i
)
3652 WARN("Invalid sub-resource data specified for sub-resource %u.\n", i
);
3653 wined3d_texture_cleanup_sync(object
);
3655 return E_INVALIDARG
;
3659 for (i
= 0; i
< sub_count
; ++i
)
3661 wined3d_device_update_sub_resource(device
, &object
->resource
,
3662 i
, NULL
, data
[i
].data
, data
[i
].row_pitch
, data
[i
].slice_pitch
, 0);
3666 TRACE("Created texture %p.\n", object
);
3672 HRESULT CDECL
wined3d_texture_get_dc(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
, HDC
*dc
)
3674 struct wined3d_device
*device
= texture
->resource
.device
;
3675 struct wined3d_texture_sub_resource
*sub_resource
;
3676 struct wined3d_dc_info
*dc_info
;
3678 TRACE("texture %p, sub_resource_idx %u, dc %p.\n", texture
, sub_resource_idx
, dc
);
3680 if (!(texture
->flags
& WINED3D_TEXTURE_GET_DC
))
3682 WARN("Texture does not support GetDC\n");
3683 /* Don't touch the DC */
3684 return WINED3DERR_INVALIDCALL
;
3687 if (!(sub_resource
= wined3d_texture_get_sub_resource(texture
, sub_resource_idx
)))
3688 return WINED3DERR_INVALIDCALL
;
3690 if (texture
->resource
.type
!= WINED3D_RTYPE_TEXTURE_2D
)
3692 WARN("Not supported on %s resources.\n", debug_d3dresourcetype(texture
->resource
.type
));
3693 return WINED3DERR_INVALIDCALL
;
3696 if (texture
->resource
.map_count
&& !(texture
->flags
& WINED3D_TEXTURE_GET_DC_LENIENT
))
3697 return WINED3DERR_INVALIDCALL
;
3699 if (!(dc_info
= texture
->dc_info
) || !dc_info
[sub_resource_idx
].dc
)
3701 struct wined3d_texture_idx texture_idx
= {texture
, sub_resource_idx
};
3703 wined3d_cs_init_object(device
->cs
, wined3d_texture_create_dc
, &texture_idx
);
3704 device
->cs
->ops
->finish(device
->cs
, WINED3D_CS_QUEUE_DEFAULT
);
3705 if (!(dc_info
= texture
->dc_info
) || !dc_info
[sub_resource_idx
].dc
)
3706 return WINED3DERR_INVALIDCALL
;
3709 if (!(texture
->flags
& WINED3D_TEXTURE_GET_DC_LENIENT
))
3710 texture
->flags
|= WINED3D_TEXTURE_DC_IN_USE
;
3711 ++texture
->resource
.map_count
;
3712 ++sub_resource
->map_count
;
3714 *dc
= dc_info
[sub_resource_idx
].dc
;
3715 TRACE("Returning dc %p.\n", *dc
);
3720 HRESULT CDECL
wined3d_texture_release_dc(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
, HDC dc
)
3722 struct wined3d_device
*device
= texture
->resource
.device
;
3723 struct wined3d_texture_sub_resource
*sub_resource
;
3724 struct wined3d_dc_info
*dc_info
;
3726 TRACE("texture %p, sub_resource_idx %u, dc %p.\n", texture
, sub_resource_idx
, dc
);
3728 if (!(sub_resource
= wined3d_texture_get_sub_resource(texture
, sub_resource_idx
)))
3729 return WINED3DERR_INVALIDCALL
;
3731 if (texture
->resource
.type
!= WINED3D_RTYPE_TEXTURE_2D
)
3733 WARN("Not supported on %s resources.\n", debug_d3dresourcetype(texture
->resource
.type
));
3734 return WINED3DERR_INVALIDCALL
;
3737 if (!(texture
->flags
& (WINED3D_TEXTURE_GET_DC_LENIENT
| WINED3D_TEXTURE_DC_IN_USE
)))
3738 return WINED3DERR_INVALIDCALL
;
3740 if (!(dc_info
= texture
->dc_info
) || dc_info
[sub_resource_idx
].dc
!= dc
)
3742 WARN("Application tries to release invalid DC %p, sub-resource DC is %p.\n",
3743 dc
, dc_info
? dc_info
[sub_resource_idx
].dc
: NULL
);
3744 return WINED3DERR_INVALIDCALL
;
3747 if (!(texture
->resource
.usage
& WINED3DUSAGE_OWNDC
))
3749 struct wined3d_texture_idx texture_idx
= {texture
, sub_resource_idx
};
3751 wined3d_cs_destroy_object(device
->cs
, wined3d_texture_destroy_dc
, &texture_idx
);
3752 device
->cs
->ops
->finish(device
->cs
, WINED3D_CS_QUEUE_DEFAULT
);
3755 --sub_resource
->map_count
;
3756 if (!--texture
->resource
.map_count
&& texture
->update_map_binding
)
3757 wined3d_texture_update_map_binding(texture
);
3758 if (!(texture
->flags
& WINED3D_TEXTURE_GET_DC_LENIENT
))
3759 texture
->flags
&= ~WINED3D_TEXTURE_DC_IN_USE
;
3764 void wined3d_texture_upload_from_texture(struct wined3d_texture
*dst_texture
, unsigned int dst_sub_resource_idx
,
3765 unsigned int dst_x
, unsigned int dst_y
, unsigned int dst_z
, struct wined3d_texture
*src_texture
,
3766 unsigned int src_sub_resource_idx
, const struct wined3d_box
*src_box
)
3768 unsigned int src_row_pitch
, src_slice_pitch
;
3769 unsigned int update_w
, update_h
, update_d
;
3770 unsigned int src_level
, dst_level
;
3771 struct wined3d_context
*context
;
3772 struct wined3d_bo_address data
;
3774 TRACE("dst_texture %p, dst_sub_resource_idx %u, dst_x %u, dst_y %u, dst_z %u, "
3775 "src_texture %p, src_sub_resource_idx %u, src_box %s.\n",
3776 dst_texture
, dst_sub_resource_idx
, dst_x
, dst_y
, dst_z
,
3777 src_texture
, src_sub_resource_idx
, debug_box(src_box
));
3779 context
= context_acquire(dst_texture
->resource
.device
, NULL
, 0);
3781 /* Only load the sub-resource for partial updates. For newly allocated
3782 * textures the texture wouldn't be the current location, and we'd upload
3783 * zeroes just to overwrite them again. */
3784 update_w
= src_box
->right
- src_box
->left
;
3785 update_h
= src_box
->bottom
- src_box
->top
;
3786 update_d
= src_box
->back
- src_box
->front
;
3787 dst_level
= dst_sub_resource_idx
% dst_texture
->level_count
;
3788 if (update_w
== wined3d_texture_get_level_width(dst_texture
, dst_level
)
3789 && update_h
== wined3d_texture_get_level_height(dst_texture
, dst_level
)
3790 && update_d
== wined3d_texture_get_level_depth(dst_texture
, dst_level
))
3791 wined3d_texture_prepare_texture(dst_texture
, context
, FALSE
);
3793 wined3d_texture_load_location(dst_texture
, dst_sub_resource_idx
, context
, WINED3D_LOCATION_TEXTURE_RGB
);
3794 wined3d_texture_bind_and_dirtify(dst_texture
, context
, FALSE
);
3796 src_level
= src_sub_resource_idx
% src_texture
->level_count
;
3797 wined3d_texture_get_memory(src_texture
, src_sub_resource_idx
, &data
,
3798 src_texture
->sub_resources
[src_sub_resource_idx
].locations
);
3799 wined3d_texture_get_pitch(src_texture
, src_level
, &src_row_pitch
, &src_slice_pitch
);
3801 wined3d_texture_upload_data(dst_texture
, dst_sub_resource_idx
, context
, src_texture
->resource
.format
,
3802 src_box
, wined3d_const_bo_address(&data
), src_row_pitch
, src_slice_pitch
, dst_x
, dst_y
, dst_z
, FALSE
);
3804 context_release(context
);
3806 wined3d_texture_validate_location(dst_texture
, dst_sub_resource_idx
, WINED3D_LOCATION_TEXTURE_RGB
);
3807 wined3d_texture_invalidate_location(dst_texture
, dst_sub_resource_idx
, ~WINED3D_LOCATION_TEXTURE_RGB
);
3810 /* Partial downloads are not supported. */
3811 void wined3d_texture_download_from_texture(struct wined3d_texture
*dst_texture
, unsigned int dst_sub_resource_idx
,
3812 struct wined3d_texture
*src_texture
, unsigned int src_sub_resource_idx
)
3814 struct wined3d_context
*context
;
3815 struct wined3d_bo_address data
;
3816 DWORD dst_location
= dst_texture
->resource
.map_binding
;
3818 context
= context_acquire(src_texture
->resource
.device
, NULL
, 0);
3820 wined3d_texture_prepare_location(dst_texture
, dst_sub_resource_idx
, context
, dst_location
);
3821 wined3d_texture_get_memory(dst_texture
, dst_sub_resource_idx
, &data
, dst_location
);
3822 wined3d_texture_bind_and_dirtify(src_texture
, context
,
3823 !(src_texture
->sub_resources
[src_sub_resource_idx
].locations
& WINED3D_LOCATION_TEXTURE_RGB
));
3824 wined3d_texture_download_data(src_texture
, src_sub_resource_idx
, context
, &data
);
3826 context_release(context
);
3828 wined3d_texture_validate_location(dst_texture
, dst_sub_resource_idx
, dst_location
);
3829 wined3d_texture_invalidate_location(dst_texture
, dst_sub_resource_idx
, ~dst_location
);