rsProgram.h revision e7ae69f4a70f1813cf8086ebd9714192c635300a
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 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 !mIsInternal;} 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 bool mIsInternal; 75 76 // Applies to vertex and fragment shaders only 77 void appendUserConstants(); 78 void setupUserConstants(ShaderCache *sc, bool isFragment); 79 void initAddUserElement(const Element *e, String8 *names, uint32_t *count, const char *prefix); 80 81 ObjectBaseRef<Allocation> mConstants[MAX_UNIFORMS]; 82 83 mutable bool mDirty; 84 String8 mShader; 85 String8 mUserShader; 86 uint32_t mShaderID; 87 88 uint32_t mTextureCount; 89 uint32_t mAttribCount; 90 uint32_t mUniformCount; 91 String8 mAttribNames[MAX_ATTRIBS]; 92 String8 mUniformNames[MAX_UNIFORMS]; 93 94 // The difference between Textures and Constants is how they are accessed 95 // Texture lookups go though a sampler which in effect converts normalized 96 // coordinates into type specific. Multiple samples may also be taken 97 // and filtered. 98 // 99 // Constants are strictly accessed by programetic loads. 100 ObjectBaseRef<Allocation> mTextures[MAX_TEXTURE]; 101 ObjectBaseRef<Sampler> mSamplers[MAX_TEXTURE]; 102 103 bool loadShader(Context *, uint32_t type); 104 105public: 106 void forceDirty() const {mDirty = true;} 107}; 108 109 110 111} 112} 113#endif 114 115 116 117