Skip to content

Commit e2fd835

Browse files
committed
clean up code and use enums
add enum classes clean up code add bool return type, fix minor changes fix initialization of temp variables
1 parent 6291e9c commit e2fd835

File tree

4 files changed

+109
-114
lines changed

4 files changed

+109
-114
lines changed

rootex/core/renderer/renderer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ void Renderer::bind(MaterialResourceFile* newMaterial, MaterialResourceFile* old
3030
{
3131
ZoneNamedN(materialBind, "Render Material Bind", true);
3232
BasicMaterialResourceFile* basicMaterialResourceFilePointer = dynamic_cast<BasicMaterialResourceFile*>(newMaterial);
33-
if (basicMaterialResourceFilePointer != nullptr)
33+
if (basicMaterialResourceFilePointer)
3434
{
3535
newMaterial->bindSamplers();
3636
newMaterial->bindTextures();

rootex/core/resource_files/custom_material_resource_file.cpp

Lines changed: 94 additions & 106 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
{
@@ -52,7 +53,7 @@ void from_json(const JSON::json& j, CustomMaterialData& s)
5253
{
5354
s.customConstantBuffers.push_back(customConstantBuffers);
5455
}
55-
for (auto& typeOfCustomConstantBuffers : j.value("typeOfCustomConstantBuffers", Vector<float>()))
56+
for (auto& typeOfCustomConstantBuffers : j.value("typeOfCustomConstantBuffers", Vector<TYPES_OF_BUFFERS>()))
5657
{
5758
s.typeOfCustomConstantBuffers.push_back(typeOfCustomConstantBuffers);
5859
}
@@ -276,8 +277,8 @@ void CustomMaterialResourceFile::reimport()
276277
typeOfCustomConstantBuffers = m_MaterialData.typeOfCustomConstantBuffers;
277278

278279
recompileShaders();
279-
float fakeArray[64];
280-
m_PSCB = RenderingDevice::GetSingleton()->createBuffer((const char*)fakeArray, 64 * sizeof(float), D3D11_BIND_CONSTANT_BUFFER, D3D11_USAGE_DYNAMIC, D3D11_CPU_ACCESS_WRITE);
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);
281282
m_VSCB = RenderingDevice::GetSingleton()->createBuffer<PerModelVSCBData>(PerModelVSCBData(), D3D11_BIND_CONSTANT_BUFFER, D3D11_USAGE_DYNAMIC, D3D11_CPU_ACCESS_WRITE);
282283
}
283284

@@ -288,46 +289,69 @@ bool CustomMaterialResourceFile::save()
288289

289290
float CustomMaterialResourceFile::getFloat(int index)
290291
{
291-
return customConstantBuffers[4 * index];
292+
if (4 * index < customConstantBuffers.size())
293+
return customConstantBuffers[4 * index];
294+
return 0.0f;
292295
}
293296

294-
Vector<float> CustomMaterialResourceFile::getFloat3(int index)
297+
Vector3 CustomMaterialResourceFile::getFloat3(int index)
295298
{
296-
Vector<float> temp;
297-
temp.push_back(customConstantBuffers[4 * index]);
298-
temp.push_back(customConstantBuffers[4 * index + 1]);
299-
temp.push_back(customConstantBuffers[4 * index + 2]);
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+
}
300306
return temp;
301307
}
302308

303-
Vector<float> CustomMaterialResourceFile::getColor(int index)
309+
Color CustomMaterialResourceFile::getColor(int index)
304310
{
305-
Vector<float> temp;
306-
temp.push_back(customConstantBuffers[4 * index]);
307-
temp.push_back(customConstantBuffers[4 * index + 1]);
308-
temp.push_back(customConstantBuffers[4 * index + 2]);
309-
temp.push_back(customConstantBuffers[4 * index + 3]);
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+
}
310319
return temp;
311320
}
312321

313-
void CustomMaterialResourceFile::setFloat(int index, float value)
322+
bool CustomMaterialResourceFile::setFloat(int index, float value)
314323
{
315-
customConstantBuffers[4 * index] = value;
324+
if (4 * index < customConstantBuffers.size())
325+
{
326+
customConstantBuffers[4 * index] = value;
327+
return true;
328+
}
329+
return false;
316330
}
317331

318-
void CustomMaterialResourceFile::setFloat3(int index, Vector3 value)
332+
bool CustomMaterialResourceFile::setFloat3(int index, Vector3 value)
319333
{
320-
customConstantBuffers[4 * index] = value.x;
321-
customConstantBuffers[4 * index + 1] = value.y;
322-
customConstantBuffers[4 * index + 2] = value.z;
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;
323342
}
324343

325-
void CustomMaterialResourceFile::setColor(int index, Color value)
344+
bool CustomMaterialResourceFile::setColor(int index, Color value)
326345
{
327-
customConstantBuffers[4 * index] = value.x;
328-
customConstantBuffers[4 * index + 1] = value.y;
329-
customConstantBuffers[4 * index + 2] = value.z;
330-
customConstantBuffers[4 * index + 3] = value.w;
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;
331355
}
332356

333357
void CustomMaterialResourceFile::draw()
@@ -511,95 +535,59 @@ void CustomMaterialResourceFile::draw()
511535
ImGui::TreePop();
512536
}
513537

514-
int i = 0;
515-
int j = 0;
516-
float tempFloat3[3];
517-
float* addressesFloat3[3];
518-
float tempColor[4];
519-
float* addressesColor[4];
520-
for (auto&& cb : customConstantBuffers)
538+
for (int i = 0; i < customConstantBuffers.size(); i += 4)
521539
{
522-
if (typeOfCustomConstantBuffers[i / 4] == 1.0)
540+
String customConstantBufferName = "CB Slot " + std::to_string(i / 4);
541+
switch (typeOfCustomConstantBuffers[i / 4])
523542
{
524-
if (i % 4 == 0)
525-
{
526-
String customConstantBufferName = "CB Slot " + std::to_string(i / 4);
527-
ImGui::DragFloat(customConstantBufferName.c_str(), &cb, 0.01f, 0.0f, 10.0f);
528-
ImGui::Separator();
529-
}
530-
}
531-
if (typeOfCustomConstantBuffers[i / 4] == 2.0)
532-
{
533-
tempFloat3[j % 3] = cb;
534-
addressesFloat3[j % 3] = &cb;
535-
if (i % 4 != 3)
536-
{
537-
j++;
538-
i++;
539-
continue;
540-
}
541-
String customConstantBufferName = "CB Slot " + std::to_string(i / 4);
542-
ImGui::DragFloat3(customConstantBufferName.c_str(), tempFloat3, 0.01f, 0.0f, 10.0f);
543-
for (int k = 0; k < 3; k++)
544-
{
545-
*addressesFloat3[k] = tempFloat3[k];
546-
}
547-
ImGui::Separator();
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;
548552
}
549-
if (typeOfCustomConstantBuffers[i / 4] == 3.0)
550-
{
551-
tempColor[i % 4] = cb;
552-
addressesColor[i % 4] = &cb;
553-
if (i % 4 != 3)
554-
{
555-
i++;
556-
continue;
557-
}
558-
String customConstantBufferName = "CB Slot " + std::to_string(i / 4);
559-
ImGui::ColorPicker4(customConstantBufferName.c_str(), tempColor);
560-
for (int k = 0; k < 4; k++)
561-
{
562-
*addressesColor[k] = tempColor[k];
563-
}
564-
ImGui::Separator();
565-
}
566-
i++;
567-
continue;
553+
ImGui::Separator();
568554
}
569555

570-
if (ImGui::Button(ICON_ROOTEX_PLUS "Push float CB"))
556+
if (customConstantBuffers.size() < MAX_NUMBER_OF_CUSTOM_CB * sizeof(float))
571557
{
572-
float value = 1.0;
573-
customConstantBuffers.push_back(value);
574-
customConstantBuffers.push_back(value);
575-
customConstantBuffers.push_back(value);
576-
customConstantBuffers.push_back(value);
577-
typeOfCustomConstantBuffers.push_back(1.0);
578-
}
579-
ImGui::SameLine();
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();
580568

581-
if (ImGui::Button(ICON_ROOTEX_PLUS "Push float3 CB"))
582-
{
583-
float value = 1.0;
584-
customConstantBuffers.push_back(value);
585-
customConstantBuffers.push_back(value);
586-
customConstantBuffers.push_back(value);
587-
customConstantBuffers.push_back(value);
588-
typeOfCustomConstantBuffers.push_back(2.0);
589-
}
590-
ImGui::SameLine();
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();
591579

592-
if (ImGui::Button(ICON_ROOTEX_PLUS "Push Color CB"))
593-
{
594-
float value = 1.0;
595-
customConstantBuffers.push_back(value);
596-
customConstantBuffers.push_back(value);
597-
customConstantBuffers.push_back(value);
598-
customConstantBuffers.push_back(value);
599-
typeOfCustomConstantBuffers.push_back(3.0);
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();
600590
}
601-
ImGui::SameLine();
602-
603591
if (ImGui::Button(ICON_ROOTEX_MINUS "Pop CB"))
604592
{
605593
customConstantBuffers.pop_back();

rootex/core/resource_files/custom_material_resource_file.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class CustomMaterialResourceFile : public MaterialResourceFile
1111
private:
1212
static inline Microsoft::WRL::ComPtr<ID3D11SamplerState> s_Sampler;
1313
Vector<float> customConstantBuffers;
14-
Vector<float> typeOfCustomConstantBuffers; // 1.0 -> float, 2.0 -> float3, 3.0 -> Color
14+
Vector<TYPES_OF_BUFFERS> typeOfCustomConstantBuffers;
1515

1616
CustomMaterialData m_MaterialData;
1717

@@ -60,10 +60,10 @@ class CustomMaterialResourceFile : public MaterialResourceFile
6060
void drawTextureSlots(const char* label, Vector<Ref<ImageResourceFile>>& textures);
6161

6262
float getFloat(int index);
63-
Vector<float> getFloat3(int index);
64-
Vector<float> getColor(int index);
63+
Vector3 getFloat3(int index);
64+
Color getColor(int index);
6565

66-
void setFloat(int index, float value);
67-
void setFloat3(int index, Vector3 value);
68-
void setColor(int index, Color value);
66+
bool setFloat(int index, float value);
67+
bool setFloat3(int index, Vector3 value);
68+
bool setColor(int index, Color value);
6969
};

rootex/core/resource_files/material_resource_file.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,14 +117,21 @@ struct SkyMaterialData
117117
void to_json(JSON::json& j, const SkyMaterialData& s);
118118
void from_json(const JSON::json& j, SkyMaterialData& s);
119119

120+
enum class TYPES_OF_BUFFERS
121+
{
122+
FLOATCB,
123+
FLOAT3CB,
124+
COLORCB
125+
};
126+
120127
struct CustomMaterialData
121128
{
122129
String vertexShaderPath;
123130
String pixelShaderPath;
124131
Vector<Ref<ImageResourceFile>> vertexShaderTextures;
125132
Vector<Ref<ImageResourceFile>> pixelShaderTextures;
126133
Vector<float> customConstantBuffers;
127-
Vector<float> typeOfCustomConstantBuffers;
134+
Vector<TYPES_OF_BUFFERS> typeOfCustomConstantBuffers;
128135
};
129136

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

0 commit comments

Comments
 (0)