build: translate more functionality to c++17

This commit is contained in:
Danny Robson 2017-09-19 18:22:40 +10:00
parent 3b1019625e
commit 8db4f7f421
9 changed files with 645 additions and 128 deletions

114
fwd.hpp
View File

@ -102,7 +102,115 @@ namespace cruft::vk {
VK_DESCENDANT_TYPE_MAP(FUNC) \
VK_OWNED_TYPE_MAP(FUNC)
#define VK_NATIVE_TYPE_MAP(FUNC) \
#define VK_NATIVE_ENUM_MAP(FUNC) \
MAP0(FUNC, \
VkImageAspectFlagBits, \
VkShaderStageFlagBits, \
VkSystemAllocationScope, \
VkInternalAllocationType, \
VkQueueFlagBits, \
VkStructureType, \
VkFilter, \
VkSamplerMipmapMode, \
VkSamplerAddressMode, \
VkCompareOp, \
VkBorderColor, \
VkIndexType, \
VkBufferCreateFlagBits, \
VkBufferUsageFlagBits, \
VkExternalMemoryHandleTypeFlagBitsKHR, \
VkPrimitiveTopology, \
VkIndirectCommandsTokenTypeNVX, \
VkDisplayPlaneAlphaFlagBitsKHR, \
VkSurfaceTransformFlagBitsKHR, \
VkCompositeAlphaFlagBitsKHR, \
VkImageUsageFlagBits, \
VkViewportCoordinateSwizzleNV, \
VkPipelineBindPoint, \
VkExternalMemoryFeatureFlagBitsNV, \
VkDebugReportFlagBitsEXT, \
VkDebugReportObjectTypeEXT, \
VkCommandBufferResetFlagBits, \
VkPhysicalDeviceType, \
VkFenceImportFlagBitsKHR, \
VkExternalFenceHandleTypeFlagBitsKHR, \
VkQueryType, \
VkQueryPipelineStatisticFlagBits, \
VkExternalSemaphoreHandleTypeFlagBitsKHR, \
VkImageLayout, \
VkFormatFeatureFlagBits, \
VkObjectEntryTypeNVX, \
VkObjectEntryUsageFlagBitsNVX, \
VkDescriptorType, \
VkSparseImageFormatFlagBits, \
VkComponentSwizzle, \
VkDescriptorSetLayoutCreateFlagBits, \
VkIndirectCommandsLayoutUsageFlagBitsNVX, \
VkAttachmentDescriptionFlagBits, \
VkSampleCountFlagBits, \
VkAttachmentLoadOp, \
VkAttachmentStoreOp, \
VkImageType, \
VkImageTiling, \
VkExternalMemoryFeatureFlagBitsKHR, \
VkStencilFaceFlagBits, \
VkStencilOp, \
VkPolygonMode, \
VkCullModeFlagBits, \
VkFrontFace, \
VkImageCreateFlagBits, \
VkExternalMemoryHandleTypeFlagBitsNV, \
VkImageViewType, \
VkSharingMode, \
VkDynamicState, \
VkCommandPoolCreateFlagBits, \
VkSemaphoreImportFlagBitsKHR, \
VkPipelineStageFlagBits, \
VkSwapchainCreateFlagBitsKHR, \
VkMemoryHeapFlagBits, \
VkPipelineCreateFlagBits, \
VkBlendOverlapEXT, \
VkSurfaceCounterFlagBitsEXT, \
VkDescriptorUpdateTemplateTypeKHR, \
VkExternalFenceFeatureFlagBitsKHR, \
VkQueryResultFlagBits, \
VkSparseMemoryBindFlagBits, \
VkValidationCacheHeaderVersionEXT, \
VkDeviceGroupPresentModeFlagBitsKHX, \
VkColorSpaceKHR, \
VkPresentModeKHR, \
VkCommandPoolResetFlagBits, \
VkVertexInputRate, \
VkSubpassDescriptionFlagBits, \
VkAccessFlagBits, \
VkDependencyFlagBits, \
VkExternalSemaphoreFeatureFlagBitsKHR, \
VkBlendFactor, \
VkBlendOp, \
VkColorComponentFlagBits, \
VkMemoryPropertyFlagBits, \
VkCommandBufferUsageFlagBits, \
VkQueryControlFlagBits, \
VkCommandBufferLevel, \
VkValidationCheckEXT, \
VkSubpassContents, \
VkLogicOp, \
VkDisplayPowerStateEXT, \
VkFenceCreateFlagBits, \
VkDisplayEventTypeEXT, \
VkDescriptorPoolCreateFlagBits, \
VkPeerMemoryFeatureFlagBitsKHX, \
VkCoverageModulationModeNV, \
VkObjectType, \
VkMemoryAllocateFlagBitsKHX, \
VkPipelineCacheHeaderVersion, \
VkDiscardRectangleModeEXT, \
VkDeviceEventTypeEXT, \
VkSamplerReductionModeEXT, \
VkRasterizationOrderAMD \
)
#define VK_NATIVE_STRUCT_MAP(FUNC) \
MAP0(FUNC, \
VkClearColorValue, \
VkClearValue, \
@ -379,6 +487,10 @@ namespace cruft::vk {
VkValidationCacheCreateInfoEXT, \
VkShaderModuleValidationCacheCreateInfoEXT)
#define VK_NATIVE_TYPE_MAP(FUNC) \
VK_NATIVE_ENUM_MAP(FUNC) \
VK_NATIVE_STRUCT_MAP(FUNC)
}
#endif

View File

@ -190,6 +190,10 @@ namespace cruft::vk {
SelfT& operator* ()& noexcept
{ return m_self; }
const SelfT*
operator-> () const& noexcept
{ return &m_self; }
SelfT*
operator-> ()& noexcept
{ return &m_self; }
@ -229,6 +233,65 @@ namespace cruft::vk {
owner
};
};
///////////////////////////////////////////////////////////////////////////
template <typename ReturnT, typename ...Args>
struct function_wrapper {
using value_type = ReturnT(*)(Args...) noexcept;
constexpr function_wrapper (value_type _value): m_function {_value} { ; }
constexpr function_wrapper (): m_function { nullptr } { ; }
value_type
reset (value_type _value)
{
value_type old = std::move (m_function);
m_function = _value;
return old;
}
template <typename SelfT, typename ...Tail>
auto
operator () (const owned_ptr<SelfT> &owned, Tail &&...tail) const
{
return dispatch (owned.owner (), owned.get (), std::forward<Tail> (tail)...);
}
template <typename ...Tail>
auto operator () (Tail &&...tail) const
{
return dispatch (std::forward<Tail> (tail)...);
}
private:
template <typename ...Tail>
auto dispatch (Tail &&...tail) const
{
if constexpr (is_values_function_v<value_type>)
return error::try_values (m_function, std::forward<Tail> (tail)...);
else if constexpr (is_query_function_v<value_type>)
return error::try_query (m_function, std::forward<Tail> (tail)...);
else
return error::try_func (m_function, std::forward<Tail> (tail)...);
}
value_type m_function;
};
#define WRAP_FUNCTION(NAME,FUNC) \
namespace detail { const function_wrapper __##NAME {FUNC}; } \
template <typename ...Args> \
auto NAME (Args &&...args) \
{ \
return detail::__##NAME (std::forward<Args> (args)... ); \
};
};
#endif

View File

@ -58,16 +58,6 @@ physical_device::features (void) const
}
//-----------------------------------------------------------------------------
VkSurfaceCapabilitiesKHR
physical_device::surface_capabilities (surface &_surface) const
{
return error::try_query (
vkGetPhysicalDeviceSurfaceCapabilitiesKHR, native (), _surface
);
}
//-----------------------------------------------------------------------------
VkPhysicalDeviceMemoryProperties
physical_device::memory_properties (void) const
@ -88,28 +78,6 @@ physical_device::queue_families (void) const
}
///////////////////////////////////////////////////////////////////////////////
std::vector<VkSurfaceFormatKHR>
physical_device::surface_formats (surface &_surface)
{
return error::try_values (
vkGetPhysicalDeviceSurfaceFormatsKHR,
native (), _surface
);
}
//-----------------------------------------------------------------------------
std::vector<VkPresentModeKHR>
physical_device::present_modes (surface &_surface)
{
return error::try_values (
vkGetPhysicalDeviceSurfacePresentModesKHR,
native (), _surface
);
}
///////////////////////////////////////////////////////////////////////////////
std::string
physical_device::name (void) const

View File

@ -33,14 +33,10 @@ namespace cruft::vk {
VkPhysicalDeviceProperties properties (void) const;
VkPhysicalDeviceFeatures features (void) const;
VkSurfaceCapabilitiesKHR surface_capabilities (surface&) const;
VkPhysicalDeviceMemoryProperties memory_properties (void) const;
std::vector<VkQueueFamilyProperties> queue_families (void) const;
std::vector<VkSurfaceFormatKHR> surface_formats (surface&);
std::vector<VkPresentModeKHR> present_modes (surface&);
std::string name (void) const;
};
}

View File

@ -18,6 +18,6 @@
#include "./surface.hpp"
#include "./instance.hpp"
#include "./physical_device.hpp"
using cruft::vk::surface;

View File

@ -24,9 +24,16 @@
namespace cruft::vk {
///////////////////////////////////////////////////////////////////////////
struct surface : public owned<surface,instance> {
using owned::owned;
};
///////////////////////////////////////////////////////////////////////////
WRAP_FUNCTION (capabilities, vkGetPhysicalDeviceSurfaceCapabilitiesKHR);
WRAP_FUNCTION (formats, vkGetPhysicalDeviceSurfaceFormatsKHR);
WRAP_FUNCTION (present_modes, vkGetPhysicalDeviceSurfacePresentModesKHR);
};
#endif

View File

@ -24,6 +24,9 @@ namespace cruft::vk {
struct swapchain : public owned<swapchain,device> {
using owned::owned;
};
function_wrapper<decltype(&vkGetSwapchainImagesKHR)> images;
};
#endif

View File

@ -19,11 +19,14 @@
#include <GLFW/glfw3.h>
#include <GLFW/glfw3native.h>
#include <cruft/util/extent.hpp>
#include <cruft/util/io.hpp>
#include <cruft/util/log.hpp>
#include <cruft/util/colour.hpp>
#include <cruft/util/point.hpp>
#include <atomic>
#include <mutex>
#include <iostream>
#include <experimental/filesystem>
@ -69,27 +72,237 @@ static constexpr vertex_t VERTICES[] = {
};
namespace glfw {
namespace detail {
constexpr const char* error_string (int code) noexcept;
}
struct error : public std::exception {
public:
static void try_code (int code);
static void throw_code [[noreturn]] (int code);
static void throw_code [[noreturn]] (void);
static void push (int code)
{
s_current = code;
}
private:
static std::atomic<int> s_current;
};
template <int CodeV>
struct error_code : public error {
virtual ~error_code () = default;
static constexpr const char *message = detail::error_string (CodeV);
};
}
std::atomic<int> glfw::error::s_current;
void
glfw_error_callback (int code ,const char *description)
{
LOG_WARN ("glfw: %s", description);
glfw::error::push (code);
}
namespace glfw {
class instance {
public:
instance ()
{
if (std::lock_guard l {s_count.guard}; !s_count.value++)
glfwInit ();
glfwSetErrorCallback (glfw_error_callback);
static constexpr struct {
int tag;
int val;
} HINTS[] {
{ GLFW_CLIENT_API, GLFW_NO_API },
{ GLFW_RESIZABLE, GLFW_FALSE },
};
for (const auto [tag,val]: HINTS)
glfwWindowHint (tag, val);
}
std::vector<const char*>
required_extensions (void) const
{
unsigned count;
auto values = glfwGetRequiredInstanceExtensions (&count);
return {
values + 0,
values + count
};
}
void poll_events (void)
{
glfwPollEvents ();
}
~instance ()
{
if (std::lock_guard l {s_count.guard}; !--s_count.value)
glfwTerminate ();
}
private:
// TODO: value doesnt' need to be atomic, but it's not performance
// critical, and I'm not particularly familiar with c++ memory
// ordering primitives. so it's probably safer this way for now.
static struct count_t {
std::mutex guard;
std::atomic<int> value;
} s_count;
};
class window {
public:
using native_t = GLFWwindow*;
window (util::extent2i resolution, const char *name):
m_native (glfwCreateWindow (resolution.w, resolution.h, name, nullptr, nullptr))
{
if (!m_native)
error::throw_code ();
}
native_t native (void) const { return m_native; }
bool
should_close (void) const
{
return glfwWindowShouldClose (native ());
}
private:
native_t m_native;
};
};
///////////////////////////////////////////////////////////////////////////////
template <typename T>
struct to_format;
#define TO_FORMAT(S,T,VALUE) \
template <template <size_t,typename> class KlassT> \
struct to_format<KlassT<S,T>> { \
static constexpr auto value = VkFormat::VALUE; \
};
TO_FORMAT(1,float,VK_FORMAT_R32_SFLOAT);
TO_FORMAT(2,float,VK_FORMAT_R32G32_SFLOAT);
TO_FORMAT(3,float,VK_FORMAT_R32G32B32_SFLOAT);
TO_FORMAT(4,float,VK_FORMAT_R32G32B32A32_SFLOAT);
template <typename T>
constexpr auto to_format_v = to_format<T>::value;
///////////////////////////////////////////////////////////////////////////////
struct glfw::instance::count_t glfw::instance::s_count;
///////////////////////////////////////////////////////////////////////////////
constexpr const char*
glfw::detail::error_string (int code) noexcept
{
switch (code) {
case GLFW_NOT_INITIALIZED: return "Not initialised";
case GLFW_NO_CURRENT_CONTEXT: return "No current context";
case GLFW_INVALID_ENUM: return "Invalid enum";
case GLFW_INVALID_VALUE: return "Invalid value";
case GLFW_OUT_OF_MEMORY: return "Out of memory";
case GLFW_API_UNAVAILABLE: return "API unavailable";
case GLFW_VERSION_UNAVAILABLE: return "Version unavailable";
case GLFW_PLATFORM_ERROR: return "Platform error";
case GLFW_FORMAT_UNAVAILABLE: return "Format unavailable";
case GLFW_NO_WINDOW_CONTEXT: return "No window context";
}
unhandled (code);
}
//-----------------------------------------------------------------------------
void
glfw::error::try_code (int code)
{
if (__builtin_expect (code, 0))
throw_code (code);
}
//-----------------------------------------------------------------------------
void
glfw::error::throw_code [[noreturn]] (int code)
{
switch (code) {
case GLFW_NOT_INITIALIZED: throw error_code<GLFW_NOT_INITIALIZED> {};
case GLFW_NO_CURRENT_CONTEXT: throw error_code<GLFW_NO_CURRENT_CONTEXT> {};
case GLFW_INVALID_ENUM: throw error_code<GLFW_INVALID_ENUM> {};
case GLFW_INVALID_VALUE: throw error_code<GLFW_INVALID_VALUE> {};
case GLFW_OUT_OF_MEMORY: throw error_code<GLFW_OUT_OF_MEMORY> {};
case GLFW_API_UNAVAILABLE: throw error_code<GLFW_API_UNAVAILABLE> {};
case GLFW_VERSION_UNAVAILABLE: throw error_code<GLFW_VERSION_UNAVAILABLE> {};
case GLFW_PLATFORM_ERROR: throw error_code<GLFW_PLATFORM_ERROR> {};
case GLFW_FORMAT_UNAVAILABLE: throw error_code<GLFW_FORMAT_UNAVAILABLE> {};
case GLFW_NO_WINDOW_CONTEXT: throw error_code<GLFW_NO_WINDOW_CONTEXT> {};
}
unhandled (code);
}
//-----------------------------------------------------------------------------
void
glfw::error::throw_code [[noreturn]] (void)
{
throw_code (s_current);
}
///////////////////////////////////////////////////////////////////////////////
class system {
public:
system ();
};
///////////////////////////////////////////////////////////////////////////////
int
main (void)
{
LOG_INFO ("intialising glfw");
glfwInit ();
glfw::instance glfw_instance;
LOG_INFO ("intialising window");
glfwWindowHint (GLFW_CLIENT_API, GLFW_NO_API);
glfwWindowHint (GLFW_RESIZABLE, GLFW_FALSE);
util::extent2i resolution { 800, 600 };
glfw::window window (resolution, "vkcruft-hello");
int resolution_x = 800, resolution_y = 600;
auto window = glfwCreateWindow (resolution_x, resolution_y, "vkcruft-hello", nullptr, nullptr);
if (!window) {
LOG_ERROR("unable to initialised GLFW");
return -1;
}
unsigned int glfwExtensionCount;
auto glfwExtensions = glfwGetRequiredInstanceExtensions (&glfwExtensionCount);
std::vector<const char*> extensions (glfwExtensions + 0, glfwExtensions + glfwExtensionCount);
auto extensions = glfw_instance.required_extensions ();
extensions.push_back ("VK_EXT_debug_report");
const char* layers[] = {
@ -130,14 +343,14 @@ main (void)
//-------------------------------------------------------------------------
auto surface = cruft::vk::make_owned<cruft::vk::surface> (
cruft::vk::error::try_query (
glfwCreateWindowSurface, instance.native (), window, nullptr
glfwCreateWindowSurface, instance.native (), window.native (), nullptr
),
instance
);
auto surface_capabilities = pdevice.surface_capabilities (surface.get ());
auto surface_formats = pdevice.surface_formats (surface.get ());
auto present_modes = pdevice.present_modes (surface.get ());
auto surface_capabilities = capabilities (pdevice, surface);
auto surface_formats = formats (pdevice, surface);
auto surface_modes = present_modes (pdevice, surface);
auto queues = pdevice.queue_families ();
int graphics_queue_id = -1, present_queue_id = -1;
@ -212,12 +425,12 @@ main (void)
VkPresentModeKHR present_mode = VK_PRESENT_MODE_FIFO_KHR;
VkExtent2D present_extent {
.width = util::limit (
unsigned (resolution_x),
unsigned (resolution.w),
surface_capabilities.minImageExtent.width,
surface_capabilities.maxImageExtent.width
),
.height = util::limit (
unsigned (resolution_y),
unsigned (resolution.h),
surface_capabilities.minImageExtent.height,
surface_capabilities.maxImageExtent.height
)
@ -285,9 +498,13 @@ main (void)
auto graphics_queue = cruft::vk::make_owned<cruft::vk::queue> (ldevice, graphics_queue_id, 0);
auto present_queue = cruft::vk::make_owned<cruft::vk::queue> (ldevice, present_queue_id, 0);
//-------------------------------------------------------------------------
auto vert_module = cruft::vk::make_owned<cruft::vk::shader_module> (ldevice, "./hello.vert.spv");
auto frag_module = cruft::vk::make_owned<cruft::vk::shader_module> (ldevice, "./hello.frag.spv");
//-------------------------------------------------------------------------
VkBufferCreateInfo buffer_info {};
buffer_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
buffer_info.size = sizeof (VERTICES);
@ -328,6 +545,7 @@ main (void)
memcpy (std::data (data), std::data (VERTICES), sizeof (VERTICES));
}
//-------------------------------------------------------------------------
VkPipelineShaderStageCreateInfo vert_stage_info {};
vert_stage_info.sType = cruft::vk::structure_type_v<VkPipelineShaderStageCreateInfo>;
vert_stage_info.stage = VK_SHADER_STAGE_VERTEX_BIT;
@ -342,6 +560,8 @@ main (void)
vert_stage_info, frag_stage_info
};
//-------------------------------------------------------------------------
VkVertexInputBindingDescription vertex_binding {};
vertex_binding.binding = 0;
vertex_binding.stride = sizeof (vertex_t);
@ -350,14 +570,16 @@ main (void)
std::array<VkVertexInputAttributeDescription,2> vertex_attribute {};
vertex_attribute[0].binding = 0;
vertex_attribute[0].location = 0;
vertex_attribute[0].format = VK_FORMAT_R32G32_SFLOAT;
vertex_attribute[0].format = to_format_v<decltype(VERTICES[0].position)>;
vertex_attribute[0].offset = offsetof(vertex_t, position);
vertex_attribute[1].binding = 0;
vertex_attribute[1].location = 1;
vertex_attribute[1].format = VK_FORMAT_R32G32B32_SFLOAT;
vertex_attribute[1].format = to_format_v<decltype(VERTICES[0].colour)>;
vertex_attribute[1].offset = offsetof (vertex_t, colour);
//-------------------------------------------------------------------------
VkPipelineVertexInputStateCreateInfo vertex_input {};
vertex_input.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
vertex_input.vertexBindingDescriptionCount = 1;
@ -370,6 +592,8 @@ main (void)
assembly_info.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
assembly_info.primitiveRestartEnable = VK_FALSE;
//-------------------------------------------------------------------------
VkViewport viewport {
.x = 0.f,
.y = 0.f,
@ -384,67 +608,21 @@ main (void)
.extent = present_extent
};
VkPipelineViewportStateCreateInfo viewport_create_info {};
viewport_create_info.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
viewport_create_info.viewportCount = 1;
viewport_create_info.pViewports = &viewport;
viewport_create_info.scissorCount = 1;
viewport_create_info.pScissors = &scissor;
VkPipelineRasterizationStateCreateInfo rasterizer {};
rasterizer.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
rasterizer.depthClampEnable = VK_FALSE;
rasterizer.rasterizerDiscardEnable = VK_FALSE;
rasterizer.polygonMode = VK_POLYGON_MODE_FILL;
rasterizer.lineWidth = 1.f;
rasterizer.cullMode = VK_CULL_MODE_BACK_BIT;
rasterizer.frontFace = VK_FRONT_FACE_CLOCKWISE;
rasterizer.depthBiasEnable = VK_FALSE;
rasterizer.depthBiasConstantFactor = 0.f;
rasterizer.depthBiasClamp = 0.f;
rasterizer.depthBiasSlopeFactor = 0.f;
VkPipelineMultisampleStateCreateInfo multisampling {};
multisampling.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
multisampling.sampleShadingEnable = VK_FALSE;
multisampling.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
multisampling.minSampleShading = 1.0f;
multisampling.pSampleMask = nullptr;
multisampling.alphaToCoverageEnable = VK_FALSE;
multisampling.alphaToOneEnable = VK_FALSE;
VkPipelineColorBlendAttachmentState blend_attachment = {};
blend_attachment.colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT;
blend_attachment.blendEnable = VK_FALSE;
blend_attachment.srcColorBlendFactor = VK_BLEND_FACTOR_ONE; // Optional
blend_attachment.dstColorBlendFactor = VK_BLEND_FACTOR_ZERO; // Optional
blend_attachment.colorBlendOp = VK_BLEND_OP_ADD; // Optional
blend_attachment.srcAlphaBlendFactor = VK_BLEND_FACTOR_ONE; // Optional
blend_attachment.dstAlphaBlendFactor = VK_BLEND_FACTOR_ZERO; // Optional
blend_attachment.alphaBlendOp = VK_BLEND_OP_ADD; // Optional
VkPipelineColorBlendStateCreateInfo colour_blend = {};
colour_blend.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
colour_blend.logicOpEnable = VK_FALSE;
colour_blend.logicOp = VK_LOGIC_OP_COPY; // Optional
colour_blend.attachmentCount = 1;
colour_blend.pAttachments = &blend_attachment;
colour_blend.blendConstants[0] = 0.0f; // Optional
colour_blend.blendConstants[1] = 0.0f; // Optional
colour_blend.blendConstants[2] = 0.0f; // Optional
colour_blend.blendConstants[3] = 0.0f; // Optional
//-------------------------------------------------------------------------
VkPipelineLayoutCreateInfo pipeline_layout_info {};
pipeline_layout_info.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
pipeline_layout_info.setLayoutCount = 0; // Optional
pipeline_layout_info.pSetLayouts = nullptr; // Optional
pipeline_layout_info.pushConstantRangeCount = 0; // Optional
pipeline_layout_info.pPushConstantRanges = 0; // Optional
pipeline_layout_info.setLayoutCount = 0;
pipeline_layout_info.pSetLayouts = nullptr;
pipeline_layout_info.pushConstantRangeCount = 0;
pipeline_layout_info.pPushConstantRanges = 0;
auto pipeline_layout = cruft::vk::make_owned<cruft::vk::pipeline_layout> (
ldevice, &pipeline_layout_info, nullptr
);
//-------------------------------------------------------------------------
VkAttachmentDescription color_attachment {};
color_attachment.format = surface_format.format;
color_attachment.samples = VK_SAMPLE_COUNT_1_BIT;
@ -475,6 +653,62 @@ main (void)
ldevice, &render_pass_info, nullptr
);
//-------------------------------------------------------------------------
VkPipelineViewportStateCreateInfo viewport_create_info {};
viewport_create_info.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
viewport_create_info.viewportCount = 1;
viewport_create_info.pViewports = &viewport;
viewport_create_info.scissorCount = 1;
viewport_create_info.pScissors = &scissor;
VkPipelineRasterizationStateCreateInfo rasterizer {};
rasterizer.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
rasterizer.depthClampEnable = VK_FALSE;
rasterizer.rasterizerDiscardEnable = VK_FALSE;
rasterizer.polygonMode = VK_POLYGON_MODE_FILL;
rasterizer.lineWidth = 1.f;
rasterizer.cullMode = VK_CULL_MODE_BACK_BIT;
rasterizer.frontFace = VK_FRONT_FACE_CLOCKWISE;
rasterizer.depthBiasEnable = VK_FALSE;
rasterizer.depthBiasConstantFactor = 0.f;
rasterizer.depthBiasClamp = 0.f;
rasterizer.depthBiasSlopeFactor = 0.f;
VkPipelineMultisampleStateCreateInfo multisampling {};
multisampling.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
multisampling.sampleShadingEnable = VK_FALSE;
multisampling.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
multisampling.minSampleShading = 1.0f;
multisampling.pSampleMask = nullptr;
multisampling.alphaToCoverageEnable = VK_FALSE;
multisampling.alphaToOneEnable = VK_FALSE;
VkPipelineColorBlendAttachmentState blend_attachment = {};
blend_attachment.colorWriteMask =
VK_COLOR_COMPONENT_R_BIT |
VK_COLOR_COMPONENT_G_BIT |
VK_COLOR_COMPONENT_B_BIT |
VK_COLOR_COMPONENT_A_BIT;
blend_attachment.blendEnable = VK_FALSE;
blend_attachment.srcColorBlendFactor = VK_BLEND_FACTOR_ONE;
blend_attachment.dstColorBlendFactor = VK_BLEND_FACTOR_ZERO;
blend_attachment.colorBlendOp = VK_BLEND_OP_ADD;
blend_attachment.srcAlphaBlendFactor = VK_BLEND_FACTOR_ONE;
blend_attachment.dstAlphaBlendFactor = VK_BLEND_FACTOR_ZERO;
blend_attachment.alphaBlendOp = VK_BLEND_OP_ADD;
VkPipelineColorBlendStateCreateInfo colour_blend = {};
colour_blend.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
colour_blend.logicOpEnable = VK_FALSE;
colour_blend.logicOp = VK_LOGIC_OP_COPY;
colour_blend.attachmentCount = 1;
colour_blend.pAttachments = &blend_attachment;
colour_blend.blendConstants[0] = 0.0f;
colour_blend.blendConstants[1] = 0.0f;
colour_blend.blendConstants[2] = 0.0f;
colour_blend.blendConstants[3] = 0.0f;
VkGraphicsPipelineCreateInfo pipeline_info {};
pipeline_info.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
pipeline_info.stageCount = 2;
@ -492,17 +726,23 @@ main (void)
pipeline_info.basePipelineHandle = VK_NULL_HANDLE;
pipeline_info.basePipelineIndex = -1;
auto graphics_pipeline = cruft::vk::make_owned<cruft::vk::pipeline<cruft::vk::bindpoint::GRAPHICS>> (
auto graphics_pipeline = cruft::vk::make_owned<
cruft::vk::pipeline<cruft::vk::bindpoint::GRAPHICS>
> (
ldevice, VK_NULL_HANDLE, 1, &pipeline_info, nullptr
);
//-------------------------------------------------------------------------
using framebuffer_ptr = cruft::vk::owned_ptr<cruft::vk::framebuffer>;
std::vector<framebuffer_ptr> swapchain_framebuffers;
swapchain_framebuffers.reserve (swap_image_views.size ());
for (size_t i = 0; i < swap_image_views.size (); ++i) {
std::transform (std::cbegin (swap_image_views),
std::cend (swap_image_views),
std::back_inserter (swapchain_framebuffers),
[&] (const auto &i) {
VkImageView attachments[] = {
swap_image_views[i]->native ()
i->native ()
};
VkFramebufferCreateInfo framebuffer_info {};
@ -514,11 +754,11 @@ main (void)
framebuffer_info.height = present_extent.height;
framebuffer_info.layers = 1;
swapchain_framebuffers.push_back (
cruft::vk::make_owned<cruft::vk::framebuffer> (ldevice, &framebuffer_info, nullptr)
);
};
return cruft::vk::make_owned<cruft::vk::framebuffer> (ldevice, &framebuffer_info, nullptr);
});
//-------------------------------------------------------------------------
VkCommandPoolCreateInfo pool_info {};
pool_info.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
pool_info.queueFamilyIndex = device_queues[0].queueFamilyIndex;
@ -576,8 +816,13 @@ main (void)
VkSemaphoreCreateInfo semaphore_info {};
semaphore_info.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
auto image_semaphore = cruft::vk::make_owned<cruft::vk::semaphore> (ldevice, &semaphore_info, nullptr);
auto render_semaphore = cruft::vk::make_owned<cruft::vk::semaphore> (ldevice, &semaphore_info, nullptr);
auto image_semaphore = cruft::vk::make_owned<cruft::vk::semaphore> (
ldevice, &semaphore_info, nullptr
);
auto render_semaphore = cruft::vk::make_owned<cruft::vk::semaphore> (
ldevice, &semaphore_info, nullptr
);
uint32_t image_index;
cruft::vk::error::try_code (
@ -630,13 +875,11 @@ main (void)
cruft::vk::error::try_func (vkQueuePresentKHR, present_queue, &present_info);
LOG_INFO ("entering runloop");
while (!glfwWindowShouldClose (window)) {
glfwPollEvents ();
while (!window.should_close ()) {
glfw_instance.poll_events ();
}
cruft::vk::error::try_func (vkDeviceWaitIdle, ldevice.native ());
LOG_INFO ("terminating glfw");
glfwDestroyWindow (window);
glfwTerminate ();
LOG_INFO ("terminating");
}

View File

@ -124,6 +124,131 @@ namespace cruft::vk {
static constexpr auto is_wrapper_v = is_wrapper<T>::value;
///////////////////////////////////////////////////////////////////////////
/// determines whether a function conceptually returns a singular Vulkan
/// data structure.
///
/// that is: is the last parameter a pointer to a Vulkan data type?
///
/// a result of true probably implies the function type can be passed to
/// error::try_query
template <typename FunctionT>
struct is_query_function : public std::false_type {};
//-------------------------------------------------------------------------
namespace detail {
template <typename ...Args>
struct is_query_args : public std::conditional_t<
std::is_pointer_v<
std::tuple_element_t<
sizeof...(Args) - 1,
std::tuple<Args...>
>
> && is_native_v<
std::remove_pointer_t<
std::tuple_element_t<
sizeof...(Args) - 1,
std::tuple<Args...>
>
>
>,
std::true_type,
std::false_type
> { };
}
//-------------------------------------------------------------------------
template <typename ReturnT, typename ...Args>
struct is_query_function<
ReturnT(Args...)noexcept
> : detail::is_query_args<Args...> { };
template <typename ReturnT, typename ...Args>
struct is_query_function<
ReturnT(*)(Args...)noexcept
> : detail::is_query_args<Args...> { };
template <typename ReturnT, typename ...Args>
struct is_query_function<
ReturnT(&)(Args...)noexcept
> : detail::is_query_args<Args...> { };
//-------------------------------------------------------------------------
template <typename FunctionT>
constexpr auto is_query_function_v = is_query_function<FunctionT>::value;
///////////////////////////////////////////////////////////////////////////
/// determines whether a function conceptually returns an array of values.
///
/// that is: does it take/return uint32_t and Vulkan types as the last two
/// pointer parameters.
///
/// a result of true likely implies such a function pointer can be passed
/// to error::try_values
template <typename FunctionT>
struct is_values_function : public std::false_type {};
//-------------------------------------------------------------------------
namespace detail {
template <typename ...Args>
struct is_values_args : public std::conditional_t<
std::is_same_v<
uint32_t*,
std::tuple_element_t<
sizeof...(Args) - 2,
std::tuple<Args...>
>
> && std::is_pointer_v<
std::tuple_element_t<
sizeof...(Args) - 1,
std::tuple<Args...>
>
> && is_native_v<
std::remove_pointer_t<
std::tuple_element_t<
sizeof...(Args) - 1,
std::tuple<Args...>
>
>
>,
std::true_type,
std::false_type
> { };
};
//-------------------------------------------------------------------------
template <typename ReturnT, typename ...Args>
struct is_values_function<
ReturnT(Args...) noexcept
> : detail::is_values_args<Args...> { };
//-------------------------------------------------------------------------
template <typename ReturnT, typename ...Args>
struct is_values_function<
ReturnT(&)(Args...) noexcept
> : detail::is_values_args<Args...> { };
//-------------------------------------------------------------------------
template <typename ReturnT, typename ...Args>
struct is_values_function<
ReturnT(*)(Args...) noexcept
> : detail::is_values_args<Args...> { };
//-------------------------------------------------------------------------
template <typename FunctionT>
constexpr auto is_values_function_v = is_values_function<FunctionT>::value;
///////////////////////////////////////////////////////////////////////////
/// return the native value for the wrapper object
template <typename SelfT>