libs/render/src/render/texture.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #pragma once | ||
| 2 | |||
| 3 | #include "base/size.h" | ||
| 4 | |||
| 5 | #include "render/opengl_labels.h" | ||
| 6 | |||
| 7 | #include <cstdint> | ||
| 8 | |||
| 9 | namespace eu::render | ||
| 10 | { | ||
| 11 | |||
| 12 | /** \addtogroup texture | ||
| 13 | * @{ | ||
| 14 | */ | ||
| 15 | |||
| 16 | enum class TextureEdge | ||
| 17 | { | ||
| 18 | clamp, | ||
| 19 | repeat | ||
| 20 | }; | ||
| 21 | |||
| 22 | |||
| 23 | enum class TextureRenderStyle | ||
| 24 | { | ||
| 25 | pixel, | ||
| 26 | mipmap, | ||
| 27 | linear | ||
| 28 | }; | ||
| 29 | |||
| 30 | |||
| 31 | enum class Transparency | ||
| 32 | { | ||
| 33 | include, | ||
| 34 | exclude | ||
| 35 | }; | ||
| 36 | |||
| 37 | enum class ColorData | ||
| 38 | { | ||
| 39 | /// Apply transformations to the color data. Texture is a diffuse/albedo map, created with human eyes on a monitor in sRGB space. | ||
| 40 | color_data, | ||
| 41 | |||
| 42 | // todo(Gustav): should this be more specific so we could define better default colors if the load fails? | ||
| 43 | /// Don't apply transformations to the color data. Texture is a normal/roughness/ao/specular map or similar and created by a software in linear space. | ||
| 44 | non_color_data, | ||
| 45 | |||
| 46 | /// Explicitly don't care about the color data but same as \ref non_color_data. | ||
| 47 | dont_care | ||
| 48 | }; | ||
| 49 | |||
| 50 | // todo(Gustav): move to colors.h? | ||
| 51 | /// A single color in a format to load directly into open gl texture(ABGR on little endian). | ||
| 52 | /// @see \ref color_from_rgba | ||
| 53 | enum class SingleColor : std::uint32_t {}; | ||
| 54 | |||
| 55 | /// Constructs a \ref SingleColor value from individual red, green, blue, and alpha components. | ||
| 56 | /// @param r The red component of the color (0x00 - 0xFF). | ||
| 57 | /// @param g The green component of the color (0x00 - 0xFF). | ||
| 58 | /// @param b The blue component of the color (0x00 - 0xFF). | ||
| 59 | /// @param a The alpha (opacity) component of the color (0x00 - 0xFF). | ||
| 60 | /// @return A \ref SingleColor value representing the color composed of the specified RGBA components. | ||
| 61 | ✗ | constexpr SingleColor color_from_rgba(uint8_t r, uint8_t g, uint8_t b, uint8_t a) { | |
| 62 | ✗ | return static_cast<SingleColor>((static_cast<uint32_t>(a) << 24) | | |
| 63 | ✗ | (static_cast<uint32_t>(b) << 16) | | |
| 64 | ✗ | (static_cast<uint32_t>(g) << 8) | | |
| 65 | ✗ | (static_cast<uint32_t>(r))); | |
| 66 | } | ||
| 67 | |||
| 68 | // todo(Gustav): this doesn't do anything except allow code reuse, remove? | ||
| 69 | /// Base class for all textures, but only exist due to code reuse and can easily be inlined. | ||
| 70 | struct BaseTexture | ||
| 71 | { | ||
| 72 | unsigned int id; | ||
| 73 | |||
| 74 | BaseTexture(); | ||
| 75 | ~BaseTexture(); | ||
| 76 | |||
| 77 | BaseTexture(const BaseTexture&) = delete; | ||
| 78 | void operator=(const BaseTexture&) = delete; | ||
| 79 | |||
| 80 | BaseTexture(BaseTexture&&) noexcept; | ||
| 81 | BaseTexture& operator=(BaseTexture&&) noexcept; | ||
| 82 | |||
| 83 | /// clears the loaded texture to a invalid texture | ||
| 84 | void unload(); | ||
| 85 | }; | ||
| 86 | |||
| 87 | /// A 2d image texture. | ||
| 88 | struct Texture2d : BaseTexture | ||
| 89 | { | ||
| 90 | Texture2d() = delete; | ||
| 91 | |||
| 92 | /// "internal" | ||
| 93 | Texture2d(DEBUG_LABEL_ARG_MANY const void* pixel_data, unsigned int pixel_format, int w, int h, TextureEdge te, TextureRenderStyle trs, Transparency t, ColorData cd); | ||
| 94 | }; | ||
| 95 | |||
| 96 | Texture2d load_image_from_color(DEBUG_LABEL_ARG_MANY SingleColor pixel, TextureEdge te, TextureRenderStyle trs, Transparency t, ColorData cd); | ||
| 97 | |||
| 98 | // todo(Gustav): turn into an enum? | ||
| 99 | /// 0=right(x+), 1=left(x-), 2=top(y+), 3=bottom(y-), 4=front(z+), 5=back(z-) | ||
| 100 | constexpr std::size_t cubemap_size = 6; | ||
| 101 | |||
| 102 | /// A cubemap texture. | ||
| 103 | /// Useful for skybox rendering or faking reflections from a (static) scene. | ||
| 104 | struct TextureCubemap : BaseTexture | ||
| 105 | { | ||
| 106 | TextureCubemap() = delete; | ||
| 107 | |||
| 108 | TextureCubemap(DEBUG_LABEL_ARG_MANY const std::array<void*, cubemap_size>& pixel_data, int width, int height, ColorData cd); | ||
| 109 | }; | ||
| 110 | |||
| 111 | TextureCubemap load_cubemap_from_color(DEBUG_LABEL_ARG_MANY SingleColor pixel, ColorData cd); | ||
| 112 | |||
| 113 | |||
| 114 | |||
| 115 | /// "render to texture" feature | ||
| 116 | /// @see \ref create-framebuffer | ||
| 117 | struct FrameBuffer : BaseTexture | ||
| 118 | { | ||
| 119 | /// @param f The FBO handle | ||
| 120 | /// @param s the texture size | ||
| 121 | FrameBuffer(unsigned int f, const Size& s); | ||
| 122 | ~FrameBuffer(); | ||
| 123 | |||
| 124 | FrameBuffer(const FrameBuffer&) = delete; | ||
| 125 | FrameBuffer(FrameBuffer&&) = delete; | ||
| 126 | void operator=(const FrameBuffer&) = delete; | ||
| 127 | void operator=(FrameBuffer&&) = delete; | ||
| 128 | |||
| 129 | Size size; | ||
| 130 | |||
| 131 | unsigned int fbo = 0; | ||
| 132 | unsigned int rbo = 0; | ||
| 133 | |||
| 134 | bool debug_is_msaa = false; | ||
| 135 | }; | ||
| 136 | |||
| 137 | /// The number of bits to use for the depth buffer. | ||
| 138 | enum class DepthBits | ||
| 139 | { | ||
| 140 | use_none, | ||
| 141 | use_16, use_24, use_32 | ||
| 142 | }; | ||
| 143 | |||
| 144 | /// The number of bits/pixel to use for the color buffer. | ||
| 145 | enum class ColorBitsPerPixel | ||
| 146 | { | ||
| 147 | use_depth, | ||
| 148 | use_8, use_16, use_32 | ||
| 149 | }; | ||
| 150 | |||
| 151 | |||
| 152 | /** Build a simple LDR frame buffer. | ||
| 153 | * | ||
| 154 | * This is mostly used for post-processing. | ||
| 155 | * | ||
| 156 | * \ingroup create-framebuffer | ||
| 157 | * | ||
| 158 | * @param size The size of the framebuffer. | ||
| 159 | * @return The created \ref FrameBuffer. | ||
| 160 | */ | ||
| 161 | std::shared_ptr<FrameBuffer> build_simple_framebuffer(DEBUG_LABEL_ARG_MANY const Size& size); | ||
| 162 | |||
| 163 | |||
| 164 | /** Builds a multisample anti-aliasing (MSAA) framebuffer for high-quality rendering. | ||
| 165 | * | ||
| 166 | * This function creates a framebuffer object (FBO) with multisample support, allowing for smoother edges and reduced aliasing artifacts in rendered images. | ||
| 167 | * | ||
| 168 | * \ingroup create-framebuffer | ||
| 169 | * | ||
| 170 | * @param size The dimensions (width, height) of the framebuffer in pixels. | ||
| 171 | * @param msaa_samples The number of MSAA samples to use. Higher values provide better anti-aliasing but may impact performance. Typical values are 2, 4, or 8. | ||
| 172 | * @param bits_per_pixel The color buffer precision. | ||
| 173 | * @return The created \ref FrameBuffer object with MSAA enabled. | ||
| 174 | */ | ||
| 175 | std::shared_ptr<FrameBuffer> build_msaa_framebuffer(DEBUG_LABEL_ARG_MANY const Size& size, int msaa_samples, ColorBitsPerPixel bits_per_pixel); | ||
| 176 | |||
| 177 | |||
| 178 | /** Build a HDR frame buffer. | ||
| 179 | * | ||
| 180 | * \ingroup create-framebuffer | ||
| 181 | * | ||
| 182 | * @param size The resolution in pixels. | ||
| 183 | * @param bits_per_pixel How many bits per pixel to use | ||
| 184 | * @return The created \ref FrameBuffer | ||
| 185 | */ | ||
| 186 | std::shared_ptr<FrameBuffer> build_hdr_floating_framebuffer(DEBUG_LABEL_ARG_MANY const Size& size, ColorBitsPerPixel bits_per_pixel); | ||
| 187 | |||
| 188 | |||
| 189 | /** Create a depth-only framebuffer for shadow rendering. | ||
| 190 | * | ||
| 191 | * \ingroup create-framebuffer | ||
| 192 | * | ||
| 193 | * @param size The resolution in pixels. | ||
| 194 | * @return The created \ref FrameBuffer | ||
| 195 | */ | ||
| 196 | std::shared_ptr<FrameBuffer> build_shadow_framebuffer(DEBUG_LABEL_ARG_MANY const Size& size); | ||
| 197 | |||
| 198 | |||
| 199 | /// raii class to render to a FrameBuffer | ||
| 200 | struct BoundFbo | ||
| 201 | { | ||
| 202 | std::shared_ptr<FrameBuffer> fbo; | ||
| 203 | |||
| 204 | BoundFbo(const BoundFbo&) = delete; | ||
| 205 | BoundFbo(BoundFbo&&) = delete; | ||
| 206 | void operator=(const BoundFbo&) = delete; | ||
| 207 | void operator=(BoundFbo&&) = delete; | ||
| 208 | |||
| 209 | explicit BoundFbo(std::shared_ptr<FrameBuffer> f); | ||
| 210 | ~BoundFbo(); | ||
| 211 | }; | ||
| 212 | |||
| 213 | void resolve_multisampled_buffer(const FrameBuffer& src, FrameBuffer* dst); | ||
| 214 | |||
| 215 | /** | ||
| 216 | * @} | ||
| 217 | */ | ||
| 218 | |||
| 219 | } | ||
| 220 |