-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathISF4AE_CommandSelector.cpp
918 lines (684 loc) · 31.4 KB
/
ISF4AE_CommandSelector.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
#include "ISF4AE.h"
#include "AEFX_SuiteHelper.h"
#include "Smart_Utils.h"
#include "AEUtil.h"
#include "SystemUtil.h"
#include "Debug.h"
#include "MiscUtil.h"
#include <VVGL.hpp>
#include <iostream>
#include <mutex>
#define GL_SILENCE_DEPRECATION
#define MIN_SAFE_FLOAT -1000000
#define MAX_SAFE_FLOAT +1000000
#ifdef _WIN32
#include "Win/resource.h"
#else
#define IDR_DEFAULT_FS resourcePath + "shaders/Default ISF4AE Shader.fs"
#define IDR_AE2GL_FS resourcePath + "shaders/ae2gl.fs"
#define IDR_GL2AE_FS resourcePath + "shaders/gl2ae.fs"
#endif
#include <cassert>
/**
* Display a dialog describing the plug-in. Populate out_data>return_msg and After Effects will display it in a simple
* modal dialog.
*/
static PF_Err About(PF_InData* in_data, PF_OutData* out_data, PF_ParamDef* params[], PF_LayerDef* output) {
AEGP_SuiteHandler suites(in_data->pica_basicP);
auto* isf = reinterpret_cast<ParamArbIsf*>(*params[Param_ISF]->u.arb_d.value);
auto desc = isf->desc;
auto& doc = *desc->scene->doc();
stringstream ss;
ss << "ISF Information" << endl;
ss << "Name: " << isf->name << endl;
ss << "Description: " << doc.description() << endl;
ss << "Credit: " << doc.credit() << endl;
ss << endl;
ss << "----------------" << endl;
ss << endl;
ss << "This effect is built with ISF4AE; Interactive Shader Format for After Effects ";
ss << "(v" << MAJOR_VERSION << "." << MINOR_VERSION << "." << BUG_VERSION << ")." << endl;
ss << "All of the source code is published at https://github.com/baku89/ISF4AE." << endl;
PF_STRCPY(out_data->return_msg, ss.str().c_str());
return PF_Err_NONE;
}
/**
* Pops up shader errors on user clicked Option label of the effect
*/
static PF_Err PopDialog(PF_InData* in_data, PF_OutData* out_data, PF_ParamDef* params[], PF_LayerDef* output) {
PF_Err err = PF_Err_NONE;
AEGP_SuiteHandler suites(in_data->pica_basicP);
auto globalData = reinterpret_cast<GlobalData*>(suites.HandleSuite1()->host_lock_handle(in_data->global_data));
auto seqData = reinterpret_cast<SequenceData*>(suites.HandleSuite1()->host_lock_handle(in_data->sequence_data));
seqData->showISFOption = !seqData->showISFOption;
ERR(AEUtil::setParamVisibility(globalData->aegpId, in_data, params, Param_ISF, seqData->showISFOption));
suites.HandleSuite1()->host_unlock_handle(in_data->global_data);
suites.HandleSuite1()->host_unlock_handle(in_data->sequence_data);
return err;
}
/**
* Set any required flags and PF_OutData fields (including out_data>my_version) to describe your plug-in’s behavior.
*/
static PF_Err GlobalSetup(PF_InData* in_data, PF_OutData* out_data, PF_ParamDef* params[], PF_LayerDef* output) {
PF_Err err = PF_Err_NONE;
AEGP_SuiteHandler suites(in_data->pica_basicP);
out_data->my_version = PF_VERSION(MAJOR_VERSION, MINOR_VERSION, BUG_VERSION, STAGE_VERSION, BUILD_VERSION);
out_data->out_flags =
PF_OutFlag_DEEP_COLOR_AWARE | PF_OutFlag_CUSTOM_UI | PF_OutFlag_I_DO_DIALOG | PF_OutFlag_NON_PARAM_VARY | PF_OutFlag_SEND_UPDATE_PARAMS_UI | PF_OutFlag_CUSTOM_UI;
out_data->out_flags2 = PF_OutFlag2_FLOAT_COLOR_AWARE | PF_OutFlag2_SUPPORTS_SMART_RENDER | PF_OutFlag2_SUPPORTS_QUERY_DYNAMIC_FLAGS | PF_OutFlag2_SUPPORTS_THREADED_RENDERING;
// Initialize globalData
PF_Handle globalDataH = suites.HandleSuite1()->host_new_handle(sizeof(GlobalData));
if (!globalDataH) {
return PF_Err_OUT_OF_MEMORY;
}
out_data->global_data = globalDataH;
GlobalData* globalData = reinterpret_cast<GlobalData*>(suites.HandleSuite1()->host_lock_handle(globalDataH));
AEFX_CLR_STRUCT(*globalData);
// Register with AEGP
if (in_data->appl_id != 'PrMr') {
ERR(suites.UtilitySuite3()->AEGP_RegisterWithAEGP(NULL, CONFIG_NAME, &globalData->aegpId));
}
// Initialize global OpenGL context
#ifdef _WIN32
GLContext::bootstrapGLEnvironmentIfNecessary();
// VVGL on Windows Doesn't have pixel format functions
globalData->context = VVGL::CreateNewGLContextRef(nullptr, nullptr);
#else
globalData->context = VVGL::CreateNewGLContextRef(NULL, CreateCompatibilityGLPixelFormat());
#endif
VVGL::CreateGlobalBufferPool(globalData->context->newContextSharingMe());
globalData->downloader = VVGL::CreateGLTexToCPUCopierRefUsing(globalData->context->newContextSharingMe());
globalData->uploader = VVGL::CreateGLCPUToTexCopierRefUsing(globalData->context->newContextSharingMe());
FX_LOG("OpenGL Version: " << glGetString(GL_VERSION));
FX_LOG("OpenGL Vendor: " << glGetString(GL_VENDOR));
FX_LOG("OpenGL Renderer: " << glGetString(GL_RENDERER));
FX_LOG("OpenGL GLSL Versions: " << glGetString(GL_SHADING_LANGUAGE_VERSION));
// Setup GL objects
string resourcePath = AEUtil::getResourcesPath(in_data);
globalData->defaultScene = VVISF::CreateISF4AESceneRefUsing(globalData->context->newContextSharingMe());
globalData->defaultScene->useCode(SystemUtil::readResourceShader(IDR_DEFAULT_FS), "");
globalData->ae2glScene = VVISF::CreateISF4AESceneRefUsing(globalData->context->newContextSharingMe());
globalData->ae2glScene->useCode(SystemUtil::readResourceShader(IDR_AE2GL_FS), "");
globalData->gl2aeScene = VVISF::CreateISF4AESceneRefUsing(globalData->context->newContextSharingMe());
globalData->gl2aeScene->useCode(SystemUtil::readResourceShader(IDR_GL2AE_FS), "");
// Without this USELESS variable I'm getting a glitch, where the scene
// doesn't work without any errors
// FIXME: Dig into it to figure it out the reason
ISF4AESceneRef useless = VVISF::CreateISF4AESceneRefUsing(globalData->context->newContextSharingMe());
globalData->scenes = make_shared<WeakMap<string, SceneDesc>>();
#ifndef _WIN32
globalData->lock = [[NSLock alloc] init];
#endif
auto notLoadedSceneDesc = make_shared<SceneDesc>();
notLoadedSceneDesc->status = "Not Loaded";
notLoadedSceneDesc->scene = globalData->defaultScene;
globalData->notLoadedSceneDesc = notLoadedSceneDesc;
suites.HandleSuite1()->host_unlock_handle(globalDataH);
return err;
}
/**
* Free all global data (only required if you allocated some).
*/
static PF_Err GlobalSetdown(PF_InData* in_data, PF_OutData* out_data, PF_ParamDef* params[], PF_LayerDef* output) {
PF_Err err = PF_Err_NONE;
AEGP_SuiteHandler suites(in_data->pica_basicP);
if (in_data->global_data) {
// Dispose globalData
// TODO: Find a way not to call destructors explicitly
auto* globalData = reinterpret_cast<GlobalData*>(suites.HandleSuite1()->host_lock_handle(in_data->global_data));
globalData->context = nullptr;
globalData->uploader = nullptr;
globalData->downloader = nullptr;
globalData->defaultScene = nullptr;
globalData->gl2aeScene = nullptr;
globalData->ae2glScene = nullptr;
globalData->notLoadedSceneDesc = nullptr;
globalData->scenes = nullptr;
#ifndef _WIN32
globalData->lock = nil;
#endif // !_WIN32
suites.HandleSuite1()->host_unlock_handle(in_data->global_data);
suites.HandleSuite1()->host_dispose_handle(in_data->global_data);
}
return err;
}
static PF_Err SequenceSetup(PF_InData* in_data, PF_OutData* out_data, PF_ParamDef* params[], PF_LayerDef* output) {
PF_Err err = PF_Err_NONE;
AEGP_SuiteHandler suites(in_data->pica_basicP);
PF_Handle seqH = suites.HandleSuite1()->host_new_handle(sizeof(SequenceData));
if (!seqH) {
return PF_Err_OUT_OF_MEMORY;
}
auto* seq = reinterpret_cast<SequenceData*>(suites.HandleSuite1()->host_lock_handle(seqH));
seq->showISFOption = true;
out_data->sequence_data = seqH;
suites.HandleSuite1()->host_unlock_handle(seqH);
return err;
}
static PF_Err SequenceFlatten(PF_InData* in_data, PF_OutData* out_data, PF_ParamDef* params[], PF_LayerDef* output) {
PF_Err err = PF_Err_NONE;
AEGP_SuiteHandler suites(in_data->pica_basicP);
auto unflatSeq = reinterpret_cast<SequenceData*>(suites.HandleSuite1()->host_lock_handle(in_data->sequence_data));
PF_Handle flatSeqH = suites.HandleSuite1()->host_new_handle(sizeof(SequenceData));
if (!flatSeqH) {
return PF_Err_OUT_OF_MEMORY;
}
auto flatSeq = reinterpret_cast<SequenceData*>(suites.HandleSuite1()->host_lock_handle(flatSeqH));
flatSeq->showISFOption = unflatSeq->showISFOption;
suites.HandleSuite1()->host_unlock_handle(flatSeqH);
suites.HandleSuite1()->host_unlock_handle(in_data->sequence_data);
out_data->sequence_data = flatSeqH;
return err;
}
static PF_Err SequenceRestart(PF_InData* in_data, PF_OutData* out_data, PF_ParamDef* params[], PF_LayerDef* output) {
PF_Err err = PF_Err_NONE;
AEGP_SuiteHandler suites(in_data->pica_basicP);
auto flatSeq = reinterpret_cast<SequenceData*>(suites.HandleSuite1()->host_lock_handle(in_data->sequence_data));
PF_Handle unflatSeqH = suites.HandleSuite1()->host_new_handle(sizeof(SequenceData));
if (!unflatSeqH) {
return PF_Err_OUT_OF_MEMORY;
}
auto unflatSeq = reinterpret_cast<SequenceData*>(suites.HandleSuite1()->host_lock_handle(unflatSeqH));
unflatSeq->showISFOption = flatSeq->showISFOption;
suites.HandleSuite1()->host_unlock_handle(unflatSeqH);
suites.HandleSuite1()->host_unlock_handle(in_data->sequence_data);
out_data->sequence_data = unflatSeqH;
return err;
}
static PF_Err SequenceSetdown(PF_InData* in_data, PF_OutData* out_data, PF_ParamDef* params[], PF_LayerDef* output) {
PF_Err err = PF_Err_NONE;
AEGP_SuiteHandler suites(in_data->pica_basicP);
suites.HandleSuite1()->host_dispose_handle(in_data->sequence_data);
return err;
}
/**
* Describe your parameters and register them using PF_ADD_PARAM.
*/
static PF_Err ParamsSetup(PF_InData* in_data, PF_OutData* out_data, PF_ParamDef* params[], PF_LayerDef* output) {
PF_Err err = PF_Err_NONE;
AEGP_SuiteHandler suites(in_data->pica_basicP);
// Customize the name of the options button
// Premiere Pro/Elements does not support this suite
if (in_data->appl_id != 'PrMr') {
auto effectUISuite = AEFX_SuiteScoper<PF_EffectUISuite1>(in_data, kPFEffectUISuite, kPFEffectUISuiteVersion1, out_data);
ERR(effectUISuite->PF_SetOptionsButtonName(in_data->effect_ref, "ISF Option.."));
}
PF_ParamDef def;
// Add parameters
// An arbitrary param for storing fragment strings
AEFX_CLR_STRUCT(def);
ERR(CreateDefaultArb(in_data, out_data, &def.u.arb_d.dephault));
PF_ADD_ARBITRARY2("ISF", // name
1, BUTTON_HEIGHT, // width, height
PF_ParamFlag_CANNOT_TIME_VARY | PF_ParamFlag_SUPERVISE, // Param flags
PF_PUI_CONTROL | PF_PUI_DONT_ERASE_CONTROL, // ECU flags
def.u.arb_d.dephault, ParamID::ISF, ARB_REFCON);
// Parameters for Time-dependent filter
AEFX_CLR_STRUCT(def);
PF_ADD_CHECKBOX("Use Layer Time", // Label
"", // A description right to the checkbox
FALSE, // Default
PF_ParamFlag_SUPERVISE | PF_ParamFlag_CANNOT_TIME_VARY, ParamID::UseLayerTime);
AEFX_CLR_STRUCT(def);
PF_ADD_FLOAT_SLIDERX("Time", // Label
MIN_SAFE_FLOAT, MAX_SAFE_FLOAT, // Valid range
0, 10, // Slider range
0, // Default
1, // Precision
PF_ValueDisplayFlag_NONE, // Display
PF_ParamFlag_COLLAPSE_TWIRLY, // Flags
ParamID::Time); // ID
// Add all possible user params
A_char name[32];
for (int userParamIndex = 0; userParamIndex < NumUserParams; userParamIndex++) {
PF_SPRINTF(name, "Bool %d", userParamIndex);
AEFX_CLR_STRUCT(def);
PF_ADD_CHECKBOX(name, "", FALSE, PF_ParamFlag_COLLAPSE_TWIRLY, getIdForUserParam(userParamIndex, UserParamType_Bool));
PF_SPRINTF(name, "Long %d", userParamIndex);
AEFX_CLR_STRUCT(def);
PF_ADD_POPUPX(name, 5, 1, "1|2|3|4|5", PF_ParamFlag_COLLAPSE_TWIRLY, getIdForUserParam(userParamIndex, UserParamType_Long));
PF_SPRINTF(name, "Float %d", userParamIndex);
AEFX_CLR_STRUCT(def);
PF_ADD_FLOAT_SLIDERX(name, // Temp label
MIN_SAFE_FLOAT, MAX_SAFE_FLOAT, // Valid range
0, 1, // Slider range
0, // Default
1, // Precision
PF_ValueDisplayFlag_NONE, PF_ParamFlag_COLLAPSE_TWIRLY, getIdForUserParam(userParamIndex, UserParamType_Float));
PF_SPRINTF(name, "Angle %d", userParamIndex);
AEFX_CLR_STRUCT(def);
def.u.ad.valid_min = PF_Fixed_MINVAL;
def.u.ad.valid_max = PF_Fixed_MAXVAL;
def.flags |= PF_ParamFlag_COLLAPSE_TWIRLY;
PF_ADD_ANGLE(name, 0, getIdForUserParam(userParamIndex, UserParamType_Angle));
PF_SPRINTF(name, "Point2D %d", userParamIndex);
AEFX_CLR_STRUCT(def);
def.flags |= PF_ParamFlag_COLLAPSE_TWIRLY;
PF_ADD_POINT(name,
// at a center of layer, with specifying in percentage
50L, 50L,
// restrict_bounds
0, getIdForUserParam(userParamIndex, UserParamType_Point2D));
PF_SPRINTF(name, "Color %d", userParamIndex);
AEFX_CLR_STRUCT(def);
def.flags |= PF_ParamFlag_COLLAPSE_TWIRLY;
PF_ADD_COLOR(name, 1, 1, 1, getIdForUserParam(userParamIndex, UserParamType_Color));
PF_SPRINTF(name, "Image %d", userParamIndex);
AEFX_CLR_STRUCT(def);
PF_ADD_LAYER(name, PF_LayerDefault_NONE, getIdForUserParam(userParamIndex, UserParamType_Image));
}
if (!err) {
// Referencing Examples/UI/CCU
PF_CustomUIInfo ci;
AEFX_CLR_STRUCT(ci);
ci.events = PF_CustomEFlag_LAYER | PF_CustomEFlag_COMP | PF_CustomEFlag_EFFECT;
ci.comp_ui_width = ci.comp_ui_height = 0;
ci.layer_ui_width = ci.layer_ui_height = 0;
ci.preview_ui_width = ci.preview_ui_height = 0;
ci.comp_ui_alignment = PF_UIAlignment_NONE;
ci.layer_ui_alignment = PF_UIAlignment_NONE;
ci.preview_ui_alignment = PF_UIAlignment_NONE;
err = (*(in_data->inter.register_ui))(in_data->effect_ref, &ci);
}
// Set PF_OutData->num_params to match the parameter count.
out_data->num_params = NumParams;
return err;
}
static PF_Err SmartPreRender(PF_InData* in_data, PF_OutData* out_data, PF_PreRenderExtra* extra) {
PF_Err err = PF_Err_NONE, err2 = PF_Err_NONE;
AEGP_SuiteHandler suites(in_data->pica_basicP);
auto* globalData = reinterpret_cast<GlobalData*>(suites.HandleSuite1()->host_lock_handle(in_data->global_data));
#ifndef _WIN32
[globalData->lock lock];
#endif // !_WIN32
// Create preRenderData
PF_Handle preRenderDataH = suites.HandleSuite1()->host_new_handle(sizeof(PreRenderData));
if (!preRenderDataH) {
return PF_Err_OUT_OF_MEMORY;
}
// Set handler
extra->output->pre_render_data = preRenderDataH;
auto* preRenderData = reinterpret_cast<PreRenderData*>(suites.HandleSuite1()->host_lock_handle(preRenderDataH));
if (!preRenderData) {
return PF_Err_OUT_OF_MEMORY;
}
PF_ParamDef paramDef;
// Get the ISF scene from cache and save its pointer to PreRenderData
{
AEFX_CLR_STRUCT(paramDef);
ERR(PF_CHECKOUT_PARAM(in_data, Param_ISF, in_data->current_time, in_data->time_step, in_data->time_scale, ¶mDef));
auto* isf = reinterpret_cast<ParamArbIsf*>(*paramDef.u.arb_d.value);
if (isf) {
auto desc = isf->desc;
preRenderData->scene = desc->scene.get();
}
ERR2(PF_CHECKIN_PARAM(in_data, ¶mDef));
}
// Checkout all image parameters
PF_CheckoutResult inResult;
int userParamIndex = 0;
PF_RenderRequest req = extra->input->output_request;
req.rect.left = 0;
req.rect.top = 0;
req.rect.right = 10000;
req.rect.bottom = 10000;
req.preserve_rgb_of_zero_alpha = true;
for (auto input : preRenderData->scene->inputs()) {
UserParamType userParamType = getUserParamTypeForISFAttr(input);
if (userParamType == UserParamType_Image) {
PF_ParamIndex paramIndex = input->isFilterInputImage() ? Param_Input : getIndexForUserParam(userParamIndex, UserParamType_Image);
(extra->cb->checkout_layer(in_data->effect_ref,
// A parameter index of layer to checkout
paramIndex,
// Unique index for retriving pixels in Cmd_SmartRender
// -- just use paramIndex itself.
paramIndex, &req, in_data->current_time, in_data->time_step, in_data->time_scale, &inResult));
if (!input->isFilterInputImage()) {
VVGL::Size& size = preRenderData->inputImageSizes[userParamIndex];
size.width = inResult.ref_width * in_data->downsample_x.num / in_data->downsample_x.den;
size.height = inResult.ref_height * in_data->downsample_y.num / in_data->downsample_y.den;
}
}
if (isISFAttrVisibleInECW(input)) {
userParamIndex++;
}
}
// Compute the rect to render
if (!err) {
// Set the output region to an entire layer multiplied by downsample, regardless of input's mask.
PF_Rect outputRect = {0, 0, (A_long)ceil((double)in_data->width * in_data->downsample_x.num / in_data->downsample_x.den),
(A_long)ceil((double)in_data->height * in_data->downsample_y.num / in_data->downsample_y.den)};
UnionLRect(&outputRect, &extra->output->result_rect);
UnionLRect(&outputRect, &extra->output->max_result_rect);
preRenderData->outSize = VVGL::Size(outputRect.right, outputRect.bottom);
extra->output->flags |= PF_RenderOutputFlag_RETURNS_EXTRA_PIXELS;
}
suites.HandleSuite1()->host_unlock_handle(preRenderDataH);
#ifndef _WIN32
[globalData->lock unlock];
#endif // !_WIN32
return err;
}
static PF_Err SmartRender(PF_InData* in_data, PF_OutData* out_data, PF_SmartRenderExtra* extra) {
PF_Err err = PF_Err_NONE;
AEGP_SuiteHandler suites(in_data->pica_basicP);
auto* globalData = reinterpret_cast<GlobalData*>(suites.HandleSuite1()->host_lock_handle(in_data->global_data));
#ifndef _WIN32
[globalData->lock lock];
#endif // !_WIN32
globalData->context->makeCurrentIfNotCurrent();
auto preRenderDataH = reinterpret_cast<PF_Handle>(extra->input->pre_render_data);
auto* preRenderData = reinterpret_cast<PreRenderData*>(suites.HandleSuite1()->host_lock_handle(preRenderDataH));
auto bitdepth = extra->input->bitdepth;
auto pixelBytes = bitdepth * 4 / 8;
auto& scene = preRenderData->scene;
// It has to be done by callee to bind all of layer inputs, before calling renderISFToCPUBuffer
int userParamIndex = 0;
for (auto& input : preRenderData->scene->inputs()) {
if (input->type() == VVISF::ISFValType_Image) {
PF_ParamIndex checkoutIndex;
VVGL::Size layerSize;
if (input->isFilterInputImage()) {
checkoutIndex = Param_Input;
layerSize = preRenderData->outSize;
} else {
checkoutIndex = getIndexForUserParam(userParamIndex, UserParamType_Image);
layerSize = preRenderData->inputImageSizes[userParamIndex];
}
VVGL::GLBufferRef image;
ERR(uploadCPUBufferInSmartRender(globalData, in_data->effect_ref, extra, checkoutIndex, layerSize, image));
input->setCurrentImageBuffer(image);
}
if (isISFAttrVisibleInECW(input)) {
userParamIndex++;
}
}
// Bind special uniforms reserved for ISF4AE
VVISF::ISFVal i4aDownsample =
VVISF::ISFVal(VVISF::ISFValType_Point2D, (float)in_data->downsample_x.num / in_data->downsample_x.den, (float)in_data->downsample_y.num / in_data->downsample_y.den);
scene->setValueForInputNamed(i4aDownsample, "i4a_Downsample");
scene->setValueForInputNamed(VVISF::ISFVal(VVISF::ISFValType_Bool, false), "i4a_CustomUI");
// Render
VVGL::GLBufferRef outputImageCPU = nullptr;
VVGL::Size pointScale = {1.0, 1.0};
renderISFToCPUBuffer(in_data, out_data, *scene, bitdepth, preRenderData->outSize, pointScale, &outputImageCPU);
// Check-in output pixels
PF_EffectWorld* outputWorld = nullptr;
ERR(extra->cb->checkout_output(in_data->effect_ref, &outputWorld));
if (outputWorld) {
// Download
char* glP = nullptr; // Pointer offset for OpenGL buffer
char* aeP = nullptr; // for AE's layerDef
auto bytesPerRowGl = outputImageCPU->calculateBackingBytesPerRow();
if (!outputImageCPU->cpuBackingPtr) {
err = PF_Err_OUT_OF_MEMORY;
// A more explicit assert that appears with the new versions
// of the Nvidia Studio driver described in this post:
// [link](https://github.com/baku89/ISF4AE/issues/19#issuecomment-1724631129)
assert("FATAL! CPU backing pointer is NULL! Cannot copy CPU buffer to EffectWorld!" && false);
}
// Copy per row
for (size_t y = 0; y < preRenderData->outSize.height; y++) {
glP = (char*)outputImageCPU->cpuBackingPtr + y * bytesPerRowGl;
aeP = (char*)outputWorld->data + y * outputWorld->rowbytes;
memcpy(aeP, glP, preRenderData->outSize.width * pixelBytes);
}
} else {
FX_LOG("Cannot checkout outputWorld");
}
suites.HandleSuite1()->host_unlock_handle(preRenderDataH);
suites.HandleSuite1()->host_dispose_handle(preRenderDataH);
suites.HandleSuite1()->host_unlock_handle(in_data->global_data);
#ifndef _WIN32
[globalData->lock unlock];
#endif // !_WIN32
return err;
}
static PF_Err UpdateParamsUI(PF_InData* in_data, PF_OutData* out_data, PF_ParamDef* params[], PF_LayerDef* output) {
PF_Err err = PF_Err_NONE;
AEGP_SuiteHandler suites(in_data->pica_basicP);
auto* globalData = reinterpret_cast<GlobalData*>(suites.HandleSuite1()->host_lock_handle(in_data->global_data));
auto* seqData = reinterpret_cast<SequenceData*>(suites.HandleSuite1()->host_lock_handle(in_data->sequence_data));
auto* isf = reinterpret_cast<ParamArbIsf*>(*params[Param_ISF]->u.arb_d.value);
auto desc = isf->desc;
bool isTransition = desc->scene->doc()->type() == VVISF::ISFFileType_Transition;
// Toggle the visibility of ISF options
ERR(AEUtil::setParamVisibility(globalData->aegpId, in_data, params, Param_ISF, seqData->showISFOption));
// Set the shader status as a label for 'Edit Shader'
string statusLabel = "ISF: " + desc->status;
AEUtil::setParamName(globalData->aegpId, in_data, params, Param_ISF, statusLabel);
// Show the time parameters if the current shader is time dependant
bool isTimeDependant = desc->scene->isTimeDependant();
ERR(AEUtil::setParamVisibility(globalData->aegpId, in_data, params, Param_UseLayerTime, isTimeDependant));
// Toggle the visibility of 'Time' parameter depending on 'Use Layer Time'
A_Boolean useLayerTime = params[Param_UseLayerTime]->u.bd.value;
ERR(AEUtil::setParamVisibility(globalData->aegpId, in_data, params, Param_Time, !useLayerTime && isTimeDependant));
// Change the visiblity of user params
PF_ParamIndex userParamIndex = 0;
UserParamType userParamType;
auto inputs = desc->scene->inputs();
for (auto& input : inputs) {
if (userParamIndex >= inputs.size()) {
break;
}
if (!isISFAttrVisibleInECW(input)) {
continue;
}
userParamType = getUserParamTypeForISFAttr(input);
auto index = getIndexForUserParam(userParamIndex, userParamType);
// Update the value and other UI options
auto& param = *params[index];
switch (userParamType) {
case UserParamType_Bool: {
param.u.bd.dephault = input->defaultVal().getBoolVal();
break;
}
case UserParamType_Long: {
auto labels = input->labelArray();
auto values = input->valArray();
param.u.pd.num_choices = labels.size();
auto joinedLabels = joinWith(labels, "|");
A_char* names = new A_char[joinedLabels.length() + 1];
PF_STRCPY(names, joinedLabels.c_str());
param.u.pd.u.namesptr = names;
auto dephaultVal = input->defaultVal().getLongVal();
A_long dephaultIndex = mmax(1, findIndex(values, dephaultVal) + 1);
param.u.pd.dephault = dephaultIndex;
break;
}
case UserParamType_Float: {
double dephault, min, max;
bool clampMin, clampMax;
A_short precision;
if (input->type() == VVISF::ISFValType_Float) {
dephault = input->defaultVal().getDoubleVal();
min = input->minVal().getDoubleVal();
max = input->maxVal().getDoubleVal();
clampMin = input->clampMin();
clampMax = input->clampMax();
precision = 1;
} else {
// input->type() == VVISF::ISFValType_Long
dephault = (double)input->defaultVal().getLongVal();
min = input->minVal().getLongVal();
max = input->maxVal().getLongVal();
clampMin = true;
clampMax = true;
precision = 0;
}
PF_ValueDisplayFlags displayFlags = PF_ValueDisplayFlag_NONE;
if (input->unit() == VVISF::ISFValUnit_Length) {
dephault = 0;
min = 0;
max = 100;
} else if (input->unit() == VVISF::ISFValUnit_Percent) {
dephault *= 100;
min *= 100;
max *= 100;
displayFlags |= PF_ValueDisplayFlag_PERCENT;
}
if (isTransition && input->name() == "progress") {
dephault = 0;
min = 0;
max = 100;
clampMin = true;
clampMax = true;
displayFlags |= PF_ValueDisplayFlag_PERCENT;
}
param.u.fs_d.dephault = dephault;
param.u.fs_d.slider_min = min;
param.u.fs_d.slider_max = max;
param.u.fs_d.valid_min = clampMin ? min : MIN_SAFE_FLOAT;
param.u.fs_d.valid_max = clampMax ? max : MAX_SAFE_FLOAT;
param.u.fs_d.precision = precision;
param.u.fs_d.display_flags = displayFlags;
break;
}
case UserParamType_Angle:
param.u.ad.dephault = getDefaultForAngleInput(input);
break;
case UserParamType_Point2D: {
auto x = input->defaultVal().getPointValByIndex(0);
auto y = input->defaultVal().getPointValByIndex(1);
param.u.td.x_dephault = FLOAT2FIX(x * in_data->width);
param.u.td.y_dephault = FLOAT2FIX((1.0 - y) * in_data->height);
break;
}
case UserParamType_Color: {
auto dephault = input->defaultVal();
param.u.cd.dephault.red = dephault.getColorValByChannel(0) * 255;
param.u.cd.dephault.green = dephault.getColorValByChannel(1) * 255;
param.u.cd.dephault.blue = dephault.getColorValByChannel(2) * 255;
param.u.cd.dephault.alpha = dephault.getColorValByChannel(3) * 255;
break;
}
case UserParamType_Image: {
if (isTransition && input->name() == "startImage") {
param.u.ld.dephault = PF_LayerDefault_MYSELF;
} else {
param.u.ld.dephault = PF_LayerDefault_NONE;
}
break;
}
default:
break;
}
param.uu.change_flags |= PF_ChangeFlag_CHANGED_VALUE;
// Set the label and visibility
for (int type = 0; type < NumUserParamType; type++) {
auto index = getIndexForUserParam(userParamIndex, (UserParamType)type);
bool visible = type == userParamType;
ERR(AEUtil::setParamVisibility(globalData->aegpId, in_data, params, index, visible));
if (visible) {
// Set label
auto label = input->label();
if (label.empty() && isTransition) {
// Set a default label for transition-type ISF.
auto name = input->name();
if (name == "startImage") {
label = "Start Image";
} else if (name == "endImage") {
label = "End Image";
} else if (name == "progress") {
label = "Progress";
}
}
if (label.empty()) {
label = input->name();
}
if (seqData->showISFOption) {
label += " (" + to_string(index) + ")";
}
ERR(AEUtil::setParamName(globalData->aegpId, in_data, params, index, label));
}
}
userParamIndex++;
}
// Hide all out-of-range parameters
for (; userParamIndex < NumUserParams; userParamIndex++) {
for (int userParamType = 0; userParamType < NumUserParamType; userParamType++) {
auto index = getIndexForUserParam(userParamIndex, (UserParamType)userParamType);
ERR(AEUtil::setParamVisibility(globalData->aegpId, in_data, params, index, false));
}
}
suites.HandleSuite1()->host_unlock_handle(in_data->global_data);
suites.HandleSuite1()->host_unlock_handle(in_data->sequence_data);
return err;
}
static PF_Err QueryDynamicFlags(PF_InData* in_data, PF_OutData* out_data, PF_ParamDef* params[], void* extra) {
PF_Err err = PF_Err_NONE, err2 = PF_Err_NONE;
PF_ParamDef def;
AEFX_CLR_STRUCT(def);
// The parameter array passed with PF_Cmd_QUERY_DYNAMIC_FLAGS
// contains invalid values; use PF_CHECKOUT_PARAM() to obtain
// valid values.
ERR(PF_CHECKOUT_PARAM(in_data, Param_UseLayerTime, in_data->current_time, in_data->time_step, in_data->time_scale, &def));
if (!err) {
auto useLayerTime = def.u.bd.value;
setBitFlag(PF_OutFlag_NON_PARAM_VARY, useLayerTime, &out_data->out_flags);
}
ERR2(PF_CHECKIN_PARAM(in_data, &def));
return err;
}
extern "C" DllExport PF_Err
PluginDataEntryFunction(PF_PluginDataPtr inPtr, PF_PluginDataCB inPluginDataCallBackPtr, SPBasicSuite* inSPBasicSuitePtr, const char* inHostName, const char* inHostVersion) {
PF_Err result = PF_Err_INVALID_CALLBACK;
result = PF_REGISTER_EFFECT(inPtr, inPluginDataCallBackPtr, CONFIG_NAME, CONFIG_MATCH_NAME, CONFIG_CATEGORY,
AE_RESERVED_INFO); // Reserved Info
return result;
}
PF_Err EffectMain(PF_Cmd cmd, PF_InData* in_data, PF_OutData* out_data, PF_ParamDef* params[], PF_LayerDef* output, void* extra) {
PF_Err err = PF_Err_NONE;
try {
switch (cmd) {
case PF_Cmd_ABOUT:
err = About(in_data, out_data, params, output);
break;
case PF_Cmd_DO_DIALOG:
err = PopDialog(in_data, out_data, params, output);
break;
case PF_Cmd_GLOBAL_SETUP:
err = GlobalSetup(in_data, out_data, params, output);
break;
case PF_Cmd_PARAMS_SETUP:
err = ParamsSetup(in_data, out_data, params, output);
break;
case PF_Cmd_GLOBAL_SETDOWN:
err = GlobalSetdown(in_data, out_data, params, output);
break;
case PF_Cmd_ARBITRARY_CALLBACK:
err = HandleArbitrary(in_data, out_data, params, output, reinterpret_cast<PF_ArbParamsExtra*>(extra));
break;
case PF_Cmd_SEQUENCE_SETUP:
err = SequenceSetup(in_data, out_data, params, output);
break;
case PF_Cmd_SEQUENCE_FLATTEN:
err = SequenceFlatten(in_data, out_data, params, output);
break;
case PF_Cmd_SEQUENCE_RESETUP:
err = SequenceRestart(in_data, out_data, params, output);
break;
case PF_Cmd_SEQUENCE_SETDOWN:
err = SequenceSetdown(in_data, out_data, params, output);
break;
case PF_Cmd_SMART_PRE_RENDER:
err = SmartPreRender(in_data, out_data, reinterpret_cast<PF_PreRenderExtra*>(extra));
break;
case PF_Cmd_SMART_RENDER:
err = SmartRender(in_data, out_data, reinterpret_cast<PF_SmartRenderExtra*>(extra));
break;
case PF_Cmd_UPDATE_PARAMS_UI:
err = UpdateParamsUI(in_data, out_data, params, output);
break;
case PF_Cmd_QUERY_DYNAMIC_FLAGS:
err = QueryDynamicFlags(in_data, out_data, params, extra);
break;
case PF_Cmd_EVENT:
err = HandleEvent(in_data, out_data, params, output, reinterpret_cast<PF_EventExtra*>(extra));
break;
}
} catch (PF_Err& thrown_err) {
err = thrown_err;
}
return err;
}