<p>Yes the raygen shader used here is correct for shooting rays inside the frustum given by the projection matrix(whose inverse is used here).</p>

CC BY-SA 4.0
2022-08-28T05:52:26.163Z
2022-08-28T05:52:26.163Z
VersesDev
<p>Hi I want to create a ray generation shader which traces rays inside a box frustum given by an Orthographic projection matrix(XMMatrixOrthographicLH) I use the following ray generation shader code:</p>
<pre><code>struct RTConstants
{

    matrix invproj;
};

struct Simpleraypayload
{
    float3 outcol;
};
RWTexture2D&lt;float4&gt; outtex: register(u0);
RaytracingAccelerationStructure basicas:register(t0);
ConstantBuffer&lt;RTConstants&gt; rtconsts:register(b0);
[shader(&quot;raygeneration&quot;)]
void rgsmain()
{
Simpleraypayload payload;
payload.outcol=float3(0.0f,0.0f,0.0f);
RayDesc ray;


ray.TMin=0.001f;
ray.TMax=10.0f;
ray.Direction=float3(0.0f,0.0f,-1.0f);
    uint3 rayidx=DispatchRaysIndex();
    uint3 raydims=DispatchRaysDimensions();
    float2 clippointxy=rayidx.xy/float2(raydims.xy);
    clippointxy=(clippointxy*2.0f)-float2(1.0f,1.0f);
    float4 clippointnear=float4(clippointxy,0.0f,1.0f);
    clippointnear=mul(rtconsts.invproj,clippointnear);clippointnear=clippointnear/clippointnear.w;
    float4 clippointfar=float4(clippointxy,1.0f,1.0f);
    clippointfar=mul(rtconsts.invproj,clippointfar);clippointfar=clippointfar/clippointfar.w;
    ray.Origin=clippointnear.xyz;
    ray.Direction=normalize(clippointfar.xyz-clippointnear.xyz);
    TraceRay(basicas,RAY_FLAG_NONE,0xFF,0,0,0,ray,payload);
    //flipping the output.
    uint2 outindex=uint2(rayidx.x,raydims.y-rayidx.y);
    
    outtex[outindex]=float4(payload.outcol,1.0f);
}
</code></pre>
<p>here &quot;invproj&quot; is the inverse of the ortho projection matrix for which we are trying to shootrays for.Am I right in using this rgs shader code for tracing all possible rays inside the frustum represented by the ortho matrix whose inverse I am using here?I have tested it for ortho projection only though.</p>

CC BY-SA 4.0
2024-09-16T21:32:18.480Z
You can do a bit better I think by setting the ray origin at the camera origin, and then firing it at the close plane. This reduces the number of multiplications you need to do whilst also allowing for an infinite far plane.
Puppy
CC BY-SA 4.0
2024-09-17T10:24:53.683Z
That code listing is just a big mess, I'm not even going to attempt to read that, kindly clean it up.
pmw1234
<raytracing><directx12>
Am I creating my raygeneration shader correctly
VersesDev
# Am I creating my raygeneration shader correctly

(Asked by: username_0 on 2022-08-12)

Hi I want to create a ray generation shader which traces rays inside a box frustum given by an Orthographic projection matrix(XMMatrixOrthographicLH) I use the following ray generation shader code:

```
struct RTConstants
{

    matrix invproj;
};

struct Simpleraypayload
{
    float3 outcol;
};
RWTexture2D<float4> outtex: register(u0);
RaytracingAccelerationStructure basicas:register(t0);
ConstantBuffer<RTConstants> rtconsts:register(b0);
[shader("raygeneration")]
void rgsmain()
{
Simpleraypayload payload;
payload.outcol=float3(0.0f,0.0f,0.0f);
RayDesc ray;

ray.TMin=0.001f;
ray.TMax=10.0f;
ray.Direction=float3(0.0f,0.0f,-1.0f);
    uint3 rayidx=DispatchRaysIndex();
    uint3 raydims=DispatchRaysDimensions();
    float2 clippointxy=rayidx.xy/float2(raydims.xy);
    clippointxy=(clippointxy*2.0f)-float2(1.0f,1.0f);
    float4 clippointnear=float4(clippointxy,0.0f,1.0f);
    clippointnear=mul(rtconsts.invproj,clippointnear);clippointnear=clippointnear/clippointnear.w;
    float4 clippointfar=float4(clippointxy,1.0f,1.0f);
    clippointfar=mul(rtconsts.invproj,clippointfar);clippointfar=clippointfar/clippointfar.w;
    ray.Origin=clippointnear.xyz;
    ray.Direction=normalize(clippointfar.xyz-clippointnear.xyz);
    TraceRay(basicas,RAY_FLAG_NONE,0xFF,0,0,0,ray,payload);
    //flipping the output.
    uint2 outindex=uint2(rayidx.x,raydims.y-rayidx.y);
    
    outtex[outindex]=float4(payload.outcol,1.0f);
}

```

here "invproj" is the inverse of the ortho projection matrix for which we are trying to shootrays for.Am I right in using this rgs shader code for tracing all possible rays inside the frustum represented by the ortho matrix whose inverse I am using here?I have tested it for ortho projection only though.

**Tags:** raytracing, directx12

> **Comments:**
> **username_1**: You can do a bit better I think by setting the ray origin at the camera origin, and then firing it at the close plane. This reduces the number of multiplications you need to do whilst also allowing for an infinite far plane.  
> **username_2**: That code listing is just a big mess, I'm not even going to attempt to read that, kindly clean it up.  

---

## 1 answers

### Answer by username_0 on 2022-08-28. Score: 0

Yes the raygen shader used here is correct for shooting rays inside the frustum given by the projection matrix(whose inverse is used here).


---

