Visual Computing Library  devel
Loading...
Searching...
No Matches
uniform.h
1/*****************************************************************************
2 * VCLib *
3 * Visual Computing Library *
4 * *
5 * Copyright(C) 2021-2026 *
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_UNIFORM_H
24#define VCL_BGFX_UNIFORM_H
25
26#include <vclib/base.h>
27
28#include <bgfx/bgfx.h>
29
30#include <string>
31
32namespace vcl {
33
43{
44 bgfx::UniformHandle mUniformHandle = BGFX_INVALID_HANDLE;
45 std::string mUniformName;
46 bgfx::UniformType::Enum mUniformType = bgfx::UniformType::Count;
47 uint16_t mArraySize = 1; // for array uniforms, the number of elements
48
49public:
53 Uniform() = default;
54
64 const std::string& name,
65 bgfx::UniformType::Enum type,
66 uint16_t size = 1) :
67 mUniformName(name), mUniformType(type), mArraySize(size)
68 {
69 mUniformHandle = bgfx::createUniform(name.c_str(), type, size);
70 }
71
72 // Copying a Uniform is not allowed
73 Uniform(const Uniform& oth) = delete;
74
80
85 {
86 if (bgfx::isValid(mUniformHandle))
87 bgfx::destroy(mUniformHandle);
88 }
89
90 // Copying a Uniform is not allowed
91 Uniform& operator=(const Uniform& oth) = delete;
92
99 {
100 swap(oth);
101 return *this;
102 }
103
109 {
110 using std::swap;
111 swap(mUniformHandle, oth.mUniformHandle);
112 swap(mUniformName, oth.mUniformName);
113 swap(mUniformType, oth.mUniformType);
114 swap(mArraySize, oth.mArraySize);
115 }
116
122 friend void swap(Uniform& a, Uniform& b) { a.swap(b); }
123
129 bool isValid() const { return bgfx::isValid(mUniformHandle); }
130
135 bgfx::UniformHandle handle() const { return mUniformHandle; }
136
141 const std::string& name() const { return mUniformName; }
142
147 bgfx::UniformType::Enum type() const { return mUniformType; }
148
154 uint16_t arraySize() const { return mArraySize; }
155
165 void bind(const void* data) const
166 {
167 bgfx::setUniform(mUniformHandle, data, mArraySize);
168 }
169
183 void bind(const void* data, uint16_t numElements) const
184 {
185 assert(numElements <= mArraySize);
186 bgfx::setUniform(mUniformHandle, data, numElements);
187 }
188};
189
190} // namespace vcl
191
192#endif // VCL_BGFX_UNIFORM_H
A class representing a line segment in n-dimensional space. The class is parameterized by a PointConc...
Definition segment.h:41
The Uniform class wraps a bgfx::UniformHandle and provides a simple interface to set the uniform data...
Definition uniform.h:43
Uniform()=default
Default constructor. Creates an invalid Uniform.
void bind(const void *data, uint16_t numElements) const
Sets the uniform data for the current shader program, specifying the number of elements for array uni...
Definition uniform.h:183
Uniform & operator=(Uniform &&oth)
Move assignment operator.
Definition uniform.h:98
uint16_t arraySize() const
Returns the array size of the uniform (number of elements for array uniforms, 1 for non-array uniform...
Definition uniform.h:154
friend void swap(Uniform &a, Uniform &b)
Swaps two Uniform objects.
Definition uniform.h:122
const std::string & name() const
Returns the name of the uniform.
Definition uniform.h:141
void swap(Uniform &oth)
Swaps the content of this Uniform with another.
Definition uniform.h:108
bgfx::UniformHandle handle() const
Returns the underlying bgfx::UniformHandle.
Definition uniform.h:135
bgfx::UniformType::Enum type() const
Returns the type of the uniform.
Definition uniform.h:147
void bind(const void *data) const
Sets the uniform data for the current shader program.
Definition uniform.h:165
bool isValid() const
Checks if the Uniform is valid (i.e., if it has a valid bgfx::UniformHandle).
Definition uniform.h:129
Uniform(Uniform &&oth)
Move constructor.
Definition uniform.h:79
~Uniform()
Destructor. Destroys the underlying bgfx::UniformHandle.
Definition uniform.h:84
Uniform(const std::string &name, bgfx::UniformType::Enum type, uint16_t size=1)
Creates a new shader uniform.
Definition uniform.h:63