Skip to content

Commit 29ec490

Browse files
HDRenderPipeline: Move all are LTC data in one texture2DArray
Required to save ressource index for PS4 until playstation platform team fix the issue of limited resource (16 texture + compute buffer)
1 parent 2219188 commit 29ec490

3 files changed

Lines changed: 37 additions & 52 deletions

File tree

Assets/ScriptableRenderLoop/HDRenderPipeline/Material/Lit/Lit.cs

Lines changed: 24 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -159,27 +159,15 @@ public void GetMaterialGBufferDescription(out RenderTextureFormat[] RTFormat, ou
159159
private Material m_InitPreFGD;
160160
private RenderTexture m_PreIntegratedFGD;
161161

162-
// For area lighting
163-
private Texture2D m_LtcGGXMatrix; // RGBA
164-
private Texture2D m_LtcDisneyDiffuseMatrix; // RGBA
165-
private Texture2D m_LtcMultiGGXFresnelDisneyDiffuse; // RGB, A unused
162+
// For area lighting - We pack all texture inside a texture array to reduce the number of resource required
163+
private Texture2DArray m_LtcData; // 0: m_LtcGGXMatrix - RGBA, 2: m_LtcDisneyDiffuseMatrix - RGBA, 3: m_LtcMultiGGXFresnelDisneyDiffuse - RGB, A unused
166164

167165
const int k_LtcLUTMatrixDim = 3; // size of the matrix (3x3)
168166
const int k_LtcLUTResolution = 64;
169167

170-
Texture2D CreateLUT(int width, int height, TextureFormat format, Color[] pixels)
171-
{
172-
Texture2D tex = new Texture2D(width, height, format, false /*mipmap*/, true /*linear*/);
173-
tex.hideFlags = HideFlags.HideAndDontSave;
174-
tex.wrapMode = TextureWrapMode.Clamp;
175-
tex.filterMode = FilterMode.Bilinear;
176-
tex.SetPixels(pixels);
177-
tex.Apply();
178-
return tex;
179-
}
180168

181169
// Load LUT with one scalar in alpha of a tex2D
182-
Texture2D LoadLUT(TextureFormat format, float[] LUTScalar)
170+
void LoadLUT(Texture2DArray tex, int arrayElement, TextureFormat format, float[] LUTScalar)
183171
{
184172
const int count = k_LtcLUTResolution * k_LtcLUTResolution;
185173
Color[] pixels = new Color[count];
@@ -189,11 +177,11 @@ Texture2D LoadLUT(TextureFormat format, float[] LUTScalar)
189177
pixels[i] = new Color(0, 0, 0, LUTScalar[i]);
190178
}
191179

192-
return CreateLUT(k_LtcLUTResolution, k_LtcLUTResolution, format, pixels);
180+
tex.SetPixels(pixels, arrayElement);
193181
}
194182

195183
// Load LUT with 3x3 matrix in RGBA of a tex2D (some part are zero)
196-
Texture2D LoadLUT(TextureFormat format, double[,] LUTTransformInv)
184+
void LoadLUT(Texture2DArray tex, int arrayElement, TextureFormat format, double[,] LUTTransformInv)
197185
{
198186
const int count = k_LtcLUTResolution * k_LtcLUTResolution;
199187
Color[] pixels = new Color[count];
@@ -208,13 +196,13 @@ Texture2D LoadLUT(TextureFormat format, double[,] LUTTransformInv)
208196
(float)LUTTransformInv[i, 6]);
209197
}
210198

211-
return CreateLUT(k_LtcLUTResolution, k_LtcLUTResolution, format, pixels);
199+
tex.SetPixels(pixels, arrayElement);
212200
}
213201

214202
// Special-case function for 'm_LtcMultiGGXFresnelDisneyDiffuse'.
215-
Texture2D LoadLUT(TextureFormat format, float[] LtcGGXMagnitudeData,
216-
float[] LtcGGXFresnelData,
217-
float[] LtcDisneyDiffuseMagnitudeData)
203+
void LoadLUT(Texture2DArray tex, int arrayElement, TextureFormat format, float[] LtcGGXMagnitudeData,
204+
float[] LtcGGXFresnelData,
205+
float[] LtcDisneyDiffuseMagnitudeData)
218206
{
219207
const int count = k_LtcLUTResolution * k_LtcLUTResolution;
220208
Color[] pixels = new Color[count];
@@ -227,7 +215,7 @@ Texture2D LoadLUT(TextureFormat format, float[] LtcGGXMagnitudeData,
227215
LtcGGXFresnelData[i], LtcDisneyDiffuseMagnitudeData[i], 1);
228216
}
229217

230-
return CreateLUT(k_LtcLUTResolution, k_LtcLUTResolution, format, pixels);
218+
tex.SetPixels(pixels, arrayElement);
231219
}
232220

233221
public void Build()
@@ -240,12 +228,19 @@ public void Build()
240228
m_PreIntegratedFGD.wrapMode = TextureWrapMode.Clamp;
241229
m_PreIntegratedFGD.Create();
242230

243-
m_LtcGGXMatrix = LoadLUT(TextureFormat.RGBAHalf, s_LtcGGXMatrixData);
244-
m_LtcDisneyDiffuseMatrix = LoadLUT(TextureFormat.RGBAHalf, s_LtcDisneyDiffuseMatrixData);
231+
m_LtcData = new Texture2DArray(k_LtcLUTResolution, k_LtcLUTResolution, 3, TextureFormat.RGBAHalf, false /*mipmap*/, true /* linear */)
232+
{
233+
hideFlags = HideFlags.HideAndDontSave,
234+
wrapMode = TextureWrapMode.Clamp,
235+
filterMode = FilterMode.Bilinear
236+
};
237+
238+
LoadLUT(m_LtcData, 0, TextureFormat.RGBAHalf, s_LtcGGXMatrixData);
239+
LoadLUT(m_LtcData, 1, TextureFormat.RGBAHalf, s_LtcDisneyDiffuseMatrixData);
245240
// TODO: switch to RGBA64 when it becomes available.
246-
m_LtcMultiGGXFresnelDisneyDiffuse = LoadLUT(TextureFormat.RGBAHalf, s_LtcGGXMagnitudeData,
247-
s_LtcGGXFresnelData,
248-
s_LtcDisneyDiffuseMagnitudeData);
241+
LoadLUT(m_LtcData, 2, TextureFormat.RGBAHalf, s_LtcGGXMagnitudeData, s_LtcGGXFresnelData, s_LtcDisneyDiffuseMagnitudeData);
242+
243+
m_LtcData.Apply();
249244

250245
isInit = false;
251246
}
@@ -271,10 +266,8 @@ public void RenderInit(Rendering.ScriptableRenderContext renderContext)
271266

272267
public void Bind()
273268
{
274-
Shader.SetGlobalTexture("_PreIntegratedFGD", m_PreIntegratedFGD);
275-
Shader.SetGlobalTexture("_LtcGGXMatrix", m_LtcGGXMatrix);
276-
Shader.SetGlobalTexture("_LtcDisneyDiffuseMatrix", m_LtcDisneyDiffuseMatrix);
277-
Shader.SetGlobalTexture("_LtcMultiGGXFresnelDisneyDiffuse", m_LtcMultiGGXFresnelDisneyDiffuse);
269+
Shader.SetGlobalTexture("_PreIntegratedFGD", m_PreIntegratedFGD);
270+
Shader.SetGlobalTexture("_LtcData", m_LtcData);
278271
}
279272
}
280273
}

Assets/ScriptableRenderLoop/HDRenderPipeline/Material/Lit/Lit.hlsl

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -40,24 +40,16 @@
4040
// TODO: Check if anisotropy with a dynamic if on anisotropy > 0 is performant. Because it may mean we always calculate both isotropy and anisotropy case.
4141
// Maybe we should always calculate anisotropy in case of standard ? Don't think the compile can optimize correctly.
4242

43-
// TODO: I haven't configure this sampler in the code, we should be able to do it (but Unity don't allow it for now...)
44-
// By default Unity provide MIG_MAG_LINEAR_POINT sampler, so it fit with our need.
45-
// TODO: to avoid the message Fragment program 'Frag' : sampler 'sampler_PreIntegratedFGD' has no matching texture and will be undefined.
46-
// We need to test this define LIGHTLOOP_TILE_DIRECT, this is a bad workaround but no alternative until we can setup sampler correctly...
47-
#if defined(LIT_DISPLAY_REFERENCE_IBL) || defined(LIGHTLOOP_TILE_DIRECT)
48-
// When reference mode is enabled, then we need to chose another sampler not related to cubemap code...
49-
SAMPLER2D(sampler_LtcGGXMatrix);
50-
#define SRL_BilinearSampler sampler_LtcGGXMatrix // Used for all textures
51-
#else
52-
SAMPLER2D(sampler_PreIntegratedFGD);
53-
#define SRL_BilinearSampler sampler_PreIntegratedFGD // Used for all textures
54-
#endif
55-
5643
// TODO: This one should be set into a constant Buffer at pass frequency (with _Screensize)
44+
// TODO: we can share the sampler here and name it SRL_BilinearSampler. However Unity currently doesn't support to set sampler in C#
45+
// + to avoid the message Fragment program 'Frag' : sampler 'sampler_PreIntegratedFGD' has no matching texture and will be undefined.
5746
TEXTURE2D(_PreIntegratedFGD);
58-
TEXTURE2D(_LtcGGXMatrix); // RGBA
59-
TEXTURE2D(_LtcDisneyDiffuseMatrix); // RGBA
60-
TEXTURE2D(_LtcMultiGGXFresnelDisneyDiffuse); // RGB, A unused
47+
SAMPLER2D(sampler_PreIntegratedFGD);
48+
TEXTURE2D_ARRAY(_LtcData); // We pack the 3 Ltc data inside a texture array
49+
SAMPLER2D(sampler_LtcData);
50+
#define LTC_GGX_MATRIX_INDEX 0 // RGBA
51+
#define LTC_DISNEY_DIFFUSE_MATRIX_INDEX 1 // RGBA
52+
#define LTC_MULTI_GGX_FRESNEL_DISNEY_DIFFUSE_INDEX 2 // RGB, A unused
6153

6254
//-----------------------------------------------------------------------------
6355
// Helper functions/variable specific to this material
@@ -82,7 +74,7 @@ void GetPreIntegratedFGD(float NdotV, float perceptualRoughness, float3 fresnel0
8274
// _PreIntegratedFGD.y = Gv * Fc
8375
// Pre integrate DisneyDiffuse FGD:
8476
// _PreIntegratedFGD.z = DisneyDiffuse
85-
float3 preFGD = SAMPLE_TEXTURE2D_LOD(_PreIntegratedFGD, SRL_BilinearSampler, float2(NdotV, perceptualRoughness), 0).xyz;
77+
float3 preFGD = SAMPLE_TEXTURE2D_LOD(_PreIntegratedFGD, sampler_PreIntegratedFGD, float2(NdotV, perceptualRoughness), 0).xyz;
8678

8779
// f0 * Gv * (1 - Fc) + Gv * Fc
8880
specularFGD = fresnel0 * preFGD.x + preFGD.y;
@@ -514,15 +506,15 @@ PreLightData GetPreLightData(float3 V, PositionInputs posInput, BSDFData bsdfDat
514506
// Note we load the matrix transpose (avoid to have to transpose it in shader)
515507
preLightData.ltcXformGGX = 0.0;
516508
preLightData.ltcXformGGX._m22 = 1.0;
517-
preLightData.ltcXformGGX._m00_m02_m11_m20 = SAMPLE_TEXTURE2D_LOD(_LtcGGXMatrix, SRL_BilinearSampler, uv, 0);
509+
preLightData.ltcXformGGX._m00_m02_m11_m20 = SAMPLE_TEXTURE2D_ARRAY_LOD(_LtcData, sampler_LtcData, uv, LTC_GGX_MATRIX_INDEX, 0);
518510

519511
// Get the inverse LTC matrix for Disney Diffuse
520512
// Note we load the matrix transpose (avoid to have to transpose it in shader)
521513
preLightData.ltcXformDisneyDiffuse = 0.0;
522514
preLightData.ltcXformDisneyDiffuse._m22 = 1.0;
523-
preLightData.ltcXformDisneyDiffuse._m00_m02_m11_m20 = SAMPLE_TEXTURE2D_LOD(_LtcDisneyDiffuseMatrix, SRL_BilinearSampler, uv, 0);
515+
preLightData.ltcXformDisneyDiffuse._m00_m02_m11_m20 = SAMPLE_TEXTURE2D_ARRAY_LOD(_LtcData, sampler_LtcData, uv, LTC_DISNEY_DIFFUSE_MATRIX_INDEX, 0);
524516

525-
float3 ltcMagnitude = SAMPLE_TEXTURE2D_LOD(_LtcMultiGGXFresnelDisneyDiffuse, SRL_BilinearSampler, uv, 0).r;
517+
float3 ltcMagnitude = SAMPLE_TEXTURE2D_ARRAY_LOD(_LtcData, sampler_LtcData, uv, LTC_MULTI_GGX_FRESNEL_DISNEY_DIFFUSE_INDEX, 0).rgb;
526518
preLightData.ltcGGXFresnelMagnitudeDiff = ltcMagnitude.r;
527519
preLightData.ltcGGXFresnelMagnitude = ltcMagnitude.g;
528520
preLightData.ltcDisneyDiffuseMagnitude = ltcMagnitude.b;

ProjectSettings/ProjectVersion.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
m_EditorVersion: 5.6.0b5
1+
m_EditorVersion: 5.6.0b6

0 commit comments

Comments
 (0)