5
5
#include " core/renderer/shaders/register_locations_vertex_shader.h"
6
6
#include " framework/systems/render_system.h"
7
7
#include " resource_loader.h"
8
+ #define MAX_NUMBER_OF_CUSTOM_CB 8
8
9
9
10
void to_json (JSON::json& j, const CustomMaterialData& s)
10
11
{
@@ -52,7 +53,7 @@ void from_json(const JSON::json& j, CustomMaterialData& s)
52
53
{
53
54
s.customConstantBuffers .push_back (customConstantBuffers);
54
55
}
55
- for (auto & typeOfCustomConstantBuffers : j.value (" typeOfCustomConstantBuffers" , Vector<float >()))
56
+ for (auto & typeOfCustomConstantBuffers : j.value (" typeOfCustomConstantBuffers" , Vector<TYPES_OF_BUFFERS >()))
56
57
{
57
58
s.typeOfCustomConstantBuffers .push_back (typeOfCustomConstantBuffers);
58
59
}
@@ -276,8 +277,8 @@ void CustomMaterialResourceFile::reimport()
276
277
typeOfCustomConstantBuffers = m_MaterialData.typeOfCustomConstantBuffers ;
277
278
278
279
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);
281
282
m_VSCB = RenderingDevice::GetSingleton ()->createBuffer <PerModelVSCBData>(PerModelVSCBData (), D3D11_BIND_CONSTANT_BUFFER, D3D11_USAGE_DYNAMIC, D3D11_CPU_ACCESS_WRITE);
282
283
}
283
284
@@ -288,46 +289,69 @@ bool CustomMaterialResourceFile::save()
288
289
289
290
float CustomMaterialResourceFile::getFloat (int index)
290
291
{
291
- return customConstantBuffers[4 * index];
292
+ if (4 * index < customConstantBuffers.size ())
293
+ return customConstantBuffers[4 * index];
294
+ return 0 .0f ;
292
295
}
293
296
294
- Vector< float > CustomMaterialResourceFile::getFloat3 (int index)
297
+ Vector3 CustomMaterialResourceFile::getFloat3 (int index)
295
298
{
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
+ }
300
306
return temp;
301
307
}
302
308
303
- Vector< float > CustomMaterialResourceFile::getColor (int index)
309
+ Color CustomMaterialResourceFile::getColor (int index)
304
310
{
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
+ }
310
319
return temp;
311
320
}
312
321
313
- void CustomMaterialResourceFile::setFloat (int index, float value)
322
+ bool CustomMaterialResourceFile::setFloat (int index, float value)
314
323
{
315
- customConstantBuffers[4 * index] = value;
324
+ if (4 * index < customConstantBuffers.size ())
325
+ {
326
+ customConstantBuffers[4 * index] = value;
327
+ return true ;
328
+ }
329
+ return false ;
316
330
}
317
331
318
- void CustomMaterialResourceFile::setFloat3 (int index, Vector3 value)
332
+ bool CustomMaterialResourceFile::setFloat3 (int index, Vector3 value)
319
333
{
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 ;
323
342
}
324
343
325
- void CustomMaterialResourceFile::setColor (int index, Color value)
344
+ bool CustomMaterialResourceFile::setColor (int index, Color value)
326
345
{
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 ;
331
355
}
332
356
333
357
void CustomMaterialResourceFile::draw ()
@@ -511,95 +535,59 @@ void CustomMaterialResourceFile::draw()
511
535
ImGui::TreePop ();
512
536
}
513
537
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 )
521
539
{
522
- if (typeOfCustomConstantBuffers[i / 4 ] == 1.0 )
540
+ String customConstantBufferName = " CB Slot " + std::to_string (i / 4 );
541
+ switch (typeOfCustomConstantBuffers[i / 4 ])
523
542
{
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 ;
548
552
}
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 ();
568
554
}
569
555
570
- if (ImGui::Button (ICON_ROOTEX_PLUS " Push float CB " ))
556
+ if (customConstantBuffers. size () < MAX_NUMBER_OF_CUSTOM_CB * sizeof ( float ))
571
557
{
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 ();
580
568
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 ();
591
579
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 ();
600
590
}
601
- ImGui::SameLine ();
602
-
603
591
if (ImGui::Button (ICON_ROOTEX_MINUS " Pop CB" ))
604
592
{
605
593
customConstantBuffers.pop_back ();
0 commit comments