BsGLSLProgramPipelineManager.cpp 2.9 KB

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