From 0c2c581c646b8620433752529b8532986a76c1b9 Mon Sep 17 00:00:00 2001
From: Sam Gateau <samuel.gateau@gmail.com>
Date: Mon, 11 Feb 2019 22:52:35 -0800
Subject: [PATCH] Add the gles backend MS texture ad framebuffer support

---
 .../src/gpu/gles/GLESBackendOutput.cpp        |  4 ++
 .../src/gpu/gles/GLESBackendTexture.cpp       | 44 ++++++++++++-------
 2 files changed, 32 insertions(+), 16 deletions(-)

diff --git a/libraries/gpu-gles/src/gpu/gles/GLESBackendOutput.cpp b/libraries/gpu-gles/src/gpu/gles/GLESBackendOutput.cpp
index 90ce8c853a..36b37083cb 100644
--- a/libraries/gpu-gles/src/gpu/gles/GLESBackendOutput.cpp
+++ b/libraries/gpu-gles/src/gpu/gles/GLESBackendOutput.cpp
@@ -130,6 +130,8 @@ public:
 
                             }
 #endif
+                        } else if (gltexture->_target == GL_TEXTURE_2D_MULTISAMPLE) {
+                            glFramebufferTexture2D(GL_FRAMEBUFFER, colorAttachments[unit], GL_TEXTURE_2D_MULTISAMPLE, gltexture->_texture, 0);
                         } else {
                             glFramebufferTextureLayer(GL_FRAMEBUFFER, colorAttachments[unit], gltexture->_texture, 0,
                                                       b._subresource);
@@ -162,6 +164,8 @@ public:
             if (gltexture) {
                 if (gltexture->_target == GL_TEXTURE_2D) {
                     glFramebufferTexture2D(GL_FRAMEBUFFER, attachement, GL_TEXTURE_2D, gltexture->_texture, 0);
+                } else if (gltexture->_target == GL_TEXTURE_2D_MULTISAMPLE) {
+                    glFramebufferTexture2D(GL_FRAMEBUFFER, attachement, GL_TEXTURE_2D_MULTISAMPLE, gltexture->_texture, 0);
                 } else {
                     glFramebufferTextureLayer(GL_FRAMEBUFFER, attachement, gltexture->_texture, 0,
                                               _gpuObject.getDepthStencilBufferSubresource());
diff --git a/libraries/gpu-gles/src/gpu/gles/GLESBackendTexture.cpp b/libraries/gpu-gles/src/gpu/gles/GLESBackendTexture.cpp
index 23dc271af9..4b2d4d09e6 100644
--- a/libraries/gpu-gles/src/gpu/gles/GLESBackendTexture.cpp
+++ b/libraries/gpu-gles/src/gpu/gles/GLESBackendTexture.cpp
@@ -272,28 +272,40 @@ void GLESFixedAllocationTexture::allocateStorage() const {
     const GLTexelFormat texelFormat = GLTexelFormat::evalGLTexelFormat(_gpuObject.getTexelFormat());
     const auto numMips = _gpuObject.getNumMips();
     const auto numSlices = _gpuObject.getNumSlices();
+    const auto numSamples = _gpuObject.getNumSamples();
 
     // glTextureStorage2D(_id, mips, texelFormat.internalFormat, dimensions.x, dimensions.y);
-    for (GLint level = 0; level < numMips; level++) {
-        Vec3u dimensions = _gpuObject.evalMipDimensions(level);
-        for (GLenum target : getFaceTargets(_target)) {
-            if (texelFormat.isCompressed()) {
-                auto size = getCompressedImageSize(dimensions.x, dimensions.y, texelFormat.internalFormat);
-                if (!_gpuObject.isArray()) {
-                    glCompressedTexImage2D(target, level, texelFormat.internalFormat, dimensions.x, dimensions.y, 0, size, nullptr);
+    if (!_gpuObject.isMultisample()) {
+        for (GLint level = 0; level < numMips; level++) {
+            Vec3u dimensions = _gpuObject.evalMipDimensions(level);
+            for (GLenum target : getFaceTargets(_target)) {
+                if (texelFormat.isCompressed()) {
+                    auto size = getCompressedImageSize(dimensions.x, dimensions.y, texelFormat.internalFormat);
+                    if (!_gpuObject.isArray()) {
+                        glCompressedTexImage2D(target, level, texelFormat.internalFormat, dimensions.x, dimensions.y, 0, size, nullptr);
+                    } else {
+                        glCompressedTexImage3D(target, level, texelFormat.internalFormat, dimensions.x, dimensions.y, numSlices, 0, size * numSlices, nullptr);
+                    }
                 } else {
-                    glCompressedTexImage3D(target, level, texelFormat.internalFormat, dimensions.x, dimensions.y, numSlices, 0, size * numSlices, nullptr);
-                }
-            } else {
-                if (!_gpuObject.isArray()) {
-                    glTexImage2D(target, level, texelFormat.internalFormat, dimensions.x, dimensions.y, 0, texelFormat.format,
-                                texelFormat.type, nullptr);
-                } else {
-                    glTexImage3D(target, level, texelFormat.internalFormat, dimensions.x, dimensions.y, numSlices, 0,
-                                texelFormat.format, texelFormat.type, nullptr);
+                    if (!_gpuObject.isArray()) {
+                        glTexImage2D(target, level, texelFormat.internalFormat, dimensions.x, dimensions.y, 0, texelFormat.format,
+                                    texelFormat.type, nullptr);
+                    } else {
+                        glTexImage3D(target, level, texelFormat.internalFormat, dimensions.x, dimensions.y, numSlices, 0,
+                                    texelFormat.format, texelFormat.type, nullptr);
+                    }
                 }
             }
         }
+    } else {
+        const auto dimensions = _gpuObject.getDimensions();
+        if (!_gpuObject.isArray()) {
+            glTexStorage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, numSamples,
+                                      texelFormat.internalFormat, dimensions.x, dimensions.y,
+                                      GL_FALSE);
+        } else {
+            // NOT SUPPORTED (yet)
+        }       
     }
 
     glTexParameteri(_target, GL_TEXTURE_BASE_LEVEL, 0);