st/vega: Add support for per-channel alpha.
authorChia-I Wu <olv@lunarg.com>
Sat, 4 Dec 2010 04:03:07 +0000 (12:03 +0800)
committerChia-I Wu <olv@lunarg.com>
Sat, 4 Dec 2010 05:20:38 +0000 (13:20 +0800)
Drawing an image in VG_DRAW_IMAGE_STENCIL mode produces per-channel
alpha for use in blending.  Add a new shader stage to produce and save
it in TEMP[1].

For other modes that do not need per-channel alpha, the stage does

  MOV TEMP[1], TEMP[0].wwww

src/gallium/state_trackers/vega/asm_fill.h
src/gallium/state_trackers/vega/shader.c
src/gallium/state_trackers/vega/shaders_cache.c
src/gallium/state_trackers/vega/shaders_cache.h

index 22ad6ac39a51e6365710f981956a19313eca32a9..a95e4c9efcbf4e19cd4446c98dd717d8d97fc767 100644 (file)
@@ -181,7 +181,9 @@ image_normal( struct ureg_program *ureg,
               struct ureg_dst *temp,
               struct ureg_src *constant)
 {
-   ureg_TEX(ureg, *out, TGSI_TEXTURE_2D, in[1], sampler[3]);
+   /* store and pass image color in TEMP[1] */
+   ureg_TEX(ureg, temp[1], TGSI_TEXTURE_2D, in[1], sampler[3]);
+   ureg_MOV(ureg, *out, ureg_src(temp[1]));
 }
 
 
@@ -193,6 +195,7 @@ image_multiply( struct ureg_program *ureg,
                 struct ureg_dst *temp,
                 struct ureg_src *constant)
 {
+   /* store and pass image color in TEMP[1] */
    ureg_TEX(ureg, temp[1], TGSI_TEXTURE_2D, in[1], sampler[3]);
    ureg_MUL(ureg, *out, ureg_src(temp[0]), ureg_src(temp[1]));
 }
@@ -206,8 +209,9 @@ image_stencil( struct ureg_program *ureg,
                struct ureg_dst *temp,
                struct ureg_src *constant)
 {
+   /* store and pass image color in TEMP[1] */
    ureg_TEX(ureg, temp[1], TGSI_TEXTURE_2D, in[1], sampler[3]);
-   ureg_MUL(ureg, *out, ureg_src(temp[0]), ureg_src(temp[1]));
+   ureg_MOV(ureg, *out, ureg_src(temp[0]));
 }
 
 static INLINE void
@@ -218,13 +222,54 @@ color_transform( struct ureg_program *ureg,
                  struct ureg_dst *temp,
                  struct ureg_src *constant)
 {
-   ureg_MAD(ureg, temp[1], ureg_src(temp[0]), constant[0], constant[1]);
+   /* note that TEMP[1] may already be used for image color */
+
+   ureg_MAD(ureg, temp[2], ureg_src(temp[0]), constant[0], constant[1]);
    /* clamp to [0.0f, 1.0f] */
-   ureg_CLAMP(ureg, temp[1],
-              ureg_src(temp[1]),
+   ureg_CLAMP(ureg, temp[2],
+              ureg_src(temp[2]),
               ureg_scalar(constant[3], TGSI_SWIZZLE_X),
               ureg_scalar(constant[3], TGSI_SWIZZLE_Y));
-   ureg_MOV(ureg, *out, ureg_src(temp[1]));
+   ureg_MOV(ureg, *out, ureg_src(temp[2]));
+}
+
+static INLINE void
+alpha_normal( struct ureg_program *ureg,
+              struct ureg_dst *out,
+              struct ureg_src *in,
+              struct ureg_src *sampler,
+              struct ureg_dst *temp,
+              struct ureg_src *constant)
+{
+   /* save per-channel alpha in TEMP[1] */
+   ureg_MOV(ureg, temp[1], ureg_scalar(ureg_src(temp[0]), TGSI_SWIZZLE_W));
+
+   ureg_MOV(ureg, *out, ureg_src(temp[0]));
+}
+
+static INLINE void
+alpha_per_channel( struct ureg_program *ureg,
+                   struct ureg_dst *out,
+                   struct ureg_src *in,
+                   struct ureg_src *sampler,
+                   struct ureg_dst *temp,
+                   struct ureg_src *constant)
+{
+   /* save per-channel alpha in TEMP[1] */
+   ureg_MUL(ureg,
+            ureg_writemask(temp[1], TGSI_WRITEMASK_W),
+            ureg_src(temp[0]),
+            ureg_src(temp[1]));
+   ureg_MUL(ureg,
+            ureg_writemask(temp[1], TGSI_WRITEMASK_XYZ),
+            ureg_src(temp[1]),
+            ureg_scalar(ureg_src(temp[1]), TGSI_SWIZZLE_W));
+
+   /* update alpha */
+   ureg_MOV(ureg,
+            ureg_writemask(temp[0], TGSI_WRITEMASK_W),
+            ureg_src(temp[1]));
+   ureg_MOV(ureg, *out, ureg_src(temp[0]));
 }
 
 /**
@@ -238,8 +283,8 @@ blend_generic(struct ureg_program *ureg,
               VGBlendMode mode,
               struct ureg_dst out,
               struct ureg_src src,
-              struct ureg_src dst,
               struct ureg_src src_channel_alpha,
+              struct ureg_src dst,
               struct ureg_src one,
               struct ureg_dst temp[2])
 {
@@ -320,13 +365,13 @@ blend_multiply( struct ureg_program *ureg,
                 struct ureg_dst *temp,
                 struct ureg_src *constant)
 {
-   ureg_TEX(ureg, temp[1], TGSI_TEXTURE_2D, in[0], sampler[2]);
+   ureg_TEX(ureg, temp[2], TGSI_TEXTURE_2D, in[0], sampler[2]);
    blend_generic(ureg, VG_BLEND_MULTIPLY, *out,
                  ureg_src(temp[0]),
                  ureg_src(temp[1]),
-                 ureg_scalar(ureg_src(temp[0]), TGSI_SWIZZLE_W),
+                 ureg_src(temp[2]),
                  ureg_scalar(constant[3], TGSI_SWIZZLE_Y),
-                 temp + 2);
+                 temp + 3);
 }
 
 static INLINE void
@@ -337,13 +382,13 @@ blend_screen( struct ureg_program *ureg,
               struct ureg_dst     *temp,
               struct ureg_src     *constant)
 {
-   ureg_TEX(ureg, temp[1], TGSI_TEXTURE_2D, in[0], sampler[2]);
+   ureg_TEX(ureg, temp[2], TGSI_TEXTURE_2D, in[0], sampler[2]);
    blend_generic(ureg, VG_BLEND_SCREEN, *out,
                  ureg_src(temp[0]),
                  ureg_src(temp[1]),
-                 ureg_scalar(ureg_src(temp[0]), TGSI_SWIZZLE_W),
+                 ureg_src(temp[2]),
                  ureg_scalar(constant[3], TGSI_SWIZZLE_Y),
-                 temp + 2);
+                 temp + 3);
 }
 
 static INLINE void
@@ -354,13 +399,13 @@ blend_darken( struct ureg_program *ureg,
               struct ureg_dst     *temp,
               struct ureg_src     *constant)
 {
-   ureg_TEX(ureg, temp[1], TGSI_TEXTURE_2D, in[0], sampler[2]);
+   ureg_TEX(ureg, temp[2], TGSI_TEXTURE_2D, in[0], sampler[2]);
    blend_generic(ureg, VG_BLEND_DARKEN, *out,
                  ureg_src(temp[0]),
                  ureg_src(temp[1]),
-                 ureg_scalar(ureg_src(temp[0]), TGSI_SWIZZLE_W),
+                 ureg_src(temp[2]),
                  ureg_scalar(constant[3], TGSI_SWIZZLE_Y),
-                 temp + 2);
+                 temp + 3);
 }
 
 static INLINE void
@@ -371,13 +416,13 @@ blend_lighten( struct ureg_program *ureg,
                struct ureg_dst *temp,
                struct ureg_src     *constant)
 {
-   ureg_TEX(ureg, temp[1], TGSI_TEXTURE_2D, in[0], sampler[2]);
+   ureg_TEX(ureg, temp[2], TGSI_TEXTURE_2D, in[0], sampler[2]);
    blend_generic(ureg, VG_BLEND_LIGHTEN, *out,
                  ureg_src(temp[0]),
                  ureg_src(temp[1]),
-                 ureg_scalar(ureg_src(temp[0]), TGSI_SWIZZLE_W),
+                 ureg_src(temp[2]),
                  ureg_scalar(constant[3], TGSI_SWIZZLE_Y),
-                 temp + 2);
+                 temp + 3);
 }
 
 static INLINE void
@@ -488,7 +533,7 @@ static const struct shader_asm_info shaders_paint_asm[] = {
 /* image draw modes */
 static const struct shader_asm_info shaders_image_asm[] = {
    {VEGA_IMAGE_NORMAL_SHADER, image_normal,
-    VG_TRUE,  0, 0, 3, 1, 0, 0},
+    VG_TRUE,  0, 0, 3, 1, 0, 2},
    {VEGA_IMAGE_MULTIPLY_SHADER, image_multiply,
     VG_TRUE,  0, 0, 3, 1, 0, 2},
    {VEGA_IMAGE_STENCIL_SHADER, image_stencil,
@@ -497,19 +542,26 @@ static const struct shader_asm_info shaders_image_asm[] = {
 
 static const struct shader_asm_info shaders_color_transform_asm[] = {
    {VEGA_COLOR_TRANSFORM_SHADER, color_transform,
-    VG_FALSE, 0, 4, 0, 0, 0, 2}
+    VG_FALSE, 0, 4, 0, 0, 0, 3}
+};
+
+static const struct shader_asm_info shaders_alpha_asm[] = {
+   {VEGA_ALPHA_NORMAL_SHADER, alpha_normal,
+    VG_FALSE, 0, 0, 0, 0, 0, 2},
+   {VEGA_ALPHA_PER_CHANNEL_SHADER, alpha_per_channel,
+    VG_FALSE, 0, 0, 0, 0, 0, 2}
 };
 
 /* extra blend modes */
 static const struct shader_asm_info shaders_blend_asm[] = {
    {VEGA_BLEND_MULTIPLY_SHADER, blend_multiply,
-    VG_TRUE,  3, 1, 2, 1, 0, 4},
+    VG_TRUE,  3, 1, 2, 1, 0, 5},
    {VEGA_BLEND_SCREEN_SHADER, blend_screen,
-    VG_TRUE,  3, 1, 2, 1, 0, 4},
+    VG_TRUE,  3, 1, 2, 1, 0, 5},
    {VEGA_BLEND_DARKEN_SHADER, blend_darken,
-    VG_TRUE,  3, 1, 2, 1, 0, 4},
+    VG_TRUE,  3, 1, 2, 1, 0, 5},
    {VEGA_BLEND_LIGHTEN_SHADER, blend_lighten,
-    VG_TRUE,  3, 1, 2, 1, 0, 4},
+    VG_TRUE,  3, 1, 2, 1, 0, 5},
 };
 
 static const struct shader_asm_info shaders_mask_asm[] = {
index 20ced813b4b9a7d719c96a96beea4b4885c235c0..8577d21efe41571ac1c92ef40e2cb06c03906b77 100644 (file)
@@ -209,6 +209,7 @@ static void setup_shader_program(struct shader *shader)
    VGint shader_id = 0;
    VGBlendMode blend_mode = ctx->state.vg.blend_mode;
    VGboolean black_white = is_format_bw(shader);
+   VGboolean advanced_blend;
 
    /* 1st stage: fill */
    if (!shader->drawing_image ||
@@ -257,22 +258,47 @@ static void setup_shader_program(struct shader *shader)
 
    switch(blend_mode) {
    case VG_BLEND_MULTIPLY:
-      shader_id |= VEGA_BLEND_MULTIPLY_SHADER;
-      break;
    case VG_BLEND_SCREEN:
-      shader_id |= VEGA_BLEND_SCREEN_SHADER;
-      break;
    case VG_BLEND_DARKEN:
-      shader_id |= VEGA_BLEND_DARKEN_SHADER;
-      break;
    case VG_BLEND_LIGHTEN:
-      shader_id |= VEGA_BLEND_LIGHTEN_SHADER;
+      advanced_blend = VG_TRUE;
       break;
    default:
       /* handled by pipe_blend_state */
+      advanced_blend = VG_FALSE;
       break;
    }
 
+   if (advanced_blend) {
+      if (shader->drawing_image && shader->image_mode == VG_DRAW_IMAGE_STENCIL)
+         shader_id |= VEGA_ALPHA_PER_CHANNEL_SHADER;
+      else
+         shader_id |= VEGA_ALPHA_NORMAL_SHADER;
+
+      switch(blend_mode) {
+      case VG_BLEND_MULTIPLY:
+         shader_id |= VEGA_BLEND_MULTIPLY_SHADER;
+         break;
+      case VG_BLEND_SCREEN:
+         shader_id |= VEGA_BLEND_SCREEN_SHADER;
+         break;
+      case VG_BLEND_DARKEN:
+         shader_id |= VEGA_BLEND_DARKEN_SHADER;
+         break;
+      case VG_BLEND_LIGHTEN:
+         shader_id |= VEGA_BLEND_LIGHTEN_SHADER;
+         break;
+      default:
+         assert(0);
+         break;
+      }
+   }
+   else {
+      /* update alpha of the source */
+      if (shader->drawing_image && shader->image_mode == VG_DRAW_IMAGE_STENCIL)
+         shader_id |= VEGA_ALPHA_PER_CHANNEL_SHADER;
+   }
+
    if (shader->masking)
       shader_id |= VEGA_MASK_SHADER;
 
index 76bac5139f412584123f66215613eb8d501de504..e9873eb18b29b5e56496f05dcd39b16590d0a357 100644 (file)
  * 1) Paint generation (color/gradient/pattern)
  * 2) Image composition (normal/multiply/stencil)
  * 3) Color transform
- * 4) Extended blend (multiply/screen/darken/lighten)
- * 5) Mask
- * 6) Premultiply/Unpremultiply
- * 7) Color transform (to black and white)
+ * 4) Per-channel alpha generation
+ * 5) Extended blend (multiply/screen/darken/lighten)
+ * 6) Mask
+ * 7) Premultiply/Unpremultiply
+ * 8) Color transform (to black and white)
  */
-#define SHADER_STAGES 7
+#define SHADER_STAGES 8
 
 struct cached_shader {
    void *driver_shader;
@@ -301,6 +302,20 @@ create_shader(struct pipe_context *pipe,
    }
 
    /* fourth stage */
+   sh = SHADERS_GET_ALPHA_SHADER(id);
+   switch (sh) {
+   case VEGA_ALPHA_NORMAL_SHADER:
+   case VEGA_ALPHA_PER_CHANNEL_SHADER:
+      shaders[idx] = &shaders_alpha_asm[
+         (sh >> SHADERS_ALPHA_SHIFT) - 1];
+      assert(shaders[idx]->id == sh);
+      idx++;
+      break;
+   default:
+      break;
+   }
+
+   /* fifth stage */
    sh = SHADERS_GET_BLEND_SHADER(id);
    switch (sh) {
    case VEGA_BLEND_MULTIPLY_SHADER:
@@ -315,7 +330,7 @@ create_shader(struct pipe_context *pipe,
       break;
    }
 
-   /* fifth stage */
+   /* sixth stage */
    sh = SHADERS_GET_MASK_SHADER(id);
    switch (sh) {
    case VEGA_MASK_SHADER:
@@ -327,7 +342,7 @@ create_shader(struct pipe_context *pipe,
       break;
    }
 
-   /* sixth stage */
+   /* seventh stage */
    sh = SHADERS_GET_PREMULTIPLY_SHADER(id);
    switch (sh) {
    case VEGA_PREMULTIPLY_SHADER:
@@ -341,7 +356,7 @@ create_shader(struct pipe_context *pipe,
       break;
    }
 
-   /* seventh stage */
+   /* eighth stage */
    sh = SHADERS_GET_BW_SHADER(id);
    switch (sh) {
    case VEGA_BW_SHADER:
index 008e4f5b94b19e67009eba474a83fafd9d495f3c..9265c547ed3ece606bf18747001a27d2fa8aed97 100644 (file)
@@ -36,6 +36,7 @@ struct shaders_cache;
 #define _SHADERS_PAINT_BITS            3
 #define _SHADERS_IMAGE_BITS            2
 #define _SHADERS_COLOR_TRANSFORM_BITS  1
+#define _SHADERS_ALPHA_BITS            2
 #define _SHADERS_BLEND_BITS            3
 #define _SHADERS_MASK_BITS             1
 #define _SHADERS_PREMULTIPLY_BITS      2
@@ -44,7 +45,8 @@ struct shaders_cache;
 #define SHADERS_PAINT_SHIFT           (0)
 #define SHADERS_IMAGE_SHIFT           (SHADERS_PAINT_SHIFT + _SHADERS_PAINT_BITS)
 #define SHADERS_COLOR_TRANSFORM_SHIFT (SHADERS_IMAGE_SHIFT + _SHADERS_IMAGE_BITS)
-#define SHADERS_BLEND_SHIFT           (SHADERS_COLOR_TRANSFORM_SHIFT + _SHADERS_COLOR_TRANSFORM_BITS)
+#define SHADERS_ALPHA_SHIFT           (SHADERS_COLOR_TRANSFORM_SHIFT + _SHADERS_COLOR_TRANSFORM_BITS)
+#define SHADERS_BLEND_SHIFT           (SHADERS_ALPHA_SHIFT + _SHADERS_ALPHA_BITS)
 #define SHADERS_MASK_SHIFT            (SHADERS_BLEND_SHIFT + _SHADERS_BLEND_BITS)
 #define SHADERS_PREMULTIPLY_SHIFT     (SHADERS_MASK_SHIFT + _SHADERS_MASK_BITS)
 #define SHADERS_BW_SHIFT              (SHADERS_PREMULTIPLY_SHIFT + _SHADERS_PREMULTIPLY_BITS)
@@ -55,6 +57,7 @@ struct shaders_cache;
 #define SHADERS_GET_PAINT_SHADER(id)           _SHADERS_GET_STAGE(PAINT, id)
 #define SHADERS_GET_IMAGE_SHADER(id)           _SHADERS_GET_STAGE(IMAGE, id)
 #define SHADERS_GET_COLOR_TRANSFORM_SHADER(id) _SHADERS_GET_STAGE(COLOR_TRANSFORM, id)
+#define SHADERS_GET_ALPHA_SHADER(id)           _SHADERS_GET_STAGE(ALPHA, id)
 #define SHADERS_GET_BLEND_SHADER(id)           _SHADERS_GET_STAGE(BLEND, id)
 #define SHADERS_GET_MASK_SHADER(id)            _SHADERS_GET_STAGE(MASK, id)
 #define SHADERS_GET_PREMULTIPLY_SHADER(id)     _SHADERS_GET_STAGE(PREMULTIPLY, id)
@@ -73,6 +76,9 @@ enum VegaShaderType {
 
    VEGA_COLOR_TRANSFORM_SHADER    = 1 <<  SHADERS_COLOR_TRANSFORM_SHIFT,
 
+   VEGA_ALPHA_NORMAL_SHADER       = 1 <<  SHADERS_ALPHA_SHIFT,
+   VEGA_ALPHA_PER_CHANNEL_SHADER  = 2 <<  SHADERS_ALPHA_SHIFT,
+
    VEGA_BLEND_MULTIPLY_SHADER     = 1 << SHADERS_BLEND_SHIFT,
    VEGA_BLEND_SCREEN_SHADER       = 2 << SHADERS_BLEND_SHIFT,
    VEGA_BLEND_DARKEN_SHADER       = 3 << SHADERS_BLEND_SHIFT,