• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011 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 <utils/String8.h>
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, uint32_t shaderLength);
43     virtual ~RsdShader();
44 
45     bool createShader();
46 
getShaderID()47     uint32_t getShaderID() const {return mShaderID;}
48 
getAttribCount()49     uint32_t getAttribCount() const {return mAttribCount;}
getUniformCount()50     uint32_t getUniformCount() const {return mUniformCount;}
getAttribName(uint32_t i)51     const android::String8 & getAttribName(uint32_t i) const {return mAttribNames[i];}
getUniformName(uint32_t i)52     const android::String8 & getUniformName(uint32_t i) const {return mUniformNames[i];}
getUniformArraySize(uint32_t i)53     uint32_t getUniformArraySize(uint32_t i) const {return mUniformArraySizes[i];}
54 
55     android::String8 getGLSLInputString() const;
56 
isValid()57     bool isValid() const {return mIsValid;}
forceDirty()58     void forceDirty() const {mDirty = true;}
59 
60     bool loadShader(const android::renderscript::Context *);
61     void setup(const android::renderscript::Context *, RsdShaderCache *sc);
62 
63 protected:
64 
65     const android::renderscript::Program *mRSProgram;
66     bool mIsValid;
67 
68     // Applies to vertex and fragment shaders only
69     void appendUserConstants();
70     void setupUserConstants(const android::renderscript::Context *rsc, RsdShaderCache *sc, bool isFragment);
71     void initAddUserElement(const android::renderscript::Element *e, android::String8 *names, uint32_t *arrayLengths, uint32_t *count, const char *prefix);
72     void setupTextures(const android::renderscript::Context *rsc, RsdShaderCache *sc);
73     void setupSampler(const android::renderscript::Context *rsc, const android::renderscript::Sampler *s, const android::renderscript::Allocation *tex);
74 
75     void appendAttributes();
76     void appendTextures();
77 
78     void initAttribAndUniformArray();
79 
80     mutable bool mDirty;
81     android::String8 mShader;
82     android::String8 mUserShader;
83     uint32_t mShaderID;
84     uint32_t mType;
85 
86     uint32_t mTextureCount;
87     uint32_t *mTextureTargets;
88     uint32_t mAttribCount;
89     uint32_t mUniformCount;
90     android::String8 *mAttribNames;
91     android::String8 *mUniformNames;
92     uint32_t *mUniformArraySizes;
93 
94     int32_t mTextureUniformIndexStart;
95 
96     void logUniform(const android::renderscript::Element *field, const float *fd, uint32_t arraySize );
97     void setUniform(const android::renderscript::Context *rsc, const android::renderscript::Element *field, const float *fd, int32_t slot, uint32_t arraySize );
98     void initMemberVars();
99     void init();
100 };
101 
102 #endif //ANDROID_RSD_SHADER_H
103 
104 
105 
106 
107