Skip to content

Commit

Permalink
Cleaning up images and other vulkan things
Browse files Browse the repository at this point in the history
  • Loading branch information
cullvox committed Jun 17, 2024
1 parent a609a8f commit cc23fbd
Show file tree
Hide file tree
Showing 22 changed files with 308 additions and 535 deletions.
3 changes: 1 addition & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,7 @@ endif()

# export compile commands for clangd on some platforms
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

if(CMAKE_EXPORT_COMPILE_COMMANDS)
if (CMAKE_EXPORT_COMPILE_COMMANDS)
set(CMAKE_CXX_STANDARD_INCLUDE_DIRECTORIES ${CMAKE_CXX_IMPLICIT_INCLUDE_DIRECTORIES})
endif()

Expand Down
3 changes: 0 additions & 3 deletions Source/Engine/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,8 @@ list(APPEND Sources
"Graphics/Material.cpp"
"Graphics/Swapchain.cpp"
"Graphics/Mesh.cpp"
"Graphics/GeometryPass.cpp"
"Graphics/PresentPass.cpp"
"Graphics/Renderer.cpp"
"Graphics/GraphicsSystem.cpp"
# "Graphics/GeometryPass.cpp"


# ImGui
Expand Down
4 changes: 3 additions & 1 deletion Source/Engine/Graphics/Device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,9 @@ std::vector<const char*> Device::GetExtensions() {
// The engines required device extensions.
std::vector requiredExtensions = {
VK_KHR_SWAPCHAIN_EXTENSION_NAME,
#ifdef BLUEMETAL_VULKAN_PORTABILITY
"VK_KHR_portability_subset",
#endif
"VK_EXT_extended_dynamic_state"
};

Expand All @@ -202,7 +204,7 @@ std::vector<const char*> Device::GetExtensions() {
[pName](const auto& properties){
return std::strcmp(pName, properties.extensionName) == 0;
})) {
throw std::runtime_error("Could not find required device extension: {}");
throw std::runtime_error("Could not find a required device extension!");
}
}

Expand Down
59 changes: 0 additions & 59 deletions Source/Engine/Graphics/GeometryPass.cpp

This file was deleted.

25 changes: 0 additions & 25 deletions Source/Engine/Graphics/GeometryPass.h

This file was deleted.

74 changes: 57 additions & 17 deletions Source/Engine/Graphics/Image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,18 @@
namespace bl
{

Image::Image(Device* device, VkImageType type, VkExtent3D extent, VkFormat format, VkImageUsageFlags usage, VkImageAspectFlags aspectMask, uint32_t mipLevels)
Image::Image(Device* device, VkImageType type, VkExtent3D extent, VkFormat format, VkImageUsageFlags usage, VkImageAspectFlags aspectMask, VkImageLayout initialLayout, uint32_t mipLevels)
: _device(device)
, _extent(extent)
, _type(type)
, _format(format)
, _usage(usage)
, _aspectMask(aspectMask)
, _layout(initialLayout)
, _mipLevels(mipLevels) {
CreateImage();
}

Image::Image(Image& rhs)
: _device(rhs._device)
, _extent(rhs._extent)
, _type(rhs._type)
, _format(rhs._format)
, _usage(rhs._usage)
, _aspectMask(rhs._aspectMask)
, _mipLevels(rhs._mipLevels) {
CreateImage();
}

Image::Image(Image&& rhs)
: _device(rhs._device)
, _extent(rhs._extent)
Expand Down Expand Up @@ -66,10 +56,6 @@ VkImageUsageFlags Image::GetUsage() const {
return _usage;
}

VkImageView Image::GetView() const {
return _imageView;
}

VkImageLayout Image::GetLayout() const {
return _layout;
}
Expand All @@ -78,6 +64,60 @@ VkImage Image::Get() const {
return _image;
}

VkImageView Image::GetView() const {
return _imageView;
}

void Image::Transition(VkImageLayout layout) {

_device->ImmediateSubmit([&](VkCommandBuffer cmd){

VkPipelineStageFlags sourceStage = VK_PIPELINE_STAGE_NONE;
VkPipelineStageFlags destinationStage = VK_PIPELINE_STAGE_NONE;

VkImageMemoryBarrier barrier = {};
barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
barrier.pNext = nullptr;

if (_layout == VK_IMAGE_LAYOUT_UNDEFINED && layout == VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL) {
barrier.srcAccessMask = 0;
barrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;

sourceStage = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
destinationStage = VK_PIPELINE_STAGE_TRANSFER_BIT;
} else if (_layout == VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL && layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
barrier.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
barrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT;

sourceStage = VK_PIPELINE_STAGE_TRANSFER_BIT;
destinationStage = VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT;
} else {
throw std::runtime_error("Unsupported image layout transition!");
}

barrier.oldLayout = _layout;
barrier.newLayout = layout;
barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
barrier.image = _image;
barrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
barrier.subresourceRange.baseMipLevel = 0;
barrier.subresourceRange.levelCount = 1;
barrier.subresourceRange.baseArrayLayer = 0;
barrier.subresourceRange.layerCount = 1;

vkCmdPipelineBarrier(
cmd,
sourceStage, destinationStage,
0,
0, nullptr,
0, nullptr,
1, &barrier);
});

_layout = layout;
}

void Image::CreateImage() {
auto graphicsFamilyIndex = _device->GetGraphicsFamilyIndex();

Expand All @@ -96,7 +136,7 @@ void Image::CreateImage() {
imageCreateInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
imageCreateInfo.queueFamilyIndexCount = 1;
imageCreateInfo.pQueueFamilyIndices = &graphicsFamilyIndex;
imageCreateInfo.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
imageCreateInfo.initialLayout = _layout;

VmaAllocationCreateInfo allocationCreateInfo = {};
allocationCreateInfo.flags = 0;
Expand Down
82 changes: 65 additions & 17 deletions Source/Engine/Graphics/Image.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,76 @@

#include "Device.h"

namespace bl
{
namespace bl {

/** @brief Creates a graphics image on the physical device. */
class Image
{
/// @brief Creates a graphics image on the physical device.
class Image {
public:
Image(Device* device, VkImageType type, VkExtent3D extent, VkFormat format, VkImageUsageFlags usage, VkImageAspectFlags aspectMask, uint32_t mipLevels = 0); /** @brief Constructor */
Image(Image& copy); /** @brief Copy constructor */
Image(Image&& move); /** @brief Move constructor */
~Image(); /** @brief Destructor */
/// @brief Default Constructor
Image();

Image& operator=(Image& rhs); /** @brief Assign copy */
/// @brief Image Constructor
/// @param[in] device Device to contruct the image from.
/// @param[in] type Type of image to create.
/// @param[in] extent The extent in pixels of the image.
/// @param[in] usage What the image is used for in api.
/// @param[in] viewAspectMask The default image view aspect mask.
/// @param[in] mipLevels How many mipmap levels will this image have.
Image(Device* device,
VkImageType type,
VkExtent3D extent,
VkFormat format,
VkImageUsageFlags usage,
VkImageAspectFlags viewAspectMask,
VkImageLayout initialLayout = VK_IMAGE_LAYOUT_UNDEFINED,
uint32_t mipLevels = 0);

/// @brief Move Constructor
/// @param[inout] image The other image to move it's data into this new object.
Image(Image&& image);

/// @brief Default Destructor
~Image();

public:
/// @brief Move Assign Operator
/// @param[inout] rhs The other image to move it's data into this new object.
Image& operator=(Image&& rhs); /** @brief Assign move */

VkImageType GetType() const; /** @brief Returns the image type. */
VkExtent3D GetExtent() const; /** @brief Returns the image size in pixels at construction. */
VkFormat GetFormat() const; /** @brief Returns the image format at construction. */
VkImageUsageFlags GetUsage() const; /** @brief Returns the image usage at construction. */
VkImage Get() const; /** @brief Returns the vulkan image created at construction. */
VkImageView GetView() const; /** @brief Returns the default image view created at construction. */
VkImageLayout GetLayout() const; /** @brief Returns the images layout. */
public:
/// @brief GetType
/// @returns The type of image this was created as.
VkImageType GetType() const;

/// @brief GetExtent
/// @returns Returns the image size in pixels at construction.
VkExtent3D GetExtent() const;

/// @brief GetFormat
/// @returns Returns the current image format.
VkFormat GetFormat() const;

/// @brief GetUsage
/// @returns Returns the image usage at construction.
VkImageUsageFlags GetUsage() const;

/// @brief GetLayout
/// @return Returns the current image layout.
VkImageLayout GetLayout() const;

/// @brief Get
/// @return Returns the underlying Vulkan image from construction.
VkImage Get() const;

/// @brief GetView
/// @return Returns the default image view created at construction.
VkImageView GetView() const;

public:
/// @brief Transitions the image from the previous layout to another new one.
/// @param layout[in] New layout to transition the image into.
/// @param format[in] A new format to set the image to, leave empty to use the current format.
void Transition(VkImageLayout layout);

private:
void CreateImage();
Expand Down
Loading

0 comments on commit cc23fbd

Please sign in to comment.