I got a GLSL Shader with those uniform variables:
#version 150
layout (shared) uniform GlobalData {
uniform mat4 worldMatrix;
uniform mat4 projMatrix;
// ...
};
How do I set those two matrices from my program? Unfortunately I can not edit the GLSL code since I am only writing a plugin for an existing application.
I tried:
glGetUniformLocation(program, "GlobalData.worldMatrix");
glGetUniformLocation(program, "worldMatrix");
neither works.
Teh GlobalData block has quite a lot more other uniforms that I don't want to change - all I care about is the worldMatrix.
How do I do that?
I am only writing a plugin for an existing application.
Then you don't. Not unless you can access the buffer object that the plugin will use to store those matrices.
GlobalData is a uniform block; it represents the shader-side of a UBO. The C++ side will store the uniform data in a buffer object. So unless your application gives you access to that buffer, there's nothing you can do about it.
You might be able to binding your own buffer, but unless the application is designed to let you override its buffers, it will probably override whatever you bind before rendering takes place.
Related
I am compiling this shader with glslangValidator, and am using OpenGL 4.3 Core Profile with the extension GL_ARB_gl_spirv, and using the GLAD webservice to generate function pointers to access the Core OpenGL API, and SDL2 to create the context, if that is of any use.
If I have a fragment shader like so:
#version 430 core
//output Fragment Color
layout (location = 0) out vec4 outFragColor;
//Texture coordinates passed from the vertex shader
layout (location = 0) in vec2 inTexCoord;
uniform sampler2D inTexture;
void main()
{
outFragColor = texture(inTexture, inTexCoord);
}
How would I be able to set which texture unit inTexture uses?
From what I have read online through documents, I cannot use glGetUniformLocation to get the location of this uniform to use in glUniform1i, because of the fact I am using SPIR-V, instead of GLSL directly.
What would I need to do to set it in a fashion like glUniform1i? Do I need to set the location in the layout modifier? The binding? I've tried to use Uniform Buffer Objects, but apparently sampler2D can only be a uniform.
Since GLSL 4.20, you have the ability to set the binding point for any opaque type like samplers from GLSL code directly. This is done through the binding layout qualifier:
layout(binding = #) uniform sampler2D inTexture;
The # here is whatever binding index you want to use. This is the index you use when binding the texture: glActiveTexture(GL_TEXTURE0 + #). That is, you don't need to use glProgramUniform to set the uniform's value anymore; you already set it in the shader.
If you want to modify the binding dynamically (and you shouldn't), GLSL 4.30 offers the ability to set the location for any non-block uniform value via the location layout qualifier:
layout(location = #) uniform sampler2D inTexture;
This makes # the uniform's location, which you can pass to any glProgramUniform call.
I have a UBO defined in my vertex shader as follows:
layout (std140) uniform matricesBuffer
{
mat4 MVPMatrix;
mat4 modelMatrix;
};
I spent ages figuring out why the matrix I needed wasn't getting through. Then I added the same uniform block to the fragment shader, and it magically worked. So my question is, I know there are specifications about interface matching between shader programs/stages when using separable shaders in a pipeline, but from what I've read these only describe the in and out variables, but this uniform block, or UBO it is, should be accessible from just the vertex shader, as I linked the UBO to that binding point. I'm confused about this, any info would be appreciated.
Edit: I've been trying some things, putting just:
layout (std140) uniform anyName
{
mat4 worldMatrix;
};
in the fragment shader makes it work. And if I change "anyName" to certain names it either works or doesn't. For example if it starts with the letter R it doesn't work, then most other letters it works. So confused.
I've written plenty of
#version 330 core
GLSL shaders I'd like to reuse along with the OpenSceneGraph (OSG) 3.2.0 framework, and try to figure out how to get the state from the OSG I need to pass in by uniforms, and how to set them without having to change well-tested shader code, as well as how to populate arbitrarily named attributes.
This (version 140, OpenGL 3.1)
http://trac.openscenegraph.org/projects/osg/browser/OpenSceneGraph/trunk/examples/osgsimplegl3/osgsimplegl3.cpp
and this (version 400)
http://trac.openscenegraph.org/projects/osg/browser/OpenSceneGraph/trunk/examples/osgtessellationshaders/osgtessellationshaders.cpp
example give rise to a notion of aliasing certain attribute and uniform names to "osg_", but I'd like to use arbitrary names for the uniforms,
uniform mat4 uMVMatrix;
/*...*/
and to refer, or let the OSG refer, to the attributes by their numbers only, so sth like this
/*...*/
layout(location = 0) in vec4 aPosition;
layout(location = 1) in vec3 aNormal;
layout(location = 2) in vec2 aST;
as used in my legacy shaders, I'd like the OSG framework to populate with the vbo it already maintains for the "Drawables", or, at least, use an API call and do it myself.
I addition, I'd like to populate uniforms for lights and shawdowmaps by means of the scenegraph and the visitors; "somewhere" and "somehow" in the SG there must be light and esp shadow information be aggregated for default shading, so I'd like simply like to use this data and tailor it to fit to my custom shaders.
So the fundamental question is
How to populate arbitrary GLSL 330 shaders with data from within OSG without having to resent to redundant uniform assignment - providing my "u[..]Matrix" manually in addition to the "osg_[...]" uniform set by OSG - or changing attribute names in the shader sources?
I just stumbled upon this, turns out, you can just use your own names after all, if you just specify the layout location (so far I only tried it for the vertex position, so you might have to take care of using the correct layout location as osg would specify them, i.e. vertex position at 0, normal at 1 (which is not done the example of the link though))
layout (location = 0) in vec3 vertex;
this is enough to use the variable named vertex in the shader.
The link also provides an example to use custom names for matrices: you create an osg::Uniform::Callback class that uploads the matrix to the uniform.
when you create the osg::Uniform object, you specify the name of your choosing and add the callback.
My (fragment) shader has a uniform array containing 12 structs:
struct LightSource
{
vec3 position;
vec4 color;
float dist;
};
uniform LightSource lightSources[12];
In my program I have 12 buffer objects that each contain the data for one light source. (They need to be seperate buffers.)
How can I bind these buffers to their respective position inside the shader?
I'm not even sure how to retrieve the location of the array.
glGetUniformLocation(program,"lightSources");
glGetUniformLocation(program,"lightSources[0]");
These run without invoking an error, but the location is definitely wrong(4294967295). (The array is being used inside the shader, so I don't think it's being optimized out)
As glGetUniformLocation docs say:
name must be an active uniform variable
name in program that is not a structure,
an array of structures, or a subcomponent of a vector or a
matrix.
...
Uniform variables that are structures or arrays of
structures may be queried by calling
glGetUniformLocation for each field within
the structure.
So, you can only query one field at a time.
Like this:
glGetUniformLocation(program,"lightSources[0].position")
glGetUniformLocation(program,"lightSources[0].color")
glGetUniformLocation(program,"lightSources[0].dist")
Hope it helps.
Edit:
You can make your life easier (at a cost of old hardware/drivers compatibility) by using Interface Blocks, Uniform Buffer Objects and glGetUniformBlockIndex. This will be more like DirectX constant buffers. Required hardware/drivers support for that: either OpenglGL 3.1 core or ARB_uniform_buffer_object extension.
I'm trying to write a shader using cg (for ogre3d). I can't seem to parse a working shader that I'd like to use as a starting point for my own code.
Here's the declaration for the shader:
void main
(
float2 iTexCoord0 : TEXCOORD0,
out float4 oColor : COLOR,
uniform sampler2D covMap1,
uniform sampler2D covMap2,
uniform sampler2D splat1,
uniform sampler2D splat2,
uniform sampler2D splat3,
uniform sampler2D splat4,
uniform sampler2D splat5,
uniform sampler2D splat6,
uniform float splatScaleX,
uniform float splatScaleZ
)
{...}
My questions:
iTexCoord0 is obviously an input parameter. Why is it not declared uniform?
(oColor is obviously an output parameter. No question)
covMap1 - splat6 are textures. Are these parameters or something loaded into the graphics card memory (like globals)? The ogre definition for the shader program doesn't list them as parameters.
Are splatScaleX and splatScaleZ also parameters? The ogre definition for the shader program also doesn't list these as parameters.
Does the order of declaration mean anything when sending values from an external program?
I'd like to pass in an array of floats (the height map). I assume that would be
uniform float splatScaleZ,
uniform float heightmap[1024]
)
{...}
If I don't pass one of the parameters will the shader just not be executed (and my object will be invisible because it has no texture)?
Is there a better way to debug these than just hit/miss and guess?
iTexCoord0 is obviously an input parameter. Why is it not declared uniform?
Uniforms are not the same things as input parameters. iTexCoord is a varying input, which is to say that for every vertex, it can have a unique value. This is set with commands like glVertexAttribPointer. Things like vertex coordinates, normals, texcoords, vertex colors, are examples of what you might use a varying input for.
Uniforms are the other hand are intended to be static for an entire draw call, or potentially for the entire frame or life of the program. They are set with glUniform* commands. A uniform might be something like the modelview matrix for an object, or the position of the sun for a lighting calculation. They don't change very often.
[edit] These specific commands I think actually work with GLSL, but the theory should be the same for CG. Lookup a cg specific tutorial to figure out the exact commands to set varyings and uniforms.
covMap1 - splat6 are textures. Are these parameters or something loaded into the graphics card memory (like globals)? The ogre definition for the shader program doesn't list them as parameters.
My CG is a little rusty, but if its the same as GLSL then the sampler2d is a uniform that takes an index that represent which sampler you want to sample from. When you do something like glActiveTexture(GL_TEXTURE3), glBindTexture(n), then you set the sampler uniform to "3" you can sample from that texture.
Does the order of declaration mean anything when sending values from an external program?
No, the variables are referred to in the external program by their string variable names.
If I don't pass one of the parameters will the shader just not be executed (and my object will be invisible because it has no texture)?
Unknown. They will likely have "some" initial value, though whether that makes any sense and will generate anything visible is hard to guess.
Is there a better way to debug these than just hit/miss and guess?
http://http.developer.nvidia.com/CgTutorial/cg_tutorial_appendix_b.html
See section B.3.8