Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 21 additions & 25 deletions mask/mask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,21 +101,18 @@ static const char* const JSON_INTRO_FADE_TIME = "intro_fade_time";
static const char* const JSON_INTRO_DURATION = "intro_duration";


static const int NUM_DRAW_BUCKETS = 1024;
static const float BUCKETS_MAX_Z = 10.0f;
static const float BUCKETS_MIN_Z = -100.0f;

Mask::MaskData::MaskData(Cache *cache) : m_data(nullptr), m_morph(nullptr),
m_cache(cache), m_elapsedTime(0.0f) {
m_drawBuckets = new Mask::SortedDrawObject*[NUM_DRAW_BUCKETS];
ClearSortedDrawObjects();
m_vidLightTex = nullptr;
m_num_render_layers = 1;
m_num_render_orders = 1;
}

Mask::MaskData::~MaskData() {
delete[] m_drawBuckets;
Clear();
}

Expand Down Expand Up @@ -569,33 +566,27 @@ void Mask::MaskData::ClearSortedDrawObjects() {

void Mask::MaskData::AddSortedDrawObject(SortedDrawObject* obj) {
float z = obj->SortDepth() + obj->m_depth_bias;
float normalized_z = z;
if (z > BUCKETS_MAX_Z)
z = BUCKETS_MAX_Z;
normalized_z = BUCKETS_MAX_Z;
if (z < BUCKETS_MIN_Z)
z = BUCKETS_MIN_Z;
z = (z - BUCKETS_MIN_Z) / (BUCKETS_MAX_Z - BUCKETS_MIN_Z);
int idx = (int)(z * (float)(NUM_DRAW_BUCKETS - 1));
normalized_z = BUCKETS_MIN_Z;
normalized_z = (normalized_z - BUCKETS_MIN_Z) / (BUCKETS_MAX_Z - BUCKETS_MIN_Z);
int idx = (int)(normalized_z * (float)(NUM_DRAW_BUCKETS - 1));

if (m_drawBuckets[idx] != nullptr)
{
float z2=m_drawBuckets[idx]->SortDepth();
if (z < z2)
{
// add new obj before current
obj->nextDrawObject = m_drawBuckets[idx];
m_drawBuckets[idx] = obj;
}
else
SortedDrawObject **node = &m_drawBuckets[idx];
while (*node) {
if (*node == obj)
{
// add it after current
obj->nextDrawObject = m_drawBuckets[idx]->nextDrawObject;
m_drawBuckets[idx]->nextDrawObject = obj;
blog(LOG_ERROR, "Duplicate SortedDrawObject was found. Discarding...");
return;
}

if (z < ((*node)->SortDepth() + (*node)->m_depth_bias)) break;
node = &(*node)->nextDrawObject;
}
else {
obj->nextDrawObject = m_drawBuckets[idx]; // which is nullptr
m_drawBuckets[idx] = obj;
}
obj->nextDrawObject = *node;
*node = obj;

obj->instanceId = instanceDatas.CurrentId();

Expand Down Expand Up @@ -916,7 +907,8 @@ void Mask::MaskData::Render(const smll::DetectionResults &faces, int width, int
for (size_t current_order = 0; current_order < m_num_render_orders; current_order++)
{
for (unsigned int i = 0; i < NUM_DRAW_BUCKETS; i++) {
SortedDrawObject* sdo = m_drawBuckets[i];
SortedDrawObject* head_sdo = m_drawBuckets[i];
SortedDrawObject* sdo = head_sdo;
Resource::IBase* res = dynamic_cast<Resource::IBase*>(sdo);
if (!res) continue; // skip if sdo is not a resource type
while (sdo) {
Expand All @@ -941,6 +933,10 @@ void Mask::MaskData::Render(const smll::DetectionResults &faces, int width, int
instanceDatas.Pop();
}
sdo = sdo->nextDrawObject;
if (sdo == head_sdo) {
blog(LOG_ERROR, "Loop found in SortedDrawObject list. Looped back to '%s'. Breaking the loop...", res->GetName().c_str());
break;
}
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion mask/mask.h
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,8 @@ namespace Mask {
std::map<std::string, std::shared_ptr<Resource::Animation>> m_animations;
obs_data_t* m_data;
std::shared_ptr<Mask::Part> m_partWorld;
SortedDrawObject** m_drawBuckets;
static const int NUM_DRAW_BUCKETS = 1024;
SortedDrawObject* m_drawBuckets[NUM_DRAW_BUCKETS];
Resource::Morph* m_morph;

// video light data
Expand Down