Fix OpenGL device initialization and file shaders
This commit is contained in:
@@ -3,10 +3,85 @@
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
namespace XCEngine {
|
||||
namespace RHI {
|
||||
|
||||
namespace {
|
||||
|
||||
bool EndsWith(const std::string& value, const char* suffix) {
|
||||
const size_t suffixLength = strlen(suffix);
|
||||
return value.size() >= suffixLength && value.compare(value.size() - suffixLength, suffixLength, suffix) == 0;
|
||||
}
|
||||
|
||||
std::string NarrowAscii(const std::wstring& value) {
|
||||
std::string result;
|
||||
result.reserve(value.size());
|
||||
for (wchar_t ch : value) {
|
||||
result.push_back(static_cast<char>(ch));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
bool ResolveShaderType(const std::string& path, const char* target, ShaderType& type) {
|
||||
if (target != nullptr) {
|
||||
if (strstr(target, "vs_") != nullptr) {
|
||||
type = ShaderType::Vertex;
|
||||
return true;
|
||||
}
|
||||
if (strstr(target, "ps_") != nullptr || strstr(target, "fs_") != nullptr) {
|
||||
type = ShaderType::Fragment;
|
||||
return true;
|
||||
}
|
||||
if (strstr(target, "gs_") != nullptr) {
|
||||
type = ShaderType::Geometry;
|
||||
return true;
|
||||
}
|
||||
if (strstr(target, "cs_") != nullptr) {
|
||||
type = ShaderType::Compute;
|
||||
return true;
|
||||
}
|
||||
if (strstr(target, "hs_") != nullptr || strstr(target, "tcs") != nullptr) {
|
||||
type = ShaderType::TessControl;
|
||||
return true;
|
||||
}
|
||||
if (strstr(target, "ds_") != nullptr || strstr(target, "tes") != nullptr) {
|
||||
type = ShaderType::TessEvaluation;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
if (EndsWith(path, ".vert") || EndsWith(path, ".vs.glsl")) {
|
||||
type = ShaderType::Vertex;
|
||||
return true;
|
||||
}
|
||||
if (EndsWith(path, ".frag") || EndsWith(path, ".fs.glsl")) {
|
||||
type = ShaderType::Fragment;
|
||||
return true;
|
||||
}
|
||||
if (EndsWith(path, ".geom") || EndsWith(path, ".gs.glsl")) {
|
||||
type = ShaderType::Geometry;
|
||||
return true;
|
||||
}
|
||||
if (EndsWith(path, ".comp") || EndsWith(path, ".cs.glsl")) {
|
||||
type = ShaderType::Compute;
|
||||
return true;
|
||||
}
|
||||
if (EndsWith(path, ".tesc")) {
|
||||
type = ShaderType::TessControl;
|
||||
return true;
|
||||
}
|
||||
if (EndsWith(path, ".tese")) {
|
||||
type = ShaderType::TessEvaluation;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
OpenGLShader::OpenGLShader()
|
||||
: m_program(0), m_uniformsCached(false) {
|
||||
}
|
||||
@@ -211,9 +286,30 @@ bool OpenGLShader::Compile(const char* source, ShaderType type) {
|
||||
}
|
||||
|
||||
bool OpenGLShader::CompileFromFile(const wchar_t* filePath, const char* entryPoint, const char* target) {
|
||||
std::wstring ws(filePath);
|
||||
std::string path(ws.begin(), ws.end());
|
||||
return CompileFromFile(path.c_str(), nullptr);
|
||||
(void)entryPoint;
|
||||
|
||||
if (filePath == nullptr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const std::wstring ws(filePath);
|
||||
const std::string path = NarrowAscii(ws);
|
||||
|
||||
std::ifstream shaderFile(path);
|
||||
if (!shaderFile.is_open()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
std::stringstream shaderStream;
|
||||
shaderStream << shaderFile.rdbuf();
|
||||
|
||||
ShaderType type = ShaderType::Vertex;
|
||||
if (!ResolveShaderType(path, target, type)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const std::string source = shaderStream.str();
|
||||
return Compile(source.c_str(), type);
|
||||
}
|
||||
|
||||
bool OpenGLShader::Compile(const void* sourceData, size_t sourceSize, const char* entryPoint, const char* target) {
|
||||
@@ -340,4 +436,4 @@ bool OpenGLShader::CheckLinkErrors(unsigned int program) {
|
||||
}
|
||||
|
||||
} // namespace RHI
|
||||
} // namespace XCEngine
|
||||
} // namespace XCEngine
|
||||
|
||||
Reference in New Issue
Block a user