-
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding serialization bindings to the CNZSL API (#43)
* adding serialization bindings to the CNZSL API --------- Co-authored-by: Jérôme Leclercq <[email protected]>
- Loading branch information
Showing
3 changed files
with
157 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
Copyright (C) 2024 kbz_8 ( [email protected] ) | ||
This file is part of the "Nazara Shading Language - C Binding" project | ||
For conditions of distribution and use, see copyright notice in Config.hpp | ||
*/ | ||
|
||
#pragma once | ||
|
||
#ifndef CNZSL_SERIALIZER_H | ||
#define CNZSL_SERIALIZER_H | ||
|
||
#include <CNZSL/Config.h> | ||
#include <CNZSL/Module.h> | ||
|
||
#include <stddef.h> | ||
|
||
#ifdef __cplusplus | ||
extern "C" | ||
{ | ||
#endif | ||
|
||
typedef struct nzslSerializer nzslSerializer; | ||
typedef struct nzslDeserializer nzslDeserializer; | ||
|
||
CNZSL_API nzslSerializer* nzslSerializerCreate(void); | ||
CNZSL_API void nzslSerializerDestroy(nzslSerializer* serializerPtr); | ||
CNZSL_API void nzslSerializeShader(nzslSerializer* serializerPtr, const nzslModule* modulePtr); | ||
CNZSL_API const char* nzslSerializerGetLastError(const nzslSerializer* serializerPtr); | ||
|
||
CNZSL_API nzslDeserializer* nzslDeserializerCreate(const void* data, size_t dataSize); | ||
CNZSL_API void nzslDeserializerDestroy(nzslDeserializer* deserializerPtr); | ||
CNZSL_API nzslModule* nzslDeserializeShader(nzslDeserializer* deserializerPtr); | ||
CNZSL_API const char* nzslDeserializerGetLastError(const nzslDeserializer* deserializerPtr); | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
|
||
#endif /* CNZSL_SERIALIZER_H */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
// Copyright (C) 2024 kbz_8 ( [email protected] ) | ||
// This file is part of the "Nazara Shading Language - C Binding" project | ||
// For conditions of distribution and use, see copyright notice in Config.hpp | ||
|
||
#include <CNZSL/Serializer.h> | ||
#include <CNZSL/Module.h> | ||
#include <CNZSL/Structs/Serializer.hpp> | ||
#include <CNZSL/Structs/Module.hpp> | ||
#include <NZSL/Ast/AstSerializer.hpp> | ||
#include <fmt/format.h> | ||
#include <cassert> | ||
|
||
extern "C" | ||
{ | ||
CNZSL_API nzslSerializer* nzslSerializerCreate(void) | ||
{ | ||
return new nzslSerializer; | ||
} | ||
|
||
CNZSL_API void nzslSerializerDestroy(nzslSerializer* serializerPtr) | ||
{ | ||
delete serializerPtr; | ||
} | ||
|
||
CNZSL_API void nzslSerializeShader(nzslSerializer* serializerPtr, const nzslModule* modulePtr) | ||
{ | ||
assert(serializerPtr); | ||
assert(modulePtr); | ||
|
||
try | ||
{ | ||
nzsl::Ast::SerializeShader(serializerPtr->serializer, *modulePtr->module); | ||
} | ||
catch(std::exception& e) | ||
{ | ||
serializerPtr->lastError = fmt::format("nzslSerializeShader failed: {}", e.what()); | ||
} | ||
catch(...) | ||
{ | ||
serializerPtr->lastError = "nzslSerializeShader failed with unknown error"; | ||
} | ||
} | ||
|
||
CNZSL_API const char* nzslSerializerGetLastError(const nzslSerializer* serializerPtr) | ||
{ | ||
assert(serializerPtr); | ||
return serializerPtr->lastError.c_str(); | ||
} | ||
|
||
CNZSL_API nzslDeserializer* nzslDeserializerCreate(const void* data, size_t dataSize) | ||
{ | ||
return new nzslDeserializer(data, dataSize); | ||
} | ||
|
||
CNZSL_API void nzslDeserializerDestroy(nzslDeserializer* deserializerPtr) | ||
{ | ||
delete deserializerPtr; | ||
} | ||
|
||
CNZSL_API nzslModule* nzslDeserializeShader(nzslDeserializer* deserializerPtr) | ||
{ | ||
assert(deserializerPtr); | ||
|
||
try | ||
{ | ||
nzsl::Ast::ModulePtr module = nzsl::Ast::DeserializeShader(deserializerPtr->deserializer); | ||
|
||
nzslModule* modulePtr = nzslModuleCreate(); | ||
modulePtr->module = std::move(module); | ||
return modulePtr; | ||
} | ||
catch(std::exception& e) | ||
{ | ||
deserializerPtr->lastError = fmt::format("nzslDeserializeShader failed: {}", e.what()); | ||
return nullptr; | ||
} | ||
catch(...) | ||
{ | ||
deserializerPtr->lastError = "nzslDeserializeShader failed with unknown error"; | ||
return nullptr; | ||
} | ||
} | ||
|
||
CNZSL_API const char* nzslDeserializerGetLastError(const nzslDeserializer* deserializerPtr) | ||
{ | ||
assert(deserializerPtr); | ||
return deserializerPtr->lastError.c_str(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Copyright (C) 2024 kbz_8 ( [email protected] ) | ||
// This file is part of the "Nazara Shading Language - C Binding" project | ||
// For conditions of distribution and use, see copyright notice in Config.hpp | ||
|
||
#pragma once | ||
|
||
#ifndef CNZSL_STRUCTS_SERIALIZER_HPP | ||
#define CNZSL_STRUCTS_SERIALIZER_HPP | ||
|
||
#include <NZSL/Serializer.hpp> | ||
|
||
#include <string> | ||
#include <cstddef> | ||
|
||
struct nzslSerializer | ||
{ | ||
nzsl::Serializer serializer; | ||
std::string lastError; | ||
}; | ||
|
||
struct nzslDeserializer | ||
{ | ||
nzsl::Deserializer deserializer; | ||
std::string lastError; | ||
|
||
nzslDeserializer(const void* data, size_t dataSize) : deserializer(data, dataSize) {} | ||
}; | ||
|
||
#endif // CNZSL_STRUCTS_SERIALIZER_HPP |