*/}}

la_tns_shader_infrastructure.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * LaGUI: A graphical application framework.
  3. * Copyright (C) 2022-2023 Wu Yiming
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #define _GNU_SOURCE 1
  19. #include "la_5.h"
  20. #include <string>
  21. #include <regex>
  22. extern tnsMain* T;
  23. char* tnsRegexReplace(char* in, char* pairs[]){
  24. std::string a=in; char** p=pairs;
  25. while(p[0]){
  26. a = std::regex_replace(a,std::regex(p[0]),p[1]); p+=2;
  27. }
  28. int len=a.length()+1; void* ret=malloc(len); memcpy(ret, a.c_str(), len); return (char*)ret;
  29. }
  30. std::string tnsEnsureShaderCommoms(std::string in){
  31. std::string str=in;
  32. for(tnsShaderComponent* sc=(tnsShaderComponent*)T->ShaderComponents.pFirst;sc;sc=(tnsShaderComponent*)sc->Item.pNext){
  33. str = std::regex_replace(str,std::regex(sc->name),sc->replacement);
  34. }
  35. return str;
  36. }
  37. std::string tnsEnsureShaderCommomsLib(std::string in, std::string lib, std::string material){
  38. std::string str=tnsEnsureShaderCommoms(in);
  39. str = std::regex_replace(str,std::regex("#with TNS_SHADER_MATERIAL"),material);
  40. str = std::regex_replace(str,std::regex("#with TNS_SHADER_LIBRARY"),lib);
  41. return str;
  42. }
  43. int tnsCheckShaderCompileStatus(GLuint shader_object, char* name){
  44. int status=0; char error[65536]={0};
  45. glGetShaderiv(shader_object, GL_COMPILE_STATUS, &status);
  46. if (status == GL_FALSE){
  47. glGetShaderInfoLog(shader_object, sizeof(error), 0, error); logPrint("%s shader error:\n%s",name?name:"(unnamed)",error);
  48. glDeleteShader(shader_object); return 0;
  49. } else {
  50. glGetShaderInfoLog(shader_object, sizeof(error), 0, error); if(error[0]) logPrint("%s shader info:\n%s",name?name:"(unnamed)",error);
  51. }
  52. return 1;
  53. }
  54. int tnsCheckProgramLinkStatus(GLuint program_object, char* name){
  55. int status=0; char error[65536]={0};
  56. glGetProgramiv(program_object, GL_LINK_STATUS, &status);
  57. if (status == GL_FALSE){
  58. glGetProgramInfoLog(program_object, sizeof(error), 0, error); logPrintNew("%s program Linking error:\n%s",name?name:"(unnamed)",error); return 0;
  59. glDeleteProgram(program_object); return 0;
  60. } else {
  61. glGetProgramInfoLog(program_object, sizeof(error), 0, error); if (error[0]) logPrintNew("%s program Linking info:\n%s",name?name:"(unnamed)",error);
  62. }
  63. return 1;
  64. }
  65. int tnsNewVertexShader(const char *Content){
  66. int status = 0;
  67. GLuint VertexShaderObject;
  68. tnsShader *s = 0;
  69. if (!Content) return -1;
  70. VertexShaderObject = glCreateShader(GL_VERTEX_SHADER);
  71. std::string UseContent=tnsEnsureShaderCommoms(Content);
  72. const char* str=UseContent.c_str();
  73. glShaderSource(VertexShaderObject, 1, &str, 0);
  74. glCompileShader(VertexShaderObject);
  75. if(!tnsCheckShaderCompileStatus(VertexShaderObject,"Vertex")){ return -1; }
  76. return VertexShaderObject;
  77. }
  78. int tnsNewFragmentShaderMaterial(const char *Content, const char* Library, const char* Material){
  79. int status = 0;
  80. GLuint FragmentShaderObject;
  81. tnsShader *s = 0;
  82. if (!Content) return -1;
  83. FragmentShaderObject = glCreateShader(GL_FRAGMENT_SHADER);
  84. std::string UseContent=tnsEnsureShaderCommomsLib(Content,Library?Library:"",Material?Material:"");
  85. const char* str=UseContent.c_str();
  86. glShaderSource(FragmentShaderObject, 1, &str, 0);
  87. glCompileShader(FragmentShaderObject);
  88. if(!tnsCheckShaderCompileStatus(FragmentShaderObject,"Vertex")){ return -1; }
  89. return FragmentShaderObject;
  90. }
  91. int tnsNewFragmentShader(const char *Content){
  92. return tnsNewFragmentShaderMaterial(Content,0,0);
  93. }
  94. int tnsNewGeometryShader(const char *Content){
  95. #ifndef LAGUI_ANDROID
  96. int status = 0;
  97. GLuint GeometryShaderObject;
  98. tnsShader *s = 0;
  99. if (!Content) return -1;
  100. GeometryShaderObject = glCreateShader(GL_GEOMETRY_SHADER);
  101. std::string UseContent=tnsEnsureShaderCommoms(Content);
  102. const char* str=UseContent.c_str();
  103. glShaderSource(GeometryShaderObject, 1, &str, 0);
  104. glCompileShader(GeometryShaderObject);
  105. if(!tnsCheckShaderCompileStatus(GeometryShaderObject,"Geometry")){ return -1; }
  106. return GeometryShaderObject;
  107. #endif
  108. return 0;
  109. }