This commit is contained in:
2026-11-24 15:51:02 +01:00
parent cca4d73e41
commit f4aa17114a
22092 changed files with 5042406 additions and 0 deletions
@@ -0,0 +1,140 @@
#ifndef _SAMPLING_SAMPLING_COMMON_HLSL_
#define _SAMPLING_SAMPLING_COMMON_HLSL_
#ifndef PI
#define PI 3.14159265358979323846
#endif
#define FLOAT_ONE_MINUS_EPSILON 0.99999994
// Paper: Building an Orthonormal Basis, Revisited.
// Tom Duff, James Burgess, Per Christensen, Christophe Hery, Andrew Kensler, Max Liani, and Ryusuke Villemin (Pixar).
// https://graphics.pixar.com/library/OrthonormalB/paper.pdf
void OrthoBasisFromVector(float3 n, out float3 b1, out float3 b2)
{
float sign = n.z >= 0.0f ? 1.0f : -1.0f;
const float a = -1.0f / (sign + n.z);
const float b = n.x * n.y * a;
b1 = float3(1.0f + sign * n.x * n.x * a, sign * b, -sign * n.x);
b2 = float3(b, sign + n.y * n.y * a, -n.y);
}
float3x3 OrthoBasisFromVector(float3 n)
{
float3 t, b;
OrthoBasisFromVector(n, t, b);
return transpose(float3x3(t, b, n));
}
void SampleDiffuseBrdf(float2 u, float3 shadingNormal, out float3 wi)
{
wi = float3(0, 0, 0);
float a = sqrt(u.x);
float b = 2.0 * PI * u.y;
float3 localWi = float3(a * cos(b), a * sin(b), sqrt(1.0f - u.x));
float3x3 TBN = OrthoBasisFromVector(shadingNormal);
wi = mul(TBN, localWi);
}
bool SampleDiffuseBrdf(float2 u, float3 geometryNormal, float3 shadingNormal, float3 wo, out float3 wi, out float pdf)
{
pdf = 0.0f;
wi = 0.0f;
bool valid = true;
if (dot(geometryNormal, wo) <= 0.0f)
valid = false;
else
{
float a = sqrt(1.0f - u.x);
float b = 2.0 * PI * u.y;
float3 localWi = float3(a * cos(b), a * sin(b), sqrt(u.x));
float3x3 TBN = OrthoBasisFromVector(shadingNormal);
wi = mul(TBN, localWi);
pdf = localWi.z / PI;
}
return valid;
}
float3 MapSquareToSphere(float2 unitSquareCoords)
{
float theta = (2.0f * PI) * unitSquareCoords.x;
float sinTheta, cosTheta;
sincos(theta, sinTheta, cosTheta);
float cosPhi = 1.0f - 2.0f * unitSquareCoords.y;
float sinPhi = sqrt(max(0.0f, 1.0f - cosPhi * cosPhi));
return float3(sinPhi * cosTheta, sinPhi * sinTheta, cosPhi);
}
// An optimized version of the area-preserving square-to-disk map presented in "A Low Distortion Map Between Disk and Square". Copied from from http://psgraphics.blogspot.com/2011/01/improved-code-for-concentric-map.html.
float2 MapSquareToDisk(float2 rnd)
{
//Code flow makes sure that division by 0 and thus NaNs cannot happen.
float phi;
float r;
float a = rnd.x * 2.0f - 1.0f;
float b = rnd.y * 2.0f - 1.0f;
if (a * a > b * b)
{
r = a;
phi = (PI * 0.25f) * (b / a);
}
else
{
r = b;
if (b == 0.0f)
phi = PI * 0.5f;
else
phi = (PI * 0.5f) - (PI * 0.25f) * (a / b);
}
return float2(r * cos(phi), r * sin(phi));
}
float3 CosineSample(float2 u, float3 normal)
{
float a = sqrt(u.x);
float b = 2.0f * PI * u.y;
float3 localDir = float3(a * cos(b), a * sin(b), sqrt(1.0f - u.x));
float3x3 basis = OrthoBasisFromVector(normal);
return mul(basis, localDir);
}
float3 UniformHemisphereSample(float2 u, float3 normal)
{
float z = u[0];
float r = sqrt(max(0.0f, 1.0f - z * z));
float phi = 2.0f * PI * u[1];
float3 localDir = float3(r * cos(phi), r * sin(phi), z);
float3x3 basis = OrthoBasisFromVector(normal);
return mul(basis, localDir);
}
float PowerHeuristic(float f, float b)
{
float q = (f * f) + (b * b);
return q > 0 ? (f * f) / q : 0;
}
float UintToFloat01(uint x)
{
return min(x * 2.3283064365386963e-10, FLOAT_ONE_MINUS_EPSILON); // (1.f / (1ULL << 32));
}
int Log2Int(uint v)
{
return firstbithigh(v);
}
int Log2IntUp(uint v)
{
int n = Log2Int(v);
return v > (1u << n) ? n + 1 : n;
}
#endif
@@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: 91796a9618276ff42930ee05801207aa
ShaderIncludeImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,148 @@
#ifndef _SAMPLING_HASHES_HLSL_
#define _SAMPLING_HASHES_HLSL_
// Low bias hash from https://github.com/skeeto/hash-prospector
uint LowBiasHash32(uint x, uint seed = 0)
{
x += seed;
x ^= x >> 16;
x *= 0x21f0aaad;
x ^= x >> 15;
x *= 0xd35a2d97;
x ^= x >> 15;
return x;
}
// Murmur Hash from https://github.com/aappleby/smhasher/wiki/MurmurHash3
uint MurmurAdd(uint hash, uint item)
{
item *= 0xcc9e2d51;
item = (item << 15) | (item >> 17);
item *= 0x1b873593;
hash ^= item;
hash = (hash << 13) | (hash >> 19);
hash = hash * 5 + 0xe6546b64;
return hash;
}
uint MurmurFinalize(uint hash)
{
hash ^= hash >> 16;
hash *= 0x85ebca6b;
hash ^= hash >> 13;
hash *= 0xc2b2ae35;
hash ^= hash >> 16;
return hash;
}
uint MurmurHash(uint x, uint seed = 0)
{
uint h = seed;
h = MurmurAdd(h, x);
return MurmurFinalize(h);
}
uint MurmurHash(uint2 x, uint seed = 0)
{
uint h = seed;
h = MurmurAdd(h, x.x);
h = MurmurAdd(h, x.y);
return MurmurFinalize(h);
}
uint MurmurHash(uint3 x, uint seed = 0)
{
uint h = seed;
h = MurmurAdd(h, x.x);
h = MurmurAdd(h, x.y);
h = MurmurAdd(h, x.z);
return MurmurFinalize(h);
}
uint MurmurHash(uint4 x, uint seed = 0)
{
uint h = seed;
h = MurmurAdd(h, x.x);
h = MurmurAdd(h, x.y);
h = MurmurAdd(h, x.z);
h = MurmurAdd(h, x.w);
return MurmurFinalize(h);
}
uint XorShift32(uint rngState)
{
rngState ^= rngState << 13;
rngState ^= rngState >> 17;
rngState ^= rngState << 5;
return rngState;
}
// From PCG: A Family of Simple Fast Space-Efficient Statistically Good Algorithms for Random Number Generation.
// and "Hash Functions for GPU Rendering" paper
uint Pcg(uint v)
{
uint state = v * 747796405u + 2891336453u;
uint word = ((state >> ((state >> 28u) + 4u)) ^ state) * 277803737u;
return (word >> 22u) ^ word;
}
uint2 Pcg2d(uint2 v)
{
v = v * 1664525u + 1013904223u;
v.x += v.y * 1664525u;
v.y += v.x * 1664525u;
v = v ^ (v >> 16u);
v.x += v.y * 1664525u;
v.y += v.x * 1664525u;
v = v ^ (v >> 16u);
return v;
}
uint3 Pcg3d(uint3 v)
{
v = v * 1664525u + 1013904223u;
v.x += v.y * v.z;
v.y += v.z * v.x;
v.z += v.x * v.y;
v ^= v >> 16u;
v.x += v.y * v.z;
v.y += v.z * v.x;
v.z += v.x * v.y;
return v;
}
uint4 Pcg4d(uint4 v)
{
v = v * 1664525u + 1013904223u;
v.x += v.y * v.w;
v.y += v.z * v.x;
v.z += v.x * v.y;
v.w += v.y * v.z;
v = v ^ (v >> 16u);
v.x += v.y * v.w;
v.y += v.z * v.x;
v.z += v.x * v.y;
v.w += v.y * v.z;
return v;
}
uint PixelHash(uint2 pixelCoord, uint seed = 0)
{
return LowBiasHash32((pixelCoord.x & 0xFFFF) | (pixelCoord.y << 16), seed);
}
#endif // _SAMPLING_HASHES_HLSL_
@@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: ba9242b5d88ccf2489a51d94185a1300
ShaderIncludeImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,71 @@
#ifndef _SAMPLING_PSEUDORANDOM_HLSL_
#define _SAMPLING_PSEUDORANDOM_HLSL_
#include "Common.hlsl"
#include "Hashes.hlsl"
// Xor shift PRNG
struct QrngXorShift
{
uint state;
void Init(uint2 pixelCoord, uint startSampleIndex)
{
state = PixelHash(pixelCoord, startSampleIndex);
}
void Init(uint seed, uint startSampleIndex)
{
state = seed;
}
float GetSample()
{
state = XorShift32(state);
return UintToFloat01(state);
}
float2 GetSample(uint dimension)
{
return float2(GetSample(), GetSample());
}
void NextSample()
{
}
};
// From paper: "Hash Functions for GPU Rendering" by Jarzynski & Olano)
struct QrngPcg4D
{
uint4 state;
void Init(uint2 pixelCoord, uint startSampleIndex)
{
// Seed for PCG uses a sequential sample number in 4th channel, which increments on every RNG call and starts from 0
state = uint4(pixelCoord, startSampleIndex, 0);
}
void Init(uint seed, uint startSampleIndex)
{
state = uint4(seed, 1, startSampleIndex, 0);
}
float GetSample()
{
state.w++;
return UintToFloat01(Pcg4d(state).x);
}
float2 GetSample(uint dimension)
{
return float2(GetSample(), GetSample());
}
void NextSample()
{
state.z++;
}
};
#endif // _SAMPLING_PSEUDORANDOM_HLSL_
@@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: 270be7a3bca5b4e038d7e90c1ca0f18c
ShaderIncludeImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,197 @@
#ifndef _SAMPLING_QUASIRANDOM_HLSL_
#define _SAMPLING_QUASIRANDOM_HLSL_
#include "Common.hlsl"
#include "SobolSampling.hlsl"
#include "SobolBluenoiseSampling.hlsl"
static const uint kMaxSobolDim = SOBOL_MATRICES_COUNT;
// Sobol sampler with Owen scrambling, from paper: Practical Hash-based Owen Scrambling by Burley
// infinite dims, 2097151 max samples, pixel tiling wraps at 65536
// Define QRNG_SOBOL_02 to only use first 2 sobol dims and rely on scrambling for the others, this
// effectively makes every pair of dims a perfect (0,2) sequence
struct QrngSobol2D
{
uint pixelSeed;
uint sampleIndex;
void Init(uint2 pixelCoord, uint startSampleIndex)
{
Init(PixelHash(pixelCoord), startSampleIndex);
}
void Init(uint seed, uint startSampleIndex)
{
pixelSeed = seed;
sampleIndex = startSampleIndex;
}
float PrivateGetFloat(uint dimension)
{
#ifdef QRNG_SOBOL_02
uint index = NestedUniformOwenScramble(sampleIndex, pixelSeed ^ ( dimension / 2));
return GetOwenScrambledSobolSample(index, dimension & 1, LowBiasHash32(dimension, pixelSeed));
#else
uint scrambleSeed = LowBiasHash32(pixelSeed, dimension);
uint shuffleSeed = pixelSeed;
return GetOwenScrambledSobolSample(sampleIndex ^ shuffleSeed, dimension % kMaxSobolDim, scrambleSeed);
#endif
}
float2 GetSample(uint dimension)
{
return float2(PrivateGetFloat(dimension*2), PrivateGetFloat(dimension*2+1));
}
void NextSample()
{
sampleIndex++;
}
};
// From paper: "A Low-Discrepancy Sampler that Distributes Monte Carlo Errors as a Blue Noise in Screen Space" by Heitz and Belcour
// 256 max dims, 256 max samples (beyond 256, the sequence keeps going with another set of 256 samples belonging to another dim, and so on every 256 samples), pixel tiling wraps at 128
struct QrngSobolBlueNoise2D
{
uint2 pixelCoord;
uint sampleIndex;
void Init(uint2 pixelCoord_, uint startSampleIndex)
{
pixelCoord = pixelCoord_;
sampleIndex = startSampleIndex;
}
void Init(uint seed, uint startSampleIndex)
{
Init(seed / 256, seed % 256);
}
float PrivateGetFloat(uint dimension)
{
// If we go past the number of stored samples per dim, just shift all to the next pair of dimensions
dimension += (sampleIndex / 256) * 2;
return GetBNDSequenceSample(pixelCoord, sampleIndex, dimension);
}
float2 GetSample(uint dimension)
{
return float2(PrivateGetFloat(dimension*2), PrivateGetFloat(dimension*2+1));
}
void NextSample()
{
sampleIndex++;
}
};
// From paper: "Screen-Space Blue-Noise Diffusion of Monte Carlo Sampling Error via Hierarchical Ordering of Pixels" by Ahmed and Wonka
// infinite dims and samples, pixel tiling depends on target sample count. The more samples, the smaller the tile (ex: for 256 samples, tiling size is 4096)
// define QRNG_GLOBAL_SOBOL_ENHANCED_TILING to get tiling to always wrap at 65536
// Define QRNG_SOBOL_02 to only use first 2 sobol dims and rely on scrambling for the others
struct QrngGlobalSobolBlueNoise2D
{
uint pixelMortonCode;
uint log2SamplesPerPixel;
uint sampleIndex;
void Init(uint2 pixelCoord, uint startSampleIndex, uint perPixelSampleCount = 256)
{
pixelMortonCode = EncodeMorton2D(pixelCoord);
log2SamplesPerPixel = Log2IntUp(perPixelSampleCount);
sampleIndex = startSampleIndex;
}
void Init(uint seed, uint startSampleIndex, uint perPixelSampleCount = 256)
{
Init(uint2(seed & 0xFFFF, seed >> 16), startSampleIndex, perPixelSampleCount);
}
float PrivateGetFloat(uint dimension)
{
#ifdef QRNG_SOBOL_02
uint index = NestedUniformOwenScramble(sampleIndex, LowBiasHash32(dimension/2, 0xe0aaaf75)) & ((1U << log2SamplesPerPixel) - 1U);
return GetOwenScrambledZShuffledSobolSample(index, dimension, 2, pixelMortonCode, log2SamplesPerPixel);
#else
return GetOwenScrambledZShuffledSobolSample(sampleIndex, dimension, kMaxSobolDim, pixelMortonCode, log2SamplesPerPixel);
#endif
}
float2 GetSample(uint dimension)
{
return float2(PrivateGetFloat(dimension*2), PrivateGetFloat(dimension*2+1));
}
void NextSample()
{
sampleIndex++;
}
};
// Kronecker sequence from paper "Optimizing Kronecker Sequences for Multidimensional Sampling"
//fast but lower quality than Sobol, infinite dims and samples, pixel tiling wraps at 65536
//define QRNG_KRONECKER_ENHANCED_QUALITY to add small scale jitter
struct QrngKronecker2D
{
uint cranleyPattersonSeed;
uint shuffledSampleIndex;
#ifdef QRNG_KRONECKER_ENHANCED_QUALITY
int sampleIndex;
#endif
void Init(uint2 pixelCoord, uint startSampleIndex)
{
Init(PixelHash(pixelCoord), startSampleIndex);
}
void Init(uint seed, uint startSampleIndex)
{
cranleyPattersonSeed = seed;
uint shuffledStartIndex = (startSampleIndex + seed) % (1 << 20);
shuffledSampleIndex = shuffledStartIndex;
#ifdef QRNG_KRONECKER_ENHANCED_QUALITY
sampleIndex = startSampleIndex+1;
#endif
}
float PrivateGetFloat(uint dimension)
{
const uint alphas[]= { // values are stored multiplied by (1 << 32)
// R2 from http://extremelearning.com.au/unreasonable-effectiveness-of-quasirandom-sequences/
3242174889, 2447445414,
// K21_2 from Optimizing Kronecker Sequences for Multidimensional Sampling
3316612456, 1538627358,
};
// compute random offset to apply to the sequence (using another Kronecker sequence)
uint cranleyPattersonRot = cranleyPattersonSeed + 3646589397 * (dimension / 4);
#ifdef QRNG_KRONECKER_ENHANCED_QUALITY // add small scale jitter as explained in paper
const float alphaJitter[] = { 2681146017, 685201898 };
uint jitter = alphaJitter[dimension % 2] * shuffledSampleIndex;
float amplitude = 0.05 * 0.78 / sqrt(2) * rsqrt(float(sampleIndex));
cranleyPattersonRot += jitter * uint(amplitude);
#endif
// Kronecker sequence evaluation
return UintToFloat01(cranleyPattersonRot + alphas[dimension % 4] * shuffledSampleIndex);
}
float2 GetSample(uint dimension)
{
return float2(PrivateGetFloat(dimension*2), PrivateGetFloat(dimension*2+1));
}
void NextSample()
{
// shuffledSampleIndex modulo 1048576 to avoid numerical precision issues when evaluating the Kronecker sequence
shuffledSampleIndex = (shuffledSampleIndex + 1) % (1 << 20);
#ifdef QRNG_KRONECKER_ENHANCED_QUALITY
sampleIndex++;
#endif
}
};
#endif // _SAMPLING_QUASIRANDOM_HLSL_
@@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: 6dd45a9bd35347a45b6cb0047cab846f
ShaderIncludeImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,70 @@
using System;
using System.Runtime.InteropServices;
#if UNITY_EDITOR
using UnityEditor;
#endif
namespace UnityEngine.Rendering.Sampling
{
internal sealed class SamplingResources : IDisposable
{
internal enum ResourceType
{
BlueNoiseTextures = 1,
SobolMatrices = 2,
All = BlueNoiseTextures | SobolMatrices
};
private Texture2D m_SobolScramblingTile;
private Texture2D m_SobolRankingTile;
private Texture2D m_SobolOwenScrambled256Samples;
private GraphicsBuffer m_SobolBuffer;
static internal readonly uint[] sobolMatrices = SobolData.SobolMatrices;
#if UNITY_EDITOR
public void Load(uint resourceBitmask = (uint)ResourceType.BlueNoiseTextures)
{
if ((resourceBitmask & (uint)ResourceType.BlueNoiseTextures) != 0)
{
const string path = "Packages/com.unity.render-pipelines.core/Runtime/";
m_SobolScramblingTile = AssetDatabase.LoadAssetAtPath<Texture2D>(path + "Sampling/Textures/SobolBlueNoise/ScramblingTile256SPP.png");
m_SobolRankingTile = AssetDatabase.LoadAssetAtPath<Texture2D>(path + "Sampling/Textures/SobolBlueNoise/RankingTile256SPP.png");
m_SobolOwenScrambled256Samples = AssetDatabase.LoadAssetAtPath<Texture2D>(path + "Sampling/Textures/SobolBlueNoise/SobolOwenScrambled256.png");
}
if ((resourceBitmask & (uint)ResourceType.SobolMatrices) != 0)
{
int sobolBufferSize = (int)(SobolData.SobolDims * SobolData.SobolSize);
m_SobolBuffer = new GraphicsBuffer(GraphicsBuffer.Target.Structured, sobolBufferSize, Marshal.SizeOf<uint>());
m_SobolBuffer.SetData(SobolData.SobolMatrices);
}
}
#endif
public static void Bind(CommandBuffer cmd, SamplingResources resources)
{
if (resources.m_SobolScramblingTile != null)
{
cmd.SetGlobalTexture(Shader.PropertyToID("_SobolScramblingTile"), resources.m_SobolScramblingTile);
cmd.SetGlobalTexture(Shader.PropertyToID("_SobolRankingTile"), resources.m_SobolRankingTile);
cmd.SetGlobalTexture(Shader.PropertyToID("_SobolOwenScrambledSequence"), resources.m_SobolOwenScrambled256Samples);
}
if (resources.m_SobolBuffer != null)
cmd.SetGlobalBuffer("_SobolMatricesBuffer", resources.m_SobolBuffer);
}
public void Dispose()
{
m_SobolBuffer?.Dispose();
}
}
}
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: e664c9077c60b314a80e696796bfcecb
@@ -0,0 +1,11 @@
#ifndef _SAMPLING_SAMPLINGRESOURCES_HLSL_
#define _SAMPLING_SAMPLINGRESOURCES_HLSL_
Texture2D<float> _SobolScramblingTile;
Texture2D<float> _SobolRankingTile;
Texture2D<float2> _SobolOwenScrambledSequence;
StructuredBuffer<uint> _SobolMatricesBuffer;
#endif
@@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: a4f17041120a6f24c8d78d484c813aec
ShaderIncludeImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,31 @@
#ifndef _SAMPLING_SOBOLBLUENOISESAMPLING_HLSL_
#define _SAMPLING_SOBOLBLUENOISESAMPLING_HLSL_
#include "SamplingResources.hlsl"
// This is an implementation of the method from the paper
// "A Low-Discrepancy Sampler that Distributes Monte Carlo Errors as a Blue Noise in Screen Space" by Heitz et al.
float GetBNDSequenceSample(uint2 pixelCoord, uint sampleIndex, uint sampleDimension)
{
// wrap arguments
pixelCoord = pixelCoord & 127;
sampleIndex = sampleIndex & 255;
sampleDimension = sampleDimension & 255;
// xor index based on optimized ranking
uint rankingIndex = (pixelCoord.x + pixelCoord.y * 128) * 8 + (sampleDimension & 7);
uint rankedSampleIndex = sampleIndex ^ clamp((uint)(_SobolRankingTile[uint2(rankingIndex & 127, rankingIndex / 128)] * 256.0), 0, 255);
// fetch value in sequence
uint value = clamp((uint)(_SobolOwenScrambledSequence[uint2(sampleDimension, rankedSampleIndex.x)] * 256.0), 0, 255);
// If the dimension is optimized, xor sequence value based on optimized scrambling
uint scramblingIndex = (pixelCoord.x + pixelCoord.y * 128) * 8 + (sampleDimension & 7);
float scramblingValue = min(_SobolScramblingTile[uint2(scramblingIndex & 127, scramblingIndex / 128)], 0.999);
value = value ^ uint(scramblingValue * 256.0);
// Convert to float (to avoid the same 1/256th quantization everywhere, we jitter by the pixel scramblingValue)
return min((max(0.001, scramblingValue) + value) / 256.0, FLOAT_ONE_MINUS_EPSILON);
}
#endif // _SAMPLING_SOBOLBLUENOISESAMPLING_HLSL_
@@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: 563a0665ce2f07e419c80acd1ad6e1f0
ShaderIncludeImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 3855c9e5913bd1e4f84f3bb238dc6c41
@@ -0,0 +1,169 @@
#ifndef _SAMPLING_SOBOLSAMPLING_HLSL_
#define _SAMPLING_SOBOLSAMPLING_HLSL_
#define SOBOL_MATRIX_SIZE 52
#define SOBOL_MATRICES_COUNT 1024
#include "SamplingResources.hlsl"
#include "Hashes.hlsl"
#include "Common.hlsl"
// HLSLcc cannot correctly translate `reversebits(x)` for large unsigned integers.
// Therefore, when using HLSLcc, we use our own implementation. https://jira.unity3d.com/browse/GFXFEAT-629
#ifdef UNITY_COMPILER_HLSLCC
uint ReverseBitsSafe(uint x)
{
x = ((x >> 1) & 0x55555555u) | ((x & 0x55555555u) << 1);
x = ((x >> 2) & 0x33333333u) | ((x & 0x33333333u) << 2);
x = ((x >> 4) & 0x0f0f0f0fu) | ((x & 0x0f0f0f0fu) << 4);
x = ((x >> 8) & 0x00ff00ffu) | ((x & 0x00ff00ffu) << 8);
x = ((x >> 16) & 0xffffu) | ((x & 0xffffu) << 16);
return x;
}
#else
#define ReverseBitsSafe reversebits
#endif
// See https://psychopath.io/post/2021_01_30_building_a_better_lk_hash
uint LaineKarrasPermutation(uint x, uint seed)
{
x ^= x * 0x3d20adea;
x += seed;
x *= (seed >> 16) | 1;
x ^= x * 0x05526c56;
x ^= x * 0x53a22864;
return x;
}
uint NestedUniformOwenScramble(uint x, uint seed)
{
x = ReverseBitsSafe(x);
x = LaineKarrasPermutation(x, seed);
x = ReverseBitsSafe(x);
return x;
}
//See https://psychopath.io/post/2022_08_14_a_fast_hash_for_base_4_owen_scrambling
uint LaineKarrasStylePermutationBase4(uint x, uint seed)
{
x ^= x * 0x3d20adeau;
x ^= (x >> 1) & (x << 1) & 0x55555555u;
x += seed;
x *= (seed >> 16) | 1;
x ^= (x >> 1) & (x << 1) & 0x55555555u;
x ^= x * 0x05526c56u;
x ^= x * 0x53a22864u;
return x;
}
uint NestedUniformScrambleBase4(uint x, uint seed)
{
x = ReverseBitsSafe(x);
x = LaineKarrasStylePermutationBase4(x, seed);
x = ReverseBitsSafe(x);
return x;
}
// "Insert" a 0 bit after each of the 16 low bits of x.
// Ref: https://fgiesen.wordpress.com/2009/12/13/decoding-morton-codes/
uint Part1By1(uint x)
{
x &= 0x0000ffff; // x = ---- ---- ---- ---- fedc ba98 7654 3210
x = (x ^ (x << 8)) & 0x00ff00ff; // x = ---- ---- fedc ba98 ---- ---- 7654 3210
x = (x ^ (x << 4)) & 0x0f0f0f0f; // x = ---- fedc ---- ba98 ---- 7654 ---- 3210
x = (x ^ (x << 2)) & 0x33333333; // x = --fe --dc --ba --98 --76 --54 --32 --10
x = (x ^ (x << 1)) & 0x55555555; // x = -f-e -d-c -b-a -9-8 -7-6 -5-4 -3-2 -1-0
return x;
}
uint EncodeMorton2D(uint2 coord)
{
return (Part1By1(coord.y) << 1) + Part1By1(coord.x);
}
uint SobolSampleUint(uint index, int dimension, uint indexMaxBitCount)
{
uint result = 0;
#ifdef UNIFIED_RT_BACKEND_HARDWARE
// Raytracing tracing shaders compile only with unrolled loop, but unrolling fails in the compute path.
// TODO: test again when Unity updates the shader compiler
[unroll]
#endif
for (uint i = dimension * SOBOL_MATRIX_SIZE; i < dimension * SOBOL_MATRIX_SIZE + indexMaxBitCount; index >>= 1, ++i)
{
result ^= _SobolMatricesBuffer[i] * (index & 1);
}
return result;
}
uint SobolSampleUint(uint2 index64, uint dimension)
{
uint result = 0;
uint i = dimension * SOBOL_MATRIX_SIZE;
for (; i < dimension * SOBOL_MATRIX_SIZE + 32; index64.x >>= 1, ++i)
{
result ^= _SobolMatricesBuffer[i] * (index64.x & 1);
}
for (; i < dimension * SOBOL_MATRIX_SIZE + 32+20; index64.y >>= 1, ++i)
{
result ^= _SobolMatricesBuffer[i] * (index64.y & 1);
}
return result;
}
uint GetMortonShuffledSampleIndex(uint index, uint pixelMortonIndex, uint mortonShuffleSeed, uint log2SamplesPerPixel)
{
uint mortonIndexShuffled = NestedUniformScrambleBase4(pixelMortonIndex, mortonShuffleSeed);
uint sampleIndexForPixel = (mortonIndexShuffled << log2SamplesPerPixel) | index;
return sampleIndexForPixel;
}
uint2 GetMortonShuffledSampleIndex64(uint index, uint pixelMortonIndex, uint mortonShuffleSeed, uint log2SamplesPerPixel)
{
uint mortonIndexShuffled = NestedUniformScrambleBase4(pixelMortonIndex, mortonShuffleSeed);
uint2 sampleIndex64;
sampleIndex64.x = (mortonIndexShuffled << log2SamplesPerPixel) | index;
sampleIndex64.y = mortonIndexShuffled >> (32 - log2SamplesPerPixel);
return sampleIndex64;
}
float GetSobolSample(uint index, int dimension)
{
uint result = SobolSampleUint(index, dimension, 20);
return UintToFloat01(result);
}
float GetOwenScrambledSobolSample(uint sampleIndex, uint dim, uint valueScrambleSeed)
{
uint sobolUInt = SobolSampleUint(sampleIndex, dim, 20);
uint result = NestedUniformOwenScramble(sobolUInt, valueScrambleSeed);
return UintToFloat01(result);
}
float GetOwenScrambledZShuffledSobolSample(uint sampleIndex, uint dim, uint maxDim, uint pixelMortonCode, uint log2SamplesPerPixel)
{
const uint kSeedValueScramble = 0xab773au;
uint mortonShuffleSeed = LowBiasHash32(dim, 0);
uint valueScrambleSeed = LowBiasHash32(dim, kSeedValueScramble);
#ifdef QRNG_GLOBAL_SOBOL_ENHANCED_TILING
uint2 shuffledSampleIndex = GetMortonShuffledSampleIndex64(sampleIndex, pixelMortonCode, mortonShuffleSeed, log2SamplesPerPixel);
uint sobolUInt = SobolSampleUint(shuffledSampleIndex, dim % maxDim);
#else
uint shuffledSampleIndex = GetMortonShuffledSampleIndex(sampleIndex, pixelMortonCode, mortonShuffleSeed, log2SamplesPerPixel);
uint sobolUInt = SobolSampleUint(shuffledSampleIndex, dim % maxDim, 32);
#endif
uint result = NestedUniformOwenScramble(sobolUInt, valueScrambleSeed);
return UintToFloat01(result);
}
#endif // UNITY_SOBOL_SAMPLING_INCLUDED
@@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: 8a9360b3acf7c304cbcb5a54b9b89409
ShaderIncludeImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 837c8a5059e05e74f8f9b8cbb40c6fa1
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 5cd9d26d95a75804d8f849a19ab998e7
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,127 @@
fileFormatVersion: 2
guid: adf003e8db5aef64fa57c155f2e599be
TextureImporter:
internalIDToNameTable: []
externalObjects: {}
serializedVersion: 10
mipmaps:
mipMapMode: 0
enableMipMap: 0
sRGBTexture: 0
linearTexture: 0
fadeOut: 0
borderMipMap: 0
mipMapsPreserveCoverage: 0
alphaTestReferenceValue: 0.5
mipMapFadeDistanceStart: 1
mipMapFadeDistanceEnd: 3
bumpmap:
convertToNormalMap: 0
externalNormalMap: 0
heightScale: 0.25
normalMapFilter: 0
isReadable: 0
streamingMipmaps: 0
streamingMipmapsPriority: 0
grayScaleToAlpha: 0
generateCubemap: 6
cubemapConvolution: 0
seamlessCubemap: 0
textureFormat: 1
maxTextureSize: 2048
textureSettings:
serializedVersion: 2
filterMode: 0
aniso: -1
mipBias: -100
wrapU: 1
wrapV: 1
wrapW: 1
nPOTScale: 1
lightmap: 0
compressionQuality: 50
spriteMode: 0
spriteExtrude: 1
spriteMeshType: 1
alignment: 0
spritePivot: {x: 0.5, y: 0.5}
spritePixelsToUnits: 100
spriteBorder: {x: 0, y: 0, z: 0, w: 0}
spriteGenerateFallbackPhysicsShape: 1
alphaUsage: 0
alphaIsTransparency: 0
spriteTessellationDetail: -1
textureType: 0
textureShape: 1
singleChannelComponent: 0
maxTextureSizeSet: 0
compressionQualitySet: 0
textureFormatSet: 0
platformSettings:
- serializedVersion: 3
buildTarget: DefaultTexturePlatform
maxTextureSize: 2048
resizeAlgorithm: 0
textureFormat: -1
textureCompression: 0
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
androidETC2FallbackOverride: 0
forceMaximumCompressionQuality_BC6H_BC7: 0
- serializedVersion: 3
buildTarget: Standalone
maxTextureSize: 2048
resizeAlgorithm: 0
textureFormat: -1
textureCompression: 0
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
androidETC2FallbackOverride: 0
forceMaximumCompressionQuality_BC6H_BC7: 0
- serializedVersion: 3
buildTarget: Windows Store Apps
maxTextureSize: 2048
resizeAlgorithm: 0
textureFormat: -1
textureCompression: 0
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
androidETC2FallbackOverride: 0
forceMaximumCompressionQuality_BC6H_BC7: 0
- serializedVersion: 3
buildTarget: PS4
maxTextureSize: 2048
resizeAlgorithm: 0
textureFormat: -1
textureCompression: 0
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
androidETC2FallbackOverride: 0
forceMaximumCompressionQuality_BC6H_BC7: 0
spriteSheet:
serializedVersion: 2
sprites: []
outline: []
physicsShape: []
bones: []
spriteID:
internalID: 0
vertices: []
indices:
edges: []
weights: []
secondaryTextures: []
spritePackingTag:
pSDRemoveMatte: 0
pSDShowRemoveMatteOption: 0
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,127 @@
fileFormatVersion: 2
guid: 11fcf3c83299e814cbb25c4d74cd045d
TextureImporter:
internalIDToNameTable: []
externalObjects: {}
serializedVersion: 10
mipmaps:
mipMapMode: 0
enableMipMap: 0
sRGBTexture: 0
linearTexture: 0
fadeOut: 0
borderMipMap: 0
mipMapsPreserveCoverage: 0
alphaTestReferenceValue: 0.5
mipMapFadeDistanceStart: 1
mipMapFadeDistanceEnd: 3
bumpmap:
convertToNormalMap: 0
externalNormalMap: 0
heightScale: 0.25
normalMapFilter: 0
isReadable: 0
streamingMipmaps: 0
streamingMipmapsPriority: 0
grayScaleToAlpha: 0
generateCubemap: 6
cubemapConvolution: 0
seamlessCubemap: 0
textureFormat: 1
maxTextureSize: 2048
textureSettings:
serializedVersion: 2
filterMode: 0
aniso: -1
mipBias: -100
wrapU: 1
wrapV: 1
wrapW: 1
nPOTScale: 1
lightmap: 0
compressionQuality: 50
spriteMode: 0
spriteExtrude: 1
spriteMeshType: 1
alignment: 0
spritePivot: {x: 0.5, y: 0.5}
spritePixelsToUnits: 100
spriteBorder: {x: 0, y: 0, z: 0, w: 0}
spriteGenerateFallbackPhysicsShape: 1
alphaUsage: 0
alphaIsTransparency: 0
spriteTessellationDetail: -1
textureType: 0
textureShape: 1
singleChannelComponent: 0
maxTextureSizeSet: 0
compressionQualitySet: 0
textureFormatSet: 0
platformSettings:
- serializedVersion: 3
buildTarget: DefaultTexturePlatform
maxTextureSize: 2048
resizeAlgorithm: 0
textureFormat: -1
textureCompression: 0
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
androidETC2FallbackOverride: 0
forceMaximumCompressionQuality_BC6H_BC7: 0
- serializedVersion: 3
buildTarget: Standalone
maxTextureSize: 2048
resizeAlgorithm: 0
textureFormat: -1
textureCompression: 0
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
androidETC2FallbackOverride: 0
forceMaximumCompressionQuality_BC6H_BC7: 0
- serializedVersion: 3
buildTarget: Windows Store Apps
maxTextureSize: 2048
resizeAlgorithm: 0
textureFormat: -1
textureCompression: 0
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
androidETC2FallbackOverride: 0
forceMaximumCompressionQuality_BC6H_BC7: 0
- serializedVersion: 3
buildTarget: PS4
maxTextureSize: 2048
resizeAlgorithm: 0
textureFormat: -1
textureCompression: 0
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
androidETC2FallbackOverride: 0
forceMaximumCompressionQuality_BC6H_BC7: 0
spriteSheet:
serializedVersion: 2
sprites: []
outline: []
physicsShape: []
bones: []
spriteID:
internalID: 0
vertices: []
indices:
edges: []
weights: []
secondaryTextures: []
spritePackingTag:
pSDRemoveMatte: 0
pSDShowRemoveMatteOption: 0
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,242 @@
fileFormatVersion: 2
guid: 8d786511913a2cb4aacb1f9a188652b4
TextureImporter:
internalIDToNameTable: []
externalObjects: {}
serializedVersion: 11
mipmaps:
mipMapMode: 0
enableMipMap: 1
sRGBTexture: 0
linearTexture: 0
fadeOut: 0
borderMipMap: 0
mipMapsPreserveCoverage: 0
alphaTestReferenceValue: 0.5
mipMapFadeDistanceStart: 1
mipMapFadeDistanceEnd: 3
bumpmap:
convertToNormalMap: 0
externalNormalMap: 0
heightScale: 0.25
normalMapFilter: 0
isReadable: 0
streamingMipmaps: 0
streamingMipmapsPriority: 0
vTOnly: 0
ignoreMasterTextureLimit: 0
grayScaleToAlpha: 0
generateCubemap: 6
cubemapConvolution: 0
seamlessCubemap: 0
textureFormat: 1
maxTextureSize: 2048
textureSettings:
serializedVersion: 2
filterMode: 1
aniso: 1
mipBias: 0
wrapU: 0
wrapV: 0
wrapW: 0
nPOTScale: 1
lightmap: 0
compressionQuality: 50
spriteMode: 0
spriteExtrude: 1
spriteMeshType: 1
alignment: 0
spritePivot: {x: 0.5, y: 0.5}
spritePixelsToUnits: 100
spriteBorder: {x: 0, y: 0, z: 0, w: 0}
spriteGenerateFallbackPhysicsShape: 1
alphaUsage: 1
alphaIsTransparency: 0
spriteTessellationDetail: -1
textureType: 0
textureShape: 1
singleChannelComponent: 0
flipbookRows: 1
flipbookColumns: 1
maxTextureSizeSet: 0
compressionQualitySet: 0
textureFormatSet: 0
ignorePngGamma: 0
applyGammaDecoding: 0
platformSettings:
- serializedVersion: 3
buildTarget: DefaultTexturePlatform
maxTextureSize: 2048
resizeAlgorithm: 0
textureFormat: -1
textureCompression: 0
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
androidETC2FallbackOverride: 0
forceMaximumCompressionQuality_BC6H_BC7: 0
- serializedVersion: 3
buildTarget: Standalone
maxTextureSize: 2048
resizeAlgorithm: 0
textureFormat: -1
textureCompression: 1
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
androidETC2FallbackOverride: 0
forceMaximumCompressionQuality_BC6H_BC7: 0
- serializedVersion: 3
buildTarget: Server
maxTextureSize: 2048
resizeAlgorithm: 0
textureFormat: -1
textureCompression: 1
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
androidETC2FallbackOverride: 0
forceMaximumCompressionQuality_BC6H_BC7: 0
- serializedVersion: 3
buildTarget: Android
maxTextureSize: 2048
resizeAlgorithm: 0
textureFormat: -1
textureCompression: 1
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
androidETC2FallbackOverride: 0
forceMaximumCompressionQuality_BC6H_BC7: 0
- serializedVersion: 3
buildTarget: PS4
maxTextureSize: 2048
resizeAlgorithm: 0
textureFormat: -1
textureCompression: 1
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
androidETC2FallbackOverride: 0
forceMaximumCompressionQuality_BC6H_BC7: 0
- serializedVersion: 3
buildTarget: PS5
maxTextureSize: 2048
resizeAlgorithm: 0
textureFormat: -1
textureCompression: 1
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
androidETC2FallbackOverride: 0
forceMaximumCompressionQuality_BC6H_BC7: 0
- serializedVersion: 3
buildTarget: GameCoreXboxOne
maxTextureSize: 2048
resizeAlgorithm: 0
textureFormat: -1
textureCompression: 1
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
androidETC2FallbackOverride: 0
forceMaximumCompressionQuality_BC6H_BC7: 0
- serializedVersion: 3
buildTarget: GameCoreScarlett
maxTextureSize: 2048
resizeAlgorithm: 0
textureFormat: -1
textureCompression: 1
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
androidETC2FallbackOverride: 0
forceMaximumCompressionQuality_BC6H_BC7: 0
- serializedVersion: 3
buildTarget: WebGL
maxTextureSize: 2048
resizeAlgorithm: 0
textureFormat: -1
textureCompression: 1
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
androidETC2FallbackOverride: 0
forceMaximumCompressionQuality_BC6H_BC7: 0
- serializedVersion: 3
buildTarget: Windows Store Apps
maxTextureSize: 2048
resizeAlgorithm: 0
textureFormat: -1
textureCompression: 1
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
androidETC2FallbackOverride: 0
forceMaximumCompressionQuality_BC6H_BC7: 0
- serializedVersion: 3
buildTarget: Stadia
maxTextureSize: 2048
resizeAlgorithm: 0
textureFormat: -1
textureCompression: 1
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
androidETC2FallbackOverride: 0
forceMaximumCompressionQuality_BC6H_BC7: 0
- serializedVersion: 3
buildTarget: Lumin
maxTextureSize: 2048
resizeAlgorithm: 0
textureFormat: -1
textureCompression: 1
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
androidETC2FallbackOverride: 0
forceMaximumCompressionQuality_BC6H_BC7: 0
- serializedVersion: 3
buildTarget: EmbeddedLinux
maxTextureSize: 2048
resizeAlgorithm: 0
textureFormat: -1
textureCompression: 1
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
androidETC2FallbackOverride: 0
forceMaximumCompressionQuality_BC6H_BC7: 0
spriteSheet:
serializedVersion: 2
sprites: []
outline: []
physicsShape: []
bones: []
spriteID:
internalID: 0
vertices: []
indices:
edges: []
weights: []
secondaryTextures: []
nameFileIdTable: {}
spritePackingTag:
pSDRemoveMatte: 0
pSDShowRemoveMatteOption: 0
userData:
assetBundleName:
assetBundleVariant: