BsGLSLProgramPipelineManager.cpp 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "GLSL/BsGLSLProgramPipelineManager.h"
  4. #include "GLSL/BsGLSLGpuProgram.h"
  5. #include "Profiling/BsRenderStats.h"
  6. namespace bs { namespace ct
  7. {
  8. ::std::size_t GLSLProgramPipelineManager::ProgramPipelineKeyHashFunction::operator()
  9. (const GLSLProgramPipelineManager::ProgramPipelineKey &key) const
  10. {
  11. std::size_t seed = 0;
  12. hash_combine(seed, key.vertexProgKey);
  13. hash_combine(seed, key.fragmentProgKey);
  14. hash_combine(seed, key.geometryProgKey);
  15. hash_combine(seed, key.hullProgKey);
  16. hash_combine(seed, key.domainProgKey);
  17. return seed;
  18. }
  19. bool GLSLProgramPipelineManager::ProgramPipelineKeyEqual::operator()
  20. (const GLSLProgramPipelineManager::ProgramPipelineKey &a, const GLSLProgramPipelineManager::ProgramPipelineKey &b) const
  21. {
  22. return a.vertexProgKey == b.vertexProgKey && a.fragmentProgKey == b.fragmentProgKey && a.geometryProgKey == b.geometryProgKey &&
  23. a.hullProgKey == b.hullProgKey && a.domainProgKey == b.domainProgKey;
  24. }
  25. GLSLProgramPipelineManager::~GLSLProgramPipelineManager()
  26. {
  27. for (auto& pipeline : mPipelines)
  28. {
  29. glDeleteProgramPipelines(1, &pipeline.second.glHandle);
  30. BS_CHECK_GL_ERROR();
  31. BS_INC_RENDER_STAT_CAT(ResDestroyed, RenderStatObject_PipelineObject);
  32. }
  33. }
  34. const GLSLProgramPipeline* GLSLProgramPipelineManager::getPipeline(GLSLGpuProgram* vertexProgram, GLSLGpuProgram* fragmentProgram,
  35. GLSLGpuProgram* geometryProgram, GLSLGpuProgram* hullProgram, GLSLGpuProgram* domainProgram)
  36. {
  37. ProgramPipelineKey key;
  38. key.vertexProgKey = vertexProgram != nullptr ? vertexProgram->getProgramID() : 0;
  39. key.fragmentProgKey = fragmentProgram != nullptr ? fragmentProgram->getProgramID() : 0;
  40. key.geometryProgKey = geometryProgram != nullptr ? geometryProgram->getProgramID() : 0;
  41. key.hullProgKey = hullProgram != nullptr ? hullProgram->getProgramID() : 0;
  42. key.domainProgKey = domainProgram != nullptr ? domainProgram->getProgramID() : 0;
  43. auto iterFind = mPipelines.find(key);
  44. if(iterFind == mPipelines.end())
  45. {
  46. GLSLProgramPipeline newPipeline;
  47. glGenProgramPipelines(1, &newPipeline.glHandle);
  48. BS_CHECK_GL_ERROR();
  49. if(vertexProgram != nullptr)
  50. {
  51. glUseProgramStages(newPipeline.glHandle, GL_VERTEX_SHADER_BIT, vertexProgram->getGLHandle());
  52. BS_CHECK_GL_ERROR();
  53. }
  54. if(fragmentProgram != nullptr)
  55. {
  56. glUseProgramStages(newPipeline.glHandle, GL_FRAGMENT_SHADER_BIT, fragmentProgram->getGLHandle());
  57. BS_CHECK_GL_ERROR();
  58. }
  59. if(geometryProgram != nullptr)
  60. {
  61. glUseProgramStages(newPipeline.glHandle, GL_GEOMETRY_SHADER_BIT, geometryProgram->getGLHandle());
  62. BS_CHECK_GL_ERROR();
  63. }
  64. if(hullProgram != nullptr)
  65. {
  66. glUseProgramStages(newPipeline.glHandle, GL_TESS_CONTROL_SHADER_BIT, hullProgram->getGLHandle());
  67. BS_CHECK_GL_ERROR();
  68. }
  69. if(domainProgram != nullptr)
  70. {
  71. glUseProgramStages(newPipeline.glHandle, GL_TESS_EVALUATION_SHADER_BIT, domainProgram->getGLHandle());
  72. BS_CHECK_GL_ERROR();
  73. }
  74. mPipelines[key] = newPipeline;
  75. BS_INC_RENDER_STAT_CAT(ResCreated, RenderStatObject_PipelineObject);
  76. return &mPipelines[key];
  77. }
  78. else
  79. return &iterFind->second;
  80. }
  81. }}