python: Allow to create/specify shaders.
authorJosé Fonseca <jrfonseca@tungstengraphics.com>
Mon, 14 Jul 2008 01:45:40 +0000 (10:45 +0900)
committerJosé Fonseca <jrfonseca@tungstengraphics.com>
Mon, 14 Jul 2008 03:41:06 +0000 (12:41 +0900)
src/gallium/state_trackers/python/gallium.i
src/gallium/state_trackers/python/samples/simple.py
src/gallium/state_trackers/python/st_device.c

index d6594f3a87ada33bc0a182574756888598def137..53d160ef6c4960199a601140465471aa9d23fb0e 100644 (file)
 #include "pipe/p_inlines.h"
 #include "pipe/p_util.h"
 #include "pipe/p_shader_tokens.h" 
+#include "cso_cache/cso_context.h"
 #include "util/u_draw_quad.h" 
 #include "util/p_tile.h" 
-#include "cso_cache/cso_context.h"
+#include "tgsi/util/tgsi_text.h" 
+#include "tgsi/util/tgsi_dump.h" 
 
 #include "st_device.h"
 
@@ -201,29 +203,32 @@ struct st_context {
       cso_set_depth_stencil_alpha($self->cso, state);
    }
 
-   
-   void * create_fs( const struct pipe_shader_state *state ) {
-      return $self->pipe->create_fs_state($self->pipe, state);
-   }
-   
-   void bind_fs( void *state_obj ) {
-      $self->pipe->bind_fs_state($self->pipe, state_obj);
-   }
-   
-   void delete_fs( void *state_obj ) {
-      $self->pipe->delete_fs_state($self->pipe, state_obj);
-   }
+   void set_fragment_shader( const struct pipe_shader_state *state ) {
+      void *fs;
+      
+      fs = $self->pipe->create_fs_state($self->pipe, state);
+      if(!fs)
+         return;
+      
+      if(cso_set_fragment_shader_handle($self->cso, fs) != PIPE_OK)
+         return;
 
-   void * create_vs( const struct pipe_shader_state *state ) {
-      return $self->pipe->create_vs_state($self->pipe, state);
-   }
-   
-   void bind_vs( void *state_obj ) {
-      $self->pipe->bind_vs_state($self->pipe, state_obj);
+      cso_delete_fragment_shader($self->cso, $self->fs);
+      $self->fs = fs;
    }
-   
-   void delete_vs( void *state_obj ) {
-      $self->pipe->delete_vs_state($self->pipe, state_obj);
+
+   void set_vertex_shader( const struct pipe_shader_state *state ) {
+      void *vs;
+      
+      vs = $self->pipe->create_vs_state($self->pipe, state);
+      if(!vs)
+         return;
+      
+      if(cso_set_vertex_shader_handle($self->cso, vs) != PIPE_OK)
+         return;
+
+      cso_delete_vertex_shader($self->cso, $self->vs);
+      $self->vs = vs;
    }
 
    /*
@@ -446,3 +451,42 @@ error1:
    }
    
 };
+
+
+%extend pipe_shader_state {
+   
+   pipe_shader_state(const char *text, unsigned num_tokens = 1024) {
+      struct tgsi_token *tokens;
+      struct pipe_shader_state *shader;
+      
+      tokens = MALLOC(num_tokens * sizeof(struct tgsi_token));
+      if(!tokens)
+         goto error1;
+      
+      if(tgsi_text_translate(text, tokens, num_tokens ) != TRUE)
+         goto error2;
+      
+      shader = CALLOC_STRUCT(pipe_shader_state);
+      if(!shader)
+         goto error3;
+      
+      shader->tokens = tokens;
+      
+      return shader;
+      
+error3:
+error2:
+      FREE(tokens);
+error1:      
+      return NULL;
+   }
+   
+   ~pipe_shader_state() {
+      FREE((void*)$self->tokens);
+      FREE($self);
+   }
+
+   void dump(unsigned flags = 0) {
+      tgsi_dump($self->tokens, flags);
+   }
+}
\ No newline at end of file
index 77e182b6447eaff1fac99023a0be455833e72eb2..6e90eec28ae0a25014946c562de4418ac0638fec 100644 (file)
@@ -103,7 +103,9 @@ def test(dev):
     ctx.set_sampler(0, sampler)
 
     #  texture 
-    texture = dev.texture_create(PIPE_FORMAT_A8R8G8B8_UNORM, width, height, usage=PIPE_TEXTURE_USAGE_RENDER_TARGET)
+    texture = dev.texture_create(PIPE_FORMAT_A8R8G8B8_UNORM, 
+                                 width, height, 
+                                 usage=PIPE_TEXTURE_USAGE_RENDER_TARGET)
     ctx.set_sampler_texture(0, texture)
 
     #  drawing dest 
@@ -116,12 +118,29 @@ def test(dev):
     ctx.set_framebuffer(fb)
 
     # vertex shader
-    # vs = Shader()
-    #ctx.set_vertex_shader(vs)
+    vs = Shader('''
+        VERT1.1
+        DCL IN[0], POSITION, CONSTANT
+        DCL IN[1], GENERIC[0], CONSTANT
+        DCL OUT[0], POSITION, CONSTANT
+        DCL OUT[1], GENERIC[0], CONSTANT
+        0:MOV OUT[0], IN[0]
+        1:MOV OUT[1], IN[1]
+        2:END
+    ''')
+    #vs.dump()
+    ctx.set_vertex_shader(vs)
 
     # fragment shader
-    #fs = Shader()
-    #ctx.set_fragment_shader(fs)
+    fs = Shader('''
+        FRAG1.1
+        DCL IN[0], COLOR, CONSTANT
+        DCL OUT[0], COLOR, CONSTANT
+        0:MOV OUT[0], IN[0]
+        1:END
+    ''')
+    #fs.dump()
+    ctx.set_fragment_shader(fs)
 
     if 0:
         nverts = 4
index 430a7af1765481533705b2e6956ee00d847348d2..e9002b493de12c183faad9998a1eb0b77022c43c 100644 (file)
@@ -99,18 +99,12 @@ st_context_destroy(struct st_context *st_ctx)
    if(st_ctx) {
       struct st_device *st_dev = st_ctx->st_dev;
       
-      if(st_ctx->vs) {
-         st_ctx->pipe->bind_vs_state(st_ctx->pipe, NULL);
-         st_ctx->pipe->delete_vs_state(st_ctx->pipe, st_ctx->vs);
-      }
-
-      if(st_ctx->fs) {
-         st_ctx->pipe->bind_fs_state(st_ctx->pipe, NULL);
-         st_ctx->pipe->delete_fs_state(st_ctx->pipe, st_ctx->fs);
-      }
-      
-      if(st_ctx->cso)
+      if(st_ctx->cso) {
+         cso_delete_vertex_shader(st_ctx->cso, st_ctx->vs);
+         cso_delete_fragment_shader(st_ctx->cso, st_ctx->fs);
+         
          cso_destroy_context(st_ctx->cso);
+      }
       
       if(st_ctx->pipe)
          st_ctx->st_dev->st_ws->context_destroy(st_ctx->pipe);
@@ -162,8 +156,8 @@ st_context_create(struct st_device *st_dev)
    st_ctx->fs = util_make_fragment_passthrough_shader(st_ctx->pipe, 
                                                       &st_ctx->frag_shader);
    
-   st_ctx->pipe->bind_fs_state(st_ctx->pipe, st_ctx->fs);
-   st_ctx->pipe->bind_vs_state(st_ctx->pipe, st_ctx->vs);
+   cso_set_fragment_shader_handle(st_ctx->cso, st_ctx->fs);
+   cso_set_vertex_shader_handle(st_ctx->cso, st_ctx->vs);
 
    return st_ctx;
 }