123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415 |
- #include "rendering/GL.h"
- #include "GL/glew.h"
- #include "data/Array.h"
- #include "utils/Logger.h"
- #include "utils/Utility.h"
- static_assert(Core::IsSame<GL::Shader, GLuint>, "shader has invalid type");
- static_assert(Core::IsSame<GL::Program, GLuint>, "p has invalid type");
- static_assert(Core::IsSame<char, GLchar>, "char has invalid type");
- static_assert(Core::IsSame<int, GLint>, "int has invalid type");
- static_assert(Core::IsSame<float, GLfloat>, "float has invalid type");
- static_assert(Core::IsSame<GL::Texture, GLuint>, "texture has invalid type");
- static_assert(Core::IsSame<GL::Framebuffer, GLuint>,
- "framebuffer has invalid type");
- static_assert(Core::IsSame<GL::ColorAttachment, GLenum>,
- "color attachment has invalid type");
- static_assert(Core::IsSame<GL::VertexArray, GLuint>,
- "vertex array has invalid type");
- static_assert(Core::IsSame<GL::Buffer, GLuint>, "buffer has invalid type");
- GL::Attribute::Attribute(int count_, int size_, int type_, bool normalized_)
- : count(count_), size(size_), type(type_), normalized(normalized_) {
- }
- GL::Attribute GL::Attribute::newFloat(int count) {
- return GL::Attribute(count, sizeof(float), GL_FLOAT, false);
- }
- GL::Attribute GL::Attribute::newColor(int count) {
- return GL::Attribute(count, sizeof(unsigned char), GL_UNSIGNED_BYTE, true);
- }
- GL::Attribute GL::Attribute::newDummy() {
- return GL::Attribute(0, 0, -1, false);
- }
- bool GL::Attribute::isDummy() const {
- return type == -1;
- }
- int GL::Attribute::getSize() const {
- return count * size;
- }
- GL::TextureFormat::TextureFormat(int internalformat_, int format_, int type_)
- : internalformat(internalformat_), format(format_), type(type_) {
- }
- GL::TextureFormat GL::TextureFormat::color8(int channels) {
- switch(channels) {
- case 1: return TextureFormat(GL_RED, GL_RED, GL_UNSIGNED_BYTE);
- case 2: return TextureFormat(GL_RG, GL_RG, GL_UNSIGNED_BYTE);
- case 3: return TextureFormat(GL_RGB, GL_RGB, GL_UNSIGNED_BYTE);
- case 4: return TextureFormat(GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE);
- }
- LOG_ERROR(StringBuffer<50>(channels).append(
- " is not a valid amount of channels"));
- return unknown();
- }
- GL::TextureFormat GL::TextureFormat::float16(int channels) {
- switch(channels) {
- case 1: return TextureFormat(GL_R16F, GL_RED, GL_FLOAT);
- case 2: return TextureFormat(GL_RG16F, GL_RG, GL_FLOAT);
- case 3: return TextureFormat(GL_RGB16F, GL_RGB, GL_FLOAT);
- case 4: return TextureFormat(GL_RGBA16F, GL_RGBA, GL_FLOAT);
- }
- LOG_ERROR(StringBuffer<50>(channels).append(
- " is not a valid amount of channels"));
- return unknown();
- }
- GL::TextureFormat GL::TextureFormat::float32(int channels) {
- switch(channels) {
- case 1: return TextureFormat(GL_R32F, GL_RED, GL_FLOAT);
- case 2: return TextureFormat(GL_RG32F, GL_RG, GL_FLOAT);
- case 3: return TextureFormat(GL_RGB32F, GL_RGB, GL_FLOAT);
- case 4: return TextureFormat(GL_RGBA32F, GL_RGBA, GL_FLOAT);
- }
- LOG_ERROR(StringBuffer<50>(channels).append(
- " is not a valid amount of channels"));
- return unknown();
- }
- GL::TextureFormat GL::TextureFormat::depth16() {
- return TextureFormat(GL_DEPTH_COMPONENT16, GL_DEPTH_COMPONENT, GL_FLOAT);
- }
- GL::TextureFormat GL::TextureFormat::depth32() {
- return TextureFormat(GL_DEPTH_COMPONENT32, GL_DEPTH_COMPONENT, GL_FLOAT);
- }
- GL::TextureFormat GL::TextureFormat::unknown() {
- return TextureFormat(-1, -1, -1);
- }
- bool GL::printError(const char* message) {
- GLenum error = glGetError();
- if(error != GL_NO_ERROR) {
- (void)message;
- LOG_ERROR(StringBuffer<100>(message).append(": ").append(error));
- return true;
- }
- return false;
- }
- Error GL::getError(const char* message) {
- GLenum error = glGetError();
- if(error != GL_NO_ERROR) {
- Error e = {message};
- e.message.append(": ").append(error);
- return e;
- }
- return {};
- }
- void GL::enableDepthTesting() {
- glEnable(GL_DEPTH_TEST);
- }
- void GL::disableDepthTesting() {
- glDisable(GL_DEPTH_TEST);
- }
- void GL::bindMainFramebuffer() {
- glBindFramebuffer(GL_FRAMEBUFFER, 0);
- }
- void GL::clear() {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- }
- void GL::enableBlending() {
- glEnable(GL_BLEND);
- glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
- glBlendEquation(GL_FUNC_ADD);
- }
- void GL::disableBlending() {
- glDisable(GL_BLEND);
- }
- void GL::setViewport(int width, int height) {
- glViewport(0, 0, width, height);
- }
- void GL::vertexAttribPointer(int index, const Attribute& a, int stride,
- int offset) {
- glVertexAttribPointer(static_cast<GLuint>(index), a.count,
- static_cast<GLenum>(a.type), a.normalized, stride,
- static_cast<char*>(0) + offset);
- glEnableVertexAttribArray(static_cast<GLuint>(index));
- }
- GL::Program GL::createProgram() {
- return glCreateProgram();
- }
- void GL::attachShader(Program p, Shader s) {
- glAttachShader(p, s);
- }
- void GL::linkProgram(Program p) {
- glLinkProgram(p);
- }
- Error GL::getLinkerError(Program p) {
- GLint linked;
- glGetProgramiv(p, GL_LINK_STATUS, &linked);
- if(!linked) {
- Array<char, 256> log;
- glGetProgramInfoLog(p, log.getLength(), nullptr, log.begin());
- Error e = {"linker log: "};
- e.message.append(static_cast<const char*>(log.begin()));
- return e;
- }
- return {};
- }
- void GL::deleteShader(Shader s) {
- if(s != 0) {
- glDeleteShader(s);
- }
- }
- void GL::deleteProgram(Program p) {
- if(p != 0) {
- glDeleteProgram(p);
- }
- }
- GL::Shader GL::createShader(ShaderType type) {
- static constexpr GLenum MAP[] = {0,
- GL_VERTEX_SHADER,
- GL_FRAGMENT_SHADER,
- GL_GEOMETRY_SHADER,
- GL_TESS_CONTROL_SHADER,
- GL_TESS_EVALUATION_SHADER};
- static constexpr int MAP_SIZE =
- static_cast<int>(sizeof(MAP) / sizeof(GLenum));
- int index = static_cast<int>(type);
- GLenum realTyp = index < 0 || index >= MAP_SIZE ? MAP[0] : MAP[index];
- return glCreateShader(realTyp);
- }
- void GL::compileShader(Shader s, const char* code) {
- glShaderSource(s, 1, &code, nullptr);
- glCompileShader(s);
- }
- Error GL::getCompileError(Shader s) {
- GLint compiled;
- glGetShaderiv(s, GL_COMPILE_STATUS, &compiled);
- if(!compiled) {
- Array<char, 256> log;
- glGetShaderInfoLog(s, log.getLength(), nullptr, log.begin());
- Error e = {"compiler log: "};
- e.message.append(static_cast<const char*>(log.begin()));
- return e;
- }
- return {};
- }
- void GL::useProgram(Program p) {
- glUseProgram(p);
- }
- void GL::setMatrix(Program p, const char* name, const float* data) {
- glUniformMatrix4fv(glGetUniformLocation(p, name), 1, GL_TRUE, data);
- }
- void GL::setInt(Program p, const char* name, int data) {
- glUniform1i(glGetUniformLocation(p, name), data);
- }
- void GL::setFloat(Program p, const char* name, float data) {
- glUniform1f(glGetUniformLocation(p, name), data);
- }
- void GL::set2Float(Program p, const char* name, const float* data) {
- glUniform2fv(glGetUniformLocation(p, name), 1, data);
- }
- void GL::set3Float(Program p, const char* name, const float* data) {
- glUniform3fv(glGetUniformLocation(p, name), 1, data);
- }
- void GL::set4Float(Program p, const char* name, const float* data) {
- glUniform4fv(glGetUniformLocation(p, name), 1, data);
- }
- void GL::texImage2D(const TextureFormat& tf, int width, int height,
- const void* data, int level) {
- glTexImage2D(GL_TEXTURE_2D, level, tf.internalformat, width, height, 0,
- static_cast<GLenum>(tf.format), static_cast<GLenum>(tf.type),
- data);
- }
- GL::Texture GL::genTexture() {
- Texture t;
- glGenTextures(1, &t);
- return t;
- }
- void GL::deleteTexture(Texture t) {
- if(t != 0) {
- glDeleteTextures(1, &t);
- }
- }
- void GL::setNearFilter2D() {
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
- }
- void GL::setMipMapNearFilter2D() {
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
- GL_NEAREST_MIPMAP_LINEAR);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
- }
- void GL::setLinearFilter2D() {
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
- }
- void GL::setMipMapLinearFilter2D() {
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
- GL_LINEAR_MIPMAP_LINEAR);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
- }
- void GL::setClampWrap2D() {
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
- }
- void GL::setRepeatWrap2D() {
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
- }
- void GL::bindTexture2D(Texture t) {
- glBindTexture(GL_TEXTURE_2D, t);
- }
- void GL::activeTexture(int index) {
- glActiveTexture(static_cast<GLenum>(GL_TEXTURE0 + index));
- }
- void GL::generateMipmap2D(int maxLevels) {
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, maxLevels);
- glGenerateMipmap(GL_TEXTURE_2D);
- }
- void GL::deleteFramebuffers(Framebuffer fb) {
- if(fb != 0) {
- glDeleteFramebuffers(1, &fb);
- }
- }
- GL::Framebuffer GL::genFramebuffer() {
- Framebuffer fb;
- glGenFramebuffers(1, &fb);
- return fb;
- }
- void GL::bindFramebuffer(Framebuffer fb) {
- glBindFramebuffer(GL_FRAMEBUFFER, fb);
- }
- void GL::framebufferDepthTexture2D(Texture t) {
- glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D,
- t, 0);
- }
- GL::ColorAttachment GL::framebufferColorTexture2D(Texture t, int index) {
- GLenum c = static_cast<GLenum>(GL_COLOR_ATTACHMENT0 + index);
- glFramebufferTexture2D(GL_FRAMEBUFFER, c, GL_TEXTURE_2D, t, 0);
- return c;
- }
- void GL::drawBuffers(int length, ColorAttachment* c) {
- glDrawBuffers(length, c);
- }
- Error GL::getFramebufferError() {
- GLenum error = glCheckFramebufferStatus(GL_FRAMEBUFFER);
- if(error != GL_FRAMEBUFFER_COMPLETE) {
- Error e = {"framebuffer error: "};
- e.message.append(error);
- return e;
- }
- return {};
- }
- GL::VertexArray GL::genVertexArray() {
- VertexArray va;
- glGenVertexArrays(1, &va);
- return va;
- }
- GL::Buffer GL::genBuffer() {
- Buffer b;
- glGenBuffers(1, &b);
- return b;
- }
- void GL::deleteBuffer(Buffer b) {
- if(b != 0) {
- glDeleteBuffers(1, &b);
- }
- }
- void GL::deleteVertexArray(VertexArray va) {
- if(va != 0) {
- glDeleteVertexArrays(1, &va);
- }
- }
- void GL::bindVertexArray(VertexArray va) {
- glBindVertexArray(va);
- }
- void GL::bindBuffer(Buffer b) {
- glBindBuffer(GL_ARRAY_BUFFER, b);
- }
- void GL::bufferData(int size, const void* data, BufferUsage usage) {
- static constexpr GLenum MAP[] = {0, GL_STATIC_DRAW, GL_STREAM_DRAW,
- GL_DYNAMIC_DRAW};
- static constexpr int MAP_SIZE =
- static_cast<int>(sizeof(MAP) / sizeof(GLenum));
- int index = static_cast<int>(usage);
- GLenum realUsage = index < 0 || index >= MAP_SIZE ? MAP[0] : MAP[index];
- glBufferData(GL_ARRAY_BUFFER, size, data, realUsage);
- }
- void GL::bufferSubData(int offset, int size, const void* data) {
- glBufferSubData(GL_ARRAY_BUFFER, offset, size, data);
- }
- void GL::drawTriangles(int offset, int vertices) {
- glDrawArrays(GL_TRIANGLES, offset, vertices);
- }
- void GL::drawTriangleStrip(int offset, int vertices) {
- glDrawArrays(GL_TRIANGLE_STRIP, offset, vertices);
- }
- void GL::drawPoints(int offset, int vertices) {
- glDrawArrays(GL_POINTS, offset, vertices);
- }
|