review fixes
This commit is contained in:
@@ -17,8 +17,6 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
#include "Shader/ShaderCompiler.hpp"
|
|
||||||
|
|
||||||
using namespace OpenVulkano;
|
using namespace OpenVulkano;
|
||||||
|
|
||||||
int main(int argc, char** argv)
|
int main(int argc, char** argv)
|
||||||
|
|||||||
@@ -7,9 +7,9 @@
|
|||||||
#include "Utils.hpp"
|
#include "Utils.hpp"
|
||||||
|
|
||||||
#ifdef _MSC_VER
|
#ifdef _MSC_VER
|
||||||
#include <Windows.h>
|
#include <Windows.h>
|
||||||
#else
|
#else
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
#endif
|
#endif
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <string>
|
#include <string>
|
||||||
@@ -57,7 +57,7 @@ namespace OpenVulkano
|
|||||||
#ifdef _MSC_VER
|
#ifdef _MSC_VER
|
||||||
return (uint64_t)::GetThreadId(::GetCurrentThread());
|
return (uint64_t)::GetThreadId(::GetCurrentThread());
|
||||||
#else
|
#else
|
||||||
return (uint64_t)pthread_self();
|
return (uint64_t) pthread_self();
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -69,8 +69,16 @@ namespace OpenVulkano
|
|||||||
if (emptyOnMissing) return {};
|
if (emptyOnMissing) return {};
|
||||||
throw std::runtime_error("Failed to open file '" + filePath + "'!");
|
throw std::runtime_error("Failed to open file '" + filePath + "'!");
|
||||||
}
|
}
|
||||||
size_t fileSize = static_cast<size_t>(file.tellg());
|
const size_t fileSize = static_cast<size_t>(file.tellg());
|
||||||
if (nullTerminateString) fileSize++;
|
if (nullTerminateString)
|
||||||
|
{
|
||||||
|
Array<char> data(fileSize + 1);
|
||||||
|
file.seekg(0);
|
||||||
|
file.read(data.Data(), fileSize);
|
||||||
|
data[fileSize] = '\0';
|
||||||
|
file.close();
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
Array<char> data(fileSize);
|
Array<char> data(fileSize);
|
||||||
file.seekg(0);
|
file.seekg(0);
|
||||||
|
|||||||
@@ -1,5 +1,9 @@
|
|||||||
#include "ShaderCompiler.hpp"
|
#include "ShaderCompiler.hpp"
|
||||||
|
|
||||||
|
#include "Base/Logger.hpp"
|
||||||
|
|
||||||
|
#include <filesystem>
|
||||||
|
|
||||||
namespace OpenVulkano
|
namespace OpenVulkano
|
||||||
{
|
{
|
||||||
Unique<void*> ShaderCompiler::CompileGLSLToSpirv(const std::string& absPath, const std::string& incPath,
|
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);
|
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::Compiler shaderCompiler;
|
||||||
shaderc::CompileOptions options;
|
shaderc::CompileOptions options;
|
||||||
|
|
||||||
@@ -25,8 +24,7 @@ namespace OpenVulkano
|
|||||||
|
|
||||||
if (preResult.GetCompilationStatus() != shaderc_compilation_status_success)
|
if (preResult.GetCompilationStatus() != shaderc_compilation_status_success)
|
||||||
{
|
{
|
||||||
throw std::runtime_error("Failed to preprocess shader: " + preResult.GetErrorMessage());
|
throw std::runtime_error("Failed preprocessing shader. Reason: " + preResult.GetErrorMessage());
|
||||||
return nullptr;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
shaderc::CompilationResult result =
|
shaderc::CompilationResult result =
|
||||||
@@ -34,68 +32,42 @@ namespace OpenVulkano
|
|||||||
|
|
||||||
if (result.GetCompilationStatus() != shaderc_compilation_status_success)
|
if (result.GetCompilationStatus() != shaderc_compilation_status_success)
|
||||||
{
|
{
|
||||||
throw std::runtime_error("Failed to compile shader: " + result.GetErrorMessage());
|
throw std::runtime_error("Failed compiling shader. Reason: " + result.GetErrorMessage());
|
||||||
return nullptr;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Unique<void*> spirv = std::make_unique<void*>((void*) result.begin());
|
Unique<void*> spirv = std::make_unique<void*>((void*) result.begin());
|
||||||
return spirv;
|
return spirv;
|
||||||
}
|
}
|
||||||
|
|
||||||
ShaderIncluder::ShaderIncluder(const std::string& path)
|
ShaderIncluder::ShaderIncluder(const std::string& path) : m_includePath(path) {}
|
||||||
{
|
|
||||||
// TODO : Add the path to your include folder
|
|
||||||
m_includes.push_back(path);
|
|
||||||
}
|
|
||||||
|
|
||||||
shaderc_include_result* ShaderIncluder::GetInclude(const char* requested_source, shaderc_include_type type,
|
shaderc_include_result* ShaderIncluder::GetInclude(const char* requestedSource, shaderc_include_type type,
|
||||||
const char* requesting_source, uint64_t include_depth)
|
const char* requestingSource, uint64_t includeDepth)
|
||||||
{
|
{
|
||||||
const char* includePath = GetActualPath(requested_source);
|
IncludeData* includeData = new IncludeData();
|
||||||
const char* content = RecordInclude(includePath);
|
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();
|
shaderc_include_result* result = &includeData->result;
|
||||||
result->content = content;
|
result->content = includeData->m_content.Data();
|
||||||
result->content_length = strlen(content);
|
result->content_length = includeData->m_content.Size();
|
||||||
result->source_name = includePath;
|
result->source_name = includeData->m_fullPath.data();
|
||||||
result->source_name_length = strlen(includePath);
|
result->source_name_length = includeData->m_fullPath.size();
|
||||||
result->user_data = nullptr;
|
result->user_data = includeData;
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ShaderIncluder::ReleaseInclude(shaderc_include_result* data) { delete data; }
|
void ShaderIncluder::ReleaseInclude(shaderc_include_result* data)
|
||||||
|
|
||||||
std::string ShaderIncluder::ResolveInclude(const std::string requestedSource)
|
|
||||||
{
|
{
|
||||||
// Check if the file exists in the include paths
|
delete static_cast<IncludeData*>(data->user_data);
|
||||||
for (const std::string& includePath: m_includes)
|
|
||||||
{
|
|
||||||
std::string path = includePath + requestedSource;
|
|
||||||
// TODO: Need to check if the folder exists
|
|
||||||
return path;
|
|
||||||
}
|
|
||||||
|
|
||||||
return "";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const char* ShaderIncluder::RecordInclude(const std::string& requestedSource)
|
std::string ShaderIncluder::ResolveInclude(const std::string& requestedSource) const
|
||||||
{
|
{
|
||||||
Array<char> data = Utils::ReadFile(requestedSource);
|
// Check if the file exists in the include path
|
||||||
|
std::string path = m_includePath + requestedSource;
|
||||||
char* result = new char[data.Size() + 1];
|
if (std::filesystem::exists(path)) return path;
|
||||||
strcpy_s(result, data.Size(), data.Data());
|
else throw std::runtime_error("Failed to resolve include '" + requestedSource + "'!");
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,21 +11,25 @@ namespace OpenVulkano
|
|||||||
{
|
{
|
||||||
class ShaderIncluder : public shaderc::CompileOptions::IncluderInterface
|
class ShaderIncluder : public shaderc::CompileOptions::IncluderInterface
|
||||||
{
|
{
|
||||||
|
struct IncludeData
|
||||||
|
{
|
||||||
|
shaderc_include_result result = {};
|
||||||
|
std::string m_fullPath;
|
||||||
|
Array<char> m_content;
|
||||||
|
};
|
||||||
public:
|
public:
|
||||||
ShaderIncluder(const std::string& path);
|
ShaderIncluder(const std::string& path);
|
||||||
~ShaderIncluder() override = default;
|
~ShaderIncluder() override = default;
|
||||||
|
|
||||||
shaderc_include_result* GetInclude(const char* requested_source, shaderc_include_type type,
|
shaderc_include_result* GetInclude(const char* requestedSource, shaderc_include_type type,
|
||||||
const char* requesting_source, uint64_t include_depth) override;
|
const char* requestingSource, uint64_t includeDepth) override;
|
||||||
void ReleaseInclude(shaderc_include_result* data) override;
|
void ReleaseInclude(shaderc_include_result* data) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::string ResolveInclude(const std::string requestedSource);
|
std::string ResolveInclude(const std::string& requestedSource) const;
|
||||||
const char* RecordInclude(const std::string& requestedSource);
|
|
||||||
const char* GetActualPath(const std::string& requestedSource);
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::vector<std::string> m_includes;
|
std::string m_includePath;
|
||||||
};
|
};
|
||||||
|
|
||||||
class ShaderCompiler
|
class ShaderCompiler
|
||||||
|
|||||||
8
openVulkanoCpp/Shader/test.vert
Normal file
8
openVulkanoCpp/Shader/test.vert
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
#version 460
|
||||||
|
#extension GL_KHR_vulkan_glsl : enable
|
||||||
|
|
||||||
|
#include "test1.glsl"
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
};
|
||||||
3
openVulkanoCpp/Shader/test1.glsl
Normal file
3
openVulkanoCpp/Shader/test1.glsl
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
void testfunction()
|
||||||
|
{
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user