Skip to content

Commit 8a14833

Browse files
committed
More updates
1 parent bb7ab35 commit 8a14833

File tree

5 files changed

+28
-38
lines changed

5 files changed

+28
-38
lines changed

Samples/WinMLSamplesGallery/WinMLSamplesGalleryNative/D3D12Quad.cpp

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44

55
inline std::string HrToString(HRESULT hr)
66
{
7-
char s_str[64] = {};
8-
sprintf_s(s_str, "HRESULT of 0x%08X", static_cast<UINT>(hr));
9-
return std::string(s_str);
7+
return std::format("HRESULT of {:08X}", static_cast<UINT>(hr));
108
}
119

1210
class HrException : public std::runtime_error
@@ -22,10 +20,8 @@ D3D12Quad::D3D12Quad(UINT width, UINT height, std::wstring name) :
2220
m_width(width),
2321
m_height(height),
2422
m_title(name),
25-
m_frameIndex(0),
2623
m_viewport(0.0f, 0.0f, static_cast<float>(width), static_cast<float>(height)),
27-
m_scissorRect(0, 0, static_cast<LONG>(width), static_cast<LONG>(height)),
28-
m_rtvDescriptorSize(0)
24+
m_scissorRect(0, 0, static_cast<LONG>(width), static_cast<LONG>(height))
2925
{
3026
m_aspectRatio = static_cast<float>(width) / static_cast<float>(height);
3127
}
@@ -331,10 +327,10 @@ void D3D12Quad::LoadImageTexture() {
331327
textureData.SlicePitch = imageBytesPerRow * textureDesc.Height; // also the size of our quad vertex data
332328

333329
// Now we copy the upload buffer contents to the default heap
334-
UpdateSubresources(m_commandList.Get(), textureBuffer, textureBufferUploadHeap, 0, 0, 1, &textureData);
330+
UpdateSubresources(m_commandList.Get(), textureBuffer.Get(), textureBufferUploadHeap.Get(), 0, 0, 1, &textureData);
335331

336332
// transition the texture default heap to a pixel shader resource (we will be sampling from this heap in the pixel shader to get the color of pixels)
337-
auto tb_transition = CD3DX12_RESOURCE_BARRIER::Transition(textureBuffer, D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE);
333+
auto tb_transition = CD3DX12_RESOURCE_BARRIER::Transition(textureBuffer.Get(), D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE);
338334
m_commandList->ResourceBarrier(1, &tb_transition);
339335

340336
// Describe and create a SRV for the texture.
@@ -343,7 +339,7 @@ void D3D12Quad::LoadImageTexture() {
343339
srvDesc.Format = textureDesc.Format;
344340
srvDesc.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2D;
345341
srvDesc.Texture2D.MipLevels = 1;
346-
m_device->CreateShaderResourceView(textureBuffer, &srvDesc, m_srvHeap->GetCPUDescriptorHandleForHeapStart());
342+
m_device->CreateShaderResourceView(textureBuffer.Get(), &srvDesc, m_srvHeap->GetCPUDescriptorHandleForHeapStart());
347343
}
348344

349345
// Create buffer that will hold the contents of the texture being drawn to the screen
@@ -524,7 +520,7 @@ void D3D12Quad::WaitForPreviousFrame()
524520
}
525521

526522
// get the dxgi format equivilent of a wic format
527-
DXGI_FORMAT D3D12Quad::GetDXGIFormatFromWICFormat(WICPixelFormatGUID& wicFormatGUID)
523+
DXGI_FORMAT D3D12Quad::GetDXGIFormatFromWICFormat(WICPixelFormatGUID wicFormatGUID)
528524
{
529525
if (wicFormatGUID == GUID_WICPixelFormat128bppRGBAFloat) return DXGI_FORMAT_R32G32B32A32_FLOAT;
530526
else if (wicFormatGUID == GUID_WICPixelFormat64bppRGBAHalf) return DXGI_FORMAT_R16G16B16A16_FLOAT;
@@ -533,7 +529,6 @@ DXGI_FORMAT D3D12Quad::GetDXGIFormatFromWICFormat(WICPixelFormatGUID& wicFormatG
533529
else if (wicFormatGUID == GUID_WICPixelFormat32bppBGRA) return DXGI_FORMAT_B8G8R8A8_UNORM;
534530
else if (wicFormatGUID == GUID_WICPixelFormat32bppBGR) return DXGI_FORMAT_B8G8R8X8_UNORM;
535531
else if (wicFormatGUID == GUID_WICPixelFormat32bppRGBA1010102XR) return DXGI_FORMAT_R10G10B10_XR_BIAS_A2_UNORM;
536-
537532
else if (wicFormatGUID == GUID_WICPixelFormat32bppRGBA1010102) return DXGI_FORMAT_R10G10B10A2_UNORM;
538533
else if (wicFormatGUID == GUID_WICPixelFormat16bppBGRA5551) return DXGI_FORMAT_B5G5R5A1_UNORM;
539534
else if (wicFormatGUID == GUID_WICPixelFormat16bppBGR565) return DXGI_FORMAT_B5G6R5_UNORM;
@@ -542,12 +537,11 @@ DXGI_FORMAT D3D12Quad::GetDXGIFormatFromWICFormat(WICPixelFormatGUID& wicFormatG
542537
else if (wicFormatGUID == GUID_WICPixelFormat16bppGray) return DXGI_FORMAT_R16_UNORM;
543538
else if (wicFormatGUID == GUID_WICPixelFormat8bppGray) return DXGI_FORMAT_R8_UNORM;
544539
else if (wicFormatGUID == GUID_WICPixelFormat8bppAlpha) return DXGI_FORMAT_A8_UNORM;
545-
546540
else return DXGI_FORMAT_UNKNOWN;
547541
}
548542

549543
// get a dxgi compatible wic format from another wic format
550-
WICPixelFormatGUID D3D12Quad::GetConvertToWICFormat(WICPixelFormatGUID& wicFormatGUID)
544+
WICPixelFormatGUID D3D12Quad::GetConvertToWICFormat(WICPixelFormatGUID wicFormatGUID)
551545
{
552546
if (wicFormatGUID == GUID_WICPixelFormatBlackWhite) return GUID_WICPixelFormat8bppGray;
553547
else if (wicFormatGUID == GUID_WICPixelFormat1bppIndexed) return GUID_WICPixelFormat32bppRGBA;
@@ -596,7 +590,7 @@ WICPixelFormatGUID D3D12Quad::GetConvertToWICFormat(WICPixelFormatGUID& wicForma
596590
}
597591

598592
// get the number of bits per pixel for a dxgi format
599-
int D3D12Quad::GetDXGIFormatBitsPerPixel(DXGI_FORMAT& dxgiFormat)
593+
int D3D12Quad::GetDXGIFormatBitsPerPixel(DXGI_FORMAT dxgiFormat)
600594
{
601595
if (dxgiFormat == DXGI_FORMAT_R32G32B32A32_FLOAT) return 128;
602596
else if (dxgiFormat == DXGI_FORMAT_R16G16B16A16_FLOAT) return 64;
@@ -616,7 +610,7 @@ int D3D12Quad::GetDXGIFormatBitsPerPixel(DXGI_FORMAT& dxgiFormat)
616610
else if (dxgiFormat == DXGI_FORMAT_A8_UNORM) return 8;
617611
}
618612

619-
// load and decode image from file
613+
// load and decode image from file, returning the number of image bytes
620614
int D3D12Quad::LoadImageDataFromFile(BYTE** imageData, D3D12_RESOURCE_DESC& resourceDescription, LPCWSTR filename, int& bytesPerRow)
621615
{
622616
HRESULT hr;
@@ -771,12 +765,7 @@ void D3D12Quad::GetHardwareAdapter(
771765
DXGI_ADAPTER_DESC1 desc;
772766
adapter->GetDesc1(&desc);
773767

774-
if (desc.Flags & DXGI_ADAPTER_FLAG_SOFTWARE)
775-
{
776-
// Don't select the Basic Render Driver adapter.
777-
// If you want a software adapter, pass in "/warp" on the command line.
778-
continue;
779-
}
768+
if (desc.Flags & DXGI_ADAPTER_FLAG_SOFTWARE) { continue; }
780769

781770
// Check to see whether the adapter supports Direct3D 12, but don't create the
782771
// actual device yet.

Samples/WinMLSamplesGallery/WinMLSamplesGalleryNative/D3D12Quad.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,14 @@ class D3D12Quad
4949
ComPtr<ID3D12DescriptorHeap> m_srvHeap;
5050
ComPtr<ID3D12PipelineState> m_pipelineState;
5151
ComPtr<ID3D12GraphicsCommandList> m_commandList;
52-
UINT m_rtvDescriptorSize;
52+
UINT m_rtvDescriptorSize = 0;
5353

5454
// App resources.
5555
ComPtr<ID3D12Resource> m_vertexBuffer;
5656
D3D12_VERTEX_BUFFER_VIEW m_vertexBufferView;
5757

5858
// Synchronization objects.
59-
UINT m_frameIndex;
59+
UINT m_frameIndex = 0;
6060
HANDLE m_fenceEvent;
6161
ComPtr<ID3D12Fence> m_fence;
6262
UINT64 m_fenceValue;
@@ -74,13 +74,13 @@ class D3D12Quad
7474
void CreateCurrentBuffer();
7575
void CopyTextureIntoCurrentBuffer();
7676

77-
ID3D12Resource* textureBuffer; // the resource heap containing our texture
77+
ComPtr<ID3D12Resource> textureBuffer; // the resource heap containing our texture
7878

7979
int LoadImageDataFromFile(BYTE** imageData, D3D12_RESOURCE_DESC& resourceDescription, LPCWSTR filename, int& bytesPerRow);
8080

81-
DXGI_FORMAT GetDXGIFormatFromWICFormat(WICPixelFormatGUID& wicFormatGUID);
82-
WICPixelFormatGUID GetConvertToWICFormat(WICPixelFormatGUID& wicFormatGUID);
83-
int GetDXGIFormatBitsPerPixel(DXGI_FORMAT& dxgiFormat);
81+
DXGI_FORMAT GetDXGIFormatFromWICFormat(WICPixelFormatGUID wicFormatGUID);
82+
WICPixelFormatGUID GetConvertToWICFormat(WICPixelFormatGUID wicFormatGUID);
83+
int GetDXGIFormatBitsPerPixel(DXGI_FORMAT dxgiFormat);
8484
void LoadImageTexture();
8585
void Reset();
8686
void ThrowIfFailed(HRESULT hr);
@@ -89,8 +89,8 @@ class D3D12Quad
8989
IDXGIAdapter1** ppAdapter,
9090
bool requestHighPerformanceAdapter = false);
9191

92-
ID3D12DescriptorHeap* mainDescriptorHeap;
93-
ID3D12Resource* textureBufferUploadHeap;
92+
ComPtr<ID3D12DescriptorHeap> mainDescriptorHeap;
93+
ComPtr<ID3D12Resource> textureBufferUploadHeap;
9494
int updateCounter;
9595
std::vector<std::wstring> fileNames;
9696
int fileIndex;

Samples/WinMLSamplesGallery/WinMLSamplesGalleryNative/DXResourceBinding.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99

1010
using namespace winrt::Microsoft::AI::MachineLearning;
1111

12-
#undef min
13-
1412
Microsoft::WRL::ComPtr<ID3D12Resource> d3dResource;
1513
static std::optional<Ort::Session> preprocesingSession;
1614
static std::optional<Ort::Session> inferenceSession;
@@ -25,7 +23,7 @@ namespace winrt::WinMLSamplesGalleryNative::implementation
2523
inferenceSession = CreateSession(Win32Application::GetAssetPath(L"efficientnet-lite4-11.onnx").c_str());
2624

2725
// Spawn the window in a separate thread
28-
std::thread d3d_th(Win32Application::Run, &sample, 10);
26+
std::jthread d3d_th(Win32Application::Run, &sample, 10);
2927

3028
// Wait until the D3D pipeline finishes
3129
while (!sample.is_initialized) {}

Samples/WinMLSamplesGallery/WinMLSamplesGalleryNative/ORTHelpers.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,9 @@ Ort::Value Preprocess(Ort::Session& session,
9393
}
9494

9595
// Classify the image using EfficientNet and return the results
96-
winrt::com_array<float> Eval(Ort::Session& session, const Ort::Value& prev_input) {
96+
winrt::com_array<float> Eval(Ort::Session& session,
97+
const Ort::Value& prev_input)
98+
{
9799
// Create input and output node names
98100
const char* inputTensorName = "images:0";
99101
const char* outputTensorName = "Softmax:0";
@@ -107,10 +109,10 @@ winrt::com_array<float> Eval(Ort::Session& session, const Ort::Value& prev_input
107109

108110
// Get the 1000 EfficientNet classifications as a com_array and return
109111
// the results
110-
float* floatarr = output_tensors.front().GetTensorMutableData<float>();
112+
float* floatArray = output_tensors.front().GetTensorMutableData<float>();
111113
winrt::com_array<float> final_results(1000);
112114
for (int i = 0; i < 1000; i++) {
113-
final_results[i] = floatarr[i];
115+
final_results[i] = floatArray[i];
114116
}
115117

116118
return final_results;

Samples/WinMLSamplesGallery/WinMLSamplesGalleryNative/Win32Application.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ void Win32Application::CloseWindow()
144144

145145
std::wstring Win32Application::GetAssetPath(LPCWSTR assetName)
146146
{
147-
WCHAR modulePath[512];
147+
WCHAR modulePath[MAX_PATH];
148148
UINT modulePathSize = std::size(modulePath);
149149
GetModuleFileName(nullptr, modulePath, modulePathSize);
150150

@@ -153,8 +153,9 @@ std::wstring Win32Application::GetAssetPath(LPCWSTR assetName)
153153

154154
// Erase everything after the first occurence of the gallery string
155155
std::wstring path(modulePath);
156-
size_t found = path.find(gallery);
157-
path.erase(path.begin() + found + gallerySize, path.end());
156+
size_t offset = path.find(gallery);
157+
assert(offset != std::string::npos);
158+
path.erase(path.begin() + offset + gallerySize, path.end());
158159

159160
std::wstring native = L"\\WinMLSamplesGalleryNative\\";
160161
path = path + native + assetName;

0 commit comments

Comments
 (0)