# Passing data to shader using texture

**URL:** https://discourse.processing.org/t/passing-data-to-shader-using-texture/44446
**Category:** Coding Questions
**Created:** [May 18, 2024, 10:44pm UTC](https://discourse.processing.org/t/passing-data-to-shader-using-texture/44446 "2024-05-18T22:44:45Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![ekez](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/ekez/32/19589_2.png) [@ekez](https://discourse.processing.org/u/ekez)
#### Post date: [May 18, 2024, 10:44pm UTC](https://discourse.processing.org/t/passing-data-to-shader-using-texture/44446/1 "2024-05-18T22:44:46Z")

</div>

Hi there,

I want to pass timeseries data into a shader. Because the data is potentially quite long, I am trying to do so by writing the data to a texture, then passing the texture to my shader. I’m finding the texture doesn’t get passed into the shader correctly.

Here’s a [small sketch](https://editor.p5js.org/0xekez/sketches/SiOFgeVlG) showing this.

Here’s a class I’ve written to manage the texture.

```auto
class TextureArray {
  constructor(length, gl) {
    this.data = new Float32Array(length)
    this.gl = gl

    const texture = gl.createTexture()
    gl.bindTexture(gl.TEXTURE_2D, texture)
    gl.texImage2D(gl.TEXTURE_2D, 0, gl.R32F, this.data.length, 1, 0, gl.RED, gl.FLOAT, this.data)
    gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE)
    gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE)
    gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR)
    gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)
    this.texture = texture
  }

  push(x) {
    for (let i = this.data.length - 1; i > 0; i -= 1) {
      this.data[i] = this.data[i - 1]
    }
    this.data[0] = x

    const gl = this.gl
    gl.bindTexture(gl.TEXTURE_2D, this.texture)
    gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, this.data.length, 1, gl.RED, gl.FLOAT, this.data)
  }
}

```

Then, I pass this texture to the shader using the following code:

```auto
s = createShader(vs, fs)
shader(s)
const uniform = s.uniforms["meow"]
const location = uniform.location
s.useProgram()
gl.activeTexture(gl.TEXTURE0 + uniform.samplerIndex)
gl.bindTexture(gl.TEXTURE_2D, a.texture)
gl.uniform1i(location, uniform.samplerIndex)

```

However, in the shader the meow texture only contains zero values (no matter how I initialize the input texture array). I was wondering if anyone here sees something wrong with the code. Thanks!

---

<div class="post-metadata">

### Author: ![davepagurek](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/davepagurek/32/17535_2.png) [@davepagurek](https://discourse.processing.org/u/davepagurek)
#### Post date: [July 26, 2024, 12:15pm UTC](https://discourse.processing.org/t/passing-data-to-shader-using-texture/44446/2 "2024-07-26T12:15:06Z")

</div>

Try using a p5.Framebuffer to create your textures (updating their pixels array) and then using that for data. I have an example of that here, passing a bunch of points into a shader: [2.5d blob test - OpenProcessing](https://openprocessing.org/sketch/2308573)

---

<div class="post-metadata">

### Author: ![scudly](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/scudly/32/5597_2.png) [@scudly](https://discourse.processing.org/u/scudly)
#### Post date: [October 25, 2024, 12:13am UTC](https://discourse.processing.org/t/passing-data-to-shader-using-texture/44446/3 "2024-10-25T00:13:27Z")

</div>

Note that because p5 pre-multiplies textures by their alpha value, if you try to pass in data on all four channels, the alpha will wipe out the other values. You can disable this with

```auto
let gl = myFB.gl;
gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, false);
myFB.updatePixels();

```

@davepagurek, perhaps we could add an optional parameter to `updatePixels()` to temporarily disable the alpha pre-multiply to better support data textures.
