Shader: Switch type<subtype> to type[subtype]

This commit is contained in:
Jérôme Leclercq
2022-01-26 19:24:46 +01:00
parent 29a01e975c
commit e6951d54a5
27 changed files with 506 additions and 510 deletions

View File

@@ -431,7 +431,7 @@ namespace Nz::ShaderAst
vectorComponentCount = std::get<VectorType>(GetExpressionType(*vectorExpr)).componentCount;
}
// cast expression (turn fromMatrix[i] to vec3<f32>(fromMatrix[i]))
// cast expression (turn fromMatrix[i] to vec3[f32](fromMatrix[i]))
ExpressionPtr castExpr;
if (vectorComponentCount != targetMatrixType.rowCount)
{
@@ -1967,7 +1967,7 @@ namespace Nz::ShaderAst
{
const ExpressionType& type = GetExpressionType(*node.parameters.front());
if (type != ExpressionType{ VectorType{ 3, PrimitiveType::Float32 } })
throw AstError{ "CrossProduct only works with vec3<f32> expressions" };
throw AstError{ "CrossProduct only works with vec3[f32] expressions" };
node.cachedExpressionType = type;
break;

View File

@@ -126,7 +126,7 @@ namespace Nz
void LangWriter::Append(const ShaderAst::ArrayType& type)
{
Append("[", type.containedType->type, "; ");
Append("array[", type.containedType->type, ", ");
if (type.length.IsResultingValue())
Append(type.length.GetResultingValue());
@@ -164,7 +164,7 @@ namespace Nz
Append(matrixType.rowCount);
}
Append("<", matrixType.type, ">");
Append("[", matrixType.type, "]");
}
void LangWriter::Append(ShaderAst::PrimitiveType type)
@@ -192,7 +192,7 @@ namespace Nz
case ImageType::Cubemap: Append("Cube"); break;
}
Append("<", samplerType.sampledType, ">");
Append("[", samplerType.sampledType, "]");
}
void LangWriter::Append(const ShaderAst::StructType& structType)
@@ -203,17 +203,17 @@ namespace Nz
void LangWriter::Append(const ShaderAst::UniformType& uniformType)
{
Append("uniform<");
Append("uniform[");
std::visit([&](auto&& arg)
{
Append(arg);
}, uniformType.containedType);
Append(">");
Append("]");
}
void LangWriter::Append(const ShaderAst::VectorType& vecType)
{
Append("vec", vecType.componentCount, "<", vecType.type, ">");
Append("vec", vecType.componentCount, "[", vecType.type, "]");
}
void LangWriter::Append(ShaderAst::NoType)
@@ -732,15 +732,15 @@ namespace Nz
else if constexpr (std::is_same_v<T, float> || std::is_same_v<T, Int32> || std::is_same_v<T, UInt32>)
Append(std::to_string(arg));
else if constexpr (std::is_same_v<T, Vector2f>)
Append("vec2<f32>(" + std::to_string(arg.x) + ", " + std::to_string(arg.y) + ")");
Append("vec2[f32](" + std::to_string(arg.x) + ", " + std::to_string(arg.y) + ")");
else if constexpr (std::is_same_v<T, Vector2i32>)
Append("vec2<i32>(" + std::to_string(arg.x) + ", " + std::to_string(arg.y) + ")");
else if constexpr (std::is_same_v<T, Vector3f>)
Append("vec3<f32>(" + std::to_string(arg.x) + ", " + std::to_string(arg.y) + ", " + std::to_string(arg.z) + ")");
Append("vec3[f32](" + std::to_string(arg.x) + ", " + std::to_string(arg.y) + ", " + std::to_string(arg.z) + ")");
else if constexpr (std::is_same_v<T, Vector3i32>)
Append("vec3<i32>(" + std::to_string(arg.x) + ", " + std::to_string(arg.y) + ", " + std::to_string(arg.z) + ")");
else if constexpr (std::is_same_v<T, Vector4f>)
Append("vec4<f32>(" + std::to_string(arg.x) + ", " + std::to_string(arg.y) + ", " + std::to_string(arg.z) + ", " + std::to_string(arg.w) + ")");
Append("vec4[f32](" + std::to_string(arg.x) + ", " + std::to_string(arg.y) + ", " + std::to_string(arg.z) + ", " + std::to_string(arg.w) + ")");
else if constexpr (std::is_same_v<T, Vector4i32>)
Append("vec4<i32>(" + std::to_string(arg.x) + ", " + std::to_string(arg.y) + ", " + std::to_string(arg.z) + ", " + std::to_string(arg.w) + ")");
else

View File

@@ -207,7 +207,25 @@ namespace Nz::ShaderLang
}
//FIXME: Handle this better
if (identifier == "mat4")
if (identifier == "array")
{
Consume();
Expect(Advance(), TokenType::OpenSquareBracket); //< [
ShaderAst::ArrayType arrayType;
arrayType.containedType = std::make_unique<ShaderAst::ContainedType>();
arrayType.containedType->type = ParseType();
Expect(Advance(), TokenType::Comma); //< ,
arrayType.length = ParseExpression();
Expect(Advance(), TokenType::ClosingSquareBracket); //< ]
return arrayType;
}
else if (identifier == "mat4")
{
Consume();
@@ -215,9 +233,9 @@ namespace Nz::ShaderLang
matrixType.columnCount = 4;
matrixType.rowCount = 4;
Expect(Advance(), TokenType::LessThan); //< '<'
Expect(Advance(), TokenType::OpenSquareBracket); //< [
matrixType.type = ParsePrimitiveType();
Expect(Advance(), TokenType::GreaterThan); //< '>'
Expect(Advance(), TokenType::ClosingSquareBracket); //< ]
return matrixType;
}
@@ -229,9 +247,9 @@ namespace Nz::ShaderLang
matrixType.columnCount = 3;
matrixType.rowCount = 3;
Expect(Advance(), TokenType::LessThan); //< '<'
Expect(Advance(), TokenType::OpenSquareBracket); //< [
matrixType.type = ParsePrimitiveType();
Expect(Advance(), TokenType::GreaterThan); //< '>'
Expect(Advance(), TokenType::ClosingSquareBracket); //< ]
return matrixType;
}
@@ -243,9 +261,9 @@ namespace Nz::ShaderLang
matrixType.columnCount = 2;
matrixType.rowCount = 2;
Expect(Advance(), TokenType::LessThan); //< '<'
Expect(Advance(), TokenType::OpenSquareBracket); //< [
matrixType.type = ParsePrimitiveType();
Expect(Advance(), TokenType::GreaterThan); //< '>'
Expect(Advance(), TokenType::ClosingSquareBracket); //< ]
return matrixType;
}
@@ -256,9 +274,9 @@ namespace Nz::ShaderLang
ShaderAst::SamplerType samplerType;
samplerType.dim = ImageType::E2D;
Expect(Advance(), TokenType::LessThan); //< '<'
Expect(Advance(), TokenType::OpenSquareBracket); //< [
samplerType.sampledType = ParsePrimitiveType();
Expect(Advance(), TokenType::GreaterThan); //< '>'
Expect(Advance(), TokenType::ClosingSquareBracket); //< ]
return samplerType;
}
@@ -269,9 +287,9 @@ namespace Nz::ShaderLang
ShaderAst::SamplerType samplerType;
samplerType.dim = ImageType::Cubemap;
Expect(Advance(), TokenType::LessThan); //< '<'
Expect(Advance(), TokenType::OpenSquareBracket); //< [
samplerType.sampledType = ParsePrimitiveType();
Expect(Advance(), TokenType::GreaterThan); //< '>'
Expect(Advance(), TokenType::ClosingSquareBracket); //< ]
return samplerType;
}
@@ -281,9 +299,9 @@ namespace Nz::ShaderLang
ShaderAst::UniformType uniformType;
Expect(Advance(), TokenType::LessThan); //< '<'
Expect(Advance(), TokenType::OpenSquareBracket); //< [
uniformType.containedType = ShaderAst::IdentifierType{ ParseIdentifierAsName() };
Expect(Advance(), TokenType::GreaterThan); //< '>'
Expect(Advance(), TokenType::ClosingSquareBracket); //< ]
return uniformType;
}
@@ -294,9 +312,9 @@ namespace Nz::ShaderLang
ShaderAst::VectorType vectorType;
vectorType.componentCount = 2;
Expect(Advance(), TokenType::LessThan); //< '<'
Expect(Advance(), TokenType::OpenSquareBracket); //< [
vectorType.type = ParsePrimitiveType();
Expect(Advance(), TokenType::GreaterThan); //< '>'
Expect(Advance(), TokenType::ClosingSquareBracket); //< ]
return vectorType;
}
@@ -307,9 +325,9 @@ namespace Nz::ShaderLang
ShaderAst::VectorType vectorType;
vectorType.componentCount = 3;
Expect(Advance(), TokenType::LessThan); //< '<'
Expect(Advance(), TokenType::OpenSquareBracket); //< [
vectorType.type = ParsePrimitiveType();
Expect(Advance(), TokenType::GreaterThan); //< '>'
Expect(Advance(), TokenType::ClosingSquareBracket); //< ]
return vectorType;
}
@@ -320,9 +338,9 @@ namespace Nz::ShaderLang
ShaderAst::VectorType vectorType;
vectorType.componentCount = 4;
Expect(Advance(), TokenType::LessThan); //< '<'
Expect(Advance(), TokenType::OpenSquareBracket); //< [
vectorType.type = ParsePrimitiveType();
Expect(Advance(), TokenType::GreaterThan); //< '>'
Expect(Advance(), TokenType::ClosingSquareBracket); //< ]
return vectorType;
}
@@ -1337,24 +1355,6 @@ namespace Nz::ShaderLang
}
}
ShaderAst::ExpressionType Parser::ParseArrayType()
{
ShaderAst::ArrayType arrayType;
Expect(Advance(), TokenType::OpenSquareBracket);
arrayType.containedType = std::make_unique<ShaderAst::ContainedType>();
arrayType.containedType->type = ParseType();
Expect(Advance(), TokenType::Semicolon);
arrayType.length = ParseExpression();
Expect(Advance(), TokenType::ClosingSquareBracket);
return arrayType;
}
ShaderAst::AttributeType Parser::ParseIdentifierAsAttributeType()
{
const Token& identifierToken = Expect(Advance(), TokenType::Identifier);
@@ -1402,9 +1402,6 @@ namespace Nz::ShaderLang
return ShaderAst::NoType{};
}
if (Peek().type == TokenType::OpenSquareBracket)
return ParseArrayType();
const Token& identifierToken = Expect(Peek(), TokenType::Identifier);
const std::string& identifier = std::get<std::string>(identifierToken.data);