blob: 32eabcfeda14218c9a93ed85cc45e216241e523a [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.com4901fca2010-04-20 18:52:41 +000067GLenum Renderbuffer::getFormat()
68{
69 return mFormat;
70}
71
daniel@transgaming.com092bd482010-05-12 03:39:36 +000072unsigned int Renderbuffer::getSerial() const
73{
74 return mSerial;
75}
76
77unsigned int Renderbuffer::issueSerial()
78{
79 return mCurrentSerial++;
80}
81
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000082Colorbuffer::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.com092bd482010-05-12 03:39:36 +000094
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000095}
96
daniel@transgaming.com70d312a2010-04-20 18:52:38 +000097Colorbuffer::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.com4901fca2010-04-20 18:52:41 +0000118 mFormat = format;
daniel@transgaming.com70d312a2010-04-20 18:52:38 +0000119 }
120 else
121 {
122 mWidth = 0;
123 mHeight = 0;
daniel@transgaming.com4901fca2010-04-20 18:52:41 +0000124 mFormat = GL_RGBA4;
daniel@transgaming.com70d312a2010-04-20 18:52:38 +0000125 }
126}
127
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000128Colorbuffer::~Colorbuffer()
129{
130 if (mRenderTarget)
131 {
132 mRenderTarget->Release();
133 }
134}
135
136bool Colorbuffer::isColorbuffer()
137{
138 return true;
139}
140
141GLuint 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
154GLuint 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
167GLuint 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
180GLuint 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
193IDirect3DSurface9 *Colorbuffer::getRenderTarget()
194{
195 return mRenderTarget;
196}
197
198Depthbuffer::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.com4901fca2010-04-20 18:52:41 +0000209 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.com4f39fd92010-03-08 20:26:45 +0000212 }
213}
214
215Depthbuffer::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.com4901fca2010-04-20 18:52:41 +0000235 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.com4f39fd92010-03-08 20:26:45 +0000238 }
239 else
240 {
241 mWidth = 0;
242 mHeight = 0;
daniel@transgaming.com4901fca2010-04-20 18:52:41 +0000243 mFormat = GL_RGBA4; //default format
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000244 }
245}
246
247Depthbuffer::~Depthbuffer()
248{
249 if (mDepthStencil)
250 {
251 mDepthStencil->Release();
252 }
253}
254
255bool Depthbuffer::isDepthbuffer()
256{
257 return true;
258}
259
260GLuint Depthbuffer::getDepthSize()
261{
262 if (mDepthStencil)
263 {
264 D3DSURFACE_DESC description;
265 mDepthStencil->GetDesc(&description);
266
daniel@transgaming.com7a2c2802010-03-16 06:23:33 +0000267 return es2dx::GetDepthSize(description.Format);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000268 }
269
270 return 0;
271}
272
273IDirect3DSurface9 *Depthbuffer::getDepthStencil()
274{
275 return mDepthStencil;
276}
277
278Stencilbuffer::Stencilbuffer(IDirect3DSurface9 *depthStencil) : mDepthStencil(depthStencil)
daniel@transgaming.com4a9d65c2010-03-08 21:30:56 +0000279{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000280 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.com4901fca2010-04-20 18:52:41 +0000289 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.com4f39fd92010-03-08 20:26:45 +0000292 }
daniel@transgaming.com4a9d65c2010-03-08 21:30:56 +0000293}
294
295Stencilbuffer::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.com4901fca2010-04-20 18:52:41 +0000315 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.com4a9d65c2010-03-08 21:30:56 +0000318 }
319 else
320 {
321 mWidth = 0;
322 mHeight = 0;
daniel@transgaming.com4901fca2010-04-20 18:52:41 +0000323 mFormat = GL_RGBA4; //default format
daniel@transgaming.com4a9d65c2010-03-08 21:30:56 +0000324 }
325}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000326
327Stencilbuffer::~Stencilbuffer()
328{
329 if (mDepthStencil)
330 {
331 mDepthStencil->Release();
332 }
333}
334
335GLuint 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
348bool Stencilbuffer::isStencilbuffer()
349{
350 return true;
351}
352
353IDirect3DSurface9 *Stencilbuffer::getDepthStencil()
354{
355 return mDepthStencil;
356}
357}