device/image.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | /// @file image.h | ||
| 3 | /// @brief Device images and samplers, described in Ernie's terms. | ||
| 4 | #pragma once | ||
| 5 | |||
| 6 | #include "ernie/device/context.h" | ||
| 7 | |||
| 8 | #include <glm/vec3.hpp> | ||
| 9 | |||
| 10 | #include <cstdint> | ||
| 11 | #include <string_view> | ||
| 12 | |||
| 13 | namespace ernie { | ||
| 14 | |||
| 15 | /// @brief The formats Ernie stores images in. | ||
| 16 | enum class ImageFormat : std::uint8_t { | ||
| 17 | Rgba8Unorm, | ||
| 18 | Rgba8Srgb, | ||
| 19 | R32Float, | ||
| 20 | Rg32Float, | ||
| 21 | Rgba16Float, | ||
| 22 | Rgba32Float, | ||
| 23 | R32Uint, | ||
| 24 | }; | ||
| 25 | |||
| 26 | /// @return The bytes of one texel. | ||
| 27 | std::uint32_t TexelBytes(ImageFormat format); | ||
| 28 | |||
| 29 | /// @brief How an image is used, as flags. | ||
| 30 | enum class ImageUsage : std::uint8_t { | ||
| 31 | Sampled = 1u << 0, | ||
| 32 | Storage = 1u << 1, | ||
| 33 | Attachment = 1u << 2, | ||
| 34 | }; | ||
| 35 | 871 | constexpr ImageUsage operator|(ImageUsage a, ImageUsage b) { | |
| 36 | 871 | return static_cast<ImageUsage>(static_cast<std::uint8_t>(a) | static_cast<std::uint8_t>(b)); | |
| 37 | } | ||
| 38 | 45 | constexpr bool Any(ImageUsage usage, ImageUsage flags) { | |
| 39 | 40 | return (static_cast<std::uint8_t>(usage) & static_cast<std::uint8_t>(flags)) != 0; | |
| 40 | } | ||
| 41 | |||
| 42 | /// @brief An image: 2D, or 3D when its extent has depth. | ||
| 43 | struct ImageDescription { | ||
| 44 | 2 | ImageFormat format = ImageFormat::Rgba8Unorm; | |
| 45 | 2 | glm::uvec3 extent{1, 1, 1}; | |
| 46 | 2 | std::uint32_t mips = 1; | |
| 47 | 2 | ImageUsage usage = ImageUsage::Sampled; | |
| 48 | 4 | bool operator==(const ImageDescription&) const = default; | |
| 49 | }; | ||
| 50 | |||
| 51 | /// @brief An image, its memory and a view of it all. Images stay in the general layout, which | ||
| 52 | /// every use accepts; the frame graph will choose layouts per pass. Destroying one the GPU may | ||
| 53 | /// still use is an error: retire it instead. | ||
| 54 | class DeviceImage { | ||
| 55 | public: | ||
| 56 | /// @throws DeviceError when the memory cannot be allocated or the format is unsupported. | ||
| 57 | DeviceImage(DeviceContext& context, const ImageDescription& description, std::string_view name); | ||
| 58 | ~DeviceImage(); | ||
| 59 | DeviceImage(const DeviceImage&) = delete; | ||
| 60 | DeviceImage& operator=(const DeviceImage&) = delete; | ||
| 61 | |||
| 62 | 2 | const ImageDescription& Description() const { | |
| 63 | 2 | return description_; | |
| 64 | } | ||
| 65 | 52 | VkImage Handle() const { | |
| 66 | 52 | return image_; | |
| 67 | } | ||
| 68 | 24 | VkImageView View() const { | |
| 69 | 24 | return view_; | |
| 70 | } | ||
| 71 | /// @return The ticket of the transition into the general layout, which first uses wait on. | ||
| 72 | 25 | Ticket Ready() const { | |
| 73 | 25 | return ready_; | |
| 74 | } | ||
| 75 | |||
| 76 | private: | ||
| 77 | DeviceContext& context_; | ||
| 78 | ImageDescription description_; | ||
| 79 | VkImage image_ = VK_NULL_HANDLE; | ||
| 80 | VmaAllocation allocation_ = VK_NULL_HANDLE; | ||
| 81 | VkImageView view_ = VK_NULL_HANDLE; | ||
| 82 | Ticket ready_; | ||
| 83 | }; | ||
| 84 | |||
| 85 | enum class Filter : std::uint8_t { Nearest, Linear }; | ||
| 86 | enum class Address : std::uint8_t { Repeat, Clamp, Mirror }; | ||
| 87 | |||
| 88 | struct SamplerDescription { | ||
| 89 | Filter filter = Filter::Linear; | ||
| 90 | Address address = Address::Repeat; | ||
| 91 | bool operator==(const SamplerDescription&) const = default; | ||
| 92 | }; | ||
| 93 | |||
| 94 | /// @brief A sampler. | ||
| 95 | class Sampler { | ||
| 96 | public: | ||
| 97 | Sampler(DeviceContext& context, const SamplerDescription& description, std::string_view name); | ||
| 98 | ~Sampler(); | ||
| 99 | Sampler(const Sampler&) = delete; | ||
| 100 | Sampler& operator=(const Sampler&) = delete; | ||
| 101 | |||
| 102 | 4 | VkSampler Handle() const { | |
| 103 | 5 | return sampler_; | |
| 104 | } | ||
| 105 | |||
| 106 | private: | ||
| 107 | VkDevice device_; | ||
| 108 | VkSampler sampler_ = VK_NULL_HANDLE; | ||
| 109 | }; | ||
| 110 | |||
| 111 | /// @return The Vulkan format of an Ernie format. | ||
| 112 | VkFormat ToVkFormat(ImageFormat format); | ||
| 113 | |||
| 114 | } // namespace ernie | ||
| 115 |