rsProgramVertex.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 "rsProgramVertex.h"
  18. #include "rsMatrix4x4.h"
  19. using android::renderscript::ProgramVertex;
  20. using android::renderscript::ProgramVertexState;
  21. ProgramVertex::ProgramVertex(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. mRSC->mHal.funcs.vertex.init(mRSC, this, mUserShader, mUserShaderLen,
  26. textureNames, textureNamesCount, textureNamesLength);
  27. }
  28. ProgramVertex::~ProgramVertex() {
  29. mRSC->mHal.funcs.vertex.destroy(mRSC, this);
  30. }
  31. void ProgramVertex::setup(Context *rsc, ProgramVertexState *state) {
  32. if ((state->mLast.get() == this) && !mDirty) {
  33. return;
  34. }
  35. if (!isUserProgram()) {
  36. if (mHal.state.constants[0] == nullptr) {
  37. rsc->setError(RS_ERROR_FATAL_UNKNOWN,
  38. "Unable to set fixed function emulation matrices because allocation is missing");
  39. return;
  40. }
  41. float *f = static_cast<float *>(rsc->mHal.funcs.allocation.lock1D(
  42. rsc, mHal.state.constants[0]));
  43. Matrix4x4 mvp;
  44. mvp.load(&f[RS_PROGRAM_VERTEX_PROJECTION_OFFSET]);
  45. Matrix4x4 t;
  46. t.load(&f[RS_PROGRAM_VERTEX_MODELVIEW_OFFSET]);
  47. mvp.multiply(&t);
  48. for (uint32_t i = 0; i < 16; i ++) {
  49. f[RS_PROGRAM_VERTEX_MVP_OFFSET + i] = mvp.m[i];
  50. }
  51. rsc->mHal.funcs.allocation.unlock1D(rsc, mHal.state.constants[0]);
  52. }
  53. state->mLast.set(this);
  54. rsc->mHal.funcs.vertex.setActive(rsc, this);
  55. }
  56. void ProgramVertex::setProjectionMatrix(Context *rsc, const rsc_Matrix *m) const {
  57. if (isUserProgram()) {
  58. rsc->setError(RS_ERROR_FATAL_UNKNOWN,
  59. "Attempting to set fixed function emulation matrix projection on user program");
  60. return;
  61. }
  62. if (mHal.state.constants[0] == nullptr) {
  63. rsc->setError(RS_ERROR_FATAL_UNKNOWN,
  64. "Unable to set fixed function emulation matrix projection because allocation is missing");
  65. return;
  66. }
  67. float *f = static_cast<float *>(rsc->mHal.funcs.allocation.lock1D(
  68. rsc, mHal.state.constants[0]));
  69. memcpy(&f[RS_PROGRAM_VERTEX_PROJECTION_OFFSET], m, sizeof(rsc_Matrix));
  70. mDirty = true;
  71. rsc->mHal.funcs.allocation.unlock1D(rsc, mHal.state.constants[0]);
  72. }
  73. void ProgramVertex::setModelviewMatrix(Context *rsc, const rsc_Matrix *m) const {
  74. if (isUserProgram()) {
  75. rsc->setError(RS_ERROR_FATAL_UNKNOWN,
  76. "Attempting to set fixed function emulation matrix modelview on user program");
  77. return;
  78. }
  79. if (mHal.state.constants[0] == nullptr) {
  80. rsc->setError(RS_ERROR_FATAL_UNKNOWN,
  81. "Unable to set fixed function emulation matrix modelview because allocation is missing");
  82. return;
  83. }
  84. float *f = static_cast<float *>(rsc->mHal.funcs.allocation.lock1D(
  85. rsc, mHal.state.constants[0]));
  86. memcpy(&f[RS_PROGRAM_VERTEX_MODELVIEW_OFFSET], m, sizeof(rsc_Matrix));
  87. mDirty = true;
  88. rsc->mHal.funcs.allocation.unlock1D(rsc, mHal.state.constants[0]);
  89. }
  90. void ProgramVertex::setTextureMatrix(Context *rsc, const rsc_Matrix *m) const {
  91. if (isUserProgram()) {
  92. rsc->setError(RS_ERROR_FATAL_UNKNOWN,
  93. "Attempting to set fixed function emulation matrix texture on user program");
  94. return;
  95. }
  96. if (mHal.state.constants[0] == nullptr) {
  97. rsc->setError(RS_ERROR_FATAL_UNKNOWN,
  98. "Unable to set fixed function emulation matrix texture because allocation is missing");
  99. return;
  100. }
  101. float *f = static_cast<float *>(rsc->mHal.funcs.allocation.lock1D(
  102. rsc, mHal.state.constants[0]));
  103. memcpy(&f[RS_PROGRAM_VERTEX_TEXTURE_OFFSET], m, sizeof(rsc_Matrix));
  104. mDirty = true;
  105. rsc->mHal.funcs.allocation.unlock1D(rsc, mHal.state.constants[0]);
  106. }
  107. void ProgramVertex::getProjectionMatrix(Context *rsc, rsc_Matrix *m) const {
  108. if (isUserProgram()) {
  109. rsc->setError(RS_ERROR_FATAL_UNKNOWN,
  110. "Attempting to get fixed function emulation matrix projection on user program");
  111. return;
  112. }
  113. if (mHal.state.constants[0] == nullptr) {
  114. rsc->setError(RS_ERROR_FATAL_UNKNOWN,
  115. "Unable to get fixed function emulation matrix projection because allocation is missing");
  116. return;
  117. }
  118. float *f = static_cast<float *>(
  119. rsc->mHal.funcs.allocation.lock1D(rsc, mHal.state.constants[0]));
  120. memcpy(m, &f[RS_PROGRAM_VERTEX_PROJECTION_OFFSET], sizeof(rsc_Matrix));
  121. rsc->mHal.funcs.allocation.unlock1D(rsc, mHal.state.constants[0]);
  122. }
  123. void ProgramVertex::transformToScreen(Context *rsc, float *v4out, const float *v3in) const {
  124. if (isUserProgram()) {
  125. return;
  126. }
  127. float *f = static_cast<float *>(
  128. rsc->mHal.funcs.allocation.lock1D(rsc, mHal.state.constants[0]));
  129. Matrix4x4 mvp;
  130. mvp.loadMultiply((Matrix4x4 *)&f[RS_PROGRAM_VERTEX_MODELVIEW_OFFSET],
  131. (Matrix4x4 *)&f[RS_PROGRAM_VERTEX_PROJECTION_OFFSET]);
  132. mvp.vectorMultiply(v4out, v3in);
  133. rsc->mHal.funcs.allocation.unlock1D(rsc, mHal.state.constants[0]);
  134. }
  135. void ProgramVertex::serialize(Context *rsc, OStream *stream) const {
  136. }
  137. ProgramVertex *ProgramVertex::createFromStream(Context *rsc, IStream *stream) {
  138. return nullptr;
  139. }
  140. ///////////////////////////////////////////////////////////////////////
  141. ProgramVertexState::ProgramVertexState() {
  142. }
  143. ProgramVertexState::~ProgramVertexState() {
  144. }
  145. void ProgramVertexState::init(Context *rsc) {
  146. ObjectBaseRef<const Element> matrixElem = Element::createRef(rsc, RS_TYPE_MATRIX_4X4,
  147. RS_KIND_USER, false, 1);
  148. ObjectBaseRef<const Element> f2Elem = Element::createRef(rsc, RS_TYPE_FLOAT_32,
  149. RS_KIND_USER, false, 2);
  150. ObjectBaseRef<const Element> f3Elem = Element::createRef(rsc, RS_TYPE_FLOAT_32,
  151. RS_KIND_USER, false, 3);
  152. ObjectBaseRef<const Element> f4Elem = Element::createRef(rsc, RS_TYPE_FLOAT_32,
  153. RS_KIND_USER, false, 4);
  154. const char *ebn1[] = { "MV", "P", "TexMatrix", "MVP" };
  155. const Element *ebe1[] = {matrixElem.get(), matrixElem.get(),
  156. matrixElem.get(), matrixElem.get()};
  157. ObjectBaseRef<const Element> constInput = Element::create(rsc, 4, ebe1, ebn1);
  158. const char *ebn2[] = { "position", "color", "normal", "texture0" };
  159. const Element *ebe2[] = {f4Elem.get(), f4Elem.get(), f3Elem.get(), f2Elem.get()};
  160. ObjectBaseRef<const Element> attrElem = Element::create(rsc, 4, ebe2, ebn2);
  161. ObjectBaseRef<Type> inputType = Type::getTypeRef(rsc, constInput.get(), 1);
  162. const char *shaderString =
  163. RS_SHADER_INTERNAL
  164. "varying vec4 varColor;\n"
  165. "varying vec2 varTex0;\n"
  166. "void main() {\n"
  167. " gl_Position = UNI_MVP * ATTRIB_position;\n"
  168. " gl_PointSize = 1.0;\n"
  169. " varColor = ATTRIB_color;\n"
  170. " varTex0 = ATTRIB_texture0;\n"
  171. "}\n";
  172. uintptr_t tmp[4];
  173. tmp[0] = RS_PROGRAM_PARAM_CONSTANT;
  174. tmp[1] = (uintptr_t)inputType.get();
  175. tmp[2] = RS_PROGRAM_PARAM_INPUT;
  176. tmp[3] = (uintptr_t)attrElem.get();
  177. ProgramVertex *pv = new ProgramVertex(rsc, shaderString, strlen(shaderString),
  178. nullptr, 0, nullptr, tmp, 4);
  179. Allocation *alloc = Allocation::createAllocation(rsc, inputType.get(),
  180. RS_ALLOCATION_USAGE_SCRIPT | RS_ALLOCATION_USAGE_GRAPHICS_CONSTANTS);
  181. pv->bindAllocation(rsc, alloc, 0);
  182. mDefaultAlloc.set(alloc);
  183. mDefault.set(pv);
  184. updateSize(rsc);
  185. }
  186. void ProgramVertexState::updateSize(Context *rsc) {
  187. float *f = static_cast<float *>(rsc->mHal.funcs.allocation.lock1D(rsc, mDefaultAlloc.get()));
  188. float surfaceWidth = (float)rsc->getCurrentSurfaceWidth();
  189. float surfaceHeight = (float)rsc->getCurrentSurfaceHeight();
  190. Matrix4x4 m;
  191. m.loadOrtho(0, surfaceWidth, surfaceHeight, 0, -1, 1);
  192. memcpy(&f[RS_PROGRAM_VERTEX_PROJECTION_OFFSET], m.m, sizeof(m));
  193. memcpy(&f[RS_PROGRAM_VERTEX_MVP_OFFSET], m.m, sizeof(m));
  194. m.loadIdentity();
  195. memcpy(&f[RS_PROGRAM_VERTEX_MODELVIEW_OFFSET], m.m, sizeof(m));
  196. memcpy(&f[RS_PROGRAM_VERTEX_TEXTURE_OFFSET], m.m, sizeof(m));
  197. rsc->mHal.funcs.allocation.unlock1D(rsc, mDefaultAlloc.get());
  198. }
  199. void ProgramVertexState::deinit(Context *rsc) {
  200. mDefaultAlloc.clear();
  201. mDefault.clear();
  202. mLast.clear();
  203. }
  204. namespace android {
  205. namespace renderscript {
  206. RsProgramVertex rsi_ProgramVertexCreate(Context *rsc, const char * shaderText, size_t shaderLength,
  207. const char** textureNames, size_t textureNamesCount,
  208. const size_t *textureNamesLength,
  209. const uintptr_t * params, size_t paramLength) {
  210. ProgramVertex *pv = new ProgramVertex(rsc, shaderText, shaderLength,
  211. textureNames, textureNamesCount, textureNamesLength,
  212. params, paramLength);
  213. pv->incUserRef();
  214. return pv;
  215. }
  216. } // namespace renderscript
  217. } // namespace android