1
0
mirror of https://github.com/ocornut/imgui.git synced 2024-11-28 01:20:55 +01:00

Examples: DX11: Cleanup state backup/restore code (#570)

This commit is contained in:
ocornut 2016-04-03 12:43:17 +02:00
parent d4d51a7802
commit 2942240072

View File

@ -116,45 +116,45 @@ void ImGui_ImplDX11_RenderDrawLists(ImDrawData* draw_data)
g_pd3dDeviceContext->Unmap(g_pVertexConstantBuffer, 0); g_pd3dDeviceContext->Unmap(g_pVertexConstantBuffer, 0);
} }
// Capture all the state that will be modified to restore it afterwards // Backup DX state that will be modified to restore it afterwards (unfortunately this is very ugly looking and verbose. Close your eyes!)
UINT oldNumScissorRects = D3D11_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE; ID3D11DeviceContext* ctx = g_pd3dDeviceContext;
D3D11_RECT oldScissorRects[D3D11_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE]; struct BACKUP_DX11_STATE
g_pd3dDeviceContext->RSGetScissorRects(&oldNumScissorRects, oldScissorRects); {
ID3D11ShaderResourceView* pOldPSSRV0; UINT ScissorRectsCount, ViewportsCount;
g_pd3dDeviceContext->PSGetShaderResources(0, 1, &pOldPSSRV0); D3D11_RECT ScissorRects[D3D11_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE];
ID3D11RasterizerState* pOldRS; D3D11_VIEWPORT Viewports[D3D11_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE];
g_pd3dDeviceContext->RSGetState(&pOldRS); ID3D11RasterizerState* RS;
ID3D11BlendState* pOldBlendState; ID3D11BlendState* BlendState;
FLOAT oldBlendFactor[4]; FLOAT BlendFactor[4];
UINT oldSampleMask; UINT SampleMask;
g_pd3dDeviceContext->OMGetBlendState(&pOldBlendState, oldBlendFactor, &oldSampleMask); ID3D11ShaderResourceView* PSShaderResource;
ID3D11SamplerState* pOldPSSampler; ID3D11SamplerState* PSSampler;
g_pd3dDeviceContext->PSGetSamplers(0, 1, &pOldPSSampler); ID3D11PixelShader* PS;
ID3D11PixelShader* pOldPS; ID3D11VertexShader* VS;
ID3D11ClassInstance* pOldPSInstances[256]; // max according to PSSetShader documentation UINT PSInstancesCount, VSInstancesCount;
UINT oldNumPSInstances = 256; ID3D11ClassInstance* PSInstances[256], *VSInstances[256]; // 256 is max according to PSSetShader documentation
g_pd3dDeviceContext->PSGetShader(&pOldPS, pOldPSInstances, &oldNumPSInstances); D3D11_PRIMITIVE_TOPOLOGY PrimitiveTopology;
ID3D11Buffer* pOldVSCBV; ID3D11Buffer* IndexBuffer, *VertexBuffer, *VSConstantBuffer;
g_pd3dDeviceContext->VSGetConstantBuffers(0, 1, &pOldVSCBV); UINT IndexBufferOffset, VertexBufferStride, VertexBufferOffset;
ID3D11VertexShader* pOldVS; DXGI_FORMAT IndexBufferFormat;
ID3D11ClassInstance* pOldVSInstances[256]; // max according to VSSetShader documentation ID3D11InputLayout* InputLayout;
UINT oldNumVSInstances = 256; };
g_pd3dDeviceContext->VSGetShader(&pOldVS, pOldVSInstances, &oldNumVSInstances); BACKUP_DX11_STATE old;
D3D11_PRIMITIVE_TOPOLOGY oldPrimitiveTopology; old.ScissorRectsCount = old.ViewportsCount = D3D11_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE;
g_pd3dDeviceContext->IAGetPrimitiveTopology(&oldPrimitiveTopology); ctx->RSGetScissorRects(&old.ScissorRectsCount, old.ScissorRects);
ID3D11Buffer* pOldIndexBuffer; ctx->RSGetViewports(&old.ViewportsCount, old.Viewports);
DXGI_FORMAT oldIndexBufferFormat; ctx->RSGetState(&old.RS);
UINT oldIndexBufferOffset; ctx->OMGetBlendState(&old.BlendState, old.BlendFactor, &old.SampleMask);
g_pd3dDeviceContext->IAGetIndexBuffer(&pOldIndexBuffer, &oldIndexBufferFormat, &oldIndexBufferOffset); ctx->PSGetShaderResources(0, 1, &old.PSShaderResource);
ID3D11Buffer* pOldVertexBuffer; ctx->PSGetSamplers(0, 1, &old.PSSampler);
UINT oldVertexBufferStride; old.PSInstancesCount = old.VSInstancesCount = 256;
UINT oldVertexBufferOffset; ctx->PSGetShader(&old.PS, old.PSInstances, &old.PSInstancesCount);
g_pd3dDeviceContext->IAGetVertexBuffers(0, 1, &pOldVertexBuffer, &oldVertexBufferStride, &oldVertexBufferOffset); ctx->VSGetShader(&old.VS, old.VSInstances, &old.VSInstancesCount);
ID3D11InputLayout* pOldInputLayout; ctx->VSGetConstantBuffers(0, 1, &old.VSConstantBuffer);
g_pd3dDeviceContext->IAGetInputLayout(&pOldInputLayout); ctx->IAGetPrimitiveTopology(&old.PrimitiveTopology);
UINT oldNumViewports = D3D11_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE; ctx->IAGetIndexBuffer(&old.IndexBuffer, &old.IndexBufferFormat, &old.IndexBufferOffset);
D3D11_VIEWPORT oldViewports[D3D11_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE]; ctx->IAGetVertexBuffers(0, 1, &old.VertexBuffer, &old.VertexBufferStride, &old.VertexBufferOffset);
g_pd3dDeviceContext->RSGetViewports(&oldNumViewports, oldViewports); ctx->IAGetInputLayout(&old.InputLayout);
// Setup viewport // Setup viewport
{ {
@ -211,32 +211,22 @@ void ImGui_ImplDX11_RenderDrawLists(ImDrawData* draw_data)
vtx_offset += cmd_list->VtxBuffer.size(); vtx_offset += cmd_list->VtxBuffer.size();
} }
// Restore modified state // Restore modified DX state
g_pd3dDeviceContext->RSSetScissorRects(oldNumScissorRects, oldScissorRects); ctx->RSSetScissorRects(old.ScissorRectsCount, old.ScissorRects);
g_pd3dDeviceContext->PSSetShaderResources(0, 1, &pOldPSSRV0); ctx->RSSetViewports(old.ViewportsCount, old.Viewports);
if (pOldPSSRV0) pOldPSSRV0->Release(); ctx->RSSetState(old.RS); if (old.RS) old.RS->Release();
g_pd3dDeviceContext->RSSetState(pOldRS); ctx->OMSetBlendState(old.BlendState, old.BlendFactor, old.SampleMask); if (old.BlendState) old.BlendState->Release();
if (pOldRS) pOldRS->Release(); ctx->PSSetShaderResources(0, 1, &old.PSShaderResource); if (old.PSShaderResource) old.PSShaderResource->Release();
g_pd3dDeviceContext->OMSetBlendState(pOldBlendState, oldBlendFactor, oldSampleMask); ctx->PSSetSamplers(0, 1, &old.PSSampler); if (old.PSSampler) old.PSSampler->Release();
if (pOldBlendState) pOldBlendState->Release(); ctx->PSSetShader(old.PS, old.PSInstances, old.PSInstancesCount); if (old.PS) old.PS->Release();
g_pd3dDeviceContext->PSSetSamplers(0, 1, &pOldPSSampler); for (UINT i = 0; i < old.PSInstancesCount; i++) if (old.PSInstances[i]) old.PSInstances[i]->Release();
if (pOldPSSampler) pOldPSSampler->Release(); ctx->VSSetShader(old.VS, old.VSInstances, old.VSInstancesCount); if (old.VS) old.VS->Release();
g_pd3dDeviceContext->PSSetShader(pOldPS, pOldPSInstances, oldNumPSInstances); ctx->VSSetConstantBuffers(0, 1, &old.VSConstantBuffer); if (old.VSConstantBuffer) old.VSConstantBuffer->Release();
if (pOldPS) pOldPS->Release(); for (UINT i = 0; i < old.VSInstancesCount; i++) if (old.VSInstances[i]) old.VSInstances[i]->Release();
for (UINT i = 0; i < oldNumPSInstances; i++) if (pOldPSInstances[i]) pOldPSInstances[i]->Release(); ctx->IASetPrimitiveTopology(old.PrimitiveTopology);
g_pd3dDeviceContext->VSSetConstantBuffers(0, 1, &pOldVSCBV); ctx->IASetIndexBuffer(old.IndexBuffer, old.IndexBufferFormat, old.IndexBufferOffset); if (old.IndexBuffer) old.IndexBuffer->Release();
if (pOldVSCBV) pOldVSCBV->Release(); ctx->IASetVertexBuffers(0, 1, &old.VertexBuffer, &old.VertexBufferStride, &old.VertexBufferOffset); if (old.VertexBuffer) old.VertexBuffer->Release();
g_pd3dDeviceContext->VSSetShader(pOldVS, pOldVSInstances, oldNumVSInstances); ctx->IASetInputLayout(old.InputLayout); if (old.InputLayout) old.InputLayout->Release();
if (pOldVS) pOldVS->Release();
for (UINT i = 0; i < oldNumVSInstances; i++) if (pOldVSInstances[i]) pOldVSInstances[i]->Release();
g_pd3dDeviceContext->IASetPrimitiveTopology(oldPrimitiveTopology);
g_pd3dDeviceContext->IASetIndexBuffer(pOldIndexBuffer, oldIndexBufferFormat, oldIndexBufferOffset);
if (pOldIndexBuffer) pOldIndexBuffer->Release();
g_pd3dDeviceContext->IASetVertexBuffers(0, 1, &pOldVertexBuffer, &oldVertexBufferStride, &oldVertexBufferOffset);
if (pOldVertexBuffer) pOldVertexBuffer->Release();
g_pd3dDeviceContext->IASetInputLayout(pOldInputLayout);
if (pOldInputLayout) pOldInputLayout->Release();
g_pd3dDeviceContext->RSSetViewports(oldNumViewports, oldViewports);
} }
IMGUI_API LRESULT ImGui_ImplDX11_WndProcHandler(HWND, UINT msg, WPARAM wParam, LPARAM lParam) IMGUI_API LRESULT ImGui_ImplDX11_WndProcHandler(HWND, UINT msg, WPARAM wParam, LPARAM lParam)