1use crate::Buffer;
2use crate::graphics::gradient;
3use crate::quad::{self, Quad};
4
5use bytemuck::{Pod, Zeroable};
6use std::ops::Range;
7
8#[derive(Clone, Copy, Debug)]
10#[repr(C)]
11pub struct Gradient {
12 pub gradient: gradient::Packed,
14
15 pub quad: Quad,
17}
18
19#[allow(unsafe_code)]
20unsafe impl Pod for Gradient {}
21
22#[allow(unsafe_code)]
23unsafe impl Zeroable for Gradient {}
24
25#[derive(Debug)]
26pub struct Layer {
27 instances: Buffer<Gradient>,
28 instance_count: usize,
29}
30
31impl Layer {
32 pub fn new(device: &wgpu::Device) -> Self {
33 let instances = Buffer::new(
34 device,
35 "iced_wgpu.quad.gradient.buffer",
36 quad::INITIAL_INSTANCES,
37 wgpu::BufferUsages::VERTEX | wgpu::BufferUsages::COPY_DST,
38 );
39
40 Self {
41 instances,
42 instance_count: 0,
43 }
44 }
45
46 pub fn prepare(
47 &mut self,
48 device: &wgpu::Device,
49 encoder: &mut wgpu::CommandEncoder,
50 belt: &mut wgpu::util::StagingBelt,
51 instances: &[Gradient],
52 ) {
53 let _ = self.instances.resize(device, instances.len());
54 let _ = self.instances.write(device, encoder, belt, 0, instances);
55
56 self.instance_count = instances.len();
57 }
58}
59
60#[derive(Debug, Clone)]
61pub struct Pipeline {
62 #[cfg(not(target_arch = "wasm32"))]
63 pipeline: wgpu::RenderPipeline,
64}
65
66impl Pipeline {
67 #[allow(unused_variables)]
68 pub fn new(
69 device: &wgpu::Device,
70 format: wgpu::TextureFormat,
71 constants_layout: &wgpu::BindGroupLayout,
72 ) -> Self {
73 #[cfg(not(target_arch = "wasm32"))]
74 {
75 let layout = device.create_pipeline_layout(
76 &wgpu::PipelineLayoutDescriptor {
77 label: Some("iced_wgpu.quad.gradient.pipeline"),
78 push_constant_ranges: &[],
79 bind_group_layouts: &[constants_layout],
80 },
81 );
82
83 let shader =
84 device.create_shader_module(wgpu::ShaderModuleDescriptor {
85 label: Some("iced_wgpu.quad.gradient.shader"),
86 source: wgpu::ShaderSource::Wgsl(
87 std::borrow::Cow::Borrowed(concat!(
88 include_str!("../shader/quad.wgsl"),
89 "\n",
90 include_str!("../shader/vertex.wgsl"),
91 "\n",
92 include_str!("../shader/quad/gradient.wgsl"),
93 "\n",
94 include_str!("../shader/color.wgsl"),
95 "\n",
96 include_str!("../shader/color/linear_rgb.wgsl")
97 )),
98 ),
99 });
100
101 let pipeline = device.create_render_pipeline(
102 &wgpu::RenderPipelineDescriptor {
103 label: Some("iced_wgpu.quad.gradient.pipeline"),
104 layout: Some(&layout),
105 vertex: wgpu::VertexState {
106 module: &shader,
107 entry_point: Some("gradient_vs_main"),
108 buffers: &[wgpu::VertexBufferLayout {
109 array_stride: std::mem::size_of::<Gradient>()
110 as u64,
111 step_mode: wgpu::VertexStepMode::Instance,
112 attributes: &wgpu::vertex_attr_array!(
113 0 => Uint32x4,
115 1 => Uint32x4,
117 2 => Uint32x4,
119 3 => Uint32x4,
121 4 => Uint32x4,
123 5 => Float32x4,
125 6 => Float32x4,
127 7 => Float32x4,
129 8 => Float32x4,
131 9 => Float32,
133 10 => Uint32,
135 ),
136 }],
137 compilation_options:
138 wgpu::PipelineCompilationOptions::default(),
139 },
140 fragment: Some(wgpu::FragmentState {
141 module: &shader,
142 entry_point: Some("gradient_fs_main"),
143 targets: &quad::color_target_state(format),
144 compilation_options:
145 wgpu::PipelineCompilationOptions::default(),
146 }),
147 primitive: wgpu::PrimitiveState {
148 topology: wgpu::PrimitiveTopology::TriangleList,
149 front_face: wgpu::FrontFace::Cw,
150 ..Default::default()
151 },
152 depth_stencil: None,
153 multisample: wgpu::MultisampleState {
154 count: 1,
155 mask: !0,
156 alpha_to_coverage_enabled: false,
157 },
158 multiview: None,
159 cache: None,
160 },
161 );
162
163 Self { pipeline }
164 }
165
166 #[cfg(target_arch = "wasm32")]
167 Self {}
168 }
169
170 #[allow(unused_variables)]
171 pub fn render<'a>(
172 &'a self,
173 render_pass: &mut wgpu::RenderPass<'a>,
174 constants: &'a wgpu::BindGroup,
175 layer: &'a Layer,
176 range: Range<usize>,
177 ) {
178 #[cfg(not(target_arch = "wasm32"))]
179 {
180 render_pass.set_pipeline(&self.pipeline);
181 render_pass.set_bind_group(0, constants, &[]);
182 render_pass.set_vertex_buffer(0, layer.instances.slice(..));
183
184 render_pass.draw(0..6, range.start as u32..range.end as u32);
185 }
186 }
187}