Advertisement Jump to content
Kavarna

DX11 GPU Raytracing

This topic is 410 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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

  • Advertisement
×

Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!