Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
geometry.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
6
7#include <memory>
8#include <ostream>
9
18#include "impeller/scene/importer/scene_flatbuffers.h"
19#include "impeller/scene/shaders/skinned.vert.h"
20#include "impeller/scene/shaders/unskinned.vert.h"
21
22namespace impeller {
23namespace scene {
24
25//------------------------------------------------------------------------------
26/// Geometry
27///
28
29Geometry::~Geometry() = default;
30
31std::shared_ptr<CuboidGeometry> Geometry::MakeCuboid(Vector3 size) {
32 auto result = std::make_shared<CuboidGeometry>();
33 result->SetSize(size);
34 return result;
35}
36
37std::shared_ptr<Geometry> Geometry::MakeVertexBuffer(VertexBuffer vertex_buffer,
38 bool is_skinned) {
39 if (is_skinned) {
40 auto result = std::make_shared<SkinnedVertexBufferGeometry>();
41 result->SetVertexBuffer(std::move(vertex_buffer));
42 return result;
43 } else {
44 auto result = std::make_shared<UnskinnedVertexBufferGeometry>();
45 result->SetVertexBuffer(std::move(vertex_buffer));
46 return result;
47 }
48}
49
50std::shared_ptr<Geometry> Geometry::MakeFromFlatbuffer(
51 const fb::MeshPrimitive& mesh,
52 Allocator& allocator) {
53 IndexType index_type;
54 switch (mesh.indices()->type()) {
55 case fb::IndexType::k16Bit:
56 index_type = IndexType::k16bit;
57 break;
58 case fb::IndexType::k32Bit:
59 index_type = IndexType::k32bit;
60 break;
61 }
62
63 const uint8_t* vertices_start;
64 size_t vertices_bytes;
65 bool is_skinned;
66
67 switch (mesh.vertices_type()) {
68 case fb::VertexBuffer::UnskinnedVertexBuffer: {
69 const auto* vertices =
70 mesh.vertices_as_UnskinnedVertexBuffer()->vertices();
71 vertices_start = reinterpret_cast<const uint8_t*>(vertices->Get(0));
72 vertices_bytes = vertices->size() * sizeof(fb::Vertex);
73 is_skinned = false;
74 break;
75 }
76 case fb::VertexBuffer::SkinnedVertexBuffer: {
77 const auto* vertices = mesh.vertices_as_SkinnedVertexBuffer()->vertices();
78 vertices_start = reinterpret_cast<const uint8_t*>(vertices->Get(0));
79 vertices_bytes = vertices->size() * sizeof(fb::SkinnedVertex);
80 is_skinned = true;
81 break;
82 }
83 case fb::VertexBuffer::NONE:
84 VALIDATION_LOG << "Invalid vertex buffer type.";
85 return nullptr;
86 }
87
88 const uint8_t* indices_start =
89 reinterpret_cast<const uint8_t*>(mesh.indices()->data()->Data());
90
91 const size_t indices_bytes = mesh.indices()->data()->size();
92 if (vertices_bytes == 0 || indices_bytes == 0) {
93 return nullptr;
94 }
95
96 DeviceBufferDescriptor buffer_desc;
97 buffer_desc.size = vertices_bytes + indices_bytes;
99
100 auto buffer = allocator.CreateBuffer(buffer_desc);
101 buffer->SetLabel("Mesh vertices+indices");
102
103 if (!buffer->CopyHostBuffer(vertices_start, Range(0, vertices_bytes))) {
104 return nullptr;
105 }
106 if (!buffer->CopyHostBuffer(indices_start, Range(0, indices_bytes),
107 vertices_bytes)) {
108 return nullptr;
109 }
110
111 VertexBuffer vertex_buffer = {
112 .vertex_buffer = {.buffer = buffer, .range = Range(0, vertices_bytes)},
113 .index_buffer = {.buffer = buffer,
114 .range = Range(vertices_bytes, indices_bytes)},
115 .vertex_count = mesh.indices()->count(),
116 .index_type = index_type,
117 };
118 return MakeVertexBuffer(std::move(vertex_buffer), is_skinned);
119}
120
121void Geometry::SetJointsTexture(const std::shared_ptr<Texture>& texture) {}
122
123//------------------------------------------------------------------------------
124/// CuboidGeometry
125///
126
128
130
132 size_ = size;
133}
134
135// |Geometry|
139
140// |Geometry|
143 // Layout: position, normal, tangent, uv
144 builder.AddVertices({
145 // Front.
146 {Vector3(0, 0, 0), Vector3(0, 0, -1), Vector3(1, 0, 0), Point(0, 0),
147 Color::White()},
148 {Vector3(1, 0, 0), Vector3(0, 0, -1), Vector3(1, 0, 0), Point(1, 0),
149 Color::White()},
150 {Vector3(1, 1, 0), Vector3(0, 0, -1), Vector3(1, 0, 0), Point(1, 1),
151 Color::White()},
152 {Vector3(1, 1, 0), Vector3(0, 0, -1), Vector3(1, 0, 0), Point(1, 1),
153 Color::White()},
154 {Vector3(0, 1, 0), Vector3(0, 0, -1), Vector3(1, 0, 0), Point(0, 1),
155 Color::White()},
156 {Vector3(0, 0, 0), Vector3(0, 0, -1), Vector3(1, 0, 0), Point(0, 0),
157 Color::White()},
158 });
159 return builder.CreateVertexBuffer(allocator);
160}
161
162// |Geometry|
165 const Matrix& transform,
166 RenderPass& pass) const {
167 pass.SetVertexBuffer(
168 GetVertexBuffer(*scene_context.GetContext()->GetResourceAllocator()));
169
170 UnskinnedVertexShader::FrameInfo info;
171 info.mvp = transform;
172 UnskinnedVertexShader::BindFrameInfo(pass, buffer.EmplaceUniform(info));
173}
174
175//------------------------------------------------------------------------------
176/// UnskinnedVertexBufferGeometry
177///
178
180
182
184 VertexBuffer vertex_buffer) {
185 vertex_buffer_ = std::move(vertex_buffer);
186}
187
188// |Geometry|
192
193// |Geometry|
195 Allocator& allocator) const {
196 return vertex_buffer_;
197}
198
199// |Geometry|
201 const SceneContext& scene_context,
203 const Matrix& transform,
204 RenderPass& pass) const {
205 pass.SetVertexBuffer(
206 GetVertexBuffer(*scene_context.GetContext()->GetResourceAllocator()));
207
208 UnskinnedVertexShader::FrameInfo info;
209 info.mvp = transform;
210 UnskinnedVertexShader::BindFrameInfo(pass, buffer.EmplaceUniform(info));
211}
212
213//------------------------------------------------------------------------------
214/// SkinnedVertexBufferGeometry
215///
216
218
220
222 vertex_buffer_ = std::move(vertex_buffer);
223}
224
225// |Geometry|
229
230// |Geometry|
232 Allocator& allocator) const {
233 return vertex_buffer_;
234}
235
236// |Geometry|
238 const SceneContext& scene_context,
240 const Matrix& transform,
241 RenderPass& pass) const {
242 pass.SetVertexBuffer(
243 GetVertexBuffer(*scene_context.GetContext()->GetResourceAllocator()));
244
245 SamplerDescriptor sampler_desc;
246 sampler_desc.min_filter = MinMagFilter::kNearest;
247 sampler_desc.mag_filter = MinMagFilter::kNearest;
248 sampler_desc.mip_filter = MipFilter::kNearest;
250 sampler_desc.label = "NN Repeat";
251
252 SkinnedVertexShader::BindJointsTexture(
253 pass,
254 joints_texture_ ? joints_texture_ : scene_context.GetPlaceholderTexture(),
255 scene_context.GetContext()->GetSamplerLibrary()->GetSampler(
256 sampler_desc));
257
258 SkinnedVertexShader::FrameInfo info;
259 info.mvp = transform;
260 info.enable_skinning = joints_texture_ ? 1 : 0;
261 info.joint_texture_size =
262 joints_texture_ ? joints_texture_->GetSize().width : 1;
263 SkinnedVertexShader::BindFrameInfo(pass, buffer.EmplaceUniform(info));
264}
265
266// |Geometry|
268 const std::shared_ptr<Texture>& texture) {
269 joints_texture_ = texture;
270}
271} // namespace scene
272} // namespace impeller
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition DM.cpp:213
An object that allocates device memory.
Definition allocator.h:22
std::shared_ptr< DeviceBuffer > CreateBuffer(const DeviceBufferDescriptor &desc)
Definition allocator.cc:44
Render passes encode render commands directed as one specific render target into an underlying comman...
Definition render_pass.h:33
virtual bool SetVertexBuffer(VertexBuffer buffer)
Specify the vertex and index buffer to use for this command.
GeometryType GetGeometryType() const override
Definition geometry.cc:136
void SetSize(Vector3 size)
Definition geometry.cc:131
void BindToCommand(const SceneContext &scene_context, HostBuffer &buffer, const Matrix &transform, RenderPass &pass) const override
Definition geometry.cc:163
VertexBuffer GetVertexBuffer(Allocator &allocator) const override
Definition geometry.cc:141
static std::shared_ptr< Geometry > MakeFromFlatbuffer(const fb::MeshPrimitive &mesh, Allocator &allocator)
Definition geometry.cc:50
static std::shared_ptr< Geometry > MakeVertexBuffer(VertexBuffer vertex_buffer, bool is_skinned)
Definition geometry.cc:37
virtual void SetJointsTexture(const std::shared_ptr< Texture > &texture)
Definition geometry.cc:121
static std::shared_ptr< CuboidGeometry > MakeCuboid(Vector3 size)
Definition geometry.cc:31
std::shared_ptr< Context > GetContext() const
std::shared_ptr< Texture > GetPlaceholderTexture() const
void BindToCommand(const SceneContext &scene_context, HostBuffer &buffer, const Matrix &transform, RenderPass &pass) const override
Definition geometry.cc:237
VertexBuffer GetVertexBuffer(Allocator &allocator) const override
Definition geometry.cc:231
void SetJointsTexture(const std::shared_ptr< Texture > &texture) override
Definition geometry.cc:267
GeometryType GetGeometryType() const override
Definition geometry.cc:226
void SetVertexBuffer(VertexBuffer vertex_buffer)
Definition geometry.cc:221
void SetVertexBuffer(VertexBuffer vertex_buffer)
Definition geometry.cc:183
GeometryType GetGeometryType() const override
Definition geometry.cc:189
void BindToCommand(const SceneContext &scene_context, HostBuffer &buffer, const Matrix &transform, RenderPass &pass) const override
Definition geometry.cc:200
VertexBuffer GetVertexBuffer(Allocator &allocator) const override
Definition geometry.cc:194
static const uint8_t buffer[]
GAsyncResult * result
FlTexture * texture
@ kNearest
Sample from the nearest mip level.
TPoint< Scalar > Point
Definition point.h:316
@ kNearest
Select nearest to the sample point. Most widely supported.
static SkColor4f transform(SkColor4f c, SkColorSpace *src, SkColorSpace *dst)
Definition p3.cpp:47
std::shared_ptr< const DeviceBuffer > buffer
Definition buffer_view.h:16
static constexpr Color White()
Definition color.h:256
A 4x4 matrix using column-major storage.
Definition matrix.h:37
SamplerAddressMode width_address_mode
#define VALIDATION_LOG
Definition validation.h:73