rsdShader.h revision 3522f40418fdf877f5a136475dbf75e57a3b7c77
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 <utils/String8.h>
21
22// ---------------------------------------------------------------------------
23namespace android {
24namespace renderscript {
25
26class Element;
27class Context;
28class Program;
29
30}
31}
32
33class RsdShaderCache;
34
35#define RS_SHADER_ATTR "ATTRIB_"
36#define RS_SHADER_UNI "UNI_"
37
38class RsdShader {
39public:
40
41    RsdShader(const android::renderscript::Program *p, uint32_t type,
42              const char * shaderText, uint32_t shaderLength,
43              const char** textureNames, size_t textureNamesCount,
44              const size_t *textureNamesLength);
45    virtual ~RsdShader();
46
47    bool createShader();
48
49    uint32_t getShaderID() const {return mShaderID;}
50
51    uint32_t getAttribCount() const {return mAttribCount;}
52    uint32_t getUniformCount() const {return mUniformCount;}
53    const android::String8 & getAttribName(uint32_t i) const {return mAttribNames[i];}
54    const android::String8 & getUniformName(uint32_t i) const {return mUniformNames[i];}
55    uint32_t getUniformArraySize(uint32_t i) const {return mUniformArraySizes[i];}
56
57    android::String8 getGLSLInputString() const;
58
59    bool isValid() const {return mIsValid;}
60    void forceDirty() const {mDirty = true;}
61
62    bool loadShader(const android::renderscript::Context *);
63    void setup(const android::renderscript::Context *, RsdShaderCache *sc);
64
65protected:
66
67    const android::renderscript::Program *mRSProgram;
68    bool mIsValid;
69
70    // Applies to vertex and fragment shaders only
71    void appendUserConstants();
72    void setupUserConstants(const android::renderscript::Context *rsc,
73                            RsdShaderCache *sc, bool isFragment);
74    void initAddUserElement(const android::renderscript::Element *e,
75                            android::String8 *names, uint32_t *arrayLengths,
76                            uint32_t *count, const char *prefix);
77    void setupTextures(const android::renderscript::Context *rsc, RsdShaderCache *sc);
78    void setupSampler(const android::renderscript::Context *rsc,
79                      const android::renderscript::Sampler *s,
80                      const android::renderscript::Allocation *tex);
81
82    void appendAttributes();
83    void appendTextures();
84
85    void initAttribAndUniformArray();
86
87    mutable bool mDirty;
88    android::String8 mShader;
89    android::String8 mUserShader;
90    uint32_t mShaderID;
91    uint32_t mType;
92
93    uint32_t mTextureCount;
94    uint32_t *mTextureTargets;
95    uint32_t mAttribCount;
96    uint32_t mUniformCount;
97    android::String8 *mAttribNames;
98    android::String8 *mUniformNames;
99    uint32_t *mUniformArraySizes;
100
101    android::Vector<android::String8> mTextureNames;
102
103    int32_t mTextureUniformIndexStart;
104
105    void logUniform(const android::renderscript::Element *field,
106                    const float *fd, uint32_t arraySize);
107    void setUniform(const android::renderscript::Context *rsc,
108                    const android::renderscript::Element *field,
109                    const float *fd, int32_t slot, uint32_t arraySize );
110    void initMemberVars();
111    void init(const char** textureNames, size_t textureNamesCount,
112              const size_t *textureNamesLength);
113};
114
115#endif //ANDROID_RSD_SHADER_H
116
117
118
119
120