Skip to content

Commit

Permalink
+ C bindings
Browse files Browse the repository at this point in the history
+ Binding Parser
+ Binding GlslWriter
+ Binding Module
+ C Error reporting
  • Loading branch information
REMqb committed Apr 27, 2024
1 parent 1236eb7 commit 077908c
Show file tree
Hide file tree
Showing 12 changed files with 503 additions and 0 deletions.
11 changes: 11 additions & 0 deletions include/CNZSL/CNZSL.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// 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 <CNZSL/Error.h>
#include <CNZSL/Parser.h>

#ifndef CNZSL_CNZSL_H
#define CNZSL_CNZSL_H

#endif //CNZSL_CNZSL_H
103 changes: 103 additions & 0 deletions include/CNZSL/Config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
#ifndef CNZSL_CNZSL_H
#define CNZSL_CNZSL_H

// #include <NazaraUtils/Prerequisites.hpp> // I don't want the C++ things

// Try to identify the compiler
#if defined(__clang__)
#define NAZARA_COMPILER_CLANG
#define NAZARA_COMPILER_CLANG_VER (__clang_major__ * 100 + __clang_minor__)
#define NAZARA_DEPRECATED(txt) __attribute__((__deprecated__(txt)))
#define NAZARA_PRETTY_FUNCTION __PRETTY_FUNCTION__

#define NAZARA_CHECK_CLANG_VER(ver) (NAZARA_COMPILER_CLANG_VER >= ver)

#define NAZARA_PRAGMA(x) _Pragma(#x)

#define NAZARA_WARNING_CLANG_DISABLE(warn) NAZARA_PRAGMA(clang diagnostic ignored warn)
#define NAZARA_WARNING_CLANG_GCC_DISABLE(warn) NAZARA_PRAGMA(clang diagnostic ignored warn)
#define NAZARA_WARNING_POP() NAZARA_PRAGMA(clang diagnostic pop)
#define NAZARA_WARNING_PUSH() NAZARA_PRAGMA(clang diagnostic push)

#ifdef __MINGW32__
#define NAZARA_COMPILER_MINGW
#ifdef __MINGW64_VERSION_MAJOR
#define NAZARA_COMPILER_MINGW_W64
#endif
#endif
#elif defined(__GNUC__) || defined(__MINGW32__)
#define NAZARA_COMPILER_GCC
#define NAZARA_COMPILER_GCC_VER (__GNUC__ * 100 + __GNUC_MINOR__)
#define NAZARA_DEPRECATED(txt) __attribute__((__deprecated__(txt)))
#define NAZARA_PRETTY_FUNCTION __PRETTY_FUNCTION__

#define NAZARA_CHECK_GCC_VER(ver) (NAZARA_COMPILER_GCC_VER >= ver)

#define NAZARA_PRAGMA(x) _Pragma(#x)

#define NAZARA_WARNING_CLANG_GCC_DISABLE(warn) NAZARA_PRAGMA(GCC diagnostic ignored warn)
#define NAZARA_WARNING_GCC_DISABLE(warn) NAZARA_PRAGMA(GCC diagnostic ignored warn)
#define NAZARA_WARNING_POP() NAZARA_PRAGMA(GCC diagnostic pop)
#define NAZARA_WARNING_PUSH() NAZARA_PRAGMA(GCC diagnostic push)

#ifdef __MINGW32__
#define NAZARA_COMPILER_MINGW
#ifdef __MINGW64_VERSION_MAJOR
#define NAZARA_COMPILER_MINGW_W64
#endif
#endif
#elif defined(__INTEL_COMPILER) || defined(__ICL)
#define NAZARA_COMPILER_ICC
#define NAZARA_COMPILER_ICC_VER __INTEL_COMPILER
#define NAZARA_DEPRECATED(txt) [[deprecated(txt)]]
#define NAZARA_PRETTY_FUNCTION __FUNCTION__

#define NAZARA_CHECK_ICC_VER(ver) (NAZARA_COMPILER_ICC_VER >= ver)

#define NAZARA_PRAGMA(x) _Pragma(x)

#define NAZARA_WARNING_ICC_DISABLE(...) NAZARA_PRAGMA(warning(disable: __VA_ARGS__))
#define NAZARA_WARNING_POP() NAZARA_PRAGMA(warning(pop))
#define NAZARA_WARNING_PUSH() NAZARA_PRAGMA(warning(push))
#elif defined(_MSC_VER)
#define NAZARA_COMPILER_MSVC
#define NAZARA_COMPILER_MSVC_VER _MSC_VER
#define NAZARA_DEPRECATED(txt) __declspec(deprecated(txt))
#define NAZARA_PRETTY_FUNCTION __FUNCSIG__

#define NAZARA_CHECK_MSVC_VER(ver) (NAZARA_COMPILER_MSVC_VER >= ver)

#define NAZARA_PRAGMA(x) __pragma(x)

#define NAZARA_WARNING_MSVC_DISABLE(...) NAZARA_PRAGMA(warning(disable: __VA_ARGS__))
#define NAZARA_WARNING_POP() NAZARA_PRAGMA(warning(pop))
#define NAZARA_WARNING_PUSH() NAZARA_PRAGMA(warning(push))

// __cplusplus isn't respected on MSVC without /Zc:__cplusplus flag
#define NAZARA_CPP_VER _MSVC_LANG
#else
#define NAZARA_COMPILER_UNKNOWN
#define NAZARA_DEPRECATED(txt)
#define NAZARA_PRETTY_FUNCTION __func__ // __func__ has been standardized in C++ 2011

#pragma message This compiler is not fully supported
#endif

// Nazara version macro
#define NZSL_VERSION_MAJOR 0
#define NZSL_VERSION_MINOR 1
#define NZSL_VERSION_PATCH 0

#if !defined(NZSL_STATIC)
#ifdef NZSL_BUILD
#define NZSL_API NAZARA_EXPORT
#else
#define NZSL_API NAZARA_IMPORT
#endif
#else
#define NZSL_API
#endif

#include <cstdint>

#endif //CNZSL_CNZSL_H
27 changes: 27 additions & 0 deletions include/CNZSL/Error.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// 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

#ifndef CNZSL_ERROR_H
#define CNZSL_ERROR_H

#include <CNZSL/Config.h>

#ifdef __cplusplus
#include <cstddef>
extern "C" {
#else
#include <stddef.h>
#endif

/** Null terminated string of the last error
* Errors are local to the thread
* @param module
*/
const char * NZSL_API nzslGetError(void);

#ifdef __cplusplus
}
#endif

#endif //CNZSL_ERROR_H
15 changes: 15 additions & 0 deletions include/CNZSL/Error.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// 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 <string>
#include <CNZSL/Config.h>

#ifndef CNZSL_ERROR_HPP
#define CNZSL_ERROR_HPP

namespace cnzsl {
void NZSL_API setError(std::string error);
}

#endif //CNZSL_ERROR_HPP
61 changes: 61 additions & 0 deletions include/CNZSL/GlslWriter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// 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

#ifndef CNZSL_GLSLWRITER_H
#define CNZSL_GLSLWRITER_H

#include <CNZSL/Config.h>
#include <CNZSL/Module.h>

#ifdef __cplusplus
extern "C" {
#endif


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

typedef struct
{
// ExtSupportCallback extCallback;
unsigned int glMajorVersion;
unsigned int glMinorVersion;
int glES;
int flipYPosition;
int remapZPosition;
int allowDrawParametersUniformsFallback;
} NZSLEnvironment;

typedef struct NZSLGlslWriterOutputInternal_s *NZSLGlslWriterOutputInternal;
typedef struct
{
NZSLGlslWriterOutputInternal internal;
const char* code;
size_t codeLen;
//std::unordered_map<std::string, unsigned int> explicitTextureBinding;
//std::unordered_map<std::string, unsigned int> explicitUniformBlockBinding;
int usesDrawParameterBaseInstanceUniform;
int usesDrawParameterBaseVertexUniform;
int usesDrawParameterDrawIndexUniform;
} NZSLGlslWriterOutput_s;
typedef NZSLGlslWriterOutput_s *NZSLGlslWriterOutput;

NZSLGlslWriter NZSL_API nzslGlslWriterCreate(void);

int NZSL_API nzslGlslWriterSetEnv(NZSLGlslWriter writer, NZSLEnvironment env);

NZSLGlslWriterOutput NZSL_API nzslGlslWriterGenerate(NZSLGlslWriter writer, NZSLModule module);

int NZSL_API nzslGlslWriterOutputGetExplicitTextureBinding(NZSLGlslWriterOutput output, const char* bindingName);

int NZSL_API nzslGlslWriterOutputGetExplicitUniformBlockBinding(NZSLGlslWriterOutput output, const char* bindingName);

void NZSL_API nzslGlslWriterDestroy(NZSLGlslWriter writer);

#ifdef __cplusplus
}
#endif


#endif //CNZSL_GLSLWRITER_H
29 changes: 29 additions & 0 deletions include/CNZSL/Module.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// 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

#ifndef CNZSL_MODULE_H
#define CNZSL_MODULE_H

#include <CNZSL/Config.h>

#ifdef __cplusplus
extern "C" {
#endif


/// Opaque pointer on nzsl::Ast::ModulePtr
typedef struct NZSLModule_s *NZSLModule;

/** Free a NZSLModule that was returned by one of the parsers functions
*
* @param module
*/
void NZSL_API nzslModuleDestroy(NZSLModule module);

#ifdef __cplusplus
}
#endif


#endif //CNZSL_MODULE_H
54 changes: 54 additions & 0 deletions include/CNZSL/Parser.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// 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

#ifndef CNZSL_PARSER_H
#define CNZSL_PARSER_H

#include <CNZSL/Config.h>
#include <CNZSL/Module.h>

#ifdef __cplusplus
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 filePathLen
* @return
*/
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
}
#endif


#endif //CNZSL_PARSER_H
19 changes: 19 additions & 0 deletions src/CNZSL/Error.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <CNZSL/Error.h>
#include <CNZSL/Error.hpp>
#include <string>

thread_local std::string lastError;

namespace cnzsl {
void NZSL_API setError(std::string error) {
lastError = std::move(error);
}
}

extern "C" {

const char * NZSL_API nzslGetError() {
return lastError.c_str();
}

}
Loading

0 comments on commit 077908c

Please sign in to comment.