• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*-------------------------------------------------------------------------
2  * drawElements Quality Program OpenGL ES 3.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 "es3fPolygonOffsetTests.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 gles3
53 {
54 namespace Functional
55 {
56 namespace
57 {
58 
59 const char* s_shaderSourceVertex	= "#version 300 es\n"
60 									  "in highp vec4 a_position;\n"
61 									  "in highp vec4 a_color;\n"
62 									  "out highp vec4 v_color;\n"
63 									  "void main (void)\n"
64 									  "{\n"
65 									  "	gl_Position = a_position;\n"
66 									  "	v_color = a_color;\n"
67 									  "}\n";
68 const char* s_shaderSourceFragment	= "#version 300 es\n"
69 									  "in highp vec4 v_color;\n"
70 									  "layout(location = 0) out mediump vec4 fragColor;"
71 									  "void main (void)\n"
72 									  "{\n"
73 									  "	fragColor = v_color;\n"
74 									  "}\n";
75 
76 static const tcu::Vec4	MASK_COLOR_OK	= tcu::Vec4(0.0f, 0.1f, 0.0f, 1.0f);
77 static const tcu::Vec4	MASK_COLOR_DEV	= tcu::Vec4(0.8f, 0.5f, 0.0f, 1.0f);
78 static const tcu::Vec4	MASK_COLOR_FAIL	= tcu::Vec4(1.0f, 0.0f, 1.0f, 1.0f);
79 
compareThreshold(const tcu::IVec4 & a,const tcu::IVec4 & b,const tcu::IVec4 & threshold)80 inline bool compareThreshold (const tcu::IVec4& a, const tcu::IVec4& b, const tcu::IVec4& threshold)
81 {
82 	return tcu::boolAll(tcu::lessThanEqual(tcu::abs(a - b), threshold));
83 }
84 
85 /*--------------------------------------------------------------------*//*!
86 * \brief Pixelwise comparison of two images.
87 * \note copied & modified from glsRasterizationTests
88 *
89 * Kernel radius defines maximum allowed distance. If radius is 0, only
90 * perfect match is allowed. Radius of 1 gives a 3x3 kernel.
91 *
92 * Return values: -1 = Perfect match
93 * 0 = Deviation within kernel
94 * >0 = Number of faulty pixels
95 *//*--------------------------------------------------------------------*/
compareImages(tcu::TestLog & log,glu::RenderContext & renderCtx,const tcu::ConstPixelBufferAccess & test,const tcu::ConstPixelBufferAccess & ref,const tcu::PixelBufferAccess & diffMask,int radius)96 int compareImages (tcu::TestLog& log, glu::RenderContext& renderCtx, const tcu::ConstPixelBufferAccess& test, const tcu::ConstPixelBufferAccess& ref, const tcu::PixelBufferAccess& diffMask, int radius)
97 {
98 	const int			height			= test.getHeight();
99 	const int			width			= test.getWidth();
100 	const int			colorThreshold	= 128;
101 	const tcu::RGBA		formatThreshold	= renderCtx.getRenderTarget().getPixelFormat().getColorThreshold();
102 	const tcu::IVec4	threshold		= tcu::IVec4(colorThreshold, colorThreshold, colorThreshold, formatThreshold.getAlpha() > 0 ? colorThreshold : 0)
103 										+ tcu::IVec4(formatThreshold.getRed(), formatThreshold.getGreen(), formatThreshold.getBlue(), formatThreshold.getAlpha());
104 
105 	int			faultyPixels	= 0;
106 	int			compareFailed	= -1;
107 
108 	tcu::clear(diffMask, MASK_COLOR_OK);
109 
110 	for (int y = 0; y < height; y++)
111 	{
112 		for (int x = 0; x < width; x++)
113 		{
114 			const tcu::IVec4 cRef = ref.getPixelInt(x, y);
115 
116 			// Pixelwise match, no deviation or fault
117 			{
118 				const tcu::IVec4 cTest = test.getPixelInt(x, y);
119 				if (compareThreshold(cRef, cTest, threshold))
120 					continue;
121 			}
122 
123 			// If not, search within kernel radius
124 			{
125 				const int kYmin = deMax32(y - radius, 0);
126 				const int kYmax = deMin32(y + radius, height-1);
127 				const int kXmin = deMax32(x - radius, 0);
128 				const int kXmax = deMin32(x + radius, width-1);
129 				bool found = false;
130 
131 				for (int kY = kYmin; kY <= kYmax; kY++)
132 				for (int kX = kXmin; kX <= kXmax; kX++)
133 				{
134 					const tcu::IVec4 cTest = test.getPixelInt(kX, kY);
135 					if (compareThreshold(cRef, cTest, threshold))
136 						found = true;
137 				}
138 
139 				if (found)	// The pixel is deviating if the color is found inside the kernel
140 				{
141 					diffMask.setPixel(MASK_COLOR_DEV, x, y);
142 					if (compareFailed == -1)
143 						compareFailed = 0;
144 					continue;
145 				}
146 			}
147 
148 			diffMask.setPixel(MASK_COLOR_FAIL, x, y);
149 			faultyPixels++;										// The pixel is faulty if the color is not found
150 			compareFailed = 1;
151 		}
152 	}
153 
154 	log << tcu::TestLog::Message << faultyPixels << " faulty pixel(s) found." << tcu::TestLog::EndMessage;
155 
156 	return (compareFailed == 1 ? faultyPixels : compareFailed);
157 }
158 
verifyImages(tcu::TestLog & log,tcu::TestContext & testCtx,glu::RenderContext & renderCtx,const tcu::ConstPixelBufferAccess & testImage,const tcu::ConstPixelBufferAccess & referenceImage)159 void verifyImages (tcu::TestLog& log, tcu::TestContext& testCtx, glu::RenderContext& renderCtx, const tcu::ConstPixelBufferAccess& testImage, const tcu::ConstPixelBufferAccess& referenceImage)
160 {
161 	using tcu::TestLog;
162 
163 	const int			kernelRadius		= 1;
164 	const int			faultyPixelLimit	= 20;
165 	int					faultyPixels;
166 	tcu::Surface		diffMask			(testImage.getWidth(), testImage.getHeight());
167 
168 	faultyPixels = compareImages(log, renderCtx, referenceImage, testImage, diffMask.getAccess(), kernelRadius);
169 
170 	if (faultyPixels > faultyPixelLimit)
171 	{
172 		log << TestLog::ImageSet("Images", "Image comparison");
173 		log << TestLog::Image("Test image", "Test image", testImage);
174 		log << TestLog::Image("Reference image", "Reference image", referenceImage);
175 		log << TestLog::Image("Difference mask", "Difference mask", diffMask.getAccess());
176 		log << TestLog::EndImageSet;
177 
178 		log << tcu::TestLog::Message << "Got " << faultyPixels << " faulty pixel(s)." << tcu::TestLog::EndMessage;
179 		testCtx.setTestResult(QP_TEST_RESULT_FAIL, "Got faulty pixels");
180 	}
181 }
182 
verifyError(tcu::TestContext & testCtx,const glw::Functions & gl,GLenum expected)183 void verifyError (tcu::TestContext& testCtx, const glw::Functions& gl, GLenum expected)
184 {
185 	deUint32 got = gl.getError();
186 	if (got != expected)
187 	{
188 		testCtx.getLog() << tcu::TestLog::Message << "// ERROR: expected " << glu::getErrorStr(expected) << "; got " << glu::getErrorStr(got) << tcu::TestLog::EndMessage;
189 		if (testCtx.getTestResult() == QP_TEST_RESULT_PASS)
190 			testCtx.setTestResult(QP_TEST_RESULT_FAIL, "Got invalid error");
191 	}
192 }
193 
checkCanvasSize(int width,int height,int minWidth,int minHeight)194 void checkCanvasSize (int width, int height, int minWidth, int minHeight)
195 {
196 	if (width < minWidth || height < minHeight)
197 		throw tcu::NotSupportedError(std::string("Render context size must be at least ") + de::toString(minWidth) + "x" + de::toString(minWidth));
198 }
199 
200 class PositionColorShader : public sglr::ShaderProgram
201 {
202 public:
203 	enum
204 	{
205 		VARYINGLOC_COLOR = 0
206 	};
207 
208 			PositionColorShader (void);
209 	void	shadeVertices		(const rr::VertexAttrib* inputs, rr::VertexPacket* const* packets, const int numPackets) const;
210 	void	shadeFragments		(rr::FragmentPacket* packets, const int numPackets, const rr::FragmentShadingContext& context) const;
211 };
212 
PositionColorShader(void)213 PositionColorShader::PositionColorShader (void)
214 	: sglr::ShaderProgram(sglr::pdec::ShaderProgramDeclaration()
215 							<< sglr::pdec::VertexAttribute("a_position", rr::GENERICVECTYPE_FLOAT)
216 							<< sglr::pdec::VertexAttribute("a_color", rr::GENERICVECTYPE_FLOAT)
217 							<< sglr::pdec::VertexToFragmentVarying(rr::GENERICVECTYPE_FLOAT)
218 							<< sglr::pdec::FragmentOutput(rr::GENERICVECTYPE_FLOAT)
219 							<< sglr::pdec::VertexSource(s_shaderSourceVertex)
220 							<< sglr::pdec::FragmentSource(s_shaderSourceFragment))
221 {
222 }
223 
shadeVertices(const rr::VertexAttrib * inputs,rr::VertexPacket * const * packets,const int numPackets) const224 void PositionColorShader::shadeVertices (const rr::VertexAttrib* inputs, rr::VertexPacket* const* packets, const int numPackets) const
225 {
226 	for (int packetNdx = 0; packetNdx < numPackets; ++packetNdx)
227 	{
228 		const int positionAttrLoc = 0;
229 		const int colorAttrLoc = 1;
230 
231 		rr::VertexPacket& packet = *packets[packetNdx];
232 
233 		// Transform to position
234 		packet.position = rr::readVertexAttribFloat(inputs[positionAttrLoc], packet.instanceNdx, packet.vertexNdx);
235 
236 		// Pass color to FS
237 		packet.outputs[VARYINGLOC_COLOR] = rr::readVertexAttribFloat(inputs[colorAttrLoc], packet.instanceNdx, packet.vertexNdx);
238 	}
239 }
240 
shadeFragments(rr::FragmentPacket * packets,const int numPackets,const rr::FragmentShadingContext & context) const241 void PositionColorShader::shadeFragments (rr::FragmentPacket* packets, const int numPackets, const rr::FragmentShadingContext& context) const
242 {
243 	for (int packetNdx = 0; packetNdx < numPackets; ++packetNdx)
244 	{
245 		rr::FragmentPacket& packet = packets[packetNdx];
246 
247 		for (int fragNdx = 0; fragNdx < 4; ++fragNdx)
248 			rr::writeFragmentOutput(context, packetNdx, fragNdx, 0, rr::readTriangleVarying<float>(packet, context, VARYINGLOC_COLOR, fragNdx));
249 	}
250 }
251 
252 // PolygonOffsetTestCase
253 
254 class PolygonOffsetTestCase : public TestCase
255 {
256 public:
257 					PolygonOffsetTestCase	(Context& context, const char* name, const char* description, GLenum internalFormat, const char* internalFormatName, int canvasSize);
258 
259 	virtual void	testPolygonOffset		(void) = DE_NULL;
260 	IterateResult	iterate					(void);
261 
262 protected:
263 	const GLenum	m_internalFormat;
264 	const char*		m_internalFormatName;
265 	const int		m_targetSize;
266 };
267 
PolygonOffsetTestCase(Context & context,const char * name,const char * description,GLenum internalFormat,const char * internalFormatName,int canvasSize)268 PolygonOffsetTestCase::PolygonOffsetTestCase (Context& context, const char* name, const char* description, GLenum internalFormat, const char* internalFormatName, int canvasSize)
269 	: TestCase				(context, name, description)
270 	, m_internalFormat		(internalFormat)
271 	, m_internalFormatName	(internalFormatName)
272 	, m_targetSize			(canvasSize)
273 {
274 }
275 
iterate(void)276 PolygonOffsetTestCase::IterateResult PolygonOffsetTestCase::iterate (void)
277 {
278 	m_testCtx.setTestResult(QP_TEST_RESULT_PASS, "Pass");
279 	m_testCtx.getLog() << tcu::TestLog::Message << "Testing PolygonOffset with " << m_internalFormatName << " depth buffer." << tcu::TestLog::EndMessage;
280 
281 	if (m_internalFormat == 0)
282 	{
283 		// default framebuffer
284 		const int width		= m_context.getRenderTarget().getWidth();
285 		const int height	= m_context.getRenderTarget().getHeight();
286 
287 		checkCanvasSize(width, height, m_targetSize, m_targetSize);
288 
289 		if (m_context.getRenderTarget().getDepthBits() == 0)
290 			throw tcu::NotSupportedError("polygon offset tests require depth buffer");
291 
292 		testPolygonOffset();
293 	}
294 	else
295 	{
296 		const glw::Functions& gl = m_context.getRenderContext().getFunctions();
297 
298 		// framebuffer object
299 		GLuint	colorRboId	= 0;
300 		GLuint	depthRboId	= 0;
301 		GLuint	fboId		= 0;
302 		bool	fboComplete;
303 
304 		gl.genRenderbuffers(1, &colorRboId);
305 		gl.bindRenderbuffer(GL_RENDERBUFFER, colorRboId);
306 		gl.renderbufferStorage(GL_RENDERBUFFER, GL_RGBA4, m_targetSize, m_targetSize);
307 		verifyError(m_testCtx, gl, GL_NO_ERROR);
308 
309 		gl.genRenderbuffers(1, &depthRboId);
310 		gl.bindRenderbuffer(GL_RENDERBUFFER, depthRboId);
311 		gl.renderbufferStorage(GL_RENDERBUFFER, m_internalFormat, m_targetSize, m_targetSize);
312 		verifyError(m_testCtx, gl, GL_NO_ERROR);
313 
314 		gl.genFramebuffers(1, &fboId);
315 		gl.bindFramebuffer(GL_FRAMEBUFFER, fboId);
316 		gl.framebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, colorRboId);
317 		gl.framebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,	GL_RENDERBUFFER, depthRboId);
318 		verifyError(m_testCtx, gl, GL_NO_ERROR);
319 
320 		fboComplete = gl.checkFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE;
321 
322 		if (fboComplete)
323 			testPolygonOffset();
324 
325 		gl.deleteFramebuffers(1, &fboId);
326 		gl.deleteRenderbuffers(1, &depthRboId);
327 		gl.deleteRenderbuffers(1, &colorRboId);
328 
329 		if (!fboComplete)
330 			throw tcu::NotSupportedError("could not create fbo for testing.");
331 	}
332 
333 	return STOP;
334 }
335 
336 // UsageTestCase
337 
338 class UsageTestCase : public PolygonOffsetTestCase
339 {
340 public:
341 			UsageTestCase		(Context& context, const char* name, const char* description, GLenum internalFormat, const char* internalFormatName);
342 
343 	void	testPolygonOffset	(void);
344 };
345 
UsageTestCase(Context & context,const char * name,const char * description,GLenum internalFormat,const char * internalFormatName)346 UsageTestCase::UsageTestCase (Context& context, const char* name, const char* description, GLenum internalFormat, const char* internalFormatName)
347 	: PolygonOffsetTestCase(context, name, description, internalFormat, internalFormatName, 200)
348 {
349 }
350 
testPolygonOffset(void)351 void UsageTestCase::testPolygonOffset (void)
352 {
353 	using tcu::TestLog;
354 
355 	const tcu::Vec4 triangle[] =
356 	{
357 		tcu::Vec4(-1,  1,  0,  1),
358 		tcu::Vec4( 1,  1,  0,  1),
359 		tcu::Vec4( 1, -1,  0,  1),
360 	};
361 
362 	tcu::TestLog&		log				= m_testCtx.getLog();
363 	tcu::Surface		testImage		(m_targetSize, m_targetSize);
364 	tcu::Surface		referenceImage	(m_targetSize, m_targetSize);
365 	int					subpixelBits	= 0;
366 
367 	// render test image
368 	{
369 		const glw::Functions&		gl			= m_context.getRenderContext().getFunctions();
370 		const glu::ShaderProgram	program		(m_context.getRenderContext(), glu::makeVtxFragSources(s_shaderSourceVertex, s_shaderSourceFragment));
371 		const GLint					positionLoc = gl.getAttribLocation(program.getProgram(), "a_position");
372 		const GLint					colorLoc	= gl.getAttribLocation(program.getProgram(), "a_color");
373 
374 		if (!program.isOk())
375 		{
376 			log << program;
377 			TCU_FAIL("Shader compile failed.");
378 		}
379 
380 		gl.clearColor				(0, 0, 0, 1);
381 		gl.clearDepthf				(1.0f);
382 		gl.clear					(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
383 		gl.viewport					(0, 0, m_targetSize, m_targetSize);
384 		gl.useProgram				(program.getProgram());
385 		gl.enable					(GL_DEPTH_TEST);
386 		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.
387 
388 		log << TestLog::Message << "DepthFunc = GL_LEQUAL" << TestLog::EndMessage;
389 
390 		gl.enableVertexAttribArray	(positionLoc);
391 		gl.vertexAttribPointer		(positionLoc, 4, GL_FLOAT, GL_FALSE, 0, triangle);
392 
393 		//draw back (offset disabled)
394 
395 		log << TestLog::Message << "Draw bottom-right. Color = White.\tState: PolygonOffset(0, -2), POLYGON_OFFSET_FILL disabled." << TestLog::EndMessage;
396 
397 		gl.polygonOffset			(0, -2);
398 		gl.disable					(GL_POLYGON_OFFSET_FILL);
399 		gl.vertexAttrib4f			(colorLoc, 1.0f, 1.0f, 1.0f, 1.0f);
400 		gl.drawArrays				(GL_TRIANGLES, 0, 3);
401 
402 		//draw front
403 
404 		log << TestLog::Message << "Draw bottom-right. Color = Red.\tState: PolygonOffset(0, -1), POLYGON_OFFSET_FILL enabled." << TestLog::EndMessage;
405 
406 		gl.polygonOffset			(0, -1);
407 		gl.enable					(GL_POLYGON_OFFSET_FILL);
408 		gl.vertexAttrib4f			(colorLoc, 1.0f, 0.0f, 0.0f, 1.0f);
409 		gl.drawArrays				(GL_TRIANGLES, 0, 3);
410 
411 		gl.disableVertexAttribArray	(positionLoc);
412 		gl.useProgram				(0);
413 		gl.finish					();
414 
415 		glu::readPixels(m_context.getRenderContext(), 0, 0, testImage.getAccess());
416 
417 		gl.getIntegerv(GL_SUBPIXEL_BITS, &subpixelBits);
418 	}
419 
420 	// render reference image
421 	{
422 		rr::Renderer		referenceRenderer;
423 		rr::VertexAttrib	attribs[2];
424 		rr::RenderState		state((rr::ViewportState)(rr::WindowRectangle(0, 0, m_targetSize, m_targetSize)), subpixelBits);
425 
426 		PositionColorShader program;
427 
428 		attribs[0].type				= rr::VERTEXATTRIBTYPE_FLOAT;
429 		attribs[0].size				= 4;
430 		attribs[0].stride			= 0;
431 		attribs[0].instanceDivisor	= 0;
432 		attribs[0].pointer			= triangle;
433 
434 		attribs[1].type				= rr::VERTEXATTRIBTYPE_DONT_CARE;
435 		attribs[1].generic			= tcu::Vec4(1.0f, 0.0f, 0.0f, 1.0f);
436 
437 		tcu::clear(referenceImage.getAccess(), tcu::Vec4(0.0f, 0.0f, 0.0f, 1.0f));
438 
439 		log << TestLog::Message << "Expecting: Bottom-right = Red." << TestLog::EndMessage;
440 
441 		referenceRenderer.draw(
442 			rr::DrawCommand(
443 				state,
444 				rr::RenderTarget(rr::MultisamplePixelBufferAccess::fromSinglesampleAccess(referenceImage.getAccess())),
445 				rr::Program(program.getVertexShader(), program.getFragmentShader()),
446 				2,
447 				attribs,
448 				rr::PrimitiveList(rr::PRIMITIVETYPE_TRIANGLES, 3, 0)));
449 	}
450 
451 	// compare
452 	verifyImages(log, m_testCtx, m_context.getRenderContext(), testImage.getAccess(), referenceImage.getAccess());
453 }
454 
455 // UsageDisplacementTestCase
456 
457 class UsageDisplacementTestCase : public PolygonOffsetTestCase
458 {
459 public:
460 				UsageDisplacementTestCase	(Context& context, const char* name, const char* description, GLenum internalFormat, const char* internalFormatName);
461 
462 private:
463 	tcu::Vec4	genRandomVec4				(de::Random& rnd) const;
464 	void		testPolygonOffset			(void);
465 };
466 
UsageDisplacementTestCase(Context & context,const char * name,const char * description,GLenum internalFormat,const char * internalFormatName)467 UsageDisplacementTestCase::UsageDisplacementTestCase (Context& context, const char* name, const char* description, GLenum internalFormat, const char* internalFormatName)
468 	: PolygonOffsetTestCase(context, name, description, internalFormat, internalFormatName, 200)
469 {
470 }
471 
genRandomVec4(de::Random & rnd) const472 tcu::Vec4 UsageDisplacementTestCase::genRandomVec4 (de::Random& rnd) const
473 {
474 	// generater triangle endpoint with following properties
475 	//	1) it will not be clipped
476 	//	2) it is not near either far or near plane to prevent possible problems related to depth clamping
477 	// => w >= 1.0 and z in (-0.9, 0.9) range
478 	tcu::Vec4 retVal;
479 
480 	retVal.x() = rnd.getFloat(-1, 1);
481 	retVal.y() = rnd.getFloat(-1, 1);
482 	retVal.z() = 0.5f;
483 	retVal.w() = 1.0f + rnd.getFloat();
484 
485 	return retVal;
486 }
487 
testPolygonOffset(void)488 void UsageDisplacementTestCase::testPolygonOffset (void)
489 {
490 	using tcu::TestLog;
491 
492 	de::Random			rnd				(0xdec0de);
493 	tcu::TestLog&		log				= m_testCtx.getLog();
494 	tcu::Surface		testImage		(m_targetSize, m_targetSize);
495 	tcu::Surface		referenceImage	(m_targetSize, m_targetSize);
496 
497 	// render test image
498 	{
499 		const glw::Functions&		gl				= m_context.getRenderContext().getFunctions();
500 		const glu::ShaderProgram	program			(m_context.getRenderContext(), glu::makeVtxFragSources(s_shaderSourceVertex, s_shaderSourceFragment));
501 		const GLint					positionLoc		= gl.getAttribLocation(program.getProgram(), "a_position");
502 		const GLint					colorLoc		= gl.getAttribLocation(program.getProgram(), "a_color");
503 		const int					numIterations	= 40;
504 
505 		if (!program.isOk())
506 		{
507 			log << program;
508 			TCU_FAIL("Shader compile failed.");
509 		}
510 
511 		gl.clearColor				(0, 0, 0, 1);
512 		gl.clearDepthf				(1.0f);
513 		gl.clear					(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
514 		gl.viewport					(0, 0, m_targetSize, m_targetSize);
515 		gl.useProgram				(program.getProgram());
516 		gl.enable					(GL_DEPTH_TEST);
517 		gl.enable					(GL_POLYGON_OFFSET_FILL);
518 		gl.enableVertexAttribArray	(positionLoc);
519 		gl.vertexAttrib4f			(colorLoc, 0.0f, 1.0f, 0.0f, 1.0f);
520 
521 		log << TestLog::Message << "Framebuffer cleared, clear color = Black." << TestLog::EndMessage;
522 		log << TestLog::Message << "POLYGON_OFFSET_FILL enabled." << TestLog::EndMessage;
523 
524 		// draw colorless (mask = 0,0,0) triangle at random* location, set offset and render green triangle with depthfunc = equal
525 		// *) w >= 1.0 and z in (-1, 1) range
526 		for (int iterationNdx = 0; iterationNdx < numIterations; ++iterationNdx)
527 		{
528 			const bool		offsetDirection = rnd.getBool();
529 			const float		offset = offsetDirection ? -1.0f : 1.0f;
530 			tcu::Vec4		triangle[3];
531 
532 			for (int vertexNdx = 0; vertexNdx < DE_LENGTH_OF_ARRAY(triangle); ++vertexNdx)
533 				triangle[vertexNdx] = genRandomVec4(rnd);
534 
535 			gl.vertexAttribPointer		(positionLoc, 4, GL_FLOAT, GL_FALSE, 0, triangle);
536 
537 			log << TestLog::Message << "Setup triangle with random coordinates:" << TestLog::EndMessage;
538 			for (size_t ndx = 0; ndx < DE_LENGTH_OF_ARRAY(triangle); ++ndx)
539 				log << TestLog::Message
540 						<< "\tx=" << triangle[ndx].x()
541 						<< "\ty=" << triangle[ndx].y()
542 						<< "\tz=" << triangle[ndx].z()
543 						<< "\tw=" << triangle[ndx].w()
544 						<< TestLog::EndMessage;
545 
546 			log << TestLog::Message << "Draw colorless triangle.\tState: DepthFunc = GL_ALWAYS, PolygonOffset(0, 0)." << TestLog::EndMessage;
547 
548 			gl.depthFunc				(GL_ALWAYS);
549 			gl.polygonOffset			(0, 0);
550 			gl.colorMask				(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
551 			gl.drawArrays				(GL_TRIANGLES, 0, 3);
552 
553 			// all fragments should have different Z => DepthFunc == GL_EQUAL fails with every fragment
554 
555 			log << TestLog::Message << "Draw green triangle.\tState: DepthFunc = GL_EQUAL, PolygonOffset(0, " << offset << ")." << TestLog::EndMessage;
556 
557 			gl.depthFunc				(GL_EQUAL);
558 			gl.polygonOffset			(0, offset);
559 			gl.colorMask				(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
560 			gl.drawArrays				(GL_TRIANGLES, 0, 3);
561 
562 			log << TestLog::Message << TestLog::EndMessage; // empty line for clarity
563 		}
564 
565 		gl.disableVertexAttribArray	(positionLoc);
566 		gl.useProgram				(0);
567 		gl.finish					();
568 
569 		glu::readPixels(m_context.getRenderContext(), 0, 0, testImage.getAccess());
570 	}
571 
572 	// render reference image
573 	log << TestLog::Message << "Expecting black framebuffer." << TestLog::EndMessage;
574 	tcu::clear(referenceImage.getAccess(), tcu::Vec4(0.0f, 0.0f, 0.0f, 1.0f));
575 
576 	// compare
577 	verifyImages(log, m_testCtx, m_context.getRenderContext(), testImage.getAccess(), referenceImage.getAccess());
578 }
579 
580 // UsagePositiveNegativeTestCase
581 
582 class UsagePositiveNegativeTestCase : public PolygonOffsetTestCase
583 {
584 public:
585 			UsagePositiveNegativeTestCase	(Context& context, const char* name, const char* description, GLenum internalFormat, const char* internalFormatName);
586 
587 	void	testPolygonOffset				(void);
588 };
589 
UsagePositiveNegativeTestCase(Context & context,const char * name,const char * description,GLenum internalFormat,const char * internalFormatName)590 UsagePositiveNegativeTestCase::UsagePositiveNegativeTestCase (Context& context, const char* name, const char* description, GLenum internalFormat, const char* internalFormatName)
591 	: PolygonOffsetTestCase(context, name, description, internalFormat, internalFormatName, 200)
592 {
593 }
594 
testPolygonOffset(void)595 void UsagePositiveNegativeTestCase::testPolygonOffset (void)
596 {
597 	using tcu::TestLog;
598 
599 	const tcu::Vec4 triangleBottomRight[] =
600 	{
601 		tcu::Vec4(-1,  1,  0,  1),
602 		tcu::Vec4( 1,  1,  0,  1),
603 		tcu::Vec4( 1, -1,  0,  1),
604 	};
605 	const tcu::Vec4 triangleTopLeft[] =
606 	{
607 		tcu::Vec4(-1, -1,  0,  1),
608 		tcu::Vec4(-1,  1,  0,  1),
609 		tcu::Vec4( 1, -1,  0,  1),
610 	};
611 
612 	tcu::TestLog&		log				= m_testCtx.getLog();
613 	tcu::Surface		testImage		(m_targetSize, m_targetSize);
614 	tcu::Surface		referenceImage	(m_targetSize, m_targetSize);
615 	int					subpixelBits	= 0;
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 		{ GL_DEPTH_COMPONENT24,		24,		DepthBufferFormat::TYPE_FIXED_POINT,	"fixed24" },
1223 		{ GL_DEPTH_COMPONENT32F,	32,		DepthBufferFormat::TYPE_FLOATING_POINT,	"float32" },
1224 	};
1225 
1226 	for (int ndx = 0; ndx < DE_LENGTH_OF_ARRAY(depthFormats); ++ndx)
1227 	{
1228 		const DepthBufferFormat& format = depthFormats[ndx];
1229 
1230 		// enable works?
1231 		addChild(new UsageTestCase(m_context, (std::string(format.name) + "_enable").c_str(), "test enable GL_POLYGON_OFFSET_FILL", format.internalFormat, format.name));
1232 
1233 		// Really moves the polygons ?
1234 		addChild(new UsageDisplacementTestCase(m_context, (std::string(format.name) + "_displacement_with_units").c_str(), "test polygon offset", format.internalFormat, format.name));
1235 
1236 		// Really moves the polygons to right direction ?
1237 		addChild(new UsagePositiveNegativeTestCase(m_context, (std::string(format.name) + "_render_with_units").c_str(), "test polygon offset", format.internalFormat, format.name));
1238 
1239 		// Is total result clamped to [0,1] like promised?
1240 		addChild(new ResultClampingTestCase(m_context, (std::string(format.name) + "_result_depth_clamp").c_str(), "test polygon offset clamping", format.internalFormat, format.name));
1241 
1242 		// Slope really moves the polygon?
1243 		addChild(new UsageSlopeTestCase(m_context, (std::string(format.name) + "_render_with_factor").c_str(), "test polygon offset factor", format.internalFormat, format.name));
1244 
1245 		// Factor with zero slope
1246 		addChild(new ZeroSlopeTestCase(m_context, (std::string(format.name) + "_factor_0_slope").c_str(), "test polygon offset factor", format.internalFormat, format.name));
1247 
1248 		// Factor with 1.0 slope
1249 		addChild(new OneSlopeTestCase(m_context, (std::string(format.name) + "_factor_1_slope").c_str(), "test polygon offset factor", format.internalFormat, format.name));
1250 	}
1251 }
1252 
1253 } // Functional
1254 } // gles3
1255 } // deqp
1256