[Metal] matching depth stencil state

This commit is contained in:
halx99 2020-11-06 08:56:25 +08:00
parent 8e35201726
commit a2a7166808
8 changed files with 30 additions and 34 deletions

View File

@ -199,7 +199,6 @@ void Renderer::init()
auto device = backend::Device::getInstance(); auto device = backend::Device::getInstance();
_commandBuffer = device->newCommandBuffer(); _commandBuffer = device->newCommandBuffer();
// @MTL: the depth stencil flags must same render target and _depthStencilDescriptor // @MTL: the depth stencil flags must same render target and _depthStencilDescriptor
// TODO: can use one?
_depthStencilDescriptor.depthStencilFlags = TargetBufferFlags::DEPTH_AND_STENCIL; _depthStencilDescriptor.depthStencilFlags = TargetBufferFlags::DEPTH_AND_STENCIL;
_defaultRT = device->newDefaultRenderTarget(TargetBufferFlags::COLOR | _depthStencilDescriptor.depthStencilFlags); _defaultRT = device->newDefaultRenderTarget(TargetBufferFlags::COLOR | _depthStencilDescriptor.depthStencilFlags);
@ -352,7 +351,6 @@ void Renderer::visitRenderQueue(RenderQueue& queue)
//Process Global-Z > 0 Queue //Process Global-Z > 0 Queue
// //
doVisitRenderQueue(queue.getSubQueue(RenderQueue::QUEUE_GROUP::GLOBALZ_POS)); doVisitRenderQueue(queue.getSubQueue(RenderQueue::QUEUE_GROUP::GLOBALZ_POS));
} }
void Renderer::doVisitRenderQueue(const std::vector<RenderCommand*>& renderCommands) void Renderer::doVisitRenderQueue(const std::vector<RenderCommand*>& renderCommands)

View File

@ -40,6 +40,10 @@ bool StencilDescriptor::operator==(const StencilDescriptor &rhs) const
DepthStencilState::~DepthStencilState() DepthStencilState::~DepthStencilState()
{} {}
bool DepthStencilState::isEnabled() const {
return bitmask::any(_depthStencilInfo.depthStencilFlags, TargetBufferFlags::DEPTH_AND_STENCIL);
}
void DepthStencilState::update(const DepthStencilDescriptor& descriptor) { void DepthStencilState::update(const DepthStencilDescriptor& descriptor) {
_depthStencilInfo = descriptor; _depthStencilInfo = descriptor;
_isBackFrontStencilEqual = descriptor.backFaceStencil == descriptor.frontFaceStencil; _isBackFrontStencilEqual = descriptor.backFaceStencil == descriptor.frontFaceStencil;

View File

@ -74,7 +74,7 @@ class DepthStencilState : public cocos2d::Ref
public: public:
virtual void update(const DepthStencilDescriptor& descriptor); virtual void update(const DepthStencilDescriptor& descriptor);
const DepthStencilDescriptor& getDepthStencilInfo()const { return _depthStencilInfo; } const DepthStencilDescriptor& getDepthStencilInfo()const { return _depthStencilInfo; }
bool isEnabled() const { return bitmask::any(_depthStencilInfo.depthStencilFlags, TargetBufferFlags::DEPTH_AND_STENCIL); } bool isEnabled() const;
protected: protected:
/** /**
* @param descriptor Specifies depth and stencil descriptor. * @param descriptor Specifies depth and stencil descriptor.

View File

@ -226,6 +226,7 @@ private:
dispatch_semaphore_t _frameBoundarySemaphore; dispatch_semaphore_t _frameBoundarySemaphore;
const RenderTarget* _currentRenderTarget = nil; // weak ref const RenderTarget* _currentRenderTarget = nil; // weak ref
RenderPassParams _currentRenderPassParams; RenderPassParams _currentRenderPassParams;
TargetBufferFlags _currentRenderTargetFlags = TargetBufferFlags::NONE;
NSAutoreleasePool* _autoReleasePool = nil; NSAutoreleasePool* _autoReleasePool = nil;
std::vector<std::pair<TextureBackend*,std::function<void(const PixelBufferDescriptor&)>>> _captureCallbacks; std::vector<std::pair<TextureBackend*,std::function<void(const PixelBufferDescriptor&)>>> _captureCallbacks;

View File

@ -200,15 +200,17 @@ void CommandBufferMTL::beginFrame()
id<MTLRenderCommandEncoder> CommandBufferMTL::getRenderCommandEncoder(const RenderTarget* renderTarget, const RenderPassParams& renderPassParams) id<MTLRenderCommandEncoder> CommandBufferMTL::getRenderCommandEncoder(const RenderTarget* renderTarget, const RenderPassParams& renderPassParams)
{ {
if(_mtlRenderEncoder != nil && _currentRenderPassParams == renderPassParams && _currentRenderTarget == renderTarget) if(_mtlRenderEncoder != nil &&
_currentRenderPassParams == renderPassParams &&
_currentRenderTarget == renderTarget &&
_currentRenderTargetFlags == renderTarget->getTargetFlags())
{ {
return _mtlRenderEncoder; return _mtlRenderEncoder;
} }
else
{
_currentRenderTarget = renderTarget; _currentRenderTarget = renderTarget;
_currentRenderPassParams = renderPassParams; _currentRenderPassParams = renderPassParams;
} _currentRenderTargetFlags = renderTarget->getTargetFlags();
if(_mtlRenderEncoder != nil) if(_mtlRenderEncoder != nil)
{ {

View File

@ -62,7 +62,7 @@ private:
void setBlendState(MTLRenderPipelineColorAttachmentDescriptor*, const BlendDescriptor&); void setBlendState(MTLRenderPipelineColorAttachmentDescriptor*, const BlendDescriptor&);
void setShaderModules(const PipelineDescriptor&); void setShaderModules(const PipelineDescriptor&);
void setBlendStateAndFormat(const BlendDescriptor&); void setBlendStateAndFormat(const BlendDescriptor&);
void getAttachmentFormat(const RenderTarget* renderTarget, PixelFormat colorAttachmentsFormat[MAX_COLOR_ATTCHMENT], PixelFormat&, PixelFormat&); void chooseAttachmentFormat(const RenderTarget* renderTarget, PixelFormat colorAttachmentsFormat[MAX_COLOR_ATTCHMENT], PixelFormat&, PixelFormat&);
id<MTLRenderPipelineState> _mtlRenderPipelineState = nil; id<MTLRenderPipelineState> _mtlRenderPipelineState = nil;
id<MTLDevice> _mtlDevice = nil; id<MTLDevice> _mtlDevice = nil;

View File

@ -183,7 +183,7 @@ void RenderPipelineMTL::update(const RenderTarget* renderTarget, const PipelineD
memset(&hashMe, 0, sizeof(hashMe)); memset(&hashMe, 0, sizeof(hashMe));
const auto& blendDescriptor = pipelineDescirptor.blendDescriptor; const auto& blendDescriptor = pipelineDescirptor.blendDescriptor;
getAttachmentFormat(renderTarget, _colorAttachmentsFormat, _depthAttachmentFormat, _stencilAttachmentFormat); chooseAttachmentFormat(renderTarget, _colorAttachmentsFormat, _depthAttachmentFormat, _stencilAttachmentFormat);
auto program = static_cast<ProgramMTL*>(pipelineDescirptor.programState->getProgram()); auto program = static_cast<ProgramMTL*>(pipelineDescirptor.programState->getProgram());
hashMe.vertexShaderHash = program->getVertexShader()->getHashValue(); hashMe.vertexShaderHash = program->getVertexShader()->getHashValue();
hashMe.fragmentShaderHash = program->getFragmentShader()->getHashValue(); hashMe.fragmentShaderHash = program->getFragmentShader()->getHashValue();
@ -292,33 +292,21 @@ void RenderPipelineMTL::setShaderModules(const PipelineDescriptor& descriptor)
_mtlRenderPipelineDescriptor.fragmentFunction = fragShaderModule->getMTLFunction(); _mtlRenderPipelineDescriptor.fragmentFunction = fragShaderModule->getMTLFunction();
} }
void RenderPipelineMTL::getAttachmentFormat(const RenderTarget* renderTarget, void RenderPipelineMTL::chooseAttachmentFormat(const RenderTarget* renderTarget,
PixelFormat colorAttachmentsFormat[MAX_COLOR_ATTCHMENT], PixelFormat colorAttachmentsFormat[MAX_COLOR_ATTCHMENT],
PixelFormat& depthFormat, PixelFormat& depthFormat,
PixelFormat& stencilFormat) PixelFormat& stencilFormat)
{ {
// Sets color attachment format
auto rtMTL = static_cast<const RenderTargetMTL*>(renderTarget); auto rtMTL = static_cast<const RenderTargetMTL*>(renderTarget);
auto rtflags = rtMTL->getTargetFlags(); auto rtflags = rtMTL->getTargetFlags();
for(auto i = 0; i < MAX_COLOR_ATTCHMENT; ++i) { for(auto i = 0; i < MAX_COLOR_ATTCHMENT; ++i) {
if (bitmask::any(rtflags, getMRTColorFlag(i))) colorAttachmentsFormat[i] = bitmask::any(rtflags, getMRTColorFlag(i)) ? rtMTL->getColorAttachmentPixelFormat(i) : PixelFormat::NONE;
{
colorAttachmentsFormat[i] = rtMTL->getColorAttachmentPixelFormat(i);
}
else
{
colorAttachmentsFormat[i] = PixelFormat::NONE;
}
} }
if (bitmask::any(rtflags, RenderTargetFlag::DEPTH_AND_STENCIL)) // Sets depth and stencil attachment format, match RenderTargetMTL::applyRenderPassAttachments
{ depthFormat = bitmask::any(rtflags, RenderTargetFlag::DEPTH) ? rtMTL->getDepthAttachmentPixelFormat() : PixelFormat::NONE;
depthFormat = rtMTL->getDepthAttachmentPixelFormat(); stencilFormat = bitmask::any(rtflags, RenderTargetFlag::STENCIL) ? rtMTL->getStencilAttachmentPixelFormat() : PixelFormat::NONE;
stencilFormat =rtMTL->getStencilAttachmentPixelFormat();
}
else
{
depthFormat = stencilFormat = PixelFormat::NONE;
}
} }
void RenderPipelineMTL::setBlendStateAndFormat(const BlendDescriptor& blendDescriptor) void RenderPipelineMTL::setBlendStateAndFormat(const BlendDescriptor& blendDescriptor)

View File

@ -96,7 +96,8 @@ void RenderTargetMTL::applyRenderPassAttachments(const RenderPassParams& params,
#endif #endif
} }
if(bitmask::any(this->_flags, RenderTargetFlag::DEPTH_AND_STENCIL)) { // Sets descriptor depth and stencil params, should match RenderTargetMTL::chooseAttachmentFormat
if(bitmask::any(this->_flags, RenderTargetFlag::DEPTH)) {
auto depthAttachment = getDepthAttachment(); auto depthAttachment = getDepthAttachment();
if(depthAttachment){ if(depthAttachment){
descriptor.depthAttachment.texture = depthAttachment.texture; descriptor.depthAttachment.texture = depthAttachment.texture;
@ -106,7 +107,9 @@ void RenderTargetMTL::applyRenderPassAttachments(const RenderPassParams& params,
descriptor.depthAttachment.storeAction = getStoreAction(params, TargetBufferFlags::DEPTH); descriptor.depthAttachment.storeAction = getStoreAction(params, TargetBufferFlags::DEPTH);
descriptor.depthAttachment.clearDepth = params.clearDepthValue; descriptor.depthAttachment.clearDepth = params.clearDepthValue;
} }
}
if(bitmask::any(this->_flags, RenderTargetFlag::STENCIL)) {
auto stencilAttachment = getStencilAttachment(); auto stencilAttachment = getStencilAttachment();
if(stencilAttachment) { if(stencilAttachment) {
descriptor.stencilAttachment.texture = stencilAttachment.texture; descriptor.stencilAttachment.texture = stencilAttachment.texture;
@ -174,7 +177,7 @@ PixelFormat RenderTargetMTL::getColorAttachmentPixelFormat(int index) const
PixelFormat RenderTargetMTL::getDepthAttachmentPixelFormat() const PixelFormat RenderTargetMTL::getDepthAttachmentPixelFormat() const
{ // FIXME: engine-x only support D24S8 { // FIXME: engine-x only support D24S8
if(bitmask::any(_flags, TargetBufferFlags::DEPTH_AND_STENCIL)) { if(bitmask::any(_flags, TargetBufferFlags::DEPTH)) {
if(isDefaultRenderTarget() || !_depth) if(isDefaultRenderTarget() || !_depth)
return PixelFormat::D24S8; return PixelFormat::D24S8;
return _depth.texture->getTextureFormat(); return _depth.texture->getTextureFormat();
@ -184,7 +187,7 @@ PixelFormat RenderTargetMTL::getDepthAttachmentPixelFormat() const
PixelFormat RenderTargetMTL::getStencilAttachmentPixelFormat() const PixelFormat RenderTargetMTL::getStencilAttachmentPixelFormat() const
{ // FIXME: engine-x only support D24S8 { // FIXME: engine-x only support D24S8
if(bitmask::any(_flags, TargetBufferFlags::DEPTH_AND_STENCIL)) { if(bitmask::any(_flags, TargetBufferFlags::STENCIL)) {
if(isDefaultRenderTarget() || !_stencil) if(isDefaultRenderTarget() || !_stencil)
return PixelFormat::D24S8; return PixelFormat::D24S8;
return _stencil.texture->getTextureFormat(); return _stencil.texture->getTextureFormat();