Added support to call not call UseResources on samee resource multiple times.

This commit is contained in:
moudgils
2021-06-13 23:36:56 -07:00
parent 4e5d690584
commit f8f282998b
4 changed files with 17 additions and 25 deletions
@@ -400,15 +400,13 @@ namespace AZ
id<MTLResource> mtlconstantBufferResource = m_constantBuffer.GetGpuAddress<id<MTLResource>>();
if(RHI::CheckBitsAny(srgResourcesVisInfo.m_constantDataStageMask, RHI::ShaderStageMask::Compute))
{
uint16_t arrayIndex = resourcesToMakeResidentCompute[MTLResourceUsageRead].m_resourceArrayLen++;
resourcesToMakeResidentCompute[MTLResourceUsageRead].m_resourceArray[arrayIndex] = mtlconstantBufferResource;
resourcesToMakeResidentCompute[MTLResourceUsageRead].emplace(mtlconstantBufferResource);
}
else
{
MTLRenderStages mtlRenderStages = GetRenderStages(srgResourcesVisInfo.m_constantDataStageMask);
AZStd::pair <MTLResourceUsage,MTLRenderStages> key = AZStd::make_pair(MTLResourceUsageRead, mtlRenderStages);
uint16_t arrayIndex = resourcesToMakeResidentGraphics[key].m_resourceArrayLen++;
resourcesToMakeResidentGraphics[key].m_resourceArray[arrayIndex] = mtlconstantBufferResource;
AZStd::pair <MTLResourceUsage,MTLRenderStages> key = AZStd::make_pair(MTLResourceUsageRead, mtlRenderStages);
resourcesToMakeResidentGraphics[key].emplace(mtlconstantBufferResource);
}
}
}
@@ -440,16 +438,18 @@ namespace AZ
//Call UseResource on all resources for Compute stage
for (const auto& key : resourcesToMakeResidentCompute)
{
[static_cast<id<MTLComputeCommandEncoder>>(commandEncoder) useResources: key.second.m_resourceArray.data()
count: key.second.m_resourceArrayLen
AZStd::vector<id <MTLResource>> resourcesToProcessVec(key.second.begin(), key.second.end());
[static_cast<id<MTLComputeCommandEncoder>>(commandEncoder) useResources: &resourcesToProcessVec[0]
count: resourcesToProcessVec.size()
usage: key.first];
}
//Call UseResource on all resources for Vertex and Fragment stages
for (const auto& key : resourcesToMakeResidentGraphics)
{
[static_cast<id<MTLRenderCommandEncoder>>(commandEncoder) useResources: key.second.m_resourceArray.data()
count: key.second.m_resourceArrayLen
AZStd::vector<id <MTLResource>> resourcesToProcessVec(key.second.begin(), key.second.end());
[static_cast<id<MTLRenderCommandEncoder>>(commandEncoder) useResources: &resourcesToProcessVec[0]
count: resourcesToProcessVec.size()
usage: key.first.first
stages: key.first.second];
}
@@ -480,9 +480,9 @@ namespace AZ
AZ_Assert(false, "Undefined Resource type");
}
}
uint16_t arrayIndex = resourcesToMakeResidentMap[resourceUsage].m_resourceArrayLen++;
id<MTLResource> mtlResourceToBind = resourceBindingData.m_resourcPtr->GetGpuAddress<id<MTLResource>>();
resourcesToMakeResidentMap[resourceUsage].m_resourceArray[arrayIndex] = mtlResourceToBind;
resourcesToMakeResidentMap[resourceUsage].emplace(mtlResourceToBind);
}
}
@@ -516,9 +516,8 @@ namespace AZ
}
AZStd::pair <MTLResourceUsage, MTLRenderStages> key = AZStd::make_pair(resourceUsage, mtlRenderStages);
uint16_t arrayIndex = resourcesToMakeResidentMap[key].m_resourceArrayLen++;
id<MTLResource> mtlResourceToBind = resourceBindingData.m_resourcPtr->GetGpuAddress<id<MTLResource>>();
resourcesToMakeResidentMap[key].m_resourceArray[arrayIndex] = mtlResourceToBind;
resourcesToMakeResidentMap[key].emplace(mtlResourceToBind);
}
}
}
@@ -120,15 +120,10 @@ namespace AZ
ResourceBindingsMap m_resourceBindings;
static const int MaxEntriesInArgTable = 31;
struct MetalResourceArray
{
AZStd::array<id <MTLResource>, MaxEntriesInArgTable> m_resourceArray;
uint16_t m_resourceArrayLen = 0;
};
//Map to cache all the resources based on the usage as we can batch all the resources for a given usage
using ComputeResourcesToMakeResidentMap = AZStd::unordered_map<MTLResourceUsage, MetalResourceArray>;
//Map to cache all the resources based on the usage and shader stage as we can batch all the resources for a given usage/shader usage
using GraphicsResourcesToMakeResidentMap = AZStd::unordered_map<AZStd::pair<MTLResourceUsage,MTLRenderStages>, MetalResourceArray>;
//Map to cache all the resources based on the usage as we can batch all the resources for a given usage.
using ComputeResourcesToMakeResidentMap = AZStd::unordered_map<MTLResourceUsage, AZStd::unordered_set<id <MTLResource>>>;
//Map to cache all the resources based on the usage and shader stage as we can batch all the resources for a given usage/shader usage.
using GraphicsResourcesToMakeResidentMap = AZStd::unordered_map<AZStd::pair<MTLResourceUsage,MTLRenderStages>, AZStd::unordered_set<id <MTLResource>>>;
void CollectResourcesForCompute(id<MTLCommandEncoder> encoder,
const ResourceBindingsSet& resourceBindingData,
@@ -42,7 +42,6 @@ namespace AZ
uploadRequest.m_attachmentBuffer = buffer;
uploadRequest.m_byteOffset = buffer->GetMemoryView().GetOffset() + request.m_byteOffset;
uploadRequest.m_stagingBuffer = stagingBuffer;
uploadRequest.m_byteSize = request.m_byteCount;
return stagingBuffer->GetMemoryView().GetCpuAddress();
}
@@ -72,7 +71,7 @@ namespace AZ
copyDescriptor.m_sourceOffset = stagingBuffer->GetMemoryView().GetOffset();
copyDescriptor.m_destinationBuffer = destBuffer;
copyDescriptor.m_destinationOffset = static_cast<uint32_t>(packet.m_byteOffset);
copyDescriptor.m_size = static_cast<uint32_t>(packet.m_byteSize);
copyDescriptor.m_size = stagingBuffer->GetMemoryView().GetSize();
commandList.Submit(RHI::CopyItem(copyDescriptor));
device.QueueForRelease(stagingBuffer->GetMemoryView());
@@ -54,7 +54,6 @@ namespace AZ
Buffer* m_attachmentBuffer = nullptr;
RHI::Ptr<Buffer> m_stagingBuffer;
size_t m_byteOffset = 0;
size_t m_byteSize = 0;
};
AZStd::mutex m_uploadPacketsLock;