12 changed files with 161 additions and 44 deletions
Binary file not shown.
@ -0,0 +1,53 @@
@@ -0,0 +1,53 @@
|
||||
#pragma once |
||||
|
||||
#include "PCH.hpp" |
||||
#include "File.hpp" |
||||
#include "Engine.hpp" |
||||
|
||||
#include <shaderc\shaderc.hpp> |
||||
|
||||
class ShaderModule |
||||
{ |
||||
public: |
||||
|
||||
enum Stage |
||||
{ |
||||
Vertex, |
||||
Fragment, |
||||
Geometry |
||||
}; |
||||
|
||||
ShaderModule(Stage stage); |
||||
ShaderModule(Stage stage, std::string path); |
||||
|
||||
VkShaderModule getVulkanModule() { return vkShaderModule; } |
||||
|
||||
void init(); |
||||
void load(std::string path); |
||||
void compile(); |
||||
void createVulkanModule(); |
||||
|
||||
private: |
||||
|
||||
VkShaderModule vkShaderModule; |
||||
|
||||
Stage stage; |
||||
|
||||
shaderc_shader_kind kind; |
||||
|
||||
std::string stageMacro; |
||||
|
||||
enum Language |
||||
{ |
||||
GLSL, |
||||
SPV, |
||||
UNKNOWN |
||||
} language; |
||||
|
||||
std::string path; |
||||
std::string source; |
||||
std::vector<U32> spvSource; |
||||
|
||||
std::string infoLog; |
||||
std::string debugLog; |
||||
}; |
Binary file not shown.
Binary file not shown.
@ -1,10 +0,0 @@
@@ -1,10 +0,0 @@
|
||||
#version 450 |
||||
#extension GL_ARB_separate_shader_objects : enable |
||||
|
||||
layout(location = 0) in vec3 fragColor; |
||||
|
||||
layout(location = 0) out vec4 outColor; |
||||
|
||||
void main() { |
||||
outColor = vec4(fragColor, 1.0); |
||||
} |
@ -1,22 +0,0 @@
@@ -1,22 +0,0 @@
|
||||
#version 450 |
||||
#extension GL_ARB_separate_shader_objects : enable |
||||
|
||||
out gl_PerVertex { |
||||
vec4 gl_Position; |
||||
}; |
||||
|
||||
layout(binding = 0) uniform UniformBufferObject { |
||||
mat4 model; |
||||
mat4 view; |
||||
mat4 proj; |
||||
} ubo; |
||||
|
||||
layout(location = 0) in vec2 inPosition; |
||||
layout(location = 1) in vec3 inColor; |
||||
|
||||
layout(location = 0) out vec3 fragColor; |
||||
|
||||
void main() { |
||||
gl_Position = ubo.proj * ubo.view * ubo.model * vec4(inPosition, 0.0, 1.0); |
||||
fragColor = inColor; |
||||
} |
Binary file not shown.
Binary file not shown.
@ -0,0 +1,95 @@
@@ -0,0 +1,95 @@
|
||||
#include "Shader.hpp" |
||||
|
||||
ShaderModule::ShaderModule(Stage s) : stage(s) |
||||
{ |
||||
switch (stage) |
||||
{ |
||||
case Vertex: |
||||
kind = shaderc_shader_kind::shaderc_glsl_vertex_shader; stageMacro = "VERTEX"; return; |
||||
case Fragment: |
||||
kind = shaderc_shader_kind::shaderc_glsl_fragment_shader; stageMacro = "FRAGMENT"; return; |
||||
case Geometry: |
||||
kind = shaderc_shader_kind::shaderc_glsl_geometry_shader; stageMacro = "GEOMETRY"; return; |
||||
} |
||||
} |
||||
|
||||
ShaderModule::ShaderModule(Stage s, std::string path) : ShaderModule(s) |
||||
{ |
||||
load(path); |
||||
} |
||||
|
||||
void ShaderModule::load(std::string path) |
||||
{ |
||||
int i = 0; |
||||
while (path[i] != '.') |
||||
{ |
||||
++i; |
||||
if (i > path.length()) |
||||
{ |
||||
LOG_WARN("Bad shader file name format: " << path); |
||||
} |
||||
} |
||||
std::string extension; |
||||
extension.assign(&path[i + 1]); |
||||
if (extension == "glsl" || extension == "GLSL") |
||||
{ |
||||
language = GLSL; |
||||
} |
||||
else if (extension == "spv" || extension == "SPV") |
||||
{ |
||||
language = SPV; |
||||
} |
||||
else |
||||
{ |
||||
LOG_WARN("Bad shader file extension: " << path << " - '.glsl' and 'spv' supported"); |
||||
language = UNKNOWN; |
||||
} |
||||
|
||||
File file; |
||||
if (!file.open(path)) |
||||
{ |
||||
LOG_WARN("Can't open shader file: " << path); |
||||
} |
||||
|
||||
if (language == GLSL) |
||||
{ |
||||
source.resize(file.getSize()); |
||||
file.readFile(&source[0]); |
||||
} |
||||
if (language == SPV) |
||||
{ |
||||
spvSource.resize(file.getSize()); |
||||
file.readFile(&spvSource[0]); |
||||
} |
||||
} |
||||
|
||||
void ShaderModule::compile() |
||||
{ |
||||
if (language == GLSL) |
||||
{ |
||||
shaderc::Compiler c; |
||||
shaderc::CompileOptions o; |
||||
o.SetAutoBindUniforms(true); |
||||
o.AddMacroDefinition(stageMacro); |
||||
auto res = c.CompileGlslToSpv(source, kind, path.c_str(), o); |
||||
spvSource.assign(res.begin(), res.end()); |
||||
} |
||||
} |
||||
|
||||
void ShaderModule::createVulkanModule() |
||||
{ |
||||
if (spvSource.size() == 0) |
||||
{ |
||||
LOG_FATAL("SPIRV source missing, cannot compile shader: " << path); |
||||
return; |
||||
} |
||||
VkShaderModuleCreateInfo createInfo = {}; |
||||
createInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO; |
||||
createInfo.codeSize = spvSource.size() * sizeof(int); |
||||
createInfo.pCode = spvSource.data(); |
||||
|
||||
if (vkCreateShaderModule(Engine::renderer->vkLogicalDevice, &createInfo, nullptr, &vkShaderModule) != VK_SUCCESS) |
||||
{ |
||||
LOG_FATAL("Failed to create shader module"); |
||||
} |
||||
} |
Binary file not shown.
Loading…
Reference in new issue