Visual Computing Library
Loading...
Searching...
No Matches
texture_unit.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_UNIT_H
24#define VCL_BGFX_TEXTURE_UNIT_H
25
26#include <vclib/space/core/point.h>
27#include <vclib/types.h>
28
29#include <bgfx/bgfx.h>
30
31namespace vcl {
32
34{
35 bgfx::TextureHandle mTextureHandle = BGFX_INVALID_HANDLE;
36 bgfx::UniformHandle mUniformHandle = BGFX_INVALID_HANDLE;
37
38public:
44 TextureUnit() = default;
45
46 // Copying an TextureUnit is not allowed
47 TextureUnit(const TextureUnit& other) = delete;
48
57
64 {
65 if (bgfx::isValid(mTextureHandle))
66 bgfx::destroy(mTextureHandle);
67 if (bgfx::isValid(mUniformHandle))
68 bgfx::destroy(mUniformHandle);
69 }
70
71 // Copying a TextureUnit is not allowed
72 TextureUnit& operator=(const TextureUnit& other) = delete;
73
83 {
84 swap(other);
85 return *this;
86 }
87
94 {
95 using std::swap;
96 swap(mTextureHandle, other.mTextureHandle);
97 swap(mUniformHandle, other.mUniformHandle);
98 }
99
100 friend void swap(TextureUnit& a, TextureUnit& b) { a.swap(b); }
101
107 bool isValid() const
108 {
109 return bgfx::isValid(mTextureHandle) && bgfx::isValid(mUniformHandle);
110 }
111
112 void set(
113 const void* data,
114 const vcl::Point2i& sizes,
115 const std::string& samplerName,
116 bool hasMips = false,
117 bgfx::ReleaseFn releaseFn = nullptr)
118 {
119 set(bgfx::makeRef(data, sizes.x() * sizes.y() * 4, releaseFn),
120 sizes,
122 hasMips,
123 1);
124 }
125
126 void set(
127 const bgfx::Memory* texture,
128 const vcl::Point2i& sizes,
129 const std::string& samplerName,
130 bool hasMips,
131 uint nLayers,
132 bgfx::TextureFormat::Enum format = bgfx::TextureFormat::RGBA8,
133 uint flags = BGFX_TEXTURE_NONE)
134 {
135 if (bgfx::isValid(mTextureHandle))
136 bgfx::destroy(mTextureHandle);
137 if (bgfx::isValid(mUniformHandle))
138 bgfx::destroy(mUniformHandle);
139
140 mTextureHandle = bgfx::createTexture2D(
141 sizes.x(), sizes.y(), hasMips, nLayers, format, flags, texture);
142 mUniformHandle = bgfx::createUniform(
143 samplerName.c_str(), bgfx::UniformType::Sampler);
144 }
145
146 void bind(uint stage) const
147 {
148 if (bgfx::isValid(mTextureHandle) && bgfx::isValid(mUniformHandle)) {
149 bgfx::setTexture(stage, mUniformHandle, mTextureHandle);
150 }
151 }
152};
153
154} // namespace vcl
155
156#endif // VCL_BGFX_TEXTURE_UNIT_H
The Point class represents an N-dimensional point containing N scalar values.
Definition point.h:58
ScalarType & x()
Returns a reference to the x-component of the Point object.
Definition point.h:131
ScalarType & y()
Returns a reference to the y-component of the Point object.
Definition point.h:153
A class representing a line segment in n-dimensional space. The class is parameterized by a PointConc...
Definition segment.h:43
Definition texture_unit.h:34
~TextureUnit()
Destructor.
Definition texture_unit.h:63
bool isValid() const
Check if the TextureUnit is valid.
Definition texture_unit.h:107
TextureUnit()=default
Empty constructor.
TextureUnit & operator=(TextureUnit &&other) noexcept
Move assignment operator.
Definition texture_unit.h:82
void swap(TextureUnit &other)
Swap the content of this object with another TextureUnit object.
Definition texture_unit.h:93
TextureUnit(TextureUnit &&other) noexcept
Move constructor.
Definition texture_unit.h:56