rsProgram.h revision 6e9342199959dc9beb5299fefc9775fe8c32620e
1/* 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#ifndef ANDROID_RS_PROGRAM_H 18#define ANDROID_RS_PROGRAM_H 19 20#include "rsObjectBase.h" 21#include "rsElement.h" 22 23// --------------------------------------------------------------------------- 24namespace android { 25namespace renderscript { 26 27 28class ShaderCache; 29 30class Program : public ObjectBase 31{ 32public: 33 const static uint32_t MAX_ATTRIBS = 8; 34 const static uint32_t MAX_UNIFORMS = 16; 35 const static uint32_t MAX_TEXTURE = 2; 36 37 Program(Context *); 38 Program(Context *, const char * shaderText, uint32_t shaderLength, 39 const uint32_t * params, uint32_t paramLength); 40 virtual ~Program(); 41 42 void bindAllocation(Allocation *, uint32_t slot); 43 virtual void createShader(); 44 45 bool isUserProgram() const {return mUserShader.size() > 0;} 46 47 void bindTexture(uint32_t slot, Allocation *); 48 void bindSampler(uint32_t slot, Sampler *); 49 50 uint32_t getShaderID() const {return mShaderID;} 51 void setShader(const char *, uint32_t len); 52 53 uint32_t getAttribCount() const {return mAttribCount;} 54 uint32_t getUniformCount() const {return mUniformCount;} 55 const String8 & getAttribName(uint32_t i) const {return mAttribNames[i];} 56 const String8 & getUniformName(uint32_t i) const {return mUniformNames[i];} 57 58 String8 getGLSLInputString() const; 59 String8 getGLSLOutputString() const; 60 String8 getGLSLConstantString() const; 61 62 bool isValid() const {return mIsValid;} 63 64protected: 65 // Components not listed in "in" will be passed though 66 // unless overwritten by components in out. 67 ObjectBaseRef<Element> *mInputElements; 68 ObjectBaseRef<Element> *mOutputElements; 69 ObjectBaseRef<Type> *mConstantTypes; 70 uint32_t mInputCount; 71 uint32_t mOutputCount; 72 uint32_t mConstantCount; 73 bool mIsValid; 74 75 // Applies to vertex and fragment shaders only 76 void appendUserConstants(); 77 void setupUserConstants(ShaderCache *sc, bool isFragment); 78 void initAddUserElement(const Element *e, String8 *names, uint32_t *count, const char *prefix); 79 80 ObjectBaseRef<Allocation> mConstants[MAX_UNIFORMS]; 81 82 mutable bool mDirty; 83 String8 mShader; 84 String8 mUserShader; 85 uint32_t mShaderID; 86 87 uint32_t mTextureCount; 88 uint32_t mAttribCount; 89 uint32_t mUniformCount; 90 String8 mAttribNames[MAX_ATTRIBS]; 91 String8 mUniformNames[MAX_UNIFORMS]; 92 93 // The difference between Textures and Constants is how they are accessed 94 // Texture lookups go though a sampler which in effect converts normalized 95 // coordinates into type specific. Multiple samples may also be taken 96 // and filtered. 97 // 98 // Constants are strictly accessed by programetic loads. 99 ObjectBaseRef<Allocation> mTextures[MAX_TEXTURE]; 100 ObjectBaseRef<Sampler> mSamplers[MAX_TEXTURE]; 101 102 bool loadShader(Context *, uint32_t type); 103 104public: 105 void forceDirty() const {mDirty = true;} 106}; 107 108 109 110} 111} 112#endif 113 114 115 116