From 4337338ed2d9525b4638f32c6b91ef61b69e41cd Mon Sep 17 00:00:00 2001 From: jsmall-nvidia Date: Thu, 10 Dec 2020 14:04:29 -0500 Subject: Building with embedded stdlib (#1634) * #include an absolute path didn't work - because paths were taken to always be relative. * Move reflection to reflection-api. * Slight reorg to pull out potentially Slang internal functions from the reflection API impls. * Remove visual studio projects * Fix for slang-binaries copy. * Add the visual studio projects in build/visual-studio * Remove miniz project. * Differentiate the linePath from the filePath. * Improve comment in premake5.lua + to kick of CI. * Kick CI. * Use COM compile request for calls to functions inside api-less-slang. Add static-slang project. * Fix const typo issue. * Don't include 'core' link in 'api-less-slang' * Removed static-slang lib causes problems on linux with linking. Embed Slang stdlib Added StaticBlob Added dumpSourceBytes Use ConstArrayView for the archive. At startup allow loading of zip with stdlib. Made -save-stdlib -load-stdlib take a name Added '-save-stdlib-bin-source' to save out serialized stdlib as source. * Ability enable/disable stdlib embedding. * Fix problem with moduleDecl not having module pointer set when serialized in. * Set of debugdir for slang-test and examples. * Add slang-stdlib-api.cpp * Update slang filters for VS. * Try to use pic, and -mcmodel=medium * Some more efforts ot make premake work. * WIP premake5.lua from previously working version. * Remove api-less-slang project. * Disable dllexport on gcc/clang. * Embed via slangc-bootstrap. * Fix slang-profile. Always compiles without stdlib. * Use pic "On" * Remove slangc-bootstrap and embed-stdlib-generator if embedding not required. Make bootstrap run the generators. * Improve comments in premake5.lua. Kick off another CI build. * Remove generation of stdlib source from std-lib-serialize.slang --- source/core/slang-blob.cpp | 10 +++++++++ source/core/slang-blob.h | 29 +++++++++++++++++++++++++ source/core/slang-hex-dump-util.cpp | 40 +++++++++++++++++++++++++++++++++++ source/core/slang-hex-dump-util.h | 4 ++++ source/core/slang-stream.h | 3 +++ source/core/slang-zip-file-system.cpp | 4 ++-- source/core/slang-zip-file-system.h | 2 +- 7 files changed, 89 insertions(+), 3 deletions(-) (limited to 'source/core') diff --git a/source/core/slang-blob.cpp b/source/core/slang-blob.cpp index 4421db09a..becccf51c 100644 --- a/source/core/slang-blob.cpp +++ b/source/core/slang-blob.cpp @@ -11,4 +11,14 @@ ISlangUnknown* BlobBase::getInterface(const Guid& guid) return (guid == IID_ISlangUnknown || guid == IID_ISlangBlob) ? static_cast(this) : nullptr; } +SlangResult StaticBlob::queryInterface(SlangUUID const& guid, void** outObject) +{ + if (guid == IID_ISlangUnknown || guid == IID_ISlangBlob) + { + *outObject = static_cast(this); + return SLANG_OK; + } + return SLANG_E_NO_INTERFACE; +} + } // namespace Slang diff --git a/source/core/slang-blob.h b/source/core/slang-blob.h index d326f9c9c..c98c41563 100644 --- a/source/core/slang-blob.h +++ b/source/core/slang-blob.h @@ -26,6 +26,7 @@ protected: }; /** A blob that uses a `String` for its storage. +NOTE! Returns length *WITHOUT* terminating 0, even though there is one. */ class StringBlob : public BlobBase { @@ -191,6 +192,34 @@ protected: }; +/** A Blob that has no ref counting and exists typically for entire execution. +The memory it references is *not* owned by the blob. +This is useful when a Blob is useful to represent some global immutable chunk of memory. +*/ +class StaticBlob : public ISlangBlob +{ +public: + + // ISlangUnknown + SLANG_NO_THROW SlangResult SLANG_MCALL queryInterface(SlangUUID const& uuid, void** outObject) SLANG_OVERRIDE; + SLANG_NO_THROW uint32_t SLANG_MCALL addRef() SLANG_OVERRIDE { return 1; } + SLANG_NO_THROW uint32_t SLANG_MCALL release() SLANG_OVERRIDE { return 1; } + + // ISlangBlob + SLANG_NO_THROW void const* SLANG_MCALL getBufferPointer() SLANG_OVERRIDE { return m_data; } + SLANG_NO_THROW size_t SLANG_MCALL getBufferSize() SLANG_OVERRIDE { return m_dataCount; } + + StaticBlob(const void* data, size_t dataCount): + m_data(data), + m_dataCount(dataCount) + { + } + +protected: + const void* m_data; + size_t m_dataCount; +}; + /// Create a blob that will retain (a copy of) raw data. /// inline ComPtr createRawBlob(void const* inData, size_t size) diff --git a/source/core/slang-hex-dump-util.cpp b/source/core/slang-hex-dump-util.cpp index cb7187499..b493141a1 100644 --- a/source/core/slang-hex-dump-util.cpp +++ b/source/core/slang-hex-dump-util.cpp @@ -53,6 +53,46 @@ static const char s_hex[] = "0123456789abcdef"; return dump(data.getBuffer(), data.getCount(), maxBytesPerLine, writer); } +SlangResult HexDumpUtil::dumpSourceBytes(const uint8_t* data, size_t dataCount, int maxBytesPerLine, ISlangWriter* writer) +{ + const uint8_t* cur = data; + const uint8_t* end = data + dataCount; + + while (cur < end) + { + size_t count = size_t(end - cur); + count = (count > size_t(maxBytesPerLine)) ? size_t(maxBytesPerLine) : count; + + // each byte is output as "0xAA, " + // Ends with '\n" + const size_t lineBytes = count * (4 + 1 + 1) * count + 1; + + char* startDst = writer->beginAppendBuffer(lineBytes); + char* dst = startDst; + + for (size_t i = 0; i < count; ++i) + { + uint8_t byte = cur[i]; + dst[0] = '0'; + dst[1] = 'x'; + dst[2] = s_hex[byte >> 4]; + dst[3] = s_hex[byte & 0xf]; + dst[4] = ','; + dst[5] = ' '; + + dst += 6; + } + + *dst++ = '\n'; + + SLANG_RETURN_ON_FAIL(writer->endAppendBuffer(startDst, size_t(dst - startDst))); + + cur += count; + } + + return SLANG_OK; +} + /* static */SlangResult HexDumpUtil::dump(const uint8_t* data, size_t dataCount, int maxBytesPerLine, ISlangWriter* writer) { int maxCharsPerLine = 2 * maxBytesPerLine + 1 + maxBytesPerLine + 1; diff --git a/source/core/slang-hex-dump-util.h b/source/core/slang-hex-dump-util.h index f38bfde78..f02728522 100644 --- a/source/core/slang-hex-dump-util.h +++ b/source/core/slang-hex-dump-util.h @@ -12,6 +12,10 @@ namespace Slang struct HexDumpUtil { + /// Dump out bytes in source format - as in + /// 0x10, 0xab, + static SlangResult dumpSourceBytes(const uint8_t* data, size_t dataCount, int maxBytesPerLine, ISlangWriter* writer); + /// Dump data to writer, with lines starting with hex data static SlangResult dump(const List& data, int numBytesPerLine, ISlangWriter* writer); diff --git a/source/core/slang-stream.h b/source/core/slang-stream.h index c4064871b..730ce70be 100644 --- a/source/core/slang-stream.h +++ b/source/core/slang-stream.h @@ -77,6 +77,9 @@ public: virtual bool canWrite() SLANG_OVERRIDE { return (int(m_access) & int(FileAccess::Write)) != 0; } virtual void close() SLANG_OVERRIDE { m_access = FileAccess::None; } + /// Get the contents + ConstArrayView getContents() const { return ConstArrayView(m_contents, m_contentsSize); } + MemoryStreamBase(FileAccess access = FileAccess::Read, const void* contents = nullptr, size_t contentsSize = 0): m_access(access) { diff --git a/source/core/slang-zip-file-system.cpp b/source/core/slang-zip-file-system.cpp index ea9a39445..8547f7eac 100644 --- a/source/core/slang-zip-file-system.cpp +++ b/source/core/slang-zip-file-system.cpp @@ -49,7 +49,7 @@ public: virtual SLANG_NO_THROW SlangResult SLANG_MCALL createDirectory(const char* path) SLANG_OVERRIDE; // CompressedFileSystem - virtual ArrayView getArchive() SLANG_OVERRIDE; + virtual ConstArrayView getArchive() SLANG_OVERRIDE; virtual void setCompressionType(CompressionType type) SLANG_OVERRIDE; ZipFileSystem(); @@ -834,7 +834,7 @@ SlangResult ZipFileSystem::createDirectory(const char* path) return SLANG_OK; } -ArrayView ZipFileSystem::getArchive() +ConstArrayView ZipFileSystem::getArchive() { // If we have anything deleted in 'Read', we need to convert to 'Write' and then back to read if (m_mode == Mode::Read && !m_removedSet.isEmpty()) diff --git a/source/core/slang-zip-file-system.h b/source/core/slang-zip-file-system.h index 47c7b8db3..4a95cd51c 100644 --- a/source/core/slang-zip-file-system.h +++ b/source/core/slang-zip-file-system.h @@ -19,7 +19,7 @@ public: }; /// Get as an archive (that can be saved to disk) - virtual ArrayView getArchive() = 0; + virtual ConstArrayView getArchive() = 0; /// Set the compression - used for any subsequent items added virtual void setCompressionType(CompressionType type) = 0; -- cgit v1.2.3