Texture implementation basics

This commit is contained in:
2024-07-04 17:00:46 +02:00
parent d79f74c21d
commit 3e73672538
9 changed files with 104 additions and 37 deletions

View File

@@ -35,7 +35,6 @@ namespace OpenVulkano
Shader shader;
SimpleDrawable drawable;
Node node;
Vector3f_SIMD position = {0, 0, -10};
OpenVulkano::Scene::UI::SimpleUi m_ui;
std::shared_ptr<OpenVulkano::Scene::UI::PerformanceInfo> m_perfInfo;
@@ -43,14 +42,15 @@ namespace OpenVulkano
public:
void Init() override
{
std::srand(1); // Fix seed for random numbers
scene.Init();
cam.Init(70, 16, 9, 0.1f, 100);
scene.SetCamera(&cam);
shader.AddShaderProgram(OpenVulkano::ShaderProgramType::VERTEX, "Shader/basic");
shader.AddShaderProgram(OpenVulkano::ShaderProgramType::FRAGMENT, "Shader/basic");
shader.AddShaderProgram(OpenVulkano::ShaderProgramType::FRAGMENT, "Shader/basicTexture");
shader.AddVertexInputDescription(OpenVulkano::Vertex::GetVertexInputDescription());
Geometry* geo = GeometryFactory::MakeCube(1, 1, 1, Vector4f((std::rand() % 255) / 255.0f, (std::rand() % 255) / 255.0f, (std::rand() % 255) / 255.0f, 1));
shader.AddDescriptorSetLayoutBinding(Texture::DESCRIPTOR_SET_LAYOUT_BINDING);
Geometry* geo = GeometryFactory::MakeCube();
mat.texture = &Texture::PLACEHOLDER;
drawable.Init(&shader, geo, &mat);
node.Init();
scene.GetRoot()->AddChild(&node);
@@ -60,10 +60,10 @@ namespace OpenVulkano
GetGraphicsAppManager()->GetRenderer()->SetScene(&scene);
camController.Init(&cam);
camController.SetDefaultKeybindings();
//camController.SetDefaultKeybindings();
camController.SetPosition({0, 0, 2});
std::shared_ptr<OpenVulkano::Scene::UI::PerformanceInfo> m_perfInfo = std::make_shared<OpenVulkano::Scene::UI::PerformanceInfo>();
m_perfInfo = std::make_shared<OpenVulkano::Scene::UI::PerformanceInfo>();
m_ui.AddElement(m_perfInfo);
GetGraphicsAppManager()->GetRenderer()->SetActiveUi(&m_ui);
}

View File

@@ -127,7 +127,7 @@ namespace OpenVulkano::Scene
{
CheckShaderInitState();
if (setId < 0) setId = static_cast<int>(descriptorSets.size() + 2);
if (setId < 2) throw std::runtime_error("Cant bind set id 0 or 1!");
if (setId < 2) throw std::runtime_error("Cant bind set id 0 or 1! They are used for node and camera!");
setId -= 2;
while (setId >= static_cast<int>(descriptorSets.size()))
{

View File

@@ -0,0 +1,30 @@
/*
* 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 https://mozilla.org/MPL/2.0/.
*/
#include "Texture.hpp"
namespace OpenVulkano::Scene
{
Texture Texture::PLACEHOLDER = Texture(true);
void Texture::MakePlaceholder(uint32_t width, uint32_t height, Math::Vector4uc color1, Math::Vector4uc color2)
{
if (textureBuffer) throw std::runtime_error("Texture data already initialized");
Math::Vector4uc* imageMemory = new Math::Vector4uc[width * height]();
for (uint32_t row = 0; row < height; row++)
{
for (uint32_t col = 0; col < width; col++)
{
imageMemory[row * width + col] = (((row & 0x10) == 0) ^ ((col & 0x10) == 0 )) ? color2 : color1;
}
}
textureBuffer = imageMemory;
resolution = {width, height, 1};
size = sizeof(Math::Vector4uc) * width * height;
format = DataFormat::B8G8R8A8_UNORM;
}
}

View File

@@ -9,18 +9,30 @@
#include "UpdateFrequency.hpp"
#include "Base/ICloseable.hpp"
#include "Math/Math.hpp"
#include "DataFormat.hpp"
#include "Scene/Shader/DescriptorInputDescription.hpp"
namespace OpenVulkano::Scene
{
class Texture
{
public:
static Texture PLACEHOLDER;
static constexpr inline DescriptorSetLayoutBinding DESCRIPTOR_SET_LAYOUT_BINDING = { 0, DescriptorSetLayoutBinding::Type::TYPE_COMBINED_IMAGE_SAMPLER, 1, ShaderProgramType::ALL_GRAPHICS };
Texture(bool placeholder = false) { if (placeholder) MakePlaceholder(); }
ICloseable* renderTexture = nullptr;
void* textureBuffer;
Math::Vector3ui resolution;
size_t size;
void* textureBuffer = nullptr;
Math::Vector3ui resolution = {0,0,0};
size_t size = 0;
DataFormat format = DataFormat::B8G8R8A8_UNORM;
bool updated = true;
UpdateFrequency updateFrequency = UpdateFrequency::Never;
ICloseable* vulkanTexture = nullptr;
void MakePlaceholder(uint32_t width = 128, uint32_t height = 128,
Math::Vector4uc color1 = {255, 135, 255, 255}, Math::Vector4uc color2 = {255, 225, 255, 255});
};
}

View File

@@ -6,6 +6,8 @@
#pragma once
#include <cinttypes>
namespace OpenVulkano::Scene
{
enum class UpdateFrequency : uint8_t

View File

@@ -16,6 +16,7 @@ namespace OpenVulkano::Vulkan
format = imageCreateInfo.format;
extent = imageCreateInfo.extent;
// TODO allocate from resource manager
const vk::MemoryRequirements memRequirements = device->device.getImageMemoryRequirements(image);
size = allocSize = memRequirements.size;
const vk::MemoryAllocateInfo memAllocInfo = { allocSize, device->GetMemoryType(memRequirements.memoryTypeBits, memoryPropertyFlags) };
@@ -38,27 +39,19 @@ namespace OpenVulkano::Vulkan
{
this->device = device->device;
vk::ImageCreateInfo imgCreateInfo;
imgCreateInfo.imageType = vk::ImageType::e2D;
imgCreateInfo.extent = resolution;
imgCreateInfo.mipLevels = 1;
imgCreateInfo.arrayLayers = 1;
imgCreateInfo.format = vk::Format::eR8G8B8Srgb;
imgCreateInfo.tiling = vk::ImageTiling::eOptimal;
imgCreateInfo.initialLayout = vk::ImageLayout::eUndefined;
imgCreateInfo.usage = vk::ImageUsageFlagBits::eTransferDst | vk::ImageUsageFlagBits::eSampled;
imgCreateInfo.sharingMode = vk::SharingMode::eExclusive;
imgCreateInfo.samples = vk::SampleCountFlagBits::e1;
vk::ImageCreateInfo imgCreateInfo { {}, vk::ImageType::e2D, vk::Format::eB8G8R8A8Unorm, resolution, 1, 1 };
vk::ImageViewCreateInfo imgViewCreateInfo;
imgViewCreateInfo.image = image;
imgViewCreateInfo.format = imgCreateInfo.format;
imgViewCreateInfo.viewType = vk::ImageViewType::e2D;
imgCreateInfo.usage = vk::ImageUsageFlagBits::eTransferDst | vk::ImageUsageFlagBits::eSampled;
imgCreateInfo.tiling = vk::ImageTiling::eOptimal;
imgCreateInfo.sharingMode = vk::SharingMode::eExclusive;
imgCreateInfo.initialLayout = vk::ImageLayout::eUndefined;
vk::ImageViewCreateInfo imgViewCreateInfo { {}, image, vk::ImageViewType::e2D, imgCreateInfo.format };
imgViewCreateInfo.subresourceRange.aspectMask = vk::ImageAspectFlagBits::eColor;
imgViewCreateInfo.subresourceRange.baseMipLevel = 0;
imgViewCreateInfo.subresourceRange.levelCount = 1;
imgViewCreateInfo.subresourceRange.baseArrayLayer = 0;
imgViewCreateInfo.subresourceRange.levelCount = 1;
imgViewCreateInfo.subresourceRange.layerCount = VK_REMAINING_ARRAY_LAYERS;
Init(device, imgCreateInfo, imgViewCreateInfo);
CreateSampler();

View File

@@ -148,7 +148,7 @@ namespace OpenVulkano::Vulkan
const std::unique_lock lock(mutex);
if (material->texture && !material->texture->renderTexture)
{
material->texture->renderTexture = PrepareTexture(material->texture);
PrepareTexture(material->texture);
}
}
@@ -291,6 +291,21 @@ namespace OpenVulkano::Vulkan
vk::BufferImageCopy region(0, 0, 0, { vk::ImageAspectFlagBits::eColor, 0, 0, 1 }, { 0, 0, 0 }, image->extent);
cmdBuffers[currentBuffer].copyBufferToImage(uploadBuffer->buffer, image->image, vk::ImageLayout::eTransferDstOptimal, 1, &region);
vk::ImageMemoryBarrier barrier {};
barrier.oldLayout = vk::ImageLayout::eUndefined;
barrier.newLayout = vk::ImageLayout::eShaderReadOnlyOptimal;
barrier.image = image->image;
barrier.subresourceRange.aspectMask = vk::ImageAspectFlagBits::eColor;
barrier.subresourceRange.baseMipLevel = 0;
barrier.subresourceRange.levelCount = 1;
barrier.subresourceRange.baseArrayLayer = 0;
barrier.subresourceRange.layerCount = 1;
barrier.setSrcAccessMask({});
barrier.setDstAccessMask(vk::AccessFlagBits::eTransferWrite);
// TODO set access masks for mip and array layers
cmdBuffers[currentBuffer].pipelineBarrier(vk::PipelineStageFlagBits::eTopOfPipe, vk::PipelineStageFlagBits::eTransfer, {}, 0, nullptr, 0, nullptr, 1, &barrier );
FreeBuffer(uploadBuffer);
}
@@ -365,8 +380,7 @@ namespace OpenVulkano::Vulkan
{
VulkanTexture* vkTexture = new VulkanTexture();
vkTexture->Init(this, texture);
//vkTexture->
vkTexture->Init(this, texture, GetDescriptorLayoutSet(Scene::Texture::DESCRIPTOR_SET_LAYOUT_BINDING), Scene::Texture::DESCRIPTOR_SET_LAYOUT_BINDING, 2);
return vkTexture;
}

View File

@@ -40,6 +40,7 @@ namespace OpenVulkano
class ResourceManager : public ICloseable, public IShaderOwner
{
friend UniformBuffer;
friend VulkanTexture;
Context* context;
vk::Device device = nullptr;

View File

@@ -10,6 +10,7 @@
#include "Vulkan/Image.hpp"
#include "Vulkan/Context.hpp"
#include "Vulkan/Resources/ResourceManager.hpp"
#include "Vulkan/Scene/VulkanShader.hpp"
#include "Scene/Texture.hpp"
namespace OpenVulkano::Vulkan
@@ -18,22 +19,36 @@ namespace OpenVulkano::Vulkan
{
public:
Scene::Texture* m_texture = nullptr;
vk::DescriptorSet m_descriptorSet;
uint32_t m_setId;
virtual void Init(ResourceManager* resManager, Scene::Texture* texture)
virtual void Init(ResourceManager* resManager, Scene::Texture* texture, vk::DescriptorSetLayout* descriptorSetLayout, const DescriptorSetLayoutBinding& binding, uint32_t setId)
{
this->m_texture = texture;
m_texture = texture;
m_setId = setId;
Image::Init(resManager->GetContext()->device.get(), { texture->resolution.x, texture->resolution.y, texture->resolution.z });
resManager->CopyDataToImage(m_texture->size, m_texture->textureBuffer, this);
texture->updated = false;
// Setup Descriptor set
const vk::DescriptorSetAllocateInfo descSetAllocInfo = { ResourceManager::INSTANCE->descriptorPool, 1, descriptorSetLayout };
m_descriptorSet = resManager->GetContext()->device->device.allocateDescriptorSets(descSetAllocInfo)[0];
vk::DescriptorImageInfo imageInfo = { sampler, view, vk::ImageLayout::eShaderReadOnlyOptimal };
vk::WriteDescriptorSet writeDescriptorSet = { m_descriptorSet, binding.bindingId, 0, 1 };
writeDescriptorSet.descriptorType = static_cast<vk::DescriptorType>(binding.descriptorType);
writeDescriptorSet.pImageInfo = &imageInfo;
resManager->GetContext()->device->device.updateDescriptorSets(1, &writeDescriptorSet, 0, nullptr);
texture->renderTexture = this;
}
void Record(VulkanDrawContext* context) override
{
//cmdBuffer.bindDescriptorSets(vk::PipelineBindPoint::eGraphics, )
context->commandBuffer.bindDescriptorSets(vk::PipelineBindPoint::eGraphics, context->GetShader()->pipelineLayout, m_setId, 1, &m_descriptorSet, 0, nullptr);
}
};
class VulkanTextureDynamic : VulkanTexture
/*class VulkanTextureDynamic : VulkanTexture
{
public:
uint32_t lastUpdate = -1;
@@ -48,13 +63,13 @@ namespace OpenVulkano::Vulkan
void Record(VulkanDrawContext* context) override
{
/*if(bufferId != lastUpdate && m_texture->updated)
if(bufferId != lastUpdate && m_texture->updated)
{
lastUpdate = bufferId;
resourceManager->CopyDataToImage(m_texture->size, m_texture->textureBuffer, this);
m_texture->updated = false;
}
VulkanTexture::Record(cmdBuffer, bufferId);*/
VulkanTexture::Record(cmdBuffer, bufferId);
}
};
};*/
}