Skip to content

Commit

Permalink
Gracefully handle dynamic buffer map failures
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMostDiligent committed Nov 24, 2024
1 parent 6ce3727 commit f46ff22
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 68 deletions.
12 changes: 7 additions & 5 deletions Components/src/BoundBoxRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -214,11 +214,13 @@ void BoundBoxRenderer::Prepare(IDeviceContext* pContext, const RenderAttribs& At
}
else
{
MapHelper<BoundBoxShaderAttribs> BBAttribs{pContext, m_RenderAttribsCB, MAP_WRITE, MAP_FLAG_DISCARD};
WriteShaderMatrix(&BBAttribs->Transform, *Attribs.BoundBoxTransform, !m_PackMatrixRowMajor);
BBAttribs->Color = Attribs.Color != nullptr ? *Attribs.Color : float4{1.0, 1.0, 1.0, 1.0};
BBAttribs->PatternLength = Attribs.PatternLength;
BBAttribs->PatternMask = Attribs.PatternMask;
if (MapHelper<BoundBoxShaderAttribs> BBAttribs{pContext, m_RenderAttribsCB, MAP_WRITE, MAP_FLAG_DISCARD})
{
WriteShaderMatrix(&BBAttribs->Transform, *Attribs.BoundBoxTransform, !m_PackMatrixRowMajor);
BBAttribs->Color = Attribs.Color != nullptr ? *Attribs.Color : float4{1.0, 1.0, 1.0, 1.0};
BBAttribs->PatternLength = Attribs.PatternLength;
BBAttribs->PatternMask = Attribs.PatternMask;
}
}
}

Expand Down
6 changes: 4 additions & 2 deletions Components/src/CoordinateGridRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,10 @@ void CoordinateGridRenderer::Render(const RenderAttributes& RenderAttribs)
m_Resources.Insert(RESOURCE_IDENTIFIER_CAMERA_CONSTANT_BUFFER, pBuffer);
}

MapHelper<HLSL::CameraAttribs> CameraAttibs{RenderAttribs.pDeviceContext, m_Resources[RESOURCE_IDENTIFIER_CAMERA_CONSTANT_BUFFER], MAP_WRITE, MAP_FLAG_DISCARD};
*CameraAttibs = *RenderAttribs.pCamera;
if (MapHelper<HLSL::CameraAttribs> CameraAttibs{RenderAttribs.pDeviceContext, m_Resources[RESOURCE_IDENTIFIER_CAMERA_CONSTANT_BUFFER], MAP_WRITE, MAP_FLAG_DISCARD})
{
*CameraAttibs = *RenderAttribs.pCamera;
}
}
else
{
Expand Down
12 changes: 7 additions & 5 deletions Components/src/EnvMapRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,11 +237,13 @@ void EnvMapRenderer::Prepare(IDeviceContext* pContext,
}
else
{
MapHelper<EnvMapShaderAttribs> EnvMapAttribs{pContext, m_RenderAttribsCB, MAP_WRITE, MAP_FLAG_DISCARD};
EnvMapAttribs->ToneMapping = ToneMapping;
EnvMapAttribs->AverageLogLum = Attribs.AverageLogLum;
EnvMapAttribs->MipLevel = Attribs.MipLevel;
EnvMapAttribs->Alpha = Attribs.Alpha;
if (MapHelper<EnvMapShaderAttribs> EnvMapAttribs{pContext, m_RenderAttribsCB, MAP_WRITE, MAP_FLAG_DISCARD})
{
EnvMapAttribs->ToneMapping = ToneMapping;
EnvMapAttribs->AverageLogLum = Attribs.AverageLogLum;
EnvMapAttribs->MipLevel = Attribs.MipLevel;
EnvMapAttribs->Alpha = Attribs.Alpha;
}
}
}

Expand Down
36 changes: 19 additions & 17 deletions Components/src/ShadowMapManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -556,25 +556,27 @@ void ShadowMapManager::ConvertToFilterable(IDeviceContext* pCtx, const ShadowMap
float fEVSMNegativeExponent;
int Is32BitEVSM;
};
MapHelper<ConversionAttribs> pAttribs(pCtx, m_pConversionAttribsBuffer, MAP_WRITE, MAP_FLAG_DISCARD);
pAttribs->iCascade = i;
if (ShadowAttribs.iFixedFilterSize > 0)
if (MapHelper<ConversionAttribs> pAttribs{pCtx, m_pConversionAttribsBuffer, MAP_WRITE, MAP_FLAG_DISCARD})
{
pAttribs->fHorzFilterRadius = static_cast<float>(iFilterRadius);
pAttribs->fVertFilterRadius = static_cast<float>(iFilterRadius);
}
else
{
const auto& Cascade = ShadowAttribs.Cascades[i];
float fNDCtoUVScale = 0.5f;
float fFilterWidth = ShadowAttribs.fFilterWorldSize * Cascade.f4LightSpaceScale.x * fNDCtoUVScale;
float fFilterHeight = ShadowAttribs.fFilterWorldSize * Cascade.f4LightSpaceScale.y * fNDCtoUVScale;
pAttribs->fHorzFilterRadius = fFilterWidth / 2.f * static_cast<float>(ShadowMapDesc.Width);
pAttribs->fVertFilterRadius = fFilterHeight / 2.f * static_cast<float>(ShadowMapDesc.Height);
pAttribs->iCascade = i;
if (ShadowAttribs.iFixedFilterSize > 0)
{
pAttribs->fHorzFilterRadius = static_cast<float>(iFilterRadius);
pAttribs->fVertFilterRadius = static_cast<float>(iFilterRadius);
}
else
{
const auto& Cascade = ShadowAttribs.Cascades[i];
float fNDCtoUVScale = 0.5f;
float fFilterWidth = ShadowAttribs.fFilterWorldSize * Cascade.f4LightSpaceScale.x * fNDCtoUVScale;
float fFilterHeight = ShadowAttribs.fFilterWorldSize * Cascade.f4LightSpaceScale.y * fNDCtoUVScale;
pAttribs->fHorzFilterRadius = fFilterWidth / 2.f * static_cast<float>(ShadowMapDesc.Width);
pAttribs->fVertFilterRadius = fFilterHeight / 2.f * static_cast<float>(ShadowMapDesc.Height);
}
pAttribs->fEVSMPositiveExponent = ShadowAttribs.fEVSMPositiveExponent;
pAttribs->fEVSMNegativeExponent = ShadowAttribs.fEVSMNegativeExponent;
pAttribs->Is32BitEVSM = ShadowAttribs.bIs32BitEVSM;
}
pAttribs->fEVSMPositiveExponent = ShadowAttribs.fEVSMPositiveExponent;
pAttribs->fEVSMNegativeExponent = ShadowAttribs.fEVSMNegativeExponent;
pAttribs->Is32BitEVSM = ShadowAttribs.bIs32BitEVSM;
}
pCtx->SetPipelineState(Tech.PSO);
pCtx->CommitShaderResources(Tech.SRB, RESOURCE_STATE_TRANSITION_MODE_TRANSITION);
Expand Down
2 changes: 1 addition & 1 deletion Components/src/VectorFieldRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,8 @@ void VectorFieldRenderer::Render(const RenderAttribs& Attribs)

m_pVectorFieldVar->Set(Attribs.pVectorField);

if (MapHelper<HLSL::VectorFieldRenderAttribs> AttribsData{Attribs.pContext, m_RenderAttribsCB, MAP_WRITE, MAP_FLAG_DISCARD})
{
MapHelper<HLSL::VectorFieldRenderAttribs> AttribsData{Attribs.pContext, m_RenderAttribsCB, MAP_WRITE, MAP_FLAG_DISCARD};
AttribsData->ScaleAndBias = {Attribs.Scale, Attribs.Bias};
AttribsData->f2GridSize = {static_cast<float>(Attribs.GridSize.x), static_cast<float>(Attribs.GridSize.y)};
AttribsData->u2GridSize = {Attribs.GridSize.x, Attribs.GridSize.y};
Expand Down
8 changes: 5 additions & 3 deletions Hydrogent/src/Tasks/HnProcessSelectionTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -342,8 +342,10 @@ void HnProcessSelectionTask::Execute(pxr::HdTaskContext* TaskCtx)
UNEXPECTED("Background depth is not set in the task context");
}

MapHelper<HLSL::ClosestSelectedLocationConstants> Constants{pCtx, m_ConstantsCB, MAP_WRITE, MAP_FLAG_DISCARD};
Constants->ClearDepth = BackgroundDepth;
if (MapHelper<HLSL::ClosestSelectedLocationConstants> Constants{pCtx, m_ConstantsCB, MAP_WRITE, MAP_FLAG_DISCARD})
{
Constants->ClearDepth = BackgroundDepth;
}
}

ITextureView* ClosestSelectedLocationRTVs[] = {Targets->ClosestSelectedLocationRTV[0], Targets->ClosestSelectedLocationRTV[1]};
Expand All @@ -354,8 +356,8 @@ void HnProcessSelectionTask::Execute(pxr::HdTaskContext* TaskCtx)

for (Uint32 i = 0; i < m_NumJFIterations; ++i)
{
if (MapHelper<HLSL::ClosestSelectedLocationConstants> Constants{pCtx, m_ConstantsCB, MAP_WRITE, MAP_FLAG_DISCARD})
{
MapHelper<HLSL::ClosestSelectedLocationConstants> Constants{pCtx, m_ConstantsCB, MAP_WRITE, MAP_FLAG_DISCARD};
Constants->SampleRange = static_cast<float>(1 << (m_NumJFIterations - 1 - i));
}

Expand Down
15 changes: 8 additions & 7 deletions PBR/src/GLTF_PBR_Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -632,15 +632,16 @@ void GLTF_PBR_Renderer::Render(IDeviceContext* pCtx,

if ((PSOFlags & PSO_FLAG_USE_JOINTS) != 0)
{
MapHelper<float4x4> pJoints{pCtx, m_JointsBuffer, MAP_WRITE, MAP_FLAG_DISCARD};

WriteSkinningDataAttribs WriteSkinningAttribs{PSOFlags, static_cast<Uint32>(JointCount)};
WriteSkinningAttribs.JointMatrices = JointMatrices.data();
if ((PSOFlags & PSO_FLAG_COMPUTE_MOTION_VECTORS) != 0)
if (MapHelper<float4x4> pJoints{pCtx, m_JointsBuffer, MAP_WRITE, MAP_FLAG_DISCARD})
{
WriteSkinningAttribs.PrevJointMatrices = PrevTransforms->Skins[Node.SkinTransformsIndex].JointMatrices.data();
WriteSkinningDataAttribs WriteSkinningAttribs{PSOFlags, static_cast<Uint32>(JointCount)};
WriteSkinningAttribs.JointMatrices = JointMatrices.data();
if ((PSOFlags & PSO_FLAG_COMPUTE_MOTION_VECTORS) != 0)
{
WriteSkinningAttribs.PrevJointMatrices = PrevTransforms->Skins[Node.SkinTransformsIndex].JointMatrices.data();
}
WriteSkinningData(pJoints, WriteSkinningAttribs);
}
WriteSkinningData(pJoints, WriteSkinningAttribs);
}
}

Expand Down
30 changes: 17 additions & 13 deletions PBR/src/PBR_Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -814,12 +814,14 @@ void PBR_Renderer::PrecomputeCubemaps(IDeviceContext* pCtx,
ProcessCubemapFaces(pCtx, pIrradianceCube, [&](ITextureView* pRTV, Uint32 mip, Uint32 face) {
VERIFY_EXPR(mip == 0);
{
MapHelper<PrecomputeEnvMapAttribs> Attribs{pCtx, m_PrecomputeEnvMapAttribsCB, MAP_WRITE, MAP_FLAG_DISCARD};
Attribs->Rotation = Matrices[face];
Attribs->EnvMapWidth = static_cast<float>(pEnvironmentMap->GetTexture()->GetDesc().Width);
Attribs->EnvMapHeight = static_cast<float>(pEnvironmentMap->GetTexture()->GetDesc().Height);
Attribs->EnvMapMipCount = static_cast<float>(pEnvironmentMap->GetTexture()->GetDesc().MipLevels);
Attribs->NumSamples = NumDiffuseSamples;
if (MapHelper<PrecomputeEnvMapAttribs> Attribs{pCtx, m_PrecomputeEnvMapAttribsCB, MAP_WRITE, MAP_FLAG_DISCARD})
{
Attribs->Rotation = Matrices[face];
Attribs->EnvMapWidth = static_cast<float>(pEnvironmentMap->GetTexture()->GetDesc().Width);
Attribs->EnvMapHeight = static_cast<float>(pEnvironmentMap->GetTexture()->GetDesc().Height);
Attribs->EnvMapMipCount = static_cast<float>(pEnvironmentMap->GetTexture()->GetDesc().MipLevels);
Attribs->NumSamples = NumDiffuseSamples;
}
}
DrawAttribs drawAttrs(4, DRAW_FLAG_VERIFY_ALL);
pCtx->Draw(drawAttrs);
Expand All @@ -834,13 +836,15 @@ void PBR_Renderer::PrecomputeCubemaps(IDeviceContext* pCtx,
auto* pPrefilteredEnvMap = m_pPrefilteredEnvMapSRV->GetTexture();
ProcessCubemapFaces(pCtx, pPrefilteredEnvMap, [&](ITextureView* pRTV, Uint32 mip, Uint32 face) {
{
MapHelper<PrecomputeEnvMapAttribs> Attribs{pCtx, m_PrecomputeEnvMapAttribsCB, MAP_WRITE, MAP_FLAG_DISCARD};
Attribs->Rotation = Matrices[face];
Attribs->Roughness = static_cast<float>(mip) / static_cast<float>(pPrefilteredEnvMap->GetDesc().MipLevels - 1);
Attribs->EnvMapWidth = static_cast<float>(pEnvironmentMap->GetTexture()->GetDesc().Width);
Attribs->EnvMapHeight = static_cast<float>(pEnvironmentMap->GetTexture()->GetDesc().Height);
Attribs->EnvMapMipCount = static_cast<float>(pEnvironmentMap->GetTexture()->GetDesc().MipLevels);
Attribs->NumSamples = NumSpecularSamples;
if (MapHelper<PrecomputeEnvMapAttribs> Attribs{pCtx, m_PrecomputeEnvMapAttribsCB, MAP_WRITE, MAP_FLAG_DISCARD})
{
Attribs->Rotation = Matrices[face];
Attribs->Roughness = static_cast<float>(mip) / static_cast<float>(pPrefilteredEnvMap->GetDesc().MipLevels - 1);
Attribs->EnvMapWidth = static_cast<float>(pEnvironmentMap->GetTexture()->GetDesc().Width);
Attribs->EnvMapHeight = static_cast<float>(pEnvironmentMap->GetTexture()->GetDesc().Height);
Attribs->EnvMapMipCount = static_cast<float>(pEnvironmentMap->GetTexture()->GetDesc().MipLevels);
Attribs->NumSamples = NumSpecularSamples;
}
}

DrawAttribs drawAttrs(4, DRAW_FLAG_VERIFY_ALL);
Expand Down
10 changes: 6 additions & 4 deletions PostProcess/Common/src/PostFXContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -311,9 +311,11 @@ void PostFXContext::Execute(const RenderAttributes& RenderAttribs)
m_Resources.Insert(RESOURCE_IDENTIFIER_CONSTANT_BUFFER, pBuffer);
}

MapHelper<HLSL::CameraAttribs> CameraAttibs{RenderAttribs.pDeviceContext, m_Resources[RESOURCE_IDENTIFIER_CONSTANT_BUFFER], MAP_WRITE, MAP_FLAG_DISCARD};
CameraAttibs[0] = *RenderAttribs.pCurrCamera;
CameraAttibs[1] = *RenderAttribs.pPrevCamera;
if (MapHelper<HLSL::CameraAttribs> CameraAttibs{RenderAttribs.pDeviceContext, m_Resources[RESOURCE_IDENTIFIER_CONSTANT_BUFFER], MAP_WRITE, MAP_FLAG_DISCARD})
{
CameraAttibs[0] = *RenderAttribs.pCurrCamera;
CameraAttibs[1] = *RenderAttribs.pPrevCamera;
}
}
else
{
Expand Down Expand Up @@ -573,8 +575,8 @@ void PostFXContext::ComputeBlueNoiseTexture(const RenderAttributes& RenderAttrib
}
else
{
if (MapHelper<Uint32> IndexBuffer{RenderAttribs.pDeviceContext, m_Resources[RESOURCE_IDENTIFIER_INDEX_BUFFER_INTERMEDIATE], MAP_WRITE, MAP_FLAG_DISCARD})
{
MapHelper<Uint32> IndexBuffer{RenderAttribs.pDeviceContext, m_Resources[RESOURCE_IDENTIFIER_INDEX_BUFFER_INTERMEDIATE], MAP_WRITE, MAP_FLAG_DISCARD};
IndexBuffer[0] = 3 * m_FrameDesc.Index + 0;
IndexBuffer[1] = 3 * m_FrameDesc.Index + 1;
IndexBuffer[2] = 3 * m_FrameDesc.Index + 2;
Expand Down
24 changes: 13 additions & 11 deletions PostProcess/EpipolarLightScattering/src/EpipolarLightScattering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1427,8 +1427,8 @@ void EpipolarLightScattering::Build1DMinMaxMipMap(int iCascadeIndex)
m_FrameAttribs.pDeviceContext->SetViewports(1, &VP, 0, 0);

// Set source and destination min/max data offsets:
if (MapHelper<MiscDynamicParams> pMiscDynamicParams{m_FrameAttribs.pDeviceContext, m_pcbMiscParams, MAP_WRITE, MAP_FLAG_DISCARD})
{
MapHelper<MiscDynamicParams> pMiscDynamicParams(m_FrameAttribs.pDeviceContext, m_pcbMiscParams, MAP_WRITE, MAP_FLAG_DISCARD);
pMiscDynamicParams->ui4SrcMinMaxLevelXOffset = uiPrevXOffset;
pMiscDynamicParams->ui4DstMinMaxLevelXOffset = uiXOffset;
pMiscDynamicParams->fCascadeInd = static_cast<float>(iCascadeIndex);
Expand Down Expand Up @@ -1560,8 +1560,8 @@ void EpipolarLightScattering::DoRayMarching(Uint32 uiMaxStepsAlongRay,
SRB_DEPENDENCY_AVERAGE_LUMINANCE_TEX;
}

if (MapHelper<MiscDynamicParams> pMiscDynamicParams{m_FrameAttribs.pDeviceContext, m_pcbMiscParams, MAP_WRITE, MAP_FLAG_DISCARD})
{
MapHelper<MiscDynamicParams> pMiscDynamicParams(m_FrameAttribs.pDeviceContext, m_pcbMiscParams, MAP_WRITE, MAP_FLAG_DISCARD);
pMiscDynamicParams->fMaxStepsAlongRay = static_cast<float>(uiMaxStepsAlongRay);
pMiscDynamicParams->fCascadeInd = static_cast<float>(iCascadeIndex);
}
Expand Down Expand Up @@ -1813,8 +1813,8 @@ void EpipolarLightScattering::UpdateAverageLuminance()
UpdateAverageLuminanceTech.PSODependencyFlags = PSO_DEPENDENCY_LIGHT_ADAPTATION;
}

if (MapHelper<MiscDynamicParams> pMiscDynamicParams{m_FrameAttribs.pDeviceContext, m_pcbMiscParams, MAP_WRITE, MAP_FLAG_DISCARD})
{
MapHelper<MiscDynamicParams> pMiscDynamicParams(m_FrameAttribs.pDeviceContext, m_pcbMiscParams, MAP_WRITE, MAP_FLAG_DISCARD);
pMiscDynamicParams->fElapsedTime = (float)m_FrameAttribs.dElapsedTime;
}

Expand Down Expand Up @@ -1952,8 +1952,8 @@ void EpipolarLightScattering::FixInscatteringAtDepthBreaks(Uint32
SRB_DEPENDENCY_AVERAGE_LUMINANCE_TEX;
}

if (MapHelper<MiscDynamicParams> pMiscDynamicParams{m_FrameAttribs.pDeviceContext, m_pcbMiscParams, MAP_WRITE, MAP_FLAG_DISCARD})
{
MapHelper<MiscDynamicParams> pMiscDynamicParams(m_FrameAttribs.pDeviceContext, m_pcbMiscParams, MAP_WRITE, MAP_FLAG_DISCARD);
pMiscDynamicParams->fMaxStepsAlongRay = static_cast<float>(uiMaxStepsAlongRay);
pMiscDynamicParams->fCascadeInd = static_cast<float>(m_PostProcessingAttribs.iFirstCascadeToRayMarch);
}
Expand Down Expand Up @@ -2310,9 +2310,10 @@ void EpipolarLightScattering::PrepareForNewFrame(FrameAttribs&
CreateUniformBuffer(m_FrameAttribs.pDevice, sizeof(CameraAttribs), "Camera attribs", &m_pcbCameraAttribs);
VERIFY_EXPR(StaleSRBDependencyFlags & SRB_DEPENDENCY_CAMERA_ATTRIBS);
}
MapHelper<CameraAttribs> CamAttribs(m_FrameAttribs.pDeviceContext, m_pcbCameraAttribs, MAP_WRITE, MAP_FLAG_DISCARD);
*CamAttribs = *m_FrameAttribs.pCameraAttribs;

if (MapHelper<CameraAttribs> CamAttribs{m_FrameAttribs.pDeviceContext, m_pcbCameraAttribs, MAP_WRITE, MAP_FLAG_DISCARD})
{
*CamAttribs = *m_FrameAttribs.pCameraAttribs;
}
m_FrameAttribs.pcbCameraAttribs = m_pcbCameraAttribs;
}

Expand All @@ -2323,9 +2324,10 @@ void EpipolarLightScattering::PrepareForNewFrame(FrameAttribs&
CreateUniformBuffer(m_FrameAttribs.pDevice, sizeof(LightAttribs), "Light attribs", &m_pcbLightAttribs);
VERIFY_EXPR(StaleSRBDependencyFlags & SRB_DEPENDENCY_LIGHT_ATTRIBS);
}
MapHelper<LightAttribs> LightAttribs(m_FrameAttribs.pDeviceContext, m_pcbLightAttribs, MAP_WRITE, MAP_FLAG_DISCARD);
*LightAttribs = *m_FrameAttribs.pLightAttribs;

if (MapHelper<LightAttribs> LightAttribs{m_FrameAttribs.pDeviceContext, m_pcbLightAttribs, MAP_WRITE, MAP_FLAG_DISCARD})
{
*LightAttribs = *m_FrameAttribs.pLightAttribs;
}
m_FrameAttribs.pcbLightAttribs = m_pcbLightAttribs;
}

Expand Down Expand Up @@ -2360,8 +2362,8 @@ void EpipolarLightScattering::PrepareForNewFrame(FrameAttribs&
}
}

if (MapHelper<EpipolarLightScatteringAttribs> pPPAttribsBuffData{m_FrameAttribs.pDeviceContext, m_pcbPostProcessingAttribs, MAP_WRITE, MAP_FLAG_DISCARD})
{
MapHelper<EpipolarLightScatteringAttribs> pPPAttribsBuffData(m_FrameAttribs.pDeviceContext, m_pcbPostProcessingAttribs, MAP_WRITE, MAP_FLAG_DISCARD);
memcpy(pPPAttribsBuffData, &m_PostProcessingAttribs, sizeof(m_PostProcessingAttribs));
}

Expand Down

0 comments on commit f46ff22

Please sign in to comment.