From 427ae4d6ab5f145c840aefc0904d6fbd1688b4ce Mon Sep 17 00:00:00 2001 From: REMqb Date: Sat, 27 Apr 2024 14:52:33 +0200 Subject: [PATCH] + Binding SpirvWriter + Binding LangWriter --- .gitignore | 3 ++ include/CNZSL/CNZSL.h | 9 +++- include/CNZSL/Config.h | 8 +++- include/CNZSL/Error.h | 2 + include/CNZSL/Error.hpp | 6 ++- include/CNZSL/GlslWriter.h | 8 ++-- include/CNZSL/LangWriter.h | 42 +++++++++++++++++ include/CNZSL/Module.h | 2 + include/CNZSL/Parser.h | 2 + include/CNZSL/SpirvWriter.h | 52 ++++++++++++++++++++ src/CNZSL/Error.cpp | 4 ++ src/CNZSL/GlslWriter.cpp | 35 +++++++++----- src/CNZSL/LangWriter.cpp | 73 ++++++++++++++++++++++++++++ src/CNZSL/Module.cpp | 4 ++ src/CNZSL/Parser.cpp | 4 ++ src/CNZSL/SpirvWriter.cpp | 94 +++++++++++++++++++++++++++++++++++++ xmake.lua | 2 +- 17 files changed, 326 insertions(+), 24 deletions(-) create mode 100644 include/CNZSL/LangWriter.h create mode 100644 include/CNZSL/SpirvWriter.h create mode 100644 src/CNZSL/LangWriter.cpp create mode 100644 src/CNZSL/SpirvWriter.cpp diff --git a/.gitignore b/.gitignore index c623595..c6f2dcc 100644 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,9 @@ build vs* vsxmake* *.user +.idea/ +cmake*/ +CMakeLists.txt # Prerequisites *.d diff --git a/include/CNZSL/CNZSL.h b/include/CNZSL/CNZSL.h index 2447ee7..a5017f5 100644 --- a/include/CNZSL/CNZSL.h +++ b/include/CNZSL/CNZSL.h @@ -2,10 +2,15 @@ // This file is part of the "Nazara Shading Language" project // For conditions of distribution and use, see copyright notice in Config.hpp -#include -#include +#pragma once #ifndef CNZSL_CNZSL_H #define CNZSL_CNZSL_H +#include +#include +#include +#include +#include + #endif //CNZSL_CNZSL_H diff --git a/include/CNZSL/Config.h b/include/CNZSL/Config.h index e950a49..2ac2724 100644 --- a/include/CNZSL/Config.h +++ b/include/CNZSL/Config.h @@ -1,3 +1,9 @@ +// Copyright (C) 2024 REMqb (remqb at remqb dot fr) +// This file is part of the "Nazara Shading Language" project +// For conditions of distribution and use, see copyright notice in Config.hpp + +#pragma once + #ifndef CNZSL_CNZSL_H #define CNZSL_CNZSL_H @@ -98,6 +104,4 @@ #define NZSL_API #endif -#include - #endif //CNZSL_CNZSL_H diff --git a/include/CNZSL/Error.h b/include/CNZSL/Error.h index 0ef1d18..841ed11 100644 --- a/include/CNZSL/Error.h +++ b/include/CNZSL/Error.h @@ -2,6 +2,8 @@ // This file is part of the "Nazara Shading Language" project // For conditions of distribution and use, see copyright notice in Config.hpp +#pragma once + #ifndef CNZSL_ERROR_H #define CNZSL_ERROR_H diff --git a/include/CNZSL/Error.hpp b/include/CNZSL/Error.hpp index b3046bb..b591727 100644 --- a/include/CNZSL/Error.hpp +++ b/include/CNZSL/Error.hpp @@ -2,12 +2,14 @@ // This file is part of the "Nazara Shading Language" project // For conditions of distribution and use, see copyright notice in Config.hpp -#include -#include +#pragma once #ifndef CNZSL_ERROR_HPP #define CNZSL_ERROR_HPP +#include +#include + namespace cnzsl { void NZSL_API setError(std::string error); } diff --git a/include/CNZSL/GlslWriter.h b/include/CNZSL/GlslWriter.h index f25e092..5e83f6e 100644 --- a/include/CNZSL/GlslWriter.h +++ b/include/CNZSL/GlslWriter.h @@ -2,6 +2,8 @@ // This file is part of the "Nazara Shading Language" project // For conditions of distribution and use, see copyright notice in Config.hpp +#pragma once + #ifndef CNZSL_GLSLWRITER_H #define CNZSL_GLSLWRITER_H @@ -25,7 +27,7 @@ typedef struct int flipYPosition; int remapZPosition; int allowDrawParametersUniformsFallback; -} NZSLEnvironment; +} NZSLGlslWriterEnvironment; typedef struct NZSLGlslWriterOutputInternal_s *NZSLGlslWriterOutputInternal; typedef struct @@ -33,8 +35,6 @@ typedef struct NZSLGlslWriterOutputInternal internal; const char* code; size_t codeLen; - //std::unordered_map explicitTextureBinding; - //std::unordered_map explicitUniformBlockBinding; int usesDrawParameterBaseInstanceUniform; int usesDrawParameterBaseVertexUniform; int usesDrawParameterDrawIndexUniform; @@ -43,7 +43,7 @@ typedef NZSLGlslWriterOutput_s *NZSLGlslWriterOutput; NZSLGlslWriter NZSL_API nzslGlslWriterCreate(void); -int NZSL_API nzslGlslWriterSetEnv(NZSLGlslWriter writer, NZSLEnvironment env); +int NZSL_API nzslGlslWriterSetEnv(NZSLGlslWriter writer, NZSLGlslWriterEnvironment env); NZSLGlslWriterOutput NZSL_API nzslGlslWriterGenerate(NZSLGlslWriter writer, NZSLModule module); diff --git a/include/CNZSL/LangWriter.h b/include/CNZSL/LangWriter.h new file mode 100644 index 0000000..dcf4f15 --- /dev/null +++ b/include/CNZSL/LangWriter.h @@ -0,0 +1,42 @@ +// Copyright (C) 2024 REMqb (remqb at remqb dot fr) +// This file is part of the "Nazara Shading Language" project +// For conditions of distribution and use, see copyright notice in Config.hpp + +#pragma once + +#ifndef CNZSL_LANGWRITER_H +#define CNZSL_LANGWRITER_H + +#include +#include + +#ifdef __cplusplus +extern "C" { +#else +#endif + + +/// Opaque pointer on nzsl::LangWriter +typedef struct NZSLLangWriter_s *NZSLLangWriter; + +typedef struct NZSLLangWriterOutputInternal_s *NZSLLangWriterOutputInternal; +typedef struct +{ + NZSLLangWriterOutputInternal internal; + const char* code; + size_t codeLen; +} NZSLLangWriterOutput_s; +typedef NZSLLangWriterOutput_s *NZSLLangWriterOutput; + +NZSLLangWriter NZSL_API nzslLangWriterCreate(void); + +NZSLLangWriterOutput NZSL_API nzslLangWriterGenerate(NZSLLangWriter writer, NZSLModule module); + +void NZSL_API nzslLangWriterDestroy(NZSLLangWriter writer); + +#ifdef __cplusplus +} +#endif + + +#endif //CNZSL_LANGWRITER_H diff --git a/include/CNZSL/Module.h b/include/CNZSL/Module.h index 91e822b..d9296db 100644 --- a/include/CNZSL/Module.h +++ b/include/CNZSL/Module.h @@ -2,6 +2,8 @@ // This file is part of the "Nazara Shading Language" project // For conditions of distribution and use, see copyright notice in Config.hpp +#pragma once + #ifndef CNZSL_MODULE_H #define CNZSL_MODULE_H diff --git a/include/CNZSL/Parser.h b/include/CNZSL/Parser.h index 337ad88..1955b25 100644 --- a/include/CNZSL/Parser.h +++ b/include/CNZSL/Parser.h @@ -2,6 +2,8 @@ // This file is part of the "Nazara Shading Language" project // For conditions of distribution and use, see copyright notice in Config.hpp +#pragma once + #ifndef CNZSL_PARSER_H #define CNZSL_PARSER_H diff --git a/include/CNZSL/SpirvWriter.h b/include/CNZSL/SpirvWriter.h new file mode 100644 index 0000000..7a69777 --- /dev/null +++ b/include/CNZSL/SpirvWriter.h @@ -0,0 +1,52 @@ +// Copyright (C) 2024 REMqb (remqb at remqb dot fr) +// This file is part of the "Nazara Shading Language" project +// For conditions of distribution and use, see copyright notice in Config.hpp + +#pragma once + +#ifndef CNZSL_SPIRVWRITER_H +#define CNZSL_SPIRVWRITER_H + +#include +#include + +#ifdef __cplusplus +#include +extern "C" { +#else +#includ +#endif + + +/// Opaque pointer on nzsl::SpirvWriter +typedef struct NZSLSpirvWriter_s *NZSLSpirvWriter; + +typedef struct +{ + uint32_t spvMajorVersion; + uint32_t spvMinorVersion; +} NZSLSpirvWriterEnvironment; + +typedef struct NZSLSpirvWriterOutputInternal_s *NZSLSpirvWriterOutputInternal; +typedef struct +{ + NZSLSpirvWriterOutputInternal internal; + const uint32_t* spirv; + size_t spirvLen; +} NZSLSpirvWriterOutput_s; +typedef NZSLSpirvWriterOutput_s *NZSLSpirvWriterOutput; + +NZSLSpirvWriter NZSL_API nzslSpirvWriterCreate(void); + +int NZSL_API nzslSpirvWriterSetEnv(NZSLSpirvWriter writer, NZSLSpirvWriterEnvironment env); + +NZSLSpirvWriterOutput NZSL_API nzslSpirvWriterGenerate(NZSLSpirvWriter writer, NZSLModule module); + +void NZSL_API nzslSpirvWriterDestroy(NZSLSpirvWriter writer); + +#ifdef __cplusplus +} +#endif + + +#endif //CNZSL_SPIRVWRITER_H diff --git a/src/CNZSL/Error.cpp b/src/CNZSL/Error.cpp index 52ce331..11108fe 100644 --- a/src/CNZSL/Error.cpp +++ b/src/CNZSL/Error.cpp @@ -1,3 +1,7 @@ +// Copyright (C) 2024 REMqb (remqb at remqb dot fr) +// This file is part of the "Nazara Shading Language" project +// For conditions of distribution and use, see copyright notice in Config.hpp + #include #include #include diff --git a/src/CNZSL/GlslWriter.cpp b/src/CNZSL/GlslWriter.cpp index 630e005..7e3c03e 100644 --- a/src/CNZSL/GlslWriter.cpp +++ b/src/CNZSL/GlslWriter.cpp @@ -1,3 +1,7 @@ +// Copyright (C) 2024 REMqb (remqb at remqb dot fr) +// This file is part of the "Nazara Shading Language" project +// For conditions of distribution and use, see copyright notice in Config.hpp + #include #include @@ -23,7 +27,7 @@ NZSLGlslWriter NZSL_API nzslGlslWriterCreate(void) { return reinterpret_cast(writer); } -int NZSL_API nzslGlslWriterSetEnv(NZSLGlslWriter writer, NZSLEnvironment env) { +int NZSL_API nzslGlslWriterSetEnv(NZSLGlslWriter writer, NZSLGlslWriterEnvironment env) { auto writerPtr = reinterpret_cast(writer); try { @@ -56,18 +60,23 @@ NZSLGlslWriterOutput NZSL_API nzslGlslWriterGenerate(NZSLGlslWriter writer, NZSL NZSLGlslWriterOutput output = nullptr; try { - auto generated = std::make_unique(writerPtr->Generate(**modulePtr)); - - output = new NZSLGlslWriterOutput_s{ - .internal = nullptr, - .code = generated->code.c_str(), - .codeLen = generated->code.size(), - .usesDrawParameterBaseInstanceUniform = generated->usesDrawParameterBaseInstanceUniform ? 1 : 0, - .usesDrawParameterBaseVertexUniform = generated->usesDrawParameterBaseVertexUniform ? 1 : 0, - .usesDrawParameterDrawIndexUniform = generated->usesDrawParameterDrawIndexUniform ? 1 : 0 - }; - - output->internal = reinterpret_cast(generated.release()); + auto generated = new nzsl::GlslWriter::Output(writerPtr->Generate(**modulePtr)); + + try + { + output = new NZSLGlslWriterOutput_s{ + .internal = reinterpret_cast(generated), + .code = generated->code.c_str(), + .codeLen = generated->code.size(), + .usesDrawParameterBaseInstanceUniform = generated->usesDrawParameterBaseInstanceUniform ? 1 : 0, + .usesDrawParameterBaseVertexUniform = generated->usesDrawParameterBaseVertexUniform ? 1 : 0, + .usesDrawParameterDrawIndexUniform = generated->usesDrawParameterDrawIndexUniform ? 1 : 0 + }; + } catch(...) { + delete generated; + + throw; + } } catch (std::exception& e) { cnzsl::setError("nzslGlslWriterGenerate failed: "s + e.what()); } catch (...) { diff --git a/src/CNZSL/LangWriter.cpp b/src/CNZSL/LangWriter.cpp new file mode 100644 index 0000000..9ce5c05 --- /dev/null +++ b/src/CNZSL/LangWriter.cpp @@ -0,0 +1,73 @@ +// Copyright (C) 2024 REMqb (remqb at remqb dot fr) +// This file is part of the "Nazara Shading Language" project +// For conditions of distribution and use, see copyright notice in Config.hpp + +#include +#include + +#include +#include + +using namespace std::literals; + +extern "C" { + + +NZSLLangWriter NZSL_API nzslLangWriterCreate(void) { + nzsl::LangWriter* writer = nullptr; + + try { + writer = new nzsl::LangWriter; + } catch (std::exception& e) { + cnzsl::setError("nzslLangWriterCreate failed: "s + e.what()); + } catch (...) { + cnzsl::setError("nzslLangWriterCreate failed with unknown error"); + } + + return reinterpret_cast(writer); +} + +NZSLLangWriterOutput NZSL_API nzslLangWriterGenerate(NZSLLangWriter writer, NZSLModule module) { + auto writerPtr = reinterpret_cast(writer); + auto modulePtr = reinterpret_cast(module); + + NZSLLangWriterOutput output = nullptr; + + try { + auto generated = new std::string{writerPtr->Generate(**modulePtr)}; + + try + { + output = new NZSLLangWriterOutput_s{ + .internal = reinterpret_cast(generated), + .code = generated->data(), + .codeLen = generated->size() + }; + } catch(...) { + delete generated; + + throw; + } + } catch (std::exception& e) { + cnzsl::setError("nzslLangWriterGenerate failed: "s + e.what()); + } catch (...) { + cnzsl::setError("nzslLangWriterGenerate failed with unknown error"); + } + + return output; +} + +void NZSL_API nzslLangWriterOutputDestroy(NZSLLangWriterOutput output) { + auto outputPtr = reinterpret_cast(output->internal); + + delete outputPtr; + delete output; +} + +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 27f9d0e..60fe8ff 100644 --- a/src/CNZSL/Module.cpp +++ b/src/CNZSL/Module.cpp @@ -1,3 +1,7 @@ +// Copyright (C) 2024 REMqb (remqb at remqb dot fr) +// This file is part of the "Nazara Shading Language" project +// For conditions of distribution and use, see copyright notice in Config.hpp + #include #include diff --git a/src/CNZSL/Parser.cpp b/src/CNZSL/Parser.cpp index abf51bc..82c511c 100644 --- a/src/CNZSL/Parser.cpp +++ b/src/CNZSL/Parser.cpp @@ -1,3 +1,7 @@ +// Copyright (C) 2024 REMqb (remqb at remqb dot fr) +// This file is part of the "Nazara Shading Language" project +// For conditions of distribution and use, see copyright notice in Config.hpp + #include #include diff --git a/src/CNZSL/SpirvWriter.cpp b/src/CNZSL/SpirvWriter.cpp new file mode 100644 index 0000000..c3d27ab --- /dev/null +++ b/src/CNZSL/SpirvWriter.cpp @@ -0,0 +1,94 @@ +// Copyright (C) 2024 REMqb (remqb at remqb dot fr) +// This file is part of the "Nazara Shading Language" project +// For conditions of distribution and use, see copyright notice in Config.hpp + +#include +#include + +#include +#include + +using namespace std::literals; + +extern "C" { + + +NZSLSpirvWriter NZSL_API nzslSpirvWriterCreate(void) { + nzsl::SpirvWriter* writer = nullptr; + + try { + writer = new nzsl::SpirvWriter; + } catch (std::exception& e) { + cnzsl::setError("nzslSpirvWriterCreate failed: "s + e.what()); + } catch (...) { + cnzsl::setError("nzslSpirvWriterCreate failed with unknown error"); + } + + return reinterpret_cast(writer); +} + +int NZSL_API nzslSpirvWriterSetEnv(NZSLSpirvWriter writer, NZSLSpirvWriterEnvironment env) { + auto writerPtr = reinterpret_cast(writer); + + try { + writerPtr->SetEnv({ + .spvMajorVersion = env.spvMajorVersion, + .spvMinorVersion = env.spvMinorVersion + }); + } catch (std::exception& e) { + cnzsl::setError("nzslSpirvWriterSetEnv failed: "s + e.what()); + + return 0; + } catch (...) { + cnzsl::setError("nzslSpirvWriterSetEnv failed with unknown error"); + + return 0; + } + + return 1; +} + +NZSLSpirvWriterOutput NZSL_API nzslSpirvWriterGenerate(NZSLSpirvWriter writer, NZSLModule module) { + auto writerPtr = reinterpret_cast(writer); + auto modulePtr = reinterpret_cast(module); + + NZSLSpirvWriterOutput output = nullptr; + + try { + auto generated = new std::vector{writerPtr->Generate(**modulePtr)}; + + try + { + output = new NZSLSpirvWriterOutput_s{ + .internal = reinterpret_cast(generated), + .spirv = generated->data(), + .spirvLen = generated->size() + }; + } catch(...) { + delete generated; + + throw; + } + } catch (std::exception& e) { + cnzsl::setError("nzslSpirvWriterGenerate failed: "s + e.what()); + } catch (...) { + cnzsl::setError("nzslSpirvWriterGenerate failed with unknown error"); + } + + return output; +} + +void NZSL_API nzslSpirvWriterOutputDestroy(NZSLSpirvWriterOutput output) { + auto outputPtr = reinterpret_cast*>(output->internal); + + delete outputPtr; + delete output; +} + +void NZSL_API nzslSpirvWriterDestroy(NZSLSpirvWriter writer) { + auto writerPtr = reinterpret_cast(writer); + + delete writerPtr; +} + +} \ No newline at end of file diff --git a/xmake.lua b/xmake.lua index 83c0ce4..a3de23a 100644 --- a/xmake.lua +++ b/xmake.lua @@ -44,7 +44,7 @@ add_rules("plugin.vsxmake.autoupdate") add_includedirs("include", "src") set_exceptions("cxx") -set_languages("c89", "c++17") +set_languages("c89", "c++20") set_rundir("./bin/$(plat)_$(arch)_$(mode)") set_targetdir("./bin/$(plat)_$(arch)_$(mode)")