cso: add set_index_buffer and draw_vbo passthrough functions
[mesa.git] / src / gallium / auxiliary / cso_cache / cso_context.c
index c95a1ba4117225e371b0324c5e51e8602418ca4c..9ec7a2a9676108fbf0c9254b059d3966761da4e9 100644 (file)
@@ -36,6 +36,7 @@
   */
 
 #include "pipe/p_state.h"
+#include "util/u_draw.h"
 #include "util/u_framebuffer.h"
 #include "util/u_inlines.h"
 #include "util/u_math.h"
@@ -165,28 +166,6 @@ static boolean delete_rasterizer_state(struct cso_context *ctx, void *state)
    return TRUE;
 }
 
-static boolean delete_fs_state(struct cso_context *ctx, void *state)
-{
-   struct cso_fragment_shader *cso = (struct cso_fragment_shader *)state;
-   if (ctx->fragment_shader == cso->data)
-      return FALSE;
-   if (cso->delete_state)
-      cso->delete_state(cso->context, cso->data);
-   FREE(state);
-   return TRUE;
-}
-
-static boolean delete_vs_state(struct cso_context *ctx, void *state)
-{
-   struct cso_vertex_shader *cso = (struct cso_vertex_shader *)state;
-   if (ctx->vertex_shader == cso->data)
-      return TRUE;
-   if (cso->delete_state)
-      cso->delete_state(cso->context, cso->data);
-   FREE(state);
-   return FALSE;
-}
-
 static boolean delete_vertex_elements(struct cso_context *ctx,
                                       void *state)
 {
@@ -218,12 +197,6 @@ static INLINE boolean delete_cso(struct cso_context *ctx,
    case CSO_RASTERIZER:
       return delete_rasterizer_state(ctx, state);
       break;
-   case CSO_FRAGMENT_SHADER:
-      return delete_fs_state(ctx, state);
-      break;
-   case CSO_VERTEX_SHADER:
-      return delete_vs_state(ctx, state);
-      break;
    case CSO_VELEMENTS:
       return delete_vertex_elements(ctx, state);
       break;
@@ -571,52 +544,6 @@ void cso_delete_fragment_shader(struct cso_context *ctx, void *handle )
    ctx->pipe->delete_fs_state(ctx->pipe, handle);
 }
 
-/* Not really working:
- */
-#if 0
-enum pipe_error cso_set_fragment_shader(struct cso_context *ctx,
-                                        const struct pipe_shader_state *templ)
-{
-   const struct tgsi_token *tokens = templ->tokens;
-   unsigned num_tokens = tgsi_num_tokens(tokens);
-   size_t tokens_size = num_tokens*sizeof(struct tgsi_token);
-   unsigned hash_key = cso_construct_key((void*)tokens, tokens_size);
-   struct cso_hash_iter iter = cso_find_state_template(ctx->cache,
-                                                       hash_key, 
-                                                       CSO_FRAGMENT_SHADER,
-                                                       (void*)tokens,
-                                                       sizeof(*templ)); /* XXX correct? tokens_size? */
-   void *handle = NULL;
-
-   if (cso_hash_iter_is_null(iter)) {
-      struct cso_fragment_shader *cso = MALLOC(sizeof(struct cso_fragment_shader) + tokens_size);
-      struct tgsi_token *cso_tokens = (struct tgsi_token *)((char *)cso + sizeof(*cso));
-
-      if (!cso)
-         return PIPE_ERROR_OUT_OF_MEMORY;
-
-      memcpy(cso_tokens, tokens, tokens_size);
-      cso->state.tokens = cso_tokens;
-      cso->data = ctx->pipe->create_fs_state(ctx->pipe, &cso->state);
-      cso->delete_state = (cso_state_callback)ctx->pipe->delete_fs_state;
-      cso->context = ctx->pipe;
-
-      iter = cso_insert_state(ctx->cache, hash_key, CSO_FRAGMENT_SHADER, cso);
-      if (cso_hash_iter_is_null(iter)) {
-         FREE(cso);
-         return PIPE_ERROR_OUT_OF_MEMORY;
-      }
-
-      handle = cso->data;
-   }
-   else {
-      handle = ((struct cso_fragment_shader *)cso_hash_iter_data(iter))->data;
-   }
-
-   return cso_set_fragment_shader_handle( ctx, handle );
-}
-#endif
-
 void cso_save_fragment_shader(struct cso_context *ctx)
 {
    assert(!ctx->fragment_shader_saved);
@@ -653,50 +580,6 @@ void cso_delete_vertex_shader(struct cso_context *ctx, void *handle )
    ctx->pipe->delete_vs_state(ctx->pipe, handle);
 }
 
-
-/* Not really working:
- */
-#if 0
-enum pipe_error cso_set_vertex_shader(struct cso_context *ctx,
-                                      const struct pipe_shader_state *templ)
-{
-   unsigned hash_key = cso_construct_key((void*)templ,
-                                         sizeof(struct pipe_shader_state));
-   struct cso_hash_iter iter = cso_find_state_template(ctx->cache,
-                                                       hash_key, CSO_VERTEX_SHADER,
-                                                       (void*)templ,
-                                                       sizeof(*templ));
-   void *handle = NULL;
-
-   if (cso_hash_iter_is_null(iter)) {
-      struct cso_vertex_shader *cso = MALLOC(sizeof(struct cso_vertex_shader));
-
-      if (!cso)
-         return PIPE_ERROR_OUT_OF_MEMORY;
-
-      memcpy(cso->state, templ, sizeof(*templ));
-      cso->data = ctx->pipe->create_vs_state(ctx->pipe, &cso->state);
-      cso->delete_state = (cso_state_callback)ctx->pipe->delete_vs_state;
-      cso->context = ctx->pipe;
-
-      iter = cso_insert_state(ctx->cache, hash_key, CSO_VERTEX_SHADER, cso);
-      if (cso_hash_iter_is_null(iter)) {
-         FREE(cso);
-         return PIPE_ERROR_OUT_OF_MEMORY;
-      }
-
-      handle = cso->data;
-   }
-   else {
-      handle = ((struct cso_vertex_shader *)cso_hash_iter_data(iter))->data;
-   }
-
-   return cso_set_vertex_shader_handle( ctx, handle );
-}
-#endif
-
-
-
 void cso_save_vertex_shader(struct cso_context *ctx)
 {
    assert(!ctx->vertex_shader_saved);
@@ -981,10 +864,18 @@ void cso_save_vertex_buffers(struct cso_context *ctx)
 
 void cso_restore_vertex_buffers(struct cso_context *ctx)
 {
+   unsigned i;
+
    util_copy_vertex_buffers(ctx->vertex_buffers,
                             &ctx->nr_vertex_buffers,
                             ctx->vertex_buffers_saved,
                             ctx->nr_vertex_buffers_saved);
+
+   for (i = 0; i < ctx->nr_vertex_buffers_saved; i++) {
+      pipe_resource_reference(&ctx->vertex_buffers_saved[i].buffer, NULL);
+   }
+   ctx->nr_vertex_buffers_saved = 0;
+
    ctx->pipe->set_vertex_buffers(ctx->pipe, ctx->nr_vertex_buffers,
                                  ctx->vertex_buffers);
 }
@@ -1400,3 +1291,37 @@ cso_restore_stream_outputs(struct cso_context *ctx)
    ctx->nr_so_targets = ctx->nr_so_targets_saved;
    ctx->nr_so_targets_saved = 0;
 }
+
+/* drawing */
+
+void
+cso_set_index_buffer(struct cso_context *cso,
+                     const struct pipe_index_buffer *ib)
+{
+   struct pipe_context *pipe = cso->pipe;
+   pipe->set_index_buffer(pipe, ib);
+}
+
+void
+cso_draw_vbo(struct cso_context *cso,
+             const struct pipe_draw_info *info)
+{
+   struct pipe_context *pipe = cso->pipe;
+   pipe->draw_vbo(pipe, info);
+}
+
+void
+cso_draw_arrays(struct cso_context *cso, uint mode, uint start, uint count)
+{
+   struct pipe_draw_info info;
+
+   util_draw_init_info(&info);
+
+   info.mode = mode;
+   info.start = start;
+   info.count = count;
+   info.min_index = start;
+   info.max_index = start + count - 1;
+
+   cso_draw_vbo(cso, &info);
+}