Change shader uniforms in the render script

I’m trying to make an optimal blur shader, for that im using learnopengl’s method:
https://learnopengl.com/Advanced-Lighting/Bloom

Thing is, this method requires the modification of a single uniform variable in the shaders. The way i aproached that is by sending a message to the go that holds the mesh which has this shader. This turned out to not work, i think its bc its not fast enough (maybe). I do this:

local first_iteration = true 
    local amount = 10   
    for i = 1, amount do
      render.set_render_target(self.blur_fbo[boolToNumber(horizontal)])
      msg.post(blur_go, hash'blur_dir', {horizontal = boolToNumber(horizontal)})

      if first_iteration then
        render.disable_render_target(self.game_world_fbo)
        render.enable_texture(0, self.game_world_fbo, render.BUFFER_COLOR0_BIT)
        render.draw(self.post_processing_pred)

        first_iteration = false

      else
        render.enable_texture(0, self.blur_fbo[boolToNumber(not horizontal)], render.BUFFER_COLOR0_BIT)
        render.draw(self.post_processing_pred)
        render.disable_render_target(self.blur_fbo[boolToNumber(horizontal)])
        
      end
      
      horizontal = not horizontal
      render.disable_texture(0)
    end

this is the C++ code from learnopengl i translated to lua/defold. Thing is, the result i get with this is either the blur is applied only vertically or horizontally, never in both ways:
image
image

The way i would overcome that is with coroutines: i send the message, stop the rendering corroutine, when the go is done continue the corroutine from the go script. IDK how performance wise is that, but its the only thing i can think of, since i cannot call a go.set on rendering script and idk any other way of setting a uniform on the render script. If anyone has any other idea please tell me.

it is also very possible that the lua code is missing something from the original cpp code in learnopengl, im testing that too now

Doing the blur in two passes is an optimized way of applying the (gaussian) blur kernel, and it’s a common way to do it.
I.e. Pass 1) Blur horizontally, Pass 2) blur the result of Pass 1 vertically.

It is much more efficient than doing a full radial blur for each pixel, especially with large kernels.
It uses the fact that a gaussian blur kernel is separable.
Here is a blog post by Alan Wolfe explaining it in more detail.

i have already the radial blur, it costs too damn much of the gpu. I will still try to apply the more optimized way

1 Like

Regarding the loop, the message is posted after the loop, when the render script is done.

Basically, setting properties on the game objects at this point is too late in the pipline.

For setting constants when doing render passes, I suggest you look into render constants