Skip to content

Commit 102ab0d

Browse files
committed
speeding up how transparency renders.
Currently when rendering transparent objects it uses up a ton of memory. this branch is to optimize the performance.
1 parent 0dfbeee commit 102ab0d

File tree

5 files changed

+93
-36
lines changed

5 files changed

+93
-36
lines changed

src/Interface/Modules/Render/ES/SRInterface.cc

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,8 @@ namespace SCIRun {
385385
uint64_t entityID = getEntityIDForName(pass.passName, port);
386386

387387
if (pass.renderType == Core::Datatypes::GeometryObject::RENDER_VBO_IBO)
388-
{
388+
{
389+
reorderIBO(pass);
389390
addVBOToEntity(entityID, pass.vboName);
390391
addIBOToEntity(entityID, pass.iboName);
391392
}
@@ -551,6 +552,55 @@ namespace SCIRun {
551552
mCore.addComponent(entityID, ibo);
552553
}
553554

555+
//------------------------------------------------------------------------------
556+
void SRInterface::reorderIBO(Core::Datatypes::GeometryObject::SpireSubPass& pass)
557+
{
558+
char* vbo_buffer = reinterpret_cast<char*>(pass.vbo.data->getBuffer());
559+
uint32_t* ibo_buffer = reinterpret_cast<uint32_t*>(pass.ibo.data->getBuffer());
560+
size_t num_triangles = pass.ibo.data->getBufferSize() / (sizeof(uint32_t) * 3);
561+
size_t stride_vbo = 0;
562+
for (auto a : pass.vbo.attributes)
563+
stride_vbo += a.sizeInBytes;
564+
565+
std::vector<DepthIndex> rel_depth(num_triangles);
566+
Core::Geometry::Vector dir(mCamera->getViewToWorld()[0][2], mCamera->getViewToWorld()[1][2], mCamera->getViewToWorld()[2][2]);
567+
568+
for (size_t j = 0; j < num_triangles; j++)
569+
{
570+
float* vertex1 = reinterpret_cast<float*>(vbo_buffer + stride_vbo * (ibo_buffer[j * 3]));
571+
Core::Geometry::Point node1(vertex1[0], vertex1[1], vertex1[2]);
572+
573+
float* vertex2 = reinterpret_cast<float*>(vbo_buffer + stride_vbo * (ibo_buffer[j * 3 + 1]));
574+
Core::Geometry::Point node2(vertex2[0], vertex2[1], vertex2[2]);
575+
576+
float* vertex3 = reinterpret_cast<float*>(vbo_buffer + stride_vbo * (ibo_buffer[j * 3 + 2]));
577+
Core::Geometry::Point node3(vertex3[0], vertex3[1], vertex3[2]);
578+
579+
rel_depth[j].mDepth = Core::Geometry::Dot(dir, node1) + Core::Geometry::Dot(dir, node2) + Core::Geometry::Dot(dir, node3);
580+
rel_depth[j].mIndex = j;
581+
}
582+
583+
std::sort(rel_depth.begin(), rel_depth.end());
584+
585+
int numPrimitives = pass.ibo.data->getBufferSize() / pass.ibo.indexSize;
586+
587+
std::vector<char> sorted_buffer(pass.ibo.data->getBufferSize());
588+
char* ibuffer = reinterpret_cast<char*>(pass.ibo.data->getBuffer());
589+
char* sbuffer = reinterpret_cast<char*>(&sorted_buffer[0]);
590+
size_t tri_size = pass.ibo.data->getBufferSize() / num_triangles;
591+
592+
for (size_t j = 0; j < num_triangles; j++)
593+
{
594+
memcpy(sbuffer + j * tri_size, ibuffer + rel_depth[j].mIndex * tri_size, tri_size);
595+
}
596+
597+
ren::IBOMan& iboMan = *mCore.getStaticComponent<ren::StaticIBOMan>()->instance;
598+
599+
auto iboData = iboMan.getIBOData(pass.iboName);
600+
601+
iboMan.addInMemoryIBO(sbuffer, pass.ibo.data->getBufferSize(), iboData.primMode, iboData.primType, iboData.numPrims, pass.iboName);
602+
}
603+
554604
//------------------------------------------------------------------------------
555605
void SRInterface::addShaderToEntity(uint64_t entityID, const std::string& shaderName)
556606
{

src/Interface/Modules/Render/ES/SRInterface.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,27 @@ namespace SCIRun {
139139

140140
private:
141141

142+
class DepthIndex {
143+
public:
144+
size_t mIndex;
145+
double mDepth;
146+
147+
DepthIndex() :
148+
mIndex(0),
149+
mDepth(0.0)
150+
{}
151+
152+
DepthIndex(size_t index, double depth) :
153+
mIndex(index),
154+
mDepth(depth)
155+
{}
156+
157+
bool operator<(const DepthIndex& di) const
158+
{
159+
return this->mDepth < di.mDepth;
160+
}
161+
};
162+
142163
class SRObject
143164
{
144165
public:
@@ -209,6 +230,8 @@ namespace SCIRun {
209230
// Adds an IBO to the given entityID.
210231
void addIBOToEntity(uint64_t entityID, const std::string& iboName);
211232

233+
void reorderIBO(Core::Datatypes::GeometryObject::SpireSubPass& pass);
234+
212235
// Adds a shader to the given entityID. Represents different materials
213236
// associated with different passes.
214237
void addShaderToEntity(uint64_t entityID, const std::string& shaderName);

src/Interface/Modules/Render/ES/systems/RenderTransBasicSys.cc

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -130,25 +130,27 @@ class RenderBasicSysTrans :
130130
{
131131
return;
132132
}
133-
134-
camera.front().data.projection;
135-
133+
/*
136134
char* vbo_buffer = reinterpret_cast<char*>(pass.front().vbo.data->getBuffer());
135+
uint32_t* ibo_buffer = reinterpret_cast<uint32_t*>(pass.front().ibo.data->getBuffer());
137136
size_t num_triangles = pass.front().ibo.data->getBufferSize() / (sizeof(uint32_t) * 3);
138-
size_t stride_vbo = pass.front().vbo.data->getBufferSize() / (num_triangles * 3);
137+
138+
size_t stride_vbo = 0;
139+
for (auto a : pass.front().vbo.attributes)
140+
stride_vbo += a.sizeInBytes;
139141
140142
std::vector<DepthIndex> rel_depth(num_triangles);
141143
Core::Geometry::Vector dir(camera.front().data.worldToView[0][2], camera.front().data.worldToView[1][2], camera.front().data.worldToView[2][2]);
142144
143145
for(size_t j = 0; j < num_triangles; j++)
144146
{
145-
float* vertex1 = reinterpret_cast<float*>(vbo_buffer + stride_vbo * (j * 3));
147+
float* vertex1 = reinterpret_cast<float*>(vbo_buffer + stride_vbo * (ibo_buffer[j * 3]));
146148
Core::Geometry::Point node1(vertex1[0], vertex1[1], vertex1[2]);
147149
148-
float* vertex2 = reinterpret_cast<float*>(vbo_buffer + stride_vbo * (j * 3 + 1));
150+
float* vertex2 = reinterpret_cast<float*>(vbo_buffer + stride_vbo * (ibo_buffer[j * 3 + 1]));
149151
Core::Geometry::Point node2(vertex2[0], vertex2[1], vertex2[2]);
150152
151-
float* vertex3 = reinterpret_cast<float*>(vbo_buffer + stride_vbo * (j * 3 + 2));
153+
float* vertex3 = reinterpret_cast<float*>(vbo_buffer + stride_vbo * (ibo_buffer[j * 3 + 2]));
152154
Core::Geometry::Point node3(vertex3[0], vertex3[1], vertex3[2]);
153155
154156
rel_depth[j].mDepth = Core::Geometry::Dot(dir, node1) + Core::Geometry::Dot(dir, node2) + Core::Geometry::Dot(dir, node3);
@@ -157,21 +159,7 @@ class RenderBasicSysTrans :
157159
158160
std::sort(rel_depth.begin(), rel_depth.end());
159161
160-
/*
161-
// setup vertex buffers
162-
std::vector<std::tuple<std::string, size_t, bool>> attributeData;
163-
for (const auto& attribData : pass.front().vbo.attributes)
164-
{
165-
attributeData.push_back(std::make_tuple(attribData.name, attribData.sizeInBytes, attribData.normalize));
166-
}
167-
168-
std::string name = pass.front().vbo.name + "trans";
169-
170-
GLuint vboID = vboMan.front().instance->addInMemoryVBO(pass.front().vbo.data->getBuffer(), pass.front().vbo.data->getBufferSize(),
171-
attributeData, name);
172-
*/
173162
// setup index buffers
174-
175163
GLenum primType = GL_UNSIGNED_SHORT;
176164
switch (pass.front().ibo.indexSize)
177165
{
@@ -222,13 +210,11 @@ class RenderBasicSysTrans :
222210
memcpy(sbuffer + j * tri_size, ibuffer + rel_depth[j].mIndex * tri_size, tri_size);
223211
}
224212
225-
//int numPrimitives = pass.front().ibo.data->getBufferSize() / pass.front().ibo.indexSize;
226-
227213
std::string transIBOName = pass.front().ibo.name + "trans";
228214
229215
GLuint iboID = iboMan.front().instance->addInMemoryIBO(sbuffer, pass.front().ibo.data->getBufferSize(), primitive, primType,
230216
numPrimitives, transIBOName);
231-
217+
*/
232218
// Setup *everything*. We don't want to enter multiple conditional
233219
// statements if we can avoid it. So we assume everything has not been
234220
// setup (including uniforms) if the simple geom hasn't been setup.
@@ -284,7 +270,7 @@ class RenderBasicSysTrans :
284270

285271
// Bind VBO and IBO
286272
GL(glBindBuffer(GL_ARRAY_BUFFER, vbo.front().glid));
287-
GL(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, iboID));
273+
GL(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo.front().glid));
288274

289275
// Bind any common uniforms.
290276
if (commonUniforms.size() > 0)
@@ -427,7 +413,7 @@ class RenderBasicSysTrans :
427413
}
428414
}
429415

430-
iboMan.front().instance->removeInMemoryIBO(iboID);
416+
//iboMan.front().instance->removeInMemoryIBO(iboID);
431417

432418
if (depthMask)
433419
{

src/Interface/Modules/Render/ES/systems/RenderTransColorMapSys.cc

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ class RenderColorMapSysTrans :
129129
return;
130130
}
131131

132+
/*
132133
char* vbo_buffer = reinterpret_cast<char*>(pass.front().vbo.data->getBuffer());
133134
uint32_t* ibo_buffer = reinterpret_cast<uint32_t*>(pass.front().ibo.data->getBuffer());
134135
size_t num_triangles = pass.front().ibo.data->getBufferSize() / (sizeof(uint32_t) * 3);
@@ -138,10 +139,7 @@ class RenderColorMapSysTrans :
138139
stride_vbo += a.sizeInBytes;
139140
140141
std::vector<DepthIndex> rel_depth(num_triangles);
141-
Core::Geometry::Vector dir(
142-
camera.front().data.worldToView[0][2],
143-
camera.front().data.worldToView[1][2],
144-
camera.front().data.worldToView[2][2]);
142+
Core::Geometry::Vector dir(camera.front().data.worldToView[0][2], camera.front().data.worldToView[1][2], camera.front().data.worldToView[2][2]);
145143
146144
for (size_t j = 0; j < num_triangles; j++)
147145
{
@@ -218,7 +216,7 @@ class RenderColorMapSysTrans :
218216
219217
GLuint iboID = iboMan.front().instance->addInMemoryIBO(sbuffer, pass.front().ibo.data->getBufferSize(), primitive, primType,
220218
numPrimitives, transIBOName);
221-
219+
*/
222220
// Setup *everything*. We don't want to enter multiple conditional
223221
// statements if we can avoid it. So we assume everything has not been
224222
// setup (including uniforms) if the simple geom hasn't been setup.
@@ -274,7 +272,7 @@ class RenderColorMapSysTrans :
274272

275273
// Bind VBO and IBO
276274
GL(glBindBuffer(GL_ARRAY_BUFFER, vbo.front().glid));
277-
GL(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, iboID));
275+
GL(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo.front().glid));
278276

279277
// Bind any common uniforms.
280278
if (commonUniforms.size() > 0)
@@ -429,7 +427,7 @@ class RenderColorMapSysTrans :
429427
}
430428
}
431429

432-
iboMan.front().instance->removeInMemoryIBO(iboID);
430+
//iboMan.front().instance->removeInMemoryIBO(iboID);
433431

434432
if (depthMask)
435433
{

src/Interface/Modules/Render/ViewScene.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,12 @@ void ViewSceneDialog::newGeometryValue()
118118
LOG_DEBUG("ViewSceneDialog::asyncExecute after locking");
119119

120120
itemManager_->removeAll();
121-
/*
121+
122122
std::shared_ptr<Render::SRInterface> spire = mSpire.lock();
123123
if (spire == nullptr)
124124
return;
125125
spire->removeAllGeomObjects();
126-
*/
126+
127127
// Grab the geomData transient value.
128128
auto geomDataTransient = state_->getTransientValue(Parameters::GeomData);
129129
if (geomDataTransient && !geomDataTransient->empty())

0 commit comments

Comments
 (0)