| Flutter Impeller |

command.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/renderer/command.h"
6
7 #include <utility>
8
9 #include "impeller/base/validation.h"
10 #include "impeller/core/formats.h"
11 #include "impeller/renderer/vertex_descriptor.h"
12
13 namespace impeller {
14
15 bool Command::BindVertices(VertexBuffer buffer) {
16 if (buffer.index_type == IndexType::kUnknown) {
17 VALIDATION_LOG << "Cannot bind vertex buffer with an unknown index type.";
18 return false;
19 }
20
21 vertex_buffer = std::move(buffer);
22 return true;
23 }
24
25 bool Command::BindResource(ShaderStage stage,
26 const ShaderUniformSlot& slot,
27 const ShaderMetadata& metadata,
28 BufferView view) {
29 return DoBindResource(stage, slot, &metadata, std::move(view));
30 }
31
32 bool Command::BindResource(
33 ShaderStage stage,
34 const ShaderUniformSlot& slot,
35 const std::shared_ptr<const ShaderMetadata>& metadata,
36 BufferView view) {
37 return DoBindResource(stage, slot, metadata, std::move(view));
38 }
39
40 template <class T>
41 bool Command::DoBindResource(ShaderStage stage,
42 const ShaderUniformSlot& slot,
43 const T metadata,
44 BufferView view) {
45 FML_DCHECK(slot.ext_res_0 != VertexDescriptor::kReservedVertexBufferIndex);
46 if (!view) {
47 return false;
48 }
49
50 switch (stage) {
51 case ShaderStage::kVertex:
52 vertex_bindings.buffers.emplace_back(BufferAndUniformSlot{
53 .slot = slot, .view = BufferResource(metadata, std::move(view))});
54 return true;
55 case ShaderStage::kFragment:
56 fragment_bindings.buffers.emplace_back(BufferAndUniformSlot{
57 .slot = slot, .view = BufferResource(metadata, std::move(view))});
58 return true;
59 case ShaderStage::kCompute:
60 VALIDATION_LOG << "Use ComputeCommands for compute shader stages.";
61 case ShaderStage::kUnknown:
62 return false;
63 }
64
65 return false;
66 }
67
68 bool Command::BindResource(ShaderStage stage,
69 const SampledImageSlot& slot,
70 const ShaderMetadata& metadata,
71 std::shared_ptr<const Texture> texture,
72 std::shared_ptr<const Sampler> sampler) {
73 if (!sampler || !sampler->IsValid()) {
74 return false;
75 }
76 if (!texture || !texture->IsValid()) {
77 return false;
78 }
79
80 switch (stage) {
81 case ShaderStage::kVertex:
82 vertex_bindings.sampled_images.emplace_back(TextureAndSampler{
83 .slot = slot,
84 .texture = {&metadata, std::move(texture)},
85 .sampler = std::move(sampler),
86 });
87 return true;
88 case ShaderStage::kFragment:
89 fragment_bindings.sampled_images.emplace_back(TextureAndSampler{
90 .slot = slot,
91 .texture = {&metadata, std::move(texture)},
92 .sampler = std::move(sampler),
93 });
94 return true;
95 case ShaderStage::kCompute:
96 VALIDATION_LOG << "Use ComputeCommands for compute shader stages.";
97 case ShaderStage::kUnknown:
98 return false;
99 }
100
101 return false;
102 }
103
104 } // namespace impeller
impeller::VertexBuffer::index_type
IndexType index_type
Definition: vertex_buffer.h:29
impeller::ShaderUniformSlot::ext_res_0
size_t ext_res_0
ext_res_0 is the Metal binding value.
Definition: shader_types.h:86
impeller::ShaderUniformSlot
Metadata required to bind a buffer.
Definition: shader_types.h:81
impeller::ShaderStage::kUnknown
@ kUnknown
impeller::ShaderMetadata
Definition: shader_types.h:72
impeller::VertexBuffer
Definition: vertex_buffer.h:13
formats.h
impeller::Bindings::sampled_images
std::vector< TextureAndSampler > sampled_images
Definition: command.h:74
impeller::ShaderStage
ShaderStage
Definition: shader_types.h:22
validation.h
vertex_descriptor.h
impeller::Command::vertex_buffer
VertexBuffer vertex_buffer
The bound per-vertex data and optional index buffer.
Definition: command.h:151
command.h
impeller::Command::vertex_bindings
Bindings vertex_bindings
Definition: command.h:101
impeller::SampledImageSlot
Metadata required to bind a combined texture and sampler.
Definition: shader_types.h:98
impeller::Command::BindVertices
bool BindVertices(VertexBuffer buffer)
Specify the vertex and index buffer to use for this command.
Definition: command.cc:15
impeller::ShaderStage::kFragment
@ kFragment
impeller::TextureAndSampler
combines the texture, sampler and sampler slot information.
Definition: command.h:61
VALIDATION_LOG
#define VALIDATION_LOG
Definition: validation.h:67
impeller::TextureAndSampler::slot
SampledImageSlot slot
Definition: command.h:62
impeller::BufferView
Definition: buffer_view.h:13
impeller::Bindings::buffers
std::vector< BufferAndUniformSlot > buffers
Definition: command.h:75
impeller::VertexDescriptor::kReservedVertexBufferIndex
static constexpr size_t kReservedVertexBufferIndex
Definition: vertex_descriptor.h:25
impeller::ShaderStage::kVertex
@ kVertex
impeller::Command::fragment_bindings
Bindings fragment_bindings
Definition: command.h:106
impeller::ShaderStage::kCompute
@ kCompute
impeller
Definition: aiks_context.cc:10
impeller::IndexType::kUnknown
@ kUnknown
impeller::Command::BindResource
bool BindResource(ShaderStage stage, const ShaderUniformSlot &slot, const ShaderMetadata &metadata, BufferView view) override
Definition: command.cc:25
impeller::BufferResource
Resource< BufferView > BufferResource
Definition: command.h:57