aboutsummaryrefslogtreecommitdiff
path: root/src/shaders/RenderVolume_DS.hlsl
blob: 880e9ed9e3d7c9c43ff47ca1e9d9259b5f3a3c1f (plain) (blame)
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
// This code contains NVIDIA Confidential Information and is disclosed 
// under the Mutual Non-Disclosure Agreement. 
// 
// Notice 
// ALL NVIDIA DESIGN SPECIFICATIONS AND CODE ("MATERIALS") ARE PROVIDED "AS IS" NVIDIA MAKES 
// NO REPRESENTATIONS, WARRANTIES, EXPRESSED, IMPLIED, STATUTORY, OR OTHERWISE WITH RESPECT TO 
// THE MATERIALS, AND EXPRESSLY DISCLAIMS ANY IMPLIED WARRANTIES OF NONINFRINGEMENT, 
// MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. 
// 
// NVIDIA Corporation assumes no responsibility for the consequences of use of such 
// information or for any infringement of patents or other rights of third parties that may 
// result from its use. No license is granted by implication or otherwise under any patent 
// or patent rights of NVIDIA Corporation. No third party distribution is allowed unless 
// expressly authorized by NVIDIA.  Details are subject to change without notice. 
// This code supersedes and replaces all information previously supplied. 
// NVIDIA Corporation products are not authorized for use as critical 
// components in life support devices or systems without express written approval of 
// NVIDIA Corporation. 
// 
// Copyright (c) 2003 - 2016 NVIDIA Corporation. All rights reserved.
//
// NVIDIA Corporation and its licensors retain all intellectual property and proprietary
// rights in and to this software and related documentation and any modifications thereto.
// Any use, reproduction, disclosure or distribution of this software and related
// documentation without an express license agreement from NVIDIA Corporation is
// strictly prohibited.
//

/*
Define the shader permutations for code generation
%% MUX_BEGIN %%

- SHADOWMAPTYPE:
    - SHADOWMAPTYPE_ATLAS
    - SHADOWMAPTYPE_ARRAY

- CASCADECOUNT:
    - CASCADECOUNT_1: 1
    - CASCADECOUNT_2: 2
    - CASCADECOUNT_3: 3
    - CASCADECOUNT_4: 4

- VOLUMETYPE:
	- VOLUMETYPE_FRUSTUM
	- VOLUMETYPE_PARABOLOID

%% MUX_END %%
*/

#include "ShaderCommon.h"

#define COARSE_CASCADE (CASCADECOUNT-1)

#if (SHADOWMAPTYPE == SHADOWMAPTYPE_ATLAS)
Texture2D<float> tShadowMap : register(t1);
#elif (SHADOWMAPTYPE == SHADOWMAPTYPE_ARRAY)
Texture2DArray<float> tShadowMap : register(t1);
#endif

float SampleShadowMap(float2 tex_coord, int cascade)
{
	float depth_value = 1.0f;
	float2 lookup_coord = g_vElementOffsetAndScale[cascade].zw * tex_coord + g_vElementOffsetAndScale[cascade].xy;
#if (SHADOWMAPTYPE == SHADOWMAPTYPE_ATLAS)
	depth_value = tShadowMap.SampleLevel( sBilinear, lookup_coord, 0).x;
#elif (SHADOWMAPTYPE == SHADOWMAPTYPE_ARRAY)
	depth_value = tShadowMap.SampleLevel( sBilinear, float3( lookup_coord, (float)g_uElementIndex[cascade] ), 0).x;
#endif
	return depth_value;
}

float3 ParaboloidProject(float3 P, float zNear, float zFar)
{
	float3 outP;
	float lenP = length(P.xyz);
	outP.xyz = P.xyz/lenP;
	outP.x = outP.x / (outP.z + 1);
	outP.y = outP.y / (outP.z + 1);			
	outP.z = (lenP - zNear) / (zFar - zNear);
	return outP;
}

float3 ParaboloidUnproject(float3 P, float zNear, float zFar)
{
	// Use a quadratic to find the Z component
	// then reverse the projection to find the unit vector, and scale
	float L = P.z*(zFar-zNear) + zNear;

	float qa = P.x*P.x + P.y*P.y + 1;
	float qb = 2*(P.x*P.x + P.y*P.y);
	float qc = P.x*P.x + P.y*P.y - 1;
	float z = (-qb + sqrt(qb*qb - 4*qa*qc)) / (2*qa);

	float3 outP;
	outP.x = P.x * (z + 1);
	outP.y = P.y * (z + 1);
	outP.z = z;
	return outP*L;
}

HS_POLYGONAL_CONSTANT_DATA_OUTPUT Unused(HS_POLYGONAL_CONSTANT_DATA_OUTPUT input)
{
	return input;
}

[domain("quad")]
PS_POLYGONAL_INPUT main( HS_POLYGONAL_CONSTANT_DATA_OUTPUT input, float2 uv : SV_DOMAINLOCATION, const OutputPatch<HS_POLYGONAL_CONTROL_POINT_OUTPUT, 4> Patch )
{
	Unused(input);//Fix a compiler warning with pssl.

	PS_POLYGONAL_INPUT output = (PS_POLYGONAL_INPUT)0;

	float3 vClipIn1 = lerp(Patch[0].vClipPos.xyz, Patch[1].vClipPos.xyz, uv.x);
	float3 vClipIn2 = lerp(Patch[3].vClipPos.xyz, Patch[2].vClipPos.xyz, uv.x);
	float3 vClipIn = lerp(vClipIn1, vClipIn2, uv.y);

	float4 vPos1 = lerp(Patch[0].vWorldPos, Patch[1].vWorldPos, uv.x);
	float4 vPos2 = lerp(Patch[3].vWorldPos, Patch[2].vWorldPos, uv.x);
	float4 vWorldPos = lerp(vPos1, vPos2, uv.y);

	if (VOLUMETYPE == VOLUMETYPE_FRUSTUM)
	{
		if (all(abs(vClipIn.xy) < EDGE_FACTOR))
		{
			int iCascade = -1;
			float4 vClipPos = float4(0,0,0,1);

			[unroll]
			for (int i = COARSE_CASCADE;i >= 0; --i)
			{
				// Try to refetch from finer cascade
				float4 vClipPosCascade = mul( g_mLightProj[i], vWorldPos );
				vClipPosCascade *= 1.f / vClipPosCascade.w;
				if (all(abs(vClipPosCascade.xy) < 1.0f))
				{
					
					float2 vTex = float2(0.5*vClipPosCascade.x + 0.5, -0.5*vClipPosCascade.y + 0.5);
					float depthSample = SampleShadowMap(vTex, i);
					if (depthSample < 1.0f)
					{
						
						vClipPos.xy = vClipPosCascade.xy;
						vClipPos.z = depthSample;
						iCascade = i;
					}
				}
			}

			if (iCascade >= 0)
			{
				vWorldPos = mul( g_mLightProjInv[iCascade], float4(vClipPos.xyz, 1) );
				vWorldPos *= 1.0f / vWorldPos.w;
				vWorldPos.xyz = g_vEyePosition + (1.0f-g_fGodrayBias)*(vWorldPos.xyz-g_vEyePosition);
			}
		}
		else
		{
			vWorldPos = mul(g_mLightToWorld, float4(vClipIn.xy, 1, 1));
			vWorldPos *= 1.0f / vWorldPos.w;
		}
	}
	else if (VOLUMETYPE == VOLUMETYPE_PARABOLOID)
	{
        vClipIn.xyz = normalize(vClipIn.xyz);
		float4 shadowPos = mul(g_mLightProj[0], vWorldPos);
		shadowPos.xyz = shadowPos.xyz/shadowPos.w;
		uint hemisphereID = (shadowPos.z > 0) ? 0 : 1;
		shadowPos.z = abs(shadowPos.z);
		shadowPos.xyz = ParaboloidProject(shadowPos.xyz, g_fLightZNear, g_fLightZFar);
		float2 shadowTC = float2(0.5f, -0.5f)*shadowPos.xy + 0.5f;
        float depthSample = SampleShadowMap(shadowTC, hemisphereID);
		float sceneDepth = depthSample*(g_fLightZFar-g_fLightZNear)+g_fLightZNear;
		vWorldPos = mul( g_mLightProjInv[0], float4(vClipIn.xyz * sceneDepth, 1));
        vWorldPos *= 1.0f / vWorldPos.w;
	}

	// Transform world position with viewprojection matrix
	output.vWorldPos = vWorldPos;
    output.vPos = mul( g_mViewProj, output.vWorldPos );
    return output;
}