mame/3rdparty/bx/tools/bin2c/bin2c.cpp
Julian Sikorski 0837e7451a WIP: sync bgfx, bx and bimg with latest upstream (#5723)
* Sync with bgfx upstream revision b91d0b6

* Sync with bx upstream revision d60912b

* Sync with bimg upstream revision bd81f60

* Add astc-codec decoder

* Rename VertexDecl to VertexLayout

* Rename UniformType enum Int1 to Sampler.

* Add NVN stub

* Fix unused-const-variable error on macOS

* Drop redundant explicit language parameters
buildoptions_cpp are only applied to c++ files and buildoptions_objcpp are only
applied to objective c++ files. As such, hardcoding -x offers no benefit while
preventing overrides (such as one needed by 3rdparty/bgfx/src/renderer_vk.cpp on
macOS) from working.

* Re-introduce -x c++ in places where C code is compiled as C++ to prevent clang from throwing a warning

* Build bgfx as Objective-C++ on macOS
It is needed due to included headers

* Enable Direct3D12 and Vulkan bgfx rendering backends

* Enable building of spirv shaders

* Properly escape /c in cmd call

* Comment out dx12 bgfx renderer

* Honor VERBOSE setting during shaders build

* Only invert hlsl shader XYZ_TO_sRGB matrix for opengl

* Add spirv shaders

* OpenGL ES needs transposed matrix too

* Metal needs transposed matrix as well
2019-10-13 07:50:38 -04:00

259 lines
5.3 KiB
C++

/*
* Copyright 2011-2019 Branimir Karadzic. All rights reserved.
* License: https://github.com/bkaradzic/bx#license-bsd-2-clause
*/
#include <bx/allocator.h>
#include <bx/commandline.h>
#include <bx/file.h>
#include <bx/string.h>
#include <bx/debug.h>
class Bin2cWriter : public bx::WriterI
{
public:
Bin2cWriter(bx::AllocatorI* _allocator, const bx::StringView& _name)
: m_mb(_allocator)
, m_mw(&m_mb)
, m_name(_name)
, m_outputAsCStr(false)
{
}
virtual ~Bin2cWriter()
{
}
virtual int32_t write(const void* _data, int32_t _size, bx::Error* _err) override
{
m_outputAsCStr = true;
const char* data = (const char*)_data;
for (int32_t ii = 0; ii < _size; ++ii)
{
char ch = data[ii];
if (!bx::isPrint(ch)
&& !bx::isSpace(ch) )
{
m_outputAsCStr = false;
break;
}
}
return bx::write(&m_mw, _data, _size, _err);
}
void output(bx::WriterI* _writer)
{
if (m_outputAsCStr)
{
outputString(_writer);
}
else
{
outputHex(_writer);
}
}
void outputString(bx::WriterI* _writer)
{
const char* data = (const char*)m_mb.more(0);
uint32_t size = uint32_t(bx::seek(&m_mw) );
bx::Error err;
bx::write(
_writer
, &err
, "static const char* %.*s = /* Generated with bin2c. */\n\t\""
, m_name.getLength()
, m_name.getPtr()
);
if (NULL != data)
{
bool escaped = false;
for (uint32_t ii = 0; ii < size; ++ii)
{
char ch = data[ii];
if (!escaped)
{
switch (ch)
{
case '\"': bx::write(_writer, "\\\"", &err); break;
case '\n': bx::write(_writer, "\\n\"\n\t\"", &err); break;
case '\r': bx::write(_writer, "\\r", &err); break;
case '\\': escaped = true; BX_FALLTHROUGH;
default: bx::write(_writer, ch, &err); break;
}
}
else
{
switch (ch)
{
case '\n': bx::write(_writer, "\\\"\n\t\"", &err); break;
case '\r': BX_FALLTHROUGH;
case '\t': bx::write(_writer, "\\", &err); BX_FALLTHROUGH;
default : bx::write(_writer, ch, &err); break;
}
escaped = false;
}
}
}
bx::write(_writer, &err, "\"\n\t;\n");
}
void outputHex(bx::WriterI* _writer)
{
#define HEX_DUMP_WIDTH 16
#define HEX_DUMP_SPACE_WIDTH 96
#define HEX_DUMP_FORMAT "%-" BX_STRINGIZE(HEX_DUMP_SPACE_WIDTH) "." BX_STRINGIZE(HEX_DUMP_SPACE_WIDTH) "s"
const char* data = (const char*)m_mb.more(0);
uint32_t size = uint32_t(bx::seek(&m_mw) );
bx::Error err;
bx::write(
_writer
, &err
, "static const uint8_t %.*s[%d] = /* Generated with bin2c. */\n{\n"
, m_name.getLength()
, m_name.getPtr()
, size
);
if (NULL != data)
{
char hex[HEX_DUMP_SPACE_WIDTH+1];
char ascii[HEX_DUMP_WIDTH+1];
uint32_t hexPos = 0;
uint32_t asciiPos = 0;
for (uint32_t ii = 0; ii < size; ++ii)
{
bx::snprintf(&hex[hexPos], sizeof(hex)-hexPos, "0x%02x, ", data[asciiPos]);
hexPos += 6;
ascii[asciiPos] = bx::isPrint(data[asciiPos]) && data[asciiPos] != '\\' ? data[asciiPos] : '.';
asciiPos++;
if (HEX_DUMP_WIDTH == asciiPos)
{
ascii[asciiPos] = '\0';
bx::write(_writer, &err, "\t" HEX_DUMP_FORMAT "// %s\n", hex, ascii);
data += asciiPos;
hexPos = 0;
asciiPos = 0;
}
}
if (0 != asciiPos)
{
ascii[asciiPos] = '\0';
bx::write(_writer, &err, "\t" HEX_DUMP_FORMAT "// %s\n", hex, ascii);
}
}
bx::write(_writer, &err, "};\n");
#undef HEX_DUMP_WIDTH
#undef HEX_DUMP_SPACE_WIDTH
#undef HEX_DUMP_FORMAT
}
bx::MemoryBlock m_mb;
bx::MemoryWriter m_mw;
bx::StringView m_name;
bool m_outputAsCStr;
};
void help(const char* _error = NULL)
{
bx::WriterI* stdOut = bx::getStdOut();
bx::Error err;
if (NULL != _error)
{
bx::write(stdOut, &err, "Error:\n%s\n\n", _error);
}
bx::write(stdOut, &err
, "bin2c, binary to C\n"
"Copyright 2011-2019 Branimir Karadzic. All rights reserved.\n"
"License: https://github.com/bkaradzic/bx#license-bsd-2-clause\n\n"
);
bx::write(stdOut, &err
, "Usage: bin2c -f <in> -o <out> -n <name>\n"
"\n"
"Options:\n"
" -f <file path> Input file path.\n"
" -o <file path> Output file path.\n"
" -n <name> Array name.\n"
"\n"
"For additional information, see https://github.com/bkaradzic/bx\n"
);
}
int main(int _argc, const char* _argv[])
{
bx::CommandLine cmdLine(_argc, _argv);
if (cmdLine.hasArg('h', "help") )
{
help();
return bx::kExitFailure;
}
bx::FilePath filePath = cmdLine.findOption('f');
if (filePath.isEmpty() )
{
help("Input file name must be specified.");
return bx::kExitFailure;
}
bx::FilePath outFilePath = cmdLine.findOption('o');
if (outFilePath.isEmpty() )
{
help("Output file name must be specified.");
return bx::kExitFailure;
}
bx::StringView name = cmdLine.findOption('n');
if (name.isEmpty() )
{
name.set("data");
}
void* data = NULL;
uint32_t size = 0;
bx::FileReader fr;
if (bx::open(&fr, filePath) )
{
size = uint32_t(bx::getSize(&fr) );
bx::DefaultAllocator allocator;
data = BX_ALLOC(&allocator, size);
bx::read(&fr, data, size);
bx::close(&fr);
bx::FileWriter fw;
if (bx::open(&fw, outFilePath) )
{
Bin2cWriter writer(&allocator, name);
bx::write(&writer, data, size);
writer.output(&fw);
bx::close(&fw);
}
BX_FREE(&allocator, data);
}
return 0;
}