Beispiel #1
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]);
                            }
                        }
                    }
        }
Beispiel #2
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]);
                        }
                    }
        }
Beispiel #3
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);
                        }
                    }
        }
        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]);
                }
        }
Beispiel #5
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);
                    }
        }