remove direct use of rtt and dirty area from the layer

This commit is contained in:
2019-10-15 11:57:39 +02:00
parent 9e93fe48d6
commit 9ccd6ed2f4
12 changed files with 404 additions and 334 deletions

View File

@@ -5,6 +5,26 @@
uint32_t Layer::s_count = 0;
RTT& Layer::rtt(int i)
{
return m_frames[m_frame_index].m_rtt[i];
}
glm::vec4& Layer::box(int i)
{
return m_frames[m_frame_index].m_dirty_box[i];
}
bool& Layer::face(int i)
{
return m_frames[m_frame_index].m_dirty_face[i];
}
LayerFrame& Layer::frame()
{
return m_frames[m_frame_index];
}
TextureCube Layer::gen_cube()
{
TextureCube ret;
@@ -14,9 +34,9 @@ TextureCube Layer::gen_cube()
ret.bind();
for (int i = 0; i < 6; i++)
{
m_rtt[i].bindFramebuffer();
rtt(i).bindFramebuffer();
glCopyTexSubImage2D(TextureCube::m_faces_map[i], 0, 0, 0, 0, 0, w, w);
m_rtt[i].unbindFramebuffer();
rtt(i).unbindFramebuffer();
}
});
return ret;
@@ -69,6 +89,189 @@ Texture2D Layer::gen_equirect()
return ret;
}
void Layer::destroy()
{
for (int i = 0; i < 6; i++)
rtt(i).destroy();
}
void Layer::optimize()
{
int saved_bytes = 0;
for (int i = 0; i < 6; i++)
{
if (!face(i))
continue;
auto data = std::unique_ptr<glm::u8vec4[]>(reinterpret_cast<glm::u8vec4*>(rtt(i).readTextureData()));
glm::ivec2 bbmin(w, h);
glm::ivec2 bbmax(0);
for (int y = 0; y < h; y++)
{
for (int x = 0; x < w; x++)
{
if (data[x + y * w].a > 0)
{
bbmin = glm::min(bbmin, { x, y });
bbmax = glm::max(bbmax, { x + 1, y + 1 });
}
}
}
glm::vec2 bbsz = bbmax - bbmin;
glm::vec2 old_size = zw(box(i)) - xy(box(i));
glm::vec2 diff;
if (bbsz.x <= 0 || bbmax.y <= 0)
{
face(i) = false;
box(i) = glm::vec4(0);
diff = old_size;
}
else
{
box(i) = { bbmin, bbmax };
diff = old_size - bbsz;
}
saved_bytes += (int)(diff.x * diff.y * 4);
}
LOG("optimized %d bytes", saved_bytes);
}
void Layer::restore(const Snapshot& snap)
{
clear({ 0, 0, 0, 0 });
for (int i = 0; i < 6; i++)
{
if (snap.image[i] == nullptr || snap.m_dirty_face[i] == false || box_area(snap.m_dirty_box[i]) <= 0)
{
box(i) = glm::vec4(snap.width, snap.height, 0, 0);
face(i) = false;
continue;
}
box(i) = snap.m_dirty_box[i];
face(i) = snap.m_dirty_face[i];
// TODO: this should not be recreated here!
// Sorry I messed up with this,
// it's just a quick fix DON'T SHIP!!
//m_rtt[i].recreate();
App::I->render_task_async([this, i, &snap]
{
rtt(i).bindTexture();
glm::vec2 box_sz = zw(box(i)) - xy(box(i));
glTexSubImage2D(GL_TEXTURE_2D, 0,
box(i).x, box(i).y,
box_sz.x, box_sz.y, GL_RGBA, GL_UNSIGNED_BYTE,
snap.image[i].get());
rtt(i).unbindTexture();
LOG("restore face %d - %d bytes (%dx%d)", i,
(int)box_sz.x * (int)box_sz.y * 4, (int)box_sz.x, (int)box_sz.y);
});
}
App::I->render_sync();
}
Layer::Snapshot Layer::snapshot(std::array<glm::vec4, 6>* dirty_box /*= nullptr*/, std::array<bool, 6>* dirty_face /*= nullptr*/)
{
Snapshot snap;
snap.width = w;
snap.height = h;
for (int i = 0; i < 6; i++)
{
snap.m_dirty_box[i] = dirty_box ? dirty_box->at(i) : box(i);
snap.m_dirty_face[i] = dirty_face ? dirty_face->at(i) : face(i);
if (!snap.m_dirty_face[i])
continue;
snap.image[i] = std::make_unique<uint8_t[]>(rtt(i).bytes());
App::I->render_task_async([this, i, &snap]
{
rtt(i).bindFramebuffer();
glm::vec2 box_sz = zw(snap.m_dirty_box[i]) - xy(snap.m_dirty_box[i]);
glReadPixels(snap.m_dirty_box[i].x, snap.m_dirty_box[i].y,
box_sz.x, box_sz.y, GL_RGBA, GL_UNSIGNED_BYTE, snap.image[i].get());
rtt(i).unbindFramebuffer();
});
}
App::I->render_sync();
return snap;
}
void Layer::clear(const glm::vec4& c)
{
App::I->render_task([&]
{
// push clear color state
GLfloat cc[4];
glGetFloatv(GL_COLOR_CLEAR_VALUE, cc);
glClearColor(c.r, c.g, c.b, c.a);
bool erase = (c.a == 0.f);
for (int i = 0; i < 6; i++)
{
rtt(i).bindFramebuffer();
glClear(GL_COLOR_BUFFER_BIT);
rtt(i).unbindFramebuffer();
if (erase)
{
box(i) = glm::vec4(w, h, 0, 0); // reset bounding box
face(i) = false;
}
else
{
box(i) = glm::vec4(0, 0, w, h); // reset bounding box
face(i) = true;
}
}
// restore clear color state
glClearColor(cc[0], cc[1], cc[2], cc[3]);
});
}
bool Layer::create(int width, int height, std::string name)
{
m_name = name;
w = width;
h = height;
if (m_frames.empty())
m_frames.emplace_back();
App::I->render_task([&]
{
for (int i = 0; i < 6; i++)
{
rtt(i).create(width, height);
rtt(i).bindFramebuffer();
rtt(i).clear();
rtt(i).unbindFramebuffer();
box(i) = glm::vec4(w, h, 0, 0); // reset bounding box
face(i) = false;
}
});
return true;
}
bool Layer::add_frame()
{
return true;
}
void Layer::resize(int width, int height)
{
w = width;
h = height;
for (auto& frame : m_frames)
frame.resize(width, height);
}
///////////////////////////////////////////////////////////////////////////////////////////
void Layer::Snapshot::create(int w, int h)
{
width = w;
@@ -129,3 +332,30 @@ int Layer::Snapshot::memsize() const
}
return ret;
}
///////////////////////////////////////////////////////////////////////////////////////////
bool LayerFrame::create(int width, int height, int duration /*= 1*/)
{
for (auto& rtt : m_rtt)
if (!rtt.create(width, height))
return false;
m_duration = duration;
w = width;
h = height;
return true;
}
bool LayerFrame::resize(int width, int height)
{
glm::vec2 ratio = glm::vec2(width, height) / glm::vec2(w, h);
for (int i = 0; i < 6; i++)
{
if (!m_rtt[i].resize(width, height))
return false;
m_dirty_box[i] = m_dirty_box[i] * glm::vec4(ratio, ratio);
}
w = width;
h = height;
return true;
}