Пример #1
0
        public void WritePixelBufferBitmap()
        {
            using (Bitmap bitmap = new Bitmap(1, 1))
                using (GraphicsWindow window = Device.CreateWindow(1, 1))
                    using (WritePixelBuffer pixelBuffer = Device.CreateWritePixelBuffer(PixelBufferHint.Stream,
                                                                                        BitmapAlgorithms.SizeOfPixelsInBytes(bitmap)))
                    {
                        Color color = Color.FromArgb(0, 1, 2, 3);
                        bitmap.SetPixel(0, 0, color);

                        pixelBuffer.CopyFromBitmap(bitmap);

                        //
                        // Verify read back - comes back BGRA
                        //
                        BlittableBGRA[] readBackPixel = pixelBuffer.CopyToSystemMemory <BlittableBGRA>();
                        Assert.AreEqual(color.R, readBackPixel[0].R);
                        Assert.AreEqual(color.G, readBackPixel[0].G);
                        Assert.AreEqual(color.B, readBackPixel[0].B);
                        Assert.AreEqual(color.A, readBackPixel[0].A);

                        //
                        // Verify read back into bitmap
                        //
                        using (Bitmap readBackBitmap = pixelBuffer.CopyToBitmap(1, 1, PixelFormat.Format32bppArgb))
                        {
                            Assert.AreEqual(color, readBackBitmap.GetPixel(0, 0));
                        }
                    }
        }
Пример #2
0
        public void Texture2D()
        {
            BlittableRGBA[] pixels = new BlittableRGBA[]
            {
                new BlittableRGBA(Color.Red),
                new BlittableRGBA(Color.Green)
            };
            int sizeInBytes = ArraySizeInBytes.Size(pixels);
            Texture2DDescription description = new Texture2DDescription(2, 1, TextureFormat.RedGreenBlueAlpha8, false);

            using (GraphicsWindow window = Device.CreateWindow(1, 1))
                using (WritePixelBuffer writePixelBuffer = Device.CreateWritePixelBuffer(PixelBufferHint.Stream, sizeInBytes))
                    using (Texture2D texture = Device.CreateTexture2D(description))
                    {
                        writePixelBuffer.CopyFromSystemMemory(pixels);

                        //
                        // Create texture with pixel buffer
                        //
                        texture.CopyFromBuffer(writePixelBuffer, BlittableRGBA.Format, BlittableRGBA.Datatype);

                        //
                        // Read back pixels
                        //
                        using (ReadPixelBuffer readPixelBuffer = texture.CopyToBuffer(BlittableRGBA.Format, BlittableRGBA.Datatype))
                        {
                            BlittableRGBA[] readPixels = readPixelBuffer.CopyToSystemMemory <BlittableRGBA>();

                            Assert.AreEqual(sizeInBytes, readPixelBuffer.SizeInBytes);
                            Assert.AreEqual(pixels[0], readPixels[0]);
                            Assert.AreEqual(pixels[1], readPixels[1]);
                            Assert.AreEqual(description, texture.Description);
                        }
                    }
        }
Пример #3
0
        public void Texture2DRowAlignment()
        {
            //
            // Create pixel buffer - RGB, 4 byte aligned, 255 is padding
            //
            byte[] pixels = new byte[]
            {
                1, 2, 3, 255,
                4, 5, 6, 255
            };
            int sizeInBytes = pixels.Length * sizeof(byte);

            using (GraphicsWindow window = Device.CreateWindow(1, 1))
                using (WritePixelBuffer writePixelBuffer = Device.CreateWritePixelBuffer(PixelBufferHint.Stream, sizeInBytes))
                    using (Texture2D texture = Device.CreateTexture2D(new Texture2DDescription(1, 2, TextureFormat.RedGreenBlue8, false)))
                    {
                        writePixelBuffer.CopyFromSystemMemory(pixels);
                        texture.CopyFromBuffer(writePixelBuffer, ImageFormat.RedGreenBlue, ImageDatatype.UnsignedByte);

                        //
                        // Read back pixels
                        //
                        using (ReadPixelBuffer readPixelBuffer = texture.CopyToBuffer(ImageFormat.RedGreenBlue, ImageDatatype.UnsignedByte, 1))
                        {
                            byte[] readPixels = readPixelBuffer.CopyToSystemMemory <byte>();
                            Assert.AreEqual(1, readPixels[0]);
                            Assert.AreEqual(2, readPixels[1]);
                            Assert.AreEqual(3, readPixels[2]);
                            Assert.AreEqual(4, readPixels[3]);
                            Assert.AreEqual(5, readPixels[4]);
                            Assert.AreEqual(6, readPixels[5]);
                        }
                    }
        }
Пример #4
0
 public virtual void CopyFromBuffer(
     WritePixelBuffer pixelBuffer,
     ImageFormat format,
     ImageDatatype dataType)
 {
     CopyFromBuffer(pixelBuffer, 0, 0, Description.Width, Description.Height, format, dataType, 4);
 }
Пример #5
0
 public abstract void CopyFromBuffer(
     WritePixelBuffer pixelBuffer,
     int xOffset,
     int yOffset,
     int width,
     int height,
     ImageFormat format,
     ImageDatatype dataType,
     int rowAlignment);
Пример #6
0
        public void Texture2DSubImage()
        {
            float[] pixels = new float[]
            {
                1, 2,
                3, 4
            };
            int sizeInBytes = pixels.Length * sizeof(float);
            Texture2DDescription description = new Texture2DDescription(2, 2, TextureFormat.Red32f, true);

            using (GraphicsWindow window = Device.CreateWindow(1, 1))
                using (WritePixelBuffer writePixelBuffer = Device.CreateWritePixelBuffer(PixelBufferHint.Stream, sizeInBytes))
                    using (Texture2D texture = Device.CreateTexture2D(description))
                    {
                        //
                        // Copy from system memory to texture via pixel buffer
                        //
                        writePixelBuffer.CopyFromSystemMemory(pixels);
                        texture.CopyFromBuffer(writePixelBuffer, ImageFormat.Red, ImageDatatype.Float, 1);

                        //
                        // Read back pixels
                        //
                        using (ReadPixelBuffer readPixelBuffer = texture.CopyToBuffer(ImageFormat.Red, ImageDatatype.Float, 1))
                        {
                            float[] readPixels = readPixelBuffer.CopyToSystemMemory <float>();

                            //
                            // Verify
                            //
                            Assert.AreEqual(sizeInBytes, readPixelBuffer.SizeInBytes);
                            Assert.AreEqual(pixels[0], readPixels[0]);
                            Assert.AreEqual(pixels[1], readPixels[1]);
                            Assert.AreEqual(pixels[2], readPixels[2]);
                            Assert.AreEqual(pixels[3], readPixels[3]);
                            Assert.AreEqual(description, texture.Description);

                            //
                            // Update sub image
                            //
                            float modifiedPixel = 9;
                            writePixelBuffer.CopyFromSystemMemory(new[] { modifiedPixel });
                            texture.CopyFromBuffer(writePixelBuffer, 1, 1, 1, 1, ImageFormat.Red, ImageDatatype.Float, 1);
                            using (ReadPixelBuffer readPixelBuffer2 = texture.CopyToBuffer(ImageFormat.Red, ImageDatatype.Float, 1))
                            {
                                float[] readPixels2 = readPixelBuffer2.CopyToSystemMemory <float>();

                                Assert.AreEqual(sizeInBytes, readPixelBuffer2.SizeInBytes);
                                Assert.AreEqual(pixels[0], readPixels2[0]);
                                Assert.AreEqual(pixels[1], readPixels2[1]);
                                Assert.AreEqual(pixels[2], readPixels2[2]);
                                Assert.AreEqual(modifiedPixel, readPixels2[3]);
                            }
                        }
                    }
        }
Пример #7
0
        private static Texture2D CreateTexture2DFromBitmap(Bitmap bitmap, TextureFormat format, bool generateMipmaps, TextureTarget textureTarget)
        {
            using (WritePixelBuffer pixelBuffer = Device.CreateWritePixelBuffer(PixelBufferHint.Stream,
                                                                                BitmapAlgorithms.SizeOfPixelsInBytes(bitmap)))
            {
                pixelBuffer.CopyFromBitmap(bitmap);

                Texture2DDescription description = new Texture2DDescription(bitmap.Width, bitmap.Height, format, generateMipmaps);
                Texture2D            texture     = new Texture2DGL3x(description, textureTarget);
                texture.CopyFromBuffer(pixelBuffer,
                                       TextureUtility.ImagingPixelFormatToImageFormat(bitmap.PixelFormat),
                                       TextureUtility.ImagingPixelFormatToDatatype(bitmap.PixelFormat));

                return(texture);
            }
        }
Пример #8
0
        public void WritePixelBuffer()
        {
            BlittableRGBA[] pixels = new BlittableRGBA[]
            {
                new BlittableRGBA(Color.Red),
                new BlittableRGBA(Color.Green),
                new BlittableRGBA(Color.Blue),
                new BlittableRGBA(Color.White)
            };
            int sizeInBytes = ArraySizeInBytes.Size(pixels);

            using (GraphicsWindow window = Device.CreateWindow(1, 1))
                using (WritePixelBuffer pixelBuffer = Device.CreateWritePixelBuffer(PixelBufferHint.Stream, sizeInBytes))
                {
                    //
                    // Verify creating pixel buffer
                    //
                    Assert.IsNotNull(pixelBuffer);
                    Assert.AreEqual(PixelBufferHint.Stream, pixelBuffer.UsageHint);
                    Assert.AreEqual(sizeInBytes, pixelBuffer.SizeInBytes);

                    //
                    // Verify copying entire buffer between system memory and pixel buffer
                    //
                    pixelBuffer.CopyFromSystemMemory(pixels);

                    BlittableRGBA[] pixels2 = pixelBuffer.CopyToSystemMemory <BlittableRGBA>(0, pixelBuffer.SizeInBytes);
                    Assert.AreEqual(pixels[0], pixels2[0]);
                    Assert.AreEqual(pixels[1], pixels2[1]);
                    Assert.AreEqual(pixels[2], pixels2[2]);

                    //
                    // Verify modiying a subset of the vertex buffer
                    //
                    BlittableRGBA modifiedPixel = new BlittableRGBA(Color.Black);
                    pixelBuffer.CopyFromSystemMemory(new[] { modifiedPixel }, SizeInBytes <BlittableRGBA> .Value);

                    BlittableRGBA[] pixels3 = pixelBuffer.CopyToSystemMemory <BlittableRGBA>(0, pixelBuffer.SizeInBytes);
                    Assert.AreEqual(pixels[0], pixels3[0]);
                    Assert.AreEqual(modifiedPixel, pixels3[1]);
                    Assert.AreEqual(pixels[2], pixels3[2]);
                }
        }
Пример #9
0
        public void Texture2DAlignment()
        {
            byte[] pixels = new byte[]
            {
                1, 2, 3, 4, 4, 6,
                7, 8, 9, 10, 11, 12
            };
            int sizeInBytes = pixels.Length * sizeof(byte);
            Texture2DDescription description = new Texture2DDescription(2, 2, TextureFormat.RedGreenBlue8, false);

            using (GraphicsWindow window = Device.CreateWindow(1, 1))
                using (WritePixelBuffer writePixelBuffer = Device.CreateWritePixelBuffer(PixelBufferHint.Stream, sizeInBytes))
                    using (Texture2D texture = Device.CreateTexture2D(description))
                    {
                        //
                        // Copy from system memory to texture via pixel buffer
                        //
                        writePixelBuffer.CopyFromSystemMemory(pixels);
                        texture.CopyFromBuffer(writePixelBuffer, ImageFormat.RedGreenBlue, ImageDatatype.UnsignedByte, 1);

                        //
                        // Read back pixels
                        //
                        using (ReadPixelBuffer readPixelBuffer = texture.CopyToBuffer(ImageFormat.RedGreenBlue, ImageDatatype.UnsignedByte, 1))
                        {
                            byte[] readPixels = readPixelBuffer.CopyToSystemMemory <byte>();

                            //
                            // Verify
                            //
                            Assert.AreEqual(sizeInBytes, readPixelBuffer.SizeInBytes);
                            for (int i = 0; i < pixels.Length; ++i)
                            {
                                Assert.AreEqual(pixels[i], readPixels[i]);
                            }
                        }
                        Assert.AreEqual(description, texture.Description);
                    }
        }