blob: 875bbf974c932283f971f2a7d866d596653399cf [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 Nicholas78aceb22018-08-31 16:13:58 -040036 static Layout::CType ParameterCType(const Context& context, const Type& type,
37 const Layout& layout);
38
Ethan Nicholasd608c092017-10-26 09:30:08 -040039 static String FieldType(const Context& context, const Type& type, const Layout& layout);
Ethan Nicholas762466e2017-06-29 10:03:38 -040040
Michael Ludwiga4275592018-08-31 10:52:47 -040041 // 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 Nicholas762466e2017-06-29 10:03:38 -040044 static String FieldName(const char* varName) {
45 return String::printf("f%c%s", toupper(varName[0]), varName + 1);
46 }
47
Ethan Nicholas929a6812018-08-06 14:56:59 -040048 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 Nicholas130fb3f2018-02-01 12:14:34 -050055 static String GetHeader(const Program& program, ErrorReporter& errors);
56
Ethan Nicholas762466e2017-06-29 10:03:38 -040057private:
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 Nicholas68990be2017-07-13 09:36:52 -040074 void failOnSection(const char* section, const char* msg);
75
Ethan Nicholasc9472af2017-10-10 16:30:21 -040076 const Context& fContext;
Ethan Nicholas762466e2017-06-29 10:03:38 -040077 String fName;
78 String fFullName;
79 SectionAndParameterHelper fSectionAndParameterHelper;
80
81 typedef CodeGenerator INHERITED;
82};
83
84} // namespace SkSL
85
86#endif