1c2c66affSColin Finck /*
2c2c66affSColin Finck * Copyright 2002-2005 Jason Edmeades
3c2c66affSColin Finck * Copyright 2002-2005 Raphael Junqueira
4c2c66affSColin Finck * Copyright 2005 Oliver Stieber
5c2c66affSColin Finck * Copyright 2007-2009, 2013 Stefan Dösinger for CodeWeavers
6c2c66affSColin Finck * Copyright 2009-2011 Henri Verbeet for CodeWeavers
7c2c66affSColin Finck *
8c2c66affSColin Finck * This library is free software; you can redistribute it and/or
9c2c66affSColin Finck * modify it under the terms of the GNU Lesser General Public
10c2c66affSColin Finck * License as published by the Free Software Foundation; either
11c2c66affSColin Finck * version 2.1 of the License, or (at your option) any later version.
12c2c66affSColin Finck *
13c2c66affSColin Finck * This library is distributed in the hope that it will be useful,
14c2c66affSColin Finck * but WITHOUT ANY WARRANTY; without even the implied warranty of
15c2c66affSColin Finck * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16c2c66affSColin Finck * Lesser General Public License for more details.
17c2c66affSColin Finck *
18c2c66affSColin Finck * You should have received a copy of the GNU Lesser General Public
19c2c66affSColin Finck * License along with this library; if not, write to the Free Software
20c2c66affSColin Finck * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21c2c66affSColin Finck */
22c2c66affSColin Finck
239987f029SAmine Khaldi #include "config.h"
249987f029SAmine Khaldi #include "wine/port.h"
25c2c66affSColin Finck #include "wined3d_private.h"
26c2c66affSColin Finck
27c2c66affSColin Finck WINE_DEFAULT_DEBUG_CHANNEL(d3d);
28c2c66affSColin Finck WINE_DECLARE_DEBUG_CHANNEL(d3d_perf);
29c2c66affSColin Finck WINE_DECLARE_DEBUG_CHANNEL(winediag);
30c2c66affSColin Finck
31c2c66affSColin Finck #define WINED3D_TEXTURE_DYNAMIC_MAP_THRESHOLD 50
32c2c66affSColin Finck
wined3d_texture_use_pbo(const struct wined3d_texture * texture,const struct wined3d_gl_info * gl_info)33c2c66affSColin Finck static BOOL wined3d_texture_use_pbo(const struct wined3d_texture *texture, const struct wined3d_gl_info *gl_info)
34c2c66affSColin Finck {
359987f029SAmine Khaldi return !(texture->resource.access & WINED3D_RESOURCE_ACCESS_CPU)
369987f029SAmine Khaldi && texture->resource.usage & WINED3DUSAGE_DYNAMIC
37c2c66affSColin Finck && gl_info->supported[ARB_PIXEL_BUFFER_OBJECT]
38d6ac0a71SAmine Khaldi && !texture->resource.format->conv_byte_count
39c2c66affSColin Finck && !(texture->flags & (WINED3D_TEXTURE_PIN_SYSMEM | WINED3D_TEXTURE_COND_NP2_EMULATED));
40c2c66affSColin Finck }
41c2c66affSColin Finck
wined3d_texture_use_immutable_storage(const struct wined3d_texture * texture,const struct wined3d_gl_info * gl_info)42c2c66affSColin Finck static BOOL wined3d_texture_use_immutable_storage(const struct wined3d_texture *texture,
43c2c66affSColin Finck const struct wined3d_gl_info *gl_info)
44c2c66affSColin Finck {
45c2c66affSColin Finck /* We don't expect to create texture views for textures with height-scaled formats.
46c2c66affSColin Finck * Besides, ARB_texture_storage doesn't allow specifying exact sizes for all levels. */
47c2c66affSColin Finck return gl_info->supported[ARB_TEXTURE_STORAGE]
48c2c66affSColin Finck && !(texture->resource.format_flags & WINED3DFMT_FLAG_HEIGHT_SCALE);
49c2c66affSColin Finck }
50c2c66affSColin Finck
wined3d_texture_get_gl_buffer(const struct wined3d_texture * texture)51c2c66affSColin Finck GLenum wined3d_texture_get_gl_buffer(const struct wined3d_texture *texture)
52c2c66affSColin Finck {
53c2c66affSColin Finck const struct wined3d_swapchain *swapchain = texture->swapchain;
54c2c66affSColin Finck
55c2c66affSColin Finck TRACE("texture %p.\n", texture);
56c2c66affSColin Finck
57c2c66affSColin Finck if (!swapchain)
58c2c66affSColin Finck {
59c2c66affSColin Finck ERR("Texture %p is not part of a swapchain.\n", texture);
60c2c66affSColin Finck return GL_NONE;
61c2c66affSColin Finck }
62c2c66affSColin Finck
63c2c66affSColin Finck if (texture == swapchain->front_buffer)
64c2c66affSColin Finck {
65c2c66affSColin Finck TRACE("Returning GL_FRONT.\n");
66c2c66affSColin Finck return GL_FRONT;
67c2c66affSColin Finck }
68c2c66affSColin Finck
69c2c66affSColin Finck if (texture == swapchain->back_buffers[0])
70c2c66affSColin Finck {
71c2c66affSColin Finck TRACE("Returning GL_BACK.\n");
72c2c66affSColin Finck return GL_BACK;
73c2c66affSColin Finck }
74c2c66affSColin Finck
75c2c66affSColin Finck FIXME("Higher back buffer, returning GL_BACK.\n");
76c2c66affSColin Finck return GL_BACK;
77c2c66affSColin Finck }
78c2c66affSColin Finck
wined3d_resource_access_from_location(DWORD location)79c2c66affSColin Finck static DWORD wined3d_resource_access_from_location(DWORD location)
80c2c66affSColin Finck {
81c2c66affSColin Finck switch (location)
82c2c66affSColin Finck {
83c2c66affSColin Finck case WINED3D_LOCATION_DISCARDED:
84c2c66affSColin Finck return 0;
85c2c66affSColin Finck
86c2c66affSColin Finck case WINED3D_LOCATION_SYSMEM:
87c2c66affSColin Finck case WINED3D_LOCATION_USER_MEMORY:
88c2c66affSColin Finck return WINED3D_RESOURCE_ACCESS_CPU;
89c2c66affSColin Finck
90c2c66affSColin Finck case WINED3D_LOCATION_BUFFER:
91c2c66affSColin Finck case WINED3D_LOCATION_DRAWABLE:
92c2c66affSColin Finck case WINED3D_LOCATION_TEXTURE_RGB:
93c2c66affSColin Finck case WINED3D_LOCATION_TEXTURE_SRGB:
94c2c66affSColin Finck case WINED3D_LOCATION_RB_MULTISAMPLE:
95c2c66affSColin Finck case WINED3D_LOCATION_RB_RESOLVED:
96c2c66affSColin Finck return WINED3D_RESOURCE_ACCESS_GPU;
97c2c66affSColin Finck
98c2c66affSColin Finck default:
99c2c66affSColin Finck FIXME("Unhandled location %#x.\n", location);
100c2c66affSColin Finck return 0;
101c2c66affSColin Finck }
102c2c66affSColin Finck }
103c2c66affSColin Finck
is_power_of_two(UINT x)104*81cffd76SJoachim Henze static BOOL is_power_of_two(UINT x)
1059987f029SAmine Khaldi {
106*81cffd76SJoachim Henze return (x != 0) && !(x & (x - 1));
1079987f029SAmine Khaldi }
1089987f029SAmine Khaldi
wined3d_texture_evict_sysmem(struct wined3d_texture * texture)109c2c66affSColin Finck static void wined3d_texture_evict_sysmem(struct wined3d_texture *texture)
110c2c66affSColin Finck {
111c2c66affSColin Finck struct wined3d_texture_sub_resource *sub_resource;
112c2c66affSColin Finck unsigned int i, sub_count;
113c2c66affSColin Finck
114c2c66affSColin Finck if (texture->flags & (WINED3D_TEXTURE_CONVERTED | WINED3D_TEXTURE_PIN_SYSMEM)
115c2c66affSColin Finck || texture->download_count > WINED3D_TEXTURE_DYNAMIC_MAP_THRESHOLD)
116c2c66affSColin Finck {
117c2c66affSColin Finck TRACE("Not evicting system memory for texture %p.\n", texture);
118c2c66affSColin Finck return;
119c2c66affSColin Finck }
120c2c66affSColin Finck
121c2c66affSColin Finck TRACE("Evicting system memory for texture %p.\n", texture);
122c2c66affSColin Finck
123c2c66affSColin Finck sub_count = texture->level_count * texture->layer_count;
124c2c66affSColin Finck for (i = 0; i < sub_count; ++i)
125c2c66affSColin Finck {
126c2c66affSColin Finck sub_resource = &texture->sub_resources[i];
127c2c66affSColin Finck if (sub_resource->locations == WINED3D_LOCATION_SYSMEM)
128c2c66affSColin Finck ERR("WINED3D_LOCATION_SYSMEM is the only location for sub-resource %u of texture %p.\n",
129c2c66affSColin Finck i, texture);
130c2c66affSColin Finck sub_resource->locations &= ~WINED3D_LOCATION_SYSMEM;
131c2c66affSColin Finck }
132c2c66affSColin Finck wined3d_resource_free_sysmem(&texture->resource);
133c2c66affSColin Finck }
134c2c66affSColin Finck
wined3d_texture_validate_location(struct wined3d_texture * texture,unsigned int sub_resource_idx,DWORD location)135c2c66affSColin Finck void wined3d_texture_validate_location(struct wined3d_texture *texture,
136c2c66affSColin Finck unsigned int sub_resource_idx, DWORD location)
137c2c66affSColin Finck {
138c2c66affSColin Finck struct wined3d_texture_sub_resource *sub_resource;
139c2c66affSColin Finck DWORD previous_locations;
140c2c66affSColin Finck
141c2c66affSColin Finck TRACE("texture %p, sub_resource_idx %u, location %s.\n",
142c2c66affSColin Finck texture, sub_resource_idx, wined3d_debug_location(location));
143c2c66affSColin Finck
144c2c66affSColin Finck sub_resource = &texture->sub_resources[sub_resource_idx];
145c2c66affSColin Finck previous_locations = sub_resource->locations;
146c2c66affSColin Finck sub_resource->locations |= location;
147c2c66affSColin Finck if (previous_locations == WINED3D_LOCATION_SYSMEM && location != WINED3D_LOCATION_SYSMEM
148c2c66affSColin Finck && !--texture->sysmem_count)
149c2c66affSColin Finck wined3d_texture_evict_sysmem(texture);
150c2c66affSColin Finck
151c2c66affSColin Finck TRACE("New locations flags are %s.\n", wined3d_debug_location(sub_resource->locations));
152c2c66affSColin Finck }
153c2c66affSColin Finck
wined3d_texture_set_dirty(struct wined3d_texture * texture)154c2c66affSColin Finck static void wined3d_texture_set_dirty(struct wined3d_texture *texture)
155c2c66affSColin Finck {
156c2c66affSColin Finck texture->flags &= ~(WINED3D_TEXTURE_RGB_VALID | WINED3D_TEXTURE_SRGB_VALID);
157c2c66affSColin Finck }
158c2c66affSColin Finck
wined3d_texture_invalidate_location(struct wined3d_texture * texture,unsigned int sub_resource_idx,DWORD location)159c2c66affSColin Finck void wined3d_texture_invalidate_location(struct wined3d_texture *texture,
160c2c66affSColin Finck unsigned int sub_resource_idx, DWORD location)
161c2c66affSColin Finck {
162c2c66affSColin Finck struct wined3d_texture_sub_resource *sub_resource;
163c2c66affSColin Finck DWORD previous_locations;
164c2c66affSColin Finck
165c2c66affSColin Finck TRACE("texture %p, sub_resource_idx %u, location %s.\n",
166c2c66affSColin Finck texture, sub_resource_idx, wined3d_debug_location(location));
167c2c66affSColin Finck
168c2c66affSColin Finck if (location & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB))
169c2c66affSColin Finck wined3d_texture_set_dirty(texture);
170c2c66affSColin Finck
171c2c66affSColin Finck sub_resource = &texture->sub_resources[sub_resource_idx];
172c2c66affSColin Finck previous_locations = sub_resource->locations;
173c2c66affSColin Finck sub_resource->locations &= ~location;
174c2c66affSColin Finck if (previous_locations != WINED3D_LOCATION_SYSMEM && sub_resource->locations == WINED3D_LOCATION_SYSMEM)
175c2c66affSColin Finck ++texture->sysmem_count;
176c2c66affSColin Finck
177c2c66affSColin Finck TRACE("New locations flags are %s.\n", wined3d_debug_location(sub_resource->locations));
178c2c66affSColin Finck
179c2c66affSColin Finck if (!sub_resource->locations)
180c2c66affSColin Finck ERR("Sub-resource %u of texture %p does not have any up to date location.\n",
181c2c66affSColin Finck sub_resource_idx, texture);
182c2c66affSColin Finck }
183c2c66affSColin Finck
wined3d_texture_copy_sysmem_location(struct wined3d_texture * texture,unsigned int sub_resource_idx,struct wined3d_context * context,DWORD location)184c2c66affSColin Finck static BOOL wined3d_texture_copy_sysmem_location(struct wined3d_texture *texture,
185c2c66affSColin Finck unsigned int sub_resource_idx, struct wined3d_context *context, DWORD location)
186c2c66affSColin Finck {
187c2c66affSColin Finck unsigned int size = texture->sub_resources[sub_resource_idx].size;
188c2c66affSColin Finck struct wined3d_device *device = texture->resource.device;
189c2c66affSColin Finck const struct wined3d_gl_info *gl_info;
190c2c66affSColin Finck struct wined3d_bo_address dst, src;
191c2c66affSColin Finck
192c2c66affSColin Finck if (!wined3d_texture_prepare_location(texture, sub_resource_idx, context, location))
193c2c66affSColin Finck return FALSE;
194c2c66affSColin Finck
195c2c66affSColin Finck wined3d_texture_get_memory(texture, sub_resource_idx, &dst, location);
196c2c66affSColin Finck wined3d_texture_get_memory(texture, sub_resource_idx, &src,
197c2c66affSColin Finck texture->sub_resources[sub_resource_idx].locations);
198c2c66affSColin Finck
199c2c66affSColin Finck if (dst.buffer_object)
200c2c66affSColin Finck {
201c2c66affSColin Finck context = context_acquire(device, NULL, 0);
202c2c66affSColin Finck gl_info = context->gl_info;
203c2c66affSColin Finck GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, dst.buffer_object));
204c2c66affSColin Finck GL_EXTCALL(glBufferSubData(GL_PIXEL_UNPACK_BUFFER, 0, size, src.addr));
205c2c66affSColin Finck GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0));
206c2c66affSColin Finck checkGLcall("PBO upload");
207c2c66affSColin Finck context_release(context);
208c2c66affSColin Finck return TRUE;
209c2c66affSColin Finck }
210c2c66affSColin Finck
211c2c66affSColin Finck if (src.buffer_object)
212c2c66affSColin Finck {
213c2c66affSColin Finck context = context_acquire(device, NULL, 0);
214c2c66affSColin Finck gl_info = context->gl_info;
215c2c66affSColin Finck GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, src.buffer_object));
216c2c66affSColin Finck GL_EXTCALL(glGetBufferSubData(GL_PIXEL_PACK_BUFFER, 0, size, dst.addr));
217c2c66affSColin Finck GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, 0));
218c2c66affSColin Finck checkGLcall("PBO download");
219c2c66affSColin Finck context_release(context);
220c2c66affSColin Finck return TRUE;
221c2c66affSColin Finck }
222c2c66affSColin Finck
223c2c66affSColin Finck memcpy(dst.addr, src.addr, size);
224c2c66affSColin Finck return TRUE;
225c2c66affSColin Finck }
226c2c66affSColin Finck
227c2c66affSColin Finck /* Context activation is done by the caller. Context may be NULL in
228c2c66affSColin Finck * WINED3D_NO3D mode. */
wined3d_texture_load_location(struct wined3d_texture * texture,unsigned int sub_resource_idx,struct wined3d_context * context,DWORD location)229c2c66affSColin Finck BOOL wined3d_texture_load_location(struct wined3d_texture *texture,
230c2c66affSColin Finck unsigned int sub_resource_idx, struct wined3d_context *context, DWORD location)
231c2c66affSColin Finck {
232c2c66affSColin Finck static const DWORD sysmem_locations = WINED3D_LOCATION_SYSMEM | WINED3D_LOCATION_USER_MEMORY
233c2c66affSColin Finck | WINED3D_LOCATION_BUFFER;
234c2c66affSColin Finck DWORD current = texture->sub_resources[sub_resource_idx].locations;
235c2c66affSColin Finck BOOL ret;
236c2c66affSColin Finck
237c2c66affSColin Finck TRACE("texture %p, sub_resource_idx %u, context %p, location %s.\n",
238c2c66affSColin Finck texture, sub_resource_idx, context, wined3d_debug_location(location));
239c2c66affSColin Finck
240c2c66affSColin Finck TRACE("Current resource location %s.\n", wined3d_debug_location(current));
241c2c66affSColin Finck
242c2c66affSColin Finck if (current & location)
243c2c66affSColin Finck {
244c2c66affSColin Finck TRACE("Location %s is already up to date.\n", wined3d_debug_location(location));
245c2c66affSColin Finck return TRUE;
246c2c66affSColin Finck }
247c2c66affSColin Finck
248c2c66affSColin Finck if (WARN_ON(d3d))
249c2c66affSColin Finck {
250c2c66affSColin Finck DWORD required_access = wined3d_resource_access_from_location(location);
2519987f029SAmine Khaldi if ((texture->resource.access & required_access) != required_access)
252c2c66affSColin Finck WARN("Operation requires %#x access, but texture only has %#x.\n",
2539987f029SAmine Khaldi required_access, texture->resource.access);
254c2c66affSColin Finck }
255c2c66affSColin Finck
256c2c66affSColin Finck if (current & WINED3D_LOCATION_DISCARDED)
257c2c66affSColin Finck {
258c2c66affSColin Finck TRACE("Sub-resource previously discarded, nothing to do.\n");
259c2c66affSColin Finck if (!wined3d_texture_prepare_location(texture, sub_resource_idx, context, location))
260c2c66affSColin Finck return FALSE;
261c2c66affSColin Finck wined3d_texture_validate_location(texture, sub_resource_idx, location);
262c2c66affSColin Finck wined3d_texture_invalidate_location(texture, sub_resource_idx, WINED3D_LOCATION_DISCARDED);
263c2c66affSColin Finck return TRUE;
264c2c66affSColin Finck }
265c2c66affSColin Finck
266c2c66affSColin Finck if (!current)
267c2c66affSColin Finck {
268c2c66affSColin Finck ERR("Sub-resource %u of texture %p does not have any up to date location.\n",
269c2c66affSColin Finck sub_resource_idx, texture);
270c2c66affSColin Finck wined3d_texture_validate_location(texture, sub_resource_idx, WINED3D_LOCATION_DISCARDED);
271c2c66affSColin Finck return wined3d_texture_load_location(texture, sub_resource_idx, context, location);
272c2c66affSColin Finck }
273c2c66affSColin Finck
274c2c66affSColin Finck if ((location & sysmem_locations) && (current & sysmem_locations))
275c2c66affSColin Finck ret = wined3d_texture_copy_sysmem_location(texture, sub_resource_idx, context, location);
276c2c66affSColin Finck else
277c2c66affSColin Finck ret = texture->texture_ops->texture_load_location(texture, sub_resource_idx, context, location);
278c2c66affSColin Finck
279c2c66affSColin Finck if (ret)
280c2c66affSColin Finck wined3d_texture_validate_location(texture, sub_resource_idx, location);
281c2c66affSColin Finck
282c2c66affSColin Finck return ret;
283c2c66affSColin Finck }
284c2c66affSColin Finck
wined3d_texture_get_memory(struct wined3d_texture * texture,unsigned int sub_resource_idx,struct wined3d_bo_address * data,DWORD locations)285c2c66affSColin Finck void wined3d_texture_get_memory(struct wined3d_texture *texture, unsigned int sub_resource_idx,
286c2c66affSColin Finck struct wined3d_bo_address *data, DWORD locations)
287c2c66affSColin Finck {
288c2c66affSColin Finck struct wined3d_texture_sub_resource *sub_resource;
289c2c66affSColin Finck
290c2c66affSColin Finck TRACE("texture %p, sub_resource_idx %u, data %p, locations %s.\n",
291c2c66affSColin Finck texture, sub_resource_idx, data, wined3d_debug_location(locations));
292c2c66affSColin Finck
293c2c66affSColin Finck sub_resource = &texture->sub_resources[sub_resource_idx];
294c2c66affSColin Finck if (locations & WINED3D_LOCATION_BUFFER)
295c2c66affSColin Finck {
296c2c66affSColin Finck data->addr = NULL;
297*81cffd76SJoachim Henze #if !defined(STAGING_CSMT)
298c2c66affSColin Finck data->buffer_object = sub_resource->buffer_object;
299*81cffd76SJoachim Henze #else /* STAGING_CSMT */
300*81cffd76SJoachim Henze data->buffer_object = sub_resource->buffer->name;
301*81cffd76SJoachim Henze #endif /* STAGING_CSMT */
302c2c66affSColin Finck return;
303c2c66affSColin Finck }
304c2c66affSColin Finck if (locations & WINED3D_LOCATION_USER_MEMORY)
305c2c66affSColin Finck {
306c2c66affSColin Finck data->addr = texture->user_memory;
307c2c66affSColin Finck data->buffer_object = 0;
308c2c66affSColin Finck return;
309c2c66affSColin Finck }
310c2c66affSColin Finck if (locations & WINED3D_LOCATION_SYSMEM)
311c2c66affSColin Finck {
312c2c66affSColin Finck data->addr = texture->resource.heap_memory;
313c2c66affSColin Finck data->addr += sub_resource->offset;
314c2c66affSColin Finck data->buffer_object = 0;
315c2c66affSColin Finck return;
316c2c66affSColin Finck }
317c2c66affSColin Finck
318c2c66affSColin Finck ERR("Unexpected locations %s.\n", wined3d_debug_location(locations));
319c2c66affSColin Finck data->addr = NULL;
320c2c66affSColin Finck data->buffer_object = 0;
321c2c66affSColin Finck }
322c2c66affSColin Finck
wined3d_texture_init(struct wined3d_texture * texture,const struct wined3d_texture_ops * texture_ops,UINT layer_count,UINT level_count,const struct wined3d_resource_desc * desc,DWORD flags,struct wined3d_device * device,void * parent,const struct wined3d_parent_ops * parent_ops,const struct wined3d_resource_ops * resource_ops)323*81cffd76SJoachim Henze static HRESULT wined3d_texture_init(struct wined3d_texture *texture, const struct wined3d_texture_ops *texture_ops,
324*81cffd76SJoachim Henze UINT layer_count, UINT level_count, const struct wined3d_resource_desc *desc, DWORD flags,
325*81cffd76SJoachim Henze struct wined3d_device *device, void *parent, const struct wined3d_parent_ops *parent_ops,
326*81cffd76SJoachim Henze const struct wined3d_resource_ops *resource_ops)
327*81cffd76SJoachim Henze {
328*81cffd76SJoachim Henze unsigned int i, j, size, offset = 0;
329*81cffd76SJoachim Henze const struct wined3d_format *format;
330*81cffd76SJoachim Henze HRESULT hr;
331*81cffd76SJoachim Henze
332*81cffd76SJoachim Henze TRACE("texture %p, texture_ops %p, layer_count %u, level_count %u, resource_type %s, format %s, "
333*81cffd76SJoachim Henze "multisample_type %#x, multisample_quality %#x, usage %s, access %s, width %u, height %u, depth %u, "
334*81cffd76SJoachim Henze "flags %#x, device %p, parent %p, parent_ops %p, resource_ops %p.\n",
335*81cffd76SJoachim Henze texture, texture_ops, layer_count, level_count, debug_d3dresourcetype(desc->resource_type),
336*81cffd76SJoachim Henze debug_d3dformat(desc->format), desc->multisample_type, desc->multisample_quality,
337*81cffd76SJoachim Henze debug_d3dusage(desc->usage), wined3d_debug_resource_access(desc->access),
338*81cffd76SJoachim Henze desc->width, desc->height, desc->depth, flags, device, parent, parent_ops, resource_ops);
339*81cffd76SJoachim Henze
340*81cffd76SJoachim Henze if (!desc->width || !desc->height || !desc->depth)
341*81cffd76SJoachim Henze return WINED3DERR_INVALIDCALL;
342*81cffd76SJoachim Henze
343*81cffd76SJoachim Henze format = wined3d_get_format(&device->adapter->gl_info, desc->format, desc->usage);
344*81cffd76SJoachim Henze
345*81cffd76SJoachim Henze for (i = 0; i < layer_count; ++i)
346*81cffd76SJoachim Henze {
347*81cffd76SJoachim Henze for (j = 0; j < level_count; ++j)
348*81cffd76SJoachim Henze {
349*81cffd76SJoachim Henze unsigned int idx = i * level_count + j;
350*81cffd76SJoachim Henze
351*81cffd76SJoachim Henze size = wined3d_format_calculate_size(format, device->surface_alignment,
352*81cffd76SJoachim Henze max(1, desc->width >> j), max(1, desc->height >> j), max(1, desc->depth >> j));
353*81cffd76SJoachim Henze texture->sub_resources[idx].offset = offset;
354*81cffd76SJoachim Henze texture->sub_resources[idx].size = size;
355*81cffd76SJoachim Henze offset += size;
356*81cffd76SJoachim Henze }
357*81cffd76SJoachim Henze offset = (offset + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1);
358*81cffd76SJoachim Henze }
359*81cffd76SJoachim Henze
360*81cffd76SJoachim Henze if (!offset)
361*81cffd76SJoachim Henze return WINED3DERR_INVALIDCALL;
362*81cffd76SJoachim Henze
363*81cffd76SJoachim Henze if (FAILED(hr = resource_init(&texture->resource, device, desc->resource_type, format,
364*81cffd76SJoachim Henze desc->multisample_type, desc->multisample_quality, desc->usage, desc->access,
365*81cffd76SJoachim Henze desc->width, desc->height, desc->depth, offset, parent, parent_ops, resource_ops)))
366*81cffd76SJoachim Henze {
367*81cffd76SJoachim Henze static unsigned int once;
368*81cffd76SJoachim Henze
369*81cffd76SJoachim Henze /* DXTn 3D textures are not supported. Do not write the ERR for them. */
370*81cffd76SJoachim Henze if ((desc->format == WINED3DFMT_DXT1 || desc->format == WINED3DFMT_DXT2 || desc->format == WINED3DFMT_DXT3
371*81cffd76SJoachim Henze || desc->format == WINED3DFMT_DXT4 || desc->format == WINED3DFMT_DXT5)
372*81cffd76SJoachim Henze && !(format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_TEXTURE)
373*81cffd76SJoachim Henze && desc->resource_type != WINED3D_RTYPE_TEXTURE_3D && !once++)
374*81cffd76SJoachim Henze ERR_(winediag)("The application tried to create a DXTn texture, but the driver does not support them.\n");
375*81cffd76SJoachim Henze
376*81cffd76SJoachim Henze WARN("Failed to initialize resource, returning %#x\n", hr);
377*81cffd76SJoachim Henze return hr;
378*81cffd76SJoachim Henze }
379*81cffd76SJoachim Henze wined3d_resource_update_draw_binding(&texture->resource);
380*81cffd76SJoachim Henze if ((flags & WINED3D_TEXTURE_CREATE_MAPPABLE) || desc->format == WINED3DFMT_D16_LOCKABLE)
381*81cffd76SJoachim Henze texture->resource.access |= WINED3D_RESOURCE_ACCESS_MAP_R | WINED3D_RESOURCE_ACCESS_MAP_W;
382*81cffd76SJoachim Henze
383*81cffd76SJoachim Henze texture->texture_ops = texture_ops;
384*81cffd76SJoachim Henze
385*81cffd76SJoachim Henze texture->layer_count = layer_count;
386*81cffd76SJoachim Henze texture->level_count = level_count;
387*81cffd76SJoachim Henze texture->lod = 0;
388*81cffd76SJoachim Henze texture->flags |= WINED3D_TEXTURE_POW2_MAT_IDENT | WINED3D_TEXTURE_NORMALIZED_COORDS;
389*81cffd76SJoachim Henze if (flags & WINED3D_TEXTURE_CREATE_GET_DC_LENIENT)
390*81cffd76SJoachim Henze texture->flags |= WINED3D_TEXTURE_PIN_SYSMEM | WINED3D_TEXTURE_GET_DC_LENIENT;
391*81cffd76SJoachim Henze if (flags & (WINED3D_TEXTURE_CREATE_GET_DC | WINED3D_TEXTURE_CREATE_GET_DC_LENIENT))
392*81cffd76SJoachim Henze texture->flags |= WINED3D_TEXTURE_GET_DC;
393*81cffd76SJoachim Henze if (flags & WINED3D_TEXTURE_CREATE_DISCARD)
394*81cffd76SJoachim Henze texture->flags |= WINED3D_TEXTURE_DISCARD;
395*81cffd76SJoachim Henze if (flags & WINED3D_TEXTURE_CREATE_GENERATE_MIPMAPS)
396*81cffd76SJoachim Henze {
397*81cffd76SJoachim Henze if (!(texture->resource.format_flags & WINED3DFMT_FLAG_GEN_MIPMAP))
398*81cffd76SJoachim Henze WARN("Format doesn't support mipmaps generation, "
399*81cffd76SJoachim Henze "ignoring WINED3D_TEXTURE_CREATE_GENERATE_MIPMAPS flag.\n");
400*81cffd76SJoachim Henze else
401*81cffd76SJoachim Henze texture->flags |= WINED3D_TEXTURE_GENERATE_MIPMAPS;
402*81cffd76SJoachim Henze }
403*81cffd76SJoachim Henze
404*81cffd76SJoachim Henze return WINED3D_OK;
405*81cffd76SJoachim Henze }
406*81cffd76SJoachim Henze
407c2c66affSColin Finck /* Context activation is done by the caller. */
wined3d_texture_remove_buffer_object(struct wined3d_texture * texture,unsigned int sub_resource_idx,const struct wined3d_gl_info * gl_info)408c2c66affSColin Finck static void wined3d_texture_remove_buffer_object(struct wined3d_texture *texture,
409*81cffd76SJoachim Henze #if !defined(STAGING_CSMT)
410c2c66affSColin Finck unsigned int sub_resource_idx, const struct wined3d_gl_info *gl_info)
411c2c66affSColin Finck {
412c2c66affSColin Finck GLuint *buffer_object = &texture->sub_resources[sub_resource_idx].buffer_object;
413c2c66affSColin Finck
414c2c66affSColin Finck GL_EXTCALL(glDeleteBuffers(1, buffer_object));
415c2c66affSColin Finck checkGLcall("glDeleteBuffers");
416c2c66affSColin Finck
417c2c66affSColin Finck TRACE("Deleted buffer object %u for texture %p, sub-resource %u.\n",
418c2c66affSColin Finck *buffer_object, texture, sub_resource_idx);
419c2c66affSColin Finck
420c2c66affSColin Finck wined3d_texture_invalidate_location(texture, sub_resource_idx, WINED3D_LOCATION_BUFFER);
421c2c66affSColin Finck *buffer_object = 0;
422*81cffd76SJoachim Henze #else /* STAGING_CSMT */
423*81cffd76SJoachim Henze unsigned int sub_resource_idx, struct wined3d_context *context)
424*81cffd76SJoachim Henze {
425*81cffd76SJoachim Henze struct wined3d_gl_bo *buffer = texture->sub_resources[sub_resource_idx].buffer;
426*81cffd76SJoachim Henze GLuint name = buffer->name;
427*81cffd76SJoachim Henze
428*81cffd76SJoachim Henze wined3d_device_release_bo(texture->resource.device, buffer, context);
429*81cffd76SJoachim Henze texture->sub_resources[sub_resource_idx].buffer = NULL;
430*81cffd76SJoachim Henze wined3d_texture_invalidate_location(texture, sub_resource_idx, WINED3D_LOCATION_BUFFER);
431*81cffd76SJoachim Henze
432*81cffd76SJoachim Henze TRACE("Deleted buffer object %u for texture %p, sub-resource %u.\n",
433*81cffd76SJoachim Henze name, texture, sub_resource_idx);
434*81cffd76SJoachim Henze #endif /* STAGING_CSMT */
435c2c66affSColin Finck }
436c2c66affSColin Finck
437c2c66affSColin Finck static void wined3d_texture_update_map_binding(struct wined3d_texture *texture)
438c2c66affSColin Finck {
439c2c66affSColin Finck unsigned int sub_count = texture->level_count * texture->layer_count;
440c2c66affSColin Finck const struct wined3d_device *device = texture->resource.device;
441c2c66affSColin Finck DWORD map_binding = texture->update_map_binding;
442c2c66affSColin Finck struct wined3d_context *context = NULL;
443c2c66affSColin Finck unsigned int i;
444c2c66affSColin Finck
445c2c66affSColin Finck if (device->d3d_initialized)
446c2c66affSColin Finck context = context_acquire(device, NULL, 0);
447c2c66affSColin Finck
448c2c66affSColin Finck for (i = 0; i < sub_count; ++i)
449c2c66affSColin Finck {
450c2c66affSColin Finck if (texture->sub_resources[i].locations == texture->resource.map_binding
451c2c66affSColin Finck && !wined3d_texture_load_location(texture, i, context, map_binding))
452c2c66affSColin Finck ERR("Failed to load location %s.\n", wined3d_debug_location(map_binding));
453c2c66affSColin Finck if (texture->resource.map_binding == WINED3D_LOCATION_BUFFER)
454*81cffd76SJoachim Henze #if !defined(STAGING_CSMT)
455c2c66affSColin Finck wined3d_texture_remove_buffer_object(texture, i, context->gl_info);
456*81cffd76SJoachim Henze #else /* STAGING_CSMT */
457*81cffd76SJoachim Henze wined3d_texture_remove_buffer_object(texture, i, context);
458*81cffd76SJoachim Henze #endif /* STAGING_CSMT */
459c2c66affSColin Finck }
460c2c66affSColin Finck
461c2c66affSColin Finck if (context)
462c2c66affSColin Finck context_release(context);
463c2c66affSColin Finck
464c2c66affSColin Finck texture->resource.map_binding = map_binding;
465c2c66affSColin Finck texture->update_map_binding = 0;
466c2c66affSColin Finck }
467c2c66affSColin Finck
468c2c66affSColin Finck void wined3d_texture_set_map_binding(struct wined3d_texture *texture, DWORD map_binding)
469c2c66affSColin Finck {
470c2c66affSColin Finck texture->update_map_binding = map_binding;
471c2c66affSColin Finck if (!texture->resource.map_count)
472c2c66affSColin Finck wined3d_texture_update_map_binding(texture);
473c2c66affSColin Finck }
474c2c66affSColin Finck
475c2c66affSColin Finck /* A GL context is provided by the caller */
476c2c66affSColin Finck static void gltexture_delete(struct wined3d_device *device, const struct wined3d_gl_info *gl_info,
477c2c66affSColin Finck struct gl_texture *tex)
478c2c66affSColin Finck {
479c2c66affSColin Finck context_gl_resource_released(device, tex->name, FALSE);
480c2c66affSColin Finck gl_info->gl_ops.gl.p_glDeleteTextures(1, &tex->name);
481c2c66affSColin Finck tex->name = 0;
482c2c66affSColin Finck }
483c2c66affSColin Finck
4849987f029SAmine Khaldi static unsigned int wined3d_texture_get_gl_sample_count(const struct wined3d_texture *texture)
4859987f029SAmine Khaldi {
4869987f029SAmine Khaldi const struct wined3d_format *format = texture->resource.format;
4879987f029SAmine Khaldi
4889987f029SAmine Khaldi /* TODO: NVIDIA expose their Coverage Sample Anti-Aliasing (CSAA)
4899987f029SAmine Khaldi * feature through type == MULTISAMPLE_XX and quality != 0. This could
4909987f029SAmine Khaldi * be mapped to GL_NV_framebuffer_multisample_coverage.
4919987f029SAmine Khaldi *
4929987f029SAmine Khaldi * AMD have a similar feature called Enhanced Quality Anti-Aliasing
4939987f029SAmine Khaldi * (EQAA), but it does not have an equivalent OpenGL extension. */
4949987f029SAmine Khaldi
4959987f029SAmine Khaldi /* We advertise as many WINED3D_MULTISAMPLE_NON_MASKABLE quality
4969987f029SAmine Khaldi * levels as the count of advertised multisample types for the texture
4979987f029SAmine Khaldi * format. */
4989987f029SAmine Khaldi if (texture->resource.multisample_type == WINED3D_MULTISAMPLE_NON_MASKABLE)
4999987f029SAmine Khaldi {
5009987f029SAmine Khaldi unsigned int i, count = 0;
5019987f029SAmine Khaldi
5029987f029SAmine Khaldi for (i = 0; i < sizeof(format->multisample_types) * CHAR_BIT; ++i)
5039987f029SAmine Khaldi {
5049987f029SAmine Khaldi if (format->multisample_types & 1u << i)
5059987f029SAmine Khaldi {
5069987f029SAmine Khaldi if (texture->resource.multisample_quality == count++)
5079987f029SAmine Khaldi break;
5089987f029SAmine Khaldi }
5099987f029SAmine Khaldi }
5109987f029SAmine Khaldi return i + 1;
5119987f029SAmine Khaldi }
5129987f029SAmine Khaldi
5139987f029SAmine Khaldi return texture->resource.multisample_type;
5149987f029SAmine Khaldi }
5159987f029SAmine Khaldi
516c2c66affSColin Finck /* Context activation is done by the caller. */
517c2c66affSColin Finck /* The caller is responsible for binding the correct texture. */
518c2c66affSColin Finck static void wined3d_texture_allocate_gl_mutable_storage(struct wined3d_texture *texture,
519c2c66affSColin Finck GLenum gl_internal_format, const struct wined3d_format *format,
520c2c66affSColin Finck const struct wined3d_gl_info *gl_info)
521c2c66affSColin Finck {
5229987f029SAmine Khaldi unsigned int level, level_count, layer, layer_count;
523*81cffd76SJoachim Henze GLsizei width, height;
5249987f029SAmine Khaldi GLenum target;
525c2c66affSColin Finck
5269987f029SAmine Khaldi level_count = texture->level_count;
527*81cffd76SJoachim Henze layer_count = texture->target == GL_TEXTURE_2D_ARRAY ? 1 : texture->layer_count;
5289987f029SAmine Khaldi
5299987f029SAmine Khaldi for (layer = 0; layer < layer_count; ++layer)
5309987f029SAmine Khaldi {
5319987f029SAmine Khaldi target = wined3d_texture_get_sub_resource_target(texture, layer * level_count);
5329987f029SAmine Khaldi
5339987f029SAmine Khaldi for (level = 0; level < level_count; ++level)
5349987f029SAmine Khaldi {
5359987f029SAmine Khaldi width = wined3d_texture_get_level_pow2_width(texture, level);
5369987f029SAmine Khaldi height = wined3d_texture_get_level_pow2_height(texture, level);
537c2c66affSColin Finck if (texture->resource.format_flags & WINED3DFMT_FLAG_HEIGHT_SCALE)
538c2c66affSColin Finck {
539c2c66affSColin Finck height *= format->height_scale.numerator;
540c2c66affSColin Finck height /= format->height_scale.denominator;
541c2c66affSColin Finck }
542c2c66affSColin Finck
5439987f029SAmine Khaldi TRACE("texture %p, layer %u, level %u, target %#x, width %u, height %u.\n",
5449987f029SAmine Khaldi texture, layer, level, target, width, height);
545c2c66affSColin Finck
546*81cffd76SJoachim Henze if (texture->target == GL_TEXTURE_2D_ARRAY)
547c2c66affSColin Finck {
5489987f029SAmine Khaldi GL_EXTCALL(glTexImage3D(target, level, gl_internal_format, width, height,
549*81cffd76SJoachim Henze texture->layer_count, 0, format->glFormat, format->glType, NULL));
550c2c66affSColin Finck checkGLcall("glTexImage3D");
551c2c66affSColin Finck }
552c2c66affSColin Finck else
553c2c66affSColin Finck {
554*81cffd76SJoachim Henze gl_info->gl_ops.gl.p_glTexImage2D(target, level, gl_internal_format,
555*81cffd76SJoachim Henze width, height, 0, format->glFormat, format->glType, NULL);
556c2c66affSColin Finck checkGLcall("glTexImage2D");
557c2c66affSColin Finck }
558c2c66affSColin Finck }
559c2c66affSColin Finck }
5609987f029SAmine Khaldi }
561c2c66affSColin Finck
562c2c66affSColin Finck /* Context activation is done by the caller. */
563c2c66affSColin Finck /* The caller is responsible for binding the correct texture. */
564c2c66affSColin Finck static void wined3d_texture_allocate_gl_immutable_storage(struct wined3d_texture *texture,
565c2c66affSColin Finck GLenum gl_internal_format, const struct wined3d_gl_info *gl_info)
566c2c66affSColin Finck {
5679987f029SAmine Khaldi unsigned int samples = wined3d_texture_get_gl_sample_count(texture);
568c2c66affSColin Finck GLsizei height = wined3d_texture_get_level_pow2_height(texture, 0);
5699987f029SAmine Khaldi GLsizei width = wined3d_texture_get_level_pow2_width(texture, 0);
570c2c66affSColin Finck
5719987f029SAmine Khaldi switch (texture->target)
572c2c66affSColin Finck {
5739987f029SAmine Khaldi case GL_TEXTURE_2D_ARRAY:
5749987f029SAmine Khaldi GL_EXTCALL(glTexStorage3D(texture->target, texture->level_count,
5759987f029SAmine Khaldi gl_internal_format, width, height, texture->layer_count));
5769987f029SAmine Khaldi break;
5779987f029SAmine Khaldi case GL_TEXTURE_2D_MULTISAMPLE:
5789987f029SAmine Khaldi GL_EXTCALL(glTexStorage2DMultisample(texture->target, samples,
5799987f029SAmine Khaldi gl_internal_format, width, height, GL_FALSE));
5809987f029SAmine Khaldi break;
5819987f029SAmine Khaldi case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
5829987f029SAmine Khaldi GL_EXTCALL(glTexStorage3DMultisample(texture->target, samples,
5839987f029SAmine Khaldi gl_internal_format, width, height, texture->layer_count, GL_FALSE));
5849987f029SAmine Khaldi break;
5859987f029SAmine Khaldi default:
5869987f029SAmine Khaldi GL_EXTCALL(glTexStorage2D(texture->target, texture->level_count,
5879987f029SAmine Khaldi gl_internal_format, width, height));
5889987f029SAmine Khaldi break;
589c2c66affSColin Finck }
5909987f029SAmine Khaldi
5919987f029SAmine Khaldi checkGLcall("allocate immutable storage");
592c2c66affSColin Finck }
593c2c66affSColin Finck
594c2c66affSColin Finck static void wined3d_texture_unload_gl_texture(struct wined3d_texture *texture)
595c2c66affSColin Finck {
596c2c66affSColin Finck struct wined3d_device *device = texture->resource.device;
597c2c66affSColin Finck const struct wined3d_gl_info *gl_info = NULL;
598c2c66affSColin Finck struct wined3d_context *context = NULL;
599c2c66affSColin Finck
600c2c66affSColin Finck if (texture->texture_rgb.name || texture->texture_srgb.name
601c2c66affSColin Finck || texture->rb_multisample || texture->rb_resolved)
602c2c66affSColin Finck {
603c2c66affSColin Finck context = context_acquire(device, NULL, 0);
604c2c66affSColin Finck gl_info = context->gl_info;
605c2c66affSColin Finck }
606c2c66affSColin Finck
607c2c66affSColin Finck if (texture->texture_rgb.name)
608c2c66affSColin Finck gltexture_delete(device, context->gl_info, &texture->texture_rgb);
609c2c66affSColin Finck
610c2c66affSColin Finck if (texture->texture_srgb.name)
611c2c66affSColin Finck gltexture_delete(device, context->gl_info, &texture->texture_srgb);
612c2c66affSColin Finck
613c2c66affSColin Finck if (texture->rb_multisample)
614c2c66affSColin Finck {
615c2c66affSColin Finck TRACE("Deleting multisample renderbuffer %u.\n", texture->rb_multisample);
616c2c66affSColin Finck context_gl_resource_released(device, texture->rb_multisample, TRUE);
617c2c66affSColin Finck gl_info->fbo_ops.glDeleteRenderbuffers(1, &texture->rb_multisample);
618c2c66affSColin Finck texture->rb_multisample = 0;
619c2c66affSColin Finck }
620c2c66affSColin Finck
621c2c66affSColin Finck if (texture->rb_resolved)
622c2c66affSColin Finck {
623c2c66affSColin Finck TRACE("Deleting resolved renderbuffer %u.\n", texture->rb_resolved);
624c2c66affSColin Finck context_gl_resource_released(device, texture->rb_resolved, TRUE);
625c2c66affSColin Finck gl_info->fbo_ops.glDeleteRenderbuffers(1, &texture->rb_resolved);
626c2c66affSColin Finck texture->rb_resolved = 0;
627c2c66affSColin Finck }
628c2c66affSColin Finck
629c2c66affSColin Finck if (context) context_release(context);
630c2c66affSColin Finck
631c2c66affSColin Finck wined3d_texture_set_dirty(texture);
632c2c66affSColin Finck
633c2c66affSColin Finck resource_unload(&texture->resource);
634c2c66affSColin Finck }
635c2c66affSColin Finck
636c2c66affSColin Finck static void wined3d_texture_sub_resources_destroyed(struct wined3d_texture *texture)
637c2c66affSColin Finck {
638c2c66affSColin Finck unsigned int sub_count = texture->level_count * texture->layer_count;
639c2c66affSColin Finck struct wined3d_texture_sub_resource *sub_resource;
640c2c66affSColin Finck unsigned int i;
641c2c66affSColin Finck
642c2c66affSColin Finck for (i = 0; i < sub_count; ++i)
643c2c66affSColin Finck {
644c2c66affSColin Finck sub_resource = &texture->sub_resources[i];
645c2c66affSColin Finck if (sub_resource->parent)
646c2c66affSColin Finck {
647c2c66affSColin Finck TRACE("sub-resource %u.\n", i);
648c2c66affSColin Finck sub_resource->parent_ops->wined3d_object_destroyed(sub_resource->parent);
649c2c66affSColin Finck sub_resource->parent = NULL;
650c2c66affSColin Finck }
651c2c66affSColin Finck }
652c2c66affSColin Finck }
653c2c66affSColin Finck
654c2c66affSColin Finck static void wined3d_texture_cleanup(struct wined3d_texture *texture)
655c2c66affSColin Finck {
656c2c66affSColin Finck unsigned int sub_count = texture->level_count * texture->layer_count;
657c2c66affSColin Finck struct wined3d_device *device = texture->resource.device;
658c2c66affSColin Finck struct wined3d_context *context = NULL;
659*81cffd76SJoachim Henze #if !defined(STAGING_CSMT)
660*81cffd76SJoachim Henze const struct wined3d_gl_info *gl_info;
661c2c66affSColin Finck GLuint buffer_object;
662*81cffd76SJoachim Henze #else /* STAGING_CSMT */
663*81cffd76SJoachim Henze struct wined3d_gl_bo *buffer;
664*81cffd76SJoachim Henze #endif /* STAGING_CSMT */
665c2c66affSColin Finck unsigned int i;
666c2c66affSColin Finck
667c2c66affSColin Finck TRACE("texture %p.\n", texture);
668c2c66affSColin Finck
669c2c66affSColin Finck for (i = 0; i < sub_count; ++i)
670c2c66affSColin Finck {
671*81cffd76SJoachim Henze #if !defined(STAGING_CSMT)
672c2c66affSColin Finck if (!(buffer_object = texture->sub_resources[i].buffer_object))
673c2c66affSColin Finck continue;
674c2c66affSColin Finck
675c2c66affSColin Finck TRACE("Deleting buffer object %u.\n", buffer_object);
676*81cffd76SJoachim Henze #else /* STAGING_CSMT */
677*81cffd76SJoachim Henze if (!(buffer = texture->sub_resources[i].buffer))
678*81cffd76SJoachim Henze continue;
679*81cffd76SJoachim Henze
680*81cffd76SJoachim Henze TRACE("Deleting buffer object %u.\n", buffer->name);
681*81cffd76SJoachim Henze #endif /* STAGING_CSMT */
682c2c66affSColin Finck
683c2c66affSColin Finck /* We may not be able to get a context in wined3d_texture_cleanup() in
684c2c66affSColin Finck * general, but if a buffer object was previously created we can. */
685c2c66affSColin Finck if (!context)
686*81cffd76SJoachim Henze #if !defined(STAGING_CSMT)
687c2c66affSColin Finck {
688c2c66affSColin Finck context = context_acquire(device, NULL, 0);
689c2c66affSColin Finck gl_info = context->gl_info;
690c2c66affSColin Finck }
691c2c66affSColin Finck
692c2c66affSColin Finck GL_EXTCALL(glDeleteBuffers(1, &buffer_object));
693*81cffd76SJoachim Henze #else /* STAGING_CSMT */
6947af3969eSAmine Khaldi context = context_acquire(device, NULL, 0);
6957af3969eSAmine Khaldi
696*81cffd76SJoachim Henze wined3d_device_release_bo(device, buffer, context);
697*81cffd76SJoachim Henze texture->sub_resources[i].buffer = NULL;
698*81cffd76SJoachim Henze #endif /* STAGING_CSMT */
6997af3969eSAmine Khaldi }
700c2c66affSColin Finck if (context)
701c2c66affSColin Finck context_release(context);
702c2c66affSColin Finck
703*81cffd76SJoachim Henze texture->texture_ops->texture_cleanup_sub_resources(texture);
7049987f029SAmine Khaldi if (texture->overlay_info)
7059987f029SAmine Khaldi {
7069987f029SAmine Khaldi for (i = 0; i < sub_count; ++i)
7079987f029SAmine Khaldi {
7089987f029SAmine Khaldi struct wined3d_overlay_info *info = &texture->overlay_info[i];
7099987f029SAmine Khaldi struct wined3d_overlay_info *overlay, *cur;
7109987f029SAmine Khaldi
7119987f029SAmine Khaldi list_remove(&info->entry);
7129987f029SAmine Khaldi LIST_FOR_EACH_ENTRY_SAFE(overlay, cur, &info->overlays, struct wined3d_overlay_info, entry)
7139987f029SAmine Khaldi {
7149987f029SAmine Khaldi list_remove(&overlay->entry);
7159987f029SAmine Khaldi }
7169987f029SAmine Khaldi }
7179987f029SAmine Khaldi heap_free(texture->overlay_info);
7189987f029SAmine Khaldi }
719c2c66affSColin Finck wined3d_texture_unload_gl_texture(texture);
720c2c66affSColin Finck }
721c2c66affSColin Finck
722c2c66affSColin Finck void wined3d_texture_set_swapchain(struct wined3d_texture *texture, struct wined3d_swapchain *swapchain)
723c2c66affSColin Finck {
724c2c66affSColin Finck texture->swapchain = swapchain;
725c2c66affSColin Finck wined3d_resource_update_draw_binding(&texture->resource);
726c2c66affSColin Finck }
727c2c66affSColin Finck
728c2c66affSColin Finck /* Context activation is done by the caller. */
729c2c66affSColin Finck void wined3d_texture_bind(struct wined3d_texture *texture,
730c2c66affSColin Finck struct wined3d_context *context, BOOL srgb)
731c2c66affSColin Finck {
732c2c66affSColin Finck const struct wined3d_gl_info *gl_info = context->gl_info;
733c2c66affSColin Finck const struct wined3d_format *format = texture->resource.format;
734c2c66affSColin Finck const struct color_fixup_desc fixup = format->color_fixup;
735c2c66affSColin Finck struct gl_texture *gl_tex;
736c2c66affSColin Finck GLenum target;
737c2c66affSColin Finck
738c2c66affSColin Finck TRACE("texture %p, context %p, srgb %#x.\n", texture, context, srgb);
739c2c66affSColin Finck
740c2c66affSColin Finck if (!needs_separate_srgb_gl_texture(context, texture))
741c2c66affSColin Finck srgb = FALSE;
742c2c66affSColin Finck
743c2c66affSColin Finck /* sRGB mode cache for preload() calls outside drawprim. */
744c2c66affSColin Finck if (srgb)
745c2c66affSColin Finck texture->flags |= WINED3D_TEXTURE_IS_SRGB;
746c2c66affSColin Finck else
747c2c66affSColin Finck texture->flags &= ~WINED3D_TEXTURE_IS_SRGB;
748c2c66affSColin Finck
749c2c66affSColin Finck gl_tex = wined3d_texture_get_gl_texture(texture, srgb);
750c2c66affSColin Finck target = texture->target;
751c2c66affSColin Finck
752c2c66affSColin Finck if (gl_tex->name)
753c2c66affSColin Finck {
754c2c66affSColin Finck context_bind_texture(context, target, gl_tex->name);
755c2c66affSColin Finck return;
756c2c66affSColin Finck }
757c2c66affSColin Finck
758c2c66affSColin Finck gl_info->gl_ops.gl.p_glGenTextures(1, &gl_tex->name);
759c2c66affSColin Finck checkGLcall("glGenTextures");
760c2c66affSColin Finck TRACE("Generated texture %d.\n", gl_tex->name);
761c2c66affSColin Finck
762c2c66affSColin Finck if (!gl_tex->name)
763c2c66affSColin Finck {
764c2c66affSColin Finck ERR("Failed to generate a texture name.\n");
765c2c66affSColin Finck return;
766c2c66affSColin Finck }
767c2c66affSColin Finck
768c2c66affSColin Finck /* Initialise the state of the texture object to the OpenGL defaults, not
769c2c66affSColin Finck * the wined3d defaults. */
770c2c66affSColin Finck gl_tex->sampler_desc.address_u = WINED3D_TADDRESS_WRAP;
771c2c66affSColin Finck gl_tex->sampler_desc.address_v = WINED3D_TADDRESS_WRAP;
772c2c66affSColin Finck gl_tex->sampler_desc.address_w = WINED3D_TADDRESS_WRAP;
773c2c66affSColin Finck memset(gl_tex->sampler_desc.border_color, 0, sizeof(gl_tex->sampler_desc.border_color));
774c2c66affSColin Finck gl_tex->sampler_desc.mag_filter = WINED3D_TEXF_LINEAR;
775c2c66affSColin Finck gl_tex->sampler_desc.min_filter = WINED3D_TEXF_POINT; /* GL_NEAREST_MIPMAP_LINEAR */
776c2c66affSColin Finck gl_tex->sampler_desc.mip_filter = WINED3D_TEXF_LINEAR; /* GL_NEAREST_MIPMAP_LINEAR */
777c2c66affSColin Finck gl_tex->sampler_desc.lod_bias = 0.0f;
778c2c66affSColin Finck gl_tex->sampler_desc.min_lod = -1000.0f;
779c2c66affSColin Finck gl_tex->sampler_desc.max_lod = 1000.0f;
780c2c66affSColin Finck gl_tex->sampler_desc.max_anisotropy = 1;
781c2c66affSColin Finck gl_tex->sampler_desc.compare = FALSE;
782c2c66affSColin Finck gl_tex->sampler_desc.comparison_func = WINED3D_CMP_LESSEQUAL;
783c2c66affSColin Finck if (context->gl_info->supported[EXT_TEXTURE_SRGB_DECODE])
784c2c66affSColin Finck gl_tex->sampler_desc.srgb_decode = TRUE;
785c2c66affSColin Finck else
786c2c66affSColin Finck gl_tex->sampler_desc.srgb_decode = srgb;
787c2c66affSColin Finck gl_tex->base_level = 0;
788c2c66affSColin Finck wined3d_texture_set_dirty(texture);
789c2c66affSColin Finck
790c2c66affSColin Finck context_bind_texture(context, target, gl_tex->name);
791c2c66affSColin Finck
792c2c66affSColin Finck /* For a new texture we have to set the texture levels after binding the
793c2c66affSColin Finck * texture. Beware that texture rectangles do not support mipmapping, but
794c2c66affSColin Finck * set the maxmiplevel if we're relying on the partial
795c2c66affSColin Finck * GL_ARB_texture_non_power_of_two emulation with texture rectangles.
796c2c66affSColin Finck * (I.e., do not care about cond_np2 here, just look for
797c2c66affSColin Finck * GL_TEXTURE_RECTANGLE_ARB.) */
798c2c66affSColin Finck if (target != GL_TEXTURE_RECTANGLE_ARB)
799c2c66affSColin Finck {
800c2c66affSColin Finck TRACE("Setting GL_TEXTURE_MAX_LEVEL to %u.\n", texture->level_count - 1);
801c2c66affSColin Finck gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, texture->level_count - 1);
802c2c66affSColin Finck checkGLcall("glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, texture->level_count)");
803c2c66affSColin Finck }
804c2c66affSColin Finck
805c2c66affSColin Finck if (target == GL_TEXTURE_CUBE_MAP_ARB)
806c2c66affSColin Finck {
807c2c66affSColin Finck /* Cubemaps are always set to clamp, regardless of the sampler state. */
808c2c66affSColin Finck gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
809c2c66affSColin Finck gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
810c2c66affSColin Finck gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
811c2c66affSColin Finck }
812c2c66affSColin Finck
813c2c66affSColin Finck if (texture->flags & WINED3D_TEXTURE_COND_NP2)
814c2c66affSColin Finck {
815c2c66affSColin Finck /* Conditinal non power of two textures use a different clamping
816c2c66affSColin Finck * default. If we're using the GL_WINE_normalized_texrect partial
817c2c66affSColin Finck * driver emulation, we're dealing with a GL_TEXTURE_2D texture which
818c2c66affSColin Finck * has the address mode set to repeat - something that prevents us
819c2c66affSColin Finck * from hitting the accelerated codepath. Thus manually set the GL
820c2c66affSColin Finck * state. The same applies to filtering. Even if the texture has only
821c2c66affSColin Finck * one mip level, the default LINEAR_MIPMAP_LINEAR filter causes a SW
822c2c66affSColin Finck * fallback on macos. */
823c2c66affSColin Finck gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
824c2c66affSColin Finck gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
825c2c66affSColin Finck gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
826c2c66affSColin Finck gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
827c2c66affSColin Finck checkGLcall("glTexParameteri");
828c2c66affSColin Finck gl_tex->sampler_desc.address_u = WINED3D_TADDRESS_CLAMP;
829c2c66affSColin Finck gl_tex->sampler_desc.address_v = WINED3D_TADDRESS_CLAMP;
830c2c66affSColin Finck gl_tex->sampler_desc.mag_filter = WINED3D_TEXF_POINT;
831c2c66affSColin Finck gl_tex->sampler_desc.min_filter = WINED3D_TEXF_POINT;
832c2c66affSColin Finck gl_tex->sampler_desc.mip_filter = WINED3D_TEXF_NONE;
833c2c66affSColin Finck }
834c2c66affSColin Finck
835c2c66affSColin Finck if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] && gl_info->supported[ARB_DEPTH_TEXTURE])
836c2c66affSColin Finck {
837c2c66affSColin Finck gl_info->gl_ops.gl.p_glTexParameteri(target, GL_DEPTH_TEXTURE_MODE_ARB, GL_INTENSITY);
838c2c66affSColin Finck checkGLcall("glTexParameteri(GL_DEPTH_TEXTURE_MODE_ARB, GL_INTENSITY)");
839c2c66affSColin Finck }
840c2c66affSColin Finck
841c2c66affSColin Finck if (!is_identity_fixup(fixup) && can_use_texture_swizzle(gl_info, format))
842c2c66affSColin Finck {
843c2c66affSColin Finck static const GLenum swizzle_source[] =
844c2c66affSColin Finck {
845c2c66affSColin Finck GL_ZERO, /* CHANNEL_SOURCE_ZERO */
846c2c66affSColin Finck GL_ONE, /* CHANNEL_SOURCE_ONE */
847c2c66affSColin Finck GL_RED, /* CHANNEL_SOURCE_X */
848c2c66affSColin Finck GL_GREEN, /* CHANNEL_SOURCE_Y */
849c2c66affSColin Finck GL_BLUE, /* CHANNEL_SOURCE_Z */
850c2c66affSColin Finck GL_ALPHA, /* CHANNEL_SOURCE_W */
851c2c66affSColin Finck };
852c2c66affSColin Finck struct
853c2c66affSColin Finck {
854c2c66affSColin Finck GLint x, y, z, w;
855c2c66affSColin Finck }
856c2c66affSColin Finck swizzle;
857c2c66affSColin Finck
858c2c66affSColin Finck swizzle.x = swizzle_source[fixup.x_source];
859c2c66affSColin Finck swizzle.y = swizzle_source[fixup.y_source];
860c2c66affSColin Finck swizzle.z = swizzle_source[fixup.z_source];
861c2c66affSColin Finck swizzle.w = swizzle_source[fixup.w_source];
862c2c66affSColin Finck gl_info->gl_ops.gl.p_glTexParameteriv(target, GL_TEXTURE_SWIZZLE_RGBA, &swizzle.x);
863c2c66affSColin Finck checkGLcall("glTexParameteriv(GL_TEXTURE_SWIZZLE_RGBA)");
864c2c66affSColin Finck }
865c2c66affSColin Finck }
866c2c66affSColin Finck
867c2c66affSColin Finck /* Context activation is done by the caller. */
868c2c66affSColin Finck void wined3d_texture_bind_and_dirtify(struct wined3d_texture *texture,
869c2c66affSColin Finck struct wined3d_context *context, BOOL srgb)
870c2c66affSColin Finck {
871c2c66affSColin Finck /* We don't need a specific texture unit, but after binding the texture
872c2c66affSColin Finck * the current unit is dirty. Read the unit back instead of switching to
873c2c66affSColin Finck * 0, this avoids messing around with the state manager's GL states. The
874c2c66affSColin Finck * current texture unit should always be a valid one.
875c2c66affSColin Finck *
876c2c66affSColin Finck * To be more specific, this is tricky because we can implicitly be
877c2c66affSColin Finck * called from sampler() in state.c. This means we can't touch anything
878c2c66affSColin Finck * other than whatever happens to be the currently active texture, or we
879c2c66affSColin Finck * would risk marking already applied sampler states dirty again. */
880c2c66affSColin Finck if (context->active_texture < ARRAY_SIZE(context->rev_tex_unit_map))
881c2c66affSColin Finck {
882c2c66affSColin Finck DWORD active_sampler = context->rev_tex_unit_map[context->active_texture];
883c2c66affSColin Finck if (active_sampler != WINED3D_UNMAPPED_STAGE)
884c2c66affSColin Finck context_invalidate_state(context, STATE_SAMPLER(active_sampler));
885c2c66affSColin Finck }
886c2c66affSColin Finck /* FIXME: Ideally we'd only do this when touching a binding that's used by
887c2c66affSColin Finck * a shader. */
888c2c66affSColin Finck context_invalidate_compute_state(context, STATE_COMPUTE_SHADER_RESOURCE_BINDING);
889c2c66affSColin Finck context_invalidate_state(context, STATE_GRAPHICS_SHADER_RESOURCE_BINDING);
890c2c66affSColin Finck
891c2c66affSColin Finck wined3d_texture_bind(texture, context, srgb);
892c2c66affSColin Finck }
893c2c66affSColin Finck
894c2c66affSColin Finck /* Context activation is done by the caller (state handler). */
895c2c66affSColin Finck /* This function relies on the correct texture being bound and loaded. */
896c2c66affSColin Finck void wined3d_texture_apply_sampler_desc(struct wined3d_texture *texture,
897c2c66affSColin Finck const struct wined3d_sampler_desc *sampler_desc, const struct wined3d_context *context)
898c2c66affSColin Finck {
899c2c66affSColin Finck const struct wined3d_gl_info *gl_info = context->gl_info;
900c2c66affSColin Finck GLenum target = texture->target;
901c2c66affSColin Finck struct gl_texture *gl_tex;
902c2c66affSColin Finck DWORD state;
903c2c66affSColin Finck
904c2c66affSColin Finck TRACE("texture %p, sampler_desc %p, context %p.\n", texture, sampler_desc, context);
905c2c66affSColin Finck
906c2c66affSColin Finck gl_tex = wined3d_texture_get_gl_texture(texture, texture->flags & WINED3D_TEXTURE_IS_SRGB);
907c2c66affSColin Finck
908c2c66affSColin Finck state = sampler_desc->address_u;
909c2c66affSColin Finck if (state != gl_tex->sampler_desc.address_u)
910c2c66affSColin Finck {
911c2c66affSColin Finck gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_S,
912c2c66affSColin Finck gl_info->wrap_lookup[state - WINED3D_TADDRESS_WRAP]);
913c2c66affSColin Finck gl_tex->sampler_desc.address_u = state;
914c2c66affSColin Finck }
915c2c66affSColin Finck
916c2c66affSColin Finck state = sampler_desc->address_v;
917c2c66affSColin Finck if (state != gl_tex->sampler_desc.address_v)
918c2c66affSColin Finck {
919c2c66affSColin Finck gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_T,
920c2c66affSColin Finck gl_info->wrap_lookup[state - WINED3D_TADDRESS_WRAP]);
921c2c66affSColin Finck gl_tex->sampler_desc.address_v = state;
922c2c66affSColin Finck }
923c2c66affSColin Finck
924c2c66affSColin Finck state = sampler_desc->address_w;
925c2c66affSColin Finck if (state != gl_tex->sampler_desc.address_w)
926c2c66affSColin Finck {
927c2c66affSColin Finck gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_R,
928c2c66affSColin Finck gl_info->wrap_lookup[state - WINED3D_TADDRESS_WRAP]);
929c2c66affSColin Finck gl_tex->sampler_desc.address_w = state;
930c2c66affSColin Finck }
931c2c66affSColin Finck
932c2c66affSColin Finck if (memcmp(gl_tex->sampler_desc.border_color, sampler_desc->border_color,
933c2c66affSColin Finck sizeof(gl_tex->sampler_desc.border_color)))
934c2c66affSColin Finck {
935c2c66affSColin Finck gl_info->gl_ops.gl.p_glTexParameterfv(target, GL_TEXTURE_BORDER_COLOR, &sampler_desc->border_color[0]);
936c2c66affSColin Finck memcpy(gl_tex->sampler_desc.border_color, sampler_desc->border_color,
937c2c66affSColin Finck sizeof(gl_tex->sampler_desc.border_color));
938c2c66affSColin Finck }
939c2c66affSColin Finck
940c2c66affSColin Finck state = sampler_desc->mag_filter;
941c2c66affSColin Finck if (state != gl_tex->sampler_desc.mag_filter)
942c2c66affSColin Finck {
943c2c66affSColin Finck gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAG_FILTER, wined3d_gl_mag_filter(state));
944c2c66affSColin Finck gl_tex->sampler_desc.mag_filter = state;
945c2c66affSColin Finck }
946c2c66affSColin Finck
947c2c66affSColin Finck if (sampler_desc->min_filter != gl_tex->sampler_desc.min_filter
948c2c66affSColin Finck || sampler_desc->mip_filter != gl_tex->sampler_desc.mip_filter)
949c2c66affSColin Finck {
950c2c66affSColin Finck gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MIN_FILTER,
951c2c66affSColin Finck wined3d_gl_min_mip_filter(sampler_desc->min_filter, sampler_desc->mip_filter));
952c2c66affSColin Finck gl_tex->sampler_desc.min_filter = sampler_desc->min_filter;
953c2c66affSColin Finck gl_tex->sampler_desc.mip_filter = sampler_desc->mip_filter;
954c2c66affSColin Finck }
955c2c66affSColin Finck
956c2c66affSColin Finck state = sampler_desc->max_anisotropy;
957c2c66affSColin Finck if (state != gl_tex->sampler_desc.max_anisotropy)
958c2c66affSColin Finck {
959d6ac0a71SAmine Khaldi if (gl_info->supported[ARB_TEXTURE_FILTER_ANISOTROPIC])
960d6ac0a71SAmine Khaldi gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAX_ANISOTROPY, state);
961c2c66affSColin Finck else
962c2c66affSColin Finck WARN("Anisotropic filtering not supported.\n");
963c2c66affSColin Finck gl_tex->sampler_desc.max_anisotropy = state;
964c2c66affSColin Finck }
965c2c66affSColin Finck
966c2c66affSColin Finck if (!sampler_desc->srgb_decode != !gl_tex->sampler_desc.srgb_decode
967c2c66affSColin Finck && (context->d3d_info->wined3d_creation_flags & WINED3D_SRGB_READ_WRITE_CONTROL)
968c2c66affSColin Finck && gl_info->supported[EXT_TEXTURE_SRGB_DECODE])
969c2c66affSColin Finck {
970c2c66affSColin Finck gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_SRGB_DECODE_EXT,
971c2c66affSColin Finck sampler_desc->srgb_decode ? GL_DECODE_EXT : GL_SKIP_DECODE_EXT);
972c2c66affSColin Finck gl_tex->sampler_desc.srgb_decode = sampler_desc->srgb_decode;
973c2c66affSColin Finck }
974c2c66affSColin Finck
975c2c66affSColin Finck if (!sampler_desc->compare != !gl_tex->sampler_desc.compare)
976c2c66affSColin Finck {
977c2c66affSColin Finck if (sampler_desc->compare)
978c2c66affSColin Finck gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE_ARB);
979c2c66affSColin Finck else
980c2c66affSColin Finck gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_NONE);
981c2c66affSColin Finck gl_tex->sampler_desc.compare = sampler_desc->compare;
982c2c66affSColin Finck }
983c2c66affSColin Finck
984c2c66affSColin Finck checkGLcall("Texture parameter application");
985c2c66affSColin Finck
986c2c66affSColin Finck if (gl_info->supported[EXT_TEXTURE_LOD_BIAS])
987c2c66affSColin Finck {
988c2c66affSColin Finck gl_info->gl_ops.gl.p_glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT,
989c2c66affSColin Finck GL_TEXTURE_LOD_BIAS_EXT, sampler_desc->lod_bias);
990c2c66affSColin Finck checkGLcall("glTexEnvf(GL_TEXTURE_LOD_BIAS_EXT, ...)");
991c2c66affSColin Finck }
992c2c66affSColin Finck }
993c2c66affSColin Finck
994c2c66affSColin Finck ULONG CDECL wined3d_texture_incref(struct wined3d_texture *texture)
995c2c66affSColin Finck {
996c2c66affSColin Finck ULONG refcount;
997c2c66affSColin Finck
998c2c66affSColin Finck TRACE("texture %p, swapchain %p.\n", texture, texture->swapchain);
999c2c66affSColin Finck
1000c2c66affSColin Finck if (texture->swapchain)
1001c2c66affSColin Finck return wined3d_swapchain_incref(texture->swapchain);
1002c2c66affSColin Finck
1003c2c66affSColin Finck refcount = InterlockedIncrement(&texture->resource.ref);
1004c2c66affSColin Finck TRACE("%p increasing refcount to %u.\n", texture, refcount);
1005c2c66affSColin Finck
1006c2c66affSColin Finck return refcount;
1007c2c66affSColin Finck }
1008c2c66affSColin Finck
1009c2c66affSColin Finck static void wined3d_texture_cleanup_sync(struct wined3d_texture *texture)
1010c2c66affSColin Finck {
1011c2c66affSColin Finck wined3d_texture_sub_resources_destroyed(texture);
1012c2c66affSColin Finck resource_cleanup(&texture->resource);
1013c2c66affSColin Finck wined3d_resource_wait_idle(&texture->resource);
1014c2c66affSColin Finck wined3d_texture_cleanup(texture);
1015c2c66affSColin Finck }
1016c2c66affSColin Finck
1017c2c66affSColin Finck static void wined3d_texture_destroy_object(void *object)
1018c2c66affSColin Finck {
1019c2c66affSColin Finck wined3d_texture_cleanup(object);
10209987f029SAmine Khaldi heap_free(object);
1021c2c66affSColin Finck }
1022c2c66affSColin Finck
1023c2c66affSColin Finck ULONG CDECL wined3d_texture_decref(struct wined3d_texture *texture)
1024c2c66affSColin Finck {
1025c2c66affSColin Finck ULONG refcount;
1026c2c66affSColin Finck
1027c2c66affSColin Finck TRACE("texture %p, swapchain %p.\n", texture, texture->swapchain);
1028c2c66affSColin Finck
1029c2c66affSColin Finck if (texture->swapchain)
1030c2c66affSColin Finck return wined3d_swapchain_decref(texture->swapchain);
1031c2c66affSColin Finck
1032c2c66affSColin Finck refcount = InterlockedDecrement(&texture->resource.ref);
1033c2c66affSColin Finck TRACE("%p decreasing refcount to %u.\n", texture, refcount);
1034c2c66affSColin Finck
1035c2c66affSColin Finck if (!refcount)
1036c2c66affSColin Finck {
1037c2c66affSColin Finck /* Wait for the texture to become idle if it's using user memory,
1038c2c66affSColin Finck * since the application is allowed to free that memory once the
1039c2c66affSColin Finck * texture is destroyed. Note that this implies that
1040c2c66affSColin Finck * wined3d_texture_destroy_object() can't access that memory either. */
1041c2c66affSColin Finck if (texture->user_memory)
1042c2c66affSColin Finck wined3d_resource_wait_idle(&texture->resource);
1043c2c66affSColin Finck wined3d_texture_sub_resources_destroyed(texture);
1044c2c66affSColin Finck texture->resource.parent_ops->wined3d_object_destroyed(texture->resource.parent);
1045c2c66affSColin Finck resource_cleanup(&texture->resource);
1046c2c66affSColin Finck wined3d_cs_destroy_object(texture->resource.device->cs, wined3d_texture_destroy_object, texture);
1047c2c66affSColin Finck }
1048c2c66affSColin Finck
1049c2c66affSColin Finck return refcount;
1050c2c66affSColin Finck }
1051c2c66affSColin Finck
1052c2c66affSColin Finck struct wined3d_resource * CDECL wined3d_texture_get_resource(struct wined3d_texture *texture)
1053c2c66affSColin Finck {
1054c2c66affSColin Finck TRACE("texture %p.\n", texture);
1055c2c66affSColin Finck
1056c2c66affSColin Finck return &texture->resource;
1057c2c66affSColin Finck }
1058c2c66affSColin Finck
1059c2c66affSColin Finck static BOOL color_key_equal(const struct wined3d_color_key *c1, struct wined3d_color_key *c2)
1060c2c66affSColin Finck {
1061c2c66affSColin Finck return c1->color_space_low_value == c2->color_space_low_value
1062c2c66affSColin Finck && c1->color_space_high_value == c2->color_space_high_value;
1063c2c66affSColin Finck }
1064c2c66affSColin Finck
1065c2c66affSColin Finck /* Context activation is done by the caller */
1066c2c66affSColin Finck void wined3d_texture_load(struct wined3d_texture *texture,
1067c2c66affSColin Finck struct wined3d_context *context, BOOL srgb)
1068c2c66affSColin Finck {
1069c2c66affSColin Finck UINT sub_count = texture->level_count * texture->layer_count;
1070c2c66affSColin Finck const struct wined3d_d3d_info *d3d_info = context->d3d_info;
1071c2c66affSColin Finck DWORD flag;
1072c2c66affSColin Finck UINT i;
1073c2c66affSColin Finck
1074c2c66affSColin Finck TRACE("texture %p, context %p, srgb %#x.\n", texture, context, srgb);
1075c2c66affSColin Finck
1076c2c66affSColin Finck if (!needs_separate_srgb_gl_texture(context, texture))
1077c2c66affSColin Finck srgb = FALSE;
1078c2c66affSColin Finck
1079c2c66affSColin Finck if (srgb)
1080c2c66affSColin Finck flag = WINED3D_TEXTURE_SRGB_VALID;
1081c2c66affSColin Finck else
1082c2c66affSColin Finck flag = WINED3D_TEXTURE_RGB_VALID;
1083c2c66affSColin Finck
1084c2c66affSColin Finck if (!d3d_info->shader_color_key
1085c2c66affSColin Finck && (!(texture->async.flags & WINED3D_TEXTURE_ASYNC_COLOR_KEY)
1086c2c66affSColin Finck != !(texture->async.color_key_flags & WINED3D_CKEY_SRC_BLT)
1087c2c66affSColin Finck || (texture->async.flags & WINED3D_TEXTURE_ASYNC_COLOR_KEY
1088c2c66affSColin Finck && !color_key_equal(&texture->async.gl_color_key, &texture->async.src_blt_color_key))))
1089c2c66affSColin Finck {
1090c2c66affSColin Finck unsigned int sub_count = texture->level_count * texture->layer_count;
1091c2c66affSColin Finck unsigned int i;
1092c2c66affSColin Finck
1093c2c66affSColin Finck TRACE("Reloading because of color key value change.\n");
1094c2c66affSColin Finck for (i = 0; i < sub_count; i++)
1095c2c66affSColin Finck {
1096c2c66affSColin Finck if (!wined3d_texture_load_location(texture, i, context, texture->resource.map_binding))
1097c2c66affSColin Finck ERR("Failed to load location %s.\n", wined3d_debug_location(texture->resource.map_binding));
1098c2c66affSColin Finck else
1099c2c66affSColin Finck wined3d_texture_invalidate_location(texture, i, ~texture->resource.map_binding);
1100c2c66affSColin Finck }
1101c2c66affSColin Finck
1102c2c66affSColin Finck texture->async.gl_color_key = texture->async.src_blt_color_key;
1103c2c66affSColin Finck }
1104c2c66affSColin Finck
1105c2c66affSColin Finck if (texture->flags & flag)
1106c2c66affSColin Finck {
1107c2c66affSColin Finck TRACE("Texture %p not dirty, nothing to do.\n", texture);
1108c2c66affSColin Finck return;
1109c2c66affSColin Finck }
1110c2c66affSColin Finck
1111c2c66affSColin Finck /* Reload the surfaces if the texture is marked dirty. */
1112c2c66affSColin Finck for (i = 0; i < sub_count; ++i)
1113c2c66affSColin Finck {
1114c2c66affSColin Finck if (!wined3d_texture_load_location(texture, i, context,
1115c2c66affSColin Finck srgb ? WINED3D_LOCATION_TEXTURE_SRGB : WINED3D_LOCATION_TEXTURE_RGB))
1116c2c66affSColin Finck ERR("Failed to load location (srgb %#x).\n", srgb);
1117c2c66affSColin Finck }
1118c2c66affSColin Finck texture->flags |= flag;
1119c2c66affSColin Finck }
1120c2c66affSColin Finck
1121c2c66affSColin Finck void * CDECL wined3d_texture_get_parent(const struct wined3d_texture *texture)
1122c2c66affSColin Finck {
1123c2c66affSColin Finck TRACE("texture %p.\n", texture);
1124c2c66affSColin Finck
1125c2c66affSColin Finck return texture->resource.parent;
1126c2c66affSColin Finck }
1127c2c66affSColin Finck
1128c2c66affSColin Finck HRESULT wined3d_texture_check_box_dimensions(const struct wined3d_texture *texture,
1129c2c66affSColin Finck unsigned int level, const struct wined3d_box *box)
1130c2c66affSColin Finck {
1131c2c66affSColin Finck const struct wined3d_format *format = texture->resource.format;
1132c2c66affSColin Finck unsigned int width_mask, height_mask, width, height, depth;
1133c2c66affSColin Finck
1134c2c66affSColin Finck width = wined3d_texture_get_level_width(texture, level);
1135c2c66affSColin Finck height = wined3d_texture_get_level_height(texture, level);
1136c2c66affSColin Finck depth = wined3d_texture_get_level_depth(texture, level);
1137c2c66affSColin Finck
1138c2c66affSColin Finck if (box->left >= box->right || box->right > width
1139c2c66affSColin Finck || box->top >= box->bottom || box->bottom > height
1140c2c66affSColin Finck || box->front >= box->back || box->back > depth)
1141c2c66affSColin Finck {
1142c2c66affSColin Finck WARN("Box %s is invalid.\n", debug_box(box));
1143c2c66affSColin Finck return WINEDDERR_INVALIDRECT;
1144c2c66affSColin Finck }
1145c2c66affSColin Finck
1146c2c66affSColin Finck if (texture->resource.format_flags & WINED3DFMT_FLAG_BLOCKS)
1147c2c66affSColin Finck {
1148c2c66affSColin Finck /* This assumes power of two block sizes, but NPOT block sizes would
1149c2c66affSColin Finck * be silly anyway.
1150c2c66affSColin Finck *
1151c2c66affSColin Finck * This also assumes that the format's block depth is 1. */
1152c2c66affSColin Finck width_mask = format->block_width - 1;
1153c2c66affSColin Finck height_mask = format->block_height - 1;
1154c2c66affSColin Finck
1155c2c66affSColin Finck if ((box->left & width_mask) || (box->top & height_mask)
1156c2c66affSColin Finck || (box->right & width_mask && box->right != width)
1157c2c66affSColin Finck || (box->bottom & height_mask && box->bottom != height))
1158c2c66affSColin Finck {
1159c2c66affSColin Finck WARN("Box %s is misaligned for %ux%u blocks.\n",
1160c2c66affSColin Finck debug_box(box), format->block_width, format->block_height);
1161c2c66affSColin Finck return WINED3DERR_INVALIDCALL;
1162c2c66affSColin Finck }
1163c2c66affSColin Finck }
1164c2c66affSColin Finck
1165c2c66affSColin Finck return WINED3D_OK;
1166c2c66affSColin Finck }
1167c2c66affSColin Finck
1168c2c66affSColin Finck void CDECL wined3d_texture_get_pitch(const struct wined3d_texture *texture,
1169c2c66affSColin Finck unsigned int level, unsigned int *row_pitch, unsigned int *slice_pitch)
1170c2c66affSColin Finck {
1171c2c66affSColin Finck const struct wined3d_resource *resource = &texture->resource;
1172c2c66affSColin Finck unsigned int width = wined3d_texture_get_level_width(texture, level);
1173c2c66affSColin Finck unsigned int height = wined3d_texture_get_level_height(texture, level);
1174c2c66affSColin Finck
1175c2c66affSColin Finck if (texture->row_pitch)
1176c2c66affSColin Finck {
1177c2c66affSColin Finck *row_pitch = texture->row_pitch;
1178c2c66affSColin Finck *slice_pitch = texture->slice_pitch;
1179c2c66affSColin Finck return;
1180c2c66affSColin Finck }
1181c2c66affSColin Finck
1182c2c66affSColin Finck wined3d_format_calculate_pitch(resource->format, resource->device->surface_alignment,
1183c2c66affSColin Finck width, height, row_pitch, slice_pitch);
1184c2c66affSColin Finck }
1185c2c66affSColin Finck
1186c2c66affSColin Finck DWORD CDECL wined3d_texture_set_lod(struct wined3d_texture *texture, DWORD lod)
1187c2c66affSColin Finck {
1188c2c66affSColin Finck DWORD old = texture->lod;
1189c2c66affSColin Finck
1190c2c66affSColin Finck TRACE("texture %p, lod %u.\n", texture, lod);
1191c2c66affSColin Finck
1192c2c66affSColin Finck /* The d3d9:texture test shows that SetLOD is ignored on non-managed
1193c2c66affSColin Finck * textures. The call always returns 0, and GetLOD always returns 0. */
11949987f029SAmine Khaldi if (!wined3d_resource_access_is_managed(texture->resource.access))
1195c2c66affSColin Finck {
11969987f029SAmine Khaldi TRACE("Ignoring LOD on texture with resource access %s.\n",
11979987f029SAmine Khaldi wined3d_debug_resource_access(texture->resource.access));
1198c2c66affSColin Finck return 0;
1199c2c66affSColin Finck }
1200c2c66affSColin Finck
1201c2c66affSColin Finck if (lod >= texture->level_count)
1202c2c66affSColin Finck lod = texture->level_count - 1;
1203c2c66affSColin Finck
1204c2c66affSColin Finck if (texture->lod != lod)
1205c2c66affSColin Finck {
1206c2c66affSColin Finck struct wined3d_device *device = texture->resource.device;
1207c2c66affSColin Finck
1208c2c66affSColin Finck wined3d_resource_wait_idle(&texture->resource);
1209c2c66affSColin Finck texture->lod = lod;
1210c2c66affSColin Finck
1211c2c66affSColin Finck texture->texture_rgb.base_level = ~0u;
1212c2c66affSColin Finck texture->texture_srgb.base_level = ~0u;
1213c2c66affSColin Finck if (texture->resource.bind_count)
1214c2c66affSColin Finck wined3d_cs_emit_set_sampler_state(device->cs, texture->sampler, WINED3D_SAMP_MAX_MIP_LEVEL,
1215c2c66affSColin Finck device->state.sampler_states[texture->sampler][WINED3D_SAMP_MAX_MIP_LEVEL]);
1216c2c66affSColin Finck }
1217c2c66affSColin Finck
1218c2c66affSColin Finck return old;
1219c2c66affSColin Finck }
1220c2c66affSColin Finck
1221c2c66affSColin Finck DWORD CDECL wined3d_texture_get_lod(const struct wined3d_texture *texture)
1222c2c66affSColin Finck {
1223c2c66affSColin Finck TRACE("texture %p, returning %u.\n", texture, texture->lod);
1224c2c66affSColin Finck
1225c2c66affSColin Finck return texture->lod;
1226c2c66affSColin Finck }
1227c2c66affSColin Finck
1228c2c66affSColin Finck DWORD CDECL wined3d_texture_get_level_count(const struct wined3d_texture *texture)
1229c2c66affSColin Finck {
1230c2c66affSColin Finck TRACE("texture %p, returning %u.\n", texture, texture->level_count);
1231c2c66affSColin Finck
1232c2c66affSColin Finck return texture->level_count;
1233c2c66affSColin Finck }
1234c2c66affSColin Finck
1235c2c66affSColin Finck HRESULT CDECL wined3d_texture_set_color_key(struct wined3d_texture *texture,
1236c2c66affSColin Finck DWORD flags, const struct wined3d_color_key *color_key)
1237c2c66affSColin Finck {
1238c2c66affSColin Finck struct wined3d_device *device = texture->resource.device;
1239c2c66affSColin Finck static const DWORD all_flags = WINED3D_CKEY_DST_BLT | WINED3D_CKEY_DST_OVERLAY
1240c2c66affSColin Finck | WINED3D_CKEY_SRC_BLT | WINED3D_CKEY_SRC_OVERLAY;
1241c2c66affSColin Finck
1242c2c66affSColin Finck TRACE("texture %p, flags %#x, color_key %p.\n", texture, flags, color_key);
1243c2c66affSColin Finck
1244c2c66affSColin Finck if (flags & ~all_flags)
1245c2c66affSColin Finck {
1246c2c66affSColin Finck WARN("Invalid flags passed, returning WINED3DERR_INVALIDCALL.\n");
1247c2c66affSColin Finck return WINED3DERR_INVALIDCALL;
1248c2c66affSColin Finck }
1249c2c66affSColin Finck
1250c2c66affSColin Finck wined3d_cs_emit_set_color_key(device->cs, texture, flags, color_key);
1251c2c66affSColin Finck
1252c2c66affSColin Finck return WINED3D_OK;
1253c2c66affSColin Finck }
1254c2c66affSColin Finck
1255*81cffd76SJoachim Henze static void texture2d_create_dc(void *object)
1256c2c66affSColin Finck {
1257*81cffd76SJoachim Henze struct wined3d_surface *surface = object;
1258*81cffd76SJoachim Henze struct wined3d_context *context = NULL;
1259*81cffd76SJoachim Henze const struct wined3d_format *format;
1260*81cffd76SJoachim Henze unsigned int row_pitch, slice_pitch;
1261*81cffd76SJoachim Henze struct wined3d_texture *texture;
1262*81cffd76SJoachim Henze struct wined3d_bo_address data;
1263*81cffd76SJoachim Henze D3DKMT_CREATEDCFROMMEMORY desc;
1264*81cffd76SJoachim Henze unsigned int sub_resource_idx;
1265*81cffd76SJoachim Henze struct wined3d_device *device;
1266*81cffd76SJoachim Henze NTSTATUS status;
1267c2c66affSColin Finck
1268*81cffd76SJoachim Henze TRACE("surface %p.\n", surface);
1269c2c66affSColin Finck
1270*81cffd76SJoachim Henze texture = surface->container;
1271*81cffd76SJoachim Henze sub_resource_idx = surface_get_sub_resource_idx(surface);
1272*81cffd76SJoachim Henze device = texture->resource.device;
1273*81cffd76SJoachim Henze
1274*81cffd76SJoachim Henze format = texture->resource.format;
1275*81cffd76SJoachim Henze if (!format->ddi_format)
1276c2c66affSColin Finck {
1277*81cffd76SJoachim Henze WARN("Cannot create a DC for format %s.\n", debug_d3dformat(format->id));
1278c2c66affSColin Finck return;
1279c2c66affSColin Finck }
1280c2c66affSColin Finck
1281*81cffd76SJoachim Henze if (device->d3d_initialized)
1282*81cffd76SJoachim Henze context = context_acquire(device, NULL, 0);
1283*81cffd76SJoachim Henze
1284*81cffd76SJoachim Henze wined3d_texture_load_location(texture, sub_resource_idx, context, texture->resource.map_binding);
1285*81cffd76SJoachim Henze wined3d_texture_invalidate_location(texture, sub_resource_idx, ~texture->resource.map_binding);
1286*81cffd76SJoachim Henze wined3d_texture_get_pitch(texture, surface->texture_level, &row_pitch, &slice_pitch);
1287*81cffd76SJoachim Henze wined3d_texture_get_memory(texture, sub_resource_idx, &data, texture->resource.map_binding);
1288*81cffd76SJoachim Henze desc.pMemory = context_map_bo_address(context, &data,
1289*81cffd76SJoachim Henze texture->sub_resources[sub_resource_idx].size,
1290*81cffd76SJoachim Henze GL_PIXEL_UNPACK_BUFFER, WINED3D_MAP_READ | WINED3D_MAP_WRITE);
1291*81cffd76SJoachim Henze
1292*81cffd76SJoachim Henze if (context)
1293*81cffd76SJoachim Henze context_release(context);
1294*81cffd76SJoachim Henze
1295*81cffd76SJoachim Henze desc.Format = format->ddi_format;
1296*81cffd76SJoachim Henze desc.Width = wined3d_texture_get_level_width(texture, surface->texture_level);
1297*81cffd76SJoachim Henze desc.Height = wined3d_texture_get_level_height(texture, surface->texture_level);
1298*81cffd76SJoachim Henze desc.Pitch = row_pitch;
1299*81cffd76SJoachim Henze desc.hDeviceDc = CreateCompatibleDC(NULL);
1300*81cffd76SJoachim Henze desc.pColorTable = NULL;
1301*81cffd76SJoachim Henze
1302*81cffd76SJoachim Henze status = D3DKMTCreateDCFromMemory(&desc);
1303*81cffd76SJoachim Henze DeleteDC(desc.hDeviceDc);
1304*81cffd76SJoachim Henze if (status)
1305c2c66affSColin Finck {
1306*81cffd76SJoachim Henze WARN("Failed to create DC, status %#x.\n", status);
1307*81cffd76SJoachim Henze return;
1308c2c66affSColin Finck }
1309c2c66affSColin Finck
1310*81cffd76SJoachim Henze surface->dc = desc.hDc;
1311*81cffd76SJoachim Henze surface->bitmap = desc.hBitmap;
1312c2c66affSColin Finck
1313*81cffd76SJoachim Henze TRACE("Created DC %p, bitmap %p for surface %p.\n", surface->dc, surface->bitmap, surface);
1314c2c66affSColin Finck }
1315c2c66affSColin Finck
1316*81cffd76SJoachim Henze static void texture2d_destroy_dc(void *object)
1317*81cffd76SJoachim Henze {
1318*81cffd76SJoachim Henze struct wined3d_surface *surface = object;
1319*81cffd76SJoachim Henze D3DKMT_DESTROYDCFROMMEMORY destroy_desc;
1320*81cffd76SJoachim Henze struct wined3d_context *context = NULL;
1321*81cffd76SJoachim Henze struct wined3d_texture *texture;
1322*81cffd76SJoachim Henze struct wined3d_bo_address data;
1323*81cffd76SJoachim Henze unsigned int sub_resource_idx;
1324*81cffd76SJoachim Henze struct wined3d_device *device;
1325*81cffd76SJoachim Henze NTSTATUS status;
1326*81cffd76SJoachim Henze
1327*81cffd76SJoachim Henze texture = surface->container;
1328*81cffd76SJoachim Henze sub_resource_idx = surface_get_sub_resource_idx(surface);
1329*81cffd76SJoachim Henze device = texture->resource.device;
1330*81cffd76SJoachim Henze
1331*81cffd76SJoachim Henze if (!surface->dc)
1332*81cffd76SJoachim Henze {
1333*81cffd76SJoachim Henze ERR("Surface %p has no DC.\n", surface);
1334*81cffd76SJoachim Henze return;
1335*81cffd76SJoachim Henze }
1336*81cffd76SJoachim Henze
1337*81cffd76SJoachim Henze TRACE("dc %p, bitmap %p.\n", surface->dc, surface->bitmap);
1338*81cffd76SJoachim Henze
1339*81cffd76SJoachim Henze destroy_desc.hDc = surface->dc;
1340*81cffd76SJoachim Henze destroy_desc.hBitmap = surface->bitmap;
1341*81cffd76SJoachim Henze if ((status = D3DKMTDestroyDCFromMemory(&destroy_desc)))
1342*81cffd76SJoachim Henze ERR("Failed to destroy dc, status %#x.\n", status);
1343*81cffd76SJoachim Henze surface->dc = NULL;
1344*81cffd76SJoachim Henze surface->bitmap = NULL;
1345*81cffd76SJoachim Henze
1346*81cffd76SJoachim Henze if (device->d3d_initialized)
1347*81cffd76SJoachim Henze context = context_acquire(device, NULL, 0);
1348*81cffd76SJoachim Henze
1349*81cffd76SJoachim Henze wined3d_texture_get_memory(texture, sub_resource_idx, &data, texture->resource.map_binding);
1350*81cffd76SJoachim Henze context_unmap_bo_address(context, &data, GL_PIXEL_UNPACK_BUFFER);
1351*81cffd76SJoachim Henze
1352*81cffd76SJoachim Henze if (context)
1353*81cffd76SJoachim Henze context_release(context);
1354c2c66affSColin Finck }
1355c2c66affSColin Finck
1356c2c66affSColin Finck HRESULT CDECL wined3d_texture_update_desc(struct wined3d_texture *texture, UINT width, UINT height,
1357c2c66affSColin Finck enum wined3d_format_id format_id, enum wined3d_multisample_type multisample_type,
1358c2c66affSColin Finck UINT multisample_quality, void *mem, UINT pitch)
1359c2c66affSColin Finck {
1360c2c66affSColin Finck struct wined3d_device *device = texture->resource.device;
1361c2c66affSColin Finck const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
1362c2c66affSColin Finck const struct wined3d_format *format = wined3d_get_format(gl_info, format_id, texture->resource.usage);
1363c2c66affSColin Finck UINT resource_size = wined3d_format_calculate_size(format, device->surface_alignment, width, height, 1);
1364c2c66affSColin Finck struct wined3d_texture_sub_resource *sub_resource;
1365*81cffd76SJoachim Henze struct wined3d_surface *surface;
1366c2c66affSColin Finck DWORD valid_location = 0;
1367c2c66affSColin Finck BOOL create_dib = FALSE;
1368c2c66affSColin Finck
1369c2c66affSColin Finck TRACE("texture %p, width %u, height %u, format %s, multisample_type %#x, multisample_quality %u, "
1370c2c66affSColin Finck "mem %p, pitch %u.\n",
1371c2c66affSColin Finck texture, width, height, debug_d3dformat(format_id), multisample_type, multisample_quality, mem, pitch);
1372c2c66affSColin Finck
1373c2c66affSColin Finck if (!resource_size)
1374c2c66affSColin Finck return WINED3DERR_INVALIDCALL;
1375c2c66affSColin Finck
1376c2c66affSColin Finck if (texture->level_count * texture->layer_count > 1)
1377c2c66affSColin Finck {
1378c2c66affSColin Finck WARN("Texture has multiple sub-resources, not supported.\n");
1379c2c66affSColin Finck return WINED3DERR_INVALIDCALL;
1380c2c66affSColin Finck }
1381c2c66affSColin Finck
13829987f029SAmine Khaldi if (texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
1383c2c66affSColin Finck {
13849987f029SAmine Khaldi WARN("Not supported on %s.\n", debug_d3dresourcetype(texture->resource.type));
13859987f029SAmine Khaldi return WINED3DERR_INVALIDCALL;
13869987f029SAmine Khaldi }
13879987f029SAmine Khaldi
1388*81cffd76SJoachim Henze if (texture->resource.type == WINED3D_RTYPE_TEXTURE_1D)
1389*81cffd76SJoachim Henze {
1390*81cffd76SJoachim Henze FIXME("Not yet supported for 1D textures.\n");
1391*81cffd76SJoachim Henze return WINED3DERR_INVALIDCALL;
1392*81cffd76SJoachim Henze }
1393*81cffd76SJoachim Henze
1394c2c66affSColin Finck if (texture->resource.map_count)
1395c2c66affSColin Finck {
1396c2c66affSColin Finck WARN("Texture is mapped.\n");
1397c2c66affSColin Finck return WINED3DERR_INVALIDCALL;
1398c2c66affSColin Finck }
1399c2c66affSColin Finck
1400c2c66affSColin Finck /* We have no way of supporting a pitch that is not a multiple of the pixel
1401c2c66affSColin Finck * byte width short of uploading the texture row-by-row.
1402c2c66affSColin Finck * Fortunately that's not an issue since D3D9Ex doesn't allow a custom pitch
1403c2c66affSColin Finck * for user-memory textures (it always expects packed data) while DirectDraw
1404c2c66affSColin Finck * requires a 4-byte aligned pitch and doesn't support texture formats
1405c2c66affSColin Finck * larger than 4 bytes per pixel nor any format using 3 bytes per pixel.
1406c2c66affSColin Finck * This check is here to verify that the assumption holds. */
1407c2c66affSColin Finck if (pitch % texture->resource.format->byte_count)
1408c2c66affSColin Finck {
1409c2c66affSColin Finck WARN("Pitch unsupported, not a multiple of the texture format byte width.\n");
1410c2c66affSColin Finck return WINED3DERR_INVALIDCALL;
1411c2c66affSColin Finck }
1412c2c66affSColin Finck
1413c2c66affSColin Finck if (device->d3d_initialized)
1414c2c66affSColin Finck wined3d_cs_emit_unload_resource(device->cs, &texture->resource);
1415c2c66affSColin Finck wined3d_resource_wait_idle(&texture->resource);
1416c2c66affSColin Finck
1417c2c66affSColin Finck sub_resource = &texture->sub_resources[0];
1418*81cffd76SJoachim Henze surface = sub_resource->u.surface;
1419*81cffd76SJoachim Henze if (surface->dc)
1420c2c66affSColin Finck {
1421*81cffd76SJoachim Henze wined3d_cs_destroy_object(device->cs, texture2d_destroy_dc, surface);
1422c2c66affSColin Finck device->cs->ops->finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
1423c2c66affSColin Finck create_dib = TRUE;
1424c2c66affSColin Finck }
1425c2c66affSColin Finck
1426c2c66affSColin Finck wined3d_resource_free_sysmem(&texture->resource);
1427c2c66affSColin Finck
1428c2c66affSColin Finck if ((texture->row_pitch = pitch))
1429c2c66affSColin Finck texture->slice_pitch = height * pitch;
1430c2c66affSColin Finck else
1431c2c66affSColin Finck /* User memory surfaces don't have the regular surface alignment. */
1432c2c66affSColin Finck wined3d_format_calculate_pitch(format, 1, width, height,
1433c2c66affSColin Finck &texture->row_pitch, &texture->slice_pitch);
1434c2c66affSColin Finck
1435c2c66affSColin Finck texture->resource.format = format;
1436c2c66affSColin Finck texture->resource.multisample_type = multisample_type;
1437c2c66affSColin Finck texture->resource.multisample_quality = multisample_quality;
1438c2c66affSColin Finck texture->resource.width = width;
1439c2c66affSColin Finck texture->resource.height = height;
1440c2c66affSColin Finck texture->resource.size = texture->slice_pitch;
1441c2c66affSColin Finck sub_resource->size = texture->slice_pitch;
1442c2c66affSColin Finck sub_resource->locations = WINED3D_LOCATION_DISCARDED;
1443c2c66affSColin Finck
14449987f029SAmine Khaldi if (multisample_type && gl_info->supported[ARB_TEXTURE_MULTISAMPLE])
14459987f029SAmine Khaldi texture->target = GL_TEXTURE_2D_MULTISAMPLE;
14469987f029SAmine Khaldi else
14479987f029SAmine Khaldi texture->target = GL_TEXTURE_2D;
14489987f029SAmine Khaldi
1449*81cffd76SJoachim Henze if ((!is_power_of_two(width) || !is_power_of_two(height)) && !gl_info->supported[ARB_TEXTURE_NON_POWER_OF_TWO]
1450c2c66affSColin Finck && !gl_info->supported[ARB_TEXTURE_RECTANGLE] && !gl_info->supported[WINED3D_GL_NORMALIZED_TEXRECT])
1451c2c66affSColin Finck {
1452c2c66affSColin Finck texture->flags |= WINED3D_TEXTURE_COND_NP2_EMULATED;
1453c2c66affSColin Finck texture->pow2_width = texture->pow2_height = 1;
1454c2c66affSColin Finck while (texture->pow2_width < width)
1455c2c66affSColin Finck texture->pow2_width <<= 1;
1456c2c66affSColin Finck while (texture->pow2_height < height)
1457c2c66affSColin Finck texture->pow2_height <<= 1;
1458c2c66affSColin Finck }
1459c2c66affSColin Finck else
1460c2c66affSColin Finck {
1461c2c66affSColin Finck texture->flags &= ~WINED3D_TEXTURE_COND_NP2_EMULATED;
1462c2c66affSColin Finck texture->pow2_width = width;
1463c2c66affSColin Finck texture->pow2_height = height;
1464c2c66affSColin Finck }
1465c2c66affSColin Finck
1466c2c66affSColin Finck if ((texture->user_memory = mem))
1467c2c66affSColin Finck {
1468c2c66affSColin Finck texture->resource.map_binding = WINED3D_LOCATION_USER_MEMORY;
1469c2c66affSColin Finck valid_location = WINED3D_LOCATION_USER_MEMORY;
1470c2c66affSColin Finck }
1471c2c66affSColin Finck else
1472c2c66affSColin Finck {
1473c2c66affSColin Finck wined3d_texture_prepare_location(texture, 0, NULL, WINED3D_LOCATION_SYSMEM);
1474c2c66affSColin Finck valid_location = WINED3D_LOCATION_SYSMEM;
1475c2c66affSColin Finck }
1476c2c66affSColin Finck
1477c2c66affSColin Finck /* The format might be changed to a format that needs conversion.
1478c2c66affSColin Finck * If the surface didn't use PBOs previously but could now, don't
1479c2c66affSColin Finck * change it - whatever made us not use PBOs might come back, e.g.
1480c2c66affSColin Finck * color keys. */
1481c2c66affSColin Finck if (texture->resource.map_binding == WINED3D_LOCATION_BUFFER && !wined3d_texture_use_pbo(texture, gl_info))
1482c2c66affSColin Finck texture->resource.map_binding = WINED3D_LOCATION_SYSMEM;
1483c2c66affSColin Finck
1484c2c66affSColin Finck wined3d_texture_validate_location(texture, 0, valid_location);
1485c2c66affSColin Finck wined3d_texture_invalidate_location(texture, 0, ~valid_location);
1486c2c66affSColin Finck
1487c2c66affSColin Finck if (create_dib)
1488c2c66affSColin Finck {
1489*81cffd76SJoachim Henze wined3d_cs_init_object(device->cs, texture2d_create_dc, surface);
1490c2c66affSColin Finck device->cs->ops->finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
1491c2c66affSColin Finck }
1492c2c66affSColin Finck
1493c2c66affSColin Finck return WINED3D_OK;
1494c2c66affSColin Finck }
1495c2c66affSColin Finck
1496c2c66affSColin Finck /* Context activation is done by the caller. */
1497c2c66affSColin Finck static void wined3d_texture_prepare_buffer_object(struct wined3d_texture *texture,
1498*81cffd76SJoachim Henze #if !defined(STAGING_CSMT)
1499c2c66affSColin Finck unsigned int sub_resource_idx, const struct wined3d_gl_info *gl_info)
1500*81cffd76SJoachim Henze #else /* STAGING_CSMT */
1501*81cffd76SJoachim Henze unsigned int sub_resource_idx, struct wined3d_context *context)
1502*81cffd76SJoachim Henze #endif /* STAGING_CSMT */
1503c2c66affSColin Finck {
1504c2c66affSColin Finck struct wined3d_texture_sub_resource *sub_resource;
1505c2c66affSColin Finck
1506c2c66affSColin Finck sub_resource = &texture->sub_resources[sub_resource_idx];
1507*81cffd76SJoachim Henze #if !defined(STAGING_CSMT)
1508c2c66affSColin Finck if (sub_resource->buffer_object)
1509c2c66affSColin Finck return;
1510c2c66affSColin Finck
1511c2c66affSColin Finck GL_EXTCALL(glGenBuffers(1, &sub_resource->buffer_object));
1512c2c66affSColin Finck GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, sub_resource->buffer_object));
1513c2c66affSColin Finck GL_EXTCALL(glBufferData(GL_PIXEL_UNPACK_BUFFER, sub_resource->size, NULL, GL_STREAM_DRAW));
1514c2c66affSColin Finck GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0));
1515c2c66affSColin Finck checkGLcall("Create buffer object");
1516c2c66affSColin Finck
1517c2c66affSColin Finck TRACE("Created buffer object %u for texture %p, sub-resource %u.\n",
1518c2c66affSColin Finck sub_resource->buffer_object, texture, sub_resource_idx);
1519*81cffd76SJoachim Henze #else /* STAGING_CSMT */
1520*81cffd76SJoachim Henze if (sub_resource->buffer)
1521*81cffd76SJoachim Henze return;
1522*81cffd76SJoachim Henze
1523*81cffd76SJoachim Henze sub_resource->buffer = wined3d_device_get_bo(texture->resource.device,
1524*81cffd76SJoachim Henze sub_resource->size, GL_STREAM_DRAW, GL_PIXEL_UNPACK_BUFFER, context);
1525*81cffd76SJoachim Henze
1526*81cffd76SJoachim Henze TRACE("Created buffer object %u for texture %p, sub-resource %u.\n",
1527*81cffd76SJoachim Henze sub_resource->buffer->name, texture, sub_resource_idx);
1528*81cffd76SJoachim Henze #endif /* STAGING_CSMT */
1529c2c66affSColin Finck }
1530c2c66affSColin Finck
1531c2c66affSColin Finck static void wined3d_texture_force_reload(struct wined3d_texture *texture)
1532c2c66affSColin Finck {
1533c2c66affSColin Finck unsigned int sub_count = texture->level_count * texture->layer_count;
1534c2c66affSColin Finck unsigned int i;
1535c2c66affSColin Finck
1536c2c66affSColin Finck texture->flags &= ~(WINED3D_TEXTURE_RGB_ALLOCATED | WINED3D_TEXTURE_SRGB_ALLOCATED
1537c2c66affSColin Finck | WINED3D_TEXTURE_CONVERTED);
1538c2c66affSColin Finck texture->async.flags &= ~WINED3D_TEXTURE_ASYNC_COLOR_KEY;
1539c2c66affSColin Finck for (i = 0; i < sub_count; ++i)
1540c2c66affSColin Finck {
1541c2c66affSColin Finck wined3d_texture_invalidate_location(texture, i,
1542c2c66affSColin Finck WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB);
1543c2c66affSColin Finck }
1544c2c66affSColin Finck }
1545c2c66affSColin Finck
1546c2c66affSColin Finck void wined3d_texture_prepare_texture(struct wined3d_texture *texture, struct wined3d_context *context, BOOL srgb)
1547c2c66affSColin Finck {
1548c2c66affSColin Finck DWORD alloc_flag = srgb ? WINED3D_TEXTURE_SRGB_ALLOCATED : WINED3D_TEXTURE_RGB_ALLOCATED;
1549c2c66affSColin Finck const struct wined3d_d3d_info *d3d_info = context->d3d_info;
1550c2c66affSColin Finck
1551c2c66affSColin Finck if (!d3d_info->shader_color_key
1552c2c66affSColin Finck && !(texture->async.flags & WINED3D_TEXTURE_ASYNC_COLOR_KEY)
1553c2c66affSColin Finck != !(texture->async.color_key_flags & WINED3D_CKEY_SRC_BLT))
1554c2c66affSColin Finck {
1555c2c66affSColin Finck wined3d_texture_force_reload(texture);
1556c2c66affSColin Finck
1557c2c66affSColin Finck if (texture->async.color_key_flags & WINED3D_CKEY_SRC_BLT)
1558c2c66affSColin Finck texture->async.flags |= WINED3D_TEXTURE_ASYNC_COLOR_KEY;
1559c2c66affSColin Finck }
1560c2c66affSColin Finck
1561c2c66affSColin Finck if (texture->flags & alloc_flag)
1562c2c66affSColin Finck return;
1563c2c66affSColin Finck
1564*81cffd76SJoachim Henze texture->texture_ops->texture_prepare_texture(texture, context, srgb);
1565c2c66affSColin Finck texture->flags |= alloc_flag;
1566c2c66affSColin Finck }
1567c2c66affSColin Finck
1568c2c66affSColin Finck static void wined3d_texture_prepare_rb(struct wined3d_texture *texture,
1569c2c66affSColin Finck const struct wined3d_gl_info *gl_info, BOOL multisample)
1570c2c66affSColin Finck {
1571c2c66affSColin Finck const struct wined3d_format *format = texture->resource.format;
1572c2c66affSColin Finck
1573c2c66affSColin Finck if (multisample)
1574c2c66affSColin Finck {
1575c2c66affSColin Finck DWORD samples;
1576c2c66affSColin Finck
1577c2c66affSColin Finck if (texture->rb_multisample)
1578c2c66affSColin Finck return;
1579c2c66affSColin Finck
15809987f029SAmine Khaldi samples = wined3d_texture_get_gl_sample_count(texture);
1581c2c66affSColin Finck
1582c2c66affSColin Finck gl_info->fbo_ops.glGenRenderbuffers(1, &texture->rb_multisample);
1583c2c66affSColin Finck gl_info->fbo_ops.glBindRenderbuffer(GL_RENDERBUFFER, texture->rb_multisample);
1584c2c66affSColin Finck gl_info->fbo_ops.glRenderbufferStorageMultisample(GL_RENDERBUFFER, samples,
1585c2c66affSColin Finck format->glInternal, texture->resource.width, texture->resource.height);
1586c2c66affSColin Finck checkGLcall("glRenderbufferStorageMultisample()");
1587c2c66affSColin Finck TRACE("Created multisample rb %u.\n", texture->rb_multisample);
1588c2c66affSColin Finck }
1589c2c66affSColin Finck else
1590c2c66affSColin Finck {
1591c2c66affSColin Finck if (texture->rb_resolved)
1592c2c66affSColin Finck return;
1593c2c66affSColin Finck
1594c2c66affSColin Finck gl_info->fbo_ops.glGenRenderbuffers(1, &texture->rb_resolved);
1595c2c66affSColin Finck gl_info->fbo_ops.glBindRenderbuffer(GL_RENDERBUFFER, texture->rb_resolved);
1596c2c66affSColin Finck gl_info->fbo_ops.glRenderbufferStorage(GL_RENDERBUFFER, format->glInternal,
1597c2c66affSColin Finck texture->resource.width, texture->resource.height);
1598c2c66affSColin Finck checkGLcall("glRenderbufferStorage()");
1599c2c66affSColin Finck TRACE("Created resolved rb %u.\n", texture->rb_resolved);
1600c2c66affSColin Finck }
1601c2c66affSColin Finck }
1602c2c66affSColin Finck
1603c2c66affSColin Finck /* Context activation is done by the caller. Context may be NULL in
1604c2c66affSColin Finck * WINED3D_NO3D mode. */
1605c2c66affSColin Finck BOOL wined3d_texture_prepare_location(struct wined3d_texture *texture, unsigned int sub_resource_idx,
1606c2c66affSColin Finck struct wined3d_context *context, DWORD location)
1607c2c66affSColin Finck {
1608c2c66affSColin Finck switch (location)
1609c2c66affSColin Finck {
1610c2c66affSColin Finck case WINED3D_LOCATION_SYSMEM:
1611c2c66affSColin Finck if (texture->resource.heap_memory)
1612c2c66affSColin Finck return TRUE;
1613c2c66affSColin Finck
1614c2c66affSColin Finck if (!wined3d_resource_allocate_sysmem(&texture->resource))
1615*81cffd76SJoachim Henze {
1616*81cffd76SJoachim Henze ERR("Failed to allocate system memory.\n");
1617c2c66affSColin Finck return FALSE;
1618*81cffd76SJoachim Henze }
1619c2c66affSColin Finck return TRUE;
1620c2c66affSColin Finck
1621c2c66affSColin Finck case WINED3D_LOCATION_USER_MEMORY:
1622c2c66affSColin Finck if (!texture->user_memory)
1623c2c66affSColin Finck ERR("Map binding is set to WINED3D_LOCATION_USER_MEMORY but surface->user_memory is NULL.\n");
1624c2c66affSColin Finck return TRUE;
1625c2c66affSColin Finck
1626c2c66affSColin Finck case WINED3D_LOCATION_BUFFER:
1627*81cffd76SJoachim Henze #if !defined(STAGING_CSMT)
1628c2c66affSColin Finck wined3d_texture_prepare_buffer_object(texture, sub_resource_idx, context->gl_info);
1629*81cffd76SJoachim Henze #else /* STAGING_CSMT */
1630*81cffd76SJoachim Henze wined3d_texture_prepare_buffer_object(texture, sub_resource_idx, context);
1631*81cffd76SJoachim Henze #endif /* STAGING_CSMT */
1632c2c66affSColin Finck return TRUE;
1633c2c66affSColin Finck
1634c2c66affSColin Finck case WINED3D_LOCATION_TEXTURE_RGB:
1635c2c66affSColin Finck wined3d_texture_prepare_texture(texture, context, FALSE);
1636c2c66affSColin Finck return TRUE;
1637c2c66affSColin Finck
1638c2c66affSColin Finck case WINED3D_LOCATION_TEXTURE_SRGB:
1639c2c66affSColin Finck wined3d_texture_prepare_texture(texture, context, TRUE);
1640c2c66affSColin Finck return TRUE;
1641c2c66affSColin Finck
1642c2c66affSColin Finck case WINED3D_LOCATION_DRAWABLE:
1643c2c66affSColin Finck if (!texture->swapchain && wined3d_settings.offscreen_rendering_mode != ORM_BACKBUFFER)
1644c2c66affSColin Finck ERR("Texture %p does not have a drawable.\n", texture);
1645c2c66affSColin Finck return TRUE;
1646c2c66affSColin Finck
1647c2c66affSColin Finck case WINED3D_LOCATION_RB_MULTISAMPLE:
1648c2c66affSColin Finck wined3d_texture_prepare_rb(texture, context->gl_info, TRUE);
1649c2c66affSColin Finck return TRUE;
1650c2c66affSColin Finck
1651c2c66affSColin Finck case WINED3D_LOCATION_RB_RESOLVED:
1652c2c66affSColin Finck wined3d_texture_prepare_rb(texture, context->gl_info, FALSE);
1653c2c66affSColin Finck return TRUE;
1654c2c66affSColin Finck
1655c2c66affSColin Finck default:
1656c2c66affSColin Finck ERR("Invalid location %s.\n", wined3d_debug_location(location));
1657c2c66affSColin Finck return FALSE;
1658c2c66affSColin Finck }
1659c2c66affSColin Finck }
1660c2c66affSColin Finck
1661c2c66affSColin Finck static struct wined3d_texture_sub_resource *wined3d_texture_get_sub_resource(struct wined3d_texture *texture,
1662c2c66affSColin Finck unsigned int sub_resource_idx)
1663c2c66affSColin Finck {
1664c2c66affSColin Finck UINT sub_count = texture->level_count * texture->layer_count;
1665c2c66affSColin Finck
1666c2c66affSColin Finck TRACE("texture %p, sub_resource_idx %u.\n", texture, sub_resource_idx);
1667c2c66affSColin Finck
1668c2c66affSColin Finck if (sub_resource_idx >= sub_count)
1669c2c66affSColin Finck {
1670c2c66affSColin Finck WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx, sub_count);
1671c2c66affSColin Finck return NULL;
1672c2c66affSColin Finck }
1673c2c66affSColin Finck
1674c2c66affSColin Finck return &texture->sub_resources[sub_resource_idx];
1675c2c66affSColin Finck }
1676c2c66affSColin Finck
1677c2c66affSColin Finck HRESULT CDECL wined3d_texture_add_dirty_region(struct wined3d_texture *texture,
1678c2c66affSColin Finck UINT layer, const struct wined3d_box *dirty_region)
1679c2c66affSColin Finck {
1680c2c66affSColin Finck TRACE("texture %p, layer %u, dirty_region %s.\n", texture, layer, debug_box(dirty_region));
1681c2c66affSColin Finck
1682c2c66affSColin Finck if (layer >= texture->layer_count)
1683c2c66affSColin Finck {
1684c2c66affSColin Finck WARN("Invalid layer %u specified.\n", layer);
1685c2c66affSColin Finck return WINED3DERR_INVALIDCALL;
1686c2c66affSColin Finck }
1687c2c66affSColin Finck
1688c2c66affSColin Finck if (dirty_region)
16899987f029SAmine Khaldi WARN("Ignoring dirty_region %s.\n", debug_box(dirty_region));
1690c2c66affSColin Finck
1691c2c66affSColin Finck wined3d_cs_emit_add_dirty_texture_region(texture->resource.device->cs, texture, layer);
1692c2c66affSColin Finck
1693c2c66affSColin Finck return WINED3D_OK;
1694c2c66affSColin Finck }
1695c2c66affSColin Finck
1696*81cffd76SJoachim Henze void wined3d_texture_upload_data(struct wined3d_texture *texture, unsigned int sub_resource_idx,
1697*81cffd76SJoachim Henze const struct wined3d_context *context, const struct wined3d_box *box,
1698*81cffd76SJoachim Henze const struct wined3d_const_bo_address *data, unsigned int row_pitch, unsigned int slice_pitch)
1699*81cffd76SJoachim Henze {
1700*81cffd76SJoachim Henze texture->texture_ops->texture_upload_data(texture, sub_resource_idx,
1701*81cffd76SJoachim Henze context, box, data, row_pitch, slice_pitch);
1702*81cffd76SJoachim Henze }
1703*81cffd76SJoachim Henze
1704*81cffd76SJoachim Henze
17059987f029SAmine Khaldi /* This call just uploads data, the caller is responsible for binding the
17069987f029SAmine Khaldi * correct texture. */
17079987f029SAmine Khaldi /* Context activation is done by the caller. */
1708*81cffd76SJoachim Henze static void texture1d_upload_data(struct wined3d_texture *texture, unsigned int sub_resource_idx,
1709*81cffd76SJoachim Henze const struct wined3d_context *context, const struct wined3d_box *box, const struct wined3d_const_bo_address *data,
1710*81cffd76SJoachim Henze unsigned int row_pitch, unsigned int slice_pitch)
17119987f029SAmine Khaldi {
1712*81cffd76SJoachim Henze struct wined3d_surface *surface = texture->sub_resources[sub_resource_idx].u.surface;
1713*81cffd76SJoachim Henze const struct wined3d_format *format = texture->resource.format;
1714*81cffd76SJoachim Henze unsigned int level = sub_resource_idx % texture->level_count;
17159987f029SAmine Khaldi const struct wined3d_gl_info *gl_info = context->gl_info;
1716*81cffd76SJoachim Henze const void *mem = data->addr;
17179987f029SAmine Khaldi void *converted_mem = NULL;
1718*81cffd76SJoachim Henze unsigned int width, x, update_w;
17199987f029SAmine Khaldi GLenum target;
17209987f029SAmine Khaldi
1721*81cffd76SJoachim Henze TRACE("texture %p, sub_resource_idx %u, context %p, box %p, data {%#x:%p}, row_pitch %#x, slice_pitch %#x.\n",
1722*81cffd76SJoachim Henze texture, sub_resource_idx, context, box, data->buffer_object, data->addr, row_pitch, slice_pitch);
17239987f029SAmine Khaldi
1724*81cffd76SJoachim Henze width = wined3d_texture_get_level_width(texture, level);
1725*81cffd76SJoachim Henze
1726*81cffd76SJoachim Henze if (!box)
17279987f029SAmine Khaldi {
1728*81cffd76SJoachim Henze x = 0;
1729*81cffd76SJoachim Henze update_w = width;
17309987f029SAmine Khaldi }
17319987f029SAmine Khaldi else
17329987f029SAmine Khaldi {
1733*81cffd76SJoachim Henze x = box->left;
1734*81cffd76SJoachim Henze update_w = box->right - box->left;
17359987f029SAmine Khaldi }
17369987f029SAmine Khaldi
17379987f029SAmine Khaldi if (format->upload)
17389987f029SAmine Khaldi {
1739*81cffd76SJoachim Henze unsigned int dst_row_pitch;
17409987f029SAmine Khaldi
1741*81cffd76SJoachim Henze if (data->buffer_object)
1742*81cffd76SJoachim Henze ERR("Loading a converted texture from a PBO.\n");
17439987f029SAmine Khaldi if (texture->resource.format_flags & WINED3DFMT_FLAG_BLOCKS)
17449987f029SAmine Khaldi ERR("Converting a block-based format.\n");
17459987f029SAmine Khaldi
1746*81cffd76SJoachim Henze dst_row_pitch = update_w * format->conv_byte_count;
17479987f029SAmine Khaldi
1748*81cffd76SJoachim Henze converted_mem = HeapAlloc(GetProcessHeap(), 0, dst_row_pitch);
1749*81cffd76SJoachim Henze format->upload(data->addr, converted_mem, row_pitch, slice_pitch, dst_row_pitch, dst_row_pitch, update_w, 1, 1);
1750*81cffd76SJoachim Henze mem = converted_mem;
17519987f029SAmine Khaldi }
17529987f029SAmine Khaldi
1753*81cffd76SJoachim Henze if (data->buffer_object)
17549987f029SAmine Khaldi {
1755*81cffd76SJoachim Henze GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, data->buffer_object));
17569987f029SAmine Khaldi checkGLcall("glBindBuffer");
17579987f029SAmine Khaldi }
17589987f029SAmine Khaldi
1759*81cffd76SJoachim Henze target = wined3d_texture_get_sub_resource_target(texture, sub_resource_idx);
1760*81cffd76SJoachim Henze if (target == GL_TEXTURE_1D_ARRAY)
17619987f029SAmine Khaldi {
1762*81cffd76SJoachim Henze gl_info->gl_ops.gl.p_glPixelStorei(GL_UNPACK_ROW_LENGTH, row_pitch / format->byte_count);
1763*81cffd76SJoachim Henze
1764*81cffd76SJoachim Henze gl_info->gl_ops.gl.p_glTexSubImage2D(target, level, x, surface->texture_layer, update_w, 1, format->glFormat, format->glType, mem);
1765*81cffd76SJoachim Henze checkGLcall("glTexSubImage2D");
1766*81cffd76SJoachim Henze
1767*81cffd76SJoachim Henze gl_info->gl_ops.gl.p_glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
1768*81cffd76SJoachim Henze }
1769*81cffd76SJoachim Henze else
1770*81cffd76SJoachim Henze {
1771*81cffd76SJoachim Henze gl_info->gl_ops.gl.p_glTexSubImage1D(target, level, x, update_w, format->glFormat, format->glType, mem);
1772*81cffd76SJoachim Henze checkGLcall("glTexSubImage1D");
1773*81cffd76SJoachim Henze }
1774*81cffd76SJoachim Henze
1775*81cffd76SJoachim Henze if (data->buffer_object)
1776*81cffd76SJoachim Henze {
1777*81cffd76SJoachim Henze GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0));
1778*81cffd76SJoachim Henze checkGLcall("glBindBuffer");
1779*81cffd76SJoachim Henze }
1780*81cffd76SJoachim Henze
1781*81cffd76SJoachim Henze HeapFree(GetProcessHeap(), 0, converted_mem);
1782*81cffd76SJoachim Henze }
1783*81cffd76SJoachim Henze
1784*81cffd76SJoachim Henze /* Context activation is done by the caller. */
1785*81cffd76SJoachim Henze static void texture1d_download_data(struct wined3d_texture *texture, unsigned int sub_resource_idx,
1786*81cffd76SJoachim Henze const struct wined3d_context *context, const struct wined3d_bo_address *data)
1787*81cffd76SJoachim Henze {
1788*81cffd76SJoachim Henze struct wined3d_surface *surface = texture->sub_resources[sub_resource_idx].u.surface;
1789*81cffd76SJoachim Henze const struct wined3d_format *format = texture->resource.format;
1790*81cffd76SJoachim Henze const struct wined3d_gl_info *gl_info = context->gl_info;
1791*81cffd76SJoachim Henze struct wined3d_texture_sub_resource *sub_resource;
1792*81cffd76SJoachim Henze BYTE *temporary_mem = NULL;
1793*81cffd76SJoachim Henze void *mem;
1794*81cffd76SJoachim Henze GLenum target;
1795*81cffd76SJoachim Henze
1796*81cffd76SJoachim Henze sub_resource = &texture->sub_resources[sub_resource_idx];
1797*81cffd76SJoachim Henze
1798*81cffd76SJoachim Henze if (format->conv_byte_count)
1799*81cffd76SJoachim Henze {
1800*81cffd76SJoachim Henze FIXME("Attempting to download a converted 1d texture, format %s.\n",
1801*81cffd76SJoachim Henze debug_d3dformat(format->id));
1802*81cffd76SJoachim Henze return;
1803*81cffd76SJoachim Henze }
1804*81cffd76SJoachim Henze
1805*81cffd76SJoachim Henze target = wined3d_texture_get_sub_resource_target(texture, sub_resource_idx);
1806*81cffd76SJoachim Henze if (target == GL_TEXTURE_1D_ARRAY)
1807*81cffd76SJoachim Henze {
1808*81cffd76SJoachim Henze WARN_(d3d_perf)("Downloading all miplevel layers to get the surface data for a single sub-resource.\n");
1809*81cffd76SJoachim Henze
1810*81cffd76SJoachim Henze if (!(temporary_mem = heap_calloc(texture->layer_count, sub_resource->size)))
1811*81cffd76SJoachim Henze {
1812*81cffd76SJoachim Henze ERR("Out of memory.\n");
1813*81cffd76SJoachim Henze return;
1814*81cffd76SJoachim Henze }
1815*81cffd76SJoachim Henze
1816*81cffd76SJoachim Henze mem = temporary_mem;
1817*81cffd76SJoachim Henze }
1818*81cffd76SJoachim Henze else if (data->buffer_object)
1819*81cffd76SJoachim Henze {
1820*81cffd76SJoachim Henze GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, data->buffer_object));
1821*81cffd76SJoachim Henze checkGLcall("glBindBuffer");
1822*81cffd76SJoachim Henze mem = data->addr;
1823*81cffd76SJoachim Henze }
1824*81cffd76SJoachim Henze else
1825*81cffd76SJoachim Henze mem = data->addr;
1826*81cffd76SJoachim Henze
1827*81cffd76SJoachim Henze gl_info->gl_ops.gl.p_glGetTexImage(target, sub_resource_idx,
1828*81cffd76SJoachim Henze format->glFormat, format->glType, mem);
1829*81cffd76SJoachim Henze checkGLcall("glGetTexImage");
1830*81cffd76SJoachim Henze
1831*81cffd76SJoachim Henze if (temporary_mem)
1832*81cffd76SJoachim Henze {
1833*81cffd76SJoachim Henze void *src_data = temporary_mem + surface->texture_layer * sub_resource->size;
1834*81cffd76SJoachim Henze if (data->buffer_object)
1835*81cffd76SJoachim Henze {
1836*81cffd76SJoachim Henze GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, data->buffer_object));
1837*81cffd76SJoachim Henze checkGLcall("glBindBuffer");
1838*81cffd76SJoachim Henze GL_EXTCALL(glBufferSubData(GL_PIXEL_PACK_BUFFER, 0, sub_resource->size, src_data));
1839*81cffd76SJoachim Henze checkGLcall("glBufferSubData");
1840*81cffd76SJoachim Henze }
1841*81cffd76SJoachim Henze else
1842*81cffd76SJoachim Henze {
1843*81cffd76SJoachim Henze memcpy(data->addr, src_data, sub_resource->size);
1844*81cffd76SJoachim Henze }
1845*81cffd76SJoachim Henze }
1846*81cffd76SJoachim Henze
1847*81cffd76SJoachim Henze if (data->buffer_object)
1848*81cffd76SJoachim Henze {
1849*81cffd76SJoachim Henze GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, 0));
1850*81cffd76SJoachim Henze checkGLcall("glBindBuffer");
1851*81cffd76SJoachim Henze }
1852*81cffd76SJoachim Henze
1853*81cffd76SJoachim Henze HeapFree(GetProcessHeap(), 0, temporary_mem);
1854*81cffd76SJoachim Henze }
1855*81cffd76SJoachim Henze
1856*81cffd76SJoachim Henze /* Context activation is done by the caller. */
1857*81cffd76SJoachim Henze static void texture1d_srgb_transfer(struct wined3d_texture *texture, unsigned int sub_resource_idx,
1858*81cffd76SJoachim Henze struct wined3d_context *context, BOOL dest_is_srgb)
1859*81cffd76SJoachim Henze {
1860*81cffd76SJoachim Henze struct wined3d_texture_sub_resource *sub_resource = &texture->sub_resources[sub_resource_idx];
1861*81cffd76SJoachim Henze unsigned int row_pitch, slice_pitch;
1862*81cffd76SJoachim Henze struct wined3d_bo_address data;
1863*81cffd76SJoachim Henze
1864*81cffd76SJoachim Henze WARN_(d3d_perf)("Performing slow rgb/srgb 1d texture transfer.\n");
1865*81cffd76SJoachim Henze data.buffer_object = 0;
1866*81cffd76SJoachim Henze if (!(data.addr = HeapAlloc(GetProcessHeap(), 0, sub_resource->size)))
1867*81cffd76SJoachim Henze return;
1868*81cffd76SJoachim Henze
1869*81cffd76SJoachim Henze wined3d_texture_get_pitch(texture, sub_resource_idx, &row_pitch, &slice_pitch);
1870*81cffd76SJoachim Henze wined3d_texture_bind_and_dirtify(texture, context, !dest_is_srgb);
1871*81cffd76SJoachim Henze texture1d_download_data(texture, sub_resource_idx, context, &data);
1872*81cffd76SJoachim Henze wined3d_texture_bind_and_dirtify(texture, context, dest_is_srgb);
1873*81cffd76SJoachim Henze texture1d_upload_data(texture, sub_resource_idx, context, NULL,
1874*81cffd76SJoachim Henze wined3d_const_bo_address(&data), row_pitch, slice_pitch);
1875*81cffd76SJoachim Henze
1876*81cffd76SJoachim Henze HeapFree(GetProcessHeap(), 0, data.addr);
1877*81cffd76SJoachim Henze }
1878*81cffd76SJoachim Henze
1879*81cffd76SJoachim Henze /* Context activation is done by the caller. */
1880*81cffd76SJoachim Henze static BOOL texture1d_load_location(struct wined3d_texture *texture, unsigned int sub_resource_idx,
1881*81cffd76SJoachim Henze struct wined3d_context *context, DWORD location)
1882*81cffd76SJoachim Henze {
1883*81cffd76SJoachim Henze struct wined3d_texture_sub_resource *sub_resource = &texture->sub_resources[sub_resource_idx];
1884*81cffd76SJoachim Henze DWORD required_access = wined3d_resource_access_from_location(location);
1885*81cffd76SJoachim Henze unsigned int row_pitch, slice_pitch;
1886*81cffd76SJoachim Henze
1887*81cffd76SJoachim Henze TRACE("texture %p, sub_resource_idx %u, context %p, location %s.\n",
1888*81cffd76SJoachim Henze texture, sub_resource_idx, context, wined3d_debug_location(location));
1889*81cffd76SJoachim Henze
1890*81cffd76SJoachim Henze TRACE("Current resource location %s.\n", wined3d_debug_location(sub_resource->locations));
1891*81cffd76SJoachim Henze
1892*81cffd76SJoachim Henze if ((sub_resource->locations & location) == location)
1893*81cffd76SJoachim Henze {
1894*81cffd76SJoachim Henze TRACE("Location(s) already up to date.\n");
1895*81cffd76SJoachim Henze return TRUE;
1896*81cffd76SJoachim Henze }
1897*81cffd76SJoachim Henze
1898*81cffd76SJoachim Henze if ((texture->resource.access & required_access) != required_access)
1899*81cffd76SJoachim Henze {
1900*81cffd76SJoachim Henze ERR("Operation requires %#x access, but 1d texture only has %#x.\n",
1901*81cffd76SJoachim Henze required_access, texture->resource.access);
1902*81cffd76SJoachim Henze return FALSE;
1903*81cffd76SJoachim Henze }
1904*81cffd76SJoachim Henze
1905*81cffd76SJoachim Henze if (!wined3d_texture_prepare_location(texture, sub_resource_idx, context, location))
1906*81cffd76SJoachim Henze return FALSE;
1907*81cffd76SJoachim Henze
1908*81cffd76SJoachim Henze if (sub_resource->locations & WINED3D_LOCATION_DISCARDED)
1909*81cffd76SJoachim Henze {
1910*81cffd76SJoachim Henze TRACE("1d texture previously discarded, nothing to do.\n");
1911*81cffd76SJoachim Henze wined3d_texture_validate_location(texture, sub_resource_idx, location);
1912*81cffd76SJoachim Henze wined3d_texture_invalidate_location(texture, sub_resource_idx, WINED3D_LOCATION_DISCARDED);
1913*81cffd76SJoachim Henze goto done;
1914*81cffd76SJoachim Henze }
1915*81cffd76SJoachim Henze
1916*81cffd76SJoachim Henze switch (location)
1917*81cffd76SJoachim Henze {
1918*81cffd76SJoachim Henze case WINED3D_LOCATION_TEXTURE_RGB:
1919*81cffd76SJoachim Henze case WINED3D_LOCATION_TEXTURE_SRGB:
1920*81cffd76SJoachim Henze if (sub_resource->locations & WINED3D_LOCATION_SYSMEM)
1921*81cffd76SJoachim Henze {
1922*81cffd76SJoachim Henze struct wined3d_const_bo_address data = {0, texture->resource.heap_memory};
1923*81cffd76SJoachim Henze data.addr += sub_resource->offset;
1924*81cffd76SJoachim Henze wined3d_texture_bind_and_dirtify(texture, context, location == WINED3D_LOCATION_TEXTURE_SRGB);
1925*81cffd76SJoachim Henze wined3d_texture_get_pitch(texture, sub_resource_idx, &row_pitch, &slice_pitch);
1926*81cffd76SJoachim Henze texture1d_upload_data(texture, sub_resource_idx, context, NULL, &data, row_pitch, slice_pitch);
1927*81cffd76SJoachim Henze }
1928*81cffd76SJoachim Henze else if (sub_resource->locations & WINED3D_LOCATION_BUFFER)
1929*81cffd76SJoachim Henze {
1930*81cffd76SJoachim Henze #if !defined(STAGING_CSMT)
1931*81cffd76SJoachim Henze struct wined3d_const_bo_address data = {sub_resource->buffer_object, NULL};
1932*81cffd76SJoachim Henze #else /* STAGING_CSMT */
1933*81cffd76SJoachim Henze struct wined3d_const_bo_address data = {sub_resource->buffer->name, NULL};
1934*81cffd76SJoachim Henze #endif /* STAGING_CSMT */
1935*81cffd76SJoachim Henze wined3d_texture_bind_and_dirtify(texture, context, location == WINED3D_LOCATION_TEXTURE_SRGB);
1936*81cffd76SJoachim Henze wined3d_texture_get_pitch(texture, sub_resource_idx, &row_pitch, &slice_pitch);
1937*81cffd76SJoachim Henze texture1d_upload_data(texture, sub_resource_idx, context, NULL, &data, row_pitch, slice_pitch);
1938*81cffd76SJoachim Henze }
1939*81cffd76SJoachim Henze else if (sub_resource->locations & WINED3D_LOCATION_TEXTURE_RGB)
1940*81cffd76SJoachim Henze {
1941*81cffd76SJoachim Henze texture1d_srgb_transfer(texture, sub_resource_idx, context, TRUE);
1942*81cffd76SJoachim Henze }
1943*81cffd76SJoachim Henze else if (sub_resource->locations & WINED3D_LOCATION_TEXTURE_SRGB)
1944*81cffd76SJoachim Henze {
1945*81cffd76SJoachim Henze texture1d_srgb_transfer(texture, sub_resource_idx, context, FALSE);
1946*81cffd76SJoachim Henze }
1947*81cffd76SJoachim Henze else
1948*81cffd76SJoachim Henze {
1949*81cffd76SJoachim Henze FIXME("Implement 1d texture loading from %s.\n", wined3d_debug_location(sub_resource->locations));
1950*81cffd76SJoachim Henze return FALSE;
1951*81cffd76SJoachim Henze }
1952*81cffd76SJoachim Henze break;
1953*81cffd76SJoachim Henze
1954*81cffd76SJoachim Henze case WINED3D_LOCATION_SYSMEM:
1955*81cffd76SJoachim Henze if (sub_resource->locations & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB))
1956*81cffd76SJoachim Henze {
1957*81cffd76SJoachim Henze struct wined3d_bo_address data = {0, texture->resource.heap_memory};
1958*81cffd76SJoachim Henze
1959*81cffd76SJoachim Henze data.addr += sub_resource->offset;
1960*81cffd76SJoachim Henze if (sub_resource->locations & WINED3D_LOCATION_TEXTURE_RGB)
1961*81cffd76SJoachim Henze wined3d_texture_bind_and_dirtify(texture, context, FALSE);
1962*81cffd76SJoachim Henze else
1963*81cffd76SJoachim Henze wined3d_texture_bind_and_dirtify(texture, context, TRUE);
1964*81cffd76SJoachim Henze
1965*81cffd76SJoachim Henze texture1d_download_data(texture, sub_resource_idx, context, &data);
1966*81cffd76SJoachim Henze ++texture->download_count;
1967*81cffd76SJoachim Henze }
1968*81cffd76SJoachim Henze else
1969*81cffd76SJoachim Henze {
1970*81cffd76SJoachim Henze FIXME("Implement WINED3D_LOCATION_SYSMEM loading from %s.\n",
1971*81cffd76SJoachim Henze wined3d_debug_location(sub_resource->locations));
1972*81cffd76SJoachim Henze return FALSE;
1973*81cffd76SJoachim Henze }
1974*81cffd76SJoachim Henze break;
1975*81cffd76SJoachim Henze
1976*81cffd76SJoachim Henze case WINED3D_LOCATION_BUFFER:
1977*81cffd76SJoachim Henze if (sub_resource->locations & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB))
1978*81cffd76SJoachim Henze {
1979*81cffd76SJoachim Henze #if !defined(STAGING_CSMT)
1980*81cffd76SJoachim Henze struct wined3d_bo_address data = {sub_resource->buffer_object, NULL};
1981*81cffd76SJoachim Henze #else /* STAGING_CSMT */
1982*81cffd76SJoachim Henze struct wined3d_bo_address data = {sub_resource->buffer->name, NULL};
1983*81cffd76SJoachim Henze #endif /* STAGING_CSMT */
1984*81cffd76SJoachim Henze
1985*81cffd76SJoachim Henze if (sub_resource->locations & WINED3D_LOCATION_TEXTURE_RGB)
1986*81cffd76SJoachim Henze wined3d_texture_bind_and_dirtify(texture, context, FALSE);
1987*81cffd76SJoachim Henze else
1988*81cffd76SJoachim Henze wined3d_texture_bind_and_dirtify(texture, context, TRUE);
1989*81cffd76SJoachim Henze
1990*81cffd76SJoachim Henze texture1d_download_data(texture, sub_resource_idx, context, &data);
1991*81cffd76SJoachim Henze }
1992*81cffd76SJoachim Henze else
1993*81cffd76SJoachim Henze {
1994*81cffd76SJoachim Henze FIXME("Implement WINED3D_LOCATION_BUFFER loading from %s.\n",
1995*81cffd76SJoachim Henze wined3d_debug_location(sub_resource->locations));
1996*81cffd76SJoachim Henze return FALSE;
1997*81cffd76SJoachim Henze }
1998*81cffd76SJoachim Henze break;
1999*81cffd76SJoachim Henze
2000*81cffd76SJoachim Henze default:
2001*81cffd76SJoachim Henze FIXME("Implement %s loading from %s.\n", wined3d_debug_location(location),
2002*81cffd76SJoachim Henze wined3d_debug_location(sub_resource->locations));
2003*81cffd76SJoachim Henze return FALSE;
2004*81cffd76SJoachim Henze }
2005*81cffd76SJoachim Henze
2006*81cffd76SJoachim Henze done:
2007*81cffd76SJoachim Henze wined3d_texture_validate_location(texture, sub_resource_idx, location);
2008*81cffd76SJoachim Henze
2009*81cffd76SJoachim Henze return TRUE;
2010*81cffd76SJoachim Henze }
2011*81cffd76SJoachim Henze
2012*81cffd76SJoachim Henze static void texture1d_prepare_texture(struct wined3d_texture *texture, struct wined3d_context *context, BOOL srgb)
2013*81cffd76SJoachim Henze {
2014*81cffd76SJoachim Henze const struct wined3d_format *format = texture->resource.format;
2015*81cffd76SJoachim Henze unsigned int sub_count = texture->level_count * texture->layer_count;
2016*81cffd76SJoachim Henze const struct wined3d_gl_info *gl_info = context->gl_info;
2017*81cffd76SJoachim Henze unsigned int width;
20187af3969eSAmine Khaldi GLenum internal;
20199987f029SAmine Khaldi
2020*81cffd76SJoachim Henze wined3d_texture_bind_and_dirtify(texture, context, srgb);
2021*81cffd76SJoachim Henze
20227af3969eSAmine Khaldi if (srgb)
20237af3969eSAmine Khaldi internal = format->glGammaInternal;
20247af3969eSAmine Khaldi else if (texture->resource.usage & WINED3DUSAGE_RENDERTARGET
20257af3969eSAmine Khaldi && wined3d_resource_is_offscreen(&texture->resource))
20267af3969eSAmine Khaldi internal = format->rtInternal;
20277af3969eSAmine Khaldi else
20287af3969eSAmine Khaldi internal = format->glInternal;
20299987f029SAmine Khaldi
2030*81cffd76SJoachim Henze if (wined3d_texture_use_immutable_storage(texture, gl_info))
2031*81cffd76SJoachim Henze {
2032*81cffd76SJoachim Henze width = wined3d_texture_get_level_width(texture, 0);
20337af3969eSAmine Khaldi
2034*81cffd76SJoachim Henze if (texture->target == GL_TEXTURE_1D_ARRAY)
20357af3969eSAmine Khaldi {
2036*81cffd76SJoachim Henze GL_EXTCALL(glTexStorage2D(texture->target, texture->level_count, internal, width, texture->layer_count));
2037*81cffd76SJoachim Henze checkGLcall("glTexStorage2D");
20389987f029SAmine Khaldi }
20399987f029SAmine Khaldi else
20409987f029SAmine Khaldi {
2041*81cffd76SJoachim Henze GL_EXTCALL(glTexStorage1D(texture->target, texture->level_count, internal, width));
2042*81cffd76SJoachim Henze checkGLcall("glTexStorage1D");
20437af3969eSAmine Khaldi }
20447af3969eSAmine Khaldi }
20457af3969eSAmine Khaldi else
20467af3969eSAmine Khaldi {
20479987f029SAmine Khaldi unsigned int i;
20489987f029SAmine Khaldi
2049*81cffd76SJoachim Henze for (i = 0; i < sub_count; ++i)
20509987f029SAmine Khaldi {
2051*81cffd76SJoachim Henze GLenum target;
2052*81cffd76SJoachim Henze struct wined3d_surface *surface = texture->sub_resources[i].u.surface;
2053*81cffd76SJoachim Henze width = wined3d_texture_get_level_width(texture, surface->texture_level);
2054*81cffd76SJoachim Henze target = wined3d_texture_get_sub_resource_target(texture, i);
2055*81cffd76SJoachim Henze
2056*81cffd76SJoachim Henze if (texture->target == GL_TEXTURE_1D_ARRAY)
2057*81cffd76SJoachim Henze {
2058*81cffd76SJoachim Henze gl_info->gl_ops.gl.p_glTexImage2D(target, surface->texture_level,
2059*81cffd76SJoachim Henze internal, width, texture->layer_count, 0, format->glFormat, format->glType, NULL);
2060*81cffd76SJoachim Henze checkGLcall("glTexImage2D");
2061*81cffd76SJoachim Henze }
2062*81cffd76SJoachim Henze else
2063*81cffd76SJoachim Henze {
2064*81cffd76SJoachim Henze gl_info->gl_ops.gl.p_glTexImage1D(target, surface->texture_level,
2065*81cffd76SJoachim Henze internal, width, 0, format->glFormat, format->glType, NULL);
2066*81cffd76SJoachim Henze checkGLcall("glTexImage1D");
2067*81cffd76SJoachim Henze }
20689987f029SAmine Khaldi }
20699987f029SAmine Khaldi }
20709987f029SAmine Khaldi }
20719987f029SAmine Khaldi
2072*81cffd76SJoachim Henze static void texture1d_cleanup_sub_resources(struct wined3d_texture *texture)
2073*81cffd76SJoachim Henze {
2074*81cffd76SJoachim Henze }
2075*81cffd76SJoachim Henze
2076*81cffd76SJoachim Henze static const struct wined3d_texture_ops texture1d_ops =
2077*81cffd76SJoachim Henze {
2078*81cffd76SJoachim Henze texture1d_upload_data,
2079*81cffd76SJoachim Henze texture1d_load_location,
2080*81cffd76SJoachim Henze texture1d_prepare_texture,
2081*81cffd76SJoachim Henze texture1d_cleanup_sub_resources,
2082*81cffd76SJoachim Henze };
2083*81cffd76SJoachim Henze
2084*81cffd76SJoachim Henze static void texture2d_upload_data(struct wined3d_texture *texture, unsigned int sub_resource_idx,
2085*81cffd76SJoachim Henze const struct wined3d_context *context, const struct wined3d_box *box,
2086*81cffd76SJoachim Henze const struct wined3d_const_bo_address *data, unsigned int row_pitch, unsigned int slice_pitch)
2087*81cffd76SJoachim Henze {
2088*81cffd76SJoachim Henze unsigned int texture_level;
2089*81cffd76SJoachim Henze POINT dst_point;
2090*81cffd76SJoachim Henze RECT src_rect;
2091*81cffd76SJoachim Henze
2092*81cffd76SJoachim Henze src_rect.left = 0;
2093*81cffd76SJoachim Henze src_rect.top = 0;
2094*81cffd76SJoachim Henze if (box)
2095*81cffd76SJoachim Henze {
2096*81cffd76SJoachim Henze dst_point.x = box->left;
2097*81cffd76SJoachim Henze dst_point.y = box->top;
2098*81cffd76SJoachim Henze src_rect.right = box->right - box->left;
2099*81cffd76SJoachim Henze src_rect.bottom = box->bottom - box->top;
2100*81cffd76SJoachim Henze }
2101*81cffd76SJoachim Henze else
2102*81cffd76SJoachim Henze {
2103*81cffd76SJoachim Henze dst_point.x = dst_point.y = 0;
2104*81cffd76SJoachim Henze texture_level = sub_resource_idx % texture->level_count;
2105*81cffd76SJoachim Henze src_rect.right = wined3d_texture_get_level_width(texture, texture_level);
2106*81cffd76SJoachim Henze src_rect.bottom = wined3d_texture_get_level_height(texture, texture_level);
2107*81cffd76SJoachim Henze }
2108*81cffd76SJoachim Henze
2109*81cffd76SJoachim Henze wined3d_surface_upload_data(texture->sub_resources[sub_resource_idx].u.surface, context->gl_info,
2110*81cffd76SJoachim Henze texture->resource.format, &src_rect, row_pitch, &dst_point, FALSE, data);
2111*81cffd76SJoachim Henze }
2112*81cffd76SJoachim Henze
2113c2c66affSColin Finck static BOOL texture2d_load_location(struct wined3d_texture *texture, unsigned int sub_resource_idx,
2114c2c66affSColin Finck struct wined3d_context *context, DWORD location)
2115c2c66affSColin Finck {
2116*81cffd76SJoachim Henze return surface_load_location(texture->sub_resources[sub_resource_idx].u.surface, context, location);
2117c2c66affSColin Finck }
2118*81cffd76SJoachim Henze
2119*81cffd76SJoachim Henze /* Context activation is done by the caller. */
2120*81cffd76SJoachim Henze static void texture2d_prepare_texture(struct wined3d_texture *texture, struct wined3d_context *context, BOOL srgb)
2121*81cffd76SJoachim Henze {
2122*81cffd76SJoachim Henze const struct wined3d_format *format = texture->resource.format;
2123*81cffd76SJoachim Henze const struct wined3d_gl_info *gl_info = context->gl_info;
2124*81cffd76SJoachim Henze const struct wined3d_color_key_conversion *conversion;
2125*81cffd76SJoachim Henze GLenum internal;
2126*81cffd76SJoachim Henze
2127*81cffd76SJoachim Henze TRACE("texture %p, context %p, format %s.\n", texture, context, debug_d3dformat(format->id));
2128*81cffd76SJoachim Henze
2129*81cffd76SJoachim Henze if (format->conv_byte_count)
2130*81cffd76SJoachim Henze {
2131*81cffd76SJoachim Henze texture->flags |= WINED3D_TEXTURE_CONVERTED;
2132*81cffd76SJoachim Henze }
2133*81cffd76SJoachim Henze else if ((conversion = wined3d_format_get_color_key_conversion(texture, TRUE)))
2134*81cffd76SJoachim Henze {
2135*81cffd76SJoachim Henze texture->flags |= WINED3D_TEXTURE_CONVERTED;
2136*81cffd76SJoachim Henze format = wined3d_get_format(gl_info, conversion->dst_format, texture->resource.usage);
2137*81cffd76SJoachim Henze TRACE("Using format %s for color key conversion.\n", debug_d3dformat(format->id));
2138*81cffd76SJoachim Henze }
2139*81cffd76SJoachim Henze
2140*81cffd76SJoachim Henze wined3d_texture_bind_and_dirtify(texture, context, srgb);
2141*81cffd76SJoachim Henze
2142*81cffd76SJoachim Henze if (srgb)
2143*81cffd76SJoachim Henze internal = format->glGammaInternal;
2144*81cffd76SJoachim Henze else if (texture->resource.usage & WINED3DUSAGE_RENDERTARGET
2145*81cffd76SJoachim Henze && wined3d_resource_is_offscreen(&texture->resource))
2146*81cffd76SJoachim Henze internal = format->rtInternal;
2147*81cffd76SJoachim Henze else
2148*81cffd76SJoachim Henze internal = format->glInternal;
2149*81cffd76SJoachim Henze
2150*81cffd76SJoachim Henze if (!internal)
2151*81cffd76SJoachim Henze FIXME("No GL internal format for format %s.\n", debug_d3dformat(format->id));
2152*81cffd76SJoachim Henze
2153*81cffd76SJoachim Henze TRACE("internal %#x, format %#x, type %#x.\n", internal, format->glFormat, format->glType);
2154*81cffd76SJoachim Henze
2155*81cffd76SJoachim Henze if (wined3d_texture_use_immutable_storage(texture, gl_info))
2156*81cffd76SJoachim Henze wined3d_texture_allocate_gl_immutable_storage(texture, internal, gl_info);
2157*81cffd76SJoachim Henze else
2158*81cffd76SJoachim Henze wined3d_texture_allocate_gl_mutable_storage(texture, internal, format, gl_info);
2159*81cffd76SJoachim Henze }
2160*81cffd76SJoachim Henze
2161*81cffd76SJoachim Henze static void texture2d_cleanup_sub_resources(struct wined3d_texture *texture)
2162*81cffd76SJoachim Henze {
2163*81cffd76SJoachim Henze unsigned int sub_count = texture->level_count * texture->layer_count;
2164*81cffd76SJoachim Henze struct wined3d_device *device = texture->resource.device;
2165*81cffd76SJoachim Henze struct wined3d_texture_sub_resource *sub_resource;
2166*81cffd76SJoachim Henze struct wined3d_renderbuffer_entry *entry, *entry2;
2167*81cffd76SJoachim Henze const struct wined3d_gl_info *gl_info = NULL;
2168*81cffd76SJoachim Henze struct wined3d_context *context = NULL;
2169*81cffd76SJoachim Henze struct wined3d_surface *surface;
2170*81cffd76SJoachim Henze unsigned int i;
2171*81cffd76SJoachim Henze
2172*81cffd76SJoachim Henze for (i = 0; i < sub_count; ++i)
2173*81cffd76SJoachim Henze {
2174*81cffd76SJoachim Henze sub_resource = &texture->sub_resources[i];
2175*81cffd76SJoachim Henze if (!(surface = sub_resource->u.surface))
2176*81cffd76SJoachim Henze continue;
2177*81cffd76SJoachim Henze
2178*81cffd76SJoachim Henze TRACE("surface %p.\n", surface);
2179*81cffd76SJoachim Henze
2180*81cffd76SJoachim Henze if (!context && !list_empty(&surface->renderbuffers))
2181*81cffd76SJoachim Henze {
2182*81cffd76SJoachim Henze context = context_acquire(device, NULL, 0);
2183*81cffd76SJoachim Henze gl_info = context->gl_info;
2184*81cffd76SJoachim Henze }
2185*81cffd76SJoachim Henze
2186*81cffd76SJoachim Henze LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &surface->renderbuffers, struct wined3d_renderbuffer_entry, entry)
2187*81cffd76SJoachim Henze {
2188*81cffd76SJoachim Henze TRACE("Deleting renderbuffer %u.\n", entry->id);
2189*81cffd76SJoachim Henze context_gl_resource_released(device, entry->id, TRUE);
2190*81cffd76SJoachim Henze gl_info->fbo_ops.glDeleteRenderbuffers(1, &entry->id);
2191*81cffd76SJoachim Henze heap_free(entry);
2192*81cffd76SJoachim Henze }
2193*81cffd76SJoachim Henze
2194*81cffd76SJoachim Henze if (surface->dc)
2195*81cffd76SJoachim Henze texture2d_destroy_dc(surface);
2196*81cffd76SJoachim Henze }
2197*81cffd76SJoachim Henze if (context)
2198*81cffd76SJoachim Henze context_release(context);
2199*81cffd76SJoachim Henze heap_free(texture->sub_resources[0].u.surface);
2200c2c66affSColin Finck }
2201c2c66affSColin Finck
2202c2c66affSColin Finck static const struct wined3d_texture_ops texture2d_ops =
2203c2c66affSColin Finck {
2204*81cffd76SJoachim Henze texture2d_upload_data,
2205c2c66affSColin Finck texture2d_load_location,
2206*81cffd76SJoachim Henze texture2d_prepare_texture,
2207*81cffd76SJoachim Henze texture2d_cleanup_sub_resources,
2208c2c66affSColin Finck };
2209c2c66affSColin Finck
2210c2c66affSColin Finck struct wined3d_texture * __cdecl wined3d_texture_from_resource(struct wined3d_resource *resource)
2211c2c66affSColin Finck {
2212c2c66affSColin Finck return texture_from_resource(resource);
2213c2c66affSColin Finck }
2214c2c66affSColin Finck
2215c2c66affSColin Finck static ULONG texture_resource_incref(struct wined3d_resource *resource)
2216c2c66affSColin Finck {
2217c2c66affSColin Finck return wined3d_texture_incref(texture_from_resource(resource));
2218c2c66affSColin Finck }
2219c2c66affSColin Finck
2220c2c66affSColin Finck static ULONG texture_resource_decref(struct wined3d_resource *resource)
2221c2c66affSColin Finck {
2222c2c66affSColin Finck return wined3d_texture_decref(texture_from_resource(resource));
2223c2c66affSColin Finck }
2224c2c66affSColin Finck
2225c2c66affSColin Finck static void texture_resource_preload(struct wined3d_resource *resource)
2226c2c66affSColin Finck {
2227c2c66affSColin Finck struct wined3d_texture *texture = texture_from_resource(resource);
2228c2c66affSColin Finck struct wined3d_context *context;
2229c2c66affSColin Finck
2230c2c66affSColin Finck context = context_acquire(resource->device, NULL, 0);
2231c2c66affSColin Finck wined3d_texture_load(texture, context, texture->flags & WINED3D_TEXTURE_IS_SRGB);
2232c2c66affSColin Finck context_release(context);
2233c2c66affSColin Finck }
2234c2c66affSColin Finck
2235c2c66affSColin Finck static void wined3d_texture_unload(struct wined3d_resource *resource)
2236c2c66affSColin Finck {
2237c2c66affSColin Finck struct wined3d_texture *texture = texture_from_resource(resource);
2238c2c66affSColin Finck UINT sub_count = texture->level_count * texture->layer_count;
2239c2c66affSColin Finck struct wined3d_device *device = resource->device;
2240c2c66affSColin Finck const struct wined3d_gl_info *gl_info;
2241c2c66affSColin Finck struct wined3d_context *context;
2242c2c66affSColin Finck UINT i;
2243c2c66affSColin Finck
2244c2c66affSColin Finck TRACE("texture %p.\n", texture);
2245c2c66affSColin Finck
2246c2c66affSColin Finck context = context_acquire(device, NULL, 0);
2247c2c66affSColin Finck gl_info = context->gl_info;
2248c2c66affSColin Finck
2249c2c66affSColin Finck for (i = 0; i < sub_count; ++i)
2250c2c66affSColin Finck {
2251c2c66affSColin Finck struct wined3d_texture_sub_resource *sub_resource = &texture->sub_resources[i];
2252c2c66affSColin Finck
22539987f029SAmine Khaldi if (resource->access & WINED3D_RESOURCE_ACCESS_CPU
2254c2c66affSColin Finck && wined3d_texture_load_location(texture, i, context, resource->map_binding))
2255c2c66affSColin Finck {
2256c2c66affSColin Finck wined3d_texture_invalidate_location(texture, i, ~resource->map_binding);
2257c2c66affSColin Finck }
2258c2c66affSColin Finck else
2259c2c66affSColin Finck {
2260c2c66affSColin Finck /* We should only get here on device reset/teardown for implicit
2261c2c66affSColin Finck * resources. */
22629987f029SAmine Khaldi if (resource->access & WINED3D_RESOURCE_ACCESS_CPU
22639987f029SAmine Khaldi || resource->type != WINED3D_RTYPE_TEXTURE_2D)
22649987f029SAmine Khaldi ERR("Discarding %s %p sub-resource %u with resource access %s.\n",
22659987f029SAmine Khaldi debug_d3dresourcetype(resource->type), resource, i,
22669987f029SAmine Khaldi wined3d_debug_resource_access(resource->access));
2267c2c66affSColin Finck wined3d_texture_validate_location(texture, i, WINED3D_LOCATION_DISCARDED);
2268c2c66affSColin Finck wined3d_texture_invalidate_location(texture, i, ~WINED3D_LOCATION_DISCARDED);
2269c2c66affSColin Finck }
2270c2c66affSColin Finck
2271*81cffd76SJoachim Henze #if !defined(STAGING_CSMT)
2272c2c66affSColin Finck if (sub_resource->buffer_object)
2273c2c66affSColin Finck wined3d_texture_remove_buffer_object(texture, i, context->gl_info);
2274*81cffd76SJoachim Henze #else /* STAGING_CSMT */
2275*81cffd76SJoachim Henze if (sub_resource->buffer)
2276*81cffd76SJoachim Henze wined3d_texture_remove_buffer_object(texture, i, context);
2277*81cffd76SJoachim Henze #endif /* STAGING_CSMT */
2278c2c66affSColin Finck
2279*81cffd76SJoachim Henze if (resource->type == WINED3D_RTYPE_TEXTURE_2D)
2280*81cffd76SJoachim Henze {
2281*81cffd76SJoachim Henze struct wined3d_surface *surface = sub_resource->u.surface;
2282*81cffd76SJoachim Henze struct wined3d_renderbuffer_entry *entry, *entry2;
2283*81cffd76SJoachim Henze
2284*81cffd76SJoachim Henze LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &surface->renderbuffers, struct wined3d_renderbuffer_entry, entry)
2285c2c66affSColin Finck {
2286c2c66affSColin Finck context_gl_resource_released(device, entry->id, TRUE);
2287c2c66affSColin Finck gl_info->fbo_ops.glDeleteRenderbuffers(1, &entry->id);
2288c2c66affSColin Finck list_remove(&entry->entry);
22899987f029SAmine Khaldi heap_free(entry);
2290c2c66affSColin Finck }
2291*81cffd76SJoachim Henze list_init(&surface->renderbuffers);
2292*81cffd76SJoachim Henze surface->current_renderbuffer = NULL;
2293*81cffd76SJoachim Henze }
2294*81cffd76SJoachim Henze }
2295c2c66affSColin Finck
2296c2c66affSColin Finck context_release(context);
2297c2c66affSColin Finck
2298c2c66affSColin Finck wined3d_texture_force_reload(texture);
2299c2c66affSColin Finck wined3d_texture_unload_gl_texture(texture);
2300c2c66affSColin Finck }
2301c2c66affSColin Finck
2302c2c66affSColin Finck static HRESULT texture_resource_sub_resource_map(struct wined3d_resource *resource, unsigned int sub_resource_idx,
2303c2c66affSColin Finck struct wined3d_map_desc *map_desc, const struct wined3d_box *box, DWORD flags)
2304c2c66affSColin Finck {
2305c2c66affSColin Finck const struct wined3d_format *format = resource->format;
2306c2c66affSColin Finck struct wined3d_texture_sub_resource *sub_resource;
2307c2c66affSColin Finck struct wined3d_device *device = resource->device;
2308c2c66affSColin Finck unsigned int fmt_flags = resource->format_flags;
2309c2c66affSColin Finck struct wined3d_context *context = NULL;
2310c2c66affSColin Finck struct wined3d_texture *texture;
2311c2c66affSColin Finck struct wined3d_bo_address data;
2312c2c66affSColin Finck unsigned int texture_level;
2313c2c66affSColin Finck BYTE *base_memory;
2314c2c66affSColin Finck BOOL ret;
2315c2c66affSColin Finck
2316c2c66affSColin Finck TRACE("resource %p, sub_resource_idx %u, map_desc %p, box %s, flags %#x.\n",
2317c2c66affSColin Finck resource, sub_resource_idx, map_desc, debug_box(box), flags);
2318c2c66affSColin Finck
2319c2c66affSColin Finck texture = texture_from_resource(resource);
2320c2c66affSColin Finck if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
2321c2c66affSColin Finck return E_INVALIDARG;
2322c2c66affSColin Finck
2323c2c66affSColin Finck texture_level = sub_resource_idx % texture->level_count;
2324c2c66affSColin Finck if (box && FAILED(wined3d_texture_check_box_dimensions(texture, texture_level, box)))
2325c2c66affSColin Finck {
2326c2c66affSColin Finck WARN("Map box is invalid.\n");
23279987f029SAmine Khaldi if (((fmt_flags & WINED3DFMT_FLAG_BLOCKS) && !(resource->access & WINED3D_RESOURCE_ACCESS_CPU))
2328c2c66affSColin Finck || resource->type != WINED3D_RTYPE_TEXTURE_2D)
2329c2c66affSColin Finck return WINED3DERR_INVALIDCALL;
2330c2c66affSColin Finck }
2331c2c66affSColin Finck
2332c2c66affSColin Finck if (texture->flags & WINED3D_TEXTURE_DC_IN_USE)
2333c2c66affSColin Finck {
2334c2c66affSColin Finck WARN("DC is in use.\n");
2335c2c66affSColin Finck return WINED3DERR_INVALIDCALL;
2336c2c66affSColin Finck }
2337c2c66affSColin Finck
2338c2c66affSColin Finck if (sub_resource->map_count)
2339c2c66affSColin Finck {
2340c2c66affSColin Finck WARN("Sub-resource is already mapped.\n");
2341c2c66affSColin Finck return WINED3DERR_INVALIDCALL;
2342c2c66affSColin Finck }
2343c2c66affSColin Finck
2344c2c66affSColin Finck if (device->d3d_initialized)
2345c2c66affSColin Finck context = context_acquire(device, NULL, 0);
2346c2c66affSColin Finck
2347c2c66affSColin Finck if (flags & WINED3D_MAP_DISCARD)
2348c2c66affSColin Finck {
2349c2c66affSColin Finck TRACE("WINED3D_MAP_DISCARD flag passed, marking %s as up to date.\n",
2350c2c66affSColin Finck wined3d_debug_location(resource->map_binding));
2351c2c66affSColin Finck if ((ret = wined3d_texture_prepare_location(texture, sub_resource_idx, context, resource->map_binding)))
2352c2c66affSColin Finck wined3d_texture_validate_location(texture, sub_resource_idx, resource->map_binding);
2353c2c66affSColin Finck }
2354c2c66affSColin Finck else
2355c2c66affSColin Finck {
2356c2c66affSColin Finck if (resource->usage & WINED3DUSAGE_DYNAMIC)
2357c2c66affSColin Finck WARN_(d3d_perf)("Mapping a dynamic texture without WINED3D_MAP_DISCARD.\n");
2358c2c66affSColin Finck ret = wined3d_texture_load_location(texture, sub_resource_idx, context, resource->map_binding);
2359c2c66affSColin Finck }
2360c2c66affSColin Finck
2361c2c66affSColin Finck if (!ret)
2362c2c66affSColin Finck {
2363c2c66affSColin Finck ERR("Failed to prepare location.\n");
2364c2c66affSColin Finck context_release(context);
2365c2c66affSColin Finck return E_OUTOFMEMORY;
2366c2c66affSColin Finck }
2367c2c66affSColin Finck
23689987f029SAmine Khaldi if (flags & WINED3D_MAP_WRITE
2369c2c66affSColin Finck && (!(flags & WINED3D_MAP_NO_DIRTY_UPDATE) || (resource->usage & WINED3DUSAGE_DYNAMIC)))
2370c2c66affSColin Finck wined3d_texture_invalidate_location(texture, sub_resource_idx, ~resource->map_binding);
2371c2c66affSColin Finck
2372c2c66affSColin Finck wined3d_texture_get_memory(texture, sub_resource_idx, &data, resource->map_binding);
2373c2c66affSColin Finck base_memory = context_map_bo_address(context, &data, sub_resource->size, GL_PIXEL_UNPACK_BUFFER, flags);
2374c2c66affSColin Finck TRACE("Base memory pointer %p.\n", base_memory);
2375c2c66affSColin Finck
2376c2c66affSColin Finck if (context)
2377c2c66affSColin Finck context_release(context);
2378c2c66affSColin Finck
2379c2c66affSColin Finck if (fmt_flags & WINED3DFMT_FLAG_BROKEN_PITCH)
2380c2c66affSColin Finck {
2381c2c66affSColin Finck map_desc->row_pitch = wined3d_texture_get_level_width(texture, texture_level) * format->byte_count;
2382c2c66affSColin Finck map_desc->slice_pitch = wined3d_texture_get_level_height(texture, texture_level) * map_desc->row_pitch;
2383c2c66affSColin Finck }
2384c2c66affSColin Finck else
2385c2c66affSColin Finck {
2386c2c66affSColin Finck wined3d_texture_get_pitch(texture, texture_level, &map_desc->row_pitch, &map_desc->slice_pitch);
2387c2c66affSColin Finck }
2388c2c66affSColin Finck
2389c2c66affSColin Finck if (!box)
2390c2c66affSColin Finck {
2391c2c66affSColin Finck map_desc->data = base_memory;
2392c2c66affSColin Finck }
2393c2c66affSColin Finck else
2394c2c66affSColin Finck {
2395c2c66affSColin Finck if ((fmt_flags & (WINED3DFMT_FLAG_BLOCKS | WINED3DFMT_FLAG_BROKEN_PITCH)) == WINED3DFMT_FLAG_BLOCKS)
2396c2c66affSColin Finck {
2397c2c66affSColin Finck /* Compressed textures are block based, so calculate the offset of
2398c2c66affSColin Finck * the block that contains the top-left pixel of the mapped box. */
2399c2c66affSColin Finck map_desc->data = base_memory
2400c2c66affSColin Finck + (box->front * map_desc->slice_pitch)
2401c2c66affSColin Finck + ((box->top / format->block_height) * map_desc->row_pitch)
2402c2c66affSColin Finck + ((box->left / format->block_width) * format->block_byte_count);
2403c2c66affSColin Finck }
2404c2c66affSColin Finck else
2405c2c66affSColin Finck {
2406c2c66affSColin Finck map_desc->data = base_memory
2407c2c66affSColin Finck + (box->front * map_desc->slice_pitch)
2408c2c66affSColin Finck + (box->top * map_desc->row_pitch)
2409c2c66affSColin Finck + (box->left * format->byte_count);
2410c2c66affSColin Finck }
2411c2c66affSColin Finck }
2412c2c66affSColin Finck
2413c2c66affSColin Finck if (texture->swapchain && texture->swapchain->front_buffer == texture)
2414c2c66affSColin Finck {
2415c2c66affSColin Finck RECT *r = &texture->swapchain->front_buffer_update;
2416c2c66affSColin Finck
2417c2c66affSColin Finck if (!box)
2418c2c66affSColin Finck SetRect(r, 0, 0, resource->width, resource->height);
2419c2c66affSColin Finck else
2420c2c66affSColin Finck SetRect(r, box->left, box->top, box->right, box->bottom);
2421c2c66affSColin Finck TRACE("Mapped front buffer %s.\n", wine_dbgstr_rect(r));
2422c2c66affSColin Finck }
2423c2c66affSColin Finck
2424c2c66affSColin Finck ++resource->map_count;
2425c2c66affSColin Finck ++sub_resource->map_count;
2426c2c66affSColin Finck
2427c2c66affSColin Finck TRACE("Returning memory %p, row pitch %u, slice pitch %u.\n",
2428c2c66affSColin Finck map_desc->data, map_desc->row_pitch, map_desc->slice_pitch);
2429c2c66affSColin Finck
2430c2c66affSColin Finck return WINED3D_OK;
2431c2c66affSColin Finck }
2432c2c66affSColin Finck
24339987f029SAmine Khaldi static HRESULT texture_resource_sub_resource_map_info(struct wined3d_resource *resource, unsigned int sub_resource_idx,
24349987f029SAmine Khaldi struct wined3d_map_info *info, DWORD flags)
24359987f029SAmine Khaldi {
24369987f029SAmine Khaldi const struct wined3d_format *format = resource->format;
24379987f029SAmine Khaldi struct wined3d_texture_sub_resource *sub_resource;
24389987f029SAmine Khaldi unsigned int fmt_flags = resource->format_flags;
24399987f029SAmine Khaldi struct wined3d_texture *texture;
24409987f029SAmine Khaldi unsigned int texture_level;
24419987f029SAmine Khaldi
24429987f029SAmine Khaldi texture = texture_from_resource(resource);
24439987f029SAmine Khaldi if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
24449987f029SAmine Khaldi return E_INVALIDARG;
24459987f029SAmine Khaldi
24469987f029SAmine Khaldi texture_level = sub_resource_idx % texture->level_count;
24479987f029SAmine Khaldi
24489987f029SAmine Khaldi if (fmt_flags & WINED3DFMT_FLAG_BROKEN_PITCH)
24499987f029SAmine Khaldi {
24509987f029SAmine Khaldi info->row_pitch = wined3d_texture_get_level_width(texture, texture_level) * format->byte_count;
24519987f029SAmine Khaldi info->slice_pitch = wined3d_texture_get_level_height(texture, texture_level) * info->row_pitch;
24529987f029SAmine Khaldi }
24539987f029SAmine Khaldi else
24549987f029SAmine Khaldi {
24559987f029SAmine Khaldi wined3d_texture_get_pitch(texture, texture_level, &info->row_pitch, &info->slice_pitch);
24569987f029SAmine Khaldi }
24579987f029SAmine Khaldi
24589987f029SAmine Khaldi info->size = info->slice_pitch * wined3d_texture_get_level_depth(texture, texture_level);
24599987f029SAmine Khaldi
24609987f029SAmine Khaldi return WINED3D_OK;
24619987f029SAmine Khaldi }
24629987f029SAmine Khaldi
2463c2c66affSColin Finck static HRESULT texture_resource_sub_resource_unmap(struct wined3d_resource *resource, unsigned int sub_resource_idx)
2464c2c66affSColin Finck {
2465c2c66affSColin Finck struct wined3d_texture_sub_resource *sub_resource;
2466c2c66affSColin Finck struct wined3d_device *device = resource->device;
2467c2c66affSColin Finck struct wined3d_context *context = NULL;
2468c2c66affSColin Finck struct wined3d_texture *texture;
2469c2c66affSColin Finck struct wined3d_bo_address data;
2470c2c66affSColin Finck
2471c2c66affSColin Finck TRACE("resource %p, sub_resource_idx %u.\n", resource, sub_resource_idx);
2472c2c66affSColin Finck
2473c2c66affSColin Finck texture = texture_from_resource(resource);
2474c2c66affSColin Finck if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
2475c2c66affSColin Finck return E_INVALIDARG;
2476c2c66affSColin Finck
2477c2c66affSColin Finck if (!sub_resource->map_count)
2478c2c66affSColin Finck {
2479c2c66affSColin Finck WARN("Trying to unmap unmapped sub-resource.\n");
2480c2c66affSColin Finck if (texture->flags & WINED3D_TEXTURE_DC_IN_USE)
2481c2c66affSColin Finck return WINED3D_OK;
2482c2c66affSColin Finck return WINEDDERR_NOTLOCKED;
2483c2c66affSColin Finck }
2484c2c66affSColin Finck
2485c2c66affSColin Finck if (device->d3d_initialized)
2486c2c66affSColin Finck context = context_acquire(device, NULL, 0);
2487c2c66affSColin Finck
2488c2c66affSColin Finck wined3d_texture_get_memory(texture, sub_resource_idx, &data, texture->resource.map_binding);
2489c2c66affSColin Finck context_unmap_bo_address(context, &data, GL_PIXEL_UNPACK_BUFFER);
2490c2c66affSColin Finck
2491c2c66affSColin Finck if (context)
2492c2c66affSColin Finck context_release(context);
2493c2c66affSColin Finck
2494c2c66affSColin Finck if (texture->swapchain && texture->swapchain->front_buffer == texture)
2495c2c66affSColin Finck {
2496c2c66affSColin Finck if (!(sub_resource->locations & (WINED3D_LOCATION_DRAWABLE | WINED3D_LOCATION_TEXTURE_RGB)))
2497c2c66affSColin Finck texture->swapchain->swapchain_ops->swapchain_frontbuffer_updated(texture->swapchain);
2498c2c66affSColin Finck }
2499c2c66affSColin Finck
2500c2c66affSColin Finck --sub_resource->map_count;
2501c2c66affSColin Finck if (!--resource->map_count && texture->update_map_binding)
2502c2c66affSColin Finck wined3d_texture_update_map_binding(texture);
2503c2c66affSColin Finck
2504c2c66affSColin Finck return WINED3D_OK;
2505c2c66affSColin Finck }
2506c2c66affSColin Finck
2507c2c66affSColin Finck static const struct wined3d_resource_ops texture_resource_ops =
2508c2c66affSColin Finck {
2509c2c66affSColin Finck texture_resource_incref,
2510c2c66affSColin Finck texture_resource_decref,
2511c2c66affSColin Finck texture_resource_preload,
2512c2c66affSColin Finck wined3d_texture_unload,
2513c2c66affSColin Finck texture_resource_sub_resource_map,
25149987f029SAmine Khaldi texture_resource_sub_resource_map_info,
2515c2c66affSColin Finck texture_resource_sub_resource_unmap,
2516c2c66affSColin Finck };
2517c2c66affSColin Finck
2518*81cffd76SJoachim Henze static HRESULT texture1d_init(struct wined3d_texture *texture, const struct wined3d_resource_desc *desc,
2519*81cffd76SJoachim Henze UINT layer_count, UINT level_count, struct wined3d_device *device, void *parent,
2520*81cffd76SJoachim Henze const struct wined3d_parent_ops *parent_ops)
2521c2c66affSColin Finck {
2522c2c66affSColin Finck struct wined3d_device_parent *device_parent = device->device_parent;
2523c2c66affSColin Finck const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
2524*81cffd76SJoachim Henze struct wined3d_surface *surfaces;
2525*81cffd76SJoachim Henze unsigned int i, j;
2526c2c66affSColin Finck HRESULT hr;
2527c2c66affSColin Finck
2528*81cffd76SJoachim Henze if (layer_count > 1 && !gl_info->supported[EXT_TEXTURE_ARRAY])
2529*81cffd76SJoachim Henze {
2530*81cffd76SJoachim Henze WARN("OpenGL implementation does not support array textures.\n");
25317af3969eSAmine Khaldi return WINED3DERR_INVALIDCALL;
25327af3969eSAmine Khaldi }
25337af3969eSAmine Khaldi
2534*81cffd76SJoachim Henze /* TODO: It should only be possible to create textures for formats
2535*81cffd76SJoachim Henze * that are reported as supported. */
2536*81cffd76SJoachim Henze if (WINED3DFMT_UNKNOWN >= desc->format)
25377af3969eSAmine Khaldi {
2538*81cffd76SJoachim Henze WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture);
2539*81cffd76SJoachim Henze return WINED3DERR_INVALIDCALL;
2540*81cffd76SJoachim Henze }
2541*81cffd76SJoachim Henze
2542*81cffd76SJoachim Henze if (desc->usage & WINED3DUSAGE_LEGACY_CUBEMAP)
2543*81cffd76SJoachim Henze {
2544*81cffd76SJoachim Henze WARN("1d textures can not be used for cube mapping, returning D3DERR_INVALIDCALL.\n");
2545*81cffd76SJoachim Henze return WINED3DERR_INVALIDCALL;
2546*81cffd76SJoachim Henze }
2547*81cffd76SJoachim Henze
2548*81cffd76SJoachim Henze if ((desc->usage & WINED3DUSAGE_DYNAMIC && wined3d_resource_access_is_managed(desc->access))
2549*81cffd76SJoachim Henze || (desc->usage & WINED3DUSAGE_SCRATCH))
2550*81cffd76SJoachim Henze {
2551*81cffd76SJoachim Henze WARN("Attempted to create a DYNAMIC texture in pool %s.\n", wined3d_debug_resource_access(desc->access));
2552*81cffd76SJoachim Henze return WINED3DERR_INVALIDCALL;
2553*81cffd76SJoachim Henze }
2554*81cffd76SJoachim Henze
2555*81cffd76SJoachim Henze if (!gl_info->supported[ARB_TEXTURE_NON_POWER_OF_TWO] && !is_power_of_two(desc->width))
2556*81cffd76SJoachim Henze {
2557*81cffd76SJoachim Henze if (desc->usage & WINED3DUSAGE_SCRATCH)
2558*81cffd76SJoachim Henze {
2559*81cffd76SJoachim Henze WARN("Creating a scratch NPOT 1d texture despite lack of HW support.\n");
2560*81cffd76SJoachim Henze }
2561*81cffd76SJoachim Henze else
2562*81cffd76SJoachim Henze {
2563*81cffd76SJoachim Henze WARN("Attempted to create a NPOT 1d texture (%u, %u, %u) without GL support.\n",
2564*81cffd76SJoachim Henze desc->width, desc->height, desc->depth);
25657af3969eSAmine Khaldi return WINED3DERR_INVALIDCALL;
25667af3969eSAmine Khaldi }
25677af3969eSAmine Khaldi }
25687af3969eSAmine Khaldi
2569*81cffd76SJoachim Henze if (desc->usage & WINED3DUSAGE_QUERY_GENMIPMAP)
2570*81cffd76SJoachim Henze {
2571*81cffd76SJoachim Henze if (level_count != 1)
2572*81cffd76SJoachim Henze {
2573*81cffd76SJoachim Henze WARN("WINED3DUSAGE_QUERY_GENMIPMAP is set, and level count != 1, returning WINED3DERR_INVALIDCALL.\n");
2574*81cffd76SJoachim Henze return WINED3DERR_INVALIDCALL;
2575*81cffd76SJoachim Henze }
2576*81cffd76SJoachim Henze }
2577*81cffd76SJoachim Henze
2578*81cffd76SJoachim Henze if (FAILED(hr = wined3d_texture_init(texture, &texture1d_ops, layer_count, level_count, desc,
2579*81cffd76SJoachim Henze 0, device, parent, parent_ops, &texture_resource_ops)))
2580*81cffd76SJoachim Henze {
2581*81cffd76SJoachim Henze WARN("Failed to initialize texture, returning %#x.\n", hr);
2582*81cffd76SJoachim Henze return hr;
2583*81cffd76SJoachim Henze }
2584*81cffd76SJoachim Henze
2585*81cffd76SJoachim Henze texture->pow2_matrix[0] = 1.0f;
2586*81cffd76SJoachim Henze texture->pow2_matrix[5] = 1.0f;
2587*81cffd76SJoachim Henze texture->pow2_matrix[10] = 1.0f;
2588*81cffd76SJoachim Henze texture->pow2_matrix[15] = 1.0f;
2589*81cffd76SJoachim Henze texture->target = (layer_count > 1) ? GL_TEXTURE_1D_ARRAY : GL_TEXTURE_1D;
2590*81cffd76SJoachim Henze
2591*81cffd76SJoachim Henze if (wined3d_texture_use_pbo(texture, gl_info))
2592*81cffd76SJoachim Henze {
2593*81cffd76SJoachim Henze wined3d_resource_free_sysmem(&texture->resource);
2594*81cffd76SJoachim Henze texture->resource.map_binding = WINED3D_LOCATION_BUFFER;
2595*81cffd76SJoachim Henze }
2596*81cffd76SJoachim Henze
2597*81cffd76SJoachim Henze if (level_count > ~(SIZE_T)0 / layer_count
2598*81cffd76SJoachim Henze || !(surfaces = heap_calloc(level_count * layer_count, sizeof(*surfaces))))
2599*81cffd76SJoachim Henze {
2600*81cffd76SJoachim Henze wined3d_texture_cleanup_sync(texture);
2601*81cffd76SJoachim Henze return E_OUTOFMEMORY;
2602*81cffd76SJoachim Henze }
2603*81cffd76SJoachim Henze
2604*81cffd76SJoachim Henze /* Generate all the surfaces. */
2605*81cffd76SJoachim Henze for (i = 0; i < texture->level_count; ++i)
2606*81cffd76SJoachim Henze {
2607*81cffd76SJoachim Henze for (j = 0; j < texture->layer_count; ++j)
2608*81cffd76SJoachim Henze {
2609*81cffd76SJoachim Henze struct wined3d_texture_sub_resource *sub_resource;
2610*81cffd76SJoachim Henze unsigned int idx = j * texture->level_count + i;
2611*81cffd76SJoachim Henze struct wined3d_surface *surface;
2612*81cffd76SJoachim Henze
2613*81cffd76SJoachim Henze surface = &surfaces[idx];
2614*81cffd76SJoachim Henze surface->container = texture;
2615*81cffd76SJoachim Henze surface->texture_level = i;
2616*81cffd76SJoachim Henze surface->texture_layer = j;
2617*81cffd76SJoachim Henze list_init(&surface->renderbuffers);
2618*81cffd76SJoachim Henze
2619*81cffd76SJoachim Henze sub_resource = &texture->sub_resources[idx];
2620*81cffd76SJoachim Henze sub_resource->locations = WINED3D_LOCATION_DISCARDED;
2621*81cffd76SJoachim Henze sub_resource->u.surface = surface;
2622*81cffd76SJoachim Henze
2623*81cffd76SJoachim Henze if (FAILED(hr = device_parent->ops->surface_created(device_parent,
2624*81cffd76SJoachim Henze texture, idx, &sub_resource->parent, &sub_resource->parent_ops)))
2625*81cffd76SJoachim Henze {
2626*81cffd76SJoachim Henze WARN("Failed to create texture1d parent, hr %#x.\n", hr);
2627*81cffd76SJoachim Henze sub_resource->parent = NULL;
2628*81cffd76SJoachim Henze wined3d_texture_cleanup_sync(texture);
2629*81cffd76SJoachim Henze return hr;
2630*81cffd76SJoachim Henze }
2631*81cffd76SJoachim Henze
2632*81cffd76SJoachim Henze TRACE("parent %p, parent_ops %p.\n", parent, parent_ops);
2633*81cffd76SJoachim Henze
2634*81cffd76SJoachim Henze TRACE("Created 1d texture surface level %u, layer %u @ %p.\n", i, j, surface);
2635*81cffd76SJoachim Henze }
2636*81cffd76SJoachim Henze }
2637*81cffd76SJoachim Henze
2638*81cffd76SJoachim Henze return WINED3D_OK;
2639*81cffd76SJoachim Henze }
2640*81cffd76SJoachim Henze
2641*81cffd76SJoachim Henze static HRESULT texture_init(struct wined3d_texture *texture, const struct wined3d_resource_desc *desc,
2642*81cffd76SJoachim Henze unsigned int layer_count, unsigned int level_count, DWORD flags, struct wined3d_device *device,
2643*81cffd76SJoachim Henze void *parent, const struct wined3d_parent_ops *parent_ops)
2644*81cffd76SJoachim Henze {
2645*81cffd76SJoachim Henze struct wined3d_device_parent *device_parent = device->device_parent;
2646*81cffd76SJoachim Henze const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
2647*81cffd76SJoachim Henze struct wined3d_surface *surfaces;
2648*81cffd76SJoachim Henze UINT pow2_width, pow2_height;
2649*81cffd76SJoachim Henze unsigned int i, j, sub_count;
2650*81cffd76SJoachim Henze HRESULT hr;
2651*81cffd76SJoachim Henze
2652c2c66affSColin Finck if (!(desc->usage & WINED3DUSAGE_LEGACY_CUBEMAP) && layer_count > 1
2653c2c66affSColin Finck && !gl_info->supported[EXT_TEXTURE_ARRAY])
2654c2c66affSColin Finck {
2655c2c66affSColin Finck WARN("OpenGL implementation does not support array textures.\n");
2656c2c66affSColin Finck return WINED3DERR_INVALIDCALL;
2657c2c66affSColin Finck }
2658c2c66affSColin Finck
2659c2c66affSColin Finck /* TODO: It should only be possible to create textures for formats
2660c2c66affSColin Finck * that are reported as supported. */
2661c2c66affSColin Finck if (WINED3DFMT_UNKNOWN >= desc->format)
2662c2c66affSColin Finck {
2663*81cffd76SJoachim Henze WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture);
2664c2c66affSColin Finck return WINED3DERR_INVALIDCALL;
2665c2c66affSColin Finck }
2666c2c66affSColin Finck
2667*81cffd76SJoachim Henze if (desc->usage & WINED3DUSAGE_DYNAMIC && wined3d_resource_access_is_managed(desc->access))
2668*81cffd76SJoachim Henze FIXME("Trying to create a managed texture with dynamic usage.\n");
2669c2c66affSColin Finck if (!(desc->usage & (WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_RENDERTARGET | WINED3DUSAGE_DEPTHSTENCIL))
2670c2c66affSColin Finck && (flags & WINED3D_TEXTURE_CREATE_MAPPABLE))
26719987f029SAmine Khaldi WARN("Creating a mappable texture that doesn't specify dynamic usage.\n");
26729987f029SAmine Khaldi if (desc->usage & WINED3DUSAGE_RENDERTARGET && desc->access & WINED3D_RESOURCE_ACCESS_CPU)
26739987f029SAmine Khaldi FIXME("Trying to create a CPU accessible render target.\n");
2674c2c66affSColin Finck
2675c2c66affSColin Finck pow2_width = desc->width;
2676c2c66affSColin Finck pow2_height = desc->height;
2677*81cffd76SJoachim Henze if (((desc->width & (desc->width - 1)) || (desc->height & (desc->height - 1)))
2678c2c66affSColin Finck && !gl_info->supported[ARB_TEXTURE_NON_POWER_OF_TWO])
2679c2c66affSColin Finck {
2680c2c66affSColin Finck /* level_count == 0 returns an error as well. */
2681*81cffd76SJoachim Henze if (level_count != 1 || layer_count != 1)
2682c2c66affSColin Finck {
26839987f029SAmine Khaldi if (!(desc->usage & WINED3DUSAGE_SCRATCH))
2684c2c66affSColin Finck {
2685*81cffd76SJoachim Henze WARN("Attempted to create a mipmapped/cube/array NPOT texture without unconditional NPOT support.\n");
2686c2c66affSColin Finck return WINED3DERR_INVALIDCALL;
2687c2c66affSColin Finck }
2688c2c66affSColin Finck
2689c2c66affSColin Finck WARN("Creating a scratch mipmapped/cube/array NPOT texture despite lack of HW support.\n");
2690c2c66affSColin Finck }
2691c2c66affSColin Finck texture->flags |= WINED3D_TEXTURE_COND_NP2;
2692c2c66affSColin Finck
2693*81cffd76SJoachim Henze if (!gl_info->supported[ARB_TEXTURE_RECTANGLE] && !gl_info->supported[WINED3D_GL_NORMALIZED_TEXRECT])
2694c2c66affSColin Finck {
2695c2c66affSColin Finck const struct wined3d_format *format = wined3d_get_format(gl_info, desc->format, desc->usage);
2696c2c66affSColin Finck
2697c2c66affSColin Finck /* TODO: Add support for non-power-of-two compressed textures. */
2698c2c66affSColin Finck if (format->flags[WINED3D_GL_RES_TYPE_TEX_2D]
2699c2c66affSColin Finck & (WINED3DFMT_FLAG_COMPRESSED | WINED3DFMT_FLAG_HEIGHT_SCALE))
2700c2c66affSColin Finck {
2701c2c66affSColin Finck FIXME("Compressed or height scaled non-power-of-two (%ux%u) textures are not supported.\n",
2702c2c66affSColin Finck desc->width, desc->height);
2703c2c66affSColin Finck return WINED3DERR_NOTAVAILABLE;
2704c2c66affSColin Finck }
2705c2c66affSColin Finck
2706c2c66affSColin Finck /* Find the nearest pow2 match. */
2707c2c66affSColin Finck pow2_width = pow2_height = 1;
2708c2c66affSColin Finck while (pow2_width < desc->width)
2709c2c66affSColin Finck pow2_width <<= 1;
2710c2c66affSColin Finck while (pow2_height < desc->height)
2711c2c66affSColin Finck pow2_height <<= 1;
2712c2c66affSColin Finck texture->flags |= WINED3D_TEXTURE_COND_NP2_EMULATED;
2713c2c66affSColin Finck }
2714c2c66affSColin Finck }
2715c2c66affSColin Finck texture->pow2_width = pow2_width;
2716c2c66affSColin Finck texture->pow2_height = pow2_height;
2717c2c66affSColin Finck
2718c2c66affSColin Finck if ((pow2_width > gl_info->limits.texture_size || pow2_height > gl_info->limits.texture_size)
2719c2c66affSColin Finck && (desc->usage & WINED3DUSAGE_TEXTURE))
2720c2c66affSColin Finck {
2721c2c66affSColin Finck /* One of four options:
2722c2c66affSColin Finck * 1: Do the same as we do with NPOT and scale the texture. (Any
2723c2c66affSColin Finck * texture ops would require the texture to be scaled which is
2724c2c66affSColin Finck * potentially slow.)
2725c2c66affSColin Finck * 2: Set the texture to the maximum size (bad idea).
2726c2c66affSColin Finck * 3: WARN and return WINED3DERR_NOTAVAILABLE.
2727c2c66affSColin Finck * 4: Create the surface, but allow it to be used only for DirectDraw
2728c2c66affSColin Finck * Blts. Some apps (e.g. Swat 3) create textures with a height of
2729c2c66affSColin Finck * 16 and a width > 3000 and blt 16x16 letter areas from them to
2730c2c66affSColin Finck * the render target. */
27319987f029SAmine Khaldi if (desc->access & WINED3D_RESOURCE_ACCESS_GPU)
2732c2c66affSColin Finck {
2733c2c66affSColin Finck WARN("Dimensions (%ux%u) exceed the maximum texture size.\n", pow2_width, pow2_height);
2734c2c66affSColin Finck return WINED3DERR_NOTAVAILABLE;
2735c2c66affSColin Finck }
2736c2c66affSColin Finck
2737c2c66affSColin Finck /* We should never use this surface in combination with OpenGL. */
2738c2c66affSColin Finck TRACE("Creating an oversized (%ux%u) surface.\n", pow2_width, pow2_height);
2739c2c66affSColin Finck }
2740c2c66affSColin Finck
2741*81cffd76SJoachim Henze if (FAILED(hr = wined3d_texture_init(texture, &texture2d_ops, layer_count, level_count, desc,
2742*81cffd76SJoachim Henze flags, device, parent, parent_ops, &texture_resource_ops)))
2743c2c66affSColin Finck {
2744*81cffd76SJoachim Henze WARN("Failed to initialize texture, returning %#x.\n", hr);
27457af3969eSAmine Khaldi return hr;
2746c2c66affSColin Finck }
27477af3969eSAmine Khaldi
2748*81cffd76SJoachim Henze /* Precalculated scaling for 'faked' non power of two texture coords. */
2749*81cffd76SJoachim Henze if (texture->resource.gl_type == WINED3D_GL_RES_TYPE_TEX_RECT)
27507af3969eSAmine Khaldi {
2751*81cffd76SJoachim Henze texture->pow2_matrix[0] = (float)desc->width;
2752*81cffd76SJoachim Henze texture->pow2_matrix[5] = (float)desc->height;
2753*81cffd76SJoachim Henze texture->flags &= ~(WINED3D_TEXTURE_POW2_MAT_IDENT | WINED3D_TEXTURE_NORMALIZED_COORDS);
2754*81cffd76SJoachim Henze texture->target = GL_TEXTURE_RECTANGLE_ARB;
2755c2c66affSColin Finck }
2756c2c66affSColin Finck else
2757*81cffd76SJoachim Henze {
2758*81cffd76SJoachim Henze if (texture->flags & WINED3D_TEXTURE_COND_NP2_EMULATED)
2759*81cffd76SJoachim Henze {
2760*81cffd76SJoachim Henze texture->pow2_matrix[0] = (((float)desc->width) / ((float)pow2_width));
2761*81cffd76SJoachim Henze texture->pow2_matrix[5] = (((float)desc->height) / ((float)pow2_height));
2762*81cffd76SJoachim Henze texture->flags &= ~WINED3D_TEXTURE_POW2_MAT_IDENT;
2763*81cffd76SJoachim Henze }
2764*81cffd76SJoachim Henze else
2765*81cffd76SJoachim Henze {
2766*81cffd76SJoachim Henze texture->pow2_matrix[0] = 1.0f;
2767*81cffd76SJoachim Henze texture->pow2_matrix[5] = 1.0f;
2768*81cffd76SJoachim Henze }
2769c2c66affSColin Finck if (desc->usage & WINED3DUSAGE_LEGACY_CUBEMAP)
27709987f029SAmine Khaldi {
2771c2c66affSColin Finck texture->target = GL_TEXTURE_CUBE_MAP_ARB;
27729987f029SAmine Khaldi }
27739987f029SAmine Khaldi else if (desc->multisample_type && gl_info->supported[ARB_TEXTURE_MULTISAMPLE])
27749987f029SAmine Khaldi {
27759987f029SAmine Khaldi if (layer_count > 1)
27769987f029SAmine Khaldi texture->target = GL_TEXTURE_2D_MULTISAMPLE_ARRAY;
27779987f029SAmine Khaldi else
27789987f029SAmine Khaldi texture->target = GL_TEXTURE_2D_MULTISAMPLE;
27799987f029SAmine Khaldi }
27809987f029SAmine Khaldi else
27819987f029SAmine Khaldi {
27829987f029SAmine Khaldi if (layer_count > 1)
2783c2c66affSColin Finck texture->target = GL_TEXTURE_2D_ARRAY;
2784c2c66affSColin Finck else
2785c2c66affSColin Finck texture->target = GL_TEXTURE_2D;
2786c2c66affSColin Finck }
27879987f029SAmine Khaldi }
2788c2c66affSColin Finck texture->pow2_matrix[10] = 1.0f;
2789c2c66affSColin Finck texture->pow2_matrix[15] = 1.0f;
2790c2c66affSColin Finck TRACE("x scale %.8e, y scale %.8e.\n", texture->pow2_matrix[0], texture->pow2_matrix[5]);
2791c2c66affSColin Finck
2792c2c66affSColin Finck if (wined3d_texture_use_pbo(texture, gl_info))
2793c2c66affSColin Finck texture->resource.map_binding = WINED3D_LOCATION_BUFFER;
2794c2c66affSColin Finck
27959987f029SAmine Khaldi sub_count = level_count * layer_count;
2796*81cffd76SJoachim Henze if (sub_count / layer_count != level_count
2797*81cffd76SJoachim Henze || !(surfaces = heap_calloc(sub_count, sizeof(*surfaces))))
2798c2c66affSColin Finck {
2799c2c66affSColin Finck wined3d_texture_cleanup_sync(texture);
2800c2c66affSColin Finck return E_OUTOFMEMORY;
2801c2c66affSColin Finck }
2802c2c66affSColin Finck
28039987f029SAmine Khaldi if (desc->usage & WINED3DUSAGE_OVERLAY)
28049987f029SAmine Khaldi {
28059987f029SAmine Khaldi if (!(texture->overlay_info = heap_calloc(sub_count, sizeof(*texture->overlay_info))))
28069987f029SAmine Khaldi {
2807*81cffd76SJoachim Henze heap_free(surfaces);
28089987f029SAmine Khaldi wined3d_texture_cleanup_sync(texture);
28099987f029SAmine Khaldi return E_OUTOFMEMORY;
28109987f029SAmine Khaldi }
28119987f029SAmine Khaldi
28129987f029SAmine Khaldi for (i = 0; i < sub_count; ++i)
28139987f029SAmine Khaldi {
28149987f029SAmine Khaldi list_init(&texture->overlay_info[i].entry);
28159987f029SAmine Khaldi list_init(&texture->overlay_info[i].overlays);
28169987f029SAmine Khaldi }
28179987f029SAmine Khaldi }
28189987f029SAmine Khaldi
2819*81cffd76SJoachim Henze /* Generate all the surfaces. */
2820*81cffd76SJoachim Henze for (i = 0; i < texture->level_count; ++i)
2821*81cffd76SJoachim Henze {
2822*81cffd76SJoachim Henze for (j = 0; j < texture->layer_count; ++j)
2823c2c66affSColin Finck {
2824c2c66affSColin Finck struct wined3d_texture_sub_resource *sub_resource;
2825*81cffd76SJoachim Henze unsigned int idx = j * texture->level_count + i;
2826*81cffd76SJoachim Henze struct wined3d_surface *surface;
2827c2c66affSColin Finck
2828*81cffd76SJoachim Henze surface = &surfaces[idx];
2829*81cffd76SJoachim Henze surface->container = texture;
2830*81cffd76SJoachim Henze surface->texture_level = i;
2831*81cffd76SJoachim Henze surface->texture_layer = j;
2832*81cffd76SJoachim Henze list_init(&surface->renderbuffers);
2833*81cffd76SJoachim Henze
2834*81cffd76SJoachim Henze sub_resource = &texture->sub_resources[idx];
2835c2c66affSColin Finck sub_resource->locations = WINED3D_LOCATION_DISCARDED;
2836*81cffd76SJoachim Henze sub_resource->u.surface = surface;
2837*81cffd76SJoachim Henze if (!(texture->resource.usage & WINED3DUSAGE_DEPTHSTENCIL))
2838c2c66affSColin Finck {
2839*81cffd76SJoachim Henze wined3d_texture_validate_location(texture, idx, WINED3D_LOCATION_SYSMEM);
2840*81cffd76SJoachim Henze wined3d_texture_invalidate_location(texture, idx, ~WINED3D_LOCATION_SYSMEM);
2841c2c66affSColin Finck }
2842c2c66affSColin Finck
2843*81cffd76SJoachim Henze if (FAILED(hr = device_parent->ops->surface_created(device_parent,
2844*81cffd76SJoachim Henze texture, idx, &sub_resource->parent, &sub_resource->parent_ops)))
2845c2c66affSColin Finck {
2846*81cffd76SJoachim Henze WARN("Failed to create surface parent, hr %#x.\n", hr);
2847c2c66affSColin Finck sub_resource->parent = NULL;
2848c2c66affSColin Finck wined3d_texture_cleanup_sync(texture);
2849c2c66affSColin Finck return hr;
2850c2c66affSColin Finck }
2851c2c66affSColin Finck
2852c2c66affSColin Finck TRACE("parent %p, parent_ops %p.\n", sub_resource->parent, sub_resource->parent_ops);
2853c2c66affSColin Finck
2854*81cffd76SJoachim Henze TRACE("Created surface level %u, layer %u @ %p.\n", i, j, surface);
2855c2c66affSColin Finck
2856c2c66affSColin Finck if ((desc->usage & WINED3DUSAGE_OWNDC) || (device->wined3d->flags & WINED3D_NO3D))
2857c2c66affSColin Finck {
2858*81cffd76SJoachim Henze wined3d_cs_init_object(device->cs, texture2d_create_dc, surface);
2859c2c66affSColin Finck device->cs->ops->finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
2860*81cffd76SJoachim Henze if (!surface->dc)
2861c2c66affSColin Finck {
2862c2c66affSColin Finck wined3d_texture_cleanup_sync(texture);
2863c2c66affSColin Finck return WINED3DERR_INVALIDCALL;
2864c2c66affSColin Finck }
2865c2c66affSColin Finck }
2866c2c66affSColin Finck }
2867*81cffd76SJoachim Henze }
2868c2c66affSColin Finck
2869c2c66affSColin Finck return WINED3D_OK;
2870c2c66affSColin Finck }
2871c2c66affSColin Finck
2872*81cffd76SJoachim Henze /* This call just uploads data, the caller is responsible for binding the
2873*81cffd76SJoachim Henze * correct texture. */
2874*81cffd76SJoachim Henze /* Context activation is done by the caller. */
2875*81cffd76SJoachim Henze static void texture3d_upload_data(struct wined3d_texture *texture, unsigned int sub_resource_idx,
2876*81cffd76SJoachim Henze const struct wined3d_context *context, const struct wined3d_box *box,
2877*81cffd76SJoachim Henze const struct wined3d_const_bo_address *data, unsigned int row_pitch, unsigned int slice_pitch)
2878*81cffd76SJoachim Henze {
2879*81cffd76SJoachim Henze const struct wined3d_format *format = texture->resource.format;
2880*81cffd76SJoachim Henze unsigned int level = sub_resource_idx % texture->level_count;
2881*81cffd76SJoachim Henze const struct wined3d_gl_info *gl_info = context->gl_info;
2882*81cffd76SJoachim Henze unsigned int x, y, z, update_w, update_h, update_d;
2883*81cffd76SJoachim Henze unsigned int dst_row_pitch, dst_slice_pitch;
2884*81cffd76SJoachim Henze unsigned int width, height, depth;
2885*81cffd76SJoachim Henze const void *mem = data->addr;
2886*81cffd76SJoachim Henze void *converted_mem = NULL;
2887*81cffd76SJoachim Henze
2888*81cffd76SJoachim Henze TRACE("texture %p, sub_resource_idx %u, context %p, box %s, data {%#x:%p}, row_pitch %#x, slice_pitch %#x.\n",
2889*81cffd76SJoachim Henze texture, sub_resource_idx, context, debug_box(box),
2890*81cffd76SJoachim Henze data->buffer_object, data->addr, row_pitch, slice_pitch);
2891*81cffd76SJoachim Henze
2892*81cffd76SJoachim Henze width = wined3d_texture_get_level_width(texture, level);
2893*81cffd76SJoachim Henze height = wined3d_texture_get_level_height(texture, level);
2894*81cffd76SJoachim Henze depth = wined3d_texture_get_level_depth(texture, level);
2895*81cffd76SJoachim Henze
2896*81cffd76SJoachim Henze if (!box)
2897*81cffd76SJoachim Henze {
2898*81cffd76SJoachim Henze x = y = z = 0;
2899*81cffd76SJoachim Henze update_w = width;
2900*81cffd76SJoachim Henze update_h = height;
2901*81cffd76SJoachim Henze update_d = depth;
2902*81cffd76SJoachim Henze }
2903*81cffd76SJoachim Henze else
2904*81cffd76SJoachim Henze {
2905*81cffd76SJoachim Henze x = box->left;
2906*81cffd76SJoachim Henze y = box->top;
2907*81cffd76SJoachim Henze z = box->front;
2908*81cffd76SJoachim Henze update_w = box->right - box->left;
2909*81cffd76SJoachim Henze update_h = box->bottom - box->top;
2910*81cffd76SJoachim Henze update_d = box->back - box->front;
2911*81cffd76SJoachim Henze }
2912*81cffd76SJoachim Henze
2913*81cffd76SJoachim Henze if (format->conv_byte_count)
2914*81cffd76SJoachim Henze {
2915*81cffd76SJoachim Henze if (data->buffer_object)
2916*81cffd76SJoachim Henze ERR("Loading a converted texture from a PBO.\n");
2917*81cffd76SJoachim Henze if (texture->resource.format_flags & WINED3DFMT_FLAG_BLOCKS)
2918*81cffd76SJoachim Henze ERR("Converting a block-based format.\n");
2919*81cffd76SJoachim Henze
2920*81cffd76SJoachim Henze dst_row_pitch = update_w * format->conv_byte_count;
2921*81cffd76SJoachim Henze dst_slice_pitch = dst_row_pitch * update_h;
2922*81cffd76SJoachim Henze
2923*81cffd76SJoachim Henze converted_mem = heap_calloc(update_d, dst_slice_pitch);
2924*81cffd76SJoachim Henze format->upload(data->addr, converted_mem, row_pitch, slice_pitch,
2925*81cffd76SJoachim Henze dst_row_pitch, dst_slice_pitch, update_w, update_h, update_d);
2926*81cffd76SJoachim Henze mem = converted_mem;
2927*81cffd76SJoachim Henze }
2928*81cffd76SJoachim Henze else
2929*81cffd76SJoachim Henze {
2930*81cffd76SJoachim Henze wined3d_texture_get_pitch(texture, sub_resource_idx, &dst_row_pitch, &dst_slice_pitch);
2931*81cffd76SJoachim Henze if (row_pitch != dst_row_pitch || slice_pitch != dst_slice_pitch)
2932*81cffd76SJoachim Henze FIXME("Ignoring row/slice pitch (%u/%u).\n", row_pitch, slice_pitch);
2933*81cffd76SJoachim Henze }
2934*81cffd76SJoachim Henze
2935*81cffd76SJoachim Henze if (data->buffer_object)
2936*81cffd76SJoachim Henze {
2937*81cffd76SJoachim Henze GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, data->buffer_object));
2938*81cffd76SJoachim Henze checkGLcall("glBindBuffer");
2939*81cffd76SJoachim Henze }
2940*81cffd76SJoachim Henze
2941*81cffd76SJoachim Henze GL_EXTCALL(glTexSubImage3D(GL_TEXTURE_3D, level, x, y, z,
2942*81cffd76SJoachim Henze update_w, update_h, update_d, format->glFormat, format->glType, mem));
2943*81cffd76SJoachim Henze checkGLcall("glTexSubImage3D");
2944*81cffd76SJoachim Henze
2945*81cffd76SJoachim Henze if (data->buffer_object)
2946*81cffd76SJoachim Henze {
2947*81cffd76SJoachim Henze GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0));
2948*81cffd76SJoachim Henze checkGLcall("glBindBuffer");
2949*81cffd76SJoachim Henze }
2950*81cffd76SJoachim Henze
2951*81cffd76SJoachim Henze heap_free(converted_mem);
2952*81cffd76SJoachim Henze }
2953*81cffd76SJoachim Henze
2954c2c66affSColin Finck /* Context activation is done by the caller. */
2955c2c66affSColin Finck static void texture3d_download_data(struct wined3d_texture *texture, unsigned int sub_resource_idx,
2956c2c66affSColin Finck const struct wined3d_context *context, const struct wined3d_bo_address *data)
2957c2c66affSColin Finck {
2958c2c66affSColin Finck const struct wined3d_format *format = texture->resource.format;
2959c2c66affSColin Finck const struct wined3d_gl_info *gl_info = context->gl_info;
2960c2c66affSColin Finck
2961d6ac0a71SAmine Khaldi if (format->conv_byte_count)
2962c2c66affSColin Finck {
2963c2c66affSColin Finck FIXME("Attempting to download a converted volume, format %s.\n",
2964c2c66affSColin Finck debug_d3dformat(format->id));
2965c2c66affSColin Finck return;
2966c2c66affSColin Finck }
2967c2c66affSColin Finck
2968c2c66affSColin Finck if (data->buffer_object)
2969c2c66affSColin Finck {
2970c2c66affSColin Finck GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, data->buffer_object));
2971c2c66affSColin Finck checkGLcall("glBindBuffer");
2972c2c66affSColin Finck }
2973c2c66affSColin Finck
2974c2c66affSColin Finck gl_info->gl_ops.gl.p_glGetTexImage(GL_TEXTURE_3D, sub_resource_idx,
2975c2c66affSColin Finck format->glFormat, format->glType, data->addr);
2976c2c66affSColin Finck checkGLcall("glGetTexImage");
2977c2c66affSColin Finck
2978c2c66affSColin Finck if (data->buffer_object)
2979c2c66affSColin Finck {
2980c2c66affSColin Finck GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, 0));
2981c2c66affSColin Finck checkGLcall("glBindBuffer");
2982c2c66affSColin Finck }
2983c2c66affSColin Finck
2984c2c66affSColin Finck }
2985c2c66affSColin Finck
2986c2c66affSColin Finck /* Context activation is done by the caller. */
2987c2c66affSColin Finck static void texture3d_srgb_transfer(struct wined3d_texture *texture, unsigned int sub_resource_idx,
2988c2c66affSColin Finck struct wined3d_context *context, BOOL dest_is_srgb)
2989c2c66affSColin Finck {
2990c2c66affSColin Finck struct wined3d_texture_sub_resource *sub_resource = &texture->sub_resources[sub_resource_idx];
2991c2c66affSColin Finck unsigned int row_pitch, slice_pitch;
2992c2c66affSColin Finck struct wined3d_bo_address data;
2993c2c66affSColin Finck
2994c2c66affSColin Finck /* Optimisations are possible, but the effort should be put into either
2995c2c66affSColin Finck * implementing EXT_SRGB_DECODE in the driver or finding out why we
2996c2c66affSColin Finck * picked the wrong copy for the original upload and fixing that.
2997c2c66affSColin Finck *
2998c2c66affSColin Finck * Also keep in mind that we want to avoid using resource.heap_memory
2999c2c66affSColin Finck * for DEFAULT pool surfaces. */
3000c2c66affSColin Finck WARN_(d3d_perf)("Performing slow rgb/srgb volume transfer.\n");
3001c2c66affSColin Finck data.buffer_object = 0;
30029987f029SAmine Khaldi if (!(data.addr = heap_alloc(sub_resource->size)))
3003c2c66affSColin Finck return;
3004c2c66affSColin Finck
3005c2c66affSColin Finck wined3d_texture_get_pitch(texture, sub_resource_idx, &row_pitch, &slice_pitch);
3006c2c66affSColin Finck wined3d_texture_bind_and_dirtify(texture, context, !dest_is_srgb);
3007c2c66affSColin Finck texture3d_download_data(texture, sub_resource_idx, context, &data);
3008c2c66affSColin Finck wined3d_texture_bind_and_dirtify(texture, context, dest_is_srgb);
3009*81cffd76SJoachim Henze texture3d_upload_data(texture, sub_resource_idx, context,
3010*81cffd76SJoachim Henze NULL, wined3d_const_bo_address(&data), row_pitch, slice_pitch);
3011c2c66affSColin Finck
30129987f029SAmine Khaldi heap_free(data.addr);
3013c2c66affSColin Finck }
3014c2c66affSColin Finck
3015c2c66affSColin Finck /* Context activation is done by the caller. */
3016c2c66affSColin Finck static BOOL texture3d_load_location(struct wined3d_texture *texture, unsigned int sub_resource_idx,
3017c2c66affSColin Finck struct wined3d_context *context, DWORD location)
3018c2c66affSColin Finck {
3019c2c66affSColin Finck struct wined3d_texture_sub_resource *sub_resource = &texture->sub_resources[sub_resource_idx];
3020c2c66affSColin Finck unsigned int row_pitch, slice_pitch;
3021c2c66affSColin Finck
3022c2c66affSColin Finck if (!wined3d_texture_prepare_location(texture, sub_resource_idx, context, location))
3023c2c66affSColin Finck return FALSE;
3024c2c66affSColin Finck
3025c2c66affSColin Finck switch (location)
3026c2c66affSColin Finck {
3027c2c66affSColin Finck case WINED3D_LOCATION_TEXTURE_RGB:
3028c2c66affSColin Finck case WINED3D_LOCATION_TEXTURE_SRGB:
3029c2c66affSColin Finck if (sub_resource->locations & WINED3D_LOCATION_SYSMEM)
3030c2c66affSColin Finck {
3031c2c66affSColin Finck struct wined3d_const_bo_address data = {0, texture->resource.heap_memory};
3032c2c66affSColin Finck data.addr += sub_resource->offset;
3033c2c66affSColin Finck wined3d_texture_bind_and_dirtify(texture, context,
3034c2c66affSColin Finck location == WINED3D_LOCATION_TEXTURE_SRGB);
3035c2c66affSColin Finck wined3d_texture_get_pitch(texture, sub_resource_idx, &row_pitch, &slice_pitch);
3036*81cffd76SJoachim Henze texture3d_upload_data(texture, sub_resource_idx, context, NULL, &data, row_pitch, slice_pitch);
3037c2c66affSColin Finck }
3038c2c66affSColin Finck else if (sub_resource->locations & WINED3D_LOCATION_BUFFER)
3039c2c66affSColin Finck {
3040*81cffd76SJoachim Henze #if !defined(STAGING_CSMT)
3041c2c66affSColin Finck struct wined3d_const_bo_address data = {sub_resource->buffer_object, NULL};
3042*81cffd76SJoachim Henze #else /* STAGING_CSMT */
3043*81cffd76SJoachim Henze struct wined3d_const_bo_address data = {sub_resource->buffer->name, NULL};
3044*81cffd76SJoachim Henze #endif /* STAGING_CSMT */
3045c2c66affSColin Finck wined3d_texture_bind_and_dirtify(texture, context,
3046c2c66affSColin Finck location == WINED3D_LOCATION_TEXTURE_SRGB);
3047c2c66affSColin Finck wined3d_texture_get_pitch(texture, sub_resource_idx, &row_pitch, &slice_pitch);
3048*81cffd76SJoachim Henze texture3d_upload_data(texture, sub_resource_idx, context, NULL, &data, row_pitch, slice_pitch);
3049c2c66affSColin Finck }
3050c2c66affSColin Finck else if (sub_resource->locations & WINED3D_LOCATION_TEXTURE_RGB)
3051c2c66affSColin Finck {
3052c2c66affSColin Finck texture3d_srgb_transfer(texture, sub_resource_idx, context, TRUE);
3053c2c66affSColin Finck }
3054c2c66affSColin Finck else if (sub_resource->locations & WINED3D_LOCATION_TEXTURE_SRGB)
3055c2c66affSColin Finck {
3056c2c66affSColin Finck texture3d_srgb_transfer(texture, sub_resource_idx, context, FALSE);
3057c2c66affSColin Finck }
3058c2c66affSColin Finck else
3059c2c66affSColin Finck {
3060c2c66affSColin Finck FIXME("Implement texture loading from %s.\n", wined3d_debug_location(sub_resource->locations));
3061c2c66affSColin Finck return FALSE;
3062c2c66affSColin Finck }
3063c2c66affSColin Finck break;
3064c2c66affSColin Finck
3065c2c66affSColin Finck case WINED3D_LOCATION_SYSMEM:
3066c2c66affSColin Finck if (sub_resource->locations & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB))
3067c2c66affSColin Finck {
3068c2c66affSColin Finck struct wined3d_bo_address data = {0, texture->resource.heap_memory};
3069c2c66affSColin Finck
3070c2c66affSColin Finck data.addr += sub_resource->offset;
3071c2c66affSColin Finck if (sub_resource->locations & WINED3D_LOCATION_TEXTURE_RGB)
3072c2c66affSColin Finck wined3d_texture_bind_and_dirtify(texture, context, FALSE);
3073c2c66affSColin Finck else
3074c2c66affSColin Finck wined3d_texture_bind_and_dirtify(texture, context, TRUE);
3075c2c66affSColin Finck
3076c2c66affSColin Finck texture3d_download_data(texture, sub_resource_idx, context, &data);
3077c2c66affSColin Finck ++texture->download_count;
3078c2c66affSColin Finck }
3079c2c66affSColin Finck else
3080c2c66affSColin Finck {
3081c2c66affSColin Finck FIXME("Implement WINED3D_LOCATION_SYSMEM loading from %s.\n",
3082c2c66affSColin Finck wined3d_debug_location(sub_resource->locations));
3083c2c66affSColin Finck return FALSE;
3084c2c66affSColin Finck }
3085c2c66affSColin Finck break;
3086c2c66affSColin Finck
3087c2c66affSColin Finck case WINED3D_LOCATION_BUFFER:
3088c2c66affSColin Finck if (sub_resource->locations & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB))
3089c2c66affSColin Finck {
3090*81cffd76SJoachim Henze #if !defined(STAGING_CSMT)
3091c2c66affSColin Finck struct wined3d_bo_address data = {sub_resource->buffer_object, NULL};
3092*81cffd76SJoachim Henze #else /* STAGING_CSMT */
3093*81cffd76SJoachim Henze struct wined3d_bo_address data = {sub_resource->buffer->name, NULL};
3094*81cffd76SJoachim Henze #endif /* STAGING_CSMT */
3095c2c66affSColin Finck
3096c2c66affSColin Finck if (sub_resource->locations & WINED3D_LOCATION_TEXTURE_RGB)
3097c2c66affSColin Finck wined3d_texture_bind_and_dirtify(texture, context, FALSE);
3098c2c66affSColin Finck else
3099c2c66affSColin Finck wined3d_texture_bind_and_dirtify(texture, context, TRUE);
3100c2c66affSColin Finck
3101c2c66affSColin Finck texture3d_download_data(texture, sub_resource_idx, context, &data);
3102c2c66affSColin Finck }
3103c2c66affSColin Finck else
3104c2c66affSColin Finck {
3105c2c66affSColin Finck FIXME("Implement WINED3D_LOCATION_BUFFER loading from %s.\n",
3106c2c66affSColin Finck wined3d_debug_location(sub_resource->locations));
3107c2c66affSColin Finck return FALSE;
3108c2c66affSColin Finck }
3109c2c66affSColin Finck break;
3110c2c66affSColin Finck
3111c2c66affSColin Finck default:
3112c2c66affSColin Finck FIXME("Implement %s loading from %s.\n", wined3d_debug_location(location),
3113c2c66affSColin Finck wined3d_debug_location(sub_resource->locations));
3114c2c66affSColin Finck return FALSE;
3115c2c66affSColin Finck }
3116c2c66affSColin Finck
3117c2c66affSColin Finck return TRUE;
3118c2c66affSColin Finck }
3119c2c66affSColin Finck
3120*81cffd76SJoachim Henze static void texture3d_prepare_texture(struct wined3d_texture *texture, struct wined3d_context *context, BOOL srgb)
3121*81cffd76SJoachim Henze {
3122*81cffd76SJoachim Henze const struct wined3d_format *format = texture->resource.format;
3123*81cffd76SJoachim Henze GLenum internal = srgb ? format->glGammaInternal : format->glInternal;
3124*81cffd76SJoachim Henze unsigned int sub_count = texture->level_count * texture->layer_count;
3125*81cffd76SJoachim Henze const struct wined3d_gl_info *gl_info = context->gl_info;
3126*81cffd76SJoachim Henze unsigned int i;
3127*81cffd76SJoachim Henze
3128*81cffd76SJoachim Henze wined3d_texture_bind_and_dirtify(texture, context, srgb);
3129*81cffd76SJoachim Henze
3130*81cffd76SJoachim Henze if (wined3d_texture_use_immutable_storage(texture, gl_info))
3131*81cffd76SJoachim Henze {
3132*81cffd76SJoachim Henze GL_EXTCALL(glTexStorage3D(GL_TEXTURE_3D, texture->level_count, internal,
3133*81cffd76SJoachim Henze wined3d_texture_get_level_width(texture, 0),
3134*81cffd76SJoachim Henze wined3d_texture_get_level_height(texture, 0),
3135*81cffd76SJoachim Henze wined3d_texture_get_level_depth(texture, 0)));
3136*81cffd76SJoachim Henze checkGLcall("glTexStorage3D");
3137*81cffd76SJoachim Henze }
3138*81cffd76SJoachim Henze else
3139*81cffd76SJoachim Henze {
3140*81cffd76SJoachim Henze for (i = 0; i < sub_count; ++i)
3141*81cffd76SJoachim Henze {
3142*81cffd76SJoachim Henze GL_EXTCALL(glTexImage3D(GL_TEXTURE_3D, i, internal,
3143*81cffd76SJoachim Henze wined3d_texture_get_level_width(texture, i),
3144*81cffd76SJoachim Henze wined3d_texture_get_level_height(texture, i),
3145*81cffd76SJoachim Henze wined3d_texture_get_level_depth(texture, i),
3146*81cffd76SJoachim Henze 0, format->glFormat, format->glType, NULL));
3147*81cffd76SJoachim Henze checkGLcall("glTexImage3D");
3148*81cffd76SJoachim Henze }
3149*81cffd76SJoachim Henze }
3150*81cffd76SJoachim Henze }
3151*81cffd76SJoachim Henze
3152*81cffd76SJoachim Henze static void texture3d_cleanup_sub_resources(struct wined3d_texture *texture)
3153*81cffd76SJoachim Henze {
3154*81cffd76SJoachim Henze }
3155*81cffd76SJoachim Henze
3156c2c66affSColin Finck static const struct wined3d_texture_ops texture3d_ops =
3157c2c66affSColin Finck {
3158*81cffd76SJoachim Henze texture3d_upload_data,
3159c2c66affSColin Finck texture3d_load_location,
3160*81cffd76SJoachim Henze texture3d_prepare_texture,
3161*81cffd76SJoachim Henze texture3d_cleanup_sub_resources,
3162c2c66affSColin Finck };
3163c2c66affSColin Finck
3164*81cffd76SJoachim Henze static HRESULT volumetexture_init(struct wined3d_texture *texture, const struct wined3d_resource_desc *desc,
3165*81cffd76SJoachim Henze UINT layer_count, UINT level_count, DWORD flags, struct wined3d_device *device, void *parent,
3166*81cffd76SJoachim Henze const struct wined3d_parent_ops *parent_ops)
3167*81cffd76SJoachim Henze {
3168*81cffd76SJoachim Henze struct wined3d_device_parent *device_parent = device->device_parent;
3169*81cffd76SJoachim Henze const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
3170*81cffd76SJoachim Henze unsigned int i;
3171*81cffd76SJoachim Henze HRESULT hr;
3172*81cffd76SJoachim Henze
3173*81cffd76SJoachim Henze if (layer_count != 1)
3174*81cffd76SJoachim Henze {
3175*81cffd76SJoachim Henze ERR("Invalid layer count for volume texture.\n");
3176*81cffd76SJoachim Henze return E_INVALIDARG;
3177*81cffd76SJoachim Henze }
3178*81cffd76SJoachim Henze
3179*81cffd76SJoachim Henze /* TODO: It should only be possible to create textures for formats
3180*81cffd76SJoachim Henze * that are reported as supported. */
3181*81cffd76SJoachim Henze if (WINED3DFMT_UNKNOWN >= desc->format)
3182*81cffd76SJoachim Henze {
3183*81cffd76SJoachim Henze WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture);
3184*81cffd76SJoachim Henze return WINED3DERR_INVALIDCALL;
3185*81cffd76SJoachim Henze }
3186*81cffd76SJoachim Henze
3187*81cffd76SJoachim Henze if (!gl_info->supported[EXT_TEXTURE3D])
3188*81cffd76SJoachim Henze {
3189*81cffd76SJoachim Henze WARN("(%p) : Texture cannot be created - no volume texture support.\n", texture);
3190*81cffd76SJoachim Henze return WINED3DERR_INVALIDCALL;
3191*81cffd76SJoachim Henze }
3192*81cffd76SJoachim Henze
3193*81cffd76SJoachim Henze if (desc->usage & WINED3DUSAGE_DYNAMIC && (wined3d_resource_access_is_managed(desc->access)
3194*81cffd76SJoachim Henze || desc->usage & WINED3DUSAGE_SCRATCH))
3195*81cffd76SJoachim Henze {
3196*81cffd76SJoachim Henze WARN("Attempted to create a DYNAMIC texture with access %s.\n",
3197*81cffd76SJoachim Henze wined3d_debug_resource_access(desc->access));
3198*81cffd76SJoachim Henze return WINED3DERR_INVALIDCALL;
3199*81cffd76SJoachim Henze }
3200*81cffd76SJoachim Henze
3201*81cffd76SJoachim Henze if (!gl_info->supported[ARB_TEXTURE_NON_POWER_OF_TWO])
3202*81cffd76SJoachim Henze {
3203*81cffd76SJoachim Henze if (!is_power_of_two(desc->width) || !is_power_of_two(desc->height) || !is_power_of_two(desc->depth))
3204*81cffd76SJoachim Henze {
3205*81cffd76SJoachim Henze if (desc->usage & WINED3DUSAGE_SCRATCH)
3206*81cffd76SJoachim Henze {
3207*81cffd76SJoachim Henze WARN("Creating a scratch NPOT volume texture despite lack of HW support.\n");
3208*81cffd76SJoachim Henze }
3209*81cffd76SJoachim Henze else
3210*81cffd76SJoachim Henze {
3211*81cffd76SJoachim Henze WARN("Attempted to create a NPOT volume texture (%u, %u, %u) without GL support.\n",
3212*81cffd76SJoachim Henze desc->width, desc->height, desc->depth);
3213*81cffd76SJoachim Henze return WINED3DERR_INVALIDCALL;
3214*81cffd76SJoachim Henze }
3215*81cffd76SJoachim Henze }
3216*81cffd76SJoachim Henze }
3217*81cffd76SJoachim Henze
3218*81cffd76SJoachim Henze if (FAILED(hr = wined3d_texture_init(texture, &texture3d_ops, 1, level_count, desc,
3219*81cffd76SJoachim Henze flags, device, parent, parent_ops, &texture_resource_ops)))
3220*81cffd76SJoachim Henze {
3221*81cffd76SJoachim Henze WARN("Failed to initialize texture, returning %#x.\n", hr);
3222*81cffd76SJoachim Henze return hr;
3223*81cffd76SJoachim Henze }
3224*81cffd76SJoachim Henze
3225*81cffd76SJoachim Henze texture->pow2_matrix[0] = 1.0f;
3226*81cffd76SJoachim Henze texture->pow2_matrix[5] = 1.0f;
3227*81cffd76SJoachim Henze texture->pow2_matrix[10] = 1.0f;
3228*81cffd76SJoachim Henze texture->pow2_matrix[15] = 1.0f;
3229*81cffd76SJoachim Henze texture->target = GL_TEXTURE_3D;
3230*81cffd76SJoachim Henze
3231*81cffd76SJoachim Henze if (wined3d_texture_use_pbo(texture, gl_info))
3232*81cffd76SJoachim Henze {
3233*81cffd76SJoachim Henze wined3d_resource_free_sysmem(&texture->resource);
3234*81cffd76SJoachim Henze texture->resource.map_binding = WINED3D_LOCATION_BUFFER;
3235*81cffd76SJoachim Henze }
3236*81cffd76SJoachim Henze
3237*81cffd76SJoachim Henze /* Generate all the sub resources. */
3238*81cffd76SJoachim Henze for (i = 0; i < texture->level_count; ++i)
3239*81cffd76SJoachim Henze {
3240*81cffd76SJoachim Henze struct wined3d_texture_sub_resource *sub_resource;
3241*81cffd76SJoachim Henze
3242*81cffd76SJoachim Henze sub_resource = &texture->sub_resources[i];
3243*81cffd76SJoachim Henze sub_resource->locations = WINED3D_LOCATION_DISCARDED;
3244*81cffd76SJoachim Henze
3245*81cffd76SJoachim Henze if (FAILED(hr = device_parent->ops->volume_created(device_parent,
3246*81cffd76SJoachim Henze texture, i, &sub_resource->parent, &sub_resource->parent_ops)))
3247*81cffd76SJoachim Henze {
3248*81cffd76SJoachim Henze WARN("Failed to create volume parent, hr %#x.\n", hr);
3249*81cffd76SJoachim Henze sub_resource->parent = NULL;
3250*81cffd76SJoachim Henze wined3d_texture_cleanup_sync(texture);
3251*81cffd76SJoachim Henze return hr;
3252*81cffd76SJoachim Henze }
3253*81cffd76SJoachim Henze
3254*81cffd76SJoachim Henze TRACE("parent %p, parent_ops %p.\n", parent, parent_ops);
3255*81cffd76SJoachim Henze
3256*81cffd76SJoachim Henze TRACE("Created volume level %u.\n", i);
3257*81cffd76SJoachim Henze }
3258*81cffd76SJoachim Henze
3259*81cffd76SJoachim Henze return WINED3D_OK;
3260*81cffd76SJoachim Henze }
3261*81cffd76SJoachim Henze
3262c2c66affSColin Finck HRESULT CDECL wined3d_texture_blt(struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx,
3263c2c66affSColin Finck const RECT *dst_rect, struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx,
3264c2c66affSColin Finck const RECT *src_rect, DWORD flags, const struct wined3d_blt_fx *fx, enum wined3d_texture_filter_type filter)
3265c2c66affSColin Finck {
3266c2c66affSColin Finck struct wined3d_box src_box = {src_rect->left, src_rect->top, src_rect->right, src_rect->bottom, 0, 1};
3267c2c66affSColin Finck struct wined3d_box dst_box = {dst_rect->left, dst_rect->top, dst_rect->right, dst_rect->bottom, 0, 1};
3268c2c66affSColin Finck unsigned int dst_format_flags, src_format_flags = 0;
3269c2c66affSColin Finck HRESULT hr;
3270c2c66affSColin Finck
3271c2c66affSColin Finck TRACE("dst_texture %p, dst_sub_resource_idx %u, dst_rect %s, src_texture %p, "
3272c2c66affSColin Finck "src_sub_resource_idx %u, src_rect %s, flags %#x, fx %p, filter %s.\n",
3273c2c66affSColin Finck dst_texture, dst_sub_resource_idx, wine_dbgstr_rect(dst_rect), src_texture,
3274c2c66affSColin Finck src_sub_resource_idx, wine_dbgstr_rect(src_rect), flags, fx, debug_d3dtexturefiltertype(filter));
3275c2c66affSColin Finck
3276c2c66affSColin Finck if (dst_sub_resource_idx >= dst_texture->level_count * dst_texture->layer_count
3277c2c66affSColin Finck || dst_texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
3278c2c66affSColin Finck return WINED3DERR_INVALIDCALL;
3279c2c66affSColin Finck
3280c2c66affSColin Finck if (src_sub_resource_idx >= src_texture->level_count * src_texture->layer_count
3281c2c66affSColin Finck || src_texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
3282c2c66affSColin Finck return WINED3DERR_INVALIDCALL;
3283c2c66affSColin Finck
3284c2c66affSColin Finck dst_format_flags = dst_texture->resource.format_flags;
3285c2c66affSColin Finck if (FAILED(hr = wined3d_texture_check_box_dimensions(dst_texture,
3286c2c66affSColin Finck dst_sub_resource_idx % dst_texture->level_count, &dst_box)))
3287c2c66affSColin Finck return hr;
3288c2c66affSColin Finck
3289c2c66affSColin Finck src_format_flags = src_texture->resource.format_flags;
3290c2c66affSColin Finck if (FAILED(hr = wined3d_texture_check_box_dimensions(src_texture,
3291c2c66affSColin Finck src_sub_resource_idx % src_texture->level_count, &src_box)))
3292c2c66affSColin Finck return hr;
3293c2c66affSColin Finck
3294c2c66affSColin Finck if (dst_texture->sub_resources[dst_sub_resource_idx].map_count
3295c2c66affSColin Finck || src_texture->sub_resources[src_sub_resource_idx].map_count)
3296c2c66affSColin Finck {
3297*81cffd76SJoachim Henze #if !defined(STAGING_CSMT)
3298c2c66affSColin Finck WARN("Sub-resource is busy, returning WINEDDERR_SURFACEBUSY.\n");
3299c2c66affSColin Finck return WINEDDERR_SURFACEBUSY;
3300*81cffd76SJoachim Henze #else /* STAGING_CSMT */
3301*81cffd76SJoachim Henze struct wined3d_device *device = dst_texture->resource.device;
3302*81cffd76SJoachim Henze device->cs->ops->finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
3303*81cffd76SJoachim Henze if (dst_texture->sub_resources[dst_sub_resource_idx].map_count
3304*81cffd76SJoachim Henze || (src_texture && src_texture->sub_resources[src_sub_resource_idx].map_count))
3305*81cffd76SJoachim Henze {
3306*81cffd76SJoachim Henze WARN("Sub-resource is busy, returning WINEDDERR_SURFACEBUSY.\n");
3307*81cffd76SJoachim Henze return WINEDDERR_SURFACEBUSY;
3308*81cffd76SJoachim Henze }
3309*81cffd76SJoachim Henze #endif /* STAGING_CSMT */
3310c2c66affSColin Finck }
3311c2c66affSColin Finck
3312c2c66affSColin Finck if ((src_format_flags & (WINED3DFMT_FLAG_DEPTH | WINED3DFMT_FLAG_STENCIL))
3313c2c66affSColin Finck != (dst_format_flags & (WINED3DFMT_FLAG_DEPTH | WINED3DFMT_FLAG_STENCIL)))
3314c2c66affSColin Finck {
3315c2c66affSColin Finck WARN("Rejecting depth/stencil blit between incompatible formats.\n");
3316c2c66affSColin Finck return WINED3DERR_INVALIDCALL;
3317c2c66affSColin Finck }
3318c2c66affSColin Finck
3319c2c66affSColin Finck wined3d_cs_emit_blt_sub_resource(dst_texture->resource.device->cs, &dst_texture->resource, dst_sub_resource_idx,
3320c2c66affSColin Finck &dst_box, &src_texture->resource, src_sub_resource_idx, &src_box, flags, fx, filter);
3321c2c66affSColin Finck
3322c2c66affSColin Finck return WINED3D_OK;
3323c2c66affSColin Finck }
3324c2c66affSColin Finck
3325c2c66affSColin Finck HRESULT CDECL wined3d_texture_get_overlay_position(const struct wined3d_texture *texture,
3326c2c66affSColin Finck unsigned int sub_resource_idx, LONG *x, LONG *y)
3327c2c66affSColin Finck {
33289987f029SAmine Khaldi struct wined3d_overlay_info *overlay;
3329c2c66affSColin Finck
3330c2c66affSColin Finck TRACE("texture %p, sub_resource_idx %u, x %p, y %p.\n", texture, sub_resource_idx, x, y);
3331c2c66affSColin Finck
33329987f029SAmine Khaldi if (!(texture->resource.usage & WINED3DUSAGE_OVERLAY)
3333c2c66affSColin Finck || sub_resource_idx >= texture->level_count * texture->layer_count)
3334c2c66affSColin Finck {
3335c2c66affSColin Finck WARN("Invalid sub-resource specified.\n");
3336c2c66affSColin Finck return WINEDDERR_NOTAOVERLAYSURFACE;
3337c2c66affSColin Finck }
3338c2c66affSColin Finck
33399987f029SAmine Khaldi overlay = &texture->overlay_info[sub_resource_idx];
3340*81cffd76SJoachim Henze if (!overlay->dst)
3341c2c66affSColin Finck {
3342c2c66affSColin Finck TRACE("Overlay not visible.\n");
3343c2c66affSColin Finck *x = 0;
3344c2c66affSColin Finck *y = 0;
3345c2c66affSColin Finck return WINEDDERR_OVERLAYNOTVISIBLE;
3346c2c66affSColin Finck }
3347c2c66affSColin Finck
33489987f029SAmine Khaldi *x = overlay->dst_rect.left;
33499987f029SAmine Khaldi *y = overlay->dst_rect.top;
3350c2c66affSColin Finck
3351c2c66affSColin Finck TRACE("Returning position %d, %d.\n", *x, *y);
3352c2c66affSColin Finck
3353c2c66affSColin Finck return WINED3D_OK;
3354c2c66affSColin Finck }
3355c2c66affSColin Finck
3356c2c66affSColin Finck HRESULT CDECL wined3d_texture_set_overlay_position(struct wined3d_texture *texture,
3357c2c66affSColin Finck unsigned int sub_resource_idx, LONG x, LONG y)
3358c2c66affSColin Finck {
33599987f029SAmine Khaldi struct wined3d_overlay_info *overlay;
3360c2c66affSColin Finck LONG w, h;
3361c2c66affSColin Finck
3362c2c66affSColin Finck TRACE("texture %p, sub_resource_idx %u, x %d, y %d.\n", texture, sub_resource_idx, x, y);
3363c2c66affSColin Finck
33649987f029SAmine Khaldi if (!(texture->resource.usage & WINED3DUSAGE_OVERLAY)
33659987f029SAmine Khaldi || sub_resource_idx >= texture->level_count * texture->layer_count)
3366c2c66affSColin Finck {
3367c2c66affSColin Finck WARN("Invalid sub-resource specified.\n");
3368c2c66affSColin Finck return WINEDDERR_NOTAOVERLAYSURFACE;
3369c2c66affSColin Finck }
3370c2c66affSColin Finck
33719987f029SAmine Khaldi overlay = &texture->overlay_info[sub_resource_idx];
33729987f029SAmine Khaldi w = overlay->dst_rect.right - overlay->dst_rect.left;
33739987f029SAmine Khaldi h = overlay->dst_rect.bottom - overlay->dst_rect.top;
33749987f029SAmine Khaldi SetRect(&overlay->dst_rect, x, y, x + w, y + h);
3375c2c66affSColin Finck
3376c2c66affSColin Finck return WINED3D_OK;
3377c2c66affSColin Finck }
3378c2c66affSColin Finck
3379c2c66affSColin Finck HRESULT CDECL wined3d_texture_update_overlay(struct wined3d_texture *texture, unsigned int sub_resource_idx,
3380c2c66affSColin Finck const RECT *src_rect, struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx,
3381c2c66affSColin Finck const RECT *dst_rect, DWORD flags)
3382c2c66affSColin Finck {
3383*81cffd76SJoachim Henze struct wined3d_texture_sub_resource *sub_resource, *dst_sub_resource;
3384*81cffd76SJoachim Henze struct wined3d_surface *surface, *dst_surface;
33859987f029SAmine Khaldi struct wined3d_overlay_info *overlay;
3386c2c66affSColin Finck
3387c2c66affSColin Finck TRACE("texture %p, sub_resource_idx %u, src_rect %s, dst_texture %p, "
3388c2c66affSColin Finck "dst_sub_resource_idx %u, dst_rect %s, flags %#x.\n",
3389c2c66affSColin Finck texture, sub_resource_idx, wine_dbgstr_rect(src_rect), dst_texture,
3390c2c66affSColin Finck dst_sub_resource_idx, wine_dbgstr_rect(dst_rect), flags);
3391c2c66affSColin Finck
3392c2c66affSColin Finck if (!(texture->resource.usage & WINED3DUSAGE_OVERLAY) || texture->resource.type != WINED3D_RTYPE_TEXTURE_2D
3393*81cffd76SJoachim Henze || !(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
3394c2c66affSColin Finck {
3395c2c66affSColin Finck WARN("Invalid sub-resource specified.\n");
3396c2c66affSColin Finck return WINEDDERR_NOTAOVERLAYSURFACE;
3397c2c66affSColin Finck }
3398c2c66affSColin Finck
3399c2c66affSColin Finck if (!dst_texture || dst_texture->resource.type != WINED3D_RTYPE_TEXTURE_2D
3400*81cffd76SJoachim Henze || !(dst_sub_resource = wined3d_texture_get_sub_resource(dst_texture, dst_sub_resource_idx)))
3401c2c66affSColin Finck {
3402c2c66affSColin Finck WARN("Invalid destination sub-resource specified.\n");
3403c2c66affSColin Finck return WINED3DERR_INVALIDCALL;
3404c2c66affSColin Finck }
3405c2c66affSColin Finck
34069987f029SAmine Khaldi overlay = &texture->overlay_info[sub_resource_idx];
34079987f029SAmine Khaldi
3408*81cffd76SJoachim Henze surface = sub_resource->u.surface;
3409c2c66affSColin Finck if (src_rect)
34109987f029SAmine Khaldi overlay->src_rect = *src_rect;
3411c2c66affSColin Finck else
34129987f029SAmine Khaldi SetRect(&overlay->src_rect, 0, 0,
3413*81cffd76SJoachim Henze wined3d_texture_get_level_width(texture, surface->texture_level),
3414*81cffd76SJoachim Henze wined3d_texture_get_level_height(texture, surface->texture_level));
3415c2c66affSColin Finck
3416*81cffd76SJoachim Henze dst_surface = dst_sub_resource->u.surface;
3417c2c66affSColin Finck if (dst_rect)
34189987f029SAmine Khaldi overlay->dst_rect = *dst_rect;
3419c2c66affSColin Finck else
34209987f029SAmine Khaldi SetRect(&overlay->dst_rect, 0, 0,
3421*81cffd76SJoachim Henze wined3d_texture_get_level_width(dst_texture, dst_surface->texture_level),
3422*81cffd76SJoachim Henze wined3d_texture_get_level_height(dst_texture, dst_surface->texture_level));
3423c2c66affSColin Finck
3424*81cffd76SJoachim Henze if (overlay->dst && (overlay->dst != dst_surface || flags & WINEDDOVER_HIDE))
3425c2c66affSColin Finck {
3426*81cffd76SJoachim Henze overlay->dst = NULL;
34279987f029SAmine Khaldi list_remove(&overlay->entry);
3428c2c66affSColin Finck }
3429c2c66affSColin Finck
3430c2c66affSColin Finck if (flags & WINEDDOVER_SHOW)
3431c2c66affSColin Finck {
3432*81cffd76SJoachim Henze if (overlay->dst != dst_surface)
3433c2c66affSColin Finck {
3434*81cffd76SJoachim Henze overlay->dst = dst_surface;
34359987f029SAmine Khaldi list_add_tail(&texture->overlay_info[dst_sub_resource_idx].overlays, &overlay->entry);
3436c2c66affSColin Finck }
3437c2c66affSColin Finck }
3438c2c66affSColin Finck else if (flags & WINEDDOVER_HIDE)
3439c2c66affSColin Finck {
3440c2c66affSColin Finck /* Tests show that the rectangles are erased on hide. */
34419987f029SAmine Khaldi SetRectEmpty(&overlay->src_rect);
34429987f029SAmine Khaldi SetRectEmpty(&overlay->dst_rect);
3443*81cffd76SJoachim Henze overlay->dst = NULL;
3444c2c66affSColin Finck }
3445c2c66affSColin Finck
3446c2c66affSColin Finck return WINED3D_OK;
3447c2c66affSColin Finck }
3448c2c66affSColin Finck
3449c2c66affSColin Finck void * CDECL wined3d_texture_get_sub_resource_parent(struct wined3d_texture *texture, unsigned int sub_resource_idx)
3450c2c66affSColin Finck {
3451c2c66affSColin Finck unsigned int sub_count = texture->level_count * texture->layer_count;
3452c2c66affSColin Finck
3453c2c66affSColin Finck TRACE("texture %p, sub_resource_idx %u.\n", texture, sub_resource_idx);
3454c2c66affSColin Finck
3455c2c66affSColin Finck if (sub_resource_idx >= sub_count)
3456c2c66affSColin Finck {
3457c2c66affSColin Finck WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx, sub_count);
3458c2c66affSColin Finck return NULL;
3459c2c66affSColin Finck }
3460c2c66affSColin Finck
3461c2c66affSColin Finck return texture->sub_resources[sub_resource_idx].parent;
3462c2c66affSColin Finck }
3463c2c66affSColin Finck
3464c2c66affSColin Finck void CDECL wined3d_texture_set_sub_resource_parent(struct wined3d_texture *texture,
3465c2c66affSColin Finck unsigned int sub_resource_idx, void *parent)
3466c2c66affSColin Finck {
3467c2c66affSColin Finck unsigned int sub_count = texture->level_count * texture->layer_count;
3468c2c66affSColin Finck
3469c2c66affSColin Finck TRACE("texture %p, sub_resource_idx %u, parent %p.\n", texture, sub_resource_idx, parent);
3470c2c66affSColin Finck
3471c2c66affSColin Finck if (sub_resource_idx >= sub_count)
3472c2c66affSColin Finck {
3473c2c66affSColin Finck WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx, sub_count);
3474c2c66affSColin Finck return;
3475c2c66affSColin Finck }
3476c2c66affSColin Finck
3477c2c66affSColin Finck texture->sub_resources[sub_resource_idx].parent = parent;
3478c2c66affSColin Finck }
3479c2c66affSColin Finck
3480c2c66affSColin Finck HRESULT CDECL wined3d_texture_get_sub_resource_desc(const struct wined3d_texture *texture,
3481c2c66affSColin Finck unsigned int sub_resource_idx, struct wined3d_sub_resource_desc *desc)
3482c2c66affSColin Finck {
3483c2c66affSColin Finck unsigned int sub_count = texture->level_count * texture->layer_count;
3484c2c66affSColin Finck const struct wined3d_resource *resource;
3485c2c66affSColin Finck unsigned int level_idx;
3486c2c66affSColin Finck
3487c2c66affSColin Finck TRACE("texture %p, sub_resource_idx %u, desc %p.\n", texture, sub_resource_idx, desc);
3488c2c66affSColin Finck
3489c2c66affSColin Finck if (sub_resource_idx >= sub_count)
3490c2c66affSColin Finck {
3491c2c66affSColin Finck WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx, sub_count);
3492c2c66affSColin Finck return WINED3DERR_INVALIDCALL;
3493c2c66affSColin Finck }
3494c2c66affSColin Finck
3495c2c66affSColin Finck resource = &texture->resource;
3496c2c66affSColin Finck desc->format = resource->format->id;
3497c2c66affSColin Finck desc->multisample_type = resource->multisample_type;
3498c2c66affSColin Finck desc->multisample_quality = resource->multisample_quality;
3499c2c66affSColin Finck desc->usage = resource->usage;
35009987f029SAmine Khaldi desc->access = resource->access;
3501c2c66affSColin Finck
3502c2c66affSColin Finck level_idx = sub_resource_idx % texture->level_count;
3503c2c66affSColin Finck desc->width = wined3d_texture_get_level_width(texture, level_idx);
3504c2c66affSColin Finck desc->height = wined3d_texture_get_level_height(texture, level_idx);
3505c2c66affSColin Finck desc->depth = wined3d_texture_get_level_depth(texture, level_idx);
3506c2c66affSColin Finck desc->size = texture->sub_resources[sub_resource_idx].size;
3507c2c66affSColin Finck
3508c2c66affSColin Finck return WINED3D_OK;
3509c2c66affSColin Finck }
3510c2c66affSColin Finck
3511c2c66affSColin Finck HRESULT CDECL wined3d_texture_create(struct wined3d_device *device, const struct wined3d_resource_desc *desc,
3512c2c66affSColin Finck UINT layer_count, UINT level_count, DWORD flags, const struct wined3d_sub_resource_data *data,
3513c2c66affSColin Finck void *parent, const struct wined3d_parent_ops *parent_ops, struct wined3d_texture **texture)
3514c2c66affSColin Finck {
3515c2c66affSColin Finck struct wined3d_texture *object;
3516c2c66affSColin Finck HRESULT hr;
3517c2c66affSColin Finck
3518c2c66affSColin Finck TRACE("device %p, desc %p, layer_count %u, level_count %u, flags %#x, data %p, "
3519c2c66affSColin Finck "parent %p, parent_ops %p, texture %p.\n",
3520c2c66affSColin Finck device, desc, layer_count, level_count, flags, data, parent, parent_ops, texture);
3521c2c66affSColin Finck
3522c2c66affSColin Finck if (!layer_count)
3523c2c66affSColin Finck {
3524c2c66affSColin Finck WARN("Invalid layer count.\n");
3525c2c66affSColin Finck return E_INVALIDARG;
3526c2c66affSColin Finck }
3527c2c66affSColin Finck if ((desc->usage & WINED3DUSAGE_LEGACY_CUBEMAP) && layer_count != 6)
3528c2c66affSColin Finck {
3529c2c66affSColin Finck ERR("Invalid layer count %u for legacy cubemap.\n", layer_count);
3530c2c66affSColin Finck layer_count = 6;
3531c2c66affSColin Finck }
3532c2c66affSColin Finck
3533c2c66affSColin Finck if (!level_count)
3534c2c66affSColin Finck {
3535c2c66affSColin Finck WARN("Invalid level count.\n");
3536c2c66affSColin Finck return WINED3DERR_INVALIDCALL;
3537c2c66affSColin Finck }
3538c2c66affSColin Finck
3539c2c66affSColin Finck if (desc->multisample_type != WINED3D_MULTISAMPLE_NONE)
3540c2c66affSColin Finck {
3541c2c66affSColin Finck const struct wined3d_format *format = wined3d_get_format(&device->adapter->gl_info,
3542c2c66affSColin Finck desc->format, desc->usage);
3543c2c66affSColin Finck
3544c2c66affSColin Finck if (desc->multisample_type == WINED3D_MULTISAMPLE_NON_MASKABLE
3545c2c66affSColin Finck && desc->multisample_quality >= wined3d_popcount(format->multisample_types))
3546c2c66affSColin Finck {
3547c2c66affSColin Finck WARN("Unsupported quality level %u requested for WINED3D_MULTISAMPLE_NON_MASKABLE.\n",
3548c2c66affSColin Finck desc->multisample_quality);
3549c2c66affSColin Finck return WINED3DERR_NOTAVAILABLE;
3550c2c66affSColin Finck }
3551c2c66affSColin Finck if (desc->multisample_type != WINED3D_MULTISAMPLE_NON_MASKABLE
3552c2c66affSColin Finck && (!(format->multisample_types & 1u << (desc->multisample_type - 1))
3553c2c66affSColin Finck || desc->multisample_quality))
3554c2c66affSColin Finck {
3555c2c66affSColin Finck WARN("Unsupported multisample type %u quality %u requested.\n", desc->multisample_type,
3556c2c66affSColin Finck desc->multisample_quality);
3557c2c66affSColin Finck return WINED3DERR_NOTAVAILABLE;
3558c2c66affSColin Finck }
3559c2c66affSColin Finck }
3560c2c66affSColin Finck
35619987f029SAmine Khaldi if (!(object = heap_alloc_zero(FIELD_OFFSET(struct wined3d_texture,
35629987f029SAmine Khaldi sub_resources[level_count * layer_count]))))
3563c2c66affSColin Finck return E_OUTOFMEMORY;
3564c2c66affSColin Finck
3565*81cffd76SJoachim Henze switch (desc->resource_type)
3566*81cffd76SJoachim Henze {
3567*81cffd76SJoachim Henze case WINED3D_RTYPE_TEXTURE_1D:
3568*81cffd76SJoachim Henze hr = texture1d_init(object, desc, layer_count, level_count, device, parent, parent_ops);
3569*81cffd76SJoachim Henze break;
3570*81cffd76SJoachim Henze
3571*81cffd76SJoachim Henze case WINED3D_RTYPE_TEXTURE_2D:
3572*81cffd76SJoachim Henze hr = texture_init(object, desc, layer_count, level_count, flags, device, parent, parent_ops);
3573*81cffd76SJoachim Henze break;
3574*81cffd76SJoachim Henze
3575*81cffd76SJoachim Henze case WINED3D_RTYPE_TEXTURE_3D:
3576*81cffd76SJoachim Henze hr = volumetexture_init(object, desc, layer_count, level_count, flags, device, parent, parent_ops);
3577*81cffd76SJoachim Henze break;
3578*81cffd76SJoachim Henze
3579*81cffd76SJoachim Henze default:
3580*81cffd76SJoachim Henze ERR("Invalid resource type %s.\n", debug_d3dresourcetype(desc->resource_type));
3581*81cffd76SJoachim Henze hr = WINED3DERR_INVALIDCALL;
3582*81cffd76SJoachim Henze break;
3583*81cffd76SJoachim Henze }
3584*81cffd76SJoachim Henze
3585*81cffd76SJoachim Henze if (FAILED(hr))
3586c2c66affSColin Finck {
3587c2c66affSColin Finck WARN("Failed to initialize texture, returning %#x.\n", hr);
35889987f029SAmine Khaldi heap_free(object);
3589c2c66affSColin Finck return hr;
3590c2c66affSColin Finck }
3591c2c66affSColin Finck
3592c2c66affSColin Finck /* FIXME: We'd like to avoid ever allocating system memory for the texture
3593c2c66affSColin Finck * in this case. */
3594c2c66affSColin Finck if (data)
3595c2c66affSColin Finck {
3596c2c66affSColin Finck unsigned int sub_count = level_count * layer_count;
3597c2c66affSColin Finck unsigned int i;
3598c2c66affSColin Finck
3599c2c66affSColin Finck for (i = 0; i < sub_count; ++i)
3600c2c66affSColin Finck {
3601c2c66affSColin Finck if (!data[i].data)
3602c2c66affSColin Finck {
3603c2c66affSColin Finck WARN("Invalid sub-resource data specified for sub-resource %u.\n", i);
3604c2c66affSColin Finck wined3d_texture_cleanup_sync(object);
36059987f029SAmine Khaldi heap_free(object);
3606c2c66affSColin Finck return E_INVALIDARG;
3607c2c66affSColin Finck }
3608c2c66affSColin Finck }
3609c2c66affSColin Finck
3610c2c66affSColin Finck for (i = 0; i < sub_count; ++i)
3611c2c66affSColin Finck {
3612c2c66affSColin Finck wined3d_device_update_sub_resource(device, &object->resource,
3613*81cffd76SJoachim Henze i, NULL, data[i].data, data[i].row_pitch, data[i].slice_pitch);
3614c2c66affSColin Finck }
3615c2c66affSColin Finck }
3616c2c66affSColin Finck
3617c2c66affSColin Finck TRACE("Created texture %p.\n", object);
3618c2c66affSColin Finck *texture = object;
3619c2c66affSColin Finck
3620c2c66affSColin Finck return WINED3D_OK;
3621c2c66affSColin Finck }
3622c2c66affSColin Finck
3623c2c66affSColin Finck HRESULT CDECL wined3d_texture_get_dc(struct wined3d_texture *texture, unsigned int sub_resource_idx, HDC *dc)
3624c2c66affSColin Finck {
3625c2c66affSColin Finck struct wined3d_device *device = texture->resource.device;
3626c2c66affSColin Finck struct wined3d_texture_sub_resource *sub_resource;
3627*81cffd76SJoachim Henze struct wined3d_surface *surface;
3628c2c66affSColin Finck
3629c2c66affSColin Finck TRACE("texture %p, sub_resource_idx %u, dc %p.\n", texture, sub_resource_idx, dc);
3630c2c66affSColin Finck
3631c2c66affSColin Finck if (!(texture->flags & WINED3D_TEXTURE_GET_DC))
3632c2c66affSColin Finck {
3633c2c66affSColin Finck WARN("Texture does not support GetDC\n");
3634c2c66affSColin Finck /* Don't touch the DC */
3635c2c66affSColin Finck return WINED3DERR_INVALIDCALL;
3636c2c66affSColin Finck }
3637c2c66affSColin Finck
3638c2c66affSColin Finck if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
3639c2c66affSColin Finck return WINED3DERR_INVALIDCALL;
3640c2c66affSColin Finck
3641c2c66affSColin Finck if (texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
3642c2c66affSColin Finck {
3643c2c66affSColin Finck WARN("Not supported on %s resources.\n", debug_d3dresourcetype(texture->resource.type));
3644c2c66affSColin Finck return WINED3DERR_INVALIDCALL;
3645c2c66affSColin Finck }
3646c2c66affSColin Finck
3647*81cffd76SJoachim Henze surface = sub_resource->u.surface;
3648*81cffd76SJoachim Henze
3649c2c66affSColin Finck if (texture->resource.map_count && !(texture->flags & WINED3D_TEXTURE_GET_DC_LENIENT))
3650c2c66affSColin Finck return WINED3DERR_INVALIDCALL;
3651c2c66affSColin Finck
3652*81cffd76SJoachim Henze if (!surface->dc)
3653c2c66affSColin Finck {
3654*81cffd76SJoachim Henze wined3d_cs_init_object(device->cs, texture2d_create_dc, surface);
3655c2c66affSColin Finck device->cs->ops->finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
36567af3969eSAmine Khaldi }
3657*81cffd76SJoachim Henze if (!surface->dc)
3658*81cffd76SJoachim Henze return WINED3DERR_INVALIDCALL;
3659c2c66affSColin Finck
3660c2c66affSColin Finck if (!(texture->flags & WINED3D_TEXTURE_GET_DC_LENIENT))
3661c2c66affSColin Finck texture->flags |= WINED3D_TEXTURE_DC_IN_USE;
3662c2c66affSColin Finck ++texture->resource.map_count;
3663c2c66affSColin Finck ++sub_resource->map_count;
3664c2c66affSColin Finck
3665*81cffd76SJoachim Henze *dc = surface->dc;
3666c2c66affSColin Finck TRACE("Returning dc %p.\n", *dc);
3667c2c66affSColin Finck
3668c2c66affSColin Finck return WINED3D_OK;
3669c2c66affSColin Finck }
3670c2c66affSColin Finck
3671c2c66affSColin Finck HRESULT CDECL wined3d_texture_release_dc(struct wined3d_texture *texture, unsigned int sub_resource_idx, HDC dc)
3672c2c66affSColin Finck {
3673c2c66affSColin Finck struct wined3d_device *device = texture->resource.device;
3674c2c66affSColin Finck struct wined3d_texture_sub_resource *sub_resource;
3675*81cffd76SJoachim Henze struct wined3d_surface *surface;
3676c2c66affSColin Finck
3677c2c66affSColin Finck TRACE("texture %p, sub_resource_idx %u, dc %p.\n", texture, sub_resource_idx, dc);
3678c2c66affSColin Finck
3679c2c66affSColin Finck if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
3680c2c66affSColin Finck return WINED3DERR_INVALIDCALL;
3681c2c66affSColin Finck
3682c2c66affSColin Finck if (texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
3683c2c66affSColin Finck {
3684c2c66affSColin Finck WARN("Not supported on %s resources.\n", debug_d3dresourcetype(texture->resource.type));
3685c2c66affSColin Finck return WINED3DERR_INVALIDCALL;
3686c2c66affSColin Finck }
3687c2c66affSColin Finck
3688*81cffd76SJoachim Henze surface = sub_resource->u.surface;
3689*81cffd76SJoachim Henze
3690c2c66affSColin Finck if (!(texture->flags & (WINED3D_TEXTURE_GET_DC_LENIENT | WINED3D_TEXTURE_DC_IN_USE)))
3691c2c66affSColin Finck return WINED3DERR_INVALIDCALL;
3692c2c66affSColin Finck
3693*81cffd76SJoachim Henze if (surface->dc != dc)
3694c2c66affSColin Finck {
3695*81cffd76SJoachim Henze WARN("Application tries to release invalid DC %p, surface DC is %p.\n", dc, surface->dc);
3696c2c66affSColin Finck return WINED3DERR_INVALIDCALL;
3697c2c66affSColin Finck }
3698c2c66affSColin Finck
3699c2c66affSColin Finck if (!(texture->resource.usage & WINED3DUSAGE_OWNDC) && !(device->wined3d->flags & WINED3D_NO3D))
3700c2c66affSColin Finck {
3701*81cffd76SJoachim Henze wined3d_cs_destroy_object(device->cs, texture2d_destroy_dc, surface);
3702c2c66affSColin Finck device->cs->ops->finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
3703c2c66affSColin Finck }
3704c2c66affSColin Finck
3705c2c66affSColin Finck --sub_resource->map_count;
3706c2c66affSColin Finck if (!--texture->resource.map_count && texture->update_map_binding)
3707c2c66affSColin Finck wined3d_texture_update_map_binding(texture);
3708c2c66affSColin Finck if (!(texture->flags & WINED3D_TEXTURE_GET_DC_LENIENT))
3709c2c66affSColin Finck texture->flags &= ~WINED3D_TEXTURE_DC_IN_USE;
3710c2c66affSColin Finck
3711c2c66affSColin Finck return WINED3D_OK;
3712c2c66affSColin Finck }
3713