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