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