Comments (15)
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.
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.
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.
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.
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.
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.
Looking into the bug
from slang.
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.
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.
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.
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.
This is now exposed in stdlib as
float3 HitTriangleVertexPosition(uint index);
from slang.
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.
I couldn't find a way to implement rayQueryGetIntersectionTriangleVertexPositionsEXT
, should I file a new issue for this one?
from slang.
Please file a new issue, thanks.
from slang.
Related Issues (20)
- AD Cleanup: Fix confusing naming conventions for synthesized methods and types
- AD Cleanup: Cleanup `slang-ir-autodiff-unzip.cpp` & `slang-ir-autodiff-unzip.h`
- AD Cleanup: Avoid dealing with invalid IR by representing out-of-scope accesses through `Push` and `Pop` instructions
- Add OpCapability only once
- AD Cleanup: CFG normalization replaces break statements with a boolean break flag `bflag`, but this currently means the opposite of what it should
- AD Cleanup: break-elimination during the CFG normalization step can cause the loop condition to be evaluated an extra time.
- Lower CMake version requirement
- GLSL texture functions need to support int and uint types of samplers as well as float HOT 1
- AD Enhancement: Avoid SSA-ing aggregate types by treating pointer-based element & field accesses as if they were differentiable method calls.
- AD Enhancement: Enhance DCE to avoid situations where mutating primal methods are called multiple times during the reverse-mode pass, potentially causing slow-down
- [] operator partially supported for Texture2DMS HOT 1
- .mips operator function not supported Texture2D
- Consider supporting #pragma pack_matrix() HOT 1
- Capability System: Profile and look into performance with larger systems
- Metal: support integer type texture
- Fix memory leak in slang.dll HOT 1
- Create the breakdown profiling of slang HOT 2
- AD Cleanup: Use a temporary accumulation inst such as `IRAccumulate` instead of explicitly loading, adding and storing differential values in reverse mode
- Implement metrics tracking for GitHub engagement
- Document the existing auto-diff type system, semantic checking and AST synthesis methods
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from slang.