OSDN Git Service

Stricter FBO completeness checks
[android-x86/device-generic-goldfish-opengl.git] / shared / OpenglCodecCommon / GLClientState.cpp
1 /*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 #include "GLClientState.h"
17 #include "ErrorLog.h"
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include "glUtils.h"
22 #include <cutils/log.h>
23
24 #ifndef MAX
25 #define MAX(a, b) ((a) < (b) ? (b) : (a))
26 #endif
27
28 GLClientState::GLClientState(int nLocations)
29 {
30     if (nLocations < LAST_LOCATION) {
31         nLocations = LAST_LOCATION;
32     }
33     m_nLocations = nLocations;
34     m_states = new VertexAttribState[m_nLocations];
35     for (int i = 0; i < m_nLocations; i++) {
36         m_states[i].enabled = 0;
37         m_states[i].enableDirty = false;
38         m_states[i].data = 0;
39     }
40     m_currentArrayVbo = 0;
41     m_currentIndexVbo = 0;
42     // init gl constans;
43     m_states[VERTEX_LOCATION].glConst = GL_VERTEX_ARRAY;
44     m_states[NORMAL_LOCATION].glConst = GL_NORMAL_ARRAY;
45     m_states[COLOR_LOCATION].glConst = GL_COLOR_ARRAY;
46     m_states[POINTSIZE_LOCATION].glConst = GL_POINT_SIZE_ARRAY_OES;
47     m_states[TEXCOORD0_LOCATION].glConst = GL_TEXTURE_COORD_ARRAY;
48     m_states[TEXCOORD1_LOCATION].glConst = GL_TEXTURE_COORD_ARRAY;
49     m_states[TEXCOORD2_LOCATION].glConst = GL_TEXTURE_COORD_ARRAY;
50     m_states[TEXCOORD3_LOCATION].glConst = GL_TEXTURE_COORD_ARRAY;
51     m_states[TEXCOORD4_LOCATION].glConst = GL_TEXTURE_COORD_ARRAY;
52     m_states[TEXCOORD5_LOCATION].glConst = GL_TEXTURE_COORD_ARRAY;
53     m_states[TEXCOORD6_LOCATION].glConst = GL_TEXTURE_COORD_ARRAY;
54     m_states[TEXCOORD7_LOCATION].glConst = GL_TEXTURE_COORD_ARRAY;
55     m_states[MATRIXINDEX_LOCATION].glConst = GL_MATRIX_INDEX_ARRAY_OES;
56     m_states[WEIGHT_LOCATION].glConst = GL_WEIGHT_ARRAY_OES;
57     m_activeTexture = 0;
58     m_currentProgram = 0;
59
60     m_pixelStore.unpack_alignment = 4;
61     m_pixelStore.pack_alignment = 4;
62
63     memset(m_tex.unit, 0, sizeof(m_tex.unit));
64     m_tex.activeUnit = &m_tex.unit[0];
65     m_tex.textures = NULL;
66     m_tex.numTextures = 0;
67     m_tex.allocTextures = 0;
68
69     mRboState.boundRenderbuffer = 0;
70     mRboState.boundRenderbufferIndex = 0;
71     addFreshRenderbuffer(0);
72
73     mFboState.boundFramebuffer = 0;
74     mFboState.boundFramebufferIndex = 0;
75     mFboState.fboCheckStatus = GL_NONE;
76     addFreshFramebuffer(0);
77
78     m_maxVertexAttribsDirty = true;
79 }
80
81 GLClientState::~GLClientState()
82 {
83     delete m_states;
84 }
85
86 void GLClientState::enable(int location, int state)
87 {
88     if (!validLocation(location)) {
89         return;
90     }
91
92     m_states[location].enableDirty |= (state != m_states[location].enabled);
93     m_states[location].enabled = state;
94 }
95
96 void GLClientState::setState(int location, int size, GLenum type, GLboolean normalized, GLsizei stride, const void *data)
97 {
98     if (!validLocation(location)) {
99         return;
100     }
101     m_states[location].size = size;
102     m_states[location].type = type;
103     m_states[location].stride = stride;
104     m_states[location].data = (void*)data;
105     m_states[location].bufferObject = m_currentArrayVbo;
106     m_states[location].elementSize = size ? (glSizeof(type) * size) : 0;
107     m_states[location].normalized = normalized;
108 }
109
110 void GLClientState::setBufferObject(int location, GLuint id)
111 {
112     if (!validLocation(location)) {
113         return;
114     }
115
116     m_states[location].bufferObject = id;
117 }
118
119 const GLClientState::VertexAttribState * GLClientState::getState(int location)
120 {
121     if (!validLocation(location)) {
122         return NULL;
123     }
124     return & m_states[location];
125 }
126
127 const GLClientState::VertexAttribState * GLClientState::getStateAndEnableDirty(int location, bool *enableChanged)
128 {
129     if (!validLocation(location)) {
130         return NULL;
131     }
132
133     if (enableChanged) {
134         *enableChanged = m_states[location].enableDirty;
135     }
136
137     m_states[location].enableDirty = false;
138     return & m_states[location];
139 }
140
141 int GLClientState::getLocation(GLenum loc)
142 {
143     int retval;
144
145     switch(loc) {
146     case GL_VERTEX_ARRAY:
147         retval = int(VERTEX_LOCATION);
148         break;
149     case GL_NORMAL_ARRAY:
150         retval = int(NORMAL_LOCATION);
151         break;
152     case GL_COLOR_ARRAY:
153         retval = int(COLOR_LOCATION);
154         break;
155     case GL_POINT_SIZE_ARRAY_OES:
156         retval = int(POINTSIZE_LOCATION);
157         break;
158     case GL_TEXTURE_COORD_ARRAY:
159         retval = int (TEXCOORD0_LOCATION + m_activeTexture);
160         break;
161     case GL_MATRIX_INDEX_ARRAY_OES:
162         retval = int (MATRIXINDEX_LOCATION);
163         break;
164     case GL_WEIGHT_ARRAY_OES:
165         retval = int (WEIGHT_LOCATION);
166         break;
167     default:
168         retval = loc;
169     }
170     return retval;
171 }
172
173 void GLClientState::getClientStatePointer(GLenum pname, GLvoid** params)
174 {
175     const GLClientState::VertexAttribState *state = NULL;
176     switch (pname) {
177     case GL_VERTEX_ARRAY_POINTER: {
178         state = getState(GLClientState::VERTEX_LOCATION);
179         break;
180         }
181     case GL_NORMAL_ARRAY_POINTER: {
182         state = getState(GLClientState::NORMAL_LOCATION);
183         break;
184         }
185     case GL_COLOR_ARRAY_POINTER: {
186         state = getState(GLClientState::COLOR_LOCATION);
187         break;
188         }
189     case GL_TEXTURE_COORD_ARRAY_POINTER: {
190         state = getState(getActiveTexture() + GLClientState::TEXCOORD0_LOCATION);
191         break;
192         }
193     case GL_POINT_SIZE_ARRAY_POINTER_OES: {
194         state = getState(GLClientState::POINTSIZE_LOCATION);
195         break;
196         }
197     case GL_MATRIX_INDEX_ARRAY_POINTER_OES: {
198         state = getState(GLClientState::MATRIXINDEX_LOCATION);
199         break;
200         }
201     case GL_WEIGHT_ARRAY_POINTER_OES: {
202         state = getState(GLClientState::WEIGHT_LOCATION);
203         break;
204         }
205     }
206     if (state && params)
207         *params = state->data;
208 }
209
210 int GLClientState::setPixelStore(GLenum param, GLint value)
211 {
212     int retval = 0;
213     switch(param) {
214     case GL_UNPACK_ALIGNMENT:
215         if (value == 1 || value == 2 || value == 4 || value == 8) {
216             m_pixelStore.unpack_alignment = value;
217         } else {
218             retval =  GL_INVALID_VALUE;
219         }
220         break;
221     case GL_PACK_ALIGNMENT:
222         if (value == 1 || value == 2 || value == 4 || value == 8) {
223             m_pixelStore.pack_alignment = value;
224         } else {
225             retval =  GL_INVALID_VALUE;
226         }
227         break;
228         default:
229             retval = GL_INVALID_ENUM;
230     }
231     return retval;
232 }
233
234
235
236
237 size_t GLClientState::pixelDataSize(GLsizei width, GLsizei height, GLenum format, GLenum type, int pack) const
238 {
239     if (width <= 0 || height <= 0) return 0;
240
241     int pixelsize = glUtilsPixelBitSize(format, type) >> 3;
242
243     int alignment = pack ? m_pixelStore.pack_alignment : m_pixelStore.unpack_alignment;
244
245     if (pixelsize == 0 ) {
246         ERR("unknown pixel size: width: %d height: %d format: %d type: %d pack: %d align: %d\n",
247              width, height, format, type, pack, alignment);
248     }
249     size_t linesize = pixelsize * width;
250     size_t aligned_linesize = int(linesize / alignment) * alignment;
251     if (aligned_linesize < linesize) {
252         aligned_linesize += alignment;
253     }
254     return aligned_linesize * height;
255 }
256
257 GLenum GLClientState::setActiveTextureUnit(GLenum texture)
258 {
259     GLuint unit = texture - GL_TEXTURE0;
260     if (unit >= MAX_TEXTURE_UNITS) {
261         return GL_INVALID_OPERATION;
262     }
263     m_tex.activeUnit = &m_tex.unit[unit];
264     return GL_NO_ERROR;
265 }
266
267 GLenum GLClientState::getActiveTextureUnit() const
268 {
269     return GL_TEXTURE0 + (m_tex.activeUnit - &m_tex.unit[0]);
270 }
271
272 void GLClientState::enableTextureTarget(GLenum target)
273 {
274     switch (target) {
275     case GL_TEXTURE_2D:
276         m_tex.activeUnit->enables |= (1u << TEXTURE_2D);
277         break;
278     case GL_TEXTURE_EXTERNAL_OES:
279         m_tex.activeUnit->enables |= (1u << TEXTURE_EXTERNAL);
280         break;
281     }
282 }
283
284 void GLClientState::disableTextureTarget(GLenum target)
285 {
286     switch (target) {
287     case GL_TEXTURE_2D:
288         m_tex.activeUnit->enables &= ~(1u << TEXTURE_2D);
289         break;
290     case GL_TEXTURE_EXTERNAL_OES:
291         m_tex.activeUnit->enables &= ~(1u << TEXTURE_EXTERNAL);
292         break;
293     }
294 }
295
296 GLenum GLClientState::getPriorityEnabledTarget(GLenum allDisabled) const
297 {
298     unsigned int enables = m_tex.activeUnit->enables;
299     if (enables & (1u << TEXTURE_EXTERNAL)) {
300         return GL_TEXTURE_EXTERNAL_OES;
301     } else if (enables & (1u << TEXTURE_2D)) {
302         return GL_TEXTURE_2D;
303     } else {
304         return allDisabled;
305     }
306 }
307
308 int GLClientState::compareTexId(const void* pid, const void* prec)
309 {
310     const GLuint* id = (const GLuint*)pid;
311     const TextureRec* rec = (const TextureRec*)prec;
312     return (GLint)(*id) - (GLint)rec->id;
313 }
314
315 GLenum GLClientState::bindTexture(GLenum target, GLuint texture,
316         GLboolean* firstUse)
317 {
318     GLboolean first = GL_FALSE;
319     TextureRec* texrec = NULL;
320     if (texture != 0) {
321         if (m_tex.textures) {
322             texrec = (TextureRec*)bsearch(&texture, m_tex.textures,
323                     m_tex.numTextures, sizeof(TextureRec), compareTexId);
324         }
325         if (!texrec) {
326             if (!(texrec = addTextureRec(texture, target))) {
327                 return GL_OUT_OF_MEMORY;
328             }
329             first = GL_TRUE;
330         }
331         if (target != texrec->target) {
332             return GL_INVALID_OPERATION;
333         }
334     }
335
336     switch (target) {
337     case GL_TEXTURE_2D:
338         m_tex.activeUnit->texture[TEXTURE_2D] = texture;
339         break;
340     case GL_TEXTURE_EXTERNAL_OES:
341         m_tex.activeUnit->texture[TEXTURE_EXTERNAL] = texture;
342         break;
343     }
344
345     if (firstUse) {
346         *firstUse = first;
347     }
348
349     return GL_NO_ERROR;
350 }
351
352 GLClientState::TextureRec* GLClientState::addTextureRec(GLuint id,
353         GLenum target)
354 {
355     if (m_tex.numTextures == m_tex.allocTextures) {
356         const GLuint MAX_TEXTURES = 0xFFFFFFFFu;
357
358         GLuint newAlloc;
359         if (MAX_TEXTURES - m_tex.allocTextures >= m_tex.allocTextures) {
360             newAlloc = MAX(4, 2 * m_tex.allocTextures);
361         } else {
362             if (m_tex.allocTextures == MAX_TEXTURES) {
363                 return NULL;
364             }
365             newAlloc = MAX_TEXTURES;
366         }
367
368         TextureRec* newTextures = (TextureRec*)realloc(m_tex.textures,
369                 newAlloc * sizeof(TextureRec));
370         if (!newTextures) {
371             return NULL;
372         }
373
374         m_tex.textures = newTextures;
375         m_tex.allocTextures = newAlloc;
376     }
377
378     TextureRec* tex = m_tex.textures + m_tex.numTextures;
379     TextureRec* prev = tex - 1;
380     while (tex != m_tex.textures && id < prev->id) {
381         *tex-- = *prev--;
382     }
383     tex->id = id;
384     tex->target = target;
385     tex->format = -1;
386     m_tex.numTextures++;
387
388     return tex;
389 }
390
391 void GLClientState::setBoundTextureInternalFormat(GLenum target, GLint internalformat) {
392     GLuint texture = getBoundTexture(target);
393     TextureRec* texrec = NULL;
394     texrec = (TextureRec*)bsearch(&texture, m_tex.textures,
395                                   m_tex.numTextures,
396                                   sizeof(TextureRec),
397                                   compareTexId);
398     if (!texrec) return;
399     texrec->internalformat = internalformat;
400 }
401
402 void GLClientState::setBoundTextureFormat(GLenum target, GLenum format) {
403     GLuint texture = getBoundTexture(target);
404     TextureRec* texrec = NULL;
405     texrec = (TextureRec*)bsearch(&texture, m_tex.textures,
406                                   m_tex.numTextures,
407                                   sizeof(TextureRec),
408                                   compareTexId);
409     if (!texrec) return;
410     texrec->format = format;
411 }
412
413 void GLClientState::setBoundTextureType(GLenum target, GLenum type) {
414     GLuint texture = getBoundTexture(target);
415     TextureRec* texrec = NULL;
416     texrec = (TextureRec*)bsearch(&texture, m_tex.textures,
417                                   m_tex.numTextures,
418                                   sizeof(TextureRec),
419                                   compareTexId);
420     if (!texrec) return;
421     texrec->type = type;
422 }
423
424 GLuint GLClientState::getBoundTexture(GLenum target) const
425 {
426     switch (target) {
427     case GL_TEXTURE_2D:
428         return m_tex.activeUnit->texture[TEXTURE_2D];
429     case GL_TEXTURE_EXTERNAL_OES:
430         return m_tex.activeUnit->texture[TEXTURE_EXTERNAL];
431     default:
432         return 0;
433     }
434 }
435
436 void GLClientState::deleteTextures(GLsizei n, const GLuint* textures)
437 {
438     // Updating the textures array could be made more efficient when deleting
439     // several textures:
440     // - compacting the array could be done in a single pass once the deleted
441     //   textures are marked, or
442     // - could swap deleted textures to the end and re-sort.
443     TextureRec* texrec;
444     for (const GLuint* texture = textures; texture != textures + n; texture++) {
445         texrec = (TextureRec*)bsearch(texture, m_tex.textures,
446                 m_tex.numTextures, sizeof(TextureRec), compareTexId);
447         if (texrec) {
448             const TextureRec* end = m_tex.textures + m_tex.numTextures;
449             memmove(texrec, texrec + 1,
450                     (end - texrec - 1) * sizeof(TextureRec));
451             m_tex.numTextures--;
452
453             for (TextureUnit* unit = m_tex.unit;
454                  unit != m_tex.unit + MAX_TEXTURE_UNITS;
455                  unit++)
456             {
457                 if (unit->texture[TEXTURE_2D] == *texture) {
458                     unit->texture[TEXTURE_2D] = 0;
459                 } else if (unit->texture[TEXTURE_EXTERNAL] == *texture) {
460                     unit->texture[TEXTURE_EXTERNAL] = 0;
461                 }
462             }
463         }
464     }
465 }
466
467 // RBO//////////////////////////////////////////////////////////////////////////
468
469 void GLClientState::addFreshRenderbuffer(GLuint name) {
470     mRboState.rboData.push_back(RboProps());
471     RboProps& props = mRboState.rboData.back();
472     props.target = GL_RENDERBUFFER;
473     props.name = name;
474     props.format = GL_NONE;
475     props.previouslyBound = false;
476 }
477
478 void GLClientState::addRenderbuffers(GLsizei n, GLuint* renderbuffers) {
479     for (size_t i = 0; i < n; i++) {
480         addFreshRenderbuffer(renderbuffers[i]);
481     }
482 }
483
484 size_t GLClientState::getRboIndex(GLuint name) const {
485     for (size_t i = 0; i < mRboState.rboData.size(); i++) {
486         if (mRboState.rboData[i].name == name) {
487             return i;
488         }
489     }
490     return -1;
491 }
492
493 void GLClientState::removeRenderbuffers(GLsizei n, const GLuint* renderbuffers) {
494     size_t bound_rbo_idx = getRboIndex(boundRboProps_const().name);
495
496     std::vector<GLuint> to_remove;
497     for (size_t i = 0; i < n; i++) {
498         if (renderbuffers[i] != 0) { // Never remove the zero rb.
499             to_remove.push_back(getRboIndex(renderbuffers[i]));
500         }
501     }
502
503     for (size_t i = 0; i < to_remove.size(); i++) {
504         mRboState.rboData[to_remove[i]] = mRboState.rboData.back();
505         mRboState.rboData.pop_back();
506     }
507
508     // If we just deleted the currently bound rb,
509     // bind the zero rb
510     if (getRboIndex(boundRboProps_const().name) != bound_rbo_idx) {
511         bindRenderbuffer(GL_RENDERBUFFER, 0);
512     }
513 }
514
515 bool GLClientState::usedRenderbufferName(GLuint name) const {
516     for (size_t i = 0; i < mRboState.rboData.size(); i++) {
517         if (mRboState.rboData[i].name == name) {
518             return true;
519         }
520     }
521     return false;
522 }
523
524 void GLClientState::setBoundRenderbufferIndex() {
525     for (size_t i = 0; i < mRboState.rboData.size(); i++) {
526         if (mRboState.rboData[i].name == mRboState.boundRenderbuffer) {
527             mRboState.boundRenderbufferIndex = i;
528             break;
529         }
530     }
531 }
532
533 RboProps& GLClientState::boundRboProps() {
534     return mRboState.rboData[mRboState.boundRenderbufferIndex];
535 }
536
537 const RboProps& GLClientState::boundRboProps_const() const {
538     return mRboState.rboData[mRboState.boundRenderbufferIndex];
539 }
540
541 void GLClientState::bindRenderbuffer(GLenum target, GLuint name) {
542     // If unused, add it.
543     if (!usedRenderbufferName(name)) {
544         addFreshRenderbuffer(name);
545     }
546     mRboState.boundRenderbuffer = name;
547     setBoundRenderbufferIndex();
548     boundRboProps().target = target;
549     boundRboProps().previouslyBound = true;
550 }
551
552 GLuint GLClientState::boundRenderbuffer() const {
553     return boundRboProps_const().name;
554 }
555
556 void GLClientState::setBoundRenderbufferFormat(GLenum format) {
557     boundRboProps().format = format;
558 }
559
560 // FBO//////////////////////////////////////////////////////////////////////////
561
562 // Format querying
563
564 GLenum GLClientState::queryRboFormat(GLuint rbo_name) const {
565     return mRboState.rboData[getRboIndex(rbo_name)].format;
566 }
567
568 GLint GLClientState::queryTexInternalFormat(GLuint tex_name) const {
569     TextureRec* texrec = NULL;
570     texrec = (TextureRec*)bsearch(&tex_name, m_tex.textures,
571                                   m_tex.numTextures, sizeof(TextureRec), compareTexId);
572     if (!texrec) return -1;
573     return texrec->internalformat;
574 }
575
576 GLenum GLClientState::queryTexFormat(GLuint tex_name) const {
577     TextureRec* texrec = NULL;
578     texrec = (TextureRec*)bsearch(&tex_name, m_tex.textures,
579                                   m_tex.numTextures, sizeof(TextureRec), compareTexId);
580     if (!texrec) return -1;
581     return texrec->format;
582 }
583
584 GLenum GLClientState::queryTexType(GLuint tex_name) const {
585     TextureRec* texrec = NULL;
586     texrec = (TextureRec*)bsearch(&tex_name, m_tex.textures,
587                                   m_tex.numTextures, sizeof(TextureRec), compareTexId);
588     if (!texrec) return -1;
589     return texrec->type;
590 }
591
592 void GLClientState::getBoundFramebufferFormat(
593         GLenum attachment, FboFormatInfo* res_info) const {
594     const FboProps& props = boundFboProps_const();
595
596     res_info->type = FBO_ATTACHMENT_NONE;
597     res_info->rb_format = GL_NONE;
598     res_info->tex_internalformat = -1;
599     res_info->tex_format = GL_NONE;
600     res_info->tex_type = GL_NONE;
601
602     switch (attachment) {
603     case GL_COLOR_ATTACHMENT0:
604         if (props.colorAttachment0_hasRbo) {
605             res_info->type = FBO_ATTACHMENT_RENDERBUFFER;
606             res_info->rb_format = queryRboFormat(props.colorAttachment0_rbo);
607         } else if (props.colorAttachment0_hasTexObj) {
608             res_info->type = FBO_ATTACHMENT_TEXTURE;
609             res_info->tex_internalformat = queryTexInternalFormat(props.colorAttachment0_texture);
610             res_info->tex_format = queryTexFormat(props.colorAttachment0_texture);
611             res_info->tex_type = queryTexType(props.colorAttachment0_texture);
612         } else {
613             res_info->type = FBO_ATTACHMENT_NONE;
614         }
615         break;
616     case GL_DEPTH_ATTACHMENT:
617         if (props.depthAttachment_hasRbo) {
618             res_info->type = FBO_ATTACHMENT_RENDERBUFFER;
619             res_info->rb_format = queryRboFormat(props.depthAttachment_rbo);
620         } else if (props.depthAttachment_hasTexObj) {
621             res_info->type = FBO_ATTACHMENT_TEXTURE;
622             res_info->tex_internalformat = queryTexInternalFormat(props.depthAttachment_texture);
623             res_info->tex_format = queryTexFormat(props.depthAttachment_texture);
624             res_info->tex_type = queryTexType(props.depthAttachment_texture);
625         } else {
626             res_info->type = FBO_ATTACHMENT_NONE;
627         }
628         break;
629     case GL_STENCIL_ATTACHMENT:
630         if (props.stencilAttachment_hasRbo) {
631             res_info->type = FBO_ATTACHMENT_RENDERBUFFER;
632             res_info->rb_format = queryRboFormat(props.stencilAttachment_rbo);
633         } else if (props.stencilAttachment_hasTexObj) {
634             res_info->type = FBO_ATTACHMENT_TEXTURE;
635             res_info->tex_internalformat = queryTexInternalFormat(props.stencilAttachment_texture);
636             res_info->tex_format = queryTexFormat(props.stencilAttachment_texture);
637             res_info->tex_type = queryTexType(props.stencilAttachment_texture);
638         } else {
639             res_info->type = FBO_ATTACHMENT_NONE;
640         }
641         break;
642     default:
643         res_info->type = FBO_ATTACHMENT_NONE;
644         break;
645     }
646 }
647
648 void GLClientState::addFreshFramebuffer(GLuint name) {
649     mFboState.fboData.push_back(FboProps());
650     FboProps& props = mFboState.fboData.back();
651     props.target = GL_FRAMEBUFFER;
652     props.name = name;
653     props.previouslyBound = false;
654
655     props.colorAttachment0_texture = 0;
656     props.depthAttachment_texture = 0;
657     props.stencilAttachment_texture = 0;
658
659     props.colorAttachment0_hasTexObj = false;
660     props.depthAttachment_hasTexObj = false;
661     props.stencilAttachment_hasTexObj = false;
662
663     props.colorAttachment0_rbo = 0;
664     props.depthAttachment_rbo = 0;
665     props.stencilAttachment_rbo = 0;
666
667     props.colorAttachment0_hasRbo = false;
668     props.depthAttachment_hasRbo = false;
669     props.stencilAttachment_hasRbo = false;
670 }
671
672 void GLClientState::addFramebuffers(GLsizei n, GLuint* framebuffers) {
673     for (size_t i = 0; i < n; i++) {
674         addFreshFramebuffer(framebuffers[i]);
675     }
676 }
677
678 size_t GLClientState::getFboIndex(GLuint name) const {
679     for (size_t i = 0; i < mFboState.fboData.size(); i++) {
680         if (mFboState.fboData[i].name == name) {
681             return i;
682         }
683     }
684     return -1;
685 }
686
687
688 void GLClientState::removeFramebuffers(GLsizei n, const GLuint* framebuffers) {
689     size_t bound_fbo_idx = getFboIndex(boundFboProps_const().name);
690
691     std::vector<GLuint> to_remove;
692     for (size_t i = 0; i < n; i++) {
693         if (framebuffers[i] != 0) { // Never remove the zero fb.
694             to_remove.push_back(getFboIndex(framebuffers[i]));
695         }
696     }
697
698     for (size_t i = 0; i < to_remove.size(); i++) {
699         mFboState.fboData[to_remove[i]] = mFboState.fboData.back();
700         mFboState.fboData.pop_back();
701     }
702
703     // If we just deleted the currently bound fb<
704     // bind the zero fb
705     if (getFboIndex(boundFboProps_const().name) != bound_fbo_idx) {
706         bindFramebuffer(GL_FRAMEBUFFER, 0);
707     }
708 }
709
710 bool GLClientState::usedFramebufferName(GLuint name) const {
711     for (size_t i = 0; i < mFboState.fboData.size(); i++) {
712         if (mFboState.fboData[i].name == name) {
713             return true;
714         }
715     }
716     return false;
717 }
718
719 void GLClientState::setBoundFramebufferIndex() {
720     for (size_t i = 0; i < mFboState.fboData.size(); i++) {
721         if (mFboState.fboData[i].name == mFboState.boundFramebuffer) {
722             mFboState.boundFramebufferIndex = i;
723             break;
724         }
725     }
726 }
727
728 FboProps& GLClientState::boundFboProps() {
729     return mFboState.fboData[mFboState.boundFramebufferIndex];
730 }
731
732 const FboProps& GLClientState::boundFboProps_const() const {
733     return mFboState.fboData[mFboState.boundFramebufferIndex];
734 }
735
736 void GLClientState::bindFramebuffer(GLenum target, GLuint name) {
737     // If unused, add it.
738     if (!usedFramebufferName(name)) {
739         addFreshFramebuffer(name);
740     }
741     mFboState.boundFramebuffer = name;
742     setBoundFramebufferIndex();
743     boundFboProps().target = target;
744     boundFboProps().previouslyBound = true;
745 }
746
747 void GLClientState::setCheckFramebufferStatus(GLenum status) {
748     mFboState.fboCheckStatus = status;
749 }
750
751 GLenum GLClientState::getCheckFramebufferStatus() const {
752     return mFboState.fboCheckStatus;
753 }
754
755 GLuint GLClientState::boundFramebuffer() const {
756     return boundFboProps_const().name;
757 }
758
759 // Texture objects for FBOs/////////////////////////////////////////////////////
760
761 void GLClientState::attachTextureObject(GLenum attachment, GLuint texture) {
762     switch (attachment) {
763     case GL_COLOR_ATTACHMENT0:
764         boundFboProps().colorAttachment0_texture = texture;
765         boundFboProps().colorAttachment0_hasTexObj = true;
766         break;
767     case GL_DEPTH_ATTACHMENT:
768         boundFboProps().depthAttachment_texture = texture;
769         boundFboProps().depthAttachment_hasTexObj = true;
770         break;
771     case GL_STENCIL_ATTACHMENT:
772         boundFboProps().stencilAttachment_texture = texture;
773         boundFboProps().stencilAttachment_hasTexObj = true;
774         break;
775     default:
776         break;
777     }
778 }
779
780 GLuint GLClientState::getFboAttachmentTextureId(GLenum attachment) const {
781     GLuint res;
782     switch (attachment) {
783     case GL_COLOR_ATTACHMENT0:
784         res = boundFboProps_const().colorAttachment0_texture;
785         break;
786     case GL_DEPTH_ATTACHMENT:
787         res = boundFboProps_const().depthAttachment_texture;
788         break;
789     case GL_STENCIL_ATTACHMENT:
790         res = boundFboProps_const().stencilAttachment_texture;
791         break;
792     default:
793         res = 0; // conservative validation for now
794     }
795     return res;
796 }
797
798 // RBOs for FBOs////////////////////////////////////////////////////////////////
799
800 void GLClientState::attachRbo(GLenum attachment, GLuint renderbuffer) {
801     switch (attachment) {
802     case GL_COLOR_ATTACHMENT0:
803         boundFboProps().colorAttachment0_rbo = renderbuffer;
804         boundFboProps().colorAttachment0_hasRbo = true;
805         break;
806     case GL_DEPTH_ATTACHMENT:
807         boundFboProps().depthAttachment_rbo = renderbuffer;
808         boundFboProps().depthAttachment_hasRbo = true;
809         break;
810     case GL_STENCIL_ATTACHMENT:
811         boundFboProps().stencilAttachment_rbo = renderbuffer;
812         boundFboProps().stencilAttachment_hasRbo = true;
813         break;
814     default:
815         break;
816     }
817 }
818
819 GLuint GLClientState::getFboAttachmentRboId(GLenum attachment) const {
820     GLuint res;
821     switch (attachment) {
822     case GL_COLOR_ATTACHMENT0:
823         res = boundFboProps_const().colorAttachment0_rbo;
824         break;
825     case GL_DEPTH_ATTACHMENT:
826         res = boundFboProps_const().depthAttachment_rbo;
827         break;
828     case GL_STENCIL_ATTACHMENT:
829         res = boundFboProps_const().stencilAttachment_rbo;
830         break;
831     default:
832         res = 0; // conservative validation for now
833     }
834     return res;
835 }
836
837 bool GLClientState::attachmentHasObject(GLenum attachment) const {
838     bool res;
839     switch (attachment) {
840     case GL_COLOR_ATTACHMENT0:
841         res = (boundFboProps_const().colorAttachment0_hasTexObj) ||
842               (boundFboProps_const().colorAttachment0_hasRbo);
843         break;
844     case GL_DEPTH_ATTACHMENT:
845         res = (boundFboProps_const().depthAttachment_hasTexObj) ||
846               (boundFboProps_const().depthAttachment_hasRbo);
847         break;
848     case GL_STENCIL_ATTACHMENT:
849         res = (boundFboProps_const().stencilAttachment_hasTexObj) ||
850               (boundFboProps_const().stencilAttachment_hasRbo);
851         break;
852     default:
853         res = true; // liberal validation for now
854     }
855     return res;
856 }
857
858 void GLClientState::fromMakeCurrent() {
859     FboProps& default_fb_props = mFboState.fboData[getFboIndex(0)];
860     default_fb_props.colorAttachment0_hasRbo = true;
861     default_fb_props.depthAttachment_hasRbo = true;
862     default_fb_props.stencilAttachment_hasRbo = true;
863 }