Skip to content

Commit 17ef666

Browse files
Merge branch 'terrainDisplacementDefaultFix' into 'main'
[REMIX-3048] fix bugs with terrain + displace_out default values. See merge request lightspeedrtx/dxvk-remix-nv!1128
2 parents 912a614 + ffe2d95 commit 17ef666

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

src/dxvk/rtx_render/rtx_terrain_baker.cpp

+13-3
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@
3737
#include "../../dxso/dxso_util.h"
3838
#include "../../d3d9/d3d9_caps.h"
3939

40+
namespace {
41+
// By default, a value of 1.f will have 0 displacement.
42+
const float kDefaultNeutralHeight = 1.f;
43+
}
44+
4045
namespace dxvk {
4146

4247
uint32_t getMipLevels(ReplacementMaterialTextureType::Enum textureType, const VkExtent3D& extent) {
@@ -79,10 +84,12 @@ namespace dxvk {
7984
}
8085

8186
VkClearColorValue TerrainBaker::getClearColor(ReplacementMaterialTextureType::Enum textureType) {
82-
float neutral_height = m_prevFrameMaxDisplaceIn / (m_prevFrameMaxDisplaceIn + m_prevFrameMaxDisplaceOut);
87+
const float prevFrameTotalHeight = m_prevFrameMaxDisplaceIn + m_prevFrameMaxDisplaceOut;
88+
float neutral_height = prevFrameTotalHeight != 0.f ? m_prevFrameMaxDisplaceIn / prevFrameTotalHeight : kDefaultNeutralHeight;
8389
switch (textureType) {
8490
case ReplacementMaterialTextureType::Height:
8591
// height maps should be cleared to neutral_height, which keeps the displaced surface identical to the original surface.
92+
// The height texture should be single channel, so only the first value actually matters.
8693
return { neutral_height, neutral_height, neutral_height, neutral_height };
8794
break;
8895

@@ -188,7 +195,8 @@ namespace dxvk {
188195
const float materialTotalHeight = replacementMaterial->getDisplaceIn() + replacementMaterial->getDisplaceOut();
189196

190197
conversionInfo.scale = prevFrameTotalHeight <= 0.f ? 0.f : materialTotalHeight / prevFrameTotalHeight;
191-
conversionInfo.offset = prevFrameTotalHeight <= 0.f ? 0.f : -1.f * replacementMaterial->getDisplaceIn() / materialTotalHeight;
198+
// We want to subtract the original neutral displacement, then scale the values, then add the new neutral displacement.
199+
conversionInfo.offset = -1.f * (materialTotalHeight == 0.f ? kDefaultNeutralHeight : (replacementMaterial->getDisplaceIn() / materialTotalHeight));
192200
m_currFrameMaxDisplaceIn = std::max(m_currFrameMaxDisplaceIn, replacementMaterial->getDisplaceIn());
193201
m_currFrameMaxDisplaceOut = std::max(m_currFrameMaxDisplaceOut, replacementMaterial->getDisplaceOut());
194202
}
@@ -404,7 +412,9 @@ namespace dxvk {
404412
ctx->setSpecConstant(VK_PIPELINE_BIND_POINT_GRAPHICS, D3D9SpecConstantId::ReplacementTextureCategory, static_cast<uint32_t>(ReplacementMaterialTextureCategory::AlbedoOpacity));
405413

406414
// The height value that corresponds to the original surface height.
407-
const float neutralDisplacement = m_prevFrameMaxDisplaceIn / ( m_prevFrameMaxDisplaceIn + m_prevFrameMaxDisplaceOut);
415+
const float prevFrameTotalHeight = m_prevFrameMaxDisplaceIn + m_prevFrameMaxDisplaceOut;
416+
const float neutralDisplacement = prevFrameTotalHeight != 0.f ? m_prevFrameMaxDisplaceIn / prevFrameTotalHeight : kDefaultNeutralHeight;
417+
408418

409419
// Bake all material textures
410420
for (uint32_t iTexture = 0; iTexture < numTexturesToBake; iTexture++) {

0 commit comments

Comments
 (0)