/// <summary> /// Function to draw some pretty pixels into a custom image for the example. /// </summary> private void CreateCustomImage() { // Create the image at the original size. var sourceImage = new GorgonImage(new GorgonImageInfo(ImageType.Image2D, BufferFormat.R8G8B8A8_UNorm) { Width = 320, Height = 240 }); // Draw something pretty... float width = sourceImage.Width; float height = sourceImage.Height; for (int x = 0; x < sourceImage.Width; ++x) { float rColorFade = ((x / width) * 1.5f).Cos(); for (int y = 0; y < sourceImage.Height; ++y) { float bColorFade = ((y / height) * 1.5f).Sin(); int pixelStride = sourceImage.Buffers[0].PitchInformation.RowPitch / sourceImage.Buffers[0].Width; // This is the position inside of the buffer, in bytes. int position = (y * sourceImage.Buffers[0].PitchInformation.RowPitch) + (x * pixelStride); var color = new GorgonColor(rColorFade, 1.0f - rColorFade, bColorFade, 1.0f); // Notice we're using ReadAs here. This allows us to read a value as another type. In this case, we've chosen an // int32 value to represent an ARGB pixel. Because this value is a reference to the location in memory, we can assign // a new value to it. // // Do note that the position is a byte address, and not an int address (i.e. position = 1 is 1 byte, not 1 int). ref int pixel = ref sourceImage.Buffers[0].Data.ReadAs <int>(position); pixel = color.ToARGB(); // We could easily do this as well (although this could be considered less readable): //_image.Buffers[0].Data.ReadAs<int>(position) = color.ToARGB(); } }
public void TestTextureUpdate() { float diver = 1.0f; var shader = _framework.Graphics.Shaders.FromMemory <GorgonPixelShader>("PS", "TestPSUpdateSub", Resources.TextureShaders); var texture = _framework.Graphics.Textures.CreateTexture("Texture", new GorgonTexture2DSettings { ArrayCount = 1, Format = BufferFormat.R8G8B8A8_UIntNormal, MipCount = 4, Height = 256, Width = 256, Usage = BufferUsage.Default }); var dynTexture = _framework.Graphics.Textures.CreateTexture("DynTexture", new GorgonTexture2DSettings { ArrayCount = 1, Format = BufferFormat.R8G8B8A8_UIntNormal, MipCount = 1, Height = 256, Width = 256, Usage = BufferUsage.Dynamic }); var imageData = GorgonImageData.CreateFromGDIImage(Resources.Glass, ImageType.Image2D, new GorgonGDIOptions { MipCount = 4 }); _framework.CreateTestScene(Shaders, Shaders, true); _framework.Graphics.Shaders.PixelShader.Current = shader; _framework.Graphics.Shaders.PixelShader.Resources[1] = texture; texture.UpdateSubResource(imageData.Buffers[0], new Rectangle { Width = 128, Height = 128, X = 0, Y = 0 }); texture.UpdateSubResource(imageData.Buffers[1], new Rectangle { Width = 64, Height = 64, X = 128, Y = 0 }); GorgonTexture2D testIntFormat = _framework.Graphics.Textures.CreateTexture("asas", new GorgonTexture2DSettings { Format = BufferFormat.R8G8B8A8_Int, Usage = BufferUsage.Dynamic, Width = 64, Height = 64 }); _framework.IdleFunc = () => { _framework.Graphics.Shaders.PixelShader.Resources[2] = null; using (var lockData = dynTexture.Lock(BufferLockFlags.Write | BufferLockFlags.Discard)) { for (int i = 0; i < 4096; ++i) { int y = GorgonRandom.RandomInt32(0, imageData.Buffers[0].Height); int x = GorgonRandom.RandomInt32(0, imageData.Buffers[0].Width); // 95417E imageData.Buffers[0].Data.Position = (y * imageData.Buffers[0].PitchInformation.RowPitch) + (x * 4); lockData.Data.Position = (y * lockData.PitchInformation.RowPitch) + (x * dynTexture.FormatInformation.SizeInBytes); var color = new GorgonColor(imageData.Buffers[0].Data.ReadInt32()); color = new GorgonColor(color.Red / diver, color.Green / diver, color.Blue / diver); lockData.Data.Write(color.ToARGB()); //lockData.Data.Write(0xFF00FF00); /*lockData.Data.Write(Color.FromArgb(color.ToARGB()).R); * lockData.Data.Write(Color.FromArgb(color.ToARGB()).G); * lockData.Data.Write(Color.FromArgb(color.ToARGB()).B); * lockData.Data.Write(Color.FromArgb(color.ToARGB()).A);*/ } } diver += 0.5f * GorgonTiming.Delta; if (diver > 32.0f) { diver = 1.0f; } _framework.Graphics.Shaders.PixelShader.Resources[2] = dynTexture; return(false); }; Assert.IsTrue(_framework.Run() == DialogResult.Yes); }