Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Hineven committed May 22, 2024
1 parent 94303d4 commit 6857e81
Show file tree
Hide file tree
Showing 7 changed files with 224 additions and 217 deletions.
367 changes: 185 additions & 182 deletions src/core/src/render_techniques/migi/migi.comp

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions src/core/src/render_techniques/migi/migi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,12 @@ void MIGI::render(CapsaicinInternal &capsaicin) noexcept
{
int flip = (int)internal_frame_index_ & 1;
gfxProgramSetParameter(gfx_, kernels_.program, "g_RWProbeHeaderPackedTexture",tex_.probe_header_packed[flip]);
gfxProgramSetParameter(gfx_, kernels_.program, "g_RWProbeScreenPositionTexture",tex_.probe_screen_position[flip]);
gfxProgramSetParameter(gfx_, kernels_.program, "g_RWProbeScreenCoordsTexture",tex_.probe_screen_coords[flip]);
gfxProgramSetParameter(gfx_, kernels_.program, "g_RWProbeLinearDepthTexture", tex_.probe_linear_depth[flip]);
gfxProgramSetParameter(gfx_, kernels_.program, "g_RWProbeWorldPositionTexture", tex_.probe_world_position[flip]);
gfxProgramSetParameter(gfx_, kernels_.program, "g_RWProbeNormalTexture", tex_.probe_normal[flip]);
gfxProgramSetParameter(gfx_, kernels_.program, "g_RWPreviousProbeHeaderPackedTexture", tex_.probe_header_packed[1 - flip]);
gfxProgramSetParameter(gfx_, kernels_.program, "g_RWPreviousProbeScreenPositionTexture", tex_.probe_screen_position[1 - flip]);
gfxProgramSetParameter(gfx_, kernels_.program, "g_RWPreviousProbeScreenCoordsTexture", tex_.probe_screen_coords[1 - flip]);
gfxProgramSetParameter(gfx_, kernels_.program, "g_RWPreviousProbeLinearDepthTexture", tex_.probe_linear_depth[1 - flip]);
gfxProgramSetParameter(gfx_, kernels_.program, "g_RWPreviousProbeWorldPositionTexture", tex_.probe_world_position[1 - flip]);
gfxProgramSetParameter(gfx_, kernels_.program, "g_RWPreviousProbeNormalTexture", tex_.probe_normal[1 - flip]);
Expand Down Expand Up @@ -242,7 +242,7 @@ void MIGI::render(CapsaicinInternal &capsaicin) noexcept
C.PreviousCameraUp = previous_constants_.CameraUp;

glm::vec2 jitter = {camera_matrices.projection[2][0], camera_matrices.projection[2][1]};
C.TAAJitterUV = jitter;
C.TAAJitterUV = jitter * 0.5f;
C.PreviousTAAJitterUV = previous_constants_.TAAJitterUV;

C.FrameSeed = options_.debug_freeze_frame_seed ? 123 : C.FrameIndex;
Expand Down
2 changes: 1 addition & 1 deletion src/core/src/render_techniques/migi/migi.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class MIGI : public RenderTechnique
// Probe header, uint32 per probe
GfxTexture probe_header_packed [2];
// Probe screen position, 2xuint16 packed in uint32
GfxTexture probe_screen_position [2];
GfxTexture probe_screen_coords[2];
// Probe linear depth 1xfloat32
GfxTexture probe_linear_depth [2];
// Probe world position 4xfloat32 because 3xfloat32 is not supported in most hardware
Expand Down
4 changes: 2 additions & 2 deletions src/core/src/render_techniques/migi/migi_inc.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,12 @@ RWTexture2D<float4> g_RWGlobalIlluminationOutput;
// ProbeClass : 4 bits
// ProbeFlag : 4 bits
RWTexture2D<uint> g_RWProbeHeaderPackedTexture;
RWTexture2D<uint> g_RWProbeScreenPositionTexture;
RWTexture2D<uint> g_RWProbeScreenCoordsTexture;
RWTexture2D<float> g_RWProbeLinearDepthTexture;
RWTexture2D<float4> g_RWProbeWorldPositionTexture;
RWTexture2D<unorm float2> g_RWProbeNormalTexture;
RWTexture2D<uint> g_RWPreviousProbeHeaderPackedTexture;
RWTexture2D<uint> g_RWPreviousProbeScreenPositionTexture;
RWTexture2D<uint> g_RWPreviousProbeScreenCoordsTexture;
RWTexture2D<float> g_RWPreviousProbeLinearDepthTexture;
RWTexture2D<float4> g_RWPreviousProbeWorldPositionTexture;
RWTexture2D<unorm float2> g_RWPreviousProbeNormalTexture;
Expand Down
16 changes: 8 additions & 8 deletions src/core/src/render_techniques/migi/migi_init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,10 +219,10 @@ bool MIGI::initResources (const CapsaicinInternal & capsaicin) {
tex_.probe_header_packed[1] = gfxCreateTexture2D(gfx_, probe_texture_width, probe_texture_height, DXGI_FORMAT_R32_UINT);
tex_.probe_header_packed[1].setName("ProbeHeaderPacked1");

tex_.probe_screen_position[0] = gfxCreateTexture2D(gfx_, probe_texture_width, probe_texture_height, DXGI_FORMAT_R32_UINT);
tex_.probe_screen_position[0].setName("ProbeScreenPosition0");
tex_.probe_screen_position[1] = gfxCreateTexture2D(gfx_, probe_texture_width, probe_texture_height, DXGI_FORMAT_R32_UINT);
tex_.probe_screen_position[1].setName("ProbeScreenPosition1");
tex_.probe_screen_coords[0] = gfxCreateTexture2D(gfx_, probe_texture_width, probe_texture_height, DXGI_FORMAT_R32_UINT);
tex_.probe_screen_coords[0].setName("ProbeScreenCoords0");
tex_.probe_screen_coords[1] = gfxCreateTexture2D(gfx_, probe_texture_width, probe_texture_height, DXGI_FORMAT_R32_UINT);
tex_.probe_screen_coords[1].setName("ProbeScreenCoords1");

tex_.probe_linear_depth[0] = gfxCreateTexture2D(gfx_, probe_texture_width, probe_texture_height, DXGI_FORMAT_R32_FLOAT);
tex_.probe_linear_depth[0].setName("ProbeLinearDepth0");
Expand All @@ -244,7 +244,7 @@ bool MIGI::initResources (const CapsaicinInternal & capsaicin) {
tex_.probe_irradiance[1] = gfxCreateTexture2D(gfx_, probe_texture_width, probe_texture_height, DXGI_FORMAT_R16G16B16A16_FLOAT);
tex_.probe_irradiance[1].setName("ProbeIrradiance1");

tex_.probe_history_trust = gfxCreateTexture2D(gfx_, probe_texture_width, probe_texture_height_uniform, DXGI_FORMAT_R32_FLOAT);
tex_.probe_history_trust = gfxCreateTexture2D(gfx_, probe_texture_width, probe_texture_height, DXGI_FORMAT_R32_FLOAT);
tex_.probe_history_trust.setName("ProbeHistoryTrust");

int tile_texture_width = divideAndRoundUp(capsaicin.getWidth(), SSRC_TILE_SIZE);
Expand Down Expand Up @@ -316,7 +316,7 @@ bool MIGI::initResources (const CapsaicinInternal & capsaicin) {
buf_.update_ray_direction = gfxCreateBuffer<uint32_t>(gfx_, options_.SSRC_max_update_ray_count);
buf_.update_ray_direction.setName("UpdateRayDirection");
buf_.update_ray_radiance_inv_pdf = gfxCreateBuffer<uint32_t>(gfx_, options_.SSRC_max_update_ray_count * 2);
buf_.update_ray_radiance_inv_pdf.setName("UpdateRayRadiancePdf");
buf_.update_ray_radiance_inv_pdf.setName("UpdateRayRadianceInvPdf");
buf_.update_ray_linear_depth = gfxCreateBuffer<uint32_t>(gfx_, options_.SSRC_max_update_ray_count);
buf_.update_ray_linear_depth.setName("UpdateRayLinearDepth");

Expand Down Expand Up @@ -429,8 +429,8 @@ void MIGI::releaseResources()
// Free textures
gfxDestroyTexture(gfx_, tex_.probe_header_packed[0]);
gfxDestroyTexture(gfx_, tex_.probe_header_packed[1]);
gfxDestroyTexture(gfx_, tex_.probe_screen_position[0]);
gfxDestroyTexture(gfx_, tex_.probe_screen_position[1]);
gfxDestroyTexture(gfx_, tex_.probe_screen_coords[0]);
gfxDestroyTexture(gfx_, tex_.probe_screen_coords[1]);
gfxDestroyTexture(gfx_, tex_.probe_linear_depth[0]);
gfxDestroyTexture(gfx_, tex_.probe_linear_depth[1]);
gfxDestroyTexture(gfx_, tex_.probe_world_position[0]);
Expand Down
17 changes: 9 additions & 8 deletions src/core/src/render_techniques/migi/migi_lib.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -195,11 +195,6 @@ void EvaluateSG_Gradients (SGData SG, float3 TargetDirection, out SGGradients Gr
Gradients.dColor = W2.xxx;

Gradients.dDirection = TargetDirection * W2 * SGColorScale * SG.Lambda;
// We need to accumulate the gradients of Direction that are parallel
// to the current Direction on dColor.
// FIXME
//float dZ = W2 * SGColorScale * SG.Lambda * dot(SG.Direction, TargetDirection);
//dColorExtra = SG.Color * dZ;
dColorExtra = 0.f.xxx;
}

Expand Down Expand Up @@ -571,12 +566,18 @@ SGData SGInterpolate (in SGData X00, in SGData X01, in SGData X10, in SGData X11

// TODO improve this
SGData MergeSG (SGData SG1, SGData SG2) {
float W1 = SGIntegrate(SG1.Lambda) * dot(SG1.Color, 1.f.xxx) + 1e-4f;
float W2 = SGIntegrate(SG2.Lambda) * dot(SG2.Color, 1.f.xxx) + 1e-4f;
float W1 = max(SGIntegrate(SG1.Lambda) * dot(SG1.Color, 1.f.xxx), 0) + 1e-4f;
float W2 = max(SGIntegrate(SG2.Lambda) * dot(SG2.Color, 1.f.xxx), 0) + 1e-4f;
SGData Result;
Result.Direction = normalize(SG1.Direction*W1 + SG2.Direction*W2);
float DirectionNorm = dot(Result.Direction, Result.Direction);
// TODO: This may be slow
if(isnan(DirectionNorm) || DirectionNorm < 1e-4f || isinf(DirectionNorm)) {
Result.Direction = W1 > W2 ? SG1.Direction : SG2.Direction;
}
Result.Lambda = (SG1.Lambda*W1 + SG2.Lambda*W2) / (W1 + W2);
Result.Color = (SG1.Color*W1 + SG2.Color*W2) / (W1 + W2);
Result.Color = (SG1.Color*W1 + SG2.Color*W2) / (W1 + W2);
Result.Depth = (SG1.Depth*W1 + SG2.Depth*W2) / (W1 + W2);
return Result;

}
Expand Down
29 changes: 16 additions & 13 deletions src/core/src/render_techniques/migi/migi_probes.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
#include "migi_lib.hlsl"

struct ProbeHeader {
// Screen pixel position of the probe
int2 ScreenPosition;
// Screen pixel coords of the probe
int2 ScreenCoords;
int BasisOffset;
// 0: 0, 1: 1, 2: 2, 3: 4, 4: 8, no larger than 8
int Class;
Expand All @@ -28,10 +28,10 @@ ProbeHeader GetScreenProbeHeader (int2 ProbeIndex, bool bPrevious = false) {
Header.BasisOffset = Packed & 0x00FFFFFF;
Header.Class = (Packed >> 24) & 0x0F;
uint Flags = (Packed >> 28) & 0x0F;
Header.ScreenPosition = UnpackUint16x2(
Header.ScreenCoords = UnpackUint16x2(
bPrevious
? g_RWPreviousProbeScreenPositionTexture[ProbeIndex].x
: g_RWProbeScreenPositionTexture[ProbeIndex].x
? g_RWPreviousProbeScreenCoordsTexture[ProbeIndex].x
: g_RWProbeScreenCoordsTexture[ProbeIndex].x
);
Header.LinearDepth =
bPrevious ? g_RWPreviousProbeLinearDepthTexture[ProbeIndex].x
Expand All @@ -46,6 +46,8 @@ ProbeHeader GetScreenProbeHeader (int2 ProbeIndex, bool bPrevious = false) {
* 2.f - 1.f);

Header.bValid = Header.LinearDepth > 0;
// If this is not a valid probe, hint the caller that it has no valid SGs
if(!Header.bValid) Header.Class = 0;
return Header;
}

Expand All @@ -55,7 +57,7 @@ void WriteScreenProbeHeader (int2 ProbeIndex, ProbeHeader Header) {
Packed |= (Header.Class & 0x0F) << 24;
// Packed |= (Header.bValid ? 1 : 0) << 28;
g_RWProbeHeaderPackedTexture[ProbeIndex] = Packed;
g_RWProbeScreenPositionTexture[ProbeIndex] = PackUint16x2(Header.ScreenPosition);
g_RWProbeScreenCoordsTexture[ProbeIndex] = PackUint16x2(Header.ScreenCoords);
g_RWProbeLinearDepthTexture[ProbeIndex] = Header.LinearDepth;
g_RWProbeWorldPositionTexture[ProbeIndex] = float4(Header.Position, 0.f);
g_RWProbeNormalTexture[ProbeIndex] = UnitVectorToOctahedron(Header.Normal) * 0.5f + 0.5f;
Expand All @@ -65,14 +67,14 @@ int2 GetTileJitter (bool bPrevious = false) {
return Hammersley16((bPrevious ? MI.PreviousFrameSeed : MI.FrameSeed) % 8, 8, 0) * SSRC_TILE_SIZE;
}

int2 GetScreenProbeScreenPosition (int2 ProbeIndex, bool bPrevious = false) {
int2 GetScreenProbeScreenCoords (int2 ProbeIndex, bool bPrevious = false) {
int2 TileJitter = GetTileJitter(bPrevious);
int2 UniformScreenProbeScreenPosition = ProbeIndex * SSRC_TILE_SIZE + TileJitter;
int2 UniformScreenProbeScreenCoords = ProbeIndex * SSRC_TILE_SIZE + TileJitter;
if(any(ProbeIndex >= MI.TileDimensions)) {
ProbeHeader Header = GetScreenProbeHeader(ProbeIndex, bPrevious);
UniformScreenProbeScreenPosition = Header.ScreenPosition;
UniformScreenProbeScreenCoords = Header.ScreenCoords;
}
return UniformScreenProbeScreenPosition;
return UniformScreenProbeScreenCoords;
}

float3 GetScreenProbePosition (int2 ProbeIndex, bool bPrevious = false) {
Expand All @@ -82,7 +84,8 @@ float3 GetScreenProbePosition (int2 ProbeIndex, bool bPrevious = false) {

int ComputeProbeRankFromSplattedError (int2 ScreenCoords) {
// TODO: Implement this function
return 0;
// FIXME
return 3;
}

int GetProbeBasisCountFromClass (int ProbeClass) {
Expand All @@ -104,12 +107,12 @@ int GetAdaptiveProbeIndex (int2 TileCoords, int AdaptiveProbeListIndex, bool bP
: g_RWTileAdaptiveProbeIndexTexture[IndexCoords].x;
}

int2 GetUniformScreenProbeScreenPosition (int2 TileCoords, bool bPrevious = false) {
int2 GetUniformScreenProbeScreenCoords (int2 TileCoords, bool bPrevious = false) {
return TileCoords * SSRC_TILE_SIZE + GetTileJitter(bPrevious);
}

float2 GetUniformScreenProbeScreenUV (int2 TileCoords, bool bPrevious = false) {
return (GetUniformScreenProbeScreenPosition(TileCoords, bPrevious) + 0.5) * MI.ScreenDimensionsInv;
return (GetUniformScreenProbeScreenCoords(TileCoords, bPrevious) + 0.5) * MI.ScreenDimensionsInv;
}

float GetScreenProbeLinearDepth (int2 ProbeIndex, bool bPrevious = false) {
Expand Down

0 comments on commit 6857e81

Please sign in to comment.