PDA

View Full Version : Create Shader in Dll



serkank
22-10-2014, 07:34 PM
I found here Sample Code about DGLOpenGL and DGLShader.
and i am trying to use Opengl Some Effect . For this i am trying to do Effect Plugin and i want create Shader in Plugin
But when you try to create shader in Plugin Dll its make error.

i found This code. its work there is not problem



begin


InitOpenGL;


DC := GetDC ( form1.Handle );
ShowCursor ( FALSE );
pfd.dwFlags := PFD_DRAW_TO_WINDOW OR PFD_SUPPORT_OPENGL OR PFD_DOUBLEBUFFER;
SetPixelFormat ( DC, ChoosePixelFormat ( DC, @pfd ), @pfd );
ActivateRenderingContext( DC, wglCreateContext ( DC ), TRUE );


DemoStart := GetTickCount;
ElapsedTime:=1;
//load simple glsl shader




glslsimpleprog := TGLSLProgram.Create();
glslsimplevert := TGLSLShader.Create('simple.vert');
glslsimplefrag := TGLSLShader.Create('simple.frag', GL_FRAGMENT_SHADER_ARB);
glslsimpleprog.Attach(glslsimplevert);
glslsimpleprog.Attach(glslsimplefrag);
glslsimpleprog.Link;
glslsimpleprog.Enable;
glslsimpleprog.Set2F('resolution',640*2,480*2);
glslsimpleprog.Set2F('mouse',0,0);




WHILE GetAsyncKeyState ( 27 ) = 0 DO
BEGIN
glslsimpleprog.setf('time', elapsedtime/320);
glLoadIdentity;
glBegin ( GL_QUADS );
glTexCoord2f ( 0.0, 0.0 ); glVertex3f ( -320, -240, 0 );
glTexCoord2f ( 1.0, 0.0 ); glVertex3f ( 320, -240, 0 );
glTexCoord2f ( 1.0, 1.0 ); glVertex3f ( 320, 240, 0 );
glTexCoord2f ( 0.0, 1.0 ); glVertex3f ( -320, 240, 0 );
glEnd;
LastTime := ElapsedTime;
{ Calculate Elapsed Time }
ElapsedTime := GetTickCount - DemoStart;
{ average it out for smoother movement }
ElapsedTime := ( LastTime + ElapsedTime ) DIV 2;


SwapBuffers ( DC )
END ;


end;

Ok. if You change this code and if you Move Shader in Dll Like bellow; its make error EXTERNAL SIGSEGV

Some One have any idea how to i can create shader in dll ?

MainApplication



VAR
DemoStart, LastTime : DWORD;
ElapsedTime : INTEGER;
glslsimplevert,
glslsimplefrag : TGLSLShader;
glslsimpleprog : TGLSLProgram;
pfd : TPixelFormatDescriptor;
DC : HDC;



begin


InitOpenGL;


DC := GetDC ( form1.Handle );
ShowCursor ( FALSE );
pfd.dwFlags := PFD_DRAW_TO_WINDOW OR PFD_SUPPORT_OPENGL OR PFD_DOUBLEBUFFER;
SetPixelFormat ( DC, ChoosePixelFormat ( DC, @pfd ), @pfd );
ActivateRenderingContext( DC, wglCreateContext ( DC ), TRUE );


DemoStart := GetTickCount;
ElapsedTime:=1;
//load simple glsl shader




glslsimpleprog:=GLRender(glslsimpleprog); // i wrote GLRender Function in a DLL




WHILE GetAsyncKeyState ( 27 ) = 0 DO
BEGIN
glslsimpleprog.setf('time', elapsedtime/320);
glLoadIdentity;
glBegin ( GL_QUADS );
glTexCoord2f ( 0.0, 0.0 ); glVertex3f ( -320, -240, 0 );
glTexCoord2f ( 1.0, 0.0 ); glVertex3f ( 320, -240, 0 );
glTexCoord2f ( 1.0, 1.0 ); glVertex3f ( 320, 240, 0 );
glTexCoord2f ( 0.0, 1.0 ); glVertex3f ( -320, 240, 0 );
glEnd;
LastTime := ElapsedTime;
{ Calculate Elapsed Time }
ElapsedTime := GetTickCount - DemoStart;
{ average it out for smoother movement }
ElapsedTime := ( LastTime + ElapsedTime ) DIV 2;


SwapBuffers ( DC )
END ;


end;


DLL


library dll2;


{$mode objfpc}{$H+}


uses
Classes ,windows , DGLShader,DGLOpengl
{ you can add units after this };




VAR
glslsimplevert,
glslsimplefrag : TGLSLShader;
glslsimpleprog : TGLSLProgram;






Function GLRender(glslsimpleprog:TGLSLProgram):TGLSLProgram ;stdcall;
begin


glslsimpleprog := TGLSLProgram.Create();
glslsimplevert := TGLSLShader.Create('simple.vert');
glslsimplefrag := TGLSLShader.Create('simple.frag', GL_FRAGMENT_SHADER_ARB);
glslsimpleprog.Attach(glslsimplevert);
glslsimpleprog.Attach(glslsimplefrag);
glslsimpleprog.Link;
glslsimpleprog.Enable;
glslsimpleprog.Set2F('resolution',640*2,480*2);
glslsimpleprog.Set2F('mouse',0,0);
result:= glslsimpleprog;
end;


exports
GLRender;
begin
end.



Result:=glCreateProgramObjectARB(); this is make error ...



// TGLSLProgram
constructor TGLSLProgram.Create();
function GETPO : Integer;
begin
Result:=glCreateProgramObjectARB();// if create program in DLL this is make Error EXERNAL SIGSEGV
end;


begin
FProgramObject:=GETPO;


// FProgramObject := glCreateProgramObjectARB
end;

laggyluk
23-10-2014, 05:53 AM
I don't know if it will help but in dll you have glslsimpleprog var declared and then you use same name and type in GLRender as parameter.

serkank
25-10-2014, 12:26 PM
Thank you. yes its true and i found another problems to ..
i was tested it yesterday. and i fix error .

1 InitOpenGL ... in my dll i dint use initOpengl.. This Function LoadOpenGl DLL..
2 ActiveRendering Context.. ThisFunction must use in DLL...
3 glslsimpleprog := TGLSLProgram.Create(); ... before i create this variable only in dll or only in main application. When i create this variable in mainapplication and in dll its work..
4. if i create first Shader and later some Component for Plugın Gui its make error.. But if firstly create shader and last create Component for Plugin ui its work ...



I don't know if it will help but in dll you have glslsimpleprog var declared and then you use same name and type in GLRender as parameter.