Commit 632e6d68 authored by Chuck Walbourn's avatar Chuck Walbourn
Browse files

TextConsole updated to support DX12

parent 07b0500f
Loading
Loading
Loading
Loading
+206 −53
Original line number Diff line number Diff line
@@ -29,6 +29,23 @@ TextConsole::TextConsole()
}


#if defined(__d3d12_h__) || defined(__d3d12_x_h__)
_Use_decl_annotations_
TextConsole::TextConsole(
    ID3D12Device* device,
    ResourceUploadBatch& upload,
    const RenderTargetState& rtState,
    const wchar_t* fontName,
    D3D12_CPU_DESCRIPTOR_HANDLE cpuDescriptor, D3D12_GPU_DESCRIPTOR_HANDLE gpuDescriptor)
    : m_foregroundColor(1.f, 1.f, 1.f, 1.f),
    m_debugOutput(false)
{
    RestoreDevice(device, upload, rtState, fontName, cpuDescriptor, gpuDescriptor);

    Clear();
}
#else
_Use_decl_annotations_
TextConsole::TextConsole(ID3D11DeviceContext* context, const wchar_t* fontName)
    : m_foregroundColor(1.f, 1.f, 1.f, 1.f),
    m_debugOutput(false)
@@ -37,9 +54,14 @@ TextConsole::TextConsole(ID3D11DeviceContext* context, const wchar_t* fontName)

    Clear();
}
#endif


#if defined(__d3d12_h__) || defined(__d3d12_x_h__)
void TextConsole::Render(_In_ ID3D12GraphicsCommandList* commandList)
#else
void TextConsole::Render()
#endif
{
    std::lock_guard<std::mutex> lock(m_mutex);

@@ -50,7 +72,11 @@ void TextConsole::Render()
    
    XMVECTOR foregroundColor = XMLoadFloat4(&m_foregroundColor);

#if defined(__d3d12_h__) || defined(__d3d12_x_h__)
    m_batch->Begin(commandList);
#else
    m_batch->Begin();
#endif

    unsigned int textLine = unsigned int(m_currentLine + 1) % m_rows;
    
@@ -240,10 +266,41 @@ void TextConsole::ReleaseDevice()
{
    m_batch.reset();
    m_font.reset();
#if defined(__d3d11_h__) || defined(__d3d11_x_h__)
    m_context.Reset();
#endif
}


#if defined(__d3d12_h__) || defined(__d3d12_x_h__)
_Use_decl_annotations_
void TextConsole::RestoreDevice(
    ID3D12Device* device,
    ResourceUploadBatch& upload,
    const RenderTargetState& rtState,
    const wchar_t* fontName,
    D3D12_CPU_DESCRIPTOR_HANDLE cpuDescriptor, D3D12_GPU_DESCRIPTOR_HANDLE gpuDescriptor)

{
    {
        SpriteBatchPipelineStateDescription pd(rtState);
        m_batch = std::make_unique<SpriteBatch>(device, upload, pd);
    }

    m_font = std::make_unique<SpriteFont>(device, upload, fontName, cpuDescriptor, gpuDescriptor);

    m_font->SetDefaultCharacter(L' ');
}


void TextConsole::SetViewport(const D3D12_VIEWPORT& viewPort)
{
    if (m_batch)
    {
        m_batch->SetViewport(viewPort);
    }
}
#else
void TextConsole::RestoreDevice(ID3D11DeviceContext* context, const wchar_t* fontName)
{
    m_context = context;
@@ -266,6 +323,7 @@ void TextConsole::SetViewport(const D3D11_VIEWPORT& viewPort)
        m_batch->SetViewport(viewPort);
    }
}
#endif


void TextConsole::SetRotation(DXGI_MODE_ROTATION rotation)
@@ -277,6 +335,7 @@ void TextConsole::SetRotation(DXGI_MODE_ROTATION rotation)
}


_Use_decl_annotations_
void TextConsole::ProcessString(FXMVECTOR color, const wchar_t* str)
{
    if (!m_lines)
@@ -338,19 +397,61 @@ void TextConsole::IncrementLine()


//--------------------------------------------------------------------------------------
#if defined(__d3d12_h__) || defined(__d3d12_x_h__)
TextConsoleImage::TextConsoleImage() :
    TextConsole(),
    m_bgGpuDescriptor{},
    m_bgSize{}
{
}
#else
TextConsoleImage::TextConsoleImage() :
    TextConsole()
{
}
#endif


#if defined(__d3d12_h__) || defined(__d3d12_x_h__)
_Use_decl_annotations_
TextConsoleImage::TextConsoleImage(
    ID3D12Device* device,
    ResourceUploadBatch& upload,
    const RenderTargetState& rtState,
    const wchar_t* fontName,
    const wchar_t* image,
    D3D12_CPU_DESCRIPTOR_HANDLE cpuDescriptorFont, D3D12_GPU_DESCRIPTOR_HANDLE gpuDescriptorFont,
    D3D12_CPU_DESCRIPTOR_HANDLE cpuDescriptorImage, D3D12_GPU_DESCRIPTOR_HANDLE gpuDescriptorImage) :
    TextConsole(),
    m_bgGpuDescriptor{},
    m_bgSize{}
{
    RestoreDevice(device, upload, rtState, fontName, image,
        cpuDescriptorFont, gpuDescriptorFont,
        cpuDescriptorImage, gpuDescriptorImage);
}
#else
_Use_decl_annotations_
TextConsoleImage::TextConsoleImage(ID3D11DeviceContext* context, const wchar_t* fontName, const wchar_t* image) :
    TextConsole()
{
    RestoreDevice(context, fontName, image);
}
#endif


#if defined(__d3d12_h__) || defined(__d3d12_x_h__)
void TextConsoleImage::Render(_In_ ID3D12GraphicsCommandList* commandList)
{
    m_batch->Begin(commandList);

    m_batch->Draw(m_bgGpuDescriptor, m_bgSize, m_fullscreen);

    m_batch->End();

    TextConsole::Render(commandList);
}
#else
void TextConsoleImage::Render()
{
    m_batch->Begin();
@@ -361,6 +462,7 @@ void TextConsoleImage::Render()

    TextConsole::Render();
}
#endif


void TextConsoleImage::SetWindow(const RECT& fullscreen, bool useSafeRect)
@@ -381,8 +483,14 @@ void TextConsoleImage::SetWindow(const RECT& fullscreen, bool useSafeRect)
    UINT width = std::max<UINT>(fullscreen.right - fullscreen.left, 1);
    UINT height = std::max<UINT>(fullscreen.bottom - fullscreen.top, 1);

#if defined(__d3d12_h__) || defined(__d3d12_x_h__)
    D3D12_VIEWPORT vp = { 0.0f, 0.0f, static_cast<float>(width), static_cast<float>(height),
        D3D12_DEFAULT_VIEWPORT_MIN_DEPTH, D3D12_DEFAULT_VIEWPORT_MAX_DEPTH };
    m_batch->SetViewport(vp);
#else
    auto vp = CD3D11_VIEWPORT(0.0f, 0.0f, static_cast<float>(width), static_cast<float>(height));
    m_batch->SetViewport(vp);
#endif
}


@@ -394,6 +502,50 @@ void TextConsoleImage::ReleaseDevice()
}


#if defined(__d3d12_h__) || defined(__d3d12_x_h__)
_Use_decl_annotations_
void TextConsoleImage::RestoreDevice(
    ID3D12Device* device,
    DirectX::ResourceUploadBatch& upload,
    const DirectX::RenderTargetState& rtState,
    const wchar_t* fontName,
    const wchar_t* image,
    D3D12_CPU_DESCRIPTOR_HANDLE cpuDescriptorFont, D3D12_GPU_DESCRIPTOR_HANDLE gpuDescriptorFont,
    D3D12_CPU_DESCRIPTOR_HANDLE cpuDescriptorImage, D3D12_GPU_DESCRIPTOR_HANDLE gpuDescriptorImage)
{
    TextConsole::RestoreDevice(device, upload, rtState, fontName, cpuDescriptorFont, gpuDescriptorFont);

    wchar_t ext[_MAX_EXT];
    _wsplitpath_s(image, nullptr, 0, nullptr, 0, nullptr, 0, ext, _MAX_EXT);

    if (_wcsicmp(ext, L".dds") == 0)
    {
        DX::ThrowIfFailed(CreateDDSTextureFromFile(device, upload, image, m_background.ReleaseAndGetAddressOf()));
    }
    else
    {
        DX::ThrowIfFailed(CreateWICTextureFromFile(device, upload, image, m_background.ReleaseAndGetAddressOf()));
    }

    auto desc = m_background->GetDesc();
    if (desc.Dimension != D3D12_RESOURCE_DIMENSION_TEXTURE2D)
    {
        throw std::exception("Only supports 2D images");
    }

    D3D12_SHADER_RESOURCE_VIEW_DESC SRVDesc = {};
    SRVDesc.Format = desc.Format;
    SRVDesc.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING;
    SRVDesc.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2D;
    SRVDesc.Texture2D.MipLevels = (!desc.MipLevels) ? -1 : desc.MipLevels;
    
    device->CreateShaderResourceView(m_background.Get(), &SRVDesc, cpuDescriptorImage);

    m_bgGpuDescriptor = gpuDescriptorImage;
    m_bgSize = XMUINT2(static_cast<uint32_t>(desc.Width), desc.Height);
}
#else
_Use_decl_annotations_
void TextConsoleImage::RestoreDevice(ID3D11DeviceContext* context, const wchar_t* fontName, const wchar_t* image)
{
    TextConsole::RestoreDevice(context, fontName);
@@ -413,3 +565,4 @@ void TextConsoleImage::RestoreDevice(ID3D11DeviceContext* context, const wchar_t
        DX::ThrowIfFailed(CreateWICTextureFromFile(device.Get(), image, nullptr, m_background.ReleaseAndGetAddressOf()));
    }
}
#endif
+85 −20
Original line number Diff line number Diff line
@@ -25,7 +25,18 @@ namespace DX
    {
    public:
        TextConsole();
        TextConsole(ID3D11DeviceContext* context, const wchar_t* fontName);
#if defined(__d3d12_h__) || defined(__d3d12_x_h__)
        TextConsole(
            _In_ ID3D12Device* device,
            DirectX::ResourceUploadBatch& upload,
            const DirectX::RenderTargetState& rtState,
            _In_z_ const wchar_t* fontName,
            D3D12_CPU_DESCRIPTOR_HANDLE cpuDescriptor, D3D12_GPU_DESCRIPTOR_HANDLE gpuDescriptor);
#elif defined(__d3d11_h__) || defined(__d3d11_x_h__)
        TextConsole(_In_ ID3D11DeviceContext* context, _In_z_ const wchar_t* fontName);
#else
#   error Please #include <d3d11.h> or <d3d12.h>
#endif

        TextConsole(TextConsole&&) = default;
        TextConsole& operator= (TextConsole&&) = default;
@@ -33,7 +44,11 @@ namespace DX
        TextConsole(TextConsole const&) = delete;
        TextConsole& operator= (TextConsole const&) = delete;

#if defined(__d3d12_h__) || defined(__d3d12_x_h__)
        void Render(_In_ ID3D12GraphicsCommandList* commandList);
#else
        void Render();
#endif

        void Clear();

@@ -53,15 +68,26 @@ namespace DX
        void SetDebugOutput(bool debug) { m_debugOutput = debug; }

        void ReleaseDevice();
#if defined(__d3d12_h__) || defined(__d3d12_x_h__)
        void RestoreDevice(
            _In_ ID3D12Device* device,
            DirectX::ResourceUploadBatch& upload,
            const DirectX::RenderTargetState& rtState,
            _In_z_ const wchar_t* fontName,
            D3D12_CPU_DESCRIPTOR_HANDLE cpuDescriptor, D3D12_GPU_DESCRIPTOR_HANDLE gpuDescriptor);

        void SetViewport(const D3D12_VIEWPORT& viewPort);
#else
        void RestoreDevice(ID3D11DeviceContext* context, const wchar_t* fontName);

        void SetViewport(const D3D11_VIEWPORT& viewPort);
#endif

        void SetRotation(DXGI_MODE_ROTATION rotation);

    protected:
        void XM_CALLCONV FormatImpl(DirectX::FXMVECTOR color, _In_z_ _Printf_format_string_ const wchar_t* strFormat, va_list args);
        void XM_CALLCONV ProcessString(DirectX::FXMVECTOR color, const wchar_t* str);
        void XM_CALLCONV ProcessString(DirectX::FXMVECTOR color, _In_z_ const wchar_t* str);
        void IncrementLine();

        struct Line
@@ -92,7 +118,9 @@ namespace DX

        std::unique_ptr<DirectX::SpriteBatch>           m_batch;
        std::unique_ptr<DirectX::SpriteFont>            m_font;
#if defined(__d3d11_h__) || defined(__d3d11_x_h__)
        Microsoft::WRL::ComPtr<ID3D11DeviceContext>     m_context;
#endif

        std::mutex                                      m_mutex;
    };
@@ -101,7 +129,18 @@ namespace DX
    {
    public:
        TextConsoleImage();
        TextConsoleImage(ID3D11DeviceContext* context, const wchar_t* fontName, const wchar_t* image);
#if defined(__d3d12_h__) || defined(__d3d12_x_h__)
        TextConsoleImage(
            _In_ ID3D12Device* device,
            DirectX::ResourceUploadBatch& upload,
            const DirectX::RenderTargetState& rtState,
            _In_z_ const wchar_t* fontName,
            _In_z_ const wchar_t* image,
            D3D12_CPU_DESCRIPTOR_HANDLE cpuDescriptorFont, D3D12_GPU_DESCRIPTOR_HANDLE gpuDescriptorFont,
            D3D12_CPU_DESCRIPTOR_HANDLE cpuDescriptorImage, D3D12_GPU_DESCRIPTOR_HANDLE gpuDescriptorImage);
#else
        TextConsoleImage(_In_ ID3D11DeviceContext* context, _In_z_ const wchar_t* fontName, _In_z_ const wchar_t* image);
#endif

        TextConsoleImage(TextConsoleImage&&) = default;
        TextConsoleImage& operator= (TextConsoleImage&&) = default;
@@ -109,18 +148,44 @@ namespace DX
        TextConsoleImage(TextConsoleImage const&) = delete;
        TextConsoleImage& operator= (TextConsoleImage const&) = delete;


#if defined(__d3d12_h__) || defined(__d3d12_x_h__)
        void Render(_In_ ID3D12GraphicsCommandList* commandList);
#else
        void Render();
#endif

        void SetWindow(const RECT& layout) = delete;
        void SetWindow(const RECT& fullscreen, bool useSafeRect);

        void ReleaseDevice();
        void RestoreDevice(ID3D11DeviceContext* context, const wchar_t* fontName) = delete;
        void RestoreDevice(ID3D11DeviceContext* context, const wchar_t* fontName, const wchar_t* image);
#if defined(__d3d12_h__) || defined(__d3d12_x_h__)
        void RestoreDevice(
            _In_ ID3D12Device* device,
            DirectX::ResourceUploadBatch& upload,
            const DirectX::RenderTargetState& rtState,
            _In_z_ const wchar_t* fontName,
            D3D12_CPU_DESCRIPTOR_HANDLE cpuDescriptor, D3D12_GPU_DESCRIPTOR_HANDLE gpuDescriptor) = delete;
        void RestoreDevice(
            _In_ ID3D12Device* device,
            DirectX::ResourceUploadBatch& upload,
            const DirectX::RenderTargetState& rtState,
            _In_z_ const wchar_t* fontName,
            _In_z_ const wchar_t* image,
            D3D12_CPU_DESCRIPTOR_HANDLE cpuDescriptorFont, D3D12_GPU_DESCRIPTOR_HANDLE gpuDescriptorFont,
            D3D12_CPU_DESCRIPTOR_HANDLE cpuDescriptorImage, D3D12_GPU_DESCRIPTOR_HANDLE gpuDescriptorImage);
#else
        void RestoreDevice(_In_ ID3D11DeviceContext* context, _In_z_ const wchar_t* fontName) = delete;
        void RestoreDevice(_In_ ID3D11DeviceContext* context, _In_z_ const wchar_t* fontName, _In_z_ const wchar_t* image);
#endif

    private:
#if defined(__d3d12_h__) || defined(__d3d12_x_h__)
        D3D12_GPU_DESCRIPTOR_HANDLE                         m_bgGpuDescriptor;
        DirectX::XMUINT2                                    m_bgSize;
        Microsoft::WRL::ComPtr<ID3D12Resource>              m_background;
#else
        Microsoft::WRL::ComPtr<ID3D11ShaderResourceView>    m_background;
#endif
        RECT                                                m_fullscreen;
    };
}
 No newline at end of file