easy-gl › Device

Device

easygl::Device is the central object for initialisation, render-state management, and draw calls. You typically create one per OpenGL context and use it throughout your application.

Construction & initialization

// Default config
easygl::Device device;

// Custom config
easygl::Config cfg;
cfg.enable_debug_logging = true;
easygl::Device device(cfg);

// Must be called after the GL context is current
device.initialize(reinterpret_cast<easygl::GLGetProcAddressFn>(SDL_GL_GetProcAddress));

Capabilities & features

const auto& caps = device.capabilities();
const auto& info = caps.context_info();
std::cout << info.vendor << ' ' << info.version_string << '\n';

if (device.supports(easygl::Feature::ComputeShader))
    std::cout << "Compute shaders available\n";

device.require(easygl::Feature::FramebufferObject); // throws if missing

ClearFlags

ClearFlags is a bitmask enum defined in Device.hpp:

ValueClears
ClearFlags::NoneNothing
ClearFlags::ColorColor buffer
ClearFlags::DepthDepth buffer
ClearFlags::StencilStencil buffer
device.set_clear_color(0.0f, 0.0f, 0.0f, 1.0f);
device.set_clear_depth(1.0f);
device.set_clear_stencil(0);
device.clear(easygl::ClearFlags::Color | easygl::ClearFlags::Depth);

Viewport & scissor

device.set_viewport(0, 0, width, height);

int x, y, w, h;
device.get_viewport(x, y, w, h);

device.set_scissor_test_enabled(true);
device.set_scissor(10, 10, 200, 200);

Blending

device.set_blend_enabled(true);
device.set_blend_func(
    easygl::BlendFactor::SrcAlpha,
    easygl::BlendFactor::OneMinusSrcAlpha);
device.set_blend_equation(easygl::BlendEquation::Add);

// Separate RGB / alpha factors:
device.set_blend_func_separate(
    easygl::BlendFactor::SrcAlpha,
    easygl::BlendFactor::OneMinusSrcAlpha,
    easygl::BlendFactor::One,
    easygl::BlendFactor::Zero);

Depth test

device.set_depth_test_enabled(true);
device.set_depth_mask(true);
device.set_depth_func(easygl::CompareFunc::Less);
device.set_depth_range(0.0f, 1.0f);

Stencil test

device.set_stencil_test_enabled(true);
device.set_stencil_func(easygl::CompareFunc::Always, 1, 0xFF);
device.set_stencil_op(
    easygl::StencilOp::Keep,
    easygl::StencilOp::Keep,
    easygl::StencilOp::Replace);
device.set_stencil_mask(0xFF);

Cull face

device.set_cull_face_enabled(true);
device.set_cull_face(easygl::CullFace::Back);
device.set_front_face(easygl::FrontFace::CounterClockwise);

Draw calls

// Non-indexed
device.draw_arrays(easygl::PrimitiveType::Triangles, 0, vertexCount);

// Indexed
device.draw_elements(easygl::PrimitiveType::Triangles,
                     indexCount, easygl::DataType::UnsignedInt, nullptr);

// Instanced
device.draw_arrays_instanced(easygl::PrimitiveType::Triangles, 0, vertexCount, instanceCount);
device.draw_elements_instanced(easygl::PrimitiveType::Triangles,
                               indexCount, easygl::DataType::UnsignedInt,
                               nullptr, instanceCount);

Compute dispatch

device.dispatch_compute(64, 64, 1);
device.memory_barrier(easygl::MemoryBarrierMask::ShaderStorage);

Debug

device.set_debug_output_enabled(true);
device.set_debug_output_synchronous_enabled(true);
device.push_debug_group(easygl::DebugSource::Application, 1, "Shadow pass");
// ... render shadow pass ...
device.pop_debug_group();
device.object_label(easygl::DebugObjectLabel::Buffer, vbo.native_handle(), "Vertex buffer");

Misc

device.flush();
device.finish();
easygl::ErrorCode err = device.get_error();
easygl::GraphicsResetStatus rs = device.get_graphics_reset_status();

Full method reference

MethodDescription
initialize(loader)Load all GL function pointers via the provided loader.
is_initialized()Returns true after successful initialize().
config()Returns the Config used at construction.
capabilities()Returns Capabilities containing ContextInfo and feature flags.
supports(Feature)Returns true if the feature is available.
require(Feature)Throws Exception if the feature is not available.
clear(ClearFlags)Clear color/depth/stencil buffers by bitmask.
set_clear_color(r,g,b,a)Set the clear color value.
set_clear_depth(d)Set the clear depth value (0–1).
set_clear_stencil(v)Set the clear stencil value.
set_viewport(x,y,w,h)Set the rendering viewport.
get_viewport(x,y,w,h)Query the current viewport.
set_blend_enabled(bool)Enable/disable blending.
set_blend_func(src,dst)Set blend source/destination factors.
set_depth_test_enabled(bool)Enable/disable depth testing.
set_depth_func(func)Set depth comparison function.
set_cull_face_enabled(bool)Enable/disable back-face culling.
draw_arrays(prim,first,count)Non-indexed draw call.
draw_elements(prim,count,type,offset)Indexed draw call.
draw_arrays_instanced(...)Instanced non-indexed draw.
draw_elements_instanced(...)Instanced indexed draw.
dispatch_compute(x,y,z)Launch a compute shader.
memory_barrier(mask)Insert a memory barrier.
get_error()Poll for a pending GL error.
get_graphics_reset_status()Check for GPU context reset.
flush() / finish()Flush or synchronously finish all pending commands.