helpers.hpp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
#pragma once


#include <felspar/exceptions.hpp>

#include <vulkan/vulkan.h>

#include <cstdint>


namespace planet::vk {


    class device;

Checks that an API has worked

If it has not then throw an exception.

19
20
21
22
23
24
25
26
27
28
29
30
31
    namespace detail {
        std::string error(VkResult);
    }
    inline VkResult
            worked(VkResult result,
                   felspar::source_location const &loc =
                           felspar::source_location::current()) {
        if (result != VK_SUCCESS) {
            throw felspar::stdexcept::runtime_error{detail::error(result), loc};
        } else {
            return result;
        }
    }

Patterns for fetching a vector of data from Vulkan

35
36
37
38
39
40
41
42
43
44
45
46
    template<auto Api, typename Array, typename... A>
    inline std::vector<Array> fetch_vector(A &&...arg) {
        std::uint32_t count{};
        Api(arg..., &count, nullptr);
        std::vector<Array> items;
        items.resize(count);
        Api(std::forward<A>(arg)..., &count, items.data());
        return items;
    }


}