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 :
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 :
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.