rsdShader.h revision 748eb07e805b93c2bf79340d4937963ab739d17c
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// ---------------------------------------------------------------------------
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    void createTexturesString(const char** textureNames, size_t textureNamesCount,
85                              const size_t *textureNamesLength);
86
87    void initAttribAndUniformArray();
88
89    mutable bool mDirty;
90    android::String8 mShader;
91    android::String8 mUserShader;
92    android::String8 mShaderTextures;
93    uint32_t mShaderID;
94    uint32_t mType;
95
96    uint32_t mTextureCount;
97    uint32_t *mTextureTargets;
98    uint32_t mAttribCount;
99    uint32_t mUniformCount;
100    android::String8 *mAttribNames;
101    android::String8 *mUniformNames;
102    uint32_t *mUniformArraySizes;
103
104    int32_t mTextureUniformIndexStart;
105
106    void logUniform(const android::renderscript::Element *field,
107                    const float *fd, uint32_t arraySize);
108    void setUniform(const android::renderscript::Context *rsc,
109                    const android::renderscript::Element *field,
110                    const float *fd, int32_t slot, uint32_t arraySize );
111    void initMemberVars();
112    void init(const char** textureNames, size_t textureNamesCount,
113              const size_t *textureNamesLength);
114};
115
116#endif //ANDROID_RSD_SHADER_H
117
118
119
120
121