Skip to content

Commit

Permalink
resolve #8
Browse files Browse the repository at this point in the history
Auto upload font without needing user to upload the texture
Sending texture now reuse ImTextureId, matching Dear ImGui
Undid some changes to Dear Imgui 'imgui_implementation_dx11.cpp' file, to be closer to original. Still some differences at file top.
  • Loading branch information
fatnasam committed Oct 11, 2020
1 parent e7b86fa commit 12305a3
Show file tree
Hide file tree
Showing 13 changed files with 175 additions and 195 deletions.
11 changes: 4 additions & 7 deletions Code/Client/NetImgui_Api.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,11 @@
//=================================================================================================
//! @author : Sammy Fatnassi
//! @date : 2020/10/11
//! @version : v1.2.4
//! @version : v1.2.5
//! @Details : For integration info : https://github.com/sammyfreg/netImgui/wiki
//=================================================================================================
#define NETIMGUI_VERSION "1.2.4"
#define NETIMGUI_VERSION_NUM 10204

struct ImGuiContext;
struct ImDrawData;
#define NETIMGUI_VERSION "1.2.5"
#define NETIMGUI_VERSION_NUM 10205

#include <stdint.h>

Expand Down Expand Up @@ -113,7 +110,7 @@ bool IsDrawingRemote(void);
// Send an updated texture used by imgui, to netImguiApp server
// Note: To remove a texture, set pData to nullptr
//=================================================================================================
void SendDataTexture(uint64_t textureId, void* pData, uint16_t width, uint16_t height, eTexFormat format);
void SendDataTexture(ImTextureID textureId, void* pData, uint16_t width, uint16_t height, eTexFormat format);

//=================================================================================================
// Start a new Imgui Frame and wait for Draws commands, using a ImGui created internally
Expand Down
251 changes: 135 additions & 116 deletions Code/Client/Private/NetImgui_Api.cpp

Large diffs are not rendered by default.

9 changes: 6 additions & 3 deletions Code/Client/Private/NetImgui_Client.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,12 @@ struct ClientInfo
ExchangePtr<CmdDrawFrame> mPendingFrameOut;
ExchangePtr<CmdInput> mPendingInputIn;
BufferKeys mPendingKeyIn;
ImGuiContext* mpContextClone = nullptr; // Default ImGui drawing context copy, used to do our internal remote drawing
ImGuiContext* mpContextEmpty = nullptr; // Placeholder ImGui drawing context, when we are not waiting for a new drawing frame but still want a valid context in place
ImGuiContext* mpContextClone = nullptr; // Default ImGui drawing context copy, used to do our internal remote drawing (client obj has ownership)
ImGuiContext* mpContextEmpty = nullptr; // Placeholder ImGui drawing context, when we are not waiting for a new drawing frame but still want a valid context in place (client obj has ownership)
ImGuiContext* mpContextRestore = nullptr; // Context to restore to Imgui once drawing is done
ImGuiContext* mpContextDrawing = nullptr; // Current context used for drawing (between a BeginFrame/EndFrame)
ImGuiContext* mpContext = nullptr; // Context that the remote drawing should use (either the one active when connection request happened, or a clone)
const void* mpFontTextureData = nullptr; // Last font texture data send to server (used to detect if font was changed)
Time mTimeTracking;
std::atomic_int32_t mTexturesPendingCount;
float mMouseWheelVertPrev = 0.f;
Expand All @@ -59,7 +61,8 @@ struct ClientInfo
bool mbHasTextureUpdate = false;
bool mbIsRemoteDrawing = false; // True if the rendering it meant for the remote netImgui server
bool mbRestorePending = false; // Original context has had some settings overridden, original values stored in mRestoreXXX
//char PADDING[5];
bool mbFontUploaded = false; // Auto detect if font was sent to server
char PADDING[7];
void TextureProcessPending();
void TextureProcessRemoval();
inline bool IsConnected()const;
Expand Down
10 changes: 10 additions & 0 deletions Code/Client/Private/NetImgui_Shared.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@ template <typename TType> TType* netImguiSizedNew(size_t placementSize);
template <typename TType> void netImguiDelete(TType* pData);
template <typename TType> void netImguiDeleteSafe(TType*& pData);

class ScopedImguiContext
{
public:
ScopedImguiContext(ImGuiContext* pNewContext) : mpSavedContext(ImGui::GetCurrentContext()){ ImGui::SetCurrentContext(pNewContext); }
~ScopedImguiContext() { ImGui::SetCurrentContext(mpSavedContext); }

protected:
ImGuiContext* mpSavedContext;
};

//=============================================================================
// Class to exchange a pointer between two threads, safely
//=============================================================================
Expand Down
7 changes: 3 additions & 4 deletions Code/Sample/Common/Sample.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ namespace SampleClient
//-----------------------------------------------------------------------------
// Methods implemented in each samples
//-----------------------------------------------------------------------------
bool Client_Startup();
void Client_Shutdown();
void Client_AddFontTexture(uint64_t texId, void* pData, uint16_t width, uint16_t height);
const ImDrawData* Client_Draw();
bool Client_Startup();
void Client_Shutdown();
ImDrawData* Client_Draw();

//-----------------------------------------------------------------------------
// Utility methods available in Samples
Expand Down
9 changes: 4 additions & 5 deletions Code/Sample/Common/imgui_impl_dx11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ struct VERTEX_CONSTANT_BUFFER
float mvp[4][4];
};

static void ImGui_ImplDX11_SetupRenderState(const ImDrawData* draw_data, ID3D11DeviceContext* ctx)
static void ImGui_ImplDX11_SetupRenderState(ImDrawData* draw_data, ID3D11DeviceContext* ctx)
{
// Setup viewport
D3D11_VIEWPORT vp;
Expand Down Expand Up @@ -97,7 +97,7 @@ static void ImGui_ImplDX11_SetupRenderState(const ImDrawData* draw_data, ID3D11D

// Render function
// (this used to be set in io.RenderDrawListsFn and called by ImGui::Render(), but you can now call this directly from your main loop)
void ImGui_ImplDX11_RenderDrawData(const ImDrawData* draw_data)
void ImGui_ImplDX11_RenderDrawData(ImDrawData* draw_data)
{
// Avoid rendering when minimized
if (draw_data->DisplaySize.x <= 0.0f || draw_data->DisplaySize.y <= 0.0f)
Expand Down Expand Up @@ -286,7 +286,7 @@ static void ImGui_ImplDX11_CreateFontsTexture()
unsigned char* pixels;
int width, height;
io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height);

// Upload texture to graphics system
{
D3D11_TEXTURE2D_DESC desc;
Expand All @@ -301,7 +301,7 @@ static void ImGui_ImplDX11_CreateFontsTexture()
desc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
desc.CPUAccessFlags = 0;

ID3D11Texture2D *pTexture = NULL;
ID3D11Texture2D* pTexture = NULL;
D3D11_SUBRESOURCE_DATA subResource;
subResource.pSysMem = pixels;
subResource.SysMemPitch = desc.Width * 4;
Expand All @@ -321,7 +321,6 @@ static void ImGui_ImplDX11_CreateFontsTexture()

// Store our identifier
io.Fonts->TexID = (ImTextureID)g_pFontTextureView;
SampleClient::Client_AddFontTexture((uint64_t)g_pFontTextureView, pixels, (uint16_t)width, (uint16_t)height);

// Create texture sampler
{
Expand Down
2 changes: 1 addition & 1 deletion Code/Sample/Common/imgui_impl_dx11.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ struct ID3D11DeviceContext;
IMGUI_IMPL_API bool ImGui_ImplDX11_Init(ID3D11Device* device, ID3D11DeviceContext* device_context);
IMGUI_IMPL_API void ImGui_ImplDX11_Shutdown();
IMGUI_IMPL_API void ImGui_ImplDX11_NewFrame();
IMGUI_IMPL_API void ImGui_ImplDX11_RenderDrawData(const ImDrawData* draw_data);
IMGUI_IMPL_API void ImGui_ImplDX11_RenderDrawData(ImDrawData* draw_data);

// Use if you want to reset your rendering device without losing Dear ImGui state.
IMGUI_IMPL_API void ImGui_ImplDX11_InvalidateDeviceObjects();
Expand Down
2 changes: 1 addition & 1 deletion Code/Sample/Common/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int n
//=============================================================================
// EDIT TO ORIGINAL IMGUI main.cpp
// Draw the Local Imgui UI and remote imgui UI
const ImDrawData* pDraw = SampleClient::Client_Draw();
ImDrawData* pDraw = SampleClient::Client_Draw();
//=============================================================================
g_pd3dDeviceContext->ClearRenderTargetView(g_mainRenderTargetView, (float*)&clear_col);
if( pDraw )
Expand Down
11 changes: 1 addition & 10 deletions Code/Sample/SampleBasic/SampleBasic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,10 @@ void Client_Shutdown()
NetImgui::Shutdown(true);
}

//=================================================================================================
// Added a call to this function in 'ImGui_ImplDX11_CreateFontsTexture()', allowing us to
// forward the Font Texture information to netImgui.
//=================================================================================================
void Client_AddFontTexture(uint64_t texId, void* pData, uint16_t width, uint16_t height)
{
NetImgui::SendDataTexture(texId, pData, width, height, NetImgui::eTexFormat::kTexFmtRGBA8 );
}

//=================================================================================================
// Function used by the sample, to draw all ImGui Content
//=================================================================================================
const ImDrawData* Client_Draw()
ImDrawData* Client_Draw()
{
//---------------------------------------------------------------------------------------------
// (1) Start a new Frame
Expand Down
11 changes: 1 addition & 10 deletions Code/Sample/SampleDisabled/SampleDisabled.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,10 @@ void Client_Shutdown()
NetImgui::Shutdown(true);
}

//=================================================================================================
// Added a call to this function in 'ImGui_ImplDX11_CreateFontsTexture()', allowing us to
// forward the Font Texture information to netImgui.
//=================================================================================================
void Client_AddFontTexture(uint64_t texId, void* pData, uint16_t width, uint16_t height)
{
NetImgui::SendDataTexture(texId, pData, width, height, NetImgui::eTexFormat::kTexFmtRGBA8 );
}

//=================================================================================================
// Function used by the sample, to draw all ImGui Content
//=================================================================================================
const ImDrawData* Client_Draw()
ImDrawData* Client_Draw()
{
//---------------------------------------------------------------------------------------------
// (1) Start a new Frame
Expand Down
11 changes: 1 addition & 10 deletions Code/Sample/SampleDualUI/SampleDualUI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,6 @@ void Client_Shutdown()
NetImgui::Shutdown(true);
}

//=================================================================================================
// Added a call to this function in 'ImGui_ImplDX11_CreateFontsTexture()', allowing us to
// forward the Font Texture information to netImgui.
//=================================================================================================
void Client_AddFontTexture(uint64_t texId, void* pData, uint16_t width, uint16_t height)
{
NetImgui::SendDataTexture(texId, pData, width, height, NetImgui::eTexFormat::kTexFmtRGBA8);
}

//=================================================================================================
//
//=================================================================================================
Expand Down Expand Up @@ -101,7 +92,7 @@ void Client_Draw_SharedContent()
//=================================================================================================
// Function used by the sample, to draw all ImGui Content
//=================================================================================================
const ImDrawData* Client_Draw()
ImDrawData* Client_Draw()
{
//---------------------------------------------------------------------------------------------
// (1) Start a new Frame
Expand Down
15 changes: 3 additions & 12 deletions Code/Sample/SampleNewFrame/SampleNewFrame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,6 @@ void Client_Shutdown()
NetImgui::Shutdown(true);
}

//=================================================================================================
// Added a call to this function in 'ImGui_ImplDX11_CreateFontsTexture()', allowing us to
// forward the Font Texture information to netImgui.
//=================================================================================================
void Client_AddFontTexture(uint64_t texId, void* pData, uint16_t width, uint16_t height)
{
NetImgui::SendDataTexture(texId, pData, width, height, NetImgui::eTexFormat::kTexFmtRGBA8);
}

//=================================================================================================
// Imgui content
//=================================================================================================
Expand Down Expand Up @@ -94,7 +85,7 @@ void Client_Draw_Content()
// Without Frame skip support. User redraw content every frame, even when netImgui doesn't
// require a new drawframe
//=================================================================================================
const ImDrawData* Client_Draw_ModeAlways()
ImDrawData* Client_Draw_ModeAlways()
{
//---------------------------------------------------------------------------------------------
// (1) Start a new Frame
Expand Down Expand Up @@ -127,7 +118,7 @@ const ImDrawData* Client_Draw_ModeAlways()
// This code path handle not rendering a frame when not needed. netImgui doesn't require
// redrawing every frame.
//=============================================================================================
const ImDrawData* Client_Draw_ModeOnDemand()
ImDrawData* Client_Draw_ModeOnDemand()
{
//---------------------------------------------------------------------------------------------
// (1) Start a new Frame
Expand Down Expand Up @@ -165,7 +156,7 @@ const ImDrawData* Client_Draw_ModeOnDemand()
//=================================================================================================
// Function used by the sample, to draw all ImGui Content
//=================================================================================================
const ImDrawData* Client_Draw()
ImDrawData* Client_Draw()
{
switch( sDrawUpdateMode )
{
Expand Down
21 changes: 5 additions & 16 deletions Code/Sample/SampleTextures/SampleTextures.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// SAMPLE TEXTURES
//-------------------------------------------------------------------------------------------------
// Example of using various textures in the ImGui context, after making netImgui aware of them.
// We need to always provide the Font texture (this sample calls Client_AddFontTexture to do that)
// but it also apply to any other textures we might want to display.
//=================================================================================================

#include <NetImgui_Api.h>
Expand Down Expand Up @@ -53,7 +51,7 @@ void CustomTextureCreate(NetImgui::eTexFormat eTexFmt)
case NetImgui::eTexFormat::kTexFmt_Invalid: assert(0); break;
}
TextureCreate(pixelData, Width, Height, gCustomTextureView[static_cast<int>(eTexFmt)]);
NetImgui::SendDataTexture(reinterpret_cast<uint64_t>(gCustomTextureView[static_cast<int>(eTexFmt)]), pixelData, Width, Height, eTexFmt);
NetImgui::SendDataTexture(static_cast<ImTextureID>(gCustomTextureView[static_cast<int>(eTexFmt)]), pixelData, Width, Height, eTexFmt);
}

//=================================================================================================
Expand All @@ -63,7 +61,7 @@ void CustomTextureDestroy(NetImgui::eTexFormat eTexFmt)
{
if (gCustomTextureView[static_cast<int>(eTexFmt)])
{
NetImgui::SendDataTexture(reinterpret_cast<uint64_t>(gCustomTextureView[static_cast<int>(eTexFmt)]), nullptr, 0, 0, NetImgui::eTexFormat::kTexFmt_Invalid);
NetImgui::SendDataTexture(static_cast<ImTextureID>(gCustomTextureView[static_cast<int>(eTexFmt)]), nullptr, 0, 0, NetImgui::eTexFormat::kTexFmt_Invalid);
TextureDestroy(gCustomTextureView[static_cast<int>(eTexFmt)]);
}
}
Expand All @@ -80,7 +78,7 @@ bool Client_Startup()
pixelData.fill(0);

TextureCreate(pixelData.data(), 8, 8, gDefaultEmptyTexture);
NetImgui::SendDataTexture(reinterpret_cast<uint64_t>(gDefaultEmptyTexture), pixelData.data(), 8, 8, NetImgui::eTexFormat::kTexFmtRGBA8);
NetImgui::SendDataTexture(static_cast<ImTextureID>(gDefaultEmptyTexture), pixelData.data(), 8, 8, NetImgui::eTexFormat::kTexFmtRGBA8);

// Can have more ImGui initialization here, like loading extra fonts.
// ...
Expand All @@ -93,24 +91,15 @@ bool Client_Startup()
//=================================================================================================
void Client_Shutdown()
{
NetImgui::SendDataTexture(reinterpret_cast<uint64_t>(gDefaultEmptyTexture), nullptr, 0, 0, NetImgui::eTexFormat::kTexFmt_Invalid);
NetImgui::SendDataTexture(static_cast<ImTextureID>(gDefaultEmptyTexture), nullptr, 0, 0, NetImgui::eTexFormat::kTexFmt_Invalid);
TextureDestroy(gDefaultEmptyTexture);
NetImgui::Shutdown(true);
}

//=================================================================================================
// Added a call to this function in 'ImGui_ImplDX11_CreateFontsTexture()', allowing us to
// forward the Font Texture information to netImgui.
//=================================================================================================
void Client_AddFontTexture(uint64_t texId, void* pData, uint16_t width, uint16_t height)
{
NetImgui::SendDataTexture(texId, pData, width, height, NetImgui::eTexFormat::kTexFmtRGBA8);
}

//=================================================================================================
// Function used by the sample, to draw all ImGui Content
//=================================================================================================
const ImDrawData* Client_Draw()
ImDrawData* Client_Draw()
{
bool bCanDisplayLocally(false);

Expand Down

0 comments on commit 12305a3

Please sign in to comment.