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,uint32_t shaderLength,const uint32_t * params,uint32_t paramLength)23 Program::Program(Context *rsc, const char * shaderText, uint32_t shaderLength,
24 const uint32_t * params, uint32_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 mHal.state.textures = new ObjectBaseRef<Allocation>[mHal.state.texturesCount];
41 mHal.state.samplers = new ObjectBaseRef<Sampler>[mHal.state.texturesCount];
42 mHal.state.textureTargets = new RsTextureTarget[mHal.state.texturesCount];
43 mHal.state.inputElements = new ObjectBaseRef<Element>[mHal.state.inputElementsCount];
44 mHal.state.constantTypes = new ObjectBaseRef<Type>[mHal.state.constantsCount];
45 mHal.state.constants = new ObjectBaseRef<Allocation>[mHal.state.constantsCount];
46
47 uint32_t input = 0;
48 uint32_t constant = 0;
49 uint32_t texture = 0;
50 for (uint32_t ct=0; ct < paramLength; ct+=2) {
51 if (params[ct] == RS_PROGRAM_PARAM_INPUT) {
52 mHal.state.inputElements[input++].set(reinterpret_cast<Element *>(params[ct+1]));
53 }
54 if (params[ct] == RS_PROGRAM_PARAM_CONSTANT) {
55 mHal.state.constantTypes[constant++].set(reinterpret_cast<Type *>(params[ct+1]));
56 }
57 if (params[ct] == RS_PROGRAM_PARAM_TEXTURE_TYPE) {
58 mHal.state.textureTargets[texture++] = (RsTextureTarget)params[ct+1];
59 }
60 }
61 mIsInternal = false;
62 uint32_t internalTokenLen = strlen(RS_SHADER_INTERNAL);
63 if (shaderLength > internalTokenLen &&
64 strncmp(RS_SHADER_INTERNAL, shaderText, internalTokenLen) == 0) {
65 mIsInternal = true;
66 shaderText += internalTokenLen;
67 shaderLength -= internalTokenLen;
68 }
69 mUserShader.setTo(shaderText, shaderLength);
70 }
71
~Program()72 Program::~Program() {
73 freeChildren();
74
75 delete[] mHal.state.textures;
76 delete[] mHal.state.samplers;
77 delete[] mHal.state.textureTargets;
78 delete[] mHal.state.inputElements;
79 delete[] mHal.state.constantTypes;
80 delete[] mHal.state.constants;
81 mHal.state.inputElementsCount = 0;
82 mHal.state.constantsCount = 0;
83 mHal.state.texturesCount = 0;
84 }
85
freeChildren()86 bool Program::freeChildren() {
87 for (uint32_t ct=0; ct < mHal.state.constantsCount; ct++) {
88 bindAllocation(NULL, NULL, ct);
89 }
90
91 for (uint32_t ct=0; ct < mHal.state.texturesCount; ct++) {
92 bindTexture(NULL, ct, NULL);
93 bindSampler(NULL, ct, NULL);
94 }
95 return false;
96 }
97
initMemberVars()98 void Program::initMemberVars() {
99 mDirty = true;
100
101 mHal.drv = NULL;
102 mHal.state.textures = NULL;
103 mHal.state.samplers = NULL;
104 mHal.state.textureTargets = NULL;
105 mHal.state.inputElements = NULL;
106 mHal.state.constantTypes = NULL;
107 mHal.state.constants = NULL;
108
109 mHal.state.inputElementsCount = 0;
110 mHal.state.constantsCount = 0;
111 mHal.state.texturesCount = 0;
112
113 mIsInternal = false;
114 }
115
bindAllocation(Context * rsc,Allocation * alloc,uint32_t slot)116 void Program::bindAllocation(Context *rsc, Allocation *alloc, uint32_t slot) {
117 if (alloc != NULL) {
118 if (slot >= mHal.state.constantsCount) {
119 LOGE("Attempt to bind alloc at slot %u, on shader id %u, but const count is %u",
120 slot, (uint32_t)this, mHal.state.constantsCount);
121 rsc->setError(RS_ERROR_BAD_SHADER, "Cannot bind allocation");
122 return;
123 }
124 if (alloc->getType() != mHal.state.constantTypes[slot].get()) {
125 LOGE("Attempt to bind alloc at slot %u, on shader id %u, but types mismatch",
126 slot, (uint32_t)this);
127 rsc->setError(RS_ERROR_BAD_SHADER, "Cannot bind allocation");
128 return;
129 }
130 }
131 if (mHal.state.constants[slot].get() == alloc) {
132 return;
133 }
134 if (mHal.state.constants[slot].get()) {
135 mHal.state.constants[slot].get()->removeProgramToDirty(this);
136 }
137 mHal.state.constants[slot].set(alloc);
138 if (alloc) {
139 alloc->addProgramToDirty(this);
140 }
141 mDirty = true;
142 }
143
bindTexture(Context * rsc,uint32_t slot,Allocation * a)144 void Program::bindTexture(Context *rsc, uint32_t slot, Allocation *a) {
145 if (slot >= mHal.state.texturesCount) {
146 LOGE("Attempt to bind texture to slot %u but tex count is %u", slot, mHal.state.texturesCount);
147 rsc->setError(RS_ERROR_BAD_SHADER, "Cannot bind texture");
148 return;
149 }
150
151 if (a && a->getType()->getDimFaces() && mHal.state.textureTargets[slot] != RS_TEXTURE_CUBE) {
152 LOGE("Attempt to bind cubemap to slot %u but 2d texture needed", slot);
153 rsc->setError(RS_ERROR_BAD_SHADER, "Cannot bind cubemap to 2d texture slot");
154 return;
155 }
156
157 mHal.state.textures[slot].set(a);
158 mDirty = true;
159 }
160
bindSampler(Context * rsc,uint32_t slot,Sampler * s)161 void Program::bindSampler(Context *rsc, uint32_t slot, Sampler *s) {
162 if (slot >= mHal.state.texturesCount) {
163 LOGE("Attempt to bind sampler to slot %u but tex count is %u", slot, mHal.state.texturesCount);
164 rsc->setError(RS_ERROR_BAD_SHADER, "Cannot bind sampler");
165 return;
166 }
167
168 mHal.state.samplers[slot].set(s);
169 mDirty = true;
170 }
171
172 namespace android {
173 namespace renderscript {
174
rsi_ProgramBindConstants(Context * rsc,RsProgram vp,uint32_t slot,RsAllocation constants)175 void rsi_ProgramBindConstants(Context *rsc, RsProgram vp, uint32_t slot, RsAllocation constants) {
176 Program *p = static_cast<Program *>(vp);
177 p->bindAllocation(rsc, static_cast<Allocation *>(constants), slot);
178 }
179
rsi_ProgramBindTexture(Context * rsc,RsProgram vpf,uint32_t slot,RsAllocation a)180 void rsi_ProgramBindTexture(Context *rsc, RsProgram vpf, uint32_t slot, RsAllocation a) {
181 Program *p = static_cast<Program *>(vpf);
182 p->bindTexture(rsc, slot, static_cast<Allocation *>(a));
183 }
184
rsi_ProgramBindSampler(Context * rsc,RsProgram vpf,uint32_t slot,RsSampler s)185 void rsi_ProgramBindSampler(Context *rsc, RsProgram vpf, uint32_t slot, RsSampler s) {
186 Program *p = static_cast<Program *>(vpf);
187 p->bindSampler(rsc, slot, static_cast<Sampler *>(s));
188 }
189
190 }
191 }
192
193