Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
[Flutter GPU] Add shader bundle format.
  • Loading branch information
bdero committed Dec 11, 2023
commit e3f8cbff9cf65f2f16558138cce80692abc93d53
1 change: 1 addition & 0 deletions impeller/compiler/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ impeller_component("compiler_lib") {
"../base",
"../geometry",
"../runtime_stage",
"../shader_bundle",
"//flutter/fml",

# All third_party deps must be included by the global license script.
Expand Down
16 changes: 14 additions & 2 deletions impeller/runtime_stage/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,25 @@ import("../tools/impeller.gni")

config("runtime_stage_config") {
configs = [ "//flutter/impeller:impeller_public_config" ]
include_dirs = [ "$root_gen_dir/flutter" ]
include_dirs = [
"$root_gen_dir/flutter",
"$root_gen_dir/flutter/impeller/runtime_stage",
]
}

flatbuffers("runtime_stage_types_flatbuffers") {
flatbuffers = [ "runtime_stage_types.fbs" ]
public_configs = [ ":runtime_stage_config" ]
public_deps = [ "//flutter/third_party/flatbuffers" ]
}

flatbuffers("runtime_stage_flatbuffers") {
flatbuffers = [ "runtime_stage.fbs" ]
public_configs = [ ":runtime_stage_config" ]
public_deps = [ "//flutter/third_party/flatbuffers" ]
public_deps = [
":runtime_stage_types_flatbuffers",
"//flutter/third_party/flatbuffers",
]
}

impeller_component("runtime_stage") {
Expand Down
9 changes: 8 additions & 1 deletion impeller/runtime_stage/runtime_stage.cc
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,14 @@ RuntimeStage::RuntimeStage(std::shared_ptr<fml::Mapping> payload)
if (!fb::RuntimeStageBufferHasIdentifier(payload_->GetMapping())) {
return;
}
auto runtime_stage = fb::GetRuntimeStage(payload_->GetMapping());
Setup(fb::GetRuntimeStage(payload_->GetMapping()));
}

RuntimeStage::RuntimeStage(const fb::RuntimeStage* runtime_stage) {
Setup(runtime_stage);
}

void RuntimeStage::Setup(const fb::RuntimeStage* runtime_stage) {
if (!runtime_stage) {
return;
}
Expand Down
50 changes: 2 additions & 48 deletions impeller/runtime_stage/runtime_stage.fbs
Original file line number Diff line number Diff line change
Expand Up @@ -2,55 +2,9 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

namespace impeller.fb;

enum Stage:byte {
kVertex,
kFragment,
kCompute,
}

enum TargetPlatform:byte {
kMetal,
kOpenGLES,
kSkSL,
kVulkan,
}
include "runtime_stage_types.fbs";

enum UniformDataType:uint32 {
kBoolean,
kSignedByte,
kUnsignedByte,
kSignedShort,
kUnsignedShort,
kSignedInt,
kUnsignedInt,
kSignedInt64,
kUnsignedInt64,
kHalfFloat,
kFloat,
kDouble,
kSampledImage,
}

table UniformDescription {
name: string;
location: uint64;
type: UniformDataType;
bit_width: uint64;
rows: uint64;
columns: uint64;
array_elements: uint64;
}

table RuntimeStage {
stage: Stage;
target_platform: TargetPlatform;
entrypoint: string;
uniforms: [UniformDescription];
shader: [ubyte];
sksl: [ubyte];
}
namespace impeller.fb;

root_type RuntimeStage;
file_identifier "IPLR";
5 changes: 5 additions & 0 deletions impeller/runtime_stage/runtime_stage.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@
#include "flutter/fml/mapping.h"

#include "flutter/impeller/core/runtime_types.h"
#include "runtime_stage_types_flatbuffers.h"

namespace impeller {

class RuntimeStage {
public:
explicit RuntimeStage(std::shared_ptr<fml::Mapping> payload);

explicit RuntimeStage(const fb::RuntimeStage* runtime_stage);

~RuntimeStage();
RuntimeStage(RuntimeStage&&);
RuntimeStage& operator=(RuntimeStage&&);
Expand Down Expand Up @@ -50,6 +53,8 @@ class RuntimeStage {
bool is_valid_ = false;
bool is_dirty_ = true;

void Setup(const fb::RuntimeStage* runtime_stage);

RuntimeStage(const RuntimeStage&) = delete;

RuntimeStage& operator=(const RuntimeStage&) = delete;
Expand Down
53 changes: 53 additions & 0 deletions impeller/runtime_stage/runtime_stage_types.fbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

namespace impeller.fb;

enum Stage:byte {
kVertex,
kFragment,
kCompute,
}

enum TargetPlatform:byte {
kMetal,
kOpenGLES,
kSkSL,
kVulkan,
}

enum UniformDataType:uint32 {
kBoolean,
kSignedByte,
kUnsignedByte,
kSignedShort,
kUnsignedShort,
kSignedInt,
kUnsignedInt,
kSignedInt64,
kUnsignedInt64,
kHalfFloat,
kFloat,
kDouble,
kSampledImage,
}

table UniformDescription {
name: string;
location: uint64;
type: UniformDataType;
bit_width: uint64;
rows: uint64;
columns: uint64;
array_elements: uint64;
}

table RuntimeStage {
stage: Stage;
target_platform: TargetPlatform;
entrypoint: string;
uniforms: [UniformDescription];
shader: [ubyte];
sksl: [ubyte];
}
32 changes: 32 additions & 0 deletions impeller/shader_bundle/BUILD.gn
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Copyright 2013 The Flutter Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import("//flutter/third_party/flatbuffers/flatbuffers.gni")
import("../tools/impeller.gni")

flatbuffers("shader_bundle_flatbuffers") {
flatbuffers = [ "shader_bundle.fbs" ]
public_configs = [ "//flutter/impeller/runtime_stage:runtime_stage_config" ]
public_deps = [
"//flutter/impeller/runtime_stage:runtime_stage_flatbuffers",
"//flutter/impeller/runtime_stage:runtime_stage_types_flatbuffers",
"//flutter/third_party/flatbuffers",
]
}

impeller_component("shader_bundle") {
sources = [
"shader.cc",
"shader.h",
"shader_bundle.cc",
"shader_bundle.h",
]
public_deps = [
":shader_bundle_flatbuffers",
"../base",
"../core",
"//flutter/fml",
"//flutter/impeller/runtime_stage:runtime_stage",
]
}
21 changes: 21 additions & 0 deletions impeller/shader_bundle/shader.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "impeller/shader_bundle/shader.h"

namespace impeller {

Shader::Shader() = default;

Shader::Shader(const fb::Shader* shader)
: runtime_stage_(
std::make_shared<RuntimeStage>(RuntimeStage(shader->shader()))) {
is_valid_ = runtime_stage_->IsValid();
}

bool Shader::IsValid() const {
return is_valid_;
}

} // namespace impeller
36 changes: 36 additions & 0 deletions impeller/shader_bundle/shader.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#pragma once

#include <map>
#include <memory>

#include "flutter/fml/mapping.h"
#include "impeller/runtime_stage/runtime_stage.h"
#include "impeller/shader_bundle/shader_bundle_flatbuffers.h"

namespace impeller {

class ShaderBundle;

class Shader {
public:
// Note: Default constructor and copy operations required for map usage in
// ShaderBundle.
Shader();

bool IsValid() const;

private:
bool is_valid_ = false;

std::shared_ptr<RuntimeStage> runtime_stage_;

explicit Shader(const fb::Shader* shader);

friend ShaderBundle;
};

} // namespace impeller
40 changes: 40 additions & 0 deletions impeller/shader_bundle/shader_bundle.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "impeller/shader_bundle/shader_bundle.h"

#include "impeller/shader_bundle/shader_bundle_flatbuffers.h"

namespace impeller {

ShaderBundle::ShaderBundle(std::shared_ptr<fml::Mapping> payload)
: payload_(std::move(payload)) {
if (payload_ == nullptr || !payload_->GetMapping()) {
return;
}
if (!fb::ShaderBundleBufferHasIdentifier(payload_->GetMapping())) {
return;
}
auto shader_bundle = fb::GetShaderBundle(payload_->GetMapping());
if (!shader_bundle) {
return;
}

auto* shaders = shader_bundle->shaders();
for (size_t i = 0; i < shaders->size(); i++) {
const fb::Shader* shader = shaders->Get(i);
shaders_[shader->name()->str()] = Shader(shader);
if (!shaders_[shader->name()->str()].IsValid()) {
return;
}
}

is_valid_ = true;
}

bool ShaderBundle::IsValid() const {
return is_valid_;
}

} // namespace impeller
19 changes: 19 additions & 0 deletions impeller/shader_bundle/shader_bundle.fbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

include "../runtime_stage/runtime_stage_types.fbs";

namespace impeller.fb;

table Shader {
name: string;
shader: RuntimeStage;
}

table ShaderBundle {
shaders: [Shader];
}

root_type ShaderBundle;
file_identifier "IPSB";
35 changes: 35 additions & 0 deletions impeller/shader_bundle/shader_bundle.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#pragma once

#include <map>
#include <memory>

#include "flutter/fml/mapping.h"
#include "impeller/runtime_stage/runtime_stage.h"
#include "impeller/shader_bundle/shader.h"

namespace impeller {

class ShaderBundle {
public:
explicit ShaderBundle(std::shared_ptr<fml::Mapping> payload);

~ShaderBundle();
ShaderBundle(ShaderBundle&&);

bool IsValid() const;

private:
bool is_valid_;

std::shared_ptr<fml::Mapping> payload_;
std::map<std::string, Shader> shaders_;

ShaderBundle(const ShaderBundle&) = delete;
ShaderBundle& operator=(const ShaderBundle&) = delete;
};

} // namespace impeller