/src/core/shaders/graphs/script.py

https://github.com/KDAB/kuesa
Python | 278 lines | 253 code | 22 blank | 3 comment | 18 complexity | 184162b70d1075cccf4dea8be29926e6 MD5 | raw file
  1. #!/usr/bin/env python3
  2. import copy
  3. import json
  4. import sys
  5. INPUT = {
  6. 'outputs': ['value'],
  7. 'parameters': {
  8. 'name': 'defaultName',
  9. 'type': {
  10. 'type': 'QShaderLanguage::VariableType',
  11. 'value': 'QShaderLanguage::Vec3'
  12. },
  13. 'location': '0'
  14. },
  15. 'rules': [
  16. {
  17. 'format': { 'api': 'OpenGLES', 'major': 2, 'minor': 0 },
  18. 'headerSnippets': ['attribute highp $type $name;'],
  19. 'substitution': 'highp $type $value = $name;'
  20. },
  21. {
  22. 'format': { 'api': 'OpenGLES', 'major': 3, 'minor': 0 },
  23. 'headerSnippets': ['in highp $type $name;'],
  24. 'substitution': 'highp $type $value = $name;'
  25. },
  26. {
  27. 'format': { 'api': 'OpenGLCoreProfile', 'major': 3, 'minor': 0 },
  28. 'headerSnippets': ['in $type $name;'],
  29. 'substitution': '$type $value = $name;'
  30. },
  31. {
  32. 'format': { 'api': 'RHI', 'major': 1, 'minor': 0 },
  33. 'headerSnippets': ['layout(location = $location) in $type $name;'],
  34. 'substitution': '$type $value = $name;'
  35. }
  36. ]
  37. }
  38. BUILTIN_INPUT = {
  39. 'outputs': ['value'],
  40. 'parameters': {
  41. 'name': 'defaultName',
  42. 'type': {
  43. 'type': 'QShaderLanguage::VariableType',
  44. 'value': 'QShaderLanguage::Vec3'
  45. }
  46. },
  47. 'rules': [
  48. {
  49. 'format': { 'api': 'OpenGLES', 'major': 2, 'minor': 0 },
  50. 'substitution': 'highp $type $value = $name;'
  51. },
  52. {
  53. 'format': { 'api': 'OpenGLCoreProfile', 'major': 3, 'minor': 0 },
  54. 'substitution': '$type $value = $name;'
  55. },
  56. {
  57. 'format': { 'api': 'RHI', 'major': 1, 'minor': 0 },
  58. 'substitution': '$type $value = $name;'
  59. }
  60. ]
  61. }
  62. UNIFORM_INPUT = {
  63. 'outputs': ['value'],
  64. 'parameters': {
  65. 'name': 'defaultName',
  66. 'type': {
  67. 'type': 'QShaderLanguage::VariableType',
  68. 'value': 'QShaderLanguage::Vec3'
  69. },
  70. 'location': '0'
  71. },
  72. 'rules': [
  73. {
  74. 'format': { 'api': 'OpenGLES', 'major': 2, 'minor': 0 },
  75. 'headerSnippets': ['uniform highp $type $name;'],
  76. 'substitution': 'highp $type $value = $name;'
  77. },
  78. {
  79. 'format': { 'api': 'OpenGLCoreProfile', 'major': 3, 'minor': 0 },
  80. 'headerSnippets': ['uniform $type $name;'],
  81. 'substitution': '$type $value = $name;'
  82. },
  83. {
  84. 'format': { 'api': 'RHI', 'major': 1, 'minor': 0 },
  85. 'headerSnippets': ['add-uniform $type $name'],
  86. 'substitution': '$type $value = $name;'
  87. }
  88. ]
  89. }
  90. UNIFORM_ARRAY_INPUT = {
  91. 'outputs': ['value'],
  92. 'parameters': {
  93. 'name': 'defaultName',
  94. 'size': '1',
  95. 'type': {
  96. 'type': 'QShaderLanguage::VariableType',
  97. 'value': 'QShaderLanguage::Vec3'
  98. },
  99. 'location': '0'
  100. },
  101. 'rules': [
  102. {
  103. 'format': { 'api': 'OpenGLES', 'major': 2, 'minor': 0 },
  104. 'headerSnippets': ['uniform highp $type $name[$size];'],
  105. 'substitution': 'highp $type $value = $name;'
  106. },
  107. {
  108. 'format': { 'api': 'OpenGLCoreProfile', 'major': 3, 'minor': 0 },
  109. 'headerSnippets': ['uniform $type $name[$size];'],
  110. 'substitution': '$type $value = $name;'
  111. },
  112. {
  113. 'format': { 'api': 'RHI', 'major': 1, 'minor': 0 },
  114. 'headerSnippets': ['add-uniform $type $name[$size]'],
  115. 'substitution': '$type $value = $name;'
  116. }
  117. ]
  118. }
  119. TEXTURE_INPUT = {
  120. 'outputs': ['value'],
  121. 'parameters': {
  122. 'name': 'defaultName',
  123. 'type': {
  124. 'type': 'QShaderLanguage::VariableType',
  125. 'value': 'QShaderLanguage::Vec3'
  126. }
  127. },
  128. 'rules': [
  129. {
  130. 'format': { 'api': 'OpenGLES', 'major': 2, 'minor': 0 },
  131. 'headerSnippets': ['uniform highp $type $name;'],
  132. 'substitution': 'highp $type $value = $name;'
  133. },
  134. {
  135. 'format': { 'api': 'OpenGLCoreProfile', 'major': 3, 'minor': 0 },
  136. 'headerSnippets': ['uniform $type $name;'],
  137. 'substitution': '$type $value = $name;'
  138. },
  139. {
  140. 'format': { 'api': 'RHI', 'major': 1, 'minor': 0 },
  141. 'headerSnippets': ['add-sampler $type $name'],
  142. 'substitution': '$type $value = $name;'
  143. }
  144. ]
  145. }
  146. SAMPLE_TEXTURE = {
  147. 'inputs': ['coord'],
  148. 'outputs': ['color'],
  149. 'parameters': {
  150. 'name': 'defaultName'
  151. },
  152. 'rules': [
  153. {
  154. 'format': { 'api': 'OpenGLES', 'major': 2, 'minor': 0 },
  155. 'headerSnippets': ['uniform sampler2D $name;'],
  156. 'substitution': 'highp vec4 $color = texture2D($name, $coord);'
  157. },
  158. {
  159. 'format': { 'api': 'OpenGLES', 'major': 3, 'minor': 0 },
  160. 'headerSnippets': ['uniform sampler2D $name;'],
  161. 'substitution': 'highp vec4 $color = texture($name, $coord);'
  162. },
  163. {
  164. 'format': { 'api': 'OpenGLCoreProfile', 'major': 3, 'minor': 0 },
  165. 'headerSnippets': ['uniform sampler2D $name;'],
  166. 'substitution': 'vec4 $color = texture($name, $coord);'
  167. },
  168. {
  169. 'format': { 'api': 'RHI', 'major': 1, 'minor': 0 },
  170. 'headerSnippets': ['add-sampler sampler2D $name'],
  171. 'substitution': 'vec4 $color = texture($name, $coord);'
  172. }
  173. ]
  174. }
  175. OUTPUT = {
  176. 'inputs': ['value'],
  177. 'parameters': {
  178. 'name': 'defaultName',
  179. 'type': {
  180. 'type': 'QShaderLanguage::VariableType',
  181. 'value': 'QShaderLanguage::Vec3'
  182. }
  183. },
  184. 'rules': [
  185. {
  186. 'format': { 'api': 'OpenGLES', 'major': 2, 'minor': 0 },
  187. 'headerSnippets': ['varying highp $type $name;'],
  188. 'substitution': '$name = $value;'
  189. },
  190. {
  191. 'format': { 'api': 'OpenGLES', 'major': 3, 'minor': 0 },
  192. 'headerSnippets': ['out highp $type $name;'],
  193. 'substitution': '$name = $value;'
  194. },
  195. {
  196. 'format': { 'api': 'OpenGLCoreProfile', 'major': 3, 'minor': 0 },
  197. 'headerSnippets': ['out $type $name;'],
  198. 'substitution': '$name = $value;'
  199. },
  200. {
  201. 'format': { 'api': 'RHI', 'major': 1, 'minor': 0 },
  202. 'headerSnippets': ['add-input out $type $name'],
  203. 'substitution': '$name = $value;'
  204. }
  205. ]
  206. }
  207. def main():
  208. filename = sys.argv[1]
  209. with open(filename) as f:
  210. graph = json.load(f)
  211. for prototype in graph['prototypes'].values():
  212. # Check if the RHI rule is already there
  213. rhi_rule = next((rule for rule in prototype['rules'] if rule['format']['api'] == 'RHI'), None)
  214. if rhi_rule is not None:
  215. continue
  216. # Otherwise add it
  217. gl3_rule = next((rule for rule in prototype['rules'] if rule['format']['api'] == 'OpenGLCoreProfile'))
  218. rhi_rule = copy.deepcopy(gl3_rule)
  219. rhi_rule['format'] = {
  220. 'api': 'RHI',
  221. 'major': 1,
  222. 'minor': 0,
  223. }
  224. prototype['rules'].append(rhi_rule)
  225. graph['prototypes']['input'] = INPUT
  226. graph['prototypes']['builtinInput'] = BUILTIN_INPUT
  227. graph['prototypes']['uniformInput'] = UNIFORM_INPUT
  228. graph['prototypes']['uniformArrayInput'] = UNIFORM_ARRAY_INPUT
  229. graph['prototypes']['textureInput'] = TEXTURE_INPUT
  230. graph['prototypes']['sampleTexture'] = SAMPLE_TEXTURE
  231. graph['prototypes']['output'] = OUTPUT
  232. currentInput = 0
  233. currentOutput = 0
  234. for node in graph['nodes']:
  235. if node['type'] == 'input':
  236. if node['parameters']['qualifier']['value'] == 'QShaderLanguage::Uniform':
  237. if node['parameters']['type']['value'] == 'QShaderLanguage::Sampler2D':
  238. node['type'] = 'textureInput'
  239. else:
  240. node['type'] = 'uniformInput'
  241. elif node['parameters']['qualifier']['value'] == 'QShaderLanguage::BuiltIn':
  242. node['type'] = 'builtinInput'
  243. else:
  244. node['parameters']['location'] = str(currentInput)
  245. currentInput += 1
  246. del node['parameters']['qualifier']
  247. elif node['type'] == 'arrayInput':
  248. node['type'] = 'uniformArrayInput'
  249. del node['parameters']['qualifier']
  250. elif node['type'] == 'output':
  251. node['parameters']['location'] = str(currentOutput)
  252. currentOutput += 1
  253. with open(filename, 'w') as f:
  254. json.dump(graph, f, indent=4)
  255. if __name__ == '__main__':
  256. main()