|
14 | 14 | * limitations under the License. |
15 | 15 | */ |
16 | 16 |
|
17 | | -static float2 scale; |
18 | | -static float2 center, dimensions; |
19 | | -static float range, inv_max_dist, shade, slope; |
20 | | -static float2 overDimensions; |
| 17 | +static float2 neg_center, axis_scale, inv_dimensions; |
| 18 | +static float sloped_neg_range, sloped_inv_max_dist, shade, opp_shade; |
21 | 19 |
|
22 | 20 | void init_vignette(uint32_t dim_x, uint32_t dim_y, float center_x, float center_y, |
23 | 21 | float desired_scale, float desired_shade, float desired_slope) { |
24 | 22 |
|
25 | | - center.x = center_x; |
26 | | - center.y = center_y; |
27 | | - dimensions.x = (float)dim_x; |
28 | | - dimensions.y = (float)dim_y; |
| 23 | + neg_center.x = -center_x; |
| 24 | + neg_center.y = -center_y; |
| 25 | + inv_dimensions.x = 1.f / (float)dim_x; |
| 26 | + inv_dimensions.y = 1.f / (float)dim_y; |
| 27 | + |
| 28 | + axis_scale = (float2)1.f; |
| 29 | + if (dim_x > dim_y) |
| 30 | + axis_scale.y = (float)dim_y / (float)dim_x; |
| 31 | + else |
| 32 | + axis_scale.x = (float)dim_x / (float)dim_y; |
| 33 | + |
| 34 | + const float max_dist = 0.5 * length(axis_scale); |
| 35 | + sloped_inv_max_dist = desired_slope * 1.f/max_dist; |
29 | 36 |
|
30 | | - float max_dist = 0.5; |
31 | | - if (dim_x > dim_y) { |
32 | | - scale.x = 1.0; |
33 | | - scale.y = dimensions.y / dimensions.x; |
34 | | - max_dist *= sqrt(scale.y*scale.y + 1); |
35 | | - } else { |
36 | | - scale.x = dimensions.x / dimensions.y; |
37 | | - scale.y = 1.0; |
38 | | - max_dist *= sqrt(scale.x*scale.x + 1); |
39 | | - } |
40 | | - inv_max_dist = 1.0/max_dist; |
41 | 37 | // Range needs to be between 1.3 to 0.6. When scale is zero then range is |
42 | 38 | // 1.3 which means no vignette at all because the luminousity difference is |
43 | 39 | // less than 1/256. Expect input scale to be between 0.0 and 1.0. |
44 | | - range = 1.3 - 0.7*sqrt(desired_scale); |
45 | | - shade = desired_shade; |
46 | | - slope = desired_slope; |
| 40 | + const float neg_range = 0.7*sqrt(desired_scale) - 1.3; |
| 41 | + sloped_neg_range = exp(neg_range * desired_slope); |
47 | 42 |
|
48 | | - overDimensions = ((float2)1.f) / dimensions; |
| 43 | + shade = desired_shade; |
| 44 | + opp_shade = 1.f - desired_shade; |
49 | 45 | } |
50 | 46 |
|
51 | 47 | void root(const uchar4 *in, uchar4 *out, uint32_t x, uint32_t y) { |
52 | 48 | // Convert x and y to floating point coordinates with center as origin |
53 | 49 | const float4 fin = convert_float4(*in); |
54 | | - float2 coord = {(float)x, (float)y}; |
55 | | - coord *= overDimensions; |
56 | | - coord -= center; |
57 | | - const float dist = length(scale * coord); |
58 | | - const float lumen = shade / (1.0 + exp((dist * inv_max_dist - range) * slope)) + (1.0 - shade); |
| 50 | + const float2 inCoord = {(float)x, (float)y}; |
| 51 | + const float2 coord = mad(inCoord, inv_dimensions, neg_center); |
| 52 | + const float sloped_dist_ratio = length(axis_scale * coord) * sloped_inv_max_dist; |
| 53 | + const float lumen = opp_shade + shade / ( 1.0 + sloped_neg_range * exp(sloped_dist_ratio) ); |
59 | 54 | float4 fout; |
60 | 55 | fout.rgb = fin.rgb * lumen; |
61 | 56 | fout.w = fin.w; |
|
0 commit comments