Skip to content

Commit

Permalink
fixed compile errors, added ordered instance setting, fixed uses of I…
Browse files Browse the repository at this point in the history
…mGui::TextColored
  • Loading branch information
GrigoryGraborenko committed Sep 25, 2024
1 parent ed3157d commit 2ba2fff
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 26 deletions.
1 change: 1 addition & 0 deletions CMake/Jumbo.txt
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ if (NOT EMSCRIPTEN)
set_target_properties(${PROJECT_NAME} PROPERTIES LINK_FLAGS_DEBUG "/SUBSYSTEM:WINDOWS")
set_target_properties(${PROJECT_NAME} PROPERTIES LINK_FLAGS_RELEASE "/SUBSYSTEM:WINDOWS")
if (WEBGPU)
target_link_libraries(${PROJECT_NAME} dxguid.lib) # required for d3d webgpu backend
target_link_libraries(${PROJECT_NAME} "${CMAKE_SOURCE_DIR}/external/WebGPU/lib/$<CONFIG>/*")
endif()
elseif (APPLE AND WEBGPU)
Expand Down
3 changes: 3 additions & 0 deletions Examples/Empty_WebGPU_Jumbo/src/PreCompiledHeader.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
#endif

#include <SDL.h>
#ifndef __EMSCRIPTEN__
#include <SDL_syswm.h>
#endif
#include <SDL_image.h>
#include <SDL_mixer.h>
#include <Metastuff/Meta.h>
Expand Down
3 changes: 3 additions & 0 deletions Examples/Empty_WebGPU_QT_Jumbo/src/PreCompiledHeader.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
#endif

#include <SDL.h>
#ifndef __EMSCRIPTEN__
#include <SDL_syswm.h>
#endif
#include <SDL_image.h>
#include <SDL_mixer.h>
#include <Metastuff/Meta.h>
Expand Down
32 changes: 13 additions & 19 deletions Src/EditorViewers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -354,12 +354,12 @@ void InfoViewer::IRenderImGui(InterfaceInfoViewer& data) {
for (const auto& segment : m_LoopHistogram) {
ImGui::TableSetColumnIndex(col++);
if (segment.second) {
ImGui::TextColored(segment.first >= 0.04 ? (segment.first >= 0.2 ? ImVec4(1.0f, 0.3f, 0.3f, 1.0f) : ImVec4(1.0f, 0.6f, 0.3f, 1.0f)) : ImVec4(0.6f, 1.0f, 0.6f, 1.0f), "%i", segment.second);
ImGuiTextColoredUnformatted(std::format("{}", segment.second), segment.first >= 0.04 ? (segment.first >= 0.2 ? ImVec4(1.0f, 0.3f, 0.3f, 1.0f) : ImVec4(1.0f, 0.6f, 0.3f, 1.0f)) : ImVec4(0.6f, 1.0f, 0.6f, 1.0f));
}
}
ImGui::TableSetColumnIndex(col++);
if (m_LoopHistogramOverflow) {
ImGui::TextColored(ImVec4(1.0f, 0.3f, 0.3f, 1.0f), "%i", m_LoopHistogramOverflow);
ImGuiTextColoredUnformatted(std::format("{}", m_LoopHistogramOverflow), ImVec4(1.0f, 0.3f, 0.3f, 1.0f));
}
ImGui::TableSetColumnIndex(col++);
if (ImGui::Button("Clear")) {
Expand Down Expand Up @@ -441,7 +441,7 @@ void LogViewer::IRenderImGui(InterfaceLogViewer& data) {
//ImGui::Text(time_str.data());
ImGui::Text(log.p_TimeStr.c_str());
ImGui::TableSetColumnIndex(1);
ImGui::TextColored(log.p_Color, log.p_Message.c_str());
ImGuiTextColoredUnformatted(log.p_Message, log.p_Color);
}

if (data.p_AutoScroll) {
Expand Down Expand Up @@ -891,11 +891,11 @@ InterfaceCollapsible* ShaderViewer::RenderShader(InterfaceShaderViewer& data, st
}

if (!src.m_Error.empty()) {
ImGui::TextColored(ImVec4(1.0f, 0.0f, 0.0f, 1.0f), src.m_Error.data());
ImGuiTextColoredUnformatted(src.m_Error, ImVec4(1.0f, 0.0f, 0.0f, 1.0f));
}
if (search.empty()) {
for (int line = 0; line < lines.size(); line++) {
ImGui::TextColored(ImVec4(0.4f, 0.4f, 0.4f, 1.0f), std::format("{}", line).c_str());
ImGuiTextColoredUnformatted(std::format("{}", line), ImVec4(0.4f, 0.4f, 0.4f, 1.0f));
ImGui::SameLine();
ImGui::SetCursorPosX(number_width);
ImGui::TextUnformatted(lines[line].c_str());
Expand Down Expand Up @@ -929,16 +929,12 @@ InterfaceCollapsible* ShaderViewer::RenderShader(InterfaceShaderViewer& data, st
ImGui::Separator();
}
auto line_num_str = std::format("{}", line);
ImGui::TextColored(ImVec4(0.4f, 0.4f, 0.4f, 1.0f), line_num_str.c_str());
ImGuiTextColoredUnformatted(line_num_str, ImVec4(0.4f, 0.4f, 0.4f, 1.0f));
ImGui::SameLine();
ImGui::SetCursorPosX(number_width);

ImGui::PushStyleColor(ImGuiCol_Text, found ? ImVec4(1.0f, 0.5f, 0.5f, 1.0f) : ImVec4(1.0f, 1.0f, 1.0f, 1.0f));
ImGui::TextUnformatted(lines[line].c_str());
ImGui::PopStyleColor();
ImGuiTextColoredUnformatted(lines[line], found ? ImVec4(1.0f, 0.5f, 0.5f, 1.0f) : ImVec4(1.0f, 1.0f, 1.0f, 1.0f));

last_line = line;

match_count -= found ? 1 : 0;
}
}
Expand All @@ -960,7 +956,7 @@ InterfaceCollapsible* ShaderViewer::RenderShader(InterfaceShaderViewer& data, st
const ImVec4 line_num_col(0.4f, 0.4f, 0.4f, 1.0f);
const ImVec4 error_col(1.0f, 0.0f, 0.0f, 1.0f);
for (const auto& err : errors) {
ImGui::TextColored(error_col, err.m_Message.data());
ImGuiTextColoredUnformatted(err.m_Message, error_col);
}
if (search.empty()) {
for (int line = 0; line < lines.size(); line++) {
Expand All @@ -970,7 +966,7 @@ InterfaceCollapsible* ShaderViewer::RenderShader(InterfaceShaderViewer& data, st
if (err.m_LineNum == line) {
err_found = true;
std::string line_str = std::format("{}", line);
ImGui::TextColored(error_col, line_str.data());
ImGuiTextColoredUnformatted(line_str, error_col);
ImGui::SameLine();
ImGui::SetCursorPosX(number_width);

Expand All @@ -979,16 +975,14 @@ InterfaceCollapsible* ShaderViewer::RenderShader(InterfaceShaderViewer& data, st

ImGui::TextUnformatted(before_error.data());
ImGui::SameLine(0.0, 0.0);
ImGui::PushStyleColor(ImGuiCol_Text, error_col);
ImGui::TextUnformatted(after_error.data());
ImGui::PopStyleColor();
ImGuiTextColoredUnformatted(after_error, error_col);
break;
}
}
}
if (!err_found) {
std::string line_str = std::format("{}", line);
ImGui::TextColored(line_num_col, line_str.data());
ImGuiTextColoredUnformatted(line_str, line_num_col);
ImGui::SameLine();
ImGui::SetCursorPosX(number_width);
ImGui::TextUnformatted(lines[line].data());
Expand Down Expand Up @@ -1023,7 +1017,7 @@ InterfaceCollapsible* ShaderViewer::RenderShader(InterfaceShaderViewer& data, st
ImGui::Separator();
}
std::string line_num_str = std::format("{}", line);
ImGui::TextColored(line_num_col, line_num_str.data());
ImGuiTextColoredUnformatted(line_num_str, line_num_col);
ImGui::SameLine();
ImGui::SetCursorPosX(number_width);

Expand Down Expand Up @@ -1251,7 +1245,7 @@ void Scrapbook2D::IRenderImGui(InterfaceScrapbook2D& data) {
for (auto& text : m_Texts) {
auto screen_pos = data.p_Cam.WorldToScreen(text.p_Pos, m_Width, m_Height);
ImGui::SetCursorPos(ImVec2(screen_pos.x + im_pos.x, screen_pos.y + im_pos.y));
ImGui::TextColored(ImVec4(text.p_Col.x, text.p_Col.y, text.p_Col.z, text.p_Col.w), text.p_Text.data());
ImGuiTextColoredUnformatted(text.p_Text, ImVec4(text.p_Col.x, text.p_Col.y, text.p_Col.z, text.p_Col.w));
}
m_Texts.clear();

Expand Down
7 changes: 7 additions & 0 deletions Src/NeshnyUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,11 @@ std::string SrcStr(const std::source_location location) {
return std::format("{}:{}", location.file_name(), location.line());
}

////////////////////////////////////////////////////////////////////////////////
void ImGuiTextColoredUnformatted(std::string str, ImVec4 text_col) {
ImGui::PushStyleColor(ImGuiCol_Text, text_col);
ImGui::TextUnformatted(str.c_str());
ImGui::PopStyleColor();
}

} // namespace Neshny
5 changes: 2 additions & 3 deletions Src/NeshnyUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,14 @@ double Random(double min_val, double max_val);
int RandomInt(int min_val, int max_val);
int RoundUpPowerTwo(int value);
size_t HashMemory(unsigned char* mem, int size);
uint64_t TimeSinceEpochMilliseconds();
bool NearlyEqual(double a, double b);
std::string ReplaceAll(std::string_view str, std::string_view before, std::string_view after);
std::string JoinStrings(const std::vector<std::string>& list, std::string_view insert_between);
std::string JoinStrings(const std::list<std::string>& list, std::string_view insert_between);
bool StringContains(std::string_view str, std::string_view search, bool case_insensitive = false);
std::string SrcStr(const std::source_location location = std::source_location::current());
void ImGuiTextColoredUnformatted(std::string str, ImVec4 text_col);

////////////////////////////////////////////////////////////////////////////////
template <class T>
Expand Down Expand Up @@ -105,9 +107,6 @@ T RadiansDiff(T rad_a, T rad_b) {
return diff;
}

////////////////////////////////////////////////////////////////////////////////
uint64_t TimeSinceEpochMilliseconds();

////////////////////////////////////////////////////////////////////////////////
//
////////////////////////////////////////////////////////////////////////////////
Expand Down
4 changes: 2 additions & 2 deletions Src/Testing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ void UnitTester::IRender(void) {
int index = 0;
for (auto& result : m_Results) {
ImGui::PushID(result.p_Label.c_str());
ImGui::TextColored(result.p_Success ? ImVec4(0.5f, 1.0f, 0.5f, 1.0f) : ImVec4(1.0f, 0.0f, 0.0f, 1.0f), "%s", result.p_Label.c_str());
Neshny::ImGuiTextColoredUnformatted(result.p_Label, result.p_Success ? ImVec4(0.5f, 1.0f, 0.5f, 1.0f) : ImVec4(1.0f, 0.0f, 0.0f, 1.0f));
ImGui::SameLine();
if (ImGui::Button("Rerun")) {
result = ExecuteTest(g_UnitTests[index]);
}
if (!result.p_Success) {
ImGui::Indent(20);
ImGui::TextColored(ImVec4(1.0f, 0.5f, 0.5f, 1.0f), "%s", result.p_Error.c_str());
Neshny::ImGuiTextColoredUnformatted(result.p_Error, ImVec4(1.0f, 0.5f, 0.5f, 1.0f));
ImGui::Unindent(20);
}
ImGui::PopID();
Expand Down
22 changes: 22 additions & 0 deletions Src/WebGPU/EntityWebGPU.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,28 @@ void GPUEntity::AddInstancesInternal(unsigned char* data, int item_count, int it
m_LastKnownInfo.p_Count += item_count;
}

////////////////////////////////////////////////////////////////////////////////
void GPUEntity::SetInstancesInternal(unsigned char* data, int item_count, int item_size) {

if (!m_SSBO) {
return;
}
m_LastKnownInfo.p_Count = item_count;
m_LastKnownInfo.p_MaxIndex = item_count;
m_LastKnownInfo.p_NextId = item_count;
m_LastKnownInfo.p_FreeCount = 0;

#pragma msg("might be good to auto-assign ids here, otherwise user is responsible for setting it from 0...N - would need offset though")

int data_size = item_count * item_size;
int total_size = sizeof(EntityInfo) + data_size;
std::vector<std::byte> buffer(total_size, std::byte(0));
memcpy(buffer.data(), &m_LastKnownInfo, sizeof(EntityInfo));
memcpy(buffer.data() + sizeof(EntityInfo), data, data_size);

m_SSBO->Write((unsigned char*)buffer.data(), 0, buffer.size());
}

////////////////////////////////////////////////////////////////////////////////
void GPUEntity::DeleteInstance(int index) {

Expand Down
8 changes: 7 additions & 1 deletion Src/WebGPU/EntityWebGPU.h
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ class GPUEntity {
~GPUEntity(void) { Destroy(); }

template <typename T> void ExtractMultiple(std::vector<T>& items, int count) {
// does this still make sense? should there be an offset?
// TODO: does this still make sense? should there be an offset?
if (count <= 0) {
return;
}
Expand Down Expand Up @@ -238,7 +238,12 @@ class GPUEntity {
bool Init ( int expected_max_count = 100000 );
void Clear ( void );

// will add instances in random order, no need to set IDs, they will be overwritten
template <typename T> void AddInstances ( std::vector<T>& items ) { AddInstancesInternal((unsigned char*)items.data(), items.size(), sizeof(T) ); }

// this version preserves the ordering, but you must set the IDs correctly yourself, and will delete all prior instances
template <typename T> void SetInstances ( std::vector<T>& items ) { SetInstancesInternal((unsigned char*)items.data(), items.size(), sizeof(T) ); }

void DeleteInstance ( int index );

std::shared_ptr<unsigned char[]> MakeCopySync ( void );
Expand Down Expand Up @@ -266,6 +271,7 @@ class GPUEntity {
protected:

void AddInstancesInternal ( unsigned char* data, int item_count, int item_size );
void SetInstancesInternal ( unsigned char* data, int item_count, int item_size );
void MakeCopyIn ( unsigned char* ptr, int offset, int size );
void Destroy ( void );

Expand Down
2 changes: 1 addition & 1 deletion Src/WebGPU/WGPUUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace Neshny {

// TODO: create global state for device, etc rather than using core
// TODO: create global state for device, etc rather than using core, to make core optional
WGPUDevice GlobalWebGPUDevice();

////////////////////////////////////////////////////////////////////////////////
Expand Down

0 comments on commit 2ba2fff

Please sign in to comment.