| 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/scene/material.h"
8 #include "impeller/scene/shaders/skinned.vert.h"
9 #include "impeller/scene/shaders/unlit.frag.h"
10 #include "impeller/scene/shaders/unskinned.vert.h"
11
12 namespace impeller {
13 namespace scene {
14
15 void SceneContextOptions::ApplyToPipelineDescriptor(
16 const Capabilities& capabilities,
17 PipelineDescriptor& desc) const {
18 DepthAttachmentDescriptor depth;
19 depth.depth_compare = CompareFunction::kLess;
20 depth.depth_write_enabled = true;
21 desc.SetDepthStencilAttachmentDescriptor(depth);
22 desc.SetDepthPixelFormat(capabilities.GetDefaultDepthStencilFormat());
23
24 StencilAttachmentDescriptor stencil;
25 stencil.stencil_compare = CompareFunction::kAlways;
26 stencil.depth_stencil_pass = StencilOperation::kKeep;
27 desc.SetStencilAttachmentDescriptors(stencil);
28 desc.SetStencilPixelFormat(capabilities.GetDefaultDepthStencilFormat());
29
30 desc.SetSampleCount(sample_count);
31 desc.SetPrimitiveType(primitive_type);
32
33 desc.SetWindingOrder(WindingOrder::kCounterClockwise);
34 desc.SetCullMode(CullMode::kBackFace);
35 }
36
37 SceneContext::SceneContext(std::shared_ptr<Context> context)
38 : context_(std::move(context)) {
39 if (!context_ || !context_->IsValid()) {
40 return;
41 }
42
43 auto unskinned_variant =
44 MakePipelineVariants<UnskinnedVertexShader, UnlitFragmentShader>(
45 *context_);
46 if (!unskinned_variant) {
47 FML_LOG(ERROR) << "Could not create unskinned pipeline variant.";
48 return;
49 }
50 pipelines_[{PipelineKey{GeometryType::kUnskinned, MaterialType::kUnlit}}] =
51 std::move(unskinned_variant);
52
53 auto skinned_variant =
54 MakePipelineVariants<SkinnedVertexShader, UnlitFragmentShader>(*context_);
55 if (!skinned_variant) {
56 FML_LOG(ERROR) << "Could not create skinned pipeline variant.";
57 return;
58 }
59 pipelines_[{PipelineKey{GeometryType::kSkinned, MaterialType::kUnlit}}] =
60 std::move(skinned_variant);
61
62 {
63 impeller::TextureDescriptor texture_descriptor;
64 texture_descriptor.storage_mode = impeller::StorageMode::kHostVisible;
65 texture_descriptor.format = PixelFormat::kR8G8B8A8UNormInt;
66 texture_descriptor.size = {1, 1};
67 texture_descriptor.mip_count = 1u;
68
69 placeholder_texture_ =
70 context_->GetResourceAllocator()->CreateTexture(texture_descriptor);
71 placeholder_texture_->SetLabel("Placeholder Texture");
72 if (!placeholder_texture_) {
73 FML_LOG(ERROR) << "Could not create placeholder texture.";
74 return;
75 }
76
77 uint8_t pixel[] = {0xFF, 0xFF, 0xFF, 0xFF};
78 if (!placeholder_texture_->SetContents(pixel, 4)) {
79 FML_LOG(ERROR) << "Could not set contents of placeholder texture.";
80 return;
81 }
82 }
83
84 is_valid_ = true;
85 }
86
87 SceneContext::~SceneContext() = default;
88
89 std::shared_ptr<Pipeline<PipelineDescriptor>> SceneContext::GetPipeline(
90 PipelineKey key,
91 SceneContextOptions opts) const {
92 if (!IsValid()) {
93 return nullptr;
94 }
95 if (auto found = pipelines_.find(key); found != pipelines_.end()) {
96 return found->second->GetPipeline(*context_, opts);
97 }
98 return nullptr;
99 }
100
101 bool SceneContext::IsValid() const {
102 return is_valid_;
103 }
104
105 std::shared_ptr<Context> SceneContext::GetContext() const {
106 return context_;
107 }
108
109 std::shared_ptr<Texture> SceneContext::GetPlaceholderTexture() const {
110 return placeholder_texture_;
111 }
112
113 } // namespace scene
114 } // namespace impeller
impeller::PipelineDescriptor
Definition: pipeline_descriptor.h:29
scene_context.h
impeller::StencilAttachmentDescriptor::stencil_compare
CompareFunction stencil_compare
Definition: formats.h:593
impeller::scene::SceneContextOptions::sample_count
SampleCount sample_count
Definition: scene_context.h:19
impeller::PipelineDescriptor::SetStencilAttachmentDescriptors
PipelineDescriptor & SetStencilAttachmentDescriptors(std::optional< StencilAttachmentDescriptor > front_and_back)
Definition: pipeline_descriptor.cc:159
impeller::scene::GeometryType::kUnskinned
@ kUnskinned
impeller::scene::SceneContext::SceneContext
SceneContext(std::shared_ptr< Context > context)
Definition: scene_context.cc:37
impeller::PipelineDescriptor::SetStencilPixelFormat
PipelineDescriptor & SetStencilPixelFormat(PixelFormat format)
Definition: pipeline_descriptor.cc:147
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:270
impeller::PixelFormat::kR8G8B8A8UNormInt
@ kR8G8B8A8UNormInt
formats.h
impeller::scene::SceneContextOptions::primitive_type
PrimitiveType primitive_type
Definition: scene_context.h:20
impeller::TextureDescriptor::mip_count
size_t mip_count
Definition: texture_descriptor.h:42
impeller::scene::SceneContext::GetPlaceholderTexture
std::shared_ptr< Texture > GetPlaceholderTexture() const
Definition: scene_context.cc:109
impeller::StorageMode::kHostVisible
@ kHostVisible
impeller::scene::SceneContext::~SceneContext
~SceneContext()
impeller::scene::SceneContext::IsValid
bool IsValid() const
Definition: scene_context.cc:101
impeller::PipelineDescriptor::SetCullMode
void SetCullMode(CullMode mode)
Definition: pipeline_descriptor.cc:254
impeller::scene::SceneContextOptions
Definition: scene_context.h:18
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:575
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:606
impeller::CompareFunction::kAlways
@ kAlways
Comparison test passes always passes.
impeller::PipelineDescriptor::SetSampleCount
PipelineDescriptor & SetSampleCount(SampleCount samples)
Definition: pipeline_descriptor.cc:78
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:105
impeller::DepthAttachmentDescriptor
Definition: formats.h:567
impeller::StencilAttachmentDescriptor
Definition: formats.h:587
impeller::PipelineDescriptor::SetDepthPixelFormat
PipelineDescriptor & SetDepthPixelFormat(PixelFormat format)
Definition: pipeline_descriptor.cc:141
impeller::scene::SceneContext::GetPipeline
std::shared_ptr< Pipeline< PipelineDescriptor > > GetPipeline(PipelineKey key, SceneContextOptions opts) const
Definition: scene_context.cc:89
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:153
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:262
impeller::scene::SceneContextOptions::ApplyToPipelineDescriptor
void ApplyToPipelineDescriptor(const Capabilities &capabilities, PipelineDescriptor &desc) const
Definition: scene_context.cc:15
impeller::CompareFunction::kLess
@ kLess
Comparison test passes if new_value < current_value.
impeller
Definition: aiks_context.cc:10
impeller::DepthAttachmentDescriptor::depth_compare
CompareFunction depth_compare
Definition: formats.h:571
impeller::Capabilities::GetDefaultDepthStencilFormat
virtual PixelFormat GetDefaultDepthStencilFormat() const =0
Returns a supported PixelFormat for textures that store both a stencil and depth component....