1
\$\begingroup\$

I have an project I'm looking for a better solution to. I have a need to render n images over each other the previous one. So I have loop if you will. This should simulate alpha bending via basic combining the target and overlay pixel color values. Please see code below.

public void Render()
    {
        Device3DModule.DeviceContext.ClearRenderTargetView(Device3DModule.RenderTargetView, Colors.Blue.ToDXColor());

        CompositeShader.Apply();

        foreach (var image in Images)
        {
            Texture2D target = null;
            var pass01 = true;

            foreach (var imageChannel in image)
            {
                if(!imageChannel.IsVisible || imageChannel.Image == null) continue;

                imageChannel.Pass01 = pass01;

                Device3DModule.DeviceContext.PixelShader.SetShaderResource(1, imageChannel.Image);
                imageChannel.CopyToDeviceBuffer(ref m_ChannelSettingBuffer);

                QuadMesh.Draw();

                target?.Dispose();

                target = new Texture2D(Device3DModule.Device, Device3DModule.RenderTarget.Description);
                Device3DModule.DeviceContext.CopyResource(Device3DModule.RenderTarget, target);

                ShaderResourceView = new ShaderResourceView(Device3DModule.Device, target);
                Device3DModule.DeviceContext.PixelShader.SetShaderResource(0, ShaderResourceView);

                pass01 = false;
            }

            target?.Dispose();
            ShaderResourceView.Dispose();    
        }
    }

Does anyone have a better idea?

\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

the way I have done it via shader is to take the 2 images, and lerp each pixel (sample really) between source and destination. Using a value between 0 and 1 to weight the lerp between each pixel. Its gives a good representation, and gives you access to additional sampling features like bilinear/trilinear/ansiotropic filtering.

The other benefit of doing this is, if the textures are different sizes then the sampling will take into account this as well (using uv's obviously).

There is additional set up like creating a fullscreen quad, but I think it gives you the best outcome for all the other issues you have to deal with (sizing etc).

For multiple images > 2, you could set your loop up as.

Create 2 backbuffers as rendertargets / shader resources.

Set first first 2 images to blend -> render to a first backbuffer. Next loop, set 1 texture source to the previous backbuffer and the other to your next image, set rendertarget to backbuffer 2.
each loop, just swap between backbuffers as render target/shader resource. for each layer, you can set your blend amount as you see fit.

Anyway, goodluck.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .