blob: c93033b66306b64529a5e96d97079d303b7b2f75 [file] [log] [blame]
Jason Samsd19f10d2009-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 Samsbb51c402009-11-25 13:22:07 -080026class ShaderCache;
Jason Samsd19f10d2009-05-22 14:03:28 -070027
Alex Sakhartchoukc984dd72010-09-14 09:50:43 -070028#define RS_SHADER_INTERNAL "//rs_shader_internal\n"
Alex Sakhartchouk4378f112010-09-29 09:49:13 -070029#define RS_SHADER_ATTR "ATTRIB_"
30#define RS_SHADER_UNI "UNI_"
Alex Sakhartchoukc984dd72010-09-14 09:50:43 -070031
Jason Samsd19f10d2009-05-22 14:03:28 -070032class Program : public ObjectBase
33{
34public:
Jason Samsbb51c402009-11-25 13:22:07 -080035 const static uint32_t MAX_ATTRIBS = 8;
36 const static uint32_t MAX_UNIFORMS = 16;
37
Jason Sams0011bcf2009-12-15 12:58:36 -080038 Program(Context *);
39 Program(Context *, const char * shaderText, uint32_t shaderLength,
40 const uint32_t * params, uint32_t paramLength);
Jason Samsd19f10d2009-05-22 14:03:28 -070041 virtual ~Program();
42
Alex Sakhartchoukb89aaac2010-09-23 16:16:33 -070043 void bindAllocation(Context *, Allocation *, uint32_t slot);
Jason Samsbb51c402009-11-25 13:22:07 -080044 virtual void createShader();
45
Alex Sakhartchoukc984dd72010-09-14 09:50:43 -070046 bool isUserProgram() const {return !mIsInternal;}
Jason Samsa09a6e12010-01-06 11:57:52 -080047
Alex Sakhartchoukb89aaac2010-09-23 16:16:33 -070048 void bindTexture(Context *, uint32_t slot, Allocation *);
49 void bindSampler(Context *, uint32_t slot, Sampler *);
Jason Sams68afd012009-12-17 16:55:08 -080050
Jason Samsbb51c402009-11-25 13:22:07 -080051 uint32_t getShaderID() const {return mShaderID;}
Jason Sams54c0ec12009-11-30 14:49:55 -080052 void setShader(const char *, uint32_t len);
Jason Samsbb51c402009-11-25 13:22:07 -080053
54 uint32_t getAttribCount() const {return mAttribCount;}
55 uint32_t getUniformCount() const {return mUniformCount;}
56 const String8 & getAttribName(uint32_t i) const {return mAttribNames[i];}
57 const String8 & getUniformName(uint32_t i) const {return mUniformNames[i];}
Jason Sams9bee51c2009-08-05 13:57:03 -070058
Jason Samse17964e2010-01-04 16:52:27 -080059 String8 getGLSLInputString() const;
60 String8 getGLSLOutputString() const;
61 String8 getGLSLConstantString() const;
62
Jason Sams156cce62010-03-03 13:03:18 -080063 bool isValid() const {return mIsValid;}
64
Jason Samsd19f10d2009-05-22 14:03:28 -070065protected:
66 // Components not listed in "in" will be passed though
67 // unless overwritten by components in out.
Jason Sams0011bcf2009-12-15 12:58:36 -080068 ObjectBaseRef<Element> *mInputElements;
69 ObjectBaseRef<Element> *mOutputElements;
70 ObjectBaseRef<Type> *mConstantTypes;
71 uint32_t mInputCount;
72 uint32_t mOutputCount;
73 uint32_t mConstantCount;
Jason Sams156cce62010-03-03 13:03:18 -080074 bool mIsValid;
Alex Sakhartchoukc984dd72010-09-14 09:50:43 -070075 bool mIsInternal;
Jason Samsd19f10d2009-05-22 14:03:28 -070076
Alex Sakhartchouk8442e0b2010-08-31 12:02:01 -070077 // Applies to vertex and fragment shaders only
78 void appendUserConstants();
Alex Sakhartchoukb89aaac2010-09-23 16:16:33 -070079 void setupUserConstants(Context *rsc, ShaderCache *sc, bool isFragment);
Alex Sakhartchouk8442e0b2010-08-31 12:02:01 -070080 void initAddUserElement(const Element *e, String8 *names, uint32_t *count, const char *prefix);
81
Jason Samsea87e962010-01-12 12:12:28 -080082 ObjectBaseRef<Allocation> mConstants[MAX_UNIFORMS];
Jason Samsd19f10d2009-05-22 14:03:28 -070083
Jason Sams9bee51c2009-08-05 13:57:03 -070084 mutable bool mDirty;
Jason Samsbb51c402009-11-25 13:22:07 -080085 String8 mShader;
Jason Sams54c0ec12009-11-30 14:49:55 -080086 String8 mUserShader;
Jason Samsbb51c402009-11-25 13:22:07 -080087 uint32_t mShaderID;
Jason Sams741a6102009-10-15 18:45:45 -070088
Jason Sams7e5ab3b2009-12-15 13:27:04 -080089 uint32_t mTextureCount;
Jason Samsbb51c402009-11-25 13:22:07 -080090 uint32_t mAttribCount;
91 uint32_t mUniformCount;
92 String8 mAttribNames[MAX_ATTRIBS];
93 String8 mUniformNames[MAX_UNIFORMS];
94
Jason Sams68afd012009-12-17 16:55:08 -080095 // The difference between Textures and Constants is how they are accessed
96 // Texture lookups go though a sampler which in effect converts normalized
97 // coordinates into type specific. Multiple samples may also be taken
98 // and filtered.
99 //
100 // Constants are strictly accessed by programetic loads.
Alex Sakhartchoukb89aaac2010-09-23 16:16:33 -0700101 ObjectBaseRef<Allocation> *mTextures;
102 ObjectBaseRef<Sampler> *mSamplers;
Jason Sams68afd012009-12-17 16:55:08 -0800103
Jason Sams5dad8b42009-12-15 19:10:11 -0800104 bool loadShader(Context *, uint32_t type);
Jason Sams741a6102009-10-15 18:45:45 -0700105
106public:
Jason Sams83f1c632009-10-26 15:19:28 -0700107 void forceDirty() const {mDirty = true;}
Jason Samsd19f10d2009-05-22 14:03:28 -0700108};
109
110
111
112}
113}
114#endif
115
116
117