Skip to content

Commit 8198193

Browse files
authored
refactor: port layer to Cobra renderer: Noise gradient (synfig#3306)
* refactor: port layer to Cobra renderer: Noise gradient * fix: compute pixel size in Cobra engine for Noise Gradient layer * style: * fix: supersample size
1 parent e3ca24d commit 8198193

File tree

2 files changed

+161
-63
lines changed

2 files changed

+161
-63
lines changed

synfig-core/src/modules/mod_noise/noise.cpp

Lines changed: 158 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@
4242
#include <synfig/time.h>
4343
#include <synfig/context.h>
4444
#include <synfig/paramdesc.h>
45-
#include <synfig/renddesc.h>
46-
#include <synfig/surface.h>
45+
#include <synfig/rendering/software/task/taskpaintpixelsw.h>
4746
#include <synfig/value.h>
4847
#include <ctime>
4948

@@ -59,10 +58,149 @@ SYNFIG_LAYER_INIT(Noise);
5958
SYNFIG_LAYER_SET_NAME(Noise,"noise");
6059
SYNFIG_LAYER_SET_LOCAL_NAME(Noise,N_("Noise Gradient"));
6160
SYNFIG_LAYER_SET_CATEGORY(Noise,N_("Gradients"));
62-
SYNFIG_LAYER_SET_VERSION(Noise,"0.0");
61+
SYNFIG_LAYER_SET_VERSION(Noise,"0.1");
6362

6463
/* === P R O C E D U R E S ================================================= */
6564

65+
class TaskNoise: public rendering::Task, public rendering::TaskInterfaceTransformation
66+
{
67+
public:
68+
typedef etl::handle<TaskNoise> Handle;
69+
SYNFIG_EXPORT static Token token;
70+
71+
Token::Handle get_token() const override { return token.handle(); }
72+
73+
Vector size;
74+
RandomNoise random;
75+
RandomNoise::SmoothType smooth;
76+
int detail;
77+
Real speed;
78+
bool turbulent;
79+
bool do_alpha;
80+
bool super_sample;
81+
CompiledGradient compiled_gradient;
82+
Time time_mark;
83+
84+
rendering::Transformation::Handle get_transformation() const override {
85+
return transformation.handle();
86+
}
87+
88+
private:
89+
rendering::Holder<rendering::TransformationAffine> transformation;
90+
};
91+
92+
93+
class TaskNoiseSW: public TaskNoise, public rendering::TaskPaintPixelSW
94+
{
95+
public:
96+
typedef etl::handle<TaskNoise> Handle;
97+
SYNFIG_EXPORT static Token token;
98+
Token::Handle get_token() const override { return token.handle(); }
99+
100+
void pre_run(const Matrix3& /*matrix*/, const Matrix3& /*inverse_matrix*/) const override
101+
{
102+
pixel_size = (std::fabs(get_units_per_pixel()[0]) + std::fabs(get_units_per_pixel()[1])) * 0.5f;
103+
}
104+
105+
Color get_color(const Vector& point) const override
106+
{
107+
float x(point[0] / size[0] * (1 << detail));
108+
float y(point[1] / size[1] * (1 << detail));
109+
float x2(0), y2(0);
110+
111+
if (super_sample && pixel_size) {
112+
x2 = (point[0] + pixel_size) / size[0] * (1 << detail);
113+
y2 = (point[1] + pixel_size) / size[1] * (1 << detail);
114+
}
115+
116+
const RandomNoise::SmoothType smooth_type((!speed && smooth == RandomNoise::SMOOTH_SPLINE)
117+
? RandomNoise::SMOOTH_FAST_SPLINE
118+
: smooth);
119+
120+
const float ftime(speed * time_mark);
121+
122+
float amount = 0.0f;
123+
float amount2 = 0.0f;
124+
float amount3 = 0.0f;
125+
float alpha = 0.0f;
126+
for (int i = 0; i < detail; i++) {
127+
amount = random(smooth_type, 0 + (detail - i) * 5, x, y, ftime) + amount * 0.5;
128+
amount = synfig::clamp(amount, -1.f, 1.f);
129+
130+
if (super_sample && pixel_size) {
131+
amount2 = random(smooth_type, 0 + (detail - i) * 5, x2, y, ftime) + amount2 * 0.5;
132+
amount2 = synfig::clamp(amount2, -1.f, 1.f);
133+
134+
amount3 = random(smooth_type, 0 + (detail - i) * 5, x, y2, ftime) + amount3 * 0.5;
135+
amount3 = synfig::clamp(amount3, -1.f, 1.f);
136+
137+
if (turbulent) {
138+
amount2 = std::fabs(amount2);
139+
amount3 = std::fabs(amount3);
140+
}
141+
142+
x2 *= 0.5f;
143+
y2 *= 0.5f;
144+
}
145+
146+
if (do_alpha) {
147+
alpha = random(smooth_type, 3 + (detail - i) * 5, x, y, ftime) + alpha * 0.5;
148+
alpha = synfig::clamp(alpha, -1.f, 1.f);
149+
}
150+
151+
if (turbulent) {
152+
amount = std::fabs(amount);
153+
alpha = std::fabs(alpha);
154+
}
155+
156+
x *= 0.5f;
157+
y *= 0.5f;
158+
}
159+
160+
if (!turbulent) {
161+
amount = amount / 2.0f + 0.5f;
162+
alpha = alpha / 2.0f + 0.5f;
163+
164+
if (super_sample && pixel_size) {
165+
amount2 = amount2 / 2.0f + 0.5f;
166+
amount3 = amount3 / 2.0f + 0.5f;
167+
}
168+
}
169+
170+
Color color;
171+
172+
if (super_sample && pixel_size) {
173+
Real da = std::max(amount3, std::max(amount, amount2)) - std::min(amount3, std::min(amount, amount2));
174+
if (da < 0)
175+
da = -da;
176+
if (approximate_greater(da, 2.))
177+
da = 2.;
178+
da *= 0.5;
179+
color = compiled_gradient.average(amount - da, amount + da);
180+
} else {
181+
color = compiled_gradient.color(amount);
182+
}
183+
184+
if (do_alpha)
185+
color.set_a(color.get_a() * alpha);
186+
187+
return color;
188+
}
189+
190+
bool run(RunParams&) const override {
191+
return run_task();
192+
}
193+
194+
protected:
195+
mutable float pixel_size = 0.0f;
196+
};
197+
198+
SYNFIG_EXPORT rendering::Task::Token TaskNoise::token(
199+
DescAbstract<TaskNoise>("Noise") );
200+
SYNFIG_EXPORT rendering::Task::Token TaskNoiseSW::token(
201+
DescReal<TaskNoiseSW, TaskNoise>("NoiseSW") );
202+
203+
66204
/* === M E T H O D S ======================================================= */
67205

68206
Noise::Noise():
@@ -115,7 +253,6 @@ Noise::color_func(const Point &point, float pixel_size,Context /*context*/)const
115253
y2=(point[1]+pixel_size)/size[1]*(1<<detail);
116254
}
117255

118-
int i;
119256
Time time;
120257
time=speed*get_time_mark();
121258
int smooth((!speed && smooth_ == (int)RandomNoise::SMOOTH_SPLINE) ? (int)RandomNoise::SMOOTH_FAST_SPLINE : smooth_);
@@ -127,8 +264,7 @@ Noise::color_func(const Point &point, float pixel_size,Context /*context*/)const
127264
float amount2=0.0f;
128265
float amount3=0.0f;
129266
float alpha=0.0f;
130-
for(i=0;i<detail;i++)
131-
{
267+
for (int i = 0; i < detail; i++) {
132268
amount=random(RandomNoise::SmoothType(smooth),0+(detail-i)*5,x,y,ftime)+amount*0.5;
133269
if (amount < -1) amount = -1;
134270
if (amount > 1) amount = 1;
@@ -196,11 +332,6 @@ Noise::color_func(const Point &point, float pixel_size,Context /*context*/)const
196332
return ret;
197333
}
198334

199-
inline float
200-
Noise::calc_supersample(const synfig::Point &/*x*/, float /*pw*/,float /*ph*/)const
201-
{
202-
return 0.0f;
203-
}
204335

205336
synfig::Layer::Handle
206337
Noise::hit_check(synfig::Context context, const synfig::Point &point)const
@@ -323,55 +454,21 @@ Noise::get_color(Context context, const Point &point)const
323454
return Color::blend(color,context.get_color(point),get_amount(),get_blend_method());
324455
}
325456

326-
327-
bool
328-
Noise::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
457+
rendering::Task::Handle
458+
Noise::build_composite_task_vfunc(ContextParams /*context_params*/) const
329459
{
330-
RENDER_TRANSFORMED_IF_NEED(__FILE__, __LINE__)
331-
332-
SuperCallback supercb(cb,0,9500,10000);
333-
334-
if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
335-
{
336-
surface->set_wh(renddesc.get_w(),renddesc.get_h());
337-
}
338-
else
339-
{
340-
if(!context.accelerated_render(surface,quality,renddesc,&supercb))
341-
return false;
342-
if(get_amount()==0)
343-
return true;
344-
}
345-
346-
347-
int x,y;
348-
349-
Surface::pen pen(surface->begin());
350-
const Real pw(renddesc.get_pw()),ph(renddesc.get_ph());
351-
Point pos;
352-
Point tl(renddesc.get_tl());
353-
const int w(surface->get_w());
354-
const int h(surface->get_h());
355-
float supersampleradius((std::fabs(pw)+std::fabs(ph))*0.5f);
356-
if(quality>=8)
357-
supersampleradius=0;
358-
359-
if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
360-
{
361-
for(y=0,pos[1]=tl[1];y<h;y++,pen.inc_y(),pen.dec_x(x),pos[1]+=ph)
362-
for(x=0,pos[0]=tl[0];x<w;x++,pen.inc_x(),pos[0]+=pw)
363-
pen.put_value(color_func(pos,supersampleradius,context));
364-
}
365-
else
366-
{
367-
for(y=0,pos[1]=tl[1];y<h;y++,pen.inc_y(),pen.dec_x(x),pos[1]+=ph)
368-
for(x=0,pos[0]=tl[0];x<w;x++,pen.inc_x(),pos[0]+=pw)
369-
pen.put_value(Color::blend(color_func(pos,supersampleradius,context),pen.get_value(),get_amount(),get_blend_method()));
370-
}
371-
372-
// Mark our progress as finished
373-
if(cb && !cb->amount_complete(10000,10000))
374-
return false;
375-
376-
return true;
460+
TaskNoise::Handle task(new TaskNoise());
461+
task->size = param_size.get(Vector());
462+
task->random.set_seed(param_random.get(int()));
463+
task->smooth = RandomNoise::SmoothType(param_smooth.get(int()));
464+
task->detail = param_detail.get(int());
465+
task->speed = param_speed.get(Real());
466+
task->turbulent = param_turbulent.get(bool());
467+
task->do_alpha = param_do_alpha.get(bool());
468+
task->super_sample = param_super_sample.get(bool());
469+
task->compiled_gradient = compiled_gradient;
470+
task->time_mark = get_time_mark();
471+
472+
return task;
377473
}
474+

synfig-core/src/modules/mod_noise/noise.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,17 +74,18 @@ class Noise : public synfig::Layer_Composite, public synfig::Layer_NoDeform
7474

7575
void compile();
7676
synfig::Color color_func(const synfig::Point &x, float supersample,synfig::Context context)const;
77-
float calc_supersample(const synfig::Point &x, float pw,float ph)const;
7877

7978
public:
8079
Noise();
8180

8281
virtual bool set_param(const synfig::String &param, const synfig::ValueBase &value);
8382
virtual synfig::ValueBase get_param(const synfig::String &param)const;
8483
virtual synfig::Color get_color(synfig::Context context, const synfig::Point &pos)const;
85-
virtual bool accelerated_render(synfig::Context context,synfig::Surface *surface,int quality, const synfig::RendDesc &renddesc, synfig::ProgressCallback *cb)const;
8684
synfig::Layer::Handle hit_check(synfig::Context context, const synfig::Point &point)const;
8785
virtual Vocab get_param_vocab()const;
86+
87+
protected:
88+
synfig::rendering::Task::Handle build_composite_task_vfunc(synfig::ContextParams /*context_params*/) const;
8889
};
8990

9091
/* === E N D =============================================================== */

0 commit comments

Comments
 (0)