BsRenderer.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #include "BsRenderer.h"
  2. #include "BsCoreThread.h"
  3. #include "BsRenderSystem.h"
  4. #include "BsMaterialProxy.h"
  5. #include "BsMesh.h"
  6. #include "BsBlendState.h"
  7. #include "BsDepthStencilState.h"
  8. #include "BsRasterizerState.h"
  9. namespace BansheeEngine
  10. {
  11. void Renderer::setPass(const MaterialProxy& material, UINT32 passIdx)
  12. {
  13. THROW_IF_NOT_CORE_THREAD;
  14. RenderSystem& rs = RenderSystem::instance();
  15. const MaterialProxyPass& pass = material.passes[passIdx];
  16. if (pass.vertexProg)
  17. {
  18. rs.bindGpuProgram(pass.vertexProg);
  19. rs.bindGpuParams(GPT_VERTEX_PROGRAM, material.params[pass.vertexProgParamsIdx]);
  20. }
  21. else
  22. rs.unbindGpuProgram(GPT_VERTEX_PROGRAM);
  23. if (pass.fragmentProg)
  24. {
  25. rs.bindGpuProgram(pass.fragmentProg);
  26. rs.bindGpuParams(GPT_FRAGMENT_PROGRAM, material.params[pass.fragmentProgParamsIdx]);
  27. }
  28. else
  29. rs.unbindGpuProgram(GPT_FRAGMENT_PROGRAM);
  30. if (pass.geometryProg)
  31. {
  32. rs.bindGpuProgram(pass.geometryProg);
  33. rs.bindGpuParams(GPT_GEOMETRY_PROGRAM, material.params[pass.geometryProgParamsIdx]);
  34. }
  35. else
  36. rs.unbindGpuProgram(GPT_GEOMETRY_PROGRAM);
  37. if (pass.hullProg)
  38. {
  39. rs.bindGpuProgram(pass.hullProg);
  40. rs.bindGpuParams(GPT_HULL_PROGRAM, material.params[pass.hullProgParamsIdx]);
  41. }
  42. else
  43. rs.unbindGpuProgram(GPT_HULL_PROGRAM);
  44. if (pass.domainProg)
  45. {
  46. rs.bindGpuProgram(pass.domainProg);
  47. rs.bindGpuParams(GPT_DOMAIN_PROGRAM, material.params[pass.domainProgParamsIdx]);
  48. }
  49. else
  50. rs.unbindGpuProgram(GPT_DOMAIN_PROGRAM);
  51. if (pass.computeProg)
  52. {
  53. rs.bindGpuProgram(pass.computeProg);
  54. rs.bindGpuParams(GPT_COMPUTE_PROGRAM, material.params[pass.computeProgParamsIdx]);
  55. }
  56. else
  57. rs.unbindGpuProgram(GPT_COMPUTE_PROGRAM);
  58. // TODO - Try to limit amount of state changes, if previous state is already the same
  59. // Set up non-texture related pass settings
  60. if (pass.blendState != nullptr)
  61. rs.setBlendState(pass.blendState->getCore());
  62. else
  63. rs.setBlendState(BlendState::getDefault()->getCore());
  64. if (pass.depthStencilState != nullptr)
  65. rs.setDepthStencilState(pass.depthStencilState->getCore(), pass.stencilRefValue);
  66. else
  67. rs.setDepthStencilState(DepthStencilState::getDefault()->getCore(), pass.stencilRefValue);
  68. if (pass.rasterizerState != nullptr)
  69. rs.setRasterizerState(pass.rasterizerState->getCore());
  70. else
  71. rs.setRasterizerState(RasterizerState::getDefault()->getCore());
  72. }
  73. void Renderer::draw(const SPtr<MeshCoreBase>& mesh, const SubMesh& subMesh)
  74. {
  75. THROW_IF_NOT_CORE_THREAD;
  76. RenderSystem& rs = RenderSystem::instance();
  77. const MeshProperties& meshProps = mesh->getProperties();
  78. std::shared_ptr<VertexData> vertexData = mesh->getVertexData();
  79. rs.setVertexDeclaration(vertexData->vertexDeclaration);
  80. auto vertexBuffers = vertexData->getBuffers();
  81. if (vertexBuffers.size() > 0)
  82. {
  83. SPtr<VertexBufferCore> buffers[MAX_BOUND_VERTEX_BUFFERS];
  84. UINT32 endSlot = 0;
  85. UINT32 startSlot = MAX_BOUND_VERTEX_BUFFERS;
  86. for (auto iter = vertexBuffers.begin(); iter != vertexBuffers.end(); ++iter)
  87. {
  88. if (iter->first >= MAX_BOUND_VERTEX_BUFFERS)
  89. BS_EXCEPT(InvalidParametersException, "Buffer index out of range");
  90. startSlot = std::min(iter->first, startSlot);
  91. endSlot = std::max(iter->first, endSlot);
  92. }
  93. for (auto iter = vertexBuffers.begin(); iter != vertexBuffers.end(); ++iter)
  94. {
  95. buffers[iter->first - startSlot] = iter->second;
  96. }
  97. rs.setVertexBuffers(startSlot, buffers, endSlot - startSlot + 1);
  98. }
  99. rs.setDrawOperation(subMesh.drawOp);
  100. SPtr<IndexBufferCore> indexBuffer = mesh->getIndexBuffer();
  101. UINT32 indexCount = subMesh.indexCount;
  102. rs.setIndexBuffer(indexBuffer);
  103. rs.drawIndexed(subMesh.indexOffset + mesh->getIndexOffset(), indexCount, mesh->getVertexOffset(), vertexData->vertexCount);
  104. mesh->_notifyUsedOnGPU();
  105. }
  106. }