blob: 2680b3ef99c24caccff33a4c3e1d802aca0a826d [file] [log] [blame]
Alex Sakhartchouk4a36b452011-04-29 16:49:08 -07001/*
Jason Samsfe1d5ff2012-03-23 11:47:26 -07002 * Copyright (C) 2011-2012 The Android Open Source Project
Alex Sakhartchouk4a36b452011-04-29 16:49:08 -07003 *
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,
Alex Sakhartchouk2123b462012-02-15 16:21:46 -080042 const char * shaderText, uint32_t shaderLength,
43 const char** textureNames, size_t textureNamesCount,
44 const size_t *textureNamesLength);
Alex Sakhartchouk4a36b452011-04-29 16:49:08 -070045 virtual ~RsdShader();
46
Alex Sakhartchouk5ff19592012-03-26 13:52:24 -070047 uint32_t getStateBasedShaderID(const android::renderscript::Context *);
Alex Sakhartchouk4a36b452011-04-29 16:49:08 -070048
Alex Sakhartchouk5ff19592012-03-26 13:52:24 -070049 // Add ability to get all ID's to clean up the cached program objects
50 uint32_t getStateBasedIDCount() const { return mStateBasedShaders.size(); }
51 uint32_t getStateBasedID(uint32_t index) const {
52 return mStateBasedShaders.itemAt(index)->mShaderID;
53 }
Alex Sakhartchouk4a36b452011-04-29 16:49:08 -070054
55 uint32_t getAttribCount() const {return mAttribCount;}
56 uint32_t getUniformCount() const {return mUniformCount;}
57 const android::String8 & getAttribName(uint32_t i) const {return mAttribNames[i];}
58 const android::String8 & getUniformName(uint32_t i) const {return mUniformNames[i];}
59 uint32_t getUniformArraySize(uint32_t i) const {return mUniformArraySizes[i];}
60
61 android::String8 getGLSLInputString() const;
62
63 bool isValid() const {return mIsValid;}
64 void forceDirty() const {mDirty = true;}
65
66 bool loadShader(const android::renderscript::Context *);
67 void setup(const android::renderscript::Context *, RsdShaderCache *sc);
68
69protected:
70
Alex Sakhartchouk5ff19592012-03-26 13:52:24 -070071 class StateBasedKey {
72 public:
73 StateBasedKey(uint32_t texCount) : mShaderID(0) {
74 mTextureTargets = new uint32_t[texCount];
75 }
76 ~StateBasedKey() {
77 delete[] mTextureTargets;
78 }
79 uint32_t mShaderID;
80 uint32_t *mTextureTargets;
81 };
82
83 bool createShader();
84 StateBasedKey *getExistingState();
85
Alex Sakhartchouk4a36b452011-04-29 16:49:08 -070086 const android::renderscript::Program *mRSProgram;
87 bool mIsValid;
88
89 // Applies to vertex and fragment shaders only
90 void appendUserConstants();
Alex Sakhartchouk2123b462012-02-15 16:21:46 -080091 void setupUserConstants(const android::renderscript::Context *rsc,
92 RsdShaderCache *sc, bool isFragment);
93 void initAddUserElement(const android::renderscript::Element *e,
94 android::String8 *names, uint32_t *arrayLengths,
95 uint32_t *count, const char *prefix);
Alex Sakhartchouk4a36b452011-04-29 16:49:08 -070096 void setupTextures(const android::renderscript::Context *rsc, RsdShaderCache *sc);
Alex Sakhartchouk2123b462012-02-15 16:21:46 -080097 void setupSampler(const android::renderscript::Context *rsc,
98 const android::renderscript::Sampler *s,
99 const android::renderscript::Allocation *tex);
Alex Sakhartchouk4a36b452011-04-29 16:49:08 -0700100
101 void appendAttributes();
102 void appendTextures();
103
104 void initAttribAndUniformArray();
105
106 mutable bool mDirty;
107 android::String8 mShader;
108 android::String8 mUserShader;
Alex Sakhartchouk4a36b452011-04-29 16:49:08 -0700109 uint32_t mType;
110
111 uint32_t mTextureCount;
Alex Sakhartchouk5ff19592012-03-26 13:52:24 -0700112 StateBasedKey *mCurrentState;
Alex Sakhartchouk4a36b452011-04-29 16:49:08 -0700113 uint32_t mAttribCount;
114 uint32_t mUniformCount;
115 android::String8 *mAttribNames;
116 android::String8 *mUniformNames;
117 uint32_t *mUniformArraySizes;
118
Jason Samsfe1d5ff2012-03-23 11:47:26 -0700119 android::Vector<android::String8> mTextureNames;
120
Alex Sakhartchouk5ff19592012-03-26 13:52:24 -0700121 android::Vector<StateBasedKey*> mStateBasedShaders;
122
Alex Sakhartchouk4a36b452011-04-29 16:49:08 -0700123 int32_t mTextureUniformIndexStart;
124
Alex Sakhartchouk2123b462012-02-15 16:21:46 -0800125 void logUniform(const android::renderscript::Element *field,
126 const float *fd, uint32_t arraySize);
127 void setUniform(const android::renderscript::Context *rsc,
128 const android::renderscript::Element *field,
129 const float *fd, int32_t slot, uint32_t arraySize );
Alex Sakhartchouk4a36b452011-04-29 16:49:08 -0700130 void initMemberVars();
Alex Sakhartchouk2123b462012-02-15 16:21:46 -0800131 void init(const char** textureNames, size_t textureNamesCount,
132 const size_t *textureNamesLength);
Alex Sakhartchouk4a36b452011-04-29 16:49:08 -0700133};
134
135#endif //ANDROID_RSD_SHADER_H
136
137
138
139