Skip to content

Commit

Permalink
--PBR Primitive support and gl context bugfix (#2235)
Browse files Browse the repository at this point in the history
* --add initial vertex color support for PBR
* --expand default material to cover PBR values
* --add vertex color support to PBR shader; render Trajectories in PBR
also a bug in the trajectory generation that was introduced when I converted everything to Magnum materials is now fixed.
* --set primitive, trajectory, and default materials to be phong or pbr via config
TODO : add support for reading config value for fallback shader to use. Defaults to Pbr now.
* --initial material-based prim keys
* --make a default metallic material
* --fix attempting to synthesize missing IBL images when no context exists
  • Loading branch information
jturner65 authored Oct 10, 2023
1 parent b3c0552 commit 8b016d7
Show file tree
Hide file tree
Showing 14 changed files with 293 additions and 127 deletions.
211 changes: 129 additions & 82 deletions src/esp/assets/ResourceManager.cpp

Large diffs are not rendered by default.

32 changes: 25 additions & 7 deletions src/esp/assets/ResourceManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -699,20 +699,33 @@ class ResourceManager {
bool forceFlatShading);

/**
* @brief Build a primitive asset based on passed template parameters. If
* exists already, does nothing. Will use primitiveImporter_ to call
* appropriate method to construct asset.
* @brief Build a primitive asset based on the template parameters encoded in
* @p primTemplateHandle , using the predefined material referenced by
* DEFAULT_MATERIAL_KEY. If primitive asset exists already, does nothing. Will
* use primitiveImporter_ to call appropriate method to construct asset.
* @param primTemplateHandle the handle referring to the attributes describing
* primitive to instantiate
*/
void buildPrimitiveAssetData(const std::string& primTemplateHandle);

/**
* @brief this will build a Phong @ref Magnum::Trade::MaterialData using
* default attributes from deprecated/removed esp::gfx::PhongMaterialData.
* @return The new phong color populated with default values
* @brief Build a primitive asset based on passed template parameters and
* passed material key. If exists already, does nothing. Will use
* primitiveImporter_ to call appropriate method to construct asset.
* @param primTemplateHandle the handle referring to the attributes describing
* primitive to instantiate
* @param materialKey The key to the existing material being used for this
* primitive.
*/
void buildPrimitiveAssetData(const std::string& primTemplateHandle,
const std::string& materialKey);
/**
* @brief this will build a MaterialData compatible with Flat, Phong and
* PBR @ref Magnum::Trade::MaterialData, using default attributes
* from deprecated/removed habitat material default values.
* @return The new material populated with default values
*/
Mn::Trade::MaterialData buildDefaultPhongMaterial();
Mn::Trade::MaterialData buildDefaultMaterial();

/**
* @brief Define and set user-defined attributes for the passed
Expand Down Expand Up @@ -1116,6 +1129,11 @@ class ResourceManager {
*/
void initDefaultMaterials();

/**
* @brief Retrieve the shader type to use for the various default materials.
*/
ObjectInstanceShaderType getDefaultMaterialShaderType();

/**
* @brief Checks if light setup is compatible with loaded asset
*/
Expand Down
8 changes: 7 additions & 1 deletion src/esp/core/Esp.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,16 @@ constexpr char DEFAULT_MATERIAL_KEY[] = "";

/**
*@brief The @ref esp::gfx::ShaderManager key for full ambient white @ref
*esp::gfx::MaterialInfo used for primitive wire-meshes
*esp::gfx::MaterialInfo
*/
constexpr char WHITE_MATERIAL_KEY[] = "ambient_white";

/**
*@brief The @ref esp::gfx::ShaderManager key for shiny metallic @ref
*esp::gfx::MaterialInfo
*/
constexpr char METALLIC_MATERIAL_KEY[] = "metallic_gray";

/**
*@brief The @ref ShaderManager key for @ref MaterialInfo with per-vertex
* object ID
Expand Down
6 changes: 6 additions & 0 deletions src/esp/gfx/PbrDrawable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,12 @@ void PbrDrawable::setMaterialValuesInternal(
matCache.volumeLayer.attenuationColor = *attenuationColor;
}
} // has KHR_materials_volume layer

// Vertex colors (for synth assets)
if (meshAttributeFlags_ & Drawable::Flag::HasVertexColor) {
flags_ |= PbrShader::Flag::VertexColor;
}

// If not reset then make sure the same shader is used
if (!reset) {
flags_ = oldFlags;
Expand Down
7 changes: 7 additions & 0 deletions src/esp/gfx/PbrShader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ PbrShader::PbrShader(Flags originalFlags,
attributeLocationsStream << Cr::Utility::formatString(
"#define ATTRIBUTE_LOCATION_TANGENT4 {}\n", Tangent4::Location);
}
if (flags_ >= Flag::VertexColor) {
attributeLocationsStream << Cr::Utility::formatString(
"#define ATTRIBUTE_LOCATION_COLOR {}\n", Color4::Location);
}

// TODO: Occlusion texture to be added.

if (isTextured_) {
Expand All @@ -126,6 +131,7 @@ PbrShader::PbrShader(Flags originalFlags,
.addSource(isTextured_ && (flags_ >= Flag::TextureTransformation)
? "#define TEXTURE_TRANSFORMATION\n"
: "")
.addSource(flags_ >= Flag::VertexColor ? "#define VERTEX_COLOR\n" : "")
.addSource(rs.getString("pbr.vert"));

std::stringstream outputAttributeLocationsStream;
Expand All @@ -146,6 +152,7 @@ PbrShader::PbrShader(Flags originalFlags,
.addSource(flags_ >= Flag::NormalTexture ? "#define NORMAL_TEXTURE\n"
: "")
.addSource(flags_ >= Flag::ObjectId ? "#define OBJECT_ID\n" : "")
.addSource(flags_ >= Flag::VertexColor ? "#define VERTEX_COLOR\n" : "")

// Clearcoat layer
.addSource(flags_ >= Flag::ClearCoatLayer ? "#define CLEAR_COAT\n" : "")
Expand Down
76 changes: 42 additions & 34 deletions src/esp/gfx/PbrShader.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ class PbrShader : public Magnum::GL::AbstractShaderProgram {
*/
typedef Magnum::Shaders::GenericGL3D::Tangent4 Tangent4;

/**
* @brief Four-component vertex color
*/
typedef Magnum::Shaders::GenericGL3D::Color4 Color4;

enum : Magnum::UnsignedInt {
/**
* Color shader output. @ref shaders-generic "Generic output",
Expand Down Expand Up @@ -108,6 +113,11 @@ class PbrShader : public Magnum::GL::AbstractShaderProgram {
*/
EmissiveTexture = 1ULL << 4,

/**
* Support mesh vertex colors
*/
VertexColor = 1ULL << 5,

/**
* Enable texture coordinate transformation. If this flag is set,
* the shader expects that at least one of
Expand All @@ -118,7 +128,7 @@ class PbrShader : public Magnum::GL::AbstractShaderProgram {
* @ref Flag::OcclusionRoughnessMetallicTexture is enabled as well.
* @see @ref setTextureMatrix()
*/
TextureTransformation = 1ULL << 5,
TextureTransformation = 1ULL << 6,

/**
* TODO: Do we need instanced object? (instanced texture, instanced id etc.)
Expand All @@ -136,86 +146,84 @@ class PbrShader : public Magnum::GL::AbstractShaderProgram {
* see PBR fragment shader code for more details
* Requires the @ref Tangent4 attribute to be present.
*/
PrecomputedTangent = 1ULL << 6,
PrecomputedTangent = 1ULL << 7,

/**
* Enable object ID output for this shader.
*/
ObjectId = 1ULL << 7,
ObjectId = 1ULL << 8,

/**
* Support Instanced object ID. Retrieves a per-instance / per-vertex
* object ID from the @ref ObjectId attribute. If this is false, the shader
* will use the node's semantic ID
*/
InstancedObjectId = (1ULL << 8) | ObjectId,
InstancedObjectId = (1ULL << 9) | ObjectId,

/**
* Has ClearCoat layer.
*/
ClearCoatLayer = 1ULL << 9,
ClearCoatLayer = 1ULL << 10,
/**
* Has ClearCoat Texture in ClearCoat layer
*/
ClearCoatTexture = (1ULL << 10) | ClearCoatLayer,
ClearCoatTexture = (1ULL << 11) | ClearCoatLayer,
/**
* Has Roughness Texture in ClearCoat layer
*/
ClearCoatRoughnessTexture = (1ULL << 11) | ClearCoatLayer,
ClearCoatRoughnessTexture = (1ULL << 12) | ClearCoatLayer,
/**
* Has Normal Texture in ClearCoat layer
*/
ClearCoatNormalTexture = (1ULL << 12) | ClearCoatLayer,
ClearCoatNormalTexture = (1ULL << 13) | ClearCoatLayer,

/**
* Has KHR_materials_specular layer
*/
SpecularLayer = 1ULL << 13,
SpecularLayer = 1ULL << 14,
/**
* Has Specular Texture in KHR_materials_specular layer
*/
SpecularLayerTexture = (1ULL << 14) | SpecularLayer,
SpecularLayerTexture = (1ULL << 15) | SpecularLayer,

/**
* Has Specular Color Texture in KHR_materials_specular layer
*/
SpecularLayerColorTexture = (1ULL << 15) | SpecularLayer,
SpecularLayerColorTexture = (1ULL << 16) | SpecularLayer,

/**
* Has KHR_materials_anisotropy layer
*/
AnisotropyLayer = 1ULL << 16,
AnisotropyLayer = 1ULL << 17,

/**
* Has Anisotropy Texture in KHR_materials_anisotropy layer
*/
AnisotropyLayerTexture = (1ULL << 17) | AnisotropyLayer,
AnisotropyLayerTexture = (1ULL << 18) | AnisotropyLayer,

/**
* Has KHR_materials_transmission layer
*/
TransmissionLayer = 1ULL << 18,
TransmissionLayer = 1ULL << 19,
/**
* Has transmission texture in KHR_materials_transmission layer
*/
TransmissionLayerTexture = (1ULL << 19) | TransmissionLayer,
TransmissionLayerTexture = (1ULL << 20) | TransmissionLayer,

/**
* Has KHR_materials_volume layer
*/
VolumeLayer = 1ULL << 20,
VolumeLayer = 1ULL << 21,

/**
* Has Thickness texture in KHR_materials_volume layer
*/
VolumeLayerThicknessTexture = (1ULL << 21) | VolumeLayer,
VolumeLayerThicknessTexture = (1ULL << 22) | VolumeLayer,

/**
* Enable double-sided rendering.
* (Temporarily STOP supporting this functionality. See comments in
* the PbrDrawable::draw() function)
*/
DoubleSided = 1ULL << 22,
DoubleSided = 1ULL << 23,

///////////////////////////////
// PbrShaderAttributes provides these values to configure the shader
Expand All @@ -224,12 +232,12 @@ class PbrShader : public Magnum::GL::AbstractShaderProgram {
* If not set, disable direct lighting regardless of presence of lights.
* Ignored if no direct lights present.
*/
DirectLighting = 1ULL << 23,
DirectLighting = 1ULL << 24,

/**
* Enable image based lighting
*/
ImageBasedLighting = 1ULL << 24,
ImageBasedLighting = 1ULL << 25,

/**
* Whether or not the direct lighting diffuse calculation should use the
Expand All @@ -238,15 +246,15 @@ class PbrShader : public Magnum::GL::AbstractShaderProgram {
* https://media.disneyanimation.com/uploads/production/publication_asset/48/asset/s2012_pbs_disney_brdf_notes_v3.pdf
* Lambertian is simpler and quicker to calculate but may not look as 'nice'
*/
UseBurleyDiffuse = 1ULL << 25,
UseBurleyDiffuse = 1ULL << 26,

/**
* If set, skip TBN frame calculation in fragment shader. This calculation
* enables normal textures and anisotropy when no precomputed tangents are
* provided.
* TODO : implement in shader.
*/
SkipMissingTBNCalc = 1ULL << 26,
SkipMissingTBNCalc = 1ULL << 27,

/**
* Use the Mikkelsen algorithm to calculate TBN, as per
Expand All @@ -256,41 +264,41 @@ class PbrShader : public Magnum::GL::AbstractShaderProgram {
* https://github.com/KhronosGroup/Vulkan-Samples/blob/main/shaders/pbr.frag,
* which empirically seems to give equivalent results.
*/
UseMikkelsenTBN = 1ULL << 27,
UseMikkelsenTBN = 1ULL << 28,

/**
* Whether we should use shader-based srgb->linear approx remapping of
* applicable material color textures in PBR rendering for direct lighting
* and IBL. This field should be removed/ignored when Magnum fully supports
* sRGB texture conversion on load.
*/
MapMatTxtrToLinear = 1ULL << 28,
MapMatTxtrToLinear = 1ULL << 29,

/**
* Whether we should use shader-based srgb->linear approx remapping of
* applicable IBL environment textures in PBR rendering for IBL
* calculations. This field should be removed/ignored when Magnum fully
* supports sRGB texture conversion on load.
*/
MapIBLTxtrToLinear = 1ULL << 29,
MapIBLTxtrToLinear = 1ULL << 30,

/**
* Whether we should use shader-based linear->srgb approx remapping of
* color output in PBR rendering for direct lighting and IBL results. This
* field should be removed/ignored when an appropriate framebuffer is used
* for output to handle this conversion.
*/
MapOutputToSRGB = 1ULL << 30,
MapOutputToSRGB = 1ULL << 31,

/**
* Whether or not to use tonemappping for direct lighting.
*/
UseDirectLightTonemap = 1ULL << 31,
UseDirectLightTonemap = 1ULL << 32,

/**
* Whether or not to use tonemappping for image-based lighting.
*/
UseIBLTonemap = 1ULL << 32,
UseIBLTonemap = 1ULL << 33,

////////////////
// Testing and debugging
Expand All @@ -299,25 +307,25 @@ class PbrShader : public Magnum::GL::AbstractShaderProgram {
* sent to the shader, but no actual calcuations will be performed if this
* is set.
*/
SkipClearCoatLayer = 1ULL << 33,
SkipClearCoatLayer = 1ULL << 34,
/**
* Whether we should skip all specular layer calcs. Values will still be
* sent to the shader, but no actual calcuations will be performed if this
* is set.
*/
SkipSpecularLayer = 1ULL << 34,
SkipSpecularLayer = 1ULL << 35,
/**
* Whether we should skip all anisotropy layer calcs. Values will still be
* sent to the shader, but no actual calcuations will be performed if this
* is set.
*/
SkipAnisotropyLayer = 1ULL << 35,
SkipAnisotropyLayer = 1ULL << 36,

/**
* Enable shader debug mode. Then developer can set the uniform
* PbrDebugDisplay in the fragment shader for debugging
*/
DebugDisplay = 1ULL << 36,
DebugDisplay = 1ULL << 37,
/*
* TODO: alphaMask
*/
Expand Down
7 changes: 7 additions & 0 deletions src/esp/metadata/MetadataMediator.h
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,13 @@ class MetadataMediator {
currDefaultPbrAttributesHandle_);
}

/**
* @brief Retrieve the shader type to use for the various default materials.
*/
attributes::ObjectInstanceShaderType getDefaultMaterialShaderType() {
return getActiveDSAttribs()->getDefaultMaterialShaderType();
}

/**
* @brief Get a list of all scene instances available in the currently active
* dataset
Expand Down
2 changes: 2 additions & 0 deletions src/esp/metadata/attributes/SceneDatasetAttributes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ SceneDatasetAttributes::SceneDatasetAttributes(
stageAttributesManager_ =
managers::StageAttributesManager::create(physAttrMgr);
stageAttributesManager_->setAssetAttributesManager(assetAttributesManager_);
// Use PBR as default materials.Override this in SceneDataset config
setDefaultMaterialIsPBR(true);
} // ctor

bool SceneDatasetAttributes::addNewSceneInstanceToDataset(
Expand Down
Loading

0 comments on commit 8b016d7

Please sign in to comment.