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"
|
2023-06-11 13:08:08 +08:00
|
|
|
#include "base/Ref.h"
|
|
|
|
#include "platform/PlatformMacros.h"
|
2019-11-23 20:27:39 +08:00
|
|
|
#include "Program.h"
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <unordered_map>
|
2023-08-09 14:37:43 +08:00
|
|
|
#include <string_view>
|
2023-08-09 13:52:28 +08:00
|
|
|
#include "ProgramStateRegistry.h"
|
2023-07-25 09:13:20 +08:00
|
|
|
|
2023-08-09 14:37:43 +08:00
|
|
|
struct XXH64_state_s;
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_BACKEND_BEGIN
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
|
|
|
* @addtogroup _backend
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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:
|
|
|
|
/** 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
|
2023-08-09 14:37:43 +08:00
|
|
|
Program* getBuiltinProgram(uint32_t type);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2023-08-09 14:37:43 +08:00
|
|
|
/*
|
|
|
|
* register a custom program
|
|
|
|
* @returns
|
|
|
|
* 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);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* load a program with vsName, fsName as CUSTOM immediately without register
|
|
|
|
* @params
|
|
|
|
* @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
|
|
|
|
* @returns 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);
|
2023-07-25 09:13:20 +08:00
|
|
|
|
2023-08-09 14:37:43 +08:00
|
|
|
/**
|
|
|
|
* Unload all unused program objects from cache.
|
|
|
|
*/
|
|
|
|
void unloadUnusedPrograms();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unload all program objects from cache.
|
|
|
|
*/
|
|
|
|
void unloadAllPrograms();
|
2020-10-16 16:23:14 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
|
|
|
* Remove a program object from cache.
|
|
|
|
* @param program Specifies the program object to move.
|
|
|
|
*/
|
2023-08-09 14:37:43 +08:00
|
|
|
AX_DEPRECATED_ATTRIBUTE void removeProgram(Program* prog) { unloadProgram(prog); }
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove all unused program objects from cache.
|
|
|
|
*/
|
2023-08-09 14:37:43 +08:00
|
|
|
AX_DEPRECATED_ATTRIBUTE void removeUnusedProgram() { unloadUnusedPrograms(); }
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove all program objects from cache.
|
|
|
|
*/
|
2023-08-09 14:37:43 +08:00
|
|
|
AX_DEPRECATED_ATTRIBUTE void removeAllPrograms() { unloadAllPrograms(); }
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
protected:
|
2023-08-09 14:37:43 +08:00
|
|
|
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();
|
|
|
|
|
2023-08-09 14:37:43 +08:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
};
|
2023-07-25 09:13:20 +08:00
|
|
|
|
2023-08-09 14:37:43 +08:00
|
|
|
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;
|
2020-10-16 16:23:14 +08:00
|
|
|
|
2023-07-25 09:13:20 +08:00
|
|
|
static ProgramManager* _sharedProgramManager; ///< A shared instance of the program cache.
|
2019-11-23 20:27:39 +08:00
|
|
|
};
|
|
|
|
|
2023-08-09 14:37:43 +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
|
|
|
/// @}
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_BACKEND_END
|
2022-10-12 19:44:31 +08:00
|
|
|
|
|
|
|
/**
|
2023-08-09 14:37:43 +08:00
|
|
|
* @alias some feq use types to namespace ax
|
|
|
|
*/
|
2022-10-12 19:44:31 +08:00
|
|
|
|
|
|
|
NS_AX_BEGIN
|
|
|
|
|
2023-08-09 14:37:43 +08:00
|
|
|
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
|