axmol/core/renderer/backend/ProgramManager.h

145 lines
4.7 KiB
C
Raw Normal View History

2019-11-23 20:27:39 +08:00
/****************************************************************************
Copyright (c) 2018-2019 Xiamen Yaji Software Co., Ltd.
2022-09-24 10:42:11 +08:00
Copyright (c) 2022 Bytedance Inc.
2019-11-23 20:27:39 +08:00
2022-10-01 16:24:52 +08:00
https://axmolengine.github.io/
2019-11-23 20:27:39 +08:00
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
#pragma once
#include "Macros.h"
#include "base/Ref.h"
#include "platform/PlatformMacros.h"
2019-11-23 20:27:39 +08:00
#include "Program.h"
#include <string>
#include <unordered_map>
#include "ProgramStateRegistry.h"
NS_AX_BACKEND_BEGIN
2019-11-23 20:27:39 +08:00
/**
* @addtogroup _backend
* @{
*/
2022-09-23 22:41:30 +08:00
struct AX_DLL VertexLayoutHelper
{
2022-09-24 10:42:11 +08:00
static void setupDummy(Program*);
static void setupPos(Program*);
static void setupTexture(Program*);
static void setupSprite(Program*);
static void setupDrawNode(Program*);
static void setupDrawNode3D(Program*);
static void setupSkyBox(Program*);
static void setupPU3D(Program*);
static void setupPosColor(Program*);
static void setupTerrain3D(Program*);
2022-09-23 22:41:30 +08:00
};
2019-11-23 20:27:39 +08:00
/**
* Cache and reuse program object.
*/
2022-10-12 19:44:31 +08:00
class AX_DLL ProgramManager : public Ref
2019-11-23 20:27:39 +08:00
{
public:
2022-10-12 19:44:31 +08:00
/** new progrma with vertexLayout setup support, user should use this API */
static Program* newProgram(std::string_view vertShaderSource,
2023-07-15 11:25:48 +08:00
std::string_view fragShaderSource,
std::function<void(Program*)> fnSetupLayout = VertexLayoutHelper::setupDummy);
2022-10-12 19:44:31 +08:00
2019-11-23 20:27:39 +08:00
/** returns the shared instance */
2022-10-12 19:44:31 +08:00
static ProgramManager* getInstance();
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/** purges the cache. It releases the retained instance. */
static void destroyInstance();
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/// get built-in program
2022-10-12 19:44:31 +08:00
Program* getBuiltinProgram(uint32_t type) const;
2021-12-25 10:04:45 +08:00
// get custom program, should call registerCustomProgramFactory first
2022-10-12 19:44:31 +08:00
Program* getCustomProgram(uint32_t type) const;
// register custom program create factory
2022-09-23 22:41:30 +08:00
void registerCustomProgramFactory(uint32_t type,
2023-07-15 11:25:48 +08:00
std::string vertShaderSource,
std::string fragShaderSource,
std::function<void(Program*)> fnSetupLayout = VertexLayoutHelper::setupDummy);
2019-11-23 20:27:39 +08:00
/**
* Remove a program object from cache.
* @param program Specifies the program object to move.
*/
2022-10-12 19:44:31 +08:00
void removeProgram(Program* program);
2019-11-23 20:27:39 +08:00
/**
* Remove all unused program objects from cache.
*/
void removeUnusedProgram();
/**
* Remove all program objects from cache.
*/
void removeAllPrograms();
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
protected:
2022-10-12 19:44:31 +08:00
ProgramManager() = default;
virtual ~ProgramManager();
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* Pre-load programs into cache.
*/
bool init();
2023-07-15 11:25:48 +08:00
void registerProgramFactory(uint32_t internalType,
std::string&& vertShaderSource,
std::string&& fragShaderSource,
std::function<void(Program*)> fnSetupLayout);
Program* addProgram(uint32_t internalType) const;
2022-10-12 19:44:31 +08:00
std::function<Program*()> _builtinFactories[(int)backend::ProgramType::BUILTIN_COUNT];
2021-12-25 10:04:45 +08:00
std::unordered_map<uint32_t, std::function<Program*()>> _customFactories;
2022-10-12 19:44:31 +08:00
mutable std::unordered_map<uint32_t, Program*> _cachedPrograms; ///< The cached program object.
2023-07-15 11:25:48 +08:00
static ProgramManager* _sharedProgramManager; ///< A shared instance of the program cache.
2019-11-23 20:27:39 +08:00
};
2023-07-15 11:25:48 +08:00
using ProgramCache = ProgramManager; // for compatible
2022-10-12 19:44:31 +08:00
2021-12-25 10:04:45 +08:00
// end of _backend group
2019-11-23 20:27:39 +08:00
/// @}
NS_AX_BACKEND_END
2022-10-12 19:44:31 +08:00
/**
2023-07-15 11:25:48 +08:00
* @alias some feq use types to namespace ax
*/
2022-10-12 19:44:31 +08:00
NS_AX_BEGIN
2023-07-15 11:25:48 +08:00
using ProgramType = ::ax::backend::ProgramType;
using Program = ::ax::backend::Program;
using VertexLayout = ::ax::backend::VertexLayout;
2022-10-12 19:44:31 +08:00
using VertexLayoutHelper = ::ax::backend::VertexLayoutHelper;
2023-07-15 11:25:48 +08:00
using ProgramManager = ::ax::backend::ProgramManager;
using ProgramRegistry = ::ax::backend::ProgramStateRegistry;
2022-10-12 19:44:31 +08:00
NS_AX_END