Discussion:
[PATCH 1/2] gallium/util: add u_transfer_helper
Add Reply
Rob Clark
2017-11-29 13:48:30 UTC
Reply
Permalink
Raw Message
Add a new helper that drivers can use to emulate various things that
need special handling in particular in transfer_map:

1) z32_s8x24.. gl/gallium treats this as a single buffer with depth
and stencil interleaved but hardware frequently treats this as
separate z32 and s8 buffers. Special pack/unpack handling is
needed in transfer_map/unmap to pack/unpack the exposed buffer

2) fake RGTC.. GPUs designed with GLES in mind, but which can other-
wise do GL3, if native RGTC is not supported it can be emulated
by converting to uncompressed internally, but needs pack/unpack
in transfer_map/unmap

This could be possibly extended to handle MSAA resolve in map/unmap.

Signed-off-by: Rob Clark <***@gmail.com>
---
src/gallium/auxiliary/Makefile.sources | 2 +
src/gallium/auxiliary/meson.build | 2 +
src/gallium/auxiliary/util/u_transfer_helper.c | 364 +++++++++++++++++++++++++
src/gallium/auxiliary/util/u_transfer_helper.h | 121 ++++++++
src/gallium/include/pipe/p_screen.h | 8 +-
5 files changed, 496 insertions(+), 1 deletion(-)
create mode 100644 src/gallium/auxiliary/util/u_transfer_helper.c
create mode 100644 src/gallium/auxiliary/util/u_transfer_helper.h

diff --git a/src/gallium/auxiliary/Makefile.sources b/src/gallium/auxiliary/Makefile.sources
index f40c4723fae..a2dae04698c 100644
--- a/src/gallium/auxiliary/Makefile.sources
+++ b/src/gallium/auxiliary/Makefile.sources
@@ -304,6 +304,8 @@ C_SOURCES := \
util/u_tile.h \
util/u_transfer.c \
util/u_transfer.h \
+ util/u_transfer_helper.c \
+ util/u_transfer_helper.h \
util/u_threaded_context.c \
util/u_threaded_context.h \
util/u_threaded_context_calls.h \
diff --git a/src/gallium/auxiliary/meson.build b/src/gallium/auxiliary/meson.build
index 3e623fd099f..8c242ec1a05 100644
--- a/src/gallium/auxiliary/meson.build
+++ b/src/gallium/auxiliary/meson.build
@@ -324,6 +324,8 @@ files_libgallium = files(
'util/u_tile.h',
'util/u_transfer.c',
'util/u_transfer.h',
+ 'util/u_transfer_helper.c',
+ 'util/u_transfer_helper.h',
'util/u_threaded_context.c',
'util/u_threaded_context.h',
'util/u_threaded_context_calls.h',
diff --git a/src/gallium/auxiliary/util/u_transfer_helper.c b/src/gallium/auxiliary/util/u_transfer_helper.c
new file mode 100644
index 00000000000..fb4024db35b
--- /dev/null
+++ b/src/gallium/auxiliary/util/u_transfer_helper.c
@@ -0,0 +1,364 @@
+/*
+ * Copyright © 2017 Red Hat
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "pipe/p_screen.h"
+
+#include "util/u_box.h"
+#include "util/u_format.h"
+#include "util/u_format_rgtc.h"
+#include "util/u_format_zs.h"
+#include "util/u_inlines.h"
+#include "util/u_transfer_helper.h"
+
+
+struct u_transfer_helper {
+ const struct u_transfer_vtbl *vtbl;
+ bool separate_z32s8;
+ bool fake_rgtc;
+ bool msaa_map;
+};
+
+static inline bool handle_transfer(struct pipe_resource *prsc)
+{
+ struct u_transfer_helper *helper = prsc->screen->transfer_helper;
+ struct u_transfer_resource *rsc = u_transfer_resource(prsc);
+ return (rsc->internal_format != prsc->format) ||
+ (helper->msaa_map && (prsc->nr_samples > 1));
+}
+
+/* The pipe_transfer ptr could either be the driver's, or u_transfer,
+ * depending on whether we are intervening or not. Check handle_transfer()
+ * before dereferencing.
+ */
+struct u_transfer {
+ struct pipe_transfer base;
+ struct pipe_transfer *trans; /* driver's transfer */
+ struct pipe_transfer *trans2; /* 2nd transfer for z32s8 */
+ void *ptr, *ptr2; /* ptr to trans, and trans2 */
+ void *staging; /* staging buffer */
+};
+
+static inline struct u_transfer *
+u_transfer(struct pipe_transfer *ptrans)
+{
+ debug_assert(handle_transfer(ptrans->resource));
+ return (struct u_transfer *)ptrans;
+}
+
+struct pipe_resource *
+u_transfer_helper_resource_create(struct pipe_screen *pscreen,
+ const struct pipe_resource *templ)
+{
+ struct u_transfer_helper *helper = pscreen->transfer_helper;
+ enum pipe_format format = templ->format;
+ struct pipe_resource *prsc;
+ struct u_transfer_resource *rsc;
+
+ if ((format == PIPE_FORMAT_Z32_FLOAT_S8X24_UINT) && helper->separate_z32s8) {
+ struct pipe_resource t = *templ;
+ t.format = PIPE_FORMAT_Z32_FLOAT;
+
+ prsc = helper->vtbl->resource_create(pscreen, &t);
+ if (!prsc)
+ return NULL;
+
+ rsc = u_transfer_resource(prsc);
+
+ prsc->format = format; /* frob the format back to the "external" format */
+ rsc->internal_format = t.format;
+
+ t.format = PIPE_FORMAT_S8_UINT;
+ rsc->stencil = helper->vtbl->resource_create(pscreen, &t);
+
+ if (!rsc->stencil) {
+ helper->vtbl->resource_destroy(pscreen, prsc);
+ return NULL;
+ }
+ } else if ((util_format_description(format)->layout == UTIL_FORMAT_LAYOUT_RGTC) &&
+ helper->fake_rgtc) {
+ struct pipe_resource t = *templ;
+ t.format = PIPE_FORMAT_R8G8B8A8_UNORM;
+
+ prsc = helper->vtbl->resource_create(pscreen, &t);
+ if (!prsc)
+ return NULL;
+
+ rsc = u_transfer_resource(prsc);
+
+ prsc->format = format; /* frob the format back to the "external" format */
+ rsc->internal_format = t.format;
+ } else {
+ /* normal case, no special handling: */
+ prsc = helper->vtbl->resource_create(pscreen, templ);
+ if (!prsc)
+ return NULL;
+
+ rsc = u_transfer_resource(prsc);
+ rsc->stencil = NULL;
+ rsc->internal_format = prsc->format;
+ }
+
+ return prsc;
+}
+
+void
+u_transfer_helper_resource_destroy(struct pipe_screen *pscreen,
+ struct pipe_resource *prsc)
+{
+ struct u_transfer_helper *helper = pscreen->transfer_helper;
+ struct u_transfer_resource *rsc = u_transfer_resource(prsc);
+
+ if (rsc->stencil)
+ helper->vtbl->resource_destroy(pscreen, rsc->stencil);
+
+ helper->vtbl->resource_destroy(pscreen, prsc);
+}
+
+static bool needs_pack(unsigned usage)
+{
+ return (usage & PIPE_TRANSFER_READ) &&
+ !(usage & (PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE | PIPE_TRANSFER_DISCARD_RANGE));
+}
+
+void *
+u_transfer_helper_transfer_map(struct pipe_context *pctx,
+ struct pipe_resource *prsc,
+ unsigned level,
+ unsigned usage,
+ const struct pipe_box *box,
+ struct pipe_transfer **pptrans)
+{
+ struct u_transfer_helper *helper = pctx->screen->transfer_helper;
+ struct u_transfer_resource *rsc = u_transfer_resource(prsc);
+ struct u_transfer *trans;
+ struct pipe_transfer *ptrans;
+ enum pipe_format format = prsc->format;
+ unsigned width = box->width;
+ unsigned height = box->height;
+
+ if (!handle_transfer(prsc))
+ return helper->vtbl->transfer_map(pctx, prsc, level, usage, box, pptrans);
+
+ debug_assert(box->depth == 1);
+
+ trans = calloc(1, sizeof(*trans));
+ if (!trans)
+ return NULL;
+
+ ptrans = &trans->base;
+ pipe_resource_reference(&ptrans->resource, prsc);
+ ptrans->level = level;
+ ptrans->usage = usage;
+ ptrans->box = *box;
+ ptrans->stride = util_format_get_stride(format, box->width);
+ ptrans->layer_stride = ptrans->stride * box->height;
+
+ trans->staging = malloc(ptrans->layer_stride);
+ if (!trans->staging)
+ goto fail;
+
+ trans->ptr = helper->vtbl->transfer_map(pctx, prsc, level, usage, box,
+ &trans->trans);
+ if (!trans->ptr)
+ goto fail;
+
+ if (prsc->format == PIPE_FORMAT_Z32_FLOAT_S8X24_UINT) {
+ trans->ptr2 = helper->vtbl->transfer_map(pctx, rsc->stencil, level,
+ usage, box, &trans->trans2);
+
+ if (needs_pack(usage)) {
+ util_format_z32_float_s8x24_uint_pack_z_float(trans->staging,
+ ptrans->stride,
+ trans->ptr,
+ trans->trans->stride,
+ width, height);
+ util_format_z32_float_s8x24_uint_pack_s_8uint(trans->staging,
+ ptrans->stride,
+ trans->ptr2,
+ trans->trans2->stride,
+ width, height);
+ }
+ } else if (needs_pack(usage) &&
+ util_format_description(prsc->format)->layout == UTIL_FORMAT_LAYOUT_RGTC) {
+ switch (prsc->format) {
+ case PIPE_FORMAT_RGTC1_UNORM:
+ case PIPE_FORMAT_RGTC1_SNORM:
+ case PIPE_FORMAT_LATC1_UNORM:
+ case PIPE_FORMAT_LATC1_SNORM:
+ util_format_rgtc1_unorm_pack_rgba_8unorm(trans->staging,
+ ptrans->stride,
+ trans->ptr,
+ trans->trans->stride,
+ width, height);
+ break;
+ case PIPE_FORMAT_RGTC2_UNORM:
+ case PIPE_FORMAT_RGTC2_SNORM:
+ case PIPE_FORMAT_LATC2_UNORM:
+ case PIPE_FORMAT_LATC2_SNORM:
+ util_format_rgtc2_unorm_pack_rgba_8unorm(trans->staging,
+ ptrans->stride,
+ trans->ptr,
+ trans->trans->stride,
+ width, height);
+ break;
+ default:
+ assert(!"Unexpected format");
+ break;
+ }
+ } else {
+ unreachable("bleh");
+ }
+
+ *pptrans = ptrans;
+ return trans->staging;
+
+fail:
+ if (trans->trans)
+ helper->vtbl->transfer_unmap(pctx, trans->trans);
+ if (trans->trans2)
+ helper->vtbl->transfer_unmap(pctx, trans->trans2);
+ pipe_resource_reference(&ptrans->resource, NULL);
+ free(trans->staging);
+ free(trans);
+ return NULL;
+}
+
+static void
+flush_region(struct pipe_transfer *ptrans, const struct pipe_box *box)
+{
+ struct u_transfer *trans = u_transfer(ptrans);
+ enum pipe_format format = ptrans->resource->format;
+ unsigned width = ptrans->box.width;
+ unsigned height = ptrans->box.height;
+
+ if (!(ptrans->usage & PIPE_TRANSFER_WRITE))
+ return;
+
+ switch (format) {
+ case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT:
+ util_format_z32_float_s8x24_uint_unpack_z_float(trans->ptr,
+ trans->trans->stride,
+ trans->staging,
+ ptrans->stride,
+ width, height);
+ /* fallthru */
+ case PIPE_FORMAT_X32_S8X24_UINT:
+ util_format_z32_float_s8x24_uint_unpack_s_8uint(trans->ptr2,
+ trans->trans2->stride,
+ trans->staging,
+ ptrans->stride,
+ width, height);
+ break;
+ case PIPE_FORMAT_RGTC1_UNORM:
+ case PIPE_FORMAT_RGTC1_SNORM:
+ case PIPE_FORMAT_LATC1_UNORM:
+ case PIPE_FORMAT_LATC1_SNORM:
+ util_format_rgtc1_unorm_unpack_rgba_8unorm(trans->ptr,
+ trans->trans->stride,
+ trans->staging,
+ ptrans->stride,
+ width, height);
+ break;
+ case PIPE_FORMAT_RGTC2_UNORM:
+ case PIPE_FORMAT_RGTC2_SNORM:
+ case PIPE_FORMAT_LATC2_UNORM:
+ case PIPE_FORMAT_LATC2_SNORM:
+ util_format_rgtc2_unorm_unpack_rgba_8unorm(trans->ptr,
+ trans->trans->stride,
+ trans->staging,
+ ptrans->stride,
+ width, height);
+ break;
+ default:
+ assert(!"Unexpected staging transfer type");
+ break;
+ }
+}
+
+void
+u_transfer_helper_transfer_flush_region(struct pipe_context *pctx,
+ struct pipe_transfer *ptrans,
+ const struct pipe_box *box)
+{
+ struct u_transfer_helper *helper = pctx->screen->transfer_helper;
+
+ if (handle_transfer(ptrans->resource)) {
+ struct u_transfer *trans = u_transfer(ptrans);
+
+ flush_region(ptrans, box);
+
+ helper->vtbl->transfer_flush_region(pctx, trans->trans, box);
+ if (trans->trans2)
+ helper->vtbl->transfer_flush_region(pctx, trans->trans2, box);
+
+ } else {
+ helper->vtbl->transfer_flush_region(pctx, ptrans, box);
+ }
+}
+
+void
+u_transfer_helper_transfer_unmap(struct pipe_context *pctx,
+ struct pipe_transfer *ptrans)
+{
+ struct u_transfer_helper *helper = pctx->screen->transfer_helper;
+
+ if (handle_transfer(ptrans->resource)) {
+ struct u_transfer *trans = u_transfer(ptrans);
+
+ if (!(ptrans->usage & PIPE_TRANSFER_FLUSH_EXPLICIT)) {
+ struct pipe_box box;
+ u_box_2d(0, 0, ptrans->box.width, ptrans->box.height, &box);
+ flush_region(ptrans, &box);
+ }
+
+ helper->vtbl->transfer_unmap(pctx, trans->trans);
+ if (trans->trans2)
+ helper->vtbl->transfer_unmap(pctx, trans->trans2);
+
+ free(trans);
+ } else {
+ helper->vtbl->transfer_unmap(pctx, ptrans);
+ }
+}
+
+struct u_transfer_helper *
+u_transfer_helper_create(const struct u_transfer_vtbl *vtbl,
+ bool separate_z32s8,
+ bool fake_rgtc,
+ bool msaa_map)
+{
+ struct u_transfer_helper *helper = calloc(1, sizeof(*helper));
+
+ helper->vtbl = vtbl;
+ helper->separate_z32s8 = separate_z32s8;
+ helper->fake_rgtc = fake_rgtc;
+ helper->msaa_map = msaa_map;
+
+ return helper;
+}
+
+void
+u_transfer_helper_destroy(struct u_transfer_helper *helper)
+{
+ free(helper);
+}
diff --git a/src/gallium/auxiliary/util/u_transfer_helper.h b/src/gallium/auxiliary/util/u_transfer_helper.h
new file mode 100644
index 00000000000..40848e0d8e5
--- /dev/null
+++ b/src/gallium/auxiliary/util/u_transfer_helper.h
@@ -0,0 +1,121 @@
+/*
+ * Copyright © 2017 Red Hat
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#ifndef _U_TRANSFER_HELPER_H
+#define _U_TRANSFER_HELPER_H
+
+#include "pipe/p_state.h"
+#include "pipe/p_context.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/* A helper to implement various "lowering" for transfers:
+ *
+ * - exposing separate z32 and s8 as z32x24s8
+ * - fake RGTC support for GLES class hardware which needs it to expose GL3+
+ * - MSAA resolves (TODO)
+ *
+ * To use this, drivers should:
+ *
+ * 1) subclass u_transfer_resource instead of pipe_resource directly
+ * 2) populate u_transfer_vtbl and plug that into pipe_screen::transfer_helper
+ * 3) plug the the transfer helpers into pipe_screen/pipe_context
+ */
+
+struct u_transfer_resource {
+ struct pipe_resource b;
+ enum pipe_format internal_format;
+ struct pipe_resource *stencil; /* separate stencil for z32x24s8 */
+};
+
+static inline struct u_transfer_resource *
+u_transfer_resource(struct pipe_resource *prsc)
+{
+ return (struct u_transfer_resource *)prsc;
+}
+
+struct u_transfer_vtbl {
+ /* NOTE I am not expecting resource_create_from_handle() or
+ * resource_create_with_modifiers() paths to be creating any
+ * resources that need special handling. Otherwise they would
+ * need to be wrapped too.
+ */
+ struct pipe_resource * (*resource_create)(struct pipe_screen *pscreen,
+ const struct pipe_resource *templ);
+
+ void (*resource_destroy)(struct pipe_screen *pscreen,
+ struct pipe_resource *prsc);
+
+ void *(*transfer_map)(struct pipe_context *pctx,
+ struct pipe_resource *prsc,
+ unsigned level,
+ unsigned usage,
+ const struct pipe_box *box,
+ struct pipe_transfer **pptrans);
+
+
+ void (*transfer_flush_region)(struct pipe_context *pctx,
+ struct pipe_transfer *ptrans,
+ const struct pipe_box *box);
+
+ void (*transfer_unmap)(struct pipe_context *pctx,
+ struct pipe_transfer *ptrans);
+};
+
+struct pipe_resource *u_transfer_helper_resource_create(
+ struct pipe_screen *pscreen, const struct pipe_resource *templ);
+
+void u_transfer_helper_resource_destroy(struct pipe_screen *pscreen,
+ struct pipe_resource *prsc);
+
+void *u_transfer_helper_transfer_map(struct pipe_context *pctx,
+ struct pipe_resource *prsc,
+ unsigned level,
+ unsigned usage,
+ const struct pipe_box *box,
+ struct pipe_transfer **pptrans);
+
+
+void u_transfer_helper_transfer_flush_region(struct pipe_context *pctx,
+ struct pipe_transfer *ptrans,
+ const struct pipe_box *box);
+
+void u_transfer_helper_transfer_unmap(struct pipe_context *pctx,
+ struct pipe_transfer *ptrans);
+
+struct u_transfer_helper;
+
+struct u_transfer_helper * u_transfer_helper_create(const struct u_transfer_vtbl *vtbl,
+ bool separate_z32s8,
+ bool fake_rgtc,
+ bool msaa_map);
+
+void u_transfer_helper_destroy(struct u_transfer_helper *helper);
+
+#ifdef __cplusplus
+} // extern "C" {
+#endif
+
+#endif /* _U_TRANSFER_HELPER_H */
diff --git a/src/gallium/include/pipe/p_screen.h b/src/gallium/include/pipe/p_screen.h
index c249c7d63b4..101e229088b 100644
--- a/src/gallium/include/pipe/p_screen.h
+++ b/src/gallium/include/pipe/p_screen.h
@@ -60,7 +60,7 @@ struct pipe_box;
struct pipe_memory_info;
struct disk_cache;
struct driOptionCache;
-
+struct u_transfer_helper;

/**
* Gallium screen/adapter context. Basically everything
@@ -68,6 +68,12 @@ struct driOptionCache;
* context.
*/
struct pipe_screen {
+
+ /**
+ * For drivers using u_transfer_helper:
+ */
+ struct u_transfer_helper *transfer_helper;
+
void (*destroy)( struct pipe_screen * );

const char *(*get_name)( struct pipe_screen * );
--
2.13.6
Rob Clark
2017-11-29 13:48:31 UTC
Reply
Permalink
Raw Message
Also removes usage of u_resource, which was basically useless for
freedreno. Perhaps that should be two commits, but that would have
required undoing and then re-doing &rsc->base.b stuff.

Signed-off-by: Rob Clark <***@gmail.com>
---
src/gallium/drivers/freedreno/a3xx/fd3_emit.c | 8 +-
src/gallium/drivers/freedreno/a3xx/fd3_gmem.c | 15 +-
src/gallium/drivers/freedreno/a4xx/fd4_emit.c | 6 +-
src/gallium/drivers/freedreno/a4xx/fd4_gmem.c | 17 +-
src/gallium/drivers/freedreno/a4xx/fd4_texture.c | 2 +-
src/gallium/drivers/freedreno/a5xx/fd5_emit.c | 2 +-
src/gallium/drivers/freedreno/a5xx/fd5_gmem.c | 19 +-
src/gallium/drivers/freedreno/a5xx/fd5_texture.c | 2 +-
src/gallium/drivers/freedreno/freedreno_batch.c | 4 +-
src/gallium/drivers/freedreno/freedreno_gmem.c | 4 +-
src/gallium/drivers/freedreno/freedreno_resource.c | 261 ++-------------------
src/gallium/drivers/freedreno/freedreno_resource.h | 12 +-
12 files changed, 67 insertions(+), 285 deletions(-)

diff --git a/src/gallium/drivers/freedreno/a3xx/fd3_emit.c b/src/gallium/drivers/freedreno/a3xx/fd3_emit.c
index aefbbea4a7c..f1577de08d8 100644
--- a/src/gallium/drivers/freedreno/a3xx/fd3_emit.c
+++ b/src/gallium/drivers/freedreno/a3xx/fd3_emit.c
@@ -306,8 +306,8 @@ fd3_emit_gmem_restore_tex(struct fd_ringbuffer *ring,
/* The restore blit_zs shader expects stencil in sampler 0, and depth
* in sampler 1
*/
- if (rsc->stencil && i == 0) {
- rsc = rsc->stencil;
+ if (rsc->base.stencil && i == 0) {
+ rsc = fd_resource(rsc->base.stencil);
format = fd_gmem_restore_format(rsc->base.b.format);
}

@@ -341,8 +341,8 @@ fd3_emit_gmem_restore_tex(struct fd_ringbuffer *ring,
if (psurf[i]) {
struct fd_resource *rsc = fd_resource(psurf[i]->texture);
/* Matches above logic for blit_zs shader */
- if (rsc->stencil && i == 0)
- rsc = rsc->stencil;
+ if (rsc->base.stencil && i == 0)
+ rsc = fd_resource(rsc->base.stencil);
unsigned lvl = psurf[i]->u.tex.level;
uint32_t offset = fd_resource_offset(rsc, lvl, psurf[i]->u.tex.first_layer);
OUT_RELOC(ring, rsc->bo, offset, 0, 0);
diff --git a/src/gallium/drivers/freedreno/a3xx/fd3_gmem.c b/src/gallium/drivers/freedreno/a3xx/fd3_gmem.c
index 4bbbcf90ffa..c12eaf3bb3e 100644
--- a/src/gallium/drivers/freedreno/a3xx/fd3_gmem.c
+++ b/src/gallium/drivers/freedreno/a3xx/fd3_gmem.c
@@ -76,8 +76,8 @@ emit_mrt(struct fd_ringbuffer *ring, unsigned nr_bufs,
/* In case we're drawing to Z32F_S8, the "color" actually goes to
* the stencil
*/
- if (rsc->stencil) {
- rsc = rsc->stencil;
+ if (rsc->base.stencil) {
+ rsc = fd_resource(rsc->base.stencil);
pformat = rsc->base.b.format;
if (bases)
bases++;
@@ -323,7 +323,7 @@ emit_gmem2mem_surf(struct fd_batch *batch,
struct fd_resource *rsc = fd_resource(psurf->texture);
enum pipe_format format = psurf->format;
if (stencil) {
- rsc = rsc->stencil;
+ rsc = fd_resource(rsc->base.stencil);
format = rsc->base.b.format;
}
struct fd_resource_slice *slice = fd_resource_slice(rsc, psurf->u.tex.level);
@@ -445,10 +445,10 @@ fd3_emit_tile_gmem2mem(struct fd_batch *batch, struct fd_tile *tile)

if (batch->resolve & (FD_BUFFER_DEPTH | FD_BUFFER_STENCIL)) {
struct fd_resource *rsc = fd_resource(pfb->zsbuf->texture);
- if (!rsc->stencil || batch->resolve & FD_BUFFER_DEPTH)
+ if (!rsc->base.stencil || batch->resolve & FD_BUFFER_DEPTH)
emit_gmem2mem_surf(batch, RB_COPY_DEPTH_STENCIL, false,
ctx->gmem.zsbuf_base[0], pfb->zsbuf);
- if (rsc->stencil && batch->resolve & FD_BUFFER_STENCIL)
+ if (rsc->base.stencil && batch->resolve & FD_BUFFER_STENCIL)
emit_gmem2mem_surf(batch, RB_COPY_DEPTH_STENCIL, true,
ctx->gmem.zsbuf_base[1], pfb->zsbuf);
}
@@ -1001,10 +1001,11 @@ fd3_emit_tile_renderprep(struct fd_batch *batch, struct fd_tile *tile)
if (pfb->zsbuf) {
struct fd_resource *rsc = fd_resource(pfb->zsbuf->texture);
OUT_RING(ring, A3XX_RB_DEPTH_PITCH(rsc->cpp * gmem->bin_w));
- if (rsc->stencil) {
+ if (rsc->base.stencil) {
+ struct fd_resource *stencil = fd_resource(rsc->base.stencil);
OUT_PKT0(ring, REG_A3XX_RB_STENCIL_INFO, 2);
OUT_RING(ring, A3XX_RB_STENCIL_INFO_STENCIL_BASE(gmem->zsbuf_base[1]));
- OUT_RING(ring, A3XX_RB_STENCIL_PITCH(rsc->stencil->cpp * gmem->bin_w));
+ OUT_RING(ring, A3XX_RB_STENCIL_PITCH(stencil->cpp * gmem->bin_w));
}
} else {
OUT_RING(ring, 0x00000000);
diff --git a/src/gallium/drivers/freedreno/a4xx/fd4_emit.c b/src/gallium/drivers/freedreno/a4xx/fd4_emit.c
index 8262b45daad..e17d861ad3f 100644
--- a/src/gallium/drivers/freedreno/a4xx/fd4_emit.c
+++ b/src/gallium/drivers/freedreno/a4xx/fd4_emit.c
@@ -191,7 +191,7 @@ emit_textures(struct fd_context *ctx, struct fd_ringbuffer *ring,
if (view->base.texture) {
struct fd_resource *rsc = fd_resource(view->base.texture);
if (view->base.format == PIPE_FORMAT_X32_S8X24_UINT)
- rsc = rsc->stencil;
+ rsc = fd_resource(rsc->base.stencil);
OUT_RELOC(ring, rsc->bo, view->offset, view->texconst4, 0);
} else {
OUT_RING(ring, 0x00000000);
@@ -296,8 +296,8 @@ fd4_emit_gmem_restore_tex(struct fd_ringbuffer *ring, unsigned nr_bufs,
/* The restore blit_zs shader expects stencil in sampler 0,
* and depth in sampler 1
*/
- if (rsc->stencil && (i == 0)) {
- rsc = rsc->stencil;
+ if (rsc->base.stencil && (i == 0)) {
+ rsc = fd_resource(rsc->base.stencil);
format = fd_gmem_restore_format(rsc->base.b.format);
}

diff --git a/src/gallium/drivers/freedreno/a4xx/fd4_gmem.c b/src/gallium/drivers/freedreno/a4xx/fd4_gmem.c
index ebfbcabf67d..e0d7c08d094 100644
--- a/src/gallium/drivers/freedreno/a4xx/fd4_gmem.c
+++ b/src/gallium/drivers/freedreno/a4xx/fd4_gmem.c
@@ -77,8 +77,8 @@ emit_mrt(struct fd_ringbuffer *ring, unsigned nr_bufs,
/* In case we're drawing to Z32F_S8, the "color" actually goes to
* the stencil
*/
- if (rsc->stencil) {
- rsc = rsc->stencil;
+ if (rsc->base.stencil) {
+ rsc = fd_resource(rsc->base.stencil);
pformat = rsc->base.b.format;
if (bases)
bases++;
@@ -158,8 +158,8 @@ emit_gmem2mem_surf(struct fd_batch *batch, bool stencil,
uint32_t offset;

if (stencil) {
- debug_assert(rsc->stencil);
- rsc = rsc->stencil;
+ debug_assert(rsc->base.stencil);
+ rsc = fd_resource(rsc->base.stencil);
pformat = rsc->base.b.format;
}

@@ -272,9 +272,9 @@ fd4_emit_tile_gmem2mem(struct fd_batch *batch, struct fd_tile *tile)

if (batch->resolve & (FD_BUFFER_DEPTH | FD_BUFFER_STENCIL)) {
struct fd_resource *rsc = fd_resource(pfb->zsbuf->texture);
- if (!rsc->stencil || (batch->resolve & FD_BUFFER_DEPTH))
+ if (!rsc->base.stencil || (batch->resolve & FD_BUFFER_DEPTH))
emit_gmem2mem_surf(batch, false, ctx->gmem.zsbuf_base[0], pfb->zsbuf);
- if (rsc->stencil && (batch->resolve & FD_BUFFER_STENCIL))
+ if (rsc->base.stencil && (batch->resolve & FD_BUFFER_STENCIL))
emit_gmem2mem_surf(batch, true, ctx->gmem.zsbuf_base[1], pfb->zsbuf);
}

@@ -723,10 +723,11 @@ fd4_emit_tile_prep(struct fd_batch *batch, struct fd_tile *tile)
OUT_RING(ring, A4XX_RB_DEPTH_PITCH2(cpp * gmem->bin_w));

OUT_PKT0(ring, REG_A4XX_RB_STENCIL_INFO, 2);
- if (rsc->stencil) {
+ if (rsc->base.stencil) {
+ struct fd_resource *stencil = fd_resource(rsc->base.stencil);
OUT_RING(ring, A4XX_RB_STENCIL_INFO_SEPARATE_STENCIL |
A4XX_RB_STENCIL_INFO_STENCIL_BASE(gmem->zsbuf_base[1]));
- OUT_RING(ring, A4XX_RB_STENCIL_PITCH(rsc->stencil->cpp * gmem->bin_w));
+ OUT_RING(ring, A4XX_RB_STENCIL_PITCH(stencil->cpp * gmem->bin_w));
} else {
OUT_RING(ring, 0x00000000);
OUT_RING(ring, 0x00000000);
diff --git a/src/gallium/drivers/freedreno/a4xx/fd4_texture.c b/src/gallium/drivers/freedreno/a4xx/fd4_texture.c
index 4b2ca463458..544c417f281 100644
--- a/src/gallium/drivers/freedreno/a4xx/fd4_texture.c
+++ b/src/gallium/drivers/freedreno/a4xx/fd4_texture.c
@@ -232,7 +232,7 @@ fd4_sampler_view_create(struct pipe_context *pctx, struct pipe_resource *prsc,
return NULL;

if (format == PIPE_FORMAT_X32_S8X24_UINT) {
- rsc = rsc->stencil;
+ rsc = fd_resource(rsc->base.stencil);
format = rsc->base.b.format;
}

diff --git a/src/gallium/drivers/freedreno/a5xx/fd5_emit.c b/src/gallium/drivers/freedreno/a5xx/fd5_emit.c
index 58c837cfd17..6cf9ee1196f 100644
--- a/src/gallium/drivers/freedreno/a5xx/fd5_emit.c
+++ b/src/gallium/drivers/freedreno/a5xx/fd5_emit.c
@@ -345,7 +345,7 @@ emit_textures(struct fd_context *ctx, struct fd_ringbuffer *ring,
if (view->base.texture) {
struct fd_resource *rsc = fd_resource(view->base.texture);
if (view->base.format == PIPE_FORMAT_X32_S8X24_UINT)
- rsc = rsc->stencil;
+ rsc = fd_resource(rsc->base.stencil);
OUT_RELOC(ring, rsc->bo, view->offset,
(uint64_t)view->texconst5 << 32, 0);
} else {
diff --git a/src/gallium/drivers/freedreno/a5xx/fd5_gmem.c b/src/gallium/drivers/freedreno/a5xx/fd5_gmem.c
index 7b655954742..ce8bcb86816 100644
--- a/src/gallium/drivers/freedreno/a5xx/fd5_gmem.c
+++ b/src/gallium/drivers/freedreno/a5xx/fd5_gmem.c
@@ -184,12 +184,13 @@ emit_zs(struct fd_ringbuffer *ring, struct pipe_surface *zsbuf,
OUT_RING(ring, 0x00000000);
}

- if (rsc->stencil) {
+ if (rsc->base.stencil) {
+ struct fd_resource *stencil = fd_resource(rsc->base.stencil);
if (gmem) {
stride = 1 * gmem->bin_w;
size = stride * gmem->bin_h;
} else {
- struct fd_resource_slice *slice = fd_resource_slice(rsc->stencil, 0);
+ struct fd_resource_slice *slice = fd_resource_slice(stencil, 0);
stride = slice->pitch * rsc->cpp;
size = slice->size0;
}
@@ -200,7 +201,7 @@ emit_zs(struct fd_ringbuffer *ring, struct pipe_surface *zsbuf,
OUT_RING(ring, gmem->zsbuf_base[1]); /* RB_STENCIL_BASE_LO */
OUT_RING(ring, 0x00000000); /* RB_STENCIL_BASE_HI */
} else {
- OUT_RELOCW(ring, rsc->stencil->bo, 0, 0, 0); /* RB_STENCIL_BASE_LO/HI */
+ OUT_RELOCW(ring, stencil->bo, 0, 0, 0); /* RB_STENCIL_BASE_LO/HI */
}
OUT_RING(ring, A5XX_RB_STENCIL_PITCH(stride));
OUT_RING(ring, A5XX_RB_STENCIL_ARRAY_PITCH(size));
@@ -474,7 +475,7 @@ emit_mem2gmem_surf(struct fd_batch *batch, uint32_t base,
debug_assert(psurf->u.tex.first_layer == psurf->u.tex.last_layer);

if (buf == BLIT_S)
- rsc = rsc->stencil;
+ rsc = fd_resource(rsc->base.stencil);

if ((buf == BLIT_ZS) || (buf == BLIT_S)) {
// XXX hack import via BLIT_MRT0 instead of BLIT_ZS, since I don't
@@ -554,9 +555,9 @@ fd5_emit_tile_mem2gmem(struct fd_batch *batch, struct fd_tile *tile)
if (fd_gmem_needs_restore(batch, tile, FD_BUFFER_DEPTH | FD_BUFFER_STENCIL)) {
struct fd_resource *rsc = fd_resource(pfb->zsbuf->texture);

- if (!rsc->stencil || fd_gmem_needs_restore(batch, tile, FD_BUFFER_DEPTH))
+ if (!rsc->base.stencil || fd_gmem_needs_restore(batch, tile, FD_BUFFER_DEPTH))
emit_mem2gmem_surf(batch, gmem->zsbuf_base[0], pfb->zsbuf, BLIT_ZS);
- if (rsc->stencil && fd_gmem_needs_restore(batch, tile, FD_BUFFER_STENCIL))
+ if (rsc->base.stencil && fd_gmem_needs_restore(batch, tile, FD_BUFFER_STENCIL))
emit_mem2gmem_surf(batch, gmem->zsbuf_base[1], pfb->zsbuf, BLIT_S);
}
}
@@ -609,7 +610,7 @@ emit_gmem2mem_surf(struct fd_batch *batch, uint32_t base,
uint32_t offset;

if (buf == BLIT_S)
- rsc = rsc->stencil;
+ rsc = fd_resource(rsc->base.stencil);

slice = fd_resource_slice(rsc, psurf->u.tex.level);
offset = fd_resource_offset(rsc, psurf->u.tex.level,
@@ -645,9 +646,9 @@ fd5_emit_tile_gmem2mem(struct fd_batch *batch, struct fd_tile *tile)
if (batch->resolve & (FD_BUFFER_DEPTH | FD_BUFFER_STENCIL)) {
struct fd_resource *rsc = fd_resource(pfb->zsbuf->texture);

- if (!rsc->stencil || (batch->resolve & FD_BUFFER_DEPTH))
+ if (!rsc->base.stencil || (batch->resolve & FD_BUFFER_DEPTH))
emit_gmem2mem_surf(batch, gmem->zsbuf_base[0], pfb->zsbuf, BLIT_ZS);
- if (rsc->stencil && (batch->resolve & FD_BUFFER_STENCIL))
+ if (rsc->base.stencil && (batch->resolve & FD_BUFFER_STENCIL))
emit_gmem2mem_surf(batch, gmem->zsbuf_base[1], pfb->zsbuf, BLIT_S);
}

diff --git a/src/gallium/drivers/freedreno/a5xx/fd5_texture.c b/src/gallium/drivers/freedreno/a5xx/fd5_texture.c
index da35f2a796a..61233550885 100644
--- a/src/gallium/drivers/freedreno/a5xx/fd5_texture.c
+++ b/src/gallium/drivers/freedreno/a5xx/fd5_texture.c
@@ -205,7 +205,7 @@ fd5_sampler_view_create(struct pipe_context *pctx, struct pipe_resource *prsc,
return NULL;

if (format == PIPE_FORMAT_X32_S8X24_UINT) {
- rsc = rsc->stencil;
+ rsc = fd_resource(rsc->base.stencil);
format = rsc->base.b.format;
}

diff --git a/src/gallium/drivers/freedreno/freedreno_batch.c b/src/gallium/drivers/freedreno/freedreno_batch.c
index 8f0f78861cf..dc59e3c2390 100644
--- a/src/gallium/drivers/freedreno/freedreno_batch.c
+++ b/src/gallium/drivers/freedreno/freedreno_batch.c
@@ -357,8 +357,8 @@ fd_batch_resource_used(struct fd_batch *batch, struct fd_resource *rsc, bool wri
{
pipe_mutex_assert_locked(batch->ctx->screen->lock);

- if (rsc->stencil)
- fd_batch_resource_used(batch, rsc->stencil, write);
+ if (rsc->base.stencil)
+ fd_batch_resource_used(batch, fd_resource(rsc->base.stencil), write);

DBG("%p: %s %p", batch, write ? "write" : "read", rsc);

diff --git a/src/gallium/drivers/freedreno/freedreno_gmem.c b/src/gallium/drivers/freedreno/freedreno_gmem.c
index fef76733abf..935a8266317 100644
--- a/src/gallium/drivers/freedreno/freedreno_gmem.c
+++ b/src/gallium/drivers/freedreno/freedreno_gmem.c
@@ -127,8 +127,8 @@ calculate_tiles(struct fd_batch *batch)
if (has_zs) {
struct fd_resource *rsc = fd_resource(pfb->zsbuf->texture);
zsbuf_cpp[0] = rsc->cpp;
- if (rsc->stencil)
- zsbuf_cpp[1] = rsc->stencil->cpp;
+ if (rsc->base.stencil)
+ zsbuf_cpp[1] = fd_resource(rsc->base.stencil)->cpp;
}
for (i = 0; i < pfb->nr_cbufs; i++) {
if (pfb->cbufs[i])
diff --git a/src/gallium/drivers/freedreno/freedreno_resource.c b/src/gallium/drivers/freedreno/freedreno_resource.c
index 880666d3af5..d9e41875332 100644
--- a/src/gallium/drivers/freedreno/freedreno_resource.c
+++ b/src/gallium/drivers/freedreno/freedreno_resource.c
@@ -299,114 +299,16 @@ fd_resource_layer_offset(struct fd_resource *rsc,
return layer * slice->size0;
}

-static void
-fd_resource_flush_z32s8(struct fd_transfer *trans, const struct pipe_box *box)
-{
- struct fd_resource *rsc = fd_resource(trans->base.resource);
- struct fd_resource_slice *slice = fd_resource_slice(rsc, trans->base.level);
- struct fd_resource_slice *sslice = fd_resource_slice(rsc->stencil, trans->base.level);
- enum pipe_format format = trans->base.resource->format;
-
- float *depth = fd_bo_map(rsc->bo) + slice->offset +
- fd_resource_layer_offset(rsc, slice, trans->base.box.z) +
- (trans->base.box.y + box->y) * slice->pitch * 4 + (trans->base.box.x + box->x) * 4;
- uint8_t *stencil = fd_bo_map(rsc->stencil->bo) + sslice->offset +
- fd_resource_layer_offset(rsc->stencil, sslice, trans->base.box.z) +
- (trans->base.box.y + box->y) * sslice->pitch + trans->base.box.x + box->x;
-
- if (format != PIPE_FORMAT_X32_S8X24_UINT)
- util_format_z32_float_s8x24_uint_unpack_z_float(
- depth, slice->pitch * 4,
- trans->staging, trans->base.stride,
- box->width, box->height);
-
- util_format_z32_float_s8x24_uint_unpack_s_8uint(
- stencil, sslice->pitch,
- trans->staging, trans->base.stride,
- box->width, box->height);
-}
-
-static void
-fd_resource_flush_rgtc(struct fd_transfer *trans, const struct pipe_box *box)
-{
- struct fd_resource *rsc = fd_resource(trans->base.resource);
- struct fd_resource_slice *slice = fd_resource_slice(rsc, trans->base.level);
- enum pipe_format format = trans->base.resource->format;
-
- uint8_t *data = fd_bo_map(rsc->bo) + slice->offset +
- fd_resource_layer_offset(rsc, slice, trans->base.box.z) +
- ((trans->base.box.y + box->y) * slice->pitch +
- trans->base.box.x + box->x) * rsc->cpp;
-
- uint8_t *source = trans->staging +
- util_format_get_nblocksy(format, box->y) * trans->base.stride +
- util_format_get_stride(format, box->x);
-
- switch (format) {
- case PIPE_FORMAT_RGTC1_UNORM:
- case PIPE_FORMAT_RGTC1_SNORM:
- case PIPE_FORMAT_LATC1_UNORM:
- case PIPE_FORMAT_LATC1_SNORM:
- util_format_rgtc1_unorm_unpack_rgba_8unorm(
- data, slice->pitch * rsc->cpp,
- source, trans->base.stride,
- box->width, box->height);
- break;
- case PIPE_FORMAT_RGTC2_UNORM:
- case PIPE_FORMAT_RGTC2_SNORM:
- case PIPE_FORMAT_LATC2_UNORM:
- case PIPE_FORMAT_LATC2_SNORM:
- util_format_rgtc2_unorm_unpack_rgba_8unorm(
- data, slice->pitch * rsc->cpp,
- source, trans->base.stride,
- box->width, box->height);
- break;
- default:
- assert(!"Unexpected format\n");
- break;
- }
-}
-
-static void
-fd_resource_flush(struct fd_transfer *trans, const struct pipe_box *box)
-{
- enum pipe_format format = trans->base.resource->format;
-
- switch (format) {
- case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT:
- case PIPE_FORMAT_X32_S8X24_UINT:
- fd_resource_flush_z32s8(trans, box);
- break;
- case PIPE_FORMAT_RGTC1_UNORM:
- case PIPE_FORMAT_RGTC1_SNORM:
- case PIPE_FORMAT_RGTC2_UNORM:
- case PIPE_FORMAT_RGTC2_SNORM:
- case PIPE_FORMAT_LATC1_UNORM:
- case PIPE_FORMAT_LATC1_SNORM:
- case PIPE_FORMAT_LATC2_UNORM:
- case PIPE_FORMAT_LATC2_SNORM:
- fd_resource_flush_rgtc(trans, box);
- break;
- default:
- assert(!"Unexpected staging transfer type");
- break;
- }
-}
-
static void fd_resource_transfer_flush_region(struct pipe_context *pctx,
struct pipe_transfer *ptrans,
const struct pipe_box *box)
{
struct fd_resource *rsc = fd_resource(ptrans->resource);
- struct fd_transfer *trans = fd_transfer(ptrans);

if (ptrans->resource->target == PIPE_BUFFER)
util_range_add(&rsc->valid_buffer_range,
ptrans->box.x + box->x,
ptrans->box.x + box->x + box->width);
-
- if (trans->staging)
- fd_resource_flush(trans, box);
}

static void
@@ -415,18 +317,9 @@ fd_resource_transfer_unmap(struct pipe_context *pctx,
{
struct fd_context *ctx = fd_context(pctx);
struct fd_resource *rsc = fd_resource(ptrans->resource);
- struct fd_transfer *trans = fd_transfer(ptrans);
-
- if (trans->staging && !(ptrans->usage & PIPE_TRANSFER_FLUSH_EXPLICIT)) {
- struct pipe_box box;
- u_box_2d(0, 0, ptrans->box.width, ptrans->box.height, &box);
- fd_resource_flush(trans, &box);
- }

if (!(ptrans->usage & PIPE_TRANSFER_UNSYNCHRONIZED)) {
fd_bo_cpu_fini(rsc->bo);
- if (rsc->stencil)
- fd_bo_cpu_fini(rsc->stencil->bo);
}

util_range_add(&rsc->valid_buffer_range,
@@ -435,8 +328,6 @@ fd_resource_transfer_unmap(struct pipe_context *pctx,

pipe_resource_reference(&ptrans->resource, NULL);
slab_free(&ctx->transfer_pool, ptrans);
-
- free(trans->staging);
}

static void *
@@ -486,8 +377,6 @@ fd_resource_transfer_map(struct pipe_context *pctx,

if (usage & PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE) {
realloc_bo(rsc, fd_bo_size(rsc->bo));
- if (rsc->stencil)
- realloc_bo(rsc->stencil, fd_bo_size(rsc->stencil->bo));
rebind_resource(ctx, prsc);
} else if ((usage & PIPE_TRANSFER_WRITE) &&
prsc->target == PIPE_BUFFER &&
@@ -569,97 +458,6 @@ fd_resource_transfer_map(struct pipe_context *pctx,
box->x / util_format_get_blockwidth(format) * rsc->cpp +
fd_resource_layer_offset(rsc, slice, box->z);

- if (prsc->format == PIPE_FORMAT_Z32_FLOAT_S8X24_UINT ||
- prsc->format == PIPE_FORMAT_X32_S8X24_UINT) {
- assert(trans->base.box.depth == 1);
-
- trans->base.stride = trans->base.box.width * rsc->cpp * 2;
- trans->staging = malloc(trans->base.stride * trans->base.box.height);
- if (!trans->staging)
- goto fail;
-
- /* if we're not discarding the whole range (or resource), we must copy
- * the real data in.
- */
- if (!(usage & (PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE |
- PIPE_TRANSFER_DISCARD_RANGE))) {
- struct fd_resource_slice *sslice =
- fd_resource_slice(rsc->stencil, level);
- void *sbuf = fd_bo_map(rsc->stencil->bo);
- if (!sbuf)
- goto fail;
-
- float *depth = (float *)(buf + slice->offset +
- fd_resource_layer_offset(rsc, slice, box->z) +
- box->y * slice->pitch * 4 + box->x * 4);
- uint8_t *stencil = sbuf + sslice->offset +
- fd_resource_layer_offset(rsc->stencil, sslice, box->z) +
- box->y * sslice->pitch + box->x;
-
- if (format != PIPE_FORMAT_X32_S8X24_UINT)
- util_format_z32_float_s8x24_uint_pack_z_float(
- trans->staging, trans->base.stride,
- depth, slice->pitch * 4,
- box->width, box->height);
-
- util_format_z32_float_s8x24_uint_pack_s_8uint(
- trans->staging, trans->base.stride,
- stencil, sslice->pitch,
- box->width, box->height);
- }
-
- buf = trans->staging;
- offset = 0;
- } else if (rsc->internal_format != format &&
- util_format_description(format)->layout == UTIL_FORMAT_LAYOUT_RGTC) {
- assert(trans->base.box.depth == 1);
-
- trans->base.stride = util_format_get_stride(
- format, trans->base.box.width);
- trans->staging = malloc(
- util_format_get_2d_size(format, trans->base.stride,
- trans->base.box.height));
- if (!trans->staging)
- goto fail;
-
- /* if we're not discarding the whole range (or resource), we must copy
- * the real data in.
- */
- if (!(usage & (PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE |
- PIPE_TRANSFER_DISCARD_RANGE))) {
- uint8_t *rgba8 = (uint8_t *)buf + slice->offset +
- fd_resource_layer_offset(rsc, slice, box->z) +
- box->y * slice->pitch * rsc->cpp + box->x * rsc->cpp;
-
- switch (format) {
- case PIPE_FORMAT_RGTC1_UNORM:
- case PIPE_FORMAT_RGTC1_SNORM:
- case PIPE_FORMAT_LATC1_UNORM:
- case PIPE_FORMAT_LATC1_SNORM:
- util_format_rgtc1_unorm_pack_rgba_8unorm(
- trans->staging, trans->base.stride,
- rgba8, slice->pitch * rsc->cpp,
- box->width, box->height);
- break;
- case PIPE_FORMAT_RGTC2_UNORM:
- case PIPE_FORMAT_RGTC2_SNORM:
- case PIPE_FORMAT_LATC2_UNORM:
- case PIPE_FORMAT_LATC2_SNORM:
- util_format_rgtc2_unorm_pack_rgba_8unorm(
- trans->staging, trans->base.stride,
- rgba8, slice->pitch * rsc->cpp,
- box->width, box->height);
- break;
- default:
- assert(!"Unexpected format");
- break;
- }
- }
-
- buf = trans->staging;
- offset = 0;
- }
-
*pptrans = ptrans;

return buf + offset;
@@ -683,8 +481,10 @@ fd_resource_destroy(struct pipe_screen *pscreen,

static boolean
fd_resource_get_handle(struct pipe_screen *pscreen,
+ struct pipe_context *pctx,
struct pipe_resource *prsc,
- struct winsys_handle *handle)
+ struct winsys_handle *handle,
+ unsigned usage)
{
struct fd_resource *rsc = fd_resource(prsc);

@@ -693,14 +493,6 @@ fd_resource_get_handle(struct pipe_screen *pscreen,
}


-static const struct u_resource_vtbl fd_resource_vtbl = {
- .resource_get_handle = fd_resource_get_handle,
- .resource_destroy = fd_resource_destroy,
- .transfer_map = fd_resource_transfer_map,
- .transfer_flush_region = fd_resource_transfer_flush_region,
- .transfer_unmap = fd_resource_transfer_unmap,
-};
-
static uint32_t
setup_slices(struct fd_resource *rsc, uint32_t alignment, enum pipe_format format)
{
@@ -836,14 +628,6 @@ fd_resource_create(struct pipe_screen *pscreen,

util_range_init(&rsc->valid_buffer_range);

- rsc->base.vtbl = &fd_resource_vtbl;
-
- if (format == PIPE_FORMAT_Z32_FLOAT_S8X24_UINT)
- format = PIPE_FORMAT_Z32_FLOAT;
- else if (screen->gpu_id < 400 &&
- util_format_description(format)->layout == UTIL_FORMAT_LAYOUT_RGTC)
- format = PIPE_FORMAT_R8G8B8A8_UNORM;
- rsc->internal_format = format;
rsc->cpp = util_format_get_blocksize(format);

assert(rsc->cpp);
@@ -897,19 +681,6 @@ fd_resource_create(struct pipe_screen *pscreen,
if (!rsc->bo)
goto fail;

- /* There is no native Z32F_S8 sampling or rendering format, so this must
- * be emulated via two separate textures. The depth texture still keeps
- * its Z32F_S8 format though, and we also keep a reference to a separate
- * S8 texture.
- */
- if (tmpl->format == PIPE_FORMAT_Z32_FLOAT_S8X24_UINT) {
- struct pipe_resource stencil = *tmpl;
- stencil.format = PIPE_FORMAT_S8_UINT;
- rsc->stencil = fd_resource(fd_resource_create(pscreen, &stencil));
- if (!rsc->stencil)
- goto fail;
- }
-
return prsc;
fail:
fd_resource_destroy(pscreen, prsc);
@@ -953,7 +724,6 @@ fd_resource_from_handle(struct pipe_screen *pscreen,
if (!rsc->bo)
goto fail;

- rsc->base.vtbl = &fd_resource_vtbl;
rsc->cpp = util_format_get_blocksize(tmpl->format);
slice->pitch = handle->stride / rsc->cpp;
slice->offset = handle->offset;
@@ -1185,21 +955,34 @@ fd_invalidate_resource(struct pipe_context *pctx, struct pipe_resource *prsc)
}
}

+static const struct u_transfer_vtbl transfer_vtbl = {
+ .resource_create = fd_resource_create,
+ .resource_destroy = fd_resource_destroy,
+ .transfer_map = fd_resource_transfer_map,
+ .transfer_flush_region = fd_resource_transfer_flush_region,
+ .transfer_unmap = fd_resource_transfer_unmap,
+};
+
void
fd_resource_screen_init(struct pipe_screen *pscreen)
{
- pscreen->resource_create = fd_resource_create;
+ bool fake_rgtc = fd_screen(pscreen)->gpu_id < 400;
+
+ pscreen->resource_create = u_transfer_helper_resource_create;
pscreen->resource_from_handle = fd_resource_from_handle;
- pscreen->resource_get_handle = u_resource_get_handle_vtbl;
- pscreen->resource_destroy = u_resource_destroy_vtbl;
+ pscreen->resource_get_handle = fd_resource_get_handle;
+ pscreen->resource_destroy = u_transfer_helper_resource_destroy;
+
+ pscreen->transfer_helper = u_transfer_helper_create(&transfer_vtbl,
+ true, fake_rgtc, true);
}

void
fd_resource_context_init(struct pipe_context *pctx)
{
- pctx->transfer_map = u_transfer_map_vtbl;
- pctx->transfer_flush_region = u_transfer_flush_region_vtbl;
- pctx->transfer_unmap = u_transfer_unmap_vtbl;
+ pctx->transfer_map = u_transfer_helper_transfer_map;
+ pctx->transfer_flush_region = u_transfer_helper_transfer_flush_region;
+ pctx->transfer_unmap = u_transfer_helper_transfer_unmap;
pctx->buffer_subdata = u_default_buffer_subdata;
pctx->texture_subdata = u_default_texture_subdata;
pctx->create_surface = fd_create_surface;
diff --git a/src/gallium/drivers/freedreno/freedreno_resource.h b/src/gallium/drivers/freedreno/freedreno_resource.h
index 5bdb0075761..855c7fdf5e9 100644
--- a/src/gallium/drivers/freedreno/freedreno_resource.h
+++ b/src/gallium/drivers/freedreno/freedreno_resource.h
@@ -32,6 +32,7 @@
#include "util/list.h"
#include "util/u_range.h"
#include "util/u_transfer.h"
+#include "util/u_transfer_helper.h"

#include "freedreno_batch.h"
#include "freedreno_util.h"
@@ -64,20 +65,15 @@ struct fd_resource_slice {
struct set;

struct fd_resource {
- struct u_resource base;
+ struct u_transfer_resource base;
struct fd_bo *bo;
uint32_t cpp;
- enum pipe_format internal_format;
bool layer_first; /* see above description */
uint32_t layer_size;
struct fd_resource_slice slices[MAX_MIP_LEVELS];
/* buffer range that has been initialized */
struct util_range valid_buffer_range;

- /* reference to the resource holding stencil data for a z32_s8 texture */
- /* TODO rename to secondary or auxiliary? */
- struct fd_resource *stencil;
-
/* bitmask of in-flight batches which reference this resource. Note
* that the batch doesn't hold reference to resources (but instead
* the fd_ringbuffer holds refs to the underlying fd_bo), but in case
@@ -123,7 +119,8 @@ pending(struct fd_resource *rsc, bool write)
if (write && rsc->batch_mask)
return true;

- if (rsc->stencil && pending(rsc->stencil, write))
+ if (rsc->base.stencil &&
+ pending(fd_resource(rsc->base.stencil), write))
return true;

return false;
@@ -131,7 +128,6 @@ pending(struct fd_resource *rsc, bool write)

struct fd_transfer {
struct pipe_transfer base;
- void *staging;
};

static inline struct fd_transfer *
--
2.13.6
Nicolai Hähnle
2017-11-30 15:04:14 UTC
Reply
Permalink
Raw Message
Post by Rob Clark
Add a new helper that drivers can use to emulate various things that
1) z32_s8x24.. gl/gallium treats this as a single buffer with depth
and stencil interleaved but hardware frequently treats this as
separate z32 and s8 buffers. Special pack/unpack handling is
needed in transfer_map/unmap to pack/unpack the exposed buffer
2) fake RGTC.. GPUs designed with GLES in mind, but which can other-
wise do GL3, if native RGTC is not supported it can be emulated
by converting to uncompressed internally, but needs pack/unpack
in transfer_map/unmap
This could be possibly extended to handle MSAA resolve in map/unmap.
This looks mostly fine (though the MSAA thing needs to be resolved one
way or the other), except there's a clash if you ever want to use
threaded contexts, which have their own threaded_resource and
threaded_transfer.

I haven't really given any thought yet to how to reconcile those.

Cheers,
Nicolai
Post by Rob Clark
---
src/gallium/auxiliary/Makefile.sources | 2 +
src/gallium/auxiliary/meson.build | 2 +
src/gallium/auxiliary/util/u_transfer_helper.c | 364 +++++++++++++++++++++++++
src/gallium/auxiliary/util/u_transfer_helper.h | 121 ++++++++
src/gallium/include/pipe/p_screen.h | 8 +-
5 files changed, 496 insertions(+), 1 deletion(-)
create mode 100644 src/gallium/auxiliary/util/u_transfer_helper.c
create mode 100644 src/gallium/auxiliary/util/u_transfer_helper.h
diff --git a/src/gallium/auxiliary/Makefile.sources b/src/gallium/auxiliary/Makefile.sources
index f40c4723fae..a2dae04698c 100644
--- a/src/gallium/auxiliary/Makefile.sources
+++ b/src/gallium/auxiliary/Makefile.sources
@@ -304,6 +304,8 @@ C_SOURCES := \
util/u_tile.h \
util/u_transfer.c \
util/u_transfer.h \
+ util/u_transfer_helper.c \
+ util/u_transfer_helper.h \
util/u_threaded_context.c \
util/u_threaded_context.h \
util/u_threaded_context_calls.h \
diff --git a/src/gallium/auxiliary/meson.build b/src/gallium/auxiliary/meson.build
index 3e623fd099f..8c242ec1a05 100644
--- a/src/gallium/auxiliary/meson.build
+++ b/src/gallium/auxiliary/meson.build
@@ -324,6 +324,8 @@ files_libgallium = files(
'util/u_tile.h',
'util/u_transfer.c',
'util/u_transfer.h',
+ 'util/u_transfer_helper.c',
+ 'util/u_transfer_helper.h',
'util/u_threaded_context.c',
'util/u_threaded_context.h',
'util/u_threaded_context_calls.h',
diff --git a/src/gallium/auxiliary/util/u_transfer_helper.c b/src/gallium/auxiliary/util/u_transfer_helper.c
new file mode 100644
index 00000000000..fb4024db35b
--- /dev/null
+++ b/src/gallium/auxiliary/util/u_transfer_helper.c
@@ -0,0 +1,364 @@
+/*
+ * Copyright © 2017 Red Hat
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "pipe/p_screen.h"
+
+#include "util/u_box.h"
+#include "util/u_format.h"
+#include "util/u_format_rgtc.h"
+#include "util/u_format_zs.h"
+#include "util/u_inlines.h"
+#include "util/u_transfer_helper.h"
+
+
+struct u_transfer_helper {
+ const struct u_transfer_vtbl *vtbl;
+ bool separate_z32s8;
+ bool fake_rgtc;
+ bool msaa_map;
+};
+
+static inline bool handle_transfer(struct pipe_resource *prsc)
+{
+ struct u_transfer_helper *helper = prsc->screen->transfer_helper;
+ struct u_transfer_resource *rsc = u_transfer_resource(prsc);
+ return (rsc->internal_format != prsc->format) ||
+ (helper->msaa_map && (prsc->nr_samples > 1));
+}
+
+/* The pipe_transfer ptr could either be the driver's, or u_transfer,
+ * depending on whether we are intervening or not. Check handle_transfer()
+ * before dereferencing.
+ */
+struct u_transfer {
+ struct pipe_transfer base;
+ struct pipe_transfer *trans; /* driver's transfer */
+ struct pipe_transfer *trans2; /* 2nd transfer for z32s8 */
+ void *ptr, *ptr2; /* ptr to trans, and trans2 */
+ void *staging; /* staging buffer */
+};
+
+static inline struct u_transfer *
+u_transfer(struct pipe_transfer *ptrans)
+{
+ debug_assert(handle_transfer(ptrans->resource));
+ return (struct u_transfer *)ptrans;
+}
+
+struct pipe_resource *
+u_transfer_helper_resource_create(struct pipe_screen *pscreen,
+ const struct pipe_resource *templ)
+{
+ struct u_transfer_helper *helper = pscreen->transfer_helper;
+ enum pipe_format format = templ->format;
+ struct pipe_resource *prsc;
+ struct u_transfer_resource *rsc;
+
+ if ((format == PIPE_FORMAT_Z32_FLOAT_S8X24_UINT) && helper->separate_z32s8) {
+ struct pipe_resource t = *templ;
+ t.format = PIPE_FORMAT_Z32_FLOAT;
+
+ prsc = helper->vtbl->resource_create(pscreen, &t);
+ if (!prsc)
+ return NULL;
+
+ rsc = u_transfer_resource(prsc);
+
+ prsc->format = format; /* frob the format back to the "external" format */
+ rsc->internal_format = t.format;
+
+ t.format = PIPE_FORMAT_S8_UINT;
+ rsc->stencil = helper->vtbl->resource_create(pscreen, &t);
+
+ if (!rsc->stencil) {
+ helper->vtbl->resource_destroy(pscreen, prsc);
+ return NULL;
+ }
+ } else if ((util_format_description(format)->layout == UTIL_FORMAT_LAYOUT_RGTC) &&
+ helper->fake_rgtc) {
+ struct pipe_resource t = *templ;
+ t.format = PIPE_FORMAT_R8G8B8A8_UNORM;
+
+ prsc = helper->vtbl->resource_create(pscreen, &t);
+ if (!prsc)
+ return NULL;
+
+ rsc = u_transfer_resource(prsc);
+
+ prsc->format = format; /* frob the format back to the "external" format */
+ rsc->internal_format = t.format;
+ } else {
+ /* normal case, no special handling: */
+ prsc = helper->vtbl->resource_create(pscreen, templ);
+ if (!prsc)
+ return NULL;
+
+ rsc = u_transfer_resource(prsc);
+ rsc->stencil = NULL;
+ rsc->internal_format = prsc->format;
+ }
+
+ return prsc;
+}
+
+void
+u_transfer_helper_resource_destroy(struct pipe_screen *pscreen,
+ struct pipe_resource *prsc)
+{
+ struct u_transfer_helper *helper = pscreen->transfer_helper;
+ struct u_transfer_resource *rsc = u_transfer_resource(prsc);
+
+ if (rsc->stencil)
+ helper->vtbl->resource_destroy(pscreen, rsc->stencil);
+
+ helper->vtbl->resource_destroy(pscreen, prsc);
+}
+
+static bool needs_pack(unsigned usage)
+{
+ return (usage & PIPE_TRANSFER_READ) &&
+ !(usage & (PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE | PIPE_TRANSFER_DISCARD_RANGE));
+}
+
+void *
+u_transfer_helper_transfer_map(struct pipe_context *pctx,
+ struct pipe_resource *prsc,
+ unsigned level,
+ unsigned usage,
+ const struct pipe_box *box,
+ struct pipe_transfer **pptrans)
+{
+ struct u_transfer_helper *helper = pctx->screen->transfer_helper;
+ struct u_transfer_resource *rsc = u_transfer_resource(prsc);
+ struct u_transfer *trans;
+ struct pipe_transfer *ptrans;
+ enum pipe_format format = prsc->format;
+ unsigned width = box->width;
+ unsigned height = box->height;
+
+ if (!handle_transfer(prsc))
+ return helper->vtbl->transfer_map(pctx, prsc, level, usage, box, pptrans);
+
+ debug_assert(box->depth == 1);
+
+ trans = calloc(1, sizeof(*trans));
+ if (!trans)
+ return NULL;
+
+ ptrans = &trans->base;
+ pipe_resource_reference(&ptrans->resource, prsc);
+ ptrans->level = level;
+ ptrans->usage = usage;
+ ptrans->box = *box;
+ ptrans->stride = util_format_get_stride(format, box->width);
+ ptrans->layer_stride = ptrans->stride * box->height;
+
+ trans->staging = malloc(ptrans->layer_stride);
+ if (!trans->staging)
+ goto fail;
+
+ trans->ptr = helper->vtbl->transfer_map(pctx, prsc, level, usage, box,
+ &trans->trans);
+ if (!trans->ptr)
+ goto fail;
+
+ if (prsc->format == PIPE_FORMAT_Z32_FLOAT_S8X24_UINT) {
+ trans->ptr2 = helper->vtbl->transfer_map(pctx, rsc->stencil, level,
+ usage, box, &trans->trans2);
+
+ if (needs_pack(usage)) {
+ util_format_z32_float_s8x24_uint_pack_z_float(trans->staging,
+ ptrans->stride,
+ trans->ptr,
+ trans->trans->stride,
+ width, height);
+ util_format_z32_float_s8x24_uint_pack_s_8uint(trans->staging,
+ ptrans->stride,
+ trans->ptr2,
+ trans->trans2->stride,
+ width, height);
+ }
+ } else if (needs_pack(usage) &&
+ util_format_description(prsc->format)->layout == UTIL_FORMAT_LAYOUT_RGTC) {
+ switch (prsc->format) {
+ util_format_rgtc1_unorm_pack_rgba_8unorm(trans->staging,
+ ptrans->stride,
+ trans->ptr,
+ trans->trans->stride,
+ width, height);
+ break;
+ util_format_rgtc2_unorm_pack_rgba_8unorm(trans->staging,
+ ptrans->stride,
+ trans->ptr,
+ trans->trans->stride,
+ width, height);
+ break;
+ assert(!"Unexpected format");
+ break;
+ }
+ } else {
+ unreachable("bleh");
+ }
+
+ *pptrans = ptrans;
+ return trans->staging;
+
+ if (trans->trans)
+ helper->vtbl->transfer_unmap(pctx, trans->trans);
+ if (trans->trans2)
+ helper->vtbl->transfer_unmap(pctx, trans->trans2);
+ pipe_resource_reference(&ptrans->resource, NULL);
+ free(trans->staging);
+ free(trans);
+ return NULL;
+}
+
+static void
+flush_region(struct pipe_transfer *ptrans, const struct pipe_box *box)
+{
+ struct u_transfer *trans = u_transfer(ptrans);
+ enum pipe_format format = ptrans->resource->format;
+ unsigned width = ptrans->box.width;
+ unsigned height = ptrans->box.height;
+
+ if (!(ptrans->usage & PIPE_TRANSFER_WRITE))
+ return;
+
+ switch (format) {
+ util_format_z32_float_s8x24_uint_unpack_z_float(trans->ptr,
+ trans->trans->stride,
+ trans->staging,
+ ptrans->stride,
+ width, height);
+ /* fallthru */
+ util_format_z32_float_s8x24_uint_unpack_s_8uint(trans->ptr2,
+ trans->trans2->stride,
+ trans->staging,
+ ptrans->stride,
+ width, height);
+ break;
+ util_format_rgtc1_unorm_unpack_rgba_8unorm(trans->ptr,
+ trans->trans->stride,
+ trans->staging,
+ ptrans->stride,
+ width, height);
+ break;
+ util_format_rgtc2_unorm_unpack_rgba_8unorm(trans->ptr,
+ trans->trans->stride,
+ trans->staging,
+ ptrans->stride,
+ width, height);
+ break;
+ assert(!"Unexpected staging transfer type");
+ break;
+ }
+}
+
+void
+u_transfer_helper_transfer_flush_region(struct pipe_context *pctx,
+ struct pipe_transfer *ptrans,
+ const struct pipe_box *box)
+{
+ struct u_transfer_helper *helper = pctx->screen->transfer_helper;
+
+ if (handle_transfer(ptrans->resource)) {
+ struct u_transfer *trans = u_transfer(ptrans);
+
+ flush_region(ptrans, box);
+
+ helper->vtbl->transfer_flush_region(pctx, trans->trans, box);
+ if (trans->trans2)
+ helper->vtbl->transfer_flush_region(pctx, trans->trans2, box);
+
+ } else {
+ helper->vtbl->transfer_flush_region(pctx, ptrans, box);
+ }
+}
+
+void
+u_transfer_helper_transfer_unmap(struct pipe_context *pctx,
+ struct pipe_transfer *ptrans)
+{
+ struct u_transfer_helper *helper = pctx->screen->transfer_helper;
+
+ if (handle_transfer(ptrans->resource)) {
+ struct u_transfer *trans = u_transfer(ptrans);
+
+ if (!(ptrans->usage & PIPE_TRANSFER_FLUSH_EXPLICIT)) {
+ struct pipe_box box;
+ u_box_2d(0, 0, ptrans->box.width, ptrans->box.height, &box);
+ flush_region(ptrans, &box);
+ }
+
+ helper->vtbl->transfer_unmap(pctx, trans->trans);
+ if (trans->trans2)
+ helper->vtbl->transfer_unmap(pctx, trans->trans2);
+
+ free(trans);
+ } else {
+ helper->vtbl->transfer_unmap(pctx, ptrans);
+ }
+}
+
+struct u_transfer_helper *
+u_transfer_helper_create(const struct u_transfer_vtbl *vtbl,
+ bool separate_z32s8,
+ bool fake_rgtc,
+ bool msaa_map)
+{
+ struct u_transfer_helper *helper = calloc(1, sizeof(*helper));
+
+ helper->vtbl = vtbl;
+ helper->separate_z32s8 = separate_z32s8;
+ helper->fake_rgtc = fake_rgtc;
+ helper->msaa_map = msaa_map;
+
+ return helper;
+}
+
+void
+u_transfer_helper_destroy(struct u_transfer_helper *helper)
+{
+ free(helper);
+}
diff --git a/src/gallium/auxiliary/util/u_transfer_helper.h b/src/gallium/auxiliary/util/u_transfer_helper.h
new file mode 100644
index 00000000000..40848e0d8e5
--- /dev/null
+++ b/src/gallium/auxiliary/util/u_transfer_helper.h
@@ -0,0 +1,121 @@
+/*
+ * Copyright © 2017 Red Hat
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#ifndef _U_TRANSFER_HELPER_H
+#define _U_TRANSFER_HELPER_H
+
+#include "pipe/p_state.h"
+#include "pipe/p_context.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+ *
+ * - exposing separate z32 and s8 as z32x24s8
+ * - fake RGTC support for GLES class hardware which needs it to expose GL3+
+ * - MSAA resolves (TODO)
+ *
+ *
+ * 1) subclass u_transfer_resource instead of pipe_resource directly
+ * 2) populate u_transfer_vtbl and plug that into pipe_screen::transfer_helper
+ * 3) plug the the transfer helpers into pipe_screen/pipe_context
+ */
+
+struct u_transfer_resource {
+ struct pipe_resource b;
+ enum pipe_format internal_format;
+ struct pipe_resource *stencil; /* separate stencil for z32x24s8 */
+};
+
+static inline struct u_transfer_resource *
+u_transfer_resource(struct pipe_resource *prsc)
+{
+ return (struct u_transfer_resource *)prsc;
+}
+
+struct u_transfer_vtbl {
+ /* NOTE I am not expecting resource_create_from_handle() or
+ * resource_create_with_modifiers() paths to be creating any
+ * resources that need special handling. Otherwise they would
+ * need to be wrapped too.
+ */
+ struct pipe_resource * (*resource_create)(struct pipe_screen *pscreen,
+ const struct pipe_resource *templ);
+
+ void (*resource_destroy)(struct pipe_screen *pscreen,
+ struct pipe_resource *prsc);
+
+ void *(*transfer_map)(struct pipe_context *pctx,
+ struct pipe_resource *prsc,
+ unsigned level,
+ unsigned usage,
+ const struct pipe_box *box,
+ struct pipe_transfer **pptrans);
+
+
+ void (*transfer_flush_region)(struct pipe_context *pctx,
+ struct pipe_transfer *ptrans,
+ const struct pipe_box *box);
+
+ void (*transfer_unmap)(struct pipe_context *pctx,
+ struct pipe_transfer *ptrans);
+};
+
+struct pipe_resource *u_transfer_helper_resource_create(
+ struct pipe_screen *pscreen, const struct pipe_resource *templ);
+
+void u_transfer_helper_resource_destroy(struct pipe_screen *pscreen,
+ struct pipe_resource *prsc);
+
+void *u_transfer_helper_transfer_map(struct pipe_context *pctx,
+ struct pipe_resource *prsc,
+ unsigned level,
+ unsigned usage,
+ const struct pipe_box *box,
+ struct pipe_transfer **pptrans);
+
+
+void u_transfer_helper_transfer_flush_region(struct pipe_context *pctx,
+ struct pipe_transfer *ptrans,
+ const struct pipe_box *box);
+
+void u_transfer_helper_transfer_unmap(struct pipe_context *pctx,
+ struct pipe_transfer *ptrans);
+
+struct u_transfer_helper;
+
+struct u_transfer_helper * u_transfer_helper_create(const struct u_transfer_vtbl *vtbl,
+ bool separate_z32s8,
+ bool fake_rgtc,
+ bool msaa_map);
+
+void u_transfer_helper_destroy(struct u_transfer_helper *helper);
+
+#ifdef __cplusplus
+} // extern "C" {
+#endif
+
+#endif /* _U_TRANSFER_HELPER_H */
diff --git a/src/gallium/include/pipe/p_screen.h b/src/gallium/include/pipe/p_screen.h
index c249c7d63b4..101e229088b 100644
--- a/src/gallium/include/pipe/p_screen.h
+++ b/src/gallium/include/pipe/p_screen.h
@@ -60,7 +60,7 @@ struct pipe_box;
struct pipe_memory_info;
struct disk_cache;
struct driOptionCache;
-
+struct u_transfer_helper;
/**
* Gallium screen/adapter context. Basically everything
@@ -68,6 +68,12 @@ struct driOptionCache;
* context.
*/
struct pipe_screen {
+
+ /**
+ */
+ struct u_transfer_helper *transfer_helper;
+
void (*destroy)( struct pipe_screen * );
const char *(*get_name)( struct pipe_screen * );
--
Lerne, wie die Welt wirklich ist,
Aber vergiss niemals, wie sie sein sollte.
Rob Clark
2017-11-30 15:33:20 UTC
Reply
Permalink
Raw Message
Post by Rob Clark
Add a new helper that drivers can use to emulate various things that
1) z32_s8x24.. gl/gallium treats this as a single buffer with depth
and stencil interleaved but hardware frequently treats this as
separate z32 and s8 buffers. Special pack/unpack handling is
needed in transfer_map/unmap to pack/unpack the exposed buffer
2) fake RGTC.. GPUs designed with GLES in mind, but which can other-
wise do GL3, if native RGTC is not supported it can be emulated
by converting to uncompressed internally, but needs pack/unpack
in transfer_map/unmap
This could be possibly extended to handle MSAA resolve in map/unmap.
This looks mostly fine (though the MSAA thing needs to be resolved one way
I was hoping if Eric was interested in using this, that maybe he could
add in the MSAA resolve bits.. otherwise that might have to wait until
I implement non-fake MSAA.
or the other), except there's a clash if you ever want to use threaded
contexts, which have their own threaded_resource and threaded_transfer.
I haven't really given any thought yet to how to reconcile those.
hmm, ok, I see the issue.. maybe it is ok though, if the driver
installs the u_transfer_helper_* pipe fxns in the threaded_context
that wraps the real context, and the vtbl used by u_transfer_helper
has the threaded_context fxns instead of actual driver fxns. (Since
the transfer_helper is installed into the pipe_screen, it means you
couldn't mix threaded and non-threaded contexts under a single
screen.. not sure if there is ever a reason to do that?)

There are two cases with the transfer-helper:

1) the transfer is passed directly through to the driver and not
intercepted. In which case the pipe_transfer returned from
->transfer_map() is the driver's transfer_map (which doesn't subclass
u_transfer.. which I should probably move to the .c file)

2) the transfer is handled by the helper, in which case the helper
calls the driver's original ->transfer_map() via vtbl. In this case
the pipe_transfer passed back to st is u_transfer, but it has pointers
to the driver's real pipe_transfer(s). The pointers to the real
pipe_transfer's could just as well be threaded_transfer's.

In either case, the driver doesn't need to subclass u_transfer.. which
I should move to .c to make move obvious, I guess.

So possibly threaded_context_create() might need to gain a 'bool
use_transfer_helper' arg, or something like that. But doesn't seem
like a major problem.

BR,
-R
Cheers,
Nicolai
Post by Rob Clark
---
src/gallium/auxiliary/Makefile.sources | 2 +
src/gallium/auxiliary/meson.build | 2 +
src/gallium/auxiliary/util/u_transfer_helper.c | 364
+++++++++++++++++++++++++
src/gallium/auxiliary/util/u_transfer_helper.h | 121 ++++++++
src/gallium/include/pipe/p_screen.h | 8 +-
5 files changed, 496 insertions(+), 1 deletion(-)
create mode 100644 src/gallium/auxiliary/util/u_transfer_helper.c
create mode 100644 src/gallium/auxiliary/util/u_transfer_helper.h
diff --git a/src/gallium/auxiliary/Makefile.sources
b/src/gallium/auxiliary/Makefile.sources
index f40c4723fae..a2dae04698c 100644
--- a/src/gallium/auxiliary/Makefile.sources
+++ b/src/gallium/auxiliary/Makefile.sources
@@ -304,6 +304,8 @@ C_SOURCES := \
util/u_tile.h \
util/u_transfer.c \
util/u_transfer.h \
+ util/u_transfer_helper.c \
+ util/u_transfer_helper.h \
util/u_threaded_context.c \
util/u_threaded_context.h \
util/u_threaded_context_calls.h \
diff --git a/src/gallium/auxiliary/meson.build
b/src/gallium/auxiliary/meson.build
index 3e623fd099f..8c242ec1a05 100644
--- a/src/gallium/auxiliary/meson.build
+++ b/src/gallium/auxiliary/meson.build
@@ -324,6 +324,8 @@ files_libgallium = files(
'util/u_tile.h',
'util/u_transfer.c',
'util/u_transfer.h',
+ 'util/u_transfer_helper.c',
+ 'util/u_transfer_helper.h',
'util/u_threaded_context.c',
'util/u_threaded_context.h',
'util/u_threaded_context_calls.h',
diff --git a/src/gallium/auxiliary/util/u_transfer_helper.c
b/src/gallium/auxiliary/util/u_transfer_helper.c
new file mode 100644
index 00000000000..fb4024db35b
--- /dev/null
+++ b/src/gallium/auxiliary/util/u_transfer_helper.c
@@ -0,0 +1,364 @@
+/*
+ * Copyright © 2017 Red Hat
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "pipe/p_screen.h"
+
+#include "util/u_box.h"
+#include "util/u_format.h"
+#include "util/u_format_rgtc.h"
+#include "util/u_format_zs.h"
+#include "util/u_inlines.h"
+#include "util/u_transfer_helper.h"
+
+
+struct u_transfer_helper {
+ const struct u_transfer_vtbl *vtbl;
+ bool separate_z32s8;
+ bool fake_rgtc;
+ bool msaa_map;
+};
+
+static inline bool handle_transfer(struct pipe_resource *prsc)
+{
+ struct u_transfer_helper *helper = prsc->screen->transfer_helper;
+ struct u_transfer_resource *rsc = u_transfer_resource(prsc);
+ return (rsc->internal_format != prsc->format) ||
+ (helper->msaa_map && (prsc->nr_samples > 1));
+}
+
+/* The pipe_transfer ptr could either be the driver's, or u_transfer,
+ * depending on whether we are intervening or not. Check
handle_transfer()
+ * before dereferencing.
+ */
+struct u_transfer {
+ struct pipe_transfer base;
+ struct pipe_transfer *trans; /* driver's transfer */
+ struct pipe_transfer *trans2; /* 2nd transfer for z32s8 */
+ void *ptr, *ptr2; /* ptr to trans, and trans2 */
+ void *staging; /* staging buffer */
+};
+
+static inline struct u_transfer *
+u_transfer(struct pipe_transfer *ptrans)
+{
+ debug_assert(handle_transfer(ptrans->resource));
+ return (struct u_transfer *)ptrans;
+}
+
+struct pipe_resource *
+u_transfer_helper_resource_create(struct pipe_screen *pscreen,
+ const struct pipe_resource *templ)
+{
+ struct u_transfer_helper *helper = pscreen->transfer_helper;
+ enum pipe_format format = templ->format;
+ struct pipe_resource *prsc;
+ struct u_transfer_resource *rsc;
+
+ if ((format == PIPE_FORMAT_Z32_FLOAT_S8X24_UINT) &&
helper->separate_z32s8) {
+ struct pipe_resource t = *templ;
+ t.format = PIPE_FORMAT_Z32_FLOAT;
+
+ prsc = helper->vtbl->resource_create(pscreen, &t);
+ if (!prsc)
+ return NULL;
+
+ rsc = u_transfer_resource(prsc);
+
+ prsc->format = format; /* frob the format back to the "external" format */
+ rsc->internal_format = t.format;
+
+ t.format = PIPE_FORMAT_S8_UINT;
+ rsc->stencil = helper->vtbl->resource_create(pscreen, &t);
+
+ if (!rsc->stencil) {
+ helper->vtbl->resource_destroy(pscreen, prsc);
+ return NULL;
+ }
+ } else if ((util_format_description(format)->layout ==
UTIL_FORMAT_LAYOUT_RGTC) &&
+ helper->fake_rgtc) {
+ struct pipe_resource t = *templ;
+ t.format = PIPE_FORMAT_R8G8B8A8_UNORM;
+
+ prsc = helper->vtbl->resource_create(pscreen, &t);
+ if (!prsc)
+ return NULL;
+
+ rsc = u_transfer_resource(prsc);
+
+ prsc->format = format; /* frob the format back to the "external" format */
+ rsc->internal_format = t.format;
+ } else {
+ /* normal case, no special handling: */
+ prsc = helper->vtbl->resource_create(pscreen, templ);
+ if (!prsc)
+ return NULL;
+
+ rsc = u_transfer_resource(prsc);
+ rsc->stencil = NULL;
+ rsc->internal_format = prsc->format;
+ }
+
+ return prsc;
+}
+
+void
+u_transfer_helper_resource_destroy(struct pipe_screen *pscreen,
+ struct pipe_resource *prsc)
+{
+ struct u_transfer_helper *helper = pscreen->transfer_helper;
+ struct u_transfer_resource *rsc = u_transfer_resource(prsc);
+
+ if (rsc->stencil)
+ helper->vtbl->resource_destroy(pscreen, rsc->stencil);
+
+ helper->vtbl->resource_destroy(pscreen, prsc);
+}
+
+static bool needs_pack(unsigned usage)
+{
+ return (usage & PIPE_TRANSFER_READ) &&
+ !(usage & (PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE |
PIPE_TRANSFER_DISCARD_RANGE));
+}
+
+void *
+u_transfer_helper_transfer_map(struct pipe_context *pctx,
+ struct pipe_resource *prsc,
+ unsigned level,
+ unsigned usage,
+ const struct pipe_box *box,
+ struct pipe_transfer **pptrans)
+{
+ struct u_transfer_helper *helper = pctx->screen->transfer_helper;
+ struct u_transfer_resource *rsc = u_transfer_resource(prsc);
+ struct u_transfer *trans;
+ struct pipe_transfer *ptrans;
+ enum pipe_format format = prsc->format;
+ unsigned width = box->width;
+ unsigned height = box->height;
+
+ if (!handle_transfer(prsc))
+ return helper->vtbl->transfer_map(pctx, prsc, level, usage, box, pptrans);
+
+ debug_assert(box->depth == 1);
+
+ trans = calloc(1, sizeof(*trans));
+ if (!trans)
+ return NULL;
+
+ ptrans = &trans->base;
+ pipe_resource_reference(&ptrans->resource, prsc);
+ ptrans->level = level;
+ ptrans->usage = usage;
+ ptrans->box = *box;
+ ptrans->stride = util_format_get_stride(format, box->width);
+ ptrans->layer_stride = ptrans->stride * box->height;
+
+ trans->staging = malloc(ptrans->layer_stride);
+ if (!trans->staging)
+ goto fail;
+
+ trans->ptr = helper->vtbl->transfer_map(pctx, prsc, level, usage, box,
+ &trans->trans);
+ if (!trans->ptr)
+ goto fail;
+
+ if (prsc->format == PIPE_FORMAT_Z32_FLOAT_S8X24_UINT) {
+ trans->ptr2 = helper->vtbl->transfer_map(pctx, rsc->stencil, level,
+ usage, box,
&trans->trans2);
+
+ if (needs_pack(usage)) {
+ util_format_z32_float_s8x24_uint_pack_z_float(trans->staging,
+ ptrans->stride,
+ trans->ptr,
+
trans->trans->stride,
+ width, height);
+ util_format_z32_float_s8x24_uint_pack_s_8uint(trans->staging,
+ ptrans->stride,
+ trans->ptr2,
+
trans->trans2->stride,
+ width, height);
+ }
+ } else if (needs_pack(usage) &&
+ util_format_description(prsc->format)->layout ==
UTIL_FORMAT_LAYOUT_RGTC) {
+ switch (prsc->format) {
+ util_format_rgtc1_unorm_pack_rgba_8unorm(trans->staging,
+ ptrans->stride,
+ trans->ptr,
+ trans->trans->stride,
+ width, height);
+ break;
+ util_format_rgtc2_unorm_pack_rgba_8unorm(trans->staging,
+ ptrans->stride,
+ trans->ptr,
+ trans->trans->stride,
+ width, height);
+ break;
+ assert(!"Unexpected format");
+ break;
+ }
+ } else {
+ unreachable("bleh");
+ }
+
+ *pptrans = ptrans;
+ return trans->staging;
+
+ if (trans->trans)
+ helper->vtbl->transfer_unmap(pctx, trans->trans);
+ if (trans->trans2)
+ helper->vtbl->transfer_unmap(pctx, trans->trans2);
+ pipe_resource_reference(&ptrans->resource, NULL);
+ free(trans->staging);
+ free(trans);
+ return NULL;
+}
+
+static void
+flush_region(struct pipe_transfer *ptrans, const struct pipe_box *box)
+{
+ struct u_transfer *trans = u_transfer(ptrans);
+ enum pipe_format format = ptrans->resource->format;
+ unsigned width = ptrans->box.width;
+ unsigned height = ptrans->box.height;
+
+ if (!(ptrans->usage & PIPE_TRANSFER_WRITE))
+ return;
+
+ switch (format) {
+ util_format_z32_float_s8x24_uint_unpack_z_float(trans->ptr,
+
trans->trans->stride,
+ trans->staging,
+ ptrans->stride,
+ width, height);
+ /* fallthru */
+ util_format_z32_float_s8x24_uint_unpack_s_8uint(trans->ptr2,
+
trans->trans2->stride,
+ trans->staging,
+ ptrans->stride,
+ width, height);
+ break;
+ util_format_rgtc1_unorm_unpack_rgba_8unorm(trans->ptr,
+ trans->trans->stride,
+ trans->staging,
+ ptrans->stride,
+ width, height);
+ break;
+ util_format_rgtc2_unorm_unpack_rgba_8unorm(trans->ptr,
+ trans->trans->stride,
+ trans->staging,
+ ptrans->stride,
+ width, height);
+ break;
+ assert(!"Unexpected staging transfer type");
+ break;
+ }
+}
+
+void
+u_transfer_helper_transfer_flush_region(struct pipe_context *pctx,
+ struct pipe_transfer *ptrans,
+ const struct pipe_box *box)
+{
+ struct u_transfer_helper *helper = pctx->screen->transfer_helper;
+
+ if (handle_transfer(ptrans->resource)) {
+ struct u_transfer *trans = u_transfer(ptrans);
+
+ flush_region(ptrans, box);
+
+ helper->vtbl->transfer_flush_region(pctx, trans->trans, box);
+ if (trans->trans2)
+ helper->vtbl->transfer_flush_region(pctx, trans->trans2, box);
+
+ } else {
+ helper->vtbl->transfer_flush_region(pctx, ptrans, box);
+ }
+}
+
+void
+u_transfer_helper_transfer_unmap(struct pipe_context *pctx,
+ struct pipe_transfer *ptrans)
+{
+ struct u_transfer_helper *helper = pctx->screen->transfer_helper;
+
+ if (handle_transfer(ptrans->resource)) {
+ struct u_transfer *trans = u_transfer(ptrans);
+
+ if (!(ptrans->usage & PIPE_TRANSFER_FLUSH_EXPLICIT)) {
+ struct pipe_box box;
+ u_box_2d(0, 0, ptrans->box.width, ptrans->box.height, &box);
+ flush_region(ptrans, &box);
+ }
+
+ helper->vtbl->transfer_unmap(pctx, trans->trans);
+ if (trans->trans2)
+ helper->vtbl->transfer_unmap(pctx, trans->trans2);
+
+ free(trans);
+ } else {
+ helper->vtbl->transfer_unmap(pctx, ptrans);
+ }
+}
+
+struct u_transfer_helper *
+u_transfer_helper_create(const struct u_transfer_vtbl *vtbl,
+ bool separate_z32s8,
+ bool fake_rgtc,
+ bool msaa_map)
+{
+ struct u_transfer_helper *helper = calloc(1, sizeof(*helper));
+
+ helper->vtbl = vtbl;
+ helper->separate_z32s8 = separate_z32s8;
+ helper->fake_rgtc = fake_rgtc;
+ helper->msaa_map = msaa_map;
+
+ return helper;
+}
+
+void
+u_transfer_helper_destroy(struct u_transfer_helper *helper)
+{
+ free(helper);
+}
diff --git a/src/gallium/auxiliary/util/u_transfer_helper.h
b/src/gallium/auxiliary/util/u_transfer_helper.h
new file mode 100644
index 00000000000..40848e0d8e5
--- /dev/null
+++ b/src/gallium/auxiliary/util/u_transfer_helper.h
@@ -0,0 +1,121 @@
+/*
+ * Copyright © 2017 Red Hat
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#ifndef _U_TRANSFER_HELPER_H
+#define _U_TRANSFER_HELPER_H
+
+#include "pipe/p_state.h"
+#include "pipe/p_context.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+ *
+ * - exposing separate z32 and s8 as z32x24s8
+ * - fake RGTC support for GLES class hardware which needs it to expose GL3+
+ * - MSAA resolves (TODO)
+ *
+ *
+ * 1) subclass u_transfer_resource instead of pipe_resource directly
+ * 2) populate u_transfer_vtbl and plug that into
pipe_screen::transfer_helper
+ * 3) plug the the transfer helpers into pipe_screen/pipe_context
+ */
+
+struct u_transfer_resource {
+ struct pipe_resource b;
+ enum pipe_format internal_format;
+ struct pipe_resource *stencil; /* separate stencil for z32x24s8 */
+};
+
+static inline struct u_transfer_resource *
+u_transfer_resource(struct pipe_resource *prsc)
+{
+ return (struct u_transfer_resource *)prsc;
+}
+
+struct u_transfer_vtbl {
+ /* NOTE I am not expecting resource_create_from_handle() or
+ * resource_create_with_modifiers() paths to be creating any
+ * resources that need special handling. Otherwise they would
+ * need to be wrapped too.
+ */
+ struct pipe_resource * (*resource_create)(struct pipe_screen *pscreen,
+ const struct pipe_resource *templ);
+
+ void (*resource_destroy)(struct pipe_screen *pscreen,
+ struct pipe_resource *prsc);
+
+ void *(*transfer_map)(struct pipe_context *pctx,
+ struct pipe_resource *prsc,
+ unsigned level,
+ unsigned usage,
+ const struct pipe_box *box,
+ struct pipe_transfer **pptrans);
+
+
+ void (*transfer_flush_region)(struct pipe_context *pctx,
+ struct pipe_transfer *ptrans,
+ const struct pipe_box *box);
+
+ void (*transfer_unmap)(struct pipe_context *pctx,
+ struct pipe_transfer *ptrans);
+};
+
+struct pipe_resource *u_transfer_helper_resource_create(
+ struct pipe_screen *pscreen, const struct pipe_resource *templ);
+
+void u_transfer_helper_resource_destroy(struct pipe_screen *pscreen,
+ struct pipe_resource *prsc);
+
+void *u_transfer_helper_transfer_map(struct pipe_context *pctx,
+ struct pipe_resource *prsc,
+ unsigned level,
+ unsigned usage,
+ const struct pipe_box *box,
+ struct pipe_transfer **pptrans);
+
+
+void u_transfer_helper_transfer_flush_region(struct pipe_context *pctx,
+ struct pipe_transfer *ptrans,
+ const struct pipe_box *box);
+
+void u_transfer_helper_transfer_unmap(struct pipe_context *pctx,
+ struct pipe_transfer *ptrans);
+
+struct u_transfer_helper;
+
+struct u_transfer_helper * u_transfer_helper_create(const struct u_transfer_vtbl *vtbl,
+ bool separate_z32s8,
+ bool fake_rgtc,
+ bool msaa_map);
+
+void u_transfer_helper_destroy(struct u_transfer_helper *helper);
+
+#ifdef __cplusplus
+} // extern "C" {
+#endif
+
+#endif /* _U_TRANSFER_HELPER_H */
diff --git a/src/gallium/include/pipe/p_screen.h
b/src/gallium/include/pipe/p_screen.h
index c249c7d63b4..101e229088b 100644
--- a/src/gallium/include/pipe/p_screen.h
+++ b/src/gallium/include/pipe/p_screen.h
@@ -60,7 +60,7 @@ struct pipe_box;
struct pipe_memory_info;
struct disk_cache;
struct driOptionCache;
-
+struct u_transfer_helper;
/**
* Gallium screen/adapter context. Basically everything
@@ -68,6 +68,12 @@ struct driOptionCache;
* context.
*/
struct pipe_screen {
+
+ /**
+ */
+ struct u_transfer_helper *transfer_helper;
+
void (*destroy)( struct pipe_screen * );
const char *(*get_name)( struct pipe_screen * );
--
Lerne, wie die Welt wirklich ist,
Aber vergiss niemals, wie sie sein sollte.
Rob Clark
2017-12-01 13:43:04 UTC
Reply
Permalink
Raw Message
Post by Rob Clark
Post by Rob Clark
Add a new helper that drivers can use to emulate various things that
1) z32_s8x24.. gl/gallium treats this as a single buffer with depth
and stencil interleaved but hardware frequently treats this as
separate z32 and s8 buffers. Special pack/unpack handling is
needed in transfer_map/unmap to pack/unpack the exposed buffer
2) fake RGTC.. GPUs designed with GLES in mind, but which can other-
wise do GL3, if native RGTC is not supported it can be emulated
by converting to uncompressed internally, but needs pack/unpack
in transfer_map/unmap
This could be possibly extended to handle MSAA resolve in map/unmap.
This looks mostly fine (though the MSAA thing needs to be resolved one way
I was hoping if Eric was interested in using this, that maybe he could
add in the MSAA resolve bits.. otherwise that might have to wait until
I implement non-fake MSAA.
or the other), except there's a clash if you ever want to use threaded
contexts, which have their own threaded_resource and threaded_transfer.
I haven't really given any thought yet to how to reconcile those.
hmm, ok, I see the issue.. maybe it is ok though, if the driver
installs the u_transfer_helper_* pipe fxns in the threaded_context
that wraps the real context, and the vtbl used by u_transfer_helper
has the threaded_context fxns instead of actual driver fxns. (Since
the transfer_helper is installed into the pipe_screen, it means you
couldn't mix threaded and non-threaded contexts under a single
screen.. not sure if there is ever a reason to do that?)
hmm, so the issue actually is there but not w/ threaded_transfer vs
u_transfer.. instead with threaded_resource vs u_transfer_resource.
:-/

u_transfer_helper really only needs to track the additional stencil
buffer and internal_format (although maybe adding MSAA resolves could
add more to that).. possibly I could track that a different way
(setter/getter fxns in vtbl?)

BR,
-R
Post by Rob Clark
1) the transfer is passed directly through to the driver and not
intercepted. In which case the pipe_transfer returned from
->transfer_map() is the driver's transfer_map (which doesn't subclass
u_transfer.. which I should probably move to the .c file)
2) the transfer is handled by the helper, in which case the helper
calls the driver's original ->transfer_map() via vtbl. In this case
the pipe_transfer passed back to st is u_transfer, but it has pointers
to the driver's real pipe_transfer(s). The pointers to the real
pipe_transfer's could just as well be threaded_transfer's.
In either case, the driver doesn't need to subclass u_transfer.. which
I should move to .c to make move obvious, I guess.
So possibly threaded_context_create() might need to gain a 'bool
use_transfer_helper' arg, or something like that. But doesn't seem
like a major problem.
BR,
-R
Cheers,
Nicolai
Post by Rob Clark
---
src/gallium/auxiliary/Makefile.sources | 2 +
src/gallium/auxiliary/meson.build | 2 +
src/gallium/auxiliary/util/u_transfer_helper.c | 364
+++++++++++++++++++++++++
src/gallium/auxiliary/util/u_transfer_helper.h | 121 ++++++++
src/gallium/include/pipe/p_screen.h | 8 +-
5 files changed, 496 insertions(+), 1 deletion(-)
create mode 100644 src/gallium/auxiliary/util/u_transfer_helper.c
create mode 100644 src/gallium/auxiliary/util/u_transfer_helper.h
diff --git a/src/gallium/auxiliary/Makefile.sources
b/src/gallium/auxiliary/Makefile.sources
index f40c4723fae..a2dae04698c 100644
--- a/src/gallium/auxiliary/Makefile.sources
+++ b/src/gallium/auxiliary/Makefile.sources
@@ -304,6 +304,8 @@ C_SOURCES := \
util/u_tile.h \
util/u_transfer.c \
util/u_transfer.h \
+ util/u_transfer_helper.c \
+ util/u_transfer_helper.h \
util/u_threaded_context.c \
util/u_threaded_context.h \
util/u_threaded_context_calls.h \
diff --git a/src/gallium/auxiliary/meson.build
b/src/gallium/auxiliary/meson.build
index 3e623fd099f..8c242ec1a05 100644
--- a/src/gallium/auxiliary/meson.build
+++ b/src/gallium/auxiliary/meson.build
@@ -324,6 +324,8 @@ files_libgallium = files(
'util/u_tile.h',
'util/u_transfer.c',
'util/u_transfer.h',
+ 'util/u_transfer_helper.c',
+ 'util/u_transfer_helper.h',
'util/u_threaded_context.c',
'util/u_threaded_context.h',
'util/u_threaded_context_calls.h',
diff --git a/src/gallium/auxiliary/util/u_transfer_helper.c
b/src/gallium/auxiliary/util/u_transfer_helper.c
new file mode 100644
index 00000000000..fb4024db35b
--- /dev/null
+++ b/src/gallium/auxiliary/util/u_transfer_helper.c
@@ -0,0 +1,364 @@
+/*
+ * Copyright © 2017 Red Hat
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "pipe/p_screen.h"
+
+#include "util/u_box.h"
+#include "util/u_format.h"
+#include "util/u_format_rgtc.h"
+#include "util/u_format_zs.h"
+#include "util/u_inlines.h"
+#include "util/u_transfer_helper.h"
+
+
+struct u_transfer_helper {
+ const struct u_transfer_vtbl *vtbl;
+ bool separate_z32s8;
+ bool fake_rgtc;
+ bool msaa_map;
+};
+
+static inline bool handle_transfer(struct pipe_resource *prsc)
+{
+ struct u_transfer_helper *helper = prsc->screen->transfer_helper;
+ struct u_transfer_resource *rsc = u_transfer_resource(prsc);
+ return (rsc->internal_format != prsc->format) ||
+ (helper->msaa_map && (prsc->nr_samples > 1));
+}
+
+/* The pipe_transfer ptr could either be the driver's, or u_transfer,
+ * depending on whether we are intervening or not. Check
handle_transfer()
+ * before dereferencing.
+ */
+struct u_transfer {
+ struct pipe_transfer base;
+ struct pipe_transfer *trans; /* driver's transfer */
+ struct pipe_transfer *trans2; /* 2nd transfer for z32s8 */
+ void *ptr, *ptr2; /* ptr to trans, and trans2 */
+ void *staging; /* staging buffer */
+};
+
+static inline struct u_transfer *
+u_transfer(struct pipe_transfer *ptrans)
+{
+ debug_assert(handle_transfer(ptrans->resource));
+ return (struct u_transfer *)ptrans;
+}
+
+struct pipe_resource *
+u_transfer_helper_resource_create(struct pipe_screen *pscreen,
+ const struct pipe_resource *templ)
+{
+ struct u_transfer_helper *helper = pscreen->transfer_helper;
+ enum pipe_format format = templ->format;
+ struct pipe_resource *prsc;
+ struct u_transfer_resource *rsc;
+
+ if ((format == PIPE_FORMAT_Z32_FLOAT_S8X24_UINT) &&
helper->separate_z32s8) {
+ struct pipe_resource t = *templ;
+ t.format = PIPE_FORMAT_Z32_FLOAT;
+
+ prsc = helper->vtbl->resource_create(pscreen, &t);
+ if (!prsc)
+ return NULL;
+
+ rsc = u_transfer_resource(prsc);
+
+ prsc->format = format; /* frob the format back to the "external" format */
+ rsc->internal_format = t.format;
+
+ t.format = PIPE_FORMAT_S8_UINT;
+ rsc->stencil = helper->vtbl->resource_create(pscreen, &t);
+
+ if (!rsc->stencil) {
+ helper->vtbl->resource_destroy(pscreen, prsc);
+ return NULL;
+ }
+ } else if ((util_format_description(format)->layout ==
UTIL_FORMAT_LAYOUT_RGTC) &&
+ helper->fake_rgtc) {
+ struct pipe_resource t = *templ;
+ t.format = PIPE_FORMAT_R8G8B8A8_UNORM;
+
+ prsc = helper->vtbl->resource_create(pscreen, &t);
+ if (!prsc)
+ return NULL;
+
+ rsc = u_transfer_resource(prsc);
+
+ prsc->format = format; /* frob the format back to the "external" format */
+ rsc->internal_format = t.format;
+ } else {
+ /* normal case, no special handling: */
+ prsc = helper->vtbl->resource_create(pscreen, templ);
+ if (!prsc)
+ return NULL;
+
+ rsc = u_transfer_resource(prsc);
+ rsc->stencil = NULL;
+ rsc->internal_format = prsc->format;
+ }
+
+ return prsc;
+}
+
+void
+u_transfer_helper_resource_destroy(struct pipe_screen *pscreen,
+ struct pipe_resource *prsc)
+{
+ struct u_transfer_helper *helper = pscreen->transfer_helper;
+ struct u_transfer_resource *rsc = u_transfer_resource(prsc);
+
+ if (rsc->stencil)
+ helper->vtbl->resource_destroy(pscreen, rsc->stencil);
+
+ helper->vtbl->resource_destroy(pscreen, prsc);
+}
+
+static bool needs_pack(unsigned usage)
+{
+ return (usage & PIPE_TRANSFER_READ) &&
+ !(usage & (PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE |
PIPE_TRANSFER_DISCARD_RANGE));
+}
+
+void *
+u_transfer_helper_transfer_map(struct pipe_context *pctx,
+ struct pipe_resource *prsc,
+ unsigned level,
+ unsigned usage,
+ const struct pipe_box *box,
+ struct pipe_transfer **pptrans)
+{
+ struct u_transfer_helper *helper = pctx->screen->transfer_helper;
+ struct u_transfer_resource *rsc = u_transfer_resource(prsc);
+ struct u_transfer *trans;
+ struct pipe_transfer *ptrans;
+ enum pipe_format format = prsc->format;
+ unsigned width = box->width;
+ unsigned height = box->height;
+
+ if (!handle_transfer(prsc))
+ return helper->vtbl->transfer_map(pctx, prsc, level, usage, box, pptrans);
+
+ debug_assert(box->depth == 1);
+
+ trans = calloc(1, sizeof(*trans));
+ if (!trans)
+ return NULL;
+
+ ptrans = &trans->base;
+ pipe_resource_reference(&ptrans->resource, prsc);
+ ptrans->level = level;
+ ptrans->usage = usage;
+ ptrans->box = *box;
+ ptrans->stride = util_format_get_stride(format, box->width);
+ ptrans->layer_stride = ptrans->stride * box->height;
+
+ trans->staging = malloc(ptrans->layer_stride);
+ if (!trans->staging)
+ goto fail;
+
+ trans->ptr = helper->vtbl->transfer_map(pctx, prsc, level, usage, box,
+ &trans->trans);
+ if (!trans->ptr)
+ goto fail;
+
+ if (prsc->format == PIPE_FORMAT_Z32_FLOAT_S8X24_UINT) {
+ trans->ptr2 = helper->vtbl->transfer_map(pctx, rsc->stencil, level,
+ usage, box,
&trans->trans2);
+
+ if (needs_pack(usage)) {
+ util_format_z32_float_s8x24_uint_pack_z_float(trans->staging,
+ ptrans->stride,
+ trans->ptr,
+
trans->trans->stride,
+ width, height);
+ util_format_z32_float_s8x24_uint_pack_s_8uint(trans->staging,
+ ptrans->stride,
+ trans->ptr2,
+
trans->trans2->stride,
+ width, height);
+ }
+ } else if (needs_pack(usage) &&
+ util_format_description(prsc->format)->layout ==
UTIL_FORMAT_LAYOUT_RGTC) {
+ switch (prsc->format) {
+ util_format_rgtc1_unorm_pack_rgba_8unorm(trans->staging,
+ ptrans->stride,
+ trans->ptr,
+ trans->trans->stride,
+ width, height);
+ break;
+ util_format_rgtc2_unorm_pack_rgba_8unorm(trans->staging,
+ ptrans->stride,
+ trans->ptr,
+ trans->trans->stride,
+ width, height);
+ break;
+ assert(!"Unexpected format");
+ break;
+ }
+ } else {
+ unreachable("bleh");
+ }
+
+ *pptrans = ptrans;
+ return trans->staging;
+
+ if (trans->trans)
+ helper->vtbl->transfer_unmap(pctx, trans->trans);
+ if (trans->trans2)
+ helper->vtbl->transfer_unmap(pctx, trans->trans2);
+ pipe_resource_reference(&ptrans->resource, NULL);
+ free(trans->staging);
+ free(trans);
+ return NULL;
+}
+
+static void
+flush_region(struct pipe_transfer *ptrans, const struct pipe_box *box)
+{
+ struct u_transfer *trans = u_transfer(ptrans);
+ enum pipe_format format = ptrans->resource->format;
+ unsigned width = ptrans->box.width;
+ unsigned height = ptrans->box.height;
+
+ if (!(ptrans->usage & PIPE_TRANSFER_WRITE))
+ return;
+
+ switch (format) {
+ util_format_z32_float_s8x24_uint_unpack_z_float(trans->ptr,
+
trans->trans->stride,
+ trans->staging,
+ ptrans->stride,
+ width, height);
+ /* fallthru */
+ util_format_z32_float_s8x24_uint_unpack_s_8uint(trans->ptr2,
+
trans->trans2->stride,
+ trans->staging,
+ ptrans->stride,
+ width, height);
+ break;
+ util_format_rgtc1_unorm_unpack_rgba_8unorm(trans->ptr,
+ trans->trans->stride,
+ trans->staging,
+ ptrans->stride,
+ width, height);
+ break;
+ util_format_rgtc2_unorm_unpack_rgba_8unorm(trans->ptr,
+ trans->trans->stride,
+ trans->staging,
+ ptrans->stride,
+ width, height);
+ break;
+ assert(!"Unexpected staging transfer type");
+ break;
+ }
+}
+
+void
+u_transfer_helper_transfer_flush_region(struct pipe_context *pctx,
+ struct pipe_transfer *ptrans,
+ const struct pipe_box *box)
+{
+ struct u_transfer_helper *helper = pctx->screen->transfer_helper;
+
+ if (handle_transfer(ptrans->resource)) {
+ struct u_transfer *trans = u_transfer(ptrans);
+
+ flush_region(ptrans, box);
+
+ helper->vtbl->transfer_flush_region(pctx, trans->trans, box);
+ if (trans->trans2)
+ helper->vtbl->transfer_flush_region(pctx, trans->trans2, box);
+
+ } else {
+ helper->vtbl->transfer_flush_region(pctx, ptrans, box);
+ }
+}
+
+void
+u_transfer_helper_transfer_unmap(struct pipe_context *pctx,
+ struct pipe_transfer *ptrans)
+{
+ struct u_transfer_helper *helper = pctx->screen->transfer_helper;
+
+ if (handle_transfer(ptrans->resource)) {
+ struct u_transfer *trans = u_transfer(ptrans);
+
+ if (!(ptrans->usage & PIPE_TRANSFER_FLUSH_EXPLICIT)) {
+ struct pipe_box box;
+ u_box_2d(0, 0, ptrans->box.width, ptrans->box.height, &box);
+ flush_region(ptrans, &box);
+ }
+
+ helper->vtbl->transfer_unmap(pctx, trans->trans);
+ if (trans->trans2)
+ helper->vtbl->transfer_unmap(pctx, trans->trans2);
+
+ free(trans);
+ } else {
+ helper->vtbl->transfer_unmap(pctx, ptrans);
+ }
+}
+
+struct u_transfer_helper *
+u_transfer_helper_create(const struct u_transfer_vtbl *vtbl,
+ bool separate_z32s8,
+ bool fake_rgtc,
+ bool msaa_map)
+{
+ struct u_transfer_helper *helper = calloc(1, sizeof(*helper));
+
+ helper->vtbl = vtbl;
+ helper->separate_z32s8 = separate_z32s8;
+ helper->fake_rgtc = fake_rgtc;
+ helper->msaa_map = msaa_map;
+
+ return helper;
+}
+
+void
+u_transfer_helper_destroy(struct u_transfer_helper *helper)
+{
+ free(helper);
+}
diff --git a/src/gallium/auxiliary/util/u_transfer_helper.h
b/src/gallium/auxiliary/util/u_transfer_helper.h
new file mode 100644
index 00000000000..40848e0d8e5
--- /dev/null
+++ b/src/gallium/auxiliary/util/u_transfer_helper.h
@@ -0,0 +1,121 @@
+/*
+ * Copyright © 2017 Red Hat
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#ifndef _U_TRANSFER_HELPER_H
+#define _U_TRANSFER_HELPER_H
+
+#include "pipe/p_state.h"
+#include "pipe/p_context.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+ *
+ * - exposing separate z32 and s8 as z32x24s8
+ * - fake RGTC support for GLES class hardware which needs it to expose GL3+
+ * - MSAA resolves (TODO)
+ *
+ *
+ * 1) subclass u_transfer_resource instead of pipe_resource directly
+ * 2) populate u_transfer_vtbl and plug that into
pipe_screen::transfer_helper
+ * 3) plug the the transfer helpers into pipe_screen/pipe_context
+ */
+
+struct u_transfer_resource {
+ struct pipe_resource b;
+ enum pipe_format internal_format;
+ struct pipe_resource *stencil; /* separate stencil for z32x24s8 */
+};
+
+static inline struct u_transfer_resource *
+u_transfer_resource(struct pipe_resource *prsc)
+{
+ return (struct u_transfer_resource *)prsc;
+}
+
+struct u_transfer_vtbl {
+ /* NOTE I am not expecting resource_create_from_handle() or
+ * resource_create_with_modifiers() paths to be creating any
+ * resources that need special handling. Otherwise they would
+ * need to be wrapped too.
+ */
+ struct pipe_resource * (*resource_create)(struct pipe_screen *pscreen,
+ const struct pipe_resource *templ);
+
+ void (*resource_destroy)(struct pipe_screen *pscreen,
+ struct pipe_resource *prsc);
+
+ void *(*transfer_map)(struct pipe_context *pctx,
+ struct pipe_resource *prsc,
+ unsigned level,
+ unsigned usage,
+ const struct pipe_box *box,
+ struct pipe_transfer **pptrans);
+
+
+ void (*transfer_flush_region)(struct pipe_context *pctx,
+ struct pipe_transfer *ptrans,
+ const struct pipe_box *box);
+
+ void (*transfer_unmap)(struct pipe_context *pctx,
+ struct pipe_transfer *ptrans);
+};
+
+struct pipe_resource *u_transfer_helper_resource_create(
+ struct pipe_screen *pscreen, const struct pipe_resource *templ);
+
+void u_transfer_helper_resource_destroy(struct pipe_screen *pscreen,
+ struct pipe_resource *prsc);
+
+void *u_transfer_helper_transfer_map(struct pipe_context *pctx,
+ struct pipe_resource *prsc,
+ unsigned level,
+ unsigned usage,
+ const struct pipe_box *box,
+ struct pipe_transfer **pptrans);
+
+
+void u_transfer_helper_transfer_flush_region(struct pipe_context *pctx,
+ struct pipe_transfer *ptrans,
+ const struct pipe_box *box);
+
+void u_transfer_helper_transfer_unmap(struct pipe_context *pctx,
+ struct pipe_transfer *ptrans);
+
+struct u_transfer_helper;
+
+struct u_transfer_helper * u_transfer_helper_create(const struct
u_transfer_vtbl *vtbl,
+ bool separate_z32s8,
+ bool fake_rgtc,
+ bool msaa_map);
+
+void u_transfer_helper_destroy(struct u_transfer_helper *helper);
+
+#ifdef __cplusplus
+} // extern "C" {
+#endif
+
+#endif /* _U_TRANSFER_HELPER_H */
diff --git a/src/gallium/include/pipe/p_screen.h
b/src/gallium/include/pipe/p_screen.h
index c249c7d63b4..101e229088b 100644
--- a/src/gallium/include/pipe/p_screen.h
+++ b/src/gallium/include/pipe/p_screen.h
@@ -60,7 +60,7 @@ struct pipe_box;
struct pipe_memory_info;
struct disk_cache;
struct driOptionCache;
-
+struct u_transfer_helper;
/**
* Gallium screen/adapter context. Basically everything
@@ -68,6 +68,12 @@ struct driOptionCache;
* context.
*/
struct pipe_screen {
+
+ /**
+ */
+ struct u_transfer_helper *transfer_helper;
+
void (*destroy)( struct pipe_screen * );
const char *(*get_name)( struct pipe_screen * );
--
Lerne, wie die Welt wirklich ist,
Aber vergiss niemals, wie sie sein sollte.
Rob Clark
2017-12-04 17:05:21 UTC
Reply
Permalink
Raw Message
Add a new helper that drivers can use to emulate various things that
need special handling in particular in transfer_map:

1) z32_s8x24.. gl/gallium treats this as a single buffer with depth
and stencil interleaved but hardware frequently treats this as
separate z32 and s8 buffers. Special pack/unpack handling is
needed in transfer_map/unmap to pack/unpack the exposed buffer

2) fake RGTC.. GPUs designed with GLES in mind, but which can other-
wise do GL3, if native RGTC is not supported it can be emulated
by converting to uncompressed internally, but needs pack/unpack
in transfer_map/unmap

3) MSAA resolves in the transfer_map() case

v2: add MSAA resolve based on Eric's "gallium: Add helpers for MSAA
resolves in pipe_transfer_map()/unmap()." patch; avoid wrapping
pipe_resource, to make it possible for drivers to use both this
and threaded_context.

Signed-off-by: Rob Clark <***@gmail.com>
---
src/gallium/auxiliary/Makefile.sources | 2 +
src/gallium/auxiliary/meson.build | 2 +
src/gallium/auxiliary/util/u_transfer_helper.c | 486 +++++++++++++++++++++++++
src/gallium/auxiliary/util/u_transfer_helper.h | 132 +++++++
src/gallium/include/pipe/p_screen.h | 8 +-
5 files changed, 629 insertions(+), 1 deletion(-)
create mode 100644 src/gallium/auxiliary/util/u_transfer_helper.c
create mode 100644 src/gallium/auxiliary/util/u_transfer_helper.h

diff --git a/src/gallium/auxiliary/Makefile.sources b/src/gallium/auxiliary/Makefile.sources
index f40c4723fae..a2dae04698c 100644
--- a/src/gallium/auxiliary/Makefile.sources
+++ b/src/gallium/auxiliary/Makefile.sources
@@ -304,6 +304,8 @@ C_SOURCES := \
util/u_tile.h \
util/u_transfer.c \
util/u_transfer.h \
+ util/u_transfer_helper.c \
+ util/u_transfer_helper.h \
util/u_threaded_context.c \
util/u_threaded_context.h \
util/u_threaded_context_calls.h \
diff --git a/src/gallium/auxiliary/meson.build b/src/gallium/auxiliary/meson.build
index 3e623fd099f..8c242ec1a05 100644
--- a/src/gallium/auxiliary/meson.build
+++ b/src/gallium/auxiliary/meson.build
@@ -324,6 +324,8 @@ files_libgallium = files(
'util/u_tile.h',
'util/u_transfer.c',
'util/u_transfer.h',
+ 'util/u_transfer_helper.c',
+ 'util/u_transfer_helper.h',
'util/u_threaded_context.c',
'util/u_threaded_context.h',
'util/u_threaded_context_calls.h',
diff --git a/src/gallium/auxiliary/util/u_transfer_helper.c b/src/gallium/auxiliary/util/u_transfer_helper.c
new file mode 100644
index 00000000000..6f7a36a5f6d
--- /dev/null
+++ b/src/gallium/auxiliary/util/u_transfer_helper.c
@@ -0,0 +1,486 @@
+/*
+ * Copyright © 2017 Red Hat
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "pipe/p_screen.h"
+
+#include "util/u_box.h"
+#include "util/u_format.h"
+#include "util/u_format_rgtc.h"
+#include "util/u_format_zs.h"
+#include "util/u_inlines.h"
+#include "util/u_transfer_helper.h"
+
+
+struct u_transfer_helper {
+ const struct u_transfer_vtbl *vtbl;
+ bool separate_z32s8;
+ bool fake_rgtc;
+ bool msaa_map;
+};
+
+static inline bool handle_transfer(struct pipe_resource *prsc)
+{
+ struct u_transfer_helper *helper = prsc->screen->transfer_helper;
+
+ if (helper->vtbl->get_internal_format) {
+ enum pipe_format internal_format =
+ helper->vtbl->get_internal_format(prsc);
+ if (internal_format != prsc->format)
+ return true;
+ }
+
+ if (helper->msaa_map && (prsc->nr_samples > 1))
+ return true;
+
+ return false;
+}
+
+/* The pipe_transfer ptr could either be the driver's, or u_transfer,
+ * depending on whether we are intervening or not. Check handle_transfer()
+ * before dereferencing.
+ */
+struct u_transfer {
+ struct pipe_transfer base;
+ /* Note that in case of MSAA resolve for transfer plus z32s8 or fake rgtc
+ * we end up with stacked u_transfer's. The MSAA resolve case doesn't call
+ * helper->vtbl fxns directly, but calls back to pctx->transfer_map()/etc
+ * so the format related handling can work in conjunction with MSAA resolve.
+ */
+ struct pipe_transfer *trans; /* driver's transfer */
+ struct pipe_transfer *trans2; /* 2nd transfer for z32s8 */
+ void *ptr, *ptr2; /* ptr to trans, and trans2 */
+ void *staging; /* staging buffer */
+ struct pipe_resource *ss; /* staging resource for MSAA resolves */
+};
+
+static inline struct u_transfer *
+u_transfer(struct pipe_transfer *ptrans)
+{
+ debug_assert(handle_transfer(ptrans->resource));
+ return (struct u_transfer *)ptrans;
+}
+
+struct pipe_resource *
+u_transfer_helper_resource_create(struct pipe_screen *pscreen,
+ const struct pipe_resource *templ)
+{
+ struct u_transfer_helper *helper = pscreen->transfer_helper;
+ enum pipe_format format = templ->format;
+ struct pipe_resource *prsc;
+
+ if ((format == PIPE_FORMAT_Z32_FLOAT_S8X24_UINT) && helper->separate_z32s8) {
+ struct pipe_resource t = *templ;
+ struct pipe_resource *stencil;
+
+ t.format = PIPE_FORMAT_Z32_FLOAT;
+
+ prsc = helper->vtbl->resource_create(pscreen, &t);
+ if (!prsc)
+ return NULL;
+
+ prsc->format = format; /* frob the format back to the "external" format */
+
+ t.format = PIPE_FORMAT_S8_UINT;
+ stencil = helper->vtbl->resource_create(pscreen, &t);
+
+ if (!stencil) {
+ helper->vtbl->resource_destroy(pscreen, prsc);
+ return NULL;
+ }
+
+ helper->vtbl->set_stencil(prsc, stencil);
+ } else if ((util_format_description(format)->layout == UTIL_FORMAT_LAYOUT_RGTC) &&
+ helper->fake_rgtc) {
+ struct pipe_resource t = *templ;
+ t.format = PIPE_FORMAT_R8G8B8A8_UNORM;
+
+ prsc = helper->vtbl->resource_create(pscreen, &t);
+ if (!prsc)
+ return NULL;
+
+ prsc->format = format; /* frob the format back to the "external" format */
+ } else {
+ /* normal case, no special handling: */
+ prsc = helper->vtbl->resource_create(pscreen, templ);
+ if (!prsc)
+ return NULL;
+ }
+
+ return prsc;
+}
+
+void
+u_transfer_helper_resource_destroy(struct pipe_screen *pscreen,
+ struct pipe_resource *prsc)
+{
+ struct u_transfer_helper *helper = pscreen->transfer_helper;
+
+ if (helper->vtbl->get_stencil) {
+ struct pipe_resource *stencil = helper->vtbl->get_stencil(prsc);
+
+ if (stencil)
+ helper->vtbl->resource_destroy(pscreen, stencil);
+ }
+
+ helper->vtbl->resource_destroy(pscreen, prsc);
+}
+
+static bool needs_pack(unsigned usage)
+{
+ return (usage & PIPE_TRANSFER_READ) &&
+ !(usage & (PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE | PIPE_TRANSFER_DISCARD_RANGE));
+}
+
+/* In the case of transfer_map of a multi-sample resource, call back into
+ * pctx->transfer_map() to map the staging resource, to handle cases of
+ * MSAA + separate_z32s8 or fake_rgtc
+ */
+static void *
+transfer_map_msaa(struct pipe_context *pctx,
+ struct pipe_resource *prsc,
+ unsigned level, unsigned usage,
+ const struct pipe_box *box,
+ struct pipe_transfer **pptrans)
+{
+ struct pipe_screen *pscreen = pctx->screen;
+ struct u_transfer *trans = calloc(1, sizeof(*trans));
+ if (!trans)
+ return NULL;
+ struct pipe_transfer *ptrans = &trans->base;
+
+ pipe_resource_reference(&ptrans->resource, prsc);
+ ptrans->level = level;
+ ptrans->usage = usage;
+ ptrans->box = *box;
+
+ struct pipe_resource tmpl = {
+ .target = prsc->target,
+ .format = prsc->format,
+ .width0 = box->width,
+ .height0 = box->height,
+ .depth0 = 1,
+ .array_size = 1,
+ };
+ trans->ss = pscreen->resource_create(pscreen, &tmpl);
+ if (!trans->ss) {
+ free(trans);
+ return NULL;
+ }
+
+ if (needs_pack(usage)) {
+ struct pipe_blit_info blit;
+ memset(&blit, 0, sizeof(blit));
+
+ blit.src.resource = ptrans->resource;
+ blit.src.format = ptrans->resource->format;
+ blit.src.level = ptrans->level;
+ blit.src.box = *box;
+
+ blit.dst.resource = trans->ss;
+ blit.dst.format = trans->ss->format;
+ blit.dst.box.width = box->width;
+ blit.dst.box.height = box->height;
+ blit.dst.box.depth = 1;
+
+ blit.mask = util_format_get_mask(prsc->format);
+ blit.filter = PIPE_TEX_FILTER_NEAREST;
+
+ pctx->blit(pctx, &blit);
+ }
+
+ void *ss_map = pctx->transfer_map(pctx, trans->ss, 0, usage, box,
+ &trans->trans);
+ if (!ss_map) {
+ free(trans);
+ return NULL;
+ }
+
+ *pptrans = ptrans;
+ return ss_map;
+}
+
+void *
+u_transfer_helper_transfer_map(struct pipe_context *pctx,
+ struct pipe_resource *prsc,
+ unsigned level, unsigned usage,
+ const struct pipe_box *box,
+ struct pipe_transfer **pptrans)
+{
+ struct u_transfer_helper *helper = pctx->screen->transfer_helper;
+ struct u_transfer *trans;
+ struct pipe_transfer *ptrans;
+ enum pipe_format format = prsc->format;
+ unsigned width = box->width;
+ unsigned height = box->height;
+
+ if (!handle_transfer(prsc))
+ return helper->vtbl->transfer_map(pctx, prsc, level, usage, box, pptrans);
+
+ if (helper->msaa_map && (prsc->nr_samples > 1))
+ return transfer_map_msaa(pctx, prsc, level, usage, box, pptrans);
+
+ debug_assert(box->depth == 1);
+
+ trans = calloc(1, sizeof(*trans));
+ if (!trans)
+ return NULL;
+
+ ptrans = &trans->base;
+ pipe_resource_reference(&ptrans->resource, prsc);
+ ptrans->level = level;
+ ptrans->usage = usage;
+ ptrans->box = *box;
+ ptrans->stride = util_format_get_stride(format, box->width);
+ ptrans->layer_stride = ptrans->stride * box->height;
+
+ trans->staging = malloc(ptrans->layer_stride);
+ if (!trans->staging)
+ goto fail;
+
+ trans->ptr = helper->vtbl->transfer_map(pctx, prsc, level, usage, box,
+ &trans->trans);
+ if (!trans->ptr)
+ goto fail;
+
+ if (prsc->format == PIPE_FORMAT_Z32_FLOAT_S8X24_UINT) {
+ struct pipe_resource *stencil = helper->vtbl->get_stencil(prsc);
+ trans->ptr2 = helper->vtbl->transfer_map(pctx, stencil, level,
+ usage, box, &trans->trans2);
+
+ if (needs_pack(usage)) {
+ util_format_z32_float_s8x24_uint_pack_z_float(trans->staging,
+ ptrans->stride,
+ trans->ptr,
+ trans->trans->stride,
+ width, height);
+ util_format_z32_float_s8x24_uint_pack_s_8uint(trans->staging,
+ ptrans->stride,
+ trans->ptr2,
+ trans->trans2->stride,
+ width, height);
+ }
+ } else if (needs_pack(usage) &&
+ util_format_description(prsc->format)->layout == UTIL_FORMAT_LAYOUT_RGTC) {
+ switch (prsc->format) {
+ case PIPE_FORMAT_RGTC1_UNORM:
+ case PIPE_FORMAT_RGTC1_SNORM:
+ case PIPE_FORMAT_LATC1_UNORM:
+ case PIPE_FORMAT_LATC1_SNORM:
+ util_format_rgtc1_unorm_pack_rgba_8unorm(trans->staging,
+ ptrans->stride,
+ trans->ptr,
+ trans->trans->stride,
+ width, height);
+ break;
+ case PIPE_FORMAT_RGTC2_UNORM:
+ case PIPE_FORMAT_RGTC2_SNORM:
+ case PIPE_FORMAT_LATC2_UNORM:
+ case PIPE_FORMAT_LATC2_SNORM:
+ util_format_rgtc2_unorm_pack_rgba_8unorm(trans->staging,
+ ptrans->stride,
+ trans->ptr,
+ trans->trans->stride,
+ width, height);
+ break;
+ default:
+ assert(!"Unexpected format");
+ break;
+ }
+ } else {
+ unreachable("bleh");
+ }
+
+ *pptrans = ptrans;
+ return trans->staging;
+
+fail:
+ if (trans->trans)
+ helper->vtbl->transfer_unmap(pctx, trans->trans);
+ if (trans->trans2)
+ helper->vtbl->transfer_unmap(pctx, trans->trans2);
+ pipe_resource_reference(&ptrans->resource, NULL);
+ free(trans->staging);
+ free(trans);
+ return NULL;
+}
+
+static void
+flush_region(struct pipe_context *pctx, struct pipe_transfer *ptrans,
+ const struct pipe_box *box)
+{
+ struct u_transfer *trans = u_transfer(ptrans);
+ enum pipe_format format = ptrans->resource->format;
+ unsigned width = ptrans->box.width;
+ unsigned height = ptrans->box.height;
+
+ if (!(ptrans->usage & PIPE_TRANSFER_WRITE))
+ return;
+
+ if (trans->ss) {
+ struct pipe_blit_info blit;
+ memset(&blit, 0, sizeof(blit));
+
+ blit.src.resource = trans->ss;
+ blit.src.format = trans->ss->format;
+ blit.src.box.width = ptrans->box.width;
+ blit.src.box.height = ptrans->box.height;
+ blit.src.box.depth = 1;
+
+ blit.dst.resource = ptrans->resource;
+ blit.dst.format = ptrans->resource->format;
+ blit.dst.level = ptrans->level;
+ blit.dst.box = ptrans->box;
+
+ blit.mask = util_format_get_mask(ptrans->resource->format);
+ blit.filter = PIPE_TEX_FILTER_NEAREST;
+
+ pctx->blit(pctx, &blit);
+
+ return;
+ }
+
+ switch (format) {
+ case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT:
+ util_format_z32_float_s8x24_uint_unpack_z_float(trans->ptr,
+ trans->trans->stride,
+ trans->staging,
+ ptrans->stride,
+ width, height);
+ /* fallthru */
+ case PIPE_FORMAT_X32_S8X24_UINT:
+ util_format_z32_float_s8x24_uint_unpack_s_8uint(trans->ptr2,
+ trans->trans2->stride,
+ trans->staging,
+ ptrans->stride,
+ width, height);
+ break;
+ case PIPE_FORMAT_RGTC1_UNORM:
+ case PIPE_FORMAT_RGTC1_SNORM:
+ case PIPE_FORMAT_LATC1_UNORM:
+ case PIPE_FORMAT_LATC1_SNORM:
+ util_format_rgtc1_unorm_unpack_rgba_8unorm(trans->ptr,
+ trans->trans->stride,
+ trans->staging,
+ ptrans->stride,
+ width, height);
+ break;
+ case PIPE_FORMAT_RGTC2_UNORM:
+ case PIPE_FORMAT_RGTC2_SNORM:
+ case PIPE_FORMAT_LATC2_UNORM:
+ case PIPE_FORMAT_LATC2_SNORM:
+ util_format_rgtc2_unorm_unpack_rgba_8unorm(trans->ptr,
+ trans->trans->stride,
+ trans->staging,
+ ptrans->stride,
+ width, height);
+ break;
+ default:
+ assert(!"Unexpected staging transfer type");
+ break;
+ }
+}
+
+void
+u_transfer_helper_transfer_flush_region(struct pipe_context *pctx,
+ struct pipe_transfer *ptrans,
+ const struct pipe_box *box)
+{
+ struct u_transfer_helper *helper = pctx->screen->transfer_helper;
+
+ if (handle_transfer(ptrans->resource)) {
+ struct u_transfer *trans = u_transfer(ptrans);
+
+ flush_region(pctx, ptrans, box);
+
+ /* handle MSAA case, since there could be multiple levels of
+ * wrapped transfer, call pctx->transfer_flush_region()
+ * instead of helper->vtbl->transfer_flush_region()
+ */
+ if (trans->ss) {
+ pctx->transfer_flush_region(pctx, trans->trans, box);
+ return;
+ }
+
+ helper->vtbl->transfer_flush_region(pctx, trans->trans, box);
+ if (trans->trans2)
+ helper->vtbl->transfer_flush_region(pctx, trans->trans2, box);
+
+ } else {
+ helper->vtbl->transfer_flush_region(pctx, ptrans, box);
+ }
+}
+
+void
+u_transfer_helper_transfer_unmap(struct pipe_context *pctx,
+ struct pipe_transfer *ptrans)
+{
+ struct u_transfer_helper *helper = pctx->screen->transfer_helper;
+
+ if (handle_transfer(ptrans->resource)) {
+ struct u_transfer *trans = u_transfer(ptrans);
+
+ if (!(ptrans->usage & PIPE_TRANSFER_FLUSH_EXPLICIT)) {
+ struct pipe_box box;
+ u_box_2d(0, 0, ptrans->box.width, ptrans->box.height, &box);
+ flush_region(pctx, ptrans, &box);
+ }
+
+ /* in MSAA case, there could be multiple levels of wrapping
+ * so don't call helper->vtbl->transfer_unmap() directly
+ */
+ if (trans->ss) {
+ pctx->transfer_unmap(pctx, trans->trans);
+ pipe_resource_reference(&trans->ss, NULL);
+ } else {
+ helper->vtbl->transfer_unmap(pctx, trans->trans);
+ if (trans->trans2)
+ helper->vtbl->transfer_unmap(pctx, trans->trans2);
+ }
+
+ free(trans);
+ } else {
+ helper->vtbl->transfer_unmap(pctx, ptrans);
+ }
+}
+
+struct u_transfer_helper *
+u_transfer_helper_create(const struct u_transfer_vtbl *vtbl,
+ bool separate_z32s8,
+ bool fake_rgtc,
+ bool msaa_map)
+{
+ struct u_transfer_helper *helper = calloc(1, sizeof(*helper));
+
+ helper->vtbl = vtbl;
+ helper->separate_z32s8 = separate_z32s8;
+ helper->fake_rgtc = fake_rgtc;
+ helper->msaa_map = msaa_map;
+
+ return helper;
+}
+
+void
+u_transfer_helper_destroy(struct u_transfer_helper *helper)
+{
+ free(helper);
+}
diff --git a/src/gallium/auxiliary/util/u_transfer_helper.h b/src/gallium/auxiliary/util/u_transfer_helper.h
new file mode 100644
index 00000000000..392b34f0697
--- /dev/null
+++ b/src/gallium/auxiliary/util/u_transfer_helper.h
@@ -0,0 +1,132 @@
+/*
+ * Copyright © 2017 Red Hat
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#ifndef _U_TRANSFER_HELPER_H
+#define _U_TRANSFER_HELPER_H
+
+#include "pipe/p_state.h"
+#include "pipe/p_context.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/* A helper to implement various "lowering" for transfers:
+ *
+ * - exposing separate z32 and s8 as z32x24s8
+ * - fake RGTC support for GLES class hardware which needs it to expose GL3+
+ * - MSAA resolves
+ *
+ * To use this, drivers should:
+ *
+ * 1) populate u_transfer_vtbl and plug that into pipe_screen::transfer_helper
+ * 2) plug the the transfer helpers into pipe_screen/pipe_context
+ *
+ * To avoid subclassing pipe_resource (and conflicting with threaded_context)
+ * the vtbl contains setter/getter methods used for fake_rgct & separate_stencil
+ * to access the internal_format and separate stencil buffer.
+ */
+
+struct u_transfer_vtbl {
+ /* NOTE I am not expecting resource_create_from_handle() or
+ * resource_create_with_modifiers() paths to be creating any
+ * resources that need special handling. Otherwise they would
+ * need to be wrapped too.
+ */
+ struct pipe_resource * (*resource_create)(struct pipe_screen *pscreen,
+ const struct pipe_resource *templ);
+
+ void (*resource_destroy)(struct pipe_screen *pscreen,
+ struct pipe_resource *prsc);
+
+ void *(*transfer_map)(struct pipe_context *pctx,
+ struct pipe_resource *prsc,
+ unsigned level,
+ unsigned usage,
+ const struct pipe_box *box,
+ struct pipe_transfer **pptrans);
+
+
+ void (*transfer_flush_region)(struct pipe_context *pctx,
+ struct pipe_transfer *ptrans,
+ const struct pipe_box *box);
+
+ void (*transfer_unmap)(struct pipe_context *pctx,
+ struct pipe_transfer *ptrans);
+
+ /*
+ * auxiliary methods to access internal format, stencil:
+ */
+
+ /**
+ * Must be implemented if separate_z32s8 or fake_rgtc is used. The
+ * internal_format is the format the resource was created with. In
+ * the case of separate_z32s8 or fake_rgtc, prsc->format is set back
+ * to the state tracker visible format (Z32_FLOAT_S8X24_UINT or
+ * PIPE_FORMAT_{RTGC,LATC}* after the resource is created.
+ */
+ enum pipe_format (*get_internal_format)(struct pipe_resource *prsc);
+
+ /**
+ * Must be implemented if separate_z32s8 is used. Used to set/get
+ * the separate s8 stencil buffer.
+ */
+ void (*set_stencil)(struct pipe_resource *prsc, struct pipe_resource *stencil);
+ struct pipe_resource *(*get_stencil)(struct pipe_resource *prsc);
+};
+
+struct pipe_resource *u_transfer_helper_resource_create(
+ struct pipe_screen *pscreen, const struct pipe_resource *templ);
+
+void u_transfer_helper_resource_destroy(struct pipe_screen *pscreen,
+ struct pipe_resource *prsc);
+
+void *u_transfer_helper_transfer_map(struct pipe_context *pctx,
+ struct pipe_resource *prsc,
+ unsigned level,
+ unsigned usage,
+ const struct pipe_box *box,
+ struct pipe_transfer **pptrans);
+
+
+void u_transfer_helper_transfer_flush_region(struct pipe_context *pctx,
+ struct pipe_transfer *ptrans,
+ const struct pipe_box *box);
+
+void u_transfer_helper_transfer_unmap(struct pipe_context *pctx,
+ struct pipe_transfer *ptrans);
+
+struct u_transfer_helper;
+
+struct u_transfer_helper * u_transfer_helper_create(const struct u_transfer_vtbl *vtbl,
+ bool separate_z32s8,
+ bool fake_rgtc,
+ bool msaa_map);
+
+void u_transfer_helper_destroy(struct u_transfer_helper *helper);
+
+#ifdef __cplusplus
+} // extern "C" {
+#endif
+
+#endif /* _U_TRANSFER_HELPER_H */
diff --git a/src/gallium/include/pipe/p_screen.h b/src/gallium/include/pipe/p_screen.h
index c249c7d63b4..101e229088b 100644
--- a/src/gallium/include/pipe/p_screen.h
+++ b/src/gallium/include/pipe/p_screen.h
@@ -60,7 +60,7 @@ struct pipe_box;
struct pipe_memory_info;
struct disk_cache;
struct driOptionCache;
-
+struct u_transfer_helper;

/**
* Gallium screen/adapter context. Basically everything
@@ -68,6 +68,12 @@ struct driOptionCache;
* context.
*/
struct pipe_screen {
+
+ /**
+ * For drivers using u_transfer_helper:
+ */
+ struct u_transfer_helper *transfer_helper;
+
void (*destroy)( struct pipe_screen * );

const char *(*get_name)( struct pipe_screen * );
--
2.13.6
Rob Clark
2017-12-04 17:05:22 UTC
Reply
Permalink
Raw Message
Signed-off-by: Rob Clark <***@gmail.com>
---
src/gallium/drivers/freedreno/freedreno_resource.c | 271 ++++-----------------
src/gallium/drivers/freedreno/freedreno_resource.h | 2 +-
2 files changed, 44 insertions(+), 229 deletions(-)

diff --git a/src/gallium/drivers/freedreno/freedreno_resource.c b/src/gallium/drivers/freedreno/freedreno_resource.c
index 2b8831f65e5..c2c0271e89a 100644
--- a/src/gallium/drivers/freedreno/freedreno_resource.c
+++ b/src/gallium/drivers/freedreno/freedreno_resource.c
@@ -289,114 +289,16 @@ fd_resource_layer_offset(struct fd_resource *rsc,
return layer * slice->size0;
}

-static void
-fd_resource_flush_z32s8(struct fd_transfer *trans, const struct pipe_box *box)
-{
- struct fd_resource *rsc = fd_resource(trans->base.resource);
- struct fd_resource_slice *slice = fd_resource_slice(rsc, trans->base.level);
- struct fd_resource_slice *sslice = fd_resource_slice(rsc->stencil, trans->base.level);
- enum pipe_format format = trans->base.resource->format;
-
- float *depth = fd_bo_map(rsc->bo) + slice->offset +
- fd_resource_layer_offset(rsc, slice, trans->base.box.z) +
- (trans->base.box.y + box->y) * slice->pitch * 4 + (trans->base.box.x + box->x) * 4;
- uint8_t *stencil = fd_bo_map(rsc->stencil->bo) + sslice->offset +
- fd_resource_layer_offset(rsc->stencil, sslice, trans->base.box.z) +
- (trans->base.box.y + box->y) * sslice->pitch + trans->base.box.x + box->x;
-
- if (format != PIPE_FORMAT_X32_S8X24_UINT)
- util_format_z32_float_s8x24_uint_unpack_z_float(
- depth, slice->pitch * 4,
- trans->staging, trans->base.stride,
- box->width, box->height);
-
- util_format_z32_float_s8x24_uint_unpack_s_8uint(
- stencil, sslice->pitch,
- trans->staging, trans->base.stride,
- box->width, box->height);
-}
-
-static void
-fd_resource_flush_rgtc(struct fd_transfer *trans, const struct pipe_box *box)
-{
- struct fd_resource *rsc = fd_resource(trans->base.resource);
- struct fd_resource_slice *slice = fd_resource_slice(rsc, trans->base.level);
- enum pipe_format format = trans->base.resource->format;
-
- uint8_t *data = fd_bo_map(rsc->bo) + slice->offset +
- fd_resource_layer_offset(rsc, slice, trans->base.box.z) +
- ((trans->base.box.y + box->y) * slice->pitch +
- trans->base.box.x + box->x) * rsc->cpp;
-
- uint8_t *source = trans->staging +
- util_format_get_nblocksy(format, box->y) * trans->base.stride +
- util_format_get_stride(format, box->x);
-
- switch (format) {
- case PIPE_FORMAT_RGTC1_UNORM:
- case PIPE_FORMAT_RGTC1_SNORM:
- case PIPE_FORMAT_LATC1_UNORM:
- case PIPE_FORMAT_LATC1_SNORM:
- util_format_rgtc1_unorm_unpack_rgba_8unorm(
- data, slice->pitch * rsc->cpp,
- source, trans->base.stride,
- box->width, box->height);
- break;
- case PIPE_FORMAT_RGTC2_UNORM:
- case PIPE_FORMAT_RGTC2_SNORM:
- case PIPE_FORMAT_LATC2_UNORM:
- case PIPE_FORMAT_LATC2_SNORM:
- util_format_rgtc2_unorm_unpack_rgba_8unorm(
- data, slice->pitch * rsc->cpp,
- source, trans->base.stride,
- box->width, box->height);
- break;
- default:
- assert(!"Unexpected format\n");
- break;
- }
-}
-
-static void
-fd_resource_flush(struct fd_transfer *trans, const struct pipe_box *box)
-{
- enum pipe_format format = trans->base.resource->format;
-
- switch (format) {
- case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT:
- case PIPE_FORMAT_X32_S8X24_UINT:
- fd_resource_flush_z32s8(trans, box);
- break;
- case PIPE_FORMAT_RGTC1_UNORM:
- case PIPE_FORMAT_RGTC1_SNORM:
- case PIPE_FORMAT_RGTC2_UNORM:
- case PIPE_FORMAT_RGTC2_SNORM:
- case PIPE_FORMAT_LATC1_UNORM:
- case PIPE_FORMAT_LATC1_SNORM:
- case PIPE_FORMAT_LATC2_UNORM:
- case PIPE_FORMAT_LATC2_SNORM:
- fd_resource_flush_rgtc(trans, box);
- break;
- default:
- assert(!"Unexpected staging transfer type");
- break;
- }
-}
-
static void fd_resource_transfer_flush_region(struct pipe_context *pctx,
struct pipe_transfer *ptrans,
const struct pipe_box *box)
{
struct fd_resource *rsc = fd_resource(ptrans->resource);
- struct fd_transfer *trans = fd_transfer(ptrans);

if (ptrans->resource->target == PIPE_BUFFER)
util_range_add(&rsc->valid_buffer_range,
ptrans->box.x + box->x,
ptrans->box.x + box->x + box->width);
-
- if (trans->staging)
- fd_resource_flush(trans, box);
}

static void
@@ -405,18 +307,9 @@ fd_resource_transfer_unmap(struct pipe_context *pctx,
{
struct fd_context *ctx = fd_context(pctx);
struct fd_resource *rsc = fd_resource(ptrans->resource);
- struct fd_transfer *trans = fd_transfer(ptrans);
-
- if (trans->staging && !(ptrans->usage & PIPE_TRANSFER_FLUSH_EXPLICIT)) {
- struct pipe_box box;
- u_box_2d(0, 0, ptrans->box.width, ptrans->box.height, &box);
- fd_resource_flush(trans, &box);
- }

if (!(ptrans->usage & PIPE_TRANSFER_UNSYNCHRONIZED)) {
fd_bo_cpu_fini(rsc->bo);
- if (rsc->stencil)
- fd_bo_cpu_fini(rsc->stencil->bo);
}

util_range_add(&rsc->valid_buffer_range,
@@ -425,8 +318,6 @@ fd_resource_transfer_unmap(struct pipe_context *pctx,

pipe_resource_reference(&ptrans->resource, NULL);
slab_free(&ctx->transfer_pool, ptrans);
-
- free(trans->staging);
}

static void *
@@ -476,8 +367,6 @@ fd_resource_transfer_map(struct pipe_context *pctx,

if (usage & PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE) {
realloc_bo(rsc, fd_bo_size(rsc->bo));
- if (rsc->stencil)
- realloc_bo(rsc->stencil, fd_bo_size(rsc->stencil->bo));
rebind_resource(ctx, prsc);
} else if ((usage & PIPE_TRANSFER_WRITE) &&
prsc->target == PIPE_BUFFER &&
@@ -572,100 +461,6 @@ fd_resource_transfer_map(struct pipe_context *pctx,
box->x / util_format_get_blockwidth(format) * rsc->cpp +
fd_resource_layer_offset(rsc, slice, box->z);

- if (prsc->format == PIPE_FORMAT_Z32_FLOAT_S8X24_UINT ||
- prsc->format == PIPE_FORMAT_X32_S8X24_UINT) {
- assert(trans->base.box.depth == 1);
-
- trans->base.stride = trans->base.box.width * rsc->cpp * 2;
- trans->staging = malloc(trans->base.stride * trans->base.box.height);
- if (!trans->staging)
- goto fail;
-
- /* if we're not discarding the whole range (or resource), we must copy
- * the real data in.
- */
- if (!(usage & (PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE |
- PIPE_TRANSFER_DISCARD_RANGE))) {
- struct fd_resource_slice *sslice =
- fd_resource_slice(rsc->stencil, level);
- void *sbuf = fd_bo_map(rsc->stencil->bo);
- if (!sbuf)
- goto fail;
-
- float *depth = (float *)(buf + slice->offset +
- fd_resource_layer_offset(rsc, slice, box->z) +
- box->y * slice->pitch * 4 + box->x * 4);
- uint8_t *stencil = sbuf + sslice->offset +
- fd_resource_layer_offset(rsc->stencil, sslice, box->z) +
- box->y * sslice->pitch + box->x;
-
- if (format != PIPE_FORMAT_X32_S8X24_UINT)
- util_format_z32_float_s8x24_uint_pack_z_float(
- trans->staging, trans->base.stride,
- depth, slice->pitch * 4,
- box->width, box->height);
-
- util_format_z32_float_s8x24_uint_pack_s_8uint(
- trans->staging, trans->base.stride,
- stencil, sslice->pitch,
- box->width, box->height);
- }
-
- buf = trans->staging;
- offset = 0;
- } else if (rsc->internal_format != format &&
- util_format_description(format)->layout == UTIL_FORMAT_LAYOUT_RGTC) {
- assert(trans->base.box.depth == 1);
-
- trans->base.stride = util_format_get_stride(
- format, trans->base.box.width);
- trans->staging = malloc(
- util_format_get_2d_size(format, trans->base.stride,
- trans->base.box.height));
- if (!trans->staging)
- goto fail;
-
- /* if we're not discarding the whole range (or resource), we must copy
- * the real data in.
- */
- if (!(usage & (PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE |
- PIPE_TRANSFER_DISCARD_RANGE))) {
- uint8_t *rgba8 = (uint8_t *)buf + slice->offset +
- fd_resource_layer_offset(rsc, slice, box->z) +
- box->y * slice->pitch * rsc->cpp + box->x * rsc->cpp;
-
- switch (format) {
- case PIPE_FORMAT_RGTC1_UNORM:
- case PIPE_FORMAT_RGTC1_SNORM:
- case PIPE_FORMAT_LATC1_UNORM:
- case PIPE_FORMAT_LATC1_SNORM:
- util_format_rgtc1_unorm_pack_rgba_8unorm(
- trans->staging, trans->base.stride,
- rgba8, slice->pitch * rsc->cpp,
- box->width, box->height);
- break;
- case PIPE_FORMAT_RGTC2_UNORM:
- case PIPE_FORMAT_RGTC2_SNORM:
- case PIPE_FORMAT_LATC2_UNORM:
- case PIPE_FORMAT_LATC2_SNORM:
- util_format_rgtc2_unorm_pack_rgba_8unorm(
- trans->staging, trans->base.stride,
- rgba8, slice->pitch * rsc->cpp,
- box->width, box->height);
- break;
- default:
- assert(!"Unexpected format");
- break;
- }
- }
-
- buf = trans->staging;
- offset = 0;
- }
-
- if (usage & PIPE_TRANSFER_WRITE)
- rsc->valid = true;
-
*pptrans = ptrans;

return buf + offset;
@@ -835,11 +630,6 @@ fd_resource_create(struct pipe_screen *pscreen,

util_range_init(&rsc->valid_buffer_range);

- if (format == PIPE_FORMAT_Z32_FLOAT_S8X24_UINT)
- format = PIPE_FORMAT_Z32_FLOAT;
- else if (screen->gpu_id < 400 &&
- util_format_description(format)->layout == UTIL_FORMAT_LAYOUT_RGTC)
- format = PIPE_FORMAT_R8G8B8A8_UNORM;
rsc->internal_format = format;
rsc->cpp = util_format_get_blocksize(format);

@@ -894,19 +684,6 @@ fd_resource_create(struct pipe_screen *pscreen,
if (!rsc->bo)
goto fail;

- /* There is no native Z32F_S8 sampling or rendering format, so this must
- * be emulated via two separate textures. The depth texture still keeps
- * its Z32F_S8 format though, and we also keep a reference to a separate
- * S8 texture.
- */
- if (tmpl->format == PIPE_FORMAT_Z32_FLOAT_S8X24_UINT) {
- struct pipe_resource stencil = *tmpl;
- stencil.format = PIPE_FORMAT_S8_UINT;
- rsc->stencil = fd_resource(fd_resource_create(pscreen, &stencil));
- if (!rsc->stencil)
- goto fail;
- }
-
return prsc;
fail:
fd_resource_destroy(pscreen, prsc);
@@ -1183,21 +960,59 @@ fd_invalidate_resource(struct pipe_context *pctx, struct pipe_resource *prsc)
rsc->valid = false;
}

+static enum pipe_format
+fd_resource_get_internal_format(struct pipe_resource *prsc)
+{
+ return fd_resource(prsc)->internal_format;
+}
+
+static void
+fd_resource_set_stencil(struct pipe_resource *prsc,
+ struct pipe_resource *stencil)
+{
+ fd_resource(prsc)->stencil = fd_resource(stencil);
+}
+
+static struct pipe_resource *
+fd_resource_get_stencil(struct pipe_resource *prsc)
+{
+ struct fd_resource *rsc = fd_resource(prsc);
+ if (rsc->stencil)
+ return &rsc->stencil->base;
+ return NULL;
+}
+
+static const struct u_transfer_vtbl transfer_vtbl = {
+ .resource_create = fd_resource_create,
+ .resource_destroy = fd_resource_destroy,
+ .transfer_map = fd_resource_transfer_map,
+ .transfer_flush_region = fd_resource_transfer_flush_region,
+ .transfer_unmap = fd_resource_transfer_unmap,
+ .get_internal_format = fd_resource_get_internal_format,
+ .set_stencil = fd_resource_set_stencil,
+ .get_stencil = fd_resource_get_stencil,
+};
+
void
fd_resource_screen_init(struct pipe_screen *pscreen)
{
- pscreen->resource_create = fd_resource_create;
+ bool fake_rgtc = fd_screen(pscreen)->gpu_id < 400;
+
+ pscreen->resource_create = u_transfer_helper_resource_create;
pscreen->resource_from_handle = fd_resource_from_handle;
pscreen->resource_get_handle = fd_resource_get_handle;
- pscreen->resource_destroy = fd_resource_destroy;
+ pscreen->resource_destroy = u_transfer_helper_resource_destroy;
+
+ pscreen->transfer_helper = u_transfer_helper_create(&transfer_vtbl,
+ true, fake_rgtc, true);
}

void
fd_resource_context_init(struct pipe_context *pctx)
{
- pctx->transfer_map = fd_resource_transfer_map;
- pctx->transfer_flush_region = fd_resource_transfer_flush_region;
- pctx->transfer_unmap = fd_resource_transfer_unmap;
+ pctx->transfer_map = u_transfer_helper_transfer_map;
+ pctx->transfer_flush_region = u_transfer_helper_transfer_flush_region;
+ pctx->transfer_unmap = u_transfer_helper_transfer_unmap;
pctx->buffer_subdata = u_default_buffer_subdata;
pctx->texture_subdata = u_default_texture_subdata;
pctx->create_surface = fd_create_surface;
diff --git a/src/gallium/drivers/freedreno/freedreno_resource.h b/src/gallium/drivers/freedreno/freedreno_resource.h
index 2a2a6c2aa17..c075570e543 100644
--- a/src/gallium/drivers/freedreno/freedreno_resource.h
+++ b/src/gallium/drivers/freedreno/freedreno_resource.h
@@ -31,6 +31,7 @@

#include "util/list.h"
#include "util/u_range.h"
+#include "util/u_transfer_helper.h"

#include "freedreno_batch.h"
#include "freedreno_util.h"
@@ -131,7 +132,6 @@ pending(struct fd_resource *rsc, bool write)

struct fd_transfer {
struct pipe_transfer base;
- void *staging;
};

static inline struct fd_transfer *
--
2.13.6
Eric Anholt
2017-12-06 21:48:34 UTC
Reply
Permalink
Raw Message
Post by Rob Clark
Add a new helper that drivers can use to emulate various things that
1) z32_s8x24.. gl/gallium treats this as a single buffer with depth
and stencil interleaved but hardware frequently treats this as
separate z32 and s8 buffers. Special pack/unpack handling is
needed in transfer_map/unmap to pack/unpack the exposed buffer
2) fake RGTC.. GPUs designed with GLES in mind, but which can other-
wise do GL3, if native RGTC is not supported it can be emulated
by converting to uncompressed internally, but needs pack/unpack
in transfer_map/unmap
3) MSAA resolves in the transfer_map() case
v2: add MSAA resolve based on Eric's "gallium: Add helpers for MSAA
resolves in pipe_transfer_map()/unmap()." patch; avoid wrapping
pipe_resource, to make it possible for drivers to use both this
and threaded_context.
The driver side is clean enough with this layer that I'm pretty happy
now. Just one significant review comment, then I think we'll be
ready...
Post by Rob Clark
+static void
+flush_region(struct pipe_context *pctx, struct pipe_transfer *ptrans,
+ const struct pipe_box *box)
+{
+ struct u_transfer *trans = u_transfer(ptrans);
+ enum pipe_format format = ptrans->resource->format;
+ unsigned width = ptrans->box.width;
+ unsigned height = ptrans->box.height;
It seems silly to be implementing flush_region and ignoring the box
argument to flush the entire mapped region on every call. We should
either drop this implementation in favor of the no-op and flush at
unmap, or actually use the box in the flushes.

That said, I don't think you can reach flush_region with explicit flush
for non-buffer resources?
Post by Rob Clark
+
+ if (!(ptrans->usage & PIPE_TRANSFER_WRITE))
+ return;
+
+ if (trans->ss) {
+ struct pipe_blit_info blit;
+ memset(&blit, 0, sizeof(blit));
+
+ blit.src.resource = trans->ss;
+ blit.src.format = trans->ss->format;
+ blit.src.box.width = ptrans->box.width;
+ blit.src.box.height = ptrans->box.height;
+ blit.src.box.depth = 1;
+
+ blit.dst.resource = ptrans->resource;
+ blit.dst.format = ptrans->resource->format;
+ blit.dst.level = ptrans->level;
+ blit.dst.box = ptrans->box;
+
+ blit.mask = util_format_get_mask(ptrans->resource->format);
+ blit.filter = PIPE_TEX_FILTER_NEAREST;
+
+ pctx->blit(pctx, &blit);
+
+ return;
+ }
+
+ switch (format) {
+ util_format_z32_float_s8x24_uint_unpack_z_float(trans->ptr,
+ trans->trans->stride,
+ trans->staging,
+ ptrans->stride,
+ width, height);
+ /* fallthru */
+ util_format_z32_float_s8x24_uint_unpack_s_8uint(trans->ptr2,
+ trans->trans2->stride,
+ trans->staging,
+ ptrans->stride,
+ width, height);
+ break;
+ util_format_rgtc1_unorm_unpack_rgba_8unorm(trans->ptr,
+ trans->trans->stride,
+ trans->staging,
+ ptrans->stride,
+ width, height);
+ break;
+ util_format_rgtc2_unorm_unpack_rgba_8unorm(trans->ptr,
+ trans->trans->stride,
+ trans->staging,
+ ptrans->stride,
+ width, height);
+ break;
+ assert(!"Unexpected staging transfer type");
+ break;
+ }
+}
diff --git a/src/gallium/auxiliary/util/u_transfer_helper.h b/src/gallium/auxiliary/util/u_transfer_helper.h
new file mode 100644
index 00000000000..392b34f0697
--- /dev/null
+++ b/src/gallium/auxiliary/util/u_transfer_helper.h
@@ -0,0 +1,132 @@
+/*
+ * Copyright © 2017 Red Hat
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#ifndef _U_TRANSFER_HELPER_H
+#define _U_TRANSFER_HELPER_H
+
+#include "pipe/p_state.h"
+#include "pipe/p_context.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+ *
+ * - exposing separate z32 and s8 as z32x24s8
+ * - fake RGTC support for GLES class hardware which needs it to expose GL3+
+ * - MSAA resolves
+ *
+ *
+ * 1) populate u_transfer_vtbl and plug that into pipe_screen::transfer_helper
+ * 2) plug the the transfer helpers into pipe_screen/pipe_context
s/the the/the/
Post by Rob Clark
+ *
+ * To avoid subclassing pipe_resource (and conflicting with threaded_context)
+ * the vtbl contains setter/getter methods used for fake_rgct & separate_stencil
+ * to access the internal_format and separate stencil buffer.
+ */
+
+struct u_transfer_vtbl {
+ /* NOTE I am not expecting resource_create_from_handle() or
+ * resource_create_with_modifiers() paths to be creating any
+ * resources that need special handling. Otherwise they would
+ * need to be wrapped too.
+ */
+ struct pipe_resource * (*resource_create)(struct pipe_screen *pscreen,
+ const struct pipe_resource *templ);
+
+ void (*resource_destroy)(struct pipe_screen *pscreen,
+ struct pipe_resource *prsc);
+
+ void *(*transfer_map)(struct pipe_context *pctx,
+ struct pipe_resource *prsc,
+ unsigned level,
+ unsigned usage,
+ const struct pipe_box *box,
+ struct pipe_transfer **pptrans);
+
+
+ void (*transfer_flush_region)(struct pipe_context *pctx,
+ struct pipe_transfer *ptrans,
+ const struct pipe_box *box);
+
+ void (*transfer_unmap)(struct pipe_context *pctx,
+ struct pipe_transfer *ptrans);
+
+ /*
+ */
+
+ /**
+ * Must be implemented if separate_z32s8 or fake_rgtc is used. The
+ * internal_format is the format the resource was created with. In
+ * the case of separate_z32s8 or fake_rgtc, prsc->format is set back
+ * to the state tracker visible format (Z32_FLOAT_S8X24_UINT or
+ * PIPE_FORMAT_{RTGC,LATC}* after the resource is created.
+ */
+ enum pipe_format (*get_internal_format)(struct pipe_resource *prsc);
+
+ /**
+ * Must be implemented if separate_z32s8 is used. Used to set/get
+ * the separate s8 stencil buffer.
+ */
+ void (*set_stencil)(struct pipe_resource *prsc, struct pipe_resource *stencil);
+ struct pipe_resource *(*get_stencil)(struct pipe_resource *prsc);
Maybe we should note that these two ops are intended to be pointer
assignments, not refcounted?
Rob Clark
2017-12-06 22:50:17 UTC
Reply
Permalink
Raw Message
Post by Eric Anholt
Post by Rob Clark
Add a new helper that drivers can use to emulate various things that
1) z32_s8x24.. gl/gallium treats this as a single buffer with depth
and stencil interleaved but hardware frequently treats this as
separate z32 and s8 buffers. Special pack/unpack handling is
needed in transfer_map/unmap to pack/unpack the exposed buffer
2) fake RGTC.. GPUs designed with GLES in mind, but which can other-
wise do GL3, if native RGTC is not supported it can be emulated
by converting to uncompressed internally, but needs pack/unpack
in transfer_map/unmap
3) MSAA resolves in the transfer_map() case
v2: add MSAA resolve based on Eric's "gallium: Add helpers for MSAA
resolves in pipe_transfer_map()/unmap()." patch; avoid wrapping
pipe_resource, to make it possible for drivers to use both this
and threaded_context.
The driver side is clean enough with this layer that I'm pretty happy
now. Just one significant review comment, then I think we'll be
ready...
Post by Rob Clark
+static void
+flush_region(struct pipe_context *pctx, struct pipe_transfer *ptrans,
+ const struct pipe_box *box)
+{
+ struct u_transfer *trans = u_transfer(ptrans);
+ enum pipe_format format = ptrans->resource->format;
+ unsigned width = ptrans->box.width;
+ unsigned height = ptrans->box.height;
It seems silly to be implementing flush_region and ignoring the box
argument to flush the entire mapped region on every call. We should
either drop this implementation in favor of the no-op and flush at
unmap, or actually use the box in the flushes.
oh, whoops.. and I guess in theory I should use those dimensions for
the MSAA blit too..
Post by Eric Anholt
That said, I don't think you can reach flush_region with explicit flush
for non-buffer resources?
hmm, not 100% sure about the APIs on the GL side of things, but I
think if that were the case mesa/st would dtrt. (I guess it could be
different w/ gallium9, not that I have a big collection of windows arm
games to play :-P)
Post by Eric Anholt
Post by Rob Clark
+
+ if (!(ptrans->usage & PIPE_TRANSFER_WRITE))
+ return;
+
+ if (trans->ss) {
+ struct pipe_blit_info blit;
+ memset(&blit, 0, sizeof(blit));
+
+ blit.src.resource = trans->ss;
+ blit.src.format = trans->ss->format;
+ blit.src.box.width = ptrans->box.width;
+ blit.src.box.height = ptrans->box.height;
+ blit.src.box.depth = 1;
+
+ blit.dst.resource = ptrans->resource;
+ blit.dst.format = ptrans->resource->format;
+ blit.dst.level = ptrans->level;
+ blit.dst.box = ptrans->box;
+
+ blit.mask = util_format_get_mask(ptrans->resource->format);
+ blit.filter = PIPE_TEX_FILTER_NEAREST;
+
+ pctx->blit(pctx, &blit);
+
+ return;
+ }
+
+ switch (format) {
+ util_format_z32_float_s8x24_uint_unpack_z_float(trans->ptr,
+ trans->trans->stride,
+ trans->staging,
+ ptrans->stride,
+ width, height);
+ /* fallthru */
+ util_format_z32_float_s8x24_uint_unpack_s_8uint(trans->ptr2,
+ trans->trans2->stride,
+ trans->staging,
+ ptrans->stride,
+ width, height);
+ break;
+ util_format_rgtc1_unorm_unpack_rgba_8unorm(trans->ptr,
+ trans->trans->stride,
+ trans->staging,
+ ptrans->stride,
+ width, height);
+ break;
+ util_format_rgtc2_unorm_unpack_rgba_8unorm(trans->ptr,
+ trans->trans->stride,
+ trans->staging,
+ ptrans->stride,
+ width, height);
+ break;
+ assert(!"Unexpected staging transfer type");
+ break;
+ }
+}
diff --git a/src/gallium/auxiliary/util/u_transfer_helper.h b/src/gallium/auxiliary/util/u_transfer_helper.h
new file mode 100644
index 00000000000..392b34f0697
--- /dev/null
+++ b/src/gallium/auxiliary/util/u_transfer_helper.h
@@ -0,0 +1,132 @@
+/*
+ * Copyright © 2017 Red Hat
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#ifndef _U_TRANSFER_HELPER_H
+#define _U_TRANSFER_HELPER_H
+
+#include "pipe/p_state.h"
+#include "pipe/p_context.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+ *
+ * - exposing separate z32 and s8 as z32x24s8
+ * - fake RGTC support for GLES class hardware which needs it to expose GL3+
+ * - MSAA resolves
+ *
+ *
+ * 1) populate u_transfer_vtbl and plug that into pipe_screen::transfer_helper
+ * 2) plug the the transfer helpers into pipe_screen/pipe_context
s/the the/the/
opps
Post by Eric Anholt
Post by Rob Clark
+ *
+ * To avoid subclassing pipe_resource (and conflicting with threaded_context)
+ * the vtbl contains setter/getter methods used for fake_rgct & separate_stencil
+ * to access the internal_format and separate stencil buffer.
+ */
+
+struct u_transfer_vtbl {
+ /* NOTE I am not expecting resource_create_from_handle() or
+ * resource_create_with_modifiers() paths to be creating any
+ * resources that need special handling. Otherwise they would
+ * need to be wrapped too.
+ */
+ struct pipe_resource * (*resource_create)(struct pipe_screen *pscreen,
+ const struct pipe_resource *templ);
+
+ void (*resource_destroy)(struct pipe_screen *pscreen,
+ struct pipe_resource *prsc);
+
+ void *(*transfer_map)(struct pipe_context *pctx,
+ struct pipe_resource *prsc,
+ unsigned level,
+ unsigned usage,
+ const struct pipe_box *box,
+ struct pipe_transfer **pptrans);
+
+
+ void (*transfer_flush_region)(struct pipe_context *pctx,
+ struct pipe_transfer *ptrans,
+ const struct pipe_box *box);
+
+ void (*transfer_unmap)(struct pipe_context *pctx,
+ struct pipe_transfer *ptrans);
+
+ /*
+ */
+
+ /**
+ * Must be implemented if separate_z32s8 or fake_rgtc is used. The
+ * internal_format is the format the resource was created with. In
+ * the case of separate_z32s8 or fake_rgtc, prsc->format is set back
+ * to the state tracker visible format (Z32_FLOAT_S8X24_UINT or
+ * PIPE_FORMAT_{RTGC,LATC}* after the resource is created.
+ */
+ enum pipe_format (*get_internal_format)(struct pipe_resource *prsc);
+
+ /**
+ * Must be implemented if separate_z32s8 is used. Used to set/get
+ * the separate s8 stencil buffer.
+ */
+ void (*set_stencil)(struct pipe_resource *prsc, struct pipe_resource *stencil);
+ struct pipe_resource *(*get_stencil)(struct pipe_resource *prsc);
Maybe we should note that these two ops are intended to be pointer
assignments, not refcounted?
Yeah, probably.. and I guess document who destroys the stencil
resource. I'm pretty sure I was leaking those before in freedreno..

BR,
-R

Loading...