aexlo 0.1.0

Run After Effects plugins without Adobe dependencies.
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
//! Rendering a frame: the legacy, smart and GPU command paths, plus the
//! frame size and time they render at.

use super::*;

/// The command path [`PluginInstance::render_with`] renders a frame through.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
#[non_exhaustive]
pub enum RenderPath {
	/// GPU when possible, else smart, else legacy, falling through on failure
	/// ([`PluginInstance::render_frame`]).
	#[default]
	Auto,
	/// `PF_Cmd_RENDER` only.
	Legacy,
	/// `PF_Cmd_SMART_PRE_RENDER` + `PF_Cmd_SMART_RENDER` only.
	Smart,
	/// `PF_Cmd_SMART_PRE_RENDER` + `PF_Cmd_SMART_RENDER_GPU` only, on a
	/// 32-bit float GPU world.
	Gpu,
}

impl PluginInstance {
	/// Send `PF_Cmd_DO_DIALOG`, the command After Effects uses when the user
	/// presses the effect's Options button. Only effects that set
	/// `PF_OutFlag_I_DO_DIALOG` have that button.
	///
	/// # Errors
	/// [`AexloError::PluginExecutionFailed`] if the plugin rejects the command.
	pub fn do_dialog(&mut self) -> Result<String> {
		self.call_plugin(RawCommand::DoDialog, null_mut())?;
		Ok(self.message())
	}

	/// Whether global setup set `PF_OutFlag_I_DO_DIALOG`, so the effect
	/// controls window shows an Options button.
	pub fn has_options_button(&self) -> bool {
		self.global_out_flags & after_effects_sys::PF_OutFlag_I_DO_DIALOG as PF_OutFlags != 0
	}

	pub(super) fn prepare_custom_control(&mut self, index: usize, width: i32, height: i32) {
		if self.custom_ui_ready.contains(&index) {
			return;
		}
		self.dispatch_effect_event(index, after_effects_sys::PF_Event_NEW_CONTEXT, width, height, None);
		self.dispatch_effect_event(index, after_effects_sys::PF_Event_ACTIVATE, width, height, None);
		self.custom_ui_ready.insert(index);
	}

	pub(super) fn dispatch_effect_event(
		&mut self,
		index: usize,
		event: after_effects_sys::PF_EventType,
		width: i32,
		height: i32,
		pointer: Option<ControlPointer>,
	) -> (i32, bool) {
		let mut kept = self.ui_contexts.remove(&index).unwrap_or_else(LiveUiContext::new);
		// SAFETY: `PF_EventExtra` is a plain C struct (integers, raw pointers, unions of those), for which all-zero bytes are a valid value.
		let mut extra: after_effects_sys::PF_EventExtra = unsafe { std::mem::zeroed() };
		extra.contextH = kept.handle();
		extra.e_type = event;
		extra.effect_win.index = index as after_effects_sys::PF_ParamIndex;
		extra.effect_win.area = after_effects_sys::PF_EA_CONTROL as after_effects_sys::PF_EffectArea;
		extra.effect_win.current_frame = after_effects_sys::PF_Rect {
			left: 0,
			top: 0,
			right: width,
			bottom: height,
		};
		let drawing = event == after_effects_sys::PF_Event_DRAW
			&& crate::suites::drawbot::begin_frame(width.max(1) as u32, height.max(1) as u32);
		if drawing && let Some(draw) = crate::suites::drawbot::current_draw_ref() {
			kept.set_draw_ref(draw);
		}
		if event == after_effects_sys::PF_Event_DRAW {
			extra.u.draw = after_effects_sys::PF_DrawEventInfo {
				update_rect: extra.effect_win.current_frame,
				depth: 32,
			};
		} else if let Some(pointer) = pointer {
			extra.u.do_click = after_effects_sys::PF_DoClickEventInfo {
				when: 0,
				screen_point: after_effects_sys::PF_Point {
					h: pointer.x,
					v: pointer.y,
				},
				num_clicks: 1,
				modifiers: 0,
				continue_refcon: [0; 4],
				send_drag: (event == after_effects_sys::PF_Event_DRAG) as after_effects_sys::PF_Boolean,
				last_time: pointer.last as after_effects_sys::PF_Boolean,
			};
		}
		let result = self.call_plugin(
			RawCommand::Event,
			(&mut extra as *mut after_effects_sys::PF_EventExtra).cast(),
		);
		// The draw ref is only valid for this DRAW. A later click must not
		// see a surface the rasteriser has already finished.
		kept.clear_draw_ref();
		if drawing && let Some(frame) = crate::suites::drawbot::end_frame() {
			self.custom_controls.insert(
				index,
				CustomControlImage {
					width: frame.width,
					height: frame.height,
					rgba: frame.rgba,
				},
			);
		}
		log::info!(
			"effect event {event} on param {index}: {result:?} evt_out_flags={:#x}",
			extra.evt_out_flags
		);
		let send_drag = matches!(
			event,
			after_effects_sys::PF_Event_DO_CLICK | after_effects_sys::PF_Event_DRAG // SAFETY: `event` is a click or drag here, so the plugin's reply is in `u.do_click`.
		) && unsafe { extra.u.do_click.send_drag } != 0;
		self.ui_contexts.insert(index, kept);
		(extra.evt_out_flags, send_drag)
	}

	/// Call the plugin with `PF_Cmd_RENDER` command ([`RenderPath::Legacy`]).
	fn render_legacy(&mut self) -> Result<()> {
		self.reset_cpu_worlds();
		self.poison_output();
		self.begin_output_guard();
		let result = self.call_plugin(RawCommand::Render, null_mut());
		self.end_output_guard();

		result
	}

	/// Call the plugin with `PF_Cmd_SMART_PRE_RENDER` command, letting it declare the
	/// input/output checkout regions it needs via [`Self::render_smart`].
	fn render_pre(&mut self) -> Result<()> {
		self.layer_checkouts.clear();
		// Each pre-render gets a fresh output, as in After Effects; plugins OR
		// their flags into it and would otherwise inherit the last frame's.
		self.smart_render_data.begin_pre_render();
		if !self.smart_render_data.is_gpu() {
			self.reset_cpu_worlds();
			self.poison_output();
			self.begin_output_guard();
		}
		let mut extra = self.smart_render_data.pre_render_extra();

		self.call_plugin(
			RawCommand::SmartPreRender,
			(&mut extra as *mut after_effects_sys::PF_PreRenderExtra).cast(),
		)?;

		self.smart_render_data.sync();

		Ok(())
	}

	/// Call the plugin with `PF_Cmd_SMART_RENDER` command, using the checkout regions
	/// declared during the preceding [`Self::render_pre`] call.
	fn render_smart(&mut self) -> Result<()> {
		let mut extra = self.smart_render_data.smart_render_extra();

		let result = self.call_plugin(
			RawCommand::SmartRender,
			(&mut extra as *mut after_effects_sys::PF_SmartRenderExtra).cast(),
		);
		self.end_output_guard();

		// The frame is done; release the plugin's pre-render data as AE does.
		self.dispose_pre_render();
		result
	}

	/// Whether the plugin declared `PF_OutFlag2_SUPPORTS_GPU_RENDER_F32` during
	/// `PF_Cmd_GLOBAL_SETUP`, i.e. it can render on the GPU into 32-bit-float
	/// `PF_PixelFormat_GPU_BGRA128` worlds ([`RenderPath::Gpu`]).
	///
	/// Only meaningful once global setup has run (i.e. after [`Host::load`]).
	pub fn supports_gpu(&self) -> bool {
		// Escape hatch for benchmarking / debugging the CPU path even on GPU-capable
		// effects: setting AEXLO_DISABLE_GPU forces the CPU render path.
		if std::env::var_os("AEXLO_DISABLE_GPU").is_some() {
			return false;
		}
		let flag = PF_OutFlag2_SUPPORTS_GPU_RENDER_F32 as PF_OutFlags2;
		self.global_out_flags2 & flag != 0
	}

	/// Run `PF_Cmd_GPU_DEVICE_SETUP`, creating aexlo's GPU context (Metal or CUDA)
	/// on first use and capturing the plugin-owned GPU data (compiled pipelines,
	/// etc.) it returns.
	///
	/// # Errors
	/// Returns an error if no GPU device is available, or if the plugin's setup
	/// command fails.
	fn gpu_device_setup(&mut self) -> Result<()> {
		if self.gpu_context.is_none() {
			self.gpu_context = crate::gpu::GpuContext::new();
			// A fresh context owns no buffers yet; force an input upload.
			self.gpu_input_uploaded = false;
		}

		let Some(ctx) = self.gpu_context.as_ref() else {
			return Err(AexloError::GpuUnavailable);
		};

		let what_gpu = ctx.framework();
		// The plugin's own GPU-API calls during setup (e.g. cudaMalloc) resolve
		// against the thread-current context.
		ctx.make_current();

		let mut input = PF_GPUDeviceSetupInput {
			what_gpu,
			device_index: 0,
		};
		let mut output = PF_GPUDeviceSetupOutput { gpu_data: null_mut() };
		let mut extra = PF_GPUDeviceSetupExtra {
			input: &mut input,
			output: &mut output,
		};

		self.call_plugin(
			RawCommand::GpuDeviceSetup,
			(&mut extra as *mut PF_GPUDeviceSetupExtra).cast(),
		)?;

		self.gpu_data = output.gpu_data;
		self.gpu_device_ready = true;
		Ok(())
	}

	/// Run `PF_Cmd_GPU_DEVICE_SETDOWN` so the plugin releases its GPU data, then drop
	/// this instance's GPU-world registrations. Safe to call when no device was set up.
	pub(super) fn gpu_device_setdown(&mut self) -> Result<()> {
		if !self.gpu_device_ready {
			return Ok(());
		}

		let what_gpu = self
			.gpu_context
			.as_ref()
			.map(|ctx| ctx.framework())
			.unwrap_or(PF_GPU_Framework_NONE as PF_GPU_Framework);
		if let Some(ctx) = &self.gpu_context {
			ctx.make_current();
		}
		let mut input = PF_GPUDeviceSetdownInput {
			gpu_data: self.gpu_data,
			what_gpu,
			device_index: 0,
		};
		let mut extra = PF_GPUDeviceSetdownExtra { input: &mut input };

		let result = self.call_plugin(
			RawCommand::GpuDeviceSetdown,
			(&mut extra as *mut PF_GPUDeviceSetdownExtra).cast(),
		);

		self.gpu_data = null_mut();
		self.gpu_device_ready = false;
		if let Some(ctx) = &self.gpu_context {
			ctx.unregister_all_worlds();
		}
		self.release_gpu_thread();
		result
	}

	/// Dispatch `PF_Cmd_SMART_RENDER_GPU` using the checkout regions declared during
	/// the preceding [`Self::render_pre`] call.
	fn render_smart_gpu(&mut self) -> Result<()> {
		let mut extra = self.smart_render_data.smart_render_extra();

		self.call_plugin(
			RawCommand::SmartRenderGpu,
			(&mut extra as *mut after_effects_sys::PF_SmartRenderExtra).cast(),
		)
	}

	/// Render one frame on the GPU: set up the device (once), back the input and
	/// output worlds with device buffers, upload the input as BGRA float, run the
	/// smart pre-render/GPU-render pair, wait for the GPU, and read the result back.
	///
	/// # Errors
	/// Propagates any failure from device setup or the render commands. On error the
	/// caller should fall back to CPU rendering (see [`Self::render_frame`]).
	fn render_gpu(&mut self) -> Result<()> {
		let result = self.render_gpu_current();
		// The UI thread (or any caller) may use another graphics API next.
		// A CUDA context left current on that thread corrupts the heap.
		self.release_gpu_thread();
		result
	}

	fn release_gpu_thread(&self) {
		if let Some(ctx) = &self.gpu_context {
			ctx.release_thread();
		}
	}

	fn render_gpu_current(&mut self) -> Result<()> {
		if !self.gpu_device_ready {
			self.gpu_device_setup()?;
		}

		// Phase A: geometry, buffers, and input upload. All borrows are of distinct
		// fields, so they coexist without borrowing `self` as a whole.
		let output_key = &self.world as *const _ as usize;
		let framework;
		let out_len;
		{
			let in_w = self.input_layer.width() as usize;
			let in_h = self.input_layer.height() as usize;
			let out_w = self.output_layer.width() as usize;
			let out_h = self.output_layer.height() as usize;
			out_len = out_w * out_h * GPU_BYTES_PER_PIXEL;

			// `in_data.width/height` describe the source layer, as in After Effects;
			// the output extent comes from the output world itself.
			self.render_context.set_size(in_w as i32, in_h as i32);

			// Present both worlds as f32 BGRA (16 bytes/pixel, no row padding).
			self.input_world.width = in_w as i32;
			self.input_world.height = in_h as i32;
			self.input_world.rowbytes = (in_w * GPU_BYTES_PER_PIXEL) as i32;
			self.world.width = out_w as i32;
			self.world.height = out_h as i32;
			self.world.rowbytes = (out_w * GPU_BYTES_PER_PIXEL) as i32;

			let input_key = &self.input_world as *const _ as usize;

			let ctx = self
				.gpu_context
				.as_mut()
				.ok_or_else(|| AexloError::Gpu("GPU context missing".to_string()))?;
			framework = ctx.framework();
			ctx.make_current();

			if !ctx.ensure_buffer(input_key, in_w * in_h * GPU_BYTES_PER_PIXEL)
				|| !ctx.ensure_buffer(output_key, out_len)
			{
				return Err(AexloError::Gpu("Failed to allocate GPU world buffers".to_string()));
			}

			// Pack + upload only when the input pixels changed since the last upload
			// (an input size change goes through `set_input`, which clears the flag,
			// so a reallocated buffer always gets fresh pixels).
			if !self.gpu_input_uploaded {
				Self::pack_layer_to_bgra_f32(self.input_layer.layer(), &mut self.gpu_upload_staging);
				if !ctx.write_buffer(input_key, bytemuck::cast_slice(&self.gpu_upload_staging)) {
					return Err(AexloError::Gpu("Failed to upload input pixels to the GPU".to_string()));
				}
				self.gpu_input_uploaded = true;
			}

			// Layers linked to layer parameters get their own device buffers.
			for linked in self.linked_layers.values_mut() {
				let key = linked.world_ptr(true) as usize;
				if !ctx.ensure_buffer(key, linked.gpu_len()) {
					return Err(AexloError::Gpu(
						"Failed to allocate GPU layer-parameter buffer".to_string(),
					));
				}
				if !linked.gpu_uploaded {
					Self::pack_layer_to_bgra_f32(linked.layer(), &mut self.gpu_upload_staging);
					if !ctx.write_buffer(key, bytemuck::cast_slice(&self.gpu_upload_staging)) {
						return Err(AexloError::Gpu(
							"Failed to upload layer-parameter pixels to the GPU".to_string(),
						));
					}
					linked.gpu_uploaded = true;
				}
			}
		}

		self.smart_render_data.configure_gpu(self.gpu_data, 0, framework);
		let result = self.run_gpu_frame(output_key, out_len);
		// Later renders default to the CPU; leaving the GPU configuration in
		// place would hand the next CPU `SMART_RENDER` a GPU frame description.
		self.smart_render_data.configure_cpu();
		result
	}

	/// Phases B and C of [`Self::render_gpu`], with the smart-render inputs
	/// configured for the GPU.
	fn run_gpu_frame(&mut self, output_key: usize, out_len: usize) -> Result<()> {
		// Phase B: pre-render declares regions, then the GPU render runs -- if the
		// plugin says this frame can render on the GPU. Otherwise After Effects
		// renders it on the CPU instead, which `render_frame` does on this error.
		self.render_pre()?;
		if !self.smart_render_data.gpu_render_possible() {
			self.dispose_pre_render();
			return Err(AexloError::GpuRenderDeclined);
		}
		// The plugin queues its kernels and returns. Those kernels still read the
		// pre-render block, so it is freed only after the context has drained.
		// Freeing it here races the GPU and corrupts the heap (Universe Glow).
		let rendered = self.render_smart_gpu();
		if let Some(ctx) = self.gpu_context.as_mut() {
			ctx.wait_for_completion();
		}
		log::debug!("GPU render waited for the device");
		self.dispose_pre_render();
		rendered?;

		// Phase C: read the rendered BGRA float output back into the output layer,
		// reusing the instance's staging buffer.
		if let Some(ctx) = self.gpu_context.as_ref() {
			self.gpu_readback_staging
				.resize(out_len / std::mem::size_of::<f32>(), 0.0);
			if ctx.read_buffer(output_key, bytemuck::cast_slice_mut(&mut self.gpu_readback_staging)) {
				Self::unpack_bgra_f32_to_layer(&self.gpu_readback_staging, self.output_layer.layer_mut());
			}
		}

		Ok(())
	}

	/// Pack an ARGB layer of any depth into `PF_PixelFormat_GPU_BGRA128` staging
	/// data: BGRA channel order, each channel normalised to `[0, 1]` float.
	///
	/// Writes into the caller-provided `staging` buffer (cleared first) so a
	/// render loop can reuse one allocation across frames.
	fn pack_layer_to_bgra_f32(layer: &AnyLayer, staging: &mut Vec<f32>) {
		fn pack<D: PixelDepth>(layer: &Layer<D>, staging: &mut Vec<f32>) {
			staging.reserve(layer.len() * 4);
			for pixel in layer.pixels() {
				staging.push(D::to_unit(pixel.blue));
				staging.push(D::to_unit(pixel.green));
				staging.push(D::to_unit(pixel.red));
				staging.push(D::to_unit(pixel.alpha));
			}
		}
		staging.clear();
		match layer {
			AnyLayer::U8(l) => pack(l, staging),
			AnyLayer::U16(l) => pack(l, staging),
			AnyLayer::F32(l) => pack(l, staging),
		}
	}

	/// Unpack `PF_PixelFormat_GPU_BGRA128` staging data (BGRA float) back into an
	/// ARGB layer of any depth, clamping and rounding integer channels.
	fn unpack_bgra_f32_to_layer(staging: &[f32], layer: &mut AnyLayer) {
		fn unpack<D: PixelDepth>(staging: &[f32], layer: &mut Layer<D>) {
			for (pixel, bgra) in layer.pixels_mut().iter_mut().zip(staging.as_chunks::<4>().0) {
				pixel.blue = D::from_unit(bgra[0]);
				pixel.green = D::from_unit(bgra[1]);
				pixel.red = D::from_unit(bgra[2]);
				pixel.alpha = D::from_unit(bgra[3]);
			}
		}
		match layer {
			AnyLayer::U8(l) => unpack(staging, l),
			AnyLayer::U16(l) => unpack(staging, l),
			AnyLayer::F32(l) => unpack(staging, l),
		}
	}

	/// Whether the plugin declared `PF_OutFlag2_SUPPORTS_SMART_RENDER` during
	/// `PF_Cmd_GLOBAL_SETUP`, i.e. it expects the smart pre-render/render command
	/// pair ([`RenderPath::Smart`]) rather than the legacy `PF_Cmd_RENDER`
	/// ([`RenderPath::Legacy`]).
	///
	/// Only meaningful once global setup has run (i.e. after [`Host::load`]).
	pub fn supports_smart_render(&self) -> bool {
		let flag = after_effects_sys::PF_OutFlag2_SUPPORTS_SMART_RENDER as after_effects_sys::PF_OutFlags2;
		self.global_out_flags2 & flag != 0
	}

	/// Render one frame the way After Effects would pick the path: on the GPU
	/// when the plugin [supports it](Self::supports_gpu), else the smart
	/// pre-render/render pair when [declared](Self::supports_smart_render), else
	/// the legacy `PF_Cmd_RENDER`. A failed GPU or smart render falls through to
	/// the next path; a cancelled one does not.
	///
	/// This is the call for a general-purpose host: many modern effects implement
	/// `PF_Cmd_SMART_RENDER`, others only the legacy command, and some declare
	/// smart support but still render correctly through the legacy path when the
	/// smart emulation can't satisfy them. Same as
	/// [`render_with(RenderPath::Auto)`](Self::render_with).
	///
	/// # Errors
	/// The error of the last path tried, typically
	/// [`AexloError::PluginExecutionFailed`].
	pub fn render_frame(&mut self) -> Result<()> {
		self.render_with(RenderPath::Auto)
	}

	/// Render one frame through `path` only, without falling back to another
	/// path (except [`RenderPath::Auto`], which is [`Self::render_frame`]).
	///
	/// For tests and tools that exercise one command path of a plugin; a host
	/// should call [`Self::render_frame`].
	///
	/// # Errors
	/// [`AexloError::PluginExecutionFailed`] if the plugin rejects a command,
	/// [`AexloError::GpuUnavailable`] or [`AexloError::GpuRenderDeclined`] for a
	/// GPU render that cannot run.
	pub fn render_with(&mut self, path: RenderPath) -> Result<()> {
		match path {
			RenderPath::Auto => self.render_auto(),
			RenderPath::Legacy => self.render_legacy(),
			RenderPath::Smart => self.render_pre().and_then(|()| self.render_smart()),
			RenderPath::Gpu => self.render_gpu(),
		}
	}

	/// [`RenderPath::Auto`]: GPU, then smart, then legacy.
	fn render_auto(&mut self) -> Result<()> {
		if self.supports_gpu() {
			match self.render_gpu() {
				Ok(()) => return Ok(()),
				// A cancelled render is not retried on another path.
				Err(err) if err.is_cancelled() => return Err(err),
				Err(err) => {
					if matches!(err, AexloError::GpuRenderDeclined) {
						log::debug!("Plugin declined GPU render for this frame; rendering on the CPU.");
					} else {
						log::warn!("GPU render failed ({err:?}); falling back to CPU render.");
					}
					// Undo GPU state so the CPU fallback doesn't leave the plugin
					// expecting a GPU frame or reporting GPU worlds.
					self.smart_render_data.configure_cpu();
					if let Some(ctx) = &self.gpu_context {
						ctx.unregister_all_worlds();
					}
				}
			}
		}

		if self.supports_smart_render() {
			match self.render_pre().and_then(|()| self.render_smart()) {
				Ok(()) => return Ok(()),
				Err(err) if err.is_cancelled() => return Err(err),
				Err(err) => {
					log::warn!("Smart render failed ({err:?}); falling back to legacy render.");
				}
			}
		}

		self.render_legacy()
	}

	/// Set the output frame size in pixels, resizing the output world and the smart-render
	/// output request rects. `in_data.width/height` keep describing the input
	/// layer (see [`Self::set_input_layer`]).
	///
	/// Call this before rendering. Global/params/sequence setup runs at the
	/// default size ([`Self::output_size`] after load), which plugins tolerate --
	/// After Effects itself resizes freely between renders.
	pub fn set_render_size(&mut self, width: u32, height: u32) {
		let kind = self.output_layer.kind();
		self.output_layer = HostLayer::new(AnyLayer::filled(kind, width, height, OPAQUE_BLACK));

		self.world = crate::core::helpers::LayerDefBuilder::new()
			.with_size(width as i32, height as i32)
			.build();
		self.sync_output_world();

		self.smart_render_data.set_output_rect(width as i32, height as i32);
	}

	/// Set the time the next commands operate on (e.g. `Time::frame(n, 30)` for
	/// frame `n` of a 30 fps comp).
	///
	/// The layer and comp the plugin sees through the AEGP suites run at the
	/// same frame rate. Plugins start at [`Time::default`], frame 0 at 30 fps.
	pub fn set_time(&mut self, time: Time) {
		let (value, step, scale) = time.into_parts();
		self.render_context.set_time(value, step, scale);
	}

	/// The time set by [`Self::set_time`].
	pub fn time(&self) -> Time {
		let (value, step, scale) = self.render_context.time();
		// Only `set_time` writes the render context's time, and it takes a `Time`.
		Time::new(value, step, scale).expect("the render context holds a valid time")
	}

	/// Release the plugin's pre-render data, attributing what it frees.
	pub(super) fn dispose_pre_render(&mut self) {
		let _tracking = self.tracking();
		self.smart_render_data.dispose_pre_render_data();
	}

	/// Strict mode: point the output world at a copy of the output with guard
	/// bands around it, for the plugin to render into.
	fn begin_output_guard(&mut self) {
		self.end_output_guard();
		if !self.strict.guard_bands {
			return;
		}
		let layer = self.output_layer.layer_mut();
		let bpp = layer.kind().bytes_per_pixel();
		let (w, h) = (layer.width() as usize, layer.height() as usize);
		let mut guarded = crate::strict::Guarded::new(w * bpp, h, bpp);
		// SAFETY: the layer's buffer is exactly `w * h * bpp` bytes.
		let bytes = unsafe { std::slice::from_raw_parts(layer.data_ptr(), w * h * bpp) };
		guarded.load_rows(bytes);
		self.world.data = guarded.data_ptr() as *mut after_effects_sys::PF_Pixel;
		self.world.rowbytes = guarded.rowbytes() as i32;
		self.output_guard = Some(guarded);
	}

	/// Copy the guarded output back and report overwritten guard bands.
	pub(super) fn end_output_guard(&mut self) {
		let Some(guarded) = self.output_guard.take() else {
			return;
		};
		let layer = self.output_layer.layer_mut();
		let len = layer.width() as usize * layer.height() as usize * layer.kind().bytes_per_pixel();
		// SAFETY: as in `begin_output_guard`.
		let bytes = unsafe { std::slice::from_raw_parts_mut(layer.data_mut_ptr(), len) };
		guarded.store_rows(bytes);
		let (w, h) = (layer.width(), layer.height());
		let broken = guarded.verify();
		if !broken.is_empty()
			&& let Ok(mut tracker) = self.tracker.lock()
		{
			for (band, bytes) in broken {
				tracker.report.guard_violations.push(crate::GuardViolation {
					world: format!("output {w}x{h}"),
					band,
					bytes,
				});
			}
		}
		self.sync_output_world();
	}

	/// Fill the output with the strict-mode poison, when asked to.
	fn poison_output(&mut self) {
		if self.strict.poison_output {
			crate::strict::poison(self.output_layer.layer_mut());
		}
	}

	/// Point `world` raw pointers at this instance's own owned buffers.
	/// Restore the 8-bit CPU layout of the input/output worlds and drop their
	/// GPU-world registration. [`Self::render_gpu`] reshapes both worlds to
	/// `GPU_BGRA128`; a later CPU render must not see that stride or have
	/// `PF_GetPixelFormat` report the float GPU format.
	fn reset_cpu_worlds(&mut self) {
		if let Some(ctx) = &self.gpu_context {
			ctx.unregister_all_worlds();
		}
		self.input_world = self.input_layer.as_sys();
		self.sync_output_world();
	}

	/// Describe `output_layer` in the output world: size, stride, depth flag
	/// and pixels. The rest of the world (extent hint, ...) is left alone.
	pub(super) fn sync_output_world(&mut self) {
		let layer = self.output_layer.as_sys();
		self.world.width = layer.width;
		self.world.height = layer.height;
		self.world.rowbytes = layer.rowbytes;
		self.world.world_flags = layer.world_flags;
		self.world.data = layer.data;
	}
}