Skip to content

Commit

Permalink
Merge pull request #1692 from Autodesk/t_gamaj/handle_fallback_value_…
Browse files Browse the repository at this point in the history
…in_vp2

Properly use fallback color in vp2RenderDelegate
  • Loading branch information
Krystian Ligenza authored Sep 10, 2021
2 parents a8ff108 + ecef6de commit ff51e81
Show file tree
Hide file tree
Showing 5 changed files with 281 additions and 7 deletions.
52 changes: 46 additions & 6 deletions lib/mayaUsd/render/vp2RenderDelegate/material.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ TF_DEFINE_PRIVATE_TOKENS(
(sRGB)
(raw)
(glslfx)
(fallback)

(input)
(output)
Expand Down Expand Up @@ -681,6 +682,16 @@ MHWRender::MSamplerStateDesc _GetSamplerStateDesc(const HdMaterialNode& node)
#endif
}

it = node.parameters.find(_tokens->fallback);
if (it != node.parameters.end()) {
const VtValue& value = it->second;
if (value.IsHolding<GfVec4f>()) {
const GfVec4f& fallbackValue = value.UncheckedGet<GfVec4f>();
float const* value = fallbackValue.data();
std::copy(value, value + 4, desc.borderColor);
}
}

return desc;
}

Expand Down Expand Up @@ -815,8 +826,11 @@ _LoadUdimTexture(const std::string& path, bool& isColorSpaceSRGB, MFloatArray& u
}

//! Load texture from the specified path
MHWRender::MTexture*
_LoadTexture(const std::string& path, bool& isColorSpaceSRGB, MFloatArray& uvScaleOffset)
MHWRender::MTexture* _LoadTexture(
const std::string& path,
bool& isColorSpaceSRGB,
MFloatArray& uvScaleOffset,
const HdMaterialNode& node)
{
MProfilingScope profilingScope(
HdVP2RenderDelegate::sProfilerCategory, MProfiler::kColorD_L2, "LoadTexture", path.c_str());
Expand Down Expand Up @@ -844,7 +858,32 @@ _LoadTexture(const std::string& path, bool& isColorSpaceSRGB, MFloatArray& uvSca
#endif

if (!TF_VERIFY(image, "Unable to create an image from %s", path.c_str())) {
return nullptr;
// Create a 1x1 texture of the fallback color, if it was specified:
auto it = node.parameters.find(_tokens->fallback);
if (it == node.parameters.end()) {
return nullptr;
}
const VtValue& value = it->second;
if (!value.IsHolding<GfVec4f>()) {
return nullptr;
}

MHWRender::MTextureDescription desc;
desc.setToDefault2DTexture();
desc.fWidth = 1;
desc.fHeight = 1;
desc.fFormat = MHWRender::kR8G8B8A8_UNORM;
desc.fBytesPerRow = 4;
desc.fBytesPerSlice = desc.fBytesPerRow;

const GfVec4f& fallbackValue = value.UncheckedGet<GfVec4f>();
std::vector<unsigned char> texels(4);
for (size_t i = 0; i < 4; ++i) {
float texelValue = std::max(std::min(fallbackValue[i], 1.0f), 0.0f);
texels[i] = static_cast<unsigned char>(texelValue * 255.0);
}
isColorSpaceSRGB = false;
return textureMgr->acquireTexture(path.c_str(), desc, texels.data());
}

// This image is used for loading pixel data from usdz only and should
Expand Down Expand Up @@ -2168,7 +2207,7 @@ void HdVP2Material::_UpdateShaderInstance(const HdMaterialNetwork& mat)
const std::string& assetPath = val.GetAssetPath();
if (_IsUsdUVTexture(node) && token == _tokens->file) {
const HdVP2TextureInfo& info
= _AcquireTexture(!resolvedPath.empty() ? resolvedPath : assetPath);
= _AcquireTexture(!resolvedPath.empty() ? resolvedPath : assetPath, node);

MHWRender::MTextureAssignment assignment;
assignment.texture = info._texture.get();
Expand Down Expand Up @@ -2262,7 +2301,8 @@ void HdVP2Material::_UpdateShaderInstance(const HdMaterialNetwork& mat)

/*! \brief Acquires a texture for the given image path.
*/
const HdVP2TextureInfo& HdVP2Material::_AcquireTexture(const std::string& path)
const HdVP2TextureInfo&
HdVP2Material::_AcquireTexture(const std::string& path, const HdMaterialNode& node)
{
const auto it = _textureMap.find(path);
if (it != _textureMap.end()) {
Expand All @@ -2271,7 +2311,7 @@ const HdVP2TextureInfo& HdVP2Material::_AcquireTexture(const std::string& path)

bool isSRGB = false;
MFloatArray uvScaleOffset;
MHWRender::MTexture* texture = _LoadTexture(path, isSRGB, uvScaleOffset);
MHWRender::MTexture* texture = _LoadTexture(path, isSRGB, uvScaleOffset, node);

HdVP2TextureInfo& info = _textureMap[path];
info._texture.reset(texture);
Expand Down
2 changes: 1 addition & 1 deletion lib/mayaUsd/render/vp2RenderDelegate/material.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ class HdVP2Material final : public HdMaterial
#endif
MHWRender::MShaderInstance* _CreateShaderInstance(const HdMaterialNetwork& mat);
void _UpdateShaderInstance(const HdMaterialNetwork& mat);
const HdVP2TextureInfo& _AcquireTexture(const std::string& path);
const HdVP2TextureInfo& _AcquireTexture(const std::string& path, const HdMaterialNode& node);

#ifdef HDVP2_MATERIAL_CONSOLIDATION_UPDATE_WORKAROUND
//! Trigger sync on all Rprims which are listening to changes on this material.
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,18 @@ def testMetallicF0(self):

self.assertSnapshotClose('F0_is_base_{}.png'.format(light_api))

def testFallbackColor(self):
cmds.file(force=True, new=True)
mayaUtils.loadPlugin("mayaUsdPlugin")

cmds.xform("persp", t=(0, 0, 5))
cmds.xform("persp", ro=[0, 0, 0], ws=True)

testFile = testUtils.getTestScene("UsdPreviewSurface", "TestFallbackColor.usda")
mayaUtils.createProxyFromFile(testFile)

self.assertSnapshotClose('TestFallbackColor.png')


if __name__ == '__main__':
fixturesUtils.runTests(globals())
222 changes: 222 additions & 0 deletions test/testSamples/UsdPreviewSurface/TestFallbackColor.usda
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
#usda 1.0
(
defaultPrim = "pPlane1"
endTimeCode = 1
framesPerSecond = 24
metersPerUnit = 0.01
startTimeCode = 1
timeCodesPerSecond = 24
upAxis = "Y"
)

def Mesh "pPlane1" (
prepend apiSchemas = ["MaterialBindingAPI"]
kind = "component"
)
{
uniform bool doubleSided = 1
float3[] extent = [(-0.5, 0, -0.5), (0.5, 0, 0.5)]
int[] faceVertexCounts = [4]
int[] faceVertexIndices = [0, 1, 3, 2]
rel material:binding = </pPlane1/Looks/usdPreviewSurface1SG>
point3f[] points = [(-0.5, 0, 0.5), (0.5, 0, 0.5), (-0.5, 0, -0.5), (0.5, 0, -0.5)]
texCoord2f[] primvars:st = [(0, 0), (1, 0), (0, 1), (1, 1)] (
customData = {
dictionary Maya = {
token name = "map1"
}
}
interpolation = "faceVarying"
)
int[] primvars:st:indices = [0, 1, 3, 2]
float3 xformOp:rotateXYZ = (90, 0, 0)
double3 xformOp:translate = (-1.5, 0, 0)
uniform token[] xformOpOrder = ["xformOp:translate", "xformOp:rotateXYZ"]

def Scope "Looks"
{
def Material "usdPreviewSurface1SG"
{
token inputs:file1:varname = "st"
token outputs:surface.connect = </pPlane1/Looks/usdPreviewSurface1SG/usdPreviewSurface1.outputs:surface>

def Shader "usdPreviewSurface1"
{
uniform token info:id = "UsdPreviewSurface"
color3f inputs:diffuseColor.connect = </pPlane1/Looks/usdPreviewSurface1SG/file1.outputs:rgb>
token outputs:displacement
token outputs:surface
}

def Shader "file1"
{
uniform token info:id = "UsdUVTexture"
float4 inputs:fallback = (0, 1, 0, 1)
asset inputs:file = @grid.png@
token inputs:sourceColorSpace = "sRGB"
float2 inputs:st.connect = </pPlane1/Looks/usdPreviewSurface1SG/file1/UsdTransform2d.outputs:result>
token inputs:wrapS = "black"
token inputs:wrapT = "black"
float3 outputs:rgb

def Shader "TexCoordReader"
{
uniform token info:id = "UsdPrimvarReader_float2"
token inputs:varname.connect = </pPlane1/Looks/usdPreviewSurface1SG.inputs:file1:varname>
float2 outputs:result
}

def Shader "UsdTransform2d"
{
uniform token info:id = "UsdTransform2d"
float2 inputs:in.connect = </pPlane1/Looks/usdPreviewSurface1SG/file1/TexCoordReader.outputs:result>
float inputs:rotation = 45
float2 inputs:scale = (1, 1)
float2 inputs:translation = (0.49999997, -0.20710683)
float2 outputs:result
}
}
}
}
}

def Mesh "pPlane2" (
prepend apiSchemas = ["MaterialBindingAPI"]
kind = "component"
)
{
uniform bool doubleSided = 1
float3[] extent = [(-0.5, 0, -0.5), (0.5, 0, 0.5)]
int[] faceVertexCounts = [4]
int[] faceVertexIndices = [0, 1, 3, 2]
rel material:binding = </pPlane2/Looks/usdPreviewSurface3SG>
point3f[] points = [(-0.5, 0, 0.5), (0.5, 0, 0.5), (-0.5, 0, -0.5), (0.5, 0, -0.5)]
texCoord2f[] primvars:st = [(0, 0), (1, 0), (0, 1), (1, 1)] (
customData = {
dictionary Maya = {
token name = "map1"
}
}
interpolation = "faceVarying"
)
int[] primvars:st:indices = [0, 1, 3, 2]
float3 xformOp:rotateXYZ = (90, 0, 0)
uniform token[] xformOpOrder = ["xformOp:rotateXYZ"]

def Scope "Looks"
{
def Material "usdPreviewSurface3SG"
{
token inputs:file3:varname = "st"
token outputs:surface.connect = </pPlane2/Looks/usdPreviewSurface3SG/usdPreviewSurface3.outputs:surface>

def Shader "usdPreviewSurface3"
{
uniform token info:id = "UsdPreviewSurface"
color3f inputs:diffuseColor.connect = </pPlane2/Looks/usdPreviewSurface3SG/file3.outputs:rgb>
token outputs:displacement
token outputs:surface
}

def Shader "file3"
{
uniform token info:id = "UsdUVTexture"
float4 inputs:fallback = (1, 1, 0, 1)
asset inputs:file = @does_not_exist.png@
token inputs:sourceColorSpace = "sRGB"
float2 inputs:st.connect = </pPlane2/Looks/usdPreviewSurface3SG/file3/UsdTransform2d.outputs:result>
token inputs:wrapS = "black"
token inputs:wrapT = "black"
float3 outputs:rgb

def Shader "TexCoordReader"
{
uniform token info:id = "UsdPrimvarReader_float2"
token inputs:varname.connect = </pPlane2/Looks/usdPreviewSurface3SG.inputs:file3:varname>
float2 outputs:result
}

def Shader "UsdTransform2d"
{
uniform token info:id = "UsdTransform2d"
float2 inputs:in.connect = </pPlane2/Looks/usdPreviewSurface3SG/file3/TexCoordReader.outputs:result>
float inputs:rotation = 45
float2 inputs:scale = (1, 1)
float2 inputs:translation = (0.49999997, -0.20710683)
float2 outputs:result
}
}
}
}
}

def Mesh "pPlane3" (
prepend apiSchemas = ["MaterialBindingAPI"]
kind = "component"
)
{
uniform bool doubleSided = 1
float3[] extent = [(-0.5, 0, -0.5), (0.5, 0, 0.5)]
int[] faceVertexCounts = [4]
int[] faceVertexIndices = [0, 1, 3, 2]
rel material:binding = </pPlane3/Looks/usdPreviewSurface4SG>
point3f[] points = [(-0.5, 0, 0.5), (0.5, 0, 0.5), (-0.5, 0, -0.5), (0.5, 0, -0.5)]
texCoord2f[] primvars:st = [(0, 0), (1, 0), (0, 1), (1, 1)] (
customData = {
dictionary Maya = {
token name = "map1"
}
}
interpolation = "faceVarying"
)
int[] primvars:st:indices = [0, 1, 3, 2]
float3 xformOp:rotateXYZ = (90, 0, 0)
double3 xformOp:translate = (1.5, 0, 0)
uniform token[] xformOpOrder = ["xformOp:translate", "xformOp:rotateXYZ"]

def Scope "Looks"
{
def Material "usdPreviewSurface4SG"
{
token inputs:file4:varname = "st"
token outputs:surface.connect = </pPlane3/Looks/usdPreviewSurface4SG/usdPreviewSurface4.outputs:surface>

def Shader "usdPreviewSurface4"
{
uniform token info:id = "UsdPreviewSurface"
color3f inputs:diffuseColor.connect = </pPlane3/Looks/usdPreviewSurface4SG/file4.outputs:rgb>
token outputs:displacement
token outputs:surface
}

def Shader "file4"
{
uniform token info:id = "UsdUVTexture"
asset inputs:file = @neither_does_this_one.png@
token inputs:sourceColorSpace = "sRGB"
float2 inputs:st.connect = </pPlane3/Looks/usdPreviewSurface4SG/file4/UsdTransform2d.outputs:result>
token inputs:wrapS = "black"
token inputs:wrapT = "black"
float3 outputs:rgb

def Shader "TexCoordReader"
{
uniform token info:id = "UsdPrimvarReader_float2"
token inputs:varname.connect = </pPlane3/Looks/usdPreviewSurface4SG.inputs:file4:varname>
float2 outputs:result
}

def Shader "UsdTransform2d"
{
uniform token info:id = "UsdTransform2d"
float2 inputs:in.connect = </pPlane3/Looks/usdPreviewSurface4SG/file4/TexCoordReader.outputs:result>
float inputs:rotation = 45
float2 inputs:scale = (1, 1)
float2 inputs:translation = (0.49999997, -0.20710683)
float2 outputs:result
}
}
}
}
}

0 comments on commit ff51e81

Please sign in to comment.