Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 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 | #include "rsContext.h" |
| 18 | #include "rsProgram.h" |
| 19 | |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame^] | 20 | #include <GLES2/gl2.h> |
| 21 | #include <GLES2/gl2ext.h> |
| 22 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 23 | using namespace android; |
| 24 | using namespace android::renderscript; |
| 25 | |
| 26 | |
Jason Sams | a9e7a05 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 27 | Program::Program(Context *rsc, Element *in, Element *out) : ObjectBase(rsc) |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 28 | { |
Jason Sams | 61f08d6 | 2009-09-25 16:37:33 -0700 | [diff] [blame] | 29 | mAllocFile = __FILE__; |
| 30 | mAllocLine = __LINE__; |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame^] | 31 | mDirty = true; |
| 32 | mShaderID = 0; |
| 33 | mAttribCount = 0; |
| 34 | mUniformCount = 0; |
Jason Sams | 61f08d6 | 2009-09-25 16:37:33 -0700 | [diff] [blame] | 35 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 36 | mElementIn.set(in); |
| 37 | mElementOut.set(out); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | Program::~Program() |
| 41 | { |
Jason Sams | 83f1c63 | 2009-10-26 15:19:28 -0700 | [diff] [blame] | 42 | bindAllocation(NULL); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | |
Jason Sams | 9bee51c | 2009-08-05 13:57:03 -0700 | [diff] [blame] | 46 | void Program::bindAllocation(Allocation *alloc) |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 47 | { |
Jason Sams | 83f1c63 | 2009-10-26 15:19:28 -0700 | [diff] [blame] | 48 | if (mConstants.get() == alloc) { |
| 49 | return; |
| 50 | } |
| 51 | if (mConstants.get()) { |
| 52 | mConstants.get()->removeProgramToDirty(this); |
| 53 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 54 | mConstants.set(alloc); |
Jason Sams | 83f1c63 | 2009-10-26 15:19:28 -0700 | [diff] [blame] | 55 | if (alloc) { |
| 56 | alloc->addProgramToDirty(this); |
| 57 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 58 | mDirty = true; |
| 59 | } |
| 60 | |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame^] | 61 | void Program::createShader() |
| 62 | { |
| 63 | } |
Jason Sams | 9bee51c | 2009-08-05 13:57:03 -0700 | [diff] [blame] | 64 | |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame^] | 65 | bool Program::loadShader(uint32_t type) |
| 66 | { |
| 67 | mShaderID = glCreateShader(type); |
| 68 | rsAssert(mShaderID); |
| 69 | |
| 70 | LOGV("Loading shader type %x", type); |
| 71 | LOGE(mShader.string()); |
| 72 | |
| 73 | if (mShaderID) { |
| 74 | const char * ss = mShader.string(); |
| 75 | glShaderSource(mShaderID, 1, &ss, NULL); |
| 76 | glCompileShader(mShaderID); |
| 77 | |
| 78 | GLint compiled = 0; |
| 79 | glGetShaderiv(mShaderID, GL_COMPILE_STATUS, &compiled); |
| 80 | if (!compiled) { |
| 81 | GLint infoLen = 0; |
| 82 | glGetShaderiv(mShaderID, GL_INFO_LOG_LENGTH, &infoLen); |
| 83 | if (infoLen) { |
| 84 | char* buf = (char*) malloc(infoLen); |
| 85 | if (buf) { |
| 86 | glGetShaderInfoLog(mShaderID, infoLen, NULL, buf); |
| 87 | LOGE("Could not compile shader \n%s\n", buf); |
| 88 | free(buf); |
| 89 | } |
| 90 | glDeleteShader(mShaderID); |
| 91 | mShaderID = 0; |
| 92 | return false; |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | LOGV("--Shader load result %x ", glGetError()); |
| 97 | return true; |
| 98 | } |