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)]
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 use crate::graphics::color;
76
77 let layout = device.create_pipeline_layout(
78 &wgpu::PipelineLayoutDescriptor {
79 label: Some("iced_wgpu.quad.gradient.pipeline"),
80 push_constant_ranges: &[],
81 bind_group_layouts: &[constants_layout],
82 },
83 );
84
85 let shader =
86 device.create_shader_module(wgpu::ShaderModuleDescriptor {
87 label: Some("iced_wgpu.quad.gradient.shader"),
88 source: wgpu::ShaderSource::Wgsl(
89 std::borrow::Cow::Borrowed(
90 if color::GAMMA_CORRECTION {
91 concat!(
92 include_str!("../shader/quad.wgsl"),
93 "\n",
94 include_str!("../shader/vertex.wgsl"),
95 "\n",
96 include_str!(
97 "../shader/quad/gradient.wgsl"
98 ),
99 "\n",
100 include_str!("../shader/color/oklab.wgsl")
101 )
102 } else {
103 concat!(
104 include_str!("../shader/quad.wgsl"),
105 "\n",
106 include_str!("../shader/vertex.wgsl"),
107 "\n",
108 include_str!(
109 "../shader/quad/gradient.wgsl"
110 ),
111 "\n",
112 include_str!(
113 "../shader/color/linear_rgb.wgsl"
114 )
115 )
116 },
117 ),
118 ),
119 });
120
121 let pipeline = device.create_render_pipeline(
122 &wgpu::RenderPipelineDescriptor {
123 label: Some("iced_wgpu.quad.gradient.pipeline"),
124 layout: Some(&layout),
125 vertex: wgpu::VertexState {
126 module: &shader,
127 entry_point: Some("gradient_vs_main"),
128 buffers: &[wgpu::VertexBufferLayout {
129 array_stride: std::mem::size_of::<Gradient>()
130 as u64,
131 step_mode: wgpu::VertexStepMode::Instance,
132 attributes: &wgpu::vertex_attr_array!(
133 0 => Uint32x4,
135 1 => Uint32x4,
137 2 => Uint32x4,
139 3 => Uint32x4,
141 4 => Uint32x4,
143 5 => Float32x4,
145 6 => Float32x4,
147 7 => Float32x4,
149 8 => Float32x4,
151 9 => Float32
153 ),
154 }],
155 compilation_options:
156 wgpu::PipelineCompilationOptions::default(),
157 },
158 fragment: Some(wgpu::FragmentState {
159 module: &shader,
160 entry_point: Some("gradient_fs_main"),
161 targets: &quad::color_target_state(format),
162 compilation_options:
163 wgpu::PipelineCompilationOptions::default(),
164 }),
165 primitive: wgpu::PrimitiveState {
166 topology: wgpu::PrimitiveTopology::TriangleList,
167 front_face: wgpu::FrontFace::Cw,
168 ..Default::default()
169 },
170 depth_stencil: None,
171 multisample: wgpu::MultisampleState {
172 count: 1,
173 mask: !0,
174 alpha_to_coverage_enabled: false,
175 },
176 multiview: None,
177 cache: None,
178 },
179 );
180
181 Self { pipeline }
182 }
183
184 #[cfg(target_arch = "wasm32")]
185 Self {}
186 }
187
188 #[allow(unused_variables)]
189 pub fn render<'a>(
190 &'a self,
191 render_pass: &mut wgpu::RenderPass<'a>,
192 constants: &'a wgpu::BindGroup,
193 layer: &'a Layer,
194 range: Range<usize>,
195 ) {
196 #[cfg(not(target_arch = "wasm32"))]
197 {
198 render_pass.set_pipeline(&self.pipeline);
199 render_pass.set_bind_group(0, constants, &[]);
200 render_pass.set_vertex_buffer(0, layer.instances.slice(..));
201
202 render_pass.draw(0..6, range.start as u32..range.end as u32);
203 }
204 }
205}