Skip to content

Commit b2e971c

Browse files
committed
Fix sampler offsets (flutter#38170)
* [impeller] dont remap floats * ++ * Update fragment_shader_test.dart * ++: * ++
1 parent 22416c2 commit b2e971c

File tree

9 files changed

+111
-67
lines changed

9 files changed

+111
-67
lines changed

ci/licenses_golden/tool_signature

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
Signature: f6d8146c82d268e2e2549bf5019ebf07
1+
Signature: 9d0f7a4c4f53b80e33da848062bef937
22

impeller/compiler/compiler.cc

+4-17
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ static CompilerBackend CreateMSLCompiler(const spirv_cross::ParsedIR& ir,
4242
// shaders in the flutter/engine tree.
4343
if (source_options.remap_samplers) {
4444
std::vector<uint32_t> sampler_offsets;
45-
std::vector<uint32_t> float_offsets;
4645
ir.for_each_typed_id<spirv_cross::SPIRVariable>(
4746
[&](uint32_t, const spirv_cross::SPIRVariable& var) {
4847
if (var.storage != spv::StorageClassUniformConstant) {
@@ -54,9 +53,6 @@ static CompilerBackend CreateMSLCompiler(const spirv_cross::ParsedIR& ir,
5453
if (spir_type.basetype ==
5554
spirv_cross::SPIRType::BaseType::SampledImage) {
5655
sampler_offsets.push_back(location);
57-
} else if (spir_type.basetype ==
58-
spirv_cross::SPIRType::BaseType::Float) {
59-
float_offsets.push_back(location);
6056
}
6157
});
6258
if (sampler_offsets.size() > 0) {
@@ -68,20 +64,11 @@ static CompilerBackend CreateMSLCompiler(const spirv_cross::ParsedIR& ir,
6864
.basetype = spirv_cross::SPIRType::BaseType::SampledImage,
6965
.binding = offset,
7066
.count = 1u,
67+
// A sampled image is both an image and a sampler, so both
68+
// offsets need to be set or depending on the partiular shader
69+
// the bindings may be incorrect.
7170
.msl_texture = offset - start_offset,
72-
});
73-
}
74-
}
75-
if (float_offsets.size() > 0) {
76-
auto start_offset =
77-
*std::min_element(float_offsets.begin(), float_offsets.end());
78-
for (auto offset : float_offsets) {
79-
sl_compiler->add_msl_resource_binding({
80-
.stage = spv::ExecutionModel::ExecutionModelFragment,
81-
.basetype = spirv_cross::SPIRType::BaseType::Float,
82-
.binding = offset,
83-
.count = 1u,
84-
.msl_buffer = offset - start_offset,
71+
.msl_sampler = offset - start_offset,
8572
});
8673
}
8774
}

impeller/fixtures/ordering/shader_with_matrix.frag

-15
This file was deleted.

impeller/fixtures/ordering/shader_with_ordered_floats.frag

-15
This file was deleted.

lib/ui/fixtures/shaders/BUILD.gn

+1-2
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ if (enable_unittests) {
3636
impellerc("sampler_order_fixture") {
3737
shaders = [
3838
"//flutter/impeller/fixtures/ordering/shader_with_samplers.frag",
39-
"//flutter/impeller/fixtures/ordering/shader_with_ordered_floats.frag",
40-
"//flutter/impeller/fixtures/ordering/shader_with_matrix.frag",
39+
"//flutter/third_party/test_shaders/selman/glow_shader.frag",
4140
]
4241
shader_target_flag = "--runtime-stage-metal"
4342
intermediates_subdir = "iplr-remap"

testing/dart/fragment_shader_test.dart

+4-17
Original file line numberDiff line numberDiff line change
@@ -347,28 +347,15 @@ void main() async {
347347
expect(data, contains(expected));
348348
});
349349

350-
test('impellerc orders floats in metal shader according to declaration and not usage', () async {
350+
test('impellerc orders samplers in metal shader according to declaration and not usage in glow', () async {
351351
if (!Platform.isMacOS) {
352352
return;
353353
}
354354
final Directory directory = shaderDirectory('iplr-remap');
355-
final String data = readAsStringLossy(File(path.join(directory.path, 'shader_with_ordered_floats.frag.iplr')));
355+
final String data = readAsStringLossy(File(path.join(directory.path, 'glow_shader.frag.iplr')));
356356

357-
const String expected = 'constant float& floatA [[buffer(0)]], '
358-
'constant float& floatB [[buffer(1)]]';
359-
360-
expect(data, contains(expected));
361-
});
362-
363-
test('impellerc orders floats/matrix in metal shader according to declaration and not usage', () async {
364-
if (!Platform.isMacOS) {
365-
return;
366-
}
367-
final Directory directory = shaderDirectory('iplr-remap');
368-
final String data = readAsStringLossy(File(path.join(directory.path, 'shader_with_matrix.frag.iplr')));
369-
370-
const String expected = 'constant float4x4& matrix [[buffer(0)]], '
371-
'constant float& floatB [[buffer(1)]]';
357+
const String expected = 'texture2d<float> tInput [[texture(0)]], texture2d<float> tNoise [[texture(1)]], '
358+
'sampler tInputSmplr [[sampler(0)]], sampler tNoiseSmplr [[sampler(1)]]';
372359

373360
expect(data, contains(expected));
374361
});
+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Copyright (c) 2022 by Selman Ay (https://codepen.io/selmanays/pen/yLVmEqY)
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4+
5+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6+
7+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
// Directional glow GLSL fragment shader.
2+
// Based on the "Let it Glow!" pen by "Selman Ay"
3+
// (https://codepen.io/selmanays/pen/yLVmEqY)
4+
precision highp float;
5+
6+
// Float uniforms
7+
uniform float width; // Width of the canvas
8+
uniform float height; // Height of the canvas
9+
uniform float sourceX; // X position of the light source
10+
uniform float
11+
scrollFraction; // Scroll fraction of the page in relation to the canvas
12+
uniform float density; // Density of the "smoke" effect
13+
uniform float lightStrength; // Strength of the light
14+
uniform float weight; // Weight of the "smoke" effect
15+
16+
// Sampler uniforms
17+
uniform sampler2D tInput; // Input texture (the application canvas)
18+
uniform sampler2D tNoise; // Some texture
19+
20+
out vec4 fragColor;
21+
22+
float sourceY = scrollFraction;
23+
vec2 resolution = vec2(width, height);
24+
vec2 lightSource = vec2(sourceX, sourceY);
25+
26+
const int samples = 20; // The number of "copies" of the canvas made to emulate
27+
// the "smoke" effect
28+
const float decay = 0.88; // Decay of the light in each sample
29+
const float exposure = .9; // The exposure to the light
30+
31+
float random2d(vec2 uv) {
32+
uv /= 256.;
33+
vec4 tex = texture(tNoise, uv);
34+
return mix(tex.r, tex.g, tex.a);
35+
}
36+
37+
float random(vec3 xyz) {
38+
return fract(sin(dot(xyz, vec3(12.9898, 78.233, 151.7182))) * 43758.5453);
39+
}
40+
41+
vec4 sampleTexture(vec2 uv) {
42+
vec4 textColor = texture(tInput, uv);
43+
return textColor;
44+
}
45+
46+
vec4 occlusion(vec2 uv, vec2 lightpos, vec4 objects) {
47+
return (1. - smoothstep(0.0, lightStrength, length(lightpos - uv))) *
48+
(objects);
49+
}
50+
51+
vec4 fragment(vec2 uv, vec2 fragCoord) {
52+
vec3 colour = vec3(0);
53+
54+
vec4 obj = sampleTexture(uv);
55+
vec4 map = occlusion(uv, lightSource, obj);
56+
57+
float random = random(vec3(fragCoord, 1.0));
58+
;
59+
60+
float exposure = exposure + (sin(random) * .5 + 1.) * .05;
61+
62+
vec2 _uv = uv;
63+
vec2 distance = (_uv - lightSource) * (1. / float(samples) * density);
64+
65+
float illumination_decay = 1.;
66+
for (int i = 0; i < samples; i++) {
67+
_uv -= distance;
68+
69+
float movement = random * 20. * float(i + 1);
70+
float dither =
71+
random2d(uv +
72+
mod(vec2(movement * sin(random * .5), -movement), 1000.)) *
73+
2.;
74+
75+
vec4 stepped_map =
76+
occlusion(uv, lightSource, sampleTexture(_uv + distance * dither));
77+
stepped_map *= illumination_decay * weight;
78+
79+
illumination_decay *= decay;
80+
map += stepped_map;
81+
}
82+
83+
float lum = dot(map.rgb, vec3(0.2126, 0.7152, 0.0722));
84+
85+
colour += vec3(map.rgb * exposure);
86+
return vec4(colour, lum);
87+
}
88+
89+
void main() {
90+
vec2 pos = gl_FragCoord.xy;
91+
vec2 uv = pos / vec2(width, height);
92+
fragColor = fragment(uv, pos);
93+
}

tools/licenses/lib/main.dart

+1
Original file line numberDiff line numberDiff line change
@@ -1025,6 +1025,7 @@ class _RepositoryDirectory extends _RepositoryEntry implements LicenseSource {
10251025
!entry.fullName.endsWith('third_party/json/docs') &&
10261026
!entry.fullName.endsWith('third_party/ninja') &&
10271027
!entry.fullName.endsWith('third_party/tinygltf') &&
1028+
!entry.fullName.endsWith('third_party/test_shaders') &&
10281029
entry.name != '.ccls-cache' &&
10291030
entry.name != '.cipd' &&
10301031
entry.name != '.git' &&

0 commit comments

Comments
 (0)