40 PBREffect
Chuck Walbourn редактировал(а) эту страницу 2022-08-14 23:48:02 -07:00
DirectXTK Effects

PBREffect implements a Disney-style (Roughness/Metalness workflow) Physically-Based Renderer (PBR) effect using Image-Based Lighting (IBL) in combination with up to three directional lights. This effect also supports GPU instancing.

SkinnedPBREffect extends PBREffect to support vertex skinning. The skinned effect does not support velocity buffer generation or GPU instancing.

See also PBREffectFactory

Related tutorials: Physically-based rendering, Multistream rendering and instancing

PBR effect

classDiagram
class IEffect{
    <<Interface>>
    +Apply()
    +GetVertexShaderBytecode()
}
class IEffectMatrices{
    <<Interface>>
    +SetWorld()
    +SetView()
    +SetProjection()
    +SetMatrices()
}
class IEffectLights{
    <<Interface>>
    +SetLightEnabled()
    +SetLightDirection()
    +SetLightDiffuseColor()
    +EnableDefaultLighting()
}
class PBREffect{
    +SetAlpha()
    +SetConstantAlbedo()
    +SetConstantMetallic()
    +SetConstantRoughness()
    +SetAlbedoTexture()
    +SetNormalTexture()
    +SetRMATexture()
    +SetEmissiveTexture()
    +SetSurfaceTextures()
    +SetIBLTextures()
    +SetBiasedVertexNormals()
    +SetInstancingEnabled()
    +SetVelocityGeneration()
    +SetRenderTargetSizeInPixels()
}
PBREffect--|> IEffect
PBREffect--|> IEffectMatrices
PBREffect--|> IEffectLights
class IEffectSkinning{
    <<Interface>>
    +SetWeightsPerVertex()
    +SetBoneTransforms()
    +ResetBoneTransforms()
}
class SkinnedPBREffect
PBREffect <|-- SkinnedPBREffect
SkinnedPBREffect --|> IEffectSkinning

Header

#include <Effects.h>

Initialization

Construction requires a Direct3D 11 device.

std::unique_ptr<PBREffect> effect;
effect = std::make_unique<PBREffect>(device);
std::unique_ptr<SkinnedPBREffect> effect;
effect = std::make_unique<SkinnedPBREffect>(device);

For exception safety, it is recommended you make use of the C++ RAII pattern and use a std::unique_ptr or std::shared_ptr

Interfaces

PBREffect supports IEffect, IEffectMatrices, and IEffectLights.

SkinnedPBREffect also supports IEffectSkinning.

Fog settings are not supported by these effects.

Input layout

These effects require SV_Position, NORMAL, and TEXCOORD0. It does not support per-vertex color.

If instancing is enabled, PBREffect also requires these vertex elements:

"InstMatrix",  0, DXGI_FORMAT_R32G32B32A32_FLOAT
"InstMatrix",  1, DXGI_FORMAT_R32G32B32A32_FLOAT
"InstMatrix",  2, DXGI_FORMAT_R32G32B32A32_FLOAT

If skinning is used, the vertex layout requires BLENDINDICES and BLENDWEIGHT.

Properties

  • SetAlpha: Sets the alpha (transparency) of the effect. Defaults to 1 (fully opaque). This value is also used for binning opaque vs. transparent geometry.

  • SetConstantAlbedo, SetConstantMetallic, and SetConstantRoughness: Used to set the constant value when not using texturing for the albedo, roughness, and metalness information.

  • SetAlbedoTexture: Sets the albedo texture. This uses the sampler in slot 0. Can be set to nullptr to remove a reference. Can optionally include an alpha channel as well.

  • SetNormalTexture: Sets the normal texture. This uses the sampler in slot 0. Can be set to nullptr to remove a reference.

See NormalMapEffect for more details about the normal map texture.

  • SetRMATexture: Sets the roughness/metalness/ambient-occlusion (RMA) texture. This uses the sampler in slot 0. Can be set to nullptr to remove a reference.

The RMA texture uses the glTF2 standard order: The metalness is in the B channel, roughness in the G channel, and ambient occlusion in the R channel. If there's no ambient occlusion, then the R channel should be set to all 1.

  • SetEmissiveTexture: Associates an emissive texture with the effect. This uses the sampler in slot 0. Can be set to nullptr to remove a reference.

  • SetSurfaceTextures: Associates a albedo texture, normal texture, and roughness/metalness/ambient-occlusion (RMA) texture with the effect in one method. This uses the sampler in slot 0. Can be set to nullptr to remove a reference.

  • SetIBLTextures: Associates a radiance and irradiance texture with the effect. The number of miplevels in the radiance texture is also required as this is used to compute roughness. This uses the sampler in slot 1. Can be set to nullptr to remove a reference.

The radiance and irradiance map are special cubemaps. They are generated by tools like AMD Cubemapgen, cmft/cmftStudio, IBLBaker, and Lys.

  • SetBiasedVertexNormals: Enables support for compressed vertex normals which require *2 - 1 biasing at runtime such as DXGI_FORMAT_R10G10B10A2_UNORM.

  • SetInstancingEnabled: Enables support for per-vertex instancing by adding a per-vertex XMFLOAT3X4 transform matrix.

  • SetVelocityGeneration: Enables the generation of a velocity buffer. If set to true, then both a Render Target 0 and Render Target 1 must be bound for rendering.

PBREffect does not support both instancing and velocity generation at the same time.

  • SetRenderTargetSizeInPixels: Used to set the pixel size of the render target when generating velocity buffers.

Remarks

These effects always performs per-pixel lighting. Calls to SetLightingEnabled(false); will generate a C++ exception, and calls to SetPerPixelLighting are ignored.

The lighting modeling for PBR does not make use of an ambient or specular term as these are more directly modeled by the image-based lighting model. Calls to SetAmbientLightColor and SetLightSpecularColor are ignored.

These effects requires a texture sampler in both slots 0 and 1. GeometricPrimitive and SpriteBatch only set a texture sampler in slot 0 by default, Model sets a sampler in slots 0 and 1.

Albedo (base color) map Normal map Emissive map
Metalness (blue) Roughness (green) Ambient Occlusion (red) Roughness/Metalness/Ambient-Occlusion map

Feature Level Notes

These effects use Shader Model 4.0 so requires Direct3D hardware feature level 10.0 or greater.

Note this means you can also count on DXGI_FORMAT_BC5_UNORM texture compression hardware support for your normal maps.

Direct3D feature levels

Further reading

Basic Theory of Physically-Based Rendering

Burley et al. "Physically-Based Shading at Disney", SIGGRAPH 2012 Course: Practical Physically Based Shading in Film and Game Production. Slides

Karis. "Real Shading in Unreal Engine 4", SIGGRAPH 2013 Course: Physically Based Shading in Theory and Practice. Slides Notes

SIGGRAPH Course: 2012 2013 2014 2015 2016 2017 2020

Pharr, Jakob, and Humphreys, Physically Based Rendering: From Theory to Implementation, Morgan Kaufmann, website code

The Comprehensive PBR Guide, Allegorithmic website

Christian Schüler, "Normal Mapping without Precomputed Tangents", ShaderX 5, Chapter 2.6, pp. 131 – 140 and this blog post

Art Pipeline for glTF

J.M.P. van Waveren and Ignacio Castaño, "Real-Time Normal Map DXT Compression", id Software, February 2008 PDF (note: BC5 is "tangent-space 3Dc")