Visual Computing Library
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/types.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:
49 Uniform() = default;
50
51 Uniform(const std::string& name, bgfx::UniformType::Enum type) :
52 mUniformName(name), mUniformType(type)
53 {
54 mUniformHandle = bgfx::createUniform(name.c_str(), type);
55 }
56
57 Uniform(const Uniform& oth) :
58 mUniformName(oth.mUniformName), mUniformType(oth.mUniformType)
59 {
60 mUniformHandle =
61 bgfx::createUniform(mUniformName.c_str(), mUniformType);
62 }
63
64 Uniform(Uniform&& oth) { swap(oth); }
65
66 ~Uniform()
67 {
68 if (bgfx::isValid(mUniformHandle))
69 bgfx::destroy(mUniformHandle);
70 }
71
72 bgfx::UniformHandle handle() const { return mUniformHandle; }
73
74 const std::string& name() const { return mUniformName; }
75
76 bgfx::UniformType::Enum type() const { return mUniformType; }
77
78 void bind(const void* data) const
79 {
80 bgfx::setUniform(mUniformHandle, data);
81 }
82
83 void swap(Uniform& oth)
84 {
85 using std::swap;
86 swap(mUniformHandle, oth.mUniformHandle);
87 swap(mUniformName, oth.mUniformName);
88 swap(mUniformType, oth.mUniformType);
89 }
90
91 friend void swap(Uniform& a, Uniform& b) { a.swap(b); }
92
93 Uniform& operator=(Uniform oth)
94 {
95 swap(oth);
96 return *this;
97 }
98
99 static float uintBitsToFloat(uint bits)
100 {
101 union
102 {
103 uint i;
104 float f;
105 } u;
106
107 u.i = bits;
108 return u.f;
109 }
110
111 static uint floatToUintBits(float f)
112 {
113 union
114 {
115 uint i;
116 float f;
117 } u;
118
119 u.f = f;
120 return u.i;
121 }
122};
123
124} // namespace vcl
125
126#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:43
The Uniform class wraps a bgfx::UniformHandle and provides a simple interface to set the uniform data...
Definition uniform.h:43