Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit e2101c4

Browse files
committed
Make runtime effect samplers work
1 parent b262dd9 commit e2101c4

File tree

3 files changed

+83
-11
lines changed

3 files changed

+83
-11
lines changed

impeller/display_list/display_list_dispatcher.cc

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <memory>
1010
#include <optional>
1111
#include <unordered_map>
12+
#include <vector>
1213

1314
#include "display_list/display_list_blend_mode.h"
1415
#include "display_list/display_list_color_filter.h"
@@ -435,11 +436,28 @@ void DisplayListDispatcher::setColorSource(
435436
auto runtime_stage =
436437
runtime_effect_color_source->runtime_effect()->runtime_stage();
437438
auto uniform_data = runtime_effect_color_source->uniform_data();
439+
auto samplers = runtime_effect_color_source->samplers();
438440

439-
paint_.color_source = [runtime_stage, uniform_data]() {
441+
std::vector<RuntimeEffectContents::TextureInput> texture_inputs;
442+
443+
for (auto& sampler : samplers) {
444+
auto* image = sampler->asImage();
445+
if (!sampler->asImage()) {
446+
UNIMPLEMENTED;
447+
return;
448+
}
449+
FML_DCHECK(image->image()->impeller_texture());
450+
texture_inputs.push_back({
451+
.sampler_descriptor = ToSamplerDescriptor(image->sampling()),
452+
.texture = image->image()->impeller_texture(),
453+
});
454+
}
455+
456+
paint_.color_source = [runtime_stage, uniform_data, texture_inputs]() {
440457
auto contents = std::make_shared<RuntimeEffectContents>();
441458
contents->SetRuntimeStage(runtime_stage);
442459
contents->SetUniformData(uniform_data);
460+
contents->SetTextureInputs(texture_inputs);
443461
return contents;
444462
};
445463
return;

impeller/entity/contents/runtime_effect_contents.cc

Lines changed: 54 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "impeller/renderer/formats.h"
1717
#include "impeller/renderer/pipeline_library.h"
1818
#include "impeller/renderer/render_pass.h"
19+
#include "impeller/renderer/sampler_library.h"
1920
#include "impeller/renderer/shader_function.h"
2021
#include "impeller/renderer/shader_types.h"
2122

@@ -31,6 +32,11 @@ void RuntimeEffectContents::SetUniformData(
3132
uniform_data_ = std::move(uniform_data);
3233
}
3334

35+
void RuntimeEffectContents::SetTextureInputs(
36+
std::vector<TextureInput> texture_inputs) {
37+
texture_inputs_ = std::move(texture_inputs);
38+
}
39+
3440
bool RuntimeEffectContents::Render(const ContentContext& renderer,
3541
const Entity& entity,
3642
RenderPass& pass) const {
@@ -136,21 +142,59 @@ bool RuntimeEffectContents::Render(const ContentContext& renderer,
136142
///
137143

138144
size_t buffer_index = 0;
145+
size_t sampler_index = 0;
139146
for (auto uniform : runtime_stage_->GetUniforms()) {
140147
// TODO(113715): Populate this metadata once GLES is able to handle
141148
// non-struct uniform names.
142149
ShaderMetadata metadata;
143150

144-
size_t alignment =
145-
std::max(uniform.bit_width / 8, DefaultUniformAlignment());
146-
auto buffer_view = pass.GetTransientsBuffer().Emplace(
147-
uniform_data_->data() + uniform.location * sizeof(float),
148-
uniform.GetSize(), alignment);
149-
150-
ShaderUniformSlot slot;
151-
slot.name = uniform.name.c_str();
152-
slot.ext_res_0 = buffer_index;
153-
cmd.BindResource(ShaderStage::kFragment, slot, metadata, buffer_view);
151+
switch (uniform.type) {
152+
case kSampledImage: {
153+
FML_DCHECK(sampler_index < texture_inputs_.size());
154+
auto& input = texture_inputs_[sampler_index];
155+
156+
auto sampler =
157+
context->GetSamplerLibrary()->GetSampler(input.sampler_descriptor);
158+
159+
SampledImageSlot image_slot;
160+
image_slot.name = uniform.name.c_str();
161+
image_slot.texture_index = sampler_index;
162+
image_slot.sampler_index = sampler_index;
163+
cmd.BindResource(ShaderStage::kFragment, image_slot, metadata,
164+
input.texture, sampler);
165+
166+
sampler_index++;
167+
break;
168+
}
169+
case kFloat: {
170+
size_t alignment =
171+
std::max(uniform.bit_width / 8, DefaultUniformAlignment());
172+
auto buffer_view = pass.GetTransientsBuffer().Emplace(
173+
uniform_data_->data() + uniform.location * sizeof(float),
174+
uniform.GetSize(), alignment);
175+
176+
ShaderUniformSlot uniform_slot;
177+
uniform_slot.name = uniform.name.c_str();
178+
uniform_slot.ext_res_0 = buffer_index;
179+
cmd.BindResource(ShaderStage::kFragment, uniform_slot, metadata,
180+
buffer_view);
181+
break;
182+
}
183+
case kBoolean:
184+
case kSignedByte:
185+
case kUnsignedByte:
186+
case kSignedShort:
187+
case kUnsignedShort:
188+
case kSignedInt:
189+
case kUnsignedInt:
190+
case kSignedInt64:
191+
case kUnsignedInt64:
192+
case kHalfFloat:
193+
case kDouble:
194+
VALIDATION_LOG << "Unsupported uniform type for " << uniform.name
195+
<< ".";
196+
return true;
197+
}
154198

155199
buffer_index++;
156200
}

impeller/entity/contents/runtime_effect_contents.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,27 @@
44

55
#include <functional>
66
#include <memory>
7+
#include <vector>
78

89
#include "impeller/entity/contents/color_source_contents.h"
10+
#include "impeller/renderer/sampler_descriptor.h"
911
#include "impeller/runtime_stage/runtime_stage.h"
1012

1113
namespace impeller {
1214

1315
class RuntimeEffectContents final : public ColorSourceContents {
1416
public:
17+
struct TextureInput {
18+
SamplerDescriptor sampler_descriptor;
19+
std::shared_ptr<Texture> texture;
20+
};
21+
1522
void SetRuntimeStage(std::shared_ptr<RuntimeStage> runtime_stage);
1623

1724
void SetUniformData(std::shared_ptr<std::vector<uint8_t>> uniform_data);
1825

26+
void SetTextureInputs(std::vector<TextureInput> texture_inputs);
27+
1928
// |Contents|
2029
bool Render(const ContentContext& renderer,
2130
const Entity& entity,
@@ -24,6 +33,7 @@ class RuntimeEffectContents final : public ColorSourceContents {
2433
private:
2534
std::shared_ptr<RuntimeStage> runtime_stage_;
2635
std::shared_ptr<std::vector<uint8_t>> uniform_data_;
36+
std::vector<TextureInput> texture_inputs_;
2737
};
2838

2939
} // namespace impeller

0 commit comments

Comments
 (0)