786 lines
27 KiB
C++
786 lines
27 KiB
C++
#include "pch.h"
|
|
#include "log.h"
|
|
#include "node_stroke_preview.h"
|
|
#include "texture.h"
|
|
#include "shader.h"
|
|
#include "bezier.h"
|
|
#include "canvas.h"
|
|
#include "app.h"
|
|
#include "legacy_canvas_draw_merge_services.h"
|
|
#include "legacy_canvas_stroke_composite_services.h"
|
|
#include "legacy_canvas_stroke_execution_services.h"
|
|
#include "legacy_canvas_stroke_preview_services.h"
|
|
#include "legacy_canvas_stroke_shader_services.h"
|
|
#include "legacy_canvas_stroke_services.h"
|
|
#include "legacy_node_stroke_preview_execution_services.h"
|
|
#include "legacy_ui_gl_dispatch.h"
|
|
#include "paint_renderer/compositor.h"
|
|
#include "renderer_gl/opengl_capabilities.h"
|
|
#include "util.h"
|
|
#include <array>
|
|
#include <cstdint>
|
|
|
|
namespace {
|
|
|
|
pp::renderer::RenderDeviceFeatures stroke_preview_render_device_features() noexcept
|
|
{
|
|
return ShaderManager::render_device_features();
|
|
}
|
|
|
|
pp::paint_renderer::CanvasStrokeFeedbackPlan stroke_preview_destination_feedback_plan(
|
|
int width,
|
|
int height) noexcept
|
|
{
|
|
return pp::panopainter::plan_legacy_node_stroke_preview_feedback(
|
|
stroke_preview_render_device_features(),
|
|
width,
|
|
height);
|
|
}
|
|
|
|
pp::paint_renderer::CanvasStrokeMaterialPlan stroke_preview_material_plan(
|
|
const Brush& brush,
|
|
bool destination_feedback_needed) noexcept
|
|
{
|
|
return pp::panopainter::plan_legacy_canvas_stroke_material(
|
|
pp::paint_renderer::CanvasStrokeMaterialRequest {
|
|
.destination_feedback_needed = destination_feedback_needed,
|
|
.pattern_enabled = brush.m_pattern_enabled,
|
|
.pattern_eachsample = brush.m_pattern_eachsample,
|
|
.wet_blend = brush.m_tip_wet > 0.F,
|
|
.mix_blend = brush.m_tip_mix > 0.F,
|
|
.noise_enabled = brush.m_tip_noise > 0.F,
|
|
.dual_brush_enabled = brush.m_dual_enabled,
|
|
.dual_blend_mode = brush.m_dual_blend_mode,
|
|
.pattern_blend_mode = brush.m_pattern_blend_mode,
|
|
.dual_alpha = brush.m_dual_opacity,
|
|
});
|
|
}
|
|
|
|
void set_active_texture_unit(std::uint32_t unit_index)
|
|
{
|
|
pp::legacy::ui_gl::activate_texture_unit(unit_index, "NodeStrokePreview");
|
|
}
|
|
|
|
void unbind_texture_2d()
|
|
{
|
|
pp::legacy::ui_gl::unbind_texture_2d("NodeStrokePreview");
|
|
}
|
|
|
|
void apply_stroke_preview_viewport(std::int32_t x, std::int32_t y, std::int32_t width, std::int32_t height)
|
|
{
|
|
pp::legacy::ui_gl::apply_viewport(x, y, width, height, "NodeStrokePreview");
|
|
}
|
|
|
|
pp::renderer::gl::OpenGlViewportRect query_stroke_preview_viewport()
|
|
{
|
|
return pp::legacy::ui_gl::query_viewport_rect("NodeStrokePreview");
|
|
}
|
|
|
|
std::array<float, 4> query_stroke_preview_clear_color()
|
|
{
|
|
return pp::legacy::ui_gl::query_clear_color("NodeStrokePreview");
|
|
}
|
|
|
|
void apply_stroke_preview_clear_color(std::array<float, 4> color)
|
|
{
|
|
pp::legacy::ui_gl::set_clear_color(color, "NodeStrokePreview");
|
|
}
|
|
|
|
void apply_stroke_preview_scissor(std::int32_t x, std::int32_t y, std::int32_t width, std::int32_t height)
|
|
{
|
|
pp::legacy::ui_gl::apply_scissor_rect(x, y, width, height, "NodeStrokePreview");
|
|
}
|
|
|
|
void apply_stroke_preview_capability(std::uint32_t state, bool enabled)
|
|
{
|
|
pp::legacy::ui_gl::set_capability(state, enabled, "NodeStrokePreview");
|
|
}
|
|
|
|
}
|
|
|
|
std::atomic_int NodeStrokePreview::s_instances{ 0 };
|
|
std::atomic_bool NodeStrokePreview::s_running{ false };
|
|
std::mutex NodeStrokePreview::s_render_mutex;
|
|
std::thread NodeStrokePreview::s_renderer;
|
|
BlockingQueue<std::shared_ptr<NodeStrokePreview>> NodeStrokePreview::s_queue;
|
|
|
|
RTT NodeStrokePreview::m_rtt;
|
|
RTT NodeStrokePreview::m_rtt_mixer;
|
|
Texture2D NodeStrokePreview::m_tex; // blending tmp texture
|
|
Texture2D NodeStrokePreview::m_tex_dual;
|
|
Texture2D NodeStrokePreview::m_tex_background;
|
|
Sampler NodeStrokePreview::m_sampler_linear;
|
|
Sampler NodeStrokePreview::m_sampler_linear_repeat;
|
|
Sampler NodeStrokePreview::m_sampler_mipmap;
|
|
DynamicShape NodeStrokePreview::m_brush_shape;
|
|
|
|
|
|
void NodeStrokePreview::terminate_renderer()
|
|
{
|
|
if (s_running && s_renderer.joinable())
|
|
{
|
|
s_running = false;
|
|
s_queue.UnlockGetters();
|
|
s_renderer.join();
|
|
}
|
|
}
|
|
|
|
void NodeStrokePreview::empty_queue()
|
|
{
|
|
s_queue.q.clear();
|
|
}
|
|
|
|
Node* NodeStrokePreview::clone_instantiate() const
|
|
{
|
|
return new NodeStrokePreview();
|
|
}
|
|
|
|
void NodeStrokePreview::clone_copy(Node* dest) const
|
|
{
|
|
NodeBorder::clone_copy(dest);
|
|
}
|
|
|
|
void NodeStrokePreview::clone_children(Node* dest) const
|
|
{
|
|
// stop children cloning
|
|
}
|
|
|
|
void NodeStrokePreview::clone_finalize(Node* dest) const
|
|
{
|
|
NodeStrokePreview* n = (NodeStrokePreview*)dest;
|
|
n->init_controls();
|
|
}
|
|
|
|
void NodeStrokePreview::init_controls()
|
|
{
|
|
// TextureManager::load("data/thumbs/Round-Hard.png");
|
|
// Canvas::I->m_current_brush.m_tex_id = const_hash("data/thumbs/Round-Hard.png");
|
|
}
|
|
|
|
void NodeStrokePreview::restore_context()
|
|
{
|
|
NodeBorder::restore_context();
|
|
init_controls();
|
|
if (m_size.x > 0 && m_size.y > 0)
|
|
m_tex_preview.create(static_cast<int>(m_size.x), static_cast<int>(m_size.y));
|
|
draw_stroke();
|
|
}
|
|
|
|
void NodeStrokePreview::clear_context()
|
|
{
|
|
NodeBorder::clear_context();
|
|
m_tex_preview.destroy();
|
|
}
|
|
|
|
void NodeStrokePreview::stroke_draw_mix(const glm::vec2& bb_min, const glm::vec2& bb_sz)
|
|
{
|
|
gl_state gl;
|
|
gl.save();
|
|
|
|
m_rtt_mixer.bindFramebuffer();
|
|
|
|
apply_stroke_preview_viewport(0, 0, m_rtt_mixer.getWidth(), m_rtt_mixer.getHeight());
|
|
apply_stroke_preview_capability(pp::renderer::gl::depth_test_state(), false);
|
|
apply_stroke_preview_capability(pp::renderer::gl::scissor_test_state(), true);
|
|
apply_stroke_preview_capability(pp::renderer::gl::blend_state(), false);
|
|
|
|
apply_stroke_preview_scissor(
|
|
static_cast<int>(bb_min.x),
|
|
static_cast<int>(bb_min.y),
|
|
static_cast<int>(bb_sz.x),
|
|
static_cast<int>(bb_sz.y));
|
|
|
|
const auto& b = m_brush;
|
|
glm::vec2 patt_scale = glm::vec2(b->m_pattern_scale);
|
|
if (b->m_pattern_flipx) patt_scale.x *= -1.f;
|
|
if (b->m_pattern_flipy) patt_scale.y *= -1.f;
|
|
|
|
const auto material = stroke_preview_material_plan(*b, false);
|
|
pp::panopainter::setup_legacy_stroke_composite_shader(
|
|
pp::panopainter::LegacyStrokeCompositeUniforms {
|
|
.resolution = m_size,
|
|
.pattern = {
|
|
.scale = patt_scale,
|
|
.invert = static_cast<float>(b->m_pattern_invert),
|
|
.brightness = b->m_pattern_brightness,
|
|
.contrast = b->m_pattern_contrast,
|
|
.depth = b->m_pattern_depth,
|
|
.blend_mode = material.composite_pass.pattern_blend_mode,
|
|
.offset = glm::vec2(b->m_pattern_rand_offset ? 0.5f : 0.0f),
|
|
},
|
|
.mvp = glm::ortho(-.5f, .5f, -.5f, .5f, -1.f, 1.f),
|
|
.layer_alpha = 1.0f,
|
|
.alpha_lock = false,
|
|
.mask_enabled = false,
|
|
.use_fragcoord = false,
|
|
.blend_mode = b->m_blend_mode,
|
|
.use_dual = material.composite_pass.use_dual,
|
|
.dual_blend_mode = material.composite_pass.dual_blend_mode,
|
|
.dual_alpha = material.composite_pass.dual_alpha,
|
|
.use_pattern = material.composite_pass.use_pattern,
|
|
});
|
|
|
|
m_sampler_linear.bind(0);
|
|
set_active_texture_unit(0U);
|
|
m_tex_background.bind();
|
|
set_active_texture_unit(1U);
|
|
m_rtt.bindTexture();
|
|
set_active_texture_unit(3U);
|
|
m_tex_dual.bind();
|
|
set_active_texture_unit(4U);
|
|
b->m_pattern_texture ?
|
|
b->m_pattern_texture->bind() :
|
|
unbind_texture_2d();
|
|
m_plane.draw_fill();
|
|
|
|
m_rtt_mixer.unbindFramebuffer();
|
|
gl.restore();
|
|
}
|
|
|
|
glm::vec4 NodeStrokePreview::stroke_draw_samples(
|
|
std::array<vertex_t, 4>& P,
|
|
Texture2D& blend_tex,
|
|
bool copy_stroke_destination)
|
|
{
|
|
const glm::vec2 size = { m_rtt.getWidth(), m_rtt.getHeight() };
|
|
const std::array<pp::paint_renderer::CanvasStrokePoint, 4> sample_points {
|
|
pp::paint_renderer::CanvasStrokePoint { .x = P[0].pos.x, .y = P[0].pos.y },
|
|
pp::paint_renderer::CanvasStrokePoint { .x = P[1].pos.x, .y = P[1].pos.y },
|
|
pp::paint_renderer::CanvasStrokePoint { .x = P[2].pos.x, .y = P[2].pos.y },
|
|
pp::paint_renderer::CanvasStrokePoint { .x = P[3].pos.x, .y = P[3].pos.y },
|
|
};
|
|
const auto result = pp::panopainter::execute_legacy_canvas_stroke_sample(
|
|
pp::panopainter::LegacyStrokeSampleExecutionRequest {
|
|
.context = "NodeStrokePreview::stroke_draw_samples",
|
|
.target_size = size,
|
|
.vertices = P,
|
|
.sample_points = sample_points,
|
|
.copy_stroke_destination = copy_stroke_destination,
|
|
.bind_destination_texture = [&] {
|
|
set_active_texture_unit(1U);
|
|
blend_tex.bind(); // bg, copy of framebuffer (copied before drawing)
|
|
},
|
|
.copy_framebuffer_to_destination_texture = [](
|
|
int src_x,
|
|
int src_y,
|
|
int dst_x,
|
|
int dst_y,
|
|
int width,
|
|
int height) {
|
|
// this is also used by the mixer
|
|
copy_framebuffer_to_texture_2d(src_x, src_y, dst_x, dst_y, width, height);
|
|
},
|
|
.unbind_destination_texture = [&] {
|
|
set_active_texture_unit(1U);
|
|
blend_tex.unbind();
|
|
},
|
|
.upload_brush_vertices = [&](std::span<const vertex_t> vertices) {
|
|
m_brush_shape.update_vertices(
|
|
const_cast<vertex_t*>(vertices.data()),
|
|
static_cast<int>(vertices.size()));
|
|
},
|
|
.draw_brush_shape = [&] {
|
|
m_brush_shape.draw_fill();
|
|
},
|
|
});
|
|
|
|
return result.dirty_bounds;
|
|
}
|
|
|
|
std::vector<NodeStrokePreview::StrokeFrame> NodeStrokePreview::stroke_draw_compute(Stroke& stroke, float zoom) const
|
|
{
|
|
auto samples = stroke.compute_samples();
|
|
StrokeSample previous_sample = stroke.m_prev_sample;
|
|
previous_sample.size *= zoom;
|
|
for (auto& sample : samples) {
|
|
sample.size *= zoom;
|
|
}
|
|
|
|
return pp::panopainter::plan_legacy_canvas_stroke_frames(
|
|
pp::panopainter::LegacyCanvasStrokeComputeRequest {
|
|
.previous_sample = previous_sample,
|
|
.samples = samples,
|
|
.zoom = 1.0f,
|
|
.mixer_size = glm::vec2(m_rtt.getWidth(), m_rtt.getHeight()),
|
|
},
|
|
[](
|
|
std::array<vertex_t, 4>& brush_quad,
|
|
bool /*project_3d*/,
|
|
glm::mat4 /*model_view*/) {
|
|
return brush_quad;
|
|
},
|
|
[](
|
|
glm::vec4 mixer_rect,
|
|
glm::vec4 color,
|
|
float flow,
|
|
float opacity,
|
|
std::array<vertex_t, 4>&& shapes) {
|
|
return StrokeFrame {
|
|
.col = color,
|
|
.flow = flow,
|
|
.opacity = opacity,
|
|
.shapes = std::move(shapes),
|
|
.m_mixer_rect = mixer_rect,
|
|
};
|
|
});
|
|
}
|
|
|
|
void NodeStrokePreview::draw_stroke_immediate()
|
|
{
|
|
if (m_size.x == 0 || m_size.y == 0)
|
|
return;
|
|
|
|
const auto vp = query_stroke_preview_viewport();
|
|
const auto cc = query_stroke_preview_clear_color();
|
|
|
|
float zoom = root()->m_zoom;
|
|
|
|
glm::vec2 size = { m_rtt.getWidth(), m_rtt.getHeight() };
|
|
glm::mat4 ortho_proj = glm::ortho<float>(0, size.x, 0, size.y, -1, 1);
|
|
apply_stroke_preview_viewport(0, 0, m_rtt.getWidth(), m_rtt.getHeight());
|
|
m_rtt.bindFramebuffer();
|
|
m_rtt.clear();
|
|
m_sampler_mipmap.bind(0);
|
|
m_sampler_linear.bind(1);
|
|
m_sampler_linear_repeat.bind(2);
|
|
m_sampler_linear.bind(3);
|
|
m_sampler_linear.bind(4);
|
|
|
|
const auto& b = m_brush;
|
|
|
|
Stroke m_stroke;
|
|
Stroke m_dual_stroke;
|
|
|
|
m_stroke.m_filter_points = false;
|
|
m_stroke.m_max_size = m_max_size > 0 ? m_max_size : m_size.y * .75f;
|
|
m_stroke.m_camera.fov = Canvas::I->m_cam_fov;
|
|
m_stroke.m_camera.rot = Canvas::I->m_cam_rot;
|
|
m_stroke.reset(true);
|
|
m_stroke.start(b);
|
|
|
|
auto dual_brush = std::make_shared<Brush>();
|
|
dual_brush->m_tip_scale = b->m_dual_scale;
|
|
dual_brush->m_tip_angle = b->m_dual_angle;
|
|
dual_brush->m_tip_flow = b->m_dual_flow;
|
|
dual_brush->m_tip_opacity = b->m_dual_opacity;
|
|
dual_brush->m_tip_flipx = b->m_dual_flipx;
|
|
dual_brush->m_tip_flipy = b->m_dual_flipy;
|
|
dual_brush->m_tip_invert = b->m_dual_invert;
|
|
dual_brush->m_blend_mode = b->m_dual_blend_mode;
|
|
dual_brush->m_tip_randflipx = b->m_dual_randflip;
|
|
dual_brush->m_tip_randflipy = b->m_dual_randflip;
|
|
dual_brush->m_tip_size = b->m_dual_size * b->m_tip_size;
|
|
dual_brush->m_tip_spacing = b->m_dual_spacing;
|
|
dual_brush->m_jitter_scatter = b->m_dual_scatter;
|
|
dual_brush->m_jitter_scatter_bothaxis = b->m_dual_scatter_bothaxis;
|
|
dual_brush->m_jitter_angle = b->m_dual_rotate;
|
|
dual_brush->m_tip_texture = b->m_dual_texture;
|
|
dual_brush->m_tip_aspect = b->m_dual_aspect;
|
|
|
|
if (b->m_dual_enabled)
|
|
{
|
|
m_dual_stroke.m_filter_points = false;
|
|
m_dual_stroke.m_max_size = m_stroke.m_max_size * b->m_dual_size;
|
|
m_dual_stroke.m_camera.fov = Canvas::I->m_cam_fov;
|
|
m_dual_stroke.m_camera.rot = Canvas::I->m_cam_rot;
|
|
m_dual_stroke.reset(true);
|
|
m_dual_stroke.start(dual_brush);
|
|
}
|
|
|
|
{
|
|
float min_pad = size.x * 0.05f;
|
|
float pad = (5.f + glm::max(glm::min(m_stroke.m_max_size, m_brush->m_tip_size) / 2.f, min_pad)) * zoom;
|
|
if (b->m_tip_size_pressure)
|
|
pad = min_pad * zoom;
|
|
if (!glm::isnan(m_pad_override))
|
|
pad = m_pad_override;
|
|
float w = m_size.x * zoom;
|
|
float h = m_size.y * zoom;
|
|
std::vector<glm::vec2> kp = {
|
|
{ pad, h / 2.f },
|
|
{ w / 2.f, 0 },
|
|
{ w / 2.f, h },
|
|
{ w - pad, h / 2.f },
|
|
};
|
|
for (int i = 0; i < 100; i++)
|
|
{
|
|
float t = (float)i / 100.f;
|
|
float p = glm::clamp((1.f - glm::abs(t * 2.f - 1.f)) * 1.1f, 0.f, 1.f);
|
|
m_stroke.add_point(glm::vec3(BezierCurve::Bezier2D(kp, t), 0), p);
|
|
if (b->m_dual_enabled)
|
|
m_dual_stroke.add_point(glm::vec3(BezierCurve::Bezier2D(kp, t), 0), p);
|
|
}
|
|
}
|
|
|
|
glm::vec2 patt_scale = glm::vec2(b->m_pattern_scale);
|
|
if (b->m_pattern_flipx) patt_scale.x *= -1.f;
|
|
if (b->m_pattern_flipy) patt_scale.y *= -1.f;
|
|
|
|
apply_stroke_preview_capability(pp::renderer::gl::blend_state(), false);
|
|
const auto stroke_feedback = stroke_preview_destination_feedback_plan(m_rtt.getWidth(), m_rtt.getHeight());
|
|
const bool copy_stroke_destination = !stroke_feedback.reads_destination_color;
|
|
const auto material = stroke_preview_material_plan(*b, copy_stroke_destination);
|
|
const auto preview_composite_plan = pp::panopainter::plan_legacy_node_stroke_preview_composite(
|
|
b->m_tip_mix > 0.0f,
|
|
material.composite_pass.use_dual,
|
|
material.composite_pass.use_pattern);
|
|
pp::panopainter::setup_legacy_stroke_shader(
|
|
pp::panopainter::LegacyStrokeShaderSetupUniforms {
|
|
.resolution = size,
|
|
.pattern = {
|
|
.scale = patt_scale,
|
|
.invert = static_cast<float>(b->m_pattern_invert),
|
|
.brightness = b->m_pattern_brightness,
|
|
.contrast = b->m_pattern_contrast,
|
|
.depth = b->m_pattern_depth,
|
|
.blend_mode = b->m_pattern_blend_mode,
|
|
.offset = glm::vec2(b->m_pattern_rand_offset ? 0.5f : 0.0f),
|
|
},
|
|
.mvp = ortho_proj,
|
|
.uses_destination_feedback = copy_stroke_destination,
|
|
.uses_pattern = false,
|
|
.mix_alpha = 0.0f,
|
|
.wet = 0.0f,
|
|
.noise = 0.0f,
|
|
.set_opacity = false,
|
|
});
|
|
|
|
// DRAW DUAL BRUSH
|
|
|
|
if (material.dual_pass.enabled)
|
|
{
|
|
m_rtt.clear();
|
|
pp::panopainter::setup_legacy_stroke_dual_shader(material.dual_pass.uses_pattern);
|
|
set_active_texture_unit(0U);
|
|
dual_brush->m_tip_texture ?
|
|
dual_brush->m_tip_texture->bind() :
|
|
unbind_texture_2d();
|
|
auto frames_dual = stroke_draw_compute(m_dual_stroke, zoom);
|
|
for (auto& f : frames_dual)
|
|
{
|
|
pp::panopainter::apply_legacy_stroke_sample_uniforms(
|
|
pp::panopainter::LegacyStrokeSampleUniforms {
|
|
.color = { 0, 0, 0, 1 },
|
|
.alpha = f.flow,
|
|
.opacity = f.opacity,
|
|
});
|
|
/*auto rect =*/ stroke_draw_samples(f.shapes, m_tex_dual, copy_stroke_destination);
|
|
}
|
|
|
|
// copy raw stroke to tex
|
|
set_active_texture_unit(1U);
|
|
m_tex_dual.bind();
|
|
copy_framebuffer_to_texture_2d(
|
|
0,
|
|
0,
|
|
0,
|
|
0,
|
|
static_cast<int>(size.x),
|
|
static_cast<int>(size.y));
|
|
}
|
|
|
|
// CHEKCERBOARD
|
|
|
|
pp::panopainter::execute_legacy_stroke_preview_background_capture(
|
|
[&] {
|
|
// copy background color to tex2
|
|
const float aspect = size.x / size.y;
|
|
pp::panopainter::setup_legacy_canvas_draw_merge_checkerboard_shader(
|
|
pp::panopainter::LegacyCanvasDrawMergeCheckerboardUniforms {
|
|
.mvp = glm::ortho(-.5f, .5f, -.5f / aspect, .5f / aspect, -1.f, 1.f),
|
|
.colorize = b->m_tip_mix > 0.f || b->m_blend_mode != 0,
|
|
});
|
|
},
|
|
[&] {
|
|
m_plane.draw_fill();
|
|
},
|
|
[&] {
|
|
//m_rtt.clear({ .3f, .3f, .3f, 1.f });
|
|
m_tex_background.bind();
|
|
},
|
|
[](
|
|
int src_x,
|
|
int src_y,
|
|
int dst_x,
|
|
int dst_y,
|
|
int width,
|
|
int height) {
|
|
copy_framebuffer_to_texture_2d(src_x, src_y, dst_x, dst_y, width, height);
|
|
},
|
|
pp::panopainter::LegacyStrokePreviewCopySize {
|
|
.width = static_cast<int>(size.x),
|
|
.height = static_cast<int>(size.y),
|
|
});
|
|
|
|
// DRAW MAIN BRUSH
|
|
|
|
pp::panopainter::use_legacy_stroke_shader();
|
|
pp::panopainter::apply_legacy_stroke_blend_uniforms(
|
|
material.stroke_pass.uses_pattern,
|
|
b->m_tip_mix,
|
|
b->m_tip_wet,
|
|
b->m_tip_noise);
|
|
|
|
set_active_texture_unit(0U);
|
|
b->m_tip_texture->bind();
|
|
if (copy_stroke_destination)
|
|
{
|
|
set_active_texture_unit(1U);
|
|
m_tex.bind(); // tmp swap for blending
|
|
}
|
|
set_active_texture_unit(2U);
|
|
b->m_pattern_texture ?
|
|
b->m_pattern_texture->bind() :
|
|
unbind_texture_2d();
|
|
set_active_texture_unit(3U);
|
|
preview_composite_plan.uses_mixer ? m_rtt_mixer.bindTexture() : unbind_texture_2d();
|
|
auto frames = stroke_draw_compute(m_stroke, zoom);
|
|
m_rtt.clear();
|
|
for (auto& f : frames)
|
|
{
|
|
if (b->m_tip_mix > 0.f)
|
|
{
|
|
stroke_draw_mix(xy(f.m_mixer_rect), zw(f.m_mixer_rect));
|
|
}
|
|
|
|
pp::panopainter::use_legacy_stroke_shader();
|
|
pp::panopainter::apply_legacy_stroke_sample_uniforms(
|
|
pp::panopainter::LegacyStrokeSampleUniforms {
|
|
.color = b->m_blend_mode != 0 || b->m_tip_mix > 0.f ?
|
|
glm::vec4 { .7, .4, .1, 1 } /*f.col*/ :
|
|
glm::vec4 { 0, 0, 0, 1 } /*f.col*/,
|
|
.alpha = glm::max(f.flow, m_min_flow),
|
|
.opacity = f.opacity,
|
|
});
|
|
/*auto rect =*/ stroke_draw_samples(f.shapes, m_tex, copy_stroke_destination);
|
|
}
|
|
set_active_texture_unit(3U);
|
|
m_rtt_mixer.unbindTexture();
|
|
|
|
// copy raw stroke to tex
|
|
set_active_texture_unit(1U);
|
|
m_tex.bind();
|
|
copy_framebuffer_to_texture_2d(
|
|
0,
|
|
0,
|
|
0,
|
|
0,
|
|
static_cast<int>(size.x),
|
|
static_cast<int>(size.y));
|
|
|
|
// COMPOSITE
|
|
|
|
pp::panopainter::execute_legacy_stroke_preview_final_composite(
|
|
[&] {
|
|
pp::panopainter::setup_legacy_stroke_composite_shader(
|
|
pp::panopainter::LegacyStrokeCompositeUniforms {
|
|
.resolution = size,
|
|
.pattern = {
|
|
.scale = patt_scale,
|
|
.invert = static_cast<float>(b->m_pattern_invert),
|
|
.brightness = b->m_pattern_brightness,
|
|
.contrast = b->m_pattern_contrast,
|
|
.depth = b->m_pattern_depth,
|
|
.blend_mode = material.composite_pass.pattern_blend_mode,
|
|
.offset = glm::vec2(b->m_pattern_rand_offset ? 0.5f : 0.0f),
|
|
},
|
|
.mvp = glm::ortho(-.5f, .5f, -.5f, .5f, -1.f, 1.f),
|
|
.layer_alpha = 1.0f,
|
|
.alpha_lock = false,
|
|
.mask_enabled = false,
|
|
.use_fragcoord = false,
|
|
.blend_mode = b->m_blend_mode,
|
|
.use_dual = material.composite_pass.use_dual,
|
|
.dual_blend_mode = material.composite_pass.dual_blend_mode,
|
|
.dual_alpha = material.composite_pass.dual_alpha,
|
|
.use_pattern = material.composite_pass.use_pattern,
|
|
});
|
|
},
|
|
[&] {
|
|
m_sampler_linear.bind(0);
|
|
m_sampler_linear.bind(1);
|
|
m_sampler_linear.bind(2);
|
|
m_sampler_linear.bind(3);
|
|
m_sampler_linear_repeat.bind(4);
|
|
},
|
|
[&] {
|
|
set_active_texture_unit(0U);
|
|
m_tex_background.bind();
|
|
set_active_texture_unit(1U);
|
|
m_tex.bind();
|
|
set_active_texture_unit(3U);
|
|
m_tex_dual.bind();
|
|
set_active_texture_unit(4U);
|
|
b->m_pattern_texture ?
|
|
b->m_pattern_texture->bind() :
|
|
unbind_texture_2d();
|
|
},
|
|
[&] {
|
|
m_plane.draw_fill();
|
|
});
|
|
|
|
// copy the result to the actual preview
|
|
pp::panopainter::copy_legacy_stroke_preview_texture(
|
|
[&] {
|
|
m_tex_preview.bind();
|
|
},
|
|
[](
|
|
int src_x,
|
|
int src_y,
|
|
int dst_x,
|
|
int dst_y,
|
|
int width,
|
|
int height) {
|
|
copy_framebuffer_to_texture_2d(src_x, src_y, dst_x, dst_y, width, height);
|
|
},
|
|
pp::panopainter::LegacyStrokePreviewCopySize {
|
|
.width = static_cast<int>(size.x),
|
|
.height = static_cast<int>(size.y),
|
|
});
|
|
|
|
m_rtt.unbindFramebuffer();
|
|
|
|
apply_stroke_preview_viewport(vp.x, vp.y, vp.width, vp.height);
|
|
apply_stroke_preview_clear_color(cc);
|
|
}
|
|
|
|
Image NodeStrokePreview::render_to_image()
|
|
{
|
|
std::lock_guard<std::mutex> _lock(s_render_mutex);
|
|
|
|
App::I->render_task([this] {
|
|
auto new_size = m_preview_size;
|
|
if (!m_tex_preview.ready() || m_tex_preview.size() != new_size)
|
|
m_tex_preview.create((int)new_size.x, (int)new_size.y);
|
|
if (m_tex.size() != new_size)
|
|
{
|
|
m_rtt.create((int)new_size.x, (int)new_size.y);
|
|
m_rtt_mixer.create((int)new_size.x, (int)new_size.y);
|
|
m_tex.create((int)new_size.x, (int)new_size.y);
|
|
m_tex_dual.create((int)new_size.x, (int)new_size.y);
|
|
m_tex_background.create((int)new_size.x, (int)new_size.y);
|
|
}
|
|
draw_stroke_immediate();
|
|
});
|
|
return m_tex_preview.get_image();
|
|
}
|
|
|
|
void NodeStrokePreview::draw_stroke()
|
|
{
|
|
if (m_size.x == 0 || m_size.y == 0)
|
|
return;
|
|
s_queue.mutex.lock();
|
|
if (!s_running)
|
|
{
|
|
s_running = true;
|
|
s_renderer = std::thread([] {
|
|
BT_SetTerminate();
|
|
|
|
m_sampler_linear.create();
|
|
m_sampler_linear_repeat.create(
|
|
pp::renderer::gl::linear_texture_filter(),
|
|
pp::renderer::gl::repeat_texture_wrap());
|
|
m_sampler_mipmap.create();
|
|
m_sampler_mipmap.set_filter(
|
|
pp::renderer::gl::linear_mipmap_linear_texture_filter(),
|
|
pp::renderer::gl::linear_texture_filter());
|
|
m_brush_shape.create();
|
|
while (s_running)
|
|
{
|
|
auto node = s_queue.Get();
|
|
if (node)
|
|
{
|
|
std::lock_guard<std::mutex> _lock(s_render_mutex);
|
|
|
|
// if the brush is not already loaded, load it and then destroy it
|
|
bool to_unload = (node->m_brush->m_tip_texture == nullptr);
|
|
node->m_brush->preload();
|
|
|
|
App::I->render_task([node, to_unload]
|
|
{
|
|
gl_state gl;
|
|
gl.save();
|
|
|
|
auto new_size = node->m_preview_size;
|
|
if (!node->m_tex_preview.ready() || node->m_tex_preview.size() != new_size)
|
|
node->m_tex_preview.create((int)new_size.x, (int)new_size.y);
|
|
if (m_tex.size() != new_size)
|
|
{
|
|
m_rtt.create((int)new_size.x, (int)new_size.y);
|
|
m_rtt_mixer.create((int)new_size.x, (int)new_size.y);
|
|
m_tex.create((int)new_size.x, (int)new_size.y);
|
|
m_tex_dual.create((int)new_size.x, (int)new_size.y);
|
|
m_tex_background.create((int)new_size.x, (int)new_size.y);
|
|
}
|
|
|
|
node->m_brush->load();
|
|
node->draw_stroke_immediate();
|
|
if (to_unload)
|
|
node->m_brush->unload();
|
|
|
|
gl.restore();
|
|
});
|
|
|
|
node->app_redraw();
|
|
|
|
//std::this_thread::sleep_for(std::chrono::milliseconds(30));
|
|
std::this_thread::yield();
|
|
}
|
|
}
|
|
m_rtt.destroy();
|
|
m_rtt_mixer.destroy();
|
|
m_tex.destroy();
|
|
m_tex_dual.destroy();
|
|
m_tex_background.destroy();
|
|
m_brush_shape.destroy();
|
|
});
|
|
}
|
|
s_queue.mutex.unlock();
|
|
s_queue.PostUnique(std::static_pointer_cast<NodeStrokePreview>(shared_from_this()), m_draw_first);
|
|
}
|
|
|
|
void NodeStrokePreview::draw()
|
|
{
|
|
pp::panopainter::setup_legacy_canvas_draw_merge_texture_shader(
|
|
pp::panopainter::LegacyCanvasDrawMergeTextureUniforms {
|
|
.mvp = m_mvp,
|
|
.texture_slot = 0,
|
|
});
|
|
m_tex_preview.bind();
|
|
m_sampler_linear.bind(0);
|
|
m_plane.draw_fill();
|
|
m_sampler_linear.unbind();
|
|
m_tex_preview.unbind();
|
|
}
|
|
|
|
void NodeStrokePreview::handle_resize(glm::vec2 old_size, glm::vec2 new_size, float zoom)
|
|
{
|
|
if (m_preview_size == (new_size * root()->m_zoom) || !m_brush)
|
|
return;
|
|
|
|
m_preview_size = new_size * root()->m_zoom;
|
|
|
|
if (m_on_screen)
|
|
draw_stroke();
|
|
}
|
|
|
|
void NodeStrokePreview::destroy()
|
|
{
|
|
m_tex_preview.destroy();
|
|
Node::destroy();
|
|
}
|
|
|
|
void NodeStrokePreview::handle_on_screen(bool old_visibility, bool new_visibility)
|
|
{
|
|
parent::handle_on_screen(old_visibility, new_visibility);
|
|
if (new_visibility)
|
|
{
|
|
draw_stroke();
|
|
}
|
|
else
|
|
{
|
|
s_queue.Remove(std::static_pointer_cast<NodeStrokePreview>(shared_from_this()));
|
|
m_tex_preview.destroy();
|
|
}
|
|
}
|