ConstData.hx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. package arm.data;
  2. import kha.graphics4.TextureFormat;
  3. import kha.Blob;
  4. import kha.Image;
  5. import iron.data.Data;
  6. import iron.system.ArmPack;
  7. class ConstData {
  8. // 2D layer view
  9. #if (kha_direct3d11 || kha_direct3d12)
  10. public static var layerViewVert = "
  11. uniform float4x4 projectionMatrix;
  12. struct SPIRV_Cross_Output { float4 color : TEXCOORD0; float2 texCoord : TEXCOORD1; float4 gl_Position : SV_Position; };
  13. SPIRV_Cross_Output main(float3 pos : TEXCOORD0, float2 tex : TEXCOORD1, float4 col : TEXCOORD2) {
  14. SPIRV_Cross_Output stage_output;
  15. stage_output.gl_Position = mul(float4(pos, 1.0f), projectionMatrix);
  16. stage_output.gl_Position.z = (stage_output.gl_Position.z + stage_output.gl_Position.w) * 0.5;
  17. stage_output.texCoord = tex;
  18. stage_output.color = col;
  19. return stage_output;
  20. }
  21. ";
  22. public static var layerViewFrag = "
  23. Texture2D<float4> tex;
  24. SamplerState _tex_sampler;
  25. float4 main(float4 color : TEXCOORD0, float2 texCoord : TEXCOORD1) : SV_Target0 {
  26. return tex.SampleLevel(_tex_sampler, texCoord, 0) * color;
  27. }
  28. ";
  29. #else // kha_opengl
  30. public static var layerViewVert = "#version 330
  31. in vec3 pos;
  32. in vec2 tex;
  33. in vec4 col;
  34. uniform mat4 projectionMatrix;
  35. out vec2 texCoord;
  36. out vec4 color;
  37. void main() {
  38. gl_Position = projectionMatrix * vec4(pos, 1.0);
  39. texCoord = tex;
  40. color = col;
  41. }
  42. ";
  43. public static var layerViewFrag = "#version 330
  44. uniform sampler2D tex;
  45. in vec2 texCoord;
  46. in vec4 color;
  47. out vec4 FragColor;
  48. void main() {
  49. FragColor = textureLod(tex, texCoord, 0) * color;
  50. }
  51. ";
  52. #end
  53. // 2D layer merge
  54. #if (kha_direct3d11 || kha_direct3d12)
  55. public static var layerMergeVert = "
  56. struct SPIRV_Cross_Output { float2 texCoord : TEXCOORD0; float4 gl_Position : SV_Position; };
  57. SPIRV_Cross_Output main(float2 pos : TEXCOORD0) {
  58. SPIRV_Cross_Output stage_output;
  59. stage_output.gl_Position = float4(pos.xy, 0.0, 1.0);
  60. stage_output.gl_Position.z = (stage_output.gl_Position.z + stage_output.gl_Position.w) * 0.5;
  61. const float2 madd = float2(0.5, 0.5);
  62. stage_output.texCoord = pos.xy * madd + madd;
  63. stage_output.texCoord.y = 1.0 - stage_output.texCoord.y;
  64. return stage_output;
  65. }
  66. ";
  67. public static var layerMergeFrag = "
  68. Texture2D<float4> tex0;
  69. SamplerState _tex0_sampler;
  70. Texture2D<float4> tex1;
  71. SamplerState _tex1_sampler;
  72. Texture2D<float4> tex2;
  73. SamplerState _tex2_sampler;
  74. Texture2D<float4> texa;
  75. SamplerState _texa_sampler;
  76. Texture2D<float4> texb;
  77. SamplerState _texb_sampler;
  78. Texture2D<float4> texc;
  79. SamplerState _texc_sampler;
  80. uniform float opac;
  81. struct SPIRV_Cross_Output { float4 color0 : SV_Target0; float4 color1 : SV_Target1; float4 color2 : SV_Target2; };
  82. SPIRV_Cross_Output main(float2 texCoord : TEXCOORD0) {
  83. float4 col0 = tex0.SampleLevel(_tex0_sampler, texCoord, 0);
  84. float4 col1 = tex1.SampleLevel(_tex1_sampler, texCoord, 0);
  85. float4 col2 = tex2.SampleLevel(_tex2_sampler, texCoord, 0);
  86. float4 cola = texa.SampleLevel(_texa_sampler, texCoord, 0);
  87. float4 colb = texb.SampleLevel(_texb_sampler, texCoord, 0);
  88. float4 colc = texc.SampleLevel(_texc_sampler, texCoord, 0);
  89. float str = col0.a * opac;
  90. SPIRV_Cross_Output stage_output;
  91. stage_output.color0 = float4(lerp(cola.rgb, col0.rgb, str), max(col0.a, cola.a));
  92. stage_output.color1 = float4(lerp(colb, col1, str));
  93. stage_output.color2 = float4(lerp(colc, col2, str));
  94. return stage_output;
  95. }
  96. ";
  97. public static var maskMergeFrag = "
  98. Texture2D<float4> tex0;
  99. SamplerState _tex0_sampler;
  100. Texture2D<float4> texa;
  101. SamplerState _texa_sampler;
  102. struct SPIRV_Cross_Output { float4 color0 : SV_Target0; };
  103. SPIRV_Cross_Output main(float2 texCoord : TEXCOORD0) {
  104. float4 col0 = tex0.SampleLevel(_tex0_sampler, texCoord, 0);
  105. float mask = texa.SampleLevel(_texa_sampler, texCoord, 0).r;
  106. SPIRV_Cross_Output stage_output;
  107. stage_output.color0 = float4(col0.rgb, col0.a * mask);
  108. return stage_output;
  109. }
  110. ";
  111. #else // kha_opengl
  112. public static var layerMergeVert = "#version 330
  113. in vec2 pos;
  114. out vec2 texCoord;
  115. void main() {
  116. gl_Position = vec4(pos.xy, 0.0, 1.0);
  117. const vec2 madd = vec2(0.5, 0.5);
  118. texCoord = pos.xy * madd + madd;
  119. }
  120. ";
  121. public static var layerMergeFrag = "#version 330
  122. uniform sampler2D tex0;
  123. uniform sampler2D tex1;
  124. uniform sampler2D tex2;
  125. uniform sampler2D texa;
  126. uniform sampler2D texb;
  127. uniform sampler2D texc;
  128. uniform float opac;
  129. in vec2 texCoord;
  130. out vec4 FragColor[3];
  131. void main() {
  132. vec4 col0 = textureLod(tex0, texCoord, 0);
  133. vec4 col1 = textureLod(tex1, texCoord, 0);
  134. vec4 col2 = textureLod(tex2, texCoord, 0);
  135. vec4 cola = textureLod(texa, texCoord, 0);
  136. vec4 colb = textureLod(texb, texCoord, 0);
  137. vec4 colc = textureLod(texc, texCoord, 0);
  138. float str = col0.a * opac;
  139. FragColor[0] = vec4(mix(cola.rgb, col0.rgb, str), max(col0.a, cola.a));
  140. FragColor[1] = vec4(mix(colb, col1, str));
  141. FragColor[2] = vec4(mix(colc, col2, str));
  142. }
  143. ";
  144. public static var maskMergeFrag = "#version 330
  145. uniform sampler2D tex0;
  146. uniform sampler2D texa;
  147. in vec2 texCoord;
  148. out vec4 FragColor;
  149. void main() {
  150. vec4 col0 = textureLod(tex0, texCoord, 0);
  151. float mask = textureLod(texa, texCoord, 0).r;
  152. FragColor = vec4(col0.rgb, col0.a * mask);
  153. }
  154. ";
  155. #end
  156. #if (kha_direct3d11 || kha_direct3d12)
  157. public static var cursorVert = "
  158. uniform float4x4 VP;
  159. uniform float4x4 invVP;
  160. uniform float2 mouse;
  161. uniform float2 step;
  162. uniform float radius;
  163. Texture2D<float4> texa; // direct3d12 unit align
  164. SamplerState _texa_sampler; // direct3d12 unit align
  165. Texture2D<float4> gbufferD;
  166. SamplerState _gbufferD_sampler;
  167. Texture2D<float4> gbuffer0;
  168. SamplerState _gbuffer0_sampler;
  169. float2 octahedronWrap(float2 v) { return (1.0 - abs(v.yx)) * (float2(v.x >= 0.0 ? 1.0 : -1.0, v.y >= 0.0 ? 1.0 : -1.0)); }
  170. float3x3 rotAxis(float3 axis, float a) {
  171. float c = cos(a);
  172. float3 as = axis * sin(a).xxx;
  173. float3x3 p = float3x3(axis.xxx * axis, axis.yyy * axis, axis.zzz * axis);
  174. float3x3 q = float3x3(c, -as.z, as.y, as.z, c, -as.x, -as.y, as.x, c);
  175. return p * (1.0 - c) + q;
  176. }
  177. float3 getNormal(float2 uv) {
  178. float2 g0 = gbuffer0.SampleLevel(_gbuffer0_sampler, uv, 0.0).rg;
  179. float3 n;
  180. n.z = 1.0 - abs(g0.x) - abs(g0.y);
  181. n.xy = n.z >= 0.0 ? g0.xy : octahedronWrap(g0.xy);
  182. return n;
  183. }
  184. struct SPIRV_Cross_Output { float2 texCoord : TEXCOORD0; float4 gl_Position : SV_Position; };
  185. SPIRV_Cross_Output main(float4 pos : TEXCOORD1, float2 nor : TEXCOORD0, float2 tex : TEXCOORD2) {
  186. SPIRV_Cross_Output stage_output;
  187. stage_output.texCoord = tex;
  188. float2 mouseinv = float2(mouse.x, 1.0 - mouse.y);
  189. float depth = gbufferD.SampleLevel(_gbufferD_sampler, mouseinv, 0).r;
  190. float keep = texa.SampleLevel(_texa_sampler, mouseinv, 0).r; // direct3d12 unit align
  191. depth += keep * 0.0; // direct3d12 unit align
  192. float4 wpos = float4(mouse * 2.0 - 1.0, depth * 2.0 - 1.0, 1.0);
  193. wpos = mul(wpos, invVP);
  194. wpos.xyz /= wpos.w;
  195. float3 n = normalize(
  196. getNormal(mouseinv + float2(step.x, step.y)) +
  197. getNormal(mouseinv + float2(-step.x, step.y)) +
  198. getNormal(mouseinv + float2(-step.x, -step.y)) +
  199. getNormal(mouseinv + float2(step.x, -step.y)) +
  200. getNormal(mouseinv)
  201. );
  202. float ax = acos(dot(float3(1,0,0), float3(n.x,0,0)));
  203. float az = acos(dot(float3(0,0,1), float3(0,0,n.z)));
  204. float sy = -sign(n.y);
  205. wpos.xyz += mul(mul(pos.xyz * radius.xxx, rotAxis(float3(0,0,1), ax + 3.14/2)),
  206. rotAxis(float3(1,0,0), -az * sy + 3.14/2));
  207. stage_output.gl_Position = mul(float4(wpos.xyz, 1.0), VP);
  208. stage_output.gl_Position.z = (stage_output.gl_Position.z + stage_output.gl_Position.w) * 0.5;
  209. return stage_output;
  210. }
  211. ";
  212. public static var cursorFrag = "
  213. Texture2D<float4> tex;
  214. SamplerState _tex_sampler;
  215. float4 main(float2 texCoord : TEXCOORD0) : SV_Target0 {
  216. float4 col = tex.Sample(_tex_sampler, texCoord);
  217. return float4(col.rgb / col.a, col.a);
  218. }
  219. ";
  220. #else // kha_opengl
  221. public static var cursorVert = "#version 330
  222. uniform mat4 VP;
  223. uniform mat4 invVP;
  224. uniform vec2 mouse;
  225. uniform vec2 step;
  226. uniform float radius;
  227. uniform sampler2D gbufferD;
  228. uniform sampler2D gbuffer0;
  229. in vec4 pos;
  230. in vec2 nor;
  231. in vec2 tex;
  232. out vec2 texCoord;
  233. vec2 octahedronWrap(const vec2 v) { return (1.0 - abs(v.yx)) * (vec2(v.x >= 0.0 ? 1.0 : -1.0, v.y >= 0.0 ? 1.0 : -1.0)); }
  234. mat3 rotAxis(vec3 axis, float a) {
  235. float c = cos(a);
  236. vec3 as = axis * sin(a);
  237. mat3 p = mat3(axis.x * axis, axis.y * axis, axis.z * axis);
  238. mat3 q = mat3(c, -as.z, as.y, as.z, c, -as.x, -as.y, as.x, c);
  239. return p * (1.0 - c) + q;
  240. }
  241. vec3 getNormal(vec2 uv) {
  242. vec2 g0 = textureLod(gbuffer0, mouse, 0.0).rg;
  243. vec3 n;
  244. n.z = 1.0 - abs(g0.x) - abs(g0.y);
  245. n.xy = n.z >= 0.0 ? g0.xy : octahedronWrap(g0.xy);
  246. return n;
  247. }
  248. void main() {
  249. texCoord = tex;
  250. float depth = textureLod(gbufferD, mouse, 0.0).r;
  251. vec4 wpos = vec4(mouse * 2.0 - 1.0, depth * 2.0 - 1.0, 1.0);
  252. wpos = invVP * wpos;
  253. wpos.xyz /= wpos.w;
  254. vec3 n = normalize(
  255. getNormal(mouse + vec2(step.x, step.y)) +
  256. getNormal(mouse + vec2(-step.x, step.y)) +
  257. getNormal(mouse + vec2(-step.x, -step.y)) +
  258. getNormal(mouse + vec2(step.x, -step.y)) +
  259. getNormal(mouse)
  260. );
  261. float ax = acos(dot(vec3(1,0,0), vec3(n.x,0,0)));
  262. float az = acos(dot(vec3(0,0,1), vec3(0,0,n.z)));
  263. float sy = -sign(n.y);
  264. wpos.xyz +=
  265. rotAxis(vec3(1,0,0), -az * sy + 3.14/2) *
  266. rotAxis(vec3(0,0,1), ax + 3.14/2) *
  267. (pos.xyz * radius);
  268. gl_Position = VP * vec4(wpos.xyz, 1.0);
  269. }
  270. ";
  271. public static var cursorFrag = "#version 330
  272. uniform sampler2D tex;
  273. in vec2 texCoord;
  274. out vec4 FragColor;
  275. void main() {
  276. vec4 col = texture(tex, texCoord);
  277. FragColor = vec4(col.rgb / col.a, col.a);
  278. }
  279. ";
  280. #end
  281. #if kha_krom
  282. public static var font_x0 = [1,2,6,10,18,25,34,42,45,50,55,61,68,71,75,79,85,92,97,104,111,118,1,8,15,22,29,32,35,41,48,55,61,72,81,89,97,105,112,119,1,9,13,20,28,35,45,53,61,69,77,85,93,101,109,1,12,20,28,36,40,46,50,56,62,66,73,80,87,94,101,106,113,120,123,1,8,11,21,28,35,42,49,54,61,66,73,80,90,97,104,111,116,119,1];
  283. public static var font_y0 = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,26,26,26,26,26,26,26,26,26,26,26,26,26,26,26,37,37,37,37,37,37,37,37,37,37,37,37,37,37,37,37,37,37,37,37,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,62];
  284. public static var font_x1 = [1,5,9,17,24,33,41,44,49,54,60,67,70,74,78,84,91,96,103,110,117,124,7,14,21,28,31,34,40,47,54,60,71,80,88,96,104,111,118,126,8,12,19,27,34,44,52,60,68,76,84,92,100,108,116,11,19,27,35,39,45,49,55,61,65,72,79,86,93,100,105,112,119,122,126,7,10,20,27,34,41,48,53,60,65,72,79,89,96,103,110,115,118,123,8];
  285. public static var font_y1 = [1,10,5,9,13,11,10,5,13,13,6,8,5,3,4,10,10,9,9,10,9,10,23,22,23,23,21,22,19,18,19,23,25,22,22,23,22,22,22,23,34,34,35,34,34,34,34,35,34,36,34,35,34,35,34,45,45,45,45,49,46,49,42,38,40,44,47,44,47,44,46,46,46,45,48,59,59,56,56,57,59,59,56,57,59,57,56,56,56,59,56,61,60,61,65];
  286. public static var font_xoff = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
  287. public static var font_yoff = [10,2,1,2,0,1,2,1,1,1,2,3,8,6,8,2,2,2,2,2,2,2,2,2,2,2,4,4,4,4,4,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,0,2,0,2,10,1,4,1,4,1,4,1,4,1,2,2,1,1,4,4,4,4,4,4,4,2,4,4,4,4,4,4,1,2,1,5];
  288. public static var font_xadvance = [2.74625,2.85458,3.54791,6.83041,6.22916,8.125,6.89541,1.93375,3.79166,3.85666,4.7775,6.28875,2.1775,3.06041,2.91958,4.57166,6.22916,6.22916,6.22916,6.22916,6.22916,6.22916,6.22916,6.22916,6.22916,6.22916,2.68666,2.34541,5.63875,6.08833,5.79583,5.23791,9.96125,7.23666,6.90625,7.22041,7.27458,6.305,6.13166,7.55625,7.9083,3.01708,6.12083,6.955,5.96916,9.685,7.90833,7.62666,6.99833,7.62666,6.83041,6.58125,6.61916,7.19333,7.05791,9.84208,6.955,6.6625,6.64083,2.94125,4.55,2.94125,4.63666,5.005,3.42875,6.03416,6.22375,5.80666,6.25625,5.87708,3.85125,6.22375,6.11,2.69208,2.64875,5.6225,2.69208,9.72291,6.12083,6.32666,6.22375,6.305,3.75375,5.72,3.62375,6.11541,5.37333,8.33625,5.49791,5.24875,5.49791,3.75375,2.70291,3.75375,7.54541];
  289. #end
  290. #if arm_ltc
  291. public static var ltcMatTex: Image = null;
  292. public static var ltcMagTex: Image = null;
  293. public static function initLTC() {
  294. // Real-Time Polygonal-Light Shading with Linearly Transformed Cosines
  295. // https://eheitzresearch.wordpress.com/415-2/
  296. Data.getBlob("ltc_mat.arm", function(ltc_mat:Blob) {
  297. Data.getBlob("ltc_mag.arm", function(ltc_mag:Blob) {
  298. ltcMatTex = Image.fromBytes(ArmPack.decode(ltc_mat.toBytes()), 64, 64, TextureFormat.RGBA128);
  299. ltcMagTex = Image.fromBytes(ArmPack.decode(ltc_mag.toBytes()), 64, 64, TextureFormat.A32);
  300. });
  301. });
  302. }
  303. #end
  304. }