blob: db40f1619185d41caa5a87a1e33daa2db5a368a6 [file] [log] [blame]
Jason Sams326e0dd2009-05-22 14:03:28 -07001/*
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 Samsc460e552009-11-25 13:22:07 -080020#include <GLES2/gl2.h>
21#include <GLES2/gl2ext.h>
22
Jason Sams326e0dd2009-05-22 14:03:28 -070023using namespace android;
24using namespace android::renderscript;
25
26
Jason Samse514b452009-09-25 14:51:22 -070027Program::Program(Context *rsc, Element *in, Element *out) : ObjectBase(rsc)
Jason Sams326e0dd2009-05-22 14:03:28 -070028{
Jason Samsf2649a92009-09-25 16:37:33 -070029 mAllocFile = __FILE__;
30 mAllocLine = __LINE__;
Jason Samsc460e552009-11-25 13:22:07 -080031 mDirty = true;
32 mShaderID = 0;
33 mAttribCount = 0;
34 mUniformCount = 0;
Jason Samsf2649a92009-09-25 16:37:33 -070035
Jason Sams326e0dd2009-05-22 14:03:28 -070036 mElementIn.set(in);
37 mElementOut.set(out);
Jason Sams326e0dd2009-05-22 14:03:28 -070038}
39
40Program::~Program()
41{
Jason Sams5c3e3bc2009-10-26 15:19:28 -070042 bindAllocation(NULL);
Jason Sams326e0dd2009-05-22 14:03:28 -070043}
44
45
Jason Samscfb1d112009-08-05 13:57:03 -070046void Program::bindAllocation(Allocation *alloc)
Jason Sams326e0dd2009-05-22 14:03:28 -070047{
Jason Sams5c3e3bc2009-10-26 15:19:28 -070048 if (mConstants.get() == alloc) {
49 return;
50 }
51 if (mConstants.get()) {
52 mConstants.get()->removeProgramToDirty(this);
53 }
Jason Sams326e0dd2009-05-22 14:03:28 -070054 mConstants.set(alloc);
Jason Sams5c3e3bc2009-10-26 15:19:28 -070055 if (alloc) {
56 alloc->addProgramToDirty(this);
57 }
Jason Sams326e0dd2009-05-22 14:03:28 -070058 mDirty = true;
59}
60
Jason Samsc460e552009-11-25 13:22:07 -080061void Program::createShader()
62{
63}
Jason Samscfb1d112009-08-05 13:57:03 -070064
Jason Samsc460e552009-11-25 13:22:07 -080065bool Program::loadShader(uint32_t type)
66{
67 mShaderID = glCreateShader(type);
68 rsAssert(mShaderID);
69
Jason Samsf2a5d732009-11-30 14:49:55 -080070 LOGV("Loading shader type %x, ID %i", type, mShaderID);
Jason Samsc460e552009-11-25 13:22:07 -080071 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}
Jason Samsf2a5d732009-11-30 14:49:55 -080099
100void Program::setShader(const char *txt, uint32_t len)
101{
102 mUserShader.setTo(txt, len);
103}
104