Reserve a slot for vertex buffer bindings.

This commit is contained in:
Chinmay Garde
2021-06-22 16:45:11 -07:00
committed by Dan Field
parent 943bf33bfd
commit 5814885863
10 changed files with 90 additions and 13 deletions

View File

@@ -32,7 +32,7 @@ struct {{camel_case(shader_name)}}{{camel_case(shader_stage)}}Info {
// ===========================================================================
{% for def in struct_definitions %}
struct alignas(16) {{def.name}} {
struct {{def.name}} {
{% for member in def.members %}
{{member.type}} {{member.name}};
{% endfor %}

View File

@@ -351,6 +351,54 @@ std::vector<Reflector::StructMember> Reflector::ReadStructMembers(
continue;
}
// Tightly packed Point
if (member.basetype == SPIRType::BaseType::Float && //
member.width == sizeof(float) * 8 && //
member.columns == 1 && //
member.vecsize == 2 //
) {
result.emplace_back(StructMember{
.type = "Point",
.name = GetMemberNameAtIndex(struct_type, i),
.offset = total_byte_length,
.byte_length = sizeof(Point),
});
total_byte_length += sizeof(Point);
continue;
}
// Tightly packed Vector3
if (member.basetype == SPIRType::BaseType::Float && //
member.width == sizeof(float) * 8 && //
member.columns == 1 && //
member.vecsize == 3 //
) {
result.emplace_back(StructMember{
.type = "Vector3",
.name = GetMemberNameAtIndex(struct_type, i),
.offset = total_byte_length,
.byte_length = sizeof(Vector3),
});
total_byte_length += sizeof(Vector3);
continue;
}
// Tightly packed Vector4
if (member.basetype == SPIRType::BaseType::Float && //
member.width == sizeof(float) * 8 && //
member.columns == 1 && //
member.vecsize == 4 //
) {
result.emplace_back(StructMember{
.type = "Vector4",
.name = GetMemberNameAtIndex(struct_type, i),
.offset = total_byte_length,
.byte_length = sizeof(Vector4),
});
total_byte_length += sizeof(Vector4);
continue;
}
// Other single isolated scalars.
{
auto maybe_known_type = ReadKnownScalarType(member.basetype);

View File

@@ -236,7 +236,6 @@ bool RenderPass::EncodeCommands(Allocator& allocator,
} else {
auto_pop_debug_marker.Release();
}
[pass setRenderPipelineState:command.pipeline->GetMTLRenderPipelineState()];
[pass setDepthStencilState:command.pipeline->GetMTLDepthStencilState()];
[pass setFrontFacingWinding:MTLWindingClockwise];

View File

@@ -31,6 +31,8 @@ class VertexDescriptor final : public Comparable<VertexDescriptor> {
MTLVertexDescriptor* GetMTLVertexDescriptor() const;
size_t GetVertexBufferIndex() const;
//| Comparable<VertexDescriptor>|
std::size_t GetHash() const override;

View File

@@ -188,23 +188,33 @@ bool VertexDescriptor::SetStageInputs(
return true;
}
size_t VertexDescriptor::GetVertexBufferIndex() const {
static constexpr size_t kReservedVertexBufferIndex =
30u; // The final slot available.
return kReservedVertexBufferIndex;
}
MTLVertexDescriptor* VertexDescriptor::GetMTLVertexDescriptor() const {
auto descriptor = [MTLVertexDescriptor vertexDescriptor];
const size_t vertex_buffer_index = GetVertexBufferIndex();
size_t stride = 0u;
for (const auto& input : stage_inputs_) {
auto attrib = descriptor.attributes[input.location];
attrib.format = input.format;
attrib.offset = stride;
// All vertex inputs are interleaved and tightly packed in one buffer at
// zero index.
attrib.bufferIndex = 0u;
// All vertex inputs are interleaved and tightly packed in one buffer at a
// reserved index.
attrib.bufferIndex = vertex_buffer_index;
stride += input.length;
}
descriptor.layouts[0].stride = stride;
descriptor.layouts[0].stepRate = 1u;
descriptor.layouts[0].stepFunction = MTLVertexStepFunctionPerVertex;
// Since it's all in one buffer, indicate its layout.
auto vertex_layout = descriptor.layouts[vertex_buffer_index];
vertex_layout.stride = stride;
vertex_layout.stepRate = 1u;
vertex_layout.stepFunction = MTLVertexStepFunctionPerVertex;
return descriptor;
}

View File

@@ -54,9 +54,11 @@ bool EntityRenderer::OnRender(RenderPass& pass) {
Command cmd;
cmd.label = "Box";
cmd.pipeline = box_primitive_->GetPipeline();
cmd.vertex_bindings.buffers[0u] =
// Vertex buffer.
cmd.vertex_bindings.buffers[box_primitive_->GetVertexBufferIndex()] =
vertex_builder.CreateVertexBuffer(pass.GetTransientsBuffer());
cmd.vertex_bindings.buffers[1u] =
// Uniform buffer.
cmd.vertex_bindings.buffers[0u] =
pass.GetTransientsBuffer().EmplaceUniform(uniforms);
cmd.index_buffer =
vertex_builder.CreateIndexBuffer(pass.GetTransientsBuffer());

View File

@@ -2,8 +2,10 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
layout(location = 0) out vec4 fragColor;
in vec3 color;
out vec4 fragColor;
void main() {
fragColor = vec4(1.0, 1.0, 1.0, 1.0);
fragColor = vec4(0.0, 1.0, 0.0, 1.0);
}

View File

@@ -6,8 +6,14 @@ uniform UniformBuffer {
mat4 mvp;
} uniforms;
uniform UB2 {
vec3 color;
} ub2;
in vec3 vertexPosition;
out vec3 color;
void main() {
gl_Position = vec4(vertexPosition, 1.0);
gl_Position = uniforms.mvp * vec4(vertexPosition, 1.0);
color = ub2.color;
}

View File

@@ -27,8 +27,11 @@ class BoxPrimitive final : public Primitive {
// |Primitive|
virtual bool Encode(RenderPass& pass) const override;
size_t GetVertexBufferIndex() const;
private:
std::shared_ptr<Pipeline> pipeline_ = 0;
size_t vertex_buffer_index_ = 0;
bool is_valid_ = false;
FML_DISALLOW_COPY_AND_ASSIGN(BoxPrimitive);

View File

@@ -31,6 +31,7 @@ BoxPrimitive::BoxPrimitive(std::shared_ptr<Context> context)
{
auto vertex_descriptor = std::make_shared<VertexDescriptor>();
vertex_buffer_index_ = vertex_descriptor->GetVertexBufferIndex();
if (!vertex_descriptor->SetStageInputs(
shader::BoxVertexInfo::kAllShaderStageInputs)) {
FML_LOG(ERROR) << "Could not configure vertex descriptor.";
@@ -67,6 +68,10 @@ BoxPrimitive::BoxPrimitive(std::shared_ptr<Context> context)
is_valid_ = true;
}
size_t BoxPrimitive::GetVertexBufferIndex() const {
return vertex_buffer_index_;
}
BoxPrimitive::~BoxPrimitive() = default;
std::shared_ptr<Pipeline> BoxPrimitive::GetPipeline() const {