procedural-3d-engine/data/shaders/hlsl/deferredshadows/deferred.frag
Sascha Willems feb939096f
Merge glTF branch (#747)
* Added helper function for easy pipeline vertex input state create info structure setup from glTF model vertex class

* Split glTF loader into header and implementation

* Updated sample to use glTF

* Removed collada files

Replaced assets are now part of the asset pack

* Return value for glTF model vertex input state create info helper

* Removed unused assets

* Use glTF assets

* Added default material for glTF node's without materials

* Use glTF assets

* Apply pre-transforms to normals

* Use glTF assets

* Use glTF assets

* Use vertex input state from glTF model class

* Scene setup

* Use glTF assets

* Use glTF assets

* Display error message and exit if glTF file could not be loaded

* Use glTF assets

* Use glTF assets

* Use glTF assets

* Remove unused buffer binds

* Use glTF assets

* Remove no longer used model files

* Remove no longer used model files

* Added support for rendering glTF models with images

* glTF model normal pre-transform ignores translation

* Use glTF assets

* Use glTF assets

* Use glTF assets

* Use glTF assets

* Use glTF assets

* Only add combined image samplers to pool if actually used in the scene

* Use global descriptor set layouts

* Use global descriptor set layouts

* Use glTF assets

* Use glTF assets

* Use glTF assets

Code cleanup
Updated GLSL and HLSL shaders

* Use glTF assets

Code cleanup

* Use glTF assets

Code cleanup
Updated GLSL and HLSL shaders

* Remove no-longer used model

* Use glTF assets

Code cleanup
Updated GLSL and HLSL shaders

* Use glTF assets

Code cleanup
Updated GLSL and HLSL shaders
Removed no-longer used model

* Use glTF assets

Code cleanup
Use RGBA texture instead of different compressed formats
Removed no-longer used assets

* Adnrdoid build file

* Use glTF assets

Code cleanup and refactoring
Updated GLSL and HLSL shaders

* Added vertex count and way of passing additional memory property type flags to glTF loader

* Use glTF assets

Updated GLSL and HLSL shaders
Removed no-longer used assets

* Use glTF assets

Updated GLSL and HLSL shaders

* Remove unfinished sample

* Completely reworked push constants sample

Use glTF assets
Updated GLSL and HLSL shaders
Removed no-longer used assets

* Android CMake build files

* Removed un-used asset

* Explicit buffer binding function

* Use glTF assets

Code cleanup
Updated GLSL and HLSL shaders

* Use glTF assets

Code cleanup

* Use glTF assets

Code cleanup
Removed no-longer used assets

* Use glTF assets

Code cleanup
Updated GLSL and HLSL shaders
Removed no-longer used assets

* Remove no-longer used asset

* Use glTF assets

Code cleanup and refactoring
Performance optimizations
Updated GLSL and HLSL shaders
Removed no-longer used assets

* Use glTF assets

Code cleanup and refactoring
Updated GLSL and HLSL shaders
Removed no-longer used assets

* Use glTF assets

Code cleanup and refactoring
Updated GLSL and HLSL shaders
Removed no-longer used assets

* Use glTF assets

Code cleanup and refactoring
Removed no-longer used assets

* Use glTF assets

Code cleanup and refactoring
Removed no-longer used assets

* Use glTF assets

Code cleanup and refactoring

* Use glTF assets

Code cleanup and refactoring

* Use glTF assets

Code cleanup and refactoring
Removed no-longer used assets

* Pass vertex size and calculate multiplier in shaders instead of hard-coding

With this, changes to the glTF vertex structure won't break the ray tracing samples

* Load tangents (if present)

* Use glTF assets

Code cleanup and heavy refactoring
Reworked debug display code

* Android build

* Normal mapping fixes

Udpated HLSL shaders

* Use glTF assets

Code cleanup and heavy refactoring
Reworked debug display code
Updated GLSL and HLSL shaders

* Code cleanup, comments

* Use glTF assets

Code cleanup and heavy refactoring
Reworked debug display code
Updated GLSL and HLSL shaders

* Added sample count to framebuffer create info

* Removed no-longer used assets

* Android build

Removed no-longer used assets

* Code cleanup and heavy refactoring

Updated GLSL and HLSL shaders
Use tangents stored in GLSL instead of calculating them in the fragment shader

* Renamed textured PBR sample main cpp file

* Use glTF assets

Code cleanup and refactoring
Updated GLSL and HLSL shaders
Removed no-longer used assets

* Use glTF assets

Removed no-longer used assets

* Android build files

* Android build files

* Use glTF assets

Removed no-longer used assets

* Fixed HLSL shaders

* Android build files

* Use glTF assets

Updated GLSL and HLSL shaders
Removed no-longer used assets

* Use glTF assets

Updated GLSL and HLSL shaders
Removed no-longer used assets

* Added flag to disable glTF image loading

Useful for samples that use their own textures or don't use textures at all to speed up loading

* Use glTF assets

Code cleanup
Use Sponza scene instead of Sibenik to better highlight the effect
Updated GLSL and HLSL shaders

* Updated Android build files

* Removed left-over comment

* Use Sponza scene for the SSAO sample

* Removed unused code

* Removed ASSIMP

No longer required as all samples now use the glTF file format

* Added missing vertex shader stage

* Removed old ASSIMP-based model loader

* Added support for loading external glTF images from ktx

Android fixes for loading external buffer files

* Scene setup

* Added missing shader stages

* Removed ASSIMP from build files

* Fixed compiler warning

* Removed ASSIMP from readmes

* Android build files cleanup

* Replaced ktx submodule with only the files required for this repo

The ktx submodule was a tad too big and contained lots of files not required for this repo

* Moved ktx build files into base project

* Use glTF assets

* Use glTF assets

* Removed license files, will be moved to asset pack

* Use RGBA textures

* Use RGBA cubemp texture with face assignment based on original images

Refs #679

* Android build files

* Removed textures

All textures will be moved to the asset pack

* Ignore asset folders

* Removed font

Fonts will be moved to the asset pack

* Link to gltf asset pack

* Updated gitignore

* Android build file
2020-07-28 20:20:38 +02:00

174 lines
No EOL
4.2 KiB
GLSL

// Copyright 2020 Google LLC
Texture2D textureposition : register(t1);
SamplerState samplerposition : register(s1);
Texture2D textureNormal : register(t2);
SamplerState samplerNormal : register(s2);
Texture2D textureAlbedo : register(t3);
SamplerState samplerAlbedo : register(s3);
// Depth from the light's point of view
//layout (binding = 5) uniform sampler2DShadow samplerShadowMap;
Texture2DArray textureShadowMap : register(t5);
SamplerState samplerShadowMap : register(s5);
#define LIGHT_COUNT 3
#define SHADOW_FACTOR 0.25
#define AMBIENT_LIGHT 0.1
#define USE_PCF
struct Light
{
float4 position;
float4 target;
float4 color;
float4x4 viewMatrix;
};
struct UBO
{
float4 viewPos;
Light lights[LIGHT_COUNT];
int useShadows;
int displayDebugTarget;
};
cbuffer ubo : register(b4) { UBO ubo; }
float textureProj(float4 P, float layer, float2 offset)
{
float shadow = 1.0;
float4 shadowCoord = P / P.w;
shadowCoord.xy = shadowCoord.xy * 0.5 + 0.5;
if (shadowCoord.z > -1.0 && shadowCoord.z < 1.0)
{
float dist = textureShadowMap.Sample(samplerShadowMap, float3(shadowCoord.xy + offset, layer)).r;
if (shadowCoord.w > 0.0 && dist < shadowCoord.z)
{
shadow = SHADOW_FACTOR;
}
}
return shadow;
}
float filterPCF(float4 sc, float layer)
{
int2 texDim; int elements; int levels;
textureShadowMap.GetDimensions(0, texDim.x, texDim.y, elements, levels);
float scale = 1.5;
float dx = scale * 1.0 / float(texDim.x);
float dy = scale * 1.0 / float(texDim.y);
float shadowFactor = 0.0;
int count = 0;
int range = 1;
for (int x = -range; x <= range; x++)
{
for (int y = -range; y <= range; y++)
{
shadowFactor += textureProj(sc, layer, float2(dx*x, dy*y));
count++;
}
}
return shadowFactor / count;
}
float3 shadow(float3 fragcolor, float3 fragPos) {
for (int i = 0; i < LIGHT_COUNT; ++i)
{
float4 shadowClip = mul(ubo.lights[i].viewMatrix, float4(fragPos.xyz, 1.0));
float shadowFactor;
#ifdef USE_PCF
shadowFactor= filterPCF(shadowClip, i);
#else
shadowFactor = textureProj(shadowClip, i, float2(0.0, 0.0));
#endif
fragcolor *= shadowFactor;
}
return fragcolor;
}
float4 main([[vk::location(0)]] float2 inUV : TEXCOORD0) : SV_TARGET
{
// Get G-Buffer values
float3 fragPos = textureposition.Sample(samplerposition, inUV).rgb;
float3 normal = textureNormal.Sample(samplerNormal, inUV).rgb;
float4 albedo = textureAlbedo.Sample(samplerAlbedo, inUV);
float3 fragcolor;
// Debug display
if (ubo.displayDebugTarget > 0) {
switch (ubo.displayDebugTarget) {
case 1:
fragcolor.rgb = shadow(float3(1.0, 1.0, 1.0), fragPos);
break;
case 2:
fragcolor.rgb = fragPos;
break;
case 3:
fragcolor.rgb = normal;
break;
case 4:
fragcolor.rgb = albedo.rgb;
break;
case 5:
fragcolor.rgb = albedo.aaa;
break;
}
return float4(fragcolor, 1.0);
}
// Ambient part
fragcolor = albedo.rgb * AMBIENT_LIGHT;
float3 N = normalize(normal);
for(int i = 0; i < LIGHT_COUNT; ++i)
{
// Vector to light
float3 L = ubo.lights[i].position.xyz - fragPos;
// Distance from light to fragment position
float dist = length(L);
L = normalize(L);
// Viewer to fragment
float3 V = ubo.viewPos.xyz - fragPos;
V = normalize(V);
float lightCosInnerAngle = cos(radians(15.0));
float lightCosOuterAngle = cos(radians(25.0));
float lightRange = 100.0;
// Direction vector from source to target
float3 dir = normalize(ubo.lights[i].position.xyz - ubo.lights[i].target.xyz);
// Dual cone spot light with smooth transition between inner and outer angle
float cosDir = dot(L, dir);
float spotEffect = smoothstep(lightCosOuterAngle, lightCosInnerAngle, cosDir);
float heightAttenuation = smoothstep(lightRange, 0.0f, dist);
// Diffuse lighting
float NdotL = max(0.0, dot(N, L));
float3 diff = NdotL.xxx;
// Specular lighting
float3 R = reflect(-L, N);
float NdotR = max(0.0, dot(R, V));
float3 spec = (pow(NdotR, 16.0) * albedo.a * 2.5).xxx;
fragcolor += float3((diff + spec) * spotEffect * heightAttenuation) * ubo.lights[i].color.rgb * albedo.rgb;
}
// Shadow calculations in a separate pass
if (ubo.useShadows > 0)
{
fragcolor = shadow(fragcolor, fragPos);
}
return float4(fragcolor, 1);
}