Skip to content

Commit

Permalink
~ some doc & formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
REMqb committed Apr 27, 2024
1 parent 427ae4d commit 8594134
Show file tree
Hide file tree
Showing 13 changed files with 176 additions and 124 deletions.
3 changes: 2 additions & 1 deletion include/CNZSL/Error.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#ifdef __cplusplus
#include <cstddef>

extern "C" {
#else
#include <stddef.h>
Expand All @@ -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
}
Expand Down
7 changes: 6 additions & 1 deletion include/CNZSL/Error.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@
#include <string>
#include <CNZSL/Config.h>

namespace cnzsl {
namespace cnzsl
{
/** Set the error string in the curent thread
*
* @param error
*/
void NZSL_API setError(std::string error);
}

Expand Down
8 changes: 5 additions & 3 deletions include/CNZSL/GlslWriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ extern "C" {


/// Opaque pointer on nzsl::GlslWriter
typedef struct NZSLGlslWriter_s *NZSLGlslWriter;
typedef struct NZSLGlslWriter_s* NZSLGlslWriter;

typedef struct
{
Expand All @@ -29,7 +29,8 @@ typedef struct
int allowDrawParametersUniformsFallback;
} NZSLGlslWriterEnvironment;

typedef struct NZSLGlslWriterOutputInternal_s *NZSLGlslWriterOutputInternal;
typedef struct NZSLGlslWriterOutputInternal_s* NZSLGlslWriterOutputInternal;

typedef struct
{
NZSLGlslWriterOutputInternal internal;
Expand All @@ -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);

Expand Down
8 changes: 5 additions & 3 deletions include/CNZSL/LangWriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion include/CNZSL/Module.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
23 changes: 3 additions & 20 deletions include/CNZSL/Parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
9 changes: 6 additions & 3 deletions include/CNZSL/SpirvWriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,32 @@

#ifdef __cplusplus
#include <cstddef>

extern "C" {
#else
#includ <stddef.h>
#endif


/// Opaque pointer on nzsl::SpirvWriter
typedef struct NZSLSpirvWriter_s *NZSLSpirvWriter;
typedef struct NZSLSpirvWriter_s* NZSLSpirvWriter;

typedef struct
{
uint32_t spvMajorVersion;
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);

Expand Down
13 changes: 7 additions & 6 deletions src/CNZSL/Error.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

}
}
74 changes: 47 additions & 27 deletions src/CNZSL/GlslWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<NZSLGlslWriter>(writer);
}

int NZSL_API nzslGlslWriterSetEnv(NZSLGlslWriter writer, NZSLGlslWriterEnvironment env) {
int NZSL_API nzslGlslWriterSetEnv(NZSLGlslWriter writer, NZSLGlslWriterEnvironment env)
{
auto writerPtr = reinterpret_cast<nzsl::GlslWriter*>(writer);

try {
try
{
writerPtr->SetEnv({
.extCallback = {},
.glMajorVersion = env.glMajorVersion,
Expand All @@ -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;
Expand All @@ -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<nzsl::GlslWriter*>(writer);
auto modulePtr = reinterpret_cast<nzsl::Ast::ModulePtr *>(module);
auto modulePtr = reinterpret_cast<nzsl::Ast::ModulePtr*>(module);

NZSLGlslWriterOutput output = nullptr;

try {
try
{
auto generated = new nzsl::GlslWriter::Output(writerPtr->Generate(**modulePtr));

try
Expand All @@ -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<nzsl::GlslWriter::Output*>(output->internal);

if(
if (
auto it = outputPtr->explicitTextureBinding.find(bindingName);
it != outputPtr->explicitTextureBinding.end()
) {
)
{
return static_cast<int>(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<nzsl::GlslWriter::Output*>(output->internal);

if(
if (
auto it = outputPtr->explicitUniformBlockBinding.find(bindingName);
it != outputPtr->explicitUniformBlockBinding.end()
) {
)
{
return static_cast<int>(it->second);
}

return -1;
}

void NZSL_API nzslGlslWriterOutputDestroy(NZSLGlslWriterOutput output) {
void NZSL_API nzslGlslWriterOutputDestroy(NZSLGlslWriterOutput output)
{
auto outputPtr = reinterpret_cast<nzsl::GlslWriter::Output*>(output->internal);

delete outputPtr;
delete output;
}

void NZSL_API nzslGlslWriterDestroy(NZSLGlslWriter writer) {
void NZSL_API nzslGlslWriterDestroy(NZSLGlslWriter writer)
{
auto writerPtr = reinterpret_cast<nzsl::GlslWriter*>(writer);

delete writerPtr;
}

}
}
Loading

0 comments on commit 8594134

Please sign in to comment.