Giter Club home page Giter Club logo

Comments (15)

csyonghe avatar csyonghe commented on May 25, 2024 1

Please check the documentation here: https://shader-slang.com/slang/user-guide/a1-04-interop.html#inline-spirv-assembly

In Slang, we support %varName : <typeOperand> = <OpCode> args syntax, which is equivalent to %varName = <OpCode> <typeOperand> args.

As documented, $ is used to reference a slang variable/value, $$ is used to reference a slang type, and % is used to reference a spirv inst previously defined in the same asm block.

from slang.

csyonghe avatar csyonghe commented on May 25, 2024

We will add them.

Slang does have a way to let the user emit specific spirv code via the spirv_asm feature: https://shader-slang.com/slang/user-guide/a1-04-interop.html

from slang.

csyonghe avatar csyonghe commented on May 25, 2024

To whoever working on this issue: you can refer to the declarations in hlsl.meta.slang: 9502 and add another function there for the missing builtins.

from slang.

mklefrancois avatar mklefrancois commented on May 25, 2024

Thanks for the tip, for the moment I tried this and it worked.

// Adding capability and extension for KHR_ray_tracing_position_fetch
__glsl_extension(GL_EXT_ray_tracing_position_fetch)
float3 getHitTriangleVertexPositions(int ID)
{
    __target_switch
    {
    case glsl:
        __intrinsic_asm "gl_HitTriangleVertexPositionsEXT[$0]";
    default:
        return float3(0, 0, 0);
    }
}

I didn't implement the spir-v version because issue #3359 is still open, but I'm happy to see this working.

from slang.

mklefrancois avatar mklefrancois commented on May 25, 2024

By curiosity, is this the syntax for SPIRV ?

  case spirv:
    return spirv_asm {
      OpExtension "SPV_KHR_ray_tracing_position_fetch";
      OpCapability RayTracingPositionFetchKHR;
      result:$$float3 = OpLoad builtin(HitTriangleVertexPositionsKHR:float3) $0;
      };

from slang.

csyonghe avatar csyonghe commented on May 25, 2024

I think HitTriangleVertexPositionsKHR should be of type float3[3], and you need to first use OpAcxessChain to get a pointer to the element at id and then load it.

from slang.

pmistryNV avatar pmistryNV commented on May 25, 2024

Looking into the bug

from slang.

mklefrancois avatar mklefrancois commented on May 25, 2024

I'm still trying to implement the spirv target for KHR_ray_tracing_position_fetch.

The code should be fine, I think, but the generated SpirV code contains an error: Invalid memory access operand

This error seems related to the OpAccessChain of the builtin.

Here is the modified function for direct Spirv.

// Adding capability and extension for KHR_ray_tracing_position_fetch
__glsl_extension(GL_EXT_ray_tracing_position_fetch)
float3 getHitTriangleVertexPositions(int ID)
{
  __target_switch
  {
  case glsl:
    __intrinsic_asm "gl_HitTriangleVertexPositionsEXT[$0]";
  case spirv:
    return spirv_asm {
      OpExtension "SPV_KHR_ray_tracing_position_fetch";
      OpCapability RayTracingPositionFetchKHR;
      
      %addr = OpAccessChain %_ptr_Input_v3float builtin(HitTriangleVertexPositionsKHR:float3[3U]) $ID;
      result:$$float3 = OpLoad %v3float %addr;
      };
  default:
    return float3(0, 0, 0);
  }
}

Unfortunately, the float3[3U] does not convert to uint3, but to int3

The above Slang code translate to

%_arr_v3float_0_int_3 = OpTypeArray %v3float_0 %int_3
%_ptr_Input__arr_v3float_0_int_3 = OpTypePointer Input %_arr_v3float_0_int_3
%367 = OpVariable %_ptr_Input__arr_v3float_0_int_3 Input
%ID = OpFunctionParameter %int
%addr = OpAccessChain %_ptr_Input_v3float %367 %ID

Where if I look to the GLSL version, it translate to using a uint3.

%_arr_v3float_uint_3 = OpTypeArray %v3float %uint_3
%_ptr_Input__arr_v3float_uint_3 = OpTypePointer Input %_arr_v3float_uint_3
%gl_HitTriangleVertexPositionsEXT = OpVariable %_ptr_Input__arr_v3float_uint_3 Input
%224 = OpAccessChain %_ptr_Input_v3float %gl_HitTriangleVertexPositionsEXT %int_0
%225 = OpLoad %v3float %224

I'm not sure if this is the issue, but I haven't found yet the way to support the feature with -emit-spirv-directly

from slang.

csyonghe avatar csyonghe commented on May 25, 2024

I noticed that in your posted code, the glslang version fetches the array with an integer constant, but the slang version fetches with a function parameter. Not sure if it is complaining about that. Perhaps try marking the function with [ForceInline]?

Slang always normalizes array types to have int typed sizes.

from slang.

csyonghe avatar csyonghe commented on May 25, 2024

I think the line:

 %addr = OpAccessChain %_ptr_Input_v3float builtin(HitTriangleVertexPositionsKHR:float3[3U]) $ID;

is wrong because %_ptr_Input_v3float is not a defined entity in the asm block. You probably meant:

%_ptr_Input_v3float = OpTypePointer $$float3 Input;
 %addr:%_ptr_Input_v3float = OpAccessChain builtin(HitTriangleVertexPositionsKHR:float3[3U]) $ID;

from slang.

mklefrancois avatar mklefrancois commented on May 25, 2024

Thanks for the tips, the following code now works.

  • Added [ForceInline]
  • Declared %pv3float instead of the hidden %_ptr_Input_v3float pre-declared
  • Removed extra parameter to OpLoad
// Adding capability and extension for KHR_ray_tracing_position_fetch
[ForceInline]
__glsl_extension(GL_EXT_ray_tracing_position_fetch)
float3 getHitTriangleVertexPositions(int ID)
{
  __target_switch
  {
  case glsl:
    __intrinsic_asm "gl_HitTriangleVertexPositionsEXT[$0]";
  case spirv:
    return spirv_asm {
      OpExtension "SPV_KHR_ray_tracing_position_fetch";
      OpCapability RayTracingPositionFetchKHR;
      
      %pv3float = OpTypePointer Input $$float3;
      %addr = OpAccessChain %pv3float builtin(HitTriangleVertexPositionsKHR:float3[3]) $ID;
      result:$$float3 = OpLoad %addr;
      };
  default:
    return float3(0, 0, 0);
  }
}

Note: The generated SpirV requires two arguments for OpLoad, while only one argument is needed in Slang. Is there any documentation available regarding how to use `__target_switch'? Also, when to prefix with %, $ or $$.

Generated SPIRV

       %addr = OpAccessChain %pv3float %361 %int_0
       %362 = OpLoad %v3float %addr

from slang.

csyonghe avatar csyonghe commented on May 25, 2024

This is now exposed in stdlib as

float3 HitTriangleVertexPosition(uint index);

from slang.

mklefrancois avatar mklefrancois commented on May 25, 2024

Thanks, would it be also possible to add the RayQuery equivalent?

void rayQueryGetIntersectionTriangleVertexPositionsEXT(rayQueryEXT q, bool committed, out vec3 positions[3]);

Meanwhile I will try to implement it using __target_switch

from slang.

mklefrancois avatar mklefrancois commented on May 25, 2024

I couldn't find a way to implement rayQueryGetIntersectionTriangleVertexPositionsEXT, should I file a new issue for this one?

from slang.

kaizhangNV avatar kaizhangNV commented on May 25, 2024

Please file a new issue, thanks.

from slang.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.