Using d3d11 shared handle in opengl results with black texture

293 views
Skip to first unread message

李欣

unread,
Jul 6, 2021, 9:16:03 AM7/6/21
to angleproject
Hi all: 
      I'm a new  hand in angle. I'm build a demo by this steps;

step 1: build a d3d texture 
##########################
// Create D3D11 Device and Context
    ID3D11Device1 *d3d11Device;
    ID3D11DeviceContext1 *d3d11DeviceContext;
    {
        ID3D11Device *baseDevice;
        ID3D11DeviceContext *baseDeviceContext;
        D3D_FEATURE_LEVEL featureLevels[] = {D3D_FEATURE_LEVEL_11_0};
        UINT creationFlags                = D3D11_CREATE_DEVICE_BGRA_SUPPORT;
#if defined(DEBUG_BUILD)
        creationFlags |= D3D11_CREATE_DEVICE_DEBUG;
#endif

        HRESULT hResult = D3D11CreateDevice(0, D3D_DRIVER_TYPE_HARDWARE, 0, creationFlags,
                                            featureLevels, ARRAYSIZE(featureLevels),
                                            D3D11_SDK_VERSION, &baseDevice, 0, &baseDeviceContext);
        if (FAILED(hResult))
        {
            MessageBoxA(0, "D3D11CreateDevice() failed", "Fatal Error", MB_OK);
            return NULL;
        }

        // Get 1.1 interface of D3D11 Device and Context
        hResult = baseDevice->QueryInterface(__uuidof(ID3D11Device1), (void **)&d3d11Device);
        assert(SUCCEEDED(hResult));
        baseDevice->Release();

        hResult = baseDeviceContext->QueryInterface(__uuidof(ID3D11DeviceContext1),
                                                    (void **)&d3d11DeviceContext);
        assert(SUCCEEDED(hResult));
        baseDeviceContext->Release();
    }

    // Create Texture
    D3D11_TEXTURE2D_DESC textureDesc = {};
    textureDesc.Width                = 1;
    textureDesc.Height               = 1;
    textureDesc.MipLevels            = 1;
    textureDesc.ArraySize            = 1;
    textureDesc.Format               = DXGI_FORMAT_B8G8R8A8_UNORM;
    textureDesc.SampleDesc.Count     = 1;
    textureDesc.SampleDesc.Quality = 0;
    textureDesc.Usage                = D3D11_USAGE_DEFAULT;
    textureDesc.BindFlags = D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE;
    textureDesc.MiscFlags = D3D11_RESOURCE_MISC_SHARED;
    textureDesc.CPUAccessFlags = 0;

     static const uint32_t s_pixel = 0xffc99aff;
    D3D11_SUBRESOURCE_DATA textureSubresourceData = {&s_pixel, sizeof(uint32_t), 0};

    ID3D11Texture2D *texture;
    d3d11Device->CreateTexture2D(&textureDesc, &textureSubresourceData, &texture);
##########################

step2: get a EGL surface 
##########################
 Microsoft::WRL::ComPtr<ID3D11Texture2D> d3dTex = CreateSimpleTexture2DFromD3D();

            Microsoft::WRL::ComPtr<IDXGIResource> dxgiResource;
            HANDLE sharedHandle;
            HRESULT hr = d3dTex.As(&dxgiResource);
            if FAILED (hr)
            {
                // error handling code
            }

            hr = dxgiResource->GetSharedHandle(&sharedHandle);
            if FAILED (hr)
            {
                // error handling code
            }

            EGLSurface surface = EGL_NO_SURFACE;

            EGLint pBufferAttributes[] = {EGL_WIDTH,
                                          1,
                                          EGL_HEIGHT,
                                          1,
                                          EGL_TEXTURE_TARGET,
                                          EGL_TEXTURE_2D,
                                          EGL_TEXTURE_FORMAT,
                                          EGL_TEXTURE_RGBA,
                                          EGL_NONE};



            surface = eglCreatePbufferFromClientBuffer(
                getDisplay(), EGL_D3D_TEXTURE_2D_SHARE_HANDLE_ANGLE, sharedHandle, getConfig(),
                pBufferAttributes);
            std::cout << glGetError() << std::endl;

            if (surface == EGL_NO_SURFACE)
            {
                // error handling code
                std::cout << "error" << std::endl;
            }
##########################

step3:get a gl texture 
##########################
  glGenTextures(1, &mTexture);
            std::cout << glGetError() << std::endl;

            glBindTexture(GL_TEXTURE_2D, mTexture);
            std::cout << glGetError() << std::endl;

            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
            std::cout << glGetError() << std::endl;

            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
            std::cout << glGetError() << std::endl;

            EGLBoolean result = eglBindTexImage(getDisplay(), surface, EGL_BACK_BUFFER);
            if (result == false)
            {
                std::cout << glGetError() << std::endl;
                return false;
            }
   
##########################

step4: render
##########################
 GLfloat vertices[] = {
            -0.5f, 0.5f,  0.0f,  // Position 0
            0.0f,  0.0f,         // TexCoord 0
            -0.5f, -0.5f, 0.0f,  // Position 1
            0.0f,  1.0f,         // TexCoord 1
            0.5f,  -0.5f, 0.0f,  // Position 2
            1.0f,  1.0f,         // TexCoord 2
            0.5f,  0.5f,  0.0f,  // Position 3
            1.0f,  0.0f          // TexCoord 3
        };
        GLushort indices[] = {0, 1, 2, 0, 2, 3};

        // Set the viewport
        glViewport(0, 0, getWindow()->getWidth(), getWindow()->getHeight());
        std::cout << glGetError() << std::endl;

        // Clear the color buffer
        glClear(GL_COLOR_BUFFER_BIT);
        std::cout << glGetError() << std::endl;

        // Load the vertex position
        glVertexPointer(3, GL_FLOAT, 5 * sizeof(GLfloat), vertices);
        std::cout << glGetError() << std::endl;

        // Load the texture coordinate
        glTexCoordPointer(2, GL_FLOAT, 5 * sizeof(GLfloat), vertices + 3);
        std::cout << glGetError() << std::endl;

        glEnableClientState(GL_VERTEX_ARRAY);
        std::cout << glGetError() << std::endl;

        glEnableClientState(GL_TEXTURE_COORD_ARRAY);
        std::cout << glGetError() << std::endl;


        // Bind the texture
        glActiveTexture(GL_TEXTURE0);
        std::cout << glGetError() << std::endl;


        glBindTexture(GL_TEXTURE_2D, mTexture);
        std::cout << glGetError() << std::endl;

        glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices);
        std::cout << glGetError() << std::endl;
##########################

I have been confused for a few days, how to solve it, thank you!

btw : I follow  gles1_simple_texture_2d and 





李欣

unread,
Jul 7, 2021, 9:05:32 AM7/7/21
to angleproject
I fix it by  d3d render texture in a windows thread.

##########################

DWORD WINAPI ThreadProc(LPVOID lpParam)
{
    //以下用于显示
    ID3D11ShaderResourceView *textureView;
    d3d11Device->CreateShaderResourceView(texture, nullptr, &textureView);

    ShowWindow(hwnd, 1);

    // Main Loop
    bool isRunning = true;
    while (isRunning)
    {
        MSG msg = {};
        while (PeekMessageW(&msg, 0, 0, 0, PM_REMOVE))
        {
            if (msg.message == WM_QUIT)
                isRunning = false;
            TranslateMessage(&msg);
            DispatchMessageW(&msg);
        }

        //Sleep(10*1000);

        if (global_windowDidResize)
        {
            d3d11DeviceContext->OMSetRenderTargets(0, 0, 0);
            d3d11FrameBufferView->Release();

            HRESULT res = d3d11SwapChain->ResizeBuffers(0, 0, 0, DXGI_FORMAT_UNKNOWN, 0);
            assert(SUCCEEDED(res));

            ID3D11Texture2D *d3d11FrameBuffer;
            res =
                d3d11SwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (void **)&d3d11FrameBuffer);
            assert(SUCCEEDED(res));

            res =
                d3d11Device->CreateRenderTargetView(d3d11FrameBuffer, NULL, &d3d11FrameBufferView);
            assert(SUCCEEDED(res));
            d3d11FrameBuffer->Release();

            global_windowDidResize = false;
        }

        FLOAT backgroundColor[4] = {0.1f, 0.2f, 0.6f, 1.0f};
        d3d11DeviceContext->ClearRenderTargetView(d3d11FrameBufferView, backgroundColor);

        RECT winRect;
        GetClientRect(hwnd, &winRect);
        D3D11_VIEWPORT viewport = {0.0f,
                                   0.0f,
                                   (FLOAT)(winRect.right - winRect.left),
                                   (FLOAT)(winRect.bottom - winRect.top),
                                   0.0f,
                                   1.0f};
        d3d11DeviceContext->RSSetViewports(1, &viewport);

        d3d11DeviceContext->OMSetRenderTargets(1, &d3d11FrameBufferView, nullptr);

        d3d11DeviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
        d3d11DeviceContext->IASetInputLayout(inputLayout);

        d3d11DeviceContext->VSSetShader(vertexShader, nullptr, 0);
        d3d11DeviceContext->PSSetShader(pixelShader, nullptr, 0);

        d3d11DeviceContext->PSSetShaderResources(0, 1, &textureView);
        d3d11DeviceContext->PSSetSamplers(0, 1, &samplerState);

        d3d11DeviceContext->IASetVertexBuffers(0, 1, &vertexBuffer, &stride, &offset);

        d3d11DeviceContext->Draw(numVerts, 0);

        d3d11SwapChain->Present(1, 0);
    }

    return 0;
}
##########################
Reply all
Reply to author
Forward
0 new messages