Render State section

omar 2024-10-08 11:57:08 +02:00
parent 94a10d4df1
commit 410c61c89e

@ -12,6 +12,8 @@
- [Example for Vulkan users](#example-for-vulkan-users)
- [Example for WebGPU users](#example-for-webgpu-users)
- [About Texture Coordinates](#about-texture-coordinates)
- [Modifying Render State](#modifying-render-state)
## TL;DR;
@ -1140,4 +1142,49 @@ ImGui::Image((void*)(intptr_t)my_image_texture, ImVec2(my_image_width*0.5f, my_i
![Scaled](https://user-images.githubusercontent.com/8225057/79073856-25b40380-7ce9-11ea-91e4-754c3232fb58.png)
----
## Modifying Render State
You can use draw callbacks to manipulate the render state, e.g. alter sampling. Since 1.91.3, backends are now exposing some of their state during the render loop, which should make it easier to leverage callbacks. Refer to the .h file of each backend to see if a `ImGui_ImplXXXX_RenderState` struct is exposed.
<BR>Access them with e.g. `ImGui_ImplDX11_RenderState* state = (ImGui_ImplDX11_RenderState*)ImGui::GetPlatformIO().Renderer_RenderState;`.
```cpp
// For DX11 backend: Callback to modify current sampler
void ImDrawCallback_ImplDX11_SetSampler(const ImDrawList* parent_list, const ImDrawCmd* cmd)
{
ImGui_ImplDX11_RenderState* state = (ImGui_ImplDX11_RenderState*)ImGui::GetPlatformIO().Renderer_RenderState;
ID3D11SamplerState* sampler = cmd->UserCallbackData ? (ID3D11SamplerState*)cmd->UserCallbackData : state->SamplerDefault;
state->DeviceContext->PSSetSamplers(0, 1, &sampler);
}
```
```cpp
// For DX11 backend: Create a Point sampler
{
D3D11_SAMPLER_DESC desc;
ZeroMemory(&desc, sizeof(desc));
desc.Filter = D3D11_FILTER_MIN_MAG_MIP_POINT;
desc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
desc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
desc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
desc.MipLODBias = 0.f;
desc.ComparisonFunc = D3D11_COMPARISON_ALWAYS;
desc.MinLOD = 0.f;
desc.MaxLOD = 0.f;
g_pd3dDevice->CreateSamplerState(&desc, &g_SamplerPoint);
}
```
```cpp
// Usage
ImGui::GetWindowDrawList()->AddCallback(ImDrawCallback_ImplDX11_SetSampler, g_SamplerPoint); // Set custom sampler
ImGui::Image((void*)my_texture, ImVec2((float)my_image_width * 4, (float)my_image_height * 4));
ImGui::GetWindowDrawList()->AddCallback(ImDrawCallback_ImplDX11_SetSampler, NULL); // Restore sampler
```
This may be easier ot harder to use for some backends. Don't hesitate to open issues to discuss your needs and so we can improve the feature.
----
##### [Return to Index](#index)