BsGLSLProgramPipelineManager.cpp 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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_INC_RENDER_STAT_CAT(ResDestroyed, RenderStatObject_PipelineObject);
  31. }
  32. }
  33. const GLSLProgramPipeline* GLSLProgramPipelineManager::getPipeline(GLSLGpuProgram* vertexProgram, GLSLGpuProgram* fragmentProgram,
  34. GLSLGpuProgram* geometryProgram, GLSLGpuProgram* hullProgram, GLSLGpuProgram* domainProgram)
  35. {
  36. ProgramPipelineKey key;
  37. key.vertexProgKey = vertexProgram != nullptr ? vertexProgram->getProgramID() : 0;
  38. key.fragmentProgKey = fragmentProgram != nullptr ? fragmentProgram->getProgramID() : 0;
  39. key.geometryProgKey = geometryProgram != nullptr ? geometryProgram->getProgramID() : 0;
  40. key.hullProgKey = hullProgram != nullptr ? hullProgram->getProgramID() : 0;
  41. key.domainProgKey = domainProgram != nullptr ? domainProgram->getProgramID() : 0;
  42. auto iterFind = mPipelines.find(key);
  43. if(iterFind == mPipelines.end())
  44. {
  45. GLSLProgramPipeline newPipeline;
  46. glGenProgramPipelines(1, &newPipeline.glHandle);
  47. if(vertexProgram != nullptr)
  48. {
  49. glUseProgramStages(newPipeline.glHandle, GL_VERTEX_SHADER_BIT, vertexProgram->getGLHandle());
  50. }
  51. if(fragmentProgram != nullptr)
  52. {
  53. glUseProgramStages(newPipeline.glHandle, GL_FRAGMENT_SHADER_BIT, fragmentProgram->getGLHandle());
  54. }
  55. if(geometryProgram != nullptr)
  56. {
  57. glUseProgramStages(newPipeline.glHandle, GL_GEOMETRY_SHADER_BIT, geometryProgram->getGLHandle());
  58. }
  59. if(hullProgram != nullptr)
  60. {
  61. glUseProgramStages(newPipeline.glHandle, GL_TESS_CONTROL_SHADER_BIT, hullProgram->getGLHandle());
  62. }
  63. if(domainProgram != nullptr)
  64. {
  65. glUseProgramStages(newPipeline.glHandle, GL_TESS_EVALUATION_SHADER_BIT, domainProgram->getGLHandle());
  66. }
  67. mPipelines[key] = newPipeline;
  68. BS_INC_RENDER_STAT_CAT(ResCreated, RenderStatObject_PipelineObject);
  69. return &mPipelines[key];
  70. }
  71. else
  72. return &iterFind->second;
  73. }
  74. }}