How to use buffers in HLSL shaders? (porting a GLSL example)

Here is a Shadertoy example of how to use a buffer.
Simple GLSL buffer example

I don’t have a problem modifying the GLSL to HLSL code…the part I don’t know how to do is to use a buffer in HLSL. How do you do that part?

Here is the GLSL to HLSL conversion which works.
The part that I need to fix/convert is the “iChannel0” stuff

#iChannel0 "file://SBBufferA.hlsl"   <----- HOW TO DO THIS IN HLSL? (works in GLSL preview)

uniform float iTime; // - seconds since preview started
uniform float2 iResolution; // - viewport size in pixels

float4 main(float2 fragCoord)
{  
    float2 uv = fragCoord.xy / iResolution.xy;
    float4 col = texture(iChannel0, uv);   <----- HOW TO DO THIS IN HLSL? (works in GLSL preview)
    float4 fragColor = float4(col.rgb, 1.);
    return fragColor;
}

For this simple example, I can simply port the code used in the buffer, into the main pixel shader.
Which works, but I don’t know if this trick would work for other GLSL shaders that use multiple buffers.

Another trick, perhaps, would be to create a shader for each material separately, like a normal shader. And have one shader “combine” them all?

So if I have a GLSL shader that is set up like this…

One material (original working GLSL shader)

  1. main shader
  2. BufferA
  3. BufferB
  4. BufferC

I can fake it, by doing this (GLSL → HLSL ported shader into Unreal 4 custom HLSL based material)
Four Materials

  1. “main” shader for material
  2. buffer shader for material A
  3. buffer shader for material B
  4. buffer shader for material C
  5. combine the pixel color from 1-4 into final displayed material.

Would that work? Is that a good (doable) solution?

Doesn’t sound like a “good” solution because of performance.
The idea of a buffer is that the information isn’t calculated several times - I think at least, because you can re-calculate it from a buffer too, so it dpeends on what the code actually does.

You may be limited to going with what works anyway.

Also, cant the “buffer” just be any variable you write data to? Maybe defined as a special struct?
I’d have to have a look at the actual shader to get ideas, don’t really have time rn…

Ps: and isnt that basically just a render target in the end? (A texture mapped to a uv).

I am using VSCode…it has an GLSL viewer…
A lot of GLSL examples use buffers.
For simple GLSL shaders, simply copying the buffer code (a seperate GLSL shader file) into main program will work. I do not know if this general procedure will work for all cases.
I looked at a simple GLSL shader + buffer example…converted into to HLSL.
And ported the buffer code, into the main HLSL file. For this simple example this process worked.
But again, I am not sure if the would work for all cases.

Are render targets the HLSL equivalent of GLSL buffers?

Dont think so.
Render targets are essentially any image that the engine sets up to edit and manage at runtime.

Usually you write to them during gameplay and access them in shaders.

In a pinch they can serve as a data buffer, even if writing costs so usually you just read from them…