daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1 | // |
| 2 | // Copyright (c) 2002-2010 The ANGLE Project Authors. All rights reserved. |
| 3 | // Use of this source code is governed by a BSD-style license that can be |
| 4 | // found in the LICENSE file. |
| 5 | // |
| 6 | |
| 7 | // Renderbuffer.cpp: the gl::Renderbuffer class and its derived classes |
| 8 | // Colorbuffer, Depthbuffer and Stencilbuffer. Implements GL renderbuffer |
| 9 | // objects and related functionality. [OpenGL ES 2.0.24] section 4.4.3 page 108. |
| 10 | |
daniel@transgaming.com | bbf56f7 | 2010-04-20 18:52:13 +0000 | [diff] [blame] | 11 | #include "libGLESv2/Renderbuffer.h" |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 12 | |
daniel@transgaming.com | bbf56f7 | 2010-04-20 18:52:13 +0000 | [diff] [blame] | 13 | #include "libGLESv2/main.h" |
| 14 | #include "libGLESv2/utilities.h" |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 15 | |
| 16 | namespace gl |
| 17 | { |
daniel@transgaming.com | 092bd48 | 2010-05-12 03:39:36 +0000 | [diff] [blame] | 18 | unsigned int Renderbuffer::mCurrentSerial = 1; |
| 19 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 20 | Renderbuffer::Renderbuffer() |
| 21 | { |
| 22 | mWidth = 0; |
| 23 | mHeight = 0; |
daniel@transgaming.com | 4901fca | 2010-04-20 18:52:41 +0000 | [diff] [blame] | 24 | mFormat = GL_RGBA4; // default format, needs to be one of the expected renderbuffer formats |
daniel@transgaming.com | 092bd48 | 2010-05-12 03:39:36 +0000 | [diff] [blame] | 25 | mSerial = issueSerial(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | Renderbuffer::~Renderbuffer() |
| 29 | { |
| 30 | } |
| 31 | |
| 32 | bool Renderbuffer::isColorbuffer() |
| 33 | { |
| 34 | return false; |
| 35 | } |
| 36 | |
| 37 | bool Renderbuffer::isDepthbuffer() |
| 38 | { |
| 39 | return false; |
| 40 | } |
| 41 | |
| 42 | bool Renderbuffer::isStencilbuffer() |
| 43 | { |
| 44 | return false; |
| 45 | } |
| 46 | |
| 47 | IDirect3DSurface9 *Renderbuffer::getRenderTarget() |
| 48 | { |
| 49 | return NULL; |
| 50 | } |
| 51 | |
| 52 | IDirect3DSurface9 *Renderbuffer::getDepthStencil() |
| 53 | { |
| 54 | return NULL; |
| 55 | } |
| 56 | |
| 57 | int Renderbuffer::getWidth() |
| 58 | { |
| 59 | return mWidth; |
| 60 | } |
| 61 | |
| 62 | int Renderbuffer::getHeight() |
| 63 | { |
| 64 | return mHeight; |
| 65 | } |
| 66 | |
daniel@transgaming.com | 4901fca | 2010-04-20 18:52:41 +0000 | [diff] [blame] | 67 | GLenum Renderbuffer::getFormat() |
| 68 | { |
| 69 | return mFormat; |
| 70 | } |
| 71 | |
daniel@transgaming.com | 092bd48 | 2010-05-12 03:39:36 +0000 | [diff] [blame] | 72 | unsigned int Renderbuffer::getSerial() const |
| 73 | { |
| 74 | return mSerial; |
| 75 | } |
| 76 | |
| 77 | unsigned int Renderbuffer::issueSerial() |
| 78 | { |
| 79 | return mCurrentSerial++; |
| 80 | } |
| 81 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 82 | Colorbuffer::Colorbuffer(IDirect3DSurface9 *renderTarget) : mRenderTarget(renderTarget) |
| 83 | { |
| 84 | if (renderTarget) |
| 85 | { |
| 86 | renderTarget->AddRef(); |
| 87 | |
| 88 | D3DSURFACE_DESC description; |
| 89 | renderTarget->GetDesc(&description); |
| 90 | |
| 91 | mWidth = description.Width; |
| 92 | mHeight = description.Height; |
| 93 | } |
daniel@transgaming.com | 092bd48 | 2010-05-12 03:39:36 +0000 | [diff] [blame] | 94 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 95 | } |
| 96 | |
daniel@transgaming.com | 70d312a | 2010-04-20 18:52:38 +0000 | [diff] [blame] | 97 | Colorbuffer::Colorbuffer(int width, int height, GLenum format) |
| 98 | { |
| 99 | IDirect3DDevice9 *device = getDevice(); |
| 100 | |
| 101 | mRenderTarget = NULL; |
| 102 | HRESULT result = device->CreateRenderTarget(width, height, es2dx::ConvertRenderbufferFormat(format), |
| 103 | D3DMULTISAMPLE_NONE, 0, FALSE, &mRenderTarget, NULL); |
| 104 | |
| 105 | if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY) |
| 106 | { |
| 107 | error(GL_OUT_OF_MEMORY); |
| 108 | |
| 109 | return; |
| 110 | } |
| 111 | |
| 112 | ASSERT(SUCCEEDED(result)); |
| 113 | |
| 114 | if (mRenderTarget) |
| 115 | { |
| 116 | mWidth = width; |
| 117 | mHeight = height; |
daniel@transgaming.com | 4901fca | 2010-04-20 18:52:41 +0000 | [diff] [blame] | 118 | mFormat = format; |
daniel@transgaming.com | 70d312a | 2010-04-20 18:52:38 +0000 | [diff] [blame] | 119 | } |
| 120 | else |
| 121 | { |
| 122 | mWidth = 0; |
| 123 | mHeight = 0; |
daniel@transgaming.com | 4901fca | 2010-04-20 18:52:41 +0000 | [diff] [blame] | 124 | mFormat = GL_RGBA4; |
daniel@transgaming.com | 70d312a | 2010-04-20 18:52:38 +0000 | [diff] [blame] | 125 | } |
| 126 | } |
| 127 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 128 | Colorbuffer::~Colorbuffer() |
| 129 | { |
| 130 | if (mRenderTarget) |
| 131 | { |
| 132 | mRenderTarget->Release(); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | bool Colorbuffer::isColorbuffer() |
| 137 | { |
| 138 | return true; |
| 139 | } |
| 140 | |
| 141 | GLuint Colorbuffer::getRedSize() |
| 142 | { |
| 143 | if (mRenderTarget) |
| 144 | { |
| 145 | D3DSURFACE_DESC description; |
| 146 | mRenderTarget->GetDesc(&description); |
| 147 | |
| 148 | return es2dx::GetRedSize(description.Format); |
| 149 | } |
| 150 | |
| 151 | return 0; |
| 152 | } |
| 153 | |
| 154 | GLuint Colorbuffer::getGreenSize() |
| 155 | { |
| 156 | if (mRenderTarget) |
| 157 | { |
| 158 | D3DSURFACE_DESC description; |
| 159 | mRenderTarget->GetDesc(&description); |
| 160 | |
| 161 | return es2dx::GetGreenSize(description.Format); |
| 162 | } |
| 163 | |
| 164 | return 0; |
| 165 | } |
| 166 | |
| 167 | GLuint Colorbuffer::getBlueSize() |
| 168 | { |
| 169 | if (mRenderTarget) |
| 170 | { |
| 171 | D3DSURFACE_DESC description; |
| 172 | mRenderTarget->GetDesc(&description); |
| 173 | |
| 174 | return es2dx::GetBlueSize(description.Format); |
| 175 | } |
| 176 | |
| 177 | return 0; |
| 178 | } |
| 179 | |
| 180 | GLuint Colorbuffer::getAlphaSize() |
| 181 | { |
| 182 | if (mRenderTarget) |
| 183 | { |
| 184 | D3DSURFACE_DESC description; |
| 185 | mRenderTarget->GetDesc(&description); |
| 186 | |
| 187 | return es2dx::GetAlphaSize(description.Format); |
| 188 | } |
| 189 | |
| 190 | return 0; |
| 191 | } |
| 192 | |
| 193 | IDirect3DSurface9 *Colorbuffer::getRenderTarget() |
| 194 | { |
| 195 | return mRenderTarget; |
| 196 | } |
| 197 | |
| 198 | Depthbuffer::Depthbuffer(IDirect3DSurface9 *depthStencil) : mDepthStencil(depthStencil) |
| 199 | { |
| 200 | if (depthStencil) |
| 201 | { |
| 202 | depthStencil->AddRef(); |
| 203 | |
| 204 | D3DSURFACE_DESC description; |
| 205 | depthStencil->GetDesc(&description); |
| 206 | |
| 207 | mWidth = description.Width; |
| 208 | mHeight = description.Height; |
daniel@transgaming.com | 4901fca | 2010-04-20 18:52:41 +0000 | [diff] [blame] | 209 | mFormat = GL_DEPTH_COMPONENT16; // If the renderbuffer parameters are queried, the calling function |
| 210 | // will expect one of the valid renderbuffer formats for use in |
| 211 | // glRenderbufferStorage |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 212 | } |
| 213 | } |
| 214 | |
| 215 | Depthbuffer::Depthbuffer(int width, int height) |
| 216 | { |
| 217 | IDirect3DDevice9 *device = getDevice(); |
| 218 | |
| 219 | mDepthStencil = NULL; |
| 220 | HRESULT result = device->CreateDepthStencilSurface(width, height, D3DFMT_D24S8, D3DMULTISAMPLE_NONE, 0, FALSE, &mDepthStencil, 0); |
| 221 | |
| 222 | if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY) |
| 223 | { |
| 224 | error(GL_OUT_OF_MEMORY); |
| 225 | |
| 226 | return; |
| 227 | } |
| 228 | |
| 229 | ASSERT(SUCCEEDED(result)); |
| 230 | |
| 231 | if (mDepthStencil) |
| 232 | { |
| 233 | mWidth = width; |
| 234 | mHeight = height; |
daniel@transgaming.com | 4901fca | 2010-04-20 18:52:41 +0000 | [diff] [blame] | 235 | mFormat = GL_DEPTH_COMPONENT16; // If the renderbuffer parameters are queried, the calling function |
| 236 | // will expect one of the valid renderbuffer formats for use in |
| 237 | // glRenderbufferStorage |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 238 | } |
| 239 | else |
| 240 | { |
| 241 | mWidth = 0; |
| 242 | mHeight = 0; |
daniel@transgaming.com | 4901fca | 2010-04-20 18:52:41 +0000 | [diff] [blame] | 243 | mFormat = GL_RGBA4; //default format |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 244 | } |
| 245 | } |
| 246 | |
| 247 | Depthbuffer::~Depthbuffer() |
| 248 | { |
| 249 | if (mDepthStencil) |
| 250 | { |
| 251 | mDepthStencil->Release(); |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | bool Depthbuffer::isDepthbuffer() |
| 256 | { |
| 257 | return true; |
| 258 | } |
| 259 | |
| 260 | GLuint Depthbuffer::getDepthSize() |
| 261 | { |
| 262 | if (mDepthStencil) |
| 263 | { |
| 264 | D3DSURFACE_DESC description; |
| 265 | mDepthStencil->GetDesc(&description); |
| 266 | |
daniel@transgaming.com | 7a2c280 | 2010-03-16 06:23:33 +0000 | [diff] [blame] | 267 | return es2dx::GetDepthSize(description.Format); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | return 0; |
| 271 | } |
| 272 | |
| 273 | IDirect3DSurface9 *Depthbuffer::getDepthStencil() |
| 274 | { |
| 275 | return mDepthStencil; |
| 276 | } |
| 277 | |
| 278 | Stencilbuffer::Stencilbuffer(IDirect3DSurface9 *depthStencil) : mDepthStencil(depthStencil) |
daniel@transgaming.com | 4a9d65c | 2010-03-08 21:30:56 +0000 | [diff] [blame] | 279 | { |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 280 | if (depthStencil) |
| 281 | { |
| 282 | depthStencil->AddRef(); |
| 283 | |
| 284 | D3DSURFACE_DESC description; |
| 285 | depthStencil->GetDesc(&description); |
| 286 | |
| 287 | mWidth = description.Width; |
| 288 | mHeight = description.Height; |
daniel@transgaming.com | 4901fca | 2010-04-20 18:52:41 +0000 | [diff] [blame] | 289 | mFormat = GL_STENCIL_INDEX8; // If the renderbuffer parameters are queried, the calling function |
| 290 | // will expect one of the valid renderbuffer formats for use in |
| 291 | // glRenderbufferStorage |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 292 | } |
daniel@transgaming.com | 4a9d65c | 2010-03-08 21:30:56 +0000 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | Stencilbuffer::Stencilbuffer(int width, int height) |
| 296 | { |
| 297 | IDirect3DDevice9 *device = getDevice(); |
| 298 | |
| 299 | mDepthStencil = NULL; |
| 300 | HRESULT result = device->CreateDepthStencilSurface(width, height, D3DFMT_D24S8, D3DMULTISAMPLE_NONE, 0, FALSE, &mDepthStencil, 0); |
| 301 | |
| 302 | if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY) |
| 303 | { |
| 304 | error(GL_OUT_OF_MEMORY); |
| 305 | |
| 306 | return; |
| 307 | } |
| 308 | |
| 309 | ASSERT(SUCCEEDED(result)); |
| 310 | |
| 311 | if (mDepthStencil) |
| 312 | { |
| 313 | mWidth = width; |
| 314 | mHeight = height; |
daniel@transgaming.com | 4901fca | 2010-04-20 18:52:41 +0000 | [diff] [blame] | 315 | mFormat = GL_STENCIL_INDEX8; // If the renderbuffer parameters are queried, the calling function |
| 316 | // will expect one of the valid renderbuffer formats for use in |
| 317 | // glRenderbufferStorage |
daniel@transgaming.com | 4a9d65c | 2010-03-08 21:30:56 +0000 | [diff] [blame] | 318 | } |
| 319 | else |
| 320 | { |
| 321 | mWidth = 0; |
| 322 | mHeight = 0; |
daniel@transgaming.com | 4901fca | 2010-04-20 18:52:41 +0000 | [diff] [blame] | 323 | mFormat = GL_RGBA4; //default format |
daniel@transgaming.com | 4a9d65c | 2010-03-08 21:30:56 +0000 | [diff] [blame] | 324 | } |
| 325 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 326 | |
| 327 | Stencilbuffer::~Stencilbuffer() |
| 328 | { |
| 329 | if (mDepthStencil) |
| 330 | { |
| 331 | mDepthStencil->Release(); |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | GLuint Stencilbuffer::getStencilSize() |
| 336 | { |
| 337 | if (mDepthStencil) |
| 338 | { |
| 339 | D3DSURFACE_DESC description; |
| 340 | mDepthStencil->GetDesc(&description); |
| 341 | |
| 342 | return es2dx::GetStencilSize(description.Format); |
| 343 | } |
| 344 | |
| 345 | return 0; |
| 346 | } |
| 347 | |
| 348 | bool Stencilbuffer::isStencilbuffer() |
| 349 | { |
| 350 | return true; |
| 351 | } |
| 352 | |
| 353 | IDirect3DSurface9 *Stencilbuffer::getDepthStencil() |
| 354 | { |
| 355 | return mDepthStencil; |
| 356 | } |
| 357 | } |