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
//! Graphics rendering pipeline system for Tessera UI framework.
//!
//! This module provides the core infrastructure for pluggable graphics rendering pipelines
//! in Tessera. The design philosophy emphasizes flexibility and extensibility, allowing
//! developers to create custom rendering effects without being constrained by built-in
//! drawing primitives.
//!
//! # Architecture Overview
//!
//! The pipeline system uses a trait-based approach with type erasure to support dynamic
//! dispatch of rendering commands. Each pipeline is responsible for rendering a specific
//! type of draw command, such as shapes, text, images, or custom visual effects.
//!
//! ## Key Components
//!
//! - [`DrawablePipeline<T>`]: The main trait for implementing custom rendering pipelines
//! - [`PipelineRegistry`]: Manages and dispatches commands to registered pipelines
//! - [`ErasedDrawablePipeline`]: Internal trait for type erasure and dynamic dispatch
//!
//! # Design Philosophy
//!
//! Unlike traditional UI frameworks that provide built-in "brush" or drawing primitives,
//! Tessera treats shaders as first-class citizens. This approach offers several advantages:
//!
//! - **Modern GPU Utilization**: Leverages WGPU and WGSL for efficient, cross-platform rendering
//! - **Advanced Visual Effects**: Enables complex effects like neumorphic design, lighting,
//! shadows, reflections, and bloom that are difficult to achieve with traditional approaches
//! - **Flexibility**: Custom shaders allow for unlimited creative possibilities
//! - **Performance**: Direct GPU programming eliminates abstraction overhead
//!
//! # Pipeline Lifecycle
//!
//! Each pipeline follows a three-phase lifecycle during rendering:
//!
//! 1. **Begin Pass**: Setup phase for initializing pipeline-specific resources
//! 2. **Draw**: Main rendering phase where commands are processed
//! 3. **End Pass**: Cleanup phase for finalizing rendering operations
//!
//! # Implementation Guide
//!
//! ## Creating a Custom Pipeline
//!
//! To create a custom rendering pipeline:
//!
//! 1. Define your draw command struct implementing [`DrawCommand`]
//! 2. Create a pipeline struct implementing [`DrawablePipeline<YourCommand>`]
//! 3. Register the pipeline with [`PipelineRegistry::register`]
//!
//! ## Example: Simple Rectangle Pipeline
//!
//! ```rust,ignore
//! use tessera_ui::{DrawCommand, DrawablePipeline, PxPosition, PxSize};
//! use wgpu;
//!
//! // 1. Define the draw command
//! #[derive(Debug)]
//! struct RectangleCommand {
//! color: [f32; 4],
//! corner_radius: f32,
//! }
//!
//! impl DrawCommand for RectangleCommand {
//! // Most commands don't need barriers
//! fn barrier(&self) -> Option<tessera_ui::BarrierRequirement> {
//! None
//! }
//! }
//!
//! // 2. Implement the pipeline
//! struct RectanglePipeline {
//! render_pipeline: wgpu::RenderPipeline,
//! uniform_buffer: wgpu::Buffer,
//! bind_group: wgpu::BindGroup,
//! }
//!
//! impl RectanglePipeline {
//! fn new(device: &wgpu::Device, config: &wgpu::SurfaceConfiguration, sample_count: u32) -> Self {
//! // Create shader, pipeline, buffers, etc.
//! // ... implementation details ...
//! # unimplemented!()
//! }
//! }
//!
//! impl DrawablePipeline<RectangleCommand> for RectanglePipeline {
//! fn draw(
//! &mut self,
//! context: &mut DrawContext<RectangleCommand>,
//! ) {
//! // Update uniforms with command data
//! // Set pipeline and draw
//! context.render_pass.set_pipeline(&self.render_pipeline);
//! context.render_pass.set_bind_group(0, &self.bind_group, &[]);
//! context.render_pass.draw(0..6, 0..1); // Draw quad
//! }
//! }
//!
//! // 3. Register the pipeline
//! let mut registry = PipelineRegistry::new();
//! let rectangle_pipeline = RectanglePipeline::new(&device, &config, sample_count);
//! registry.register(rectangle_pipeline);
//! ```
//!
//! # Integration with Basic Components
//!
//! The `tessera_basic_components` crate demonstrates real-world pipeline implementations:
//!
//! - **ShapePipeline**: Renders rounded rectangles, circles, and complex shapes with shadows and ripple effects
//! - **TextPipeline**: Handles text rendering with font management and glyph caching
//! - **ImagePipeline**: Displays images with various scaling and filtering options
//! - **FluidGlassPipeline**: Creates advanced glass effects with distortion and transparency
//!
//! These pipelines are registered in `tessera_ui_basic_components::pipelines::register_pipelines()`.
//!
//! # Performance Considerations
//!
//! - **Batch Similar Commands**: Group similar draw commands to minimize pipeline switches
//! - **Resource Management**: Reuse buffers and textures when possible
//! - **Shader Optimization**: Write efficient shaders optimized for your target platforms
//! - **State Changes**: Minimize render state changes within the draw method
//!
//! # Advanced Features
//!
//! ## Barrier Requirements
//!
//! Some rendering effects need to sample from previously rendered content (e.g., blur effects).
//! Implement [`DrawCommand::barrier()`] to return [`BarrierRequirement::SampleBackground`]
//! for such commands.
//!
//! ## Multi-Pass Rendering
//!
//! Use `begin_pass()` and `end_pass()` for pipelines that require multiple rendering passes
//! or complex setup/teardown operations.
//!
//! ## Scene Texture Access
//!
//! The `scene_texture_view` parameter provides access to the current scene texture,
//! enabling effects that sample from the background or perform post-processing.
use ;
use crate::;
/// Provides context for operations that occur once per frame.
///
/// This struct bundles essential WGPU resources and configuration that are relevant
/// for the entire rendering frame, but are not specific to a single render pass.
/// Provides context for operations within a single render pass.
///
/// This struct bundles WGPU resources and configuration specific to a render pass,
/// including the active render pass encoder and the scene texture view for sampling.
/// Provides comprehensive context for drawing operations within a render pass.
///
/// This struct extends `PassContext` with information specific to individual draw calls,
/// including the commands to be rendered and an optional clipping rectangle.
///
/// # Type Parameters
///
/// * `T` - The specific [`DrawCommand`] type being processed.
///
/// # Fields
///
/// * `device` - The WGPU device, used for creating and managing GPU resources.
/// * `queue` - The WGPU queue, used for submitting command buffers and writing buffer data.
/// * `config` - The current surface configuration, providing information like format and dimensions.
/// * `render_pass` - The active `wgpu::RenderPass` encoder, used to record rendering commands.
/// * `commands` - A slice of tuples, each containing a draw command, its size, and its position.
/// * `scene_texture_view` - A view of the current scene texture, useful for effects that sample from the background.
/// * `clip_rect` - An optional rectangle defining the clipping area for the draw call.
/// Core trait for implementing custom graphics rendering pipelines.
///
/// This trait defines the interface for rendering pipelines that process specific types
/// of draw commands. Each pipeline is responsible for setting up GPU resources,
/// managing render state, and executing the actual drawing operations.
///
/// # Type Parameters
///
/// * `T` - The specific [`DrawCommand`] type this pipeline can handle
///
/// # Lifecycle Methods
///
/// The pipeline system provides five lifecycle hooks, executed in the following order:
///
/// 1. [`begin_frame()`](Self::begin_frame): Called once at the start of a new frame, before any render passes.
/// 2. [`begin_pass()`](Self::begin_pass): Called at the start of each render pass that involves this pipeline.
/// 3. [`draw()`](Self::draw): Called for each command of type `T` within a render pass.
/// 4. [`end_pass()`](Self::end_pass): Called at the end of each render pass that involved this pipeline.
/// 5. [`end_frame()`](Self::end_frame): Called once at the end of the frame, after all render passes are complete.
///
/// Typically, `begin_pass`, `draw`, and `end_pass` are used for the core rendering logic within a pass,
/// while `begin_frame` and `end_frame` are used for setup and teardown that spans the entire frame.
///
/// # Implementation Notes
///
/// - Only the [`draw()`](Self::draw) method is required; others have default empty implementations.
/// - Pipelines should be stateless between frames when possible
/// - Resource management should prefer reuse over recreation
/// - Consider batching multiple commands for better performance
///
/// # Example
///
/// See the module-level documentation for a complete implementation example.
/// Internal trait for type erasure of drawable pipelines.
///
/// This trait enables dynamic dispatch of draw commands to their corresponding pipelines
/// without knowing the specific command type at compile time. It's used internally by
/// the [`PipelineRegistry`] and should not be implemented directly by users.
///
/// The type erasure is achieved through the [`AsAny`] trait, which allows downcasting
/// from `&dyn DrawCommand` to concrete command types.
///
/// # Implementation Note
///
/// This trait is automatically implemented for any type that implements
/// [`DrawablePipeline<T>`] through the [`DrawablePipelineImpl`] wrapper.
/// Registry for managing and dispatching drawable pipelines.
///
/// The `PipelineRegistry` serves as the central hub for all rendering pipelines in the
/// Tessera framework. It maintains a collection of registered pipelines and handles
/// the dispatch of draw commands to their appropriate pipelines.
///
/// # Architecture
///
/// The registry uses type erasure to store pipelines of different types in a single
/// collection. When a draw command needs to be rendered, the registry iterates through
/// all registered pipelines until it finds one that can handle the command type.
///
/// # Usage Pattern
///
/// 1. Create a new registry
/// 2. Register all required pipelines during application initialization
/// 3. The renderer uses the registry to dispatch commands during frame rendering
///
/// # Example
///
/// ```rust,ignore
/// use tessera_ui::renderer::drawer::PipelineRegistry;
///
/// // Create registry and register pipelines
/// let mut registry = PipelineRegistry::new();
/// registry.register(my_shape_pipeline);
/// registry.register(my_text_pipeline);
/// registry.register(my_image_pipeline);
///
/// // Registry is now ready for use by the renderer
/// ```
///
/// # Performance Considerations
///
/// - Pipeline lookup is O(1) on average due to HashMap implementation.