Reorder GLSL output so that functions are emitted last.

The Inliner likes to move function bodies around; after inlining, code
can inadvertently move upwards, above ProgramElements that the code
relies on. We work around this by always emitting functions last.

Change-Id: Ie5486cc3a79a478920342fb9f578d575486fb4cf
Bug: skia:11186
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/354669
Commit-Queue: John Stiles <johnstiles@google.com>
Auto-Submit: John Stiles <johnstiles@google.com>
Reviewed-by: Brian Osman <brianosman@google.com>
This commit is contained in:
John Stiles 2021-01-15 15:54:02 -05:00 committed by Skia Commit-Bot
parent add35d9474
commit 9d7aa41081
22 changed files with 57 additions and 44 deletions

View File

@ -1574,8 +1574,20 @@ bool GLSLCodeGenerator::generateCode() {
OutputStream* rawOut = fOut;
StringStream body;
fOut = &body;
// Write all the program elements except for functions.
for (const ProgramElement* e : fProgram.elements()) {
this->writeProgramElement(*e);
if (!e->is<FunctionDefinition>()) {
this->writeProgramElement(*e);
}
}
// Write the functions last.
// Why don't we write things in their original order? Because the Inliner likes to move function
// bodies around. After inlining, code can inadvertently move upwards, above ProgramElements
// that the code relies on.
for (const ProgramElement* e : fProgram.elements()) {
if (e->is<FunctionDefinition>()) {
this->writeProgramElement(*e);
}
}
fOut = rawOut;

View File

@ -1,5 +1,7 @@
#version 400
out vec4 sk_FragColor;
in vec4 src;
in vec4 dst;
float _color_burn_component(vec2 s, vec2 d) {
if (d.y == d.x) {
return (s.y * d.y + s.x * (1.0 - d.y)) + d.x * (1.0 - s.y);
@ -12,8 +14,6 @@ float _color_burn_component(vec2 s, vec2 d) {
return (delta * s.y + s.x * (1.0 - d.y)) + d.x * (1.0 - s.y);
}
}
in vec4 src;
in vec4 dst;
void main() {
sk_FragColor = vec4(_color_burn_component(src.xw, dst.xw), _color_burn_component(src.yw, dst.yw), _color_burn_component(src.zw, dst.zw), src.w + (1.0 - src.w) * dst.w);

View File

@ -1,5 +1,7 @@
out vec4 sk_FragColor;
in vec4 src;
in vec4 dst;
float _color_burn_component(vec2 s, vec2 d) {
if (d.y == d.x) {
return (s.y * d.y + s.x * (1.0 - d.y)) + d.x * (1.0 - s.y);
@ -12,8 +14,6 @@ float _color_burn_component(vec2 s, vec2 d) {
return (delta * s.y + s.x * (1.0 - d.y)) + d.x * (1.0 - s.y);
}
}
in vec4 src;
in vec4 dst;
void main() {
sk_FragColor = vec4(_color_burn_component(src.xw, dst.xw), _color_burn_component(src.yw, dst.yw), _color_burn_component(src.zw, dst.zw), src.w + (1.0 - src.w) * dst.w);

View File

@ -1,5 +1,7 @@
#version 400
out vec4 sk_FragColor;
in vec4 src;
in vec4 dst;
float _color_dodge_component(vec2 s, vec2 d) {
if (d.x == 0.0) {
return s.x * (1.0 - d.y);
@ -15,8 +17,6 @@ float _color_dodge_component(vec2 s, vec2 d) {
}
}
}
in vec4 src;
in vec4 dst;
void main() {
sk_FragColor = vec4(_color_dodge_component(src.xw, dst.xw), _color_dodge_component(src.yw, dst.yw), _color_dodge_component(src.zw, dst.zw), src.w + (1.0 - src.w) * dst.w);

View File

@ -1,5 +1,7 @@
out vec4 sk_FragColor;
in vec4 src;
in vec4 dst;
float _color_dodge_component(vec2 s, vec2 d) {
if (d.x == 0.0) {
return s.x * (1.0 - d.y);
@ -15,8 +17,6 @@ float _color_dodge_component(vec2 s, vec2 d) {
}
}
}
in vec4 src;
in vec4 dst;
void main() {
sk_FragColor = vec4(_color_dodge_component(src.xw, dst.xw), _color_dodge_component(src.yw, dst.yw), _color_dodge_component(src.zw, dst.zw), src.w + (1.0 - src.w) * dst.w);

View File

@ -1,10 +1,10 @@
#version 400
out vec4 sk_FragColor;
in vec4 src;
in vec4 dst;
float _blend_overlay_component(vec2 s, vec2 d) {
return 2.0 * d.x <= d.y ? (2.0 * s.x) * d.x : s.y * d.y - (2.0 * (d.y - d.x)) * (s.y - s.x);
}
in vec4 src;
in vec4 dst;
void main() {
vec4 _2_result = vec4(_blend_overlay_component(dst.xw, src.xw), _blend_overlay_component(dst.yw, src.yw), _blend_overlay_component(dst.zw, src.zw), dst.w + (1.0 - dst.w) * src.w);
_2_result.xyz += src.xyz * (1.0 - dst.w) + dst.xyz * (1.0 - src.w);

View File

@ -1,10 +1,10 @@
out vec4 sk_FragColor;
in vec4 src;
in vec4 dst;
float _blend_overlay_component(vec2 s, vec2 d) {
return 2.0 * d.x <= d.y ? (2.0 * s.x) * d.x : s.y * d.y - (2.0 * (d.y - d.x)) * (s.y - s.x);
}
in vec4 src;
in vec4 dst;
void main() {
vec4 _2_result = vec4(_blend_overlay_component(dst.xw, src.xw), _blend_overlay_component(dst.yw, src.yw), _blend_overlay_component(dst.zw, src.zw), dst.w + (1.0 - dst.w) * src.w);
_2_result.xyz += src.xyz * (1.0 - dst.w) + dst.xyz * (1.0 - src.w);

View File

@ -1,10 +1,10 @@
#version 400
out vec4 sk_FragColor;
in vec4 src;
in vec4 dst;
vec3 _blend_set_color_saturation_helper(vec3 minMidMax, float sat) {
return minMidMax.x < minMidMax.z ? vec3(0.0, (sat * (minMidMax.y - minMidMax.x)) / (minMidMax.z - minMidMax.x), sat) : vec3(0.0);
}
in vec4 src;
in vec4 dst;
void main() {
float _1_alpha = dst.w * src.w;
vec3 _2_sda = src.xyz * dst.w;

View File

@ -1,10 +1,10 @@
out vec4 sk_FragColor;
in vec4 src;
in vec4 dst;
vec3 _blend_set_color_saturation_helper(vec3 minMidMax, float sat) {
return minMidMax.x < minMidMax.z ? vec3(0.0, (sat * (minMidMax.y - minMidMax.x)) / (minMidMax.z - minMidMax.x), sat) : vec3(0.0);
}
in vec4 src;
in vec4 dst;
void main() {
float _1_alpha = dst.w * src.w;
vec3 _2_sda = src.xyz * dst.w;

View File

@ -1,10 +1,10 @@
#version 400
out vec4 sk_FragColor;
in vec4 src;
in vec4 dst;
float _blend_overlay_component(vec2 s, vec2 d) {
return 2.0 * d.x <= d.y ? (2.0 * s.x) * d.x : s.y * d.y - (2.0 * (d.y - d.x)) * (s.y - s.x);
}
in vec4 src;
in vec4 dst;
void main() {
vec4 _1_result = vec4(_blend_overlay_component(src.xw, dst.xw), _blend_overlay_component(src.yw, dst.yw), _blend_overlay_component(src.zw, dst.zw), src.w + (1.0 - src.w) * dst.w);
_1_result.xyz += dst.xyz * (1.0 - src.w) + src.xyz * (1.0 - dst.w);

View File

@ -1,10 +1,10 @@
out vec4 sk_FragColor;
in vec4 src;
in vec4 dst;
float _blend_overlay_component(vec2 s, vec2 d) {
return 2.0 * d.x <= d.y ? (2.0 * s.x) * d.x : s.y * d.y - (2.0 * (d.y - d.x)) * (s.y - s.x);
}
in vec4 src;
in vec4 dst;
void main() {
vec4 _1_result = vec4(_blend_overlay_component(src.xw, dst.xw), _blend_overlay_component(src.yw, dst.yw), _blend_overlay_component(src.zw, dst.zw), src.w + (1.0 - src.w) * dst.w);
_1_result.xyz += dst.xyz * (1.0 - src.w) + src.xyz * (1.0 - dst.w);

View File

@ -1,10 +1,10 @@
#version 400
out vec4 sk_FragColor;
in vec4 src;
in vec4 dst;
vec3 _blend_set_color_saturation_helper(vec3 minMidMax, float sat) {
return minMidMax.x < minMidMax.z ? vec3(0.0, (sat * (minMidMax.y - minMidMax.x)) / (minMidMax.z - minMidMax.x), sat) : vec3(0.0);
}
in vec4 src;
in vec4 dst;
void main() {
float _1_alpha = dst.w * src.w;
vec3 _2_sda = src.xyz * dst.w;

View File

@ -1,10 +1,10 @@
out vec4 sk_FragColor;
in vec4 src;
in vec4 dst;
vec3 _blend_set_color_saturation_helper(vec3 minMidMax, float sat) {
return minMidMax.x < minMidMax.z ? vec3(0.0, (sat * (minMidMax.y - minMidMax.x)) / (minMidMax.z - minMidMax.x), sat) : vec3(0.0);
}
in vec4 src;
in vec4 dst;
void main() {
float _1_alpha = dst.w * src.w;
vec3 _2_sda = src.xyz * dst.w;

View File

@ -1,5 +1,7 @@
#version 400
out vec4 sk_FragColor;
in vec4 src;
in vec4 dst;
float _soft_light_component(vec2 s, vec2 d) {
if (2.0 * s.x <= s.y) {
float _8_n = (d.x * d.x) * (s.y - 2.0 * s.x);
@ -17,8 +19,6 @@ float _soft_light_component(vec2 s, vec2 d) {
return ((d.x * ((s.y - 2.0 * s.x) + 1.0) + s.x) - sqrt(d.y * d.x) * (s.y - 2.0 * s.x)) - d.y * s.x;
}
}
in vec4 src;
in vec4 dst;
void main() {
sk_FragColor = dst.w == 0.0 ? src : vec4(_soft_light_component(src.xw, dst.xw), _soft_light_component(src.yw, dst.yw), _soft_light_component(src.zw, dst.zw), src.w + (1.0 - src.w) * dst.w);

View File

@ -1,5 +1,7 @@
out vec4 sk_FragColor;
in vec4 src;
in vec4 dst;
float _soft_light_component(vec2 s, vec2 d) {
if (2.0 * s.x <= s.y) {
float _8_n = (d.x * d.x) * (s.y - 2.0 * s.x);
@ -17,8 +19,6 @@ float _soft_light_component(vec2 s, vec2 d) {
return ((d.x * ((s.y - 2.0 * s.x) + 1.0) + s.x) - sqrt(d.y * d.x) * (s.y - 2.0 * s.x)) - d.y * s.x;
}
}
in vec4 src;
in vec4 dst;
void main() {
sk_FragColor = dst.w == 0.0 ? src : vec4(_soft_light_component(src.xw, dst.xw), _soft_light_component(src.yw, dst.yw), _soft_light_component(src.zw, dst.zw), src.w + (1.0 - src.w) * dst.w);

View File

@ -1,5 +1,6 @@
out vec4 sk_FragColor;
uniform vec4 color;
vec4 blend_src_in(vec4 src, vec4 dst) {
return src * dst.w;
}
@ -47,7 +48,6 @@ vec4 blend_hue(vec4 src, vec4 dst) {
vec3 dsa = dst.xyz * src.w;
return vec4((((_blend_set_color_luminance(_blend_set_color_saturation(sda, dsa), alpha, dsa) + dst.xyz) - dsa) + src.xyz) - sda, (src.w + dst.w) - alpha);
}
uniform vec4 color;
float singleuse() {
return 1.25;
}

View File

@ -1,5 +1,6 @@
out vec4 sk_FragColor;
uniform vec4 color;
vec3 _blend_set_color_luminance(vec3 hueSatColor, float alpha, vec3 lumColor) {
float lum = dot(vec3(0.30000001192092896, 0.5899999737739563, 0.10999999940395355), lumColor);
@ -40,7 +41,6 @@ vec4 blend_hue(vec4 src, vec4 dst) {
vec3 dsa = dst.xyz * src.w;
return vec4((((_blend_set_color_luminance(_blend_set_color_saturation(sda, dsa), alpha, dsa) + dst.xyz) - dsa) + src.xyz) - sda, (src.w + dst.w) - alpha);
}
uniform vec4 color;
void main() {
float _7_a = color.x * color.y;
float _8_c = _7_a + color.z;

View File

@ -1,13 +1,14 @@
out vec4 sk_FragColor;
vec4 helper();
struct Color {
float red;
float green;
float blue;
float alpha;
};
void main() {
struct Color {
float red;
float green;
float blue;
float alpha;
} _1_c;
Color _1_c;
_1_c.red = 0.25;
_1_c.green = 0.5;
_1_c.blue = 0.75;

View File

@ -3,10 +3,11 @@ out vec4 sk_FragColor;
float this_function_is_prototyped_at_the_start_and_never_defined();
vec4 this_function_is_defined_before_use();
vec4 this_function_is_defined_after_use();
bool this_function_is_prototyped_in_the_middle_and_never_defined(mat4 a);
ivec3 this_function_is_prototyped_at_the_very_end_and_never_defined(mat2x3 x, bvec2 y);
vec4 this_function_is_defined_before_use() {
return vec4(1.0);
}
bool this_function_is_prototyped_in_the_middle_and_never_defined(mat4 a);
void main() {
sk_FragColor = this_function_is_defined_before_use();
sk_FragColor = this_function_is_defined_after_use();
@ -14,4 +15,3 @@ void main() {
vec4 this_function_is_defined_after_use() {
return vec4(2.0);
}
ivec3 this_function_is_prototyped_at_the_very_end_and_never_defined(mat2x3 x, bvec2 y);

View File

@ -1,5 +1,6 @@
out vec4 sk_FragColor;
layout (set = 0) uniform vec3 colRGB;
float fn(float v) {
switch (int(v)) {
case 1:
@ -8,7 +9,6 @@ float fn(float v) {
return 3.0;
}
}
layout (set = 0) uniform vec3 colRGB;
void main() {
float v = sqrt(1.0);
sk_FragColor = vec4(v);

View File

@ -1,5 +1,7 @@
out vec4 sk_FragColor;
in vec4 src;
in vec4 dst;
float _color_dodge_component(vec2 s, vec2 d) {
if (d.x == 0.0) {
return s.x * (1.0 - d.y);
@ -44,8 +46,6 @@ float _soft_light_component(vec2 s, vec2 d) {
return ((d.x * ((s.y - 2.0 * s.x) + 1.0) + s.x) - sqrt(d.y * d.x) * (s.y - 2.0 * s.x)) - d.y * s.x;
}
}
in vec4 src;
in vec4 dst;
void main() {
sk_FragColor = vec4(_color_dodge_component(src.xw, dst.xw), _color_dodge_component(src.yw, dst.yw), _color_dodge_component(src.zw, dst.zw), src.w + (1.0 - src.w) * dst.w);

View File

@ -1,5 +1,7 @@
out vec4 sk_FragColor;
in vec4 src;
in vec4 dst;
float _color_dodge_component(vec2 s, vec2 d) {
if (d.x == 0.0) {
return s.x * (1.0 - d.y);
@ -44,8 +46,6 @@ float _soft_light_component(vec2 s, vec2 d) {
return ((d.x * ((s.y - 2.0 * s.x) + 1.0) + s.x) - sqrt(d.y * d.x) * (s.y - 2.0 * s.x)) - d.y * s.x;
}
}
in vec4 src;
in vec4 dst;
void main() {
sk_FragColor = vec4(_color_dodge_component(src.xw, dst.xw), _color_dodge_component(src.yw, dst.yw), _color_dodge_component(src.zw, dst.zw), src.w + (1.0 - src.w) * dst.w);