Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebGL: Is it forbidden to bind the same uniform in a vertex shader and fragment shader?

Tags:

I am porting an OpenGL framework to JavaScript using Emscriten.

The state is stored in a uniform struct:

struct UniformState {
    ...
    mat4 modelviewprojection_matrix; 
    mat4 modelview_matrix;
    mat3 normal_matrix; 
    mat4 texture_matrix;
    ...
};

that I'd like to access in both the vertex and the fragment shaders as:

uniform UniformState GLUP;

when I do that, I get an error at link time:

Uniform `GLUP`is not linkable between attached shaders

Is it forbidden to bind the same uniform in the vertex shader and fragment shader ?

(if it is forbidden, then clearly I can declare two different sets of uniform variables for each shader, but I'd prefer to have only one, since it makes the code cleaner / simpler, this is why I'm asking in case there is something special to do to make it possible).

like image 433
BrunoLevy Avatar asked May 04 '16 08:05

BrunoLevy


People also ask

What are uniforms in WebGL?

A uniform is a global Shader variable declared with the "uniform" storage qualifier. These act as parameters that the user of a shader program can pass to that program. Their values are stored in a program object.

How do I use WebGL multiple shaders?

So, if you want to render two objects with two different shaders, you should bind one object (it's buffers actually), use first material ( gl. useProgram(shaderProgramA) ) and dispatch draw. Then bind second object, set second material ( gl. useProgram(shaderProgramB) ) and dispatch another call.

How many times a vertex shader is called?

Your shader is called once per vertex. Each time it's called you are required to set the special global variable, gl_Position to some clip space coordinates. Vertex shaders need data. They can get that data in 3 ways. Attributes (data pulled from buffers)

What is vertex shader in WebGL?

WebGL runs on the GPU on your computer. As such you need to provide the code that runs on that GPU. You provide that code in the form of pairs of functions. Those 2 functions are called a vertex shader and a fragment shader and they are each written in a very strictly typed C/C++ like language called GLSL.


1 Answers

Seems to work for me.

Note as Reto mentioned if I don't set the precision I get a link error

"use strict";
var gl = document.createElement("canvas").getContext("webgl");
var program = twgl.createProgramFromScripts(gl, ["vs", "fs"], [], [], log);
log("--done--");

function log(msg) {
  var elem = document.createElement("pre");
  elem.appendChild(document.createTextNode(msg));
  document.body.appendChild(elem);
}
  <script id="vs" type="notjs">
struct Test {
  vec4 color;
  vec4 mult;
};

uniform Test test;
  
attribute vec4 position;

void main() {
  gl_Position = position * test.mult;
}
  </script>
  <script id="fs" type="notjs">
precision highp float;

struct Test {
  vec4 color;
  vec4 mult;
};

uniform Test test;

void main() {
  gl_FragColor = test.color;
  }
  </script>
  <script src="https://twgljs.org/dist/twgl.min.js"></script>
like image 170
gman Avatar answered Sep 28 '22 02:09

gman