Skip to content

Commit

Permalink
resource loader init
Browse files Browse the repository at this point in the history
  • Loading branch information
K1ngst0m committed Oct 11, 2023
1 parent c5c7003 commit 73ff8e6
Show file tree
Hide file tree
Showing 6 changed files with 4,657 additions and 1 deletion.
2 changes: 2 additions & 0 deletions engine/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ target_include_directories(engine PUBLIC
../external/volk
../external/reckless/reckless/include
../external/backward-cpp/include
../external/tinyktx/
../external/tinydds/

PRIVATE
./
Expand Down
2 changes: 1 addition & 1 deletion engine/api/vulkan/device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ VkResult Device::createImage(const ImageCreateInfo& createInfo, Image** ppImage)
imageCreateInfo.extent.depth = createInfo.extent.depth;

VkImage image;
_VR(vkCreateImage(m_handle, &imageCreateInfo, nullptr, &image));
_VR(m_table.vkCreateImage(m_handle, &imageCreateInfo, nullptr, &image));

VkMemoryDedicatedRequirementsKHR dedicatedRequirements = {
VK_STRUCTURE_TYPE_MEMORY_DEDICATED_REQUIREMENTS_KHR,
Expand Down
82 changes: 82 additions & 0 deletions engine/resLoader/resLoader.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#include "resLoader.h"
#include "api/vulkan/image.h"
#include "tinyktx.h"
#include "tinydds.h"

#include <fstream>

namespace
{
inline bool loadKTXTextureDesc(std::filesystem::path path, aph::vk::ImageCreateInfo* pOutDesc)
{
if (!std::filesystem::exists(path))
{
return false;
}

std::ifstream file(path);

file.seekg(std::ios::end);
ssize_t ktxDataSize = file.tellg();

if (ktxDataSize > UINT32_MAX)
{
return false;
}

file.seekg(0);

TinyKtx_Callbacks callbacks
{
[](void* user, char const* msg) { CM_LOG_ERR("%s", msg); },
[](void* user, size_t size) { return malloc(size); },
[](void* user, void* memory) { free(memory); },
[](void* user, void* buffer, size_t byteCount) {


return fsReadFromStream((FileStream*)user, buffer, (ssize_t)byteCount);
},
[](void* user, int64_t offset) {
auto ifs = reinterpret_cast<std::ifstream*>(user);
return ifs->tellg()
},
[](void *user) { return (int64_t)fsGetStreamSeekPosition((FileStream*)user); }
};

TinyKtx_ContextHandle ctx = TinyKtx_CreateContext(&callbacks, &file);
bool headerOkay = TinyKtx_ReadHeader(ctx);
if (!headerOkay)
{
TinyKtx_DestroyContext(ctx);
return false;
}

aph::vk::ImageCreateInfo& textureCI = *pOutDesc;
textureCI.extent = {
.width = TinyKtx_Width(ctx),
.height = TinyKtx_Height(ctx),
.depth = std::max(1U, TinyKtx_Depth(ctx)),
};
textureCI.arrayLayers = std::max(1U, TinyKtx_ArraySlices(ctx));
textureCI.mipLevels = std::max(1U, TinyKtx_NumberOfMipmaps(ctx));
textureCI.format = TinyImageFormat_FromTinyKtxFormat(TinyKtx_GetFormat(ctx));
textureCI.mDescriptors = DESCRIPTOR_TYPE_TEXTURE;
textureCI.mSampleCount = SAMPLE_COUNT_1;

if (textureCI.format == VK_FORMAT_UNDEFINED)
{
TinyKtx_DestroyContext(ctx);
return false;
}

if (TinyKtx_IsCubemap(ctx))
{
textureCI.mArraySize *= 6;
textureDesc.mDescriptors |= DESCRIPTOR_TYPE_TEXTURE_CUBE;
}

TinyKtx_DestroyContext(ctx);

return true;
}
}
45 changes: 45 additions & 0 deletions engine/resLoader/resLoader.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#ifndef RES_LOADER_H_
#define RES_LOADER_H_

#include "common/common.h"

namespace aph
{

struct ResourceLoaderCreateInfo
{
bool isMultiThreads = false;
};

enum class ImageContainerType
{
Dds = 0,
Ktx,
Png,
Jpg,
};

struct ImageLoadInfo
{
std::string path;
ImageContainerType containerType;
};

struct BufferLoadInfo
{
std::string path;
};

class ResourceLoader
{
public:
ResourceLoader(const ResourceLoaderCreateInfo& createInfo) {}

~ResourceLoader() = default;

void loadImages();
void loadBuffers();
};
} // namespace aph

#endif
Loading

0 comments on commit 73ff8e6

Please sign in to comment.