1 /* 2 * Copyright (C) 2011-2012 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 setActiveVertex(RsdShader * pv)40 void setActiveVertex(RsdShader *pv) { 41 mVertexDirty = true; 42 mVertex = pv; 43 } 44 setActiveFragment(RsdShader * pf)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(RsdShader *s); 53 void cleanupFragment(RsdShader *s); 54 55 void cleanupAll(); 56 57 int32_t vtxAttribSlot(const android::String8 &attrName) const; vtxUniformSlot(uint32_t a)58 int32_t vtxUniformSlot(uint32_t a) const {return mCurrent->vtxUniforms[a].slot;} vtxUniformSize(uint32_t a)59 uint32_t vtxUniformSize(uint32_t a) const {return mCurrent->vtxUniforms[a].arraySize;} fragUniformSlot(uint32_t a)60 int32_t fragUniformSlot(uint32_t a) const {return mCurrent->fragUniforms[a].slot;} fragUniformSize(uint32_t a)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; UniformQueryDataUniformQueryData76 UniformQueryData(uint32_t maxName) { 77 name = NULL; 78 nameLength = maxName; 79 if (nameLength > 0 ) { 80 name = new char[nameLength]; 81 } 82 } ~UniformQueryDataUniformQueryData83 ~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 { ProgramEntryProgramEntry99 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 fragUniformIsSTO(0) { 103 vtxAttrCount = numVtxAttr; 104 if (numVtxAttr) { 105 vtxAttrs = new AttrData[numVtxAttr]; 106 } 107 if (numVtxUnis) { 108 vtxUniforms = new UniformData[numVtxUnis]; 109 } 110 if (numFragUnis) { 111 fragUniforms = new UniformData[numFragUnis]; 112 fragUniformIsSTO = new bool[numFragUnis]; 113 } 114 } ~ProgramEntryProgramEntry115 ~ProgramEntry() { 116 if (vtxAttrs) { 117 delete[] vtxAttrs; 118 vtxAttrs = NULL; 119 } 120 if (vtxUniforms) { 121 delete[] vtxUniforms; 122 vtxUniforms = NULL; 123 } 124 if (fragUniforms) { 125 delete[] fragUniforms; 126 fragUniforms = NULL; 127 } 128 if (fragUniformIsSTO) { 129 delete[] fragUniformIsSTO; 130 fragUniformIsSTO = NULL; 131 } 132 } 133 uint32_t vtx; 134 uint32_t frag; 135 uint32_t program; 136 uint32_t vtxAttrCount; 137 AttrData *vtxAttrs; 138 UniformData *vtxUniforms; 139 UniformData *fragUniforms; 140 bool *fragUniformIsSTO; 141 }; 142 android::Vector<ProgramEntry*> mEntries; 143 ProgramEntry *mCurrent; 144 145 bool hasArrayUniforms(RsdShader *vtx, RsdShader *frag); 146 void populateUniformData(RsdShader *prog, uint32_t linkedID, UniformData *data); 147 void updateUniformArrayData(const android::renderscript::Context *rsc, 148 RsdShader *prog, uint32_t linkedID, 149 UniformData *data, const char* logTag, 150 UniformQueryData **uniformList, uint32_t uniListSize); 151 }; 152 153 154 #endif //ANDROID_RSD_SHADER_CACHE_H 155 156 157 158 159