47 device_->deleteTexture(handle_);
52 : device_(other.device_)
53 , handle_(other.handle_)
54 , width_(other.width_)
55 , height_(other.height_)
56 , format_(other.format_)
57 , owns_(other.owns_) {
67 device_->deleteTexture(handle_);
69 device_ = other.device_;
70 handle_ = other.handle_;
71 width_ = other.width_;
72 height_ = other.height_;
73 format_ = other.format_;
85 texture->device_ = &device;
86 if (!texture->initialize(spec, content,
nullptr,
false)) {
95 texture->device_ = &device;
96 if (!texture->initialize(spec, content, pixels,
false)) {
103 std::span<const u8> pixels,
TextureFormat format,
bool flipY) {
104 [[maybe_unused]]
u32 expectedSize = width * height * bytesPerPixel(format);
105 ES_ASSERT(pixels.size() == expectedSize,
"Pixel data size mismatch");
106 return createRaw(device, content, width, height, pixels.data(), format, flipY);
110 const std::vector<u8>& pixels,
TextureFormat format,
bool flipY) {
111 return create(device, content, width, height, std::span<const u8>(pixels), format, flipY);
125 texture->device_ = &device;
126 if (!texture->initialize(spec, content, data, flipY)) {
145 texture->device_ = &device;
146 if (!texture->initialize(spec, content, data,
false)) {
156 texture->device_ = &device;
157 texture->width_ = width;
158 texture->height_ = height;
171 static_cast<u32>(data.size()), mipLevels);
173 ES_LOG_ERROR(
"Texture::createCompressed: failed for {}x{}", width, height);
184 desc.
format = toGfxPixelFormat(format);
187 texture->device_ = &device;
189 texture->width_ = width;
190 texture->height_ = height;
191 texture->format_ = format;
198 texture->device_ = &device;
199 texture->handle_ =
handle;
200 texture->width_ = width;
201 texture->height_ = height;
203 texture->owns_ =
false;
208 if (!device_ || !owns_ || !from.owns_ || !device_->adoptTextureContent(handle_, from.handle_)) {
211 width_ = from.width_;
212 height_ = from.height_;
213 format_ = from.format_;
240 ES_LOG_ERROR(
"Texture::initialize: createTexture failed for {}x{}", width_, height_);
244 ES_LOG_DEBUG(
"Created texture {}x{} (handle: {})", width_, height_,
static_cast<u32>(handle_));
249 if (device_) device_->bindTexture(slot, handle_);
257 [[maybe_unused]]
u32 expectedSize = width_ * height_ * bytesPerPixel(format_);
258 ES_ASSERT(pixels.size() == expectedSize,
"Pixel data size mismatch");
263 setData(std::span<const u8>(pixels));
270 u32 expectedSize = width_ * height_ * bytesPerPixel(format_);
271 if (sizeBytes < expectedSize) {
272 ES_LOG_ERROR(
"Texture::setDataRaw: data size {} < required {} for {}x{}; skipping upload to avoid OOB read",
273 sizeBytes, expectedSize, width_, height_);
277 device_->updateTexture(handle_, 0, 0, width_, height_, data, flipY);
281 const void* data,
u32 sizeBytes,
bool flipY) {
284 if (width > width_ || xoffset > width_ - width ||
285 height > height_ || yoffset > height_ - height) {
286 ES_LOG_ERROR(
"Texture::updateSubRegion: rect {}x{} at ({},{}) exceeds texture {}x{}; skipping",
287 width, height, xoffset, yoffset, width_, height_);
293 u32 expectedSize = width * height * bytesPerPixel(format_);
294 if (sizeBytes < expectedSize) {
295 ES_LOG_ERROR(
"Texture::updateSubRegion: data size {} < required {} for {}x{}; skipping to avoid OOB read",
296 sizeBytes, expectedSize, width, height);
300 device_->updateTexture(handle_,
static_cast<i32>(xoffset),
static_cast<i32>(yoffset),
301 width, height, data, flipY);
Abstract graphics device interface.
Logging utilities for ESEngine.
#define ES_LOG_ERROR(...)
Logs an error message.
#define ES_ASSERT(condition, message)
Assert macro (debug builds only).
#define ES_LOG_DEBUG(...)
GPU texture abstraction for 2D images.
A content policy. provider names who refills a Sourced object and key what it refills; both are the p...
Abstract graphics device interface.
TextureHandle importExternalTexture(u32 nativeId, const TextureDesc &desc, GfxContent content)
Adopts a texture a host created with the device's own API (a JS-side WebGL upload).
TextureHandle createTexture(const TextureDesc &desc, GfxContent content, const void *pixels)
Creates a 2D texture; uploads pixels (tightly packed, desc-sized) when non-null.
TextureHandle createCompressedTexture(const TextureDesc &desc, GfxContent content, GfxCompressedFormat format, const void *data, u32 byteLength, u32 mipLevels)
Creates a texture from pre-compressed GPU block data.
const TextureDesc * textureDesc(TextureHandle texture) const
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.
void bind(u32 slot=0) const
Binds the texture to a texture unit.
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 ...
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.
void setDataRaw(const void *data, u32 sizeBytes, bool flipY=false)
Updates texture pixel data from raw pointer (internal use).
void unbind() const
Unbinds the texture.
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 > create(GfxDevice &device, GfxContent content, const TextureSpecification &spec)
Creates an empty texture from specification.
void setData(std::span< const u8 > pixels)
Updates texture pixel data from span.
TextureHandle handle() const
Gets the device texture handle.
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),...
bool adoptContent(Texture &from)
Takes from's pixels behind this texture's handle; from is empty afterwards.
constexpr Unique< T > makeUnique(Args &&... args)
Creates a Unique pointer with the given arguments.
std::int32_t i32
Signed 32-bit integer.
@ ClampToEdge
Clamp to edge pixels.
@ Linear
Bilinear interpolation (smooth).
TextureFormat
Texture pixel format.
@ RGB8
3 channels, 8 bits each (24 bpp)
@ RGBA32F
Full-float storage (128 bpp); fetched by texel, never filtered.
@ SRGB8A8
sRGB-encoded RGBA8: hardware-linearized on sample (32 bpp)
@ RGBA16F
Half-float HDR (64 bpp); render use gated on supportsFloatTargets.
@ RGBA8
4 channels, 8 bits each (32 bpp)
@ Depth24
Depth buffer format (24 bits).
GfxCompressedFormat
GPU-compressed texture internal formats for createCompressedTexture.
std::unique_ptr< T > Unique
Unique ownership smart pointer.
std::uint32_t u32
Unsigned 32-bit integer.
u32 gfxBytesPerPixel(GfxPixelFormat fmt)
Bytes one pixel occupies in the CALLER's upload buffer — the single source both backends size a read ...
Immutable creation parameters of a 2D texture or 2D texture array.
Texture creation parameters.
TextureFilter minFilter
Minification filter.
bool volume
A 3D texture layers deep (see TextureDesc::volume).
TextureWrap wrapS
Horizontal wrap mode.
u32 height
Texture height in pixels.
bool generateMips
Generate mipmaps automatically.
TextureFormat format
Pixel format.
u32 width
Texture width in pixels.
TextureWrap wrapT
Vertical wrap mode.
TextureFilter magFilter
Magnification filter.
u32 layers
Above 1, a 2D texture array of this many layers (see TextureDesc::layers).