Skip to content

Commit

Permalink
fix for half floats and transforms
Browse files Browse the repository at this point in the history
  • Loading branch information
carsonbrownlee committed May 14, 2024
1 parent 4433e42 commit ba61426
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 14 deletions.
31 changes: 20 additions & 11 deletions hdOSPRay/material.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ TF_DEFINE_PRIVATE_TOKENS(
(map_opacity)
(UsdUVTexture)
(normal)
(map_normal)
(displacement)
(file)
(filename)
Expand All @@ -68,6 +69,7 @@ TF_DEFINE_PRIVATE_TOKENS(
(repeat)
(mirror)
(rotation)
(map_rotation)
(translation)
(diffuse)
(coat)
Expand Down Expand Up @@ -494,6 +496,8 @@ HdOSPRayMaterial::_ProcessTextureNode(HdMaterialNode node,
filename = path.GetResolvedPath();
} else if (name == HdOSPRayMaterialTokens->scale) {
texture.scale = value.Get<GfVec4f>();
texture.xfm_scale = GfVec2f(texture.scale[0], texture.scale[1]);
texture.hasXfm = true;
} else if (name == HdOSPRayMaterialTokens->wrapS) {
} else if (name == HdOSPRayMaterialTokens->wrapT) {
} else if (name == HdOSPRayMaterialTokens->st) {
Expand Down Expand Up @@ -621,7 +625,12 @@ HdOSPRayMaterial::UpdatePrincipledMaterial(const std::string& rendererType)
} else if (key == HdOSPRayMaterialTokens->roughness
|| key == HdOSPRayMaterialTokens->map_roughness) {
name = "map_roughness";
hasRoughnessTex = true;
} else if (key == HdOSPRayMaterialTokens->normal
|| key == HdOSPRayMaterialTokens->map_normal) {
name = "map_normal";
} else if (key == HdOSPRayMaterialTokens->rotation
|| key == HdOSPRayMaterialTokens->map_rotation) {
name = "map_rotation";
} else if (key == HdOSPRayMaterialTokens->opacity
|| key == HdOSPRayMaterialTokens->map_opacity) {
if (_type == MaterialTypes::preview) {
Expand All @@ -646,8 +655,8 @@ HdOSPRayMaterial::UpdatePrincipledMaterial(const std::string& rendererType)
vec2f(-value.xfm_translation[0],
-value.xfm_translation[1]));
_ospMaterial.setParam(st_scale.c_str(),
vec2f(1.0f / value.xfm_scale[0],
1.0f / value.xfm_scale[1]));
vec2f(value.xfm_scale[0],
value.xfm_scale[1]));
_ospMaterial.setParam(st_rotation.c_str(),
-value.xfm_rotation);
}
Expand Down Expand Up @@ -719,8 +728,8 @@ HdOSPRayMaterial::UpdateCarPaintMaterial()
vec2f(-value.xfm_translation[0],
-value.xfm_translation[1]));
_ospMaterial.setParam(st_scale.c_str(),
vec2f(1.0f / value.xfm_scale[0],
1.0f / value.xfm_scale[1]));
vec2f(value.xfm_scale[0],
value.xfm_scale[1]));
_ospMaterial.setParam(st_rotation.c_str(), -value.xfm_rotation);
}
}
Expand Down Expand Up @@ -774,8 +783,8 @@ HdOSPRayMaterial::UpdateLuminousMaterial()
vec2f(-value.xfm_translation[0],
-value.xfm_translation[1]));
_ospMaterial.setParam(st_scale.c_str(),
vec2f(1.0f / value.xfm_scale[0],
1.0f / value.xfm_scale[1]));
vec2f(value.xfm_scale[0],
value.xfm_scale[1]));
_ospMaterial.setParam(st_rotation.c_str(), -value.xfm_rotation);
}
}
Expand Down Expand Up @@ -813,8 +822,8 @@ HdOSPRayMaterial::UpdateGlassMaterial()
vec2f(-value.xfm_translation[0],
-value.xfm_translation[1]));
_ospMaterial.setParam(st_scale.c_str(),
vec2f(1.0f / value.xfm_scale[0],
1.0f / value.xfm_scale[1]));
vec2f(value.xfm_scale[0],
value.xfm_scale[1]));
_ospMaterial.setParam(st_rotation.c_str(), -value.xfm_rotation);
}
}
Expand Down Expand Up @@ -850,8 +859,8 @@ HdOSPRayMaterial::UpdateThinGlassMaterial()
vec2f(-value.xfm_translation[0],
-value.xfm_translation[1]));
_ospMaterial.setParam(st_scale.c_str(),
vec2f(1.0f / value.xfm_scale[0],
1.0f / value.xfm_scale[1]));
vec2f(value.xfm_scale[0],
value.xfm_scale[1]));
_ospMaterial.setParam(st_rotation.c_str(), -value.xfm_rotation);
}
}
Expand Down
42 changes: 39 additions & 3 deletions hdOSPRay/texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ osprayTextureFormat(int depth, int channels, bool preferLinear)
return preferLinear ? OSP_TEXTURE_RGB8 : OSP_TEXTURE_SRGB;
if (channels == 4)
return preferLinear ? OSP_TEXTURE_RGBA8 : OSP_TEXTURE_SRGBA;
} else if (depth == 2) {
if (channels == 1)
return OSP_TEXTURE_R16;
if (channels == 2)
return OSP_TEXTURE_RA16;
if (channels == 3)
return OSP_TEXTURE_RGB16;
if (channels == 4)
return OSP_TEXTURE_RGBA16;
} else if (depth == 4) {
if (channels == 1)
return OSP_TEXTURE_R32F;
Expand Down Expand Up @@ -82,18 +91,22 @@ LoadHioTexture2D(const std::string file, const std::string channelsStr,
size.x = desc.width;
size.y = desc.height;
const bool srgb = image->IsColorSpaceSRGB();
bool isHalfFloat = false;
int depth = 1;
if (desc.format == HioFormatFloat16 || desc.format == HioFormatFloat16Vec2
|| desc.format == HioFormatFloat16Vec3
|| desc.format == HioFormatFloat16Vec4 || desc.format == HioFormatUInt16
|| desc.format == HioFormatFloat16Vec4) {
depth = 2;
isHalfFloat = true;
} else if (desc.format == HioFormatUInt16
|| desc.format == HioFormatUInt16Vec2
|| desc.format == HioFormatUInt16Vec3
|| desc.format == HioFormatUInt16Vec4 || desc.format == HioFormatInt16
|| desc.format == HioFormatInt16Vec2
|| desc.format == HioFormatInt16Vec3
|| desc.format == HioFormatInt16Vec4)
depth = 2;
if (desc.format == HioFormatFloat32 || desc.format == HioFormatFloat32Vec2
else if (desc.format == HioFormatFloat32 || desc.format == HioFormatFloat32Vec2
|| desc.format == HioFormatFloat32Vec3
|| desc.format == HioFormatFloat32Vec4 || desc.format == HioFormatUInt32
|| desc.format == HioFormatUInt32Vec2
Expand All @@ -116,6 +129,21 @@ LoadHioTexture2D(const std::string file, const std::string channelsStr,
return HdOSPRayTexture();
}

// ospray does not support float16, convert to float32
if (depth == 2) {
depth = 4;
GfHalf* cdata = (GfHalf*)data;
float* floatData = new float[size.y * size.x * channels];
for (size_t i = 0; i < size.x * size.y; i++) {
for(int j = 0; j < channels; j++) {
int const idx = i * channels + j;
floatData[idx] = cdata[idx];
}
}
delete[] data;
data = (uint8_t*)floatData;
}

const int outChannels
= channelsStr.empty() ? channels : channelsStr.length();
int outDepth = depth;
Expand All @@ -142,6 +170,14 @@ LoadHioTexture2D(const std::string file, const std::string channelsStr,
dataType = OSP_VEC3F;
else if (format == OSP_TEXTURE_RGBA32F)
dataType = OSP_VEC4F;
else if (format == OSP_TEXTURE_R16)
dataType = OSP_HALF;
else if (format == OSP_TEXTURE_RA16)
dataType = OSP_VEC2H;
else if (format == OSP_TEXTURE_RGB16)
dataType = OSP_VEC3H;
else if (format == OSP_TEXTURE_RGBA16)
dataType = OSP_VEC4H;
else if ((format == OSP_TEXTURE_R8) || (format == OSP_TEXTURE_L8))
dataType = OSP_UCHAR;
else if ((format == OSP_TEXTURE_RGB8) || (format == OSP_TEXTURE_SRGB))
Expand All @@ -150,7 +186,7 @@ LoadHioTexture2D(const std::string file, const std::string channelsStr,
dataType = OSP_VEC4UC;
else {
TF_DEBUG_MSG(OSP, "#osp: unknown texture format \"%s\" \"%d\" \"%d\" \"%d\"\n",
file.c_str(), depth, channels, format);
file.c_str(), outDepth, outChannels, format);
return HdOSPRayTexture();
}

Expand Down

0 comments on commit ba61426

Please sign in to comment.