| 442 | } |
| 443 | |
| 444 | void StructuredBuffer::WriteToFile(const wchar* path, ID3D11Device* device, ID3D11DeviceContext* context) |
| 445 | { |
| 446 | Assert_(Buffer != nullptr); |
| 447 | |
| 448 | // Get the buffer info |
| 449 | D3D11_BUFFER_DESC desc; |
| 450 | Buffer->GetDesc(&desc); |
| 451 | |
| 452 | uint32 useAsUAV = (desc.BindFlags & D3D11_BIND_UNORDERED_ACCESS) ? 1 : 0; |
| 453 | |
| 454 | uint32 appendConsume = 0; |
| 455 | uint32 hiddenCounter = 0; |
| 456 | if(useAsUAV) |
| 457 | { |
| 458 | D3D11_UNORDERED_ACCESS_VIEW_DESC uavDesc; |
| 459 | UAView->GetDesc(&uavDesc); |
| 460 | appendConsume = (uavDesc.Format & D3D11_BUFFER_UAV_FLAG_APPEND) ? 1 : 0; |
| 461 | hiddenCounter =(uavDesc.Format & D3D11_BUFFER_UAV_FLAG_COUNTER) ? 1 : 0; |
| 462 | } |
| 463 | |
| 464 | // If the exists, delete it |
| 465 | if(FileExists(path)) |
| 466 | Win32Call(DeleteFile(path)); |
| 467 | |
| 468 | // Create the file |
| 469 | HANDLE fileHandle = CreateFile(path, GENERIC_WRITE, 0, nullptr, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, nullptr); |
| 470 | if(fileHandle == INVALID_HANDLE_VALUE) |
| 471 | Win32Call(false); |
| 472 | |
| 473 | // Write the buffer info |
| 474 | DWORD bytesWritten = 0; |
| 475 | Win32Call(WriteFile(fileHandle, &Size, 4, &bytesWritten, nullptr)); |
| 476 | Win32Call(WriteFile(fileHandle, &Stride, 4, &bytesWritten, nullptr)); |
| 477 | Win32Call(WriteFile(fileHandle, &NumElements, 4, &bytesWritten, nullptr)); |
| 478 | Win32Call(WriteFile(fileHandle, &useAsUAV, 4, &bytesWritten, nullptr)); |
| 479 | Win32Call(WriteFile(fileHandle, &hiddenCounter, 4, &bytesWritten, nullptr)); |
| 480 | Win32Call(WriteFile(fileHandle, &appendConsume, 4, &bytesWritten, nullptr)); |
| 481 | |
| 482 | // Get the buffer data |
| 483 | StagingBuffer stagingBuffer; |
| 484 | stagingBuffer.Initialize(device, Size); |
| 485 | context->CopyResource(stagingBuffer.Buffer, Buffer); |
| 486 | const void* bufferData= stagingBuffer.Map(context); |
| 487 | |
| 488 | // Write the data to the file |
| 489 | Win32Call(WriteFile(fileHandle, bufferData, Size, &bytesWritten, nullptr)); |
| 490 | |
| 491 | // Un-map the staging buffer |
| 492 | stagingBuffer.Unmap(context); |
| 493 | |
| 494 | // Close the file |
| 495 | Win32Call(CloseHandle(fileHandle)); |
| 496 | } |
| 497 | |
| 498 | void StructuredBuffer::ReadFromFile(const wchar* path, ID3D11Device* device) |
| 499 | { |
nothing calls this directly
no test coverage detected