Skip to content

Commit

Permalink
[HdSt,UsdShaders] fixed UsdPreview material displacement
Browse files Browse the repository at this point in the history
Updated previewSurface displacementShader to sample the
displacement signal distinctly for each vertex emitted from the
geometry shader. In order to do this the displacement shader
needs to forward the emitted-vertex "index" argument to the
HdGet_displacement() method used to sample the signal.

Fixed codeGen to:
- emit several missing HdGet_name(int index) accessors.
- emit a few missing HdGet_textureName(coord) accessors.
- use a preprocessor macro for the implementation of the
  HdGetSampler_textureName() accessors when the
  bindless_texture extension is not enabled.

Fixes #922
Fixes #1026

(Internal change: 2030120)
  • Loading branch information
davidgyu authored and pixar-oss committed Dec 17, 2019
1 parent 5f1b01e commit 4946504
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 35 deletions.
148 changes: 114 additions & 34 deletions pxr/imaging/hdSt/codeGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2678,38 +2678,60 @@ HdSt_CodeGen::_GenerateShaderParameters()

HdBinding::Type bindingType = it->first.GetType();
if (bindingType == HdBinding::FALLBACK) {
// vec4 HdGet_name(int localIndex)
accessors
<< _GetUnpackedType(it->second.dataType, false)
<< " HdGet_" << it->second.name << "() {\n"
<< " HdGet_" << it->second.name << "(int localIndex) {\n"
<< " int shaderCoord = GetDrawingCoord().shaderCoord; \n"
<< " return "
<< _GetPackedTypeAccessor(it->second.dataType, false)
<< "(shaderData[shaderCoord]." << it->second.name << swizzle
<< ");\n"
<< "}\n";

// vec4 HdGet_name()
accessors
<< _GetUnpackedType(it->second.dataType, false)
<< " HdGet_" << it->second.name
<< "() { return HdGet_" << it->second.name << "(0); }\n";

} else if (bindingType == HdBinding::BINDLESS_TEXTURE_2D) {
// a function returning sampler2D is allowed in 430 or later
if (caps.glslVersion >= 430) {
// a function returning sampler requires bindless_texture
if (caps.bindlessTextureEnabled) {
accessors
<< "sampler2D\n"
<< "HdGetSampler_" << it->second.name << "() {\n"
<< " int shaderCoord = GetDrawingCoord().shaderCoord; \n"
<< " return sampler2D(shaderData[shaderCoord]." << it->second.name << ");\n"
<< " }\n";
<< "}\n";
}

// vec4 HdGet_name(vec2 coord)
accessors
<< _GetUnpackedType(it->second.dataType, false)
<< " HdGet_" << it->second.name << "(vec2 coord) {\n"
<< " int shaderCoord = GetDrawingCoord().shaderCoord;\n"
<< " return "
<< _GetPackedTypeAccessor(it->second.dataType, false)
<< "(texture(sampler2D(shaderData[shaderCoord]."
<< it->second.name << "), coord)" << swizzle
<< ");\n}\n";

// vec4 HdGet_name(int localIndex)
accessors
<< _GetUnpackedType(it->second.dataType, false)
<< " HdGet_" << it->second.name << "() {\n"
<< " HdGet_" << it->second.name << "(int localIndex) {\n"
<< " int shaderCoord = GetDrawingCoord().shaderCoord; \n"
<< " return "
<< _GetPackedTypeAccessor(it->second.dataType, false) << "("
<< "texture(sampler2D(shaderData[shaderCoord]." << it->second.name << "), ";

<< "texture(sampler2D(shaderData[shaderCoord]."
<< it->second.name << "), ";
if (!it->second.inPrimvars.empty()) {
accessors
<< "\n"
<< "#if defined(HD_HAS_" << it->second.inPrimvars[0] << ")\n"
<< " HdGet_" << it->second.inPrimvars[0] << "().xy\n"
<< " HdGet_" << it->second.inPrimvars[0]
<< "(localIndex).xy\n"
<< "#else\n"
<< "vec2(0.0, 0.0)\n"
<< "#endif\n";
Expand All @@ -2721,36 +2743,49 @@ HdSt_CodeGen::_GenerateShaderParameters()
accessors
<< ")" << swizzle << ");\n"
<< "}\n";

// vec4 HdGet_name()
accessors
<< _GetUnpackedType(it->second.dataType, false)
<< " HdGet_" << it->second.name
<< "() { return HdGet_" << it->second.name << "(0); }\n";

} else if (bindingType == HdBinding::TEXTURE_2D) {
declarations
<< LayoutQualifier(it->first)
<< "uniform sampler2D sampler2d_" << it->second.name << ";\n";
// a function returning sampler2D is allowed in 430 or later
if (caps.glslVersion >= 430) {
// a function returning sampler requires bindless_texture
if (caps.bindlessTextureEnabled) {
accessors
<< "sampler2D\n"
<< "HdGetSampler_" << it->second.name << "() {\n"
<< " return sampler2d_" << it->second.name << ";"
<< "}\n";
} else {
accessors
<< "#define HdGetSampler_" << it->second.name << "()"
<< " sampler2d_" << it->second.name << "\n";
}
// vec4 HdGet_name(vec2 coord) { return texture(sampler2d_name, coord).xyz; }

// vec4 HdGet_name(vec2 coord)
accessors
<< _GetUnpackedType(it->second.dataType, false)
<< " HdGet_" << it->second.name
<< "(vec2 coord) { return "
<< " HdGet_" << it->second.name << "(vec2 coord) { return "
<< _GetPackedTypeAccessor(it->second.dataType, false)
<< "(texture(sampler2d_" << it->second.name << ", coord)"
<< swizzle << ");}\n";
// vec4 HdGet_name() { return HdGet_name(HdGet_st().xy); }

// vec4 HdGet_name(int localIndex)
accessors
<< _GetUnpackedType(it->second.dataType, false)
<< " HdGet_" << it->second.name
<< "() { return HdGet_" << it->second.name << "(";
<< "(int localIndex) { return HdGet_" << it->second.name << "(";
if (!it->second.inPrimvars.empty()) {
accessors
<< "\n"
<< "#if defined(HD_HAS_" << it->second.inPrimvars[0] << ")\n"
<< "HdGet_" << it->second.inPrimvars[0] << "().xy\n"
<< "HdGet_" << it->second.inPrimvars[0]
<< "(localIndex).xy\n"
<< "#else\n"
<< "vec2(0.0, 0.0)\n"
<< "#endif\n";
Expand All @@ -2759,19 +2794,39 @@ HdSt_CodeGen::_GenerateShaderParameters()
<< "vec2(0.0, 0.0)";
}
accessors << "); }\n";

// vec4 HdGet_name()
accessors
<< _GetUnpackedType(it->second.dataType, false)
<< " HdGet_" << it->second.name
<< "() { return HdGet_" << it->second.name << "(0); }\n";

} else if (bindingType == HdBinding::BINDLESS_TEXTURE_3D) {
// a function returning sampler3D is allowed in 430 or later
if (caps.glslVersion >= 430) {
// a function returning sampler requires bindless_texture
if (caps.bindlessTextureEnabled) {
accessors
<< "sampler3D\n"
<< "HdGetSampler_" << it->second.name << "() {\n"
<< " int shaderCoord = GetDrawingCoord().shaderCoord; \n"
<< " return sampler3D(shaderData[shaderCoord]." << it->second.name << ");\n"
<< " }\n";
<< "}\n";
}

// vec4 HdGet_name(vec3 coord)
accessors
<< _GetUnpackedType(it->second.dataType, false)
<< " HdGet_" << it->second.name << "(vec3 coord) { \n"
<< " int shaderCoord = GetDrawingCoord().shaderCoord; \n"
<< " return "
<< _GetPackedTypeAccessor(it->second.dataType, false) << "("
<< "texture(sampler3D(shaderData[shaderCoord]."
<< it->second.name << "), coord)"
<< swizzle << ");\n}\n";

// vec4 HdGet_name(int localIndex)
accessors
<< _GetUnpackedType(it->second.dataType, false)
<< " HdGet_" << it->second.name << "() {\n"
<< " HdGet_" << it->second.name << "(int localIndex) {\n"
<< " int shaderCoord = GetDrawingCoord().shaderCoord; \n"
<< " return "
<< _GetPackedTypeAccessor(it->second.dataType, false) << "("
Expand All @@ -2781,7 +2836,8 @@ HdSt_CodeGen::_GenerateShaderParameters()
accessors
<< "\n"
<< "#if defined(HD_HAS_" << it->second.inPrimvars[0] << ")\n"
<< " HdGet_" << it->second.inPrimvars[0] << "().xyz\n"
<< " HdGet_" << it->second.inPrimvars[0]
<< "(localIndex).xyz\n"
<< "#else\n"
<< "vec3(0.0, 0.0, 0.0)\n"
<< "#endif\n";
Expand All @@ -2793,47 +2849,67 @@ HdSt_CodeGen::_GenerateShaderParameters()
accessors
<< ")" << swizzle << ");\n"
<< "}\n";

// vec4 HdGet_name()
accessors
<< _GetUnpackedType(it->second.dataType, false)
<< " HdGet_" << it->second.name
<< "() { return HdGet_" << it->second.name << "(0); }\n";

} else if (bindingType == HdBinding::TEXTURE_3D) {
declarations
<< LayoutQualifier(it->first)
<< "uniform sampler3D sampler3d_" << it->second.name << ";\n";
// a function returning sampler3D is allowed in 430 or later
if (caps.glslVersion >= 430) {
// a function returning sampler requires bindless_texture
if (caps.bindlessTextureEnabled) {
accessors
<< "sampler3D\n"
<< "HdGetSampler_" << it->second.name << "() {\n"
<< " return sampler3d_" << it->second.name << ";"
<< "}\n";
} else {
accessors
<< "#define HdGetSampler_" << it->second.name << "()"
<< " sampler3d_" << it->second.name << "\n";
}
// vec4 HdGet_name(vec3 coord) { return texture(sampler3d_name, coord).xyz; }

// vec4 HdGet_name(vec3 coord)
accessors
<< _GetUnpackedType(it->second.dataType, false)
<< " HdGet_" << it->second.name
<< "(vec3 coord) { return "
<< " HdGet_" << it->second.name << "(vec3 coord) { return "
<< _GetPackedTypeAccessor(it->second.dataType, false)
<< "(texture(sampler3d_" << it->second.name << ", coord)"
<< swizzle << ");}\n";
// vec4 HdGet_name() { return HdGet_name(HdGet_st().xyz); }

// vec4 HdGet_name(int localIndex)
accessors
<< _GetUnpackedType(it->second.dataType, false)
<< " HdGet_" << it->second.name
<< "() { return HdGet_" << it->second.name << "(";
<< "(int localIndex) { return HdGet_" << it->second.name << "(";
if (!it->second.inPrimvars.empty()) {
accessors
<< "\n"
<< "#if defined(HD_HAS_" << it->second.inPrimvars[0] << ")\n"
<< "HdGet_" << it->second.inPrimvars[0] << "().xyz\n"
<< "HdGet_" << it->second.inPrimvars[0]
<< "(localIndex).xyz\n"
<< "#else\n"
<< "vec3(0.0, 0.0, 0.0)\n"
<< "#endif\n";
} else {
accessors
<< "vec3(0.0, 0.0, 0.0)";
}
accessors << "); }\n";
accessors << ");\n}\n";

// vec4 HdGet_name()
accessors
<< _GetUnpackedType(it->second.dataType, false)
<< " HdGet_" << it->second.name
<< "() { return HdGet_" << it->second.name << "(0); }\n";

} else if (bindingType == HdBinding::BINDLESS_TEXTURE_UDIM_ARRAY) {
// a function returning sampler2DArray is allowed in 430 or later
if (caps.glslVersion >= 430) {
// a function returning sampler requires bindless_texture
if (caps.bindlessTextureEnabled) {
accessors
<< "sampler2DArray\n"
<< "HdGetSampler_" << it->second.name << "() {\n"
Expand All @@ -2846,7 +2922,6 @@ HdSt_CodeGen::_GenerateShaderParameters()
<< it->second.dataType
<< " HdGet_" << it->second.name << "()" << " {\n"
<< " int shaderCoord = GetDrawingCoord().shaderCoord;\n";

if (!it->second.inPrimvars.empty()) {
accessors
<< "#if defined(HD_HAS_"
Expand All @@ -2873,12 +2948,17 @@ HdSt_CodeGen::_GenerateShaderParameters()
<< "uniform sampler2DArray sampler2dArray_"
<< it->second.name << ";\n";

if (caps.glslVersion >= 430) {
// a function returning sampler requires bindless_texture
if (caps.bindlessTextureEnabled) {
accessors
<< "sampler2DArray\n"
<< "HdGetSampler_" << it->second.name << "() {\n"
<< " return sampler2dArray_" << it->second.name << ";"
<< "}\n";
} else {
accessors
<< "#define HdGetSampler_" << it->second.name << "()"
<< " sampler2dArray_" << it->second.name << "\n";
}
// vec4 HdGet_name(vec2 coord) { vec3 c = hd_sample_udim(coord);
// c.z = texelFetch(sampler1d_name_layout, int(c.z), 0).x - 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ vec4
displacementShader(int index, vec4 Peye, vec3 Neye, vec4 patchCoord)
{
// Calculate scalar displacement.
float texDisplacement = HdGet_displacement().x;
float texDisplacement = HdGet_displacement(index).x;
return Peye + vec4(Neye*texDisplacement, 0);
}

Expand Down

0 comments on commit 4946504

Please sign in to comment.