blob: ba27fd4196a7686147dfa11fc2adff6079ce91de [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 Sams4815c0d2009-12-15 12:58:36 -080027Program::Program(Context *rsc) : 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 Sams4815c0d2009-12-15 12:58:36 -080036 mInputElements = NULL;
37 mOutputElements = NULL;
38 mConstantTypes = NULL;
39 mInputCount = 0;
40 mOutputCount = 0;
41 mConstantCount = 0;
42}
43
44Program::Program(Context *rsc, const char * shaderText, uint32_t shaderLength,
45 const uint32_t * params, uint32_t paramLength) :
46 ObjectBase(rsc)
47{
48 mAllocFile = __FILE__;
49 mAllocLine = __LINE__;
50 mDirty = true;
51 mShaderID = 0;
52 mAttribCount = 0;
53 mUniformCount = 0;
54
55 mInputCount = 0;
56 mOutputCount = 0;
57 mConstantCount = 0;
58
59 for (uint32_t ct=0; ct < paramLength; ct+=2) {
60 if (params[ct] == RS_PROGRAM_PARAM_INPUT) {
61 mInputCount++;
62 }
63 if (params[ct] == RS_PROGRAM_PARAM_OUTPUT) {
64 mOutputCount++;
65 }
66 if (params[ct] == RS_PROGRAM_PARAM_CONSTANT) {
67 mConstantCount++;
68 }
69 }
70
71 mInputElements = new ObjectBaseRef<Element>[mInputCount];
72 mOutputElements = new ObjectBaseRef<Element>[mOutputCount];
73 mConstantTypes = new ObjectBaseRef<Type>[mConstantCount];
74
75 uint32_t input = 0;
76 uint32_t output = 0;
77 uint32_t constant = 0;
78 for (uint32_t ct=0; ct < paramLength; ct+=2) {
79 if (params[ct] == RS_PROGRAM_PARAM_INPUT) {
80 mInputElements[input++].set(reinterpret_cast<Element *>(params[ct+1]));
81 }
82 if (params[ct] == RS_PROGRAM_PARAM_OUTPUT) {
83 mOutputElements[output++].set(reinterpret_cast<Element *>(params[ct+1]));
84 }
85 if (params[ct] == RS_PROGRAM_PARAM_CONSTANT) {
86 mConstantTypes[constant++].set(reinterpret_cast<Type *>(params[ct+1]));
87 }
88 }
89 mUserShader.setTo(shaderText, shaderLength);
Jason Sams326e0dd2009-05-22 14:03:28 -070090}
91
92Program::~Program()
93{
Jason Sams5c3e3bc2009-10-26 15:19:28 -070094 bindAllocation(NULL);
Jason Sams4815c0d2009-12-15 12:58:36 -080095
96 delete[] mInputElements;
97 delete[] mOutputElements;
98 delete[] mConstantTypes;
99 mInputCount = 0;
100 mOutputCount = 0;
101 mConstantCount = 0;
Jason Sams326e0dd2009-05-22 14:03:28 -0700102}
103
104
Jason Samscfb1d112009-08-05 13:57:03 -0700105void Program::bindAllocation(Allocation *alloc)
Jason Sams326e0dd2009-05-22 14:03:28 -0700106{
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700107 if (mConstants.get() == alloc) {
108 return;
109 }
110 if (mConstants.get()) {
111 mConstants.get()->removeProgramToDirty(this);
112 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700113 mConstants.set(alloc);
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700114 if (alloc) {
115 alloc->addProgramToDirty(this);
116 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700117 mDirty = true;
118}
119
Jason Samsc460e552009-11-25 13:22:07 -0800120void Program::createShader()
121{
122}
Jason Samscfb1d112009-08-05 13:57:03 -0700123
Jason Samsc460e552009-11-25 13:22:07 -0800124bool Program::loadShader(uint32_t type)
125{
126 mShaderID = glCreateShader(type);
127 rsAssert(mShaderID);
128
Jason Samsf2a5d732009-11-30 14:49:55 -0800129 LOGV("Loading shader type %x, ID %i", type, mShaderID);
Jason Samsc460e552009-11-25 13:22:07 -0800130 LOGE(mShader.string());
131
132 if (mShaderID) {
133 const char * ss = mShader.string();
134 glShaderSource(mShaderID, 1, &ss, NULL);
135 glCompileShader(mShaderID);
136
137 GLint compiled = 0;
138 glGetShaderiv(mShaderID, GL_COMPILE_STATUS, &compiled);
139 if (!compiled) {
140 GLint infoLen = 0;
141 glGetShaderiv(mShaderID, GL_INFO_LOG_LENGTH, &infoLen);
142 if (infoLen) {
143 char* buf = (char*) malloc(infoLen);
144 if (buf) {
145 glGetShaderInfoLog(mShaderID, infoLen, NULL, buf);
146 LOGE("Could not compile shader \n%s\n", buf);
147 free(buf);
148 }
149 glDeleteShader(mShaderID);
150 mShaderID = 0;
151 return false;
152 }
153 }
154 }
155 LOGV("--Shader load result %x ", glGetError());
156 return true;
157}
Jason Samsf2a5d732009-11-30 14:49:55 -0800158
159void Program::setShader(const char *txt, uint32_t len)
160{
161 mUserShader.setTo(txt, len);
162}
163
Jason Sams4815c0d2009-12-15 12:58:36 -0800164
165
166namespace android {
167namespace renderscript {
168
169
170void rsi_ProgramBindConstants(Context *rsc, RsProgram vp, uint32_t slot, RsAllocation constants)
171{
172 Program *p = static_cast<Program *>(vp);
173 p->bindAllocation(static_cast<Allocation *>(constants));
174}
175
176
177}
178}
179