forked from firka/flutter
Adds FragmentProgram.initFromAsset (flutter/engine#34649)
This commit is contained in:
@@ -331,6 +331,7 @@ std::shared_ptr<RuntimeStageData> Reflector::GenerateRuntimeStageData() const {
|
||||
uniform_description.type = spir_type.basetype;
|
||||
uniform_description.rows = spir_type.vecsize;
|
||||
uniform_description.columns = spir_type.columns;
|
||||
uniform_description.bit_width = spir_type.width;
|
||||
data->AddUniformDescription(std::move(uniform_description));
|
||||
});
|
||||
return data;
|
||||
|
||||
@@ -55,11 +55,12 @@ static std::optional<fb::TargetPlatform> ToTargetPlatform(
|
||||
case TargetPlatform::kMetalDesktop:
|
||||
case TargetPlatform::kMetalIOS:
|
||||
case TargetPlatform::kFlutterSPIRV:
|
||||
case TargetPlatform::kSkSL:
|
||||
case TargetPlatform::kOpenGLES:
|
||||
case TargetPlatform::kOpenGLDesktop:
|
||||
case TargetPlatform::kVulkan:
|
||||
return std::nullopt;
|
||||
case TargetPlatform::kSkSL:
|
||||
return fb::TargetPlatform::kSkSL;
|
||||
case TargetPlatform::kRuntimeStageMetal:
|
||||
return fb::TargetPlatform::kMetal;
|
||||
case TargetPlatform::kRuntimeStageGLES:
|
||||
@@ -155,6 +156,7 @@ std::shared_ptr<fml::Mapping> RuntimeStageData::CreateMapping() const {
|
||||
return nullptr;
|
||||
}
|
||||
desc->type = uniform_type.value();
|
||||
desc->bit_width = uniform.bit_width;
|
||||
|
||||
runtime_stage.uniforms.emplace_back(std::move(desc));
|
||||
}
|
||||
|
||||
@@ -21,6 +21,7 @@ struct UniformDescription {
|
||||
spirv_cross::SPIRType::BaseType type = spirv_cross::SPIRType::BaseType::Float;
|
||||
size_t rows = 0u;
|
||||
size_t columns = 0u;
|
||||
size_t bit_width = 0u;
|
||||
};
|
||||
|
||||
class RuntimeStageData {
|
||||
|
||||
@@ -85,7 +85,9 @@ RuntimeStage::RuntimeStage(std::shared_ptr<fml::Mapping> payload)
|
||||
desc.name = i->name()->str();
|
||||
desc.location = i->location();
|
||||
desc.type = ToType(i->type());
|
||||
desc.dimensions = RuntimeUniformDimensions{i->rows(), i->columns()};
|
||||
desc.dimensions = RuntimeUniformDimensions{
|
||||
static_cast<size_t>(i->rows()), static_cast<size_t>(i->columns())};
|
||||
desc.bit_width = i->bit_width();
|
||||
uniforms_.emplace_back(std::move(desc));
|
||||
}
|
||||
|
||||
|
||||
@@ -15,6 +15,7 @@ enum Stage:byte {
|
||||
enum TargetPlatform:byte {
|
||||
kMetal,
|
||||
kOpenGLES,
|
||||
kSkSL,
|
||||
}
|
||||
|
||||
enum UniformDataType:uint32 {
|
||||
@@ -37,6 +38,7 @@ table UniformDescription {
|
||||
name: string;
|
||||
location: uint64;
|
||||
type: UniformDataType;
|
||||
bit_width: uint64;
|
||||
rows: uint64;
|
||||
columns: uint64;
|
||||
}
|
||||
|
||||
@@ -40,8 +40,11 @@ struct RuntimeUniformDescription {
|
||||
size_t location = 0u;
|
||||
RuntimeUniformType type = kFloat;
|
||||
RuntimeUniformDimensions dimensions;
|
||||
size_t bit_width;
|
||||
};
|
||||
|
||||
size_t SizeOfRuntimeUniformType(RuntimeUniformType type);
|
||||
|
||||
class RuntimeStage {
|
||||
public:
|
||||
RuntimeStage(std::shared_ptr<fml::Mapping> payload);
|
||||
|
||||
@@ -21,7 +21,9 @@ if (enable_unittests) {
|
||||
}
|
||||
|
||||
group("test") {
|
||||
testonly = true
|
||||
deps = [
|
||||
":fixtures",
|
||||
":sksl_ink_sparkle",
|
||||
":spirv_ink_sparkle",
|
||||
"//flutter/lib/spirv/test/exception_shaders:spirv_compile_exception_shaders",
|
||||
@@ -43,4 +45,17 @@ if (enable_unittests) {
|
||||
intermediates_subdir = "sksl"
|
||||
sl_file_extension = "sksl"
|
||||
}
|
||||
|
||||
impellerc("iplr_ink_sparkle") {
|
||||
shaders = [ "//flutter/impeller/fixtures/ink_sparkle.frag" ]
|
||||
shader_target_flag = "--sksl"
|
||||
intermediates_subdir = "iplr"
|
||||
sl_file_extension = "iplr"
|
||||
}
|
||||
|
||||
test_fixtures("fixtures") {
|
||||
deps = [ ":iplr_ink_sparkle" ]
|
||||
fixtures = get_target_outputs(":iplr_ink_sparkle")
|
||||
dest = "$root_gen_dir/flutter/lib/ui"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,7 +17,9 @@ if (enable_unittests) {
|
||||
]
|
||||
|
||||
group("general_shaders") {
|
||||
testonly = true
|
||||
deps = [
|
||||
":fixtures",
|
||||
":sksl_compile_general_shaders",
|
||||
":spirv_compile_general_shaders",
|
||||
]
|
||||
@@ -35,4 +37,17 @@ if (enable_unittests) {
|
||||
intermediates_subdir = "sksl"
|
||||
sl_file_extension = "sksl"
|
||||
}
|
||||
|
||||
impellerc("sksl_compile_general_shaders_iplr") {
|
||||
shaders = test_shaders
|
||||
shader_target_flag = "--sksl"
|
||||
intermediates_subdir = "iplr"
|
||||
sl_file_extension = "iplr"
|
||||
}
|
||||
|
||||
test_fixtures("fixtures") {
|
||||
deps = [ ":sksl_compile_general_shaders_iplr" ]
|
||||
fixtures = get_target_outputs(":sksl_compile_general_shaders_iplr")
|
||||
dest = "$root_gen_dir/flutter/lib/ui"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -44,7 +44,9 @@ if (enable_unittests) {
|
||||
]
|
||||
|
||||
group("supported_glsl_op_shaders") {
|
||||
testonly = true
|
||||
deps = [
|
||||
":fixtures",
|
||||
":sksl_compile_supported_glsl_shaders",
|
||||
":spirv_compile_supported_glsl_shaders",
|
||||
]
|
||||
@@ -62,4 +64,17 @@ if (enable_unittests) {
|
||||
intermediates_subdir = "sksl"
|
||||
sl_file_extension = "sksl"
|
||||
}
|
||||
|
||||
impellerc("iplr_compile_supported_glsl_shaders") {
|
||||
shaders = test_shaders
|
||||
shader_target_flag = "--sksl"
|
||||
intermediates_subdir = "iplr"
|
||||
sl_file_extension = "iplr"
|
||||
}
|
||||
|
||||
test_fixtures("fixtures") {
|
||||
deps = [ ":iplr_compile_supported_glsl_shaders" ]
|
||||
fixtures = get_target_outputs(":iplr_compile_supported_glsl_shaders")
|
||||
dest = "$root_gen_dir/flutter/lib/ui"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,7 +40,9 @@ if (enable_unittests) {
|
||||
]
|
||||
|
||||
group("supported_op_shaders") {
|
||||
testonly = true
|
||||
deps = [
|
||||
":fixtures",
|
||||
":sksl_compile_supported_op_shaders",
|
||||
":spirv_compile_supported_op_shaders",
|
||||
]
|
||||
@@ -58,4 +60,17 @@ if (enable_unittests) {
|
||||
intermediates_subdir = "sksl"
|
||||
sl_file_extension = "sksl"
|
||||
}
|
||||
|
||||
impellerc("iplr_compile_supported_op_shaders") {
|
||||
shaders = test_shaders
|
||||
shader_target_flag = "--sksl"
|
||||
intermediates_subdir = "iplr"
|
||||
sl_file_extension = "iplr"
|
||||
}
|
||||
|
||||
test_fixtures("fixtures") {
|
||||
deps = [ ":iplr_compile_supported_op_shaders" ]
|
||||
fixtures = get_target_outputs(":iplr_compile_supported_op_shaders")
|
||||
dest = "$root_gen_dir/flutter/lib/ui"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -148,6 +148,7 @@ source_set("ui") {
|
||||
"//flutter/common",
|
||||
"//flutter/display_list",
|
||||
"//flutter/fml",
|
||||
"//flutter/impeller/runtime_stage",
|
||||
"//flutter/runtime:dart_plugin_registrant",
|
||||
"//flutter/runtime:test_font",
|
||||
"//flutter/third_party/tonic",
|
||||
|
||||
@@ -162,6 +162,7 @@ typedef CanvasPath Path;
|
||||
V(ColorFilter, initSrgbToLinearGamma, 1) \
|
||||
V(EngineLayer, dispose, 1) \
|
||||
V(FragmentProgram, init, 3) \
|
||||
V(FragmentProgram, initFromAsset, 2) \
|
||||
V(FragmentProgram, shader, 4) \
|
||||
V(Gradient, initLinear, 6) \
|
||||
V(Gradient, initRadial, 8) \
|
||||
|
||||
@@ -47,9 +47,9 @@ const Endian _kFakeHostEndian = Endian.little;
|
||||
// A service protocol extension to schedule a frame to be rendered into the
|
||||
// window.
|
||||
Future<developer.ServiceExtensionResponse> _scheduleFrame(
|
||||
String method,
|
||||
Map<String, String> parameters
|
||||
) async {
|
||||
String method,
|
||||
Map<String, String> parameters,
|
||||
) async {
|
||||
// Schedule the frame.
|
||||
PlatformDispatcher.instance.scheduleFrame();
|
||||
// Always succeed.
|
||||
@@ -58,11 +58,32 @@ Future<developer.ServiceExtensionResponse> _scheduleFrame(
|
||||
}));
|
||||
}
|
||||
|
||||
Future<developer.ServiceExtensionResponse> _reinitializeShader(
|
||||
String method,
|
||||
Map<String, String> parameters,
|
||||
) async {
|
||||
final String? assetKey = parameters['assetKey'];
|
||||
if (assetKey != null) {
|
||||
FragmentProgram._reinitializeShader(assetKey);
|
||||
}
|
||||
|
||||
// Always succeed.
|
||||
return developer.ServiceExtensionResponse.result(json.encode(<String, String>{
|
||||
'type': 'Success',
|
||||
}));
|
||||
}
|
||||
|
||||
@pragma('vm:entry-point')
|
||||
void _setupHooks() {
|
||||
assert(() {
|
||||
// In debug mode, register the schedule frame extension.
|
||||
developer.registerExtension('ext.ui.window.scheduleFrame', _scheduleFrame);
|
||||
|
||||
// In debug mode, allow shaders to be reinitialized.
|
||||
developer.registerExtension(
|
||||
'ext.ui.window.reinitializeShader',
|
||||
_reinitializeShader,
|
||||
);
|
||||
return true;
|
||||
}());
|
||||
}
|
||||
|
||||
@@ -4079,6 +4079,26 @@ class FragmentProgram extends NativeFieldWrapperClass1 {
|
||||
));
|
||||
}
|
||||
|
||||
static Map<String, WeakReference<FragmentProgram>> _shaderRegistry =
|
||||
<String, WeakReference<FragmentProgram>>{};
|
||||
|
||||
// TODO(zra): Document custom shaders on the website and add a link to it
|
||||
// here. https://github.com/flutter/flutter/issues/107929.
|
||||
/// Creates a fragment program from the asset with key [assetKey].
|
||||
///
|
||||
/// The asset must be a file produced as the output of the `impellerc`
|
||||
/// compiler. The constructed object should then be reused via the [shader]
|
||||
/// method to create [Shader] objects that can be used by [Shader.paint].
|
||||
static FragmentProgram fromAsset(String assetKey) {
|
||||
FragmentProgram? program = _shaderRegistry[assetKey]?.target;
|
||||
if (program == null) {
|
||||
program = FragmentProgram._fromAsset(assetKey);
|
||||
_shaderRegistry[assetKey] = WeakReference<FragmentProgram>(program);
|
||||
}
|
||||
|
||||
return program;
|
||||
}
|
||||
|
||||
@pragma('vm:entry-point')
|
||||
FragmentProgram._({
|
||||
ByteBuffer? spirv,
|
||||
@@ -4103,6 +4123,31 @@ class FragmentProgram extends NativeFieldWrapperClass1 {
|
||||
}
|
||||
}
|
||||
|
||||
@pragma('vm:entry-point')
|
||||
FragmentProgram._fromAsset(String assetKey) {
|
||||
_constructor();
|
||||
_initFromAsset(assetKey);
|
||||
}
|
||||
|
||||
static void _reinitializeShader(String assetKey) {
|
||||
// If a shader for the assent isn't already registered, then there's no
|
||||
// need to reinitialize it. The new shader will be loaded and initialized
|
||||
// the next time the program access it.
|
||||
final WeakReference<FragmentProgram>? programRef = _shaderRegistry == null
|
||||
? null
|
||||
: _shaderRegistry[assetKey];
|
||||
if (programRef == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
final FragmentProgram? program = programRef.target;
|
||||
if (program == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
program._initFromAsset(assetKey);
|
||||
}
|
||||
|
||||
late final int _uniformFloatCount;
|
||||
late final int _samplerCount;
|
||||
|
||||
@@ -4112,6 +4157,9 @@ class FragmentProgram extends NativeFieldWrapperClass1 {
|
||||
@FfiNative<Void Function(Pointer<Void>, Handle, Bool)>('FragmentProgram::init')
|
||||
external void _init(String sksl, bool debugPrint);
|
||||
|
||||
@FfiNative<Void Function(Pointer<Void>, Handle)>('FragmentProgram::initFromAsset')
|
||||
external void _initFromAsset(String assetKey);
|
||||
|
||||
/// Constructs a [Shader] object suitable for use by [Paint.shader] with
|
||||
/// the given uniforms.
|
||||
///
|
||||
|
||||
@@ -6,8 +6,11 @@
|
||||
|
||||
#include "flutter/lib/ui/painting/fragment_program.h"
|
||||
|
||||
#include "flutter/assets/asset_manager.h"
|
||||
#include "flutter/impeller/runtime_stage/runtime_stage.h"
|
||||
#include "flutter/lib/ui/dart_wrapper.h"
|
||||
#include "flutter/lib/ui/ui_dart_state.h"
|
||||
#include "flutter/lib/ui/window/platform_configuration.h"
|
||||
#include "third_party/skia/include/core/SkString.h"
|
||||
#include "third_party/tonic/converter/dart_converter.h"
|
||||
#include "third_party/tonic/dart_args.h"
|
||||
@@ -19,6 +22,70 @@ namespace flutter {
|
||||
|
||||
IMPLEMENT_WRAPPERTYPEINFO(ui, FragmentProgram);
|
||||
|
||||
void FragmentProgram::initFromAsset(std::string asset_name) {
|
||||
std::shared_ptr<AssetManager> asset_manager = UIDartState::Current()
|
||||
->platform_configuration()
|
||||
->client()
|
||||
->GetAssetManager();
|
||||
std::unique_ptr<fml::Mapping> data = asset_manager->GetAsMapping(asset_name);
|
||||
if (data == nullptr) {
|
||||
Dart_ThrowException(tonic::ToDart("Asset '" + asset_name + "' not found"));
|
||||
return;
|
||||
}
|
||||
|
||||
auto runtime_stage = impeller::RuntimeStage(std::move(data));
|
||||
{
|
||||
auto code_mapping = runtime_stage.GetCodeMapping();
|
||||
auto code_size = code_mapping->GetSize();
|
||||
const char* sksl =
|
||||
reinterpret_cast<const char*>(code_mapping->GetMapping());
|
||||
// SkString makes a copy.
|
||||
SkRuntimeEffect::Result result =
|
||||
SkRuntimeEffect::MakeForShader(SkString(sksl, code_size));
|
||||
if (result.effect == nullptr) {
|
||||
Dart_ThrowException(tonic::ToDart(std::string("Invalid SkSL:\n") + sksl +
|
||||
std::string("\nSkSL Error:\n") +
|
||||
result.errorText.c_str()));
|
||||
return;
|
||||
}
|
||||
runtime_effect_ = result.effect;
|
||||
}
|
||||
|
||||
int sampled_image_count = 0;
|
||||
size_t other_uniforms_bytes = 0;
|
||||
for (const auto& uniform_description : runtime_stage.GetUniforms()) {
|
||||
if (uniform_description.type ==
|
||||
impeller::RuntimeUniformType::kSampledImage) {
|
||||
sampled_image_count++;
|
||||
} else {
|
||||
size_t size = uniform_description.dimensions.rows *
|
||||
uniform_description.dimensions.cols *
|
||||
uniform_description.bit_width / 8u;
|
||||
other_uniforms_bytes += size;
|
||||
}
|
||||
}
|
||||
|
||||
Dart_Handle ths = Dart_HandleFromWeakPersistent(dart_wrapper());
|
||||
if (Dart_IsError(ths)) {
|
||||
Dart_PropagateError(ths);
|
||||
}
|
||||
|
||||
Dart_Handle result = Dart_SetField(ths, tonic::ToDart("_samplerCount"),
|
||||
Dart_NewInteger(sampled_image_count));
|
||||
if (Dart_IsError(result)) {
|
||||
Dart_PropagateError(result);
|
||||
}
|
||||
|
||||
size_t rounded_uniform_bytes =
|
||||
(other_uniforms_bytes + sizeof(float) - 1) & ~(sizeof(float) - 1);
|
||||
size_t float_count = rounded_uniform_bytes / sizeof(float);
|
||||
result = Dart_SetField(ths, tonic::ToDart("_uniformFloatCount"),
|
||||
Dart_NewInteger(float_count));
|
||||
if (Dart_IsError(result)) {
|
||||
Dart_PropagateError(result);
|
||||
}
|
||||
}
|
||||
|
||||
void FragmentProgram::init(std::string sksl, bool debugPrintSksl) {
|
||||
SkRuntimeEffect::Result result =
|
||||
SkRuntimeEffect::MakeForShader(SkString(sksl));
|
||||
|
||||
@@ -24,6 +24,8 @@ class FragmentProgram : public RefCountedDartWrappable<FragmentProgram> {
|
||||
~FragmentProgram() override;
|
||||
static void Create(Dart_Handle wrapper);
|
||||
|
||||
void initFromAsset(std::string asset_name);
|
||||
|
||||
void init(std::string sksl, bool debugPrintSksl);
|
||||
|
||||
fml::RefPtr<FragmentShader> shader(Dart_Handle shader,
|
||||
|
||||
@@ -6,9 +6,9 @@
|
||||
|
||||
#include "flutter/lib/ui/painting/image_decoder_skia.h"
|
||||
|
||||
#if IMPELLER_SUPPORTS_PLATFORM
|
||||
#if IMPELLER_SUPPORTS_RENDERING
|
||||
#include "flutter/lib/ui/painting/image_decoder_impeller.h"
|
||||
#endif // IMPELLER_SUPPORTS_PLATFORM
|
||||
#endif // IMPELLER_SUPPORTS_RENDERING
|
||||
|
||||
namespace flutter {
|
||||
|
||||
@@ -17,7 +17,7 @@ std::unique_ptr<ImageDecoder> ImageDecoder::Make(
|
||||
TaskRunners runners,
|
||||
std::shared_ptr<fml::ConcurrentTaskRunner> concurrent_task_runner,
|
||||
fml::WeakPtr<IOManager> io_manager) {
|
||||
#if IMPELLER_SUPPORTS_PLATFORM
|
||||
#if IMPELLER_SUPPORTS_RENDERING
|
||||
if (settings.enable_impeller) {
|
||||
return std::make_unique<ImageDecoderImpeller>(
|
||||
std::move(runners), //
|
||||
@@ -25,7 +25,7 @@ std::unique_ptr<ImageDecoder> ImageDecoder::Make(
|
||||
std::move(io_manager) //
|
||||
);
|
||||
}
|
||||
#endif // IMPELLER_SUPPORTS_PLATFORM
|
||||
#endif // IMPELLER_SUPPORTS_RENDERING
|
||||
return std::make_unique<ImageDecoderSkia>(
|
||||
std::move(runners), //
|
||||
std::move(concurrent_task_runner), //
|
||||
|
||||
@@ -845,6 +845,10 @@ class FragmentProgram {
|
||||
throw UnsupportedError('FragmentProgram is not supported for the CanvasKit or HTML renderers.');
|
||||
}
|
||||
|
||||
static FragmentProgram fromAsset(String assetKey) {
|
||||
throw UnsupportedError('FragmentProgram is not supported for the CanvasKit or HTML renderers.');
|
||||
}
|
||||
|
||||
FragmentProgram._();
|
||||
|
||||
Shader shader({
|
||||
|
||||
@@ -54,28 +54,9 @@ void main() {
|
||||
_expectShaderRendersGreen(shader);
|
||||
});
|
||||
|
||||
test('shader with functions renders green', () async {
|
||||
final ByteBuffer spirv = shaderFile(
|
||||
path.join('general_shaders', 'spirv'),
|
||||
'functions.frag.spirv',
|
||||
).readAsBytesSync().buffer;
|
||||
final FragmentProgram program = await FragmentProgram.compile(
|
||||
spirv: spirv,
|
||||
);
|
||||
final Shader shader = program.shader(
|
||||
floatUniforms: Float32List.fromList(<double>[1]),
|
||||
);
|
||||
_expectShaderRendersGreen(shader);
|
||||
});
|
||||
|
||||
test('sksl shader with functions renders green', () async {
|
||||
final ByteBuffer sksl = shaderFile(
|
||||
path.join('general_shaders', 'sksl'),
|
||||
'functions.frag.sksl',
|
||||
).readAsBytesSync().buffer;
|
||||
final FragmentProgram program = await FragmentProgram.compile(
|
||||
raw: sksl,
|
||||
uniformFloatCount: 1,
|
||||
test('simple iplr shader renders correctly', () async {
|
||||
final FragmentProgram program = FragmentProgram.fromAsset(
|
||||
'functions.frag.iplr',
|
||||
);
|
||||
final Shader shader = program.shader(
|
||||
floatUniforms: Float32List.fromList(<double>[1]),
|
||||
@@ -123,6 +104,20 @@ void main() {
|
||||
await _expectShaderRendersGreen(shader);
|
||||
});
|
||||
|
||||
test('iplr blue-green image renders green', () async {
|
||||
final FragmentProgram program = FragmentProgram.fromAsset(
|
||||
'blue_green_sampler.frag.iplr',
|
||||
);
|
||||
final Image blueGreenImage = await _createBlueGreenImage();
|
||||
final ImageShader imageShader = ImageShader(
|
||||
blueGreenImage, TileMode.clamp, TileMode.clamp, _identityMatrix);
|
||||
final Shader shader = program.shader(
|
||||
floatUniforms: Float32List.fromList(<double>[]),
|
||||
samplerUniforms: <ImageShader>[imageShader],
|
||||
);
|
||||
await _expectShaderRendersGreen(shader);
|
||||
});
|
||||
|
||||
test('shader with uniforms renders correctly', () async {
|
||||
final Uint8List shaderBytes = await shaderFile(
|
||||
path.join('general_shaders', 'spirv'),
|
||||
@@ -184,6 +179,32 @@ void main() {
|
||||
expect(toFloat(renderedBytes.getUint8(3)), closeTo(1.0, epsilon));
|
||||
});
|
||||
|
||||
test('iplr shader with uniforms renders correctly', () async {
|
||||
final FragmentProgram program = FragmentProgram.fromAsset(
|
||||
'uniforms.frag.iplr',
|
||||
);
|
||||
|
||||
final Shader shader = program.shader(
|
||||
floatUniforms: Float32List.fromList(<double>[
|
||||
0.0, // iFloatUniform
|
||||
0.25, // iVec2Uniform.x
|
||||
0.75, // iVec2Uniform.y
|
||||
0.0, // iMat2Uniform[0][0]
|
||||
0.0, // iMat2Uniform[0][1]
|
||||
0.0, // iMat2Uniform[1][0]
|
||||
1.0, // iMat2Uniform[1][1]
|
||||
]));
|
||||
|
||||
final ByteData renderedBytes = (await _imageByteDataFromShader(
|
||||
shader: shader,
|
||||
))!;
|
||||
|
||||
expect(toFloat(renderedBytes.getUint8(0)), closeTo(0.0, epsilon));
|
||||
expect(toFloat(renderedBytes.getUint8(1)), closeTo(0.25, epsilon));
|
||||
expect(toFloat(renderedBytes.getUint8(2)), closeTo(0.75, epsilon));
|
||||
expect(toFloat(renderedBytes.getUint8(3)), closeTo(1.0, epsilon));
|
||||
});
|
||||
|
||||
test('spirv for the ink_sparkle shader is accepted', () async {
|
||||
final Uint8List spirv = await shaderFile(
|
||||
path.join('spirv'),
|
||||
@@ -244,6 +265,36 @@ void main() {
|
||||
await _expectShaderRendersGreen(shader);
|
||||
});
|
||||
|
||||
test('iplr for the ink_sparkle shader is accepted', () async {
|
||||
final FragmentProgram program = FragmentProgram.fromAsset(
|
||||
'ink_sparkle.frag.iplr',
|
||||
);
|
||||
final Shader shader = program.shader(
|
||||
floatUniforms: Float32List(32),
|
||||
);
|
||||
|
||||
await _imageByteDataFromShader(shader: shader);
|
||||
|
||||
// Testing that no exceptions are thrown. Tests that the ink_sparkle shader
|
||||
// produces the correct pixels are in the framework.
|
||||
});
|
||||
|
||||
test('iplr uniforms are sorted correctly', () async {
|
||||
final FragmentProgram program = FragmentProgram.fromAsset(
|
||||
'uniforms_sorted.frag.iplr',
|
||||
);
|
||||
|
||||
// The shader will not render green if the compiler doesn't keep the
|
||||
// uniforms in the right order.
|
||||
final Shader shader = program.shader(
|
||||
floatUniforms: Float32List.fromList(
|
||||
List<double>.generate(32, (int i) => i.toDouble()),
|
||||
),
|
||||
);
|
||||
|
||||
await _expectShaderRendersGreen(shader);
|
||||
});
|
||||
|
||||
// Test all supported GLSL ops. See lib/spirv/lib/src/constants.dart
|
||||
final Map<String, ByteBuffer> supportedGLSLOpShaders = _loadShaders(
|
||||
path.join('supported_glsl_op_shaders', 'spirv'),
|
||||
@@ -260,6 +311,14 @@ void main() {
|
||||
expect(skslSupportedGLSLOpShaders.isNotEmpty, true);
|
||||
_expectSkSLShadersRenderGreen(skslSupportedGLSLOpShaders);
|
||||
|
||||
|
||||
final Map<String, FragmentProgram> iplrSupportedGLSLOpShaders = _loadShaderAssets(
|
||||
path.join('supported_glsl_op_shaders', 'iplr'),
|
||||
'.iplr',
|
||||
);
|
||||
expect(iplrSupportedGLSLOpShaders.isNotEmpty, true);
|
||||
_expectIplrShadersRenderGreen(iplrSupportedGLSLOpShaders);
|
||||
|
||||
// Test all supported instructions. See lib/spirv/lib/src/constants.dart
|
||||
final Map<String, ByteBuffer> supportedOpShaders = _loadShaders(
|
||||
path.join('supported_op_shaders', 'spirv'),
|
||||
@@ -276,6 +335,13 @@ void main() {
|
||||
expect(skslSupportedOpShaders.isNotEmpty, true);
|
||||
_expectSkSLShadersRenderGreen(skslSupportedOpShaders);
|
||||
|
||||
final Map<String, FragmentProgram> iplrSupportedOpShaders = _loadShaderAssets(
|
||||
path.join('supported_op_shaders', 'iplr'),
|
||||
'.iplr',
|
||||
);
|
||||
expect(iplrSupportedOpShaders.isNotEmpty, true);
|
||||
_expectIplrShadersRenderGreen(iplrSupportedOpShaders);
|
||||
|
||||
test('equality depends on floatUniforms', () async {
|
||||
final ByteBuffer spirv = shaderFile(
|
||||
path.join('general_shaders', 'spirv'),
|
||||
@@ -327,6 +393,28 @@ void main() {
|
||||
}
|
||||
});
|
||||
|
||||
test('iplr equality depends on floatUniforms', () {
|
||||
final FragmentProgram program = FragmentProgram.fromAsset(
|
||||
'simple.frag.iplr',
|
||||
);
|
||||
final Float32List ones = Float32List.fromList(<double>[1]);
|
||||
final Float32List zeroes = Float32List.fromList(<double>[0]);
|
||||
|
||||
{
|
||||
final Shader a = program.shader(floatUniforms: ones);
|
||||
final Shader b = program.shader(floatUniforms: ones);
|
||||
expect(a, b);
|
||||
expect(a.hashCode, b.hashCode);
|
||||
}
|
||||
|
||||
{
|
||||
final Shader a = program.shader(floatUniforms: ones);
|
||||
final Shader b = program.shader(floatUniforms: zeroes);
|
||||
expect(a, notEquals(b));
|
||||
expect(a.hashCode, notEquals(b.hashCode));
|
||||
}
|
||||
});
|
||||
|
||||
test('equality depends on spirv', () async {
|
||||
final ByteBuffer spirvA = shaderFile(
|
||||
path.join('general_shaders', 'spirv'),
|
||||
@@ -369,6 +457,20 @@ void main() {
|
||||
expect(a.hashCode, notEquals(b.hashCode));
|
||||
});
|
||||
|
||||
test('iplr equality depends on data', () async {
|
||||
final FragmentProgram programA = FragmentProgram.fromAsset(
|
||||
'simple.frag.iplr',
|
||||
);
|
||||
final FragmentProgram programB = FragmentProgram.fromAsset(
|
||||
'uniforms.frag.iplr',
|
||||
);
|
||||
final Shader a = programA.shader();
|
||||
final Shader b = programB.shader();
|
||||
|
||||
expect(a, notEquals(b));
|
||||
expect(a.hashCode, notEquals(b.hashCode));
|
||||
});
|
||||
|
||||
test('Compilation does not create a Timer object', () async {
|
||||
final ByteBuffer spirvA = shaderFile(
|
||||
path.join('general_shaders', 'spirv'),
|
||||
@@ -446,6 +548,18 @@ void _expectSkSLShadersRenderGreen(Map<String, ByteBuffer> shaders) {
|
||||
}
|
||||
}
|
||||
|
||||
void _expectIplrShadersRenderGreen(Map<String, FragmentProgram> shaders) {
|
||||
for (final String key in shaders.keys) {
|
||||
test('iplr $key renders green', () async {
|
||||
final FragmentProgram program = shaders[key]!;
|
||||
final Shader shader = program.shader(
|
||||
floatUniforms: Float32List.fromList(<double>[1]),
|
||||
);
|
||||
_expectShaderRendersGreen(shader);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
void _expectShadersHaveOp(Map<String, ByteBuffer> shaders, bool glsl) {
|
||||
for (final String key in shaders.keys) {
|
||||
test('$key contains opcode', () {
|
||||
@@ -540,6 +654,24 @@ Map<String, ByteBuffer> _loadShaders(String leafFolderName, String ext) {
|
||||
return out;
|
||||
}
|
||||
|
||||
Map<String, FragmentProgram> _loadShaderAssets(String leafFolderName, String ext) {
|
||||
final Map<String, FragmentProgram> out = SplayTreeMap<String, FragmentProgram>();
|
||||
|
||||
final Directory directory = shaderDirectory(leafFolderName);
|
||||
if (!directory.existsSync()) {
|
||||
return out;
|
||||
}
|
||||
|
||||
directory
|
||||
.listSync()
|
||||
.where((FileSystemEntity entry) => path.extension(entry.path) == ext)
|
||||
.forEach((FileSystemEntity entry) {
|
||||
final String key = path.basenameWithoutExtension(entry.path);
|
||||
out[key] = FragmentProgram.fromAsset(path.basename(entry.path));
|
||||
});
|
||||
return out;
|
||||
}
|
||||
|
||||
// Arbitrary, but needs to be greater than 1 for frag coord tests.
|
||||
const int _shaderImageDimension = 4;
|
||||
|
||||
@@ -588,4 +720,3 @@ final Float64List _identityMatrix = Float64List.fromList(<double>[
|
||||
0, 0, 1, 0,
|
||||
0, 0, 0, 1,
|
||||
]);
|
||||
|
||||
|
||||
@@ -246,11 +246,20 @@ template("dart_snapshot") {
|
||||
# Arguments:
|
||||
#
|
||||
# fixtures (required): The list of fixtures to copy.
|
||||
#
|
||||
# dest (optional): When specified, the fixtures are placed under an
|
||||
# 'assets' subdirectory of this path rather than an
|
||||
# 'assets' subdirectory of target_gen_dir.
|
||||
template("copy_fixtures") {
|
||||
testonly = true
|
||||
|
||||
assert(defined(invoker.fixtures), "The test fixtures must be specified.")
|
||||
|
||||
dest = target_gen_dir
|
||||
if (defined(invoker.dest)) {
|
||||
dest = invoker.dest
|
||||
}
|
||||
|
||||
has_fixtures = false
|
||||
foreach(fixture, invoker.fixtures) {
|
||||
has_fixtures = true
|
||||
@@ -259,7 +268,7 @@ template("copy_fixtures") {
|
||||
if (has_fixtures) {
|
||||
copy(target_name) {
|
||||
sources = invoker.fixtures
|
||||
outputs = [ "$target_gen_dir/assets/{{source_file_part}}" ]
|
||||
outputs = [ "$dest/assets/{{source_file_part}}" ]
|
||||
forward_variables_from(invoker, [ "deps" ])
|
||||
}
|
||||
} else {
|
||||
@@ -289,8 +298,21 @@ template("copy_fixtures") {
|
||||
# dart_main (optional): The path to the main Dart file. If specified, it is
|
||||
# snapshotted.
|
||||
#
|
||||
# use_target_as_artifact_prefix(optional): If true, adds the target name as prefix of the kernel and AOT ELF snapshot filename.
|
||||
# use_target_as_artifact_prefix(optional): If true, adds the target name as
|
||||
# prefix of the kernel and AOT ELF snapshot filename.
|
||||
#
|
||||
# dest (optional): When specified, the fixtures are placed under an
|
||||
# 'assets' subdirectory of this path rather than an
|
||||
# 'assets' subdirectory of target_gen_dir.
|
||||
template("test_fixtures") {
|
||||
dest = target_gen_dir
|
||||
if (defined(invoker.dest)) {
|
||||
dest = invoker.dest
|
||||
}
|
||||
|
||||
# Not all paths use 'dest'
|
||||
not_needed([ "dest" ])
|
||||
|
||||
# Even if no fixtures are present, the location of the fixtures directory
|
||||
# must always be known to tests.
|
||||
fixtures_location_target_name = "_fl_$target_name"
|
||||
@@ -298,7 +320,7 @@ template("test_fixtures") {
|
||||
if (is_fuchsia) {
|
||||
assets_dir = "/pkg/data/assets"
|
||||
} else {
|
||||
assets_dir = "$target_gen_dir/assets"
|
||||
assets_dir = "$dest/assets"
|
||||
}
|
||||
}
|
||||
test_deps = [ ":$fixtures_location_target_name" ]
|
||||
@@ -309,6 +331,7 @@ template("test_fixtures") {
|
||||
copy_fixtures_target_name = "_cf_$target_name"
|
||||
copy_fixtures(copy_fixtures_target_name) {
|
||||
fixtures = invoker.fixtures
|
||||
dest = dest
|
||||
forward_variables_from(invoker, [ "deps" ])
|
||||
}
|
||||
test_public_deps += [ ":$copy_fixtures_target_name" ]
|
||||
|
||||
Reference in New Issue
Block a user