Drawer.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. #include "anki/renderer/Drawer.h"
  2. #include "anki/resource/ShaderProgramResource.h"
  3. #include "anki/scene/Frustumable.h"
  4. #include "anki/resource/Material.h"
  5. #include "anki/scene/Renderable.h"
  6. #include "anki/scene/Camera.h"
  7. #include "anki/scene/ModelNode.h"
  8. #include "anki/resource/TextureResource.h"
  9. #include "anki/renderer/Renderer.h"
  10. namespace anki {
  11. //==============================================================================
  12. /// Visitor that sets a uniform
  13. struct SetupMaterialVariableVisitor
  14. {
  15. PassLevelKey key;
  16. const Frustumable* fr = nullptr;
  17. Renderer* r = nullptr;
  18. Renderable* renderable = nullptr;
  19. Array<U8, RenderableDrawer::UNIFORM_BLOCK_MAX_SIZE> clientBlock;
  20. RenderableMaterialVariable* rvar = nullptr;
  21. /// Set a uniform in a client block
  22. template<typename T>
  23. void uniSet(const ShaderProgramUniformVariable& uni, const T& value)
  24. {
  25. ANKI_ASSERT(0);
  26. }
  27. template<typename TProp>
  28. void visit(TProp& x)
  29. {
  30. const MaterialVariable& mv = rvar->getMaterialVariable();
  31. const ShaderProgramUniformVariable* uni =
  32. mv.findShaderProgramUniformVariable(key);
  33. if(!uni)
  34. {
  35. return;
  36. }
  37. // Set uniform
  38. //
  39. const Transform* rwtrf = renderable->getRenderableWorldTransform();
  40. Mat4 mMat = (rwtrf) ? Mat4(*rwtrf) : Mat4::getIdentity();
  41. const Mat4& vpMat = fr->getViewProjectionMatrix();
  42. Mat4 mvMat;
  43. Bool mvMatCalculated = false; // Opt
  44. switch(rvar->getBuildinId())
  45. {
  46. case BMV_NO_BUILDIN:
  47. uniSet(*uni, x);
  48. break;
  49. case BMV_MODEL_VIEW_PROJECTION_MATRIX:
  50. {
  51. Mat4 mvpMat = vpMat * mMat;
  52. uniSet(*uni, mvpMat);
  53. }
  54. break;
  55. case BMV_MODEL_VIEW_MATRIX:
  56. if(!mvMatCalculated)
  57. {
  58. mvMat = fr->getViewMatrix() * mMat;
  59. mvMatCalculated = true;
  60. }
  61. uniSet(*uni, mvMat);
  62. break;
  63. case BMV_NORMAL_MATRIX:
  64. if(!mvMatCalculated)
  65. {
  66. mvMat = fr->getViewMatrix() * mMat;
  67. mvMatCalculated = true;
  68. }
  69. uniSet(*uni, mvMat.getRotationPart());
  70. break;
  71. case BMV_INSTANCING_MODEL_VIEW_PROJECTION_MATRICES:
  72. {
  73. U32 instancesCount = renderable->getRenderableInstancesCount();
  74. Array<Mat4, 64> mvps;
  75. ANKI_ASSERT(mvps.getSize() >= instancesCount);
  76. const Transform* trfs =
  77. renderable->getRenderableInstancingWorldTransforms();
  78. ANKI_ASSERT(trfs != nullptr);
  79. for(U i = 0; i < instancesCount; i++)
  80. {
  81. mvps[i] = vpMat * Mat4(trfs[i]);
  82. }
  83. uni->set(&mvps[0], instancesCount);
  84. }
  85. break;
  86. case BMV_BLURRING:
  87. uniSet(*uni, 0.0);
  88. break;
  89. default:
  90. ANKI_ASSERT(0);
  91. break;
  92. }
  93. }
  94. };
  95. /// Specialize the material accepted types. The un-specialized will be used for
  96. /// all Property types like strings, we don't need strings in our case
  97. #define TEMPLATE_SPECIALIZATION(type) \
  98. template<> \
  99. void SetupMaterialVariableVisitor::uniSet<type>( \
  100. const ShaderProgramUniformVariable& uni, const type& value) \
  101. { \
  102. if(uni.getUniformBlock()) \
  103. { \
  104. uni.setClientMemory(&clientBlock[0], \
  105. RenderableDrawer::UNIFORM_BLOCK_MAX_SIZE, \
  106. &value, 1); \
  107. } \
  108. else \
  109. { \
  110. uni.set(value); \
  111. } \
  112. }
  113. TEMPLATE_SPECIALIZATION(F32)
  114. TEMPLATE_SPECIALIZATION(Vec2)
  115. TEMPLATE_SPECIALIZATION(Vec3)
  116. TEMPLATE_SPECIALIZATION(Vec4)
  117. TEMPLATE_SPECIALIZATION(Mat3)
  118. TEMPLATE_SPECIALIZATION(Mat4)
  119. // Texture specialization
  120. template<>
  121. void SetupMaterialVariableVisitor::uniSet<TextureResourcePointer>(
  122. const ShaderProgramUniformVariable& uni,
  123. const TextureResourcePointer& value)
  124. {
  125. const Texture* tex = value.get();
  126. uni.set(*tex);
  127. }
  128. //==============================================================================
  129. void RenderableDrawer::setupShaderProg(
  130. const PassLevelKey& key,
  131. const Frustumable& fr,
  132. Renderable& renderable)
  133. {
  134. const Material& mtl = renderable.getRenderableMaterial();
  135. const ShaderProgram& sprog = mtl.findShaderProgram(key);
  136. sprog.bind();
  137. SetupMaterialVariableVisitor vis;
  138. vis.fr = &fr;
  139. vis.key = key;
  140. vis.renderable = &renderable;
  141. vis.r = r;
  142. // Set the uniforms
  143. for(auto it = renderable.getVariablesBegin();
  144. it != renderable.getVariablesEnd(); ++it)
  145. {
  146. RenderableMaterialVariable* rvar = *it;
  147. vis.rvar = rvar;
  148. rvar->getMaterialVariable().acceptVisitor(vis);
  149. }
  150. // Write the block
  151. const ShaderProgramUniformBlock* block = mtl.getCommonUniformBlock();
  152. if(block)
  153. {
  154. ANKI_ASSERT(block->getSize() <= UNIFORM_BLOCK_MAX_SIZE);
  155. ANKI_ASSERT(block->getBinding() == 0);
  156. renderable.getUbo().write(&vis.clientBlock[0]);
  157. renderable.getUbo().setBinding(0);
  158. }
  159. }
  160. //==============================================================================
  161. void RenderableDrawer::render(const Frustumable& fr, RenderingStage stage,
  162. U32 pass, Renderable& renderable)
  163. {
  164. const Material& mtl = renderable.getRenderableMaterial();
  165. Bool blending = mtl.isBlendingEnabled();
  166. if(blending)
  167. {
  168. if(stage != RS_BLEND)
  169. {
  170. return;
  171. }
  172. GlStateSingleton::get().setBlendFunctions(
  173. mtl.getBlendingSfactor(), mtl.getBlendingDfactor());
  174. }
  175. else
  176. {
  177. if(stage == RS_BLEND)
  178. {
  179. return;
  180. }
  181. }
  182. GlStateSingleton::get().enable(GL_BLEND, blending);
  183. /*float dist = (node.getWorldTransform().getOrigin() -
  184. cam.getWorldTransform().getOrigin()).getLength();
  185. uint lod = std::min(r.calculateLod(dist), mtl.getLevelsOfDetail() - 1);*/
  186. U32 instancesCount = renderable.getRenderableInstancesCount();
  187. if(instancesCount < 1)
  188. {
  189. return;
  190. }
  191. PassLevelKey key(pass, 0);
  192. // Setup shader
  193. setupShaderProg(key, fr, renderable);
  194. // Render
  195. U32 indicesCount =
  196. renderable.getRenderableModelPatchBase().getIndecesCount(0);
  197. const Vao& vao = renderable.getRenderableModelPatchBase().getVao(key);
  198. ANKI_ASSERT(vao.getAttachmentsCount() > 1);
  199. vao.bind();
  200. if(instancesCount == 1)
  201. {
  202. glDrawElements(GL_TRIANGLES, indicesCount, GL_UNSIGNED_SHORT, 0);
  203. }
  204. else
  205. {
  206. glDrawElementsInstanced(
  207. GL_TRIANGLES, indicesCount, GL_UNSIGNED_SHORT, 0, instancesCount);
  208. }
  209. }
  210. } // end namespace anki