wined3d: Introduce a helper function to allocate arrays.
[wine.git] / dlls / wined3d / volume.c
blob98e184dae772410bd501fa72a3273765a113c04c
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 wined3d_volume_download_data(struct wined3d_volume *volume,
92 const struct wined3d_context *context, const struct wined3d_bo_address *data)
94 const struct wined3d_format *format = volume->container->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, volume->texture_level,
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 struct wined3d_texture *texture = volume->container;
148 struct wined3d_bo_address data;
149 /* Optimizations are possible, but the effort should be put into either
150 * implementing EXT_SRGB_DECODE in the driver or finding out why we
151 * picked the wrong copy for the original upload and fixing that.
153 * Also keep in mind that we want to avoid using resource.heap_memory
154 * for DEFAULT pool surfaces. */
156 WARN_(d3d_perf)("Performing slow rgb/srgb volume transfer.\n");
157 data.buffer_object = 0;
158 if (!(data.addr = HeapAlloc(GetProcessHeap(), 0, texture->sub_resources[volume->texture_level].size)))
159 return;
161 wined3d_texture_bind_and_dirtify(texture, context, !dest_is_srgb);
162 wined3d_volume_download_data(volume, context, &data);
163 wined3d_texture_bind_and_dirtify(texture, context, dest_is_srgb);
164 wined3d_volume_upload_data(texture, volume->texture_level, context, wined3d_const_bo_address(&data));
166 HeapFree(GetProcessHeap(), 0, data.addr);
169 /* Context activation is done by the caller. */
170 BOOL wined3d_volume_load_location(struct wined3d_volume *volume,
171 struct wined3d_context *context, DWORD location)
173 DWORD required_access = volume_access_from_location(location);
174 unsigned int sub_resource_idx = volume->texture_level;
175 struct wined3d_texture *texture = volume->container;
176 struct wined3d_texture_sub_resource *sub_resource;
178 sub_resource = &texture->sub_resources[sub_resource_idx];
179 TRACE("Volume %p, loading %s, have %s.\n", volume, wined3d_debug_location(location),
180 wined3d_debug_location(sub_resource->locations));
182 if ((sub_resource->locations & location) == location)
184 TRACE("Location(s) already up to date.\n");
185 return TRUE;
188 if ((texture->resource.access_flags & required_access) != required_access)
190 ERR("Operation requires %#x access, but volume only has %#x.\n",
191 required_access, texture->resource.access_flags);
192 return FALSE;
195 if (!wined3d_texture_prepare_location(texture, sub_resource_idx, context, location))
196 return FALSE;
198 if (sub_resource->locations & WINED3D_LOCATION_DISCARDED)
200 TRACE("Volume previously discarded, nothing to do.\n");
201 wined3d_texture_validate_location(texture, sub_resource_idx, location);
202 wined3d_texture_invalidate_location(texture, sub_resource_idx, WINED3D_LOCATION_DISCARDED);
203 goto done;
206 switch (location)
208 case WINED3D_LOCATION_TEXTURE_RGB:
209 case WINED3D_LOCATION_TEXTURE_SRGB:
210 if (sub_resource->locations & WINED3D_LOCATION_SYSMEM)
212 struct wined3d_const_bo_address data = {0, texture->resource.heap_memory};
213 data.addr += sub_resource->offset;
214 wined3d_texture_bind_and_dirtify(texture, context,
215 location == WINED3D_LOCATION_TEXTURE_SRGB);
216 wined3d_volume_upload_data(texture, sub_resource_idx, context, &data);
218 else if (sub_resource->locations & WINED3D_LOCATION_BUFFER)
220 struct wined3d_const_bo_address data = {sub_resource->buffer_object, NULL};
221 wined3d_texture_bind_and_dirtify(texture, context,
222 location == WINED3D_LOCATION_TEXTURE_SRGB);
223 wined3d_volume_upload_data(texture, sub_resource_idx, context, &data);
225 else if (sub_resource->locations & WINED3D_LOCATION_TEXTURE_RGB)
227 wined3d_volume_srgb_transfer(volume, context, TRUE);
229 else if (sub_resource->locations & WINED3D_LOCATION_TEXTURE_SRGB)
231 wined3d_volume_srgb_transfer(volume, context, FALSE);
233 else
235 FIXME("Implement texture loading from %s.\n", wined3d_debug_location(sub_resource->locations));
236 return FALSE;
238 break;
240 case WINED3D_LOCATION_SYSMEM:
241 if (sub_resource->locations & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB))
243 struct wined3d_bo_address data = {0, texture->resource.heap_memory};
245 data.addr += sub_resource->offset;
246 if (sub_resource->locations & WINED3D_LOCATION_TEXTURE_RGB)
247 wined3d_texture_bind_and_dirtify(texture, context, FALSE);
248 else
249 wined3d_texture_bind_and_dirtify(texture, context, TRUE);
251 wined3d_volume_download_data(volume, context, &data);
252 ++texture->download_count;
254 else
256 FIXME("Implement WINED3D_LOCATION_SYSMEM loading from %s.\n",
257 wined3d_debug_location(sub_resource->locations));
258 return FALSE;
260 break;
262 case WINED3D_LOCATION_BUFFER:
263 if (sub_resource->locations & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB))
265 struct wined3d_bo_address data = {sub_resource->buffer_object, NULL};
267 if (sub_resource->locations & WINED3D_LOCATION_TEXTURE_RGB)
268 wined3d_texture_bind_and_dirtify(texture, context, FALSE);
269 else
270 wined3d_texture_bind_and_dirtify(texture, context, TRUE);
272 wined3d_volume_download_data(volume, context, &data);
274 else
276 FIXME("Implement WINED3D_LOCATION_BUFFER loading from %s.\n",
277 wined3d_debug_location(sub_resource->locations));
278 return FALSE;
280 break;
282 default:
283 FIXME("Implement %s loading from %s.\n", wined3d_debug_location(location),
284 wined3d_debug_location(sub_resource->locations));
285 return FALSE;
288 done:
289 wined3d_texture_validate_location(texture, sub_resource_idx, location);
291 return TRUE;