eglCreatePbufferFromClientBuffer throw EGL_BAD_PARAMETER while doing D3D11Texture2D sharing

40 views
Skip to first unread message

Albert Yang

unread,
Aug 11, 2023, 4:43:01 AM8/11/23
to angleproject
Hi, I'm working on the D3D11Texture2D and angle Texture sharing depends on the EGL_ANGLE_d3d_share_handle_client_buffer extension. After I choose an EGLConfig with eglChooseConfig, and pass the config to eglCreatePbufferFromClientBuffer, it returns the EGL_BAD_PARAMETER error. After digging into the angle source code, I found the reason is that render target format (BGRA) of first config return by eglChooseConfig differs with  D3D11Texture2D format (RGBA) as I assigned.

/// error throw place:

egl::Error Renderer11::validateShareHandle(const egl::Config *config,
                                           HANDLE shareHandle,
                                           const egl::AttributeMap &attribs) const
{
// ....
// ....
// desc.Format != backbufferFormatInfo.texFormat is true.
    const d3d11::Format &backbufferFormatInfo =
        d3d11::Format::Get(config->renderTargetFormat, getRenderer11DeviceCaps());

    if (desc.Width != static_cast<UINT>(width) || desc.Height != static_cast<UINT>(height) ||
        desc.Format != backbufferFormatInfo.texFormat || desc.MipLevels != 1 || desc.ArraySize != 1)
    {
        std::string msgHeader = std::to_string(desc.Format) + " vs " + std::to_string(backbufferFormatInfo.texFormat);
        std::cout << msgHeader << std::endl;
        return egl::EglBadParameter() << msgHeader + ", Invalid texture parameters in share handle texture.";
    }

    return egl::NoError();
}

So the question is, if it's possible to query the expected render target format with  eglChooseConfig API? And if not, any suggestion on how to get appropriate  EGLConfig  to eglCreatePbufferFromClientBuffer?

following is my code snippet, any help is appreciated:
    D3D11_TEXTURE2D_DESC dst_desc;
    memset(&dst_desc, 0x0, sizeof(dst_desc));
    dst_desc.Width = width;
    dst_desc.Height = height;
    dst_desc.MipLevels = dst_desc.ArraySize = 1;
    dst_desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
    dst_desc.SampleDesc.Count = 1;
    dst_desc.Usage = D3D11_USAGE_DEFAULT;
    dst_desc.BindFlags = D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE;
    dst_desc.CPUAccessFlags = 0;
    dst_desc.MiscFlags = D3D11_RESOURCE_MISC_SHARED;

    ID3D11Texture2D* pd3dtex = NULL;
    HRESULT hr = device->CreateTexture2D(&dst_desc, NULL, &pd3dtex);

    IDXGIResource* dxgiResource;

    HANDLE sharedHandle;
    pd3dtex->QueryInterface(__uuidof(IDXGIResource), (LPVOID*)&dxgiResource);

    hr = dxgiResource->GetSharedHandle(&sharedHandle);
    if FAILED(hr)
    {
        std::cerr << "open shared handle failed: " << GetLastError() << std::endl;
    }
    else
    {
        std::cout << "shared handle: " << sharedHandle << std::endl;
    }

    dxgiResource->Release();

    ptex->d3d_tex = pd3dtex;

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

    int attribs[] = {
        // 32 bit color
        EGL_RED_SIZE, 8,
        EGL_GREEN_SIZE, 8,
        EGL_BLUE_SIZE, 8,
        EGL_ALPHA_SIZE, 8,
        // at least 24 bit depth
        EGL_DEPTH_SIZE, 0,
        EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
        EGL_BIND_TO_TEXTURE_RGBA, EGL_TRUE,
        EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,
        EGL_NONE, EGL_NONE
    };

    EGLConfig configs[10];
    int num_config;

    eglChooseConfig(display, attribs, configs, 10, &num_config);
    if (num_config <= 0)
    {
        std::cerr << "no egl config found supports input attributes.\n";
    }
    else
    {
        //std::cout << "query count: " << num_config << ", selected config: " << configs[0] << std::endl;
    }

    EGLSurface surface1 = eglCreatePbufferFromClientBuffer(display, EGL_D3D_TEXTURE_2D_SHARE_HANDLE_ANGLE,
        sharedHandle, configs[0], pBufferAttributes);

    if (surface1 == EGL_NO_SURFACE)
    {
        EGLint error = eglGetError();
        std::cout << "egl error: " << std::hex << error << std::dec << std::endl;
        std::abort();
    }

Geoff Lang

unread,
Aug 14, 2023, 10:41:14 AM8/14/23
to ruijia...@gmail.com, angleproject
One of the drawbacks of the pbuffer creation part of this extension is that the configs have to match and BGRA can't be represented in a config.

Definitely prefer the EGLImage portion of the EGL_ANGLE_d3d_texture_client_buffer extension if you can. EGLImages are much more flexible and easier to create.

--
You received this message because you are subscribed to the Google Groups "angleproject" group.
To unsubscribe from this group and stop receiving emails from it, send an email to angleproject...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/angleproject/1a8e605a-8bd1-40a7-933e-bb1e1347f648n%40googlegroups.com.

Albert Yang

unread,
Aug 15, 2023, 2:54:51 AM8/15/23
to angleproject
Have tried  EGL_ANGLE_d3d_texture_client_buffer extension, a few differents with  EGL_ANGLE_d3d_share_handle_client_buffer. With EGL_ANGLE_d3d_texture_client_buffer, I get EGL display with EGL_ANGLE_device_creation_d3d11 extension and an exist d3d11 device. This works and I going to do more test.

Any one could comments on what is the most efficient way to share textures between D3D11 and ANGLE? currently, I suppose EGL_ANGLE_d3d_texture_client_buffer and pass external d3d11 device to ANGLE may be the most efficient one.

Geoff Lang

unread,
Aug 15, 2023, 10:45:39 AM8/15/23
to ruijia...@gmail.com, angleproject
All of these approaches are fine, there will be no copies of textures. Possibly some GPU synchronization but the overhead will be low.

Please be aware if you initialize ANGLE using an existing D3D device, you should only use that device to create resources and not use the ImmediateContext. Only one ImmediateContext can be created per D3D device and ANGLE's internal state caching will not know about any state you change.

Albert Yang

unread,
Aug 18, 2023, 1:28:36 AM8/18/23
to Geoff Lang, angleproject
Thanks for the tip! 
Reply all
Reply to author
Forward
0 new messages