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: 



#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 =;
    r.Direction =;
    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;





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

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

  • Forum Statistics

    • Total Topics
    • Total Posts
  • Similar Content

    • By Josheir
      void update() { if (thrust) { dx += cos(angle*DEGTORAD)*.02; dy += sin(angle*DEGTORAD)*.02; } else { dx*=0.99; dy*=0.99; } int maxSpeed = 15; float speed = sqrt(dx*dx+dy*dy); if (speed>maxSpeed) { dx *= maxSpeed/speed; dy *= maxSpeed/speed; } x+=dx; y+=dy; . . . } In the above code, why is maxSpeed being divided by the speed variable.  I'm stumped.
      Thank you,
    • By Benjamin Shefte
      Hey there,  I have this old code im trying to compile using GCC and am running into a few issues..
      im trying to figure out how to convert these functions to gcc
      static __int64 MyQueryPerformanceFrequency() { static __int64 aFreq = 0; if(aFreq!=0) return aFreq; LARGE_INTEGER s1, e1, f1; __int64 s2, e2, f2; QueryPerformanceCounter(&s1); s2 = MyQueryPerformanceCounter(); Sleep(50); e2 = MyQueryPerformanceCounter(); QueryPerformanceCounter(&e1); QueryPerformanceFrequency(&f1); double aTime = (double)(e1.QuadPart - s1.QuadPart)/f1.QuadPart; f2 = (e2 - s2)/aTime; aFreq = f2; return aFreq; } void PerfTimer::GlobalStart(const char *theName) { gPerfTimerStarted = true; gPerfTotalTime = 0; gPerfTimerStartCount = 0; gPerfElapsedTime = 0; LARGE_INTEGER anInt; QueryPerformanceCounter(&anInt); gPerfResetTick = anInt.QuadPart; } /////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////// void PerfTimer::GlobalStop(const char *theName) { LARGE_INTEGER anInt; QueryPerformanceCounter(&anInt); LARGE_INTEGER aFreq; QueryPerformanceFrequency(&aFreq); gPerfElapsedTime = (double)(anInt.QuadPart - gPerfResetTick)/aFreq.QuadPart*1000.0; gPerfTimerStarted = false; }  
      I also tried converting this function (original function is the first function below and my converted for gcc function is under that) is this correct?:
      #if defined(WIN32) static __int64 MyQueryPerformanceCounter() { // LARGE_INTEGER anInt; // QueryPerformanceCounter(&anInt); // return anInt.QuadPart; #if defined(WIN32) unsigned long x,y; _asm { rdtsc mov x, eax mov y, edx } __int64 result = y; result<<=32; result|=x; return result; } #else static __int64 MyQueryPerformanceCounter() { struct timeval t1, t2; double elapsedTime; // start timer gettimeofday(&t1, NULL); Sleep(50); // stop timer gettimeofday(&t2, NULL); // compute and print the elapsed time in millisec elapsedTime = (t2.tv_sec - t1.tv_sec) * 1000.0; // sec to ms elapsedTime += (t2.tv_usec - t1.tv_usec) / 1000.0; // us to ms return elapsedTime; } #endif Any help would be appreciated, Thank you!
    • By GreenGodDiary
      Having some issues with a geometry shader in a very basic DX app.
      We have an assignment where we are supposed to render a rotating textured quad, and in the geometry shader duplicate this quad and offset it by its normal. Very basic stuff essentially.
      My issue is that the duplicated quad, when rendered in front of the original quad, seems to fail the Z test and thus the original quad is rendered on top of it.
      Whats even weirder is that this only happens for one of the triangles in the duplicated quad, against one of the original quads triangles.

      Here's a video to show you what happens: Video (ignore the stretched textures)

      Here's my GS: (VS is simple passthrough shader and PS is just as basic)
      struct VS_OUT { float4 Pos : SV_POSITION; float2 UV : TEXCOORD; }; struct VS_IN { float4 Pos : POSITION; float2 UV : TEXCOORD; }; cbuffer cbPerObject : register(b0) { float4x4 WVP; }; [maxvertexcount(6)] void main( triangle VS_IN input[3], inout TriangleStream< VS_OUT > output ) { //Calculate normal float4 faceEdgeA = input[1].Pos - input[0].Pos; float4 faceEdgeB = input[2].Pos - input[0].Pos; float3 faceNormal = normalize(cross(,; //Input triangle, transformed for (uint i = 0; i < 3; i++) { VS_OUT element; VS_IN vert = input[i]; element.Pos = mul(vert.Pos, WVP); element.UV = vert.UV; output.Append(element); } output.RestartStrip(); for (uint j = 0; j < 3; j++) { VS_OUT element; VS_IN vert = input[j]; element.Pos = mul(vert.Pos + float4(faceNormal, 0.0f), WVP);; element.UV = vert.UV; output.Append(element); } }  
      I havent used geometry shaders much so im not 100% on what happens behind the scenes.
      Any tips appreciated! 
    • By mister345
      Hi, I'm building a game engine using DirectX11 in c++.
      I need a basic physics engine to handle collisions and motion, and no time to write my own.
      What is the easiest solution for this? Bullet and PhysX both seem too complicated and would still require writing my own wrapper classes, it seems. 
      I found this thing called PAL - physics abstraction layer that can support bullet, physx, etc, but it's so old and no info on how to download or install it.
      The simpler the better. Please let me know, thanks!
  • Popular Now