blob: dc7f8abb889b3457750866db0584d645d2f3e68c [file] [log] [blame]
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -07001/*
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 <GLES2/gl2.h>
18#include <GLES2/gl2ext.h>
19
20#include <rs_hal.h>
21#include <rsContext.h>
22#include <rsProgram.h>
23
Alex Sakhartchouk7f126c72011-05-05 16:56:27 -070024#include "rsdCore.h"
Jason Samseb4fe182011-05-26 16:33:01 -070025#include "rsdAllocation.h"
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -070026#include "rsdShader.h"
27#include "rsdShaderCache.h"
28
29using namespace android;
30using namespace android::renderscript;
31
32RsdShader::RsdShader(const Program *p, uint32_t type,
33 const char * shaderText, uint32_t shaderLength) {
34
35 mUserShader.setTo(shaderText, shaderLength);
36 mRSProgram = p;
37 mType = type;
38 initMemberVars();
39 initAttribAndUniformArray();
40 init();
41}
42
43RsdShader::~RsdShader() {
44 if (mShaderID) {
45 glDeleteShader(mShaderID);
46 }
47
48 delete[] mAttribNames;
49 delete[] mUniformNames;
50 delete[] mUniformArraySizes;
Alex Sakhartchoukba157302011-08-05 15:27:25 -070051 delete[] mTextureTargets;
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -070052}
53
54void RsdShader::initMemberVars() {
55 mDirty = true;
56 mShaderID = 0;
57 mAttribCount = 0;
58 mUniformCount = 0;
59
60 mAttribNames = NULL;
61 mUniformNames = NULL;
62 mUniformArraySizes = NULL;
Alex Sakhartchoukba157302011-08-05 15:27:25 -070063 mTextureTargets = NULL;
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -070064
65 mIsValid = false;
66}
67
68void RsdShader::init() {
69 uint32_t attribCount = 0;
70 uint32_t uniformCount = 0;
71 for (uint32_t ct=0; ct < mRSProgram->mHal.state.inputElementsCount; ct++) {
Alex Sakhartchouk064aa7e2011-10-18 10:54:29 -070072 initAddUserElement(mRSProgram->mHal.state.inputElements[ct], mAttribNames, NULL, &attribCount, RS_SHADER_ATTR);
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -070073 }
74 for (uint32_t ct=0; ct < mRSProgram->mHal.state.constantsCount; ct++) {
75 initAddUserElement(mRSProgram->mHal.state.constantTypes[ct]->getElement(), mUniformNames, mUniformArraySizes, &uniformCount, RS_SHADER_UNI);
76 }
77
78 mTextureUniformIndexStart = uniformCount;
79 char buf[256];
80 for (uint32_t ct=0; ct < mRSProgram->mHal.state.texturesCount; ct++) {
81 snprintf(buf, sizeof(buf), "UNI_Tex%i", ct);
82 mUniformNames[uniformCount].setTo(buf);
83 mUniformArraySizes[uniformCount] = 1;
84 uniformCount++;
85 }
Alex Sakhartchoukba157302011-08-05 15:27:25 -070086
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -070087}
88
89String8 RsdShader::getGLSLInputString() const {
90 String8 s;
91 for (uint32_t ct=0; ct < mRSProgram->mHal.state.inputElementsCount; ct++) {
Alex Sakhartchouk064aa7e2011-10-18 10:54:29 -070092 const Element *e = mRSProgram->mHal.state.inputElements[ct];
Alex Sakhartchouk4a43e3e2011-12-28 13:47:05 -080093 for (uint32_t field=0; field < e->mHal.state.fieldsCount; field++) {
94 const Element *f = e->mHal.state.fields[field];
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -070095
96 // Cannot be complex
Alex Sakhartchouk4a43e3e2011-12-28 13:47:05 -080097 rsAssert(!f->mHal.state.fieldsCount);
98 switch (f->mHal.state.vectorSize) {
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -070099 case 1: s.append("attribute float ATTRIB_"); break;
100 case 2: s.append("attribute vec2 ATTRIB_"); break;
101 case 3: s.append("attribute vec3 ATTRIB_"); break;
102 case 4: s.append("attribute vec4 ATTRIB_"); break;
103 default:
104 rsAssert(0);
105 }
106
Alex Sakhartchouk4a43e3e2011-12-28 13:47:05 -0800107 s.append(e->mHal.state.fieldNames[field]);
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -0700108 s.append(";\n");
109 }
110 }
111 return s;
112}
113
114void RsdShader::appendAttributes() {
115 for (uint32_t ct=0; ct < mRSProgram->mHal.state.inputElementsCount; ct++) {
Alex Sakhartchouk064aa7e2011-10-18 10:54:29 -0700116 const Element *e = mRSProgram->mHal.state.inputElements[ct];
Alex Sakhartchouk4a43e3e2011-12-28 13:47:05 -0800117 for (uint32_t field=0; field < e->mHal.state.fieldsCount; field++) {
118 const Element *f = e->mHal.state.fields[field];
119 const char *fn = e->mHal.state.fieldNames[field];
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -0700120
121 // Cannot be complex
Alex Sakhartchouk4a43e3e2011-12-28 13:47:05 -0800122 rsAssert(!f->mHal.state.fieldsCount);
123 switch (f->mHal.state.vectorSize) {
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -0700124 case 1: mShader.append("attribute float ATTRIB_"); break;
125 case 2: mShader.append("attribute vec2 ATTRIB_"); break;
126 case 3: mShader.append("attribute vec3 ATTRIB_"); break;
127 case 4: mShader.append("attribute vec4 ATTRIB_"); break;
128 default:
129 rsAssert(0);
130 }
131
132 mShader.append(fn);
133 mShader.append(";\n");
134 }
135 }
136}
137
138void RsdShader::appendTextures() {
139 char buf[256];
140 for (uint32_t ct=0; ct < mRSProgram->mHal.state.texturesCount; ct++) {
141 if (mRSProgram->mHal.state.textureTargets[ct] == RS_TEXTURE_2D) {
Jason Sams41e373d2012-01-13 14:01:20 -0800142 Allocation *a = mRSProgram->mHal.state.textures[ct];
143 if (a && a->mHal.state.surfaceTextureID) {
144 snprintf(buf, sizeof(buf), "uniform samplerExternalOES UNI_Tex%i;\n", ct);
145 } else {
146 snprintf(buf, sizeof(buf), "uniform sampler2D UNI_Tex%i;\n", ct);
147 }
Alex Sakhartchoukba157302011-08-05 15:27:25 -0700148 mTextureTargets[ct] = GL_TEXTURE_2D;
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -0700149 } else {
150 snprintf(buf, sizeof(buf), "uniform samplerCube UNI_Tex%i;\n", ct);
Alex Sakhartchoukba157302011-08-05 15:27:25 -0700151 mTextureTargets[ct] = GL_TEXTURE_CUBE_MAP;
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -0700152 }
153 mShader.append(buf);
154 }
155}
156
157bool RsdShader::createShader() {
158
159 if (mType == GL_FRAGMENT_SHADER) {
160 mShader.append("precision mediump float;\n");
161 }
162 appendUserConstants();
163 appendAttributes();
164 appendTextures();
165
166 mShader.append(mUserShader);
167
168 return true;
169}
170
171bool RsdShader::loadShader(const Context *rsc) {
172 mShaderID = glCreateShader(mType);
173 rsAssert(mShaderID);
174
175 if (rsc->props.mLogShaders) {
Steve Block65982012011-10-20 11:56:00 +0100176 ALOGV("Loading shader type %x, ID %i", mType, mShaderID);
177 ALOGV("%s", mShader.string());
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -0700178 }
179
180 if (mShaderID) {
181 const char * ss = mShader.string();
Jason Sams2382aba2011-09-13 15:41:01 -0700182 RSD_CALL_GL(glShaderSource, mShaderID, 1, &ss, NULL);
183 RSD_CALL_GL(glCompileShader, mShaderID);
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -0700184
185 GLint compiled = 0;
Jason Sams2382aba2011-09-13 15:41:01 -0700186 RSD_CALL_GL(glGetShaderiv, mShaderID, GL_COMPILE_STATUS, &compiled);
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -0700187 if (!compiled) {
188 GLint infoLen = 0;
Jason Sams2382aba2011-09-13 15:41:01 -0700189 RSD_CALL_GL(glGetShaderiv, mShaderID, GL_INFO_LOG_LENGTH, &infoLen);
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -0700190 if (infoLen) {
191 char* buf = (char*) malloc(infoLen);
192 if (buf) {
Jason Sams2382aba2011-09-13 15:41:01 -0700193 RSD_CALL_GL(glGetShaderInfoLog, mShaderID, infoLen, NULL, buf);
Steve Blockaf12ac62012-01-06 19:20:56 +0000194 ALOGE("Could not compile shader \n%s\n", buf);
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -0700195 free(buf);
196 }
Jason Sams2382aba2011-09-13 15:41:01 -0700197 RSD_CALL_GL(glDeleteShader, mShaderID);
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -0700198 mShaderID = 0;
199 rsc->setError(RS_ERROR_BAD_SHADER, "Error returned from GL driver loading shader text,");
200 return false;
201 }
202 }
203 }
204
205 if (rsc->props.mLogShaders) {
Steve Block65982012011-10-20 11:56:00 +0100206 ALOGV("--Shader load result %x ", glGetError());
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -0700207 }
208 mIsValid = true;
209 return true;
210}
211
212void RsdShader::appendUserConstants() {
213 for (uint32_t ct=0; ct < mRSProgram->mHal.state.constantsCount; ct++) {
214 const Element *e = mRSProgram->mHal.state.constantTypes[ct]->getElement();
Alex Sakhartchouk4a43e3e2011-12-28 13:47:05 -0800215 for (uint32_t field=0; field < e->mHal.state.fieldsCount; field++) {
216 const Element *f = e->mHal.state.fields[field];
217 const char *fn = e->mHal.state.fieldNames[field];
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -0700218
219 // Cannot be complex
Alex Sakhartchouk4a43e3e2011-12-28 13:47:05 -0800220 rsAssert(!f->mHal.state.fieldsCount);
221 if (f->mHal.state.dataType == RS_TYPE_MATRIX_4X4) {
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -0700222 mShader.append("uniform mat4 UNI_");
Alex Sakhartchouk4a43e3e2011-12-28 13:47:05 -0800223 } else if (f->mHal.state.dataType == RS_TYPE_MATRIX_3X3) {
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -0700224 mShader.append("uniform mat3 UNI_");
Alex Sakhartchouk4a43e3e2011-12-28 13:47:05 -0800225 } else if (f->mHal.state.dataType == RS_TYPE_MATRIX_2X2) {
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -0700226 mShader.append("uniform mat2 UNI_");
227 } else {
Alex Sakhartchouk4a43e3e2011-12-28 13:47:05 -0800228 switch (f->mHal.state.vectorSize) {
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -0700229 case 1: mShader.append("uniform float UNI_"); break;
230 case 2: mShader.append("uniform vec2 UNI_"); break;
231 case 3: mShader.append("uniform vec3 UNI_"); break;
232 case 4: mShader.append("uniform vec4 UNI_"); break;
233 default:
234 rsAssert(0);
235 }
236 }
237
238 mShader.append(fn);
Alex Sakhartchouk4a43e3e2011-12-28 13:47:05 -0800239 if (e->mHal.state.fieldArraySizes[field] > 1) {
240 mShader.appendFormat("[%d]", e->mHal.state.fieldArraySizes[field]);
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -0700241 }
242 mShader.append(";\n");
243 }
244 }
245}
246
247void RsdShader::logUniform(const Element *field, const float *fd, uint32_t arraySize ) {
Alex Sakhartchouk4a43e3e2011-12-28 13:47:05 -0800248 RsDataType dataType = field->mHal.state.dataType;
249 uint32_t elementSize = field->mHal.state.elementSizeBytes / sizeof(float);
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -0700250 for (uint32_t i = 0; i < arraySize; i ++) {
251 if (arraySize > 1) {
Steve Block65982012011-10-20 11:56:00 +0100252 ALOGV("Array Element [%u]", i);
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -0700253 }
254 if (dataType == RS_TYPE_MATRIX_4X4) {
Steve Block65982012011-10-20 11:56:00 +0100255 ALOGV("Matrix4x4");
256 ALOGV("{%f, %f, %f, %f", fd[0], fd[4], fd[8], fd[12]);
257 ALOGV(" %f, %f, %f, %f", fd[1], fd[5], fd[9], fd[13]);
258 ALOGV(" %f, %f, %f, %f", fd[2], fd[6], fd[10], fd[14]);
259 ALOGV(" %f, %f, %f, %f}", fd[3], fd[7], fd[11], fd[15]);
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -0700260 } else if (dataType == RS_TYPE_MATRIX_3X3) {
Steve Block65982012011-10-20 11:56:00 +0100261 ALOGV("Matrix3x3");
262 ALOGV("{%f, %f, %f", fd[0], fd[3], fd[6]);
263 ALOGV(" %f, %f, %f", fd[1], fd[4], fd[7]);
264 ALOGV(" %f, %f, %f}", fd[2], fd[5], fd[8]);
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -0700265 } else if (dataType == RS_TYPE_MATRIX_2X2) {
Steve Block65982012011-10-20 11:56:00 +0100266 ALOGV("Matrix2x2");
267 ALOGV("{%f, %f", fd[0], fd[2]);
268 ALOGV(" %f, %f}", fd[1], fd[3]);
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -0700269 } else {
Alex Sakhartchouk4a43e3e2011-12-28 13:47:05 -0800270 switch (field->mHal.state.vectorSize) {
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -0700271 case 1:
Steve Block65982012011-10-20 11:56:00 +0100272 ALOGV("Uniform 1 = %f", fd[0]);
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -0700273 break;
274 case 2:
Steve Block65982012011-10-20 11:56:00 +0100275 ALOGV("Uniform 2 = %f %f", fd[0], fd[1]);
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -0700276 break;
277 case 3:
Steve Block65982012011-10-20 11:56:00 +0100278 ALOGV("Uniform 3 = %f %f %f", fd[0], fd[1], fd[2]);
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -0700279 break;
280 case 4:
Steve Block65982012011-10-20 11:56:00 +0100281 ALOGV("Uniform 4 = %f %f %f %f", fd[0], fd[1], fd[2], fd[3]);
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -0700282 break;
283 default:
284 rsAssert(0);
285 }
286 }
Steve Blockaf12ac62012-01-06 19:20:56 +0000287 ALOGE("Element size %u data=%p", elementSize, fd);
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -0700288 fd += elementSize;
Steve Blockaf12ac62012-01-06 19:20:56 +0000289 ALOGE("New data=%p", fd);
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -0700290 }
291}
292
293void RsdShader::setUniform(const Context *rsc, const Element *field, const float *fd,
294 int32_t slot, uint32_t arraySize ) {
Alex Sakhartchouk4a43e3e2011-12-28 13:47:05 -0800295 RsDataType dataType = field->mHal.state.dataType;
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -0700296 if (dataType == RS_TYPE_MATRIX_4X4) {
Jason Sams2382aba2011-09-13 15:41:01 -0700297 RSD_CALL_GL(glUniformMatrix4fv, slot, arraySize, GL_FALSE, fd);
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -0700298 } else if (dataType == RS_TYPE_MATRIX_3X3) {
Jason Sams2382aba2011-09-13 15:41:01 -0700299 RSD_CALL_GL(glUniformMatrix3fv, slot, arraySize, GL_FALSE, fd);
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -0700300 } else if (dataType == RS_TYPE_MATRIX_2X2) {
Jason Sams2382aba2011-09-13 15:41:01 -0700301 RSD_CALL_GL(glUniformMatrix2fv, slot, arraySize, GL_FALSE, fd);
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -0700302 } else {
Alex Sakhartchouk4a43e3e2011-12-28 13:47:05 -0800303 switch (field->mHal.state.vectorSize) {
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -0700304 case 1:
Jason Sams2382aba2011-09-13 15:41:01 -0700305 RSD_CALL_GL(glUniform1fv, slot, arraySize, fd);
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -0700306 break;
307 case 2:
Jason Sams2382aba2011-09-13 15:41:01 -0700308 RSD_CALL_GL(glUniform2fv, slot, arraySize, fd);
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -0700309 break;
310 case 3:
Jason Sams2382aba2011-09-13 15:41:01 -0700311 RSD_CALL_GL(glUniform3fv, slot, arraySize, fd);
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -0700312 break;
313 case 4:
Jason Sams2382aba2011-09-13 15:41:01 -0700314 RSD_CALL_GL(glUniform4fv, slot, arraySize, fd);
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -0700315 break;
316 default:
317 rsAssert(0);
318 }
319 }
320}
321
Alex Sakhartchouk7f126c72011-05-05 16:56:27 -0700322void RsdShader::setupSampler(const Context *rsc, const Sampler *s, const Allocation *tex) {
323 RsdHal *dc = (RsdHal *)rsc->mHal.drv;
324
325 GLenum trans[] = {
326 GL_NEAREST, //RS_SAMPLER_NEAREST,
327 GL_LINEAR, //RS_SAMPLER_LINEAR,
328 GL_LINEAR_MIPMAP_LINEAR, //RS_SAMPLER_LINEAR_MIP_LINEAR,
329 GL_REPEAT, //RS_SAMPLER_WRAP,
330 GL_CLAMP_TO_EDGE, //RS_SAMPLER_CLAMP
331 GL_LINEAR_MIPMAP_NEAREST, //RS_SAMPLER_LINEAR_MIP_NEAREST
332 };
333
334 GLenum transNP[] = {
335 GL_NEAREST, //RS_SAMPLER_NEAREST,
336 GL_LINEAR, //RS_SAMPLER_LINEAR,
337 GL_LINEAR, //RS_SAMPLER_LINEAR_MIP_LINEAR,
338 GL_CLAMP_TO_EDGE, //RS_SAMPLER_WRAP,
339 GL_CLAMP_TO_EDGE, //RS_SAMPLER_CLAMP
340 GL_LINEAR, //RS_SAMPLER_LINEAR_MIP_NEAREST,
341 };
342
343 // This tells us the correct texture type
Jason Samseb4fe182011-05-26 16:33:01 -0700344 DrvAllocation *drvTex = (DrvAllocation *)tex->mHal.drv;
345 const GLenum target = drvTex->glTarget;
Alex Sakhartchouk7f126c72011-05-05 16:56:27 -0700346
347 if (!dc->gl.gl.OES_texture_npot && tex->getType()->getIsNp2()) {
348 if (tex->getHasGraphicsMipmaps() &&
349 (dc->gl.gl.GL_NV_texture_npot_2D_mipmap || dc->gl.gl.GL_IMG_texture_npot)) {
350 if (dc->gl.gl.GL_NV_texture_npot_2D_mipmap) {
Jason Sams2382aba2011-09-13 15:41:01 -0700351 RSD_CALL_GL(glTexParameteri, target, GL_TEXTURE_MIN_FILTER,
352 trans[s->mHal.state.minFilter]);
Alex Sakhartchouk7f126c72011-05-05 16:56:27 -0700353 } else {
354 switch (trans[s->mHal.state.minFilter]) {
355 case GL_LINEAR_MIPMAP_LINEAR:
Jason Sams2382aba2011-09-13 15:41:01 -0700356 RSD_CALL_GL(glTexParameteri, target, GL_TEXTURE_MIN_FILTER,
357 GL_LINEAR_MIPMAP_NEAREST);
Alex Sakhartchouk7f126c72011-05-05 16:56:27 -0700358 break;
359 default:
Jason Sams2382aba2011-09-13 15:41:01 -0700360 RSD_CALL_GL(glTexParameteri, target, GL_TEXTURE_MIN_FILTER,
361 trans[s->mHal.state.minFilter]);
Alex Sakhartchouk7f126c72011-05-05 16:56:27 -0700362 break;
363 }
364 }
365 } else {
Jason Sams2382aba2011-09-13 15:41:01 -0700366 RSD_CALL_GL(glTexParameteri, target, GL_TEXTURE_MIN_FILTER,
367 transNP[s->mHal.state.minFilter]);
Alex Sakhartchouk7f126c72011-05-05 16:56:27 -0700368 }
Jason Sams2382aba2011-09-13 15:41:01 -0700369 RSD_CALL_GL(glTexParameteri, target, GL_TEXTURE_MAG_FILTER,
370 transNP[s->mHal.state.magFilter]);
371 RSD_CALL_GL(glTexParameteri, target, GL_TEXTURE_WRAP_S, transNP[s->mHal.state.wrapS]);
372 RSD_CALL_GL(glTexParameteri, target, GL_TEXTURE_WRAP_T, transNP[s->mHal.state.wrapT]);
Alex Sakhartchouk7f126c72011-05-05 16:56:27 -0700373 } else {
374 if (tex->getHasGraphicsMipmaps()) {
Jason Sams2382aba2011-09-13 15:41:01 -0700375 RSD_CALL_GL(glTexParameteri, target, GL_TEXTURE_MIN_FILTER,
376 trans[s->mHal.state.minFilter]);
Alex Sakhartchouk7f126c72011-05-05 16:56:27 -0700377 } else {
Jason Sams2382aba2011-09-13 15:41:01 -0700378 RSD_CALL_GL(glTexParameteri, target, GL_TEXTURE_MIN_FILTER,
379 transNP[s->mHal.state.minFilter]);
Alex Sakhartchouk7f126c72011-05-05 16:56:27 -0700380 }
Jason Sams2382aba2011-09-13 15:41:01 -0700381 RSD_CALL_GL(glTexParameteri, target, GL_TEXTURE_MAG_FILTER, trans[s->mHal.state.magFilter]);
382 RSD_CALL_GL(glTexParameteri, target, GL_TEXTURE_WRAP_S, trans[s->mHal.state.wrapS]);
383 RSD_CALL_GL(glTexParameteri, target, GL_TEXTURE_WRAP_T, trans[s->mHal.state.wrapT]);
Alex Sakhartchouk7f126c72011-05-05 16:56:27 -0700384 }
385
386 float anisoValue = rsMin(dc->gl.gl.EXT_texture_max_aniso, s->mHal.state.aniso);
387 if (dc->gl.gl.EXT_texture_max_aniso > 1.0f) {
Jason Sams2382aba2011-09-13 15:41:01 -0700388 RSD_CALL_GL(glTexParameterf, target, GL_TEXTURE_MAX_ANISOTROPY_EXT, anisoValue);
Alex Sakhartchouk7f126c72011-05-05 16:56:27 -0700389 }
390
Alex Sakhartchoukc19ff012011-05-06 14:59:45 -0700391 rsdGLCheckError(rsc, "Sampler::setup tex env");
Alex Sakhartchouk7f126c72011-05-05 16:56:27 -0700392}
393
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -0700394void RsdShader::setupTextures(const Context *rsc, RsdShaderCache *sc) {
395 if (mRSProgram->mHal.state.texturesCount == 0) {
396 return;
397 }
398
Alex Sakhartchoukc19ff012011-05-06 14:59:45 -0700399 RsdHal *dc = (RsdHal *)rsc->mHal.drv;
400
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -0700401 uint32_t numTexturesToBind = mRSProgram->mHal.state.texturesCount;
Alex Sakhartchoukc19ff012011-05-06 14:59:45 -0700402 uint32_t numTexturesAvailable = dc->gl.gl.maxFragmentTextureImageUnits;
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -0700403 if (numTexturesToBind >= numTexturesAvailable) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000404 ALOGE("Attempting to bind %u textures on shader id %u, but only %u are available",
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -0700405 mRSProgram->mHal.state.texturesCount, (uint32_t)this, numTexturesAvailable);
406 rsc->setError(RS_ERROR_BAD_SHADER, "Cannot bind more textuers than available");
407 numTexturesToBind = numTexturesAvailable;
408 }
409
410 for (uint32_t ct=0; ct < numTexturesToBind; ct++) {
Jason Sams2382aba2011-09-13 15:41:01 -0700411 RSD_CALL_GL(glActiveTexture, GL_TEXTURE0 + ct);
412 RSD_CALL_GL(glUniform1i, sc->fragUniformSlot(mTextureUniformIndexStart + ct), ct);
Alex Sakhartchoukba157302011-08-05 15:27:25 -0700413
Alex Sakhartchouk064aa7e2011-10-18 10:54:29 -0700414 if (!mRSProgram->mHal.state.textures[ct]) {
Alex Sakhartchoukba157302011-08-05 15:27:25 -0700415 // if nothing is bound, reset to default GL texture
Jason Sams2382aba2011-09-13 15:41:01 -0700416 RSD_CALL_GL(glBindTexture, mTextureTargets[ct], 0);
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -0700417 continue;
418 }
419
Jason Samseb4fe182011-05-26 16:33:01 -0700420 DrvAllocation *drvTex = (DrvAllocation *)mRSProgram->mHal.state.textures[ct]->mHal.drv;
421 if (drvTex->glTarget != GL_TEXTURE_2D && drvTex->glTarget != GL_TEXTURE_CUBE_MAP) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000422 ALOGE("Attempting to bind unknown texture to shader id %u, texture unit %u", (uint)this, ct);
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -0700423 rsc->setError(RS_ERROR_BAD_SHADER, "Non-texture allocation bound to a shader");
424 }
Jason Sams2382aba2011-09-13 15:41:01 -0700425 RSD_CALL_GL(glBindTexture, drvTex->glTarget, drvTex->textureID);
Alex Sakhartchoukc19ff012011-05-06 14:59:45 -0700426 rsdGLCheckError(rsc, "ProgramFragment::setup tex bind");
Alex Sakhartchouk064aa7e2011-10-18 10:54:29 -0700427 if (mRSProgram->mHal.state.samplers[ct]) {
428 setupSampler(rsc, mRSProgram->mHal.state.samplers[ct],
429 mRSProgram->mHal.state.textures[ct]);
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -0700430 } else {
Jason Sams2382aba2011-09-13 15:41:01 -0700431 RSD_CALL_GL(glTexParameteri, drvTex->glTarget, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
432 RSD_CALL_GL(glTexParameteri, drvTex->glTarget, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
433 RSD_CALL_GL(glTexParameteri, drvTex->glTarget, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
434 RSD_CALL_GL(glTexParameteri, drvTex->glTarget, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Alex Sakhartchoukc19ff012011-05-06 14:59:45 -0700435 rsdGLCheckError(rsc, "ProgramFragment::setup tex env");
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -0700436 }
Alex Sakhartchoukc19ff012011-05-06 14:59:45 -0700437 rsdGLCheckError(rsc, "ProgramFragment::setup uniforms");
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -0700438 }
439
Jason Sams2382aba2011-09-13 15:41:01 -0700440 RSD_CALL_GL(glActiveTexture, GL_TEXTURE0);
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -0700441 mDirty = false;
Alex Sakhartchoukc19ff012011-05-06 14:59:45 -0700442 rsdGLCheckError(rsc, "ProgramFragment::setup");
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -0700443}
444
445void RsdShader::setupUserConstants(const Context *rsc, RsdShaderCache *sc, bool isFragment) {
446 uint32_t uidx = 0;
447 for (uint32_t ct=0; ct < mRSProgram->mHal.state.constantsCount; ct++) {
Alex Sakhartchouk064aa7e2011-10-18 10:54:29 -0700448 Allocation *alloc = mRSProgram->mHal.state.constants[ct];
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -0700449 if (!alloc) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000450 ALOGE("Attempting to set constants on shader id %u, but alloc at slot %u is not set",
Jason Sams2382aba2011-09-13 15:41:01 -0700451 (uint32_t)this, ct);
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -0700452 rsc->setError(RS_ERROR_BAD_SHADER, "No constant allocation bound");
453 continue;
454 }
455
456 const uint8_t *data = static_cast<const uint8_t *>(alloc->getPtr());
457 const Element *e = mRSProgram->mHal.state.constantTypes[ct]->getElement();
Alex Sakhartchouk4a43e3e2011-12-28 13:47:05 -0800458 for (uint32_t field=0; field < e->mHal.state.fieldsCount; field++) {
459 const Element *f = e->mHal.state.fields[field];
460 const char *fieldName = e->mHal.state.fieldNames[field];
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -0700461
Alex Sakhartchouk4a43e3e2011-12-28 13:47:05 -0800462 uint32_t offset = e->mHal.state.fieldOffsetBytes[field];
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -0700463 const float *fd = reinterpret_cast<const float *>(&data[offset]);
464
465 int32_t slot = -1;
466 uint32_t arraySize = 1;
467 if (!isFragment) {
468 slot = sc->vtxUniformSlot(uidx);
469 arraySize = sc->vtxUniformSize(uidx);
470 } else {
471 slot = sc->fragUniformSlot(uidx);
472 arraySize = sc->fragUniformSize(uidx);
473 }
474 if (rsc->props.mLogShadersUniforms) {
Steve Block65982012011-10-20 11:56:00 +0100475 ALOGV("Uniform slot=%i, offset=%i, constant=%i, field=%i, uidx=%i, name=%s",
Jason Sams2382aba2011-09-13 15:41:01 -0700476 slot, offset, ct, field, uidx, fieldName);
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -0700477 }
478 uidx ++;
479 if (slot < 0) {
480 continue;
481 }
482
483 if (rsc->props.mLogShadersUniforms) {
484 logUniform(f, fd, arraySize);
485 }
486 setUniform(rsc, f, fd, slot, arraySize);
487 }
488 }
489}
490
491void RsdShader::setup(const android::renderscript::Context *rsc, RsdShaderCache *sc) {
492
493 setupUserConstants(rsc, sc, mType == GL_FRAGMENT_SHADER);
494 setupTextures(rsc, sc);
495}
496
497void RsdShader::initAttribAndUniformArray() {
498 mAttribCount = 0;
499 for (uint32_t ct=0; ct < mRSProgram->mHal.state.inputElementsCount; ct++) {
Alex Sakhartchouk064aa7e2011-10-18 10:54:29 -0700500 const Element *elem = mRSProgram->mHal.state.inputElements[ct];
Alex Sakhartchoukc0a65422012-01-05 14:55:11 -0800501 mAttribCount += elem->mHal.state.fieldsCount;
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -0700502 }
503
504 mUniformCount = 0;
505 for (uint32_t ct=0; ct < mRSProgram->mHal.state.constantsCount; ct++) {
506 const Element *elem = mRSProgram->mHal.state.constantTypes[ct]->getElement();
Alex Sakhartchouk4a43e3e2011-12-28 13:47:05 -0800507 mUniformCount += elem->mHal.state.fieldsCount;
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -0700508 }
509 mUniformCount += mRSProgram->mHal.state.texturesCount;
510
511 if (mAttribCount) {
512 mAttribNames = new String8[mAttribCount];
513 }
514 if (mUniformCount) {
515 mUniformNames = new String8[mUniformCount];
516 mUniformArraySizes = new uint32_t[mUniformCount];
517 }
Alex Sakhartchoukba157302011-08-05 15:27:25 -0700518
519 mTextureCount = mRSProgram->mHal.state.texturesCount;
520 if (mTextureCount) {
521 mTextureTargets = new uint32_t[mTextureCount];
522 }
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -0700523}
524
Jason Sams2382aba2011-09-13 15:41:01 -0700525void RsdShader::initAddUserElement(const Element *e, String8 *names, uint32_t *arrayLengths,
526 uint32_t *count, const char *prefix) {
Alex Sakhartchouk4a43e3e2011-12-28 13:47:05 -0800527 rsAssert(e->mHal.state.fieldsCount);
528 for (uint32_t ct=0; ct < e->mHal.state.fieldsCount; ct++) {
529 const Element *ce = e->mHal.state.fields[ct];
530 if (ce->mHal.state.fieldsCount) {
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -0700531 initAddUserElement(ce, names, arrayLengths, count, prefix);
Alex Sakhartchouk4a43e3e2011-12-28 13:47:05 -0800532 } else {
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -0700533 String8 tmp(prefix);
Alex Sakhartchouk4a43e3e2011-12-28 13:47:05 -0800534 tmp.append(e->mHal.state.fieldNames[ct]);
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -0700535 names[*count].setTo(tmp.string());
536 if (arrayLengths) {
Alex Sakhartchouk4a43e3e2011-12-28 13:47:05 -0800537 arrayLengths[*count] = e->mHal.state.fieldArraySizes[ct];
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -0700538 }
539 (*count)++;
540 }
541 }
542}