NDEVR
API Documentation
ViewPortSmoothShaderLogic.h
1#pragma once
2#include <NDEVR/GLComputeShader.h>
3#include <NDEVR/Matrix.h>
4#include <NDEVR/PointContainer.h>
5#include <NDEVR/RGBColor.h>
6namespace NDEVR
7{
15 class ViewPortSmoothShaderLogic : public GLComputeShader
16 {
17 public:
18 ViewPortSmoothShaderLogic();
22 struct alignas(16) UBO {
23 // 0..63
24 Matrix<fltp04> inv_view;
25
26 // 64..111 (vec2s back-to-back)
30 Vector<2, fltp04> size_f;
31 Vector<2, fltp04> size_fr;
32 Vector<2, uint04> size_i;
33
34 // 112..127
35 Vector<4, fltp04> k12p1p2;
36
37 // 128..143
38 Vector<4, fltp04> cam_pos4;
39
40 // 144..159 (four floats)
41 float k3, max_dist2, max_dist2_relax, max_error_percent_sqrd;
42
43 // 160..175
44 Vector<4, uint04> weightsA; // record, discard, max, reduce
45
46 // 192..207
47 Vector<4, uint04> counts;
48 };
49 static_assert(sizeof(UBO) == 192, "UBO size mismatch");
50 static_assert(alignof(UBO) == 16, "bad alignment");
51 static_assert(offsetof(UBO, pp) == 64, "bad offset pp");
52 static_assert(offsetof(UBO, size_i) == 104, "bad offset size_i");
53 static_assert(offsetof(UBO, k12p1p2) == 112, "bad offset k12p1p2");
54 static_assert(offsetof(UBO, cam_pos4) == 128, "bad offset cam_pos4");
55 static_assert(offsetof(UBO, k3) == 144, "bad offset k3");
56 static_assert(offsetof(UBO, weightsA) == 160, "bad offset weightsA");
57 static_assert(offsetof(UBO, counts) == 176, "bad offset counts");
60 void runAll(uint04 count);
65
70 template<class t_point_type>
72 {
73 //wait for previous calc to finish
74 m_gl->glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT | GL_BUFFER_UPDATE_BARRIER_BIT);
75 m_temp_idx.setSize(m_last_size);
76 m_colors.setSize(m_last_size);
77 active.setSize(m_last_size);
78 readBytes(m_ssboOutIdx, m_temp_idx.begin(), GLsizeiptr(sizeof(uint04) * active.size()));
79 readBytes(m_ssboInW, active.weights.begin(), GLsizeiptr(sizeof(uint04) * active.size()));
80 readBytes(m_ssboInPos, active.locations.begin(), GLsizeiptr(sizeof(Vector<3, fltp04>) * active.size()));
81 readBytes(m_ssboInColor, m_colors.begin(), GLsizeiptr(sizeof(uint04) * active.size()));
82 uint04 offset = 0;
83 const uint04 count = active.size();
84 for (uint04 i = 0; i < count; i++)
85 {
86 active.data[i].setColor(m_colors[i]);
87 if (m_temp_idx[i] == 0)
88 {
89 if (active.weights[i] >= record_weight)
90 {
91 inactive.locations.add(active.locations[i]);
92 inactive.data.add(active.data[i]);
93 inactive.weights.add(active.weights[i]);
94 }
95 }
96 else
97 {
98 active.locations[offset] = active.locations[i];
99 active.data[offset] = active.data[i];
100 active.weights[offset] = active.weights[i];
101 ++offset;
102 }
103 }
104 active.setSize(offset);
105 }
106
109 template<class t_point_type>
110 void uploadInputs(const PointContainer<t_point_type>& active, uint04 pix_count)
111 {
112 if (!m_ssboPxKey)
113 m_gl->glGenBuffers(1, &m_ssboPxKey);
114 m_colors.setSize(active.size());
115 for (uint04 i = 0; i < m_colors.size(); i++)
116 m_colors[i] = active.data[i].color();
117 m_gl->glBindBuffer(GL_SHADER_STORAGE_BUFFER, m_ssboPxKey);
118 if (m_px_key_size < pix_count)
119 {
120 m_px_key_size = pix_count;
121 m_gl->glBufferData(GL_SHADER_STORAGE_BUFFER
122 , GLsizeiptr(sizeof(uint04) * pix_count)
123 , nullptr, GL_DYNAMIC_DRAW);
124 }
125 const GLuint fill = 0xFFFFFFFFu;
126 m_gl->glClearBufferData(GL_SHADER_STORAGE_BUFFER, GL_R32UI,
127 GL_RED_INTEGER, GL_UNSIGNED_INT, &fill);
128
129 if (m_out_size < active.size())
130 {
131 m_out_size = 2 * active.size();
132 if (!m_ssboOutIdx)
133 m_gl->glGenBuffers(1, &m_ssboOutIdx);
134 m_gl->glBindBuffer(GL_SHADER_STORAGE_BUFFER, m_ssboOutIdx);
135 m_gl->glBufferData(GL_SHADER_STORAGE_BUFFER
136 , GLsizeiptr(sizeof(uint04)) * m_out_size
137 , nullptr, GL_DYNAMIC_DRAW);
138 //position
139 if (!m_ssboInPos)
140 m_gl->glGenBuffers(1, &m_ssboInPos);
141 m_gl->glBindBuffer(GL_SHADER_STORAGE_BUFFER, m_ssboInPos);
142 m_gl->glBufferData(GL_SHADER_STORAGE_BUFFER
143 , GLsizeiptr(sizeof(Vertex<3, fltp04>)) * m_out_size
144 , nullptr, GL_DYNAMIC_DRAW);
145 //color
146 if (!m_ssboInColor)
147 m_gl->glGenBuffers(1, &m_ssboInColor);
148 m_gl->glBindBuffer(GL_SHADER_STORAGE_BUFFER, m_ssboInColor);
149 m_gl->glBufferData(GL_SHADER_STORAGE_BUFFER
150 , GLsizeiptr(sizeof(uint04)) * m_out_size
151 , nullptr, GL_DYNAMIC_DRAW);
152 //weight
153 if (!m_ssboInW)
154 m_gl->glGenBuffers(1, &m_ssboInW);
155 m_gl->glBindBuffer(GL_SHADER_STORAGE_BUFFER, m_ssboInW);
156 m_gl->glBufferData(GL_SHADER_STORAGE_BUFFER
157 , GLsizeiptr(sizeof(uint04)) * m_out_size
158 , nullptr, GL_DYNAMIC_DRAW);
159 }
160 m_gl->glBindBuffer(GL_SHADER_STORAGE_BUFFER, m_ssboInPos);
161 m_gl->glBufferSubData(GL_SHADER_STORAGE_BUFFER
162 , 0, GLsizeiptr(sizeof(Vertex<3, fltp04>)) * active.size()
163 , active.locations.begin());
164
165 m_gl->glBindBuffer(GL_SHADER_STORAGE_BUFFER, m_ssboInColor);
166 m_gl->glBufferSubData(GL_SHADER_STORAGE_BUFFER
167 , 0, GLsizeiptr(sizeof(uint04)) * active.size()
168 , m_colors.begin());
169
170 m_gl->glBindBuffer(GL_SHADER_STORAGE_BUFFER, m_ssboInW);
171 m_gl->glBufferSubData(GL_SHADER_STORAGE_BUFFER
172 , 0, GLsizeiptr(sizeof(uint04)) * active.size()
173 , active.weights.begin());
174 m_last_size = active.size();
175
176 }
177
179 void setUBO(const UBO& u);
182 void setQtGL(QOpenGLFunctions_4_3_Core* gl);
189 static void ReadBytes(QOpenGLFunctions_4_3_Core* gl, GLenum target, GLuint buf, void* dst, GLsizeiptr bytes);
190 protected:
194 QOpenGLShaderProgram* m_shader;
195 GLuint m_csA = 0;
196 GLuint m_uboU = 0;
197 GLuint m_ssboInPos = 0;
198 GLuint m_ssboInColor = 0;
199 GLuint m_ssboInW = 0;
200 GLuint m_ssboPxKey = 0;
201 GLuint m_ssboOutIdx = 0;
204 };
205}
The equivelent of std::vector but with a bit more control.
Definition Buffer.hpp:58
void add(t_type &&object)
Adds object to the end of the buffer.
Definition Buffer.hpp:190
Templated logic for doing matrix multiplication.
Definition Matrix.hpp:182
A templated container for 3D point cloud data with per-point positions, typed attributes,...
Buffer< Vertex< 3, fltp04 > > locations
3D positions of each point
Buffer< uint04 > weights
Per-point confidence weights.
void setSize(uint04 size)
Resize all internal buffers to hold the given number of points.
Buffer< t_point_type > data
Per-point attribute data.
uint04 size() const
Get the number of points in the container.
A fixed-size array with N dimensions used as the basis for geometric and mathematical types.
Definition Vector.hpp:62
A point in N-dimensional space, used primarily for spatial location information.
Definition Vertex.hpp:44
GLuint m_uboU
Uniform buffer object handle.
uint04 m_last_size
Number of points in the last upload.
void buildShader()
Compile and link the compute shader program.
uint04 m_px_key_size
Allocated size of the per-pixel key buffer.
GLuint m_ssboInW
Input weight storage buffer.
Buffer< uint04 > m_temp_idx
CPU-side copy of the output index buffer.
Buffer< RGBColor > m_colors
CPU-side color buffer for GPU upload/download.
GLuint m_ssboOutIdx
Output index buffer indicating kept points.
uint04 m_out_size
Allocated size of the output index buffer.
GLuint m_ssboInPos
Input position storage buffer.
void runAll(uint04 count)
Dispatch the compute shader over the given number of points.
QOpenGLShaderProgram * m_shader
Compiled compute shader program.
void uploadInputs(const PointContainer< t_point_type > &active, uint04 pix_count)
Upload point cloud data to GPU storage buffers for the smooth pass.
void setUBO(const UBO &u)
Upload uniform buffer data to the GPU.
static void ReadBytes(QOpenGLFunctions_4_3_Core *gl, GLenum target, GLuint buf, void *dst, GLsizeiptr bytes)
Read bytes from a GPU buffer into CPU memory.
GLuint m_ssboInColor
Input color storage buffer.
GLuint m_ssboPxKey
Per-pixel key buffer for duplicate detection.
void createUBOBuffer()
Create the GPU uniform buffer object for shader parameters.
void setQtGL(QOpenGLFunctions_4_3_Core *gl)
Set the OpenGL function pointer table used by this shader.
void getInput(PointContainer< t_point_type > &active, PointContainer< t_point_type > &inactive, uint04 record_weight)
Read back smoothed points from the GPU, partitioning into active and inactive sets.
GLuint m_csA
Compute shader handle.
The primary namespace for the NDEVR SDK.
uint32_t uint04
-Defines an alias representing a 4 byte, unsigned integer -Can represent exact integer values 0 throu...
Uniform buffer object layout for the viewport smooth compute shader.