1e5c31af7Sopenharmony_ci/*-------------------------------------------------------------------------
2e5c31af7Sopenharmony_ci * drawElements Quality Program OpenGL ES 3.0 Module
3e5c31af7Sopenharmony_ci * -------------------------------------------------
4e5c31af7Sopenharmony_ci *
5e5c31af7Sopenharmony_ci * Copyright 2014 The Android Open Source Project
6e5c31af7Sopenharmony_ci *
7e5c31af7Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
8e5c31af7Sopenharmony_ci * you may not use this file except in compliance with the License.
9e5c31af7Sopenharmony_ci * You may obtain a copy of the License at
10e5c31af7Sopenharmony_ci *
11e5c31af7Sopenharmony_ci *      http://www.apache.org/licenses/LICENSE-2.0
12e5c31af7Sopenharmony_ci *
13e5c31af7Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
14e5c31af7Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
15e5c31af7Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16e5c31af7Sopenharmony_ci * See the License for the specific language governing permissions and
17e5c31af7Sopenharmony_ci * limitations under the License.
18e5c31af7Sopenharmony_ci *
19e5c31af7Sopenharmony_ci *//*!
20e5c31af7Sopenharmony_ci * \file
21e5c31af7Sopenharmony_ci * \brief Randomized per-fragment operation tests.
22e5c31af7Sopenharmony_ci *//*--------------------------------------------------------------------*/
23e5c31af7Sopenharmony_ci
24e5c31af7Sopenharmony_ci#include "es2fRandomFragmentOpTests.hpp"
25e5c31af7Sopenharmony_ci#include "glsFragmentOpUtil.hpp"
26e5c31af7Sopenharmony_ci#include "glsInteractionTestUtil.hpp"
27e5c31af7Sopenharmony_ci#include "tcuRenderTarget.hpp"
28e5c31af7Sopenharmony_ci#include "tcuTestLog.hpp"
29e5c31af7Sopenharmony_ci#include "tcuSurface.hpp"
30e5c31af7Sopenharmony_ci#include "tcuCommandLine.hpp"
31e5c31af7Sopenharmony_ci#include "tcuImageCompare.hpp"
32e5c31af7Sopenharmony_ci#include "tcuVectorUtil.hpp"
33e5c31af7Sopenharmony_ci#include "tcuTextureUtil.hpp"
34e5c31af7Sopenharmony_ci#include "gluPixelTransfer.hpp"
35e5c31af7Sopenharmony_ci#include "gluCallLogWrapper.hpp"
36e5c31af7Sopenharmony_ci#include "gluRenderContext.hpp"
37e5c31af7Sopenharmony_ci#include "deStringUtil.hpp"
38e5c31af7Sopenharmony_ci#include "deRandom.hpp"
39e5c31af7Sopenharmony_ci#include "deMath.h"
40e5c31af7Sopenharmony_ci#include "glwFunctions.hpp"
41e5c31af7Sopenharmony_ci#include "glwEnums.hpp"
42e5c31af7Sopenharmony_ci#include "rrFragmentOperations.hpp"
43e5c31af7Sopenharmony_ci#include "sglrReferenceUtils.hpp"
44e5c31af7Sopenharmony_ci
45e5c31af7Sopenharmony_ci#include <algorithm>
46e5c31af7Sopenharmony_ci
47e5c31af7Sopenharmony_cinamespace deqp
48e5c31af7Sopenharmony_ci{
49e5c31af7Sopenharmony_cinamespace gles2
50e5c31af7Sopenharmony_ci{
51e5c31af7Sopenharmony_cinamespace Functional
52e5c31af7Sopenharmony_ci{
53e5c31af7Sopenharmony_ci
54e5c31af7Sopenharmony_ciusing std::vector;
55e5c31af7Sopenharmony_ciusing tcu::TestLog;
56e5c31af7Sopenharmony_ciusing tcu::Vec2;
57e5c31af7Sopenharmony_ciusing tcu::Vec4;
58e5c31af7Sopenharmony_ciusing tcu::IVec2;
59e5c31af7Sopenharmony_ciusing tcu::BVec4;
60e5c31af7Sopenharmony_ci
61e5c31af7Sopenharmony_cienum
62e5c31af7Sopenharmony_ci{
63e5c31af7Sopenharmony_ci	VIEWPORT_WIDTH				= 64,
64e5c31af7Sopenharmony_ci	VIEWPORT_HEIGHT				= 64,
65e5c31af7Sopenharmony_ci	NUM_CALLS_PER_ITERATION		= 3,
66e5c31af7Sopenharmony_ci	NUM_ITERATIONS_PER_CASE		= 10
67e5c31af7Sopenharmony_ci};
68e5c31af7Sopenharmony_ci
69e5c31af7Sopenharmony_cistatic const tcu::Vec4		CLEAR_COLOR			(0.25f, 0.5f, 0.75f, 1.0f);
70e5c31af7Sopenharmony_cistatic const float			CLEAR_DEPTH			= 1.0f;
71e5c31af7Sopenharmony_cistatic const int			CLEAR_STENCIL		= 0;
72e5c31af7Sopenharmony_cistatic const bool			ENABLE_CALL_LOG		= true;
73e5c31af7Sopenharmony_ci
74e5c31af7Sopenharmony_ciusing namespace gls::FragmentOpUtil;
75e5c31af7Sopenharmony_ciusing namespace gls::InteractionTestUtil;
76e5c31af7Sopenharmony_ci
77e5c31af7Sopenharmony_civoid translateStencilState (const StencilState& src, rr::StencilState& dst)
78e5c31af7Sopenharmony_ci{
79e5c31af7Sopenharmony_ci	dst.func		= sglr::rr_util::mapGLTestFunc(src.function);
80e5c31af7Sopenharmony_ci	dst.ref			= src.reference;
81e5c31af7Sopenharmony_ci	dst.compMask	= src.compareMask;
82e5c31af7Sopenharmony_ci	dst.sFail		= sglr::rr_util::mapGLStencilOp(src.stencilFailOp);
83e5c31af7Sopenharmony_ci	dst.dpFail		= sglr::rr_util::mapGLStencilOp(src.depthFailOp);
84e5c31af7Sopenharmony_ci	dst.dpPass		= sglr::rr_util::mapGLStencilOp(src.depthPassOp);
85e5c31af7Sopenharmony_ci	dst.writeMask	= src.writeMask;
86e5c31af7Sopenharmony_ci}
87e5c31af7Sopenharmony_ci
88e5c31af7Sopenharmony_civoid translateBlendState (const BlendState& src, rr::BlendState& dst)
89e5c31af7Sopenharmony_ci{
90e5c31af7Sopenharmony_ci	dst.equation	= sglr::rr_util::mapGLBlendEquation(src.equation);
91e5c31af7Sopenharmony_ci	dst.srcFunc		= sglr::rr_util::mapGLBlendFunc(src.srcFunc);
92e5c31af7Sopenharmony_ci	dst.dstFunc		= sglr::rr_util::mapGLBlendFunc(src.dstFunc);
93e5c31af7Sopenharmony_ci}
94e5c31af7Sopenharmony_ci
95e5c31af7Sopenharmony_civoid translateState (const RenderState& src, rr::FragmentOperationState& dst, const tcu::RenderTarget& renderTarget)
96e5c31af7Sopenharmony_ci{
97e5c31af7Sopenharmony_ci	bool hasDepth		= renderTarget.getDepthBits() > 0;
98e5c31af7Sopenharmony_ci	bool hasStencil		= renderTarget.getStencilBits() > 0;
99e5c31af7Sopenharmony_ci
100e5c31af7Sopenharmony_ci	dst.scissorTestEnabled		= src.scissorTestEnabled;
101e5c31af7Sopenharmony_ci	dst.scissorRectangle		= src.scissorRectangle;
102e5c31af7Sopenharmony_ci	dst.stencilTestEnabled		= hasStencil && src.stencilTestEnabled;
103e5c31af7Sopenharmony_ci	dst.depthTestEnabled		= hasDepth && src.depthTestEnabled;
104e5c31af7Sopenharmony_ci	dst.blendMode				= src.blendEnabled ? rr::BLENDMODE_STANDARD : rr::BLENDMODE_NONE;
105e5c31af7Sopenharmony_ci	dst.numStencilBits			= renderTarget.getStencilBits();
106e5c31af7Sopenharmony_ci
107e5c31af7Sopenharmony_ci	dst.colorMask = src.colorMask;
108e5c31af7Sopenharmony_ci
109e5c31af7Sopenharmony_ci	if (dst.depthTestEnabled)
110e5c31af7Sopenharmony_ci	{
111e5c31af7Sopenharmony_ci		dst.depthFunc	= sglr::rr_util::mapGLTestFunc(src.depthFunc);
112e5c31af7Sopenharmony_ci		dst.depthMask	= src.depthWriteMask;
113e5c31af7Sopenharmony_ci	}
114e5c31af7Sopenharmony_ci
115e5c31af7Sopenharmony_ci	if (dst.stencilTestEnabled)
116e5c31af7Sopenharmony_ci	{
117e5c31af7Sopenharmony_ci		translateStencilState(src.stencil[rr::FACETYPE_BACK],	dst.stencilStates[rr::FACETYPE_BACK]);
118e5c31af7Sopenharmony_ci		translateStencilState(src.stencil[rr::FACETYPE_FRONT],	dst.stencilStates[rr::FACETYPE_FRONT]);
119e5c31af7Sopenharmony_ci	}
120e5c31af7Sopenharmony_ci
121e5c31af7Sopenharmony_ci	if (src.blendEnabled)
122e5c31af7Sopenharmony_ci	{
123e5c31af7Sopenharmony_ci		translateBlendState(src.blendRGBState, dst.blendRGBState);
124e5c31af7Sopenharmony_ci		translateBlendState(src.blendAState, dst.blendAState);
125e5c31af7Sopenharmony_ci		dst.blendColor = tcu::clamp(src.blendColor, Vec4(0.0f), Vec4(1.0f));
126e5c31af7Sopenharmony_ci	}
127e5c31af7Sopenharmony_ci}
128e5c31af7Sopenharmony_ci
129e5c31af7Sopenharmony_cistatic void renderQuad (const glw::Functions& gl, gls::FragmentOpUtil::QuadRenderer& renderer, const gls::FragmentOpUtil::IntegerQuad& quad, int baseX, int baseY)
130e5c31af7Sopenharmony_ci{
131e5c31af7Sopenharmony_ci	gls::FragmentOpUtil::Quad translated;
132e5c31af7Sopenharmony_ci
133e5c31af7Sopenharmony_ci	std::copy(DE_ARRAY_BEGIN(quad.color), DE_ARRAY_END(quad.color), DE_ARRAY_BEGIN(translated.color));
134e5c31af7Sopenharmony_ci
135e5c31af7Sopenharmony_ci	bool	flipX		= quad.posB.x() < quad.posA.x();
136e5c31af7Sopenharmony_ci	bool	flipY		= quad.posB.y() < quad.posA.y();
137e5c31af7Sopenharmony_ci	int		viewportX	= de::min(quad.posA.x(), quad.posB.x());
138e5c31af7Sopenharmony_ci	int		viewportY	= de::min(quad.posA.y(), quad.posB.y());
139e5c31af7Sopenharmony_ci	int		viewportW	= de::abs(quad.posA.x()-quad.posB.x())+1;
140e5c31af7Sopenharmony_ci	int		viewportH	= de::abs(quad.posA.y()-quad.posB.y())+1;
141e5c31af7Sopenharmony_ci
142e5c31af7Sopenharmony_ci	translated.posA = Vec2(flipX ? 1.0f : -1.0f, flipY ? 1.0f : -1.0f);
143e5c31af7Sopenharmony_ci	translated.posB = Vec2(flipX ? -1.0f : 1.0f, flipY ? -1.0f : 1.0f);
144e5c31af7Sopenharmony_ci
145e5c31af7Sopenharmony_ci	// \todo [2012-12-18 pyry] Pass in DepthRange parameters.
146e5c31af7Sopenharmony_ci	for (int ndx = 0; ndx < DE_LENGTH_OF_ARRAY(quad.depth); ndx++)
147e5c31af7Sopenharmony_ci		translated.depth[ndx] = quad.depth[ndx]*2.0f - 1.0f;
148e5c31af7Sopenharmony_ci
149e5c31af7Sopenharmony_ci	gl.viewport(baseX+viewportX, baseY+viewportY, viewportW, viewportH);
150e5c31af7Sopenharmony_ci	renderer.render(translated);
151e5c31af7Sopenharmony_ci}
152e5c31af7Sopenharmony_ci
153e5c31af7Sopenharmony_cistatic void setGLState (glu::CallLogWrapper& wrapper, const RenderState& state, int viewportX, int viewportY)
154e5c31af7Sopenharmony_ci{
155e5c31af7Sopenharmony_ci	if (state.scissorTestEnabled)
156e5c31af7Sopenharmony_ci	{
157e5c31af7Sopenharmony_ci		wrapper.glEnable(GL_SCISSOR_TEST);
158e5c31af7Sopenharmony_ci		wrapper.glScissor(viewportX+state.scissorRectangle.left, viewportY+state.scissorRectangle.bottom,
159e5c31af7Sopenharmony_ci						  state.scissorRectangle.width, state.scissorRectangle.height);
160e5c31af7Sopenharmony_ci	}
161e5c31af7Sopenharmony_ci	else
162e5c31af7Sopenharmony_ci		wrapper.glDisable(GL_SCISSOR_TEST);
163e5c31af7Sopenharmony_ci
164e5c31af7Sopenharmony_ci	if (state.stencilTestEnabled)
165e5c31af7Sopenharmony_ci	{
166e5c31af7Sopenharmony_ci		wrapper.glEnable(GL_STENCIL_TEST);
167e5c31af7Sopenharmony_ci
168e5c31af7Sopenharmony_ci		for (int face = 0; face < rr::FACETYPE_LAST; face++)
169e5c31af7Sopenharmony_ci		{
170e5c31af7Sopenharmony_ci			deUint32				glFace	= face == rr::FACETYPE_BACK ? GL_BACK : GL_FRONT;
171e5c31af7Sopenharmony_ci			const StencilState&		sParams	= state.stencil[face];
172e5c31af7Sopenharmony_ci
173e5c31af7Sopenharmony_ci			wrapper.glStencilFuncSeparate(glFace, sParams.function, sParams.reference, sParams.compareMask);
174e5c31af7Sopenharmony_ci			wrapper.glStencilOpSeparate(glFace, sParams.stencilFailOp, sParams.depthFailOp, sParams.depthPassOp);
175e5c31af7Sopenharmony_ci			wrapper.glStencilMaskSeparate(glFace, sParams.writeMask);
176e5c31af7Sopenharmony_ci		}
177e5c31af7Sopenharmony_ci	}
178e5c31af7Sopenharmony_ci	else
179e5c31af7Sopenharmony_ci		wrapper.glDisable(GL_STENCIL_TEST);
180e5c31af7Sopenharmony_ci
181e5c31af7Sopenharmony_ci	if (state.depthTestEnabled)
182e5c31af7Sopenharmony_ci	{
183e5c31af7Sopenharmony_ci		wrapper.glEnable(GL_DEPTH_TEST);
184e5c31af7Sopenharmony_ci		wrapper.glDepthFunc(state.depthFunc);
185e5c31af7Sopenharmony_ci		wrapper.glDepthMask(state.depthWriteMask ? GL_TRUE : GL_FALSE);
186e5c31af7Sopenharmony_ci	}
187e5c31af7Sopenharmony_ci	else
188e5c31af7Sopenharmony_ci		wrapper.glDisable(GL_DEPTH_TEST);
189e5c31af7Sopenharmony_ci
190e5c31af7Sopenharmony_ci	if (state.blendEnabled)
191e5c31af7Sopenharmony_ci	{
192e5c31af7Sopenharmony_ci		wrapper.glEnable(GL_BLEND);
193e5c31af7Sopenharmony_ci		wrapper.glBlendEquationSeparate(state.blendRGBState.equation, state.blendAState.equation);
194e5c31af7Sopenharmony_ci		wrapper.glBlendFuncSeparate(state.blendRGBState.srcFunc, state.blendRGBState.dstFunc, state.blendAState.srcFunc, state.blendAState.dstFunc);
195e5c31af7Sopenharmony_ci		wrapper.glBlendColor(state.blendColor.x(), state.blendColor.y(), state.blendColor.z(), state.blendColor.w());
196e5c31af7Sopenharmony_ci	}
197e5c31af7Sopenharmony_ci	else
198e5c31af7Sopenharmony_ci		wrapper.glDisable(GL_BLEND);
199e5c31af7Sopenharmony_ci
200e5c31af7Sopenharmony_ci	if (state.ditherEnabled)
201e5c31af7Sopenharmony_ci		wrapper.glEnable(GL_DITHER);
202e5c31af7Sopenharmony_ci	else
203e5c31af7Sopenharmony_ci		wrapper.glDisable(GL_DITHER);
204e5c31af7Sopenharmony_ci
205e5c31af7Sopenharmony_ci	wrapper.glColorMask(state.colorMask[0] ? GL_TRUE : GL_FALSE,
206e5c31af7Sopenharmony_ci						state.colorMask[1] ? GL_TRUE : GL_FALSE,
207e5c31af7Sopenharmony_ci						state.colorMask[2] ? GL_TRUE : GL_FALSE,
208e5c31af7Sopenharmony_ci						state.colorMask[3] ? GL_TRUE : GL_FALSE);
209e5c31af7Sopenharmony_ci}
210e5c31af7Sopenharmony_ci
211e5c31af7Sopenharmony_ciclass RandomFragmentOpCase : public TestCase
212e5c31af7Sopenharmony_ci{
213e5c31af7Sopenharmony_cipublic:
214e5c31af7Sopenharmony_ci						RandomFragmentOpCase		(Context& context, const char* name, const char* desc, deUint32 seed);
215e5c31af7Sopenharmony_ci						~RandomFragmentOpCase		(void);
216e5c31af7Sopenharmony_ci
217e5c31af7Sopenharmony_ci	void				init						(void);
218e5c31af7Sopenharmony_ci	void				deinit						(void);
219e5c31af7Sopenharmony_ci	IterateResult		iterate						(void);
220e5c31af7Sopenharmony_ci
221e5c31af7Sopenharmony_ciprivate:
222e5c31af7Sopenharmony_ci	tcu::UVec4			getCompareThreshold			(void) const;
223e5c31af7Sopenharmony_ci
224e5c31af7Sopenharmony_ci	deUint32										m_seed;
225e5c31af7Sopenharmony_ci
226e5c31af7Sopenharmony_ci	glu::CallLogWrapper						m_callLogWrapper;
227e5c31af7Sopenharmony_ci
228e5c31af7Sopenharmony_ci	gls::FragmentOpUtil::QuadRenderer*				m_renderer;
229e5c31af7Sopenharmony_ci	tcu::TextureLevel*								m_refColorBuffer;
230e5c31af7Sopenharmony_ci	tcu::TextureLevel*								m_refDepthBuffer;
231e5c31af7Sopenharmony_ci	tcu::TextureLevel*								m_refStencilBuffer;
232e5c31af7Sopenharmony_ci	gls::FragmentOpUtil::ReferenceQuadRenderer*		m_refRenderer;
233e5c31af7Sopenharmony_ci
234e5c31af7Sopenharmony_ci	int												m_iterNdx;
235e5c31af7Sopenharmony_ci};
236e5c31af7Sopenharmony_ci
237e5c31af7Sopenharmony_ciRandomFragmentOpCase::RandomFragmentOpCase (Context& context, const char* name, const char* desc, deUint32 seed)
238e5c31af7Sopenharmony_ci	: TestCase				(context, name, desc)
239e5c31af7Sopenharmony_ci	, m_seed				(seed)
240e5c31af7Sopenharmony_ci	, m_callLogWrapper		(context.getRenderContext().getFunctions(), context.getTestContext().getLog())
241e5c31af7Sopenharmony_ci	, m_renderer			(DE_NULL)
242e5c31af7Sopenharmony_ci	, m_refColorBuffer		(DE_NULL)
243e5c31af7Sopenharmony_ci	, m_refDepthBuffer		(DE_NULL)
244e5c31af7Sopenharmony_ci	, m_refStencilBuffer	(DE_NULL)
245e5c31af7Sopenharmony_ci	, m_refRenderer			(DE_NULL)
246e5c31af7Sopenharmony_ci	, m_iterNdx				(0)
247e5c31af7Sopenharmony_ci{
248e5c31af7Sopenharmony_ci	m_callLogWrapper.enableLogging(ENABLE_CALL_LOG);
249e5c31af7Sopenharmony_ci}
250e5c31af7Sopenharmony_ci
251e5c31af7Sopenharmony_ciRandomFragmentOpCase::~RandomFragmentOpCase (void)
252e5c31af7Sopenharmony_ci{
253e5c31af7Sopenharmony_ci	delete m_renderer;
254e5c31af7Sopenharmony_ci	delete m_refColorBuffer;
255e5c31af7Sopenharmony_ci	delete m_refDepthBuffer;
256e5c31af7Sopenharmony_ci	delete m_refStencilBuffer;
257e5c31af7Sopenharmony_ci	delete m_refRenderer;
258e5c31af7Sopenharmony_ci}
259e5c31af7Sopenharmony_ci
260e5c31af7Sopenharmony_civoid RandomFragmentOpCase::init (void)
261e5c31af7Sopenharmony_ci{
262e5c31af7Sopenharmony_ci	DE_ASSERT(!m_renderer && !m_refColorBuffer && !m_refDepthBuffer && !m_refStencilBuffer && !m_refRenderer);
263e5c31af7Sopenharmony_ci
264e5c31af7Sopenharmony_ci	int		width	= de::min<int>(m_context.getRenderTarget().getWidth(), VIEWPORT_WIDTH);
265e5c31af7Sopenharmony_ci	int		height	= de::min<int>(m_context.getRenderTarget().getHeight(), VIEWPORT_HEIGHT);
266e5c31af7Sopenharmony_ci	bool	useRGB	= m_context.getRenderTarget().getPixelFormat().alphaBits == 0;
267e5c31af7Sopenharmony_ci
268e5c31af7Sopenharmony_ci	m_renderer			= new gls::FragmentOpUtil::QuadRenderer(m_context.getRenderContext(), glu::GLSL_VERSION_100_ES);
269e5c31af7Sopenharmony_ci	m_refColorBuffer	= new tcu::TextureLevel(tcu::TextureFormat(useRGB ? tcu::TextureFormat::RGB : tcu::TextureFormat::RGBA, tcu::TextureFormat::UNORM_INT8), width, height);
270e5c31af7Sopenharmony_ci	m_refDepthBuffer	= new tcu::TextureLevel(tcu::TextureFormat(tcu::TextureFormat::D, tcu::TextureFormat::FLOAT),			width, height);
271e5c31af7Sopenharmony_ci	m_refStencilBuffer	= new tcu::TextureLevel(tcu::TextureFormat(tcu::TextureFormat::S, tcu::TextureFormat::UNSIGNED_INT32),	width, height);
272e5c31af7Sopenharmony_ci	m_refRenderer		= new gls::FragmentOpUtil::ReferenceQuadRenderer();
273e5c31af7Sopenharmony_ci	m_iterNdx			= 0;
274e5c31af7Sopenharmony_ci
275e5c31af7Sopenharmony_ci	m_testCtx.setTestResult(QP_TEST_RESULT_PASS, "Pass");
276e5c31af7Sopenharmony_ci}
277e5c31af7Sopenharmony_ci
278e5c31af7Sopenharmony_civoid RandomFragmentOpCase::deinit (void)
279e5c31af7Sopenharmony_ci{
280e5c31af7Sopenharmony_ci	delete m_renderer;
281e5c31af7Sopenharmony_ci	delete m_refColorBuffer;
282e5c31af7Sopenharmony_ci	delete m_refDepthBuffer;
283e5c31af7Sopenharmony_ci	delete m_refStencilBuffer;
284e5c31af7Sopenharmony_ci	delete m_refRenderer;
285e5c31af7Sopenharmony_ci
286e5c31af7Sopenharmony_ci	m_renderer			= DE_NULL;
287e5c31af7Sopenharmony_ci	m_refColorBuffer	= DE_NULL;
288e5c31af7Sopenharmony_ci	m_refDepthBuffer	= DE_NULL;
289e5c31af7Sopenharmony_ci	m_refStencilBuffer	= DE_NULL;
290e5c31af7Sopenharmony_ci	m_refRenderer		= DE_NULL;
291e5c31af7Sopenharmony_ci}
292e5c31af7Sopenharmony_ci
293e5c31af7Sopenharmony_ciRandomFragmentOpCase::IterateResult RandomFragmentOpCase::iterate (void)
294e5c31af7Sopenharmony_ci{
295e5c31af7Sopenharmony_ci	const glw::Functions&	gl				= m_context.getRenderContext().getFunctions();
296e5c31af7Sopenharmony_ci	const bool				isMSAA			= m_context.getRenderTarget().getNumSamples() > 1;
297e5c31af7Sopenharmony_ci	const deUint32			iterSeed		= deUint32Hash(m_seed) ^ deInt32Hash(m_iterNdx) ^ deInt32Hash(m_testCtx.getCommandLine().getBaseSeed());
298e5c31af7Sopenharmony_ci	de::Random				rnd				(iterSeed);
299e5c31af7Sopenharmony_ci
300e5c31af7Sopenharmony_ci	const int				width			= m_refColorBuffer->getWidth();
301e5c31af7Sopenharmony_ci	const int				height			= m_refColorBuffer->getHeight();
302e5c31af7Sopenharmony_ci	const int				viewportX		= rnd.getInt(0, m_context.getRenderTarget().getWidth()-width);
303e5c31af7Sopenharmony_ci	const int				viewportY		= rnd.getInt(0, m_context.getRenderTarget().getHeight()-height);
304e5c31af7Sopenharmony_ci
305e5c31af7Sopenharmony_ci	tcu::Surface			renderedImg		(width, height);
306e5c31af7Sopenharmony_ci	tcu::Surface			referenceImg	(width, height);
307e5c31af7Sopenharmony_ci
308e5c31af7Sopenharmony_ci	const Vec4				clearColor		= CLEAR_COLOR;
309e5c31af7Sopenharmony_ci	const float				clearDepth		= CLEAR_DEPTH;
310e5c31af7Sopenharmony_ci	const int				clearStencil	= CLEAR_STENCIL;
311e5c31af7Sopenharmony_ci
312e5c31af7Sopenharmony_ci	bool					gotError		= false;
313e5c31af7Sopenharmony_ci
314e5c31af7Sopenharmony_ci	const tcu::ScopedLogSection	iterSection	(m_testCtx.getLog(), std::string("Iteration") + de::toString(m_iterNdx), std::string("Iteration ") + de::toString(m_iterNdx));
315e5c31af7Sopenharmony_ci
316e5c31af7Sopenharmony_ci	// Compute randomized rendering commands.
317e5c31af7Sopenharmony_ci	vector<RenderCommand> commands;
318e5c31af7Sopenharmony_ci	computeRandomRenderCommands(rnd, glu::ApiType::es(2,0), NUM_CALLS_PER_ITERATION, width, height, commands);
319e5c31af7Sopenharmony_ci
320e5c31af7Sopenharmony_ci	// Reset default fragment state.
321e5c31af7Sopenharmony_ci	gl.disable(GL_SCISSOR_TEST);
322e5c31af7Sopenharmony_ci	gl.colorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
323e5c31af7Sopenharmony_ci	gl.depthMask(GL_TRUE);
324e5c31af7Sopenharmony_ci	gl.stencilMask(~0u);
325e5c31af7Sopenharmony_ci
326e5c31af7Sopenharmony_ci	// Render using GL.
327e5c31af7Sopenharmony_ci	m_callLogWrapper.glClearColor(clearColor.x(), clearColor.y(), clearColor.z(), clearColor.w());
328e5c31af7Sopenharmony_ci	m_callLogWrapper.glClearDepthf(clearDepth);
329e5c31af7Sopenharmony_ci	m_callLogWrapper.glClearStencil(clearStencil);
330e5c31af7Sopenharmony_ci	m_callLogWrapper.glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT);
331e5c31af7Sopenharmony_ci	m_callLogWrapper.glViewport(viewportX, viewportY, width, height);
332e5c31af7Sopenharmony_ci
333e5c31af7Sopenharmony_ci	for (vector<RenderCommand>::const_iterator cmd = commands.begin(); cmd != commands.end(); cmd++)
334e5c31af7Sopenharmony_ci	{
335e5c31af7Sopenharmony_ci		setGLState(m_callLogWrapper, cmd->state, viewportX, viewportY);
336e5c31af7Sopenharmony_ci
337e5c31af7Sopenharmony_ci		if (ENABLE_CALL_LOG)
338e5c31af7Sopenharmony_ci			m_testCtx.getLog() << TestLog::Message << "// Quad: " << cmd->quad.posA << " -> " << cmd->quad.posB
339e5c31af7Sopenharmony_ci												   << ", color: [" << cmd->quad.color[0] << ", " << cmd->quad.color[1] << ", " << cmd->quad.color[2] << ", " << cmd->quad.color[3] << "]"
340e5c31af7Sopenharmony_ci												   << ", depth: [" << cmd->quad.depth[0] << ", " << cmd->quad.depth[1] << ", " << cmd->quad.depth[2] << ", " << cmd->quad.depth[3] << "]"
341e5c31af7Sopenharmony_ci								<< TestLog::EndMessage;
342e5c31af7Sopenharmony_ci
343e5c31af7Sopenharmony_ci		renderQuad(gl, *m_renderer, cmd->quad, viewportX, viewportY);
344e5c31af7Sopenharmony_ci	}
345e5c31af7Sopenharmony_ci
346e5c31af7Sopenharmony_ci	// Check error.
347e5c31af7Sopenharmony_ci	if (m_callLogWrapper.glGetError() != GL_NO_ERROR)
348e5c31af7Sopenharmony_ci		gotError = true;
349e5c31af7Sopenharmony_ci
350e5c31af7Sopenharmony_ci	gl.flush();
351e5c31af7Sopenharmony_ci
352e5c31af7Sopenharmony_ci	// Render reference while GPU is doing work.
353e5c31af7Sopenharmony_ci	tcu::clear			(m_refColorBuffer->getAccess(),		clearColor);
354e5c31af7Sopenharmony_ci	tcu::clearDepth		(m_refDepthBuffer->getAccess(),		clearDepth);
355e5c31af7Sopenharmony_ci	tcu::clearStencil	(m_refStencilBuffer->getAccess(),	clearStencil);
356e5c31af7Sopenharmony_ci
357e5c31af7Sopenharmony_ci	for (vector<RenderCommand>::const_iterator cmd = commands.begin(); cmd != commands.end(); cmd++)
358e5c31af7Sopenharmony_ci	{
359e5c31af7Sopenharmony_ci		rr::FragmentOperationState refState;
360e5c31af7Sopenharmony_ci		translateState(cmd->state, refState, m_context.getRenderTarget());
361e5c31af7Sopenharmony_ci		m_refRenderer->render(gls::FragmentOpUtil::getMultisampleAccess(m_refColorBuffer->getAccess()),
362e5c31af7Sopenharmony_ci							  gls::FragmentOpUtil::getMultisampleAccess(m_refDepthBuffer->getAccess()),
363e5c31af7Sopenharmony_ci							  gls::FragmentOpUtil::getMultisampleAccess(m_refStencilBuffer->getAccess()),
364e5c31af7Sopenharmony_ci							  cmd->quad, refState);
365e5c31af7Sopenharmony_ci	}
366e5c31af7Sopenharmony_ci
367e5c31af7Sopenharmony_ci	// Expand reference color buffer to RGBA8
368e5c31af7Sopenharmony_ci	copy(referenceImg.getAccess(), m_refColorBuffer->getAccess());
369e5c31af7Sopenharmony_ci
370e5c31af7Sopenharmony_ci	// Read rendered image.
371e5c31af7Sopenharmony_ci	glu::readPixels(m_context.getRenderContext(), viewportX, viewportY, renderedImg.getAccess());
372e5c31af7Sopenharmony_ci
373e5c31af7Sopenharmony_ci	m_iterNdx += 1;
374e5c31af7Sopenharmony_ci
375e5c31af7Sopenharmony_ci	// Compare to reference.
376e5c31af7Sopenharmony_ci	bool				isLastIter	= m_iterNdx >= NUM_ITERATIONS_PER_CASE;
377e5c31af7Sopenharmony_ci	const tcu::UVec4	threshold	= getCompareThreshold();
378e5c31af7Sopenharmony_ci	bool				compareOk;
379e5c31af7Sopenharmony_ci
380e5c31af7Sopenharmony_ci	if (isMSAA)
381e5c31af7Sopenharmony_ci	{
382e5c31af7Sopenharmony_ci		// in MSAA cases, the sampling points could be anywhere in the pixel and we could
383e5c31af7Sopenharmony_ci		// even have multiple samples that are combined in resolve. Allow arbitrary sample
384e5c31af7Sopenharmony_ci		// positions by using bilinearCompare.
385e5c31af7Sopenharmony_ci		compareOk = tcu::bilinearCompare(m_testCtx.getLog(),
386e5c31af7Sopenharmony_ci										 "CompareResult",
387e5c31af7Sopenharmony_ci										 "Image Comparison Result",
388e5c31af7Sopenharmony_ci										 referenceImg.getAccess(),
389e5c31af7Sopenharmony_ci										 renderedImg.getAccess(),
390e5c31af7Sopenharmony_ci										 tcu::RGBA(threshold.x(), threshold.y(), threshold.z(), threshold.w()),
391e5c31af7Sopenharmony_ci										 tcu::COMPARE_LOG_RESULT);
392e5c31af7Sopenharmony_ci	}
393e5c31af7Sopenharmony_ci	else
394e5c31af7Sopenharmony_ci		compareOk = tcu::intThresholdCompare(m_testCtx.getLog(),
395e5c31af7Sopenharmony_ci											 "CompareResult",
396e5c31af7Sopenharmony_ci											 "Image Comparison Result",
397e5c31af7Sopenharmony_ci											 referenceImg.getAccess(),
398e5c31af7Sopenharmony_ci											 renderedImg.getAccess(),
399e5c31af7Sopenharmony_ci											 threshold,
400e5c31af7Sopenharmony_ci											 tcu::COMPARE_LOG_RESULT);
401e5c31af7Sopenharmony_ci
402e5c31af7Sopenharmony_ci	m_testCtx.getLog() << TestLog::Message << (compareOk ? "  Passed." : "  FAILED!") << TestLog::EndMessage;
403e5c31af7Sopenharmony_ci
404e5c31af7Sopenharmony_ci	if (!compareOk)
405e5c31af7Sopenharmony_ci		m_testCtx.setTestResult(QP_TEST_RESULT_FAIL, "Image comparison failed");
406e5c31af7Sopenharmony_ci	else if (gotError)
407e5c31af7Sopenharmony_ci		m_testCtx.setTestResult(QP_TEST_RESULT_FAIL, "GL error");
408e5c31af7Sopenharmony_ci
409e5c31af7Sopenharmony_ci	if (compareOk && !gotError && !isLastIter)
410e5c31af7Sopenharmony_ci		return CONTINUE;
411e5c31af7Sopenharmony_ci	else
412e5c31af7Sopenharmony_ci		return STOP;
413e5c31af7Sopenharmony_ci}
414e5c31af7Sopenharmony_ci
415e5c31af7Sopenharmony_citcu::UVec4 RandomFragmentOpCase::getCompareThreshold (void) const
416e5c31af7Sopenharmony_ci{
417e5c31af7Sopenharmony_ci	tcu::PixelFormat format = m_context.getRenderTarget().getPixelFormat();
418e5c31af7Sopenharmony_ci
419e5c31af7Sopenharmony_ci	if (format == tcu::PixelFormat(8, 8, 8, 8) || format == tcu::PixelFormat(8, 8, 8, 0))
420e5c31af7Sopenharmony_ci		return format.getColorThreshold().toIVec().asUint() + tcu::UVec4(6); // Default threshold.
421e5c31af7Sopenharmony_ci	else
422e5c31af7Sopenharmony_ci		return format.getColorThreshold().toIVec().asUint()
423e5c31af7Sopenharmony_ci			   * tcu::UVec4(5) + tcu::UVec4(2); // \note Non-scientific ad hoc formula. Need big threshold when few color bits; especially multiple blendings bring extra inaccuracy.
424e5c31af7Sopenharmony_ci}
425e5c31af7Sopenharmony_ci
426e5c31af7Sopenharmony_ciRandomFragmentOpTests::RandomFragmentOpTests (Context& context)
427e5c31af7Sopenharmony_ci	: TestCaseGroup(context, "random", "Randomized Per-Fragment Operation Tests")
428e5c31af7Sopenharmony_ci{
429e5c31af7Sopenharmony_ci}
430e5c31af7Sopenharmony_ci
431e5c31af7Sopenharmony_ciRandomFragmentOpTests::~RandomFragmentOpTests (void)
432e5c31af7Sopenharmony_ci{
433e5c31af7Sopenharmony_ci}
434e5c31af7Sopenharmony_ci
435e5c31af7Sopenharmony_civoid RandomFragmentOpTests::init (void)
436e5c31af7Sopenharmony_ci{
437e5c31af7Sopenharmony_ci	for (int ndx = 0; ndx < 100; ndx++)
438e5c31af7Sopenharmony_ci		addChild(new RandomFragmentOpCase(m_context, de::toString(ndx).c_str(), "", (deUint32)(ndx*NUM_ITERATIONS_PER_CASE)));
439e5c31af7Sopenharmony_ci}
440e5c31af7Sopenharmony_ci
441e5c31af7Sopenharmony_ci} // Functional
442e5c31af7Sopenharmony_ci} // gles2
443e5c31af7Sopenharmony_ci} // deqp
444