I have a very simple GLSL buffer example…
Now I don’t need help porting the “regular” GLSL code to HLSL, but I do need help with one GLSL specific thing.
Now I did have one approach I used which worked, and I was able to create a custom HLSL shader material in UE4, while this approach works fine for this simple example, I was wondering if I can get a more precise equivalent version in HLSL…maybe there is one, I am not sure.
It’s a simple shader that produces a nice animated gradient effect.
Attached is both the GLSL version and the HLSL version…
SimpleBuffer.zip (1.7 KB)
Here is the GLSL version…
#iChannel0 "file://SBBufferA.glsl"
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec2 uv = fragCoord.xy / iResolution.xy;
vec4 col = texture(iChannel0, uv); <--- THIS IS THE LINE THAT I NEED HELP WITH
fragColor = vec4(col.rgb, 1.);
}
I need help converting “iChannel0” to it’s HLSL equivalent or the equivalent nodes I need to use in the material editor in UE4.
Here is one solution for HLSL version
#include "SBBufferA.hlsl"
uniform float iTime; // - seconds since preview started
uniform float2 iResolution; // - viewport size in pixels
float4 main(float2 fragCoord : TEXCOORD0) : SV_Target
{
float2 iResolution2 = float2(1,1);
float2 uv = fragCoord.xy / iResolution2.xy;
// HLSL version that simulates an IChannelx in GLSL
//texture(iChannel0, uv);
float4 col = mainA(fragCoord, iTime, iResolution2); <-- I call the main function in my simulated "buffer"
float4 fragColor = float4(col.rgb, 1.);
return fragColor;
}
While this approach works, is there a more exact, equivalent version either in HLSL or with using nodes in the material editor in UE4.27?
Can you “sample” from a separate Texture the way that the GLSL does?
Or sample one material from another material in UE4?
(I’m still learning, so maybe this answer is very obvious to some)
There are some GLSL examples that use buffers, where my simple solution would work.
But I have seen complex buffer examples, that seem to be recursive, that I don’t think my version would work with. (In one test, the performance took a nose dive in HLSL version, usually the HLSL is a lot faster…for “normal” GLSL ports)
Probably a regular (non HLSL custom material), would be the easiest version to make and show the best way to do this kind of thing…
For example some GLSL examples, have up to 4 channels in use, and the main GLSL program samples the results from all 4 channels to create a final output.
By “recursive”, I’ve seen GLSL examples, where the buffer refers to it’s own self…
For example this GLSL code fragment…
The name of the file this is in is “BufferA.glsl”
#include "Common.glsl"
#iChannel0 "file://BufferD.glsl"
#iChannel1 "file://BufferA.glsl" <-------- IT'S REFERRING TO ITSELF AS A BUFFER
vec3 sampleMinusGradient(vec2 coord)
{
vec3 veld = texture(iChannel1, coord / iResolution.xy).xyz; <--- IT IS REFERRING TO iChannel1.
BUT THIS IS ITSELF(???)
float left = texture(iChannel0,(coord + vec2(-1, 0)) / iResolution.xy).x;
.... CODE .....
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec2 velocity = sampleMinusGradient(fragCoord).xy;
vec3 veld = sampleMinusGradient(fragCoord - dissipation * velocity).xyz;
.... CODE .....
}
So is this a recursive shader? If so, how would it terminate the recursion?
There is no escape statement to end recursion.