Skip to content

Commit

Permalink
Merge branch 'master' into cheneym2/adapterInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
cheneym2 authored Feb 18, 2025
2 parents 26fb244 + 64dfdbd commit c5c1c72
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 35 deletions.
8 changes: 8 additions & 0 deletions source/slang/slang-diagnostic-defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -2055,6 +2055,14 @@ DIAGNOSTIC(
"shader parameter '$0' has a 'register' specified for D3D, but no '[[vk::binding(...)]]` "
"specified for Vulkan, nor is `-fvk-$1-shift` used.")

DIAGNOSTIC(
39071,
Warning,
bindingAttributeIgnoredOnUniform,
"binding attribute on uniform '$0' will be ignored since it will be packed into the default "
"constant buffer at descriptor set 0 binding 0. To use explicit bindings, declare the uniform "
"inside a constant buffer.")

//

// 4xxxx - IL code generation.
Expand Down
42 changes: 15 additions & 27 deletions source/slang/slang-parameter-binding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3511,34 +3511,22 @@ static void collectParameters(ParameterBindingContext* inContext, ComponentType*
/// Emit a diagnostic about a uniform/ordinary parameter at global scope.
void diagnoseGlobalUniform(SharedParameterBindingContext* sharedContext, VarDeclBase* varDecl)
{
// This subroutine gets invoked if a shader parameter containing
// "ordinary" data (sometimes just called "uniform" data) is present
// at the global scope.
//
// Slang can support such parameters by aggregating them into
// an implicit constant buffer, but it is also common for programmers
// to accidentally declare a global-scope shader parameter when they
// meant to declare a global variable instead:
//
// int gCounter = 0; // this is a shader parameter, not a global
//
// In order to avoid mistakes, we'd like to warn the user when
// they write code like the above, and hint to them that they
// should make their intention more explicit with a keyword:
//
// static int gCounter = 0; // this is now a (static) global
//
// uniform int gCounter; // this is now explicitly a shader parameter
//
// We skip the diagnostic whenever the variable was explicitly `uniform`,
// under the assumption that the programmer who added that modifier
// knew what they were opting into.
//
if (varDecl->hasModifier<HLSLUniformModifier>())
return;
// Don't emit the implicit global shader parameter warning if the variable is explicitly marked
// as uniform
if (!varDecl->hasModifier<HLSLUniformModifier>())
{
getSink(sharedContext)
->diagnose(varDecl, Diagnostics::globalUniformNotExpected, varDecl->getName());
}

getSink(sharedContext)
->diagnose(varDecl, Diagnostics::globalUniformNotExpected, varDecl->getName());
// Always check and warn about binding attributes being ignored, regardless of uniform modifier
if (varDecl->findModifier<GLSLBindingAttribute>())
{
sharedContext->m_sink->diagnose(
varDecl,
Diagnostics::bindingAttributeIgnoredOnUniform,
varDecl->getName());
}
}


Expand Down
18 changes: 16 additions & 2 deletions source/slang/slang-preprocessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2011,6 +2011,13 @@ TokenReader MacroInvocation::_getArgTokens(Index paramIndex)
// to the parameter, and we construct a `TokenReader` that will play
// back the tokens of that argument.
//
// Special case: If we have no arguments but the macro expects one parameter,
// return an empty token range
if (m_args.getCount() == 0 && m_macro->params.getCount() == 1)
{
return TokenReader(argTokens, argTokens);
}

SLANG_ASSERT(paramIndex < m_args.getCount());
auto arg = m_args[paramIndex];
return TokenReader(argTokens + arg.beginTokenIndex, argTokens + arg.endTokenIndex);
Expand All @@ -2037,8 +2044,15 @@ TokenReader MacroInvocation::_getArgTokens(Index paramIndex)
// When there are no arguments for the varaidic parameter we will
// construct an empty token range that comes after the other arguments.
//
auto arg = m_args[lastArgIndex];
return TokenReader(argTokens + arg.endTokenIndex, argTokens + arg.endTokenIndex);
if (lastArgIndex >= 0)
{
auto arg = m_args[lastArgIndex];
return TokenReader(argTokens + arg.endTokenIndex, argTokens + arg.endTokenIndex);
}
else
{
return TokenReader(argTokens, argTokens);
}
}

// Because the `m_argTokens` array includes the commas between arguments,
Expand Down
22 changes: 22 additions & 0 deletions tests/bugs/binding-attribute-ignored.slang
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// binding-attribute-ignored.slang
// Test that binding attributes on uniforms that get packed into the default uniform buffer trigger a warning

//TEST:SIMPLE(filecheck=CHECK):-target spirv

//CHECK: ([[# @LINE+2]]): warning 39071
[[vk::binding(1, 2)]]
uniform float4 g_position;

//CHECK: ([[# @LINE+2]]): warning 39071
[[vk::binding(3, 1)]]
uniform float4x4 g_transform;

// This won't trigger a warning because it's a texture (not packed into default uniform buffer)
[[vk::binding(0, 0)]]
Texture2D g_texture;

[shader("vertex")]
float4 main(float4 pos : POSITION) : SV_POSITION
{
return g_position;
}
13 changes: 13 additions & 0 deletions tests/diagnostics/array-zero-size.slang
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// array-zero-size.slang

// Test that array size cannot be zero

//TEST:SIMPLE:

[numthreads(4, 1, 1)]
void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID)
{
bar();
}

func bar() -> int[0]; // expected-error 30025 "array size must be larger than zero."
11 changes: 5 additions & 6 deletions tests/hlsl/simple-hull-shader-1.slang
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
//TEST:SIMPLE(filecheck=HLSL):-target hlsl -entry hullMain -stage hull -allow-glsl
//TEST:SIMPLE(filecheck=GLSL):-target glsl -entry hullMain -stage hull -allow-glsl
// Currently SPIR-V fails to compile this hull shader (#4914)
//DISABLE_TEST:SIMPLE(filecheck=SPIRV):-target spirv -entry hullMain -stage hull -allow-glsl
//TEST:SIMPLE(filecheck=SPIRV):-target spirv -entry hullMain -stage hull -allow-glsl

//HLSL: hullMain

//GLSL-DAG: location = 0
//GLSL-DAG: location = 1
//GLSL-DAG: location = 2

//SPIRV-DAG: location 0
//SPIRV-DAG: location 1
//SPIRV-DAG: location 2
//SPIRV-DAG: OpDecorate {{.*}} Location 0
//SPIRV-DAG: OpDecorate {{.*}} Location 1
//SPIRV-DAG: OpDecorate {{.*}} Location 2

struct HsOut
{
Expand Down Expand Up @@ -50,4 +49,4 @@ HscOut constants()
o.InsideTessFactor[0] = 0.5;
o.InsideTessFactor[1] = 0.5;
return o;
}
}

0 comments on commit c5c1c72

Please sign in to comment.