C++ Вопрос DirectX Toolkit не рисует в игре

Новичок
Статус
Оффлайн
Регистрация
25 Дек 2018
Сообщения
2
Реакции[?]
1
Поинты[?]
0
Использую DiectX11 TK, сделал сохранения состояния - в тестовой среде по типу GFX выводит и текст, и треугольник. А вот если инжектить в игру - почему то выводит только текст, не совсем понятно в чем дело, никто не сталкивался с такой или подобной проблемой?



C++:
HRESULT __stdcall Base::Hooks::Present(IDXGISwapChain* pSwapChain, UINT SyncInterval, UINT Flags)
{
    if (!Data::InitImGui)
    {
        if (SUCCEEDED(pSwapChain->GetDevice(__uuidof(ID3D11Device), (void**)&Data::pDevice)))
        {
            Data::pDevice->GetImmediateContext(&Data::pContext);
            DXGI_SWAP_CHAIN_DESC sd;
            pSwapChain->GetDesc(&sd);
            Data::window = sd.OutputWindow;
            ID3D11Texture2D* pBackBuffer = NULL;
            pSwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&pBackBuffer);


            if (pBackBuffer == NULL)
                return Data::oPresent(pSwapChain, SyncInterval, Flags);

            Data::pDevice->CreateRenderTargetView(pBackBuffer, NULL, &Data::mainRenderTargetView);
            pBackBuffer->Release();
            Data::oWndProc = (WNDPROC)SetWindowLongPtr(Data::window, GWLP_WNDPROC, (LONG_PTR)WndProc);


            ImGui::CreateContext();
            ImGuiIO& io = ImGui::GetIO();
            io.ConfigFlags = ImGuiConfigFlags_NoMouseCursorChange;
            ImGui_ImplWin32_Init(Base::Data::window);
            ImGui_ImplDX11_Init(Base::Data::pDevice, Base::Data::pContext);
            SDK.Init(pSwapChain);
            Data::InitImGui = true;
        }

        else
            return Data::oPresent(pSwapChain, SyncInterval, Flags);
    }
    ImGui_ImplDX11_NewFrame();
    ImGui_ImplWin32_NewFrame();
    ImGui::NewFrame();

    if (Data::ShowMenu)
    {
        SDK.Menu();
    }

    ImGui::Render();

    Data::pContext->OMSetRenderTargets(1, &Data::mainRenderTargetView, NULL);
    ImGui_ImplDX11_RenderDrawData(ImGui::GetDrawData());
    SDK.DXBegin();
    {
        DirectX::VertexPositionColor v1({ 0.f, 0.5f, 0.5f }, DirectX::Colors::Yellow);
        DirectX::VertexPositionColor v2({ 0.5f, -0.5f, 0.5f }, DirectX::Colors::Yellow);
        DirectX::VertexPositionColor v3({ -0.5f, -0.5f, 0.5f }, DirectX::Colors::Yellow);

        Data::primitiveBatch->DrawTriangle(v1, v2, v3);
        SDK.DrawString(L"Drawing", { 10,10 }, { 255,0,0,255 }, 1);
    }
    SDK.DXEnd();
    return Data::oPresent(pSwapChain, SyncInterval, Flags);
}


void OnyxSDK::DXBegin() {
    if (!isInitializated) return;
    restoreState = FALSE;
    stateSaver->saveCurrentState(Base::Data::pContext);
    restoreState = TRUE;
    Base::Data::spriteBatch->Begin(DirectX::SpriteSortMode_Deferred);
    Base::Data::primitiveBatch->Begin();
    Base::Data::pContext->IASetInputLayout(InputLayout);
}
void OnyxSDK::DXEnd(){
    if (!isInitializated) return;
    Base::Data::primitiveBatch->End();
    Base::Data::spriteBatch->End();
    if (restoreState) stateSaver->restoreSavedState();
}


C++:
#include <pch.h>
#include <DXTK/state.h>

DXTKStateSaver::DXTKStateSaver() : m_savedState(false), m_featureLevel(D3D_FEATURE_LEVEL_11_0), m_pContext(NULL), m_primitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_UNDEFINED), m_pInputLayout(NULL), m_pBlendState(NULL),
m_sampleMask(0xFFFFFFFF), m_pDepthStencilState(NULL), m_stencilRef(0), m_pRasterizerState(NULL), m_pPSSRV(NULL), m_pSamplerState(NULL), m_pVS(NULL), m_numVSClassInstances(0), m_pVSConstantBuffer(NULL), m_pGS(NULL),
m_numGSClassInstances(0), m_pGSConstantBuffer(NULL), m_pGSSRV(NULL), m_pPS(NULL), m_numPSClassInstances(0), m_pHS(NULL), m_numHSClassInstances(0), m_pDS(NULL), m_numDSClassInstances(0), m_pVB(NULL), m_vertexStride(0),
m_vertexOffset(0), m_pIndexBuffer(NULL), m_indexFormat(DXGI_FORMAT_UNKNOWN), m_indexOffset(0)
{
    for (int i = 0; i < 4; ++i)
    {
        m_blendFactor[i] = 0.0f;
    }
    for (int i = 0; i < 256; ++i)
    {
        m_pVSClassInstances[i] = NULL;
        m_pGSClassInstances[i] = NULL;
        m_pPSClassInstances[i] = NULL;
        m_pHSClassInstances[i] = NULL;
        m_pDSClassInstances[i] = NULL;
    }
}

DXTKStateSaver::~DXTKStateSaver()
{
    releaseSavedState();
}

HRESULT DXTKStateSaver::saveCurrentState(ID3D11DeviceContext* pContext)
{
    if (m_savedState) releaseSavedState();
    if (pContext == NULL) return E_INVALIDARG;

    ID3D11Device* pDevice;
    pContext->GetDevice(&pDevice);
    if (pDevice != NULL)
    {
        m_featureLevel = pDevice->GetFeatureLevel();
        pDevice->Release();
    }

    pContext->AddRef();
    m_pContext = pContext;
    m_pContext->IAGetPrimitiveTopology(&m_primitiveTopology);
    m_pContext->IAGetInputLayout(&m_pInputLayout);
    m_pContext->OMGetBlendState(&m_pBlendState, m_blendFactor, &m_sampleMask);
    m_pContext->OMGetDepthStencilState(&m_pDepthStencilState, &m_stencilRef);
    m_pContext->RSGetState(&m_pRasterizerState);
    m_numVSClassInstances = 256;
    m_pContext->VSGetShader(&m_pVS, m_pVSClassInstances, &m_numVSClassInstances);
    m_pContext->VSGetConstantBuffers(0, 1, &m_pVSConstantBuffer);
    m_numPSClassInstances = 256;
    m_pContext->PSGetShader(&m_pPS, m_pPSClassInstances, &m_numPSClassInstances);
    m_pContext->PSGetShaderResources(0, 1, &m_pPSSRV);
    pContext->PSGetSamplers(0, 1, &m_pSamplerState);

    if (m_featureLevel >= D3D_FEATURE_LEVEL_10_0)
    {
        m_numGSClassInstances = 256;
        m_pContext->GSGetShader(&m_pGS, m_pGSClassInstances, &m_numGSClassInstances);
        m_pContext->GSGetConstantBuffers(0, 1, &m_pGSConstantBuffer);

        m_pContext->GSGetShaderResources(0, 1, &m_pGSSRV);

        if (m_featureLevel >= D3D_FEATURE_LEVEL_11_0)
        {
            m_numHSClassInstances = 256;
            m_pContext->HSGetShader(&m_pHS, m_pHSClassInstances, &m_numHSClassInstances);

            m_numDSClassInstances = 256;
            m_pContext->DSGetShader(&m_pDS, m_pDSClassInstances, &m_numDSClassInstances);
        }
    }

    m_pContext->IAGetVertexBuffers(0, 1, &m_pVB, &m_vertexStride, &m_vertexOffset);
    m_pContext->IAGetIndexBuffer(&m_pIndexBuffer, &m_indexFormat, &m_indexOffset);
    m_savedState = true;

    return S_OK;
}

HRESULT DXTKStateSaver::restoreSavedState()
{
    if (!m_savedState) return E_FAIL;

    m_pContext->IASetPrimitiveTopology(m_primitiveTopology);
    m_pContext->IASetInputLayout(m_pInputLayout);

    m_pContext->OMSetBlendState(m_pBlendState, m_blendFactor, m_sampleMask);
    m_pContext->OMSetDepthStencilState(m_pDepthStencilState, m_stencilRef);

    m_pContext->RSSetState(m_pRasterizerState);

    m_pContext->VSSetShader(m_pVS, m_pVSClassInstances, m_numVSClassInstances);
    m_pContext->VSSetConstantBuffers(0, 1, &m_pVSConstantBuffer);

    m_pContext->PSSetShader(m_pPS, m_pPSClassInstances, m_numPSClassInstances);
    m_pContext->PSSetShaderResources(0, 1, &m_pPSSRV);
    m_pContext->PSSetSamplers(0, 1, &m_pSamplerState);

    if (m_featureLevel >= D3D_FEATURE_LEVEL_10_0)
    {
        m_pContext->GSSetShader(m_pGS, m_pGSClassInstances, m_numGSClassInstances);
        m_pContext->GSSetConstantBuffers(0, 1, &m_pGSConstantBuffer);

        m_pContext->GSSetShaderResources(0, 1, &m_pGSSRV);

        if (m_featureLevel >= D3D_FEATURE_LEVEL_11_0)
        {
            m_pContext->HSSetShader(m_pHS, m_pHSClassInstances, m_numHSClassInstances);

            m_pContext->DSSetShader(m_pDS, m_pDSClassInstances, m_numDSClassInstances);
        }
    }

    m_pContext->IASetVertexBuffers(0, 1, &m_pVB, &m_vertexStride, &m_vertexOffset);

    m_pContext->IASetIndexBuffer(m_pIndexBuffer, m_indexFormat, m_indexOffset);

    return S_OK;
}

void DXTKStateSaver::ZeroShaders(ID3D11DeviceContext* pContext)
{
    pContext->VSSetShader(NULL, NULL, 0);
    pContext->PSSetShader(NULL, NULL, 0);
    pContext->HSSetShader(NULL, NULL, 0);
    pContext->DSSetShader(NULL, NULL, 0);
    pContext->GSSetShader(NULL, NULL, 0);
}

void DXTKStateSaver::releaseSavedState()
{
    m_primitiveTopology = D3D11_PRIMITIVE_TOPOLOGY_UNDEFINED;
    SAFE_RELEASE(m_pInputLayout);
    SAFE_RELEASE(m_pBlendState);
    for (int i = 0; i < 4; ++i)
        m_blendFactor[i] = 0.0f;
    m_sampleMask = 0xffffffff;
    SAFE_RELEASE(m_pDepthStencilState);
    m_stencilRef = 0;
    SAFE_RELEASE(m_pRasterizerState);
    SAFE_RELEASE(m_pPSSRV);
    SAFE_RELEASE(m_pSamplerState);
    SAFE_RELEASE(m_pVS);
    for (UINT i = 0; i < m_numVSClassInstances; ++i)
        SAFE_RELEASE(m_pVSClassInstances[i]);
    m_numVSClassInstances = 0;
    SAFE_RELEASE(m_pVSConstantBuffer);
    SAFE_RELEASE(m_pGS);
    for (UINT i = 0; i < m_numGSClassInstances; ++i)
        SAFE_RELEASE(m_pGSClassInstances[i]);
    m_numGSClassInstances = 0;
    SAFE_RELEASE(m_pGSConstantBuffer);
    SAFE_RELEASE(m_pGSSRV);
    SAFE_RELEASE(m_pPS);
    for (UINT i = 0; i < m_numPSClassInstances; ++i)
        SAFE_RELEASE(m_pPSClassInstances[i]);
    m_numPSClassInstances = 0;
    SAFE_RELEASE(m_pHS);
    for (UINT i = 0; i < m_numHSClassInstances; ++i)
        SAFE_RELEASE(m_pHSClassInstances[i]);
    m_numHSClassInstances = 0;
    SAFE_RELEASE(m_pDS);
    for (UINT i = 0; i < m_numDSClassInstances; ++i)
        SAFE_RELEASE(m_pDSClassInstances[i]);
    m_numDSClassInstances = 0;
    SAFE_RELEASE(m_pVB);
    m_vertexStride = 0;
    m_vertexOffset = 0;
    SAFE_RELEASE(m_pIndexBuffer);
    m_indexFormat = DXGI_FORMAT_UNKNOWN;
    m_indexOffset = 0;

    SAFE_RELEASE(m_pContext);
    m_featureLevel = D3D_FEATURE_LEVEL_11_0;

    m_savedState = false;
}
 
Новичок
Статус
Оффлайн
Регистрация
25 Дек 2018
Сообщения
2
Реакции[?]
1
Поинты[?]
0
Исправил с помощью адекватной инициации классов primitiveBatch, spriteBatch


C++:
    __try {
        pSwapChain = _pSwapChain;
        void const* shaderByteCode;
        size_t byteCodeLength;
        Base::Data::pContext->IAGetInputLayout(&InputLayout);
        Base::Data::spriteBatch.reset(new DirectX::SpriteBatch(Base::Data::pContext));
        Base::Data::spriteFont.reset(new DirectX::SpriteFont(Base::Data::pDevice, Fonts::Purista, sizeof(Fonts::Purista)));
        Base::Data::primitiveBatch.reset(new DirectX::PrimitiveBatch<DirectX::VertexPositionColor>(Base::Data::pContext));
        Base::Data::commonStates.reset(new DirectX::CommonStates(Base::Data::pDevice));
        Base::Data::basicEffect.reset(new DirectX::BasicEffect(Base::Data::pDevice));
        Base::Data::basicEffect->SetVertexColorEnabled(true);


        HRESULT hr = S_OK;

        if (FAILED(hr)) {
            Base::Detach();
        }

        Base::Data::pDevice->GetImmediateContext(&Base::Data::pContext);

        Base::Data::basicEffect->SetVertexColorEnabled(true);

        Base::Data::basicEffect->GetVertexShaderBytecode(&shaderByteCode, &byteCodeLength);

        hr = Base::Data::pDevice->CreateInputLayout(
            DirectX::VertexPositionColor::InputElements,
            DirectX::VertexPositionColor::InputElementCount,
            shaderByteCode, byteCodeLength,
            &InputLayout
        );

        if (FAILED(hr)) {
            Base::Detach();
        }

        //pSwapChainVFT = *(PDWORD64*)pSwapChain;

        stateSaver = new DXTKStateSaver;

        RECT screen;
        GetWindowRect(Base::Data::window, &screen);
        ScreenX = screen.left;
        ScreenY = screen.top;
        ScreenSX = screen.right - screen.left;
        ScreenSY = screen.bottom - screen.top;
    }
    __except (EXCEPTION_EXECUTE_HANDLER) {
        Base::Detach();
    }
    isInitializated = true;
 
Сверху Снизу