GBufferGeneric.ankiprog 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. // Copyright (C) 2009-2021, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #pragma anki mutator ANKI_INSTANCED 0 1
  6. #pragma anki mutator ANKI_LOD 0 1 2
  7. #pragma anki mutator ANKI_VELOCITY 0 1
  8. #pragma anki mutator ANKI_PASS 0 2 3
  9. #pragma anki mutator ANKI_BONES 0 1
  10. #pragma anki mutator DIFFUSE_TEX 0 1
  11. #pragma anki mutator SPECULAR_TEX 0 1
  12. #pragma anki mutator ROUGHNESS_TEX 0 1
  13. #pragma anki mutator METAL_TEX 0 1
  14. #pragma anki mutator NORMAL_TEX 0 1
  15. #pragma anki mutator PARALLAX 0 1
  16. #pragma anki mutator EMISSIVE_TEX 0 1
  17. #pragma anki rewrite_mutation ANKI_PASS 2 DIFFUSE_TEX 1 to ANKI_PASS 2 DIFFUSE_TEX 0
  18. #pragma anki rewrite_mutation ANKI_PASS 3 DIFFUSE_TEX 1 to ANKI_PASS 3 DIFFUSE_TEX 0
  19. #pragma anki rewrite_mutation ANKI_PASS 2 SPECULAR_TEX 1 to ANKI_PASS 2 SPECULAR_TEX 0
  20. #pragma anki rewrite_mutation ANKI_PASS 3 SPECULAR_TEX 1 to ANKI_PASS 3 SPECULAR_TEX 0
  21. #pragma anki rewrite_mutation ANKI_PASS 2 NORMAL_TEX 1 to ANKI_PASS 2 NORMAL_TEX 0
  22. #pragma anki rewrite_mutation ANKI_PASS 3 NORMAL_TEX 1 to ANKI_PASS 3 NORMAL_TEX 0
  23. #pragma anki rewrite_mutation ANKI_PASS 2 ROUGHNESS_TEX 1 to ANKI_PASS 2 ROUGHNESS_TEX 0
  24. #pragma anki rewrite_mutation ANKI_PASS 3 ROUGHNESS_TEX 1 to ANKI_PASS 3 ROUGHNESS_TEX 0
  25. #pragma anki rewrite_mutation ANKI_PASS 2 METAL_TEX 1 to ANKI_PASS 2 METAL_TEX 0
  26. #pragma anki rewrite_mutation ANKI_PASS 3 METAL_TEX 1 to ANKI_PASS 3 METAL_TEX 0
  27. #pragma anki rewrite_mutation ANKI_PASS 2 EMISSIVE_TEX 1 to ANKI_PASS 2 EMISSIVE_TEX 0
  28. #pragma anki rewrite_mutation ANKI_PASS 3 EMISSIVE_TEX 1 to ANKI_PASS 3 EMISSIVE_TEX 0
  29. #pragma anki rewrite_mutation ANKI_PASS 2 PARALLAX 1 to ANKI_PASS 2 PARALLAX 0
  30. #pragma anki rewrite_mutation ANKI_PASS 3 PARALLAX 1 to ANKI_PASS 3 PARALLAX 0
  31. #define REALLY_USING_PARALLAX (PARALLAX == 1 && ANKI_PASS == 0 && ANKI_LOD == 0)
  32. #include <AnKi/Shaders/GBufferCommon.glsl>
  33. #pragma anki reflect u_ankiGlobalSampler
  34. layout(set = 0, binding = 2) uniform sampler u_ankiGlobalSampler;
  35. #if DIFFUSE_TEX == 1 && ANKI_PASS == PASS_GB
  36. # pragma anki reflect u_diffTex
  37. layout(set = 0, binding = 3) uniform texture2D u_diffTex;
  38. # define USING_DIFF_TEX 1
  39. #endif
  40. #if SPECULAR_TEX == 1 && ANKI_PASS == PASS_GB
  41. # pragma anki reflect u_specTex
  42. layout(set = 0, binding = 4) uniform texture2D u_specTex;
  43. # define USING_SPECULAR_TEX 1
  44. #endif
  45. #if ROUGHNESS_TEX == 1 && ANKI_PASS == PASS_GB
  46. # pragma anki reflect u_roughnessTex
  47. layout(set = 0, binding = 5) uniform texture2D u_roughnessTex;
  48. # define USING_ROUGHNESS_TEX 1
  49. #endif
  50. #if NORMAL_TEX == 1 && ANKI_PASS == PASS_GB && ANKI_LOD < 2
  51. # pragma anki reflect u_normalTex
  52. layout(set = 0, binding = 6) uniform texture2D u_normalTex;
  53. # define USING_NORMAL_TEX 1
  54. #endif
  55. #if METAL_TEX == 1 && ANKI_PASS == PASS_GB
  56. # pragma anki reflect u_metallicTex
  57. layout(set = 0, binding = 7) uniform texture2D u_metallicTex;
  58. # define USING_METALLIC_TEX 1
  59. #endif
  60. #if REALLY_USING_PARALLAX
  61. # pragma anki reflect u_heightTex
  62. layout(set = 0, binding = 8) uniform texture2D u_heightTex;
  63. #endif
  64. #if EMISSIVE_TEX == 1 && ANKI_PASS == PASS_GB
  65. # pragma anki reflect u_emissiveTex
  66. layout(set = 0, binding = 9) uniform texture2D u_emissiveTex;
  67. # define USING_EMISSIVE_TEX 1
  68. #endif
  69. #if ANKI_PASS == PASS_GB
  70. struct PerDraw
  71. {
  72. # if !defined(USING_DIFF_TEX)
  73. Vec3 m_diffColor;
  74. # endif
  75. # if !defined(USING_ROUGHNESS_TEX)
  76. F32 m_roughness;
  77. # endif
  78. # if !defined(USING_SPECULAR_TEX)
  79. Vec3 m_specColor;
  80. # endif
  81. # if !defined(USING_METALLIC_TEX)
  82. F32 m_metallic;
  83. # endif
  84. # if !defined(USING_EMISSIVE_TEX)
  85. Vec3 m_emission;
  86. # endif
  87. # if REALLY_USING_PARALLAX
  88. F32 m_heightmapScale;
  89. # endif
  90. # if ANKI_PASS == PASS_GB
  91. F32 m_subsurface;
  92. # endif
  93. };
  94. #endif
  95. struct PerInstance
  96. {
  97. Mat4 m_ankiMvp;
  98. #if ANKI_PASS == PASS_GB
  99. Mat3 m_ankiRotationMatrix;
  100. #endif
  101. #if REALLY_USING_PARALLAX
  102. Mat4 m_ankiModelViewMatrix;
  103. #endif
  104. #if ANKI_PASS == PASS_GB && ANKI_VELOCITY == 1
  105. Mat4 m_ankiPreviousMvp;
  106. #endif
  107. };
  108. #if ANKI_PASS == PASS_GB
  109. # pragma anki reflect b_ankiPerDraw
  110. layout(set = 0, binding = 0, row_major, std140) uniform b_ankiPerDraw
  111. {
  112. PerDraw u_ankiPerDraw;
  113. };
  114. #endif
  115. #pragma anki reflect b_ankiPerInstance
  116. layout(set = 0, binding = 1, row_major, std140) uniform b_ankiPerInstance
  117. {
  118. PerInstance u_ankiPerInstance[MAX_INSTANCE_COUNT];
  119. };
  120. #if ANKI_BONES
  121. # pragma anki reflect b_ankiBoneTransforms
  122. layout(set = 0, binding = 10, row_major, std140) readonly buffer b_ankiBoneTransforms
  123. {
  124. Mat4 u_ankiBoneTransforms[];
  125. };
  126. # pragma anki reflect b_ankiPrevFrameBoneTransforms
  127. layout(set = 0, binding = 11, row_major, std140) readonly buffer b_ankiPrevFrameBoneTransforms
  128. {
  129. Mat4 u_ankiPrevFrameBoneTransforms[];
  130. };
  131. #endif
  132. #if !ANKI_INSTANCED
  133. # define INSTANCE_ID 0
  134. #else
  135. # define INSTANCE_ID gl_InstanceIndex
  136. #endif
  137. #pragma anki start vert
  138. // Globals (always in local space)
  139. Vec3 g_position = in_position;
  140. #if ANKI_PASS == PASS_GB
  141. Vec3 g_prevPosition = in_position;
  142. Vec2 g_uv = in_uv;
  143. Vec3 g_normal = in_normal;
  144. Vec4 g_tangent = in_tangent;
  145. #endif
  146. // Perform skinning
  147. #if ANKI_BONES
  148. void skinning()
  149. {
  150. Mat4 skinMat = u_ankiBoneTransforms[in_boneIndices[0]] * in_boneWeights[0];
  151. Mat4 prevSkinMat = u_ankiPrevFrameBoneTransforms[in_boneIndices[0]] * in_boneWeights[0];
  152. ANKI_UNROLL for(U32 i = 1u; i < 4u; ++i)
  153. {
  154. skinMat += u_ankiBoneTransforms[in_boneIndices[i]] * in_boneWeights[i];
  155. prevSkinMat += u_ankiPrevFrameBoneTransforms[in_boneIndices[i]] * in_boneWeights[i];
  156. }
  157. # if ANKI_PASS == PASS_GB
  158. g_prevPosition = (prevSkinMat * Vec4(g_position, 1.0)).xyz;
  159. g_tangent.xyz = (skinMat * Vec4(g_tangent.xyz, 0.0)).xyz;
  160. g_normal = (skinMat * Vec4(g_normal, 0.0)).xyz;
  161. # endif
  162. g_position = (skinMat * Vec4(g_position, 1.0)).xyz;
  163. }
  164. #endif
  165. // Common store function
  166. #if ANKI_PASS == PASS_GB
  167. void positionUvNormalTangent()
  168. {
  169. gl_Position = u_ankiPerInstance[INSTANCE_ID].m_ankiMvp * Vec4(g_position, 1.0);
  170. out_normal = u_ankiPerInstance[INSTANCE_ID].m_ankiRotationMatrix * g_normal.xyz;
  171. out_tangent = u_ankiPerInstance[INSTANCE_ID].m_ankiRotationMatrix * g_tangent.xyz;
  172. out_bitangent = cross(out_normal, out_tangent) * g_tangent.w;
  173. out_uv = g_uv;
  174. }
  175. #endif
  176. // Store stuff for parallax mapping
  177. #if REALLY_USING_PARALLAX
  178. void parallax()
  179. {
  180. const Mat4 modelViewMat = u_ankiPerInstance[INSTANCE_ID].m_ankiModelViewMatrix;
  181. const Vec3 n = in_normal;
  182. const Vec3 t = in_tangent.xyz;
  183. const Vec3 b = cross(n, t) * in_tangent.w;
  184. const Mat3 normalMat = Mat3(modelViewMat);
  185. const Mat3 invTbn = transpose(normalMat * Mat3(t, b, n));
  186. const Vec3 viewPos = (modelViewMat * Vec4(g_position, 1.0)).xyz;
  187. out_distFromTheCamera = viewPos.z;
  188. out_eyeTangentSpace = invTbn * viewPos;
  189. out_normalTangentSpace = invTbn * n;
  190. }
  191. #endif
  192. #if(ANKI_VELOCITY || ANKI_BONES) && ANKI_PASS == PASS_GB
  193. void velocity()
  194. {
  195. const Vec3 prevLocalPos = g_prevPosition;
  196. # if ANKI_VELOCITY
  197. const Mat4 mvp = u_ankiPerInstance[INSTANCE_ID].m_ankiPreviousMvp;
  198. # else
  199. const Mat4 mvp = u_ankiPerInstance[INSTANCE_ID].m_ankiMvp;
  200. # endif
  201. const Vec4 v4 = mvp * Vec4(prevLocalPos, 1.0);
  202. const Vec2 prevNdc = v4.xy / v4.w;
  203. const Vec2 crntNdc = gl_Position.xy / gl_Position.w;
  204. // It's NDC_TO_UV(prevNdc) - NDC_TO_UV(crntNdc) or:
  205. out_velocity = (prevNdc - crntNdc) * 0.5;
  206. }
  207. #endif
  208. void main()
  209. {
  210. #if ANKI_BONES
  211. skinning();
  212. #endif
  213. #if ANKI_PASS == PASS_GB
  214. positionUvNormalTangent();
  215. # if REALLY_USING_PARALLAX
  216. parallax();
  217. # endif
  218. # if ANKI_VELOCITY || ANKI_BONES
  219. velocity();
  220. # endif
  221. #else
  222. gl_Position = u_ankiPerInstance[INSTANCE_ID].m_ankiMvp * Vec4(g_position, 1.0);
  223. #endif
  224. }
  225. #pragma anki end
  226. #pragma anki start frag
  227. #if REALLY_USING_PARALLAX
  228. Vec2 computeTextureCoordParallax(texture2D heightMap, sampler sampl, Vec2 uv, F32 heightMapScale)
  229. {
  230. const U32 MAX_SAMPLES = 25u;
  231. const U32 MIN_SAMPLES = 1u;
  232. const F32 MAX_EFFECTIVE_DISTANCE = 32.0;
  233. // Get that because we are sampling inside a loop
  234. const Vec2 dPdx = dFdx(uv);
  235. const Vec2 dPdy = dFdy(uv);
  236. const Vec3 eyeTangentSpace = in_eyeTangentSpace;
  237. const Vec3 normTangentSpace = in_normalTangentSpace;
  238. F32 parallaxLimit = -length(eyeTangentSpace.xy) / eyeTangentSpace.z;
  239. parallaxLimit *= heightMapScale;
  240. const Vec2 offsetDir = normalize(eyeTangentSpace.xy);
  241. const Vec2 maxOffset = offsetDir * parallaxLimit;
  242. const Vec3 E = normalize(eyeTangentSpace);
  243. const F32 factor0 = -dot(E, normTangentSpace);
  244. const F32 factor1 = in_distFromTheCamera / -MAX_EFFECTIVE_DISTANCE;
  245. const F32 factor = saturate((1.0 - factor0) * (1.0 - factor1));
  246. const F32 sampleCountf = mix(F32(MIN_SAMPLES), F32(MAX_SAMPLES), factor);
  247. const F32 stepSize = 1.0 / sampleCountf;
  248. F32 crntRayHeight = 1.0;
  249. Vec2 crntOffset = Vec2(0.0);
  250. Vec2 lastOffset = Vec2(0.0);
  251. F32 lastSampledHeight = 1.0;
  252. F32 crntSampledHeight = 1.0;
  253. U32 crntSample = 0u;
  254. const U32 sampleCount = U32(sampleCountf);
  255. ANKI_LOOP while(crntSample < sampleCount)
  256. {
  257. crntSampledHeight = textureGrad(heightMap, sampl, uv + crntOffset, dPdx, dPdy).r;
  258. if(crntSampledHeight > crntRayHeight)
  259. {
  260. const F32 delta1 = crntSampledHeight - crntRayHeight;
  261. const F32 delta2 = (crntRayHeight + stepSize) - lastSampledHeight;
  262. const F32 ratio = delta1 / (delta1 + delta2);
  263. crntOffset = mix(crntOffset, lastOffset, ratio);
  264. crntSample = sampleCount + 1u;
  265. }
  266. else
  267. {
  268. crntSample++;
  269. crntRayHeight -= stepSize;
  270. lastOffset = crntOffset;
  271. crntOffset += stepSize * maxOffset;
  272. lastSampledHeight = crntSampledHeight;
  273. }
  274. }
  275. return uv + crntOffset;
  276. }
  277. #endif
  278. // Do normal mapping
  279. #if ANKI_PASS == PASS_GB
  280. Vec3 readNormalFromTexture(texture2D map, sampler sampl, highp Vec2 texCoords)
  281. {
  282. // First read the texture
  283. const Vec3 nAtTangentspace = normalize((texture(map, sampl, texCoords).rgb - 0.5) * 2.0);
  284. const Vec3 n = normalize(in_normal);
  285. const Vec3 t = normalize(in_tangent);
  286. const Vec3 b = normalize(in_bitangent);
  287. const Mat3 tbnMat = Mat3(t, b, n);
  288. return tbnMat * nAtTangentspace;
  289. }
  290. #endif
  291. void main()
  292. {
  293. #if ANKI_PASS == PASS_GB
  294. # if REALLY_USING_PARALLAX
  295. const Vec2 uv =
  296. computeTextureCoordParallax(u_heightTex, u_ankiGlobalSampler, in_uv, u_ankiPerDraw.m_heightmapScale);
  297. # else
  298. const Vec2 uv = in_uv;
  299. # endif
  300. # if defined(USING_DIFF_TEX)
  301. const Vec3 diffColor = texture(u_diffTex, u_ankiGlobalSampler, uv).rgb;
  302. # else
  303. const Vec3 diffColor = u_ankiPerDraw.m_diffColor;
  304. # endif
  305. # if defined(USING_SPECULAR_TEX)
  306. const Vec3 specColor = texture(u_specTex, u_ankiGlobalSampler, uv).rgb;
  307. # else
  308. const Vec3 specColor = u_ankiPerDraw.m_specColor;
  309. # endif
  310. # if defined(USING_ROUGHNESS_TEX)
  311. const F32 roughness = texture(u_roughnessTex, u_ankiGlobalSampler, uv).g;
  312. # else
  313. const F32 roughness = u_ankiPerDraw.m_roughness;
  314. # endif
  315. # if defined(USING_METALLIC_TEX)
  316. const F32 metallic = texture(u_metallicTex, u_ankiGlobalSampler, uv).b;
  317. # else
  318. const F32 metallic = u_ankiPerDraw.m_metallic;
  319. # endif
  320. # if defined(USING_NORMAL_TEX)
  321. const Vec3 normal = readNormalFromTexture(u_normalTex, u_ankiGlobalSampler, uv);
  322. # else
  323. const Vec3 normal = normalize(in_normal);
  324. # endif
  325. # if defined(USING_EMISSIVE_TEX)
  326. const Vec3 emission = texture(u_emissiveTex, u_ankiGlobalSampler, uv).rgb;
  327. # else
  328. const Vec3 emission = u_ankiPerDraw.m_emission;
  329. # endif
  330. # if ANKI_VELOCITY || ANKI_BONES
  331. const Vec2 velocity = in_velocity;
  332. # else
  333. const Vec2 velocity = Vec2(1.0);
  334. # endif
  335. writeGBuffer(diffColor, normal, specColor, roughness, u_ankiPerDraw.m_subsurface, emission, metallic, velocity);
  336. #elif ANKI_PASS == PASS_EZ
  337. out_gbuffer0 = Vec4(0.0);
  338. out_gbuffer1 = Vec4(0.0);
  339. out_gbuffer2 = Vec4(0.0);
  340. out_gbuffer3 = Vec2(0.0);
  341. #endif
  342. }
  343. #pragma anki end