Skip to content

Commit

Permalink
[*] WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
IAmNotHanni committed Nov 3, 2023
1 parent 92de322 commit 8b87f66
Show file tree
Hide file tree
Showing 8 changed files with 152 additions and 55 deletions.
4 changes: 3 additions & 1 deletion include/inexor/vulkan-renderer/application.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ class Application {
std::unique_ptr<RenderGraph> m_render_graph;
TextureResource *m_back_buffer{nullptr};
TextureResource *m_depth_buffer{nullptr};
TextureResource *m_msaa_target{nullptr};
TextureResource *m_msaa_depth{nullptr};
TextureResource *m_msaa_color{nullptr};

BufferResource *m_index_buffer{nullptr};
BufferResource *m_vertex_buffer{nullptr};
BufferResource *m_uniform_buffer{nullptr};
Expand Down
12 changes: 6 additions & 6 deletions include/inexor/vulkan-renderer/render_graph.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,17 +135,16 @@ class BufferResource : public RenderResource {
};

enum class TextureUsage {
/// Specifies that this texture is the output of the render graph
// TODO: Refactor back buffer system more (remove need for BACK_BUFFER texture usage)
BACK_BUFFER,

/// A render target for multi sampling anti aliasing (MSAA)
MSAA_RENDER_TARGET,
BACK_BUFFER,
MSAA_BACK_BUFFER,

/// Specifies that this texture is a combined depth/stencil buffer
/// @note This may mean that this texture is completely GPU-sided and cannot be accessed by the CPU in any way.
DEPTH_STENCIL_BUFFER,

MSAA_DEPTH_STENCIL_BUFFER,

/// Specifies that this texture isn't used for any special purpose
NORMAL,
};
Expand Down Expand Up @@ -285,7 +284,7 @@ class GraphicsStage : public RenderStage {
bool m_depth_write{false};
VkClearValue m_clear_value{};

VkFormat m_swapchain_img_format;
VkFormat m_swapchain_img_format{VK_FORMAT_UNDEFINED};
VkPipelineRenderingCreateInfo m_pipeline_rendering_ci{};
VkPipelineColorBlendAttachmentState m_color_blend_attachment{};

Expand Down Expand Up @@ -595,6 +594,7 @@ class PhysicalImage : public PhysicalResource {
}
};

// TODO: Delete me I am useless
class PhysicalBackBuffer : public PhysicalResource {
friend RenderGraph;

Expand Down
1 change: 1 addition & 0 deletions include/inexor/vulkan-renderer/wrapper/device.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class Device {
std::string m_gpu_name;
VkPhysicalDeviceFeatures m_enabled_features{};
VkPhysicalDeviceProperties m_properties{};
VkSampleCountFlagBits m_max_usable_sample_count{VK_SAMPLE_COUNT_1_BIT};

VkQueue m_graphics_queue{VK_NULL_HANDLE};
VkQueue m_present_queue{VK_NULL_HANDLE};
Expand Down
4 changes: 4 additions & 0 deletions include/inexor/vulkan-renderer/wrapper/image.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ class Image {
Image(const Device &device, VkFormat format, std::uint32_t width, std::uint32_t height, VkImageUsageFlags usage,
VkImageAspectFlags aspect_flags, const std::string &name);

// TODO
Image(const Device &device, VkFormat format, std::uint32_t width, std::uint32_t height, VkImageUsageFlags usage,
VkImageAspectFlags aspect_flags, const std::string &name, VkSampleCountFlags sample_count);

/// Constructor 6 (calls constructor 3 internally)
/// @param device The device wrapper
/// @param format The image format
Expand Down
22 changes: 16 additions & 6 deletions src/vulkan-renderer/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -304,10 +304,15 @@ Application::Application(int argc, char **argv) {

const VkPhysicalDeviceFeatures required_features{
// Add required physical device features here
.sampleRateShading = VK_TRUE,
.samplerAnisotropy = VK_TRUE,
};

const VkPhysicalDeviceFeatures optional_features{
// Add optional physical device features here
// TODO: Add callback on_device_feature_not_available and remove optional features
// Then, return true or false from the callback, indicating if you can run the app
// without this physical device feature being present.
};

std::vector<const char *> required_extensions{
Expand Down Expand Up @@ -392,9 +397,9 @@ void Application::recreate_swapchain() {
m_camera->set_movement_speed(5.0f);
m_camera->set_rotation_speed(0.5f);

m_imgui_overlay.reset();
m_imgui_overlay = std::make_unique<ImGUIOverlay>(*m_device, m_render_graph.get(), m_back_buffer,
[&]() { update_imgui_overlay(); });
// m_imgui_overlay.reset();
// m_imgui_overlay = std::make_unique<ImGUIOverlay>(*m_device, m_render_graph.get(), m_back_buffer,
// [&]() { update_imgui_overlay(); });

m_render_graph->compile(m_back_buffer);
}
Expand Down Expand Up @@ -431,14 +436,17 @@ void Application::render_frame() {

void Application::setup_render_graph() {
m_back_buffer =
m_render_graph->add<TextureResource>(TextureUsage::BACK_BUFFER, m_swapchain->image_format(), "Back Buffer");
m_render_graph->add<TextureResource>(TextureUsage::BACK_BUFFER, m_swapchain->image_format(), "color");

m_msaa_target = m_render_graph->add<TextureResource>(TextureUsage::MSAA_RENDER_TARGET, m_swapchain->image_format(),
"MSAA Target");
m_msaa_color =
m_render_graph->add<TextureResource>(TextureUsage::MSAA_BACK_BUFFER, m_swapchain->image_format(), "MSAA color");

m_depth_buffer = m_render_graph->add<TextureResource>(TextureUsage::DEPTH_STENCIL_BUFFER,
VK_FORMAT_D32_SFLOAT_S8_UINT, "Depth Buffer");

m_msaa_depth = m_render_graph->add<TextureResource>(TextureUsage::MSAA_DEPTH_STENCIL_BUFFER,
VK_FORMAT_D32_SFLOAT_S8_UINT, "MSAA depth");

m_vertex_buffer = m_render_graph->add<BufferResource>("Octree", BufferUsage::VERTEX_BUFFER, [&]() {
if (m_input_data->was_key_pressed_once(GLFW_KEY_N)) {
load_octree_geometry(false);
Expand Down Expand Up @@ -492,6 +500,8 @@ void Application::setup_render_graph() {
->set_vertex_input_binding_descriptions<OctreeGpuVertex>()
->writes_to(m_back_buffer)
->writes_to(m_depth_buffer)
->writes_to(m_msaa_color)
->writes_to(m_msaa_depth)
->reads_from(m_index_buffer)
->reads_from(m_vertex_buffer)
->reads_from(m_uniform_buffer, VK_SHADER_STAGE_VERTEX_BIT)
Expand Down
110 changes: 81 additions & 29 deletions src/vulkan-renderer/render_graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ VkGraphicsPipelineCreateInfo GraphicsStage::make_create_info(const VkFormat swap
.pNext = nullptr,
.colorAttachmentCount = 1,
.pColorAttachmentFormats = &m_swapchain_img_format,
// TODO: Implement set_depth_format() and move to pipeline builder!
.depthAttachmentFormat = VK_FORMAT_D32_SFLOAT_S8_UINT,
// TODO: Implement set_stencil_format() and move to pipeline builder!
.stencilAttachmentFormat = VK_FORMAT_D32_SFLOAT_S8_UINT,
});

return wrapper::make_info<VkGraphicsPipelineCreateInfo>({
Expand Down Expand Up @@ -276,39 +280,63 @@ void RenderGraph::record_command_buffer(const bool first_stage, const bool last_
const auto *phys_graphics_stage = physical.as<PhysicalStage>();
assert(phys_graphics_stage != nullptr);

const auto color_attachment = wrapper::make_info<VkRenderingAttachmentInfo>({
.imageView = m_swapchain.image_view(image_index),
.imageLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
.loadOp = graphics_stage->m_clears_screen ? VK_ATTACHMENT_LOAD_OP_CLEAR : VK_ATTACHMENT_LOAD_OP_LOAD,
.storeOp = VK_ATTACHMENT_STORE_OP_STORE,
.clearValue =
{
.color = graphics_stage->m_clear_value.color,
},
});

VkImageView depth_buffer_img_view{VK_NULL_HANDLE};
VkImage depth_buffer{VK_NULL_HANDLE};
VkImageView depth_buffer{VK_NULL_HANDLE};
VkImageView resolve_color_buffer{VK_NULL_HANDLE};
VkImageView resolve_depth_buffer{VK_NULL_HANDLE};

// Loop through all writes and find the depth buffer
for (const auto &resource : stage->m_reads) {
const auto *texture_resource = resource.first->as<TextureResource>();
for (const auto &resource : stage->m_writes) {
const auto *texture_resource = resource->as<TextureResource>();
if (texture_resource == nullptr) {
continue;
}
auto *physical_texture = texture_resource->m_physical->as<PhysicalImage>();
if (physical_texture == nullptr) {
continue;
}
if (physical_texture->m_img == nullptr) {
continue;
}
if (texture_resource->m_usage == TextureUsage::DEPTH_STENCIL_BUFFER) {
depth_buffer_img_view = physical_texture->image_view();
depth_buffer = physical_texture->m_img->image();
depth_buffer = physical_texture->image_view();
}
if (texture_resource->m_usage == TextureUsage::MSAA_DEPTH_STENCIL_BUFFER) {
resolve_depth_buffer = physical_texture->image_view();
}
if (first_stage) {
if (texture_resource->m_usage == TextureUsage::MSAA_BACK_BUFFER) {
resolve_color_buffer = physical_texture->image_view();
}
}
}

if (first_stage) {
assert(resolve_color_buffer);
}

const auto depth_attachment = wrapper::make_info<VkRenderingAttachmentInfo>({
.imageView = depth_buffer_img_view,
assert(depth_buffer);
assert(resolve_depth_buffer);

const auto color_attachment = wrapper::make_info<VkRenderingAttachmentInfo>({
.imageView = resolve_color_buffer,
.imageLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
.resolveMode = VK_RESOLVE_MODE_AVERAGE_BIT,
.resolveImageView = m_swapchain.image_view(image_index),
.resolveImageLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
.loadOp = graphics_stage->m_clears_screen ? VK_ATTACHMENT_LOAD_OP_CLEAR : VK_ATTACHMENT_LOAD_OP_LOAD,
.storeOp = VK_ATTACHMENT_STORE_OP_STORE,
.clearValue =
{
.color = graphics_stage->m_clear_value.color,
},
});

auto depth_attachment = wrapper::make_info<VkRenderingAttachmentInfo>({
.imageView = resolve_depth_buffer,
.imageLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
.resolveMode = VK_RESOLVE_MODE_MIN_BIT,
.resolveImageView = depth_buffer,
.resolveImageLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
.loadOp = graphics_stage->m_clears_screen ? VK_ATTACHMENT_LOAD_OP_CLEAR : VK_ATTACHMENT_LOAD_OP_LOAD,
.storeOp = VK_ATTACHMENT_STORE_OP_STORE,
.clearValue =
Expand Down Expand Up @@ -415,8 +443,9 @@ void RenderGraph::create_texture_resources() {
// TODO: Move this to representation header
const std::unordered_map<TextureUsage, std::string> texture_usage_name{
{TextureUsage::BACK_BUFFER, "BACK_BUFFER"},
{TextureUsage::MSAA_RENDER_TARGET, "MSAA_RENDER_TARGET"},
{TextureUsage::MSAA_BACK_BUFFER, "MSAA_BACK_BUFFER"},
{TextureUsage::DEPTH_STENCIL_BUFFER, "DEPTH_STENCIL_BUFFER"},
{TextureUsage::MSAA_DEPTH_STENCIL_BUFFER, "MSAA_DEPTH_STENCIL_BUFFER"},
{TextureUsage::NORMAL, "NORMAL"},
};

Expand All @@ -430,15 +459,30 @@ void RenderGraph::create_texture_resources() {
auto physical = std::make_shared<PhysicalImage>(m_device);
texture_resource->m_physical = physical;

physical->m_img = std::make_unique<wrapper::Image>(
m_device, texture_resource->m_format, m_swapchain.extent().width, m_swapchain.extent().height,
texture_resource->m_usage == TextureUsage::DEPTH_STENCIL_BUFFER
? static_cast<VkImageUsageFlags>(VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT)
: static_cast<VkImageUsageFlags>(VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT),
static_cast<VkImageAspectFlags>(texture_resource->m_usage == TextureUsage::DEPTH_STENCIL_BUFFER
? VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT
: VK_IMAGE_ASPECT_COLOR_BIT),
VK_IMAGE_LAYOUT_UNDEFINED, texture_resource->name());
switch (texture_resource->m_usage) {
case TextureUsage::MSAA_BACK_BUFFER: {
physical->m_img = std::make_unique<wrapper::Image>(
m_device, texture_resource->m_format, m_swapchain.extent().width, m_swapchain.extent().height,
VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, VK_IMAGE_ASPECT_COLOR_BIT, texture_resource->name(),
m_device.get_max_usable_sample_count());
break;
}
case TextureUsage::DEPTH_STENCIL_BUFFER: {
physical->m_img = std::make_unique<wrapper::Image>(
m_device, texture_resource->m_format, m_swapchain.extent().width, m_swapchain.extent().height,
VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT,
VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT, texture_resource->name());
break;
}
case TextureUsage::MSAA_DEPTH_STENCIL_BUFFER: {
physical->m_img = std::make_unique<wrapper::Image>(
m_device, texture_resource->m_format, m_swapchain.extent().width, m_swapchain.extent().height,
VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT,
VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT, texture_resource->name(),
m_device.get_max_usable_sample_count());
break;
}
}
}
}
}
Expand Down Expand Up @@ -485,7 +529,14 @@ void RenderGraph::create_pipeline_layout(PhysicalStage &physical, GraphicsStage
"Graphics Pipeline Layout " + stage->name());
}

// TODO: Remove graphics pipeline construction entirely from rendergraph
void RenderGraph::create_graphics_pipeline(PhysicalStage &physical, GraphicsStage *stage) {
// Ugly hack, remove me
bool is_first = false;
if (stage->name() == "Octree") {
is_first = true;
}

physical.m_pipeline = std::make_unique<wrapper::pipelines::GraphicsPipeline>(
m_device,
stage
Expand All @@ -498,6 +549,7 @@ void RenderGraph::create_graphics_pipeline(PhysicalStage &physical, GraphicsStag
.depthWriteEnable = stage->m_depth_write ? VK_TRUE : VK_FALSE,
.depthCompareOp = VK_COMPARE_OP_LESS_OR_EQUAL,
}))
->set_multisampling(m_device.get_max_usable_sample_count(), 0.1f)
->set_pipeline_layout(physical.m_pipeline_layout->pipeline_layout())
->set_scissor(m_swapchain.extent())
->set_viewport(m_swapchain.extent())
Expand Down
32 changes: 19 additions & 13 deletions src/vulkan-renderer/wrapper/device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,24 @@ Device::Device(const Instance &inst, const VkSurfaceKHR surface, const bool pref

// Store the properties of this physical device
vkGetPhysicalDeviceProperties(m_physical_device, &m_properties);

auto determine_max_usable_sample_count = [&]() {
const auto sample_count =
m_properties.limits.framebufferColorSampleCounts & m_properties.limits.framebufferDepthSampleCounts;

const std::array<VkSampleCountFlagBits, 6> sample_count_flag_bits{
VK_SAMPLE_COUNT_64_BIT, VK_SAMPLE_COUNT_32_BIT, VK_SAMPLE_COUNT_16_BIT,
VK_SAMPLE_COUNT_8_BIT, VK_SAMPLE_COUNT_4_BIT, VK_SAMPLE_COUNT_2_BIT};

for (const auto &sample_count_flag_bit : sample_count_flag_bits) {
if (sample_count & sample_count_flag_bit) {
return sample_count_flag_bit;
}
}
return VK_SAMPLE_COUNT_1_BIT;
};

m_max_usable_sample_count = determine_max_usable_sample_count();
}

Device::~Device() {
Expand Down Expand Up @@ -423,19 +441,7 @@ VkSurfaceCapabilitiesKHR Device::get_surface_capabilities(const VkSurfaceKHR sur
}

VkSampleCountFlagBits Device::get_max_usable_sample_count() const {
const auto sample_count =
m_properties.limits.framebufferColorSampleCounts & m_properties.limits.framebufferDepthSampleCounts;

const std::array<VkSampleCountFlagBits, 6> sample_count_flag_bits{VK_SAMPLE_COUNT_64_BIT, VK_SAMPLE_COUNT_32_BIT,
VK_SAMPLE_COUNT_16_BIT, VK_SAMPLE_COUNT_8_BIT,
VK_SAMPLE_COUNT_4_BIT, VK_SAMPLE_COUNT_2_BIT};

for (const auto &sample_count_flag_bit : sample_count_flag_bits) {
if (sample_count & sample_count_flag_bit) {
return sample_count_flag_bit;
}
}
return VK_SAMPLE_COUNT_1_BIT;
return m_max_usable_sample_count;
}

bool Device::format_supports_feature(const VkFormat format, const VkFormatFeatureFlagBits feature) const {
Expand Down
22 changes: 22 additions & 0 deletions src/vulkan-renderer/wrapper/image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,28 @@ Image::Image(const Device &device, const VkFormat format, const std::uint32_t wi
}),
aspect_flags, name) {}

Image::Image(const Device &device, const VkFormat format, const std::uint32_t width, const std::uint32_t height,
const VkImageUsageFlags usage, const VkImageAspectFlags aspect_flags, const std::string &name,
VkSampleCountFlags sample_count)
: Image(device,
wrapper::make_info<VkImageCreateInfo>({
.imageType = VK_IMAGE_TYPE_2D,
.format = format,
.extent{
.width = width,
.height = height,
.depth = 1,
},
.mipLevels = 1,
.arrayLayers = 1,
.samples = static_cast<VkSampleCountFlagBits>(sample_count),
.tiling = VK_IMAGE_TILING_OPTIMAL,
.usage = usage,
.sharingMode = VK_SHARING_MODE_EXCLUSIVE,
.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED,
}),
aspect_flags, name) {}

Image::Image(const Device &device, const VkFormat format, const std::uint32_t width, const std::uint32_t height,
const VkImageUsageFlags usage, const VkImageAspectFlags aspect_flags, const VkImageLayout initial_layout,
const std::string &name)
Expand Down

0 comments on commit 8b87f66

Please sign in to comment.