Grindstone Game Engine v0.2.0
An open source game engine and toolkit.
Loading...
Searching...
No Matches
Image.hpp
1#pragma once
2
3#include <stdint.h>
4
5#include <Common/Containers/Bitset.hpp>
6#include <Common/Buffer.hpp>
7
8#include "Formats.hpp"
9
10namespace Grindstone::GraphicsAPI {
11 enum class ImageUsageFlags : uint8_t {
12 Sampled = 1,
13 RenderTarget = 1 << 1,
14 DepthStencil = 1 << 2,
15 Storage = 1 << 3,
16 GenerateMipmaps = 1 << 4,
17 Cubemap = 1 << 5,
18 TransferSrc = 1 << 6,
19 TransferDst = 1 << 7,
20 Count = 8
21 };
22}
23
24template <>
25struct EnumFlagsTraits<Grindstone::GraphicsAPI::ImageUsageFlags> {
26 static constexpr const char* names[] = {
27 "Sampled",
28 "RenderTarget",
29 "DepthStencil",
30 "Storage",
31 "GenerateMipmaps",
32 "Cubemap",
33 "TransferSrc",
34 "TransferDst"
35 };
36 static constexpr size_t size = 8;
37};
38
39inline Grindstone::GraphicsAPI::ImageUsageFlags operator|(Grindstone::GraphicsAPI::ImageUsageFlags a, const Grindstone::GraphicsAPI::ImageUsageFlags b) {
40 using Underlying = uint8_t;
41 return static_cast<Grindstone::GraphicsAPI::ImageUsageFlags>(static_cast<Underlying>(a) | static_cast<Underlying>(b));
42}
43
44namespace Grindstone::GraphicsAPI {
49 class Image {
50 public:
51 static const uint64_t MAPPED_MEMORY_ENTIRE_BUFFER = UINT64_MAX;
52
53 struct CreateInfo {
54 const char* debugName;
55 uint32_t width = 1;
56 uint32_t height = 1;
57 uint32_t depth = 1;
58 uint32_t mipLevels = 1;
59 uint32_t arrayLayers = 1;
60
61 GraphicsAPI::Format format = GraphicsAPI::Format::Invalid;
62 GraphicsAPI::ImageDimension imageDimensions = GraphicsAPI::ImageDimension::Dimension2D;
63 GraphicsAPI::MemoryUsage memoryUsage = GraphicsAPI::MemoryUsage::GPUOnly;
65
66 const char* initialData = nullptr;
67 uint64_t initialDataSize = 0;
68 };
69
70 // ImageRegions are used to map regions to a buffer thatis passed in, and mapping specific parts of memory to
71 // coordinates, dimensions, array layers, and mip levels of the image. It is used with UploadDataRegions.
72 struct ImageRegion {
73 uint64_t bufferOffset = 0;
74 uint32_t bufferRowLength = 0;
75 uint32_t bufferImageHeight = 0;
76 int32_t x = 0;
77 int32_t y = 0;
78 int32_t z = 0;
79 uint32_t width = 1;
80 uint32_t height = 1;
81 uint32_t depth = 1;
82 uint32_t mipLevel = 0;
83 uint32_t baseArrayLayer = 0;
84 uint32_t arrayLayerCount = 1;
85 };
86
87 Image() = default;
88 Image(const Image&) = default;
89 Image(Image&&) noexcept = default;
90 Image& operator=(const Image&) = default;
91 Image& operator=(Image&&) noexcept = default;
92
93 Image(
94 uint32_t width,
95 uint32_t height,
96 uint32_t depth,
97 uint32_t mipLevels,
98 uint32_t arrayLayers,
99 uint64_t maxImageSize,
100 GraphicsAPI::ImageDimension imageDimension,
101 GraphicsAPI::Format format,
102 Grindstone::Containers::BitsetFlags<GraphicsAPI::ImageUsageFlags> imageUsage,
103 GraphicsAPI::MemoryUsage memoryUsage = GraphicsAPI::MemoryUsage::GPUOnly
104 ) : width(width),
105 height(height),
106 depth(depth),
107 mipLevels(mipLevels),
108 arrayLayers(arrayLayers),
109 maxImageSize(maxImageSize),
110 imageDimension(imageDimension),
111 format(format),
112 imageUsage(imageUsage),
113 memoryUsage(memoryUsage) {}
114
115 bool IsCubemap() const { return imageUsage.Test(GraphicsAPI::ImageUsageFlags::Cubemap); }
116 uint32_t GetWidth() const { return width; }
117 uint32_t GetHeight() const { return height; }
118 uint32_t GetDepth() const { return depth; }
119 uint32_t GetMipLevels() const { return mipLevels; }
120 uint32_t GetArrayLayers() const { return arrayLayers; }
121 uint64_t GetMaxImageSize() const { return maxImageSize; }
122 GraphicsAPI::ImageDimension GetImageDimension() const { return imageDimension; }
123 GraphicsAPI::Format GetFormat() const { return format; }
124 Grindstone::Containers::BitsetFlags<GraphicsAPI::ImageUsageFlags> GetImageUsage() const { return imageUsage; }
125 GraphicsAPI::MemoryUsage GetMemoryUsage() const { return memoryUsage; }
126
127 // Discards the previous image and creates a new image of a certain size, useful for RenderTargets.
128 virtual void Resize(uint32_t width, uint32_t height) = 0;
129
130 // Upload data to the entire image based on the data from the CreateInfo.
131 virtual void UploadData(const char* data, uint64_t dataSize) = 0;
132
133 // Obtain a CPU poitner to GPU memory.
134 virtual void* MapMemory(uint64_t dataSize = MAPPED_MEMORY_ENTIRE_BUFFER, uint64_t dataOffset = 0) = 0;
135
136 // Stop reading memory.
137 virtual void UnmapMemory() = 0;
138
139 // Upload data to specific regions for more control, used especially for creating Texture Atlases or other
140 // more specific graphical techniques. A buffer is passed in, and regions map specific parts of memory to
141 // coordinates, dimensions, array layers, and mip levels of the image.
142 virtual void UploadDataRegions(void* buffer, size_t bufferSize, ImageRegion* regions, uint32_t regionCount) = 0;
143
144 // Readback memory using a staging buffer, and then copy that to a Grindstone::Buffer.
145 virtual Grindstone::Buffer ReadbackMemory() = 0;
146
147 protected:
148
149 uint32_t width;
150 uint32_t height;
151 uint32_t depth;
152 uint32_t mipLevels;
153 uint32_t arrayLayers;
154 uint64_t maxImageSize;
155 GraphicsAPI::ImageDimension imageDimension;
156 GraphicsAPI::Format format;
157 Grindstone::Containers::BitsetFlags<GraphicsAPI::ImageUsageFlags> imageUsage;
158 GraphicsAPI::MemoryUsage memoryUsage = GraphicsAPI::MemoryUsage::GPUOnly;
159
160 };
161}
Definition Bitset.hpp:331
Definition EnumTraits.hpp:10