Estella 0.113.0
A fast 2D and 3D game engine powered by WebAssembly and ECS
Loading...
Searching...
No Matches
Texture.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
15#pragma once
16
17// =============================================================================
18// Includes
19// =============================================================================
20
21// Project includes
22#include "../../core/Types.hpp"
23#include "./GfxContent.hpp"
24#include "./GfxEnums.hpp"
25
26// Standard library
27#include <span>
28#include <string>
29#include <vector>
30
31namespace esengine {
32
33class GfxDevice;
34
35// =============================================================================
36// Texture Enums
37// =============================================================================
38
39// TextureFilter / TextureWrap live in GfxEnums.hpp with the other backend-agnostic
40// sampling enums; TextureFormat below is this wrapper's user-facing format set.
41
54
55// =============================================================================
56// Texture Specification
57// =============================================================================
58
96
97// =============================================================================
98// Texture Class
99// =============================================================================
100
116class Texture {
117public:
118 Texture() = default;
119 virtual ~Texture();
120
121 // Non-copyable, movable
122 Texture(const Texture&) = delete;
123 Texture& operator=(const Texture&) = delete;
124 Texture(Texture&& other) noexcept;
125 Texture& operator=(Texture&& other) noexcept;
126
127 // =========================================================================
128 // Creation
129 // =========================================================================
130
139 static Unique<Texture> create(GfxDevice& device, GfxContent content, const TextureSpecification& spec);
140
149 static Unique<Texture> create(GfxDevice& device, GfxContent content, u32 width, u32 height,
150 std::span<const u8> pixels,
152 bool flipY = false);
153
163 static Unique<Texture> create(GfxDevice& device, GfxContent content, u32 width, u32 height,
164 const std::vector<u8>& pixels,
166 bool flipY = false);
167
172 static Unique<Texture> borrow(GfxDevice& device, TextureHandle texture, u32 width, u32 height);
173
179 static Unique<Texture> createArray(GfxDevice& device, GfxContent content, const TextureSpecification& spec,
180 const void* pixels);
181
187 bool adoptContent(Texture& from);
188
189 // =========================================================================
190 // Operations
191 // =========================================================================
192
197 void bind(u32 slot = 0) const;
198
200 void unbind() const;
201
206 void setData(std::span<const u8> pixels);
207
212 void setData(const std::vector<u8>& pixels);
213
225 void updateSubRegion(u32 xoffset, u32 yoffset, u32 width, u32 height,
226 const void* data, u32 sizeBytes, bool flipY = false);
227
228 // =========================================================================
229 // Properties
230 // =========================================================================
231
233 TextureHandle handle() const { return handle_; }
234
236 u32 getId() const { return static_cast<u32>(handle_); }
237
239 u32 getWidth() const { return width_; }
240
242 u32 getHeight() const { return height_; }
243
245 TextureFormat getFormat() const { return format_; }
246
252 bool operator==(const Texture& other) const {
253 return handle_ == other.handle_;
254 }
255
256 // =========================================================================
257 // Raw API for internal use only
258 // =========================================================================
259
268 static Unique<Texture> createRaw(GfxDevice& device, GfxContent content, u32 width, u32 height,
269 const void* data,
271 bool flipY = false);
272
277 static Unique<Texture> createVolume(GfxDevice& device, GfxContent content, u32 width, u32 height, u32 depth,
278 const void* data, TextureFormat format);
279
286 static Unique<Texture> createCompressed(GfxDevice& device, GfxContent content, u32 width, u32 height,
287 GfxCompressedFormat format, std::span<const u8> data,
288 u32 mipLevels = 1);
289
294 static Unique<Texture> createFromExternalId(GfxDevice& device, GfxContent content, u32 glTextureId,
295 u32 width, u32 height,
297
303 void setDataRaw(const void* data, u32 sizeBytes, bool flipY = false);
304
305private:
313 bool initialize(const TextureSpecification& spec, GfxContent content, const void* pixels, bool flipY);
314
315 GfxDevice* device_ = nullptr;
317 u32 width_ = 0;
318 u32 height_ = 0;
321 bool owns_ = true;
322};
323
324} // namespace esengine
Who restores a GPU object's contents after the device is lost.
Backend-agnostic graphics enumerations.
Core type definitions and utilities for ESEngine.
A content policy. provider names who refills a Sourced object and key what it refills; both are the p...
Abstract graphics device interface.
Definition GfxDevice.hpp:75
void updateSubRegion(u32 xoffset, u32 yoffset, u32 width, u32 height, const void *data, u32 sizeBytes, bool flipY=false)
Uploads pixel data to a sub-rectangle of the texture.
Definition Texture.cpp:280
u32 getWidth() const
Gets the texture width in pixels.
Definition Texture.hpp:239
Texture(const Texture &)=delete
void bind(u32 slot=0) const
Binds the texture to a texture unit.
Definition Texture.cpp:248
static Unique< Texture > createCompressed(GfxDevice &device, GfxContent content, u32 width, u32 height, GfxCompressedFormat format, std::span< const u8 > data, u32 mipLevels=1)
Creates a block-compressed texture (KTX2 → ETC2/ASTC/BC), uploading the pre-compressed blocks in one ...
Definition Texture.cpp:152
bool operator==(const Texture &other) const
Compares textures by GPU ID.
Definition Texture.hpp:252
static Unique< Texture > createFromExternalId(GfxDevice &device, GfxContent content, u32 glTextureId, u32 width, u32 height, TextureFormat format=TextureFormat::RGBA8)
Hands the device a texture the host made with its own API; the device owns it from here.
Definition Texture.cpp:179
void setDataRaw(const void *data, u32 sizeBytes, bool flipY=false)
Updates texture pixel data from raw pointer (internal use).
Definition Texture.cpp:266
void unbind() const
Unbinds the texture.
Definition Texture.cpp:252
static Unique< Texture > createArray(GfxDevice &device, GfxContent content, const TextureSpecification &spec, const void *pixels)
Creates a 2D texture array from spec.layers layers of spec.width x spec.height.
Definition Texture.cpp:92
virtual ~Texture()
Definition Texture.cpp:45
u32 getHeight() const
Gets the texture height in pixels.
Definition Texture.hpp:242
static Unique< Texture > createRaw(GfxDevice &device, GfxContent content, u32 width, u32 height, const void *data, TextureFormat format=TextureFormat::RGBA8, bool flipY=false)
Creates a texture from raw pixel pointer (internal use).
Definition Texture.cpp:114
static Unique< Texture > createVolume(GfxDevice &device, GfxContent content, u32 width, u32 height, u32 depth, const void *data, TextureFormat format)
A 3D texture, linearly filtered across all three axes and clamped at every edge.
Definition Texture.cpp:132
TextureFormat getFormat() const
Gets the pixel format.
Definition Texture.hpp:245
static Unique< Texture > create(GfxDevice &device, GfxContent content, const TextureSpecification &spec)
Creates an empty texture from specification.
Definition Texture.cpp:83
u32 getId() const
Gets the texture handle's raw value, for command payloads and the WASM boundary.
Definition Texture.hpp:236
void setData(std::span< const u8 > pixels)
Updates texture pixel data from span.
Definition Texture.cpp:256
TextureHandle handle() const
Gets the device texture handle.
Definition Texture.hpp:233
Texture & operator=(const Texture &)=delete
static Unique< Texture > borrow(GfxDevice &device, TextureHandle texture, u32 width, u32 height)
Names a texture the device already holds for another owner (a render target's colour plane),...
Definition Texture.cpp:195
bool adoptContent(Texture &from)
Takes from's pixels behind this texture's handle; from is empty afterwards.
Definition Texture.cpp:207
@ Repeat
Tile the texture.
Definition GfxEnums.hpp:63
@ Linear
Bilinear interpolation (smooth).
Definition GfxEnums.hpp:59
TextureFormat
Texture pixel format.
Definition Texture.hpp:45
@ SRGB8A8
sRGB-encoded RGBA8: hardware-linearized on sample (32 bpp)
Definition Texture.hpp:49
@ RGBA8
4 channels, 8 bits each (32 bpp)
Definition Texture.hpp:48
@ Depth24
Depth buffer format (24 bits).
Definition Texture.hpp:52
GfxCompressedFormat
GPU-compressed texture internal formats for createCompressedTexture.
Definition GfxEnums.hpp:326
std::unique_ptr< T > Unique
Unique ownership smart pointer.
Definition Types.hpp:75
std::uint32_t u32
Unsigned 32-bit integer.
Definition Types.hpp:45
Texture creation parameters.
Definition Texture.hpp:74
TextureFilter minFilter
Minification filter.
Definition Texture.hpp:82
bool volume
A 3D texture layers deep (see TextureDesc::volume).
Definition Texture.hpp:94
TextureWrap wrapS
Horizontal wrap mode.
Definition Texture.hpp:86
u32 height
Texture height in pixels.
Definition Texture.hpp:78
bool generateMips
Generate mipmaps automatically.
Definition Texture.hpp:90
TextureFormat format
Pixel format.
Definition Texture.hpp:80
u32 width
Texture width in pixels.
Definition Texture.hpp:76
TextureWrap wrapT
Vertical wrap mode.
Definition Texture.hpp:88
TextureFilter magFilter
Magnification filter.
Definition Texture.hpp:84
u32 layers
Above 1, a 2D texture array of this many layers (see TextureDesc::layers).
Definition Texture.hpp:92