Skip to content

Commit a4683b1

Browse files
committed
Allow .member syntax on vector and scalars.
1 parent 0101e5a commit a4683b1

File tree

2 files changed

+64
-7
lines changed

2 files changed

+64
-7
lines changed

source/slang/slang-check-expr.cpp

+47-7
Original file line numberDiff line numberDiff line change
@@ -4914,6 +4914,40 @@ Expr* SemanticsVisitor::checkGeneralMemberLookupExpr(MemberExpr* expr, Type* bas
49144914
return createLookupResultExpr(expr->name, lookupResult, expr->baseExpression, expr->loc, expr);
49154915
}
49164916

4917+
bool isValidVectorSwizzleName(Name* name)
4918+
{
4919+
if (!name)
4920+
return false;
4921+
if (name->text.getLength() > 4 || name->text.getLength() == 0)
4922+
return false;
4923+
bool isRGBA = false;
4924+
bool isXYZW = false;
4925+
for (auto ch : name->text)
4926+
{
4927+
switch (ch)
4928+
{
4929+
case 'w':
4930+
case 'x':
4931+
case 'y':
4932+
case 'z':
4933+
isXYZW = true;
4934+
continue;
4935+
case 'r':
4936+
case 'g':
4937+
case 'b':
4938+
case 'a':
4939+
isRGBA = true;
4940+
continue;
4941+
default:
4942+
return false;
4943+
}
4944+
}
4945+
// A valid swizzle cannot mix XYZW and RGBA.
4946+
if (isXYZW && isRGBA)
4947+
return false;
4948+
return true;
4949+
}
4950+
49174951
Expr* SemanticsExprVisitor::visitMemberExpr(MemberExpr* expr)
49184952
{
49194953
bool needDeref = false;
@@ -4955,15 +4989,21 @@ Expr* SemanticsExprVisitor::visitMemberExpr(MemberExpr* expr)
49554989
}
49564990
if (auto baseVecType = as<VectorExpressionType>(baseType))
49574991
{
4958-
return CheckSwizzleExpr(
4959-
expr,
4960-
baseVecType->getElementType(),
4961-
baseVecType->getElementCount());
4992+
if (isValidVectorSwizzleName(expr->name))
4993+
{
4994+
return CheckSwizzleExpr(
4995+
expr,
4996+
baseVecType->getElementType(),
4997+
baseVecType->getElementCount());
4998+
}
49624999
}
4963-
else if (auto baseScalarType = as<BasicExpressionType>(baseType))
5000+
if (auto baseScalarType = as<BasicExpressionType>(baseType))
49645001
{
4965-
// Treat scalar like a 1-element vector when swizzling
4966-
return CheckSwizzleExpr(expr, baseScalarType, 1);
5002+
if (isValidVectorSwizzleName(expr->name))
5003+
{
5004+
// Treat scalar like a 1-element vector when swizzling
5005+
return CheckSwizzleExpr(expr, baseScalarType, 1);
5006+
}
49675007
}
49685008
else if (as<NamespaceType>(baseType))
49695009
{

tests/front-end/vector-member.slang

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//TEST:COMPARE_COMPUTE(filecheck-buffer=CHECK):-output-using-type
2+
3+
extension float3
4+
{
5+
float sum() { return this.x + this.y + this.z; }
6+
}
7+
8+
//TEST_INPUT: set outputBuffer = out ubuffer(data=[0 0 0 0], stride = 4)
9+
RWStructuredBuffer<float> outputBuffer;
10+
11+
[numthreads(1,1,1)]
12+
void computeMain()
13+
{
14+
float3 v = { 1, 2, 3 };
15+
// CHECK: 6
16+
outputBuffer[0] = v.sum();
17+
}

0 commit comments

Comments
 (0)