19 std::shared_ptr<RuntimeStage> runtime_stage) {
20 runtime_stage_ = std::move(runtime_stage);
24 std::shared_ptr<std::vector<uint8_t>> uniforms) {
25 uniforms_ = std::move(uniforms);
29 std::vector<RuntimeEffectContents::TextureInput> texture_inputs) {
30 texture_inputs_ = std::move(texture_inputs);
34 std::optional<Entity> RuntimeEffectFilterContents::RenderFilter(
38 const Matrix& effect_transform,
40 const std::optional<Rect>& coverage_hint)
const {
45 std::optional<Snapshot> input_snapshot =
46 inputs[0]->GetSnapshot(
"RuntimeEffectContents", renderer, entity);
47 if (!input_snapshot.has_value()) {
51 std::optional<Rect> maybe_input_coverage = input_snapshot->GetCoverage();
52 if (!maybe_input_coverage.has_value()) {
69 if (!input_snapshot->transform.IsIdentity()) {
70 Matrix inverse = input_snapshot->transform.Invert();
77 TextureContents texture_contents;
78 texture_contents.SetTexture(input_snapshot->texture);
79 std::optional<Rect> bounds =
81 if (bounds.has_value()) {
82 texture_contents.SetSourceRect(bounds.value());
83 texture_contents.SetDestinationRect(coverage);
84 texture_contents.SetStencilEnabled(
false);
85 texture_contents.SetSamplerDescriptor(input_snapshot->sampler_descriptor);
90 input_snapshot = texture_contents.RenderToSnapshot(
91 renderer, entity, {.coverage_expansion = 0});
92 if (!input_snapshot.has_value()) {
102 if (texture_inputs_.size() < 1 || uniforms_->size() < 8) {
104 <<
"Invalid fragment shader in RuntimeEffectFilterContents. "
105 <<
"Shader must have at least one sampler and a vec2 size uniform.";
110 std::vector<RuntimeEffectContents::TextureInput> texture_input_copy =
112 texture_input_copy[0].texture = input_snapshot->texture;
114 Size size =
Size(input_snapshot->texture->GetSize());
115 memcpy(uniforms_->data(), &size,
sizeof(
Size));
117 Matrix snapshot_transform = input_snapshot->transform;
122 [snapshot_transform, input_snapshot, runtime_stage = runtime_stage_,
123 uniforms = uniforms_, texture_inputs = texture_input_copy](
124 const ContentContext& renderer,
const Entity& entity,
125 RenderPass& pass) ->
bool {
126 RuntimeEffectContents contents;
127 FillRectGeometry geom(
Rect::MakeSize(input_snapshot->texture->GetSize()));
128 contents.SetRuntimeStage(runtime_stage);
129 contents.SetUniformData(uniforms);
130 contents.SetTextureInputs(texture_inputs);
131 contents.SetGeometry(&geom);
132 Entity offset_entity = entity.Clone();
133 offset_entity.SetTransform(entity.GetTransform() * snapshot_transform);
134 return contents.Render(renderer, offset_entity, pass);
138 [coverage](
const Entity& entity) -> std::optional<Rect> {
145 sub_entity.SetContents(std::move(contents));
146 sub_entity.SetBlendMode(entity.GetBlendMode());
147 sub_entity.SetTransform(input_snapshot->transform *
148 snapshot_transform.Invert());
154 std::optional<Rect> RuntimeEffectFilterContents::GetFilterSourceCoverage(
155 const Matrix& effect_transform,
156 const Rect& output_limit)
const {
static std::shared_ptr< Contents > Make(RenderProc render_proc, CoverageProc coverage_proc)
std::function< std::optional< Rect >(const Entity &entity)> CoverageProc
std::function< bool(const ContentContext &renderer, const Entity &entity, RenderPass &pass)> RenderProc
void SetTextureInputs(std::vector< RuntimeEffectContents::TextureInput > texture_inputs)
void SetUniforms(std::shared_ptr< std::vector< uint8_t >> uniforms)
void SetRuntimeStage(std::shared_ptr< RuntimeStage > runtime_stage)
std::array< Point, 4 > Quad
A 4x4 matrix using column-major storage.
constexpr static std::optional< TRect > MakePointBounds(const U &value)
constexpr TPoint< T > GetLeftBottom() const
constexpr TPoint< T > GetRightTop() const
constexpr static TRect MakeSize(const TSize< U > &size)
constexpr TPoint< T > GetRightBottom() const
constexpr TPoint< T > GetLeftTop() const