initial GLSL1.2 test version
authorWolfgang Steffens <WolfgangSteff@gmail.com>
Fri, 25 May 2012 09:00:54 +0200
changeset 7129 0ce4f478ea6c
parent 7127 8e5e3fef16fc
child 7136 6f3c9ec79338
initial GLSL1.2 test version
hedgewars/options.inc
hedgewars/uStore.pas
hedgewars/uTypes.pas
hedgewars/uVariables.pas
share/hedgewars/Data/CMakeLists.txt
share/hedgewars/Data/Shaders/CMakeLists.txt
share/hedgewars/Data/Shaders/default.fs
share/hedgewars/Data/Shaders/default.vs
--- a/hedgewars/options.inc	Fri May 25 08:51:54 2012 +0200
+++ b/hedgewars/options.inc	Fri May 25 09:00:54 2012 +0200
@@ -28,6 +28,8 @@
 {$MACRO ON}
 {$DEFINE GLunit:=GL}
 
+{$DEFINE GL2}
+
 {$IFDEF ANDROID}
     {$DEFINE SDL13}
     {$DEFINE HWLIBRARY}
--- a/hedgewars/uStore.pas	Fri May 25 08:51:54 2012 +0200
+++ b/hedgewars/uStore.pas	Fri May 25 09:00:54 2012 +0200
@@ -58,6 +58,9 @@
 {$ELSE}
     SDLPrimSurface: PSDL_Surface;
 {$ENDIF}
+{$IFDEF GL2}
+    Shader: GLuint;
+{$ENDIF}
 
 function WriteInRect(Surface: PSDL_Surface; X, Y: LongInt; Color: LongWord; Font: THWFont; s: ansistring): TSDL_Rect;
 var w, h: LongInt;
@@ -626,6 +629,106 @@
     SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1); // try to prefer hardware rendering
 end;
 
+{$IFDEF GL2}
+function CompileShader(shaderFile: string; shaderType: GLenum): GLuint;
+var
+    shader: GLuint;
+    f: Textfile;
+    source, line: AnsiString;
+    sourceA: Pchar;
+    lengthA: GLint;
+    compileResult: GLint;
+    logLength: GLint;
+    log: PChar;
+begin
+    Assign(f, Pathz[ptShaders] + '/' + shaderFile);
+    filemode:= 0; // readonly
+    Reset(f);
+    if IOResult <> 0 then
+    begin
+        AddFileLog('Unable to load ' + shaderFile);
+        halt(-1);
+    end;
+
+    source:='';
+    while not eof(f) do
+    begin
+        ReadLn(f, line);
+        source:= source + line + #10;
+    end;
+    
+    CloseFile(f);
+
+    writeln('Compiling shader: ' + Pathz[ptShaders] + '/' + shaderFile);
+
+    sourceA:=PChar(source);
+    lengthA:=Length(source);
+
+    shader:=glCreateShader(shaderType);
+    glShaderSource(shader, 1, @sourceA, @lengthA);
+    glCompileShader(shader);
+    glGetShaderiv(shader, GL_COMPILE_STATUS, @compileResult);
+    glGetShaderiv(shader, GL_INFO_LOG_LENGTH, @logLength);
+
+    if logLength > 1 then
+    begin
+        GetMem(log, logLength);
+        glGetShaderInfoLog(shader, logLength, nil, log);
+        writeln('========== Compiler log  ==========');
+        writeln(log);
+        writeln('===================================');
+        FreeMem(log, logLength);
+    end;
+
+    if compileResult <> GL_TRUE then
+    begin
+        writeln('Shader compilation failed, halting');
+        halt(-1);
+    end;
+
+    CompileShader:= shader;
+end;
+
+function CompileProgram(shaderName: string): GLuint;
+var
+    program_: GLuint;
+    vs, fs: GLuint;
+    linkResult: GLint;
+    logLength: GLint;
+    log: PChar;
+begin
+    program_:= glCreateProgram();
+    vs:= CompileShader(shaderName + '.vs', GL_VERTEX_SHADER);
+    fs:= CompileShader(shaderName + '.fs', GL_FRAGMENT_SHADER);
+    glAttachShader(program_, vs);
+    glAttachShader(program_, fs);
+    glLinkProgram(program_);
+    glDeleteShader(vs);
+    glDeleteShader(fs);
+
+    glGetProgramiv(program_, GL_LINK_STATUS, @linkResult);
+    glGetProgramiv(program_, GL_INFO_LOG_LENGTH, @logLength);
+
+    if logLength > 1 then
+    begin
+        GetMem(log, logLength);
+        glGetProgramInfoLog(program_, logLength, nil, log);
+        writeln('========== Compiler log  ==========');
+        writeln(log);
+        writeln('===================================');
+        FreeMem(log, logLength);
+    end;
+
+    if linkResult <> GL_TRUE then
+    begin
+        writeln('Linking program failed, halting');
+        halt(-1);
+    end;
+
+    CompileProgram:= program_;
+end;
+{$ENDIF}
+
 procedure SetupOpenGL;
 //var vendor: shortstring = '';
 var buf: array[byte] of char;
@@ -683,6 +786,13 @@
     AddFileLog('  \----- Extensions: ' + shortstring(pchar(glGetString(GL_EXTENSIONS))));
     //TODO: don't have the Extensions line trimmed but slipt it into multiple lines
 
+{$IFDEF GL2}
+    Load_GL_VERSION_2_0;
+    Shader:= CompileProgram('default');
+    glUseProgram(Shader);
+    glUniform1i(glGetUniformLocation(Shader, 'tex'), 0);
+{$ENDIF}
+
 {$IFNDEF S3D_DISABLED}
     if (cStereoMode = smHorizontal) or (cStereoMode = smVertical) or (cStereoMode = smAFR) then
     begin
@@ -1151,6 +1261,9 @@
 
 procedure freeModule;
 begin
+{$IFDEF GL2}
+    glDeleteProgram(Shader);
+{$ENDIF}
     StoreRelease(false);
     TTF_Quit();
 {$IFDEF SDL13}
--- a/hedgewars/uTypes.pas	Fri May 25 08:51:54 2012 +0200
+++ b/hedgewars/uTypes.pas	Fri May 25 09:00:54 2012 +0200
@@ -44,7 +44,8 @@
     // Different files are stored in different folders, this enumeration is used to tell which folder to use
     TPathType = (ptNone, ptData, ptGraphics, ptThemes, ptCurrTheme, ptTeams, ptMaps,
             ptMapCurrent, ptDemos, ptSounds, ptGraves, ptFonts, ptForts,
-            ptLocale, ptAmmoMenu, ptHedgehog, ptVoices, ptHats, ptFlags, ptMissionMaps, ptSuddenDeath, ptButtons);
+            ptLocale, ptAmmoMenu, ptHedgehog, ptVoices, ptHats, ptFlags, ptMissionMaps, ptSuddenDeath, ptButtons,
+            ptShaders);
 
     // Available sprites for displaying stuff
     TSprite = (sprWater, sprCloud, sprBomb, sprBigDigit, sprFrame,
--- a/hedgewars/uVariables.pas	Fri May 25 08:51:54 2012 +0200
+++ b/hedgewars/uVariables.pas	Fri May 25 09:00:54 2012 +0200
@@ -225,8 +225,9 @@
         'Graphics/Hats',                 // ptHats
         'Graphics/Flags',                // ptFlags
         'Missions/Maps',                 // ptMissionMaps
-        'Graphics/SuddenDeath',           // ptSuddenDeath
-        'Graphics/Buttons'                // ptButton
+        'Graphics/SuddenDeath',          // ptSuddenDeath
+        'Graphics/Buttons',              // ptButton
+        'Shaders'                        // ptShaders
     );
 
     Fontz: array[THWFont] of THHFont = (
--- a/share/hedgewars/Data/CMakeLists.txt	Fri May 25 08:51:54 2012 +0200
+++ b/share/hedgewars/Data/CMakeLists.txt	Fri May 25 09:00:54 2012 +0200
@@ -1,3 +1,3 @@
-foreach(dir "Fonts" "Forts" "Graphics" "Locale" "Maps" "Music" "Sounds" "Themes" "Missions" "Names" "misc" "Scripts")
+foreach(dir "Fonts" "Forts" "Graphics" "Locale" "Maps" "Music" "Sounds" "Themes" "Missions" "Names" "misc" "Scripts" "Shaders")
   add_subdirectory(${dir})
 endforeach(dir)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/share/hedgewars/Data/Shaders/CMakeLists.txt	Fri May 25 09:00:54 2012 +0200
@@ -0,0 +1,5 @@
+file(GLOB BaseShaders *.vs *.fs)
+
+install(FILES
+	${BaseShaders}
+	DESTINATION ${SHAREPATH}Data/Shaders)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/share/hedgewars/Data/Shaders/default.fs	Fri May 25 09:00:54 2012 +0200
@@ -0,0 +1,9 @@
+// !!!just testing!!! 
+// This is not GLSL 1.3+ compatible, as its using the compatibility profile!
+uniform sampler2D tex0;
+varying vec4 tint;
+
+void main()
+{
+    gl_FragColor = texture2D(tex0, gl_TexCoord[0].st) * tint;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/share/hedgewars/Data/Shaders/default.vs	Fri May 25 09:00:54 2012 +0200
@@ -0,0 +1,10 @@
+// !!!just testing!!! 
+// This is not GLSL 1.3+ compatible, as its using the compatibility profile!
+varying vec4 tint;
+
+void main()
+{
+    gl_Position = ftransform();
+    gl_TexCoord[0] = gl_MultiTexCoord0;
+    tint = gl_Color;
+}