Skip to content

Commit da34591

Browse files
committed
more cleanup
1 parent ceb3e1a commit da34591

3 files changed

Lines changed: 152 additions & 134 deletions

File tree

tools/clang/unittests/HLSLExec/HlslExecTestUtils.cpp

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,3 +636,131 @@ UINT getMaxGroupSharedMemoryMS(ID3D12Device *Device) {
636636
D3D12_FEATURE_D3D12_OPTIONS_PREVIEW, &O, sizeof(O)));
637637
return O.MaxGroupSharedMemoryPerGroupMS;
638638
}
639+
640+
std::unique_ptr<st::ShaderOp>
641+
createComputeOp(const char *Source, const char *Target, const char *RootSig,
642+
const char *Args, UINT DispatchX,
643+
UINT DispatchY, UINT DispatchZ) {
644+
auto Op = std::make_unique<st::ShaderOp>();
645+
LPCSTR CSName = Op->Strings.insert("CS");
646+
Op->Name = CSName;
647+
Op->CS = CSName;
648+
Op->RootSignature = Op->Strings.insert(RootSig);
649+
Op->DispatchX = DispatchX;
650+
Op->DispatchY = DispatchY;
651+
Op->DispatchZ = DispatchZ;
652+
Op->UseWarpDevice = true;
653+
654+
st::ShaderOpShader Shader = {};
655+
Shader.Name = CSName;
656+
Shader.Target = Op->Strings.insert(Target);
657+
Shader.EntryPoint = Op->Strings.insert("main");
658+
Shader.Text = Op->Strings.insert(Source);
659+
Shader.Arguments = Args ? Op->Strings.insert(Args) : nullptr;
660+
Shader.Compiled = FALSE;
661+
Shader.Callback = FALSE;
662+
Op->Shaders.push_back(Shader);
663+
664+
return Op;
665+
}
666+
667+
void addUAVBuffer(st::ShaderOp *Op, const char *Name, UINT64 Width,
668+
bool ReadBack, const char *Init) {
669+
st::ShaderOpResource Res = {};
670+
Res.Name = Op->Strings.insert(Name);
671+
Res.Init = Op->Strings.insert(Init);
672+
Res.ReadBack = ReadBack ? TRUE : FALSE;
673+
674+
Res.HeapProperties.Type = D3D12_HEAP_TYPE_DEFAULT;
675+
Res.HeapFlags = D3D12_HEAP_FLAG_NONE;
676+
Res.Desc.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER;
677+
Res.Desc.Width = Width;
678+
Res.Desc.Height = 1;
679+
Res.Desc.DepthOrArraySize = 1;
680+
Res.Desc.MipLevels = 1;
681+
Res.Desc.SampleDesc.Count = 1;
682+
Res.Desc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR;
683+
Res.Desc.Flags = D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS;
684+
Res.InitialResourceState = D3D12_RESOURCE_STATE_COPY_DEST;
685+
Res.TransitionTo = D3D12_RESOURCE_STATE_UNORDERED_ACCESS;
686+
687+
Op->Resources.push_back(Res);
688+
}
689+
690+
void addRootUAV(st::ShaderOp *Op, UINT Index, const char *ResName) {
691+
st::ShaderOpRootValue RV = {};
692+
RV.ResName = Op->Strings.insert(ResName);
693+
RV.HeapName = nullptr;
694+
RV.Index = Index;
695+
Op->RootValues.push_back(RV);
696+
}
697+
698+
std::shared_ptr<st::ShaderOpTestResult>
699+
runShaderOp(ID3D12Device *Device, dxc::SpecificDllLoader &DxcSupport,
700+
std::unique_ptr<st::ShaderOp> Op,
701+
st::ShaderOpTest::TInitCallbackFn InitCallback) {
702+
auto OpSet = std::make_shared<st::ShaderOpSet>();
703+
OpSet->ShaderOps.push_back(std::move(Op));
704+
705+
return st::RunShaderOpTestAfterParse(
706+
Device, DxcSupport, nullptr, std::move(InitCallback), std::move(OpSet));
707+
}
708+
709+
void compileShader(dxc::SpecificDllLoader &DxcSupport,
710+
const char *Source, const char *Target,
711+
const std::string &Args) {
712+
CComPtr<IDxcCompiler3> Compiler;
713+
VERIFY_SUCCEEDED(DxcSupport.CreateInstance(CLSID_DxcCompiler, &Compiler));
714+
715+
CComPtr<IDxcUtils> Utils;
716+
VERIFY_SUCCEEDED(DxcSupport.CreateInstance(CLSID_DxcUtils, &Utils));
717+
718+
CComPtr<IDxcBlobEncoding> SourceBlob;
719+
VERIFY_SUCCEEDED(Utils->CreateBlobFromPinned(
720+
Source, static_cast<UINT32>(strlen(Source)), DXC_CP_UTF8, &SourceBlob));
721+
722+
// Build wide-string argument list: -T <target> -E main <extra args>.
723+
std::vector<std::wstring> WArgStorage;
724+
WArgStorage.push_back(L"-T");
725+
WArgStorage.push_back(std::wstring(Target, Target + strlen(Target)));
726+
WArgStorage.push_back(L"-E");
727+
WArgStorage.push_back(L"main");
728+
729+
// Tokenize the additional arguments string.
730+
std::istringstream SS(Args);
731+
std::string Tok;
732+
while (SS >> Tok)
733+
WArgStorage.push_back(std::wstring(Tok.begin(), Tok.end()));
734+
735+
std::vector<LPCWSTR> WArgPtrs;
736+
std::wstringstream LogFlags;
737+
LogFlags << L"Compiling with flags:";
738+
for (const auto &A : WArgStorage) {
739+
WArgPtrs.push_back(A.c_str());
740+
LogFlags << L" " << A;
741+
}
742+
743+
DxcBuffer Buf = {};
744+
Buf.Ptr = SourceBlob->GetBufferPointer();
745+
Buf.Size = SourceBlob->GetBufferSize();
746+
Buf.Encoding = DXC_CP_UTF8;
747+
748+
hlsl_test::LogCommentFmt(LogFlags.str().c_str());
749+
750+
CComPtr<IDxcResult> Result;
751+
VERIFY_SUCCEEDED(Compiler->Compile(&Buf, WArgPtrs.data(),
752+
static_cast<UINT32>(WArgPtrs.size()),
753+
nullptr, IID_PPV_ARGS(&Result)));
754+
755+
HRESULT HR;
756+
VERIFY_SUCCEEDED(Result->GetStatus(&HR));
757+
758+
if (FAILED(HR)) {
759+
CComPtr<IDxcBlobUtf8> Errors;
760+
Result->GetOutput(DXC_OUT_ERRORS, IID_PPV_ARGS(&Errors), nullptr);
761+
if (Errors && Errors->GetStringLength() > 0)
762+
hlsl_test::LogErrorFmt(L"Shader compilation failed:\n%S",
763+
Errors->GetStringPointer());
764+
VERIFY_SUCCEEDED(HR);
765+
}
766+
}

tools/clang/unittests/HLSLExec/HlslExecTestUtils.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@
33

44
#include <atlcomcli.h>
55
#include <d3d12.h>
6+
#include <memory>
67
#include <optional>
8+
#include <string>
79
#include <windows.h>
810

911
#include "dxc/Support/dxcapi.use.h"
12+
#include "ShaderOpTest.h"
1013

1114
// D3D_SHADER_MODEL_6_10 is not yet in the released Windows SDK.
1215
// Define locally so the tests can target SM 6.10.
@@ -74,4 +77,25 @@ UINT getMaxGroupSharedMemoryCS(ID3D12Device *Device);
7477
UINT getMaxGroupSharedMemoryAS(ID3D12Device *Device);
7578
UINT getMaxGroupSharedMemoryMS(ID3D12Device *Device);
7679

80+
/// Create a ShaderOp for a compute shader dispatch.
81+
std::unique_ptr<st::ShaderOp>
82+
createComputeOp(const char *Source, const char *Target, const char *RootSig,
83+
const char *Args = nullptr, UINT DispatchX = 1,
84+
UINT DispatchY = 1, UINT DispatchZ = 1);
85+
/// Add a UAV buffer resource to a ShaderOp.
86+
void addUAVBuffer(st::ShaderOp *Op, const char *Name, UINT64 Width,
87+
bool ReadBack, const char *Init = "zero");
88+
/// Bind a resource to a root UAV parameter by index.
89+
void addRootUAV(st::ShaderOp *Op, UINT Index, const char *ResName);
90+
/// Run a programmatically-built ShaderOp and return the result.
91+
std::shared_ptr<st::ShaderOpTestResult>
92+
runShaderOp(ID3D12Device *Device, dxc::SpecificDllLoader &DxcSupport,
93+
std::unique_ptr<st::ShaderOp> Op,
94+
st::ShaderOpTest::TInitCallbackFn InitCallback = nullptr);
95+
/// Compiles an HLSL shader using the DXC API to verify it is well-formed.
96+
/// Fails the test on compile error.
97+
void compileShader(dxc::SpecificDllLoader &DxcSupport,
98+
const char *Source, const char *Target,
99+
const std::string &Args);
100+
77101
#endif // HLSLEXECTESTUTILS_H

tools/clang/unittests/HLSLExec/LinAlgTests.cpp

Lines changed: 0 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -53,140 +53,6 @@ static int elemSize(ComponentType CT) {
5353
}
5454
}
5555

56-
/// Create a ShaderOp for a compute shader dispatch.
57-
static std::unique_ptr<st::ShaderOp>
58-
createComputeOp(const char *Source, const char *Target, const char *RootSig,
59-
const char *Args = nullptr, UINT DispatchX = 1,
60-
UINT DispatchY = 1, UINT DispatchZ = 1) {
61-
auto Op = std::make_unique<st::ShaderOp>();
62-
LPCSTR CSName = Op->Strings.insert("CS");
63-
Op->Name = CSName;
64-
Op->CS = CSName;
65-
Op->RootSignature = Op->Strings.insert(RootSig);
66-
Op->DispatchX = DispatchX;
67-
Op->DispatchY = DispatchY;
68-
Op->DispatchZ = DispatchZ;
69-
Op->UseWarpDevice = true;
70-
71-
st::ShaderOpShader Shader = {};
72-
Shader.Name = CSName;
73-
Shader.Target = Op->Strings.insert(Target);
74-
Shader.EntryPoint = Op->Strings.insert("main");
75-
Shader.Text = Op->Strings.insert(Source);
76-
Shader.Arguments = Args ? Op->Strings.insert(Args) : nullptr;
77-
Shader.Compiled = FALSE;
78-
Shader.Callback = FALSE;
79-
Op->Shaders.push_back(Shader);
80-
81-
return Op;
82-
}
83-
84-
/// Add a UAV buffer resource to a ShaderOp.
85-
static void addUAVBuffer(st::ShaderOp *Op, const char *Name, UINT64 Width,
86-
bool ReadBack, const char *Init = "zero") {
87-
st::ShaderOpResource Res = {};
88-
Res.Name = Op->Strings.insert(Name);
89-
Res.Init = Op->Strings.insert(Init);
90-
Res.ReadBack = ReadBack ? TRUE : FALSE;
91-
92-
Res.HeapProperties.Type = D3D12_HEAP_TYPE_DEFAULT;
93-
Res.HeapFlags = D3D12_HEAP_FLAG_NONE;
94-
Res.Desc.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER;
95-
Res.Desc.Width = Width;
96-
Res.Desc.Height = 1;
97-
Res.Desc.DepthOrArraySize = 1;
98-
Res.Desc.MipLevels = 1;
99-
Res.Desc.SampleDesc.Count = 1;
100-
Res.Desc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR;
101-
Res.Desc.Flags = D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS;
102-
Res.InitialResourceState = D3D12_RESOURCE_STATE_COPY_DEST;
103-
Res.TransitionTo = D3D12_RESOURCE_STATE_UNORDERED_ACCESS;
104-
105-
Op->Resources.push_back(Res);
106-
}
107-
108-
/// Bind a resource to a root UAV parameter by index.
109-
static void addRootUAV(st::ShaderOp *Op, UINT Index, const char *ResName) {
110-
st::ShaderOpRootValue RV = {};
111-
RV.ResName = Op->Strings.insert(ResName);
112-
RV.HeapName = nullptr;
113-
RV.Index = Index;
114-
Op->RootValues.push_back(RV);
115-
}
116-
117-
/// Run a programmatically-built ShaderOp and return the result.
118-
static std::shared_ptr<st::ShaderOpTestResult>
119-
runShaderOp(ID3D12Device *Device, dxc::SpecificDllLoader &DxcSupport,
120-
std::unique_ptr<st::ShaderOp> Op,
121-
st::ShaderOpTest::TInitCallbackFn InitCallback = nullptr) {
122-
auto OpSet = std::make_shared<st::ShaderOpSet>();
123-
OpSet->ShaderOps.push_back(std::move(Op));
124-
125-
return st::RunShaderOpTestAfterParse(
126-
Device, DxcSupport, nullptr, std::move(InitCallback), std::move(OpSet));
127-
}
128-
129-
/// Compiles an HLSL shader using the DXC API to verify it is well-formed.
130-
/// Fails the test on compile error.
131-
static void compileShader(dxc::SpecificDllLoader &DxcSupport,
132-
const char *Source, const char *Target,
133-
const std::string &Args) {
134-
CComPtr<IDxcCompiler3> Compiler;
135-
VERIFY_SUCCEEDED(DxcSupport.CreateInstance(CLSID_DxcCompiler, &Compiler));
136-
137-
CComPtr<IDxcUtils> Utils;
138-
VERIFY_SUCCEEDED(DxcSupport.CreateInstance(CLSID_DxcUtils, &Utils));
139-
140-
CComPtr<IDxcBlobEncoding> SourceBlob;
141-
VERIFY_SUCCEEDED(Utils->CreateBlobFromPinned(
142-
Source, static_cast<UINT32>(strlen(Source)), DXC_CP_UTF8, &SourceBlob));
143-
144-
// Build wide-string argument list: -T <target> -E main <extra args>.
145-
std::vector<std::wstring> WArgStorage;
146-
WArgStorage.push_back(L"-T");
147-
WArgStorage.push_back(std::wstring(Target, Target + strlen(Target)));
148-
WArgStorage.push_back(L"-E");
149-
WArgStorage.push_back(L"main");
150-
151-
// Tokenize the additional arguments string.
152-
std::istringstream SS(Args);
153-
std::string Tok;
154-
while (SS >> Tok)
155-
WArgStorage.push_back(std::wstring(Tok.begin(), Tok.end()));
156-
157-
std::vector<LPCWSTR> WArgPtrs;
158-
std::wstringstream LogFlags;
159-
LogFlags << L"Compiling with flags:";
160-
for (const auto &A : WArgStorage) {
161-
WArgPtrs.push_back(A.c_str());
162-
LogFlags << L" " << A;
163-
}
164-
165-
DxcBuffer Buf = {};
166-
Buf.Ptr = SourceBlob->GetBufferPointer();
167-
Buf.Size = SourceBlob->GetBufferSize();
168-
Buf.Encoding = DXC_CP_UTF8;
169-
170-
hlsl_test::LogCommentFmt(LogFlags.str().c_str());
171-
172-
CComPtr<IDxcResult> Result;
173-
VERIFY_SUCCEEDED(Compiler->Compile(&Buf, WArgPtrs.data(),
174-
static_cast<UINT32>(WArgPtrs.size()),
175-
nullptr, IID_PPV_ARGS(&Result)));
176-
177-
HRESULT HR;
178-
VERIFY_SUCCEEDED(Result->GetStatus(&HR));
179-
180-
if (FAILED(HR)) {
181-
CComPtr<IDxcBlobUtf8> Errors;
182-
Result->GetOutput(DXC_OUT_ERRORS, IID_PPV_ARGS(&Errors), nullptr);
183-
if (Errors && Errors->GetStringLength() > 0)
184-
hlsl_test::LogErrorFmt(L"Shader compilation failed:\n%S",
185-
Errors->GetStringPointer());
186-
VERIFY_SUCCEEDED(HR);
187-
}
188-
}
189-
19056
struct MatrixParams {
19157
ComponentType CompType;
19258
int M;

0 commit comments

Comments
 (0)