Revert "Revert "Add support for image load to SkSL""
authorBrian Salomon <bsalomon@google.com>
Wed, 16 Nov 2016 17:06:01 +0000 (12:06 -0500)
committerSkia Commit-Bot <skia-commit-bot@chromium.org>
Wed, 16 Nov 2016 19:31:11 +0000 (19:31 +0000)
This reverts commit cb115bdeed5898ded3fdbe572a14616cff809b7c.

GOLD_TRYBOT_URL= https://gold.skia.org/search?issue=4900

Change-Id: Ibcb381bae83d0cfc1a1226be90792061d401426a
Reviewed-on: https://skia-review.googlesource.com/4900
Reviewed-by: Brian Salomon <bsalomon@google.com>
Commit-Queue: Brian Salomon <bsalomon@google.com>

src/sksl/SkSLCompiler.cpp
src/sksl/SkSLContext.h
src/sksl/SkSLGLSLCodeGenerator.cpp
src/sksl/SkSLGLSLCodeGenerator.h
src/sksl/SkSLParser.cpp
src/sksl/ast/SkSLASTLayout.h
src/sksl/ir/SkSLLayout.h
src/sksl/sksl.include

index 4794759..510d610 100644 (file)
@@ -113,6 +113,9 @@ Compiler::Compiler()
 
     ADD_TYPE(ISampler2D);
 
+    ADD_TYPE(Image2D);
+    ADD_TYPE(IImage2D);
+
     ADD_TYPE(GSampler1D);
     ADD_TYPE(GSampler2D);
     ADD_TYPE(GSampler3D);
index 80ad7fb..a42a4cc 100644 (file)
@@ -78,8 +78,14 @@ public:
     , fSampler1DArrayShadow_Type(new Type("sampler1DArrayShadow"))
     , fSampler2DArrayShadow_Type(new Type("sampler2DArrayShadow"))
     , fSamplerCubeArrayShadow_Type(new Type("samplerCubeArrayShadow"))
+
     // Related to below FIXME, gsampler*s don't currently expand to cover integer case.
     , fISampler2D_Type(new Type("isampler2D", SpvDim2D, false, false, false, true))
+
+    // FIXME express these as "gimage2D" that expand to image2D, iimage2D, and uimage2D.
+    , fImage2D_Type(new Type("image2D", SpvDim2D, false, false, false, true))
+    , fIImage2D_Type(new Type("iimage2D", SpvDim2D, false, false, false, true))
+
     // FIXME figure out what we're supposed to do with the gsampler et al. types)
     , fGSampler1D_Type(new Type("$gsampler1D", static_type(*fSampler1D_Type)))
     , fGSampler2D_Type(new Type("$gsampler2D", static_type(*fSampler2D_Type)))
@@ -195,8 +201,12 @@ public:
     const std::unique_ptr<Type> fSampler2DArrayShadow_Type;
     const std::unique_ptr<Type> fSamplerCubeArrayShadow_Type;
 
+
     const std::unique_ptr<Type> fISampler2D_Type;
 
+    const std::unique_ptr<Type> fImage2D_Type;
+    const std::unique_ptr<Type> fIImage2D_Type;
+
     const std::unique_ptr<Type> fGSampler1D_Type;
     const std::unique_ptr<Type> fGSampler2D_Type;
     const std::unique_ptr<Type> fGSampler3D_Type;
index c6d2e6e..1252f86 100644 (file)
@@ -474,6 +474,12 @@ void GLSLCodeGenerator::writeVarDeclarations(const VarDeclarations& decl, bool g
             this->write(" = ");
             this->writeExpression(*var.fValue, kTopLevel_Precedence);
         }
+        if (!fFoundImageDecl && var.fVar->fType == *fContext.fImage2D_Type) {
+            if (fCaps.imageLoadStoreExtensionString()) {
+                fHeader << "#extension " << fCaps.imageLoadStoreExtensionString() << " : require\n";
+            }
+            fFoundImageDecl = true;
+        }
     }
     this->write(";");
 }
index 5ed6104..16d6192 100644 (file)
@@ -169,6 +169,7 @@ private:
     std::vector<const Type*> fWrittenStructs;
     // true if we have run into usages of dFdx / dFdy
     bool fFoundDerivatives = false;
+    bool fFoundImageDecl = false;
 };
 
 }
index 324bd41..2be664d 100644 (file)
@@ -537,11 +537,12 @@ ASTLayout Parser::layout() {
     bool originUpperLeft = false;
     bool overrideCoverage = false;
     bool blendSupportAllEquations = false;
+    ASTLayout::Format format = ASTLayout::Format::kUnspecified;
     if (this->peek().fKind == Token::LAYOUT) {
         this->nextToken();
         if (!this->expect(Token::LPAREN, "'('")) {
             return ASTLayout(location, binding, index, set, builtin, originUpperLeft,
-                             overrideCoverage, blendSupportAllEquations);
+                             overrideCoverage, blendSupportAllEquations, format);
         }
         for (;;) {
             Token t = this->nextToken();
@@ -561,6 +562,8 @@ ASTLayout Parser::layout() {
                 overrideCoverage = true;
             } else if (t.fText == "blend_support_all_equations") {
                 blendSupportAllEquations = true;
+            } else if (ASTLayout::ReadFormat(t.fText, &format)) {
+               // AST::ReadFormat stored the result in 'format'.
             } else {
                 this->error(t.fPosition, ("'" + t.fText + 
                                           "' is not a valid layout qualifier").c_str());
@@ -575,7 +578,7 @@ ASTLayout Parser::layout() {
         }
     }
     return ASTLayout(location, binding, index, set, builtin, originUpperLeft, overrideCoverage,
-                     blendSupportAllEquations);
+                     blendSupportAllEquations, format);
 }
 
 /* layout? (UNIFORM | CONST | IN | OUT | INOUT | LOWP | MEDIUMP | HIGHP | FLAT | NOPERSPECTIVE)* */
index 515eb2b..ae3c3b6 100644 (file)
@@ -19,9 +19,67 @@ namespace SkSL {
  * layout (location = 0) int x;
  */
 struct ASTLayout : public ASTNode {
-    // For all parameters, a -1 means no value
+    // These are used by images in GLSL. We only support a subset of what GL supports.
+    enum class Format {
+        kUnspecified = -1,
+        kRGBA32F,
+        kR32F,
+        kRGBA16F,
+        kR16F,
+        kRGBA8,
+        kR8,
+        kRGBA8I,
+        kR8I,
+    };
+
+    static const char* FormatToStr(Format format) {
+        switch (format) {
+            case Format::kUnspecified:  return "";
+            case Format::kRGBA32F:      return "rgba32f";
+            case Format::kR32F:         return "r32f";
+            case Format::kRGBA16F:      return "rgba16f";
+            case Format::kR16F:         return "r16f";
+            case Format::kRGBA8:        return "rgba8";
+            case Format::kR8:           return "r8";
+            case Format::kRGBA8I:       return "rgba8i";
+            case Format::kR8I:          return "r8i";
+        }
+        SkFAIL("Unexpected format");
+        return "";
+    }
+
+    static bool ReadFormat(std::string str, Format* format) {
+        if (str == "rgba32f") {
+            *format = Format::kRGBA32F;
+            return true;
+        } else if (str == "r32f") {
+            *format = Format::kR32F;
+            return true;
+        } else if (str == "rgba16f") {
+            *format = Format::kRGBA16F;
+            return true;
+        } else if (str == "r16f") {
+            *format = Format::kR16F;
+            return true;
+        } else if (str == "rgba8") {
+            *format = Format::kRGBA8;
+            return true;
+        } else if (str == "r8") {
+            *format = Format::kR8;
+            return true;
+        } else if (str == "rgba8i") {
+            *format = Format::kRGBA8I;
+            return true;
+        } else if (str == "r8i") {
+            *format = Format::kR8I;
+            return true;
+        }
+        return false;
+    }
+
+    // For int parameters, a -1 means no value
     ASTLayout(int location, int binding, int index, int set, int builtin, bool originUpperLeft,
-              bool overrideCoverage, bool blendSupportAllEquations)
+              bool overrideCoverage, bool blendSupportAllEquations, Format format)
     : fLocation(location)
     , fBinding(binding)
     , fIndex(index)
@@ -29,7 +87,8 @@ struct ASTLayout : public ASTNode {
     , fBuiltin(builtin)
     , fOriginUpperLeft(originUpperLeft)
     , fOverrideCoverage(overrideCoverage)
-    , fBlendSupportAllEquations(blendSupportAllEquations) {}
+    , fBlendSupportAllEquations(blendSupportAllEquations)
+    , fFormat(format) {}
 
     std::string description() const {
         std::string result;
@@ -66,6 +125,10 @@ struct ASTLayout : public ASTNode {
             result += separator + "blend_support_all_equations";
             separator = ", ";
         }
+        if (fFormat != Format::kUnspecified) {
+            result += separator + FormatToStr(fFormat);
+            separator = ", ";
+        }
         if (result.length() > 0) {
             result = "layout (" + result + ")";
         }
@@ -80,6 +143,7 @@ struct ASTLayout : public ASTNode {
     const bool fOriginUpperLeft;
     const bool fOverrideCoverage;
     const bool fBlendSupportAllEquations;
+    const Format fFormat;
 };
 
 } // namespace
index 24087d0..4cfd1a2 100644 (file)
@@ -24,10 +24,11 @@ struct Layout {
     , fBuiltin(layout.fBuiltin)
     , fOriginUpperLeft(layout.fOriginUpperLeft)
     , fOverrideCoverage(layout.fOverrideCoverage)
-    , fBlendSupportAllEquations(layout.fBlendSupportAllEquations) {}
+    , fBlendSupportAllEquations(layout.fBlendSupportAllEquations)
+    , fFormat(layout.fFormat) {}
 
     Layout(int location, int binding, int index, int set, int builtin, bool originUpperLeft,
-           bool overrideCoverage, bool blendSupportAllEquations)
+           bool overrideCoverage, bool blendSupportAllEquations, ASTLayout::Format format)
     : fLocation(location)
     , fBinding(binding)
     , fIndex(index)
@@ -35,7 +36,19 @@ struct Layout {
     , fBuiltin(builtin)
     , fOriginUpperLeft(originUpperLeft)
     , fOverrideCoverage(overrideCoverage)
-    , fBlendSupportAllEquations(blendSupportAllEquations) {}
+    , fBlendSupportAllEquations(blendSupportAllEquations)
+    , fFormat(format) {}
+
+    Layout() 
+    : fLocation(-1)
+    , fBinding(-1)
+    , fIndex(-1)
+    , fSet(-1)
+    , fBuiltin(-1)
+    , fOriginUpperLeft(false)
+    , fOverrideCoverage(false)
+    , fBlendSupportAllEquations(false)
+    , fFormat(ASTLayout::Format::kUnspecified) {}
 
     std::string description() const {
         std::string result;
@@ -72,6 +85,10 @@ struct Layout {
             result += separator + "blend_support_all_equations";
             separator = ", ";
         }
+        if (ASTLayout::Format::kUnspecified != fFormat) {
+            result += separator + ASTLayout::FormatToStr(fFormat);
+            separator = ", ";
+        }
         if (result.length() > 0) {
             result = "layout (" + result + ")";
         }
@@ -86,7 +103,8 @@ struct Layout {
                fBuiltin                  == other.fBuiltin &&
                fOriginUpperLeft          == other.fOriginUpperLeft &&
                fOverrideCoverage         == other.fOverrideCoverage &&
-               fBlendSupportAllEquations == other.fBlendSupportAllEquations;
+               fBlendSupportAllEquations == other.fBlendSupportAllEquations &&
+               fFormat                   == other.fFormat;
     }
 
     bool operator!=(const Layout& other) const {
@@ -103,6 +121,7 @@ struct Layout {
     bool fOriginUpperLeft;
     bool fOverrideCoverage;
     bool fBlendSupportAllEquations;
+    ASTLayout::Format fFormat;
 };
 
 } // namespace
index 6458a15..83c6aed 100644 (file)
@@ -537,8 +537,10 @@ int atomicExchange(inout int mem, int data);
 uint atomicCompSwap(inout uint mem, uint compare, uint data);
 int atomicCompSwap(inout int mem, int compare, int data);
 */
-// section 8.12 Image Functions will go here if and when we add support for them
-
+// section 8.12 Additional Image Functions will go here if and when we add
+// support for them
+vec4 imageLoad(image2D image, ivec2 P);
+ivec4 imageLoad(iimage2D image, ivec2 P);
 $genType dFdx($genType p);
 $genType dFdy($genType p);
 float interpolateAtSample(float interpolant, int sample);