diff --git a/src/backend/BlendState.cpp b/src/backend/BlendState.cpp index f4be052..69ded1f 100644 --- a/src/backend/BlendState.cpp +++ b/src/backend/BlendState.cpp @@ -1,5 +1,8 @@ #include "BlendState.h" CC_BACKEND_BEGIN - +BlendState::BlendState(const BlendDescriptor& descriptor) +: _blendDescriptor(descriptor) +{ +} CC_BACKEND_END diff --git a/src/backend/BlendState.h b/src/backend/BlendState.h index 420e598..61110fa 100644 --- a/src/backend/BlendState.h +++ b/src/backend/BlendState.h @@ -25,6 +25,9 @@ class BlendState : public cocos2d::Ref { protected: virtual ~BlendState() = default; + BlendState(const BlendDescriptor& descriptor); + + BlendDescriptor _blendDescriptor; }; CC_BACKEND_END diff --git a/src/backend/RenderPass.cpp b/src/backend/RenderPass.cpp index d28bd87..f6a1d6a 100644 --- a/src/backend/RenderPass.cpp +++ b/src/backend/RenderPass.cpp @@ -13,4 +13,20 @@ RenderPass::RenderPass(const RenderPassDescriptor& descriptor) RenderPass::~RenderPass() {} +bool RenderPass::Find(const RenderPassDescriptor& descriptor) const +{ + if(_colorAttachmentsSet != descriptor.getColorAttachmentSet() || + _depthStencilAttachmentSet != descriptor.getDepthStencilAttachmentSet()) + return false; + + const RenderPassColorAttachments colorAttachment = descriptor.getColorAttachments(); + const RenderPassDepthStencilAttachment depthStencilAttachment = descriptor.getDepthStencilAttachment(); + + if(_colorAttachments == colorAttachment && + _depthStencilAttachment == depthStencilAttachment) + return true; + + return false; +} + CC_BACKEND_END diff --git a/src/backend/RenderPass.h b/src/backend/RenderPass.h index b34d1d9..033bf07 100644 --- a/src/backend/RenderPass.h +++ b/src/backend/RenderPass.h @@ -14,6 +14,7 @@ class RenderPass : public cocos2d::Ref inline const RenderPassDepthStencilAttachment& getDepthStencilAttachment() const { return _depthStencilAttachment; } inline const RenderPassColorAttachments& getColorAttachments() const { return _colorAttachments; } + bool Find(const RenderPassDescriptor& descriptor) const; protected: RenderPass(const RenderPassDescriptor& descriptor); diff --git a/src/backend/RenderPassDescriptor.cpp b/src/backend/RenderPassDescriptor.cpp index 1fd3d8b..7d98557 100644 --- a/src/backend/RenderPassDescriptor.cpp +++ b/src/backend/RenderPassDescriptor.cpp @@ -49,6 +49,15 @@ void RenderPassColorAttachments::retainTextures() const CC_SAFE_RETAIN(texture); } +bool RenderPassColorAttachments::operator==(const RenderPassColorAttachments& colorAttachment) const +{ + if(needClearColor != colorAttachment.needClearColor || + clearColor != colorAttachment.clearColor) + return false; + + return textures == colorAttachment.textures; +} + RenderPassDepthStencilAttachment::RenderPassDepthStencilAttachment(const RenderPassDepthStencilAttachment& rhs) { *this = rhs; @@ -75,6 +84,22 @@ RenderPassDepthStencilAttachment& RenderPassDepthStencilAttachment::operator=(co return *this; } +bool RenderPassDepthStencilAttachment::operator==(const RenderPassDepthStencilAttachment& renderAttachment) const +{ + if(needClearDepth != renderAttachment.needClearDepth || + clearDepth != renderAttachment.clearDepth || + needClearStencil != renderAttachment.needClearStencil || + clearStencil != renderAttachment.clearStencil) + return false; + + if(texture && renderAttachment.texture) + return texture->getTextureHashCode()?texture->getTextureHashCode() == renderAttachment.texture->getTextureHashCode():false; + else if(!texture && !(renderAttachment.texture)) + return true; + + return false; +} + void RenderPassDescriptor::setColorAttachment(uint32_t attachment, Texture* texture) { assert(TextureUsage::RENDER_TARGET == texture->getTextureUsage()); diff --git a/src/backend/RenderPassDescriptor.h b/src/backend/RenderPassDescriptor.h index caf226c..b088a51 100644 --- a/src/backend/RenderPassDescriptor.h +++ b/src/backend/RenderPassDescriptor.h @@ -18,6 +18,7 @@ struct RenderPassColorAttachments ~RenderPassColorAttachments(); RenderPassColorAttachments& operator=(const RenderPassColorAttachments& rhs); void setTexture(uint32_t attachment, Texture* texture); + bool operator==(const RenderPassColorAttachments& colorAttachment) const; std::vector textures; @@ -35,6 +36,7 @@ struct RenderPassDepthStencilAttachment RenderPassDepthStencilAttachment(const RenderPassDepthStencilAttachment& rhs); ~RenderPassDepthStencilAttachment(); RenderPassDepthStencilAttachment& operator =(const RenderPassDepthStencilAttachment& rhs); + bool operator==(const RenderPassDepthStencilAttachment& renderAttachment) const; float clearDepth = 1.f; bool needClearDepth = false; @@ -58,6 +60,8 @@ class RenderPassDescriptor inline const RenderPassDepthStencilAttachment& getDepthStencilAttachment() const { return _depthStencilAttachment; } inline const RenderPassColorAttachments& getColorAttachments() const { return _colorAttachments; } + inline bool getColorAttachmentSet() const {return _colorAttachmentsSet;} + inline bool getDepthStencilAttachmentSet() const {return _depthStencilAttachmentSet;} private: bool _colorAttachmentsSet = false; diff --git a/src/backend/StringUtils.cpp b/src/backend/StringUtils.cpp new file mode 100644 index 0000000..84e7b34 --- /dev/null +++ b/src/backend/StringUtils.cpp @@ -0,0 +1,200 @@ +// +// StringUtils.cpp +// Test +// +// Created by Cocos on 2018/11/2. +// Copyright © 2018 cocos. All rights reserved. +// + +#include "StringUtils.h" + +CC_BACKEND_BEGIN + +std::string StringUtils::TextureFormat2String(const TextureFormat& textureFormat) +{ + switch (textureFormat) { + case TextureFormat::R8G8B8A8: + return "R8G8B8A8"; + case TextureFormat::R8G8B8: + return "R8G8B8"; + case TextureFormat::A8: + return "A8"; + default: + return ""; + } +} + +std::string StringUtils::TextureType2String(const TextureType& textureType) +{ + switch (textureType) { + case TextureType::TEXTURE_2D: + return "TEXTURE_2D"; + case TextureType::TEXTURE_CUBE: + return "TEXTURE_CUBE"; + default: + return ""; + } +} + +std::string StringUtils::TextureUsage2String(const TextureUsage& textureUsage) +{ + switch (textureUsage) { + case TextureUsage::READ: + return "READ"; + case TextureUsage::WRITE: + return "WRITE"; + case TextureUsage::RENDER_TARGET: + return "RENDER_TARGET"; + default: + return ""; + } +} + +std::string StringUtils::SamplerFilterType2String(const SamplerFilter& filterType) +{ + switch (filterType) { + case SamplerFilter::LINEAR: + return "LINEAR"; + case SamplerFilter::NEAREST: + return "NEAREST"; + default: + return ""; + } +} + +std::string StringUtils::SamplerAddressMode2String(const SamplerAddressMode& addressMode) +{ + switch (addressMode) { + case SamplerAddressMode::REPEAT: + return "REPEAT"; + case SamplerAddressMode::MIRROR_REPEAT: + return "MIRROR_REPEAT"; + case SamplerAddressMode::CLAMP_TO_EDGE: + return "CLAMP_TO_EDGE"; + default: + return ""; + } +} + +std::string StringUtils::SamplerDescriptor2String(const SamplerDescriptor& descriptor) +{ + std::string samplerInfo = descriptor.mipmapEnabled ? "mipmapEnable":"mipmapDisable"; + samplerInfo += SamplerFilterType2String(descriptor.magFilter); + samplerInfo += SamplerFilterType2String(descriptor.minFilter); + samplerInfo += SamplerFilterType2String(descriptor.mipmapFilter); + samplerInfo += SamplerAddressMode2String(descriptor.sAddressMode); + samplerInfo += SamplerAddressMode2String(descriptor.tAddressMode); + return samplerInfo; +} + +std::string StringUtils::StencilOperation2String(const StencilOperation& operation) +{ + switch (operation) { + case StencilOperation::KEEP: + return "KEEP"; + case StencilOperation::ZERO: + return "ZERO"; + case StencilOperation::REPLACE: + return "REPLACE"; + case StencilOperation::INVERT: + return "INVERT"; + case StencilOperation::INCREMENT_WRAP: + return "INCREMENT_WRAP"; + case StencilOperation::DECREMENT_WRAP: + return "DECREMENT_WRAP"; + default: + return ""; + } +} + +std::string StringUtils::CompareFunction2String(const CompareFunction& compareFunction) +{ + switch (compareFunction) { + case CompareFunction::NEVER: + return "NEVER"; + case CompareFunction::LESS: + return "LESS"; + case CompareFunction::LESS_EQUAL: + return "LESS_EQUAL"; + case CompareFunction::GREATER: + return "GREATER"; + case CompareFunction::GREATER_EQUAL: + return "GREATER_EQUAL"; + case CompareFunction::EQUAL: + return "EQUAL"; + case CompareFunction::NOT_EQUAL: + return "NOT_EQUAL"; + case CompareFunction::ALWAYS: + return "ALWAYS"; + default: + return ""; + } +} + +std::string StringUtils::ColorWriteMask2String(const ColorWriteMask& colorWriteMask) +{ + switch (colorWriteMask) { + case ColorWriteMask::NONE: + return "NONE"; + case ColorWriteMask::RED: + return "RED"; + case ColorWriteMask::GREEN: + return "GREEN"; + case ColorWriteMask::BLUE: + return "BLUE"; + case ColorWriteMask::ALPHA: + return "ALPHA"; + case ColorWriteMask::ALL: + return "ALL"; + default: + return ""; + } +} + +std::string StringUtils::BlendOperation2String(const BlendOperation& blendOperation) +{ + switch (blendOperation) { + case BlendOperation::ADD: + return "ADD"; + case BlendOperation::SUBTRACT: + return "SUBTRACT"; + case BlendOperation::RESERVE_SUBTRACT: + return "RESERVE_SUBTRACT"; + default: + return ""; + } +} + +std::string StringUtils::BlendFactor2String(const BlendFactor& blendFactor) +{ + switch (blendFactor) { + case BlendFactor::ZERO: + return "ZERO"; + case BlendFactor::ONE: + return "ONE"; + case BlendFactor::SRC_COLOR: + return "SRC_COLOR"; + case BlendFactor::ONE_MINUS_SRC_COLOR: + return "ONE_MINUS_SRC_COLOR"; + case BlendFactor::SRC_ALPHA: + return "SRC_ALPHA"; + case BlendFactor::ONE_MINUS_SRC_ALPHA: + return "ONE_MINUS_SRC_ALPHA"; + case BlendFactor::DST_COLOR: + return "DST_COLOR"; + case BlendFactor::ONE_MINUS_DST_COLOR: + return "ONE_MINUS_DST_COLOR"; + case BlendFactor::DST_ALPHA: + return "DST_ALPHA"; + case BlendFactor::ONE_MINUS_DST_ALPHA: + return "ONE_MINUS_DST_ALPHA"; + case BlendFactor::SRC_ALPHA_SATURATE: + return "SRC_ALPHA_SATURATE"; + case BlendFactor::BLEND_CLOLOR: + return "BLEND_CLOLOR"; + default: + return ""; + } +} + +CC_BACKEND_END diff --git a/src/backend/StringUtils.h b/src/backend/StringUtils.h new file mode 100644 index 0000000..feb5edd --- /dev/null +++ b/src/backend/StringUtils.h @@ -0,0 +1,33 @@ +// +// StringUtils.hpp +// Test +// +// Created by Cocos on 2018/11/2. +// Copyright © 2018 cocos. All rights reserved. +// + +#ifndef StringUtils_hpp +#define StringUtils_hpp + +#include +#include "Types.h" +CC_BACKEND_BEGIN + +class StringUtils +{ +public: + static std::string TextureFormat2String(const TextureFormat& textureFormat); + static std::string TextureType2String(const TextureType& textureType); + static std::string TextureUsage2String(const TextureUsage& textureUsage); + static std::string SamplerFilterType2String(const SamplerFilter& filterType); + static std::string SamplerAddressMode2String(const SamplerAddressMode& addressMode); + static std::string SamplerDescriptor2String(const SamplerDescriptor& descriptor); + static std::string StencilOperation2String(const StencilOperation& operation); + static std::string CompareFunction2String(const CompareFunction& compareFunction); + static std::string ColorWriteMask2String(const ColorWriteMask& colorWriteMask); + static std::string BlendOperation2String(const BlendOperation& blendOperation); + static std::string BlendFactor2String(const BlendFactor& blendFactor); +}; + +CC_BACKEND_END +#endif /* StringUtils_hpp */ diff --git a/src/backend/Texture.cpp b/src/backend/Texture.cpp index e224f28..0c18ec2 100644 --- a/src/backend/Texture.cpp +++ b/src/backend/Texture.cpp @@ -1,4 +1,7 @@ #include "Texture.h" +#include "StringUtils.h" + +#include CC_BACKEND_BEGIN diff --git a/src/backend/Texture.h b/src/backend/Texture.h index bb9c1f4..7496026 100644 --- a/src/backend/Texture.h +++ b/src/backend/Texture.h @@ -27,7 +27,7 @@ class Texture : public cocos2d::Ref inline TextureUsage getTextureUsage() const { return _textureUsage; } inline uint32_t getWidth() const { return _width; } inline uint32_t getHeight() const { return _height; } - + inline size_t getTextureHashCode() const {return _textureHashCode;} protected: Texture(const TextureDescriptor& descriptor); virtual ~Texture(); @@ -40,6 +40,7 @@ class Texture : public cocos2d::Ref TextureFormat _textureFormat = TextureFormat::R8G8B8; TextureUsage _textureUsage = TextureUsage::READ; bool _isMipmapEnabled = false; + size_t _textureHashCode = 0; }; CC_BACKEND_END diff --git a/src/backend/metal/BlendStateMTL.h b/src/backend/metal/BlendStateMTL.h index 43b95dc..4033e39 100644 --- a/src/backend/metal/BlendStateMTL.h +++ b/src/backend/metal/BlendStateMTL.h @@ -26,7 +26,7 @@ class BlendStateMTL : public BlendState BlendStateMTL(const BlendDescriptor& descriptor); inline const BlendDescriptorMTL& getBlendDescriptorMTL() const { return _blendDescriptorMTL; } - + inline const BlendDescriptor& getBlendDescriptor() const {return _blendDescriptor;} private: BlendDescriptorMTL _blendDescriptorMTL; }; diff --git a/src/backend/metal/BlendStateMTL.mm b/src/backend/metal/BlendStateMTL.mm index fa8b6a6..0331eeb 100644 --- a/src/backend/metal/BlendStateMTL.mm +++ b/src/backend/metal/BlendStateMTL.mm @@ -67,6 +67,7 @@ MTLBlendOperation toMTLBlendOperation(BlendOperation operation) } BlendStateMTL::BlendStateMTL(const BlendDescriptor& descriptor) +: BlendState(descriptor) { _blendDescriptorMTL.writeMask = toMTLColorWriteMask(descriptor.writeMask); _blendDescriptorMTL.blendEnabled = descriptor.blendEnabled; diff --git a/src/backend/metal/DepthStencilStateMTL.h b/src/backend/metal/DepthStencilStateMTL.h index 0ad8b09..61f6ef5 100644 --- a/src/backend/metal/DepthStencilStateMTL.h +++ b/src/backend/metal/DepthStencilStateMTL.h @@ -12,6 +12,7 @@ class DepthStencilStateMTL : public DepthStencilState ~DepthStencilStateMTL(); inline id getMTLDepthStencilState() const { return _mtlDepthStencilState; } + inline const DepthStencilDescriptor& getDepthStencilDescriptor() const {return _depthStencilInfo;} private: id _mtlDepthStencilState = nil; diff --git a/src/backend/metal/DeviceMTL.h b/src/backend/metal/DeviceMTL.h index 24fb040..f61dc26 100644 --- a/src/backend/metal/DeviceMTL.h +++ b/src/backend/metal/DeviceMTL.h @@ -3,7 +3,8 @@ #include "../Device.h" #import #import - +#import +#include CC_BACKEND_BEGIN class DeviceMTL : public Device @@ -27,16 +28,24 @@ class DeviceMTL : public Device virtual DepthStencilState* createDepthStencilState(const DepthStencilDescriptor& descriptor) override; virtual BlendState* createBlendState(const BlendDescriptor& descriptor) override; virtual RenderPipeline* newRenderPipeline(const RenderPipelineDescriptor& descriptor) override; - inline id getMTLDevice() const { return _mtlDevice; } inline id getMTLCommandQueue() const { return _mtlCommandQueue; } +private: + size_t findRenderPassInCache(const RenderPassDescriptor& descriptor); + size_t findShaderModuleInCache(const ShaderStage& stage, const std::string& source); + size_t findRenderPipelineInCache(const RenderPipelineDescriptor& descriptor); + private: static CAMetalLayer* _metalLayer; static id _currentDrawable; id _mtlDevice = nil; id _mtlCommandQueue = nil; + + std::vector _cachedRenderPass; + std::vector _cachedShaderModule; + std::vector _cachedRenderPipeline; }; CC_BACKEND_END diff --git a/src/backend/metal/DeviceMTL.mm b/src/backend/metal/DeviceMTL.mm index 8d4b1e7..8af6538 100644 --- a/src/backend/metal/DeviceMTL.mm +++ b/src/backend/metal/DeviceMTL.mm @@ -48,6 +48,17 @@ DeviceMTL::~DeviceMTL() { + for(auto& renderPass : _cachedRenderPass) + CC_SAFE_RELEASE(renderPass); + _cachedRenderPass.clear(); + + for(auto& renderPipeline : _cachedRenderPipeline) + CC_SAFE_RELEASE(renderPipeline); + _cachedRenderPipeline.clear(); + + for(auto& shaderModule : _cachedShaderModule) + CC_SAFE_RELEASE(shaderModule); + _cachedShaderModule.clear(); } CommandBuffer* DeviceMTL::newCommandBuffer() @@ -67,16 +78,64 @@ RenderPass* DeviceMTL::newRenderPass(const RenderPassDescriptor& descriptor) { - return new (std::nothrow) RenderPassMTL(_mtlDevice, descriptor); + auto posIndex = findRenderPassInCache(descriptor); + if(posIndex != -1) + { + _cachedRenderPass[posIndex]->retain(); + return _cachedRenderPass[posIndex]; + } + else + { + auto renderPass = new (std::nothrow) RenderPassMTL(_mtlDevice, descriptor); + renderPass->retain(); + _cachedRenderPass.push_back(renderPass); + return renderPass; + } +} + +size_t DeviceMTL::findRenderPassInCache(const RenderPassDescriptor& descriptor) +{ + for(std::vector::const_iterator iter = _cachedRenderPass.begin(); iter != _cachedRenderPass.end(); iter++) + { + bool bFound = (*iter)->Find(descriptor); + if(bFound) + { + return iter - _cachedRenderPass.begin(); + } + } + + return -1; } ShaderModule* DeviceMTL::createShaderModule(ShaderStage stage, const std::string& source) { - auto ret = new (std::nothrow) ShaderModuleMTL(_mtlDevice, stage, source); - if (ret) - ret->autorelease(); - - return ret; + auto posIndex = findShaderModuleInCache(stage, source); + if (posIndex != -1) + { + _cachedShaderModule[posIndex]->retain(); + return _cachedShaderModule[posIndex]; + } + + auto shaderModule = new (std::nothrow) ShaderModuleMTL(_mtlDevice, stage, source); + shaderModule->retain(); + _cachedShaderModule.push_back(shaderModule); + return shaderModule; +} + +size_t DeviceMTL::findShaderModuleInCache(const ShaderStage& stage, const std::string& source) +{ + for(std::vector::const_iterator iter = _cachedShaderModule.begin(); iter != _cachedShaderModule.end(); iter++) + { + ShaderModuleMTL* shader = static_cast(*iter); + if(shader->getShaderStage() != stage) + continue; + bool bFound = shader->Find(source); + if(bFound) + { + return iter - _cachedShaderModule.begin(); + } + } + return -1; } DepthStencilState* DeviceMTL::createDepthStencilState(const DepthStencilDescriptor& descriptor) @@ -99,7 +158,32 @@ RenderPipeline* DeviceMTL::newRenderPipeline(const RenderPipelineDescriptor& descriptor) { - return new (std::nothrow) RenderPipelineMTL(_mtlDevice, descriptor); + auto posIndex = findRenderPipelineInCache(descriptor); + if (posIndex != -1) + { + _cachedRenderPipeline[posIndex]->retain(); + return _cachedRenderPipeline[posIndex]; + } + else + { + auto renderPipeline = new (std::nothrow) RenderPipelineMTL(_mtlDevice, descriptor); + renderPipeline->retain(); + _cachedRenderPipeline.push_back(renderPipeline); + return renderPipeline; + } +} + +size_t DeviceMTL::findRenderPipelineInCache(const RenderPipelineDescriptor& descriptor) +{ + for(std::vector::const_iterator iter = _cachedRenderPipeline.begin(); iter != _cachedRenderPipeline.end(); iter++) + { + RenderPipelineMTL* renderPipeline = static_cast(*iter); + if(renderPipeline->getHash() == renderPipeline->createHash(descriptor)) + { + return iter - _cachedRenderPipeline.begin(); + } + } + return -1; } CC_BACKEND_END diff --git a/src/backend/metal/RenderPassMTL.mm b/src/backend/metal/RenderPassMTL.mm index e2ba064..e94aa94 100644 --- a/src/backend/metal/RenderPassMTL.mm +++ b/src/backend/metal/RenderPassMTL.mm @@ -11,7 +11,7 @@ if (_colorAttachmentsSet || _depthStencilAttachmentSet) _mtlRenderPassDescritpr = [MTLRenderPassDescriptor renderPassDescriptor]; else - _mtlRenderPassDescritpr = Utils::getDefaultRenderPassDescriptor(); + _mtlRenderPassDescritpr = Utils::createNewRenderPassDescriptor(); [_mtlRenderPassDescritpr retain]; setColorAttachments(descriptor); @@ -32,7 +32,11 @@ if (!_colorAttachmentsSet) _mtlRenderPassDescritpr.colorAttachments[0].texture = defaultRenderPassDescriptor.colorAttachments[0].texture; if (!_depthStencilAttachmentSet) + { +// When depth and stencil are used together, the texture bound to the depth and stencil render pass attachments must be the same depth and stencil _mtlRenderPassDescritpr.depthAttachment.texture = defaultRenderPassDescriptor.depthAttachment.texture; + _mtlRenderPassDescritpr.stencilAttachment.texture = defaultRenderPassDescriptor.stencilAttachment.texture; + } } return _mtlRenderPassDescritpr; diff --git a/src/backend/metal/RenderPipelineMTL.h b/src/backend/metal/RenderPipelineMTL.h index 58be19c..7b43361 100644 --- a/src/backend/metal/RenderPipelineMTL.h +++ b/src/backend/metal/RenderPipelineMTL.h @@ -28,6 +28,8 @@ class RenderPipelineMTL : public RenderPipeline inline const std::vector& getFragmentUniforms() const { return _fragmentUniforms; } inline const std::vector& getVertexTextures() const { return _vertexTextures; } inline const std::vector& getFragmentTextures() const { return _fragmentTextures; } + inline const size_t getHash() const {return _hashRenderPipelineDescriptor;}; + size_t createHash(const RenderPipelineDescriptor& descriptor); private: void setVertexLayout(MTLRenderPipelineDescriptor*, const RenderPipelineDescriptor&); @@ -47,6 +49,7 @@ class RenderPipelineMTL : public RenderPipeline MTLRenderPipelineDescriptor* _mtlRenderPipelineDescriptor = nil; BlendDescriptorMTL _blendDescriptorMTL; + size_t _hashRenderPipelineDescriptor = 0; }; CC_BACKEND_END diff --git a/src/backend/metal/RenderPipelineMTL.mm b/src/backend/metal/RenderPipelineMTL.mm index 317de28..bf991ac 100644 --- a/src/backend/metal/RenderPipelineMTL.mm +++ b/src/backend/metal/RenderPipelineMTL.mm @@ -3,6 +3,7 @@ #include "ShaderModuleMTL.h" #include "DepthStencilStateMTL.h" #include "Utils.h" +#include "../StringUtils.h" CC_BACKEND_BEGIN @@ -92,6 +93,8 @@ MTLVertexFormat toMTLVertexFormat(VertexFormat vertexFormat) auto blendState = static_cast(descriptor.getBlendState()); if (blendState) _blendDescriptorMTL = blendState->getBlendDescriptorMTL(); + + _hashRenderPipelineDescriptor = createHash(descriptor); } RenderPipelineMTL::~RenderPipelineMTL() @@ -193,4 +196,58 @@ MTLVertexFormat toMTLVertexFormat(VertexFormat vertexFormat) colorAttachmentDescriptor.destinationAlphaBlendFactor = _blendDescriptorMTL.destinationAlphaBlendFactor; } +size_t RenderPipelineMTL::createHash(const RenderPipelineDescriptor& descriptor) +{ + std::string info = ""; + ShaderModuleMTL* vertModule = static_cast(descriptor.getVertexShaderModule()); + ShaderModuleMTL* fragModule = static_cast(descriptor.getFragmentShaderModule()); + if(vertModule || fragModule) + { + info += std::to_string(vertModule->getHash()); + info += std::to_string(fragModule->getHash()); + } + + DepthStencilState* depStencilState = descriptor.getDepthStencilState(); + if(depStencilState) + { + DepthStencilStateMTL* depthStencilStateMTL = static_cast(depStencilState); + DepthStencilDescriptor depthStencilDescriptor = depthStencilStateMTL->getDepthStencilDescriptor(); + + info += StringUtils::CompareFunction2String(depthStencilDescriptor.depthCompareFunction); + info += depthStencilDescriptor.depthWriteEnabled; + + info += StringUtils::StencilOperation2String(depthStencilDescriptor.backFaceStencil.stencilFailureOperation); + info += StringUtils::StencilOperation2String(depthStencilDescriptor.backFaceStencil.depthFailureOperation); + info += StringUtils::StencilOperation2String(depthStencilDescriptor.backFaceStencil.depthStencilPassOperation); + info += StringUtils::CompareFunction2String(depthStencilDescriptor.backFaceStencil.stencilCompareFunction); + info += depthStencilDescriptor.backFaceStencil.readMask; + info += depthStencilDescriptor.backFaceStencil.writeMask; + + info += StringUtils::StencilOperation2String(depthStencilDescriptor.frontFaceStencil.stencilFailureOperation); + info += StringUtils::StencilOperation2String(depthStencilDescriptor.frontFaceStencil.depthFailureOperation); + info += StringUtils::StencilOperation2String(depthStencilDescriptor.frontFaceStencil.depthStencilPassOperation); + info += StringUtils::CompareFunction2String(depthStencilDescriptor.frontFaceStencil.stencilCompareFunction); + info += depthStencilDescriptor.frontFaceStencil.readMask; + info += depthStencilDescriptor.frontFaceStencil.writeMask; + } + + BlendState* blendState = descriptor.getBlendState(); + if(blendState) + { + BlendStateMTL* blendStateMTL = static_cast(blendState); + BlendDescriptor blendDescriptor = blendStateMTL->getBlendDescriptor(); + + info += StringUtils::ColorWriteMask2String(blendDescriptor.writeMask); + info += blendDescriptor.blendEnabled; + info += StringUtils::BlendOperation2String(blendDescriptor.rgbBlendOperation); + info += StringUtils::BlendOperation2String(blendDescriptor.alphaBlendOperation); + info += StringUtils::BlendFactor2String(blendDescriptor.sourceRGBBlendFactor); + info += StringUtils::BlendFactor2String(blendDescriptor.destinationRGBBlendFactor); + info += StringUtils::BlendFactor2String(blendDescriptor.sourceAlphaBlendFactor); + info += StringUtils::BlendFactor2String(blendDescriptor.destinationAlphaBlendFactor); + } + + return std::hash{}(info); +} + CC_BACKEND_END diff --git a/src/backend/metal/ShaderModuleMTL.h b/src/backend/metal/ShaderModuleMTL.h index de123c8..430dad9 100644 --- a/src/backend/metal/ShaderModuleMTL.h +++ b/src/backend/metal/ShaderModuleMTL.h @@ -20,7 +20,8 @@ class ShaderModuleMTL : public ShaderModule inline const std::shared_ptr& getUniformBuffer() const { return _uniformBuffer; } inline const std::vector& getUniforms() const { return _uniforms; } inline const std::vector& getTextures() const { return _textures; } - + inline const uint32_t getHash() const {return _hashCode;} + bool Find(const std::string& source); private: void parseUniform(id mtlDevice, glslopt_shader* shader); void parseTexture(id mtlDevice, glslopt_shader* shader); @@ -32,6 +33,7 @@ class ShaderModuleMTL : public ShaderModule // Texture index is the same as vector index. std::vector _textures; + size_t _hashCode = 0; }; CC_BACKEND_END diff --git a/src/backend/metal/ShaderModuleMTL.mm b/src/backend/metal/ShaderModuleMTL.mm index 497b1bd..d2480e3 100644 --- a/src/backend/metal/ShaderModuleMTL.mm +++ b/src/backend/metal/ShaderModuleMTL.mm @@ -59,6 +59,8 @@ assert(false); } + _hashCode = std::hash{}(source); + glslopt_shader_delete(glslShader); glslopt_cleanup(ctx); } @@ -102,4 +104,11 @@ } } +bool ShaderModuleMTL::Find(const std::string& source) +{ + size_t hashShader = std::hash{}(source); + + return _hashCode == hashShader ? true : false; +} + CC_BACKEND_END diff --git a/src/backend/metal/TextureMTL.mm b/src/backend/metal/TextureMTL.mm index 793fe85..4bd95f4 100644 --- a/src/backend/metal/TextureMTL.mm +++ b/src/backend/metal/TextureMTL.mm @@ -91,6 +91,10 @@ bool convertData(uint8_t* src, uint32_t length, TextureFormat format, uint8_t** void TextureMTL::updateData(uint8_t* data) { + size_t dataHash = std::hash{}(data); + if(_textureHashCode == dataHash) + return; + _textureHashCode = dataHash; updateSubData(0, 0, (uint32_t)_mtlTexture.width, (uint32_t)_mtlTexture.height, data); } diff --git a/src/backend/metal/Utils.h b/src/backend/metal/Utils.h index 0dd35af..cfc0102 100644 --- a/src/backend/metal/Utils.h +++ b/src/backend/metal/Utils.h @@ -13,6 +13,7 @@ class Utils static id getTempDepthStencilAttachmentTexture(); static void createDefaultRenderPassDescriptor(); + static MTLRenderPassDescriptor* createNewRenderPassDescriptor(); static MTLRenderPassDescriptor* getDefaultRenderPassDescriptor(); static void updateDefaultRenderPassDescriptor(id texture); static MTLPixelFormat getDefaultDepthStencilAttachmentPixelFormat(); diff --git a/src/backend/metal/Utils.mm b/src/backend/metal/Utils.mm index c159fc6..9b0aa43 100644 --- a/src/backend/metal/Utils.mm +++ b/src/backend/metal/Utils.mm @@ -63,6 +63,27 @@ Utils::_defaultRenderPassDescriptor.stencilAttachment.loadAction = MTLLoadActionClear; [Utils::_defaultRenderPassDescriptor retain]; } + +} + +MTLRenderPassDescriptor* Utils:: createNewRenderPassDescriptor() +{ + MTLRenderPassDescriptor* _renderPassDescriptor = [MTLRenderPassDescriptor renderPassDescriptor]; + + //The MTLLoadActionDontCare action allows the GPU to avoid loading the existing contents of the texture, ensuring the best performance. + _renderPassDescriptor.colorAttachments[0].loadAction = MTLLoadActionClear; + _renderPassDescriptor.colorAttachments[0].storeAction = MTLStoreActionStore; + + // Set default depth and stencil texture. + auto defaultDepthStencilTexture = Utils::createDepthStencilAttachmentTexture(); + _renderPassDescriptor.depthAttachment.texture = defaultDepthStencilTexture; + _renderPassDescriptor.stencilAttachment.texture = defaultDepthStencilTexture; + + _renderPassDescriptor.depthAttachment.loadAction = MTLLoadActionClear; + _renderPassDescriptor.stencilAttachment.loadAction = MTLLoadActionClear; + [_renderPassDescriptor retain]; + + return _renderPassDescriptor; } MTLRenderPassDescriptor* Utils::getDefaultRenderPassDescriptor() diff --git a/src/backend/opengl/BlendStateGL.cpp b/src/backend/opengl/BlendStateGL.cpp index 0673e88..683c828 100644 --- a/src/backend/opengl/BlendStateGL.cpp +++ b/src/backend/opengl/BlendStateGL.cpp @@ -79,7 +79,8 @@ void BlendStateGL::reset() } BlendStateGL::BlendStateGL(const BlendDescriptor& descriptor) -: _blendEnabled(descriptor.blendEnabled) +: BlendState(descriptor) +, _blendEnabled(descriptor.blendEnabled) , _rgbBlendOperation(toGLBlendOperation(descriptor.rgbBlendOperation)) , _alphaBlendOperation(toGLBlendOperation(descriptor.alphaBlendOperation)) , _sourceRGBBlendFactor(toGLBlendFactor(descriptor.sourceRGBBlendFactor)) diff --git a/src/backend/opengl/DeviceGL.cpp b/src/backend/opengl/DeviceGL.cpp index e651d20..197866f 100644 --- a/src/backend/opengl/DeviceGL.cpp +++ b/src/backend/opengl/DeviceGL.cpp @@ -69,5 +69,4 @@ RenderPipeline* DeviceGL::newRenderPipeline(const RenderPipelineDescriptor& desc { return new (std::nothrow) RenderPipelineGL(descriptor); } - CC_BACKEND_END diff --git a/test/Test.xcodeproj/project.pbxproj b/test/Test.xcodeproj/project.pbxproj index f656910..cd81e56 100644 --- a/test/Test.xcodeproj/project.pbxproj +++ b/test/Test.xcodeproj/project.pbxproj @@ -196,10 +196,12 @@ 469492801FE252EA008D19CC /* Cocoa.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 4694927F1FE252E3008D19CC /* Cocoa.framework */; }; 469492821FE25305008D19CC /* IOKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 469492811FE252FB008D19CC /* IOKit.framework */; }; 469492841FE25314008D19CC /* CoreVideo.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 469492831FE2530E008D19CC /* CoreVideo.framework */; }; + ED0875D2218BF27900B7F6A4 /* StringUtils.cpp in Sources */ = {isa = PBXBuildFile; fileRef = ED0875D0218BF27900B7F6A4 /* StringUtils.cpp */; }; + ED0875D3218BF27900B7F6A4 /* StringUtils.cpp in Sources */ = {isa = PBXBuildFile; fileRef = ED0875D0218BF27900B7F6A4 /* StringUtils.cpp */; }; + ED2F9F46218FDC40008E6BC2 /* ParticleBackend.cpp in Sources */ = {isa = PBXBuildFile; fileRef = ED2F9F45218FDC3F008E6BC2 /* ParticleBackend.cpp */; }; + ED2F9F47218FDC40008E6BC2 /* ParticleBackend.cpp in Sources */ = {isa = PBXBuildFile; fileRef = ED2F9F45218FDC3F008E6BC2 /* ParticleBackend.cpp */; }; ED3B4C42217E15C000D982A0 /* GuiProjectionBackend.cpp in Sources */ = {isa = PBXBuildFile; fileRef = ED3B4C3E217E15BF00D982A0 /* GuiProjectionBackend.cpp */; }; ED3B4C43217E15C000D982A0 /* GuiProjectionBackend.cpp in Sources */ = {isa = PBXBuildFile; fileRef = ED3B4C3E217E15BF00D982A0 /* GuiProjectionBackend.cpp */; }; - ED3B4C44217E15C000D982A0 /* ParticleBackend.cpp in Sources */ = {isa = PBXBuildFile; fileRef = ED3B4C41217E15C000D982A0 /* ParticleBackend.cpp */; }; - ED3B4C45217E15C000D982A0 /* ParticleBackend.cpp in Sources */ = {isa = PBXBuildFile; fileRef = ED3B4C41217E15C000D982A0 /* ParticleBackend.cpp */; }; /* End PBXBuildFile section */ /* Begin PBXCopyFilesBuildPhase section */ @@ -485,10 +487,12 @@ 4694927F1FE252E3008D19CC /* Cocoa.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Cocoa.framework; path = System/Library/Frameworks/Cocoa.framework; sourceTree = SDKROOT; }; 469492811FE252FB008D19CC /* IOKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = IOKit.framework; path = System/Library/Frameworks/IOKit.framework; sourceTree = SDKROOT; }; 469492831FE2530E008D19CC /* CoreVideo.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreVideo.framework; path = System/Library/Frameworks/CoreVideo.framework; sourceTree = SDKROOT; }; + ED0875D0218BF27900B7F6A4 /* StringUtils.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = StringUtils.cpp; sourceTree = ""; }; + ED2F9F45218FDC3F008E6BC2 /* ParticleBackend.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ParticleBackend.cpp; sourceTree = ""; }; + ED366CF92199B77400035735 /* StringUtils.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = StringUtils.h; sourceTree = ""; }; ED3B4C3E217E15BF00D982A0 /* GuiProjectionBackend.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = GuiProjectionBackend.cpp; sourceTree = ""; }; ED3B4C3F217E15BF00D982A0 /* ParticleBackend.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ParticleBackend.h; sourceTree = ""; }; ED3B4C40217E15C000D982A0 /* GuiProjectionBackend.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GuiProjectionBackend.h; sourceTree = ""; }; - ED3B4C41217E15C000D982A0 /* ParticleBackend.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ParticleBackend.cpp; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -844,6 +848,8 @@ 460373AF212FA9EB00DC9ED4 /* backend */ = { isa = PBXGroup; children = ( + ED366CF92199B77400035735 /* StringUtils.h */, + ED0875D0218BF27900B7F6A4 /* StringUtils.cpp */, 460374512148F78600DC9ED4 /* Backend.h */, 460373F9213932AE00DC9ED4 /* BindGroup.cpp */, 460373FA213932AE00DC9ED4 /* BindGroup.h */, @@ -907,9 +913,9 @@ 460373D821363EDD00DC9ED4 /* backend */ = { isa = PBXGroup; children = ( + ED2F9F45218FDC3F008E6BC2 /* ParticleBackend.cpp */, ED3B4C3E217E15BF00D982A0 /* GuiProjectionBackend.cpp */, ED3B4C40217E15C000D982A0 /* GuiProjectionBackend.h */, - ED3B4C41217E15C000D982A0 /* ParticleBackend.cpp */, ED3B4C3F217E15BF00D982A0 /* ParticleBackend.h */, 460375802150C91C00DC9ED4 /* MultiTexturesBackend.cpp */, 4603757F2150C91C00DC9ED4 /* MultiTexturesBackend.h */, @@ -1287,7 +1293,7 @@ 1ACB61B51FF6028B0007F081 /* ioapi_mem.cpp in Sources */, 4603743F2147742800DC9ED4 /* DepthStencilState.cpp in Sources */, 1A255E2C20034B0D00069420 /* CCES2Renderer-ios.m in Sources */, - ED3B4C45217E15C000D982A0 /* ParticleBackend.cpp in Sources */, + ED0875D3218BF27900B7F6A4 /* StringUtils.cpp in Sources */, 46037417214247C200DC9ED4 /* VertexLayout.cpp in Sources */, 1A255E2A20034B0D00069420 /* CCImage.cpp in Sources */, 1A255E3020034B0D00069420 /* CCDevice-ios.mm in Sources */, @@ -1296,6 +1302,7 @@ 1A255E2420034B0D00069420 /* CCDevice-apple.mm in Sources */, 1A255E2E20034B0D00069420 /* CCEAGLView-ios.mm in Sources */, 1A255E5C20034B0D00069420 /* Vec3.cpp in Sources */, + ED2F9F47218FDC40008E6BC2 /* ParticleBackend.cpp in Sources */, 46233BEE2176C979000F1F21 /* StencilBackend.cpp in Sources */, 1A255E6E20034B0D00069420 /* pvr.cpp in Sources */, 46037414214247B100DC9ED4 /* RenderPass.cpp in Sources */, @@ -1347,7 +1354,6 @@ 1A255E2720034B0D00069420 /* CCSAXParser.cpp in Sources */, 46233BEA217446B3000F1F21 /* BlendingBackend.cpp in Sources */, 468BD2E32154FE75007BCACF /* RenderPassDescriptor.cpp in Sources */, - ED3B4C44217E15C000D982A0 /* ParticleBackend.cpp in Sources */, 461DD0D821536FC900A8E43F /* main.mm in Sources */, 1A255E2920034B0D00069420 /* CCImage.cpp in Sources */, 466BA3B6216C79C7006C15A5 /* DepthStencilStateMTL.mm in Sources */, @@ -1373,6 +1379,7 @@ 1A255E3320034B0D00069420 /* CCDevice-mac.mm in Sources */, 1A255E4720034B0D00069420 /* MathUtil.cpp in Sources */, 1A255E4D20034B0D00069420 /* Mat4.cpp in Sources */, + ED0875D2218BF27900B7F6A4 /* StringUtils.cpp in Sources */, 461DD0ED21538B0100A8E43F /* CommandBuffer.cpp in Sources */, 1A255E5D20034B0D00069420 /* CCAffineTransform.cpp in Sources */, 46486F31217077020078BE0E /* Utils.mm in Sources */, @@ -1382,6 +1389,7 @@ 468BD2F1215A142B007BCACF /* RenderPipelineMTL.mm in Sources */, 467E26232154A3FB0055AD62 /* RenderPassMTL.mm in Sources */, 1A69F1011FF22B0200B224DF /* tinyxml2.cpp in Sources */, + ED2F9F46218FDC40008E6BC2 /* ParticleBackend.cpp in Sources */, 1A255E5520034B0D00069420 /* Vec4.cpp in Sources */, 1A69F1001FF22B0200B224DF /* unzip.cpp in Sources */, 4627246A1FF4E39E000D13BC /* Utils.cpp in Sources */, diff --git a/test/tests/backend/BasicBackend.cpp b/test/tests/backend/BasicBackend.cpp index 3da5daf..f73b745 100644 --- a/test/tests/backend/BasicBackend.cpp +++ b/test/tests/backend/BasicBackend.cpp @@ -62,7 +62,6 @@ BasicBackend::BasicBackend() )"; auto device = cocos2d::backend::Device::getInstance(); - auto vs = device->createShaderModule(cocos2d::backend::ShaderStage::VERTEX, vert); auto fs = device->createShaderModule(cocos2d::backend::ShaderStage::FRAGMENT, frag); cocos2d::backend::RenderPipelineDescriptor renderPipelineDescriptor; @@ -73,7 +72,6 @@ BasicBackend::BasicBackend() vertexLayout.setAtrribute("a_position", 0, cocos2d::backend::VertexFormat::FLOAT_R32G32, 0); vertexLayout.setLayout(sizeof(float) * 2, cocos2d::backend::VertexStepMode::VERTEX); renderPipelineDescriptor.setVertexLayout(0, vertexLayout); - _renderPipeline = device->newRenderPipeline(renderPipelineDescriptor); _commandBuffer = device->newCommandBuffer(); diff --git a/test/tests/backend/BlendingBackend.cpp b/test/tests/backend/BlendingBackend.cpp index d6d6dab..7ee6a20 100644 --- a/test/tests/backend/BlendingBackend.cpp +++ b/test/tests/backend/BlendingBackend.cpp @@ -88,7 +88,7 @@ namespace auto blendStateNoBlending = device->createBlendState(blendDescriptorNoBlending); renderPipelineDescriptor.setBlendState(blendStateNoBlending); renderPipelineNoBlending = device->newRenderPipeline(renderPipelineDescriptor); - + backend::BlendDescriptor blendDescriptorNormal; blendDescriptorNormal.blendEnabled = true; blendDescriptorNormal.rgbBlendOperation = backend::BlendOperation::ADD; @@ -112,7 +112,7 @@ namespace auto blendStateAddtive = device->createBlendState(blendDescriptorAddtive); renderPipelineDescriptor.setBlendState(blendStateAddtive); renderPipelineAddtive = device->newRenderPipeline(renderPipelineDescriptor); - + backend::BlendDescriptor blendDescriptorSubstract; blendDescriptorSubstract.blendEnabled = true; blendDescriptorSubstract.rgbBlendOperation = backend::BlendOperation::ADD; diff --git a/test/tests/backend/BunnyBackend.cpp b/test/tests/backend/BunnyBackend.cpp index 3029aa2..c3782c6 100644 --- a/test/tests/backend/BunnyBackend.cpp +++ b/test/tests/backend/BunnyBackend.cpp @@ -78,12 +78,12 @@ BunnyBackend::BunnyBackend() depthStencilDescriptor.depthCompareFunction = cocos2d::backend::CompareFunction::LESS; auto depthStencilState = device->createDepthStencilState(depthStencilDescriptor); renderPipelineDescriptor.setDepthStencilState(depthStencilState); - + auto vs = device->createShaderModule(cocos2d::backend::ShaderStage::VERTEX, vert); auto fs = device->createShaderModule(cocos2d::backend::ShaderStage::FRAGMENT, frag); renderPipelineDescriptor.setVertexShaderModule(vs); renderPipelineDescriptor.setFragmentShaderModule(fs); - + _renderPipeline = device->newRenderPipeline(renderPipelineDescriptor); // render pass diff --git a/test/tests/backend/DepthTextureBackend.cpp b/test/tests/backend/DepthTextureBackend.cpp index e2d62bf..5701f1b 100644 --- a/test/tests/backend/DepthTextureBackend.cpp +++ b/test/tests/backend/DepthTextureBackend.cpp @@ -84,7 +84,6 @@ namespace vertexLayout.setAtrribute("a_position", 0, cocos2d::backend::VertexFormat::FLOAT_R32G32, 0); vertexLayout.setLayout(2 * sizeof(float), cocos2d::backend::VertexStepMode::VERTEX); renderPipelineDescriptor.setVertexLayout(0, vertexLayout); - renderPipeline = device->newRenderPipeline(renderPipelineDescriptor); // vertex buffer @@ -158,8 +157,8 @@ namespace depthStencilDescriptor.depthCompareFunction = backend::CompareFunction::LESS; auto depthStencilState = device->createDepthStencilState(depthStencilDescriptor); renderPipelineDescriptor.setDepthStencilState(depthStencilState); - renderPipeline = device->newRenderPipeline(renderPipelineDescriptor); + renderPipeline->retain(); // vertex buffer @@ -217,9 +216,10 @@ DepthTextureBackend::DepthTextureBackend() _renderPassBunny1 = device->newRenderPass(renderPassDescriptorBunny1); // render pass 2 - backend::RenderPassDescriptor renderPassDescriptorBunny2; - renderPassDescriptorBunny2.setDepthStencilAttachment(_depthTexture); - _renderPassBunny2 = device->newRenderPass(renderPassDescriptorBunny2); +// backend::RenderPassDescriptor renderPassDescriptorBunny2; +// renderPassDescriptorBunny2.setDepthStencilAttachment(_depthTexture); +//// _renderPassBunny2 = device->newRenderPass(renderPassDescriptorBunny2); +// _renderPassBunny2 = device->cacheRenderPass(renderPassDescriptorBunny2); // render pass BigTriangle backend::RenderPassDescriptor renderPassDescriptorBigTriangle; @@ -240,7 +240,7 @@ DepthTextureBackend::~DepthTextureBackend() CC_SAFE_RELEASE(_depthTexture); CC_SAFE_RELEASE(_renderPassBunny1); - CC_SAFE_RELEASE(_renderPassBunny2); +// CC_SAFE_RELEASE(_renderPassBunny2); CC_SAFE_RELEASE(_renderPassBigTriangle); CC_SAFE_RELEASE(_commandBuffer); } diff --git a/test/tests/backend/MultiTexturesBackend.cpp b/test/tests/backend/MultiTexturesBackend.cpp index b2c1566..cc69b57 100644 --- a/test/tests/backend/MultiTexturesBackend.cpp +++ b/test/tests/backend/MultiTexturesBackend.cpp @@ -75,7 +75,6 @@ MultiTexturesBackend::MultiTexturesBackend() renderPipelineDescriptor.setVertexLayout(0, vertexLayout); _renderPipeline = device->newRenderPipeline(renderPipelineDescriptor); - float vertices[] = {-1, -1, 1, -1, 1, 1, 1, 1, -1, 1, -1, -1}; _vertexBuffer = device->newBuffer(sizeof(vertices), cocos2d::backend::BufferType::VERTEX, diff --git a/test/tests/backend/ParticleBackend.cpp b/test/tests/backend/ParticleBackend.cpp index 37dd575..8589b62 100644 --- a/test/tests/backend/ParticleBackend.cpp +++ b/test/tests/backend/ParticleBackend.cpp @@ -161,10 +161,9 @@ ParticleBackend::ParticleBackend() Mat4::createLookAt(Vec3(30.0f , 20.0f, 30.0f), Vec3(0.0f, 2.5f, 0.0f), Vec3(0.0f, 1.0f, 0.f), &_view); backend::RenderPassDescriptor renderPassDescriptor; - renderPassDescriptor.setClearColor(0.1f, 0.1f, 0.1f, 1.f); + renderPassDescriptor.setClearColor(0.1f, 0.1f, 0.1f, 0.1f); renderPassDescriptor.setClearDepth(1); _renderPass = device->newRenderPass(renderPassDescriptor); - } ParticleBackend::~ParticleBackend() diff --git a/test/tests/backend/PostProcessBackend.cpp b/test/tests/backend/PostProcessBackend.cpp index 5bdb634..2da44f1 100644 --- a/test/tests/backend/PostProcessBackend.cpp +++ b/test/tests/backend/PostProcessBackend.cpp @@ -97,7 +97,7 @@ namespace vertexLayout.setLayout(2 * sizeof(float), backend::VertexStepMode::VERTEX); renderPipelineDescriptor.setVertexLayout(0, vertexLayout); _renderPipeline = device->newRenderPipeline(renderPipelineDescriptor); - + float vertices[] = {-1, 4, -1, -1, 4, -1}; _vertexBuffer = device->newBuffer(sizeof(vertices), backend::BufferType::VERTEX, @@ -170,7 +170,7 @@ namespace auto depthStencilState = device->createDepthStencilState(depthStencilDescriptor); renderPipelineDescriptor.setDepthStencilState(depthStencilState); _renderPipeline = device->newRenderPipeline(renderPipelineDescriptor); - + _vertexBuffer = device->newBuffer(sizeof(bunny_positions), backend::BufferType::VERTEX, backend::BufferUsage::READ); diff --git a/test/tests/backend/StencilBackend.cpp b/test/tests/backend/StencilBackend.cpp index 693631d..add07ad 100644 --- a/test/tests/backend/StencilBackend.cpp +++ b/test/tests/backend/StencilBackend.cpp @@ -98,7 +98,7 @@ StencilBackend::StencilBackend() vertexLayout.setLayout(2 * sizeof(float), cocos2d::backend::VertexStepMode::VERTEX); renderPipelineDescriptor.setVertexLayout(0, vertexLayout); _renderPipeline = device->newRenderPipeline(renderPipelineDescriptor); - + backend::DepthStencilDescriptor depthStencilDescriptor; depthStencilDescriptor.backFaceStencil.stencilCompareFunction = backend::CompareFunction::NEVER; depthStencilDescriptor.backFaceStencil.readMask = 0xFF; @@ -110,25 +110,25 @@ StencilBackend::StencilBackend() auto depthStencilState = device->createDepthStencilState(depthStencilDescriptor); renderPipelineDescriptor.setDepthStencilState(depthStencilState); _renderPipelineCavasTexture = device->newRenderPipeline(renderPipelineDescriptor); - + depthStencilDescriptor.backFaceStencil.stencilCompareFunction = backend::CompareFunction::EQUAL; depthStencilDescriptor.backFaceStencil.stencilFailureOperation = backend::StencilOperation::KEEP; depthStencilDescriptor.frontFaceStencil = depthStencilDescriptor.backFaceStencil; depthStencilState = device->createDepthStencilState(depthStencilDescriptor); renderPipelineDescriptor.setDepthStencilState(depthStencilState); _renderPipelineTextureBackAndFront = device->newRenderPipeline(renderPipelineDescriptor); - + depthStencilDescriptor.frontFaceStencil.stencilCompareFunction = backend::CompareFunction::ALWAYS; depthStencilState = device->createDepthStencilState(depthStencilDescriptor); renderPipelineDescriptor.setDepthStencilState(depthStencilState); _renderPipelineTextureBack = device->newRenderPipeline(renderPipelineDescriptor); - + depthStencilDescriptor.frontFaceStencil.stencilCompareFunction = backend::CompareFunction::EQUAL; depthStencilDescriptor.backFaceStencil.stencilCompareFunction = backend::CompareFunction::ALWAYS; depthStencilState = device->createDepthStencilState(depthStencilDescriptor); renderPipelineDescriptor.setDepthStencilState(depthStencilState); _renderPipelineTextureFront = device->newRenderPipeline(renderPipelineDescriptor); - + backend::RenderPassDescriptor renderPassDescriptor; renderPassDescriptor.setClearColor(1.0f, 0.1f, 0.1f, 1.f); renderPassDescriptor.setClearDepth(1); diff --git a/test/tests/backend/SubImageBackend.cpp b/test/tests/backend/SubImageBackend.cpp index 70ae8fc..8b36e0a 100644 --- a/test/tests/backend/SubImageBackend.cpp +++ b/test/tests/backend/SubImageBackend.cpp @@ -70,7 +70,7 @@ SubImageBackend::SubImageBackend() vertexLayout.setLayout(2 * sizeof(float), backend::VertexStepMode::VERTEX); renderPipelineDescriptor.setVertexLayout(0, vertexLayout); _renderPipeline = device->newRenderPipeline(renderPipelineDescriptor); - + backend::TextureDescriptor textureDescriptor; textureDescriptor.width = 128; textureDescriptor.height = 128; diff --git a/test/tests/backend/Texture2DBackend.cpp b/test/tests/backend/Texture2DBackend.cpp index f756afe..d15d31b 100644 --- a/test/tests/backend/Texture2DBackend.cpp +++ b/test/tests/backend/Texture2DBackend.cpp @@ -127,7 +127,7 @@ Texture2DBackendTest::Texture2DBackendTest() vertexLayout.setLayout(2 * sizeof(float), cocos2d::backend::VertexStepMode::VERTEX); renderPipelineDescriptor.setVertexLayout(0, vertexLayout); _renderPipeline = device->newRenderPipeline(renderPipelineDescriptor); - + _transform0.translate(-0.5f, 0, 0); _transform0.scale(0.5f); _transform1.translate(0.5f, 0, 0);