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]); } } } }
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 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]); } } }
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]); } } }
/////////////////////////////////////////////////////////////////////// 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]); } }
public static void ValidateColor(Texture2D colorTexture, byte red, byte green, byte blue) { using (ReadPixelBuffer readPixelBuffer = colorTexture.CopyToBuffer(ImageFormat.RedGreenBlue, ImageDatatype.UnsignedByte, 1)) { byte[] color = readPixelBuffer.CopyToSystemMemory<byte>(); Assert.AreEqual(red, color[0], "Red does not match"); Assert.AreEqual(green, color[1], "Green does not match"); Assert.AreEqual(blue, color[2], "Blue does not match"); } }
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); } }
/////////////////////////////////////////////////////////////////////// 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]); } }