PrevUpHomeNext

Uniforms

#include <oglplus/uniform.hpp>

Basic location operations

template <>
class ProgVarLocOps<tag::Uniform>
{
public:
	static GLint GetLocation(
		ProgramName program,
		StrCRef identifier,
		bool active_only
	); 1
};

1

Finds the location of the uniform variable specified by identifier in a program. If active_only is true then throws ProgVarError if no such uniform exists or if it is not active. See glGetUniformLocation.

Value get/set operations

template <typename OpsTag, typename T>
class ProgVarGetSetOps<OpsTag, tag::Uniform, T>
 : public ProgVarCommonOps<tag::Uniform>
{
public:
	void Set(T value);
	void SetValue(T value); 1

	void SetValues(std::size_t n, const T* values); 2

	void TrySet(T value); 3
};

template <typename OpsTag>
class ProgVarGetSetOps<OpsTag, tag::Uniform, void>
 : public ProgVarCommonOps<tag::Uniform>
{
public:
	template <typename T>
	void Set(T value);

	template <typename T>
	void SetValue(T value); 4

	template <typename T>
	void SetValues(std::size_t n, const T* values); 5
};

1

Sets uniform value.

2

Sets multiple consecutive values.

3

Sets the uniform value if it is active. See glVertexAttrib.

4

Sets uniform value.

5

Sets multiple consecutive values.

Definition

template <typename T>
using Uniform = ProgVar<
	tag::ImplicitSel,
	tag::Uniform,
	tag::NoTypecheck,
	T
>; 1

typedef Uniform<GLint> UniformSampler;

1

Uniform indirectly inherits from ProgVarLocOps and ProgVarGetSetOps.

Untyped

typedef ProgVar<
	tag::ImplicitSel,
	tag::Uniform,
	tag::NoTypecheck,
	void
> UntypedUniform; 1

UntypedUniform
operator / (ProgramName program, StrCRef identifier);

1

Uniform indirectly inherits from ProgVarLocOps and ProgVarGetSetOps.

Examples of usage

Context gl;

VertexShader vs;

vs.Source(
	"#version 150\n"
	"in vec3 Position;\n"
	"void main(void)\n"
	"{\n"
	"	gl_Position = Position;\n"
	"}\n"
).Compile();

FragmentShader fs;

fs.Source(
	"#version 130\n"
	"uniform vec4 Color;\n"
	"void main(void)\n"
	"{\n"
	"	gl_FragColor = Color;\n"
	"}\n"
).Compile();

Program prog;

prog.AttachShader(vs).AttachShader(fs).Link();
gl.Use(prog);

UniformLoc color_loc1(prog, "Color"); 1

assert(color_loc1.IsActive()); 2
assert(color_loc1);

UniformLoc color_loc2(prog, "Color", false 3); 4

assert(color_loc1 == color_loc2); 5

assert(color_loc1.Location() == GetGLLocation(color_loc1)); 6

UniformLoc color_loc3(0); 7

Uniform<Vec4f> color1(prog, "Color"); 8

Uniform<Vec4f> color2(color_loc2); 9

Uniform<Vec4f> color3(prog); 10

color3.BindTo("Color"); 11

color1.Set(Vec4f(1, 0, 0, 1)); 12

Typechecked<Uniform<Vec2f>> color4(prog, "Color"); 13

1

Getting the location of a uniform in a GPU program, by its name. Throws if no such uniform is active in the current program. To get the location of a program which is not currently active, ProgramUniform must be used.

2

Check if the uniform is active.

3

Don't throw if inactive.

4

Getting the location of a uniform in a GPU program, by its name. Does not throw if the uniform is not active.

5

Compare two uniform locations for equality (in both the program name and the location index).

6

Two ways to get the location index.

7

Explicitly initializing a uniform location.

8

Initialize a typed reference to a uniform variable, from a program name and identifier.

9

Initialize a typed reference to a uniform variable, from a uniform location.

10

Initialize a uniform from a program name, the location is not obtained in this case and must be bound later.

11

Late binding of a uniform to an identifier.

12

Setting the value of a GPU program uniform variable. This will throw if the uniform is not active.

13

The Typechecked modifier enables uniform typechecking, which compares the C++ template parameter type to the GLSL data type. In this case the constructor would throw because Vec2f does not match vec4 declared in the fragment shader.

UntypedUniform color5(prog, "Color"); 1

color5.Set(Vec4f(0,0,1,1));

(prog/"Color").Set(Vec4f(0,0,1,1)); 2

Optional<Uniform<Vec4f>> color6(prog, "Blah"); 3

color6.TrySet(Vec4f(0,1,0,1)); 4

Lazy<Uniform<Vec4f>> color7(prog, "Color"); 5

try { color7.Init(); 6 }
catch(...) { /* ... */ }

color7.TryInit(); 7

color7.Set(Vec4f(0,0,0,1)); 8

1

Untyped uniforms do not carry the type information on the C++ side and cannot be typechecked during construction.

2

Syntax sugar operator for constructing untyped uniforms, equivalent to UntypedUniform(prog,"Color").Set(...).

3

The Optional modifier allows to construct instances of Uniform without throwing even if the specified identifier does not refer to an active uniform in the GPU program.

4

If the referenced uniform is active, then this sets its value, but unlike Set, TrySet does not throw if the uniform is inactive.

5

The Lazy modifier allows to postpone the querying of uniform location until it is actually needed at the cost of having to store the identifier of the shader unifom variable in the Lazy<Uniform> instance. This means that the referenced program does not have to be linked and current when the Uniform is constructed.

6

The uniform location can be explicitly queried by calling the Init member function which throws in case of failure.

7

The TryInit function also queries the uniform location, but does not throw, the instance just remains not active.

8

If the location is still not initialized yet, then it is queried before setting the uniform value.


PrevUpHomeNext