Jason Sams | c460e55 | 2009-11-25 13:22:07 -0800 | [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_SHADER_CACHE_H |
| 18 | #define ANDROID_SHADER_CACHE_H |
| 19 | |
| 20 | |
| 21 | #include "rsObjectBase.h" |
| 22 | #include "rsVertexArray.h" |
| 23 | |
| 24 | // --------------------------------------------------------------------------- |
| 25 | namespace android { |
| 26 | namespace renderscript { |
| 27 | |
| 28 | |
| 29 | // An element is a group of Components that occupies one cell in a structure. |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 30 | class ShaderCache { |
Jason Sams | c460e55 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 31 | public: |
| 32 | ShaderCache(); |
| 33 | virtual ~ShaderCache(); |
| 34 | |
Jason Sams | cd50653 | 2009-12-15 19:10:11 -0800 | [diff] [blame] | 35 | bool lookup(Context *rsc, ProgramVertex *, ProgramFragment *); |
Jason Sams | c460e55 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 36 | |
| 37 | void cleanupVertex(uint32_t id); |
| 38 | void cleanupFragment(uint32_t id); |
| 39 | |
| 40 | void cleanupAll(); |
| 41 | |
Alex Sakhartchouk | 886f11a | 2010-09-29 09:49:13 -0700 | [diff] [blame] | 42 | int32_t vtxAttribSlot(const String8 &attrName) const; |
Alex Sakhartchouk | 54929cc | 2010-11-08 15:10:52 -0800 | [diff] [blame] | 43 | int32_t vtxUniformSlot(uint32_t a) const {return mCurrent->vtxUniforms[a].slot;} |
| 44 | uint32_t vtxUniformSize(uint32_t a) const {return mCurrent->vtxUniforms[a].arraySize;} |
| 45 | int32_t fragUniformSlot(uint32_t a) const {return mCurrent->fragUniforms[a].slot;} |
| 46 | uint32_t fragUniformSize(uint32_t a) const {return mCurrent->fragUniforms[a].arraySize;} |
Jason Sams | c460e55 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 47 | |
| 48 | protected: |
Alex Sakhartchouk | 54929cc | 2010-11-08 15:10:52 -0800 | [diff] [blame] | 49 | struct UniformQueryData { |
| 50 | char *name; |
| 51 | uint32_t nameLength; |
| 52 | int32_t writtenLength; |
| 53 | int32_t arraySize; |
| 54 | uint32_t type; |
| 55 | UniformQueryData(uint32_t maxName) { |
| 56 | name = NULL; |
| 57 | nameLength = maxName; |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 58 | if (nameLength > 0 ) { |
Alex Sakhartchouk | 54929cc | 2010-11-08 15:10:52 -0800 | [diff] [blame] | 59 | name = new char[nameLength]; |
| 60 | } |
| 61 | } |
| 62 | ~UniformQueryData() { |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 63 | if (name != NULL) { |
Alex Sakhartchouk | 54929cc | 2010-11-08 15:10:52 -0800 | [diff] [blame] | 64 | delete[] name; |
| 65 | name = NULL; |
| 66 | } |
| 67 | } |
| 68 | }; |
| 69 | struct UniformData { |
| 70 | int32_t slot; |
| 71 | uint32_t arraySize; |
| 72 | }; |
| 73 | struct AttrData { |
| 74 | int32_t slot; |
| 75 | const char* name; |
| 76 | }; |
| 77 | struct ProgramEntry { |
| 78 | ProgramEntry(uint32_t numVtxAttr, uint32_t numVtxUnis, |
| 79 | uint32_t numFragUnis) : vtx(0), frag(0), program(0), vtxAttrCount(0), |
| 80 | vtxAttrs(0), vtxUniforms(0), fragUniforms(0) { |
| 81 | vtxAttrCount = numVtxAttr; |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 82 | if (numVtxAttr) { |
Alex Sakhartchouk | 54929cc | 2010-11-08 15:10:52 -0800 | [diff] [blame] | 83 | vtxAttrs = new AttrData[numVtxAttr]; |
| 84 | } |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 85 | if (numVtxUnis) { |
Alex Sakhartchouk | 54929cc | 2010-11-08 15:10:52 -0800 | [diff] [blame] | 86 | vtxUniforms = new UniformData[numVtxUnis]; |
| 87 | } |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 88 | if (numFragUnis) { |
Alex Sakhartchouk | 54929cc | 2010-11-08 15:10:52 -0800 | [diff] [blame] | 89 | fragUniforms = new UniformData[numFragUnis]; |
| 90 | } |
| 91 | } |
| 92 | ~ProgramEntry() { |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 93 | if (vtxAttrs) { |
Alex Sakhartchouk | 54929cc | 2010-11-08 15:10:52 -0800 | [diff] [blame] | 94 | delete[] vtxAttrs; |
| 95 | vtxAttrs = NULL; |
| 96 | } |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 97 | if (vtxUniforms) { |
Alex Sakhartchouk | 54929cc | 2010-11-08 15:10:52 -0800 | [diff] [blame] | 98 | delete[] vtxUniforms; |
| 99 | vtxUniforms = NULL; |
| 100 | } |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 101 | if (fragUniforms) { |
Alex Sakhartchouk | 54929cc | 2010-11-08 15:10:52 -0800 | [diff] [blame] | 102 | delete[] fragUniforms; |
| 103 | fragUniforms = NULL; |
| 104 | } |
| 105 | } |
Jason Sams | c460e55 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 106 | uint32_t vtx; |
| 107 | uint32_t frag; |
| 108 | uint32_t program; |
Alex Sakhartchouk | 886f11a | 2010-09-29 09:49:13 -0700 | [diff] [blame] | 109 | uint32_t vtxAttrCount; |
Alex Sakhartchouk | 54929cc | 2010-11-08 15:10:52 -0800 | [diff] [blame] | 110 | AttrData *vtxAttrs; |
| 111 | UniformData *vtxUniforms; |
| 112 | UniformData *fragUniforms; |
Alex Sakhartchouk | 54929cc | 2010-11-08 15:10:52 -0800 | [diff] [blame] | 113 | }; |
| 114 | Vector<ProgramEntry*> mEntries; |
| 115 | ProgramEntry *mCurrent; |
Jason Sams | c460e55 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 116 | |
Alex Sakhartchouk | 54929cc | 2010-11-08 15:10:52 -0800 | [diff] [blame] | 117 | bool hasArrayUniforms(ProgramVertex *vtx, ProgramFragment *frag); |
| 118 | void populateUniformData(Program *prog, uint32_t linkedID, UniformData *data); |
| 119 | void updateUniformArrayData(Context *rsc, Program *prog, uint32_t linkedID, |
| 120 | UniformData *data, const char* logTag, |
| 121 | UniformQueryData **uniformList, uint32_t uniListSize); |
Jason Sams | c460e55 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 122 | }; |
| 123 | |
| 124 | |
| 125 | |
| 126 | } |
| 127 | } |
| 128 | #endif //ANDROID_SHADER_CACHE_H |
| 129 | |
| 130 | |
| 131 | |
| 132 | |