Skip to content

Commit

Permalink
Fixed a few code scanning issues
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMostDiligent committed Aug 26, 2024
1 parent 94c1722 commit 8b57210
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 72 deletions.
40 changes: 21 additions & 19 deletions Graphics/GraphicsAccessories/src/ColorConversion.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2023 Diligent Graphics LLC
* Copyright 2019-2024 Diligent Graphics LLC
* Copyright 2015-2019 Egor Yusov
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -38,41 +38,43 @@ namespace
class LinearToGammaMap
{
public:
LinearToGammaMap() noexcept
{
for (Uint32 i = 0; i < m_ToGamma.size(); ++i)
{
m_ToGamma[i] = LinearToGamma(static_cast<float>(i) / 255.f);
}
}

float operator[](Uint8 x) const
{
return m_ToGamma[x];
}

private:
std::array<float, 256> m_ToGamma;
const std::array<float, 256> m_ToGamma{
[]() {
std::array<float, 256> ToGamma;
for (Uint32 i = 0; i < ToGamma.size(); ++i)
{
ToGamma[i] = LinearToGamma(static_cast<float>(i) / 255.f);
}
return ToGamma;
}(),
};
};

class GammaToLinearMap
{
public:
GammaToLinearMap() noexcept
{
for (Uint32 i = 0; i < m_ToLinear.size(); ++i)
{
m_ToLinear[i] = GammaToLinear(static_cast<float>(i) / 255.f);
}
}

float operator[](Uint8 x) const
{
return m_ToLinear[x];
}

private:
std::array<float, 256> m_ToLinear;
const std::array<float, 256> m_ToLinear{
[]() {
std::array<float, 256> ToLinear;
for (Uint32 i = 0; i < ToLinear.size(); ++i)
{
ToLinear[i] = GammaToLinear(static_cast<float>(i) / 255.f);
}
return ToLinear;
}(),
};
};

} // namespace
Expand Down
48 changes: 26 additions & 22 deletions Graphics/GraphicsEngineD3D12/src/D3D12TypeConversions.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2023 Diligent Graphics LLC
* Copyright 2019-2024 Diligent Graphics LLC
* Copyright 2015-2019 Egor Yusov
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -402,24 +402,26 @@ static D3D12_RESOURCE_STATES ResourceStateFlagToD3D12ResourceState(RESOURCE_STAT
class StateFlagBitPosToD3D12ResourceState
{
public:
StateFlagBitPosToD3D12ResourceState()
{
static_assert((1 << MaxFlagBitPos) == RESOURCE_STATE_MAX_BIT, "This function must be updated to handle new resource state flag");
for (Uint32 bit = 0; bit < FlagBitPosToResStateMap.size(); ++bit)
{
FlagBitPosToResStateMap[bit] = ResourceStateFlagToD3D12ResourceState(static_cast<RESOURCE_STATE>(1 << bit));
}
}

D3D12_RESOURCE_STATES operator()(Uint32 BitPos) const
{
VERIFY(BitPos <= MaxFlagBitPos, "Resource state flag bit position (", BitPos, ") exceeds max bit position (", MaxFlagBitPos, ")");
return FlagBitPosToResStateMap[BitPos];
}

private:
static constexpr Uint32 MaxFlagBitPos = 21;
std::array<D3D12_RESOURCE_STATES, MaxFlagBitPos + 1> FlagBitPosToResStateMap;
static constexpr Uint32 MaxFlagBitPos = 21;

const std::array<D3D12_RESOURCE_STATES, MaxFlagBitPos + 1> FlagBitPosToResStateMap{
[]() {
std::array<D3D12_RESOURCE_STATES, MaxFlagBitPos + 1> BitPosToStateMap;
static_assert((1 << MaxFlagBitPos) == RESOURCE_STATE_MAX_BIT, "This function must be updated to handle new resource state flag");
for (Uint32 bit = 0; bit < BitPosToStateMap.size(); ++bit)
{
BitPosToStateMap[bit] = ResourceStateFlagToD3D12ResourceState(static_cast<RESOURCE_STATE>(1 << bit));
}
return BitPosToStateMap;
}(),
};
};

D3D12_RESOURCE_STATES ResourceStateFlagsToD3D12ResourceStates(RESOURCE_STATE StateFlags)
Expand Down Expand Up @@ -515,23 +517,25 @@ static RESOURCE_STATE D3D12ResourceStateToResourceStateFlags(D3D12_RESOURCE_STAT
class D3D12StateFlagBitPosToResourceState
{
public:
D3D12StateFlagBitPosToResourceState()
{
for (Uint32 bit = 0; bit < FlagBitPosToResStateMap.size(); ++bit)
{
FlagBitPosToResStateMap[bit] = D3D12ResourceStateToResourceStateFlags(static_cast<D3D12_RESOURCE_STATES>(1 << bit));
}
}

RESOURCE_STATE operator()(Uint32 BitPos) const
{
VERIFY(BitPos <= MaxFlagBitPos, "Resource state flag bit position (", BitPos, ") exceeds max bit position (", MaxFlagBitPos, ")");
return FlagBitPosToResStateMap[BitPos];
}

private:
static constexpr Uint32 MaxFlagBitPos = 13;
std::array<RESOURCE_STATE, MaxFlagBitPos + 1> FlagBitPosToResStateMap;
static constexpr Uint32 MaxFlagBitPos = 13;

const std::array<RESOURCE_STATE, MaxFlagBitPos + 1> FlagBitPosToResStateMap{
[]() {
std::array<RESOURCE_STATE, MaxFlagBitPos + 1> BitPosToStateMap;
for (Uint32 bit = 0; bit < BitPosToStateMap.size(); ++bit)
{
BitPosToStateMap[bit] = D3D12ResourceStateToResourceStateFlags(static_cast<D3D12_RESOURCE_STATES>(1 << bit));
}
return BitPosToStateMap;
}(),
};
};

RESOURCE_STATE D3D12ResourceStatesToResourceStateFlags(D3D12_RESOURCE_STATES StateFlags)
Expand Down
43 changes: 22 additions & 21 deletions Graphics/GraphicsEngineD3D12/src/PipelineStateD3D12Impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,24 +118,25 @@ class PrimitiveTopology_To_D3D12_PRIMITIVE_TOPOLOGY_TYPE
std::array<D3D12_PRIMITIVE_TOPOLOGY_TYPE, PRIMITIVE_TOPOLOGY_NUM_TOPOLOGIES> m_Map;
};

template <typename TShaderStages>
void BuildRTPipelineDescription(const RayTracingPipelineStateCreateInfo& CreateInfo,
std::vector<D3D12_STATE_SUBOBJECT>& Subobjects,
DynamicLinearAllocator& TempPool,
TShaderStages& ShaderStages) noexcept(false)
PipelineStateD3D12Impl::TShaderStages& ShaderStages) noexcept(false)
{
#define LOG_PSO_ERROR_AND_THROW(...) LOG_ERROR_AND_THROW("Description of ray tracing PSO '", (CreateInfo.PSODesc.Name ? CreateInfo.PSODesc.Name : ""), "' is invalid: ", ##__VA_ARGS__)

Uint32 UnnamedExportIndex = 0;

std::unordered_map<IShader*, LPCWSTR> UniqueShaders;

std::array<typename TShaderStages::value_type*, MAX_SHADERS_IN_PIPELINE> StagesPtr = {};
std::array<Uint32, MAX_SHADERS_IN_PIPELINE> ShaderIndices = {};
using ShaderStageInfo = PipelineStateD3D12Impl::ShaderStageInfo;

for (auto& Stage : ShaderStages)
std::array<ShaderStageInfo*, MAX_SHADERS_IN_PIPELINE> StagesPtr = {};
std::array<Uint32, MAX_SHADERS_IN_PIPELINE> ShaderIndices = {};

for (ShaderStageInfo& Stage : ShaderStages)
{
const auto Idx = GetShaderTypePipelineIndex(Stage.Type, PIPELINE_TYPE_RAY_TRACING);
const Int32 Idx = GetShaderTypePipelineIndex(Stage.Type, PIPELINE_TYPE_RAY_TRACING);
VERIFY_EXPR(StagesPtr[Idx] == nullptr);
StagesPtr[Idx] = &Stage;
}
Expand All @@ -147,18 +148,18 @@ void BuildRTPipelineDescription(const RayTracingPipelineStateCreateInfo& CreateI
auto it_inserted = UniqueShaders.emplace(pShader, nullptr);
if (it_inserted.second)
{
const auto StageIdx = GetShaderTypePipelineIndex(pShader->GetDesc().ShaderType, PIPELINE_TYPE_RAY_TRACING);
const auto& Stage = *StagesPtr[StageIdx];
auto& ShaderIndex = ShaderIndices[StageIdx];
const Int32 StageIdx = GetShaderTypePipelineIndex(pShader->GetDesc().ShaderType, PIPELINE_TYPE_RAY_TRACING);
const ShaderStageInfo& Stage = *StagesPtr[StageIdx];
Uint32& ShaderIndex = ShaderIndices[StageIdx];

// shaders must be in same order as in ExtractShaders()
RefCntAutoPtr<ShaderD3D12Impl> pShaderD3D12{pShader, ShaderD3D12Impl::IID_InternalImpl};
VERIFY(pShaderD3D12, "Unexpected shader object implementation");
VERIFY_EXPR(Stage.Shaders[ShaderIndex] == pShaderD3D12);

auto& LibDesc = *TempPool.Construct<D3D12_DXIL_LIBRARY_DESC>();
auto& ExportDesc = *TempPool.Construct<D3D12_EXPORT_DESC>();
const IDataBlob* pByteCode = Stage.ByteCodes[ShaderIndex];
D3D12_DXIL_LIBRARY_DESC& LibDesc = *TempPool.Construct<D3D12_DXIL_LIBRARY_DESC>();
D3D12_EXPORT_DESC& ExportDesc = *TempPool.Construct<D3D12_EXPORT_DESC>();
const IDataBlob* pByteCode = Stage.ByteCodes[ShaderIndex];
++ShaderIndex;

LibDesc.DXILLibrary.BytecodeLength = pByteCode->GetSize();
Expand Down Expand Up @@ -189,15 +190,15 @@ void BuildRTPipelineDescription(const RayTracingPipelineStateCreateInfo& CreateI

for (Uint32 i = 0; i < CreateInfo.GeneralShaderCount; ++i)
{
const auto& GeneralShader = CreateInfo.pGeneralShaders[i];
const RayTracingGeneralShaderGroup& GeneralShader = CreateInfo.pGeneralShaders[i];
AddDxilLib(GeneralShader.pShader, GeneralShader.Name);
}

for (Uint32 i = 0; i < CreateInfo.TriangleHitShaderCount; ++i)
{
const auto& TriHitShader = CreateInfo.pTriangleHitShaders[i];
const RayTracingTriangleHitShaderGroup& TriHitShader = CreateInfo.pTriangleHitShaders[i];

auto& HitGroupDesc = *TempPool.Construct<D3D12_HIT_GROUP_DESC>();
D3D12_HIT_GROUP_DESC& HitGroupDesc = *TempPool.Construct<D3D12_HIT_GROUP_DESC>();
HitGroupDesc.HitGroupExport = TempPool.CopyWString(TriHitShader.Name);
HitGroupDesc.Type = D3D12_HIT_GROUP_TYPE_TRIANGLES;
HitGroupDesc.ClosestHitShaderImport = AddDxilLib(TriHitShader.pClosestHitShader, nullptr);
Expand All @@ -209,9 +210,9 @@ void BuildRTPipelineDescription(const RayTracingPipelineStateCreateInfo& CreateI

for (Uint32 i = 0; i < CreateInfo.ProceduralHitShaderCount; ++i)
{
const auto& ProcHitShader = CreateInfo.pProceduralHitShaders[i];
const RayTracingProceduralHitShaderGroup& ProcHitShader = CreateInfo.pProceduralHitShaders[i];

auto& HitGroupDesc = *TempPool.Construct<D3D12_HIT_GROUP_DESC>();
D3D12_HIT_GROUP_DESC& HitGroupDesc = *TempPool.Construct<D3D12_HIT_GROUP_DESC>();
HitGroupDesc.HitGroupExport = TempPool.CopyWString(ProcHitShader.Name);
HitGroupDesc.Type = D3D12_HIT_GROUP_TYPE_PROCEDURAL_PRIMITIVE;
HitGroupDesc.ClosestHitShaderImport = AddDxilLib(ProcHitShader.pClosestHitShader, nullptr);
Expand All @@ -223,14 +224,14 @@ void BuildRTPipelineDescription(const RayTracingPipelineStateCreateInfo& CreateI

constexpr Uint32 DefaultPayloadSize = sizeof(float) * 8;

auto& PipelineConfig = *TempPool.Construct<D3D12_RAYTRACING_PIPELINE_CONFIG>();
D3D12_RAYTRACING_PIPELINE_CONFIG& PipelineConfig = *TempPool.Construct<D3D12_RAYTRACING_PIPELINE_CONFIG>();

PipelineConfig.MaxTraceRecursionDepth = CreateInfo.RayTracingPipeline.MaxRecursionDepth;
Subobjects.push_back({D3D12_STATE_SUBOBJECT_TYPE_RAYTRACING_PIPELINE_CONFIG, &PipelineConfig});

auto& ShaderConfig = *TempPool.Construct<D3D12_RAYTRACING_SHADER_CONFIG>();
ShaderConfig.MaxAttributeSizeInBytes = CreateInfo.MaxAttributeSize == 0 ? D3D12_RAYTRACING_MAX_ATTRIBUTE_SIZE_IN_BYTES : CreateInfo.MaxAttributeSize;
ShaderConfig.MaxPayloadSizeInBytes = CreateInfo.MaxPayloadSize == 0 ? DefaultPayloadSize : CreateInfo.MaxPayloadSize;
D3D12_RAYTRACING_SHADER_CONFIG& ShaderConfig = *TempPool.Construct<D3D12_RAYTRACING_SHADER_CONFIG>();
ShaderConfig.MaxAttributeSizeInBytes = CreateInfo.MaxAttributeSize == 0 ? D3D12_RAYTRACING_MAX_ATTRIBUTE_SIZE_IN_BYTES : CreateInfo.MaxAttributeSize;
ShaderConfig.MaxPayloadSizeInBytes = CreateInfo.MaxPayloadSize == 0 ? DefaultPayloadSize : CreateInfo.MaxPayloadSize;
Subobjects.push_back({D3D12_STATE_SUBOBJECT_TYPE_RAYTRACING_SHADER_CONFIG, &ShaderConfig});
#undef LOG_PSO_ERROR_AND_THROW
}
Expand Down
22 changes: 12 additions & 10 deletions Graphics/GraphicsEngineVulkan/src/VulkanTypeConversions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1371,23 +1371,25 @@ static RESOURCE_STATE VkAccessFlagToResourceStates(VkAccessFlagBits AccessFlagBi
class VkAccessFlagBitPosToResourceState
{
public:
VkAccessFlagBitPosToResourceState()
{
for (Uint32 bit = 0; bit < FlagBitPosToResourceState.size(); ++bit)
{
FlagBitPosToResourceState[bit] = VkAccessFlagToResourceStates(static_cast<VkAccessFlagBits>(1 << bit));
}
}

RESOURCE_STATE operator()(Uint32 BitPos) const
{
VERIFY(BitPos <= MaxFlagBitPos, "Resource state flag bit position (", BitPos, ") exceeds max bit position (", Uint32{MaxFlagBitPos}, ")");
return FlagBitPosToResourceState[BitPos];
}

private:
static constexpr const Uint32 MaxFlagBitPos = 20;
std::array<RESOURCE_STATE, MaxFlagBitPos + 1> FlagBitPosToResourceState;
static constexpr const Uint32 MaxFlagBitPos = 20;

const std::array<RESOURCE_STATE, MaxFlagBitPos + 1> FlagBitPosToResourceState{
[]() {
std::array<RESOURCE_STATE, MaxFlagBitPos + 1> BitPosToState;
for (Uint32 bit = 0; bit < BitPosToState.size(); ++bit)
{
BitPosToState[bit] = VkAccessFlagToResourceStates(static_cast<VkAccessFlagBits>(1 << bit));
}
return BitPosToState;
}(),
};
};


Expand Down
1 change: 1 addition & 0 deletions Graphics/ShaderTools/src/GLSLParsingTools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ static TEXTURE_FORMAT ParseStandardGLSLImageFormat(const std::string& Format)
case 2: return TEX_FORMAT_RG32_FLOAT;
case 3: return TEX_FORMAT_RGB32_FLOAT;
case 4: return TEX_FORMAT_RGBA32_FLOAT;
default: return TEX_FORMAT_UNKNOWN;
}

default: return TEX_FORMAT_UNKNOWN;
Expand Down

0 comments on commit 8b57210

Please sign in to comment.