Commit 6daaa4fa by LoopDawg

HLSL: Add template style constructors for vector & matrix types

parent d02dc5d0
float PixelShaderFunction()
{
vector r00 = float4(1,2,3,4); // vector means float4
float4 r01 = vector(2,3,4,5); // vector means float4
vector<bool, 1> r12 = bool1(false);
vector<int, 1> r13 = int1(1);
vector<float, 1> r14 = float1(1);
vector<double, 1> r15 = double1(1);
vector<uint, 1> r16 = uint1(1);
vector<bool, 2> r20 = bool2(false, true);
vector<int, 2> r21 = int2(1,2);
vector<float, 2> r22 = float2(1,2);
vector<double, 2> r23 = double2(1,2);
vector<uint, 2> r24 = uint2(1,2);
vector<bool, 3> r30 = bool3(false, true, true);
vector<int, 3> r31 = int3(1,2,3);
vector<float, 3> r32 = float3(1,2,3);
vector<double, 3> r33 = double3(1,2,3);
vector<uint, 3> r34 = uint3(1,2,3);
vector<bool, 4> r40 = bool4(false, true, true, false);
vector<int, 4> r41 = int4(1,2,3,4);
vector<float, 4> r42 = float4(1,2,3,4);
vector<double, 4> r43 = double4(1,2,3,4);
vector<uint, 4> r44 = uint4(1,2,3,4);
matrix r50 = float4x4(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15); // matrix means float4x4
float4x4 r51 = matrix(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15); // matrix means float4x4
// matrix<bool, 2, 3> r60 = bool2x3(false, true, false, true, false, true); // TODO:
matrix<float, 2, 3> r61 = float2x3(1,2,3,4,5,6);
matrix<float, 3, 2> r62 = float3x2(1,2,3,4,5,6);
// matrix<float, 4, 1> r63 = float4x1(1,2,3,4); // TODO:
// matrix<float, 1, 4> r64 = float1x4(1,2,3,4); // TODO:
matrix<float, 4, 2> r65 = float4x2(1,2,3,4,5,6,7,8);
matrix<float, 4, 3> r66 = float4x3(1,2,3,4,5,6,7,8,9,10,11,12);
// TODO: bool mats
// TODO: int mats
return 0.0;
}
float PixelShaderFunction()
{
// TODO: All of the below should fail, although presently the first failure
// aborts compilation and the rest are skipped. Having a separate test for
// each would be cumbersome.
vector<void, 2> r00; // cannot declare vectors of voids
matrix<void, 2, 3> r01; // cannot declare matrices of voids
vector<float, 2, 3> r02; // too many parameters to vector
matrix<float, 2> r03; // not enough parameters to matrix
int three = 3;
vector<void, three> r04; // size must be a literal constant integer
matrix<void, three, three> r05; // size must be a literal constant integer
vector<vector<int, 3>, 3> r06; // type must be a simple scalar
vector<float3, 3> r07; // type must be a simple scalar
return 0.0;
}
...@@ -102,6 +102,7 @@ INSTANTIATE_TEST_CASE_P( ...@@ -102,6 +102,7 @@ INSTANTIATE_TEST_CASE_P(
{"hlsl.struct.frag", "PixelShaderFunction"}, {"hlsl.struct.frag", "PixelShaderFunction"},
{"hlsl.switch.frag", "PixelShaderFunction"}, {"hlsl.switch.frag", "PixelShaderFunction"},
{"hlsl.swizzle.frag", "PixelShaderFunction"}, {"hlsl.swizzle.frag", "PixelShaderFunction"},
{"hlsl.templatetypes.frag", "PixelShaderFunction"},
{"hlsl.whileLoop.frag", "PixelShaderFunction"}, {"hlsl.whileLoop.frag", "PixelShaderFunction"},
{"hlsl.void.frag", "PixelShaderFunction"}, {"hlsl.void.frag", "PixelShaderFunction"},
}), }),
......
// //
//Copyright (C) 2016 Google, Inc. //Copyright (C) 2016 Google, Inc.
//Copyright (C) 2016 LunarG, Inc.
// //
//All rights reserved. //All rights reserved.
// //
...@@ -291,12 +292,176 @@ void HlslGrammar::acceptQualifier(TQualifier& qualifier) ...@@ -291,12 +292,176 @@ void HlslGrammar::acceptQualifier(TQualifier& qualifier)
} while (true); } while (true);
} }
// template_type
// : FLOAT
// | DOUBLE
// | INT
// | DWORD
// | UINT
// | BOOL
//
bool HlslGrammar::acceptTemplateType(TBasicType& basicType)
{
switch (peek()) {
case EHTokFloat:
basicType = EbtFloat;
break;
case EHTokDouble:
basicType = EbtDouble;
break;
case EHTokInt:
case EHTokDword:
basicType = EbtInt;
break;
case EHTokUint:
basicType = EbtUint;
break;
case EHTokBool:
basicType = EbtBool;
break;
default:
return false;
}
advanceToken();
return true;
}
// vector_template_type
// : VECTOR
// | VECTOR LEFT_ANGLE template_type COMMA integer_literal RIGHT_ANGLE
//
bool HlslGrammar::acceptVectorTemplateType(TType& type)
{
if (! acceptTokenClass(EHTokVector))
return false;
if (! acceptTokenClass(EHTokLeftAngle)) {
// in HLSL, 'vector' alone means float4.
new(&type) TType(EbtFloat, EvqTemporary, 4);
return true;
}
TBasicType basicType;
if (! acceptTemplateType(basicType)) {
expected("scalar type");
return false;
}
// COMMA
if (! acceptTokenClass(EHTokComma)) {
expected(",");
return false;
}
// integer
if (! peekTokenClass(EHTokIntConstant)) {
expected("literal integer");
return false;
}
TIntermTyped* vecSize;
if (! acceptLiteral(vecSize))
return false;
const int vecSizeI = vecSize->getAsConstantUnion()->getConstArray()[0].getIConst();
new(&type) TType(basicType, EvqTemporary, vecSizeI);
if (vecSizeI == 1)
type.makeVector();
if (!acceptTokenClass(EHTokRightAngle)) {
expected("right angle bracket");
return false;
}
return true;
}
// matrix_template_type
// : MATRIX
// | MATRIX LEFT_ANGLE template_type COMMA integer_literal COMMA integer_literal RIGHT_ANGLE
//
bool HlslGrammar::acceptMatrixTemplateType(TType& type)
{
if (! acceptTokenClass(EHTokMatrix))
return false;
if (! acceptTokenClass(EHTokLeftAngle)) {
// in HLSL, 'matrix' alone means float4x4.
new(&type) TType(EbtFloat, EvqTemporary, 0, 4, 4);
return true;
}
TBasicType basicType;
if (! acceptTemplateType(basicType)) {
expected("scalar type");
return false;
}
// COMMA
if (! acceptTokenClass(EHTokComma)) {
expected(",");
return false;
}
// integer rows
if (! peekTokenClass(EHTokIntConstant)) {
expected("literal integer");
return false;
}
TIntermTyped* rows;
if (! acceptLiteral(rows))
return false;
// COMMA
if (! acceptTokenClass(EHTokComma)) {
expected(",");
return false;
}
// integer cols
if (! peekTokenClass(EHTokIntConstant)) {
expected("literal integer");
return false;
}
TIntermTyped* cols;
if (! acceptLiteral(cols))
return false;
new(&type) TType(basicType, EvqTemporary, 0,
cols->getAsConstantUnion()->getConstArray()[0].getIConst(),
rows->getAsConstantUnion()->getConstArray()[0].getIConst());
if (!acceptTokenClass(EHTokRightAngle)) {
expected("right angle bracket");
return false;
}
return true;
}
// If token is for a type, update 'type' with the type information, // If token is for a type, update 'type' with the type information,
// and return true and advance. // and return true and advance.
// Otherwise, return false, and don't advance // Otherwise, return false, and don't advance
bool HlslGrammar::acceptType(TType& type) bool HlslGrammar::acceptType(TType& type)
{ {
TBasicType basicType;
switch (peek()) { switch (peek()) {
case EHTokVector:
return acceptVectorTemplateType(type);
break;
case EHTokMatrix:
return acceptMatrixTemplateType(type);
break;
case EHTokStruct: case EHTokStruct:
return acceptStruct(type); return acceptStruct(type);
break; break;
...@@ -386,6 +551,7 @@ bool HlslGrammar::acceptType(TType& type) ...@@ -386,6 +551,7 @@ bool HlslGrammar::acceptType(TType& type)
new(&type) TType(EbtUint, EvqTemporary, 4); new(&type) TType(EbtUint, EvqTemporary, 4);
break; break;
case EHTokBool: case EHTokBool:
new(&type) TType(EbtBool); new(&type) TType(EbtBool);
break; break;
......
// //
//Copyright (C) 2016 Google, Inc. //Copyright (C) 2016 Google, Inc.
//Copyright (C) 2016 LunarG, Inc.
// //
//All rights reserved. //All rights reserved.
// //
...@@ -62,6 +63,9 @@ namespace glslang { ...@@ -62,6 +63,9 @@ namespace glslang {
bool acceptFullySpecifiedType(TType&); bool acceptFullySpecifiedType(TType&);
void acceptQualifier(TQualifier&); void acceptQualifier(TQualifier&);
bool acceptType(TType&); bool acceptType(TType&);
bool acceptTemplateType(TBasicType&);
bool acceptVectorTemplateType(TType&);
bool acceptMatrixTemplateType(TType&);
bool acceptStruct(TType&); bool acceptStruct(TType&);
bool acceptStructDeclarationList(TTypeList*&); bool acceptStructDeclarationList(TTypeList*&);
bool acceptFunctionParameters(TFunction&); bool acceptFunctionParameters(TFunction&);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment