Skip to content

Commit

Permalink
New shader system, added Python scripts for retrieving dependencies, …
Browse files Browse the repository at this point in the history
…added uniform buffers, added command line args, and better premake scripts
  • Loading branch information
TheCherno committed Apr 30, 2021
1 parent e9b6488 commit 93eef73
Show file tree
Hide file tree
Showing 28 changed files with 710 additions and 157 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,8 @@ bin-int/
**.vcxproj
**.vcxproj.filters
**.vcxproj.user

# Directories
Hazel/vendor/VulkanSDK
Hazelnut/assets/cache
scripts/__pycache__
35 changes: 35 additions & 0 deletions Dependencies.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@

-- Hazel Dependencies

VULKAN_SDK = os.getenv("VULKAN_SDK")

IncludeDir = {}
IncludeDir["stb_image"] = "%{wks.location}/Hazel/vendor/stb_image"
IncludeDir["yaml_cpp"] = "%{wks.location}/Hazel/vendor/yaml-cpp/include"
IncludeDir["GLFW"] = "%{wks.location}/Hazel/vendor/GLFW/include"
IncludeDir["Glad"] = "%{wks.location}/Hazel/vendor/Glad/include"
IncludeDir["ImGui"] = "%{wks.location}/Hazel/vendor/ImGui"
IncludeDir["ImGuizmo"] = "%{wks.location}/Hazel/vendor/ImGuizmo"
IncludeDir["glm"] = "%{wks.location}/Hazel/vendor/glm"
IncludeDir["entt"] = "%{wks.location}/Hazel/vendor/entt/include"
IncludeDir["shaderc"] = "%{wks.location}/Hazel/vendor/shaderc/include"
IncludeDir["SPIRV_Cross"] = "%{wks.location}/Hazel/vendor/SPIRV-Cross"
IncludeDir["VulkanSDK"] = "%{VULKAN_SDK}/Include"

LibraryDir = {}

LibraryDir["VulkanSDK"] = "%{VULKAN_SDK}/Lib"
LibraryDir["VulkanSDK_Debug"] = "%{wks.location}/Hazel/vendor/VulkanSDK/Lib"

Library = {}
Library["Vulkan"] = "%{LibraryDir.VulkanSDK}/vulkan-1.lib"
Library["VulkanUtils"] = "%{LibraryDir.VulkanSDK}/VkLayer_utils.lib"

Library["ShaderC_Debug"] = "%{LibraryDir.VulkanSDK_Debug}/shaderc_sharedd.lib"
Library["SPIRV_Cross_Debug"] = "%{LibraryDir.VulkanSDK_Debug}/spirv-cross-cored.lib"
Library["SPIRV_Cross_GLSL_Debug"] = "%{LibraryDir.VulkanSDK_Debug}/spirv-cross-glsld.lib"
Library["SPIRV_Tools_Debug"] = "%{LibraryDir.VulkanSDK_Debug}/SPIRV-Toolsd.lib"

Library["ShaderC_Release"] = "%{LibraryDir.VulkanSDK}/shaderc_shared.lib"
Library["SPIRV_Cross_Release"] = "%{LibraryDir.VulkanSDK}/spirv-cross-core.lib"
Library["SPIRV_Cross_GLSL_Release"] = "%{LibraryDir.VulkanSDK}/spirv-cross-glsl.lib"
26 changes: 24 additions & 2 deletions Hazel/premake5.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ project "Hazel"
kind "StaticLib"
language "C++"
cppdialect "C++17"
staticruntime "on"
staticruntime "off"

targetdir ("%{wks.location}/bin/" .. outputdir .. "/%{prj.name}")
objdir ("%{wks.location}/bin-int/" .. outputdir .. "/%{prj.name}")
Expand Down Expand Up @@ -40,7 +40,8 @@ project "Hazel"
"%{IncludeDir.stb_image}",
"%{IncludeDir.entt}",
"%{IncludeDir.yaml_cpp}",
"%{IncludeDir.ImGuizmo}"
"%{IncludeDir.ImGuizmo}",
"%{IncludeDir.VulkanSDK}"
}

links
Expand All @@ -67,12 +68,33 @@ project "Hazel"
runtime "Debug"
symbols "on"

links
{
"%{Library.ShaderC_Debug}",
"%{Library.SPIRV_Cross_Debug}",
"%{Library.SPIRV_Cross_GLSL_Debug}"
}

filter "configurations:Release"
defines "HZ_RELEASE"
runtime "Release"
optimize "on"

links
{
"%{Library.ShaderC_Release}",
"%{Library.SPIRV_Cross_Release}",
"%{Library.SPIRV_Cross_GLSL_Release}"
}

filter "configurations:Dist"
defines "HZ_DIST"
runtime "Release"
optimize "on"

links
{
"%{Library.ShaderC_Release}",
"%{Library.SPIRV_Cross_Release}",
"%{Library.SPIRV_Cross_GLSL_Release}"
}
3 changes: 2 additions & 1 deletion Hazel/src/Hazel/Core/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ namespace Hazel {

Application* Application::s_Instance = nullptr;

Application::Application(const std::string& name)
Application::Application(const std::string& name, ApplicationCommandLineArgs args)
: m_CommandLineArgs(args)
{
HZ_PROFILE_FUNCTION();

Expand Down
21 changes: 18 additions & 3 deletions Hazel/src/Hazel/Core/Application.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,22 @@ int main(int argc, char** argv);

namespace Hazel {

struct ApplicationCommandLineArgs
{
int Count = 0;
char** Args = nullptr;

const char* operator[](int index) const
{
HZ_CORE_ASSERT(index < Count);
return Args[index];
}
};

class Application
{
public:
Application(const std::string& name = "Hazel App");
Application(const std::string& name = "Hazel App", ApplicationCommandLineArgs args = ApplicationCommandLineArgs());
virtual ~Application();

void OnEvent(Event& e);
Expand All @@ -33,11 +45,14 @@ namespace Hazel {
ImGuiLayer* GetImGuiLayer() { return m_ImGuiLayer; }

static Application& Get() { return *s_Instance; }

ApplicationCommandLineArgs GetCommandLineArgs() const { return m_CommandLineArgs; }
private:
void Run();
bool OnWindowClose(WindowCloseEvent& e);
bool OnWindowResize(WindowResizeEvent& e);
private:
ApplicationCommandLineArgs m_CommandLineArgs;
Scope<Window> m_Window;
ImGuiLayer* m_ImGuiLayer;
bool m_Running = true;
Expand All @@ -50,6 +65,6 @@ namespace Hazel {
};

// To be defined in CLIENT
Application* CreateApplication();
Application* CreateApplication(ApplicationCommandLineArgs args);

}
}
5 changes: 3 additions & 2 deletions Hazel/src/Hazel/Core/EntryPoint.h
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
#pragma once
#include "Hazel/Core/Base.h"
#include "Hazel/Core/Application.h"

#ifdef HZ_PLATFORM_WINDOWS

extern Hazel::Application* Hazel::CreateApplication();
extern Hazel::Application* Hazel::CreateApplication(ApplicationCommandLineArgs args);

int main(int argc, char** argv)
{
Hazel::Log::Init();

HZ_PROFILE_BEGIN_SESSION("Startup", "HazelProfile-Startup.json");
auto app = Hazel::CreateApplication();
auto app = Hazel::CreateApplication({ argc, argv });
HZ_PROFILE_END_SESSION();

HZ_PROFILE_BEGIN_SESSION("Runtime", "HazelProfile-Runtime.json");
Expand Down
34 changes: 34 additions & 0 deletions Hazel/src/Hazel/Core/Timer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#pragma once

#include <chrono>

namespace Hazel {

class Timer
{
public:
Timer()
{
Reset();
}

void Timer::Reset()
{
m_Start = std::chrono::high_resolution_clock::now();
}

float Timer::Elapsed()
{
return std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::high_resolution_clock::now() - m_Start).count() * 0.001f * 0.001f * 0.001f;
}

float Timer::ElapsedMillis()
{
return Elapsed() * 1000.0f;
}

private:
std::chrono::time_point<std::chrono::high_resolution_clock> m_Start;
};

}
26 changes: 16 additions & 10 deletions Hazel/src/Hazel/Renderer/Renderer2D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@

#include "Hazel/Renderer/VertexArray.h"
#include "Hazel/Renderer/Shader.h"
#include "Hazel/Renderer/UniformBuffer.h"
#include "Hazel/Renderer/RenderCommand.h"

#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>

namespace Hazel {

Expand Down Expand Up @@ -43,6 +45,13 @@ namespace Hazel {
glm::vec4 QuadVertexPositions[4];

Renderer2D::Statistics Stats;

struct CameraData
{
glm::mat4 ViewProjection;
};
CameraData CameraBuffer;
Ref<UniformBuffer> CameraUniformBuffer;
};

static Renderer2DData s_Data;
Expand Down Expand Up @@ -95,8 +104,6 @@ namespace Hazel {
samplers[i] = i;

s_Data.TextureShader = Shader::Create("assets/shaders/Texture.glsl");
s_Data.TextureShader->Bind();
s_Data.TextureShader->SetIntArray("u_Textures", samplers, s_Data.MaxTextureSlots);

// Set first texture slot to 0
s_Data.TextureSlots[0] = s_Data.WhiteTexture;
Expand All @@ -105,6 +112,8 @@ namespace Hazel {
s_Data.QuadVertexPositions[1] = { 0.5f, -0.5f, 0.0f, 1.0f };
s_Data.QuadVertexPositions[2] = { 0.5f, 0.5f, 0.0f, 1.0f };
s_Data.QuadVertexPositions[3] = { -0.5f, 0.5f, 0.0f, 1.0f };

s_Data.CameraUniformBuffer = UniformBuffer::Create(sizeof(Renderer2DData::CameraData), 0);
}

void Renderer2D::Shutdown()
Expand All @@ -128,10 +137,8 @@ namespace Hazel {
{
HZ_PROFILE_FUNCTION();

glm::mat4 viewProj = camera.GetProjection() * glm::inverse(transform);

s_Data.TextureShader->Bind();
s_Data.TextureShader->SetMat4("u_ViewProjection", viewProj);
s_Data.CameraBuffer.ViewProjection = camera.GetProjection() * glm::inverse(transform);
s_Data.CameraUniformBuffer->SetData(&s_Data.CameraBuffer, sizeof(Renderer2DData::CameraData));

StartBatch();
}
Expand All @@ -140,10 +147,8 @@ namespace Hazel {
{
HZ_PROFILE_FUNCTION();

glm::mat4 viewProj = camera.GetViewProjection();

s_Data.TextureShader->Bind();
s_Data.TextureShader->SetMat4("u_ViewProjection", viewProj);
s_Data.CameraBuffer.ViewProjection = camera.GetViewProjection();
s_Data.CameraUniformBuffer->SetData(&s_Data.CameraBuffer, sizeof(Renderer2DData::CameraData));

StartBatch();
}
Expand Down Expand Up @@ -175,6 +180,7 @@ namespace Hazel {
for (uint32_t i = 0; i < s_Data.TextureSlotIndex; i++)
s_Data.TextureSlots[i]->Bind(i);

s_Data.TextureShader->Bind();
RenderCommand::DrawIndexed(s_Data.QuadVertexArray, s_Data.QuadIndexCount);
s_Data.Stats.DrawCalls++;
}
Expand Down
21 changes: 21 additions & 0 deletions Hazel/src/Hazel/Renderer/UniformBuffer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include "hzpch.h"
#include "UniformBuffer.h"

#include "Hazel/Renderer/Renderer.h"
#include "Platform/OpenGL/OpenGLUniformBuffer.h"

namespace Hazel {

Ref<UniformBuffer> UniformBuffer::Create(uint32_t size, uint32_t binding)
{
switch (Renderer::GetAPI())
{
case RendererAPI::API::None: HZ_CORE_ASSERT(false, "RendererAPI::None is currently not supported!"); return nullptr;
case RendererAPI::API::OpenGL: return CreateRef<OpenGLUniformBuffer>(size, binding);
}

HZ_CORE_ASSERT(false, "Unknown RendererAPI!");
return nullptr;
}

}
16 changes: 16 additions & 0 deletions Hazel/src/Hazel/Renderer/UniformBuffer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#pragma once

#include "Hazel/Core/Base.h"

namespace Hazel {

class UniformBuffer
{
public:
virtual ~UniformBuffer() {}
virtual void SetData(const void* data, uint32_t size, uint32_t offset = 0) = 0;

static Ref<UniformBuffer> Create(uint32_t size, uint32_t binding);
};

}
3 changes: 2 additions & 1 deletion Hazel/src/Hazel/Scene/Scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,8 @@ namespace Hazel {
template<>
void Scene::OnComponentAdded<CameraComponent>(Entity entity, CameraComponent& component)
{
component.Camera.SetViewportSize(m_ViewportWidth, m_ViewportHeight);
if (m_ViewportWidth > 0 && m_ViewportHeight > 0)
component.Camera.SetViewportSize(m_ViewportWidth, m_ViewportHeight);
}

template<>
Expand Down
1 change: 1 addition & 0 deletions Hazel/src/Hazel/Scene/SceneCamera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ namespace Hazel {

void SceneCamera::SetViewportSize(uint32_t width, uint32_t height)
{
HZ_CORE_ASSERT(width > 0 && height > 0);
m_AspectRatio = (float)width / (float)height;
RecalculateProjection();
}
Expand Down
4 changes: 2 additions & 2 deletions Hazel/src/Hazel/Utils/PlatformUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ namespace Hazel {
{
public:
// These return empty strings if cancelled
static std::optional<std::string> OpenFile(const char* filter);
static std::optional<std::string> SaveFile(const char* filter);
static std::string OpenFile(const char* filter);
static std::string SaveFile(const char* filter);
};

}
Loading

0 comments on commit 93eef73

Please sign in to comment.