Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added -
Binary file not shown.
37 changes: 37 additions & 0 deletions source/slang/slang-check-modifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
// focused on `[attributes]`.

#include "slang-lookup.h"
#include "slang-target.h"

namespace Slang
{
Expand Down Expand Up @@ -1677,6 +1678,42 @@ Modifier* SemanticsVisitor::checkModifier(
}
}

// Check for layout qualifiers on incompatible targets
if (auto layoutMod = as<GLSLBufferDataLayoutModifier>(m))
{
String layoutName;

if (as<GLSLStd140Modifier>(layoutMod))
layoutName = "std140";
else if (as<GLSLStd430Modifier>(layoutMod))
layoutName = "std430";
else if (as<GLSLScalarModifier>(layoutMod))
layoutName = "scalar";
else
layoutName = "layout";

for (auto target : getLinkage()->targets)
{
if (!isKhronosTarget(target->getTarget()) && !isWGPUTarget(target->getTarget()))
{
String targetName;
switch (target->getTarget())
{
case CodeGenTarget::HLSL: targetName = "HLSL"; break;
case CodeGenTarget::DXIL: targetName = "DXIL"; break;
case CodeGenTarget::DXBytecode: targetName = "DXBC"; break;
case CodeGenTarget::CSource: targetName = "C"; break;
case CodeGenTarget::CPPSource: targetName = "C++"; break;
case CodeGenTarget::CUDASource: targetName = "CUDA"; break;
case CodeGenTarget::MetalLib: targetName = "Metal"; break;
default: targetName = "unknown"; break;
}
getSink()->diagnose(m, Diagnostics::layoutQualifierUnsupportedForTarget, layoutName, targetName);
break;
}
}
}

if (as<ConstModifier>(m))
{
if (auto varDeclBase = as<VarDeclBase>(syntaxNode))
Expand Down
6 changes: 6 additions & 0 deletions source/slang/slang-diagnostic-defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -2299,6 +2299,12 @@ DIAGNOSTIC(39009, Error, expectedSpace, "expected 'space', got '$0'")
DIAGNOSTIC(39010, Error, expectedSpaceIndex, "expected a register space index after 'space'")
DIAGNOSTIC(39011, Error, invalidComponentMask, "invalid register component mask '$0'.")

DIAGNOSTIC(
39012,
Warning,
layoutQualifierUnsupportedForTarget,
"layout qualifier '$0' is not supported for target '$1'")

DIAGNOSTIC(
39013,
Warning,
Expand Down
4 changes: 2 additions & 2 deletions source/slang/slang-parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3578,8 +3578,8 @@ static Decl* ParseBufferBlockDecl(

static NodeBase* parseHLSLCBufferDecl(Parser* parser, void* /*userData*/)
{
// Check for GLSL layout qualifiers when GLSL input is allowed
if (parser->options.allowGLSLInput && parser->pendingModifiers)
// Check for GLSL layout qualifiers (layout modifiers can be used even without allowGLSLInput)
if (parser->pendingModifiers)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot, I think it needs to warn when layout (std430) etc are used when compiling for other targets (warn instead of error to make it a non-breaking PR), and accept these layout modifiers when compiling for GLSL/SPIRV without requiring -allow-glsl

{
auto getLayoutArg = [&](const char* defaultLayout)
{
Expand Down
15 changes: 15 additions & 0 deletions tests/spirv/cbuffer-std430-layout.slang
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//TEST:SIMPLE(filecheck=SPIRV): -target spirv -entry main -stage fragment -emit-spirv-directly

// Test that layout(std430) cbuffer uses correct ArrayStride (8 for int2 array)
// instead of std140 ArrayStride (16 for int2 array)

layout(std430) cbuffer CBlock {
int2 bb[2];
};

//SPIRV-DAG: OpDecorate %_arr_v2int_int_2 ArrayStride 8

float4 main() : SV_Target
{
return float4(0.3, 0.7, 0.55, 1.0 + bb[1].y);
}