# Unable to unroll loop / Forced to unroll loop, but unrolling failed.

**URL:** <https://forums.unrealengine.com/t/unable-to-unroll-loop-forced-to-unroll-loop-but-unrolling-failed/484990>\
**Category:** Rendering\
**Tags:** unreal-engine, Shaders, custom-nodes, hlsl, question\
**Created:** [June 17, 2021, 11:34am UTC](https://forums.unrealengine.com/t/unable-to-unroll-loop-forced-to-unroll-loop-but-unrolling-failed/484990 "2021-06-17T11:34:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![noillimryM](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@noillimryM](https://forums.unrealengine.com/u/noillimryM)\
**Post date:** [June 17, 2021, 11:34am UTC](https://forums.unrealengine.com/t/unable-to-unroll-loop-forced-to-unroll-loop-but-unrolling-failed/484990/1 "2021-06-17T11:34:13Z")

</div>

Hi everybody,

I am currently encountering a problem with some HLSL custom node.

First of all, I created a custom node which allows me to perform a Gaussian Blur on a Texture Object. This custom node takes in values such as the texture ( `Texture2D`), the UV ( `Vector2D`), and a Radius ( `Scalar`).

At some point I have to perform the following loops to visit the colors in a certain radius :

```
for (int x = -Radius; x <= Radius; x++)
{
    for (int y = -Radius; y <= Radius; y++)
    {
        // gaussian blur
    }
}

```

Yes my `Radius` is variable and actually comes from a `Material Parameter Collection`. However, I am not getting any problems at this point and my gaussian blur works perfectly fine. I tested it in Real-Time and it works very well too :

 ![341622-2021-06-16-19h35-10.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/c/1/f/c1f3726a305bb714f4a078494f37419fed5ac752.png)

So this custom node works fine, however in my use case, I would prefer not to Blur the entire Texture but only the edges (to try and smooth them a little). So I basically redid the **exact same** work, except that before performing the Gaussian Blur, I perform an edge detection with the use of Sobel. It goes as follows :

```
float x = 0;

x += Texture2DSample(Tex, TexSampler, UV + float2(-TexelSize.x, -TexelSize.y)).r * -1.0;
x += Texture2DSample(Tex, TexSampler, UV + float2(-TexelSize.x, 0)).r * -2.0;

// ...

float y = 0;

y += Texture2DSample(Tex, TexSampler, UV + float2(-TexelSize.x, -TexelSize.y)).r * -1.0;
y += Texture2DSample(Tex, TexSampler, UV + float2(0, -TexelSize.y)).r * -2.0;

// ...

if (sqrt(x * x + y * y) > 0.5)
{
    for (int x = -Radius; x <= Radius; x++)
    {
        for (int y = -Radius; y <= Radius; y++)
        {
            // gaussian blur
        }
    }
}
else
{
    return Texture2DSample(Tex, TexSampler, UV).r;
}

```

So except the fact that I check if the “pixel” is part of an edge or not first, I do realize the exact same work than the Gaussian Blur.

To me this code should work but I get the following warning and errors :

 ![341623-2021-06-16-19h49-57.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/d/b/1/db172ac065d3c5b0d0d97697dd24cf42cef73bca.png)

After quite a lot of researches done on the matter, I came to ther conclusion that because the compiler cannot know how many iterations will be performed (as the `Radius` is variable), it creates those errors. I tried with a fixed value and it indeed works. **But then** why does it work fine with the Gaussian Blur ? Its `Radius` is also variable so the same issue should logicially apply, no ?

Also (with the edge detection) even with a fixed value, if I push the `Radius` a little too high, the same errors will happen. It is however not the case for the Gaussian Blur. Even if I push its `Radius` very high, it will impact the performances (FPS) and eventually Unreal Engine will crash, but this “unrolling” issue absolutely does not occur.

Why is that ? Why do my Gaussian Blur and my Edge Detection work perfectly seperately, but can’t work when used one after the other ? Would anybody have, please, any insights on that one ? Thanks in advance.

---

<div class="post-metadata">

**Author:** ![noillimryM](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@noillimryM](https://forums.unrealengine.com/u/noillimryM)\
**Post date:** [June 17, 2021, 5:51pm UTC](https://forums.unrealengine.com/t/unable-to-unroll-loop-forced-to-unroll-loop-but-unrolling-failed/484990/2 "2021-06-17T17:51:38Z")

</div>

Up, does anybody have any idea ?

---

<div class="post-metadata">

**Author:** ![Deathrey](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Deathrey](https://forums.unrealengine.com/u/Deathrey)\
**Post date:** [June 18, 2021, 8:13am UTC](https://forums.unrealengine.com/t/unable-to-unroll-loop-forced-to-unroll-loop-but-unrolling-failed/484990/3 "2021-06-18T08:13:18Z")

</div>

You can use LOOP and BRANCH/FLATTEN words above loop and and if respectively.

---

<div class="post-metadata">

**Author:** ![noillimryM](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@noillimryM](https://forums.unrealengine.com/u/noillimryM)\
**Post date:** [June 18, 2021, 9:18am UTC](https://forums.unrealengine.com/t/unable-to-unroll-loop-forced-to-unroll-loop-but-unrolling-failed/484990/4 "2021-06-18T09:18:41Z")

</div>

Wow thanks a lot ! This actually solved my problem and it definitely did improve the performances quite a significant amount !
