@@ -37,6 +37,12 @@ void BindVertices(Command& cmd,
37
37
cmd.BindVertices (vtx_buffer);
38
38
}
39
39
40
+ Matrix MakeAnchorScale (const Point& anchor, Vector2 scale) {
41
+ return Matrix::MakeTranslation ({anchor.x , anchor.y , 0 }) *
42
+ Matrix::MakeScale (scale) *
43
+ Matrix::MakeTranslation ({-anchor.x , -anchor.y , 0 });
44
+ }
45
+
40
46
std::shared_ptr<Texture> MakeDownsampleSubpass (
41
47
const ContentContext& renderer,
42
48
std::shared_ptr<Texture> input_texture,
@@ -59,19 +65,14 @@ std::shared_ptr<Texture> MakeDownsampleSubpass(
59
65
frame_info.texture_sampler_y_coord_scale = 1.0 ;
60
66
frame_info.alpha = 1.0 ;
61
67
62
- Quad vertices = {Point (0 , 0 ), Point (1 , 0 ), Point (0 , 1 ), Point (1 , 1 )};
63
-
64
68
// Insert transparent gutter around the downsampled image so the blur
65
69
// creates a halo effect.
66
- ISize texture_size = input_texture->GetSize ();
67
- vertices =
68
- (Matrix::MakeTranslation ({0.5 , 0.5 , 0 }) *
69
- Matrix::MakeScale (
70
- {texture_size.width / (texture_size.width + padding.x * 2 ),
71
- texture_size.height / (texture_size.height + padding.y * 2 ),
72
- 1.0 }) *
73
- Matrix::MakeTranslation ({-0.5 , -0.5 , 0 }))
74
- .Transform (vertices);
70
+ Vector2 texture_size = Vector2 (input_texture->GetSize ());
71
+ Quad vertices =
72
+ MakeAnchorScale ({0.5 , 0.5 },
73
+ texture_size / (texture_size + padding * 2 ))
74
+ .Transform (
75
+ {Point (0 , 0 ), Point (1 , 0 ), Point (0 , 1 ), Point (1 , 1 )});
75
76
76
77
BindVertices<TextureFillVertexShader>(cmd, host_buffer,
77
78
{
@@ -153,12 +154,6 @@ Scalar CalculateScale(Scalar radius) {
153
154
return (curve - 1 ) * limit + 1 ;
154
155
};
155
156
156
- template <typename T, typename U>
157
- Vector2 CalculateSizeRatio (const T& x, const U& y) {
158
- return Vector2{x.width / static_cast <Scalar>(y.width ),
159
- x.height / static_cast <Scalar>(y.height )};
160
- }
161
-
162
157
} // namespace
163
158
164
159
GaussianBlurFilterContents::GaussianBlurFilterContents (Scalar sigma)
@@ -220,13 +215,13 @@ std::optional<Entity> GaussianBlurFilterContents::RenderFilter(
220
215
Vector2 downsample_scalar (desired_scalar, desired_scalar);
221
216
Vector2 padding (ceil (blur_radius), ceil (blur_radius));
222
217
223
- Size expanded_size (
224
- input_snapshot->texture ->GetSize ().width + 2.0 * padding.x ,
225
- input_snapshot->texture ->GetSize ().height + 2.0 * padding.y );
218
+ Vector2 padded_size =
219
+ Vector2 (input_snapshot->texture ->GetSize ()) + 2.0 * padding;
226
220
// TODO(gaaclarke): I don't think we are correctly handling this fractional
227
221
// amount we are throwing away.
228
- ISize subpass_size = ISize (round (expanded_size.width * downsample_scalar.x ),
229
- round (expanded_size.height * downsample_scalar.y ));
222
+ Vector2 downsampled_size = padded_size * downsample_scalar;
223
+ ISize subpass_size =
224
+ ISize (round (downsampled_size.x ), round (downsampled_size.y ));
230
225
231
226
Quad uvs =
232
227
CalculateUVs (inputs[0 ], entity, input_snapshot->texture ->GetSize ());
@@ -235,13 +230,12 @@ std::optional<Entity> GaussianBlurFilterContents::RenderFilter(
235
230
renderer, input_snapshot->texture , input_snapshot->sampler_descriptor ,
236
231
uvs, subpass_size, padding);
237
232
238
- Size pass1_pixel_size (1.0 / pass1_out_texture->GetSize ().width ,
239
- 1.0 / pass1_out_texture->GetSize ().height );
233
+ Vector2 pass1_pixel_size = 1.0 / Vector2 (pass1_out_texture->GetSize ());
240
234
241
235
std::shared_ptr<Texture> pass2_out_texture = MakeBlurSubpass (
242
236
renderer, pass1_out_texture, input_snapshot->sampler_descriptor ,
243
237
GaussianBlurFragmentShader::BlurInfo{
244
- .blur_uv_offset = Point (0.0 , pass1_pixel_size.height ),
238
+ .blur_uv_offset = Point (0.0 , pass1_pixel_size.y ),
245
239
.blur_sigma = sigma_ * downsample_scalar.y ,
246
240
.blur_radius = blur_radius * downsample_scalar.y ,
247
241
.step_size = 1.0 ,
@@ -251,7 +245,7 @@ std::optional<Entity> GaussianBlurFilterContents::RenderFilter(
251
245
std::shared_ptr<Texture> pass3_out_texture = MakeBlurSubpass (
252
246
renderer, pass2_out_texture, input_snapshot->sampler_descriptor ,
253
247
GaussianBlurFragmentShader::BlurInfo{
254
- .blur_uv_offset = Point (pass1_pixel_size.width , 0.0 ),
248
+ .blur_uv_offset = Point (pass1_pixel_size.x , 0.0 ),
255
249
.blur_sigma = sigma_ * downsample_scalar.x ,
256
250
.blur_radius = blur_radius * downsample_scalar.x ,
257
251
.step_size = 1.0 ,
@@ -260,14 +254,13 @@ std::optional<Entity> GaussianBlurFilterContents::RenderFilter(
260
254
SamplerDescriptor sampler_desc = MakeSamplerDescriptor (
261
255
MinMagFilter::kLinear , SamplerAddressMode::kClampToEdge );
262
256
263
- Vector2 final_scale =
264
- CalculateSizeRatio (expanded_size, pass1_out_texture->GetSize ());
265
257
return Entity::FromSnapshot (
266
258
Snapshot{
267
259
.texture = pass3_out_texture,
268
260
.transform = entity.GetTransformation () *
269
261
Matrix::MakeTranslation ({-padding.x , -padding.y , 0 }) *
270
- Matrix::MakeScale (final_scale),
262
+ Matrix::MakeScale (padded_size /
263
+ Vector2 (pass1_out_texture->GetSize ())),
271
264
.sampler_descriptor = sampler_desc,
272
265
.opacity = input_snapshot->opacity },
273
266
entity.GetBlendMode (), entity.GetClipDepth ());
0 commit comments