Skip to content
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

Update impeller-cmake to ToT #22

Merged
merged 1 commit into from
Jun 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 19 additions & 16 deletions src/examples/mesh/mesh_example.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "impeller/core/buffer_view.h"
#include "impeller/core/device_buffer_descriptor.h"
#include "impeller/core/formats.h"
#include "impeller/core/host_buffer.h"
#include "impeller/core/vertex_buffer.h"
#include "impeller/geometry/matrix.h"
#include "impeller/geometry/scalar.h"
Expand Down Expand Up @@ -43,6 +44,9 @@ ExampleBase::Info MeshExample::GetInfo() {
}

bool MeshExample::Setup(impeller::Context& context) {
transients_buffer_ =
impeller::HostBuffer::Create(context.GetResourceAllocator());

//----------------------------------------------------------------------------
/// Load/unpack the model.
///
Expand All @@ -67,14 +71,9 @@ bool MeshExample::Setup(impeller::Context& context) {
fb::GetMesh(data.data())->UnPackTo(&mesh);

//----------------------------------------------------------------------------
/// Create sampler and load textures.
/// Load textures.
///

impeller::SamplerDescriptor sampler_desc;
sampler_desc.min_filter = impeller::MinMagFilter::kLinear;
sampler_desc.mag_filter = impeller::MinMagFilter::kLinear;
sampler_ = context.GetSamplerLibrary()->GetSampler(sampler_desc);

const auto asset_path = std::filesystem::current_path() / "assets/";

base_color_texture_ =
Expand Down Expand Up @@ -168,6 +167,7 @@ bool MeshExample::Render(impeller::Context& context,
const impeller::RenderTarget& render_target,
impeller::CommandBuffer& command_buffer) {
clock_.Tick();
transients_buffer_->Reset();

static float exposure = 5;
ImGui::SliderFloat("Exposure", &exposure, 0, 15);
Expand All @@ -177,11 +177,10 @@ bool MeshExample::Render(impeller::Context& context,
return false;
}

impeller::Command cmd;
DEBUG_COMMAND_INFO(cmd, "Mesh Example");
cmd.pipeline = pipeline_;
pass->SetCommandLabel("Mesh Example");
pass->SetPipeline(pipeline_);

cmd.BindVertices(vertex_buffer_);
pass->SetVertexBuffer(vertex_buffer_);

auto time = clock_.GetTime();

Expand All @@ -197,19 +196,23 @@ bool MeshExample::Render(impeller::Context& context,
impeller::Matrix::MakeRotationX(
impeller::Radians{std::cos(time * 0.27f) / 4});

VS::BindVertInfo(cmd, pass->GetTransientsBuffer().EmplaceUniform(vs_uniform));
VS::BindVertInfo(*pass, transients_buffer_->EmplaceUniform(vs_uniform));

FS::FragInfo fs_uniform;
fs_uniform.exposure = exposure;
fs_uniform.camera_position = {0, 0, -50};
FS::BindFragInfo(cmd, pass->GetTransientsBuffer().EmplaceUniform(fs_uniform));
FS::BindFragInfo(*pass, transients_buffer_->EmplaceUniform(fs_uniform));

FS::BindBaseColorTexture(cmd, base_color_texture_, sampler_);
FS::BindNormalTexture(cmd, normal_texture_, sampler_);
impeller::SamplerDescriptor sampler_desc;
sampler_desc.min_filter = impeller::MinMagFilter::kLinear;
sampler_desc.mag_filter = impeller::MinMagFilter::kLinear;
const auto& sampler = context.GetSamplerLibrary()->GetSampler(sampler_desc);
FS::BindBaseColorTexture(*pass, base_color_texture_, sampler);
FS::BindNormalTexture(*pass, normal_texture_, sampler);
FS::BindOcclusionRoughnessMetallicTexture(
cmd, occlusion_roughness_metallic_texture_, sampler_);
*pass, occlusion_roughness_metallic_texture_, sampler);

if (!pass->AddCommand(std::move(cmd))) {
if (!pass->Draw().ok()) {
return false;
}

Expand Down
4 changes: 2 additions & 2 deletions src/examples/mesh/mesh_example.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ class MeshExample final : public ExampleBase {
private:
example::Clock clock_;

std::shared_ptr<impeller::HostBuffer> transients_buffer_;

std::shared_ptr<impeller::Pipeline<impeller::PipelineDescriptor>> pipeline_;
impeller::VertexBuffer vertex_buffer_;

std::shared_ptr<const impeller::Sampler> sampler_;

std::shared_ptr<impeller::Texture> base_color_texture_;
std::shared_ptr<impeller::Texture> normal_texture_;
std::shared_ptr<impeller::Texture> occlusion_roughness_metallic_texture_;
Expand Down
39 changes: 22 additions & 17 deletions src/examples/the_impeller/the_impeller_example.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
#include <filesystem>
#include <iostream>

#include "impeller/core/host_buffer.h"
#include "impeller/renderer/command.h"
#include "impeller/renderer/pipeline_library.h"
#include "impeller/renderer/render_pass.h"
#include "impeller/renderer/render_target.h"
#include "impeller/renderer/sampler_library.h"
#include "impeller/renderer/vertex_buffer_builder.h"

#include "examples/assets.h"

Expand All @@ -30,6 +32,9 @@ ExampleBase::Info TheImpellerExample::GetInfo() {
}

bool TheImpellerExample::Setup(impeller::Context& context) {
transients_buffer_ =
impeller::HostBuffer::Create(context.GetResourceAllocator());

const auto fixture_path =
std::filesystem::current_path() /
"third_party/impeller-cmake/third_party/flutter/impeller/fixtures/";
Expand All @@ -43,12 +48,6 @@ bool TheImpellerExample::Setup(impeller::Context& context) {
std::cerr << "Failed to load blue noise texture." << std::endl;
return false;
}
impeller::SamplerDescriptor noise_sampler_desc;
noise_sampler_desc.width_address_mode = impeller::SamplerAddressMode::kRepeat;
noise_sampler_desc.height_address_mode =
impeller::SamplerAddressMode::kRepeat;
blue_noise_sampler_ =
context.GetSamplerLibrary()->GetSampler(noise_sampler_desc);

cube_map_texture_ =
example::LoadTextureCube({fixture_path / "table_mountain_px.png",
Expand All @@ -58,7 +57,6 @@ bool TheImpellerExample::Setup(impeller::Context& context) {
fixture_path / "table_mountain_pz.png",
fixture_path / "table_mountain_nz.png"},
*context.GetResourceAllocator());
cube_map_sampler_ = context.GetSamplerLibrary()->GetSampler({});

auto pipeline_desc =
impeller::PipelineBuilder<VS, FS>::MakeDefaultPipelineDescriptor(context);
Expand All @@ -76,15 +74,15 @@ bool TheImpellerExample::Render(impeller::Context& context,
const impeller::RenderTarget& render_target,
impeller::CommandBuffer& command_buffer) {
clock_.Tick();
transients_buffer_->Reset();

auto pass = command_buffer.CreateRenderPass(render_target);
if (!pass) {
return false;
}

impeller::Command cmd;
DEBUG_COMMAND_INFO(cmd, "Impeller SDF showcase");
cmd.pipeline = pipeline_;
pass->SetCommandLabel("Impeller SDF showcase");
pass->SetPipeline(pipeline_);

auto size = render_target.GetRenderTargetSize();

Expand All @@ -95,21 +93,28 @@ bool TheImpellerExample::Render(impeller::Context& context,
{impeller::Point(size.width, 0)},
{impeller::Point(0, size.height)},
{impeller::Point(size.width, size.height)}});
cmd.BindVertices(builder.CreateVertexBuffer(pass->GetTransientsBuffer()));
pass->SetVertexBuffer(builder.CreateVertexBuffer(*transients_buffer_));

VS::FrameInfo vs_uniform;
vs_uniform.mvp = impeller::Matrix::MakeOrthographic(size);
VS::BindFrameInfo(cmd,
pass->GetTransientsBuffer().EmplaceUniform((vs_uniform)));
VS::BindFrameInfo(*pass, transients_buffer_->EmplaceUniform((vs_uniform)));

FS::FragInfo fs_uniform;
fs_uniform.texture_size = impeller::Point(size);
fs_uniform.time = clock_.GetTime();
FS::BindFragInfo(cmd, pass->GetTransientsBuffer().EmplaceUniform(fs_uniform));
FS::BindBlueNoise(cmd, blue_noise_texture_, blue_noise_sampler_);
FS::BindCubeMap(cmd, cube_map_texture_, cube_map_sampler_);
FS::BindFragInfo(*pass, transients_buffer_->EmplaceUniform(fs_uniform));

impeller::SamplerDescriptor noise_sampler_desc;
noise_sampler_desc.width_address_mode = impeller::SamplerAddressMode::kRepeat;
noise_sampler_desc.height_address_mode =
impeller::SamplerAddressMode::kRepeat;
FS::BindBlueNoise(
*pass, blue_noise_texture_,
context.GetSamplerLibrary()->GetSampler(noise_sampler_desc));
FS::BindCubeMap(*pass, cube_map_texture_,
context.GetSamplerLibrary()->GetSampler({}));

if (!pass->AddCommand(std::move(cmd))) {
if (!pass->Draw().ok()) {
return false;
}

Expand Down
5 changes: 3 additions & 2 deletions src/examples/the_impeller/the_impeller_example.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <memory>

#include "examples/clock.h"
#include "impeller/core/host_buffer.h"
#include "impeller/core/sampler.h"
#include "impeller/core/texture.h"
#include "impeller/renderer/pipeline.h"
Expand All @@ -33,11 +34,11 @@ class TheImpellerExample final : public ExampleBase {
private:
example::Clock clock_;

std::shared_ptr<impeller::HostBuffer> transients_buffer_;

std::shared_ptr<impeller::Texture> blue_noise_texture_;
std::shared_ptr<const impeller::Sampler> blue_noise_sampler_;

std::shared_ptr<impeller::Texture> cube_map_texture_;
std::shared_ptr<const impeller::Sampler> cube_map_sampler_;

std::shared_ptr<impeller::Pipeline<impeller::PipelineDescriptor>> pipeline_;
};
Expand Down
7 changes: 5 additions & 2 deletions src/main_gles.cc
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ int main() {
ImGui::SetNextWindowPos({10, 10});

impeller::Renderer::RenderCallback render_callback =
[&renderer, &examples,
[&context, &renderer, &examples,
&example_names](impeller::RenderTarget& render_target) -> bool {
static int selected_example_index = 1;
auto example = examples[selected_example_index].get();
Expand Down Expand Up @@ -286,7 +286,10 @@ int main() {
}
}

return buffer->SubmitCommands();
// TODO(bdero): GetComandQueue shouldn't be private...
std::shared_ptr<impeller::Context>(context)->GetCommandQueue()->Submit(
{buffer});
return true;
};
renderer->Render(std::move(surface), render_callback);

Expand Down
Loading