easy-gl › Program

Program

Manages a linked OpenGL shader program. Handles shader compilation, linking, uniform setting and attribute binding. Non-copyable, movable, RAII.

One-line construction (recommended)

Compile and link a vertex + fragment program in a single constructor call:

easygl::Program prog(vertexShaderSource, fragmentShaderSource);
// Throws easygl::Exception if compilation or link fails
prog.use();

Step-by-step construction

easygl::Program prog;
prog.create();

easygl::Shader vert(easygl::ShaderType::Vertex);
vert.create();
vert.compile_from_source(vertSrc);

easygl::Shader frag(easygl::ShaderType::Fragment);
frag.create();
frag.compile_from_source(fragSrc);

prog.attach_owned(vert);   // ownership transferred
prog.attach_owned(frag);
prog.link();               // throws on failure
// vert and frag are now empty; prog owns them internally

compile_from_sources

Resets and recompiles/relinks an existing program object:

prog.compile_from_sources(newVertSrc, newFragSrc);

Uniforms — scalar and vector

int loc = prog.uniform_location("uColor");
if (loc < 0) { /* uniform not found or optimised away */ }

prog.set_uniform(loc, 1.0f);                        // float
prog.set_uniform(loc, 1.0f, 0.5f);                   // vec2
prog.set_uniform(loc, 1.0f, 0.5f, 0.2f);              // vec3
prog.set_uniform(loc, 1.0f, 0.5f, 0.2f, 1.0f);        // vec4

prog.set_uniform(loc, 42);                             // int
prog.set_uniform(loc, 42u);                            // uint

Uniforms — arrays

std::vector<float> pts = { 0.0f, 1.0f, 2.0f, 3.0f };
prog.set_uniform_fv(loc, pts, 2);  // 2 components per element → 2 × vec2

Uniforms — matrices

float model[16] = { /* column-major 4×4 */ };
float mvp[16]   = { /* ... */ };

prog.set_uniform_matrix4(prog.uniform_location("uModel"), model);
prog.set_uniform_matrix4(prog.uniform_location("uMVP"), mvp);

// Non-square matrices:
prog.set_uniform_matrix3x4(prog.uniform_location("uBones"), boneData);

// Transpose flag (false = column-major, the OpenGL default):
prog.set_uniform_matrix4(loc, data, false);

Uniform blocks (UBOs)

auto blockIdx = prog.uniform_block_index("Matrices"); // returns std::optional<unsigned int>
if (blockIdx.has_value()) {
    prog.set_uniform_block_binding(*blockIdx, 0); // bind block to point 0
}

// C++17 if-init shorthand:
// if (auto idx = prog.uniform_block_index("Matrices")) {
//     prog.set_uniform_block_binding(*idx, 0);
// }

// Then bind the Buffer to the same point:
ubo.bind_base(easygl::BufferTarget::Uniform, 0);

Attribute binding

// Must be done BEFORE linking:
prog.bind_attrib_location(0, "aPos");
prog.bind_attrib_location(1, "aTexCoord");
prog.link();

// Or query after linking:
int posIdx = prog.attrib_location("aPos");

Validation

prog.validate(); // Calls glValidateProgram — useful in debug builds
std::cout << prog.info_log() << '\n';

Method reference

MethodDescription
Program()Default construction — empty, no GL handle yet.
Program(vertSrc, fragSrc)Compile & link in one step. Throws on failure.
from_sources(v, f) [static]Static factory: create from vertex + fragment sources.
from_sources(v, g, f) [static]Static factory: create from vertex + geometry + fragment sources.
create()Allocate GL program handle.
destroy()Free handle. Noexcept.
attach(shader)Attach shader (const ref, no ownership transfer).
attach_owned(shader)Attach shader and transfer ownership to program.
detach(shader)Detach shader from program.
link()Link attached shaders. Throws on failure.
compile_from_sources(v,f)Recompile/relink entire program from sources.
use()Make this program current (glUseProgram).
validate()Validate program state for current GL state.
info_log()Link or validation log string.
attrib_location(name)Get attribute location by name.
bind_attrib_location(idx, name)Bind attribute to index before linking.
uniform_location(name)Get uniform location by name.
set_uniform(loc, ...)Overloaded: float/int/uint scalars and vectors.
set_uniform_fv(loc, span, components)Float array uniform.
set_uniform_iv(loc, span, components)Integer array uniform.
set_uniform_uiv(loc, span, components)Unsigned int array uniform.
set_uniform_matrix2..4(loc, data, transpose)Square matrix uniforms.
set_uniform_matrix2x3..4x3(loc, data, transpose)Non-square matrix uniforms.
uniform_block_index(name)Returns std::optional<unsigned int>. Nullopt if block not found.
set_uniform_block_binding(idx, point)Bind UBO block to binding point.
is_linked()True after successful link.
is_created()True if GL handle allocated.
native_handle()Raw GL integer ID.
reset_handle_no_gl()Context-loss recovery.

Separable program uniforms (ProgramPipeline)

These methods set uniforms directly on the program object without requiring it to be bound via use(). Required when using programs with ProgramPipeline.

MethodDescription
set_program_uniform(loc, ...)Set float/int/uint scalar or vector uniform without binding.
set_program_uniform_fv(loc, span, components)Float array uniform for separable program.
set_program_uniform_iv(loc, span, components)Int array uniform for separable program.
set_program_uniform_uiv(loc, span, components)Unsigned int array uniform for separable program.
set_program_uniform_matrix2..4(loc, data, transpose)Square matrix uniforms for separable program.
set_program_uniform_matrix2x3..4x3(loc, data, transpose)Non-square matrix uniforms for separable program.