/libs/rs/driver/rsdVertexArray.cpp

http://github.com/CyanogenMod/android_frameworks_base · C++ · 138 lines · 105 code · 17 blank · 16 comment · 10 complexity · 2276587d44967027b5427a7dff43a77f MD5 · raw file

  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 <rs_hal.h>
  17. #include <rsContext.h>
  18. #include <GLES/gl.h>
  19. #include <GLES2/gl2.h>
  20. #include "rsdGL.h"
  21. #include "rsdCore.h"
  22. #include "rsdVertexArray.h"
  23. #include "rsdShaderCache.h"
  24. using namespace android;
  25. using namespace android::renderscript;
  26. RsdVertexArray::RsdVertexArray(const Attrib *attribs, uint32_t numAttribs) {
  27. mAttribs = attribs;
  28. mCount = numAttribs;
  29. }
  30. RsdVertexArray::~RsdVertexArray() {
  31. }
  32. RsdVertexArray::Attrib::Attrib() {
  33. clear();
  34. }
  35. void RsdVertexArray::Attrib::clear() {
  36. buffer = 0;
  37. offset = 0;
  38. type = 0;
  39. size = 0;
  40. stride = 0;
  41. ptr = NULL;
  42. normalized = false;
  43. name.setTo("");
  44. }
  45. void RsdVertexArray::Attrib::set(uint32_t type, uint32_t size, uint32_t stride,
  46. bool normalized, uint32_t offset,
  47. const char *name) {
  48. clear();
  49. this->type = type;
  50. this->size = size;
  51. this->offset = offset;
  52. this->normalized = normalized;
  53. this->stride = stride;
  54. this->name.setTo(name);
  55. }
  56. void RsdVertexArray::logAttrib(uint32_t idx, uint32_t slot) const {
  57. if (idx == 0) {
  58. LOGV("Starting vertex attribute binding");
  59. }
  60. LOGV("va %i: slot=%i name=%s buf=%i ptr=%p size=%i type=0x%x stride=0x%x norm=%i offset=0x%x",
  61. idx, slot,
  62. mAttribs[idx].name.string(),
  63. mAttribs[idx].buffer,
  64. mAttribs[idx].ptr,
  65. mAttribs[idx].size,
  66. mAttribs[idx].type,
  67. mAttribs[idx].stride,
  68. mAttribs[idx].normalized,
  69. mAttribs[idx].offset);
  70. }
  71. void RsdVertexArray::setup(const Context *rsc) const {
  72. RsdHal *dc = (RsdHal *)rsc->mHal.drv;
  73. RsdVertexArrayState *state = dc->gl.vertexArrayState;
  74. RsdShaderCache *sc = dc->gl.shaderCache;
  75. rsdGLCheckError(rsc, "RsdVertexArray::setup start");
  76. uint32_t maxAttrs = state->mAttrsEnabledSize;
  77. for (uint32_t ct=1; ct < maxAttrs; ct++) {
  78. if(state->mAttrsEnabled[ct]) {
  79. glDisableVertexAttribArray(ct);
  80. state->mAttrsEnabled[ct] = false;
  81. }
  82. }
  83. rsdGLCheckError(rsc, "RsdVertexArray::setup disabled");
  84. for (uint32_t ct=0; ct < mCount; ct++) {
  85. int32_t slot = sc->vtxAttribSlot(mAttribs[ct].name);
  86. if (rsc->props.mLogShadersAttr) {
  87. logAttrib(ct, slot);
  88. }
  89. if (slot < 0 || slot >= (int32_t)maxAttrs) {
  90. continue;
  91. }
  92. glEnableVertexAttribArray(slot);
  93. state->mAttrsEnabled[slot] = true;
  94. glBindBuffer(GL_ARRAY_BUFFER, mAttribs[ct].buffer);
  95. glVertexAttribPointer(slot,
  96. mAttribs[ct].size,
  97. mAttribs[ct].type,
  98. mAttribs[ct].normalized,
  99. mAttribs[ct].stride,
  100. mAttribs[ct].ptr + mAttribs[ct].offset);
  101. }
  102. rsdGLCheckError(rsc, "RsdVertexArray::setup done");
  103. }
  104. ////////////////////////////////////////////
  105. RsdVertexArrayState::RsdVertexArrayState() {
  106. mAttrsEnabled = NULL;
  107. mAttrsEnabledSize = 0;
  108. }
  109. RsdVertexArrayState::~RsdVertexArrayState() {
  110. if (mAttrsEnabled) {
  111. delete[] mAttrsEnabled;
  112. mAttrsEnabled = NULL;
  113. }
  114. }
  115. void RsdVertexArrayState::init(uint32_t maxAttrs) {
  116. mAttrsEnabledSize = maxAttrs;
  117. mAttrsEnabled = new bool[mAttrsEnabledSize];
  118. for (uint32_t ct = 0; ct < mAttrsEnabledSize; ct++) {
  119. mAttrsEnabled[ct] = false;
  120. }
  121. }