blob: aaf8b247d2c7828cedb12c4e4c6161653a549739 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
#pragma once
#include "../../DataView.hpp"
#include "../../utils.hpp"
#include "../../Result.hpp"
#include "CommonShader.hpp"
#include "ShaderVec.hpp"
#include <string>
#include <unordered_map>
#include <vector>
#include <stdexcept>
#include <functional>
namespace amalgine
{
class VertexShaderTooManyAttributes : public std::runtime_error
{
public:
VertexShaderTooManyAttributes(i32 maxVertexAttributes);
};
class VertexShaderAttributeAlreadyDefined : public std::runtime_error
{
public:
VertexShaderAttributeAlreadyDefined(const std::string &attributeName);
};
class VertexShaderInvalidAttributeName : public std::runtime_error
{
public:
VertexShaderInvalidAttributeName(const std::string &attributeName);
};
class VertexShaderFunctionAlreadyDefined : public std::runtime_error
{
public:
VertexShaderFunctionAlreadyDefined(const std::string &funcName);
};
using VertexShaderMainFunc = std::function<ShaderVec4()>;
/*
* Using the same VertexShader instance in multiple threads to define data is not thread safe.
* All get*** functions are thread safe.
*/
class CompiledVertexShader;
class VertexShader
{
DISABLE_COPY(VertexShader)
friend class ShaderProgram;
public:
VertexShader();
const std::string& getInputAttributeName(i32 attributeIndex) const;
AttributeType getInputAttributeType(i32 attributeIndex) const;
ShaderInputVec2 defineInputVec2(const std::string &name);
void defineMain(VertexShaderMainFunc mainFunc);
Result<CompiledVertexShader*> compile();
private:
void writeHeader(const std::string &code);
void writeBody(const std::string &code);
std::string build() const;
/*
* Throws VertexShaderTooManyInputAttributes if too many vertex attributes are defined for the platform.
* Throws VertexShaderAttributeAlreadyDefined if a vertex attribute with the same name has already been defined.
*/
i32 defineInputVariable(const std::string &variableName, const char *typeName);
private:
std::string header;
std::string body;
int locationCounter;
i32 maxVertexAttribs; // Could make this static
std::unordered_map<std::string, i32> vertexAttributes;
std::vector<ShaderAttribute> vertexAttributeNames;
bool mainFuncDefined;
};
}
|