From 4946504cc723443d768429732a52323192fb2c2d Mon Sep 17 00:00:00 2001 From: davidgyu Date: Tue, 17 Dec 2019 10:54:12 -0800 Subject: [PATCH] [HdSt,UsdShaders] fixed UsdPreview material displacement 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) --- pxr/imaging/hdSt/codeGen.cpp | 148 ++++++++++++++---- .../usdShaders/shaders/previewSurface.glslfx | 2 +- 2 files changed, 115 insertions(+), 35 deletions(-) diff --git a/pxr/imaging/hdSt/codeGen.cpp b/pxr/imaging/hdSt/codeGen.cpp index 1cc389cb59..2315412537 100644 --- a/pxr/imaging/hdSt/codeGen.cpp +++ b/pxr/imaging/hdSt/codeGen.cpp @@ -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"; @@ -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"; @@ -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) << "(" @@ -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"; @@ -2793,36 +2849,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_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"; @@ -2830,10 +2899,17 @@ HdSt_CodeGen::_GenerateShaderParameters() 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" @@ -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_" @@ -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; diff --git a/pxr/usdImaging/plugin/usdShaders/shaders/previewSurface.glslfx b/pxr/usdImaging/plugin/usdShaders/shaders/previewSurface.glslfx index 6140f4a81d..502d415b3c 100644 --- a/pxr/usdImaging/plugin/usdShaders/shaders/previewSurface.glslfx +++ b/pxr/usdImaging/plugin/usdShaders/shaders/previewSurface.glslfx @@ -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); }