| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- unit fPostProcessingD;
- interface
- uses
- Winapi.OpenGL,
- Winapi.OpenGLext,
- System.SysUtils,
- System.Variants,
- System.Classes,
- Vcl.Graphics,
- Vcl.Controls,
- Vcl.Forms,
- Vcl.Dialogs,
- Vcl.ComCtrls,
- GLS.TextureFormat
- GLS.Scene,
- GLS.Coordinates,
- GLS.Objects,
- GLS.GeomObjects,
- GLS.Material,
- GLS.SimpleNavigation,
- GLS.Cadencer,
- GLS.SceneViewer,
-
- GLS.BaseClasses,
- GLS.FBORenderer,
- GLS.HUDObjects,
- CUDA.APIComps,
- CUDA.Graphics,
- CUDA.Compiler,
- CUDA.Context,
- GLS.State,
- GLS.RenderContextInfo,
- GLS.Context,
- GLSL.CustomShader,
- GLSL.Shader,
- GLS.Utils,
- GLS.Texture;
- type
- TFormPP = class(TForm)
- GLScene1: TGLScene;
- GLSceneViewer1: TGLSceneViewer;
- GLCadencer1: TGLCadencer;
- GLSimpleNavigation1: TGLSimpleNavigation;
- GLMaterialLibrary1: TGLMaterialLibrary;
- GLCamera1: TGLCamera;
- GLTeapot1: TGLTeapot;
- GLLightSource1: TGLLightSource;
- RenderRoot: TGLDummyCube;
- GLCylinder1: TGLCylinder;
- RenderToTexture: TGLFBORenderer;
- GLCUDADevice1: TGLCUDADevice;
- GLCUDA1: TGLCUDA;
- GLCUDACompiler1: TGLCUDACompiler;
- MainModule: TCUDAModule;
- processedTextureMapper: TCUDAImageResource;
- CallPostProcess: TGLDirectOpenGL;
- GLCapsule1: TGLCapsule;
- ResultShader: TGLSLShader;
- processedTextureArray: TCUDAMemData;
- outputBuffer: TCUDAMemData;
- inputBuffer: TCUDAMemData;
- CommonShader: TGLSLShader;
- GLSphere1: TGLSphere;
- TrackBar1: TTrackBar;
- GLHUDSprite1: TGLHUDSprite;
- cudaProcess: TCUDAFunction;
- cudaProcess_k_g_data: TCUDAFuncParam;
- cudaProcess_k_g_odata: TCUDAFuncParam;
- cudaProcess_k_imgw: TCUDAFuncParam;
- cudaProcess_k_imgh: TCUDAFuncParam;
- cudaProcess_k_tilew: TCUDAFuncParam;
- cudaProcess_k_r: TCUDAFuncParam;
- cudaProcess_k_threshold: TCUDAFuncParam;
- cudaProcess_k_highlight: TCUDAFuncParam;
- procedure FormResize(Sender: TObject);
- procedure GLCadencer1Progress(Sender: TObject; const deltaTime,
- newTime: Double);
- procedure cudaProcessParameterSetup(Sender: TObject);
- procedure FormCreate(Sender: TObject);
- procedure CallPostProcessRender(Sender: TObject;
- var rci: TGLRenderContextInfo);
- procedure ResultShaderApply(Shader: TGLCustomGLSLShader);
- procedure RenderToTextureBeforeRender(Sender: TObject; var rci: TGLRenderContextInfo);
- procedure RenderToTextureAfterRender(Sender: TObject; var rci: TGLRenderContextInfo);
- procedure TrackBar1Change(Sender: TObject);
- procedure GLCUDA1OpenGLInteropInit(out Context: TGLContext);
- private
- Path: TFileName;
- public
- Radius: Integer;
- Threshold: Single;
- Highlight: SIngle;
- end;
- var
- FormPP: TFormPP;
- implementation
- {$R *.dfm}
- procedure TFormPP.FormCreate(Sender: TObject);
- begin
- Path := GetCurrentAssetPath();
- // Load lena image as texture
- SetCurrentDir(Path + '\texture');
- Radius := 8;
- Threshold := 0.8;
- Highlight := 0.4;
- with GLMaterialLibrary1.TextureByName('processedTexture') do
- begin
- TGLBlankImage(Image).ColorFormat := GL_RGB_INTEGER;
- Disabled := false;
- end;
- GLHUDSprite1.Visible := True;
- end;
- procedure TFormPP.CallPostProcessRender(Sender: TObject;
- var rci: TGLRenderContextInfo);
- begin
- processedTextureMapper.MapResources;
- processedTextureMapper.BindArrayToTexture(processedTextureArray, 0, 0);
- processedTextureArray.CopyTo(inputBuffer);
- cudaProcess.Launch;
- outputBuffer.CopyTo(processedTextureArray);
- processedTextureMapper.UnMapResources;
- end;
- procedure TFormPP.cudaProcessParameterSetup(Sender: TObject);
- begin
- with cudaProcess do
- begin
- SharedMemorySize :=
- (BlockShape.SizeX+(2*Radius))*(BlockShape.SizeY+(2*Radius))*sizeof(Integer);
- SetParam(inputBuffer);
- SetParam(outputBuffer);
- with GLMaterialLibrary1.TextureByName('processedTexture') do
- begin
- SetParam(TexWidth);
- SetParam(TexHeight);
- end;
- SetParam(BlockShape.SizeX + 2*Radius);
- SetParam(Radius);
- SetParam(Threshold);
- SetParam(Highlight);
- end;
- end;
- procedure TFormPP.FormResize(Sender: TObject);
- begin
- GLCamera1.SceneScale := GLSceneViewer1.Width / GLSceneViewer1.Height;
- end;
- procedure TFormPP.GLCadencer1Progress(Sender: TObject; const deltaTime,
- newTime: Double);
- begin
- GLSceneViewer1.Invalidate;
- end;
- procedure TFormPP.RenderToTextureBeforeRender(Sender: TObject; var rci: TGLRenderContextInfo);
- begin
- CommonShader.Apply(rci, Self);
- end;
- procedure TFormPP.GLCUDA1OpenGLInteropInit(out Context: TGLContext);
- begin
- Context := GLSceneViewer1.Buffer.RenderingContext;
- end;
- procedure TFormPP.RenderToTextureAfterRender(Sender: TObject; var rci: TGLRenderContextInfo);
- begin
- CommonShader.UnApply(rci);
- end;
- procedure TFormPP.ResultShaderApply(Shader: TGLCustomGLSLShader);
- begin
- with CurrentGLContext.GLStates do
- begin
- Disable(stDepthTest);
- DepthWriteMask := False;
- end;
- Shader.Param['TexUnit0'].AsTexture[0] :=
- GLMaterialLibrary1.TextureByName('processedTexture');
- end;
- procedure TFormPP.TrackBar1Change(Sender: TObject);
- begin
- Radius := TrackBar1.Position;
- end;
- end.
|