Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to process arrays of images with gpu-shader #826

Open
nikhedonia opened this issue Sep 18, 2023 · 0 comments
Open

How to process arrays of images with gpu-shader #826

nikhedonia opened this issue Sep 18, 2023 · 0 comments

Comments

@nikhedonia
Copy link

Hey Guys,
I've been now using gpujs to create a complex image processing pipeline and been wondering what is the best way to pass an array of images/textures to a shader?

The naive approach of just passing an array of images is not supported

const sumKernel = gpu.createKernelfunction(images: number[][][][], len: number, weights: number[]) {
    let sums = [0, 0, 0, 0];

    for (let k = 0; k < 4; ++k) {
      for (let i = 0; i < len; ++i) {
        sums[k] += weights[i] * images[i][this.thread.y][this.thread.x][k]
      }
    }
    this.color(sums[0],sums[1],sums[2],sums[3]);
  })
  
 sumKernel([image1, image2], 2, [0.5, 0.5);

I see couple alternative solutions eg. draw all images side by side and then do some index fiddling:

const sumKernel = gpu.createKernelfunction(image: number[][][], len: number, weights: number[]) {
    let sums = [0, 0, 0, 0];

    for (let k = 0; k < 4; ++k) {
      for (let i = 0; i < len; ++i) {
        sums[k] += weights[i] * images[this.thread.y][this.thread.x + i * this.output.width][k] // assuming all images have same dimensions
      }
    }
    this.color(sums[0],sums[1],sums[2],sums[3]);
  })
  
 sumKernel([image1, image2], 2, [0.5, 0.5);

What is best practice?

@nikhedonia nikhedonia changed the title processing arrays of images with gpu-shader How to process arrays of images with gpu-shader Sep 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant