summaryrefslogtreecommitdiff
path: root/materialsystem/stdshaders/weapon_sheen_pass_helper.cpp
blob: 05fb43f1428833329617adc482e5112089d0d963 (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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
//========= Copyright Valve Corporation, All rights reserved. ============//

/* Based heavily on cloak_blended_pass, look at cloak_blended_pass_helper.cpp

==================================================================================================== */

#include "BaseVSShader.h"
#include "mathlib/vmatrix.h"
#include "weapon_sheen_pass_helper.h"
#include "convar.h"

// Auto generated inc files
#include "weapon_sheen_pass_vs20.inc"
#include "weapon_sheen_pass_ps20.inc"
#include "weapon_sheen_pass_ps20b.inc"

#ifndef _X360
#include "weapon_sheen_pass_vs30.inc"
#include "weapon_sheen_pass_ps30.inc"
#endif

void InitParamsWeaponSheenPass( CBaseVSShader *pShader, IMaterialVar** params, const char *pMaterialName, WeaponSheenPassVars_t &info )
{
	// Set material flags
	SET_FLAGS2( MATERIAL_VAR2_SUPPORTS_HW_SKINNING );
	SET_FLAGS( MATERIAL_VAR_MODEL );
	SET_FLAGS2( MATERIAL_VAR2_NEEDS_TANGENT_SPACES );

	// Set material parameter default values
	if ( ( info.m_nSheenMapMaskFrame != -1 ) && ( !params[info.m_nSheenMapMaskFrame]->IsDefined() ) )
	{
		params[info.m_nSheenMapMaskFrame]->SetFloatValue( 0 );
	}

	if ( ( info.m_nSheenMapTint != -1 ) && ( !params[info.m_nSheenMapTint]->IsDefined() ) )
	{
		params[info.m_nSheenMapTint]->SetVecValue( 1.0f, 1.0f, 1.0f, 1.0f );
	}

	if ( ( info.m_nSheenMapMaskScaleX != -1 ) && ( !params[info.m_nSheenMapMaskScaleX]->IsDefined() ) )
	{
		params[info.m_nSheenMapMaskScaleX]->SetFloatValue( 1.0f );
	}
	if ( ( info.m_nSheenMapMaskScaleY != -1 ) && ( !params[info.m_nSheenMapMaskScaleY]->IsDefined() ) )
	{
		params[info.m_nSheenMapMaskScaleY]->SetFloatValue( 1.0f );
	}

	if ( ( info.m_nSheenMapMaskOffsetX != -1 ) && ( !params[info.m_nSheenMapMaskOffsetX]->IsDefined() ) )
	{
		params[info.m_nSheenMapMaskOffsetX]->SetFloatValue( 0 );
	}
	if ( ( info.m_nSheenMapMaskOffsetY != -1 ) && ( !params[info.m_nSheenMapMaskOffsetY]->IsDefined() ) )
	{
		params[info.m_nSheenMapMaskOffsetY]->SetFloatValue( 0 );
	}

	if ( ( info.m_nSheenMapMaskDirection != -1 ) && ( !params[info.m_nSheenMapMaskDirection]->IsDefined() ) )
	{
		params[info.m_nSheenMapMaskDirection]->SetFloatValue( 0 );
	}

	if( (info.m_nSheenIndex != -1 ) && !params[info.m_nSheenIndex]->IsDefined() )
	{
		params[info.m_nSheenIndex]->SetIntValue( 0 );
	}

	if( (info.m_nBumpFrame != -1 ) && !params[info.m_nBumpFrame]->IsDefined() )
	{
		params[info.m_nBumpFrame]->SetIntValue( 0 );
	}
}

void InitWeaponSheenPass( CBaseVSShader *pShader, IMaterialVar** params, WeaponSheenPassVars_t &info )
{
	// Load textures
	if ( g_pConfig->UseBumpmapping() )
	{
		if ( (info.m_nBumpmap != -1) && params[info.m_nBumpmap]->IsDefined() )
		{
			pShader->LoadTexture( info.m_nBumpmap );
		}
	}

	if ( (info.m_nSheenMap != -1) && params[info.m_nSheenMap]->IsDefined() )
	{
		pShader->LoadCubeMap( info.m_nSheenMap, g_pHardwareConfig->GetHDRType() == HDR_TYPE_NONE ? TEXTUREFLAGS_SRGB : 0  );
	}

	if ( (info.m_nSheenMapMask != -1) && params[info.m_nSheenMapMask]->IsDefined() )
	{
		pShader->LoadTexture( info.m_nSheenMapMask );
	}
	
}

void DrawWeaponSheenPass( CBaseVSShader *pShader, IMaterialVar** params, IShaderDynamicAPI *pShaderAPI,
						  IShaderShadow* pShaderShadow, WeaponSheenPassVars_t &info, VertexCompressionType_t vertexCompression )
{
	bool bBumpMapping = ( !g_pConfig->UseBumpmapping() ) || ( info.m_nBumpmap == -1 ) || !params[info.m_nBumpmap]->IsTexture() ? 0 : 1;

	SHADOW_STATE
	{
		// Reset shadow state manually since we're drawing from two materials
		pShader->SetInitialShadowState( );

		// Set stream format (note that this shader supports compression)
		unsigned int flags = VERTEX_POSITION | VERTEX_NORMAL | VERTEX_FORMAT_COMPRESSED;
		int nTexCoordCount = 1;
		int userDataSize = 0;
		pShaderShadow->VertexShaderVertexFormat( flags, nTexCoordCount, NULL, userDataSize );

#ifndef _X360
		if ( !g_pHardwareConfig->HasFastVertexTextures() )
#endif
		{
			// Vertex Shader
			DECLARE_STATIC_VERTEX_SHADER( weapon_sheen_pass_vs20 );
			SET_STATIC_VERTEX_SHADER_COMBO( BUMPMAP, bBumpMapping ? 1 : 0 );
			SET_STATIC_VERTEX_SHADER( weapon_sheen_pass_vs20 );

			// Pixel Shader
			if( g_pHardwareConfig->SupportsPixelShaders_2_b() )
			{
				DECLARE_STATIC_PIXEL_SHADER( weapon_sheen_pass_ps20b );
				SET_STATIC_PIXEL_SHADER_COMBO( BUMPMAP, bBumpMapping ? 1 : 0 );
				SET_STATIC_PIXEL_SHADER( weapon_sheen_pass_ps20b );
			}
			else
			{
				DECLARE_STATIC_PIXEL_SHADER( weapon_sheen_pass_ps20 );
				SET_STATIC_PIXEL_SHADER_COMBO( BUMPMAP, bBumpMapping ? 1 : 0 );
				SET_STATIC_PIXEL_SHADER( weapon_sheen_pass_ps20 );
			}
		}
#ifndef _X360
		else
		{
			// The vertex shader uses the vertex id stream
			SET_FLAGS2( MATERIAL_VAR2_USES_VERTEXID );

			// Vertex Shader
			DECLARE_STATIC_VERTEX_SHADER( weapon_sheen_pass_vs30 );
			SET_STATIC_VERTEX_SHADER_COMBO( BUMPMAP, bBumpMapping ? 1 : 0 );
			SET_STATIC_VERTEX_SHADER( weapon_sheen_pass_vs30 );

			// Pixel Shader
			DECLARE_STATIC_PIXEL_SHADER( weapon_sheen_pass_ps30 );
			SET_STATIC_PIXEL_SHADER_COMBO( BUMPMAP, bBumpMapping ? 1 : 0 );
			SET_STATIC_PIXEL_SHADER( weapon_sheen_pass_ps30 );
		}
#endif

		// Textures
		pShaderShadow->EnableTexture( SHADER_SAMPLER0, true ); // Refraction texture
		pShaderShadow->EnableSRGBRead( SHADER_SAMPLER0, true );
		if ( bBumpMapping )
		{
			pShaderShadow->EnableTexture( SHADER_SAMPLER1, true ); // Bump
			pShaderShadow->EnableSRGBRead( SHADER_SAMPLER1, false ); // Not sRGB
		}
		pShaderShadow->EnableSRGBWrite( true );

		pShaderShadow->EnableTexture( SHADER_SAMPLER2, true );
		if( g_pHardwareConfig->GetHDRType() == HDR_TYPE_NONE )
		{
			pShaderShadow->EnableSRGBRead( SHADER_SAMPLER2, true );
		}

		pShaderShadow->EnableTexture( SHADER_SAMPLER3, true );
		if( g_pHardwareConfig->GetHDRType() == HDR_TYPE_NONE )
		{
			pShaderShadow->EnableSRGBRead( SHADER_SAMPLER3, true );
		}

		// Blending
		pShader->EnableAlphaBlending( SHADER_BLEND_SRC_ALPHA, SHADER_BLEND_ONE_MINUS_SRC_ALPHA );
		pShaderShadow->EnableAlphaWrites( false );

		// !!! We need to turn this back on because EnableAlphaBlending() above disables it!
		pShaderShadow->EnableDepthWrites( true );
	}
	DYNAMIC_STATE
	{
		// Reset render state manually since we're drawing from two materials
		pShaderAPI->SetDefaultState();

		// Set Vertex Shader Constants 
		if ( ( bBumpMapping ) && ( info.m_nBumpTransform != -1 ) )
		{
			pShader->SetVertexShaderTextureTransform( VERTEX_SHADER_SHADER_SPECIFIC_CONST_0, info.m_nBumpTransform );
		}

#ifndef _X360
		if ( !g_pHardwareConfig->HasFastVertexTextures() )
#endif
		{
			// Set Vertex Shader Combos
			DECLARE_DYNAMIC_VERTEX_SHADER( weapon_sheen_pass_vs20 );
			SET_DYNAMIC_VERTEX_SHADER_COMBO( SKINNING, pShaderAPI->GetCurrentNumBones() > 0 );
			SET_DYNAMIC_VERTEX_SHADER_COMBO( COMPRESSED_VERTS, (int)vertexCompression );
			SET_DYNAMIC_VERTEX_SHADER( weapon_sheen_pass_vs20 );

			// Set Pixel Shader Combos
			if( g_pHardwareConfig->SupportsPixelShaders_2_b() )
			{
				DECLARE_DYNAMIC_PIXEL_SHADER( weapon_sheen_pass_ps20b );
				SET_DYNAMIC_PIXEL_SHADER( weapon_sheen_pass_ps20b );
			}
			else
			{
				DECLARE_DYNAMIC_PIXEL_SHADER( weapon_sheen_pass_ps20 );
				SET_DYNAMIC_PIXEL_SHADER( weapon_sheen_pass_ps20 );
			}
		}
#ifndef _X360
		else
		{
			pShader->SetHWMorphVertexShaderState( VERTEX_SHADER_SHADER_SPECIFIC_CONST_6, VERTEX_SHADER_SHADER_SPECIFIC_CONST_7, SHADER_VERTEXTEXTURE_SAMPLER0 );

			// Set Vertex Shader Combos
			DECLARE_DYNAMIC_VERTEX_SHADER( weapon_sheen_pass_vs30 );
			SET_DYNAMIC_VERTEX_SHADER_COMBO( SKINNING, pShaderAPI->GetCurrentNumBones() > 0 );
			SET_DYNAMIC_VERTEX_SHADER_COMBO( MORPHING, pShaderAPI->IsHWMorphingEnabled() );
			SET_DYNAMIC_VERTEX_SHADER_COMBO( COMPRESSED_VERTS, (int)vertexCompression );
			SET_DYNAMIC_VERTEX_SHADER( weapon_sheen_pass_vs30 );

			// Set Pixel Shader Combos
			DECLARE_DYNAMIC_PIXEL_SHADER( weapon_sheen_pass_ps30 );
			SET_DYNAMIC_PIXEL_SHADER( weapon_sheen_pass_ps30 );
		}
#endif

		// Bind textures
		pShaderAPI->BindStandardTexture( SHADER_SAMPLER0, TEXTURE_FRAME_BUFFER_FULL_TEXTURE_0 ); // Refraction Map
		if ( bBumpMapping )
		{
			pShader->BindTexture( SHADER_SAMPLER1, info.m_nBumpmap, info.m_nBumpFrame );
		}

		if ( info.m_nSheenMap != -1 )
		{
			pShader->BindTexture( SHADER_SAMPLER2, info.m_nSheenMap, -1 );
		}
		
		if ( info.m_nSheenMapMask != -1 )
		{
			pShader->BindTexture( SHADER_SAMPLER3, info.m_nSheenMapMask, info.m_nSheenMapMaskFrame );
		}

		// Set Pixel Shader Constants 
		float vEyePos[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
		pShaderAPI->GetWorldSpaceCameraPosition( vEyePos );
		pShaderAPI->SetPixelShaderConstant( 5, vEyePos, 1 );

		float vPackedConst1[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
		vPackedConst1[0] = IS_PARAM_DEFINED( info.m_nSheenMapMaskScaleX ) ? params[info.m_nSheenMapMaskScaleX]->GetFloatValue() : 1.0f;
		vPackedConst1[1] = IS_PARAM_DEFINED( info.m_nSheenMapMaskScaleY ) ? params[info.m_nSheenMapMaskScaleY]->GetFloatValue() : 1.0f;
		vPackedConst1[2] = IS_PARAM_DEFINED( info.m_nSheenMapMaskOffsetX ) ? params[info.m_nSheenMapMaskOffsetX]->GetFloatValue() : 0.0f;
		vPackedConst1[3] = IS_PARAM_DEFINED( info.m_nSheenMapMaskOffsetY ) ? params[info.m_nSheenMapMaskOffsetY]->GetFloatValue() : 0.0f;
		pShaderAPI->SetPixelShaderConstant( 6, vPackedConst1, 1 );

		float vPackedConst2[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
		vPackedConst2[0] = IS_PARAM_DEFINED( info.m_nSheenMapMaskDirection ) ? params[info.m_nSheenMapMaskDirection]->GetFloatValue() : 0.0f;
		vPackedConst2[1] = IS_PARAM_DEFINED( info.m_nSheenIndex ) ? params[info.m_nSheenIndex]->GetFloatValue() : 0.0f;
		pShaderAPI->SetPixelShaderConstant( 7, vPackedConst2, 1 );

		// Map color tint
		pShaderAPI->SetPixelShaderConstant( 8, IS_PARAM_DEFINED( info.m_nSheenMapTint ) ? params[info.m_nSheenMapTint]->GetVecValue() : kDefaultSheenColorTint, 1 );

		// Set c0 and c1 to contain first two rows of ViewProj matrix
		VMatrix mView, mProj;
		pShaderAPI->GetMatrix( MATERIAL_VIEW, mView.m[0] );
		pShaderAPI->GetMatrix( MATERIAL_PROJECTION, mProj.m[0] );
		VMatrix mViewProj = mView * mProj;
		mViewProj = mViewProj.Transpose3x3();
		pShaderAPI->SetPixelShaderConstant( 0, mViewProj.m[0], 2 );
	}
	pShader->Draw();
}

bool ShouldDrawMaterialSheen ( IMaterialVar** params, WeaponSheenPassVars_t &info )
{
	// If the frame is zero we're not rendering
	if ( IS_PARAM_DEFINED( info.m_nSheenMapMaskFrame ) && params[info.m_nSheenMapMaskFrame]->GetIntValue() > 0 )
	{
		return true;
	}
	
	return false;
}