From 601e1ca99ded44750a6c7d64f90f1a908515f537 Mon Sep 17 00:00:00 2001 From: Green Sky Date: Wed, 16 Feb 2022 19:22:25 +0100 Subject: [PATCH] add multisampled textures --- .../src/mm/opengl/texture.cpp | 37 ++++++++++++++++--- .../src/mm/opengl/texture.hpp | 5 ++- 2 files changed, 36 insertions(+), 6 deletions(-) diff --git a/framework/opengl_primitives/src/mm/opengl/texture.cpp b/framework/opengl_primitives/src/mm/opengl/texture.cpp index 57bfa7f..7e99e1d 100644 --- a/framework/opengl_primitives/src/mm/opengl/texture.cpp +++ b/framework/opengl_primitives/src/mm/opengl/texture.cpp @@ -16,28 +16,41 @@ uint32_t Texture::getHandle(void) const { Texture::Texture( uint32_t handle, int32_t width_, int32_t height_, - int32_t internalFormat, int32_t format, int32_t type + int32_t internalFormat, int32_t format, int32_t type, + uint32_t samples ) : _handle(handle), width(width_), height(height_), - _internalFormat(internalFormat), _format(format), _type(type) {} + _internalFormat(internalFormat), _format(format), _type(type), + _samples(samples) { +} Texture::~Texture(void) { glDeleteTextures(1, &_handle); } void Texture::unbind(void) const { + // TODO: do i need ms variant? glBindTexture(GL_TEXTURE_2D, 0); } void Texture::bind(uint32_t slot) const { glActiveTexture(GL_TEXTURE0 + slot); - glBindTexture(GL_TEXTURE_2D, _handle); + if (_samples == 0) { + glBindTexture(GL_TEXTURE_2D, _handle); + } else { + glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, _handle); + } } void Texture::resize(int32_t new_width, int32_t new_height) { // if (glTexImage2D == true) - glBindTexture(GL_TEXTURE_2D, _handle); - glTexImage2D(GL_TEXTURE_2D, 0, _internalFormat, new_width, new_height, 0, _format, _type, NULL); + if (_samples == 0) { + glBindTexture(GL_TEXTURE_2D, _handle); + glTexImage2D(GL_TEXTURE_2D, 0, _internalFormat, new_width, new_height, 0, _format, _type, NULL); + } else { + glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, _handle); + glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, _samples, _internalFormat, new_width, new_height, 0); + } // HACK: super dirty *(const_cast(&width)) = new_width; @@ -57,5 +70,19 @@ Texture::handle_t Texture::createEmpty(int32_t internalFormat, int32_t width, in return handle_t(new Texture(id, width, height, internalFormat, format, type)); } +Texture::handle_t Texture::createEmptyMultiSampled(int32_t internalFormat, int32_t width, int32_t height, uint32_t samples) { + uint32_t id; + glGenTextures(1, &id); + glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, id); + + glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, samples, internalFormat, width, height, GL_TRUE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, 0); + + // HACK: format + type? + return handle_t(new Texture(id, width, height, internalFormat, 0, 0, samples)); +} + } // MM::OpenGL diff --git a/framework/opengl_primitives/src/mm/opengl/texture.hpp b/framework/opengl_primitives/src/mm/opengl/texture.hpp index b27c8d5..8f9603b 100644 --- a/framework/opengl_primitives/src/mm/opengl/texture.hpp +++ b/framework/opengl_primitives/src/mm/opengl/texture.hpp @@ -20,7 +20,8 @@ namespace MM::OpenGL { Texture( uint32_t handle, int32_t width_, int32_t height_, - int32_t internalFormat, int32_t format, int32_t type + int32_t internalFormat, int32_t format, int32_t type, + uint32_t samples = 0u ); public: @@ -33,6 +34,7 @@ namespace MM::OpenGL { int32_t const _internalFormat; int32_t const _format; int32_t const _type; + uint32_t const _samples{0u}; // sample count, 0 == off public: ~Texture(); @@ -45,6 +47,7 @@ namespace MM::OpenGL { void resize(int32_t new_width, int32_t new_height); static handle_t createEmpty(int32_t internalFormat, int32_t width, int32_t height, int32_t format, int32_t type); + static handle_t createEmptyMultiSampled(int32_t internalFormat, int32_t width, int32_t height, uint32_t samples); }; } // MM::OpenGL