Skip to content

Commit 76454f1

Browse files
committed
Minor API improvements, ABI and test fixes
Signed-off-by: Nick Avramoussis <[email protected]>
1 parent 4ff1d35 commit 76454f1

File tree

6 files changed

+82
-55
lines changed

6 files changed

+82
-55
lines changed

openvdb_ax/openvdb_ax/codegen/Codecs.cc

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ inline FunctionGroup::UniquePtr axprfxpt16encode() { return axfxptencode(false,
303303
///////////////////////////////////////////////////////////////////////////////
304304
///////////////////////////////////////////////////////////////////////////////
305305

306-
const CodecTypeMap& Codec::getCodecTypeMap()
306+
const CodecTypeMap& getCodecTypeMap()
307307
{
308308
// Initialise the static codec registry of supported types.
309309
// This can easily be exposed to users so they can write their own codecs,
@@ -317,11 +317,11 @@ const CodecTypeMap& Codec::getCodecTypeMap()
317317
// in a separate change set.
318318

319319
static std::array<Codec::UniquePtr, 5> codecs {
320-
std::make_unique<Codec>(std::move(axtrncencode()), std::move(axtrncdecode()), 1<<0),
321-
std::make_unique<Codec>(std::move(axufxpt8encode()), std::move(axufxpt8decode()), 1<<1),
322-
std::make_unique<Codec>(std::move(axufxpt16encode()), std::move(axufxpt16decode()), 1<<2),
323-
std::make_unique<Codec>(std::move(axprfxpt8encode()), std::move(axprfxpt8decode()), 1<<3),
324-
std::make_unique<Codec>(std::move(axprfxpt16encode()), std::move(axprfxpt16decode()), 1<<4),
320+
std::make_unique<Codec>(axtrncencode(), axtrncdecode(), 1<<0),
321+
std::make_unique<Codec>(axufxpt8encode(), axufxpt8decode(), 1<<1),
322+
std::make_unique<Codec>(axufxpt16encode(), axufxpt16decode(), 1<<2),
323+
std::make_unique<Codec>(axprfxpt8encode(), axprfxpt8decode(), 1<<3),
324+
std::make_unique<Codec>(axprfxpt16encode(), axprfxpt16decode(), 1<<4),
325325
};
326326

327327
static CodecTypeMap map {
@@ -365,7 +365,7 @@ llvm::Type* Codec::findReturnTypeFromArg(const codegen::FunctionGroup* const gro
365365

366366
const Codec* getCodec(const ast::tokens::CoreType type, const std::string& name)
367367
{
368-
const CodecTypeMap& map = Codec::getCodecTypeMap();
368+
const CodecTypeMap& map = getCodecTypeMap();
369369

370370
auto typeiter = map.find(type);
371371
if (typeiter != map.cend()) {
@@ -379,7 +379,7 @@ const Codec* getCodec(const ast::tokens::CoreType type, const std::string& name)
379379

380380
const CodecNameMap* getTypeSupportedCodecs(const ast::tokens::CoreType type)
381381
{
382-
const CodecTypeMap& map = Codec::getCodecTypeMap();
382+
const CodecTypeMap& map = getCodecTypeMap();
383383

384384
auto typeiter = map.find(type);
385385
if (typeiter != map.cend()) {

openvdb_ax/openvdb_ax/codegen/Codecs.h

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,39 @@ using CodecNameMap = std::map<const std::string, const Codec*>;
2222
using CodecTypeMap = std::map<const ast::tokens::CoreType, CodecNameMap>;
2323
using Codecs = std::vector<const Codec*>;
2424

25+
/// @brief Get the global codec map
26+
OPENVDB_AX_API const CodecTypeMap& getCodecTypeMap();
27+
28+
/// @brief Get a specific codec. Returns a nullptr if no codec exists.
29+
/// @param type The type the codec encodes
30+
/// @param name The name of the codec
31+
OPENVDB_AX_API const Codec* getCodec(const ast::tokens::CoreType type, const std::string& name);
32+
33+
/// @brief Get a specific set of codecs which encode a given type. Returns a
34+
/// nullptr if no codec exists.
35+
/// @param type The type the codecs encode
36+
OPENVDB_AX_API const CodecNameMap* getTypeSupportedCodecs(const ast::tokens::CoreType type);
37+
2538
class Codec
2639
{
2740
public:
2841
using UniquePtr = std::unique_ptr<Codec>;
2942

30-
static const CodecTypeMap& getCodecTypeMap();
43+
Codec(codegen::FunctionGroup::UniquePtr encoder,
44+
codegen::FunctionGroup::UniquePtr decoder,
45+
uint32_t flag)
46+
: mEncoder(std::move(encoder))
47+
, mDecoder(std::move(decoder))
48+
, mFlag(flag) {
49+
#ifndef NDEBUG
50+
assert(!mEncoder->list().empty());
51+
assert(!mDecoder->list().empty());
52+
assert(mEncoder->list().size() == mDecoder->list().size());
53+
for (const auto& F : mEncoder->list()) {
54+
assert(F->size() == 1 || F->size() == 2);
55+
}
56+
#endif
57+
}
3158

3259
/// @brief Given a core type supported by the AX frontend, return a llvm
3360
/// compatible type which represents how the core type is encoded in
@@ -54,7 +81,7 @@ class Codec
5481
/// or a nullptr.
5582
/// @return A llvm type representing the decoded C type. Can be a nullptr
5683
/// if this codec does not support the provided core type.
57-
llvm::Type* encodedToDecoded(llvm::Type* in, llvm::LLVMContext& C) const
84+
llvm::Type* encodedToDecoded(llvm::Type* in) const
5885
{
5986
// For each decoder function in this codec, find the one which
6087
// one takes the provided "in" encoded type and return the type
@@ -66,35 +93,16 @@ class Codec
6693

6794
const codegen::FunctionGroup* encoder() const { return mEncoder.get(); }
6895
const codegen::FunctionGroup* decoder() const { return mDecoder.get(); }
69-
inline const uint32_t bit() const { return mBit; }
70-
71-
//private:
72-
Codec(codegen::FunctionGroup::UniquePtr encoder,
73-
codegen::FunctionGroup::UniquePtr decoder,
74-
uint32_t bit)
75-
: mEncoder(std::move(encoder))
76-
, mDecoder(std::move(decoder))
77-
, mBit(bit) {
78-
#ifndef NDEBUG
79-
assert(!mEncoder->list().empty());
80-
assert(!mDecoder->list().empty());
81-
assert(mEncoder->list().size() == mDecoder->list().size());
82-
for (const auto& F : mEncoder->list()) {
83-
assert(F->size() == 1 || F->size() == 2);
84-
}
85-
#endif
86-
}
96+
inline uint32_t flag() const { return mFlag; }
8797

98+
private:
8899
llvm::Type* findReturnTypeFromArg(const codegen::FunctionGroup* const, llvm::Type*) const;
89100

90-
private:
91101
const codegen::FunctionGroup::UniquePtr mEncoder;
92102
const codegen::FunctionGroup::UniquePtr mDecoder;
93-
const uint32_t mBit;
103+
const uint32_t mFlag;
94104
};
95105

96-
const Codec* getCodec(const ast::tokens::CoreType type, const std::string& name);
97-
const CodecNameMap* getTypeSupportedCodecs(const ast::tokens::CoreType type);
98106

99107
} // namespace codegen
100108
} // namespace ax

openvdb_ax/openvdb_ax/codegen/PointComputeGenerator.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ decode(llvm::Value* buffer,
341341
llvm::BasicBlock* els = llvm::BasicBlock::Create(C, "", self);
342342

343343
llvm::Value* usescodec =
344-
B.CreateAnd(flag, LLVMType<uint64_t>::get(C, codec->bit()));
344+
B.CreateAnd(flag, LLVMType<uint64_t>::get(C, codec->flag()));
345345
usescodec = boolComparison(usescodec, B);
346346
B.CreateCondBr(usescodec, then, els);
347347

@@ -416,7 +416,7 @@ encode(llvm::Value* in,
416416
llvm::BasicBlock* els = llvm::BasicBlock::Create(C, "", self);
417417

418418
llvm::Value* usescodec =
419-
B.CreateAnd(flag, LLVMType<uint64_t>::get(C, codec->bit()));
419+
B.CreateAnd(flag, LLVMType<uint64_t>::get(C, codec->flag()));
420420
usescodec = boolComparison(usescodec, B);
421421
B.CreateCondBr(usescodec, then, els);
422422

@@ -493,7 +493,7 @@ inline void PointComputeGenerator::computePKB(const AttributeRegistry& registry)
493493
// If the value type has supported codecs we have to allocate the
494494
// expected decoded type that will be stored. Otherwise, decode()
495495
// will simply extract the value ptr directly from the buffer.
496-
llvm::Value* decodedStore;
496+
llvm::Value* decodedStore = nullptr;
497497
const auto* codecs = getTypeSupportedCodecs(data.type());
498498
if (codecs) decodedStore = insertStaticAlloca(B, llvmTypeFromToken(data.type(), C)); // allocated to prologue
499499

openvdb_ax/openvdb_ax/compiler/PointExecutable.cc

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -250,20 +250,28 @@ struct PointFunctionArguments
250250

251251
// @todo if the array is shared we should probably make it unique?
252252

253+
#if OPENVDB_ABI_VERSION_NUMBER >= 9
253254
if (mData.mUseBufferKernel) {
254255
const_cast<points::AttributeArray&>(array).loadData();
255256
const char* data = array.constDataAsByteArray();
256257
void* ptr = static_cast<void*>(const_cast<char*>(data));
257258
mHandlesOrBuffers.emplace_back(ptr);
258259
const codegen::Codec* codec =
259260
codegen::getCodec(ast::tokens::tokenFromTypeString(array.valueType()), array.codecType());
260-
if (codec) flag |= codec->bit();
261+
if (codec) flag |= codec->flag();
261262
}
262263
else {
263264
typename ReadHandle<ValueT>::UniquePtr handle(new ReadHandle<ValueT>(leaf, Index(pos)));
264265
mHandlesOrBuffers.emplace_back(handle->mHandle.get());
265266
mAttributeHandles.emplace_back(std::move(handle));
266267
}
268+
#else
269+
assert(!mData.mUseBufferKernel);
270+
typename ReadHandle<ValueT>::UniquePtr handle(new ReadHandle<ValueT>(leaf, Index(pos)));
271+
mHandlesOrBuffers.emplace_back(handle->mHandle.get());
272+
mAttributeHandles.emplace_back(std::move(handle));
273+
#endif
274+
267275
mFlags.emplace_back(flag);
268276
}
269277

@@ -274,21 +282,29 @@ struct PointFunctionArguments
274282
points::AttributeArray& array = leaf.attributeArray(pos);
275283
array.expand();
276284

285+
#if OPENVDB_ABI_VERSION_NUMBER >= 9
277286
if (mData.mUseBufferKernel) {
278287
array.loadData();
279288
const char* data = array.constDataAsByteArray();
280289
void* ptr = static_cast<void*>(const_cast<char*>(data));
281290
mHandlesOrBuffers.emplace_back(ptr);
282291
const codegen::Codec* codec =
283292
codegen::getCodec(ast::tokens::tokenFromTypeString(array.valueType()), array.codecType());
284-
if (codec) flag |= codec->bit();
293+
if (codec) flag |= codec->flag();
285294
assert(array.isDataLoaded() && !array.isUniform());
286295
}
287296
else {
288297
typename WriteHandle<ValueT>::UniquePtr handle(new WriteHandle<ValueT>(leaf, Index(pos)));
289298
mHandlesOrBuffers.emplace_back(handle->mHandle.get());
290299
mAttributeHandles.emplace_back(std::move(handle));
291300
}
301+
#else
302+
assert(!mData.mUseBufferKernel);
303+
typename WriteHandle<ValueT>::UniquePtr handle(new WriteHandle<ValueT>(leaf, Index(pos)));
304+
mHandlesOrBuffers.emplace_back(handle->mHandle.get());
305+
mAttributeHandles.emplace_back(std::move(handle));
306+
#endif
307+
292308
mFlags.emplace_back(flag);
293309
}
294310

@@ -434,7 +450,6 @@ struct PointExecuterOp
434450

435451
void operator()(LeafNode& leaf, size_t idx) const
436452
{
437-
const points::AttributeSet& set = leaf.attributeSet();
438453
auto& leafLocalData = mLeafLocalData[idx];
439454
leafLocalData.reset(new PointLeafLocalData(leaf.getLastValue()));
440455

@@ -454,7 +469,6 @@ struct PointExecuterOp
454469

455470
PointFunctionArguments args(mData, leaf, leafLocalData.get());
456471
void* buffer = static_cast<void*>(leaf.buffer().data());
457-
static std::once_flag flag;
458472

459473
if (group) {
460474
const auto kernel = args.bindValueKernel();
@@ -527,7 +541,7 @@ inline NamePair typePairFromToken(const ast::tokens::CoreType type)
527541
return NamePair();
528542
}
529543
}
530-
};
544+
}
531545

532546
void processAttributes(points::PointDataGrid& grid,
533547
std::vector<PointAttributeInfo>& attributeInfo,
@@ -745,6 +759,7 @@ void PointExecutable::execute(openvdb::points::PointDataGrid& grid) const
745759
// Compute whether we can use the accelerated kernel
746760
// @note Assumes attributes are valid (i.e. has errored out if they are not)
747761

762+
#if OPENVDB_ABI_VERSION_NUMBER >= 9
748763
if (!usingGroup) {
749764
const auto& desc = leafIter->attributeSet().descriptor();
750765
data.mUseBufferKernel = checkCodecs(desc, *mAttributeRegistry,
@@ -755,6 +770,10 @@ void PointExecutable::execute(openvdb::points::PointDataGrid& grid) const
755770
// if a group has been specified we can't use the buffer range yet
756771
data.mUseBufferKernel = false;
757772
}
773+
#else
774+
// can't access data buffers until ABI >= 9
775+
data.mUseBufferKernel = false;
776+
#endif
758777

759778
// execute
760779

openvdb_ax/openvdb_ax/test/backend/TestCodecs.cc

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ inline const Codec*
4747
getCodecByCodecName(const std::string& name)
4848
{
4949
std::vector<const Codec*> codecs;
50-
const auto& map = Codec::getCodecTypeMap();
50+
const auto& map = getCodecTypeMap();
5151
for (const auto& typemap : map) {
5252
for (const auto& nameToCodec : typemap.second) {
5353
if (nameToCodec.first == name) {
@@ -67,7 +67,7 @@ void TestCodecs::testRegisteredCodecs()
6767

6868
// Get all unique registered codecs
6969
std::set<const Codec*> codecs;
70-
const auto& map = Codec::getCodecTypeMap();
70+
const auto& map = getCodecTypeMap();
7171

7272
for (const auto& typemap : map) {
7373
for (const auto& nameToCodec : typemap.second) {
@@ -84,8 +84,8 @@ void TestCodecs::testRegisteredCodecs()
8484

8585
std::set<uint32_t> flags;
8686
for (const Codec* codec : codecs) {
87-
CPPUNIT_ASSERT(!flags.count(codec->bit()));
88-
flags.insert(codec->bit());
87+
CPPUNIT_ASSERT(!flags.count(codec->flag()));
88+
flags.insert(codec->flag());
8989
}
9090

9191
//
@@ -191,8 +191,8 @@ void TestCodecs::testTruncateCodec()
191191

192192
CPPUNIT_ASSERT_EQUAL(halfty, codec->decodedToEncoded(ast::tokens::CoreType::FLOAT, C));
193193
CPPUNIT_ASSERT_EQUAL(vhalfty, codec->decodedToEncoded(ast::tokens::CoreType::VEC3F, C));
194-
CPPUNIT_ASSERT_EQUAL(floatty, codec->encodedToDecoded(halfty, C));
195-
CPPUNIT_ASSERT_EQUAL(vfloatty, codec->encodedToDecoded(vhalfty, C));
194+
CPPUNIT_ASSERT_EQUAL(floatty, codec->encodedToDecoded(halfty));
195+
CPPUNIT_ASSERT_EQUAL(vfloatty, codec->encodedToDecoded(vhalfty));
196196

197197
// JIT the codec and test the IR
198198

@@ -216,8 +216,8 @@ void TestCodecs::testTruncateCodec()
216216

217217
const std::vector<float> floatInputs {
218218
1.0f, 0.0f, -1.0f,
219-
0.5f, 0.13454, -0.98781,
220-
1.0431e-6, 1.0431e+6, std::numeric_limits<float>::max(),
219+
0.5f, 0.13454f, -0.98781f,
220+
1.0431e-6f, 1.0431e+6f, std::numeric_limits<float>::max(),
221221
313.33f, std::numeric_limits<float>::min(), std::numeric_limits<float>::lowest()
222222
};
223223

@@ -269,8 +269,8 @@ void TestCodecs::testTruncateCodec()
269269

270270
const std::vector<HalfTy> halfInputs {
271271
1.0f, 0.0f, -1.0f,
272-
0.5f, 0.13454, -0.98781,
273-
1.0431e-6, 1.0431e+6, std::numeric_limits<HalfTy>::max(),
272+
0.5f, 0.13454f, -0.98781f,
273+
1.0431e-6f, 1.0431e+6f, std::numeric_limits<HalfTy>::max(),
274274
313.33f, std::numeric_limits<HalfTy>::min(), std::numeric_limits<HalfTy>::lowest()
275275
};
276276

@@ -340,8 +340,8 @@ void TestCodecs::testFxptCodec()
340340
CPPUNIT_ASSERT(nullptr == codec->decodedToEncoded(ast::tokens::CoreType::STRING, C));
341341
CPPUNIT_ASSERT_EQUAL(uintty, codec->decodedToEncoded(ast::tokens::CoreType::FLOAT, C));
342342
CPPUNIT_ASSERT_EQUAL(vuintty, codec->decodedToEncoded(ast::tokens::CoreType::VEC3F, C));
343-
CPPUNIT_ASSERT_EQUAL(floatty, codec->encodedToDecoded(uintty, C));
344-
CPPUNIT_ASSERT_EQUAL(vfloatty, codec->encodedToDecoded(vuintty, C));
343+
CPPUNIT_ASSERT_EQUAL(floatty, codec->encodedToDecoded(uintty));
344+
CPPUNIT_ASSERT_EQUAL(vfloatty, codec->encodedToDecoded(vuintty));
345345

346346
// JIT the codec and test the IR
347347

@@ -367,8 +367,8 @@ void TestCodecs::testFxptCodec()
367367

368368
const std::vector<float> floatInputs {
369369
1.0f, 0.0f, -1.0f,
370-
0.5f, 0.20024414435034715, -0.98781,
371-
1e-3, 0.2f, 0.6f,
370+
0.5f, 0.20024414435034715f, -0.98781f,
371+
1e-3f, 0.2f, 0.6f,
372372
0.8f, 1.5f, -1.5f,
373373
100.0f, std::numeric_limits<float>::lowest(), -100.0f
374374
};

openvdb_ax/openvdb_ax/test/compiler/TestPointExecutable.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -739,7 +739,7 @@ TestPointExecutable::testAttributeCodecs()
739739
pos.z() += 2.0f;
740740

741741
const math::Coord coord = leafIter->cbeginValueOn().getCoord();
742-
pos = defaultTransform->worldToIndex(pos);
742+
pos = Vec3f(defaultTransform->worldToIndex(pos));
743743
pos -= coord.asVec3s();
744744

745745
CPPUNIT_ASSERT_EQUAL(compress(points::FixedPointCodec<true, points::PositionRange>(), pos.x()), handle0.get(0).x());

0 commit comments

Comments
 (0)