blob: 8c5114a376a603badfda3607f88b1c8268886f6d [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// Framebuffer.cpp: Implements the gl::Framebuffer class. Implements GL framebuffer
8// objects and related functionality. [OpenGL ES 2.0.24] section 4.4 page 105.
9
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000010#include "libGLESv2/Framebuffer.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000011
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000012#include "libGLESv2/main.h"
13#include "libGLESv2/Renderbuffer.h"
14#include "libGLESv2/Texture.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000015
16namespace gl
17{
18Framebuffer::Framebuffer()
19{
20 mColorbufferType = GL_NONE;
21 mColorbufferHandle = 0;
22
23 mDepthbufferType = GL_NONE;
24 mDepthbufferHandle = 0;
25
26 mStencilbufferType = GL_NONE;
27 mStencilbufferHandle = 0;
28}
29
30Framebuffer::~Framebuffer()
31{
32}
33
34void Framebuffer::setColorbuffer(GLenum type, GLuint colorbuffer)
35{
36 mColorbufferType = type;
37 mColorbufferHandle = colorbuffer;
38}
39
40void Framebuffer::setDepthbuffer(GLenum type, GLuint depthbuffer)
41{
42 mDepthbufferType = type;
43 mDepthbufferHandle = depthbuffer;
44}
45
46void Framebuffer::setStencilbuffer(GLenum type, GLuint stencilbuffer)
47{
48 mStencilbufferType = type;
49 mStencilbufferHandle = stencilbuffer;
50}
51
52void Framebuffer::detachTexture(GLuint texture)
53{
54 if (mColorbufferHandle == texture && mColorbufferType == GL_TEXTURE)
55 {
56 mColorbufferType = GL_NONE;
57 mColorbufferHandle = 0;
58 }
59}
60
61void Framebuffer::detachRenderbuffer(GLuint renderbuffer)
62{
63 if (mColorbufferHandle == renderbuffer && mColorbufferType == GL_RENDERBUFFER)
64 {
65 mColorbufferType = GL_NONE;
66 mColorbufferHandle = 0;
67 }
68
69 if (mDepthbufferHandle == renderbuffer && mDepthbufferType == GL_RENDERBUFFER)
70 {
71 mDepthbufferType = GL_NONE;
72 mDepthbufferHandle = 0;
73 }
74
75 if (mStencilbufferHandle == renderbuffer && mStencilbufferType == GL_RENDERBUFFER)
76 {
77 mStencilbufferType = GL_NONE;
78 mStencilbufferHandle = 0;
79 }
80}
81
82IDirect3DSurface9 *Framebuffer::getRenderTarget()
83{
84 Renderbuffer *colorbuffer = getColorbuffer();
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +000085
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000086 if (colorbuffer)
87 {
88 return colorbuffer->getRenderTarget();
89 }
90
91 return NULL;
92}
93
94IDirect3DSurface9 *Framebuffer::getDepthStencil()
95{
96 gl::Context *context = gl::getContext();
97 Depthbuffer *depthbuffer = context->getDepthbuffer(mDepthbufferHandle);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +000098
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000099 if (depthbuffer)
100 {
101 return depthbuffer->getDepthStencil();
102 }
103
104 return NULL;
105}
106
107Colorbuffer *Framebuffer::getColorbuffer()
108{
109 gl::Context *context = gl::getContext();
110 Colorbuffer *colorbuffer = NULL;
111
112 if (mColorbufferType == GL_RENDERBUFFER)
113 {
114 colorbuffer = context->getColorbuffer(mColorbufferHandle);
115 }
116 else if (mColorbufferType == GL_TEXTURE)
117 {
118 colorbuffer = context->getTexture(mColorbufferHandle);
119 }
120 else UNREACHABLE();
121
122 if (colorbuffer && colorbuffer->isColorbuffer())
123 {
124 return colorbuffer;
125 }
126
127 return NULL;
128}
129
130Depthbuffer *Framebuffer::getDepthbuffer()
131{
132 gl::Context *context = gl::getContext();
133 Depthbuffer *depthbuffer = context->getDepthbuffer(mDepthbufferHandle);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +0000134
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000135 if (depthbuffer && depthbuffer->isDepthbuffer())
136 {
137 return depthbuffer;
138 }
139
140 return NULL;
141}
142
143Stencilbuffer *Framebuffer::getStencilbuffer()
144{
145 gl::Context *context = gl::getContext();
146 Stencilbuffer *stencilbuffer = context->getStencilbuffer(mStencilbufferHandle);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +0000147
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000148 if (stencilbuffer && stencilbuffer->isStencilbuffer())
149 {
150 return stencilbuffer;
151 }
152
153 return NULL;
154}
155
156GLenum Framebuffer::completeness()
157{
158 gl::Context *context = gl::getContext();
159
160 int width = 0;
161 int height = 0;
162
163 if (mColorbufferType != GL_NONE)
164 {
165 Colorbuffer *colorbuffer = getColorbuffer();
166
167 if (!colorbuffer)
168 {
169 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
170 }
171
172 if (colorbuffer->getWidth() == 0 || colorbuffer->getHeight() == 0)
173 {
174 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
175 }
176
177 width = colorbuffer->getWidth();
178 height = colorbuffer->getHeight();
179 }
180
181 if (mDepthbufferType != GL_NONE)
182 {
183 Depthbuffer *depthbuffer = context->getDepthbuffer(mDepthbufferHandle);
184
185 if (!depthbuffer)
186 {
187 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
188 }
189
190 if (depthbuffer->getWidth() == 0 || depthbuffer->getHeight() == 0)
191 {
192 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
193 }
194
195 if (width == 0)
196 {
197 width = depthbuffer->getWidth();
198 height = depthbuffer->getHeight();
199 }
200 else if (width != depthbuffer->getWidth() || height != depthbuffer->getHeight())
201 {
202 return GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS;
203 }
204 }
205
206 if (mStencilbufferType != GL_NONE)
207 {
208 Stencilbuffer *stencilbuffer = context->getStencilbuffer(mStencilbufferHandle);
209
210 if (!stencilbuffer)
211 {
212 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
213 }
214
215 if (stencilbuffer->getWidth() == 0 || stencilbuffer->getHeight() == 0)
216 {
217 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
218 }
219
220 if (width == 0)
221 {
222 width = stencilbuffer->getWidth();
223 height = stencilbuffer->getHeight();
224 }
225 else if (width != stencilbuffer->getWidth() || height != stencilbuffer->getHeight())
226 {
227 return GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS;
228 }
229 }
230
231 return GL_FRAMEBUFFER_COMPLETE;
232}
233}