Skip to content

Commit

Permalink
Resource + Example 1 Multi-thread add for initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
mtuncbilek95 committed Nov 25, 2024
1 parent 32cc4ad commit c806675
Show file tree
Hide file tree
Showing 18 changed files with 130 additions and 157 deletions.
94 changes: 56 additions & 38 deletions Example/01-HelloTriangle/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <Engine/Vulkan/Descriptor/VDescSet.h>
#include <Engine/Vulkan/Pipeline/VPipeline.h>
#include <Engine/Vulkan/Buffer/VBuffer.h>
#include <Engine/Thread/JobSystem.h>

using namespace MAGE;

Expand Down Expand Up @@ -42,6 +43,8 @@ struct MVPStruct

int main(int argC, char** argV)
{
JobSystem jobs(16);

WindowProps windProp =
{
.windowTitle = "Hello Triangle",
Expand Down Expand Up @@ -91,43 +94,59 @@ int main(int argC, char** argV)

vector<Owned<VCmdBuffer>> gPrimBuffers;
vector<Owned<VSemaphore>> gRenderSemaphores;

for (u32 i = 0; i < gSwapchain->GetImageCount(); i++)
{
gPrimBuffers.push_back(gPool->CreateCmdBuffer());
gRenderSemaphores.push_back(MakeOwned<VSemaphore>(&*gDevice));
}
Owned<VFence> gImgFence = MakeOwned<VFence>(false, &*gDevice);

ShaderProps shaderProp = {};
shaderProp.shaderPath = R"(D:\Projects\MAGE\Shaders\HelloTriangle.vert)";
shaderProp.shaderStage = VK_SHADER_STAGE_VERTEX_BIT;
Owned<VShader> vShader = MakeOwned<VShader>(shaderProp, &*gDevice);

shaderProp.shaderPath = R"(D:\Projects\MAGE\Shaders\HelloTriangle.frag)";
shaderProp.shaderStage = VK_SHADER_STAGE_FRAGMENT_BIT;
Owned<VShader> fShader = MakeOwned<VShader>(shaderProp, &*gDevice);

DescLayoutProps descLayoutProp = {};
descLayoutProp.bindings = { { 0, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1, VK_SHADER_STAGE_VERTEX_BIT } };

GraphicsPipelineProps pipeProp = {};
pipeProp.shaderStages = { &*vShader, &*fShader };
pipeProp.viewportState.viewport = vk::Viewport(0, 0, 1600, 900, 0.0f, 1.0f);
pipeProp.viewportState.scissor = vk::Rect2D({ 0,0 }, { 1600, 900 });
pipeProp.inputAssembler.bindings = { {VK_VERTEX_INPUT_RATE_VERTEX, {VK_FORMAT_R32G32B32_SFLOAT, VK_FORMAT_R32G32B32A32_SFLOAT}} };
pipeProp.dynamicRendering.colorAttachments = { VK_FORMAT_R8G8B8A8_UNORM };
pipeProp.blendState.attachments = { BlendStateAttachment() };

Owned<VPipeline> pipeline = MakeOwned<VPipeline>(pipeProp, &*gDevice);

BufferProps bufferProp = {};
bufferProp.sizeInBytes = sizeof(Vertex) * square.size() + indices.size() * sizeof(u32);
bufferProp.usageFlags = VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | VK_BUFFER_USAGE_INDEX_BUFFER_BIT;
Owned<VBuffer> vBuffer = MakeOwned<VBuffer>(bufferProp, &*gDevice);
vBuffer->BindMemory(gAlloc->GetAvailableMemory(AllocProps(vBuffer->GetRequestedSize(), VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT | VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT)));
vBuffer->Update({ square.data(), sizeof(Vertex) * square.size() }, 0);
vBuffer->Update({ indices.data(), indices.size() * sizeof(u32) }, sizeof(Vertex) * square.size());
Owned<VFence> gImgFence;

jobs.SubmitJob([&]() {
for (u32 i = 0; i < gSwapchain->GetImageCount(); i++)
{
gPrimBuffers.push_back(gPool->CreateCmdBuffer());
gRenderSemaphores.push_back(MakeOwned<VSemaphore>(&*gDevice));
}
});

jobs.SubmitJob([&]() {
gImgFence = MakeOwned<VFence>(false, &*gDevice);
});

Owned<VShader> vShader;
Owned<VShader> fShader;
Owned<VPipeline> pipeline;

jobs.SubmitJob([&]() {
ShaderProps shaderProp = {};
shaderProp.shaderPath = R"(D:\Projects\MAGE\Shaders\HelloTriangle.vert)";
shaderProp.shaderStage = VK_SHADER_STAGE_VERTEX_BIT;
vShader = MakeOwned<VShader>(shaderProp, &*gDevice);

shaderProp.shaderPath = R"(D:\Projects\MAGE\Shaders\HelloTriangle.frag)";
shaderProp.shaderStage = VK_SHADER_STAGE_FRAGMENT_BIT;
fShader = MakeOwned<VShader>(shaderProp, &*gDevice);

GraphicsPipelineProps pipeProp = {};
pipeProp.shaderStages = { &*vShader, &*fShader };
pipeProp.viewportState.viewport = vk::Viewport(0, 0, 1600, 900, 0.0f, 1.0f);
pipeProp.viewportState.scissor = vk::Rect2D({ 0,0 }, { 1600, 900 });
pipeProp.inputAssembler.bindings = { {VK_VERTEX_INPUT_RATE_VERTEX, {VK_FORMAT_R32G32B32_SFLOAT, VK_FORMAT_R32G32B32A32_SFLOAT}} };
pipeProp.dynamicRendering.colorAttachments = { VK_FORMAT_R8G8B8A8_UNORM };
pipeProp.blendState.attachments = { BlendStateAttachment() };

pipeline = MakeOwned<VPipeline>(pipeProp, &*gDevice);
});

Owned<VBuffer> vBuffer;

jobs.SubmitJob([&]() {
BufferProps bufferProp = {};
bufferProp.sizeInBytes = sizeof(Vertex) * square.size() + indices.size() * sizeof(u32);
bufferProp.usageFlags = VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | VK_BUFFER_USAGE_INDEX_BUFFER_BIT;

vBuffer = MakeOwned<VBuffer>(bufferProp, &*gDevice);
vBuffer->BindMemory(gAlloc->GetAvailableMemory(AllocProps(vBuffer->GetRequestedSize(), VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT | VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT)));
vBuffer->Update({ square.data(), sizeof(Vertex) * square.size() }, 0);
vBuffer->Update({ indices.data(), indices.size() * sizeof(u32) }, sizeof(Vertex) * square.size());
});

jobs.WaitForIdle();

window.Show();
while (!window.IsClosed())
Expand Down Expand Up @@ -176,5 +195,4 @@ int main(int argC, char** argV)
window.Hide();

gDevice->WaitForIdle();

}
2 changes: 1 addition & 1 deletion Source/Engine/Data/DayTime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace MAGE
return monthsInString[m_month - 1];
}

DayTime DayTime::GetCurrentTime()
DayTime DayTime::GetCurrentDayTime()
{
auto now = std::chrono::system_clock::now();
std::time_t currentTime = std::chrono::system_clock::to_time_t(now);
Expand Down
2 changes: 1 addition & 1 deletion Source/Engine/Data/DayTime.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ namespace MAGE
u8 GetMinute() const { return m_minute; }
u8 GetSecond() const { return m_second; }

static DayTime GetCurrentTime();
static DayTime GetCurrentDayTime();

string ToString() const;

Expand Down
11 changes: 3 additions & 8 deletions Source/Engine/Resource/Core/Resource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,16 @@ namespace MAGE
{
}

void Resource::Serialize(const path& absPath)
void Serialize(const path& absPath)
{
nlohmann::json json = {};
/*nlohmann::json json = {};
json["ResourceProperties"]["Guid"] = m_resProps.guid.ToString().c_str();
json["ResourceProperties"]["SizeInBytes"] = m_resProps.sizeInBytes;
json["ResourceProperties"]["MetaPath"] = m_resProps.relativePath;
json["ResourceProperties"]["ResourceName"] = m_resProps.resourceName;
json["ResourceProperties"]["ResourceType"] = m_resProps.type;
json["ResourceProperties"]["LastModified"] = m_resProps.lastModified.ToString().c_str();
json["ResourceProperties"]["LastModified"] = m_resProps.lastModified.ToString().c_str();*/

// Write data from m_props.data into the binary file
}

void Resource::Deserialize(const path& relPath)
{

}
}
6 changes: 0 additions & 6 deletions Source/Engine/Resource/Core/Resource.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,6 @@ namespace MAGE
Resource(const ResourceProps& desc);
virtual ~Resource();

virtual bool Load() = 0;
virtual bool Unload() = 0;

virtual void Serialize(const path& absPath);
virtual void Deserialize(const path& relPath);

virtual void Destroy() override = 0;

protected:
Expand Down
43 changes: 2 additions & 41 deletions Source/Engine/Resource/Helpers/ResourceHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,48 +58,9 @@ namespace MAGE
props.resourceName = newPath.stem().string();
props.relativePath = (Config::GetAppConfig().projectPath / relExtPath).string();
props.guid = Guid::GenerateID();
props.lastModified = DayTime::GetCurrentTime();
props.lastModified = DayTime::GetCurrentDayTime();
props.type = GetResourceType(newPath);
props.sizeInBytes = std::filesystem::file_size(newPath);

switch (props.type)
{
case ResourceType::Texture:
// Read with framework
// Write as binary
break;
case ResourceType::Mesh:
// Read with framework
// Write as binary
break;
case ResourceType::Shader:
// Read as string
// write as binary
break;
case ResourceType::Script:
// Copy
break;
case ResourceType::Font:
// Read with framework
// Write as binary
break;
case ResourceType::Audio:
// Read with framework
// Write as binary
break;
default:
spdlog::critical("You fucked up!");
break;
}
}

void ResourceHandler::ReadResourceFile(const string& relPath, OwnedBuffer& buffer)
{
path newPath = Config::GetAppConfig().projectPath;
newPath /= relPath;

if (!PlatformFile::Read(newPath.string(), buffer))
spdlog::error("Failed to load from {}", relPath);
}

void ResourceHandler::ReadTextureDisk(const string& fPath, Imagery& buffer)
Expand All @@ -110,7 +71,7 @@ namespace MAGE

buffer.channelCount = channels;
buffer.imageSize = { static_cast<u32>(width), static_cast<u32>(height) };
buffer.buffer = OwnedBuffer(data, static_cast<u64>(width * height * 8 * channels));
buffer.buffer = OwnedBuffer(data, static_cast<u64>(width * height * buffer.depthBit * channels));
}

void ResourceHandler::ReadGeometryDisk(const string& fPath, Geometry& buffer)
Expand Down
1 change: 0 additions & 1 deletion Source/Engine/Resource/Helpers/ResourceHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ namespace MAGE
{
public:
static void ReadFileFromDisk(const string& aPath, const string& relExtPath, ResourceProps& props);
static void ReadResourceFile(const string& fPath, OwnedBuffer& buffer);
static void ReadGeometryDisk(const string& fPath, Geometry& buffer);
static void ReadTextureDisk(const string& fPath, Imagery& buffer);
};
Expand Down
Empty file.
Empty file.
24 changes: 1 addition & 23 deletions Source/Engine/Resource/Texture/Texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace MAGE
{
Texture::Texture(const TextureProps& texDesc, const ResourceProps& resDesc) : Resource(resDesc), m_texProps(texDesc)
Texture::Texture(const ResourceProps& resDesc) : Resource(resDesc)
{
}

Expand All @@ -23,28 +23,6 @@ namespace MAGE
m_stage->Update(m_resProps.data, 0);
}

bool Texture::Load()
{
return false;
}

bool Texture::Unload()
{
return false;
}

void Texture::Serialize(const path& absPath)
{
Resource::Serialize(absPath);


}

void Texture::Deserialize(const path& relPath)
{
Resource::Deserialize(relPath);
}

void Texture::Destroy()
{
if (m_stage)
Expand Down
12 changes: 4 additions & 8 deletions Source/Engine/Resource/Texture/Texture.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#pragma once

#include "Engine/Core/Core.h"
#include "../Core/Resource.h"
#include "Engine/Resource/Core/Resource.h"

#include "Engine/Vulkan/Buffer/VBuffer.h"
#include "Engine/Vulkan/Image/VImage.h"
Expand All @@ -26,18 +26,14 @@ namespace MAGE

class Texture : public Resource
{
friend class TextureSerializer;

public:
Texture(const TextureProps& texDesc, const ResourceProps& resDesc);
Texture(const ResourceProps& resDesc);
virtual ~Texture() override;

virtual void GenerateTexture();

virtual bool Load() override;
virtual bool Unload() override;

virtual void Serialize(const path& absPath) override;
virtual void Deserialize(const path& relPath) override;

void Destroy() override final;

private:
Expand Down
5 changes: 5 additions & 0 deletions Source/Engine/Resource/Texture/TextureSerializer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include "TextureSerializer.h"

namespace MAGE
{
}
32 changes: 32 additions & 0 deletions Source/Engine/Resource/Texture/TextureSerializer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* Copyright (c) 2024 Metehan Tuncbilek
*/

#pragma once

#include "Engine/Core/Core.h"
#include "Engine/Objects/Singleton.h"
#include "Engine/Resource/Core/Resource.h"
#include "Engine/Resource/Texture/Texture.h"

namespace MAGE
{
class TextureSerializer final : public Singleton<TextureSerializer>
{
public:
template<typename T, typename = std::enable_if<std::is_base_of_v<Texture, T>>>
Shared<T> SerializeTexture(const ResourceProps& desc)
{
// Read texture file such as .png, .tga, .tiff then make it mage object
Shared<T> tex = MakeShared<T>(desc);

return Shared<T>();
}

//Shared<Texture> DeserializeTexture(const path& absPath);
};
}
23 changes: 0 additions & 23 deletions Source/Engine/Serialization/ISerialObject.h

This file was deleted.

Loading

0 comments on commit c806675

Please sign in to comment.