Pioneer
Loading...
Searching...
No Matches
ShaderParser.h
Go to the documentation of this file.
1// Copyright © 2008-2023 Pioneer Developers. See AUTHORS.txt for details
2// Licensed under the terms of the GPL v3. See licenses/GPL-3.txt
3
4#pragma once
5
6#include "graphics/Texture.h"
7#include "graphics/Types.h"
8
9#include <cstdint>
10#include <memory>
11#include <string>
12#include <string_view>
13#include <vector>
14
15namespace Graphics {
16
17 namespace ShaderParser {
18
19 struct Token {
20 // very lightweight token info, just enough to parse a barebones
21 // shaderdef file. Eventually look at being able to lex GLSL files for
22 // inline 'combined' shader code.
23 enum Type : uint8_t {
25 String = 1,
26 Number = 2,
29
30 LBrace = '{',
31 RBrace = '}',
32 Equals = '=',
33 Hash = '#'
34 };
35
37 std::string_view range;
38
39 std::string_view trim(int32_t start, int32_t end)
40 {
41 size_t pos = start < 0 ? range.size() - start : start;
42 size_t len = (end < 0 ? range.size() + end : end) - start;
43 return range.substr(pos, len);
44 }
45 };
46
47 struct Tokenizer {
48 public:
49 Tokenizer(std::string_view data);
50
51 // Returns the next token in the stream.
52 // TODO: support for pushing the last token back on the stream (for look-ahead)
53 Token advance();
54
55 // Discard all tokens on the current line, such that a subsequent call to
56 // advance() will return the first token on the next non-empty line.
57 // Returns true if any tokens were discarded.
58 bool discardLine();
59 bool peekIsNextLine() const { return nextLine != currentLine; }
60
61 const Token &peek() const { return next; }
62 uint32_t getCurrentLine() const { return currentLine; }
63 uint32_t getCurrentChar() const { return currentOffset; }
64
65 private:
66 std::string_view consume(size_t num);
67
68 Token next;
69 std::string_view remaining;
70 uint32_t currentLine;
71 uint32_t currentOffset;
72 uint32_t nextLine;
73 uint32_t nextOffset;
74 };
75
76 struct TextureInfo {
78 std::string bindName;
79 std::string name;
80 uint32_t binding;
81 };
82
83 struct BufferInfo {
84 std::string name;
85 uint32_t binding;
86 };
87
90 std::string name;
91 uint32_t binding;
92 };
93
94 struct ShaderInfo {
95 std::string name;
96 std::string vertexPath;
97 std::string fragmentPath;
98
99 std::vector<TextureInfo> textureBindings;
100 std::vector<BufferInfo> bufferBindings;
101 std::vector<PushConstantInfo> pushConstantBindings;
102 };
103
104 class Parser {
105 public:
106 ShaderInfo Parse(std::string filename, std::string_view fileData);
107
108 protected:
112 Parsed
113 };
114
118
119 // advance a token and handle comments
120 void advance();
121 // advance a token if the next one is of the given type, ignoring comments
122 bool advanceIfType(Token::Type type, bool shouldExpect = true);
123 // advance a token if the next one is the given keyword, ignoring comments
124 bool advanceIfKeyword(std::string_view keyword);
126
127 bool expect(Token::Type type, const Token &tok);
128 bool isKeyword(std::string_view keyword, const Token &tok);
129 std::string makeLineInfo();
130
131 private:
132 std::string m_fileName;
133 Token m_currentTok;
134 ShaderInfo m_shaderData;
135 uint32_t m_nextTextureBinding;
136 uint32_t m_nextBufferBinding;
137 uint32_t m_nextPushConstantBinding;
138 std::unique_ptr<Tokenizer> m_tokenizer;
139 };
140
141 } // namespace ShaderParser
142
143} // namespace Graphics
Definition ShaderParser.h:104
ParseResult parseTexture()
Definition ShaderParser.cpp:220
ParseResult parsePushConstant()
Definition ShaderParser.cpp:287
bool advanceIfType(Token::Type type, bool shouldExpect=true)
Definition ShaderParser.cpp:390
std::string makeLineInfo()
Definition ShaderParser.cpp:352
ParseResult
Definition ShaderParser.h:109
@ DidNotMatch
Definition ShaderParser.h:110
@ ParseFailure
Definition ShaderParser.h:111
@ Parsed
Definition ShaderParser.h:112
bool expect(Token::Type type, const Token &tok)
Definition ShaderParser.cpp:359
bool isKeyword(std::string_view keyword, const Token &tok)
Definition ShaderParser.cpp:372
ShaderInfo Parse(std::string filename, std::string_view fileData)
Definition ShaderParser.cpp:150
ParseResult parseBuffer()
Definition ShaderParser.cpp:263
bool advanceIfLineRemaining()
Definition ShaderParser.cpp:427
bool advanceIfKeyword(std::string_view keyword)
Definition ShaderParser.cpp:410
void advance()
Definition ShaderParser.cpp:378
Definition Background.h:14
TextureType
Definition Texture.h:38
ConstantDataFormat
Definition Types.h:55
Definition ShaderParser.h:83
std::string name
Definition ShaderParser.h:84
uint32_t binding
Definition ShaderParser.h:85
Definition ShaderParser.h:88
std::string name
Definition ShaderParser.h:90
Graphics::ConstantDataFormat type
Definition ShaderParser.h:89
uint32_t binding
Definition ShaderParser.h:91
Definition ShaderParser.h:94
std::vector< TextureInfo > textureBindings
Definition ShaderParser.h:99
std::vector< BufferInfo > bufferBindings
Definition ShaderParser.h:100
std::string fragmentPath
Definition ShaderParser.h:97
std::vector< PushConstantInfo > pushConstantBindings
Definition ShaderParser.h:101
std::string name
Definition ShaderParser.h:95
std::string vertexPath
Definition ShaderParser.h:96
Definition ShaderParser.h:76
std::string bindName
Definition ShaderParser.h:78
uint32_t binding
Definition ShaderParser.h:80
Graphics::TextureType type
Definition ShaderParser.h:77
std::string name
Definition ShaderParser.h:79
Definition ShaderParser.h:19
std::string_view trim(int32_t start, int32_t end)
Definition ShaderParser.h:39
std::string_view range
Definition ShaderParser.h:37
Type
Definition ShaderParser.h:23
@ LBrace
Definition ShaderParser.h:30
@ String
Definition ShaderParser.h:25
@ Number
Definition ShaderParser.h:26
@ RBrace
Definition ShaderParser.h:31
@ Identifier
Definition ShaderParser.h:27
@ Equals
Definition ShaderParser.h:32
@ EndOfFile
Definition ShaderParser.h:28
@ Hash
Definition ShaderParser.h:33
@ Discard
Definition ShaderParser.h:24
Type type
Definition ShaderParser.h:36
Definition ShaderParser.h:47
Token advance()
Definition ShaderParser.cpp:74
uint32_t getCurrentLine() const
Definition ShaderParser.h:62
bool peekIsNextLine() const
Definition ShaderParser.h:59
const Token & peek() const
Definition ShaderParser.h:61
bool discardLine()
Definition ShaderParser.cpp:132
uint32_t getCurrentChar() const
Definition ShaderParser.h:63