You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

108 lines
2.3 KiB

4 years ago
  1. #include "GPUProgram.h"
  2. #include "utils.h"
  3. #include <stdio.h>
  4. GLuint CompileShader(const char*shaderPath, GLenum shaderType)
  5. {
  6. GLuint shader = glCreateShader(shaderType);
  7. const char* code = LoadFileContent(shaderPath);
  8. if (code == nullptr)
  9. {
  10. printf("cannot load shader source from file %s\n", shaderPath);
  11. return 0;
  12. }
  13. glShaderSource(shader, 1, &code, nullptr);//ram -> vram
  14. glCompileShader(shader);
  15. GLint nResult;
  16. glGetShaderiv(shader, GL_COMPILE_STATUS, &nResult);
  17. if (nResult == GL_FALSE)
  18. {
  19. printf("compile shader %s fail\n", shaderPath);
  20. GLint logLength;
  21. glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &logLength);
  22. char*log = new char[logLength];
  23. GLsizei writed = 0;
  24. glGetShaderInfoLog(shader, logLength, &writed, log);
  25. printf("%s\n", log);
  26. delete log;
  27. glDeleteShader(shader);
  28. return 0;
  29. }
  30. return shader;
  31. }
  32. GPUProgram::GPUProgram()
  33. {
  34. mProgram = glCreateProgram();
  35. }
  36. GPUProgram::~GPUProgram()
  37. {
  38. }
  39. void GPUProgram::AttachShader(GLenum shaderType, const char*shaderPath)
  40. {
  41. GLuint shader = CompileShader(shaderPath, shaderType);
  42. if (shader!=0)
  43. {
  44. glAttachShader(mProgram, shader);
  45. mAttachedShaders.push(shader);
  46. }
  47. }
  48. void GPUProgram::Link()
  49. {
  50. glLinkProgram(mProgram);
  51. GLint nResult;
  52. glGetProgramiv(mProgram, GL_LINK_STATUS, &nResult);
  53. if (nResult == GL_FALSE)
  54. {
  55. printf("create gpu program fail,link error\n");
  56. GLint logLength;
  57. glGetProgramiv(mProgram, GL_INFO_LOG_LENGTH, &logLength);
  58. char*log = new char[logLength];
  59. GLsizei writed = 0;
  60. glGetProgramInfoLog(mProgram, logLength, &writed, log);
  61. printf("%s\n", log);
  62. delete log;
  63. glDeleteProgram(mProgram);
  64. mProgram = 0;
  65. }
  66. while (!mAttachedShaders.empty())
  67. {
  68. GLuint shader = mAttachedShaders.top();
  69. glDetachShader(mProgram, shader);
  70. glDeleteShader(shader);
  71. mAttachedShaders.pop();
  72. }
  73. }
  74. void GPUProgram::DetectAttribute(const char*attributeName)
  75. {
  76. GLint loc = glGetAttribLocation(mProgram, attributeName);
  77. if (loc!=-1)
  78. {
  79. mLocations.insert(std::pair<std::string,GLint>(attributeName,loc));
  80. }
  81. }
  82. void GPUProgram::DetectUniform(const char*uniformName)
  83. {
  84. GLint loc = glGetUniformLocation(mProgram, uniformName);
  85. if (loc != -1)
  86. {
  87. mLocations.insert(std::pair<std::string, GLint>(uniformName, loc));
  88. }
  89. }
  90. GLint GPUProgram::GetLocation(const char*name)
  91. {
  92. auto iter = mLocations.find(name);
  93. if (iter ==mLocations.end())
  94. {
  95. return -1;
  96. }
  97. return iter->second;
  98. }