Skip to content

Commit

Permalink
[Impeller] Format shader sources. (flutter#37770)
Browse files Browse the repository at this point in the history
* Update format.dart to include glsl, format repo

* format more
  • Loading branch information
dnfield authored Nov 21, 2022
1 parent 55b0891 commit 4a4df4b
Show file tree
Hide file tree
Showing 122 changed files with 496 additions and 566 deletions.
23 changes: 15 additions & 8 deletions ci/bin/format.dart
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ FormatCheck nameToFormatCheck(String name) {
String formatCheckToName(FormatCheck check) {
switch (check) {
case FormatCheck.clang:
return 'C++/ObjC';
return 'C++/ObjC/Shader';
case FormatCheck.gn:
return 'GN';
case FormatCheck.java:
Expand Down Expand Up @@ -301,7 +301,7 @@ abstract class FormatChecker {
}
}

/// Checks and formats C++/ObjC files using clang-format.
/// Checks and formats C++/ObjC/Shader files using clang-format.
class ClangFormatChecker extends FormatChecker {
ClangFormatChecker({
ProcessManager processManager = const LocalProcessManager(),
Expand Down Expand Up @@ -350,7 +350,7 @@ class ClangFormatChecker extends FormatChecker {

@override
Future<bool> fixFormatting() async {
message('Fixing C++/ObjC formatting...');
message('Fixing C++/ObjC/Shader formatting...');
final List<String> failures = await _getCFormatFailures(fixing: true);
if (failures.isEmpty) {
return true;
Expand All @@ -365,7 +365,7 @@ class ClangFormatChecker extends FormatChecker {
}

Future<List<String>> _getCFormatFailures({bool fixing = false}) async {
message('Checking C++/ObjC formatting...');
message('Checking C++/ObjC/Shader formatting...');
const List<String> clangFiletypes = <String>[
'*.c',
'*.cc',
Expand All @@ -374,10 +374,17 @@ class ClangFormatChecker extends FormatChecker {
'*.h',
'*.m',
'*.mm',
'*.glsl',
'*.hlsl',
'*.comp',
'*.tese',
'*.tesc',
'*.vert',
'*.frag',
];
final List<String> files = await getFileList(clangFiletypes);
if (files.isEmpty) {
message('No C++/ObjC files with changes, skipping C++/ObjC format check.');
message('No C++/ObjC/Shader files with changes, skipping C++/ObjC/Shader format check.');
return <String>[];
}
if (verbose) {
Expand Down Expand Up @@ -416,10 +423,10 @@ class ClangFormatChecker extends FormatChecker {
if (failed.isNotEmpty) {
final bool plural = failed.length > 1;
if (fixing) {
message('Fixing ${failed.length} C++/ObjC file${plural ? 's' : ''}'
message('Fixing ${failed.length} C++/ObjC/Shader file${plural ? 's' : ''}'
' which ${plural ? 'were' : 'was'} formatted incorrectly.');
} else {
error('Found ${failed.length} C++/ObjC file${plural ? 's' : ''}'
error('Found ${failed.length} C++/ObjC/Shader file${plural ? 's' : ''}'
' which ${plural ? 'were' : 'was'} formatted incorrectly.');
stdout.writeln('To fix, run:');
stdout.writeln();
Expand All @@ -431,7 +438,7 @@ class ClangFormatChecker extends FormatChecker {
stdout.writeln();
}
} else {
message('Completed checking ${diffJobs.length} C++/ObjC files with no formatting problems.');
message('Completed checking ${diffJobs.length} C++/ObjC/Shader files with no formatting problems.');
}
return failed.map<String>((WorkerJob job) {
return job.result.stdout;
Expand Down
23 changes: 16 additions & 7 deletions impeller/compiler/shader_lib/impeller/texture.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,12 @@ vec4 IPSample(sampler2D texture_sampler, vec2 coords, float y_coord_scale) {
/// If `y_coord_scale` < 0.0, the Y coordinate is flipped. This is useful
/// for Impeller graphics backends that use a flipped framebuffer coordinate
/// space.
/// The range of `coods` will be mapped from [0, 1] to [half_texel, 1 - half_texel]
vec4 IPSampleLinear(sampler2D texture_sampler, vec2 coords, float y_coord_scale, vec2 half_texel) {
/// The range of `coods` will be mapped from [0, 1] to [half_texel, 1 -
/// half_texel]
vec4 IPSampleLinear(sampler2D texture_sampler,
vec2 coords,
float y_coord_scale,
vec2 half_texel) {
coords.x = mix(half_texel.x, 1 - half_texel.x, coords.x);
coords.y = mix(half_texel.y, 1 - half_texel.y, coords.y);
return IPSample(texture_sampler, coords, y_coord_scale);
Expand Down Expand Up @@ -79,7 +83,8 @@ vec4 IPSampleWithTileMode(sampler2D tex,
return vec4(0);
}

return IPSample(tex, IPVec2Tile(coords, x_tile_mode, y_tile_mode), y_coord_scale);
return IPSample(tex, IPVec2Tile(coords, x_tile_mode, y_tile_mode),
y_coord_scale);
}

/// Sample a texture, emulating a specific tile mode.
Expand All @@ -97,7 +102,8 @@ vec4 IPSampleWithTileMode(sampler2D tex,
///
/// This is useful for Impeller graphics backend that don't have native support
/// for Decal.
/// The range of `coods` will be mapped from [0, 1] to [half_texel, 1 - half_texel]
/// The range of `coods` will be mapped from [0, 1] to [half_texel, 1 -
/// half_texel]
vec4 IPSampleLinearWithTileMode(sampler2D tex,
vec2 coords,
float y_coord_scale,
Expand All @@ -109,20 +115,23 @@ vec4 IPSampleLinearWithTileMode(sampler2D tex,
return vec4(0);
}

return IPSampleLinear(tex, IPVec2Tile(coords, x_tile_mode, y_tile_mode), y_coord_scale, half_texel);
return IPSampleLinear(tex, IPVec2Tile(coords, x_tile_mode, y_tile_mode),
y_coord_scale, half_texel);
}

/// Sample a texture, emulating a specific tile mode.
///
/// This is useful for Impeller graphics backend that don't have native support
/// for Decal.
/// The range of `coods` will be mapped from [0, 1] to [half_texel, 1 - half_texel]
/// The range of `coods` will be mapped from [0, 1] to [half_texel, 1 -
/// half_texel]
vec4 IPSampleLinearWithTileMode(sampler2D tex,
vec2 coords,
float y_coord_scale,
vec2 half_texel,
float tile_mode) {
return IPSampleLinearWithTileMode(tex, coords, y_coord_scale, half_texel, tile_mode, tile_mode);
return IPSampleLinearWithTileMode(tex, coords, y_coord_scale, half_texel,
tile_mode, tile_mode);
}

#endif
8 changes: 5 additions & 3 deletions impeller/entity/shaders/blending/advanced_blend.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ uniform BlendInfo {
float src_y_coord_scale;
float color_factor;
vec4 color; // This color input is expected to be unpremultiplied.
} blend_info;
}
blend_info;

uniform sampler2D texture_sampler_dst;
uniform sampler2D texture_sampler_src;
Expand All @@ -28,7 +29,8 @@ void main() {
v_dst_texture_coords, // texture coordinates
blend_info.dst_y_coord_scale, // y coordinate scale
kTileModeDecal // tile mode
) * blend_info.dst_input_alpha;
) *
blend_info.dst_input_alpha;

vec4 dst = IPUnpremultiply(dst_sample);
vec4 src = blend_info.color_factor > 0
Expand All @@ -43,5 +45,5 @@ void main() {
vec4 blended = vec4(Blend(dst.rgb, src.rgb), 1) * dst.a;

frag_color = mix(dst_sample, blended, src.a);
//frag_color = dst_sample;
// frag_color = dst_sample;
}
5 changes: 3 additions & 2 deletions impeller/entity/shaders/blending/blend.frag
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ uniform sampler2D texture_sampler_src;
uniform FragInfo {
float texture_sampler_y_coord_scale;
float input_alpha;
} frag_info;
}
frag_info;

in vec2 v_texture_coords;

Expand All @@ -18,5 +19,5 @@ out vec4 frag_color;
void main() {
frag_color = IPSample(texture_sampler_src, v_texture_coords,
frag_info.texture_sampler_y_coord_scale) *
frag_info.input_alpha;
frag_info.input_alpha;
}
3 changes: 2 additions & 1 deletion impeller/entity/shaders/blending/blend.vert
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

uniform FrameInfo {
mat4 mvp;
} frame_info;
}
frame_info;

in vec2 vertices;
in vec2 texture_coords;
Expand Down
3 changes: 2 additions & 1 deletion impeller/entity/shaders/border_mask_blur.frag
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ uniform sampler2D texture_sampler;

uniform FragInfo {
float texture_sampler_y_coord_scale;
} frag_info;
}
frag_info;

in vec2 v_texture_coords;
in vec2 v_sigma_uv;
Expand Down
14 changes: 8 additions & 6 deletions impeller/entity/shaders/color_matrix_color_filter.frag
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@

// A color filter that transforms colors through a 4x5 color matrix.
//
// This filter can be used to change the saturation of pixels, convert from YUV to RGB, etc.
// This filter can be used to change the saturation of pixels, convert from YUV
// to RGB, etc.
//
// 4x5 matrix for transforming the color and alpha components of a Bitmap.
// The matrix can be passed as single array, and is treated as follows:
Expand All @@ -24,14 +25,16 @@
// B’ = k*R + l*G + m*B + n*A + o;
// A’ = p*R + q*G + r*B + s*A + t;
//
// That resulting color [R’, G’, B’, A’] then has each channel clamped to the 0 to 255 range.
// That resulting color [R’, G’, B’, A’] then has each channel clamped to the 0
// to 255 range.

uniform FragInfo {
mat4 color_m;
vec4 color_v;
float texture_sampler_y_coord_scale;
float input_alpha;
} frag_info;
}
frag_info;

uniform sampler2D input_texture;

Expand All @@ -41,14 +44,13 @@ out vec4 frag_color;
void main() {
vec4 input_color = IPSample(input_texture, v_position,
frag_info.texture_sampler_y_coord_scale) *
frag_info.input_alpha;

frag_info.input_alpha;

// unpremultiply first, as filter inputs are premultiplied.
vec4 color = IPUnpremultiply(input_color);

color = clamp(frag_info.color_m * color + frag_info.color_v, 0.0, 1.0);

// premultiply the outputs
frag_color = vec4(color.rgb * color.a, color.a);
}
3 changes: 2 additions & 1 deletion impeller/entity/shaders/color_matrix_color_filter.vert
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

uniform FrameInfo {
mat4 mvp;
} frame_info;
}
frame_info;

in vec2 position;
out vec2 v_position;
Expand Down
3 changes: 2 additions & 1 deletion impeller/entity/shaders/gradient_fill.vert
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
uniform FrameInfo {
mat4 mvp;
mat4 matrix;
} frame_info;
}
frame_info;

in vec2 position;

Expand Down
20 changes: 9 additions & 11 deletions impeller/entity/shaders/linear_gradient_fill.frag
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,22 @@ uniform GradientInfo {
float texture_sampler_y_coord_scale;
float alpha;
vec2 half_texel;
} gradient_info;
}
gradient_info;

in vec2 v_position;

out vec4 frag_color;

void main() {
float len = length(gradient_info.end_point - gradient_info.start_point);
float dot = dot(
v_position - gradient_info.start_point,
gradient_info.end_point - gradient_info.start_point
);
float dot = dot(v_position - gradient_info.start_point,
gradient_info.end_point - gradient_info.start_point);
float t = dot / (len * len);
frag_color = IPSampleLinearWithTileMode(
texture_sampler,
vec2(t, 0.5),
gradient_info.texture_sampler_y_coord_scale,
gradient_info.half_texel,
gradient_info.tile_mode);
frag_color = vec4(frag_color.xyz * frag_color.a, frag_color.a) * gradient_info.alpha;
texture_sampler, vec2(t, 0.5),
gradient_info.texture_sampler_y_coord_scale, gradient_info.half_texel,
gradient_info.tile_mode);
frag_color =
vec4(frag_color.xyz * frag_color.a, frag_color.a) * gradient_info.alpha;
}
18 changes: 10 additions & 8 deletions impeller/entity/shaders/linear_gradient_ssbo_fill.frag
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,26 @@

readonly buffer ColorData {
vec4 colors[];
} color_data;
}
color_data;

uniform GradientInfo {
vec2 start_point;
vec2 end_point;
float alpha;
float tile_mode;
float colors_length;
} gradient_info;
}
gradient_info;

in vec2 v_position;

out vec4 frag_color;

void main() {
float len = length(gradient_info.end_point - gradient_info.start_point);
float dot = dot(
v_position - gradient_info.start_point,
gradient_info.end_point - gradient_info.start_point
);
float dot = dot(v_position - gradient_info.start_point,
gradient_info.end_point - gradient_info.start_point);
float t = dot / (len * len);

if ((t < 0.0 || t > 1.0) && gradient_info.tile_mode == kTileModeDecal) {
Expand All @@ -36,6 +36,8 @@ void main() {
t = IPFloatTile(t, gradient_info.tile_mode);
vec3 values = IPComputeFixedGradientValues(t, gradient_info.colors_length);

frag_color = mix(color_data.colors[int(values.x)], color_data.colors[int(values.y)], values.z);
frag_color = vec4(frag_color.xyz * frag_color.a, frag_color.a) * gradient_info.alpha;
frag_color = mix(color_data.colors[int(values.x)],
color_data.colors[int(values.y)], values.z);
frag_color =
vec4(frag_color.xyz * frag_color.a, frag_color.a) * gradient_info.alpha;
}
5 changes: 3 additions & 2 deletions impeller/entity/shaders/linear_to_srgb_filter.frag
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,16 @@ uniform sampler2D input_texture;
uniform FragInfo {
float texture_sampler_y_coord_scale;
float input_alpha;
} frag_info;
}
frag_info;

in vec2 v_position;
out vec4 frag_color;

void main() {
vec4 input_color = IPSample(input_texture, v_position,
frag_info.texture_sampler_y_coord_scale) *
frag_info.input_alpha;
frag_info.input_alpha;

vec4 color = IPUnpremultiply(input_color);
for (int i = 0; i < 3; i++) {
Expand Down
3 changes: 2 additions & 1 deletion impeller/entity/shaders/linear_to_srgb_filter.vert
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

uniform FrameInfo {
mat4 mvp;
} frame_info;
}
frame_info;

in vec2 position;
out vec2 v_position;
Expand Down
Loading

0 comments on commit 4a4df4b

Please sign in to comment.