mirror of
https://github.com/flutter/flutter.git
synced 2026-02-20 02:29:02 +08:00
Add support for primitive types.
This commit is contained in:
parent
27a7f7e831
commit
e968f9a9ee
@ -52,8 +52,8 @@ impeller_component("compositor") {
|
||||
"surface.mm",
|
||||
"texture.h",
|
||||
"texture.mm",
|
||||
"vertex_buffer.h",
|
||||
"vertex_buffer.mm",
|
||||
"vertex_buffer_builder.h",
|
||||
"vertex_buffer_builder.mm",
|
||||
"vertex_descriptor.h",
|
||||
"vertex_descriptor.mm",
|
||||
]
|
||||
|
||||
@ -10,6 +10,7 @@
|
||||
|
||||
#include "flutter/fml/macros.h"
|
||||
#include "impeller/compositor/buffer_view.h"
|
||||
#include "impeller/compositor/formats.h"
|
||||
#include "impeller/compositor/pipeline.h"
|
||||
|
||||
namespace impeller {
|
||||
@ -30,6 +31,7 @@ struct Command {
|
||||
BufferView index_buffer;
|
||||
size_t index_count = 0u;
|
||||
std::string label;
|
||||
PrimitiveType primitive_type;
|
||||
|
||||
constexpr operator bool() const { return pipeline && pipeline->IsValid(); }
|
||||
};
|
||||
|
||||
@ -56,6 +56,16 @@ enum class StoreAction {
|
||||
kStore,
|
||||
};
|
||||
|
||||
enum class PrimitiveType {
|
||||
kTriange,
|
||||
kTriangeStrip,
|
||||
kLine,
|
||||
kLineStrip,
|
||||
kPoint,
|
||||
// Triangle fans are implementation dependent and need extra extensions
|
||||
// checks. Hence, they are not supported here.
|
||||
};
|
||||
|
||||
enum class ColorWriteMask : uint64_t {
|
||||
kNone = 0,
|
||||
kRed = 1 << 0,
|
||||
|
||||
@ -64,6 +64,22 @@ constexpr MTLBlendFactor ToMTLBlendFactor(BlendFactor type) {
|
||||
return MTLBlendFactorZero;
|
||||
};
|
||||
|
||||
constexpr MTLPrimitiveType ToMTLPrimitiveType(PrimitiveType type) {
|
||||
switch (type) {
|
||||
case PrimitiveType::kTriange:
|
||||
return MTLPrimitiveTypeTriangle;
|
||||
case PrimitiveType::kTriangeStrip:
|
||||
return MTLPrimitiveTypeTriangleStrip;
|
||||
case PrimitiveType::kLine:
|
||||
return MTLPrimitiveTypeLine;
|
||||
case PrimitiveType::kLineStrip:
|
||||
return MTLPrimitiveTypeLineStrip;
|
||||
case PrimitiveType::kPoint:
|
||||
return MTLPrimitiveTypePoint;
|
||||
}
|
||||
return MTLPrimitiveTypePoint;
|
||||
}
|
||||
|
||||
constexpr MTLBlendOperation ToMTLBlendOperation(BlendOperation type) {
|
||||
switch (type) {
|
||||
case BlendOperation::kAdd:
|
||||
|
||||
@ -260,7 +260,7 @@ bool RenderPass::EncodeCommands(Allocator& allocator,
|
||||
}
|
||||
FML_DCHECK(command.index_count * sizeof(uint32_t) ==
|
||||
command.index_buffer.range.length);
|
||||
[pass drawIndexedPrimitives:MTLPrimitiveTypeTriangleStrip
|
||||
[pass drawIndexedPrimitives:ToMTLPrimitiveType(command.primitive_type)
|
||||
indexCount:command.index_count
|
||||
indexType:MTLIndexTypeUInt32
|
||||
indexBuffer:mtl_index_buffer
|
||||
|
||||
@ -53,6 +53,10 @@ bool Renderer::Render(const Surface& surface) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!surface.Present()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!render_pass->FinishEncoding(*GetContext()->GetTransientsAllocator())) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -6,6 +6,7 @@
|
||||
|
||||
#include <Metal/Metal.h>
|
||||
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
|
||||
#include "flutter/fml/macros.h"
|
||||
@ -16,16 +17,21 @@ namespace impeller {
|
||||
|
||||
class Surface {
|
||||
public:
|
||||
Surface(RenderPassDescriptor desc);
|
||||
Surface(RenderPassDescriptor desc,
|
||||
std::function<bool(void)> present_callback);
|
||||
|
||||
~Surface();
|
||||
|
||||
bool Present() const;
|
||||
|
||||
bool IsValid() const;
|
||||
|
||||
const RenderPassDescriptor& GetRenderPassDescriptor() const;
|
||||
|
||||
private:
|
||||
RenderPassDescriptor desc_;
|
||||
std::function<bool(void)> present_callback_;
|
||||
|
||||
bool is_valid_ = false;
|
||||
|
||||
FML_DISALLOW_COPY_AND_ASSIGN(Surface);
|
||||
|
||||
@ -8,7 +8,9 @@
|
||||
|
||||
namespace impeller {
|
||||
|
||||
Surface::Surface(RenderPassDescriptor desc) : desc_(std::move(desc)) {
|
||||
Surface::Surface(RenderPassDescriptor desc,
|
||||
std::function<bool(void)> present_callback)
|
||||
: desc_(std::move(desc)), present_callback_(present_callback) {
|
||||
if (!desc_.HasColorAttachment(0)) {
|
||||
return;
|
||||
}
|
||||
@ -17,6 +19,16 @@ Surface::Surface(RenderPassDescriptor desc) : desc_(std::move(desc)) {
|
||||
|
||||
Surface::~Surface() = default;
|
||||
|
||||
bool Surface::Present() const {
|
||||
auto callback = present_callback_;
|
||||
|
||||
if (!callback) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return callback();
|
||||
}
|
||||
|
||||
bool Surface::IsValid() const {
|
||||
return is_valid_;
|
||||
}
|
||||
|
||||
@ -2,7 +2,7 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include "impeller/compositor/vertex_buffer.h"
|
||||
#include "impeller/compositor/vertex_buffer_builder.h"
|
||||
|
||||
namespace impeller {
|
||||
|
||||
@ -3,8 +3,9 @@
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include "impeller/entity/entity_renderer.h"
|
||||
|
||||
#include "impeller/compositor/command.h"
|
||||
#include "impeller/compositor/vertex_buffer.h"
|
||||
#include "impeller/compositor/vertex_buffer_builder.h"
|
||||
#include "impeller/primitives/box.frag.h"
|
||||
#include "impeller/primitives/box.vert.h"
|
||||
|
||||
@ -40,23 +41,27 @@ bool EntityRenderer::OnRender(RenderPass& pass) {
|
||||
|
||||
shader::BoxVertexInfo::UniformBuffer uniforms;
|
||||
uniforms.mvp = Matrix::MakeOrthographic({800, 600});
|
||||
VertexBufferBuilder vertices;
|
||||
vertices.AddVertices({
|
||||
VertexBufferBuilder vertex_builder;
|
||||
vertex_builder.AddVertices({
|
||||
{-0.5, 0.5, 1.0}, //
|
||||
{0.5, 0.5, 1.0}, //
|
||||
{0.5, -0.5, 1.0}, //
|
||||
{0.5, -0.5, 1.0}, //
|
||||
{-0.5, -0.5, 1.0}, //
|
||||
{-0.5, 0.5, 1.0}, //
|
||||
});
|
||||
|
||||
Command cmd;
|
||||
cmd.label = "Box";
|
||||
cmd.pipeline = box_primitive_->GetPipeline();
|
||||
cmd.vertex_bindings.buffers[0u] =
|
||||
vertices.CreateVertexBuffer(pass.GetTransientsBuffer());
|
||||
vertex_builder.CreateVertexBuffer(pass.GetTransientsBuffer());
|
||||
cmd.vertex_bindings.buffers[1u] =
|
||||
pass.GetTransientsBuffer().EmplaceUniform(uniforms);
|
||||
cmd.index_buffer = vertices.CreateIndexBuffer(pass.GetTransientsBuffer());
|
||||
cmd.index_count = vertices.GetIndexCount();
|
||||
cmd.index_buffer =
|
||||
vertex_builder.CreateIndexBuffer(pass.GetTransientsBuffer());
|
||||
cmd.index_count = vertex_builder.GetIndexCount();
|
||||
cmd.primitive_type = PrimitiveType::kTriange;
|
||||
if (!pass.RecordCommand(std::move(cmd))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -226,11 +226,15 @@ static const size_t kAlignedUniformsSize = (sizeof(Uniforms) & ~0xFF) + 0x100;
|
||||
|
||||
- (void)drawInMTKView:(nonnull MTKView*)view {
|
||||
impeller::Surface surface(
|
||||
impeller::FromMTLRenderPassDescriptor(view.currentRenderPassDescriptor));
|
||||
impeller::FromMTLRenderPassDescriptor(view.currentRenderPassDescriptor),
|
||||
[drawable = view.currentDrawable]() {
|
||||
[drawable present];
|
||||
return true;
|
||||
});
|
||||
if (!renderer_->Render(surface)) {
|
||||
FML_LOG(ERROR) << "Could not render.";
|
||||
}
|
||||
|
||||
return;
|
||||
/// Per frame updates here
|
||||
dispatch_semaphore_wait(_inFlightSemaphore, DISPATCH_TIME_FOREVER);
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user