merging light shading paths part1

This commit is contained in:
turanszkij
2016-10-06 01:31:53 +02:00
parent 6932beecf7
commit 311659d8a9
25 changed files with 454 additions and 612 deletions
+14 -17
View File
@@ -10,31 +10,28 @@
#define CBSLOT_RENDERER_FRAME 1
#define CBSLOT_RENDERER_CAMERA 2
#define CBSLOT_RENDERER_MATERIAL 3
#define CBSLOT_RENDERER_DIRLIGHT 4
#define CBSLOT_RENDERER_MISC 5
#define CBSLOT_RENDERER_SHADOW 6
#define CBSLOT_RENDERER_MISC 4
#define CBSLOT_RENDERER_SHADOW 5
#define CBSLOT_IMAGE_IMAGE 7
#define CBSLOT_IMAGE_POSTPROCESS 8
#define CBSLOT_IMAGE_IMAGE 6
#define CBSLOT_IMAGE_POSTPROCESS 7
#define CBSLOT_FONT_FONT 9
#define CBSLOT_FONT_FONT 8
#define CBSLOT_API 10
#define CBSLOT_API 9
// On demand buffers:
// These are bound on demand and alive until another is bound at the same slot
#define CBSLOT_RENDERER_CUBEMAPRENDER 11
#define CBSLOT_RENDERER_POINTLIGHT 12
#define CBSLOT_RENDERER_SPOTLIGHT 13
#define CBSLOT_RENDERER_VOLUMELIGHT 11
#define CBSLOT_RENDERER_DECAL 11
#define CBSLOT_RENDERER_TESSELLATION 11
#define CBSLOT_RENDERER_DISPATCHPARAMS 11
#define CBSLOT_RENDERER_CUBEMAPRENDER 10
#define CBSLOT_RENDERER_VOLUMELIGHT 10
#define CBSLOT_RENDERER_DECAL 10
#define CBSLOT_RENDERER_TESSELLATION 10
#define CBSLOT_RENDERER_DISPATCHPARAMS 10
#define CBSLOT_OTHER_EMITTEDPARTICLE 11
#define CBSLOT_OTHER_HAIRPARTICLE 11
#define CBSLOT_OTHER_LENSFLARE 11
#define CBSLOT_OTHER_EMITTEDPARTICLE 10
#define CBSLOT_OTHER_HAIRPARTICLE 10
#define CBSLOT_OTHER_LENSFLARE 10
+2 -2
View File
@@ -24,9 +24,9 @@ public:
class Editor;
class EditorComponent
//: public DeferredRenderableComponent
: public DeferredRenderableComponent
//: public ForwardRenderableComponent
: public TiledForwardRenderableComponent
//: public TiledForwardRenderableComponent
{
private:
wiGraphicsTypes::Texture2D pointLightTex, spotLightTex, dirLightTex;
+28 -34
View File
@@ -18,49 +18,43 @@
// Textures, StructuredBuffers (t slot):
#define TEXSLOT_DEPTH 0
#define TEXSLOT_LINEARDEPTH 1
#define TEXSLOT_DEPTH 0
#define TEXSLOT_LINEARDEPTH 1
#define TEXSLOT_GBUFFER0 2
#define TEXSLOT_GBUFFER1 3
#define TEXSLOT_GBUFFER2 4
#define TEXSLOT_GBUFFER3 5
#define TEXSLOT_GBUFFER4 6
#define TEXSLOT_GBUFFER0 2
#define TEXSLOT_GBUFFER1 3
#define TEXSLOT_GBUFFER2 4
#define TEXSLOT_GBUFFER3 5
#define TEXSLOT_GBUFFER4 6
#define TEXSLOT_ENV_GLOBAL 7
#define TEXSLOT_ENV0 8
#define TEXSLOT_ENV1 9
#define TEXSLOT_ENV2 10
#define TEXSLOT_ENV_GLOBAL 7
#define TEXSLOT_ENV0 8
#define TEXSLOT_ENV1 9
#define TEXSLOT_ENV2 10
#define TEXSLOT_SHADOW0 11
#define TEXSLOT_SHADOW1 12
#define TEXSLOT_SHADOW2 13
#define TEXSLOT_SHADOW_CUBE 14
#define TEXSLOT_SHADOWARRAY_2D 11
#define TEXSLOT_SHADOWARRAY_CUBE 12
#define TEXSLOT_ONDEMAND0 15
#define TEXSLOT_ONDEMAND1 16
#define TEXSLOT_ONDEMAND2 17
#define TEXSLOT_ONDEMAND3 18
#define TEXSLOT_ONDEMAND4 19
#define TEXSLOT_ONDEMAND5 20
#define TEXSLOT_ONDEMAND6 21
#define TEXSLOT_ONDEMAND7 22
#define TEXSLOT_ONDEMAND8 23
#define TEXSLOT_ONDEMAND9 24
#define TEXSLOT_ONDEMAND0 13
#define TEXSLOT_ONDEMAND1 14
#define TEXSLOT_ONDEMAND2 15
#define TEXSLOT_ONDEMAND3 16
#define TEXSLOT_ONDEMAND4 17
#define TEXSLOT_ONDEMAND5 18
#define TEXSLOT_ONDEMAND6 19
#define TEXSLOT_ONDEMAND7 20
#define TEXSLOT_ONDEMAND8 21
#define TEXSLOT_ONDEMAND9 22
#define TEXSLOT_ONDEMAND_COUNT (TEXSLOT_ONDEMAND9 - TEXSLOT_ONDEMAND0 + 1)
#define TEXSLOT_LIGHTGRID 25
#define TEXSLOT_LIGHTGRID 23
#define TEXSLOT_COUNT TEXSLOT_LIGHTGRID
#define SBSLOT_BONE 0
#define SBSLOT_TILEFRUSTUMS 25
#define SBSLOT_LIGHTARRAY 26
#define SBSLOT_LIGHTINDEXLIST 27
#define TEXSLOT_SHADOWARRAY_2D 28
#define TEXSLOT_SHADOWARRAY_CUBE 29
#define SBSLOT_BONE 0
#define SBSLOT_TILEFRUSTUMS 23
#define SBSLOT_LIGHTINDEXLIST 24
#define SBSLOT_LIGHTARRAY 25
///////////////////////////
+2 -5
View File
@@ -17,7 +17,6 @@
<None Include="cube.hlsli" />
<None Include="cullingShaderHF.hlsli" />
<None Include="depthConvertHF.hlsli" />
<None Include="dirLightHF.hlsli" />
<None Include="ditherHF.hlsli" />
<None Include="envMapHF.hlsli" />
<None Include="envReflectionHF.hlsli" />
@@ -29,21 +28,19 @@
<None Include="grassHF_PS.hlsli" />
<None Include="icosphere.hlsli" />
<None Include="imageHF.hlsli" />
<None Include="lightHF.hlsli" />
<None Include="deferredLightHF.hlsli" />
<None Include="lightingHF.hlsli" />
<None Include="normalsCompressHF.hlsli" />
<None Include="objectHF.hlsli" />
<None Include="objectInputLayoutHF.hlsli" />
<None Include="packHF.hlsli" />
<None Include="pointLightHF.hlsli" />
<None Include="postProcessHF.hlsli" />
<None Include="quad.hlsli" />
<None Include="reconstructPositionHF.hlsli" />
<None Include="skinningHF.hlsli" />
<None Include="skyHF.hlsli" />
<None Include="sOHF.hlsli" />
<None Include="spotLightHF.hlsli" />
<None Include="tangentComputeHF.hlsli" />
<None Include="tiledLightingHF.hlsli" />
<None Include="uvsphere.hlsli" />
<None Include="volumeLightHF.hlsli" />
<None Include="windHF.hlsli" />
@@ -13,9 +13,6 @@
<None Include="depthConvertHF.hlsli">
<Filter>HF</Filter>
</None>
<None Include="dirLightHF.hlsli">
<Filter>HF</Filter>
</None>
<None Include="ditherHF.hlsli">
<Filter>HF</Filter>
</None>
@@ -43,9 +40,6 @@
<None Include="icosphere.hlsli">
<Filter>HF</Filter>
</None>
<None Include="lightHF.hlsli">
<Filter>HF</Filter>
</None>
<None Include="normalsCompressHF.hlsli">
<Filter>HF</Filter>
</None>
@@ -58,9 +52,6 @@
<None Include="packHF.hlsli">
<Filter>HF</Filter>
</None>
<None Include="pointLightHF.hlsli">
<Filter>HF</Filter>
</None>
<None Include="postProcessHF.hlsli">
<Filter>HF</Filter>
</None>
@@ -79,9 +70,6 @@
<None Include="sOHF.hlsli">
<Filter>HF</Filter>
</None>
<None Include="spotLightHF.hlsli">
<Filter>HF</Filter>
</None>
<None Include="tangentComputeHF.hlsli">
<Filter>HF</Filter>
</None>
@@ -106,7 +94,10 @@
<None Include="cullingShaderHF.hlsli">
<Filter>HF</Filter>
</None>
<None Include="tiledLightingHF.hlsli">
<None Include="lightingHF.hlsli">
<Filter>HF</Filter>
</None>
<None Include="deferredLightHF.hlsli">
<Filter>HF</Filter>
</None>
</ItemGroup>
+1 -2
View File
@@ -1,4 +1,3 @@
#include "pointLightHF.hlsli"
#include "objectHF.hlsli"
struct VertextoPixel
@@ -12,5 +11,5 @@ struct VertextoPixel
float main(VertextoPixel PSIn) : SV_DEPTH
{
ALPHATEST(xBaseColorMap.Sample(sampler_linear_wrap,PSIn.tex).a);
return distance(PSIn.pos3D.xyz, xLightPos.xyz) / xLightEnerDis.y;
return distance(PSIn.pos3D.xyz, g_xColor.xyz) / g_xColor.w;
}
@@ -3,19 +3,16 @@
#include "globals.hlsli"
#include "brdf.hlsli"
#include "packHF.hlsli"
#include "lightingHF.hlsli"
struct VertexToPixel{
float4 pos : SV_POSITION;
float4 pos2D : POSITION2D;
nointerpolation int lightIndex : LIGHTARRAYINDEX;
};
#include "reconstructPositionHF.hlsli"
//Texture2D<float> depthMap:register(t0);
//Texture2D<float4> normalMap:register(t1);
////Texture2D<float4> specularMap:register(t2);
//Texture2D<float4> materialMap:register(t2);
static const float specularMaximumIntensity = 1;
@@ -29,31 +26,50 @@ struct LightOutputType
// MACROS
#define DEFERREDLIGHT_MAKEPARAMS \
LightArrayType light = LightArray[PSIn.lightIndex]; \
float3 diffuse, specular; \
float2 screenPos = float2(1, -1) * PSIn.pos2D.xy / PSIn.pos2D.w / 2.0f + 0.5f; \
float depth = texture_depth.SampleLevel(sampler_point_clamp, screenPos, 0); \
float4 baseColor = texture_gbuffer0.SampleLevel(sampler_point_clamp,screenPos,0); \
float4 g1 = texture_gbuffer1.SampleLevel(sampler_point_clamp,screenPos,0); \
float4 g3 = texture_gbuffer3.SampleLevel(sampler_point_clamp,screenPos,0); \
float3 N = decode(g1.xy); \
float3 N = decode(g1.xy); \
float roughness = g3.x; \
float reflectance = g3.y; \
float metalness = g3.z; \
BRDF_HELPER_MAKEINPUTS( baseColor, reflectance, metalness ) \
float3 P = getPosition(screenPos, depth); \
float3 V = normalize(g_xCamera_CamPos - P);
#define DEFERREDLIGHT_RETURN \
LightOutputType Out = (LightOutputType)0; \
Out.diffuse = float4(diffuse, 1); \
Out.specular = float4(specular, 1); \
return Out;
float3 V = normalize(g_xCamera_CamPos - P); \
float3 L = light.positionWS - P; \
float lightDistance = length(L);
#define DEFERREDLIGHT_ENVIRONMENTALLIGHT \
diffuse = 0; \
#define DEFERREDLIGHT_ENVIRONMENTALLIGHT \
diffuse = 0; \
specular = EnvironmentReflection(N, V, P, roughness, f0);
#define DEFERREDLIGHT_DIRECTIONAL \
LightingResult result = DirectionalLight(light, N, V, P, roughness, f0); \
diffuse = result.diffuse; \
specular = result.specular;
#define DEFERREDLIGHT_SPOT \
LightingResult result = SpotLight(light, L, lightDistance, N, V, P, roughness, f0); \
diffuse = result.diffuse; \
specular = result.specular;
#define DEFERREDLIGHT_POINT \
LightingResult result = PointLight(light, L, lightDistance, N, V, P, roughness, f0);\
diffuse = result.diffuse; \
specular = result.specular;
#define DEFERREDLIGHT_RETURN \
LightOutputType Out = (LightOutputType)0; \
Out.diffuse = float4(diffuse, 1); \
Out.specular = float4(specular, 1); \
return Out;
#endif // _LIGHTHF_
-108
View File
@@ -1,108 +0,0 @@
#ifndef _DIRLIGHT_HF_
#define _DIRLIGHT_HF_
#include "brdf.hlsli"
#include "envReflectionHF.hlsli"
// dir light constant buffer is global
//Texture2D<float> xTextureSh[3]:register(t13);
inline float offset_lookup(Texture2D<float> intex, SamplerComparisonState map,
float2 loc,
float2 offset,
float scale,
float biasedDistance)
{
return intex.SampleCmpLevelZero( map, loc + offset / scale, biasedDistance).r;
}
inline float shadowCascade(float4 shadowPos, float2 ShTex, float bias, Texture2D<float> shadowTexture){
float realDistance = shadowPos.z/shadowPos.w - bias;
float sum = 0;
float scale = g_xDirLight_mBiasResSoftshadow.y;
float retVal = 1;
#ifdef DIRECTIONALLIGHT_SOFT
//[branch]if(g_xDirLight_mBiasResSoftshadow.z)
{
float samples = 0.0f;
static const float range = 1.5f;
for (float y = -range; y <= range; y += 1.0f)
for (float x = -range; x <= range; x += 1.0f)
{
sum += offset_lookup(shadowTexture, sampler_cmp_depth, ShTex, float2(x, y), scale, realDistance);
samples++;
}
retVal *= sum / samples;
}
//else
#else
retVal *= offset_lookup(shadowTexture, sampler_cmp_depth, ShTex, float2(0, 0), scale, realDistance);
#endif
return retVal;
}
inline void dirLight(in float3 P, in float3 N, in float3 V, in float roughness, in float3 f0,
out float3 diffuse, out float3 specular)
{
float3 L = g_xDirLight_direction.xyz;
BRDF_MAKE(N, L, V);
specular = g_xDirLight_col.rgb * BRDF_SPECULAR(roughness, f0);
diffuse = g_xDirLight_col.rgb * BRDF_DIFFUSE(roughness);
float sh = max(NdotL, 0);
float4 ShPos[3];
ShPos[0] = mul(float4(P,1),g_xDirLight_ShM[0]);
ShPos[1] = mul(float4(P,1),g_xDirLight_ShM[1]);
ShPos[2] = mul(float4(P,1),g_xDirLight_ShM[2]);
float3 ShTex[3];
ShTex[0] = ShPos[0].xyz*float3(1,-1,1)/ShPos[0].w/2.0f +0.5f;
ShTex[1] = ShPos[1].xyz*float3(1,-1,1)/ShPos[1].w/2.0f +0.5f;
ShTex[2] = ShPos[2].xyz*float3(1,-1,1)/ShPos[2].w/2.0f +0.5f;
const float shadows[3]={
shadowCascade(ShPos[0],ShTex[0].xy, g_xDirLight_mBiasResSoftshadow.x,texture_shadow0),
shadowCascade(ShPos[1],ShTex[1].xy, g_xDirLight_mBiasResSoftshadow.x,texture_shadow1),
shadowCascade(ShPos[2],ShTex[2].xy, g_xDirLight_mBiasResSoftshadow.x,texture_shadow2)
};
[branch]if((saturate(ShTex[2].x) == ShTex[2].x) && (saturate(ShTex[2].y) == ShTex[2].y) && (saturate(ShTex[2].z) == ShTex[2].z))
{
//color.r+=0.5f;
const float2 lerpVal = abs( ShTex[2].xy*2-1 );
sh *= lerp( shadows[2],shadows[1], pow( max(lerpVal.x,lerpVal.y),4 ) );
}
else [branch]if((saturate(ShTex[1].x) == ShTex[1].x) && (saturate(ShTex[1].y) == ShTex[1].y) && (saturate(ShTex[1].z) == ShTex[1].z))
{
//color.g+=0.5f;
const float2 lerpVal = abs( ShTex[1].xy*2-1 );
sh *= lerp( shadows[1],shadows[0], pow( max(lerpVal.x,lerpVal.y),4 ) );
}
else [branch]if((saturate(ShTex[0].x) == ShTex[0].x) && (saturate(ShTex[0].y) == ShTex[0].y) && (saturate(ShTex[0].z) == ShTex[0].z))
{
//color.b+=0.5f;
sh *= shadows[0];
}
diffuse *= sh;
specular *= sh;
//specular += EnvironmentReflection(N, V, P, roughness, f0);
diffuse = max(diffuse, 0);
specular = max(specular, 0);
}
// MACROS
//#define DEFERRED_DIRLIGHT_MAIN \
// float lighting = dirLight(P,N,color); \
// color.rgb *= lighting; \
// applySpecular(color, (color*lighting).rgb, N, V, g_xDirLight_direction.xyz, 1, specular_power, specular);
#define DEFERRED_DIRLIGHT_MAIN \
dirLight(P, N, V, roughness, f0, diffuse, specular);
#endif // _DIRLIGHT_HF_
+2 -5
View File
@@ -1,13 +1,10 @@
#include "lightHF.hlsli"
#include "dirLightHF.hlsli"
#include "deferredLightHF.hlsli"
LightOutputType main( VertexToPixel PSIn )
{
DEFERREDLIGHT_MAKEPARAMS
DEFERRED_DIRLIGHT_MAIN
DEFERREDLIGHT_DIRECTIONAL
DEFERREDLIGHT_RETURN
}
+2 -6
View File
@@ -1,15 +1,11 @@
#include "lightHF.hlsli"
#define DIRECTIONALLIGHT_SOFT
#include "dirLightHF.hlsli"
#include "deferredLightHF.hlsli"
LightOutputType main(VertexToPixel PSIn)
{
DEFERREDLIGHT_MAKEPARAMS
DEFERRED_DIRLIGHT_MAIN
DEFERREDLIGHT_DIRECTIONAL
DEFERREDLIGHT_RETURN
}
+3 -2
View File
@@ -1,5 +1,4 @@
#include "lightHF.hlsli"
#include "quad.hlsli"
#include "deferredLightHF.hlsli"
#include "fullScreenTriangleHF.hlsli"
VertexToPixel main(uint vid : SV_VERTEXID)
@@ -9,5 +8,7 @@ VertexToPixel main(uint vid : SV_VERTEXID)
FullScreenTriangle(vid, Out.pos);
Out.pos2D = Out.pos;
Out.lightIndex = g_xMisc_int4[0];
return Out;
}
+1 -1
View File
@@ -1,4 +1,4 @@
#include "lightHF.hlsli"
#include "deferredLightHF.hlsli"
#include "envReflectionHF.hlsli"
+3 -13
View File
@@ -15,10 +15,8 @@ TEXTURECUBE(texture_env_global, float4, TEXSLOT_ENV_GLOBAL)
TEXTURECUBE(texture_env0, float4, TEXSLOT_ENV0)
TEXTURECUBE(texture_env1, float4, TEXSLOT_ENV1)
TEXTURECUBE(texture_env2, float4, TEXSLOT_ENV2)
TEXTURE2D(texture_shadow0, float, TEXSLOT_SHADOW0)
TEXTURE2D(texture_shadow1, float, TEXSLOT_SHADOW1)
TEXTURE2D(texture_shadow2, float, TEXSLOT_SHADOW2)
TEXTURECUBE(texture_shadow_cube, float, TEXSLOT_SHADOW_CUBE)
TEXTURE2DARRAY(texture_shadowarray_2d, float, TEXSLOT_SHADOWARRAY_2D)
TEXTURECUBEARRAY(texture_shadowarray_cube, float, TEXSLOT_SHADOWARRAY_CUBE)
TEXTURE2D(texture_0, float4, TEXSLOT_ONDEMAND0)
TEXTURE2D(texture_1, float4, TEXSLOT_ONDEMAND1)
TEXTURE2D(texture_2, float4, TEXSLOT_ONDEMAND2)
@@ -30,8 +28,6 @@ TEXTURE2D(texture_7, float4, TEXSLOT_ONDEMAND7)
TEXTURE2D(texture_8, float4, TEXSLOT_ONDEMAND8)
TEXTURE2D(texture_9, float4, TEXSLOT_ONDEMAND9)
TEXTURE2DARRAY(texture_shadowarray_2d, float, TEXSLOT_SHADOWARRAY_2D)
TEXTURECUBEARRAY(texture_shadowarray_cube,float,TEXSLOT_SHADOWARRAY_CUBE)
SAMPLERSTATE( sampler_linear_clamp, SSLOT_LINEAR_CLAMP )
SAMPLERSTATE( sampler_linear_wrap, SSLOT_LINEAR_WRAP )
@@ -96,17 +92,11 @@ CBUFFER(MaterialCB, CBSLOT_RENDERER_MATERIAL)
float g_xMat_normalMapStrength;
float g_xMat_parallaxOcclusionMapping;
};
CBUFFER(DirectionalLightCB, CBSLOT_RENDERER_DIRLIGHT)
{
float4 g_xDirLight_direction;
float4 g_xDirLight_col;
float4 g_xDirLight_mBiasResSoftshadow;
float4x4 g_xDirLight_ShM[3];
};
CBUFFER(MiscCB, CBSLOT_RENDERER_MISC)
{
float4x4 g_xTransform;
float4 g_xColor;
int4 g_xMisc_int4;
};
CBUFFER(ShadowCB, CBSLOT_RENDERER_SHADOW)
{
+1 -1
View File
@@ -1,6 +1,6 @@
#include "globals.hlsli"
#include "cullingShaderHF.hlsli"
#include "tiledLightingHF.hlsli"
#include "lightingHF.hlsli"
#ifdef DEBUG_TILEDLIGHTCULLING
RWTEXTURE2D(DebugTexture, float4, UAVSLOT_DEBUGTEXTURE);
+194
View File
@@ -0,0 +1,194 @@
#ifndef _LIGHTING_HF_
#define _LIGHTING_HF_
#include "globals.hlsli"
#include "brdf.hlsli"
struct LightArrayType
{
float3 positionVS; // View Space!
float range;
// --
float4 color;
// --
float3 positionWS;
float energy;
// --
float3 directionVS;
float _pad0;
// --
float3 directionWS;
uint type;
// --
float shadowBias;
int shadowMap_index;
float coneAngle;
float coneAngleCos;
// --
float4x4 shadowMat[3];
};
TEXTURE2D(LightGrid, uint2, TEXSLOT_LIGHTGRID);
STRUCTUREDBUFFER(LightIndexList, uint, SBSLOT_LIGHTINDEXLIST);
STRUCTUREDBUFFER(LightArray, LightArrayType, SBSLOT_LIGHTARRAY);
struct LightingResult
{
float3 diffuse;
float3 specular;
};
inline float offset_lookup(float2 loc, float2 offset, float scale, float biasedDistance, float slice)
{
return texture_shadowarray_2d.SampleCmpLevelZero(sampler_cmp_depth, float3(loc + offset / scale, slice), biasedDistance).r;
}
inline float shadowCascade(float4 shadowPos, float2 ShTex, float bias, float slice) {
float realDistance = shadowPos.z / shadowPos.w - bias;
float sum = 0;
float scale = 1.0f;
float retVal = 1;
#ifdef DIRECTIONALLIGHT_SOFT
float samples = 0.0f;
static const float range = 1.5f;
for (float y = -range; y <= range; y += 1.0f)
{
for (float x = -range; x <= range; x += 1.0f)
{
sum += offset_lookup(ShTex, float2(x, y), scale, realDistance, slice);
samples++;
}
}
retVal *= sum / samples;
#else
retVal *= offset_lookup(ShTex, float2(0, 0), scale, realDistance, slice);
#endif
return retVal;
}
inline LightingResult DirectionalLight(in LightArrayType light, in float3 N, in float3 V, in float3 P, in float roughness, in float3 f0)
{
LightingResult result;
float3 lightColor = light.color.rgb*light.energy;
float3 L = light.directionWS.xyz;
BRDF_MAKE(N, L, V);
result.specular = lightColor * BRDF_SPECULAR(roughness, f0);
result.diffuse = lightColor * BRDF_DIFFUSE(roughness);
float sh = max(NdotL, 0);
[branch]
if (light.shadowMap_index >= 0)
{
float4 ShPos[3];
ShPos[0] = mul(float4(P, 1), light.shadowMat[0]);
ShPos[1] = mul(float4(P, 1), light.shadowMat[1]);
ShPos[2] = mul(float4(P, 1), light.shadowMat[2]);
float3 ShTex[3];
ShTex[0] = ShPos[0].xyz*float3(1, -1, 1) / ShPos[0].w / 2.0f + 0.5f;
ShTex[1] = ShPos[1].xyz*float3(1, -1, 1) / ShPos[1].w / 2.0f + 0.5f;
ShTex[2] = ShPos[2].xyz*float3(1, -1, 1) / ShPos[2].w / 2.0f + 0.5f;
[branch]if ((saturate(ShTex[2].x) == ShTex[2].x) && (saturate(ShTex[2].y) == ShTex[2].y) && (saturate(ShTex[2].z) == ShTex[2].z))
{
const float shadows[] = {
shadowCascade(ShPos[1],ShTex[1].xy, light.shadowBias,light.shadowMap_index + 1),
shadowCascade(ShPos[2],ShTex[2].xy, light.shadowBias,light.shadowMap_index + 2)
};
const float2 lerpVal = abs(ShTex[2].xy * 2 - 1);
sh *= lerp(shadows[1], shadows[0], pow(max(lerpVal.x, lerpVal.y), 4));
}
else[branch]if ((saturate(ShTex[1].x) == ShTex[1].x) && (saturate(ShTex[1].y) == ShTex[1].y) && (saturate(ShTex[1].z) == ShTex[1].z))
{
const float shadows[] = {
shadowCascade(ShPos[0],ShTex[0].xy, light.shadowBias,light.shadowMap_index + 0),
shadowCascade(ShPos[1],ShTex[1].xy, light.shadowBias,light.shadowMap_index + 1),
};
const float2 lerpVal = abs(ShTex[1].xy * 2 - 1);
sh *= lerp(shadows[1], shadows[0], pow(max(lerpVal.x, lerpVal.y), 4));
}
else[branch]if ((saturate(ShTex[0].x) == ShTex[0].x) && (saturate(ShTex[0].y) == ShTex[0].y) && (saturate(ShTex[0].z) == ShTex[0].z))
{
sh *= shadowCascade(ShPos[0], ShTex[0].xy, light.shadowBias, light.shadowMap_index + 0);
}
}
result.diffuse *= sh;
result.specular *= sh;
result.diffuse = max(0.0f, result.diffuse);
result.specular = max(0.0f, result.specular);
return result;
}
inline LightingResult PointLight(in LightArrayType light, in float3 L, in float distance, in float3 N, in float3 V, in float3 P, in float roughness, in float3 f0)
{
LightingResult result = (LightingResult)0;
float3 lightColor = light.color.rgb*light.energy;
BRDF_MAKE(N, L, V);
result.specular = lightColor * BRDF_SPECULAR(roughness, f0);
result.diffuse = lightColor * BRDF_DIFFUSE(roughness);
float att = (light.energy * (light.range / (light.range + 1 + distance)));
float attenuation = (att * (light.range - distance) / light.range);
result.diffuse *= attenuation;
result.specular *= attenuation;
float sh = max(NdotL, 0);
[branch]
if (light.shadowMap_index >= 0) {
static const float bias = 0.025;
sh *= texture_shadowarray_cube.SampleCmpLevelZero(sampler_cmp_depth, float4(-L, light.shadowMap_index), distance / light.range - bias).r;
}
result.diffuse *= sh;
result.specular *= sh;
result.diffuse = max(0.0f, result.diffuse);
result.specular = max(0.0f, result.specular);
return result;
}
inline LightingResult SpotLight(in LightArrayType light, in float3 L, in float distance, in float3 N, in float3 V, in float3 P, in float roughness, in float3 f0)
{
LightingResult result = (LightingResult)0;
float3 lightColor = light.color.rgb*light.energy;
float SpotFactor = dot(L, light.directionWS);
float spotCutOff = light.coneAngleCos;
[branch]
if (SpotFactor > spotCutOff)
{
BRDF_MAKE(N, L, V);
result.specular = lightColor * BRDF_SPECULAR(roughness, f0);
result.diffuse = lightColor * BRDF_DIFFUSE(roughness);
float att = (light.energy * (light.range / (light.range + 1 + distance)));
float attenuation = (att * (light.range - distance) / light.range);
attenuation *= saturate((1.0 - (1.0 - SpotFactor) * 1.0 / (1.0 - spotCutOff)));
result.diffuse *= attenuation;
result.specular *= attenuation;
float sh = max(NdotL, 0);
[branch]
if (light.shadowMap_index >= 0)
{
float4 ShPos = mul(float4(P, 1), light.shadowMat[0]);
float2 ShTex = ShPos.xy / ShPos.w * float2(0.5f, -0.5f) + float2(0.5f, 0.5f);
[branch]
if ((saturate(ShTex.x) == ShTex.x) && (saturate(ShTex.y) == ShTex.y))
{
sh *= shadowCascade(ShPos, ShTex.xy, light.shadowBias, light.shadowMap_index);
}
}
result.diffuse *= sh;
result.specular *= sh;
result.diffuse = max(result.diffuse, 0);
result.specular = max(result.specular, 0);
}
result.diffuse = max(0.0f, result.diffuse);
result.specular = max(0.0f, result.specular);
return result;
}
#endif // _LIGHTING_HF_
+9 -171
View File
@@ -7,20 +7,15 @@
#include "tangentComputeHF.hlsli"
#include "depthConvertHF.hlsli"
#include "fogHF.hlsli"
#include "dirLightHF.hlsli"
#include "brdf.hlsli"
#include "envReflectionHF.hlsli"
#include "packHF.hlsli"
#include "lightCullingCSInterop.h"
#include "tiledLightingHF.hlsli"
#include "lightingHF.hlsli"
// DEFINITIONS
//////////////////
TEXTURE2D(LightGrid, uint2, TEXSLOT_LIGHTGRID);
STRUCTUREDBUFFER(LightIndexList, uint, SBSLOT_LIGHTINDEXLIST);
STRUCTUREDBUFFER(LightArray, LightArrayType, SBSLOT_LIGHTARRAY);
#define xBaseColorMap texture_0
#define xNormalMap texture_1
#define xRoughnessMap texture_2
@@ -57,12 +52,6 @@ struct GBUFFEROutputType
float4 g3 : SV_TARGET3; // texture_gbuffer3
};
struct LightingResult
{
float3 diffuse;
float3 specular;
};
// METHODS
////////////
@@ -119,63 +108,11 @@ inline void Refraction(in float2 ScreenCoord, in float2 normal2D, in float3 bump
inline void DirectionalLight(in float3 N, in float3 V, in float3 P, in float3 f0, in float3 albedo, in float roughness,
inout float3 diffuse, out float3 specular)
{
float3 L = GetSunDirection();
float3 lightColor = GetSunColor();
BRDF_MAKE(N, L, V);
specular = lightColor * BRDF_SPECULAR(roughness, f0);
diffuse = lightColor * BRDF_DIFFUSE(roughness);
float sh = max(NdotL, 0);
float4 ShPos[3];
ShPos[0] = mul(float4(P, 1), g_xDirLight_ShM[0]);
ShPos[1] = mul(float4(P, 1), g_xDirLight_ShM[1]);
ShPos[2] = mul(float4(P, 1), g_xDirLight_ShM[2]);
float3 ShTex[3];
ShTex[0] = ShPos[0].xyz*float3(1, -1, 1) / ShPos[0].w / 2.0f + 0.5f;
ShTex[1] = ShPos[1].xyz*float3(1, -1, 1) / ShPos[1].w / 2.0f + 0.5f;
ShTex[2] = ShPos[2].xyz*float3(1, -1, 1) / ShPos[2].w / 2.0f + 0.5f;
[branch]if ((saturate(ShTex[2].x) == ShTex[2].x) && (saturate(ShTex[2].y) == ShTex[2].y) && (saturate(ShTex[2].z) == ShTex[2].z))
{
const float shadows[] = {
shadowCascade(ShPos[1],ShTex[1].xy,0.0001f,texture_shadow1),
shadowCascade(ShPos[2],ShTex[2].xy,0.0001f,texture_shadow2)
};
const float2 lerpVal = abs(ShTex[2].xy * 2 - 1);
sh *= lerp(shadows[1], shadows[0], pow(max(lerpVal.x, lerpVal.y), 4));
}
else[branch]if ((saturate(ShTex[1].x) == ShTex[1].x) && (saturate(ShTex[1].y) == ShTex[1].y) && (saturate(ShTex[1].z) == ShTex[1].z))
{
const float shadows[] = {
shadowCascade(ShPos[0],ShTex[0].xy,0.0001f,texture_shadow0),
shadowCascade(ShPos[1],ShTex[1].xy,0.0001f,texture_shadow1),
};
const float2 lerpVal = abs(ShTex[1].xy * 2 - 1);
sh *= lerp(shadows[1], shadows[0], pow(max(lerpVal.x, lerpVal.y), 4));
}
else[branch]if ((saturate(ShTex[0].x) == ShTex[0].x) && (saturate(ShTex[0].y) == ShTex[0].y) && (saturate(ShTex[0].z) == ShTex[0].z))
{
sh *= shadowCascade(ShPos[0], ShTex[0].xy, 0.0001f, texture_shadow0);
}
diffuse *= sh;
specular *= sh;
diffuse = max(diffuse, 0);
specular = max(specular, 0);
LightingResult result = DirectionalLight(LightArray[0], N, V, P, roughness, f0);
diffuse = result.diffuse;
specular = result.specular;
}
inline float offset_lookup1(float2 loc, float2 offset, float scale, float biasedDistance,float slice)
{
return texture_shadowarray_2d.SampleCmpLevelZero(sampler_cmp_depth, float3(loc + offset / scale, slice), biasedDistance).r;
}
inline float shadowCascade1(float4 shadowPos, float2 ShTex, float bias, float slice) {
float realDistance = shadowPos.z / shadowPos.w - bias;
float sum = 0;
float scale = g_xDirLight_mBiasResSoftshadow.y;
float retVal = 1;
retVal *= offset_lookup1(ShTex, float2(0, 0), scale, realDistance,slice);
return retVal;
}
inline void TiledLighting(in float2 pixel, in float3 N, in float3 V, in float3 P, in float3 f0, in float3 albedo, in float roughness,
inout float3 diffuse, out float3 specular)
@@ -184,6 +121,8 @@ inline void TiledLighting(in float2 pixel, in float3 N, in float3 V, in float3 P
uint startOffset = LightGrid[tileIndex].x;
uint lightCount = LightGrid[tileIndex].y;
LightingResult result = (LightingResult)0;
specular = 0;
diffuse = 0;
for (uint i = 0; i < lightCount; i++)
@@ -197,123 +136,22 @@ inline void TiledLighting(in float2 pixel, in float3 N, in float3 V, in float3 P
continue;
L /= lightDistance;
LightingResult result = (LightingResult)0;
float3 lightColor = light.color.rgb * light.energy;
[branch]
switch (light.type)
{
case 0/*DIRECTIONAL*/:
{
L = light.directionWS.xyz;
BRDF_MAKE(N, L, V);
result.specular = lightColor * BRDF_SPECULAR(roughness, f0);
result.diffuse = lightColor * BRDF_DIFFUSE(roughness);
float sh = max(NdotL, 0);
[branch]
if (light.shadowMap_index >= 0)
{
float4 ShPos[3];
ShPos[0] = mul(float4(P, 1), light.shadowMat[0]);
ShPos[1] = mul(float4(P, 1), light.shadowMat[1]);
ShPos[2] = mul(float4(P, 1), light.shadowMat[2]);
float3 ShTex[3];
ShTex[0] = ShPos[0].xyz*float3(1, -1, 1) / ShPos[0].w / 2.0f + 0.5f;
ShTex[1] = ShPos[1].xyz*float3(1, -1, 1) / ShPos[1].w / 2.0f + 0.5f;
ShTex[2] = ShPos[2].xyz*float3(1, -1, 1) / ShPos[2].w / 2.0f + 0.5f;
[branch]if ((saturate(ShTex[2].x) == ShTex[2].x) && (saturate(ShTex[2].y) == ShTex[2].y) && (saturate(ShTex[2].z) == ShTex[2].z))
{
const float shadows[] = {
shadowCascade1(ShPos[1],ShTex[1].xy, light.shadowBias,light.shadowMap_index + 1),
shadowCascade1(ShPos[2],ShTex[2].xy, light.shadowBias,light.shadowMap_index + 2)
};
const float2 lerpVal = abs(ShTex[2].xy * 2 - 1);
sh *= lerp(shadows[1], shadows[0], pow(max(lerpVal.x, lerpVal.y), 4));
}
else[branch]if ((saturate(ShTex[1].x) == ShTex[1].x) && (saturate(ShTex[1].y) == ShTex[1].y) && (saturate(ShTex[1].z) == ShTex[1].z))
{
const float shadows[] = {
shadowCascade1(ShPos[0],ShTex[0].xy, light.shadowBias,light.shadowMap_index + 0),
shadowCascade1(ShPos[1],ShTex[1].xy, light.shadowBias,light.shadowMap_index + 1),
};
const float2 lerpVal = abs(ShTex[1].xy * 2 - 1);
sh *= lerp(shadows[1], shadows[0], pow(max(lerpVal.x, lerpVal.y), 4));
}
else[branch]if ((saturate(ShTex[0].x) == ShTex[0].x) && (saturate(ShTex[0].y) == ShTex[0].y) && (saturate(ShTex[0].z) == ShTex[0].z))
{
sh *= shadowCascade1(ShPos[0], ShTex[0].xy, light.shadowBias, light.shadowMap_index + 0);
}
}
result.diffuse *= sh;
result.specular *= sh;
result.diffuse = max(result.diffuse, 0);
result.specular = max(result.specular, 0);
result = DirectionalLight(light, N, V, P, roughness, f0);
}
break;
case 1/*POINT*/:
{
BRDF_MAKE(N, L, V);
result.specular = lightColor * BRDF_SPECULAR(roughness, f0);
result.diffuse = lightColor * BRDF_DIFFUSE(roughness);
float att = (light.energy * (light.range / (light.range + 1 + lightDistance)));
float attenuation = (att * (light.range - lightDistance) / light.range);
result.diffuse *= attenuation;
result.specular *= attenuation;
float sh = max(NdotL, 0);
[branch]
if (light.shadowMap_index >=0) {
static const float bias = 0.025;
sh *= texture_shadowarray_cube.SampleCmpLevelZero(sampler_cmp_depth, float4(-L,light.shadowMap_index), lightDistance / light.range - bias).r;
}
result.diffuse *= sh;
result.specular *= sh;
result = PointLight(light, L, lightDistance, N, V, P, roughness, f0);
}
break;
case 2/*SPOT*/:
{
float SpotFactor = dot(L, light.directionWS);
float spotCutOff = light.coneAngleCos;
[branch]
if (SpotFactor > spotCutOff)
{
BRDF_MAKE(N, L, V);
result.specular = lightColor * BRDF_SPECULAR(roughness, f0);
result.diffuse = lightColor * BRDF_DIFFUSE(roughness);
float att = (light.energy * (light.range / (light.range + 1 + lightDistance)));
float attenuation = (att * (light.range - lightDistance) / light.range);
attenuation *= saturate((1.0 - (1.0 - SpotFactor) * 1.0 / (1.0 - spotCutOff)));
result.diffuse *= attenuation;
result.specular *= attenuation;
float sh = max(NdotL, 0);
[branch]
if (light.shadowMap_index >= 0)
{
float4 ShPos = mul(float4(P, 1), light.shadowMat[0]);
float2 ShTex = ShPos.xy / ShPos.w * float2(0.5f, -0.5f) + float2(0.5f, 0.5f);
[branch]
if ((saturate(ShTex.x) == ShTex.x) && (saturate(ShTex.y) == ShTex.y))
{
sh *= shadowCascade1(ShPos, ShTex.xy, light.shadowBias, light.shadowMap_index);
}
}
result.diffuse *= sh;
result.specular *= sh;
result.diffuse = max(result.diffuse, 0);
result.specular = max(result.specular, 0);
}
result = SpotLight(light, L, lightDistance, N, V, P, roughness, f0);
}
break;
}
+2 -4
View File
@@ -1,12 +1,10 @@
#include "lightHF.hlsli"
#include "pointLightHF.hlsli"
#include "deferredLightHF.hlsli"
LightOutputType main(VertexToPixel PSIn)
{
DEFERREDLIGHT_MAKEPARAMS
DEFERRED_POINTLIGHT_MAIN
DEFERREDLIGHT_POINT
DEFERREDLIGHT_RETURN
}
+3 -5
View File
@@ -1,14 +1,12 @@
#include "lightHF.hlsli"
#include "deferredLightHF.hlsli"
#include "icosphere.hlsli"
#include "pointLightHF.hlsli"
VertexToPixel main(uint vid : SV_VERTEXID)
{
VertexToPixel Out = (VertexToPixel)0;
float4 pos = float4(ICOSPHERE[vid],1);
pos.xyz *= xLightEnerDis.y+1;
pos.xyz += xLightPos;
Out.pos = Out.pos2D = mul(pos,g_xCamera_VP);
Out.pos = Out.pos2D = mul(mul(pos,g_xTransform),g_xCamera_VP);
Out.lightIndex = g_xMisc_int4[0];
return Out;
}
+2 -4
View File
@@ -1,12 +1,10 @@
#include "lightHF.hlsli"
#include "spotLightHF.hlsli"
#include "deferredLightHF.hlsli"
LightOutputType main(VertexToPixel PSIn)
{
DEFERREDLIGHT_MAKEPARAMS
DEFERRED_SPOTLIGHT_MAIN
DEFERREDLIGHT_SPOT
DEFERREDLIGHT_RETURN
}
+3 -3
View File
@@ -1,6 +1,5 @@
#include "lightHF.hlsli"
#include "deferredLightHF.hlsli"
#include "cone.hlsli"
#include "spotLightHF.hlsli"
VertexToPixel main(uint vid : SV_VERTEXID)
@@ -8,7 +7,8 @@ VertexToPixel main(uint vid : SV_VERTEXID)
VertexToPixel Out = (VertexToPixel)0;
float4 pos = float4(CONE[vid],1);
pos = mul( pos,xLightWorld );
pos = mul( pos, g_xTransform );
Out.pos = Out.pos2D = mul(pos,g_xCamera_VP);
Out.lightIndex = g_xMisc_int4[0];
return Out;
}
-28
View File
@@ -1,28 +0,0 @@
#ifndef _TILEDLIGHTING_HF_
#define _TILEDLIGHTING_HF_
struct LightArrayType
{
float3 positionVS; // View Space!
float range;
// --
float4 color;
// --
float3 positionWS;
float energy;
// --
float3 directionVS;
float _pad0;
// --
float3 directionWS;
uint type;
// --
float shadowBias;
int shadowMap_index;
float coneAngle;
float coneAngleCos;
// --
float4x4 shadowMat[3];
};
#endif // _TILEDLIGHTING_HF_
+1
View File
@@ -4042,6 +4042,7 @@ Light::Light():Transform() {
lensFlareRimTextures.resize(0);
lensFlareNames.resize(0);
shadowMap_index = -1;
lightArray_index = 0;
shadowBias = 0.0001f;
}
Light::~Light() {
+1
View File
@@ -778,6 +778,7 @@ struct Light : public Cullable , public Transform
static wiGraphicsTypes::Texture2D* shadowMapArray_2D;
static wiGraphicsTypes::Texture2D* shadowMapArray_Cube;
int shadowMap_index;
int lightArray_index;
vector<SHCAM> shadowCam_pointLight;
vector<SHCAM> shadowCam_dirLight;
+144 -139
View File
@@ -22,6 +22,7 @@
#include "ResourceMapping.h"
#include "wiGraphicsDevice_DX11.h"
#include "wiTranslator.h"
#include "lightCullingCSInterop.h"
using namespace wiGraphicsTypes;
@@ -561,9 +562,6 @@ void wiRenderer::LoadBuffers()
bd.ByteWidth = sizeof(MaterialCB);
GetDevice()->CreateBuffer(&bd, nullptr, constantBuffers[CBTYPE_MATERIAL]);
bd.ByteWidth = sizeof(DirectionalLightCB);
GetDevice()->CreateBuffer(&bd, nullptr, constantBuffers[CBTYPE_DIRLIGHT]);
bd.ByteWidth = sizeof(MiscCB);
GetDevice()->CreateBuffer(&bd, nullptr, constantBuffers[CBTYPE_MISC]);
@@ -575,11 +573,6 @@ void wiRenderer::LoadBuffers()
// On demand buffers...
bd.ByteWidth = sizeof(PointLightCB);
GetDevice()->CreateBuffer(&bd, nullptr, constantBuffers[CBTYPE_POINTLIGHT]);
bd.ByteWidth = sizeof(SpotLightCB);
GetDevice()->CreateBuffer(&bd, nullptr, constantBuffers[CBTYPE_SPOTLIGHT]);
bd.ByteWidth = sizeof(VolumeLightCB);
GetDevice()->CreateBuffer(&bd, nullptr, constantBuffers[CBTYPE_VOLUMELIGHT]);
@@ -616,7 +609,7 @@ void wiRenderer::LoadBuffers()
bd.StructureByteStride = sizeof(ShaderBoneType);
GetDevice()->CreateBuffer(&bd, nullptr, resourceBuffers[RBTYPE_BONE]);
bd.ByteWidth = sizeof(LightArrayType) * 1024;
bd.ByteWidth = sizeof(LightArrayType) * MAX_LIGHTS;
bd.BindFlags = BIND_SHADER_RESOURCE;
bd.MiscFlags = RESOURCE_MISC_BUFFER_STRUCTURED;
bd.StructureByteStride = sizeof(LightArrayType);
@@ -1214,9 +1207,6 @@ void wiRenderer::BindPersistentState(GRAPHICSTHREAD threadID)
GetDevice()->BindConstantBufferPS(constantBuffers[CBTYPE_MATERIAL], CB_GETBINDSLOT(MaterialCB), threadID);
GetDevice()->BindConstantBufferPS(constantBuffers[CBTYPE_DIRLIGHT], CB_GETBINDSLOT(DirectionalLightCB), threadID);
GetDevice()->BindConstantBufferVS(constantBuffers[CBTYPE_DIRLIGHT], CB_GETBINDSLOT(DirectionalLightCB), threadID);
GetDevice()->BindConstantBufferVS(constantBuffers[CBTYPE_MISC], CB_GETBINDSLOT(MiscCB), threadID);
GetDevice()->BindConstantBufferPS(constantBuffers[CBTYPE_MISC], CB_GETBINDSLOT(MiscCB), threadID);
GetDevice()->BindConstantBufferGS(constantBuffers[CBTYPE_MISC], CB_GETBINDSLOT(MiscCB), threadID);
@@ -1454,6 +1444,12 @@ void wiRenderer::UpdatePerFrameData()
Frustum frustum;
frustum.ConstructFrustum(min(camera->zFarP, GetScene().worldInfo.fogSEH.y), camera->Projection, camera->View);
wiSPTree::getVisible(spTree_lights->root, frustum, culling.culledLights);
UINT i = 0;
for (auto& l : culling.culledLights)
{
((Light*)l)->lightArray_index = i;
i++;
}
}
}
}
@@ -1591,6 +1587,68 @@ void wiRenderer::UpdateRenderData(GRAPHICSTHREAD threadID)
GetDevice()->BindResourcePS(envMaps[1], TEXSLOT_ENV1, threadID);
}
// Fill Light Array with lights in the frustum
{
const FrameCulling& culling = frameCullings[getCamera()];
const CulledList& culledLights = culling.culledLights;
static LightArrayType* lightArray = (LightArrayType*)_mm_malloc(sizeof(LightArrayType)*MAX_LIGHTS, 16);
ZeroMemory(lightArray, sizeof(lightArray));
XMMATRIX viewMatrix = cam->GetView();
UINT lightCounter = 0;
for (Cullable* c : culledLights)
{
Light* l = (Light*)c;
lightArray[lightCounter].posWS = l->translation;
XMStoreFloat3(&lightArray[lightCounter].posVS, XMVector3TransformCoord(XMLoadFloat3(&lightArray[lightCounter].posWS), viewMatrix));
lightArray[lightCounter].distance = l->enerDis.y;
lightArray[lightCounter].col = l->color;
lightArray[lightCounter].energy = l->enerDis.x;
lightArray[lightCounter].type = l->type;
lightArray[lightCounter].shadowBias = l->shadowBias;
lightArray[lightCounter].shadowMap_index = l->shadowMap_index;
switch (l->type)
{
case Light::DIRECTIONAL:
{
lightArray[lightCounter].directionWS = l->GetDirection();
for (unsigned int shmap = 0; shmap < min(l->shadowCam_dirLight.size(), ARRAYSIZE(lightArray[lightCounter].shadowMatrix)); ++shmap) {
lightArray[lightCounter].shadowMatrix[shmap] = l->shadowCam_dirLight[shmap].getVP();
}
}
break;
case Light::SPOT:
{
lightArray[lightCounter].coneAngle = (l->enerDis.z * 0.5f);
lightArray[lightCounter].coneAngleCos = cosf(lightArray[lightCounter].coneAngle);
lightArray[lightCounter].directionWS = l->GetDirection();
XMStoreFloat3(&lightArray[lightCounter].directionVS, XMVector3TransformNormal(XMLoadFloat3(&lightArray[lightCounter].directionWS), viewMatrix));
if (l->shadow && l->shadowMap_index >= 0)
{
lightArray[lightCounter].shadowMatrix[0] = l->shadowCam_spotLight[0].getVP();
}
}
break;
default:
break;
}
lightCounter++;
if (lightCounter == MAX_LIGHTS)
{
assert(0 && "Maximum Lightcount exceeded for a single tiled lightculling pass! Please redefine MAX_LIGHTS to fit!");
lightCounter--;
break;
}
}
GetDevice()->UpdateBuffer(resourceBuffers[RBTYPE_LIGHTARRAY], lightArray, threadID, (int)(sizeof(LightArrayType)*lightCounter));
GetDevice()->BindResourcePS(resourceBuffers[RBTYPE_LIGHTARRAY], STRUCTUREDBUFFER_GETBINDSLOT(LightArrayType), threadID);
GetDevice()->BindResourceCS(resourceBuffers[RBTYPE_LIGHTARRAY], STRUCTUREDBUFFER_GETBINDSLOT(LightArrayType), threadID);
}
}
void wiRenderer::UpdateImages(){
@@ -2170,12 +2228,6 @@ void wiRenderer::DrawLights(Camera* camera, GRAPHICSTHREAD threadID)
GetDevice()->Draw(3, threadID);
}
GetDevice()->BindConstantBufferPS(constantBuffers[CBTYPE_POINTLIGHT], CB_GETBINDSLOT(PointLightCB), threadID);
GetDevice()->BindConstantBufferVS(constantBuffers[CBTYPE_POINTLIGHT], CB_GETBINDSLOT(PointLightCB), threadID);
GetDevice()->BindConstantBufferPS(constantBuffers[CBTYPE_SPOTLIGHT], CB_GETBINDSLOT(SpotLightCB), threadID);
GetDevice()->BindConstantBufferVS(constantBuffers[CBTYPE_SPOTLIGHT], CB_GETBINDSLOT(SpotLightCB), threadID);
for(int type=0;type<3;++type){
@@ -2214,67 +2266,88 @@ void wiRenderer::DrawLights(Camera* camera, GRAPHICSTHREAD threadID)
if(type==0) //dir
{
DirectionalLightCB lcb;
lcb.direction=XMVector3Normalize(
-XMVector3Transform( XMVectorSet(0,-1,0,1), XMMatrixRotationQuaternion( XMLoadFloat4(&l->rotation) ) )
);
lcb.col=XMFLOAT4(l->color.x*l->enerDis.x,l->color.y*l->enerDis.x,l->color.z*l->enerDis.x,1);
lcb.mBiasResSoftshadow=XMFLOAT4(l->shadowBias,(float)SHADOWRES_2D,(float)SOFTSHADOWQUALITY_2D,0);
//for (unsigned int shmap = 0; shmap < l->shadowMaps_dirLight.size(); ++shmap){
// lcb.mShM[shmap]=l->shadowCam_dirLight[shmap].getVP();
// if(l->shadowMaps_dirLight[shmap].depth)
// GetDevice()->BindResourcePS(l->shadowMaps_dirLight[shmap].depth->GetTexture(),TEXSLOT_SHADOW0+shmap,threadID);
//}
GetDevice()->UpdateBuffer(constantBuffers[CBTYPE_DIRLIGHT],&lcb,threadID);
//DirectionalLightCB lcb;
//lcb.direction=XMVector3Normalize(
// -XMVector3Transform( XMVectorSet(0,-1,0,1), XMMatrixRotationQuaternion( XMLoadFloat4(&l->rotation) ) )
// );
//lcb.col=XMFLOAT4(l->color.x*l->enerDis.x,l->color.y*l->enerDis.x,l->color.z*l->enerDis.x,1);
//lcb.mBiasResSoftshadow=XMFLOAT4(l->shadowBias,(float)SHADOWRES_2D,(float)SOFTSHADOWQUALITY_2D,0);
////for (unsigned int shmap = 0; shmap < l->shadowMaps_dirLight.size(); ++shmap){
//// lcb.mShM[shmap]=l->shadowCam_dirLight[shmap].getVP();
//// if(l->shadowMaps_dirLight[shmap].depth)
//// GetDevice()->BindResourcePS(l->shadowMaps_dirLight[shmap].depth->GetTexture(),TEXSLOT_SHADOW0+shmap,threadID);
////}
//GetDevice()->UpdateBuffer(constantBuffers[CBTYPE_DIRLIGHT],&lcb,threadID);
MiscCB miscCb;
miscCb.mInt[0] = l->lightArray_index;
GetDevice()->UpdateBuffer(constantBuffers[CBTYPE_MISC], &miscCb, threadID);
GetDevice()->Draw(3, threadID);
}
else if(type==1) //point
{
PointLightCB lcb;
lcb.pos=l->translation;
lcb.col=l->color;
lcb.enerdis=l->enerDis;
lcb.enerdis.w = 0.f;
//PointLightCB lcb;
//lcb.pos=l->translation;
//lcb.col=l->color;
//lcb.enerdis=l->enerDis;
//lcb.enerdis.w = 0.f;
//if (l->shadow && l->shadowMap_index>=0)
//{
// lcb.enerdis.w = 1.f;
// if(Light::shadowMaps_pointLight[l->shadowMap_index].depth)
// GetDevice()->BindResourcePS(Light::shadowMaps_pointLight[l->shadowMap_index].depth->GetTexture(), TEXSLOT_SHADOW_CUBE, threadID);
//}
GetDevice()->UpdateBuffer(constantBuffers[CBTYPE_POINTLIGHT], &lcb, threadID);
////if (l->shadow && l->shadowMap_index>=0)
////{
//// lcb.enerdis.w = 1.f;
//// if(Light::shadowMaps_pointLight[l->shadowMap_index].depth)
//// GetDevice()->BindResourcePS(Light::shadowMaps_pointLight[l->shadowMap_index].depth->GetTexture(), TEXSLOT_SHADOW_CUBE, threadID);
////}
//GetDevice()->UpdateBuffer(constantBuffers[CBTYPE_POINTLIGHT], &lcb, threadID);
MiscCB miscCb;
miscCb.mInt[0] = l->lightArray_index;
float sca = l->enerDis.y + 1;
miscCb.mTransform = XMMatrixTranspose(XMMatrixScaling(sca,sca,sca)*XMMatrixTranslation(l->translation.x, l->translation.y, l->translation.z));
GetDevice()->UpdateBuffer(constantBuffers[CBTYPE_MISC], &miscCb, threadID);
GetDevice()->Draw(240, threadID);
}
else if(type==2) //spot
{
SpotLightCB lcb;
const float coneS = (const float)(l->enerDis.z / XM_PIDIV4);
XMMATRIX world,rot;
world = XMMatrixTranspose(
XMMatrixScaling(coneS*l->enerDis.y,l->enerDis.y,coneS*l->enerDis.y)*
XMMatrixRotationQuaternion( XMLoadFloat4( &l->rotation ) )*
XMMatrixTranslationFromVector( XMLoadFloat3(&l->translation) )
);
rot=XMMatrixRotationQuaternion( XMLoadFloat4(&l->rotation) );
lcb.direction=XMVector3Normalize(
-XMVector3Transform( XMVectorSet(0,-1,0,1), rot )
);
lcb.world=world;
lcb.mBiasResSoftshadow=XMFLOAT4(l->shadowBias,(float)SHADOWRES_2D,(float)SOFTSHADOWQUALITY_2D,0);
lcb.mShM = XMMatrixIdentity();
lcb.col=l->color;
lcb.enerdis=l->enerDis;
lcb.enerdis.z=(float)cos(l->enerDis.z/2.0);
//SpotLightCB lcb;
//const float coneS = (const float)(l->enerDis.z / XM_PIDIV4);
//XMMATRIX world,rot;
//world = XMMatrixTranspose(
// XMMatrixScaling(coneS*l->enerDis.y,l->enerDis.y,coneS*l->enerDis.y)*
// XMMatrixRotationQuaternion( XMLoadFloat4( &l->rotation ) )*
// XMMatrixTranslationFromVector( XMLoadFloat3(&l->translation) )
// );
//rot=XMMatrixRotationQuaternion( XMLoadFloat4(&l->rotation) );
//lcb.direction=XMVector3Normalize(
// -XMVector3Transform( XMVectorSet(0,-1,0,1), rot )
// );
//lcb.world=world;
//lcb.mBiasResSoftshadow=XMFLOAT4(l->shadowBias,(float)SHADOWRES_2D,(float)SOFTSHADOWQUALITY_2D,0);
//lcb.mShM = XMMatrixIdentity();
//lcb.col=l->color;
//lcb.enerdis=l->enerDis;
//lcb.enerdis.z=(float)cos(l->enerDis.z/2.0);
//if (l->shadow && l->shadowMap_index>=0)
//{
// lcb.mShM = l->shadowCam_spotLight[0].getVP();
// if(Light::shadowMaps_spotLight[l->shadowMap_index].depth)
// GetDevice()->BindResourcePS(Light::shadowMaps_spotLight[l->shadowMap_index].depth->GetTexture(), TEXSLOT_SHADOW0, threadID);
//}
GetDevice()->UpdateBuffer(constantBuffers[CBTYPE_SPOTLIGHT], &lcb, threadID);
////if (l->shadow && l->shadowMap_index>=0)
////{
//// lcb.mShM = l->shadowCam_spotLight[0].getVP();
//// if(Light::shadowMaps_spotLight[l->shadowMap_index].depth)
//// GetDevice()->BindResourcePS(Light::shadowMaps_spotLight[l->shadowMap_index].depth->GetTexture(), TEXSLOT_SHADOW0, threadID);
////}
//GetDevice()->UpdateBuffer(constantBuffers[CBTYPE_SPOTLIGHT], &lcb, threadID);
MiscCB miscCb;
miscCb.mInt[0] = l->lightArray_index;
const float coneS = (const float)(l->enerDis.z / XM_PIDIV4);
miscCb.mTransform = XMMatrixTranspose(
XMMatrixScaling(coneS*l->enerDis.y, l->enerDis.y, coneS*l->enerDis.y)*
XMMatrixRotationQuaternion(XMLoadFloat4(&l->rotation))*
XMMatrixTranslationFromVector(XMLoadFloat3(&l->translation))
);
GetDevice()->UpdateBuffer(constantBuffers[CBTYPE_MISC], &miscCb, threadID);
GetDevice()->Draw(192, threadID);
}
@@ -2741,7 +2814,6 @@ void wiRenderer::DrawForShadowMap(GRAPHICSTHREAD threadID)
GetDevice()->BindVS(vertexShaders[VSTYPE_SHADOWCUBEMAPRENDER], threadID);
GetDevice()->BindGS(geometryShaders[GSTYPE_SHADOWCUBEMAPRENDER], threadID);
GetDevice()->BindConstantBufferPS(constantBuffers[CBTYPE_POINTLIGHT], CB_GETBINDSLOT(PointLightCB), threadID);
GetDevice()->BindConstantBufferGS(constantBuffers[CBTYPE_CUBEMAPRENDER], CB_GETBINDSLOT(CubeMapRenderCB), threadID);
//int i = 0;
@@ -2762,10 +2834,9 @@ void wiRenderer::DrawForShadowMap(GRAPHICSTHREAD threadID)
GetDevice()->BindViewports(1, &vp, threadID);
GetDevice()->BindRenderTargets(0, nullptr, Light::shadowMapArray_Cube, threadID, l->shadowMap_index);
PointLightCB lcb;
lcb.enerdis = l->enerDis;
lcb.pos = l->translation;
GetDevice()->UpdateBuffer(constantBuffers[CBTYPE_POINTLIGHT], &lcb, threadID);
MiscCB miscCb;
miscCb.mColor = XMFLOAT4(l->translation.x, l->translation.y, l->translation.z, l->enerDis.y);
GetDevice()->UpdateBuffer(constantBuffers[CBTYPE_MISC], &miscCb, threadID);
CubeMapRenderCB cb;
for (unsigned int shcam = 0; shcam < l->shadowCam_pointLight.size(); ++shcam)
@@ -3058,7 +3129,6 @@ void wiRenderer::DrawWorld(Camera* camera, bool tessellation, GRAPHICSTHREAD thr
if (shaderType == SHADERTYPE_TILEDFORWARD)
{
GetDevice()->BindResourcePS(resourceBuffers[RBTYPE_LIGHTARRAY], SBSLOT_LIGHTARRAY, threadID);
GetDevice()->BindResourcePS(resourceBuffers[RBTYPE_LIGHTINDEXLIST_OPAQUE], SBSLOT_LIGHTINDEXLIST, threadID);
GetDevice()->BindResourcePS(textures[TEXTYPE_2D_LIGHTGRID_OPAQUE], TEXSLOT_LIGHTGRID, threadID);
@@ -3323,7 +3393,6 @@ void wiRenderer::DrawWorldTransparent(Camera* camera, SHADERTYPE shaderType, Tex
if (shaderType == SHADERTYPE_TILEDFORWARD)
{
GetDevice()->BindResourcePS(resourceBuffers[RBTYPE_LIGHTARRAY], SBSLOT_LIGHTARRAY, threadID);
GetDevice()->BindResourcePS(resourceBuffers[RBTYPE_LIGHTINDEXLIST_TRANSPARENT], SBSLOT_LIGHTINDEXLIST, threadID);
GetDevice()->BindResourcePS(textures[TEXTYPE_2D_LIGHTGRID_TRANSPARENT], TEXSLOT_LIGHTGRID, threadID);
}
@@ -3598,7 +3667,6 @@ void wiRenderer::DrawDecals(Camera* camera, GRAPHICSTHREAD threadID)
void wiRenderer::ComputeTiledLightCulling(GRAPHICSTHREAD threadID)
{
#include "lightCullingCSInterop.h"
GraphicsDevice* device = wiRenderer::GetDevice();
@@ -3626,72 +3694,11 @@ void wiRenderer::ComputeTiledLightCulling(GRAPHICSTHREAD threadID)
dispatchParams.numThreadGroups[0] = (UINT)ceilf(dispatchParams.numThreads[0] / (float)BLOCK_SIZE);
dispatchParams.numThreadGroups[1] = (UINT)ceilf(dispatchParams.numThreads[1] / (float)BLOCK_SIZE);
dispatchParams.numThreadGroups[2] = 1;
// Fill Light Array with lights in the frustum
const FrameCulling& culling = frameCullings[getCamera()];
const CulledList& culledLights = culling.culledLights;
static LightArrayType* lightArray = (LightArrayType*)_mm_malloc(sizeof(LightArrayType)*MAX_LIGHTS, 16);
ZeroMemory(lightArray, sizeof(lightArray));
XMMATRIX viewMatrix = cam->GetView();
UINT lightCounter = 0;
for (Cullable* c : culledLights)
{
Light* l = (Light*)c;
lightArray[lightCounter].posWS = l->translation;
XMStoreFloat3(&lightArray[lightCounter].posVS, XMVector3TransformCoord(XMLoadFloat3(&lightArray[lightCounter].posWS), viewMatrix));
lightArray[lightCounter].distance = l->enerDis.y;
lightArray[lightCounter].col = l->color;
lightArray[lightCounter].energy = l->enerDis.x;
lightArray[lightCounter].type = l->type;
lightArray[lightCounter].shadowBias = l->shadowBias;
lightArray[lightCounter].shadowMap_index = l->shadowMap_index;
switch (l->type)
{
case Light::DIRECTIONAL:
{
lightArray[lightCounter].directionWS = l->GetDirection();
for (unsigned int shmap = 0; shmap < min(l->shadowCam_dirLight.size(),ARRAYSIZE(lightArray[lightCounter].shadowMatrix)); ++shmap) {
lightArray[lightCounter].shadowMatrix[shmap] = l->shadowCam_dirLight[shmap].getVP();
}
}
break;
case Light::SPOT:
{
lightArray[lightCounter].coneAngle = (l->enerDis.z * 0.5f);
lightArray[lightCounter].coneAngleCos = cosf(lightArray[lightCounter].coneAngle);
lightArray[lightCounter].directionWS = l->GetDirection();
XMStoreFloat3(&lightArray[lightCounter].directionVS, XMVector3TransformNormal(XMLoadFloat3(&lightArray[lightCounter].directionWS), viewMatrix));
if (l->shadow && l->shadowMap_index >= 0)
{
lightArray[lightCounter].shadowMatrix[0] = l->shadowCam_spotLight[0].getVP();
}
}
break;
default:
break;
}
lightCounter++;
if (lightCounter == MAX_LIGHTS)
{
assert(0 && "Maximum Lightcount exceeded for a single tiled lightculling pass! Please redefine MAX_LIGHTS to fit!");
lightCounter--;
break;
}
}
device->UpdateBuffer(resourceBuffers[RBTYPE_LIGHTARRAY], lightArray, threadID, (int)(sizeof(LightArrayType)*lightCounter));
dispatchParams.value0 = lightCounter;
dispatchParams.value0 = (UINT)frameCullings[getCamera()].culledLights.size();
device->UpdateBuffer(constantBuffers[CBTYPE_DISPATCHPARAMS], &dispatchParams, threadID);
device->BindConstantBufferCS(constantBuffers[CBTYPE_DISPATCHPARAMS], CB_GETBINDSLOT(DispatchParamsCB), threadID);
}
static GPUBuffer* frustumBuffer = nullptr;
if (frustumBuffer == nullptr || _resolutionChanged)
{
@@ -3807,8 +3814,6 @@ void wiRenderer::ComputeTiledLightCulling(GRAPHICSTHREAD threadID)
device->EventBegin(L"Light Culling", threadID);
device->UnBindResources(TEXSLOT_LIGHTGRID, SBSLOT_LIGHTINDEXLIST - TEXSLOT_LIGHTGRID + 1, threadID);
device->BindResourceCS(resourceBuffers[RBTYPE_LIGHTARRAY], STRUCTUREDBUFFER_GETBINDSLOT(LightArrayType), threadID);
device->BindResourceCS(frustumBuffer, SBSLOT_TILEFRUSTUMS, threadID);
+1 -34
View File
@@ -160,21 +160,11 @@ public:
ALIGN_16
};
GFX_STRUCT DirectionalLightCB
{
XMVECTOR direction;
XMFLOAT4 col;
XMFLOAT4 mBiasResSoftshadow;
XMMATRIX mShM[3];
CB_SETBINDSLOT(CBSLOT_RENDERER_DIRLIGHT)
ALIGN_16
};
GFX_STRUCT MiscCB
{
XMMATRIX mTransform;
XMFLOAT4 mColor;
int mInt[4];
CB_SETBINDSLOT(CBSLOT_RENDERER_MISC)
@@ -190,29 +180,6 @@ public:
};
// On demand buffers:
GFX_STRUCT PointLightCB
{
XMFLOAT3 pos; float pad;
XMFLOAT4 col;
XMFLOAT4 enerdis;
CB_SETBINDSLOT(CBSLOT_RENDERER_POINTLIGHT)
ALIGN_16
};
GFX_STRUCT SpotLightCB
{
XMMATRIX world;
XMVECTOR direction;
XMFLOAT4 col;
XMFLOAT4 enerdis;
XMFLOAT4 mBiasResSoftshadow;
XMMATRIX mShM;
CB_SETBINDSLOT(CBSLOT_RENDERER_SPOTLIGHT)
ALIGN_16
};
GFX_STRUCT VolumeLightCB
{
XMMATRIX world;