/* * 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 { using namespace AZ; // This sample demonstrates the use of Atom Ray Tracing through the RHI abstraction layer. // It creates three triangles and one rectangle in a scene, and ray traces that scene to // an output image and displays it. class RayTracingExampleComponent final : public BasicRHIComponent { public: AZ_COMPONENT(RayTracingExampleComponent, "{FC4636BC-9C5C-4D7D-8FEF-41A02C56B62D}", AZ::Component); AZ_DISABLE_COPY(RayTracingExampleComponent); static void Reflect(AZ::ReflectContext* context); RayTracingExampleComponent(); ~RayTracingExampleComponent() override {} protected: // AZ::Component void Activate() override; void Deactivate() override; private: void CreateResourcePools(); void CreateGeometry(); void CreateFullScreenBuffer(); void CreateOutputTexture(); void CreateRasterShader(); void CreateRayTracingAccelerationStructureObjects(); void CreateRayTracingPipelineState(); void CreateRayTracingShaderTable(); void CreateRayTracingAccelerationTableScope(); void CreateRayTracingDispatchScope(); void CreateRasterScope(); static const uint32_t m_imageWidth = 1920; static const uint32_t m_imageHeight = 1080; // resource pools RHI::Ptr m_inputAssemblyBufferPool; RHI::Ptr m_imagePool; RHI::Ptr m_rayTracingBufferPools; // triangle vertex/index buffers AZStd::array m_triangleVertices; AZStd::array m_triangleIndices; RHI::Ptr m_triangleVB; RHI::Ptr m_triangleIB; // rectangle vertex/index buffers AZStd::array m_rectangleVertices; AZStd::array m_rectangleIndices; RHI::Ptr m_rectangleVB; RHI::Ptr m_rectangleIB; // ray tracing acceleration structures RHI::Ptr m_triangleRayTracingBlas; RHI::Ptr m_rectangleRayTracingBlas; RHI::Ptr m_rayTracingTlas; RHI::BufferViewDescriptor m_tlasBufferViewDescriptor; RHI::AttachmentId m_tlasBufferAttachmentId = RHI::AttachmentId("tlasBufferAttachmentId"); // ray tracing shaders Data::Instance m_rayGenerationShader; Data::Instance m_missShader; Data::Instance m_closestHitGradientShader; Data::Instance m_closestHitSolidShader; // ray tracing pipeline state RHI::Ptr m_rayTracingPipelineState; // ray tracing shader table RHI::Ptr m_rayTracingShaderTable; // ray tracing global shader resource group and pipeline state Data::Instance m_globalSrg; RHI::ConstPtr m_globalPipelineState; // ray tracing local shader resource groups, one for each object in the scene enum LocalSrgs { Triangle1, Triangle2, Triangle3, Rectangle, Count }; AZStd::array, LocalSrgs::Count> m_localSrgs; bool m_buildLocalSrgs = true; // output image, written to by the ray tracing shader and displayed in the fullscreen draw shader RHI::Ptr m_outputImage; RHI::Ptr m_outputImageView; RHI::ImageViewDescriptor m_outputImageViewDescriptor; RHI::AttachmentId m_outputImageAttachmentId = RHI::AttachmentId("outputImageAttachmentId"); // fullscreen buffer for the raster pass to display the output image struct FullScreenBufferData { AZStd::array m_positions; AZStd::array m_uvs; AZStd::array m_indices; }; RHI::Ptr m_fullScreenInputAssemblyBuffer; RHI::GeometryView m_geometryView; RHI::InputStreamLayout m_fullScreenInputStreamLayout; RHI::ConstPtr m_drawPipelineState; Data::Instance m_drawSRG; RHI::ShaderInputConstantIndex m_drawDimensionConstantIndex; // time variable for moving the triangles and rectangle each frame float m_time = 0.0f; }; } // namespace AtomSampleViewer