23#ifndef VCL_BGFX_TEXTURE_H
24#define VCL_BGFX_TEXTURE_H
26#include <vclib/base.h>
27#include <vclib/space/core.h>
29#include <vclib/bgfx/context.h>
83 if (bgfx::isValid(mTextureHandle))
84 bgfx::destroy(mTextureHandle);
118 swap(mTextureHandle,
other.mTextureHandle);
133 bool isValid()
const {
return bgfx::isValid(mTextureHandle); }
151 if (bgfx::isValid(mTextureHandle))
152 bgfx::destroy(mTextureHandle);
157 if (
image.m_cubeMap) {
158 mTextureHandle = bgfx::createTextureCube(
162 bgfx::TextureFormat::Enum(
image.m_format),
167 mTextureHandle = bgfx::createTexture2D(
172 bgfx::TextureFormat::Enum(
image.m_format),
182 if (
image.m_cubeMap) {
183 mTextureHandle = bgfx::createTextureCube(
187 bgfx::TextureFormat::Enum(
image.m_format),
190 for (
uint8_t face = 0; face < 6; face++)
191 copyMip0ToTexture(
image, face);
194 mTextureHandle = bgfx::createTexture2D(
199 bgfx::TextureFormat::Enum(
image.m_format),
202 copyMip0ToTexture(
image);
227 bgfx::TextureFormat::Enum format = bgfx::TextureFormat::RGBA8,
240 static_cast<bimg::TextureFormat::Enum
>(toUnderlying(format)));
268 const bgfx::Memory* texture,
272 bgfx::TextureFormat::Enum format = bgfx::TextureFormat::RGBA8,
276 if (bgfx::isValid(mTextureHandle))
277 bgfx::destroy(mTextureHandle);
280 mTextureHandle = bgfx::createTextureCube(
283 mTextureHandle = bgfx::createTexture2D(
304 if (bgfx::isValid(mTextureHandle) && bgfx::isValid(
samplerHandle)) {
321 bgfx::Access::Enum
access,
322 bgfx::TextureFormat::Enum format = bgfx::TextureFormat::Count)
const
324 if (bgfx::isValid(mTextureHandle))
344 if (
tex.minFilter() == NEAREST ||
345 tex.minFilter() == NEAREST_MIPMAP_LINEAR ||
346 tex.minFilter() == NEAREST_MIPMAP_NEAREST)
350 if (
tex.minFilter() == NEAREST_MIPMAP_NEAREST ||
351 tex.minFilter() == LINEAR_MIPMAP_NEAREST)
359 if (
tex.wrapU() == CLAMP_TO_EDGE)
361 else if (
tex.wrapU() == MIRRORED_REPEAT)
364 if (
tex.wrapV() == CLAMP_TO_EDGE)
366 else if (
tex.wrapV() == MIRRORED_REPEAT)
373 void copyMip0ToTexture(
const bimg::ImageContainer&
image,
uint8_t face = 0)
378 if (
image.m_cubeMap) {
379 bgfx::updateTextureCube(
388 bgfxCopyMemory(
mip.m_data,
mip.m_size));
391 bgfx::updateTexture2D(
399 bgfxCopyMemory(mip.m_data, mip.m_size));
403 static const bgfx::Memory* bgfxCopyMemory(
407 auto [buffer, releaseFn] =
408 Context::getAllocatedBufferAndReleaseFn<uint8_t>(size);
410 std::copy(data, data + size, buffer);
412 return bgfx::makeRef(buffer, size, releaseFn);
The Point class represents an N-dimensional point containing N scalar values.
Definition point.h:55
ScalarType & x()
Returns a reference to the x-component of the Point object.
Definition point.h:128
ScalarType & y()
Returns a reference to the y-component of the Point object.
Definition point.h:150
A class representing a line segment in n-dimensional space. The class is parameterized by a PointConc...
Definition segment.h:41
Describes the properties of a texture, such as its source path and rendering parameters.
Definition texture_descriptor.h:42
MinificationFilter
Defines the texture minification filter modes, following the glTF 2.0 specification....
Definition texture_descriptor.h:50
@ NEAREST
Nearest neighbor filtering.
WrapMode
Defines the texture wrapping modes for S (U) and T (V) coordinates, following the glTF 2....
Definition texture_descriptor.h:81
Manages a BGFX texture.
Definition texture.h:45
bool isValid() const
Checks if the Texture holds valid BGFX texture handle.
Definition texture.h:133
void bindForCompute(uint stage, uint mip, bgfx::Access::Enum access, bgfx::TextureFormat::Enum format=bgfx::TextureFormat::Count) const
Binds the texture to a texture stage for compute shaders.
Definition texture.h:318
Texture(const Texture &other)=delete
Deleted copy constructor.
Texture & operator=(Texture &&other) noexcept
Move assignment operator.
Definition texture.h:104
void set(const bgfx::Memory *texture, const vcl::Point2i &size, bool hasMips, uint nLayers, bgfx::TextureFormat::Enum format=bgfx::TextureFormat::RGBA8, bool isCubemap=false, uint64_t flags=BGFX_TEXTURE_NONE|BGFX_SAMPLER_NONE)
Creates a 2D texture from a bgfx::Memory reference.
Definition texture.h:267
friend void swap(Texture &a, Texture &b)
Swaps two Texture objects.
Definition texture.h:126
Texture & operator=(const Texture &other)=delete
Deleted copy assignment operator.
void set(const bimg::ImageContainer &image, bool hasMips, uint64_t flags)
Creates a texture from a bgfx::ImageContainer.
Definition texture.h:145
Texture()=default
Default constructor.
static uint samplerFlagsFromTexture(const TextureDescriptor &tex)
Generates BGFX sampler flags based on the texture's filtering and wrapping modes.
Definition texture.h:336
void swap(Texture &other)
Swaps the content of this object with another Texture.
Definition texture.h:115
Texture(Texture &&other) noexcept
Move constructor.
Definition texture.h:73
~Texture()
Destructor.
Definition texture.h:81
void bind(uint stage, bgfx::UniformHandle samplerHandle, uint samplerFlags=UINT32_MAX) const
Binds the texture to a texture stage for rendering.
Definition texture.h:299
void set(const void *data, const vcl::Point2i &size, bool hasMips=false, uint64_t flags=BGFX_TEXTURE_NONE|BGFX_SAMPLER_NONE, bgfx::TextureFormat::Enum format=bgfx::TextureFormat::RGBA8, bool isCubemap=false, bgfx::ReleaseFn releaseFn=nullptr)
Creates a 2D texture from raw pixel data.
Definition texture.h:222