Skip to content

Commit 3583f59

Browse files
azhirnovTheMostDiligent
authored andcommitted
Added Matrix3x3::Inverse(), added check for RT feature when using BIND_RAY_TRACING, clamping maxAnisotropy to supported values
1 parent a2655d0 commit 3583f59

File tree

7 files changed

+139
-34
lines changed

7 files changed

+139
-34
lines changed

Common/interface/BasicMath.hpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,8 @@ template <class T> struct Vector2
228228

229229
Vector2() :
230230
x{0}, y{0} {}
231+
explicit Vector2(T s) :
232+
x{s}, y{s} {}
231233
Vector2(T _x, T _y) :
232234
x{_x}, y{_y} {}
233235

@@ -433,6 +435,8 @@ template <class T> struct Vector3
433435

434436
Vector3() :
435437
x{0}, y{0}, z{0} {}
438+
explicit Vector3(T s) :
439+
x{s}, y{s}, z{s} {}
436440
Vector3(T _x, T _y, T _z) :
437441
x{_x}, y{_y}, z{_z} {}
438442

@@ -652,6 +656,8 @@ template <class T> struct Vector4
652656

653657
Vector4() :
654658
x{0}, y{0}, z{0}, w{0} {}
659+
explicit Vector4(T s) :
660+
x{s}, y{s}, z{s}, w{s} {}
655661
Vector4(T _x, T _y, T _z, T _w) :
656662
x{_x}, y{_y}, z{_z}, w{_w} {}
657663
Vector4(const Vector3<T>& v3, T _w) :
@@ -1059,6 +1065,55 @@ template <class T> struct Matrix3x3
10591065
det += _13 * (_21 * _32 - _31 * _22);
10601066
return det;
10611067
}
1068+
1069+
Matrix3x3 Inverse() const
1070+
{
1071+
Matrix3x3 Inv;
1072+
1073+
Inv._11 = Matrix2x2<T>(_22, _23,
1074+
_32, _33)
1075+
.Determinant();
1076+
1077+
Inv._12 = -Matrix2x2<T>(_21, _23,
1078+
_31, _33)
1079+
.Determinant();
1080+
1081+
Inv._13 = Matrix2x2<T>(_21, _22,
1082+
_31, _32)
1083+
.Determinant();
1084+
1085+
1086+
Inv._21 = -Matrix2x2<T>(_12, _13,
1087+
_32, _33)
1088+
.Determinant();
1089+
1090+
Inv._22 = Matrix2x2<T>(_11, _13,
1091+
_31, _33)
1092+
.Determinant();
1093+
1094+
Inv._23 = -Matrix2x2<T>(_11, _12,
1095+
_31, _32)
1096+
.Determinant();
1097+
1098+
1099+
Inv._31 = Matrix2x2<T>(_12, _13,
1100+
_22, _23)
1101+
.Determinant();
1102+
1103+
Inv._32 = -Matrix2x2<T>(_11, _13,
1104+
_21, _23)
1105+
.Determinant();
1106+
1107+
Inv._33 = Matrix2x2<T>(_11, _12,
1108+
_21, _22)
1109+
.Determinant();
1110+
1111+
auto det = _11 * Inv._11 + _12 * Inv._12 + _13 * Inv._13;
1112+
Inv = Inv.Transpose();
1113+
Inv *= static_cast<T>(1) / det;
1114+
1115+
return Inv;
1116+
}
10621117
};
10631118

10641119
template <class T> struct Matrix4x4

Graphics/GraphicsEngine/include/BufferBase.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ namespace Diligent
4343
{
4444

4545
/// Validates buffer description and throws an exception in case of an error.
46-
void ValidateBufferDesc(const BufferDesc& Desc, const AdapterMemoryInfo& memoryInfo) noexcept(false);
46+
void ValidateBufferDesc(const BufferDesc& Desc, const IRenderDevice* pDevice) noexcept(false);
4747

4848
/// Validates initial buffer data parameters and throws an exception in case of an error.
4949
void ValidateBufferInitData(const BufferDesc& Desc, const BufferData* pBuffData) noexcept(false);
@@ -94,7 +94,7 @@ class BufferBase : public DeviceObjectBase<typename EngineImplTraits::BufferInte
9494
m_pDefaultUAV{nullptr, STDDeleter<BufferViewImplType, TBuffViewObjAllocator>(BuffViewObjAllocator)},
9595
m_pDefaultSRV{nullptr, STDDeleter<BufferViewImplType, TBuffViewObjAllocator>(BuffViewObjAllocator)}
9696
{
97-
ValidateBufferDesc(this->m_Desc, pDevice->GetAdapterInfo().Memory);
97+
ValidateBufferDesc(this->m_Desc, pDevice);
9898

9999
Uint64 DeviceQueuesMask = pDevice->GetCommandQueueMask();
100100
DEV_CHECK_ERR((this->m_Desc.ImmediateContextMask & DeviceQueuesMask) != 0,

Graphics/GraphicsEngine/interface/DeviceContext.h

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1176,11 +1176,14 @@ struct InstanceMatrix
11761176
}
11771177

11781178
/// Sets the rotation part.
1179-
InstanceMatrix& SetRotation(const float* pMatrix3x3) noexcept
1179+
InstanceMatrix& SetRotation(const float* pMatrix, Uint32 RowSize = 3) noexcept
11801180
{
1181-
data[0][0] = pMatrix3x3[0]; data[1][0] = pMatrix3x3[1]; data[2][0] = pMatrix3x3[2];
1182-
data[0][1] = pMatrix3x3[3]; data[1][1] = pMatrix3x3[4]; data[2][1] = pMatrix3x3[5];
1183-
data[0][2] = pMatrix3x3[6]; data[1][2] = pMatrix3x3[7]; data[2][2] = pMatrix3x3[8];
1181+
for (Uint32 r = 0; r < 3; ++r)
1182+
{
1183+
for (Uint32 c = 0; c < 3; ++c)
1184+
data[r][c] = pMatrix[c * RowSize + r];
1185+
}
1186+
11841187
return *this;
11851188
}
11861189
#endif
@@ -1202,7 +1205,12 @@ struct TLASBuildInstanceData
12021205
/// Instance to world transformation.
12031206
InstanceMatrix Transform;
12041207

1205-
/// User-defined value that can be accessed in the shader via InstanceID() in HLSL and gl_InstanceCustomIndex in GLSL.
1208+
/// User-defined value that can be accessed in the shader:
1209+
/// HLSL: `InstanceID()` in closest-hit and intersection shaders.
1210+
/// HLSL: `RayQuery::CommittedInstanceID()` within inline ray tracing.
1211+
/// GLSL: `gl_InstanceCustomIndex` in closest-hit and intersection shaders.
1212+
/// GLSL: `rayQueryGetIntersectionInstanceCustomIndex` within inline ray tracing.
1213+
/// MSL: `intersection_result< instancing >::instance_id`.
12061214
/// Only the lower 24 bits are used.
12071215
Uint32 CustomId DEFAULT_INITIALIZER(0);
12081216

Graphics/GraphicsEngine/src/BufferBase.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,11 @@ namespace Diligent
4343
} while (false)
4444

4545

46-
void ValidateBufferDesc(const BufferDesc& Desc, const AdapterMemoryInfo& memoryInfo) noexcept(false)
46+
void ValidateBufferDesc(const BufferDesc& Desc, const IRenderDevice* pDevice) noexcept(false)
4747
{
48+
const auto& MemoryInfo = pDevice->GetAdapterInfo().Memory;
49+
const auto& Features = pDevice->GetDeviceInfo().Features;
50+
4851
static_assert(BIND_FLAGS_LAST == 0x400L, "Please update this function to handle the new bind flags");
4952

5053
constexpr Uint32 AllowedBindFlags =
@@ -72,6 +75,9 @@ void ValidateBufferDesc(const BufferDesc& Desc, const AdapterMemoryInfo& memoryI
7275
}
7376
}
7477

78+
if ((Desc.BindFlags & BIND_RAY_TRACING) != 0)
79+
VERIFY_BUFFER(Features.RayTracing, "BIND_RAY_TRACING flag can't be used when RayTracing feature is not enabled.");
80+
7581
switch (Desc.Usage)
7682
{
7783
case USAGE_IMMUTABLE:
@@ -91,20 +97,20 @@ void ValidateBufferDesc(const BufferDesc& Desc, const AdapterMemoryInfo& memoryI
9197
break;
9298

9399
case USAGE_UNIFIED:
94-
VERIFY_BUFFER(memoryInfo.UnifiedMemory != 0,
100+
VERIFY_BUFFER(MemoryInfo.UnifiedMemory != 0,
95101
"Unified memory is not present on this device. Check the amount of available unified memory "
96102
"in the device caps before creating unified buffers.");
97103
VERIFY_BUFFER(Desc.CPUAccessFlags != CPU_ACCESS_NONE,
98104
"at least one of CPU_ACCESS_WRITE or CPU_ACCESS_READ flags must be specified for a unified buffer.");
99105
if (Desc.CPUAccessFlags & CPU_ACCESS_WRITE)
100106
{
101-
VERIFY_BUFFER(memoryInfo.UnifiedMemoryCPUAccess & CPU_ACCESS_WRITE,
107+
VERIFY_BUFFER(MemoryInfo.UnifiedMemoryCPUAccess & CPU_ACCESS_WRITE,
102108
"Unified memory on this device does not support write access. Check the available access flags "
103109
"in the device caps before creating unified buffers.");
104110
}
105111
if (Desc.CPUAccessFlags & CPU_ACCESS_READ)
106112
{
107-
VERIFY_BUFFER(memoryInfo.UnifiedMemoryCPUAccess & CPU_ACCESS_READ,
113+
VERIFY_BUFFER(MemoryInfo.UnifiedMemoryCPUAccess & CPU_ACCESS_READ,
108114
"Unified memory on this device does not support read access. Check the available access flags "
109115
"in the device caps before creating unified buffers.");
110116
}

Graphics/GraphicsEngineVulkan/src/SamplerVkImpl.cpp

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "RenderDeviceVkImpl.hpp"
3131
#include "VulkanTypeConversions.hpp"
3232
#include "GraphicsAccessories.hpp"
33+
#include "BasicMath.hpp"
3334

3435
namespace Diligent
3536
{
@@ -45,34 +46,30 @@ SamplerVkImpl::SamplerVkImpl(IReferenceCounters* pRefCounters, RenderDeviceVkImp
4546
// clang-format on
4647
{
4748
const auto& LogicalDevice = pRenderDeviceVk->GetLogicalDevice();
49+
const auto& Limits = pRenderDeviceVk->GetPhysicalDevice().GetProperties().limits;
4850

4951
VkSamplerCreateInfo SamplerCI{};
50-
SamplerCI.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
51-
SamplerCI.pNext = nullptr;
52-
SamplerCI.flags = 0; // reserved for future use
53-
SamplerCI.magFilter = FilterTypeToVkFilter(m_Desc.MagFilter);
54-
SamplerCI.minFilter = FilterTypeToVkFilter(m_Desc.MinFilter);
55-
SamplerCI.mipmapMode = FilterTypeToVkMipmapMode(m_Desc.MipFilter);
56-
SamplerCI.addressModeU = AddressModeToVkAddressMode(m_Desc.AddressU);
57-
SamplerCI.addressModeV = AddressModeToVkAddressMode(m_Desc.AddressV);
58-
SamplerCI.addressModeW = AddressModeToVkAddressMode(m_Desc.AddressW);
59-
SamplerCI.mipLodBias = m_Desc.MipLODBias;
52+
SamplerCI.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
53+
SamplerCI.pNext = nullptr;
54+
SamplerCI.flags = 0; // reserved for future use
55+
SamplerCI.magFilter = FilterTypeToVkFilter(m_Desc.MagFilter);
56+
SamplerCI.minFilter = FilterTypeToVkFilter(m_Desc.MinFilter);
57+
SamplerCI.mipmapMode = FilterTypeToVkMipmapMode(m_Desc.MipFilter);
58+
SamplerCI.addressModeU = AddressModeToVkAddressMode(m_Desc.AddressU);
59+
SamplerCI.addressModeV = AddressModeToVkAddressMode(m_Desc.AddressV);
60+
SamplerCI.addressModeW = AddressModeToVkAddressMode(m_Desc.AddressW);
61+
SamplerCI.mipLodBias = m_Desc.MipLODBias;
62+
6063
SamplerCI.anisotropyEnable = IsAnisotropicFilter(m_Desc.MinFilter);
61-
#ifdef DILIGENT_DEVELOPMENT
62-
if ((SamplerCI.anisotropyEnable != VK_FALSE) != IsAnisotropicFilter(m_Desc.MagFilter))
63-
{
64-
LOG_ERROR("Min and mag filters must both be either anisotropic filters or non-anisotropic ones");
65-
}
66-
#endif
64+
DEV_CHECK_ERR(!SamplerCI.anisotropyEnable || (m_Desc.MaxAnisotropy >= 1 && static_cast<float>(m_Desc.MaxAnisotropy) <= Limits.maxSamplerAnisotropy),
65+
"MaxAnisotropy (", m_Desc.MaxAnisotropy, ") must be in range 1 .. ", Limits.maxSamplerAnisotropy, ".");
66+
SamplerCI.maxAnisotropy = SamplerCI.anisotropyEnable ? clamp(static_cast<float>(m_Desc.MaxAnisotropy), 1.f, Limits.maxSamplerAnisotropy) : 0.f;
67+
DEV_CHECK_ERR((SamplerCI.anisotropyEnable != VK_FALSE) == IsAnisotropicFilter(m_Desc.MagFilter),
68+
"Min and mag filters must both be either anisotropic filters or non-anisotropic ones");
6769

68-
SamplerCI.maxAnisotropy = static_cast<float>(m_Desc.MaxAnisotropy);
6970
SamplerCI.compareEnable = IsComparisonFilter(m_Desc.MinFilter);
70-
#ifdef DILIGENT_DEVELOPMENT
71-
if ((SamplerCI.compareEnable != VK_FALSE) != IsComparisonFilter(m_Desc.MagFilter))
72-
{
73-
LOG_ERROR("Min and mag filters must both be either comparison filters or non-comparison ones");
74-
}
75-
#endif
71+
DEV_CHECK_ERR((SamplerCI.compareEnable != VK_FALSE) == IsComparisonFilter(m_Desc.MagFilter),
72+
"Min and mag filters must both be either comparison filters or non-comparison ones");
7673

7774
SamplerCI.compareOp = ComparisonFuncToVkCompareOp(m_Desc.ComparisonFunc);
7875
SamplerCI.minLod = m_Desc.MinLOD;

Graphics/ShaderTools/src/DXCompiler.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -751,6 +751,7 @@ void DXCompilerImpl::Compile(const ShaderCreateInfo& ShaderCI,
751751
L"-fspv-reflect",
752752
//L"-WX", // Warnings as errors
753753
L"-O3", // Optimization level 3
754+
L"-Zpc" // Matrices in column-major order
754755
});
755756

756757
if (m_APIVersion >= VK_API_VERSION_1_2 && ShaderModel >= ShaderVersion{6, 3})

Tests/DiligentCoreTest/src/Common/MathLibTest.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -964,6 +964,44 @@ TEST(Common_BasicMath, MatrixInverse)
964964
}
965965
}
966966
}
967+
968+
// Determinant
969+
{
970+
// clang-format off
971+
float3x3 m1
972+
{
973+
1, 2, 3,
974+
5, 6, 7,
975+
9, 10, 11
976+
};
977+
// clang-format on
978+
auto det = m1.Determinant();
979+
EXPECT_EQ(det, 0);
980+
}
981+
982+
{
983+
// clang-format off
984+
const float3x3 m
985+
{
986+
7, 8, 3,
987+
5, 1, 4,
988+
5, 11, 7
989+
};
990+
// clang-format on
991+
992+
auto inv = m.Inverse();
993+
auto identity = m * inv;
994+
995+
for (int j = 0; j < 3; ++j)
996+
{
997+
for (int i = 0; i < 3; ++i)
998+
{
999+
float ref = i == j ? 1.f : 0.f;
1000+
auto val = identity[i][j];
1001+
EXPECT_NEAR(val, ref, 1e-6f);
1002+
}
1003+
}
1004+
}
9671005
}
9681006

9691007

0 commit comments

Comments
 (0)