public static cudaArray_t CreateCudaArray(cudaChannelFormatDesc channelDescTex, IntPtr src, int width, int height) { cudaArray_t cuArrayTex; cuda.MallocArray(out cuArrayTex, ref channelDescTex, width, height, cudaMallocArrayFlags.cudaArrayDefault); cuda.MemcpyToArray(cuArrayTex, 0, 0, src, width * height * sizeof(float), cudaMemcpyKind.cudaMemcpyHostToDevice); return(cuArrayTex); }
protected override void OnLoad(System.EventArgs e) { try { Console.WriteLine(ClientSize.Width + " " + ClientSize.Height); Console.WriteLine(GL.GetString(StringName.Version)); Console.WriteLine(GL.GetString(StringName.ShadingLanguageVersion)); GL.Viewport(0, 0, ClientSize.Width, ClientSize.Height); VSync = VSyncMode.On; GL.Enable(EnableCap.Texture2D); GL.Enable(EnableCap.DepthTest); GL.DepthFunc(DepthFunction.Less); // Setup quad VAO quadVAO = GL.GenVertexArray(); int quadVBO = GL.GenBuffer(); GL.BindVertexArray(quadVAO); GL.BindBuffer(BufferTarget.ArrayBuffer, quadVBO); GL.BufferData(BufferTarget.ArrayBuffer, quadVertices.Length * sizeof(float), quadVertices, BufferUsageHint.StaticDraw); GL.EnableVertexAttribArray(0); GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, 5 * sizeof(float), 0); GL.EnableVertexAttribArray(1); GL.VertexAttribPointer(1, 2, VertexAttribPointerType.Float, false, 5 * sizeof(float), 3 * sizeof(float)); uchar4[] data = new uchar4[Width * Height]; textureID = GL.GenTexture(); GL.BindTexture(TextureTarget.Texture2D, textureID); GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, Width, Height, 0, PixelFormat.Bgra, PixelType.UnsignedByte, data); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, Convert.ToInt32(TextureWrapMode.Repeat)); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, Convert.ToInt32(TextureWrapMode.Repeat)); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, Convert.ToInt32(TextureMinFilter.Linear)); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, Convert.ToInt32(TextureMagFilter.Linear)); GL.BindTexture(TextureTarget.Texture2D, 0); string vsource = File.ReadAllText(@"vertex.glsl"); string fsource = File.ReadAllText(@"fragment.glsl"); shaderProgram = LoadShaderProgram(vsource, fsource); IntPtr resource; cuda.ERROR_CHECK(cuda.GraphicsGLRegisterImage(out resource, (uint)textureID, (uint)GL_TEXTURE_MODE.GL_TEXTURE_2D, (uint)cudaGraphicsRegisterFlags.SurfaceLoadStore)); cuda.ERROR_CHECK(cuda.GraphicsMapResources(1, new IntPtr[1] { resource }, cudaStream_t.NO_STREAM)); cudaArray_t array; cuda.ERROR_CHECK(cuda.GraphicsSubResourceGetMappedArray(out array, resource, 0, 0)); cudaChannelFormatDesc channelDescSurf = TextureHelpers.cudaCreateChannelDesc(32, 0, 0, 0, cudaChannelFormatKind.cudaChannelFormatKindUnsigned); cudaResourceDesc resDescSurf = TextureHelpers.CreateCudaResourceDesc(array); cuda.CreateSurfaceObject(out surface, ref resDescSurf); } catch (Exception ex) { Console.WriteLine(ex.Message); } }
static void Main(string[] args) { HybRunner runner = HybRunner.Cuda().SetDistrib(32, 32, 16, 16, 1, 0); GrayBitmap image = GrayBitmap.Load("../../images/lena512.bmp"); uint height = image.Height, width = image.Width; ushort[] inputPixels = image.PixelsUShort; float[] imageFloat = new float[width * height]; float[] imageCompute = new float[width * height]; for (int i = 0; i < width * height; ++i) { imageFloat[i] = (float)inputPixels[i]; } IntPtr src = runner.Marshaller.MarshalManagedToNative(imageFloat); //bind texture cudaChannelFormatDesc channelDescTex = TextureHelpers.cudaCreateChannelDesc(32, 0, 0, 0, cudaChannelFormatKind.cudaChannelFormatKindFloat); cudaArray_t cuArrayTex = TextureHelpers.CreateCudaArray(channelDescTex, src, (int)width, (int)height); cudaResourceDesc resDescTex = TextureHelpers.CreateCudaResourceDesc(cuArrayTex); //create Texture descriptor cudaTextureDesc texDesc = TextureHelpers.CreateCudaTextureDesc(); //create Texture object cudaTextureObject_t texObj; cuda.CreateTextureObject(out texObj, ref resDescTex, ref texDesc); //create and bind surface dynamic wrapper = runner.Wrap(new Program()); wrapper.Sobel(texObj, imageCompute, (int)width, (int)height); ushort[] outputPixel = new ushort[width * height]; for (int i = 0; i < width * height; ++i) { outputPixel[i] = (ushort)imageCompute[i]; } GrayBitmap imageSobel = new GrayBitmap(width, height); imageSobel.PixelsUShort = outputPixel; imageSobel.Save("../../output-03-surface/sobel.bmp"); }
static void Main(string[] args) { HybRunner runner = HybRunner.Cuda().SetDistrib(32, 32, 16, 16, 1, 0); GrayBitmap image = GrayBitmap.Load("../../images/lena512.bmp"); uint height = image.Height, width = image.Width; ushort[] inputPixels = image.PixelsUShort; float[] imageFloat = new float[width * height]; for (int i = 0; i < width * height; ++i) { imageFloat[i] = (float)inputPixels[i]; } IntPtr src = runner.Marshaller.MarshalManagedToNative(imageFloat); //bind texture cudaChannelFormatDesc channelDescTex = TextureHelpers.cudaCreateChannelDesc(32, 0, 0, 0, cudaChannelFormatKind.cudaChannelFormatKindFloat); cudaArray_t cuArrayTex = TextureHelpers.CreateCudaArray(channelDescTex, src, (int)width, (int)height); cudaResourceDesc resDescTex = TextureHelpers.CreateCudaResourceDesc(cuArrayTex); //create Texture descriptor cudaTextureDesc texDesc = TextureHelpers.CreateCudaTextureDesc(); //create Texture object cudaTextureObject_t texObj; cuda.CreateTextureObject(out texObj, ref resDescTex, ref texDesc); //bind surface cudaChannelFormatDesc channelDescSurf = TextureHelpers.cudaCreateChannelDesc(32, 0, 0, 0, cudaChannelFormatKind.cudaChannelFormatKindFloat); cudaArray_t cuArraySurf; cuda.MallocArray(out cuArraySurf, ref channelDescSurf, width, height, cudaMallocArrayFlags.cudaArraySurfaceLoadStore); //create cudaResourceDesc for surface cudaResourceDesc resDescSurf = TextureHelpers.CreateCudaResourceDesc(cuArraySurf); //create surface object cudaSurfaceObject_t surfObj; cuda.CreateSurfaceObject(out surfObj, ref resDescSurf); dynamic wrapper = runner.Wrap(new Program()); wrapper.Sobel(texObj, surfObj, (int)width, (int)height); //pinned float array to allow the copy of the surface object on the host float[] imageCompute = new float[width * height]; GCHandle handle = GCHandle.Alloc(imageCompute, GCHandleType.Pinned); IntPtr dest = handle.AddrOfPinnedObject(); cuda.MemcpyFromArray(dest, cuArraySurf, 0, 0, width * height * sizeof(float), cudaMemcpyKind.cudaMemcpyDeviceToHost); ushort[] outputPixel = new ushort[width * height]; for (int i = 0; i < width * height; ++i) { outputPixel[i] = (ushort)imageCompute[i]; } GrayBitmap imageSobel = new GrayBitmap(width, height); imageSobel.PixelsUShort = outputPixel; imageSobel.Save("../../output-3-surface/sobel.bmp"); }
unsafe static void Main(string[] args) { HybRunner runner = HybRunner.Cuda(@"Textures and Surfaces_CUDA.dll").SetDistrib(32, 32, 16, 16, 1, 0); Bitmap baseImage = (Bitmap)Image.FromFile("lena512.bmp"); int height = baseImage.Height, width = baseImage.Width; byte[] inputPixels = new byte[width * height]; ReadImage(inputPixels, baseImage, width, height); float[] imagefloat = new float[width * height]; for (int i = 0; i < width * height; ++i) { imagefloat[i] = (float)inputPixels[i]; } IntPtr src = runner.Marshaller.MarshalManagedToNative(imagefloat); //bind texture cudaChannelFormatDesc channelDescTex = TextureHelpers.cudaCreateChannelDesc(32, 0, 0, 0, cudaChannelFormatKind.cudaChannelFormatKindFloat); cudaArray_t cuArrayTex = TextureHelpers.CreateCudaArray(channelDescTex, src, width, height); cudaResourceDesc resDescTex = TextureHelpers.CreateCudaResourceDesc(cuArrayTex); //create Texture descriptor cudaTextureDesc texDesc = TextureHelpers.CreateCudaTextureDesc(); //create Texture object cudaTextureObject_t texObj; cuda.CreateTextureObject(out texObj, ref resDescTex, ref texDesc); //bind surface cudaChannelFormatDesc channelDescSurf = TextureHelpers.cudaCreateChannelDesc(32, 0, 0, 0, cudaChannelFormatKind.cudaChannelFormatKindFloat); cudaArray_t cuArraySurf; cuda.MallocArray(out cuArraySurf, ref channelDescSurf, width, height, cudaMallocArrayFlags.cudaArraySurfaceLoadStore); //create cudaResourceDesc cudaResourceDesc resDescSurf = TextureHelpers.CreateCudaResourceDesc(cuArraySurf); //create surface object cudaSurfaceObject_t surfObj; cuda.CreateSurfaceObject(out surfObj, ref resDescSurf); dynamic wrapper = runner.Wrap(new Program()); // call kernek wrapper.Sobel(texObj, surfObj, width, height); float[] imageSobel = new float[width * height]; for (int i = 0; i < width * height; ++i) { imageSobel[i] = 128.0F; } GCHandle handle = GCHandle.Alloc(imageSobel, GCHandleType.Pinned); IntPtr dest = handle.AddrOfPinnedObject(); cuda.MemcpyFromArray(dest, cuArraySurf, 0, 0, width * height * sizeof(float), cudaMemcpyKind.cudaMemcpyDeviceToHost); byte[] imageSobelByte = new byte[width * height]; for (int i = 0; i < width * height; ++i) { imageSobelByte[i] = (byte)imageSobel[i]; } SaveImage("lenaSobel512.bmp", imageSobelByte, width, height); cuda.DestroySurfaceObject(surfObj); cuda.DestroyTextureObject(texObj); }