wined3d: Pass a texture and sub-resource index to wined3d_volume_download_data().
[wine.git] / dlls / wined3d / volume.c
blob72ebd19d5f93da3b91b8eb819fc664b9ca49f023
1 /*
2 * Copyright 2002-2005 Jason Edmeades
3 * Copyright 2002-2005 Raphael Junqueira
4 * Copyright 2005 Oliver Stieber
5 * Copyright 2009-2011 Henri Verbeet for CodeWeavers
6 * Copyright 2013 Stefan Dösinger for CodeWeavers
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "config.h"
24 #include "wine/port.h"
25 #include "wined3d_private.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
28 WINE_DECLARE_DEBUG_CHANNEL(d3d_perf);
30 /* This call just uploads data, the caller is responsible for binding the
31 * correct texture. */
32 /* Context activation is done by the caller. */
33 void wined3d_volume_upload_data(struct wined3d_texture *texture, unsigned int sub_resource_idx,
34 const struct wined3d_context *context, const struct wined3d_const_bo_address *data)
36 const struct wined3d_format *format = texture->resource.format;
37 unsigned int level = sub_resource_idx % texture->level_count;
38 const struct wined3d_gl_info *gl_info = context->gl_info;
39 unsigned int width, height, depth;
40 const void *mem = data->addr;
41 void *converted_mem = NULL;
43 TRACE("texture %p, sub_resource_idx %u, context %p, format %s (%#x).\n",
44 texture, level, context, debug_d3dformat(format->id), format->id);
46 width = wined3d_texture_get_level_width(texture, level);
47 height = wined3d_texture_get_level_height(texture, level);
48 depth = wined3d_texture_get_level_depth(texture, level);
50 if (format->convert)
52 UINT dst_row_pitch, dst_slice_pitch;
53 UINT src_row_pitch, src_slice_pitch;
55 if (data->buffer_object)
56 ERR("Loading a converted texture from a PBO.\n");
57 if (texture->resource.format_flags & WINED3DFMT_FLAG_BLOCKS)
58 ERR("Converting a block-based format.\n");
60 dst_row_pitch = width * format->conv_byte_count;
61 dst_slice_pitch = dst_row_pitch * height;
63 wined3d_texture_get_pitch(texture, level, &src_row_pitch, &src_slice_pitch);
65 converted_mem = wined3d_calloc(depth, dst_slice_pitch);
66 format->convert(data->addr, converted_mem, src_row_pitch, src_slice_pitch,
67 dst_row_pitch, dst_slice_pitch, width, height, depth);
68 mem = converted_mem;
71 if (data->buffer_object)
73 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, data->buffer_object));
74 checkGLcall("glBindBuffer");
77 GL_EXTCALL(glTexSubImage3D(GL_TEXTURE_3D, level, 0, 0, 0,
78 width, height, depth, format->glFormat, format->glType, mem));
79 checkGLcall("glTexSubImage3D");
81 if (data->buffer_object)
83 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0));
84 checkGLcall("glBindBuffer");
87 HeapFree(GetProcessHeap(), 0, converted_mem);
90 /* Context activation is done by the caller. */
91 static void texture3d_download_data(struct wined3d_texture *texture, unsigned int sub_resource_idx,
92 const struct wined3d_context *context, const struct wined3d_bo_address *data)
94 const struct wined3d_format *format = texture->resource.format;
95 const struct wined3d_gl_info *gl_info = context->gl_info;
97 if (format->convert)
99 FIXME("Attempting to download a converted volume, format %s.\n",
100 debug_d3dformat(format->id));
101 return;
104 if (data->buffer_object)
106 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, data->buffer_object));
107 checkGLcall("glBindBuffer");
110 gl_info->gl_ops.gl.p_glGetTexImage(GL_TEXTURE_3D, sub_resource_idx,
111 format->glFormat, format->glType, data->addr);
112 checkGLcall("glGetTexImage");
114 if (data->buffer_object)
116 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, 0));
117 checkGLcall("glBindBuffer");
122 static DWORD volume_access_from_location(DWORD location)
124 switch (location)
126 case WINED3D_LOCATION_DISCARDED:
127 return 0;
129 case WINED3D_LOCATION_SYSMEM:
130 return WINED3D_RESOURCE_ACCESS_CPU;
132 case WINED3D_LOCATION_BUFFER:
133 case WINED3D_LOCATION_TEXTURE_RGB:
134 case WINED3D_LOCATION_TEXTURE_SRGB:
135 return WINED3D_RESOURCE_ACCESS_GPU;
137 default:
138 FIXME("Unhandled location %#x.\n", location);
139 return 0;
143 /* Context activation is done by the caller. */
144 static void wined3d_volume_srgb_transfer(struct wined3d_volume *volume,
145 struct wined3d_context *context, BOOL dest_is_srgb)
147 unsigned int sub_resource_idx = volume->texture_level;
148 struct wined3d_texture *texture = volume->container;
149 struct wined3d_bo_address data;
150 /* Optimizations are possible, but the effort should be put into either
151 * implementing EXT_SRGB_DECODE in the driver or finding out why we
152 * picked the wrong copy for the original upload and fixing that.
154 * Also keep in mind that we want to avoid using resource.heap_memory
155 * for DEFAULT pool surfaces. */
157 WARN_(d3d_perf)("Performing slow rgb/srgb volume transfer.\n");
158 data.buffer_object = 0;
159 if (!(data.addr = HeapAlloc(GetProcessHeap(), 0, texture->sub_resources[sub_resource_idx].size)))
160 return;
162 wined3d_texture_bind_and_dirtify(texture, context, !dest_is_srgb);
163 texture3d_download_data(texture, sub_resource_idx, context, &data);
164 wined3d_texture_bind_and_dirtify(texture, context, dest_is_srgb);
165 wined3d_volume_upload_data(texture, sub_resource_idx, context, wined3d_const_bo_address(&data));
167 HeapFree(GetProcessHeap(), 0, data.addr);
170 /* Context activation is done by the caller. */
171 BOOL wined3d_volume_load_location(struct wined3d_volume *volume,
172 struct wined3d_context *context, DWORD location)
174 DWORD required_access = volume_access_from_location(location);
175 unsigned int sub_resource_idx = volume->texture_level;
176 struct wined3d_texture *texture = volume->container;
177 struct wined3d_texture_sub_resource *sub_resource;
179 sub_resource = &texture->sub_resources[sub_resource_idx];
180 TRACE("Volume %p, loading %s, have %s.\n", volume, wined3d_debug_location(location),
181 wined3d_debug_location(sub_resource->locations));
183 if ((sub_resource->locations & location) == location)
185 TRACE("Location(s) already up to date.\n");
186 return TRUE;
189 if ((texture->resource.access_flags & required_access) != required_access)
191 ERR("Operation requires %#x access, but volume only has %#x.\n",
192 required_access, texture->resource.access_flags);
193 return FALSE;
196 if (!wined3d_texture_prepare_location(texture, sub_resource_idx, context, location))
197 return FALSE;
199 if (sub_resource->locations & WINED3D_LOCATION_DISCARDED)
201 TRACE("Volume previously discarded, nothing to do.\n");
202 wined3d_texture_validate_location(texture, sub_resource_idx, location);
203 wined3d_texture_invalidate_location(texture, sub_resource_idx, WINED3D_LOCATION_DISCARDED);
204 goto done;
207 switch (location)
209 case WINED3D_LOCATION_TEXTURE_RGB:
210 case WINED3D_LOCATION_TEXTURE_SRGB:
211 if (sub_resource->locations & WINED3D_LOCATION_SYSMEM)
213 struct wined3d_const_bo_address data = {0, texture->resource.heap_memory};
214 data.addr += sub_resource->offset;
215 wined3d_texture_bind_and_dirtify(texture, context,
216 location == WINED3D_LOCATION_TEXTURE_SRGB);
217 wined3d_volume_upload_data(texture, sub_resource_idx, context, &data);
219 else if (sub_resource->locations & WINED3D_LOCATION_BUFFER)
221 struct wined3d_const_bo_address data = {sub_resource->buffer_object, NULL};
222 wined3d_texture_bind_and_dirtify(texture, context,
223 location == WINED3D_LOCATION_TEXTURE_SRGB);
224 wined3d_volume_upload_data(texture, sub_resource_idx, context, &data);
226 else if (sub_resource->locations & WINED3D_LOCATION_TEXTURE_RGB)
228 wined3d_volume_srgb_transfer(volume, context, TRUE);
230 else if (sub_resource->locations & WINED3D_LOCATION_TEXTURE_SRGB)
232 wined3d_volume_srgb_transfer(volume, context, FALSE);
234 else
236 FIXME("Implement texture loading from %s.\n", wined3d_debug_location(sub_resource->locations));
237 return FALSE;
239 break;
241 case WINED3D_LOCATION_SYSMEM:
242 if (sub_resource->locations & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB))
244 struct wined3d_bo_address data = {0, texture->resource.heap_memory};
246 data.addr += sub_resource->offset;
247 if (sub_resource->locations & WINED3D_LOCATION_TEXTURE_RGB)
248 wined3d_texture_bind_and_dirtify(texture, context, FALSE);
249 else
250 wined3d_texture_bind_and_dirtify(texture, context, TRUE);
252 texture3d_download_data(texture, sub_resource_idx, context, &data);
253 ++texture->download_count;
255 else
257 FIXME("Implement WINED3D_LOCATION_SYSMEM loading from %s.\n",
258 wined3d_debug_location(sub_resource->locations));
259 return FALSE;
261 break;
263 case WINED3D_LOCATION_BUFFER:
264 if (sub_resource->locations & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB))
266 struct wined3d_bo_address data = {sub_resource->buffer_object, NULL};
268 if (sub_resource->locations & WINED3D_LOCATION_TEXTURE_RGB)
269 wined3d_texture_bind_and_dirtify(texture, context, FALSE);
270 else
271 wined3d_texture_bind_and_dirtify(texture, context, TRUE);
273 texture3d_download_data(texture, sub_resource_idx, context, &data);
275 else
277 FIXME("Implement WINED3D_LOCATION_BUFFER loading from %s.\n",
278 wined3d_debug_location(sub_resource->locations));
279 return FALSE;
281 break;
283 default:
284 FIXME("Implement %s loading from %s.\n", wined3d_debug_location(location),
285 wined3d_debug_location(sub_resource->locations));
286 return FALSE;
289 done:
290 wined3d_texture_validate_location(texture, sub_resource_idx, location);
292 return TRUE;