Skip to content

Commit 023c1a4

Browse files
committed
Fix copytextureregion
1 parent 28df502 commit 023c1a4

File tree

6 files changed

+107
-4
lines changed

6 files changed

+107
-4
lines changed
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
*.onnx
22
!squeezenet1.1-7-batched.onnx
33
!encrypted.onnx
4-
!dx_preprocessor.onnx
4+
!dx_preprocessor.onnx
5+
!efficientnet-lite4-11.onnx
6+
!dx_preprocessor_efficient_net.onnx
7+
!dx_preprocessor_efficient_net_v2.onnx
8+
Binary file not shown.

Samples/WinMLSamplesGallery/WinMLSamplesGalleryNative/D3D12Quad.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,11 +157,13 @@ void D3D12Quad::LoadAssets()
157157
m_commandList->ResourceBarrier(1, &present_to_copy_src);
158158

159159
auto desc = m_renderTargets[m_frameIndex]->GetDesc();
160+
UINT64 rowSize, totalSize;
161+
m_device->GetCopyableFootprints(&desc, 0, 1, 0, nullptr, nullptr, &rowSize, &totalSize);
160162
D3D12_PLACED_SUBRESOURCE_FOOTPRINT bufferFootprint = {};
161163
bufferFootprint.Footprint.Width = static_cast<UINT>(desc.Width);
162164
bufferFootprint.Footprint.Height = desc.Height;
163165
bufferFootprint.Footprint.Depth = 1;
164-
bufferFootprint.Footprint.RowPitch = static_cast<UINT>(imageBytesPerRow);
166+
bufferFootprint.Footprint.RowPitch = static_cast<UINT>((rowSize + 255) & ~255);
165167
bufferFootprint.Footprint.Format = desc.Format;
166168

167169
const CD3DX12_TEXTURE_COPY_LOCATION copyDest(currentBuffer.Get(), bufferFootprint);
@@ -397,7 +399,7 @@ void D3D12Quad::CreateCurrentBuffer()
397399
bufferDesc.Format = DXGI_FORMAT_UNKNOWN;
398400
//bufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
399401
bufferDesc.Height = 1;
400-
bufferDesc.Width = imageBytesPerRow * desc.Height;
402+
bufferDesc.Width = ((desc.Width + 255) & ~255) * 4 * desc.Height;
401403
//bufferDesc.Width = static_cast<uint64_t>(800 * 600 * 3 * 4);
402404
bufferDesc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR;
403405
//bufferDesc.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN;
@@ -533,11 +535,13 @@ void D3D12Quad::PopulateCommandList()
533535
//m_commandList->CopyResource(currentBuffer.Get(), m_renderTargets[m_frameIndex].Get());
534536

535537
auto desc = m_renderTargets[m_frameIndex]->GetDesc();
538+
UINT64 rowSize, totalSize;
539+
m_device->GetCopyableFootprints(&desc, 0, 1, 0, nullptr, nullptr, &rowSize, &totalSize);
536540
D3D12_PLACED_SUBRESOURCE_FOOTPRINT bufferFootprint = {};
537541
bufferFootprint.Footprint.Width = desc.Width;
538542
bufferFootprint.Footprint.Height = desc.Height;
539543
bufferFootprint.Footprint.Depth = 1;
540-
bufferFootprint.Footprint.RowPitch = static_cast<UINT>(imageBytesPerRow);
544+
bufferFootprint.Footprint.RowPitch = static_cast<UINT>((rowSize + 255) & ~255);
541545
bufferFootprint.Footprint.Format = desc.Format;
542546

543547
const CD3DX12_TEXTURE_COPY_LOCATION copyDest(currentBuffer.Get(), bufferFootprint);

Samples/WinMLSamplesGallery/WinMLSamplesGalleryNative/DXResourceBinding.cpp

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,101 @@ namespace winrt::WinMLSamplesGalleryNative::implementation
1818
{
1919
// Create ORT Sessions and launch D3D window in a separate thread
2020
void DXResourceBinding::LaunchWindow() {
21+
22+
23+
long newH = 224;
24+
long newW = 224;
25+
long h = 512;
26+
long w = 512;
27+
std::array<long, 6> center_fill_dimensions = CalculateCenterFillDimensions(h, w, newH, newW);
28+
long resizedW = center_fill_dimensions[0];
29+
long resizedH = center_fill_dimensions[1];
30+
long top = center_fill_dimensions[2];
31+
long bottom = center_fill_dimensions[3];
32+
long left = center_fill_dimensions[4];
33+
long right = center_fill_dimensions[5];
34+
winrt::hstring interpolationMode = L"nearest";
35+
long c = 3;
36+
37+
auto width = 800;
38+
auto height = 600;
39+
auto rowPitchInPixels = (width + 255) & ~255;
40+
auto rowPitchInBytes = rowPitchInPixels * 4;
41+
auto bufferInBytes = rowPitchInBytes * height;
42+
const std::array<int64_t, 4> preprocessInputShape = { 1, bufferInBytes};
43+
const std::array<int64_t, 4> preprocessOutputShape = { 1, 224, 224, 3 };
44+
45+
var kernel = new float[] {
46+
0,0,1,
47+
0,1,0,
48+
1,0,0
49+
};
50+
51+
.Inputs.Add(LearningModelBuilder.CreateTensorFeatureDescriptor("Input", TensorKind.UInt8, new long[] { 1, bufferInBytes }))
52+
.Outputs.Add(LearningModelBuilder.CreateTensorFeatureDescriptor("Output", TensorKind.Float, new long[] { 1, newH, newW, c }))
53+
.Operators.Add(new LearningModelOperator("Cast")
54+
.SetInput("input", "Input")
55+
.SetAttribute("to", TensorInt64Bit.CreateFromIterable(new long[] {}, new long[] { (long)OnnxDataType.FLOAT }))
56+
.SetOutput("output", "CastOutput"))
57+
.Operators.Add(new LearningModelOperator("Reshape")
58+
.SetInput("data", "CastOutput")
59+
.SetConstant("shape", TensorInt64Bit.CreateFromIterable(new long[] { 4 }, new long[] { 1, height, width, 4 }))
60+
.SetOutput("reshaped", "ReshapeOutput"))
61+
.Operators.Add(new LearningModelOperator("Slice")
62+
.SetInput("data", "ReshapeOutput")
63+
.SetConstant("starts", TensorInt64Bit.CreateFromIterable(new long[] { 4 }, new long[] { 0, 0, 0, 0 }))
64+
.SetConstant("ends", TensorInt64Bit.CreateFromIterable(new long[] { 4 }, new long[] { long.MaxValue, long.MaxValue, width, c - 1 }))
65+
.SetOutput("output", "SliceOutput"))
66+
.Operators.Add(new LearningModelOperator("Resize")
67+
.SetInput("X", "ReshapeOutput")
68+
.SetConstant("roi", TensorFloat.CreateFromIterable(new long[] { 8 }, new float[] { 0, 0, 0, 0, 1, 1, 1, 1 }))
69+
.SetConstant("scales", TensorFloat.CreateFromIterable(new long[] { 4 }, new float[] { 1, (float)(1 + resizedH) / (float)h, (float)(1 + resizedH) / (float)h, 1 }))
70+
//.SetConstant("sizes", TensorInt64Bit.CreateFromIterable(new long[] { 4 }, new long[] { 1, 3, resizedH, resizedW }))
71+
.SetAttribute("mode", TensorString.CreateFromArray(new long[] {}, new string[]{ interpolationMode }))
72+
.SetOutput("Y", "ResizeOutput"))
73+
.Operators.Add(new LearningModelOperator("Slice")
74+
.SetInput("data", "ResizeOutput")
75+
.SetConstant("starts", TensorInt64Bit.CreateFromIterable(new long[] { 4 }, new long[] { 0, 0, 0, 0 }))
76+
.SetConstant("ends", TensorInt64Bit.CreateFromIterable(new long[] { 4 }, new long[] { long.MaxValue, 224, 224, 3 }))
77+
.SetOutput("output", "SliceOutput"))
78+
// This is just getting bgr to rgb
79+
/* .Operators.Add(new LearningModelOperator("Conv")
80+
.SetInput("X", "Input")
81+
.SetConstant("W", TensorFloat.CreateFromArray(new long[] { 3, 3, 1, 1 }, kernel))
82+
.SetConstant("B", TensorFloat.CreateFromArray(new long[] { 1, 3, 1, 1 }, new float[] { 0, 0, 0 }))
83+
.SetOutput("Y", "Output"));*/
84+
85+
//if above doesn't work try
86+
//.SetConstant("W", TensorFloat.CreateFromArray(new long[] { 3, 1, 1, 3 }, kernel))
87+
//.SetConstant("B", TensorFloat.CreateFromArray(new long[] { 1, 1, 1, 3 }, new float[] { 0, 0, 0 }))
88+
89+
auto resize_op = LearningModelOperator(L"Resize")
90+
.SetInput(L"X", L"Input")
91+
.SetConstant(L"roi", TensorFloat::CreateFromIterable({ 8 }, { 0, 0, 0, 0, 1, 1, 1, 1 }))
92+
.SetConstant(L"scales", TensorFloat::CreateFromIterable({ 4 }, { 1, (float)(1 + resizedH) / (float)h, (float)(1 + resizedH) / (float)h, 1 }))
93+
.SetAttribute(L"mode", TensorString::CreateFromArray({}, { interpolationMode }))
94+
.SetOutput(L"Y", L"ResizeOutput");
95+
96+
auto slice_op = LearningModelOperator(L"Slice")
97+
.SetInput(L"data", L"ResizeOutput")
98+
.SetConstant(L"starts", TensorInt64Bit::CreateFromIterable({ 4 }, { 0, top, left, 0 }))
99+
.SetConstant(L"ends", TensorInt64Bit::CreateFromIterable({ 4 }, { LLONG_MAX, bottom, right, 3 }))
100+
.SetOutput(L"output", L"Output");
101+
102+
103+
auto preprocessingModelBuilder =
104+
LearningModelBuilder::Create(12)
105+
.Inputs().Add(LearningModelBuilder::CreateTensorFeatureDescriptor(L"Input", TensorKind::UInt8, preprocessInputShape))
106+
.Outputs().Add(LearningModelBuilder::CreateTensorFeatureDescriptor(L"Output", TensorKind::Float, preprocessOutputShape))
107+
.Operators().Add(resize_op)
108+
.Operators().Add(slice_op);
109+
//.Operators().Add(dimension_transpose);
110+
auto preprocessingModel = preprocessingModelBuilder.CreateModel();
111+
112+
preprocessingModelBuilder.Save(L"C:/Users/numform/Windows-Machine-Learning/Samples/WinMLSamplesGallery/WinMLSamplesGalleryNative/dx_preprocessor_efficient_net.onnx");
113+
114+
115+
21116
// Create ORT Sessions that will be used for preprocessing and classification
22117
const wchar_t* preprocessingModelFilePath = L"C:/Users/numform/Windows-Machine-Learning/Samples/WinMLSamplesGallery/WinMLSamplesGalleryNative/dx_preprocessor_efficient_net.onnx";
23118
const wchar_t* inferencemodelFilePath = L"C:/Users/numform/Windows-Machine-Learning/Samples/WinMLSamplesGallery/WinMLSamplesGalleryNative/efficientnet-lite4-11.onnx";
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)