Skip to content

Commit d79a202

Browse files
smilediverhalx99
andauthored
Renderer fixes (#1996)
* Backend: remove RenderTargetFlag and refactor depth/stencil state setup * Fix Z-test and Z-write being enabled by default for main queue * GL: fix framebuffer attachment not being cleared * Use dirtyFlags for RenderTarget update --------- Co-authored-by: halx99 <[email protected]>
1 parent 8175eb2 commit d79a202

23 files changed

+144
-234
lines changed

core/2d/Grid.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ void GridBase::beforeDraw()
219219
_oldRenderTarget = renderer->getRenderTarget();
220220
AX_SAFE_RELEASE(_renderTarget);
221221
_renderTarget =
222-
backend::DriverBase::getInstance()->newRenderTarget(TargetBufferFlags::COLOR, _texture->getBackendTexture());
222+
backend::DriverBase::getInstance()->newRenderTarget(_texture->getBackendTexture());
223223
renderer->setRenderTarget(_renderTarget);
224224
};
225225
renderer->addCallbackCommand(beforeDrawCommandFunc);

core/2d/RenderTexture.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,11 +187,9 @@ bool RenderTexture::initWithWidthAndHeight(int w,
187187
descriptor.textureFormat = PixelFormat::RGBA8;
188188
_texture2D = new Texture2D();
189189
_texture2D->updateTextureDescriptor(descriptor, !!AX_ENABLE_PREMULTIPLIED_ALPHA);
190-
_renderTargetFlags = RenderTargetFlag::COLOR;
191190

192191
if (PixelFormat::D24S8 == depthStencilFormat || sharedRenderTarget)
193192
{
194-
_renderTargetFlags = RenderTargetFlag::ALL;
195193
descriptor.textureFormat = PixelFormat::D24S8;
196194

197195
AX_SAFE_RELEASE(_depthStencilTexture);
@@ -210,7 +208,7 @@ bool RenderTexture::initWithWidthAndHeight(int w,
210208
else
211209
{
212210
_renderTarget = backend::DriverBase::getInstance()->newRenderTarget(
213-
_renderTargetFlags, _texture2D ? _texture2D->getBackendTexture() : nullptr,
211+
_texture2D ? _texture2D->getBackendTexture() : nullptr,
214212
_depthStencilTexture ? _depthStencilTexture->getBackendTexture() : nullptr,
215213
_depthStencilTexture ? _depthStencilTexture->getBackendTexture() : nullptr);
216214
}

core/2d/RenderTexture.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,6 @@ class AX_DLL RenderTexture : public Node
396396

397397
backend::RenderTarget* _renderTarget = nullptr;
398398
backend::RenderTarget* _oldRenderTarget = nullptr;
399-
RenderTargetFlag _renderTargetFlags{};
400399

401400
RefPtr<Image> _UITextureImage = nullptr;
402401
backend::PixelFormat _pixelFormat = backend::PixelFormat::RGBA8;

core/base/Types.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,6 @@ using PixelFormat = backend::PixelFormat;
469469

470470
using TargetBufferFlags = backend::TargetBufferFlags;
471471
using DepthStencilFlags = backend::DepthStencilFlags;
472-
using RenderTargetFlag = backend::RenderTargetFlag;
473472
using ClearFlag = backend::ClearFlag;
474473

475474
typedef void (*AsyncOperation)(void* param);

core/renderer/Renderer.cpp

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -204,9 +204,8 @@ void Renderer::init()
204204

205205
auto driver = backend::DriverBase::getInstance();
206206
_commandBuffer = driver->newCommandBuffer();
207-
// @MTL: the depth stencil flags must same render target and _dsDesc
208207
_dsDesc.flags = DepthStencilFlags::ALL;
209-
_defaultRT = driver->newDefaultRenderTarget(TargetBufferFlags::COLOR | TargetBufferFlags::DEPTH_AND_STENCIL);
208+
_defaultRT = driver->newDefaultRenderTarget();
210209

211210
_currentRT = _defaultRT;
212211
_renderPipeline = driver->newRenderPipeline();
@@ -218,7 +217,7 @@ void Renderer::init()
218217

219218
backend::RenderTarget* Renderer::getOffscreenRenderTarget() {
220219
if (_offscreenRT != nullptr) return _offscreenRT;
221-
return (_offscreenRT = backend::DriverBase::getInstance()->newRenderTarget(TargetBufferFlags::COLOR | TargetBufferFlags::DEPTH_AND_STENCIL));
220+
return (_offscreenRT = backend::DriverBase::getInstance()->newRenderTarget());
222221
}
223222

224223
void Renderer::addCallbackCommand(std::function<void()> func, float globalZOrder)
@@ -283,13 +282,7 @@ void Renderer::processGroupCommand(GroupCommand* command)
283282

284283
int renderQueueID = ((GroupCommand*)command)->getRenderQueueID();
285284

286-
pushStateBlock();
287-
// apply default state for all render queues
288-
setDepthTest(false);
289-
setDepthWrite(false);
290-
setCullMode(backend::CullMode::NONE);
291285
visitRenderQueue(_renderGroups[renderQueueID]);
292-
popStateBlock();
293286
}
294287

295288
void Renderer::processRenderCommand(RenderCommand* command)
@@ -358,6 +351,13 @@ void Renderer::processRenderCommand(RenderCommand* command)
358351

359352
void Renderer::visitRenderQueue(RenderQueue& queue)
360353
{
354+
pushStateBlock();
355+
356+
// Apply default state for all render queues
357+
setDepthTest(false);
358+
setDepthWrite(false);
359+
setCullMode(backend::CullMode::NONE);
360+
361361
//
362362
// Process Global-Z < 0 Objects
363363
//
@@ -388,6 +388,8 @@ void Renderer::visitRenderQueue(RenderQueue& queue)
388388
// Process Global-Z > 0 Queue
389389
//
390390
doVisitRenderQueue(queue.getSubQueue(RenderQueue::QUEUE_GROUP::GLOBALZ_POS));
391+
392+
popStateBlock();
391393
}
392394

393395
void Renderer::doVisitRenderQueue(const std::vector<RenderCommand*>& renderCommands)
@@ -455,29 +457,17 @@ void Renderer::clean()
455457
void Renderer::setDepthTest(bool value)
456458
{
457459
if (value)
458-
{
459-
_currentRT->addFlag(TargetBufferFlags::DEPTH);
460460
_dsDesc.addFlag(DepthStencilFlags::DEPTH_TEST);
461-
}
462461
else
463-
{
464-
_currentRT->removeFlag(TargetBufferFlags::DEPTH);
465462
_dsDesc.removeFlag(DepthStencilFlags::DEPTH_TEST);
466-
}
467463
}
468464

469465
void Renderer::setStencilTest(bool value)
470466
{
471467
if (value)
472-
{
473-
_currentRT->addFlag(TargetBufferFlags::STENCIL);
474468
_dsDesc.addFlag(DepthStencilFlags::STENCIL_TEST);
475-
}
476469
else
477-
{
478-
_currentRT->removeFlag(TargetBufferFlags::STENCIL);
479470
_dsDesc.removeFlag(DepthStencilFlags::STENCIL_TEST);
480-
}
481471
}
482472

483473
void Renderer::setDepthWrite(bool value)
@@ -849,7 +839,18 @@ void Renderer::readPixels(backend::RenderTarget* rt,
849839
void Renderer::beginRenderPass()
850840
{
851841
_commandBuffer->beginRenderPass(_currentRT, _renderPassDesc);
852-
_commandBuffer->updateDepthStencilState(_dsDesc);
842+
843+
// Disable depth/stencil access if render target has no relevant attachments.
844+
auto depthStencil = _dsDesc;
845+
if (!_currentRT->isDefaultRenderTarget())
846+
{
847+
if (!_currentRT->_depth)
848+
depthStencil.removeFlag(DepthStencilFlags::DEPTH_TEST | DepthStencilFlags::DEPTH_WRITE);
849+
if (!_currentRT->_stencil)
850+
depthStencil.removeFlag(DepthStencilFlags::STENCIL_TEST);
851+
}
852+
853+
_commandBuffer->updateDepthStencilState(depthStencil);
853854
_commandBuffer->setStencilReferenceValue(_stencilRef);
854855

855856
_commandBuffer->setViewport(_viewport.x, _viewport.y, _viewport.width, _viewport.height);
@@ -929,11 +930,6 @@ ClearFlag Renderer::getClearFlag() const
929930
return _clearFlag;
930931
}
931932

932-
RenderTargetFlag Renderer::getRenderTargetFlag() const
933-
{
934-
return _currentRT->getTargetFlags();
935-
}
936-
937933
void Renderer::setScissorTest(bool enabled)
938934
{
939935
_scissorState.isEnabled = enabled;

core/renderer/Renderer.h

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Copyright (c) 2013-2016 Chukong Technologies Inc.
33
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
44
Copyright (c) 2019-present Axmol Engine contributors (see AUTHORS.md).
5-
5+
66
https://axmol.dev/
77
88
Permission is hereby granted, free of charge, to any person obtaining a copy
@@ -234,12 +234,6 @@ class AX_DLL Renderer
234234
*/
235235
ClearFlag getClearFlag() const;
236236

237-
/**
238-
* Get the render target flag.
239-
* @return The render target flag.
240-
*/
241-
RenderTargetFlag getRenderTargetFlag() const;
242-
243237
// depth/stencil state.
244238

245239
/**

core/renderer/backend/DepthStencilState.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ struct DepthStencilDescriptor
6262
StencilDescriptor frontFaceStencil;
6363
void addFlag(DepthStencilFlags flag) { this->flags |= flag; }
6464
void removeFlag(DepthStencilFlags flag) { this->flags &= ~flag; }
65-
// must match current render target
6665
DepthStencilFlags flags = DepthStencilFlags::ALL;
6766
};
6867

core/renderer/backend/DriverBase.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,9 @@ class AX_DLL DriverBase
110110
*/
111111
virtual TextureBackend* newTexture(const TextureDescriptor& descriptor) = 0;
112112

113-
virtual RenderTarget* newDefaultRenderTarget(TargetBufferFlags rtf) = 0;
113+
virtual RenderTarget* newDefaultRenderTarget() = 0;
114114

115-
virtual RenderTarget* newRenderTarget(TargetBufferFlags rtf,
116-
TextureBackend* colorAttachment = nullptr,
115+
virtual RenderTarget* newRenderTarget(TextureBackend* colorAttachment = nullptr,
117116
TextureBackend* depthAttachment = nullptr,
118117
TextureBackend* stencilAttachhment = nullptr) = 0;
119118

core/renderer/backend/RenderTarget.h

Lines changed: 35 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -28,67 +28,64 @@ class RenderTarget : public ax::Object
2828

2929
bool isDefaultRenderTarget() const { return _defaultRenderTarget; }
3030

31-
void addFlag(TargetBufferFlags flag) {
32-
setTargetFlags(_flags |= flag);
33-
}
34-
void removeFlag(TargetBufferFlags flag) {
35-
setTargetFlags(_flags & ~flag);
36-
}
37-
38-
TargetBufferFlags getTargetFlags() const { return _flags; }
39-
void setTargetFlags(TargetBufferFlags flags) {
40-
_flags = flags;
41-
}
42-
4331
void setColorAttachment(ColorAttachment attachment)
4432
{
45-
for (auto colorItem : _color)
33+
for (int i = 0; i < MAX_COLOR_ATTCHMENT; ++i)
34+
{
35+
auto colorItem = _color[i];
36+
if (colorItem.texture != attachment[i].texture || colorItem.level != attachment[i].level)
37+
_dirtyFlags |= getMRTColorFlag(i);
4638
AX_SAFE_RELEASE(colorItem.texture);
39+
}
4740
memcpy(_color, attachment, sizeof(ColorAttachment));
4841
for (auto colorItem : _color)
4942
AX_SAFE_RETAIN(colorItem.texture);
50-
51-
_dirty = true;
5243
};
5344

54-
void setColorAttachment(TextureBackend* attachment, int level = 0, int index = 0) {
55-
AX_SAFE_RELEASE(_color[index].texture);
56-
_color[index].texture = attachment;
57-
_color[index].level = level;
58-
AX_SAFE_RETAIN(_color[index].texture);
59-
_dirty = true;
45+
void setColorAttachment(TextureBackend* attachment, int level = 0, int index = 0)
46+
{
47+
if (_color[index].texture != attachment || _color[index].level != level)
48+
{
49+
_dirtyFlags |= getMRTColorFlag(index);
50+
AX_SAFE_RELEASE(_color[index].texture);
51+
_color[index].texture = attachment;
52+
_color[index].level = level;
53+
AX_SAFE_RETAIN(_color[index].texture);
54+
}
6055
}
6156

6257
void setDepthAttachment(TextureBackend* attachment, int level = 0)
6358
{
64-
AX_SAFE_RELEASE(_depth.texture);
65-
_depth.texture = attachment;
66-
_depth.level = level;
67-
AX_SAFE_RETAIN(_depth.texture);
68-
69-
_dirty = true;
59+
if (_depth.texture != attachment || _depth.level != level)
60+
{
61+
_dirtyFlags |= TargetBufferFlags::DEPTH;
62+
AX_SAFE_RELEASE(_depth.texture);
63+
_depth.texture = attachment;
64+
_depth.level = level;
65+
AX_SAFE_RETAIN(_depth.texture);
66+
}
7067
};
7168
void setStencilAttachment(TextureBackend* attachment, int level = 0)
7269
{
73-
AX_SAFE_RELEASE(_stencil.texture);
74-
_stencil.texture = attachment;
75-
_stencil.level = level;
76-
AX_SAFE_RETAIN(_stencil.texture);
77-
78-
_dirty = true;
70+
if (_stencil.texture != attachment || _depth.level != level)
71+
{
72+
_dirtyFlags |= TargetBufferFlags::STENCIL;
73+
AX_SAFE_RELEASE(_stencil.texture);
74+
_stencil.texture = attachment;
75+
_stencil.level = level;
76+
AX_SAFE_RETAIN(_stencil.texture);
77+
}
7978
};
80-
81-
bool isDirty() const { return _dirty; }
79+
80+
bool isDirty() const { return !!_dirtyFlags; }
8281

8382
ColorAttachment _color{};
8483
RenderBuffer _depth{};
8584
RenderBuffer _stencil{};
86-
TargetBufferFlags _flags{};
8785

8886
protected:
8987
bool _defaultRenderTarget = false;
90-
mutable bool _dirty = false;
91-
// uint8_t samples = 1;
88+
mutable TargetBufferFlags _dirtyFlags{};
9289
};
9390

9491
NS_AX_BACKEND_END

core/renderer/backend/Types.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ inline TargetBufferFlags getMRTColorFlag(size_t index) noexcept
4545
}
4646

4747
typedef TargetBufferFlags ClearFlag;
48-
typedef TargetBufferFlags RenderTargetFlag;
4948

5049
struct SamplerDescriptor
5150
{

0 commit comments

Comments
 (0)