<p>Good Morning. I'm testing Path Tracing techniques with unity, following this tutorial.
<a href="http://three-eyed-games.com/2019/03/18/gpu-path-tracing-in-unity-part-3/" rel="nofollow noreferrer">http://three-eyed-games.com/2019/03/18/gpu-path-tracing-in-unity-part-3/</a>
But I'm in trouble.</p>
<p>Whenever I try to run the compute shader it crashes the system.</p>
<p>I was erasing some lines and found that the problem happens when I try to change the values ​​of the hit.</p>
<pre><code>    void IntersectMeshObject(int index, CameraRay ray, inout CameraRayHit bestHit)
{
    int offset = _Objects[index].indices_offset;
    int count = offset + _Objects[index].indices_count;

    for (int i = offset; i &lt; count; i += 3)
    {
       float3 v0 = (mul(_Objects[index].localToWorldMatrix, float4(_Vertices[_Indices[i]], 1))).xyz;
       float3 v1 = (mul(_Objects[index].localToWorldMatrix, float4(_Vertices[_Indices[i + 1]], 1))).xyz;
       float3 v2 = (mul(_Objects[index].localToWorldMatrix, float4(_Vertices[_Indices[i + 2]], 1))).xyz;

       float t, u, v;

        if (IntersectTriangle(ray, v0, v1, v2, t, u, v))
        {
            if (t &gt; 0 &amp;&amp; t &lt; bestHit.rayHitDistance)
            {
                bestHit.rayHitDistance = t; //&lt;--That makes it crash
                bestHit.rayHitPosition = ray.raySource + t * ray.rayDirection;//&lt;--That makes it crash
                bestHit.rayHitNormal = float3(0.0f, 1.0f, 0.0f);//&lt;--That makes it crash

                bestHit.viewDirection = normalize(_WorldSpaceCameraPos - bestHit.rayHitPosition);//&lt;--That makes it crash
                bestHit.lightDirection = _DirectionalLightDirection;//&lt;--That makes it crash

                bestHit.rayHitAlbedo = float4(0.6, 0.6, 0.6, 1);//&lt;--That makes it crash
                bestHit.rayHitEmission = float4(0, 0, 0, 0);//&lt;--That makes it crash
                bestHit.rayHitSmoothness = 0.6;//&lt;--That makes it crash
                bestHit.rayHitMetallic = 0.6;//&lt;--That makes it crash
                bestHit.rayHitAnisotropy = 0;//&lt;--That makes it crash

                bestHit.hitRenderType = 0;//&lt;--That makes it crash
            }
        }
    }
}
</code></pre>
<p><a href="https://i.sstatic.net/VJMFZ.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/VJMFZ.png" alt="enter image description here" /></a></p>
<p>When executing only with sphere hits
<a href="https://i.sstatic.net/xnMVu.jpg" rel="nofollow noreferrer"><img src="https://i.sstatic.net/xnMVu.jpg" alt="enter image description here" /></a></p>
<p>When I leave the if without instructions it works normally.</p>
<p>All codes:
<a href="https://gist.github.com/MatheusMarkies/3a334b97d3ee4330c335143e496ded67" rel="nofollow noreferrer">https://gist.github.com/MatheusMarkies/3a334b97d3ee4330c335143e496ded67</a></p>
<p>Can someone help me find out where the problem is?</p>

CC BY-SA 4.0
2021-04-03T23:54:20.507Z
Doing some tests I found that the error is in this FOR.
Probably the way I'm saving the mesh information is wrong, but I don't know why.
Matheus Markies
CC BY-SA 4.0
2021-04-04T09:56:41.187Z
The inout variable bestHit is most likely not valid, reads will usually work, but writes will cause a crash. The caller my be over running the buffer, or some other code may be stepping on bestHit and creating a wild reference.
pmw1234
<raytracing><pathtracing><compute-shader><hlsl><unity>
Unity path tracing makes the pc crash
Matheus Markies
# Unity path tracing makes the pc crash

(Asked by: username_0 on 2021-04-02)

Good Morning. I'm testing Path Tracing techniques with unity, following this tutorial.
<http://three-eyed-games.com/2019/03/18/gpu-path-tracing-in-unity-part-3/>
But I'm in trouble.

Whenever I try to run the compute shader it crashes the system.

I was erasing some lines and found that the problem happens when I try to change the values ​​of the hit.

```
    void IntersectMeshObject(int index, CameraRay ray, inout CameraRayHit bestHit)
{
    int offset = _Objects[index].indices_offset;
    int count = offset + _Objects[index].indices_count;

    for (int i = offset; i < count; i += 3)
    {
       float3 v0 = (mul(_Objects[index].localToWorldMatrix, float4(_Vertices[_Indices[i]], 1))).xyz;
       float3 v1 = (mul(_Objects[index].localToWorldMatrix, float4(_Vertices[_Indices[i + 1]], 1))).xyz;
       float3 v2 = (mul(_Objects[index].localToWorldMatrix, float4(_Vertices[_Indices[i + 2]], 1))).xyz;

       float t, u, v;

        if (IntersectTriangle(ray, v0, v1, v2, t, u, v))
        {
            if (t > 0 && t < bestHit.rayHitDistance)
            {
                bestHit.rayHitDistance = t; //<--That makes it crash
                bestHit.rayHitPosition = ray.raySource + t * ray.rayDirection;//<--That makes it crash
                bestHit.rayHitNormal = float3(0.0f, 1.0f, 0.0f);//<--That makes it crash

                bestHit.viewDirection = normalize(_WorldSpaceCameraPos - bestHit.rayHitPosition);//<--That makes it crash
                bestHit.lightDirection = _DirectionalLightDirection;//<--That makes it crash

                bestHit.rayHitAlbedo = float4(0.6, 0.6, 0.6, 1);//<--That makes it crash
                bestHit.rayHitEmission = float4(0, 0, 0, 0);//<--That makes it crash
                bestHit.rayHitSmoothness = 0.6;//<--That makes it crash
                bestHit.rayHitMetallic = 0.6;//<--That makes it crash
                bestHit.rayHitAnisotropy = 0;//<--That makes it crash

                bestHit.hitRenderType = 0;//<--That makes it crash
            }
        }
    }
}

```

[![enter image description here](https://i.sstatic.net/VJMFZ.png)](https://i.sstatic.net/VJMFZ.png)

When executing only with sphere hits
[![enter image description here](https://i.sstatic.net/xnMVu.jpg)](https://i.sstatic.net/xnMVu.jpg)

When I leave the if without instructions it works normally.

All codes:
<https://gist.github.com/username_0/3a334b97d3ee4330c335143e496ded67>

Can someone help me find out where the problem is?

**Tags:** raytracing, pathtracing, compute-shader, hlsl, unity

> **Comments:**
> **username_0**: Doing some tests I found that the error is in this FOR. Probably the way I'm saving the mesh information is wrong, but I don't know why.  
> **username_1**: The inout variable bestHit is most likely not valid, reads will usually work, but writes will cause a crash. The caller my be over running the buffer, or some other code may be stepping on bestHit and creating a wild reference.  

---

## 0 answers

