public void TextureUsageCases() { TypelessTexture2D texture = new TypelessTexture2D(Usage.Default, TextureUsage.Texture | TextureUsage.RenderTarget, CPUAccess.None, PixelFormat.Parse("R.T8 G.T8 B.T8 A.T8"), 2, 2, 1, GraphicsLocality.DeviceOrSystemMemory, null); TextureView textureView = texture.CreateTexture(PixelFormat.Parse("R.UN8 G.UN8 B.UN8 A.UN8")); // Makes sure we know when it was disposed. texture.Disposed += delegate(IResource who) { TypelessTexture2D t = who as TypelessTexture2D; Console.Write("Texture disposed."); }; // Make sure texture is disposed when all views are disposed (default is false). texture.DisposeOnViewDispose = true; // We fill data. using (Mipmap mipmap = texture.Map(MapOptions.Write, 0)) { // We fill the mipmap. unsafe { fixed(byte *b = mipmap.Data) { uint *data = (uint *)b; data[0] = Colour.Green.PackedRGBA; data[1] = Colour.Black.PackedRGBA; data[2] = Colour.White.PackedRGBA; data[3] = Colour.Green.PackedRGBA; } } } // texture.UnLock(), Could also call mipmap.Dispose() but here not required since // using statement takes care of it. // Mipmap disposed here, cannot be used. At this point, texture // is still not bound to device, creation reuses the same buffer. GraphicsDevice device = InitializeDevice(); try { device.Enter(); textureView.BindToDevice(device); // Hardware support is now true. // We can render with it if we want ... } finally { // Makes sure we exit state. device.Exit(); } // We dispose at the end, texture also disposed (no more views). textureView.Dispose(); }
/// <summary> /// Initializes the specified face witd default data. /// </summary> /// <param name="face">The face.</param> public void Initialize(uint face) { Mipmap mip = this.Map(MapOptions.Write, 0, face, true); mip.Data.Initialize(); this.UnMap(0); }
/// <summary> /// Maps all mipmaps. /// </summary> /// <param name="op">The options.</param> /// <returns>All mipmas locked.</returns> public Mipmap[] MapAll(MapOptions op) { Mipmap[] mipmaps = new Mipmap[MipmapCount]; for (uint i = 0; i < MipmapCount; i++) { mipmaps[i] = Map(op, i); } return(mipmaps); }
/// <summary> /// Initializes the image's face with specific colour. Other (non RGBA components) /// stay the same. /// </summary> /// <param name="face">The face index.</param> /// <param name="colour">The colour.</param> public void Initialize(uint face, Colour colour) { Mipmap mip = this.Map(MapOptions.Write, 0, face, true); try { byte[] data = mip.Data; // We go through red first. PixelFormat format = this.Format; PixelFormat.Element red = format.Find(PixelComponent.Red); PixelFormat.Element green = format.Find(PixelComponent.Green); PixelFormat.Element blue = format.Find(PixelComponent.Blue); PixelFormat.Element alpha = format.Find(PixelComponent.Alpha); // We go through each component, red first. if (red != null) { Images.ImageHelper.WriteComponentToImage(red.Format, data, colour.R, red.Offset, mip.PixelSize, format.Size); } // Green ... if (green != null) { Images.ImageHelper.WriteComponentToImage(green.Format, data, colour.G, green.Offset, mip.PixelSize, format.Size); } // Blue. if (blue != null) { Images.ImageHelper.WriteComponentToImage(blue.Format, data, colour.B, blue.Offset, mip.PixelSize, format.Size); } // And alpha channel. if (alpha != null) { Images.ImageHelper.WriteComponentToImage(alpha.Format, data, colour.A, alpha.Offset, mip.PixelSize, format.Size); } } finally { this.UnMap(0); } }
public unsafe void HardwareComposition1() { Compositor compositor = new Compositor(device); // We composite image. compositor.Push(sampleImage); compositor.Resize(BuildImageFilter.Nearest, 4, 4); compositor.CopyTo(renderTarget); Assert.AreEqual(renderTarget.Width, 4); Assert.AreEqual(renderTarget.Height, 4); // We check for correctness using (Mipmap mipmap = renderTarget.Map(MapOptions.Read, 0)) { // We examine the contents. fixed(byte *pp = mipmap.Data) { uint *chunk = (uint *)pp; for (int x = 0; x < 4; x++) { for (int y = 0; y < 4; y++) { if (x < 2 && y < 2) { Assert.AreEqual(chunk[y * 4 + x], 0xFF0000FF); } if (x > 2 && y < 2) { Assert.AreEqual(chunk[y * 4 + x], 0x00FF00FF); } if (x < 2 && y > 2) { Assert.AreEqual(chunk[y * 4 + x], 0x0000FFFF); } else { Assert.AreEqual(chunk[y * 4 + x], 0xFFFFFFFF); } } } } } }