Class: RLSL::BaseTranslator
- Inherits:
-
Object
- Object
- RLSL::BaseTranslator
- Defined in:
- lib/rlsl/base_translator.rb,
lib/rlsl/base_translator/call_parser.rb,
lib/rlsl/base_translator/code_scanner.rb,
lib/rlsl/base_translator/code_rewriter.rb
Direct Known Subclasses
Defined Under Namespace
Classes: CallParser, CodeRewriter, CodeScanner, SourceSnippet, TargetProfile
Constant Summary collapse
- REMOVED_IDENTIFIERS =
%w[static inline].freeze
Class Method Summary collapse
- .build_profile(uniform_target:, identifier_replacements:, call_rewrites:, removed_identifiers: REMOVED_IDENTIFIERS) ⇒ Object
- .common_call_rewrites(target_vec2:, target_vec3:, target_vec4:) ⇒ Object
- .infix_call(operator) ⇒ Object
- .rename_call(name) ⇒ Object
Instance Method Summary collapse
-
#initialize(uniforms, helpers_code, fragment_code) ⇒ BaseTranslator
constructor
A new instance of BaseTranslator.
- #translate ⇒ Object
Constructor Details
#initialize(uniforms, helpers_code, fragment_code) ⇒ BaseTranslator
Returns a new instance of BaseTranslator.
31 32 33 34 35 |
# File 'lib/rlsl/base_translator.rb', line 31 def initialize(uniforms, helpers_code, fragment_code) @uniforms = uniforms @helpers_source = normalize_source(helpers_code) @fragment_source = normalize_source(fragment_code) end |
Class Method Details
.build_profile(uniform_target:, identifier_replacements:, call_rewrites:, removed_identifiers: REMOVED_IDENTIFIERS) ⇒ Object
132 133 134 135 136 137 138 139 |
# File 'lib/rlsl/base_translator.rb', line 132 def self.build_profile(uniform_target:, identifier_replacements:, call_rewrites:, removed_identifiers: REMOVED_IDENTIFIERS) TargetProfile.new( uniform_target: uniform_target, identifier_replacements: identifier_replacements.freeze, call_rewrites: call_rewrites.freeze, removed_identifiers: removed_identifiers.freeze ).freeze end |
.common_call_rewrites(target_vec2:, target_vec3:, target_vec4:) ⇒ Object
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/rlsl/base_translator.rb', line 141 def self.common_call_rewrites(target_vec2:, target_vec3:, target_vec4:) { "vec2_new" => rename_call(target_vec2), "vec3_new" => rename_call(target_vec3), "vec4_new" => rename_call(target_vec4), "vec2_add" => infix_call("+"), "vec3_add" => infix_call("+"), "vec2_sub" => infix_call("-"), "vec3_sub" => infix_call("-"), "vec2_mul" => infix_call("*"), "vec3_mul" => infix_call("*"), "vec2_div" => infix_call("/"), "vec3_div" => infix_call("/"), "vec2_dot" => rename_call("dot"), "vec3_dot" => rename_call("dot"), "vec2_length" => rename_call("length"), "vec3_length" => rename_call("length"), "vec2_normalize" => rename_call("normalize"), "vec3_normalize" => rename_call("normalize"), "sqrtf" => rename_call("sqrt"), "sinf" => rename_call("sin"), "cosf" => rename_call("cos"), "tanf" => rename_call("tan"), "fabsf" => rename_call("abs"), "fminf" => rename_call("min"), "fmaxf" => rename_call("max"), "floorf" => rename_call("floor"), "ceilf" => rename_call("ceil"), "powf" => rename_call("pow"), "expf" => rename_call("exp"), "logf" => rename_call("log"), "atan2f" => rename_call("atan2"), "fmodf" => rename_call("fmod"), "mix_f" => rename_call("mix"), "mix_v3" => rename_call("mix"), "clamp_f" => rename_call("clamp"), "smoothstep" => rename_call("smoothstep"), "fract" => rename_call("fract") }.freeze end |
.infix_call(operator) ⇒ Object
188 189 190 191 192 193 194 |
# File 'lib/rlsl/base_translator.rb', line 188 def self.infix_call(operator) lambda do |arguments| raise ArgumentError, "Expected 2 arguments for #{operator} rewrite, got #{arguments.length}" unless arguments.length == 2 "(#{arguments[0]} #{operator} #{arguments[1]})" end end |
.rename_call(name) ⇒ Object
182 183 184 185 186 |
# File 'lib/rlsl/base_translator.rb', line 182 def self.rename_call(name) lambda do |arguments| "#{name}(#{arguments.join(', ')})" end end |
Instance Method Details
#translate ⇒ Object
37 38 39 40 41 |
# File 'lib/rlsl/base_translator.rb', line 37 def translate helpers_translated = translate_code(@helpers_source) fragment_translated = translate_code(@fragment_source) generate_shader(helpers_translated, fragment_translated) end |