浏览代码

Merge pull request #600 from Unity-Technologies/feature/BC6HOnGPU

BC6H Compression on GPU
/main
GitHub 7 年前
当前提交
8e4cb10b
共有 27 个文件被更改,包括 1264 次插入0 次删除
  1. 57
      ScriptableRenderPipeline/Core/ShaderLibrary/Sampling.hlsl
  2. 8
      SampleScenes/Common/Materials.meta
  3. 91
      SampleScenes/Common/Scripts/CompressBC6HAndDisplay.cs
  4. 11
      SampleScenes/Common/Scripts/CompressBC6HAndDisplay.cs.meta
  5. 8
      SampleScenes/Common/Shaders.meta
  6. 77
      SampleScenes/Common/Textures/Alexs_Apt_2k.hdr.meta
  7. 139
      SampleScenes/Common/Textures/Alexs_Apt_2k_cube.hdr.meta
  8. 8
      SampleScenes/CoreTest.meta
  9. 122
      ScriptableRenderPipeline/Core/EncodeBC6H.cs
  10. 13
      ScriptableRenderPipeline/Core/EncodeBC6H.cs.meta
  11. 49
      ScriptableRenderPipeline/Core/Resources/EncodeBC6H.compute
  12. 10
      ScriptableRenderPipeline/Core/Resources/EncodeBC6H.compute.meta
  13. 176
      ScriptableRenderPipeline/Core/ShaderLibrary/BC6H.hlsl
  14. 10
      ScriptableRenderPipeline/Core/ShaderLibrary/BC6H.hlsl.meta
  15. 198
      SampleScenes/Common/Materials/Mat_Test_CubemapDebug.mat
  16. 8
      SampleScenes/Common/Materials/Mat_Test_CubemapDebug.mat.meta
  17. 194
      SampleScenes/Common/Materials/Mat_Test_Unlit.mat
  18. 8
      SampleScenes/Common/Materials/Mat_Test_Unlit.mat.meta
  19. 62
      SampleScenes/Common/Shaders/CubemapColorMap.shader
  20. 8
      SampleScenes/Common/Shaders/CubemapColorMap.shader.meta
  21. 7
      SampleScenes/CoreTest/Test_BC6HCompression.unity.meta

57
ScriptableRenderPipeline/Core/ShaderLibrary/Sampling.hlsl


return TransformGLtoDX(SphericalToCartesian(phi, cosTheta));
}
// Convert a texel position into normalized position [-1..1]x[-1..1]
float2 CubemapTexelToNVC(uint2 unPositionTXS, uint cubemapSize)
{
return 2.0 * float2(unPositionTXS) / float(max(cubemapSize - 1, 1)) - 1.0;
}
// Map cubemap face to world vector basis
static const float3 CUBEMAP_FACE_BASIS_MAPPING[6][3] =
{
//XPOS face
{
float3(0.0, 0.0, -1.0),
float3(0.0, -1.0, 0.0),
float3(1.0, 0.0, 0.0)
},
//XNEG face
{
float3(0.0, 0.0, 1.0),
float3(0.0, -1.0, 0.0),
float3(-1.0, 0.0, 0.0)
},
//YPOS face
{
float3(1.0, 0.0, 0.0),
float3(0.0, 0.0, 1.0),
float3(0.0, 1.0, 0.0)
},
//YNEG face
{
float3(1.0, 0.0, 0.0),
float3(0.0, 0.0, -1.0),
float3(0.0, -1.0, 0.0)
},
//ZPOS face
{
float3(1.0, 0.0, 0.0),
float3(0.0, -1.0, 0.0),
float3(0.0, 0.0, 1.0)
},
//ZNEG face
{
float3(-1.0, 0.0, 0.0),
float3(0.0, -1.0, 0.0),
float3(0.0, 0.0, -1.0)
}
};
// Convert a normalized cubemap face position into a direction
float3 CubemapTexelToDirection(float2 positionNVC, uint faceId)
{
float3 dir = CUBEMAP_FACE_BASIS_MAPPING[faceId][0] * positionNVC.x
+ CUBEMAP_FACE_BASIS_MAPPING[faceId][1] * positionNVC.y
+ CUBEMAP_FACE_BASIS_MAPPING[faceId][2];
return normalize(dir);
}
//-----------------------------------------------------------------------------
// Sampling function
// Reference : http://www.cs.virginia.edu/~jdl/bib/globillum/mis/shirley96.pdf + PBRT

8
SampleScenes/Common/Materials.meta


fileFormatVersion: 2
guid: d6989f167ae4d07419315e04a2920061
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

91
SampleScenes/Common/Scripts/CompressBC6HAndDisplay.cs


using UnityEngine;
using UnityEngine.Experimental.Rendering;
using UnityEngine.Rendering;
[ExecuteInEditMode]
public class CompressBC6HAndDisplay : MonoBehaviour
{
[SerializeField]
ComputeShader m_BC6HShader;
[SerializeField]
Texture m_SourceTexture;
[SerializeField]
Material m_SourceMaterial;
[SerializeField]
string m_TextureName = "_ColorMap";
int m_Hash = 0;
RenderTargetIdentifier m_SourceId;
Texture m_Target;
RenderTargetIdentifier m_TargetId;
int m_MipCount = 0;
Renderer m_Renderer;
Material m_Material;
EncodeBC6H m_EncodeBC6H;
void OnEnable()
{
if (m_Material != null)
DestroyImmediate(m_Material);
m_Material = Instantiate(m_SourceMaterial);
var renderer = m_Renderer ?? (m_Renderer = GetComponent<Renderer>());
renderer.material = m_Material;
m_EncodeBC6H = new EncodeBC6H(m_BC6HShader);
}
void OnPreRender(CommandBuffer cmb)
{
if (m_SourceTexture == null
|| m_SourceMaterial == null
|| m_BC6HShader == null)
{
enabled = false;
return;
}
using (new ProfilingSample(cmb, "EncodeBC6H Test"))
{
var hash = CalculateHash(m_SourceTexture);
if (m_Hash != hash)
{
m_Hash = hash;
m_SourceId = new RenderTargetIdentifier(m_SourceTexture);
CreateTargetInstance();
}
m_EncodeBC6H.EncodeFastCubemap(cmb, m_SourceId, m_SourceTexture.width, m_TargetId, 0, m_MipCount - 1);
}
m_Material.SetTexture(m_TextureName, m_Target);
}
void Update()
{
var cmd = new CommandBuffer { name = "EncodeBC6H Compress" };
OnPreRender(cmd);
Graphics.ExecuteCommandBuffer(cmd);
}
[ContextMenu("Create Target")]
void CreateTargetInstance()
{
if (m_Target is RenderTexture)
((RenderTexture)m_Target).Release();
var t = new Cubemap(m_SourceTexture.width, TextureFormat.BC6H, true);
m_Target = t;
m_Material.SetTexture(m_TextureName, m_Target);
m_SourceId = new RenderTargetIdentifier(m_SourceTexture);
m_TargetId = new RenderTargetIdentifier(m_Target);
m_MipCount = ((Cubemap)m_SourceTexture).mipmapCount;
}
static int CalculateHash(Texture texture)
{
return texture.width ^ texture.height ^ texture.GetInstanceID();
}
}

11
SampleScenes/Common/Scripts/CompressBC6HAndDisplay.cs.meta


fileFormatVersion: 2
guid: 73fbe8f92db5fb3408250f4bdc5b42cc
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

8
SampleScenes/Common/Shaders.meta


fileFormatVersion: 2
guid: 3ac57b45e32d4e742b7ee5f0e5ad1cda
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

77
SampleScenes/Common/Textures/Alexs_Apt_2k.hdr.meta


fileFormatVersion: 2
guid: 7ab4e79cadc728f41bb8678cad593a13
TextureImporter:
fileIDToRecycleName: {}
externalObjects: {}
serializedVersion: 5
mipmaps:
mipMapMode: 0
enableMipMap: 1
sRGBTexture: 1
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
grayScaleToAlpha: 0
generateCubemap: 6
cubemapConvolution: 0
seamlessCubemap: 0
textureFormat: 1
maxTextureSize: 2048
textureSettings:
serializedVersion: 2
filterMode: -1
aniso: -1
mipBias: -1
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}
spriteBorder: {x: 0, y: 0, z: 0, w: 0}
spritePixelsToUnits: 100
alphaUsage: 1
alphaIsTransparency: 0
spriteTessellationDetail: -1
textureType: 0
textureShape: 1
singleChannelComponent: 0
maxTextureSizeSet: 0
compressionQualitySet: 0
textureFormatSet: 0
platformSettings:
- serializedVersion: 2
buildTarget: DefaultTexturePlatform
maxTextureSize: 2048
resizeAlgorithm: 0
textureFormat: -1
textureCompression: 1
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
androidETC2FallbackOverride: 0
spriteSheet:
serializedVersion: 2
sprites: []
outline: []
physicsShape: []
spritePackingTag:
userData:
assetBundleName:
assetBundleVariant:

139
SampleScenes/Common/Textures/Alexs_Apt_2k_cube.hdr.meta


fileFormatVersion: 2
guid: 8714eafe2130af14b835106673e808d1
TextureImporter:
fileIDToRecycleName:
8900000: generatedCubemap
externalObjects: {}
serializedVersion: 5
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
grayScaleToAlpha: 0
generateCubemap: 2
cubemapConvolution: 0
seamlessCubemap: 0
textureFormat: 1
maxTextureSize: 2048
textureSettings:
serializedVersion: 2
filterMode: -1
aniso: -1
mipBias: -1
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}
spriteBorder: {x: 0, y: 0, z: 0, w: 0}
spritePixelsToUnits: 100
alphaUsage: 1
alphaIsTransparency: 0
spriteTessellationDetail: -1
textureType: 0
textureShape: 2
singleChannelComponent: 0
maxTextureSizeSet: 0
compressionQualitySet: 0
textureFormatSet: 0
platformSettings:
- serializedVersion: 2
buildTarget: DefaultTexturePlatform
maxTextureSize: 256
resizeAlgorithm: 0
textureFormat: -1
textureCompression: 0
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
androidETC2FallbackOverride: 0
- serializedVersion: 2
buildTarget: Standalone
maxTextureSize: 256
resizeAlgorithm: 0
textureFormat: -1
textureCompression: 0
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
androidETC2FallbackOverride: 0
- serializedVersion: 2
buildTarget: iPhone
maxTextureSize: 256
resizeAlgorithm: 0
textureFormat: -1
textureCompression: 0
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
androidETC2FallbackOverride: 0
- serializedVersion: 2
buildTarget: tvOS
maxTextureSize: 1024
resizeAlgorithm: 0
textureFormat: -1
textureCompression: 0
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
androidETC2FallbackOverride: 0
- serializedVersion: 2
buildTarget: Windows Store Apps
maxTextureSize: 256
resizeAlgorithm: 0
textureFormat: -1
textureCompression: 0
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
androidETC2FallbackOverride: 0
- serializedVersion: 2
buildTarget: PS4
maxTextureSize: 256
resizeAlgorithm: 0
textureFormat: -1
textureCompression: 0
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
androidETC2FallbackOverride: 0
spriteSheet:
serializedVersion: 2
sprites: []
outline: []
physicsShape: []
bones: []
spriteID:
vertices: []
indices:
edges: []
weights: []
spritePackingTag:
userData:
assetBundleName:
assetBundleVariant:

8
SampleScenes/CoreTest.meta


fileFormatVersion: 2
guid: 35e65ef485d9f994d9d6519b4b27fc7a
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

122
ScriptableRenderPipeline/Core/EncodeBC6H.cs


using UnityEngine.Assertions;
using UnityEngine.Rendering;
namespace UnityEngine.Experimental.Rendering
{
public class EncodeBC6H
{
public static EncodeBC6H DefaultInstance;
static readonly int _Source = Shader.PropertyToID("_Source");
static readonly int _Target = Shader.PropertyToID("_Target");
static readonly int _MipIndex = Shader.PropertyToID("_MipIndex");
static readonly int[] __Tmp_RT =
{
Shader.PropertyToID("__Tmp_RT0"),
Shader.PropertyToID("__Tmp_RT1"),
Shader.PropertyToID("__Tmp_RT2"),
Shader.PropertyToID("__Tmp_RT3"),
Shader.PropertyToID("__Tmp_RT4"),
Shader.PropertyToID("__Tmp_RT5"),
Shader.PropertyToID("__Tmp_RT6"),
Shader.PropertyToID("__Tmp_RT7"),
Shader.PropertyToID("__Tmp_RT8"),
Shader.PropertyToID("__Tmp_RT9"),
Shader.PropertyToID("__Tmp_RT10"),
Shader.PropertyToID("__Tmp_RT11"),
Shader.PropertyToID("__Tmp_RT12"),
Shader.PropertyToID("__Tmp_RT13")
};
readonly ComputeShader m_Shader;
readonly int m_KEncodeFastCubemapMip;
public EncodeBC6H(ComputeShader shader)
{
Assert.IsNotNull(shader);
m_Shader = shader;
m_KEncodeFastCubemapMip = m_Shader.FindKernel("KEncodeFastCubemapMip");
uint x, y, z;
m_Shader.GetKernelThreadGroupSizes(m_KEncodeFastCubemapMip, out x, out y, out z);
}
// Only use mode11 of BC6H encoding
/// <summary>
/// Encode a Cubemap in BC6H.
///
/// It will encode all faces and selected mips of the Cubemap.
///
/// It uses only mode 11 of BC6H.
/// </summary>
/// <param name="cmb">Command buffer for execution</param>
/// <param name="source">The source Cubemap</param>
/// <param name="sourceSize">The size of the source Cubemap</param>
/// <param name="target">The compressed texture.
/// It must be a BC6H Cubemap or Cubemap array with the same size as the source Cubemap</param>
/// <param name="fromMip">Starting mip to encode</param>
/// <param name="toMip">Last mip to encode</param>
/// <param name="targetArrayIndex">The index of the cubemap to store the compressed texture.
///
/// Only relevant when target is a CubemapArray</param>
public void EncodeFastCubemap(CommandBuffer cmb, RenderTargetIdentifier source, int sourceSize, RenderTargetIdentifier target, int fromMip, int toMip, int targetArrayIndex = 0)
{
var maxMip = Mathf.Max(0, (int)(Mathf.Log(sourceSize) / Mathf.Log(2)) - 2);
var actualFromMip = (int)Mathf.Clamp(fromMip, 0, maxMip);
var actualToMip = (int)Mathf.Min(maxMip, Mathf.Max(toMip, actualFromMip));
// Convert TextureCube source to Texture2DArray
var d = new RenderTextureDescriptor
{
autoGenerateMips = false,
bindMS = false,
colorFormat = RenderTextureFormat.ARGBInt,
depthBufferBits = 0,
dimension = TextureDimension.Tex2DArray,
enableRandomWrite = true,
msaaSamples = 1,
volumeDepth = 6,
sRGB = false,
useMipMap = false,
};
cmb.SetComputeTextureParam(m_Shader, m_KEncodeFastCubemapMip, _Source, source);
for (var mip = actualFromMip; mip <= actualToMip; ++mip)
{
var size = (sourceSize >> mip) >> 2;
d.width = size;
d.height = size;
cmb.GetTemporaryRT(__Tmp_RT[mip], d);
}
for (var mip = actualFromMip; mip <= actualToMip; ++mip)
{
var size = (sourceSize >> mip) >> 2;
cmb.SetComputeTextureParam(m_Shader, m_KEncodeFastCubemapMip, _Target, __Tmp_RT[mip]);
cmb.SetComputeIntParam(m_Shader, _MipIndex, mip);
cmb.DispatchCompute(m_Shader, m_KEncodeFastCubemapMip, size, size, 6);
}
var startSlice = 6 * targetArrayIndex;
for (var mip = fromMip; mip <= toMip; ++mip)
{
var rtMip = Mathf.Clamp(mip, actualFromMip, actualToMip);
for (var faceId = 0; faceId < 6; ++faceId)
cmb.CopyTexture(__Tmp_RT[rtMip], faceId, 0, target, startSlice + faceId, mip);
}
for (var mip = actualFromMip; mip <= actualToMip; ++mip)
cmb.ReleaseTemporaryRT(__Tmp_RT[mip]);
}
}
public static class BC6HExtensions
{
public static void BC6HEncodeFastCubemap(this CommandBuffer cmb, RenderTargetIdentifier source, int sourceSize, RenderTargetIdentifier target, int fromMip, int toMip, int targetArrayIndex = 0)
{
EncodeBC6H.DefaultInstance.EncodeFastCubemap(cmb, source, sourceSize, target, fromMip, toMip, targetArrayIndex);
}
}
}

13
ScriptableRenderPipeline/Core/EncodeBC6H.cs.meta


fileFormatVersion: 2
guid: ef7e375d470b6404a9e355690703502b
timeCreated: 1507290672
licenseType: Pro
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

49
ScriptableRenderPipeline/Core/Resources/EncodeBC6H.compute


#include "../ShaderLibrary/Common.hlsl"
#include "../ShaderLibrary/BC6H.hlsl"
#include "../ShaderLibrary/Sampling.hlsl"
TextureCube<float4> _Source;
RWTexture2DArray<uint4> _Target;
int _MipIndex;
SamplerState s_point_clamp;
static const uint2 kOffsets[] =
{
uint2(0, 0), uint2(1, 0), uint2(2, 0), uint2(3, 0),
uint2(0, 1), uint2(1, 1), uint2(2, 1), uint2(3, 1),
uint2(0, 2), uint2(1, 2), uint2(2, 2), uint2(3, 2),
uint2(0, 3), uint2(1, 3), uint2(2, 3), uint2(3, 3),
};
#pragma kernel KEncodeFastCubemapMip
[numthreads(1, 1, 1)]
void KEncodeFastCubemapMip(uint3 groupId : SV_GroupID, uint3 groupThreadId : SV_GroupThreadID, uint3 dispatchThreadId : SV_DispatchThreadID)
{
uint width = 0;
uint height = 0;
_Source.GetDimensions(width, height);
int size = width >> _MipIndex;
// Load 4x4 pixel block
float3 texels[16];
uint2 topLeftSourceID = dispatchThreadId.xy << 2;
uint faceId = dispatchThreadId.z;
float2 nvc;
float3 dir;
for (uint i = 0u; i < 16; ++i)
{
nvc = CubemapTexelToNVC(topLeftSourceID + kOffsets[i], size);
dir = CubemapTexelToDirection(nvc, faceId);
texels[i] = _Source.SampleLevel(s_point_clamp, dir, _MipIndex).rgb;
}
uint4 block = uint4(0, 0, 0, 0);
float blockMSLE = 0;
EncodeMode11(block, blockMSLE, texels);
_Target[dispatchThreadId] = block;
}

10
ScriptableRenderPipeline/Core/Resources/EncodeBC6H.compute.meta


fileFormatVersion: 2
guid: b69b95b3420fd904e8530b79f665a1f8
timeCreated: 1507123133
licenseType: Pro
ComputeShaderImporter:
externalObjects: {}
currentAPIMask: 4
userData:
assetBundleName:
assetBundleVariant:

176
ScriptableRenderPipeline/Core/ShaderLibrary/BC6H.hlsl


// Ref: https://github.com/knarkowicz/GPURealTimeBC6H/blob/master/bin/compress.hlsl
// Doc: https://msdn.microsoft.com/en-us/library/windows/desktop/hh308952(v=vs.85).aspx
#include "Common.hlsl"
// Measure compression error
float CalcMSLE(float3 a, float3 b)
{
float3 err = log2(( b + 1.0f) / (a + 1.0f ));
err = err * err;
return err.x + err.y + err.z;
}
// Quantification Helpers
float3 Quantize7(float3 x)
{
return (f32tof16(x) * 128.0f) / (0x7bff + 1.0f);
}
float3 Quantize9(float3 x)
{
return (f32tof16(x) * 512.0f) / (0x7bff + 1.0f);
}
float3 Quantize10(float3 x)
{
return (f32tof16(x) * 1024.0f) / (0x7bff + 1.0f);
}
float3 Unquantize7(float3 x)
{
return (x * 65536.0f + 0x8000) / 128.0f;
}
float3 Unquantize9(float3 x)
{
return (x * 65536.0f + 0x8000) / 512.0f;
}
float3 Unquantize10(float3 x)
{
return (x * 65536.0f + 0x8000) / 1024.0f;
}
// BC6H Helpers
// Compute index of a texel projected against endpoints
uint ComputeIndex3( float texelPos, float endPoint0Pos, float endPoint1Pos )
{
float r = ( texelPos - endPoint0Pos ) / ( endPoint1Pos - endPoint0Pos );
return (uint) clamp( r * 6.98182f + 0.00909f + 0.5f, 0.0f, 7.0f );
}
uint ComputeIndex4( float texelPos, float endPoint0Pos, float endPoint1Pos )
{
float r = ( texelPos - endPoint0Pos ) / ( endPoint1Pos - endPoint0Pos );
return (uint) clamp( r * 14.93333f + 0.03333f + 0.5f, 0.0f, 15.0f );
}
void SignExtend( inout float3 v1, uint mask, uint signFlag )
{
int3 v = (int3) v1;
v.x = ( v.x & mask ) | ( v.x < 0 ? signFlag : 0 );
v.y = ( v.y & mask ) | ( v.y < 0 ? signFlag : 0 );
v.z = ( v.z & mask ) | ( v.z < 0 ? signFlag : 0 );
v1 = v;
}
// 2nd step for unquantize
float3 FinishUnquantize( float3 endpoint0Unq, float3 endpoint1Unq, float weight )
{
float3 comp = ( endpoint0Unq * ( 64.0f - weight ) + endpoint1Unq * weight + 32.0f ) * ( 31.0f / 4096.0f );
return f16tof32( uint3( comp ) );
}
// BC6H Modes
void EncodeMode11( inout uint4 block, inout float blockMSLE, float3 texels[ 16 ] )
{
// compute endpoints (min/max RGB bbox)
float3 blockMin = texels[ 0 ];
float3 blockMax = texels[ 0 ];
for ( uint i = 1; i < 16; ++i )
{
blockMin = min( blockMin, texels[ i ] );
blockMax = max( blockMax, texels[ i ] );
}
// refine endpoints in log2 RGB space
float3 refinedBlockMin = blockMax;
float3 refinedBlockMax = blockMin;
for (i = 0; i < 16; ++i )
{
refinedBlockMin = min( refinedBlockMin, texels[ i ] == blockMin ? refinedBlockMin : texels[ i ] );
refinedBlockMax = max( refinedBlockMax, texels[ i ] == blockMax ? refinedBlockMax : texels[ i ] );
}
float3 logBlockMax = log2( blockMax + 1.0f );
float3 logBlockMin = log2( blockMin + 1.0f );
float3 logRefinedBlockMax = log2( refinedBlockMax + 1.0f );
float3 logRefinedBlockMin = log2( refinedBlockMin + 1.0f );
float3 logBlockMaxExt = ( logBlockMax - logBlockMin ) * ( 1.0f / 32.0f );
logBlockMin += min( logRefinedBlockMin - logBlockMin, logBlockMaxExt );
logBlockMax -= min( logBlockMax - logRefinedBlockMax, logBlockMaxExt );
blockMin = exp2( logBlockMin ) - 1.0f;
blockMax = exp2( logBlockMax ) - 1.0f;
float3 blockDir = blockMax - blockMin;
blockDir = blockDir / ( blockDir.x + blockDir.y + blockDir.z );
float3 endpoint0 = Quantize10( blockMin );
float3 endpoint1 = Quantize10( blockMax );
float endPoint0Pos = f32tof16( dot( blockMin, blockDir ) );
float endPoint1Pos = f32tof16( dot( blockMax, blockDir ) );
// check if endpoint swap is required
float fixupTexelPos = f32tof16( dot( texels[ 0 ], blockDir ) );
uint fixupIndex = ComputeIndex4( fixupTexelPos, endPoint0Pos, endPoint1Pos );
if ( fixupIndex > 7 )
{
Swap( endPoint0Pos, endPoint1Pos );
Swap( endpoint0, endpoint1 );
}
// compute indices
uint indices[ 16 ] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
for (i = 0; i < 16; ++i )
{
float texelPos = f32tof16( dot( texels[ i ], blockDir ) );
indices[ i ] = ComputeIndex4( texelPos, endPoint0Pos, endPoint1Pos );
}
// compute compression error (MSLE)
float3 endpoint0Unq = Unquantize10( endpoint0 );
float3 endpoint1Unq = Unquantize10( endpoint1 );
float msle = 0.0f;
for (i = 0; i < 16; ++i )
{
float weight = floor( ( indices[ i ] * 64.0f ) / 15.0f + 0.5f );
float3 texelUnc = FinishUnquantize( endpoint0Unq, endpoint1Unq, weight );
msle += CalcMSLE( texels[ i ], texelUnc );
}
// encode block for mode 11
blockMSLE = msle;
block.x = 0x03;
// endpoints
block.x |= (uint) endpoint0.x << 5;
block.x |= (uint) endpoint0.y << 15;
block.x |= (uint) endpoint0.z << 25;
block.y |= (uint) endpoint0.z >> 7;
block.y |= (uint) endpoint1.x << 3;
block.y |= (uint) endpoint1.y << 13;
block.y |= (uint) endpoint1.z << 23;
block.z |= (uint) endpoint1.z >> 9;
// indices
block.z |= indices[ 0 ] << 1;
block.z |= indices[ 1 ] << 4;
block.z |= indices[ 2 ] << 8;
block.z |= indices[ 3 ] << 12;
block.z |= indices[ 4 ] << 16;
block.z |= indices[ 5 ] << 20;
block.z |= indices[ 6 ] << 24;
block.z |= indices[ 7 ] << 28;
block.w |= indices[ 8 ] << 0;
block.w |= indices[ 9 ] << 4;
block.w |= indices[ 10 ] << 8;
block.w |= indices[ 11 ] << 12;
block.w |= indices[ 12 ] << 16;
block.w |= indices[ 13 ] << 20;
block.w |= indices[ 14 ] << 24;
block.w |= indices[ 15 ] << 28;
}

10
ScriptableRenderPipeline/Core/ShaderLibrary/BC6H.hlsl.meta


fileFormatVersion: 2
guid: 27d419a4917d0ea49978c236e058d464
timeCreated: 1507282342
licenseType: Pro
ShaderImporter:
externalObjects: {}
defaultTextures: []
userData:
assetBundleName:
assetBundleVariant:

198
SampleScenes/Common/Materials/Mat_Test_CubemapDebug.mat


%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!21 &2100000
Material:
serializedVersion: 6
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_Name: Mat_Test_CubemapDebug
m_Shader: {fileID: 4800000, guid: 52626b3f25f2ae94bb64059e8f42c487, type: 3}
m_ShaderKeywords: _ALBEDOAFFECTEMISSIVE_OFF _ALPHACUTOFFENABLE_OFF _BLENDMODE_PRESERVE_SPECULAR_LIGHTING
_DEPTHOFFSETENABLE_OFF _DISTORTIONENABLE_OFF _DOUBLESIDEDENABLE_OFF _ENABLESPECULAROCCLUSION_OFF
_ENABLEWIND_OFF _NORMALMAP_TANGENT_SPACE _PREREFRACTIONPASS_OFF
m_LightmapFlags: 4
m_EnableInstancingVariants: 0
m_DoubleSidedGI: 0
m_CustomRenderQueue: 3000
stringTagMap: {}
disabledShaderPasses:
- DistortionVectors
m_SavedProperties:
serializedVersion: 3
m_TexEnvs:
- _AnisotropyMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _BaseColorMap:
m_Texture: {fileID: 2800000, guid: 7ab4e79cadc728f41bb8678cad593a13, type: 3}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _BentNormalMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _BentNormalMapOS:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _ColorMap:
m_Texture: {fileID: 8900000, guid: 8714eafe2130af14b835106673e808d1, type: 3}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _DetailMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _Diffuse:
m_Texture: {fileID: 8900000, guid: 8714eafe2130af14b835106673e808d1, type: 3}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _DistortionVectorMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _EmissiveColorMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _HeightMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _MainTex:
m_Texture: {fileID: 2800000, guid: 7ab4e79cadc728f41bb8678cad593a13, type: 3}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _MaskMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _NormalMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _NormalMapOS:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _SpecularColorMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _SubsurfaceRadiusMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _TangentMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _TangentMapOS:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _ThicknessMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _TransmittanceColorMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
m_Floats:
- _AORemapMax: 1
- _AORemapMin: 0
- _ATDistance: 1
- _AlbedoAffectEmissive: 0
- _AlphaCutoff: 0.5
- _AlphaCutoffEnable: 0
- _Anisotropy: 0
- _BlendMode: 0
- _CoatCoverage: 1
- _CoatIOR: 0.5
- _CullMode: 2
- _Cutoff: 0.5
- _DepthOffsetEnable: 0
- _DetailAlbedoScale: 1
- _DetailNormalScale: 1
- _DetailSmoothnessScale: 1
- _DisplacementLockObjectScale: 1
- _DisplacementLockTilingScale: 1
- _DisplacementMode: 0
- _DistortionBlendMode: 0
- _DistortionBlurBlendMode: 0
- _DistortionBlurDstBlend: 1
- _DistortionBlurRemapMax: 1
- _DistortionBlurRemapMin: 0
- _DistortionBlurScale: 1
- _DistortionBlurSrcBlend: 1
- _DistortionDepthTest: 1
- _DistortionDstBlend: 1
- _DistortionEnable: 0
- _DistortionScale: 1
- _DistortionSrcBlend: 1
- _DistortionVectorBias: -1
- _DistortionVectorScale: 2
- _DoubleSidedEnable: 0
- _DoubleSidedNormalMode: 1
- _Drag: 1
- _DstBlend: 0
- _EmissiveColorMode: 1
- _EmissiveIntensity: 0
- _EnableBlendModePreserveSpecularLighting: 1
- _EnableFogOnTransparent: 1
- _EnableSpecularOcclusion: 0
- _EnableWind: 0
- _Glossiness: 0.5
- _HeightAmplitude: 0.02
- _HeightCenter: 0.5
- _HeightMax: 1
- _HeightMin: -1
- _IOR: 1
- _InitialBend: 1
- _InvTilingScale: 1
- _LinkDetailsWithBase: 1
- _MaterialID: 1
- _Metallic: 0
- _MipIndex: 0
- _NormalMapSpace: 0
- _NormalScale: 1
- _PPDLodThreshold: 5
- _PPDMaxSamples: 15
- _PPDMinSamples: 5
- _PPDPrimitiveLength: 1
- _PPDPrimitiveWidth: 1
- _PreRefractionPass: 0
- _RefractionMode: 0
- _ShiverDirectionality: 0.5
- _ShiverDrag: 0.2
- _Smoothness: 1
- _SmoothnessRemapMax: 1
- _SmoothnessRemapMin: 0
- _SrcBlend: 1
- _StencilRef: 2
- _Stiffness: 1
- _SubsurfaceProfile: 0
- _SubsurfaceRadius: 1
- _SurfaceType: 0
- _TexWorldScale: 1
- _Thickness: 1
- _ThicknessMultiplier: 1
- _UVBase: 0
- _UVDetail: 0
- _ZTestMode: 4
- _ZWrite: 1
m_Colors:
- _BaseColor: {r: 1, g: 1, b: 1, a: 1}
- _Color: {r: 1, g: 1, b: 1, a: 1}
- _DoubleSidedConstants: {r: 1, g: 1, b: -1, a: 0}
- _EmissionColor: {r: 1, g: 1, b: 1, a: 1}
- _EmissiveColor: {r: 1, g: 1, b: 1, a: 1}
- _InvPrimScale: {r: 1, g: 1, b: 0, a: 0}
- _SpecularColor: {r: 1, g: 1, b: 1, a: 1}
- _ThicknessRemap: {r: 0, g: 1, b: 0, a: 0}
- _TransmittanceColor: {r: 1, g: 1, b: 1, a: 1}
- _UVDetailsMappingMask: {r: 1, g: 0, b: 0, a: 0}
- _UVMappingMask: {r: 1, g: 0, b: 0, a: 0}

8
SampleScenes/Common/Materials/Mat_Test_CubemapDebug.mat.meta


fileFormatVersion: 2
guid: 78e152488d58e8242bf90ac931fc7c68
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 2100000
userData:
assetBundleName:
assetBundleVariant:

194
SampleScenes/Common/Materials/Mat_Test_Unlit.mat


%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!21 &2100000
Material:
serializedVersion: 6
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_Name: Mat_Test_Unlit
m_Shader: {fileID: 4800000, guid: c4edd00ff2db5b24391a4fcb1762e459, type: 3}
m_ShaderKeywords: _ALBEDOAFFECTEMISSIVE_OFF _ALPHACUTOFFENABLE_OFF _DEPTHOFFSETENABLE_OFF
_DISTORTIONENABLE_OFF _DISTORTIONONLY_OFF _DOUBLESIDEDENABLE_OFF _ENABLESPECULAROCCLUSION_OFF
_ENABLEWIND_OFF _PREREFRACTIONPASS_OFF
m_LightmapFlags: 4
m_EnableInstancingVariants: 0
m_DoubleSidedGI: 0
m_CustomRenderQueue: -1
stringTagMap: {}
disabledShaderPasses:
- DistortionVectors
m_SavedProperties:
serializedVersion: 3
m_TexEnvs:
- _AnisotropyMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _BaseColorMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _BentNormalMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _BentNormalMapOS:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _DetailMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _DistortionVectorMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _EmissiveColorMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _HeightMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _MainTex:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _MaskMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _NormalMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _NormalMapOS:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _SpecularColorMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _SubsurfaceRadiusMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _TangentMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _TangentMapOS:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _ThicknessMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _TransmittanceColorMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _UnlitColorMap:
m_Texture: {fileID: 2800000, guid: 7ab4e79cadc728f41bb8678cad593a13, type: 3}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
m_Floats:
- _AORemapMax: 1
- _AORemapMin: 0
- _ATDistance: 1
- _AlbedoAffectEmissive: 0
- _AlphaCutoff: 0.5
- _AlphaCutoffEnable: 0
- _Anisotropy: 0
- _BlendMode: 0
- _CoatCoverage: 1
- _CoatIOR: 0.5
- _CullMode: 2
- _Cutoff: 0.5
- _DepthOffsetEnable: 0
- _DetailAlbedoScale: 1
- _DetailNormalScale: 1
- _DetailSmoothnessScale: 1
- _DisplacementLockObjectScale: 1
- _DisplacementLockTilingScale: 1
- _DisplacementMode: 0
- _DistortionBlendMode: 0
- _DistortionBlurBlendMode: 0
- _DistortionBlurDstBlend: 1
- _DistortionBlurRemapMax: 1
- _DistortionBlurRemapMin: 0
- _DistortionBlurScale: 1
- _DistortionBlurSrcBlend: 1
- _DistortionDepthTest: 1
- _DistortionDstBlend: 1
- _DistortionEnable: 0
- _DistortionOnly: 0
- _DistortionScale: 1
- _DistortionSrcBlend: 1
- _DistortionVectorBias: -1
- _DistortionVectorScale: 2
- _DoubleSidedEnable: 0
- _DoubleSidedNormalMode: 1
- _Drag: 1
- _DstBlend: 0
- _EmissiveColorMode: 1
- _EmissiveIntensity: 0
- _EnableBlendModePreserveSpecularLighting: 1
- _EnableFogOnTransparent: 1
- _EnableSpecularOcclusion: 0
- _EnableWind: 0
- _HeightAmplitude: 0.02
- _HeightCenter: 0.5
- _HeightMax: 1
- _HeightMin: -1
- _IOR: 1
- _InitialBend: 1
- _InvTilingScale: 1
- _LinkDetailsWithBase: 1
- _MaterialID: 1
- _Metallic: 0
- _NormalMapSpace: 0
- _NormalScale: 1
- _PPDLodThreshold: 5
- _PPDMaxSamples: 15
- _PPDMinSamples: 5
- _PPDPrimitiveLength: 1
- _PPDPrimitiveWidth: 1
- _PreRefractionPass: 0
- _RefractionMode: 0
- _ShiverDirectionality: 0.5
- _ShiverDrag: 0.2
- _Smoothness: 1
- _SmoothnessRemapMax: 1
- _SmoothnessRemapMin: 0
- _SrcBlend: 1
- _StencilRef: 2
- _Stiffness: 1
- _SubsurfaceProfile: 0
- _SubsurfaceRadius: 1
- _SurfaceType: 0
- _TexWorldScale: 1
- _Thickness: 1
- _ThicknessMultiplier: 1
- _UVBase: 0
- _UVDetail: 0
- _ZTestMode: 4
- _ZWrite: 1
m_Colors:
- _BaseColor: {r: 1, g: 1, b: 1, a: 1}
- _Color: {r: 1, g: 1, b: 1, a: 1}
- _DoubleSidedConstants: {r: 1, g: 1, b: -1, a: 0}
- _EmissionColor: {r: 1, g: 1, b: 1, a: 1}
- _EmissiveColor: {r: 1, g: 1, b: 1, a: 1}
- _InvPrimScale: {r: 1, g: 1, b: 0, a: 0}
- _SpecularColor: {r: 1, g: 1, b: 1, a: 1}
- _ThicknessRemap: {r: 0, g: 1, b: 0, a: 0}
- _TransmittanceColor: {r: 1, g: 1, b: 1, a: 1}
- _UVDetailsMappingMask: {r: 1, g: 0, b: 0, a: 0}
- _UVMappingMask: {r: 1, g: 0, b: 0, a: 0}
- _UnlitColor: {r: 1, g: 1, b: 1, a: 1}

8
SampleScenes/Common/Materials/Mat_Test_Unlit.mat.meta


fileFormatVersion: 2
guid: 1df706eb139563743a23dbac2becc078
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 2100000
userData:
assetBundleName:
assetBundleVariant:

62
SampleScenes/Common/Shaders/CubemapColorMap.shader


Shader "Hidden/CubemapColorMap" {
Properties {
_ColorMap("Color", Cube) = "white" {}
_MipIndex("Mip Index", Int) = 0
}
HLSLINCLUDE
#pragma target 5.0
#include "UnityCG.cginc"
TextureCube _ColorMap;
int _MipIndex;
SamplerState s_bilinear_clamp;
struct appdata {
float3 positionOS : POSITION;
float3 normalOS : NORMAL;
};
struct v2f {
float4 positionCS : SV_POSITION;
float3 normalWS : TEXCOORD0;
};
v2f Vert(appdata i) {
v2f o;
float4 positionCS = UnityObjectToClipPos(i.positionOS);
float3 normalWS = normalize(mul(i.normalOS, (float3x3)unity_WorldToObject));
o.positionCS = positionCS;
o.normalWS = normalWS;
return o;
}
float4 Frag(v2f i) : SV_Target {
float3 col = _ColorMap.SampleLevel(s_bilinear_clamp, i.normalWS, _MipIndex).xyz;
return float4(col, 0.5);
}
#pragma vertex Vert
#pragma fragment Frag
ENDHLSL
SubShader {
Pass {
Name "ForwardUnlit"
Tags{ "LightMode" = "ForwardOnly" }
Blend One Zero
ZWrite On
Cull Back
HLSLPROGRAM
#define SHADERPASS SHADERPASS_FORWARD_UNLIT
ENDHLSL
}
}
}

8
SampleScenes/Common/Shaders/CubemapColorMap.shader.meta


fileFormatVersion: 2
guid: 52626b3f25f2ae94bb64059e8f42c487
ShaderImporter:
externalObjects: {}
defaultTextures: []
userData:
assetBundleName:
assetBundleVariant:

7
SampleScenes/CoreTest/Test_BC6HCompression.unity.meta


fileFormatVersion: 2
guid: 433aa972fff1ec44ca66dab7228e336b
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

部分文件因为文件数量过多而无法显示

正在加载...
取消
保存