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.
clear_bufferfv(buffer, drawbuffer, value)Clear a specific draw buffer with float values.
clear_bufferiv(buffer, drawbuffer, value)Clear a specific draw buffer with int values.
clear_bufferuiv(buffer, drawbuffer, value)Clear a specific draw buffer with uint values.
clear_bufferfi(drawbuffer, depth, stencil)Clear depth + stencil simultaneously.
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_scissor_test_enabled(bool)Enable/disable the scissor test.
set_scissor(x,y,w,h)Set the scissor rectangle.
set_blend_enabled(bool)Enable/disable blending.
set_blend_func(src,dst)Set blend source/destination factors.
set_blend_func_separate(srcRGB,dstRGB,srcA,dstA)Separate RGB/alpha blend factors.
set_blend_equation(mode)Set blend equation.
set_blend_equation_separate(rgb, alpha)Separate RGB/alpha blend equations.
set_blend_color(r,g,b,a)Set the blend constant color.
get_blend_func(src, dst)Query current blend factors.
set_depth_test_enabled(bool)Enable/disable depth testing.
set_depth_mask(bool)Enable/disable depth writes.
set_depth_func(func)Set depth comparison function.
set_depth_range(near, far)Set depth range mapping.
get_depth_func()Query current depth comparison function.
set_stencil_test_enabled(bool)Enable/disable stencil testing.
set_stencil_func(func, ref, mask)Set stencil comparison function.
set_stencil_func_separate(face, func, ref, mask)Per-face stencil function.
set_stencil_op(sfail, dpfail, dppass)Set stencil operations.
set_stencil_op_separate(face, sfail, dpfail, dppass)Per-face stencil operations.
set_stencil_mask(mask)Stencil write mask.
set_stencil_mask_separate(face, mask)Per-face stencil write mask.
set_cull_face_enabled(bool)Enable/disable back-face culling.
set_cull_face(face)Set which face to cull (Front, Back, FrontAndBack).
set_front_face(face)Set winding order for front face.
get_cull_face()Query current cull face setting.
set_polygon_offset_fill_enabled(bool)Enable/disable polygon offset fill.
set_polygon_offset(factor, units)Set polygon offset parameters.
set_line_width(width)Set line rasterisation width.
set_color_mask(r,g,b,a)Set color channel write mask.
get_color_mask(r,g,b,a)Query current color write mask.
set_sample_coverage(value, invert)Set sample coverage value.
set_sample_alpha_to_coverage_enabled(bool)Enable alpha-to-coverage.
set_rasterizer_discard_enabled(bool)Discard rasterisation (useful for transform feedback).
set_pixel_store(pname, value)Set pixel pack/unpack alignment.
read_pixels(x,y,w,h,format,type,pixels)Read pixels from framebuffer into CPU memory.
set_read_buffer(src)Set source buffer for read_pixels.
set_draw_buffers(span)Set draw buffer targets.
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.
draw_arrays_indirect(prim, indirect)Indirect non-indexed draw.
draw_elements_indirect(prim, type, indirect)Indirect indexed draw.
draw_range_elements(prim, start, end, count, type, indices)Indexed draw with vertex range hint.
dispatch_compute(x,y,z)Launch a compute shader.
memory_barrier(mask)Insert a memory barrier.
memory_barrier_by_region(mask)Region-scoped memory barrier.
set_patch_vertices(count)Set patch vertex count for tessellation shaders.
set_debug_output_enabled(bool)Enable/disable debug output.
set_debug_output_synchronous_enabled(bool)Enable synchronous debug output.
push_debug_group(source, id, message)Push a named debug group.
pop_debug_group()Pop the current debug group.
object_label(identifier, name, label)Attach a debug label to a GL object.
set_hint(target, mode)Set an implementation hint.
is_enabled(cap)Query if a capability is enabled.
is_enabled(cap, index)Query an indexed capability.
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.

State getters

MethodDescription
get_boolean(pname)Query a bool GL state value.
get_float(pname)Query a float GL state value.
get_integer(pname)Query an int GL state value.
get_integer64(pname)Query a 64-bit int GL state value.
get_integeri(pname, index)Query an indexed int GL state value.
get_integer64i(pname, index)Query an indexed 64-bit int GL state value.
get_booleani(pname, index)Query an indexed bool GL state value.