1/*-------------------------------------------------------------------------
2 * drawElements Quality Program OpenGL Utilities
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 Program interface query utilities
22 *//*--------------------------------------------------------------------*/
23
24#include "gluProgramInterfaceQuery.hpp"
25#include "glwFunctions.hpp"
26#include "glwEnums.hpp"
27
28#include <sstream>
29
30namespace glu
31{
32
33deUint32 getProgramResourceUint (const glw::Functions& gl, deUint32 program, deUint32 programInterface, deUint32 index, deUint32 queryParam)
34{
35	deUint32 value = 0;
36	gl.getProgramResourceiv(program, programInterface, index, 1, &queryParam, 1, DE_NULL, (int*)&value);
37	GLU_EXPECT_NO_ERROR(gl.getError(), "glGetProgramResourceiv()");
38	return value;
39}
40
41void getProgramResourceName (const glw::Functions& gl, deUint32 program, deUint32 programInterface, deUint32 index, std::string& dst)
42{
43	const int length = getProgramResourceInt(gl, program, programInterface, index, GL_NAME_LENGTH);
44
45	if (length > 0)
46	{
47		std::vector<char> buf(length+1);
48		gl.getProgramResourceName(program, programInterface, index, (int)buf.size(), DE_NULL, &buf[0]);
49		GLU_EXPECT_NO_ERROR(gl.getError(), "glGetProgramResourceName()");
50
51		dst = (const char*)&buf[0];
52	}
53	else
54	{
55		std::ostringstream msg;
56		msg << "Empty name returned for " << programInterface << " at index " << index;
57		throw tcu::TestError(msg.str());
58	}
59}
60
61static void getProgramInterfaceActiveVariables (const glw::Functions& gl, deUint32 program, deUint32 programInterface, deUint32 index, std::vector<int>& activeVariables)
62{
63	const int numActiveVariables = getProgramResourceInt(gl, program, programInterface, index, GL_NUM_ACTIVE_VARIABLES);
64
65	activeVariables.resize(numActiveVariables);
66	if (numActiveVariables > 0)
67	{
68		const deUint32 queryParam = GL_ACTIVE_VARIABLES;
69		gl.getProgramResourceiv(program, programInterface, index, 1, &queryParam, (int)activeVariables.size(), DE_NULL, &activeVariables[0]);
70		GLU_EXPECT_NO_ERROR(gl.getError(), "glGetProgramResourceiv(GL_PROGRAM_ACTIVE_VARIABLES)");
71	}
72}
73
74void getProgramInterfaceBlockInfo (const glw::Functions& gl, deUint32 program, deUint32 programInterface, deUint32 index, InterfaceBlockInfo& info)
75{
76	info.index			= index;
77	info.bufferBinding	= getProgramResourceUint(gl, program, programInterface, index, GL_BUFFER_BINDING);
78	info.dataSize		= getProgramResourceUint(gl, program, programInterface, index, GL_BUFFER_DATA_SIZE);
79
80	getProgramInterfaceActiveVariables(gl, program, programInterface, index, info.activeVariables);
81
82	if (programInterface != GL_ATOMIC_COUNTER_BUFFER)
83		getProgramResourceName(gl, program, programInterface, index, info.name);
84}
85
86void getProgramInterfaceVariableInfo (const glw::Functions& gl, deUint32 program, deUint32 programInterface, deUint32 index, InterfaceVariableInfo& info)
87{
88	// \todo [2013-08-27 pyry] Batch queries!
89	info.index			= index;
90	info.blockIndex		= getProgramResourceUint(gl, program, programInterface, index, GL_BLOCK_INDEX);
91	info.type			= getProgramResourceUint(gl, program, programInterface, index, GL_TYPE);
92	info.arraySize		= getProgramResourceUint(gl, program, programInterface, index, GL_ARRAY_SIZE);
93	info.offset			= getProgramResourceUint(gl, program, programInterface, index, GL_OFFSET);
94	info.arrayStride	= getProgramResourceUint(gl, program, programInterface, index, GL_ARRAY_STRIDE);
95	info.matrixStride	= getProgramResourceUint(gl, program, programInterface, index, GL_MATRIX_STRIDE);
96	info.isRowMajor		= getProgramResourceUint(gl, program, programInterface, index, GL_IS_ROW_MAJOR) != GL_FALSE;
97
98	if (programInterface == GL_UNIFORM)
99		info.atomicCounterBufferIndex = getProgramResourceUint(gl, program, programInterface, index, GL_ATOMIC_COUNTER_BUFFER_INDEX);
100
101	if (programInterface == GL_BUFFER_VARIABLE)
102	{
103		info.topLevelArraySize		= getProgramResourceUint(gl, program, programInterface, index, GL_TOP_LEVEL_ARRAY_SIZE);
104		info.topLevelArrayStride	= getProgramResourceUint(gl, program, programInterface, index, GL_TOP_LEVEL_ARRAY_STRIDE);
105	}
106
107	getProgramResourceName(gl, program, programInterface, index, info.name);
108}
109
110} // glu
111