rsProgramFragment.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. #include "rsContext.h"
  17. #include "rsProgramFragment.h"
  18. #include <inttypes.h>
  19. namespace android {
  20. namespace renderscript {
  21. ProgramFragment::ProgramFragment(Context *rsc, const char * shaderText, size_t shaderLength,
  22. const char** textureNames, size_t textureNamesCount, const size_t *textureNamesLength,
  23. const uintptr_t * params, size_t paramLength)
  24. : Program(rsc, shaderText, shaderLength, params, paramLength) {
  25. mConstantColor[0] = 1.f;
  26. mConstantColor[1] = 1.f;
  27. mConstantColor[2] = 1.f;
  28. mConstantColor[3] = 1.f;
  29. mRSC->mHal.funcs.fragment.init(mRSC, this, mUserShader, mUserShaderLen,
  30. textureNames, textureNamesCount, textureNamesLength);
  31. }
  32. ProgramFragment::~ProgramFragment() {
  33. mRSC->mHal.funcs.fragment.destroy(mRSC, this);
  34. }
  35. void ProgramFragment::setConstantColor(Context *rsc, float r, float g, float b, float a) {
  36. if (isUserProgram()) {
  37. ALOGE("Attempting to set fixed function emulation color on user program");
  38. rsc->setError(RS_ERROR_BAD_SHADER, "Cannot set fixed function emulation color on user program");
  39. return;
  40. }
  41. if (mHal.state.constants[0] == nullptr) {
  42. ALOGE("Unable to set fixed function emulation color because allocation is missing");
  43. rsc->setError(RS_ERROR_BAD_SHADER, "Unable to set fixed function emulation color because allocation is missing");
  44. return;
  45. }
  46. mConstantColor[0] = r;
  47. mConstantColor[1] = g;
  48. mConstantColor[2] = b;
  49. mConstantColor[3] = a;
  50. void *p = rsc->mHal.funcs.allocation.lock1D(rsc, mHal.state.constants[0]);
  51. memcpy(p, mConstantColor, 4*sizeof(float));
  52. mDirty = true;
  53. rsc->mHal.funcs.allocation.unlock1D(rsc, mHal.state.constants[0]);
  54. }
  55. void ProgramFragment::setup(Context *rsc, ProgramFragmentState *state) {
  56. if ((state->mLast.get() == this) && !mDirty) {
  57. return;
  58. }
  59. state->mLast.set(this);
  60. for (uint32_t ct=0; ct < mHal.state.texturesCount; ct++) {
  61. if (!mHal.state.textures[ct]) {
  62. ALOGE("No texture bound for shader id %" PRIuPTR ", texture unit %u", (uintptr_t)this, ct);
  63. rsc->setError(RS_ERROR_BAD_SHADER, "No texture bound");
  64. continue;
  65. }
  66. }
  67. rsc->mHal.funcs.fragment.setActive(rsc, this);
  68. }
  69. void ProgramFragment::serialize(Context *rsc, OStream *stream) const {
  70. }
  71. ProgramFragment *ProgramFragment::createFromStream(Context *rsc, IStream *stream) {
  72. return nullptr;
  73. }
  74. ProgramFragmentState::ProgramFragmentState() {
  75. mPF = nullptr;
  76. }
  77. ProgramFragmentState::~ProgramFragmentState() {
  78. ObjectBase::checkDelete(mPF);
  79. mPF = nullptr;
  80. }
  81. void ProgramFragmentState::init(Context *rsc) {
  82. const char *shaderString =
  83. RS_SHADER_INTERNAL
  84. "varying lowp vec4 varColor;\n"
  85. "varying vec2 varTex0;\n"
  86. "void main() {\n"
  87. " lowp vec4 col = UNI_Color;\n"
  88. " gl_FragColor = col;\n"
  89. "}\n";
  90. ObjectBaseRef<const Element> colorElem = Element::createRef(rsc, RS_TYPE_FLOAT_32, RS_KIND_USER, false, 4);
  91. const char *enames[] = { "Color" };
  92. const Element *eins[] = {colorElem.get()};
  93. ObjectBaseRef<const Element> constInput = Element::create(rsc, 1, eins, enames);
  94. ObjectBaseRef<Type> inputType = Type::getTypeRef(rsc, constInput.get(), 1);
  95. uintptr_t tmp[2];
  96. tmp[0] = RS_PROGRAM_PARAM_CONSTANT;
  97. tmp[1] = (uintptr_t)inputType.get();
  98. Allocation *constAlloc = Allocation::createAllocation(rsc, inputType.get(),
  99. RS_ALLOCATION_USAGE_SCRIPT | RS_ALLOCATION_USAGE_GRAPHICS_CONSTANTS);
  100. ProgramFragment *pf = new ProgramFragment(rsc, shaderString, strlen(shaderString),
  101. nullptr, 0, nullptr, tmp, 2);
  102. pf->bindAllocation(rsc, constAlloc, 0);
  103. pf->setConstantColor(rsc, 1.0f, 1.0f, 1.0f, 1.0f);
  104. mDefault.set(pf);
  105. }
  106. void ProgramFragmentState::deinit(Context *rsc) {
  107. mDefault.clear();
  108. mLast.clear();
  109. }
  110. RsProgramFragment rsi_ProgramFragmentCreate(Context *rsc, const char * shaderText,
  111. size_t shaderLength,
  112. const char** textureNames,
  113. size_t textureNamesCount,
  114. const size_t *textureNamesLength,
  115. const uintptr_t * params, size_t paramLength) {
  116. ProgramFragment *pf = new ProgramFragment(rsc, shaderText, shaderLength,
  117. textureNames, textureNamesCount, textureNamesLength,
  118. params, paramLength);
  119. pf->incUserRef();
  120. //ALOGE("rsi_ProgramFragmentCreate %p", pf);
  121. return pf;
  122. }
  123. } // namespace renderscript
  124. } // namespace android