GLSL compile error '/' does not operate on 'vec4' and 'vec2' - glsl

Part of my fragment shader not compiling
vec2 uv=gl_FragCoord/uTDOutputInfo.res.zw;
returns the following error
'/' does not operate on 'vec4' and 'vec2'
Also tried which gives same error.
vec2 uv=gl_FragCoord/iResolution.xy;

The type of gl_FragCoord is vec4.
It is not possible to divide a vector with 4 components by a vector with 2 components.
Use Swizzling to get the x and y component of gl_FragCoord:
vec2 uv=gl_FragCoord.xy/iResolution.xy;

Related

Can I access the spare floats in a mat3 in GLSL?

I'm using a mat3 uniform in a GLSL shader.
layout (std140)
struct UNI {
mat3 mat;
} u;
A mat3 is stored as 3 rows of vec4 in GLSL so that means there's 3 unused floats in there.
Is there any way to access those floats? I want to to pack some more data in there.
nb. I tried using u.mat[0][3] but it says "array index out of bounds" when I compile it.
I think you are mixing two things here:
the actual memory layout of the data
the API that GLSL provides you to access the data
When you are using Uniform Buffer Objects (UBO) to back the storage of the mat3 and are using the std140 layout, then yes, a mat3 is layed out in exactly the same way as 3 vec4 values. So, essentially, under std140 layout there is no difference in the memory layout between a mat3x4 (three columns each with 4 rows/elements) and a mat3 or mat3x3.
However, you cannot access the fourth row of each column when using a mat3 as the API type in the GLSL shader.
What you could do instead is simply use the mat3x4 type, which would give you access to the fourth row. And when you need a mat3 you can cast it with mat3(theMat3x4Value).

C++/OpenGL glm Can't plug matrix into uniform in shader

I can't use my matrix in my OpenGL shader; this is giving me an error:
glUniform4f(matLocation, mat[0], mat[1], mat[2], mat[3]); // glm mat4
no suitable conversion function from "glm::tvec4<float, glm::highp>" to "GLfloat" exists
I don't entirely understand why it's giving me this error message, and as usual, Googling doesn't give me any good results. Why would it want me to convert matrices to floats?
glUniform4f only updates a vec4 with four float values, so you need glUniformMatrix4fv which takes a pointer to an array of 16 float values glm can provide with glm::value_ptr.
mat[n] only yields the nth column, a vec4; thus the compilation error.

Uniform and const in GLSL; Unity Shader

I'm writing shaders in GLSL for Unity and I've come across a quirk that I can't find an explanation for:
In a ShaderLab SubShader Pass, I have declared vec3s within GLSLPROGAM ENDPROGRAM and outside the main function
const vec3 binomial_offsets = vec3(0.0, 1.3846153846, 3.2307692308);
const vec3 binomial_weights = vec3(0.2270270270, 0.3162162162, 0.0702702703);
This compiles fine.
However, when I replace const with uniform, for example:
uniform vec3 binomial_offsets = vec3(0.0, 1.3846153846, 3.2307692308);
uniform vec3 binomial_weights = vec3(0.2270270270, 0.3162162162, 0.0702702703);
Unity complains with:
GLSL shader load error (stage 1 shader 40):
ERROR: 0.40: Initializer not allowed
Can someone please tell me what the difference between uniform and const are in the context of GLSL (I'm familiar with the const qualifier in C/C++)
I'm trying to implement a gaussian blur by the way, if anyone is interested
You can't initialise uniforms in GLSL. Uniform is different than const. Uniform must take values from your c++/unity program

fragment shader if statment and loading array of mat4 into uniform

I have problem with if here, for reasons uknown to me, it dosnt work.
When i delete if statment or malualy write shadowMap[0] 1 or 2 it works fine, but with if i just get set of white triangles and squares.
here is part of my frag shader
float shadow(float bias)
{
float visibility = 0;
int index=1;
if(gl_FragCoord.z<1.0){
index=0;
}
vec4 shadowCoord=depthPV*vPos;
if ( texture(shadowMap[index], shadowCoord.xy).z < shadowCoord.z+bias){
visibility = -1;
}
return visibility;
}
Other problem i have is with loading array of mat4 into uniform here is code i tried, but it dosnt work, i use lwjgl 3 libery in java
shadowPVs=GL20.glGetUniformLocation(pId, "shadowPVs");
ByteBuffer shadowPVbuff=BufferUtils.createByteBuffer(shadePV.length*16*4);
for(int i=0;i<shadePV.length;i++){
for(int v=0;v<16;v++){
shadowPVbuff.putFloat(shadePV[i].val[v]);
}
}
shadowPVbuff.flip();
GL20.glUniformMatrix4f(shadowPVs, shadePV.length, false, shadowPVbuff);
and in shader
uniform mat4 shadowPVs[3];
What you are trying to do is not possible with current GL.
As #gemse already pointed out in the comments, the relevant part of the GLSL 3.30 spec is:
Samplers aggregated into arrays within a shader (using square brackets
[ ]) can only be indexed with integral constant expressions (see
section 4.3.3 “Constant Expressions”).
In GL 4, this constraint has been somehwat relaxed, but not far enough. In the most current GLSL 4.50 spec, the statement is:
When aggregated into arrays within a shader, samplers can only be
indexed with a dynamically uniform integral expression, otherwise
results are undefined.
With dynamically uniform being defined as
A fragment-shader expression is dynamically uniform if all fragments
evaluating it get the same resulting value. When loops are involved,
this refers to the expression's value for the same loop iteration.
When functions are involved, this refers to calls from the same call
point. This is similarly defined for other shader stages, based on the
per-instance data they process. Note that constant expressions are
trivially dynamically uniform. It follows that typical loop counters
based on these are also dynamically uniform.
Your index depends on data which can vary per fragment, so you are getting undefined results.

How to get a value from vec3 in vertex shader? OpenGL 3.3

I have the following vertex shader:
#version 330
layout (location = 0) in vec3 Position;
uniform mat4 gWVP;
out vec4 Color;
void main()
{
gl_Position = gWVP * vec4(Position, 1.0);
};
How can I get, for example, the third value of vec3? The first my thought was: "Maybe I can get it by multiplying this vector(Position) on something?" But I am not sure that something like "vertical vector type" exists.
So, what is the best way? I need this value to set the color of the pixel.
There are at least 4 options:
You can access vector components with component names x, y, z, w. This is mostly used for vectors that represent points/vectors. In your example, that would be Position.z.
You can use component names r, g, b, a. This is mostly used for vectors that represent colors. In your example, you could use Position.b, even though that would not be very readable. On the other hand, Color.b would be a good option for the other variable.
You can use component names s, t, p, q. This is mostly used for vectors that represent texture coordinates. In our example, Position.p would also give you the 3rd component.
You can use the subscript notation with 0-based indices. In your example, Position[2] also gives he 3rd element.
Each vector has overloaded access to elements. In this case, using Position.z should work.