74 {
75 if (renderer.GetDeviceCapabilities().SupportsSSBO()) {
76 return RenderSSBO(renderer, entity, pass);
77 }
78 if (colors_.size() <= kMaxUniformGradientStops &&
79 stops_.size() <= kMaxUniformGradientStops) {
80 return RenderUniform(renderer, entity, pass);
81 }
82 return RenderTexture(renderer, entity, pass);
83}
84
85bool SweepGradientContents::RenderSSBO(const ContentContext& renderer,
86 const Entity& entity,
87 RenderPass& pass) const {
88 using VS = SweepGradientSSBOFillPipeline::VertexShader;
89 using FS = SweepGradientSSBOFillPipeline::FragmentShader;
90
91 VS::FrameInfo frame_info;
92 frame_info.matrix = GetInverseEffectTransform();
93 VS::BindFrameInfo(
94 pass, renderer.GetTransientsDataBuffer().EmplaceUniform(frame_info));
95
96 PipelineBuilderCallback pipeline_callback =
97 [&renderer](ContentContextOptions options) {
98 return renderer.GetSweepGradientSSBOFillPipeline(options);
99 };
100 return ColorSourceContents::DrawGeometry<VS>(
101 renderer, entity, pass, pipeline_callback, frame_info,
102 [this, &renderer, &entity](RenderPass& pass) {
103 FS::FragInfo frag_info;
104 frag_info.center = center_;
105 frag_info.bias = bias_;
106 frag_info.scale = scale_;
107 frag_info.tile_mode =
static_cast<Scalar>(tile_mode_);
108 frag_info.decal_border_color = decal_border_color_;
109 frag_info.alpha =
110 GetOpacityFactor() *
111 GetGeometry()->ComputeAlphaCoverage(entity.GetTransform());
112
113 auto& data_host_buffer = renderer.GetTransientsDataBuffer();
115
116 frag_info.colors_length = colors.size();
117 auto color_buffer = data_host_buffer.Emplace(
118 colors.data(), colors.size() * sizeof(StopData),
119 renderer.GetDeviceCapabilities()
120 .GetMinimumStorageBufferAlignment());
121
122 pass.SetCommandLabel("SweepGradientSSBOFill");
123
124 FS::BindFragInfo(
125 pass, renderer.GetTransientsDataBuffer().EmplaceUniform(frag_info));
126 FS::BindColorData(pass, color_buffer);
127
128 return true;
129 });
130}
131
132bool SweepGradientContents::RenderUniform(const ContentContext& renderer,
133 const Entity& entity,
134 RenderPass& pass) const {
135 using VS = SweepGradientUniformFillPipeline::VertexShader;
136 using FS = SweepGradientUniformFillPipeline::FragmentShader;
137
138 VS::FrameInfo frame_info;
139 frame_info.matrix = GetInverseEffectTransform();
140 VS::BindFrameInfo(
141 pass, renderer.GetTransientsDataBuffer().EmplaceUniform(frame_info));
142
143 PipelineBuilderCallback pipeline_callback =
144 [&renderer](ContentContextOptions options) {
145 return renderer.GetSweepGradientUniformFillPipeline(options);
146 };
147 return ColorSourceContents::DrawGeometry<VS>(
148 renderer, entity, pass, pipeline_callback, frame_info,
149 [this, &renderer, &entity](RenderPass& pass) {
150 FS::FragInfo frag_info;
151 frag_info.center = center_;
152 frag_info.bias = bias_;
153 frag_info.scale = scale_;
154 frag_info.tile_mode =
static_cast<Scalar>(tile_mode_);
155 frag_info.alpha =
156 GetOpacityFactor() *
157 GetGeometry()->ComputeAlphaCoverage(entity.GetTransform());
159 colors_, stops_, frag_info.colors, frag_info.stop_pairs);
160
161 frag_info.decal_border_color = decal_border_color_;
162
163 pass.SetCommandLabel("SweepGradientUniformFill");
164
165 FS::BindFragInfo(
166 pass, renderer.GetTransientsDataBuffer().EmplaceUniform(frag_info));
167
168 return true;
169 });
170}
171
172bool SweepGradientContents::RenderTexture(const ContentContext& renderer,
173 const Entity& entity,
174 RenderPass& pass) const {
175 using VS = SweepGradientFillPipeline::VertexShader;
176 using FS = SweepGradientFillPipeline::FragmentShader;
177
179 auto gradient_texture =
181 if (gradient_texture == nullptr) {
182 return false;
183 }
184
185 VS::FrameInfo frame_info;
186 frame_info.matrix = GetInverseEffectTransform();
187
188 PipelineBuilderCallback pipeline_callback =
189 [&renderer](ContentContextOptions options) {
190 return renderer.GetSweepGradientFillPipeline(options);
191 };
192 return ColorSourceContents::DrawGeometry<VS>(
193 renderer, entity, pass, pipeline_callback, frame_info,
194 [this, &renderer, &gradient_texture, &entity](RenderPass& pass) {
195 FS::FragInfo frag_info;
196 frag_info.center = center_;
197 frag_info.bias = bias_;
198 frag_info.scale = scale_;
199 frag_info.texture_sampler_y_coord_scale =
200 gradient_texture->GetYCoordScale();
201 frag_info.tile_mode =
static_cast<Scalar>(tile_mode_);
202 frag_info.decal_border_color = decal_border_color_;
203 frag_info.alpha =
204 GetOpacityFactor() *
205 GetGeometry()->ComputeAlphaCoverage(entity.GetTransform());
206 frag_info.half_texel =
207 Vector2(0.5 / gradient_texture->GetSize().width,
208 0.5 / gradient_texture->GetSize().height);
209
210 SamplerDescriptor sampler_desc;
211 sampler_desc.min_filter = MinMagFilter::kLinear;
212 sampler_desc.mag_filter = MinMagFilter::kLinear;
213
214 pass.SetCommandLabel("SweepGradientFill");
215
216 FS::BindFragInfo(
217 pass, renderer.GetTransientsDataBuffer().EmplaceUniform(frag_info));
218 FS::BindTextureSampler(
219 pass, gradient_texture,
220 renderer.GetContext()->GetSamplerLibrary()->GetSampler(
221 sampler_desc));
222
223 return true;
224 });
225}
226
227bool SweepGradientContents::ApplyColorFilter(
228 const ColorFilterProc& color_filter_proc) {
229 for (Color& color : colors_) {
230 color = color_filter_proc(color);
231 }
232 decal_border_color_ = color_filter_proc(decal_border_color_);
233 return true;
234}
235
236}
LinePipeline::FragmentShader FS
int PopulateUniformGradientColors(const std::vector< Color > &colors, const std::vector< Scalar > &stops, Vector4 frag_info_colors[kMaxUniformGradientStops], Vector4 frag_info_stop_pairs[kMaxUniformGradientStops/2])
Populate 2 arrays with the colors and stop data for a gradient.
std::vector< StopData > CreateGradientColors(const std::vector< Color > &colors, const std::vector< Scalar > &stops)
Populate a vector with the color and stop data for a gradient.
LinePipeline::VertexShader VS
std::shared_ptr< Texture > CreateGradientTexture(const GradientData &gradient_data, const std::shared_ptr< impeller::Context > &context)
Create a host visible texture that contains the gradient defined by the provided gradient data.
GradientData CreateGradientBuffer(const std::vector< Color > &colors, const std::vector< Scalar > &stops)
Populate a vector with the interpolated color bytes for the linear gradient described by colors and s...