| |
| |
|
|
| |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| #include "imgui.h" |
| #ifndef IMGUI_DISABLE |
| #include "imgui_impl_dx10.h" |
|
|
| |
| #include <stdio.h> |
| #include <d3d10_1.h> |
| #include <d3d10.h> |
| #include <d3dcompiler.h> |
| #ifdef _MSC_VER |
| #pragma comment(lib, "d3dcompiler") |
| #endif |
|
|
| |
| struct ImGui_ImplDX10_Data |
| { |
| ID3D10Device* pd3dDevice; |
| IDXGIFactory* pFactory; |
| ID3D10Buffer* pVB; |
| ID3D10Buffer* pIB; |
| ID3D10VertexShader* pVertexShader; |
| ID3D10InputLayout* pInputLayout; |
| ID3D10Buffer* pVertexConstantBuffer; |
| ID3D10PixelShader* pPixelShader; |
| ID3D10SamplerState* pFontSampler; |
| ID3D10ShaderResourceView* pFontTextureView; |
| ID3D10RasterizerState* pRasterizerState; |
| ID3D10BlendState* pBlendState; |
| ID3D10DepthStencilState* pDepthStencilState; |
| int VertexBufferSize; |
| int IndexBufferSize; |
|
|
| ImGui_ImplDX10_Data() { memset((void*)this, 0, sizeof(*this)); VertexBufferSize = 5000; IndexBufferSize = 10000; } |
| }; |
|
|
| struct VERTEX_CONSTANT_BUFFER_DX10 |
| { |
| float mvp[4][4]; |
| }; |
|
|
| |
| |
| static ImGui_ImplDX10_Data* ImGui_ImplDX10_GetBackendData() |
| { |
| return ImGui::GetCurrentContext() ? (ImGui_ImplDX10_Data*)ImGui::GetIO().BackendRendererUserData : nullptr; |
| } |
|
|
| |
| static void ImGui_ImplDX10_SetupRenderState(ImDrawData* draw_data, ID3D10Device* device) |
| { |
| ImGui_ImplDX10_Data* bd = ImGui_ImplDX10_GetBackendData(); |
|
|
| |
| D3D10_VIEWPORT vp = {}; |
| vp.Width = (UINT)draw_data->DisplaySize.x; |
| vp.Height = (UINT)draw_data->DisplaySize.y; |
| vp.MinDepth = 0.0f; |
| vp.MaxDepth = 1.0f; |
| vp.TopLeftX = vp.TopLeftY = 0; |
| device->RSSetViewports(1, &vp); |
|
|
| |
| |
| void* mapped_resource; |
| if (bd->pVertexConstantBuffer->Map(D3D10_MAP_WRITE_DISCARD, 0, &mapped_resource) == S_OK) |
| { |
| VERTEX_CONSTANT_BUFFER_DX10* constant_buffer = (VERTEX_CONSTANT_BUFFER_DX10*)mapped_resource; |
| 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)); |
| bd->pVertexConstantBuffer->Unmap(); |
| } |
|
|
| |
| unsigned int stride = sizeof(ImDrawVert); |
| unsigned int offset = 0; |
| device->IASetInputLayout(bd->pInputLayout); |
| device->IASetVertexBuffers(0, 1, &bd->pVB, &stride, &offset); |
| device->IASetIndexBuffer(bd->pIB, sizeof(ImDrawIdx) == 2 ? DXGI_FORMAT_R16_UINT : DXGI_FORMAT_R32_UINT, 0); |
| device->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELIST); |
| device->VSSetShader(bd->pVertexShader); |
| device->VSSetConstantBuffers(0, 1, &bd->pVertexConstantBuffer); |
| device->PSSetShader(bd->pPixelShader); |
| device->PSSetSamplers(0, 1, &bd->pFontSampler); |
| device->GSSetShader(nullptr); |
|
|
| |
| const float blend_factor[4] = { 0.f, 0.f, 0.f, 0.f }; |
| device->OMSetBlendState(bd->pBlendState, blend_factor, 0xffffffff); |
| device->OMSetDepthStencilState(bd->pDepthStencilState, 0); |
| device->RSSetState(bd->pRasterizerState); |
| } |
|
|
| |
| void ImGui_ImplDX10_RenderDrawData(ImDrawData* draw_data) |
| { |
| |
| if (draw_data->DisplaySize.x <= 0.0f || draw_data->DisplaySize.y <= 0.0f) |
| return; |
|
|
| ImGui_ImplDX10_Data* bd = ImGui_ImplDX10_GetBackendData(); |
| ID3D10Device* device = bd->pd3dDevice; |
|
|
| |
| if (!bd->pVB || bd->VertexBufferSize < draw_data->TotalVtxCount) |
| { |
| if (bd->pVB) { bd->pVB->Release(); bd->pVB = nullptr; } |
| bd->VertexBufferSize = draw_data->TotalVtxCount + 5000; |
| D3D10_BUFFER_DESC desc = {}; |
| desc.Usage = D3D10_USAGE_DYNAMIC; |
| desc.ByteWidth = bd->VertexBufferSize * sizeof(ImDrawVert); |
| desc.BindFlags = D3D10_BIND_VERTEX_BUFFER; |
| desc.CPUAccessFlags = D3D10_CPU_ACCESS_WRITE; |
| desc.MiscFlags = 0; |
| if (device->CreateBuffer(&desc, nullptr, &bd->pVB) < 0) |
| return; |
| } |
|
|
| if (!bd->pIB || bd->IndexBufferSize < draw_data->TotalIdxCount) |
| { |
| if (bd->pIB) { bd->pIB->Release(); bd->pIB = nullptr; } |
| bd->IndexBufferSize = draw_data->TotalIdxCount + 10000; |
| D3D10_BUFFER_DESC desc = {}; |
| desc.Usage = D3D10_USAGE_DYNAMIC; |
| desc.ByteWidth = bd->IndexBufferSize * sizeof(ImDrawIdx); |
| desc.BindFlags = D3D10_BIND_INDEX_BUFFER; |
| desc.CPUAccessFlags = D3D10_CPU_ACCESS_WRITE; |
| if (device->CreateBuffer(&desc, nullptr, &bd->pIB) < 0) |
| return; |
| } |
|
|
| |
| ImDrawVert* vtx_dst = nullptr; |
| ImDrawIdx* idx_dst = nullptr; |
| bd->pVB->Map(D3D10_MAP_WRITE_DISCARD, 0, (void**)&vtx_dst); |
| bd->pIB->Map(D3D10_MAP_WRITE_DISCARD, 0, (void**)&idx_dst); |
| for (int n = 0; n < draw_data->CmdListsCount; n++) |
| { |
| const ImDrawList* draw_list = draw_data->CmdLists[n]; |
| memcpy(vtx_dst, draw_list->VtxBuffer.Data, draw_list->VtxBuffer.Size * sizeof(ImDrawVert)); |
| memcpy(idx_dst, draw_list->IdxBuffer.Data, draw_list->IdxBuffer.Size * sizeof(ImDrawIdx)); |
| vtx_dst += draw_list->VtxBuffer.Size; |
| idx_dst += draw_list->IdxBuffer.Size; |
| } |
| bd->pVB->Unmap(); |
| bd->pIB->Unmap(); |
|
|
| |
| struct BACKUP_DX10_STATE |
| { |
| UINT ScissorRectsCount, ViewportsCount; |
| D3D10_RECT ScissorRects[D3D10_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE]; |
| D3D10_VIEWPORT Viewports[D3D10_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE]; |
| ID3D10RasterizerState* RS; |
| ID3D10BlendState* BlendState; |
| FLOAT BlendFactor[4]; |
| UINT SampleMask; |
| UINT StencilRef; |
| ID3D10DepthStencilState* DepthStencilState; |
| ID3D10ShaderResourceView* PSShaderResource; |
| ID3D10SamplerState* PSSampler; |
| ID3D10PixelShader* PS; |
| ID3D10VertexShader* VS; |
| ID3D10GeometryShader* GS; |
| D3D10_PRIMITIVE_TOPOLOGY PrimitiveTopology; |
| ID3D10Buffer* IndexBuffer, *VertexBuffer, *VSConstantBuffer; |
| UINT IndexBufferOffset, VertexBufferStride, VertexBufferOffset; |
| DXGI_FORMAT IndexBufferFormat; |
| ID3D10InputLayout* InputLayout; |
| }; |
| BACKUP_DX10_STATE old = {}; |
| old.ScissorRectsCount = old.ViewportsCount = D3D10_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE; |
| device->RSGetScissorRects(&old.ScissorRectsCount, old.ScissorRects); |
| device->RSGetViewports(&old.ViewportsCount, old.Viewports); |
| device->RSGetState(&old.RS); |
| device->OMGetBlendState(&old.BlendState, old.BlendFactor, &old.SampleMask); |
| device->OMGetDepthStencilState(&old.DepthStencilState, &old.StencilRef); |
| device->PSGetShaderResources(0, 1, &old.PSShaderResource); |
| device->PSGetSamplers(0, 1, &old.PSSampler); |
| device->PSGetShader(&old.PS); |
| device->VSGetShader(&old.VS); |
| device->VSGetConstantBuffers(0, 1, &old.VSConstantBuffer); |
| device->GSGetShader(&old.GS); |
| device->IAGetPrimitiveTopology(&old.PrimitiveTopology); |
| device->IAGetIndexBuffer(&old.IndexBuffer, &old.IndexBufferFormat, &old.IndexBufferOffset); |
| device->IAGetVertexBuffers(0, 1, &old.VertexBuffer, &old.VertexBufferStride, &old.VertexBufferOffset); |
| device->IAGetInputLayout(&old.InputLayout); |
|
|
| |
| ImGui_ImplDX10_SetupRenderState(draw_data, device); |
| |
| ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO(); |
| ImGui_ImplDX10_RenderState render_state; |
| render_state.Device = bd->pd3dDevice; |
| render_state.SamplerDefault = bd->pFontSampler; |
| render_state.VertexConstantBuffer = bd->pVertexConstantBuffer; |
| platform_io.Renderer_RenderState = &render_state; |
|
|
| |
| |
| int global_vtx_offset = 0; |
| int global_idx_offset = 0; |
| ImVec2 clip_off = draw_data->DisplayPos; |
| for (int n = 0; n < draw_data->CmdListsCount; n++) |
| { |
| const ImDrawList* draw_list = draw_data->CmdLists[n]; |
| for (int cmd_i = 0; cmd_i < draw_list->CmdBuffer.Size; cmd_i++) |
| { |
| const ImDrawCmd* pcmd = &draw_list->CmdBuffer[cmd_i]; |
| if (pcmd->UserCallback != nullptr) |
| { |
| |
| |
| if (pcmd->UserCallback == ImDrawCallback_ResetRenderState) |
| ImGui_ImplDX10_SetupRenderState(draw_data, device); |
| else |
| pcmd->UserCallback(draw_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 D3D10_RECT r = { (LONG)clip_min.x, (LONG)clip_min.y, (LONG)clip_max.x, (LONG)clip_max.y }; |
| device->RSSetScissorRects(1, &r); |
|
|
| |
| ID3D10ShaderResourceView* texture_srv = (ID3D10ShaderResourceView*)pcmd->GetTexID(); |
| device->PSSetShaderResources(0, 1, &texture_srv); |
| device->DrawIndexed(pcmd->ElemCount, pcmd->IdxOffset + global_idx_offset, pcmd->VtxOffset + global_vtx_offset); |
| } |
| } |
| global_idx_offset += draw_list->IdxBuffer.Size; |
| global_vtx_offset += draw_list->VtxBuffer.Size; |
| } |
| platform_io.Renderer_RenderState = nullptr; |
|
|
| |
| device->RSSetScissorRects(old.ScissorRectsCount, old.ScissorRects); |
| device->RSSetViewports(old.ViewportsCount, old.Viewports); |
| device->RSSetState(old.RS); if (old.RS) old.RS->Release(); |
| device->OMSetBlendState(old.BlendState, old.BlendFactor, old.SampleMask); if (old.BlendState) old.BlendState->Release(); |
| device->OMSetDepthStencilState(old.DepthStencilState, old.StencilRef); if (old.DepthStencilState) old.DepthStencilState->Release(); |
| device->PSSetShaderResources(0, 1, &old.PSShaderResource); if (old.PSShaderResource) old.PSShaderResource->Release(); |
| device->PSSetSamplers(0, 1, &old.PSSampler); if (old.PSSampler) old.PSSampler->Release(); |
| device->PSSetShader(old.PS); if (old.PS) old.PS->Release(); |
| device->VSSetShader(old.VS); if (old.VS) old.VS->Release(); |
| device->GSSetShader(old.GS); if (old.GS) old.GS->Release(); |
| device->VSSetConstantBuffers(0, 1, &old.VSConstantBuffer); if (old.VSConstantBuffer) old.VSConstantBuffer->Release(); |
| device->IASetPrimitiveTopology(old.PrimitiveTopology); |
| device->IASetIndexBuffer(old.IndexBuffer, old.IndexBufferFormat, old.IndexBufferOffset); if (old.IndexBuffer) old.IndexBuffer->Release(); |
| device->IASetVertexBuffers(0, 1, &old.VertexBuffer, &old.VertexBufferStride, &old.VertexBufferOffset); if (old.VertexBuffer) old.VertexBuffer->Release(); |
| device->IASetInputLayout(old.InputLayout); if (old.InputLayout) old.InputLayout->Release(); |
| } |
|
|
| static void ImGui_ImplDX10_CreateFontsTexture() |
| { |
| |
| ImGui_ImplDX10_Data* bd = ImGui_ImplDX10_GetBackendData(); |
| ImGuiIO& io = ImGui::GetIO(); |
| unsigned char* pixels; |
| int width, height; |
| io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height); |
|
|
| |
| { |
| D3D10_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 = D3D10_USAGE_DEFAULT; |
| desc.BindFlags = D3D10_BIND_SHADER_RESOURCE; |
| desc.CPUAccessFlags = 0; |
|
|
| ID3D10Texture2D* pTexture = nullptr; |
| D3D10_SUBRESOURCE_DATA subResource; |
| subResource.pSysMem = pixels; |
| subResource.SysMemPitch = desc.Width * 4; |
| subResource.SysMemSlicePitch = 0; |
| bd->pd3dDevice->CreateTexture2D(&desc, &subResource, &pTexture); |
| IM_ASSERT(pTexture != nullptr); |
|
|
| |
| D3D10_SHADER_RESOURCE_VIEW_DESC srv_desc; |
| ZeroMemory(&srv_desc, sizeof(srv_desc)); |
| srv_desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; |
| srv_desc.ViewDimension = D3D10_SRV_DIMENSION_TEXTURE2D; |
| srv_desc.Texture2D.MipLevels = desc.MipLevels; |
| srv_desc.Texture2D.MostDetailedMip = 0; |
| bd->pd3dDevice->CreateShaderResourceView(pTexture, &srv_desc, &bd->pFontTextureView); |
| pTexture->Release(); |
| } |
|
|
| |
| io.Fonts->SetTexID((ImTextureID)bd->pFontTextureView); |
| } |
|
|
| static void ImGui_ImplDX10_DestroyFontsTexture() |
| { |
| ImGui_ImplDX10_Data* bd = ImGui_ImplDX10_GetBackendData(); |
| if (bd->pFontTextureView) |
| { |
| bd->pFontTextureView->Release(); |
| bd->pFontTextureView = nullptr; |
| ImGui::GetIO().Fonts->SetTexID(0); |
| } |
| } |
|
|
| bool ImGui_ImplDX10_CreateDeviceObjects() |
| { |
| ImGui_ImplDX10_Data* bd = ImGui_ImplDX10_GetBackendData(); |
| if (!bd->pd3dDevice) |
| return false; |
| if (bd->pFontSampler) |
| ImGui_ImplDX10_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), nullptr, nullptr, nullptr, "main", "vs_4_0", 0, 0, &vertexShaderBlob, nullptr))) |
| return false; |
| if (bd->pd3dDevice->CreateVertexShader(vertexShaderBlob->GetBufferPointer(), vertexShaderBlob->GetBufferSize(), &bd->pVertexShader) != S_OK) |
| { |
| vertexShaderBlob->Release(); |
| return false; |
| } |
|
|
| |
| D3D10_INPUT_ELEMENT_DESC local_layout[] = |
| { |
| { "POSITION", 0, DXGI_FORMAT_R32G32_FLOAT, 0, (UINT)offsetof(ImDrawVert, pos), D3D10_INPUT_PER_VERTEX_DATA, 0 }, |
| { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, (UINT)offsetof(ImDrawVert, uv), D3D10_INPUT_PER_VERTEX_DATA, 0 }, |
| { "COLOR", 0, DXGI_FORMAT_R8G8B8A8_UNORM, 0, (UINT)offsetof(ImDrawVert, col), D3D10_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(); |
|
|
| |
| { |
| D3D10_BUFFER_DESC desc = {}; |
| desc.ByteWidth = sizeof(VERTEX_CONSTANT_BUFFER_DX10); |
| desc.Usage = D3D10_USAGE_DYNAMIC; |
| desc.BindFlags = D3D10_BIND_CONSTANT_BUFFER; |
| desc.CPUAccessFlags = D3D10_CPU_ACCESS_WRITE; |
| desc.MiscFlags = 0; |
| bd->pd3dDevice->CreateBuffer(&desc, nullptr, &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), nullptr, nullptr, nullptr, "main", "ps_4_0", 0, 0, &pixelShaderBlob, nullptr))) |
| return false; |
| if (bd->pd3dDevice->CreatePixelShader(pixelShaderBlob->GetBufferPointer(), pixelShaderBlob->GetBufferSize(), &bd->pPixelShader) != S_OK) |
| { |
| pixelShaderBlob->Release(); |
| return false; |
| } |
| pixelShaderBlob->Release(); |
| } |
|
|
| |
| { |
| D3D10_BLEND_DESC desc; |
| ZeroMemory(&desc, sizeof(desc)); |
| desc.AlphaToCoverageEnable = false; |
| desc.BlendEnable[0] = true; |
| desc.SrcBlend = D3D10_BLEND_SRC_ALPHA; |
| desc.DestBlend = D3D10_BLEND_INV_SRC_ALPHA; |
| desc.BlendOp = D3D10_BLEND_OP_ADD; |
| desc.SrcBlendAlpha = D3D10_BLEND_ONE; |
| desc.DestBlendAlpha = D3D10_BLEND_INV_SRC_ALPHA; |
| desc.BlendOpAlpha = D3D10_BLEND_OP_ADD; |
| desc.RenderTargetWriteMask[0] = D3D10_COLOR_WRITE_ENABLE_ALL; |
| bd->pd3dDevice->CreateBlendState(&desc, &bd->pBlendState); |
| } |
|
|
| |
| { |
| D3D10_RASTERIZER_DESC desc; |
| ZeroMemory(&desc, sizeof(desc)); |
| desc.FillMode = D3D10_FILL_SOLID; |
| desc.CullMode = D3D10_CULL_NONE; |
| desc.ScissorEnable = true; |
| desc.DepthClipEnable = true; |
| bd->pd3dDevice->CreateRasterizerState(&desc, &bd->pRasterizerState); |
| } |
|
|
| |
| { |
| D3D10_DEPTH_STENCIL_DESC desc; |
| ZeroMemory(&desc, sizeof(desc)); |
| desc.DepthEnable = false; |
| desc.DepthWriteMask = D3D10_DEPTH_WRITE_MASK_ALL; |
| desc.DepthFunc = D3D10_COMPARISON_ALWAYS; |
| desc.StencilEnable = false; |
| desc.FrontFace.StencilFailOp = desc.FrontFace.StencilDepthFailOp = desc.FrontFace.StencilPassOp = D3D10_STENCIL_OP_KEEP; |
| desc.FrontFace.StencilFunc = D3D10_COMPARISON_ALWAYS; |
| desc.BackFace = desc.FrontFace; |
| bd->pd3dDevice->CreateDepthStencilState(&desc, &bd->pDepthStencilState); |
| } |
|
|
| |
| |
| { |
| D3D10_SAMPLER_DESC desc; |
| ZeroMemory(&desc, sizeof(desc)); |
| desc.Filter = D3D10_FILTER_MIN_MAG_MIP_LINEAR; |
| desc.AddressU = D3D10_TEXTURE_ADDRESS_CLAMP; |
| desc.AddressV = D3D10_TEXTURE_ADDRESS_CLAMP; |
| desc.AddressW = D3D10_TEXTURE_ADDRESS_CLAMP; |
| desc.MipLODBias = 0.f; |
| desc.ComparisonFunc = D3D10_COMPARISON_ALWAYS; |
| desc.MinLOD = 0.f; |
| desc.MaxLOD = 0.f; |
| bd->pd3dDevice->CreateSamplerState(&desc, &bd->pFontSampler); |
| } |
|
|
| ImGui_ImplDX10_CreateFontsTexture(); |
|
|
| return true; |
| } |
|
|
| void ImGui_ImplDX10_InvalidateDeviceObjects() |
| { |
| ImGui_ImplDX10_Data* bd = ImGui_ImplDX10_GetBackendData(); |
| if (!bd->pd3dDevice) |
| return; |
|
|
| ImGui_ImplDX10_DestroyFontsTexture(); |
|
|
| if (bd->pFontSampler) { bd->pFontSampler->Release(); bd->pFontSampler = nullptr; } |
| if (bd->pIB) { bd->pIB->Release(); bd->pIB = nullptr; } |
| if (bd->pVB) { bd->pVB->Release(); bd->pVB = nullptr; } |
| if (bd->pBlendState) { bd->pBlendState->Release(); bd->pBlendState = nullptr; } |
| if (bd->pDepthStencilState) { bd->pDepthStencilState->Release(); bd->pDepthStencilState = nullptr; } |
| if (bd->pRasterizerState) { bd->pRasterizerState->Release(); bd->pRasterizerState = nullptr; } |
| if (bd->pPixelShader) { bd->pPixelShader->Release(); bd->pPixelShader = nullptr; } |
| if (bd->pVertexConstantBuffer) { bd->pVertexConstantBuffer->Release(); bd->pVertexConstantBuffer = nullptr; } |
| if (bd->pInputLayout) { bd->pInputLayout->Release(); bd->pInputLayout = nullptr; } |
| if (bd->pVertexShader) { bd->pVertexShader->Release(); bd->pVertexShader = nullptr; } |
| } |
|
|
| bool ImGui_ImplDX10_Init(ID3D10Device* device) |
| { |
| ImGuiIO& io = ImGui::GetIO(); |
| IMGUI_CHECKVERSION(); |
| IM_ASSERT(io.BackendRendererUserData == nullptr && "Already initialized a renderer backend!"); |
|
|
| |
| ImGui_ImplDX10_Data* bd = IM_NEW(ImGui_ImplDX10_Data)(); |
| io.BackendRendererUserData = (void*)bd; |
| io.BackendRendererName = "imgui_impl_dx10"; |
| io.BackendFlags |= ImGuiBackendFlags_RendererHasVtxOffset; |
|
|
| |
| IDXGIDevice* pDXGIDevice = nullptr; |
| IDXGIAdapter* pDXGIAdapter = nullptr; |
| IDXGIFactory* pFactory = nullptr; |
| 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->pFactory = pFactory; |
| } |
| if (pDXGIDevice) pDXGIDevice->Release(); |
| if (pDXGIAdapter) pDXGIAdapter->Release(); |
| bd->pd3dDevice->AddRef(); |
|
|
| return true; |
| } |
|
|
| void ImGui_ImplDX10_Shutdown() |
| { |
| ImGui_ImplDX10_Data* bd = ImGui_ImplDX10_GetBackendData(); |
| IM_ASSERT(bd != nullptr && "No renderer backend to shutdown, or already shutdown?"); |
| ImGuiIO& io = ImGui::GetIO(); |
|
|
| ImGui_ImplDX10_InvalidateDeviceObjects(); |
| if (bd->pFactory) { bd->pFactory->Release(); } |
| if (bd->pd3dDevice) { bd->pd3dDevice->Release(); } |
| io.BackendRendererName = nullptr; |
| io.BackendRendererUserData = nullptr; |
| io.BackendFlags &= ~ImGuiBackendFlags_RendererHasVtxOffset; |
| IM_DELETE(bd); |
| } |
|
|
| void ImGui_ImplDX10_NewFrame() |
| { |
| ImGui_ImplDX10_Data* bd = ImGui_ImplDX10_GetBackendData(); |
| IM_ASSERT(bd != nullptr && "Context or backend not initialized! Did you call ImGui_ImplDX10_Init()?"); |
|
|
| if (!bd->pVertexShader) |
| ImGui_ImplDX10_CreateDeviceObjects(); |
| } |
|
|
| |
|
|
| #endif |
|
|