blob: 8a6c42003da69a27a32752bccc34e9e81347a59d [file] [log] [blame]
Ethan Nicholas762466e2017-06-29 10:03:38 -04001/*
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
18constexpr const char* kFragmentProcessorHeader =
Ethan Nicholas130fb3f2018-02-01 12:14:34 -050019R"(
20/**************************************************************************************************
21 *** This file was autogenerated from %s.fp; do not modify.
22 **************************************************************************************************/
Ethan Nicholas762466e2017-06-29 10:03:38 -040023)";
24
25namespace SkSL {
26
27class HCodeGenerator : public CodeGenerator {
28public:
Ethan Nicholasc9472af2017-10-10 16:30:21 -040029 HCodeGenerator(const Context* context, const Program* program, ErrorReporter* errors,
30 String name, OutputStream* out);
Ethan Nicholas762466e2017-06-29 10:03:38 -040031
32 bool generateCode() override;
33
Ethan Nicholasd608c092017-10-26 09:30:08 -040034 static String ParameterType(const Context& context, const Type& type, const Layout& layout);
Ethan Nicholas762466e2017-06-29 10:03:38 -040035
Ethan Nicholasd608c092017-10-26 09:30:08 -040036 static String FieldType(const Context& context, const Type& type, const Layout& layout);
Ethan Nicholas762466e2017-06-29 10:03:38 -040037
38 static String FieldName(const char* varName) {
39 return String::printf("f%c%s", toupper(varName[0]), varName + 1);
40 }
41
Ethan Nicholas929a6812018-08-06 14:56:59 -040042 static String CoordTransformName(const String& arg, int index) {
43 if (arg.size()) {
44 return HCodeGenerator::FieldName(arg.c_str()) + "CoordTransform";
45 }
46 return "fCoordTransform" + to_string(index);
47 }
48
Ethan Nicholas130fb3f2018-02-01 12:14:34 -050049 static String GetHeader(const Program& program, ErrorReporter& errors);
50
Ethan Nicholas762466e2017-06-29 10:03:38 -040051private:
52 void writef(const char* s, va_list va) SKSL_PRINTF_LIKE(2, 0);
53
54 void writef(const char* s, ...) SKSL_PRINTF_LIKE(2, 3);
55
56 bool writeSection(const char* name, const char* prefix = "");
57
58 // given a @constructorParams section of e.g. 'int x, float y', writes out "<separator>x, y".
59 // Writes nothing (not even the separator) if there is no @constructorParams section.
60 void writeExtraConstructorParams(const char* separator);
61
62 void writeMake();
63
64 void writeConstructor();
65
66 void writeFields();
67
Ethan Nicholas68990be2017-07-13 09:36:52 -040068 void failOnSection(const char* section, const char* msg);
69
Ethan Nicholasc9472af2017-10-10 16:30:21 -040070 const Context& fContext;
Ethan Nicholas762466e2017-06-29 10:03:38 -040071 String fName;
72 String fFullName;
73 SectionAndParameterHelper fSectionAndParameterHelper;
74
75 typedef CodeGenerator INHERITED;
76};
77
78} // namespace SkSL
79
80#endif