Add test to check rendering to multiple primitives.
This commit is contained in:
@@ -269,9 +269,12 @@ struct Matrix {
|
||||
|
||||
Matrix operator+(const Matrix& m) const;
|
||||
|
||||
static constexpr Matrix MakeOrthographic(Scalar width, Scalar height) {
|
||||
template <class T>
|
||||
static constexpr Matrix MakeOrthographic(TSize<T> size) {
|
||||
// Per assumptions about NDC documented above.
|
||||
const auto scale = MakeScale({1.0f / width, -1.0f / height, 1.0});
|
||||
const auto scale =
|
||||
MakeScale({1.0f / static_cast<Scalar>(size.width),
|
||||
-1.0f / static_cast<Scalar>(size.height), 1.0});
|
||||
const auto translate = MakeTranslation({-1.0, 1.0, 0.5});
|
||||
return translate * scale;
|
||||
}
|
||||
|
||||
@@ -60,8 +60,7 @@ TEST_F(PrimitivesTest, CanCreateBoxPrimitive) {
|
||||
cmd.BindVertices(vertex_buffer);
|
||||
|
||||
BoxVertexShader::UniformBuffer uniforms;
|
||||
uniforms.mvp = Matrix::MakeOrthographic(surface.GetSize().width,
|
||||
surface.GetSize().height);
|
||||
uniforms.mvp = Matrix::MakeOrthographic(surface.GetSize());
|
||||
BoxVertexShader::BindUniformBuffer(
|
||||
cmd, pass.GetTransientsBuffer().EmplaceUniform(uniforms));
|
||||
|
||||
@@ -82,7 +81,87 @@ TEST_F(PrimitivesTest, CanCreateBoxPrimitive) {
|
||||
}
|
||||
return true;
|
||||
};
|
||||
OpenPlaygroundHere(callback);
|
||||
// OpenPlaygroundHere(callback);
|
||||
}
|
||||
|
||||
TEST_F(PrimitivesTest, CanRenderMultiplePrimitives) {
|
||||
auto context = GetContext();
|
||||
ASSERT_TRUE(context);
|
||||
using BoxPipelineBuilder =
|
||||
PipelineBuilder<BoxVertexShader, BoxFragmentShader>;
|
||||
auto desc = BoxPipelineBuilder::MakeDefaultPipelineDescriptor(*context);
|
||||
auto box_pipeline =
|
||||
context->GetPipelineLibrary()->GetRenderPipeline(std::move(desc)).get();
|
||||
ASSERT_TRUE(box_pipeline);
|
||||
|
||||
// Vertex buffer.
|
||||
VertexBufferBuilder<BoxVertexShader::PerVertexData> vertex_builder;
|
||||
vertex_builder.SetLabel("Box");
|
||||
vertex_builder.AddVertices({
|
||||
{{100, 100, 0.0}, {0.0, 0.0}}, // 1
|
||||
{{800, 100, 0.0}, {1.0, 0.0}}, // 2
|
||||
{{800, 800, 0.0}, {1.0, 1.0}}, // 3
|
||||
{{100, 100, 0.0}, {0.0, 0.0}}, // 1
|
||||
{{800, 800, 0.0}, {1.0, 1.0}}, // 3
|
||||
{{100, 800, 0.0}, {0.0, 1.0}}, // 4
|
||||
});
|
||||
auto vertex_buffer =
|
||||
vertex_builder.CreateVertexBuffer(*context->GetPermanentsAllocator());
|
||||
ASSERT_TRUE(vertex_buffer);
|
||||
|
||||
auto bridge = CreateTextureForFixture("bay_bridge.jpg");
|
||||
auto boston = CreateTextureForFixture("boston.jpg");
|
||||
ASSERT_TRUE(bridge && boston);
|
||||
auto sampler = context->GetSamplerLibrary()->GetSampler({});
|
||||
ASSERT_TRUE(sampler);
|
||||
|
||||
Renderer::RenderCallback callback = [&](const Surface& surface,
|
||||
RenderPass& pass) {
|
||||
Command cmd;
|
||||
cmd.label = "Box";
|
||||
cmd.pipeline = box_pipeline;
|
||||
|
||||
cmd.BindVertices(vertex_buffer);
|
||||
|
||||
BoxFragmentShader::FrameInfo frame_info;
|
||||
frame_info.current_time = fml::TimePoint::Now().ToEpochDelta().ToSecondsF();
|
||||
frame_info.cursor_position = GetCursorPosition();
|
||||
frame_info.window_size.x = GetWindowSize().width;
|
||||
frame_info.window_size.y = GetWindowSize().height;
|
||||
|
||||
BoxFragmentShader::BindFrameInfo(
|
||||
cmd, pass.GetTransientsBuffer().EmplaceUniform(frame_info));
|
||||
BoxFragmentShader::BindContents1(cmd, boston, sampler);
|
||||
BoxFragmentShader::BindContents2(cmd, bridge, sampler);
|
||||
|
||||
cmd.primitive_type = PrimitiveType::kTriangle;
|
||||
|
||||
{
|
||||
BoxVertexShader::UniformBuffer uniforms;
|
||||
uniforms.mvp = Matrix::MakeOrthographic(surface.GetSize());
|
||||
BoxVertexShader::BindUniformBuffer(
|
||||
cmd, pass.GetTransientsBuffer().EmplaceUniform(uniforms));
|
||||
|
||||
if (!pass.RecordCommand(cmd)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
BoxVertexShader::UniformBuffer uniforms;
|
||||
uniforms.mvp = Matrix::MakeOrthographic(surface.GetSize()) *
|
||||
Matrix::MakeTranslation({100.0, 100.0, 0.0});
|
||||
BoxVertexShader::BindUniformBuffer(
|
||||
cmd, pass.GetTransientsBuffer().EmplaceUniform(uniforms));
|
||||
|
||||
if (!pass.RecordCommand(cmd)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
};
|
||||
// OpenPlaygroundHere(callback);
|
||||
}
|
||||
|
||||
} // namespace testing
|
||||
|
||||
Reference in New Issue
Block a user