freedreno/ir3: add ctx->mem_to_mem()
[mesa.git] / src / gallium / drivers / virgl / virgl_context.c
index 615bb507afb5ae4334363ae2b0d357c8606f71ac..f1b6ef45018e1e07c97d92c4f47f6a3a1d186b60 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"
@@ -99,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];
@@ -123,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 = 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 = virgl_resource(vctx->index_buffer.buffer);
+   res = virgl_resource(ib->buffer);
    if (res)
       vws->emit_res(vws, vctx->cbuf, res->hw_res, FALSE);
 }
@@ -153,7 +155,7 @@ static void virgl_attach_res_so_targets(struct virgl_context *vctx)
 }
 
 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;
@@ -172,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 */
@@ -182,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);
 }
@@ -403,29 +404,16 @@ 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,
+                                     enum pipe_shader_type shader, uint index,
                                      const struct pipe_constant_buffer *buf)
 {
    struct virgl_context *vctx = virgl_context(ctx);
@@ -589,19 +577,23 @@ 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, 256,
@@ -614,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);
@@ -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)
 {
@@ -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,7 +891,6 @@ 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.create_vs_state = virgl_create_vs_state;
@@ -942,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,
-                                     PIPE_BIND_INDEX_BUFFER, PIPE_USAGE_STREAM);
+                                     PIPE_BIND_INDEX_BUFFER, PIPE_USAGE_STREAM, 0);
    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);