Чтение значений пикселей трафарета DirectX

Мы можем успешно прочитать значения буфера глубины, используя следующий метод. Как мы можем изменить его для чтения значений трафарета?

Спасибо.

    byte[] ReadDepthPixels(RenderContextBase context, Texture2D other, System.Drawing.Rectangle rect)
    {
        var depthTex = new D3DTextureDepth(context, other);

        var depths = D3DTexture2D.ReadDepthsInternal(rect, (Texture2D)depthTex.texture);
        var rgbValues = new byte[rect.Width * rect.Height * 4];

        double ratio = 255.0 / short.MaxValue;
        int w = ((Texture2D)depthTex.texture).Description.Width;
        int h = ((Texture2D)depthTex.texture).Description.Height;
        int offset = w - rect.Width;
        int pxlIndex = rect.Y * w + rect.X;
        int count = 0;

        for (int i = 0; i < rect.Height; i++)
        {
            pxlIndex = (h - i - 1) * w; // flip in Y
            for (int j = 0; j < rect.Width; j++, pxlIndex++)
            {
                double val = ratio * depths[pxlIndex];
                rgbValues[count++] = (byte)val;
                rgbValues[count++] = (byte)val;
                rgbValues[count++] = (byte)val;
                rgbValues[count++] = 255;
            }
        }

        depthTex.Dispose();

        return rgbValues;
    }

    short[] ReadDepthsInternal(Rectangle rect, Texture2D texture)
    {
        short[] values = null;

        using (Surface surface = texture.QueryInterface<Surface>())
        {
            int bytesPerElement = 4;
            DataStream dataStream;
            var map = surface.Map(SharpDX.DXGI.MapFlags.Read, out dataStream);
            values = new short[rect.Width*rect.Height];

            int actualWidth = rect.Width*bytesPerElement;

            byte[] rowBytes = new byte[actualWidth];

            int rowDiffBytes = map.Pitch - actualWidth;

            int endY = rect.Y + rect.Height;

            dataStream.Seek(rect.Y*map.Pitch + rect.X*bytesPerElement, SeekOrigin.Begin);

            if (texture.Description.Format == Format.D24_UNorm_S8_UInt)
            {
                int[] intValues = new int[rect.Width];

                float den = (float) Math.Pow(2, 24) - 1; // 16777215

                // Convert from 24 bit depth to 16 bit depth
                float ratio = (float) short.MaxValue/den;

                for (int i = rect.Y, countRow = 0; i < endY; i++, countRow++)
                {
                    dataStream.Read(rowBytes, 0, actualWidth);

                    System.Buffer.BlockCopy(rowBytes, 0, intValues, 0, actualWidth);

                    // REVERSE the read of the depth buffer lines
                    int count = rect.Size.Width*(rect.Size.Height - countRow - 1);

                    for (int j = 0; j < intValues.Length; j++)

                        values[count++] = (short) Math.Round(ratio*intValues[j]);

                    if ((dataStream.Position + rowDiffBytes) <= dataStream.Length)

                        dataStream.Seek(rowDiffBytes, SeekOrigin.Current);
                }
            }              

            dataStream.Dispose();

            surface.Unmap();
        }

        return values;
    }

person abenci    schedule 26.12.2017    source источник


Ответы (1)


Только что пришло мне в голову - и полное раскрытие, поскольку я на самом деле не пробовал этого, но вы можете попробовать создать представление ресурсов на текстуре глубины/трафарета, используя формат DXGI_FORMAT_X24_TYPELESS_G8_UINT; это предоставит вам доступ к компоненту G8_UINT для чтения.

person Varrak    schedule 27.04.2018