remove direct use of rtt and dirty area from the layer
This commit is contained in:
315
src/canvas.cpp
315
src/canvas.cpp
@@ -81,11 +81,11 @@ void Canvas::pick_update(int plane)
|
||||
draw_merge(true, faces);
|
||||
|
||||
int i = plane;
|
||||
m_layers_merge.m_rtt[i].bindFramebuffer();
|
||||
m_layers_merge.rtt(i).bindFramebuffer();
|
||||
if (!m_pick_data[plane])
|
||||
m_pick_data[plane] = std::make_unique<glm::u8vec4[]>(m_width * m_height);
|
||||
glReadPixels(0, 0, m_width, m_height, GL_RGBA, GL_UNSIGNED_BYTE, m_pick_data[plane].get());
|
||||
m_layers_merge.m_rtt[i].unbindFramebuffer();
|
||||
m_layers_merge.rtt(i).unbindFramebuffer();
|
||||
});
|
||||
|
||||
m_pick_ready[plane] = true;
|
||||
@@ -168,7 +168,7 @@ void Canvas::stroke_draw_mix(const glm::vec2& bb_min, const glm::vec2& bb_sz)
|
||||
{
|
||||
if (!m_layers[layer_index]->m_visible ||
|
||||
m_layers[layer_index]->m_opacity == .0f ||
|
||||
!m_layers[layer_index]->m_dirty_face[plane_index])
|
||||
!m_layers[layer_index]->face(plane_index))
|
||||
continue;
|
||||
|
||||
//glm::mat4 proj = glm::perspective(glm::radians(m_cam_fov), (float)m_mixer.getWidth() / m_mixer.getHeight(), 0.1f, 1000.f);
|
||||
@@ -198,17 +198,17 @@ void Canvas::stroke_draw_mix(const glm::vec2& bb_min, const glm::vec2& bb_sz)
|
||||
ShaderManager::u_int(kShaderUniform::BlendMode, b->m_blend_mode);
|
||||
ShaderManager::u_mat4(kShaderUniform::MVP, plane_mvp_z);
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
m_layers[layer_index]->m_rtt[plane_index].bindTexture();
|
||||
m_layers[layer_index]->rtt(plane_index).bindTexture();
|
||||
glActiveTexture(GL_TEXTURE1);
|
||||
m_tmp[plane_index].bindTexture();
|
||||
glActiveTexture(GL_TEXTURE2);
|
||||
m_smask.m_rtt[plane_index].bindTexture();
|
||||
m_smask.rtt(plane_index).bindTexture();
|
||||
m_node->m_face_plane.draw_fill();
|
||||
m_smask.m_rtt[plane_index].unbindTexture();
|
||||
m_smask.rtt(plane_index).unbindTexture();
|
||||
glActiveTexture(GL_TEXTURE1);
|
||||
m_tmp[plane_index].unbindTexture();
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
m_layers[layer_index]->m_rtt[plane_index].unbindTexture();
|
||||
m_layers[layer_index]->rtt(plane_index).unbindTexture();
|
||||
}
|
||||
m_sampler.unbind();
|
||||
m_mixer.unbindFramebuffer();
|
||||
@@ -766,7 +766,7 @@ void Canvas::stroke_commit()
|
||||
if (!m_dirty_face[i])
|
||||
continue; // no stroke on this face, skip it
|
||||
|
||||
m_layers[m_current_layer_idx]->m_rtt[i].bindFramebuffer();
|
||||
m_layers[m_current_layer_idx]->rtt(i).bindFramebuffer();
|
||||
|
||||
// save image before commit
|
||||
glm::vec2 box_sz = zw(m_dirty_box[i]) - xy(m_dirty_box[i]);
|
||||
@@ -774,18 +774,18 @@ void Canvas::stroke_commit()
|
||||
glReadPixels(m_dirty_box[i].x, m_dirty_box[i].y, box_sz.x, box_sz.y, GL_RGBA, GL_UNSIGNED_BYTE, action->m_image[i].get());
|
||||
|
||||
action->m_box[i] = m_dirty_box[i];
|
||||
action->m_old_box[i] = m_layers[m_current_layer_idx]->m_dirty_box[i];
|
||||
action->m_old_dirty[i] = m_layers[m_current_layer_idx]->m_dirty_face[i];
|
||||
action->m_old_box[i] = m_layers[m_current_layer_idx]->box(i);
|
||||
action->m_old_dirty[i] = m_layers[m_current_layer_idx]->face(i);
|
||||
|
||||
if (!m_layers[m_current_layer_idx]->m_alpha_locked)
|
||||
{
|
||||
auto& lbox = m_layers[m_current_layer_idx]->m_dirty_box[i];
|
||||
auto& lbox = m_layers[m_current_layer_idx]->box(i);
|
||||
lbox = glm::vec4(
|
||||
glm::min(xy(m_dirty_box[i]), xy(lbox)),
|
||||
glm::max(zw(m_dirty_box[i]), zw(lbox))
|
||||
);
|
||||
}
|
||||
m_layers[m_current_layer_idx]->m_dirty_face[i] = true;
|
||||
m_layers[m_current_layer_idx]->face(i) = true;
|
||||
|
||||
// copy to tmp2 for layer blending
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
@@ -816,9 +816,9 @@ void Canvas::stroke_commit()
|
||||
glActiveTexture(GL_TEXTURE1);
|
||||
m_tmp[i].bindTexture();
|
||||
glActiveTexture(GL_TEXTURE2);
|
||||
m_smask.m_rtt[i].bindTexture();
|
||||
m_smask.rtt(i).bindTexture();
|
||||
m_plane.draw_fill();
|
||||
m_smask.m_rtt[i].unbindTexture();
|
||||
m_smask.rtt(i).unbindTexture();
|
||||
glActiveTexture(GL_TEXTURE1);
|
||||
m_tmp[i].unbindTexture();
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
@@ -860,7 +860,7 @@ void Canvas::stroke_commit()
|
||||
glActiveTexture(GL_TEXTURE1);
|
||||
m_tmp[i].bindTexture();
|
||||
glActiveTexture(GL_TEXTURE2);
|
||||
m_smask.m_rtt[i].bindTexture();
|
||||
m_smask.rtt(i).bindTexture();
|
||||
glActiveTexture(GL_TEXTURE3);
|
||||
if (b->m_dual_enabled)
|
||||
m_tmp_dual[i].bindTexture();
|
||||
@@ -873,7 +873,7 @@ void Canvas::stroke_commit()
|
||||
if (b->m_dual_enabled)
|
||||
m_tmp_dual[i].unbindTexture();
|
||||
glActiveTexture(GL_TEXTURE2);
|
||||
m_smask.m_rtt[i].unbindTexture();
|
||||
m_smask.rtt(i).unbindTexture();
|
||||
glActiveTexture(GL_TEXTURE1);
|
||||
m_tmp[i].unbindTexture();
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
@@ -904,7 +904,7 @@ void Canvas::stroke_commit()
|
||||
glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, m_width, m_height);
|
||||
m_plane.draw_fill();
|
||||
|
||||
m_layers[m_current_layer_idx]->m_rtt[i].unbindFramebuffer();
|
||||
m_layers[m_current_layer_idx]->rtt(i).unbindFramebuffer();
|
||||
}
|
||||
|
||||
// restore viewport and clear color states
|
||||
@@ -945,13 +945,13 @@ void Canvas::draw_merge(bool draw_checkerboard, std::array<bool, 6> faces /*= SI
|
||||
if (!faces[plane_index])
|
||||
continue;
|
||||
|
||||
m_layers_merge.m_rtt[plane_index].bindFramebuffer();
|
||||
m_layers_merge.m_rtt[plane_index].clear({ 1, 1, 1, 0 });
|
||||
m_layers_merge.rtt(plane_index).bindFramebuffer();
|
||||
m_layers_merge.rtt(plane_index).clear({ 1, 1, 1, 0 });
|
||||
|
||||
if (use_blend)
|
||||
{
|
||||
glDisable(GL_BLEND);
|
||||
m_layers_merge.m_rtt[plane_index].clear();
|
||||
m_layers_merge.rtt(plane_index).clear();
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -970,7 +970,7 @@ void Canvas::draw_merge(bool draw_checkerboard, std::array<bool, 6> faces /*= SI
|
||||
if (!(m_show_tmp && m_current_layer_idx == layer_index) &&
|
||||
(!m_layers[layer_index]->m_visible ||
|
||||
m_layers[layer_index]->m_opacity == .0f ||
|
||||
!m_layers[layer_index]->m_dirty_face[plane_index]))
|
||||
!m_layers[layer_index]->face(plane_index)))
|
||||
continue;
|
||||
|
||||
if (use_blend)
|
||||
@@ -995,17 +995,17 @@ void Canvas::draw_merge(bool draw_checkerboard, std::array<bool, 6> faces /*= SI
|
||||
ShaderManager::u_int(kShaderUniform::Mask, m_smask_active);
|
||||
ShaderManager::u_mat4(kShaderUniform::MVP, ortho);
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
m_layers[layer_index]->m_rtt[plane_index].bindTexture();
|
||||
m_layers[layer_index]->rtt(plane_index).bindTexture();
|
||||
glActiveTexture(GL_TEXTURE1);
|
||||
m_tmp[plane_index].bindTexture();
|
||||
glActiveTexture(GL_TEXTURE2);
|
||||
m_smask.m_rtt[plane_index].bindTexture();
|
||||
m_smask.rtt(plane_index).bindTexture();
|
||||
m_plane.draw_fill();
|
||||
m_smask.m_rtt[plane_index].unbindTexture();
|
||||
m_smask.rtt(plane_index).unbindTexture();
|
||||
glActiveTexture(GL_TEXTURE1);
|
||||
m_tmp[plane_index].unbindTexture();
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
m_layers[layer_index]->m_rtt[plane_index].unbindTexture();
|
||||
m_layers[layer_index]->rtt(plane_index).unbindTexture();
|
||||
}
|
||||
else if (m_current_stroke && m_show_tmp && m_current_layer_idx == layer_index)
|
||||
{
|
||||
@@ -1045,11 +1045,11 @@ void Canvas::draw_merge(bool draw_checkerboard, std::array<bool, 6> faces /*= SI
|
||||
ShaderManager::u_float(kShaderUniform::DualAlpha, b->m_dual_opacity);
|
||||
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
m_layers[layer_index]->m_rtt[plane_index].bindTexture();
|
||||
m_layers[layer_index]->rtt(plane_index).bindTexture();
|
||||
glActiveTexture(GL_TEXTURE1);
|
||||
m_tmp[plane_index].bindTexture();
|
||||
glActiveTexture(GL_TEXTURE2);
|
||||
m_smask.m_rtt[plane_index].bindTexture();
|
||||
m_smask.rtt(plane_index).bindTexture();
|
||||
glActiveTexture(GL_TEXTURE3);
|
||||
if (b->m_dual_enabled)
|
||||
m_tmp_dual[plane_index].bindTexture();
|
||||
@@ -1062,11 +1062,11 @@ void Canvas::draw_merge(bool draw_checkerboard, std::array<bool, 6> faces /*= SI
|
||||
if (b->m_dual_enabled)
|
||||
m_tmp_dual[plane_index].unbindTexture();
|
||||
glActiveTexture(GL_TEXTURE2);
|
||||
m_smask.m_rtt[plane_index].unbindTexture();
|
||||
m_smask.rtt(plane_index).unbindTexture();
|
||||
glActiveTexture(GL_TEXTURE1);
|
||||
m_tmp[plane_index].unbindTexture();
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
m_layers[layer_index]->m_rtt[plane_index].unbindTexture();
|
||||
m_layers[layer_index]->rtt(plane_index).unbindTexture();
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -1078,9 +1078,9 @@ void Canvas::draw_merge(bool draw_checkerboard, std::array<bool, 6> faces /*= SI
|
||||
ShaderManager::u_mat4(kShaderUniform::MVP, ortho);
|
||||
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
m_layers[layer_index]->m_rtt[plane_index].bindTexture();
|
||||
m_layers[layer_index]->rtt(plane_index).bindTexture();
|
||||
m_plane.draw_fill();
|
||||
m_layers[layer_index]->m_rtt[plane_index].unbindTexture();
|
||||
m_layers[layer_index]->rtt(plane_index).unbindTexture();
|
||||
}
|
||||
|
||||
if (use_blend)
|
||||
@@ -1155,7 +1155,7 @@ void Canvas::draw_merge(bool draw_checkerboard, std::array<bool, 6> faces /*= SI
|
||||
m_merge_tex.unbind();
|
||||
}
|
||||
|
||||
m_layers_merge.m_rtt[plane_index].unbindFramebuffer();
|
||||
m_layers_merge.rtt(plane_index).unbindFramebuffer();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1275,17 +1275,17 @@ void Canvas::layer_merge(int source_idx, int dest_idx) // m_layer index
|
||||
|
||||
for (int i = 0; i < 6; i++)
|
||||
{
|
||||
if (!m_layers[source_idx]->m_dirty_face[i])
|
||||
if (!m_layers[source_idx]->face(i))
|
||||
continue; // no stroke on this face, skip it
|
||||
|
||||
m_layers[dest_idx]->m_rtt[i].bindFramebuffer();
|
||||
m_layers[dest_idx]->rtt(i).bindFramebuffer();
|
||||
|
||||
auto& lbox = m_layers[dest_idx]->m_dirty_box[i];
|
||||
auto& lbox = m_layers[dest_idx]->box(i);
|
||||
lbox = glm::vec4(
|
||||
glm::min(xy(m_layers[source_idx]->m_dirty_box[i]), xy(lbox)),
|
||||
glm::max(zw(m_layers[source_idx]->m_dirty_box[i]), zw(lbox))
|
||||
glm::min(xy(m_layers[source_idx]->box(i)), xy(lbox)),
|
||||
glm::max(zw(m_layers[source_idx]->box(i)), zw(lbox))
|
||||
);
|
||||
m_layers[dest_idx]->m_dirty_face[i] = true;
|
||||
m_layers[dest_idx]->face(i) = true;
|
||||
|
||||
// copy to tmp2 for layer blending
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
@@ -1312,14 +1312,14 @@ void Canvas::layer_merge(int source_idx, int dest_idx) // m_layer index
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
m_tex2[i].bind();
|
||||
glActiveTexture(GL_TEXTURE1);
|
||||
m_layers[source_idx]->m_rtt[i].bindTexture();
|
||||
m_layers[source_idx]->rtt(i).bindTexture();
|
||||
m_plane.draw_fill();
|
||||
m_layers[source_idx]->m_rtt[i].unbindTexture();
|
||||
m_layers[source_idx]->rtt(i).unbindTexture();
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
m_tex2[i].unbind();
|
||||
}
|
||||
|
||||
m_layers[dest_idx]->m_rtt[i].unbindFramebuffer();
|
||||
m_layers[dest_idx]->rtt(i).unbindFramebuffer();
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -1346,14 +1346,14 @@ void Canvas::flood_fill(int layer, int plane, std::vector<glm::ivec2> pos, Flood
|
||||
|
||||
LOG("flood_fill plane %d", plane);
|
||||
|
||||
auto& rtt = m_layers[layer]->m_rtt[plane];
|
||||
auto& rtt = m_layers[layer]->rtt(plane);
|
||||
auto sz = rtt.getSize();
|
||||
|
||||
if (!plane_data.mask[plane])
|
||||
{
|
||||
plane_data.mask[plane] = std::make_unique<bool[]>((size_t)sz.x * sz.y);
|
||||
plane_data.rgb[plane] = std::unique_ptr<glm::u8vec4[]>(
|
||||
reinterpret_cast<glm::u8vec4*>(m_layers[layer]->m_rtt[plane].readTextureData()));
|
||||
reinterpret_cast<glm::u8vec4*>(m_layers[layer]->rtt(plane).readTextureData()));
|
||||
plane_data.bb[plane] = { sz.x, sz.y, 0, 0 };
|
||||
plane_data.dirty[plane] = false;
|
||||
plane_data.layer = m_layers[layer];
|
||||
@@ -1488,7 +1488,7 @@ void Canvas::FloodData::apply()
|
||||
{
|
||||
if (!dirty[plane])
|
||||
continue;
|
||||
auto& rtt = layer->m_rtt[plane];
|
||||
auto& rtt = layer->rtt(plane);
|
||||
App::I->render_task([&]
|
||||
{
|
||||
rtt.bindTexture();
|
||||
@@ -1496,8 +1496,8 @@ void Canvas::FloodData::apply()
|
||||
GL_RGBA, GL_UNSIGNED_BYTE, rgb[plane].get());
|
||||
rtt.unbindTexture();
|
||||
});
|
||||
layer->m_dirty_face[plane] = true;
|
||||
layer->m_dirty_box[plane] = box_union(layer->m_dirty_box[plane], bb[plane]);
|
||||
layer->face(plane) = true;
|
||||
layer->box(plane) = box_union(layer->box(plane), bb[plane]);
|
||||
}
|
||||
Canvas::I->m_unsaved = true;
|
||||
}
|
||||
@@ -1713,8 +1713,8 @@ void Canvas::import_equirectangular_thread(std::string file_path, std::shared_pt
|
||||
}
|
||||
for (int i = 0; i < 6; i++)
|
||||
{
|
||||
layer->m_dirty_box[i] = glm::vec4(0, 0, m_width, m_height);
|
||||
layer->m_dirty_face[i] = true;
|
||||
layer->box(i) = glm::vec4(0, 0, m_width, m_height);
|
||||
layer->face(i) = true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1852,9 +1852,9 @@ void Canvas::export_depth_thread(std::string file_name)
|
||||
ShaderManager::u_mat4(kShaderUniform::MVP, plane_mvp_z);
|
||||
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
m_layers_merge.m_rtt[plane_index].bindTexture();
|
||||
m_layers_merge.rtt(plane_index).bindTexture();
|
||||
m_plane.draw_fill();
|
||||
m_layers_merge.m_rtt[plane_index].unbindTexture();
|
||||
m_layers_merge.rtt(plane_index).unbindTexture();
|
||||
}
|
||||
rtt.unbindFramebuffer();
|
||||
});
|
||||
@@ -1878,7 +1878,7 @@ void Canvas::export_depth_thread(std::string file_name)
|
||||
{
|
||||
if ((!m_layers[layer_index]->m_visible ||
|
||||
m_layers[layer_index]->m_opacity == .0f ||
|
||||
!m_layers[layer_index]->m_dirty_face[plane_index]))
|
||||
!m_layers[layer_index]->face(plane_index)))
|
||||
continue;
|
||||
|
||||
auto plane_mvp_z = proj * camera *
|
||||
@@ -1893,9 +1893,9 @@ void Canvas::export_depth_thread(std::string file_name)
|
||||
ShaderManager::u_mat4(kShaderUniform::MVP, plane_mvp_z);
|
||||
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
m_layers[layer_index]->m_rtt[plane_index].bindTexture();
|
||||
m_layers[layer_index]->rtt(plane_index).bindTexture();
|
||||
m_plane.draw_fill();
|
||||
m_layers[layer_index]->m_rtt[plane_index].unbindTexture();
|
||||
m_layers[layer_index]->rtt(plane_index).unbindTexture();
|
||||
}
|
||||
}
|
||||
rtt.unbindFramebuffer();
|
||||
@@ -1967,7 +1967,7 @@ void Canvas::export_cube_faces_thread(std::string file_name)
|
||||
|
||||
for (int i = 0; i < 6; i++)
|
||||
{
|
||||
Image face = m_layers_merge.m_rtt[i].get_image();
|
||||
Image face = m_layers_merge.rtt(i).get_image();
|
||||
std::string path = fmt::format("{}/{}-{}.png", App::I->work_path, file_name, plane_names[i]);
|
||||
face.save_png(path);
|
||||
pb->increment();
|
||||
@@ -2405,7 +2405,7 @@ Image Canvas::thumbnail_generate(int w, int h)
|
||||
{
|
||||
if (!m_layers[layer_index]->m_visible ||
|
||||
m_layers[layer_index]->m_opacity == 0.f ||
|
||||
!m_layers[layer_index]->m_dirty_face[i])
|
||||
!m_layers[layer_index]->face(i))
|
||||
continue;
|
||||
if (!ShaderManager::ext_framebuffer_fetch)
|
||||
{
|
||||
@@ -2415,9 +2415,9 @@ Image Canvas::thumbnail_generate(int w, int h)
|
||||
ShaderManager::u_int(kShaderUniform::BlendMode, m_layers[layer_index]->m_blend_mode);
|
||||
ShaderManager::u_float(kShaderUniform::Alpha, m_layers[layer_index]->m_opacity);
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
m_layers[layer_index]->m_rtt[i].bindTexture();
|
||||
m_layers[layer_index]->rtt(i).bindTexture();
|
||||
m_face_plane.draw_fill();
|
||||
m_layers[layer_index]->m_rtt[i].unbindTexture();
|
||||
m_layers[layer_index]->rtt(i).unbindTexture();
|
||||
}
|
||||
|
||||
if (!ShaderManager::ext_framebuffer_fetch)
|
||||
@@ -2517,16 +2517,16 @@ void Canvas::draw_objects_direct(std::function<void(const glm::mat4& camera, con
|
||||
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();
|
||||
layer.rtt(i).bindFramebuffer();
|
||||
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rboID);
|
||||
|
||||
observer(plane_camera, proj, i);
|
||||
|
||||
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, 0);
|
||||
layer.m_rtt[i].unbindFramebuffer();
|
||||
layer.rtt(i).unbindFramebuffer();
|
||||
|
||||
layer.m_dirty_face[i] = true;
|
||||
layer.m_dirty_box[i] = { 0, 0, layer.w, layer.h };
|
||||
layer.face(i) = true;
|
||||
layer.box(i) = { 0, 0, layer.w, layer.h };
|
||||
}
|
||||
|
||||
glDeleteRenderbuffers(1, &rboID);
|
||||
@@ -2581,7 +2581,7 @@ void Canvas::draw_objects(std::function<void(const glm::mat4& camera, const glm:
|
||||
|
||||
glm::vec4 bounds = rtt.calc_bounds();
|
||||
|
||||
layer.m_rtt[i].bindFramebuffer();
|
||||
layer.rtt(i).bindFramebuffer();
|
||||
|
||||
// save image before commit
|
||||
glm::vec2 box_sz = zw(bounds) - xy(bounds);
|
||||
@@ -2593,8 +2593,8 @@ void Canvas::draw_objects(std::function<void(const glm::mat4& camera, const glm:
|
||||
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];
|
||||
action->m_old_box[i] = layer.box(i);
|
||||
action->m_old_dirty[i] = layer.face(i);
|
||||
|
||||
// draw the tmp layer into the actual layer
|
||||
if (has_data)
|
||||
@@ -2608,11 +2608,11 @@ void Canvas::draw_objects(std::function<void(const glm::mat4& camera, const glm:
|
||||
m_plane.draw_fill();
|
||||
rtt.unbindTexture();
|
||||
|
||||
layer.m_dirty_face[i] = true;
|
||||
layer.m_dirty_box[i] = { glm::min(xy(layer.m_dirty_box[i]), xy(bounds)), glm::max(zw(layer.m_dirty_box[i]), zw(bounds)) };
|
||||
layer.face(i) = true;
|
||||
layer.box(i) = { glm::min(xy(layer.box(i)), xy(bounds)), glm::max(zw(layer.box(i)), zw(bounds)) };
|
||||
}
|
||||
|
||||
layer.m_rtt[i].unbindFramebuffer();
|
||||
layer.rtt(i).unbindFramebuffer();
|
||||
}
|
||||
|
||||
// save history
|
||||
@@ -2733,184 +2733,3 @@ void Canvas::set_camera(const CameraData& c)
|
||||
m_proj = c.m_proj;
|
||||
m_vp = c.m_vp;
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
void Layer::destroy()
|
||||
{
|
||||
for (int i = 0; i < 6; i++)
|
||||
m_rtt[i].destroy();
|
||||
}
|
||||
|
||||
void Layer::optimize()
|
||||
{
|
||||
int saved_bytes = 0;
|
||||
for (int i = 0; i < 6; i++)
|
||||
{
|
||||
if (!m_dirty_face[i])
|
||||
continue;
|
||||
|
||||
auto data = std::unique_ptr<glm::u8vec4[]>(reinterpret_cast<glm::u8vec4*>(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 });
|
||||
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)
|
||||
{
|
||||
m_dirty_box[i] = glm::vec4(snap.width, snap.height, 0, 0);
|
||||
m_dirty_face[i] = false;
|
||||
continue;
|
||||
}
|
||||
|
||||
m_dirty_box[i] = snap.m_dirty_box[i];
|
||||
m_dirty_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]
|
||||
{
|
||||
m_rtt[i].bindTexture();
|
||||
glm::vec2 box_sz = zw(m_dirty_box[i]) - xy(m_dirty_box[i]);
|
||||
glTexSubImage2D(GL_TEXTURE_2D, 0,
|
||||
m_dirty_box[i].x, m_dirty_box[i].y,
|
||||
box_sz.x, box_sz.y, GL_RGBA, GL_UNSIGNED_BYTE,
|
||||
snap.image[i].get());
|
||||
m_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) : m_dirty_box[i];
|
||||
snap.m_dirty_face[i] = dirty_face ? dirty_face->at(i) : m_dirty_face[i];
|
||||
|
||||
if (!snap.m_dirty_face[i])
|
||||
continue;
|
||||
|
||||
snap.image[i] = std::make_unique<uint8_t[]>(m_rtt[i].bytes());
|
||||
|
||||
App::I->render_task_async([this,i,&snap]
|
||||
{
|
||||
m_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());
|
||||
m_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++)
|
||||
{
|
||||
m_rtt[i].bindFramebuffer();
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
m_rtt[i].unbindFramebuffer();
|
||||
|
||||
if (erase)
|
||||
{
|
||||
m_dirty_box[i] = glm::vec4(w, h, 0, 0); // reset bounding box
|
||||
m_dirty_face[i] = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
m_dirty_box[i] = glm::vec4(0, 0, w, h); // reset bounding box
|
||||
m_dirty_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;
|
||||
App::I->render_task([&]
|
||||
{
|
||||
for (int i = 0; i < 6; i++)
|
||||
{
|
||||
m_rtt[i].create(width, height);
|
||||
m_rtt[i].bindFramebuffer();
|
||||
m_rtt[i].clear();
|
||||
m_rtt[i].unbindFramebuffer();
|
||||
m_dirty_box[i] = glm::vec4(w, h, 0, 0); // reset bounding box
|
||||
m_dirty_face[i] = false;
|
||||
}
|
||||
});
|
||||
return true;
|
||||
}
|
||||
|
||||
void Layer::resize(int width, int height)
|
||||
{
|
||||
glm::vec2 ratio = glm::vec2(width, height) / glm::vec2(w, h);
|
||||
w = width;
|
||||
h = height;
|
||||
for (int i = 0; i < 6; i++)
|
||||
{
|
||||
m_rtt[i].resize(width, height);
|
||||
m_dirty_box[i] = m_dirty_box[i] * glm::vec4(ratio, ratio);
|
||||
//m_dirty_face[i] = true;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user