| Flutter Impeller |

scene_context.cc
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "impeller/scene/scene_context.h"
6 #include "impeller/core/formats.h"
7 #include "impeller/core/host_buffer.h"
8 #include "impeller/scene/material.h"
9 #include "impeller/scene/shaders/skinned.vert.h"
10 #include "impeller/scene/shaders/unlit.frag.h"
11 #include "impeller/scene/shaders/unskinned.vert.h"
12
13 namespace impeller {
14 namespace scene {
15
16 void SceneContextOptions::ApplyToPipelineDescriptor(
17 const Capabilities& capabilities,
18 PipelineDescriptor& desc) const {
19 DepthAttachmentDescriptor depth;
20 depth.depth_compare = CompareFunction::kLess;
21 depth.depth_write_enabled = true;
22 desc.SetDepthStencilAttachmentDescriptor(depth);
23 desc.SetDepthPixelFormat(capabilities.GetDefaultDepthStencilFormat());
24
25 StencilAttachmentDescriptor stencil;
26 stencil.stencil_compare = CompareFunction::kAlways;
27 stencil.depth_stencil_pass = StencilOperation::kKeep;
28 desc.SetStencilAttachmentDescriptors(stencil);
29 desc.SetStencilPixelFormat(capabilities.GetDefaultDepthStencilFormat());
30
31 desc.SetSampleCount(sample_count);
32 desc.SetPrimitiveType(primitive_type);
33
34 desc.SetWindingOrder(WindingOrder::kCounterClockwise);
35 desc.SetCullMode(CullMode::kBackFace);
36 }
37
38 SceneContext::SceneContext(std::shared_ptr<Context> context)
39 : context_(std::move(context)) {
40 if (!context_ || !context_->IsValid()) {
41 return;
42 }
43
44 auto unskinned_variant =
45 MakePipelineVariants<UnskinnedVertexShader, UnlitFragmentShader>(
46 *context_);
47 if (!unskinned_variant) {
48 FML_LOG(ERROR) << "Could not create unskinned pipeline variant.";
49 return;
50 }
51 pipelines_[{PipelineKey{GeometryType::kUnskinned, MaterialType::kUnlit}}] =
52 std::move(unskinned_variant);
53
54 auto skinned_variant =
55 MakePipelineVariants<SkinnedVertexShader, UnlitFragmentShader>(*context_);
56 if (!skinned_variant) {
57 FML_LOG(ERROR) << "Could not create skinned pipeline variant.";
58 return;
59 }
60 pipelines_[{PipelineKey{GeometryType::kSkinned, MaterialType::kUnlit}}] =
61 std::move(skinned_variant);
62
63 {
64 impeller::TextureDescriptor texture_descriptor;
65 texture_descriptor.storage_mode = impeller::StorageMode::kHostVisible;
66 texture_descriptor.format = PixelFormat::kR8G8B8A8UNormInt;
67 texture_descriptor.size = {1, 1};
68 texture_descriptor.mip_count = 1u;
69
70 placeholder_texture_ =
71 context_->GetResourceAllocator()->CreateTexture(texture_descriptor);
72 placeholder_texture_->SetLabel("Placeholder Texture");
73 if (!placeholder_texture_) {
74 FML_LOG(ERROR) << "Could not create placeholder texture.";
75 return;
76 }
77
78 uint8_t pixel[] = {0xFF, 0xFF, 0xFF, 0xFF};
79 if (!placeholder_texture_->SetContents(pixel, 4)) {
80 FML_LOG(ERROR) << "Could not set contents of placeholder texture.";
81 return;
82 }
83 }
84 host_buffer_ = HostBuffer::Create(GetContext()->GetResourceAllocator());
85 is_valid_ = true;
86 }
87
88 SceneContext::~SceneContext() = default;
89
90 std::shared_ptr<Pipeline<PipelineDescriptor>> SceneContext::GetPipeline(
91 PipelineKey key,
92 SceneContextOptions opts) const {
93 if (!IsValid()) {
94 return nullptr;
95 }
96 if (auto found = pipelines_.find(key); found != pipelines_.end()) {
97 return found->second->GetPipeline(*context_, opts);
98 }
99 return nullptr;
100 }
101
102 bool SceneContext::IsValid() const {
103 return is_valid_;
104 }
105
106 std::shared_ptr<Context> SceneContext::GetContext() const {
107 return context_;
108 }
109
110 std::shared_ptr<Texture> SceneContext::GetPlaceholderTexture() const {
111 return placeholder_texture_;
112 }
113
114 } // namespace scene
115 } // namespace impeller
impeller::PipelineDescriptor
Definition: pipeline_descriptor.h:24
host_buffer.h
scene_context.h
impeller::StencilAttachmentDescriptor::stencil_compare
CompareFunction stencil_compare
Definition: formats.h:598
impeller::scene::SceneContextOptions::sample_count
SampleCount sample_count
Definition: scene_context.h:20
impeller::PipelineDescriptor::SetStencilAttachmentDescriptors
PipelineDescriptor & SetStencilAttachmentDescriptors(std::optional< StencilAttachmentDescriptor > front_and_back)
Definition: pipeline_descriptor.cc:157
impeller::scene::GeometryType::kUnskinned
@ kUnskinned
impeller::scene::SceneContext::SceneContext
SceneContext(std::shared_ptr< Context > context)
Definition: scene_context.cc:38
impeller::PipelineDescriptor::SetStencilPixelFormat
PipelineDescriptor & SetStencilPixelFormat(PixelFormat format)
Definition: pipeline_descriptor.cc:145
impeller::StencilOperation::kKeep
@ kKeep
Don't modify the current stencil value.
impeller::TextureDescriptor::format
PixelFormat format
Definition: texture_descriptor.h:40
impeller::PipelineDescriptor::SetPrimitiveType
void SetPrimitiveType(PrimitiveType type)
Definition: pipeline_descriptor.cc:268
impeller::PixelFormat::kR8G8B8A8UNormInt
@ kR8G8B8A8UNormInt
formats.h
impeller::scene::SceneContextOptions::primitive_type
PrimitiveType primitive_type
Definition: scene_context.h:21
impeller::TextureDescriptor::mip_count
size_t mip_count
Definition: texture_descriptor.h:42
impeller::HostBuffer::Create
static std::shared_ptr< HostBuffer > Create(const std::shared_ptr< Allocator > &allocator)
Definition: host_buffer.cc:20
impeller::scene::SceneContext::GetPlaceholderTexture
std::shared_ptr< Texture > GetPlaceholderTexture() const
Definition: scene_context.cc:110
impeller::StorageMode::kHostVisible
@ kHostVisible
impeller::scene::SceneContext::~SceneContext
~SceneContext()
impeller::scene::SceneContext::IsValid
bool IsValid() const
Definition: scene_context.cc:102
impeller::PipelineDescriptor::SetCullMode
void SetCullMode(CullMode mode)
Definition: pipeline_descriptor.cc:252
impeller::scene::SceneContextOptions
Definition: scene_context.h:19
impeller::Capabilities
Definition: capabilities.h:15
impeller::scene::PipelineKey
Definition: pipeline_key.h:23
impeller::DepthAttachmentDescriptor::depth_write_enabled
bool depth_write_enabled
Definition: formats.h:580
impeller::CullMode::kBackFace
@ kBackFace
material.h
impeller::scene::MaterialType::kUnlit
@ kUnlit
impeller::WindingOrder::kCounterClockwise
@ kCounterClockwise
impeller::StencilAttachmentDescriptor::depth_stencil_pass
StencilOperation depth_stencil_pass
Definition: formats.h:611
impeller::CompareFunction::kAlways
@ kAlways
Comparison test passes always passes.
impeller::PipelineDescriptor::SetSampleCount
PipelineDescriptor & SetSampleCount(SampleCount samples)
Definition: pipeline_descriptor.cc:76
impeller::TextureDescriptor::size
ISize size
Definition: texture_descriptor.h:41
impeller::scene::GeometryType::kSkinned
@ kSkinned
std
Definition: comparable.h:95
impeller::scene::SceneContext::GetContext
std::shared_ptr< Context > GetContext() const
Definition: scene_context.cc:106
impeller::DepthAttachmentDescriptor
Definition: formats.h:572
impeller::StencilAttachmentDescriptor
Definition: formats.h:592
impeller::PipelineDescriptor::SetDepthPixelFormat
PipelineDescriptor & SetDepthPixelFormat(PixelFormat format)
Definition: pipeline_descriptor.cc:139
impeller::scene::SceneContext::GetPipeline
std::shared_ptr< Pipeline< PipelineDescriptor > > GetPipeline(PipelineKey key, SceneContextOptions opts) const
Definition: scene_context.cc:90
impeller::TextureDescriptor::storage_mode
StorageMode storage_mode
Definition: texture_descriptor.h:38
impeller::PipelineDescriptor::SetDepthStencilAttachmentDescriptor
PipelineDescriptor & SetDepthStencilAttachmentDescriptor(std::optional< DepthAttachmentDescriptor > desc)
Definition: pipeline_descriptor.cc:151
impeller::TextureDescriptor
A lightweight object that describes the attributes of a texture that can then used an allocator to cr...
Definition: texture_descriptor.h:37
impeller::PipelineDescriptor::SetWindingOrder
void SetWindingOrder(WindingOrder order)
Definition: pipeline_descriptor.cc:260
impeller::scene::SceneContextOptions::ApplyToPipelineDescriptor
void ApplyToPipelineDescriptor(const Capabilities &capabilities, PipelineDescriptor &desc) const
Definition: scene_context.cc:16
impeller::CompareFunction::kLess
@ kLess
Comparison test passes if new_value < current_value.
impeller
Definition: aiks_blur_unittests.cc:20
impeller::DepthAttachmentDescriptor::depth_compare
CompareFunction depth_compare
Definition: formats.h:576
impeller::Capabilities::GetDefaultDepthStencilFormat
virtual PixelFormat GetDefaultDepthStencilFormat() const =0
Returns a supported PixelFormat for textures that store both a stencil and depth component....