Skip to content

Integrate HDRI #88

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 39 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
dce1eab
Multiple Importance Sampling for HDRI Sky (Ray Generation)
skhiat Jan 20, 2020
041b98c
backup
skhiat Jan 21, 2020
4991934
Merge branch 'HDRP/staging' of https://github.com/Unity-Technologies/…
skhiat Jan 21, 2020
7dd03f6
Multiple helpers
skhiat Jan 22, 2020
86ef8fc
backup
skhiat Jan 24, 2020
7e90ad6
Merge branch 'HDRP/staging' of https://github.com/Unity-Technologies/…
skhiat Jan 24, 2020
e33d246
Add SampleCubemapProjectionNode (Project cubemap to Geometry)
skhiat Jan 28, 2020
a503a3b
delete useless files
skhiat Jan 28, 2020
fd735d7
Merge branch 'HDRP/staging' of https://github.com/Unity-Technologies/…
skhiat Jan 29, 2020
ef8942b
Merge branch 'HDRP/backplate_ao_fix' of https://github.com/Unity-Tech…
skhiat Jan 29, 2020
8ba607a
update
skhiat Jan 30, 2020
6424029
Cube To LatLong for CubeArray
skhiat Feb 5, 2020
cb03955
Merge branch 'HDRP/staging' of https://github.com/Unity-Technologies/…
skhiat Feb 5, 2020
a6fc066
Functional generation
skhiat Feb 5, 2020
8f1a57d
Integration on ray tracer step
skhiat Feb 5, 2020
2608bbb
Store HDRI Integral
skhiat Feb 6, 2020
64c4938
.
skhiat Feb 11, 2020
b344dad
Integrate HDRI Sky
skhiat Feb 18, 2020
f32a21d
Update Importance Sampling
skhiat Feb 20, 2020
e13acdf
Update Algorithm with Hemisphere Path
skhiat Feb 27, 2020
40db532
Backup
skhiat Feb 28, 2020
6a74733
Merge branch 'HDRP/staging' of https://github.com/Unity-Technologies/…
skhiat Feb 28, 2020
99ca181
Update merge
skhiat Feb 28, 2020
3c8372d
Update Merge 001
skhiat Feb 28, 2020
515efb1
Update Merge 002
skhiat Feb 28, 2020
61c45a0
Update Merge 003
skhiat Feb 28, 2020
c5cbee5
Update Merge 004
skhiat Feb 28, 2020
7ee966c
Update Merge 005
skhiat Feb 28, 2020
41652ea
Update Merge 006
skhiat Feb 28, 2020
d46c512
Update Merge 007
skhiat Feb 28, 2020
62e7145
Merge branch 'HDRP/staging' of https://github.com/Unity-Technologies/…
skhiat Feb 28, 2020
d471635
Cleanup
skhiat Feb 28, 2020
01cd199
Update Integration of HDRISky Upper Hemisphere
skhiat Feb 28, 2020
4dcb181
Remove useless dump images on disk
skhiat Feb 28, 2020
b34cb1d
Fix change log issue
skhiat Mar 1, 2020
ef12f90
Update missing update call
skhiat Mar 5, 2020
ba6381c
Update fix form Important Sampling Branch
skhiat Mar 5, 2020
c9931c4
Merge branch 'HDRP/staging' of https://github.com/Unity-Technologies/…
skhiat Apr 9, 2020
3730e53
Fix merge
skhiat Apr 9, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using System;
using System.Collections.Generic;

namespace UnityEngine.Rendering
{
/// <summary>
/// RTHandleDeleter to schedule a release of a RTHandle in N ('lifetime') frame
/// </summary>
public static class RTHandleDeleter
{
internal class RTHandleDesc
{
public int lifetime = 3;
public RTHandle rtHandle = null;
}

internal static List<RTHandleDesc> m_RTHandleDescs = new List<RTHandleDesc>();

/// <summary>
/// Schedule a release of a RTHandle in 'lifetime' frames
/// </summary>
/// <param name="rtHandle">Considered rtHandle.</param>
/// <param name="lifetime">lifetime remaining of this rtHandle (unit: frame), default: 3 frames.</param>
public static void ScheduleRelease(RTHandle rtHandle, int lifetime = 3)
{
if (rtHandle != null && lifetime > 0)
{
RTHandleDesc desc = new RTHandleDesc();
desc.lifetime = lifetime;
desc.rtHandle = rtHandle;
m_RTHandleDescs.Add(desc);
}
else
{
rtHandle?.Release();
}
}

/// <summary>
/// Schedule a release of a RTHandle in 'lifetime' frames
/// </summary>
public static void Update()
{
foreach (RTHandleDesc desc in m_RTHandleDescs)
{
--desc.lifetime;
}

// Release 'too old' RTHandle
for (int i = m_RTHandleDescs.Count - 1; i >= 0; i--)
{
var cur = m_RTHandleDescs[i];
if (cur.lifetime <= 0)
{
cur.rtHandle.Release();
m_RTHandleDescs.Remove(cur);
}
}
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions com.unity.render-pipelines.high-definition/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
## [Unreleased]

### Added
- Update Integration of HDRI Sky for Lux intensity
- Ray tracing support for VR single-pass
- Added sharpen filter shader parameter and UI for TemporalAA to control image quality instead of hardcoded value
- Added frame settings option for custom post process and custom passes as well as custom color buffer format option.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ class HDRISkyEditor
SerializedDataParameter m_ShadowTint;

RTHandle m_IntensityTexture;
Material m_IntegrateHDRISkyMaterial; // Compute the HDRI sky intensity in lux for the skybox
Texture2D m_ReadBackTexture;
Material m_CubeToHemiLatLong;

public override bool hasAdvancedMode => true;

public override void OnEnable()
Expand Down Expand Up @@ -63,7 +64,9 @@ public override void OnEnable()
m_IntensityTexture = RTHandles.Alloc(1, 1, colorFormat: GraphicsFormat.R32G32B32A32_SFloat);
var hdrp = HDRenderPipeline.defaultAsset;
if (hdrp != null)
m_IntegrateHDRISkyMaterial = CoreUtils.CreateEngineMaterial(hdrp.renderPipelineResources.shaders.integrateHdriSkyPS);
{
m_CubeToHemiLatLong = CoreUtils.CreateEngineMaterial(hdrp.renderPipelineResources.shaders.cubeToHemiPanoPS);
}
m_ReadBackTexture = new Texture2D(1, 1, TextureFormat.RGBAFloat, false, false);
}

Expand All @@ -79,28 +82,46 @@ public override void OnDisable()
public void GetUpperHemisphereLuxValue()
{
Cubemap hdri = m_hdriSky.value.objectReferenceValue as Cubemap;

// null material can happen when no HDRP asset is present.
if (hdri == null || m_IntegrateHDRISkyMaterial == null)
if (hdri == null || m_CubeToHemiLatLong == null)
return;

m_IntegrateHDRISkyMaterial.SetTexture(HDShaderIDs._Cubemap, hdri);

Graphics.Blit(Texture2D.whiteTexture, m_IntensityTexture.rt, m_IntegrateHDRISkyMaterial);

// Copy the rendertexture containing the lux value inside a Texture2D
RenderTexture.active = m_IntensityTexture.rt;
m_ReadBackTexture.ReadPixels(new Rect(0.0f, 0.0f, 1, 1), 0, 0);
// Render LatLong
RTHandle latLongMap = RTHandles.Alloc( 4*hdri.width, hdri.width,
colorFormat: GraphicsFormat.R32G32B32A32_SFloat,
enableRandomWrite: true);
RTHandleDeleter.ScheduleRelease(latLongMap);
m_CubeToHemiLatLong.SetTexture ("_srcCubeTexture", hdri);
m_CubeToHemiLatLong.SetInt ("_cubeMipLvl", 0);
m_CubeToHemiLatLong.SetInt ("_cubeArrayIndex", 0);
m_CubeToHemiLatLong.SetInt ("_buildPDF", 0);
m_CubeToHemiLatLong.SetInt ("_preMultiplyByJacobian", 1);
m_CubeToHemiLatLong.SetInt ("_preMultiplyByCosTheta", 1);
m_CubeToHemiLatLong.SetInt ("_preMultiplyBySolidAngle", 0);
m_CubeToHemiLatLong.SetVector (HDShaderIDs._Sizes, new Vector4( (float)latLongMap.rt.width, (float)latLongMap.rt.height,
1.0f/((float)latLongMap.rt.width), 1.0f/((float)latLongMap.rt.height)));
Graphics.Blit(Texture2D.whiteTexture, latLongMap.rt, m_CubeToHemiLatLong, 0);

RTHandle totalRows = GPUScan.ComputeOperation(latLongMap, null, GPUScan.Operation.Total, GPUScan.Direction.Horizontal, latLongMap.rt.graphicsFormat);
RTHandle totalCols = GPUScan.ComputeOperation(totalRows, null, GPUScan.Operation.Total, GPUScan.Direction.Vertical, latLongMap.rt.graphicsFormat);
RTHandleDeleter.ScheduleRelease(totalRows);
RTHandleDeleter.ScheduleRelease(totalCols);

RenderTexture.active = totalCols.rt;
m_ReadBackTexture.ReadPixels(new Rect(0.0f, 0.0f, 1.0f, 1.0f), 0, 0);
RenderTexture.active = null;

// And then the value inside this texture
Color hdriIntensity = m_ReadBackTexture.GetPixel(0, 0);
m_UpperHemisphereLuxValue.value.floatValue = hdriIntensity.a;

m_UpperHemisphereLuxValue.value.floatValue = Mathf.PI*Mathf.PI*Mathf.Max(hdriIntensity.r, hdriIntensity.g, hdriIntensity.b)
/ // ----------------------------------------------------------------------------------------
((float)(latLongMap.rt.width*latLongMap.rt.height));

float max = Mathf.Max(hdriIntensity.r, hdriIntensity.g, hdriIntensity.b);
if (max == 0.0f)
max = 1.0f;

m_UpperHemisphereLuxColor.value.vector3Value = new Vector3(hdriIntensity.r/max, hdriIntensity.g/max, hdriIntensity.b/max);
m_UpperHemisphereLuxColor.value.vector3Value *= 0.5f; // Arbitrary 25% to not have too dark or too bright shadow
m_UpperHemisphereLuxColor.value.vector3Value *= 0.5f; // Arbitrary 50% to not have too dark or too bright shadow
}

public override void OnInspectorGUI()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#pragma only_renderers d3d11 ps4 xboxone vulkan metal switch

#pragma kernel CSMainAdd Add KerName=CSMainAdd
#pragma kernel CSMainSub Sub KerName=CSMainSub
#pragma kernel CSMainMult Mult KerName=CSMainMult
#pragma kernel CSMainDiv Div KerName=CSMainDiv
#pragma kernel CSMainMAD MAD KerName=CSMainMAD
#pragma kernel CSMainMAD_RG MAD_RG KerName=CSMainMAD_RG
#pragma kernel CSMainMean Mean KerName=CSMainMean
#pragma kernel CSMainSelfAdd READ_WRITE Add KerName=CSMainSelfAdd
#pragma kernel CSMainSelfSub READ_WRITE Sub KerName=CSMainSelfSub
#pragma kernel CSMainSelfMult READ_WRITE Mult KerName=CSMainSelfMult
#pragma kernel CSMainSelfDiv READ_WRITE Div KerName=CSMainSelfDiv
#pragma kernel CSMainSelfMAD READ_WRITE MAD KerName=CSMainSelfMAD
#pragma kernel CSMainSelfMAD_RG READ_WRITE MAD_RG KerName=CSMainSelfMAD_RG
#pragma kernel CSMainSelfMean READ_WRITE Mean KerName=CSMainSelfMean

#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Common.hlsl"
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Macros.hlsl"
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/ShaderVariables.hlsl"
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/ShaderVariablesFunctions.hlsl"

Texture2D<float4> _InputVal;

#ifdef READ_WRITE
RWTexture2D<float4> _Output;
#define _Input _Output
#else
Texture2D<float4> _Input;
RWTexture2D<float4> _Output;
#endif

uint4 _Sizes; // xy: InputSize; zw: OutputSize

[numthreads(8, 8, 1)]
void KerName(uint3 id : SV_DispatchThreadID)
{
if (all(id.xy < _Sizes.xy))
{
float4 v = _Input[id.xy];

#ifdef Add
_Output[id.xy] = v + _InputVal[uint2(0, 0)];
#elif defined(Sub)
_Output[id.xy] = v - _InputVal[uint2(0, 0)];
#elif defined(Mult)
_Output[id.xy] = v*_InputVal[uint2(0, 0)];
#elif defined(Div)
float4 a = _InputVal[uint2(0, 0)];
_Output[id.xy] = sign(a)*v/max(abs(a), 1e-4f);
#elif defined(MAD)
_Output[id.xy] = v*_InputVal[uint2(0, 0)] + _InputVal[uint2(1, 0)];
#elif defined(Mean)
float mean = dot(v.xyz, float3((1.0f/3.0f).xxx));
_Output[id.xy] = mean.xxxx;
#endif
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading