libdonut  2.3.2
Application framework for cross-platform game development in C++20
Texture.hpp
Go to the documentation of this file.
1 #ifndef DONUT_GRAPHICS_TEXTURE_HPP
2 #define DONUT_GRAPHICS_TEXTURE_HPP
3 
4 #include <donut/Color.hpp>
5 #include <donut/UniqueHandle.hpp>
8 #include <donut/math.hpp>
9 
10 #include <cstddef> // std::size_t
11 #include <cstdint> // std::int32_t
12 #include <optional> // std::optional
13 
14 namespace donut::graphics {
15 
16 class Renderer; // Forward declaration, to avoid a circular include of Renderer.hpp.
17 
22 enum class TextureFormat : std::int32_t {
23  R8_UNORM = 0x8229,
24  R16_FLOAT = 0x822D,
25  R32_FLOAT = 0x822E,
26  R8G8_UNORM = 0x822B,
27  R16G16_FLOAT = 0x822F,
28  R32G32_FLOAT = 0x8230,
29  R8G8B8_UNORM = 0x8051,
30  R16G16B16_FLOAT = 0x881B,
31  R32G32B32_FLOAT = 0x8815,
32  R8G8B8A8_UNORM = 0x8058,
33  R16G16B16A16_FLOAT = 0x881A,
34  R32G32B32A32_FLOAT = 0x8814,
35 };
36 
47  bool repeat = true;
48 
57  bool useLinearFiltering = true;
58 
68  bool useMipmap = true;
69 };
70 
75 class Texture {
76 public:
85  static const Texture* const TRANSPARENT;
86 
95  static const Texture* const BLACK;
96 
105  static const Texture* const WHITE;
106 
115  static const Texture* const DEFAULT_SPECULAR;
116 
125  static const Texture* const DEFAULT_NORMAL;
126 
135  [[nodiscard]] static std::size_t getChannelCount(TextureFormat internalFormat) noexcept;
136 
146  [[nodiscard]] static PixelFormat getPixelFormat(TextureFormat internalFormat) noexcept;
147 
157  [[nodiscard]] static PixelComponentType getPixelComponentType(TextureFormat internalFormat) noexcept;
158 
167  [[nodiscard]] static TextureFormat getInternalFormat(PixelFormat pixelFormat, PixelComponentType pixelComponentType) noexcept;
168 
172  Texture() noexcept = default;
173 
200  Texture(TextureFormat internalFormat, std::size_t width, std::size_t height, PixelFormat pixelFormat, PixelComponentType pixelComponentType, const void* pixels,
201  const TextureOptions& options = {});
202 
230  Texture(TextureFormat internalFormat, std::size_t width, std::size_t height, std::size_t depth, PixelFormat pixelFormat, PixelComponentType pixelComponentType,
231  const void* pixels, const TextureOptions& options = {});
232 
247  Texture(TextureFormat internalFormat, std::size_t width, std::size_t height, const TextureOptions& options = {});
248 
264  Texture(TextureFormat internalFormat, std::size_t width, std::size_t height, std::size_t depth, const TextureOptions& options = {});
265 
287  Texture(const ImageView& image, const TextureOptions& options = {});
288 
294  explicit operator bool() const noexcept {
295  return static_cast<bool>(texture);
296  }
297 
306  void setOptions2D(const TextureOptions& newOptions);
307 
317  void setOptions2DArray(const TextureOptions& newOptions);
318 
344  void pasteImage2D(std::size_t width, std::size_t height, PixelFormat pixelFormat, PixelComponentType pixelComponentType, const void* pixels, std::size_t x, std::size_t y);
345 
363  void pasteImage2D(const ImageView& image, std::size_t x, std::size_t y);
364 
395  void pasteImage2DArray(std::size_t width, std::size_t height, std::size_t depth, PixelFormat pixelFormat, PixelComponentType pixelComponentType, const void* pixels,
396  std::size_t x, std::size_t y, std::size_t z);
397 
417  void pasteImage2DArray(const ImageView& image, std::size_t x, std::size_t y, std::size_t z);
418 
429  void fill2D(Renderer& renderer, Color color);
430 
455  void grow2D(Renderer& renderer, std::size_t newWidth, std::size_t newHeight, std::optional<Color> backgroundColor = {});
456 
476  [[nodiscard]] Texture copy2D(Renderer& renderer) const;
477 
504  [[nodiscard]] Texture copyGrow2D(Renderer& renderer, std::size_t newWidth, std::size_t newHeight, std::optional<Color> backgroundColor = {}) const;
505 
519  [[nodiscard]] vec2 getSize2D() const noexcept {
520  return {static_cast<float>(width), static_cast<float>(height)};
521  }
522 
529  [[nodiscard]] TextureFormat getInternalFormat() const noexcept {
530  return internalFormat;
531  }
532 
545  [[nodiscard]] std::size_t getWidth() const noexcept {
546  return width;
547  }
548 
561  [[nodiscard]] std::size_t getHeight() const noexcept {
562  return height;
563  }
564 
571  [[nodiscard]] const TextureOptions& getOptions() const noexcept {
572  return options;
573  }
574 
585  [[nodiscard]] Handle get() const noexcept {
586  return texture.get();
587  }
588 
589 private:
590  friend Renderer;
591 
592  static void createSharedTextures();
593  static void destroySharedTextures() noexcept;
594 
595  struct TextureDeleter {
596  void operator()(Handle handle) const noexcept;
597  };
598 
600  std::size_t width = 0;
601  std::size_t height = 0;
602  TextureFormat internalFormat = TextureFormat::R8_UNORM;
603  TextureOptions options{};
604 };
605 
606 } // namespace donut::graphics
607 
608 #endif
Normalized floating-point RGBA color type with 32 bits per component.
Definition: Color.hpp:11
constexpr Handle get() const noexcept
Get the value of the underlying resource handle.
Definition: UniqueHandle.hpp:152
Read-only non-owning view over a 2D image.
Definition: Image.hpp:40
Persistent system for rendering the batched draw commands of a RenderPass onto a Framebuffer,...
Definition: Renderer.hpp:38
Storage for multidimensional data, such as 2D images, on the GPU, combined with a sampler configurati...
Definition: Texture.hpp:75
void pasteImage2DArray(const ImageView &image, std::size_t x, std::size_t y, std::size_t z)
Copy a 2D image into the 2D array texture at a specific position.
const TextureOptions & getOptions() const noexcept
Get the configuration options of this texture and its associated sampler.
Definition: Texture.hpp:571
Texture(TextureFormat internalFormat, std::size_t width, std::size_t height, std::size_t depth, const TextureOptions &options={})
Create a new texture object and allocate uninitialized GPU memory for storing an array of layers of 2...
vec2 getSize2D() const noexcept
Get the floating-point size, in texels, of the 2D image data stored in this texture.
Definition: Texture.hpp:519
Texture copyGrow2D(Renderer &renderer, std::size_t newWidth, std::size_t newHeight, std::optional< Color > backgroundColor={}) const
Create a new texture object and allocate GPU memory in an expanded version of this texture onto which...
void pasteImage2DArray(std::size_t width, std::size_t height, std::size_t depth, PixelFormat pixelFormat, PixelComponentType pixelComponentType, const void *pixels, std::size_t x, std::size_t y, std::size_t z)
Copy an array of layers of 2D image data into the 2D array texture at a specific position.
void pasteImage2D(const ImageView &image, std::size_t x, std::size_t y)
Copy a 2D image into the 2D texture at a specific position.
Texture(TextureFormat internalFormat, std::size_t width, std::size_t height, std::size_t depth, PixelFormat pixelFormat, PixelComponentType pixelComponentType, const void *pixels, const TextureOptions &options={})
Create a new texture object and allocate GPU memory for storing an array of layers of 2D image data.
static PixelComponentType getPixelComponentType(TextureFormat internalFormat) noexcept
Get a description of the pixel component type that corresponds to the texel component type of an inte...
static const Texture *const DEFAULT_SPECULAR
Pointer to the statically allocated storage for the built-in default specular-map 2D texture.
Definition: Texture.hpp:115
std::size_t getHeight() const noexcept
Get the height, in texels, of the 2D image data stored in this texture.
Definition: Texture.hpp:561
static TextureFormat getInternalFormat(PixelFormat pixelFormat, PixelComponentType pixelComponentType) noexcept
Get the internal texture format that correspons to an image format.
static std::size_t getChannelCount(TextureFormat internalFormat) noexcept
Get the number of texel component channels defined by an internal texel format.
static PixelFormat getPixelFormat(TextureFormat internalFormat) noexcept
Get a description of the pixel format that corresponds to the texel format of an internal texture for...
static const Texture *const BLACK
Pointer to the statically allocated storage for the built-in black 2D texture.
Definition: Texture.hpp:95
Texture(TextureFormat internalFormat, std::size_t width, std::size_t height, const TextureOptions &options={})
Create a new texture object and allocate uninitialized GPU memory for storing 2D image data.
void setOptions2DArray(const TextureOptions &newOptions)
Apply a new configuration of texture/sampler options to the 2D array texture.
TextureFormat getInternalFormat() const noexcept
Get the internal texel format of this texture.
Definition: Texture.hpp:529
Texture(const ImageView &image, const TextureOptions &options={})
Create a new texture object and allocate GPU memory for storing 2D image data loaded from an image.
void setOptions2D(const TextureOptions &newOptions)
Apply a new configuration of texture/sampler options to the 2D texture.
static const Texture *const WHITE
Pointer to the statically allocated storage for the built-in white 2D texture.
Definition: Texture.hpp:105
std::size_t getWidth() const noexcept
Get the width, in texels, of the 2D image data stored in this texture.
Definition: Texture.hpp:545
void grow2D(Renderer &renderer, std::size_t newWidth, std::size_t newHeight, std::optional< Color > backgroundColor={})
Expand the allocated 2D texture data by allocating larger texture storage and copying the old texture...
Handle get() const noexcept
Get an opaque handle to the GPU representation of the texture.
Definition: Texture.hpp:585
Texture() noexcept=default
Construct an empty texture without a value.
void pasteImage2D(std::size_t width, std::size_t height, PixelFormat pixelFormat, PixelComponentType pixelComponentType, const void *pixels, std::size_t x, std::size_t y)
Copy 2D image data into the 2D texture at a specific position.
static const Texture *const TRANSPARENT
Pointer to the statically allocated storage for the built-in transparent 2D texture.
Definition: Texture.hpp:85
void fill2D(Renderer &renderer, Color color)
Fill the entire allocated 2D texture data with pixels of the given color.
Texture copy2D(Renderer &renderer) const
Create a new texture object and allocate GPU memory onto which the 2D image data of this 2D texture i...
static const Texture *const DEFAULT_NORMAL
Pointer to the statically allocated storage for the built-in default normal-map 2D texture.
Definition: Texture.hpp:125
Definition: Buffer.hpp:7
TextureFormat
Description of the internal texel format of a Texture, including the number of component channels,...
Definition: Texture.hpp:22
@ R32G32_FLOAT
Each texel comprises 2 32-bit floating-point components: red, green.
@ R16_FLOAT
Each texel comprises 1 16-bit floating-point component: red.
@ R32_FLOAT
Each texel comprises 1 32-bit floating-point component: red.
@ R16G16B16_FLOAT
Each texel comprises 3 16-bit floating-point components: red, green, blue.
@ R16G16_FLOAT
Each texel comprises 2 16-bit floating-point components: red, green.
@ R8G8B8_UNORM
Each texel comprises 3 normalized 8-bit unsigned integer components: red, green, blue.
@ R8_UNORM
Each texel comprises 1 normalized 8-bit unsigned integer component: red.
@ R8G8_UNORM
Each texel comprises 2 normalized 8-bit unsigned integer components: red, green.
@ R32G32B32A32_FLOAT
Each texel comprises 4 32-bit floating-point components: red, green, blue, alpha.
@ R16G16B16A16_FLOAT
Each texel comprises 4 16-bit floating-point components: red, green, blue, alpha.
@ R32G32B32_FLOAT
Each texel comprises 3 32-bit floating-point components: red, green, blue.
@ R8G8B8A8_UNORM
Each texel comprises 4 normalized 8-bit unsigned integer components: red, green, blue,...
std::uint32_t Handle
Generic GPU resource handle.
Definition: Handle.hpp:11
PixelComponentType
Description of the data type of the pixel components of an image.
Definition: Image.hpp:29
PixelFormat
Description of the number and meaning of the pixel component channels of an image.
Definition: Image.hpp:19
Configuration options for a Texture and its assocated sampler.
Definition: Texture.hpp:40
bool useMipmap
Generate a mipmap of different levels of detail for the texture and use it when sampling the texture ...
Definition: Texture.hpp:68
bool useLinearFiltering
Use bilinear filtering rather than nearest-neighbor interpolation when sampling the texture at coordi...
Definition: Texture.hpp:57
bool repeat
Treat the texture as repeating when sampling it outside of the 0-1 texture coordinate range.
Definition: Texture.hpp:47