| 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 pipelines_[{PipelineKey{GeometryType::kUnskinned, MaterialType::kUnlit}}] =
44 MakePipelineVariants<UnskinnedVertexShader, UnlitFragmentShader>(
45 *context_);
46 pipelines_[{PipelineKey{GeometryType::kSkinned, MaterialType::kUnlit}}] =
47 MakePipelineVariants<SkinnedVertexShader, UnlitFragmentShader>(*context_);
48
49 {
50 impeller::TextureDescriptor texture_descriptor;
51 texture_descriptor.storage_mode = impeller::StorageMode::kHostVisible;
52 texture_descriptor.format = PixelFormat::kR8G8B8A8UNormInt;
53 texture_descriptor.size = {1, 1};
54 texture_descriptor.mip_count = 1u;
55
56 placeholder_texture_ =
57 context_->GetResourceAllocator()->CreateTexture(texture_descriptor);
58 placeholder_texture_->SetLabel("Placeholder Texture");
59 if (!placeholder_texture_) {
60 FML_LOG(ERROR) << "Could not create placeholder texture.";
61 return;
62 }
63
64 uint8_t pixel[] = {0xFF, 0xFF, 0xFF, 0xFF};
65 if (!placeholder_texture_->SetContents(pixel, 4)) {
66 FML_LOG(ERROR) << "Could not set contents of placeholder texture.";
67 return;
68 }
69 }
70
71 is_valid_ = true;
72 }
73
74 SceneContext::~SceneContext() = default;
75
76 std::shared_ptr<Pipeline<PipelineDescriptor>> SceneContext::GetPipeline(
77 PipelineKey key,
78 SceneContextOptions opts) const {
79 if (!IsValid()) {
80 return nullptr;
81 }
82 if (auto found = pipelines_.find(key); found != pipelines_.end()) {
83 return found->second->GetPipeline(*context_, opts);
84 }
85 return nullptr;
86 }
87
88 bool SceneContext::IsValid() const {
89 return is_valid_;
90 }
91
92 std::shared_ptr<Context> SceneContext::GetContext() const {
93 return context_;
94 }
95
96 std::shared_ptr<Texture> SceneContext::GetPlaceholderTexture() const {
97 return placeholder_texture_;
98 }
99
100 } // namespace scene
101 } // namespace impeller
impeller::PipelineDescriptor
Definition: pipeline_descriptor.h:30
scene_context.h
impeller::StencilAttachmentDescriptor::stencil_compare
CompareFunction stencil_compare
Definition: formats.h:550
impeller::scene::SceneContextOptions::sample_count
SampleCount sample_count
Definition: scene_context.h:18
impeller::PipelineDescriptor::SetStencilAttachmentDescriptors
PipelineDescriptor & SetStencilAttachmentDescriptors(std::optional< StencilAttachmentDescriptor > front_and_back)
Definition: pipeline_descriptor.cc:153
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:141
impeller::TextureDescriptor::format
PixelFormat format
Definition: texture_descriptor.h:42
impeller::PipelineDescriptor::SetPrimitiveType
void SetPrimitiveType(PrimitiveType type)
Definition: pipeline_descriptor.cc:264
formats.h
impeller::scene::SceneContextOptions::primitive_type
PrimitiveType primitive_type
Definition: scene_context.h:19
impeller::TextureDescriptor::mip_count
size_t mip_count
Definition: texture_descriptor.h:44
impeller::scene::SceneContext::GetPlaceholderTexture
std::shared_ptr< Texture > GetPlaceholderTexture() const
Definition: scene_context.cc:96
impeller::PixelFormat::kR8G8B8A8UNormInt
@ kR8G8B8A8UNormInt
impeller::StorageMode::kHostVisible
@ kHostVisible
impeller::scene::SceneContext::~SceneContext
~SceneContext()
impeller::scene::SceneContext::IsValid
bool IsValid() const
Definition: scene_context.cc:88
impeller::PipelineDescriptor::SetCullMode
void SetCullMode(CullMode mode)
Definition: pipeline_descriptor.cc:248
impeller::scene::SceneContextOptions
Definition: scene_context.h:17
impeller::Capabilities
Definition: capabilities.h:14
impeller::scene::PipelineKey
Definition: pipeline_key.h:22
impeller::StencilOperation::kKeep
@ kKeep
Don't modify the current stencil value.
impeller::DepthAttachmentDescriptor::depth_write_enabled
bool depth_write_enabled
Definition: formats.h:532
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:563
impeller::CompareFunction::kLess
@ kLess
Comparison test passes if new_value < current_value.
impeller::PipelineDescriptor::SetSampleCount
PipelineDescriptor & SetSampleCount(SampleCount samples)
Definition: pipeline_descriptor.cc:72
impeller::TextureDescriptor::size
ISize size
Definition: texture_descriptor.h:43
impeller::scene::GeometryType::kSkinned
@ kSkinned
impeller::CompareFunction::kAlways
@ kAlways
Comparison test passes always passes.
std
Definition: comparable.h:98
impeller::scene::SceneContext::GetContext
std::shared_ptr< Context > GetContext() const
Definition: scene_context.cc:92
impeller::DepthAttachmentDescriptor
Definition: formats.h:524
impeller::StencilAttachmentDescriptor
Definition: formats.h:544
impeller::PipelineDescriptor::SetDepthPixelFormat
PipelineDescriptor & SetDepthPixelFormat(PixelFormat format)
Definition: pipeline_descriptor.cc:135
impeller::scene::SceneContext::GetPipeline
std::shared_ptr< Pipeline< PipelineDescriptor > > GetPipeline(PipelineKey key, SceneContextOptions opts) const
Definition: scene_context.cc:76
impeller::TextureDescriptor::storage_mode
StorageMode storage_mode
Definition: texture_descriptor.h:40
impeller::PipelineDescriptor::SetDepthStencilAttachmentDescriptor
PipelineDescriptor & SetDepthStencilAttachmentDescriptor(std::optional< DepthAttachmentDescriptor > desc)
Definition: pipeline_descriptor.cc:147
impeller::TextureDescriptor
A lightweight object that describes the attributes of a texture that can then used an allocator to cr...
Definition: texture_descriptor.h:39
impeller::PipelineDescriptor::SetWindingOrder
void SetWindingOrder(WindingOrder order)
Definition: pipeline_descriptor.cc:256
impeller::scene::SceneContextOptions::ApplyToPipelineDescriptor
void ApplyToPipelineDescriptor(const Capabilities &capabilities, PipelineDescriptor &desc) const
Definition: scene_context.cc:15
impeller
Definition: aiks_context.cc:10
impeller::DepthAttachmentDescriptor::depth_compare
CompareFunction depth_compare
Definition: formats.h:528
impeller::Capabilities::GetDefaultDepthStencilFormat
virtual PixelFormat GetDefaultDepthStencilFormat() const =0
Returns a supported PixelFormat for textures that store both a stencil and depth component....