blob: 318b86236e472e68791fe45eea13a36b1b816fb2 [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"
14#include "libGLESv2/utilities.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000015
16namespace gl
17{
daniel@transgaming.com092bd482010-05-12 03:39:36 +000018unsigned int Renderbuffer::mCurrentSerial = 1;
19
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000020Renderbuffer::Renderbuffer()
21{
22 mWidth = 0;
23 mHeight = 0;
daniel@transgaming.com4901fca2010-04-20 18:52:41 +000024 mFormat = GL_RGBA4; // default format, needs to be one of the expected renderbuffer formats
daniel@transgaming.com092bd482010-05-12 03:39:36 +000025 mSerial = issueSerial();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000026}
27
28Renderbuffer::~Renderbuffer()
29{
30}
31
32bool Renderbuffer::isColorbuffer()
33{
34 return false;
35}
36
37bool Renderbuffer::isDepthbuffer()
38{
39 return false;
40}
41
42bool Renderbuffer::isStencilbuffer()
43{
44 return false;
45}
46
47IDirect3DSurface9 *Renderbuffer::getRenderTarget()
48{
49 return NULL;
50}
51
52IDirect3DSurface9 *Renderbuffer::getDepthStencil()
53{
54 return NULL;
55}
56
57int Renderbuffer::getWidth()
58{
59 return mWidth;
60}
61
62int Renderbuffer::getHeight()
63{
64 return mHeight;
65}
66
daniel@transgaming.com866f3182010-05-20 19:28:22 +000067void Renderbuffer::setSize(int width, int height)
68{
69 mWidth = width;
70 mHeight = height;
71}
72
73
daniel@transgaming.com4901fca2010-04-20 18:52:41 +000074GLenum Renderbuffer::getFormat()
75{
76 return mFormat;
77}
78
daniel@transgaming.com092bd482010-05-12 03:39:36 +000079unsigned int Renderbuffer::getSerial() const
80{
81 return mSerial;
82}
83
84unsigned int Renderbuffer::issueSerial()
85{
86 return mCurrentSerial++;
87}
88
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000089Colorbuffer::Colorbuffer(IDirect3DSurface9 *renderTarget) : mRenderTarget(renderTarget)
90{
91 if (renderTarget)
92 {
93 renderTarget->AddRef();
94
95 D3DSURFACE_DESC description;
96 renderTarget->GetDesc(&description);
97
daniel@transgaming.com866f3182010-05-20 19:28:22 +000098 setSize(description.Width, description.Height);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000099 }
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000100
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000101}
102
daniel@transgaming.com70d312a2010-04-20 18:52:38 +0000103Colorbuffer::Colorbuffer(int width, int height, GLenum format)
104{
105 IDirect3DDevice9 *device = getDevice();
106
107 mRenderTarget = NULL;
108 HRESULT result = device->CreateRenderTarget(width, height, es2dx::ConvertRenderbufferFormat(format),
109 D3DMULTISAMPLE_NONE, 0, FALSE, &mRenderTarget, NULL);
110
111 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
112 {
113 error(GL_OUT_OF_MEMORY);
114
115 return;
116 }
117
118 ASSERT(SUCCEEDED(result));
119
120 if (mRenderTarget)
121 {
daniel@transgaming.com866f3182010-05-20 19:28:22 +0000122 setSize(width, height);
daniel@transgaming.com4901fca2010-04-20 18:52:41 +0000123 mFormat = format;
daniel@transgaming.com70d312a2010-04-20 18:52:38 +0000124 }
125 else
126 {
daniel@transgaming.com866f3182010-05-20 19:28:22 +0000127 setSize(0, 0);
daniel@transgaming.com4901fca2010-04-20 18:52:41 +0000128 mFormat = GL_RGBA4;
daniel@transgaming.com70d312a2010-04-20 18:52:38 +0000129 }
130}
131
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000132Colorbuffer::~Colorbuffer()
133{
134 if (mRenderTarget)
135 {
136 mRenderTarget->Release();
137 }
138}
139
140bool Colorbuffer::isColorbuffer()
141{
142 return true;
143}
144
145GLuint Colorbuffer::getRedSize()
146{
147 if (mRenderTarget)
148 {
149 D3DSURFACE_DESC description;
150 mRenderTarget->GetDesc(&description);
151
152 return es2dx::GetRedSize(description.Format);
153 }
154
155 return 0;
156}
157
158GLuint Colorbuffer::getGreenSize()
159{
160 if (mRenderTarget)
161 {
162 D3DSURFACE_DESC description;
163 mRenderTarget->GetDesc(&description);
164
165 return es2dx::GetGreenSize(description.Format);
166 }
167
168 return 0;
169}
170
171GLuint Colorbuffer::getBlueSize()
172{
173 if (mRenderTarget)
174 {
175 D3DSURFACE_DESC description;
176 mRenderTarget->GetDesc(&description);
177
178 return es2dx::GetBlueSize(description.Format);
179 }
180
181 return 0;
182}
183
184GLuint Colorbuffer::getAlphaSize()
185{
186 if (mRenderTarget)
187 {
188 D3DSURFACE_DESC description;
189 mRenderTarget->GetDesc(&description);
190
191 return es2dx::GetAlphaSize(description.Format);
192 }
193
194 return 0;
195}
196
197IDirect3DSurface9 *Colorbuffer::getRenderTarget()
198{
199 return mRenderTarget;
200}
201
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +0000202DepthStencilbuffer::DepthStencilbuffer(IDirect3DSurface9 *depthStencil) : mDepthStencil(depthStencil)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000203{
204 if (depthStencil)
205 {
206 depthStencil->AddRef();
207
208 D3DSURFACE_DESC description;
209 depthStencil->GetDesc(&description);
210
daniel@transgaming.com866f3182010-05-20 19:28:22 +0000211 setSize(description.Width, description.Height);
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +0000212 mFormat = GL_DEPTH24_STENCIL8_OES;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000213 }
214}
215
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +0000216DepthStencilbuffer::DepthStencilbuffer(int width, int height)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000217{
218 IDirect3DDevice9 *device = getDevice();
219
220 mDepthStencil = NULL;
221 HRESULT result = device->CreateDepthStencilSurface(width, height, D3DFMT_D24S8, D3DMULTISAMPLE_NONE, 0, FALSE, &mDepthStencil, 0);
222
223 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
224 {
225 error(GL_OUT_OF_MEMORY);
226
227 return;
228 }
229
230 ASSERT(SUCCEEDED(result));
231
232 if (mDepthStencil)
233 {
daniel@transgaming.com866f3182010-05-20 19:28:22 +0000234 setSize(width, height);
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +0000235 mFormat = GL_DEPTH24_STENCIL8_OES;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000236 }
237 else
238 {
daniel@transgaming.com866f3182010-05-20 19:28:22 +0000239 setSize(0, 0);
daniel@transgaming.com4901fca2010-04-20 18:52:41 +0000240 mFormat = GL_RGBA4; //default format
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000241 }
242}
243
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +0000244DepthStencilbuffer::~DepthStencilbuffer()
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000245{
246 if (mDepthStencil)
247 {
248 mDepthStencil->Release();
249 }
250}
251
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +0000252bool DepthStencilbuffer::isDepthbuffer()
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000253{
254 return true;
255}
256
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +0000257bool DepthStencilbuffer::isStencilbuffer()
258{
259 return true;
260}
261
262GLuint DepthStencilbuffer::getDepthSize()
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000263{
264 if (mDepthStencil)
265 {
266 D3DSURFACE_DESC description;
267 mDepthStencil->GetDesc(&description);
268
daniel@transgaming.com7a2c2802010-03-16 06:23:33 +0000269 return es2dx::GetDepthSize(description.Format);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000270 }
271
272 return 0;
273}
274
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +0000275GLuint DepthStencilbuffer::getStencilSize()
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000276{
277 if (mDepthStencil)
278 {
279 D3DSURFACE_DESC description;
280 mDepthStencil->GetDesc(&description);
281
282 return es2dx::GetStencilSize(description.Format);
283 }
284
285 return 0;
286}
287
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +0000288IDirect3DSurface9 *DepthStencilbuffer::getDepthStencil()
289{
290 return mDepthStencil;
291}
292
293Depthbuffer::Depthbuffer(IDirect3DSurface9 *depthStencil) : DepthStencilbuffer(depthStencil)
294{
295 if (depthStencil)
296 {
297 mFormat = GL_DEPTH_COMPONENT16; // If the renderbuffer parameters are queried, the calling function
298 // will expect one of the valid renderbuffer formats for use in
299 // glRenderbufferStorage
300 }
301}
302
303Depthbuffer::Depthbuffer(int width, int height) : DepthStencilbuffer(width, height)
304{
305 if (getDepthStencil())
306 {
307 mFormat = GL_DEPTH_COMPONENT16; // If the renderbuffer parameters are queried, the calling function
308 // will expect one of the valid renderbuffer formats for use in
309 // glRenderbufferStorage
310 }
311}
312
313Depthbuffer::~Depthbuffer()
314{
315}
316
317bool Depthbuffer::isDepthbuffer()
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000318{
319 return true;
320}
321
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +0000322bool Depthbuffer::isStencilbuffer()
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000323{
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +0000324 return false;
325}
326
327Stencilbuffer::Stencilbuffer(IDirect3DSurface9 *depthStencil) : DepthStencilbuffer(depthStencil)
328{
329 if (depthStencil)
330 {
331 mFormat = GL_STENCIL_INDEX8; // If the renderbuffer parameters are queried, the calling function
332 // will expect one of the valid renderbuffer formats for use in
333 // glRenderbufferStorage
334 }
335 else
336 {
337 mFormat = GL_RGBA4; //default format
338 }
339}
340
341Stencilbuffer::Stencilbuffer(int width, int height) : DepthStencilbuffer(width, height)
342{
343 if (getDepthStencil())
344 {
345 mFormat = GL_STENCIL_INDEX8; // If the renderbuffer parameters are queried, the calling function
346 // will expect one of the valid renderbuffer formats for use in
347 // glRenderbufferStorage
348 }
349}
350
351Stencilbuffer::~Stencilbuffer()
352{
353}
354
355bool Stencilbuffer::isDepthbuffer()
356{
357 return false;
358}
359
360bool Stencilbuffer::isStencilbuffer()
361{
362 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000363}
364}