-
Notifications
You must be signed in to change notification settings - Fork 0
/
shader_def.cpp
179 lines (157 loc) · 4.53 KB
/
shader_def.cpp
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include "shader_def.h"
#include <cassert>
#include <cstring>
//-----------------------------------API INTERNAL CODE------------------------------------
static const int NAME_MAX_LENGTH = 30;
static const int SHADER_PARAM_MAX_COUNT = 10;
static const int SHADER_DEF_MAX_COUNT = 10;
struct fixed_string
{
char Str[NAME_MAX_LENGTH];
};
struct shader_def
{
int32_t Count;
int32_t CurrentIndex;
fixed_string Names[SHADER_PARAM_MAX_COUNT];
fixed_string UniformNames[SHADER_PARAM_MAX_COUNT];
shader_param_def Params[SHADER_PARAM_MAX_COUNT];
};
struct shader_defs
{
int32_t Count;
fixed_string Names[SHADER_DEF_MAX_COUNT];
int32_t Types[SHADER_DEF_MAX_COUNT];
rid RIDs[SHADER_DEF_MAX_COUNT];
shader_def Defs[SHADER_DEF_MAX_COUNT];
} static g_ShaderDefs;
static int32_t
CompareFixedString(const fixed_string* FixedString, const char* String)
{
assert(String);
size_t SourceStringLength = strnlen(String, sizeof(fixed_string));
assert(SourceStringLength < sizeof(fixed_string));
return strncmp((const char*)FixedString, String, SourceStringLength);
}
static void
SetFixedString(fixed_string* Dest, const char* Source)
{
assert(Source);
assert(Dest);
size_t SourceLength = strnlen(Source, sizeof(fixed_string));
assert(SourceLength < sizeof(fixed_string));
strncpy((char*)Dest->Str, Source, SourceLength);
}
//---------------------------------PUBLIC API IMPLEMENTATION-------------------------------
shader_def*
AddShaderDef(int32_t ShaderType, rid ShaderRID, const char* ShaderName)
{
SetFixedString(&g_ShaderDefs.Names[g_ShaderDefs.Count], ShaderName);
g_ShaderDefs.Types[g_ShaderDefs.Count] = ShaderType;
g_ShaderDefs.RIDs[g_ShaderDefs.Count] = ShaderRID;
g_ShaderDefs.Defs[g_ShaderDefs.Count].Count = 0;
return &g_ShaderDefs.Defs[g_ShaderDefs.Count++];
}
void
AddParamDef(shader_def* ShaderDefPtr, const char* ParamName, const char* UniformName,
shader_param_def ParamDef)
{
assert(ShaderDefPtr);
assert(ParamName);
assert(UniformName);
SetFixedString(&ShaderDefPtr->Names[ShaderDefPtr->Count], ParamName);
SetFixedString(&ShaderDefPtr->UniformNames[ShaderDefPtr->Count], UniformName);
ShaderDefPtr->Params[ShaderDefPtr->Count++] = ParamDef;
}
int32_t
GetShaderType(const struct shader_def* ShaderDef)
{
assert(ShaderDef);
size_t DefIndex = ShaderDef - g_ShaderDefs.Defs;
assert(DefIndex >= 0);
return g_ShaderDefs.Types[DefIndex];
}
rid
GetShaderRID(const shader_def* ShaderDef)
{
assert(ShaderDef);
size_t DefIndex = ShaderDef - g_ShaderDefs.Defs;
assert(DefIndex >= 0);
return g_ShaderDefs.RIDs[DefIndex];
}
const char*
GetShaderName(const shader_def* ShaderDef)
{
assert(ShaderDef);
size_t DefIndex = ShaderDef - g_ShaderDefs.Defs;
assert(DefIndex >= 0);
return (char*)g_ShaderDefs.Names[DefIndex].Str;
}
bool
GetShaderDef(struct shader_def** OutputDefPtrPtr, const char* ShaderName)
{
for(int i = 0; i < g_ShaderDefs.Count; i++)
{
if(CompareFixedString(&g_ShaderDefs.Names[i], ShaderName) == 0)
{
*OutputDefPtrPtr = g_ShaderDefs.Defs + i;
return true;
}
}
*OutputDefPtrPtr = NULL;
return false;
}
bool
GetShaderDef(struct shader_def** OutputDefPtrPtr, int32_t ShaderType)
{
for(int i = 0; i < g_ShaderDefs.Count; i++)
{
if(g_ShaderDefs.Types[i] == ShaderType)
{
*OutputDefPtrPtr = g_ShaderDefs.Defs + i;
return true;
}
}
*OutputDefPtrPtr = NULL;
return false;
}
bool
GetShaderParamDef(shader_param_def* OutputParamPtr, const shader_def* ShaderDefPtr,
const char* ParamName)
{
int Count = ShaderDefPtr->Count;
for(int i = 0; i < Count; i++)
{
if(CompareFixedString(&ShaderDefPtr->Names[i], ParamName) == 0)
{
*OutputParamPtr = ShaderDefPtr->Params[i];
return true;
}
}
return false;
}
void
ResetShaderDefIterator(shader_def* ShaderDef)
{
assert(ShaderDef);
ShaderDef->CurrentIndex = 0;
}
// returns0 on error
bool
GetNextShaderParam(named_shader_param_def* Output, struct shader_def* ShaderDef)
{
int Index = ShaderDef->CurrentIndex;
if(Index < ShaderDef->Count)
{
named_shader_param_def Result = {};
Result.Name = (char*)ShaderDef->Names[Index].Str;
Result.UniformName = (char*)ShaderDef->UniformNames[Index].Str;
Result.OffsetIntoMaterial = ShaderDef->Params[Index].OffsetIntoMaterial;
Result.Type = ShaderDef->Params[Index].Type;
ShaderDef->CurrentIndex++;
*Output = Result;
return true;
}
ShaderDef->CurrentIndex = 0;
return false;
}