Commit 2fea8816 authored by Lukas Tietze's avatar Lukas Tietze

Texturen

parent 2a256434
......@@ -19,6 +19,7 @@
#include "VkUtil/Shader.hpp"
#include "VkUtil/TopLevelAccelerationStructure.hpp"
#include "VkUtil/VkUtil.hpp"
#include "VkUtil/Texture.hpp"
#include "imgui_impl_vulkan.h"
namespace lib
......@@ -221,6 +222,7 @@ namespace lib
void CreateSwapChain();
void CreateImageViews();
void CreateCommandPool();
void LoadDummyTexture();
bool TryLoadModel();
void CreateUniformBuffers();
void UpdateUniformBuffers(uint32_t imageIndex, lib::time_diff_t timediff);
......
......@@ -20,11 +20,13 @@ namespace lib
CgContext *parent;
vk::Format format;
vk::Image image;
vk::ImageView view;
vk::DeviceMemory memory;
vk::Sampler sampler;
void LoadData(uint32_t width, uint32_t height, const void *data, uint8_t pixelByteCount);
constexpr DefaultFormat = vk::Format::eR8G8B8A8Srgb;
static constexpr vk::Format DefaultFormat = vk::Format::eR8G8B8A8Srgb;
public:
/**
......@@ -76,6 +78,10 @@ namespace lib
~Texture();
const vk::Image &getHandle();
const vk::Image &GetHandle() const;
const vk::ImageView &GetView() const;
const vk::Sampler &GetSampler() const;
}; // namespace lib
} // namespace lib
......@@ -22,6 +22,8 @@ namespace lib
uint32_t FindOptimalMemoryType(uint32_t typeFilter, const vk::PhysicalDeviceMemoryProperties &memoryProperties, vk::MemoryPropertyFlags flags);
vk::ImageView CreateImageView(const vk::Device &device, const vk::Image &image, vk::Format format);
void TransitionImageLayout(vk::CommandBuffer commandBuffer,
vk::Image image,
vk::Format format,
......
......@@ -106,6 +106,7 @@ lib::CgContext::CgContext(const AppCreateInfo &info, GLFWwindow *window) : windo
this->CreateImageViews();
this->CreateUniformBuffers();
this->CreateCommandPool();
this->LoadDummyTexture();
this->modelLoaded = this->TryLoadModel();
if (this->modelLoaded)
......@@ -555,6 +556,7 @@ void lib::CgContext::CreateLogicalDevice()
}
vk::PhysicalDeviceFeatures requiredDeviceFeatures;
requiredDeviceFeatures.samplerAnisotropy = true;
vk::StructureChain<vk::DeviceCreateInfo, vk::PhysicalDeviceDescriptorIndexingFeaturesEXT> deviceCreateInfo;
deviceCreateInfo.get<vk::DeviceCreateInfo>().pQueueCreateInfos = queueCreateInfos.data();
......@@ -657,21 +659,7 @@ void lib::CgContext::CreateImageViews()
for (const auto &image : this->swapChainImages)
{
vk::ImageViewCreateInfo createInfo;
createInfo.image = image;
createInfo.viewType = vk::ImageViewType::e2D;
createInfo.format = this->swapChainImageFormat;
createInfo.components.r = vk::ComponentSwizzle::eIdentity;
createInfo.components.g = vk::ComponentSwizzle::eIdentity;
createInfo.components.b = vk::ComponentSwizzle::eIdentity;
createInfo.components.a = vk::ComponentSwizzle::eIdentity;
createInfo.subresourceRange.aspectMask = vk::ImageAspectFlagBits::eColor;
createInfo.subresourceRange.baseMipLevel = 0;
createInfo.subresourceRange.levelCount = 1;
createInfo.subresourceRange.baseArrayLayer = 0;
createInfo.subresourceRange.layerCount = 1;
this->swapChainImageViews.push_back(this->logicalDevice.createImageView(createInfo));
this->swapChainImageViews.push_back(lib::CreateImageView(this->logicalDevice, image, this->swapChainImageFormat));
}
Log("Successfully created all image views!");
......@@ -1011,6 +999,24 @@ void lib::CgContext::DrawFrame(lib::time_diff_t delta)
this->frameIndex = (this->frameIndex + 1) % this->appInfo.maxImagesInFlight;
}
void lib::CgContext::LoadDummyTexture()
{
this->dummyTexture.reset(new lib::Texture(this, 256, 256, [](uint32_t x, uint32_t y) {
auto rx = x / 255.f;
auto ry = y / 255.f;
auto r = static_cast<uint8_t>(rx * 255);
auto g = static_cast<uint8_t>(rx * 255);
auto b = static_cast<uint8_t>(rx * 255);
auto a = uint8_t{255};
return (uint32_t{r} << 24) |
(uint32_t{g} << 16) |
(uint32_t{b} << 8) |
(uint32_t{a} << 0);
}));
}
bool lib::CgContext::TryLoadModel()
{
using model_t = AssimpModel<vertex_t, index_t>;
......@@ -1240,6 +1246,8 @@ void lib::CgContext::CreateDescriptorSetLayout()
layoutBindings.back().descriptorType = vk::DescriptorType::eStorageBuffer;
layoutBindings.back().stageFlags = vk::ShaderStageFlagBits::eClosestHitNV;
// dummy texture
vk::DescriptorSetLayoutCreateInfo createInfo;
createInfo.bindingCount = static_cast<uint32_t>(layoutBindings.size());
createInfo.pBindings = layoutBindings.data();
......
......@@ -44,6 +44,18 @@ lib::Texture::~Texture()
device.freeMemory(this->memory);
this->memory = nullptr;
}
if (this->view)
{
device.destroyImageView(this->view);
this->view = nullptr;
}
if (this->sampler)
{
device.destroySampler(this->sampler);
this->sampler = nullptr;
}
}
void lib::Texture::LoadData(uint32_t width, uint32_t height, const void *data, uint8_t pixelByteCount)
......@@ -106,9 +118,35 @@ void lib::Texture::LoadData(uint32_t width, uint32_t height, const void *data, u
vk::PipelineStageFlagBits::eTransfer, vk::PipelineStageFlagBits::eRayTracingShaderNV);
commandBuffer.Run();
this->view = CreateImageView(device, this->image, this->format);
vk::SamplerCreateInfo samplerCreateInfo;
samplerCreateInfo.magFilter = vk::Filter::eLinear;
samplerCreateInfo.minFilter = vk::Filter::eLinear;
samplerCreateInfo.addressModeU = vk::SamplerAddressMode::eRepeat;
samplerCreateInfo.addressModeV = vk::SamplerAddressMode::eRepeat;
samplerCreateInfo.addressModeW = vk::SamplerAddressMode::eRepeat;
samplerCreateInfo.anisotropyEnable = true;
samplerCreateInfo.maxAnisotropy = 16.f;
samplerCreateInfo.unnormalizedCoordinates = false;
samplerCreateInfo.compareEnable = false;
samplerCreateInfo.mipmapMode = vk::SamplerMipmapMode::eLinear;
this->sampler = device.createSampler(samplerCreateInfo);
}
const vk::Image &lib::Texture::getHandle()
const vk::Image &lib::Texture::GetHandle() const
{
return this->image;
}
const vk::ImageView &lib::Texture::GetView() const
{
return this->view;
}
const vk::Sampler &lib::Texture::GetSampler() const
{
return this->sampler;
}
\ No newline at end of file
......@@ -36,4 +36,19 @@ void lib::TransitionImageLayout(vk::CommandBuffer commandBuffer,
commandBuffer.pipelineBarrier(srcStage, dstStage, vk::DependencyFlagBits::eByRegion,
{}, {}, barrier);
}
vk::ImageView lib::CreateImageView(const vk::Device &device, const vk::Image &image, vk::Format format)
{
vk::ImageViewCreateInfo viewCreateInfo;
viewCreateInfo.image = image;
viewCreateInfo.viewType = vk::ImageViewType::e2D;
viewCreateInfo.format = format;
viewCreateInfo.subresourceRange.aspectMask = vk::ImageAspectFlagBits::eColor;
viewCreateInfo.subresourceRange.baseMipLevel = 0;
viewCreateInfo.subresourceRange.baseArrayLayer = 0;
viewCreateInfo.subresourceRange.levelCount = 1;
viewCreateInfo.subresourceRange.layerCount = 1;
return device.createImageView(viewCreateInfo);
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment