add a flag to determine if need depth/stencil attachment

This commit is contained in:
coulsonwang 2019-04-02 10:05:53 +08:00 committed by minggo
parent 147c564492
commit bb60c173a1
3 changed files with 19 additions and 12 deletions

View File

@ -408,12 +408,13 @@ void Renderer::setDepthTest(bool value)
{
_depthStencilDescriptor.depthTestEnabled = value;
_renderPassDescriptor.depthTestEnabled = value;
_renderPassDescriptor.needDepthAttachment = value || _depthStencilDescriptor.depthWriteEnabled;
}
void Renderer::setDepthWrite(bool value)
{
_depthStencilDescriptor.depthWriteEnabled = value;
// _renderPassDescriptor.depthTestEnabled = value;
_renderPassDescriptor.needDepthAttachment = _depthStencilDescriptor.depthTestEnabled || value;
}
void Renderer::setDepthCompareFunction(backend::CompareFunction func)
@ -440,6 +441,7 @@ void Renderer::setStencilTest(bool value)
{
_depthStencilDescriptor.stencilTestEnabled = value;
_renderPassDescriptor.stencilTestEnabled = value;
_renderPassDescriptor.needStencilAttachment = value;
}
void Renderer::setStencilCompareFunction(backend::CompareFunction func, unsigned int ref, unsigned int readMask)
@ -821,16 +823,7 @@ void Renderer::setRenderPipeline(const PipelineDescriptor& pipelineDescriptor, c
auto device = backend::Device::getInstance();
auto blendState = device->createBlendState(pipelineDescriptor.blendDescriptor);
renderPipelineDescriptor.blendState = blendState;
// bool needDepthStencilAttachment = _depthStencilDescriptor.depthTestEnabled ||
// _depthStencilDescriptor.depthWriteEnabled ||
// _depthStencilDescriptor.stencilTestEnabled;
backend::DepthStencilState* depthStencilState = nullptr;
// if (needDepthStencilAttachment)
{
depthStencilState = device->createDepthStencilState(_depthStencilDescriptor);
}
if (renderPassDescriptor.needColorAttachment)
{
// FIXME: now just handle color attachment 0.
@ -838,8 +831,12 @@ void Renderer::setRenderPipeline(const PipelineDescriptor& pipelineDescriptor, c
renderPipelineDescriptor.colorAttachmentsFormat[0] = renderPassDescriptor.colorAttachmentsTexture[0]->getTextureFormat();
}
// if (needDepthStencilAttachment)
backend::DepthStencilState* depthStencilState = nullptr;
auto needDepthStencilAttachment = renderPassDescriptor.needDepthAttachment || renderPassDescriptor.needStencilAttachment;
if (needDepthStencilAttachment)
{
depthStencilState = device->createDepthStencilState(_depthStencilDescriptor);
if(renderPassDescriptor.depthAttachmentTexture)
{
renderPipelineDescriptor.depthAttachmentFormat = renderPassDescriptor.depthAttachmentTexture->getTextureFormat();
@ -848,6 +845,7 @@ void Renderer::setRenderPipeline(const PipelineDescriptor& pipelineDescriptor, c
{
renderPipelineDescriptor.depthAttachmentFormat = TextureFormat::D24S8;
}
if (renderPassDescriptor.stencilAttachmentTexture)
{
renderPipelineDescriptor.stencilAttachmentFormat = renderPassDescriptor.stencilAttachmentTexture->getTextureFormat();
@ -901,6 +899,7 @@ void Renderer::setRenderTarget(RenderTargetFlag flags, Texture2D* colorAttachmen
if (flags & RenderTargetFlag::DEPTH)
{
_renderPassDescriptor.depthTestEnabled = true;
_renderPassDescriptor.needDepthAttachment = true;
if (depthAttachment)
_renderPassDescriptor.depthAttachmentTexture = depthAttachment->getBackendTexture();
else
@ -911,6 +910,7 @@ void Renderer::setRenderTarget(RenderTargetFlag flags, Texture2D* colorAttachmen
else
{
_renderPassDescriptor.depthTestEnabled = false;
_renderPassDescriptor.needDepthAttachment = false;
_renderPassDescriptor.depthAttachmentTexture = nullptr;
_depthAttachment = nullptr;
}
@ -919,6 +919,7 @@ void Renderer::setRenderTarget(RenderTargetFlag flags, Texture2D* colorAttachmen
{
_stencilAttachment = stencilAttachment;
_renderPassDescriptor.stencilTestEnabled = true;
_renderPassDescriptor.needStencilAttachment = true;
if (_stencilAttachment)
_renderPassDescriptor.stencilAttachmentTexture = stencilAttachment->getBackendTexture();
else
@ -928,6 +929,7 @@ void Renderer::setRenderTarget(RenderTargetFlag flags, Texture2D* colorAttachmen
{
_stencilAttachment = nullptr;
_renderPassDescriptor.stencilTestEnabled = false;
_renderPassDescriptor.needStencilAttachment = false;
_renderPassDescriptor.stencilAttachmentTexture = nullptr;
}
}
@ -954,6 +956,7 @@ void Renderer::clear(ClearFlag flags, const Color4F& color, float depth, unsigne
descriptor.clearDepthValue = depth;
descriptor.needClearDepth = true;
descriptor.depthTestEnabled = true;
descriptor.needDepthAttachment = true;
descriptor.depthAttachmentTexture = _renderPassDescriptor.depthAttachmentTexture;
}
if (flags & ClearFlag::STENCIL)
@ -961,6 +964,7 @@ void Renderer::clear(ClearFlag flags, const Color4F& color, float depth, unsigne
descriptor.clearStencilValue = stencil;
descriptor.needClearStencil = true;
descriptor.stencilTestEnabled = true;
descriptor.needStencilAttachment = true;
descriptor.stencilAttachmentTexture = _renderPassDescriptor.stencilAttachmentTexture;
}

View File

@ -19,6 +19,8 @@ struct RenderPassDescriptor
float clearStencilValue = 0.f;
std::array<float, 4> clearColorValue {{0.f, 0.f, 0.f, 0.f}}; // double-braces required in C++11
bool needColorAttachment = true;
bool needDepthAttachment = false;
bool needStencilAttachment = false;
bool depthTestEnabled = false;
bool stencilTestEnabled = false;
bool needClearColor = false;

View File

@ -113,6 +113,7 @@ namespace
}
// if(descriptor.depthTestEnabled || descriptor.stencilTestEnabled)
if(descriptor.needDepthAttachment || descriptor.needStencilAttachment)
{
// Set depth attachment
{