skip to Main Content

So Im trying to make a compute shader for my unity game, and I keep getting this strange error:

Shader error in 'PathTracing': undeclared identifier 'intersectionPos' at kernel CSMain at PathTracing.compute(76) (on d3d11)

I am using cursor editor for coding. It’s same as visual studio code but it has GPT-4 integrated.

Here is the shader:

// Each #kernel tells which function to compile; you can have many kernels
#pragma kernel CSMain

// Create a RenderTexture with enableRandomWrite flag and set it
// with cs.SetTexture
RWTexture2D<float4> Result;

float4x4 _CameraToWorld;
float4x4 _CameraInverseProjection;

struct Ray
{
    float3 origin;
    float3 direction;
};

Ray CreateRay(float3 origin, float3 direction)
{
    Ray ray;
    ray.origin = origin;
    ray.direction = direction;
    return ray;
}

struct Triangle {
    float3 v0;
    float3 v1;
    float3 v2;
};

Triangle CreateTriangle(float3 Vert1, float3 Vert2, float3 Vert3)
{
    Triangle triangle;
    triangle.v0 = Vert1;
    triangle.v1 = Vert2;
    triangle.v2 = Vert3;
    return triangle;
}

bool IntersectRayTriangle(Ray ray, Triangle triangle, out float3 intersectionPos) {
    // Compute vectors for two edges of the triangle
    float3 edge1 = triangle.v1 - triangle.v0;
    float3 edge2 = triangle.v2 - triangle.v0;

    // Compute determinant to check for back-face culling
    float3 pvec = cross(ray.direction, edge2);
    float det = dot(edge1, pvec);

    // If determinant is near zero, the ray and triangle are parallel
    if (abs(det) < 0.00001f)
        return false;

    float invDet = 1.0f / det;

    // Compute distance from triangle origin to ray origin
    float3 tvec = ray.origin - triangle.v0;

    // Compute u parameter and test bounds
    float u = dot(tvec, pvec) * invDet;
    if (u < 0.0f || u > 1.0f)
        return false;

    // Compute v parameter and test bounds
    float3 qvec = cross(tvec, edge1);
    float v = dot(ray.direction, qvec) * invDet;
    if (v < 0.0f || u + v > 1.0f)
        return false;

    // Compute distance along ray to intersection point
    float t = dot(edge2, qvec) * invDet;

    // Ensure intersection is in front of ray origin
    if (t > 0.00001f) {
        intersectionPos = ray.origin + ray.direction * t;
        return true;
    }

    return false;
}

[numthreads(8,8,1)]
void CSMain (uint3 id : SV_DispatchThreadID)
{
    uint width, height;
    Result.GetDimensions(width, height);

    float2 uv = float2((id.xy + float2(0.5f, 0.5f)) / float2(width, height) * 0.2f - 1.0f);

    float3 RayOrigin = float3(0.0f, 2.5f, -2.0f); //mul(_CameraToWorld, float4(0, 0, 0, 1)).xyz;
    float3 RayDir = normalize(float3(0.0f, 2.5f, 20.0f) - RayOrigin); //mul(_CameraInverseProjection, float4(uv, 0, 1)).xyz;

    Ray ray = CreateRay(RayOrigin, RayDir);
    Triangle triangle = CreateTriangle(float3(-2.0f, 1.0f, 3.0f), float3(2.0f, 1.0f, 3.0f), float3(0.0f, 4.0f, 3.0f));

    float4 Col = float4(0.0f, 0.0f, 0.0f, 0.0f);

    bool RayHit = IntersectRayTriangle(ray, triangle, out float3 RayHitPos);

    if(RayHit == true)
    {
        Col = float4(1.0f, 1.0f, 1.0f, 0.0f);
    }

    Result[id.xy] = Col;
    //Result[id.xy] = float4(id.x & id.y, (id.x & 15)/15.0, (id.y & 15)/15.0, 0.0);
}

I searched for the solution on other websites, but it seems like most people dont use HLSL. I asked GPT-4 about this and it didn’t find any error.

2

Answers


  1. Chosen as BEST ANSWER

    So I tried to rename "triangle" to "meshtriangle" and it worked. It seems that the word "triangle" is a reserved keyword in HLSL. You can find them here:

    https://learn.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl-appendix-keywords


  2. I believe the issue is that intersectionPos is an out parameter but not all your possible condition branches assign a value to it.

    You probably need something like

    if (t > 0.00001f) {
        intersectionPos = ray.origin + ray.direction * t;
        return true;
    }
    
    intersectionPos = ray.origin; // or basically any fallback value
    return false;
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search