implement layer bounds optminize function, add history to draw_objects with the right bounds.
This commit is contained in:
@@ -1090,6 +1090,7 @@ bool Canvas::create(int width, int height)
|
||||
}
|
||||
m_sampler.create(GL_NEAREST);
|
||||
m_sampler.create(GL_LINEAR);
|
||||
m_sampler_nearest.create(GL_NEAREST);
|
||||
m_sampler_brush.create(GL_LINEAR, GL_CLAMP_TO_BORDER);
|
||||
m_sampler_brush.set_filter(GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR);
|
||||
m_sampler_brush.set_border({ 1, 1, 1, 1 });
|
||||
@@ -1097,6 +1098,7 @@ bool Canvas::create(int width, int height)
|
||||
m_sampler_mask.create(GL_LINEAR);
|
||||
m_sampler_stencil.create(GL_LINEAR, GL_REPEAT);
|
||||
m_sampler_mix.create(GL_NEAREST, GL_REPEAT);
|
||||
m_sampler_linear.create();
|
||||
m_plane.create<1>(1, 1);
|
||||
m_plane_brush.create<1>(1, 1);
|
||||
m_brush_shape.create();
|
||||
@@ -2212,22 +2214,58 @@ void Canvas::draw_objects(std::function<void(const glm::mat4& camera, const glm:
|
||||
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, layer.w, layer.h);
|
||||
glBindRenderbuffer(GL_RENDERBUFFER, 0);
|
||||
|
||||
// allocate action to add to history
|
||||
auto action = new ActionStroke;
|
||||
action->was_saved = !m_unsaved;
|
||||
|
||||
glm::mat4 proj = glm::perspective(glm::radians(90.f), 1.f, .01f, 1000.f);
|
||||
for (int i = 0; i < 6; i++)
|
||||
{
|
||||
glm::mat4 plane_camera = glm::lookAt(glm::vec3(0), m_plane_origin[i], m_plane_tangent[i]);
|
||||
layer.m_rtt[i].bindFramebuffer();
|
||||
m_tmp[i].bindFramebuffer();
|
||||
m_tmp[i].clear({ 1, 1, 1, 0 });
|
||||
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rboID);
|
||||
|
||||
observer(plane_camera, proj, i);
|
||||
|
||||
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, 0);
|
||||
m_tmp[i].unbindFramebuffer();
|
||||
|
||||
glm::vec4 bounds = m_tmp[i].calc_bounds();
|
||||
glm::vec2 box_sz = zw(bounds) - xy(bounds);
|
||||
if (box_sz.x <= 0 || box_sz.y <= 0)
|
||||
continue;
|
||||
|
||||
layer.m_rtt[i].bindFramebuffer();
|
||||
|
||||
// save image before commit
|
||||
action->m_image[i] = std::make_unique<uint8_t[]>(box_sz.x * box_sz.y * 4);
|
||||
glReadPixels(bounds.x, bounds.y, box_sz.x, box_sz.y, GL_RGBA, GL_UNSIGNED_BYTE, action->m_image[i].get());
|
||||
|
||||
action->m_box[i] = bounds;
|
||||
action->m_old_box[i] = layer.m_dirty_box[i];
|
||||
action->m_old_dirty[i] = layer.m_dirty_face[i];
|
||||
|
||||
// draw the tmp layer into the actual layer
|
||||
ShaderManager::use(kShader::Texture);
|
||||
ShaderManager::u_int(kShaderUniform::Tex, 0);
|
||||
ShaderManager::u_mat4(kShaderUniform::MVP, glm::ortho(-0.5f, 0.5f, -0.5f, 0.5f));
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
m_sampler_nearest.bind(0);
|
||||
m_tmp[i].bindTexture();
|
||||
m_plane.draw_fill();
|
||||
m_tmp[i].unbindTexture();
|
||||
|
||||
layer.m_rtt[i].unbindFramebuffer();
|
||||
|
||||
layer.m_dirty_face[i] = true;
|
||||
layer.m_dirty_box[i] = { 0, 0, layer.w, layer.h };
|
||||
layer.m_dirty_box[i] = rect_union(layer.m_dirty_box[i], bounds);
|
||||
}
|
||||
|
||||
// save history
|
||||
action->m_layer_idx = m_current_layer_idx;
|
||||
action->m_canvas = this;
|
||||
action->m_stroke = std::move(m_current_stroke);
|
||||
ActionManager::add(action);
|
||||
|
||||
glDeleteRenderbuffers(1, &rboID);
|
||||
|
||||
// restore viewport and clear color states
|
||||
@@ -2515,6 +2553,49 @@ void Layer::destroy()
|
||||
m_rtt[i].destroy();
|
||||
}
|
||||
|
||||
void Layer::optimize()
|
||||
{
|
||||
int saved_bytes = 0;
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
for (int i = 0; i < 6; i++)
|
||||
{
|
||||
if (!m_dirty_face[i])
|
||||
continue;
|
||||
|
||||
auto data = std::unique_ptr<glm::i8vec4[]>(reinterpret_cast<glm::i8vec4*>(m_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(m_dirty_box[i]) - xy(m_dirty_box[i]);
|
||||
glm::vec2 diff;
|
||||
if (bbsz.x <= 0 || bbmax.y <= 0)
|
||||
{
|
||||
m_dirty_face[i] = false;
|
||||
m_dirty_box[i] = glm::vec4(0);
|
||||
diff = old_size;
|
||||
}
|
||||
else
|
||||
{
|
||||
m_dirty_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 });
|
||||
|
||||
Reference in New Issue
Block a user