Compare commits
No commits in common. "c62f7270520835d99bb8bfac2747e7e603fe6af9" and "3088a42a079e9a0760468972032d9a700a009ec0" have entirely different histories.
c62f727052
...
3088a42a07
@ -1,11 +1,11 @@
|
||||
# NF library CMakeLists.txt
|
||||
add_library(NothinFancy STATIC "src/Engine.cpp" "src/include/nf.h" "src/pch.h" "src/util.h" "src/util/log.h" "src/util/log.cpp" "src/include/nf/config.h" "src/util/util.cpp" "src/util/file.h" "src/util/file.cpp" "src/client/Client.h" "src/client/Client.cpp" "src/client/Window.h" "src/client/Window.cpp" "src/client/render/RenderEngine.h" "src/client/render/RenderEngine.cpp" "src/client/render/ShaderModule.h" "src/client/render/ShaderModule.cpp" "src/client/render/GraphicsResource.h" "src/client/render/Buffer.h" "src/client/render/Buffer.cpp" "src/client/render/VideoMemoryAllocator.h" "src/client/render/VideoMemoryAllocator.cpp" "src/client/render/Image.h" "src/client/render/Image.cpp" "src/client/render/CommandPool.h" "src/client/render/CommandPool.cpp")
|
||||
add_library(NothinFancy STATIC "src/Engine.cpp" "src/include/nf.h" "src/pch.h" "src/util.h" "src/util/log.h" "src/util/log.cpp" "src/include/nf/config.h" "src/util/util.cpp" "src/util/file.h" "src/util/file.cpp" "src/client/Client.h" "src/client/Client.cpp" "src/client/Window.h" "src/client/Window.cpp" "src/client/render/RenderEngine.h" "src/client/render/RenderEngine.cpp" "src/client/render/ShaderModule.h" "src/client/render/ShaderModule.cpp")
|
||||
|
||||
# Use C++20
|
||||
set_property(TARGET NothinFancy PROPERTY CXX_STANDARD 20)
|
||||
|
||||
# Additional include directories
|
||||
target_include_directories(NothinFancy PUBLIC "src" "src/include" "dep/include")
|
||||
target_include_directories(NothinFancy PUBLIC "src" "src/include")
|
||||
|
||||
# Use precompiled header
|
||||
target_precompile_headers(NothinFancy PUBLIC "src/pch.h")
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -1,14 +1,9 @@
|
||||
#version 450
|
||||
|
||||
// Inputs
|
||||
layout(location = 0) in vec2 inTextureCoordinates;
|
||||
layout(location = 0) in vec3 fragColor;
|
||||
|
||||
// Outputs
|
||||
layout(location = 0) out vec4 outColor;
|
||||
|
||||
// Uniforms
|
||||
layout(binding = 1) uniform sampler2D image;
|
||||
|
||||
void main() {
|
||||
outColor = texture(image, inTextureCoordinates);
|
||||
outColor = vec4(fragColor, 1.0);
|
||||
}
|
||||
|
@ -1,18 +1,20 @@
|
||||
#version 450
|
||||
|
||||
// Inputs
|
||||
layout(location = 0) in vec3 inPosition;
|
||||
layout(location = 1) in vec2 inTextureCoordinates;
|
||||
vec2 trianglePositions[3] = vec2[] (
|
||||
vec2(0.0, -0.5),
|
||||
vec2(0.5, 0.5),
|
||||
vec2(-0.5, 0.5)
|
||||
);
|
||||
|
||||
// Outputs
|
||||
layout(location = 0) out vec2 outTextureCoordinates;
|
||||
vec3 triangleColors[3] = vec3[] (
|
||||
vec3(1.0, 0.0, 0.0),
|
||||
vec3(0.0, 1.0, 0.0),
|
||||
vec3(0.0, 0.0, 1.0)
|
||||
);
|
||||
|
||||
// Uniforms
|
||||
layout(binding = 0) uniform MVPMatrixUniformBufferObject {
|
||||
mat4 mvp;
|
||||
} mvpUBO;
|
||||
layout(location = 0) out vec3 fragColor;
|
||||
|
||||
void main() {
|
||||
outTextureCoordinates = inTextureCoordinates;
|
||||
gl_Position = mvpUBO.mvp * vec4(inPosition, 1.0);
|
||||
fragColor = triangleColors[gl_VertexIndex];
|
||||
gl_Position = vec4(trianglePositions[gl_VertexIndex], 0.0, 1.0);
|
||||
}
|
||||
|
@ -18,18 +18,8 @@ namespace nf::client {
|
||||
std::thread inputThread(&Client::runInputThread, this, std::move(windowPromise));
|
||||
m_renderEngine = std::make_unique<render::RenderEngine>(std::move(windowFuture.get()), m_config.display);
|
||||
|
||||
auto fpsClock1 = std::chrono::high_resolution_clock::now(), fpsClock2 = fpsClock1;
|
||||
unsigned int frame = 0;
|
||||
while (m_running) {
|
||||
m_renderEngine->doFrame();
|
||||
frame++;
|
||||
fpsClock2 = std::chrono::high_resolution_clock::now();
|
||||
std::chrono::duration<double> duration = fpsClock2 - fpsClock1;
|
||||
if (duration.count() >= 1.0) {
|
||||
NFLog(std::format("FPS: {}", frame));
|
||||
frame = 0;
|
||||
fpsClock1 = std::chrono::high_resolution_clock::now();
|
||||
}
|
||||
}
|
||||
|
||||
inputThread.join();
|
||||
|
@ -12,7 +12,6 @@ namespace nf::client {
|
||||
, m_styleFullscreen(WS_POPUP)
|
||||
, m_currentWidth()
|
||||
, m_currentHeight()
|
||||
, m_active(true)
|
||||
{
|
||||
NFLog("Creating window");
|
||||
|
||||
@ -30,8 +29,6 @@ namespace nf::client {
|
||||
|
||||
void Window::setDisplay(DisplayConfig& config) {
|
||||
NFLog("Setting window display");
|
||||
bool wasShown = IsWindowVisible(m_handle);
|
||||
show(false);
|
||||
|
||||
// TODO: Only use "active" monitor when starting windowed
|
||||
POINT cursor = {};
|
||||
@ -58,15 +55,12 @@ namespace nf::client {
|
||||
SetWindowLongPtr(m_handle, GWL_STYLE, m_styleFullscreen);
|
||||
windowX = monitorX, windowY = monitorY;
|
||||
windowWidth = monitorWidth, windowHeight = monitorHeight;
|
||||
m_currentWidth = windowWidth, m_currentHeight = windowHeight;
|
||||
config.width = windowWidth, config.height = windowHeight;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
SetWindowPos(m_handle, nullptr, windowX, windowY, windowWidth, windowHeight, SWP_NOZORDER | SWP_FRAMECHANGED);
|
||||
|
||||
show(wasShown);
|
||||
}
|
||||
|
||||
void Window::runLoop() {
|
||||
@ -77,18 +71,12 @@ namespace nf::client {
|
||||
TranslateMessage(&msg);
|
||||
DispatchMessage(&msg);
|
||||
}
|
||||
|
||||
m_active = false;
|
||||
}
|
||||
|
||||
void Window::show(bool show) {
|
||||
ShowWindow(m_handle, show ? SW_SHOW : SW_HIDE);
|
||||
}
|
||||
|
||||
bool Window::isActive() const {
|
||||
return m_active;
|
||||
}
|
||||
|
||||
void Window::registerWindowClass() {
|
||||
WNDCLASS wc = {};
|
||||
wc.lpszClassName = m_wndClassName;
|
||||
|
@ -14,7 +14,6 @@ namespace nf::client {
|
||||
void setDisplay(DisplayConfig& config);
|
||||
void runLoop();
|
||||
void show(bool show = true);
|
||||
bool isActive() const;
|
||||
|
||||
~Window();
|
||||
private:
|
||||
@ -29,7 +28,5 @@ namespace nf::client {
|
||||
const DWORD m_styleFullscreen;
|
||||
|
||||
unsigned int m_currentWidth, m_currentHeight;
|
||||
|
||||
bool m_active;
|
||||
};
|
||||
}
|
||||
|
@ -1,91 +0,0 @@
|
||||
// Buffer class implementation
|
||||
#include "pch.h"
|
||||
|
||||
#include "Buffer.h"
|
||||
#include "util.h"
|
||||
|
||||
namespace nf::client::render {
|
||||
Buffer::Buffer(BufferType type, const VkDevice& device, VideoMemoryAllocator& allocator, const CommandPool& commandPool, void* bufferData, size_t bufferSize)
|
||||
: GraphicsResource(device)
|
||||
, m_allocator(allocator)
|
||||
, m_handle()
|
||||
, m_allocation()
|
||||
, m_indexCount()
|
||||
{
|
||||
if (type == BufferType::Staging) {
|
||||
createBuffer(bufferSize, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, m_handle);
|
||||
m_allocator.allocateForBuffer(m_handle, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, m_allocation);
|
||||
if (m_allocation.mappedMemoryPointer)
|
||||
memcpy(m_allocation.mappedMemoryPointer, bufferData, bufferSize);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (type == BufferType::Uniform) {
|
||||
createBuffer(bufferSize, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, m_handle);
|
||||
m_allocator.allocateForBuffer(m_handle, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, m_allocation);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
Buffer stagingBuffer(BufferType::Staging, m_device, m_allocator, commandPool, bufferData, bufferSize);
|
||||
|
||||
VkBufferUsageFlags mainUsage = NULL;
|
||||
|
||||
switch (type) {
|
||||
case BufferType::Vertex:
|
||||
mainUsage = VK_BUFFER_USAGE_VERTEX_BUFFER_BIT;
|
||||
break;
|
||||
|
||||
case BufferType::Index:
|
||||
mainUsage = VK_BUFFER_USAGE_INDEX_BUFFER_BIT;
|
||||
m_indexCount = bufferSize / sizeof(uint32_t);
|
||||
break;
|
||||
}
|
||||
|
||||
createBuffer(bufferSize, VK_BUFFER_USAGE_TRANSFER_DST_BIT | mainUsage, m_handle);
|
||||
m_allocator.allocateForBuffer(m_handle, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, m_allocation);
|
||||
|
||||
copyBuffer(stagingBuffer.getHandle(), m_handle, bufferSize, commandPool);
|
||||
}
|
||||
|
||||
const VkBuffer& Buffer::getHandle() const {
|
||||
return m_handle;
|
||||
}
|
||||
|
||||
void* Buffer::getPointer() const {
|
||||
return m_allocation.mappedMemoryPointer;
|
||||
}
|
||||
|
||||
uint32_t Buffer::getIndicesCount() const {
|
||||
return m_indexCount;
|
||||
}
|
||||
|
||||
void Buffer::createBuffer(VkDeviceSize size, VkBufferUsageFlags usage, VkBuffer& buffer) {
|
||||
VkBufferCreateInfo bufferCI = { VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO };
|
||||
bufferCI.size = size;
|
||||
bufferCI.usage = usage;
|
||||
|
||||
if (vkCreateBuffer(m_device, &bufferCI, nullptr, &buffer) != VK_SUCCESS)
|
||||
NFError("Could not create vertex buffer.");
|
||||
}
|
||||
|
||||
void Buffer::copyBuffer(VkBuffer bufferSource, VkBuffer bufferDestination, VkDeviceSize size, const CommandPool& commandPool) {
|
||||
VkCommandBuffer commandBuffer = commandPool.beginOneTimeExecution();
|
||||
|
||||
VkBufferCopy bufferCopyRegion = {};
|
||||
bufferCopyRegion.size = size;
|
||||
vkCmdCopyBuffer(commandBuffer, bufferSource, bufferDestination, 1, &bufferCopyRegion);
|
||||
|
||||
commandPool.endOneTimeExecution(commandBuffer);
|
||||
}
|
||||
|
||||
void Buffer::destroyBuffer(VkBuffer buffer, VideoMemoryAllocation& allocation) {
|
||||
m_allocator.deallocate(allocation);
|
||||
vkDestroyBuffer(m_device, buffer, nullptr);
|
||||
}
|
||||
|
||||
Buffer::~Buffer() {
|
||||
destroyBuffer(m_handle, m_allocation);
|
||||
}
|
||||
}
|
@ -1,37 +0,0 @@
|
||||
// Buffer class header
|
||||
#pragma once
|
||||
|
||||
#include "GraphicsResource.h"
|
||||
#include "VideoMemoryAllocator.h"
|
||||
#include "CommandPool.h"
|
||||
|
||||
namespace nf::client::render {
|
||||
enum class BufferType {
|
||||
Staging,
|
||||
Vertex,
|
||||
Index,
|
||||
Uniform
|
||||
};
|
||||
|
||||
class Buffer : GraphicsResource {
|
||||
public:
|
||||
Buffer(BufferType type, const VkDevice& device, VideoMemoryAllocator& allocator, const CommandPool& commandPool, void* bufferData, size_t bufferSize);
|
||||
|
||||
const VkBuffer& getHandle() const;
|
||||
void* getPointer() const;
|
||||
uint32_t getIndicesCount() const;
|
||||
|
||||
~Buffer();
|
||||
private:
|
||||
void createBuffer(VkDeviceSize size, VkBufferUsageFlags usage, VkBuffer& buffer);
|
||||
void copyBuffer(VkBuffer sourceBuffer, VkBuffer destinationBuffer, VkDeviceSize size, const CommandPool& commandPool);
|
||||
|
||||
void destroyBuffer(VkBuffer buffer, VideoMemoryAllocation& allocation);
|
||||
|
||||
VideoMemoryAllocator& m_allocator;
|
||||
|
||||
VkBuffer m_handle;
|
||||
VideoMemoryAllocation m_allocation;
|
||||
uint32_t m_indexCount;
|
||||
};
|
||||
}
|
@ -1,61 +0,0 @@
|
||||
// CommandPool class implementation
|
||||
#include "pch.h"
|
||||
|
||||
#include "CommandPool.h"
|
||||
#include "util.h"
|
||||
|
||||
namespace nf::client::render {
|
||||
CommandPool::CommandPool(const VkDevice& device, const VkQueue& queue, uint32_t queueFamilyIndex)
|
||||
: GraphicsResource(device)
|
||||
, m_queue(queue)
|
||||
, m_handle()
|
||||
{
|
||||
VkCommandPoolCreateInfo commandPoolCI = { VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO };
|
||||
commandPoolCI.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
|
||||
commandPoolCI.queueFamilyIndex = queueFamilyIndex;
|
||||
|
||||
if (vkCreateCommandPool(m_device, &commandPoolCI, nullptr, &m_handle) != VK_SUCCESS)
|
||||
NFError("Could not create command pool.");
|
||||
}
|
||||
|
||||
VkCommandBuffer CommandPool::allocateCommandBuffer() const {
|
||||
VkCommandBuffer commandBuffer = nullptr;
|
||||
|
||||
VkCommandBufferAllocateInfo commandBufferAI = { VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO };
|
||||
commandBufferAI.commandPool = m_handle;
|
||||
commandBufferAI.commandBufferCount = 1;
|
||||
|
||||
if (vkAllocateCommandBuffers(m_device, &commandBufferAI, &commandBuffer) != VK_SUCCESS)
|
||||
NFError("Could not create command buffer.");
|
||||
|
||||
return commandBuffer;
|
||||
}
|
||||
|
||||
VkCommandBuffer CommandPool::beginOneTimeExecution() const {
|
||||
VkCommandBuffer commandBuffer = allocateCommandBuffer();
|
||||
|
||||
VkCommandBufferBeginInfo beginInfo = { VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO };
|
||||
beginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
|
||||
|
||||
vkBeginCommandBuffer(commandBuffer, &beginInfo);
|
||||
|
||||
return commandBuffer;
|
||||
}
|
||||
|
||||
void CommandPool::endOneTimeExecution(VkCommandBuffer commandBuffer) const {
|
||||
vkEndCommandBuffer(commandBuffer);
|
||||
|
||||
VkSubmitInfo submitInfo = { VK_STRUCTURE_TYPE_SUBMIT_INFO };
|
||||
submitInfo.commandBufferCount = 1;
|
||||
submitInfo.pCommandBuffers = &commandBuffer;
|
||||
|
||||
vkQueueSubmit(m_queue, 1, &submitInfo, nullptr);
|
||||
vkQueueWaitIdle(m_queue);
|
||||
|
||||
vkFreeCommandBuffers(m_device, m_handle, 1, &commandBuffer);
|
||||
}
|
||||
|
||||
CommandPool::~CommandPool() {
|
||||
vkDestroyCommandPool(m_device, m_handle, nullptr);
|
||||
}
|
||||
}
|
@ -1,22 +0,0 @@
|
||||
// CommandPool class header
|
||||
#pragma once
|
||||
|
||||
#include "GraphicsResource.h"
|
||||
|
||||
namespace nf::client::render {
|
||||
class CommandPool : GraphicsResource {
|
||||
public:
|
||||
CommandPool(const VkDevice& device, const VkQueue& queue, uint32_t queueFamilyIndex);
|
||||
|
||||
VkCommandBuffer allocateCommandBuffer() const;
|
||||
|
||||
VkCommandBuffer beginOneTimeExecution() const;
|
||||
void endOneTimeExecution(VkCommandBuffer commandBuffer) const;
|
||||
|
||||
~CommandPool();
|
||||
private:
|
||||
const VkQueue& m_queue;
|
||||
|
||||
VkCommandPool m_handle;
|
||||
};
|
||||
}
|
@ -1,14 +0,0 @@
|
||||
// Vulkan resource base class header
|
||||
#pragma once
|
||||
|
||||
namespace nf::client::render {
|
||||
class GraphicsResource {
|
||||
public:
|
||||
GraphicsResource(const VkDevice& device)
|
||||
: m_device(device)
|
||||
{}
|
||||
|
||||
protected:
|
||||
const VkDevice& m_device;
|
||||
};
|
||||
}
|
@ -1,131 +0,0 @@
|
||||
// Image class implementation
|
||||
#include "pch.h"
|
||||
|
||||
#include "Image.h"
|
||||
#include "util.h"
|
||||
#include "Buffer.h"
|
||||
|
||||
#define STB_IMAGE_IMPLEMENTATION
|
||||
#include "stb_image.h"
|
||||
|
||||
namespace nf::client::render {
|
||||
Image::Image(ImageType type, const VkDevice& device, VideoMemoryAllocator& allocator, const CommandPool& commandPool, const std::string& imageData, VkExtent2D attachmentExtent)
|
||||
: GraphicsResource(device)
|
||||
, m_allocator(allocator)
|
||||
, m_handle()
|
||||
, m_allocation()
|
||||
, m_view()
|
||||
{
|
||||
VkFormat imageFormat = VK_FORMAT_UNDEFINED;
|
||||
|
||||
switch (type) {
|
||||
case ImageType::Texture:
|
||||
imageFormat = VK_FORMAT_R8G8B8A8_SRGB;
|
||||
createTextureImage(imageFormat, commandPool, imageData);
|
||||
break;
|
||||
|
||||
case ImageType::DepthAttachment:
|
||||
imageFormat = VK_FORMAT_D32_SFLOAT;
|
||||
createImage(imageFormat, attachmentExtent, VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT);
|
||||
break;
|
||||
}
|
||||
|
||||
createImageView(imageFormat);
|
||||
}
|
||||
|
||||
const VkImageView& Image::getView() const {
|
||||
return m_view;
|
||||
}
|
||||
|
||||
void Image::createTextureImage(VkFormat imageFormat, const CommandPool& commandPool, const std::string& imageData) {
|
||||
int imageWidth = 0, imageHeight = 0, numChannels = 0;
|
||||
stbi_uc* rawImageData = stbi_load_from_memory(reinterpret_cast<const stbi_uc*>(imageData.data()), imageData.size(), &imageWidth, &imageHeight, &numChannels, STBI_rgb_alpha);
|
||||
|
||||
VkDeviceSize imageSize = static_cast<VkDeviceSize>(imageWidth) * imageHeight * 4;
|
||||
|
||||
Buffer stagingBuffer(BufferType::Staging, m_device, m_allocator, commandPool, rawImageData, imageSize);
|
||||
|
||||
stbi_image_free(rawImageData);
|
||||
|
||||
VkExtent2D imageExtent = { static_cast<uint32_t>(imageWidth), static_cast<uint32_t>(imageHeight) };
|
||||
createImage(imageFormat, imageExtent, VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT);
|
||||
|
||||
VkCommandBuffer commandBuffer = commandPool.beginOneTimeExecution();
|
||||
|
||||
// Image transition undefined -> transfer destination
|
||||
VkImageMemoryBarrier imageMemoryBarrier = { {VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER} };
|
||||
imageMemoryBarrier.image = m_handle;
|
||||
imageMemoryBarrier.newLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
|
||||
imageMemoryBarrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
|
||||
imageMemoryBarrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
|
||||
imageMemoryBarrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
|
||||
imageMemoryBarrier.subresourceRange.levelCount = 1;
|
||||
imageMemoryBarrier.subresourceRange.layerCount = 1;
|
||||
imageMemoryBarrier.srcAccessMask = 0;
|
||||
imageMemoryBarrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
|
||||
vkCmdPipelineBarrier(commandBuffer, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, nullptr, 0, nullptr, 1, &imageMemoryBarrier);
|
||||
|
||||
commandPool.endOneTimeExecution(commandBuffer);
|
||||
|
||||
commandBuffer = commandPool.beginOneTimeExecution();
|
||||
|
||||
VkBufferImageCopy bufferImageCopyRegion = {};
|
||||
bufferImageCopyRegion.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
|
||||
bufferImageCopyRegion.imageSubresource.layerCount = 1;
|
||||
bufferImageCopyRegion.imageExtent = { static_cast<uint32_t>(imageWidth), static_cast<uint32_t>(imageHeight), 1 };
|
||||
vkCmdCopyBufferToImage(commandBuffer, stagingBuffer.getHandle(), m_handle, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &bufferImageCopyRegion);
|
||||
|
||||
commandPool.endOneTimeExecution(commandBuffer);
|
||||
|
||||
commandBuffer = commandPool.beginOneTimeExecution();
|
||||
|
||||
// Image transition transfer destination -> shader read only
|
||||
imageMemoryBarrier.oldLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
|
||||
imageMemoryBarrier.newLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
|
||||
imageMemoryBarrier.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
|
||||
imageMemoryBarrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT;
|
||||
vkCmdPipelineBarrier(commandBuffer, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT, 0, 0, nullptr, 0, nullptr, 1, &imageMemoryBarrier);
|
||||
|
||||
commandPool.endOneTimeExecution(commandBuffer);
|
||||
}
|
||||
|
||||
void Image::createImage(VkFormat format, VkExtent2D& size, VkImageUsageFlags usage) {
|
||||
VkImageCreateInfo imageCI = { VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO };
|
||||
imageCI.imageType = VK_IMAGE_TYPE_2D;
|
||||
imageCI.extent = { size.width, size.height, 1 };
|
||||
imageCI.mipLevels = 1;
|
||||
imageCI.arrayLayers = 1;
|
||||
imageCI.samples = VK_SAMPLE_COUNT_1_BIT;
|
||||
imageCI.format = format;
|
||||
imageCI.usage = usage;
|
||||
imageCI.tiling = VK_IMAGE_TILING_OPTIMAL;
|
||||
|
||||
if (vkCreateImage(m_device, &imageCI, nullptr, &m_handle) != VK_SUCCESS)
|
||||
NFError("Could not create image.");
|
||||
|
||||
m_allocator.allocateForImage(m_handle, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, m_allocation);
|
||||
}
|
||||
|
||||
void Image::createImageView(VkFormat format) {
|
||||
VkImageAspectFlags aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
|
||||
if (format == VK_FORMAT_D32_SFLOAT)
|
||||
aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
|
||||
|
||||
VkImageViewCreateInfo imageViewCI = { VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO };
|
||||
imageViewCI.image = m_handle;
|
||||
imageViewCI.viewType = VK_IMAGE_VIEW_TYPE_2D;
|
||||
imageViewCI.format = format;
|
||||
imageViewCI.subresourceRange.aspectMask = aspectMask;
|
||||
imageViewCI.subresourceRange.levelCount = 1;
|
||||
imageViewCI.subresourceRange.layerCount = 1;
|
||||
|
||||
if (vkCreateImageView(m_device, &imageViewCI, nullptr, &m_view) != VK_SUCCESS)
|
||||
NFError("Could not create image view.");
|
||||
}
|
||||
|
||||
Image::~Image() {
|
||||
vkDestroyImageView(m_device, m_view, nullptr);
|
||||
m_allocator.deallocate(m_allocation);
|
||||
vkDestroyImage(m_device, m_handle, nullptr);
|
||||
}
|
||||
}
|
@ -1,32 +0,0 @@
|
||||
// Image class header
|
||||
#pragma once
|
||||
|
||||
#include "GraphicsResource.h"
|
||||
#include "VideoMemoryAllocator.h"
|
||||
#include "CommandPool.h"
|
||||
|
||||
namespace nf::client::render {
|
||||
enum class ImageType {
|
||||
Texture,
|
||||
DepthAttachment
|
||||
};
|
||||
|
||||
class Image : GraphicsResource {
|
||||
public:
|
||||
Image(ImageType type, const VkDevice& device, VideoMemoryAllocator& allocator, const CommandPool& commandPool, const std::string& imageData, VkExtent2D attachmentExtent = {});
|
||||
|
||||
const VkImageView& getView() const;
|
||||
|
||||
~Image();
|
||||
private:
|
||||
void createTextureImage(VkFormat imageFormat, const CommandPool& commandPool, const std::string& imageData);
|
||||
void createImage(VkFormat imageFormat, VkExtent2D& size, VkImageUsageFlags usage);
|
||||
void createImageView(VkFormat format);
|
||||
|
||||
VideoMemoryAllocator& m_allocator;
|
||||
|
||||
VkImage m_handle;
|
||||
VideoMemoryAllocation m_allocation;
|
||||
VkImageView m_view;
|
||||
};
|
||||
}
|
@ -6,12 +6,6 @@
|
||||
#include "ShaderModule.h"
|
||||
|
||||
namespace nf::client::render {
|
||||
// Vertex layout definition
|
||||
struct Vertex {
|
||||
glm::vec3 position;
|
||||
glm::vec2 textureCoordinates;
|
||||
};
|
||||
|
||||
RenderEngine::RenderEngine(std::shared_ptr<Window> window, DisplayConfig display)
|
||||
: m_window(window)
|
||||
, m_display(display)
|
||||
@ -23,31 +17,18 @@ namespace nf::client::render {
|
||||
, m_device()
|
||||
, m_queueGraphics()
|
||||
, m_queuePresent()
|
||||
, m_commandPool()
|
||||
, m_commandBuffer()
|
||||
, m_semaphoreImageAvailable()
|
||||
, m_semaphoreRenderingDone()
|
||||
, m_fenceFrameInFlight()
|
||||
, m_renderPassOutput()
|
||||
, m_swapchain()
|
||||
, m_swapchainImageFormat()
|
||||
, m_swapchainExtent()
|
||||
, m_swapchainImages()
|
||||
, m_swapchainImageViews()
|
||||
, m_renderPassOutput()
|
||||
, m_pipelineOutputDescriptorSetLayout()
|
||||
, m_pipelineOutputLayout()
|
||||
, m_pipelineOutputDescriptorPool()
|
||||
, m_pipelineOutput()
|
||||
, m_allocator()
|
||||
, m_imageDepth()
|
||||
, m_swapchainFramebuffers()
|
||||
, m_bufferVertex()
|
||||
, m_bufferIndex()
|
||||
, m_bufferUniformMVP()
|
||||
, m_imageTest()
|
||||
, m_sampler()
|
||||
, m_pipelineOutputDescriptorSet()
|
||||
|
||||
, m_pipelineLayoutOutput()
|
||||
, m_pipelineOutput()
|
||||
, m_commandPool()
|
||||
, m_numFramesInFlight(2)
|
||||
, m_frameExecutors()
|
||||
, m_currentFrameExecutor()
|
||||
{
|
||||
NFLog("Initializing render engine");
|
||||
m_window->setDisplay(m_display);
|
||||
@ -56,18 +37,10 @@ namespace nf::client::render {
|
||||
createSurface();
|
||||
pickPhysicalDevice();
|
||||
createDevice();
|
||||
createExecutionObjects();
|
||||
|
||||
createSwapchain();
|
||||
|
||||
createOutputRenderPass();
|
||||
createOutputPipeline();
|
||||
|
||||
m_allocator = std::make_unique<VideoMemoryAllocator>(m_device, m_physicalDevice);
|
||||
createSwapchainFramebuffers();
|
||||
createBuffers();
|
||||
createImage();
|
||||
createDescriptorSet();
|
||||
createFrameExecutors();
|
||||
|
||||
m_window->show();
|
||||
}
|
||||
@ -113,7 +86,6 @@ namespace nf::client::render {
|
||||
// Then gather information on them and save first dedicated
|
||||
std::optional<int> firstDedicatedPhysicalDeviceIndex;
|
||||
std::vector<VkPhysicalDeviceProperties> physicalDeviceProperties;
|
||||
physicalDeviceProperties.reserve(numPhysicalDevices);
|
||||
for (int i = 0; i < numPhysicalDevices; i++) {
|
||||
VkPhysicalDeviceProperties currentPhysicalDeviceProperties = {};
|
||||
vkGetPhysicalDeviceProperties(physicalDevices[i], ¤tPhysicalDeviceProperties);
|
||||
@ -182,7 +154,6 @@ namespace nf::client::render {
|
||||
|
||||
float queuePriority = 1.0f;
|
||||
std::vector<VkDeviceQueueCreateInfo> queueCIs;
|
||||
queueCIs.reserve(uniqueQueueFamilyIndices.size());
|
||||
for (uint32_t currentQueueFamilyIndex : uniqueQueueFamilyIndices) {
|
||||
VkDeviceQueueCreateInfo queueCI = { VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO };
|
||||
queueCI.queueFamilyIndex = currentQueueFamilyIndex;
|
||||
@ -234,13 +205,13 @@ namespace nf::client::render {
|
||||
vkGetPhysicalDeviceSurfacePresentModesKHR(m_physicalDevice, m_surface, &numPresentModes, presentModes.data());
|
||||
VkPresentModeKHR chosenPresentMode = VK_PRESENT_MODE_FIFO_KHR;
|
||||
// TODO: IMMEDIATE isn't always available, so do something about that
|
||||
/*for (int i = 0; i < presentModes.size(); i++)
|
||||
for (int i = 0; i < presentModes.size(); i++)
|
||||
if (presentModes[i] == VK_PRESENT_MODE_IMMEDIATE_KHR)
|
||||
chosenPresentMode = presentModes[i];*/
|
||||
chosenPresentMode = presentModes[i];
|
||||
|
||||
m_swapchainExtent = surfaceCapabilities.currentExtent;
|
||||
if (m_swapchainExtent.width == std::numeric_limits<uint32_t>::max())
|
||||
m_swapchainExtent = { m_display.width, m_display.height };
|
||||
VkExtent2D swapchainExtent = surfaceCapabilities.currentExtent;
|
||||
if (swapchainExtent.width == std::numeric_limits<uint32_t>::max())
|
||||
swapchainExtent.width = m_display.width, swapchainExtent.height = m_display.height;
|
||||
|
||||
uint32_t numRequestedImages = surfaceCapabilities.minImageCount + (surfaceCapabilities.minImageCount != surfaceCapabilities.maxImageCount ? 1 : 0);
|
||||
|
||||
@ -249,7 +220,7 @@ namespace nf::client::render {
|
||||
swapchainCI.minImageCount = numRequestedImages;
|
||||
swapchainCI.imageFormat = m_swapchainImageFormat = chosenSurfaceFormat.format;
|
||||
swapchainCI.imageColorSpace = chosenSurfaceFormat.colorSpace;
|
||||
swapchainCI.imageExtent = m_swapchainExtent;
|
||||
swapchainCI.imageExtent = swapchainExtent;
|
||||
swapchainCI.imageArrayLayers = 1;
|
||||
swapchainCI.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
|
||||
|
||||
@ -288,62 +259,35 @@ namespace nf::client::render {
|
||||
}
|
||||
}
|
||||
|
||||
void RenderEngine::createExecutionObjects() {
|
||||
m_commandPool = std::make_unique<CommandPool>(m_device, m_queueGraphics, m_queueFIGraphics);
|
||||
m_commandBuffer = m_commandPool->allocateCommandBuffer();
|
||||
|
||||
VkSemaphoreCreateInfo semaphoreCI = { VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO };
|
||||
if (vkCreateSemaphore(m_device, &semaphoreCI, nullptr, &m_semaphoreImageAvailable) != VK_SUCCESS ||
|
||||
vkCreateSemaphore(m_device, &semaphoreCI, nullptr, &m_semaphoreRenderingDone) != VK_SUCCESS)
|
||||
NFError("Could not create semaphore.");
|
||||
|
||||
VkFenceCreateInfo fenceCI = { VK_STRUCTURE_TYPE_FENCE_CREATE_INFO };
|
||||
fenceCI.flags = VK_FENCE_CREATE_SIGNALED_BIT;
|
||||
if (vkCreateFence(m_device, &fenceCI, nullptr, &m_fenceFrameInFlight) != VK_SUCCESS)
|
||||
NFError("Could not create fence.");
|
||||
}
|
||||
|
||||
void RenderEngine::createOutputRenderPass() {
|
||||
VkAttachmentDescription attachmentDescriptions[2] = {};
|
||||
VkAttachmentDescription outputColorAttachment = {};
|
||||
outputColorAttachment.format = m_swapchainImageFormat;
|
||||
outputColorAttachment.samples = VK_SAMPLE_COUNT_1_BIT;
|
||||
outputColorAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
|
||||
outputColorAttachment.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
|
||||
outputColorAttachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
|
||||
outputColorAttachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
|
||||
outputColorAttachment.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
|
||||
outputColorAttachment.finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
|
||||
|
||||
// Color attachment
|
||||
attachmentDescriptions[0].format = m_swapchainImageFormat;
|
||||
attachmentDescriptions[0].samples = VK_SAMPLE_COUNT_1_BIT;
|
||||
attachmentDescriptions[0].loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
|
||||
attachmentDescriptions[0].storeOp = VK_ATTACHMENT_STORE_OP_STORE;
|
||||
attachmentDescriptions[0].stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
|
||||
attachmentDescriptions[0].stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
|
||||
attachmentDescriptions[0].initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
|
||||
attachmentDescriptions[0].finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
|
||||
|
||||
// Depth attachment
|
||||
attachmentDescriptions[1] = attachmentDescriptions[0];
|
||||
attachmentDescriptions[1].format = VK_FORMAT_D32_SFLOAT;
|
||||
attachmentDescriptions[1].finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
|
||||
|
||||
VkAttachmentReference attachmentReferences[2] = {};
|
||||
attachmentReferences[0].attachment = 0;
|
||||
attachmentReferences[0].layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
|
||||
attachmentReferences[1].attachment = 1;
|
||||
attachmentReferences[1].layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
|
||||
VkAttachmentReference outputColorAttachmentReference = {};
|
||||
outputColorAttachmentReference.attachment = 0;
|
||||
outputColorAttachmentReference.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
|
||||
|
||||
VkSubpassDescription subpassDescription = {};
|
||||
subpassDescription.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
|
||||
subpassDescription.colorAttachmentCount = 1;
|
||||
subpassDescription.pColorAttachments = &attachmentReferences[0];
|
||||
subpassDescription.pDepthStencilAttachment = &attachmentReferences[1];
|
||||
subpassDescription.pColorAttachments = &outputColorAttachmentReference;
|
||||
|
||||
VkSubpassDependency dependency = {};
|
||||
dependency.srcSubpass = VK_SUBPASS_EXTERNAL;
|
||||
dependency.dstSubpass = 0;
|
||||
dependency.srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT | VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT;
|
||||
dependency.srcAccessMask = VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
|
||||
dependency.dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT | VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT;
|
||||
dependency.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
|
||||
dependency.srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
|
||||
dependency.dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
|
||||
dependency.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
|
||||
|
||||
VkRenderPassCreateInfo renderPassCI = { VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO };
|
||||
renderPassCI.attachmentCount = 2;
|
||||
renderPassCI.pAttachments = attachmentDescriptions;
|
||||
renderPassCI.attachmentCount = 1;
|
||||
renderPassCI.pAttachments = &outputColorAttachment;
|
||||
renderPassCI.subpassCount = 1;
|
||||
renderPassCI.pSubpasses = &subpassDescription;
|
||||
renderPassCI.dependencyCount = 1;
|
||||
@ -371,52 +315,35 @@ namespace nf::client::render {
|
||||
outputShaderStages[1].module = outputShaderFragmentModule.getHandle();
|
||||
|
||||
// Specify dynamic state
|
||||
VkDynamicState dynamicState = VK_DYNAMIC_STATE_VIEWPORT;
|
||||
VkPipelineDynamicStateCreateInfo dynamicStateCI = { VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO };
|
||||
std::vector<VkDynamicState> dynamicStates = { VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_SCISSOR };
|
||||
dynamicStateCI.dynamicStateCount = dynamicStates.size();
|
||||
dynamicStateCI.pDynamicStates = dynamicStates.data();
|
||||
dynamicStateCI.dynamicStateCount = 1;
|
||||
dynamicStateCI.pDynamicStates = &dynamicState;
|
||||
|
||||
// Specify vertex input state
|
||||
VkPipelineVertexInputStateCreateInfo vertexInputStateCI = { VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO };
|
||||
VkVertexInputBindingDescription vertexBindingDescription = {};
|
||||
vertexBindingDescription.stride = sizeof(Vertex);
|
||||
VkVertexInputAttributeDescription vertexAttributeDescriptions[2] = {};
|
||||
vertexAttributeDescriptions[0].location = 0;
|
||||
vertexAttributeDescriptions[0].format = VK_FORMAT_R32G32B32_SFLOAT;
|
||||
vertexAttributeDescriptions[0].offset = offsetof(Vertex, position);
|
||||
vertexAttributeDescriptions[1].location = 1;
|
||||
vertexAttributeDescriptions[1].format = VK_FORMAT_R32G32_SFLOAT;
|
||||
vertexAttributeDescriptions[1].offset = offsetof(Vertex, textureCoordinates);
|
||||
vertexInputStateCI.vertexBindingDescriptionCount = 1;
|
||||
vertexInputStateCI.pVertexBindingDescriptions = &vertexBindingDescription;
|
||||
vertexInputStateCI.vertexAttributeDescriptionCount = 2;
|
||||
vertexInputStateCI.pVertexAttributeDescriptions = vertexAttributeDescriptions;
|
||||
|
||||
// Specify input assembly state
|
||||
VkPipelineInputAssemblyStateCreateInfo inputAssemblyStateCI = { VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO };
|
||||
inputAssemblyStateCI.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
|
||||
|
||||
// Specify viewport state
|
||||
VkRect2D scissor = { {0, 0}, {m_display.width, m_display.height} };
|
||||
VkPipelineViewportStateCreateInfo viewportStateCI = { VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO };
|
||||
viewportStateCI.viewportCount = 1;
|
||||
viewportStateCI.scissorCount = 1;
|
||||
viewportStateCI.pScissors = &scissor;
|
||||
|
||||
// Specify rasterization state
|
||||
VkPipelineRasterizationStateCreateInfo rasterizationCI = { VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO };
|
||||
rasterizationCI.lineWidth = 1.0;
|
||||
rasterizationCI.cullMode = VK_CULL_MODE_BACK_BIT;
|
||||
rasterizationCI.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
|
||||
rasterizationCI.frontFace = VK_FRONT_FACE_CLOCKWISE;
|
||||
|
||||
// Specify multisample state
|
||||
VkPipelineMultisampleStateCreateInfo multisampleStateCI = { VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO };
|
||||
multisampleStateCI.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
|
||||
|
||||
// Specify depth stencil state
|
||||
VkPipelineDepthStencilStateCreateInfo depthStencilStateCI = { VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO };
|
||||
depthStencilStateCI.depthTestEnable = VK_TRUE;
|
||||
depthStencilStateCI.depthWriteEnable = VK_TRUE;
|
||||
depthStencilStateCI.depthCompareOp = VK_COMPARE_OP_LESS;
|
||||
|
||||
// Specify color blend state
|
||||
VkPipelineColorBlendAttachmentState colorBlendAttachmentState = {};
|
||||
colorBlendAttachmentState.colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT;
|
||||
@ -424,46 +351,12 @@ namespace nf::client::render {
|
||||
colorBlendStateCI.attachmentCount = 1;
|
||||
colorBlendStateCI.pAttachments = &colorBlendAttachmentState;
|
||||
|
||||
// Create descriptor set layout
|
||||
VkDescriptorSetLayoutCreateInfo descriptorSetLayoutCI = { VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO };
|
||||
VkDescriptorSetLayoutBinding layoutBindings[] = { {}, {} };
|
||||
layoutBindings[0].binding = 0;
|
||||
layoutBindings[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
|
||||
layoutBindings[0].descriptorCount = 1;
|
||||
layoutBindings[0].stageFlags = VK_SHADER_STAGE_VERTEX_BIT;
|
||||
layoutBindings[1].binding = 1;
|
||||
layoutBindings[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
|
||||
layoutBindings[1].descriptorCount = 1;
|
||||
layoutBindings[1].stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT;
|
||||
descriptorSetLayoutCI.bindingCount = 2;
|
||||
descriptorSetLayoutCI.bindingCount = 2;
|
||||
descriptorSetLayoutCI.pBindings = layoutBindings;
|
||||
|
||||
if (vkCreateDescriptorSetLayout(m_device, &descriptorSetLayoutCI, nullptr, &m_pipelineOutputDescriptorSetLayout) != VK_SUCCESS)
|
||||
NFError("Could not create descriptor set layout.");
|
||||
|
||||
// Create pipeline layout
|
||||
VkPipelineLayoutCreateInfo layoutCI = { VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO };
|
||||
layoutCI.setLayoutCount = 1;
|
||||
layoutCI.pSetLayouts = &m_pipelineOutputDescriptorSetLayout;
|
||||
|
||||
if (vkCreatePipelineLayout(m_device, &layoutCI, nullptr, &m_pipelineOutputLayout) != VK_SUCCESS)
|
||||
if (vkCreatePipelineLayout(m_device, &layoutCI, nullptr, &m_pipelineLayoutOutput) != VK_SUCCESS)
|
||||
NFError("Could not create pipeline layout.");
|
||||
|
||||
// Create descriptor pool
|
||||
VkDescriptorPoolCreateInfo descriptorPoolCI = { VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO };
|
||||
VkDescriptorPoolSize descriptorPoolSizes[] = { {}, {} };
|
||||
descriptorPoolSizes[0].type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
|
||||
descriptorPoolSizes[0].descriptorCount = 1;
|
||||
descriptorPoolSizes[1].type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
|
||||
descriptorPoolSizes[1].descriptorCount = 1;
|
||||
descriptorPoolCI.poolSizeCount = 2;
|
||||
descriptorPoolCI.pPoolSizes = descriptorPoolSizes;
|
||||
descriptorPoolCI.maxSets = 1;
|
||||
|
||||
if (vkCreateDescriptorPool(m_device, &descriptorPoolCI, nullptr, &m_pipelineOutputDescriptorPool) != VK_SUCCESS)
|
||||
NFError("Could not create descriptor pool.");
|
||||
|
||||
// And finally put it all together
|
||||
VkGraphicsPipelineCreateInfo pipelineCI = { VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO };
|
||||
pipelineCI.stageCount = 2;
|
||||
@ -473,30 +366,22 @@ namespace nf::client::render {
|
||||
pipelineCI.pViewportState = &viewportStateCI;
|
||||
pipelineCI.pRasterizationState = &rasterizationCI;
|
||||
pipelineCI.pMultisampleState = &multisampleStateCI;
|
||||
pipelineCI.pDepthStencilState = &depthStencilStateCI;
|
||||
pipelineCI.pColorBlendState = &colorBlendStateCI;
|
||||
pipelineCI.pDynamicState = &dynamicStateCI;
|
||||
pipelineCI.layout = m_pipelineOutputLayout;
|
||||
pipelineCI.layout = m_pipelineLayoutOutput;
|
||||
pipelineCI.renderPass = m_renderPassOutput;
|
||||
|
||||
if (vkCreateGraphicsPipelines(m_device, nullptr, 1, &pipelineCI, nullptr, &m_pipelineOutput) != VK_SUCCESS)
|
||||
NFError("Could not create graphics pipeline.");
|
||||
}
|
||||
|
||||
void RenderEngine::createSwapchainFramebuffers() {
|
||||
// TODO: Won't need these forever
|
||||
|
||||
// Create depth buffer first
|
||||
m_imageDepth = std::make_unique<Image>(ImageType::DepthAttachment, m_device, *m_allocator, *m_commandPool, std::string(), m_swapchainExtent);
|
||||
|
||||
// TODO: For now, create swapchain framebuffers
|
||||
m_swapchainFramebuffers.resize(m_swapchainImageViews.size());
|
||||
for (int i = 0; i < m_swapchainImageViews.size(); i++) {
|
||||
VkFramebufferCreateInfo framebufferCI = { VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO };
|
||||
framebufferCI.renderPass = m_renderPassOutput;
|
||||
VkImageView views[] = { m_swapchainImageViews[i], m_imageDepth->getView() };
|
||||
framebufferCI.attachmentCount = 2;
|
||||
framebufferCI.pAttachments = views;
|
||||
framebufferCI.width = m_swapchainExtent.width, framebufferCI.height = m_swapchainExtent.height;
|
||||
framebufferCI.attachmentCount = 1;
|
||||
framebufferCI.pAttachments = &m_swapchainImageViews[i];
|
||||
framebufferCI.width = m_display.width, framebufferCI.height = m_display.height;
|
||||
framebufferCI.layers = 1;
|
||||
|
||||
if (vkCreateFramebuffer(m_device, &framebufferCI, nullptr, &m_swapchainFramebuffers[i]) != VK_SUCCESS)
|
||||
@ -504,278 +389,132 @@ namespace nf::client::render {
|
||||
}
|
||||
}
|
||||
|
||||
void RenderEngine::createBuffers() {
|
||||
std::vector<Vertex> cubeVertices = {
|
||||
{{-0.5, -0.5, 0.5}, {0.0, 1.0}},
|
||||
{{ 0.5, -0.5, 0.5}, {1.0, 1.0}},
|
||||
{{ 0.5, 0.5, 0.5}, {1.0, 0.0}},
|
||||
{{-0.5, 0.5, 0.5}, {0.0, 0.0}},
|
||||
void RenderEngine::createFrameExecutors() {
|
||||
VkCommandPoolCreateInfo commandPoolCI = { VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO };
|
||||
commandPoolCI.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
|
||||
commandPoolCI.queueFamilyIndex = m_queueFIGraphics;
|
||||
|
||||
{{ 0.5, 0.5, 0.5}, {0.0, 0.0}},
|
||||
{{ 0.5, -0.5, -0.5}, {1.0, 1.0}},
|
||||
{{ 0.5, 0.5, -0.5}, {1.0, 0.0}},
|
||||
{{ 0.5, -0.5, 0.5}, {0.0, 1.0}},
|
||||
if (vkCreateCommandPool(m_device, &commandPoolCI, nullptr, &m_commandPool) != VK_SUCCESS)
|
||||
NFError("Could not create command pool.");
|
||||
|
||||
{{-0.5, 0.5, -0.5}, {0.0, 0.0}},
|
||||
{{-0.5, 0.5, 0.5}, {0.0, 1.0}},
|
||||
{{ 0.5, 0.5, -0.5}, {1.0, 0.0}},
|
||||
{{ 0.5, 0.5, 0.5}, {1.0, 1.0}},
|
||||
m_frameExecutors.resize(m_numFramesInFlight);
|
||||
for (int i = 0; i < m_frameExecutors.size(); i++) {
|
||||
VkCommandBufferAllocateInfo commandBufferAI = { VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO };
|
||||
commandBufferAI.commandPool = m_commandPool;
|
||||
commandBufferAI.commandBufferCount = 1;
|
||||
|
||||
{{-0.5, -0.5, -0.5}, {1.0, 0.0}},
|
||||
{{-0.5, -0.5, 0.5}, {1.0, 1.0}},
|
||||
{{ 0.5, -0.5, -0.5}, {0.0, 0.0}},
|
||||
{{ 0.5, -0.5, 0.5}, {0.0, 1.0}},
|
||||
if (vkAllocateCommandBuffers(m_device, &commandBufferAI, &m_frameExecutors[i].commandBuffer) != VK_SUCCESS)
|
||||
NFError("Could not create command buffer.");
|
||||
|
||||
{{-0.5, 0.5, -0.5}, {0.0, 0.0}},
|
||||
{{-0.5, 0.5, 0.5}, {1.0, 0.0}},
|
||||
{{-0.5, -0.5, 0.5}, {1.0, 1.0}},
|
||||
{{-0.5, -0.5, -0.5}, {0.0, 1.0}},
|
||||
VkSemaphoreCreateInfo semaphoreCI = { VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO };
|
||||
if (vkCreateSemaphore(m_device, &semaphoreCI, nullptr, &m_frameExecutors[i].semaphoreImageAvailable) != VK_SUCCESS ||
|
||||
vkCreateSemaphore(m_device, &semaphoreCI, nullptr, &m_frameExecutors[i].semaphoreRenderFinished) != VK_SUCCESS)
|
||||
NFError("Could not create semaphore.");
|
||||
|
||||
{{ 0.5, 0.5, -0.5}, {1.0, 0.0}},
|
||||
{{-0.5, 0.5, -0.5}, {0.0, 0.0}},
|
||||
{{-0.5, -0.5, -0.5}, {0.0, 1.0}},
|
||||
{{ 0.5, -0.5, -0.5}, {1.0, 1.0}},
|
||||
|
||||
{{ 0.5, -0.6, 0.5}, {1.0, 1.0}},
|
||||
{{ 0.5, -0.6, -0.5}, {1.0, 0.0}},
|
||||
{{-0.5, -0.6, 0.5}, {0.0, 1.0}}
|
||||
};
|
||||
|
||||
size_t cubeVerticesSize = sizeof(cubeVertices[0]) * cubeVertices.size();
|
||||
m_bufferVertex = std::make_unique<Buffer>(BufferType::Vertex, m_device, *m_allocator, *m_commandPool, cubeVertices.data(), cubeVerticesSize);
|
||||
|
||||
std::vector<uint32_t> cubeIndices = {
|
||||
0, 1, 2,
|
||||
2, 3, 0,
|
||||
|
||||
4, 5, 6,
|
||||
4, 7, 5,
|
||||
|
||||
8, 9, 10,
|
||||
10, 9, 11,
|
||||
|
||||
12, 14, 13,
|
||||
14, 15, 13,
|
||||
|
||||
16, 19, 17,
|
||||
17, 19, 18,
|
||||
|
||||
20, 23, 21,
|
||||
23, 22, 21,
|
||||
|
||||
24, 25, 26
|
||||
};
|
||||
|
||||
size_t cubeIndicesSize = sizeof(cubeIndices[0]) * cubeIndices.size();
|
||||
m_bufferIndex = std::make_unique<Buffer>(BufferType::Index, m_device, *m_allocator, *m_commandPool, cubeIndices.data(), cubeIndicesSize);
|
||||
|
||||
size_t mvpUniformBufferSize = sizeof(glm::mat4);
|
||||
m_bufferUniformMVP = std::make_unique<Buffer>(BufferType::Uniform, m_device, *m_allocator, *m_commandPool, nullptr, mvpUniformBufferSize);
|
||||
}
|
||||
|
||||
void RenderEngine::createImage() {
|
||||
std::string imageData;
|
||||
util::readFile("grayson.jpg", imageData);
|
||||
m_imageTest = std::make_unique<Image>(ImageType::Texture, m_device, *m_allocator, *m_commandPool, imageData);
|
||||
|
||||
VkSamplerCreateInfo samplerCI = { VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO };
|
||||
samplerCI.magFilter = samplerCI.minFilter = VK_FILTER_LINEAR;
|
||||
/*samplerCI.anisotropyEnable = VK_TRUE;
|
||||
samplerCI.maxAnisotropy = 16;*/
|
||||
|
||||
if (vkCreateSampler(m_device, &samplerCI, nullptr, &m_sampler) != VK_SUCCESS)
|
||||
NFError("Could not create sampler.");
|
||||
}
|
||||
|
||||
void RenderEngine::createDescriptorSet() {
|
||||
VkDescriptorSetAllocateInfo descriptorSetAI = { VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO };
|
||||
descriptorSetAI.descriptorPool = m_pipelineOutputDescriptorPool;
|
||||
descriptorSetAI.descriptorSetCount = 1;
|
||||
descriptorSetAI.pSetLayouts = &m_pipelineOutputDescriptorSetLayout;
|
||||
|
||||
if (vkAllocateDescriptorSets(m_device, &descriptorSetAI, &m_pipelineOutputDescriptorSet) != VK_SUCCESS)
|
||||
NFError("Could not allocate descriptor set.");
|
||||
|
||||
VkWriteDescriptorSet descriptorSetWrites[] = { {VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET}, {VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET} };
|
||||
VkDescriptorBufferInfo uniformDescriptorBI = {};
|
||||
uniformDescriptorBI.buffer = m_bufferUniformMVP->getHandle();
|
||||
uniformDescriptorBI.range = VK_WHOLE_SIZE;
|
||||
VkDescriptorImageInfo imageDescriptorII = {};
|
||||
imageDescriptorII.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
|
||||
imageDescriptorII.imageView = m_imageTest->getView();
|
||||
imageDescriptorII.sampler = m_sampler;
|
||||
descriptorSetWrites[0].dstSet = m_pipelineOutputDescriptorSet;
|
||||
descriptorSetWrites[0].dstBinding = 0;
|
||||
descriptorSetWrites[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
|
||||
descriptorSetWrites[0].descriptorCount = 1;
|
||||
descriptorSetWrites[0].pBufferInfo = &uniformDescriptorBI;
|
||||
descriptorSetWrites[1].dstSet = m_pipelineOutputDescriptorSet;
|
||||
descriptorSetWrites[1].dstBinding = 1;
|
||||
descriptorSetWrites[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
|
||||
descriptorSetWrites[1].descriptorCount = 1;
|
||||
descriptorSetWrites[1].pImageInfo = &imageDescriptorII;
|
||||
vkUpdateDescriptorSets(m_device, 2, descriptorSetWrites, 0, nullptr);
|
||||
VkFenceCreateInfo fenceCI = { VK_STRUCTURE_TYPE_FENCE_CREATE_INFO };
|
||||
fenceCI.flags = VK_FENCE_CREATE_SIGNALED_BIT;
|
||||
if (vkCreateFence(m_device, &fenceCI, nullptr, &m_frameExecutors[i].fenceFrameInFlight) != VK_SUCCESS)
|
||||
NFError("Could not create fence.");
|
||||
}
|
||||
}
|
||||
|
||||
void RenderEngine::doFrame() {
|
||||
// Get current frame executor
|
||||
auto& [commandBuffer, semaphoreImageAvailable, semaphoreRenderFinished, fenceFrameInFlight] = m_frameExecutors[m_currentFrameExecutor];
|
||||
|
||||
// First, wait for previous frame to complete
|
||||
vkWaitForFences(m_device, 1, &m_fenceFrameInFlight, VK_TRUE, UINT64_MAX);
|
||||
vkWaitForFences(m_device, 1, &fenceFrameInFlight, VK_TRUE, UINT64_MAX);
|
||||
vkResetFences(m_device, 1, &fenceFrameInFlight);
|
||||
|
||||
// Get next swapchain image and recreate if necessary
|
||||
// Get next swapchain image
|
||||
uint32_t nextImageIndex = 0;
|
||||
VkResult result = vkAcquireNextImageKHR(m_device, m_swapchain, UINT64_MAX, m_semaphoreImageAvailable, nullptr, &nextImageIndex);
|
||||
if (result == VK_ERROR_OUT_OF_DATE_KHR) {
|
||||
recreateSwapchain();
|
||||
return;
|
||||
}
|
||||
else if (result != VK_SUCCESS && result != VK_SUBOPTIMAL_KHR)
|
||||
NFError("Could not aquire next swapchain image.");
|
||||
|
||||
// Reset fence for this frame
|
||||
vkResetFences(m_device, 1, &m_fenceFrameInFlight);
|
||||
vkAcquireNextImageKHR(m_device, m_swapchain, UINT64_MAX, semaphoreImageAvailable, nullptr, &nextImageIndex);
|
||||
|
||||
// Begin recording
|
||||
VkCommandBufferBeginInfo commandBufferBI = { VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO };
|
||||
|
||||
vkBeginCommandBuffer(m_commandBuffer, &commandBufferBI);
|
||||
if (vkBeginCommandBuffer(commandBuffer, &commandBufferBI) != VK_SUCCESS)
|
||||
NFError("Could not begin recording command buffer.");
|
||||
|
||||
// Start the render pass
|
||||
VkClearValue clearValues[] = { {{0.0, 0.0, 0.0, 1.0}}, {1.0, 0} };
|
||||
VkClearValue black = { {{0.0, 0.0, 0.0, 1.0}} };
|
||||
VkRenderPassBeginInfo renderPassBI = { VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO };
|
||||
renderPassBI.renderPass = m_renderPassOutput;
|
||||
renderPassBI.framebuffer = m_swapchainFramebuffers[nextImageIndex];
|
||||
renderPassBI.renderArea.extent = m_swapchainExtent;
|
||||
renderPassBI.clearValueCount = 2;
|
||||
renderPassBI.pClearValues = clearValues;
|
||||
vkCmdBeginRenderPass(m_commandBuffer, &renderPassBI, VK_SUBPASS_CONTENTS_INLINE);
|
||||
renderPassBI.renderArea.extent = { m_display.width, m_display.height };
|
||||
renderPassBI.clearValueCount = 1;
|
||||
renderPassBI.pClearValues = &black;
|
||||
vkCmdBeginRenderPass(commandBuffer, &renderPassBI, VK_SUBPASS_CONTENTS_INLINE);
|
||||
|
||||
// Bind output pipeline
|
||||
vkCmdBindPipeline(m_commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, m_pipelineOutput);
|
||||
vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, m_pipelineOutput);
|
||||
|
||||
// Set viewport and scissor
|
||||
// Set viewport
|
||||
VkViewport viewport = {};
|
||||
viewport.width = static_cast<float>(m_swapchainExtent.width), viewport.height = static_cast<float>(m_swapchainExtent.height);
|
||||
viewport.width = static_cast<float>(m_display.width), viewport.height = static_cast<float>(m_display.height);
|
||||
viewport.maxDepth = 1.0;
|
||||
vkCmdSetViewport(m_commandBuffer, 0, 1, &viewport);
|
||||
VkRect2D scissor = { {}, m_swapchainExtent };
|
||||
vkCmdSetScissor(m_commandBuffer, 0, 1, &scissor);
|
||||
|
||||
// Bind buffers
|
||||
VkDeviceSize bufferOffset = 0;
|
||||
vkCmdBindVertexBuffers(m_commandBuffer, 0, 1, &m_bufferVertex->getHandle(), &bufferOffset);
|
||||
vkCmdBindIndexBuffer(m_commandBuffer, m_bufferIndex->getHandle(), 0, VK_INDEX_TYPE_UINT32);
|
||||
|
||||
// Update uniform buffer
|
||||
static auto startTime = std::chrono::high_resolution_clock::now();
|
||||
auto currentTime = std::chrono::high_resolution_clock::now();
|
||||
float time = std::chrono::duration<float>(currentTime - startTime).count();
|
||||
glm::mat4 modelMatrix = glm::rotate(glm::mat4(1.0), time * glm::radians(20.0f), glm::vec3(0.0, 1.0, 0.0)),
|
||||
viewMatrix = glm::lookAt(glm::vec3(1.0, 1.0, 2.0), glm::vec3(0.0), glm::vec3(0.0, 1.0, 0.0)),
|
||||
projectionMatrix = glm::perspective(glm::radians(45.0f), static_cast<float>(m_swapchainExtent.width) / m_swapchainExtent.height, 0.1f, 10.0f);
|
||||
|
||||
projectionMatrix[1][1] *= -1;
|
||||
glm::mat4 mvpMatrix = projectionMatrix * viewMatrix * modelMatrix;
|
||||
memcpy(m_bufferUniformMVP->getPointer(), &mvpMatrix, sizeof(mvpMatrix));
|
||||
|
||||
// Bind descriptors
|
||||
vkCmdBindDescriptorSets(m_commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, m_pipelineOutputLayout, 0, 1, &m_pipelineOutputDescriptorSet, 0, nullptr);
|
||||
vkCmdSetViewport(commandBuffer, 0, 1, &viewport);
|
||||
|
||||
// Draw
|
||||
vkCmdDrawIndexed(m_commandBuffer, m_bufferIndex->getIndicesCount(), 1, 0, 0, 0);
|
||||
vkCmdDraw(commandBuffer, 3, 1, 0, 0);
|
||||
|
||||
// End the render pass
|
||||
vkCmdEndRenderPass(m_commandBuffer);
|
||||
vkCmdEndRenderPass(commandBuffer);
|
||||
|
||||
// Finish recording
|
||||
vkEndCommandBuffer(m_commandBuffer);
|
||||
if (vkEndCommandBuffer(commandBuffer) != VK_SUCCESS)
|
||||
NFError("Could not end command buffer.");
|
||||
|
||||
// Submit to graphics queue
|
||||
VkPipelineStageFlags waitStage = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
|
||||
VkSubmitInfo submitInfo = { VK_STRUCTURE_TYPE_SUBMIT_INFO };
|
||||
submitInfo.waitSemaphoreCount = 1;
|
||||
submitInfo.pWaitSemaphores = &m_semaphoreImageAvailable;
|
||||
submitInfo.pWaitSemaphores = &semaphoreImageAvailable;
|
||||
submitInfo.pWaitDstStageMask = &waitStage;
|
||||
submitInfo.commandBufferCount = 1;
|
||||
submitInfo.pCommandBuffers = &m_commandBuffer;
|
||||
submitInfo.pCommandBuffers = &commandBuffer;
|
||||
submitInfo.signalSemaphoreCount = 1;
|
||||
submitInfo.pSignalSemaphores = &m_semaphoreRenderingDone;
|
||||
submitInfo.pSignalSemaphores = &semaphoreRenderFinished;
|
||||
|
||||
if (vkQueueSubmit(m_queueGraphics, 1, &submitInfo, m_fenceFrameInFlight) != VK_SUCCESS)
|
||||
if (vkQueueSubmit(m_queueGraphics, 1, &submitInfo, fenceFrameInFlight) != VK_SUCCESS)
|
||||
NFError("Could not submit to Vulkan queue.");
|
||||
|
||||
// And present!
|
||||
VkPresentInfoKHR presentInfo = { VK_STRUCTURE_TYPE_PRESENT_INFO_KHR };
|
||||
presentInfo.waitSemaphoreCount = 1;
|
||||
presentInfo.pWaitSemaphores = &m_semaphoreRenderingDone;
|
||||
presentInfo.pWaitSemaphores = &semaphoreRenderFinished;
|
||||
presentInfo.swapchainCount = 1;
|
||||
presentInfo.pSwapchains = &m_swapchain;
|
||||
presentInfo.pImageIndices = &nextImageIndex;
|
||||
|
||||
result = vkQueuePresentKHR(m_queuePresent, &presentInfo);
|
||||
if (result == VK_ERROR_OUT_OF_DATE_KHR || result == VK_SUBOPTIMAL_KHR)
|
||||
recreateSwapchain();
|
||||
else if (result != VK_SUCCESS)
|
||||
NFError("Could not present image.");
|
||||
vkQueuePresentKHR(m_queuePresent, &presentInfo);
|
||||
|
||||
// Advance to next frame executor
|
||||
m_currentFrameExecutor = (m_currentFrameExecutor + 1) % m_numFramesInFlight;
|
||||
}
|
||||
|
||||
void RenderEngine::setDisplay(DisplayConfig config) {
|
||||
m_display = config;
|
||||
m_window->setDisplay(m_display);
|
||||
}
|
||||
RenderEngine::~RenderEngine() {
|
||||
vkDeviceWaitIdle(m_device);
|
||||
|
||||
void RenderEngine::recreateSwapchain() {
|
||||
// Might be here due to window closing
|
||||
if (!m_window->isActive())
|
||||
return;
|
||||
for (int i = 0; i < m_frameExecutors.size(); i++) {
|
||||
vkDestroyFence(m_device, m_frameExecutors[i].fenceFrameInFlight, nullptr);
|
||||
vkDestroySemaphore(m_device, m_frameExecutors[i].semaphoreRenderFinished, nullptr);
|
||||
vkDestroySemaphore(m_device, m_frameExecutors[i].semaphoreImageAvailable, nullptr);
|
||||
}
|
||||
|
||||
waitIdle();
|
||||
vkDestroyCommandPool(m_device, m_commandPool, nullptr);
|
||||
|
||||
destroySwapchain();
|
||||
|
||||
createSwapchain();
|
||||
createSwapchainFramebuffers();
|
||||
}
|
||||
|
||||
void RenderEngine::destroySwapchain() {
|
||||
for (int i = 0; i < m_swapchainFramebuffers.size(); i++)
|
||||
vkDestroyFramebuffer(m_device, m_swapchainFramebuffers[i], nullptr);
|
||||
|
||||
m_imageDepth.reset();
|
||||
vkDestroyPipeline(m_device, m_pipelineOutput, nullptr);
|
||||
vkDestroyPipelineLayout(m_device, m_pipelineLayoutOutput, nullptr);
|
||||
vkDestroyRenderPass(m_device, m_renderPassOutput, nullptr);
|
||||
|
||||
for (int i = 0; i < m_swapchainImageViews.size(); i++)
|
||||
vkDestroyImageView(m_device, m_swapchainImageViews[i], nullptr);
|
||||
|
||||
vkDestroySwapchainKHR(m_device, m_swapchain, nullptr);
|
||||
}
|
||||
|
||||
void RenderEngine::waitIdle() {
|
||||
vkDeviceWaitIdle(m_device);
|
||||
}
|
||||
|
||||
RenderEngine::~RenderEngine() {
|
||||
waitIdle();
|
||||
|
||||
vkDestroySampler(m_device, m_sampler, nullptr);
|
||||
|
||||
m_imageTest.reset();
|
||||
|
||||
m_bufferUniformMVP.reset();
|
||||
m_bufferIndex.reset();
|
||||
m_bufferVertex.reset();
|
||||
|
||||
vkDestroyPipeline(m_device, m_pipelineOutput, nullptr);
|
||||
vkDestroyDescriptorPool(m_device, m_pipelineOutputDescriptorPool, nullptr);
|
||||
vkDestroyPipelineLayout(m_device, m_pipelineOutputLayout, nullptr);
|
||||
vkDestroyDescriptorSetLayout(m_device, m_pipelineOutputDescriptorSetLayout, nullptr);
|
||||
vkDestroyRenderPass(m_device, m_renderPassOutput, nullptr);
|
||||
destroySwapchain();
|
||||
|
||||
vkDestroyFence(m_device, m_fenceFrameInFlight, nullptr);
|
||||
vkDestroySemaphore(m_device, m_semaphoreRenderingDone, nullptr);
|
||||
vkDestroySemaphore(m_device, m_semaphoreImageAvailable, nullptr);
|
||||
|
||||
m_commandPool.reset();
|
||||
|
||||
vkDestroyDevice(m_device, nullptr);
|
||||
vkDestroySurfaceKHR(m_instance, m_surface, nullptr);
|
||||
vkDestroyInstance(m_instance, nullptr);
|
||||
|
@ -3,18 +3,19 @@
|
||||
|
||||
#include "client/Window.h"
|
||||
#include "nf/config.h"
|
||||
#include "CommandPool.h"
|
||||
#include "VideoMemoryAllocator.h"
|
||||
#include "Buffer.h"
|
||||
#include "Image.h"
|
||||
|
||||
namespace nf::client::render {
|
||||
struct FrameExecutor {
|
||||
VkCommandBuffer commandBuffer;
|
||||
VkSemaphore semaphoreImageAvailable, semaphoreRenderFinished;
|
||||
VkFence fenceFrameInFlight;
|
||||
};
|
||||
|
||||
class RenderEngine {
|
||||
public:
|
||||
RenderEngine(std::shared_ptr<Window> window, DisplayConfig display);
|
||||
|
||||
void doFrame();
|
||||
void setDisplay(DisplayConfig config);
|
||||
|
||||
~RenderEngine();
|
||||
private:
|
||||
@ -22,22 +23,10 @@ namespace nf::client::render {
|
||||
void createSurface();
|
||||
void pickPhysicalDevice();
|
||||
void createDevice();
|
||||
void createExecutionObjects();
|
||||
|
||||
void createSwapchain();
|
||||
|
||||
void createOutputRenderPass();
|
||||
void createOutputPipeline();
|
||||
|
||||
void createSwapchainFramebuffers();
|
||||
void createBuffers();
|
||||
void createImage();
|
||||
void createDescriptorSet();
|
||||
|
||||
void recreateSwapchain();
|
||||
void destroySwapchain();
|
||||
|
||||
void waitIdle();
|
||||
void createFrameExecutors();
|
||||
|
||||
std::shared_ptr<Window> m_window;
|
||||
DisplayConfig m_display;
|
||||
@ -49,37 +38,23 @@ namespace nf::client::render {
|
||||
uint32_t m_queueFIGraphics, m_queueFIPresent;
|
||||
VkDevice m_device;
|
||||
VkQueue m_queueGraphics, m_queuePresent;
|
||||
|
||||
// Execution objects
|
||||
std::unique_ptr<CommandPool> m_commandPool;
|
||||
VkCommandBuffer m_commandBuffer;
|
||||
VkSemaphore m_semaphoreImageAvailable;
|
||||
VkSemaphore m_semaphoreRenderingDone;
|
||||
VkFence m_fenceFrameInFlight;
|
||||
VkRenderPass m_renderPassOutput;
|
||||
|
||||
// Swapchain
|
||||
VkSwapchainKHR m_swapchain;
|
||||
VkFormat m_swapchainImageFormat;
|
||||
VkExtent2D m_swapchainExtent;
|
||||
std::vector<VkImage> m_swapchainImages;
|
||||
std::vector<VkImageView> m_swapchainImageViews;
|
||||
std::vector<VkFramebuffer> m_swapchainFramebuffers;
|
||||
|
||||
// RenderPass and Pipeline
|
||||
VkRenderPass m_renderPassOutput;
|
||||
VkDescriptorSetLayout m_pipelineOutputDescriptorSetLayout;
|
||||
VkPipelineLayout m_pipelineOutputLayout;
|
||||
VkDescriptorPool m_pipelineOutputDescriptorPool;
|
||||
// Pipelines
|
||||
VkPipelineLayout m_pipelineLayoutOutput;
|
||||
VkPipeline m_pipelineOutput;
|
||||
|
||||
// Temporary objects
|
||||
std::unique_ptr<VideoMemoryAllocator> m_allocator;
|
||||
std::unique_ptr<Image> m_imageDepth;
|
||||
std::vector<VkFramebuffer> m_swapchainFramebuffers;
|
||||
std::unique_ptr<Buffer> m_bufferVertex;
|
||||
std::unique_ptr<Buffer> m_bufferIndex;
|
||||
std::unique_ptr<Buffer> m_bufferUniformMVP;
|
||||
std::unique_ptr<Image> m_imageTest;
|
||||
VkSampler m_sampler;
|
||||
VkDescriptorSet m_pipelineOutputDescriptorSet;
|
||||
// Execution objects
|
||||
VkCommandPool m_commandPool;
|
||||
const uint32_t m_numFramesInFlight;
|
||||
std::vector<FrameExecutor> m_frameExecutors;
|
||||
uint32_t m_currentFrameExecutor;
|
||||
};
|
||||
}
|
||||
|
@ -6,7 +6,7 @@
|
||||
|
||||
namespace nf::client::render {
|
||||
ShaderModule::ShaderModule(const VkDevice& device, const std::string& shaderBinary)
|
||||
: GraphicsResource(device)
|
||||
: m_device(device)
|
||||
, m_shaderModule()
|
||||
{
|
||||
VkShaderModuleCreateInfo shaderModuleCI = { VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO };
|
||||
|
@ -1,10 +1,8 @@
|
||||
// ShaderModule class header
|
||||
#pragma once
|
||||
|
||||
#include "GraphicsResource.h"
|
||||
|
||||
namespace nf::client::render {
|
||||
class ShaderModule : GraphicsResource {
|
||||
class ShaderModule {
|
||||
public:
|
||||
ShaderModule(const VkDevice& device, const std::string& shaderBinary);
|
||||
|
||||
@ -12,6 +10,8 @@ namespace nf::client::render {
|
||||
|
||||
~ShaderModule();
|
||||
private:
|
||||
const VkDevice& m_device;
|
||||
|
||||
VkShaderModule m_shaderModule;
|
||||
};
|
||||
}
|
||||
|
@ -1,145 +0,0 @@
|
||||
// VideoMemoryAllocator class implementation
|
||||
#include "pch.h"
|
||||
|
||||
#include "VideoMemoryAllocator.h"
|
||||
#include "util.h"
|
||||
|
||||
// 1 GiB
|
||||
#define HEAP_SIZE_LARGE 1024ull * 1024 * 1024
|
||||
// 50 MiB
|
||||
#define DEFAULT_ALLOCATION_AMOUNT 50ull * 1024 * 1024
|
||||
|
||||
namespace nf::client::render {
|
||||
VideoMemoryAllocator::VideoMemoryAllocator(const VkDevice& device, const VkPhysicalDevice& physicalDevice)
|
||||
: m_device(device)
|
||||
, m_physicalDevice(physicalDevice)
|
||||
, m_blocks()
|
||||
{}
|
||||
|
||||
void VideoMemoryAllocator::allocateForBuffer(VkBuffer buffer, VkMemoryPropertyFlags memoryPropertyFlags, VideoMemoryAllocation& outAllocation) {
|
||||
VkMemoryRequirements memoryRequirements = {};
|
||||
vkGetBufferMemoryRequirements(m_device, buffer, &memoryRequirements);
|
||||
|
||||
allocate(memoryRequirements, memoryPropertyFlags, outAllocation);
|
||||
vkBindBufferMemory(m_device, buffer, outAllocation.memory, outAllocation.offset);
|
||||
}
|
||||
|
||||
void VideoMemoryAllocator::allocateForImage(VkImage image, VkMemoryPropertyFlags memoryPropertyFlags, VideoMemoryAllocation& outAllocation) {
|
||||
VkMemoryRequirements memoryRequirements = {};
|
||||
vkGetImageMemoryRequirements(m_device, image, &memoryRequirements);
|
||||
|
||||
allocate(memoryRequirements, memoryPropertyFlags, outAllocation);
|
||||
vkBindImageMemory(m_device, image, outAllocation.memory, outAllocation.offset);
|
||||
}
|
||||
|
||||
void VideoMemoryAllocator::deallocate(const VideoMemoryAllocation& allocation) {
|
||||
const auto blockIterator = std::find_if(m_blocks.begin(), m_blocks.end(), [&](const auto& currentBlock) {
|
||||
if (currentBlock.memory == allocation.memory)
|
||||
return true;
|
||||
else return false;
|
||||
});
|
||||
|
||||
if (blockIterator == m_blocks.end())
|
||||
NFError("Could not find video memory block to deallocate from.");
|
||||
|
||||
MemoryBlock& block = *blockIterator;
|
||||
|
||||
const auto allocationIterator = block.allocations.find(allocation.offset);
|
||||
|
||||
if (allocationIterator == block.allocations.end())
|
||||
NFError("Could not find video memory allocation to deallocate.");
|
||||
|
||||
block.allocations.erase(allocationIterator);
|
||||
|
||||
// If block is now empty, free it
|
||||
if (block.allocations.empty()) {
|
||||
vkFreeMemory(m_device, block.memory, nullptr);
|
||||
m_blocks.erase(blockIterator);
|
||||
}
|
||||
}
|
||||
|
||||
void VideoMemoryAllocator::allocate(VkMemoryRequirements memoryRequirements, VkMemoryPropertyFlags memoryPropertyFlags, VideoMemoryAllocation& outAllocation) {
|
||||
uint32_t memoryTypeIndex = findMemoryType(memoryRequirements.memoryTypeBits, memoryPropertyFlags);
|
||||
|
||||
if (memoryTypeIndex == -1)
|
||||
NFError("Could not find suitable memory type.");
|
||||
|
||||
while (true) {
|
||||
for (auto& block : m_blocks) {
|
||||
if (block.memoryTypeIndex == memoryTypeIndex) {
|
||||
VkDeviceSize lastOffset = 0, lastSize = 0;
|
||||
// Check between existing allocations
|
||||
for (const auto& [currentOffset, currentSize] : block.allocations) {
|
||||
// Check for alignment
|
||||
VkDeviceSize checkOffset = lastOffset + lastSize, remainder = checkOffset % memoryRequirements.alignment;
|
||||
checkOffset = remainder ? checkOffset + (memoryRequirements.alignment - remainder) : checkOffset;
|
||||
// Check for enough space
|
||||
if (currentOffset - lastOffset - lastSize >= memoryRequirements.size) {
|
||||
outAllocation.memory = block.memory;
|
||||
outAllocation.offset = checkOffset;
|
||||
if (block.mappedMemoryPointer)
|
||||
outAllocation.mappedMemoryPointer = reinterpret_cast<char*>(block.mappedMemoryPointer) + outAllocation.offset;
|
||||
block.allocations[checkOffset] = memoryRequirements.size;
|
||||
return;
|
||||
}
|
||||
|
||||
lastOffset = currentOffset, lastSize = currentSize;
|
||||
}
|
||||
|
||||
// Check end
|
||||
VkDeviceSize checkOffset = lastOffset + lastSize, remainder = checkOffset % memoryRequirements.alignment;
|
||||
checkOffset = remainder ? checkOffset + (memoryRequirements.alignment - remainder) : checkOffset;
|
||||
if ((block.size - 1) - lastOffset - lastSize >= memoryRequirements.size) {
|
||||
outAllocation.memory = block.memory;
|
||||
outAllocation.offset = checkOffset;
|
||||
if (block.mappedMemoryPointer)
|
||||
outAllocation.mappedMemoryPointer = reinterpret_cast<char*>(block.mappedMemoryPointer) + outAllocation.offset;
|
||||
block.allocations[checkOffset] = memoryRequirements.size;
|
||||
return;
|
||||
}
|
||||
|
||||
// If not enough space, continue
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
// No suitable block found, allocate one
|
||||
VkMemoryAllocateInfo newBlockAllocateInfo = { VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO };
|
||||
newBlockAllocateInfo.memoryTypeIndex = memoryTypeIndex;
|
||||
newBlockAllocateInfo.allocationSize = calculateBlockSize(memoryTypeIndex);
|
||||
allocateBlock(newBlockAllocateInfo);
|
||||
|
||||
// If host visible, map
|
||||
if (memoryPropertyFlags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT)
|
||||
vkMapMemory(m_device, m_blocks.back().memory, 0, VK_WHOLE_SIZE, 0, &m_blocks.back().mappedMemoryPointer);
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t VideoMemoryAllocator::findMemoryType(uint32_t typeFilter, VkMemoryPropertyFlags desiredPropertyFlags) {
|
||||
VkPhysicalDeviceMemoryProperties memoryProperties = {};
|
||||
vkGetPhysicalDeviceMemoryProperties(m_physicalDevice, &memoryProperties);
|
||||
|
||||
for (int i = 0; i < memoryProperties.memoryTypeCount; i++)
|
||||
if (typeFilter & (1 << i) && (memoryProperties.memoryTypes[i].propertyFlags & desiredPropertyFlags) == desiredPropertyFlags)
|
||||
return i;
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
size_t VideoMemoryAllocator::calculateBlockSize(uint32_t memoryTypeIndex) const {
|
||||
VkPhysicalDeviceMemoryProperties memoryProperties = {};
|
||||
vkGetPhysicalDeviceMemoryProperties(m_physicalDevice, &memoryProperties);
|
||||
size_t heapSize = memoryProperties.memoryHeaps[memoryProperties.memoryTypes[memoryTypeIndex].heapIndex].size;
|
||||
|
||||
return heapSize > HEAP_SIZE_LARGE ? DEFAULT_ALLOCATION_AMOUNT : (heapSize / 8);
|
||||
}
|
||||
|
||||
void VideoMemoryAllocator::allocateBlock(VkMemoryAllocateInfo& allocateInfo) {
|
||||
VkDeviceMemory memoryBlock = nullptr;
|
||||
|
||||
if (vkAllocateMemory(m_device, &allocateInfo, nullptr, &memoryBlock) != VK_SUCCESS)
|
||||
NFError("Could not allocate video memory.");
|
||||
|
||||
m_blocks.emplace_back(memoryBlock, allocateInfo.memoryTypeIndex, allocateInfo.allocationSize, nullptr);
|
||||
}
|
||||
}
|
@ -1,41 +0,0 @@
|
||||
// VideoMemoryAllocator class header
|
||||
#pragma once
|
||||
|
||||
namespace nf::client::render {
|
||||
struct VideoMemoryAllocation {
|
||||
VkDeviceMemory memory;
|
||||
VkDeviceSize offset;
|
||||
void* mappedMemoryPointer;
|
||||
};
|
||||
|
||||
struct MemoryBlock {
|
||||
VkDeviceMemory memory;
|
||||
uint32_t memoryTypeIndex;
|
||||
size_t size;
|
||||
void* mappedMemoryPointer;
|
||||
|
||||
std::map<VkDeviceSize, VkDeviceSize> allocations;
|
||||
// Offset Size
|
||||
};
|
||||
|
||||
class VideoMemoryAllocator {
|
||||
public:
|
||||
VideoMemoryAllocator(const VkDevice& device, const VkPhysicalDevice& physicalDevice);
|
||||
|
||||
void allocateForBuffer(VkBuffer buffer, VkMemoryPropertyFlags memoryPropertyFlags, VideoMemoryAllocation& outAllocation);
|
||||
void allocateForImage(VkImage image, VkMemoryPropertyFlags memoryPropertyFlags, VideoMemoryAllocation& outAllocation);
|
||||
|
||||
void deallocate(const VideoMemoryAllocation& allocation);
|
||||
private:
|
||||
void allocate(VkMemoryRequirements memoryRequirements, VkMemoryPropertyFlags memoryPropertyFlags, VideoMemoryAllocation& outAllocation);
|
||||
uint32_t findMemoryType(uint32_t typeFilter, VkMemoryPropertyFlags desiredPropertyFlags);
|
||||
|
||||
size_t calculateBlockSize(uint32_t memoryTypeIndex) const;
|
||||
void allocateBlock(VkMemoryAllocateInfo& allocateInfo);
|
||||
|
||||
const VkDevice& m_device;
|
||||
const VkPhysicalDevice& m_physicalDevice;
|
||||
|
||||
std::vector<MemoryBlock> m_blocks;
|
||||
};
|
||||
}
|
@ -42,8 +42,3 @@
|
||||
// Vulkan
|
||||
#define VK_USE_PLATFORM_WIN32_KHR
|
||||
#include <vulkan/vulkan.h>
|
||||
|
||||
// GLM
|
||||
#define GLM_FORCE_DEPTH_ZERO_TO_ONE
|
||||
#include <glm/glm.hpp>
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
|
Loading…
x
Reference in New Issue
Block a user