gl-shaderparser.h 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /******************************************************************************
  2. Copyright (C) 2023 by Lain Bailey <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. #pragma once
  15. /*
  16. * Parses shaders into GLSL. Shaders are almost identical to HLSL
  17. * model 5 so it requires quite a bit of tweaking to convert correctly.
  18. * Takes the parsed shader data, and builds a GLSL string out of it.
  19. */
  20. #include <util/dstr.h>
  21. #include <graphics/shader-parser.h>
  22. struct gl_parser_attrib {
  23. struct dstr name;
  24. const char *mapping;
  25. bool input;
  26. };
  27. static inline void gl_parser_attrib_init(struct gl_parser_attrib *attr)
  28. {
  29. memset(attr, 0, sizeof(struct gl_parser_attrib));
  30. }
  31. static inline void gl_parser_attrib_free(struct gl_parser_attrib *attr)
  32. {
  33. dstr_free(&attr->name);
  34. }
  35. struct gl_shader_parser {
  36. enum gs_shader_type type;
  37. const char *input_prefix;
  38. const char *output_prefix;
  39. struct shader_parser parser;
  40. struct dstr gl_string;
  41. int sincos_counter;
  42. DARRAY(uint32_t) texture_samplers;
  43. DARRAY(struct gl_parser_attrib) attribs;
  44. };
  45. static inline void gl_shader_parser_init(struct gl_shader_parser *glsp, enum gs_shader_type type)
  46. {
  47. glsp->type = type;
  48. if (type == GS_SHADER_VERTEX) {
  49. glsp->input_prefix = "_input_attrib";
  50. glsp->output_prefix = "_vertex_shader_attrib";
  51. } else if (type == GS_SHADER_PIXEL) {
  52. glsp->input_prefix = "_vertex_shader_attrib";
  53. glsp->output_prefix = "_pixel_shader_attrib";
  54. }
  55. shader_parser_init(&glsp->parser);
  56. dstr_init(&glsp->gl_string);
  57. da_init(glsp->texture_samplers);
  58. da_init(glsp->attribs);
  59. glsp->sincos_counter = 1;
  60. }
  61. static inline void gl_shader_parser_free(struct gl_shader_parser *glsp)
  62. {
  63. size_t i;
  64. for (i = 0; i < glsp->attribs.num; i++)
  65. gl_parser_attrib_free(glsp->attribs.array + i);
  66. da_free(glsp->attribs);
  67. da_free(glsp->texture_samplers);
  68. dstr_free(&glsp->gl_string);
  69. shader_parser_free(&glsp->parser);
  70. }
  71. extern bool gl_shader_parse(struct gl_shader_parser *glsp, const char *shader_str, const char *file);