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