Engine²
Open-source game engine written in C++.
Loading...
Searching...
No Matches
graphic Directory Reference

Directories

 
src
 
tests

Detailed Description

Graphic Plugin

Hardware-accelerated graphics backend for EngineSquared, built on WebGPU via wgpu-native. It provides GPU context management, shaders, textures, buffers, bind groups, and a render-graph pipeline integrated with the engine’s rendering stages.


Description

The Graphic plugin adds a full graphics stack to EngineSquared:

  • WebGPU / wgpu-native — Cross-platform GPU API (Vulkan, Metal, D3D12).
  • Context & device — Instance, adapter, device, and queue setup with configurable limits and features.
  • Resources — Shaders (WGSL), textures, samplers, GPU buffers, and bind groups.
  • Render graph — Declarative render passes with dependencies and topological execution.
  • Pipeline integration — Hooks into the engine’s RenderingPipeline (Setup, Preparation, CommandCreation, Presentation) and Shutdown.

It registers resources (e.g. Context, GraphicSettings, ShaderContainer, TextureContainer, GPUBufferContainer, SamplerContainer, BindGroupManager, RenderGraphContainer) and systems for initialization, frame preparation, command recording, presentation, and cleanup. Other plugins (e.g. Default Pipeline, RmlUi) can depend on it for rendering.


Dependencies

External (xmake packages)

Package Purpose
wgpu-native WebGPU implementation
glfw Windowing (optional)
glfw3webgpu GLFW–WebGPU surface
glm Math types
entt ECS / hashed names
spdlog Logging
fmt Formatting
stb Image loading
lodepng PNG support
tinyobjloader OBJ mesh loading

Engine

  • EngineSquaredCore — Core engine and scheduler
  • PluginObject — Entities and components (e.g. Mesh, Transform)
  • PluginRenderingPipeline — Rendering stages (Setup, Preparation, CommandCreation, Presentation)
  • PluginWindowWindow and surface (when using a window)
  • PluginEventEvent handling
  • UtilsLog — Logging utilities

The plugin requires RenderingPipeline::Plugin and Event::Plugin; use with Window::Plugin when rendering to a window.


Install

The Graphic plugin is built as part of EngineSquared with xmake. There is no separate install step.

  1. Clone and enter the repo
    git clone https://github.com/EngineSquared/EngineSquared.git
    cd EngineSquared
  2. Install xmake (if needed)
    See xmake install.
  3. Configure and build

    xmake config
    xmake build

    The static target PluginGraphic is built with the rest of the engine. Link it (or the main EngineSquared target that already depends on it) into your application.

  4. Run tests (optional)
    xmake build PluginGraphicTests
    xmake build RenderGraphTest # or any test binary from tests/

Configuration

Configure the plugin by editing the GraphicSettings resource before running the rendering pipeline (e.g. in a system registered for RenderingPipeline::Init or earlier). Access it via the core:

auto& settings = core.GetResource<Graphic::Resource::GraphicSettings>();
Definition GraphicSettings.hpp:18

Window system

Choose how the surface is created (e.g. for headless tests vs windowed apps):

// Use a window (default; requires PluginWindow)
settings.SetWindowSystem(Graphic::Resource::WindowSystem::GLFW);
// No window (headless; no surface from window)
settings.SetWindowSystem(Graphic::Resource::WindowSystem::None);
@ None
Definition GraphicSettings.hpp:9
@ GLFW
Definition GraphicSettings.hpp:10

Power preference

// or
settings.SetPowerPreference(Graphic::Resource::PowerPreference::LowPower);
@ LowPower
Definition GraphicSettings.hpp:14
@ HighPerformance
Definition GraphicSettings.hpp:15

Device limits and features

// Optional: adjust limits (see resource/Limits.hpp)
settings.GetWantedLimits() = Limits(wgpu::Default); // or custom limits
// Require specific WebGPU features
settings.AddRequiredFeature(wgpu::FeatureName::TextureCompressionBC);
constexpr DefaultFlag Default
Definition webgpu.hpp:78

Uncaptured device errors

Set a custom callback for WebGPU uncaptured errors (e.g. to log and/or throw in debug):

settings.SetOnErrorCallback(
[](WGPUDevice const*, WGPUErrorType type, WGPUStringView message,
void*, void*) {
Log::Error(fmt::format("WebGPU error: type {:x} ({})",
static_cast<uint32_t>(type),
std::string(message.data, message.length)));
// optionally: throw or abort
});
void Error(const T &msg) noexcept
Definition Logger.hpp:51

Usage

Adding the plugin

Ensure required plugins are added; then add the Graphic plugin:

#include "Engine.hpp"
#include "Graphic.hpp"
int main() {
// Configure GraphicSettings in Init if needed (see Configuration)
core.RunCore();
return 0;
}
Definition PluginDefaultPipeline.hpp:6
The core is the place where all the data of the engine is stored. It contains the registry (entities)...
Definition Core.hpp:33
void AddPlugins()
Adds multiple plugins that will be call instantly through the Bind method to agregate their systems a...
Definition Core.inl:88
void RunCore()
Execute the core loop, which will run all the schedulers.
Definition Core.cpp:74
Definition PluginGraphic.hpp:6
Definition PluginWindow.hpp:6
int main(void)
Definition main.cpp:80

Other plugins that depend on graphics (e.g. Default Pipeline, RmlUi) will typically RequirePlugins<..., Graphic::Plugin>(); your app only needs to add the plugin (and Window, if you render to a window).

Configuring settings at startup

settings.SetOnErrorCallback([](WGPUDevice const*, WGPUErrorType type,
WGPUStringView message, void*, void*) {
Log::Error(fmt::format("Device error: {}", std::string(message.data, message.length)));
});
});
TResource & GetResource()
Get a reference of a resource.
Definition Core.inl:14
decltype(auto) RegisterSystem(Systems... systems)
Add one or multiple systems associated with a specific scheduler.
Definition Core.inl:61
GraphicSettings & SetOnErrorCallback(WGPUUncapturedErrorCallback callback)
Definition GraphicSettings.hpp:64
GraphicSettings & SetWindowSystem(WindowSystem system)
Definition GraphicSettings.hpp:27
GraphicSettings & SetPowerPreference(PowerPreference preference)
Definition GraphicSettings.hpp:34
This class is used to initialize libraries like GLFW, etc.
Definition Init.hpp:9

Headless (no window)

Useful for tests or offscreen rendering:

});
core.RunSystems();
void RunSystems()
Run all the systems once by calling each scheduler.
Definition Core.cpp:83

Accessing graphics resources

After the pipeline has run setup, you can use the registered resources:

auto& context = core.GetResource<Graphic::Resource::Context>();
Definition Context.hpp:8
Object::Resource::ResourceManager< Sampler > SamplerContainer
SamplerContainer is a resource that stores Sampler resources.
Definition SamplerContainer.hpp:17
Object::Resource::ResourceManager< std::unique_ptr< AGPUBuffer > > GPUBufferContainer
GPUBufferContainer is a resource that stores GPUBuffer resources.
Definition GPUBufferContainer.hpp:17
Object::Resource::ResourceManager< Shader > ShaderContainer
ShaderContainer is a resource that stores Shader resources.
Definition ShaderContainer.hpp:17
Object::Resource::ResourceManager< RenderGraph > RenderGraphContainer
RenderGraphContainer is a resource that stores RenderGraph resources.
Definition RenderGraphContainer.hpp:17
Object::Resource::ResourceManager< BindGroup > BindGroupManager
BindGroupManager is a resource that stores BindGroup resources.
Definition BindGroupManager.hpp:17
Object::Resource::ResourceManager< Texture > TextureContainer
TextureContainer is a resource that stores Texture resources.
Definition TextureContainer.hpp:17

Shader from WGSL source

desc.setShader(R"(
@vertex fn vs_main() -> @builtin(position) vec4f {
return vec4f(0.0, 0.0, 0.0, 1.0);
}
@fragment fn fs_main() -> @location(0) vec4f {
return vec4f(1.0, 0.0, 0.0, 1.0);
}
)")
.setVertexEntryPoint("vs_main")
.addColorTargetState(Graphic::Resource::ColorTargetState(wgpu::TextureFormat::BGRA8Unorm));
auto& context = core.GetResource<Graphic::Resource::Context>();
container.Add("my_shader", std::move(shader));
Definition ShaderDescriptor.hpp:21
ShaderDescriptor & setVertexEntryPoint(std::string_view entryPoint)
Definition ShaderDescriptor.hpp:50
ShaderDescriptor & setFragmentEntryPoint(std::string_view entryPoint)
Definition ShaderDescriptor.hpp:56
ShaderDescriptor & setShader(std::string_view source)
Definition ShaderDescriptor.hpp:26
Definition Shader.hpp:14
static Shader Create(const ShaderDescriptor &descriptor, Context &context)
Definition Shader.hpp:45

Render graph with a render pass

auto& graph = graphContainer.Get();
graph.Add<MyRenderPass>("main_pass", MyRenderPass(core, ...));
graph.SetDependency("other_pass", "main_pass");
// Execution order is resolved automatically; then each frame:
// graph.Execute(core) is driven by the pipeline's CommandCreation stage
entt::resource< ResourceType > Add(const entt::hashed_string &id, Args &&...args)
Adds a resource to the manager.
Definition ResourceManager.hpp:52

(Your render pass type must satisfy the plugin’s ARenderPass interface and be registered/used within the engine’s render graph execution.)

Full example (minimal app with error callback)

See examples/graphic_usage/src/main.cpp for a complete sample: plugin setup, GraphicSettings (including SetOnErrorCallback), and a simple scene with mesh and camera.


License

Copyright (c) 2025 EngineSquared.
This plugin is part of EngineSquared and is distributed under the MIT License.
See the LICENSE file in the repository root for the full text.