Jump to content
  • Advertisement
Kavarna

DX11 GPU Raytracing

Recommended Posts

Hi everybody!

I am currently trying to write my own GPU Raytracer. 

I am using DirectX 11 and Compute Shader.

Here is what I've tried so far: 

RayTracer.hlsl

Spoiler

#include "RayTracingHeader.hlsli"

SamplerState gWrapSampler : register(s0);
TextureCube gObjBackground : register(t0); // Not used right now
RWTexture2D<float4> gResult : register(u0);

cbuffer cbCamera : register(b0)
{
    Camera gCamera;
};

cbuffer cbGeneralInfo : register(b1)
{
    uint gWidth;
    uint gHeight;
    uint gMaxRecursion; // Not used right now
    float gRayMaxLength;
};

[numthreads(32, 32, 1)]
void main( uint3 DTid : SV_DispatchThreadID,
			uint3 GroupID : SV_GroupThreadID )
{
    gResult[DTid.xy] = float4(0, 0, 0, 1);
    float u = (float) DTid.x / (float) gWidth;
    float v = (float) DTid.y / (float) gHeight;

    float4 currentRayDirection = gCamera.TopLeftCorner + u * (gCamera.TopRightCorner - gCamera.TopLeftCorner)
										+ v * (gCamera.BottomLeftCorner - gCamera.TopLeftCorner);
    currentRayDirection.w = 0.0f;
    currentRayDirection = normalize(currentRayDirection);
    Ray r;
    r.Origin = gCamera.CamPosition.xyz;
    r.Direction = currentRayDirection.xyz;
    r.length = gRayMaxLength;

    Sphere s1;
    s1.Position = float3(0, 0, 10);
    s1.Radius = 1;
    if (s1.Intersect(r))
    {
        float4 reflectedColor = float4(1, 0, 0, 1);
		gResult[DTid.xy] = reflectedColor;
    }

}

 

RayTracingHeader.hlsli

Spoiler


class Camera
{
    float4 CamPosition;
    float4 TopLeftCorner;
    float4 TopRightCorner;
    float4 BottomLeftCorner;
};

class Ray
{
    float3 Origin;
    float3 Direction;
    float length;
};

class Sphere
{
    float3 Position;
    float Radius;
    bool Intersect(Ray r)
    {
        float3 OriginToSphere = Position - r.Origin;
        float projection = dot(OriginToSphere, r.Direction);
        float3 distanceVector = OriginToSphere - projection * r.Direction;
        float distanceVectorLengthSQ = dot(distanceVector, distanceVector);
        float radiusSQ = Radius * Radius;
        if (distanceVectorLengthSQ > radiusSQ)
            return false;
		
        float newLength = projection - sqrt(radiusSQ - distanceVectorLengthSQ);
        if (newLength < 0.0f || newLength > r.length)
            return false;

        r.length = newLength;

        return true;
    }
};

 

 

But the result is not what I expected.

For example, the sphere is located at (0,0,10) with radius 1, but this is the result when CamPos is 4.5, which I think is wrong.

Also, for some reason, when I rotate the camera, the sphere expands.

Anyone could give me some pieces of advice, please? 

Share this post


Link to post
Share on other sites
Advertisement

Are TopLeftCorner, BottomLeftCorner, and TopRightCorner relative to the camera origin? or are they the positions in the 3d world? If they are the location in the 3d world, you'll have to subtract CamPosition from currentRayDirection

Share this post


Link to post
Share on other sites
34 minutes ago, iedoc said:

Are TopLeftCorner, BottomLeftCorner, and TopRightCorner relative to the camera origin? or are they the positions in the 3d world? If they are the location in the 3d world, you'll have to subtract CamPosition from currentRayDirection

They are location in 3d world.

I took your advice, it worked. Thank you very very very much!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!