2 * Copyright (C) 2011 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include "GLClientState.h"
22 #include <cutils/log.h>
25 #define MAX(a, b) ((a) < (b) ? (b) : (a))
28 GLClientState::GLClientState(int nLocations)
30 if (nLocations < LAST_LOCATION) {
31 nLocations = LAST_LOCATION;
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;
40 m_currentArrayVbo = 0;
41 m_currentIndexVbo = 0;
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;
60 m_pixelStore.unpack_alignment = 4;
61 m_pixelStore.pack_alignment = 4;
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;
69 mRboState.boundRenderbuffer = 0;
70 mRboState.boundRenderbufferIndex = 0;
71 addFreshRenderbuffer(0);
73 mFboState.boundFramebuffer = 0;
74 mFboState.boundFramebufferIndex = 0;
75 mFboState.fboCheckStatus = GL_NONE;
76 addFreshFramebuffer(0);
78 m_maxVertexAttribsDirty = true;
81 GLClientState::~GLClientState()
86 void GLClientState::enable(int location, int state)
88 if (!validLocation(location)) {
92 m_states[location].enableDirty |= (state != m_states[location].enabled);
93 m_states[location].enabled = state;
96 void GLClientState::setState(int location, int size, GLenum type, GLboolean normalized, GLsizei stride, const void *data)
98 if (!validLocation(location)) {
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;
110 void GLClientState::setBufferObject(int location, GLuint id)
112 if (!validLocation(location)) {
116 m_states[location].bufferObject = id;
119 const GLClientState::VertexAttribState * GLClientState::getState(int location)
121 if (!validLocation(location)) {
124 return & m_states[location];
127 const GLClientState::VertexAttribState * GLClientState::getStateAndEnableDirty(int location, bool *enableChanged)
129 if (!validLocation(location)) {
134 *enableChanged = m_states[location].enableDirty;
137 m_states[location].enableDirty = false;
138 return & m_states[location];
141 int GLClientState::getLocation(GLenum loc)
146 case GL_VERTEX_ARRAY:
147 retval = int(VERTEX_LOCATION);
149 case GL_NORMAL_ARRAY:
150 retval = int(NORMAL_LOCATION);
153 retval = int(COLOR_LOCATION);
155 case GL_POINT_SIZE_ARRAY_OES:
156 retval = int(POINTSIZE_LOCATION);
158 case GL_TEXTURE_COORD_ARRAY:
159 retval = int (TEXCOORD0_LOCATION + m_activeTexture);
161 case GL_MATRIX_INDEX_ARRAY_OES:
162 retval = int (MATRIXINDEX_LOCATION);
164 case GL_WEIGHT_ARRAY_OES:
165 retval = int (WEIGHT_LOCATION);
173 void GLClientState::getClientStatePointer(GLenum pname, GLvoid** params)
175 const GLClientState::VertexAttribState *state = NULL;
177 case GL_VERTEX_ARRAY_POINTER: {
178 state = getState(GLClientState::VERTEX_LOCATION);
181 case GL_NORMAL_ARRAY_POINTER: {
182 state = getState(GLClientState::NORMAL_LOCATION);
185 case GL_COLOR_ARRAY_POINTER: {
186 state = getState(GLClientState::COLOR_LOCATION);
189 case GL_TEXTURE_COORD_ARRAY_POINTER: {
190 state = getState(getActiveTexture() + GLClientState::TEXCOORD0_LOCATION);
193 case GL_POINT_SIZE_ARRAY_POINTER_OES: {
194 state = getState(GLClientState::POINTSIZE_LOCATION);
197 case GL_MATRIX_INDEX_ARRAY_POINTER_OES: {
198 state = getState(GLClientState::MATRIXINDEX_LOCATION);
201 case GL_WEIGHT_ARRAY_POINTER_OES: {
202 state = getState(GLClientState::WEIGHT_LOCATION);
207 *params = state->data;
210 int GLClientState::setPixelStore(GLenum param, GLint value)
214 case GL_UNPACK_ALIGNMENT:
215 if (value == 1 || value == 2 || value == 4 || value == 8) {
216 m_pixelStore.unpack_alignment = value;
218 retval = GL_INVALID_VALUE;
221 case GL_PACK_ALIGNMENT:
222 if (value == 1 || value == 2 || value == 4 || value == 8) {
223 m_pixelStore.pack_alignment = value;
225 retval = GL_INVALID_VALUE;
229 retval = GL_INVALID_ENUM;
237 size_t GLClientState::pixelDataSize(GLsizei width, GLsizei height, GLenum format, GLenum type, int pack) const
239 if (width <= 0 || height <= 0) return 0;
241 int pixelsize = glUtilsPixelBitSize(format, type) >> 3;
243 int alignment = pack ? m_pixelStore.pack_alignment : m_pixelStore.unpack_alignment;
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);
249 size_t linesize = pixelsize * width;
250 size_t aligned_linesize = int(linesize / alignment) * alignment;
251 if (aligned_linesize < linesize) {
252 aligned_linesize += alignment;
254 return aligned_linesize * height;
257 GLenum GLClientState::setActiveTextureUnit(GLenum texture)
259 GLuint unit = texture - GL_TEXTURE0;
260 if (unit >= MAX_TEXTURE_UNITS) {
261 return GL_INVALID_ENUM;
263 m_tex.activeUnit = &m_tex.unit[unit];
267 GLenum GLClientState::getActiveTextureUnit() const
269 return GL_TEXTURE0 + (m_tex.activeUnit - &m_tex.unit[0]);
272 void GLClientState::enableTextureTarget(GLenum target)
276 m_tex.activeUnit->enables |= (1u << TEXTURE_2D);
278 case GL_TEXTURE_EXTERNAL_OES:
279 m_tex.activeUnit->enables |= (1u << TEXTURE_EXTERNAL);
284 void GLClientState::disableTextureTarget(GLenum target)
288 m_tex.activeUnit->enables &= ~(1u << TEXTURE_2D);
290 case GL_TEXTURE_EXTERNAL_OES:
291 m_tex.activeUnit->enables &= ~(1u << TEXTURE_EXTERNAL);
296 GLenum GLClientState::getPriorityEnabledTarget(GLenum allDisabled) const
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;
308 int GLClientState::compareTexId(const void* pid, const void* prec)
310 const GLuint* id = (const GLuint*)pid;
311 const TextureRec* rec = (const TextureRec*)prec;
312 return (GLint)(*id) - (GLint)rec->id;
315 GLenum GLClientState::bindTexture(GLenum target, GLuint texture,
318 GLboolean first = GL_FALSE;
319 TextureRec* texrec = NULL;
321 if (m_tex.textures) {
322 texrec = (TextureRec*)bsearch(&texture, m_tex.textures,
323 m_tex.numTextures, sizeof(TextureRec), compareTexId);
326 if (!(texrec = addTextureRec(texture, target))) {
327 return GL_OUT_OF_MEMORY;
331 if (target != texrec->target) {
332 return GL_INVALID_OPERATION;
338 m_tex.activeUnit->texture[TEXTURE_2D] = texture;
340 case GL_TEXTURE_EXTERNAL_OES:
341 m_tex.activeUnit->texture[TEXTURE_EXTERNAL] = texture;
352 GLClientState::TextureRec* GLClientState::addTextureRec(GLuint id,
355 if (m_tex.numTextures == m_tex.allocTextures) {
356 const GLuint MAX_TEXTURES = 0xFFFFFFFFu;
359 if (MAX_TEXTURES - m_tex.allocTextures >= m_tex.allocTextures) {
360 newAlloc = MAX(4, 2 * m_tex.allocTextures);
362 if (m_tex.allocTextures == MAX_TEXTURES) {
365 newAlloc = MAX_TEXTURES;
368 TextureRec* newTextures = (TextureRec*)realloc(m_tex.textures,
369 newAlloc * sizeof(TextureRec));
374 m_tex.textures = newTextures;
375 m_tex.allocTextures = newAlloc;
378 TextureRec* tex = m_tex.textures + m_tex.numTextures;
379 TextureRec* prev = tex - 1;
380 while (tex != m_tex.textures && id < prev->id) {
384 tex->target = target;
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,
399 texrec->internalformat = internalformat;
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,
410 texrec->format = format;
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,
424 GLuint GLClientState::getBoundTexture(GLenum target) const
428 return m_tex.activeUnit->texture[TEXTURE_2D];
429 case GL_TEXTURE_EXTERNAL_OES:
430 return m_tex.activeUnit->texture[TEXTURE_EXTERNAL];
436 // BEGIN driver workarounds-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-
437 // (>' ')><(' '<)(>' ')><(' '<)(>' ')><(' '<)(>' ')><(' '<)(>' ')><(' '<)(>' ')>
439 static bool unreliableInternalFormat(GLenum internalformat) {
440 switch (internalformat) {
448 void GLClientState::writeCopyTexImageState
449 (GLenum target, GLint level, GLenum internalformat) {
450 if (unreliableInternalFormat(internalformat)) {
452 entry.id = getBoundTexture(GL_TEXTURE_2D);
453 entry.target = target;
455 entry.internalformat = internalformat;
456 m_cubeMapDefs.insert(entry);
460 static GLenum identifyPositiveCubeMapComponent(GLenum target) {
462 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
463 return GL_TEXTURE_CUBE_MAP_POSITIVE_X;
464 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
465 return GL_TEXTURE_CUBE_MAP_POSITIVE_Y;
466 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
467 return GL_TEXTURE_CUBE_MAP_POSITIVE_Z;
473 GLenum GLClientState::copyTexImageNeededTarget
474 (GLenum target, GLint level, GLenum internalformat) {
475 if (unreliableInternalFormat(internalformat)) {
476 GLenum positiveComponent =
477 identifyPositiveCubeMapComponent(target);
478 if (positiveComponent) {
480 query.id = getBoundTexture(GL_TEXTURE_2D);
481 query.target = positiveComponent;
483 query.internalformat = internalformat;
484 if (m_cubeMapDefs.find(query) ==
485 m_cubeMapDefs.end()) {
486 return positiveComponent;
493 GLenum GLClientState::copyTexImageLuminanceCubeMapAMDWorkaround
494 (GLenum target, GLint level, GLenum internalformat) {
495 writeCopyTexImageState(target, level, internalformat);
496 return copyTexImageNeededTarget(target, level, internalformat);
499 // (>' ')><(' '<)(>' ')><(' '<)(>' ')><(' '<)(>' ')><(' '<)(>' ')><(' '<)(>' ')>
500 // END driver workarounds-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-
502 void GLClientState::deleteTextures(GLsizei n, const GLuint* textures)
504 // Updating the textures array could be made more efficient when deleting
506 // - compacting the array could be done in a single pass once the deleted
507 // textures are marked, or
508 // - could swap deleted textures to the end and re-sort.
510 for (const GLuint* texture = textures; texture != textures + n; texture++) {
511 texrec = (TextureRec*)bsearch(texture, m_tex.textures,
512 m_tex.numTextures, sizeof(TextureRec), compareTexId);
514 const TextureRec* end = m_tex.textures + m_tex.numTextures;
515 memmove(texrec, texrec + 1,
516 (end - texrec - 1) * sizeof(TextureRec));
519 for (TextureUnit* unit = m_tex.unit;
520 unit != m_tex.unit + MAX_TEXTURE_UNITS;
523 if (unit->texture[TEXTURE_2D] == *texture) {
524 unit->texture[TEXTURE_2D] = 0;
525 } else if (unit->texture[TEXTURE_EXTERNAL] == *texture) {
526 unit->texture[TEXTURE_EXTERNAL] = 0;
533 // RBO//////////////////////////////////////////////////////////////////////////
535 void GLClientState::addFreshRenderbuffer(GLuint name) {
536 mRboState.rboData.push_back(RboProps());
537 RboProps& props = mRboState.rboData.back();
538 props.target = GL_RENDERBUFFER;
540 props.format = GL_NONE;
541 props.previouslyBound = false;
544 void GLClientState::addRenderbuffers(GLsizei n, GLuint* renderbuffers) {
545 for (size_t i = 0; i < n; i++) {
546 addFreshRenderbuffer(renderbuffers[i]);
550 size_t GLClientState::getRboIndex(GLuint name) const {
551 for (size_t i = 0; i < mRboState.rboData.size(); i++) {
552 if (mRboState.rboData[i].name == name) {
559 void GLClientState::removeRenderbuffers(GLsizei n, const GLuint* renderbuffers) {
560 size_t bound_rbo_idx = getRboIndex(boundRboProps_const().name);
562 std::vector<GLuint> to_remove;
563 for (size_t i = 0; i < n; i++) {
564 if (renderbuffers[i] != 0) { // Never remove the zero rb.
565 to_remove.push_back(getRboIndex(renderbuffers[i]));
569 for (size_t i = 0; i < to_remove.size(); i++) {
570 mRboState.rboData[to_remove[i]] = mRboState.rboData.back();
571 mRboState.rboData.pop_back();
574 // If we just deleted the currently bound rb,
576 if (getRboIndex(boundRboProps_const().name) != bound_rbo_idx) {
577 bindRenderbuffer(GL_RENDERBUFFER, 0);
581 bool GLClientState::usedRenderbufferName(GLuint name) const {
582 for (size_t i = 0; i < mRboState.rboData.size(); i++) {
583 if (mRboState.rboData[i].name == name) {
590 void GLClientState::setBoundRenderbufferIndex() {
591 for (size_t i = 0; i < mRboState.rboData.size(); i++) {
592 if (mRboState.rboData[i].name == mRboState.boundRenderbuffer) {
593 mRboState.boundRenderbufferIndex = i;
599 RboProps& GLClientState::boundRboProps() {
600 return mRboState.rboData[mRboState.boundRenderbufferIndex];
603 const RboProps& GLClientState::boundRboProps_const() const {
604 return mRboState.rboData[mRboState.boundRenderbufferIndex];
607 void GLClientState::bindRenderbuffer(GLenum target, GLuint name) {
608 // If unused, add it.
609 if (!usedRenderbufferName(name)) {
610 addFreshRenderbuffer(name);
612 mRboState.boundRenderbuffer = name;
613 setBoundRenderbufferIndex();
614 boundRboProps().target = target;
615 boundRboProps().previouslyBound = true;
618 GLuint GLClientState::boundRenderbuffer() const {
619 return boundRboProps_const().name;
622 void GLClientState::setBoundRenderbufferFormat(GLenum format) {
623 boundRboProps().format = format;
626 // FBO//////////////////////////////////////////////////////////////////////////
630 GLenum GLClientState::queryRboFormat(GLuint rbo_name) const {
631 return mRboState.rboData[getRboIndex(rbo_name)].format;
634 GLint GLClientState::queryTexInternalFormat(GLuint tex_name) const {
635 TextureRec* texrec = NULL;
636 texrec = (TextureRec*)bsearch(&tex_name, m_tex.textures,
637 m_tex.numTextures, sizeof(TextureRec), compareTexId);
638 if (!texrec) return -1;
639 return texrec->internalformat;
642 GLenum GLClientState::queryTexFormat(GLuint tex_name) const {
643 TextureRec* texrec = NULL;
644 texrec = (TextureRec*)bsearch(&tex_name, m_tex.textures,
645 m_tex.numTextures, sizeof(TextureRec), compareTexId);
646 if (!texrec) return -1;
647 return texrec->format;
650 GLenum GLClientState::queryTexType(GLuint tex_name) const {
651 TextureRec* texrec = NULL;
652 texrec = (TextureRec*)bsearch(&tex_name, m_tex.textures,
653 m_tex.numTextures, sizeof(TextureRec), compareTexId);
654 if (!texrec) return -1;
658 void GLClientState::getBoundFramebufferFormat(
659 GLenum attachment, FboFormatInfo* res_info) const {
660 const FboProps& props = boundFboProps_const();
662 res_info->type = FBO_ATTACHMENT_NONE;
663 res_info->rb_format = GL_NONE;
664 res_info->tex_internalformat = -1;
665 res_info->tex_format = GL_NONE;
666 res_info->tex_type = GL_NONE;
668 switch (attachment) {
669 case GL_COLOR_ATTACHMENT0:
670 if (props.colorAttachment0_hasRbo) {
671 res_info->type = FBO_ATTACHMENT_RENDERBUFFER;
672 res_info->rb_format = queryRboFormat(props.colorAttachment0_rbo);
673 } else if (props.colorAttachment0_hasTexObj) {
674 res_info->type = FBO_ATTACHMENT_TEXTURE;
675 res_info->tex_internalformat = queryTexInternalFormat(props.colorAttachment0_texture);
676 res_info->tex_format = queryTexFormat(props.colorAttachment0_texture);
677 res_info->tex_type = queryTexType(props.colorAttachment0_texture);
679 res_info->type = FBO_ATTACHMENT_NONE;
682 case GL_DEPTH_ATTACHMENT:
683 if (props.depthAttachment_hasRbo) {
684 res_info->type = FBO_ATTACHMENT_RENDERBUFFER;
685 res_info->rb_format = queryRboFormat(props.depthAttachment_rbo);
686 } else if (props.depthAttachment_hasTexObj) {
687 res_info->type = FBO_ATTACHMENT_TEXTURE;
688 res_info->tex_internalformat = queryTexInternalFormat(props.depthAttachment_texture);
689 res_info->tex_format = queryTexFormat(props.depthAttachment_texture);
690 res_info->tex_type = queryTexType(props.depthAttachment_texture);
692 res_info->type = FBO_ATTACHMENT_NONE;
695 case GL_STENCIL_ATTACHMENT:
696 if (props.stencilAttachment_hasRbo) {
697 res_info->type = FBO_ATTACHMENT_RENDERBUFFER;
698 res_info->rb_format = queryRboFormat(props.stencilAttachment_rbo);
699 } else if (props.stencilAttachment_hasTexObj) {
700 res_info->type = FBO_ATTACHMENT_TEXTURE;
701 res_info->tex_internalformat = queryTexInternalFormat(props.stencilAttachment_texture);
702 res_info->tex_format = queryTexFormat(props.stencilAttachment_texture);
703 res_info->tex_type = queryTexType(props.stencilAttachment_texture);
705 res_info->type = FBO_ATTACHMENT_NONE;
709 res_info->type = FBO_ATTACHMENT_NONE;
714 void GLClientState::addFreshFramebuffer(GLuint name) {
715 mFboState.fboData.push_back(FboProps());
716 FboProps& props = mFboState.fboData.back();
717 props.target = GL_FRAMEBUFFER;
719 props.previouslyBound = false;
721 props.colorAttachment0_texture = 0;
722 props.depthAttachment_texture = 0;
723 props.stencilAttachment_texture = 0;
725 props.colorAttachment0_hasTexObj = false;
726 props.depthAttachment_hasTexObj = false;
727 props.stencilAttachment_hasTexObj = false;
729 props.colorAttachment0_rbo = 0;
730 props.depthAttachment_rbo = 0;
731 props.stencilAttachment_rbo = 0;
733 props.colorAttachment0_hasRbo = false;
734 props.depthAttachment_hasRbo = false;
735 props.stencilAttachment_hasRbo = false;
738 void GLClientState::addFramebuffers(GLsizei n, GLuint* framebuffers) {
739 for (size_t i = 0; i < n; i++) {
740 addFreshFramebuffer(framebuffers[i]);
744 size_t GLClientState::getFboIndex(GLuint name) const {
745 for (size_t i = 0; i < mFboState.fboData.size(); i++) {
746 if (mFboState.fboData[i].name == name) {
754 void GLClientState::removeFramebuffers(GLsizei n, const GLuint* framebuffers) {
755 size_t bound_fbo_idx = getFboIndex(boundFboProps_const().name);
757 std::vector<GLuint> to_remove;
758 for (size_t i = 0; i < n; i++) {
759 if (framebuffers[i] != 0) { // Never remove the zero fb.
760 to_remove.push_back(getFboIndex(framebuffers[i]));
764 for (size_t i = 0; i < to_remove.size(); i++) {
765 mFboState.fboData[to_remove[i]] = mFboState.fboData.back();
766 mFboState.fboData.pop_back();
769 // If we just deleted the currently bound fb<
771 if (getFboIndex(boundFboProps_const().name) != bound_fbo_idx) {
772 bindFramebuffer(GL_FRAMEBUFFER, 0);
776 bool GLClientState::usedFramebufferName(GLuint name) const {
777 for (size_t i = 0; i < mFboState.fboData.size(); i++) {
778 if (mFboState.fboData[i].name == name) {
785 void GLClientState::setBoundFramebufferIndex() {
786 for (size_t i = 0; i < mFboState.fboData.size(); i++) {
787 if (mFboState.fboData[i].name == mFboState.boundFramebuffer) {
788 mFboState.boundFramebufferIndex = i;
794 FboProps& GLClientState::boundFboProps() {
795 return mFboState.fboData[mFboState.boundFramebufferIndex];
798 const FboProps& GLClientState::boundFboProps_const() const {
799 return mFboState.fboData[mFboState.boundFramebufferIndex];
802 void GLClientState::bindFramebuffer(GLenum target, GLuint name) {
803 // If unused, add it.
804 if (!usedFramebufferName(name)) {
805 addFreshFramebuffer(name);
807 mFboState.boundFramebuffer = name;
808 setBoundFramebufferIndex();
809 boundFboProps().target = target;
810 boundFboProps().previouslyBound = true;
813 void GLClientState::setCheckFramebufferStatus(GLenum status) {
814 mFboState.fboCheckStatus = status;
817 GLenum GLClientState::getCheckFramebufferStatus() const {
818 return mFboState.fboCheckStatus;
821 GLuint GLClientState::boundFramebuffer() const {
822 return boundFboProps_const().name;
825 // Texture objects for FBOs/////////////////////////////////////////////////////
827 void GLClientState::attachTextureObject(GLenum attachment, GLuint texture) {
828 switch (attachment) {
829 case GL_COLOR_ATTACHMENT0:
830 boundFboProps().colorAttachment0_texture = texture;
831 boundFboProps().colorAttachment0_hasTexObj = true;
833 case GL_DEPTH_ATTACHMENT:
834 boundFboProps().depthAttachment_texture = texture;
835 boundFboProps().depthAttachment_hasTexObj = true;
837 case GL_STENCIL_ATTACHMENT:
838 boundFboProps().stencilAttachment_texture = texture;
839 boundFboProps().stencilAttachment_hasTexObj = true;
846 GLuint GLClientState::getFboAttachmentTextureId(GLenum attachment) const {
848 switch (attachment) {
849 case GL_COLOR_ATTACHMENT0:
850 res = boundFboProps_const().colorAttachment0_texture;
852 case GL_DEPTH_ATTACHMENT:
853 res = boundFboProps_const().depthAttachment_texture;
855 case GL_STENCIL_ATTACHMENT:
856 res = boundFboProps_const().stencilAttachment_texture;
859 res = 0; // conservative validation for now
864 // RBOs for FBOs////////////////////////////////////////////////////////////////
866 void GLClientState::attachRbo(GLenum attachment, GLuint renderbuffer) {
867 switch (attachment) {
868 case GL_COLOR_ATTACHMENT0:
869 boundFboProps().colorAttachment0_rbo = renderbuffer;
870 boundFboProps().colorAttachment0_hasRbo = true;
872 case GL_DEPTH_ATTACHMENT:
873 boundFboProps().depthAttachment_rbo = renderbuffer;
874 boundFboProps().depthAttachment_hasRbo = true;
876 case GL_STENCIL_ATTACHMENT:
877 boundFboProps().stencilAttachment_rbo = renderbuffer;
878 boundFboProps().stencilAttachment_hasRbo = true;
885 GLuint GLClientState::getFboAttachmentRboId(GLenum attachment) const {
887 switch (attachment) {
888 case GL_COLOR_ATTACHMENT0:
889 res = boundFboProps_const().colorAttachment0_rbo;
891 case GL_DEPTH_ATTACHMENT:
892 res = boundFboProps_const().depthAttachment_rbo;
894 case GL_STENCIL_ATTACHMENT:
895 res = boundFboProps_const().stencilAttachment_rbo;
898 res = 0; // conservative validation for now
903 bool GLClientState::attachmentHasObject(GLenum attachment) const {
905 switch (attachment) {
906 case GL_COLOR_ATTACHMENT0:
907 res = (boundFboProps_const().colorAttachment0_hasTexObj) ||
908 (boundFboProps_const().colorAttachment0_hasRbo);
910 case GL_DEPTH_ATTACHMENT:
911 res = (boundFboProps_const().depthAttachment_hasTexObj) ||
912 (boundFboProps_const().depthAttachment_hasRbo);
914 case GL_STENCIL_ATTACHMENT:
915 res = (boundFboProps_const().stencilAttachment_hasTexObj) ||
916 (boundFboProps_const().stencilAttachment_hasRbo);
919 res = true; // liberal validation for now
924 void GLClientState::fromMakeCurrent() {
925 FboProps& default_fb_props = mFboState.fboData[getFboIndex(0)];
926 default_fb_props.colorAttachment0_hasRbo = true;
927 default_fb_props.depthAttachment_hasRbo = true;
928 default_fb_props.stencilAttachment_hasRbo = true;