Skip to content

Commit

Permalink
Improving conversions between integer types. (Autodesk#889)
Browse files Browse the repository at this point in the history
* Setting unsigned int parameters from int. (Autodesk#887)

* Adding more tests and conversions. (Autodesk#887)
  • Loading branch information
sirpalee authored Sep 22, 2021
1 parent 6ed1247 commit 82bffbc
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 6 deletions.
26 changes: 20 additions & 6 deletions render_delegate/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,28 @@ auto nodeSetByteFromUChar = [](AtNode* node, const AtString paramName, unsigned
auto nodeSetByteFromLong = [](AtNode* node, const AtString paramName, long v) {
AiNodeSetByte(node, paramName, static_cast<uint8_t>(v));
};
auto nodeSetByteFromUInt = [](AtNode* node, const AtString paramName, unsigned int v) {
AiNodeSetByte(node, paramName, static_cast<uint8_t>(v));
};
auto nodeSetIntFromLong = [](AtNode* node, const AtString paramName, long v) {
AiNodeSetInt(node, paramName, static_cast<int>(v));
};
auto nodeSetIntFromUInt = [](AtNode* node, const AtString paramName, unsigned int v) {
AiNodeSetInt(node, paramName, static_cast<unsigned int>(v));
};
auto nodeSetUIntFromInt = [](AtNode* node, const AtString paramName, int v) {
AiNodeSetUInt(node, paramName, static_cast<unsigned int>(std::max(0, v)));
};
auto nodeSetStrFromToken = [](AtNode* node, const AtString paramName, TfToken v) {
AiNodeSetStr(node, paramName, AtString(v.GetText()));
};
auto nodeSetStrFromStdStr = [](AtNode* node, const AtString paramName, const std::string& v) {
AiNodeSetStr(node, paramName, AtString(v.c_str()));
};
auto nodeSetBoolFromInt = [](AtNode* node, const AtString paramName, int v) { AiNodeSetBool(node, paramName, v != 0); };
auto nodeSetBoolFromUInt = [](AtNode* node, const AtString paramName, unsigned int v) {
AiNodeSetBool(node, paramName, v != 0);
};
auto nodeSetBoolFromLong = [](AtNode* node, const AtString paramName, long v) {
AiNodeSetBool(node, paramName, v != 0);
};
Expand Down Expand Up @@ -1082,19 +1094,21 @@ void HdArnoldSetParameter(AtNode* node, const AtParamEntry* pentry, const VtValu
}
switch (paramType) {
case AI_TYPE_BYTE:
_SetFromValueOrArray<int, unsigned char, long>(
node, paramName, value, nodeSetByteFromInt, nodeSetByteFromUChar, nodeSetByteFromLong);
_SetFromValueOrArray<uint8_t, int, unsigned char, long, unsigned int>(
node, paramName, value, AiNodeSetByte, nodeSetByteFromInt, nodeSetByteFromUChar, nodeSetByteFromLong,
nodeSetByteFromUInt);
break;
case AI_TYPE_INT:
_SetFromValueOrArray<int, long>(node, paramName, value, AiNodeSetInt, nodeSetIntFromLong);
_SetFromValueOrArray<int, long, unsigned int>(
node, paramName, value, AiNodeSetInt, nodeSetIntFromLong, nodeSetIntFromUInt);
break;
case AI_TYPE_UINT:
case AI_TYPE_USHORT:
_SetFromValueOrArray<unsigned int>(node, paramName, value, AiNodeSetUInt);
_SetFromValueOrArray<unsigned int, int>(node, paramName, value, AiNodeSetUInt, nodeSetUIntFromInt);
break;
case AI_TYPE_BOOLEAN:
_SetFromValueOrArray<bool, int, long>(
node, paramName, value, AiNodeSetBool, nodeSetBoolFromInt, nodeSetBoolFromLong);
_SetFromValueOrArray<bool, int, unsigned int, long>(
node, paramName, value, AiNodeSetBool, nodeSetBoolFromInt, nodeSetBoolFromUInt, nodeSetBoolFromLong);
break;
case AI_TYPE_FLOAT:
case AI_TYPE_HALF:
Expand Down
70 changes: 70 additions & 0 deletions testsuite/test_0134/data/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,76 @@ TEST(HdArnoldSetParameter, AssetPath)
EXPECT_EQ(AiNodeGetStr(node, "filename"), AtString("second"));
}

TEST(HdArnoldSetParameter, ByteConversions)
{
auto* node = AiNode("polymesh");
auto* entry = AiNodeGetNodeEntry(node);
auto* subdiv_iterations = AiNodeEntryLookUpParameter(entry, "subdiv_iterations");
uint8_t u8 = 11;
HdArnoldSetParameter(node, subdiv_iterations, VtValue{u8});
EXPECT_EQ(AiNodeGetByte(node, "subdiv_iterations"), u8);
int i = 12;
HdArnoldSetParameter(node, subdiv_iterations, VtValue{i});
EXPECT_EQ(AiNodeGetByte(node, "subdiv_iterations"), i);
unsigned char uc = 13;
HdArnoldSetParameter(node, subdiv_iterations, VtValue{uc});
EXPECT_EQ(AiNodeGetByte(node, "subdiv_iterations"), uc);
long l = 14;
HdArnoldSetParameter(node, subdiv_iterations, VtValue{l});
EXPECT_EQ(AiNodeGetByte(node, "subdiv_iterations"), l);
unsigned int ui = 15;
HdArnoldSetParameter(node, subdiv_iterations, VtValue{ui});
EXPECT_EQ(AiNodeGetByte(node, "subdiv_iterations"), ui);
}

TEST(HdArnoldSetParameter, IntConversions)
{
auto* node = AiNode("standard_surface");
auto* entry = AiNodeGetNodeEntry(node);
auto* dielectric_priority = AiNodeEntryLookUpParameter(entry, "dielectric_priority");
int i = 1;
HdArnoldSetParameter(node, dielectric_priority, VtValue{i});
EXPECT_EQ(AiNodeGetInt(node, "dielectric_priority"), i);
long l = 2;
HdArnoldSetParameter(node, dielectric_priority, VtValue{l});
EXPECT_EQ(AiNodeGetInt(node, "dielectric_priority"), l);
unsigned int ui = 3;
HdArnoldSetParameter(node, dielectric_priority, VtValue{ui});
EXPECT_EQ(AiNodeGetInt(node, "dielectric_priority"), ui);
}

TEST(HdArnoldSetParameter, UnsignedIntConversions)
{
auto* node = AiNode("standard_hair");
auto* entry = AiNodeGetNodeEntry(node);
auto* extra_samples = AiNodeEntryLookUpParameter(entry, "extra_samples");
unsigned int ui = 1;
HdArnoldSetParameter(node, extra_samples, VtValue{ui});
EXPECT_EQ(AiNodeGetUInt(node, "extra_samples"), ui);
int i = 2;
HdArnoldSetParameter(node, extra_samples, VtValue{i});
EXPECT_EQ(AiNodeGetUInt(node, "extra_samples"), i);
}

TEST(HdArnoldSetParameter, BoolConversions)
{
auto* node = AiNode("polymesh");
auto* entry = AiNodeGetNodeEntry(node);
auto* matte = AiNodeEntryLookUpParameter(entry, "matte");
bool b = true;
HdArnoldSetParameter(node, matte, VtValue{b});
EXPECT_EQ(AiNodeGetBool(node, "matte"), b);
int i = 0;
HdArnoldSetParameter(node, matte, VtValue{i});
EXPECT_EQ(AiNodeGetBool(node, "matte"), i != 0);
unsigned int ui = 1;
HdArnoldSetParameter(node, matte, VtValue{ui});
EXPECT_EQ(AiNodeGetBool(node, "matte"), ui != 0);
long l = 0;
HdArnoldSetParameter(node, matte, VtValue{l});
EXPECT_EQ(AiNodeGetBool(node, "matte"), l != 0);
}

TEST(HdArnoldSetConstantPrimvar, Base)
{
auto* node = AiNode("polymesh");
Expand Down

0 comments on commit 82bffbc

Please sign in to comment.