rsProgram.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. #include "rsContext.h"
  17. #include "rsProgram.h"
  18. #include <inttypes.h>
  19. namespace android {
  20. namespace renderscript {
  21. Program::Program(Context *rsc, const char * shaderText, size_t shaderLength,
  22. const uintptr_t * params, size_t paramLength)
  23. : ProgramBase(rsc) {
  24. initMemberVars();
  25. for (uint32_t ct=0; ct < paramLength; ct+=2) {
  26. if (params[ct] == RS_PROGRAM_PARAM_INPUT) {
  27. mHal.state.inputElementsCount++;
  28. }
  29. if (params[ct] == RS_PROGRAM_PARAM_CONSTANT) {
  30. mHal.state.constantsCount++;
  31. }
  32. if (params[ct] == RS_PROGRAM_PARAM_TEXTURE_TYPE) {
  33. mHal.state.texturesCount++;
  34. }
  35. }
  36. mTextures = new ObjectBaseRef<Allocation>[mHal.state.texturesCount];
  37. mSamplers = new ObjectBaseRef<Sampler>[mHal.state.texturesCount];
  38. mInputElements = new ObjectBaseRef<Element>[mHal.state.inputElementsCount];
  39. mConstantTypes = new ObjectBaseRef<Type>[mHal.state.constantsCount];
  40. mConstants = new ObjectBaseRef<Allocation>[mHal.state.constantsCount];
  41. mHal.state.textures = new Allocation*[mHal.state.texturesCount];
  42. mHal.state.samplers = new Sampler*[mHal.state.texturesCount];
  43. mHal.state.textureTargets = new RsTextureTarget[mHal.state.texturesCount];
  44. mHal.state.inputElements = new Element*[mHal.state.inputElementsCount];
  45. mHal.state.constantTypes = new Type*[mHal.state.constantsCount];
  46. mHal.state.constants = new Allocation*[mHal.state.constantsCount];
  47. // Will initialize everything
  48. freeChildren();
  49. uint32_t input = 0;
  50. uint32_t constant = 0;
  51. uint32_t texture = 0;
  52. for (uint32_t ct=0; ct < paramLength; ct+=2) {
  53. if (params[ct] == RS_PROGRAM_PARAM_INPUT) {
  54. mInputElements[input].set(reinterpret_cast<Element *>(params[ct+1]));
  55. mHal.state.inputElements[input++] = reinterpret_cast<Element *>(params[ct+1]);
  56. }
  57. if (params[ct] == RS_PROGRAM_PARAM_CONSTANT) {
  58. mConstantTypes[constant].set(reinterpret_cast<Type *>(params[ct+1]));
  59. mHal.state.constantTypes[constant++] = reinterpret_cast<Type *>(params[ct+1]);
  60. }
  61. if (params[ct] == RS_PROGRAM_PARAM_TEXTURE_TYPE) {
  62. mHal.state.textureTargets[texture++] = (RsTextureTarget)params[ct+1];
  63. }
  64. }
  65. mIsInternal = false;
  66. uint32_t internalTokenLen = strlen(RS_SHADER_INTERNAL);
  67. if (shaderLength > internalTokenLen &&
  68. strncmp(RS_SHADER_INTERNAL, shaderText, internalTokenLen) == 0) {
  69. mIsInternal = true;
  70. shaderText += internalTokenLen;
  71. shaderLength -= internalTokenLen;
  72. }
  73. mUserShader = rsuCopyString(shaderText, shaderLength);
  74. mUserShaderLen = shaderLength;
  75. }
  76. Program::~Program() {
  77. freeChildren();
  78. delete[] mTextures;
  79. delete[] mSamplers;
  80. delete[] mInputElements;
  81. delete[] mConstantTypes;
  82. delete[] mConstants;
  83. delete[] mHal.state.textures;
  84. delete[] mHal.state.samplers;
  85. delete[] mHal.state.textureTargets;
  86. delete[] mHal.state.inputElements;
  87. delete[] mHal.state.constantTypes;
  88. delete[] mHal.state.constants;
  89. mHal.state.inputElementsCount = 0;
  90. mHal.state.constantsCount = 0;
  91. mHal.state.texturesCount = 0;
  92. if (mUserShader != nullptr) {
  93. delete[] mUserShader;
  94. mUserShader = nullptr;
  95. }
  96. mUserShaderLen = 0;
  97. }
  98. bool Program::freeChildren() {
  99. for (uint32_t ct=0; ct < mHal.state.constantsCount; ct++) {
  100. bindAllocation(nullptr, nullptr, ct);
  101. }
  102. for (uint32_t ct=0; ct < mHal.state.texturesCount; ct++) {
  103. bindTexture(nullptr, ct, nullptr);
  104. bindSampler(nullptr, ct, nullptr);
  105. }
  106. return false;
  107. }
  108. void Program::initMemberVars() {
  109. mDirty = true;
  110. mHal.drv = nullptr;
  111. mHal.state.textures = nullptr;
  112. mHal.state.samplers = nullptr;
  113. mHal.state.textureTargets = nullptr;
  114. mHal.state.inputElements = nullptr;
  115. mHal.state.constantTypes = nullptr;
  116. mHal.state.constants = nullptr;
  117. mHal.state.inputElementsCount = 0;
  118. mHal.state.constantsCount = 0;
  119. mHal.state.texturesCount = 0;
  120. mTextures = nullptr;
  121. mSamplers = nullptr;
  122. mInputElements = nullptr;
  123. mConstantTypes = nullptr;
  124. mConstants = nullptr;
  125. mIsInternal = false;
  126. mUserShader = nullptr;
  127. mUserShaderLen = 0;
  128. }
  129. void Program::bindAllocation(Context *rsc, Allocation *alloc, uint32_t slot) {
  130. if (alloc != nullptr) {
  131. if (slot >= mHal.state.constantsCount) {
  132. ALOGE("Attempt to bind alloc at slot %u, on shader id %" PRIuPTR ", but const count is %u",
  133. slot, (uintptr_t)this, mHal.state.constantsCount);
  134. rsc->setError(RS_ERROR_BAD_SHADER, "Cannot bind allocation");
  135. return;
  136. }
  137. if (alloc->getType() != mConstantTypes[slot].get()) {
  138. ALOGE("Attempt to bind alloc at slot %u, on shader id %" PRIuPTR ", but types mismatch",
  139. slot, (uintptr_t)this);
  140. rsc->setError(RS_ERROR_BAD_SHADER, "Cannot bind allocation");
  141. return;
  142. }
  143. }
  144. if (mConstants[slot].get() == alloc) {
  145. return;
  146. }
  147. if (mConstants[slot].get()) {
  148. mConstants[slot]->removeProgramToDirty(this);
  149. }
  150. mConstants[slot].set(alloc);
  151. mHal.state.constants[slot] = alloc;
  152. if (alloc) {
  153. alloc->addProgramToDirty(this);
  154. }
  155. mDirty = true;
  156. }
  157. void Program::bindTexture(Context *rsc, uint32_t slot, Allocation *a) {
  158. if (slot >= mHal.state.texturesCount) {
  159. ALOGE("Attempt to bind texture to slot %u but tex count is %u", slot, mHal.state.texturesCount);
  160. rsc->setError(RS_ERROR_BAD_SHADER, "Cannot bind texture");
  161. return;
  162. }
  163. if (a && a->getType()->getDimFaces() && mHal.state.textureTargets[slot] != RS_TEXTURE_CUBE) {
  164. ALOGE("Attempt to bind cubemap to slot %u but 2d texture needed", slot);
  165. rsc->setError(RS_ERROR_BAD_SHADER, "Cannot bind cubemap to 2d texture slot");
  166. return;
  167. }
  168. mTextures[slot].set(a);
  169. mHal.state.textures[slot] = a;
  170. mDirty = true;
  171. }
  172. void Program::bindSampler(Context *rsc, uint32_t slot, Sampler *s) {
  173. if (slot >= mHal.state.texturesCount) {
  174. ALOGE("Attempt to bind sampler to slot %u but tex count is %u", slot, mHal.state.texturesCount);
  175. rsc->setError(RS_ERROR_BAD_SHADER, "Cannot bind sampler");
  176. return;
  177. }
  178. mSamplers[slot].set(s);
  179. mHal.state.samplers[slot] = s;
  180. mDirty = true;
  181. }
  182. void rsi_ProgramBindConstants(Context *rsc, RsProgram vp, uint32_t slot, RsAllocation constants) {
  183. Program *p = static_cast<Program *>(vp);
  184. p->bindAllocation(rsc, static_cast<Allocation *>(constants), slot);
  185. }
  186. void rsi_ProgramBindTexture(Context *rsc, RsProgram vpf, uint32_t slot, RsAllocation a) {
  187. Program *p = static_cast<Program *>(vpf);
  188. p->bindTexture(rsc, slot, static_cast<Allocation *>(a));
  189. }
  190. void rsi_ProgramBindSampler(Context *rsc, RsProgram vpf, uint32_t slot, RsSampler s) {
  191. Program *p = static_cast<Program *>(vpf);
  192. p->bindSampler(rsc, slot, static_cast<Sampler *>(s));
  193. }
  194. } // namespace renderscript
  195. } // namespace android