directx-11cefsharpchromium-embeddedsharpdxcefsharp.offscreen

Updating Texture2D frequently causes process to crash (UpdateSubresource)


I am using SharpDX to basically render browser (chromium) output buffer on directX process.

Process is relatively simple, I intercept CEF buffer (by overriding OnPaint method) and write that to a texture2D.

Code is relatively simple:

Texture creation:

public void BuildTextureWrap() {
    var oldTexture = texture;

    texture = new D3D11.Texture2D(DxHandler.Device, new D3D11.Texture2DDescription() {
        Width = overlay.Size.Width,
        Height = overlay.Size.Height,
        MipLevels = 1,
        ArraySize = 1,
        Format = DXGI.Format.B8G8R8A8_UNorm,
        SampleDescription = new DXGI.SampleDescription(1, 0),
        Usage = D3D11.ResourceUsage.Default,
        BindFlags = D3D11.BindFlags.ShaderResource,
        CpuAccessFlags = D3D11.CpuAccessFlags.None,
        OptionFlags = D3D11.ResourceOptionFlags.None,
    });

    var view = new D3D11.ShaderResourceView(
        DxHandler.Device,
        texture,
        new D3D11.ShaderResourceViewDescription {
            Format = texture.Description.Format,
            Dimension = D3D.ShaderResourceViewDimension.Texture2D,
            Texture2D = { MipLevels = texture.Description.MipLevels },
        }
    );

    textureWrap = new D3DTextureWrap(view, texture.Description.Width, texture.Description.Height);

    if (oldTexture != null) {
        obsoleteTextures.Add(oldTexture);
    }
}

That piece of code is executed at start and when resize is happening.

Now when CEF OnDraw I basically copy their buffer to texture:

var destinationRegion = new D3D11.ResourceRegion {
    Top = Math.Min(r.dirtyRect.y, texDesc.Height),
    Bottom = Math.Min(r.dirtyRect.y + r.dirtyRect.height, texDesc.Height),
    Left = Math.Min(r.dirtyRect.x, texDesc.Width),
    Right = Math.Min(r.dirtyRect.x + r.dirtyRect.width, texDesc.Width),
    Front = 0,
    Back = 1,
};

// Draw to the target
var context = targetTexture.Device.ImmediateContext;
context.UpdateSubresource(targetTexture, 0, destinationRegion, sourceRegionPtr, rowPitch, depthPitch);

There are some more code out there but basically this is only relevant piece. Whole thing works until OnDraw happens frequently.

Apparently if I force CEF to Paint frequently, whole host process dies. This is happening at UpdateSubresource.

So my question is, is there another, safer way to do this? (Update texture frequently)


Solution

  • Solution to this problem was relatively simple yet not so obvious at the beginning.

    I simply moved the code responsible for updating texture inside render loop and just keep internal buffer pointer cached.