blob: 1c44e71051add559e0be999e4347b4f75289d6a6 [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
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070017#ifndef ANDROID_RS_BUILD_FOR_HOST
Jason Sams326e0dd2009-05-22 14:03:28 -070018#include "rsContext.h"
Jason Samsc460e552009-11-25 13:22:07 -080019#include <GLES2/gl2.h>
20#include <GLES2/gl2ext.h>
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070021#else
22#include "rsContextHostStub.h"
23#include <OpenGL/gl.h>
24#include <OpenGL/glext.h>
25#endif //ANDROID_RS_BUILD_FOR_HOST
26
27#include "rsProgram.h"
Jason Samsc460e552009-11-25 13:22:07 -080028
Jason Sams326e0dd2009-05-22 14:03:28 -070029using namespace android;
30using namespace android::renderscript;
31
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080032Program::Program(Context *rsc) : ObjectBase(rsc) {
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -080033 initMemberVars();
Jason Sams4815c0d2009-12-15 12:58:36 -080034}
35
36Program::Program(Context *rsc, const char * shaderText, uint32_t shaderLength,
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080037 const uint32_t * params, uint32_t paramLength)
38 : ObjectBase(rsc) {
Jason Sams4815c0d2009-12-15 12:58:36 -080039
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080040 initMemberVars();
Jason Sams4815c0d2009-12-15 12:58:36 -080041 for (uint32_t ct=0; ct < paramLength; ct+=2) {
42 if (params[ct] == RS_PROGRAM_PARAM_INPUT) {
43 mInputCount++;
44 }
45 if (params[ct] == RS_PROGRAM_PARAM_OUTPUT) {
46 mOutputCount++;
47 }
48 if (params[ct] == RS_PROGRAM_PARAM_CONSTANT) {
49 mConstantCount++;
50 }
Jason Samsf2e4fa22009-12-15 13:27:04 -080051 if (params[ct] == RS_PROGRAM_PARAM_TEXTURE_COUNT) {
52 mTextureCount = params[ct+1];
53 }
Jason Sams4815c0d2009-12-15 12:58:36 -080054 }
55
Alex Sakhartchouk383e5b12010-09-23 16:16:33 -070056 mTextures = new ObjectBaseRef<Allocation>[mTextureCount];
57 mSamplers = new ObjectBaseRef<Sampler>[mTextureCount];
Jason Sams4815c0d2009-12-15 12:58:36 -080058 mInputElements = new ObjectBaseRef<Element>[mInputCount];
59 mOutputElements = new ObjectBaseRef<Element>[mOutputCount];
60 mConstantTypes = new ObjectBaseRef<Type>[mConstantCount];
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -080061 mConstants = new ObjectBaseRef<Allocation>[mConstantCount];
Jason Sams4815c0d2009-12-15 12:58:36 -080062
63 uint32_t input = 0;
64 uint32_t output = 0;
65 uint32_t constant = 0;
66 for (uint32_t ct=0; ct < paramLength; ct+=2) {
67 if (params[ct] == RS_PROGRAM_PARAM_INPUT) {
68 mInputElements[input++].set(reinterpret_cast<Element *>(params[ct+1]));
69 }
70 if (params[ct] == RS_PROGRAM_PARAM_OUTPUT) {
71 mOutputElements[output++].set(reinterpret_cast<Element *>(params[ct+1]));
72 }
73 if (params[ct] == RS_PROGRAM_PARAM_CONSTANT) {
74 mConstantTypes[constant++].set(reinterpret_cast<Type *>(params[ct+1]));
75 }
76 }
Alex Sakhartchouke7ae69f2010-09-14 09:50:43 -070077 mIsInternal = false;
78 uint32_t internalTokenLen = strlen(RS_SHADER_INTERNAL);
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080079 if (shaderLength > internalTokenLen &&
Alex Sakhartchouke7ae69f2010-09-14 09:50:43 -070080 strncmp(RS_SHADER_INTERNAL, shaderText, internalTokenLen) == 0) {
81 mIsInternal = true;
82 shaderText += internalTokenLen;
83 shaderLength -= internalTokenLen;
84 }
Jason Sams4815c0d2009-12-15 12:58:36 -080085 mUserShader.setTo(shaderText, shaderLength);
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -080086
87 initAttribAndUniformArray();
Jason Sams326e0dd2009-05-22 14:03:28 -070088}
89
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080090Program::~Program() {
91 if (mRSC->props.mLogShaders) {
Alex Sakhartchouk889fe502010-10-01 10:54:06 -070092 LOGV("Program::~Program with shader id %u", mShaderID);
93 }
94
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080095 if (mShaderID) {
Alex Sakhartchouk889fe502010-10-01 10:54:06 -070096 glDeleteShader(mShaderID);
97 }
98
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -080099 for (uint32_t ct=0; ct < mConstantCount; ct++) {
Alex Sakhartchouk383e5b12010-09-23 16:16:33 -0700100 bindAllocation(NULL, NULL, ct);
Jason Sams9ebb0c42010-01-12 12:12:28 -0800101 }
Jason Sams4815c0d2009-12-15 12:58:36 -0800102
Alex Sakhartchouk383e5b12010-09-23 16:16:33 -0700103 for (uint32_t ct=0; ct < mTextureCount; ct++) {
104 bindTexture(NULL, ct, NULL);
105 bindSampler(NULL, ct, NULL);
106 }
107 delete[] mTextures;
108 delete[] mSamplers;
Jason Sams4815c0d2009-12-15 12:58:36 -0800109 delete[] mInputElements;
110 delete[] mOutputElements;
111 delete[] mConstantTypes;
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800112 delete[] mConstants;
113 delete[] mAttribNames;
114 delete[] mUniformNames;
115 delete[] mUniformArraySizes;
Jason Sams4815c0d2009-12-15 12:58:36 -0800116 mInputCount = 0;
117 mOutputCount = 0;
118 mConstantCount = 0;
Jason Sams326e0dd2009-05-22 14:03:28 -0700119}
120
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800121void Program::initMemberVars() {
122 mDirty = true;
123 mShaderID = 0;
124 mAttribCount = 0;
125 mUniformCount = 0;
126 mTextureCount = 0;
127
128 mTextures = NULL;
129 mSamplers = NULL;
130 mInputElements = NULL;
131 mOutputElements = NULL;
132 mConstantTypes = NULL;
133 mConstants = NULL;
134 mAttribNames = NULL;
135 mUniformNames = NULL;
136 mUniformArraySizes = NULL;
137 mInputCount = 0;
138 mOutputCount = 0;
139 mConstantCount = 0;
140 mIsValid = false;
141 mIsInternal = false;
142}
Jason Sams326e0dd2009-05-22 14:03:28 -0700143
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800144void Program::bindAllocation(Context *rsc, Allocation *alloc, uint32_t slot) {
Alex Sakhartchouk383e5b12010-09-23 16:16:33 -0700145 if (alloc != NULL) {
146 if (slot >= mConstantCount) {
147 LOGE("Attempt to bind alloc at slot %u, on shader id %u, but const count is %u",
148 slot, (uint32_t)this, mConstantCount);
149 rsc->setError(RS_ERROR_BAD_SHADER, "Cannot bind allocation");
150 return;
151 }
152 if (!alloc->getType()->isEqual(mConstantTypes[slot].get())) {
153 LOGE("Attempt to bind alloc at slot %u, on shader id %u, but types mismatch",
154 slot, (uint32_t)this);
155 rsc->setError(RS_ERROR_BAD_SHADER, "Cannot bind allocation");
156 return;
157 }
158 }
Jason Sams9ebb0c42010-01-12 12:12:28 -0800159 if (mConstants[slot].get() == alloc) {
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700160 return;
161 }
Jason Sams9ebb0c42010-01-12 12:12:28 -0800162 if (mConstants[slot].get()) {
163 mConstants[slot].get()->removeProgramToDirty(this);
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700164 }
Jason Sams9ebb0c42010-01-12 12:12:28 -0800165 mConstants[slot].set(alloc);
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700166 if (alloc) {
167 alloc->addProgramToDirty(this);
168 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700169 mDirty = true;
170}
171
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800172void Program::bindTexture(Context *rsc, uint32_t slot, Allocation *a) {
Alex Sakhartchouk383e5b12010-09-23 16:16:33 -0700173 if (slot >= mTextureCount) {
174 LOGE("Attempt to bind texture to slot %u but tex count is %u", slot, mTextureCount);
175 rsc->setError(RS_ERROR_BAD_SHADER, "Cannot bind texture");
Jason Sams7dad9c32009-12-17 16:55:08 -0800176 return;
177 }
178
179 //LOGE("bindtex %i %p", slot, a);
180 mTextures[slot].set(a);
181 mDirty = true;
182}
183
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800184void Program::bindSampler(Context *rsc, uint32_t slot, Sampler *s) {
Alex Sakhartchouk383e5b12010-09-23 16:16:33 -0700185 if (slot >= mTextureCount) {
186 LOGE("Attempt to bind sampler to slot %u but tex count is %u", slot, mTextureCount);
187 rsc->setError(RS_ERROR_BAD_SHADER, "Cannot bind sampler");
Jason Sams7dad9c32009-12-17 16:55:08 -0800188 return;
189 }
190
191 mSamplers[slot].set(s);
192 mDirty = true;
193}
194
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800195String8 Program::getGLSLInputString() const {
Jason Samsb4d35682010-01-04 16:52:27 -0800196 String8 s;
197 for (uint32_t ct=0; ct < mInputCount; ct++) {
198 const Element *e = mInputElements[ct].get();
199 for (uint32_t field=0; field < e->getFieldCount(); field++) {
200 const Element *f = e->getField(field);
201
202 // Cannot be complex
203 rsAssert(!f->getFieldCount());
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800204 switch (f->getComponent().getVectorSize()) {
Jason Samsb4d35682010-01-04 16:52:27 -0800205 case 1: s.append("attribute float ATTRIB_"); break;
206 case 2: s.append("attribute vec2 ATTRIB_"); break;
207 case 3: s.append("attribute vec3 ATTRIB_"); break;
208 case 4: s.append("attribute vec4 ATTRIB_"); break;
209 default:
210 rsAssert(0);
211 }
212
213 s.append(e->getFieldName(field));
214 s.append(";\n");
215 }
216 }
217 return s;
218}
219
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800220String8 Program::getGLSLOutputString() const {
Jason Samsb4d35682010-01-04 16:52:27 -0800221 return String8();
222}
223
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800224String8 Program::getGLSLConstantString() const {
Jason Samsb4d35682010-01-04 16:52:27 -0800225 return String8();
226}
227
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800228void Program::createShader() {
Jason Samsc460e552009-11-25 13:22:07 -0800229}
Jason Samscfb1d112009-08-05 13:57:03 -0700230
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800231bool Program::loadShader(Context *rsc, uint32_t type) {
Jason Samsc460e552009-11-25 13:22:07 -0800232 mShaderID = glCreateShader(type);
233 rsAssert(mShaderID);
234
Jason Samscd506532009-12-15 19:10:11 -0800235 if (rsc->props.mLogShaders) {
236 LOGV("Loading shader type %x, ID %i", type, mShaderID);
Nick Kralevich8492a702010-05-13 14:46:27 -0700237 LOGV("%s", mShader.string());
Jason Samscd506532009-12-15 19:10:11 -0800238 }
Jason Samsc460e552009-11-25 13:22:07 -0800239
240 if (mShaderID) {
241 const char * ss = mShader.string();
242 glShaderSource(mShaderID, 1, &ss, NULL);
243 glCompileShader(mShaderID);
244
245 GLint compiled = 0;
246 glGetShaderiv(mShaderID, GL_COMPILE_STATUS, &compiled);
247 if (!compiled) {
248 GLint infoLen = 0;
249 glGetShaderiv(mShaderID, GL_INFO_LOG_LENGTH, &infoLen);
250 if (infoLen) {
251 char* buf = (char*) malloc(infoLen);
252 if (buf) {
253 glGetShaderInfoLog(mShaderID, infoLen, NULL, buf);
254 LOGE("Could not compile shader \n%s\n", buf);
255 free(buf);
256 }
257 glDeleteShader(mShaderID);
258 mShaderID = 0;
Jason Samsa2cf7552010-03-03 13:03:18 -0800259 rsc->setError(RS_ERROR_BAD_SHADER, "Error returned from GL driver loading shader text,");
Jason Samsc460e552009-11-25 13:22:07 -0800260 return false;
261 }
262 }
263 }
Jason Samscd506532009-12-15 19:10:11 -0800264
265 if (rsc->props.mLogShaders) {
266 LOGV("--Shader load result %x ", glGetError());
267 }
Jason Samsa2cf7552010-03-03 13:03:18 -0800268 mIsValid = true;
Jason Samsc460e552009-11-25 13:22:07 -0800269 return true;
270}
Jason Samsf2a5d732009-11-30 14:49:55 -0800271
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800272void Program::setShader(const char *txt, uint32_t len) {
Jason Samsf2a5d732009-11-30 14:49:55 -0800273 mUserShader.setTo(txt, len);
274}
275
Alex Sakhartchouk6e934212010-08-31 12:02:01 -0700276void Program::appendUserConstants() {
277 for (uint32_t ct=0; ct < mConstantCount; ct++) {
278 const Element *e = mConstantTypes[ct]->getElement();
279 for (uint32_t field=0; field < e->getFieldCount(); field++) {
280 const Element *f = e->getField(field);
281 const char *fn = e->getFieldName(field);
Jason Sams4815c0d2009-12-15 12:58:36 -0800282
Alex Sakhartchouk6e934212010-08-31 12:02:01 -0700283 if (fn[0] == '#') {
284 continue;
285 }
286
287 // Cannot be complex
288 rsAssert(!f->getFieldCount());
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800289 if (f->getType() == RS_TYPE_MATRIX_4X4) {
Alex Sakhartchouk6e934212010-08-31 12:02:01 -0700290 mShader.append("uniform mat4 UNI_");
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800291 } else if (f->getType() == RS_TYPE_MATRIX_3X3) {
Alex Sakhartchouk6e934212010-08-31 12:02:01 -0700292 mShader.append("uniform mat3 UNI_");
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800293 } else if (f->getType() == RS_TYPE_MATRIX_2X2) {
Alex Sakhartchouk6e934212010-08-31 12:02:01 -0700294 mShader.append("uniform mat2 UNI_");
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800295 } else {
296 switch (f->getComponent().getVectorSize()) {
Alex Sakhartchouk6e934212010-08-31 12:02:01 -0700297 case 1: mShader.append("uniform float UNI_"); break;
298 case 2: mShader.append("uniform vec2 UNI_"); break;
299 case 3: mShader.append("uniform vec3 UNI_"); break;
300 case 4: mShader.append("uniform vec4 UNI_"); break;
301 default:
302 rsAssert(0);
303 }
304 }
305
306 mShader.append(fn);
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800307 if (e->getFieldArraySize(field) > 1) {
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800308 mShader.appendFormat("[%d]", e->getFieldArraySize(field));
309 }
Alex Sakhartchouk6e934212010-08-31 12:02:01 -0700310 mShader.append(";\n");
311 }
312 }
313}
314
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800315void Program::logUniform(const Element *field, const float *fd, uint32_t arraySize ) {
316 RsDataType dataType = field->getType();
317 uint32_t elementSize = field->getSizeBytes() / sizeof(float);
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800318 for (uint32_t i = 0; i < arraySize; i ++) {
319 if (arraySize > 1) {
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800320 LOGV("Array Element [%u]", i);
321 }
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800322 if (dataType == RS_TYPE_MATRIX_4X4) {
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800323 LOGV("Matrix4x4");
324 LOGV("{%f, %f, %f, %f", fd[0], fd[4], fd[8], fd[12]);
325 LOGV(" %f, %f, %f, %f", fd[1], fd[5], fd[9], fd[13]);
326 LOGV(" %f, %f, %f, %f", fd[2], fd[6], fd[10], fd[14]);
327 LOGV(" %f, %f, %f, %f}", fd[3], fd[7], fd[11], fd[15]);
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800328 } else if (dataType == RS_TYPE_MATRIX_3X3) {
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800329 LOGV("Matrix3x3");
330 LOGV("{%f, %f, %f", fd[0], fd[3], fd[6]);
331 LOGV(" %f, %f, %f", fd[1], fd[4], fd[7]);
332 LOGV(" %f, %f, %f}", fd[2], fd[5], fd[8]);
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800333 } else if (dataType == RS_TYPE_MATRIX_2X2) {
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800334 LOGV("Matrix2x2");
335 LOGV("{%f, %f", fd[0], fd[2]);
336 LOGV(" %f, %f}", fd[1], fd[3]);
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800337 } else {
338 switch (field->getComponent().getVectorSize()) {
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800339 case 1:
340 LOGV("Uniform 1 = %f", fd[0]);
341 break;
342 case 2:
343 LOGV("Uniform 2 = %f %f", fd[0], fd[1]);
344 break;
345 case 3:
346 LOGV("Uniform 3 = %f %f %f", fd[0], fd[1], fd[2]);
347 break;
348 case 4:
349 LOGV("Uniform 4 = %f %f %f %f", fd[0], fd[1], fd[2], fd[3]);
350 break;
351 default:
352 rsAssert(0);
353 }
354 }
355 LOGE("Element size %u data=%p", elementSize, fd);
356 fd += elementSize;
357 LOGE("New data=%p", fd);
358 }
359}
360
361void Program::setUniform(Context *rsc, const Element *field, const float *fd,
362 int32_t slot, uint32_t arraySize ) {
363 RsDataType dataType = field->getType();
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800364 if (dataType == RS_TYPE_MATRIX_4X4) {
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800365 glUniformMatrix4fv(slot, arraySize, GL_FALSE, fd);
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800366 } else if (dataType == RS_TYPE_MATRIX_3X3) {
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800367 glUniformMatrix3fv(slot, arraySize, GL_FALSE, fd);
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800368 } else if (dataType == RS_TYPE_MATRIX_2X2) {
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800369 glUniformMatrix2fv(slot, arraySize, GL_FALSE, fd);
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800370 } else {
371 switch (field->getComponent().getVectorSize()) {
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800372 case 1:
373 glUniform1fv(slot, arraySize, fd);
374 break;
375 case 2:
376 glUniform2fv(slot, arraySize, fd);
377 break;
378 case 3:
379 glUniform3fv(slot, arraySize, fd);
380 break;
381 case 4:
382 glUniform4fv(slot, arraySize, fd);
383 break;
384 default:
385 rsAssert(0);
386 }
387 }
388}
389
Alex Sakhartchouk383e5b12010-09-23 16:16:33 -0700390void Program::setupUserConstants(Context *rsc, ShaderCache *sc, bool isFragment) {
Alex Sakhartchouke7ae69f2010-09-14 09:50:43 -0700391 uint32_t uidx = 0;
Alex Sakhartchouk6e934212010-08-31 12:02:01 -0700392 for (uint32_t ct=0; ct < mConstantCount; ct++) {
Alex Sakhartchouke7ae69f2010-09-14 09:50:43 -0700393 Allocation *alloc = mConstants[ct].get();
Alex Sakhartchouk6e934212010-08-31 12:02:01 -0700394 if (!alloc) {
Alex Sakhartchouk383e5b12010-09-23 16:16:33 -0700395 LOGE("Attempting to set constants on shader id %u, but alloc at slot %u is not set", (uint32_t)this, ct);
396 rsc->setError(RS_ERROR_BAD_SHADER, "No constant allocation bound");
Alex Sakhartchouk6e934212010-08-31 12:02:01 -0700397 continue;
398 }
399
400 const uint8_t *data = static_cast<const uint8_t *>(alloc->getPtr());
401 const Element *e = mConstantTypes[ct]->getElement();
402 for (uint32_t field=0; field < e->getFieldCount(); field++) {
403 const Element *f = e->getField(field);
404 const char *fieldName = e->getFieldName(field);
405 // If this field is padding, skip it
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800406 if (fieldName[0] == '#') {
Alex Sakhartchouk6e934212010-08-31 12:02:01 -0700407 continue;
408 }
409
410 uint32_t offset = e->getFieldOffsetBytes(field);
411 const float *fd = reinterpret_cast<const float *>(&data[offset]);
412
413 int32_t slot = -1;
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800414 uint32_t arraySize = 1;
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800415 if (!isFragment) {
Alex Sakhartchouk6e934212010-08-31 12:02:01 -0700416 slot = sc->vtxUniformSlot(uidx);
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800417 arraySize = sc->vtxUniformSize(uidx);
418 } else {
Alex Sakhartchouk6e934212010-08-31 12:02:01 -0700419 slot = sc->fragUniformSlot(uidx);
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800420 arraySize = sc->fragUniformSize(uidx);
Alex Sakhartchouk6e934212010-08-31 12:02:01 -0700421 }
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800422 if (rsc->props.mLogShadersUniforms) {
Alex Sakhartchouk886f11a2010-09-29 09:49:13 -0700423 LOGV("Uniform slot=%i, offset=%i, constant=%i, field=%i, uidx=%i, name=%s", slot, offset, ct, field, uidx, fieldName);
424 }
Alex Sakhartchouk6e934212010-08-31 12:02:01 -0700425 uidx ++;
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800426 if (slot < 0) {
427 continue;
428 }
429
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800430 if (rsc->props.mLogShadersUniforms) {
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800431 logUniform(f, fd, arraySize);
432 }
433 setUniform(rsc, f, fd, slot, arraySize);
Alex Sakhartchouk6e934212010-08-31 12:02:01 -0700434 }
435 }
436}
437
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800438void Program::initAttribAndUniformArray() {
439 mAttribCount = 0;
440 for (uint32_t ct=0; ct < mInputCount; ct++) {
441 const Element *elem = mInputElements[ct].get();
442 for (uint32_t field=0; field < elem->getFieldCount(); field++) {
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800443 if (elem->getFieldName(field)[0] != '#') {
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800444 mAttribCount ++;
445 }
446 }
447 }
448
449 mUniformCount = 0;
450 for (uint32_t ct=0; ct < mConstantCount; ct++) {
451 const Element *elem = mConstantTypes[ct]->getElement();
452
453 for (uint32_t field=0; field < elem->getFieldCount(); field++) {
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800454 if (elem->getFieldName(field)[0] != '#') {
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800455 mUniformCount ++;
456 }
457 }
458 }
459 mUniformCount += mTextureCount;
460
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800461 if (mAttribCount) {
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800462 mAttribNames = new String8[mAttribCount];
463 }
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800464 if (mUniformCount) {
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800465 mUniformNames = new String8[mUniformCount];
466 mUniformArraySizes = new uint32_t[mUniformCount];
467 }
468}
469
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800470void Program::initAddUserElement(const Element *e, String8 *names, uint32_t *arrayLengths, uint32_t *count, const char *prefix) {
Alex Sakhartchouk6e934212010-08-31 12:02:01 -0700471 rsAssert(e->getFieldCount());
472 for (uint32_t ct=0; ct < e->getFieldCount(); ct++) {
473 const Element *ce = e->getField(ct);
474 if (ce->getFieldCount()) {
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800475 initAddUserElement(ce, names, arrayLengths, count, prefix);
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800476 } else if (e->getFieldName(ct)[0] != '#') {
Alex Sakhartchouk6e934212010-08-31 12:02:01 -0700477 String8 tmp(prefix);
478 tmp.append(e->getFieldName(ct));
479 names[*count].setTo(tmp.string());
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800480 if (arrayLengths) {
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800481 arrayLengths[*count] = e->getFieldArraySize(ct);
482 }
Alex Sakhartchouk6e934212010-08-31 12:02:01 -0700483 (*count)++;
484 }
485 }
486}
Jason Sams4815c0d2009-12-15 12:58:36 -0800487
488namespace android {
489namespace renderscript {
490
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800491void rsi_ProgramBindConstants(Context *rsc, RsProgram vp, uint32_t slot, RsAllocation constants) {
Jason Sams4815c0d2009-12-15 12:58:36 -0800492 Program *p = static_cast<Program *>(vp);
Alex Sakhartchouk383e5b12010-09-23 16:16:33 -0700493 p->bindAllocation(rsc, static_cast<Allocation *>(constants), slot);
Jason Sams4815c0d2009-12-15 12:58:36 -0800494}
495
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800496void rsi_ProgramBindTexture(Context *rsc, RsProgram vpf, uint32_t slot, RsAllocation a) {
Jason Sams7dad9c32009-12-17 16:55:08 -0800497 Program *p = static_cast<Program *>(vpf);
Alex Sakhartchouk383e5b12010-09-23 16:16:33 -0700498 p->bindTexture(rsc, slot, static_cast<Allocation *>(a));
Jason Sams7dad9c32009-12-17 16:55:08 -0800499}
500
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800501void rsi_ProgramBindSampler(Context *rsc, RsProgram vpf, uint32_t slot, RsSampler s) {
Jason Sams7dad9c32009-12-17 16:55:08 -0800502 Program *p = static_cast<Program *>(vpf);
Alex Sakhartchouk383e5b12010-09-23 16:16:33 -0700503 p->bindSampler(rsc, slot, static_cast<Sampler *>(s));
Jason Sams7dad9c32009-12-17 16:55:08 -0800504}
Jason Sams4815c0d2009-12-15 12:58:36 -0800505
506}
507}
508