blob: ff314b7f642971ccf36f0226f9c907cf3a712841 [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
17#include "rsContext.h"
Alex Sakhartchouk77d9f4b2011-01-31 14:53:24 -080018#ifndef ANDROID_RS_SERIALIZE
Jason Sams1aa5a4e2009-06-22 17:15:15 -070019#include <GLES/gl.h>
20#include <GLES/glext.h>
Jason Samsc460e552009-11-25 13:22:07 -080021#include <GLES2/gl2.h>
22#include <GLES2/gl2ext.h>
Alex Sakhartchouk77d9f4b2011-01-31 14:53:24 -080023#endif //ANDROID_RS_SERIALIZE
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070024
25#include "rsProgramFragment.h"
Jason Sams1aa5a4e2009-06-22 17:15:15 -070026
Jason Sams326e0dd2009-05-22 14:03:28 -070027using namespace android;
28using namespace android::renderscript;
29
Jason Samsf2e4fa22009-12-15 13:27:04 -080030ProgramFragment::ProgramFragment(Context *rsc, const char * shaderText,
31 uint32_t shaderLength, const uint32_t * params,
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080032 uint32_t paramLength)
33 : Program(rsc, shaderText, shaderLength, params, paramLength) {
34
Jason Sams6445e522010-08-04 17:50:20 -070035 mConstantColor[0] = 1.f;
36 mConstantColor[1] = 1.f;
37 mConstantColor[2] = 1.f;
38 mConstantColor[3] = 1.f;
39
Alex Sakhartchouk6e934212010-08-31 12:02:01 -070040 init(rsc);
Jason Samsf2e4fa22009-12-15 13:27:04 -080041}
42
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080043ProgramFragment::~ProgramFragment() {
44 if (mShaderID) {
Alex Sakhartchouk889fe502010-10-01 10:54:06 -070045 mRSC->mShaderCache.cleanupFragment(mShaderID);
46 }
Jason Sams326e0dd2009-05-22 14:03:28 -070047}
48
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080049void ProgramFragment::setConstantColor(Context *rsc, float r, float g, float b, float a) {
50 if (isUserProgram()) {
Alex Sakhartchouk383e5b12010-09-23 16:16:33 -070051 LOGE("Attempting to set fixed function emulation color on user program");
52 rsc->setError(RS_ERROR_BAD_SHADER, "Cannot set fixed function emulation color on user program");
53 return;
54 }
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080055 if (mConstants[0].get() == NULL) {
Alex Sakhartchouk383e5b12010-09-23 16:16:33 -070056 LOGE("Unable to set fixed function emulation color because allocation is missing");
57 rsc->setError(RS_ERROR_BAD_SHADER, "Unable to set fixed function emulation color because allocation is missing");
Alex Sakhartchouke7ae69f2010-09-14 09:50:43 -070058 return;
59 }
Jason Sams6445e522010-08-04 17:50:20 -070060 mConstantColor[0] = r;
61 mConstantColor[1] = g;
62 mConstantColor[2] = b;
63 mConstantColor[3] = a;
Alex Sakhartchouke7ae69f2010-09-14 09:50:43 -070064 memcpy(mConstants[0]->getPtr(), mConstantColor, 4*sizeof(float));
Jason Sams6445e522010-08-04 17:50:20 -070065 mDirty = true;
66}
67
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080068void ProgramFragment::setupGL2(Context *rsc, ProgramFragmentState *state, ShaderCache *sc) {
Jason Samsc460e552009-11-25 13:22:07 -080069 //LOGE("sgl2 frag1 %x", glGetError());
70 if ((state->mLast.get() == this) && !mDirty) {
Jason Samse64c2f12010-06-22 17:22:13 -070071 return;
Jason Samsc460e552009-11-25 13:22:07 -080072 }
73 state->mLast.set(this);
74
Jason Sams3eb28f02010-01-27 14:41:43 -080075 rsc->checkError("ProgramFragment::setupGL2 start");
Jason Sams6445e522010-08-04 17:50:20 -070076
Alex Sakhartchouk6e934212010-08-31 12:02:01 -070077 rsc->checkError("ProgramFragment::setupGL2 begin uniforms");
Alex Sakhartchouk383e5b12010-09-23 16:16:33 -070078 setupUserConstants(rsc, sc, true);
Alex Sakhartchouk6e934212010-08-31 12:02:01 -070079
Alex Sakhartchouk383e5b12010-09-23 16:16:33 -070080 uint32_t numTexturesToBind = mTextureCount;
81 uint32_t numTexturesAvailable = rsc->getMaxFragmentTextures();
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080082 if (numTexturesToBind >= numTexturesAvailable) {
Alex Sakhartchouk383e5b12010-09-23 16:16:33 -070083 LOGE("Attempting to bind %u textures on shader id %u, but only %u are available",
84 mTextureCount, (uint32_t)this, numTexturesAvailable);
85 rsc->setError(RS_ERROR_BAD_SHADER, "Cannot bind more textuers than available");
86 numTexturesToBind = numTexturesAvailable;
87 }
88
89 for (uint32_t ct=0; ct < numTexturesToBind; ct++) {
Jason Samsc460e552009-11-25 13:22:07 -080090 glActiveTexture(GL_TEXTURE0 + ct);
Alex Sakhartchouk383e5b12010-09-23 16:16:33 -070091 if (!mTextures[ct].get()) {
92 LOGE("No texture bound for shader id %u, texture unit %u", (uint)this, ct);
93 rsc->setError(RS_ERROR_BAD_SHADER, "No texture bound");
Jason Samsc460e552009-11-25 13:22:07 -080094 continue;
95 }
96
Jason Samscf4c7c92009-12-14 12:57:40 -080097 mTextures[ct]->uploadCheck(rsc);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -080098 GLenum target = (GLenum)mTextures[ct]->getGLTarget();
99 if (target != GL_TEXTURE_2D && target != GL_TEXTURE_CUBE_MAP) {
100 LOGE("Attempting to bind unknown texture to shader id %u, texture unit %u", (uint)this, ct);
101 rsc->setError(RS_ERROR_BAD_SHADER, "Non-texture allocation bound to a shader");
102 }
103 glBindTexture(target, mTextures[ct]->getTextureID());
Jason Sams3eb28f02010-01-27 14:41:43 -0800104 rsc->checkError("ProgramFragment::setupGL2 tex bind");
Jason Samsc460e552009-11-25 13:22:07 -0800105 if (mSamplers[ct].get()) {
Jason Sams900f1612010-09-16 18:18:29 -0700106 mSamplers[ct]->setupGL(rsc, mTextures[ct].get());
Jason Samsc460e552009-11-25 13:22:07 -0800107 } else {
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800108 glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
109 glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
110 glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
111 glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Jason Sams3eb28f02010-01-27 14:41:43 -0800112 rsc->checkError("ProgramFragment::setupGL2 tex env");
Jason Samsc460e552009-11-25 13:22:07 -0800113 }
114
Alex Sakhartchouk660ccc72010-09-02 18:43:24 -0700115 glUniform1i(sc->fragUniformSlot(mTextureUniformIndexStart + ct), ct);
Jason Sams3eb28f02010-01-27 14:41:43 -0800116 rsc->checkError("ProgramFragment::setupGL2 uniforms");
Jason Samsc460e552009-11-25 13:22:07 -0800117 }
118
119 glActiveTexture(GL_TEXTURE0);
120 mDirty = false;
Jason Sams433eca32010-01-06 11:57:52 -0800121 rsc->checkError("ProgramFragment::setupGL2");
Jason Samsc460e552009-11-25 13:22:07 -0800122}
123
Jason Samscd506532009-12-15 19:10:11 -0800124void ProgramFragment::loadShader(Context *rsc) {
125 Program::loadShader(rsc, GL_FRAGMENT_SHADER);
Jason Samsc460e552009-11-25 13:22:07 -0800126}
127
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800128void ProgramFragment::createShader() {
Jason Samsf2e4fa22009-12-15 13:27:04 -0800129 if (mUserShader.length() > 1) {
Alex Sakhartchouke7ae69f2010-09-14 09:50:43 -0700130 mShader.append("precision mediump float;\n");
Alex Sakhartchouk660ccc72010-09-02 18:43:24 -0700131 appendUserConstants();
Alex Sakhartchouk383e5b12010-09-23 16:16:33 -0700132 char buf[256];
Jason Samsf2e4fa22009-12-15 13:27:04 -0800133 for (uint32_t ct=0; ct < mTextureCount; ct++) {
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800134 if (mTextureTargets[ct] == RS_TEXTURE_2D) {
Jason Sams87319de2010-11-22 16:20:16 -0800135 snprintf(buf, sizeof(buf), "uniform sampler2D UNI_Tex%i;\n", ct);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800136 } else {
Jason Sams87319de2010-11-22 16:20:16 -0800137 snprintf(buf, sizeof(buf), "uniform samplerCube UNI_Tex%i;\n", ct);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800138 }
Jason Samsc460e552009-11-25 13:22:07 -0800139 mShader.append(buf);
Jason Samsc460e552009-11-25 13:22:07 -0800140 }
Jason Samsf2e4fa22009-12-15 13:27:04 -0800141 mShader.append(mUserShader);
142 } else {
Alex Sakhartchouke7ae69f2010-09-14 09:50:43 -0700143 LOGE("ProgramFragment::createShader cannot create program, shader code not defined");
144 rsAssert(0);
Jason Samsc460e552009-11-25 13:22:07 -0800145 }
Jason Samsc460e552009-11-25 13:22:07 -0800146}
Jason Sams326e0dd2009-05-22 14:03:28 -0700147
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800148void ProgramFragment::init(Context *rsc) {
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800149 uint32_t uniformIndex = 0;
Alex Sakhartchouk6e934212010-08-31 12:02:01 -0700150 if (mUserShader.size() > 0) {
151 for (uint32_t ct=0; ct < mConstantCount; ct++) {
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800152 initAddUserElement(mConstantTypes[ct]->getElement(), mUniformNames, mUniformArraySizes, &uniformIndex, RS_SHADER_UNI);
Alex Sakhartchouk6e934212010-08-31 12:02:01 -0700153 }
154 }
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800155 mTextureUniformIndexStart = uniformIndex;
Alex Sakhartchouk383e5b12010-09-23 16:16:33 -0700156 char buf[256];
157 for (uint32_t ct=0; ct < mTextureCount; ct++) {
Jason Sams87319de2010-11-22 16:20:16 -0800158 snprintf(buf, sizeof(buf), "UNI_Tex%i", ct);
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800159 mUniformNames[uniformIndex].setTo(buf);
160 mUniformArraySizes[uniformIndex] = 1;
161 uniformIndex++;
Alex Sakhartchouk383e5b12010-09-23 16:16:33 -0700162 }
Alex Sakhartchouk6e934212010-08-31 12:02:01 -0700163
164 createShader();
Jason Samsc460e552009-11-25 13:22:07 -0800165}
Jason Sams326e0dd2009-05-22 14:03:28 -0700166
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800167void ProgramFragment::serialize(OStream *stream) const {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700168}
169
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800170ProgramFragment *ProgramFragment::createFromStream(Context *rsc, IStream *stream) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700171 return NULL;
172}
173
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800174ProgramFragmentState::ProgramFragmentState() {
Jason Sams326e0dd2009-05-22 14:03:28 -0700175 mPF = NULL;
176}
177
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800178ProgramFragmentState::~ProgramFragmentState() {
Jason Sams225afd32010-10-21 14:06:55 -0700179 ObjectBase::checkDelete(mPF);
180 mPF = NULL;
Jason Sams326e0dd2009-05-22 14:03:28 -0700181}
182
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800183void ProgramFragmentState::init(Context *rsc) {
Alex Sakhartchouke7ae69f2010-09-14 09:50:43 -0700184 String8 shaderString(RS_SHADER_INTERNAL);
185 shaderString.append("varying lowp vec4 varColor;\n");
Alex Sakhartchouk7ffcaf22010-10-06 11:15:01 -0700186 shaderString.append("varying vec2 varTex0;\n");
Alex Sakhartchouke7ae69f2010-09-14 09:50:43 -0700187 shaderString.append("void main() {\n");
188 shaderString.append(" lowp vec4 col = UNI_Color;\n");
189 shaderString.append(" gl_FragColor = col;\n");
190 shaderString.append("}\n");
191
192 const Element *colorElem = Element::create(rsc, RS_TYPE_FLOAT_32, RS_KIND_USER, false, 4);
193 rsc->mStateElement.elementBuilderBegin();
194 rsc->mStateElement.elementBuilderAdd(colorElem, "Color", 1);
195 const Element *constInput = rsc->mStateElement.elementBuilderCreate(rsc);
196
Jason Samsf0c1df42010-10-26 13:09:17 -0700197 Type *inputType = Type::getType(rsc, constInput, 1, 0, 0, false, false);
Alex Sakhartchouke7ae69f2010-09-14 09:50:43 -0700198
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800199 uint32_t tmp[2];
Alex Sakhartchouke7ae69f2010-09-14 09:50:43 -0700200 tmp[0] = RS_PROGRAM_PARAM_CONSTANT;
201 tmp[1] = (uint32_t)inputType;
Alex Sakhartchouke7ae69f2010-09-14 09:50:43 -0700202
Jason Sams366c9c82010-12-08 16:14:36 -0800203 Allocation *constAlloc = new Allocation(rsc, inputType, RS_ALLOCATION_USAGE_SCRIPT | RS_ALLOCATION_USAGE_GRAPHICS_CONSTANTS);
Alex Sakhartchouke7ae69f2010-09-14 09:50:43 -0700204 ProgramFragment *pf = new ProgramFragment(rsc, shaderString.string(),
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800205 shaderString.length(), tmp, 2);
Alex Sakhartchouk383e5b12010-09-23 16:16:33 -0700206 pf->bindAllocation(rsc, constAlloc, 0);
207 pf->setConstantColor(rsc, 1.0f, 1.0f, 1.0f, 1.0f);
Alex Sakhartchouke7ae69f2010-09-14 09:50:43 -0700208
Jason Sams8ce125b2009-06-17 16:52:59 -0700209 mDefault.set(pf);
210}
Jason Sams326e0dd2009-05-22 14:03:28 -0700211
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800212void ProgramFragmentState::deinit(Context *rsc) {
Jason Samsf2649a92009-09-25 16:37:33 -0700213 mDefault.clear();
214 mLast.clear();
215}
216
Jason Sams326e0dd2009-05-22 14:03:28 -0700217namespace android {
218namespace renderscript {
219
Alex Sakhartchouk383e5b12010-09-23 16:16:33 -0700220RsProgramFragment rsi_ProgramFragmentCreate(Context *rsc, const char * shaderText,
Jason Samsf2e4fa22009-12-15 13:27:04 -0800221 uint32_t shaderLength, const uint32_t * params,
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800222 uint32_t paramLength) {
Jason Samsf2e4fa22009-12-15 13:27:04 -0800223 ProgramFragment *pf = new ProgramFragment(rsc, shaderText, shaderLength, params, paramLength);
224 pf->incUserRef();
Alex Sakhartchouk383e5b12010-09-23 16:16:33 -0700225 //LOGE("rsi_ProgramFragmentCreate %p", pf);
Jason Samsf2e4fa22009-12-15 13:27:04 -0800226 return pf;
227}
Jason Sams326e0dd2009-05-22 14:03:28 -0700228
229}
230}
231