DawnTest: Factor Instance creation, Adapter discovery, and config logging

Bug: dawn:396
Change-Id: Idae3f388cf001e97c7fb4c84c50bca6825a2020b
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/21761
Reviewed-by: Corentin Wallez <cwallez@chromium.org>
Reviewed-by: Stephen White <senorblanco@chromium.org>
Commit-Queue: Austin Eng <enga@chromium.org>
This commit is contained in:
Austin Eng 2020-05-15 16:04:32 +00:00 committed by Commit Bot service account
parent 0b8e319407
commit 5133221139
3 changed files with 86 additions and 72 deletions

View File

@ -146,7 +146,13 @@ void InitDawnEnd2EndTestEnvironment(int argc, char** argv) {
testing::AddGlobalTestEnvironment(gTestEnv);
}
// static
void DawnTestEnvironment::SetEnvironment(DawnTestEnvironment* env) {
gTestEnv = env;
}
DawnTestEnvironment::DawnTestEnvironment(int argc, char** argv) {
size_t argLen = 0; // Set when parsing --arg=X arguments
for (int i = 1; i < argc; ++i) {
if (strcmp("-w", argv[i]) == 0 || strcmp("--use-wire", argv[i]) == 0) {
mUseWire = true;
@ -235,8 +241,9 @@ DawnTestEnvironment::DawnTestEnvironment(int argc, char** argv) {
}
constexpr const char kVendorIdFilterArg[] = "--adapter-vendor-id=";
if (strstr(argv[i], kVendorIdFilterArg) == argv[i]) {
const char* vendorIdFilter = argv[i] + strlen(kVendorIdFilterArg);
argLen = sizeof(kVendorIdFilterArg) - 1;
if (strncmp(argv[i], kVendorIdFilterArg, argLen) == 0) {
const char* vendorIdFilter = argv[i] + argLen;
if (vendorIdFilter[0] != '\0') {
mVendorIdFilter = strtoul(vendorIdFilter, nullptr, 16);
// Set filter flag if vendor id is non-zero.
@ -246,8 +253,9 @@ DawnTestEnvironment::DawnTestEnvironment(int argc, char** argv) {
}
constexpr const char kWireTraceDirArg[] = "--wire-trace-dir=";
if (strstr(argv[i], kWireTraceDirArg) == argv[i]) {
const char* wireTraceDir = argv[i] + strlen(kWireTraceDirArg);
argLen = sizeof(kWireTraceDirArg) - 1;
if (strncmp(argv[i], kWireTraceDirArg, argLen) == 0) {
const char* wireTraceDir = argv[i] + argLen;
if (wireTraceDir[0] != '\0') {
const char* sep = GetPathSeparator();
mWireTraceDir = wireTraceDir;
@ -281,45 +289,64 @@ DawnTestEnvironment::DawnTestEnvironment(int argc, char** argv) {
}
}
// static
void DawnTestEnvironment::SetEnvironment(DawnTestEnvironment* env) {
gTestEnv = env;
std::unique_ptr<dawn_native::Instance> DawnTestEnvironment::CreateInstanceAndDiscoverAdapters()
const {
auto instance = std::make_unique<dawn_native::Instance>();
instance->EnableBackendValidation(mEnableBackendValidation);
instance->EnableBeginCaptureOnStartup(mBeginCaptureOnStartup);
instance->DiscoverDefaultAdapters();
#ifdef DAWN_ENABLE_BACKEND_OPENGL
if (!glfwInit()) {
return instance;
}
glfwDefaultWindowHints();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 4);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLFW_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
std::string windowName = "Dawn OpenGL test window";
GLFWwindow* window = glfwCreateWindow(400, 400, windowName.c_str(), nullptr, nullptr);
glfwMakeContextCurrent(window);
dawn_native::opengl::AdapterDiscoveryOptions adapterOptions;
adapterOptions.getProc = reinterpret_cast<void* (*)(const char*)>(glfwGetProcAddress);
instance->DiscoverAdapters(&adapterOptions);
#endif // DAWN_ENABLE_BACKEND_OPENGL
return instance;
}
void DawnTestEnvironment::SetUp() {
mInstance = std::make_unique<dawn_native::Instance>();
mInstance->EnableBackendValidation(mEnableBackendValidation);
mInstance->EnableBeginCaptureOnStartup(mBeginCaptureOnStartup);
void DawnTestEnvironment::PrintTestConfigurationAndAdapterInfo() const {
dawn::LogMessage log = dawn::InfoLog();
log << "Testing configuration\n"
"---------------------\n"
"UseWire: "
<< (mUseWire ? "true" : "false")
<< "\n"
"EnableBackendValidation: "
<< (mEnableBackendValidation ? "true" : "false")
<< "\n"
"SkipDawnValidation: "
<< (mSkipDawnValidation ? "true" : "false")
<< "\n"
"UseSpvc: "
<< (mUseSpvc ? "true" : "false")
<< "\n"
"UseSpvcParser: "
<< (mUseSpvcParser ? "true" : "false")
<< "\n"
"BeginCaptureOnStartup: "
<< (mBeginCaptureOnStartup ? "true" : "false")
<< "\n"
"\n"
<< "System adapters: \n";
mInstance.get()->DiscoverDefaultAdapters();
DiscoverOpenGLAdapter();
dawn::InfoLog() << "Testing configuration\n"
"---------------------\n"
"UseWire: "
<< (mUseWire ? "true" : "false")
<< "\n"
"EnableBackendValidation: "
<< (mEnableBackendValidation ? "true" : "false")
<< "\n"
"SkipDawnValidation: "
<< (mSkipDawnValidation ? "true" : "false")
<< "\n"
"UseSpvc: "
<< (mUseSpvc ? "true" : "false")
<< "\n"
"UseSpvcParser: "
<< (mUseSpvcParser ? "true" : "false")
<< "\n"
"BeginCaptureOnStartup: "
<< (mBeginCaptureOnStartup ? "true" : "false")
<< "\n"
"\n"
<< "System adapters: \n";
for (const dawn_native::Adapter& adapter : mInstance->GetAdapters()) {
wgpu::AdapterProperties properties;
adapter.GetProperties(&properties);
std::ostringstream vendorId;
std::ostringstream deviceId;
vendorId << std::setfill('0') << std::uppercase << std::internal << std::hex << std::setw(4)
@ -328,19 +355,23 @@ void DawnTestEnvironment::SetUp() {
<< properties.deviceID;
// Preparing for outputting hex numbers
dawn::InfoLog() << std::showbase << std::hex << std::setfill('0') << std::setw(4)
log << std::showbase << std::hex << std::setfill('0') << std::setw(4)
<< " - \"" << properties.name << "\"\n"
<< " type: " << AdapterTypeName(properties.adapterType)
<< ", backend: " << ParamName(properties.backendType) << "\n"
<< " vendorId: 0x" << vendorId.str() << ", deviceId: 0x" << deviceId.str()
<< (mHasVendorIdFilter && mVendorIdFilter == properties.vendorID
? " [Selected]"
: "")
<< "\n";
<< " - \"" << properties.name << "\"\n"
<< " type: " << AdapterTypeName(properties.adapterType)
<< ", backend: " << ParamName(properties.backendType) << "\n"
<< " vendorId: 0x" << vendorId.str() << ", deviceId: 0x" << deviceId.str()
<< (mHasVendorIdFilter && mVendorIdFilter == properties.vendorID ? " [Selected]" : "")
<< "\n";
}
}
void DawnTestEnvironment::SetUp() {
mInstance = CreateInstanceAndDiscoverAdapters();
ASSERT(mInstance);
PrintTestConfigurationAndAdapterInfo();
}
void DawnTestEnvironment::TearDown() {
// When Vulkan validation layers are enabled, it's unsafe to call Vulkan APIs in the destructor
// of a static/global variable, so the instance must be manually released beforehand.
@ -386,27 +417,6 @@ const char* DawnTestEnvironment::GetWireTraceDir() const {
return mWireTraceDir.c_str();
}
void DawnTestEnvironment::DiscoverOpenGLAdapter() {
#ifdef DAWN_ENABLE_BACKEND_OPENGL
if (!glfwInit()) {
return;
}
glfwDefaultWindowHints();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 4);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLFW_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
std::string windowName = "Dawn OpenGL test window";
GLFWwindow* window = glfwCreateWindow(400, 400, windowName.c_str(), nullptr, nullptr);
glfwMakeContextCurrent(window);
dawn_native::opengl::AdapterDiscoveryOptions adapterOptions;
adapterOptions.getProc = reinterpret_cast<void* (*)(const char*)>(glfwGetProcAddress);
mInstance->DiscoverAdapters(&adapterOptions);
#endif // DAWN_ENABLE_BACKEND_OPENGL
}
class WireServerTraceLayer : public dawn_wire::CommandHandler {
public:
WireServerTraceLayer(const char* file, dawn_wire::CommandHandler* handler)

View File

@ -157,7 +157,8 @@ class DawnTestEnvironment : public testing::Environment {
std::unique_ptr<dawn_native::Instance> mInstance;
private:
void DiscoverOpenGLAdapter();
std::unique_ptr<dawn_native::Instance> CreateInstanceAndDiscoverAdapters() const;
void PrintTestConfigurationAndAdapterInfo() const;
bool mUseWire = false;
bool mEnableBackendValidation = false;

View File

@ -78,6 +78,7 @@ void InitDawnPerfTestEnvironment(int argc, char** argv) {
DawnPerfTestEnvironment::DawnPerfTestEnvironment(int argc, char** argv)
: DawnTestEnvironment(argc, argv) {
size_t argLen = 0; // Set when parsing --arg=X arguments
for (int i = 1; i < argc; ++i) {
if (strcmp("--calibration", argv[i]) == 0) {
mIsCalibrating = true;
@ -85,8 +86,9 @@ DawnPerfTestEnvironment::DawnPerfTestEnvironment(int argc, char** argv)
}
constexpr const char kOverrideStepsArg[] = "--override-steps=";
if (strstr(argv[i], kOverrideStepsArg) == argv[i]) {
const char* overrideSteps = argv[i] + strlen(kOverrideStepsArg);
argLen = sizeof(kOverrideStepsArg) - 1;
if (strncmp(argv[i], kOverrideStepsArg, argLen) == 0) {
const char* overrideSteps = argv[i] + argLen;
if (overrideSteps[0] != '\0') {
mOverrideStepsToRun = strtoul(overrideSteps, nullptr, 0);
}
@ -94,8 +96,9 @@ DawnPerfTestEnvironment::DawnPerfTestEnvironment(int argc, char** argv)
}
constexpr const char kTraceFileArg[] = "--trace-file=";
if (strstr(argv[i], kTraceFileArg) == argv[i]) {
const char* traceFile = argv[i] + strlen(kTraceFileArg);
argLen = sizeof(kTraceFileArg) - 1;
if (strncmp(argv[i], kTraceFileArg, argLen) == 0) {
const char* traceFile = argv[i] + argLen;
if (traceFile[0] != '\0') {
mTraceFile = traceFile;
}