-
Notifications
You must be signed in to change notification settings - Fork 80
/
HDR_PS.hlsl
186 lines (151 loc) · 5.56 KB
/
HDR_PS.hlsl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
struct PixelShaderInput
{
float4 PositionVS : POSITION;
float3 NormalVS : NORMAL;
float2 TexCoord : TEXCOORD;
};
struct Material
{
float4 Emissive;
//----------------------------------- (16 byte boundary)
float4 Ambient;
//----------------------------------- (16 byte boundary)
float4 Diffuse;
//----------------------------------- (16 byte boundary)
float4 Specular;
//----------------------------------- (16 byte boundary)
float SpecularPower;
float3 Padding;
//----------------------------------- (16 byte boundary)
// Total: 16 * 5 = 80 bytes
};
struct PointLight
{
float4 PositionWS; // Light position in world space.
//----------------------------------- (16 byte boundary)
float4 PositionVS; // Light position in view space.
//----------------------------------- (16 byte boundary)
float4 Color;
//----------------------------------- (16 byte boundary)
float Intensity;
float Attenuation;
float2 Padding; // Pad to 16 bytes
//----------------------------------- (16 byte boundary)
// Total: 16 * 4 = 64 bytes
};
struct SpotLight
{
float4 PositionWS; // Light position in world space.
//----------------------------------- (16 byte boundary)
float4 PositionVS; // Light position in view space.
//----------------------------------- (16 byte boundary)
float4 DirectionWS; // Light direction in world space.
//----------------------------------- (16 byte boundary)
float4 DirectionVS; // Light direction in view space.
//----------------------------------- (16 byte boundary)
float4 Color;
//----------------------------------- (16 byte boundary)
float Intensity;
float SpotAngle;
float Attenuation;
float Padding; // Pad to 16 bytes.
//----------------------------------- (16 byte boundary)
// Total: 16 * 6 = 96 bytes
};
struct LightProperties
{
uint NumPointLights;
uint NumSpotLights;
};
struct LightResult
{
float4 Diffuse;
float4 Specular;
};
ConstantBuffer<Material> MaterialCB : register( b0, space1 );
ConstantBuffer<LightProperties> LightPropertiesCB : register( b1 );
StructuredBuffer<PointLight> PointLights : register( t0 );
StructuredBuffer<SpotLight> SpotLights : register( t1 );
Texture2D DiffuseTexture : register( t2 );
SamplerState LinearRepeatSampler : register(s0);
float3 LinearToSRGB( float3 x )
{
// This is exactly the sRGB curve
//return x < 0.0031308 ? 12.92 * x : 1.055 * pow(abs(x), 1.0 / 2.4) - 0.055;
// This is cheaper but nearly equivalent
return x < 0.0031308 ? 12.92 * x : 1.13005 * sqrt( abs( x - 0.00228 ) ) - 0.13448 * x + 0.005719;
}
float DoDiffuse( float3 N, float3 L )
{
return max( 0, dot( N, L ) );
}
float DoSpecular( float3 V, float3 N, float3 L )
{
float3 R = normalize( reflect( -L, N ) );
float RdotV = max( 0, dot( R, V ) );
return pow( RdotV, MaterialCB.SpecularPower );
}
float DoAttenuation( float attenuation, float distance )
{
return 1.0f / ( 1.0f + attenuation * distance * distance );
}
float DoSpotCone( float3 spotDir, float3 L, float spotAngle )
{
float minCos = cos( spotAngle );
float maxCos = ( minCos + 1.0f ) / 2.0f;
float cosAngle = dot( spotDir, -L );
return smoothstep( minCos, maxCos, cosAngle );
}
LightResult DoPointLight( PointLight light, float3 V, float3 P, float3 N )
{
LightResult result;
float3 L = ( light.PositionVS.xyz - P );
float d = length( L );
L = L / d;
float attenuation = DoAttenuation( light.Attenuation, d );
result.Diffuse = DoDiffuse( N, L ) * attenuation * light.Color * light.Intensity;
result.Specular = DoSpecular( V, N, L ) * attenuation * light.Color * light.Intensity;
return result;
}
LightResult DoSpotLight( SpotLight light, float3 V, float3 P, float3 N )
{
LightResult result;
float3 L = ( light.PositionVS.xyz - P );
float d = length( L );
L = L / d;
float attenuation = DoAttenuation( light.Attenuation, d );
float spotIntensity = DoSpotCone( light.DirectionVS.xyz, L, light.SpotAngle );
result.Diffuse = DoDiffuse( N, L ) * attenuation * spotIntensity * light.Color * light.Intensity;
result.Specular = DoSpecular( V, N, L ) * attenuation * spotIntensity * light.Color * light.Intensity;
return result;
}
LightResult DoLighting( float3 P, float3 N )
{
uint i;
// Lighting is performed in view space.
float3 V = normalize( -P );
LightResult totalResult = (LightResult)0;
for ( i = 0; i < LightPropertiesCB.NumPointLights; ++i )
{
LightResult result = DoPointLight( PointLights[i], V, P, N );
totalResult.Diffuse += result.Diffuse;
totalResult.Specular += result.Specular;
}
for ( i = 0; i < LightPropertiesCB.NumSpotLights; ++i )
{
LightResult result = DoSpotLight( SpotLights[i], V, P, N );
totalResult.Diffuse += result.Diffuse;
totalResult.Specular += result.Specular;
}
return totalResult;
}
float4 main( PixelShaderInput IN ) : SV_Target
{
LightResult lit = DoLighting( IN.PositionVS.xyz, normalize( IN.NormalVS ) );
float4 emissive = MaterialCB.Emissive;
float4 ambient = MaterialCB.Ambient;
float4 diffuse = MaterialCB.Diffuse * lit.Diffuse;
float4 specular = MaterialCB.Specular * lit.Specular;
float4 texColor = DiffuseTexture.Sample( LinearRepeatSampler, IN.TexCoord );
return ( emissive + ambient + diffuse + specular ) * texColor;
}