[PATCH v2 2/9] wined3d: Try to avoid glGetTexImage() for 2D array textures.
Józef Kucia
jkucia at codeweavers.com
Wed Apr 20 02:09:41 CDT 2016
Signed-off-by: Józef Kucia <jkucia at codeweavers.com>
---
Version 2: Rebased.
---
dlls/wined3d/surface.c | 262 ++++++++++++++++++++++++++-----------------------
1 file changed, 138 insertions(+), 124 deletions(-)
diff --git a/dlls/wined3d/surface.c b/dlls/wined3d/surface.c
index fc6c2f7..0c8f671 100644
--- a/dlls/wined3d/surface.c
+++ b/dlls/wined3d/surface.c
@@ -854,12 +854,127 @@ static const struct wined3d_resource_ops surface_resource_ops =
surface_resource_sub_resource_unmap,
};
-/* This call just downloads data, the caller is responsible for binding the
- * correct texture. */
+static void read_from_framebuffer(struct wined3d_surface *surface,
+ struct wined3d_context *old_ctx, DWORD dst_location)
+{
+ unsigned int sub_resource_idx = surface_get_sub_resource_idx(surface);
+ struct wined3d_texture *texture = surface->container;
+ struct wined3d_device *device = texture->resource.device;
+ const struct wined3d_gl_info *gl_info;
+ struct wined3d_context *context = old_ctx;
+ struct wined3d_surface *restore_rt = NULL;
+ unsigned int row_pitch, slice_pitch;
+ unsigned int width, height;
+ BYTE *mem;
+ BYTE *row, *top, *bottom;
+ int i;
+ BOOL srcIsUpsideDown;
+ struct wined3d_bo_address data;
+
+ wined3d_texture_get_memory(texture, sub_resource_idx, &data, dst_location);
+
+ restore_rt = context_get_rt_surface(old_ctx);
+ if (restore_rt != surface)
+ context = context_acquire(device, surface);
+ else
+ restore_rt = NULL;
+
+ context_apply_blit_state(context, device);
+ gl_info = context->gl_info;
+
+ /* Select the correct read buffer, and give some debug output.
+ * There is no need to keep track of the current read buffer or reset it, every part of the code
+ * that reads sets the read buffer as desired.
+ */
+ if (wined3d_resource_is_offscreen(&texture->resource))
+ {
+ /* Mapping the primary render target which is not on a swapchain.
+ * Read from the back buffer. */
+ TRACE("Mapping offscreen render target.\n");
+ gl_info->gl_ops.gl.p_glReadBuffer(context_get_offscreen_gl_buffer(context));
+ srcIsUpsideDown = TRUE;
+ }
+ else
+ {
+ /* Onscreen surfaces are always part of a swapchain */
+ GLenum buffer = wined3d_texture_get_gl_buffer(texture);
+ TRACE("Mapping %#x buffer.\n", buffer);
+ gl_info->gl_ops.gl.p_glReadBuffer(buffer);
+ checkGLcall("glReadBuffer");
+ srcIsUpsideDown = FALSE;
+ }
+
+ if (data.buffer_object)
+ {
+ GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, data.buffer_object));
+ checkGLcall("glBindBuffer");
+ }
+
+ wined3d_texture_get_pitch(texture, surface->texture_level, &row_pitch, &slice_pitch);
+
+ /* Setup pixel store pack state -- to glReadPixels into the correct place */
+ gl_info->gl_ops.gl.p_glPixelStorei(GL_PACK_ROW_LENGTH, row_pitch / texture->resource.format->byte_count);
+ checkGLcall("glPixelStorei");
+
+ width = wined3d_texture_get_level_width(texture, surface->texture_level);
+ height = wined3d_texture_get_level_height(texture, surface->texture_level);
+ gl_info->gl_ops.gl.p_glReadPixels(0, 0, width, height,
+ texture->resource.format->glFormat,
+ texture->resource.format->glType, data.addr);
+ checkGLcall("glReadPixels");
+
+ /* Reset previous pixel store pack state */
+ gl_info->gl_ops.gl.p_glPixelStorei(GL_PACK_ROW_LENGTH, 0);
+ checkGLcall("glPixelStorei");
+
+ if (!srcIsUpsideDown)
+ {
+ /* glReadPixels returns the image upside down, and there is no way to
+ * prevent this. Flip the lines in software. */
+
+ if (!(row = HeapAlloc(GetProcessHeap(), 0, row_pitch)))
+ goto error;
+
+ if (data.buffer_object)
+ {
+ mem = GL_EXTCALL(glMapBuffer(GL_PIXEL_PACK_BUFFER, GL_READ_WRITE));
+ checkGLcall("glMapBuffer");
+ }
+ else
+ mem = data.addr;
+
+ top = mem;
+ bottom = mem + row_pitch * (height - 1);
+ for (i = 0; i < height / 2; i++)
+ {
+ memcpy(row, top, row_pitch);
+ memcpy(top, bottom, row_pitch);
+ memcpy(bottom, row, row_pitch);
+ top += row_pitch;
+ bottom -= row_pitch;
+ }
+ HeapFree(GetProcessHeap(), 0, row);
+
+ if (data.buffer_object)
+ GL_EXTCALL(glUnmapBuffer(GL_PIXEL_PACK_BUFFER));
+ }
+
+error:
+ if (data.buffer_object)
+ {
+ GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, 0));
+ checkGLcall("glBindBuffer");
+ }
+
+ if (restore_rt)
+ context_restore(context, restore_rt);
+}
+
/* Context activation is done by the caller. */
-static void surface_download_data(struct wined3d_surface *surface, const struct wined3d_gl_info *gl_info,
- DWORD dst_location)
+static void surface_download_data(struct wined3d_surface *surface, struct wined3d_context *context,
+ DWORD src_locations, DWORD dst_location)
{
+ const struct wined3d_gl_info *gl_info = context->gl_info;
struct wined3d_texture *texture = surface->container;
const struct wined3d_format *format = texture->resource.format;
unsigned int dst_row_pitch, dst_slice_pitch;
@@ -874,6 +989,24 @@ static void surface_download_data(struct wined3d_surface *surface, const struct
return;
}
+ if (surface->texture_target == GL_TEXTURE_2D_ARRAY)
+ {
+ /* Try to avoid using glGetTexImage() for layered textures. */
+ if ((texture->resource.draw_binding & src_locations)
+ && !(texture->resource.format_flags & WINED3DFMT_FLAG_COMPRESSED)
+ && !(texture->resource.format_flags & (WINED3DFMT_FLAG_DEPTH | WINED3DFMT_FLAG_STENCIL)))
+ {
+ read_from_framebuffer(surface, context, dst_location);
+ return;
+ }
+
+ FIXME("Cannot download surface %p, level %u, layer %u.\n",
+ surface, surface->texture_level, surface->texture_layer);
+ return;
+ }
+
+ wined3d_texture_bind_and_dirtify(texture, context, !(src_locations & WINED3D_LOCATION_TEXTURE_RGB));
+
wined3d_texture_get_memory(texture, surface_get_sub_resource_idx(surface), &data, dst_location);
if (texture->flags & WINED3D_TEXTURE_COND_NP2_EMULATED)
@@ -1701,122 +1834,6 @@ do { \
return WINED3D_OK;
}
-static void read_from_framebuffer(struct wined3d_surface *surface,
- struct wined3d_context *old_ctx, DWORD dst_location)
-{
- unsigned int sub_resource_idx = surface_get_sub_resource_idx(surface);
- struct wined3d_texture *texture = surface->container;
- struct wined3d_device *device = texture->resource.device;
- const struct wined3d_gl_info *gl_info;
- struct wined3d_context *context = old_ctx;
- struct wined3d_surface *restore_rt = NULL;
- unsigned int row_pitch, slice_pitch;
- unsigned int width, height;
- BYTE *mem;
- BYTE *row, *top, *bottom;
- int i;
- BOOL srcIsUpsideDown;
- struct wined3d_bo_address data;
-
- wined3d_texture_get_memory(texture, sub_resource_idx, &data, dst_location);
-
- restore_rt = context_get_rt_surface(old_ctx);
- if (restore_rt != surface)
- context = context_acquire(device, surface);
- else
- restore_rt = NULL;
-
- context_apply_blit_state(context, device);
- gl_info = context->gl_info;
-
- /* Select the correct read buffer, and give some debug output.
- * There is no need to keep track of the current read buffer or reset it, every part of the code
- * that reads sets the read buffer as desired.
- */
- if (wined3d_resource_is_offscreen(&texture->resource))
- {
- /* Mapping the primary render target which is not on a swapchain.
- * Read from the back buffer. */
- TRACE("Mapping offscreen render target.\n");
- gl_info->gl_ops.gl.p_glReadBuffer(context_get_offscreen_gl_buffer(context));
- srcIsUpsideDown = TRUE;
- }
- else
- {
- /* Onscreen surfaces are always part of a swapchain */
- GLenum buffer = wined3d_texture_get_gl_buffer(texture);
- TRACE("Mapping %#x buffer.\n", buffer);
- gl_info->gl_ops.gl.p_glReadBuffer(buffer);
- checkGLcall("glReadBuffer");
- srcIsUpsideDown = FALSE;
- }
-
- if (data.buffer_object)
- {
- GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, data.buffer_object));
- checkGLcall("glBindBuffer");
- }
-
- wined3d_texture_get_pitch(texture, surface->texture_level, &row_pitch, &slice_pitch);
-
- /* Setup pixel store pack state -- to glReadPixels into the correct place */
- gl_info->gl_ops.gl.p_glPixelStorei(GL_PACK_ROW_LENGTH, row_pitch / texture->resource.format->byte_count);
- checkGLcall("glPixelStorei");
-
- width = wined3d_texture_get_level_width(texture, surface->texture_level);
- height = wined3d_texture_get_level_height(texture, surface->texture_level);
- gl_info->gl_ops.gl.p_glReadPixels(0, 0, width, height,
- texture->resource.format->glFormat,
- texture->resource.format->glType, data.addr);
- checkGLcall("glReadPixels");
-
- /* Reset previous pixel store pack state */
- gl_info->gl_ops.gl.p_glPixelStorei(GL_PACK_ROW_LENGTH, 0);
- checkGLcall("glPixelStorei");
-
- if (!srcIsUpsideDown)
- {
- /* glReadPixels returns the image upside down, and there is no way to
- * prevent this. Flip the lines in software. */
-
- if (!(row = HeapAlloc(GetProcessHeap(), 0, row_pitch)))
- goto error;
-
- if (data.buffer_object)
- {
- mem = GL_EXTCALL(glMapBuffer(GL_PIXEL_PACK_BUFFER, GL_READ_WRITE));
- checkGLcall("glMapBuffer");
- }
- else
- mem = data.addr;
-
- top = mem;
- bottom = mem + row_pitch * (height - 1);
- for (i = 0; i < height / 2; i++)
- {
- memcpy(row, top, row_pitch);
- memcpy(top, bottom, row_pitch);
- memcpy(bottom, row, row_pitch);
- top += row_pitch;
- bottom -= row_pitch;
- }
- HeapFree(GetProcessHeap(), 0, row);
-
- if (data.buffer_object)
- GL_EXTCALL(glUnmapBuffer(GL_PIXEL_PACK_BUFFER));
- }
-
-error:
- if (data.buffer_object)
- {
- GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, 0));
- checkGLcall("glBindBuffer");
- }
-
- if (restore_rt)
- context_restore(context, restore_rt);
-}
-
/* Read the framebuffer contents into a texture. Note that this function
* doesn't do any kind of flipping. Using this on an onscreen surface will
* result in a flipped D3D texture.
@@ -2793,7 +2810,6 @@ static void surface_copy_simple_location(struct wined3d_surface *surface, DWORD
static void surface_load_sysmem(struct wined3d_surface *surface,
struct wined3d_context *context, DWORD dst_location)
{
- const struct wined3d_gl_info *gl_info = context->gl_info;
struct wined3d_texture_sub_resource *sub_resource;
wined3d_surface_prepare(surface, context, dst_location);
@@ -2813,9 +2829,7 @@ static void surface_load_sysmem(struct wined3d_surface *surface,
{
struct wined3d_texture *texture = surface->container;
- wined3d_texture_bind_and_dirtify(texture, context,
- !(sub_resource->locations & WINED3D_LOCATION_TEXTURE_RGB));
- surface_download_data(surface, gl_info, dst_location);
+ surface_download_data(surface, context, sub_resource->locations, dst_location);
++texture->download_count;
return;
--
2.4.10
More information about the wine-patches
mailing list