Home | History | Annotate | Download | only in driver
      1 /*
      2  * Copyright (C) 2011-2012 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_RSD_SHADER_H
     18 #define ANDROID_RSD_SHADER_H
     19 
     20 #include <string>
     21 
     22 // ---------------------------------------------------------------------------
     23 namespace android {
     24 namespace renderscript {
     25 
     26 class Element;
     27 class Context;
     28 class Program;
     29 
     30 }
     31 }
     32 
     33 class RsdShaderCache;
     34 
     35 #define RS_SHADER_ATTR "ATTRIB_"
     36 #define RS_SHADER_UNI "UNI_"
     37 
     38 class RsdShader {
     39 public:
     40 
     41     RsdShader(const android::renderscript::Program *p, uint32_t type,
     42               const char * shaderText, size_t shaderLength,
     43               const char** textureNames, size_t textureNamesCount,
     44               const size_t *textureNamesLength);
     45     virtual ~RsdShader();
     46 
     47     uint32_t getStateBasedShaderID(const android::renderscript::Context *);
     48 
     49     // Add ability to get all ID's to clean up the cached program objects
     50     uint32_t getStateBasedIDCount() const { return mStateBasedShaders.size(); }
     51     uint32_t getStateBasedID(uint32_t index) const {
     52         return mStateBasedShaders.itemAt(index)->mShaderID;
     53     }
     54 
     55     uint32_t getAttribCount() const {return mAttribCount;}
     56     uint32_t getUniformCount() const {return mUniformCount;}
     57     const std::string & getAttribName(uint32_t i) const {return mAttribNames[i];}
     58     const std::string & getUniformName(uint32_t i) const {return mUniformNames[i];}
     59     uint32_t getUniformArraySize(uint32_t i) const {return mUniformArraySizes[i];}
     60 
     61     std::string getGLSLInputString() const;
     62 
     63     bool isValid() const {return mIsValid;}
     64     void forceDirty() const {mDirty = true;}
     65 
     66     bool loadShader(const android::renderscript::Context *);
     67     void setup(const android::renderscript::Context *, RsdShaderCache *sc);
     68 
     69 protected:
     70 
     71     class StateBasedKey {
     72     public:
     73         StateBasedKey(uint32_t texCount) : mShaderID(0) {
     74             mTextureTargets = new uint32_t[texCount];
     75         }
     76         ~StateBasedKey() {
     77             delete[] mTextureTargets;
     78         }
     79         uint32_t mShaderID;
     80         uint32_t *mTextureTargets;
     81     };
     82 
     83     bool createShader();
     84     StateBasedKey *getExistingState();
     85 
     86     const android::renderscript::Program *mRSProgram;
     87     bool mIsValid;
     88 
     89     // Applies to vertex and fragment shaders only
     90     void appendUserConstants();
     91     void setupUserConstants(const android::renderscript::Context *rsc,
     92                             RsdShaderCache *sc, bool isFragment);
     93     void initAddUserElement(const android::renderscript::Element *e,
     94                             std::string *names, uint32_t *arrayLengths,
     95                             uint32_t *count, const char *prefix);
     96     void setupTextures(const android::renderscript::Context *rsc, RsdShaderCache *sc);
     97     void setupSampler(const android::renderscript::Context *rsc,
     98                       const android::renderscript::Sampler *s,
     99                       const android::renderscript::Allocation *tex);
    100 
    101     void appendAttributes();
    102     void appendTextures();
    103 
    104     void initAttribAndUniformArray();
    105 
    106     mutable bool mDirty;
    107     std::string mShader;
    108     std::string mUserShader;
    109     uint32_t mType;
    110 
    111     uint32_t mTextureCount;
    112     StateBasedKey *mCurrentState;
    113     uint32_t mAttribCount;
    114     uint32_t mUniformCount;
    115     std::string *mAttribNames;
    116     std::string *mUniformNames;
    117     uint32_t *mUniformArraySizes;
    118 
    119     android::Vector<android::String8> mTextureNames;
    120 
    121     android::Vector<StateBasedKey*> mStateBasedShaders;
    122 
    123     int32_t mTextureUniformIndexStart;
    124 
    125     void logUniform(const android::renderscript::Element *field,
    126                     const float *fd, uint32_t arraySize);
    127     void setUniform(const android::renderscript::Context *rsc,
    128                     const android::renderscript::Element *field,
    129                     const float *fd, int32_t slot, uint32_t arraySize );
    130     void initMemberVars();
    131     void init(const char** textureNames, size_t textureNamesCount,
    132               const size_t *textureNamesLength);
    133 };
    134 
    135 #endif //ANDROID_RSD_SHADER_H
    136 
    137 
    138 
    139 
    140