Skip to content

Commit f89a5c6

Browse files
authored
Merge pull request #541 from sdslabs/custom-constant-buffer
Custom constant buffer
2 parents bdc6c81 + 69f288b commit f89a5c6

13 files changed

+228
-34
lines changed

rootex/core/renderer/renderer.cpp

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,35 +28,27 @@ void Renderer::resetCurrentShader()
2828

2929
void Renderer::bind(MaterialResourceFile* newMaterial, MaterialResourceFile* oldMaterial)
3030
{
31-
BasicMaterialResourceFile* BasicMaterialResourceFilePointer = dynamic_cast<BasicMaterialResourceFile*>(newMaterial);
32-
if (BasicMaterialResourceFilePointer != nullptr)
31+
ZoneNamedN(materialBind, "Render Material Bind", true);
32+
BasicMaterialResourceFile* basicMaterialResourceFilePointer = dynamic_cast<BasicMaterialResourceFile*>(newMaterial);
33+
if (basicMaterialResourceFilePointer)
3334
{
34-
ZoneNamedN(materialBind, "Render Material Bind", true);
35-
if (newMaterial->getShader() != m_CurrentShader)
36-
{
37-
ZoneNamedN(materialBind, "Shader Bind", true);
38-
m_CurrentShader = newMaterial->getShader();
39-
newMaterial->bindShader();
40-
}
4135
newMaterial->bindSamplers();
4236
newMaterial->bindTextures();
43-
newMaterial->bindVSCB();
44-
newMaterial->bindPSCB();
4537
}
4638
else
4739
{
48-
ZoneNamedN(materialBind, "Render Material Bind", true);
49-
if (newMaterial->getShader() != m_CurrentShader)
50-
{
51-
ZoneNamedN(materialBind, "Shader Bind", true);
52-
m_CurrentShader = newMaterial->getShader();
53-
newMaterial->bindShader();
54-
}
5540
oldMaterial->bindSamplers();
5641
oldMaterial->bindTextures();
57-
oldMaterial->bindVSCB();
5842
oldMaterial->bindPSCB();
5943
}
44+
if (newMaterial->getShader() != m_CurrentShader)
45+
{
46+
ZoneNamedN(materialBind, "Shader Bind", true);
47+
m_CurrentShader = newMaterial->getShader();
48+
newMaterial->bindShader();
49+
}
50+
newMaterial->bindVSCB();
51+
newMaterial->bindPSCB();
6052
}
6153
void Renderer::bind(MaterialResourceFile* material)
6254
{

rootex/core/renderer/shaders/cel_pixel_shader.hlsl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
#include "basic_material.hlsli"
44
#include "sky.hlsli"
55

6-
#define NUMBER_OF_SHADES float(2.0)
76

87
TextureCube SkyTexture : register(SKY_PS_HLSL);
98
Texture2D ShaderTexture : register(DIFFUSE_PS_HLSL);
@@ -29,6 +28,11 @@ cbuffer CBuf : register(PER_OBJECT_PS_HLSL)
2928
BasicMaterial material;
3029
};
3130

31+
cbuffer CBuf : register(CUSTOM_PER_OBJECT_PS_HLSL)
32+
{
33+
float NUMBER_OF_SHADES = 2.0;
34+
};
35+
3236
cbuffer CBuf : register(PER_MODEL_PS_HLSL)
3337
{
3438
int staticPointLightAffectingCount = 0;

rootex/core/renderer/shaders/register_locations_pixel_shader.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#define PER_CAMERA_CHANGE_PS_HLSL CONCAT(b, PER_CAMERA_CHANGE_PS_CPP)
1818
#define PER_DECAL_PS_CPP 7
1919
#define PER_DECAL_PS_HLSL CONCAT(b, PER_DECAL_PS_CPP)
20+
#define CUSTOM_PER_OBJECT_PS_CPP 8
21+
#define CUSTOM_PER_OBJECT_PS_HLSL CONCAT(b, CUSTOM_PER_OBJECT_PS_CPP)
2022

2123
#define DIFFUSE_PS_CPP 1
2224
#define DIFFUSE_PS_HLSL CONCAT(t, DIFFUSE_PS_CPP)

rootex/core/resource_files/custom_material_resource_file.cpp

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "core/renderer/shaders/register_locations_vertex_shader.h"
66
#include "framework/systems/render_system.h"
77
#include "resource_loader.h"
8+
#define MAX_NUMBER_OF_CUSTOM_CB 8
89

910
void to_json(JSON::json& j, const CustomMaterialData& s)
1011
{
@@ -20,6 +21,14 @@ void to_json(JSON::json& j, const CustomMaterialData& s)
2021
{
2122
j["vertexShaderTextures"].push_back(texture->getPath().generic_string());
2223
}
24+
for (auto& customConstantBuffers : s.customConstantBuffers)
25+
{
26+
j["customConstantBuffers"].push_back(customConstantBuffers);
27+
}
28+
for (auto& typeOfCustomConstantBuffers : s.typeOfCustomConstantBuffers)
29+
{
30+
j["typeOfCustomConstantBuffers"].push_back(typeOfCustomConstantBuffers);
31+
}
2332
}
2433

2534
void from_json(const JSON::json& j, CustomMaterialData& s)
@@ -40,6 +49,14 @@ void from_json(const JSON::json& j, CustomMaterialData& s)
4049
s.vertexShaderTextures.push_back(texture);
4150
}
4251
}
52+
for (auto& customConstantBuffers : j.value("customConstantBuffers", Vector<float>()))
53+
{
54+
s.customConstantBuffers.push_back(customConstantBuffers);
55+
}
56+
for (auto& typeOfCustomConstantBuffers : j.value("typeOfCustomConstantBuffers", Vector<TYPES_OF_BUFFERS>()))
57+
{
58+
s.typeOfCustomConstantBuffers.push_back(typeOfCustomConstantBuffers);
59+
}
4360
}
4461

4562
void CustomMaterialResourceFile::Load()
@@ -228,6 +245,9 @@ void CustomMaterialResourceFile::bindVSCB()
228245

229246
void CustomMaterialResourceFile::bindPSCB()
230247
{
248+
int size = customConstantBuffers.size() * sizeof(float);
249+
RenderingDevice::GetSingleton()->editBuffer((const char*)customConstantBuffers.data(), size, m_PSCB.Get());
250+
RenderingDevice::GetSingleton()->setPSCB(CUSTOM_PER_OBJECT_PS_CPP, 1, m_PSCB.GetAddressOf());
231251
}
232252

233253
JSON::json CustomMaterialResourceFile::getJSON() const
@@ -253,7 +273,12 @@ void CustomMaterialResourceFile::reimport()
253273
m_MaterialData = j;
254274
MaterialResourceFile::readJSON(j);
255275

276+
customConstantBuffers = m_MaterialData.customConstantBuffers;
277+
typeOfCustomConstantBuffers = m_MaterialData.typeOfCustomConstantBuffers;
278+
256279
recompileShaders();
280+
float fakeArray[MAX_NUMBER_OF_CUSTOM_CB * 4];
281+
m_PSCB = RenderingDevice::GetSingleton()->createBuffer((const char*)fakeArray, sizeof(fakeArray), D3D11_BIND_CONSTANT_BUFFER, D3D11_USAGE_DYNAMIC, D3D11_CPU_ACCESS_WRITE);
257282
m_VSCB = RenderingDevice::GetSingleton()->createBuffer<PerModelVSCBData>(PerModelVSCBData(), D3D11_BIND_CONSTANT_BUFFER, D3D11_USAGE_DYNAMIC, D3D11_CPU_ACCESS_WRITE);
258283
}
259284

@@ -262,6 +287,73 @@ bool CustomMaterialResourceFile::save()
262287
return saveMaterialData(getJSON());
263288
}
264289

290+
float CustomMaterialResourceFile::getFloat(int index)
291+
{
292+
if (4 * index < customConstantBuffers.size())
293+
return customConstantBuffers[4 * index];
294+
return 0.0f;
295+
}
296+
297+
Vector3 CustomMaterialResourceFile::getFloat3(int index)
298+
{
299+
Vector3 temp = { 0.0f, 0.0f, 0.0f };
300+
if (4 * index < customConstantBuffers.size())
301+
{
302+
temp.x = customConstantBuffers[4 * index];
303+
temp.y = customConstantBuffers[4 * index + 1];
304+
temp.z = customConstantBuffers[4 * index + 2];
305+
}
306+
return temp;
307+
}
308+
309+
Color CustomMaterialResourceFile::getColor(int index)
310+
{
311+
Color temp = { 0.0f, 0.0f, 0.0f, 0.0f };
312+
if (4 * index < customConstantBuffers.size())
313+
{
314+
temp.x = customConstantBuffers[4 * index];
315+
temp.y = customConstantBuffers[4 * index + 1];
316+
temp.z = customConstantBuffers[4 * index + 2];
317+
temp.w = customConstantBuffers[4 * index + 3];
318+
}
319+
return temp;
320+
}
321+
322+
bool CustomMaterialResourceFile::setFloat(int index, float value)
323+
{
324+
if (4 * index < customConstantBuffers.size())
325+
{
326+
customConstantBuffers[4 * index] = value;
327+
return true;
328+
}
329+
return false;
330+
}
331+
332+
bool CustomMaterialResourceFile::setFloat3(int index, Vector3 value)
333+
{
334+
if (4 * index < customConstantBuffers.size())
335+
{
336+
customConstantBuffers[4 * index] = value.x;
337+
customConstantBuffers[4 * index + 1] = value.y;
338+
customConstantBuffers[4 * index + 2] = value.z;
339+
return true;
340+
}
341+
return false;
342+
}
343+
344+
bool CustomMaterialResourceFile::setColor(int index, Color value)
345+
{
346+
if (4 * index < customConstantBuffers.size())
347+
{
348+
customConstantBuffers[4 * index] = value.x;
349+
customConstantBuffers[4 * index + 1] = value.y;
350+
customConstantBuffers[4 * index + 2] = value.z;
351+
customConstantBuffers[4 * index + 3] = value.w;
352+
return true;
353+
}
354+
return false;
355+
}
356+
265357
void CustomMaterialResourceFile::draw()
266358
{
267359
MaterialResourceFile::draw();
@@ -442,4 +534,68 @@ void CustomMaterialResourceFile::draw()
442534

443535
ImGui::TreePop();
444536
}
537+
538+
for (int i = 0; i < customConstantBuffers.size(); i += 4)
539+
{
540+
String customConstantBufferName = "CB Slot " + std::to_string(i / 4);
541+
switch (typeOfCustomConstantBuffers[i / 4])
542+
{
543+
case TYPES_OF_BUFFERS::FLOATCB:
544+
ImGui::DragFloat(customConstantBufferName.c_str(), &customConstantBuffers[i], 0.01f, 0.0f, 10.0f);
545+
break;
546+
case TYPES_OF_BUFFERS::FLOAT3CB:
547+
ImGui::DragFloat3(customConstantBufferName.c_str(), &customConstantBuffers[i], 0.01f, 0.0f, 10.0f);
548+
break;
549+
case TYPES_OF_BUFFERS::COLORCB:
550+
ImGui::ColorPicker4(customConstantBufferName.c_str(), &customConstantBuffers[i]);
551+
break;
552+
}
553+
ImGui::Separator();
554+
}
555+
556+
if (customConstantBuffers.size() < MAX_NUMBER_OF_CUSTOM_CB * sizeof(float))
557+
{
558+
if (ImGui::Button(ICON_ROOTEX_PLUS "Push float CB"))
559+
{
560+
float value = 1.0;
561+
customConstantBuffers.push_back(value);
562+
customConstantBuffers.push_back(value);
563+
customConstantBuffers.push_back(value);
564+
customConstantBuffers.push_back(value);
565+
typeOfCustomConstantBuffers.push_back(TYPES_OF_BUFFERS::FLOATCB);
566+
}
567+
ImGui::SameLine();
568+
569+
if (ImGui::Button(ICON_ROOTEX_PLUS "Push float3 CB"))
570+
{
571+
float value = 1.0;
572+
customConstantBuffers.push_back(value);
573+
customConstantBuffers.push_back(value);
574+
customConstantBuffers.push_back(value);
575+
customConstantBuffers.push_back(value);
576+
typeOfCustomConstantBuffers.push_back(TYPES_OF_BUFFERS::FLOAT3CB);
577+
}
578+
ImGui::SameLine();
579+
580+
if (ImGui::Button(ICON_ROOTEX_PLUS "Push Color CB"))
581+
{
582+
float value = 1.0;
583+
customConstantBuffers.push_back(value);
584+
customConstantBuffers.push_back(value);
585+
customConstantBuffers.push_back(value);
586+
customConstantBuffers.push_back(value);
587+
typeOfCustomConstantBuffers.push_back(TYPES_OF_BUFFERS::COLORCB);
588+
}
589+
ImGui::SameLine();
590+
}
591+
if (ImGui::Button(ICON_ROOTEX_MINUS "Pop CB"))
592+
{
593+
customConstantBuffers.pop_back();
594+
customConstantBuffers.pop_back();
595+
customConstantBuffers.pop_back();
596+
customConstantBuffers.pop_back();
597+
typeOfCustomConstantBuffers.pop_back();
598+
}
599+
m_MaterialData.customConstantBuffers = customConstantBuffers;
600+
m_MaterialData.typeOfCustomConstantBuffers = typeOfCustomConstantBuffers;
445601
}

rootex/core/resource_files/custom_material_resource_file.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,13 @@ class CustomMaterialResourceFile : public MaterialResourceFile
1010
{
1111
private:
1212
static inline Microsoft::WRL::ComPtr<ID3D11SamplerState> s_Sampler;
13+
Vector<float> customConstantBuffers;
14+
Vector<TYPES_OF_BUFFERS> typeOfCustomConstantBuffers;
1315

1416
CustomMaterialData m_MaterialData;
1517

1618
Ptr<Shader> m_Shader;
19+
Microsoft::WRL::ComPtr<ID3D11Buffer> m_PSCB;
1720
Microsoft::WRL::ComPtr<ID3D11Buffer> m_VSCB;
1821

1922
void pushPSTexture(Ref<ImageResourceFile> texture);
@@ -55,4 +58,12 @@ class CustomMaterialResourceFile : public MaterialResourceFile
5558
bool save() override;
5659
void draw() override;
5760
void drawTextureSlots(const char* label, Vector<Ref<ImageResourceFile>>& textures);
61+
62+
float getFloat(int index);
63+
Vector3 getFloat3(int index);
64+
Color getColor(int index);
65+
66+
bool setFloat(int index, float value);
67+
bool setFloat3(int index, Vector3 value);
68+
bool setColor(int index, Color value);
5869
};

rootex/core/resource_files/material_resource_file.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ class MaterialResourceFile : public ResourceFile
3434
bool isAlpha() const { return m_IsAlpha; }
3535

3636
void draw() override;
37+
template <class T>
38+
T* as() { return dynamic_cast<T*>(this); }
3739
};
3840

3941
/// Kept separate from the main data buffer class because it needs proper packing
@@ -115,12 +117,21 @@ struct SkyMaterialData
115117
void to_json(JSON::json& j, const SkyMaterialData& s);
116118
void from_json(const JSON::json& j, SkyMaterialData& s);
117119

120+
enum class TYPES_OF_BUFFERS
121+
{
122+
FLOATCB,
123+
FLOAT3CB,
124+
COLORCB
125+
};
126+
118127
struct CustomMaterialData
119128
{
120129
String vertexShaderPath;
121130
String pixelShaderPath;
122131
Vector<Ref<ImageResourceFile>> vertexShaderTextures;
123132
Vector<Ref<ImageResourceFile>> pixelShaderTextures;
133+
Vector<float> customConstantBuffers;
134+
Vector<TYPES_OF_BUFFERS> typeOfCustomConstantBuffers;
124135
};
125136

126137
void to_json(JSON::json& j, const CustomMaterialData& s);

rootex/framework/components/visual/model/animated_model_component.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ void AnimatedModelComponent::render(float viewDistance)
4545
bool uploadBones = true;
4646
for (auto& [material, meshes] : m_AnimatedModelResourceFile->getMeshes())
4747
{
48-
if (Ref<AnimatedBasicMaterialResourceFile> overridingMaterial = std::dynamic_pointer_cast<AnimatedBasicMaterialResourceFile>(m_MaterialOverrides[material]))
48+
if (Ref<AnimatedBasicMaterialResourceFile> overridingMaterial = std::dynamic_pointer_cast<AnimatedBasicMaterialResourceFile>(m_MaterialOverrides[material.get()]))
4949
{
5050
if (uploadBones)
5151
{
@@ -194,12 +194,12 @@ void AnimatedModelComponent::assignOverrides(Ref<AnimatedModelResourceFile> file
194194
m_MaterialOverrides.clear();
195195
for (auto& [material, meshes] : m_AnimatedModelResourceFile->getMeshes())
196196
{
197-
setMaterialOverride(material, material);
197+
setMaterialOverride(material.get(), material);
198198
}
199199
for (auto& [oldMaterial, newMaterial] : materialOverrides)
200200
{
201201
setMaterialOverride(
202-
ResourceLoader::CreateNewAnimatedBasicMaterialResourceFile(oldMaterial),
202+
ResourceLoader::CreateNewAnimatedBasicMaterialResourceFile(oldMaterial).get(),
203203
ResourceLoader::CreateNewAnimatedBasicMaterialResourceFile(newMaterial));
204204
}
205205
}

rootex/framework/components/visual/model/model_component.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ void ModelComponent::render(float viewDistance)
5151

5252
for (auto& [material, meshes] : m_ModelResourceFile->getMeshes())
5353
{
54-
RenderSystem::GetSingleton()->getRenderer()->bind(m_MaterialOverrides.at(material).get(), material.get());
54+
RenderSystem::GetSingleton()->getRenderer()->bind(m_MaterialOverrides.at(material.get()).get(), material.get());
5555
i++;
5656

5757
for (auto& mesh : meshes)
@@ -109,11 +109,11 @@ void ModelComponent::assignOverrides(Ref<ModelResourceFile> newModel, const Hash
109109
m_MaterialOverrides.clear();
110110
for (auto& [material, meshes] : m_ModelResourceFile->getMeshes())
111111
{
112-
setMaterialOverride(material, material);
112+
setMaterialOverride(material.get(), material);
113113
}
114114
for (auto& [oldMaterial, newMaterial] : materialOverrides)
115115
{
116-
setMaterialOverride(ResourceLoader::CreateMaterialResourceFile(oldMaterial), ResourceLoader::CreateMaterialResourceFile(newMaterial));
116+
setMaterialOverride(ResourceLoader::CreateMaterialResourceFile(oldMaterial).get(), ResourceLoader::CreateMaterialResourceFile(newMaterial));
117117
}
118118
}
119119

0 commit comments

Comments
 (0)