This commit is contained in:
halx99 2022-10-12 19:44:31 +08:00
parent 567069236b
commit 8c18f962d5
19 changed files with 110 additions and 71 deletions

View File

@ -60,7 +60,6 @@ THE SOFTWARE.
#include "base/CCAsyncTaskPool.h"
#include "base/ObjectFactory.h"
#include "platform/CCApplication.h"
#include "renderer/backend/ProgramCache.h"
#include "audio/AudioEngine.h"
#if AX_ENABLE_SCRIPT_BINDING
@ -1031,7 +1030,7 @@ void Director::reset()
SpriteFrameCache::destroyInstance();
FileUtils::destroyInstance();
AsyncTaskPool::destroyInstance();
backend::ProgramCache::destroyInstance();
backend::ProgramManager::destroyInstance();
// cocos2d-x specific data structures
UserDefault::destroyInstance();

View File

@ -2,7 +2,6 @@
#include "math/Mat3.h"
#include "2d/CCNode.h"
#include "renderer/ccShaders.h"
#include "renderer/backend/ProgramCache.h"
#include "renderer/backend/Device.h"
NS_AX_BEGIN

View File

@ -32,6 +32,7 @@
#include "platform/CCPlatformMacros.h"
#include "renderer/CCRenderCommand.h"
#include "renderer/backend/Types.h"
#include "renderer/backend/ProgramManager.h"
/**
* @addtogroup renderer

View File

@ -18,6 +18,9 @@ set(_AX_RENDERER_HEADER
renderer/CCTextureCube.h
renderer/CCTrianglesCommand.h
renderer/ccShaders.h
renderer/backend/ProgramManager.h
renderer/backend/ProgramStateRegistry.h
renderer/backend/Backend.h
renderer/backend/Buffer.h
@ -25,7 +28,7 @@ set(_AX_RENDERER_HEADER
renderer/backend/DepthStencilState.h
renderer/backend/Device.h
renderer/backend/Program.h
renderer/backend/ProgramCache.h
renderer/backend/Macros.h
renderer/backend/RenderPassDescriptor.h
renderer/backend/RenderPipeline.h
@ -36,7 +39,6 @@ set(_AX_RENDERER_HEADER
renderer/backend/Enums.h
renderer/backend/VertexLayout.h
renderer/backend/ProgramState.h
renderer/backend/ProgramStateRegistry.h
renderer/backend/ShaderCache.h
renderer/backend/DeviceInfo.h
)
@ -60,6 +62,9 @@ set(_AX_RENDERER_SRC
renderer/CCTrianglesCommand.cpp
renderer/ccShaders.cpp
renderer/CCColorizer.cpp
renderer/backend/ProgramManager.cpp
renderer/backend/ProgramStateRegistry.cpp
renderer/backend/CommandBuffer.cpp
renderer/backend/DepthStencilState.cpp
@ -69,10 +74,8 @@ set(_AX_RENDERER_SRC
renderer/backend/PixelFormatUtils.cpp
renderer/backend/Types.cpp
renderer/backend/VertexLayout.cpp
renderer/backend/ProgramCache.cpp
renderer/backend/Program.cpp
renderer/backend/ProgramState.cpp
renderer/backend/ProgramStateRegistry.cpp
renderer/backend/ShaderCache.cpp
renderer/backend/RenderPassDescriptor.cpp
)

View File

@ -29,7 +29,6 @@
#include "RenderPassDescriptor.h"
#include "Texture.h"
#include "DepthStencilState.h"
#include "ProgramCache.h"
#include "ShaderCache.h"
#include "DeviceInfo.h"
@ -46,6 +45,8 @@ class RenderPipeline;
class RenderPass;
class RenderTarget;
class ProgramManager;
/**
* @addtogroup _backend
* @{
@ -57,7 +58,7 @@ class RenderTarget;
class AX_DLL Device : public ax::Ref
{
public:
friend class ProgramCache;
friend class ProgramManager;
friend class ShaderCache;
/**

View File

@ -23,7 +23,7 @@
****************************************************************************/
#include "Program.h"
#include "ProgramCache.h"
#include "ProgramManager.h"
#include "VertexLayout.h"
NS_AX_BACKEND_BEGIN
@ -43,7 +43,7 @@ void Program::setProgramType(uint32_t type)
Program* Program::getBuiltinProgram(uint32_t type)
{
return ProgramCache::getInstance()->getBuiltinProgram(type);
return ProgramManager::getInstance()->getBuiltinProgram(type);
}
NS_AX_BACKEND_END

View File

@ -38,6 +38,7 @@ NS_AX_BACKEND_BEGIN
class ShaderModule;
class VertexLayout;
class ProgramManager;
/**
* @addtogroup _backend
@ -185,7 +186,7 @@ protected:
virtual const std::unordered_map<std::string, int> getAllUniformsLocation() const = 0;
friend class ProgramState;
#endif
friend class ProgramCache;
friend class ProgramManager;
std::string _vertexShader; ///< Vertex shader.
std::string _fragmentShader; ///< Fragment shader.

View File

@ -23,7 +23,7 @@
THE SOFTWARE.
****************************************************************************/
#include "ProgramCache.h"
#include "ProgramManager.h"
#include "Device.h"
#include "ShaderModule.h"
#include "renderer/ccShaders.h"
@ -50,34 +50,44 @@ std::string getShaderMacrosForLight()
}
} // namespace
ProgramCache* ProgramCache::_sharedProgramCache = nullptr;
ProgramManager* ProgramManager::_sharedProgramManager = nullptr;
ProgramCache* ProgramCache::getInstance()
Program* ProgramManager::newProgram(std::string_view vertShaderSource,
std::string_view fragShaderSource,
std::function<void(Program*)> fnSetupLayout)
{
if (!_sharedProgramCache)
auto program = Device::getInstance()->newProgram(vertShaderSource, fragShaderSource);
if (program)
fnSetupLayout(program);
return program;
}
ProgramManager* ProgramManager::getInstance()
{
if (!_sharedProgramManager)
{
_sharedProgramCache = new ProgramCache();
if (!_sharedProgramCache->init())
_sharedProgramManager = new ProgramManager();
if (!_sharedProgramManager->init())
{
AX_SAFE_DELETE(_sharedProgramCache);
AX_SAFE_DELETE(_sharedProgramManager);
}
}
return _sharedProgramCache;
return _sharedProgramManager;
}
void ProgramCache::destroyInstance()
void ProgramManager::destroyInstance()
{
AX_SAFE_RELEASE_NULL(_sharedProgramCache);
AX_SAFE_RELEASE_NULL(_sharedProgramManager);
}
ProgramCache::~ProgramCache()
ProgramManager::~ProgramManager()
{
for (auto&& program : _cachedPrograms)
{
AX_SAFE_RELEASE(program.second);
}
AXLOGINFO("deallocing ProgramCache: %p", this);
ShaderCache::destroyInstance();
AXLOGINFO("deallocing ProgramManager: %p", this);
backend::ShaderCache::destroyInstance();
}
/*
@ -215,7 +225,7 @@ void VertexLayoutHelper::setupTerrain3D(Program* program)
// ### end of vertex layout setup functions
bool ProgramCache::init()
bool ProgramManager::init()
{
registerProgramFactory(ProgramType::POSITION_TEXTURE_COLOR, positionTextureColor_vert, positionTextureColor_frag,
VertexLayoutHelper::setupSprite);
@ -299,12 +309,12 @@ bool ProgramCache::init()
return true;
}
backend::Program* ProgramCache::getCustomProgram(uint32_t type) const
Program* ProgramManager::getCustomProgram(uint32_t type) const
{
return getBuiltinProgram(type | ProgramType::CUSTOM_PROGRAM);
}
backend::Program* ProgramCache::getBuiltinProgram(uint32_t type) const
Program* ProgramManager::getBuiltinProgram(uint32_t type) const
{
auto iter = _cachedPrograms.find(type);
if (iter != _cachedPrograms.end())
@ -313,7 +323,7 @@ backend::Program* ProgramCache::getBuiltinProgram(uint32_t type) const
return addProgram(type);
}
Program* ProgramCache::addProgram(uint32_t internalType) const
Program* ProgramManager::addProgram(uint32_t internalType) const
{
Program* program = nullptr;
if (internalType < ProgramType::BUILTIN_COUNT)
@ -342,7 +352,7 @@ Program* ProgramCache::addProgram(uint32_t internalType) const
return program;
}
void ProgramCache::registerCustomProgramFactory(uint32_t type,
void ProgramManager::registerCustomProgramFactory(uint32_t type,
std::string vertShaderSource,
std::string fragShaderSource,
std::function<void(Program*)> fnSetupLayout)
@ -352,7 +362,7 @@ void ProgramCache::registerCustomProgramFactory(uint32_t type,
std::move(fnSetupLayout));
}
void ProgramCache::registerProgramFactory(uint32_t internalType,
void ProgramManager::registerProgramFactory(uint32_t internalType,
std::string&& vertShaderSource,
std::string&& fragShaderSource,
std::function<void(Program*)> fnSetupLayout)
@ -378,7 +388,7 @@ void ProgramCache::registerProgramFactory(uint32_t internalType,
}
}
void ProgramCache::removeProgram(backend::Program* program)
void ProgramManager::removeProgram(Program* program)
{
if (!program)
{
@ -398,7 +408,7 @@ void ProgramCache::removeProgram(backend::Program* program)
}
}
void ProgramCache::removeUnusedProgram()
void ProgramManager::removeUnusedProgram()
{
for (auto iter = _cachedPrograms.cbegin(); iter != _cachedPrograms.cend();)
{
@ -416,7 +426,7 @@ void ProgramCache::removeUnusedProgram()
}
}
void ProgramCache::removeAllPrograms()
void ProgramManager::removeAllPrograms()
{
ProgramStateRegistry::getInstance()->clearPrograms();
for (auto&& program : _cachedPrograms)

View File

@ -58,32 +58,37 @@ struct AX_DLL VertexLayoutHelper
/**
* Cache and reuse program object.
*/
class ProgramCache : public Ref
class AX_DLL ProgramManager : public Ref
{
public:
/** new progrma with vertexLayout setup support, user should use this API */
static Program* newProgram(std::string_view vertShaderSource,
std::string_view fragShaderSource,
std::function<void(Program*)> fnSetupLayout = VertexLayoutHelper::setupDummy);
/** returns the shared instance */
static ProgramCache* getInstance();
static ProgramManager* getInstance();
/** purges the cache. It releases the retained instance. */
static void destroyInstance();
/// get built-in program
backend::Program* getBuiltinProgram(uint32_t type) const;
Program* getBuiltinProgram(uint32_t type) const;
// get custom program, should call registerCustomProgramFactory first
backend::Program* getCustomProgram(uint32_t type) const;
Program* getCustomProgram(uint32_t type) const;
// register custom program create factory
void registerCustomProgramFactory(uint32_t type,
std::string vertShaderSource,
std::string fragShaderSource,
std::function<void(Program*)> fnSetupLayout = VertexLayoutHelper::setupDummy);
std::function<void(Program*)> fnSetupLayout = VertexLayoutHelper::setupDummy);
/**
* Remove a program object from cache.
* @param program Specifies the program object to move.
*/
void removeProgram(backend::Program* program);
void removeProgram(Program* program);
/**
* Remove all unused program objects from cache.
@ -96,8 +101,8 @@ public:
void removeAllPrograms();
protected:
ProgramCache() = default;
virtual ~ProgramCache();
ProgramManager() = default;
virtual ~ProgramManager();
/**
* Pre-load programs into cache.
@ -110,13 +115,30 @@ protected:
std::function<void(Program*)> fnSetupLayout);
Program* addProgram(uint32_t internalType) const;
std::function<Program*()> _builtinFactories[(int)ProgramType::BUILTIN_COUNT];
std::function<Program*()> _builtinFactories[(int)backend::ProgramType::BUILTIN_COUNT];
std::unordered_map<uint32_t, std::function<Program*()>> _customFactories;
mutable std::unordered_map<uint32_t, backend::Program*> _cachedPrograms; ///< The cached program object.
static ProgramCache* _sharedProgramCache; ///< A shared instance of the program cache.
mutable std::unordered_map<uint32_t, Program*> _cachedPrograms; ///< The cached program object.
static ProgramManager* _sharedProgramManager; ///< A shared instance of the program cache.
};
using ProgramCache = ProgramManager; // for compatible
// end of _backend group
/// @}
NS_AX_BACKEND_END
/**
* @alias some feq use types to namespace ax
*/
NS_AX_BEGIN
using ProgramType = ::ax::backend::ProgramType;
using Program = ::ax::backend::Program;
using VertexLayout = ::ax::backend::VertexLayout;
using VertexLayoutHelper = ::ax::backend::VertexLayoutHelper;
using ProgramManager = ::ax::backend::ProgramManager;
using ProgramRegistry = ::ax::backend::ProgramStateRegistry;
NS_AX_END

View File

@ -24,7 +24,6 @@
****************************************************************************/
#include "renderer/backend/ProgramState.h"
#include "renderer/backend/ProgramCache.h"
#include "renderer/backend/Program.h"
#include "renderer/backend/Texture.h"
#include "renderer/backend/Types.h"

View File

@ -9,12 +9,13 @@
#include <unordered_map>
NS_AX_BACKEND_BEGIN
/**
* @addtogroup _backend
* @{
* #todo: Rename to ProgramStateRegistry
*/
class ProgramStateRegistry : public Ref
class AX_DLL ProgramStateRegistry : public Ref
{
public:
/** returns the shared instance */

View File

@ -54,7 +54,7 @@ ShaderCache::~ShaderCache()
{
AX_SAFE_RELEASE(shaderModule.second);
}
AXLOGINFO("deallocing ProgramCache: %p", this);
AXLOGINFO("deallocing ShaderCache: %p", this);
}
bool ShaderCache::init()

View File

@ -35,6 +35,8 @@
#include "base/ccMacros.h"
#include "renderer/backend/ProgramManager.h"
NS_AX_BACKEND_BEGIN
CAMetalLayer* DeviceMTL::_metalLayer = nil;
@ -80,7 +82,7 @@ DeviceMTL::DeviceMTL()
DeviceMTL::~DeviceMTL()
{
ProgramCache::destroyInstance();
ProgramManager::destroyInstance();
delete _deviceInfo;
_deviceInfo = nullptr;
}

View File

@ -33,6 +33,8 @@
#include "DeviceInfoGL.h"
#include "RenderTargetGL.h"
#include "renderer/backend/ProgramManager.h"
NS_AX_BACKEND_BEGIN
Device* Device::getInstance()
@ -57,7 +59,7 @@ DeviceGL::DeviceGL()
DeviceGL::~DeviceGL()
{
ProgramCache::destroyInstance();
ProgramManager::destroyInstance();
delete _deviceInfo;
_deviceInfo = nullptr;
}

View File

@ -35,7 +35,6 @@
# include "platform/CCFileUtils.h"
# include "ui/UIHelper.h"
# include "ui/UIVideoPlayer/MFMediaPlayer.h"
# include "renderer/backend/ProgramCache.h"
# include "yasio/detail/byte_buffer.hpp"
# include "ntcvt/ntcvt.hpp"
# include "ui/LayoutHelper.h"
@ -414,7 +413,7 @@ void VideoPlayer::draw(Renderer* renderer, const Mat4& transform, uint32_t flags
{
pvd->_vtexture = new Texture2D();
auto programCache = backend::ProgramCache::getInstance();
auto programManager = ProgramManager::getInstance();
auto& sampleOutFormat = pvd->_vplayer->GetVideoOutputFormat();
@ -430,11 +429,11 @@ void VideoPlayer::draw(Renderer* renderer, const Mat4& transform, uint32_t flags
case VideoSampleFormat::YUY2:
case VideoSampleFormat::NV12:
{
programCache->registerCustomProgramFactory(
programManager->registerCustomProgramFactory(
VIDEO_PROGRAM_ID, positionTextureColor_vert,
std::string{pvd->_sampleFormat == VideoSampleFormat::NV12 ? NV12_FRAG : YUY2_FRAG},
backend::VertexLayoutHelper::setupSprite);
auto program = programCache->getCustomProgram(VIDEO_PROGRAM_ID);
VertexLayoutHelper::setupSprite);
auto program = programManager->getCustomProgram(VIDEO_PROGRAM_ID);
pvd->_vrender->setProgramState(new backend::ProgramState(program), false);
break;
}

View File

@ -1255,7 +1255,7 @@ void FogTestDemo::onEnter()
auto vertexSource = FileUtils::getInstance()->getStringFromFile("MeshRendererTest/fog.vert");
auto fragSource = FileUtils::getInstance()->getStringFromFile("MeshRendererTest/fog.frag");
auto program = backend::Device::getInstance()->newProgram(vertexSource, fragSource);
auto program = ProgramManager::newProgram(vertexSource, fragSource);
_programState1 = new backend::ProgramState(program);
_programState2 = new backend::ProgramState(program);
AX_SAFE_RELEASE(program);
@ -1305,7 +1305,7 @@ void FogTestDemo::onEnter()
auto vertexSource = FileUtils::getInstance()->getStringFromFile("MeshRendererTest/fog.vert");
auto fragSource = FileUtils::getInstance()->getStringFromFile("MeshRendererTest/fog.frag");
auto program = backend::Device::getInstance()->newProgram(vertexSource, fragSource);
auto program = ProgramManager::newProgram(vertexSource, fragSource);
_programState1 = new backend::ProgramState(program);
_programState2 = new backend::ProgramState(program);

View File

@ -74,14 +74,14 @@ private:
NewRendererTests::NewRendererTests()
{
auto programCache = backend::ProgramCache::getInstance();
programCache->registerCustomProgramFactory(CustomProgramType::BLUR, positionTextureColor_vert,
auto programManager = ProgramManager::getInstance();
programManager->registerCustomProgramFactory(CustomProgramType::BLUR, positionTextureColor_vert,
FileUtils::getInstance()->getStringFromFile("Shaders/example_Blur.fsh"),
backend::VertexLayoutHelper::setupSprite);
programCache->registerCustomProgramFactory(
VertexLayoutHelper::setupSprite);
programManager->registerCustomProgramFactory(
CustomProgramType::SEPIA, positionTextureColor_vert,
FileUtils::getInstance()->getStringFromFile("Shaders/example_Sepia.fsh"),
backend::VertexLayoutHelper::setupSprite);
VertexLayoutHelper::setupSprite);
ADD_TEST_CASE(NewSpriteTest);
ADD_TEST_CASE(GroupCommandTest);
@ -859,7 +859,7 @@ RendererUniformBatch::RendererUniformBatch()
axis::backend::ProgramState* RendererUniformBatch::createBlurProgramState()
{
auto programState =
new backend::ProgramState(backend::ProgramCache::getInstance()->getCustomProgram(CustomProgramType::BLUR));
new backend::ProgramState(ProgramManager::getInstance()->getCustomProgram(CustomProgramType::BLUR));
programState->autorelease();
backend::UniformLocation loc = programState->getUniformLocation("resolution");
@ -880,7 +880,7 @@ axis::backend::ProgramState* RendererUniformBatch::createBlurProgramState()
axis::backend::ProgramState* RendererUniformBatch::createSepiaProgramState()
{
auto programState =
new backend::ProgramState(backend::ProgramCache::getInstance()->getCustomProgram(CustomProgramType::SEPIA));
new backend::ProgramState(ProgramManager::getInstance()->getCustomProgram(CustomProgramType::SEPIA));
// programState->updateUniformID();
@ -933,7 +933,7 @@ RendererUniformBatch2::RendererUniformBatch2()
backend::ProgramState* RendererUniformBatch2::createBlurProgramState()
{
auto programState =
new backend::ProgramState(backend::ProgramCache::getInstance()->getCustomProgram(CustomProgramType::BLUR));
new backend::ProgramState(ProgramManager::getInstance()->getCustomProgram(CustomProgramType::BLUR));
backend::UniformLocation loc = programState->getUniformLocation("resolution");
auto resolution = Vec2(85, 121);
@ -953,7 +953,7 @@ backend::ProgramState* RendererUniformBatch2::createBlurProgramState()
backend::ProgramState* RendererUniformBatch2::createSepiaProgramState()
{
auto programState =
new backend::ProgramState(backend::ProgramCache::getInstance()->getCustomProgram(CustomProgramType::SEPIA));
new backend::ProgramState(ProgramManager::getInstance()->getCustomProgram(CustomProgramType::SEPIA));
programState->autorelease();
return programState;
}

View File

@ -142,7 +142,7 @@ void ShaderNode::loadShaderVertex(std::string_view vert, std::string_view frag)
std::string vertexFilePath = fileUtiles->fullPathForFilename(vert);
vertSource = fileUtiles->getStringFromFile(vertexFilePath);
}
auto program = backend::Device::getInstance()->newProgram(vertSource.c_str(), fragSource.c_str());
auto program = ProgramManager::newProgram(vertSource, fragSource, VertexLayoutHelper::setupSprite);
auto programState = new backend::ProgramState(program);
setProgramState(programState);
AX_SAFE_RELEASE(programState);
@ -451,7 +451,7 @@ void SpriteBlur::initProgram()
std::string fragSource = FileUtils::getInstance()->getStringFromFile(
FileUtils::getInstance()->fullPathForFilename("Shaders/example_Blur.fsh"));
auto program = backend::Device::getInstance()->newProgram(positionTextureColor_vert, fragSource.data());
auto program = ProgramManager::newProgram(positionTextureColor_vert, fragSource, VertexLayoutHelper::setupSprite);
auto programState = new backend::ProgramState(program);
setProgramState(programState);
AX_SAFE_RELEASE(programState);
@ -584,7 +584,7 @@ bool ShaderRetroEffect::init()
FileUtils::getInstance()->fullPathForFilename("Shaders/example_HorizontalColor.fsh"));
char* fragSource = (char*)fragStr.c_str();
auto program = backend::Device::getInstance()->newProgram(positionTextureColor_vert, fragSource);
auto program = ProgramManager::newProgram(positionTextureColor_vert, fragSource, VertexLayoutHelper::setupSprite);
auto p = new backend::ProgramState(program);
auto director = Director::getInstance();
const auto& screenSizeLocation = p->getUniformLocation("u_screenSize");
@ -767,7 +767,7 @@ bool ShaderMultiTexture::init()
auto* fu = FileUtils::getInstance();
auto vertexShader = fu->getStringFromFile("Shaders/example_MultiTexture.vsh");
auto fragmentShader = fu->getStringFromFile("Shaders/example_MultiTexture.fsh");
auto program = backend::Device::getInstance()->newProgram(vertexShader.c_str(), fragmentShader.c_str());
auto program = ProgramManager::newProgram(vertexShader, fragmentShader, VertexLayoutHelper::setupSprite);
auto programState = new backend::ProgramState(program);
_sprite->setProgramState(programState);

View File

@ -190,7 +190,7 @@ bool Effect::initProgramState(std::string_view fragmentFilename)
#if (AX_TARGET_PLATFORM == AX_PLATFORM_ANDROID)
_fragSource = fragSource;
#endif
auto program = backend::Device::getInstance()->newProgram(positionTextureColor_vert, fragSource.c_str());
auto program = ProgramManager::newProgram(positionTextureColor_vert, fragSource, VertexLayoutHelper::setupSprite);
auto programState = new backend::ProgramState(program);
AX_SAFE_RELEASE(_programState);
AX_SAFE_RELEASE(program);