|
Estella 0.113.0
A fast 2D and 3D game engine powered by WebAssembly and ECS
|
Immediate mode 2D drawing API. More...
#include <ImmediateDraw.hpp>
Public Member Functions | |
| ImmediateDraw (GfxDevice &device, RenderContext &context, resource::ResourceManager &resource_manager) | |
| Constructs an immediate draw instance. | |
| ~ImmediateDraw () | |
| ImmediateDraw (const ImmediateDraw &)=delete | |
| ImmediateDraw & | operator= (const ImmediateDraw &)=delete |
| void | init () |
| void | shutdown () |
| void | begin (const glm::mat4 &viewProjection, i32 viewportWidth=0, i32 viewportHeight=0) |
| Begins a new draw frame. | |
| void | end () |
| Ends the frame and submits all draw commands. | |
| void | deferEnd () |
| void | flushDeferred () |
| void | flushDeferred (ShaderHandle program) |
| bool | hasDeferred () const |
| const glm::mat4 & | viewProjection () const |
| void | flush () |
| Flushes pending draw commands without ending the frame. | |
| void | line (const glm::vec2 &from, const glm::vec2 &to, const glm::vec4 &color, f32 thickness=1.0f) |
| void | line3D (const glm::vec3 &from, const glm::vec3 &to, const glm::vec4 &color, f32 thickness=1.0f) |
| A line between two points in SPACE, widened across the view. | |
| void | line3DScreen (const glm::vec3 &from, const glm::vec3 &to, const glm::vec4 &color, f32 thickness=1.0f) |
| void | line3DScreenOffset (const glm::vec3 &anchor, const glm::vec2 &fromOffset, const glm::vec2 &toOffset, const glm::vec4 &color, f32 thickness=1.0f) |
| A framebuffer-sized line attached to one world-space anchor. | |
| void | polyline (std::span< const glm::vec2 > vertices, const glm::vec4 &color, f32 thickness=1.0f, bool closed=false) |
| void | rect (const glm::vec2 &position, const glm::vec2 &size, const glm::vec4 &color, bool filled=true) |
| void | rectOutline (const glm::vec2 &position, const glm::vec2 &size, const glm::vec4 &color, f32 thickness=1.0f) |
| void | circle (const glm::vec2 ¢er, f32 radius, const glm::vec4 &color, bool filled=true, i32 segments=32) |
| void | circleOutline (const glm::vec2 ¢er, f32 radius, const glm::vec4 &color, f32 thickness=1.0f, i32 segments=32) |
| void | polygon (std::span< const glm::vec2 > vertices, const glm::vec4 &color) |
| void | triangles (const f32 *positions, u32 vertexCount, const f32 *colors, const u32 *indices, u32 indexCount, const glm::vec4 &tint, const f32 xform[6]) |
| Triangles with a colour at every vertex, in one call: a shape tessellated once and drawn as often as asked. | |
| u32 | createShape (const f32 *positions, u32 vertexCount, const f32 *colors, const u32 *indices, u32 indexCount) |
| Puts a shape on the GPU once, for drawMesh to draw any number of times without sending its vertices again. | |
| void | destroyMesh (u32 id) |
| void | drawMesh (u32 id, const glm::vec4 &tint, const f32 xform[6]) |
| void | drawMeshMany (u32 id, const void *records, u32 count) |
count copies of a mesh in one call: drawMesh for each, without a call a copy. | |
| u32 | meshCount () const |
| void | texture (const glm::vec2 &position, const glm::vec2 &size, u32 textureId, const glm::vec4 &tint=glm::vec4(1.0f)) |
| void | textureRotated (const glm::vec2 &position, const glm::vec2 &size, f32 rotation, u32 textureId, const glm::vec4 &tint=glm::vec4(1.0f)) |
| void | setLayer (i32 layer) |
| i32 | getLayer () const |
| void | setDepth (f32 depth) |
| f32 | getDepth () const |
| void | setBlendMode (BlendMode mode) |
| void | setDepthTest (bool enabled) |
| void | setDepthState (bool test, bool write) |
| BlendMode | blendMode () const |
| bool | depthTest () const |
| bool | depthWrite () const |
| void | setDither (bool on, bool phase=false) |
| Draw what follows dithered: one half of a checkerboard (phase 0 or 1), at full colour; the two halves drawn in turn cover every pixel once. Per vertex, so it batches with everything else; it applies to the immediate geometry (lines, triangles, quads), not to a retained mesh's runs. | |
| bool | dither () const |
| bool | ditherPhase () const |
| void | deferBegin () |
| u32 | getDrawCallCount () const |
| u32 | getPrimitiveCount () const |
Immediate mode 2D drawing API.
Provides a simple API for drawing 2D primitives. All commands submitted between begin() and end() are batched (per texture) and drawn in submission order. The buffer is cleared each frame.
Definition at line 60 of file ImmediateDraw.hpp.
| esengine::ImmediateDraw::ImmediateDraw | ( | GfxDevice & | device, |
| RenderContext & | context, | ||
| resource::ResourceManager & | resource_manager ) |
Constructs an immediate draw instance.
| device | Graphics device (GPU command sink) |
| context | Render context for shared resources (white texture) |
| resource_manager | Resource manager for shader access |
Definition at line 95 of file ImmediateDraw.cpp.
Referenced by ImmediateDraw(), and operator=().
| esengine::ImmediateDraw::~ImmediateDraw | ( | ) |
Definition at line 103 of file ImmediateDraw.cpp.
References shutdown().
|
delete |
References ImmediateDraw().
| void esengine::ImmediateDraw::begin | ( | const glm::mat4 & | viewProjection, |
| i32 | viewportWidth = 0, | ||
| i32 | viewportHeight = 0 ) |
Begins a new draw frame.
Definition at line 180 of file ImmediateDraw.cpp.
References esengine::Invalid, and viewProjection().
|
inline |
Definition at line 244 of file ImmediateDraw.hpp.
| void esengine::ImmediateDraw::circle | ( | const glm::vec2 & | center, |
| f32 | radius, | ||
| const glm::vec4 & | color, | ||
| bool | filled = true, | ||
| i32 | segments = 32 ) |
Definition at line 592 of file ImmediateDraw.cpp.
References circleOutline().
| u32 esengine::ImmediateDraw::createShape | ( | const f32 * | positions, |
| u32 | vertexCount, | ||
| const f32 * | colors, | ||
| const u32 * | indices, | ||
| u32 | indexCount ) |
Puts a shape on the GPU once, for drawMesh to draw any number of times without sending its vertices again.
The same arrays as triangles(). Returns the mesh's id, or 0 when there is nothing to keep or an index names a vertex the shape does not hold. Ids outlive frames; destroyMesh gives the GPU memory back.
Definition at line 662 of file ImmediateDraw.cpp.
References esengine::Index, esengine::Invalid, esengine::packColor(), esengine::GfxContent::retained(), and esengine::Vertex.
|
inline |
Collects all following primitive batches for the owning scene pass.
Definition at line 256 of file ImmediateDraw.hpp.
| void esengine::ImmediateDraw::deferEnd | ( | ) |
Close collection now and submit it when the owning scene pass executes.
Definition at line 303 of file ImmediateDraw.cpp.
References flush().
Referenced by end().
|
inline |
Definition at line 245 of file ImmediateDraw.hpp.
|
inline |
Definition at line 246 of file ImmediateDraw.hpp.
| void esengine::ImmediateDraw::destroyMesh | ( | u32 | id | ) |
Gives a mesh's GPU memory back. A draw already collected this frame still draws; the id is not drawn again.
Definition at line 700 of file ImmediateDraw.cpp.
|
inline |
Definition at line 253 of file ImmediateDraw.hpp.
|
inline |
Definition at line 254 of file ImmediateDraw.hpp.
The transform is applied on the GPU: a copy costs one 28-byte instance record, and consecutive copies of one mesh in one state are one instanced draw. Anything else drawn between two copies keeps its place.
Definition at line 707 of file ImmediateDraw.cpp.
References flush(), and esengine::packColor().
count copies of a mesh in one call: drawMesh for each, without a call a copy.
records holds one 28-byte record a copy, as the instance stream is laid out: six floats (the affine, in drawMesh's order) and the tint packed as packColor packs it. They are copied into the frame's stream whole.
Definition at line 720 of file ImmediateDraw.cpp.
References flush().
| void esengine::ImmediateDraw::end | ( | ) |
Ends the frame and submits all draw commands.
Definition at line 288 of file ImmediateDraw.cpp.
References deferEnd(), flush(), and flushDeferred().
Referenced by flush().
| void esengine::ImmediateDraw::flush | ( | ) |
Flushes pending draw commands without ending the frame.
Use before operations that change GL state to ensure accumulated primitives are rendered with the correct state.
Definition at line 262 of file ImmediateDraw.cpp.
References esengine::Batch, end(), and esengine::UnsignedInt.
Referenced by deferEnd(), drawMesh(), drawMeshMany(), end(), flushDeferred(), setBlendMode(), and setDepthState().
| void esengine::ImmediateDraw::flushDeferred | ( | ) |
Submit a collection closed by deferEnd inside the active render pass.
Definition at line 311 of file ImmediateDraw.cpp.
References flushDeferred(), and esengine::Invalid.
Referenced by esengine::RenderFrame::drawOverScreen(), end(), and flushDeferred().
| void esengine::ImmediateDraw::flushDeferred | ( | ShaderHandle | program | ) |
The same, with every batch drawn through program instead of the one it was collected with: a pass that writes the display itself draws a linear frame's colours through the variant that encodes them, as the scene's present did.
Definition at line 315 of file ImmediateDraw.cpp.
References esengine::Batch, flush(), esengine::Invalid, esengine::PipelineDesc::program, and esengine::UnsignedInt.
|
inline |
Definition at line 236 of file ImmediateDraw.hpp.
|
inline |
Definition at line 262 of file ImmediateDraw.hpp.
|
inline |
Definition at line 234 of file ImmediateDraw.hpp.
|
inline |
Definition at line 263 of file ImmediateDraw.hpp.
|
inline |
Whether a collection closed by deferEnd is waiting for its pass.
Definition at line 107 of file ImmediateDraw.hpp.
Referenced by esengine::RenderFrame::drawOverScreen().
| void esengine::ImmediateDraw::init | ( | ) |
Definition at line 109 of file ImmediateDraw.cpp.
References esengine::resource::ShaderParser::assembleStage(), esengine::ShaderEmbeds::BATCH, esengine::Batch, ES_LOG_ERROR, esengine::resource::Fragment, esengine::Shader::handle(), esengine::Invalid, esengine::Shader::isValid(), esengine::Normal, esengine::Off, esengine::resource::ShaderParser::parse(), esengine::PipelineDesc::program, and esengine::resource::Vertex.
| void esengine::ImmediateDraw::line | ( | const glm::vec2 & | from, |
| const glm::vec2 & | to, | ||
| const glm::vec4 & | color, | ||
| f32 | thickness = 1.0f ) |
Definition at line 411 of file ImmediateDraw.cpp.
Referenced by circleOutline(), polyline(), and rectOutline().
| void esengine::ImmediateDraw::line3D | ( | const glm::vec3 & | from, |
| const glm::vec3 & | to, | ||
| const glm::vec4 & | color, | ||
| f32 | thickness = 1.0f ) |
A line between two points in SPACE, widened across the view.
Widened along cross(direction, view forward) rather than in a plane, so it reads as a line from wherever the camera is. Thickness is in WORLD units, so a distant line thins out with the geometry beside it.
Definition at line 427 of file ImmediateDraw.cpp.
References esengine::Batch, and esengine::packColor().
| void esengine::ImmediateDraw::line3DScreen | ( | const glm::vec3 & | from, |
| const glm::vec3 & | to, | ||
| const glm::vec4 & | color, | ||
| f32 | thickness = 1.0f ) |
A world-space line with an explicit framebuffer-pixel thickness.
Definition at line 467 of file ImmediateDraw.cpp.
References esengine::Batch, and esengine::packColor().
Referenced by line3DScreenOffset().
| void esengine::ImmediateDraw::line3DScreenOffset | ( | const glm::vec3 & | anchor, |
| const glm::vec2 & | fromOffset, | ||
| const glm::vec2 & | toOffset, | ||
| const glm::vec4 & | color, | ||
| f32 | thickness = 1.0f ) |
A framebuffer-sized line attached to one world-space anchor.
Offsets use framebuffer pixels (+X right, +Y up). The anchor is projected through the active draw frame, so editor handles keep a stable size without sampling a second camera.
Definition at line 528 of file ImmediateDraw.cpp.
References line3DScreen().
|
inline |
Definition at line 216 of file ImmediateDraw.hpp.
|
delete |
References ImmediateDraw(), and viewProjection().
| void esengine::ImmediateDraw::polygon | ( | std::span< const glm::vec2 > | vertices, |
| const glm::vec4 & | color ) |
Definition at line 623 of file ImmediateDraw.cpp.
| void esengine::ImmediateDraw::polyline | ( | std::span< const glm::vec2 > | vertices, |
| const glm::vec4 & | color, | ||
| f32 | thickness = 1.0f, | ||
| bool | closed = false ) |
Definition at line 550 of file ImmediateDraw.cpp.
References line().
| void esengine::ImmediateDraw::rect | ( | const glm::vec2 & | position, |
| const glm::vec2 & | size, | ||
| const glm::vec4 & | color, | ||
| bool | filled = true ) |
Definition at line 562 of file ImmediateDraw.cpp.
References rectOutline().
| void esengine::ImmediateDraw::rectOutline | ( | const glm::vec2 & | position, |
| const glm::vec2 & | size, | ||
| const glm::vec4 & | color, | ||
| f32 | thickness = 1.0f ) |
| void esengine::ImmediateDraw::setBlendMode | ( | BlendMode | mode | ) |
Switches the blend mode for subsequent primitives (flushes pending geometry).
Definition at line 243 of file ImmediateDraw.cpp.
References flush().
|
inline |
Definition at line 235 of file ImmediateDraw.hpp.
| void esengine::ImmediateDraw::setDepthState | ( | bool | test, |
| bool | write ) |
Changes depth testing and writes together, flushing the previous batch.
Definition at line 254 of file ImmediateDraw.cpp.
References flush().
Referenced by setDepthTest().
| void esengine::ImmediateDraw::setDepthTest | ( | bool | enabled | ) |
Toggles depth testing for subsequent primitives (flushes pending geometry).
Definition at line 250 of file ImmediateDraw.cpp.
References setDepthState().
|
inline |
Draw what follows dithered: one half of a checkerboard (phase 0 or 1), at full colour; the two halves drawn in turn cover every pixel once. Per vertex, so it batches with everything else; it applies to the immediate geometry (lines, triangles, quads), not to a retained mesh's runs.
Definition at line 252 of file ImmediateDraw.hpp.
|
inline |
Definition at line 233 of file ImmediateDraw.hpp.
| void esengine::ImmediateDraw::shutdown | ( | ) |
Definition at line 157 of file ImmediateDraw.cpp.
References ES_LOG_INFO, and esengine::Invalid.
Referenced by ~ImmediateDraw().
| void esengine::ImmediateDraw::texture | ( | const glm::vec2 & | position, |
| const glm::vec2 & | size, | ||
| u32 | textureId, | ||
| const glm::vec4 & | tint = glm::vec4(1.0f) ) |
Definition at line 841 of file ImmediateDraw.cpp.
| void esengine::ImmediateDraw::textureRotated | ( | const glm::vec2 & | position, |
| const glm::vec2 & | size, | ||
| f32 | rotation, | ||
| u32 | textureId, | ||
| const glm::vec4 & | tint = glm::vec4(1.0f) ) |
Definition at line 848 of file ImmediateDraw.cpp.
| void esengine::ImmediateDraw::triangles | ( | const f32 * | positions, |
| u32 | vertexCount, | ||
| const f32 * | colors, | ||
| const u32 * | indices, | ||
| u32 | indexCount, | ||
| const glm::vec4 & | tint, | ||
| const f32 | xform[6] ) |
Triangles with a colour at every vertex, in one call: a shape tessellated once and drawn as often as asked.
positions holds an x, y pair a vertex, colors an r, g, b, a quadruple a vertex (null: white), indices three a triangle. Every colour is multiplied by tint, and every point is moved by the 2D affine xform (x' = xform[0] x + xform[2] y + xform[4], y' = xform[1] x + xform[3] y + xform[5]), so one shape is drawn where and as faded as this frame says without being built again. A call naming a vertex it does not hold draws nothing.
Definition at line 632 of file ImmediateDraw.cpp.
References esengine::Batch, and esengine::packColor().
|
inline |
The view-projection the current or last collection began with.
Definition at line 110 of file ImmediateDraw.hpp.
Referenced by begin(), esengine::RenderFrame::drawOverScreen(), and operator=().