shader-slang / slang

Making it easier to work with shaders
MIT License
1.78k stars 159 forks source link

Does Slang allow function implementation to live outside of namespace? #4446

Open chaoticbob opened 1 week ago

chaoticbob commented 1 week ago

For the shader below, Slang current generates the following error due to the implementation of A::B::AddBlue() and A::AddGreen() living outside the namespace:

shader.hlsl(46): error 20001: unexpected '::', expected ';'
float3 A::B::AddBlue() { return float3(1, 1, 1); }
        ^~
shader.hlsl(46): error 20001: unexpected ')', expected identifier
float3 A::B::AddBlue() { return float3(1, 1, 1); }
                     ^
shader.hlsl(46): error 20001: unexpected '{', expected ';'
float3 A::B::AddBlue() { return float3(1, 1, 1); }
                       ^
shader.hlsl(47): error 20001: unexpected '::', expected ';'
float3 A::AddGreen() { return float3(3, 3, 3); }
        ^~
shader.hlsl(47): error 20001: unexpected ')', expected identifier
float3 A::AddGreen() { return float3(3, 3, 3); }
                   ^
shaders.hlsl(47): error 20001: unexpected '{', expected ';'
float3 A::AddGreen() { return float3(3, 3, 3); }

I couldn't find an example in the tests of something similar.

Shader

namespace A {
  float3 AddRed() { return float3(0, 0, 0); }
  float3 AddGreen();

  namespace B {
    typedef float3 f3;
    float3 AddRed() { return float3(1, 1, 1); }
    float3 AddBlue();
  }  // end namespace B

  struct myStruct {
    int point1;
    int point2;
    int add() {
      return point1 + point2;
    }
  };

  myStruct createMyStruct() {
    myStruct s;
    return s;
  }
}  // end namespace A

float3 AddRed() { return float3(2, 2, 2); }

float4 main(float4 PosCS : SV_Position) : SV_Target
{
  float3 val_1 = AddRed();
  float3 val_2 = A::AddRed();
  float3 val_3 = A::B::AddRed();

  float3 val_4 = A::B::AddBlue();
  float3 val_5 = A::AddGreen();

  A::B::f3 vec3f = float3(2,2,2);

  A::myStruct s = A::createMyStruct();
  int val_6 = s.add();

  return float4(0,0,0,0);
}

float3 A::B::AddBlue() { return float3(1, 1, 1); }
float3 A::AddGreen() { return float3(3, 3, 3); }
csyonghe commented 1 week ago

Slang does not support function declaration. Slang allows out of order definitions so declaration is not needed. Declaring a function without implementation and provide the implementation in a different place is not a supported language feature.

chaoticbob commented 1 week ago

I glanced at the function section of the docs and didn't see any mention of out of order definition (or maybe I'm looking in the wrong spot?). If it's not in there, it would be helpful to have an explanation and some examples.

jkwak-work commented 1 day ago

The user guide explains that the forward declaration is not needed.

One source file in a translation unit may freely refer to declarations in another source file from the same translation unit without any need for forward declarations.

https://shader-slang.com/slang/user-guide/compiling.html

chaoticbob commented 1 day ago

That is a single sentence in a relatively obscure part of the documentation to discuss functions. At the very least, the Function section of the documentation should mention the scoping behavior with in the same source file and refer to that Compiling section for more information on behavior within source units/modules.

jkwak-work commented 1 day ago

I agree that it should be documented better. Changing the labe to to documentation