Skip to content

Commit 1e8f6d2

Browse files
Mee-guminggo
authored andcommitted
fix render pipeline (#20041)
1 parent 3c08471 commit 1e8f6d2

28 files changed

+299
-647
lines changed

cocos/renderer/CCPipelineDescriptor.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525

2626
#include "platform/CCPlatformMacros.h"
2727
#include "renderer/backend/DepthStencilState.h"
28-
#include "renderer/backend/BlendState.h"
2928
#include "renderer/backend/Texture.h"
3029
#include "renderer/backend/VertexLayout.h"
3130
#include "renderer/backend/RenderPassDescriptor.h"

cocos/renderer/CCRenderer.cpp

Lines changed: 7 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,6 @@ Renderer::Renderer()
168168

169169
// for the batched TriangleCommand
170170
_triBatchesToDraw = (TriBatchToDraw*) malloc(sizeof(_triBatchesToDraw[0]) * _triBatchesToDrawCapacity);
171-
172-
_renderPipelineCache.reserve(100);
173171
}
174172

175173
Renderer::~Renderer()
@@ -180,12 +178,7 @@ Renderer::~Renderer()
180178
free(_triBatchesToDraw);
181179

182180
CC_SAFE_RELEASE(_commandBuffer);
183-
184-
for (auto pipeline :_renderPipelineCache)
185-
{
186-
pipeline.second->release();
187-
}
188-
_renderPipelineCache.clear();
181+
CC_SAFE_RELEASE(_renderPipeline);
189182
}
190183

191184
void Renderer::init()
@@ -197,6 +190,8 @@ void Renderer::init()
197190

198191
auto device = backend::Device::getInstance();
199192
_commandBuffer = device->newCommandBuffer();
193+
_renderPipeline = device->newRenderPipeline();
194+
_commandBuffer->setRenderPipeline(_renderPipeline);
200195
}
201196

202197
void Renderer::addCommand(RenderCommand* command)
@@ -752,113 +747,20 @@ bool Renderer::checkVisibility(const Mat4 &transform, const Size &size)
752747
return ret;
753748
}
754749

755-
backend::RenderPipeline* Renderer::getRenderPipeline(const backend::RenderPipelineDescriptor& renderPipelineDescriptor, const backend::BlendDescriptor blendDescriptor)
756-
{
757-
struct
758-
{
759-
void* program;
760-
unsigned int vertexLayoutInfo[32];
761-
backend::PixelFormat colorAttachment;
762-
backend::PixelFormat depthAttachment;
763-
backend::PixelFormat stencilAttachment;
764-
bool blendEnabled;
765-
unsigned int writeMask;
766-
unsigned int rgbBlendOperation;
767-
unsigned int alphaBlendOperation;
768-
unsigned int sourceRGBBlendFactor;
769-
unsigned int destinationRGBBlendFactor;
770-
unsigned int sourceAlphaBlendFactor;
771-
unsigned int destinationAlphaBlendFactor;
772-
}hashMe;
773-
774-
memset(&hashMe, 0, sizeof(hashMe));
775-
hashMe.program = renderPipelineDescriptor.programState->getProgram();
776-
hashMe.colorAttachment = renderPipelineDescriptor.colorAttachmentsFormat[0];
777-
hashMe.depthAttachment = renderPipelineDescriptor.depthAttachmentFormat;
778-
hashMe.stencilAttachment = renderPipelineDescriptor.stencilAttachmentFormat;
779-
hashMe.blendEnabled = blendDescriptor.blendEnabled;
780-
hashMe.writeMask = (unsigned int)blendDescriptor.writeMask;
781-
hashMe.rgbBlendOperation = (unsigned int)blendDescriptor.rgbBlendOperation;
782-
hashMe.alphaBlendOperation = (unsigned int)blendDescriptor.alphaBlendOperation;
783-
hashMe.sourceRGBBlendFactor = (unsigned int)blendDescriptor.sourceRGBBlendFactor;
784-
hashMe.destinationRGBBlendFactor = (unsigned int)blendDescriptor.destinationRGBBlendFactor;
785-
hashMe.sourceAlphaBlendFactor = (unsigned int)blendDescriptor.sourceAlphaBlendFactor;
786-
hashMe.destinationAlphaBlendFactor = (unsigned int)blendDescriptor.destinationAlphaBlendFactor;
787-
int index = 0;
788-
auto vertexLayout = renderPipelineDescriptor.programState->getVertexLayout();
789-
const auto& attributes = vertexLayout->getAttributes();
790-
for (const auto& it : attributes)
791-
{
792-
auto &attribute = it.second;
793-
/*
794-
stepFunction:1 stride:15 offest:10 format:5 needNormalized:1
795-
bit31 bit30 ~ bit16 bit15 ~ bit6 bit5 ~ bit1 bit0
796-
*/
797-
hashMe.vertexLayoutInfo[index++] =
798-
((unsigned int)vertexLayout->getVertexStepMode() & 0x1) << 31 |
799-
((unsigned int)(vertexLayout->getStride() & 0x7FFF)) << 16 |
800-
((unsigned int)attribute.offset & 0x3FF) << 6 |
801-
((unsigned int)attribute.format & 0x1F) << 1 |
802-
((unsigned int)attribute.needToBeNormallized & 0x1);
803-
}
804-
805-
unsigned int hash = XXH32((const void*)&hashMe, sizeof(hashMe), 0);
806-
auto iter = _renderPipelineCache.find(hash);
807-
if (_renderPipelineCache.end() == iter)
808-
{
809-
auto renderPipeline = backend::Device::getInstance()->newRenderPipeline(renderPipelineDescriptor);
810-
_renderPipelineCache.emplace(hash, renderPipeline);
811-
return renderPipeline;
812-
}
813-
else
814-
{
815-
return iter->second;
816-
}
817-
}
818-
819750
void Renderer::setRenderPipeline(const PipelineDescriptor& pipelineDescriptor, const backend::RenderPassDescriptor& renderPassDescriptor)
820751
{
821-
backend::RenderPipelineDescriptor renderPipelineDescriptor;
822-
renderPipelineDescriptor.programState = pipelineDescriptor.programState;
823-
824752
auto device = backend::Device::getInstance();
825-
auto blendState = device->createBlendState(pipelineDescriptor.blendDescriptor);
826-
renderPipelineDescriptor.blendState = blendState;
827-
828-
if (renderPassDescriptor.needColorAttachment)
829-
{
830-
// FIXME: now just handle color attachment 0.
831-
if (renderPassDescriptor.colorAttachmentsTexture[0])
832-
renderPipelineDescriptor.colorAttachmentsFormat[0] = renderPassDescriptor.colorAttachmentsTexture[0]->getTextureFormat();
833-
}
834-
753+
_renderPipeline->update(pipelineDescriptor, renderPassDescriptor);
835754
backend::DepthStencilState* depthStencilState = nullptr;
836755
auto needDepthStencilAttachment = renderPassDescriptor.depthTestEnabled || renderPassDescriptor.stencilTestEnabled;
837756
if (needDepthStencilAttachment)
838757
{
839758
depthStencilState = device->createDepthStencilState(_depthStencilDescriptor);
840-
841-
if(renderPassDescriptor.depthAttachmentTexture)
842-
{
843-
renderPipelineDescriptor.depthAttachmentFormat = renderPassDescriptor.depthAttachmentTexture->getTextureFormat();
844-
}
845-
else
846-
{
847-
renderPipelineDescriptor.depthAttachmentFormat = PixelFormat::D24S8;
848-
}
849-
850-
if (renderPassDescriptor.stencilAttachmentTexture)
851-
{
852-
renderPipelineDescriptor.stencilAttachmentFormat = renderPassDescriptor.stencilAttachmentTexture->getTextureFormat();
853-
}
854-
else
855-
{
856-
renderPipelineDescriptor.stencilAttachmentFormat = PixelFormat::D24S8;
857-
}
858759
}
859-
860-
_commandBuffer->setRenderPipeline(getRenderPipeline(renderPipelineDescriptor, pipelineDescriptor.blendDescriptor));
861760
_commandBuffer->setDepthStencilState(depthStencilState);
761+
#ifdef CC_USE_METAL
762+
_commandBuffer->setRenderPipeline(_renderPipeline);
763+
#endif
862764
}
863765

864766
void Renderer::beginRenderPass(RenderCommand* cmd)

cocos/renderer/CCRenderer.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -480,13 +480,11 @@ class CC_DLL Renderer
480480
*/
481481
void setRenderPipeline(const PipelineDescriptor&, const backend::RenderPassDescriptor&);
482482

483-
backend::RenderPipeline* getRenderPipeline(const backend::RenderPipelineDescriptor& renderPipelineDescriptor, const backend::BlendDescriptor blendDescriptor);
484-
485483
void pushStateBlock();
486484

487485
void popStateBlock();
488486

489-
std::unordered_map<unsigned int, backend::RenderPipeline*> _renderPipelineCache;
487+
backend::RenderPipeline* _renderPipeline = nullptr;
490488

491489
Viewport _viewport;
492490
CullMode _cullMode = CullMode::NONE;

cocos/renderer/CMakeLists.txt

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ set(COCOS_RENDERER_HEADER
2121
renderer/ccShaders.h
2222

2323
renderer/backend/Backend.h
24-
renderer/backend/BlendState.h
2524
renderer/backend/Buffer.h
2625
renderer/backend/CommandBuffer.h
2726
renderer/backend/DepthStencilState.h
@@ -61,7 +60,6 @@ set(COCOS_RENDERER_SRC
6160
renderer/CCTrianglesCommand.cpp
6261
renderer/ccShaders.cpp
6362

64-
renderer/backend/BlendState.cpp
6563
renderer/backend/CommandBuffer.cpp
6664
renderer/backend/DepthStencilState.cpp
6765
renderer/backend/Device.cpp
@@ -92,7 +90,6 @@ list(APPEND COCOS_RENDERER_HEADER
9290
)
9391

9492
list(APPEND COCOS_RENDERER_SRC
95-
renderer/backend/opengl/BlendStateGL.cpp
9693
renderer/backend/opengl/BufferGL.cpp
9794
renderer/backend/opengl/CommandBufferGL.cpp
9895
renderer/backend/opengl/DepthStencilStateGL.cpp
@@ -108,7 +105,6 @@ list(APPEND COCOS_RENDERER_SRC
108105
else()
109106

110107
list(APPEND COCOS_RENDERER_HEADER
111-
renderer/backend/metal/BlendStateMTL.h
112108
renderer/backend/metal/BufferMTL.h
113109
renderer/backend/metal/BufferManager.h
114110
renderer/backend/metal/CommandBufferMTL.h
@@ -123,7 +119,6 @@ list(APPEND COCOS_RENDERER_HEADER
123119
)
124120

125121
list(APPEND COCOS_RENDERER_SRC
126-
renderer/backend/metal/BlendStateMTL.mm
127122
renderer/backend/metal/BufferMTL.mm
128123
renderer/backend/metal/BufferManager.mm
129124
renderer/backend/metal/CommandBufferMTL.mm

cocos/renderer/backend/Backend.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,3 @@
3434
#include "renderer/backend/VertexLayout.h"
3535
#include "renderer/backend/Texture.h"
3636
#include "renderer/backend/DepthStencilState.h"
37-
#include "renderer/backend/BlendState.h"

cocos/renderer/backend/BlendState.cpp

Lines changed: 0 additions & 29 deletions
This file was deleted.

cocos/renderer/backend/BlendState.h

Lines changed: 0 additions & 68 deletions
This file was deleted.

cocos/renderer/backend/Device.h

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
#include "RenderPassDescriptor.h"
3131
#include "Texture.h"
3232
#include "DepthStencilState.h"
33-
#include "BlendState.h"
3433
#include "ProgramCache.h"
3534
#include "ShaderCache.h"
3635
#include "DeviceInfo.h"
@@ -97,19 +96,12 @@ class Device : public cocos2d::Ref
9796
*/
9897
virtual DepthStencilState* createDepthStencilState(const DepthStencilDescriptor& descriptor) = 0;
9998

100-
/**
101-
* Create an auto released BlendState object.
102-
* @param descriptor Specifies blend description.
103-
* @return An auto release BlendState object.
104-
*/
105-
virtual BlendState* createBlendState(const BlendDescriptor& descriptor) = 0;
106-
10799
/**
108100
* New a RenderPipeline object, not auto released.
109101
* @param descriptor Specifies render pipeline description.
110102
* @return A RenderPipeline object.
111103
*/
112-
virtual RenderPipeline* newRenderPipeline(const RenderPipelineDescriptor& descriptor) = 0;
104+
virtual RenderPipeline* newRenderPipeline() = 0;
113105

114106
/**
115107
* This property controls whether or not the drawables'

cocos/renderer/backend/ProgramState.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
CC_BACKEND_BEGIN
4040

4141
class TextureBackend;
42+
class VertexLayout;
4243

4344
/**
4445
* @addtogroup _backend

cocos/renderer/backend/RenderPassDescriptor.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ struct RenderPassDescriptor
4545
{
4646
RenderPassDescriptor& operator=(const RenderPassDescriptor& descriptor);
4747
bool operator==(const RenderPassDescriptor& descriptor) const;
48+
bool needDepthStencilAttachment() const { return depthTestEnabled || stencilTestEnabled; }
4849

4950
float clearDepthValue = 0.f;
5051
float clearStencilValue = 0.f;

0 commit comments

Comments
 (0)