20 std::shared_ptr<RuntimeStage> runtime_stage) {
21 runtime_stage_ = std::move(runtime_stage);
25 std::shared_ptr<std::vector<uint8_t>> uniforms) {
26 uniforms_ = std::move(uniforms);
30 std::vector<RuntimeEffectContents::TextureInput> texture_inputs) {
31 texture_inputs_ = std::move(texture_inputs);
35 std::optional<Entity> RuntimeEffectFilterContents::RenderFilter(
39 const Matrix& effect_transform,
41 const std::optional<Rect>& coverage_hint)
const {
46 std::optional<Snapshot> input_snapshot =
47 inputs[0]->GetSnapshot(
"RuntimeEffectContents", renderer, entity);
48 if (!input_snapshot.has_value()) {
52 std::optional<Rect> maybe_input_coverage = input_snapshot->GetCoverage();
53 if (!maybe_input_coverage.has_value()) {
70 if (input_snapshot->ShouldRasterizeForRuntimeEffects()) {
73 Matrix inverse = input_snapshot->transform.Invert();
80 TextureContents texture_contents;
81 texture_contents.SetTexture(input_snapshot->texture);
82 std::optional<Rect> bounds =
84 if (bounds.has_value()) {
85 texture_contents.SetSourceRect(bounds.value());
86 texture_contents.SetDestinationRect(coverage);
87 texture_contents.SetStencilEnabled(
false);
88 texture_contents.SetSamplerDescriptor(input_snapshot->sampler_descriptor);
94 [&texture_contents](
const ContentContext& renderer,
95 const Entity& entity, RenderPass& pass) ->
bool {
96 return texture_contents.Render(renderer, entity, pass);
98 [maybe_input_coverage,
99 entity_offset](
const Entity& entity) -> std::optional<Rect> {
100 Rect coverage = maybe_input_coverage.value();
114 coverage.GetRight(), coverage.GetBottom());
120 input_snapshot = anonymous_contents->RenderToSnapshot(
121 renderer, entity, {.coverage_expansion = 0});
122 if (!input_snapshot.has_value()) {
132 if (texture_inputs_.size() < 1 || uniforms_->size() < 8) {
134 <<
"Invalid fragment shader in RuntimeEffectFilterContents. "
135 <<
"Shader must have at least one sampler and a vec2 size uniform.";
140 std::vector<RuntimeEffectContents::TextureInput> texture_input_copy =
142 texture_input_copy[0].texture = input_snapshot->texture;
144 Size size =
Size(input_snapshot->texture->GetSize());
145 memcpy(uniforms_->data(), &size,
sizeof(
Size));
147 Matrix snapshot_transform = input_snapshot->transform;
152 [snapshot_transform, input_snapshot, runtime_stage = runtime_stage_,
153 uniforms = uniforms_, texture_inputs = texture_input_copy](
154 const ContentContext& renderer,
const Entity& entity,
155 RenderPass& pass) ->
bool {
156 RuntimeEffectContents contents;
157 FillRectGeometry geom(
Rect::MakeSize(input_snapshot->texture->GetSize()));
158 contents.SetRuntimeStage(runtime_stage);
159 contents.SetUniformData(uniforms);
160 contents.SetTextureInputs(texture_inputs);
161 contents.SetGeometry(&geom);
162 Entity offset_entity = entity.Clone();
163 offset_entity.SetTransform(entity.GetTransform() * snapshot_transform);
164 return contents.Render(renderer, offset_entity, pass);
168 [coverage](
const Entity& entity) -> std::optional<Rect> {
175 sub_entity.SetContents(std::move(contents));
176 sub_entity.SetBlendMode(entity.GetBlendMode());
177 sub_entity.SetTransform(input_snapshot->transform *
178 snapshot_transform.Invert());
184 std::optional<Rect> RuntimeEffectFilterContents::GetFilterSourceCoverage(
185 const Matrix& effect_transform,
186 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
const Matrix & GetTransform() const
Get the global transform matrix for this Entity.
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
constexpr static TRect MakeLTRB(Type left, Type top, Type right, Type bottom)