Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #ifndef SKSL_HCODEGENERATOR |
| 9 | #define SKSL_HCODEGENERATOR |
| 10 | |
| 11 | #include "SkSLCodeGenerator.h" |
| 12 | #include "SkSLSectionAndParameterHelper.h" |
| 13 | #include "ir/SkSLType.h" |
| 14 | #include "ir/SkSLVariable.h" |
| 15 | |
| 16 | #include <cctype> |
| 17 | |
| 18 | constexpr const char* kFragmentProcessorHeader = |
Ethan Nicholas | 130fb3f | 2018-02-01 12:14:34 -0500 | [diff] [blame] | 19 | R"( |
| 20 | /************************************************************************************************** |
| 21 | *** This file was autogenerated from %s.fp; do not modify. |
| 22 | **************************************************************************************************/ |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 23 | )"; |
| 24 | |
| 25 | namespace SkSL { |
| 26 | |
| 27 | class HCodeGenerator : public CodeGenerator { |
| 28 | public: |
Ethan Nicholas | c9472af | 2017-10-10 16:30:21 -0400 | [diff] [blame] | 29 | HCodeGenerator(const Context* context, const Program* program, ErrorReporter* errors, |
| 30 | String name, OutputStream* out); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 31 | |
| 32 | bool generateCode() override; |
| 33 | |
Ethan Nicholas | d608c09 | 2017-10-26 09:30:08 -0400 | [diff] [blame] | 34 | static String ParameterType(const Context& context, const Type& type, const Layout& layout); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 35 | |
Ethan Nicholas | 78aceb2 | 2018-08-31 16:13:58 -0400 | [diff] [blame] | 36 | static Layout::CType ParameterCType(const Context& context, const Type& type, |
| 37 | const Layout& layout); |
| 38 | |
Ethan Nicholas | d608c09 | 2017-10-26 09:30:08 -0400 | [diff] [blame] | 39 | static String FieldType(const Context& context, const Type& type, const Layout& layout); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 40 | |
Michael Ludwig | a427559 | 2018-08-31 10:52:47 -0400 | [diff] [blame] | 41 | // Either the field type, or a const reference of the field type if the field type is complex. |
| 42 | static String AccessType(const Context& context, const Type& type, const Layout& layout); |
| 43 | |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 44 | static String FieldName(const char* varName) { |
| 45 | return String::printf("f%c%s", toupper(varName[0]), varName + 1); |
| 46 | } |
| 47 | |
Ethan Nicholas | 929a681 | 2018-08-06 14:56:59 -0400 | [diff] [blame] | 48 | static String CoordTransformName(const String& arg, int index) { |
| 49 | if (arg.size()) { |
| 50 | return HCodeGenerator::FieldName(arg.c_str()) + "CoordTransform"; |
| 51 | } |
| 52 | return "fCoordTransform" + to_string(index); |
| 53 | } |
| 54 | |
Ethan Nicholas | 130fb3f | 2018-02-01 12:14:34 -0500 | [diff] [blame] | 55 | static String GetHeader(const Program& program, ErrorReporter& errors); |
| 56 | |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 57 | private: |
| 58 | void writef(const char* s, va_list va) SKSL_PRINTF_LIKE(2, 0); |
| 59 | |
| 60 | void writef(const char* s, ...) SKSL_PRINTF_LIKE(2, 3); |
| 61 | |
| 62 | bool writeSection(const char* name, const char* prefix = ""); |
| 63 | |
| 64 | // given a @constructorParams section of e.g. 'int x, float y', writes out "<separator>x, y". |
| 65 | // Writes nothing (not even the separator) if there is no @constructorParams section. |
| 66 | void writeExtraConstructorParams(const char* separator); |
| 67 | |
| 68 | void writeMake(); |
| 69 | |
| 70 | void writeConstructor(); |
| 71 | |
| 72 | void writeFields(); |
| 73 | |
Ethan Nicholas | 68990be | 2017-07-13 09:36:52 -0400 | [diff] [blame] | 74 | void failOnSection(const char* section, const char* msg); |
| 75 | |
Ethan Nicholas | c9472af | 2017-10-10 16:30:21 -0400 | [diff] [blame] | 76 | const Context& fContext; |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 77 | String fName; |
| 78 | String fFullName; |
| 79 | SectionAndParameterHelper fSectionAndParameterHelper; |
| 80 | |
| 81 | typedef CodeGenerator INHERITED; |
| 82 | }; |
| 83 | |
| 84 | } // namespace SkSL |
| 85 | |
| 86 | #endif |