review fixes
This commit is contained in:
@@ -1,5 +1,9 @@
|
||||
#include "ShaderCompiler.hpp"
|
||||
|
||||
#include "Base/Logger.hpp"
|
||||
|
||||
#include <filesystem>
|
||||
|
||||
namespace OpenVulkano
|
||||
{
|
||||
Unique<void*> ShaderCompiler::CompileGLSLToSpirv(const std::string& absPath, const std::string& incPath,
|
||||
@@ -8,11 +12,6 @@ namespace OpenVulkano
|
||||
{
|
||||
Array<char> file = Utils::ReadFile(absPath, false, true);
|
||||
|
||||
#if defined(_DEBUG)
|
||||
printf("Compiling shader: %s\n", absPath.c_str());
|
||||
printf("%s\n", file.Data() + 1);
|
||||
#endif
|
||||
|
||||
shaderc::Compiler shaderCompiler;
|
||||
shaderc::CompileOptions options;
|
||||
|
||||
@@ -25,8 +24,7 @@ namespace OpenVulkano
|
||||
|
||||
if (preResult.GetCompilationStatus() != shaderc_compilation_status_success)
|
||||
{
|
||||
throw std::runtime_error("Failed to preprocess shader: " + preResult.GetErrorMessage());
|
||||
return nullptr;
|
||||
throw std::runtime_error("Failed preprocessing shader. Reason: " + preResult.GetErrorMessage());
|
||||
}
|
||||
|
||||
shaderc::CompilationResult result =
|
||||
@@ -34,68 +32,42 @@ namespace OpenVulkano
|
||||
|
||||
if (result.GetCompilationStatus() != shaderc_compilation_status_success)
|
||||
{
|
||||
throw std::runtime_error("Failed to compile shader: " + result.GetErrorMessage());
|
||||
return nullptr;
|
||||
throw std::runtime_error("Failed compiling shader. Reason: " + result.GetErrorMessage());
|
||||
}
|
||||
|
||||
Unique<void*> spirv = std::make_unique<void*>((void*) result.begin());
|
||||
return spirv;
|
||||
}
|
||||
|
||||
ShaderIncluder::ShaderIncluder(const std::string& path)
|
||||
{
|
||||
// TODO : Add the path to your include folder
|
||||
m_includes.push_back(path);
|
||||
}
|
||||
ShaderIncluder::ShaderIncluder(const std::string& path) : m_includePath(path) {}
|
||||
|
||||
shaderc_include_result* ShaderIncluder::GetInclude(const char* requested_source, shaderc_include_type type,
|
||||
const char* requesting_source, uint64_t include_depth)
|
||||
shaderc_include_result* ShaderIncluder::GetInclude(const char* requestedSource, shaderc_include_type type,
|
||||
const char* requestingSource, uint64_t includeDepth)
|
||||
{
|
||||
const char* includePath = GetActualPath(requested_source);
|
||||
const char* content = RecordInclude(includePath);
|
||||
IncludeData* includeData = new IncludeData();
|
||||
includeData->m_fullPath = ResolveInclude(requestedSource);
|
||||
includeData->m_content = Utils::ReadFile(includeData->m_fullPath, false, false); // using nullTerminate as true causes crash in here. Needed to use as false.
|
||||
|
||||
shaderc_include_result* result = new shaderc_include_result();
|
||||
result->content = content;
|
||||
result->content_length = strlen(content);
|
||||
result->source_name = includePath;
|
||||
result->source_name_length = strlen(includePath);
|
||||
result->user_data = nullptr;
|
||||
shaderc_include_result* result = &includeData->result;
|
||||
result->content = includeData->m_content.Data();
|
||||
result->content_length = includeData->m_content.Size();
|
||||
result->source_name = includeData->m_fullPath.data();
|
||||
result->source_name_length = includeData->m_fullPath.size();
|
||||
result->user_data = includeData;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void ShaderIncluder::ReleaseInclude(shaderc_include_result* data) { delete data; }
|
||||
|
||||
std::string ShaderIncluder::ResolveInclude(const std::string requestedSource)
|
||||
void ShaderIncluder::ReleaseInclude(shaderc_include_result* data)
|
||||
{
|
||||
// Check if the file exists in the include paths
|
||||
for (const std::string& includePath: m_includes)
|
||||
{
|
||||
std::string path = includePath + requestedSource;
|
||||
// TODO: Need to check if the folder exists
|
||||
return path;
|
||||
}
|
||||
|
||||
return "";
|
||||
delete static_cast<IncludeData*>(data->user_data);
|
||||
}
|
||||
|
||||
const char* ShaderIncluder::RecordInclude(const std::string& requestedSource)
|
||||
std::string ShaderIncluder::ResolveInclude(const std::string& requestedSource) const
|
||||
{
|
||||
Array<char> data = Utils::ReadFile(requestedSource);
|
||||
|
||||
char* result = new char[data.Size() + 1];
|
||||
strcpy_s(result, data.Size(), data.Data());
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
const char* ShaderIncluder::GetActualPath(const std::string& requestedSource)
|
||||
{
|
||||
std::string includePath = ResolveInclude(requestedSource);
|
||||
|
||||
char* result = new char[includePath.size() + 1];
|
||||
strcpy_s(result, includePath.size(), includePath.c_str());
|
||||
|
||||
return result;
|
||||
// Check if the file exists in the include path
|
||||
std::string path = m_includePath + requestedSource;
|
||||
if (std::filesystem::exists(path)) return path;
|
||||
else throw std::runtime_error("Failed to resolve include '" + requestedSource + "'!");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user