]> git.tdb.fi Git - libs/gl.git/blobdiff - source/core/blend.cpp
Add color write mask to blend state
[libs/gl.git] / source / core / blend.cpp
index 324440d32e9362ad39c33c3e7afafeb878aed37b..8ceb23829a76894e978dfad03a1531c3421c215e 100644 (file)
@@ -1,6 +1,7 @@
 #include <msp/gl/extensions/ext_blend_minmax.h>
 #include <msp/gl/extensions/ext_blend_subtract.h>
 #include <msp/strings/format.h>
+#include <msp/strings/utils.h>
 #include "blend.h"
 
 using namespace std;
@@ -13,7 +14,8 @@ Blend::Blend():
        equation(ADD),
        src_factor(ONE),
        dst_factor(ZERO),
-       constant(0.0f, 0.0f, 0.0f, 0.0f)
+       constant(0.0f, 0.0f, 0.0f, 0.0f),
+       write_mask(WRITE_ALL)
 { }
 
 Blend::Blend(BlendFactor sf, BlendFactor df):
@@ -21,7 +23,8 @@ Blend::Blend(BlendFactor sf, BlendFactor df):
        equation(ADD),
        src_factor(sf),
        dst_factor(df),
-       constant(0.0f, 0.0f, 0.0f, 0.0f)
+       constant(0.0f, 0.0f, 0.0f, 0.0f),
+       write_mask(WRITE_ALL)
 { }
 
 Blend::Blend(BlendEquation e, BlendFactor sf, BlendFactor df):
@@ -29,7 +32,8 @@ Blend::Blend(BlendEquation e, BlendFactor sf, BlendFactor df):
        equation(e),
        src_factor(sf),
        dst_factor(df),
-       constant(0.0f, 0.0f, 0.0f, 0.0f)
+       constant(0.0f, 0.0f, 0.0f, 0.0f),
+       write_mask(WRITE_ALL)
 { }
 
 
@@ -182,5 +186,47 @@ void operator<<(LexicalConverter &conv, BlendFactor factor)
        }
 }
 
+void operator>>(const LexicalConverter &conv, ColorWriteMask &mask)
+{
+       ColorWriteMask result = WRITE_NONE;
+       for(const string &p: split(conv.get(), '_'))
+       {
+               if(p=="ALL")
+                       result = result|WRITE_ALL;
+               else if(p=="RED")
+                       result = result|WRITE_RED;
+               else if(p=="GREEN")
+                       result = result|WRITE_GREEN;
+               else if(p=="BLUE")
+                       result = result|WRITE_BLUE;
+               else if(p=="ALPHA")
+                       result = result|WRITE_ALPHA;
+               else
+                       throw lexical_error(format("conversion of '%s' to ColorWriteMask", conv.get()));
+       }
+       mask = result;
+}
+
+void operator<<(LexicalConverter &conv, ColorWriteMask mask)
+{
+       if(mask==WRITE_ALL)
+               conv.result("ALL");
+       else if(mask&~WRITE_ALL)
+               conv.result(format("ColorWriteMask(%#x)", static_cast<int>(mask)));
+       else
+       {
+               string result;
+               if(mask&WRITE_RED)
+                       result = "RED";
+               if(mask&WRITE_GREEN)
+                       append(result, "_", "GREEN");
+               if(mask&WRITE_BLUE)
+                       append(result, "_", "BLUE");
+               if(mask&WRITE_ALPHA)
+                       append(result, "_", "ALPHA");
+               conv.result(result);
+       }
+}
+
 } // namespace GL
 } // namespace Msp