public GameTexture GetWorley(List <float> positions, int width, int height) { CLFilter filter = _kernels["worley"]; byte[] buffer = new byte[width * height * 4]; MemoryBuffer mb = c.CreateBuffer(MemoryFlag.ReadWrite | MemoryFlag.CopyHostPointer, buffer); MemoryBuffer points = c.CreateBuffer(MemoryFlag.ReadOnly | MemoryFlag.CopyHostPointer, positions.ToArray()); //Maybe wrong struct filter.SetArg(0, mb); int3 dims = new int3(width, height, 1); filter.SetArg(1, dims); filter.SetArg(2, 4); filter.SetArg(3, enablechannels); filter.SetArg(4, points); filter.SetArg(5, positions.Count / 3); filter.SetArg(6, 0.3f); CommandQueue cq = CommandQueue.CreateCommandQueue(c, d); cq.EnqueueNDRangeKernel(filter.kernel, 1, buffer.Length); buffer = cq.EnqueueReadBuffer <byte>(mb, buffer.Length); return(GameTexture.Load(buffer, width, height)); }
public void RunSmoothNoise(GameTexture tex, int octave) { CLFilter filter = _kernels["smooth"]; GL.GetTexLevelParameter(TextureTarget.Texture2D, 0, GetTextureParameter.TextureWidth, out float w); GL.GetTexLevelParameter(TextureTarget.Texture2D, 0, GetTextureParameter.TextureHeight, out float h); int width = (int)w; int height = (int)h; byte[] buffer = new byte[width * height * 4]; GL.GetTexImage(TextureTarget.Texture2D, 0, PixelFormat.Bgra, PixelType.UnsignedByte, buffer); MemoryBuffer mb = c.CreateBuffer(MemoryFlag.ReadWrite | MemoryFlag.CopyHostPointer, buffer); filter.SetArg(0, mb); filter.SetArg(1, new int3(width, height, 1)); filter.SetArg(2, 4); filter.SetArg(3, enablechannels); int samplePeriod = 1 << octave; filter.SetArg(4, samplePeriod); filter.SetArg(5, 1f / samplePeriod); CommandQueue cq = CommandQueue.CreateCommandQueue(c, d); cq.EnqueueNDRangeKernel(filter.kernel, 1, buffer.Length); buffer = cq.EnqueueReadBuffer <byte>(mb, buffer.Length); GL.BindTexture(TextureTarget.Texture2D, tex.textureID); GL.TexSubImage2D(TextureTarget.Texture2D, 0, 0, 0, (int)width, (int)height, PixelFormat.Bgra, PixelType.UnsignedByte, buffer); }
public void RunLightKernel(GameTexture tex) { //MemoryBuffer buf = c.CreateFromGLTexture2D(MemoryFlag.ReadWrite | MemoryFlag.UseHostPointer, (uint)TextureTarget.Texture2D, 0, (uint)tex.textureID); //string test = d.GetDeviceInformation<string>(DeviceInformation.Extensions); //DevicesNativeApi.GetDeviceInformation(d, DeviceInformation.Extensions, UIntPtr.Zero, ) GL.BindTexture(TextureTarget.Texture2D, tex.textureID); //Custom CLFilter filter = _kernels["light"]; GL.GetTexLevelParameter(TextureTarget.Texture2D, 0, GetTextureParameter.TextureWidth, out float width); GL.GetTexLevelParameter(TextureTarget.Texture2D, 0, GetTextureParameter.TextureHeight, out float height); byte[] buffer = new byte[(int)(width * height * 4)]; GL.GetTexImage(TextureTarget.Texture2D, 0, PixelFormat.Bgra, PixelType.UnsignedByte, buffer); MemoryBuffer mb = c.CreateBuffer(MemoryFlag.ReadWrite | MemoryFlag.CopyHostPointer, buffer); filter.SetArg(0, mb); filter.SetArg(1, new int3((int)width, (int)height, 1)); filter.SetArg(2, 4); filter.SetArg(3, enablechannels); CommandQueue cq = CommandQueue.CreateCommandQueue(c, d); cq.EnqueueNDRangeKernel(filter.kernel, 1, buffer.Length); buffer = cq.EnqueueReadBuffer <byte>(mb, buffer.Length); GL.BindTexture(TextureTarget.Texture2D, tex.textureID); GL.TexSubImage2D(TextureTarget.Texture2D, 0, 0, 0, (int)width, (int)height, PixelFormat.Bgra, PixelType.UnsignedByte, buffer); }
public void RunOverlayKernel(GameTexture tex, GameTexture overlay, float weight) { GL.BindTexture(TextureTarget.Texture2D, tex.textureID); GL.GetTexLevelParameter(TextureTarget.Texture2D, 0, GetTextureParameter.TextureWidth, out float width); GL.GetTexLevelParameter(TextureTarget.Texture2D, 0, GetTextureParameter.TextureHeight, out float height); byte[] buffer = new byte[(int)(width * height * 4)]; GL.GetTexImage(TextureTarget.Texture2D, 0, PixelFormat.Bgra, PixelType.UnsignedByte, buffer); MemoryBuffer mb = c.CreateBuffer(MemoryFlag.CopyHostPointer, buffer); //Custom CLFilter filter = _kernels["overlay"]; byte[] bufferOverlay = new byte[(int)(width * height * 4)]; GL.BindTexture(TextureTarget.Texture2D, overlay.textureID); GL.GetTexImage(TextureTarget.Texture2D, 0, PixelFormat.Bgra, PixelType.UnsignedByte, bufferOverlay); MemoryBuffer mbOverlay = c.CreateBuffer(MemoryFlag.CopyHostPointer, bufferOverlay); filter.SetArg(0, mb); filter.SetArg(1, new int3((int)width, (int)height, 1)); filter.SetArg(2, 4); filter.SetArg(3, enablechannels); //Custom filter.SetArg(5, mbOverlay); filter.SetArg(6, weight); CommandQueue cq = CommandQueue.CreateCommandQueue(c, d); cq.EnqueueNDRangeKernel(filter.kernel, 1, buffer.Length); buffer = cq.EnqueueReadBuffer <byte>(mb, buffer.Length); GL.BindTexture(TextureTarget.Texture2D, tex.textureID); GL.TexSubImage2D(TextureTarget.Texture2D, 0, 0, 0, (int)width, (int)height, PixelFormat.Bgra, PixelType.UnsignedByte, buffer); }
public void RunCheckerBoardKernel(GameTexture tex, float length) { //Custom CLFilter filter = _kernels["checkerboard"]; //Get Width & Height GL.BindTexture(TextureTarget.Texture2D, tex.textureID); GL.GetTexLevelParameter(TextureTarget.Texture2D, 0, GetTextureParameter.TextureWidth, out float width); GL.GetTexLevelParameter(TextureTarget.Texture2D, 0, GetTextureParameter.TextureHeight, out float height); //Get Image source from opengl //Custom byte[] buffer = new byte[(int)(width * height * 4)]; GL.GetTexImage(TextureTarget.Texture2D, 0, PixelFormat.Bgra, PixelType.UnsignedByte, buffer); //Create buffer with image content MemoryBuffer mb = c.CreateBuffer(MemoryFlag.ReadWrite | MemoryFlag.CopyHostPointer, buffer); filter.SetArg(0, mb); filter.SetArg(1, new int3((int)width, (int)height, 1)); filter.SetArg(2, 4); filter.SetArg(3, enablechannels); //Custom filter.SetArg(3, length); filter.SetArg(4, (int)width); CommandQueue cq = CommandQueue.CreateCommandQueue(c, d); cq.EnqueueNDRangeKernel(filter.kernel, 1, buffer.Length); buffer = cq.EnqueueReadBuffer <byte>(mb, buffer.Length); GL.BindTexture(TextureTarget.Texture2D, tex.textureID); GL.TexSubImage2D(TextureTarget.Texture2D, 0, 0, 0, (int)width, (int)height, PixelFormat.Bgra, PixelType.UnsignedByte, buffer); }
public GameTexture GetPerlin(int width, int height, int octaves, float persistence) { CLFilter filter = _kernels["perlin"]; byte[] buffer = new byte[width * height * 4]; Random rnd = new Random(); for (int i = 0; i < buffer.Length; i++) { if (i % 4 == 0) { byte val = (byte)rnd.Next(0, 255); buffer[i] = buffer[i + 1] = buffer[i + 2] = val; buffer[i + 3] = 255; // Alpha } } MemoryBuffer mb = c.CreateBuffer(MemoryFlag.ReadWrite | MemoryFlag.CopyHostPointer, buffer); filter.SetArg(0, mb); int3 dims = new int3(width, height, 1); filter.SetArg(1, dims); filter.SetArg(2, 4); filter.SetArg(3, enablechannels); filter.SetArg(4, persistence); filter.SetArg(5, octaves); CommandQueue cq = CommandQueue.CreateCommandQueue(c, d); cq.EnqueueNDRangeKernel(filter.kernel, 1, buffer.Length); buffer = cq.EnqueueReadBuffer <byte>(mb, buffer.Length); return(GameTexture.Load(buffer, width, height)); }