Browse Source

Vk: Cleanup some code

Signed-off-by: Squareys <squareys@googlemail.com>
pull/202/head
Squareys 10 years ago committed by Squareys
parent
commit
bb513de4b9
  1. 2
      src/Magnum/Vk/Instance.h
  2. 4
      src/Magnum/Vk/PhysicalDevice.h
  3. 53
      src/Magnum/Vk/Pipeline.h
  4. 40
      src/Magnum/Vk/Swapchain.cpp

2
src/Magnum/Vk/Instance.h

@ -30,7 +30,7 @@
* @brief Class @ref Magnum::Vk::Instance * @brief Class @ref Magnum::Vk::Instance
*/ */
#include "vulkan.h" #include <vulkan.h>
#include <cstdlib> #include <cstdlib>
#include <array> #include <array>

4
src/Magnum/Vk/PhysicalDevice.h

@ -104,7 +104,7 @@ class MAGNUM_VK_EXPORT PhysicalDevice {
/** /**
* @brief Get the underlying VkPhysicalDevice handle * @brief Get the underlying VkPhysicalDevice handle
*/ */
VkPhysicalDevice vkPhysicalDevice() { operator VkPhysicalDevice() const {
return _physicalDevice; return _physicalDevice;
} }
@ -112,7 +112,7 @@ class MAGNUM_VK_EXPORT PhysicalDevice {
UnsignedInt getQueueFamilyIndex(QueueFamily family); UnsignedInt getQueueFamilyIndex(QueueFamily family);
VkPhysicalDeviceProperties getProperties() { VkPhysicalDeviceProperties getProperties() const {
VkPhysicalDeviceProperties deviceProperties; VkPhysicalDeviceProperties deviceProperties;
vkGetPhysicalDeviceProperties(_physicalDevice, &deviceProperties); vkGetPhysicalDeviceProperties(_physicalDevice, &deviceProperties);

53
src/Magnum/Vk/Pipeline.h

@ -50,6 +50,11 @@ enum class Format: UnsignedInt;
class DescriptorSetLayout; class DescriptorSetLayout;
/**
@brief Dynamic state enum
States which can be set changable using commands.
*/
enum class DynamicState: UnsignedInt { enum class DynamicState: UnsignedInt {
Viewport = VK_DYNAMIC_STATE_VIEWPORT, Viewport = VK_DYNAMIC_STATE_VIEWPORT,
Scissor = VK_DYNAMIC_STATE_SCISSOR, Scissor = VK_DYNAMIC_STATE_SCISSOR,
@ -62,6 +67,9 @@ enum class DynamicState: UnsignedInt {
StencilReference = VK_DYNAMIC_STATE_STENCIL_REFERENCE, StencilReference = VK_DYNAMIC_STATE_STENCIL_REFERENCE,
}; };
/**
@brief Primitive topology enum
*/
enum class PrimitiveTopology: UnsignedInt { enum class PrimitiveTopology: UnsignedInt {
PointList = VK_PRIMITIVE_TOPOLOGY_POINT_LIST, PointList = VK_PRIMITIVE_TOPOLOGY_POINT_LIST,
LineList = VK_PRIMITIVE_TOPOLOGY_LINE_LIST, LineList = VK_PRIMITIVE_TOPOLOGY_LINE_LIST,
@ -76,6 +84,11 @@ enum class PrimitiveTopology: UnsignedInt {
PatchList = VK_PRIMITIVE_TOPOLOGY_PATCH_LIST, PatchList = VK_PRIMITIVE_TOPOLOGY_PATCH_LIST,
}; };
/**
@brief Pipeline state enum
Describes pipeline stages.
*/
enum class PipelineStage: UnsignedInt { enum class PipelineStage: UnsignedInt {
TopOfThePipe = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, TopOfThePipe = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
DrawIndirect = VK_PIPELINE_STAGE_DRAW_INDIRECT_BIT, DrawIndirect = VK_PIPELINE_STAGE_DRAW_INDIRECT_BIT,
@ -105,6 +118,13 @@ enum class BindPoint: UnsignedInt {
Compute = VK_PIPELINE_BIND_POINT_COMPUTE, Compute = VK_PIPELINE_BIND_POINT_COMPUTE,
}; };
/**
@brief Pipeline class
Contains state of a graphics or compute pipeline to be bound during a renderpass.
@see GraphicsPipelineBuilder
*/
class MAGNUM_VK_EXPORT Pipeline { class MAGNUM_VK_EXPORT Pipeline {
public: public:
Pipeline(Device& device, VkPipeline pipeline, VkPipelineCache cache, VkPipelineLayout layout): Pipeline(Device& device, VkPipeline pipeline, VkPipelineCache cache, VkPipelineLayout layout):
@ -183,6 +203,12 @@ class MAGNUM_VK_EXPORT Pipeline {
VkPipelineLayout _layout; VkPipelineLayout _layout;
}; };
/**
@brief Graphics pipeline builder
Contains properties to create a @ref Pipeline. While the pipeline cannot be changed after it is created,
the pipeline builder can be used to create another one.
*/
class MAGNUM_VK_EXPORT GraphicsPipelineBuilder { class MAGNUM_VK_EXPORT GraphicsPipelineBuilder {
public: public:
@ -201,9 +227,7 @@ class MAGNUM_VK_EXPORT GraphicsPipelineBuilder {
}; };
_depthStencilState = { _depthStencilState = {
VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO, VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO, nullptr, 0,
nullptr,
0,
VK_TRUE, /* depth test enable */ VK_TRUE, /* depth test enable */
VK_TRUE, /* depth write enable */ VK_TRUE, /* depth write enable */
VK_COMPARE_OP_LESS_OR_EQUAL, /* depth compare op */ VK_COMPARE_OP_LESS_OR_EQUAL, /* depth compare op */
@ -215,9 +239,7 @@ class MAGNUM_VK_EXPORT GraphicsPipelineBuilder {
}; };
_rasterizationState = { _rasterizationState = {
VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO, VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO, nullptr, 0,
nullptr,
0,
VK_FALSE, /* depth clamp enable */ VK_FALSE, /* depth clamp enable */
VK_FALSE, /* rasterizer discard enable */ VK_FALSE, /* rasterizer discard enable */
VK_POLYGON_MODE_FILL, /* poygon mode */ VK_POLYGON_MODE_FILL, /* poygon mode */
@ -239,9 +261,7 @@ class MAGNUM_VK_EXPORT GraphicsPipelineBuilder {
_blendAttachments.push_back(blendAttachmentState); _blendAttachments.push_back(blendAttachmentState);
_colorBlendState = { _colorBlendState = {
VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO, VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO, nullptr, 0,
nullptr,
0,
VK_FALSE, VK_FALSE,
VK_LOGIC_OP_CLEAR, VK_LOGIC_OP_CLEAR,
UnsignedInt(_blendAttachments.size()), UnsignedInt(_blendAttachments.size()),
@ -250,18 +270,16 @@ class MAGNUM_VK_EXPORT GraphicsPipelineBuilder {
}; };
_viewportState = { _viewportState = {
VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO, VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO, nullptr, 0,
nullptr,
0,
1, 1,
nullptr, // TODO currently expecting dynamic state here... nullptr, // TODO currently allways expecting dynamic state here...
1, 1,
nullptr nullptr
}; };
} }
/** @brief Copying is not allowed */ // TODO(squareys) probably should be, though /** @brief Copying is not allowed */
GraphicsPipelineBuilder(const GraphicsPipelineBuilder&) = delete; GraphicsPipelineBuilder(const GraphicsPipelineBuilder&) = delete;
/** @brief Move constructor */ /** @brief Move constructor */
@ -272,8 +290,7 @@ class MAGNUM_VK_EXPORT GraphicsPipelineBuilder {
* *
* @see @fn_vk{DestroyPipeline} * @see @fn_vk{DestroyPipeline}
*/ */
~GraphicsPipelineBuilder() { ~GraphicsPipelineBuilder() = default;
}
/** @brief Copying is not allowed */ /** @brief Copying is not allowed */
GraphicsPipelineBuilder& operator=(const GraphicsPipelineBuilder&) = delete; GraphicsPipelineBuilder& operator=(const GraphicsPipelineBuilder&) = delete;
@ -295,8 +312,12 @@ class MAGNUM_VK_EXPORT GraphicsPipelineBuilder {
return *this; return *this;
} }
/**
* @brief Create a pipeline with currently set properties
*/
std::unique_ptr<Pipeline> build(); std::unique_ptr<Pipeline> build();
GraphicsPipelineBuilder& setRenderPass(RenderPass& renderPass) { GraphicsPipelineBuilder& setRenderPass(RenderPass& renderPass) {
_renderPass = &renderPass; _renderPass = &renderPass;
return *this; return *this;

40
src/Magnum/Vk/Swapchain.cpp

@ -39,8 +39,7 @@ Swapchain::Swapchain(Device& device, CommandBuffer& cb, VkSurfaceKHR surface):
_swapchain{VK_NULL_HANDLE}, _swapchain{VK_NULL_HANDLE},
_currentIndex{0} _currentIndex{0}
{ {
VkDevice vkDevice = _device.vkDevice(); VkDevice vkDevice = _device;
VkPhysicalDevice vkPhysicalDevice = _device.physicalDevice().vkPhysicalDevice();
#define GET_INSTANCE_PROC_ADDR(entrypoint) vk##entrypoint = PFN_vk##entrypoint(vkGetInstanceProcAddr(Vk::Instance::current(), "vk"#entrypoint)); do{if(vk##entrypoint == nullptr) { Error() << "Failed to get function pointer.";} }while(false) #define GET_INSTANCE_PROC_ADDR(entrypoint) vk##entrypoint = PFN_vk##entrypoint(vkGetInstanceProcAddr(Vk::Instance::current(), "vk"#entrypoint)); do{if(vk##entrypoint == nullptr) { Error() << "Failed to get function pointer.";} }while(false)
#define GET_DEVICE_PROC_ADDR(entrypoint) vk##entrypoint = PFN_vk##entrypoint(vkGetDeviceProcAddr(vkDevice, "vk"#entrypoint)); do{ if(vk##entrypoint == nullptr) { Error() << "Failed to get function pointer.";} }while(false) #define GET_DEVICE_PROC_ADDR(entrypoint) vk##entrypoint = PFN_vk##entrypoint(vkGetDeviceProcAddr(vkDevice, "vk"#entrypoint)); do{ if(vk##entrypoint == nullptr) { Error() << "Failed to get function pointer.";} }while(false)
@ -59,26 +58,28 @@ Swapchain::Swapchain(Device& device, CommandBuffer& cb, VkSurfaceKHR surface):
#undef GET_INSTANCE_PROC_ADDR #undef GET_INSTANCE_PROC_ADDR
#undef GET_DEVICE_PROC_ADDR #undef GET_DEVICE_PROC_ADDR
VkPhysicalDevice vkPhysicalDevice = _device.physicalDevice();
VkResult err; VkResult err;
// Get available queue family properties // Get available queue family properties TODO(squareys): Move to PhysicalDevice
uint32_t queueCount; uint32_t queueCount;
vkGetPhysicalDeviceQueueFamilyProperties(vkPhysicalDevice, vkGetPhysicalDeviceQueueFamilyProperties(vkPhysicalDevice,
&queueCount, nullptr); &queueCount, nullptr);
assert(queueCount >= 1); assert(queueCount >= 1);
// TODO(squareys): Move to PhysicalDevice
std::vector<VkQueueFamilyProperties> queueProps(queueCount); std::vector<VkQueueFamilyProperties> queueProps(queueCount);
vkGetPhysicalDeviceQueueFamilyProperties(vkPhysicalDevice, vkGetPhysicalDeviceQueueFamilyProperties(vkPhysicalDevice,
&queueCount, queueProps.data()); &queueCount, queueProps.data());
// TODO(squareys): Move to Device/PhysicalDevice instead
std::vector<VkBool32> supportsPresent(queueCount); std::vector<VkBool32> supportsPresent(queueCount);
for (uint32_t i = 0; i < queueCount; i++) { for (uint32_t i = 0; i < queueCount; i++) {
vkGetPhysicalDeviceSurfaceSupportKHR(vkPhysicalDevice, vkGetPhysicalDeviceSurfaceSupportKHR(vkPhysicalDevice, i, surface, &supportsPresent[i]);
i, surface, &supportsPresent[i]);
} }
// Search for a graphics and a present queue in the array of queue // Search for a graphics and a present queue in the array of queue
// families, try to find one that supports both // families, try to find one that supports both TODO(squareys): Move to PhysicalDevice
uint32_t graphicsQueueNodeIndex = UINT32_MAX; uint32_t graphicsQueueNodeIndex = UINT32_MAX;
uint32_t presentQueueNodeIndex = UINT32_MAX; uint32_t presentQueueNodeIndex = UINT32_MAX;
for (uint32_t i = 0; i < queueCount; i++) { for (uint32_t i = 0; i < queueCount; i++) {
@ -96,6 +97,7 @@ Swapchain::Swapchain(Device& device, CommandBuffer& cb, VkSurfaceKHR surface):
} }
} }
} }
if (presentQueueNodeIndex == UINT32_MAX) { if (presentQueueNodeIndex == UINT32_MAX) {
// If there's no queue that supports both present and graphics // If there's no queue that supports both present and graphics
// try to find a separate present queue // try to find a separate present queue
@ -108,24 +110,15 @@ Swapchain::Swapchain(Device& device, CommandBuffer& cb, VkSurfaceKHR surface):
} }
// Exit if either a graphics or a presenting queue hasn't been found // Exit if either a graphics or a presenting queue hasn't been found
if (graphicsQueueNodeIndex == UINT32_MAX || presentQueueNodeIndex == UINT32_MAX) { CORRADE_ASSERT(graphicsQueueNodeIndex != UINT32_MAX, "No graphics queue found.", );
return; // fatal CORRADE_ASSERT(presentQueueNodeIndex != UINT32_MAX, "No present queue found.", );
} CORRADE_ASSERT(graphicsQueueNodeIndex == presentQueueNodeIndex, "Separate graphics and present queues are not supported (yet)", );
// todo : Add support for separate graphics and presenting queue
if (graphicsQueueNodeIndex != presentQueueNodeIndex) {
Error() << "I am unsure about this error in Swapchain.cpp#L" << __LINE__;
return; // fatal
}
// Get list of supported surface formats // Get list of supported surface formats TODO(squareys): Move to PhysicalDevice
UnsignedInt formatCount; UnsignedInt formatCount;
err = vkGetPhysicalDeviceSurfaceFormatsKHR(vkPhysicalDevice, surface, &formatCount, nullptr); err = vkGetPhysicalDeviceSurfaceFormatsKHR(vkPhysicalDevice, surface, &formatCount, nullptr);
MAGNUM_VK_ASSERT_ERROR(err); MAGNUM_VK_ASSERT_ERROR(err);
if(formatCount < 1) { CORRADE_ASSERT(formatCount > 0, "The device does not support any surface formats.", );
Error() << "The device does not support any surface formats."; // TODO: Can this even happen?
return;
}
std::vector<VkSurfaceFormatKHR> surfaceFormats(formatCount); std::vector<VkSurfaceFormatKHR> surfaceFormats(formatCount);
err = vkGetPhysicalDeviceSurfaceFormatsKHR(vkPhysicalDevice, err = vkGetPhysicalDeviceSurfaceFormatsKHR(vkPhysicalDevice,
@ -268,12 +261,7 @@ Swapchain::Swapchain(Device& device, CommandBuffer& cb, VkSurfaceKHR surface):
_buffers[i].view.reset( _buffers[i].view.reset(
new Vk::ImageView(_device, *_buffers[i].image, colorFormat, new Vk::ImageView(_device, *_buffers[i].image, colorFormat,
VK_IMAGE_VIEW_TYPE_2D, ImageAspect::Color, VK_IMAGE_VIEW_TYPE_2D, ImageAspect::Color,
VkComponentMapping{ VkComponentMapping{VK_COMPONENT_SWIZZLE_R, VK_COMPONENT_SWIZZLE_G, VK_COMPONENT_SWIZZLE_B, VK_COMPONENT_SWIZZLE_A}
VK_COMPONENT_SWIZZLE_R,
VK_COMPONENT_SWIZZLE_G,
VK_COMPONENT_SWIZZLE_B,
VK_COMPONENT_SWIZZLE_A
}
) )
); );
} }

Loading…
Cancel
Save