Skip to content

Commit

Permalink
Adjust the LOD of textures in the hit shaders based on roughness and …
Browse files Browse the repository at this point in the history
…distance from the camera
  • Loading branch information
godlikepanos committed Nov 9, 2024
1 parent 485e7f1 commit 62afb38
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 2 deletions.
4 changes: 2 additions & 2 deletions AnKi/Shaders/GBufferGeneric.ankiprog
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,7 @@ GBufferPixelOut main(

// Diffuse
# if DIFFUSE_TEX
RVec3 diffColor = getBindlessTexture2DRVec4(localConstants.m_diffuseTex).SampleLevel(g_globalSampler, uv, 0.0).xyz;
RVec3 diffColor = getBindlessTexture2DRVec4(localConstants.m_diffuseTex).SampleLevel(g_globalSampler, uv, payload.m_textureLod).xyz;
# else
RVec3 diffColor = 1.0;
# endif
Expand All @@ -725,7 +725,7 @@ GBufferPixelOut main(

// Emissive
# if EMISSIVE_TEX
RVec3 emission = getBindlessTexture2DRVec4(localConstants.m_emissiveTex).SampleLevel(g_globalSampler, uv, 0.0).rgb;
RVec3 emission = getBindlessTexture2DRVec4(localConstants.m_emissiveTex).SampleLevel(g_globalSampler, uv, payload.m_textureLod).rgb;
# else
RVec3 emission = 1.0;
# endif
Expand Down
1 change: 1 addition & 0 deletions AnKi/Shaders/RtMaterialFetch.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ struct [raypayload] RtMaterialFetchRayPayload
Vec3 m_worldNormal : write(closesthit, miss): read(caller);
Vec3 m_emission : write(closesthit, miss): read(caller);
F32 m_rayT : write(closesthit, miss): read(caller);
F32 m_textureLod : write(caller): read(closesthit);
};

// Have a common resouce interface for all shaders
Expand Down
1 change: 1 addition & 0 deletions AnKi/Shaders/RtMaterialFetchDbg.ankiprog
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
// Trace
RtMaterialFetchRayPayload payload;
payload = (RtMaterialFetchRayPayload)0;
payload.m_textureLod = 0.0;
const U32 flags = RAY_FLAG_FORCE_OPAQUE;
const U32 sbtRecordOffset = 0u;
const U32 sbtRecordStride = 0u;
Expand Down
9 changes: 9 additions & 0 deletions AnKi/Shaders/RtReflections.ankiprog
Original file line number Diff line number Diff line change
Expand Up @@ -261,9 +261,18 @@ ANKI_FAST_CONSTANTS(Consts, g_consts)
const F32 pdf = 1.0;
# endif

// The more rough and the more far this pixel is then instruct the hit shaders to choose less detail mip
const F32 distanceToMaxMip = 50.0;
const F32 pixelDistFromCamera = length(worldPos - g_globalRendererConstants.m_cameraPosition);
const F32 distFactor = pow(pixelDistFromCamera / distanceToMaxMip, 4.0);
const F32 maxMips = 8.0;
const F32 textureLod = max(roughness, distFactor) * maxMips;

// Trace
RtMaterialFetchRayPayload payload;
payload = (RtMaterialFetchRayPayload)0;
payload.m_textureLod = textureLod;

constexpr U32 flags = RAY_FLAG_FORCE_OPAQUE | RAY_FLAG_SKIP_PROCEDURAL_PRIMITIVES;
const U32 sbtRecordOffset = 0u;
const U32 sbtRecordStride = 0u;
Expand Down

0 comments on commit 62afb38

Please sign in to comment.