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: inject GfxDevice via constructors, remove Engine singleton

esengine9083400d8022
diff --git a/src/esengine/app/App.cpp b/src/esengine/app/App.cpp
index 85ac43b69..7a01b9f3d 100644
--- a/src/esengine/app/App.cpp
+++ b/src/esengine/app/App.cpp
@@ -109,12 +109,11 @@ void App::init() {
resourceManager_.init();
gfxDevice_ = makeUnique<GLDevice>();
- RenderCommand::setDevice(gfxDevice_.get());
- renderContext_ = makeUnique<RenderContext>();
+ renderContext_ = makeUnique<RenderContext>(*gfxDevice_);
renderContext_->init();
- renderer_ = makeUnique<Renderer>(*renderContext_);
+ renderer_ = makeUnique<Renderer>(*gfxDevice_, *renderContext_);
renderer_->setViewport(0, 0, config_.width, config_.height);
services_.registerService<Platform>(platform_.get());
@@ -152,7 +151,6 @@ void App::shutdown() {
renderContext_->shutdown();
renderContext_.reset();
- RenderCommand::setDevice(nullptr);
gfxDevice_.reset();
resourceManager_.shutdown();
diff --git a/src/esengine/bindings/GeometryBindings.cpp b/src/esengine/bindings/GeometryBindings.cpp
index dfc2057f2..c6b535bee 100644
--- a/src/esengine/bindings/GeometryBindings.cpp
+++ b/src/esengine/bindings/GeometryBindings.cpp
@@ -141,7 +141,7 @@ void draw_mesh(u32 geometryHandle, u32 shaderHandle, uintptr_t transformPtr) {
shader->setUniform("u_projection", g_currentViewProjection);
shader->setUniform("u_model", transform);
- geom->bind();
+ geom->bind(ctx().require<GfxDevice>());
if (geom->hasIndices()) {
auto* ib = geom->getVAO() ? geom->getVAO()->getIndexBuffer().get() : nullptr;
@@ -231,7 +231,7 @@ void draw_meshWithUniforms(u32 geometryHandle, u32 shaderHandle, uintptr_t trans
}
}
- geom->bind();
+ geom->bind(ctx().require<GfxDevice>());
if (geom->hasIndices()) {
auto* ib = geom->getVAO() ? geom->getVAO()->getIndexBuffer().get() : nullptr;
diff --git a/src/esengine/bindings/PostProcessBindings.cpp b/src/esengine/bindings/PostProcessBindings.cpp
index 4d1c6cc70..408d3a9f1 100644
--- a/src/esengine/bindings/PostProcessBindings.cpp
+++ b/src/esengine/bindings/PostProcessBindings.cpp
@@ -33,7 +33,7 @@ bool postprocess_init(u32 width, u32 height) {
if (!g_postProcessPipeline) {
ctx().services().registerOwned<PostProcessPipeline>(
- makeUnique<PostProcessPipeline>(*g_renderContext, *g_resourceManager));
+ makeUnique<PostProcessPipeline>(ctx().require<GfxDevice>(), *g_renderContext, *g_resourceManager));
}
g_postProcessPipeline->init(width, height);
diff --git a/src/esengine/bindings/WebSDKEntry.cpp b/src/esengine/bindings/WebSDKEntry.cpp
index edfaba019..314982690 100644
--- a/src/esengine/bindings/WebSDKEntry.cpp
+++ b/src/esengine/bindings/WebSDKEntry.cpp
@@ -210,10 +210,10 @@ static void initSubsystems() {
svc.registerOwned<resource::ResourceManager>(std::move(resourceManager));
auto gfxDevice = makeUnique<GLDevice>();
- RenderCommand::setDevice(gfxDevice.get());
+ auto* gfxDevicePtr = gfxDevice.get();
svc.registerOwned<GfxDevice>(std::move(gfxDevice));
- auto renderContext = makeUnique<RenderContext>();
+ auto renderContext = makeUnique<RenderContext>(*gfxDevicePtr);
renderContext->init();
svc.registerOwned<RenderContext>(std::move(renderContext));
@@ -233,7 +233,7 @@ static void initSubsystems() {
svc.registerOwned<spine::SpineSystem>(makeUnique<spine::SpineSystem>(*g_spineResourceManager));
#endif
- auto immediateDraw = makeUnique<ImmediateDraw>(*g_renderContext, *g_resourceManager);
+ auto immediateDraw = makeUnique<ImmediateDraw>(*gfxDevicePtr, *g_renderContext, *g_resourceManager);
immediateDraw->init();
svc.registerOwned<ImmediateDraw>(std::move(immediateDraw));
diff --git a/src/esengine/core/Engine.cpp b/src/esengine/core/Engine.cpp
index 721b9003b..7bae99894 100644
--- a/src/esengine/core/Engine.cpp
+++ b/src/esengine/core/Engine.cpp
@@ -1,6 +1,6 @@
/**
* @file Engine.cpp
- * @brief Engine singleton implementation
+ * @brief Engine static utility implementation
* @details Provides platform detection and GPU capability queries.
*
* @author ESEngine Team
@@ -29,11 +29,6 @@
namespace esengine {
-Engine& Engine::get() {
- static Engine instance;
- return instance;
-}
-
const char* Engine::getPlatformName() {
#ifdef ES_PLATFORM_WXGAME
return "WeChat MiniGame";
diff --git a/src/esengine/core/Engine.hpp b/src/esengine/core/Engine.hpp
index 330dfd97e..54700a8b8 100644
--- a/src/esengine/core/Engine.hpp
+++ b/src/esengine/core/Engine.hpp
@@ -77,16 +77,6 @@ struct EngineVersion {
*/
class Engine {
public:
- // =========================================================================
- // Instance Access
- // =========================================================================
-
- /**
- * @brief Gets the engine singleton instance
- * @return Reference to the engine instance
- */
- static Engine& get();
-
// =========================================================================
// Engine Information
// =========================================================================
@@ -137,13 +127,7 @@ public:
*/
static u32 getMaxTextureSize();
-private:
- Engine() = default;
- ~Engine() = default;
-
- // Non-copyable
- Engine(const Engine&) = delete;
- Engine& operator=(const Engine&) = delete;
+ Engine() = delete;
};
} // namespace esengine
diff --git a/src/esengine/renderer/CustomGeometry.cpp b/src/esengine/renderer/CustomGeometry.cpp
index d20242447..0730e962e 100644
--- a/src/esengine/renderer/CustomGeometry.cpp
+++ b/src/esengine/renderer/CustomGeometry.cpp
@@ -11,7 +11,7 @@
#include "CustomGeometry.hpp"
-#include "RenderCommand.hpp"
+#include "GfxDevice.hpp"
#include "../core/Log.hpp"
namespace esengine {
@@ -63,7 +63,7 @@ void CustomGeometry::updateVertices(const f32* vertices, u32 vertexCount, u32 of
}
}
-void CustomGeometry::bind() const {
+void CustomGeometry::bind(GfxDevice& device) const {
if (!vao_) return;
vao_->bind();
@@ -91,9 +91,8 @@ void CustomGeometry::bind() const {
gfxType = GfxDataType::Float;
break;
}
- auto* device = RenderCommand::getDevice();
- device->enableVertexAttrib(index);
- device->vertexAttribPointer(
+ device.enableVertexAttrib(index);
+ device.vertexAttribPointer(
index,
static_cast<i32>(shaderDataTypeComponentCount(attr.type)),
gfxType,
diff --git a/src/esengine/renderer/CustomGeometry.hpp b/src/esengine/renderer/CustomGeometry.hpp
index fa73824d8..1ff1f801b 100644
--- a/src/esengine/renderer/CustomGeometry.hpp
+++ b/src/esengine/renderer/CustomGeometry.hpp
@@ -67,7 +67,7 @@ public:
/**
* @brief Binds the geometry for rendering
*/
- void bind() const;
+ void bind(GfxDevice& device) const;
/**
* @brief Unbinds the geometry
diff --git a/src/esengine/renderer/ImmediateDraw.cpp b/src/esengine/renderer/ImmediateDraw.cpp
index ab98afdbf..e8b3a1e4f 100644
--- a/src/esengine/renderer/ImmediateDraw.cpp
+++ b/src/esengine/renderer/ImmediateDraw.cpp
@@ -11,7 +11,7 @@
#include "ImmediateDraw.hpp"
#include "Renderer.hpp"
-#include "RenderCommand.hpp"
+#include "GfxDevice.hpp"
#include "RenderContext.hpp"
#include "../resource/ResourceManager.hpp"
#include "../core/Log.hpp"
@@ -26,9 +26,11 @@ struct ImmediateDraw::Impl {
glm::mat4 viewProjection{1.0f};
};
-ImmediateDraw::ImmediateDraw(RenderContext& context,
+ImmediateDraw::ImmediateDraw(GfxDevice& device,
+ RenderContext& context,
resource::ResourceManager& resource_manager)
: impl_(makeUnique<Impl>())
+ , device_(device)
, context_(context)
, resource_manager_(resource_manager) {
}
@@ -42,7 +44,7 @@ ImmediateDraw::~ImmediateDraw() {
void ImmediateDraw::init() {
if (initialized_) return;
- impl_->batcher = makeUnique<BatchRenderer2D>(context_, resource_manager_);
+ impl_->batcher = makeUnique<BatchRenderer2D>(device_, context_, resource_manager_);
impl_->batcher->init();
initialized_ = true;
@@ -63,10 +65,9 @@ void ImmediateDraw::shutdown() {
void ImmediateDraw::begin(const glm::mat4& viewProjection) {
if (!initialized_) return;
- auto* device = RenderCommand::getDevice();
- device->setBlendEnabled(true);
- device->setBlendMode(BlendMode::Normal);
- device->setDepthTest(false);
+ device_.setBlendEnabled(true);
+ device_.setBlendMode(BlendMode::Normal);
+ device_.setDepthTest(false);
impl_->viewProjection = viewProjection;
impl_->batcher->setProjection(viewProjection);
@@ -80,9 +81,8 @@ void ImmediateDraw::end() {
if (!initialized_ || !inFrame_) return;
impl_->batcher->endBatch();
- auto* device = RenderCommand::getDevice();
- device->setDepthTest(true);
- device->setBlendMode(BlendMode::Normal);
+ device_.setDepthTest(true);
+ device_.setBlendMode(BlendMode::Normal);
inFrame_ = false;
}
diff --git a/src/esengine/renderer/ImmediateDraw.hpp b/src/esengine/renderer/ImmediateDraw.hpp
index b0f64fa42..0482d0e6e 100644
--- a/src/esengine/renderer/ImmediateDraw.hpp
+++ b/src/esengine/renderer/ImmediateDraw.hpp
@@ -22,6 +22,7 @@
namespace esengine {
+class GfxDevice;
class RenderContext;
class BatchRenderer2D;
@@ -55,7 +56,7 @@ public:
* @param context Render context for shared resources
* @param resource_manager Resource manager for shader/texture access
*/
- ImmediateDraw(RenderContext& context, resource::ResourceManager& resource_manager);
+ ImmediateDraw(GfxDevice& device, RenderContext& context, resource::ResourceManager& resource_manager);
~ImmediateDraw();
ImmediateDraw(const ImmediateDraw&) = delete;
@@ -240,6 +241,7 @@ private:
struct Impl;
Unique<Impl> impl_;
+ GfxDevice& device_;
RenderContext& context_;
resource::ResourceManager& resource_manager_;
diff --git a/src/esengine/renderer/PostProcessPipeline.cpp b/src/esengine/renderer/PostProcessPipeline.cpp
index 34dbce6e0..57f637805 100644
--- a/src/esengine/renderer/PostProcessPipeline.cpp
+++ b/src/esengine/renderer/PostProcessPipeline.cpp
@@ -11,7 +11,7 @@
#include "PostProcessPipeline.hpp"
#include "RenderContext.hpp"
-#include "RenderCommand.hpp" // for RenderCommand::getDevice()
+#include "GfxDevice.hpp"
#include "Shader.hpp"
#include "../resource/ResourceManager.hpp"
#include "../core/Log.hpp"
@@ -47,9 +47,11 @@ void main() {
}
)";
-PostProcessPipeline::PostProcessPipeline(RenderContext& context,
+PostProcessPipeline::PostProcessPipeline(GfxDevice& device,
+ RenderContext& context,
resource::ResourceManager& resourceManager)
- : context_(context)
+ : device_(device)
+ , context_(context)
, resourceManager_(resourceManager) {
}
@@ -113,7 +115,7 @@ void PostProcessPipeline::shutdown() {
passes_.clear();
screenPasses_.clear();
- auto* device = RenderCommand::getDevice();
+ auto* device = &device_;
if (screen_quad_vbo_ != 0 && device) {
device->deleteBuffer(screen_quad_vbo_);
screen_quad_vbo_ = 0;
@@ -243,7 +245,7 @@ PostProcessPass* PostProcessPipeline::findPass(const std::string& name) {
void PostProcessPipeline::ensureScreenQuad() {
if (screen_quad_vao_ != 0) return;
- auto* device = RenderCommand::getDevice();
+ auto* device = &device_;
f32 vertices[] = {
-1.0f, -1.0f, 0.0f, 0.0f,
@@ -273,7 +275,7 @@ void PostProcessPipeline::ensureScreenQuad() {
void PostProcessPipeline::drawScreenQuad() {
ensureScreenQuad();
- auto* device = RenderCommand::getDevice();
+ auto* device = &device_;
device->bindVertexArray(screen_quad_vao_);
device->drawArrays(0, 3);
}
@@ -284,7 +286,7 @@ void PostProcessPipeline::begin() {
ensureFBOs();
if (!fboOriginalCreated_) return;
- auto* device = RenderCommand::getDevice();
+ auto* device = &device_;
fboOriginal_->bind();
device->setViewport(0, 0, width_, height_);
device->clear(true, true, false);
@@ -296,7 +298,7 @@ void PostProcessPipeline::begin() {
void PostProcessPipeline::end() {
if (!initialized_ || !inFrame_ || bypass_) return;
- auto* device = RenderCommand::getDevice();
+ auto* device = &device_;
u32 enabledCount = 0;
for (const auto& pass : passes_) {
@@ -350,7 +352,7 @@ void PostProcessPipeline::renderPass(const PostProcessPass& pass, u32 inputTextu
Shader* shader = resourceManager_.getShader(pass.shader);
if (!shader) return;
- auto* device = RenderCommand::getDevice();
+ auto* device = &device_;
device->bindTexture(0, inputTexture);
shader->bind();
@@ -388,7 +390,7 @@ void PostProcessPipeline::blitToOutput(u32 texture) {
Shader* shader = resourceManager_.getShader(blitShader_);
if (!shader) return;
- auto* device = RenderCommand::getDevice();
+ auto* device = &device_;
device->bindFramebuffer(output_target_fbo_);
if (output_vp_w_ > 0 && output_vp_h_ > 0) {
@@ -435,7 +437,7 @@ void PostProcessPipeline::beginScreenCapture() {
ensureScreenFBO();
if (!screenFBOCreated_) return;
- auto* device = RenderCommand::getDevice();
+ auto* device = &device_;
screenFBO_->bind();
device->setViewport(0, 0, width_, height_);
device->clear(true, true, false);
@@ -453,7 +455,7 @@ void PostProcessPipeline::endScreenCapture() {
void PostProcessPipeline::executeScreenPasses() {
if (!initialized_ || !screenFBOCreated_) return;
- auto* device = RenderCommand::getDevice();
+ auto* device = &device_;
u32 enabledCount = 0;
for (const auto& pass : screenPasses_) {
diff --git a/src/esengine/renderer/PostProcessPipeline.hpp b/src/esengine/renderer/PostProcessPipeline.hpp
index 69c584142..54c285e14 100644
--- a/src/esengine/renderer/PostProcessPipeline.hpp
+++ b/src/esengine/renderer/PostProcessPipeline.hpp
@@ -22,6 +22,7 @@
namespace esengine {
+class GfxDevice;
class RenderContext;
namespace resource {
@@ -47,7 +48,7 @@ struct PostProcessPass {
*/
class PostProcessPipeline {
public:
- PostProcessPipeline(RenderContext& context, resource::ResourceManager& resourceManager);
+ PostProcessPipeline(GfxDevice& device, RenderContext& context, resource::ResourceManager& resourceManager);
~PostProcessPipeline();
PostProcessPipeline(const PostProcessPipeline&) = delete;
@@ -222,6 +223,7 @@ private:
void renderPass(const PostProcessPass& pass, u32 inputTexture);
void blitToOutput(u32 texture);
+ GfxDevice& device_;
RenderContext& context_;
resource::ResourceManager& resourceManager_;
diff --git a/src/esengine/renderer/RenderContext.cpp b/src/esengine/renderer/RenderContext.cpp
index 378cb033d..c6d9ca8ef 100644
--- a/src/esengine/renderer/RenderContext.cpp
+++ b/src/esengine/renderer/RenderContext.cpp
@@ -10,13 +10,17 @@
*/
#include "RenderContext.hpp"
-#include "RenderCommand.hpp"
+#include "GfxDevice.hpp"
#include "ShaderEmbeds.generated.hpp"
#include "../resource/ShaderParser.hpp"
#include "../core/Log.hpp"
namespace esengine {
+RenderContext::RenderContext(GfxDevice& device)
+ : device_(device) {
+}
+
RenderContext::~RenderContext() {
if (initialized_) {
shutdown();
@@ -29,7 +33,7 @@ void RenderContext::init() {
return;
}
- RenderCommand::init();
+ device_.init();
initQuadData();
initShaders();
initWhiteTexture();
@@ -42,9 +46,8 @@ void RenderContext::shutdown() {
return;
}
- auto* device = RenderCommand::getDevice();
- if (whiteTextureId_ != 0 && device) {
- device->deleteTexture(whiteTextureId_);
+ if (whiteTextureId_ != 0) {
+ device_.deleteTexture(whiteTextureId_);
whiteTextureId_ = 0;
}
@@ -53,7 +56,7 @@ void RenderContext::shutdown() {
textureShader_.reset();
extMeshShader_.reset();
- RenderCommand::shutdown();
+ device_.shutdown();
initialized_ = false;
ES_LOG_INFO("RenderContext shutdown");
}
@@ -107,12 +110,11 @@ void RenderContext::initShaders() {
}
void RenderContext::initWhiteTexture() {
- auto* device = RenderCommand::getDevice();
- whiteTextureId_ = device->createTexture();
+ whiteTextureId_ = device_.createTexture();
u32 whiteData = 0xFFFFFFFF;
- device->texImage2D(whiteTextureId_, 1, 1, GfxPixelFormat::RGBA8, &whiteData);
- device->setTextureParams(whiteTextureId_, TextureFilter::Nearest, TextureFilter::Nearest,
+ device_.texImage2D(whiteTextureId_, 1, 1, GfxPixelFormat::RGBA8, &whiteData);
+ device_.setTextureParams(whiteTextureId_, TextureFilter::Nearest, TextureFilter::Nearest,
TextureWrap::ClampToEdge, TextureWrap::ClampToEdge);
ES_LOG_DEBUG("White texture created (ID: {})", whiteTextureId_);
diff --git a/src/esengine/renderer/RenderContext.hpp b/src/esengine/renderer/RenderContext.hpp
index 0e3caed54..1cafb75fd 100644
--- a/src/esengine/renderer/RenderContext.hpp
+++ b/src/esengine/renderer/RenderContext.hpp
@@ -67,9 +67,12 @@ struct RenderContextStats {
* context.shutdown();
* @endcode
*/
+class GfxDevice;
+
class RenderContext {
public:
- RenderContext() = default;
+ explicit RenderContext(GfxDevice& device);
+ RenderContext() = delete;
~RenderContext();
// Non-copyable
@@ -169,6 +172,7 @@ private:
Unique<Shader> extMeshShader_;
u32 whiteTextureId_ = 0;
+ GfxDevice& device_;
bool initialized_ = false;
};
diff --git a/src/esengine/renderer/RenderFrame.cpp b/src/esengine/renderer/RenderFrame.cpp
index caa8a1e4c..1f73b6a34 100644
--- a/src/esengine/renderer/RenderFrame.cpp
+++ b/src/esengine/renderer/RenderFrame.cpp
@@ -78,7 +78,7 @@ void RenderFrame::init(u32 width, u32 height) {
state_tracker_.init();
#ifdef ES_ENABLE_POSTPROCESS
- post_process_ = makeUnique<PostProcessPipeline>(context_, resource_manager_);
+ post_process_ = makeUnique<PostProcessPipeline>(device_, context_, resource_manager_);
post_process_->init(width, height);
#endif
diff --git a/src/esengine/renderer/Renderer.cpp b/src/esengine/renderer/Renderer.cpp
index 536abb046..8c7358da1 100644
--- a/src/esengine/renderer/Renderer.cpp
+++ b/src/esengine/renderer/Renderer.cpp
@@ -12,7 +12,7 @@
#include "Renderer.hpp"
#include "BatchVertex.hpp"
-#include "RenderCommand.hpp"
+#include "GfxDevice.hpp"
#include "TextureSlotAllocator.hpp"
#include "ShaderEmbeds.generated.hpp"
#include "../resource/ShaderParser.hpp"
@@ -114,8 +114,8 @@ void RenderCommand::setWireframe(bool enabled) {
// Renderer Implementation
// ========================================
-Renderer::Renderer(RenderContext& context)
- : context_(context) {
+Renderer::Renderer(GfxDevice& device, RenderContext& context)
+ : device_(device), context_(context) {
}
void Renderer::beginFrame() {
@@ -127,15 +127,15 @@ void Renderer::endFrame() {
}
void Renderer::setViewport(i32 x, i32 y, u32 width, u32 height) {
- RenderCommand::setViewport(x, y, width, height);
+ device_.setViewport(x, y, width, height);
}
void Renderer::setClearColor(const glm::vec4& color) {
- RenderCommand::setClearColor(color);
+ device_.setClearColor(color.r, color.g, color.b, color.a);
}
void Renderer::clear() {
- RenderCommand::clear();
+ device_.clear(true, true, false);
}
void Renderer::beginScene(const glm::mat4& viewProjection) {
@@ -151,7 +151,13 @@ void Renderer::submit(const Shader& shader, const VertexArray& vao, const glm::m
shader.setUniform("u_projection", context_.viewProjection());
shader.setUniform("u_model", transform);
- RenderCommand::drawIndexed(vao);
+ vao.bind();
+ auto ib = vao.getIndexBuffer();
+ u32 count = ib ? ib->getCount() : 0;
+ if (count > 0 && ib) {
+ auto type = ib->is16Bit() ? GfxDataType::UnsignedShort : GfxDataType::UnsignedInt;
+ device_.drawElements(count, type, 0);
+ }
context_.stats().drawCalls++;
}
@@ -173,7 +179,15 @@ void Renderer::drawQuad(const glm::vec3& position, const glm::vec2& size, const
shader->setUniform("u_model", transform);
shader->setUniform("u_color", color);
- RenderCommand::drawIndexed(*vao);
+ vao->bind();
+ {
+ auto ib = vao->getIndexBuffer();
+ u32 cnt = ib ? ib->getCount() : 0;
+ if (cnt > 0 && ib) {
+ auto type = ib->is16Bit() ? GfxDataType::UnsignedShort : GfxDataType::UnsignedInt;
+ device_.drawElements(cnt, type, 0);
+ }
+ }
context_.stats().drawCalls++;
context_.stats().triangleCount += 2;
@@ -201,7 +215,15 @@ void Renderer::drawQuad(const glm::vec3& position, const glm::vec2& size,
shader->setUniform("u_color", tintColor);
shader->setUniform("u_texture", 0);
- RenderCommand::drawIndexed(*vao);
+ vao->bind();
+ {
+ auto ib = vao->getIndexBuffer();
+ u32 cnt = ib ? ib->getCount() : 0;
+ if (cnt > 0 && ib) {
+ auto type = ib->is16Bit() ? GfxDataType::UnsignedShort : GfxDataType::UnsignedInt;
+ device_.drawElements(cnt, type, 0);
+ }
+ }
context_.stats().drawCalls++;
context_.stats().triangleCount += 2;
@@ -272,9 +294,11 @@ struct BatchRenderer2D::BatchData {
bool initialized = false;
};
-BatchRenderer2D::BatchRenderer2D(RenderContext& context,
+BatchRenderer2D::BatchRenderer2D(GfxDevice& device,
+ RenderContext& context,
resource::ResourceManager& resource_manager)
: data_(makeUnique<BatchData>())
+ , device_(device)
, context_(context)
, resource_manager_(resource_manager) {
}
@@ -346,10 +370,9 @@ void BatchRenderer2D::init() {
if (batchShader && batchShader->isValid()) {
batchShader->bind();
- auto* dev = RenderCommand::getDevice();
- i32 texLoc = dev->getUniformLocation(batchShader->getProgramId(), "u_texture");
+ i32 texLoc = device_.getUniformLocation(batchShader->getProgramId(), "u_texture");
if (texLoc >= 0) {
- dev->setUniform1i(texLoc, 0);
+ device_.setUniform1i(texLoc, 0);
}
} else {
ES_LOG_ERROR("All batch shader variants FAILED!");
@@ -411,26 +434,25 @@ void BatchRenderer2D::flush() {
data_->vbo->setDataRaw(data_->triVertices.data(), triBytes);
}
- data_->texSlots.bindAll(*RenderCommand::getDevice());
+ data_->texSlots.bindAll(device_);
shader->bind();
shader->setUniform(data_->projectionLoc, data_->projection);
data_->vao->bind();
- auto* dev = RenderCommand::getDevice();
if (hasQuads) {
auto ib = data_->vao->getIndexBuffer();
if (ib) {
ib->bind();
auto type = ib->is16Bit() ? GfxDataType::UnsignedShort : GfxDataType::UnsignedInt;
- dev->drawElements(data_->indexCount, type, 0);
+ device_.drawElements(data_->indexCount, type, 0);
}
data_->drawCallCount++;
}
if (hasTris) {
- dev->drawArrays(static_cast<u32>(data_->vertices.size()),
+ device_.drawArrays(static_cast<u32>(data_->vertices.size()),
static_cast<u32>(data_->triVertices.size()));
data_->drawCallCount++;
}
diff --git a/src/esengine/renderer/Renderer.hpp b/src/esengine/renderer/Renderer.hpp
index ad93cfbfc..e1f2b96cb 100644
--- a/src/esengine/renderer/Renderer.hpp
+++ b/src/esengine/renderer/Renderer.hpp
@@ -70,10 +70,11 @@ using RendererStats = RenderContextStats;
class Renderer {
public:
/**
- * @brief Constructs a renderer with the given context
+ * @brief Constructs a renderer with the given device and context
+ * @param device Reference to the graphics device
* @param context Reference to the rendering context
*/
- explicit Renderer(RenderContext& context);
+ Renderer(GfxDevice& device, RenderContext& context);
~Renderer() = default;
@@ -218,6 +219,7 @@ public:
RenderContext& getContext() { return context_; }
private:
+ GfxDevice& device_;
RenderContext& context_;
};
@@ -248,10 +250,11 @@ class BatchRenderer2D {
public:
/**
* @brief Constructs a batch renderer
+ * @param device Graphics device
* @param context Render context for shared resources
* @param resource_manager Resource manager for shader loading
*/
- BatchRenderer2D(RenderContext& context, resource::ResourceManager& resource_manager);
+ BatchRenderer2D(GfxDevice& device, RenderContext& context, resource::ResourceManager& resource_manager);
~BatchRenderer2D();
@@ -416,6 +419,7 @@ public:
private:
struct BatchData;
Unique<BatchData> data_;
+ GfxDevice& device_;
RenderContext& context_;
resource::ResourceManager& resource_manager_;
FlushCallback flush_callback_ = nullptr;
diff --git a/src/esengine/renderer/plugins/ShapePlugin.cpp b/src/esengine/renderer/plugins/ShapePlugin.cpp
index 0262704a6..4b8137d62 100644
--- a/src/esengine/renderer/plugins/ShapePlugin.cpp
+++ b/src/esengine/renderer/plugins/ShapePlugin.cpp
@@ -1,5 +1,4 @@
#include "ShapePlugin.hpp"
-#include "../RenderCommand.hpp"
#include "../RenderContext.hpp"
#include "../RenderFrame.hpp"
#include "../Shader.hpp"
@@ -137,7 +136,7 @@ void ShapePlugin::customDraw(
buffers.bindLayout(LayoutId::Shape);
- RenderCommand::getDevice()->drawElements(
+ state.device().drawElements(
cmd.index_count,
GfxDataType::UnsignedShort,
static_cast<u32>(cmd.index_offset * sizeof(u16)));