GBufferGeneric.ankiprog 11 KB

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