Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

refactor: replace raw GL enum constants with engine-level GfxEnums

esengineb51ea91b647d
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 029ddfe0c..c7b63175e 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -148,6 +148,7 @@ set(ESENGINE_HEADERS
src/esengine/renderer/ImmediateDraw.hpp
src/esengine/renderer/BlendMode.hpp
src/esengine/renderer/CustomGeometry.hpp
+ src/esengine/renderer/GfxEnums.hpp
src/esengine/renderer/GfxDevice.hpp
src/esengine/renderer/GLDevice.hpp
src/esengine/renderer/StateTracker.hpp
diff --git a/src/esengine/renderer/CustomGeometry.cpp b/src/esengine/renderer/CustomGeometry.cpp
index f73cf5065..d20242447 100644
--- a/src/esengine/renderer/CustomGeometry.cpp
+++ b/src/esengine/renderer/CustomGeometry.cpp
@@ -11,7 +11,6 @@
#include "CustomGeometry.hpp"
-#include "OpenGLHeaders.hpp"
#include "RenderCommand.hpp"
#include "../core/Log.hpp"
@@ -76,19 +75,20 @@ void CustomGeometry::bind() const {
const auto& layout = vbo_->getLayout();
u32 index = 0;
for (const auto& attr : layout) {
- GLenum glType = GL_FLOAT;
+ GfxDataType gfxType = GfxDataType::Float;
switch (attr.type) {
case ShaderDataType::Int:
case ShaderDataType::Int2:
case ShaderDataType::Int3:
case ShaderDataType::Int4:
- glType = GL_INT;
+ gfxType = GfxDataType::Int;
break;
case ShaderDataType::Bool:
- glType = GL_UNSIGNED_BYTE;
+ case ShaderDataType::UByte4N:
+ gfxType = GfxDataType::UnsignedByte;
break;
default:
- glType = GL_FLOAT;
+ gfxType = GfxDataType::Float;
break;
}
auto* device = RenderCommand::getDevice();
@@ -96,7 +96,7 @@ void CustomGeometry::bind() const {
device->vertexAttribPointer(
index,
static_cast<i32>(shaderDataTypeComponentCount(attr.type)),
- glType,
+ gfxType,
attr.normalized,
static_cast<i32>(layout.getStride()),
attr.offset
diff --git a/src/esengine/renderer/DrawList.cpp b/src/esengine/renderer/DrawList.cpp
index 0895c77cf..ed8051c7d 100644
--- a/src/esengine/renderer/DrawList.cpp
+++ b/src/esengine/renderer/DrawList.cpp
@@ -1,5 +1,4 @@
#include "DrawList.hpp"
-#include "OpenGLHeaders.hpp"
#include <glm/glm.hpp>
#include <algorithm>
@@ -104,7 +103,7 @@ void DrawList::execute(GfxDevice& device, StateTracker& state, TransientBufferPo
device.drawElements(
cmd.index_count,
- GL_UNSIGNED_SHORT,
+ GfxDataType::UnsignedShort,
static_cast<u32>(static_cast<uintptr_t>(cmd.index_offset) * sizeof(u16)));
}
diff --git a/src/esengine/renderer/GLDevice.cpp b/src/esengine/renderer/GLDevice.cpp
index 0a708e75e..022d364b2 100644
--- a/src/esengine/renderer/GLDevice.cpp
+++ b/src/esengine/renderer/GLDevice.cpp
@@ -46,6 +46,76 @@ void setCapability(GLenum cap, bool enabled) {
}
}
+GLenum toGLBufferTarget(GfxBufferTarget target) {
+ switch (target) {
+ case GfxBufferTarget::Vertex: return GL_ARRAY_BUFFER;
+ case GfxBufferTarget::Index: return GL_ELEMENT_ARRAY_BUFFER;
+ default: return GL_ARRAY_BUFFER;
+ }
+}
+
+GLenum toGLDataType(GfxDataType type) {
+ switch (type) {
+ case GfxDataType::Float: return GL_FLOAT;
+ case GfxDataType::Int: return GL_INT;
+ case GfxDataType::UnsignedByte: return GL_UNSIGNED_BYTE;
+ case GfxDataType::UnsignedShort: return GL_UNSIGNED_SHORT;
+ case GfxDataType::UnsignedInt: return GL_UNSIGNED_INT;
+ default: return GL_FLOAT;
+ }
+}
+
+GLenum toGLStencilFunc(GfxStencilFunc func) {
+ switch (func) {
+ case GfxStencilFunc::Never: return GL_NEVER;
+ case GfxStencilFunc::Less: return GL_LESS;
+ case GfxStencilFunc::Equal: return GL_EQUAL;
+ case GfxStencilFunc::LEqual: return GL_LEQUAL;
+ case GfxStencilFunc::Greater: return GL_GREATER;
+ case GfxStencilFunc::NotEqual: return GL_NOTEQUAL;
+ case GfxStencilFunc::GEqual: return GL_GEQUAL;
+ case GfxStencilFunc::Always: return GL_ALWAYS;
+ default: return GL_ALWAYS;
+ }
+}
+
+GLenum toGLStencilOp(GfxStencilOp op) {
+ switch (op) {
+ case GfxStencilOp::Keep: return GL_KEEP;
+ case GfxStencilOp::Zero: return GL_ZERO;
+ case GfxStencilOp::Replace: return GL_REPLACE;
+ case GfxStencilOp::Incr: return GL_INCR;
+ case GfxStencilOp::Decr: return GL_DECR;
+ case GfxStencilOp::Invert: return GL_INVERT;
+ case GfxStencilOp::IncrWrap: return GL_INCR_WRAP;
+ case GfxStencilOp::DecrWrap: return GL_DECR_WRAP;
+ default: return GL_KEEP;
+ }
+}
+
+struct GLPixelFormatInfo {
+ GLenum internalFormat;
+ GLenum format;
+ GLenum type;
+};
+
+GLPixelFormatInfo toGLPixelFormat(GfxPixelFormat fmt) {
+ switch (fmt) {
+ case GfxPixelFormat::RGB8: return { GL_RGB8, GL_RGB, GL_UNSIGNED_BYTE };
+ case GfxPixelFormat::RGBA8: return { GL_RGBA8, GL_RGBA, GL_UNSIGNED_BYTE };
+ case GfxPixelFormat::DepthComponent24: return { GL_DEPTH_COMPONENT24, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT };
+ default: return { GL_RGBA8, GL_RGBA, GL_UNSIGNED_BYTE };
+ }
+}
+
+GLenum toGLAttachment(GfxAttachment attachment) {
+ switch (attachment) {
+ case GfxAttachment::Color0: return GL_COLOR_ATTACHMENT0;
+ case GfxAttachment::Depth: return GL_DEPTH_ATTACHMENT;
+ default: return GL_COLOR_ATTACHMENT0;
+ }
+}
+
} // namespace
// =============================================================================
@@ -116,10 +186,6 @@ void GLDevice::setBlendMode(BlendMode mode) {
}
}
-void GLDevice::setBlendFunc(u32 srcFactor, u32 dstFactor, u32 srcAlphaFactor, u32 dstAlphaFactor) {
- glBlendFuncSeparate(srcFactor, dstFactor, srcAlphaFactor, dstAlphaFactor);
-}
-
// =============================================================================
// Depth State
// =============================================================================
@@ -140,12 +206,12 @@ void GLDevice::setStencilTest(bool enabled) {
setCapability(GL_STENCIL_TEST, enabled);
}
-void GLDevice::setStencilFunc(u32 func, i32 ref, u32 mask) {
- glStencilFunc(func, ref, mask);
+void GLDevice::setStencilFunc(GfxStencilFunc func, i32 ref, u32 mask) {
+ glStencilFunc(toGLStencilFunc(func), ref, mask);
}
-void GLDevice::setStencilOp(u32 sfail, u32 dpfail, u32 dppass) {
- glStencilOp(sfail, dpfail, dppass);
+void GLDevice::setStencilOp(GfxStencilOp sfail, GfxStencilOp dpfail, GfxStencilOp dppass) {
+ glStencilOp(toGLStencilOp(sfail), toGLStencilOp(dpfail), toGLStencilOp(dppass));
}
void GLDevice::setStencilMask(u32 mask) {
@@ -253,12 +319,12 @@ void GLDevice::bindIndexBuffer(u32 bufferId) {
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bufferId);
}
-void GLDevice::bufferData(u32 target, const void* data, u32 sizeBytes, bool dynamic) {
- glBufferData(target, sizeBytes, data, dynamic ? GL_DYNAMIC_DRAW : GL_STATIC_DRAW);
+void GLDevice::bufferData(GfxBufferTarget target, const void* data, u32 sizeBytes, bool dynamic) {
+ glBufferData(toGLBufferTarget(target), sizeBytes, data, dynamic ? GL_DYNAMIC_DRAW : GL_STATIC_DRAW);
}
-void GLDevice::bufferSubData(u32 target, u32 offset, const void* data, u32 sizeBytes) {
- glBufferSubData(target, offset, sizeBytes, data);
+void GLDevice::bufferSubData(GfxBufferTarget target, u32 offset, const void* data, u32 sizeBytes) {
+ glBufferSubData(toGLBufferTarget(target), offset, sizeBytes, data);
}
// =============================================================================
@@ -284,9 +350,9 @@ void GLDevice::enableVertexAttrib(u32 index) {
glEnableVertexAttribArray(index);
}
-void GLDevice::vertexAttribPointer(u32 index, i32 size, u32 type,
+void GLDevice::vertexAttribPointer(u32 index, i32 size, GfxDataType type,
bool normalized, i32 stride, u32 offset) {
- glVertexAttribPointer(index, size, type, normalized ? GL_TRUE : GL_FALSE,
+ glVertexAttribPointer(index, size, toGLDataType(type), normalized ? GL_TRUE : GL_FALSE,
stride, reinterpret_cast<const void*>(static_cast<uintptr_t>(offset)));
}
@@ -298,8 +364,8 @@ void GLDevice::vertexAttribDivisor(u32 index, u32 divisor) {
// Draw Calls
// =============================================================================
-void GLDevice::drawElements(u32 indexCount, u32 indexType, u32 byteOffset) {
- glDrawElements(GL_TRIANGLES, static_cast<GLsizei>(indexCount), indexType,
+void GLDevice::drawElements(u32 indexCount, GfxDataType indexType, u32 byteOffset) {
+ glDrawElements(GL_TRIANGLES, static_cast<GLsizei>(indexCount), toGLDataType(indexType),
reinterpret_cast<const void*>(static_cast<uintptr_t>(byteOffset)));
}
@@ -307,8 +373,8 @@ void GLDevice::drawArrays(u32 first, u32 vertexCount) {
glDrawArrays(GL_TRIANGLES, static_cast<GLint>(first), static_cast<GLsizei>(vertexCount));
}
-void GLDevice::drawElementsInstanced(u32 indexCount, u32 indexType, u32 byteOffset, u32 instanceCount) {
- glDrawElementsInstanced(GL_TRIANGLES, static_cast<GLsizei>(indexCount), indexType,
+void GLDevice::drawElementsInstanced(u32 indexCount, GfxDataType indexType, u32 byteOffset, u32 instanceCount) {
+ glDrawElementsInstanced(GL_TRIANGLES, static_cast<GLsizei>(indexCount), toGLDataType(indexType),
reinterpret_cast<const void*>(static_cast<uintptr_t>(byteOffset)),
static_cast<GLsizei>(instanceCount));
}
@@ -329,21 +395,22 @@ void GLDevice::deleteTexture(u32 textureId) {
}
void GLDevice::texImage2D(u32 textureId, u32 width, u32 height,
- u32 internalFormat, u32 format, u32 type,
- const void* data) {
+ GfxPixelFormat format, const void* data) {
+ auto gl = toGLPixelFormat(format);
glBindTexture(GL_TEXTURE_2D, textureId);
- glTexImage2D(GL_TEXTURE_2D, 0, static_cast<GLint>(internalFormat),
+ glTexImage2D(GL_TEXTURE_2D, 0, static_cast<GLint>(gl.internalFormat),
static_cast<GLsizei>(width), static_cast<GLsizei>(height),
- 0, format, type, data);
+ 0, gl.format, gl.type, data);
}
void GLDevice::texSubImage2D(u32 textureId, i32 xoffset, i32 yoffset,
u32 width, u32 height,
- u32 format, u32 type, const void* data) {
+ GfxPixelFormat format, const void* data) {
+ auto gl = toGLPixelFormat(format);
glBindTexture(GL_TEXTURE_2D, textureId);
glTexSubImage2D(GL_TEXTURE_2D, 0, xoffset, yoffset,
static_cast<GLsizei>(width), static_cast<GLsizei>(height),
- format, type, data);
+ gl.format, gl.type, data);
}
void GLDevice::setTextureParams(u32 textureId, TextureFilter min, TextureFilter mag,
@@ -383,9 +450,9 @@ void GLDevice::bindFramebuffer(u32 fboId) {
glBindFramebuffer(GL_FRAMEBUFFER, fboId);
}
-void GLDevice::framebufferTexture2D(u32 fboId, u32 attachment, u32 textureId) {
+void GLDevice::framebufferTexture2D(u32 fboId, GfxAttachment attachment, u32 textureId) {
glBindFramebuffer(GL_FRAMEBUFFER, fboId);
- glFramebufferTexture2D(GL_FRAMEBUFFER, attachment, GL_TEXTURE_2D, textureId, 0);
+ glFramebufferTexture2D(GL_FRAMEBUFFER, toGLAttachment(attachment), GL_TEXTURE_2D, textureId, 0);
}
bool GLDevice::checkFramebufferStatus() {
@@ -396,8 +463,9 @@ bool GLDevice::checkFramebufferStatus() {
// Readback
// =============================================================================
-void GLDevice::readPixels(i32 x, i32 y, u32 w, u32 h, u32 format, u32 type, void* data) {
- glReadPixels(x, y, static_cast<GLsizei>(w), static_cast<GLsizei>(h), format, type, data);
+void GLDevice::readPixels(i32 x, i32 y, u32 w, u32 h, GfxPixelFormat format, void* data) {
+ auto gl = toGLPixelFormat(format);
+ glReadPixels(x, y, static_cast<GLsizei>(w), static_cast<GLsizei>(h), gl.format, gl.type, data);
}
// =============================================================================
diff --git a/src/esengine/renderer/GLDevice.hpp b/src/esengine/renderer/GLDevice.hpp
index bd4aa660a..a86cdc969 100644
--- a/src/esengine/renderer/GLDevice.hpp
+++ b/src/esengine/renderer/GLDevice.hpp
@@ -31,70 +31,33 @@ public:
GLDevice() = default;
~GLDevice() override = default;
- // =========================================================================
- // Lifecycle
- // =========================================================================
-
void init() override;
void shutdown() override;
- // =========================================================================
- // Viewport & Clear
- // =========================================================================
-
void setViewport(i32 x, i32 y, u32 w, u32 h) override;
void setClearColor(f32 r, f32 g, f32 b, f32 a) override;
void clear(bool color, bool depth, bool stencil) override;
- // =========================================================================
- // Blend State
- // =========================================================================
-
void setBlendEnabled(bool enabled) override;
void setBlendMode(BlendMode mode) override;
- void setBlendFunc(u32 srcFactor, u32 dstFactor, u32 srcAlphaFactor, u32 dstAlphaFactor) override;
-
- // =========================================================================
- // Depth State
- // =========================================================================
void setDepthTest(bool enabled) override;
void setDepthWrite(bool enabled) override;
- // =========================================================================
- // Stencil State
- // =========================================================================
-
void setStencilTest(bool enabled) override;
- void setStencilFunc(u32 func, i32 ref, u32 mask) override;
- void setStencilOp(u32 sfail, u32 dpfail, u32 dppass) override;
+ void setStencilFunc(GfxStencilFunc func, i32 ref, u32 mask) override;
+ void setStencilOp(GfxStencilOp sfail, GfxStencilOp dpfail, GfxStencilOp dppass) override;
void setStencilMask(u32 mask) override;
void setColorMask(bool r, bool g, bool b, bool a) override;
- // =========================================================================
- // Scissor State
- // =========================================================================
-
void setScissorTest(bool enabled) override;
void setScissor(i32 x, i32 y, i32 w, i32 h) override;
- // =========================================================================
- // Culling
- // =========================================================================
-
void setCulling(bool enabled) override;
void setCullFace(bool front) override;
- // =========================================================================
- // Texture Binding
- // =========================================================================
-
void bindTexture(u32 slot, u32 textureId) override;
- // =========================================================================
- // Shader Program
- // =========================================================================
-
void useProgram(u32 programId) override;
i32 getUniformLocation(u32 programId, const char* name) override;
void setUniform1i(i32 location, i32 value) override;
@@ -105,73 +68,44 @@ public:
void setUniformMat3(i32 location, const f32* data) override;
void setUniformMat4(i32 location, const f32* data) override;
- // =========================================================================
- // Buffer Operations
- // =========================================================================
-
u32 createBuffer() override;
void deleteBuffer(u32 bufferId) override;
void bindVertexBuffer(u32 bufferId) override;
void bindIndexBuffer(u32 bufferId) override;
- void bufferData(u32 target, const void* data, u32 sizeBytes, bool dynamic) override;
- void bufferSubData(u32 target, u32 offset, const void* data, u32 sizeBytes) override;
-
- // =========================================================================
- // VAO Operations
- // =========================================================================
+ void bufferData(GfxBufferTarget target, const void* data, u32 sizeBytes, bool dynamic) override;
+ void bufferSubData(GfxBufferTarget target, u32 offset, const void* data, u32 sizeBytes) override;
u32 createVertexArray() override;
void deleteVertexArray(u32 vaoId) override;
void bindVertexArray(u32 vaoId) override;
void enableVertexAttrib(u32 index) override;
- void vertexAttribPointer(u32 index, i32 size, u32 type,
+ void vertexAttribPointer(u32 index, i32 size, GfxDataType type,
bool normalized, i32 stride, u32 offset) override;
void vertexAttribDivisor(u32 index, u32 divisor) override;
- // =========================================================================
- // Draw Calls
- // =========================================================================
-
- void drawElements(u32 indexCount, u32 indexType, u32 byteOffset) override;
+ void drawElements(u32 indexCount, GfxDataType indexType, u32 byteOffset) override;
void drawArrays(u32 first, u32 vertexCount) override;
- void drawElementsInstanced(u32 indexCount, u32 indexType, u32 byteOffset, u32 instanceCount) override;
-
- // =========================================================================
- // Texture Creation
- // =========================================================================
+ void drawElementsInstanced(u32 indexCount, GfxDataType indexType, u32 byteOffset, u32 instanceCount) override;
u32 createTexture() override;
void deleteTexture(u32 textureId) override;
void texImage2D(u32 textureId, u32 width, u32 height,
- u32 internalFormat, u32 format, u32 type,
- const void* data) override;
+ GfxPixelFormat format, const void* data) override;
void texSubImage2D(u32 textureId, i32 xoffset, i32 yoffset,
u32 width, u32 height,
- u32 format, u32 type, const void* data) override;
+ GfxPixelFormat format, const void* data) override;
void setTextureParams(u32 textureId, TextureFilter min, TextureFilter mag,
TextureWrap wrapS, TextureWrap wrapT) override;
void generateMipmaps(u32 textureId) override;
void pixelStorei(u32 pname, i32 param) override;
- // =========================================================================
- // Framebuffer
- // =========================================================================
-
u32 createFramebuffer() override;
void deleteFramebuffer(u32 fboId) override;
void bindFramebuffer(u32 fboId) override;
- void framebufferTexture2D(u32 fboId, u32 attachment, u32 textureId) override;
+ void framebufferTexture2D(u32 fboId, GfxAttachment attachment, u32 textureId) override;
bool checkFramebufferStatus() override;
- // =========================================================================
- // Readback
- // =========================================================================
-
- void readPixels(i32 x, i32 y, u32 w, u32 h, u32 format, u32 type, void* data) override;
-
- // =========================================================================
- // Debug
- // =========================================================================
+ void readPixels(i32 x, i32 y, u32 w, u32 h, GfxPixelFormat format, void* data) override;
void setWireframe(bool enabled) override;
u32 getError() override;
diff --git a/src/esengine/renderer/GfxDevice.hpp b/src/esengine/renderer/GfxDevice.hpp
index d3ba33e09..4ab7da183 100644
--- a/src/esengine/renderer/GfxDevice.hpp
+++ b/src/esengine/renderer/GfxDevice.hpp
@@ -19,6 +19,7 @@
#include "../core/Types.hpp"
#include "BlendMode.hpp"
+#include "GfxEnums.hpp"
#include "Texture.hpp"
namespace esengine {
@@ -70,9 +71,6 @@ public:
/** @brief Sets blend mode using predefined BlendMode */
virtual void setBlendMode(BlendMode mode) = 0;
- /** @brief Sets custom blend function (src, dst, srcAlpha, dstAlpha as GL enums) */
- virtual void setBlendFunc(u32 srcFactor, u32 dstFactor, u32 srcAlphaFactor, u32 dstAlphaFactor) = 0;
-
// =========================================================================
// Depth State
// =========================================================================
@@ -90,11 +88,11 @@ public:
/** @brief Enables or disables stencil testing */
virtual void setStencilTest(bool enabled) = 0;
- /** @brief Sets stencil function (func, ref, mask as GL enums) */
- virtual void setStencilFunc(u32 func, i32 ref, u32 mask) = 0;
+ /** @brief Sets stencil function */
+ virtual void setStencilFunc(GfxStencilFunc func, i32 ref, u32 mask) = 0;
- /** @brief Sets stencil operations (sfail, dpfail, dppass as GL enums) */
- virtual void setStencilOp(u32 sfail, u32 dpfail, u32 dppass) = 0;
+ /** @brief Sets stencil operations */
+ virtual void setStencilOp(GfxStencilOp sfail, GfxStencilOp dpfail, GfxStencilOp dppass) = 0;
/** @brief Sets stencil write mask */
virtual void setStencilMask(u32 mask) = 0;
@@ -170,17 +168,17 @@ public:
/** @brief Deletes a buffer */
virtual void deleteBuffer(u32 bufferId) = 0;
- /** @brief Binds a buffer to GL_ARRAY_BUFFER */
+ /** @brief Binds a buffer to the vertex (array) target */
virtual void bindVertexBuffer(u32 bufferId) = 0;
- /** @brief Binds a buffer to GL_ELEMENT_ARRAY_BUFFER */
+ /** @brief Binds a buffer to the index (element) target */
virtual void bindIndexBuffer(u32 bufferId) = 0;
- /** @brief Uploads data to the currently bound buffer target */
- virtual void bufferData(u32 target, const void* data, u32 sizeBytes, bool dynamic) = 0;
+ /** @brief Uploads data to a buffer target */
+ virtual void bufferData(GfxBufferTarget target, const void* data, u32 sizeBytes, bool dynamic) = 0;
- /** @brief Updates a sub-region of the currently bound buffer */
- virtual void bufferSubData(u32 target, u32 offset, const void* data, u32 sizeBytes) = 0;
+ /** @brief Updates a sub-region of a buffer target */
+ virtual void bufferSubData(GfxBufferTarget target, u32 offset, const void* data, u32 sizeBytes) = 0;
// =========================================================================
// VAO Operations
@@ -199,7 +197,7 @@ public:
virtual void enableVertexAttrib(u32 index) = 0;
/** @brief Configures a vertex attribute pointer */
- virtual void vertexAttribPointer(u32 index, i32 size, u32 type,
+ virtual void vertexAttribPointer(u32 index, i32 size, GfxDataType type,
bool normalized, i32 stride, u32 offset) = 0;
/** @brief Sets vertex attribute divisor for instanced rendering */
@@ -209,14 +207,14 @@ public:
// Draw Calls
// =========================================================================
- /** @brief Draws indexed primitives (GL_TRIANGLES) */
- virtual void drawElements(u32 indexCount, u32 indexType, u32 byteOffset) = 0;
+ /** @brief Draws indexed triangles */
+ virtual void drawElements(u32 indexCount, GfxDataType indexType, u32 byteOffset) = 0;
- /** @brief Draws non-indexed primitives (GL_TRIANGLES) */
+ /** @brief Draws non-indexed triangles */
virtual void drawArrays(u32 first, u32 vertexCount) = 0;
- /** @brief Draws indexed primitives with instancing */
- virtual void drawElementsInstanced(u32 indexCount, u32 indexType, u32 byteOffset, u32 instanceCount) = 0;
+ /** @brief Draws indexed triangles with instancing */
+ virtual void drawElementsInstanced(u32 indexCount, GfxDataType indexType, u32 byteOffset, u32 instanceCount) = 0;
// =========================================================================
// Texture Creation
@@ -230,13 +228,12 @@ public:
/** @brief Allocates texture storage with optional initial data */
virtual void texImage2D(u32 textureId, u32 width, u32 height,
- u32 internalFormat, u32 format, u32 type,
- const void* data) = 0;
+ GfxPixelFormat format, const void* data) = 0;
/** @brief Updates a sub-region of a texture */
virtual void texSubImage2D(u32 textureId, i32 xoffset, i32 yoffset,
u32 width, u32 height,
- u32 format, u32 type, const void* data) = 0;
+ GfxPixelFormat format, const void* data) = 0;
/** @brief Sets texture filtering and wrap parameters */
virtual void setTextureParams(u32 textureId, TextureFilter min, TextureFilter mag,
@@ -262,7 +259,7 @@ public:
virtual void bindFramebuffer(u32 fboId) = 0;
/** @brief Attaches a texture to a framebuffer */
- virtual void framebufferTexture2D(u32 fboId, u32 attachment, u32 textureId) = 0;
+ virtual void framebufferTexture2D(u32 fboId, GfxAttachment attachment, u32 textureId) = 0;
/** @brief Checks framebuffer completeness */
virtual bool checkFramebufferStatus() = 0;
@@ -272,7 +269,7 @@ public:
// =========================================================================
/** @brief Reads pixels from the current framebuffer */
- virtual void readPixels(i32 x, i32 y, u32 w, u32 h, u32 format, u32 type, void* data) = 0;
+ virtual void readPixels(i32 x, i32 y, u32 w, u32 h, GfxPixelFormat format, void* data) = 0;
// =========================================================================
// Debug
diff --git a/src/esengine/renderer/GfxEnums.hpp b/src/esengine/renderer/GfxEnums.hpp
new file mode 100644
index 000000000..be4dbb260
--- /dev/null
+++ b/src/esengine/renderer/GfxEnums.hpp
@@ -0,0 +1,89 @@
+/**
+ * @file GfxEnums.hpp
+ * @brief Backend-agnostic graphics enumerations
+ * @details Replaces raw GL enum constants in the GfxDevice interface so that
+ * upper-layer code has zero dependency on OpenGL headers.
+ *
+ * @author ESEngine Team
+ * @date 2026
+ *
+ * @copyright Copyright (c) 2026 ESEngine Team
+ * Licensed under the MIT License.
+ */
+#pragma once
+
+#include "../core/Types.hpp"
+
+namespace esengine {
+
+// =============================================================================
+// Buffer Target
+// =============================================================================
+
+enum class GfxBufferTarget : u8 {
+ Vertex,
+ Index,
+};
+
+// =============================================================================
+// Data Type (vertex attributes, index type, pixel data type)
+// =============================================================================
+
+enum class GfxDataType : u8 {
+ Float,
+ Int,
+ UnsignedByte,
+ UnsignedShort,
+ UnsignedInt,
+};
+
+// =============================================================================
+// Stencil Function
+// =============================================================================
+
+enum class GfxStencilFunc : u8 {
+ Never,
+ Less,
+ Equal,
+ LEqual,
+ Greater,
+ NotEqual,
+ GEqual,
+ Always,
+};
+
+// =============================================================================
+// Stencil Operation
+// =============================================================================
+
+enum class GfxStencilOp : u8 {
+ Keep,
+ Zero,
+ Replace,
+ Incr,
+ Decr,
+ Invert,
+ IncrWrap,
+ DecrWrap,
+};
+
+// =============================================================================
+// Pixel Format (texture internal format + transfer format)
+// =============================================================================
+
+enum class GfxPixelFormat : u8 {
+ RGB8,
+ RGBA8,
+ DepthComponent24,
+};
+
+// =============================================================================
+// Framebuffer Attachment
+// =============================================================================
+
+enum class GfxAttachment : u8 {
+ Color0,
+ Depth,
+};
+
+} // namespace esengine
diff --git a/src/esengine/renderer/PostProcessPipeline.cpp b/src/esengine/renderer/PostProcessPipeline.cpp
index e1f55c632..34dbce6e0 100644
--- a/src/esengine/renderer/PostProcessPipeline.cpp
+++ b/src/esengine/renderer/PostProcessPipeline.cpp
@@ -17,7 +17,7 @@
#include "../core/Log.hpp"
#include <algorithm>
-#include "OpenGLHeaders.hpp"
+#include "GfxEnums.hpp"
namespace esengine {
@@ -258,12 +258,12 @@ void PostProcessPipeline::ensureScreenQuad() {
u32 vbo = device->createBuffer();
device->bindVertexBuffer(vbo);
- device->bufferData(GL_ARRAY_BUFFER, vertices, static_cast<u32>(sizeof(vertices)), false);
+ device->bufferData(GfxBufferTarget::Vertex, vertices, static_cast<u32>(sizeof(vertices)), false);
device->enableVertexAttrib(0);
- device->vertexAttribPointer(0, 2, GL_FLOAT, false, STRIDE, 0);
+ device->vertexAttribPointer(0, 2, GfxDataType::Float, false, STRIDE, 0);
device->enableVertexAttrib(1);
- device->vertexAttribPointer(1, 2, GL_FLOAT, false, STRIDE, 2 * sizeof(f32));
+ device->vertexAttribPointer(1, 2, GfxDataType::Float, false, STRIDE, 2 * sizeof(f32));
device->bindVertexArray(0);
diff --git a/src/esengine/renderer/RenderContext.cpp b/src/esengine/renderer/RenderContext.cpp
index 5fa8568ee..378cb033d 100644
--- a/src/esengine/renderer/RenderContext.cpp
+++ b/src/esengine/renderer/RenderContext.cpp
@@ -14,7 +14,6 @@
#include "ShaderEmbeds.generated.hpp"
#include "../resource/ShaderParser.hpp"
#include "../core/Log.hpp"
-#include "OpenGLHeaders.hpp"
namespace esengine {
@@ -112,7 +111,7 @@ void RenderContext::initWhiteTexture() {
whiteTextureId_ = device->createTexture();
u32 whiteData = 0xFFFFFFFF;
- device->texImage2D(whiteTextureId_, 1, 1, GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE, &whiteData);
+ device->texImage2D(whiteTextureId_, 1, 1, GfxPixelFormat::RGBA8, &whiteData);
device->setTextureParams(whiteTextureId_, TextureFilter::Nearest, TextureFilter::Nearest,
TextureWrap::ClampToEdge, TextureWrap::ClampToEdge);
diff --git a/src/esengine/renderer/RenderFrame.cpp b/src/esengine/renderer/RenderFrame.cpp
index ba7fe5773..4203fb85c 100644
--- a/src/esengine/renderer/RenderFrame.cpp
+++ b/src/esengine/renderer/RenderFrame.cpp
@@ -1,5 +1,4 @@
#include "RenderFrame.hpp"
-#include "OpenGLHeaders.hpp"
#include "Shader.hpp"
#include "ShaderEmbeds.generated.hpp"
#include "../resource/ShaderParser.hpp"
@@ -281,8 +280,7 @@ void RenderFrame::replayToDrawCall(i32 stopAtDrawCall) {
u32 pixelCount = width_ * height_ * 4;
snapshot_pixels_.resize(pixelCount);
- device_.readPixels(0, 0, width_, height_, GL_RGBA, GL_UNSIGNED_BYTE,
- snapshot_pixels_.data());
+ device_.readPixels(0, 0, width_, height_, GfxPixelFormat::RGBA8, snapshot_pixels_.data());
rt->unbind();
}
diff --git a/src/esengine/renderer/Renderer.cpp b/src/esengine/renderer/Renderer.cpp
index 44787d8cd..536abb046 100644
--- a/src/esengine/renderer/Renderer.cpp
+++ b/src/esengine/renderer/Renderer.cpp
@@ -19,15 +19,6 @@
#include "../core/Log.hpp"
#include "../resource/ResourceManager.hpp"
-#ifdef ES_PLATFORM_WEB
- #include <GLES3/gl3.h>
-#else
- #ifdef _WIN32
- #include <windows.h>
- #endif
- #include <glad/glad.h>
-#endif
-
#include <array>
#include <cmath>
#include <vector>
@@ -78,7 +69,7 @@ void RenderCommand::drawIndexed(const VertexArray& vao, u32 indexCount) {
auto ib = vao.getIndexBuffer();
u32 count = indexCount ? indexCount : (ib ? ib->getCount() : 0);
if (count > 0 && ib) {
- GLenum type = ib->is16Bit() ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT;
+ auto type = ib->is16Bit() ? GfxDataType::UnsignedShort : GfxDataType::UnsignedInt;
device_->drawElements(count, type, 0);
}
}
@@ -432,7 +423,7 @@ void BatchRenderer2D::flush() {
auto ib = data_->vao->getIndexBuffer();
if (ib) {
ib->bind();
- GLenum type = ib->is16Bit() ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT;
+ auto type = ib->is16Bit() ? GfxDataType::UnsignedShort : GfxDataType::UnsignedInt;
dev->drawElements(data_->indexCount, type, 0);
}
data_->drawCallCount++;
diff --git a/src/esengine/renderer/StateTracker.cpp b/src/esengine/renderer/StateTracker.cpp
index 77637c2ea..0516c5853 100644
--- a/src/esengine/renderer/StateTracker.cpp
+++ b/src/esengine/renderer/StateTracker.cpp
@@ -1,5 +1,4 @@
#include "StateTracker.hpp"
-#include "OpenGLHeaders.hpp"
namespace esengine {
@@ -84,8 +83,8 @@ void StateTracker::beginStencilWrite(i32 refValue) {
stencil_state_ = StencilState::Write;
stencil_ref_ = refValue;
device_.setStencilTest(true);
- device_.setStencilFunc(GL_ALWAYS, refValue, 0xFF);
- device_.setStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
+ device_.setStencilFunc(GfxStencilFunc::Always, refValue, 0xFF);
+ device_.setStencilOp(GfxStencilOp::Keep, GfxStencilOp::Keep, GfxStencilOp::Replace);
device_.setColorMask(false, false, false, false);
device_.setStencilMask(0xFF);
}
@@ -100,8 +99,8 @@ void StateTracker::beginStencilTest(i32 refValue) {
stencil_state_ = StencilState::Test;
stencil_ref_ = refValue;
device_.setStencilTest(true);
- device_.setStencilFunc(GL_EQUAL, refValue, 0xFF);
- device_.setStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
+ device_.setStencilFunc(GfxStencilFunc::Equal, refValue, 0xFF);
+ device_.setStencilOp(GfxStencilOp::Keep, GfxStencilOp::Keep, GfxStencilOp::Keep);
device_.setStencilMask(0x00);
}
diff --git a/src/esengine/renderer/TransientBufferPool.cpp b/src/esengine/renderer/TransientBufferPool.cpp
index 5e36d82d5..a70003ed6 100644
--- a/src/esengine/renderer/TransientBufferPool.cpp
+++ b/src/esengine/renderer/TransientBufferPool.cpp
@@ -1,5 +1,4 @@
#include "TransientBufferPool.hpp"
-#include "OpenGLHeaders.hpp"
#include "../core/Log.hpp"
#include <cstring>
@@ -23,11 +22,11 @@ void TransientBufferPool::init(u32 initialVertexBytes, u32 initialIndexCount) {
ebo_ = device_.createBuffer();
device_.bindVertexBuffer(vbo_);
- device_.bufferData(GL_ARRAY_BUFFER, nullptr, vbo_capacity_, true);
+ device_.bufferData(GfxBufferTarget::Vertex, nullptr, vbo_capacity_, true);
device_.bindVertexBuffer(0);
device_.bindIndexBuffer(ebo_);
- device_.bufferData(GL_ELEMENT_ARRAY_BUFFER, nullptr, ebo_capacity_ * sizeof(u16), true);
+ device_.bufferData(GfxBufferTarget::Index, nullptr, ebo_capacity_ * sizeof(u16), true);
device_.bindIndexBuffer(0);
for (auto layout : {LayoutId::Batch, LayoutId::Shape, LayoutId::MatSprite}) {
@@ -107,9 +106,9 @@ void TransientBufferPool::upload() {
device_.bindVertexBuffer(vbo_);
if (vertex_write_pos_ > vbo_capacity_) {
vbo_capacity_ = vertex_write_pos_;
- device_.bufferData(GL_ARRAY_BUFFER, vertex_staging_.data(), vbo_capacity_, true);
+ device_.bufferData(GfxBufferTarget::Vertex, vertex_staging_.data(), vbo_capacity_, true);
} else {
- device_.bufferSubData(GL_ARRAY_BUFFER, 0, vertex_staging_.data(), vertex_write_pos_);
+ device_.bufferSubData(GfxBufferTarget::Vertex, 0, vertex_staging_.data(), vertex_write_pos_);
}
device_.bindIndexBuffer(ebo_);
@@ -117,10 +116,10 @@ void TransientBufferPool::upload() {
u32 eboCapBytes = ebo_capacity_ * sizeof(u16);
if (eboBytes > eboCapBytes) {
ebo_capacity_ = index_write_pos_;
- device_.bufferData(GL_ELEMENT_ARRAY_BUFFER, index_staging_.data(),
+ device_.bufferData(GfxBufferTarget::Index, index_staging_.data(),
ebo_capacity_ * sizeof(u16), true);
} else {
- device_.bufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, index_staging_.data(), eboBytes);
+ device_.bufferSubData(GfxBufferTarget::Index, 0, index_staging_.data(), eboBytes);
}
}
@@ -143,33 +142,33 @@ void TransientBufferPool::setupLayoutVAO(LayoutId layout) {
case LayoutId::Batch: {
constexpr u32 STRIDE = 20;
device_.enableVertexAttrib(0);
- device_.vertexAttribPointer(0, 2, GL_FLOAT, false, STRIDE, 0);
+ device_.vertexAttribPointer(0, 2, GfxDataType::Float, false, STRIDE, 0);
device_.enableVertexAttrib(1);
- device_.vertexAttribPointer(1, 4, GL_UNSIGNED_BYTE, true, STRIDE, 8);
+ device_.vertexAttribPointer(1, 4, GfxDataType::UnsignedByte, true, STRIDE, 8);
device_.enableVertexAttrib(2);
- device_.vertexAttribPointer(2, 2, GL_FLOAT, false, STRIDE, 12);
+ device_.vertexAttribPointer(2, 2, GfxDataType::Float, false, STRIDE, 12);
break;
}
case LayoutId::Shape: {
constexpr u32 STRIDE = 48;
device_.enableVertexAttrib(0);
- device_.vertexAttribPointer(0, 2, GL_FLOAT, false, STRIDE, 0);
+ device_.vertexAttribPointer(0, 2, GfxDataType::Float, false, STRIDE, 0);
device_.enableVertexAttrib(1);
- device_.vertexAttribPointer(1, 2, GL_FLOAT, false, STRIDE, 8);
+ device_.vertexAttribPointer(1, 2, GfxDataType::Float, false, STRIDE, 8);
device_.enableVertexAttrib(2);
- device_.vertexAttribPointer(2, 4, GL_FLOAT, false, STRIDE, 16);
+ device_.vertexAttribPointer(2, 4, GfxDataType::Float, false, STRIDE, 16);
device_.enableVertexAttrib(3);
- device_.vertexAttribPointer(3, 4, GL_FLOAT, false, STRIDE, 32);
+ device_.vertexAttribPointer(3, 4, GfxDataType::Float, false, STRIDE, 32);
break;
}
case LayoutId::MatSprite: {
constexpr u32 STRIDE = 32;
device_.enableVertexAttrib(0);
- device_.vertexAttribPointer(0, 2, GL_FLOAT, false, STRIDE, 0);
+ device_.vertexAttribPointer(0, 2, GfxDataType::Float, false, STRIDE, 0);
device_.enableVertexAttrib(1);
- device_.vertexAttribPointer(1, 2, GL_FLOAT, false, STRIDE, 8);
+ device_.vertexAttribPointer(1, 2, GfxDataType::Float, false, STRIDE, 8);
device_.enableVertexAttrib(2);
- device_.vertexAttribPointer(2, 4, GL_FLOAT, false, STRIDE, 16);
+ device_.vertexAttribPointer(2, 4, GfxDataType::Float, false, STRIDE, 16);
break;
}
}
diff --git a/src/esengine/renderer/plugins/ShapePlugin.cpp b/src/esengine/renderer/plugins/ShapePlugin.cpp
index 1b00d290b..59d223f60 100644
--- a/src/esengine/renderer/plugins/ShapePlugin.cpp
+++ b/src/esengine/renderer/plugins/ShapePlugin.cpp
@@ -1,5 +1,4 @@
#include "ShapePlugin.hpp"
-#include "../OpenGLHeaders.hpp"
#include "../RenderCommand.hpp"
#include "../RenderContext.hpp"
#include "../RenderFrame.hpp"
@@ -141,7 +140,7 @@ void ShapePlugin::customDraw(
RenderCommand::getDevice()->drawElements(
cmd.index_count,
- GL_UNSIGNED_SHORT,
+ GfxDataType::UnsignedShort,
static_cast<u32>(cmd.index_offset * sizeof(u16)));
}