How to pass the texture with data to shader programs?

I would like to pass the array data as a constant in the vertex and fragment programs. And since uniform arrays can only be set with go.set() and not with render.constant_buffer(), the idea is to pass the data as a generated small texture.

Looking at all these examples where people create post effects with a depth buffer etc., I still do not understand how it all works.

As I understand it’s not a problem to create a texture buffer with buffer.create() and buffer.get_stream() to fill the rgb with data (is it truth?)

But can anyone explain step by step what to do with this buffer afterwards? How to pass it as the sampler texture to the shader? And will I be able to transfer this data to the vertex shader in the same way as I would to the fragment shader?

1 Like

This is what I have in the render script now:

function init(self)
  ...
  -- create a render target
  local parameters = {
    format = render.FORMAT_RGB,
    width = 4, height = 4,
    min_filter = render.FILTER_NEAREST,
    mag_filter = render.FILTER_NEAREST,
    u_wrap = render.WRAP_REPEAT,
    v_wrap = render.WRAP_REPEAT
  }
  self.lights_render_target = render.render_target("lights", parameters)

  -- draw the data to the buffer
  self.buffer = buffer.create(16, {{name=hash("rgb"), type=buffer.VALUE_TYPE_UINT8, count=3}} )
  self.stream = buffer.get_stream(self.buffer, hash("rgb"))
  self.stream[1] = 0xff
   ...
  self.stream[16] = 0xff
end

function update(self) 
  ...
  render.set_render_target(self.lights_render_target)
  -- how to draw the texture from the buffer to the render_target here?
  render.set_render_target(render.RENDER_TARGET_DEFAULT)
  render.enable_texture(2, self.lights_render_target, render.BUFFER_COLOR_BIT)
   ...
end

As I understand, the idea of passing a texture directly to a shader in this way works only by using a quad mesh with a special predicate and rendering it.

If that’s the case, it might be easier to register all the meshes I want to set the array to and set it via go.set()… as long as there is no way to set arrays in constants_buffer.

You can use resource.set_texture() to pass the buffer as a texture.
You can check hyper trails as an example. There are some useful encoding functions too.

And no you can pass a buffer as a texture to any kind of material you want. Not just a quad and no need to create another predicate.

2 Likes

Yes, I can set the buffer as a resource. But the question was how to pass it as an additional texture to the vertex shader program for all at once, as it happens, for example, with render.enable_texture(N, self.my_render_target). So I thought about rendering the quad into a separate render target, etc.

Since it seemed weird to me, I decided to go through all 50-200-500 meshes after init the scene and set the one desired buffer texture with data to them as you suggested.

But if there’s still a way to set a buffer for all users of the material without iterating over those users, that would be great.

Sorry, I really didn’t indicate that in the original post. The goal is to set the one texture to all at once, as is possible with constants via render.constant_buffer().

1 Like