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 #include "rsContext.h"
18 #include "rsProgram.h"
19
20 using namespace android;
21 using namespace android::renderscript;
22
Program(Context * rsc,const char * shaderText,size_t shaderLength,const uint32_t * params,size_t paramLength)23 Program::Program(Context *rsc, const char * shaderText, size_t shaderLength,
24 const uint32_t * params, size_t paramLength)
25 : ProgramBase(rsc) {
26
27 initMemberVars();
28 for (uint32_t ct=0; ct < paramLength; ct+=2) {
29 if (params[ct] == RS_PROGRAM_PARAM_INPUT) {
30 mHal.state.inputElementsCount++;
31 }
32 if (params[ct] == RS_PROGRAM_PARAM_CONSTANT) {
33 mHal.state.constantsCount++;
34 }
35 if (params[ct] == RS_PROGRAM_PARAM_TEXTURE_TYPE) {
36 mHal.state.texturesCount++;
37 }
38 }
39
40 mTextures = new ObjectBaseRef<Allocation>[mHal.state.texturesCount];
41 mSamplers = new ObjectBaseRef<Sampler>[mHal.state.texturesCount];
42 mInputElements = new ObjectBaseRef<Element>[mHal.state.inputElementsCount];
43 mConstantTypes = new ObjectBaseRef<Type>[mHal.state.constantsCount];
44 mConstants = new ObjectBaseRef<Allocation>[mHal.state.constantsCount];
45
46 mHal.state.textures = new Allocation*[mHal.state.texturesCount];
47 mHal.state.samplers = new Sampler*[mHal.state.texturesCount];
48 mHal.state.textureTargets = new RsTextureTarget[mHal.state.texturesCount];
49 mHal.state.inputElements = new Element*[mHal.state.inputElementsCount];
50 mHal.state.constantTypes = new Type*[mHal.state.constantsCount];
51 mHal.state.constants = new Allocation*[mHal.state.constantsCount];
52
53 // Will initialize everything
54 freeChildren();
55
56 uint32_t input = 0;
57 uint32_t constant = 0;
58 uint32_t texture = 0;
59 for (uint32_t ct=0; ct < paramLength; ct+=2) {
60 if (params[ct] == RS_PROGRAM_PARAM_INPUT) {
61 mInputElements[input].set(reinterpret_cast<Element *>(params[ct+1]));
62 mHal.state.inputElements[input++] = reinterpret_cast<Element *>(params[ct+1]);
63 }
64 if (params[ct] == RS_PROGRAM_PARAM_CONSTANT) {
65 mConstantTypes[constant].set(reinterpret_cast<Type *>(params[ct+1]));
66 mHal.state.constantTypes[constant++] = reinterpret_cast<Type *>(params[ct+1]);
67 }
68 if (params[ct] == RS_PROGRAM_PARAM_TEXTURE_TYPE) {
69 mHal.state.textureTargets[texture++] = (RsTextureTarget)params[ct+1];
70 }
71 }
72 mIsInternal = false;
73 uint32_t internalTokenLen = strlen(RS_SHADER_INTERNAL);
74 if (shaderLength > internalTokenLen &&
75 strncmp(RS_SHADER_INTERNAL, shaderText, internalTokenLen) == 0) {
76 mIsInternal = true;
77 shaderText += internalTokenLen;
78 shaderLength -= internalTokenLen;
79 }
80
81 mUserShader = rsuCopyString(shaderText, shaderLength);
82 mUserShaderLen = shaderLength;
83 }
84
~Program()85 Program::~Program() {
86 freeChildren();
87
88 delete[] mTextures;
89 delete[] mSamplers;
90 delete[] mInputElements;
91 delete[] mConstantTypes;
92 delete[] mConstants;
93
94 delete[] mHal.state.textures;
95 delete[] mHal.state.samplers;
96 delete[] mHal.state.textureTargets;
97 delete[] mHal.state.inputElements;
98 delete[] mHal.state.constantTypes;
99 delete[] mHal.state.constants;
100 mHal.state.inputElementsCount = 0;
101 mHal.state.constantsCount = 0;
102 mHal.state.texturesCount = 0;
103
104 if (mUserShader != NULL) {
105 delete[] mUserShader;
106 mUserShader = NULL;
107 }
108 mUserShaderLen = 0;
109 }
110
freeChildren()111 bool Program::freeChildren() {
112 for (uint32_t ct=0; ct < mHal.state.constantsCount; ct++) {
113 bindAllocation(NULL, NULL, ct);
114 }
115
116 for (uint32_t ct=0; ct < mHal.state.texturesCount; ct++) {
117 bindTexture(NULL, ct, NULL);
118 bindSampler(NULL, ct, NULL);
119 }
120 return false;
121 }
122
initMemberVars()123 void Program::initMemberVars() {
124 mDirty = true;
125
126 mHal.drv = NULL;
127 mHal.state.textures = NULL;
128 mHal.state.samplers = NULL;
129 mHal.state.textureTargets = NULL;
130 mHal.state.inputElements = NULL;
131 mHal.state.constantTypes = NULL;
132 mHal.state.constants = NULL;
133
134 mHal.state.inputElementsCount = 0;
135 mHal.state.constantsCount = 0;
136 mHal.state.texturesCount = 0;
137
138 mTextures = NULL;
139 mSamplers = NULL;
140 mInputElements = NULL;
141 mConstantTypes = NULL;
142 mConstants = NULL;
143
144 mIsInternal = false;
145
146 mUserShader = NULL;
147 mUserShaderLen = 0;
148 }
149
bindAllocation(Context * rsc,Allocation * alloc,uint32_t slot)150 void Program::bindAllocation(Context *rsc, Allocation *alloc, uint32_t slot) {
151 if (alloc != NULL) {
152 if (slot >= mHal.state.constantsCount) {
153 ALOGE("Attempt to bind alloc at slot %u, on shader id %u, but const count is %u",
154 slot, (uint32_t)this, mHal.state.constantsCount);
155 rsc->setError(RS_ERROR_BAD_SHADER, "Cannot bind allocation");
156 return;
157 }
158 if (alloc->getType() != mConstantTypes[slot].get()) {
159 ALOGE("Attempt to bind alloc at slot %u, on shader id %u, but types mismatch",
160 slot, (uint32_t)this);
161 rsc->setError(RS_ERROR_BAD_SHADER, "Cannot bind allocation");
162 return;
163 }
164 }
165 if (mConstants[slot].get() == alloc) {
166 return;
167 }
168 if (mConstants[slot].get()) {
169 mConstants[slot]->removeProgramToDirty(this);
170 }
171 mConstants[slot].set(alloc);
172 mHal.state.constants[slot] = alloc;
173 if (alloc) {
174 alloc->addProgramToDirty(this);
175 }
176 mDirty = true;
177 }
178
bindTexture(Context * rsc,uint32_t slot,Allocation * a)179 void Program::bindTexture(Context *rsc, uint32_t slot, Allocation *a) {
180 if (slot >= mHal.state.texturesCount) {
181 ALOGE("Attempt to bind texture to slot %u but tex count is %u", slot, mHal.state.texturesCount);
182 rsc->setError(RS_ERROR_BAD_SHADER, "Cannot bind texture");
183 return;
184 }
185
186 if (a && a->getType()->getDimFaces() && mHal.state.textureTargets[slot] != RS_TEXTURE_CUBE) {
187 ALOGE("Attempt to bind cubemap to slot %u but 2d texture needed", slot);
188 rsc->setError(RS_ERROR_BAD_SHADER, "Cannot bind cubemap to 2d texture slot");
189 return;
190 }
191
192 mTextures[slot].set(a);
193 mHal.state.textures[slot] = a;
194
195 mDirty = true;
196 }
197
bindSampler(Context * rsc,uint32_t slot,Sampler * s)198 void Program::bindSampler(Context *rsc, uint32_t slot, Sampler *s) {
199 if (slot >= mHal.state.texturesCount) {
200 ALOGE("Attempt to bind sampler to slot %u but tex count is %u", slot, mHal.state.texturesCount);
201 rsc->setError(RS_ERROR_BAD_SHADER, "Cannot bind sampler");
202 return;
203 }
204
205 mSamplers[slot].set(s);
206 mHal.state.samplers[slot] = s;
207 mDirty = true;
208 }
209
210 namespace android {
211 namespace renderscript {
212
rsi_ProgramBindConstants(Context * rsc,RsProgram vp,uint32_t slot,RsAllocation constants)213 void rsi_ProgramBindConstants(Context *rsc, RsProgram vp, uint32_t slot, RsAllocation constants) {
214 Program *p = static_cast<Program *>(vp);
215 p->bindAllocation(rsc, static_cast<Allocation *>(constants), slot);
216 }
217
rsi_ProgramBindTexture(Context * rsc,RsProgram vpf,uint32_t slot,RsAllocation a)218 void rsi_ProgramBindTexture(Context *rsc, RsProgram vpf, uint32_t slot, RsAllocation a) {
219 Program *p = static_cast<Program *>(vpf);
220 p->bindTexture(rsc, slot, static_cast<Allocation *>(a));
221 }
222
rsi_ProgramBindSampler(Context * rsc,RsProgram vpf,uint32_t slot,RsSampler s)223 void rsi_ProgramBindSampler(Context *rsc, RsProgram vpf, uint32_t slot, RsSampler s) {
224 Program *p = static_cast<Program *>(vpf);
225 p->bindSampler(rsc, slot, static_cast<Sampler *>(s));
226 }
227
228 }
229 }
230
231