gl-shaderparser.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /******************************************************************************
  2. Copyright (C) 2013 by Hugh 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,
  46. enum gs_shader_type type)
  47. {
  48. glsp->type = type;
  49. if (type == GS_SHADER_VERTEX) {
  50. glsp->input_prefix = "_input_attrib";
  51. glsp->output_prefix = "_vertex_shader_attrib";
  52. } else if (type == GS_SHADER_PIXEL) {
  53. glsp->input_prefix = "_vertex_shader_attrib";
  54. glsp->output_prefix = "_pixel_shader_attrib";
  55. }
  56. shader_parser_init(&glsp->parser);
  57. dstr_init(&glsp->gl_string);
  58. da_init(glsp->texture_samplers);
  59. da_init(glsp->attribs);
  60. glsp->sincos_counter = 1;
  61. }
  62. static inline void gl_shader_parser_free(struct gl_shader_parser *glsp)
  63. {
  64. size_t i;
  65. for (i = 0; i < glsp->attribs.num; i++)
  66. gl_parser_attrib_free(glsp->attribs.array + i);
  67. da_free(glsp->attribs);
  68. da_free(glsp->texture_samplers);
  69. dstr_free(&glsp->gl_string);
  70. shader_parser_free(&glsp->parser);
  71. }
  72. extern bool gl_shader_parse(struct gl_shader_parser *glsp,
  73. const char *shader_str, const char *file);