{"id":4928,"date":"2020-08-31T04:00:54","date_gmt":"2020-08-31T11:00:54","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/pix\/?p=4928"},"modified":"2020-09-01T09:55:07","modified_gmt":"2020-09-01T16:55:07","slug":"gpu-captures-how-we-support-placed-and-reserved-resources","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/pix\/gpu-captures-how-we-support-placed-and-reserved-resources\/","title":{"rendered":"GPU Captures: How we support placed and reserved resources"},"content":{"rendered":"<h3>Quick Links<\/h3>\n<ul>\n<li><a href=\"#_Toc47365040\">Summary<\/a><\/li>\n<li><a href=\"#_Toc47365041\">Recap: placed and reserved resources in D3D12<\/a><\/li>\n<li><a href=\"#_Toc47365042\">Why are placed and reserved resources difficult for PIX to capture?<\/a><\/li>\n<li><a href=\"#_Toc47365043\">How does PIX capture placed and reserved resources?<\/a><\/li>\n<li><a href=\"#_Toc47365044\">When does PIX need help capturing placed and reserved resources?<\/a><\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<h3><a name=\"_Toc47365040\"><\/a>Summary<\/h3>\n<ul>\n<li>PIX on Windows supports placed and reserved (a.k.a. tiled) resources.<\/li>\n<li>For most applications, PIX will \u201cjust work\u201d, and it will capture\/replay placed and reserved resources correctly.<\/li>\n<li>In some cases, PIX needs hints from the application to correctly capture placed and reserved resources.<\/li>\n<li>The best way you can help PIX is to use an <a href=\"https:\/\/docs.microsoft.com\/en-us\/windows\/win32\/api\/d3d12\/nf-d3d12-id3d12graphicscommandlist-resourcebarrier#remarks\">aliasing barrier<\/a> (with a non-NULL \u201cafter\u201d resource) whenever your application starts using a placed and\/or reserved resource that overlaps with a previously-used placed\/reserved resource. <\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<h3><a name=\"_Toc47365041\"><\/a>Recap: placed and reserved resources in D3D12<\/h3>\n<p>In D3D12, there are <a href=\"https:\/\/docs.microsoft.com\/en-us\/windows\/win32\/direct3d12\/uploading-resources#resources\">three types of resources<\/a>:<\/p>\n<ul>\n<li>Committed<\/li>\n<li>Placed<\/li>\n<li>Reserved (also known as \u201cTiled\u201d)<\/li>\n<\/ul>\n<p>A committed resource has its own dedicated backing memory that isn\u2019t shared with any other resource. This makes them relatively easy for PIX to capture, so we won\u2019t discuss them here.<\/p>\n<p>Placed and reserved resources don\u2019t have their own dedicated backing memory. Instead, applications must create ID3D12Heap objects that own the backing memory, and applications must define \u201cmappings\u201d between placed\/reserved resources and the backing memory in heaps.<\/p>\n<p>A reserved resource allows applications to map regions of the resource to regions of heaps. Different parts of a reserved resource can be mapped to different heaps. These mappings can be updated at any point in time via the <a href=\"https:\/\/docs.microsoft.com\/en-us\/windows\/win32\/api\/d3d12\/nf-d3d12-id3d12commandqueue-updatetilemappings\">ID3D12CommandQueue::UpdateTileMappings()<\/a> API.<\/p>\n<p>A placed resource is conceptually similar to a reserved resource with one mapping that can never be changed. Applications pass a heap and an offset into CreatePlacedResource(), and the application can\u2019t change the heap or offset without destroying and recreating the resource.<\/p>\n<p>&nbsp;<\/p>\n<h3><a name=\"_Toc47365042\"><\/a>Why are placed and reserved resources difficult for PIX to capture?<\/h3>\n<p>In D3D12, it\u2019s legal for multiple placed and\/or reserved resources to be mapped into the same part of a heap at once. These resources are also known as \u201caliased\u201d or \u201coverlapping\u201d resources. It\u2019s difficult for PIX on Windows to correctly capture overlapping resources, especially on some hardware.<\/p>\n<p>In an ideal world, PIX would just serialize the following into its capture file:<\/p>\n<ul>\n<li>The creation parameters for each ID3D12Resource and ID3D12Heap<\/li>\n<li>The mappings between ID3D12Resources and ID3D12Heaps<\/li>\n<li>The contents of ID3D12Heaps<\/li>\n<\/ul>\n<p>Unfortunately, step 3 isn\u2019t possible on a lot of modern GPUs. One reason is that some GPUs store additional compression metadata for placed\/reserved resources in memory that wouldn\u2019t be captured if PIX used this approach. Without this metadata, PIX wouldn\u2019t correctly recreate the application\u2019s resources and heaps at replay time.<\/p>\n<p>A better approach would be for PIX to serialize the following into its capture file:<\/p>\n<ul>\n<li>The creation parameters for each ID3D12Resource and ID3D12Heap<\/li>\n<li>The mappings between ID3D12Resources and ID3D12Heaps<\/li>\n<li>The contents of all ID3D12Resources (including overlapping resources)<\/li>\n<\/ul>\n<p>This approach is still problematic though, because data inheritance between overlapping resources isn\u2019t deterministic on some hardware. This means that PIX can\u2019t capture the contents of multiple overlapping resources and recreate them at replay time without potentially corrupting some of them. Even worse, if PIX reads back the wrong placed resource at capture time then it could trigger a hardware decompression that invalidates the compression metadata of a different overlapping placed resource that the application may be using. This could cause corruption at capture time.<\/p>\n<p>&nbsp;<\/p>\n<h3><a name=\"_Toc47365043\"><\/a>How does PIX capture placed and reserved resources?<\/h3>\n<p>PIX on Windows relies on a modified version of the \u201cSimple Model\u201d described <a href=\"https:\/\/docs.microsoft.com\/en-us\/windows\/win32\/api\/d3d12\/nf-d3d12-id3d12device-createplacedresource#remarks\">here<\/a>.<\/p>\n<p>In this modified model, PIX re-uses the concept of \u201cactive\u201d and \u201cinactive\u201d resources. There can only ever be one \u201cactive\u201d resource at any part of an ID3D12Heap at any point in time. If an application starts using a resource (e.g. as a render target), then that resource becomes \u201cactive\u201d and all other overlapping resources are marked as \u201cinactive\u201d. PIX tracks which resources are active at any given point in time.<\/p>\n<p>PIX then serializes the following into its capture file:<\/p>\n<ul>\n<li>The creation parameters for each ID3D12Resource and ID3D12Heap<\/li>\n<li>The mappings between ID3D12Resources and ID3D12Heaps<\/li>\n<li>The contents of all buffer resources <em>(buffer resources don\u2019t have compression metadata, so they aren\u2019t susceptible to the corruption issues described above)<\/em><\/li>\n<li>The contents of all \u201cactive\u201d texture resources\/subresources<\/li>\n<\/ul>\n<p>This means that PIX won\u2019t capture the contents of any \u201cinactive\u201d texture resources\/subresources.<\/p>\n<p>Resources are activated when they\u2019re used as an API parameter to most command list APIs. For example, the source and destination resource will be activated when a Copy() operation in a command list is executed on the GPU. One exception is transition ResourceBarrier()s: transitioning a resource from one state to another does not activate that resource.<\/p>\n<p>As-of August 2020, PIX tracks active\/inactive placed resources at the resource level, but it tracks active\/inactive reserved resources at the subresource level. For example, if a reserved resource has 3 mip levels, then it\u2019s possible for mips 0 and 2 to be active while mip 1 is inactive. If capture is triggered in this state, then PIX will capture the contents of mips 0 and 2 but not mip 1. If a placed resource has 3 mip levels then all 3 mips will always have the same active\/inactive state.<\/p>\n<p>&nbsp;<\/p>\n<h3><a name=\"_Toc47365044\"><\/a>When does PIX need help capturing placed and reserved resources?<\/h3>\n<p>For most applications we\u2019ve seen, PIX on Windows can correctly capture and replay their placed\/reserved resource usage without any changes to the application.<\/p>\n<p>Here are some examples of placed\/reserved resource usage that may not \u201cjust work\u201d in PIX on Windows:<\/p>\n<ul>\n<li><strong>Applications that aren\u2019t correctly initializing their placed\/reserved render targets (RT) and depth stencil (DS) resources after switching between overlapping resources<\/strong>. In D3D12, these resources must be <a href=\"https:\/\/docs.microsoft.com\/en-us\/windows\/win32\/api\/d3d12\/nf-d3d12-id3d12device-createplacedresource#notes-on-the-required-resource-initialization\">explicitly initialized<\/a> with a Clear(), Copy(), or DiscardResource() operation after the switch. If an application does this correctly, then PIX will see these initialization operations and it will use them to correctly track active resources. This rule isn\u2019t enforced by the D3D12 debug layer though (as-of August 2020), so it is possible that a D3D12 application is breaking the rule without realizing it. In these cases, please make your application initialize the resources correctly.<\/li>\n<li><strong>Applications that are switching between overlapping non-RT\/DS placed\/reserved resources without using the resources in a command list API.<\/strong> For example, an application could create overlapping placed UAVs, create a descriptor to each UAV in a descriptor heap, and then dynamically index into the descriptor heap at runtime to choose which UAV to write to. In this scenario, it would be too computationally expensive for PIX to track the active UAV at capture time while maintaining a reasonable framerate. Note that transitioning the UAV to another state (e.g. SRV) is not enough to activate the resource. In these cases, PIX relies on hints from the application to help it track which resources are active. The best way to do this is to use an aliasing barrier with a non-NULL \u201cafter\u201d resource.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Quick Links Summary Recap: placed and reserved resources in D3D12 Why are placed and reserved resources difficult for PIX to capture? How does PIX capture placed and reserved resources? When does PIX need help capturing placed and reserved resources? &nbsp; Summary PIX on Windows supports placed and reserved (a.k.a. tiled) resources. For most applications, PIX [&hellip;]<\/p>\n","protected":false},"author":1899,"featured_media":4769,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-4928","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-pix"],"acf":[],"blog_post_summary":"<p>Quick Links Summary Recap: placed and reserved resources in D3D12 Why are placed and reserved resources difficult for PIX to capture? How does PIX capture placed and reserved resources? When does PIX need help capturing placed and reserved resources? &nbsp; Summary PIX on Windows supports placed and reserved (a.k.a. tiled) resources. For most applications, PIX [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/pix\/wp-json\/wp\/v2\/posts\/4928","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/pix\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/pix\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/pix\/wp-json\/wp\/v2\/users\/1899"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/pix\/wp-json\/wp\/v2\/comments?post=4928"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/pix\/wp-json\/wp\/v2\/posts\/4928\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/pix\/wp-json\/wp\/v2\/media\/4769"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/pix\/wp-json\/wp\/v2\/media?parent=4928"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/pix\/wp-json\/wp\/v2\/categories?post=4928"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/pix\/wp-json\/wp\/v2\/tags?post=4928"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}