summaryrefslogtreecommitdiff
path: root/modules/gles31/functional/es31fComputeShaderBuiltinVarTests.cpp
blob: d074306dad468d4b6315f95ac12140a43b5701c0 (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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
/*-------------------------------------------------------------------------
 * drawElements Quality Program OpenGL ES 3.1 Module
 * -------------------------------------------------
 *
 * Copyright 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 *//*!
 * \file
 * \brief Compute Shader Built-in variable tests.
 *//*--------------------------------------------------------------------*/

#include "es31fComputeShaderBuiltinVarTests.hpp"
#include "gluShaderProgram.hpp"
#include "gluShaderUtil.hpp"
#include "gluRenderContext.hpp"
#include "gluObjectWrapper.hpp"
#include "gluProgramInterfaceQuery.hpp"
#include "tcuVector.hpp"
#include "tcuTestLog.hpp"
#include "tcuVectorUtil.hpp"
#include "deSharedPtr.hpp"
#include "deStringUtil.hpp"
#include "glwFunctions.hpp"
#include "glwEnums.hpp"

#include <map>

namespace deqp
{
namespace gles31
{
namespace Functional
{

using std::string;
using std::vector;
using std::map;
using tcu::TestLog;
using tcu::UVec3;
using tcu::IVec3;

using namespace glu;

template<typename T, int Size>
struct LexicalCompareVec
{
	inline bool operator() (const tcu::Vector<T, Size>& a, const tcu::Vector<T, Size>& b) const
	{
		for (int ndx = 0; ndx < Size; ndx++)
		{
			if (a[ndx] < b[ndx])
				return true;
			else if (a[ndx] > b[ndx])
				return false;
		}
		return false;
	}
};

typedef de::SharedPtr<glu::ShaderProgram>										ShaderProgramSp;
typedef std::map<tcu::UVec3, ShaderProgramSp, LexicalCompareVec<deUint32, 3> >	LocalSizeProgramMap;

class ComputeBuiltinVarCase : public TestCase
{
public:
							ComputeBuiltinVarCase	(Context& context, const char* name, const char* varName, DataType varType);
							~ComputeBuiltinVarCase	(void);

	void					init					(void);
	void					deinit					(void);
	IterateResult			iterate					(void);

	virtual UVec3			computeReference		(const UVec3& numWorkGroups, const UVec3& workGroupSize, const UVec3& workGroupID, const UVec3& localInvocationID) const = 0;

protected:
	struct SubCase
	{
		UVec3		localSize;
		UVec3		numWorkGroups;

		SubCase (void) {}
		SubCase (const UVec3& localSize_, const UVec3& numWorkGroups_) : localSize(localSize_), numWorkGroups(numWorkGroups_) {}
	};

	vector<SubCase>			m_subCases;

private:
							ComputeBuiltinVarCase	(const ComputeBuiltinVarCase& other);
	ComputeBuiltinVarCase&	operator=				(const ComputeBuiltinVarCase& other);

	deUint32				getProgram				(const UVec3& localSize);

	const string			m_varName;
	const DataType			m_varType;

	LocalSizeProgramMap		m_progMap;
	int						m_subCaseNdx;
};

ComputeBuiltinVarCase::ComputeBuiltinVarCase (Context& context, const char* name, const char* varName, DataType varType)
	: TestCase		(context, name, varName)
	, m_varName		(varName)
	, m_varType		(varType)
	, m_subCaseNdx	(0)
{
}

ComputeBuiltinVarCase::~ComputeBuiltinVarCase (void)
{
	ComputeBuiltinVarCase::deinit();
}

void ComputeBuiltinVarCase::init (void)
{
	m_testCtx.setTestResult(QP_TEST_RESULT_PASS, "Pass");
	m_subCaseNdx = 0;
}

void ComputeBuiltinVarCase::deinit (void)
{
	m_progMap.clear();
}

static string genBuiltinVarSource (const string& varName, DataType varType, const UVec3& localSize)
{
	std::ostringstream src;

	src << "#version 310 es\n"
		<< "layout (local_size_x = " << localSize.x() << ", local_size_y = " << localSize.y() << ", local_size_z = " << localSize.z() << ") in;\n"
		<< "uniform highp uvec2 u_stride;\n"
		<< "layout(binding = 0) buffer Output\n"
		<< "{\n"
		<< "	" << glu::getDataTypeName(varType) << " result[];\n"
		<< "} sb_out;\n"
		<< "\n"
		<< "void main (void)\n"
		<< "{\n"
		<< "	highp uint offset = u_stride.x*gl_GlobalInvocationID.z + u_stride.y*gl_GlobalInvocationID.y + gl_GlobalInvocationID.x;\n"
		<< "	sb_out.result[offset] = " << varName << ";\n"
		<< "}\n";

	return src.str();
}

deUint32 ComputeBuiltinVarCase::getProgram (const UVec3& localSize)
{
	LocalSizeProgramMap::const_iterator cachePos = m_progMap.find(localSize);
	if (cachePos != m_progMap.end())
		return cachePos->second->getProgram();
	else
	{
		ShaderProgramSp program(new ShaderProgram(m_context.getRenderContext(),
												  ProgramSources() << ComputeSource(genBuiltinVarSource(m_varName, m_varType, localSize))));

		// Log all compiled programs.
		m_testCtx.getLog() << *program;
		if (!program->isOk())
			throw tcu::TestError("Compile failed");

		m_progMap[localSize] = program;
		return program->getProgram();
	}
}

static inline UVec3 readResultVec (const deUint32* ptr, int numComps)
{
	UVec3 res;
	for (int ndx = 0; ndx < numComps; ndx++)
		res[ndx] = ptr[ndx];
	return res;
}

static inline bool compareComps (const UVec3& a, const UVec3& b, int numComps)
{
	DE_ASSERT(numComps == 1 || numComps == 3);
	return numComps == 3 ? tcu::allEqual(a, b) : a.x() == b.x();
}

struct LogComps
{
	const UVec3&	v;
	int				numComps;

	LogComps (const UVec3& v_, int numComps_) : v(v_), numComps(numComps_) {}
};

static inline std::ostream& operator<< (std::ostream& str, const LogComps& c)
{
	DE_ASSERT(c.numComps == 1 || c.numComps == 3);
	return c.numComps == 3 ? str << c.v : str << c.v.x();
}

ComputeBuiltinVarCase::IterateResult ComputeBuiltinVarCase::iterate (void)
{
	const tcu::ScopedLogSection		section			(m_testCtx.getLog(), string("Iteration") + de::toString(m_subCaseNdx), string("Iteration ") + de::toString(m_subCaseNdx));
	const glw::Functions&			gl				= m_context.getRenderContext().getFunctions();
	const SubCase&					subCase			= m_subCases[m_subCaseNdx];
	const deUint32					program			= getProgram(subCase.localSize);

	const tcu::UVec3				globalSize		= subCase.localSize*subCase.numWorkGroups;
	const tcu::UVec2				stride			(globalSize[0]*globalSize[1], globalSize[0]);
	const deUint32					numInvocations	= subCase.localSize[0]*subCase.localSize[1]*subCase.localSize[2]*subCase.numWorkGroups[0]*subCase.numWorkGroups[1]*subCase.numWorkGroups[2];

	const deUint32					outVarIndex		= gl.getProgramResourceIndex(program, GL_BUFFER_VARIABLE, "Output.result");
	const InterfaceVariableInfo		outVarInfo		= getProgramInterfaceVariableInfo(gl, program, GL_BUFFER_VARIABLE, outVarIndex);
	const deUint32					bufferSize		= numInvocations*outVarInfo.arrayStride;
	Buffer							outputBuffer	(m_context.getRenderContext());

	TCU_CHECK(outVarInfo.arraySize == 0); // Unsized variable.

	m_testCtx.getLog() << TestLog::Message << "Number of work groups = " << subCase.numWorkGroups << TestLog::EndMessage
					   << TestLog::Message << "Work group size = " << subCase.localSize << TestLog::EndMessage;

	gl.bindBuffer(GL_SHADER_STORAGE_BUFFER, *outputBuffer);
	gl.bufferData(GL_SHADER_STORAGE_BUFFER, (glw::GLsizeiptr)bufferSize, DE_NULL, GL_STREAM_READ);
	gl.bindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, *outputBuffer);
	GLU_EXPECT_NO_ERROR(gl.getError(), "Buffer setup failed");

	gl.useProgram(program);
	gl.uniform2uiv(gl.getUniformLocation(program, "u_stride"), 1, stride.getPtr());
	GLU_EXPECT_NO_ERROR(gl.getError(), "Program setup failed");

	gl.dispatchCompute(subCase.numWorkGroups[0], subCase.numWorkGroups[1], subCase.numWorkGroups[2]);
	GLU_EXPECT_NO_ERROR(gl.getError(), "glDispatchCompute() failed");

	{
		const void*	ptr				= gl.mapBufferRange(GL_SHADER_STORAGE_BUFFER, 0, bufferSize, GL_MAP_READ_BIT);
		int			numFailed		= 0;
		const int	numScalars		= getDataTypeScalarSize(m_varType);
		const int	maxLogPrints	= 10;

		GLU_EXPECT_NO_ERROR(gl.getError(), "glMapBufferRange() failed");
		TCU_CHECK(ptr);

		for (deUint32 groupZ = 0; groupZ < subCase.numWorkGroups.z(); groupZ++)
		for (deUint32 groupY = 0; groupY < subCase.numWorkGroups.y(); groupY++)
		for (deUint32 groupX = 0; groupX < subCase.numWorkGroups.x(); groupX++)
		for (deUint32 localZ = 0; localZ < subCase.localSize.z(); localZ++)
		for (deUint32 localY = 0; localY < subCase.localSize.y(); localY++)
		for (deUint32 localX = 0; localX < subCase.localSize.x(); localX++)
		{
			const UVec3			refGroupID		(groupX, groupY, groupZ);
			const UVec3			refLocalID		(localX, localY, localZ);
			const UVec3			refGlobalID		= refGroupID * subCase.localSize + refLocalID;
			const deUint32		refOffset		= stride.x()*refGlobalID.z() + stride.y()*refGlobalID.y() + refGlobalID.x();
			const UVec3			refValue		= computeReference(subCase.numWorkGroups, subCase.localSize, refGroupID, refLocalID);

			const deUint32*		resPtr			= (const deUint32*)((const deUint8*)ptr + refOffset*outVarInfo.arrayStride);
			const UVec3			resValue		= readResultVec(resPtr, numScalars);

			if (!compareComps(refValue, resValue, numScalars))
			{
				if (numFailed < maxLogPrints)
					m_testCtx.getLog() << TestLog::Message << "ERROR: comparison failed at offset " << refOffset
														   << ": expected " << LogComps(refValue, numScalars)
														   << ", got " << LogComps(resValue, numScalars)
									   << TestLog::EndMessage;
				else if (numFailed == maxLogPrints)
					m_testCtx.getLog() << TestLog::Message << "..." << TestLog::EndMessage;

				numFailed += 1;
			}
		}

		m_testCtx.getLog() << TestLog::Message << (numInvocations-numFailed) << " / " << numInvocations << " values passed" << TestLog::EndMessage;

		if (numFailed > 0)
			m_testCtx.setTestResult(QP_TEST_RESULT_FAIL, "Comparison failed");

		gl.unmapBuffer(GL_SHADER_STORAGE_BUFFER);
	}

	m_subCaseNdx += 1;
	return (m_subCaseNdx < (int)m_subCases.size() && m_testCtx.getTestResult() == QP_TEST_RESULT_PASS) ? CONTINUE : STOP;
}

// Test cases

class NumWorkGroupsCase : public ComputeBuiltinVarCase
{
public:
	NumWorkGroupsCase (Context& context)
		: ComputeBuiltinVarCase(context, "num_work_groups", "gl_NumWorkGroups", TYPE_UINT_VEC3)
	{
		m_subCases.push_back(SubCase(UVec3(1,1,1), UVec3(1,1,1)));
		m_subCases.push_back(SubCase(UVec3(1,1,1), UVec3(52,1,1)));
		m_subCases.push_back(SubCase(UVec3(1,1,1), UVec3(1,39,1)));
		m_subCases.push_back(SubCase(UVec3(1,1,1), UVec3(1,1,78)));
		m_subCases.push_back(SubCase(UVec3(1,1,1), UVec3(4,7,11)));
		m_subCases.push_back(SubCase(UVec3(2,3,4), UVec3(4,7,11)));
	}

	UVec3 computeReference (const UVec3& numWorkGroups, const UVec3& workGroupSize, const UVec3& workGroupID, const UVec3& localInvocationID) const
	{
		DE_UNREF(numWorkGroups);
		DE_UNREF(workGroupSize);
		DE_UNREF(workGroupID);
		DE_UNREF(localInvocationID);
		return numWorkGroups;
	}
};

class WorkGroupSizeCase : public ComputeBuiltinVarCase
{
public:
	WorkGroupSizeCase (Context& context)
		: ComputeBuiltinVarCase(context, "work_group_size", "gl_WorkGroupSize", TYPE_UINT_VEC3)
	{
		m_subCases.push_back(SubCase(UVec3(1,1,1), UVec3(1,1,1)));
		m_subCases.push_back(SubCase(UVec3(1,1,1), UVec3(2,7,3)));
		m_subCases.push_back(SubCase(UVec3(2,1,1), UVec3(1,1,1)));
		m_subCases.push_back(SubCase(UVec3(2,1,1), UVec3(1,3,5)));
		m_subCases.push_back(SubCase(UVec3(1,3,1), UVec3(1,1,1)));
		m_subCases.push_back(SubCase(UVec3(1,1,7), UVec3(1,1,1)));
		m_subCases.push_back(SubCase(UVec3(1,1,7), UVec3(3,3,1)));
		m_subCases.push_back(SubCase(UVec3(11,3,8), UVec3(1,1,1)));
		m_subCases.push_back(SubCase(UVec3(11,3,8), UVec3(3,1,2)));
	}

	UVec3 computeReference (const UVec3& numWorkGroups, const UVec3& workGroupSize, const UVec3& workGroupID, const UVec3& localInvocationID) const
	{
		DE_UNREF(numWorkGroups);
		DE_UNREF(workGroupID);
		DE_UNREF(localInvocationID);
		return workGroupSize;
	}
};

class WorkGroupIDCase : public ComputeBuiltinVarCase
{
public:
	WorkGroupIDCase (Context& context)
		: ComputeBuiltinVarCase(context, "work_group_id", "gl_WorkGroupID", TYPE_UINT_VEC3)
	{
		m_subCases.push_back(SubCase(UVec3(1,1,1), UVec3(1,1,1)));
		m_subCases.push_back(SubCase(UVec3(1,1,1), UVec3(52,1,1)));
		m_subCases.push_back(SubCase(UVec3(1,1,1), UVec3(1,39,1)));
		m_subCases.push_back(SubCase(UVec3(1,1,1), UVec3(1,1,78)));
		m_subCases.push_back(SubCase(UVec3(1,1,1), UVec3(4,7,11)));
		m_subCases.push_back(SubCase(UVec3(2,3,4), UVec3(4,7,11)));
	}

	UVec3 computeReference (const UVec3& numWorkGroups, const UVec3& workGroupSize, const UVec3& workGroupID, const UVec3& localInvocationID) const
	{
		DE_UNREF(numWorkGroups);
		DE_UNREF(workGroupSize);
		DE_UNREF(localInvocationID);
		return workGroupID;
	}
};

class LocalInvocationIDCase : public ComputeBuiltinVarCase
{
public:
	LocalInvocationIDCase (Context& context)
		: ComputeBuiltinVarCase(context, "local_invocation_id", "gl_LocalInvocationID", TYPE_UINT_VEC3)
	{
		m_subCases.push_back(SubCase(UVec3(1,1,1), UVec3(1,1,1)));
		m_subCases.push_back(SubCase(UVec3(1,1,1), UVec3(2,7,3)));
		m_subCases.push_back(SubCase(UVec3(2,1,1), UVec3(1,1,1)));
		m_subCases.push_back(SubCase(UVec3(2,1,1), UVec3(1,3,5)));
		m_subCases.push_back(SubCase(UVec3(1,3,1), UVec3(1,1,1)));
		m_subCases.push_back(SubCase(UVec3(1,1,7), UVec3(1,1,1)));
		m_subCases.push_back(SubCase(UVec3(1,1,7), UVec3(3,3,1)));
		m_subCases.push_back(SubCase(UVec3(11,3,8), UVec3(1,1,1)));
		m_subCases.push_back(SubCase(UVec3(11,3,8), UVec3(3,1,2)));
	}

	UVec3 computeReference (const UVec3& numWorkGroups, const UVec3& workGroupSize, const UVec3& workGroupID, const UVec3& localInvocationID) const
	{
		DE_UNREF(numWorkGroups);
		DE_UNREF(workGroupSize);
		DE_UNREF(workGroupID);
		return localInvocationID;
	}
};

class GlobalInvocationIDCase : public ComputeBuiltinVarCase
{
public:
	GlobalInvocationIDCase (Context& context)
		: ComputeBuiltinVarCase(context, "global_invocation_id", "gl_GlobalInvocationID", TYPE_UINT_VEC3)
	{
		m_subCases.push_back(SubCase(UVec3(1,1,1), UVec3(1,1,1)));
		m_subCases.push_back(SubCase(UVec3(1,1,1), UVec3(52,1,1)));
		m_subCases.push_back(SubCase(UVec3(1,1,1), UVec3(1,39,1)));
		m_subCases.push_back(SubCase(UVec3(1,1,1), UVec3(1,1,78)));
		m_subCases.push_back(SubCase(UVec3(1,1,1), UVec3(4,7,11)));
		m_subCases.push_back(SubCase(UVec3(2,3,4), UVec3(4,7,11)));
		m_subCases.push_back(SubCase(UVec3(11,3,8), UVec3(1,1,1)));
		m_subCases.push_back(SubCase(UVec3(11,3,8), UVec3(3,1,2)));
	}

	UVec3 computeReference (const UVec3& numWorkGroups, const UVec3& workGroupSize, const UVec3& workGroupID, const UVec3& localInvocationID) const
	{
		DE_UNREF(numWorkGroups);
		return workGroupID * workGroupSize + localInvocationID;
	}
};

class LocalInvocationIndexCase : public ComputeBuiltinVarCase
{
public:
	LocalInvocationIndexCase (Context& context)
		: ComputeBuiltinVarCase(context, "local_invocation_index", "gl_LocalInvocationIndex", TYPE_UINT)
	{
		m_subCases.push_back(SubCase(UVec3(1,1,1), UVec3(1,1,1)));
		m_subCases.push_back(SubCase(UVec3(1,1,1), UVec3(1,39,1)));
		m_subCases.push_back(SubCase(UVec3(1,1,1), UVec3(4,7,11)));
		m_subCases.push_back(SubCase(UVec3(2,3,4), UVec3(4,7,11)));
		m_subCases.push_back(SubCase(UVec3(11,3,8), UVec3(1,1,1)));
		m_subCases.push_back(SubCase(UVec3(11,3,8), UVec3(3,1,2)));
	}

	UVec3 computeReference (const UVec3& numWorkGroups, const UVec3& workGroupSize, const UVec3& workGroupID, const UVec3& localInvocationID) const
	{
		DE_UNREF(workGroupID);
		DE_UNREF(numWorkGroups);
		return UVec3(localInvocationID.z()*workGroupSize.x()*workGroupSize.y() + localInvocationID.y()*workGroupSize.x() + localInvocationID.x(), 0, 0);
	}
};

ComputeShaderBuiltinVarTests::ComputeShaderBuiltinVarTests (Context& context)
	: TestCaseGroup(context, "compute", "Compute Shader Builtin Variables")
{
}

ComputeShaderBuiltinVarTests::~ComputeShaderBuiltinVarTests (void)
{
}

void ComputeShaderBuiltinVarTests::init (void)
{
	addChild(new NumWorkGroupsCase			(m_context));
	addChild(new WorkGroupSizeCase			(m_context));
	addChild(new WorkGroupIDCase			(m_context));
	addChild(new LocalInvocationIDCase		(m_context));
	addChild(new GlobalInvocationIDCase		(m_context));
	addChild(new LocalInvocationIndexCase	(m_context));
}

} // Functional
} // gles31
} // deqp