gallium: remove pipe_index_buffer and set_index_buffer
[mesa.git] / src / gallium / drivers / virgl / virgl_context.c
index f222e532bf05f78270ee645006c53c46867ff249..232d2951c0b324163d9589bc1d177b4b02e641cf 100644 (file)
 #include "util/u_inlines.h"
 #include "util/u_memory.h"
 #include "util/u_format.h"
+#include "util/u_prim.h"
 #include "util/u_transfer.h"
 #include "util/u_helpers.h"
-#include "util/u_slab.h"
+#include "util/slab.h"
 #include "util/u_upload_mgr.h"
 #include "util/u_blitter.h"
 #include "tgsi/tgsi_text.h"
+#include "indices/u_primconvert.h"
 
 #include "pipebuffer/pb_buffer.h"
-#include "state_tracker/graw.h"
-#include "state_tracker/drm_driver.h"
 
 #include "virgl_encode.h"
-
 #include "virgl_context.h"
-
+#include "virgl_protocol.h"
 #include "virgl_resource.h"
 #include "virgl_screen.h"
-#include "state_tracker/sw_winsys.h"
- struct pipe_screen encscreen;
 
 static uint32_t next_handle;
 uint32_t virgl_object_assign_handle(void)
@@ -88,14 +85,14 @@ static void virgl_attach_res_framebuffer(struct virgl_context *vctx)
 
    surf = vctx->framebuffer.zsbuf;
    if (surf) {
-      res = (struct virgl_resource *)surf->texture;
+      res = virgl_resource(surf->texture);
       if (res)
          vws->emit_res(vws, vctx->cbuf, res->hw_res, FALSE);
    }
    for (i = 0; i < vctx->framebuffer.nr_cbufs; i++) {
       surf = vctx->framebuffer.cbufs[i];
       if (surf) {
-         res = (struct virgl_resource *)surf->texture;
+         res = virgl_resource(surf->texture);
          if (res)
             vws->emit_res(vws, vctx->cbuf, res->hw_res, FALSE);
       }
@@ -103,7 +100,7 @@ static void virgl_attach_res_framebuffer(struct virgl_context *vctx)
 }
 
 static void virgl_attach_res_sampler_views(struct virgl_context *vctx,
-                                           unsigned shader_type)
+                                           enum pipe_shader_type shader_type)
 {
    struct virgl_winsys *vws = virgl_screen(vctx->base.screen)->vws;
    struct virgl_textures_info *tinfo = &vctx->samplers[shader_type];
@@ -114,7 +111,7 @@ static void virgl_attach_res_sampler_views(struct virgl_context *vctx,
       i = u_bit_scan(&remaining_mask);
       assert(tinfo->views[i]);
 
-      res = (struct virgl_resource *)tinfo->views[i]->base.texture;
+      res = virgl_resource(tinfo->views[i]->base.texture);
       if (res)
          vws->emit_res(vws, vctx->cbuf, res->hw_res, FALSE);
    }
@@ -127,18 +124,19 @@ static void virgl_attach_res_vertex_buffers(struct virgl_context *vctx)
    unsigned i;
 
    for (i = 0; i < vctx->num_vertex_buffers; i++) {
-      res = (struct virgl_resource *)vctx->vertex_buffer[i].buffer;
+      res = virgl_resource(vctx->vertex_buffer[i].buffer.resource);
       if (res)
          vws->emit_res(vws, vctx->cbuf, res->hw_res, FALSE);
    }
 }
 
-static void virgl_attach_res_index_buffer(struct virgl_context *vctx)
+static void virgl_attach_res_index_buffer(struct virgl_context *vctx,
+                                         struct virgl_indexbuf *ib)
 {
    struct virgl_winsys *vws = virgl_screen(vctx->base.screen)->vws;
    struct virgl_resource *res;
 
-   res = (struct virgl_resource *)vctx->index_buffer.buffer;
+   res = virgl_resource(ib->buffer);
    if (res)
       vws->emit_res(vws, vctx->cbuf, res->hw_res, FALSE);
 }
@@ -150,20 +148,20 @@ static void virgl_attach_res_so_targets(struct virgl_context *vctx)
    unsigned i;
 
    for (i = 0; i < vctx->num_so_targets; i++) {
-      res = (struct virgl_resource *)vctx->so_targets[i].base.buffer;
+      res = virgl_resource(vctx->so_targets[i].base.buffer);
       if (res)
          vws->emit_res(vws, vctx->cbuf, res->hw_res, FALSE);
    }
 }
 
 static void virgl_attach_res_uniform_buffers(struct virgl_context *vctx,
-                                             unsigned shader_type)
+                                             enum pipe_shader_type shader_type)
 {
    struct virgl_winsys *vws = virgl_screen(vctx->base.screen)->vws;
    struct virgl_resource *res;
    unsigned i;
    for (i = 0; i < PIPE_MAX_CONSTANT_BUFFERS; i++) {
-      res = (struct virgl_resource *)vctx->ubos[shader_type][i];
+      res = virgl_resource(vctx->ubos[shader_type][i]);
       if (res) {
          vws->emit_res(vws, vctx->cbuf, res->hw_res, FALSE);
       }
@@ -176,7 +174,7 @@ static void virgl_attach_res_uniform_buffers(struct virgl_context *vctx,
  */
 static void virgl_reemit_res(struct virgl_context *vctx)
 {
-   unsigned shader_type;
+   enum pipe_shader_type shader_type;
 
    /* reattach any flushed resources */
    /* framebuffer, sampler views, vertex/index/uniform/stream buffers */
@@ -186,7 +184,6 @@ static void virgl_reemit_res(struct virgl_context *vctx)
       virgl_attach_res_sampler_views(vctx, shader_type);
       virgl_attach_res_uniform_buffers(vctx, shader_type);
    }
-   virgl_attach_res_index_buffer(vctx);
    virgl_attach_res_vertex_buffers(vctx);
    virgl_attach_res_so_targets(vctx);
 }
@@ -201,7 +198,7 @@ static struct pipe_surface *virgl_create_surface(struct pipe_context *ctx,
    uint32_t handle;
 
    surf = CALLOC_STRUCT(virgl_surface);
-   if (surf == NULL)
+   if (!surf)
       return NULL;
 
    res->clean = FALSE;
@@ -231,7 +228,7 @@ static void virgl_surface_destroy(struct pipe_context *ctx,
                                  struct pipe_surface *psurf)
 {
    struct virgl_context *vctx = virgl_context(ctx);
-   struct virgl_surface *surf = (struct virgl_surface *)psurf;
+   struct virgl_surface *surf = virgl_surface(psurf);
 
    pipe_resource_reference(&surf->base.texture, NULL);
    virgl_encode_delete_object(vctx, surf->handle, VIRGL_OBJECT_SURFACE);
@@ -407,36 +404,23 @@ static void virgl_set_blend_color(struct pipe_context *ctx,
    virgl_encoder_set_blend_color(vctx, color);
 }
 
-static void virgl_set_index_buffer(struct pipe_context *ctx,
-                                  const struct pipe_index_buffer *ib)
-{
-   struct virgl_context *vctx = virgl_context(ctx);
-
-   if (ib) {
-      pipe_resource_reference(&vctx->index_buffer.buffer, ib->buffer);
-      memcpy(&vctx->index_buffer, ib, sizeof(*ib));
-   } else {
-      pipe_resource_reference(&vctx->index_buffer.buffer, NULL);
-   }
-}
-
 static void virgl_hw_set_index_buffer(struct pipe_context *ctx,
-                                     struct pipe_index_buffer *ib)
+                                     struct virgl_indexbuf *ib)
 {
    struct virgl_context *vctx = virgl_context(ctx);
    virgl_encoder_set_index_buffer(vctx, ib);
-   virgl_attach_res_index_buffer(vctx);
+   virgl_attach_res_index_buffer(vctx, ib);
 }
 
 static void virgl_set_constant_buffer(struct pipe_context *ctx,
-                                     uint shader, uint index,
-                                     struct pipe_constant_buffer *buf)
+                                     enum pipe_shader_type shader, uint index,
+                                     const struct pipe_constant_buffer *buf)
 {
    struct virgl_context *vctx = virgl_context(ctx);
 
    if (buf) {
       if (!buf->user_buffer){
-         struct virgl_resource *res = (struct virgl_resource *)buf->buffer;
+         struct virgl_resource *res = virgl_resource(buf->buffer);
          virgl_encoder_set_uniform_buffer(vctx, shader, index, buf->buffer_offset,
                                           buf->buffer_size, res);
          pipe_resource_reference(&vctx->ubos[shader][index], buf->buffer);
@@ -461,7 +445,7 @@ 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 = (struct virgl_resource *)res;
+   struct virgl_resource *grres = virgl_resource(res);
    struct virgl_buffer *vbuf = virgl_buffer(res);
 
    grres->clean = FALSE;
@@ -593,22 +577,26 @@ static void virgl_draw_vbo(struct pipe_context *ctx,
 {
    struct virgl_context *vctx = virgl_context(ctx);
    struct virgl_screen *rs = virgl_screen(ctx->screen);
-   struct pipe_index_buffer ib = {};
+   struct virgl_indexbuf ib = {};
    struct pipe_draw_info info = *dinfo;
 
+   if (!dinfo->count_from_stream_output && !dinfo->indirect &&
+       !dinfo->primitive_restart &&
+       !u_trim_pipe_prim(dinfo->mode, (unsigned*)&dinfo->count))
+      return;
+
    if (!(rs->caps.caps.v1.prim_mask & (1 << dinfo->mode))) {
-      util_primconvert_save_index_buffer(vctx->primconvert, &vctx->index_buffer);
       util_primconvert_draw_vbo(vctx->primconvert, dinfo);
       return;
    }
-   if (info.indexed) {
-           pipe_resource_reference(&ib.buffer, vctx->index_buffer.buffer);
-           ib.user_buffer = vctx->index_buffer.user_buffer;
-           ib.index_size = vctx->index_buffer.index_size;
-           ib.offset = vctx->index_buffer.offset + info.start * ib.index_size;
+   if (info.index_size) {
+           pipe_resource_reference(&ib.buffer, info.has_user_indices ? NULL : info.index.resource);
+           ib.user_buffer = info.has_user_indices ? info.index.user : NULL;
+           ib.index_size = dinfo->index_size;
+           ib.offset = info.start * ib.index_size;
 
            if (ib.user_buffer) {
-                   u_upload_data(vctx->uploader, 0, info.count * ib.index_size,
+                   u_upload_data(vctx->uploader, 0, info.count * ib.index_size, 256,
                                  ib.user_buffer, &ib.offset, &ib.buffer);
                    ib.user_buffer = NULL;
            }
@@ -618,7 +606,7 @@ static void virgl_draw_vbo(struct pipe_context *ctx,
 
    vctx->num_draws++;
    virgl_hw_set_vertex_buffers(ctx);
-   if (info.indexed)
+   if (info.index_size)
       virgl_hw_set_index_buffer(ctx, &ib);
 
    virgl_encoder_draw_vbo(vctx, &info);
@@ -668,14 +656,18 @@ static struct pipe_sampler_view *virgl_create_sampler_view(struct pipe_context *
                                       const struct pipe_sampler_view *state)
 {
    struct virgl_context *vctx = virgl_context(ctx);
-   struct virgl_sampler_view *grview = CALLOC_STRUCT(virgl_sampler_view);
+   struct virgl_sampler_view *grview;
    uint32_t handle;
    struct virgl_resource *res;
 
-   if (state == NULL)
+   if (!state)
       return NULL;
 
-   res = (struct virgl_resource *)texture;
+   grview = CALLOC_STRUCT(virgl_sampler_view);
+   if (!grview)
+      return NULL;
+
+   res = virgl_resource(texture);
    handle = virgl_object_assign_handle();
    virgl_encode_sampler_view(vctx, handle, res, state);
 
@@ -690,7 +682,7 @@ static struct pipe_sampler_view *virgl_create_sampler_view(struct pipe_context *
 }
 
 static void virgl_set_sampler_views(struct pipe_context *ctx,
-                                   unsigned shader_type,
+                                   enum pipe_shader_type shader_type,
                                    unsigned start_slot,
                                    unsigned num_views,
                                    struct pipe_sampler_view **views)
@@ -765,7 +757,8 @@ static void virgl_delete_sampler_state(struct pipe_context *ctx,
 }
 
 static void virgl_bind_sampler_states(struct pipe_context *ctx,
-                                     unsigned shader, unsigned start_slot,
+                                     enum pipe_shader_type shader,
+                                     unsigned start_slot,
                                      unsigned num_samplers,
                                      void **samplers)
 {
@@ -817,8 +810,8 @@ static void virgl_resource_copy_region(struct pipe_context *ctx,
                                       const struct pipe_box *src_box)
 {
    struct virgl_context *vctx = virgl_context(ctx);
-   struct virgl_resource *dres = (struct virgl_resource *)dst;
-   struct virgl_resource *sres = (struct virgl_resource *)src;
+   struct virgl_resource *dres = virgl_resource(dst);
+   struct virgl_resource *sres = virgl_resource(src);
 
    dres->clean = FALSE;
    virgl_encode_resource_copy_region(vctx, dres,
@@ -837,8 +830,8 @@ static void virgl_blit(struct pipe_context *ctx,
                       const struct pipe_blit_info *blit)
 {
    struct virgl_context *vctx = virgl_context(ctx);
-   struct virgl_resource *dres = (struct virgl_resource *)blit->dst.resource;
-   struct virgl_resource *sres = (struct virgl_resource *)blit->src.resource;
+   struct virgl_resource *dres = virgl_resource(blit->dst.resource);
+   struct virgl_resource *sres = virgl_resource(blit->src.resource);
 
    dres->clean = FALSE;
    virgl_encode_blit(vctx, dres, sres,
@@ -861,7 +854,7 @@ virgl_context_destroy( struct pipe_context *ctx )
       u_upload_destroy(vctx->uploader);
    util_primconvert_destroy(vctx->primconvert);
 
-   util_slab_destroy(&vctx->texture_transfer_pool);
+   slab_destroy_child(&vctx->texture_transfer_pool);
    FREE(vctx);
 }
 
@@ -898,9 +891,7 @@ struct pipe_context *virgl_context_create(struct pipe_screen *pscreen,
    vctx->base.bind_vertex_elements_state = virgl_bind_vertex_elements_state;
    vctx->base.delete_vertex_elements_state = virgl_delete_vertex_elements_state;
    vctx->base.set_vertex_buffers = virgl_set_vertex_buffers;
-   vctx->base.set_index_buffer = virgl_set_index_buffer;
    vctx->base.set_constant_buffer = virgl_set_constant_buffer;
-   vctx->base.transfer_inline_write = virgl_transfer_inline_write;
 
    vctx->base.create_vs_state = virgl_create_vs_state;
    vctx->base.create_gs_state = virgl_create_gs_state;
@@ -943,14 +934,15 @@ struct pipe_context *virgl_context_create(struct pipe_screen *pscreen,
    virgl_init_so_functions(vctx);
 
    list_inithead(&vctx->to_flush_bufs);
-   util_slab_create(&vctx->texture_transfer_pool, sizeof(struct virgl_transfer),
-                    16, UTIL_SLAB_SINGLETHREADED);
+   slab_create_child(&vctx->texture_transfer_pool, &rs->texture_transfer_pool);
 
    vctx->primconvert = util_primconvert_create(&vctx->base, rs->caps.caps.v1.prim_mask);
-   vctx->uploader = u_upload_create(&vctx->base, 1024 * 1024, 256,
-                                     PIPE_BIND_INDEX_BUFFER);
+   vctx->uploader = u_upload_create(&vctx->base, 1024 * 1024,
+                                     PIPE_BIND_INDEX_BUFFER, PIPE_USAGE_STREAM);
    if (!vctx->uploader)
            goto fail;
+   vctx->base.stream_uploader = vctx->uploader;
+   vctx->base.const_uploader = vctx->uploader;
 
    vctx->hw_sub_ctx_id = rs->sub_ctx_id++;
    virgl_encoder_create_sub_ctx(vctx, vctx->hw_sub_ctx_id);