Estella 0.113.0
A fast 2D and 3D game engine powered by WebAssembly and ECS
Loading...
Searching...
No Matches
esengine::Texture Class Reference

2D texture for GPU rendering More...

#include <Texture.hpp>

Public Member Functions

 Texture ()=default
virtual ~Texture ()
 Texture (const Texture &)=delete
Texture & operator= (const Texture &)=delete
 Texture (Texture &&other) noexcept
Texture & operator= (Texture &&other) noexcept
bool adoptContent (Texture &from)
 Takes from's pixels behind this texture's handle; from is empty afterwards.
void bind (u32 slot=0) const
 Binds the texture to a texture unit.
void unbind () const
 Unbinds the texture.
void setData (std::span< const u8 > pixels)
 Updates texture pixel data from span.
void setData (const std::vector< u8 > &pixels)
 Updates texture pixel data from vector.
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.
TextureHandle handle () const
 Gets the device texture handle.
u32 getId () const
 Gets the texture handle's raw value, for command payloads and the WASM boundary.
u32 getWidth () const
 Gets the texture width in pixels.
u32 getHeight () const
 Gets the texture height in pixels.
TextureFormat getFormat () const
 Gets the pixel format.
bool operator== (const Texture &other) const
 Compares textures by GPU ID.
void setDataRaw (const void *data, u32 sizeBytes, bool flipY=false)
 Updates texture pixel data from raw pointer (internal use).

Static Public Member Functions

static Unique< Texture > create (GfxDevice &device, GfxContent content, const TextureSpecification &spec)
 Creates an empty texture from specification.
static Unique< Texture > create (GfxDevice &device, GfxContent content, u32 width, u32 height, std::span< const u8 > pixels, TextureFormat format=TextureFormat::RGBA8, bool flipY=false)
 Creates a texture from a span of pixel data.
static Unique< Texture > create (GfxDevice &device, GfxContent content, u32 width, u32 height, const std::vector< u8 > &pixels, TextureFormat format=TextureFormat::RGBA8, bool flipY=false)
 Creates a texture from pixel vector.
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), without taking it over.
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.
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).
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.
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 shot. No setDataRaw afterward — compressed data is immutable here.
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.

Detailed Description

2D texture for GPU rendering

Encapsulates an OpenGL/WebGL texture object. Supports creation from pixel data, files, or empty specifications.

// Create from pixel data
std::vector<u8> pixels = {...}; // RGBA data
auto texture = Texture::create(64, 64, std::span(pixels));
// Bind for rendering
texture->bind(0); // Bind to texture unit 0
static Unique< Texture > create(GfxDevice &device, GfxContent content, const TextureSpecification &spec)
Creates an empty texture from specification.
Definition Texture.cpp:83

Definition at line 116 of file Texture.hpp.

Constructor & Destructor Documentation

◆ Texture() [1/3]

esengine::Texture::Texture ( )
default

◆ ~Texture()

esengine::Texture::~Texture ( )
virtual

Definition at line 45 of file Texture.cpp.

References esengine::Invalid.

◆ Texture() [2/3]

esengine::Texture::Texture ( const Texture & )
delete

References Texture().

◆ Texture() [3/3]

esengine::Texture::Texture ( Texture && other)
noexcept

Definition at line 51 of file Texture.cpp.

References Texture(), esengine::Invalid, and esengine::None.

Member Function Documentation

◆ adoptContent()

bool esengine::Texture::adoptContent ( Texture & from)

Takes from's pixels behind this texture's handle; from is empty afterwards.

How owed content is paid: the provider loads it the ordinary way and hands the result over, so nothing that names this texture changes.

Definition at line 207 of file Texture.cpp.

References Texture(), and esengine::Invalid.

Referenced by esengine::resource::ResourceManager::adoptTextureContent().

◆ bind()

void esengine::Texture::bind ( u32 slot = 0) const

Binds the texture to a texture unit.

Parameters
slotTexture unit index (0-7 typical for WebGL)

Definition at line 248 of file Texture.cpp.

◆ borrow()

Unique< Texture > esengine::Texture::borrow ( GfxDevice & device,
TextureHandle texture,
u32 width,
u32 height )
static

Names a texture the device already holds for another owner (a render target's colour plane), without taking it over.

Definition at line 195 of file Texture.cpp.

References handle(), esengine::makeUnique(), esengine::RGBA8, and esengine::GfxDevice::textureDesc().

Referenced by esengine::resource::ResourceManager::wrapDeviceTexture().

◆ create() [1/3]

Unique< Texture > esengine::Texture::create ( GfxDevice & device,
GfxContent content,
const TextureSpecification & spec )
static

Creates an empty texture from specification.

Parameters
specTexture parameters
Returns
Unique pointer to the texture

Creates a texture with uninitialized pixel data. Use setData() to upload pixels later.

Definition at line 83 of file Texture.cpp.

References esengine::makeUnique().

Referenced by create(), esengine::resource::ResourceManager::createTexture(), and esengine::resource::ResourceManager::createTexture().

◆ create() [2/3]

Unique< Texture > esengine::Texture::create ( GfxDevice & device,
GfxContent content,
u32 width,
u32 height,
const std::vector< u8 > & pixels,
TextureFormat format = TextureFormat::RGBA8,
bool flipY = false )
static

Creates a texture from pixel vector.

Parameters
widthTexture width in pixels
heightTexture height in pixels
pixelsPixel data (size must match width*height*channels)
formatPixel format (default RGBA8)
flipYFlip vertically on upload (for web image data)
Returns
Unique pointer to the texture

Definition at line 109 of file Texture.cpp.

References create().

◆ create() [3/3]

Unique< Texture > esengine::Texture::create ( GfxDevice & device,
GfxContent content,
u32 width,
u32 height,
std::span< const u8 > pixels,
TextureFormat format = TextureFormat::RGBA8,
bool flipY = false )
static

Creates a texture from a span of pixel data.

Parameters
widthTexture width in pixels
heightTexture height in pixels
pixelsPixel data (size must match width*height*channels)
formatPixel format (default RGBA8)
Returns
Unique pointer to the texture

Definition at line 102 of file Texture.cpp.

References createRaw(), and ES_ASSERT.

◆ createArray()

Unique< Texture > esengine::Texture::createArray ( GfxDevice & device,
GfxContent content,
const TextureSpecification & spec,
const void * pixels )
static

Creates a 2D texture array from spec.layers layers of spec.width x spec.height.

Parameters
pixelsThe layers back to back, layer 0 first, rows bottom-up as the engine samples them; spec.width * spec.height * bpp * spec.layers bytes. Its contents are fixed: setData is refused.

Definition at line 92 of file Texture.cpp.

References esengine::makeUnique().

Referenced by esengine::resource::ResourceManager::createTextureArray().

◆ createCompressed()

Unique< Texture > esengine::Texture::createCompressed ( GfxDevice & device,
GfxContent content,
u32 width,
u32 height,
GfxCompressedFormat format,
std::span< const u8 > data,
u32 mipLevels = 1 )
static

Creates a block-compressed texture (KTX2 → ETC2/ASTC/BC), uploading the pre-compressed blocks in one shot. No setDataRaw afterward — compressed data is immutable here.

Parameters
dataCompressed block data for mip level 0.

Definition at line 152 of file Texture.cpp.

References esengine::ClampToEdge, esengine::GfxDevice::createCompressedTexture(), ES_LOG_ERROR, esengine::TextureDesc::height, esengine::Invalid, esengine::Linear, esengine::TextureDesc::magFilter, esengine::makeUnique(), esengine::TextureDesc::minFilter, esengine::TextureDesc::mipmaps, esengine::RGBA8, esengine::TextureDesc::width, esengine::TextureDesc::wrapS, and esengine::TextureDesc::wrapT.

Referenced by esengine::resource::ResourceManager::createCompressedTexture().

◆ createFromExternalId()

Unique< Texture > esengine::Texture::createFromExternalId ( GfxDevice & device,
GfxContent content,
u32 glTextureId,
u32 width,
u32 height,
TextureFormat format = TextureFormat::RGBA8 )
static

Hands the device a texture the host made with its own API; the device owns it from here.

Parameters
glTextureIdThe host's id for it (a WebGL texture registered with the GL layer)

Definition at line 179 of file Texture.cpp.

References esengine::TextureDesc::format, esengine::TextureDesc::height, esengine::GfxDevice::importExternalTexture(), esengine::makeUnique(), and esengine::TextureDesc::width.

Referenced by esengine::resource::ResourceManager::registerExternalTexture().

◆ createRaw()

Unique< Texture > esengine::Texture::createRaw ( GfxDevice & device,
GfxContent content,
u32 width,
u32 height,
const void * data,
TextureFormat format = TextureFormat::RGBA8,
bool flipY = false )
static

Creates a texture from raw pixel pointer (internal use).

Parameters
widthTexture width in pixels
heightTexture height in pixels
dataPointer to pixel data
formatPixel format (default RGBA8)
Returns
Unique pointer to the texture

Definition at line 114 of file Texture.cpp.

References esengine::ClampToEdge, esengine::TextureSpecification::format, esengine::TextureSpecification::generateMips, esengine::TextureSpecification::height, esengine::makeUnique(), esengine::TextureSpecification::width, esengine::TextureSpecification::wrapS, and esengine::TextureSpecification::wrapT.

Referenced by create().

◆ createVolume()

Unique< Texture > esengine::Texture::createVolume ( GfxDevice & device,
GfxContent content,
u32 width,
u32 height,
u32 depth,
const void * data,
TextureFormat format )
static

◆ getFormat()

TextureFormat esengine::Texture::getFormat ( ) const
inline

Gets the pixel format.

Definition at line 245 of file Texture.hpp.

◆ getHeight()

u32 esengine::Texture::getHeight ( ) const
inline

Gets the texture height in pixels.

Definition at line 242 of file Texture.hpp.

Referenced by esengine::SpritePlugin::collect(), and esengine::UIElementPlugin::collect().

◆ getId()

◆ getWidth()

u32 esengine::Texture::getWidth ( ) const
inline

Gets the texture width in pixels.

Definition at line 239 of file Texture.hpp.

Referenced by esengine::SpritePlugin::collect(), and esengine::UIElementPlugin::collect().

◆ handle()

TextureHandle esengine::Texture::handle ( ) const
inline

Gets the device texture handle.

Definition at line 233 of file Texture.hpp.

Referenced by borrow(), esengine::draw_meshWithUniforms(), and esengine::resource::ResourceManager::textureNativeId().

◆ operator=() [1/2]

Texture & esengine::Texture::operator= ( const Texture & )
delete

References Texture(), and esengine::RGBA8.

◆ operator=() [2/2]

Texture & esengine::Texture::operator= ( Texture && other)
noexcept

Definition at line 64 of file Texture.cpp.

References Texture(), esengine::Invalid, and esengine::None.

◆ operator==()

bool esengine::Texture::operator== ( const Texture & other) const
inline

Compares textures by GPU ID.

Parameters
otherTexture to compare with
Returns
True if same GPU texture

Definition at line 252 of file Texture.hpp.

References Texture().

◆ setData() [1/2]

void esengine::Texture::setData ( const std::vector< u8 > & pixels)

Updates texture pixel data from vector.

Parameters
pixelsNew pixel data

Definition at line 262 of file Texture.cpp.

References setData().

◆ setData() [2/2]

void esengine::Texture::setData ( std::span< const u8 > pixels)

Updates texture pixel data from span.

Parameters
pixelsNew pixel data

Definition at line 256 of file Texture.cpp.

References ES_ASSERT, and setDataRaw().

Referenced by setData().

◆ setDataRaw()

void esengine::Texture::setDataRaw ( const void * data,
u32 sizeBytes,
bool flipY = false )

Updates texture pixel data from raw pointer (internal use).

Parameters
dataPointer to pixel data
sizeBytesSize of data in bytes

Definition at line 266 of file Texture.cpp.

References ES_LOG_ERROR.

Referenced by setData().

◆ unbind()

void esengine::Texture::unbind ( ) const

Unbinds the texture.

Definition at line 252 of file Texture.cpp.

References esengine::Invalid.

◆ updateSubRegion()

void esengine::Texture::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.

Lets the dynamic glyph atlas pack glyphs one-by-one without re-uploading the whole atlas. The sub-rect must lie fully inside the texture and data must be tightly packed in this texture's format.

Parameters
xoffset,yoffsettop-left of the sub-rect, in texels
width,heightsub-rect size, in texels
data,sizeBytespixel buffer matching the texture's format

Definition at line 280 of file Texture.cpp.

References ES_LOG_ERROR.


The documentation for this class was generated from the following files:
  • /home/runner/work/estella/estella/src/esengine/renderer/rhi/Texture.hpp
  • /home/runner/work/estella/estella/src/esengine/renderer/rhi/Texture.cpp