blob: 8d0e43c5319c9e9658f821b68663e9a8585c61c5 [file] [log] [blame]
Jason Sams326e0dd2009-05-22 14:03:28 -07001/*
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -07002 * Copyright (C) 2011 The Android Open Source Project
Jason Sams326e0dd2009-05-22 14:03:28 -07003 *
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"
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070018#include "rsProgram.h"
Jason Samsc460e552009-11-25 13:22:07 -080019
Ian Rogersf8852d02014-01-29 15:35:17 -080020#include <inttypes.h>
21
Chih-Hung Hsieh11496ac2016-11-15 15:14:05 -080022namespace android {
23namespace renderscript {
Jason Sams326e0dd2009-05-22 14:03:28 -070024
Alex Sakhartchouk748eb072012-02-15 16:21:46 -080025Program::Program(Context *rsc, const char * shaderText, size_t shaderLength,
Ian Rogersf8852d02014-01-29 15:35:17 -080026 const uintptr_t * params, size_t paramLength)
Alex Sakhartchouk7f126c72011-05-05 16:56:27 -070027 : ProgramBase(rsc) {
Jason Sams4815c0d2009-12-15 12:58:36 -080028
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080029 initMemberVars();
Jason Sams4815c0d2009-12-15 12:58:36 -080030 for (uint32_t ct=0; ct < paramLength; ct+=2) {
31 if (params[ct] == RS_PROGRAM_PARAM_INPUT) {
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -070032 mHal.state.inputElementsCount++;
Jason Sams4815c0d2009-12-15 12:58:36 -080033 }
34 if (params[ct] == RS_PROGRAM_PARAM_CONSTANT) {
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -070035 mHal.state.constantsCount++;
Jason Sams4815c0d2009-12-15 12:58:36 -080036 }
Alex Sakhartchouk84e40272010-11-18 15:22:43 -080037 if (params[ct] == RS_PROGRAM_PARAM_TEXTURE_TYPE) {
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -070038 mHal.state.texturesCount++;
Jason Samsf2e4fa22009-12-15 13:27:04 -080039 }
Jason Sams4815c0d2009-12-15 12:58:36 -080040 }
41
Alex Sakhartchouk064aa7e2011-10-18 10:54:29 -070042 mTextures = new ObjectBaseRef<Allocation>[mHal.state.texturesCount];
43 mSamplers = new ObjectBaseRef<Sampler>[mHal.state.texturesCount];
44 mInputElements = new ObjectBaseRef<Element>[mHal.state.inputElementsCount];
45 mConstantTypes = new ObjectBaseRef<Type>[mHal.state.constantsCount];
46 mConstants = new ObjectBaseRef<Allocation>[mHal.state.constantsCount];
47
48 mHal.state.textures = new Allocation*[mHal.state.texturesCount];
49 mHal.state.samplers = new Sampler*[mHal.state.texturesCount];
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -070050 mHal.state.textureTargets = new RsTextureTarget[mHal.state.texturesCount];
Alex Sakhartchouk064aa7e2011-10-18 10:54:29 -070051 mHal.state.inputElements = new Element*[mHal.state.inputElementsCount];
52 mHal.state.constantTypes = new Type*[mHal.state.constantsCount];
53 mHal.state.constants = new Allocation*[mHal.state.constantsCount];
54
55 // Will initialize everything
56 freeChildren();
Jason Sams4815c0d2009-12-15 12:58:36 -080057
58 uint32_t input = 0;
Jason Sams4815c0d2009-12-15 12:58:36 -080059 uint32_t constant = 0;
Alex Sakhartchouk84e40272010-11-18 15:22:43 -080060 uint32_t texture = 0;
Jason Sams4815c0d2009-12-15 12:58:36 -080061 for (uint32_t ct=0; ct < paramLength; ct+=2) {
62 if (params[ct] == RS_PROGRAM_PARAM_INPUT) {
Alex Sakhartchouk064aa7e2011-10-18 10:54:29 -070063 mInputElements[input].set(reinterpret_cast<Element *>(params[ct+1]));
64 mHal.state.inputElements[input++] = reinterpret_cast<Element *>(params[ct+1]);
Jason Sams4815c0d2009-12-15 12:58:36 -080065 }
66 if (params[ct] == RS_PROGRAM_PARAM_CONSTANT) {
Alex Sakhartchouk064aa7e2011-10-18 10:54:29 -070067 mConstantTypes[constant].set(reinterpret_cast<Type *>(params[ct+1]));
68 mHal.state.constantTypes[constant++] = reinterpret_cast<Type *>(params[ct+1]);
Jason Sams4815c0d2009-12-15 12:58:36 -080069 }
Alex Sakhartchouk84e40272010-11-18 15:22:43 -080070 if (params[ct] == RS_PROGRAM_PARAM_TEXTURE_TYPE) {
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -070071 mHal.state.textureTargets[texture++] = (RsTextureTarget)params[ct+1];
Alex Sakhartchouk84e40272010-11-18 15:22:43 -080072 }
Jason Sams4815c0d2009-12-15 12:58:36 -080073 }
Alex Sakhartchouke7ae69f2010-09-14 09:50:43 -070074 mIsInternal = false;
75 uint32_t internalTokenLen = strlen(RS_SHADER_INTERNAL);
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080076 if (shaderLength > internalTokenLen &&
Alex Sakhartchouke7ae69f2010-09-14 09:50:43 -070077 strncmp(RS_SHADER_INTERNAL, shaderText, internalTokenLen) == 0) {
78 mIsInternal = true;
79 shaderText += internalTokenLen;
80 shaderLength -= internalTokenLen;
81 }
Jason Samsf313dc32013-07-09 14:29:39 -070082
83 mUserShader = rsuCopyString(shaderText, shaderLength);
84 mUserShaderLen = shaderLength;
Jason Sams326e0dd2009-05-22 14:03:28 -070085}
86
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080087Program::~Program() {
Jason Samsc7cec1e2011-08-18 18:01:33 -070088 freeChildren();
Alex Sakhartchouk889fe502010-10-01 10:54:06 -070089
Alex Sakhartchouk064aa7e2011-10-18 10:54:29 -070090 delete[] mTextures;
91 delete[] mSamplers;
92 delete[] mInputElements;
93 delete[] mConstantTypes;
94 delete[] mConstants;
95
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -070096 delete[] mHal.state.textures;
97 delete[] mHal.state.samplers;
98 delete[] mHal.state.textureTargets;
99 delete[] mHal.state.inputElements;
100 delete[] mHal.state.constantTypes;
101 delete[] mHal.state.constants;
102 mHal.state.inputElementsCount = 0;
103 mHal.state.constantsCount = 0;
104 mHal.state.texturesCount = 0;
Jason Samsf313dc32013-07-09 14:29:39 -0700105
Chris Wailes44bef6f2014-08-12 13:51:10 -0700106 if (mUserShader != nullptr) {
Jason Samsf313dc32013-07-09 14:29:39 -0700107 delete[] mUserShader;
Chris Wailes44bef6f2014-08-12 13:51:10 -0700108 mUserShader = nullptr;
Jason Samsf313dc32013-07-09 14:29:39 -0700109 }
110 mUserShaderLen = 0;
Jason Sams326e0dd2009-05-22 14:03:28 -0700111}
112
Jason Samsc7cec1e2011-08-18 18:01:33 -0700113bool Program::freeChildren() {
114 for (uint32_t ct=0; ct < mHal.state.constantsCount; ct++) {
Chris Wailes44bef6f2014-08-12 13:51:10 -0700115 bindAllocation(nullptr, nullptr, ct);
Jason Samsc7cec1e2011-08-18 18:01:33 -0700116 }
117
118 for (uint32_t ct=0; ct < mHal.state.texturesCount; ct++) {
Chris Wailes44bef6f2014-08-12 13:51:10 -0700119 bindTexture(nullptr, ct, nullptr);
120 bindSampler(nullptr, ct, nullptr);
Jason Samsc7cec1e2011-08-18 18:01:33 -0700121 }
122 return false;
123}
124
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800125void Program::initMemberVars() {
126 mDirty = true;
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800127
Chris Wailes44bef6f2014-08-12 13:51:10 -0700128 mHal.drv = nullptr;
129 mHal.state.textures = nullptr;
130 mHal.state.samplers = nullptr;
131 mHal.state.textureTargets = nullptr;
132 mHal.state.inputElements = nullptr;
133 mHal.state.constantTypes = nullptr;
134 mHal.state.constants = nullptr;
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -0700135
136 mHal.state.inputElementsCount = 0;
137 mHal.state.constantsCount = 0;
138 mHal.state.texturesCount = 0;
139
Chris Wailes44bef6f2014-08-12 13:51:10 -0700140 mTextures = nullptr;
141 mSamplers = nullptr;
142 mInputElements = nullptr;
143 mConstantTypes = nullptr;
144 mConstants = nullptr;
Alex Sakhartchouk064aa7e2011-10-18 10:54:29 -0700145
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800146 mIsInternal = false;
Jason Samsf313dc32013-07-09 14:29:39 -0700147
Chris Wailes44bef6f2014-08-12 13:51:10 -0700148 mUserShader = nullptr;
Jason Samsf313dc32013-07-09 14:29:39 -0700149 mUserShaderLen = 0;
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800150}
Jason Sams326e0dd2009-05-22 14:03:28 -0700151
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800152void Program::bindAllocation(Context *rsc, Allocation *alloc, uint32_t slot) {
Chris Wailes44bef6f2014-08-12 13:51:10 -0700153 if (alloc != nullptr) {
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -0700154 if (slot >= mHal.state.constantsCount) {
Ian Rogersf8852d02014-01-29 15:35:17 -0800155 ALOGE("Attempt to bind alloc at slot %u, on shader id %" PRIuPTR ", but const count is %u",
156 slot, (uintptr_t)this, mHal.state.constantsCount);
Alex Sakhartchouk383e5b12010-09-23 16:16:33 -0700157 rsc->setError(RS_ERROR_BAD_SHADER, "Cannot bind allocation");
158 return;
159 }
Alex Sakhartchouk064aa7e2011-10-18 10:54:29 -0700160 if (alloc->getType() != mConstantTypes[slot].get()) {
Ian Rogersf8852d02014-01-29 15:35:17 -0800161 ALOGE("Attempt to bind alloc at slot %u, on shader id %" PRIuPTR ", but types mismatch",
162 slot, (uintptr_t)this);
Alex Sakhartchouk383e5b12010-09-23 16:16:33 -0700163 rsc->setError(RS_ERROR_BAD_SHADER, "Cannot bind allocation");
164 return;
165 }
166 }
Alex Sakhartchouk064aa7e2011-10-18 10:54:29 -0700167 if (mConstants[slot].get() == alloc) {
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700168 return;
169 }
Alex Sakhartchouk064aa7e2011-10-18 10:54:29 -0700170 if (mConstants[slot].get()) {
171 mConstants[slot]->removeProgramToDirty(this);
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700172 }
Alex Sakhartchouk064aa7e2011-10-18 10:54:29 -0700173 mConstants[slot].set(alloc);
174 mHal.state.constants[slot] = alloc;
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700175 if (alloc) {
176 alloc->addProgramToDirty(this);
177 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700178 mDirty = true;
179}
180
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800181void Program::bindTexture(Context *rsc, uint32_t slot, Allocation *a) {
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -0700182 if (slot >= mHal.state.texturesCount) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000183 ALOGE("Attempt to bind texture to slot %u but tex count is %u", slot, mHal.state.texturesCount);
Alex Sakhartchouk383e5b12010-09-23 16:16:33 -0700184 rsc->setError(RS_ERROR_BAD_SHADER, "Cannot bind texture");
Jason Sams7dad9c32009-12-17 16:55:08 -0800185 return;
186 }
187
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -0700188 if (a && a->getType()->getDimFaces() && mHal.state.textureTargets[slot] != RS_TEXTURE_CUBE) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000189 ALOGE("Attempt to bind cubemap to slot %u but 2d texture needed", slot);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800190 rsc->setError(RS_ERROR_BAD_SHADER, "Cannot bind cubemap to 2d texture slot");
191 return;
192 }
193
Alex Sakhartchouk064aa7e2011-10-18 10:54:29 -0700194 mTextures[slot].set(a);
195 mHal.state.textures[slot] = a;
196
Jason Sams7dad9c32009-12-17 16:55:08 -0800197 mDirty = true;
198}
199
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800200void Program::bindSampler(Context *rsc, uint32_t slot, Sampler *s) {
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -0700201 if (slot >= mHal.state.texturesCount) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000202 ALOGE("Attempt to bind sampler to slot %u but tex count is %u", slot, mHal.state.texturesCount);
Alex Sakhartchouk383e5b12010-09-23 16:16:33 -0700203 rsc->setError(RS_ERROR_BAD_SHADER, "Cannot bind sampler");
Jason Sams7dad9c32009-12-17 16:55:08 -0800204 return;
205 }
206
Alex Sakhartchouk064aa7e2011-10-18 10:54:29 -0700207 mSamplers[slot].set(s);
208 mHal.state.samplers[slot] = s;
Jason Sams7dad9c32009-12-17 16:55:08 -0800209 mDirty = true;
210}
211
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800212void rsi_ProgramBindConstants(Context *rsc, RsProgram vp, uint32_t slot, RsAllocation constants) {
Jason Sams4815c0d2009-12-15 12:58:36 -0800213 Program *p = static_cast<Program *>(vp);
Alex Sakhartchouk383e5b12010-09-23 16:16:33 -0700214 p->bindAllocation(rsc, static_cast<Allocation *>(constants), slot);
Jason Sams4815c0d2009-12-15 12:58:36 -0800215}
216
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800217void rsi_ProgramBindTexture(Context *rsc, RsProgram vpf, uint32_t slot, RsAllocation a) {
Jason Sams7dad9c32009-12-17 16:55:08 -0800218 Program *p = static_cast<Program *>(vpf);
Alex Sakhartchouk383e5b12010-09-23 16:16:33 -0700219 p->bindTexture(rsc, slot, static_cast<Allocation *>(a));
Jason Sams7dad9c32009-12-17 16:55:08 -0800220}
221
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800222void rsi_ProgramBindSampler(Context *rsc, RsProgram vpf, uint32_t slot, RsSampler s) {
Jason Sams7dad9c32009-12-17 16:55:08 -0800223 Program *p = static_cast<Program *>(vpf);
Alex Sakhartchouk383e5b12010-09-23 16:16:33 -0700224 p->bindSampler(rsc, slot, static_cast<Sampler *>(s));
Jason Sams7dad9c32009-12-17 16:55:08 -0800225}
Jason Sams4815c0d2009-12-15 12:58:36 -0800226
Chih-Hung Hsieh11496ac2016-11-15 15:14:05 -0800227} // namespace renderscript
228} // namespace android