From c446e8186afa31c8d346383c0db8b8070fa563cd Mon Sep 17 00:00:00 2001
From: chai <chaifix@163.com>
Date: Fri, 4 Jan 2019 22:54:59 +0800
Subject: =?UTF-8?q?*=E7=BC=96=E8=AF=91jsl=20shader=E5=A4=B1=E8=B4=A5?=
 =?UTF-8?q?=E6=97=B6=E4=BD=BF=E7=94=A8error=20shader?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 bin/jin.exe                                        | Bin 572416 -> 573440 bytes
 build/vc++/libjin-lua/libjin-lua.vcxproj           |   2 +-
 build/vc++/libjin-lua/libjin-lua.vcxproj.filters   |   9 +-
 samples/post-processing/shaders/rgb-split.jsl      |   2 +-
 src/3rdparty/luax/luax.h                           |  14 ++-
 src/libjin-lua/common/je_lua_common.h              |   1 -
 src/libjin-lua/common/je_lua_error.h               |  30 ------
 src/libjin-lua/je_lua_jin.cpp                      |   2 +
 src/libjin-lua/modules/audio/je_lua_audio.cpp      |   4 +-
 .../modules/graphics/je_lua_graphics.cpp           |  30 ++++--
 src/libjin-lua/scripts/audio/audio.lua             |   5 +
 src/libjin-lua/scripts/audio/audio.lua.h           |   6 ++
 src/libjin-lua/scripts/graphics/graphics.lua       |  48 +++++++++-
 src/libjin-lua/scripts/graphics/graphics.lua.h     | 104 +++++++++++++++------
 src/libjin/graphics/shaders/je_shader.cpp          |   4 +-
 15 files changed, 175 insertions(+), 86 deletions(-)
 delete mode 100644 src/libjin-lua/common/je_lua_error.h
 create mode 100644 src/libjin-lua/scripts/audio/audio.lua
 create mode 100644 src/libjin-lua/scripts/audio/audio.lua.h

diff --git a/bin/jin.exe b/bin/jin.exe
index dd2f048..1478b8e 100644
Binary files a/bin/jin.exe and b/bin/jin.exe differ
diff --git a/build/vc++/libjin-lua/libjin-lua.vcxproj b/build/vc++/libjin-lua/libjin-lua.vcxproj
index d19961d..c680269 100644
--- a/build/vc++/libjin-lua/libjin-lua.vcxproj
+++ b/build/vc++/libjin-lua/libjin-lua.vcxproj
@@ -209,7 +209,6 @@
     <ClInclude Include="..\..\..\src\libjin-lua\common\je_lua.h" />
     <ClInclude Include="..\..\..\src\libjin-lua\common\je_lua_common.h" />
     <ClInclude Include="..\..\..\src\libjin-lua\common\je_lua_constant.h" />
-    <ClInclude Include="..\..\..\src\libjin-lua\common\je_lua_error.h" />
     <ClInclude Include="..\..\..\src\libjin-lua\common\je_lua_callback.h" />
     <ClInclude Include="..\..\..\src\libjin-lua\common\je_lua_function.h" />
     <ClInclude Include="..\..\..\src\libjin-lua\common\je_lua_object.h" />
@@ -268,6 +267,7 @@
   <ItemGroup>
     <None Include="..\..\..\src\libjin-lua\scripts\ai\ai.lua" />
     <None Include="..\..\..\src\libjin-lua\scripts\ai\state_machine.lua" />
+    <None Include="..\..\..\src\libjin-lua\scripts\audio\audio.lua" />
     <None Include="..\..\..\src\libjin-lua\scripts\log.lua" />
     <None Include="..\..\..\src\libjin-lua\scripts\physics\physics.lua" />
     <None Include="..\..\..\src\libjin-lua\scripts\tiledmap\tiledmap.lua" />
diff --git a/build/vc++/libjin-lua/libjin-lua.vcxproj.filters b/build/vc++/libjin-lua/libjin-lua.vcxproj.filters
index eccf119..dd4b486 100644
--- a/build/vc++/libjin-lua/libjin-lua.vcxproj.filters
+++ b/build/vc++/libjin-lua/libjin-lua.vcxproj.filters
@@ -91,6 +91,9 @@
     <Filter Include="source\scripts\physics">
       <UniqueIdentifier>{1a9ea013-c770-410a-a2ec-a9b51d1c4112}</UniqueIdentifier>
     </Filter>
+    <Filter Include="source\scripts\audio">
+      <UniqueIdentifier>{76da0223-0e72-44da-8abe-8dd97c1cec35}</UniqueIdentifier>
+    </Filter>
   </ItemGroup>
   <ItemGroup>
     <ClCompile Include="..\..\..\src\libjin-lua\modules\graphics\je_lua_sprite.cpp">
@@ -239,9 +242,6 @@
     <ClInclude Include="..\..\..\src\libjin-lua\common\je_lua_constant.h">
       <Filter>source\common</Filter>
     </ClInclude>
-    <ClInclude Include="..\..\..\src\libjin-lua\common\je_lua_error.h">
-      <Filter>source\common</Filter>
-    </ClInclude>
     <ClInclude Include="..\..\..\src\libjin-lua\modules\time\je_lua_timer.h">
       <Filter>source\modules\time</Filter>
     </ClInclude>
@@ -403,6 +403,9 @@
     <None Include="..\..\..\src\libjin-lua\scripts\physics\physics.lua">
       <Filter>source\scripts\physics</Filter>
     </None>
+    <None Include="..\..\..\src\libjin-lua\scripts\audio\audio.lua">
+      <Filter>source\scripts\audio</Filter>
+    </None>
   </ItemGroup>
   <ItemGroup>
     <Text Include="..\..\..\src\libjin-lua\scripts\app.lua">
diff --git a/samples/post-processing/shaders/rgb-split.jsl b/samples/post-processing/shaders/rgb-split.jsl
index e3b8abc..41e7762 100644
--- a/samples/post-processing/shaders/rgb-split.jsl
+++ b/samples/post-processing/shaders/rgb-split.jsl
@@ -22,4 +22,4 @@ Color frag(Color col, Texture tex, Vertex v)
     vec4 c3 = texel(tex, v.uv + value / jin_RenderTargetSize.y);
     return vec4(c1.r, c2.g, c3.b, c1.a + c2.a + c3.b);
 }
-#END_FRAGMENT_SHADER
\ No newline at end of file
+#END_FRAGMENT_SHADER
diff --git a/src/3rdparty/luax/luax.h b/src/3rdparty/luax/luax.h
index aac9ee4..08411f9 100644
--- a/src/3rdparty/luax/luax.h
+++ b/src/3rdparty/luax/luax.h
@@ -106,12 +106,24 @@ inline float luax_rawgetnumberthenpop(lua_State* L, int i, int k)
 */
 #define luax_typerror luaL_typerror
 
-
 /**
 * Error checking
 */
 #define luax_error luaL_error
 
+inline void luax_errorf(lua_State* L, const char* fmt, ...)
+{
+    const int FORMAT_MSG_BUFFER_SIZE = 2048;
+    char err[FORMAT_MSG_BUFFER_SIZE + 1] = { 0 };
+    va_list args;
+    va_start(args, fmt);
+    vsnprintf(err + strlen(err), FORMAT_MSG_BUFFER_SIZE, fmt, args);
+    va_end(args);
+    //luax_getglobal(L, "jin");
+    //luax_setfieldstring(L, "error", err);
+    luax_error(L, err);
+}
+
 /**
 * Push value on the top of stack.
 */
diff --git a/src/libjin-lua/common/je_lua_common.h b/src/libjin-lua/common/je_lua_common.h
index cdd4f36..dbda9a0 100644
--- a/src/libjin-lua/common/je_lua_common.h
+++ b/src/libjin-lua/common/je_lua_common.h
@@ -3,7 +3,6 @@
 
 #include "je_lua.h"
 #include "je_lua_port.h"
-#include "je_lua_error.h"
 #include "je_lua_reference.h"
 
 #endif
\ No newline at end of file
diff --git a/src/libjin-lua/common/je_lua_error.h b/src/libjin-lua/common/je_lua_error.h
deleted file mode 100644
index bd5695d..0000000
--- a/src/libjin-lua/common/je_lua_error.h
+++ /dev/null
@@ -1,30 +0,0 @@
-#ifndef __JIN_ERROR_H
-#define __JIN_ERROR_H
-
-#include <string.h>
-
-#include "common/je_lua.h"
-
-namespace JinEngine
-{
-    namespace Lua
-    {
-
-        static const int FORMAT_MSG_BUFFER_SIZE = 2048;
-
-        inline void error(lua_State* L, const char* fmt, ...)
-        {
-            char err[FORMAT_MSG_BUFFER_SIZE + 1] = { 0 };
-            va_list args;
-            va_start(args, fmt);
-            vsnprintf(err + strlen(err), FORMAT_MSG_BUFFER_SIZE, fmt, args);
-            va_end(args);
-            //luax_getglobal(L, "jin");
-            //luax_setfieldstring(L, "error", err);
-            luax_error(L, err);
-        }
-
-    } // namespace Lua
-} // namespace JinEngine 
-
-#endif
\ No newline at end of file
diff --git a/src/libjin-lua/je_lua_jin.cpp b/src/libjin-lua/je_lua_jin.cpp
index debd79e..256e081 100644
--- a/src/libjin-lua/je_lua_jin.cpp
+++ b/src/libjin-lua/je_lua_jin.cpp
@@ -111,6 +111,7 @@ namespace JinEngine
         #include "scripts/log.lua.h"
         #include "scripts/tiledmap/tiledmap.lua.h"
         #include "scripts/physics/physics.lua.h"
+        #include "scripts/audio/audio.lua.h"
 
         #include "scripts/app.lua.h"
 
@@ -128,6 +129,7 @@ namespace JinEngine
             { "xml.lua",           xml_lua           },
             { "tiledmap.lua",      tiledmap_lua      },
             { "physics.lua",       physics_lua       },
+            { "audio.lua",         audio_lua         },
             // 
             { "log.lua",           log_lua           },
             { 0,                   0                 }
diff --git a/src/libjin-lua/modules/audio/je_lua_audio.cpp b/src/libjin-lua/modules/audio/je_lua_audio.cpp
index f5ef31f..d5cfb43 100644
--- a/src/libjin-lua/modules/audio/je_lua_audio.cpp
+++ b/src/libjin-lua/modules/audio/je_lua_audio.cpp
@@ -87,14 +87,14 @@ namespace JinEngine
             }
             catch (Exception& e)
             {
-                error(L, "Failed to read source file %s", f);
+                luax_errorf(L, "Failed to read source file %s", f);
                 luax_pushnil(L);
                 return 1;
             }
             Source* src = new SDLSource((void*)&b, b.size());
             if (src == nullptr)
             {
-                error(L, "Failed to decode source file %s", f);
+                luax_errorf(L, "Failed to decode source file %s", f);
                 luax_pushnil(L);
                 return 1;
             }
diff --git a/src/libjin-lua/modules/graphics/je_lua_graphics.cpp b/src/libjin-lua/modules/graphics/je_lua_graphics.cpp
index a77f1ad..96577f5 100644
--- a/src/libjin-lua/modules/graphics/je_lua_graphics.cpp
+++ b/src/libjin-lua/modules/graphics/je_lua_graphics.cpp
@@ -197,14 +197,14 @@ namespace JinEngine
                 }
                 catch (Exception& e)
                 {
-                    error(L, "Failed to read image %s", f);
+                    luax_errorf(L, "Failed to read image %s", f);
                     luax_pushnil(L);
                     return 1;
                 }
                 bitmap = new Bitmap(&b, b.size());
                 if (bitmap == nullptr)
                 {
-                    error(L, "Failed to decode image file %s", f);
+                    luax_errorf(L, "Failed to decode image file %s", f);
                     luax_pushnil(L);
                     return 1;
                 }
@@ -232,13 +232,18 @@ namespace JinEngine
             return 1;
         }
 
+        // See embed graphics.lua.
         LUA_IMPLEMENT int l_newShader(lua_State* L)
         {
             const char* program = luax_checkstring(L, 1);
-            Shader* jsl = new Shader(program);
-            if (jsl == nullptr)
+            Shader* jsl = nullptr;
+            try
             {
-                error(L, "Failed to compile shader");
+                jsl = new Shader(program);
+            }
+            catch (JinEngine::Exception& e)
+            {
+                //luax_errorf(L, "Failed to compile shader");
                 luax_pushnil(L);
                 return 1;
             }
@@ -246,22 +251,27 @@ namespace JinEngine
             return 1;
         }
 
+        // See embed graphics.lua
         LUA_IMPLEMENT int l_newShaderf(lua_State* L)
         {
             const char* path = luax_checkstring(L, 1);
             AssetDatabase* fs = AssetDatabase::get();
             if (!fs->exists(path))
             {
-                error(L, "No such shader file \"%s\"", path);
+                //luax_errorf(L, "No such shader file \"%s\"", path);
                 luax_pushnil(L);
                 return 1;
             }
             Buffer b;
             fs->read(path, b);
-            Shader* jsl = new Shader((char*)&b);
-            if (jsl == nullptr)
+            Shader* jsl = nullptr;
+            try
+            {
+                jsl = new Shader((char*)&b);
+            }
+            catch (JinEngine::Exception& e)
             {
-                error(L, "Failed to compile shader");
+                //luax_errorf(L, "Failed to compile shader");
                 luax_pushnil(L);
                 return 1;
             }
@@ -705,7 +715,7 @@ namespace JinEngine
                 AssetDatabase* fs = AssetDatabase::get();
                 if (!fs->exists(path))
                 {
-                    error(L, "No such font \"%s\"", path);
+                    luax_errorf(L, "No such font \"%s\"", path);
                     luax_pushnil(L);
                     return 1;
                 }
diff --git a/src/libjin-lua/scripts/audio/audio.lua b/src/libjin-lua/scripts/audio/audio.lua
new file mode 100644
index 0000000..e462652
--- /dev/null
+++ b/src/libjin-lua/scripts/audio/audio.lua
@@ -0,0 +1,5 @@
+
+jin.audio = jin.audio or {} 
+
+
+
diff --git a/src/libjin-lua/scripts/audio/audio.lua.h b/src/libjin-lua/scripts/audio/audio.lua.h
new file mode 100644
index 0000000..2ea6d41
--- /dev/null
+++ b/src/libjin-lua/scripts/audio/audio.lua.h
@@ -0,0 +1,6 @@
+/*Auto generated, don't modify by hand.*/
+static char audio_lua[] = {
+13,10,106,105,110,46,97,117,100,105,111,32,61,32,106,105,110,46,97,117,
+100,105,111,32,111,114,32,123,125,32,13,10,13,10,13,10,13,10,0
+};
+
diff --git a/src/libjin-lua/scripts/graphics/graphics.lua b/src/libjin-lua/scripts/graphics/graphics.lua
index ae896d0..3abb64d 100644
--- a/src/libjin-lua/scripts/graphics/graphics.lua
+++ b/src/libjin-lua/scripts/graphics/graphics.lua
@@ -138,7 +138,21 @@ local function compileBuiltInShaders()
 			return col * texel(tex, v.uv);
 		}
 		#END_FRAGMENT_SHADER
-	]] 
+	]]
+    jg.Shaders.Error = jg.newShader[[
+    	#VERTEX_SHADER
+		Vertex vert(Vertex v)
+		{
+			return v;
+		}
+		#END_VERTEX_SHADER
+		#FRAGMENT_SHADER
+		Color frag(Color col, Texture tex, Vertex v)
+		{
+			return Color(1, 0, 1, 1);
+		}
+		#END_FRAGMENT_SHADER
+    ]]
 end
 
 local _init = jg.init
@@ -155,10 +169,6 @@ jg.init = function(setting)
     return initialized 
 end
 
-jg.unuseShader = function()
-    jg.useShader(jg.Shaders.Default)
-end
-
 -- Reset all attributes to default value.
 jg.reset = function()
     jg.setColor(255, 255, 255, 255)
@@ -167,3 +177,31 @@ jg.reset = function()
     jg.unsetFont()
     jg.unuseShader()
 end
+
+jg.unuseShader = function()
+    jg.useShader(jg.Shaders.Default)
+end
+
+local _newShader = jg.newShader
+
+jg.newShader = function(program) 
+    local shader = _newShader(program)
+    if shader == nil then 
+        jin.log.error("Compile shader failed:\n" .. debug.traceback())
+        return jg.Shaders.Error
+    else
+        return shader
+    end
+end
+
+local _newShaderf = jg.newShaderf
+
+jg.newShaderf = function(file)
+    local shader = _newShaderf(file)
+    if shader == nil then 
+        jin.log.error("Compile shader failed:\n" .. debug.traceback())
+        return jg.Shaders.Error
+    else
+        return shader
+    end
+end
diff --git a/src/libjin-lua/scripts/graphics/graphics.lua.h b/src/libjin-lua/scripts/graphics/graphics.lua.h
index eb686c9..cb6cf56 100644
--- a/src/libjin-lua/scripts/graphics/graphics.lua.h
+++ b/src/libjin-lua/scripts/graphics/graphics.lua.h
@@ -131,37 +131,79 @@ static char graphics_lua[] = {
 120,32,118,41,13,10,9,9,123,13,10,9,9,9,114,101,116,117,114,110,
 32,99,111,108,32,42,32,116,101,120,101,108,40,116,101,120,44,32,118,46,
 117,118,41,59,13,10,9,9,125,13,10,9,9,35,69,78,68,95,70,82,
-65,71,77,69,78,84,95,83,72,65,68,69,82,13,10,9,93,93,32,13,
-10,101,110,100,13,10,13,10,108,111,99,97,108,32,95,105,110,105,116,32,
-61,32,106,103,46,105,110,105,116,13,10,108,111,99,97,108,32,105,110,105,
-116,105,97,108,105,122,101,100,32,61,32,102,97,108,115,101,13,10,106,103,
-46,105,110,105,116,32,61,32,102,117,110,99,116,105,111,110,40,115,101,116,
-116,105,110,103,41,32,13,10,32,32,32,32,105,102,32,105,110,105,116,105,
-97,108,105,122,101,100,32,116,104,101,110,32,13,10,32,32,32,32,32,32,
-32,32,114,101,116,117,114,110,32,105,110,105,116,105,97,108,105,122,101,100,
-13,10,32,32,32,32,101,110,100,13,10,32,32,32,32,105,110,105,116,105,
-97,108,105,122,101,100,32,61,32,95,105,110,105,116,40,115,101,116,116,105,
-110,103,41,13,10,32,32,32,32,105,102,32,105,110,105,116,105,97,108,105,
-122,101,100,32,116,104,101,110,32,13,10,32,32,32,32,32,32,32,32,99,
-111,109,112,105,108,101,66,117,105,108,116,73,110,83,104,97,100,101,114,115,
-40,41,13,10,32,32,32,32,32,32,32,32,106,103,46,117,115,101,83,104,
+65,71,77,69,78,84,95,83,72,65,68,69,82,13,10,9,93,93,13,10,
+32,32,32,32,106,103,46,83,104,97,100,101,114,115,46,69,114,114,111,114,
+32,61,32,106,103,46,110,101,119,83,104,97,100,101,114,91,91,13,10,32,
+32,32,32,9,35,86,69,82,84,69,88,95,83,72,65,68,69,82,13,10,
+9,9,86,101,114,116,101,120,32,118,101,114,116,40,86,101,114,116,101,120,
+32,118,41,13,10,9,9,123,13,10,9,9,9,114,101,116,117,114,110,32,
+118,59,13,10,9,9,125,13,10,9,9,35,69,78,68,95,86,69,82,84,
+69,88,95,83,72,65,68,69,82,13,10,9,9,35,70,82,65,71,77,69,
+78,84,95,83,72,65,68,69,82,13,10,9,9,67,111,108,111,114,32,102,
+114,97,103,40,67,111,108,111,114,32,99,111,108,44,32,84,101,120,116,117,
+114,101,32,116,101,120,44,32,86,101,114,116,101,120,32,118,41,13,10,9,
+9,123,13,10,9,9,9,114,101,116,117,114,110,32,67,111,108,111,114,40,
+49,44,32,48,44,32,49,44,32,49,41,59,13,10,9,9,125,13,10,9,
+9,35,69,78,68,95,70,82,65,71,77,69,78,84,95,83,72,65,68,69,
+82,13,10,32,32,32,32,93,93,13,10,101,110,100,13,10,13,10,108,111,
+99,97,108,32,95,105,110,105,116,32,61,32,106,103,46,105,110,105,116,13,
+10,108,111,99,97,108,32,105,110,105,116,105,97,108,105,122,101,100,32,61,
+32,102,97,108,115,101,13,10,106,103,46,105,110,105,116,32,61,32,102,117,
+110,99,116,105,111,110,40,115,101,116,116,105,110,103,41,32,13,10,32,32,
+32,32,105,102,32,105,110,105,116,105,97,108,105,122,101,100,32,116,104,101,
+110,32,13,10,32,32,32,32,32,32,32,32,114,101,116,117,114,110,32,105,
+110,105,116,105,97,108,105,122,101,100,13,10,32,32,32,32,101,110,100,13,
+10,32,32,32,32,105,110,105,116,105,97,108,105,122,101,100,32,61,32,95,
+105,110,105,116,40,115,101,116,116,105,110,103,41,13,10,32,32,32,32,105,
+102,32,105,110,105,116,105,97,108,105,122,101,100,32,116,104,101,110,32,13,
+10,32,32,32,32,32,32,32,32,99,111,109,112,105,108,101,66,117,105,108,
+116,73,110,83,104,97,100,101,114,115,40,41,13,10,32,32,32,32,32,32,
+32,32,106,103,46,117,115,101,83,104,97,100,101,114,40,106,103,46,83,104,
+97,100,101,114,115,46,68,101,102,97,117,108,116,41,13,10,32,32,32,32,
+101,110,100,13,10,32,32,32,32,114,101,116,117,114,110,32,105,110,105,116,
+105,97,108,105,122,101,100,32,13,10,101,110,100,13,10,13,10,45,45,32,
+82,101,115,101,116,32,97,108,108,32,97,116,116,114,105,98,117,116,101,115,
+32,116,111,32,100,101,102,97,117,108,116,32,118,97,108,117,101,46,13,10,
+106,103,46,114,101,115,101,116,32,61,32,102,117,110,99,116,105,111,110,40,
+41,13,10,32,32,32,32,106,103,46,115,101,116,67,111,108,111,114,40,50,
+53,53,44,32,50,53,53,44,32,50,53,53,44,32,50,53,53,41,13,10,
+32,32,32,32,106,103,46,115,101,116,67,108,101,97,114,67,111,108,111,114,
+40,48,44,32,48,44,32,48,44,32,48,41,13,10,32,32,32,32,106,103,
+46,99,108,101,97,114,40,41,13,10,32,32,32,32,106,103,46,117,110,115,
+101,116,70,111,110,116,40,41,13,10,32,32,32,32,106,103,46,117,110,117,
+115,101,83,104,97,100,101,114,40,41,13,10,101,110,100,13,10,13,10,106,
+103,46,117,110,117,115,101,83,104,97,100,101,114,32,61,32,102,117,110,99,
+116,105,111,110,40,41,13,10,32,32,32,32,106,103,46,117,115,101,83,104,
 97,100,101,114,40,106,103,46,83,104,97,100,101,114,115,46,68,101,102,97,
-117,108,116,41,13,10,32,32,32,32,101,110,100,13,10,32,32,32,32,114,
-101,116,117,114,110,32,105,110,105,116,105,97,108,105,122,101,100,32,13,10,
-101,110,100,13,10,13,10,106,103,46,117,110,117,115,101,83,104,97,100,101,
-114,32,61,32,102,117,110,99,116,105,111,110,40,41,13,10,32,32,32,32,
-106,103,46,117,115,101,83,104,97,100,101,114,40,106,103,46,83,104,97,100,
-101,114,115,46,68,101,102,97,117,108,116,41,13,10,101,110,100,13,10,13,
-10,45,45,32,82,101,115,101,116,32,97,108,108,32,97,116,116,114,105,98,
-117,116,101,115,32,116,111,32,100,101,102,97,117,108,116,32,118,97,108,117,
-101,46,13,10,106,103,46,114,101,115,101,116,32,61,32,102,117,110,99,116,
-105,111,110,40,41,13,10,32,32,32,32,106,103,46,115,101,116,67,111,108,
-111,114,40,50,53,53,44,32,50,53,53,44,32,50,53,53,44,32,50,53,
-53,41,13,10,32,32,32,32,106,103,46,115,101,116,67,108,101,97,114,67,
-111,108,111,114,40,48,44,32,48,44,32,48,44,32,48,41,13,10,32,32,
-32,32,106,103,46,99,108,101,97,114,40,41,13,10,32,32,32,32,106,103,
-46,117,110,115,101,116,70,111,110,116,40,41,13,10,32,32,32,32,106,103,
-46,117,110,117,115,101,83,104,97,100,101,114,40,41,13,10,101,110,100,13,
-10,0
+117,108,116,41,13,10,101,110,100,13,10,13,10,108,111,99,97,108,32,95,
+110,101,119,83,104,97,100,101,114,32,61,32,106,103,46,110,101,119,83,104,
+97,100,101,114,13,10,13,10,106,103,46,110,101,119,83,104,97,100,101,114,
+32,61,32,102,117,110,99,116,105,111,110,40,112,114,111,103,114,97,109,41,
+32,13,10,32,32,32,32,108,111,99,97,108,32,115,104,97,100,101,114,32,
+61,32,95,110,101,119,83,104,97,100,101,114,40,112,114,111,103,114,97,109,
+41,13,10,32,32,32,32,105,102,32,115,104,97,100,101,114,32,61,61,32,
+110,105,108,32,116,104,101,110,32,13,10,32,32,32,32,32,32,32,32,106,
+105,110,46,108,111,103,46,101,114,114,111,114,40,34,67,111,109,112,105,108,
+101,32,115,104,97,100,101,114,32,102,97,105,108,101,100,58,92,110,34,32,
+46,46,32,100,101,98,117,103,46,116,114,97,99,101,98,97,99,107,40,41,
+41,13,10,32,32,32,32,32,32,32,32,114,101,116,117,114,110,32,106,103,
+46,83,104,97,100,101,114,115,46,69,114,114,111,114,13,10,32,32,32,32,
+101,108,115,101,13,10,32,32,32,32,32,32,32,32,114,101,116,117,114,110,
+32,115,104,97,100,101,114,13,10,32,32,32,32,101,110,100,13,10,101,110,
+100,13,10,13,10,108,111,99,97,108,32,95,110,101,119,83,104,97,100,101,
+114,102,32,61,32,106,103,46,110,101,119,83,104,97,100,101,114,102,13,10,
+13,10,106,103,46,110,101,119,83,104,97,100,101,114,102,32,61,32,102,117,
+110,99,116,105,111,110,40,102,105,108,101,41,13,10,32,32,32,32,108,111,
+99,97,108,32,115,104,97,100,101,114,32,61,32,95,110,101,119,83,104,97,
+100,101,114,102,40,102,105,108,101,41,13,10,32,32,32,32,105,102,32,115,
+104,97,100,101,114,32,61,61,32,110,105,108,32,116,104,101,110,32,13,10,
+32,32,32,32,32,32,32,32,106,105,110,46,108,111,103,46,101,114,114,111,
+114,40,34,67,111,109,112,105,108,101,32,115,104,97,100,101,114,32,102,97,
+105,108,101,100,58,92,110,34,32,46,46,32,100,101,98,117,103,46,116,114,
+97,99,101,98,97,99,107,40,41,41,13,10,32,32,32,32,32,32,32,32,
+114,101,116,117,114,110,32,106,103,46,83,104,97,100,101,114,115,46,69,114,
+114,111,114,13,10,32,32,32,32,101,108,115,101,13,10,32,32,32,32,32,
+32,32,32,114,101,116,117,114,110,32,115,104,97,100,101,114,13,10,32,32,
+32,32,101,110,100,13,10,101,110,100,13,10,0
 };
 
diff --git a/src/libjin/graphics/shaders/je_shader.cpp b/src/libjin/graphics/shaders/je_shader.cpp
index 0eb3631..059c411 100644
--- a/src/libjin/graphics/shaders/je_shader.cpp
+++ b/src/libjin/graphics/shaders/je_shader.cpp
@@ -5,6 +5,7 @@
 
 #include "../../time/je_timer.h"
 #include "../../filesystem/je_buffer.h"
+#include "../../utils/je_log.h"
 #include "../../utils/je_macros.h"
 
 #include "../je_gl.h"
@@ -65,7 +66,8 @@ namespace JinEngine
             {
                 if (!compile(program))
                 {
-                    throw 0;
+                    jin_log_error("Compile jsl shader failed.");
+                    throw Exception("Compile jsl shader failed");
                 }
             }
 
-- 
cgit v1.1-26-g67d0


From da056982e50bdd9cb9f944691cc88ca98b053b77 Mon Sep 17 00:00:00 2001
From: chai <chaifix@163.com>
Date: Sun, 6 Jan 2019 23:33:08 +0800
Subject: *misc

---
 bin/jin.exe                                   | Bin 573440 -> 573440 bytes
 samples/post-processing/config.lua            |   2 +-
 samples/post-processing/shaders/rgb-split.jsl |   2 +-
 src/libjin/graphics/fonts/je_texture_font.cpp |   3 ++-
 src/libjin/graphics/fonts/je_ttf.cpp          |   3 ++-
 src/libjin/graphics/je_graphic.cpp            |   8 ++++++--
 src/libjin/graphics/je_mesh.cpp               |   4 +++-
 src/libjin/graphics/je_shapes.cpp             |  20 +++++++++++++++-----
 src/libjin/graphics/shaders/je_shader.cpp     |  22 ++++++++++++++--------
 src/libjin/graphics/shaders/je_shader.h       |   9 +++++++--
 10 files changed, 51 insertions(+), 22 deletions(-)

diff --git a/bin/jin.exe b/bin/jin.exe
index 1478b8e..92e0cbc 100644
Binary files a/bin/jin.exe and b/bin/jin.exe differ
diff --git a/samples/post-processing/config.lua b/samples/post-processing/config.lua
index 095ee28..3c6ff07 100644
--- a/samples/post-processing/config.lua
+++ b/samples/post-processing/config.lua
@@ -1,6 +1,6 @@
 return 
 {
-    title = "window form", 
+    title = "post-processing", 
     icon = "splash.png",
 	vsync = true
 }
\ No newline at end of file
diff --git a/samples/post-processing/shaders/rgb-split.jsl b/samples/post-processing/shaders/rgb-split.jsl
index 41e7762..aba401b 100644
--- a/samples/post-processing/shaders/rgb-split.jsl
+++ b/samples/post-processing/shaders/rgb-split.jsl
@@ -9,7 +9,7 @@ Vertex vert(Vertex v)
 Color frag(Color col, Texture tex, Vertex v)
 {
 	float t = jin_Time.x;
-	t = 0.02;
+	//t = 0.02;
 	float a = abs(sin(t)) * 3.14;
     vec2 p = vec2(0.5*cos(a) + 0.5, 0.5*sin(a) + 0.5);
     vec2 dir = v.uv - p;
diff --git a/src/libjin/graphics/fonts/je_texture_font.cpp b/src/libjin/graphics/fonts/je_texture_font.cpp
index 016367c..49ac797 100644
--- a/src/libjin/graphics/fonts/je_texture_font.cpp
+++ b/src/libjin/graphics/fonts/je_texture_font.cpp
@@ -276,7 +276,7 @@ namespace JinEngine
                 const vector<GlyphArrayDrawInfo>& glyphinfolist = page->glyphinfolist;
                 const vector<GlyphVertex>& glyphvertices = page->glyphvertices;
                 Matrix modelMatrix = gl.getModelViewMatrix(x, y, 1, 1, 0, 0, 0);
-                shader->prepare()
+                shader->begin()
                     .sendMatrix4(SHADER_MODELVIEW_MATRIX, &modelMatrix)
                     .sendMatrix4(SHADER_PROJECTION_MATRIX, &gl.getProjectionMatrix());
                 for (int i = 0; i < glyphinfolist.size(); ++i)
@@ -288,6 +288,7 @@ namespace JinEngine
                     gl.drawArrays(GL_QUADS, 0, info.count);
                     gl.bindTexture(0);
                 }
+                shader->end();
             }
 
             void TextureFont::render(const Content& text, int x, int y, int lineheight, int spacing)
diff --git a/src/libjin/graphics/fonts/je_ttf.cpp b/src/libjin/graphics/fonts/je_ttf.cpp
index 1df7fa7..89aa096 100644
--- a/src/libjin/graphics/fonts/je_ttf.cpp
+++ b/src/libjin/graphics/fonts/je_ttf.cpp
@@ -295,7 +295,7 @@ namespace JinEngine
                 const vector<GlyphArrayDrawInfo>& glyphinfolist = page->glyphinfolist;
                 const vector<GlyphVertex>& glyphvertices = page->glyphvertices;
                 Matrix modelMatrix = gl.getModelViewMatrix(x, y, 1, 1, 0, 0, 0);
-                shader->prepare()
+                shader->begin()
                     .sendMatrix4(SHADER_MODELVIEW_MATRIX, &modelMatrix)
                     .sendMatrix4(SHADER_PROJECTION_MATRIX, &gl.getProjectionMatrix());
                 for (int i = 0; i < glyphinfolist.size(); ++i)
@@ -307,6 +307,7 @@ namespace JinEngine
                     gl.drawArrays(GL_QUADS, 0, info.count);
                     gl.bindTexture(0);
                 }
+                shader->end();
             }
 
             void TTF::render(const Text& text, int x, int y, int lineheight, int spacing /* = 0 */)
diff --git a/src/libjin/graphics/je_graphic.cpp b/src/libjin/graphics/je_graphic.cpp
index 18dbf42..e2c22d1 100644
--- a/src/libjin/graphics/je_graphic.cpp
+++ b/src/libjin/graphics/je_graphic.cpp
@@ -71,7 +71,7 @@ namespace JinEngine
             textureCoords[6] = 1; textureCoords[7] = 0;
             // Set shader.
             Shader* shader = gl.getShader();
-            shader->prepare()
+            shader->begin()
                 .sendMatrix4(SHADER_MODELVIEW_MATRIX, &modelViewMatrix)
                 .sendMatrix4(SHADER_PROJECTION_MATRIX, &gl.getProjectionMatrix())
                 .uploadVertices(2, GL_FLOAT, 0, vertexCoords)
@@ -80,6 +80,8 @@ namespace JinEngine
             gl.bindTexture(getGLTexture());
             gl.drawArrays(GL_QUADS, 0, 4);
             gl.bindTexture(0);
+
+            shader->end();
         }
 
         void Graphic::render(const Math::Quad& slice, float x, float y, float sx, float sy, float r, float ax, float ay) const
@@ -105,7 +107,7 @@ namespace JinEngine
             Math::Matrix modelViewMatrix = gl.getModelViewMatrix(x, y, sx, sy, r, ax, ay);
 
             Shader* shader = gl.getShader();
-            shader->prepare()
+            shader->begin()
                 .sendMatrix4(SHADER_MODELVIEW_MATRIX, &modelViewMatrix)
                 .sendMatrix4(SHADER_PROJECTION_MATRIX, &gl.getProjectionMatrix())
                 .uploadVertices(2, GL_FLOAT, 0, vertexCoords)
@@ -114,6 +116,8 @@ namespace JinEngine
             gl.bindTexture(getGLTexture());
             gl.drawArrays(GL_QUADS, 0, 4);
             gl.bindTexture(0);
+
+            shader->end();
         }
 
         void Graphic::render(const Math::Transform& transform) const
diff --git a/src/libjin/graphics/je_mesh.cpp b/src/libjin/graphics/je_mesh.cpp
index 2d453b0..e3bebe6 100644
--- a/src/libjin/graphics/je_mesh.cpp
+++ b/src/libjin/graphics/je_mesh.cpp
@@ -60,7 +60,7 @@ namespace JinEngine
             Math::Matrix modelViewMatrix = gl.getModelViewMatrix(x, y, sx, sy, r, ox, oy);
 
             Shader* shader = gl.getShader();
-            shader->prepare()
+            shader->begin()
                 .sendMatrix4(SHADER_MODELVIEW_MATRIX, &modelViewMatrix)
                 .sendMatrix4(SHADER_PROJECTION_MATRIX, &gl.getProjectionMatrix())
                 .uploadVertices(2, GL_FLOAT, sizeof(Vertex), &(mVertices[0].xy))
@@ -70,6 +70,8 @@ namespace JinEngine
             gl.bindTexture(mGraphic->getGLTexture());
             gl.drawArrays(GL_POLYGON, 0, mVertices.size());
             gl.bindTexture(0);
+
+            shader->end();
         };
 
     } // namespace Graphics
diff --git a/src/libjin/graphics/je_shapes.cpp b/src/libjin/graphics/je_shapes.cpp
index ce5c7e7..493e173 100644
--- a/src/libjin/graphics/je_shapes.cpp
+++ b/src/libjin/graphics/je_shapes.cpp
@@ -25,12 +25,14 @@ namespace JinEngine
             Matrix modelMatrix = gl.getModelViewMatrix();
 
             Shader* shader = gl.getShader();
-            shader->prepare()
+            shader->begin()
                 .uploadVertices(2, GL_FLOAT, 0, verts)
                 .sendMatrix4(SHADER_MODELVIEW_MATRIX, &modelMatrix)
                 .sendMatrix4(SHADER_PROJECTION_MATRIX, &gl.getProjectionMatrix());
 
             gl.drawArrays(GL_POINTS, 0, 1);
+
+            shader->end();
         }
 
         void points(int n, GLshort* p)
@@ -38,12 +40,14 @@ namespace JinEngine
             Matrix modelMatrix = gl.getModelViewMatrix();
 
             Shader* shader = gl.getShader();
-            shader->prepare()
+            shader->begin()
                 .uploadVertices(2, GL_SHORT, 0, p)
                 .sendMatrix4(SHADER_MODELVIEW_MATRIX, &modelMatrix)
                 .sendMatrix4(SHADER_PROJECTION_MATRIX, &gl.getProjectionMatrix());
         
             gl.drawArrays(GL_POINTS, 0, n);
+
+            shader->end();
         }
 
         void line(int x1, int y1, int x2, int y2)
@@ -56,12 +60,14 @@ namespace JinEngine
             Matrix modelMatrix = gl.getModelViewMatrix();
         
             Shader* shader = gl.getShader();
-            shader->prepare()
+            shader->begin()
                 .uploadVertices(2, GL_FLOAT, 0, verts)
                 .sendMatrix4(SHADER_MODELVIEW_MATRIX, &modelMatrix)
                 .sendMatrix4(SHADER_PROJECTION_MATRIX, &gl.getProjectionMatrix());
         
             gl.drawArrays(GL_LINES, 0, 2);
+
+            shader->end();
         }
 
         void circle(RenderMode mode, int x, int y, int r)
@@ -105,12 +111,14 @@ namespace JinEngine
         {
             Shader* shader = gl.getShader();
             Matrix modelMatrix = gl.getModelViewMatrix();
-            shader->prepare()
+            shader->begin()
                 .sendMatrix4(SHADER_MODELVIEW_MATRIX, &modelMatrix)
                 .sendMatrix4(SHADER_PROJECTION_MATRIX, &gl.getProjectionMatrix())
                 .uploadVertices(2, GL_FLOAT, 0, p);
 
             gl.drawArrays(GL_LINE_LOOP, 0, count);
+
+            shader->end();
         }
 
         void polygon(RenderMode mode, float* p, int count)
@@ -123,12 +131,14 @@ namespace JinEngine
             {
                 Shader* shader = gl.getShader();
                 Matrix modelMatrix = gl.getModelViewMatrix();
-                shader->prepare()
+                shader->begin()
                     .sendMatrix4(SHADER_MODELVIEW_MATRIX, &modelMatrix)
                     .sendMatrix4(SHADER_PROJECTION_MATRIX, &gl.getProjectionMatrix())
                     .uploadVertices(2, GL_FLOAT, 0, p);
 
                 gl.drawArrays(GL_POLYGON, 0, count);
+
+                shader->end();
             }
         }
 
diff --git a/src/libjin/graphics/shaders/je_shader.cpp b/src/libjin/graphics/shaders/je_shader.cpp
index 059c411..0803e65 100644
--- a/src/libjin/graphics/shaders/je_shader.cpp
+++ b/src/libjin/graphics/shaders/je_shader.cpp
@@ -58,7 +58,8 @@ namespace JinEngine
             // 
             const int DEFAULT_TEXTURE_UNIT = 0;
 
-            GLint Shader::mTextureUnit = DEFAULT_TEXTURE_UNIT;
+            static GLint textureUnit = 0;
+
             GLint Shader::mAttributeIndex = 0;
 
             Shader::Shader(const string& program)
@@ -79,8 +80,11 @@ namespace JinEngine
                 glDeleteShader(mPID);
             }
 
-            Shader& Shader::prepare()
+            Shader& Shader::begin()
             {
+
+                textureUnit = DEFAULT_TEXTURE_UNIT;
+
                 // Send uniforms.
                 sendInt(SHADER_MAIN_TEXTURE, DEFAULT_TEXTURE_UNIT);
                 sendVec2(SHADER_TIME, Time::getSecond(), Time::getDeltaTime());
@@ -93,15 +97,17 @@ namespace JinEngine
                 {
                     sendVec2(SHADER_RENDERTARGET_SIZE, rt->getWidth(), rt->getHeight());
                 }
-                // Reset attribute index.
-                for(; mAttributeIndex > 0; --mAttributeIndex)
-                    glDisableVertexAttribArray(mAttributeIndex);
-
-                mTextureUnit = DEFAULT_TEXTURE_UNIT;
 
                 return *this;
             }
 
+            void Shader::end()
+            {
+                // Reset attribute index.
+                for (; mAttributeIndex > 0; --mAttributeIndex)
+                    glDisableVertexAttribArray(mAttributeIndex);
+            }
+
             bool Shader::compile(const string& program)
             {
                 string vertex_shader, fragment_shader;
@@ -159,7 +165,7 @@ namespace JinEngine
                 //    return 0;
                 //mTextureUnits[name] = mCurrentTextureUnit;
                 //return mCurrentTextureUnit;
-                return mTextureUnit++;
+                return textureUnit++;
             }
 
             GLint Shader::getUniformLocation(const char* uniform)
diff --git a/src/libjin/graphics/shaders/je_shader.h b/src/libjin/graphics/shaders/je_shader.h
index c3ca721..402805a 100644
--- a/src/libjin/graphics/shaders/je_shader.h
+++ b/src/libjin/graphics/shaders/je_shader.h
@@ -47,7 +47,12 @@ namespace JinEngine
                 ///
                 /// Prepare shader and set default uniforms.
                 ///
-                Shader& prepare();
+                Shader& begin();
+
+                ///
+                /// End use shader.
+                ///
+                void end();
 
                 /// 
                 /// Send float value to shader.
@@ -181,7 +186,7 @@ namespace JinEngine
                 /// 
                 bool compile(const std::string& program);
 
-                static GLint mTextureUnit;
+                //static GLint mTextureUnit;
                 static GLint mAttributeIndex;
 
                 GLuint mPID;
-- 
cgit v1.1-26-g67d0