blob: fc56db18394eaa3b090bd9e9d19c290277e6e045 [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
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.combbf56f72010-04-20 18:52:13 +000011#include "libGLESv2/Renderbuffer.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000012
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000013#include "libGLESv2/main.h"
enne@chromium.org0fa74632010-09-21 16:18:52 +000014#include "libGLESv2/Texture.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000015#include "libGLESv2/utilities.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000016
17namespace gl
18{
daniel@transgaming.comd14558a2011-11-09 17:46:18 +000019unsigned int RenderbufferInterface::mCurrentSerial = 1;
daniel@transgaming.com092bd482010-05-12 03:39:36 +000020
daniel@transgaming.comd14558a2011-11-09 17:46:18 +000021RenderbufferInterface::RenderbufferInterface() : mSerial(issueSerial())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000022{
daniel@transgaming.comd14558a2011-11-09 17:46:18 +000023}
24
25GLuint RenderbufferInterface::getRedSize() const
26{
27 return dx2es::GetRedSize(getD3DFormat());
28}
29
30GLuint RenderbufferInterface::getGreenSize() const
31{
32 return dx2es::GetGreenSize(getD3DFormat());
33}
34
35GLuint RenderbufferInterface::getBlueSize() const
36{
37 return dx2es::GetBlueSize(getD3DFormat());
38}
39
40GLuint RenderbufferInterface::getAlphaSize() const
41{
42 return dx2es::GetAlphaSize(getD3DFormat());
43}
44
45GLuint RenderbufferInterface::getDepthSize() const
46{
47 return dx2es::GetDepthSize(getD3DFormat());
48}
49
50GLuint RenderbufferInterface::getStencilSize() const
51{
52 return dx2es::GetStencilSize(getD3DFormat());
53}
54
55unsigned int RenderbufferInterface::getSerial() const
56{
57 return mSerial;
58}
59
60unsigned int RenderbufferInterface::issueSerial()
61{
62 return mCurrentSerial++;
63}
64
65RenderbufferTexture::RenderbufferTexture(Texture *texture, GLenum target) : mTexture(texture), mTarget(target)
66{
67}
68
69RenderbufferTexture::~RenderbufferTexture()
70{
71}
72
73IDirect3DSurface9 *RenderbufferTexture::getRenderTarget()
74{
75 return mTexture->getRenderTarget(mTarget);
76}
77
78IDirect3DSurface9 *RenderbufferTexture::getDepthStencil()
79{
80 return NULL;
81}
82
83GLsizei RenderbufferTexture::getWidth() const
84{
85 return mTexture->getWidth();
86}
87
88GLsizei RenderbufferTexture::getHeight() const
89{
90 return mTexture->getHeight();
91}
92
93GLenum RenderbufferTexture::getInternalFormat() const
94{
95 return mTexture->getInternalFormat();
96}
97
98GLsizei RenderbufferTexture::getSamples() const
99{
100 return 0;
101}
102
103D3DFORMAT RenderbufferTexture::getD3DFormat() const
104{
105 return mTexture->getD3DFormat();
106}
107
108Renderbuffer::Renderbuffer(GLuint id, RenderbufferInterface *instance) : RefCountObject(id)
109{
110 ASSERT(instance != NULL);
111 mInstance = instance;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000112}
113
114Renderbuffer::~Renderbuffer()
115{
daniel@transgaming.comd14558a2011-11-09 17:46:18 +0000116 delete mInstance;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000117}
118
119IDirect3DSurface9 *Renderbuffer::getRenderTarget()
120{
daniel@transgaming.comd14558a2011-11-09 17:46:18 +0000121 return mInstance->getRenderTarget();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000122}
123
124IDirect3DSurface9 *Renderbuffer::getDepthStencil()
125{
daniel@transgaming.comd14558a2011-11-09 17:46:18 +0000126 return mInstance->getDepthStencil();
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000127}
128
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +0000129GLsizei Renderbuffer::getWidth() const
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000130{
daniel@transgaming.comd14558a2011-11-09 17:46:18 +0000131 return mInstance->getWidth();
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000132}
133
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +0000134GLsizei Renderbuffer::getHeight() const
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000135{
daniel@transgaming.comd14558a2011-11-09 17:46:18 +0000136 return mInstance->getHeight();
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000137}
138
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +0000139GLenum Renderbuffer::getInternalFormat() const
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000140{
daniel@transgaming.comd14558a2011-11-09 17:46:18 +0000141 return mInstance->getInternalFormat();
142}
143
144D3DFORMAT Renderbuffer::getD3DFormat() const
145{
146 return mInstance->getD3DFormat();
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000147}
148
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +0000149GLuint Renderbuffer::getRedSize() const
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +0000150{
daniel@transgaming.comd14558a2011-11-09 17:46:18 +0000151 return mInstance->getRedSize();
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +0000152}
153
154GLuint Renderbuffer::getGreenSize() const
155{
daniel@transgaming.comd14558a2011-11-09 17:46:18 +0000156 return mInstance->getGreenSize();
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +0000157}
158
159GLuint Renderbuffer::getBlueSize() const
160{
daniel@transgaming.comd14558a2011-11-09 17:46:18 +0000161 return mInstance->getBlueSize();
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +0000162}
163
164GLuint Renderbuffer::getAlphaSize() const
165{
daniel@transgaming.comd14558a2011-11-09 17:46:18 +0000166 return mInstance->getAlphaSize();
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +0000167}
168
169GLuint Renderbuffer::getDepthSize() const
170{
daniel@transgaming.comd14558a2011-11-09 17:46:18 +0000171 return mInstance->getDepthSize();
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +0000172}
173
174GLuint Renderbuffer::getStencilSize() const
175{
daniel@transgaming.comd14558a2011-11-09 17:46:18 +0000176 return mInstance->getStencilSize();
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +0000177}
178
179GLsizei Renderbuffer::getSamples() const
180{
daniel@transgaming.comd14558a2011-11-09 17:46:18 +0000181 return mInstance->getSamples();
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +0000182}
183
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000184unsigned int Renderbuffer::getSerial() const
185{
daniel@transgaming.comd14558a2011-11-09 17:46:18 +0000186 return mInstance->getSerial();
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000187}
188
189void Renderbuffer::setStorage(RenderbufferStorage *newStorage)
190{
191 ASSERT(newStorage != NULL);
192
daniel@transgaming.comd14558a2011-11-09 17:46:18 +0000193 delete mInstance;
194 mInstance = newStorage;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000195}
196
daniel@transgaming.comd14558a2011-11-09 17:46:18 +0000197RenderbufferStorage::RenderbufferStorage()
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000198{
daniel@transgaming.com73a5db62010-10-15 17:58:13 +0000199 mWidth = 0;
200 mHeight = 0;
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +0000201 mInternalFormat = GL_RGBA4;
daniel@transgaming.com73a5db62010-10-15 17:58:13 +0000202 mD3DFormat = D3DFMT_A8R8G8B8;
203 mSamples = 0;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000204}
205
206RenderbufferStorage::~RenderbufferStorage()
207{
208}
209
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000210IDirect3DSurface9 *RenderbufferStorage::getRenderTarget()
211{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000212 return NULL;
213}
214
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000215IDirect3DSurface9 *RenderbufferStorage::getDepthStencil()
216{
217 return NULL;
218}
219
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +0000220GLsizei RenderbufferStorage::getWidth() const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000221{
222 return mWidth;
223}
224
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +0000225GLsizei RenderbufferStorage::getHeight() const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000226{
227 return mHeight;
228}
229
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +0000230GLenum RenderbufferStorage::getInternalFormat() const
daniel@transgaming.com866f3182010-05-20 19:28:22 +0000231{
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +0000232 return mInternalFormat;
daniel@transgaming.com866f3182010-05-20 19:28:22 +0000233}
234
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000235GLsizei RenderbufferStorage::getSamples() const
236{
237 return mSamples;
238}
239
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +0000240D3DFORMAT RenderbufferStorage::getD3DFormat() const
241{
242 return mD3DFormat;
243}
244
daniel@transgaming.comd14558a2011-11-09 17:46:18 +0000245Colorbuffer::Colorbuffer(IDirect3DSurface9 *renderTarget) : mRenderTarget(renderTarget)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000246{
247 if (renderTarget)
248 {
249 renderTarget->AddRef();
250
251 D3DSURFACE_DESC description;
252 renderTarget->GetDesc(&description);
253
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +0000254 mWidth = description.Width;
255 mHeight = description.Height;
256 mInternalFormat = dx2es::ConvertBackBufferFormat(description.Format);
daniel@transgaming.comca7c0082010-08-24 19:20:20 +0000257 mD3DFormat = description.Format;
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +0000258 mSamples = dx2es::GetSamplesFromMultisampleType(description.MultiSampleType);
daniel@transgaming.comca7c0082010-08-24 19:20:20 +0000259 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000260}
261
daniel@transgaming.comd14558a2011-11-09 17:46:18 +0000262Colorbuffer::Colorbuffer(int width, int height, GLenum format, GLsizei samples) : mRenderTarget(NULL)
daniel@transgaming.com70d312a2010-04-20 18:52:38 +0000263{
264 IDirect3DDevice9 *device = getDevice();
265
daniel@transgaming.comca7c0082010-08-24 19:20:20 +0000266 D3DFORMAT requestedFormat = es2dx::ConvertRenderbufferFormat(format);
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000267 int supportedSamples = getContext()->getNearestSupportedSamples(requestedFormat, samples);
268
269 if (supportedSamples == -1)
270 {
271 error(GL_OUT_OF_MEMORY);
272
273 return;
274 }
daniel@transgaming.com70d312a2010-04-20 18:52:38 +0000275
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000276 if (width > 0 && height > 0)
daniel@transgaming.com70d312a2010-04-20 18:52:38 +0000277 {
daniel@transgaming.comca7c0082010-08-24 19:20:20 +0000278 HRESULT result = device->CreateRenderTarget(width, height, requestedFormat,
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000279 es2dx::GetMultisampleTypeFromSamples(supportedSamples), 0, FALSE, &mRenderTarget, NULL);
daniel@transgaming.com70d312a2010-04-20 18:52:38 +0000280
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000281 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
282 {
283 error(GL_OUT_OF_MEMORY);
284
285 return;
286 }
287
288 ASSERT(SUCCEEDED(result));
daniel@transgaming.com70d312a2010-04-20 18:52:38 +0000289 }
290
apatrick@chromium.org831fe2a2011-03-17 18:44:29 +0000291 mWidth = width;
292 mHeight = height;
293 mInternalFormat = format;
294 mD3DFormat = requestedFormat;
295 mSamples = supportedSamples;
daniel@transgaming.com70d312a2010-04-20 18:52:38 +0000296}
297
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000298Colorbuffer::~Colorbuffer()
299{
300 if (mRenderTarget)
301 {
302 mRenderTarget->Release();
303 }
304}
305
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000306IDirect3DSurface9 *Colorbuffer::getRenderTarget()
307{
daniel@transgaming.com5e4dbb32011-11-11 04:10:18 +0000308 if (mRenderTarget)
309 {
310 mRenderTarget->AddRef();
311 }
312
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000313 return mRenderTarget;
314}
315
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +0000316DepthStencilbuffer::DepthStencilbuffer(IDirect3DSurface9 *depthStencil) : mDepthStencil(depthStencil)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000317{
318 if (depthStencil)
319 {
320 depthStencil->AddRef();
321
322 D3DSURFACE_DESC description;
323 depthStencil->GetDesc(&description);
324
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +0000325 mWidth = description.Width;
326 mHeight = description.Height;
327 mInternalFormat = dx2es::ConvertDepthStencilFormat(description.Format);
328 mSamples = dx2es::GetSamplesFromMultisampleType(description.MultiSampleType);
daniel@transgaming.comca7c0082010-08-24 19:20:20 +0000329 mD3DFormat = description.Format;
330 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000331}
332
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000333DepthStencilbuffer::DepthStencilbuffer(int width, int height, GLsizei samples)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000334{
335 IDirect3DDevice9 *device = getDevice();
336
337 mDepthStencil = NULL;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000338
339 int supportedSamples = getContext()->getNearestSupportedSamples(D3DFMT_D24S8, samples);
340
341 if (supportedSamples == -1)
342 {
343 error(GL_OUT_OF_MEMORY);
344
345 return;
346 }
347
apatrick@chromium.org831fe2a2011-03-17 18:44:29 +0000348 if (width > 0 && height > 0)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000349 {
apatrick@chromium.org831fe2a2011-03-17 18:44:29 +0000350 HRESULT result = device->CreateDepthStencilSurface(width, height, D3DFMT_D24S8, es2dx::GetMultisampleTypeFromSamples(supportedSamples),
351 0, FALSE, &mDepthStencil, 0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000352
apatrick@chromium.org831fe2a2011-03-17 18:44:29 +0000353 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
354 {
355 error(GL_OUT_OF_MEMORY);
356
357 return;
358 }
359
360 ASSERT(SUCCEEDED(result));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000361 }
362
apatrick@chromium.org831fe2a2011-03-17 18:44:29 +0000363 mWidth = width;
364 mHeight = height;
365 mInternalFormat = GL_DEPTH24_STENCIL8_OES;
366 mD3DFormat = D3DFMT_D24S8;
367 mSamples = supportedSamples;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000368}
369
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +0000370DepthStencilbuffer::~DepthStencilbuffer()
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000371{
372 if (mDepthStencil)
373 {
374 mDepthStencil->Release();
375 }
376}
377
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +0000378IDirect3DSurface9 *DepthStencilbuffer::getDepthStencil()
379{
380 return mDepthStencil;
381}
382
383Depthbuffer::Depthbuffer(IDirect3DSurface9 *depthStencil) : DepthStencilbuffer(depthStencil)
384{
385 if (depthStencil)
386 {
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +0000387 mInternalFormat = GL_DEPTH_COMPONENT16; // If the renderbuffer parameters are queried, the calling function
388 // will expect one of the valid renderbuffer formats for use in
389 // glRenderbufferStorage
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +0000390 }
391}
392
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000393Depthbuffer::Depthbuffer(int width, int height, GLsizei samples) : DepthStencilbuffer(width, height, samples)
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +0000394{
daniel@transgaming.comd14558a2011-11-09 17:46:18 +0000395 if (mDepthStencil)
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +0000396 {
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +0000397 mInternalFormat = GL_DEPTH_COMPONENT16; // If the renderbuffer parameters are queried, the calling function
398 // will expect one of the valid renderbuffer formats for use in
399 // glRenderbufferStorage
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +0000400 }
401}
402
403Depthbuffer::~Depthbuffer()
404{
405}
406
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +0000407Stencilbuffer::Stencilbuffer(IDirect3DSurface9 *depthStencil) : DepthStencilbuffer(depthStencil)
408{
409 if (depthStencil)
410 {
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +0000411 mInternalFormat = GL_STENCIL_INDEX8; // If the renderbuffer parameters are queried, the calling function
412 // will expect one of the valid renderbuffer formats for use in
413 // glRenderbufferStorage
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +0000414 }
415}
416
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000417Stencilbuffer::Stencilbuffer(int width, int height, GLsizei samples) : DepthStencilbuffer(width, height, samples)
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +0000418{
daniel@transgaming.comd14558a2011-11-09 17:46:18 +0000419 if (mDepthStencil)
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +0000420 {
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +0000421 mInternalFormat = GL_STENCIL_INDEX8; // If the renderbuffer parameters are queried, the calling function
422 // will expect one of the valid renderbuffer formats for use in
423 // glRenderbufferStorage
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +0000424 }
425}
426
427Stencilbuffer::~Stencilbuffer()
428{
429}
430
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000431}