rsProgram.h revision a2cf755a28a1e7ffff2955df656d714f40e4d715
1/*
2 * Copyright (C) 2009 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_RS_PROGRAM_H
18#define ANDROID_RS_PROGRAM_H
19
20#include "rsObjectBase.h"
21#include "rsElement.h"
22
23// ---------------------------------------------------------------------------
24namespace android {
25namespace renderscript {
26
27
28class ShaderCache;
29
30class Program : public ObjectBase
31{
32public:
33    const static uint32_t MAX_ATTRIBS = 8;
34    const static uint32_t MAX_UNIFORMS = 16;
35    const static uint32_t MAX_TEXTURE = 2;
36
37    Program(Context *);
38    Program(Context *, const char * shaderText, uint32_t shaderLength,
39                       const uint32_t * params, uint32_t paramLength);
40    virtual ~Program();
41
42    void bindAllocation(Allocation *, uint32_t slot);
43    virtual void createShader();
44
45    bool isUserProgram() const {return mUserShader.size() > 0;}
46
47    void bindTexture(uint32_t slot, Allocation *);
48    void bindSampler(uint32_t slot, Sampler *);
49
50    uint32_t getShaderID() const {return mShaderID;}
51    void setShader(const char *, uint32_t len);
52
53    uint32_t getAttribCount() const {return mAttribCount;}
54    uint32_t getUniformCount() const {return mUniformCount;}
55    const String8 & getAttribName(uint32_t i) const {return mAttribNames[i];}
56    const String8 & getUniformName(uint32_t i) const {return mUniformNames[i];}
57
58    String8 getGLSLInputString() const;
59    String8 getGLSLOutputString() const;
60    String8 getGLSLConstantString() const;
61
62    bool isValid() const {return mIsValid;}
63
64protected:
65    // Components not listed in "in" will be passed though
66    // unless overwritten by components in out.
67    ObjectBaseRef<Element> *mInputElements;
68    ObjectBaseRef<Element> *mOutputElements;
69    ObjectBaseRef<Type> *mConstantTypes;
70    uint32_t mInputCount;
71    uint32_t mOutputCount;
72    uint32_t mConstantCount;
73    bool mIsValid;
74
75    ObjectBaseRef<Allocation> mConstants[MAX_UNIFORMS];
76
77    mutable bool mDirty;
78    String8 mShader;
79    String8 mUserShader;
80    uint32_t mShaderID;
81
82    uint32_t mTextureCount;
83    uint32_t mAttribCount;
84    uint32_t mUniformCount;
85    String8 mAttribNames[MAX_ATTRIBS];
86    String8 mUniformNames[MAX_UNIFORMS];
87
88    // The difference between Textures and Constants is how they are accessed
89    // Texture lookups go though a sampler which in effect converts normalized
90    // coordinates into type specific.  Multiple samples may also be taken
91    // and filtered.
92    //
93    // Constants are strictly accessed by programetic loads.
94    ObjectBaseRef<Allocation> mTextures[MAX_TEXTURE];
95    ObjectBaseRef<Sampler> mSamplers[MAX_TEXTURE];
96
97    bool loadShader(Context *, uint32_t type);
98
99public:
100    void forceDirty() const {mDirty = true;}
101};
102
103
104
105}
106}
107#endif
108
109
110
111