Ejemplo n.º 1
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);
                        }
                    }
        }
Ejemplo n.º 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]);
                        }
                    }
        }
Ejemplo n.º 3
0
        private void SaveFloat(string filename, ImageFormat imageFormat)
        {
            using (ReadPixelBuffer pixelBuffer = CopyToBuffer(imageFormat, ImageDatatype.Float, 1))
            {
                float[] depths = pixelBuffer.CopyToSystemMemory <float>();

                float minValue = depths[0];
                float maxValue = depths[0];
                for (int i = 0; i < depths.Length; ++i)
                {
                    minValue = Math.Min(depths[i], minValue);
                    maxValue = Math.Max(depths[i], maxValue);
                }
                float deltaValue = maxValue - minValue;

                //
                // Avoid divide by zero if all depths are the same.
                //
                float oneOverDelta = (deltaValue > 0) ? (1 / deltaValue) : 1;

                Bitmap bitmap = new Bitmap(Description.Width, Description.Height, PixelFormat.Format24bppRgb);
                int    j      = 0;
                for (int y = Description.Height - 1; y >= 0; --y)
                {
                    for (int x = 0; x < Description.Width; ++x)
                    {
                        float linearDepth = (depths[j++] - minValue) * oneOverDelta;
                        int   intensity   = (int)(linearDepth * 255.0f);
                        bitmap.SetPixel(x, y, Color.FromArgb(intensity, intensity, intensity));
                    }
                }
                bitmap.Save(filename);
            }
        }
Ejemplo n.º 4
0
        public void RenderPointWithStencil()
        {
            using (GraphicsWindow window = Device.CreateWindow(1, 1))
                using (Framebuffer framebuffer = TestUtility.CreateFramebuffer(window.Context))
                    using (Texture2D depthStencilTexture = Device.CreateTexture2D(new Texture2DDescription(1, 1, TextureFormat.Depth24Stencil8, false)))
                        using (ShaderProgram sp = Device.CreateShaderProgram(ShaderSources.PassThroughVertexShader(), ShaderSources.PassThroughFragmentShader()))
                            using (VertexArray va = TestUtility.CreateVertexArray(window.Context, sp.VertexAttributes["position"].Location))
                            {
                                framebuffer.DepthStencilAttachment = depthStencilTexture;

                                StencilTest stencilTest = new StencilTest();
                                stencilTest.Enabled = true;
                                stencilTest.FrontFace.DepthFailStencilPassOperation = StencilOperation.Replace;
                                stencilTest.FrontFace.DepthPassStencilPassOperation = StencilOperation.Replace;
                                stencilTest.FrontFace.StencilFailOperation          = StencilOperation.Replace;
                                stencilTest.FrontFace.ReferenceValue = 2;

                                RenderState renderState = new RenderState();
                                renderState.StencilTest = stencilTest;

                                window.Context.Framebuffer = framebuffer;
                                window.Context.Clear(new ClearState());
                                window.Context.Draw(PrimitiveType.Points, 0, 1, new DrawState(renderState, sp, va), new SceneState());

                                TestUtility.ValidateColor(framebuffer.ColorAttachments[0], 255, 0, 0);

                                using (ReadPixelBuffer readPixelBuffer = depthStencilTexture.CopyToBuffer(ImageFormat.DepthStencil, ImageDatatype.UnsignedInt248, 1))
                                {
                                    byte[] depthStencil = readPixelBuffer.CopyToSystemMemory <byte>();
                                    Assert.AreEqual(stencilTest.FrontFace.ReferenceValue, depthStencil[0]);
                                }
                            }
        }
Ejemplo n.º 5
0
        ///////////////////////////////////////////////////////////////////////

        private static void ValidateDepth(Texture2D depthTexture, float depth)
        {
            using (ReadPixelBuffer readPixelBuffer = depthTexture.CopyToBuffer(ImageFormat.DepthComponent, ImageDatatype.Float, 1))
            {
                float[] readDepth = readPixelBuffer.CopyToSystemMemory <float>();
                Assert.AreEqual(depth, readDepth[0]);
            }
        }
Ejemplo n.º 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]);
                            }
                        }
                    }
        }
Ejemplo n.º 7
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);
                    }
        }