| | |
| | |
| |
|
| | |
| | |
| | |
| |
|
| | |
| | |
| | |
| | |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | #include "imgui.h" |
| | #include "imgui_impl_dx11.h" |
| |
|
| | |
| | #include <stdio.h> |
| | #include <d3d11.h> |
| | #include <d3dcompiler.h> |
| | #ifdef _MSC_VER |
| | #pragma comment(lib, "d3dcompiler") |
| | #endif |
| |
|
| | |
| | struct ImGui_ImplDX11_Data |
| | { |
| | ID3D11Device* pd3dDevice; |
| | ID3D11DeviceContext* pd3dDeviceContext; |
| | IDXGIFactory* pFactory; |
| | ID3D11Buffer* pVB; |
| | ID3D11Buffer* pIB; |
| | ID3D11VertexShader* pVertexShader; |
| | ID3D11InputLayout* pInputLayout; |
| | ID3D11Buffer* pVertexConstantBuffer; |
| | ID3D11PixelShader* pPixelShader; |
| | ID3D11SamplerState* pFontSampler; |
| | ID3D11ShaderResourceView* pFontTextureView; |
| | ID3D11RasterizerState* pRasterizerState; |
| | ID3D11BlendState* pBlendState; |
| | ID3D11DepthStencilState* pDepthStencilState; |
| | int VertexBufferSize; |
| | int IndexBufferSize; |
| |
|
| | ImGui_ImplDX11_Data() { memset(this, 0, sizeof(*this)); VertexBufferSize = 5000; IndexBufferSize = 10000; } |
| | }; |
| |
|
| | struct VERTEX_CONSTANT_BUFFER |
| | { |
| | float mvp[4][4]; |
| | }; |
| |
|
| | |
| | |
| | static ImGui_ImplDX11_Data* ImGui_ImplDX11_GetBackendData() |
| | { |
| | return ImGui::GetCurrentContext() ? (ImGui_ImplDX11_Data*)ImGui::GetIO().BackendRendererUserData : NULL; |
| | } |
| |
|
| | |
| | static void ImGui_ImplDX11_SetupRenderState(ImDrawData* draw_data, ID3D11DeviceContext* ctx) |
| | { |
| | ImGui_ImplDX11_Data* bd = ImGui_ImplDX11_GetBackendData(); |
| |
|
| | |
| | D3D11_VIEWPORT vp; |
| | memset(&vp, 0, sizeof(D3D11_VIEWPORT)); |
| | vp.Width = draw_data->DisplaySize.x; |
| | vp.Height = draw_data->DisplaySize.y; |
| | vp.MinDepth = 0.0f; |
| | vp.MaxDepth = 1.0f; |
| | vp.TopLeftX = vp.TopLeftY = 0; |
| | ctx->RSSetViewports(1, &vp); |
| |
|
| | |
| | unsigned int stride = sizeof(ImDrawVert); |
| | unsigned int offset = 0; |
| | ctx->IASetInputLayout(bd->pInputLayout); |
| | ctx->IASetVertexBuffers(0, 1, &bd->pVB, &stride, &offset); |
| | ctx->IASetIndexBuffer(bd->pIB, sizeof(ImDrawIdx) == 2 ? DXGI_FORMAT_R16_UINT : DXGI_FORMAT_R32_UINT, 0); |
| | ctx->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); |
| | ctx->VSSetShader(bd->pVertexShader, NULL, 0); |
| | ctx->VSSetConstantBuffers(0, 1, &bd->pVertexConstantBuffer); |
| | ctx->PSSetShader(bd->pPixelShader, NULL, 0); |
| | ctx->PSSetSamplers(0, 1, &bd->pFontSampler); |
| | ctx->GSSetShader(NULL, NULL, 0); |
| | ctx->HSSetShader(NULL, NULL, 0); |
| | ctx->DSSetShader(NULL, NULL, 0); |
| | ctx->CSSetShader(NULL, NULL, 0); |
| |
|
| | |
| | const float blend_factor[4] = { 0.f, 0.f, 0.f, 0.f }; |
| | ctx->OMSetBlendState(bd->pBlendState, blend_factor, 0xffffffff); |
| | ctx->OMSetDepthStencilState(bd->pDepthStencilState, 0); |
| | ctx->RSSetState(bd->pRasterizerState); |
| | } |
| |
|
| | |
| | void ImGui_ImplDX11_RenderDrawData(ImDrawData* draw_data) |
| | { |
| | |
| | if (draw_data->DisplaySize.x <= 0.0f || draw_data->DisplaySize.y <= 0.0f) |
| | return; |
| |
|
| | ImGui_ImplDX11_Data* bd = ImGui_ImplDX11_GetBackendData(); |
| | ID3D11DeviceContext* ctx = bd->pd3dDeviceContext; |
| |
|
| | |
| | if (!bd->pVB || bd->VertexBufferSize < draw_data->TotalVtxCount) |
| | { |
| | if (bd->pVB) { bd->pVB->Release(); bd->pVB = NULL; } |
| | bd->VertexBufferSize = draw_data->TotalVtxCount + 5000; |
| | D3D11_BUFFER_DESC desc; |
| | memset(&desc, 0, sizeof(D3D11_BUFFER_DESC)); |
| | desc.Usage = D3D11_USAGE_DYNAMIC; |
| | desc.ByteWidth = bd->VertexBufferSize * sizeof(ImDrawVert); |
| | desc.BindFlags = D3D11_BIND_VERTEX_BUFFER; |
| | desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; |
| | desc.MiscFlags = 0; |
| | if (bd->pd3dDevice->CreateBuffer(&desc, NULL, &bd->pVB) < 0) |
| | return; |
| | } |
| | if (!bd->pIB || bd->IndexBufferSize < draw_data->TotalIdxCount) |
| | { |
| | if (bd->pIB) { bd->pIB->Release(); bd->pIB = NULL; } |
| | bd->IndexBufferSize = draw_data->TotalIdxCount + 10000; |
| | D3D11_BUFFER_DESC desc; |
| | memset(&desc, 0, sizeof(D3D11_BUFFER_DESC)); |
| | desc.Usage = D3D11_USAGE_DYNAMIC; |
| | desc.ByteWidth = bd->IndexBufferSize * sizeof(ImDrawIdx); |
| | desc.BindFlags = D3D11_BIND_INDEX_BUFFER; |
| | desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; |
| | if (bd->pd3dDevice->CreateBuffer(&desc, NULL, &bd->pIB) < 0) |
| | return; |
| | } |
| |
|
| | |
| | D3D11_MAPPED_SUBRESOURCE vtx_resource, idx_resource; |
| | if (ctx->Map(bd->pVB, 0, D3D11_MAP_WRITE_DISCARD, 0, &vtx_resource) != S_OK) |
| | return; |
| | if (ctx->Map(bd->pIB, 0, D3D11_MAP_WRITE_DISCARD, 0, &idx_resource) != S_OK) |
| | return; |
| | ImDrawVert* vtx_dst = (ImDrawVert*)vtx_resource.pData; |
| | ImDrawIdx* idx_dst = (ImDrawIdx*)idx_resource.pData; |
| | for (int n = 0; n < draw_data->CmdListsCount; n++) |
| | { |
| | const ImDrawList* cmd_list = draw_data->CmdLists[n]; |
| | memcpy(vtx_dst, cmd_list->VtxBuffer.Data, cmd_list->VtxBuffer.Size * sizeof(ImDrawVert)); |
| | memcpy(idx_dst, cmd_list->IdxBuffer.Data, cmd_list->IdxBuffer.Size * sizeof(ImDrawIdx)); |
| | vtx_dst += cmd_list->VtxBuffer.Size; |
| | idx_dst += cmd_list->IdxBuffer.Size; |
| | } |
| | ctx->Unmap(bd->pVB, 0); |
| | ctx->Unmap(bd->pIB, 0); |
| |
|
| | |
| | |
| | { |
| | D3D11_MAPPED_SUBRESOURCE mapped_resource; |
| | if (ctx->Map(bd->pVertexConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped_resource) != S_OK) |
| | return; |
| | VERTEX_CONSTANT_BUFFER* constant_buffer = (VERTEX_CONSTANT_BUFFER*)mapped_resource.pData; |
| | float L = draw_data->DisplayPos.x; |
| | float R = draw_data->DisplayPos.x + draw_data->DisplaySize.x; |
| | float T = draw_data->DisplayPos.y; |
| | float B = draw_data->DisplayPos.y + draw_data->DisplaySize.y; |
| | float mvp[4][4] = |
| | { |
| | { 2.0f/(R-L), 0.0f, 0.0f, 0.0f }, |
| | { 0.0f, 2.0f/(T-B), 0.0f, 0.0f }, |
| | { 0.0f, 0.0f, 0.5f, 0.0f }, |
| | { (R+L)/(L-R), (T+B)/(B-T), 0.5f, 1.0f }, |
| | }; |
| | memcpy(&constant_buffer->mvp, mvp, sizeof(mvp)); |
| | ctx->Unmap(bd->pVertexConstantBuffer, 0); |
| | } |
| |
|
| | |
| | struct BACKUP_DX11_STATE |
| | { |
| | UINT ScissorRectsCount, ViewportsCount; |
| | D3D11_RECT ScissorRects[D3D11_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE]; |
| | D3D11_VIEWPORT Viewports[D3D11_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE]; |
| | ID3D11RasterizerState* RS; |
| | ID3D11BlendState* BlendState; |
| | FLOAT BlendFactor[4]; |
| | UINT SampleMask; |
| | UINT StencilRef; |
| | ID3D11DepthStencilState* DepthStencilState; |
| | ID3D11ShaderResourceView* PSShaderResource; |
| | ID3D11SamplerState* PSSampler; |
| | ID3D11PixelShader* PS; |
| | ID3D11VertexShader* VS; |
| | ID3D11GeometryShader* GS; |
| | UINT PSInstancesCount, VSInstancesCount, GSInstancesCount; |
| | ID3D11ClassInstance *PSInstances[256], *VSInstances[256], *GSInstances[256]; |
| | D3D11_PRIMITIVE_TOPOLOGY PrimitiveTopology; |
| | ID3D11Buffer* IndexBuffer, *VertexBuffer, *VSConstantBuffer; |
| | UINT IndexBufferOffset, VertexBufferStride, VertexBufferOffset; |
| | DXGI_FORMAT IndexBufferFormat; |
| | ID3D11InputLayout* InputLayout; |
| | }; |
| | BACKUP_DX11_STATE old = {}; |
| | old.ScissorRectsCount = old.ViewportsCount = D3D11_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE; |
| | ctx->RSGetScissorRects(&old.ScissorRectsCount, old.ScissorRects); |
| | ctx->RSGetViewports(&old.ViewportsCount, old.Viewports); |
| | ctx->RSGetState(&old.RS); |
| | ctx->OMGetBlendState(&old.BlendState, old.BlendFactor, &old.SampleMask); |
| | ctx->OMGetDepthStencilState(&old.DepthStencilState, &old.StencilRef); |
| | ctx->PSGetShaderResources(0, 1, &old.PSShaderResource); |
| | ctx->PSGetSamplers(0, 1, &old.PSSampler); |
| | old.PSInstancesCount = old.VSInstancesCount = old.GSInstancesCount = 256; |
| | ctx->PSGetShader(&old.PS, old.PSInstances, &old.PSInstancesCount); |
| | ctx->VSGetShader(&old.VS, old.VSInstances, &old.VSInstancesCount); |
| | ctx->VSGetConstantBuffers(0, 1, &old.VSConstantBuffer); |
| | ctx->GSGetShader(&old.GS, old.GSInstances, &old.GSInstancesCount); |
| |
|
| | ctx->IAGetPrimitiveTopology(&old.PrimitiveTopology); |
| | ctx->IAGetIndexBuffer(&old.IndexBuffer, &old.IndexBufferFormat, &old.IndexBufferOffset); |
| | ctx->IAGetVertexBuffers(0, 1, &old.VertexBuffer, &old.VertexBufferStride, &old.VertexBufferOffset); |
| | ctx->IAGetInputLayout(&old.InputLayout); |
| |
|
| | |
| | ImGui_ImplDX11_SetupRenderState(draw_data, ctx); |
| |
|
| | |
| | |
| | int global_idx_offset = 0; |
| | int global_vtx_offset = 0; |
| | ImVec2 clip_off = draw_data->DisplayPos; |
| | for (int n = 0; n < draw_data->CmdListsCount; n++) |
| | { |
| | const ImDrawList* cmd_list = draw_data->CmdLists[n]; |
| | for (int cmd_i = 0; cmd_i < cmd_list->CmdBuffer.Size; cmd_i++) |
| | { |
| | const ImDrawCmd* pcmd = &cmd_list->CmdBuffer[cmd_i]; |
| | if (pcmd->UserCallback != NULL) |
| | { |
| | |
| | |
| | if (pcmd->UserCallback == ImDrawCallback_ResetRenderState) |
| | ImGui_ImplDX11_SetupRenderState(draw_data, ctx); |
| | else |
| | pcmd->UserCallback(cmd_list, pcmd); |
| | } |
| | else |
| | { |
| | |
| | ImVec2 clip_min(pcmd->ClipRect.x - clip_off.x, pcmd->ClipRect.y - clip_off.y); |
| | ImVec2 clip_max(pcmd->ClipRect.z - clip_off.x, pcmd->ClipRect.w - clip_off.y); |
| | if (clip_max.x <= clip_min.x || clip_max.y <= clip_min.y) |
| | continue; |
| |
|
| | |
| | const D3D11_RECT r = { (LONG)clip_min.x, (LONG)clip_min.y, (LONG)clip_max.x, (LONG)clip_max.y }; |
| | ctx->RSSetScissorRects(1, &r); |
| |
|
| | |
| | ID3D11ShaderResourceView* texture_srv = (ID3D11ShaderResourceView*)pcmd->GetTexID(); |
| | ctx->PSSetShaderResources(0, 1, &texture_srv); |
| | ctx->DrawIndexed(pcmd->ElemCount, pcmd->IdxOffset + global_idx_offset, pcmd->VtxOffset + global_vtx_offset); |
| | } |
| | } |
| | global_idx_offset += cmd_list->IdxBuffer.Size; |
| | global_vtx_offset += cmd_list->VtxBuffer.Size; |
| | } |
| |
|
| | |
| | ctx->RSSetScissorRects(old.ScissorRectsCount, old.ScissorRects); |
| | ctx->RSSetViewports(old.ViewportsCount, old.Viewports); |
| | ctx->RSSetState(old.RS); if (old.RS) old.RS->Release(); |
| | ctx->OMSetBlendState(old.BlendState, old.BlendFactor, old.SampleMask); if (old.BlendState) old.BlendState->Release(); |
| | ctx->OMSetDepthStencilState(old.DepthStencilState, old.StencilRef); if (old.DepthStencilState) old.DepthStencilState->Release(); |
| | ctx->PSSetShaderResources(0, 1, &old.PSShaderResource); if (old.PSShaderResource) old.PSShaderResource->Release(); |
| | ctx->PSSetSamplers(0, 1, &old.PSSampler); if (old.PSSampler) old.PSSampler->Release(); |
| | ctx->PSSetShader(old.PS, old.PSInstances, old.PSInstancesCount); if (old.PS) old.PS->Release(); |
| | for (UINT i = 0; i < old.PSInstancesCount; i++) if (old.PSInstances[i]) old.PSInstances[i]->Release(); |
| | ctx->VSSetShader(old.VS, old.VSInstances, old.VSInstancesCount); if (old.VS) old.VS->Release(); |
| | ctx->VSSetConstantBuffers(0, 1, &old.VSConstantBuffer); if (old.VSConstantBuffer) old.VSConstantBuffer->Release(); |
| | ctx->GSSetShader(old.GS, old.GSInstances, old.GSInstancesCount); if (old.GS) old.GS->Release(); |
| | for (UINT i = 0; i < old.VSInstancesCount; i++) if (old.VSInstances[i]) old.VSInstances[i]->Release(); |
| | ctx->IASetPrimitiveTopology(old.PrimitiveTopology); |
| | ctx->IASetIndexBuffer(old.IndexBuffer, old.IndexBufferFormat, old.IndexBufferOffset); if (old.IndexBuffer) old.IndexBuffer->Release(); |
| | ctx->IASetVertexBuffers(0, 1, &old.VertexBuffer, &old.VertexBufferStride, &old.VertexBufferOffset); if (old.VertexBuffer) old.VertexBuffer->Release(); |
| | ctx->IASetInputLayout(old.InputLayout); if (old.InputLayout) old.InputLayout->Release(); |
| | } |
| |
|
| | static void ImGui_ImplDX11_CreateFontsTexture() |
| | { |
| | |
| | ImGuiIO& io = ImGui::GetIO(); |
| | ImGui_ImplDX11_Data* bd = ImGui_ImplDX11_GetBackendData(); |
| | unsigned char* pixels; |
| | int width, height; |
| | io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height); |
| |
|
| | |
| | { |
| | D3D11_TEXTURE2D_DESC desc; |
| | ZeroMemory(&desc, sizeof(desc)); |
| | desc.Width = width; |
| | desc.Height = height; |
| | desc.MipLevels = 1; |
| | desc.ArraySize = 1; |
| | desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; |
| | desc.SampleDesc.Count = 1; |
| | desc.Usage = D3D11_USAGE_DEFAULT; |
| | desc.BindFlags = D3D11_BIND_SHADER_RESOURCE; |
| | desc.CPUAccessFlags = 0; |
| |
|
| | ID3D11Texture2D* pTexture = NULL; |
| | D3D11_SUBRESOURCE_DATA subResource; |
| | subResource.pSysMem = pixels; |
| | subResource.SysMemPitch = desc.Width * 4; |
| | subResource.SysMemSlicePitch = 0; |
| | bd->pd3dDevice->CreateTexture2D(&desc, &subResource, &pTexture); |
| | IM_ASSERT(pTexture != NULL); |
| |
|
| | |
| | D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc; |
| | ZeroMemory(&srvDesc, sizeof(srvDesc)); |
| | srvDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; |
| | srvDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D; |
| | srvDesc.Texture2D.MipLevels = desc.MipLevels; |
| | srvDesc.Texture2D.MostDetailedMip = 0; |
| | bd->pd3dDevice->CreateShaderResourceView(pTexture, &srvDesc, &bd->pFontTextureView); |
| | pTexture->Release(); |
| | } |
| |
|
| | |
| | io.Fonts->SetTexID((ImTextureID)bd->pFontTextureView); |
| |
|
| | |
| | { |
| | D3D11_SAMPLER_DESC desc; |
| | ZeroMemory(&desc, sizeof(desc)); |
| | desc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR; |
| | 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; |
| | bd->pd3dDevice->CreateSamplerState(&desc, &bd->pFontSampler); |
| | } |
| | } |
| |
|
| | bool ImGui_ImplDX11_CreateDeviceObjects() |
| | { |
| | ImGui_ImplDX11_Data* bd = ImGui_ImplDX11_GetBackendData(); |
| | if (!bd->pd3dDevice) |
| | return false; |
| | if (bd->pFontSampler) |
| | ImGui_ImplDX11_InvalidateDeviceObjects(); |
| |
|
| | |
| | |
| | |
| | |
| | |
| |
|
| | |
| | { |
| | static const char* vertexShader = |
| | "cbuffer vertexBuffer : register(b0) \ |
| | {\ |
| | float4x4 ProjectionMatrix; \ |
| | };\ |
| | struct VS_INPUT\ |
| | {\ |
| | float2 pos : POSITION;\ |
| | float4 col : COLOR0;\ |
| | float2 uv : TEXCOORD0;\ |
| | };\ |
| | \ |
| | struct PS_INPUT\ |
| | {\ |
| | float4 pos : SV_POSITION;\ |
| | float4 col : COLOR0;\ |
| | float2 uv : TEXCOORD0;\ |
| | };\ |
| | \ |
| | PS_INPUT main(VS_INPUT input)\ |
| | {\ |
| | PS_INPUT output;\ |
| | output.pos = mul( ProjectionMatrix, float4(input.pos.xy, 0.f, 1.f));\ |
| | output.col = input.col;\ |
| | output.uv = input.uv;\ |
| | return output;\ |
| | }"; |
| |
|
| | ID3DBlob* vertexShaderBlob; |
| | if (FAILED(D3DCompile(vertexShader, strlen(vertexShader), NULL, NULL, NULL, "main", "vs_4_0", 0, 0, &vertexShaderBlob, NULL))) |
| | return false; |
| | if (bd->pd3dDevice->CreateVertexShader(vertexShaderBlob->GetBufferPointer(), vertexShaderBlob->GetBufferSize(), NULL, &bd->pVertexShader) != S_OK) |
| | { |
| | vertexShaderBlob->Release(); |
| | return false; |
| | } |
| |
|
| | |
| | D3D11_INPUT_ELEMENT_DESC local_layout[] = |
| | { |
| | { "POSITION", 0, DXGI_FORMAT_R32G32_FLOAT, 0, (UINT)IM_OFFSETOF(ImDrawVert, pos), D3D11_INPUT_PER_VERTEX_DATA, 0 }, |
| | { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, (UINT)IM_OFFSETOF(ImDrawVert, uv), D3D11_INPUT_PER_VERTEX_DATA, 0 }, |
| | { "COLOR", 0, DXGI_FORMAT_R8G8B8A8_UNORM, 0, (UINT)IM_OFFSETOF(ImDrawVert, col), D3D11_INPUT_PER_VERTEX_DATA, 0 }, |
| | }; |
| | if (bd->pd3dDevice->CreateInputLayout(local_layout, 3, vertexShaderBlob->GetBufferPointer(), vertexShaderBlob->GetBufferSize(), &bd->pInputLayout) != S_OK) |
| | { |
| | vertexShaderBlob->Release(); |
| | return false; |
| | } |
| | vertexShaderBlob->Release(); |
| |
|
| | |
| | { |
| | D3D11_BUFFER_DESC desc; |
| | desc.ByteWidth = sizeof(VERTEX_CONSTANT_BUFFER); |
| | desc.Usage = D3D11_USAGE_DYNAMIC; |
| | desc.BindFlags = D3D11_BIND_CONSTANT_BUFFER; |
| | desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; |
| | desc.MiscFlags = 0; |
| | bd->pd3dDevice->CreateBuffer(&desc, NULL, &bd->pVertexConstantBuffer); |
| | } |
| | } |
| |
|
| | |
| | { |
| | static const char* pixelShader = |
| | "struct PS_INPUT\ |
| | {\ |
| | float4 pos : SV_POSITION;\ |
| | float4 col : COLOR0;\ |
| | float2 uv : TEXCOORD0;\ |
| | };\ |
| | sampler sampler0;\ |
| | Texture2D texture0;\ |
| | \ |
| | float4 main(PS_INPUT input) : SV_Target\ |
| | {\ |
| | float4 out_col = input.col * texture0.Sample(sampler0, input.uv); \ |
| | return out_col; \ |
| | }"; |
| |
|
| | ID3DBlob* pixelShaderBlob; |
| | if (FAILED(D3DCompile(pixelShader, strlen(pixelShader), NULL, NULL, NULL, "main", "ps_4_0", 0, 0, &pixelShaderBlob, NULL))) |
| | return false; |
| | if (bd->pd3dDevice->CreatePixelShader(pixelShaderBlob->GetBufferPointer(), pixelShaderBlob->GetBufferSize(), NULL, &bd->pPixelShader) != S_OK) |
| | { |
| | pixelShaderBlob->Release(); |
| | return false; |
| | } |
| | pixelShaderBlob->Release(); |
| | } |
| |
|
| | |
| | { |
| | D3D11_BLEND_DESC desc; |
| | ZeroMemory(&desc, sizeof(desc)); |
| | desc.AlphaToCoverageEnable = false; |
| | desc.RenderTarget[0].BlendEnable = true; |
| | desc.RenderTarget[0].SrcBlend = D3D11_BLEND_SRC_ALPHA; |
| | desc.RenderTarget[0].DestBlend = D3D11_BLEND_INV_SRC_ALPHA; |
| | desc.RenderTarget[0].BlendOp = D3D11_BLEND_OP_ADD; |
| | desc.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_ONE; |
| | desc.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_INV_SRC_ALPHA; |
| | desc.RenderTarget[0].BlendOpAlpha = D3D11_BLEND_OP_ADD; |
| | desc.RenderTarget[0].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL; |
| | bd->pd3dDevice->CreateBlendState(&desc, &bd->pBlendState); |
| | } |
| |
|
| | |
| | { |
| | D3D11_RASTERIZER_DESC desc; |
| | ZeroMemory(&desc, sizeof(desc)); |
| | desc.FillMode = D3D11_FILL_SOLID; |
| | desc.CullMode = D3D11_CULL_NONE; |
| | desc.ScissorEnable = true; |
| | desc.DepthClipEnable = true; |
| | bd->pd3dDevice->CreateRasterizerState(&desc, &bd->pRasterizerState); |
| | } |
| |
|
| | |
| | { |
| | D3D11_DEPTH_STENCIL_DESC desc; |
| | ZeroMemory(&desc, sizeof(desc)); |
| | desc.DepthEnable = false; |
| | desc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL; |
| | desc.DepthFunc = D3D11_COMPARISON_ALWAYS; |
| | desc.StencilEnable = false; |
| | desc.FrontFace.StencilFailOp = desc.FrontFace.StencilDepthFailOp = desc.FrontFace.StencilPassOp = D3D11_STENCIL_OP_KEEP; |
| | desc.FrontFace.StencilFunc = D3D11_COMPARISON_ALWAYS; |
| | desc.BackFace = desc.FrontFace; |
| | bd->pd3dDevice->CreateDepthStencilState(&desc, &bd->pDepthStencilState); |
| | } |
| |
|
| | ImGui_ImplDX11_CreateFontsTexture(); |
| |
|
| | return true; |
| | } |
| |
|
| | void ImGui_ImplDX11_InvalidateDeviceObjects() |
| | { |
| | ImGui_ImplDX11_Data* bd = ImGui_ImplDX11_GetBackendData(); |
| | if (!bd->pd3dDevice) |
| | return; |
| |
|
| | if (bd->pFontSampler) { bd->pFontSampler->Release(); bd->pFontSampler = NULL; } |
| | if (bd->pFontTextureView) { bd->pFontTextureView->Release(); bd->pFontTextureView = NULL; ImGui::GetIO().Fonts->SetTexID(NULL); } |
| | if (bd->pIB) { bd->pIB->Release(); bd->pIB = NULL; } |
| | if (bd->pVB) { bd->pVB->Release(); bd->pVB = NULL; } |
| | if (bd->pBlendState) { bd->pBlendState->Release(); bd->pBlendState = NULL; } |
| | if (bd->pDepthStencilState) { bd->pDepthStencilState->Release(); bd->pDepthStencilState = NULL; } |
| | if (bd->pRasterizerState) { bd->pRasterizerState->Release(); bd->pRasterizerState = NULL; } |
| | if (bd->pPixelShader) { bd->pPixelShader->Release(); bd->pPixelShader = NULL; } |
| | if (bd->pVertexConstantBuffer) { bd->pVertexConstantBuffer->Release(); bd->pVertexConstantBuffer = NULL; } |
| | if (bd->pInputLayout) { bd->pInputLayout->Release(); bd->pInputLayout = NULL; } |
| | if (bd->pVertexShader) { bd->pVertexShader->Release(); bd->pVertexShader = NULL; } |
| | } |
| |
|
| | bool ImGui_ImplDX11_Init(ID3D11Device* device, ID3D11DeviceContext* device_context) |
| | { |
| | ImGuiIO& io = ImGui::GetIO(); |
| | IM_ASSERT(io.BackendRendererUserData == NULL && "Already initialized a renderer backend!"); |
| |
|
| | |
| | ImGui_ImplDX11_Data* bd = IM_NEW(ImGui_ImplDX11_Data)(); |
| | io.BackendRendererUserData = (void*)bd; |
| | io.BackendRendererName = "imgui_impl_dx11"; |
| | io.BackendFlags |= ImGuiBackendFlags_RendererHasVtxOffset; |
| |
|
| | |
| | IDXGIDevice* pDXGIDevice = NULL; |
| | IDXGIAdapter* pDXGIAdapter = NULL; |
| | IDXGIFactory* pFactory = NULL; |
| |
|
| | if (device->QueryInterface(IID_PPV_ARGS(&pDXGIDevice)) == S_OK) |
| | if (pDXGIDevice->GetParent(IID_PPV_ARGS(&pDXGIAdapter)) == S_OK) |
| | if (pDXGIAdapter->GetParent(IID_PPV_ARGS(&pFactory)) == S_OK) |
| | { |
| | bd->pd3dDevice = device; |
| | bd->pd3dDeviceContext = device_context; |
| | bd->pFactory = pFactory; |
| | } |
| | if (pDXGIDevice) pDXGIDevice->Release(); |
| | if (pDXGIAdapter) pDXGIAdapter->Release(); |
| | bd->pd3dDevice->AddRef(); |
| | bd->pd3dDeviceContext->AddRef(); |
| |
|
| | return true; |
| | } |
| |
|
| | void ImGui_ImplDX11_Shutdown() |
| | { |
| | ImGui_ImplDX11_Data* bd = ImGui_ImplDX11_GetBackendData(); |
| | IM_ASSERT(bd != NULL && "No renderer backend to shutdown, or already shutdown?"); |
| | ImGuiIO& io = ImGui::GetIO(); |
| |
|
| | ImGui_ImplDX11_InvalidateDeviceObjects(); |
| | if (bd->pFactory) { bd->pFactory->Release(); } |
| | if (bd->pd3dDevice) { bd->pd3dDevice->Release(); } |
| | if (bd->pd3dDeviceContext) { bd->pd3dDeviceContext->Release(); } |
| | io.BackendRendererName = NULL; |
| | io.BackendRendererUserData = NULL; |
| | IM_DELETE(bd); |
| | } |
| |
|
| | void ImGui_ImplDX11_NewFrame() |
| | { |
| | ImGui_ImplDX11_Data* bd = ImGui_ImplDX11_GetBackendData(); |
| | IM_ASSERT(bd != NULL && "Did you call ImGui_ImplDX11_Init()?"); |
| |
|
| | if (!bd->pFontSampler) |
| | ImGui_ImplDX11_CreateDeviceObjects(); |
| | } |
| |
|