Skip to content

Commit

Permalink
Merge branch 'main' into yutji/cuda_decode
Browse files Browse the repository at this point in the history
  • Loading branch information
yukirora authored Aug 18, 2023
2 parents 779af1d + 6c0205c commit 222810f
Show file tree
Hide file tree
Showing 23 changed files with 4,353 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/workflows/build-image.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ jobs:
sudo docker rmi $(sudo docker images --format "{{.Repository}}:{{.Tag}}" --filter=reference="node" --filter=reference="buildpack-deps")
df -h
- name: Free Up GitHub Actions Ubuntu Runner Disk Space 🔧
uses: jlumbroso/free-disk-space@main
uses: hirnidrin/free-disk-space@main
with:
# This might remove tools that are actually needed, if set to "true" but frees about 6 GB
tool-cache: false
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

#pragma once

#include <algorithm>
#include <codecvt>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>

#include "../directx_utils/Options.h"

using namespace std;

// enum class for pass type
enum class PassType { GeometryPass, ShadowMapPass, LightingPass };

class BenchmarkOptions : public Options {
public:
int m_textureSize = 0;
int m_textureNum = 10;
int m_vertexNum = 3000;
int m_indexNum = 3000;
int m_width = 1080;
int m_height = 720;
int m_warmup = 500;
int m_num_object = 1;
string m_outfile = "outfile.txt";
PassType m_pass_type = PassType::ShadowMapPass;
int m_num_frames = 3000;
int m_num_light = 1;
bool m_quiet = true;

BenchmarkOptions(int argc, char *argv[]) : Options(argc, argv) {}

virtual void get_option_usage() {
cout << "Usage: " << endl;
cout << " --width <int> set the width of the window" << endl;
cout << " --height <int> set the height of the window" << endl;
cout << " --warmup <int> set the warmup frames" << endl;
cout << " --vertex <int> set the number of vertices" << endl;
cout << " --index <int> set the number of indices" << endl;
cout << " --texture_size <int> set the size of textures <x,x>" << endl;
cout << " --outfile <string> set the output file name" << endl;
cout << " --pass <string> set the pass type" << endl;
cout << " --object <int> set the number of objects" << endl;
cout << " --frame <int> set the number of frames" << endl;
cout << " --light <int> set the number of lights" << endl;
cout << " --quiet disable window" << endl;
}

virtual void parse_arguments() {
m_width = get_cmd_line_argument_int("--width", 1080);
m_height = get_cmd_line_argument_int("--height", 720);
m_warmup = get_cmd_line_argument_int("--warmup", 500);
m_vertexNum = get_cmd_line_argument_int("--vertex", m_vertexNum);
m_indexNum = get_cmd_line_argument_int("--index", m_indexNum);
m_textureSize = get_cmd_line_argument_int("--texture", 3);
m_textureNum = get_cmd_line_argument_int("--texture_num", 3);
m_outfile = get_cmd_line_argument_string("--outfile");
auto pass = get_cmd_line_argument_string("--pass");
std::transform(pass.begin(), pass.end(), pass.begin(), [](unsigned char c) { return std::tolower(c); });
if (pass == "geometry") {
m_pass_type = PassType::GeometryPass;
} else if (pass == "shadow") {
m_pass_type = PassType::ShadowMapPass;
} else if (pass == "lighting") {
m_pass_type = PassType::LightingPass;
} else {
cout << "Error: Invalid pass type: " << pass << endl;
exit(1);
}
m_num_object = get_cmd_line_argument_int("--object", m_num_object);
m_num_frames = get_cmd_line_argument_int("--frame", m_num_frames);
m_num_light = get_cmd_line_argument_int("--light", m_num_light);
m_quiet = get_cmd_line_argument_bool("--quiet");
};
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

#include "BufferHelper.h"

// Function to calculate the byte size of the constant buffer,
// which must be a multiple of 256 bytes.
UINT CalcConstantBufferByteSize(UINT byteSize) {
// Calculate the aligned size.
return (byteSize + 255) & ~255;
}

/*
* @brief: Create a default buffer.
* @param: device the device of GPU object.
* @param: cmdList the command list of GPU object.
* @param: initData the data to be copied to the default buffer.
* @param: byteSize the size of data.
* @return: the default buffer.
*/
Microsoft::WRL::ComPtr<ID3D12Resource> CreateDefaultBuffer(ID3D12Device *device, ID3D12GraphicsCommandList *cmdList,
const void *initData, UINT64 byteSize,
Microsoft::WRL::ComPtr<ID3D12Resource> &uploadBuffer) {
ComPtr<ID3D12Resource> defaultBuffer;

// Create the actual default buffer resource.
ThrowIfFailed(device->CreateCommittedResource(&CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_DEFAULT),
D3D12_HEAP_FLAG_NONE, &CD3DX12_RESOURCE_DESC::Buffer(byteSize),
D3D12_RESOURCE_STATE_COMMON, nullptr,
IID_PPV_ARGS(defaultBuffer.GetAddressOf())));

// In order to copy CPU memory data into our default buffer, we need to create
// an intermediate upload heap.
ThrowIfFailed(device->CreateCommittedResource(&CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_UPLOAD),
D3D12_HEAP_FLAG_NONE, &CD3DX12_RESOURCE_DESC::Buffer(byteSize),
D3D12_RESOURCE_STATE_GENERIC_READ, nullptr,
IID_PPV_ARGS(uploadBuffer.GetAddressOf())));

// Describe the data we want to copy into the default buffer.
D3D12_SUBRESOURCE_DATA subResourceData = {};
subResourceData.pData = initData;
subResourceData.RowPitch = byteSize;
subResourceData.SlicePitch = subResourceData.RowPitch;

// Schedule to copy the data to the default buffer resource. At a high level, the helper function
// UpdateSubresources will copy the CPU memory into the intermediate upload heap. Then, using
// ID3D12CommandList::CopySubresourceRegion, the intermediate upload heap data will be copied to mBuffer.
cmdList->ResourceBarrier(1, &CD3DX12_RESOURCE_BARRIER::Transition(defaultBuffer.Get(), D3D12_RESOURCE_STATE_COMMON,
D3D12_RESOURCE_STATE_COPY_DEST));
UpdateSubresources<1>(cmdList, defaultBuffer.Get(), uploadBuffer.Get(), 0, 0, 1, &subResourceData);
cmdList->ResourceBarrier(1,
&CD3DX12_RESOURCE_BARRIER::Transition(defaultBuffer.Get(), D3D12_RESOURCE_STATE_COPY_DEST,
D3D12_RESOURCE_STATE_GENERIC_READ));

// Note: uploadBuffer has to be kept alive after the above function calls because
// the command list has not been executed yet that performs the actual copy.
// The caller can Release the uploadBuffer after it knows the copy has been executed.

return defaultBuffer;
}

std::vector<UINT8> CreateRandomTexture(const UINT width, const UINT height, const UINT texturePixelSize) {
// Create a buffer to store the texture data
std::vector<unsigned char> textureData(width * height * texturePixelSize);

// Initialize the random number generator
std::random_device rd;
std::mt19937 generator(rd());
std::uniform_int_distribution<int> distribution(0, 255);

// Generate random data for the texture
for (UINT i = 0; i < width * height * texturePixelSize; ++i) {
textureData[i] = static_cast<unsigned char>(distribution(generator));
}
return textureData;
}

void UploadTexture(ID3D12Device *device, ID3D12GraphicsCommandList *pCmdList, const std::vector<UINT8> &textureData,
Microsoft::WRL::ComPtr<ID3D12Resource> &texture, const UINT width, const UINT height,
const UINT texturePixelSize) {
// Create the GPU upload buffer.
const UINT64 uploadBufferSize = GetRequiredIntermediateSize(texture.Get(), 0, 1);

ID3D12Resource *textureUploadHeap;
ThrowIfFailed(
device->CreateCommittedResource(&CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_UPLOAD), D3D12_HEAP_FLAG_NONE,
&CD3DX12_RESOURCE_DESC::Buffer(uploadBufferSize),
D3D12_RESOURCE_STATE_GENERIC_READ, nullptr, IID_PPV_ARGS(&textureUploadHeap)));

// Copy data to the intermediate upload heap and then schedule a copy
// from the upload heap to the Texture2D.
D3D12_SUBRESOURCE_DATA textureDataDesc = {};
textureDataDesc.pData = textureData.data();
textureDataDesc.RowPitch = width * texturePixelSize;
textureDataDesc.SlicePitch = textureDataDesc.RowPitch * height;

UpdateSubresources(pCmdList, texture.Get(), textureUploadHeap, 0, 0, 1, &textureDataDesc);
pCmdList->ResourceBarrier(1, &CD3DX12_RESOURCE_BARRIER::Transition(texture.Get(), D3D12_RESOURCE_STATE_COPY_DEST,
D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE));
}

void CreateTextureResource(ID3D12Device *device, UINT width, UINT height, DXGI_FORMAT format,
Microsoft::WRL::ComPtr<ID3D12Resource> &textureResource, UINT16 arraySize) {
D3D12_RESOURCE_DESC textureDesc = {};
textureDesc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D;
textureDesc.Width = width;
textureDesc.Height = height;
textureDesc.DepthOrArraySize = arraySize;
textureDesc.MipLevels = 1;
textureDesc.Format = format;
textureDesc.SampleDesc.Count = 1;
textureDesc.SampleDesc.Quality = 0;
textureDesc.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN;
textureDesc.Flags = D3D12_RESOURCE_FLAG_NONE;

ThrowIfFailed(device->CreateCommittedResource(&CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_DEFAULT),
D3D12_HEAP_FLAG_NONE, &textureDesc, D3D12_RESOURCE_STATE_COPY_DEST,
nullptr, IID_PPV_ARGS(&textureResource)));
}

void Texture2D(ID3D12Device *device, ID3D12GraphicsCommandList *cmdList,
Microsoft::WRL::ComPtr<ID3D12Resource> &textureResource, int width, int height, DXGI_FORMAT format) {
CreateTextureResource(device, width, height, format, textureResource, 1);
auto textureData = CreateRandomTexture(width, height);
UploadTexture(device, cmdList, textureData, textureResource, width, height);
}

void TextureCube(ID3D12Device *device, ID3D12GraphicsCommandList *cmdList,
Microsoft::WRL::ComPtr<ID3D12Resource> &textureResource, int width, int height, DXGI_FORMAT format) {
CreateTextureResource(device, width, height, format, textureResource, 6);
std::vector<UINT8> textureCubeData;
for (int i = 0; i < 6; ++i) {
auto textureData = CreateRandomTexture(width, height);
textureCubeData.insert(textureCubeData.end(), textureData.begin(), textureData.end());
}
UploadTexture(device, cmdList, textureCubeData, textureResource, width, height);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

#pragma once

#include <random>

#include "../directx_third_party/DXSampleHelper.h"
#include "../directx_third_party/d3dx12.h"

// Helper class for creating and uploading resources to the GPU.
template <typename T> class UploadBuffer {
public:
UploadBuffer(ID3D12Device *device, UINT elementCount, bool isConstantBuffer)
: m_isConstantBuffer(isConstantBuffer) {
m_elementByteSize = sizeof(T);

if (isConstantBuffer)
m_elementByteSize = CalcConstantBufferByteSize(sizeof(T));

ThrowIfFailed(
device->CreateCommittedResource(&CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_UPLOAD), D3D12_HEAP_FLAG_NONE,
&CD3DX12_RESOURCE_DESC::Buffer(m_elementByteSize * elementCount),
D3D12_RESOURCE_STATE_GENERIC_READ, nullptr, IID_PPV_ARGS(&m_uploadBuffer)));
}

UploadBuffer(const UploadBuffer &rhs) = delete;
UploadBuffer &operator=(const UploadBuffer &rhs) = delete;
~UploadBuffer() {
if (m_uploadBuffer != nullptr)
m_uploadBuffer->Unmap(0, nullptr);

m_mappedData = nullptr;
}

ID3D12Resource *Resource() const { return m_uploadBuffer.Get(); }

void CopyData(int elementIndex, const T &data) {
ThrowIfFailed(m_uploadBuffer->Map(0, nullptr, reinterpret_cast<void **>(&m_mappedData)));
memcpy(&m_mappedData[elementIndex * m_elementByteSize], &data, sizeof(T));
m_uploadBuffer->Unmap(0, nullptr);
}

private:
Microsoft::WRL::ComPtr<ID3D12Resource> m_uploadBuffer;
BYTE *m_mappedData = nullptr;

UINT m_elementByteSize = 0;
bool m_isConstantBuffer = false;
};

/*
* @brief: Create a default buffer.
* @param: device the device of GPU object.
* @param: cmdList the command list of GPU object.
* @param: initData the data to be copied to the default buffer.
* @param: byteSize the size of data.
* @return: the default buffer.
*/
Microsoft::WRL::ComPtr<ID3D12Resource> CreateDefaultBuffer(ID3D12Device *device, ID3D12GraphicsCommandList *cmdList,
const void *initData, UINT64 byteSize,
Microsoft::WRL::ComPtr<ID3D12Resource> &uploadBuffer);

/*
* @brief: Calculate the size of constant buffer.
*/
UINT CalcConstantBufferByteSize(UINT byteSize);

/*
* @brief: Create a random texture.
* @param: width the width of texture.
* @param: height the height of texture.
* @param: texturePixelSize the size of texture pixel.
* @return: the random texture data.
*/
std::vector<UINT8> CreateRandomTexture(const UINT width, const UINT height, const UINT texturePixelSize = 4);

/*
* @brief: Upload the texture to GPU.
* @param: device the device of GPU object.
* @param: pCmdList the command list of GPU object.
* @param: textureData the texture data to be uploaded.
* @param: texture the texture resource.
* @param: width the width of texture.
* @param: height the height of texture.
* @param: texturePixelSize the size of texture pixel.
*/
void UploadTexture(ID3D12Device *device, ID3D12GraphicsCommandList *pCmdList, const std::vector<UINT8> &textureData,
Microsoft::WRL::ComPtr<ID3D12Resource> &texture, const UINT width, const UINT height,
const UINT texturePixelSize = 4);

/*
* @brief: Create a texture resource.
* @param: device the device of GPU object.
* @param: width the width of texture.
* @param: height the height of texture.
* @param: format the format of texture.
* @param: textureResource the texture resource.
* @param: arraySize the size of texture array.
*/
void CreateTextureResource(ID3D12Device *device, UINT width, UINT height, DXGI_FORMAT format,
Microsoft::WRL::ComPtr<ID3D12Resource> &textureResource, UINT16 arraySize);

/*
* @brief: Create a random texture resource and upload it to GPU.
* @param: device the device of GPU object.
* @param: cmdList the command list of GPU object.
* @param: textureResource the texture resource.
* @param: width the width of texture.
* @param: height the height of texture.
* @param: format the format of texture.
*/
void Texture2D(ID3D12Device *device, ID3D12GraphicsCommandList *cmdList,
Microsoft::WRL::ComPtr<ID3D12Resource> &textureResource, int width, int height, DXGI_FORMAT format);

/*
* @brief: Create a random texture cube resource and upload it to GPU.
* @param: device the device of GPU object.
* @param: cmdList the command list of GPU object.
* @param: textureResource the texture resource.
* @param: width the width of texture.
* @param: height the height of texture.
* @param: format the format of texture.
*/
void TextureCube(ID3D12Device *device, ID3D12GraphicsCommandList *cmdList,
Microsoft::WRL::ComPtr<ID3D12Resource> &textureResource, int width, int height, DXGI_FORMAT format);
Loading

0 comments on commit 222810f

Please sign in to comment.