Runtime Effects: Support 'uniform shader' (vs. 'in shader')
The previous behavior leaked Skia-internal concepts into public SkSL.
Users coming from GLSL will expect that bindable/sampleable objects are
uniform (just like texture2D). This keeps the old support around (and
tested), but updates all of our examples to use 'uniform'.
Bug: skia:10679
Change-Id: I0c98162f5e21dad7014d9778ceb26143d2f6030e
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/332376
Commit-Queue: Brian Osman <brianosman@google.com>
Reviewed-by: John Stiles <johnstiles@google.com>
diff --git a/bench/ColorFilterBench.cpp b/bench/ColorFilterBench.cpp
index 8f81c38..202c45d 100644
--- a/bench/ColorFilterBench.cpp
+++ b/bench/ColorFilterBench.cpp
@@ -128,7 +128,7 @@
};
const char RuntimeNone_GPU_SRC[] = R"(
- in shader input;
+ uniform shader input;
half4 main() { return sample(input); }
)";
@@ -138,7 +138,7 @@
m5 , m6 , m7 , m8 , m9 ,
m10, m11, m12, m13, m14,
m15, m16, m17, m18, m19;
- in shader input;
+ uniform shader input;
half4 main() {
half4 c = unpremul(sample(input));
diff --git a/gm/fp_sample_chaining.cpp b/gm/fp_sample_chaining.cpp
index 35a1693..cde56d5 100644
--- a/gm/fp_sample_chaining.cpp
+++ b/gm/fp_sample_chaining.cpp
@@ -277,7 +277,7 @@
}
const char* gConstantMatrixSkSL = R"(
- in shader child;
+ uniform shader child;
half4 main(float2 xy) {
return sample(child, float3x3(0.5, 0.0, 0.0,
0.0, 1.0, 0.0,
@@ -286,7 +286,7 @@
)";
const char* gUniformMatrixSkSL = R"(
- in shader child;
+ uniform shader child;
uniform float3x3 matrix;
half4 main(float2 xy) {
return sample(child, matrix);
@@ -297,7 +297,7 @@
// when scanning for sample matrices. With that pulled into a separate local, it's highly unlikely
// we'll ever treat this as anything else.
const char* gVariableMatrixSkSL = R"(
- in shader child;
+ uniform shader child;
uniform float3x3 matrix;
half4 main(float2 xy) {
float3x3 varMatrix = matrix * 0.5;
@@ -306,7 +306,7 @@
)";
const char* gExplicitCoordSkSL = R"(
- in shader child;
+ uniform shader child;
half4 main(float2 xy) {
return sample(child, xy + float2(0, 8));
}
diff --git a/gm/mixercolorfilter.cpp b/gm/mixercolorfilter.cpp
index d190e69..5d73024 100644
--- a/gm/mixercolorfilter.cpp
+++ b/gm/mixercolorfilter.cpp
@@ -110,8 +110,8 @@
sk_sp<SkRuntimeEffect> effect;
if (fRuntime) {
const char* sksl = R"(
- in shader cf0;
- in shader cf1;
+ uniform shader cf0;
+ uniform shader cf1;
uniform half t;
half4 main() {
return mix(sample(cf0), sample(cf1), t);
diff --git a/gm/runtimecolorfilter.cpp b/gm/runtimecolorfilter.cpp
index 1fe2d52..5857702 100644
--- a/gm/runtimecolorfilter.cpp
+++ b/gm/runtimecolorfilter.cpp
@@ -21,21 +21,21 @@
#include <utility>
const char* gNoop = R"(
- in shader input;
+ uniform shader input;
half4 main() {
return sample(input);
}
)";
const char* gLumaSrc = R"(
- in shader input;
+ uniform shader input;
half4 main() {
return dot(sample(input).rgb, half3(0.3, 0.6, 0.1)).000r;
}
)";
const char* gLumaSrcWithCoords = R"(
- in shader input;
+ uniform shader input;
half4 main(float2 p) {
return dot(sample(input).rgb, half3(0.3, 0.6, 0.1)).000r;
}
@@ -46,7 +46,7 @@
// Simplest to run; hardest to write?
const char* gTernary = R"(
- in shader input;
+ uniform shader input;
half4 main() {
half4 color = sample(input);
half luma = dot(color.rgb, half3(0.3, 0.6, 0.1));
@@ -60,7 +60,7 @@
// Uses conditional if statements but no early return.
const char* gIfs = R"(
- in shader input;
+ uniform shader input;
half4 main() {
half4 color = sample(input);
half luma = dot(color.rgb, half3(0.3, 0.6, 0.1));
@@ -79,7 +79,7 @@
// Distilled from AOSP tone mapping shaders, more like what people tend to write.
const char* gEarlyReturn = R"(
- in shader input;
+ uniform shader input;
half4 main() {
half4 color = sample(input);
half luma = dot(color.rgb, half3(0.3, 0.6, 0.1));
diff --git a/gm/runtimeshader.cpp b/gm/runtimeshader.cpp
index 9200238..b7ca692 100644
--- a/gm/runtimeshader.cpp
+++ b/gm/runtimeshader.cpp
@@ -120,9 +120,9 @@
class ThresholdRT : public RuntimeShaderGM {
public:
ThresholdRT() : RuntimeShaderGM("threshold_rt", {256, 256}, R"(
- in shader before_map;
- in shader after_map;
- in shader threshold_map;
+ uniform shader before_map;
+ uniform shader after_map;
+ uniform shader threshold_map;
uniform float cutoff;
uniform float slope;
@@ -218,8 +218,8 @@
class ColorCubeRT : public RuntimeShaderGM {
public:
ColorCubeRT() : RuntimeShaderGM("color_cube_rt", {512, 512}, R"(
- in shader input;
- in shader color_cube;
+ uniform shader input;
+ uniform shader color_cube;
uniform float rg_scale;
uniform float rg_bias;
@@ -303,7 +303,7 @@
// This test also *explicitly* doesn't include coords in main's parameter list, to test that
// runtime shaders work without them being declared (when they're not used).
DefaultColorRT() : RuntimeShaderGM("default_color_rt", {512, 256}, R"(
- in shader input;
+ uniform shader input;
half4 main() {
return sample(input);
}
diff --git a/gm/vertices.cpp b/gm/vertices.cpp
index 19037fc..8a8110c 100644
--- a/gm/vertices.cpp
+++ b/gm/vertices.cpp
@@ -377,8 +377,8 @@
SkPaint paint;
const char* gProg = R"(
- in shader c0;
- in shader c1;
+ uniform shader c0;
+ uniform shader c1;
varying float vtx_lerp;
half4 main(float2 p) {
half4 col0 = sample(c0, p);
diff --git a/modules/canvaskit/canvaskit/extra.html b/modules/canvaskit/canvaskit/extra.html
index b717c9a..f4367b5 100644
--- a/modules/canvaskit/canvaskit/extra.html
+++ b/modules/canvaskit/canvaskit/extra.html
@@ -333,9 +333,9 @@
}
const prog = `
- in shader before_map;
- in shader after_map;
- in shader threshold_map;
+ uniform shader before_map;
+ uniform shader after_map;
+ uniform shader threshold_map;
uniform float cutoff;
uniform float slope;
@@ -503,8 +503,8 @@
const children = [textureShader, normalShader];
const prog = `
- in shader color_map;
- in shader normal_map;
+ uniform shader color_map;
+ uniform shader normal_map;
uniform float3 lightPos;
layout (marker=local_to_world) uniform float4x4 localToWorld;
diff --git a/modules/canvaskit/tests/rtshader.spec.js b/modules/canvaskit/tests/rtshader.spec.js
index 075b647..b3fec1f 100644
--- a/modules/canvaskit/tests/rtshader.spec.js
+++ b/modules/canvaskit/tests/rtshader.spec.js
@@ -78,9 +78,9 @@
.then((response) => response.arrayBuffer());
const thresholdSkSL = `
-in shader before_map;
-in shader after_map;
-in shader threshold_map;
+uniform shader before_map;
+uniform shader after_map;
+uniform shader threshold_map;
uniform float cutoff;
uniform float slope;
diff --git a/modules/skottie/src/effects/BlackAndWhiteEffect.cpp b/modules/skottie/src/effects/BlackAndWhiteEffect.cpp
index 511dc6d..b84232c 100644
--- a/modules/skottie/src/effects/BlackAndWhiteEffect.cpp
+++ b/modules/skottie/src/effects/BlackAndWhiteEffect.cpp
@@ -34,7 +34,7 @@
static sk_sp<SkRuntimeEffect> make_effect() {
static constexpr char BLACK_AND_WHITE_EFFECT[] = R"(
- in shader input;
+ uniform shader input;
uniform half kR, kY, kG, kC, kB, kM;
half4 main() {
diff --git a/modules/skottie/src/effects/BrightnessContrastEffect.cpp b/modules/skottie/src/effects/BrightnessContrastEffect.cpp
index 76ef37c..5acdb33 100644
--- a/modules/skottie/src/effects/BrightnessContrastEffect.cpp
+++ b/modules/skottie/src/effects/BrightnessContrastEffect.cpp
@@ -73,7 +73,7 @@
uniform half a;
uniform half b;
uniform half c;
- in shader input;
+ uniform shader input;
half4 main() {
// C' = a*C^3 + b*C^2 + c*C
@@ -97,7 +97,7 @@
static constexpr char CONTRAST_EFFECT[] = R"(
uniform half a;
- in shader input;
+ uniform shader input;
half4 main() {
half4 color = sample(input);
@@ -124,7 +124,7 @@
static constexpr char BRIGHTNESS_EFFECT[] = R"(
uniform half a;
- in shader input;
+ uniform shader input;
half4 main() {
half4 color = sample(input);
diff --git a/modules/skottie/src/effects/DisplacementMapEffect.cpp b/modules/skottie/src/effects/DisplacementMapEffect.cpp
index 0a0825e..94091d1 100644
--- a/modules/skottie/src/effects/DisplacementMapEffect.cpp
+++ b/modules/skottie/src/effects/DisplacementMapEffect.cpp
@@ -38,8 +38,8 @@
// |selector_matrix| and |selector_offset| are set up to select and scale the x/y displacement
// in R/G, and the x/y coverage modulation in B/A.
static constexpr char gDisplacementSkSL[] = R"(
- in shader child;
- in shader displ;
+ uniform shader child;
+ uniform shader displ;
uniform half4x4 selector_matrix;
uniform half4 selector_offset;
diff --git a/samplecode/Sample3D.cpp b/samplecode/Sample3D.cpp
index 9d591a8..1c55a3f 100644
--- a/samplecode/Sample3D.cpp
+++ b/samplecode/Sample3D.cpp
@@ -368,8 +368,8 @@
fBmpShader = img->makeShader(SkMatrix::Scale(2, 2));
const char code[] = R"(
- in shader color_map;
- in shader normal_map;
+ uniform shader color_map;
+ uniform shader normal_map;
layout (marker=local_to_world) uniform float4x4 localToWorld;
layout (marker=normals(local_to_world)) uniform float4x4 localToWorldAdjInv;
diff --git a/site/user/modules/canvaskit.md b/site/user/modules/canvaskit.md
index 020e32c..331ef3c 100644
--- a/site/user/modules/canvaskit.md
+++ b/site/user/modules/canvaskit.md
@@ -579,8 +579,8 @@
const children = [textureShader, normalShader];
const prog = `
- in shader color_map;
- in shader normal_map;
+ uniform shader color_map;
+ uniform shader normal_map;
uniform float3 lightPos;
layout (marker=local_to_world) uniform float4x4 localToWorld;
diff --git a/src/effects/SkOverdrawColorFilter.cpp b/src/effects/SkOverdrawColorFilter.cpp
index c539eff..d8e48aa 100644
--- a/src/effects/SkOverdrawColorFilter.cpp
+++ b/src/effects/SkOverdrawColorFilter.cpp
@@ -18,7 +18,7 @@
uniform half4 color3;
uniform half4 color4;
uniform half4 color5;
- in shader input;
+ uniform shader input;
half4 main() {
half4 color = sample(input);
diff --git a/src/sksl/SkSLPipelineStageCodeGenerator.cpp b/src/sksl/SkSLPipelineStageCodeGenerator.cpp
index 171af16..6802932 100644
--- a/src/sksl/SkSLPipelineStageCodeGenerator.cpp
+++ b/src/sksl/SkSLPipelineStageCodeGenerator.cpp
@@ -132,7 +132,10 @@
found = true;
break;
}
- if (var.modifiers().fFlags & flag) {
+ // Skip over fragmentProcessors (shaders).
+ // These are indexed separately from other globals.
+ if (var.modifiers().fFlags & flag &&
+ var.type().nonnullable() != *fContext.fFragmentProcessor_Type) {
++index;
}
}
diff --git a/tests/SkRuntimeEffectTest.cpp b/tests/SkRuntimeEffectTest.cpp
index ed04972..4a815aa 100644
--- a/tests/SkRuntimeEffectTest.cpp
+++ b/tests/SkRuntimeEffectTest.cpp
@@ -62,16 +62,16 @@
test("",
"shader child;",
"must be global");
- test("in shader child; half4 helper(shader fp) { return sample(fp); }",
+ test("uniform shader child; half4 helper(shader fp) { return sample(fp); }",
"half4 color = helper(child);",
"parameter");
- test("in shader child; shader get_child() { return child; }",
+ test("uniform shader child; shader get_child() { return child; }",
"half4 color = sample(get_child());",
"return");
- test("in shader child;",
+ test("uniform shader child;",
"half4 color = sample(shader(child));",
"construct");
- test("in shader child1; in shader child2;",
+ test("uniform shader child1; uniform shader child2;",
"half4 color = sample(p.x > 10 ? child1 : child2);",
"expression");
@@ -232,7 +232,7 @@
//
// Sampling a null child should return the paint color
- effect.build("in shader child;",
+ effect.build("uniform shader child;",
"return sample(child);");
effect.child("child") = nullptr;
effect.test(0xFF00FFFF,
@@ -241,23 +241,29 @@
sk_sp<SkShader> rgbwShader = make_RGBW_shader();
// Sampling a simple child at our coordinates (implicitly)
- effect.build("in shader child;",
+ effect.build("uniform shader child;",
"return sample(child);");
effect.child("child") = rgbwShader;
effect.test(0xFF0000FF, 0xFF00FF00, 0xFFFF0000, 0xFFFFFFFF);
// Sampling with explicit coordinates (reflecting about the diagonal)
- effect.build("in shader child;",
+ effect.build("uniform shader child;",
"return sample(child, p.yx);");
effect.child("child") = rgbwShader;
effect.test(0xFF0000FF, 0xFFFF0000, 0xFF00FF00, 0xFFFFFFFF);
// Sampling with a matrix (again, reflecting about the diagonal)
- effect.build("in shader child;",
+ effect.build("uniform shader child;",
"return sample(child, float3x3(0, 1, 0, 1, 0, 0, 0, 0, 1));");
effect.child("child") = rgbwShader;
effect.test(0xFF0000FF, 0xFFFF0000, 0xFF00FF00, 0xFFFFFFFF);
+ // Legacy behavior - shaders can be declared 'in' rather than 'uniform'
+ effect.build("in shader child;",
+ "return sample(child);");
+ effect.child("child") = rgbwShader;
+ effect.test(0xFF0000FF, 0xFF00FF00, 0xFFFF0000, 0xFFFFFFFF);
+
//
// Helper functions
//
diff --git a/tools/viewer/SkSLSlide.cpp b/tools/viewer/SkSLSlide.cpp
index 7572bec..7e422f2 100644
--- a/tools/viewer/SkSLSlide.cpp
+++ b/tools/viewer/SkSLSlide.cpp
@@ -38,7 +38,7 @@
fSkSL =
- "in shader child;\n"
+ "uniform shader child;\n"
"\n"
"half4 main(float2 p) {\n"
" return sample(child, p);\n"