From 85941342ecff030532fbbce1f1d1756c3990b540 Mon Sep 17 00:00:00 2001 From: REMqb Date: Sat, 27 Apr 2024 15:00:50 +0200 Subject: [PATCH] =?UTF-8?q?~=C2=A0some=20doc=20&=20formatting?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/CNZSL/Error.h | 3 +- include/CNZSL/Error.hpp | 7 +++- include/CNZSL/GlslWriter.h | 8 ++-- include/CNZSL/LangWriter.h | 8 ++-- include/CNZSL/Module.h | 2 +- include/CNZSL/Parser.h | 23 ++---------- include/CNZSL/SpirvWriter.h | 9 +++-- src/CNZSL/Error.cpp | 13 ++++--- src/CNZSL/GlslWriter.cpp | 74 +++++++++++++++++++++++-------------- src/CNZSL/LangWriter.cpp | 43 +++++++++++++-------- src/CNZSL/Module.cpp | 9 ++--- src/CNZSL/Parser.cpp | 43 +++++++++++++-------- src/CNZSL/SpirvWriter.cpp | 58 ++++++++++++++++++----------- 13 files changed, 176 insertions(+), 124 deletions(-) diff --git a/include/CNZSL/Error.h b/include/CNZSL/Error.h index 841ed11..6087543 100644 --- a/include/CNZSL/Error.h +++ b/include/CNZSL/Error.h @@ -11,6 +11,7 @@ #ifdef __cplusplus #include + extern "C" { #else #include @@ -20,7 +21,7 @@ extern "C" { * Errors are local to the thread * @param module */ -const char * NZSL_API nzslGetError(void); +const char* NZSL_API nzslGetError(void); #ifdef __cplusplus } diff --git a/include/CNZSL/Error.hpp b/include/CNZSL/Error.hpp index b591727..1923e72 100644 --- a/include/CNZSL/Error.hpp +++ b/include/CNZSL/Error.hpp @@ -10,7 +10,12 @@ #include #include -namespace cnzsl { +namespace cnzsl +{ + /** Set the error string in the curent thread + * + * @param error + */ void NZSL_API setError(std::string error); } diff --git a/include/CNZSL/GlslWriter.h b/include/CNZSL/GlslWriter.h index 5e83f6e..d840bd8 100644 --- a/include/CNZSL/GlslWriter.h +++ b/include/CNZSL/GlslWriter.h @@ -16,7 +16,7 @@ extern "C" { /// Opaque pointer on nzsl::GlslWriter -typedef struct NZSLGlslWriter_s *NZSLGlslWriter; +typedef struct NZSLGlslWriter_s* NZSLGlslWriter; typedef struct { @@ -29,7 +29,8 @@ typedef struct int allowDrawParametersUniformsFallback; } NZSLGlslWriterEnvironment; -typedef struct NZSLGlslWriterOutputInternal_s *NZSLGlslWriterOutputInternal; +typedef struct NZSLGlslWriterOutputInternal_s* NZSLGlslWriterOutputInternal; + typedef struct { NZSLGlslWriterOutputInternal internal; @@ -39,7 +40,8 @@ typedef struct int usesDrawParameterBaseVertexUniform; int usesDrawParameterDrawIndexUniform; } NZSLGlslWriterOutput_s; -typedef NZSLGlslWriterOutput_s *NZSLGlslWriterOutput; + +typedef NZSLGlslWriterOutput_s* NZSLGlslWriterOutput; NZSLGlslWriter NZSL_API nzslGlslWriterCreate(void); diff --git a/include/CNZSL/LangWriter.h b/include/CNZSL/LangWriter.h index dcf4f15..d2b76b3 100644 --- a/include/CNZSL/LangWriter.h +++ b/include/CNZSL/LangWriter.h @@ -17,16 +17,18 @@ extern "C" { /// Opaque pointer on nzsl::LangWriter -typedef struct NZSLLangWriter_s *NZSLLangWriter; +typedef struct NZSLLangWriter_s* NZSLLangWriter; + +typedef struct NZSLLangWriterOutputInternal_s* NZSLLangWriterOutputInternal; -typedef struct NZSLLangWriterOutputInternal_s *NZSLLangWriterOutputInternal; typedef struct { NZSLLangWriterOutputInternal internal; const char* code; size_t codeLen; } NZSLLangWriterOutput_s; -typedef NZSLLangWriterOutput_s *NZSLLangWriterOutput; + +typedef NZSLLangWriterOutput_s* NZSLLangWriterOutput; NZSLLangWriter NZSL_API nzslLangWriterCreate(void); diff --git a/include/CNZSL/Module.h b/include/CNZSL/Module.h index d9296db..5cd1fec 100644 --- a/include/CNZSL/Module.h +++ b/include/CNZSL/Module.h @@ -15,7 +15,7 @@ extern "C" { /// Opaque pointer on nzsl::Ast::ModulePtr -typedef struct NZSLModule_s *NZSLModule; +typedef struct NZSLModule_s* NZSLModule; /** Free a NZSLModule that was returned by one of the parsers functions * diff --git a/include/CNZSL/Parser.h b/include/CNZSL/Parser.h index 1955b25..5c89caf 100644 --- a/include/CNZSL/Parser.h +++ b/include/CNZSL/Parser.h @@ -15,38 +15,21 @@ extern "C" { #endif -/** - * - * @param source - * @param sourceLen - * @param f - * @return - */ NZSLModule NZSL_API nzslParserParseSource(const char* source, size_t sourceLen); /** * * @param source * @param sourceLen - * @param filePath + * @param filePath used when reporting errors * @param filePathLen * @return */ -NZSLModule NZSL_API nzslParserParseSourceWithFilePath(const char* source, size_t sourceLen, const char* filePath, size_t filePathLen); +NZSLModule NZSL_API nzslParserParseSourceWithFilePath(const char* source, size_t sourceLen, const char* filePath, + size_t filePathLen); -/** - * - * @param sourcePath - * @param sourcePathLen - * @return - */ NZSLModule NZSL_API nzslParserParseFromFile(const char* sourcePath, size_t sourcePathLen); -/** Free a NZSLModule that was returned by one of the parsers functions - * - * @param module - */ -void NZSL_API nzslModuleDestroy(NZSLModule module); #ifdef __cplusplus } diff --git a/include/CNZSL/SpirvWriter.h b/include/CNZSL/SpirvWriter.h index 7a69777..249a3b8 100644 --- a/include/CNZSL/SpirvWriter.h +++ b/include/CNZSL/SpirvWriter.h @@ -12,6 +12,7 @@ #ifdef __cplusplus #include + extern "C" { #else #includ @@ -19,7 +20,7 @@ extern "C" { /// Opaque pointer on nzsl::SpirvWriter -typedef struct NZSLSpirvWriter_s *NZSLSpirvWriter; +typedef struct NZSLSpirvWriter_s* NZSLSpirvWriter; typedef struct { @@ -27,14 +28,16 @@ typedef struct uint32_t spvMinorVersion; } NZSLSpirvWriterEnvironment; -typedef struct NZSLSpirvWriterOutputInternal_s *NZSLSpirvWriterOutputInternal; +typedef struct NZSLSpirvWriterOutputInternal_s* NZSLSpirvWriterOutputInternal; + typedef struct { NZSLSpirvWriterOutputInternal internal; const uint32_t* spirv; size_t spirvLen; } NZSLSpirvWriterOutput_s; -typedef NZSLSpirvWriterOutput_s *NZSLSpirvWriterOutput; + +typedef NZSLSpirvWriterOutput_s* NZSLSpirvWriterOutput; NZSLSpirvWriter NZSL_API nzslSpirvWriterCreate(void); diff --git a/src/CNZSL/Error.cpp b/src/CNZSL/Error.cpp index 11108fe..a04cc31 100644 --- a/src/CNZSL/Error.cpp +++ b/src/CNZSL/Error.cpp @@ -8,16 +8,17 @@ thread_local std::string lastError; -namespace cnzsl { - void NZSL_API setError(std::string error) { +namespace cnzsl +{ + void NZSL_API setError(std::string error) + { lastError = std::move(error); } } extern "C" { - -const char * NZSL_API nzslGetError() { +const char* NZSL_API nzslGetError() +{ return lastError.c_str(); } - -} \ No newline at end of file +} diff --git a/src/CNZSL/GlslWriter.cpp b/src/CNZSL/GlslWriter.cpp index 7e3c03e..d298d78 100644 --- a/src/CNZSL/GlslWriter.cpp +++ b/src/CNZSL/GlslWriter.cpp @@ -11,26 +11,31 @@ using namespace std::literals; extern "C" { - - -NZSLGlslWriter NZSL_API nzslGlslWriterCreate(void) { +NZSLGlslWriter NZSL_API nzslGlslWriterCreate(void) +{ nzsl::GlslWriter* writer = nullptr; - try { + try + { writer = new nzsl::GlslWriter; - } catch (std::exception& e) { + } + catch (std::exception& e) + { cnzsl::setError("nzslGlslWriterCreate failed: "s + e.what()); - } catch (...) { + } catch (...) + { cnzsl::setError("nzslGlslWriterCreate failed with unknown error"); } return reinterpret_cast(writer); } -int NZSL_API nzslGlslWriterSetEnv(NZSLGlslWriter writer, NZSLGlslWriterEnvironment env) { +int NZSL_API nzslGlslWriterSetEnv(NZSLGlslWriter writer, NZSLGlslWriterEnvironment env) +{ auto writerPtr = reinterpret_cast(writer); - try { + try + { writerPtr->SetEnv({ .extCallback = {}, .glMajorVersion = env.glMajorVersion, @@ -39,12 +44,15 @@ int NZSL_API nzslGlslWriterSetEnv(NZSLGlslWriter writer, NZSLGlslWriterEnvironme .flipYPosition = env.flipYPosition >= 1, .remapZPosition = env.remapZPosition >= 1, .allowDrawParametersUniformsFallback = env.allowDrawParametersUniformsFallback >= 1 - }); - } catch (std::exception& e) { + }); + } + catch (std::exception& e) + { cnzsl::setError("nzslGlslWriterSetEnv failed: "s + e.what()); return 0; - } catch (...) { + } catch (...) + { cnzsl::setError("nzslGlslWriterSetEnv failed with unknown error"); return 0; @@ -53,13 +61,15 @@ int NZSL_API nzslGlslWriterSetEnv(NZSLGlslWriter writer, NZSLGlslWriterEnvironme return 1; } -NZSLGlslWriterOutput NZSL_API nzslGlslWriterGenerate(NZSLGlslWriter writer, NZSLModule module) { +NZSLGlslWriterOutput NZSL_API nzslGlslWriterGenerate(NZSLGlslWriter writer, NZSLModule module) +{ auto writerPtr = reinterpret_cast(writer); - auto modulePtr = reinterpret_cast(module); + auto modulePtr = reinterpret_cast(module); NZSLGlslWriterOutput output = nullptr; - try { + try + { auto generated = new nzsl::GlslWriter::Output(writerPtr->Generate(**modulePtr)); try @@ -72,57 +82,67 @@ NZSLGlslWriterOutput NZSL_API nzslGlslWriterGenerate(NZSLGlslWriter writer, NZSL .usesDrawParameterBaseVertexUniform = generated->usesDrawParameterBaseVertexUniform ? 1 : 0, .usesDrawParameterDrawIndexUniform = generated->usesDrawParameterDrawIndexUniform ? 1 : 0 }; - } catch(...) { + } + catch (...) + { delete generated; throw; } - } catch (std::exception& e) { + } + catch (std::exception& e) + { cnzsl::setError("nzslGlslWriterGenerate failed: "s + e.what()); - } catch (...) { + } catch (...) + { cnzsl::setError("nzslGlslWriterGenerate failed with unknown error"); } return output; } -int NZSL_API nzslGlslWriterOutputGetExplicitTextureBinding(NZSLGlslWriterOutput output, const char* bindingName) { +int NZSL_API nzslGlslWriterOutputGetExplicitTextureBinding(NZSLGlslWriterOutput output, const char* bindingName) +{ auto outputPtr = reinterpret_cast(output->internal); - if( + if ( auto it = outputPtr->explicitTextureBinding.find(bindingName); it != outputPtr->explicitTextureBinding.end() - ) { + ) + { return static_cast(it->second); } return -1; } -int NZSL_API nzslGlslWriterOutputGetExplicitUniformBlockBinding(NZSLGlslWriterOutput output, const char* bindingName) { +int NZSL_API nzslGlslWriterOutputGetExplicitUniformBlockBinding(NZSLGlslWriterOutput output, const char* bindingName) +{ auto outputPtr = reinterpret_cast(output->internal); - if( + if ( auto it = outputPtr->explicitUniformBlockBinding.find(bindingName); it != outputPtr->explicitUniformBlockBinding.end() - ) { + ) + { return static_cast(it->second); } return -1; } -void NZSL_API nzslGlslWriterOutputDestroy(NZSLGlslWriterOutput output) { +void NZSL_API nzslGlslWriterOutputDestroy(NZSLGlslWriterOutput output) +{ auto outputPtr = reinterpret_cast(output->internal); delete outputPtr; delete output; } -void NZSL_API nzslGlslWriterDestroy(NZSLGlslWriter writer) { +void NZSL_API nzslGlslWriterDestroy(NZSLGlslWriter writer) +{ auto writerPtr = reinterpret_cast(writer); delete writerPtr; } - -} \ No newline at end of file +} diff --git a/src/CNZSL/LangWriter.cpp b/src/CNZSL/LangWriter.cpp index 9ce5c05..48485b4 100644 --- a/src/CNZSL/LangWriter.cpp +++ b/src/CNZSL/LangWriter.cpp @@ -11,29 +11,34 @@ using namespace std::literals; extern "C" { - - -NZSLLangWriter NZSL_API nzslLangWriterCreate(void) { +NZSLLangWriter NZSL_API nzslLangWriterCreate(void) +{ nzsl::LangWriter* writer = nullptr; - try { + try + { writer = new nzsl::LangWriter; - } catch (std::exception& e) { + } + catch (std::exception& e) + { cnzsl::setError("nzslLangWriterCreate failed: "s + e.what()); - } catch (...) { + } catch (...) + { cnzsl::setError("nzslLangWriterCreate failed with unknown error"); } return reinterpret_cast(writer); } -NZSLLangWriterOutput NZSL_API nzslLangWriterGenerate(NZSLLangWriter writer, NZSLModule module) { +NZSLLangWriterOutput NZSL_API nzslLangWriterGenerate(NZSLLangWriter writer, NZSLModule module) +{ auto writerPtr = reinterpret_cast(writer); - auto modulePtr = reinterpret_cast(module); + auto modulePtr = reinterpret_cast(module); NZSLLangWriterOutput output = nullptr; - try { + try + { auto generated = new std::string{writerPtr->Generate(**modulePtr)}; try @@ -43,31 +48,37 @@ NZSLLangWriterOutput NZSL_API nzslLangWriterGenerate(NZSLLangWriter writer, NZSL .code = generated->data(), .codeLen = generated->size() }; - } catch(...) { + } + catch (...) + { delete generated; throw; } - } catch (std::exception& e) { + } + catch (std::exception& e) + { cnzsl::setError("nzslLangWriterGenerate failed: "s + e.what()); - } catch (...) { + } catch (...) + { cnzsl::setError("nzslLangWriterGenerate failed with unknown error"); } return output; } -void NZSL_API nzslLangWriterOutputDestroy(NZSLLangWriterOutput output) { +void NZSL_API nzslLangWriterOutputDestroy(NZSLLangWriterOutput output) +{ auto outputPtr = reinterpret_cast(output->internal); delete outputPtr; delete output; } -void NZSL_API nzslLangWriterDestroy(NZSLLangWriter writer) { +void NZSL_API nzslLangWriterDestroy(NZSLLangWriter writer) +{ auto writerPtr = reinterpret_cast(writer); delete writerPtr; } - -} \ No newline at end of file +} diff --git a/src/CNZSL/Module.cpp b/src/CNZSL/Module.cpp index 60fe8ff..dfccdbf 100644 --- a/src/CNZSL/Module.cpp +++ b/src/CNZSL/Module.cpp @@ -6,13 +6,10 @@ #include extern "C" { - - -void NZSL_API nzslModuleDestroy(NZSLModule module) { +void NZSL_API nzslModuleDestroy(NZSLModule module) +{ auto modulePtr = reinterpret_cast(module); delete modulePtr; } - - -} \ No newline at end of file +} diff --git a/src/CNZSL/Parser.cpp b/src/CNZSL/Parser.cpp index 82c511c..8495510 100644 --- a/src/CNZSL/Parser.cpp +++ b/src/CNZSL/Parser.cpp @@ -11,43 +11,54 @@ using namespace std::literals; extern "C" { - - -NZSLModule NZSL_API nzslParserParseSource(const char* source, size_t sourceLen) { +NZSLModule NZSL_API nzslParserParseSource(const char* source, size_t sourceLen) +{ return nzslParserParseSourceWithFilePath(source, sourceLen, nullptr, 0); } -NZSLModule NZSL_API nzslParserParseSourceWithFilePath(const char* source, size_t sourceLen, const char* filePath, size_t filePathLen) { +NZSLModule NZSL_API nzslParserParseSourceWithFilePath(const char* source, size_t sourceLen, const char* filePath, + size_t filePathLen) +{ nzsl::Ast::ModulePtr* module = nullptr; - try { - if (filePath) { + try + { + if (filePath) + { module = new nzsl::Ast::ModulePtr(nzsl::Parse({source, sourceLen}, std::string(filePath, filePathLen))); - } else { + } + else + { module = new nzsl::Ast::ModulePtr(nzsl::Parse({source, sourceLen})); } - } catch (std::exception& e) { + } + catch (std::exception& e) + { cnzsl::setError("nzslParserParseSourceWithFilePath failed: "s + e.what()); - } catch (...) { + } catch (...) + { cnzsl::setError("nzslParserParseSourceWithFilePath failed with unknown error"); } return reinterpret_cast(module); } -NZSLModule nzslParserParseFromFile(const char* sourcePath, size_t sourcePathLen) { +NZSLModule nzslParserParseFromFile(const char* sourcePath, size_t sourcePathLen) +{ nzsl::Ast::ModulePtr* module = nullptr; - try { + try + { module = new nzsl::Ast::ModulePtr(nzsl::ParseFromFile({sourcePath, sourcePath + sourcePathLen})); - } catch (std::exception& e) { + } + catch (std::exception& e) + { cnzsl::setError("nzslParserParseFromFile failed: "s + e.what()); - } catch (...) { + } catch (...) + { cnzsl::setError("nzslParserParseFromFile failed with unknown error"); } return reinterpret_cast(module); } - - -} \ No newline at end of file +} diff --git a/src/CNZSL/SpirvWriter.cpp b/src/CNZSL/SpirvWriter.cpp index c3d27ab..7b3d4ec 100644 --- a/src/CNZSL/SpirvWriter.cpp +++ b/src/CNZSL/SpirvWriter.cpp @@ -11,35 +11,43 @@ using namespace std::literals; extern "C" { - - -NZSLSpirvWriter NZSL_API nzslSpirvWriterCreate(void) { +NZSLSpirvWriter NZSL_API nzslSpirvWriterCreate(void) +{ nzsl::SpirvWriter* writer = nullptr; - try { + try + { writer = new nzsl::SpirvWriter; - } catch (std::exception& e) { + } + catch (std::exception& e) + { cnzsl::setError("nzslSpirvWriterCreate failed: "s + e.what()); - } catch (...) { + } catch (...) + { cnzsl::setError("nzslSpirvWriterCreate failed with unknown error"); } return reinterpret_cast(writer); } -int NZSL_API nzslSpirvWriterSetEnv(NZSLSpirvWriter writer, NZSLSpirvWriterEnvironment env) { +int NZSL_API nzslSpirvWriterSetEnv(NZSLSpirvWriter writer, NZSLSpirvWriterEnvironment env) +{ auto writerPtr = reinterpret_cast(writer); - try { + try + { writerPtr->SetEnv({ .spvMajorVersion = env.spvMajorVersion, .spvMinorVersion = env.spvMinorVersion - }); - } catch (std::exception& e) { + }); + } + catch (std::exception& e) + { cnzsl::setError("nzslSpirvWriterSetEnv failed: "s + e.what()); return 0; - } catch (...) { + } catch (...) + { cnzsl::setError("nzslSpirvWriterSetEnv failed with unknown error"); return 0; @@ -48,13 +56,15 @@ int NZSL_API nzslSpirvWriterSetEnv(NZSLSpirvWriter writer, NZSLSpirvWriterEnviro return 1; } -NZSLSpirvWriterOutput NZSL_API nzslSpirvWriterGenerate(NZSLSpirvWriter writer, NZSLModule module) { +NZSLSpirvWriterOutput NZSL_API nzslSpirvWriterGenerate(NZSLSpirvWriter writer, NZSLModule module) +{ auto writerPtr = reinterpret_cast(writer); - auto modulePtr = reinterpret_cast(module); + auto modulePtr = reinterpret_cast(module); NZSLSpirvWriterOutput output = nullptr; - try { + try + { auto generated = new std::vector{writerPtr->Generate(**modulePtr)}; try @@ -64,31 +74,37 @@ NZSLSpirvWriterOutput NZSL_API nzslSpirvWriterGenerate(NZSLSpirvWriter writer, N .spirv = generated->data(), .spirvLen = generated->size() }; - } catch(...) { + } + catch (...) + { delete generated; throw; } - } catch (std::exception& e) { + } + catch (std::exception& e) + { cnzsl::setError("nzslSpirvWriterGenerate failed: "s + e.what()); - } catch (...) { + } catch (...) + { cnzsl::setError("nzslSpirvWriterGenerate failed with unknown error"); } return output; } -void NZSL_API nzslSpirvWriterOutputDestroy(NZSLSpirvWriterOutput output) { +void NZSL_API nzslSpirvWriterOutputDestroy(NZSLSpirvWriterOutput output) +{ auto outputPtr = reinterpret_cast*>(output->internal); delete outputPtr; delete output; } -void NZSL_API nzslSpirvWriterDestroy(NZSLSpirvWriter writer) { +void NZSL_API nzslSpirvWriterDestroy(NZSLSpirvWriter writer) +{ auto writerPtr = reinterpret_cast(writer); delete writerPtr; } - -} \ No newline at end of file +}