-
Notifications
You must be signed in to change notification settings - Fork 6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[impeller] drawAtlas #35009
Merged
auto-submit
merged 8 commits into
flutter:main
from
jonahwilliams:add_draw_atlas_impeller
Aug 2, 2022
Merged
[impeller] drawAtlas #35009
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
320cb32
[impeller] drawAtlas
jonahwilliams a3b31d6
everything broke lol
jonahwilliams 4a69912
Actually use atlas shader
jonahwilliams 42792c6
no colors
jonahwilliams d29db5f
remove coverage computation for now
jonahwilliams 7132185
update licenses
jonahwilliams de84926
implement coverage, fix transform calculation, remove count, remove i…
jonahwilliams 0563130
rename attributes use default ctr
jonahwilliams File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
// 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 <optional> | ||
|
||
#include "impeller/renderer/formats.h" | ||
#include "impeller/renderer/sampler_library.h" | ||
#include "impeller/renderer/vertex_buffer_builder.h" | ||
|
||
#include "impeller/entity/atlas_fill.frag.h" | ||
#include "impeller/entity/atlas_fill.vert.h" | ||
#include "impeller/entity/contents/atlas_contents.h" | ||
#include "impeller/entity/contents/content_context.h" | ||
#include "impeller/entity/entity.h" | ||
#include "impeller/renderer/render_pass.h" | ||
|
||
namespace impeller { | ||
|
||
AtlasContents::AtlasContents() = default; | ||
|
||
AtlasContents::~AtlasContents() = default; | ||
|
||
void AtlasContents::SetTexture(std::shared_ptr<Texture> texture) { | ||
texture_ = std::move(texture); | ||
} | ||
|
||
std::shared_ptr<Texture> AtlasContents::GetTexture() const { | ||
return texture_; | ||
} | ||
|
||
void AtlasContents::SetTransforms(std::vector<Matrix> transforms) { | ||
transforms_ = transforms; | ||
} | ||
|
||
void AtlasContents::SetTextureCoordinates(std::vector<Rect> texture_coords) { | ||
texture_coords_ = texture_coords; | ||
} | ||
|
||
void AtlasContents::SetColors(std::vector<Color> colors) { | ||
colors_ = colors; | ||
} | ||
|
||
void AtlasContents::SetBlendMode(Entity::BlendMode blend_mode) { | ||
// TODO(jonahwilliams): blending of colors with texture. | ||
blend_mode_ = blend_mode; | ||
} | ||
|
||
std::optional<Rect> AtlasContents::GetCoverage(const Entity& entity) const { | ||
Rect bounding_box = {}; | ||
for (size_t i = 0; i < texture_coords_.size(); i++) { | ||
auto matrix = transforms_[i]; | ||
auto sample_rect = texture_coords_[i]; | ||
auto bounds = Rect::MakeSize(sample_rect.size).TransformBounds(matrix); | ||
bounding_box = bounds.Union(bounding_box); | ||
} | ||
return bounding_box.TransformBounds(entity.GetTransformation()); | ||
} | ||
|
||
void AtlasContents::SetSamplerDescriptor(SamplerDescriptor desc) { | ||
sampler_descriptor_ = std::move(desc); | ||
} | ||
|
||
const SamplerDescriptor& AtlasContents::GetSamplerDescriptor() const { | ||
return sampler_descriptor_; | ||
} | ||
|
||
bool AtlasContents::Render(const ContentContext& renderer, | ||
const Entity& entity, | ||
RenderPass& pass) const { | ||
if (texture_ == nullptr) { | ||
return true; | ||
} | ||
|
||
using VS = AtlasFillVertexShader; | ||
using FS = AtlasFillFragmentShader; | ||
|
||
const auto texture_size = texture_->GetSize(); | ||
if (texture_size.IsEmpty()) { | ||
return true; | ||
} | ||
|
||
VertexBufferBuilder<VS::PerVertexData> vertex_builder; | ||
vertex_builder.Reserve(texture_coords_.size() * 6); | ||
constexpr size_t indices[6] = {0, 1, 2, 1, 2, 3}; | ||
constexpr Scalar width[6] = {0, 1, 0, 1, 0, 1}; | ||
constexpr Scalar height[6] = {0, 0, 1, 0, 1, 1}; | ||
for (size_t i = 0; i < texture_coords_.size(); i++) { | ||
auto sample_rect = texture_coords_[i]; | ||
auto matrix = transforms_[i]; | ||
auto color = (colors_.size() > 0 ? colors_[i] : Color::Black()); | ||
auto transformed_points = | ||
Rect::MakeSize(sample_rect.size).GetTransformedPoints(matrix); | ||
|
||
for (size_t j = 0; j < 6; j++) { | ||
VS::PerVertexData data; | ||
data.position = transformed_points[indices[j]]; | ||
data.texture_coords = | ||
(sample_rect.origin + Point(sample_rect.size.width * width[j], | ||
sample_rect.size.height * height[j])) / | ||
texture_size; | ||
data.color = color.Premultiply(); | ||
vertex_builder.AppendVertex(data); | ||
} | ||
} | ||
|
||
if (!vertex_builder.HasVertices()) { | ||
return true; | ||
} | ||
|
||
auto& host_buffer = pass.GetTransientsBuffer(); | ||
|
||
VS::VertInfo vert_info; | ||
vert_info.mvp = Matrix::MakeOrthographic(pass.GetRenderTargetSize()) * | ||
entity.GetTransformation(); | ||
|
||
FS::FragInfo frag_info; | ||
frag_info.texture_sampler_y_coord_scale = texture_->GetYCoordScale(); | ||
frag_info.has_color = colors_.size() > 0 ? 1.0 : 0.0; | ||
|
||
Command cmd; | ||
cmd.label = "DrawAtlas"; | ||
cmd.pipeline = | ||
renderer.GetAtlasPipeline(OptionsFromPassAndEntity(pass, entity)); | ||
cmd.stencil_reference = entity.GetStencilDepth(); | ||
cmd.BindVertices(vertex_builder.CreateVertexBuffer(host_buffer)); | ||
VS::BindVertInfo(cmd, host_buffer.EmplaceUniform(vert_info)); | ||
FS::BindFragInfo(cmd, host_buffer.EmplaceUniform(frag_info)); | ||
FS::BindTextureSampler(cmd, texture_, | ||
renderer.GetContext()->GetSamplerLibrary()->GetSampler( | ||
sampler_descriptor_)); | ||
pass.AddCommand(std::move(cmd)); | ||
|
||
return true; | ||
} | ||
|
||
} // namespace impeller |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// 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 <functional> | ||
#include <memory> | ||
#include <vector> | ||
|
||
#include "flutter/fml/macros.h" | ||
#include "impeller/entity/contents/contents.h" | ||
#include "impeller/entity/entity.h" | ||
#include "impeller/renderer/sampler_descriptor.h" | ||
|
||
namespace impeller { | ||
|
||
class AtlasContents final : public Contents { | ||
public: | ||
explicit AtlasContents(); | ||
|
||
~AtlasContents() override; | ||
|
||
void SetTexture(std::shared_ptr<Texture> texture); | ||
|
||
std::shared_ptr<Texture> GetTexture() const; | ||
|
||
void SetTransforms(std::vector<Matrix> transforms); | ||
|
||
void SetBlendMode(Entity::BlendMode blend_mode); | ||
|
||
void SetTextureCoordinates(std::vector<Rect> texture_coords); | ||
|
||
void SetColors(std::vector<Color> colors); | ||
|
||
void SetSamplerDescriptor(SamplerDescriptor desc); | ||
|
||
const SamplerDescriptor& GetSamplerDescriptor() const; | ||
|
||
// |Contents| | ||
std::optional<Rect> GetCoverage(const Entity& entity) const override; | ||
|
||
// |Contents| | ||
bool Render(const ContentContext& renderer, | ||
const Entity& entity, | ||
RenderPass& pass) const override; | ||
|
||
private: | ||
std::shared_ptr<Texture> texture_; | ||
std::vector<Rect> texture_coords_; | ||
std::vector<Color> colors_; | ||
std::vector<Matrix> transforms_; | ||
Entity::BlendMode blend_mode_; | ||
SamplerDescriptor sampler_descriptor_ = {}; | ||
|
||
FML_DISALLOW_COPY_AND_ASSIGN(AtlasContents); | ||
}; | ||
|
||
} // namespace impeller |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we need to incorporate this cull rect at some point, but we can do this as follow-up work. It can probably be done by either surrounding DrawAtlas with DrawClip/Restore calls or doing clipping tricks while building the draw call + coverage itself.
May be worth inserting a TODO nearby.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added a todo is aiks canvas.