axmol/core/renderer/backend/ProgramManager.h

190 lines
6.0 KiB
C
Raw Normal View History

2019-11-23 20:27:39 +08:00
/****************************************************************************
Copyright (c) 2018-2019 Xiamen Yaji Software Co., Ltd.
Copyright (c) 2019-present Axmol Engine contributors (see AUTHORS.md).
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 <string_view>
2023-09-02 19:56:33 +08:00
#include "ProgramStateRegistry.h"
struct XXH64_state_s;
NS_AX_BACKEND_BEGIN
2019-11-23 20:27:39 +08:00
/**
* @addtogroup _backend
* @{
*/
/**
* Cache and reuse program object.
*/
class AX_DLL ProgramManager
2019-11-23 20:27:39 +08:00
{
public:
/** 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
Program* getBuiltinProgram(uint32_t type);
/**
* register a custom program
* @return
* the id of custom program, 0: fail, the id can use by loadProgram
*/
uint64_t registerCustomProgram(std::string_view vsName,
std::string_view fsName,
VertexLayoutType vlt = VertexLayoutType::Unspec,
bool force = false);
/**
* load a builtin/or custom program:
* @param id: the id of program to load, the id value returned by registerCustomProgram
* or builtin programType, whe the id < ProgramType:BUILTIN_COUNT, this function
* is identical to 'getBuiltinProgram'
*/
Program* loadProgram(uint64_t progId);
/**
* @brief Load a program with vsName, fsName as CUSTOM immediately without register
* @param vsName the vertex shader name: custom/xxx_vs
* @param fsName the fragment shader name: custom/xxx_vs
* @param vlt the builtin vertex layout type used for loading program
* @return Program* (nullable)
* @remark the returend program type always ProgramType::CUSTOM_PROGRAM
*/
Program* loadProgram(std::string_view vsName,
std::string_view fsName,
VertexLayoutType vlt = VertexLayoutType::Unspec);
/**
* Unload a program object from cache.
* @param program Specifies the program object to move.
*/
void unloadProgram(Program* program);
2021-12-25 10:04:45 +08:00
/**
* Unload all unused program objects from cache.
*/
void unloadUnusedPrograms();
/**
* Unload all program objects from cache.
*/
void unloadAllPrograms();
2019-11-23 20:27:39 +08:00
/**
* Remove a program object from cache.
* @param program Specifies the program object to move.
*/
AX_DEPRECATED_ATTRIBUTE void removeProgram(Program* prog) { unloadProgram(prog); }
2019-11-23 20:27:39 +08:00
/**
* Remove all unused program objects from cache.
*/
AX_DEPRECATED_ATTRIBUTE void removeUnusedProgram() { unloadUnusedPrograms(); }
2019-11-23 20:27:39 +08:00
/**
* Remove all program objects from cache.
*/
AX_DEPRECATED_ATTRIBUTE void removeAllPrograms() { unloadAllPrograms(); }
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
protected:
ProgramManager();
2022-10-12 19:44:31 +08:00
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();
/**
* register a program
*/
uint64_t registerProgram(uint32_t progType,
std::string_view vsName,
std::string_view fsName,
VertexLayoutType vlt,
bool force = false);
/**
* load a custom program by vsName, fsName, vertexLayout
*/
Program* loadProgram(std::string_view vsName,
std::string_view fsName,
uint32_t progType,
uint64_t progId,
VertexLayoutType vlt);
uint64_t computeProgramId(std::string_view vsName, std::string_view fsName);
struct BuiltinRegInfo
{ // builtin shader name is literal string, so use std::string_view ok
std::string_view vsName;
std::string_view fsName;
VertexLayoutType vlt;
};
BuiltinRegInfo _builtinRegistry[(int)backend::ProgramType::BUILTIN_COUNT];
std::unordered_map<int64_t, BuiltinRegInfo> _customRegistry;
std::unordered_map<int64_t, Program*> _cachedPrograms; ///< The cached program object.
XXH64_state_s* _programIdGen;
static ProgramManager* _sharedProgramManager; ///< A shared instance of the program cache.
2019-11-23 20:27:39 +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
/**
* @alias some feq use types to namespace ax
*/
2022-10-12 19:44:31 +08:00
NS_AX_BEGIN
using ProgramType = ::ax::backend::ProgramType;
using ProgramState = ::ax::backend::ProgramState;
using Program = ::ax::backend::Program;
using VertexLayout = ::ax::backend::VertexLayout;
using VertexLayoutType = ::ax::backend::VertexLayoutType;
using ProgramManager = ::ax::backend::ProgramManager;
using ProgramRegistry = ::ax::backend::ProgramStateRegistry;
2022-10-12 19:44:31 +08:00
NS_AX_END