John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 1 | // SwiftShader Software Renderer |
| 2 | // |
| 3 | // Copyright(c) 2005-2013 TransGaming Inc. |
| 4 | // |
| 5 | // All rights reserved. No part of this software may be copied, distributed, transmitted, |
| 6 | // transcribed, stored in a retrieval system, translated into any human or computer |
| 7 | // language by any means, or disclosed to third parties without the explicit written |
| 8 | // agreement of TransGaming Inc. Without such an agreement, no rights or licenses, express |
| 9 | // or implied, including but not limited to any patent rights, are granted to you. |
| 10 | // |
| 11 | |
| 12 | // Framebuffer.cpp: Implements the Framebuffer class. Implements GL framebuffer |
| 13 | // objects and related functionality. [OpenGL ES 2.0.24] section 4.4 page 105. |
| 14 | |
| 15 | #include "Framebuffer.h" |
| 16 | |
| 17 | #include "main.h" |
| 18 | #include "Renderbuffer.h" |
| 19 | #include "Texture.h" |
| 20 | #include "utilities.h" |
| 21 | |
Nicolas Capens | 14ee762 | 2014-10-28 23:48:41 -0400 | [diff] [blame] | 22 | namespace es2 |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 23 | { |
| 24 | |
| 25 | Framebuffer::Framebuffer() |
| 26 | { |
Alexis Hetu | 1b2f628 | 2015-04-16 16:27:42 -0400 | [diff] [blame] | 27 | for(int i = 0; i < IMPLEMENTATION_MAX_COLOR_ATTACHMENTS; ++i) |
| 28 | { |
| 29 | mColorbufferType[i] = GL_NONE; |
| 30 | } |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 31 | mDepthbufferType = GL_NONE; |
| 32 | mStencilbufferType = GL_NONE; |
| 33 | } |
| 34 | |
| 35 | Framebuffer::~Framebuffer() |
| 36 | { |
Alexis Hetu | 1b2f628 | 2015-04-16 16:27:42 -0400 | [diff] [blame] | 37 | for(int i = 0; i < IMPLEMENTATION_MAX_COLOR_ATTACHMENTS; ++i) |
| 38 | { |
| 39 | mColorbufferPointer[i] = NULL; |
| 40 | } |
Nicolas Capens | d7d9b4b | 2015-01-29 23:46:44 -0500 | [diff] [blame] | 41 | mDepthbufferPointer = NULL; |
| 42 | mStencilbufferPointer = NULL; |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 43 | } |
| 44 | |
Alexis Hetu | 8af5007 | 2015-04-29 14:29:49 -0400 | [diff] [blame] | 45 | Renderbuffer *Framebuffer::lookupRenderbuffer(GLenum type, GLuint handle, GLint level, GLint layer) const |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 46 | { |
| 47 | Context *context = getContext(); |
| 48 | Renderbuffer *buffer = NULL; |
| 49 | |
| 50 | if(type == GL_NONE) |
| 51 | { |
| 52 | buffer = NULL; |
| 53 | } |
| 54 | else if(type == GL_RENDERBUFFER) |
| 55 | { |
| 56 | buffer = context->getRenderbuffer(handle); |
| 57 | } |
| 58 | else if(IsTextureTarget(type)) |
| 59 | { |
Alexis Hetu | 8af5007 | 2015-04-29 14:29:49 -0400 | [diff] [blame] | 60 | buffer = context->getTexture(handle)->getRenderbuffer(type, level, layer); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 61 | } |
Nicolas Capens | 3713cd4 | 2015-06-22 10:41:54 -0400 | [diff] [blame] | 62 | else UNREACHABLE(type); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 63 | |
| 64 | return buffer; |
| 65 | } |
| 66 | |
Alexis Hetu | 8af5007 | 2015-04-29 14:29:49 -0400 | [diff] [blame] | 67 | void Framebuffer::setColorbuffer(GLenum type, GLuint colorbuffer, GLuint index, GLint level, GLint layer) |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 68 | { |
Alexis Hetu | 1b2f628 | 2015-04-16 16:27:42 -0400 | [diff] [blame] | 69 | mColorbufferType[index] = (colorbuffer != 0) ? type : GL_NONE; |
Alexis Hetu | 8af5007 | 2015-04-29 14:29:49 -0400 | [diff] [blame] | 70 | mColorbufferPointer[index] = lookupRenderbuffer(type, colorbuffer, level, layer); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 71 | } |
| 72 | |
Alexis Hetu | 8af5007 | 2015-04-29 14:29:49 -0400 | [diff] [blame] | 73 | void Framebuffer::setDepthbuffer(GLenum type, GLuint depthbuffer, GLint level, GLint layer) |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 74 | { |
| 75 | mDepthbufferType = (depthbuffer != 0) ? type : GL_NONE; |
Alexis Hetu | 8af5007 | 2015-04-29 14:29:49 -0400 | [diff] [blame] | 76 | mDepthbufferPointer = lookupRenderbuffer(type, depthbuffer, level, layer); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 77 | } |
| 78 | |
Alexis Hetu | 8af5007 | 2015-04-29 14:29:49 -0400 | [diff] [blame] | 79 | void Framebuffer::setStencilbuffer(GLenum type, GLuint stencilbuffer, GLint level, GLint layer) |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 80 | { |
| 81 | mStencilbufferType = (stencilbuffer != 0) ? type : GL_NONE; |
Alexis Hetu | 8af5007 | 2015-04-29 14:29:49 -0400 | [diff] [blame] | 82 | mStencilbufferPointer = lookupRenderbuffer(type, stencilbuffer, level, layer); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | void Framebuffer::detachTexture(GLuint texture) |
| 86 | { |
Alexis Hetu | 1b2f628 | 2015-04-16 16:27:42 -0400 | [diff] [blame] | 87 | for(int i = 0; i < IMPLEMENTATION_MAX_COLOR_ATTACHMENTS; ++i) |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 88 | { |
Alexis Hetu | 1b2f628 | 2015-04-16 16:27:42 -0400 | [diff] [blame] | 89 | if(mColorbufferPointer[i].name() == texture && IsTextureTarget(mColorbufferType[i])) |
| 90 | { |
| 91 | mColorbufferType[i] = GL_NONE; |
| 92 | mColorbufferPointer[i] = NULL; |
| 93 | } |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 94 | } |
| 95 | |
Nicolas Capens | 7cc75e1 | 2015-01-29 14:44:24 -0500 | [diff] [blame] | 96 | if(mDepthbufferPointer.name() == texture && IsTextureTarget(mDepthbufferType)) |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 97 | { |
| 98 | mDepthbufferType = GL_NONE; |
Nicolas Capens | d7d9b4b | 2015-01-29 23:46:44 -0500 | [diff] [blame] | 99 | mDepthbufferPointer = NULL; |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 100 | } |
| 101 | |
Nicolas Capens | 7cc75e1 | 2015-01-29 14:44:24 -0500 | [diff] [blame] | 102 | if(mStencilbufferPointer.name() == texture && IsTextureTarget(mStencilbufferType)) |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 103 | { |
| 104 | mStencilbufferType = GL_NONE; |
Nicolas Capens | d7d9b4b | 2015-01-29 23:46:44 -0500 | [diff] [blame] | 105 | mStencilbufferPointer = NULL; |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 106 | } |
| 107 | } |
| 108 | |
| 109 | void Framebuffer::detachRenderbuffer(GLuint renderbuffer) |
| 110 | { |
Alexis Hetu | 1b2f628 | 2015-04-16 16:27:42 -0400 | [diff] [blame] | 111 | for(int i = 0; i < IMPLEMENTATION_MAX_COLOR_ATTACHMENTS; ++i) |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 112 | { |
Alexis Hetu | 1b2f628 | 2015-04-16 16:27:42 -0400 | [diff] [blame] | 113 | if(mColorbufferPointer[i].name() == renderbuffer && mColorbufferType[i] == GL_RENDERBUFFER) |
| 114 | { |
| 115 | mColorbufferType[i] = GL_NONE; |
| 116 | mColorbufferPointer[i] = NULL; |
| 117 | } |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 118 | } |
| 119 | |
Nicolas Capens | 7cc75e1 | 2015-01-29 14:44:24 -0500 | [diff] [blame] | 120 | if(mDepthbufferPointer.name() == renderbuffer && mDepthbufferType == GL_RENDERBUFFER) |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 121 | { |
| 122 | mDepthbufferType = GL_NONE; |
Nicolas Capens | d7d9b4b | 2015-01-29 23:46:44 -0500 | [diff] [blame] | 123 | mDepthbufferPointer = NULL; |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 124 | } |
| 125 | |
Nicolas Capens | 7cc75e1 | 2015-01-29 14:44:24 -0500 | [diff] [blame] | 126 | if(mStencilbufferPointer.name() == renderbuffer && mStencilbufferType == GL_RENDERBUFFER) |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 127 | { |
| 128 | mStencilbufferType = GL_NONE; |
Nicolas Capens | d7d9b4b | 2015-01-29 23:46:44 -0500 | [diff] [blame] | 129 | mStencilbufferPointer = NULL; |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | |
| 133 | // Increments refcount on surface. |
| 134 | // caller must Release() the returned surface |
Alexis Hetu | 1b2f628 | 2015-04-16 16:27:42 -0400 | [diff] [blame] | 135 | egl::Image *Framebuffer::getRenderTarget(GLuint index) |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 136 | { |
Alexis Hetu | 1b2f628 | 2015-04-16 16:27:42 -0400 | [diff] [blame] | 137 | Renderbuffer *colorbuffer = mColorbufferPointer[index]; |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 138 | |
| 139 | if(colorbuffer) |
| 140 | { |
| 141 | return colorbuffer->getRenderTarget(); |
| 142 | } |
| 143 | |
| 144 | return NULL; |
| 145 | } |
| 146 | |
Alexis Hetu | 1b2f628 | 2015-04-16 16:27:42 -0400 | [diff] [blame] | 147 | egl::Image *Framebuffer::getReadRenderTarget() |
| 148 | { |
| 149 | Context *context = getContext(); |
| 150 | return getRenderTarget(context->getReadFramebufferColorIndex()); |
| 151 | } |
| 152 | |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 153 | // Increments refcount on surface. |
| 154 | // caller must Release() the returned surface |
Nicolas Capens | ead7ac5 | 2014-10-27 23:56:02 -0400 | [diff] [blame] | 155 | egl::Image *Framebuffer::getDepthStencil() |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 156 | { |
Nicolas Capens | d7d9b4b | 2015-01-29 23:46:44 -0500 | [diff] [blame] | 157 | Renderbuffer *depthstencilbuffer = mDepthbufferPointer; |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 158 | |
| 159 | if(!depthstencilbuffer) |
| 160 | { |
Nicolas Capens | d7d9b4b | 2015-01-29 23:46:44 -0500 | [diff] [blame] | 161 | depthstencilbuffer = mStencilbufferPointer; |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | if(depthstencilbuffer) |
| 165 | { |
| 166 | return depthstencilbuffer->getRenderTarget(); |
| 167 | } |
| 168 | |
| 169 | return NULL; |
| 170 | } |
| 171 | |
Alexis Hetu | 1b2f628 | 2015-04-16 16:27:42 -0400 | [diff] [blame] | 172 | Renderbuffer *Framebuffer::getColorbuffer(GLuint index) |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 173 | { |
Alexis Hetu | 1b2f628 | 2015-04-16 16:27:42 -0400 | [diff] [blame] | 174 | return (index < MAX_COLOR_ATTACHMENTS) ? mColorbufferPointer[index] : (Renderbuffer*)NULL; |
| 175 | } |
| 176 | |
| 177 | Renderbuffer *Framebuffer::getReadColorbuffer() |
| 178 | { |
| 179 | Context *context = getContext(); |
| 180 | return getColorbuffer(context->getReadFramebufferColorIndex()); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | Renderbuffer *Framebuffer::getDepthbuffer() |
| 184 | { |
Nicolas Capens | d7d9b4b | 2015-01-29 23:46:44 -0500 | [diff] [blame] | 185 | return mDepthbufferPointer; |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | Renderbuffer *Framebuffer::getStencilbuffer() |
| 189 | { |
Nicolas Capens | d7d9b4b | 2015-01-29 23:46:44 -0500 | [diff] [blame] | 190 | return mStencilbufferPointer; |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 191 | } |
| 192 | |
Alexis Hetu | 1b2f628 | 2015-04-16 16:27:42 -0400 | [diff] [blame] | 193 | GLenum Framebuffer::getColorbufferType(GLuint index) |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 194 | { |
Alexis Hetu | 1b2f628 | 2015-04-16 16:27:42 -0400 | [diff] [blame] | 195 | return mColorbufferType[index]; |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | GLenum Framebuffer::getDepthbufferType() |
| 199 | { |
| 200 | return mDepthbufferType; |
| 201 | } |
| 202 | |
| 203 | GLenum Framebuffer::getStencilbufferType() |
| 204 | { |
| 205 | return mStencilbufferType; |
| 206 | } |
| 207 | |
Alexis Hetu | 1b2f628 | 2015-04-16 16:27:42 -0400 | [diff] [blame] | 208 | GLuint Framebuffer::getColorbufferName(GLuint index) |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 209 | { |
Alexis Hetu | 1b2f628 | 2015-04-16 16:27:42 -0400 | [diff] [blame] | 210 | return mColorbufferPointer[index].name(); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 211 | } |
| 212 | |
Nicolas Capens | 7cc75e1 | 2015-01-29 14:44:24 -0500 | [diff] [blame] | 213 | GLuint Framebuffer::getDepthbufferName() |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 214 | { |
Nicolas Capens | 7cc75e1 | 2015-01-29 14:44:24 -0500 | [diff] [blame] | 215 | return mDepthbufferPointer.name(); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 216 | } |
| 217 | |
Nicolas Capens | 7cc75e1 | 2015-01-29 14:44:24 -0500 | [diff] [blame] | 218 | GLuint Framebuffer::getStencilbufferName() |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 219 | { |
Nicolas Capens | 7cc75e1 | 2015-01-29 14:44:24 -0500 | [diff] [blame] | 220 | return mStencilbufferPointer.name(); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 221 | } |
| 222 | |
Alexis Hetu | 074c641 | 2015-06-22 15:57:27 -0400 | [diff] [blame] | 223 | GLint Framebuffer::getColorbufferLayer(GLuint index) |
| 224 | { |
| 225 | Renderbuffer *colorbuffer = mColorbufferPointer[index]; |
Alexis Hetu | 0fecd6f | 2015-07-22 16:49:31 -0400 | [diff] [blame] | 226 | return colorbuffer ? colorbuffer->getLayer() : 0; |
| 227 | } |
| 228 | |
| 229 | GLint Framebuffer::getDepthbufferLayer() |
| 230 | { |
| 231 | return mDepthbufferPointer ? mDepthbufferPointer->getLayer() : 0; |
| 232 | } |
| 233 | |
| 234 | GLint Framebuffer::getStencilbufferLayer() |
| 235 | { |
| 236 | return mStencilbufferPointer ? mStencilbufferPointer->getLayer() : 0; |
Alexis Hetu | 074c641 | 2015-06-22 15:57:27 -0400 | [diff] [blame] | 237 | } |
| 238 | |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 239 | bool Framebuffer::hasStencil() |
| 240 | { |
| 241 | if(mStencilbufferType != GL_NONE) |
| 242 | { |
| 243 | Renderbuffer *stencilbufferObject = getStencilbuffer(); |
| 244 | |
| 245 | if(stencilbufferObject) |
| 246 | { |
| 247 | return stencilbufferObject->getStencilSize() > 0; |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | return false; |
| 252 | } |
| 253 | |
| 254 | GLenum Framebuffer::completeness() |
| 255 | { |
| 256 | int width; |
| 257 | int height; |
| 258 | int samples; |
| 259 | |
| 260 | return completeness(width, height, samples); |
| 261 | } |
| 262 | |
| 263 | GLenum Framebuffer::completeness(int &width, int &height, int &samples) |
| 264 | { |
| 265 | width = -1; |
| 266 | height = -1; |
| 267 | samples = -1; |
| 268 | |
Alexis Hetu | 1b2f628 | 2015-04-16 16:27:42 -0400 | [diff] [blame] | 269 | for(int i = 0; i < IMPLEMENTATION_MAX_COLOR_ATTACHMENTS; ++i) |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 270 | { |
Alexis Hetu | 1b2f628 | 2015-04-16 16:27:42 -0400 | [diff] [blame] | 271 | if(mColorbufferType[i] != GL_NONE) |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 272 | { |
Alexis Hetu | 1b2f628 | 2015-04-16 16:27:42 -0400 | [diff] [blame] | 273 | Renderbuffer *colorbuffer = getColorbuffer(i); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 274 | |
Alexis Hetu | 1b2f628 | 2015-04-16 16:27:42 -0400 | [diff] [blame] | 275 | if(!colorbuffer) |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 276 | { |
| 277 | return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT; |
| 278 | } |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 279 | |
Alexis Hetu | e23029a | 2015-12-04 17:12:35 -0500 | [diff] [blame] | 280 | if(colorbuffer->getWidth() == 0 || colorbuffer->getHeight() == 0 || (colorbuffer->getDepth() <= colorbuffer->getLayer())) |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 281 | { |
| 282 | return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT; |
| 283 | } |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 284 | |
Alexis Hetu | 1b2f628 | 2015-04-16 16:27:42 -0400 | [diff] [blame] | 285 | if(mColorbufferType[i] == GL_RENDERBUFFER) |
| 286 | { |
Alexis Hetu | c8f95e8 | 2015-11-12 16:36:34 -0500 | [diff] [blame] | 287 | if(!es2::IsColorRenderable(colorbuffer->getFormat(), egl::getClientVersion())) |
Alexis Hetu | 1b2f628 | 2015-04-16 16:27:42 -0400 | [diff] [blame] | 288 | { |
| 289 | return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT; |
| 290 | } |
| 291 | } |
| 292 | else if(IsTextureTarget(mColorbufferType[i])) |
| 293 | { |
| 294 | GLenum format = colorbuffer->getFormat(); |
| 295 | |
Alexis Hetu | 460e41f | 2015-09-01 10:58:37 -0400 | [diff] [blame] | 296 | if(IsCompressed(format, egl::getClientVersion()) || |
Alexis Hetu | 1b2f628 | 2015-04-16 16:27:42 -0400 | [diff] [blame] | 297 | format == GL_ALPHA || |
| 298 | format == GL_LUMINANCE || |
| 299 | format == GL_LUMINANCE_ALPHA) |
| 300 | { |
| 301 | return GL_FRAMEBUFFER_UNSUPPORTED; |
| 302 | } |
| 303 | |
| 304 | if(es2::IsDepthTexture(format) || es2::IsStencilTexture(format)) |
| 305 | { |
| 306 | return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT; |
| 307 | } |
| 308 | } |
| 309 | else |
| 310 | { |
Nicolas Capens | 3713cd4 | 2015-06-22 10:41:54 -0400 | [diff] [blame] | 311 | UNREACHABLE(mColorbufferType[i]); |
Alexis Hetu | 1b2f628 | 2015-04-16 16:27:42 -0400 | [diff] [blame] | 312 | return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT; |
| 313 | } |
| 314 | |
| 315 | width = colorbuffer->getWidth(); |
| 316 | height = colorbuffer->getHeight(); |
Alexis Hetu | 137364a | 2015-12-08 11:24:21 -0500 | [diff] [blame] | 317 | |
| 318 | if(samples == -1) |
| 319 | { |
| 320 | samples = colorbuffer->getSamples(); |
| 321 | } |
| 322 | else if(samples != colorbuffer->getSamples()) |
| 323 | { |
| 324 | return GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE_ANGLE; |
| 325 | } |
| 326 | |
Alexis Hetu | 1b2f628 | 2015-04-16 16:27:42 -0400 | [diff] [blame] | 327 | } |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | Renderbuffer *depthbuffer = NULL; |
| 331 | Renderbuffer *stencilbuffer = NULL; |
| 332 | |
| 333 | if(mDepthbufferType != GL_NONE) |
| 334 | { |
| 335 | depthbuffer = getDepthbuffer(); |
| 336 | |
| 337 | if(!depthbuffer) |
| 338 | { |
| 339 | return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT; |
| 340 | } |
| 341 | |
| 342 | if(depthbuffer->getWidth() == 0 || depthbuffer->getHeight() == 0) |
| 343 | { |
| 344 | return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT; |
| 345 | } |
| 346 | |
| 347 | if(mDepthbufferType == GL_RENDERBUFFER) |
| 348 | { |
Nicolas Capens | 14ee762 | 2014-10-28 23:48:41 -0400 | [diff] [blame] | 349 | if(!es2::IsDepthRenderable(depthbuffer->getFormat())) |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 350 | { |
| 351 | return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT; |
| 352 | } |
| 353 | } |
| 354 | else if(IsTextureTarget(mDepthbufferType)) |
| 355 | { |
Nicolas Capens | 14ee762 | 2014-10-28 23:48:41 -0400 | [diff] [blame] | 356 | if(!es2::IsDepthTexture(depthbuffer->getFormat())) |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 357 | { |
| 358 | return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT; |
| 359 | } |
| 360 | } |
| 361 | else |
| 362 | { |
Nicolas Capens | 3713cd4 | 2015-06-22 10:41:54 -0400 | [diff] [blame] | 363 | UNREACHABLE(mDepthbufferType); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 364 | return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT; |
| 365 | } |
| 366 | |
| 367 | if(width == -1 || height == -1) |
| 368 | { |
| 369 | width = depthbuffer->getWidth(); |
| 370 | height = depthbuffer->getHeight(); |
| 371 | samples = depthbuffer->getSamples(); |
| 372 | } |
| 373 | else if(width != depthbuffer->getWidth() || height != depthbuffer->getHeight()) |
| 374 | { |
| 375 | return GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS; |
| 376 | } |
| 377 | else if(samples != depthbuffer->getSamples()) |
| 378 | { |
| 379 | return GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE_ANGLE; |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | if(mStencilbufferType != GL_NONE) |
| 384 | { |
| 385 | stencilbuffer = getStencilbuffer(); |
| 386 | |
| 387 | if(!stencilbuffer) |
| 388 | { |
| 389 | return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT; |
| 390 | } |
| 391 | |
| 392 | if(stencilbuffer->getWidth() == 0 || stencilbuffer->getHeight() == 0) |
| 393 | { |
| 394 | return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT; |
| 395 | } |
| 396 | |
| 397 | if(mStencilbufferType == GL_RENDERBUFFER) |
| 398 | { |
Nicolas Capens | 14ee762 | 2014-10-28 23:48:41 -0400 | [diff] [blame] | 399 | if(!es2::IsStencilRenderable(stencilbuffer->getFormat())) |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 400 | { |
| 401 | return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT; |
| 402 | } |
| 403 | } |
| 404 | else if(IsTextureTarget(mStencilbufferType)) |
| 405 | { |
| 406 | GLenum internalformat = stencilbuffer->getFormat(); |
| 407 | |
Nicolas Capens | 14ee762 | 2014-10-28 23:48:41 -0400 | [diff] [blame] | 408 | if(!es2::IsStencilTexture(internalformat)) |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 409 | { |
| 410 | return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT; |
| 411 | } |
| 412 | } |
| 413 | else |
| 414 | { |
Nicolas Capens | 3713cd4 | 2015-06-22 10:41:54 -0400 | [diff] [blame] | 415 | UNREACHABLE(mStencilbufferType); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 416 | return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT; |
| 417 | } |
| 418 | |
| 419 | if(width == -1 || height == -1) |
| 420 | { |
| 421 | width = stencilbuffer->getWidth(); |
| 422 | height = stencilbuffer->getHeight(); |
| 423 | samples = stencilbuffer->getSamples(); |
| 424 | } |
| 425 | else if(width != stencilbuffer->getWidth() || height != stencilbuffer->getHeight()) |
| 426 | { |
| 427 | return GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS; |
| 428 | } |
| 429 | else if(samples != stencilbuffer->getSamples()) |
| 430 | { |
| 431 | return GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE_ANGLE; |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | // If we have both a depth and stencil buffer, they must refer to the same object |
| 436 | // since we only support packed_depth_stencil and not separate depth and stencil |
| 437 | if(depthbuffer && stencilbuffer && (depthbuffer != stencilbuffer)) |
| 438 | { |
| 439 | return GL_FRAMEBUFFER_UNSUPPORTED; |
| 440 | } |
| 441 | |
| 442 | // We need to have at least one attachment to be complete |
| 443 | if(width == -1 || height == -1) |
| 444 | { |
| 445 | return GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT; |
| 446 | } |
| 447 | |
| 448 | return GL_FRAMEBUFFER_COMPLETE; |
| 449 | } |
| 450 | |
Nicolas Capens | 4b8df2f | 2015-01-29 13:20:27 -0500 | [diff] [blame] | 451 | GLenum Framebuffer::getImplementationColorReadFormat() |
| 452 | { |
Alexis Hetu | 1b2f628 | 2015-04-16 16:27:42 -0400 | [diff] [blame] | 453 | Renderbuffer *colorbuffer = getReadColorbuffer(); |
Nicolas Capens | 4b8df2f | 2015-01-29 13:20:27 -0500 | [diff] [blame] | 454 | |
| 455 | if(colorbuffer) |
| 456 | { |
Nicolas Capens | de6b75c | 2015-03-29 00:27:33 -0400 | [diff] [blame] | 457 | // Don't return GL_RGBA since that's always supported. Provide a second option here. |
Nicolas Capens | 4b8df2f | 2015-01-29 13:20:27 -0500 | [diff] [blame] | 458 | switch(colorbuffer->getInternalFormat()) |
| 459 | { |
Alexis Hetu | c8f95e8 | 2015-11-12 16:36:34 -0500 | [diff] [blame] | 460 | case sw::FORMAT_A8B8G8R8I: |
| 461 | case sw::FORMAT_A8B8G8R8UI: |
| 462 | case sw::FORMAT_A16B16G16R16I: |
| 463 | case sw::FORMAT_A16B16G16R16UI: |
| 464 | case sw::FORMAT_A32B32G32R32I: |
| 465 | case sw::FORMAT_A32B32G32R32UI:return GL_RGBA_INTEGER; |
| 466 | case sw::FORMAT_A2B10G10R10: return GL_RGB10_A2; |
| 467 | case sw::FORMAT_A8B8G8R8I_SNORM: |
| 468 | case sw::FORMAT_A16B16G16R16F: |
| 469 | case sw::FORMAT_A32B32G32R32F: |
| 470 | case sw::FORMAT_A8R8G8B8: |
| 471 | case sw::FORMAT_A8B8G8R8: |
Nicolas Capens | 4b8df2f | 2015-01-29 13:20:27 -0500 | [diff] [blame] | 472 | case sw::FORMAT_A1R5G5B5: return GL_BGRA_EXT; |
Alexis Hetu | c8f95e8 | 2015-11-12 16:36:34 -0500 | [diff] [blame] | 473 | case sw::FORMAT_X8B8G8R8I: |
| 474 | case sw::FORMAT_X8B8G8R8UI: |
| 475 | case sw::FORMAT_X16B16G16R16I: |
| 476 | case sw::FORMAT_X16B16G16R16UI: |
| 477 | case sw::FORMAT_X32B32G32R32I: |
| 478 | case sw::FORMAT_X32B32G32R32UI:return GL_RGB_INTEGER; |
Alexis Hetu | e727775 | 2015-12-04 11:16:35 -0500 | [diff] [blame^] | 479 | case sw::FORMAT_B16G16R16F: |
Alexis Hetu | c8f95e8 | 2015-11-12 16:36:34 -0500 | [diff] [blame] | 480 | case sw::FORMAT_X8B8G8R8I_SNORM: |
| 481 | case sw::FORMAT_X8B8G8R8: |
| 482 | case sw::FORMAT_X8R8G8B8: |
Nicolas Capens | 4b8df2f | 2015-01-29 13:20:27 -0500 | [diff] [blame] | 483 | case sw::FORMAT_R5G6B5: return 0x80E0; // GL_BGR_EXT |
Alexis Hetu | c8f95e8 | 2015-11-12 16:36:34 -0500 | [diff] [blame] | 484 | case sw::FORMAT_G8R8I: |
| 485 | case sw::FORMAT_G8R8UI: |
| 486 | case sw::FORMAT_G16R16I: |
| 487 | case sw::FORMAT_G16R16UI: |
| 488 | case sw::FORMAT_G32R32I: |
| 489 | case sw::FORMAT_G32R32UI: return GL_RG_INTEGER; |
| 490 | case sw::FORMAT_G8R8: |
| 491 | case sw::FORMAT_G8R8I_SNORM: |
| 492 | case sw::FORMAT_G16R16F: |
| 493 | case sw::FORMAT_G32R32F: return GL_RG; |
| 494 | case sw::FORMAT_R8I: |
| 495 | case sw::FORMAT_R8UI: |
| 496 | case sw::FORMAT_R16I: |
| 497 | case sw::FORMAT_R16UI: |
| 498 | case sw::FORMAT_R32I: |
| 499 | case sw::FORMAT_R32UI: return GL_RED_INTEGER; |
| 500 | case sw::FORMAT_R8: |
| 501 | case sw::FORMAT_R8I_SNORM: |
| 502 | case sw::FORMAT_R16F: |
| 503 | case sw::FORMAT_R32F: return GL_RED; |
Nicolas Capens | 4b8df2f | 2015-01-29 13:20:27 -0500 | [diff] [blame] | 504 | default: |
Nicolas Capens | 3713cd4 | 2015-06-22 10:41:54 -0400 | [diff] [blame] | 505 | UNREACHABLE(colorbuffer->getInternalFormat()); |
Nicolas Capens | 4b8df2f | 2015-01-29 13:20:27 -0500 | [diff] [blame] | 506 | } |
| 507 | } |
| 508 | |
| 509 | return GL_RGBA; |
| 510 | } |
| 511 | |
Nicolas Capens | dddc4ab | 2015-01-13 15:49:15 -0500 | [diff] [blame] | 512 | GLenum Framebuffer::getImplementationColorReadType() |
| 513 | { |
Alexis Hetu | 1b2f628 | 2015-04-16 16:27:42 -0400 | [diff] [blame] | 514 | Renderbuffer *colorbuffer = getReadColorbuffer(); |
Nicolas Capens | 4b8df2f | 2015-01-29 13:20:27 -0500 | [diff] [blame] | 515 | |
| 516 | if(colorbuffer) |
| 517 | { |
| 518 | switch(colorbuffer->getInternalFormat()) |
| 519 | { |
Alexis Hetu | c8f95e8 | 2015-11-12 16:36:34 -0500 | [diff] [blame] | 520 | case sw::FORMAT_R16F: |
| 521 | case sw::FORMAT_G16R16F: |
| 522 | case sw::FORMAT_B16G16R16F: |
| 523 | case sw::FORMAT_A16B16G16R16F: |
| 524 | case sw::FORMAT_R32F: |
| 525 | case sw::FORMAT_G32R32F: |
| 526 | case sw::FORMAT_B32G32R32F: |
Nicolas Capens | 4b8df2f | 2015-01-29 13:20:27 -0500 | [diff] [blame] | 527 | case sw::FORMAT_A32B32G32R32F: return GL_FLOAT; |
Alexis Hetu | c8f95e8 | 2015-11-12 16:36:34 -0500 | [diff] [blame] | 528 | case sw::FORMAT_R8I_SNORM: |
| 529 | case sw::FORMAT_G8R8I_SNORM: |
| 530 | case sw::FORMAT_X8B8G8R8I_SNORM: |
| 531 | case sw::FORMAT_A8B8G8R8I_SNORM:return GL_BYTE; |
| 532 | case sw::FORMAT_R8: |
| 533 | case sw::FORMAT_G8R8: |
| 534 | case sw::FORMAT_A8R8G8B8: |
| 535 | case sw::FORMAT_A8B8G8R8: |
| 536 | case sw::FORMAT_X8R8G8B8: |
Nicolas Capens | de6b75c | 2015-03-29 00:27:33 -0400 | [diff] [blame] | 537 | case sw::FORMAT_X8B8G8R8: return GL_UNSIGNED_BYTE; |
Alexis Hetu | c8f95e8 | 2015-11-12 16:36:34 -0500 | [diff] [blame] | 538 | case sw::FORMAT_R8I: |
| 539 | case sw::FORMAT_G8R8I: |
| 540 | case sw::FORMAT_X8B8G8R8I: |
| 541 | case sw::FORMAT_A8B8G8R8I: |
| 542 | case sw::FORMAT_R16I: |
| 543 | case sw::FORMAT_G16R16I: |
| 544 | case sw::FORMAT_X16B16G16R16I: |
| 545 | case sw::FORMAT_A16B16G16R16I: |
| 546 | case sw::FORMAT_R32I: |
| 547 | case sw::FORMAT_G32R32I: |
| 548 | case sw::FORMAT_X32B32G32R32I: |
| 549 | case sw::FORMAT_A32B32G32R32I: return GL_INT; |
| 550 | case sw::FORMAT_R8UI: |
| 551 | case sw::FORMAT_G8R8UI: |
| 552 | case sw::FORMAT_X8B8G8R8UI: |
| 553 | case sw::FORMAT_A8B8G8R8UI: |
| 554 | case sw::FORMAT_R16UI: |
| 555 | case sw::FORMAT_G16R16UI: |
| 556 | case sw::FORMAT_X16B16G16R16UI: |
| 557 | case sw::FORMAT_A16B16G16R16UI: |
| 558 | case sw::FORMAT_R32UI: |
| 559 | case sw::FORMAT_G32R32UI: |
| 560 | case sw::FORMAT_X32B32G32R32UI: |
| 561 | case sw::FORMAT_A32B32G32R32UI:return GL_UNSIGNED_INT; |
| 562 | case sw::FORMAT_A2B10G10R10: return GL_UNSIGNED_INT_10_10_10_2_OES; |
Nicolas Capens | 4b8df2f | 2015-01-29 13:20:27 -0500 | [diff] [blame] | 563 | case sw::FORMAT_A1R5G5B5: return GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT; |
| 564 | case sw::FORMAT_R5G6B5: return GL_UNSIGNED_SHORT_5_6_5; |
| 565 | default: |
Nicolas Capens | 3713cd4 | 2015-06-22 10:41:54 -0400 | [diff] [blame] | 566 | UNREACHABLE(colorbuffer->getInternalFormat()); |
Nicolas Capens | 4b8df2f | 2015-01-29 13:20:27 -0500 | [diff] [blame] | 567 | } |
Nicolas Capens | 9703d1a | 2015-01-14 15:55:33 -0500 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | return GL_UNSIGNED_BYTE; |
Nicolas Capens | dddc4ab | 2015-01-13 15:49:15 -0500 | [diff] [blame] | 571 | } |
| 572 | |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 573 | DefaultFramebuffer::DefaultFramebuffer(Colorbuffer *colorbuffer, DepthStencilbuffer *depthStencil) |
| 574 | { |
Alexis Hetu | 1b2f628 | 2015-04-16 16:27:42 -0400 | [diff] [blame] | 575 | mColorbufferPointer[0] = new Renderbuffer(0, colorbuffer); |
| 576 | mColorbufferType[0] = GL_RENDERBUFFER; |
| 577 | |
| 578 | for(int i = 1; i < IMPLEMENTATION_MAX_COLOR_ATTACHMENTS; ++i) |
| 579 | { |
| 580 | mColorbufferPointer[i] = nullptr; |
| 581 | mColorbufferType[i] = GL_NONE; |
| 582 | } |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 583 | |
| 584 | Renderbuffer *depthStencilRenderbuffer = new Renderbuffer(0, depthStencil); |
Nicolas Capens | d7d9b4b | 2015-01-29 23:46:44 -0500 | [diff] [blame] | 585 | mDepthbufferPointer = depthStencilRenderbuffer; |
| 586 | mStencilbufferPointer = depthStencilRenderbuffer; |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 587 | |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 588 | mDepthbufferType = (depthStencilRenderbuffer->getDepthSize() != 0) ? GL_RENDERBUFFER : GL_NONE; |
| 589 | mStencilbufferType = (depthStencilRenderbuffer->getStencilSize() != 0) ? GL_RENDERBUFFER : GL_NONE; |
| 590 | } |
| 591 | |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 592 | } |