@@ -168,8 +168,6 @@ Renderer::Renderer()
168
168
169
169
// for the batched TriangleCommand
170
170
_triBatchesToDraw = (TriBatchToDraw*) malloc (sizeof (_triBatchesToDraw[0 ]) * _triBatchesToDrawCapacity);
171
-
172
- _renderPipelineCache.reserve (100 );
173
171
}
174
172
175
173
Renderer::~Renderer ()
@@ -180,12 +178,7 @@ Renderer::~Renderer()
180
178
free (_triBatchesToDraw);
181
179
182
180
CC_SAFE_RELEASE (_commandBuffer);
183
-
184
- for (auto pipeline :_renderPipelineCache)
185
- {
186
- pipeline.second ->release ();
187
- }
188
- _renderPipelineCache.clear ();
181
+ CC_SAFE_RELEASE (_renderPipeline);
189
182
}
190
183
191
184
void Renderer::init ()
@@ -197,6 +190,8 @@ void Renderer::init()
197
190
198
191
auto device = backend::Device::getInstance ();
199
192
_commandBuffer = device->newCommandBuffer ();
193
+ _renderPipeline = device->newRenderPipeline ();
194
+ _commandBuffer->setRenderPipeline (_renderPipeline);
200
195
}
201
196
202
197
void Renderer::addCommand (RenderCommand* command)
@@ -752,113 +747,20 @@ bool Renderer::checkVisibility(const Mat4 &transform, const Size &size)
752
747
return ret;
753
748
}
754
749
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
-
819
750
void Renderer::setRenderPipeline (const PipelineDescriptor& pipelineDescriptor, const backend::RenderPassDescriptor& renderPassDescriptor)
820
751
{
821
- backend::RenderPipelineDescriptor renderPipelineDescriptor;
822
- renderPipelineDescriptor.programState = pipelineDescriptor.programState ;
823
-
824
752
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);
835
754
backend::DepthStencilState* depthStencilState = nullptr ;
836
755
auto needDepthStencilAttachment = renderPassDescriptor.depthTestEnabled || renderPassDescriptor.stencilTestEnabled ;
837
756
if (needDepthStencilAttachment)
838
757
{
839
758
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
- }
858
759
}
859
-
860
- _commandBuffer->setRenderPipeline (getRenderPipeline (renderPipelineDescriptor, pipelineDescriptor.blendDescriptor ));
861
760
_commandBuffer->setDepthStencilState (depthStencilState);
761
+ #ifdef CC_USE_METAL
762
+ _commandBuffer->setRenderPipeline (_renderPipeline);
763
+ #endif
862
764
}
863
765
864
766
void Renderer::beginRenderPass (RenderCommand* cmd)
0 commit comments