Alex Sakhartchouk | 2f6964f | 2011-05-13 14:53:34 -0700 | [diff] [blame^] | 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 | |
| 17 | |
| 18 | #include "rsdCore.h" |
| 19 | #include "rsdFrameBuffer.h" |
| 20 | |
| 21 | #include "rsContext.h" |
| 22 | #include "rsFBOCache.h" |
| 23 | |
| 24 | #include <GLES2/gl2.h> |
| 25 | #include <GLES2/gl2ext.h> |
| 26 | |
| 27 | using namespace android; |
| 28 | using namespace android::renderscript; |
| 29 | |
| 30 | struct DrvFrameBuffer { |
| 31 | GLuint mFBOId; |
| 32 | }; |
| 33 | |
| 34 | void checkError(const Context *rsc) { |
| 35 | GLenum status; |
| 36 | status = glCheckFramebufferStatus(GL_FRAMEBUFFER); |
| 37 | switch (status) { |
| 38 | case GL_FRAMEBUFFER_COMPLETE: |
| 39 | break; |
| 40 | case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT: |
| 41 | rsc->setError(RS_ERROR_BAD_VALUE, |
| 42 | "Unable to set up render Target: RFRAMEBUFFER_INCOMPLETE_ATTACHMENT"); |
| 43 | break; |
| 44 | case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT: |
| 45 | rsc->setError(RS_ERROR_BAD_VALUE, |
| 46 | "Unable to set up render Target: GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT"); |
| 47 | break; |
| 48 | case GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS: |
| 49 | rsc->setError(RS_ERROR_BAD_VALUE, |
| 50 | "Unable to set up render Target: GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS"); |
| 51 | break; |
| 52 | case GL_FRAMEBUFFER_UNSUPPORTED: |
| 53 | rsc->setError(RS_ERROR_BAD_VALUE, |
| 54 | "Unable to set up render Target: GL_FRAMEBUFFER_UNSUPPORTED"); |
| 55 | break; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | |
| 60 | void setDepthAttachment(const Context *rsc, const FBOCache *fb) { |
| 61 | if (fb->mHal.state.depthTarget.get() != NULL) { |
| 62 | if (fb->mHal.state.depthTarget->getIsTexture()) { |
| 63 | uint32_t texID = fb->mHal.state.depthTarget->getTextureID(); |
| 64 | glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, |
| 65 | GL_TEXTURE_2D, texID, 0); |
| 66 | } else { |
| 67 | uint32_t texID = fb->mHal.state.depthTarget->getRenderTargetID(); |
| 68 | glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, |
| 69 | GL_RENDERBUFFER, texID); |
| 70 | } |
| 71 | } else { |
| 72 | // Reset last attachment |
| 73 | glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, |
| 74 | GL_RENDERBUFFER, 0); |
| 75 | glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, |
| 76 | GL_TEXTURE_2D, 0, 0); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | void setColorAttachment(const Context *rsc, const FBOCache *fb) { |
| 81 | // Now attach color targets |
| 82 | for (uint32_t i = 0; i < fb->mHal.state.colorTargetsCount; i ++) { |
| 83 | uint32_t texID = 0; |
| 84 | if (fb->mHal.state.colorTargets[i].get() != NULL) { |
| 85 | if (fb->mHal.state.colorTargets[i]->getIsTexture()) { |
| 86 | uint32_t texID = fb->mHal.state.colorTargets[i]->getTextureID(); |
| 87 | glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i, |
| 88 | GL_TEXTURE_2D, texID, 0); |
| 89 | } else { |
| 90 | uint32_t texID = fb->mHal.state.depthTarget->getRenderTargetID(); |
| 91 | glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i, |
| 92 | GL_RENDERBUFFER, texID); |
| 93 | } |
| 94 | } else { |
| 95 | // Reset last attachment |
| 96 | glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i, |
| 97 | GL_RENDERBUFFER, 0); |
| 98 | glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i, |
| 99 | GL_TEXTURE_2D, 0, 0); |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | bool renderToFramebuffer(const FBOCache *fb) { |
| 105 | if (fb->mHal.state.depthTarget.get() != NULL) { |
| 106 | return false; |
| 107 | } |
| 108 | |
| 109 | for (uint32_t i = 0; i < fb->mHal.state.colorTargetsCount; i ++) { |
| 110 | if (fb->mHal.state.colorTargets[i].get() != NULL) { |
| 111 | return false; |
| 112 | } |
| 113 | } |
| 114 | return true; |
| 115 | } |
| 116 | |
| 117 | |
| 118 | bool rsdFrameBufferInit(const Context *rsc, const FBOCache *fb) { |
| 119 | DrvFrameBuffer *drv = (DrvFrameBuffer *)calloc(1, sizeof(DrvFrameBuffer)); |
| 120 | if (drv == NULL) { |
| 121 | return false; |
| 122 | } |
| 123 | fb->mHal.drv = drv; |
| 124 | drv->mFBOId = 0; |
| 125 | |
| 126 | return true; |
| 127 | } |
| 128 | |
| 129 | void rsdFrameBufferSetActive(const Context *rsc, const FBOCache *fb) { |
| 130 | DrvFrameBuffer *drv = (DrvFrameBuffer *)fb->mHal.drv; |
| 131 | |
| 132 | bool framebuffer = renderToFramebuffer(fb); |
| 133 | if (!framebuffer) { |
| 134 | if(drv->mFBOId == 0) { |
| 135 | glGenFramebuffers(1, &drv->mFBOId); |
| 136 | } |
| 137 | glBindFramebuffer(GL_FRAMEBUFFER, drv->mFBOId); |
| 138 | |
| 139 | setDepthAttachment(rsc, fb); |
| 140 | setColorAttachment(rsc, fb); |
| 141 | |
| 142 | glViewport(0, 0, fb->mHal.state.colorTargets[0]->getType()->getDimX(), |
| 143 | fb->mHal.state.colorTargets[0]->getType()->getDimY()); |
| 144 | |
| 145 | checkError(rsc); |
| 146 | } else { |
| 147 | glBindFramebuffer(GL_FRAMEBUFFER, 0); |
| 148 | glViewport(0, 0, rsc->getWidth(), rsc->getHeight()); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | void rsdFrameBufferDestroy(const Context *rsc, const FBOCache *fb) { |
| 153 | DrvFrameBuffer *drv = (DrvFrameBuffer *)fb->mHal.drv; |
| 154 | if(drv->mFBOId != 0) { |
| 155 | glDeleteFramebuffers(1, &drv->mFBOId); |
| 156 | } |
| 157 | |
| 158 | free(fb->mHal.drv); |
| 159 | fb->mHal.drv = NULL; |
| 160 | } |
| 161 | |
| 162 | |