blob: 46692fdc0e4d624adb6906f9b1f5c848422d9dbc [file] [log] [blame]
Jason Sams326e0dd2009-05-22 14:03:28 -07001/*
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 {
Jason Samsc460e552009-11-25 13:22:07 -080026class ShaderCache;
Jason Sams326e0dd2009-05-22 14:03:28 -070027
Alex Sakhartchouke7ae69f2010-09-14 09:50:43 -070028#define RS_SHADER_INTERNAL "//rs_shader_internal\n"
Alex Sakhartchouk886f11a2010-09-29 09:49:13 -070029#define RS_SHADER_ATTR "ATTRIB_"
30#define RS_SHADER_UNI "UNI_"
Alex Sakhartchouke7ae69f2010-09-14 09:50:43 -070031
Jason Sams326e0dd2009-05-22 14:03:28 -070032class Program : public ObjectBase
33{
34public:
Jason Samsc460e552009-11-25 13:22:07 -080035
Jason Sams4815c0d2009-12-15 12:58:36 -080036 Program(Context *);
37 Program(Context *, const char * shaderText, uint32_t shaderLength,
38 const uint32_t * params, uint32_t paramLength);
Jason Sams326e0dd2009-05-22 14:03:28 -070039 virtual ~Program();
40
Alex Sakhartchouk383e5b12010-09-23 16:16:33 -070041 void bindAllocation(Context *, Allocation *, uint32_t slot);
Jason Samsc460e552009-11-25 13:22:07 -080042 virtual void createShader();
43
Alex Sakhartchouke7ae69f2010-09-14 09:50:43 -070044 bool isUserProgram() const {return !mIsInternal;}
Jason Sams433eca32010-01-06 11:57:52 -080045
Alex Sakhartchouk383e5b12010-09-23 16:16:33 -070046 void bindTexture(Context *, uint32_t slot, Allocation *);
47 void bindSampler(Context *, uint32_t slot, Sampler *);
Jason Sams7dad9c32009-12-17 16:55:08 -080048
Jason Samsc460e552009-11-25 13:22:07 -080049 uint32_t getShaderID() const {return mShaderID;}
Jason Samsf2a5d732009-11-30 14:49:55 -080050 void setShader(const char *, uint32_t len);
Jason Samsc460e552009-11-25 13:22:07 -080051
52 uint32_t getAttribCount() const {return mAttribCount;}
53 uint32_t getUniformCount() const {return mUniformCount;}
54 const String8 & getAttribName(uint32_t i) const {return mAttribNames[i];}
55 const String8 & getUniformName(uint32_t i) const {return mUniformNames[i];}
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -080056 uint32_t getUniformArraySize(uint32_t i) const {return mUniformArraySizes[i];}
Jason Samscfb1d112009-08-05 13:57:03 -070057
Jason Samsb4d35682010-01-04 16:52:27 -080058 String8 getGLSLInputString() const;
59 String8 getGLSLOutputString() const;
60 String8 getGLSLConstantString() const;
61
Jason Samsa2cf7552010-03-03 13:03:18 -080062 bool isValid() const {return mIsValid;}
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -080063 void forceDirty() const {mDirty = true;}
Jason Samsa2cf7552010-03-03 13:03:18 -080064
Jason Sams326e0dd2009-05-22 14:03:28 -070065protected:
66 // Components not listed in "in" will be passed though
67 // unless overwritten by components in out.
Jason Sams4815c0d2009-12-15 12:58:36 -080068 ObjectBaseRef<Element> *mInputElements;
69 ObjectBaseRef<Element> *mOutputElements;
70 ObjectBaseRef<Type> *mConstantTypes;
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -080071 ObjectBaseRef<Allocation> *mConstants;
Jason Sams4815c0d2009-12-15 12:58:36 -080072 uint32_t mInputCount;
73 uint32_t mOutputCount;
74 uint32_t mConstantCount;
Jason Samsa2cf7552010-03-03 13:03:18 -080075 bool mIsValid;
Alex Sakhartchouke7ae69f2010-09-14 09:50:43 -070076 bool mIsInternal;
Jason Sams326e0dd2009-05-22 14:03:28 -070077
Alex Sakhartchouk6e934212010-08-31 12:02:01 -070078 // Applies to vertex and fragment shaders only
79 void appendUserConstants();
Alex Sakhartchouk383e5b12010-09-23 16:16:33 -070080 void setupUserConstants(Context *rsc, ShaderCache *sc, bool isFragment);
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -080081 void initAddUserElement(const Element *e, String8 *names, uint32_t *arrayLengths, uint32_t *count, const char *prefix);
Alex Sakhartchouk6e934212010-08-31 12:02:01 -070082
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -080083 void initAttribAndUniformArray();
Jason Sams326e0dd2009-05-22 14:03:28 -070084
Jason Samscfb1d112009-08-05 13:57:03 -070085 mutable bool mDirty;
Jason Samsc460e552009-11-25 13:22:07 -080086 String8 mShader;
Jason Samsf2a5d732009-11-30 14:49:55 -080087 String8 mUserShader;
Jason Samsc460e552009-11-25 13:22:07 -080088 uint32_t mShaderID;
Jason Samsc2f94902009-10-15 18:45:45 -070089
Jason Samsf2e4fa22009-12-15 13:27:04 -080090 uint32_t mTextureCount;
Jason Samsc460e552009-11-25 13:22:07 -080091 uint32_t mAttribCount;
92 uint32_t mUniformCount;
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -080093 String8 *mAttribNames;
94 String8 *mUniformNames;
95 uint32_t *mUniformArraySizes;
96
97 void logUniform(const Element *field, const float *fd, uint32_t arraySize );
98 void setUniform(Context *rsc, const Element *field, const float *fd, int32_t slot, uint32_t arraySize );
99 void initMemberVars();
Jason Samsc460e552009-11-25 13:22:07 -0800100
Jason Sams7dad9c32009-12-17 16:55:08 -0800101 // The difference between Textures and Constants is how they are accessed
102 // Texture lookups go though a sampler which in effect converts normalized
103 // coordinates into type specific. Multiple samples may also be taken
104 // and filtered.
105 //
106 // Constants are strictly accessed by programetic loads.
Alex Sakhartchouk383e5b12010-09-23 16:16:33 -0700107 ObjectBaseRef<Allocation> *mTextures;
108 ObjectBaseRef<Sampler> *mSamplers;
Jason Sams7dad9c32009-12-17 16:55:08 -0800109
Jason Samscd506532009-12-15 19:10:11 -0800110 bool loadShader(Context *, uint32_t type);
Jason Sams326e0dd2009-05-22 14:03:28 -0700111};
112
113
114
115}
116}
117#endif
118
119
120