1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
|
/*
** Copyright (c) 2021 Advanced Micro Devices, Inc. All rights reserved.
**
** Permission is hereby granted, free of charge, to any person obtaining a
** copy of this software and associated documentation files (the "Software"),
** to deal in the Software without restriction, including without limitation
** the rights to use, copy, modify, merge, publish, distribute, sublicense,
** and/or sell copies of the Software, and to permit persons to whom the
** Software is furnished to do so, subject to the following conditions:
**
** The above copyright notice and this permission notice shall be included in
** all copies or substantial portions of the Software.
**
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
** LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
** FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
** DEALINGS IN THE SOFTWARE.
*/
#include "graphics/dx12_image_renderer.h"
#include <d3dcompiler.h>
#include <util/logging.h>
GFXRECON_BEGIN_NAMESPACE(gfxrecon)
GFXRECON_BEGIN_NAMESPACE(graphics)
//-----------------------------------------------------------------------------
/// Statically create a DX12ImageRenderer.
/// \param config A structure containing all of the necessary initialization
/// info. \returns A new DX12ImageRenderer instance.
//-----------------------------------------------------------------------------
std::unique_ptr<DX12ImageRenderer> DX12ImageRenderer::Create(const DX12ImageRendererConfig& config)
{
std::unique_ptr<DX12ImageRenderer> out(new DX12ImageRenderer());
if (out != nullptr)
{
if (out->Init(config) != S_OK)
{
GFXRECON_LOG_WARNING("DX12ImageRenderer initialization failed");
}
}
return std::move(out);
}
void DX12ImageRenderer::ConvertR8G8B8A8ToB8G8R8(std::vector<char>& data, UINT width, UINT height, UINT pitch)
{
uint32_t* pixel;
uint32_t r16, g16, b16, a16, b8g8r8a8;
for (UINT j = 0; j < height; j++)
{
for (UINT i = 0; i < width; i++)
{
pixel = (uint32_t*)((uint8_t*)&data[0] + i * 4 + j * pitch);
r16 = ((*pixel >> 0U) & 0xFFU);
g16 = ((*pixel >> 8U) & 0xFFU);
b16 = ((*pixel >> 16U) & 0xFFU);
a16 = ((*pixel >> 24U) & 0xFFU);
b8g8r8a8 = ((b16 << 0U) & 0xFFU) | ((g16 << 8U) & 0xFF00U) | ((r16 << 16U) & 0xFF0000U) |
((a16 << 24U) & 0xFF000000);
*pixel = b8g8r8a8;
}
}
}
void DX12ImageRenderer::ConvertR10G10B10A2ToB8G8R8(std::vector<char>& data, UINT width, UINT height, UINT pitch)
{
uint32_t* pixel;
uint32_t r16, g16, b16, a16, b8g8r8a8;
for (UINT j = 0; j < height; j++)
{
for (UINT i = 0; i < width; i++)
{
pixel = (uint32_t*)((uint8_t*)&data[0] + i * 4 + j * pitch);
r16 = ((*pixel >> 0U) & 0x3FFU) << 6U;
g16 = ((*pixel >> 10U) & 0x3FFU) << 6U;
b16 = ((*pixel >> 20U) & 0x3FFU) << 6U;
a16 = ((*pixel >> 30U) & 0x03U) << 14U;
b8g8r8a8 = ((b16 >> 8U) << 0U) | ((g16 >> 8U) << 8U) | ((r16 >> 8U) << 16U) | ((a16 >> 8U) << 24U);
*pixel = b8g8r8a8;
}
}
}
//-----------------------------------------------------------------------------
/// Constructor.
//-----------------------------------------------------------------------------
DX12ImageRenderer::DX12ImageRenderer() :
cmd_allocator_(nullptr), cmd_list_(nullptr), fence_(nullptr), fence_event_(0), fence_value_(0), staging_(nullptr),
staging_resource_size_(0)
{}
//-----------------------------------------------------------------------------
/// Destructor.
//-----------------------------------------------------------------------------
DX12ImageRenderer::~DX12ImageRenderer()
{
if (fence_event_)
{
CloseHandle(fence_event_);
}
}
//-----------------------------------------------------------------------------
/// Initialize all members needed by this rendering class.
/// \param config A structure containing all of the necessary initialization
/// info. \returns The result code for the initialization step.
//-----------------------------------------------------------------------------
HRESULT DX12ImageRenderer::Init(const DX12ImageRendererConfig& config)
{
config_ = std::make_unique<DX12ImageRendererConfig>(config);
// Create command allocator
HRESULT result =
config_->device->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(&cmd_allocator_));
// Create command list
if (result == S_OK)
{
result = config_->device->CreateCommandList(
0, D3D12_COMMAND_LIST_TYPE_DIRECT, cmd_allocator_.Get(), nullptr, IID_PPV_ARGS(&cmd_list_));
}
// Create synchronization objects
if (result == S_OK)
{
result = config_->device->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&fence_));
fence_value_ = 1;
// Create an event handle to use for frame synchronization
fence_event_ = CreateEvent(nullptr, FALSE, FALSE, nullptr);
}
if (result == S_OK)
{
// Command lists are created in the recording state, but there is nothing
// to record yet. The main loop expects it to be closed, so close it now.
result = cmd_list_->Close();
}
return result;
}
//-----------------------------------------------------------------------------
/// Convert a DX12 resource to a CPU-visible linear buffer of pixels.
/// The data is filled in a user - provided CpuImage struct.
/// IMPORTANT : Memory inside pImgOut is allocated on behalf of the caller, so
/// it is their responsibility to free it. \param pRes The Render Target
/// resource to capture image data for. \param prevState The previous state that
/// has been set on the resource. \param newWidth The width of the output image
/// data. \param newHeight The height of the output image data. \param format
/// The image format for the output image data. \param pImgOut A pointer to the
/// structure containing all capture image data. \param bFlipX Option used to
/// flip the image horizontally. \param bFlipY Option used to flip the image
/// vertically. \returns The result code of the capture operation.
//-----------------------------------------------------------------------------
HRESULT DX12ImageRenderer::CaptureImage(
ID3D12Resource* res, D3D12_RESOURCE_STATES prev_state, UINT width, UINT height, UINT pitch, DXGI_FORMAT format)
{
HRESULT result = E_FAIL;
if ((res != nullptr) && (width > 0) && (height > 0))
{
auto desc = res->GetDesc();
// Create temp assets used in this capture
result = cmd_allocator_->Reset();
if (result == S_OK)
{
result = cmd_list_->Reset(cmd_allocator_.Get(), nullptr);
}
// Render work
if (result == S_OK)
{
D3D12_TEXTURE_COPY_LOCATION src_location = { res, D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX };
src_location.SubresourceIndex = 0;
auto buffer_byte_size = pitch * height;
if (staging_resource_size_ != buffer_byte_size)
{
if (staging_ != nullptr)
{
staging_->Release();
}
result = CreateStagingResource(buffer_byte_size);
}
if (result == S_OK)
{
D3D12_TEXTURE_COPY_LOCATION dst_location = { staging_.Get(), D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT };
dst_location.PlacedFootprint.Footprint.Width = width;
dst_location.PlacedFootprint.Footprint.Height = height;
dst_location.PlacedFootprint.Footprint.Depth = 1;
dst_location.PlacedFootprint.Footprint.Format = format;
dst_location.PlacedFootprint.Footprint.RowPitch = pitch;
dst_location.PlacedFootprint.Offset = 0;
cmd_list_->CopyTextureRegion(&dst_location, 0, 0, 0, &src_location, nullptr);
D3D12_RESOURCE_BARRIER barrier{};
barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
barrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE;
barrier.Transition.pResource = res;
barrier.Transition.StateBefore = D3D12_RESOURCE_STATE_COPY_SOURCE;
barrier.Transition.StateAfter = prev_state;
barrier.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES;
cmd_list_->ResourceBarrier(1, &barrier);
// Execute the command list
result = cmd_list_->Close();
ID3D12CommandList* pp_command_lists[] = { cmd_list_.Get() };
config_->cmd_queue->ExecuteCommandLists(_countof(pp_command_lists), pp_command_lists);
WaitCmdListFinish();
}
}
}
return result;
}
HRESULT DX12ImageRenderer::CreateStagingResource(unsigned int buffer_byte_size)
{
D3D12_HEAP_PROPERTIES props{};
props.Type = D3D12_HEAP_TYPE_READBACK;
props.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN;
props.MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN;
props.CreationNodeMask = 1;
props.VisibleNodeMask = 1;
D3D12_RESOURCE_DESC descriptor{};
descriptor.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER;
descriptor.Alignment = 0;
descriptor.Width = buffer_byte_size;
descriptor.Height = 1;
descriptor.DepthOrArraySize = 1;
descriptor.MipLevels = 1;
descriptor.Format = DXGI_FORMAT_UNKNOWN;
descriptor.SampleDesc.Count = 1;
descriptor.SampleDesc.Quality = 0;
descriptor.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR;
descriptor.Flags = D3D12_RESOURCE_FLAG_NONE;
auto result = config_->device->CreateCommittedResource(
&props, D3D12_HEAP_FLAG_NONE, &descriptor, D3D12_RESOURCE_STATE_COPY_DEST, nullptr, IID_PPV_ARGS(&staging_));
if (result == S_OK)
{
staging_resource_size_ = buffer_byte_size;
}
return result;
}
void DX12ImageRenderer::WaitCmdListFinish()
{
// Signal and increment the fence value
const UINT64 fence_v = fence_value_;
HRESULT result = config_->cmd_queue->Signal(fence_.Get(), fence_v);
fence_value_++;
const UINT64 fenceCompletedVal = fence_->GetCompletedValue();
// Wait until the previous frame is finished
if (fenceCompletedVal < fence_v)
{
result = fence_->SetEventOnCompletion(fence_v, fence_event_);
WaitForSingleObject(fence_event_, INFINITE);
}
}
HRESULT
DX12ImageRenderer::RetrieveImageData(CpuImage* img_out, UINT width, UINT height, UINT pitch, DXGI_FORMAT format)
{
void* uav_data = nullptr;
D3D12_RANGE read_range = { 0, 0 };
auto result = staging_->Map(0, &read_range, &uav_data);
if (result == S_OK)
{
const UINT total_bytes = pitch * height;
img_out->pitch = pitch;
img_out->width = width;
img_out->height = height;
img_out->data.resize(total_bytes);
memcpy(&img_out->data[0], uav_data, total_bytes);
if (B8G8R8.find(format) != B8G8R8.end())
{
staging_->Unmap(0, nullptr);
}
else if (R10G10B10A2.find(format) != R10G10B10A2.end())
{
ConvertR10G10B10A2ToB8G8R8(img_out->data, width, height, pitch);
staging_->Unmap(0, nullptr);
}
else if (R8G8B8A8.find(format) != R8G8B8A8.end())
{
ConvertR8G8B8A8ToB8G8R8(img_out->data, width, height, pitch);
staging_->Unmap(0, nullptr);
}
else
{
auto entry = std::find(issued_warning_list_.begin(), issued_warning_list_.end(), format);
if (entry == issued_warning_list_.end())
{
issued_warning_list_.insert(entry, format);
GFXRECON_LOG_ERROR("DX12ImageRenderer does not support %d DXGI_FORMAT", format);
}
staging_->Unmap(0, nullptr);
}
}
return result;
}
GFXRECON_END_NAMESPACE(graphics)
GFXRECON_END_NAMESPACE(gfxrecon)
|