1
0

gl-shaderparser.h 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 3 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_free(struct gl_parser_attrib *attr)
  28. {
  29. dstr_free(&attr->name);
  30. }
  31. struct gl_shader_parser {
  32. enum shader_type type;
  33. const char *input_prefix;
  34. const char *output_prefix;
  35. struct shader_parser parser;
  36. struct dstr gl_string;
  37. DARRAY(uint32_t) texture_samplers;
  38. DARRAY(struct gl_parser_attrib) attribs;
  39. };
  40. static inline void gl_shader_parser_init(struct gl_shader_parser *glsp,
  41. enum shader_type type)
  42. {
  43. glsp->type = type;
  44. if (type == SHADER_VERTEX) {
  45. glsp->input_prefix = "_geom_shader_attrib";
  46. glsp->output_prefix = "_vertex_shader_attrib";
  47. } else if (type == SHADER_PIXEL) {
  48. glsp->input_prefix = "_vertex_shader_attrib";
  49. glsp->output_prefix = "_pixel_shader_attrib";
  50. }
  51. shader_parser_init(&glsp->parser);
  52. dstr_init(&glsp->gl_string);
  53. da_init(glsp->texture_samplers);
  54. da_init(glsp->attribs);
  55. }
  56. static inline void gl_shader_parser_free(struct gl_shader_parser *glsp)
  57. {
  58. size_t i;
  59. for (i = 0; i < glsp->attribs.num; i++)
  60. gl_parser_attrib_free(glsp->attribs.array+i);
  61. da_free(glsp->attribs);
  62. da_free(glsp->texture_samplers);
  63. dstr_free(&glsp->gl_string);
  64. shader_parser_free(&glsp->parser);
  65. }
  66. extern bool gl_shader_parse(struct gl_shader_parser *glsp,
  67. const char *shader_str, const char *file);