summaryrefslogtreecommitdiff
path: root/Runtime/Shaders/ShaderTests.cpp
blob: 21395435961cecd15c2ecf1e2dbc423e6399286f (plain)
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
#include "UnityPrefix.h"

#if ENABLE_UNIT_TESTS
#include "External/UnitTest++/src/UnitTest++.h"

#include "Runtime/Shaders/Shader.h"
#include "Runtime/Shaders/Material.h"
#include "Runtime/Shaders/ShaderNameRegistry.h"
#include "Runtime/Shaders/GraphicsCaps.h"

#include "External/shaderlab/Library/intshader.h"
#include "External/shaderlab/Library/properties.h"
#include "External/shaderlab/Library/shaderlab.h"
#include "External/shaderlab/Library/shaderlab.h"
#include "External/shaderlab/Library/ShaderParser.h"
#include "External/shaderlab/Library/SLParserData.h"
#include "External/shaderlab/Library/ShaderParserInterm.h"
#include "External/shaderlab/Library/ShaderParser.h"

#include "Runtime/Profiler/Profiler.h"
#include "Runtime/Profiler/ExternalGraphicsProfiler.h"
#include "Runtime/GfxDevice/GfxDevice.h"
#include "Runtime/Misc/ResourceManager.h"
#include "Runtime/Utilities/Word.h"

using namespace ShaderLab;

static void CheckPassAffectingProps_TestHelper (const char* desc, const ShaderLab::Pass* pass, const char* propName, ...)
{
	const PropertyNamesSet& names = pass->GetAffectingProps();

	vector_set<int> namesFound;
	bool error = false;

	// See if properties we expect do in fact affect the pass
	const char* currName = propName;
	va_list ap;
	va_start (ap, propName);
	while (currName != NULL)
	{
		ShaderLab::FastPropertyName currProp = ShaderLab::Property(currName);
		if (names.names.find(currProp.index) == names.names.end())
		{
			printf_console("ShaderTests '%s': property %s expected to affect it, but it doesn't!\n", desc, currName);
			error = true;
		}
		else
			namesFound.insert(currProp.index);
		currName = va_arg (ap, const char*);
	};
	va_end (ap);

	// Now print all properties that are affecting the pass, but we didn't expect them to
	for (vector_set<int>::const_iterator it = names.names.begin(); it != names.names.end(); ++it)
	{
		if (namesFound.find(*it) == namesFound.end())
		{
			ShaderLab::FastPropertyName prop;
			prop.index = *it;
			printf_console("Shader test %s: property %s is affecting shader, but is not expected to!\n", desc, prop.GetName());
			error = true;
		}
	}

	// If we had errors, print out all properties that are affecting the pass
	if (error)
	{
		std::string msg = "..shader test failed, properties that are affecting: ";
		for (vector_set<int>::const_iterator it = names.names.begin(); it != names.names.end(); ++it)
		{
			ShaderLab::FastPropertyName name;
			name.index = *it;
			msg += name.GetName();
			msg += " ";
		}
		printf_console("%s\n\n", msg.c_str());
		printf_console ("ShaderTests: Shader Pass affecting properties test failed, check output log");
		CHECK (false);
	}
}

struct ParseTestFixture
{
	ShaderErrors errors;
	bool ParseTest (const char* text)
	{
		errors.Clear();
		ShaderLab::ParserShader* parsedForm = PreParseShader (text, strlen(text), kGfxRendererNull, errors);
		if (!parsedForm)
			return false;
		UNITY_DELETE(parsedForm,kMemShader);
		return true;
	}
};

SUITE (ShaderTests)
{
#if UNITY_EDITOR

	TEST (Shader_ShadowCollectorPassCorrectZWrite_EditorOnly)
	{
		if (GetGfxDevice().GetRenderer() == kGfxRendererNull)
			return;

		// Ensure the presence of the 'VertexLit' shader.
		Shader* shader = GetScriptMapper().FindShader ("VertexLit");
		CHECK (shader != NULL);

		// 'VertexLit' shader should always have shadow collector pass.
		dynamic_array<ShaderLab::Pass*> collectorPasses;
		shader->GetShaderLabShader()->GetActiveSubShader().FindNamedPasses ("SHADOWCOLLECTOR", collectorPasses);
		CHECK (!collectorPasses.empty());

		// shadow collector should use LEqual depth test!
		const DeviceDepthState* depthState = collectorPasses[0]->GetState().GetDepthState();
		CHECK (depthState);
		CHECK (depthState->sourceState.depthFunc == kFuncLEqual);

		// FindNamedPasses calls Retain(), so do not leak memory: need to release them
		for (int i = 0; i < collectorPasses.size(); ++i)
			collectorPasses[i]->Release();
	}

	TEST (Shader_RenderQueueRespectsLOD_EditorOnly)
	{
		// two subshaders with different LOD, using different render queues
		const char* kShaderText =
			"Shader \"__Foo\" {\n"
			"SubShader { LOD 300 Tags {Queue=Geometry} Pass { } }\n"
			"SubShader { LOD 100 Tags {Queue=Transparent} Pass { } }\n"
			"}\n";
		ShaderLab::ParserShader* parsedForm = NULL;
		ShaderErrors errors;
		ShaderPtrVector dependencies;
		ShaderLab::IntShader* shader = ParseShader(kShaderText, strlen(kShaderText), dependencies, false, &parsedForm, errors, kGfxRendererD3D9);
		CHECK (shader && !errors.HasErrorsOrWarnings());
		CHECK (shader->GetSubShaders().size() == 2);

		// uses first subshader, Geometry queue
		CHECK (shader->GetRenderQueue() == 2000);

		// set LOD to not use the first subshader
		shader->SetMaximumShaderLOD(200);
		// should get queue from the 2nd subshader
		CHECK (shader->GetRenderQueue() == 3000);

		UNITY_DELETE( parsedForm, kMemShader);
		UNITY_DELETE( shader, kMemShader);
	}

	TEST (Shader_DependenciesDontHaveDuplicates_EditorOnly)
	{
		// two subshaders with different LOD, using different render queues
		const char* kShaderText =
			"Shader \"__Foo\" {\n"
			"SubShader {\n"
			"UsePass \"VertexLit/SHADOWCASTER\"\n"
			"UsePass \"VertexLit/SHADOWCASTER\"\n"
			"UsePass \"VertexLit/SHADOWCASTER\"\n"
			"}\n"
			"Fallback Off\n"
			"}\n";
		ShaderLab::ParserShader* parsedForm = NULL;
		ShaderErrors errors;
		ShaderPtrVector dependencies;
		ShaderLab::IntShader* shader = ParseShader(kShaderText, strlen(kShaderText), dependencies, false, &parsedForm, errors, kGfxRendererD3D9);
		CHECK (shader && !errors.HasErrorsOrWarnings());
		CHECK (shader->GetSubShaders().size() == 1);

		CHECK (dependencies.size() == 1);

		UNITY_DELETE (parsedForm, kMemShader);
		UNITY_DELETE (shader, kMemShader);
	}

	static void UsedVertexComponents_TestHelper (const char* name, UInt32 expected, UInt32 expectedLM)
	{
		Shader* shader = GetScriptMapper().FindShader (name);
		Assert (shader);
		UInt32 channels = shader->CalculateUsedVertexComponents(false);
		Assert (channels == expected);
		channels = shader->CalculateUsedVertexComponents(true);
		Assert (channels == expectedLM);
	}

	TEST (Shader_UsedVertexComponents_EditorOnly)
	{
		if (gGraphicsCaps.GetEmulation() != GraphicsCaps::kEmulNone ||
			GetGfxDevice().GetRenderer() == kGfxRendererNull ||
			GetGfxDevice().GetRenderer() == kGfxRendererD3D11)
			return;

		UInt32 expLightingUV = (1<<kShaderChannelVertex) | (1<<kShaderChannelNormal) | (1<<kShaderChannelTexCoord0);
		UInt32 expLightingUV2 = (1<<kShaderChannelVertex) | (1<<kShaderChannelNormal) | (1<<kShaderChannelTexCoord0) | (1<<kShaderChannelTexCoord1);
		UInt32 expLightingTanUV = (1<<kShaderChannelVertex) | (1<<kShaderChannelNormal) | (1<<kShaderChannelTexCoord0) | (1<<kShaderChannelTangent);
		UInt32 expLightingTanUV2 = (1<<kShaderChannelVertex) | (1<<kShaderChannelNormal) | (1<<kShaderChannelTexCoord0) | (1<<kShaderChannelTexCoord1) | (1<<kShaderChannelTangent);
		UInt32 expUV1 = (1<<kShaderChannelVertex) | (1<<kShaderChannelTexCoord0);
		UInt32 expColorUV1 = (1<<kShaderChannelVertex) | (1<<kShaderChannelTexCoord0) | (1<<kShaderChannelColor);

		UsedVertexComponents_TestHelper ("VertexLit", expLightingUV, expLightingUV2);
		UsedVertexComponents_TestHelper ("Diffuse", expLightingUV, expLightingUV2);
		UsedVertexComponents_TestHelper ("Bumped Diffuse", expLightingTanUV, expLightingTanUV2);
		UsedVertexComponents_TestHelper ("Unlit/Texture", expUV1, expUV1);
		UsedVertexComponents_TestHelper ("Particles/Additive", expColorUV1, expColorUV1);
	}

	TEST (Shader_PassAffectingPropsWork_EditorOnly)
	{
		if (GetGfxDevice().GetRenderer() == kGfxRendererNull || gGraphicsCaps.shaderCaps < kShaderLevel2)
			return;

		Shader* shSpec = GetBuiltinExtraResource<Shader>("Normal-Glossy.shader");
		CHECK (shSpec != NULL);

		CheckPassAffectingProps_TestHelper("Specular ForwardBase pass", shSpec->GetShaderLabShader()->GetSubShader(0).GetPass(0), "_MainTex", "_Color", "_Shininess", "_SpecColor", "_MainTex_ST", "_ShadowMapTexture", NULL);
		CheckPassAffectingProps_TestHelper("Specular ForwardAdd pass",  shSpec->GetShaderLabShader()->GetSubShader(0).GetPass(1), "_MainTex", "_Color", "_Shininess", "_SpecColor", "_MainTex_ST", "_LightTexture0", "_LightTextureB0", NULL);

		// None of user-accessible properties are affecting shadow caster pass
		CheckPassAffectingProps_TestHelper("Specular ShadowCaster pass", shSpec->GetShadowCasterPass(), NULL);

		// Specular falls back to VertexLit, so subshader #1 is vertex lit
		CheckPassAffectingProps_TestHelper("VertexLit vertex pass", shSpec->GetShaderLabShader()->GetSubShader(1).GetPass(0), "_MainTex", "_Color", "_Shininess", "_SpecColor", "_Emission", NULL);

		Shader* shCutoutBSpec = GetBuiltinExtraResource<Shader>("AlphaTest-Bumped.shader");
		CHECK (shCutoutBSpec !=  NULL);

		CheckPassAffectingProps_TestHelper("Cutout BumpSpec ForwardBase pass", shCutoutBSpec->GetShaderLabShader()->GetSubShader(0).GetPass(0), "_MainTex", "_Cutoff", "_BumpMap", "_Color", "_MainTex_ST", "_BumpMap_ST", "_ShadowMapTexture", NULL);

		// Alpha tested shader: texture, cutoff etc. affect shadow caster pass. Bumpmap doesn't affect it.
		CheckPassAffectingProps_TestHelper("Cutout BumpSpec pass", shCutoutBSpec->GetShadowCasterPass(), "_MainTex", "_Cutoff", "_Color", "_MainTex_ST", NULL);
	}

	TEST (Shader_MaterialGetDefaultProperties_EditorOnly)
	{
		// Check that a shader with some default property values actually gets them in the material.
		const char* kShader =
			"Shader \"\" {\n"
			"Properties {\n"
			"myColor (\"\", color) = (1,2,3,4)\n"
			"myFloat (\"\", float) = 0.5\n"
			"myRange (\"\", range(0,10)) = 5.0\n"
			"myVector (\"\", vector) = (5,6,7,8)\n"
			"}\n"
			"SubShader { Pass { } }\n"
			"}";
		Material* mat = Material::CreateMaterial(kShader,Object::kHideAndDontSave);
		Shader* myShader = mat->GetShader();

		ShaderLab::FastPropertyName propShine = ShaderLab::Property("_Shininess");
		ShaderLab::FastPropertyName propMyColor = ShaderLab::Property("myColor");
		ShaderLab::FastPropertyName propMyFloat = ShaderLab::Property("myFloat");
		ShaderLab::FastPropertyName propMyRange = ShaderLab::Property("myRange");
		ShaderLab::FastPropertyName propMyVector = ShaderLab::Property("myVector");

		// Don't compare in linear space; returned value close but slightly different due to float conversions.
		if (GetActiveColorSpace() == kGammaColorSpace)
		{
			CHECK (mat->GetColor(propMyColor).Equals(ColorRGBAf(1,2,3,4)));
		}
		CHECK (mat->GetFloat(propMyFloat) == 0.5f);
		CHECK (mat->GetFloat(propMyRange) == 5.0f);
		CHECK (mat->GetColor(propMyVector).Equals(ColorRGBAf(5,6,7,8)));

		// Assign a different shader.
		Shader* vlitShader = GetScriptMapper().FindShader("VertexLit");
		CHECK (!mat->HasProperty(propShine));
		mat->SetShader (vlitShader);

		// Now we should get _Shininess, with the default value.
		float shininess = mat->GetFloat(propShine);
		CHECK (shininess == 0.7f);

		// Now, new shader doesn't have myFloat, so it shouldn't be in the runtime PropertySheet.
		CHECK (!mat->HasProperty(propMyFloat));
		// But the value should still be intact in the serialized sheet!
		CHECK (mat->GetSavedProperties().m_Floats[propMyFloat] == 0.5f);

		DestroySingleObject (mat);
		DestroySingleObject (myShader);
	}

#endif // #if UNITY_EDITOR
}

#endif // #if ENABLE_UNIT_TESTS