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