• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*-------------------------------------------------------------------------
2  * drawElements Quality Program OpenGL ES 2.0 Module
3  * -------------------------------------------------
4  *
5  * Copyright 2014 The Android Open Source Project
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  *//*!
20  * \file
21  * \brief Polygon offset tests.
22  *//*--------------------------------------------------------------------*/
23 
24 #include "es2fPolygonOffsetTests.hpp"
25 #include "deStringUtil.hpp"
26 #include "deRandom.hpp"
27 #include "gluContextInfo.hpp"
28 #include "gluRenderContext.hpp"
29 #include "gluShaderProgram.hpp"
30 #include "gluPixelTransfer.hpp"
31 #include "gluStrUtil.hpp"
32 #include "glwEnums.hpp"
33 #include "glwDefs.hpp"
34 #include "glwFunctions.hpp"
35 #include "tcuTestContext.hpp"
36 #include "tcuTestLog.hpp"
37 #include "tcuTextureUtil.hpp"
38 #include "tcuRenderTarget.hpp"
39 #include "tcuVectorUtil.hpp"
40 #include "rrRenderer.hpp"
41 #include "rrFragmentOperations.hpp"
42 
43 #include "sglrReferenceContext.hpp"
44 
45 #include <string>
46 #include <limits>
47 
48 using namespace glw; // GLint and other GL types
49 
50 namespace deqp
51 {
52 namespace gles2
53 {
54 namespace Functional
55 {
56 namespace
57 {
58 
59 const char* s_shaderSourceVertex	= "attribute highp vec4 a_position;\n"
60 									  "attribute highp vec4 a_color;\n"
61 									  "varying mediump vec4 v_color;\n"
62 									  "void main (void)\n"
63 									  "{\n"
64 									  "	gl_Position = a_position;\n"
65 									  "	v_color = a_color;\n"
66 									  "}\n";
67 const char* s_shaderSourceFragment	= "varying mediump vec4 v_color;\n"
68 									  "void main (void)\n"
69 									  "{\n"
70 									  "	gl_FragColor = v_color;\n"
71 									  "}\n";
72 
73 static const tcu::Vec4	MASK_COLOR_OK	= tcu::Vec4(0.0f, 0.1f, 0.0f, 1.0f);
74 static const tcu::Vec4	MASK_COLOR_DEV	= tcu::Vec4(0.8f, 0.5f, 0.0f, 1.0f);
75 static const tcu::Vec4	MASK_COLOR_FAIL	= tcu::Vec4(1.0f, 0.0f, 1.0f, 1.0f);
76 
compareThreshold(const tcu::IVec4 & a,const tcu::IVec4 & b,const tcu::IVec4 & threshold)77 inline bool compareThreshold (const tcu::IVec4& a, const tcu::IVec4& b, const tcu::IVec4& threshold)
78 {
79 	return tcu::boolAll(tcu::lessThanEqual(tcu::abs(a - b), threshold));
80 }
81 
82 /*--------------------------------------------------------------------*//*!
83 * \brief Pixelwise comparison of two images.
84 * \note copied & modified from glsRasterizationTests
85 *
86 * Kernel radius defines maximum allowed distance. If radius is 0, only
87 * perfect match is allowed. Radius of 1 gives a 3x3 kernel.
88 *
89 * Return values: -1 = Perfect match
90 * 0 = Deviation within kernel
91 * >0 = Number of faulty pixels
92 *//*--------------------------------------------------------------------*/
compareImages(tcu::TestLog & log,glu::RenderContext & renderCtx,const tcu::ConstPixelBufferAccess & test,const tcu::ConstPixelBufferAccess & ref,const tcu::PixelBufferAccess & diffMask,int radius)93 int compareImages (tcu::TestLog& log, glu::RenderContext& renderCtx, const tcu::ConstPixelBufferAccess& test, const tcu::ConstPixelBufferAccess& ref, const tcu::PixelBufferAccess& diffMask, int radius)
94 {
95 	const int			height			= test.getHeight();
96 	const int			width			= test.getWidth();
97 	const int			colorThreshold	= 128;
98 	const tcu::RGBA		formatThreshold	= renderCtx.getRenderTarget().getPixelFormat().getColorThreshold();
99 	const tcu::IVec4	threshold		= tcu::IVec4(colorThreshold, colorThreshold, colorThreshold, formatThreshold.getAlpha() > 0 ? colorThreshold : 0)
100 										+ tcu::IVec4(formatThreshold.getRed(), formatThreshold.getGreen(), formatThreshold.getBlue(), formatThreshold.getAlpha());
101 
102 	int			deviatingPixels = 0;
103 	int			faultyPixels	= 0;
104 	int			compareFailed	= -1;
105 
106 	tcu::clear(diffMask, MASK_COLOR_OK);
107 
108 	for (int y = 0; y < height; y++)
109 	{
110 		for (int x = 0; x < width; x++)
111 		{
112 			const tcu::IVec4 cRef = ref.getPixelInt(x, y);
113 
114 			// Pixelwise match, no deviation or fault
115 			{
116 				const tcu::IVec4 cTest = test.getPixelInt(x, y);
117 				if (compareThreshold(cRef, cTest, threshold))
118 					continue;
119 			}
120 
121 			// If not, search within kernel radius
122 			{
123 				const int kYmin = deMax32(y - radius, 0);
124 				const int kYmax = deMin32(y + radius, height-1);
125 				const int kXmin = deMax32(x - radius, 0);
126 				const int kXmax = deMin32(x + radius, width-1);
127 				bool found = false;
128 
129 				for (int kY = kYmin; kY <= kYmax; kY++)
130 				for (int kX = kXmin; kX <= kXmax; kX++)
131 				{
132 					const tcu::IVec4 cTest = test.getPixelInt(kX, kY);
133 					if (compareThreshold(cRef, cTest, threshold))
134 						found = true;
135 				}
136 
137 				if (found)	// The pixel is deviating if the color is found inside the kernel
138 				{
139 					diffMask.setPixel(MASK_COLOR_DEV, x, y);
140 					if (compareFailed == -1)
141 						compareFailed = 0;
142 					deviatingPixels++;
143 					continue;
144 				}
145 			}
146 
147 			diffMask.setPixel(MASK_COLOR_FAIL, x, y);
148 			faultyPixels++;										// The pixel is faulty if the color is not found
149 			compareFailed = 1;
150 		}
151 	}
152 
153 	log << tcu::TestLog::Message << faultyPixels << " faulty pixel(s) found." << tcu::TestLog::EndMessage;
154 
155 	return (compareFailed == 1 ? faultyPixels : compareFailed);
156 }
157 
verifyImages(tcu::TestLog & log,tcu::TestContext & testCtx,glu::RenderContext & renderCtx,const tcu::ConstPixelBufferAccess & testImage,const tcu::ConstPixelBufferAccess & referenceImage)158 void verifyImages (tcu::TestLog& log, tcu::TestContext& testCtx, glu::RenderContext& renderCtx, const tcu::ConstPixelBufferAccess& testImage, const tcu::ConstPixelBufferAccess& referenceImage)
159 {
160 	using tcu::TestLog;
161 
162 	const int			kernelRadius		= 1;
163 	const int			faultyPixelLimit	= 20;
164 	int					faultyPixels;
165 	tcu::Surface		diffMask			(testImage.getWidth(), testImage.getHeight());
166 
167 	faultyPixels = compareImages(log, renderCtx, referenceImage, testImage, diffMask.getAccess(), kernelRadius);
168 
169 	if (faultyPixels > faultyPixelLimit)
170 	{
171 		log << TestLog::ImageSet("Images", "Image comparison");
172 		log << TestLog::Image("Test image", "Test image", testImage);
173 		log << TestLog::Image("Reference image", "Reference image", referenceImage);
174 		log << TestLog::Image("Difference mask", "Difference mask", diffMask.getAccess());
175 		log << TestLog::EndImageSet;
176 
177 		log << tcu::TestLog::Message << "Got " << faultyPixels << " faulty pixel(s)." << tcu::TestLog::EndMessage;
178 		testCtx.setTestResult(QP_TEST_RESULT_FAIL, "Got faulty pixels");
179 	}
180 }
181 
verifyError(tcu::TestContext & testCtx,const glw::Functions & gl,GLenum expected)182 void verifyError (tcu::TestContext& testCtx, const glw::Functions& gl, GLenum expected)
183 {
184 	deUint32 got = gl.getError();
185 	if (got != expected)
186 	{
187 		testCtx.getLog() << tcu::TestLog::Message << "// ERROR: expected " << glu::getErrorStr(expected) << "; got " << glu::getErrorStr(got) << tcu::TestLog::EndMessage;
188 		if (testCtx.getTestResult() == QP_TEST_RESULT_PASS)
189 			testCtx.setTestResult(QP_TEST_RESULT_FAIL, "Got invalid error");
190 	}
191 }
192 
checkCanvasSize(int width,int height,int minWidth,int minHeight)193 void checkCanvasSize (int width, int height, int minWidth, int minHeight)
194 {
195 	if (width < minWidth || height < minHeight)
196 		throw tcu::NotSupportedError(std::string("Render context size must be at least ") + de::toString(minWidth) + "x" + de::toString(minWidth));
197 }
198 
199 class PositionColorShader : public sglr::ShaderProgram
200 {
201 public:
202 	enum
203 	{
204 		VARYINGLOC_COLOR = 0
205 	};
206 
207 			PositionColorShader (void);
208 	void	shadeVertices		(const rr::VertexAttrib* inputs, rr::VertexPacket* const* packets, const int numPackets) const;
209 	void	shadeFragments		(rr::FragmentPacket* packets, const int numPackets, const rr::FragmentShadingContext& context) const;
210 };
211 
PositionColorShader(void)212 PositionColorShader::PositionColorShader (void)
213 	: sglr::ShaderProgram(sglr::pdec::ShaderProgramDeclaration()
214 							<< sglr::pdec::VertexAttribute("a_position", rr::GENERICVECTYPE_FLOAT)
215 							<< sglr::pdec::VertexAttribute("a_color", rr::GENERICVECTYPE_FLOAT)
216 							<< sglr::pdec::VertexToFragmentVarying(rr::GENERICVECTYPE_FLOAT)
217 							<< sglr::pdec::FragmentOutput(rr::GENERICVECTYPE_FLOAT)
218 							<< sglr::pdec::VertexSource(s_shaderSourceVertex)
219 							<< sglr::pdec::FragmentSource(s_shaderSourceFragment))
220 {
221 }
222 
shadeVertices(const rr::VertexAttrib * inputs,rr::VertexPacket * const * packets,const int numPackets) const223 void PositionColorShader::shadeVertices (const rr::VertexAttrib* inputs, rr::VertexPacket* const* packets, const int numPackets) const
224 {
225 	for (int packetNdx = 0; packetNdx < numPackets; ++packetNdx)
226 	{
227 		const int positionAttrLoc = 0;
228 		const int colorAttrLoc = 1;
229 
230 		rr::VertexPacket& packet = *packets[packetNdx];
231 
232 		// Transform to position
233 		packet.position = rr::readVertexAttribFloat(inputs[positionAttrLoc], packet.instanceNdx, packet.vertexNdx);
234 
235 		// Pass color to FS
236 		packet.outputs[VARYINGLOC_COLOR] = rr::readVertexAttribFloat(inputs[colorAttrLoc], packet.instanceNdx, packet.vertexNdx);
237 	}
238 }
239 
shadeFragments(rr::FragmentPacket * packets,const int numPackets,const rr::FragmentShadingContext & context) const240 void PositionColorShader::shadeFragments (rr::FragmentPacket* packets, const int numPackets, const rr::FragmentShadingContext& context) const
241 {
242 	for (int packetNdx = 0; packetNdx < numPackets; ++packetNdx)
243 	{
244 		rr::FragmentPacket& packet = packets[packetNdx];
245 
246 		for (int fragNdx = 0; fragNdx < 4; ++fragNdx)
247 			rr::writeFragmentOutput(context, packetNdx, fragNdx, 0, rr::readTriangleVarying<float>(packet, context, VARYINGLOC_COLOR, fragNdx));
248 	}
249 }
250 
251 // PolygonOffsetTestCase
252 
253 class PolygonOffsetTestCase : public TestCase
254 {
255 public:
256 					PolygonOffsetTestCase	(Context& context, const char* name, const char* description, GLenum internalFormat, const char* internalFormatName, int canvasSize);
257 
258 	virtual void	testPolygonOffset		(void) = DE_NULL;
259 	IterateResult	iterate					(void);
260 
261 protected:
262 	const GLenum	m_internalFormat;
263 	const char*		m_internalFormatName;
264 	const int		m_targetSize;
265 };
266 
PolygonOffsetTestCase(Context & context,const char * name,const char * description,GLenum internalFormat,const char * internalFormatName,int canvasSize)267 PolygonOffsetTestCase::PolygonOffsetTestCase (Context& context, const char* name, const char* description, GLenum internalFormat, const char* internalFormatName, int canvasSize)
268 	: TestCase				(context, name, description)
269 	, m_internalFormat		(internalFormat)
270 	, m_internalFormatName	(internalFormatName)
271 	, m_targetSize			(canvasSize)
272 {
273 }
274 
iterate(void)275 PolygonOffsetTestCase::IterateResult PolygonOffsetTestCase::iterate (void)
276 {
277 	m_testCtx.setTestResult(QP_TEST_RESULT_PASS, "Pass");
278 	m_testCtx.getLog() << tcu::TestLog::Message << "Testing PolygonOffset with " << m_internalFormatName << " depth buffer." << tcu::TestLog::EndMessage;
279 
280 	if (m_internalFormat == 0)
281 	{
282 		// default framebuffer
283 		const int width		= m_context.getRenderTarget().getWidth();
284 		const int height	= m_context.getRenderTarget().getHeight();
285 
286 		checkCanvasSize(width, height, m_targetSize, m_targetSize);
287 
288 		if (m_context.getRenderTarget().getDepthBits() == 0)
289 			throw tcu::NotSupportedError("polygon offset tests require depth buffer");
290 
291 		testPolygonOffset();
292 	}
293 	else
294 	{
295 		const glw::Functions& gl = m_context.getRenderContext().getFunctions();
296 
297 		// framebuffer object
298 		GLuint	colorRboId	= 0;
299 		GLuint	depthRboId	= 0;
300 		GLuint	fboId		= 0;
301 		bool	fboComplete;
302 
303 		gl.genRenderbuffers(1, &colorRboId);
304 		gl.bindRenderbuffer(GL_RENDERBUFFER, colorRboId);
305 		gl.renderbufferStorage(GL_RENDERBUFFER, GL_RGBA4, m_targetSize, m_targetSize);
306 		verifyError(m_testCtx, gl, GL_NO_ERROR);
307 
308 		gl.genRenderbuffers(1, &depthRboId);
309 		gl.bindRenderbuffer(GL_RENDERBUFFER, depthRboId);
310 		gl.renderbufferStorage(GL_RENDERBUFFER, m_internalFormat, m_targetSize, m_targetSize);
311 		verifyError(m_testCtx, gl, GL_NO_ERROR);
312 
313 		gl.genFramebuffers(1, &fboId);
314 		gl.bindFramebuffer(GL_FRAMEBUFFER, fboId);
315 		gl.framebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, colorRboId);
316 		gl.framebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,	GL_RENDERBUFFER, depthRboId);
317 		verifyError(m_testCtx, gl, GL_NO_ERROR);
318 
319 		fboComplete = gl.checkFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE;
320 
321 		if (fboComplete)
322 			testPolygonOffset();
323 
324 		gl.deleteFramebuffers(1, &fboId);
325 		gl.deleteRenderbuffers(1, &depthRboId);
326 		gl.deleteRenderbuffers(1, &colorRboId);
327 
328 		if (!fboComplete)
329 			throw tcu::NotSupportedError("could not create fbo for testing.");
330 	}
331 
332 	return STOP;
333 }
334 
335 // UsageTestCase
336 
337 class UsageTestCase : public PolygonOffsetTestCase
338 {
339 public:
340 			UsageTestCase		(Context& context, const char* name, const char* description, GLenum internalFormat, const char* internalFormatName);
341 
342 	void	testPolygonOffset	(void);
343 };
344 
UsageTestCase(Context & context,const char * name,const char * description,GLenum internalFormat,const char * internalFormatName)345 UsageTestCase::UsageTestCase (Context& context, const char* name, const char* description, GLenum internalFormat, const char* internalFormatName)
346 	: PolygonOffsetTestCase(context, name, description, internalFormat, internalFormatName, 200)
347 {
348 }
349 
testPolygonOffset(void)350 void UsageTestCase::testPolygonOffset (void)
351 {
352 	using tcu::TestLog;
353 
354 	const tcu::Vec4 triangle[] =
355 	{
356 		tcu::Vec4(-1,  1,  0,  1),
357 		tcu::Vec4( 1,  1,  0,  1),
358 		tcu::Vec4( 1, -1,  0,  1),
359 	};
360 
361 	tcu::TestLog&		log				= m_testCtx.getLog();
362 	tcu::Surface		testImage		(m_targetSize, m_targetSize);
363 	tcu::Surface		referenceImage	(m_targetSize, m_targetSize);
364 	int					subpixelBits	= 0;
365 
366 	// render test image
367 	{
368 		const glw::Functions&		gl			= m_context.getRenderContext().getFunctions();
369 		const glu::ShaderProgram	program		(m_context.getRenderContext(), glu::makeVtxFragSources(s_shaderSourceVertex, s_shaderSourceFragment));
370 		const GLint					positionLoc = gl.getAttribLocation(program.getProgram(), "a_position");
371 		const GLint					colorLoc	= gl.getAttribLocation(program.getProgram(), "a_color");
372 
373 		if (!program.isOk())
374 		{
375 			log << program;
376 			TCU_FAIL("Shader compile failed.");
377 		}
378 
379 		gl.clearColor				(0, 0, 0, 1);
380 		gl.clearDepthf				(1.0f);
381 		gl.clear					(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
382 		gl.viewport					(0, 0, m_targetSize, m_targetSize);
383 		gl.useProgram				(program.getProgram());
384 		gl.enable					(GL_DEPTH_TEST);
385 		gl.depthFunc				(GL_LEQUAL);	// make test pass if polygon offset doesn't do anything. It has its own test case. This test is only for to detect always-on cases.
386 
387 		log << TestLog::Message << "DepthFunc = GL_LEQUAL" << TestLog::EndMessage;
388 
389 		gl.enableVertexAttribArray	(positionLoc);
390 		gl.vertexAttribPointer		(positionLoc, 4, GL_FLOAT, GL_FALSE, 0, triangle);
391 
392 		//draw back (offset disabled)
393 
394 		log << TestLog::Message << "Draw bottom-right. Color = White.\tState: PolygonOffset(0, -2), POLYGON_OFFSET_FILL disabled." << TestLog::EndMessage;
395 
396 		gl.polygonOffset			(0, -2);
397 		gl.disable					(GL_POLYGON_OFFSET_FILL);
398 		gl.vertexAttrib4f			(colorLoc, 1.0f, 1.0f, 1.0f, 1.0f);
399 		gl.drawArrays				(GL_TRIANGLES, 0, 3);
400 
401 		//draw front
402 
403 		log << TestLog::Message << "Draw bottom-right. Color = Red.\tState: PolygonOffset(0, -1), POLYGON_OFFSET_FILL enabled." << TestLog::EndMessage;
404 
405 		gl.polygonOffset			(0, -1);
406 		gl.enable					(GL_POLYGON_OFFSET_FILL);
407 		gl.vertexAttrib4f			(colorLoc, 1.0f, 0.0f, 0.0f, 1.0f);
408 		gl.drawArrays				(GL_TRIANGLES, 0, 3);
409 
410 		gl.disableVertexAttribArray	(positionLoc);
411 		gl.useProgram				(0);
412 		gl.finish					();
413 
414 		glu::readPixels(m_context.getRenderContext(), 0, 0, testImage.getAccess());
415 
416 		gl.getIntegerv(GL_SUBPIXEL_BITS, &subpixelBits);
417 	}
418 
419 	// render reference image
420 	{
421 		rr::Renderer		referenceRenderer;
422 		rr::VertexAttrib	attribs[2];
423 		rr::RenderState		state((rr::ViewportState)(rr::WindowRectangle(0, 0, m_targetSize, m_targetSize)), subpixelBits);
424 
425 		PositionColorShader program;
426 
427 		attribs[0].type				= rr::VERTEXATTRIBTYPE_FLOAT;
428 		attribs[0].size				= 4;
429 		attribs[0].stride			= 0;
430 		attribs[0].instanceDivisor	= 0;
431 		attribs[0].pointer			= triangle;
432 
433 		attribs[1].type				= rr::VERTEXATTRIBTYPE_DONT_CARE;
434 		attribs[1].generic			= tcu::Vec4(1.0f, 0.0f, 0.0f, 1.0f);
435 
436 		tcu::clear(referenceImage.getAccess(), tcu::Vec4(0.0f, 0.0f, 0.0f, 1.0f));
437 
438 		log << TestLog::Message << "Expecting: Bottom-right = Red." << TestLog::EndMessage;
439 
440 		referenceRenderer.draw(
441 			rr::DrawCommand(
442 				state,
443 				rr::RenderTarget(rr::MultisamplePixelBufferAccess::fromSinglesampleAccess(referenceImage.getAccess())),
444 				rr::Program(program.getVertexShader(), program.getFragmentShader()),
445 				2,
446 				attribs,
447 				rr::PrimitiveList(rr::PRIMITIVETYPE_TRIANGLES, 3, 0)));
448 	}
449 
450 	// compare
451 	verifyImages(log, m_testCtx, m_context.getRenderContext(), testImage.getAccess(), referenceImage.getAccess());
452 }
453 
454 // UsageDisplacementTestCase
455 
456 class UsageDisplacementTestCase : public PolygonOffsetTestCase
457 {
458 public:
459 				UsageDisplacementTestCase	(Context& context, const char* name, const char* description, GLenum internalFormat, const char* internalFormatName);
460 
461 private:
462 	tcu::Vec4	genRandomVec4				(de::Random& rnd) const;
463 	void		testPolygonOffset			(void);
464 };
465 
UsageDisplacementTestCase(Context & context,const char * name,const char * description,GLenum internalFormat,const char * internalFormatName)466 UsageDisplacementTestCase::UsageDisplacementTestCase (Context& context, const char* name, const char* description, GLenum internalFormat, const char* internalFormatName)
467 	: PolygonOffsetTestCase(context, name, description, internalFormat, internalFormatName, 200)
468 {
469 }
470 
genRandomVec4(de::Random & rnd) const471 tcu::Vec4 UsageDisplacementTestCase::genRandomVec4 (de::Random& rnd) const
472 {
473 	// generater triangle endpoint with following properties
474 	//	1) it will not be clipped
475 	//	2) it is not near either far or near plane to prevent possible problems related to depth clamping
476 	// => w >= 1.0 and z in (-0.9, 0.9) range
477 	tcu::Vec4 retVal;
478 
479 	retVal.x() = rnd.getFloat(-1, 1);
480 	retVal.y() = rnd.getFloat(-1, 1);
481 	retVal.z() = 0.5f;
482 	retVal.w() = 1.0f + rnd.getFloat();
483 
484 	return retVal;
485 }
486 
testPolygonOffset(void)487 void UsageDisplacementTestCase::testPolygonOffset (void)
488 {
489 	using tcu::TestLog;
490 
491 	de::Random			rnd				(0xdec0de);
492 	tcu::TestLog&		log				= m_testCtx.getLog();
493 	tcu::Surface		testImage		(m_targetSize, m_targetSize);
494 	tcu::Surface		referenceImage	(m_targetSize, m_targetSize);
495 
496 	// render test image
497 	{
498 		const glw::Functions&		gl				= m_context.getRenderContext().getFunctions();
499 		const glu::ShaderProgram	program			(m_context.getRenderContext(), glu::makeVtxFragSources(s_shaderSourceVertex, s_shaderSourceFragment));
500 		const GLint					positionLoc		= gl.getAttribLocation(program.getProgram(), "a_position");
501 		const GLint					colorLoc		= gl.getAttribLocation(program.getProgram(), "a_color");
502 		const int					numIterations	= 40;
503 
504 		if (!program.isOk())
505 		{
506 			log << program;
507 			TCU_FAIL("Shader compile failed.");
508 		}
509 
510 		gl.clearColor				(0, 0, 0, 1);
511 		gl.clearDepthf				(1.0f);
512 		gl.clear					(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
513 		gl.viewport					(0, 0, m_targetSize, m_targetSize);
514 		gl.useProgram				(program.getProgram());
515 		gl.enable					(GL_DEPTH_TEST);
516 		gl.enable					(GL_POLYGON_OFFSET_FILL);
517 		gl.enableVertexAttribArray	(positionLoc);
518 		gl.vertexAttrib4f			(colorLoc, 0.0f, 1.0f, 0.0f, 1.0f);
519 
520 		log << TestLog::Message << "Framebuffer cleared, clear color = Black." << TestLog::EndMessage;
521 		log << TestLog::Message << "POLYGON_OFFSET_FILL enabled." << TestLog::EndMessage;
522 
523 		// draw colorless (mask = 0,0,0) triangle at random* location, set offset and render green triangle with depthfunc = equal
524 		// *) w >= 1.0 and z in (-1, 1) range
525 		for (int iterationNdx = 0; iterationNdx < numIterations; ++iterationNdx)
526 		{
527 			const bool		offsetDirection = rnd.getBool();
528 			const float		offset = offsetDirection ? -1.0f : 1.0f;
529 			tcu::Vec4		triangle[3];
530 
531 			for (int vertexNdx = 0; vertexNdx < DE_LENGTH_OF_ARRAY(triangle); ++vertexNdx)
532 				triangle[vertexNdx] = genRandomVec4(rnd);
533 
534 			gl.vertexAttribPointer		(positionLoc, 4, GL_FLOAT, GL_FALSE, 0, triangle);
535 
536 			log << TestLog::Message << "Setup triangle with random coordinates:" << TestLog::EndMessage;
537 			for (size_t ndx = 0; ndx < DE_LENGTH_OF_ARRAY(triangle); ++ndx)
538 				log << TestLog::Message
539 						<< "\tx=" << triangle[ndx].x()
540 						<< "\ty=" << triangle[ndx].y()
541 						<< "\tz=" << triangle[ndx].z()
542 						<< "\tw=" << triangle[ndx].w()
543 						<< TestLog::EndMessage;
544 
545 			log << TestLog::Message << "Draw colorless triangle.\tState: DepthFunc = GL_ALWAYS, PolygonOffset(0, 0)." << TestLog::EndMessage;
546 
547 			gl.depthFunc				(GL_ALWAYS);
548 			gl.polygonOffset			(0, 0);
549 			gl.colorMask				(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
550 			gl.drawArrays				(GL_TRIANGLES, 0, 3);
551 
552 			// all fragments should have different Z => DepthFunc == GL_EQUAL fails with every fragment
553 
554 			log << TestLog::Message << "Draw green triangle.\tState: DepthFunc = GL_EQUAL, PolygonOffset(0, " << offset << ")." << TestLog::EndMessage;
555 
556 			gl.depthFunc				(GL_EQUAL);
557 			gl.polygonOffset			(0, offset);
558 			gl.colorMask				(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
559 			gl.drawArrays				(GL_TRIANGLES, 0, 3);
560 
561 			log << TestLog::Message << TestLog::EndMessage; // empty line for clarity
562 		}
563 
564 		gl.disableVertexAttribArray	(positionLoc);
565 		gl.useProgram				(0);
566 		gl.finish					();
567 
568 		glu::readPixels(m_context.getRenderContext(), 0, 0, testImage.getAccess());
569 	}
570 
571 	// render reference image
572 	log << TestLog::Message << "Expecting black framebuffer." << TestLog::EndMessage;
573 	tcu::clear(referenceImage.getAccess(), tcu::Vec4(0.0f, 0.0f, 0.0f, 1.0f));
574 
575 	// compare
576 	verifyImages(log, m_testCtx, m_context.getRenderContext(), testImage.getAccess(), referenceImage.getAccess());
577 }
578 
579 // UsagePositiveNegativeTestCase
580 
581 class UsagePositiveNegativeTestCase : public PolygonOffsetTestCase
582 {
583 public:
584 			UsagePositiveNegativeTestCase	(Context& context, const char* name, const char* description, GLenum internalFormat, const char* internalFormatName);
585 
586 	void	testPolygonOffset				(void);
587 };
588 
UsagePositiveNegativeTestCase(Context & context,const char * name,const char * description,GLenum internalFormat,const char * internalFormatName)589 UsagePositiveNegativeTestCase::UsagePositiveNegativeTestCase (Context& context, const char* name, const char* description, GLenum internalFormat, const char* internalFormatName)
590 	: PolygonOffsetTestCase(context, name, description, internalFormat, internalFormatName, 200)
591 {
592 }
593 
testPolygonOffset(void)594 void UsagePositiveNegativeTestCase::testPolygonOffset (void)
595 {
596 	using tcu::TestLog;
597 
598 	const tcu::Vec4 triangleBottomRight[] =
599 	{
600 		tcu::Vec4(-1,  1,  0,  1),
601 		tcu::Vec4( 1,  1,  0,  1),
602 		tcu::Vec4( 1, -1,  0,  1),
603 	};
604 	const tcu::Vec4 triangleTopLeft[] =
605 	{
606 		tcu::Vec4(-1, -1,  0,  1),
607 		tcu::Vec4(-1,  1,  0,  1),
608 		tcu::Vec4( 1, -1,  0,  1),
609 	};
610 
611 	tcu::TestLog&		log				= m_testCtx.getLog();
612 	tcu::Surface		testImage		(m_targetSize, m_targetSize);
613 	tcu::Surface		referenceImage	(m_targetSize, m_targetSize);
614 	int					subpixelBits	= 0;
615 
616 	// render test image
617 	{
618 		const glw::Functions&		gl				= m_context.getRenderContext().getFunctions();
619 		const glu::ShaderProgram	program			(m_context.getRenderContext(), glu::makeVtxFragSources(s_shaderSourceVertex, s_shaderSourceFragment));
620 		const GLint					positionLoc		= gl.getAttribLocation(program.getProgram(), "a_position");
621 		const GLint					colorLoc		= gl.getAttribLocation(program.getProgram(), "a_color");
622 
623 		if (!program.isOk())
624 		{
625 			log << program;
626 			TCU_FAIL("Shader compile failed.");
627 		}
628 
629 		gl.clearColor				(0, 0, 0, 1);
630 		gl.clearDepthf				(1.0f);
631 		gl.clear					(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
632 		gl.viewport					(0, 0, m_targetSize, m_targetSize);
633 		gl.depthFunc				(GL_LESS);
634 		gl.useProgram				(program.getProgram());
635 		gl.enable					(GL_DEPTH_TEST);
636 		gl.enable					(GL_POLYGON_OFFSET_FILL);
637 		gl.enableVertexAttribArray	(positionLoc);
638 
639 		log << TestLog::Message << "DepthFunc = GL_LESS." << TestLog::EndMessage;
640 		log << TestLog::Message << "POLYGON_OFFSET_FILL enabled." << TestLog::EndMessage;
641 
642 		//draw top left (negative offset test)
643 		{
644 			gl.vertexAttribPointer		(positionLoc, 4, GL_FLOAT, GL_FALSE, 0, triangleTopLeft);
645 
646 			log << TestLog::Message << "Draw top-left. Color = White.\tState: PolygonOffset(0, 0)." << TestLog::EndMessage;
647 
648 			gl.polygonOffset			(0, 0);
649 			gl.vertexAttrib4f			(colorLoc, 1.0f, 1.0f, 1.0f, 1.0f);
650 			gl.drawArrays				(GL_TRIANGLES, 0, 3);
651 
652 			log << TestLog::Message << "Draw top-left. Color = Green.\tState: PolygonOffset(0, -1)." << TestLog::EndMessage;
653 
654 			gl.polygonOffset			(0, -1);
655 			gl.vertexAttrib4f			(colorLoc, 0.0f, 1.0f, 0.0f, 1.0f);
656 			gl.drawArrays				(GL_TRIANGLES, 0, 3);
657 		}
658 
659 		//draw bottom right (positive offset test)
660 		{
661 			gl.vertexAttribPointer		(positionLoc, 4, GL_FLOAT, GL_FALSE, 0, triangleBottomRight);
662 
663 			log << TestLog::Message << "Draw bottom-right. Color = White.\tState: PolygonOffset(0, 1)." << TestLog::EndMessage;
664 
665 			gl.polygonOffset			(0, 1);
666 			gl.vertexAttrib4f			(colorLoc, 1.0f, 1.0f, 1.0f, 1.0f);
667 			gl.drawArrays				(GL_TRIANGLES, 0, 3);
668 
669 			log << TestLog::Message << "Draw bottom-right. Color = Yellow.\tState: PolygonOffset(0, 0)." << TestLog::EndMessage;
670 
671 			gl.polygonOffset			(0, 0);
672 			gl.vertexAttrib4f			(colorLoc, 1.0f, 1.0f, 0.0f, 1.0f);
673 			gl.drawArrays				(GL_TRIANGLES, 0, 3);
674 		}
675 
676 		gl.disableVertexAttribArray	(positionLoc);
677 		gl.useProgram				(0);
678 		gl.finish					();
679 
680 		glu::readPixels(m_context.getRenderContext(), 0, 0, testImage.getAccess());
681 
682 		gl.getIntegerv(GL_SUBPIXEL_BITS, &subpixelBits);
683 	}
684 
685 	// render reference image
686 	{
687 		rr::Renderer		referenceRenderer;
688 		rr::VertexAttrib	attribs[2];
689 		rr::RenderState		state((rr::ViewportState)(rr::WindowRectangle(0, 0, m_targetSize, m_targetSize)), subpixelBits);
690 
691 		PositionColorShader program;
692 
693 		attribs[0].type				= rr::VERTEXATTRIBTYPE_FLOAT;
694 		attribs[0].size				= 4;
695 		attribs[0].stride			= 0;
696 		attribs[0].instanceDivisor	= 0;
697 		attribs[0].pointer			= triangleTopLeft;
698 
699 		attribs[1].type				= rr::VERTEXATTRIBTYPE_DONT_CARE;
700 		attribs[1].generic			= tcu::Vec4(0.0f, 1.0f, 0.0f, 1.0f);
701 
702 		tcu::clear(referenceImage.getAccess(), tcu::Vec4(0.0f, 0.0f, 0.0f, 1.0f));
703 
704 		log << TestLog::Message << "Expecting: Top-left = Green, Bottom-right = Yellow." << TestLog::EndMessage;
705 
706 		referenceRenderer.draw(
707 			rr::DrawCommand(
708 				state,
709 				rr::RenderTarget(rr::MultisamplePixelBufferAccess::fromSinglesampleAccess(referenceImage.getAccess())),
710 				rr::Program(program.getVertexShader(), program.getFragmentShader()),
711 				2,
712 				attribs,
713 				rr::PrimitiveList(rr::PRIMITIVETYPE_TRIANGLES, 3, 0)));
714 
715 		attribs[0].pointer = triangleBottomRight;
716 		attribs[1].generic = tcu::Vec4(1.0f, 1.0f, 0.0f, 1.0f);
717 
718 		referenceRenderer.draw(
719 			rr::DrawCommand(
720 				state,
721 				rr::RenderTarget(rr::MultisamplePixelBufferAccess::fromSinglesampleAccess(referenceImage.getAccess())),
722 				rr::Program(program.getVertexShader(), program.getFragmentShader()),
723 				2,
724 				attribs,
725 				rr::PrimitiveList(rr::PRIMITIVETYPE_TRIANGLES, 3, 0)));
726 	}
727 
728 	// compare
729 	verifyImages(log, m_testCtx, m_context.getRenderContext(), testImage.getAccess(), referenceImage.getAccess());
730 }
731 
732 // ResultClampingTestCase
733 
734 class ResultClampingTestCase : public PolygonOffsetTestCase
735 {
736 public:
737 			ResultClampingTestCase	(Context& context, const char* name, const char* description, GLenum internalFormat, const char* internalFormatName);
738 
739 	void	testPolygonOffset		(void);
740 };
741 
ResultClampingTestCase(Context & context,const char * name,const char * description,GLenum internalFormat,const char * internalFormatName)742 ResultClampingTestCase::ResultClampingTestCase (Context& context, const char* name, const char* description, GLenum internalFormat, const char* internalFormatName)
743 	: PolygonOffsetTestCase(context, name, description, internalFormat, internalFormatName, 200)
744 {
745 }
746 
testPolygonOffset(void)747 void ResultClampingTestCase::testPolygonOffset (void)
748 {
749 	using tcu::TestLog;
750 
751 	const tcu::Vec4 triangleBottomRight[] =
752 	{
753 		tcu::Vec4(-1,  1,  1,  1),
754 		tcu::Vec4( 1,  1,  1,  1),
755 		tcu::Vec4( 1, -1,  1,  1),
756 	};
757 	const tcu::Vec4 triangleTopLeft[] =
758 	{
759 		tcu::Vec4(-1, -1, -1,  1),
760 		tcu::Vec4(-1,  1, -1,  1),
761 		tcu::Vec4( 1, -1, -1,  1),
762 	};
763 
764 	tcu::TestLog&		log				= m_testCtx.getLog();
765 	tcu::Surface		testImage		(m_targetSize, m_targetSize);
766 	tcu::Surface		referenceImage	(m_targetSize, m_targetSize);
767 
768 	// render test image
769 	{
770 		const glw::Functions&		gl			= m_context.getRenderContext().getFunctions();
771 		const glu::ShaderProgram	program		(m_context.getRenderContext(), glu::makeVtxFragSources(s_shaderSourceVertex, s_shaderSourceFragment));
772 		const GLint					positionLoc = gl.getAttribLocation(program.getProgram(), "a_position");
773 		const GLint					colorLoc	= gl.getAttribLocation(program.getProgram(), "a_color");
774 
775 		if (!program.isOk())
776 		{
777 			log << program;
778 			TCU_FAIL("Shader compile failed.");
779 		}
780 
781 		gl.clearColor				(0, 0, 0, 1);
782 		gl.clearDepthf				(1.0f);
783 		gl.clear					(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
784 		gl.viewport					(0, 0, m_targetSize, m_targetSize);
785 		gl.useProgram				(program.getProgram());
786 		gl.enable					(GL_DEPTH_TEST);
787 		gl.enable					(GL_POLYGON_OFFSET_FILL);
788 		gl.enableVertexAttribArray	(positionLoc);
789 
790 		log << TestLog::Message << "POLYGON_OFFSET_FILL enabled." << TestLog::EndMessage;
791 
792 		//draw bottom right (far)
793 		{
794 			gl.vertexAttribPointer		(positionLoc, 4, GL_FLOAT, GL_FALSE, 0, triangleBottomRight);
795 
796 			log << TestLog::Message << "Draw bottom-right. Color = White.\tState: DepthFunc = ALWAYS, PolygonOffset(0, 8), Polygon Z = 1.0. (Result depth should clamp to 1.0)." << TestLog::EndMessage;
797 
798 			gl.depthFunc				(GL_ALWAYS);
799 			gl.polygonOffset			(0, 8);
800 			gl.vertexAttrib4f			(colorLoc, 1.0f, 1.0f, 1.0f, 1.0f);
801 			gl.drawArrays				(GL_TRIANGLES, 0, 3);
802 
803 			log << TestLog::Message << "Draw bottom-right. Color = Red.\tState: DepthFunc = GREATER, PolygonOffset(0, 9), Polygon Z = 1.0. (Result depth should clamp to 1.0 too)" << TestLog::EndMessage;
804 
805 			gl.depthFunc				(GL_GREATER);
806 			gl.polygonOffset			(0, 9);
807 			gl.vertexAttrib4f			(colorLoc, 1.0f, 0.0f, 0.0f, 1.0f);
808 			gl.drawArrays				(GL_TRIANGLES, 0, 3);
809 		}
810 
811 		//draw top left (near)
812 		{
813 			gl.vertexAttribPointer		(positionLoc, 4, GL_FLOAT, GL_FALSE, 0, triangleTopLeft);
814 
815 			log << TestLog::Message << "Draw top-left. Color = White.\tState: DepthFunc = ALWAYS, PolygonOffset(0, -8), Polygon Z = -1.0. (Result depth should clamp to -1.0)" << TestLog::EndMessage;
816 
817 			gl.depthFunc				(GL_ALWAYS);
818 			gl.polygonOffset			(0, -8);
819 			gl.vertexAttrib4f			(colorLoc, 1.0f, 1.0f, 1.0f, 1.0f);
820 			gl.drawArrays				(GL_TRIANGLES, 0, 3);
821 
822 			log << TestLog::Message << "Draw top-left. Color = Yellow.\tState: DepthFunc = LESS, PolygonOffset(0, -9), Polygon Z = -1.0. (Result depth should clamp to -1.0 too)." << TestLog::EndMessage;
823 
824 			gl.depthFunc				(GL_LESS);
825 			gl.polygonOffset			(0, -9);
826 			gl.vertexAttrib4f			(colorLoc, 1.0f, 1.0f, 0.0f, 1.0f);
827 			gl.drawArrays				(GL_TRIANGLES, 0, 3);
828 		}
829 
830 		gl.disableVertexAttribArray	(positionLoc);
831 		gl.useProgram				(0);
832 		gl.finish					();
833 
834 		glu::readPixels(m_context.getRenderContext(), 0, 0, testImage.getAccess());
835 	}
836 
837 	// render reference image
838 	log << TestLog::Message << "Expecting: Top-left = White, Bottom-right = White." << TestLog::EndMessage;
839 	tcu::clear(referenceImage.getAccess(), tcu::Vec4(1.0f, 1.0f, 1.0f, 1.0f));
840 
841 	// compare
842 	verifyImages(log, m_testCtx, m_context.getRenderContext(), testImage.getAccess(), referenceImage.getAccess());
843 }
844 
845 // UsageSlopeTestCase
846 
847 class UsageSlopeTestCase  : public PolygonOffsetTestCase
848 {
849 public:
850 			UsageSlopeTestCase	(Context& context, const char* name, const char* description, GLenum internalFormat, const char* internalFormatName);
851 
852 	void	testPolygonOffset	(void);
853 };
854 
UsageSlopeTestCase(Context & context,const char * name,const char * description,GLenum internalFormat,const char * internalFormatName)855 UsageSlopeTestCase::UsageSlopeTestCase (Context& context, const char* name, const char* description, GLenum internalFormat, const char* internalFormatName)
856 	: PolygonOffsetTestCase(context, name, description, internalFormat, internalFormatName, 200)
857 {
858 }
859 
testPolygonOffset(void)860 void UsageSlopeTestCase::testPolygonOffset (void)
861 {
862 	using tcu::TestLog;
863 
864 	const tcu::Vec4 triangleBottomRight[] =
865 	{
866 		tcu::Vec4(-1,  1,  0.0f,  1),
867 		tcu::Vec4( 1,  1,  0.9f,  1),
868 		tcu::Vec4( 1, -1,  0.9f,  1),
869 	};
870 	const tcu::Vec4 triangleTopLeft[] =
871 	{
872 		tcu::Vec4(-1, -1,  -0.9f,  1),
873 		tcu::Vec4(-1,  1,   0.9f,  1),
874 		tcu::Vec4( 1, -1,   0.0f,  1),
875 	};
876 
877 	tcu::TestLog&		log				= m_testCtx.getLog();
878 	tcu::Surface		testImage		(m_targetSize, m_targetSize);
879 	tcu::Surface		referenceImage	(m_targetSize, m_targetSize);
880 
881 	// render test image
882 	{
883 		const glw::Functions&		gl			= m_context.getRenderContext().getFunctions();
884 		const glu::ShaderProgram	program		(m_context.getRenderContext(), glu::makeVtxFragSources(s_shaderSourceVertex, s_shaderSourceFragment));
885 		const GLint					positionLoc = gl.getAttribLocation(program.getProgram(), "a_position");
886 		const GLint					colorLoc	= gl.getAttribLocation(program.getProgram(), "a_color");
887 
888 		if (!program.isOk())
889 		{
890 			log << program;
891 			TCU_FAIL("Shader compile failed.");
892 		}
893 
894 		gl.clearColor				(0, 0, 0, 1);
895 		gl.clearDepthf				(1.0f);
896 		gl.clear					(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
897 		gl.viewport					(0, 0, m_targetSize, m_targetSize);
898 		gl.useProgram				(program.getProgram());
899 		gl.enable					(GL_DEPTH_TEST);
900 		gl.enable					(GL_POLYGON_OFFSET_FILL);
901 		gl.enableVertexAttribArray	(positionLoc);
902 
903 		log << TestLog::Message << "POLYGON_OFFSET_FILL enabled." << TestLog::EndMessage;
904 
905 		//draw top left (negative offset test)
906 		{
907 			gl.vertexAttribPointer		(positionLoc, 4, GL_FLOAT, GL_FALSE, 0, triangleTopLeft);
908 
909 			log << TestLog::Message << "Draw top-left. Color = White.\tState: DepthFunc = ALWAYS, PolygonOffset(0, 0)." << TestLog::EndMessage;
910 
911 			gl.depthFunc				(GL_ALWAYS);
912 			gl.polygonOffset			(0, 0);
913 			gl.vertexAttrib4f			(colorLoc, 1.0f, 1.0f, 1.0f, 1.0f);
914 			gl.drawArrays				(GL_TRIANGLES, 0, 3);
915 
916 			log << TestLog::Message << "Draw top-left. Color = Green.\tState: DepthFunc = LESS, PolygonOffset(-1, 0)." << TestLog::EndMessage;
917 
918 			gl.depthFunc				(GL_LESS);
919 			gl.polygonOffset			(-1, 0);
920 			gl.vertexAttrib4f			(colorLoc, 0.0f, 1.0f, 0.0f, 1.0f);
921 			gl.drawArrays				(GL_TRIANGLES, 0, 3);
922 		}
923 
924 		//draw bottom right (positive offset test)
925 		{
926 			gl.vertexAttribPointer		(positionLoc, 4, GL_FLOAT, GL_FALSE, 0, triangleBottomRight);
927 
928 			log << TestLog::Message << "Draw bottom-right. Color = White.\tState: DepthFunc = ALWAYS, PolygonOffset(0, 0)." << TestLog::EndMessage;
929 
930 			gl.depthFunc				(GL_ALWAYS);
931 			gl.polygonOffset			(0, 0);
932 			gl.vertexAttrib4f			(colorLoc, 1.0f, 1.0f, 1.0f, 1.0f);
933 			gl.drawArrays				(GL_TRIANGLES, 0, 3);
934 
935 			log << TestLog::Message << "Draw bottom-right. Color = Green.\tState: DepthFunc = GREATER, PolygonOffset(1, 0)." << TestLog::EndMessage;
936 
937 			gl.depthFunc				(GL_GREATER);
938 			gl.polygonOffset			(1, 0);
939 			gl.vertexAttrib4f			(colorLoc, 0.0f, 1.0f, 0.0f, 1.0f);
940 			gl.drawArrays				(GL_TRIANGLES, 0, 3);
941 		}
942 
943 		gl.disableVertexAttribArray	(positionLoc);
944 		gl.useProgram				(0);
945 		gl.finish					();
946 
947 		glu::readPixels(m_context.getRenderContext(), 0, 0, testImage.getAccess());
948 	}
949 
950 	// render reference image
951 	log << TestLog::Message << "Expecting: Top-left = Green, Bottom-right = Green." << TestLog::EndMessage;
952 	tcu::clear(referenceImage.getAccess(), tcu::Vec4(0.0f, 1.0f, 0.0f, 1.0f));
953 
954 	// compare
955 	verifyImages(log, m_testCtx, m_context.getRenderContext(), testImage.getAccess(), referenceImage.getAccess());
956 }
957 
958 // ZeroSlopeTestCase
959 
960 class ZeroSlopeTestCase : public PolygonOffsetTestCase
961 {
962 public:
963 			ZeroSlopeTestCase	(Context& context, const char* name, const char* description, GLenum internalFormat, const char* internalFormatName);
964 
965 	void	testPolygonOffset	(void);
966 };
967 
ZeroSlopeTestCase(Context & context,const char * name,const char * description,GLenum internalFormat,const char * internalFormatName)968 ZeroSlopeTestCase::ZeroSlopeTestCase (Context& context, const char* name, const char* description, GLenum internalFormat, const char* internalFormatName)
969 	: PolygonOffsetTestCase(context, name, description, internalFormat, internalFormatName, 200)
970 {
971 }
972 
testPolygonOffset(void)973 void ZeroSlopeTestCase::testPolygonOffset (void)
974 {
975 	using tcu::TestLog;
976 
977 	const tcu::Vec4 triangle[] =
978 	{
979 		tcu::Vec4(-0.4f,  0.4f, 0.0f, 1.0f),
980 		tcu::Vec4(-0.8f, -0.5f, 0.0f, 1.0f),
981 		tcu::Vec4( 0.7f,  0.2f, 0.0f, 1.0f),
982 	};
983 
984 	tcu::TestLog&		log				= m_testCtx.getLog();
985 	tcu::Surface		testImage		(m_targetSize, m_targetSize);
986 	tcu::Surface		referenceImage	(m_targetSize, m_targetSize);
987 
988 	// log the triangle
989 	log << TestLog::Message << "Setup triangle with coordinates:" << TestLog::EndMessage;
990 	for (size_t ndx = 0; ndx < DE_LENGTH_OF_ARRAY(triangle); ++ndx)
991 		log << TestLog::Message
992 				<< "\tx=" << triangle[ndx].x()
993 				<< "\ty=" << triangle[ndx].y()
994 				<< "\tz=" << triangle[ndx].z()
995 				<< "\tw=" << triangle[ndx].w()
996 				<< TestLog::EndMessage;
997 
998 	// render test image
999 	{
1000 		const glw::Functions&		gl			= m_context.getRenderContext().getFunctions();
1001 		const glu::ShaderProgram	program		(m_context.getRenderContext(), glu::makeVtxFragSources(s_shaderSourceVertex, s_shaderSourceFragment));
1002 		const GLint					positionLoc = gl.getAttribLocation(program.getProgram(), "a_position");
1003 		const GLint					colorLoc	= gl.getAttribLocation(program.getProgram(), "a_color");
1004 
1005 		if (!program.isOk())
1006 		{
1007 			log << program;
1008 			TCU_FAIL("Shader compile failed.");
1009 		}
1010 
1011 		gl.clearColor				(0, 0, 0, 1);
1012 		gl.clearDepthf				(1.0f);
1013 		gl.clear					(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
1014 		gl.viewport					(0, 0, m_targetSize, m_targetSize);
1015 		gl.useProgram				(program.getProgram());
1016 		gl.enable					(GL_DEPTH_TEST);
1017 		gl.enable					(GL_POLYGON_OFFSET_FILL);
1018 		gl.enableVertexAttribArray	(positionLoc);
1019 
1020 		log << TestLog::Message << "POLYGON_OFFSET_FILL enabled." << TestLog::EndMessage;
1021 
1022 		{
1023 			gl.vertexAttribPointer		(positionLoc, 4, GL_FLOAT, GL_FALSE, 0, triangle);
1024 
1025 			log << TestLog::Message << "Draw triangle. Color = Red.\tState: DepthFunc = ALWAYS, PolygonOffset(0, 0)." << TestLog::EndMessage;
1026 
1027 			gl.depthFunc				(GL_ALWAYS);
1028 			gl.polygonOffset			(0, 0);
1029 			gl.vertexAttrib4f			(colorLoc, 1.0f, 0.0f, 0.0f, 1.0f);
1030 			gl.drawArrays				(GL_TRIANGLES, 0, 3);
1031 
1032 			log << TestLog::Message << "Draw triangle. Color = Black.\tState: DepthFunc = EQUAL, PolygonOffset(4, 0)." << TestLog::EndMessage;
1033 
1034 			gl.depthFunc				(GL_EQUAL);
1035 			gl.polygonOffset			(4, 0);	// triangle slope == 0
1036 			gl.vertexAttrib4f			(colorLoc, 0.0f, 0.0f, 0.0f, 1.0f);
1037 			gl.drawArrays				(GL_TRIANGLES, 0, 3);
1038 		}
1039 
1040 		gl.disableVertexAttribArray	(positionLoc);
1041 		gl.useProgram				(0);
1042 		gl.finish					();
1043 
1044 		glu::readPixels(m_context.getRenderContext(), 0, 0, testImage.getAccess());
1045 	}
1046 
1047 	// render reference image
1048 	log << TestLog::Message << "Expecting black triangle." << TestLog::EndMessage;
1049 	tcu::clear(referenceImage.getAccess(), tcu::Vec4(0.0f, 0.0f, 0.0f, 1.0f));
1050 
1051 	// compare
1052 	verifyImages(log, m_testCtx, m_context.getRenderContext(), testImage.getAccess(), referenceImage.getAccess());
1053 }
1054 
1055 // OneSlopeTestCase
1056 
1057 class OneSlopeTestCase : public PolygonOffsetTestCase
1058 {
1059 public:
1060 			OneSlopeTestCase	(Context& context, const char* name, const char* description, GLenum internalFormat, const char* internalFormatName);
1061 
1062 	void	testPolygonOffset	(void);
1063 };
1064 
OneSlopeTestCase(Context & context,const char * name,const char * description,GLenum internalFormat,const char * internalFormatName)1065 OneSlopeTestCase::OneSlopeTestCase (Context& context, const char* name, const char* description, GLenum internalFormat, const char* internalFormatName)
1066 	: PolygonOffsetTestCase(context, name, description, internalFormat, internalFormatName, 200)
1067 {
1068 }
1069 
testPolygonOffset(void)1070 void OneSlopeTestCase::testPolygonOffset (void)
1071 {
1072 	using tcu::TestLog;
1073 
1074 	/*
1075 		* setup vertices subject to following properties
1076 		*   dz_w / dx_w == 1
1077 		*   dz_w / dy_w == 0
1078 		* or
1079 		*   dz_w / dx_w == 0
1080 		*   dz_w / dy_w == 1
1081 		* ==> m == 1
1082 		*/
1083 	const float cornerDepth = float(m_targetSize);
1084 	const tcu::Vec4 triangles[2][3] =
1085 	{
1086 		{
1087 			tcu::Vec4(-1, -1, -cornerDepth, 1),
1088 			tcu::Vec4(-1,  1, -cornerDepth, 1),
1089 			tcu::Vec4( 1, -1,  cornerDepth, 1),
1090 		},
1091 		{
1092 			tcu::Vec4(-1,  1,  cornerDepth, 1),
1093 			tcu::Vec4( 1,  1,  cornerDepth, 1),
1094 			tcu::Vec4( 1, -1, -cornerDepth, 1),
1095 		},
1096 	};
1097 
1098 	tcu::TestLog&		log				= m_testCtx.getLog();
1099 	tcu::Surface		testImage		(m_targetSize, m_targetSize);
1100 	tcu::Surface		referenceImage	(m_targetSize, m_targetSize);
1101 
1102 	// log triangle info
1103 	log << TestLog::Message << "Setup triangle0 coordinates: (slope in window coordinates = 1.0)" << TestLog::EndMessage;
1104 	for (size_t ndx = 0; ndx < DE_LENGTH_OF_ARRAY(triangles[0]); ++ndx)
1105 		log << TestLog::Message
1106 				<< "\tx=" << triangles[0][ndx].x()
1107 				<< "\ty=" << triangles[0][ndx].y()
1108 				<< "\tz=" << triangles[0][ndx].z()
1109 				<< "\tw=" << triangles[0][ndx].w()
1110 				<< TestLog::EndMessage;
1111 	log << TestLog::Message << "Setup triangle1 coordinates: (slope in window coordinates = 1.0)" << TestLog::EndMessage;
1112 	for (size_t ndx = 0; ndx < DE_LENGTH_OF_ARRAY(triangles[1]); ++ndx)
1113 		log << TestLog::Message
1114 				<< "\tx=" << triangles[1][ndx].x()
1115 				<< "\ty=" << triangles[1][ndx].y()
1116 				<< "\tz=" << triangles[1][ndx].z()
1117 				<< "\tw=" << triangles[1][ndx].w()
1118 				<< TestLog::EndMessage;
1119 
1120 	// render test image
1121 	{
1122 		const glw::Functions&		gl			= m_context.getRenderContext().getFunctions();
1123 		const glu::ShaderProgram	program		(m_context.getRenderContext(), glu::makeVtxFragSources(s_shaderSourceVertex, s_shaderSourceFragment));
1124 		const GLint					positionLoc = gl.getAttribLocation(program.getProgram(), "a_position");
1125 		const GLint					colorLoc	= gl.getAttribLocation(program.getProgram(), "a_color");
1126 
1127 		if (!program.isOk())
1128 		{
1129 			log << program;
1130 			TCU_FAIL("Shader compile failed.");
1131 		}
1132 
1133 		gl.clearColor				(0, 0, 0, 1);
1134 		gl.clear					(GL_COLOR_BUFFER_BIT);
1135 		gl.viewport					(0, 0, m_targetSize, m_targetSize);
1136 		gl.useProgram				(program.getProgram());
1137 		gl.enable					(GL_DEPTH_TEST);
1138 		gl.enable					(GL_POLYGON_OFFSET_FILL);
1139 		gl.enableVertexAttribArray	(positionLoc);
1140 
1141 		log << TestLog::Message << "Framebuffer cleared, clear color = Black." << TestLog::EndMessage;
1142 		log << TestLog::Message << "POLYGON_OFFSET_FILL enabled." << TestLog::EndMessage;
1143 
1144 		// top left (positive offset)
1145 		{
1146 			log << TestLog::Message << "Clear depth to 1.0." << TestLog::EndMessage;
1147 
1148 			gl.clearDepthf			(1.0f); // far
1149 			gl.clear				(GL_DEPTH_BUFFER_BIT);
1150 
1151 			gl.vertexAttribPointer	(positionLoc, 4, GL_FLOAT, GL_FALSE, 0, triangles[0]);
1152 
1153 			log << TestLog::Message << "Draw triangle0. Color = Red.\tState: DepthFunc = NOTEQUAL, PolygonOffset(10, 0). (Result depth should clamp to 1.0)." << TestLog::EndMessage;
1154 
1155 			gl.polygonOffset		(10, 0);		// clamps any depth on the triangle to 1
1156 			gl.depthFunc			(GL_NOTEQUAL);
1157 			gl.vertexAttrib4f		(colorLoc, 1.0f, 0.0f, 0.0f, 1.0f);
1158 			gl.drawArrays			(GL_TRIANGLES, 0, 3);
1159 		}
1160 		// bottom right (negative offset)
1161 		{
1162 			log << TestLog::Message << "Clear depth to 0.0." << TestLog::EndMessage;
1163 
1164 			gl.clearDepthf			(0.0f); // far
1165 			gl.clear				(GL_DEPTH_BUFFER_BIT);
1166 
1167 			gl.vertexAttribPointer	(positionLoc, 4, GL_FLOAT, GL_FALSE, 0, triangles[1]);
1168 
1169 			log << TestLog::Message << "Draw triangle1. Color = Green.\tState: DepthFunc = NOTEQUAL, PolygonOffset(-10, 0). (Result depth should clamp to 0.0)." << TestLog::EndMessage;
1170 
1171 			gl.polygonOffset		(-10, 0); // clamps depth to 0
1172 			gl.depthFunc			(GL_NOTEQUAL);
1173 			gl.vertexAttrib4f		(colorLoc, 0.0f, 1.0f, 0.0f, 1.0f);
1174 			gl.drawArrays			(GL_TRIANGLES, 0, 3);
1175 		}
1176 
1177 		gl.disableVertexAttribArray	(positionLoc);
1178 		gl.useProgram				(0);
1179 		gl.finish					();
1180 
1181 		glu::readPixels(m_context.getRenderContext(), 0, 0, testImage.getAccess());
1182 	}
1183 
1184 	// render reference image
1185 	log << TestLog::Message << "Expecting black framebuffer." << TestLog::EndMessage;
1186 	tcu::clear(referenceImage.getAccess(), tcu::Vec4(0.0f, 0.0f, 0.0f, 1.0f));
1187 
1188 	// compare
1189 	verifyImages(log, m_testCtx, m_context.getRenderContext(), testImage.getAccess(), referenceImage.getAccess());
1190 }
1191 
1192 } // anonymous
1193 
PolygonOffsetTests(Context & context)1194 PolygonOffsetTests::PolygonOffsetTests (Context& context)
1195 	: TestCaseGroup(context, "polygon_offset", "Polygon offset tests")
1196 {
1197 }
1198 
~PolygonOffsetTests(void)1199 PolygonOffsetTests::~PolygonOffsetTests (void)
1200 {
1201 }
1202 
init(void)1203 void PolygonOffsetTests::init (void)
1204 {
1205 	const struct DepthBufferFormat
1206 	{
1207 		enum BufferType
1208 		{
1209 			TYPE_FIXED_POINT,
1210 			TYPE_FLOATING_POINT,
1211 			TYPE_UNKNOWN
1212 		};
1213 
1214 		GLenum		internalFormat;
1215 		int			bits;
1216 		BufferType	floatingPoint;
1217 		const char* name;
1218 	} depthFormats[]=
1219 	{
1220 		{ 0,						0,		DepthBufferFormat::TYPE_UNKNOWN,		"default" },
1221 		{ GL_DEPTH_COMPONENT16,		16,		DepthBufferFormat::TYPE_FIXED_POINT,	"fixed16" },
1222 	};
1223 
1224 	for (int ndx = 0; ndx < DE_LENGTH_OF_ARRAY(depthFormats); ++ndx)
1225 	{
1226 		const DepthBufferFormat& format = depthFormats[ndx];
1227 
1228 		// enable works?
1229 		addChild(new UsageTestCase(m_context, (std::string(format.name) + "_enable").c_str(), "test enable GL_POLYGON_OFFSET_FILL", format.internalFormat, format.name));
1230 
1231 		// Really moves the polygons ?
1232 		addChild(new UsageDisplacementTestCase(m_context, (std::string(format.name) + "_displacement_with_units").c_str(), "test polygon offset", format.internalFormat, format.name));
1233 
1234 		// Really moves the polygons to right direction ?
1235 		addChild(new UsagePositiveNegativeTestCase(m_context, (std::string(format.name) + "_render_with_units").c_str(), "test polygon offset", format.internalFormat, format.name));
1236 
1237 		// Is total result clamped to [0,1] like promised?
1238 		addChild(new ResultClampingTestCase(m_context, (std::string(format.name) + "_result_depth_clamp").c_str(), "test polygon offset clamping", format.internalFormat, format.name));
1239 
1240 		// Slope really moves the polygon?
1241 		addChild(new UsageSlopeTestCase(m_context, (std::string(format.name) + "_render_with_factor").c_str(), "test polygon offset factor", format.internalFormat, format.name));
1242 
1243 		// Factor with zero slope
1244 		addChild(new ZeroSlopeTestCase(m_context, (std::string(format.name) + "_factor_0_slope").c_str(), "test polygon offset factor", format.internalFormat, format.name));
1245 
1246 		// Factor with 1.0 slope
1247 		addChild(new OneSlopeTestCase(m_context, (std::string(format.name) + "_factor_1_slope").c_str(), "test polygon offset factor", format.internalFormat, format.name));
1248 	}
1249 }
1250 
1251 } // Functional
1252 } // gles2
1253 } // deqp
1254