opynsim
Unofficial C++ Documentation
Loading...
Searching...
No Matches
texture2d.h
Go to the documentation of this file.
1#pragma once
2
12
13#include <cstdint>
14#include <functional>
15#include <iosfwd>
16#include <span>
17#include <vector>
18
19namespace osc
20{
21 // Represents a 2D image array that can be read by `Shader`s.
23 public:
24 // Constructs a `Texture2D` that contains a single pixel.
26 Texture2D{Vector2i{1, 1}}
27 {}
28
29 // Constructs a `Texture2D` with the given `pixel_dimensions`.
30 explicit Texture2D(
36 );
37
38 // Returns the dimensions of the texture in physical pixels.
40
41 // Returns the dimensions of the texture in device-independent pixels.
42 //
43 // Effectively, returns the equivalent of `texture.pixel_dimensions() / texture.device_pixel_ratio()`.
45
46 // Returns the ratio of the resolution of the texture in physical pixels
47 // to the resolution of it in device-independent pixels. This is useful
48 // when compositing the texture into mixed/high DPI user interfaces that
49 // are built with device-independent pixel scaling in mind.
50 float device_pixel_ratio() const;
51
52 // Sets the device-to-pixel ratio for the texture, which has the effect
53 // of scaling the `device_independent_dimensions()` of the texture.
55
56 // Returns the storage format of the underlying pixel data.
58
59 // Returns the color space of the texture.
61
62 // Returns the equivalent of `wrap_mode_u`.
64
65 // Sets all wrap axes (`u`, `v`, and `w`) to the specified `TextureWrapMode`.
73
76
77 // Returns the pixels, parsed into a `Color` (i.e. HDR sRGB RGBA) format, where:
78 //
79 // - Pixels are returned row-by-row, where:
80 // - The first pixel corresponds to the lower-left corner of the image.
81 // - Subsequent pixels progress left-to-right through the remaining pixels in the
82 // lowest row of the image, and then in successively higher rows of the image.
83 // - The final pixel corresponds to the upper-right corner of the image.
84 // - Note: this right-handed coordinate system matches samplers in GLSL shaders. That
85 // is, a texture/uv coordinate of `(0, 0)` sampled in a shader would sample the
86 // bottom-left pixel of the texture in GLSL.
87 // - The returned pixels are parsed from the underlying `TextureFormat` storage. If
88 // the storage format has fewer components than a `Color` (RGBA), the missing
89 // components default to `0.0f` - apart from alpha, which defaults to `1.0f`.
90 std::vector<Color> pixels() const;
91
92 // Assigns the given pixels to the texture.
93 //
94 // - Pixels should be provided row-by-row, where:
95 // - The first pixel corresponds to the lower-left corner of the image.
96 // - Subsequent pixels progress left-to-right through the remaining pixels in the
97 // lowest row of the image, and then in successively higher rows of the image.
98 // - The final pixel corresponds to the upper-right corner of the image.
99 // - Note: this right-handed coordinate system matches samplers in GLSL shaders. That
100 // is, a texture/uv coordinate of `(0, 0)` used in a shader would sample the bottom-left
101 // pixel of the texture in GLSL.
102 // - The `size()` of the provided pixel span must be equal to the area of this texture.
103 // - The provided pixels will be converted into the underlying `TextureFormat` storage
104 // of this texture, which may change the provided pixels' component values, depending
105 // on the format. This means that the return value of `pixels()` may not be equal to
106 // the pixels provided to this function.
107 void set_pixels(std::span<const Color>);
108
109 // Returns the pixels, parsed into a `Color32` (i.e. LDR sRGB RGBA) format, where:
110 //
111 // - Pixels are returned row-by-row
112 // - The first pixel corresponds to the lower-left corner of the image.
113 // - Subsequent pixels progress left-to-right through the remaining pixels in the
114 // lowest row of the image, and then in successively higher rows of the image.
115 // - The final pixel corresponds to the upper-right corner of the image.
116 // - Note: this right-handed coordinate system matches samplers in GLSL shaders. That
117 // is, a texture/uv coordinate of `(0, 0)` used in a shader would sample the bottom-left
118 // pixel of the texture in GLSL.
119 // - The returned pixels are parsed from the underlying `TextureFormat` storage. If
120 // the storage format has fewer components than a `Color` (RGBA), the missing
121 // components default to `0.0f` - apart from alpha, which defaults to `1.0f`.
122 std::vector<Color32> pixels32() const;
123 void set_pixels32(std::span<const Color32>);
124
125 // Returns the pixels, parsed into a `Color24` (i.e. LDR sRGB RGB) format, where:
126 //
127 // - Pixels are returned row-by-row
128 // - The first pixel corresponds to the lower-left corner of the image.
129 // - Subsequent pixels progress left-to-right through the remaining pixels in the
130 // lowest row of the image, and then in successively higher rows of the image.
131 // - The final pixel corresponds to the upper-right corner of the image.
132 // - Note: this right-handed coordinate system matches samplers in GLSL shaders. That
133 // is, a texture/uv coordinate of `(0, 0)` used in a shader would sample the bottom-left
134 // pixel of the texture in GLSL.
135 // - The returned pixels are parsed from the underlying `TextureFormat` storage. If
136 // the storage format has fewer components than a `Color` (RGB), the missing
137 // components default to `0.0f` - apart from alpha, which defaults to `1.0f`.
138 std::vector<Color24> pixels24() const;
139
140 // - must contain pixel _data_ row-by-row
141 // - the size of the data span must be equal to:
142 // - width*height*NumBytesPerPixel(texture_format())
143 // - will not perform any internal conversion of the data (it's a memcpy)
144 std::span<const uint8_t> pixel_data() const;
145 void set_pixel_data(std::span<const uint8_t>);
146
147 // Updates this texture's pixel data in-place. Equivalent to calling `pixel_data`, mutating
148 // it, and then passing that to `set_pixel_data`.
149 void update_pixel_data(const std::function<void(std::span<uint8_t>)>& updater);
150
151 friend bool operator==(const Texture2D&, const Texture2D&) = default;
152
153 class Impl;
154
155 // returns a reference to the `Texture2D`'s private implementation (for internal use).
156 const Impl& impl() const { return *impl_; }
157 private:
158 friend std::ostream& operator<<(std::ostream&, const Texture2D&);
159 friend class GraphicsBackend;
160
162 };
163
164 std::ostream& operator<<(std::ostream&, const Texture2D&);
165}
Definition copy_on_upd_shared_value.h:16
Definition texture2d.h:22
friend class GraphicsBackend
Definition texture2d.h:159
float device_pixel_ratio() const
void set_filter_mode(TextureFilterMode)
ColorSpace color_space() const
void set_device_pixel_ratio(float)
void set_wrap_mode_w(TextureWrapMode)
void set_wrap_mode_v(TextureWrapMode)
void set_pixel_data(std::span< const uint8_t >)
TextureWrapMode wrap_mode_w() const
std::span< const uint8_t > pixel_data() const
TextureFilterMode filter_mode() const
Vector2 dimensions() const
TextureWrapMode wrap_mode_u() const
void set_pixels(std::span< const Color >)
void set_pixels32(std::span< const Color32 >)
Texture2D()
Definition texture2d.h:25
const Impl & impl() const
Definition texture2d.h:156
std::vector< Color24 > pixels24() const
TextureWrapMode wrap_mode_v() const
void update_pixel_data(const std::function< void(std::span< uint8_t >)> &updater)
std::vector< Color32 > pixels32() const
Vector2i pixel_dimensions() const
std::vector< Color > pixels() const
friend std::ostream & operator<<(std::ostream &, const Texture2D &)
void set_wrap_mode_u(TextureWrapMode)
TextureFormat texture_format() const
Texture2D(Vector2i pixel_dimensions, TextureFormat=TextureFormat::RGBA32, ColorSpace=ColorSpace::sRGB, TextureWrapMode=TextureWrapMode::Repeat, TextureFilterMode=TextureFilterMode::Linear)
void set_wrap_mode(TextureWrapMode)
friend bool operator==(const Texture2D &, const Texture2D &)=default
TextureWrapMode wrap_mode() const
Definition custom_decoration_generator.h:5
std::ostream & operator<<(std::ostream &, const Object &)
TextureFilterMode
Definition texture_filter_mode.h:8
ColorSpace
Definition color_space.h:20
TextureWrapMode
Definition texture_wrap_mode.h:9
TextureFormat
Definition texture_format.h:10
constexpr U to(T &&value)
Definition conversion.h:56