Skip to content

Commit

Permalink
Fix sampler offsets (#38170)
Browse files Browse the repository at this point in the history
* [impeller] dont remap floats

* ++

* Update fragment_shader_test.dart

* ++:

* ++
  • Loading branch information
jonahwilliams authored Dec 10, 2022
1 parent 3dfd13f commit 690f0e8
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 67 deletions.
2 changes: 1 addition & 1 deletion ci/licenses_golden/tool_signature
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Signature: f6d8146c82d268e2e2549bf5019ebf07
Signature: 9d0f7a4c4f53b80e33da848062bef937

21 changes: 4 additions & 17 deletions impeller/compiler/compiler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ static CompilerBackend CreateMSLCompiler(const spirv_cross::ParsedIR& ir,
// shaders in the flutter/engine tree.
if (source_options.remap_samplers) {
std::vector<uint32_t> sampler_offsets;
std::vector<uint32_t> float_offsets;
ir.for_each_typed_id<spirv_cross::SPIRVariable>(
[&](uint32_t, const spirv_cross::SPIRVariable& var) {
if (var.storage != spv::StorageClassUniformConstant) {
Expand All @@ -54,9 +53,6 @@ static CompilerBackend CreateMSLCompiler(const spirv_cross::ParsedIR& ir,
if (spir_type.basetype ==
spirv_cross::SPIRType::BaseType::SampledImage) {
sampler_offsets.push_back(location);
} else if (spir_type.basetype ==
spirv_cross::SPIRType::BaseType::Float) {
float_offsets.push_back(location);
}
});
if (sampler_offsets.size() > 0) {
Expand All @@ -68,20 +64,11 @@ static CompilerBackend CreateMSLCompiler(const spirv_cross::ParsedIR& ir,
.basetype = spirv_cross::SPIRType::BaseType::SampledImage,
.binding = offset,
.count = 1u,
// A sampled image is both an image and a sampler, so both
// offsets need to be set or depending on the partiular shader
// the bindings may be incorrect.
.msl_texture = offset - start_offset,
});
}
}
if (float_offsets.size() > 0) {
auto start_offset =
*std::min_element(float_offsets.begin(), float_offsets.end());
for (auto offset : float_offsets) {
sl_compiler->add_msl_resource_binding({
.stage = spv::ExecutionModel::ExecutionModelFragment,
.basetype = spirv_cross::SPIRType::BaseType::Float,
.binding = offset,
.count = 1u,
.msl_buffer = offset - start_offset,
.msl_sampler = offset - start_offset,
});
}
}
Expand Down
15 changes: 0 additions & 15 deletions impeller/fixtures/ordering/shader_with_matrix.frag

This file was deleted.

15 changes: 0 additions & 15 deletions impeller/fixtures/ordering/shader_with_ordered_floats.frag

This file was deleted.

3 changes: 1 addition & 2 deletions lib/ui/fixtures/shaders/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ if (enable_unittests) {
impellerc("sampler_order_fixture") {
shaders = [
"//flutter/impeller/fixtures/ordering/shader_with_samplers.frag",
"//flutter/impeller/fixtures/ordering/shader_with_ordered_floats.frag",
"//flutter/impeller/fixtures/ordering/shader_with_matrix.frag",
"//flutter/third_party/test_shaders/selman/glow_shader.frag",
]
shader_target_flag = "--runtime-stage-metal"
intermediates_subdir = "iplr-remap"
Expand Down
21 changes: 4 additions & 17 deletions testing/dart/fragment_shader_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -347,28 +347,15 @@ void main() async {
expect(data, contains(expected));
});

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

const String expected = 'constant float& floatA [[buffer(0)]], '
'constant float& floatB [[buffer(1)]]';

expect(data, contains(expected));
});

test('impellerc orders floats/matrix in metal shader according to declaration and not usage', () async {
if (!Platform.isMacOS) {
return;
}
final Directory directory = shaderDirectory('iplr-remap');
final String data = readAsStringLossy(File(path.join(directory.path, 'shader_with_matrix.frag.iplr')));

const String expected = 'constant float4x4& matrix [[buffer(0)]], '
'constant float& floatB [[buffer(1)]]';
const String expected = 'texture2d<float> tInput [[texture(0)]], texture2d<float> tNoise [[texture(1)]], '
'sampler tInputSmplr [[sampler(0)]], sampler tNoiseSmplr [[sampler(1)]]';

expect(data, contains(expected));
});
Expand Down
7 changes: 7 additions & 0 deletions third_party/test_shaders/selman/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Copyright (c) 2022 by Selman Ay (https://codepen.io/selmanays/pen/yLVmEqY)

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:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

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.
93 changes: 93 additions & 0 deletions third_party/test_shaders/selman/glow_shader.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// Directional glow GLSL fragment shader.
// Based on the "Let it Glow!" pen by "Selman Ay"
// (https://codepen.io/selmanays/pen/yLVmEqY)
precision highp float;

// Float uniforms
uniform float width; // Width of the canvas
uniform float height; // Height of the canvas
uniform float sourceX; // X position of the light source
uniform float
scrollFraction; // Scroll fraction of the page in relation to the canvas
uniform float density; // Density of the "smoke" effect
uniform float lightStrength; // Strength of the light
uniform float weight; // Weight of the "smoke" effect

// Sampler uniforms
uniform sampler2D tInput; // Input texture (the application canvas)
uniform sampler2D tNoise; // Some texture

out vec4 fragColor;

float sourceY = scrollFraction;
vec2 resolution = vec2(width, height);
vec2 lightSource = vec2(sourceX, sourceY);

const int samples = 20; // The number of "copies" of the canvas made to emulate
// the "smoke" effect
const float decay = 0.88; // Decay of the light in each sample
const float exposure = .9; // The exposure to the light

float random2d(vec2 uv) {
uv /= 256.;
vec4 tex = texture(tNoise, uv);
return mix(tex.r, tex.g, tex.a);
}

float random(vec3 xyz) {
return fract(sin(dot(xyz, vec3(12.9898, 78.233, 151.7182))) * 43758.5453);
}

vec4 sampleTexture(vec2 uv) {
vec4 textColor = texture(tInput, uv);
return textColor;
}

vec4 occlusion(vec2 uv, vec2 lightpos, vec4 objects) {
return (1. - smoothstep(0.0, lightStrength, length(lightpos - uv))) *
(objects);
}

vec4 fragment(vec2 uv, vec2 fragCoord) {
vec3 colour = vec3(0);

vec4 obj = sampleTexture(uv);
vec4 map = occlusion(uv, lightSource, obj);

float random = random(vec3(fragCoord, 1.0));
;

float exposure = exposure + (sin(random) * .5 + 1.) * .05;

vec2 _uv = uv;
vec2 distance = (_uv - lightSource) * (1. / float(samples) * density);

float illumination_decay = 1.;
for (int i = 0; i < samples; i++) {
_uv -= distance;

float movement = random * 20. * float(i + 1);
float dither =
random2d(uv +
mod(vec2(movement * sin(random * .5), -movement), 1000.)) *
2.;

vec4 stepped_map =
occlusion(uv, lightSource, sampleTexture(_uv + distance * dither));
stepped_map *= illumination_decay * weight;

illumination_decay *= decay;
map += stepped_map;
}

float lum = dot(map.rgb, vec3(0.2126, 0.7152, 0.0722));

colour += vec3(map.rgb * exposure);
return vec4(colour, lum);
}

void main() {
vec2 pos = gl_FragCoord.xy;
vec2 uv = pos / vec2(width, height);
fragColor = fragment(uv, pos);
}
1 change: 1 addition & 0 deletions tools/licenses/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1025,6 +1025,7 @@ class _RepositoryDirectory extends _RepositoryEntry implements LicenseSource {
!entry.fullName.endsWith('third_party/json/docs') &&
!entry.fullName.endsWith('third_party/ninja') &&
!entry.fullName.endsWith('third_party/tinygltf') &&
!entry.fullName.endsWith('third_party/test_shaders') &&
entry.name != '.ccls-cache' &&
entry.name != '.cipd' &&
entry.name != '.git' &&
Expand Down

0 comments on commit 690f0e8

Please sign in to comment.