Implementing ktx texture loading
This commit is contained in:
+4
-1
@@ -20,4 +20,7 @@
|
|||||||
path = external/ktx
|
path = external/ktx
|
||||||
url = https://github.com/KhronosGroup/KTX-Software
|
url = https://github.com/KhronosGroup/KTX-Software
|
||||||
[submodule "external/stb"]
|
[submodule "external/stb"]
|
||||||
url = https://github.com/nothings/stb
|
url = https://github.com/nothings/stb
|
||||||
|
[submodule "external/slang"]
|
||||||
|
path = external/slang
|
||||||
|
url = https://github.com/shader-slang/slang.git
|
||||||
|
|||||||
+2
-1
@@ -50,6 +50,7 @@ find_package(JSON REQUIRED)
|
|||||||
find_package(GLFW REQUIRED)
|
find_package(GLFW REQUIRED)
|
||||||
find_package(GLM REQUIRED)
|
find_package(GLM REQUIRED)
|
||||||
find_package(KTX REQUIRED)
|
find_package(KTX REQUIRED)
|
||||||
|
find_package(STB REQUIRED)
|
||||||
find_package(SLANG REQUIRED)
|
find_package(SLANG REQUIRED)
|
||||||
find_package(Aftermath REQUIRED)
|
find_package(Aftermath REQUIRED)
|
||||||
|
|
||||||
@@ -80,8 +81,8 @@ target_link_libraries(SeeleEngine ${Boost_LIBRARIES})
|
|||||||
target_link_libraries(SeeleEngine ${GLFW_LIBRARIES})
|
target_link_libraries(SeeleEngine ${GLFW_LIBRARIES})
|
||||||
target_link_libraries(SeeleEngine ${SLANG_LIBRARIES})
|
target_link_libraries(SeeleEngine ${SLANG_LIBRARIES})
|
||||||
target_link_libraries(SeeleEngine ${ASSIMP_LIBRARIES})
|
target_link_libraries(SeeleEngine ${ASSIMP_LIBRARIES})
|
||||||
target_link_libraries(SeeleEngine ${JSON_LIBRARY})
|
|
||||||
target_link_libraries(SeeleEngine ${NSAM_LIBRARIES})
|
target_link_libraries(SeeleEngine ${NSAM_LIBRARIES})
|
||||||
|
target_link_libraries(SeeleEngine ${KTX_LIBRARIES})
|
||||||
#target_link_libraries(SeeleEngine ${SPIRV_LIBRARIES})
|
#target_link_libraries(SeeleEngine ${SPIRV_LIBRARIES})
|
||||||
|
|
||||||
target_precompile_headers(SeeleEngine
|
target_precompile_headers(SeeleEngine
|
||||||
|
|||||||
@@ -68,7 +68,7 @@ ExternalProject_Add(slang
|
|||||||
SOURCE_DIR ${SLANG_ROOT}
|
SOURCE_DIR ${SLANG_ROOT}
|
||||||
BINARY_DIR ${SLANG_ROOT}
|
BINARY_DIR ${SLANG_ROOT}
|
||||||
CONFIGURE_COMMAND ""
|
CONFIGURE_COMMAND ""
|
||||||
BUILD_COMMAND msbuild slang.sln
|
BUILD_COMMAND msbuild slang.sln -p:Configuration=Release -p:Platform=${CMAKE_PLATFORM}
|
||||||
INSTALL_COMMAND "")
|
INSTALL_COMMAND "")
|
||||||
|
|
||||||
set(SLANG_LIB_PATH ${SLANG_ROOT}/${SLANG_LIB_PATH})
|
set(SLANG_LIB_PATH ${SLANG_ROOT}/${SLANG_LIB_PATH})
|
||||||
|
|||||||
+1
Submodule external/slang added at 858c7c57b1
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -2,9 +2,7 @@
|
|||||||
#include "Graphics/GraphicsResources.h"
|
#include "Graphics/GraphicsResources.h"
|
||||||
#include "Graphics/Graphics.h"
|
#include "Graphics/Graphics.h"
|
||||||
#include "Window/WindowManager.h"
|
#include "Window/WindowManager.h"
|
||||||
#include <stb_image.h>
|
#include "Graphics/Vulkan/VulkanGraphicsEnums.h"
|
||||||
#include <stb_image_write.h>
|
|
||||||
|
|
||||||
using namespace Seele;
|
using namespace Seele;
|
||||||
|
|
||||||
|
|
||||||
@@ -34,19 +32,23 @@ void TextureAsset::save()
|
|||||||
void TextureAsset::load()
|
void TextureAsset::load()
|
||||||
{
|
{
|
||||||
setStatus(Status::Loading);
|
setStatus(Status::Loading);
|
||||||
int x, y, n;
|
ktxTexture2* kTexture;
|
||||||
unsigned char* data = stbi_load(getFullPath().c_str(), &x, &y, &n, 4);
|
KTX_error_code result = ktxTexture2_CreateFromNamedFile(getFullPath().c_str(),
|
||||||
|
KTX_TEXTURE_CREATE_LOAD_IMAGE_DATA_BIT,
|
||||||
|
&kTexture);
|
||||||
TextureCreateInfo createInfo;
|
TextureCreateInfo createInfo;
|
||||||
|
createInfo.width = kTexture->baseWidth;
|
||||||
//TODO: support other formats
|
createInfo.height = kTexture->baseHeight;
|
||||||
createInfo.format = Gfx::SE_FORMAT_R8G8B8A8_UINT;
|
createInfo.depth = kTexture->baseDepth;
|
||||||
createInfo.resourceData.data = data;
|
createInfo.bArray = kTexture->isArray;
|
||||||
createInfo.resourceData.size = x * y * 4 * sizeof(unsigned char);
|
createInfo.arrayLayers = kTexture->numLayers;
|
||||||
|
createInfo.format = Vulkan::cast((VkFormat)kTexture->vkFormat);
|
||||||
|
createInfo.mipLevels = kTexture->numLevels;
|
||||||
|
createInfo.usage = Gfx::SE_IMAGE_USAGE_SAMPLED_BIT;
|
||||||
|
createInfo.resourceData.data = ktxTexture_GetData(ktxTexture(kTexture));
|
||||||
|
createInfo.resourceData.size = (uint32)ktxTexture_GetDataSize(ktxTexture(kTexture));
|
||||||
createInfo.resourceData.owner = Gfx::QueueType::DEDICATED_TRANSFER;
|
createInfo.resourceData.owner = Gfx::QueueType::DEDICATED_TRANSFER;
|
||||||
createInfo.width = x;
|
|
||||||
createInfo.height = y;
|
|
||||||
Gfx::PTexture2D texture = WindowManager::getGraphics()->createTexture2D(createInfo);
|
Gfx::PTexture2D texture = WindowManager::getGraphics()->createTexture2D(createInfo);
|
||||||
stbi_image_free(data);
|
|
||||||
texture->transferOwnership(Gfx::QueueType::GRAPHICS);
|
texture->transferOwnership(Gfx::QueueType::GRAPHICS);
|
||||||
setTexture(texture);
|
setTexture(texture);
|
||||||
setStatus(Asset::Status::Ready);
|
setStatus(Asset::Status::Ready);
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
#include "TextureAsset.h"
|
#include "TextureAsset.h"
|
||||||
#include "Graphics/Graphics.h"
|
#include "Graphics/Graphics.h"
|
||||||
#include "AssetRegistry.h"
|
#include "AssetRegistry.h"
|
||||||
|
#include "Graphics/Vulkan/VulkanGraphicsEnums.h"
|
||||||
#define STB_IMAGE_IMPLEMENTATION
|
#define STB_IMAGE_IMPLEMENTATION
|
||||||
#include <stb_image.h>
|
#include <stb_image.h>
|
||||||
#define STB_IMAGE_WRITE_IMPLEMENTATION
|
#define STB_IMAGE_WRITE_IMPLEMENTATION
|
||||||
@@ -12,10 +13,8 @@ using namespace Seele;
|
|||||||
TextureLoader::TextureLoader(Gfx::PGraphics graphics)
|
TextureLoader::TextureLoader(Gfx::PGraphics graphics)
|
||||||
: graphics(graphics)
|
: graphics(graphics)
|
||||||
{
|
{
|
||||||
placeholderAsset = new TextureAsset();
|
placeholderAsset = new TextureAsset(std::filesystem::absolute("./textures/placeholder.ktx"));
|
||||||
placeholderTexture = import("./textures/placeholder.png");
|
placeholderAsset->load();
|
||||||
placeholderAsset->setTexture(placeholderTexture);
|
|
||||||
placeholderAsset->setStatus(Asset::Status::Ready);
|
|
||||||
AssetRegistry::get().textures[""] = placeholderAsset;
|
AssetRegistry::get().textures[""] = placeholderAsset;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -28,13 +27,13 @@ void TextureLoader::importAsset(const std::filesystem::path& filePath)
|
|||||||
auto assetFileName = filePath;
|
auto assetFileName = filePath;
|
||||||
PTextureAsset asset = new TextureAsset(assetFileName.replace_extension("asset").filename().generic_string());
|
PTextureAsset asset = new TextureAsset(assetFileName.replace_extension("asset").filename().generic_string());
|
||||||
asset->setStatus(Asset::Status::Loading);
|
asset->setStatus(Asset::Status::Loading);
|
||||||
asset->setTexture(placeholderTexture);
|
asset->setTexture(placeholderAsset->getTexture());
|
||||||
AssetRegistry::get().textures[asset->getFileName()] = asset;
|
AssetRegistry::get().textures[asset->getFileName()] = asset;
|
||||||
futures.add(std::async(std::launch::async, [this, filePath, asset] () mutable {
|
futures.add(std::async(std::launch::async, [this, filePath, asset] () mutable {
|
||||||
using namespace std::chrono_literals;
|
using namespace std::chrono_literals;
|
||||||
//std::this_thread::sleep_for(5s);
|
//std::this_thread::sleep_for(5s);
|
||||||
Gfx::PTexture2D texture = import(filePath);
|
import(filePath, asset);
|
||||||
asset->setTexture(texture);
|
asset->load();
|
||||||
asset->setStatus(Asset::Status::Ready);
|
asset->setStatus(Asset::Status::Ready);
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
@@ -44,20 +43,34 @@ PTextureAsset TextureLoader::getPlaceholderTexture()
|
|||||||
return placeholderAsset;
|
return placeholderAsset;
|
||||||
}
|
}
|
||||||
|
|
||||||
Gfx::PTexture2D TextureLoader::import(const std::filesystem::path& path)
|
void TextureLoader::import(const std::filesystem::path& path, PTextureAsset textureAsset)
|
||||||
{
|
{
|
||||||
int x, y, n;
|
int x, y, n;
|
||||||
unsigned char* data = stbi_load(path.string().c_str(), &x, &y, &n, 4);
|
unsigned char* data = stbi_load(path.string().c_str(), &x, &y, &n, 4);
|
||||||
|
ktxTexture2* kTexture;
|
||||||
TextureCreateInfo createInfo;
|
ktxTextureCreateInfo createInfo;
|
||||||
createInfo.format = Gfx::SE_FORMAT_R8G8B8A8_SRGB;
|
KTX_error_code result;
|
||||||
createInfo.resourceData.data = data;
|
|
||||||
createInfo.resourceData.size = x * y * 4 * sizeof(unsigned char);
|
createInfo.vkFormat = VK_FORMAT_R8G8B8A8_SRGB;
|
||||||
createInfo.resourceData.owner = Gfx::QueueType::DEDICATED_TRANSFER;
|
createInfo.baseWidth = x;
|
||||||
createInfo.width = x;
|
createInfo.baseHeight = y;
|
||||||
createInfo.height = y;
|
createInfo.baseDepth = 1;
|
||||||
Gfx::PTexture2D texture = graphics->createTexture2D(createInfo);
|
createInfo.numDimensions = 1 + (y > 1);
|
||||||
|
createInfo.numLevels = 1;
|
||||||
|
createInfo.numLayers = 1;
|
||||||
|
createInfo.numFaces = 1;
|
||||||
|
createInfo.isArray = false;
|
||||||
|
createInfo.generateMipmaps = true;
|
||||||
|
|
||||||
|
result = ktxTexture2_Create(&createInfo,
|
||||||
|
KTX_TEXTURE_CREATE_ALLOC_STORAGE,
|
||||||
|
&kTexture);
|
||||||
|
|
||||||
|
result = ktxTexture_SetImageFromMemory(ktxTexture(kTexture),
|
||||||
|
0, 0, 0, data, x * y * 4 * sizeof(unsigned char));
|
||||||
|
|
||||||
stbi_image_free(data);
|
stbi_image_free(data);
|
||||||
texture->transferOwnership(Gfx::QueueType::GRAPHICS);
|
|
||||||
return texture;
|
result = ktxTexture_WriteToNamedFile(ktxTexture(kTexture), textureAsset->getFullPath().c_str());
|
||||||
|
ktxTexture_Destroy(ktxTexture(kTexture));
|
||||||
}
|
}
|
||||||
@@ -18,10 +18,9 @@ public:
|
|||||||
void importAsset(const std::filesystem::path& filePath);
|
void importAsset(const std::filesystem::path& filePath);
|
||||||
PTextureAsset getPlaceholderTexture();
|
PTextureAsset getPlaceholderTexture();
|
||||||
private:
|
private:
|
||||||
Gfx::PTexture2D import(const std::filesystem::path& path);
|
void import(const std::filesystem::path& path, PTextureAsset asset);
|
||||||
Gfx::PGraphics graphics;
|
Gfx::PGraphics graphics;
|
||||||
List<std::future<void>> futures;
|
List<std::future<void>> futures;
|
||||||
Gfx::PTexture2D placeholderTexture;
|
|
||||||
PTextureAsset placeholderAsset;
|
PTextureAsset placeholderAsset;
|
||||||
};
|
};
|
||||||
DEFINE_REF(TextureLoader)
|
DEFINE_REF(TextureLoader)
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
#include "Containers/Array.h"
|
#include "Containers/Array.h"
|
||||||
#include "VertexShaderInput.h"
|
#include "VertexShaderInput.h"
|
||||||
#include "ShaderCompiler.h"
|
#include "ShaderCompiler.h"
|
||||||
|
#include "ktx.h"
|
||||||
|
|
||||||
namespace Seele
|
namespace Seele
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -500,7 +500,9 @@ public:
|
|||||||
virtual SeFormat getFormat() const = 0;
|
virtual SeFormat getFormat() const = 0;
|
||||||
virtual uint32 getSizeX() const = 0;
|
virtual uint32 getSizeX() const = 0;
|
||||||
virtual uint32 getSizeY() const = 0;
|
virtual uint32 getSizeY() const = 0;
|
||||||
|
virtual uint32 getSizeZ() const = 0;
|
||||||
virtual SeSampleCountFlags getNumSamples() const = 0;
|
virtual SeSampleCountFlags getNumSamples() const = 0;
|
||||||
|
virtual uint32 getMipLevels() const = 0;
|
||||||
virtual void changeLayout(SeImageLayout newLayout) = 0;
|
virtual void changeLayout(SeImageLayout newLayout) = 0;
|
||||||
virtual class Texture2D* getTexture2D() { return nullptr; }
|
virtual class Texture2D* getTexture2D() { return nullptr; }
|
||||||
virtual void* getNativeHandle() { return nullptr; }
|
virtual void* getNativeHandle() { return nullptr; }
|
||||||
@@ -520,7 +522,9 @@ public:
|
|||||||
virtual SeFormat getFormat() const = 0;
|
virtual SeFormat getFormat() const = 0;
|
||||||
virtual uint32 getSizeX() const = 0;
|
virtual uint32 getSizeX() const = 0;
|
||||||
virtual uint32 getSizeY() const = 0;
|
virtual uint32 getSizeY() const = 0;
|
||||||
|
virtual uint32 getSizeZ() const = 0;
|
||||||
virtual SeSampleCountFlags getNumSamples() const = 0;
|
virtual SeSampleCountFlags getNumSamples() const = 0;
|
||||||
|
virtual uint32 getMipLevels() const = 0;
|
||||||
virtual void changeLayout(SeImageLayout newLayout) = 0;
|
virtual void changeLayout(SeImageLayout newLayout) = 0;
|
||||||
virtual class Texture2D* getTexture2D() { return this; }
|
virtual class Texture2D* getTexture2D() { return this; }
|
||||||
protected:
|
protected:
|
||||||
|
|||||||
@@ -144,6 +144,10 @@ public:
|
|||||||
PCmdBuffer getCommands();
|
PCmdBuffer getCommands();
|
||||||
PRenderCommand createRenderCommand(const std::string& name);
|
PRenderCommand createRenderCommand(const std::string& name);
|
||||||
PComputeCommand createComputeCommand(const std::string& name);
|
PComputeCommand createComputeCommand(const std::string& name);
|
||||||
|
VkCommandPool getPoolHandle() const
|
||||||
|
{
|
||||||
|
return commandPool;
|
||||||
|
}
|
||||||
void submitCommands(PSemaphore signalSemaphore = nullptr);
|
void submitCommands(PSemaphore signalSemaphore = nullptr);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|||||||
@@ -360,6 +360,7 @@ PStagingManager Graphics::getStagingManager()
|
|||||||
return stagingManager;
|
return stagingManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Array<const char *> Graphics::getRequiredExtensions()
|
Array<const char *> Graphics::getRequiredExtensions()
|
||||||
{
|
{
|
||||||
Array<const char *> extensions;
|
Array<const char *> extensions;
|
||||||
|
|||||||
@@ -249,6 +249,10 @@ public:
|
|||||||
{
|
{
|
||||||
return samples;
|
return samples;
|
||||||
}
|
}
|
||||||
|
inline uint32 getMipLevels() const
|
||||||
|
{
|
||||||
|
return mipLevels;
|
||||||
|
}
|
||||||
inline bool isDepthStencil() const
|
inline bool isDepthStencil() const
|
||||||
{
|
{
|
||||||
return aspect & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT);
|
return aspect & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT);
|
||||||
@@ -303,6 +307,10 @@ public:
|
|||||||
{
|
{
|
||||||
return textureHandle->sizeY;
|
return textureHandle->sizeY;
|
||||||
}
|
}
|
||||||
|
virtual uint32 getSizeZ() const override
|
||||||
|
{
|
||||||
|
return textureHandle->sizeZ;
|
||||||
|
}
|
||||||
virtual Gfx::SeFormat getFormat() const override
|
virtual Gfx::SeFormat getFormat() const override
|
||||||
{
|
{
|
||||||
return textureHandle->format;
|
return textureHandle->format;
|
||||||
@@ -311,6 +319,10 @@ public:
|
|||||||
{
|
{
|
||||||
return textureHandle->getNumSamples();
|
return textureHandle->getNumSamples();
|
||||||
}
|
}
|
||||||
|
virtual uint32 getMipLevels() const override
|
||||||
|
{
|
||||||
|
return textureHandle->getMipLevels();
|
||||||
|
}
|
||||||
virtual void changeLayout(Gfx::SeImageLayout newLayout) override;
|
virtual void changeLayout(Gfx::SeImageLayout newLayout) override;
|
||||||
virtual void* getNativeHandle() override
|
virtual void* getNativeHandle() override
|
||||||
{
|
{
|
||||||
|
|||||||
+1
-1
@@ -29,7 +29,7 @@ int main()
|
|||||||
AssetRegistry::init("D:\\Private\\Programming\\C++\\TestSeeleProject\\");
|
AssetRegistry::init("D:\\Private\\Programming\\C++\\TestSeeleProject\\");
|
||||||
AssetRegistry::importFile("D:\\Private\\Programming\\Unreal Engine\\Assets\\Ely\\Ely.fbx");
|
AssetRegistry::importFile("D:\\Private\\Programming\\Unreal Engine\\Assets\\Ely\\Ely.fbx");
|
||||||
AssetRegistry::importFile("D:\\Private\\Programming\\Unreal Engine\\Assets\\Cube\\cube.obj");
|
AssetRegistry::importFile("D:\\Private\\Programming\\Unreal Engine\\Assets\\Cube\\cube.obj");
|
||||||
AssetRegistry::importFile("D:\\Private\\Programming\\Unreal Engine\\Assets\\Plane\\plane.obj");
|
AssetRegistry::importFile("D:\\Private\\Programming\\Unreal Engine\\Assets\\Plane\\plane.fbx");
|
||||||
PPrimitiveComponent plane = new PrimitiveComponent(AssetRegistry::findMesh("plane"));
|
PPrimitiveComponent plane = new PrimitiveComponent(AssetRegistry::findMesh("plane"));
|
||||||
plane->setWorldScale(Vector(100, 100, 100));
|
plane->setWorldScale(Vector(100, 100, 100));
|
||||||
PPrimitiveComponent arissa = new PrimitiveComponent(AssetRegistry::findMesh("Ely"));
|
PPrimitiveComponent arissa = new PrimitiveComponent(AssetRegistry::findMesh("Ely"));
|
||||||
|
|||||||
Reference in New Issue
Block a user