Alex Sakhartchouk | 4a36b45 | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 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, |
Alex Sakhartchouk | 2123b46 | 2012-02-15 16:21:46 -0800 | [diff] [blame^] | 42 | const char * shaderText, uint32_t shaderLength, |
| 43 | const char** textureNames, size_t textureNamesCount, |
| 44 | const size_t *textureNamesLength); |
Alex Sakhartchouk | 4a36b45 | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 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 | |
| 65 | protected: |
| 66 | |
| 67 | const android::renderscript::Program *mRSProgram; |
| 68 | bool mIsValid; |
| 69 | |
| 70 | // Applies to vertex and fragment shaders only |
| 71 | void appendUserConstants(); |
Alex Sakhartchouk | 2123b46 | 2012-02-15 16:21:46 -0800 | [diff] [blame^] | 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); |
Alex Sakhartchouk | 4a36b45 | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 77 | void setupTextures(const android::renderscript::Context *rsc, RsdShaderCache *sc); |
Alex Sakhartchouk | 2123b46 | 2012-02-15 16:21:46 -0800 | [diff] [blame^] | 78 | void setupSampler(const android::renderscript::Context *rsc, |
| 79 | const android::renderscript::Sampler *s, |
| 80 | const android::renderscript::Allocation *tex); |
Alex Sakhartchouk | 4a36b45 | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 81 | |
| 82 | void appendAttributes(); |
| 83 | void appendTextures(); |
Alex Sakhartchouk | 2123b46 | 2012-02-15 16:21:46 -0800 | [diff] [blame^] | 84 | void createTexturesString(const char** textureNames, size_t textureNamesCount, |
| 85 | const size_t *textureNamesLength); |
Alex Sakhartchouk | 4a36b45 | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 86 | |
| 87 | void initAttribAndUniformArray(); |
| 88 | |
| 89 | mutable bool mDirty; |
| 90 | android::String8 mShader; |
| 91 | android::String8 mUserShader; |
Alex Sakhartchouk | 2123b46 | 2012-02-15 16:21:46 -0800 | [diff] [blame^] | 92 | android::String8 mShaderTextures; |
Alex Sakhartchouk | 4a36b45 | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 93 | uint32_t mShaderID; |
| 94 | uint32_t mType; |
| 95 | |
| 96 | uint32_t mTextureCount; |
Alex Sakhartchouk | bbc41c0 | 2011-08-05 15:27:25 -0700 | [diff] [blame] | 97 | uint32_t *mTextureTargets; |
Alex Sakhartchouk | 4a36b45 | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 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 | |
Alex Sakhartchouk | 2123b46 | 2012-02-15 16:21:46 -0800 | [diff] [blame^] | 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 ); |
Alex Sakhartchouk | 4a36b45 | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 111 | void initMemberVars(); |
Alex Sakhartchouk | 2123b46 | 2012-02-15 16:21:46 -0800 | [diff] [blame^] | 112 | void init(const char** textureNames, size_t textureNamesCount, |
| 113 | const size_t *textureNamesLength); |
Alex Sakhartchouk | 4a36b45 | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 114 | }; |
| 115 | |
| 116 | #endif //ANDROID_RSD_SHADER_H |
| 117 | |
| 118 | |
| 119 | |
| 120 | |