Skip to content

Commit 60d6275

Browse files
authored
Merge pull request #103 from Legacy-LuaSTG-Engine/dev/swap-chain-code-review
[dev] SwapChain code review
2 parents e72415e + ae413dc commit 60d6275

10 files changed

Lines changed: 1853 additions & 1955 deletions

File tree

engine/graphics/common/PrimitiveBatchRenderer.cpp

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,27 @@
11
#include "common/PrimitiveBatchRenderer.hpp"
22

33
namespace core {
4+
void PrimitiveBatchRenderer::onGraphicsDeviceCreate() {
5+
// nothing
6+
}
7+
void PrimitiveBatchRenderer::onGraphicsDeviceDestroy() {
8+
if (m_initialized && m_batch_scope) {
9+
mapBuffers(false);
10+
}
11+
}
12+
413
PrimitiveBatchRenderer::PrimitiveBatchRenderer() = default;
5-
PrimitiveBatchRenderer::~PrimitiveBatchRenderer() = default;
14+
PrimitiveBatchRenderer::~PrimitiveBatchRenderer() {
15+
if (m_initialized) {
16+
m_device->removeEventListener(this);
17+
}
18+
}
619

720
bool PrimitiveBatchRenderer::createResources(IGraphicsDevice* const device) {
21+
if (m_initialized && m_device) {
22+
m_device->removeEventListener(this);
23+
}
24+
825
if (device == nullptr) {
926
assert(false); return false;
1027
}
@@ -20,10 +37,14 @@ namespace core {
2037
return false;
2138
}
2239

40+
m_device->addEventListener(this);
2341
m_initialized = true;
2442
return true;
2543
}
2644

45+
bool PrimitiveBatchRenderer::isBatch() const noexcept {
46+
return m_batch_scope;
47+
}
2748
bool PrimitiveBatchRenderer::beginBatch(const bool auto_draw) {
2849
if (m_batch_scope) {
2950
assert(false); return false;

engine/graphics/common/PrimitiveBatchRenderer.hpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@
44
#include "core/SmartReference.hpp"
55

66
namespace core {
7-
class PrimitiveBatchRenderer {
7+
class PrimitiveBatchRenderer : public IGraphicsDeviceEventListener {
88
public:
99
using DrawVertex = Graphics::IRenderer::DrawVertex;
1010
using DrawIndex = Graphics::IRenderer::DrawIndex;
1111

12+
void onGraphicsDeviceCreate() override;
13+
void onGraphicsDeviceDestroy() override;
14+
1215
PrimitiveBatchRenderer();
1316
PrimitiveBatchRenderer(const PrimitiveBatchRenderer&) = delete;
1417
PrimitiveBatchRenderer(PrimitiveBatchRenderer&&) = delete;
@@ -20,6 +23,8 @@ namespace core {
2023
// [free]
2124
bool createResources(IGraphicsDevice* device);
2225

26+
// [free]
27+
bool isBatch() const noexcept;
2328
// [free]
2429
bool beginBatch(bool auto_draw);
2530
// [batch]

engine/graphics/core/Graphics/Renderer_D3D11.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,9 @@ namespace core::Graphics {
303303

304304
if (discard) {
305305
m_primitive_batch_renderer.discard();
306-
m_primitive_batch_renderer.endBatch();
306+
if (m_primitive_batch_renderer.isBatch()) {
307+
m_primitive_batch_renderer.endBatch();
308+
}
307309
m_primitive_batch_renderer.setCycleOnNextBatch();
308310
setTexture(_state_texture.get());
309311
return true;

engine/graphics/d3d11/GraphicsPipeline.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,12 @@ namespace core {
290290
}
291291
m_device = device;
292292
m_graphics_pipeline_state_helper.save(create_info);
293-
return createResources();
293+
if (!createResources()) {
294+
return false;
295+
}
296+
m_device->addEventListener(this);
297+
m_initialized = true;
298+
return true;
294299
}
295300
void GraphicsPipeline::apply() {
296301
const auto ctx = static_cast<ID3D11DeviceContext*>(m_device->getCommandbuffer()->getNativeHandle());
@@ -403,8 +408,6 @@ namespace core {
403408
return false;
404409
}
405410

406-
m_device->addEventListener(this);
407-
m_initialized = true;
408411
return true;
409412
}
410413
}

0 commit comments

Comments
 (0)