https://github.com/shader-slang/slang
Raw File
Tip revision: 6fa4edbfbf01ef582a3ddc2fdfdedc79ba60d365 authored by Yong He on 30 March 2023, 01:23:21 UTC
Convert tensor types in `make_tensor_view`. (#2755)
Tip revision: 6fa4edb
raygen.slang
//TEST:CROSS_COMPILE: -profile glsl_460+GL_EXT_ray_tracing -stage raygeneration -entry main -target spirv-assembly

#define TRACING_EPSILON 1e-6

Texture2D   samplerPosition;
Texture2D   samplerNormal;
SamplerState sampler;

struct Light {
    float4 position;
    float4 color;
};

struct Uniforms
{
    Light       light;
    float4      viewPos;
    float4x4    view;
    float4x4    model;    
};
ConstantBuffer<Uniforms> ubo;


layout(rgba8);
RWTexture2D<float4> outputImage;

RaytracingAccelerationStructure as;

struct ShadowRay
{
    float hitDistance;
};

struct ReflectionRay
{
    float color;
};

#define gl_LaunchIDNV DispatchRaysIndex()
#define gl_LaunchSizeNV DispatchRaysDimensions()

void main() 
{
    float2 inUV = float2(
        (float(gl_LaunchIDNV.x) + 0.5f) / float(gl_LaunchSizeNV.x),
        (float(gl_LaunchIDNV.y) + 0.5f) / float(gl_LaunchSizeNV.y)
    );

    float3 P = samplerPosition.Sample(sampler, inUV).rgb;
    float3 N = samplerNormal.Sample(sampler, inUV).rgb * 2.0 - 1.0;

    float3 lightPos = ubo.light.position.xyz;
    float3 lightDelta = lightPos - P;
    float lightDist = length(lightDelta);
    float3 L = normalize(lightDelta);
    float atten = 1.0f / (lightDist*lightDist);

    RayDesc ray;
    ray.Origin = P;
    ray.TMin = TRACING_EPSILON;
    ray.Direction = lightDelta;
    ray.TMax = lightDist;

    {
        ShadowRay shadowRay;
        shadowRay.hitDistance = 0;

        TraceRay(as,
                // ray flags
                1,
                // cull mask
                0xff,
                // sbt record offset
                0,
                // sbt record stride
                0,
                // missIndex
                2,
                // ray
                ray,
                // payload
                shadowRay);
        
        if (shadowRay.hitDistance < lightDist)
        {
            atten = 0.f;
        }
    }

    float3 color = ubo.light.color.xyz * saturate(dot(N,L)) * atten;


    {
        ReflectionRay reflectionRay;
        TraceRay(as,
                // ray flags
                1,
                // cull mask
                0xff,
                // sbt record offset
                0,
                // sbt record stride
                0,
                // missIndex
                2,
                // ray
                ray,
                // payload
                reflectionRay);


        color = color + reflectionRay.color;
    }

    outputImage[int2(gl_LaunchIDNV.xy)] = float4(color, 1.0);
}
back to top