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_CACHE_H 18 #define ANDROID_RSD_SHADER_CACHE_H 19 20 namespace android { 21 namespace renderscript { 22 23 class Context; 24 25 } 26 } 27 28 #include <utils/String8.h> 29 #include <utils/Vector.h> 30 class RsdShader; 31 32 // --------------------------------------------------------------------------- 33 34 // An element is a group of Components that occupies one cell in a structure. 35 class RsdShaderCache { 36 public: 37 RsdShaderCache(); 38 virtual ~RsdShaderCache(); 39 40 void setActiveVertex(RsdShader *pv) { 41 mVertexDirty = true; 42 mVertex = pv; 43 } 44 45 void setActiveFragment(RsdShader *pf) { 46 mFragmentDirty = true; 47 mFragment = pf; 48 } 49 50 bool setup(const android::renderscript::Context *rsc); 51 52 void cleanupVertex(uint32_t id); 53 void cleanupFragment(uint32_t id); 54 55 void cleanupAll(); 56 57 int32_t vtxAttribSlot(const android::String8 &attrName) const; 58 int32_t vtxUniformSlot(uint32_t a) const {return mCurrent->vtxUniforms[a].slot;} 59 uint32_t vtxUniformSize(uint32_t a) const {return mCurrent->vtxUniforms[a].arraySize;} 60 int32_t fragUniformSlot(uint32_t a) const {return mCurrent->fragUniforms[a].slot;} 61 uint32_t fragUniformSize(uint32_t a) const {return mCurrent->fragUniforms[a].arraySize;} 62 63 protected: 64 bool link(const android::renderscript::Context *rsc); 65 bool mFragmentDirty; 66 bool mVertexDirty; 67 RsdShader *mVertex; 68 RsdShader *mFragment; 69 70 struct UniformQueryData { 71 char *name; 72 uint32_t nameLength; 73 int32_t writtenLength; 74 int32_t arraySize; 75 uint32_t type; 76 UniformQueryData(uint32_t maxName) { 77 name = NULL; 78 nameLength = maxName; 79 if (nameLength > 0 ) { 80 name = new char[nameLength]; 81 } 82 } 83 ~UniformQueryData() { 84 if (name != NULL) { 85 delete[] name; 86 name = NULL; 87 } 88 } 89 }; 90 struct UniformData { 91 int32_t slot; 92 uint32_t arraySize; 93 }; 94 struct AttrData { 95 int32_t slot; 96 const char* name; 97 }; 98 struct ProgramEntry { 99 ProgramEntry(uint32_t numVtxAttr, uint32_t numVtxUnis, 100 uint32_t numFragUnis) : vtx(0), frag(0), program(0), vtxAttrCount(0), 101 vtxAttrs(0), vtxUniforms(0), fragUniforms(0) { 102 vtxAttrCount = numVtxAttr; 103 if (numVtxAttr) { 104 vtxAttrs = new AttrData[numVtxAttr]; 105 } 106 if (numVtxUnis) { 107 vtxUniforms = new UniformData[numVtxUnis]; 108 } 109 if (numFragUnis) { 110 fragUniforms = new UniformData[numFragUnis]; 111 } 112 } 113 ~ProgramEntry() { 114 if (vtxAttrs) { 115 delete[] vtxAttrs; 116 vtxAttrs = NULL; 117 } 118 if (vtxUniforms) { 119 delete[] vtxUniforms; 120 vtxUniforms = NULL; 121 } 122 if (fragUniforms) { 123 delete[] fragUniforms; 124 fragUniforms = NULL; 125 } 126 } 127 uint32_t vtx; 128 uint32_t frag; 129 uint32_t program; 130 uint32_t vtxAttrCount; 131 AttrData *vtxAttrs; 132 UniformData *vtxUniforms; 133 UniformData *fragUniforms; 134 }; 135 android::Vector<ProgramEntry*> mEntries; 136 ProgramEntry *mCurrent; 137 138 bool hasArrayUniforms(RsdShader *vtx, RsdShader *frag); 139 void populateUniformData(RsdShader *prog, uint32_t linkedID, UniformData *data); 140 void updateUniformArrayData(const android::renderscript::Context *rsc, 141 RsdShader *prog, uint32_t linkedID, 142 UniformData *data, const char* logTag, 143 UniformQueryData **uniformList, uint32_t uniListSize); 144 }; 145 146 147 #endif //ANDROID_RSD_SHADER_CACHE_H 148 149 150 151 152