virgl: move resource metadata into base resource
authorGurchetan Singh <gurchetansingh@chromium.org>
Sat, 1 Dec 2018 04:45:44 +0000 (20:45 -0800)
committerBas Nieuwenhuizen <bas@basnieuwenhuizen.nl>
Wed, 19 Dec 2018 12:29:16 +0000 (13:29 +0100)
A resource is just a buffer with some metadata.

Reviewed-by: Elie Tournier <elie.tournier@collabora.com>
src/gallium/drivers/virgl/virgl_buffer.c
src/gallium/drivers/virgl/virgl_context.c
src/gallium/drivers/virgl/virgl_resource.h
src/gallium/drivers/virgl/virgl_texture.c

index bdbb35178264bd09895cf6d6e16a0cdcd661888b..c0f2afe3fb0a6a304f0129595ea78e6510b171d2 100644 (file)
@@ -31,9 +31,9 @@ static void virgl_buffer_destroy(struct pipe_screen *screen,
                                  struct pipe_resource *buf)
 {
    struct virgl_screen *vs = virgl_screen(screen);
-   struct virgl_buffer *vbuf = virgl_buffer(buf);
+   struct virgl_resource *vbuf = virgl_resource(buf);
 
-   vs->vws->resource_unref(vs->vws, vbuf->base.hw_res);
+   vs->vws->resource_unref(vs->vws, vbuf->hw_res);
    FREE(vbuf);
 }
 
@@ -46,7 +46,7 @@ static void *virgl_buffer_transfer_map(struct pipe_context *ctx,
 {
    struct virgl_context *vctx = virgl_context(ctx);
    struct virgl_screen *vs = virgl_screen(ctx->screen);
-   struct virgl_buffer *vbuf = virgl_buffer(resource);
+   struct virgl_resource *vbuf = virgl_resource(resource);
    struct virgl_transfer *trans;
    void *ptr;
    bool readback;
@@ -55,7 +55,7 @@ static void *virgl_buffer_transfer_map(struct pipe_context *ctx,
    if (usage & PIPE_TRANSFER_READ)
       doflushwait = true;
    else
-      doflushwait = virgl_res_needs_flush_wait(vctx, &vbuf->base, usage);
+      doflushwait = virgl_res_needs_flush_wait(vctx, vbuf, usage);
 
    if (doflushwait)
       ctx->flush(ctx, NULL, 0);
@@ -63,18 +63,18 @@ static void *virgl_buffer_transfer_map(struct pipe_context *ctx,
    trans = virgl_resource_create_transfer(ctx, resource, &vbuf->metadata, level,
                                           usage, box);
 
-   readback = virgl_res_needs_readback(vctx, &vbuf->base, usage);
+   readback = virgl_res_needs_readback(vctx, vbuf, usage);
    if (readback)
-      vs->vws->transfer_get(vs->vws, vbuf->base.hw_res, box, trans->base.stride,
+      vs->vws->transfer_get(vs->vws, vbuf->hw_res, box, trans->base.stride,
                             trans->l_stride, trans->offset, level);
 
    if (!(usage & PIPE_TRANSFER_UNSYNCHRONIZED))
       doflushwait = true;
 
    if (doflushwait || readback)
-      vs->vws->resource_wait(vs->vws, vbuf->base.hw_res);
+      vs->vws->resource_wait(vs->vws, vbuf->hw_res);
 
-   ptr = vs->vws->resource_map(vs->vws, vbuf->base.hw_res);
+   ptr = vs->vws->resource_map(vs->vws, vbuf->hw_res);
    if (!ptr) {
       return NULL;
    }
@@ -88,7 +88,7 @@ static void virgl_buffer_transfer_unmap(struct pipe_context *ctx,
 {
    struct virgl_context *vctx = virgl_context(ctx);
    struct virgl_transfer *trans = virgl_transfer(transfer);
-   struct virgl_buffer *vbuf = virgl_buffer(transfer->resource);
+   struct virgl_resource *vbuf = virgl_resource(transfer->resource);
 
    if (trans->base.usage & PIPE_TRANSFER_WRITE) {
       struct virgl_screen *vs = virgl_screen(ctx->screen);
@@ -99,7 +99,7 @@ static void virgl_buffer_transfer_unmap(struct pipe_context *ctx,
       }
 
       vctx->num_transfers++;
-      vs->vws->transfer_put(vs->vws, vbuf->base.hw_res,
+      vs->vws->transfer_put(vs->vws, vbuf->hw_res,
                             &transfer->box, trans->base.stride,
                             trans->l_stride, trans->offset,
                             transfer->level);
@@ -113,7 +113,7 @@ static void virgl_buffer_transfer_flush_region(struct pipe_context *ctx,
                                                struct pipe_transfer *transfer,
                                                const struct pipe_box *box)
 {
-   struct virgl_buffer *vbuf = virgl_buffer(transfer->resource);
+   struct virgl_resource *vbuf = virgl_resource(transfer->resource);
    struct virgl_transfer *trans = virgl_transfer(transfer);
 
    /*
@@ -126,7 +126,7 @@ static void virgl_buffer_transfer_flush_region(struct pipe_context *ctx,
     * We'll end up flushing 25 --> 70.
     */
    util_range_add(&trans->range, box->x, box->x + box->width);
-   vbuf->base.clean = FALSE;
+   vbuf->clean = FALSE;
 }
 
 static const struct u_resource_vtbl virgl_buffer_vtbl =
@@ -141,22 +141,21 @@ static const struct u_resource_vtbl virgl_buffer_vtbl =
 struct pipe_resource *virgl_buffer_create(struct virgl_screen *vs,
                                           const struct pipe_resource *template)
 {
-   struct virgl_buffer *buf;
+   struct virgl_resource *buf;
    uint32_t vbind;
-   buf = CALLOC_STRUCT(virgl_buffer);
-   buf->base.clean = TRUE;
-   buf->base.u.b = *template;
-   buf->base.u.b.screen = &vs->base;
-   buf->base.u.vtbl = &virgl_buffer_vtbl;
-   pipe_reference_init(&buf->base.u.b.reference, 1);
-   virgl_resource_layout(&buf->base.u.b, &buf->metadata);
+   buf = CALLOC_STRUCT(virgl_resource);
+   buf->clean = TRUE;
+   buf->u.b = *template;
+   buf->u.b.screen = &vs->base;
+   buf->u.vtbl = &virgl_buffer_vtbl;
+   pipe_reference_init(&buf->u.b.reference, 1);
+   virgl_resource_layout(&buf->u.b, &buf->metadata);
 
    vbind = pipe_to_virgl_bind(template->bind);
 
-   buf->base.hw_res = vs->vws->resource_create(vs->vws, template->target,
-                                               template->format, vbind,
-                                               template->width0, 1, 1, 1, 0, 0,
-                                               buf->metadata.total_size);
-
-   return &buf->base.u.b;
+   buf->hw_res = vs->vws->resource_create(vs->vws, template->target,
+                                          template->format, vbind,
+                                          template->width0, 1, 1, 1, 0, 0,
+                                          buf->metadata.total_size);
+   return &buf->u.b;
 }
index bafec289cc746f9a0735041683d3413ec493d438..f095920489f39e8cc770f3a3516de5f345b2cd50 100644 (file)
@@ -506,14 +506,13 @@ void virgl_transfer_inline_write(struct pipe_context *ctx,
    struct virgl_context *vctx = virgl_context(ctx);
    struct virgl_screen *vs = virgl_screen(ctx->screen);
    struct virgl_resource *grres = virgl_resource(res);
-   struct virgl_buffer *vbuf = virgl_buffer(res);
 
    grres->clean = FALSE;
 
-   if (virgl_res_needs_flush_wait(vctx, &vbuf->base, usage)) {
+   if (virgl_res_needs_flush_wait(vctx, grres, usage)) {
       ctx->flush(ctx, NULL, 0);
 
-      vs->vws->resource_wait(vs->vws, vbuf->base.hw_res);
+      vs->vws->resource_wait(vs->vws, grres->hw_res);
    }
 
    virgl_encoder_inline_write(vctx, grres, level, usage,
index 6b76bc26d323a68d702499b2aaf9874513c364a8..28b12c317f62e605dd3f601a66c478373e850827 100644 (file)
@@ -46,17 +46,8 @@ struct virgl_resource_metadata
 
 struct virgl_resource {
    struct u_resource u;
-   struct virgl_hw_res *hw_res;
    boolean clean;
-};
-
-struct virgl_buffer {
-   struct virgl_resource base;
-   struct virgl_resource_metadata metadata;
-};
-
-struct virgl_texture {
-   struct virgl_resource base;
+   struct virgl_hw_res *hw_res;
    struct virgl_resource_metadata metadata;
 };
 
@@ -86,16 +77,6 @@ static inline struct virgl_resource *virgl_resource(struct pipe_resource *r)
    return (struct virgl_resource *)r;
 }
 
-static inline struct virgl_buffer *virgl_buffer(struct pipe_resource *r)
-{
-   return (struct virgl_buffer *)r;
-}
-
-static inline struct virgl_texture *virgl_texture(struct pipe_resource *r)
-{
-   return (struct virgl_texture *)r;
-}
-
 static inline struct virgl_transfer *virgl_transfer(struct pipe_transfer *trans)
 {
    return (struct virgl_transfer *)trans;
index 80094fb6ee7c45744e88848d075e8dd34da524d4..d79ae4d3d098cf72af6636c39aae618101b0d6ec 100644 (file)
@@ -103,19 +103,19 @@ static void *virgl_texture_transfer_map(struct pipe_context *ctx,
 {
    struct virgl_context *vctx = virgl_context(ctx);
    struct virgl_screen *vs = virgl_screen(ctx->screen);
-   struct virgl_texture *vtex = virgl_texture(resource);
+   struct virgl_resource *vtex = virgl_resource(resource);
    struct virgl_transfer *trans;
    void *ptr;
    boolean readback = TRUE;
    struct virgl_hw_res *hw_res;
    bool doflushwait;
 
-   doflushwait = virgl_res_needs_flush_wait(vctx, &vtex->base, usage);
+   doflushwait = virgl_res_needs_flush_wait(vctx, vtex, usage);
    if (doflushwait)
       ctx->flush(ctx, NULL, 0);
 
-   trans = virgl_resource_create_transfer(ctx, resource, &vtex->metadata, level,
-                                          usage, box);
+   trans = virgl_resource_create_transfer(ctx, resource, &vtex->metadata,
+                                          level, usage, box);
 
    if (resource->nr_samples > 1) {
       struct pipe_resource tmp_resource;
@@ -128,22 +128,22 @@ static void *virgl_texture_transfer_map(struct pipe_context *ctx,
       ctx->flush(ctx, NULL, 0);
       /* we want to do a resolve blit into the temporary */
       hw_res = trans->resolve_tmp->hw_res;
-      struct virgl_resource_metadata *data = &((struct virgl_texture*)trans->resolve_tmp)->metadata;
+      struct virgl_resource_metadata *data = &trans->resolve_tmp->metadata;
       trans->base.stride = data->stride[level];
       trans->base.layer_stride = data->layer_stride[level];
       trans->offset = 0;
    } else {
-      hw_res = vtex->base.hw_res;
+      hw_res = vtex->hw_res;
       trans->resolve_tmp = NULL;
    }
 
-   readback = virgl_res_needs_readback(vctx, &vtex->base, usage);
+   readback = virgl_res_needs_readback(vctx, vtex, usage);
    if (readback)
       vs->vws->transfer_get(vs->vws, hw_res, box, trans->base.stride,
                             trans->l_stride, trans->offset, level);
 
    if (doflushwait || readback)
-      vs->vws->resource_wait(vs->vws, vtex->base.hw_res);
+      vs->vws->resource_wait(vs->vws, vtex->hw_res);
 
    ptr = vs->vws->resource_map(vs->vws, hw_res);
    if (!ptr) {
@@ -160,14 +160,14 @@ static void virgl_texture_transfer_unmap(struct pipe_context *ctx,
 {
    struct virgl_context *vctx = virgl_context(ctx);
    struct virgl_transfer *trans = virgl_transfer(transfer);
-   struct virgl_texture *vtex = virgl_texture(transfer->resource);
+   struct virgl_resource *vtex = virgl_resource(transfer->resource);
 
    if (trans->base.usage & PIPE_TRANSFER_WRITE) {
       if (!(transfer->usage & PIPE_TRANSFER_FLUSH_EXPLICIT)) {
          struct virgl_screen *vs = virgl_screen(ctx->screen);
-         vtex->base.clean = FALSE;
+         vtex->clean = FALSE;
          vctx->num_transfers++;
-         vs->vws->transfer_put(vs->vws, vtex->base.hw_res,
+         vs->vws->transfer_put(vs->vws, vtex->hw_res,
                                &transfer->box, trans->base.stride,
                                trans->l_stride, trans->offset,
                                transfer->level);
@@ -186,18 +186,19 @@ static boolean virgl_texture_get_handle(struct pipe_screen *screen,
                                          struct winsys_handle *whandle)
 {
    struct virgl_screen *vs = virgl_screen(screen);
-   struct virgl_texture *vtex = virgl_texture(ptex);
+   struct virgl_resource *vtex = virgl_resource(ptex);
 
-   return vs->vws->resource_get_handle(vs->vws, vtex->base.hw_res,
-                                       vtex->metadata.stride[0], whandle);
+   return vs->vws->resource_get_handle(vs->vws, vtex->hw_res,
+                                       vtex->metadata.stride[0],
+                                       whandle);
 }
 
 static void virgl_texture_destroy(struct pipe_screen *screen,
                                   struct pipe_resource *res)
 {
    struct virgl_screen *vs = virgl_screen(screen);
-   struct virgl_texture *vtex = virgl_texture(res);
-   vs->vws->resource_unref(vs->vws, vtex->base.hw_res);
+   struct virgl_resource *vtex = virgl_resource(res);
+   vs->vws->resource_unref(vs->vws, vtex->hw_res);
    FREE(vtex);
 }
 
@@ -215,43 +216,43 @@ virgl_texture_from_handle(struct virgl_screen *vs,
                           const struct pipe_resource *template,
                           struct winsys_handle *whandle)
 {
-   struct virgl_texture *tex = CALLOC_STRUCT(virgl_texture);
-   tex->base.u.b = *template;
-   tex->base.u.b.screen = &vs->base;
-   pipe_reference_init(&tex->base.u.b.reference, 1);
-   tex->base.u.vtbl = &virgl_texture_vtbl;
+   struct virgl_resource *tex = CALLOC_STRUCT(virgl_resource);
+   tex->u.b = *template;
+   tex->u.b.screen = &vs->base;
+   pipe_reference_init(&tex->u.b.reference, 1);
+   tex->u.vtbl = &virgl_texture_vtbl;
 
-   tex->base.hw_res = vs->vws->resource_create_from_handle(vs->vws, whandle);
-   return &tex->base.u.b;
+   tex->hw_res = vs->vws->resource_create_from_handle(vs->vws, whandle);
+   return &tex->u.b;
 }
 
 struct pipe_resource *virgl_texture_create(struct virgl_screen *vs,
                                            const struct pipe_resource *template)
 {
-   struct virgl_texture *tex;
+   struct virgl_resource *tex;
    unsigned vbind;
 
-   tex = CALLOC_STRUCT(virgl_texture);
-   tex->base.clean = TRUE;
-   tex->base.u.b = *template;
-   tex->base.u.b.screen = &vs->base;
-   pipe_reference_init(&tex->base.u.b.reference, 1);
-   tex->base.u.vtbl = &virgl_texture_vtbl;
-   virgl_resource_layout(&tex->base.u.b, &tex->metadata);
+   tex = CALLOC_STRUCT(virgl_resource);
+   tex->clean = TRUE;
+   tex->u.b = *template;
+   tex->u.b.screen = &vs->base;
+   pipe_reference_init(&tex->u.b.reference, 1);
+   tex->u.vtbl = &virgl_texture_vtbl;
+   virgl_resource_layout(&tex->u.b, &tex->metadata);
 
    vbind = pipe_to_virgl_bind(template->bind);
-   tex->base.hw_res = vs->vws->resource_create(vs->vws, template->target,
-                                               template->format, vbind,
-                                               template->width0,
-                                               template->height0,
-                                               template->depth0,
-                                               template->array_size,
-                                               template->last_level,
-                                               template->nr_samples,
-                                               tex->metadata.total_size);
-   if (!tex->base.hw_res) {
+   tex->hw_res = vs->vws->resource_create(vs->vws, template->target,
+                                          template->format, vbind,
+                                          template->width0,
+                                          template->height0,
+                                          template->depth0,
+                                          template->array_size,
+                                          template->last_level,
+                                          template->nr_samples,
+                                          tex->metadata.total_size);
+   if (!tex->hw_res) {
       FREE(tex);
       return NULL;
    }
-   return &tex->base.u.b;
+   return &tex->u.b;
 }