Skip to content

Commit

Permalink
Got Shaders Compiling
Browse files Browse the repository at this point in the history
- Added SPIRV-Cross to compile SPIRV into glsl
  • Loading branch information
tomheeleynz committed Nov 13, 2022
1 parent aaa657d commit 0d3f7c3
Show file tree
Hide file tree
Showing 10 changed files with 108 additions and 20 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,6 @@
[submodule "Arcane/vendor/assimp"]
path = Arcane/vendor/assimp
url = https://github.com/tomheeleynz/assimp.git
[submodule "Arcane/vendor/SPIRV-Cross"]
path = Arcane/vendor/SPIRV-Cross
url = https://github.com/KhronosGroup/SPIRV-Cross.git
4 changes: 2 additions & 2 deletions Arcane/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ if (WIN32 OR (UNIX AND NOT APPLE))
add_subdirectory("vendor/SPIRV-Reflect")

# Set Libraries
set(LIBS glfw imgui ${Vulkan_LIBRARIES} assimp SPIRV-REFLECT glad ${OPENGL_LIBRARIES})
set(LIBS glfw imgui ${Vulkan_LIBRARIES} assimp SPIRV-REFLECT glad ${OPENGL_LIBRARIES} spirv-cross-glsl)

# Set Vulkan engine files
file(GLOB VULKAN_FILES "src/Arcane/Platform/Vulkan/*.h" "src/Arcane/Platform/Vulkan/*.cpp")
Expand Down Expand Up @@ -61,7 +61,7 @@ add_library(
)

# Link Source Directories
target_include_directories(${PROJECT_NAME} PUBLIC "src" "vendor/glm" "vendor/stb" "vendor/entt/single_include" "vendor/json/single_include" "vendor/dotnet" ${OPENGL_INCLUDE_DIRS})
target_include_directories(${PROJECT_NAME} PUBLIC "src" "vendor/glm" "vendor/stb" "vendor/entt/single_include" "vendor/json/single_include" "vendor/dotnet" ${OPENGL_INCLUDE_DIRS} "vendor/SPIRV-Cross")

# Link Libraries
target_link_libraries(${PROJECT_NAME} ${LIBS})
Expand Down
8 changes: 8 additions & 0 deletions Arcane/src/Arcane/Platform/OpenGL/OpenGLPipeline.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include "OpenGLPipeline.h"

namespace Arcane
{
OpenGLPipeline::OpenGLPipeline(PipelineSpecification& spec)
{
}
}
13 changes: 13 additions & 0 deletions Arcane/src/Arcane/Platform/OpenGL/OpenGLPipeline.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#pragma once
#include "Arcane/Renderer/Pipeline.h"

namespace Arcane
{
class OpenGLPipeline : public Pipeline
{
public:
OpenGLPipeline(PipelineSpecification& spec);
private:

};
}
83 changes: 70 additions & 13 deletions Arcane/src/Arcane/Platform/OpenGL/OpenGLShader.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#include "OpenGLShader.h"

#include <glad/glad.h>
#include <iostream>

namespace Arcane
{
static std::vector<char> readFile(const std::string& filename)
static std::vector<uint32_t> readFile(const std::string& filename)
{
std::ifstream file(filename, std::ios::ate | std::ios::binary);

Expand All @@ -18,19 +19,33 @@ namespace Arcane
file.read(buffer.data(), fileSize);
file.close();

return buffer;
std::vector<uint32_t> test;
for (int i = 0; i < buffer.size() - 3; i += 4) {
uint32_t num = (uint32_t)buffer[i] << 24 |
(uint32_t)buffer[i + 1] << 16 |
(uint32_t)buffer[i + 2] << 8 |
(uint32_t)buffer[i + 3];

test.push_back(num);
}

return test;
}

OpenGLShader::OpenGLShader(std::string vertexShader, std::string fragmentShader)
{
// Vertex Shader
GLuint vertexProgram = 0;
// Setup Vertex Source
{
auto vertexSpirv = readFile(vertexShader);
uint32_t vertexShaderProgram = glCreateShader(GL_VERTEX_SHADER);
glShaderBinary(1, &vertexShaderProgram, GL_SHADER_BINARY_FORMAT_SPIR_V, vertexSpirv.data(), vertexSpirv.size());
std::string vsEntryPoint = "main";
spirv_cross::CompilerGLSL glsl(std::move(vertexSpirv));
std::string source = glsl.compile();

glSpecializeShader(vertexShaderProgram, (const GLchar*)vsEntryPoint.c_str(), 0, nullptr, nullptr);
GLuint vertexShaderProgram = glCreateShader(GL_VERTEX_SHADER);
const GLchar* shaderSource = (const GLchar*)source.c_str();

glShaderSource(vertexShaderProgram, 1, &shaderSource, 0);
glCompileShader(vertexShaderProgram);

GLint isCompiled = 0;
glGetShaderiv(vertexShaderProgram, GL_COMPILE_STATUS, &isCompiled);
Expand All @@ -46,16 +61,23 @@ namespace Arcane
// We don't need the shader anymore.
glDeleteShader(vertexShaderProgram);
}
else {
vertexProgram = vertexShaderProgram;
}
}

// Fragment Shader
GLuint fragmentProgram = 0;
// Setup Fragment Source
{
auto fragmentSpirv = readFile(vertexShader);
uint32_t fragmentShaderProgram = glCreateShader(GL_VERTEX_SHADER);
glShaderBinary(1, &fragmentShaderProgram, GL_SHADER_BINARY_FORMAT_SPIR_V, fragmentSpirv.data(), fragmentSpirv.size());
std::string fsEntryPoint = "main";
auto fragmentSpirv = readFile(fragmentShader);
spirv_cross::CompilerGLSL glsl(std::move(fragmentSpirv));
std::string source = glsl.compile();

GLuint fragmentShaderProgram = glCreateShader(GL_FRAGMENT_SHADER);
const GLchar* shaderSource = (const GLchar*)source.c_str();

glSpecializeShader(fragmentShaderProgram, (const GLchar*)fsEntryPoint.c_str(), 0, nullptr, nullptr);
glShaderSource(fragmentShaderProgram, 1, &shaderSource, 0);
glCompileShader(fragmentShaderProgram);

GLint isCompiled = 0;
glGetShaderiv(fragmentShaderProgram, GL_COMPILE_STATUS, &isCompiled);
Expand All @@ -71,7 +93,42 @@ namespace Arcane
// We don't need the shader anymore.
glDeleteShader(fragmentShaderProgram);
}
else {
fragmentProgram = fragmentShaderProgram;
}
}

// Link Program
m_ShaderProgram = glCreateProgram();

// Attach our shaders to our program
glAttachShader(m_ShaderProgram, vertexProgram);
glAttachShader(m_ShaderProgram, fragmentProgram);

// Link our program
glLinkProgram(m_ShaderProgram);

// Note the different functions here: glGetProgram* instead of glGetShader*.
GLint isLinked = 0;
glGetProgramiv(m_ShaderProgram, GL_LINK_STATUS, (int*)&isLinked);
if (isLinked == GL_FALSE)
{
GLint maxLength = 0;
glGetProgramiv(m_ShaderProgram, GL_INFO_LOG_LENGTH, &maxLength);

std::vector<GLchar> infoLog(maxLength);
glGetProgramInfoLog(m_ShaderProgram, maxLength, &maxLength, &infoLog[0]);

glDeleteProgram(m_ShaderProgram);

glDeleteShader(vertexProgram);
glDeleteShader(fragmentProgram);
}

// Always detach shaders after a successful link.
glDetachShader(m_ShaderProgram, vertexProgram);
glDetachShader(m_ShaderProgram, fragmentProgram);

}

DescriptorSet* OpenGLShader::GetMaterialDescriptor()
Expand Down
1 change: 1 addition & 0 deletions Arcane/src/Arcane/Platform/OpenGL/OpenGLShader.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <string>
#include <fstream>
#include <spirv_glsl.hpp>

namespace Arcane
{
Expand Down
6 changes: 6 additions & 0 deletions Arcane/src/Arcane/Renderer/Pipeline.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
#include "Pipeline.h"
#include "Arcane/Renderer/Renderer.h"

// -- Vulkan Pipeline
#include "Arcane/Platform/Vulkan/VulkanPipeline.h"

// -- OpenGL Pipeline
#include "Arcane/Platform/OpenGL/OpenGLPipeline.h"

namespace Arcane {
Pipeline* Pipeline::Create(PipelineSpecification& spec)
{
switch (RendererAPI::Current())
{
case RendererAPIType::Vulkan: return new VulkanPipeline(spec);
case RendererAPIType::OpenGL: return new OpenGLPipeline(spec);
default: return nullptr;
}
}
Expand Down
1 change: 1 addition & 0 deletions Arcane/vendor/SPIRV-Cross
Submodule SPIRV-Cross added at edd66a
5 changes: 4 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ add_subdirectory(Arcane)
# EnchantingTable Editor
add_subdirectory(EnchantingTable)

# Script COre
# Script Core
add_subdirectory(Arcane-ScriptCore)

# Vendor Libraries
Expand All @@ -26,6 +26,9 @@ add_subdirectory("Arcane/vendor/assimp")
# -- Glad
add_subdirectory("Arcane/vendor/glad")

# -- SPIRV-Cross
add_subdirectory("Arcane/vendor/SPIRV-Cross")

# -- Set Global Properties
set_property(TARGET Arcane PROPERTY CXX_STANDARD 17)
set_property(TARGET EnchantingTable PROPERTY CXX_STANDARD 17)
Expand Down
4 changes: 0 additions & 4 deletions EnchantingTable/src/OpenGLTestLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ OpenGLTestLayer::OpenGLTestLayer()

void OpenGLTestLayer::OnAttach()
{
Arcane::Shader* testShader = Arcane::Shader::Create(
".\\src\\EditorAssets\\Shaders\\MeshVert.spv",
".\\src\\EditorAssets\\Shaders\\MeshTextured.spv"
);
}

void OpenGLTestLayer::OnDetach()
Expand Down

0 comments on commit 0d3f7c3

Please sign in to comment.