Source

GL Profile Suite / tests / util / globj.h

Full commit

#ifndef PERFORMANCE_OPENGL_OBJECTS_H
#define PERFORMANCE_OPENGL_OBJECTS_H

#include <vector>
#include <glload/gl_all.h>

namespace util
{
	GLuint CreateBufferObject();
	std::vector<GLuint> CreateBufferObjects(int numBuffers);
	void DestroyBufferObjects(const std::vector<GLuint> &buffers);

	std::vector<GLuint> CreateVertexArrays(int numArrays);
	void DestroyVertexArrays(const std::vector<GLuint> &arrays);

	GLuint CreateQuery();
	std::vector<GLuint> CreateQueries(int numQueries);
	void DestroyQueries(const std::vector<GLuint> &queries);

	std::vector<GLuint> CreatePrograms(int numPrograms);
	void DestroyPrograms(const std::vector<GLuint> &programs);

	inline std::vector<char> RepeatBuffer(const std::vector<char> &buffer, const int numRepetitions)
	{
		std::vector<char> ret;
		ret.resize(buffer.size() * numRepetitions);
		for(int loop = 0; loop < numRepetitions; ++loop)
			memcpy(&ret[loop * (buffer.size())], &buffer[0], buffer.size());
/*
		ret.reserve(buffer.size() * numRepetitions);
		for(int loop = 0; loop < numRepetitions; ++loop)
			ret.insert(ret.end(), buffer.begin(), buffer.end());
*/

		return ret;
	}

	template<typename T>
	void FillBufferData(GLuint buf, GLenum target, const std::vector<T> &data, GLenum usage)
	{
		glBindBuffer(target, buf);
		glBufferData(target, data.size() * sizeof(T), &data[0], usage);
		glBindBuffer(target, 0);
	}

	template<typename T>
	size_t CalcOffset(const std::vector<T> &data)
	{
		return sizeof(T) * data.size();
	}

	template<typename T>
	size_t FillBufferSubData(size_t offset, GLuint buf, GLenum target, const std::vector<T> &data)
	{
		glBindBuffer(target, buf);
		glBufferSubData(target, offset, data.size() * sizeof(T), &data[0]);
		glBindBuffer(target, 0);

		return offset + (data.size() * sizeof(T));
	}

	//Uses the currently bound buffer.
	template<typename T>
	size_t FillBufferSubData(size_t offset, GLenum target, const std::vector<T> &data)
	{
		glBufferSubData(target, offset, data.size() * sizeof(T), &data[0]);

		return offset + (data.size() * sizeof(T));
	}

	template<typename T>
	void AddToInterleaveBuffer(std::vector<char> &byteBuffer, T value, const int repeat)
	{
		size_t elementSize = (((repeat * sizeof(T)) + 4 - 1) / 4) * 4;
		size_t currLoc = byteBuffer.size();
		byteBuffer.insert(byteBuffer.end(), elementSize, 0);

		char *buffPtr = &byteBuffer[currLoc];

		for(int loop = 0; loop < repeat; ++loop)
		{
			*reinterpret_cast<T*>(buffPtr) = value;
			buffPtr += sizeof(T);
		}
	}

}

#endif //PERFORMANCE_OPENGL_OBJECTS_H