Ver código fonte

Store ray tracing index format explicitly

Signed-off-by: Markus Prettner <[email protected]>
Markus Prettner 5 dias atrás
pai
commit
ab38ca9602

+ 1 - 2
Gems/Atom/Feature/Common/Assets/ShaderLib/Atom/Features/RayTracing/RayTracingSceneSrg.azsli

@@ -67,6 +67,7 @@ ShaderResourceGroup RayTracingSceneSrg : SRG_RayTracingScene
         uint m_bitangentOffset;
         uint m_uvOffset;
 
+        uint m_indexFormat;
         uint m_positionFormat;
         uint m_normalFormat;
         uint m_uvFormat;
@@ -78,7 +79,6 @@ ShaderResourceGroup RayTracingSceneSrg : SRG_RayTracingScene
 
         uint _padding1;
         uint _padding2;
-        uint _padding3;
 
         float3x4 m_worldInvTranspose;
     };
@@ -110,7 +110,6 @@ ShaderResourceGroup RayTracingSceneSrg : SRG_RayTracingScene
     #define MESH_BUFFER_FLAG_TANGENT        (1 << 0)
     #define MESH_BUFFER_FLAG_BITANGENT      (1 << 1)
     #define MESH_BUFFER_FLAG_UV             (1 << 2)
-    #define MESH_BUFFER_UINT16_INDEX        (1 << 3)
 
     // Specifies which debug visualization to use (value must be from RayTracingDebugViewMode enum)
     uint m_debugViewMode;

+ 4 - 3
Gems/Atom/Feature/Common/Assets/ShaderLib/Atom/Features/RayTracing/RayTracingSceneUtils.azsli

@@ -7,6 +7,7 @@
  */
 
 #include <Atom/Features/Bindless.azsli>
+#include <Atom/RHI/IndexFormat.azsli>
 #include <Atom/RPI/VertexBufferOperations.azsli>
 
 // returns the normalized camera view ray into the scene for this raytracing dispatch thread
@@ -25,13 +26,13 @@ uint3 GetHitIndices(RayTracingSceneSrg::MeshInfo meshInfo)
     // get the index buffer resource index from the indirection list
     uint meshIndexBufferArrayIndex = RayTracingSceneSrg::m_meshBufferIndices[NonUniformResourceIndex(meshInfo.m_bufferStartIndex + MESH_INDEX_BUFFER_OFFSET)];
 
-    uint indexFormatSize = meshInfo.m_bufferFlags & MESH_BUFFER_UINT16_INDEX ? 2 : 4;
+    IndexFormat indexFormat = (IndexFormat)meshInfo.m_indexFormat;
 
     // compute the offset into the index buffer for this primitve of the mesh
-    uint offsetBytes = meshInfo.m_indexOffset + (PrimitiveIndex() * 3 * indexFormatSize);
+    uint offsetBytes = meshInfo.m_indexOffset + (PrimitiveIndex() * 3 * GetIndexFormatSize(indexFormat));
 
     // load the indices for this primitive from the index buffer
-    if (meshInfo.m_bufferFlags & MESH_BUFFER_UINT16_INDEX)
+    if (indexFormat == IndexFormat::Uint16)
     {
 #if USE_BINDLESS_SRG
         return LoadUint16x3FromBufferUnaligned(Bindless::GetByteAddressBuffer(meshIndexBufferArrayIndex), offsetBytes);

+ 0 - 1
Gems/Atom/Feature/Common/Code/Include/Atom/Feature/RayTracing/RayTracingFeatureProcessorInterface.h

@@ -34,7 +34,6 @@ namespace AZ::Render
         Tangent = AZ_BIT(0),
         Bitangent = AZ_BIT(1),
         UV = AZ_BIT(2),
-        Uint16Index = AZ_BIT(3),
     };
     AZ_DEFINE_ENUM_BITWISE_OPERATORS(AZ::Render::RayTracingSubMeshBufferFlags);
 

+ 1 - 0
Gems/Atom/Feature/Common/Code/Source/RayTracing/RayTracingFeatureProcessor.cpp

@@ -462,6 +462,7 @@ namespace AZ
                         subMesh.m_bitangentShaderBufferView ? subMesh.m_bitangentVertexBufferView.GetByteOffset() : 0;
                     meshInfo.m_uvByteOffset = subMesh.m_uvShaderBufferView ? subMesh.m_uvVertexBufferView.GetByteOffset() : 0;
 
+                    meshInfo.m_indexFormat = subMesh.m_indexBufferView.GetIndexFormat();
                     meshInfo.m_positionFormat = subMesh.m_positionFormat;
                     meshInfo.m_normalFormat = subMesh.m_normalFormat;
                     meshInfo.m_uvFormat = subMesh.m_uvFormat;

+ 1 - 1
Gems/Atom/Feature/Common/Code/Source/RayTracing/RayTracingFeatureProcessor.h

@@ -192,6 +192,7 @@ namespace AZ
                 uint32_t m_bitangentByteOffset = 0;
                 uint32_t m_uvByteOffset = 0;
 
+                RHI::IndexFormat m_indexFormat;
                 RHI::VertexFormat m_positionFormat;
                 RHI::VertexFormat m_normalFormat;
                 RHI::VertexFormat m_uvFormat;
@@ -203,7 +204,6 @@ namespace AZ
 
                 uint32_t _padding1;
                 uint32_t _padding2;
-                uint32_t _padding3;
 
                 AZStd::array<float, 12> m_worldInvTranspose; // float3x4
             };

+ 35 - 0
Gems/Atom/RHI/Assets/ShaderLib/Atom/RHI/IndexFormat.azsli

@@ -0,0 +1,35 @@
+/*
+ * 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
+
+#if !defined(IndexFormatEnumType)
+#define IndexFormatEnumType
+#endif
+
+enum class IndexFormat IndexFormatEnumType
+{
+    Unknown = 0,
+
+    Uint16,
+    Uint32,
+};
+
+inline uint GetIndexFormatSize(IndexFormat format)
+{
+    switch (format)
+    {
+    case IndexFormat::Uint16:
+        return 2;
+    case IndexFormat::Uint32:
+        return 4;
+
+    default:
+        return 0;
+    }
+}

+ 1 - 0
Gems/Atom/RHI/Assets/atom_rhi_asset_files.cmake

@@ -6,5 +6,6 @@
 # 
 
 set(FILES
+    ShaderLib/Atom/RHI/IndexFormat.azsli
     ShaderLib/Atom/RHI/VertexFormat.azsli
 )

+ 28 - 0
Gems/Atom/RHI/Code/Include/Atom/RHI.Reflect/IndexFormat.h

@@ -0,0 +1,28 @@
+/*
+ * 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 <AzCore/base.h>
+
+namespace AZ::RHI
+{
+    // Use "Internal" namespace to avoid exposing "using uint..." to the AZ::RHI namespace
+    namespace Internal
+    {
+        using uint = uint32_t;
+#define IndexFormatEnumType : uint32_t
+#include "../../../RHI/Assets/ShaderLib/Atom/RHI/IndexFormat.azsli"
+#undef IndexFormatEnumType
+    } // namespace Internal
+
+    using IndexFormat = Internal::IndexFormat;
+
+    //! @brief Returns the size of the given index format in bytes.
+    uint32_t GetIndexFormatSize(IndexFormat indexFormat);
+} // namespace AZ::RHI

+ 1 - 9
Gems/Atom/RHI/Code/Include/Atom/RHI/DeviceIndexBufferView.h

@@ -7,21 +7,13 @@
  */
 #pragma once
 
-#include <Atom/RHI.Reflect/Format.h>
+#include <Atom/RHI.Reflect/IndexFormat.h>
 #include <AzCore/Utils/TypeHash.h>
 
 namespace AZ::RHI
 {
     class DeviceBuffer;
 
-    enum class IndexFormat : uint32_t
-    {
-        Uint16 = 0,
-        Uint32
-    };
-
-    uint32_t GetIndexFormatSize(IndexFormat indexFormat);
-
     class alignas(8) DeviceIndexBufferView
     {
     public:

+ 19 - 0
Gems/Atom/RHI/Code/Source/RHI.Reflect/IndexFormat.cpp

@@ -0,0 +1,19 @@
+/*
+ * 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
+ *
+ */
+
+#include <Atom/RHI.Reflect/IndexFormat.h>
+
+namespace AZ::RHI
+{
+    uint32_t GetIndexFormatSize(IndexFormat indexFormat)
+    {
+        uint32_t indexFormatSize{ Internal::GetIndexFormatSize(indexFormat) };
+        AZ_Assert(indexFormatSize != 0, "Failed to get size of index format %d", indexFormat);
+        return indexFormatSize;
+    }
+} // namespace AZ::RHI

+ 0 - 14
Gems/Atom/RHI/Code/Source/RHI/DeviceIndexBufferView.cpp

@@ -11,20 +11,6 @@
 
 namespace AZ::RHI
 {
-    uint32_t GetIndexFormatSize(IndexFormat indexFormat)
-    {
-        switch (indexFormat)
-        {
-        case IndexFormat::Uint16:
-            return 2;
-        case IndexFormat::Uint32:
-            return 4;
-        default:
-            AZ_Error("RHI", false, "Unknown index format %d", (uint32_t)indexFormat);
-            return 4;
-        }
-    }
-
     DeviceIndexBufferView::DeviceIndexBufferView(
         const DeviceBuffer& buffer,
         uint32_t byteOffset,

+ 2 - 0
Gems/Atom/RHI/Code/atom_rhi_reflect_files.cmake

@@ -132,6 +132,8 @@ set(FILES
     Source/RHI.Reflect/ShaderInputNameIndex.cpp
     Include/Atom/RHI.Reflect/VariableRateShadingEnums.h
     Include/Atom/RHI.Reflect/Allocators.h
+    Include/Atom/RHI.Reflect/IndexFormat.h
     Include/Atom/RHI.Reflect/VertexFormat.h
+    Source/RHI.Reflect/IndexFormat.cpp
     Source/RHI.Reflect/VertexFormat.cpp
 )

+ 13 - 0
Gems/Atom/RPI/Assets/ShaderLib/Atom/RPI/VertexBufferOperations.azsli

@@ -84,6 +84,13 @@ float3 DecodeNormalPackedOctahedron(uint encodedPackedValue, uint xyBits)
     return DecodeNormalSignedOctahedron(encodedNormal);
 }
 
+float4 DecodeTangentPackedOctahedron(uint encodedPackedValue, uint xyBits)
+{
+    return float4(
+        DecodeNormalPackedOctahedron(encodedPackedValue, xyBits),
+        ((encodedPackedValue >> (xyBits * 2 + 1)) & 1) * 2.f - 1.f);
+}
+
 float2 LoadFloat2FromBuffer(ByteAddressBuffer buffer, uint byteOffset, VertexFormat format)
 {
     switch (format)
@@ -128,6 +135,12 @@ float4 LoadFloat4FromBuffer(ByteAddressBuffer buffer, uint byteOffset, VertexFor
         return asfloat(buffer.Load4(byteOffset));
     case VertexFormat::R16G16B16A16_FLOAT:
         return LoadFloat16x4FromBuffer(buffer, byteOffset);
+    case VertexFormat::T32_OCT:
+        return DecodeTangentPackedOctahedron(buffer.Load(byteOffset), 15);
+    case VertexFormat::T16_OCT:
+        return DecodeTangentPackedOctahedron(LoadUint16FromBufferUnaligned(buffer, byteOffset), 7);
+    case VertexFormat::T8_OCT:
+        return DecodeTangentPackedOctahedron(LoadUint8FromBufferUnaligned(buffer, byteOffset), 3);
     default:
         return float4(0.f, 0.f, 0.f, 0.f);
     }