public CompositorTest() { byte[] data = new byte[] { // Red. 255, 0, 0, 255, // Green. 0, 255, 0, 255, // Blue. 0, 0, 255, 255, // White 255, 255, 255, 255 }; TypelessTexture2D t = new TypelessTexture2D(Usage.Default, TextureUsage.Texture, CPUAccess.Read, PixelFormat.Parse("R.UN8 R.UN8 G.UN8 A.UN8"), 2, 2, 1, GraphicsLocality.DeviceOrSystemMemory, new byte[][] { data }); t.DisposeOnViewDispose = true; sampleImage = t.CreateTexture(); t = new TypelessTexture2D(Usage.Default, TextureUsage.Texture | TextureUsage.RenderTarget, CPUAccess.Read, PixelFormat.Parse("R.UN8 R.UN8 G.UN8 A.UN8"), 4, 4, 1, GraphicsLocality.DeviceOrSystemMemory, null); renderTarget = t.CreateRenderTarget(); }
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> /// Creates a 2D render target. /// </summary> /// <param name="format"></param> /// <param name="width"></param> /// <param name="height"></param> /// <param name="multiSampleType"></param> /// <param name="multiSampleQuality"></param> /// <returns></returns> public static RenderTargetView Create(GraphicsDevice graphicsDevice, PixelFormat format, uint width, uint height, uint multiSampleType, uint multiSampleQuality) { TypelessTexture2D t = new TypelessTexture2D(graphicsDevice, Usage.Default, TextureUsage.RenderTarget, CPUAccess.None, format, width, height, 1, multiSampleType, multiSampleQuality, GraphicsLocality.DeviceOrSystemMemory, null); t.DisposeOnViewDispose = true; return(t.CreateRenderTargetMS(format)); }
/// <summary> /// Simple black screen initialization. /// </summary> //[CorrectnessTest] public void CustomRT() { using (GraphicsDevice device = InitializeDevice()) { TypelessTexture2D texture = new TypelessTexture2D(Usage.Default, TextureUsage.RenderTarget, CPUAccess.None, PixelFormat.Parse("R.UN8 G.UN8 B.UN8 A.UN8"), 512, 512, 1, GraphicsLocality.DeviceOrSystemMemory, null); RenderTargetView view = texture.CreateRenderTarget(0); bool isClosed = false; window.Closed += delegate(Window w) { isClosed = true; }; for (uint i = 0; i < 1000; i++) { window.DoEvents(); if (!isClosed) { SwapChain chain = device.SwapChain; device.Enter(); try { // We just clear. device.Clear(chain, Colour.Blue); device.Clear(view, Colour.Black); } finally { device.Exit(); } chain.Present(); Thread.Sleep(10); } } } }
/// <summary> /// Obtains an already created device. /// </summary> /// <param name="parameters">Parameters for default rendering surface, may be null for shared mode only.</param> /// <returns></returns> public GraphicsDevice ObtainDevice(RenderTargetParameters parameters) { GraphicsDevice device = new GraphicsDevice(service.Obtain()); // We now create a render target. if (parameters != null) { // TODO account for multisampling. TypelessTexture2D rt = new TypelessTexture2D(Usage.Default, TextureUsage.RenderTarget, CPUAccess.None, parameters.Format, parameters.BackBufferWidth, parameters.BackBufferHeight, 1, GraphicsLocality.DeviceMemoryOnly, null); if (parameters.DepthStencilCommonFormat != CommonPixelFormatLayout.NotCommonLayout) { TypelessTexture2D dt = new TypelessTexture2D(Usage.Default, TextureUsage.DepthStencilTarget, CPUAccess.None, parameters.DepthStencilFormat, parameters.BackBufferWidth, parameters.BackBufferHeight, 1, GraphicsLocality.DeviceMemoryOnly, null); // Associate render target and depth stencil with graphics device. device.Initialize(rt.CreateRenderTarget(), dt.CreateDepthStencil()); } else { device.Initialize(rt.CreateRenderTarget(), null); } // They will be auto-bound. } else { device.Initialize(null, null); } return(device); }