Visual Computing Library  devel
Loading...
Searching...
No Matches
uniform.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_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
48public:
52 Uniform() = default;
53
60 Uniform(const std::string& name, bgfx::UniformType::Enum type) :
61 mUniformName(name), mUniformType(type)
62 {
63 mUniformHandle = bgfx::createUniform(name.c_str(), type);
64 }
65
66 // Copying a Uniform is not allowed
67 Uniform(const Uniform& oth) = delete;
68
74
79 {
80 if (bgfx::isValid(mUniformHandle))
81 bgfx::destroy(mUniformHandle);
82 }
83
84 // Copying a Uniform is not allowed
85 Uniform& operator=(const Uniform& oth) = delete;
86
93 {
94 swap(oth);
95 return *this;
96 }
97
103 {
104 using std::swap;
105 swap(mUniformHandle, oth.mUniformHandle);
106 swap(mUniformName, oth.mUniformName);
107 swap(mUniformType, oth.mUniformType);
108 }
109
115 friend void swap(Uniform& a, Uniform& b) { a.swap(b); }
116
121 bgfx::UniformHandle handle() const { return mUniformHandle; }
122
127 const std::string& name() const { return mUniformName; }
128
133 bgfx::UniformType::Enum type() const { return mUniformType; }
134
144 void bind(const void* data) const
145 {
146 bgfx::setUniform(mUniformHandle, data);
147 }
148
156 {
157 union
158 {
159 uint i;
160 float f;
161 } u;
162
163 u.i = bits;
164 return u.f;
165 }
166
173 static uint floatToUintBits(float f)
174 {
175 union
176 {
177 uint i;
178 float f;
179 } u;
180
181 u.f = f;
182 return u.i;
183 }
184};
185
186} // namespace vcl
187
188#endif // VCL_BGFX_UNIFORM_H
A class representing a box in N-dimensional space.
Definition box.h:46
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.
Uniform & operator=(Uniform &&oth)
Move assignment operator.
Definition uniform.h:92
Uniform(const std::string &name, bgfx::UniformType::Enum type)
Creates a new shader uniform.
Definition uniform.h:60
friend void swap(Uniform &a, Uniform &b)
Swaps two Uniform objects.
Definition uniform.h:115
static float uintBitsToFloat(uint bits)
Utility function to reinterpret the bits of an unsigned integer as a float.
Definition uniform.h:155
const std::string & name() const
Returns the name of the uniform.
Definition uniform.h:127
void swap(Uniform &oth)
Swaps the content of this Uniform with another.
Definition uniform.h:102
bgfx::UniformHandle handle() const
Returns the underlying bgfx::UniformHandle.
Definition uniform.h:121
bgfx::UniformType::Enum type() const
Returns the type of the uniform.
Definition uniform.h:133
static uint floatToUintBits(float f)
Utility function to reinterpret the bits of a float as an unsigned integer.
Definition uniform.h:173
void bind(const void *data) const
Sets the uniform data for the current shader program.
Definition uniform.h:144
Uniform(Uniform &&oth)
Move constructor.
Definition uniform.h:73
~Uniform()
Destructor. Destroys the underlying bgfx::UniformHandle.
Definition uniform.h:78