首页 > 解决方案 > 如何使用 direct3d9、direct3d10 或 direct3d11 对保存的原始 argb 视频(图像和视频也可以)执行 alpha 混合?

问题描述

我正在尝试使用 direct3d11 对视频执行 alpha 混合。我编写了下面的代码片段来实现这一点。我试图逐帧读取视频文件out2.argb并将其存储在rawData中。后来,我将该rawData传递给CreateTexture2D,但pTexture在执行 API 后仍然为空。所以我尝试将NULL传递给CreateTexture2D并将rawData传递给UpdateSubresourcepTexture再次保持为NULLDirectX 11 ID3DDevice::CreateTexture2D 初始数据失败 我已经在此处提到了问题和答案以使这项工作正常进行,但 API 返回失败。

HRESULT result;
    FILE * fp = NULL;
    int size = 1280 * 720 * 4 ;
    void * rawData = NULL;
    D3D11_TEXTURE2D_DESC desc;
    desc.Width = 1280;
    desc.Height = 720;
    desc.MipLevels = 0;
    desc.ArraySize = 1;
    desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
    desc.SampleDesc.Count = 1;
    desc.Usage = D3D11_USAGE_DYNAMIC;
    desc.BindFlags = D3D11_BIND_SHADER_RESOURCE| D3D11_BIND_RENDER_TARGET;
    desc.CPUAccessFlags = 0;
    desc.MiscFlags = D3D11_RESOURCE_MISC_GENERATE_MIPS;


    D3D11_SHADER_RESOURCE_VIEW_DESC shader;
    shader.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
    shader.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
    shader.Texture2D.MipLevels = -1 ;
    shader.Texture2D.MostDetailedMip = 0;


    fp = fopen("../Engine/Data/out2.argb", "rb");
    rawData = malloc(size);

    D3D11_SUBRESOURCE_DATA data;
    data.SysMemPitch = 1280 * 4;
    data.SysMemSlicePitch = 1280 * 720 * 4;


    if (NULL == rawData)
        return -1;
    int row_pitch = (1280 * 4) * sizeof(unsigned char);
    if (fp)
    {
        /*while (!feof(fp))
        {*/
            fread(rawData, size,1, fp);
            //D3D11_SUBRESOURCE_DATA *sSubData = new D3D11_SUBRESOURCE_DATA[1];
            //for (int i = 0; i < 1; i++) {
            //  sSubData[i].pSysMem = rawData;
            //  sSubData[i].SysMemPitch = (UINT)(1280 * 4);
            //  sSubData[i].SysMemSlicePitch = /*(UINT)(1280 * 720 * 4)*/0;
            //}

            //data.pSysMem = rawData;
            // GOT FRAME
            device->CreateTexture2D(&desc, NULL, &pTexture);
            deviceContext->UpdateSubresource(pTexture, 0, NULL, rawData, row_pitch , 0);
            result = device->CreateShaderResourceView(pTexture, &shader, &m_texture);
            if (FAILED(result))
            {
                return false;
            }


    /*  }*/

        fclose(fp);
        fp = NULL;
        free(rawData);
        return true;

标签: alphablendingalpha-transparencydirect3d11direct3d9direct3d10

解决方案


推荐阅读