Generate reflection info for samplers and images.

This commit is contained in:
Chinmay Garde
2021-06-30 12:39:44 -07:00
committed by Dan Field
parent f86c10efca
commit 13c5f46edc
11 changed files with 135 additions and 22 deletions

View File

@@ -71,6 +71,21 @@ struct {{camel_case(shader_name)}}{{camel_case(shader_stage)}}Info {
{% endfor %}
};
// ===========================================================================
// Sampled Images ============================================================
// ===========================================================================
{% for sampled_image in sampled_images %}
static constexpr auto kInput{{camel_case(sampled_image.name)}} = ShaderStageIOSlot { // {{sampled_image.name}}
"{{sampled_image.name}}", // name
{{sampled_image.location}}u, // attribute location
{{sampled_image.type.type_name}}, // type
{{sampled_image.type.bit_width}}u, // bit width of type
{{sampled_image.type.vec_size}}u, // vec size
{{sampled_image.type.columns}}u // number of columns
};
{% endfor %}
// ===========================================================================
// Stage Outputs =============================================================
// ===========================================================================

View File

@@ -180,6 +180,27 @@ std::optional<nlohmann::json> Reflector::GenerateTemplateArguments() const {
return std::nullopt;
}
{
auto combined_sampled_images =
ReflectResources(shader_resources.sampled_images);
auto images = ReflectResources(shader_resources.separate_images);
auto samplers = ReflectResources(shader_resources.separate_samplers);
if (!combined_sampled_images.has_value() || !images.has_value() ||
!samplers.has_value()) {
return std::nullopt;
}
auto& stage_inputs = root["sampled_images"] = nlohmann::json::array_t{};
for (auto value : combined_sampled_images.value()) {
stage_inputs.emplace_back(std::move(value));
}
for (auto value : images.value()) {
stage_inputs.emplace_back(std::move(value));
}
for (auto value : samplers.value()) {
stage_inputs.emplace_back(std::move(value));
}
}
if (auto stage_outputs = ReflectResources(shader_resources.stage_outputs);
stage_outputs.has_value()) {
root["stage_outputs"] = std::move(stage_outputs.value());

View File

@@ -20,8 +20,8 @@ class Sampler;
struct Bindings {
std::map<size_t, BufferView> buffers;
std::map<size_t, std::shared_ptr<Texture>> textures;
std::map<size_t, std::shared_ptr<Sampler>> samplers;
std::map<size_t, std::shared_ptr<const Texture>> textures;
std::map<size_t, std::shared_ptr<const Sampler>> samplers;
};
struct Command {

View File

@@ -9,6 +9,7 @@
#include "impeller/base/base.h"
#include "impeller/compositor/device_buffer.h"
#include "impeller/compositor/formats_metal.h"
#include "impeller/compositor/sampler.h"
#include "impeller/shader_glue/shader_types.h"
namespace impeller {
@@ -195,23 +196,71 @@ static bool Bind(id<MTLRenderCommandEncoder> pass,
return false;
}
[pass setVertexBuffer:buffer offset:view.range.offset atIndex:bind_index];
return true;
}
switch (stage) {
case ShaderStage::kVertex:
[pass setVertexBuffer:buffer offset:view.range.offset atIndex:bind_index];
return true;
case ShaderStage::kFragment:
[pass setFragmentBuffer:buffer
offset:view.range.offset
atIndex:bind_index];
return true;
default:
FML_DLOG(ERROR) << "Cannot bind buffer to unknown shader stage.";
return false;
}
static bool Bind(Allocator& allocator,
ShaderStage stage,
size_t bind_index,
const Texture& view) {
FML_CHECK(false);
return false;
}
static bool Bind(Allocator& allocator,
static bool Bind(id<MTLRenderCommandEncoder> pass,
Allocator& allocator,
ShaderStage stage,
size_t bind_index,
const Sampler& view) {
FML_CHECK(false);
const Texture& texture) {
if (!texture.IsValid()) {
return false;
}
switch (stage) {
case ShaderStage::kVertex:
[pass setVertexTexture:texture.GetMTLTexture() atIndex:bind_index];
return true;
case ShaderStage::kFragment:
[pass setFragmentTexture:texture.GetMTLTexture() atIndex:bind_index];
return true;
default:
FML_DLOG(ERROR) << "Cannot bind buffer to unknown shader stage.";
return false;
}
return false;
}
static bool Bind(id<MTLRenderCommandEncoder> pass,
Allocator& allocator,
ShaderStage stage,
size_t bind_index,
const Sampler& sampler) {
if (!sampler.IsValid()) {
return false;
}
switch (stage) {
case ShaderStage::kVertex:
[pass setVertexSamplerState:sampler.GetMTLSamplerState()
atIndex:bind_index];
return true;
case ShaderStage::kFragment:
[pass setFragmentSamplerState:sampler.GetMTLSamplerState()
atIndex:bind_index];
return true;
default:
FML_DLOG(ERROR) << "Cannot bind buffer to unknown shader stage.";
return false;
}
return false;
}
@@ -228,12 +277,12 @@ bool RenderPass::EncodeCommands(Allocator& allocator,
}
}
for (const auto texture : bindings.textures) {
if (!Bind(allocator, stage, texture.first, *texture.second)) {
if (!Bind(pass, allocator, stage, texture.first, *texture.second)) {
return false;
}
}
for (const auto sampler : bindings.samplers) {
if (!Bind(allocator, stage, sampler.first, *sampler.second)) {
if (!Bind(pass, allocator, stage, sampler.first, *sampler.second)) {
return false;
}
}

View File

@@ -18,6 +18,8 @@ class Sampler {
~Sampler();
id<MTLSamplerState> GetMTLSamplerState() const;
private:
friend SamplerLibrary;

View File

@@ -14,4 +14,8 @@ bool Sampler::IsValid() const {
return state_;
}
id<MTLSamplerState> Sampler::GetMTLSamplerState() const {
return state_;
}
} // namespace impeller

View File

@@ -17,6 +17,8 @@ class Texture {
~Texture();
bool IsValid() const;
Size GetSize() const;
id<MTLTexture> GetMTLTexture() const;

View File

@@ -19,4 +19,8 @@ id<MTLTexture> Texture::GetMTLTexture() const {
return texture_;
}
bool Texture::IsValid() const {
return texture_;
}
} // namespace impeller

View File

@@ -6,6 +6,7 @@
#include "flutter/fml/time/time_point.h"
#include "impeller/compositor/command.h"
#include "impeller/compositor/sampler_descriptor.h"
#include "impeller/compositor/surface.h"
#include "impeller/compositor/vertex_buffer_builder.h"
#include "impeller/primitives/box.frag.h"
@@ -67,11 +68,23 @@ bool EntityRenderer::OnRender(const Surface& surface, RenderPass& pass) {
Command cmd;
cmd.label = "Box";
cmd.pipeline = box_primitive_->GetPipeline();
cmd.vertex_bindings.buffers[box_primitive_->GetVertexBufferIndex()] =
vertex_buffer_.vertex_buffer;
cmd.vertex_bindings
.buffers[shader::BoxVertexInfo::kUniformUniformBuffer.binding] =
pass.GetTransientsBuffer().EmplaceUniform(uniforms);
cmd.fragment_bindings
.samplers[shader::BoxFragmentInfo::kInputContents1.location] =
GetContext()->GetSamplerLibrary()->GetSampler({});
cmd.fragment_bindings
.samplers[shader::BoxFragmentInfo::kInputContents2.location] =
GetContext()->GetSamplerLibrary()->GetSampler({});
cmd.fragment_bindings
.samplers[shader::BoxFragmentInfo::kInputContents3.location] =
GetContext()->GetSamplerLibrary()->GetSampler({});
cmd.index_buffer = vertex_buffer_.index_buffer;
cmd.index_count = vertex_buffer_.index_count;
cmd.primitive_type = PrimitiveType::kTriange;

View File

@@ -5,15 +5,15 @@
in vec4 color;
in vec2 interpolated_texture_coordinates;
uniform sampler2D contents;
// uniform sampler2D texture_sampler2;
// uniform sampler2D texture_sampler3;
uniform sampler2D contents1;
uniform sampler2D contents2;
uniform sampler2D contents3;
out vec4 frag_color;
void main() {
vec4 color1 = texture(contents, interpolated_texture_coordinates);
// vec4 color2 = texture(texture_sampler2, interpolated_texture_coordinates);
// vec4 color3 = texture(texture_sampler3, interpolated_texture_coordinates);
frag_color = color1;
vec4 color1 = texture(contents1, interpolated_texture_coordinates);
vec4 color2 = texture(contents2, interpolated_texture_coordinates);
vec4 color3 = texture(contents3, interpolated_texture_coordinates);
frag_color = color3;
}

View File

@@ -41,6 +41,9 @@ def Main():
# debugging but should be removed from release builds.
"-MO",
"-gline-tables-only",
# These warnings are from generated code and would make no sense to the GLSL
# author.
"-Wno-unused-variable",
# Both user and system header will be tracked.
"-MMD",
"-MF",