2#include "SShaderGroup.h"
3#include "../GLProgramHandle.h"
4#include "../Loader/XML/XML.h"
6#include "../../Manager/EngineCore.h"
12SShaderGroup::SShaderGroup() {
15SShaderGroup::~SShaderGroup() {
18void SShaderGroup::Exterminate() {
25 root =
XFILE(asset->name_path.c_str()).getRoot();
32 const auto& cse_mat = root->getChild(
"CSESHADER");
33 const auto& cse_shaders = cse_mat.children;
34 for (
const auto& shader : cse_shaders) {
35 const auto& pass = shader.getAttribute(
"pass").value;
36 const auto& vert_path = shader.getAttribute(
"v").value;
37 const auto& frag_path = shader.getAttribute(
"f").value;
39 std::string&& vert =
"";
40 std::string&& frag =
"";
42 std::string hashes =
"";
44 if (shader.hasAttribute(
"localPath")
45 && shader.getAttribute(
"localPath").value ==
"1") {
46 const auto vert_asset_path = asset->path + vert_path;
47 const auto frag_asset_path = asset->path + frag_path;
48 vert = AssetMgr::LoadAssetFile(vert_asset_path);
49 frag = AssetMgr::LoadAssetFile(frag_asset_path);
50 hashes = GetShaderHash(vert_asset_path, frag_asset_path);
52 const auto& resMgr = CORE->GetCore(
ResMgr);
53 const auto& vert_asset = resMgr->GetAssetReference(vert_path);
54 const auto& frag_asset = resMgr->GetAssetReference(frag_path);
56 vert = AssetMgr::LoadAssetFile(vert_asset->name_path);
57 frag = AssetMgr::LoadAssetFile(frag_asset->name_path);
58 hashes = vert_asset->hash + frag_asset->hash;
61 if (vert.empty() || frag.empty() || hashes.empty())
continue;
63 const auto& existHandle = SResource::Get<GLProgramHandle>(hashes);
65 if (existHandle ==
nullptr) {
67 handle->SetName(hashes);
71 m_handles.insert(std::pair<std::string, GLProgramHandle*>(pass, handle));
72 if(pass ==
"geometry") m_geometryHandle = handle;
73 if(pass ==
"forward") m_forwardHandle = handle;
74 if(pass ==
"depthOnly") m_depthOnlyHandle = handle;
80std::string SShaderGroup::GetShaderHash(
const std::string& vert_path,
const std::string& frag_path) {
81 return AssetMgr::GetAssetHash(vert_path) + AssetMgr::GetAssetHash(frag_path);
84const GLProgramHandle* SShaderGroup::GetHandle(
const std::string& pass)
const {
85 return m_handles.at(pass);
89 const std::unordered_map<SMaterial::SMaterialMode, std::string> pass_map = {
90 {SMaterial::NORMAL,
"forward"},
91 {SMaterial::DEFERRED,
"geometry"},
92 {SMaterial::DEPTH_ONLY,
"depthOnly"}
95 return m_handles.at(pass_map.at(
static_cast<SMaterial::SMaterialMode
>(mode)));
static GLProgramHandle * CreateProgramHandle(const GLchar *vertexSource, const GLchar *fragmentSource, GLProgramHandle *handle=nullptr)
Creates a program handle.