libdonut 2.3.6
Application framework for cross-platform game development in C++20
Loading...
Searching...
No Matches
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>
8#include <donut/math.hpp>
9
10#include <cstddef> // std::size_t
11#include <cstdint> // std::int32_t
12#include <optional> // std::optional
13
14namespace donut::graphics {
15
16class Renderer; // Forward declaration, to avoid a circular include of Renderer.hpp.
17
22enum 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
75class Texture {
76public:
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 imageWidth, std::size_t imageHeight, PixelFormat pixelFormat, PixelComponentType pixelComponentType, const void* pixels, std::size_t x,
345 std::size_t y);
346
364 void pasteImage2D(const ImageView& image, std::size_t x, std::size_t y);
365
396 void pasteImage2DArray(std::size_t imageWidth, std::size_t imageHeight, std::size_t arrayDepth, PixelFormat pixelFormat, PixelComponentType pixelComponentType,
397 const void* pixels, std::size_t x, std::size_t y, std::size_t z);
398
418 void pasteImage2DArray(const ImageView& image, std::size_t x, std::size_t y, std::size_t z);
419
430 void fill2D(Renderer& renderer, Color color);
431
456 void grow2D(Renderer& renderer, std::size_t newWidth, std::size_t newHeight, std::optional<Color> backgroundColor = {});
457
477 [[nodiscard]] Texture copy2D(Renderer& renderer) const;
478
505 [[nodiscard]] Texture copyGrow2D(Renderer& renderer, std::size_t newWidth, std::size_t newHeight, std::optional<Color> backgroundColor = {}) const;
506
520 [[nodiscard]] vec2 getSize2D() const noexcept {
521 return {static_cast<float>(width), static_cast<float>(height)};
522 }
523
530 [[nodiscard]] TextureFormat getInternalFormat() const noexcept {
531 return internalFormat;
532 }
533
546 [[nodiscard]] std::size_t getWidth() const noexcept {
547 return width;
548 }
549
562 [[nodiscard]] std::size_t getHeight() const noexcept {
563 return height;
564 }
565
572 [[nodiscard]] const TextureOptions& getOptions() const noexcept {
573 return options;
574 }
575
586 [[nodiscard]] Handle get() const noexcept {
587 return texture.get();
588 }
589
590private:
591 friend Renderer;
592
593 static void createSharedTextures();
594 static void destroySharedTextures() noexcept;
595
596 struct TextureDeleter {
597 void operator()(Handle handle) const noexcept;
598 };
599
601 std::size_t width = 0;
602 std::size_t height = 0;
603 TextureFormat internalFormat = TextureFormat::R8_UNORM;
604 TextureOptions options{};
605};
606
607} // namespace donut::graphics
608
609#endif
Normalized floating-point RGBA color type with 32 bits per component.
Definition Color.hpp:11
Generic nullable RAII resource handle with exclusive ownership of a resource that is automatically de...
Definition UniqueHandle.hpp:21
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:39
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.
void pasteImage2D(std::size_t imageWidth, std::size_t imageHeight, 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.
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:520
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 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...
const TextureOptions & getOptions() const noexcept
Get the configuration options of this texture and its associated sampler.
Definition Texture.hpp:572
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:562
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:530
void pasteImage2DArray(std::size_t imageWidth, std::size_t imageHeight, std::size_t arrayDepth, 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.
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:546
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:586
Texture() noexcept=default
Construct an empty texture without a value.
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:28
PixelFormat
Description of the number and meaning of the pixel component channels of an image.
Definition Image.hpp:18
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