Skip to content

Commit 6eda1d0

Browse files
Update shader.hpp
1 parent e168fa7 commit 6eda1d0

File tree

1 file changed

+114
-24
lines changed

1 file changed

+114
-24
lines changed

include/borealis/gfx/shader.hpp

Lines changed: 114 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ namespace brl
1515
struct GfxDrawCall;
1616
struct GfxAttribBuffer;
1717
struct GfxMaterial;
18-
using GfxUniformList = std::unordered_map<GfxShaderUniform*, GfxShaderValue>;
18+
19+
1920

2021
struct GfxShader
2122
{
@@ -50,6 +51,7 @@ namespace brl
5051
std::string name;
5152
GfxUniformType type;
5253
int location;
54+
int count;
5355
GfxShaderUniform() = default;
5456
};
5557

@@ -60,8 +62,51 @@ namespace brl
6062
glm::vec2 v2value;
6163
glm::vec3 v3value;
6264
glm::vec4 v4value;
63-
glm::mat4 m4value;
64-
GfxTexture* txValue;
65+
std::shared_ptr<std::vector<glm::mat4>> m4value = nullptr;
66+
GfxTexture* txValue = nullptr;
67+
};
68+
69+
struct GfxShaderBinding
70+
{
71+
brl::GfxShaderUniform* uniform = nullptr;
72+
brl::GfxShaderValue* value = nullptr;
73+
74+
GfxShaderBinding() = default;
75+
GfxShaderBinding(brl::GfxShaderUniform* u, brl::GfxShaderValue* v) : uniform(u), value(v) {}
76+
77+
~GfxShaderBinding();
78+
};
79+
80+
struct GfxUniformList : public std::vector<GfxShaderBinding>
81+
{
82+
bool contains(GfxShaderUniform* uniform);
83+
84+
85+
86+
// Non-const version for read/write access
87+
GfxShaderValue*& operator[](GfxShaderUniform* uniform)
88+
{
89+
for (auto& element1 : *this) // Non-const loop for non-const method
90+
{
91+
if (element1.uniform == uniform)
92+
return element1.value;
93+
}
94+
95+
// Not found - throw exception or add default behavior
96+
throw std::out_of_range("Uniform not found");
97+
}
98+
99+
// Const version for read-only access
100+
GfxShaderValue* const& operator[](GfxShaderUniform* uniform) const
101+
{
102+
for (const auto& element1 : *this)
103+
{
104+
if (element1.uniform == uniform)
105+
return element1.value;
106+
}
107+
108+
throw std::out_of_range("Uniform not found");
109+
}
65110
};
66111

67112
struct GfxShaderProgram
@@ -137,8 +182,8 @@ namespace brl
137182
if (!shader->getUniform(name))
138183
return;
139184

140-
GfxShaderValue val;
141-
val.intValue = value;
185+
GfxShaderValue* val = new GfxShaderValue();
186+
val->intValue = value;
142187
setOverride({shader->getUniform(name), val});
143188
}
144189

@@ -149,8 +194,8 @@ namespace brl
149194
if (!shader->getUniform(name))
150195
return;
151196

152-
GfxShaderValue val;
153-
val.floatValue = value;
197+
GfxShaderValue* val = new GfxShaderValue();
198+
val->floatValue = value;
154199
setOverride({shader->getUniform(name), val});
155200
}
156201

@@ -161,8 +206,8 @@ namespace brl
161206
if (!shader->getUniform(name))
162207
return;
163208

164-
GfxShaderValue val;
165-
val.v2value = value;
209+
GfxShaderValue* val = new GfxShaderValue();
210+
val->v2value = value;
166211
setOverride({shader->getUniform(name), val});
167212
}
168213

@@ -172,8 +217,8 @@ namespace brl
172217
if (shader->getUniform(name) == nullptr)
173218
return;
174219

175-
GfxShaderValue val;
176-
val.v3value = value;
220+
GfxShaderValue* val = new GfxShaderValue();
221+
val->v3value = value;
177222
setOverride({shader->getUniform(name), val});
178223
}
179224

@@ -184,39 +229,68 @@ namespace brl
184229
if (!shader->getUniform(name))
185230
return;
186231

187-
GfxShaderValue val;
188-
val.v4value = value;
232+
GfxShaderValue* val = new GfxShaderValue();
233+
val->v4value = value;
189234
setOverride({shader->getUniform(name), val});
190235
}
191236

192-
void setMat4(std::string name, glm::mat4 value)
237+
void setMat4(std::string name, glm::mat4 value, int index=0)
193238
{
239+
const auto uniform = shader->getUniform(name);
194240

241+
if (!uniform)
242+
return;
195243

196-
if (!shader->getUniform(name))
244+
if (overrides.contains(uniform))
245+
{
246+
overrides[uniform]->m4value->operator[](index) = value;
247+
}
248+
else
249+
{
250+
251+
GfxShaderValue* val = new GfxShaderValue();
252+
val->m4value->reserve(uniform->count);
253+
val->m4value->operator[](index) = value;
254+
setOverride({uniform, val});
255+
}
256+
}
257+
258+
void setMat4(std::string name, std::vector<glm::mat4>& value)
259+
{
260+
const auto uniform = shader->getUniform(name);
261+
262+
if (!uniform)
197263
return;
198264

199-
GfxShaderValue val;
200-
val.m4value = value;
201-
setOverride({shader->getUniform(name), val});
265+
if (overrides.contains(uniform))
266+
{
267+
overrides[uniform]->m4value = std::make_shared<std::vector<glm::mat4>>(value);
268+
}
269+
else
270+
{
271+
272+
GfxShaderValue* val = new GfxShaderValue();
273+
val->m4value = std::make_shared<std::vector<glm::mat4>>(value);
274+
setOverride({uniform, val});
275+
}
202276
}
203277

204278
void setTexture(std::string name, GfxTexture* value)
205279
{
206280
if (!shader->getUniform(name))
207281
return;
208282

209-
GfxShaderValue val;
210-
val.txValue = value;
283+
GfxShaderValue* val = new GfxShaderValue();
284+
val->txValue = value;
211285
setOverride({shader->getUniform(name), val});
212286
}
213287

214-
GfxShaderValue getUniform(std::string name)
288+
GfxShaderValue* getUniform(std::string name)
215289
{
216290
for (const auto& uniform : overrides)
217291
{
218-
if (uniform.first->name == name)
219-
return uniform.second;
292+
if (uniform.uniform->name == name)
293+
return uniform.value;
220294
}
221295

222296
return {};
@@ -235,7 +309,7 @@ namespace brl
235309
GfxUniformList overrides;
236310
unsigned int registryIndex = UINT_MAX;
237311

238-
void setOverride(std::pair<GfxShaderUniform*, GfxShaderValue> pair);
312+
void setOverride(GfxShaderBinding pair);
239313
};
240314

241315
struct GfxMaterialMgr
@@ -253,6 +327,22 @@ namespace brl
253327

254328
};
255329

330+
331+
struct GfxDrawCall
332+
{
333+
GfxMaterial* material;
334+
GfxAttribBuffer* gfxBuffer;
335+
const glm::mat4 transform;
336+
GfxUniformList uniqueOverrides;
337+
};
338+
339+
struct GfxInstancedDrawCall
340+
{
341+
GfxMaterial* material;
342+
std::vector<glm::mat4> transforms;
343+
GfxAttribBuffer* gfxBuffer;
344+
};
345+
256346
} // namespace brl
257347

258348

0 commit comments

Comments
 (0)