Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 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 | // --------------------------------------------------------------------------- |
| 24 | namespace android { |
| 25 | namespace renderscript { |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 26 | class ShaderCache; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 27 | |
Alex Sakhartchouk | c984dd7 | 2010-09-14 09:50:43 -0700 | [diff] [blame] | 28 | #define RS_SHADER_INTERNAL "//rs_shader_internal\n" |
Alex Sakhartchouk | 4378f11 | 2010-09-29 09:49:13 -0700 | [diff] [blame] | 29 | #define RS_SHADER_ATTR "ATTRIB_" |
| 30 | #define RS_SHADER_UNI "UNI_" |
Alex Sakhartchouk | c984dd7 | 2010-09-14 09:50:43 -0700 | [diff] [blame] | 31 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 32 | class Program : public ObjectBase { |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 33 | public: |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 34 | |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 35 | Program(Context *); |
| 36 | Program(Context *, const char * shaderText, uint32_t shaderLength, |
| 37 | const uint32_t * params, uint32_t paramLength); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 38 | virtual ~Program(); |
| 39 | |
Alex Sakhartchouk | b89aaac | 2010-09-23 16:16:33 -0700 | [diff] [blame] | 40 | void bindAllocation(Context *, Allocation *, uint32_t slot); |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 41 | virtual void createShader(); |
| 42 | |
Alex Sakhartchouk | c984dd7 | 2010-09-14 09:50:43 -0700 | [diff] [blame] | 43 | bool isUserProgram() const {return !mIsInternal;} |
Jason Sams | a09a6e1 | 2010-01-06 11:57:52 -0800 | [diff] [blame] | 44 | |
Alex Sakhartchouk | b89aaac | 2010-09-23 16:16:33 -0700 | [diff] [blame] | 45 | void bindTexture(Context *, uint32_t slot, Allocation *); |
| 46 | void bindSampler(Context *, uint32_t slot, Sampler *); |
Jason Sams | 68afd01 | 2009-12-17 16:55:08 -0800 | [diff] [blame] | 47 | |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 48 | uint32_t getShaderID() const {return mShaderID;} |
Jason Sams | 54c0ec1 | 2009-11-30 14:49:55 -0800 | [diff] [blame] | 49 | void setShader(const char *, uint32_t len); |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 50 | |
| 51 | uint32_t getAttribCount() const {return mAttribCount;} |
| 52 | uint32_t getUniformCount() const {return mUniformCount;} |
| 53 | const String8 & getAttribName(uint32_t i) const {return mAttribNames[i];} |
| 54 | const String8 & getUniformName(uint32_t i) const {return mUniformNames[i];} |
Alex Sakhartchouk | 9d71e21 | 2010-11-08 15:10:52 -0800 | [diff] [blame] | 55 | uint32_t getUniformArraySize(uint32_t i) const {return mUniformArraySizes[i];} |
Jason Sams | 9bee51c | 2009-08-05 13:57:03 -0700 | [diff] [blame] | 56 | |
Jason Sams | e17964e | 2010-01-04 16:52:27 -0800 | [diff] [blame] | 57 | String8 getGLSLInputString() const; |
| 58 | String8 getGLSLOutputString() const; |
| 59 | String8 getGLSLConstantString() const; |
| 60 | |
Jason Sams | 156cce6 | 2010-03-03 13:03:18 -0800 | [diff] [blame] | 61 | bool isValid() const {return mIsValid;} |
Alex Sakhartchouk | 9d71e21 | 2010-11-08 15:10:52 -0800 | [diff] [blame] | 62 | void forceDirty() const {mDirty = true;} |
Jason Sams | 156cce6 | 2010-03-03 13:03:18 -0800 | [diff] [blame] | 63 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 64 | protected: |
| 65 | // Components not listed in "in" will be passed though |
| 66 | // unless overwritten by components in out. |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 67 | ObjectBaseRef<Element> *mInputElements; |
| 68 | ObjectBaseRef<Element> *mOutputElements; |
| 69 | ObjectBaseRef<Type> *mConstantTypes; |
Alex Sakhartchouk | 9d71e21 | 2010-11-08 15:10:52 -0800 | [diff] [blame] | 70 | ObjectBaseRef<Allocation> *mConstants; |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 71 | uint32_t mInputCount; |
| 72 | uint32_t mOutputCount; |
| 73 | uint32_t mConstantCount; |
Jason Sams | 156cce6 | 2010-03-03 13:03:18 -0800 | [diff] [blame] | 74 | bool mIsValid; |
Alex Sakhartchouk | c984dd7 | 2010-09-14 09:50:43 -0700 | [diff] [blame] | 75 | bool mIsInternal; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 76 | |
Alex Sakhartchouk | 8442e0b | 2010-08-31 12:02:01 -0700 | [diff] [blame] | 77 | // Applies to vertex and fragment shaders only |
| 78 | void appendUserConstants(); |
Alex Sakhartchouk | b89aaac | 2010-09-23 16:16:33 -0700 | [diff] [blame] | 79 | void setupUserConstants(Context *rsc, ShaderCache *sc, bool isFragment); |
Alex Sakhartchouk | 9d71e21 | 2010-11-08 15:10:52 -0800 | [diff] [blame] | 80 | void initAddUserElement(const Element *e, String8 *names, uint32_t *arrayLengths, uint32_t *count, const char *prefix); |
Alex Sakhartchouk | 8442e0b | 2010-08-31 12:02:01 -0700 | [diff] [blame] | 81 | |
Alex Sakhartchouk | 9d71e21 | 2010-11-08 15:10:52 -0800 | [diff] [blame] | 82 | void initAttribAndUniformArray(); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 83 | |
Jason Sams | 9bee51c | 2009-08-05 13:57:03 -0700 | [diff] [blame] | 84 | mutable bool mDirty; |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 85 | String8 mShader; |
Jason Sams | 54c0ec1 | 2009-11-30 14:49:55 -0800 | [diff] [blame] | 86 | String8 mUserShader; |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 87 | uint32_t mShaderID; |
Jason Sams | 741a610 | 2009-10-15 18:45:45 -0700 | [diff] [blame] | 88 | |
Jason Sams | 7e5ab3b | 2009-12-15 13:27:04 -0800 | [diff] [blame] | 89 | uint32_t mTextureCount; |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 90 | uint32_t mAttribCount; |
| 91 | uint32_t mUniformCount; |
Alex Sakhartchouk | 9d71e21 | 2010-11-08 15:10:52 -0800 | [diff] [blame] | 92 | String8 *mAttribNames; |
| 93 | String8 *mUniformNames; |
| 94 | uint32_t *mUniformArraySizes; |
| 95 | |
| 96 | void logUniform(const Element *field, const float *fd, uint32_t arraySize ); |
| 97 | void setUniform(Context *rsc, const Element *field, const float *fd, int32_t slot, uint32_t arraySize ); |
| 98 | void initMemberVars(); |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 99 | |
Jason Sams | 68afd01 | 2009-12-17 16:55:08 -0800 | [diff] [blame] | 100 | // The difference between Textures and Constants is how they are accessed |
| 101 | // Texture lookups go though a sampler which in effect converts normalized |
| 102 | // coordinates into type specific. Multiple samples may also be taken |
| 103 | // and filtered. |
| 104 | // |
| 105 | // Constants are strictly accessed by programetic loads. |
Alex Sakhartchouk | b89aaac | 2010-09-23 16:16:33 -0700 | [diff] [blame] | 106 | ObjectBaseRef<Allocation> *mTextures; |
| 107 | ObjectBaseRef<Sampler> *mSamplers; |
Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 108 | RsTextureTarget *mTextureTargets; |
Jason Sams | 5dad8b4 | 2009-12-15 19:10:11 -0800 | [diff] [blame] | 109 | bool loadShader(Context *, uint32_t type); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 110 | }; |
| 111 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 112 | } |
| 113 | } |
| 114 | #endif |
| 115 | |
| 116 | |
| 117 | |