Skip to content

Commit

Permalink
Can now load shaders to material
Browse files Browse the repository at this point in the history
- Shaders can now be linked to shaders
- Need to move pipeline creation into materials (At Least for objects that use a material)
  • Loading branch information
tomheeleynz committed Jan 25, 2023
1 parent 6e8faa7 commit 04573fa
Show file tree
Hide file tree
Showing 12 changed files with 111 additions and 25 deletions.
2 changes: 1 addition & 1 deletion Arcane/CMakeLists.txt
Expand Up @@ -23,7 +23,7 @@ if (WIN32 OR (UNIX AND NOT APPLE))
message(STATUS ${Vulkan_shaderc_combined_LIBRARY})

# Set Libraries
set(LIBS glfw imgui ${Vulkan_LIBRARIES} "C:/VulkanSDK/1.3.216.0/Lib/shaderc_combinedd.lib" assimp SPIRV-REFLECT glad ${OPENGL_LIBRARIES} spirv-cross-glsl)
set(LIBS glfw imgui ${Vulkan_LIBRARIES} "C:/VulkanSDK/1.3.231.1/Lib/shaderc_combinedd.lib" 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
1 change: 1 addition & 0 deletions Arcane/src/Arcane.h
Expand Up @@ -23,6 +23,7 @@
#include "Arcane/Renderer/Camera.h"
#include "Arcane/Renderer/DescriptorSet.h"
#include "Arcane/Renderer/MeshFactory.h"
#include "Arcane/Renderer/MaterialSerializer.h"

/////////////////////////////////////////////////////
////////// ImGui
Expand Down
7 changes: 7 additions & 0 deletions Arcane/src/Arcane/Assets/AssetDatabase.cpp
Expand Up @@ -10,6 +10,7 @@
#include "Arcane/Renderer/Mesh.h"
#include "Arcane/Renderer/Texture.h"
#include "Arcane/Scripting/Script.h"
#include "Arcane/Renderer/MaterialDeserializer.h"

namespace Arcane
{
Expand Down Expand Up @@ -99,6 +100,12 @@ namespace Arcane
}
else if (currentAssetPath.extension() == ".arcanemat")
{
// Set path for material deserialization
MaterialDeserializer deserializer(currentAssetPath);
Material* material = deserializer.Deserialize();
material->SetAssetType(AssetType::MATERIAL);
material->SetID(Arcane::Core::UUID(assetID));
m_Assets[assetID] = material;
}
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion Arcane/src/Arcane/Platform/Vulkan/VulkanMaterial.h
Expand Up @@ -32,7 +32,7 @@ namespace Arcane
std::vector<ShaderVariable> GetMaterialVariables();
private:
// Things to render material
Shader* m_Shader;
Shader* m_Shader = nullptr;
DescriptorSet* m_DescriptorSet;
UniformBuffer* m_UniformBuffer;

Expand Down
4 changes: 2 additions & 2 deletions Arcane/src/Arcane/Platform/Vulkan/VulkanShader.cpp
Expand Up @@ -128,7 +128,7 @@ namespace Arcane {
printf("Fragment shader created\n");
}

// ReflectModule(fragmentBytes, m_FragShaderModule);
ReflectModule(fragmentBytes, m_FragShaderModule);
}
}

Expand Down Expand Up @@ -158,7 +158,7 @@ namespace Arcane {
SpvReflectShaderModule module;

SpvReflectResult result = spvReflectCreateShaderModule(
byteCode.size(),
byteCode.size() * 4,
byteCode.data(),
&module
);
Expand Down
22 changes: 22 additions & 0 deletions Arcane/src/Arcane/Renderer/MaterialDeserializer.cpp
@@ -0,0 +1,22 @@
#include "MaterialDeserializer.h"

namespace Arcane
{
MaterialDeserializer::MaterialDeserializer(std::filesystem::path path)
{
m_Path = path;
}

Material* MaterialDeserializer::Deserialize()
{
// Reading in material json
nlohmann::json jsonObject;
std::ifstream i(m_Path.string());
i >> jsonObject;

// Get name of material
std::string name = jsonObject["name"];

return Material::Create(nullptr);
}
}
20 changes: 20 additions & 0 deletions Arcane/src/Arcane/Renderer/MaterialDeserializer.h
@@ -0,0 +1,20 @@
#pragma once

#include <fstream>
#include <filesystem>
#include <nlohmann/json.hpp>

#include "Arcane/Renderer/Material.h"

namespace Arcane
{
class MaterialDeserializer
{
public:
MaterialDeserializer(std::filesystem::path path);

Material* Deserialize();
private:
std::filesystem::path m_Path;
};
}
1 change: 1 addition & 0 deletions Arcane/src/Arcane/Renderer/MaterialSerializer.cpp
Expand Up @@ -15,6 +15,7 @@ namespace Arcane

// Set Name of scene
jsonObject["name"] = "DefaultMaterial";
jsonObject["shader"] = m_Material->GetShader()->GetID();

// variable array
nlohmann::json variableArray = nlohmann::json::array();
Expand Down
3 changes: 2 additions & 1 deletion Arcane/src/Arcane/Scene/Scene.cpp
Expand Up @@ -74,10 +74,11 @@ namespace Arcane
auto& transform = view.get<TransformComponent>(entity);
auto& meshRenderer = view.get<MeshRendererComponent>(entity);

if (mesh.mesh != nullptr)
if (mesh.mesh != nullptr && meshRenderer.material != nullptr && meshRenderer.material->GetShader() != nullptr)
m_SceneRenderer->SubmitMesh(mesh.mesh, transform, meshRenderer.material);
}
}

m_SceneRenderer->RenderScene();
}
}
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 4 additions & 1 deletion EnchantingTable/src/Panels/FileBrowser.cpp
Expand Up @@ -26,6 +26,9 @@ FileBrowserPanel::FileBrowserPanel()

// -- Shader Icon
m_Icons["Shader"] = Arcane::UI::AddTexture(Arcane::Texture::Create("./src/EditorAssets/Icons/shader_icon.png"));

// -- Material Icon
m_Icons["Material"] = Arcane::UI::AddTexture(Arcane::Texture::Create("./src/EditorAssets/Icons/material_icon.png"));
}

void FileBrowserPanel::OnUpdate()
Expand Down Expand Up @@ -117,7 +120,7 @@ std::string FileBrowserPanel::GetIconType(std::string extension)
return "Image";
}

if (extension == ".arcmat") {
if (extension == ".arcanemat") {
return "Material";
}

Expand Down
69 changes: 50 additions & 19 deletions EnchantingTable/src/Panels/MaterialViewerPanel.cpp
Expand Up @@ -12,36 +12,67 @@ void MaterialViewerPanel::OnUpdate()
if (m_Material == nullptr) return;

ImGui::Begin("Material Viewer");

std::vector<Arcane::ShaderVariable> materialVariables = m_Material->GetMaterialVariables();
for (auto& variable : materialVariables)

ImGui::Text("Shader");

if (ImGui::BeginDragDropTarget())
{
// Image Sampler
if (variable.Type == Arcane::ShaderVariableType::Sampler)
{
Arcane::Texture* texture = m_Material->GetTexture(variable.binding);
Arcane::UI::Image(texture);
const ImGuiPayload* payload = ImGui::AcceptDragDropPayload("CURRENT_SELECTED_ASSET");

if (payload != nullptr) {
AssetInfo assetInfo = *static_cast<AssetInfo*>(payload->Data);
Asset* asset = Arcane::Application::Get().GetAssetDatabase().GetAsset(assetInfo.id);

// Add drag and drop
if (ImGui::BeginDragDropTarget())
if (asset != nullptr && asset->GetAssetType() == AssetType::SHADER)
{
const ImGuiPayload* payload = ImGui::AcceptDragDropPayload("CURRENT_SELECTED_ASSET");
Shader* shader = static_cast<Shader*>(asset);
m_Material->SetShader(shader);
}
}
ImGui::EndDragDropTarget();
}

if (payload != nullptr) {
AssetInfo assetInfo = *static_cast<AssetInfo*>(payload->Data);
Asset* asset = Arcane::Application::Get().GetAssetDatabase().GetAsset(assetInfo.id);
if (m_Material->GetShader() != nullptr) {
std::vector<Arcane::ShaderVariable> materialVariables = m_Material->GetMaterialVariables();
for (auto& variable : materialVariables)
{
// Image Sampler
if (variable.Type == Arcane::ShaderVariableType::Sampler)
{
Arcane::Texture* texture = m_Material->GetTexture(variable.binding);
Arcane::UI::Image(texture);

// Add drag and drop
if (ImGui::BeginDragDropTarget())
{
const ImGuiPayload* payload = ImGui::AcceptDragDropPayload("CURRENT_SELECTED_ASSET");

if (asset != nullptr && asset->GetAssetType() == AssetType::TEXTURE)
{
Texture* texture = static_cast<Texture*>(asset);
m_Material->WriteTexture(variable.binding, texture);
if (payload != nullptr) {
AssetInfo assetInfo = *static_cast<AssetInfo*>(payload->Data);
Asset* asset = Arcane::Application::Get().GetAssetDatabase().GetAsset(assetInfo.id);

if (asset != nullptr && asset->GetAssetType() == AssetType::TEXTURE)
{
Texture* texture = static_cast<Texture*>(asset);
m_Material->WriteTexture(variable.binding, texture);
}
}
ImGui::EndDragDropTarget();
}
ImGui::EndDragDropTarget();
}
}
}

if (ImGui::Button("Save Material"))
{
std::string filename = Arcane::FileDialog::SaveFile();

if (!filename.empty()) {
Arcane::MaterialSerializer serializer(m_Material);
serializer.Serialize(filename);
}
}

ImGui::End();
}

Expand Down

0 comments on commit 04573fa

Please sign in to comment.