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 Sampler object testcases.
22 *//*--------------------------------------------------------------------*/
23
24#include "glsSamplerObjectTest.hpp"
25
26#include "tcuTexture.hpp"
27#include "tcuSurface.hpp"
28#include "tcuTextureUtil.hpp"
29#include "tcuImageCompare.hpp"
30#include "tcuTestLog.hpp"
31#include "tcuRGBA.hpp"
32#include "tcuRenderTarget.hpp"
33#include "tcuStringTemplate.hpp"
34
35#include "gluShaderProgram.hpp"
36#include "gluPixelTransfer.hpp"
37#include "gluDrawUtil.hpp"
38#include "gluRenderContext.hpp"
39#include "gluTextureUtil.hpp"
40
41#include "glwFunctions.hpp"
42
43#include "deRandom.hpp"
44#include "deString.h"
45
46#include "deString.h"
47
48#include <map>
49
50namespace deqp
51{
52namespace gls
53{
54
55namespace
56{
57const int VIEWPORT_WIDTH	= 128;
58const int VIEWPORT_HEIGHT	= 128;
59
60const int TEXTURE2D_WIDTH	= 32;
61const int TEXTURE2D_HEIGHT	= 32;
62
63const int TEXTURE3D_WIDTH	= 32;
64const int TEXTURE3D_HEIGHT	= 32;
65const int TEXTURE3D_DEPTH	= 32;
66
67const int CUBEMAP_SIZE		= 32;
68
69} // anonymous
70
71
72TextureSamplerTest::TextureSamplerTest (tcu::TestContext& testCtx, glu::RenderContext& renderCtx, const TestSpec& spec)
73	: tcu::TestCase		(testCtx, spec.name, spec.desc)
74	, m_renderCtx		(renderCtx)
75	, m_program			(NULL)
76	, m_target			(spec.target)
77	, m_textureState	(spec.textureState)
78	, m_samplerState	(spec.samplerState)
79	, m_random			(deStringHash(spec.name))
80{
81}
82
83void TextureSamplerTest::setTextureState (const glw::Functions& gl, GLenum target, SamplingState state)
84{
85	gl.texParameteri(target, GL_TEXTURE_MIN_FILTER, state.minFilter);
86	GLU_EXPECT_NO_ERROR(gl.getError(), "glTexParameteri(target, GL_TEXTURE_MIN_FILTER, state.minFilter)");
87	gl.texParameteri(target, GL_TEXTURE_MAG_FILTER, state.magFilter);
88	GLU_EXPECT_NO_ERROR(gl.getError(), "glTexParameteri(target, GL_TEXTURE_MAG_FILTER, state.magFilter)");
89	gl.texParameteri(target, GL_TEXTURE_WRAP_S, state.wrapS);
90	GLU_EXPECT_NO_ERROR(gl.getError(), "glTexParameteri(target, GL_TEXTURE_WRAP_S, state.wrapS)");
91	gl.texParameteri(target, GL_TEXTURE_WRAP_T, state.wrapT);
92	GLU_EXPECT_NO_ERROR(gl.getError(), "glTexParameteri(target, GL_TEXTURE_WRAP_T, state.wrapT)");
93	gl.texParameteri(target, GL_TEXTURE_WRAP_R, state.wrapR);
94	GLU_EXPECT_NO_ERROR(gl.getError(), "glTexParameteri(target, GL_TEXTURE_WRAP_R, state.wrapR)");
95	gl.texParameterf(target, GL_TEXTURE_MAX_LOD, state.maxLod);
96	GLU_EXPECT_NO_ERROR(gl.getError(), "glTexParameterf(target, GL_TEXTURE_MAX_LOD, state.maxLod)");
97	gl.texParameterf(target, GL_TEXTURE_MIN_LOD, state.minLod);
98	GLU_EXPECT_NO_ERROR(gl.getError(), "glTexParameterf(target, GL_TEXTURE_MIN_LOD, state.minLod)");
99}
100
101void TextureSamplerTest::setSamplerState (const glw::Functions& gl, SamplingState state, GLuint sampler)
102{
103	gl.samplerParameteri(sampler, GL_TEXTURE_MIN_FILTER, state.minFilter);
104	GLU_EXPECT_NO_ERROR(gl.getError(), "glSamplerParameteri(sampler, GL_TEXTURE_MIN_FILTER, state.minFilter)");
105	gl.samplerParameteri(sampler, GL_TEXTURE_MAG_FILTER, state.magFilter);
106	GLU_EXPECT_NO_ERROR(gl.getError(), "glSamplerParameteri(sampler, GL_TEXTURE_MAG_FILTER, state.magFilter)");
107	gl.samplerParameteri(sampler, GL_TEXTURE_WRAP_S, state.wrapS);
108	GLU_EXPECT_NO_ERROR(gl.getError(), "glSamplerParameteri(sampler, GL_TEXTURE_WRAP_S, state.wrapS)");
109	gl.samplerParameteri(sampler, GL_TEXTURE_WRAP_T, state.wrapT);
110	GLU_EXPECT_NO_ERROR(gl.getError(), "glSamplerParameteri(sampler, GL_TEXTURE_WRAP_T, state.wrapT)");
111	gl.samplerParameteri(sampler, GL_TEXTURE_WRAP_R, state.wrapR);
112	GLU_EXPECT_NO_ERROR(gl.getError(), "glSamplerParameteri(sampler, GL_TEXTURE_WRAP_R, state.wrapR)");
113	gl.samplerParameterf(sampler, GL_TEXTURE_MAX_LOD, state.maxLod);
114	GLU_EXPECT_NO_ERROR(gl.getError(), "glSamplerParameterf(sampler, GL_TEXTURE_MAX_LOD, state.maxLod)");
115	gl.samplerParameterf(sampler, GL_TEXTURE_MIN_LOD, state.minLod);
116	GLU_EXPECT_NO_ERROR(gl.getError(), "glSamplerParameterf(sampler, GL_TEXTURE_MIN_LOD, state.minLod)");
117}
118
119const char* TextureSamplerTest::selectVertexShader (GLenum target)
120{
121	switch (target)
122	{
123		case GL_TEXTURE_2D:
124			return
125			"${VTX_HDR}"
126			"${VTX_IN} ${HIGHP} vec2 a_position;\n"
127			"uniform ${HIGHP} float u_posScale;\n"
128			"${VTX_OUT} ${MEDIUMP} vec2 v_texCoord;\n"
129			"void main (void)\n"
130			"{\n"
131			"\tv_texCoord = a_position;\n"
132			"\tgl_Position = vec4(u_posScale * a_position, 0.0, 1.0);\n"
133			"}";
134
135		case GL_TEXTURE_3D:
136			return
137			"${VTX_HDR}"
138			"${VTX_IN} ${HIGHP} vec3 a_position;\n"
139			"uniform ${HIGHP} float u_posScale;\n"
140			"${VTX_OUT} ${MEDIUMP} vec3 v_texCoord;\n"
141			"void main (void)\n"
142			"{\n"
143			"\tv_texCoord = a_position;\n"
144			"\tgl_Position = vec4(u_posScale * a_position.xy, 0.0, 1.0);\n"
145			"}";
146
147		case GL_TEXTURE_CUBE_MAP:
148			return
149			"${VTX_HDR}"
150			"${VTX_IN} ${HIGHP} vec4 a_position;\n"
151			"uniform ${HIGHP} float u_posScale;\n"
152			"${VTX_OUT} ${MEDIUMP} vec2 v_texCoord;\n"
153			"void main (void)\n"
154			"{\n"
155			"\tv_texCoord = a_position.zw;\n"
156			"\tgl_Position = vec4(u_posScale * a_position.xy, 0.0, 1.0);\n"
157			"}";
158
159		default:
160			DE_ASSERT(false);
161			return NULL;
162	}
163}
164
165const char* TextureSamplerTest::selectFragmentShader (GLenum target)
166{
167	switch (target)
168	{
169		case GL_TEXTURE_2D:
170			return
171			"${FRAG_HDR}"
172			"uniform ${LOWP} sampler2D u_sampler;\n"
173			"${FRAG_IN} ${MEDIUMP} vec2 v_texCoord;\n"
174			"void main (void)\n"
175			"{\n"
176			"\t${FRAG_COLOR} = texture(u_sampler, v_texCoord);\n"
177			"}";
178
179		case GL_TEXTURE_3D:
180			return
181			"${FRAG_HDR}"
182			"uniform ${LOWP} sampler3D u_sampler;\n"
183			"${FRAG_IN} ${MEDIUMP} vec3 v_texCoord;\n"
184			"void main (void)\n"
185			"{\n"
186			"\t${FRAG_COLOR} = texture(u_sampler, v_texCoord);\n"
187			"}";
188
189		case GL_TEXTURE_CUBE_MAP:
190			return
191			"${FRAG_HDR}"
192			"uniform ${LOWP} samplerCube u_sampler;\n"
193			"${FRAG_IN} ${MEDIUMP} vec2 v_texCoord;\n"
194			"void main (void)\n"
195			"{\n"
196			"\t${FRAG_COLOR} = texture(u_sampler, vec3(cos(3.14 * v_texCoord.y) * sin(3.14 * v_texCoord.x), sin(3.14 * v_texCoord.y), cos(3.14 * v_texCoord.y) * cos(3.14 * v_texCoord.x)));\n"
197			"}";
198
199		default:
200			DE_ASSERT(false);
201			return NULL;
202	}
203}
204
205void TextureSamplerTest::init (void)
206{
207	const char* vertexShaderTemplate	= selectVertexShader(m_target);
208	const char* fragmentShaderTemplate	= selectFragmentShader(m_target);
209
210	std::map<std::string, std::string>	params;
211
212	if (glu::isGLSLVersionSupported(m_renderCtx.getType(), glu::GLSL_VERSION_300_ES))
213	{
214		params["VTX_HDR"]		= "#version 300 es\n";
215		params["FRAG_HDR"]		= "#version 300 es\nlayout(location = 0) out mediump vec4 o_color;\n";
216		params["VTX_IN"]		= "in";
217		params["VTX_OUT"]		= "out";
218		params["FRAG_IN"]		= "in";
219		params["FRAG_COLOR"]	= "o_color";
220		params["HIGHP"]			= "highp";
221		params["LOWP"]			= "lowp";
222		params["MEDIUMP"]		= "mediump";
223	}
224	else if (glu::isGLSLVersionSupported(m_renderCtx.getType(), glu::GLSL_VERSION_330))
225	{
226		params["VTX_HDR"]		= "#version 330\n";
227		params["FRAG_HDR"]		= "#version 330\nlayout(location = 0) out mediump vec4 o_color;\n";
228		params["VTX_IN"]		= "in";
229		params["VTX_OUT"]		= "out";
230		params["FRAG_IN"]		= "in";
231		params["FRAG_COLOR"]	= "o_color";
232		params["HIGHP"]			= "highp";
233		params["LOWP"]			= "lowp";
234		params["MEDIUMP"]		= "mediump";
235	}
236	else
237		DE_ASSERT(false);
238
239	DE_ASSERT(!m_program);
240	m_program = new glu::ShaderProgram(m_renderCtx,
241									   glu::makeVtxFragSources(tcu::StringTemplate(vertexShaderTemplate).specialize(params),
242															   tcu::StringTemplate(fragmentShaderTemplate).specialize(params)));
243
244	if (!m_program->isOk())
245	{
246		tcu::TestLog& log = m_testCtx.getLog();
247		log << *m_program;
248		TCU_FAIL("Failed to compile shaders");
249	}
250}
251
252void TextureSamplerTest::deinit (void)
253{
254	delete m_program;
255	m_program = NULL;
256}
257
258TextureSamplerTest::~TextureSamplerTest (void)
259{
260	deinit();
261}
262
263const float s_positions[] = {
264	-1.0, -1.0,
265	 1.0, -1.0,
266	 1.0,  1.0,
267
268	 1.0,  1.0,
269	-1.0,  1.0,
270	-1.0, -1.0
271};
272
273const float s_positions3D[] = {
274	-1.0f, -1.0f, -1.0f,
275	 1.0f, -1.0f,  1.0f,
276	 1.0f,  1.0f, -1.0f,
277
278	 1.0f,  1.0f, -1.0f,
279	-1.0f,  1.0f,  1.0f,
280	-1.0f, -1.0f, -1.0f
281};
282
283const float s_positionsCube[] = {
284	-1.0f, -1.0f, -1.0f, -0.5f,
285	 1.0f, -1.0f,  1.0f, -0.5f,
286	 1.0f,  1.0f,  1.0f,  0.5f,
287
288	 1.0f,  1.0f,  1.0f,  0.5f,
289	-1.0f,  1.0f, -1.0f,  0.5f,
290	-1.0f, -1.0f, -1.0f, -0.5f
291};
292
293void TextureSamplerTest::render (void)
294{
295	const glw::Functions& gl = m_renderCtx.getFunctions();
296
297	GLuint	samplerLoc	= (GLuint)-1;
298	GLuint	scaleLoc	= (GLuint)-1;
299
300	gl.useProgram(m_program->getProgram());
301	GLU_EXPECT_NO_ERROR(gl.getError(), "glUseProgram(m_program->getProgram())");
302
303	samplerLoc = gl.getUniformLocation(m_program->getProgram(), "u_sampler");
304	TCU_CHECK(samplerLoc != (GLuint)-1);
305
306	scaleLoc = gl.getUniformLocation(m_program->getProgram(), "u_posScale");
307	TCU_CHECK(scaleLoc != (GLuint)-1);
308
309	gl.clearColor(0.5f, 0.5f, 0.5f, 1.0f);
310	GLU_EXPECT_NO_ERROR(gl.getError(), "glClearColor(0.5f, 0.5f, 0.5f, 1.0f)");
311
312	gl.clear(GL_COLOR_BUFFER_BIT);
313	GLU_EXPECT_NO_ERROR(gl.getError(), "glClear(GL_COLOR_BUFFER_BIT)");
314
315	gl.uniform1i(samplerLoc, 0);
316	GLU_EXPECT_NO_ERROR(gl.getError(), "glUniform1i(samplerLoc, 0)");
317
318	gl.uniform1f(scaleLoc, 1.0f);
319	GLU_EXPECT_NO_ERROR(gl.getError(), "glUniform1f(scaleLoc, 1.0f)");
320
321	switch (m_target)
322	{
323		case GL_TEXTURE_2D:
324		{
325			glu::VertexArrayBinding vertexArrays[] =
326			{
327				glu::VertexArrayBinding(glu::BindingPoint("a_position"), glu::VertexArrayPointer(glu::VTX_COMP_FLOAT, glu::VTX_COMP_CONVERT_NONE, 2, 6, 0, s_positions))
328			};
329
330			glu::draw(m_renderCtx, m_program->getProgram(), DE_LENGTH_OF_ARRAY(vertexArrays), vertexArrays, glu::PrimitiveList(glu::PRIMITIVETYPE_TRIANGLES, 6));
331
332			gl.uniform1f(scaleLoc, 0.25f);
333			GLU_EXPECT_NO_ERROR(gl.getError(), "glUniform1f(scaleLoc, 0.25f)");
334
335			glu::draw(m_renderCtx, m_program->getProgram(), DE_LENGTH_OF_ARRAY(vertexArrays), vertexArrays, glu::PrimitiveList(glu::PRIMITIVETYPE_TRIANGLES, 6));
336
337			break;
338		}
339
340		case GL_TEXTURE_3D:
341		{
342			glu::VertexArrayBinding vertexArrays[] =
343			{
344				glu::VertexArrayBinding(glu::BindingPoint("a_position"), glu::VertexArrayPointer(glu::VTX_COMP_FLOAT, glu::VTX_COMP_CONVERT_NONE, 3, 6, 0, s_positions3D))
345			};
346
347			glu::draw(m_renderCtx, m_program->getProgram(), DE_LENGTH_OF_ARRAY(vertexArrays), vertexArrays, glu::PrimitiveList(glu::PRIMITIVETYPE_TRIANGLES, 6));
348
349			gl.uniform1f(scaleLoc, 0.25f);
350			GLU_EXPECT_NO_ERROR(gl.getError(), "glUniform1f(scaleLoc, 0.25f)");
351
352			glu::draw(m_renderCtx, m_program->getProgram(), DE_LENGTH_OF_ARRAY(vertexArrays), vertexArrays, glu::PrimitiveList(glu::PRIMITIVETYPE_TRIANGLES, 6));
353
354			break;
355		}
356
357		case GL_TEXTURE_CUBE_MAP:
358		{
359			glu::VertexArrayBinding vertexArrays[] =
360			{
361				glu::VertexArrayBinding(glu::BindingPoint("a_position"), glu::VertexArrayPointer(glu::VTX_COMP_FLOAT, glu::VTX_COMP_CONVERT_NONE, 4, 6, 0, s_positionsCube))
362			};
363
364			glu::draw(m_renderCtx, m_program->getProgram(), DE_LENGTH_OF_ARRAY(vertexArrays), vertexArrays, glu::PrimitiveList(glu::PRIMITIVETYPE_TRIANGLES, 6));
365
366			gl.uniform1f(scaleLoc, 0.25f);
367			GLU_EXPECT_NO_ERROR(gl.getError(), "glUniform1f(scaleLoc, 0.25f)");
368
369			glu::draw(m_renderCtx, m_program->getProgram(), DE_LENGTH_OF_ARRAY(vertexArrays), vertexArrays, glu::PrimitiveList(glu::PRIMITIVETYPE_TRIANGLES, 6));
370
371			break;
372		}
373
374		default:
375			DE_ASSERT(false);
376	}
377}
378
379GLuint TextureSamplerTest::createTexture2D (const glw::Functions& gl)
380{
381	GLuint			texture		= (GLuint)-1;
382	tcu::Texture2D	refTexture	(tcu::TextureFormat(tcu::TextureFormat::RGBA, tcu::TextureFormat::UNORM_INT8), TEXTURE2D_WIDTH, TEXTURE2D_HEIGHT);
383
384	refTexture.allocLevel(0);
385	tcu::fillWithComponentGradients(refTexture.getLevel(0), tcu::Vec4(0.0f, 0.0f, 0.0f, 0.0f), tcu::Vec4(1.0f, 1.0f, 1.0f, 1.0f));
386
387	gl.genTextures(1, &texture);
388	GLU_EXPECT_NO_ERROR(gl.getError(), "glGenTextures(1, &texture)");
389
390	gl.bindTexture(GL_TEXTURE_2D, texture);
391	GLU_EXPECT_NO_ERROR(gl.getError(), "glBindTexture(GL_TEXTURE_2D, texture)");
392
393	gl.texImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, refTexture.getWidth(), refTexture.getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, refTexture.getLevel(0).getDataPtr());
394	GLU_EXPECT_NO_ERROR(gl.getError(), "glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, refTexture.getWidth(), refTexture.getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, refTexture.getLevel(0).getDataPtr())");
395
396	gl.generateMipmap(GL_TEXTURE_2D);
397	GLU_EXPECT_NO_ERROR(gl.getError(), "glGenerateMipmap(GL_TEXTURE_2D)");
398
399	gl.bindTexture(GL_TEXTURE_2D, 0);
400	GLU_EXPECT_NO_ERROR(gl.getError(), "glBindTexture(GL_TEXTURE_2D, texture)");
401
402	return texture;
403}
404
405GLuint TextureSamplerTest::createTexture3D (const glw::Functions& gl)
406{
407	GLuint			texture		= (GLuint)-1;
408	tcu::Texture3D	refTexture	(tcu::TextureFormat(tcu::TextureFormat::RGBA, tcu::TextureFormat::UNORM_INT8), TEXTURE3D_WIDTH, TEXTURE3D_HEIGHT, TEXTURE3D_DEPTH);
409
410	refTexture.allocLevel(0);
411	tcu::fillWithComponentGradients(refTexture.getLevel(0), tcu::Vec4(0.0f, 0.0f, 0.0f, 0.0f), tcu::Vec4(1.0f, 1.0f, 1.0f, 1.0f));
412
413	gl.genTextures(1, &texture);
414	GLU_EXPECT_NO_ERROR(gl.getError(), "glGenTextures(1, &texture)");
415
416	gl.bindTexture(GL_TEXTURE_3D, texture);
417	GLU_EXPECT_NO_ERROR(gl.getError(), "glBindTexture(GL_TEXTURE_3D, texture)");
418
419	gl.texImage3D(GL_TEXTURE_3D, 0, GL_RGBA8, refTexture.getWidth(), refTexture.getHeight(), refTexture.getDepth(), 0, GL_RGBA, GL_UNSIGNED_BYTE, refTexture.getLevel(0).getDataPtr());
420	GLU_EXPECT_NO_ERROR(gl.getError(), "glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA8, refTexture.getWidth(), refTexture.getHeight(), refTexture.getDepth(), 0, GL_RGBA, GL_UNSIGNED_BYTE, refTexture.getLevel(0).getDataPtr())");
421
422	gl.generateMipmap(GL_TEXTURE_3D);
423	GLU_EXPECT_NO_ERROR(gl.getError(), "glGenerateMipmap(GL_TEXTURE_3D)");
424
425	gl.bindTexture(GL_TEXTURE_3D, 0);
426	GLU_EXPECT_NO_ERROR(gl.getError(), "glBindTexture(GL_TEXTURE_3D, 0)");
427
428	return texture;
429}
430
431GLuint TextureSamplerTest::createTextureCube (const glw::Functions& gl)
432{
433	GLuint				texture		= (GLuint)-1;
434	tcu::TextureCube	refTexture	(tcu::TextureFormat(tcu::TextureFormat::RGBA, tcu::TextureFormat::UNORM_INT8), CUBEMAP_SIZE);
435
436	refTexture.allocLevel(tcu::CUBEFACE_POSITIVE_X, 0);
437	refTexture.allocLevel(tcu::CUBEFACE_POSITIVE_Y, 0);
438	refTexture.allocLevel(tcu::CUBEFACE_POSITIVE_Z, 0);
439	refTexture.allocLevel(tcu::CUBEFACE_NEGATIVE_X, 0);
440	refTexture.allocLevel(tcu::CUBEFACE_NEGATIVE_Y, 0);
441	refTexture.allocLevel(tcu::CUBEFACE_NEGATIVE_Z, 0);
442
443	tcu::fillWithComponentGradients(refTexture.getLevelFace(0, tcu::CUBEFACE_POSITIVE_X), tcu::Vec4(0.0f, 0.0f, 0.0f, 0.0f), tcu::Vec4(1.0f, 1.0f, 1.0f, 1.0f));
444	tcu::fillWithComponentGradients(refTexture.getLevelFace(0, tcu::CUBEFACE_POSITIVE_Y), tcu::Vec4(0.0f, 0.0f, 0.0f, 0.0f), tcu::Vec4(1.0f, 1.0f, 1.0f, 1.0f));
445	tcu::fillWithComponentGradients(refTexture.getLevelFace(0, tcu::CUBEFACE_POSITIVE_Z), tcu::Vec4(0.0f, 0.0f, 0.0f, 0.0f), tcu::Vec4(1.0f, 1.0f, 1.0f, 1.0f));
446	tcu::fillWithComponentGradients(refTexture.getLevelFace(0, tcu::CUBEFACE_NEGATIVE_X), tcu::Vec4(0.0f, 0.0f, 0.0f, 0.0f), tcu::Vec4(1.0f, 1.0f, 1.0f, 1.0f));
447	tcu::fillWithComponentGradients(refTexture.getLevelFace(0, tcu::CUBEFACE_NEGATIVE_Y), tcu::Vec4(0.0f, 0.0f, 0.0f, 0.0f), tcu::Vec4(1.0f, 1.0f, 1.0f, 1.0f));
448	tcu::fillWithComponentGradients(refTexture.getLevelFace(0, tcu::CUBEFACE_NEGATIVE_Z), tcu::Vec4(0.0f, 0.0f, 0.0f, 0.0f), tcu::Vec4(1.0f, 1.0f, 1.0f, 1.0f));
449
450	gl.bindTexture(GL_TEXTURE_CUBE_MAP, texture);
451	GLU_EXPECT_NO_ERROR(gl.getError(), "glBindTexture(GL_TEXTURE_CUBE_MAP, texture)");
452
453	for (int face = 0; face < tcu::CUBEFACE_LAST; face++)
454	{
455		const deUint32 target = glu::getGLCubeFace((tcu::CubeFace)face);
456		gl.texImage2D(target, 0, GL_RGBA8, refTexture.getSize(), refTexture.getSize(), 0, GL_RGBA, GL_UNSIGNED_BYTE, refTexture.getLevelFace(0, (tcu::CubeFace)face).getDataPtr());
457	}
458	GLU_EXPECT_NO_ERROR(gl.getError(), "glTexImage2D(GL_TEXTURE_CUBE_MAP_...) failed");
459
460	gl.generateMipmap(GL_TEXTURE_CUBE_MAP);
461	GLU_EXPECT_NO_ERROR(gl.getError(), "glGenerateMipmap(GL_TEXTURE_CUBE_MAP)");
462	gl.bindTexture(GL_TEXTURE_CUBE_MAP, 0);
463	GLU_EXPECT_NO_ERROR(gl.getError(), "glBindTexture(GL_TEXTURE_CUBE_MAP, texture)");
464
465	return texture;
466}
467
468GLuint TextureSamplerTest::createTexture (const glw::Functions& gl, GLenum target)
469{
470	switch (target)
471	{
472		case GL_TEXTURE_2D:
473			return createTexture2D(gl);
474
475		case GL_TEXTURE_3D:
476			return createTexture3D(gl);
477
478		case GL_TEXTURE_CUBE_MAP:
479			return createTextureCube(gl);
480
481		default:
482			DE_ASSERT(false);
483			return (GLuint)-1;
484	}
485}
486
487void TextureSamplerTest::renderReferences (tcu::Surface& textureRef, tcu::Surface& samplerRef, int x, int y)
488{
489	const glw::Functions&	gl		= m_renderCtx.getFunctions();
490	GLuint					texture	= createTexture(gl, m_target);
491
492	gl.viewport(x, y, VIEWPORT_WIDTH, VIEWPORT_HEIGHT);
493	GLU_EXPECT_NO_ERROR(gl.getError(), "glViewport(x, y, VIEWPORT_WIDTH, VIEWPORT_HEIGHT)");
494
495	gl.bindTexture(m_target, texture);
496	GLU_EXPECT_NO_ERROR(gl.getError(), "glBindTexture(m_target, texture)");
497
498	setTextureState(gl, m_target, m_textureState);
499	render();
500	glu::readPixels(m_renderCtx, x, y, textureRef.getAccess());
501
502	setTextureState(gl, m_target, m_samplerState);
503	render();
504	glu::readPixels(m_renderCtx, x, y, samplerRef.getAccess());
505
506	gl.deleteTextures(1, &texture);
507	GLU_EXPECT_NO_ERROR(gl.getError(), "glDeleteTextures(1, &texture)");
508}
509
510void TextureSamplerTest::renderResults (tcu::Surface& textureResult, tcu::Surface& samplerResult, int x, int y)
511{
512	const glw::Functions&	gl		= m_renderCtx.getFunctions();
513	GLuint					texture	= createTexture(gl, m_target);
514	GLuint					sampler	= -1;
515
516	gl.viewport(x, y, VIEWPORT_WIDTH, VIEWPORT_HEIGHT);
517	GLU_EXPECT_NO_ERROR(gl.getError(), "glViewport(x, y, VIEWPORT_WIDTH, VIEWPORT_HEIGHT)");
518
519	gl.genSamplers(1, &sampler);
520	GLU_EXPECT_NO_ERROR(gl.getError(), "glGenSamplers(1, &sampler)");
521	TCU_CHECK(sampler != (GLuint)-1);
522
523	gl.bindSampler(0, sampler);
524	GLU_EXPECT_NO_ERROR(gl.getError(), "glBindSampler(0, sampler)");
525
526	// First set sampler state
527	setSamplerState(gl, m_samplerState, sampler);
528
529	// Set texture state
530	gl.bindTexture(m_target, texture);
531	GLU_EXPECT_NO_ERROR(gl.getError(), "glBindTexture(m_target, texture)");
532
533	setTextureState(gl, m_target, m_textureState);
534
535	// Render using sampler
536	render();
537	glu::readPixels(m_renderCtx, x, y, samplerResult.getAccess());
538
539	// Render without sampler
540	gl.bindSampler(0, 0);
541	GLU_EXPECT_NO_ERROR(gl.getError(), "glBindSampler(0, 0)");
542
543	render();
544	glu::readPixels(m_renderCtx, x, y, textureResult.getAccess());
545
546	gl.deleteSamplers(1, &sampler);
547	GLU_EXPECT_NO_ERROR(gl.getError(), "glDeleteSamplers(1, &sampler)");
548	gl.deleteTextures(1, &texture);
549	GLU_EXPECT_NO_ERROR(gl.getError(), "glDeleteTextures(1, &texture)");
550}
551
552tcu::TestCase::IterateResult TextureSamplerTest::iterate (void)
553{
554	tcu::TestLog&	log = m_testCtx.getLog();
555
556	tcu::Surface	textureRef(VIEWPORT_WIDTH, VIEWPORT_HEIGHT);
557	tcu::Surface	samplerRef(VIEWPORT_WIDTH, VIEWPORT_HEIGHT);
558
559	tcu::Surface	textureResult(VIEWPORT_WIDTH, VIEWPORT_HEIGHT);
560	tcu::Surface	samplerResult(VIEWPORT_WIDTH, VIEWPORT_HEIGHT);
561
562	int				x = m_random.getInt(0, m_renderCtx.getRenderTarget().getWidth() - VIEWPORT_WIDTH);
563	int				y = m_random.getInt(0, m_renderCtx.getRenderTarget().getHeight() - VIEWPORT_HEIGHT);
564
565	renderReferences(textureRef, samplerRef, x, y);
566	renderResults(textureResult, samplerResult, x, y);
567
568	bool isOk = pixelThresholdCompare (log, "Sampler render result", "Result from rendering with sampler", samplerRef, samplerResult, tcu::RGBA(0, 0, 0, 0), tcu::COMPARE_LOG_RESULT);
569
570	if (!pixelThresholdCompare (log, "Texture render result", "Result from rendering with texture state", textureRef, textureResult, tcu::RGBA(0, 0, 0, 0), tcu::COMPARE_LOG_RESULT))
571		isOk = false;
572
573	if (!isOk)
574	{
575		m_testCtx.setTestResult(QP_TEST_RESULT_FAIL, "Fail");
576		return STOP;
577	}
578
579	m_testCtx.setTestResult(QP_TEST_RESULT_PASS, "Pass");
580	return STOP;
581}
582
583MultiTextureSamplerTest::MultiTextureSamplerTest (tcu::TestContext& testCtx, glu::RenderContext& renderCtx, const TestSpec& spec)
584	: TestCase			(testCtx, spec.name, spec.desc)
585	, m_renderCtx		(renderCtx)
586	, m_program			(NULL)
587	, m_target			(spec.target)
588	, m_textureState1	(spec.textureState1)
589	, m_textureState2	(spec.textureState2)
590	, m_samplerState	(spec.samplerState)
591	, m_random			(deStringHash(spec.name))
592{
593}
594
595void MultiTextureSamplerTest::setTextureState (const glw::Functions& gl, GLenum target, SamplingState state)
596{
597	gl.texParameteri(target, GL_TEXTURE_MIN_FILTER, state.minFilter);
598	GLU_EXPECT_NO_ERROR(gl.getError(), "glTexParameteri(target, GL_TEXTURE_MIN_FILTER, state.minFilter)");
599	gl.texParameteri(target, GL_TEXTURE_MAG_FILTER, state.magFilter);
600	GLU_EXPECT_NO_ERROR(gl.getError(), "glTexParameteri(target, GL_TEXTURE_MAG_FILTER, state.magFilter)");
601	gl.texParameteri(target, GL_TEXTURE_WRAP_S, state.wrapS);
602	GLU_EXPECT_NO_ERROR(gl.getError(), "glTexParameteri(target, GL_TEXTURE_WRAP_S, state.wrapS)");
603	gl.texParameteri(target, GL_TEXTURE_WRAP_T, state.wrapT);
604	GLU_EXPECT_NO_ERROR(gl.getError(), "glTexParameteri(target, GL_TEXTURE_WRAP_T, state.wrapT)");
605	gl.texParameteri(target, GL_TEXTURE_WRAP_R, state.wrapR);
606	GLU_EXPECT_NO_ERROR(gl.getError(), "glTexParameteri(target, GL_TEXTURE_WRAP_R, state.wrapR)");
607	gl.texParameterf(target, GL_TEXTURE_MAX_LOD, state.maxLod);
608	GLU_EXPECT_NO_ERROR(gl.getError(), "glTexParameterf(target, GL_TEXTURE_MAX_LOD, state.maxLod)");
609	gl.texParameterf(target, GL_TEXTURE_MIN_LOD, state.minLod);
610	GLU_EXPECT_NO_ERROR(gl.getError(), "glTexParameterf(target, GL_TEXTURE_MIN_LOD, state.minLod)");
611}
612
613void MultiTextureSamplerTest::setSamplerState (const glw::Functions& gl, SamplingState state, GLuint sampler)
614{
615	gl.samplerParameteri(sampler, GL_TEXTURE_MIN_FILTER, state.minFilter);
616	GLU_EXPECT_NO_ERROR(gl.getError(), "glSamplerParameteri(sampler, GL_TEXTURE_MIN_FILTER, state.minFilter)");
617	gl.samplerParameteri(sampler, GL_TEXTURE_MAG_FILTER, state.magFilter);
618	GLU_EXPECT_NO_ERROR(gl.getError(), "glSamplerParameteri(sampler, GL_TEXTURE_MAG_FILTER, state.magFilter)");
619	gl.samplerParameteri(sampler, GL_TEXTURE_WRAP_S, state.wrapS);
620	GLU_EXPECT_NO_ERROR(gl.getError(), "glSamplerParameteri(sampler, GL_TEXTURE_WRAP_S, state.wrapS)");
621	gl.samplerParameteri(sampler, GL_TEXTURE_WRAP_T, state.wrapT);
622	GLU_EXPECT_NO_ERROR(gl.getError(), "glSamplerParameteri(sampler, GL_TEXTURE_WRAP_T, state.wrapT)");
623	gl.samplerParameteri(sampler, GL_TEXTURE_WRAP_R, state.wrapR);
624	GLU_EXPECT_NO_ERROR(gl.getError(), "glSamplerParameteri(sampler, GL_TEXTURE_WRAP_R, state.wrapR)");
625	gl.samplerParameterf(sampler, GL_TEXTURE_MAX_LOD, state.maxLod);
626	GLU_EXPECT_NO_ERROR(gl.getError(), "glSamplerParameterf(sampler, GL_TEXTURE_MAX_LOD, state.maxLod)");
627	gl.samplerParameterf(sampler, GL_TEXTURE_MIN_LOD, state.minLod);
628	GLU_EXPECT_NO_ERROR(gl.getError(), "glSamplerParameterf(sampler, GL_TEXTURE_MIN_LOD, state.minLod)");
629}
630
631const char* MultiTextureSamplerTest::selectVertexShader (GLenum target)
632{
633	switch (target)
634	{
635		case GL_TEXTURE_2D:
636			return
637			"${VTX_HDR}"
638			"${VTX_IN} ${HIGHP} vec2 a_position;\n"
639			"uniform ${HIGHP} float u_posScale;\n"
640			"${VTX_OUT} ${MEDIUMP} vec2 v_texCoord;\n"
641			"void main (void)\n"
642			"{\n"
643			"\tv_texCoord = a_position;\n"
644			"\tgl_Position = vec4(u_posScale * a_position, 0.0, 1.0);\n"
645			"}";
646
647		case GL_TEXTURE_3D:
648			return
649			"${VTX_HDR}"
650			"${VTX_IN} ${HIGHP} vec3 a_position;\n"
651			"uniform ${HIGHP} float u_posScale;\n"
652			"${VTX_OUT} ${MEDIUMP} vec3 v_texCoord;\n"
653			"void main (void)\n"
654			"{\n"
655			"\tv_texCoord = a_position;\n"
656			"\tgl_Position = vec4(u_posScale * a_position.xy, 0.0, 1.0);\n"
657			"}";
658
659		case GL_TEXTURE_CUBE_MAP:
660			return
661			"${VTX_HDR}"
662			"${VTX_IN} ${HIGHP} vec4 a_position;\n"
663			"uniform ${HIGHP} float u_posScale;\n"
664			"${VTX_OUT} ${MEDIUMP} vec2 v_texCoord;\n"
665			"void main (void)\n"
666			"{\n"
667			"\tv_texCoord = a_position.zw;\n"
668			"\tgl_Position = vec4(u_posScale * a_position.xy, 0.0, 1.0);\n"
669			"}";
670
671		default:
672			DE_ASSERT(false);
673			return NULL;
674	}
675
676}
677
678const char* MultiTextureSamplerTest::selectFragmentShader (GLenum target)
679{
680	switch (target)
681	{
682		case GL_TEXTURE_2D:
683			return
684			"${FRAG_HDR}"
685			"uniform ${LOWP} sampler2D u_sampler1;\n"
686			"uniform ${LOWP} sampler2D u_sampler2;\n"
687			"${FRAG_IN} ${MEDIUMP} vec2 v_texCoord;\n"
688			"void main (void)\n"
689			"{\n"
690			"\t${FRAG_COLOR} = vec4(0.75, 0.75, 0.75, 1.0) * (texture(u_sampler1, v_texCoord) + texture(u_sampler2, v_texCoord));\n"
691			"}";
692
693		case GL_TEXTURE_3D:
694			return
695			"${FRAG_HDR}"
696			"uniform ${LOWP} sampler3D u_sampler1;\n"
697			"uniform ${LOWP} sampler3D u_sampler2;\n"
698			"${FRAG_IN} ${MEDIUMP} vec3 v_texCoord;\n"
699			"void main (void)\n"
700			"{\n"
701			"\t${FRAG_COLOR} = vec4(0.75, 0.75, 0.75, 1.0) * (texture(u_sampler1, v_texCoord) + texture(u_sampler2, v_texCoord));\n"
702			"}";
703
704		case GL_TEXTURE_CUBE_MAP:
705			return
706			"${FRAG_HDR}"
707			"uniform ${LOWP} samplerCube u_sampler1;\n"
708			"uniform ${LOWP} samplerCube u_sampler2;\n"
709			"${FRAG_IN} ${MEDIUMP} vec2 v_texCoord;\n"
710			"void main (void)\n"
711			"{\n"
712			"\t${FRAG_COLOR} = vec4(0.5, 0.5, 0.5, 1.0) * (texture(u_sampler1, vec3(cos(3.14 * v_texCoord.y) * sin(3.14 * v_texCoord.x), sin(3.14 * v_texCoord.y), cos(3.14 * v_texCoord.y) * cos(3.14 * v_texCoord.x)))"
713			"+ texture(u_sampler2, vec3(cos(3.14 * v_texCoord.y) * sin(3.14 * v_texCoord.x), sin(3.14 * v_texCoord.y), cos(3.14 * v_texCoord.y) * cos(3.14 * v_texCoord.x))));\n"
714			"}";
715
716		default:
717			DE_ASSERT(false);
718			return NULL;
719	}
720
721}
722
723void MultiTextureSamplerTest::init (void)
724{
725	const char* vertexShaderTemplate	= selectVertexShader(m_target);
726	const char* fragmentShaderTemplate	= selectFragmentShader(m_target);
727
728	std::map<std::string, std::string>	params;
729
730	if (glu::isGLSLVersionSupported(m_renderCtx.getType(), glu::GLSL_VERSION_300_ES))
731	{
732		params["VTX_HDR"]		= "#version 300 es\n";
733		params["FRAG_HDR"]		= "#version 300 es\nlayout(location = 0) out mediump vec4 o_color;\n";
734		params["VTX_IN"]		= "in";
735		params["VTX_OUT"]		= "out";
736		params["FRAG_IN"]		= "in";
737		params["FRAG_COLOR"]	= "o_color";
738		params["HIGHP"]			= "highp";
739		params["LOWP"]			= "lowp";
740		params["MEDIUMP"]		= "mediump";
741	}
742	else if (glu::isGLSLVersionSupported(m_renderCtx.getType(), glu::GLSL_VERSION_330))
743	{
744		params["VTX_HDR"]		= "#version 330\n";
745		params["FRAG_HDR"]		= "#version 330\nlayout(location = 0) out mediump vec4 o_color;\n";
746		params["VTX_IN"]		= "in";
747		params["VTX_OUT"]		= "out";
748		params["FRAG_IN"]		= "in";
749		params["FRAG_COLOR"]	= "o_color";
750		params["HIGHP"]			= "highp";
751		params["LOWP"]			= "lowp";
752		params["MEDIUMP"]		= "mediump";
753	}
754	else
755		DE_ASSERT(false);
756
757	DE_ASSERT(!m_program);
758	m_program = new glu::ShaderProgram(m_renderCtx,
759									   glu::makeVtxFragSources(tcu::StringTemplate(vertexShaderTemplate).specialize(params),
760															   tcu::StringTemplate(fragmentShaderTemplate).specialize(params)));
761	if (!m_program->isOk())
762	{
763		tcu::TestLog& log = m_testCtx.getLog();
764
765		log << *m_program;
766		TCU_FAIL("Failed to compile shaders");
767	}
768}
769
770void MultiTextureSamplerTest::deinit (void)
771{
772	delete m_program;
773	m_program = NULL;
774}
775
776MultiTextureSamplerTest::~MultiTextureSamplerTest (void)
777{
778	deinit();
779}
780
781void MultiTextureSamplerTest::render (void)
782{
783	const glw::Functions& gl = m_renderCtx.getFunctions();
784
785	GLuint	samplerLoc1	= (GLuint)-1;
786	GLuint	samplerLoc2	= (GLuint)-1;
787	GLuint	scaleLoc	= (GLuint)-1;
788
789	gl.useProgram(m_program->getProgram());
790	GLU_EXPECT_NO_ERROR(gl.getError(), "glUseProgram(m_program->getProgram())");
791
792	samplerLoc1 = glGetUniformLocation(m_program->getProgram(), "u_sampler1");
793	TCU_CHECK(samplerLoc1 != (GLuint)-1);
794
795	samplerLoc2 = glGetUniformLocation(m_program->getProgram(), "u_sampler2");
796	TCU_CHECK(samplerLoc2 != (GLuint)-1);
797
798	scaleLoc = glGetUniformLocation(m_program->getProgram(), "u_posScale");
799	TCU_CHECK(scaleLoc != (GLuint)-1);
800
801	gl.clearColor(0.5f, 0.5f, 0.5f, 1.0f);
802	GLU_EXPECT_NO_ERROR(gl.getError(), "glClearColor(0.5f, 0.5f, 0.5f, 1.0f)");
803
804	gl.clear(GL_COLOR_BUFFER_BIT);
805	GLU_EXPECT_NO_ERROR(gl.getError(), "glClear(GL_COLOR_BUFFER_BIT)");
806
807	gl.uniform1i(samplerLoc1, 0);
808	GLU_EXPECT_NO_ERROR(gl.getError(), "glUniform1i(samplerLoc1, 0)");
809
810	gl.uniform1i(samplerLoc2, 1);
811	GLU_EXPECT_NO_ERROR(gl.getError(), "glUniform1i(samplerLoc2, 1)");
812
813	gl.uniform1f(scaleLoc, 1.0f);
814	GLU_EXPECT_NO_ERROR(gl.getError(), "glUniform1f(scaleLoc, 1.0f)");
815
816	switch (m_target)
817	{
818		case GL_TEXTURE_2D:
819		{
820			glu::VertexArrayBinding vertexArrays[] =
821			{
822				glu::VertexArrayBinding(glu::BindingPoint("a_position"), glu::VertexArrayPointer(glu::VTX_COMP_FLOAT, glu::VTX_COMP_CONVERT_NONE, 2, 6, 0, s_positions))
823			};
824
825			glu::draw(m_renderCtx, m_program->getProgram(), DE_LENGTH_OF_ARRAY(vertexArrays), vertexArrays, glu::PrimitiveList(glu::PRIMITIVETYPE_TRIANGLES, 6));
826
827			gl.uniform1f(scaleLoc, 0.25f);
828			GLU_EXPECT_NO_ERROR(gl.getError(), "glUniform1f(scaleLoc, 0.25f)");
829
830			glu::draw(m_renderCtx, m_program->getProgram(), DE_LENGTH_OF_ARRAY(vertexArrays), vertexArrays, glu::PrimitiveList(glu::PRIMITIVETYPE_TRIANGLES, 6));
831
832			break;
833		}
834
835		case GL_TEXTURE_3D:
836		{
837			glu::VertexArrayBinding vertexArrays[] =
838			{
839				glu::VertexArrayBinding(glu::BindingPoint("a_position"), glu::VertexArrayPointer(glu::VTX_COMP_FLOAT, glu::VTX_COMP_CONVERT_NONE, 3, 6, 0, s_positions3D))
840			};
841
842			glu::draw(m_renderCtx, m_program->getProgram(), DE_LENGTH_OF_ARRAY(vertexArrays), vertexArrays, glu::PrimitiveList(glu::PRIMITIVETYPE_TRIANGLES, 6));
843
844			gl.uniform1f(scaleLoc, 0.25f);
845			GLU_EXPECT_NO_ERROR(gl.getError(), "glUniform1f(scaleLoc, 0.25f)");
846
847			glu::draw(m_renderCtx, m_program->getProgram(), DE_LENGTH_OF_ARRAY(vertexArrays), vertexArrays, glu::PrimitiveList(glu::PRIMITIVETYPE_TRIANGLES, 6));
848
849			break;
850		}
851
852		case GL_TEXTURE_CUBE_MAP:
853		{
854			glu::VertexArrayBinding vertexArrays[] =
855			{
856				glu::VertexArrayBinding(glu::BindingPoint("a_position"), glu::VertexArrayPointer(glu::VTX_COMP_FLOAT, glu::VTX_COMP_CONVERT_NONE, 4, 6, 0, s_positionsCube))
857			};
858
859			glu::draw(m_renderCtx, m_program->getProgram(), DE_LENGTH_OF_ARRAY(vertexArrays), vertexArrays, glu::PrimitiveList(glu::PRIMITIVETYPE_TRIANGLES, 6));
860
861			gl.uniform1f(scaleLoc, 0.25f);
862			GLU_EXPECT_NO_ERROR(gl.getError(), "glUniform1f(scaleLoc, 0.25f)");
863
864			glu::draw(m_renderCtx, m_program->getProgram(), DE_LENGTH_OF_ARRAY(vertexArrays), vertexArrays, glu::PrimitiveList(glu::PRIMITIVETYPE_TRIANGLES, 6));
865
866			break;
867		}
868
869		default:
870			DE_ASSERT(false);
871	}
872}
873
874GLuint MultiTextureSamplerTest::createTexture2D (const glw::Functions& gl, int id)
875{
876	GLuint			texture		= (GLuint)-1;
877	tcu::Texture2D	refTexture	(tcu::TextureFormat(tcu::TextureFormat::RGBA, tcu::TextureFormat::UNORM_INT8), TEXTURE2D_WIDTH, TEXTURE2D_HEIGHT);
878
879	refTexture.allocLevel(0);
880
881	gl.genTextures(1, &texture);
882	GLU_EXPECT_NO_ERROR(gl.getError(), "glGenTextures(1, &texture)");
883
884	switch (id)
885	{
886		case 0:
887			tcu::fillWithComponentGradients(refTexture.getLevel(0), tcu::Vec4(0.0f, 0.0f, 0.0f, 0.0f), tcu::Vec4(1.0f, 1.0f, 0.5f, 0.5f));
888			break;
889
890		case 1:
891			tcu::fillWithComponentGradients(refTexture.getLevel(0), tcu::Vec4(0.0f, 0.0f, 0.0f, 0.0f), tcu::Vec4(0.5f, 0.5f, 1.0f, 1.0f));
892			break;
893
894		default:
895			DE_ASSERT(false);
896	}
897
898	gl.bindTexture(GL_TEXTURE_2D, texture);
899	GLU_EXPECT_NO_ERROR(gl.getError(), "glBindTexture(GL_TEXTURE_2D, texture)");
900
901	gl.texImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, refTexture.getWidth(), refTexture.getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, refTexture.getLevel(0).getDataPtr());
902	GLU_EXPECT_NO_ERROR(gl.getError(), "glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, refTexture.getWidth(), refTexture.getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, refTexture.getLevel(0).getDataPtr())");
903
904	gl.generateMipmap(GL_TEXTURE_2D);
905	GLU_EXPECT_NO_ERROR(gl.getError(), "glGenerateMipmap(GL_TEXTURE_2D)");
906
907	gl.bindTexture(GL_TEXTURE_2D, 0);
908	GLU_EXPECT_NO_ERROR(gl.getError(), "glBindTexture(GL_TEXTURE_2D, 0)");
909
910	return texture;
911}
912
913GLuint MultiTextureSamplerTest::createTexture3D (const glw::Functions& gl, int id)
914{
915	GLuint			texture		= (GLuint)-1;
916	tcu::Texture3D	refTexture	(tcu::TextureFormat(tcu::TextureFormat::RGBA, tcu::TextureFormat::UNORM_INT8), TEXTURE3D_WIDTH, TEXTURE3D_HEIGHT, TEXTURE3D_DEPTH);
917
918	refTexture.allocLevel(0);
919
920	gl.genTextures(1, &texture);
921	GLU_EXPECT_NO_ERROR(gl.getError(), "glGenTextures(1, &texture)");
922
923	switch (id)
924	{
925		case 0:
926			tcu::fillWithComponentGradients(refTexture.getLevel(0), tcu::Vec4(0.0f, 0.0f, 0.0f, 0.0f), tcu::Vec4(1.0f, 1.0f, 0.5f, 0.5f));
927			break;
928
929		case 1:
930			tcu::fillWithComponentGradients(refTexture.getLevel(0), tcu::Vec4(0.0f, 0.0f, 0.0f, 0.0f), tcu::Vec4(0.5f, 0.5f, 1.0f, 1.0f));
931			break;
932
933		default:
934			DE_ASSERT(false);
935	}
936
937	gl.bindTexture(GL_TEXTURE_3D, texture);
938	GLU_EXPECT_NO_ERROR(gl.getError(), "glBindTexture(GL_TEXTURE_3D, texture)");
939
940	gl.texImage3D(GL_TEXTURE_3D, 0, GL_RGBA8, refTexture.getWidth(), refTexture.getHeight(), refTexture.getDepth(), 0, GL_RGBA, GL_UNSIGNED_BYTE, refTexture.getLevel(0).getDataPtr());
941	GLU_EXPECT_NO_ERROR(gl.getError(), "glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA8, refTexture.getWidth(), refTexture.getHeight(), refTexture.getDepth(), 0, GL_RGBA, GL_UNSIGNED_BYTE, refTexture.getLevel(0).getDataPtr())");
942
943	gl.generateMipmap(GL_TEXTURE_3D);
944	GLU_EXPECT_NO_ERROR(gl.getError(), "glGenerateMipmap(GL_TEXTURE_3D)");
945
946	gl.bindTexture(GL_TEXTURE_3D, 0);
947	GLU_EXPECT_NO_ERROR(gl.getError(), "glBindTexture(GL_TEXTURE_3D, 0)");
948
949	return texture;
950}
951
952GLuint MultiTextureSamplerTest::createTextureCube (const glw::Functions& gl, int id)
953{
954	GLuint				texture		= (GLuint)-1;
955	tcu::TextureCube	refTexture	(tcu::TextureFormat(tcu::TextureFormat::RGBA, tcu::TextureFormat::UNORM_INT8), CUBEMAP_SIZE);
956
957	gl.genTextures(1, &texture);
958	GLU_EXPECT_NO_ERROR(gl.getError(), "glGenTextures(1, &texture)");
959
960	refTexture.allocLevel(tcu::CUBEFACE_POSITIVE_X, 0);
961	refTexture.allocLevel(tcu::CUBEFACE_POSITIVE_Y, 0);
962	refTexture.allocLevel(tcu::CUBEFACE_POSITIVE_Z, 0);
963	refTexture.allocLevel(tcu::CUBEFACE_NEGATIVE_X, 0);
964	refTexture.allocLevel(tcu::CUBEFACE_NEGATIVE_Y, 0);
965	refTexture.allocLevel(tcu::CUBEFACE_NEGATIVE_Z, 0);
966
967	switch (id)
968	{
969		case 0:
970			tcu::fillWithComponentGradients(refTexture.getLevelFace(0, tcu::CUBEFACE_POSITIVE_X), tcu::Vec4(0.0f, 0.0f, 0.0f, 0.0f), tcu::Vec4(0.5f, 0.5f, 0.5f, 0.5f));
971			tcu::fillWithComponentGradients(refTexture.getLevelFace(0, tcu::CUBEFACE_POSITIVE_Y), tcu::Vec4(0.0f, 0.0f, 0.0f, 0.0f), tcu::Vec4(0.5f, 0.5f, 0.5f, 0.5f));
972			tcu::fillWithComponentGradients(refTexture.getLevelFace(0, tcu::CUBEFACE_POSITIVE_Z), tcu::Vec4(0.0f, 0.0f, 0.0f, 0.0f), tcu::Vec4(0.5f, 0.5f, 0.5f, 0.5f));
973			tcu::fillWithComponentGradients(refTexture.getLevelFace(0, tcu::CUBEFACE_NEGATIVE_X), tcu::Vec4(0.0f, 0.0f, 0.0f, 0.0f), tcu::Vec4(0.5f, 0.5f, 0.5f, 0.5f));
974			tcu::fillWithComponentGradients(refTexture.getLevelFace(0, tcu::CUBEFACE_NEGATIVE_Y), tcu::Vec4(0.0f, 0.0f, 0.0f, 0.0f), tcu::Vec4(0.5f, 0.5f, 0.5f, 0.5f));
975			tcu::fillWithComponentGradients(refTexture.getLevelFace(0, tcu::CUBEFACE_NEGATIVE_Z), tcu::Vec4(0.0f, 0.0f, 0.0f, 0.0f), tcu::Vec4(0.5f, 0.5f, 0.5f, 0.5f));
976			break;
977
978		case 1:
979			tcu::fillWithComponentGradients(refTexture.getLevelFace(0, tcu::CUBEFACE_POSITIVE_X), tcu::Vec4(0.5f, 0.5f, 0.5f, 0.5f), tcu::Vec4(1.0f, 1.0f, 1.0f, 1.0f));
980			tcu::fillWithComponentGradients(refTexture.getLevelFace(0, tcu::CUBEFACE_POSITIVE_Y), tcu::Vec4(0.5f, 0.5f, 0.5f, 0.5f), tcu::Vec4(1.0f, 1.0f, 1.0f, 1.0f));
981			tcu::fillWithComponentGradients(refTexture.getLevelFace(0, tcu::CUBEFACE_POSITIVE_Z), tcu::Vec4(0.5f, 0.5f, 0.5f, 0.5f), tcu::Vec4(1.0f, 1.0f, 1.0f, 1.0f));
982			tcu::fillWithComponentGradients(refTexture.getLevelFace(0, tcu::CUBEFACE_NEGATIVE_X), tcu::Vec4(0.5f, 0.5f, 0.5f, 0.5f), tcu::Vec4(1.0f, 1.0f, 1.0f, 1.0f));
983			tcu::fillWithComponentGradients(refTexture.getLevelFace(0, tcu::CUBEFACE_NEGATIVE_Y), tcu::Vec4(0.5f, 0.5f, 0.5f, 0.5f), tcu::Vec4(1.0f, 1.0f, 1.0f, 1.0f));
984			tcu::fillWithComponentGradients(refTexture.getLevelFace(0, tcu::CUBEFACE_NEGATIVE_Z), tcu::Vec4(0.5f, 0.5f, 0.5f, 0.5f), tcu::Vec4(1.0f, 1.0f, 1.0f, 1.0f));
985			break;
986
987		default:
988			DE_ASSERT(false);
989	}
990
991	gl.bindTexture(GL_TEXTURE_CUBE_MAP, texture);
992	GLU_EXPECT_NO_ERROR(gl.getError(), "glBindTexture(GL_TEXTURE_CUBE_MAP, texture)");
993
994	for (int face = 0; face < tcu::CUBEFACE_LAST; face++)
995	{
996		const deUint32 target = glu::getGLCubeFace((tcu::CubeFace)face);
997		gl.texImage2D(target, 0, GL_RGBA8, refTexture.getSize(), refTexture.getSize(), 0, GL_RGBA, GL_UNSIGNED_BYTE, refTexture.getLevelFace(0, (tcu::CubeFace)face).getDataPtr());
998	}
999	GLU_EXPECT_NO_ERROR(gl.getError(), "glTexImage2D(GL_TEXTURE_CUBE_MAP_...) failed");
1000
1001	gl.generateMipmap(GL_TEXTURE_CUBE_MAP);
1002	GLU_EXPECT_NO_ERROR(gl.getError(), "glGenerateMipmap(GL_TEXTURE_CUBE_MAP)");
1003	gl.bindTexture(GL_TEXTURE_CUBE_MAP, 0);
1004	GLU_EXPECT_NO_ERROR(gl.getError(), "glBindTexture(GL_TEXTURE_CUBE_MAP, 0)");
1005
1006	return texture;
1007}
1008
1009GLuint MultiTextureSamplerTest::createTexture (const glw::Functions& gl, GLenum target, int id)
1010{
1011	switch (target)
1012	{
1013		case GL_TEXTURE_2D:
1014			return createTexture2D(gl, id);
1015
1016		case GL_TEXTURE_3D:
1017			return createTexture3D(gl, id);
1018
1019		case GL_TEXTURE_CUBE_MAP:
1020			return createTextureCube(gl, id);
1021
1022		default:
1023			DE_ASSERT(false);
1024			return (GLuint)-1;
1025	}
1026}
1027
1028void MultiTextureSamplerTest::renderReferences (tcu::Surface& textureRef, tcu::Surface& samplerRef, int x, int y)
1029{
1030	const glw::Functions&	gl			= m_renderCtx.getFunctions();
1031	GLuint					texture1	= createTexture(gl, m_target, 0);
1032	GLuint					texture2	= createTexture(gl, m_target, 1);
1033
1034	gl.viewport(x, y, VIEWPORT_WIDTH, VIEWPORT_HEIGHT);
1035	GLU_EXPECT_NO_ERROR(gl.getError(), "glViewport(x, y, VIEWPORT_WIDTH, VIEWPORT_HEIGHT)");
1036
1037	// Generate texture rendering reference
1038	gl.activeTexture(GL_TEXTURE0);
1039	GLU_EXPECT_NO_ERROR(gl.getError(), "glActiveTexture(GL_TEXTURE0)");
1040	gl.bindTexture(m_target, texture1);
1041	GLU_EXPECT_NO_ERROR(gl.getError(), "glBindTexture(m_target, texture1)");
1042	setTextureState(gl, m_target, m_textureState1);
1043
1044	gl.activeTexture(GL_TEXTURE1);
1045	GLU_EXPECT_NO_ERROR(gl.getError(), "glActiveTexture(GL_TEXTURE1)");
1046	gl.bindTexture(m_target, texture2);
1047	GLU_EXPECT_NO_ERROR(gl.getError(), "glBindTexture(m_target, texture2)");
1048	setTextureState(gl, m_target, m_textureState2);
1049
1050	render();
1051	glu::readPixels(m_renderCtx, x, y, textureRef.getAccess());
1052
1053	// Generate sampler rendering reference
1054	gl.activeTexture(GL_TEXTURE0);
1055	GLU_EXPECT_NO_ERROR(gl.getError(), "glActiveTexture(GL_TEXTURE0)");
1056	gl.bindTexture(m_target, texture1);
1057	GLU_EXPECT_NO_ERROR(gl.getError(), "glBindTexture(m_target, texture1)");
1058	setTextureState(gl, m_target, m_samplerState);
1059
1060	gl.activeTexture(GL_TEXTURE1);
1061	GLU_EXPECT_NO_ERROR(gl.getError(), "glActiveTexture(GL_TEXTURE1)");
1062	gl.bindTexture(m_target, texture2);
1063	GLU_EXPECT_NO_ERROR(gl.getError(), "glBindTexture(m_target, texture2)");
1064	setTextureState(gl, m_target, m_samplerState);
1065
1066	render();
1067	glu::readPixels(m_renderCtx, x, y, samplerRef.getAccess());
1068}
1069
1070void MultiTextureSamplerTest::renderResults (tcu::Surface& textureResult, tcu::Surface& samplerResult, int x, int y)
1071{
1072	const glw::Functions&	gl			= m_renderCtx.getFunctions();
1073	GLuint					texture1	= createTexture(gl, m_target, 0);
1074	GLuint					texture2	= createTexture(gl, m_target, 1);
1075	GLuint					sampler		= -1;
1076
1077	gl.viewport(x, y, VIEWPORT_WIDTH, VIEWPORT_HEIGHT);
1078	GLU_EXPECT_NO_ERROR(gl.getError(), "glViewport(x, y, VIEWPORT_WIDTH, VIEWPORT_HEIGHT)");
1079
1080	gl.genSamplers(1, &sampler);
1081	GLU_EXPECT_NO_ERROR(gl.getError(), "glGenSamplers(1, &sampler)");
1082	TCU_CHECK(sampler != (GLuint)-1);
1083
1084	gl.bindSampler(0, sampler);
1085	GLU_EXPECT_NO_ERROR(gl.getError(), "glBindSampler(0, sampler)");
1086	gl.bindSampler(1, sampler);
1087	GLU_EXPECT_NO_ERROR(gl.getError(), "glBindSampler(1, sampler)");
1088
1089	// First set sampler state
1090	setSamplerState(gl, m_samplerState, sampler);
1091
1092	// Set texture state
1093	gl.bindTexture(m_target, texture1);
1094	GLU_EXPECT_NO_ERROR(gl.getError(), "glBindTexture(m_target, texture1)");
1095	setTextureState(gl, m_target, m_textureState1);
1096
1097	gl.bindTexture(m_target, texture2);
1098	GLU_EXPECT_NO_ERROR(gl.getError(), "glBindTexture(m_target, texture2)");
1099	setTextureState(gl, m_target, m_textureState2);
1100
1101	gl.activeTexture(GL_TEXTURE0);
1102	GLU_EXPECT_NO_ERROR(gl.getError(), "glActiveTexture(GL_TEXTURE0)");
1103	gl.bindTexture(m_target, texture1);
1104	GLU_EXPECT_NO_ERROR(gl.getError(), "glBindTexture(m_target, texture1)");
1105
1106	gl.activeTexture(GL_TEXTURE1);
1107	GLU_EXPECT_NO_ERROR(gl.getError(), "glActiveTexture(GL_TEXTURE1)");
1108	gl.bindTexture(m_target, texture2);
1109	GLU_EXPECT_NO_ERROR(gl.getError(), "glBindTexture(m_target, texture2)");
1110
1111	// Render using sampler
1112	render();
1113	glu::readPixels(m_renderCtx, x, y, samplerResult.getAccess());
1114
1115	gl.bindSampler(0, 0);
1116	GLU_EXPECT_NO_ERROR(gl.getError(), "glBindSampler(0, 0)");
1117	gl.bindSampler(1, 0);
1118	GLU_EXPECT_NO_ERROR(gl.getError(), "glBindSampler(1, 0)");
1119
1120	render();
1121	glu::readPixels(m_renderCtx, x, y, textureResult.getAccess());
1122
1123	gl.activeTexture(GL_TEXTURE0);
1124	GLU_EXPECT_NO_ERROR(gl.getError(), "glActiveTexture(GL_TEXTURE0)");
1125	gl.bindTexture(m_target, 0);
1126	GLU_EXPECT_NO_ERROR(gl.getError(), "glBindTexture(m_target, 0)");
1127
1128	gl.activeTexture(GL_TEXTURE1);
1129	GLU_EXPECT_NO_ERROR(gl.getError(), "glActiveTexture(GL_TEXTURE1)");
1130	gl.bindTexture(m_target, 0);
1131	GLU_EXPECT_NO_ERROR(gl.getError(), "glBindTexture(m_target, 0)");
1132
1133	gl.deleteSamplers(1, &sampler);
1134	GLU_EXPECT_NO_ERROR(gl.getError(), "glDeleteSamplers(1, &sampler)");
1135	gl.deleteTextures(1, &texture1);
1136	GLU_EXPECT_NO_ERROR(gl.getError(), "glDeleteTextures(1, &texture1)");
1137	gl.deleteTextures(1, &texture2);
1138	GLU_EXPECT_NO_ERROR(gl.getError(), "glDeleteTextures(1, &texture2)");
1139}
1140
1141tcu::TestCase::IterateResult MultiTextureSamplerTest::iterate (void)
1142{
1143	tcu::TestLog&	log = m_testCtx.getLog();
1144
1145	tcu::Surface	textureRef(VIEWPORT_WIDTH, VIEWPORT_HEIGHT);
1146	tcu::Surface	samplerRef(VIEWPORT_WIDTH, VIEWPORT_HEIGHT);
1147
1148	tcu::Surface	textureResult(VIEWPORT_WIDTH, VIEWPORT_HEIGHT);
1149	tcu::Surface	samplerResult(VIEWPORT_WIDTH, VIEWPORT_HEIGHT);
1150
1151	int				x = m_random.getInt(0, m_renderCtx.getRenderTarget().getWidth() - VIEWPORT_WIDTH);
1152	int				y = m_random.getInt(0, m_renderCtx.getRenderTarget().getHeight() - VIEWPORT_HEIGHT);
1153
1154	renderReferences(textureRef, samplerRef, x, y);
1155	renderResults(textureResult, samplerResult, x, y);
1156
1157	bool isOk = pixelThresholdCompare (log, "Sampler render result", "Result from rendering with sampler", samplerRef, samplerResult, tcu::RGBA(0, 0, 0, 0), tcu::COMPARE_LOG_RESULT);
1158
1159	if (!pixelThresholdCompare (log, "Texture render result", "Result from rendering with texture state", textureRef, textureResult, tcu::RGBA(0, 0, 0, 0), tcu::COMPARE_LOG_RESULT))
1160		isOk = false;
1161
1162	if (!isOk)
1163	{
1164		m_testCtx.setTestResult(QP_TEST_RESULT_FAIL, "Fail");
1165		return STOP;
1166	}
1167
1168	m_testCtx.setTestResult(QP_TEST_RESULT_PASS, "Pass");
1169	return STOP;
1170}
1171
1172
1173} // gls
1174} // deqp
1175