blob: 7114f29d050973bcb7eaf92ff6db5f3b32e670e9 [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
Jason Sams326e0dd2009-05-22 14:03:28 -070020using namespace android;
21using namespace android::renderscript;
22
Alex Sakhartchouk748eb072012-02-15 16:21:46 -080023Program::Program(Context *rsc, const char * shaderText, size_t shaderLength,
24 const uint32_t * params, size_t paramLength)
Alex Sakhartchouk7f126c72011-05-05 16:56:27 -070025 : ProgramBase(rsc) {
Jason Sams4815c0d2009-12-15 12:58:36 -080026
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080027 initMemberVars();
Jason Sams4815c0d2009-12-15 12:58:36 -080028 for (uint32_t ct=0; ct < paramLength; ct+=2) {
29 if (params[ct] == RS_PROGRAM_PARAM_INPUT) {
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -070030 mHal.state.inputElementsCount++;
Jason Sams4815c0d2009-12-15 12:58:36 -080031 }
32 if (params[ct] == RS_PROGRAM_PARAM_CONSTANT) {
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -070033 mHal.state.constantsCount++;
Jason Sams4815c0d2009-12-15 12:58:36 -080034 }
Alex Sakhartchouk84e40272010-11-18 15:22:43 -080035 if (params[ct] == RS_PROGRAM_PARAM_TEXTURE_TYPE) {
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -070036 mHal.state.texturesCount++;
Jason Samsf2e4fa22009-12-15 13:27:04 -080037 }
Jason Sams4815c0d2009-12-15 12:58:36 -080038 }
39
Alex Sakhartchouk064aa7e2011-10-18 10:54:29 -070040 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];
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -070048 mHal.state.textureTargets = new RsTextureTarget[mHal.state.texturesCount];
Alex Sakhartchouk064aa7e2011-10-18 10:54:29 -070049 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();
Jason Sams4815c0d2009-12-15 12:58:36 -080055
56 uint32_t input = 0;
Jason Sams4815c0d2009-12-15 12:58:36 -080057 uint32_t constant = 0;
Alex Sakhartchouk84e40272010-11-18 15:22:43 -080058 uint32_t texture = 0;
Jason Sams4815c0d2009-12-15 12:58:36 -080059 for (uint32_t ct=0; ct < paramLength; ct+=2) {
60 if (params[ct] == RS_PROGRAM_PARAM_INPUT) {
Alex Sakhartchouk064aa7e2011-10-18 10:54:29 -070061 mInputElements[input].set(reinterpret_cast<Element *>(params[ct+1]));
62 mHal.state.inputElements[input++] = reinterpret_cast<Element *>(params[ct+1]);
Jason Sams4815c0d2009-12-15 12:58:36 -080063 }
64 if (params[ct] == RS_PROGRAM_PARAM_CONSTANT) {
Alex Sakhartchouk064aa7e2011-10-18 10:54:29 -070065 mConstantTypes[constant].set(reinterpret_cast<Type *>(params[ct+1]));
66 mHal.state.constantTypes[constant++] = reinterpret_cast<Type *>(params[ct+1]);
Jason Sams4815c0d2009-12-15 12:58:36 -080067 }
Alex Sakhartchouk84e40272010-11-18 15:22:43 -080068 if (params[ct] == RS_PROGRAM_PARAM_TEXTURE_TYPE) {
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -070069 mHal.state.textureTargets[texture++] = (RsTextureTarget)params[ct+1];
Alex Sakhartchouk84e40272010-11-18 15:22:43 -080070 }
Jason Sams4815c0d2009-12-15 12:58:36 -080071 }
Alex Sakhartchouke7ae69f2010-09-14 09:50:43 -070072 mIsInternal = false;
73 uint32_t internalTokenLen = strlen(RS_SHADER_INTERNAL);
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080074 if (shaderLength > internalTokenLen &&
Alex Sakhartchouke7ae69f2010-09-14 09:50:43 -070075 strncmp(RS_SHADER_INTERNAL, shaderText, internalTokenLen) == 0) {
76 mIsInternal = true;
77 shaderText += internalTokenLen;
78 shaderLength -= internalTokenLen;
79 }
Jason Sams4815c0d2009-12-15 12:58:36 -080080 mUserShader.setTo(shaderText, shaderLength);
Jason Sams326e0dd2009-05-22 14:03:28 -070081}
82
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080083Program::~Program() {
Jason Samsc7cec1e2011-08-18 18:01:33 -070084 freeChildren();
Alex Sakhartchouk889fe502010-10-01 10:54:06 -070085
Alex Sakhartchouk064aa7e2011-10-18 10:54:29 -070086 delete[] mTextures;
87 delete[] mSamplers;
88 delete[] mInputElements;
89 delete[] mConstantTypes;
90 delete[] mConstants;
91
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -070092 delete[] mHal.state.textures;
93 delete[] mHal.state.samplers;
94 delete[] mHal.state.textureTargets;
95 delete[] mHal.state.inputElements;
96 delete[] mHal.state.constantTypes;
97 delete[] mHal.state.constants;
98 mHal.state.inputElementsCount = 0;
99 mHal.state.constantsCount = 0;
100 mHal.state.texturesCount = 0;
Jason Sams326e0dd2009-05-22 14:03:28 -0700101}
102
Jason Samsc7cec1e2011-08-18 18:01:33 -0700103bool Program::freeChildren() {
104 for (uint32_t ct=0; ct < mHal.state.constantsCount; ct++) {
105 bindAllocation(NULL, NULL, ct);
106 }
107
108 for (uint32_t ct=0; ct < mHal.state.texturesCount; ct++) {
109 bindTexture(NULL, ct, NULL);
110 bindSampler(NULL, ct, NULL);
111 }
112 return false;
113}
114
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800115void Program::initMemberVars() {
116 mDirty = true;
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800117
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -0700118 mHal.drv = NULL;
119 mHal.state.textures = NULL;
120 mHal.state.samplers = NULL;
121 mHal.state.textureTargets = NULL;
122 mHal.state.inputElements = NULL;
123 mHal.state.constantTypes = NULL;
124 mHal.state.constants = NULL;
125
126 mHal.state.inputElementsCount = 0;
127 mHal.state.constantsCount = 0;
128 mHal.state.texturesCount = 0;
129
Alex Sakhartchouk064aa7e2011-10-18 10:54:29 -0700130 mTextures = NULL;
131 mSamplers = NULL;
132 mInputElements = NULL;
133 mConstantTypes = NULL;
134 mConstants = NULL;
135
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800136 mIsInternal = false;
137}
Jason Sams326e0dd2009-05-22 14:03:28 -0700138
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800139void Program::bindAllocation(Context *rsc, Allocation *alloc, uint32_t slot) {
Alex Sakhartchouk383e5b12010-09-23 16:16:33 -0700140 if (alloc != NULL) {
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -0700141 if (slot >= mHal.state.constantsCount) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000142 ALOGE("Attempt to bind alloc at slot %u, on shader id %u, but const count is %u",
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -0700143 slot, (uint32_t)this, mHal.state.constantsCount);
Alex Sakhartchouk383e5b12010-09-23 16:16:33 -0700144 rsc->setError(RS_ERROR_BAD_SHADER, "Cannot bind allocation");
145 return;
146 }
Alex Sakhartchouk064aa7e2011-10-18 10:54:29 -0700147 if (alloc->getType() != mConstantTypes[slot].get()) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000148 ALOGE("Attempt to bind alloc at slot %u, on shader id %u, but types mismatch",
Alex Sakhartchouk383e5b12010-09-23 16:16:33 -0700149 slot, (uint32_t)this);
150 rsc->setError(RS_ERROR_BAD_SHADER, "Cannot bind allocation");
151 return;
152 }
153 }
Alex Sakhartchouk064aa7e2011-10-18 10:54:29 -0700154 if (mConstants[slot].get() == alloc) {
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700155 return;
156 }
Alex Sakhartchouk064aa7e2011-10-18 10:54:29 -0700157 if (mConstants[slot].get()) {
158 mConstants[slot]->removeProgramToDirty(this);
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700159 }
Alex Sakhartchouk064aa7e2011-10-18 10:54:29 -0700160 mConstants[slot].set(alloc);
161 mHal.state.constants[slot] = alloc;
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700162 if (alloc) {
163 alloc->addProgramToDirty(this);
164 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700165 mDirty = true;
166}
167
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800168void Program::bindTexture(Context *rsc, uint32_t slot, Allocation *a) {
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -0700169 if (slot >= mHal.state.texturesCount) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000170 ALOGE("Attempt to bind texture to slot %u but tex count is %u", slot, mHal.state.texturesCount);
Alex Sakhartchouk383e5b12010-09-23 16:16:33 -0700171 rsc->setError(RS_ERROR_BAD_SHADER, "Cannot bind texture");
Jason Sams7dad9c32009-12-17 16:55:08 -0800172 return;
173 }
174
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -0700175 if (a && a->getType()->getDimFaces() && mHal.state.textureTargets[slot] != RS_TEXTURE_CUBE) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000176 ALOGE("Attempt to bind cubemap to slot %u but 2d texture needed", slot);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800177 rsc->setError(RS_ERROR_BAD_SHADER, "Cannot bind cubemap to 2d texture slot");
178 return;
179 }
180
Alex Sakhartchouk064aa7e2011-10-18 10:54:29 -0700181 mTextures[slot].set(a);
182 mHal.state.textures[slot] = a;
183
Jason Sams7dad9c32009-12-17 16:55:08 -0800184 mDirty = true;
185}
186
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800187void Program::bindSampler(Context *rsc, uint32_t slot, Sampler *s) {
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -0700188 if (slot >= mHal.state.texturesCount) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000189 ALOGE("Attempt to bind sampler to slot %u but tex count is %u", slot, mHal.state.texturesCount);
Alex Sakhartchouk383e5b12010-09-23 16:16:33 -0700190 rsc->setError(RS_ERROR_BAD_SHADER, "Cannot bind sampler");
Jason Sams7dad9c32009-12-17 16:55:08 -0800191 return;
192 }
193
Alex Sakhartchouk064aa7e2011-10-18 10:54:29 -0700194 mSamplers[slot].set(s);
195 mHal.state.samplers[slot] = s;
Jason Sams7dad9c32009-12-17 16:55:08 -0800196 mDirty = true;
197}
198
Jason Sams4815c0d2009-12-15 12:58:36 -0800199namespace android {
200namespace renderscript {
201
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800202void rsi_ProgramBindConstants(Context *rsc, RsProgram vp, uint32_t slot, RsAllocation constants) {
Jason Sams4815c0d2009-12-15 12:58:36 -0800203 Program *p = static_cast<Program *>(vp);
Alex Sakhartchouk383e5b12010-09-23 16:16:33 -0700204 p->bindAllocation(rsc, static_cast<Allocation *>(constants), slot);
Jason Sams4815c0d2009-12-15 12:58:36 -0800205}
206
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800207void rsi_ProgramBindTexture(Context *rsc, RsProgram vpf, uint32_t slot, RsAllocation a) {
Jason Sams7dad9c32009-12-17 16:55:08 -0800208 Program *p = static_cast<Program *>(vpf);
Alex Sakhartchouk383e5b12010-09-23 16:16:33 -0700209 p->bindTexture(rsc, slot, static_cast<Allocation *>(a));
Jason Sams7dad9c32009-12-17 16:55:08 -0800210}
211
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800212void rsi_ProgramBindSampler(Context *rsc, RsProgram vpf, uint32_t slot, RsSampler s) {
Jason Sams7dad9c32009-12-17 16:55:08 -0800213 Program *p = static_cast<Program *>(vpf);
Alex Sakhartchouk383e5b12010-09-23 16:16:33 -0700214 p->bindSampler(rsc, slot, static_cast<Sampler *>(s));
Jason Sams7dad9c32009-12-17 16:55:08 -0800215}
Jason Sams4815c0d2009-12-15 12:58:36 -0800216
217}
218}
219