Add shaders re-implemented in HLSL
These were written against the shaders at revision eddd724e7.
There have been changes made since then, which will need to be mirrored.
See `data/hlsl/README.md` for the current status of each sample.
This commit is contained in:
parent
10a1ecaf7b
commit
cce75f1859
287 changed files with 11263 additions and 0 deletions
25
data/hlsl/pbrtexture/filtercube.vert
Normal file
25
data/hlsl/pbrtexture/filtercube.vert
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
// Copyright 2020 Google LLC
|
||||
|
||||
struct VSInput
|
||||
{
|
||||
[[vk::location(0)]] float3 Pos : POSITION0;
|
||||
};
|
||||
|
||||
struct PushConsts {
|
||||
[[vk::offset(0)]] float4x4 mvp;
|
||||
};
|
||||
[[vk::push_constant]] PushConsts pushConsts;
|
||||
|
||||
struct VSOutput
|
||||
{
|
||||
float4 Pos : SV_POSITION;
|
||||
[[vk::location(0)]] float3 UVW : TEXCOORD0;
|
||||
};
|
||||
|
||||
VSOutput main(VSInput input)
|
||||
{
|
||||
VSOutput output = (VSOutput)0;
|
||||
output.UVW = input.Pos;
|
||||
output.Pos = mul(pushConsts.mvp, float4(input.Pos.xyz, 1.0));
|
||||
return output;
|
||||
}
|
||||
88
data/hlsl/pbrtexture/genbrdflut.frag
Normal file
88
data/hlsl/pbrtexture/genbrdflut.frag
Normal file
|
|
@ -0,0 +1,88 @@
|
|||
// Copyright 2020 Google LLC
|
||||
|
||||
[[vk::constant_id(0)]] const uint NUM_SAMPLES = 1024u;
|
||||
|
||||
#define PI 3.1415926536
|
||||
|
||||
// Based omn http://byteblacksmith.com/improvements-to-the-canonical-one-liner-glsl-rand-for-opengl-es-2-0/
|
||||
float random(float2 co)
|
||||
{
|
||||
float a = 12.9898;
|
||||
float b = 78.233;
|
||||
float c = 43758.5453;
|
||||
float dt= dot(co.xy ,float2(a,b));
|
||||
float sn= fmod(dt,3.14);
|
||||
return frac(sin(sn) * c);
|
||||
}
|
||||
|
||||
float2 hammersley2d(uint i, uint N)
|
||||
{
|
||||
// Radical inverse based on http://holger.dammertz.org/stuff/notes_HammersleyOnHemisphere.html
|
||||
uint bits = (i << 16u) | (i >> 16u);
|
||||
bits = ((bits & 0x55555555u) << 1u) | ((bits & 0xAAAAAAAAu) >> 1u);
|
||||
bits = ((bits & 0x33333333u) << 2u) | ((bits & 0xCCCCCCCCu) >> 2u);
|
||||
bits = ((bits & 0x0F0F0F0Fu) << 4u) | ((bits & 0xF0F0F0F0u) >> 4u);
|
||||
bits = ((bits & 0x00FF00FFu) << 8u) | ((bits & 0xFF00FF00u) >> 8u);
|
||||
float rdi = float(bits) * 2.3283064365386963e-10;
|
||||
return float2(float(i) /float(N), rdi);
|
||||
}
|
||||
|
||||
// Based on http://blog.selfshadow.com/publications/s2013-shading-course/karis/s2013_pbs_epic_slides.pdf
|
||||
float3 importanceSample_GGX(float2 Xi, float roughness, float3 normal)
|
||||
{
|
||||
// Maps a 2D point to a hemisphere with spread based on roughness
|
||||
float alpha = roughness * roughness;
|
||||
float phi = 2.0 * PI * Xi.x + random(normal.xz) * 0.1;
|
||||
float cosTheta = sqrt((1.0 - Xi.y) / (1.0 + (alpha*alpha - 1.0) * Xi.y));
|
||||
float sinTheta = sqrt(1.0 - cosTheta * cosTheta);
|
||||
float3 H = float3(sinTheta * cos(phi), sinTheta * sin(phi), cosTheta);
|
||||
|
||||
// Tangent space
|
||||
float3 up = abs(normal.z) < 0.999 ? float3(0.0, 0.0, 1.0) : float3(1.0, 0.0, 0.0);
|
||||
float3 tangentX = normalize(cross(up, normal));
|
||||
float3 tangentY = normalize(cross(normal, tangentX));
|
||||
|
||||
// Convert to world Space
|
||||
return normalize(tangentX * H.x + tangentY * H.y + normal * H.z);
|
||||
}
|
||||
|
||||
// Geometric Shadowing function
|
||||
float G_SchlicksmithGGX(float dotNL, float dotNV, float roughness)
|
||||
{
|
||||
float k = (roughness * roughness) / 2.0;
|
||||
float GL = dotNL / (dotNL * (1.0 - k) + k);
|
||||
float GV = dotNV / (dotNV * (1.0 - k) + k);
|
||||
return GL * GV;
|
||||
}
|
||||
|
||||
float2 BRDF(float NoV, float roughness)
|
||||
{
|
||||
// Normal always points along z-axis for the 2D lookup
|
||||
const float3 N = float3(0.0, 0.0, 1.0);
|
||||
float3 V = float3(sqrt(1.0 - NoV*NoV), 0.0, NoV);
|
||||
|
||||
float2 LUT = float2(0.0, 0.0);
|
||||
for(uint i = 0u; i < NUM_SAMPLES; i++) {
|
||||
float2 Xi = hammersley2d(i, NUM_SAMPLES);
|
||||
float3 H = importanceSample_GGX(Xi, roughness, N);
|
||||
float3 L = 2.0 * dot(V, H) * H - V;
|
||||
|
||||
float dotNL = max(dot(N, L), 0.0);
|
||||
float dotNV = max(dot(N, V), 0.0);
|
||||
float dotVH = max(dot(V, H), 0.0);
|
||||
float dotNH = max(dot(H, N), 0.0);
|
||||
|
||||
if (dotNL > 0.0) {
|
||||
float G = G_SchlicksmithGGX(dotNL, dotNV, roughness);
|
||||
float G_Vis = (G * dotVH) / (dotNH * dotNV);
|
||||
float Fc = pow(1.0 - dotVH, 5.0);
|
||||
LUT += float2((1.0 - Fc) * G_Vis, Fc * G_Vis);
|
||||
}
|
||||
}
|
||||
return LUT / float(NUM_SAMPLES);
|
||||
}
|
||||
|
||||
float4 main([[vk::location(0)]] float2 inUV : TEXCOORD0) : SV_TARGET
|
||||
{
|
||||
return float4(BRDF(inUV.x, 1.0-inUV.y), 0.0, 1.0);
|
||||
}
|
||||
15
data/hlsl/pbrtexture/genbrdflut.vert
Normal file
15
data/hlsl/pbrtexture/genbrdflut.vert
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
// Copyright 2020 Google LLC
|
||||
|
||||
struct VSOutput
|
||||
{
|
||||
float4 Pos : SV_POSITION;
|
||||
[[vk::location(0)]] float2 UV : TEXCOORD0;
|
||||
};
|
||||
|
||||
VSOutput main(uint VertexIndex : SV_VertexID)
|
||||
{
|
||||
VSOutput output = (VSOutput)0;
|
||||
output.UV = float2((VertexIndex << 1) & 2, VertexIndex & 2);
|
||||
output.Pos = float4(output.UV * 2.0f - 1.0f, 0.0f, 1.0f);
|
||||
return output;
|
||||
}
|
||||
35
data/hlsl/pbrtexture/irradiancecube.frag
Normal file
35
data/hlsl/pbrtexture/irradiancecube.frag
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
// Copyright 2020 Google LLC
|
||||
|
||||
TextureCube textureEnv : register(t0);
|
||||
SamplerState samplerEnv : register(s0);
|
||||
|
||||
struct PushConsts {
|
||||
[[vk::offset(64)]] float deltaPhi;
|
||||
[[vk::offset(68)]] float deltaTheta;
|
||||
};
|
||||
[[vk::push_constant]]PushConsts consts;
|
||||
|
||||
#define PI 3.1415926535897932384626433832795
|
||||
|
||||
float4 main([[vk::location(0)]] float3 inPos : TEXCOORD0) : SV_TARGET
|
||||
{
|
||||
float3 N = normalize(inPos);
|
||||
float3 up = float3(0.0, 1.0, 0.0);
|
||||
float3 right = normalize(cross(up, N));
|
||||
up = cross(N, right);
|
||||
|
||||
const float TWO_PI = PI * 2.0;
|
||||
const float HALF_PI = PI * 0.5;
|
||||
|
||||
float3 color = float3(0.0, 0.0, 0.0);
|
||||
uint sampleCount = 0u;
|
||||
for (float phi = 0.0; phi < TWO_PI; phi += consts.deltaPhi) {
|
||||
for (float theta = 0.0; theta < HALF_PI; theta += consts.deltaTheta) {
|
||||
float3 tempVec = cos(phi) * right + sin(phi) * up;
|
||||
float3 sampleVector = cos(theta) * N + sin(theta) * tempVec;
|
||||
color += textureEnv.Sample(samplerEnv, sampleVector).rgb * cos(theta) * sin(theta);
|
||||
sampleCount++;
|
||||
}
|
||||
}
|
||||
return float4(PI * color / float(sampleCount), 1.0);
|
||||
}
|
||||
189
data/hlsl/pbrtexture/pbrtexture.frag
Normal file
189
data/hlsl/pbrtexture/pbrtexture.frag
Normal file
|
|
@ -0,0 +1,189 @@
|
|||
// Copyright 2020 Google LLC
|
||||
|
||||
struct VSOutput
|
||||
{
|
||||
[[vk::location(0)]] float3 WorldPos : POSITION0;
|
||||
[[vk::location(1)]] float3 Normal : NORMAL0;
|
||||
[[vk::location(2)]] float2 UV : TEXCOORD0;
|
||||
};
|
||||
|
||||
struct UBO {
|
||||
float4x4 projection;
|
||||
float4x4 model;
|
||||
float4x4 view;
|
||||
float3 camPos;
|
||||
};
|
||||
|
||||
cbuffer ubo : register(b0) { UBO ubo; }
|
||||
|
||||
struct UBOParams {
|
||||
float4 lights[4];
|
||||
float exposure;
|
||||
float gamma;
|
||||
};
|
||||
cbuffer uboParams : register(b1) { UBOParams uboParams; };
|
||||
|
||||
TextureCube textureIrradiance : register(t2);
|
||||
SamplerState samplerIrradiance : register(s2);
|
||||
Texture2D textureBRDFLUT : register(t3);
|
||||
SamplerState samplerBRDFLUT : register(s3);
|
||||
TextureCube prefilteredMapTexture : register(t4);
|
||||
SamplerState prefilteredMapSampler : register(s4);
|
||||
|
||||
Texture2D albedoMapTexture : register(t5);
|
||||
SamplerState albedoMapSampler : register(s5);
|
||||
Texture2D normalMapTexture : register(t6);
|
||||
SamplerState normalMapSampler : register(s6);
|
||||
Texture2D aoMapTexture : register(t7);
|
||||
SamplerState aoMapSampler : register(s7);
|
||||
Texture2D metallicMapTexture : register(t8);
|
||||
SamplerState metallicMapSampler : register(s8);
|
||||
Texture2D roughnessMapTexture : register(t9);
|
||||
SamplerState roughnessMapSampler : register(s9);
|
||||
|
||||
#define PI 3.1415926535897932384626433832795
|
||||
#define ALBEDO(uv) pow(albedoMapTexture.Sample(albedoMapSampler, uv).rgb, float3(2.2, 2.2, 2.2))
|
||||
|
||||
// From http://filmicgames.com/archives/75
|
||||
float3 Uncharted2Tonemap(float3 x)
|
||||
{
|
||||
float A = 0.15;
|
||||
float B = 0.50;
|
||||
float C = 0.10;
|
||||
float D = 0.20;
|
||||
float E = 0.02;
|
||||
float F = 0.30;
|
||||
return ((x*(A*x+C*B)+D*E)/(x*(A*x+B)+D*F))-E/F;
|
||||
}
|
||||
|
||||
// Normal Distribution function --------------------------------------
|
||||
float D_GGX(float dotNH, float roughness)
|
||||
{
|
||||
float alpha = roughness * roughness;
|
||||
float alpha2 = alpha * alpha;
|
||||
float denom = dotNH * dotNH * (alpha2 - 1.0) + 1.0;
|
||||
return (alpha2)/(PI * denom*denom);
|
||||
}
|
||||
|
||||
// Geometric Shadowing function --------------------------------------
|
||||
float G_SchlicksmithGGX(float dotNL, float dotNV, float roughness)
|
||||
{
|
||||
float r = (roughness + 1.0);
|
||||
float k = (r*r) / 8.0;
|
||||
float GL = dotNL / (dotNL * (1.0 - k) + k);
|
||||
float GV = dotNV / (dotNV * (1.0 - k) + k);
|
||||
return GL * GV;
|
||||
}
|
||||
|
||||
// Fresnel function ----------------------------------------------------
|
||||
float3 F_Schlick(float cosTheta, float3 F0)
|
||||
{
|
||||
return F0 + (1.0 - F0) * pow(1.0 - cosTheta, 5.0);
|
||||
}
|
||||
float3 F_SchlickR(float cosTheta, float3 F0, float roughness)
|
||||
{
|
||||
return F0 + (max((1.0 - roughness).xxx, F0) - F0) * pow(1.0 - cosTheta, 5.0);
|
||||
}
|
||||
|
||||
float3 prefilteredReflection(float3 R, float roughness)
|
||||
{
|
||||
const float MAX_REFLECTION_LOD = 9.0; // todo: param/const
|
||||
float lod = roughness * MAX_REFLECTION_LOD;
|
||||
float lodf = floor(lod);
|
||||
float lodc = ceil(lod);
|
||||
float3 a = prefilteredMapTexture.SampleLevel(prefilteredMapSampler, R, lodf).rgb;
|
||||
float3 b = prefilteredMapTexture.SampleLevel(prefilteredMapSampler, R, lodc).rgb;
|
||||
return lerp(a, b, lod - lodf);
|
||||
}
|
||||
|
||||
float3 specularContribution(float2 inUV, float3 L, float3 V, float3 N, float3 F0, float metallic, float roughness)
|
||||
{
|
||||
// Precalculate vectors and dot products
|
||||
float3 H = normalize (V + L);
|
||||
float dotNH = clamp(dot(N, H), 0.0, 1.0);
|
||||
float dotNV = clamp(dot(N, V), 0.0, 1.0);
|
||||
float dotNL = clamp(dot(N, L), 0.0, 1.0);
|
||||
|
||||
// Light color fixed
|
||||
float3 lightColor = float3(1.0, 1.0, 1.0);
|
||||
|
||||
float3 color = float3(0.0, 0.0, 0.0);
|
||||
|
||||
if (dotNL > 0.0) {
|
||||
// D = Normal distribution (Distribution of the microfacets)
|
||||
float D = D_GGX(dotNH, roughness);
|
||||
// G = Geometric shadowing term (Microfacets shadowing)
|
||||
float G = G_SchlicksmithGGX(dotNL, dotNV, roughness);
|
||||
// F = Fresnel factor (Reflectance depending on angle of incidence)
|
||||
float3 F = F_Schlick(dotNV, F0);
|
||||
float3 spec = D * F * G / (4.0 * dotNL * dotNV + 0.001);
|
||||
float3 kD = (float3(1.0, 1.0, 1.0) - F) * (1.0 - metallic);
|
||||
color += (kD * ALBEDO(inUV) / PI + spec) * dotNL;
|
||||
}
|
||||
|
||||
return color;
|
||||
}
|
||||
|
||||
// See http://www.thetenthplanet.de/archives/1180
|
||||
float3 perturbNormal(float2 inUV, float3 inWorldPos, float3 inNormal)
|
||||
{
|
||||
float3 tangentNormal = normalMapTexture.Sample(normalMapSampler, inUV).xyz * 2.0 - 1.0;
|
||||
|
||||
float3 q1 = ddx(inWorldPos);
|
||||
float3 q2 = ddy(inWorldPos);
|
||||
float2 st1 = ddx(inUV);
|
||||
float2 st2 = ddy(inUV);
|
||||
|
||||
float3 N = normalize(inNormal);
|
||||
float3 T = normalize(q1 * st2.y - q2 * st1.y);
|
||||
float3 B = -normalize(cross(N, T));
|
||||
float3x3 TBN = transpose(float3x3(T, B, N));
|
||||
|
||||
return normalize(mul(TBN, tangentNormal));
|
||||
}
|
||||
|
||||
float4 main(VSOutput input) : SV_TARGET
|
||||
{
|
||||
float3 N = perturbNormal(input.UV, input.WorldPos, input.Normal);
|
||||
float3 V = normalize(ubo.camPos - input.WorldPos);
|
||||
float3 R = reflect(-V, N);
|
||||
|
||||
float metallic = metallicMapTexture.Sample(metallicMapSampler, input.UV).r;
|
||||
float roughness = roughnessMapTexture.Sample(roughnessMapSampler, input.UV).r;
|
||||
|
||||
float3 F0 = float3(0.04, 0.04, 0.04);
|
||||
F0 = lerp(F0, ALBEDO(input.UV), metallic);
|
||||
|
||||
float3 Lo = float3(0.0, 0.0, 0.0);
|
||||
for(int i = 0; i < 4; i++) {
|
||||
float3 L = normalize(uboParams.lights[i].xyz - input.WorldPos);
|
||||
Lo += specularContribution(input.UV, L, V, N, F0, metallic, roughness);
|
||||
}
|
||||
|
||||
float2 brdf = textureBRDFLUT.Sample(samplerBRDFLUT, float2(max(dot(N, V), 0.0), roughness)).rg;
|
||||
float3 reflection = prefilteredReflection(R, roughness).rgb;
|
||||
float3 irradiance = textureIrradiance.Sample(samplerIrradiance, N).rgb;
|
||||
|
||||
// Diffuse based on irradiance
|
||||
float3 diffuse = irradiance * ALBEDO(input.UV);
|
||||
|
||||
float3 F = F_SchlickR(max(dot(N, V), 0.0), F0, roughness);
|
||||
|
||||
// Specular reflectance
|
||||
float3 specular = reflection * (F * brdf.x + brdf.y);
|
||||
|
||||
// Ambient part
|
||||
float3 kD = 1.0 - F;
|
||||
kD *= 1.0 - metallic;
|
||||
float3 ambient = (kD * diffuse + specular) * aoMapTexture.Sample(aoMapSampler, input.UV).rrr;
|
||||
|
||||
float3 color = ambient + Lo;
|
||||
|
||||
// Tone mapping
|
||||
color = Uncharted2Tonemap(color * uboParams.exposure);
|
||||
color = color * (1.0f / Uncharted2Tonemap((11.2f).xxx));
|
||||
// Gamma correction
|
||||
color = pow(color, (1.0f / uboParams.gamma).xxx);
|
||||
|
||||
return float4(color, 1.0);
|
||||
}
|
||||
38
data/hlsl/pbrtexture/pbrtexture.vert
Normal file
38
data/hlsl/pbrtexture/pbrtexture.vert
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
// Copyright 2020 Google LLC
|
||||
|
||||
struct VSInput
|
||||
{
|
||||
[[vk::location(0)]] float3 Pos : POSITION0;
|
||||
[[vk::location(1)]] float3 Normal : NORMAL0;
|
||||
[[vk::location(2)]] float2 UV : TEXCOORD0;
|
||||
};
|
||||
|
||||
struct UBO
|
||||
{
|
||||
float4x4 projection;
|
||||
float4x4 model;
|
||||
float4x4 view;
|
||||
float3 camPos;
|
||||
};
|
||||
|
||||
cbuffer ubo : register(b0) { UBO ubo; }
|
||||
|
||||
struct VSOutput
|
||||
{
|
||||
float4 Pos : SV_POSITION;
|
||||
[[vk::location(0)]] float3 WorldPos : POSITION0;
|
||||
[[vk::location(1)]] float3 Normal : NORMAL0;
|
||||
[[vk::location(2)]] float2 UV : TEXCOORD0;
|
||||
};
|
||||
|
||||
VSOutput main(VSInput input)
|
||||
{
|
||||
VSOutput output = (VSOutput)0;
|
||||
float3 locPos = mul(ubo.model, float4(input.Pos, 1.0)).xyz;
|
||||
output.WorldPos = locPos;
|
||||
output.Normal = mul((float3x3)ubo.model, input.Normal);
|
||||
output.UV = input.UV;
|
||||
output.UV.y = 1.0 - input.UV.y;
|
||||
output.Pos = mul(ubo.projection, mul(ubo.view, float4(output.WorldPos, 1.0)));
|
||||
return output;
|
||||
}
|
||||
106
data/hlsl/pbrtexture/prefilterenvmap.frag
Normal file
106
data/hlsl/pbrtexture/prefilterenvmap.frag
Normal file
|
|
@ -0,0 +1,106 @@
|
|||
// Copyright 2020 Google LLC
|
||||
|
||||
TextureCube textureEnv : register(t0);
|
||||
SamplerState samplerEnv : register(s0);
|
||||
|
||||
struct PushConsts {
|
||||
[[vk::offset(64)]] float roughness;
|
||||
[[vk::offset(68)]] uint numSamples;
|
||||
};
|
||||
[[vk::push_constant]] PushConsts consts;
|
||||
|
||||
#define PI 3.1415926536
|
||||
|
||||
// Based omn http://byteblacksmith.com/improvements-to-the-canonical-one-liner-glsl-rand-for-opengl-es-2-0/
|
||||
float random(float2 co)
|
||||
{
|
||||
float a = 12.9898;
|
||||
float b = 78.233;
|
||||
float c = 43758.5453;
|
||||
float dt= dot(co.xy ,float2(a,b));
|
||||
float sn= fmod(dt,3.14);
|
||||
return frac(sin(sn) * c);
|
||||
}
|
||||
|
||||
float2 hammersley2d(uint i, uint N)
|
||||
{
|
||||
// Radical inverse based on http://holger.dammertz.org/stuff/notes_HammersleyOnHemisphere.html
|
||||
uint bits = (i << 16u) | (i >> 16u);
|
||||
bits = ((bits & 0x55555555u) << 1u) | ((bits & 0xAAAAAAAAu) >> 1u);
|
||||
bits = ((bits & 0x33333333u) << 2u) | ((bits & 0xCCCCCCCCu) >> 2u);
|
||||
bits = ((bits & 0x0F0F0F0Fu) << 4u) | ((bits & 0xF0F0F0F0u) >> 4u);
|
||||
bits = ((bits & 0x00FF00FFu) << 8u) | ((bits & 0xFF00FF00u) >> 8u);
|
||||
float rdi = float(bits) * 2.3283064365386963e-10;
|
||||
return float2(float(i) /float(N), rdi);
|
||||
}
|
||||
|
||||
// Based on http://blog.selfshadow.com/publications/s2013-shading-course/karis/s2013_pbs_epic_slides.pdf
|
||||
float3 importanceSample_GGX(float2 Xi, float roughness, float3 normal)
|
||||
{
|
||||
// Maps a 2D point to a hemisphere with spread based on roughness
|
||||
float alpha = roughness * roughness;
|
||||
float phi = 2.0 * PI * Xi.x + random(normal.xz) * 0.1;
|
||||
float cosTheta = sqrt((1.0 - Xi.y) / (1.0 + (alpha*alpha - 1.0) * Xi.y));
|
||||
float sinTheta = sqrt(1.0 - cosTheta * cosTheta);
|
||||
float3 H = float3(sinTheta * cos(phi), sinTheta * sin(phi), cosTheta);
|
||||
|
||||
// Tangent space
|
||||
float3 up = abs(normal.z) < 0.999 ? float3(0.0, 0.0, 1.0) : float3(1.0, 0.0, 0.0);
|
||||
float3 tangentX = normalize(cross(up, normal));
|
||||
float3 tangentY = normalize(cross(normal, tangentX));
|
||||
|
||||
// Convert to world Space
|
||||
return normalize(tangentX * H.x + tangentY * H.y + normal * H.z);
|
||||
}
|
||||
|
||||
// Normal Distribution function
|
||||
float D_GGX(float dotNH, float roughness)
|
||||
{
|
||||
float alpha = roughness * roughness;
|
||||
float alpha2 = alpha * alpha;
|
||||
float denom = dotNH * dotNH * (alpha2 - 1.0) + 1.0;
|
||||
return (alpha2)/(PI * denom*denom);
|
||||
}
|
||||
|
||||
float3 prefilterEnvMap(float3 R, float roughness)
|
||||
{
|
||||
float3 N = R;
|
||||
float3 V = R;
|
||||
float3 color = float3(0.0, 0.0, 0.0);
|
||||
float totalWeight = 0.0;
|
||||
int2 envMapDims;
|
||||
textureEnv.GetDimensions(envMapDims.x, envMapDims.y);
|
||||
float envMapDim = float(envMapDims.x);
|
||||
for(uint i = 0u; i < consts.numSamples; i++) {
|
||||
float2 Xi = hammersley2d(i, consts.numSamples);
|
||||
float3 H = importanceSample_GGX(Xi, roughness, N);
|
||||
float3 L = 2.0 * dot(V, H) * H - V;
|
||||
float dotNL = clamp(dot(N, L), 0.0, 1.0);
|
||||
if(dotNL > 0.0) {
|
||||
// Filtering based on https://placeholderart.wordpress.com/2015/07/28/implementation-notes-runtime-environment-map-filtering-for-image-based-lighting/
|
||||
|
||||
float dotNH = clamp(dot(N, H), 0.0, 1.0);
|
||||
float dotVH = clamp(dot(V, H), 0.0, 1.0);
|
||||
|
||||
// Probability Distribution Function
|
||||
float pdf = D_GGX(dotNH, roughness) * dotNH / (4.0 * dotVH) + 0.0001;
|
||||
// Slid angle of current smple
|
||||
float omegaS = 1.0 / (float(consts.numSamples) * pdf);
|
||||
// Solid angle of 1 pixel across all cube faces
|
||||
float omegaP = 4.0 * PI / (6.0 * envMapDim * envMapDim);
|
||||
// Biased (+1.0) mip level for better result
|
||||
float mipLevel = roughness == 0.0 ? 0.0 : max(0.5 * log2(omegaS / omegaP) + 1.0, 0.0f);
|
||||
color += textureEnv.SampleLevel(samplerEnv, L, mipLevel).rgb * dotNL;
|
||||
totalWeight += dotNL;
|
||||
|
||||
}
|
||||
}
|
||||
return (color / totalWeight);
|
||||
}
|
||||
|
||||
|
||||
float4 main([[vk::location(0)]] float3 inPos : POSITION0) : SV_TARGET
|
||||
{
|
||||
float3 N = normalize(inPos);
|
||||
return float4(prefilterEnvMap(N, consts.roughness), 1.0);
|
||||
}
|
||||
37
data/hlsl/pbrtexture/skybox.frag
Normal file
37
data/hlsl/pbrtexture/skybox.frag
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
// Copyright 2020 Google LLC
|
||||
|
||||
TextureCube textureEnv : register(t2);
|
||||
SamplerState samplerEnv : register(s2);
|
||||
|
||||
struct UBOParams {
|
||||
float4 lights[4];
|
||||
float exposure;
|
||||
float gamma;
|
||||
};
|
||||
cbuffer uboParams : register(b1) { UBOParams uboParams; };
|
||||
|
||||
// From http://filmicworlds.com/blog/filmic-tonemapping-operators/
|
||||
float3 Uncharted2Tonemap(float3 color)
|
||||
{
|
||||
float A = 0.15;
|
||||
float B = 0.50;
|
||||
float C = 0.10;
|
||||
float D = 0.20;
|
||||
float E = 0.02;
|
||||
float F = 0.30;
|
||||
float W = 11.2;
|
||||
return ((color*(A*color+C*B)+D*E)/(color*(A*color+B)+D*F))-E/F;
|
||||
}
|
||||
|
||||
float4 main([[vk::location(0)]] float3 inUVW : POSITION0) : SV_TARGET
|
||||
{
|
||||
float3 color = textureEnv.Sample(samplerEnv, inUVW).rgb;
|
||||
|
||||
// Tone mapping
|
||||
color = Uncharted2Tonemap(color * uboParams.exposure);
|
||||
color = color * (1.0f / Uncharted2Tonemap((11.2f).xxx));
|
||||
// Gamma correction
|
||||
color = pow(color, (1.0f / uboParams.gamma).xxx);
|
||||
|
||||
return float4(color, 1.0);
|
||||
}
|
||||
30
data/hlsl/pbrtexture/skybox.vert
Normal file
30
data/hlsl/pbrtexture/skybox.vert
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
// Copyright 2020 Google LLC
|
||||
|
||||
struct VSInput
|
||||
{
|
||||
[[vk::location(0)]] float3 Pos : POSITION0;
|
||||
[[vk::location(1)]] float3 Normal : NORMAL0;
|
||||
[[vk::location(2)]] float2 UV : TEXCOORD0;
|
||||
};
|
||||
|
||||
struct UBO
|
||||
{
|
||||
float4x4 projection;
|
||||
float4x4 model;
|
||||
};
|
||||
|
||||
cbuffer ubo : register(b0) { UBO ubo; }
|
||||
|
||||
struct VSOutput
|
||||
{
|
||||
float4 Pos : SV_POSITION;
|
||||
[[vk::location(0)]] float3 UVW : TEXCOORD0;
|
||||
};
|
||||
|
||||
VSOutput main(VSInput input)
|
||||
{
|
||||
VSOutput output = (VSOutput)0;
|
||||
output.UVW = input.Pos;
|
||||
output.Pos = mul(ubo.projection, mul(ubo.model, float4(input.Pos.xyz, 1.0)));
|
||||
return output;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue