cso: add a higher-level interface which does all pipe interactions to set a given...
authorKeith Whitwell <keith@tungstengraphics.com>
Sun, 9 Mar 2008 14:09:55 +0000 (15:09 +0100)
committerKeith Whitwell <keith@tungstengraphics.com>
Sun, 9 Mar 2008 20:23:04 +0000 (20:23 +0000)
src/gallium/auxiliary/cso_cache/Makefile
src/gallium/auxiliary/cso_cache/SConscript
src/gallium/auxiliary/cso_cache/cso_context.c [new file with mode: 0644]
src/gallium/auxiliary/cso_cache/cso_context.h [new file with mode: 0644]

index 3e49266..6bd6602 100644 (file)
@@ -4,6 +4,7 @@ include $(TOP)/configs/current
 LIBNAME = cso_cache
 
 C_SOURCES = \
+       cso_context.c \
        cso_cache.c \
        cso_hash.c
 
index 9751881..651e68a 100644 (file)
@@ -3,6 +3,7 @@ Import('*')
 cso_cache = env.ConvenienceLibrary(
        target = 'cso_cache',
        source = [
+               'cso_context.c',
                'cso_cache.c',
                'cso_hash.c',
        ])
diff --git a/src/gallium/auxiliary/cso_cache/cso_context.c b/src/gallium/auxiliary/cso_cache/cso_context.c
new file mode 100644 (file)
index 0000000..7723746
--- /dev/null
@@ -0,0 +1,340 @@
+/**************************************************************************\r
+ *\r
+ * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.\r
+ * All Rights Reserved.\r
+ *\r
+ * Permission is hereby granted, free of charge, to any person obtaining a\r
+ * copy of this software and associated documentation files (the\r
+ * "Software"), to deal in the Software without restriction, including\r
+ * without limitation the rights to use, copy, modify, merge, publish,\r
+ * distribute, sub license, and/or sell copies of the Software, and to\r
+ * permit persons to whom the Software is furnished to do so, subject to\r
+ * the following conditions:\r
+ *\r
+ * The above copyright notice and this permission notice (including the\r
+ * next paragraph) shall be included in all copies or substantial portions\r
+ * of the Software.\r
+ *\r
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS\r
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\r
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.\r
+ * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR\r
+ * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,\r
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE\r
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
+ *\r
+ **************************************************************************/\r
+\r
+ /* Wrap the cso cache & hash mechanisms in a simplified\r
+  * pipe-driver-specific interface.\r
+  *\r
+  * Authors:\r
+  *   Zack Rusin <zack@tungstengraphics.com>\r
+  *   Keith Whitwell <keith@tungstengraphics.com>\r
+  */\r
+\r
+#include "pipe/p_state.h"\r
+#include "pipe/p_util.h"\r
+\r
+#include "cso_cache/cso_context.h"\r
+#include "cso_cache/cso_cache.h"\r
+#include "cso_cache/cso_hash.h"\r
+\r
+struct cso_context {\r
+   struct pipe_context *pipe;\r
+   struct cso_cache *cache;\r
+\r
+   struct {\r
+      void *samplers[PIPE_MAX_SAMPLERS];\r
+      unsigned nr_samplers;\r
+   } hw;\r
+\r
+   void *samplers[PIPE_MAX_SAMPLERS];\r
+   unsigned nr_samplers;\r
+\r
+   void *blend;\r
+   void *depth_stencil;\r
+   void *rasterizer;\r
+   void *fragment_shader;\r
+   void *vertex_shader;\r
+};\r
+\r
+\r
+struct cso_context *cso_create_context( struct pipe_context *pipe )\r
+{\r
+   struct cso_context *ctx = CALLOC_STRUCT(cso_context);\r
+   if (ctx == NULL)\r
+      goto out;\r
+\r
+   ctx->cache = cso_cache_create();\r
+   if (ctx->cache == NULL)\r
+      goto out;\r
+\r
+   ctx->pipe = pipe;\r
+\r
+   return ctx;\r
+\r
+out:\r
+   cso_destroy_context( ctx );      \r
+   return NULL;\r
+}\r
+\r
+\r
+void cso_destroy_context( struct cso_context *ctx )\r
+{\r
+   if (ctx == NULL)\r
+      return;\r
+   \r
+/*\r
+   if (ctx->pipe) \r
+      ctx->pipe->flush( ctx->pipe, PIPE_FLUSH_UNBIND_ALL );\r
+*/\r
+\r
+   if (ctx->cache)\r
+      cso_cache_delete( ctx->cache );\r
+   \r
+   FREE( ctx );\r
+}\r
+\r
+\r
+/* Those function will either find the state of the given template\r
+ * in the cache or they will create a new state from the given\r
+ * template, insert it in the cache and return it.\r
+ */\r
+\r
+/*\r
+ * If the driver returns 0 from the create method then they will assign\r
+ * the data member of the cso to be the template itself.\r
+ */\r
+\r
+void cso_set_blend(struct cso_context *ctx,\r
+                   const struct pipe_blend_state *templ)\r
+{\r
+   unsigned hash_key = cso_construct_key((void*)templ, sizeof(struct pipe_blend_state));\r
+   struct cso_hash_iter iter = cso_find_state_template(ctx->cache,\r
+                                                       hash_key, CSO_BLEND,\r
+                                                       (void*)templ);\r
+   void *handle;\r
+\r
+   if (cso_hash_iter_is_null(iter)) {\r
+      struct cso_blend *cso = malloc(sizeof(struct cso_blend));\r
+\r
+      cso->state = *templ;\r
+      cso->data = ctx->pipe->create_blend_state(ctx->pipe, &cso->state);\r
+      cso->delete_state = (cso_state_callback)ctx->pipe->delete_blend_state;\r
+      cso->context = ctx->pipe;\r
+\r
+      iter = cso_insert_state(ctx->cache, hash_key, CSO_BLEND, cso);\r
+      handle = cso->data;\r
+   }\r
+   else {\r
+      handle = ((struct cso_blend *)cso_hash_iter_data(iter))->data;\r
+   }\r
+\r
+   if (ctx->blend != handle) {\r
+      ctx->blend = handle;\r
+      ctx->pipe->bind_blend_state(ctx->pipe, handle);\r
+   }\r
+}\r
+\r
+void cso_single_sampler(struct cso_context *ctx,\r
+                        unsigned idx,\r
+                        const struct pipe_sampler_state *templ)\r
+{\r
+   void *handle = NULL;\r
+   \r
+   if (templ != NULL) {\r
+      unsigned hash_key = cso_construct_key((void*)templ, sizeof(struct pipe_sampler_state));\r
+      struct cso_hash_iter iter = cso_find_state_template(ctx->cache,\r
+                                                          hash_key, CSO_SAMPLER,\r
+                                                          (void*)templ);\r
+\r
+      if (cso_hash_iter_is_null(iter)) {\r
+         struct cso_sampler *cso = malloc(sizeof(struct cso_sampler));\r
+         \r
+         cso->state = *templ;\r
+         cso->data = ctx->pipe->create_sampler_state(ctx->pipe, &cso->state);\r
+         cso->delete_state = (cso_state_callback)ctx->pipe->delete_sampler_state;\r
+         cso->context = ctx->pipe;\r
+\r
+         iter = cso_insert_state(ctx->cache, hash_key, CSO_SAMPLER, cso);\r
+         handle = cso->data;\r
+      }\r
+      else {\r
+         handle = ((struct cso_sampler *)cso_hash_iter_data(iter))->data;\r
+      }\r
+   }\r
+\r
+   ctx->samplers[idx] = handle;\r
+}\r
+\r
+void cso_single_sampler_done( struct cso_context *ctx )\r
+{\r
+   unsigned i; \r
+\r
+   for (i = 0; i < 8; i++)\r
+      if (ctx->samplers[i] == NULL)\r
+         break;\r
+\r
+   ctx->nr_samplers = i;\r
+\r
+   if (ctx->hw.nr_samplers != ctx->nr_samplers ||\r
+       memcmp(ctx->hw.samplers, \r
+              ctx->samplers, \r
+              ctx->nr_samplers * sizeof(void *)) != 0) \r
+   {\r
+      memcpy(ctx->hw.samplers, ctx->samplers, ctx->nr_samplers * sizeof(void *));\r
+      ctx->hw.nr_samplers = ctx->nr_samplers;\r
+\r
+      ctx->pipe->bind_sampler_states(ctx->pipe, ctx->nr_samplers, ctx->samplers);\r
+   }\r
+}\r
+\r
+void cso_set_samplers( struct cso_context *ctx,\r
+                       unsigned nr,\r
+                       const struct pipe_sampler_state **templates )\r
+{\r
+   unsigned i;\r
+   \r
+   /* TODO: fastpath\r
+    */\r
+\r
+   for (i = 0; i < nr; i++)\r
+      cso_single_sampler( ctx, i, templates[i] );\r
+\r
+   for ( ; i < ctx->nr_samplers; i++)\r
+      cso_single_sampler( ctx, i, NULL );\r
+   \r
+   cso_single_sampler_done( ctx );\r
+}\r
+\r
+void cso_set_depth_stencil_alpha(struct cso_context *ctx,\r
+                                 const struct pipe_depth_stencil_alpha_state *templ)\r
+{\r
+   unsigned hash_key = cso_construct_key((void*)templ,\r
+                                         sizeof(struct pipe_depth_stencil_alpha_state));\r
+   struct cso_hash_iter iter = cso_find_state_template(ctx->cache,\r
+                                                       hash_key, \r
+                                                      CSO_DEPTH_STENCIL_ALPHA,\r
+                                                       (void*)templ);\r
+   void *handle;\r
+\r
+   if (cso_hash_iter_is_null(iter)) {\r
+      struct cso_depth_stencil_alpha *cso = malloc(sizeof(struct cso_depth_stencil_alpha));\r
+\r
+      cso->state = *templ;\r
+      cso->data = ctx->pipe->create_depth_stencil_alpha_state(ctx->pipe, &cso->state);\r
+      cso->delete_state = (cso_state_callback)ctx->pipe->delete_depth_stencil_alpha_state;\r
+      cso->context = ctx->pipe;\r
+\r
+      cso_insert_state(ctx->cache, hash_key, CSO_DEPTH_STENCIL_ALPHA, cso);\r
+      handle = cso->data;\r
+   }\r
+   else {\r
+      handle = ((struct cso_depth_stencil_alpha *)cso_hash_iter_data(iter))->data;\r
+   }\r
+\r
+   if (ctx->depth_stencil != handle) {\r
+      ctx->depth_stencil = handle;\r
+      ctx->pipe->bind_depth_stencil_alpha_state(ctx->pipe, handle);\r
+   }\r
+}\r
+\r
+\r
+\r
+void cso_set_rasterizer(struct cso_context *ctx,\r
+                              const struct pipe_rasterizer_state *templ)\r
+{\r
+   unsigned hash_key = cso_construct_key((void*)templ,\r
+                                         sizeof(struct pipe_rasterizer_state));\r
+   struct cso_hash_iter iter = cso_find_state_template(ctx->cache,\r
+                                                       hash_key, CSO_RASTERIZER,\r
+                                                       (void*)templ);\r
+   void *handle = NULL;\r
+\r
+   if (cso_hash_iter_is_null(iter)) {\r
+      struct cso_rasterizer *cso = malloc(sizeof(struct cso_rasterizer));\r
+\r
+      cso->state = *templ;\r
+      cso->data = ctx->pipe->create_rasterizer_state(ctx->pipe, &cso->state);\r
+      cso->delete_state = (cso_state_callback)ctx->pipe->delete_rasterizer_state;\r
+      cso->context = ctx->pipe;\r
+\r
+      cso_insert_state(ctx->cache, hash_key, CSO_RASTERIZER, cso);\r
+      handle = cso->data;\r
+   }\r
+   else {\r
+      handle = ((struct cso_rasterizer *)cso_hash_iter_data(iter))->data;\r
+   }\r
+\r
+   if (ctx->rasterizer != handle) {\r
+      ctx->rasterizer = handle;\r
+      ctx->pipe->bind_rasterizer_state(ctx->pipe, handle);\r
+   }\r
+}\r
+\r
+\r
+\r
+\r
+\r
+void cso_set_fragment_shader(struct cso_context *ctx,\r
+                             const struct pipe_shader_state *templ)\r
+{\r
+   unsigned hash_key = cso_construct_key((void*)templ,\r
+                                         sizeof(struct pipe_shader_state));\r
+   struct cso_hash_iter iter = cso_find_state_template(ctx->cache,\r
+                                                       hash_key, CSO_FRAGMENT_SHADER,\r
+                                                       (void*)templ);\r
+   void *handle = NULL;\r
+\r
+   if (cso_hash_iter_is_null(iter)) {\r
+      struct cso_fragment_shader *cso = malloc(sizeof(struct cso_fragment_shader));\r
+\r
+      cso->state = *templ;\r
+      cso->data = ctx->pipe->create_fs_state(ctx->pipe, &cso->state);\r
+      cso->delete_state = (cso_state_callback)ctx->pipe->delete_fs_state;\r
+      cso->context = ctx->pipe;\r
+\r
+      iter = cso_insert_state(ctx->cache, hash_key, CSO_FRAGMENT_SHADER, cso);\r
+      handle = cso->data;\r
+   }\r
+   else {\r
+      handle = ((struct cso_fragment_shader *)cso_hash_iter_data(iter))->data;\r
+   }\r
+\r
+   if (ctx->fragment_shader != handle) {\r
+      ctx->fragment_shader = handle;\r
+      ctx->pipe->bind_fs_state(ctx->pipe, handle);\r
+   }\r
+}\r
+\r
+void cso_set_vertex_shader(struct cso_context *ctx,\r
+                           const struct pipe_shader_state *templ)\r
+{\r
+   unsigned hash_key = cso_construct_key((void*)templ,\r
+                                         sizeof(struct pipe_shader_state));\r
+   struct cso_hash_iter iter = cso_find_state_template(ctx->cache,\r
+                                                       hash_key, CSO_VERTEX_SHADER,\r
+                                                       (void*)templ);\r
+   void *handle = NULL;\r
+\r
+   if (cso_hash_iter_is_null(iter)) {\r
+      struct cso_vertex_shader *cso = malloc(sizeof(struct cso_vertex_shader));\r
+\r
+      cso->state = *templ;\r
+      cso->data = ctx->pipe->create_vs_state(ctx->pipe, &cso->state);\r
+      cso->delete_state = (cso_state_callback)ctx->pipe->delete_vs_state;\r
+      cso->context = ctx->pipe;\r
+\r
+      iter = cso_insert_state(ctx->cache, hash_key, CSO_VERTEX_SHADER, cso);\r
+      handle = cso->data;\r
+   }\r
+   else {\r
+      handle = ((struct cso_vertex_shader *)cso_hash_iter_data(iter))->data;\r
+   }\r
+\r
+   if (ctx->vertex_shader != handle) {\r
+      ctx->vertex_shader = handle;\r
+      ctx->pipe->bind_fs_state(ctx->pipe, handle);\r
+   }\r
+}\r
diff --git a/src/gallium/auxiliary/cso_cache/cso_context.h b/src/gallium/auxiliary/cso_cache/cso_context.h
new file mode 100644 (file)
index 0000000..1f2a630
--- /dev/null
@@ -0,0 +1,85 @@
+/**************************************************************************
+ *
+ * Copyright 2007-2008 Tungsten Graphics, Inc., Cedar Park, Texas.
+ * All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sub license, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial portions
+ * of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
+ * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
+ * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ *
+ **************************************************************************/
+
+
+#ifndef CSO_CONTEXT_H
+#define CSO_CONTEXT_H
+
+#include "pipe/p_context.h"
+#include "pipe/p_state.h"
+
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+struct cso_context;
+
+struct cso_context *cso_create_context( struct pipe_context *pipe );
+
+void cso_set_blend( struct cso_context *cso,
+                    const struct pipe_blend_state *blend );
+
+void cso_set_depth_stencil_alpha( struct cso_context *cso,
+                                  const struct pipe_depth_stencil_alpha_state *dsa );
+
+void cso_set_rasterizer( struct cso_context *cso,
+                         const struct pipe_rasterizer_state *rasterizer );
+
+void cso_set_samplers( struct cso_context *cso,
+                       unsigned count,
+                       const struct pipe_sampler_state **states );
+
+/* Alternate interface to support state trackers that like to modify
+ * samplers one at a time:
+ */
+void cso_single_sampler( struct cso_context *cso,
+                         unsigned nr,
+                         const struct pipe_sampler_state *states );
+
+void cso_single_sampler_done( struct cso_context *cso );
+
+
+/* These aren't really sensible -- most of the time the api provides
+ * object semantics for shaders anyway, and the cases where it doesn't
+ * (eg mesa's internall-generated texenv programs), it will be up to
+ * the state tracker to implement their own specialized caching.
+ */
+void cso_set_fragment_shader( struct cso_context *cso,
+                              const struct pipe_shader_state *shader );
+
+void cso_set_vertex_shader( struct cso_context *cso,
+                            const struct pipe_shader_state *shader );
+
+void cso_destroy_context( struct cso_context *cso );
+
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif