gallium: Add PIPE_CAP_BUFFER_SAMPLER_VIEW_RGBA_ONLY
[mesa.git] / src / gallium / drivers / virgl / virgl_context.c
index 31464595dad783d9ca682347920978ce95fe4f28..c322503d81630141c9b13ed1de8f98e83a14c768 100644 (file)
 #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);
       }
@@ -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,7 +124,7 @@ 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);
       if (res)
          vws->emit_res(vws, vctx->cbuf, res->hw_res, FALSE);
    }
@@ -138,7 +135,7 @@ static void virgl_attach_res_index_buffer(struct virgl_context *vctx)
    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(vctx->index_buffer.buffer);
    if (res)
       vws->emit_res(vws, vctx->cbuf, res->hw_res, FALSE);
 }
@@ -150,7 +147,7 @@ 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);
    }
@@ -163,7 +160,7 @@ static void virgl_attach_res_uniform_buffers(struct virgl_context *vctx,
    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);
       }
@@ -195,13 +192,13 @@ static struct pipe_surface *virgl_create_surface(struct pipe_context *ctx,
                                                 struct pipe_resource *resource,
                                                 const struct pipe_surface *templ)
 {
-   struct virgl_context *vctx = (struct virgl_context *)ctx;
+   struct virgl_context *vctx = virgl_context(ctx);
    struct virgl_surface *surf;
    struct virgl_resource *res = virgl_resource(resource);
    uint32_t handle;
 
    surf = CALLOC_STRUCT(virgl_surface);
-   if (surf == NULL)
+   if (!surf)
       return NULL;
 
    res->clean = FALSE;
@@ -230,8 +227,8 @@ static struct pipe_surface *virgl_create_surface(struct pipe_context *ctx,
 static void virgl_surface_destroy(struct pipe_context *ctx,
                                  struct pipe_surface *psurf)
 {
-   struct virgl_context *vctx = (struct virgl_context *)ctx;
-   struct virgl_surface *surf = (struct virgl_surface *)psurf;
+   struct virgl_context *vctx = virgl_context(ctx);
+   struct virgl_surface *surf = virgl_surface(psurf);
 
    pipe_resource_reference(&surf->base.texture, NULL);
    virgl_encode_delete_object(vctx, surf->handle, VIRGL_OBJECT_SURFACE);
@@ -241,7 +238,7 @@ static void virgl_surface_destroy(struct pipe_context *ctx,
 static void *virgl_create_blend_state(struct pipe_context *ctx,
                                               const struct pipe_blend_state *blend_state)
 {
-   struct virgl_context *vctx = (struct virgl_context *)ctx;
+   struct virgl_context *vctx = virgl_context(ctx);
    uint32_t handle;
    handle = virgl_object_assign_handle();
 
@@ -253,7 +250,7 @@ static void *virgl_create_blend_state(struct pipe_context *ctx,
 static void virgl_bind_blend_state(struct pipe_context *ctx,
                                            void *blend_state)
 {
-   struct virgl_context *vctx = (struct virgl_context *)ctx;
+   struct virgl_context *vctx = virgl_context(ctx);
    uint32_t handle = (unsigned long)blend_state;
    virgl_encode_bind_object(vctx, handle, VIRGL_OBJECT_BLEND);
 }
@@ -261,7 +258,7 @@ static void virgl_bind_blend_state(struct pipe_context *ctx,
 static void virgl_delete_blend_state(struct pipe_context *ctx,
                                      void *blend_state)
 {
-   struct virgl_context *vctx = (struct virgl_context *)ctx;
+   struct virgl_context *vctx = virgl_context(ctx);
    uint32_t handle = (unsigned long)blend_state;
    virgl_encode_delete_object(vctx, handle, VIRGL_OBJECT_BLEND);
 }
@@ -269,7 +266,7 @@ static void virgl_delete_blend_state(struct pipe_context *ctx,
 static void *virgl_create_depth_stencil_alpha_state(struct pipe_context *ctx,
                                                    const struct pipe_depth_stencil_alpha_state *blend_state)
 {
-   struct virgl_context *vctx = (struct virgl_context *)ctx;
+   struct virgl_context *vctx = virgl_context(ctx);
    uint32_t handle;
    handle = virgl_object_assign_handle();
 
@@ -280,7 +277,7 @@ static void *virgl_create_depth_stencil_alpha_state(struct pipe_context *ctx,
 static void virgl_bind_depth_stencil_alpha_state(struct pipe_context *ctx,
                                                 void *blend_state)
 {
-   struct virgl_context *vctx = (struct virgl_context *)ctx;
+   struct virgl_context *vctx = virgl_context(ctx);
    uint32_t handle = (unsigned long)blend_state;
    virgl_encode_bind_object(vctx, handle, VIRGL_OBJECT_DSA);
 }
@@ -288,7 +285,7 @@ static void virgl_bind_depth_stencil_alpha_state(struct pipe_context *ctx,
 static void virgl_delete_depth_stencil_alpha_state(struct pipe_context *ctx,
                                                   void *dsa_state)
 {
-   struct virgl_context *vctx = (struct virgl_context *)ctx;
+   struct virgl_context *vctx = virgl_context(ctx);
    uint32_t handle = (unsigned long)dsa_state;
    virgl_encode_delete_object(vctx, handle, VIRGL_OBJECT_DSA);
 }
@@ -296,7 +293,7 @@ static void virgl_delete_depth_stencil_alpha_state(struct pipe_context *ctx,
 static void *virgl_create_rasterizer_state(struct pipe_context *ctx,
                                                    const struct pipe_rasterizer_state *rs_state)
 {
-   struct virgl_context *vctx = (struct virgl_context *)ctx;
+   struct virgl_context *vctx = virgl_context(ctx);
    uint32_t handle;
    handle = virgl_object_assign_handle();
 
@@ -307,7 +304,7 @@ static void *virgl_create_rasterizer_state(struct pipe_context *ctx,
 static void virgl_bind_rasterizer_state(struct pipe_context *ctx,
                                                 void *rs_state)
 {
-   struct virgl_context *vctx = (struct virgl_context *)ctx;
+   struct virgl_context *vctx = virgl_context(ctx);
    uint32_t handle = (unsigned long)rs_state;
 
    virgl_encode_bind_object(vctx, handle, VIRGL_OBJECT_RASTERIZER);
@@ -316,7 +313,7 @@ static void virgl_bind_rasterizer_state(struct pipe_context *ctx,
 static void virgl_delete_rasterizer_state(struct pipe_context *ctx,
                                          void *rs_state)
 {
-   struct virgl_context *vctx = (struct virgl_context *)ctx;
+   struct virgl_context *vctx = virgl_context(ctx);
    uint32_t handle = (unsigned long)rs_state;
    virgl_encode_delete_object(vctx, handle, VIRGL_OBJECT_RASTERIZER);
 }
@@ -324,7 +321,7 @@ static void virgl_delete_rasterizer_state(struct pipe_context *ctx,
 static void virgl_set_framebuffer_state(struct pipe_context *ctx,
                                                 const struct pipe_framebuffer_state *state)
 {
-   struct virgl_context *vctx = (struct virgl_context *)ctx;
+   struct virgl_context *vctx = virgl_context(ctx);
 
    vctx->framebuffer = *state;
    virgl_encoder_set_framebuffer_state(vctx, state);
@@ -336,7 +333,7 @@ static void virgl_set_viewport_states(struct pipe_context *ctx,
                                      unsigned num_viewports,
                                      const struct pipe_viewport_state *state)
 {
-   struct virgl_context *vctx = (struct virgl_context *)ctx;
+   struct virgl_context *vctx = virgl_context(ctx);
    virgl_encoder_set_viewport_states(vctx, start_slot, num_viewports, state);
 }
 
@@ -344,7 +341,7 @@ static void *virgl_create_vertex_elements_state(struct pipe_context *ctx,
                                                         unsigned num_elements,
                                                         const struct pipe_vertex_element *elements)
 {
-   struct virgl_context *vctx = (struct virgl_context *)ctx;
+   struct virgl_context *vctx = virgl_context(ctx);
    uint32_t handle = virgl_object_assign_handle();
    virgl_encoder_create_vertex_elements(vctx, handle,
                                        num_elements, elements);
@@ -355,7 +352,7 @@ static void *virgl_create_vertex_elements_state(struct pipe_context *ctx,
 static void virgl_delete_vertex_elements_state(struct pipe_context *ctx,
                                               void *ve)
 {
-   struct virgl_context *vctx = (struct virgl_context *)ctx;
+   struct virgl_context *vctx = virgl_context(ctx);
    uint32_t handle = (unsigned long)ve;
 
    virgl_encode_delete_object(vctx, handle, VIRGL_OBJECT_VERTEX_ELEMENTS);
@@ -364,7 +361,7 @@ static void virgl_delete_vertex_elements_state(struct pipe_context *ctx,
 static void virgl_bind_vertex_elements_state(struct pipe_context *ctx,
                                                      void *ve)
 {
-   struct virgl_context *vctx = (struct virgl_context *)ctx;
+   struct virgl_context *vctx = virgl_context(ctx);
    uint32_t handle = (unsigned long)ve;
    virgl_encode_bind_object(vctx, handle, VIRGL_OBJECT_VERTEX_ELEMENTS);
 }
@@ -374,7 +371,7 @@ static void virgl_set_vertex_buffers(struct pipe_context *ctx,
                                     unsigned num_buffers,
                                     const struct pipe_vertex_buffer *buffers)
 {
-   struct virgl_context *vctx = (struct virgl_context *)ctx;
+   struct virgl_context *vctx = virgl_context(ctx);
 
    util_set_vertex_buffers_count(vctx->vertex_buffer,
                                  &vctx->num_vertex_buffers,
@@ -385,7 +382,7 @@ static void virgl_set_vertex_buffers(struct pipe_context *ctx,
 
 static void virgl_hw_set_vertex_buffers(struct pipe_context *ctx)
 {
-   struct virgl_context *vctx = (struct virgl_context *)ctx;
+   struct virgl_context *vctx = virgl_context(ctx);
 
    if (vctx->vertex_array_dirty) {
       virgl_encoder_set_vertex_buffers(vctx, vctx->num_vertex_buffers, vctx->vertex_buffer);
@@ -396,21 +393,21 @@ static void virgl_hw_set_vertex_buffers(struct pipe_context *ctx)
 static void virgl_set_stencil_ref(struct pipe_context *ctx,
                                  const struct pipe_stencil_ref *ref)
 {
-   struct virgl_context *vctx = (struct virgl_context *)ctx;
+   struct virgl_context *vctx = virgl_context(ctx);
    virgl_encoder_set_stencil_ref(vctx, ref);
 }
 
 static void virgl_set_blend_color(struct pipe_context *ctx,
                                  const struct pipe_blend_color *color)
 {
-   struct virgl_context *vctx = (struct virgl_context *)ctx;
+   struct virgl_context *vctx = virgl_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 = (struct virgl_context *)ctx;
+   struct virgl_context *vctx = virgl_context(ctx);
 
    if (ib) {
       pipe_resource_reference(&vctx->index_buffer.buffer, ib->buffer);
@@ -423,7 +420,7 @@ static void virgl_set_index_buffer(struct pipe_context *ctx,
 static void virgl_hw_set_index_buffer(struct pipe_context *ctx,
                                      struct pipe_index_buffer *ib)
 {
-   struct virgl_context *vctx = (struct virgl_context *)ctx;
+   struct virgl_context *vctx = virgl_context(ctx);
    virgl_encoder_set_index_buffer(vctx, ib);
    virgl_attach_res_index_buffer(vctx);
 }
@@ -432,11 +429,11 @@ static void virgl_set_constant_buffer(struct pipe_context *ctx,
                                      uint shader, uint index,
                                      struct pipe_constant_buffer *buf)
 {
-   struct virgl_context *vctx = (struct virgl_context *)ctx;
+   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);
@@ -459,9 +456,9 @@ void virgl_transfer_inline_write(struct pipe_context *ctx,
                                 unsigned stride,
                                 unsigned layer_stride)
 {
-   struct virgl_context *vctx = (struct virgl_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;
@@ -480,7 +477,7 @@ static void *virgl_shader_encoder(struct pipe_context *ctx,
                                   const struct pipe_shader_state *shader,
                                   unsigned type)
 {
-   struct virgl_context *vctx = (struct virgl_context *)ctx;
+   struct virgl_context *vctx = virgl_context(ctx);
    uint32_t handle;
    struct tgsi_token *new_tokens;
    int ret;
@@ -525,7 +522,7 @@ virgl_delete_fs_state(struct pipe_context *ctx,
                      void *fs)
 {
    uint32_t handle = (unsigned long)fs;
-   struct virgl_context *vctx = (struct virgl_context *)ctx;
+   struct virgl_context *vctx = virgl_context(ctx);
 
    virgl_encode_delete_object(vctx, handle, VIRGL_OBJECT_SHADER);
 }
@@ -535,7 +532,7 @@ virgl_delete_gs_state(struct pipe_context *ctx,
                      void *gs)
 {
    uint32_t handle = (unsigned long)gs;
-   struct virgl_context *vctx = (struct virgl_context *)ctx;
+   struct virgl_context *vctx = virgl_context(ctx);
 
    virgl_encode_delete_object(vctx, handle, VIRGL_OBJECT_SHADER);
 }
@@ -545,7 +542,7 @@ virgl_delete_vs_state(struct pipe_context *ctx,
                      void *vs)
 {
    uint32_t handle = (unsigned long)vs;
-   struct virgl_context *vctx = (struct virgl_context *)ctx;
+   struct virgl_context *vctx = virgl_context(ctx);
 
    virgl_encode_delete_object(vctx, handle, VIRGL_OBJECT_SHADER);
 }
@@ -554,7 +551,7 @@ static void virgl_bind_vs_state(struct pipe_context *ctx,
                                         void *vss)
 {
    uint32_t handle = (unsigned long)vss;
-   struct virgl_context *vctx = (struct virgl_context *)ctx;
+   struct virgl_context *vctx = virgl_context(ctx);
 
    virgl_encode_bind_shader(vctx, handle, PIPE_SHADER_VERTEX);
 }
@@ -563,7 +560,7 @@ static void virgl_bind_gs_state(struct pipe_context *ctx,
                                void *vss)
 {
    uint32_t handle = (unsigned long)vss;
-   struct virgl_context *vctx = (struct virgl_context *)ctx;
+   struct virgl_context *vctx = virgl_context(ctx);
 
    virgl_encode_bind_shader(vctx, handle, PIPE_SHADER_GEOMETRY);
 }
@@ -573,7 +570,7 @@ static void virgl_bind_fs_state(struct pipe_context *ctx,
                                         void *vss)
 {
    uint32_t handle = (unsigned long)vss;
-   struct virgl_context *vctx = (struct virgl_context *)ctx;
+   struct virgl_context *vctx = virgl_context(ctx);
 
    virgl_encode_bind_shader(vctx, handle, PIPE_SHADER_FRAGMENT);
 }
@@ -583,7 +580,7 @@ static void virgl_clear(struct pipe_context *ctx,
                                 const union pipe_color_union *color,
                                 double depth, unsigned stencil)
 {
-   struct virgl_context *vctx = (struct virgl_context *)ctx;
+   struct virgl_context *vctx = virgl_context(ctx);
 
    virgl_encode_clear(vctx, buffers, color, depth, stencil);
 }
@@ -591,7 +588,7 @@ static void virgl_clear(struct pipe_context *ctx,
 static void virgl_draw_vbo(struct pipe_context *ctx,
                                    const struct pipe_draw_info *dinfo)
 {
-   struct virgl_context *vctx = (struct virgl_context *)ctx;
+   struct virgl_context *vctx = virgl_context(ctx);
    struct virgl_screen *rs = virgl_screen(ctx->screen);
    struct pipe_index_buffer ib = {};
    struct pipe_draw_info info = *dinfo;
@@ -608,7 +605,7 @@ static void virgl_draw_vbo(struct pipe_context *ctx,
            ib.offset = vctx->index_buffer.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;
            }
@@ -645,7 +642,7 @@ static void virgl_flush_from_st(struct pipe_context *ctx,
                                struct pipe_fence_handle **fence,
                                enum pipe_flush_flags flags)
 {
-   struct virgl_context *vctx = (struct virgl_context *)ctx;
+   struct virgl_context *vctx = virgl_context(ctx);
    struct virgl_screen *rs = virgl_screen(ctx->screen);
    struct virgl_buffer *buf, *tmp;
 
@@ -667,15 +664,19 @@ static struct pipe_sampler_view *virgl_create_sampler_view(struct pipe_context *
                                       struct pipe_resource *texture,
                                       const struct pipe_sampler_view *state)
 {
-   struct virgl_context *vctx = (struct virgl_context *)ctx;
-   struct virgl_sampler_view *grview = CALLOC_STRUCT(virgl_sampler_view);
+   struct virgl_context *vctx = virgl_context(ctx);
+   struct virgl_sampler_view *grview;
    uint32_t handle;
    struct virgl_resource *res;
 
-   if (state == NULL)
+   if (!state)
+      return NULL;
+
+   grview = CALLOC_STRUCT(virgl_sampler_view);
+   if (!grview)
       return NULL;
 
-   res = (struct virgl_resource *)texture;
+   res = virgl_resource(texture);
    handle = virgl_object_assign_handle();
    virgl_encode_sampler_view(vctx, handle, res, state);
 
@@ -695,7 +696,7 @@ static void virgl_set_sampler_views(struct pipe_context *ctx,
                                    unsigned num_views,
                                    struct pipe_sampler_view **views)
 {
-   struct virgl_context *vctx = (struct virgl_context *)ctx;
+   struct virgl_context *vctx = virgl_context(ctx);
    int i;
    uint32_t disable_mask = ~((1ull << num_views) - 1);
    struct virgl_textures_info *tinfo = &vctx->samplers[shader_type];
@@ -712,7 +713,7 @@ static void virgl_set_sampler_views(struct pipe_context *ctx,
    }
 
    for (i = 0; i < num_views; i++) {
-      struct virgl_sampler_view *grview = (struct virgl_sampler_view *)views[i];
+      struct virgl_sampler_view *grview = virgl_sampler_view(views[i]);
 
       if (views[i] == (struct pipe_sampler_view *)tinfo->views[i])
          continue;
@@ -735,8 +736,8 @@ static void virgl_set_sampler_views(struct pipe_context *ctx,
 static void virgl_destroy_sampler_view(struct pipe_context *ctx,
                                  struct pipe_sampler_view *view)
 {
-   struct virgl_context *vctx = (struct virgl_context *)ctx;
-   struct virgl_sampler_view *grview = (struct virgl_sampler_view *)view;
+   struct virgl_context *vctx = virgl_context(ctx);
+   struct virgl_sampler_view *grview = virgl_sampler_view(view);
 
    virgl_encode_delete_object(vctx, grview->handle, VIRGL_OBJECT_SAMPLER_VIEW);
    pipe_resource_reference(&view->texture, NULL);
@@ -746,7 +747,7 @@ static void virgl_destroy_sampler_view(struct pipe_context *ctx,
 static void *virgl_create_sampler_state(struct pipe_context *ctx,
                                         const struct pipe_sampler_state *state)
 {
-   struct virgl_context *vctx = (struct virgl_context *)ctx;
+   struct virgl_context *vctx = virgl_context(ctx);
    uint32_t handle;
 
    handle = virgl_object_assign_handle();
@@ -758,7 +759,7 @@ static void *virgl_create_sampler_state(struct pipe_context *ctx,
 static void virgl_delete_sampler_state(struct pipe_context *ctx,
                                       void *ss)
 {
-   struct virgl_context *vctx = (struct virgl_context *)ctx;
+   struct virgl_context *vctx = virgl_context(ctx);
    uint32_t handle = (unsigned long)ss;
 
    virgl_encode_delete_object(vctx, handle, VIRGL_OBJECT_SAMPLER_STATE);
@@ -769,7 +770,7 @@ static void virgl_bind_sampler_states(struct pipe_context *ctx,
                                      unsigned num_samplers,
                                      void **samplers)
 {
-   struct virgl_context *vctx = (struct virgl_context *)ctx;
+   struct virgl_context *vctx = virgl_context(ctx);
    uint32_t handles[32];
    int i;
    for (i = 0; i < num_samplers; i++) {
@@ -781,7 +782,7 @@ static void virgl_bind_sampler_states(struct pipe_context *ctx,
 static void virgl_set_polygon_stipple(struct pipe_context *ctx,
                                      const struct pipe_poly_stipple *ps)
 {
-   struct virgl_context *vctx = (struct virgl_context *)ctx;
+   struct virgl_context *vctx = virgl_context(ctx);
    virgl_encoder_set_polygon_stipple(vctx, ps);
 }
 
@@ -790,21 +791,21 @@ static void virgl_set_scissor_states(struct pipe_context *ctx,
                                     unsigned num_scissor,
                                    const struct pipe_scissor_state *ss)
 {
-   struct virgl_context *vctx = (struct virgl_context *)ctx;
+   struct virgl_context *vctx = virgl_context(ctx);
    virgl_encoder_set_scissor_state(vctx, start_slot, num_scissor, ss);
 }
 
 static void virgl_set_sample_mask(struct pipe_context *ctx,
                                  unsigned sample_mask)
 {
-   struct virgl_context *vctx = (struct virgl_context *)ctx;
+   struct virgl_context *vctx = virgl_context(ctx);
    virgl_encoder_set_sample_mask(vctx, sample_mask);
 }
 
 static void virgl_set_clip_state(struct pipe_context *ctx,
                                 const struct pipe_clip_state *clip)
 {
-   struct virgl_context *vctx = (struct virgl_context *)ctx;
+   struct virgl_context *vctx = virgl_context(ctx);
    virgl_encoder_set_clip_state(vctx, clip);
 }
 
@@ -816,9 +817,9 @@ static void virgl_resource_copy_region(struct pipe_context *ctx,
                                       unsigned src_level,
                                       const struct pipe_box *src_box)
 {
-   struct virgl_context *vctx = (struct virgl_context *)ctx;
-   struct virgl_resource *dres = (struct virgl_resource *)dst;
-   struct virgl_resource *sres = (struct virgl_resource *)src;
+   struct virgl_context *vctx = virgl_context(ctx);
+   struct virgl_resource *dres = virgl_resource(dst);
+   struct virgl_resource *sres = virgl_resource(src);
 
    dres->clean = FALSE;
    virgl_encode_resource_copy_region(vctx, dres,
@@ -836,9 +837,9 @@ virgl_flush_resource(struct pipe_context *pipe,
 static void virgl_blit(struct pipe_context *ctx,
                       const struct pipe_blit_info *blit)
 {
-   struct virgl_context *vctx = (struct 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_context *vctx = virgl_context(ctx);
+   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,
@@ -848,7 +849,7 @@ static void virgl_blit(struct pipe_context *ctx,
 static void
 virgl_context_destroy( struct pipe_context *ctx )
 {
-   struct virgl_context *vctx = (struct virgl_context *)ctx;
+   struct virgl_context *vctx = virgl_context(ctx);
    struct virgl_screen *rs = virgl_screen(ctx->screen);
 
    vctx->framebuffer.zsbuf = NULL;
@@ -947,8 +948,8 @@ struct pipe_context *virgl_context_create(struct pipe_screen *pscreen,
                     16, UTIL_SLAB_SINGLETHREADED);
 
    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;