Clone of mesa.
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * Copyright © 2008, 2009 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21. * DEALINGS IN THE SOFTWARE.
  22. */
  23. #include <cstdlib>
  24. #include <cstdio>
  25. #include <sys/types.h>
  26. #include <sys/stat.h>
  27. #include <fcntl.h>
  28. #include <unistd.h>
  29. #include "ast.h"
  30. #include "glsl_parser_extras.h"
  31. #include "glsl_parser.h"
  32. #include "ir_optimization.h"
  33. #include "ir_print_visitor.h"
  34. static char *
  35. load_text_file(const char *file_name, size_t *size)
  36. {
  37. char *text = NULL;
  38. struct stat st;
  39. ssize_t total_read = 0;
  40. int fd = open(file_name, O_RDONLY);
  41. *size = 0;
  42. if (fd < 0) {
  43. return NULL;
  44. }
  45. if (fstat(fd, & st) == 0) {
  46. text = (char *) malloc(st.st_size + 1);
  47. if (text != NULL) {
  48. do {
  49. ssize_t bytes = read(fd, text + total_read,
  50. st.st_size - total_read);
  51. if (bytes < 0) {
  52. free(text);
  53. text = NULL;
  54. break;
  55. }
  56. if (bytes == 0) {
  57. break;
  58. }
  59. total_read += bytes;
  60. } while (total_read < st.st_size);
  61. text[total_read] = '\0';
  62. *size = total_read;
  63. }
  64. }
  65. close(fd);
  66. return text;
  67. }
  68. int
  69. main(int argc, char **argv)
  70. {
  71. struct _mesa_glsl_parse_state state;
  72. char *shader;
  73. size_t shader_len;
  74. exec_list instructions;
  75. if (argc < 3) {
  76. printf("Usage: %s [v|g|f|i] <shader_file>\n", argv[0]);
  77. return EXIT_FAILURE;
  78. }
  79. memset(& state, 0, sizeof(state));
  80. switch (argv[1][0]) {
  81. case 'v':
  82. state.target = vertex_shader;
  83. break;
  84. case 'g':
  85. state.target = geometry_shader;
  86. break;
  87. case 'f':
  88. state.target = fragment_shader;
  89. break;
  90. default:
  91. printf("Usage: %s [v|g|f] <shader_file>\n", argv[0]);
  92. return EXIT_FAILURE;
  93. }
  94. shader = load_text_file(argv[2], & shader_len);
  95. state.scanner = NULL;
  96. state.translation_unit.make_empty();
  97. state.symbols = new glsl_symbol_table;
  98. state.error = false;
  99. state.temp_index = 0;
  100. state.loop_or_switch_nesting = NULL;
  101. state.ARB_texture_rectangle_enable = true;
  102. _mesa_glsl_lexer_ctor(& state, shader, shader_len);
  103. _mesa_glsl_parse(& state);
  104. _mesa_glsl_lexer_dtor(& state);
  105. foreach_list_const(n, &state.translation_unit) {
  106. ast_node *ast = exec_node_data(ast_node, n, link);
  107. ast->print();
  108. }
  109. if (!state.error && !state.translation_unit.is_empty())
  110. _mesa_ast_to_hir(&instructions, &state);
  111. /* Optimization passes */
  112. if (!state.error && !instructions.is_empty()) {
  113. bool progress;
  114. do {
  115. progress = false;
  116. progress = do_function_inlining(&instructions) || progress;
  117. progress = do_if_simplification(&instructions) || progress;
  118. progress = do_copy_propagation(&instructions) || progress;
  119. progress = do_dead_code_local(&instructions) || progress;
  120. progress = do_dead_code_unlinked(&instructions) || progress;
  121. progress = do_constant_variable_unlinked(&instructions) || progress;
  122. progress = do_constant_folding(&instructions) || progress;
  123. progress = do_vec_index_to_swizzle(&instructions) || progress;
  124. progress = do_swizzle_swizzle(&instructions) || progress;
  125. } while (progress);
  126. }
  127. /* Print out the resulting IR */
  128. printf("\n\n");
  129. if (!state.error) {
  130. _mesa_print_ir(&instructions, &state);
  131. }
  132. delete state.symbols;
  133. return state.error != 0;
  134. }