CSEngine
Loading...
Searching...
No Matches
SShaderGroup.cpp
1
2#include "SShaderGroup.h"
3#include "../GLProgramHandle.h"
4#include "../Loader/XML/XML.h"
5#include "ShaderUtil.h"
6#include "../../Manager/EngineCore.h"
7#include "SMaterial.h"
8
9
10using namespace CSE;
11
12SShaderGroup::SShaderGroup() {
13}
14
15SShaderGroup::~SShaderGroup() {
16}
17
18void SShaderGroup::Exterminate() {
19}
20
21void SShaderGroup::Init(const AssetMgr::AssetReference* asset) {
22 const XNode* root;
23
24 try {
25 root = XFILE(asset->name_path.c_str()).getRoot();
26 }
27 catch (int e) {
28// SAFE_DELETE(root);
29 return;
30 }
31
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;
38
39 std::string&& vert = "";
40 std::string&& frag = "";
41
42 std::string hashes = "";
43
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);
51 } else {
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);
55
56 vert = AssetMgr::LoadAssetFile(vert_asset->name_path);
57 frag = AssetMgr::LoadAssetFile(frag_asset->name_path);
58 hashes = vert_asset->hash + frag_asset->hash;
59 }
60
61 if (vert.empty() || frag.empty() || hashes.empty()) continue;
62
63 const auto& existHandle = SResource::Get<GLProgramHandle>(hashes);
64 GLProgramHandle* handle = nullptr;
65 if (existHandle == nullptr) {
66 handle = ShaderUtil::CreateProgramHandle(vert.c_str(), frag.c_str());
67 handle->SetName(hashes);
68 } else {
69 handle = existHandle;
70 }
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;
75 }
76
77 SAFE_DELETE(root);
78}
79
80std::string SShaderGroup::GetShaderHash(const std::string& vert_path, const std::string& frag_path) {
81 return AssetMgr::GetAssetHash(vert_path) + AssetMgr::GetAssetHash(frag_path);
82}
83
84const GLProgramHandle* SShaderGroup::GetHandle(const std::string& pass) const {
85 return m_handles.at(pass);
86}
87
88const GLProgramHandle* SShaderGroup::GetHandleByMode(int mode) const {
89 const std::unordered_map<SMaterial::SMaterialMode, std::string> pass_map = {
90 {SMaterial::NORMAL, "forward"},
91 {SMaterial::DEFERRED, "geometry"},
92 {SMaterial::DEPTH_ONLY, "depthOnly"}
93 };
94
95 return m_handles.at(pass_map.at(static_cast<SMaterial::SMaterialMode>(mode)));
96}
static GLProgramHandle * CreateProgramHandle(const GLchar *vertexSource, const GLchar *fragmentSource, GLProgramHandle *handle=nullptr)
Creates a program handle.
Definition XML.h:77
Definition XML.h:43