blob: 74d60f262c3ee734d0aa563744350e474eb231bc [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{
308 return mRenderTarget;
309}
310
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +0000311DepthStencilbuffer::DepthStencilbuffer(IDirect3DSurface9 *depthStencil) : mDepthStencil(depthStencil)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000312{
313 if (depthStencil)
314 {
315 depthStencil->AddRef();
316
317 D3DSURFACE_DESC description;
318 depthStencil->GetDesc(&description);
319
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +0000320 mWidth = description.Width;
321 mHeight = description.Height;
322 mInternalFormat = dx2es::ConvertDepthStencilFormat(description.Format);
323 mSamples = dx2es::GetSamplesFromMultisampleType(description.MultiSampleType);
daniel@transgaming.comca7c0082010-08-24 19:20:20 +0000324 mD3DFormat = description.Format;
325 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000326}
327
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000328DepthStencilbuffer::DepthStencilbuffer(int width, int height, GLsizei samples)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000329{
330 IDirect3DDevice9 *device = getDevice();
331
332 mDepthStencil = NULL;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000333
334 int supportedSamples = getContext()->getNearestSupportedSamples(D3DFMT_D24S8, samples);
335
336 if (supportedSamples == -1)
337 {
338 error(GL_OUT_OF_MEMORY);
339
340 return;
341 }
342
apatrick@chromium.org831fe2a2011-03-17 18:44:29 +0000343 if (width > 0 && height > 0)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000344 {
apatrick@chromium.org831fe2a2011-03-17 18:44:29 +0000345 HRESULT result = device->CreateDepthStencilSurface(width, height, D3DFMT_D24S8, es2dx::GetMultisampleTypeFromSamples(supportedSamples),
346 0, FALSE, &mDepthStencil, 0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000347
apatrick@chromium.org831fe2a2011-03-17 18:44:29 +0000348 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
349 {
350 error(GL_OUT_OF_MEMORY);
351
352 return;
353 }
354
355 ASSERT(SUCCEEDED(result));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000356 }
357
apatrick@chromium.org831fe2a2011-03-17 18:44:29 +0000358 mWidth = width;
359 mHeight = height;
360 mInternalFormat = GL_DEPTH24_STENCIL8_OES;
361 mD3DFormat = D3DFMT_D24S8;
362 mSamples = supportedSamples;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000363}
364
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +0000365DepthStencilbuffer::~DepthStencilbuffer()
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000366{
367 if (mDepthStencil)
368 {
369 mDepthStencil->Release();
370 }
371}
372
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +0000373IDirect3DSurface9 *DepthStencilbuffer::getDepthStencil()
374{
375 return mDepthStencil;
376}
377
378Depthbuffer::Depthbuffer(IDirect3DSurface9 *depthStencil) : DepthStencilbuffer(depthStencil)
379{
380 if (depthStencil)
381 {
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +0000382 mInternalFormat = GL_DEPTH_COMPONENT16; // If the renderbuffer parameters are queried, the calling function
383 // will expect one of the valid renderbuffer formats for use in
384 // glRenderbufferStorage
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +0000385 }
386}
387
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000388Depthbuffer::Depthbuffer(int width, int height, GLsizei samples) : DepthStencilbuffer(width, height, samples)
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +0000389{
daniel@transgaming.comd14558a2011-11-09 17:46:18 +0000390 if (mDepthStencil)
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +0000391 {
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +0000392 mInternalFormat = GL_DEPTH_COMPONENT16; // If the renderbuffer parameters are queried, the calling function
393 // will expect one of the valid renderbuffer formats for use in
394 // glRenderbufferStorage
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +0000395 }
396}
397
398Depthbuffer::~Depthbuffer()
399{
400}
401
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +0000402Stencilbuffer::Stencilbuffer(IDirect3DSurface9 *depthStencil) : DepthStencilbuffer(depthStencil)
403{
404 if (depthStencil)
405 {
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +0000406 mInternalFormat = GL_STENCIL_INDEX8; // If the renderbuffer parameters are queried, the calling function
407 // will expect one of the valid renderbuffer formats for use in
408 // glRenderbufferStorage
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +0000409 }
410}
411
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000412Stencilbuffer::Stencilbuffer(int width, int height, GLsizei samples) : DepthStencilbuffer(width, height, samples)
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +0000413{
daniel@transgaming.comd14558a2011-11-09 17:46:18 +0000414 if (mDepthStencil)
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +0000415 {
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +0000416 mInternalFormat = GL_STENCIL_INDEX8; // If the renderbuffer parameters are queried, the calling function
417 // will expect one of the valid renderbuffer formats for use in
418 // glRenderbufferStorage
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +0000419 }
420}
421
422Stencilbuffer::~Stencilbuffer()
423{
424}
425
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000426}