Visual Computing Library  devel
Loading...
Searching...
No Matches
texture.h
1/*****************************************************************************
2 * VCLib *
3 * Visual Computing Library *
4 * *
5 * Copyright(C) 2021-2025 *
6 * Visual Computing Lab *
7 * ISTI - Italian National Research Council *
8 * *
9 * All rights reserved. *
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the Mozilla Public License Version 2.0 as published *
13 * by the Mozilla Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * Mozilla Public License Version 2.0 *
20 * (https://www.mozilla.org/en-US/MPL/2.0/) for more details. *
21 ****************************************************************************/
22
23#ifndef VCL_BGFX_TEXTURE_H
24#define VCL_BGFX_TEXTURE_H
25
26#include <vclib/base.h>
27#include <vclib/space/core.h>
28
29#include <bgfx/bgfx.h>
30#include <bimg/bimg.h>
31
32namespace vcl {
33
43{
44 bgfx::TextureHandle mTextureHandle = BGFX_INVALID_HANDLE;
45
46public:
53 Texture() = default;
54
61 Texture(const Texture& other) = delete;
62
71 Texture(Texture&& other) noexcept { swap(other); }
72
80 {
81 if (bgfx::isValid(mTextureHandle))
82 bgfx::destroy(mTextureHandle);
83 }
84
91 Texture& operator=(const Texture& other) = delete;
92
103 {
104 swap(other);
105 return *this;
106 }
107
114 {
115 using std::swap;
116 swap(mTextureHandle, other.mTextureHandle);
117 }
118
124 friend void swap(Texture& a, Texture& b) { a.swap(b); }
125
131 bool isValid() const { return bgfx::isValid(mTextureHandle); }
132
146 void set(
147 const void* data,
148 const vcl::Point2i& size,
149 bool hasMips = false,
151 bgfx::ReleaseFn releaseFn = nullptr)
152 {
153 uint32_t sz = bimg::imageGetSize(
154 nullptr,
155 size.x(),
156 size.y(),
157 1,
158 false,
159 hasMips,
160 1,
161 bimg::TextureFormat::RGBA8);
162 set(bgfx::makeRef(data, sz, releaseFn),
163 size,
164 hasMips,
165 1,
166 bgfx::TextureFormat::RGBA8,
167 flags);
168 }
169
186 void set(
187 const bgfx::Memory* texture,
188 const vcl::Point2i& size,
189 bool hasMips,
191 bgfx::TextureFormat::Enum format = bgfx::TextureFormat::RGBA8,
193 {
194 if (bgfx::isValid(mTextureHandle))
195 bgfx::destroy(mTextureHandle);
196
197 mTextureHandle = bgfx::createTexture2D(
198 size.x(), size.y(), hasMips, nLayers, format, flags, texture);
199 }
200
213 void bind(
214 uint stage,
215 bgfx::UniformHandle samplerHandle,
217 {
218 if (bgfx::isValid(mTextureHandle) && bgfx::isValid(samplerHandle)) {
219 bgfx::setTexture(
220 stage, samplerHandle, mTextureHandle, samplerFlags);
221 }
222 }
223
233 {
236
237 uint flags = BGFX_SAMPLER_NONE;
238
239 // set minification filter - bgfx default is linear
240 if (tex.minFilter() == NEAREST ||
241 tex.minFilter() == NEAREST_MIPMAP_LINEAR ||
242 tex.minFilter() == NEAREST_MIPMAP_NEAREST)
243 flags |= BGFX_SAMPLER_MIN_POINT;
244
245 // set mipmap filter - bgfx default is linear
246 if (tex.minFilter() == NEAREST_MIPMAP_NEAREST ||
247 tex.minFilter() == LINEAR_MIPMAP_NEAREST)
248 flags |= BGFX_SAMPLER_MIP_POINT;
249
250 // set magnification filter - bgfx default is linear
252 flags |= BGFX_SAMPLER_MAG_POINT;
253
254 // set wrap modes - bgfx default is repeat
255 if (tex.wrapU() == CLAMP_TO_EDGE)
256 flags |= BGFX_SAMPLER_U_CLAMP;
257 else if (tex.wrapU() == MIRRORED_REPEAT)
258 flags |= BGFX_SAMPLER_U_MIRROR;
259
260 if (tex.wrapV() == CLAMP_TO_EDGE)
261 flags |= BGFX_SAMPLER_V_CLAMP;
262 else if (tex.wrapV() == MIRRORED_REPEAT)
263 flags |= BGFX_SAMPLER_V_MIRROR;
264
265 return flags;
266 }
267};
268
269} // namespace vcl
270
271#endif // VCL_BGFX_TEXTURE_H
A class representing a box in N-dimensional space.
Definition box.h:46
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
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:43
bool isValid() const
Checks if the Texture holds valid BGFX texture handle.
Definition texture.h:131
Texture(const Texture &other)=delete
Deleted copy constructor.
Texture & operator=(Texture &&other) noexcept
Move assignment operator.
Definition texture.h:102
void set(const bgfx::Memory *texture, const vcl::Point2i &size, bool hasMips, uint nLayers, bgfx::TextureFormat::Enum format=bgfx::TextureFormat::RGBA8, uint64_t flags=BGFX_TEXTURE_NONE|BGFX_SAMPLER_NONE)
Creates a 2D texture from a bgfx::Memory reference.
Definition texture.h:186
friend void swap(Texture &a, Texture &b)
Swaps two Texture objects.
Definition texture.h:124
Texture & operator=(const Texture &other)=delete
Deleted copy assignment operator.
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:232
void swap(Texture &other)
Swaps the content of this object with another Texture.
Definition texture.h:113
Texture(Texture &&other) noexcept
Move constructor.
Definition texture.h:71
~Texture()
Destructor.
Definition texture.h:79
void bind(uint stage, bgfx::UniformHandle samplerHandle, uint samplerFlags=UINT32_MAX) const
Binds the texture to a texture stage for rendering.
Definition texture.h:213
void set(const void *data, const vcl::Point2i &size, bool hasMips=false, uint64_t flags=BGFX_TEXTURE_NONE|BGFX_SAMPLER_NONE, bgfx::ReleaseFn releaseFn=nullptr)
Creates a 2D texture from raw pixel data.
Definition texture.h:146