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. 46
      src/Magnum/Vk/Swapchain.cpp

2
src/Magnum/Vk/Instance.h

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

4
src/Magnum/Vk/PhysicalDevice.h

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

53
src/Magnum/Vk/Pipeline.h

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

46
src/Magnum/Vk/Swapchain.cpp

@ -39,8 +39,7 @@ Swapchain::Swapchain(Device& device, CommandBuffer& cb, VkSurfaceKHR surface):
_swapchain{VK_NULL_HANDLE},
_currentIndex{0}
{
VkDevice vkDevice = _device.vkDevice();
VkPhysicalDevice vkPhysicalDevice = _device.physicalDevice().vkPhysicalDevice();
VkDevice vkDevice = _device;
#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)
@ -59,26 +58,28 @@ Swapchain::Swapchain(Device& device, CommandBuffer& cb, VkSurfaceKHR surface):
#undef GET_INSTANCE_PROC_ADDR
#undef GET_DEVICE_PROC_ADDR
VkPhysicalDevice vkPhysicalDevice = _device.physicalDevice();
VkResult err;
// Get available queue family properties
// Get available queue family properties TODO(squareys): Move to PhysicalDevice
uint32_t queueCount;
vkGetPhysicalDeviceQueueFamilyProperties(vkPhysicalDevice,
&queueCount, nullptr);
assert(queueCount >= 1);
// TODO(squareys): Move to PhysicalDevice
std::vector<VkQueueFamilyProperties> queueProps(queueCount);
vkGetPhysicalDeviceQueueFamilyProperties(vkPhysicalDevice,
&queueCount, queueProps.data());
// TODO(squareys): Move to Device/PhysicalDevice instead
std::vector<VkBool32> supportsPresent(queueCount);
for (uint32_t i = 0; i < queueCount; i++) {
vkGetPhysicalDeviceSurfaceSupportKHR(vkPhysicalDevice,
i, surface, &supportsPresent[i]);
vkGetPhysicalDeviceSurfaceSupportKHR(vkPhysicalDevice, i, surface, &supportsPresent[i]);
}
// 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 presentQueueNodeIndex = UINT32_MAX;
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 there's no queue that supports both present and graphics
// 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
if (graphicsQueueNodeIndex == UINT32_MAX || presentQueueNodeIndex == UINT32_MAX) {
return; // fatal
}
// 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
}
CORRADE_ASSERT(graphicsQueueNodeIndex != UINT32_MAX, "No graphics queue found.", );
CORRADE_ASSERT(presentQueueNodeIndex != UINT32_MAX, "No present queue found.", );
CORRADE_ASSERT(graphicsQueueNodeIndex == presentQueueNodeIndex, "Separate graphics and present queues are not supported (yet)", );
// Get list of supported surface formats
// Get list of supported surface formats TODO(squareys): Move to PhysicalDevice
UnsignedInt formatCount;
err = vkGetPhysicalDeviceSurfaceFormatsKHR(vkPhysicalDevice, surface, &formatCount, nullptr);
MAGNUM_VK_ASSERT_ERROR(err);
if(formatCount < 1) {
Error() << "The device does not support any surface formats."; // TODO: Can this even happen?
return;
}
CORRADE_ASSERT(formatCount > 0, "The device does not support any surface formats.", );
std::vector<VkSurfaceFormatKHR> surfaceFormats(formatCount);
err = vkGetPhysicalDeviceSurfaceFormatsKHR(vkPhysicalDevice,
@ -266,15 +259,10 @@ Swapchain::Swapchain(Device& device, CommandBuffer& cb, VkSurfaceKHR surface):
1, &imageMemoryBarrier);
_buffers[i].view.reset(
new Vk::ImageView(_device, *_buffers[i].image, colorFormat,
VK_IMAGE_VIEW_TYPE_2D, ImageAspect::Color,
VkComponentMapping{
VK_COMPONENT_SWIZZLE_R,
VK_COMPONENT_SWIZZLE_G,
VK_COMPONENT_SWIZZLE_B,
VK_COMPONENT_SWIZZLE_A
}
)
new Vk::ImageView(_device, *_buffers[i].image, colorFormat,
VK_IMAGE_VIEW_TYPE_2D, ImageAspect::Color,
VkComponentMapping{VK_COMPONENT_SWIZZLE_R, VK_COMPONENT_SWIZZLE_G, VK_COMPONENT_SWIZZLE_B, VK_COMPONENT_SWIZZLE_A}
)
);
}
}

Loading…
Cancel
Save