Skip to content

Commit

Permalink
Adding toascii output cmd line argument
Browse files Browse the repository at this point in the history
  • Loading branch information
patrick-lunarg authored and patrick-lunarg committed Feb 14, 2022
1 parent 00ba37f commit 7618377
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 83 deletions.
23 changes: 5 additions & 18 deletions framework/decode/vulkan_ascii_consumer_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@
#include "decode/custom_vulkan_ascii_consumer.h"
#include "generated/generated_vulkan_ascii_consumer.h"

#include "util/platform.h"

GFXRECON_BEGIN_NAMESPACE(gfxrecon)
GFXRECON_BEGIN_NAMESPACE(decode)

Expand All @@ -37,30 +35,19 @@ VulkanAsciiConsumerBase::~VulkanAsciiConsumerBase()
Destroy();
}

bool VulkanAsciiConsumerBase::Initialize(const std::string& filename)
void VulkanAsciiConsumerBase::Initialize(FILE* file)
{
bool success = false;

if (file_ == nullptr)
{
int32_t result = util::platform::FileOpen(&file_, filename.c_str(), "w");
if (result == 0)
{
success = true;
filename_ = filename;
fprintf(file_, "{");
}
}

return success;
assert(file);
file_ = file;
fprintf(file_, "{");
}

void VulkanAsciiConsumerBase::Destroy()
{
if (file_ != nullptr)
{
fprintf(file_, "\n}\n");
util::platform::FileClose(file_);
file_ = nullptr;
}
}

Expand Down
9 changes: 3 additions & 6 deletions framework/decode/vulkan_ascii_consumer_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,12 @@ class VulkanAsciiConsumerBase : public VulkanConsumer

virtual ~VulkanAsciiConsumerBase() override;

bool Initialize(const std::string& filename);
void Initialize(FILE* file);

void Destroy();

bool IsValid() const { return (file_ != nullptr); }

const std::string& GetFilename() const { return filename_; }

virtual void
Process_vkAllocateCommandBuffers(VkResult returnValue,
format::HandleId device,
Expand Down Expand Up @@ -117,9 +115,8 @@ class VulkanAsciiConsumerBase : public VulkanConsumer
}

private:
FILE* file_;
std::string filename_;
uint64_t api_call_count_{ 0 };
FILE* file_{ nullptr };
uint64_t api_call_count_{ 0 };
};

GFXRECON_END_NAMESPACE(decode)
Expand Down
5 changes: 5 additions & 0 deletions framework/util/argument_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,11 @@ bool ArgumentParser::IsOptionSet(const std::string& option) const
return false;
}

bool ArgumentParser::IsArgumentSet(const std::string& argument) const
{
return static_cast<bool>(arguments_indices_.count(argument));
}

const std::string& ArgumentParser::GetArgumentValue(const std::string& argument) const
{
static const std::string empty_string;
Expand Down
1 change: 1 addition & 0 deletions framework/util/argument_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class ArgumentParser
bool IsInvalid() const { return is_invalid_; }
const std::vector<std::string>& GetInvalidArgumentOrOptions() const { return invalid_values_present_; };
bool IsOptionSet(const std::string& option) const;
bool IsArgumentSet(const std::string& argument) const;
const std::string& GetArgumentValue(const std::string& argument) const;
size_t GetPositionalArgumentsCount() const { return positional_arguments_present_.size(); }
const std::vector<std::string>& GetPositionalArguments() const { return positional_arguments_present_; }
Expand Down
2 changes: 1 addition & 1 deletion tools/toascii/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ target_sources(gfxrecon-toascii
${CMAKE_CURRENT_LIST_DIR}/main.cpp
)

target_include_directories(gfxrecon-toascii PUBLIC ${CMAKE_BINARY_DIR})
target_include_directories(gfxrecon-toascii PUBLIC ${CMAKE_BINARY_DIR} ${CMAKE_CURRENT_LIST_DIR}/..)

target_link_libraries(gfxrecon-toascii gfxrecon_decode gfxrecon_graphics gfxrecon_format gfxrecon_util platform_specific)

Expand Down
113 changes: 55 additions & 58 deletions tools/toascii/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,15 @@

#include "project_version.h"

#include "decode/file_processor.h"
#include "tool_settings.h"
#include "format/format.h"
#include "generated/generated_vulkan_ascii_consumer.h"
#include "generated/generated_vulkan_decoder.h"
#include "util/argument_parser.h"
#include "util/logging.h"

#include "vulkan/vulkan_core.h"

#include <cstdlib>

const char kHelpShortOption[] = "-h";
const char kHelpLongOption[] = "--help";
const char kVersionOption[] = "--version";
const char kNoDebugPopup[] = "--no-debug-popup";
#include "util/platform.h"

const char kOptions[] = "-h|--help,--version,--no-debug-popup";

const char kArguments[] = "--output";

static void PrintUsage(const char* exe_name)
{
std::string app_name = exe_name;
Expand All @@ -58,53 +49,40 @@ static void PrintUsage(const char* exe_name)
GFXRECON_WRITE_CONSOLE("\nOptional arguments:");
GFXRECON_WRITE_CONSOLE(" -h\t\t\tPrint usage information and exit (same as --help).");
GFXRECON_WRITE_CONSOLE(" --version\t\tPrint version information and exit.");
GFXRECON_WRITE_CONSOLE(" --output file\t\t'stdout' or a path to a file to write JSON output");
GFXRECON_WRITE_CONSOLE(" \t\tto. Default is the input filepath with \"gfxr\" replaced by \"txt\".");
#if defined(WIN32) && defined(_DEBUG)
GFXRECON_WRITE_CONSOLE(" --no-debug-popup\tDisable the 'Abort, Retry, Ignore' message box");
GFXRECON_WRITE_CONSOLE(" \t\tdisplayed when abort() is called (Windows debug only).");
#endif
}

static bool CheckOptionPrintUsage(const char* exe_name, const gfxrecon::util::ArgumentParser& arg_parser)
static std::string GetOutputFileName(const gfxrecon::util::ArgumentParser& arg_parser,
const std::string& input_filename)
{
if (arg_parser.IsOptionSet(kHelpShortOption) || arg_parser.IsOptionSet(kHelpLongOption))
std::string output_filename;
if (arg_parser.IsArgumentSet(kOutput))
{
PrintUsage(exe_name);
return true;
output_filename = arg_parser.GetArgumentValue(kOutput);
}

return false;
}

static bool CheckOptionPrintVersion(const char* exe_name, const gfxrecon::util::ArgumentParser& arg_parser)
{
if (arg_parser.IsOptionSet(kVersionOption))
else
{
std::string app_name = exe_name;
size_t dir_location = app_name.find_last_of("/\\");

if (dir_location >= 0)
std::string output_filename = input_filename;
size_t suffix_pos = output_filename.find(GFXRECON_FILE_EXTENSION);
if (suffix_pos != std::string::npos)
{
app_name.replace(0, dir_location + 1, "");
output_filename = output_filename.substr(0, suffix_pos);
}

GFXRECON_WRITE_CONSOLE("%s version info:", app_name.c_str());
GFXRECON_WRITE_CONSOLE(" GFXReconstruct Version %s", GFXRECON_PROJECT_VERSION_STRING);
GFXRECON_WRITE_CONSOLE(" Vulkan Header Version %u.%u.%u",
VK_VERSION_MAJOR(VK_HEADER_VERSION_COMPLETE),
VK_VERSION_MINOR(VK_HEADER_VERSION_COMPLETE),
VK_VERSION_PATCH(VK_HEADER_VERSION_COMPLETE));

return true;
output_filename += ".txt";
}

return false;
return output_filename;
}

int main(int argc, const char** argv)
{
gfxrecon::util::Log::Init();

gfxrecon::util::ArgumentParser arg_parser(argc, argv, kOptions, "");
gfxrecon::util::ArgumentParser arg_parser(argc, argv, kOptions, kArguments);

if (CheckOptionPrintUsage(argv[0], arg_parser) || CheckOptionPrintVersion(argv[0], arg_parser))
{
Expand All @@ -117,6 +95,12 @@ int main(int argc, const char** argv)
gfxrecon::util::Log::Release();
exit(-1);
}
else if (arg_parser.IsArgumentSet(kOutput) && arg_parser.GetArgumentValue(kOutput).empty())
{
GFXRECON_LOG_ERROR("Empty string given for argument \"--output\"; must be a valid path or 'stdout'");
gfxrecon::util::Log::Release();
exit(-1);
}
else
{
#if defined(WIN32) && defined(_DEBUG)
Expand All @@ -127,28 +111,41 @@ int main(int argc, const char** argv)
#endif
}

const std::vector<std::string>& positional_arguments = arg_parser.GetPositionalArguments();
std::string input_filename = positional_arguments[0];
std::string output_filename = input_filename;
size_t suffix_pos = output_filename.find(GFXRECON_FILE_EXTENSION);
if (suffix_pos != std::string::npos)
{
output_filename = output_filename.substr(0, suffix_pos);
}

output_filename += ".txt";
const auto& positional_arguments = arg_parser.GetPositionalArguments();
std::string input_filename = positional_arguments[0];
std::string output_filename = GetOutputFileName(arg_parser, input_filename);

gfxrecon::decode::FileProcessor file_processor;
if (file_processor.Initialize(input_filename))
{
gfxrecon::decode::VulkanDecoder decoder;
gfxrecon::decode::VulkanAsciiConsumer ascii_consumer;

ascii_consumer.Initialize(output_filename);
decoder.AddConsumer(&ascii_consumer);
FILE* output_file = nullptr;
if (gfxrecon::util::platform::StringCompare(output_filename.c_str(), "stdout") == 0)
{
output_file = stdout;
}
else
{
gfxrecon::util::platform::FileOpen(&output_file, output_filename.c_str(), "w");
}

file_processor.AddDecoder(&decoder);
file_processor.ProcessAllFrames();
if (output_file)
{
gfxrecon::decode::VulkanAsciiConsumer ascii_consumer;
ascii_consumer.Initialize(output_file);
gfxrecon::decode::VulkanDecoder decoder;
decoder.AddConsumer(&ascii_consumer);
file_processor.AddDecoder(&decoder);
file_processor.ProcessAllFrames();
ascii_consumer.Destroy();
if (output_file != stdout)
{
gfxrecon::util::platform::FileClose(output_file);
}
}
else
{
GFXRECON_LOG_ERROR("Failed to open/create output file \"%s\"; is the path valid?", output_filename.c_str());
}
}

gfxrecon::util::Log::Release();
Expand Down
1 change: 1 addition & 0 deletions tools/tool_settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ const char kScreenshotRangeArgument[] = "--screenshots";
const char kScreenshotFormatArgument[] = "--screenshot-format";
const char kScreenshotDirArgument[] = "--screenshot-dir";
const char kScreenshotFilePrefixArgument[] = "--screenshot-prefix";
const char kOutput[] = "--output";
#if defined(WIN32)
const char kApiFamilyOption[] = "--api";
#endif
Expand Down

0 comments on commit 7618377

Please sign in to comment.