OpenVDB 12.1.0

Loading...
Searching...
No Matches
Codecs.h
Go to the documentation of this file.
1// Copyright Contributors to the OpenVDB Project
2// SPDX-License-Identifier: Apache-2.0
3
4#ifndef OPENVDB_AX_CODEGEN_CODECS_HAS_BEEN_INCLUDED
5#define OPENVDB_AX_CODEGEN_CODECS_HAS_BEEN_INCLUDED
6
7#include <openvdb/openvdb.h>
8#include <openvdb/version.h>
9#include <openvdb/util/Assert.h>
10
11#include "openvdb_ax/ast/Tokens.h"
12#include "openvdb_ax/codegen/FunctionTypes.h"
13
14 namespace openvdb {
15 OPENVDB_USE_VERSION_NAMESPACE
16 namespace OPENVDB_VERSION_NAME {
17 namespace ax {
18 namespace codegen {
19
20 class Codec;
21
22 using CodecNameMap = std::map<const std::string, const Codec*>;
23 using CodecTypeMap = std::map<const ast::tokens::CoreType, CodecNameMap>;
24 using Codecs = std::vector<const Codec*>;
25
26/// @brief Get the global codec map
27 OPENVDB_AX_API const CodecTypeMap& getCodecTypeMap();
28
29/// @brief Get a specific codec. Returns a nullptr if no codec exists.
30/// @param type The type the codec encodes
31/// @param name The name of the codec
32 OPENVDB_AX_API const Codec* getCodec(const ast::tokens::CoreType type, const std::string& name);
33
34/// @brief Get a specific set of codecs which encode a given type. Returns a
35/// nullptr if no codec exists.
36/// @param type The type the codecs encode
37 OPENVDB_AX_API const CodecNameMap* getTypeSupportedCodecs(const ast::tokens::CoreType type);
38
39 class OPENVDB_AX_API Codec
40{
41 public:
42 using UniquePtr = std::unique_ptr<Codec>;
43
44 Codec(codegen::FunctionGroup::UniquePtr encoder,
45 codegen::FunctionGroup::UniquePtr decoder,
46 uint32_t flag)
47 : mEncoder(std::move(encoder))
48 , mDecoder(std::move(decoder))
49 , mFlag(flag) {
50#ifdef OPENVDB_ENABLE_ASSERTS
51 OPENVDB_ASSERT(!mEncoder->list().empty());
52 OPENVDB_ASSERT(!mDecoder->list().empty());
53 OPENVDB_ASSERT(mEncoder->list().size() == mDecoder->list().size());
54 for ([[maybe_unused]] const auto& F : mEncoder->list()) {
55 OPENVDB_ASSERT(F->size() == 1 || F->size() == 2);
56 }
57#endif
58 }
59
60 /// @brief Given a core type supported by the AX frontend, return a llvm
61 /// compatible type which represents how the core type is encoded in
62 /// memory.
63 /// @return A llvm type representing the encoded C type. Can be a nullptr
64 /// if this codec does not support the provided core type.
65 llvm::Type* decodedToEncoded(const ast::tokens::CoreType& in, llvm::LLVMContext& C) const
66 {
67 // the input "decoded" type - unlike the encoded type, the decoded type
68 // has to be available as an AX "CoreType" which is why this function
69 // takes a CoreType in
70 llvm::Type* type = codegen::llvmTypeFromToken(in, C);
71 // For each encoder function in this codec, find the one which
72 // one takes the provided "in" decoded type and return the type
73 // of that function return signature
74 llvm::Type* ret = findReturnTypeFromArg(this->encoder(), type);
75 return ret;
76 }
77
78 /// @brief Given a llvm type, return a compatible llvm type which
79 /// represents how the provided type should be exposed to the AX frontend.
80 /// @note The return type is guaranteed to either be a supported CoreType
81 /// (such that ax::codegen::tokenFromLLVMType(in) returns a valid value)
82 /// or a nullptr.
83 /// @return A llvm type representing the decoded C type. Can be a nullptr
84 /// if this codec does not support the provided core type.
85 llvm::Type* encodedToDecoded(llvm::Type* in) const
86 {
87 // For each decoder function in this codec, find the one which
88 // one takes the provided "in" encoded type and return the type
89 // of that function return signature
90 llvm::Type* ret = findReturnTypeFromArg(this->decoder(), in);
91 return ret;
92 }
93
94 const codegen::FunctionGroup* encoder() const { return mEncoder.get(); }
95 const codegen::FunctionGroup* decoder() const { return mDecoder.get(); }
96 inline uint32_t flag() const { return mFlag; }
97
98 private:
99 llvm::Type* findReturnTypeFromArg(const codegen::FunctionGroup* const, llvm::Type*) const;
100
101 const codegen::FunctionGroup::UniquePtr mEncoder;
102 const codegen::FunctionGroup::UniquePtr mDecoder;
103 const uint32_t mFlag;
104};
105
106
107} // namespace codegen
108} // namespace ax
109} // namespace OPENVDB_VERSION_NAME
110} // namespace openvdb
111
112#endif // OPENVDB_AX_CODEGEN_CODECS_HAS_BEEN_INCLUDED
113
Assert.h
OPENVDB_ASSERT
#define OPENVDB_ASSERT(X)
Definition Assert.h:41
FunctionTypes.h
Contains frameworks for creating custom AX functions which can be registered within the FunctionRegis...
OPENVDB_AX_API
#define OPENVDB_AX_API
Definition Platform.h:312
Tokens.h
Various function and operator tokens used throughout the AST and code generation.
openvdb::v12_1::ax::codegen::Codec
Definition Codecs.h:40
openvdb::v12_1::ax::codegen::Codec::Codec
Codec(codegen::FunctionGroup::UniquePtr encoder, codegen::FunctionGroup::UniquePtr decoder, uint32_t flag)
Definition Codecs.h:44
openvdb::v12_1::ax::codegen::Codec::encodedToDecoded
llvm::Type * encodedToDecoded(llvm::Type *in) const
Given a llvm type, return a compatible llvm type which represents how the provided type should be exp...
Definition Codecs.h:85
openvdb::v12_1::ax::codegen::Codec::decodedToEncoded
llvm::Type * decodedToEncoded(const ast::tokens::CoreType &in, llvm::LLVMContext &C) const
Given a core type supported by the AX frontend, return a llvm compatible type which represents how th...
Definition Codecs.h:65
openvdb::v12_1::ax::codegen::Codec::encoder
const codegen::FunctionGroup * encoder() const
Definition Codecs.h:94
openvdb::v12_1::ax::codegen::Codec::decoder
const codegen::FunctionGroup * decoder() const
Definition Codecs.h:95
openvdb::v12_1::ax::codegen::Codec::UniquePtr
std::unique_ptr< Codec > UniquePtr
Definition Codecs.h:42
openvdb::v12_1::ax::codegen::Codec::flag
uint32_t flag() const
Definition Codecs.h:96
openvdb::v12_1::ax::ast::tokens::CoreType
CoreType
Definition Tokens.h:32
openvdb::v12_1::ax::codegen
Definition Codecs.h:18
openvdb::v12_1::ax::codegen::CodecTypeMap
std::map< const ast::tokens::CoreType, CodecNameMap > CodecTypeMap
Definition Codecs.h:23
openvdb::v12_1::ax::codegen::getTypeSupportedCodecs
OPENVDB_AX_API const CodecNameMap * getTypeSupportedCodecs(const ast::tokens::CoreType type)
Get a specific set of codecs which encode a given type. Returns a nullptr if no codec exists.
openvdb::v12_1::ax::codegen::llvmTypeFromToken
OPENVDB_AX_API llvm::Type * llvmTypeFromToken(const ast::tokens::CoreType &type, llvm::LLVMContext &C)
Returns an llvm type representing a type defined by a string.
openvdb::v12_1::ax::codegen::CodecNameMap
std::map< const std::string, const Codec * > CodecNameMap
Definition Codecs.h:22
openvdb::v12_1::ax::codegen::getCodecTypeMap
OPENVDB_AX_API const CodecTypeMap & getCodecTypeMap()
Get the global codec map.
openvdb::v12_1::ax::codegen::getCodec
OPENVDB_AX_API const Codec * getCodec(const ast::tokens::CoreType type, const std::string &name)
Get a specific codec. Returns a nullptr if no codec exists.
openvdb::v12_1::ax::codegen::Codecs
std::vector< const Codec * > Codecs
Definition Codecs.h:24
openvdb
Definition Exceptions.h:13
std
Definition Coord.h:590
openvdb.h
openvdb::v12_1::ax::codegen::FunctionGroup
A group of functions which all have the same name but different signatures. For example: float abs(fl...
Definition FunctionTypes.h:1390
openvdb::v12_1::ax::codegen::FunctionGroup::UniquePtr
std::unique_ptr< FunctionGroup > UniquePtr
Definition FunctionTypes.h:1392
OPENVDB_VERSION_NAME
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition version.h.in:121
OPENVDB_USE_VERSION_NAMESPACE
#define OPENVDB_USE_VERSION_NAMESPACE
Definition version.h.in:218