/* * Copyright (c) Contributors to the Open 3D Engine Project. * For complete copyright and license terms please see the LICENSE at the root of this distribution. * * SPDX-License-Identifier: Apache-2.0 OR MIT * */ #pragma once #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace AtomSampleViewer { /** * This example is similar to Triangle example, but it uses instancing to render * 30 triangles in 1 draw call. The main purpose of this example is show how to use a * Constant Buffer to pass data to the shader instead of using SRG Constants. * The data of all instances is uploaded with a single Constant Buffer * once per frame, then the vertex shader uses the SV_InstanceID to access to the * right instance information from the Constant Buffer. */ class TrianglesConstantBufferExampleComponent final : public BasicRHIComponent , public AZ::TickBus::Handler { struct InstanceInfo { AZStd::array m_matrix; AZStd::array m_colorMultiplier; }; public: AZ_COMPONENT(TrianglesConstantBufferExampleComponent, "{EF33DA37-65F8-44E5-BD09-B1C948229374}", AZ::Component); AZ_DISABLE_COPY(TrianglesConstantBufferExampleComponent); static void Reflect(AZ::ReflectContext* context); TrianglesConstantBufferExampleComponent(); ~TrianglesConstantBufferExampleComponent() override = default; private: static const char* s_trianglesConstantBufferExampleName; static const uint32_t s_numberOfTrianglesTotal; // AZ::Component void Activate() override; void Deactivate() override; // TickBus::Handler void OnTick(float deltaTime, AZ::ScriptTimePoint time) override; // RHISystemNotificationBus::Handler void OnFramePrepare(AZ::RHI::FrameGraphBuilder& frameGraphBuilder) override; // Updates a single constant buffer with multiple triangle instances void UploadDataToConstantBuffer(InstanceInfo* data, uint32_t elementSize, uint32_t elementCount); void CreateConstantBufferView(); float m_time = 0.0f; // ------------------------------------------------- // Input Assembly buffer and its Streams/Index Views // ------------------------------------------------- struct IABufferData { AZStd::array m_positions; AZStd::array m_colors; AZStd::array m_indices; }; AZ::RHI::Ptr m_inputAssemblyBufferPool; AZ::RHI::Ptr m_inputAssemblyBuffer; AZ::RHI::IndexBufferView m_indexBufferView; AZ::RHI::Ptr m_constantBufferPool; AZ::RHI::Ptr m_constantBuffer; AZ::RHI::Ptr m_constantBufferView; AZ::RHI::GeometryView m_geometryView; // -------------------------------------------------------- // Pipeline state and SRG to be constructed from the shader // -------------------------------------------------------- AZ::RHI::ConstPtr m_pipelineState; AZ::Data::Instance m_shaderResourceGroup; }; } // namespace AtomSampleViewer