Closed soufianekhiat closed 5 months ago
This code is not working, and produce a gpu hang.
I'm not sure how we can help, it's seems likely a problem with your use of DX11.
It will be a simpler (no brainer) to have a trivial pixel shader with 4 vertices instead.
It doesn't seem simpler to me IMHO because you are going to need lots of shader variants. But maybe you can find a way to let dear imgui generate the mesh and only act on the shader.
I'm not sure how we can help, it's seems likely a problem with your use of DX11.
If it's dx11 problem I can work on it. I never saw a proper usage of draw callback I'm not sure of the proper use.
It doesn't seem simpler to me IMHO because you are going to need lots of shader variants. But maybe you can find a way to let dear imgui generate the mesh and only act on the shader.
True! More shader variant, but traded for more flexibility, for a simple linear gradient I need lot of vertices (based on where the gradient start end). What do you mean by the last sentence? Current I had to tesselate the geometry https://github.com/soufianekhiat/DearWidgets/blob/rework/src/api/dear_widgets.cpp#L2078 From a simple geometry: https://github.com/soufianekhiat/DearWidgets/blob/rework/src/api/dear_widgets.cpp#L2355 To have a sense of a simple linear gradient: https://github.com/soufianekhiat/DearWidgets/blob/rework/src/api/dear_widgets.cpp#L2439
Here an illustration of the problem:
You should probably use RenderDoc or another gpu debugger to understand why your code is crashing.
What do you mean by the last sentence?
DrawQuadWithCustomShader output a simple quad which can already be output by ImDrawList, so maybe you can simply your callback logic to set shader + some uniforms for your shader, and not attempt to create/bind buffers.
AddCallback(bind your shader and data) AddRectFilled() AddCallback(ImDrawCallback_ResetRenderState)
[Fixed]
ImGui::Begin( "Custom Shader" );
ImDrawList* draw = ImGui::GetWindowDrawList();
ImVec2 cur = ImGui::GetCursorScreenPos();
draw->AddCallback( &DrawCustomShaderQuad, &shader );
ImRect bb( cur, cur + ImGui::GetContentRegionAvail() );
draw->AddImageQuad( img, bb.GetBL(), bb.GetBR(), bb.GetTR(), bb.GetTL(), ImVec2( 0, 0 ), ImVec2( 1, 0 ), ImVec2( 1, 1 ), ImVec2( 0, 1 ), IM_COL32_WHITE );
draw->AddCallback( ImDrawCallback_ResetRenderState, NULL );
ImGui::End();
Your second recommendation is much simple, it simplify the memory management etc. And rely on unique Vertex Buffer.
void DrawCustomShaderQuad( const ImDrawList* parent_list, const ImDrawCmd* cmd )
{
ImPlatform::ImShader* shaders = ( ImPlatform::ImShader* )cmd->UserCallbackData;
ImGui_ImplDX11_Data* bd = ImGui_ImplDX11_GetBackendData();
ID3D11DeviceContext* ctx = bd->pd3dDeviceContext;
ctx->PSSetShader( ( ID3D11PixelShader* )( shaders->ps ), nullptr, 0 );
}
I guess you’ll need to pass some extra data to your shader but that’s the general idea yes (as long as the vertex format used by ImDrawList is sufficient for you).
Yes indeed It need extra for to add more data etc. https://github.com/soufianekhiat/ImPlatform/blob/main/ImPlatform/ImPlatform.cpp#L1173 If I need another Vertex Format I'll need to create my own data struct for draw call, and by pass the ImDrawList pass. Interpretation of that available on ImPlatform: https://github.com/soufianekhiat/ImPlatform/blob/main/ImPlatformDemo/main.cpp#L106 https://github.com/soufianekhiat/ImPlatform/blob/main/ImPlatformDemo/main.cpp#L201 https://github.com/soufianekhiat/ImPlatform/blob/main/ImPlatformDemo/main.cpp#L201 Hidden under 'IM_SUPPORT_CUSTOM_SHADER' while I don't support "enough" backend.
Sidenote to have it portable for GLSL and GLSL I prefix all shaders with: https://github.com/soufianekhiat/ImPlatform/blob/main/ImPlatform/ImPlatform.cpp#L1016
Good to hear. Do you need anything else on my end or can we close this?
Version/Branch of Dear ImGui:
master
Back-ends:
imgui_impl_dx11.cpp + imgui_impl_win32.cpp
Compiler, OS:
MSVC 2022
Full config/build information:
No response
Details:
Attempt to render a quad with a custom shader. I wasn't able to see how to hook my code with DrawCall back and the reset of RenderStates. Here an example I tried with win32/dx11 backend. This code is not working, and produce a gpu hang.
XY Problem: Various rendering function I had on DearWidgets rely on vertex shader interpolation, so for a given gradient I need lot of vertices. It will be a simpler (no brainer) to have a trivial pixel shader with 4 vertices instead.
Screenshots/Video:
No response
Minimal, Complete and Verifiable Example code: