From c2eaf37f01c49eff82ac193d4ad9a20a58221b09 Mon Sep 17 00:00:00 2001
From: Rhys Weatherley <rhys.weatherley@nokia.com>
Date: Fri, 26 Nov 2010 13:51:45 +1000
Subject: [PATCH] Add GLSL sampler types to semantic analysis phase

---
 src/libs/glsl/glsl.h           |  1 +
 src/libs/glsl/glslengine.cpp   |  5 ++++
 src/libs/glsl/glslengine.h     |  2 ++
 src/libs/glsl/glslsemantic.cpp | 44 ++++++++++++++++++++++++++++++++++
 src/libs/glsl/glsltype.h       |  1 +
 src/libs/glsl/glsltypes.cpp    | 17 +++++++++++++
 src/libs/glsl/glsltypes.h      | 16 +++++++++++++
 7 files changed, 86 insertions(+)

diff --git a/src/libs/glsl/glsl.h b/src/libs/glsl/glsl.h
index 73f35be0c1e..bb43c1d009f 100644
--- a/src/libs/glsl/glsl.h
+++ b/src/libs/glsl/glsl.h
@@ -62,6 +62,7 @@ class IndexType;
 class VectorType;
 class MatrixType;
 class ArrayType;
+class SamplerType;
 
 // symbols
 class Symbol;
diff --git a/src/libs/glsl/glslengine.cpp b/src/libs/glsl/glslengine.cpp
index 6b11bc5f309..87c59e2f59c 100644
--- a/src/libs/glsl/glslengine.cpp
+++ b/src/libs/glsl/glslengine.cpp
@@ -30,6 +30,7 @@
 #include "glslengine.h"
 #include "glslsymbols.h"
 #include "glsltypes.h"
+#include "glslparser.h"
 
 using namespace GLSL;
 
@@ -144,6 +145,10 @@ const DoubleType *Engine::doubleType()
     return &t;
 }
 
+const SamplerType *Engine::samplerType(int kind)
+{
+    return _samplerTypes.intern(SamplerType(kind));
+}
 
 const VectorType *Engine::vectorType(const Type *elementType, int dimension)
 {
diff --git a/src/libs/glsl/glslengine.h b/src/libs/glsl/glslengine.h
index b89a07ba6bd..417f72e9422 100644
--- a/src/libs/glsl/glslengine.h
+++ b/src/libs/glsl/glslengine.h
@@ -106,6 +106,7 @@ public:
     const UIntType *uintType();
     const FloatType *floatType();
     const DoubleType *doubleType();
+    const SamplerType *samplerType(int kind);
     const VectorType *vectorType(const Type *elementType, int dimension);
     const MatrixType *matrixType(const Type *elementType, int columns, int rows);
 
@@ -126,6 +127,7 @@ private:
     QSet<QString> _identifiers;
     TypeTable<VectorType> _vectorTypes;
     TypeTable<MatrixType> _matrixTypes;
+    TypeTable<SamplerType> _samplerTypes;
     MemoryPool _pool;
     QList<DiagnosticMessage> _diagnosticMessages;
     QList<Symbol *> _symbols;
diff --git a/src/libs/glsl/glslsemantic.cpp b/src/libs/glsl/glslsemantic.cpp
index 5353581bfab..fc5829c2072 100644
--- a/src/libs/glsl/glslsemantic.cpp
+++ b/src/libs/glsl/glslsemantic.cpp
@@ -464,6 +464,50 @@ bool Semantic::visit(BasicTypeAST *ast)
         _type = _engine->matrixType(_engine->doubleType(), 4, 4);
         break;
 
+    // samplers
+    case Parser::T_SAMPLER1D:
+    case Parser::T_SAMPLER2D:
+    case Parser::T_SAMPLER3D:
+    case Parser::T_SAMPLERCUBE:
+    case Parser::T_SAMPLER1DSHADOW:
+    case Parser::T_SAMPLER2DSHADOW:
+    case Parser::T_SAMPLERCUBESHADOW:
+    case Parser::T_SAMPLER1DARRAY:
+    case Parser::T_SAMPLER2DARRAY:
+    case Parser::T_SAMPLER1DARRAYSHADOW:
+    case Parser::T_SAMPLER2DARRAYSHADOW:
+    case Parser::T_SAMPLERCUBEARRAY:
+    case Parser::T_SAMPLERCUBEARRAYSHADOW:
+    case Parser::T_SAMPLER2DRECT:
+    case Parser::T_SAMPLER2DRECTSHADOW:
+    case Parser::T_SAMPLERBUFFER:
+    case Parser::T_SAMPLER2DMS:
+    case Parser::T_SAMPLER2DMSARRAY:
+    case Parser::T_ISAMPLER1D:
+    case Parser::T_ISAMPLER2D:
+    case Parser::T_ISAMPLER3D:
+    case Parser::T_ISAMPLERCUBE:
+    case Parser::T_ISAMPLER1DARRAY:
+    case Parser::T_ISAMPLER2DARRAY:
+    case Parser::T_ISAMPLERCUBEARRAY:
+    case Parser::T_ISAMPLER2DRECT:
+    case Parser::T_ISAMPLERBUFFER:
+    case Parser::T_ISAMPLER2DMS:
+    case Parser::T_ISAMPLER2DMSARRAY:
+    case Parser::T_USAMPLER1D:
+    case Parser::T_USAMPLER2D:
+    case Parser::T_USAMPLER3D:
+    case Parser::T_USAMPLERCUBE:
+    case Parser::T_USAMPLER1DARRAY:
+    case Parser::T_USAMPLER2DARRAY:
+    case Parser::T_USAMPLERCUBEARRAY:
+    case Parser::T_USAMPLER2DRECT:
+    case Parser::T_USAMPLERBUFFER:
+    case Parser::T_USAMPLER2DMS:
+    case Parser::T_USAMPLER2DMSARRAY:
+        _type = _engine->samplerType(ast->token);
+        break;
+
     default:
         qDebug() << "unknown type:" << GLSLParserTable::spell[ast->token];
     }
diff --git a/src/libs/glsl/glsltype.h b/src/libs/glsl/glsltype.h
index 46ba3913edd..1159856b891 100644
--- a/src/libs/glsl/glsltype.h
+++ b/src/libs/glsl/glsltype.h
@@ -50,6 +50,7 @@ public:
     virtual const VectorType *asVectorType() const { return 0; }
     virtual const MatrixType *asMatrixType() const { return 0; }
     virtual const ArrayType *asArrayType() const { return 0; }
+    virtual const SamplerType *asSamplerType() const { return 0; }
 
     virtual const Struct *asStructType() const { return 0; }
     virtual const Function *asFunctionType() const { return 0; }
diff --git a/src/libs/glsl/glsltypes.cpp b/src/libs/glsl/glsltypes.cpp
index 3e53fa4f8c7..41c3b141490 100644
--- a/src/libs/glsl/glsltypes.cpp
+++ b/src/libs/glsl/glsltypes.cpp
@@ -373,3 +373,20 @@ Symbol *Function::find(const QString &name) const
     }
     return 0;
 }
+
+bool SamplerType::isEqualTo(const Type *other) const
+{
+    if (other) {
+        if (const SamplerType *samp = other->asSamplerType())
+            return _kind == samp->kind();
+    }
+    return false;
+}
+
+bool SamplerType::isLessThan(const Type *other) const
+{
+    Q_ASSERT(other != 0);
+    const SamplerType *samp = other->asSamplerType();
+    Q_ASSERT(samp != 0);
+    return _kind < samp->kind();
+}
diff --git a/src/libs/glsl/glsltypes.h b/src/libs/glsl/glsltypes.h
index 74e969f80fb..cabb60b47b5 100644
--- a/src/libs/glsl/glsltypes.h
+++ b/src/libs/glsl/glsltypes.h
@@ -233,6 +233,22 @@ private:
     QVector<Argument *> _arguments;
 };
 
+class GLSL_EXPORT SamplerType: public Type
+{
+public:
+    explicit SamplerType(int kind) : _kind(kind) {}
+
+    // Kind of sampler as a token code; e.g. T_SAMPLER2D.
+    int kind() const { return _kind; }
+
+    virtual const SamplerType *asSamplerType() const { return this; }
+    virtual bool isEqualTo(const Type *other) const;
+    virtual bool isLessThan(const Type *other) const;
+
+private:
+    int _kind;
+};
+
 } // end of namespace GLSL
 
 #endif // GLSLTYPES_H
-- 
GitLab