rsdShaderCache.h 4.7 KB

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