scene_forward_clustered.glsl 108 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303
  1. #[vertex]
  2. #version 450
  3. #VERSION_DEFINES
  4. #include "scene_forward_clustered_inc.glsl"
  5. /* INPUT ATTRIBS */
  6. layout(location = 0) in vec3 vertex_attrib;
  7. //only for pure render depth when normal is not used
  8. #ifdef NORMAL_USED
  9. layout(location = 1) in vec3 normal_attrib;
  10. #endif
  11. #if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED)
  12. layout(location = 2) in vec4 tangent_attrib;
  13. #endif
  14. #if defined(COLOR_USED)
  15. layout(location = 3) in vec4 color_attrib;
  16. #endif
  17. #ifdef UV_USED
  18. layout(location = 4) in vec2 uv_attrib;
  19. #endif
  20. #if defined(UV2_USED) || defined(USE_LIGHTMAP) || defined(MODE_RENDER_MATERIAL)
  21. layout(location = 5) in vec2 uv2_attrib;
  22. #endif
  23. #if defined(CUSTOM0_USED)
  24. layout(location = 6) in vec4 custom0_attrib;
  25. #endif
  26. #if defined(CUSTOM1_USED)
  27. layout(location = 7) in vec4 custom1_attrib;
  28. #endif
  29. #if defined(CUSTOM2_USED)
  30. layout(location = 8) in vec4 custom2_attrib;
  31. #endif
  32. #if defined(CUSTOM3_USED)
  33. layout(location = 9) in vec4 custom3_attrib;
  34. #endif
  35. #if defined(BONES_USED) || defined(USE_PARTICLE_TRAILS)
  36. layout(location = 10) in uvec4 bone_attrib;
  37. #endif
  38. #if defined(WEIGHTS_USED) || defined(USE_PARTICLE_TRAILS)
  39. layout(location = 11) in vec4 weight_attrib;
  40. #endif
  41. /* Varyings */
  42. layout(location = 0) out vec3 vertex_interp;
  43. #ifdef NORMAL_USED
  44. layout(location = 1) out vec3 normal_interp;
  45. #endif
  46. #if defined(COLOR_USED)
  47. layout(location = 2) out vec4 color_interp;
  48. #endif
  49. #ifdef UV_USED
  50. layout(location = 3) out vec2 uv_interp;
  51. #endif
  52. #if defined(UV2_USED) || defined(USE_LIGHTMAP)
  53. layout(location = 4) out vec2 uv2_interp;
  54. #endif
  55. #if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED)
  56. layout(location = 5) out vec3 tangent_interp;
  57. layout(location = 6) out vec3 binormal_interp;
  58. #endif
  59. #ifdef MATERIAL_UNIFORMS_USED
  60. layout(set = MATERIAL_UNIFORM_SET, binding = 0, std140) uniform MaterialUniforms{
  61. #MATERIAL_UNIFORMS
  62. } material;
  63. #endif
  64. invariant gl_Position;
  65. #ifdef MODE_DUAL_PARABOLOID
  66. layout(location = 8) out float dp_clip;
  67. #endif
  68. layout(location = 9) out flat uint instance_index;
  69. #GLOBALS
  70. void main() {
  71. vec4 instance_custom = vec4(0.0);
  72. #if defined(COLOR_USED)
  73. color_interp = color_attrib;
  74. #endif
  75. instance_index = draw_call.instance_index;
  76. bool is_multimesh = bool(instances.data[instance_index].flags & INSTANCE_FLAGS_MULTIMESH);
  77. if (!is_multimesh) {
  78. instance_index += gl_InstanceIndex;
  79. }
  80. mat4 world_matrix = instances.data[instance_index].transform;
  81. mat3 world_normal_matrix;
  82. if (bool(instances.data[instance_index].flags & INSTANCE_FLAGS_NON_UNIFORM_SCALE)) {
  83. world_normal_matrix = inverse(mat3(world_matrix));
  84. } else {
  85. world_normal_matrix = mat3(world_matrix);
  86. }
  87. if (is_multimesh) {
  88. //multimesh, instances are for it
  89. mat4 matrix;
  90. #ifdef USE_PARTICLE_TRAILS
  91. uint trail_size = (instances.data[instance_index].flags >> INSTANCE_FLAGS_PARTICLE_TRAIL_SHIFT) & INSTANCE_FLAGS_PARTICLE_TRAIL_MASK;
  92. uint stride = 3 + 1 + 1; //particles always uses this format
  93. uint offset = trail_size * stride * gl_InstanceIndex;
  94. #ifdef COLOR_USED
  95. vec4 pcolor;
  96. #endif
  97. {
  98. uint boffset = offset + bone_attrib.x * stride;
  99. matrix = mat4(transforms.data[boffset + 0], transforms.data[boffset + 1], transforms.data[boffset + 2], vec4(0.0, 0.0, 0.0, 1.0)) * weight_attrib.x;
  100. #ifdef COLOR_USED
  101. pcolor = transforms.data[boffset + 3] * weight_attrib.x;
  102. #endif
  103. }
  104. if (weight_attrib.y > 0.001) {
  105. uint boffset = offset + bone_attrib.y * stride;
  106. matrix += mat4(transforms.data[boffset + 0], transforms.data[boffset + 1], transforms.data[boffset + 2], vec4(0.0, 0.0, 0.0, 1.0)) * weight_attrib.y;
  107. #ifdef COLOR_USED
  108. pcolor += transforms.data[boffset + 3] * weight_attrib.y;
  109. #endif
  110. }
  111. if (weight_attrib.z > 0.001) {
  112. uint boffset = offset + bone_attrib.z * stride;
  113. matrix += mat4(transforms.data[boffset + 0], transforms.data[boffset + 1], transforms.data[boffset + 2], vec4(0.0, 0.0, 0.0, 1.0)) * weight_attrib.z;
  114. #ifdef COLOR_USED
  115. pcolor += transforms.data[boffset + 3] * weight_attrib.z;
  116. #endif
  117. }
  118. if (weight_attrib.w > 0.001) {
  119. uint boffset = offset + bone_attrib.w * stride;
  120. matrix += mat4(transforms.data[boffset + 0], transforms.data[boffset + 1], transforms.data[boffset + 2], vec4(0.0, 0.0, 0.0, 1.0)) * weight_attrib.w;
  121. #ifdef COLOR_USED
  122. pcolor += transforms.data[boffset + 3] * weight_attrib.w;
  123. #endif
  124. }
  125. instance_custom = transforms.data[offset + 4];
  126. #ifdef COLOR_USED
  127. color_interp *= pcolor;
  128. #endif
  129. #else
  130. uint stride = 0;
  131. {
  132. //TODO implement a small lookup table for the stride
  133. if (bool(instances.data[instance_index].flags & INSTANCE_FLAGS_MULTIMESH_FORMAT_2D)) {
  134. stride += 2;
  135. } else {
  136. stride += 3;
  137. }
  138. if (bool(instances.data[instance_index].flags & INSTANCE_FLAGS_MULTIMESH_HAS_COLOR)) {
  139. stride += 1;
  140. }
  141. if (bool(instances.data[instance_index].flags & INSTANCE_FLAGS_MULTIMESH_HAS_CUSTOM_DATA)) {
  142. stride += 1;
  143. }
  144. }
  145. uint offset = stride * gl_InstanceIndex;
  146. if (bool(instances.data[instance_index].flags & INSTANCE_FLAGS_MULTIMESH_FORMAT_2D)) {
  147. matrix = mat4(transforms.data[offset + 0], transforms.data[offset + 1], vec4(0.0, 0.0, 1.0, 0.0), vec4(0.0, 0.0, 0.0, 1.0));
  148. offset += 2;
  149. } else {
  150. matrix = mat4(transforms.data[offset + 0], transforms.data[offset + 1], transforms.data[offset + 2], vec4(0.0, 0.0, 0.0, 1.0));
  151. offset += 3;
  152. }
  153. if (bool(instances.data[instance_index].flags & INSTANCE_FLAGS_MULTIMESH_HAS_COLOR)) {
  154. #ifdef COLOR_USED
  155. color_interp *= transforms.data[offset];
  156. #endif
  157. offset += 1;
  158. }
  159. if (bool(instances.data[instance_index].flags & INSTANCE_FLAGS_MULTIMESH_HAS_CUSTOM_DATA)) {
  160. instance_custom = transforms.data[offset];
  161. }
  162. #endif
  163. //transpose
  164. matrix = transpose(matrix);
  165. world_matrix = world_matrix * matrix;
  166. world_normal_matrix = world_normal_matrix * mat3(matrix);
  167. }
  168. vec3 vertex = vertex_attrib;
  169. #ifdef NORMAL_USED
  170. vec3 normal = normal_attrib * 2.0 - 1.0;
  171. #endif
  172. #if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED)
  173. vec3 tangent = tangent_attrib.xyz * 2.0 - 1.0;
  174. float binormalf = tangent_attrib.a * 2.0 - 1.0;
  175. vec3 binormal = normalize(cross(normal, tangent) * binormalf);
  176. #endif
  177. #ifdef UV_USED
  178. uv_interp = uv_attrib;
  179. #endif
  180. #if defined(UV2_USED) || defined(USE_LIGHTMAP)
  181. uv2_interp = uv2_attrib;
  182. #endif
  183. #ifdef OVERRIDE_POSITION
  184. vec4 position;
  185. #endif
  186. mat4 projection_matrix = scene_data.projection_matrix;
  187. //using world coordinates
  188. #if !defined(SKIP_TRANSFORM_USED) && defined(VERTEX_WORLD_COORDS_USED)
  189. vertex = (world_matrix * vec4(vertex, 1.0)).xyz;
  190. normal = world_normal_matrix * normal;
  191. #if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED)
  192. tangent = world_normal_matrix * tangent;
  193. binormal = world_normal_matrix * binormal;
  194. #endif
  195. #endif
  196. float roughness = 1.0;
  197. mat4 modelview = scene_data.inv_camera_matrix * world_matrix;
  198. mat3 modelview_normal = mat3(scene_data.inv_camera_matrix) * world_normal_matrix;
  199. {
  200. #CODE : VERTEX
  201. }
  202. // using local coordinates (default)
  203. #if !defined(SKIP_TRANSFORM_USED) && !defined(VERTEX_WORLD_COORDS_USED)
  204. vertex = (modelview * vec4(vertex, 1.0)).xyz;
  205. #ifdef NORMAL_USED
  206. normal = modelview_normal * normal;
  207. #endif
  208. #endif
  209. #if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED)
  210. binormal = modelview_normal * binormal;
  211. tangent = modelview_normal * tangent;
  212. #endif
  213. //using world coordinates
  214. #if !defined(SKIP_TRANSFORM_USED) && defined(VERTEX_WORLD_COORDS_USED)
  215. vertex = (scene_data.inv_camera_matrix * vec4(vertex, 1.0)).xyz;
  216. normal = mat3(scene_data.inverse_normal_matrix) * normal;
  217. #if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED)
  218. binormal = mat3(scene_data.camera_inverse_binormal_matrix) * binormal;
  219. tangent = mat3(scene_data.camera_inverse_tangent_matrix) * tangent;
  220. #endif
  221. #endif
  222. vertex_interp = vertex;
  223. #ifdef NORMAL_USED
  224. normal_interp = normal;
  225. #endif
  226. #if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED)
  227. tangent_interp = tangent;
  228. binormal_interp = binormal;
  229. #endif
  230. #ifdef MODE_RENDER_DEPTH
  231. #ifdef MODE_DUAL_PARABOLOID
  232. vertex_interp.z *= scene_data.dual_paraboloid_side;
  233. dp_clip = vertex_interp.z; //this attempts to avoid noise caused by objects sent to the other parabolloid side due to bias
  234. //for dual paraboloid shadow mapping, this is the fastest but least correct way, as it curves straight edges
  235. vec3 vtx = vertex_interp;
  236. float distance = length(vtx);
  237. vtx = normalize(vtx);
  238. vtx.xy /= 1.0 - vtx.z;
  239. vtx.z = (distance / scene_data.z_far);
  240. vtx.z = vtx.z * 2.0 - 1.0;
  241. vertex_interp = vtx;
  242. #endif
  243. #endif //MODE_RENDER_DEPTH
  244. #ifdef OVERRIDE_POSITION
  245. gl_Position = position;
  246. #else
  247. gl_Position = projection_matrix * vec4(vertex_interp, 1.0);
  248. #endif
  249. #ifdef MODE_RENDER_DEPTH
  250. if (scene_data.pancake_shadows) {
  251. if (gl_Position.z <= 0.00001) {
  252. gl_Position.z = 0.00001;
  253. }
  254. }
  255. #endif
  256. #ifdef MODE_RENDER_MATERIAL
  257. if (scene_data.material_uv2_mode) {
  258. vec2 uv_offset = unpackHalf2x16(draw_call.uv_offset);
  259. gl_Position.xy = (uv2_attrib.xy + uv_offset) * 2.0 - 1.0;
  260. gl_Position.z = 0.00001;
  261. gl_Position.w = 1.0;
  262. }
  263. #endif
  264. }
  265. #[fragment]
  266. #version 450
  267. #VERSION_DEFINES
  268. #include "scene_forward_clustered_inc.glsl"
  269. /* Varyings */
  270. layout(location = 0) in vec3 vertex_interp;
  271. #ifdef NORMAL_USED
  272. layout(location = 1) in vec3 normal_interp;
  273. #endif
  274. #if defined(COLOR_USED)
  275. layout(location = 2) in vec4 color_interp;
  276. #endif
  277. #ifdef UV_USED
  278. layout(location = 3) in vec2 uv_interp;
  279. #endif
  280. #if defined(UV2_USED) || defined(USE_LIGHTMAP)
  281. layout(location = 4) in vec2 uv2_interp;
  282. #endif
  283. #if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED)
  284. layout(location = 5) in vec3 tangent_interp;
  285. layout(location = 6) in vec3 binormal_interp;
  286. #endif
  287. #ifdef MODE_DUAL_PARABOLOID
  288. layout(location = 8) in float dp_clip;
  289. #endif
  290. layout(location = 9) in flat uint instance_index;
  291. //defines to keep compatibility with vertex
  292. #define world_matrix instances.data[instance_index].transform
  293. #define projection_matrix scene_data.projection_matrix
  294. #if defined(ENABLE_SSS) && defined(ENABLE_TRANSMITTANCE)
  295. //both required for transmittance to be enabled
  296. #define LIGHT_TRANSMITTANCE_USED
  297. #endif
  298. #ifdef MATERIAL_UNIFORMS_USED
  299. layout(set = MATERIAL_UNIFORM_SET, binding = 0, std140) uniform MaterialUniforms{
  300. #MATERIAL_UNIFORMS
  301. } material;
  302. #endif
  303. #GLOBALS
  304. #ifdef MODE_RENDER_DEPTH
  305. #ifdef MODE_RENDER_MATERIAL
  306. layout(location = 0) out vec4 albedo_output_buffer;
  307. layout(location = 1) out vec4 normal_output_buffer;
  308. layout(location = 2) out vec4 orm_output_buffer;
  309. layout(location = 3) out vec4 emission_output_buffer;
  310. layout(location = 4) out float depth_output_buffer;
  311. #endif
  312. #ifdef MODE_RENDER_NORMAL_ROUGHNESS
  313. layout(location = 0) out vec4 normal_roughness_output_buffer;
  314. #ifdef MODE_RENDER_GIPROBE
  315. layout(location = 1) out uvec2 giprobe_buffer;
  316. #endif
  317. #endif //MODE_RENDER_NORMAL
  318. #else // RENDER DEPTH
  319. #ifdef MODE_MULTIPLE_RENDER_TARGETS
  320. layout(location = 0) out vec4 diffuse_buffer; //diffuse (rgb) and roughness
  321. layout(location = 1) out vec4 specular_buffer; //specular and SSS (subsurface scatter)
  322. #else
  323. layout(location = 0) out vec4 frag_color;
  324. #endif
  325. #endif // RENDER DEPTH
  326. #ifdef ALPHA_HASH_USED
  327. float hash_2d(vec2 p) {
  328. return fract(1.0e4 * sin(17.0 * p.x + 0.1 * p.y) *
  329. (0.1 + abs(sin(13.0 * p.y + p.x))));
  330. }
  331. float hash_3d(vec3 p) {
  332. return hash_2d(vec2(hash_2d(p.xy), p.z));
  333. }
  334. float compute_alpha_hash_threshold(vec3 pos, float hash_scale) {
  335. vec3 dx = dFdx(pos);
  336. vec3 dy = dFdx(pos);
  337. float delta_max_sqr = max(length(dx), length(dy));
  338. float pix_scale = 1.0 / (hash_scale * delta_max_sqr);
  339. vec2 pix_scales =
  340. vec2(exp2(floor(log2(pix_scale))), exp2(ceil(log2(pix_scale))));
  341. vec2 a_thresh = vec2(hash_3d(floor(pix_scales.x * pos.xyz)),
  342. hash_3d(floor(pix_scales.y * pos.xyz)));
  343. float lerp_factor = fract(log2(pix_scale));
  344. float a_interp = (1.0 - lerp_factor) * a_thresh.x + lerp_factor * a_thresh.y;
  345. float min_lerp = min(lerp_factor, 1.0 - lerp_factor);
  346. vec3 cases = vec3(a_interp * a_interp / (2.0 * min_lerp * (1.0 - min_lerp)),
  347. (a_interp - 0.5 * min_lerp) / (1.0 - min_lerp),
  348. 1.0 - ((1.0 - a_interp) * (1.0 - a_interp) /
  349. (2.0 * min_lerp * (1.0 - min_lerp))));
  350. float alpha_hash_threshold =
  351. (lerp_factor < (1.0 - min_lerp)) ? ((lerp_factor < min_lerp) ? cases.x : cases.y) : cases.z;
  352. return clamp(alpha_hash_threshold, 0.0, 1.0);
  353. }
  354. #endif // ALPHA_HASH_USED
  355. #ifdef ALPHA_ANTIALIASING_EDGE_USED
  356. float calc_mip_level(vec2 texture_coord) {
  357. vec2 dx = dFdx(texture_coord);
  358. vec2 dy = dFdy(texture_coord);
  359. float delta_max_sqr = max(dot(dx, dx), dot(dy, dy));
  360. return max(0.0, 0.5 * log2(delta_max_sqr));
  361. }
  362. float compute_alpha_antialiasing_edge(float input_alpha, vec2 texture_coord, float alpha_edge) {
  363. input_alpha *= 1.0 + max(0, calc_mip_level(texture_coord)) * 0.25; // 0.25 mip scale, magic number
  364. input_alpha = (input_alpha - alpha_edge) / max(fwidth(input_alpha), 0.0001) + 0.5;
  365. return clamp(input_alpha, 0.0, 1.0);
  366. }
  367. #endif // ALPHA_ANTIALIASING_USED
  368. // This returns the G_GGX function divided by 2 cos_theta_m, where in practice cos_theta_m is either N.L or N.V.
  369. // We're dividing this factor off because the overall term we'll end up looks like
  370. // (see, for example, the first unnumbered equation in B. Burley, "Physically Based Shading at Disney", SIGGRAPH 2012):
  371. //
  372. // F(L.V) D(N.H) G(N.L) G(N.V) / (4 N.L N.V)
  373. //
  374. // We're basically regouping this as
  375. //
  376. // F(L.V) D(N.H) [G(N.L)/(2 N.L)] [G(N.V) / (2 N.V)]
  377. //
  378. // and thus, this function implements the [G(N.m)/(2 N.m)] part with m = L or V.
  379. //
  380. // The contents of the D and G (G1) functions (GGX) are taken from
  381. // E. Heitz, "Understanding the Masking-Shadowing Function in Microfacet-Based BRDFs", J. Comp. Graph. Tech. 3 (2) (2014).
  382. // Eqns 71-72 and 85-86 (see also Eqns 43 and 80).
  383. #if !defined(MODE_RENDER_DEPTH) && !defined(MODE_UNSHADED)
  384. float G_GGX_2cos(float cos_theta_m, float alpha) {
  385. // Schlick's approximation
  386. // C. Schlick, "An Inexpensive BRDF Model for Physically-based Rendering", Computer Graphics Forum. 13 (3): 233 (1994)
  387. // Eq. (19), although see Heitz (2014) the about the problems with his derivation.
  388. // It nevertheless approximates GGX well with k = alpha/2.
  389. float k = 0.5 * alpha;
  390. return 0.5 / (cos_theta_m * (1.0 - k) + k);
  391. // float cos2 = cos_theta_m * cos_theta_m;
  392. // float sin2 = (1.0 - cos2);
  393. // return 1.0 / (cos_theta_m + sqrt(cos2 + alpha * alpha * sin2));
  394. }
  395. float D_GGX(float cos_theta_m, float alpha) {
  396. float alpha2 = alpha * alpha;
  397. float d = 1.0 + (alpha2 - 1.0) * cos_theta_m * cos_theta_m;
  398. return alpha2 / (M_PI * d * d);
  399. }
  400. float G_GGX_anisotropic_2cos(float cos_theta_m, float alpha_x, float alpha_y, float cos_phi, float sin_phi) {
  401. float cos2 = cos_theta_m * cos_theta_m;
  402. float sin2 = (1.0 - cos2);
  403. float s_x = alpha_x * cos_phi;
  404. float s_y = alpha_y * sin_phi;
  405. return 1.0 / max(cos_theta_m + sqrt(cos2 + (s_x * s_x + s_y * s_y) * sin2), 0.001);
  406. }
  407. float D_GGX_anisotropic(float cos_theta_m, float alpha_x, float alpha_y, float cos_phi, float sin_phi) {
  408. float cos2 = cos_theta_m * cos_theta_m;
  409. float sin2 = (1.0 - cos2);
  410. float r_x = cos_phi / alpha_x;
  411. float r_y = sin_phi / alpha_y;
  412. float d = cos2 + sin2 * (r_x * r_x + r_y * r_y);
  413. return 1.0 / max(M_PI * alpha_x * alpha_y * d * d, 0.001);
  414. }
  415. float SchlickFresnel(float u) {
  416. float m = 1.0 - u;
  417. float m2 = m * m;
  418. return m2 * m2 * m; // pow(m,5)
  419. }
  420. float GTR1(float NdotH, float a) {
  421. if (a >= 1.0)
  422. return 1.0 / M_PI;
  423. float a2 = a * a;
  424. float t = 1.0 + (a2 - 1.0) * NdotH * NdotH;
  425. return (a2 - 1.0) / (M_PI * log(a2) * t);
  426. }
  427. vec3 F0(float metallic, float specular, vec3 albedo) {
  428. float dielectric = 0.16 * specular * specular;
  429. // use albedo * metallic as colored specular reflectance at 0 angle for metallic materials;
  430. // see https://google.github.io/filament/Filament.md.html
  431. return mix(vec3(dielectric), albedo, vec3(metallic));
  432. }
  433. void light_compute(vec3 N, vec3 L, vec3 V, vec3 light_color, float attenuation, vec3 f0, uint orms, float specular_amount,
  434. #ifdef LIGHT_BACKLIGHT_USED
  435. vec3 backlight,
  436. #endif
  437. #ifdef LIGHT_TRANSMITTANCE_USED
  438. vec4 transmittance_color,
  439. float transmittance_depth,
  440. float transmittance_curve,
  441. float transmittance_boost,
  442. float transmittance_z,
  443. #endif
  444. #ifdef LIGHT_RIM_USED
  445. float rim, float rim_tint, vec3 rim_color,
  446. #endif
  447. #ifdef LIGHT_CLEARCOAT_USED
  448. float clearcoat, float clearcoat_gloss,
  449. #endif
  450. #ifdef LIGHT_ANISOTROPY_USED
  451. vec3 B, vec3 T, float anisotropy,
  452. #endif
  453. #ifdef USE_SOFT_SHADOWS
  454. float A,
  455. #endif
  456. #ifdef USE_SHADOW_TO_OPACITY
  457. inout float alpha,
  458. #endif
  459. inout vec3 diffuse_light, inout vec3 specular_light) {
  460. #if defined(LIGHT_CODE_USED)
  461. // light is written by the light shader
  462. vec3 normal = N;
  463. vec3 light = L;
  464. vec3 view = V;
  465. #CODE : LIGHT
  466. #else
  467. #ifdef USE_SOFT_SHADOWS
  468. float NdotL = min(A + dot(N, L), 1.0);
  469. #else
  470. float NdotL = dot(N, L);
  471. #endif
  472. float cNdotL = max(NdotL, 0.0); // clamped NdotL
  473. float NdotV = dot(N, V);
  474. float cNdotV = max(NdotV, 0.0);
  475. #if defined(DIFFUSE_BURLEY) || defined(SPECULAR_BLINN) || defined(SPECULAR_SCHLICK_GGX) || defined(LIGHT_CLEARCOAT_USED)
  476. vec3 H = normalize(V + L);
  477. #endif
  478. #if defined(SPECULAR_BLINN) || defined(SPECULAR_SCHLICK_GGX) || defined(LIGHT_CLEARCOAT_USED)
  479. #ifdef USE_SOFT_SHADOWS
  480. float cNdotH = clamp(A + dot(N, H), 0.0, 1.0);
  481. #else
  482. float cNdotH = clamp(dot(N, H), 0.0, 1.0);
  483. #endif
  484. #endif
  485. #if defined(DIFFUSE_BURLEY) || defined(SPECULAR_SCHLICK_GGX) || defined(LIGHT_CLEARCOAT_USED)
  486. #ifdef USE_SOFT_SHADOWS
  487. float cLdotH = clamp(A + dot(L, H), 0.0, 1.0);
  488. #else
  489. float cLdotH = clamp(dot(L, H), 0.0, 1.0);
  490. #endif
  491. #endif
  492. float metallic = unpackUnorm4x8(orms).z;
  493. if (metallic < 1.0) {
  494. float roughness = unpackUnorm4x8(orms).y;
  495. #if defined(DIFFUSE_OREN_NAYAR)
  496. vec3 diffuse_brdf_NL;
  497. #else
  498. float diffuse_brdf_NL; // BRDF times N.L for calculating diffuse radiance
  499. #endif
  500. #if defined(DIFFUSE_LAMBERT_WRAP)
  501. // energy conserving lambert wrap shader
  502. diffuse_brdf_NL = max(0.0, (NdotL + roughness) / ((1.0 + roughness) * (1.0 + roughness)));
  503. #elif defined(DIFFUSE_TOON)
  504. diffuse_brdf_NL = smoothstep(-roughness, max(roughness, 0.01), NdotL);
  505. #elif defined(DIFFUSE_BURLEY)
  506. {
  507. float FD90_minus_1 = 2.0 * cLdotH * cLdotH * roughness - 0.5;
  508. float FdV = 1.0 + FD90_minus_1 * SchlickFresnel(cNdotV);
  509. float FdL = 1.0 + FD90_minus_1 * SchlickFresnel(cNdotL);
  510. diffuse_brdf_NL = (1.0 / M_PI) * FdV * FdL * cNdotL;
  511. /*
  512. float energyBias = mix(roughness, 0.0, 0.5);
  513. float energyFactor = mix(roughness, 1.0, 1.0 / 1.51);
  514. float fd90 = energyBias + 2.0 * VoH * VoH * roughness;
  515. float f0 = 1.0;
  516. float lightScatter = f0 + (fd90 - f0) * pow(1.0 - cNdotL, 5.0);
  517. float viewScatter = f0 + (fd90 - f0) * pow(1.0 - cNdotV, 5.0);
  518. diffuse_brdf_NL = lightScatter * viewScatter * energyFactor;
  519. */
  520. }
  521. #else
  522. // lambert
  523. diffuse_brdf_NL = cNdotL * (1.0 / M_PI);
  524. #endif
  525. diffuse_light += light_color * diffuse_brdf_NL * attenuation;
  526. #if defined(LIGHT_BACKLIGHT_USED)
  527. diffuse_light += light_color * (vec3(1.0 / M_PI) - diffuse_brdf_NL) * backlight * attenuation;
  528. #endif
  529. #if defined(LIGHT_RIM_USED)
  530. float rim_light = pow(max(0.0, 1.0 - cNdotV), max(0.0, (1.0 - roughness) * 16.0));
  531. diffuse_light += rim_light * rim * mix(vec3(1.0), rim_color, rim_tint) * light_color;
  532. #endif
  533. #ifdef LIGHT_TRANSMITTANCE_USED
  534. #ifdef SSS_MODE_SKIN
  535. {
  536. float scale = 8.25 / transmittance_depth;
  537. float d = scale * abs(transmittance_z);
  538. float dd = -d * d;
  539. vec3 profile = vec3(0.233, 0.455, 0.649) * exp(dd / 0.0064) +
  540. vec3(0.1, 0.336, 0.344) * exp(dd / 0.0484) +
  541. vec3(0.118, 0.198, 0.0) * exp(dd / 0.187) +
  542. vec3(0.113, 0.007, 0.007) * exp(dd / 0.567) +
  543. vec3(0.358, 0.004, 0.0) * exp(dd / 1.99) +
  544. vec3(0.078, 0.0, 0.0) * exp(dd / 7.41);
  545. diffuse_light += profile * transmittance_color.a * light_color * clamp(transmittance_boost - NdotL, 0.0, 1.0) * (1.0 / M_PI);
  546. }
  547. #else
  548. if (transmittance_depth > 0.0) {
  549. float fade = clamp(abs(transmittance_z / transmittance_depth), 0.0, 1.0);
  550. fade = pow(max(0.0, 1.0 - fade), transmittance_curve);
  551. fade *= clamp(transmittance_boost - NdotL, 0.0, 1.0);
  552. diffuse_light += transmittance_color.rgb * light_color * (1.0 / M_PI) * transmittance_color.a * fade;
  553. }
  554. #endif //SSS_MODE_SKIN
  555. #endif //LIGHT_TRANSMITTANCE_USED
  556. }
  557. float roughness = unpackUnorm4x8(orms).y;
  558. if (roughness > 0.0) { // FIXME: roughness == 0 should not disable specular light entirely
  559. // D
  560. #if defined(SPECULAR_BLINN)
  561. //normalized blinn
  562. float shininess = exp2(15.0 * (1.0 - roughness) + 1.0) * 0.25;
  563. float blinn = pow(cNdotH, shininess) * cNdotL;
  564. blinn *= (shininess + 8.0) * (1.0 / (8.0 * M_PI));
  565. float intensity = blinn;
  566. specular_light += light_color * intensity * attenuation * specular_amount;
  567. #elif defined(SPECULAR_PHONG)
  568. vec3 R = normalize(-reflect(L, N));
  569. float cRdotV = clamp(A + dot(R, V), 0.0, 1.0);
  570. float shininess = exp2(15.0 * (1.0 - roughness) + 1.0) * 0.25;
  571. float phong = pow(cRdotV, shininess);
  572. phong *= (shininess + 8.0) * (1.0 / (8.0 * M_PI));
  573. float intensity = (phong) / max(4.0 * cNdotV * cNdotL, 0.75);
  574. specular_light += light_color * intensity * attenuation * specular_amount;
  575. #elif defined(SPECULAR_TOON)
  576. vec3 R = normalize(-reflect(L, N));
  577. float RdotV = dot(R, V);
  578. float mid = 1.0 - roughness;
  579. mid *= mid;
  580. float intensity = smoothstep(mid - roughness * 0.5, mid + roughness * 0.5, RdotV) * mid;
  581. diffuse_light += light_color * intensity * attenuation * specular_amount; // write to diffuse_light, as in toon shading you generally want no reflection
  582. #elif defined(SPECULAR_DISABLED)
  583. // none..
  584. #elif defined(SPECULAR_SCHLICK_GGX)
  585. // shlick+ggx as default
  586. #if defined(LIGHT_ANISOTROPY_USED)
  587. float alpha_ggx = roughness * roughness;
  588. float aspect = sqrt(1.0 - anisotropy * 0.9);
  589. float ax = alpha_ggx / aspect;
  590. float ay = alpha_ggx * aspect;
  591. float XdotH = dot(T, H);
  592. float YdotH = dot(B, H);
  593. float D = D_GGX_anisotropic(cNdotH, ax, ay, XdotH, YdotH);
  594. float G = G_GGX_anisotropic_2cos(cNdotL, ax, ay, XdotH, YdotH) * G_GGX_anisotropic_2cos(cNdotV, ax, ay, XdotH, YdotH);
  595. #else
  596. float alpha_ggx = roughness * roughness;
  597. float D = D_GGX(cNdotH, alpha_ggx);
  598. float G = G_GGX_2cos(cNdotL, alpha_ggx) * G_GGX_2cos(cNdotV, alpha_ggx);
  599. #endif
  600. // F
  601. float cLdotH5 = SchlickFresnel(cLdotH);
  602. vec3 F = mix(vec3(cLdotH5), vec3(1.0), f0);
  603. vec3 specular_brdf_NL = cNdotL * D * F * G;
  604. specular_light += specular_brdf_NL * light_color * attenuation * specular_amount;
  605. #endif
  606. #if defined(LIGHT_CLEARCOAT_USED)
  607. #if !defined(SPECULAR_SCHLICK_GGX)
  608. float cLdotH5 = SchlickFresnel(cLdotH);
  609. #endif
  610. float Dr = GTR1(cNdotH, mix(.1, .001, clearcoat_gloss));
  611. float Fr = mix(.04, 1.0, cLdotH5);
  612. float Gr = G_GGX_2cos(cNdotL, .25) * G_GGX_2cos(cNdotV, .25);
  613. float clearcoat_specular_brdf_NL = 0.25 * clearcoat * Gr * Fr * Dr * cNdotL;
  614. specular_light += clearcoat_specular_brdf_NL * light_color * attenuation * specular_amount;
  615. #endif
  616. }
  617. #ifdef USE_SHADOW_TO_OPACITY
  618. alpha = min(alpha, clamp(1.0 - attenuation), 0.0, 1.0));
  619. #endif
  620. #endif //defined(LIGHT_CODE_USED)
  621. }
  622. #ifndef USE_NO_SHADOWS
  623. // Interleaved Gradient Noise
  624. // http://www.iryoku.com/next-generation-post-processing-in-call-of-duty-advanced-warfare
  625. float quick_hash(vec2 pos) {
  626. const vec3 magic = vec3(0.06711056f, 0.00583715f, 52.9829189f);
  627. return fract(magic.z * fract(dot(pos, magic.xy)));
  628. }
  629. float sample_directional_pcf_shadow(texture2D shadow, vec2 shadow_pixel_size, vec4 coord) {
  630. vec2 pos = coord.xy;
  631. float depth = coord.z;
  632. //if only one sample is taken, take it from the center
  633. if (scene_data.directional_soft_shadow_samples == 1) {
  634. return textureProj(sampler2DShadow(shadow, shadow_sampler), vec4(pos, depth, 1.0));
  635. }
  636. mat2 disk_rotation;
  637. {
  638. float r = quick_hash(gl_FragCoord.xy) * 2.0 * M_PI;
  639. float sr = sin(r);
  640. float cr = cos(r);
  641. disk_rotation = mat2(vec2(cr, -sr), vec2(sr, cr));
  642. }
  643. float avg = 0.0;
  644. for (uint i = 0; i < scene_data.directional_soft_shadow_samples; i++) {
  645. avg += textureProj(sampler2DShadow(shadow, shadow_sampler), vec4(pos + shadow_pixel_size * (disk_rotation * scene_data.directional_soft_shadow_kernel[i].xy), depth, 1.0));
  646. }
  647. return avg * (1.0 / float(scene_data.directional_soft_shadow_samples));
  648. }
  649. float sample_pcf_shadow(texture2D shadow, vec2 shadow_pixel_size, vec4 coord) {
  650. vec2 pos = coord.xy;
  651. float depth = coord.z;
  652. //if only one sample is taken, take it from the center
  653. if (scene_data.soft_shadow_samples == 1) {
  654. return textureProj(sampler2DShadow(shadow, shadow_sampler), vec4(pos, depth, 1.0));
  655. }
  656. mat2 disk_rotation;
  657. {
  658. float r = quick_hash(gl_FragCoord.xy) * 2.0 * M_PI;
  659. float sr = sin(r);
  660. float cr = cos(r);
  661. disk_rotation = mat2(vec2(cr, -sr), vec2(sr, cr));
  662. }
  663. float avg = 0.0;
  664. for (uint i = 0; i < scene_data.soft_shadow_samples; i++) {
  665. avg += textureProj(sampler2DShadow(shadow, shadow_sampler), vec4(pos + shadow_pixel_size * (disk_rotation * scene_data.soft_shadow_kernel[i].xy), depth, 1.0));
  666. }
  667. return avg * (1.0 / float(scene_data.soft_shadow_samples));
  668. }
  669. float sample_directional_soft_shadow(texture2D shadow, vec3 pssm_coord, vec2 tex_scale) {
  670. //find blocker
  671. float blocker_count = 0.0;
  672. float blocker_average = 0.0;
  673. mat2 disk_rotation;
  674. {
  675. float r = quick_hash(gl_FragCoord.xy) * 2.0 * M_PI;
  676. float sr = sin(r);
  677. float cr = cos(r);
  678. disk_rotation = mat2(vec2(cr, -sr), vec2(sr, cr));
  679. }
  680. for (uint i = 0; i < scene_data.directional_penumbra_shadow_samples; i++) {
  681. vec2 suv = pssm_coord.xy + (disk_rotation * scene_data.directional_penumbra_shadow_kernel[i].xy) * tex_scale;
  682. float d = textureLod(sampler2D(shadow, material_samplers[SAMPLER_LINEAR_CLAMP]), suv, 0.0).r;
  683. if (d < pssm_coord.z) {
  684. blocker_average += d;
  685. blocker_count += 1.0;
  686. }
  687. }
  688. if (blocker_count > 0.0) {
  689. //blockers found, do soft shadow
  690. blocker_average /= blocker_count;
  691. float penumbra = (pssm_coord.z - blocker_average) / blocker_average;
  692. tex_scale *= penumbra;
  693. float s = 0.0;
  694. for (uint i = 0; i < scene_data.directional_penumbra_shadow_samples; i++) {
  695. vec2 suv = pssm_coord.xy + (disk_rotation * scene_data.directional_penumbra_shadow_kernel[i].xy) * tex_scale;
  696. s += textureProj(sampler2DShadow(shadow, shadow_sampler), vec4(suv, pssm_coord.z, 1.0));
  697. }
  698. return s / float(scene_data.directional_penumbra_shadow_samples);
  699. } else {
  700. //no blockers found, so no shadow
  701. return 1.0;
  702. }
  703. }
  704. #endif //USE_NO_SHADOWS
  705. float get_omni_attenuation(float distance, float inv_range, float decay) {
  706. float nd = distance * inv_range;
  707. nd *= nd;
  708. nd *= nd; // nd^4
  709. nd = max(1.0 - nd, 0.0);
  710. nd *= nd; // nd^2
  711. return nd * pow(max(distance, 0.0001), -decay);
  712. }
  713. float light_process_omni_shadow(uint idx, vec3 vertex, vec3 normal) {
  714. #ifndef USE_NO_SHADOWS
  715. if (omni_lights.data[idx].shadow_enabled) {
  716. // there is a shadowmap
  717. vec3 light_rel_vec = omni_lights.data[idx].position - vertex;
  718. float light_length = length(light_rel_vec);
  719. vec4 v = vec4(vertex, 1.0);
  720. vec4 splane = (omni_lights.data[idx].shadow_matrix * v);
  721. float shadow_len = length(splane.xyz); //need to remember shadow len from here
  722. {
  723. vec3 nofs = normal_interp * omni_lights.data[idx].shadow_normal_bias / omni_lights.data[idx].inv_radius;
  724. nofs *= (1.0 - max(0.0, dot(normalize(light_rel_vec), normalize(normal_interp))));
  725. v.xyz += nofs;
  726. splane = (omni_lights.data[idx].shadow_matrix * v);
  727. }
  728. float shadow;
  729. #ifdef USE_SOFT_SHADOWS
  730. if (omni_lights.data[idx].soft_shadow_size > 0.0) {
  731. //soft shadow
  732. //find blocker
  733. float blocker_count = 0.0;
  734. float blocker_average = 0.0;
  735. mat2 disk_rotation;
  736. {
  737. float r = quick_hash(gl_FragCoord.xy) * 2.0 * M_PI;
  738. float sr = sin(r);
  739. float cr = cos(r);
  740. disk_rotation = mat2(vec2(cr, -sr), vec2(sr, cr));
  741. }
  742. vec3 normal = normalize(splane.xyz);
  743. vec3 v0 = abs(normal.z) < 0.999 ? vec3(0.0, 0.0, 1.0) : vec3(0.0, 1.0, 0.0);
  744. vec3 tangent = normalize(cross(v0, normal));
  745. vec3 bitangent = normalize(cross(tangent, normal));
  746. float z_norm = shadow_len * omni_lights.data[idx].inv_radius;
  747. tangent *= omni_lights.data[idx].soft_shadow_size * omni_lights.data[idx].soft_shadow_scale;
  748. bitangent *= omni_lights.data[idx].soft_shadow_size * omni_lights.data[idx].soft_shadow_scale;
  749. for (uint i = 0; i < scene_data.penumbra_shadow_samples; i++) {
  750. vec2 disk = disk_rotation * scene_data.penumbra_shadow_kernel[i].xy;
  751. vec3 pos = splane.xyz + tangent * disk.x + bitangent * disk.y;
  752. pos = normalize(pos);
  753. vec4 uv_rect = omni_lights.data[idx].atlas_rect;
  754. if (pos.z >= 0.0) {
  755. pos.z += 1.0;
  756. uv_rect.y += uv_rect.w;
  757. } else {
  758. pos.z = 1.0 - pos.z;
  759. }
  760. pos.xy /= pos.z;
  761. pos.xy = pos.xy * 0.5 + 0.5;
  762. pos.xy = uv_rect.xy + pos.xy * uv_rect.zw;
  763. float d = textureLod(sampler2D(shadow_atlas, material_samplers[SAMPLER_LINEAR_CLAMP]), pos.xy, 0.0).r;
  764. if (d < z_norm) {
  765. blocker_average += d;
  766. blocker_count += 1.0;
  767. }
  768. }
  769. if (blocker_count > 0.0) {
  770. //blockers found, do soft shadow
  771. blocker_average /= blocker_count;
  772. float penumbra = (z_norm - blocker_average) / blocker_average;
  773. tangent *= penumbra;
  774. bitangent *= penumbra;
  775. z_norm -= omni_lights.data[idx].inv_radius * omni_lights.data[idx].shadow_bias;
  776. shadow = 0.0;
  777. for (uint i = 0; i < scene_data.penumbra_shadow_samples; i++) {
  778. vec2 disk = disk_rotation * scene_data.penumbra_shadow_kernel[i].xy;
  779. vec3 pos = splane.xyz + tangent * disk.x + bitangent * disk.y;
  780. pos = normalize(pos);
  781. vec4 uv_rect = omni_lights.data[idx].atlas_rect;
  782. if (pos.z >= 0.0) {
  783. pos.z += 1.0;
  784. uv_rect.y += uv_rect.w;
  785. } else {
  786. pos.z = 1.0 - pos.z;
  787. }
  788. pos.xy /= pos.z;
  789. pos.xy = pos.xy * 0.5 + 0.5;
  790. pos.xy = uv_rect.xy + pos.xy * uv_rect.zw;
  791. shadow += textureProj(sampler2DShadow(shadow_atlas, shadow_sampler), vec4(pos.xy, z_norm, 1.0));
  792. }
  793. shadow /= float(scene_data.penumbra_shadow_samples);
  794. } else {
  795. //no blockers found, so no shadow
  796. shadow = 1.0;
  797. }
  798. } else {
  799. #endif
  800. splane.xyz = normalize(splane.xyz);
  801. vec4 clamp_rect = omni_lights.data[idx].atlas_rect;
  802. if (splane.z >= 0.0) {
  803. splane.z += 1.0;
  804. clamp_rect.y += clamp_rect.w;
  805. } else {
  806. splane.z = 1.0 - splane.z;
  807. }
  808. splane.xy /= splane.z;
  809. splane.xy = splane.xy * 0.5 + 0.5;
  810. splane.z = (shadow_len - omni_lights.data[idx].shadow_bias) * omni_lights.data[idx].inv_radius;
  811. splane.xy = clamp_rect.xy + splane.xy * clamp_rect.zw;
  812. splane.w = 1.0; //needed? i think it should be 1 already
  813. shadow = sample_pcf_shadow(shadow_atlas, omni_lights.data[idx].soft_shadow_scale * scene_data.shadow_atlas_pixel_size, splane);
  814. #ifdef USE_SOFT_SHADOWS
  815. }
  816. #endif
  817. return shadow;
  818. }
  819. #endif
  820. return 1.0;
  821. }
  822. void light_process_omni(uint idx, vec3 vertex, vec3 eye_vec, vec3 normal, vec3 vertex_ddx, vec3 vertex_ddy, vec3 f0, uint orms, float shadow,
  823. #ifdef LIGHT_BACKLIGHT_USED
  824. vec3 backlight,
  825. #endif
  826. #ifdef LIGHT_TRANSMITTANCE_USED
  827. vec4 transmittance_color,
  828. float transmittance_depth,
  829. float transmittance_curve,
  830. float transmittance_boost,
  831. #endif
  832. #ifdef LIGHT_RIM_USED
  833. float rim, float rim_tint, vec3 rim_color,
  834. #endif
  835. #ifdef LIGHT_CLEARCOAT_USED
  836. float clearcoat, float clearcoat_gloss,
  837. #endif
  838. #ifdef LIGHT_ANISOTROPY_USED
  839. vec3 binormal, vec3 tangent, float anisotropy,
  840. #endif
  841. #ifdef USE_SHADOW_TO_OPACITY
  842. inout float alpha,
  843. #endif
  844. inout vec3 diffuse_light, inout vec3 specular_light) {
  845. vec3 light_rel_vec = omni_lights.data[idx].position - vertex;
  846. float light_length = length(light_rel_vec);
  847. float omni_attenuation = get_omni_attenuation(light_length, omni_lights.data[idx].inv_radius, omni_lights.data[idx].attenuation);
  848. float light_attenuation = omni_attenuation;
  849. vec3 color = omni_lights.data[idx].color;
  850. #ifdef USE_SOFT_SHADOWS
  851. float size_A = 0.0;
  852. if (omni_lights.data[idx].size > 0.0) {
  853. float t = omni_lights.data[idx].size / max(0.001, light_length);
  854. size_A = max(0.0, 1.0 - 1 / sqrt(1 + t * t));
  855. }
  856. #endif
  857. #ifdef LIGHT_TRANSMITTANCE_USED
  858. float transmittance_z = transmittance_depth; //no transmittance by default
  859. transmittance_color.a *= light_attenuation;
  860. {
  861. vec4 clamp_rect = omni_lights.data[idx].atlas_rect;
  862. //redo shadowmapping, but shrink the model a bit to avoid arctifacts
  863. vec4 splane = (omni_lights.data[idx].shadow_matrix * vec4(vertex - normalize(normal_interp) * omni_lights.data[idx].transmittance_bias, 1.0));
  864. shadow_len = length(splane.xyz);
  865. splane = normalize(splane.xyz);
  866. if (splane.z >= 0.0) {
  867. splane.z += 1.0;
  868. } else {
  869. splane.z = 1.0 - splane.z;
  870. }
  871. splane.xy /= splane.z;
  872. splane.xy = splane.xy * 0.5 + 0.5;
  873. splane.z = shadow_len * omni_lights.data[idx].inv_radius;
  874. splane.xy = clamp_rect.xy + splane.xy * clamp_rect.zw;
  875. splane.w = 1.0; //needed? i think it should be 1 already
  876. float shadow_z = textureLod(sampler2D(shadow_atlas, material_samplers[SAMPLER_LINEAR_CLAMP]), splane.xy, 0.0).r;
  877. transmittance_z = (splane.z - shadow_z) / omni_lights.data[idx].inv_radius;
  878. }
  879. #endif
  880. #if 0
  881. if (omni_lights.data[idx].projector_rect != vec4(0.0)) {
  882. vec3 local_v = (omni_lights.data[idx].shadow_matrix * vec4(vertex, 1.0)).xyz;
  883. local_v = normalize(local_v);
  884. vec4 atlas_rect = omni_lights.data[idx].projector_rect;
  885. if (local_v.z >= 0.0) {
  886. local_v.z += 1.0;
  887. atlas_rect.y += atlas_rect.w;
  888. } else {
  889. local_v.z = 1.0 - local_v.z;
  890. }
  891. local_v.xy /= local_v.z;
  892. local_v.xy = local_v.xy * 0.5 + 0.5;
  893. vec2 proj_uv = local_v.xy * atlas_rect.zw;
  894. vec2 proj_uv_ddx;
  895. vec2 proj_uv_ddy;
  896. {
  897. vec3 local_v_ddx = (omni_lights.data[idx].shadow_matrix * vec4(vertex + vertex_ddx, 1.0)).xyz;
  898. local_v_ddx = normalize(local_v_ddx);
  899. if (local_v_ddx.z >= 0.0) {
  900. local_v_ddx.z += 1.0;
  901. } else {
  902. local_v_ddx.z = 1.0 - local_v_ddx.z;
  903. }
  904. local_v_ddx.xy /= local_v_ddx.z;
  905. local_v_ddx.xy = local_v_ddx.xy * 0.5 + 0.5;
  906. proj_uv_ddx = local_v_ddx.xy * atlas_rect.zw - proj_uv;
  907. vec3 local_v_ddy = (omni_lights.data[idx].shadow_matrix * vec4(vertex + vertex_ddy, 1.0)).xyz;
  908. local_v_ddy = normalize(local_v_ddy);
  909. if (local_v_ddy.z >= 0.0) {
  910. local_v_ddy.z += 1.0;
  911. } else {
  912. local_v_ddy.z = 1.0 - local_v_ddy.z;
  913. }
  914. local_v_ddy.xy /= local_v_ddy.z;
  915. local_v_ddy.xy = local_v_ddy.xy * 0.5 + 0.5;
  916. proj_uv_ddy = local_v_ddy.xy * atlas_rect.zw - proj_uv;
  917. }
  918. vec4 proj = textureGrad(sampler2D(decal_atlas_srgb, material_samplers[SAMPLER_LINEAR_WITH_MIPMAPS_CLAMP]), proj_uv + atlas_rect.xy, proj_uv_ddx, proj_uv_ddy);
  919. no_shadow = mix(no_shadow, proj.rgb, proj.a);
  920. }
  921. #endif
  922. light_attenuation *= shadow;
  923. light_compute(normal, normalize(light_rel_vec), eye_vec, color, light_attenuation, f0, orms, omni_lights.data[idx].specular_amount,
  924. #ifdef LIGHT_BACKLIGHT_USED
  925. backlight,
  926. #endif
  927. #ifdef LIGHT_TRANSMITTANCE_USED
  928. transmittance_color,
  929. transmittance_depth,
  930. transmittance_curve,
  931. transmittance_boost,
  932. transmittance_z,
  933. #endif
  934. #ifdef LIGHT_RIM_USED
  935. rim * omni_attenuation, rim_tint, rim_color,
  936. #endif
  937. #ifdef LIGHT_CLEARCOAT_USED
  938. clearcoat, clearcoat_gloss,
  939. #endif
  940. #ifdef LIGHT_ANISOTROPY_USED
  941. binormal, tangent, anisotropy,
  942. #endif
  943. #ifdef USE_SOFT_SHADOWS
  944. size_A,
  945. #endif
  946. #ifdef USE_SHADOW_TO_OPACITY
  947. alpha,
  948. #endif
  949. diffuse_light,
  950. specular_light);
  951. }
  952. float light_process_spot_shadow(uint idx, vec3 vertex, vec3 normal) {
  953. #ifndef USE_NO_SHADOWS
  954. if (spot_lights.data[idx].shadow_enabled) {
  955. vec3 light_rel_vec = spot_lights.data[idx].position - vertex;
  956. float light_length = length(light_rel_vec);
  957. vec3 spot_dir = spot_lights.data[idx].direction;
  958. //there is a shadowmap
  959. vec4 v = vec4(vertex, 1.0);
  960. v.xyz -= spot_dir * spot_lights.data[idx].shadow_bias;
  961. float z_norm = dot(spot_dir, -light_rel_vec) * spot_lights.data[idx].inv_radius;
  962. float depth_bias_scale = 1.0 / (max(0.0001, z_norm)); //the closer to the light origin, the more you have to offset to reach 1px in the map
  963. vec3 normal_bias = normalize(normal_interp) * (1.0 - max(0.0, dot(spot_dir, -normalize(normal_interp)))) * spot_lights.data[idx].shadow_normal_bias * depth_bias_scale;
  964. normal_bias -= spot_dir * dot(spot_dir, normal_bias); //only XY, no Z
  965. v.xyz += normal_bias;
  966. //adjust with bias
  967. z_norm = dot(spot_dir, v.xyz - spot_lights.data[idx].position) * spot_lights.data[idx].inv_radius;
  968. float shadow;
  969. vec4 splane = (spot_lights.data[idx].shadow_matrix * v);
  970. splane /= splane.w;
  971. #ifdef USE_SOFT_SHADOWS
  972. if (spot_lights.data[idx].soft_shadow_size > 0.0) {
  973. //soft shadow
  974. //find blocker
  975. vec2 shadow_uv = splane.xy * spot_lights.data[idx].atlas_rect.zw + spot_lights.data[idx].atlas_rect.xy;
  976. float blocker_count = 0.0;
  977. float blocker_average = 0.0;
  978. mat2 disk_rotation;
  979. {
  980. float r = quick_hash(gl_FragCoord.xy) * 2.0 * M_PI;
  981. float sr = sin(r);
  982. float cr = cos(r);
  983. disk_rotation = mat2(vec2(cr, -sr), vec2(sr, cr));
  984. }
  985. float uv_size = spot_lights.data[idx].soft_shadow_size * z_norm * spot_lights.data[idx].soft_shadow_scale;
  986. vec2 clamp_max = spot_lights.data[idx].atlas_rect.xy + spot_lights.data[idx].atlas_rect.zw;
  987. for (uint i = 0; i < scene_data.penumbra_shadow_samples; i++) {
  988. vec2 suv = shadow_uv + (disk_rotation * scene_data.penumbra_shadow_kernel[i].xy) * uv_size;
  989. suv = clamp(suv, spot_lights.data[idx].atlas_rect.xy, clamp_max);
  990. float d = textureLod(sampler2D(shadow_atlas, material_samplers[SAMPLER_LINEAR_CLAMP]), suv, 0.0).r;
  991. if (d < z_norm) {
  992. blocker_average += d;
  993. blocker_count += 1.0;
  994. }
  995. }
  996. if (blocker_count > 0.0) {
  997. //blockers found, do soft shadow
  998. blocker_average /= blocker_count;
  999. float penumbra = (z_norm - blocker_average) / blocker_average;
  1000. uv_size *= penumbra;
  1001. shadow = 0.0;
  1002. for (uint i = 0; i < scene_data.penumbra_shadow_samples; i++) {
  1003. vec2 suv = shadow_uv + (disk_rotation * scene_data.penumbra_shadow_kernel[i].xy) * uv_size;
  1004. suv = clamp(suv, spot_lights.data[idx].atlas_rect.xy, clamp_max);
  1005. shadow += textureProj(sampler2DShadow(shadow_atlas, shadow_sampler), vec4(suv, z_norm, 1.0));
  1006. }
  1007. shadow /= float(scene_data.penumbra_shadow_samples);
  1008. } else {
  1009. //no blockers found, so no shadow
  1010. shadow = 1.0;
  1011. }
  1012. } else {
  1013. #endif
  1014. //hard shadow
  1015. vec4 shadow_uv = vec4(splane.xy * spot_lights.data[idx].atlas_rect.zw + spot_lights.data[idx].atlas_rect.xy, splane.z, 1.0);
  1016. shadow = sample_pcf_shadow(shadow_atlas, spot_lights.data[idx].soft_shadow_scale * scene_data.shadow_atlas_pixel_size, shadow_uv);
  1017. #ifdef USE_SOFT_SHADOWS
  1018. }
  1019. #endif
  1020. return shadow;
  1021. }
  1022. #endif //USE_NO_SHADOWS
  1023. return 1.0;
  1024. }
  1025. void light_process_spot(uint idx, vec3 vertex, vec3 eye_vec, vec3 normal, vec3 vertex_ddx, vec3 vertex_ddy, vec3 f0, uint orms, float shadow,
  1026. #ifdef LIGHT_BACKLIGHT_USED
  1027. vec3 backlight,
  1028. #endif
  1029. #ifdef LIGHT_TRANSMITTANCE_USED
  1030. vec4 transmittance_color,
  1031. float transmittance_depth,
  1032. float transmittance_curve,
  1033. float transmittance_boost,
  1034. #endif
  1035. #ifdef LIGHT_RIM_USED
  1036. float rim, float rim_tint, vec3 rim_color,
  1037. #endif
  1038. #ifdef LIGHT_CLEARCOAT_USED
  1039. float clearcoat, float clearcoat_gloss,
  1040. #endif
  1041. #ifdef LIGHT_ANISOTROPY_USED
  1042. vec3 binormal, vec3 tangent, float anisotropy,
  1043. #endif
  1044. #ifdef USE_SHADOW_TO_OPACITY
  1045. inout float alpha,
  1046. #endif
  1047. inout vec3 diffuse_light,
  1048. inout vec3 specular_light) {
  1049. vec3 light_rel_vec = spot_lights.data[idx].position - vertex;
  1050. float light_length = length(light_rel_vec);
  1051. float spot_attenuation = get_omni_attenuation(light_length, spot_lights.data[idx].inv_radius, spot_lights.data[idx].attenuation);
  1052. vec3 spot_dir = spot_lights.data[idx].direction;
  1053. float scos = max(dot(-normalize(light_rel_vec), spot_dir), spot_lights.data[idx].cone_angle);
  1054. float spot_rim = max(0.0001, (1.0 - scos) / (1.0 - spot_lights.data[idx].cone_angle));
  1055. spot_attenuation *= 1.0 - pow(spot_rim, spot_lights.data[idx].cone_attenuation);
  1056. float light_attenuation = spot_attenuation;
  1057. vec3 color = spot_lights.data[idx].color;
  1058. float specular_amount = spot_lights.data[idx].specular_amount;
  1059. #ifdef USE_SOFT_SHADOWS
  1060. float size_A = 0.0;
  1061. if (spot_lights.data[idx].size > 0.0) {
  1062. float t = spot_lights.data[idx].size / max(0.001, light_length);
  1063. size_A = max(0.0, 1.0 - 1 / sqrt(1 + t * t));
  1064. }
  1065. #endif
  1066. /*
  1067. if (spot_lights.data[idx].atlas_rect!=vec4(0.0)) {
  1068. //use projector texture
  1069. }
  1070. */
  1071. #ifdef LIGHT_TRANSMITTANCE_USED
  1072. float transmittance_z = transmittance_depth;
  1073. transmittance_color.a *= light_attenuation;
  1074. {
  1075. splane = (spot_lights.data[idx].shadow_matrix * vec4(vertex - normalize(normal_interp) * spot_lights.data[idx].transmittance_bias, 1.0));
  1076. splane /= splane.w;
  1077. splane.xy = splane.xy * spot_lights.data[idx].atlas_rect.zw + spot_lights.data[idx].atlas_rect.xy;
  1078. float shadow_z = textureLod(sampler2D(shadow_atlas, material_samplers[SAMPLER_LINEAR_CLAMP]), splane.xy, 0.0).r;
  1079. //reconstruct depth
  1080. shadow_z /= spot_lights.data[idx].inv_radius;
  1081. //distance to light plane
  1082. float z = dot(spot_dir, -light_rel_vec);
  1083. transmittance_z = z - shadow_z;
  1084. }
  1085. #endif //LIGHT_TRANSMITTANCE_USED
  1086. light_attenuation *= shadow;
  1087. light_compute(normal, normalize(light_rel_vec), eye_vec, color, light_attenuation, f0, orms, spot_lights.data[idx].specular_amount,
  1088. #ifdef LIGHT_BACKLIGHT_USED
  1089. backlight,
  1090. #endif
  1091. #ifdef LIGHT_TRANSMITTANCE_USED
  1092. transmittance_color,
  1093. transmittance_depth,
  1094. transmittance_curve,
  1095. transmittance_boost,
  1096. transmittance_z,
  1097. #endif
  1098. #ifdef LIGHT_RIM_USED
  1099. rim * spot_attenuation, rim_tint, rim_color,
  1100. #endif
  1101. #ifdef LIGHT_CLEARCOAT_USED
  1102. clearcoat, clearcoat_gloss,
  1103. #endif
  1104. #ifdef LIGHT_ANISOTROPY_USED
  1105. binormal, tangent, anisotropy,
  1106. #endif
  1107. #ifdef USE_SOFT_SHADOW
  1108. size_A,
  1109. #endif
  1110. #ifdef USE_SHADOW_TO_OPACITY
  1111. alpha,
  1112. #endif
  1113. diffuse_light, specular_light);
  1114. }
  1115. void reflection_process(uint ref_index, vec3 vertex, vec3 normal, float roughness, vec3 ambient_light, vec3 specular_light, inout vec4 ambient_accum, inout vec4 reflection_accum) {
  1116. vec3 box_extents = reflections.data[ref_index].box_extents;
  1117. vec3 local_pos = (reflections.data[ref_index].local_matrix * vec4(vertex, 1.0)).xyz;
  1118. if (any(greaterThan(abs(local_pos), box_extents))) { //out of the reflection box
  1119. return;
  1120. }
  1121. vec3 ref_vec = normalize(reflect(vertex, normal));
  1122. vec3 inner_pos = abs(local_pos / box_extents);
  1123. float blend = max(inner_pos.x, max(inner_pos.y, inner_pos.z));
  1124. //make blend more rounded
  1125. blend = mix(length(inner_pos), blend, blend);
  1126. blend *= blend;
  1127. blend = max(0.0, 1.0 - blend);
  1128. if (reflections.data[ref_index].intensity > 0.0) { // compute reflection
  1129. vec3 local_ref_vec = (reflections.data[ref_index].local_matrix * vec4(ref_vec, 0.0)).xyz;
  1130. if (reflections.data[ref_index].box_project) { //box project
  1131. vec3 nrdir = normalize(local_ref_vec);
  1132. vec3 rbmax = (box_extents - local_pos) / nrdir;
  1133. vec3 rbmin = (-box_extents - local_pos) / nrdir;
  1134. vec3 rbminmax = mix(rbmin, rbmax, greaterThan(nrdir, vec3(0.0, 0.0, 0.0)));
  1135. float fa = min(min(rbminmax.x, rbminmax.y), rbminmax.z);
  1136. vec3 posonbox = local_pos + nrdir * fa;
  1137. local_ref_vec = posonbox - reflections.data[ref_index].box_offset;
  1138. }
  1139. vec4 reflection;
  1140. reflection.rgb = textureLod(samplerCubeArray(reflection_atlas, material_samplers[SAMPLER_LINEAR_WITH_MIPMAPS_CLAMP]), vec4(local_ref_vec, reflections.data[ref_index].index), roughness * MAX_ROUGHNESS_LOD).rgb;
  1141. if (reflections.data[ref_index].exterior) {
  1142. reflection.rgb = mix(specular_light, reflection.rgb, blend);
  1143. }
  1144. reflection.rgb *= reflections.data[ref_index].intensity; //intensity
  1145. reflection.a = blend;
  1146. reflection.rgb *= reflection.a;
  1147. reflection_accum += reflection;
  1148. }
  1149. switch (reflections.data[ref_index].ambient_mode) {
  1150. case REFLECTION_AMBIENT_DISABLED: {
  1151. //do nothing
  1152. } break;
  1153. case REFLECTION_AMBIENT_ENVIRONMENT: {
  1154. //do nothing
  1155. vec3 local_amb_vec = (reflections.data[ref_index].local_matrix * vec4(normal, 0.0)).xyz;
  1156. vec4 ambient_out;
  1157. ambient_out.rgb = textureLod(samplerCubeArray(reflection_atlas, material_samplers[SAMPLER_LINEAR_WITH_MIPMAPS_CLAMP]), vec4(local_amb_vec, reflections.data[ref_index].index), MAX_ROUGHNESS_LOD).rgb;
  1158. ambient_out.a = blend;
  1159. if (reflections.data[ref_index].exterior) {
  1160. ambient_out.rgb = mix(ambient_light, ambient_out.rgb, blend);
  1161. }
  1162. ambient_out.rgb *= ambient_out.a;
  1163. ambient_accum += ambient_out;
  1164. } break;
  1165. case REFLECTION_AMBIENT_COLOR: {
  1166. vec4 ambient_out;
  1167. ambient_out.a = blend;
  1168. ambient_out.rgb = reflections.data[ref_index].ambient;
  1169. if (reflections.data[ref_index].exterior) {
  1170. ambient_out.rgb = mix(ambient_light, ambient_out.rgb, blend);
  1171. }
  1172. ambient_out.rgb *= ambient_out.a;
  1173. ambient_accum += ambient_out;
  1174. } break;
  1175. }
  1176. }
  1177. #ifdef USE_FORWARD_GI
  1178. //standard voxel cone trace
  1179. vec4 voxel_cone_trace(texture3D probe, vec3 cell_size, vec3 pos, vec3 direction, float tan_half_angle, float max_distance, float p_bias) {
  1180. float dist = p_bias;
  1181. vec4 color = vec4(0.0);
  1182. while (dist < max_distance && color.a < 0.95) {
  1183. float diameter = max(1.0, 2.0 * tan_half_angle * dist);
  1184. vec3 uvw_pos = (pos + dist * direction) * cell_size;
  1185. float half_diameter = diameter * 0.5;
  1186. //check if outside, then break
  1187. if (any(greaterThan(abs(uvw_pos - 0.5), vec3(0.5f + half_diameter * cell_size)))) {
  1188. break;
  1189. }
  1190. vec4 scolor = textureLod(sampler3D(probe, material_samplers[SAMPLER_LINEAR_WITH_MIPMAPS_CLAMP]), uvw_pos, log2(diameter));
  1191. float a = (1.0 - color.a);
  1192. color += a * scolor;
  1193. dist += half_diameter;
  1194. }
  1195. return color;
  1196. }
  1197. vec4 voxel_cone_trace_45_degrees(texture3D probe, vec3 cell_size, vec3 pos, vec3 direction, float tan_half_angle, float max_distance, float p_bias) {
  1198. float dist = p_bias;
  1199. vec4 color = vec4(0.0);
  1200. float radius = max(0.5, tan_half_angle * dist);
  1201. float lod_level = log2(radius * 2.0);
  1202. while (dist < max_distance && color.a < 0.95) {
  1203. vec3 uvw_pos = (pos + dist * direction) * cell_size;
  1204. //check if outside, then break
  1205. if (any(greaterThan(abs(uvw_pos - 0.5), vec3(0.5f + radius * cell_size)))) {
  1206. break;
  1207. }
  1208. vec4 scolor = textureLod(sampler3D(probe, material_samplers[SAMPLER_LINEAR_WITH_MIPMAPS_CLAMP]), uvw_pos, lod_level);
  1209. lod_level += 1.0;
  1210. float a = (1.0 - color.a);
  1211. scolor *= a;
  1212. color += scolor;
  1213. dist += radius;
  1214. radius = max(0.5, tan_half_angle * dist);
  1215. }
  1216. return color;
  1217. }
  1218. void gi_probe_compute(uint index, vec3 position, vec3 normal, vec3 ref_vec, mat3 normal_xform, float roughness, vec3 ambient, vec3 environment, inout vec4 out_spec, inout vec4 out_diff) {
  1219. position = (gi_probes.data[index].xform * vec4(position, 1.0)).xyz;
  1220. ref_vec = normalize((gi_probes.data[index].xform * vec4(ref_vec, 0.0)).xyz);
  1221. normal = normalize((gi_probes.data[index].xform * vec4(normal, 0.0)).xyz);
  1222. position += normal * gi_probes.data[index].normal_bias;
  1223. //this causes corrupted pixels, i have no idea why..
  1224. if (any(bvec2(any(lessThan(position, vec3(0.0))), any(greaterThan(position, gi_probes.data[index].bounds))))) {
  1225. return;
  1226. }
  1227. vec3 blendv = abs(position / gi_probes.data[index].bounds * 2.0 - 1.0);
  1228. float blend = clamp(1.0 - max(blendv.x, max(blendv.y, blendv.z)), 0.0, 1.0);
  1229. //float blend=1.0;
  1230. float max_distance = length(gi_probes.data[index].bounds);
  1231. vec3 cell_size = 1.0 / gi_probes.data[index].bounds;
  1232. //radiance
  1233. #define MAX_CONE_DIRS 4
  1234. vec3 cone_dirs[MAX_CONE_DIRS] = vec3[](
  1235. vec3(0.707107, 0.0, 0.707107),
  1236. vec3(0.0, 0.707107, 0.707107),
  1237. vec3(-0.707107, 0.0, 0.707107),
  1238. vec3(0.0, -0.707107, 0.707107));
  1239. float cone_weights[MAX_CONE_DIRS] = float[](0.25, 0.25, 0.25, 0.25);
  1240. float cone_angle_tan = 0.98269;
  1241. vec3 light = vec3(0.0);
  1242. for (int i = 0; i < MAX_CONE_DIRS; i++) {
  1243. vec3 dir = normalize((gi_probes.data[index].xform * vec4(normal_xform * cone_dirs[i], 0.0)).xyz);
  1244. vec4 cone_light = voxel_cone_trace_45_degrees(gi_probe_textures[index], cell_size, position, dir, cone_angle_tan, max_distance, gi_probes.data[index].bias);
  1245. if (gi_probes.data[index].blend_ambient) {
  1246. cone_light.rgb = mix(ambient, cone_light.rgb, min(1.0, cone_light.a / 0.95));
  1247. }
  1248. light += cone_weights[i] * cone_light.rgb;
  1249. }
  1250. light *= gi_probes.data[index].dynamic_range;
  1251. out_diff += vec4(light * blend, blend);
  1252. //irradiance
  1253. vec4 irr_light = voxel_cone_trace(gi_probe_textures[index], cell_size, position, ref_vec, tan(roughness * 0.5 * M_PI * 0.99), max_distance, gi_probes.data[index].bias);
  1254. if (gi_probes.data[index].blend_ambient) {
  1255. irr_light.rgb = mix(environment, irr_light.rgb, min(1.0, irr_light.a / 0.95));
  1256. }
  1257. irr_light.rgb *= gi_probes.data[index].dynamic_range;
  1258. //irr_light=vec3(0.0);
  1259. out_spec += vec4(irr_light.rgb * blend, blend);
  1260. }
  1261. vec2 octahedron_wrap(vec2 v) {
  1262. vec2 signVal;
  1263. signVal.x = v.x >= 0.0 ? 1.0 : -1.0;
  1264. signVal.y = v.y >= 0.0 ? 1.0 : -1.0;
  1265. return (1.0 - abs(v.yx)) * signVal;
  1266. }
  1267. vec2 octahedron_encode(vec3 n) {
  1268. // https://twitter.com/Stubbesaurus/status/937994790553227264
  1269. n /= (abs(n.x) + abs(n.y) + abs(n.z));
  1270. n.xy = n.z >= 0.0 ? n.xy : octahedron_wrap(n.xy);
  1271. n.xy = n.xy * 0.5 + 0.5;
  1272. return n.xy;
  1273. }
  1274. void sdfgi_process(uint cascade, vec3 cascade_pos, vec3 cam_pos, vec3 cam_normal, vec3 cam_specular_normal, bool use_specular, float roughness, out vec3 diffuse_light, out vec3 specular_light, out float blend) {
  1275. cascade_pos += cam_normal * sdfgi.normal_bias;
  1276. vec3 base_pos = floor(cascade_pos);
  1277. //cascade_pos += mix(vec3(0.0),vec3(0.01),lessThan(abs(cascade_pos-base_pos),vec3(0.01))) * cam_normal;
  1278. ivec3 probe_base_pos = ivec3(base_pos);
  1279. vec4 diffuse_accum = vec4(0.0);
  1280. vec3 specular_accum;
  1281. ivec3 tex_pos = ivec3(probe_base_pos.xy, int(cascade));
  1282. tex_pos.x += probe_base_pos.z * sdfgi.probe_axis_size;
  1283. tex_pos.xy = tex_pos.xy * (SDFGI_OCT_SIZE + 2) + ivec2(1);
  1284. vec3 diffuse_posf = (vec3(tex_pos) + vec3(octahedron_encode(cam_normal) * float(SDFGI_OCT_SIZE), 0.0)) * sdfgi.lightprobe_tex_pixel_size;
  1285. vec3 specular_posf;
  1286. if (use_specular) {
  1287. specular_accum = vec3(0.0);
  1288. specular_posf = (vec3(tex_pos) + vec3(octahedron_encode(cam_specular_normal) * float(SDFGI_OCT_SIZE), 0.0)) * sdfgi.lightprobe_tex_pixel_size;
  1289. }
  1290. vec4 light_accum = vec4(0.0);
  1291. float weight_accum = 0.0;
  1292. for (uint j = 0; j < 8; j++) {
  1293. ivec3 offset = (ivec3(j) >> ivec3(0, 1, 2)) & ivec3(1, 1, 1);
  1294. ivec3 probe_posi = probe_base_pos;
  1295. probe_posi += offset;
  1296. // Compute weight
  1297. vec3 probe_pos = vec3(probe_posi);
  1298. vec3 probe_to_pos = cascade_pos - probe_pos;
  1299. vec3 probe_dir = normalize(-probe_to_pos);
  1300. vec3 trilinear = vec3(1.0) - abs(probe_to_pos);
  1301. float weight = trilinear.x * trilinear.y * trilinear.z * max(0.005, dot(cam_normal, probe_dir));
  1302. // Compute lightprobe occlusion
  1303. if (sdfgi.use_occlusion) {
  1304. ivec3 occ_indexv = abs((sdfgi.cascades[cascade].probe_world_offset + probe_posi) & ivec3(1, 1, 1)) * ivec3(1, 2, 4);
  1305. vec4 occ_mask = mix(vec4(0.0), vec4(1.0), equal(ivec4(occ_indexv.x | occ_indexv.y), ivec4(0, 1, 2, 3)));
  1306. vec3 occ_pos = clamp(cascade_pos, probe_pos - sdfgi.occlusion_clamp, probe_pos + sdfgi.occlusion_clamp) * sdfgi.probe_to_uvw;
  1307. occ_pos.z += float(cascade);
  1308. if (occ_indexv.z != 0) { //z bit is on, means index is >=4, so make it switch to the other half of textures
  1309. occ_pos.x += 1.0;
  1310. }
  1311. occ_pos *= sdfgi.occlusion_renormalize;
  1312. float occlusion = dot(textureLod(sampler3D(sdfgi_occlusion_cascades, material_samplers[SAMPLER_LINEAR_CLAMP]), occ_pos, 0.0), occ_mask);
  1313. weight *= max(occlusion, 0.01);
  1314. }
  1315. // Compute lightprobe texture position
  1316. vec3 diffuse;
  1317. vec3 pos_uvw = diffuse_posf;
  1318. pos_uvw.xy += vec2(offset.xy) * sdfgi.lightprobe_uv_offset.xy;
  1319. pos_uvw.x += float(offset.z) * sdfgi.lightprobe_uv_offset.z;
  1320. diffuse = textureLod(sampler2DArray(sdfgi_lightprobe_texture, material_samplers[SAMPLER_LINEAR_CLAMP]), pos_uvw, 0.0).rgb;
  1321. diffuse_accum += vec4(diffuse * weight, weight);
  1322. if (use_specular) {
  1323. vec3 specular = vec3(0.0);
  1324. vec3 pos_uvw = specular_posf;
  1325. pos_uvw.xy += vec2(offset.xy) * sdfgi.lightprobe_uv_offset.xy;
  1326. pos_uvw.x += float(offset.z) * sdfgi.lightprobe_uv_offset.z;
  1327. if (roughness < 0.99) {
  1328. specular = textureLod(sampler2DArray(sdfgi_lightprobe_texture, material_samplers[SAMPLER_LINEAR_CLAMP]), pos_uvw + vec3(0, 0, float(sdfgi.max_cascades)), 0.0).rgb;
  1329. }
  1330. if (roughness > 0.5) {
  1331. specular = mix(specular, textureLod(sampler2DArray(sdfgi_lightprobe_texture, material_samplers[SAMPLER_LINEAR_CLAMP]), pos_uvw, 0.0).rgb, (roughness - 0.5) * 2.0);
  1332. }
  1333. specular_accum += specular * weight;
  1334. }
  1335. }
  1336. if (diffuse_accum.a > 0.0) {
  1337. diffuse_accum.rgb /= diffuse_accum.a;
  1338. }
  1339. diffuse_light = diffuse_accum.rgb;
  1340. if (use_specular) {
  1341. if (diffuse_accum.a > 0.0) {
  1342. specular_accum /= diffuse_accum.a;
  1343. }
  1344. specular_light = specular_accum;
  1345. }
  1346. {
  1347. //process blend
  1348. float blend_from = (float(sdfgi.probe_axis_size - 1) / 2.0) - 2.5;
  1349. float blend_to = blend_from + 2.0;
  1350. vec3 inner_pos = cam_pos * sdfgi.cascades[cascade].to_probe;
  1351. float len = length(inner_pos);
  1352. inner_pos = abs(normalize(inner_pos));
  1353. len *= max(inner_pos.x, max(inner_pos.y, inner_pos.z));
  1354. if (len >= blend_from) {
  1355. blend = smoothstep(blend_from, blend_to, len);
  1356. } else {
  1357. blend = 0.0;
  1358. }
  1359. }
  1360. }
  1361. #endif //USE_FORWARD_GI
  1362. #endif //!defined(MODE_RENDER_DEPTH) && !defined(MODE_UNSHADED)
  1363. #ifndef MODE_RENDER_DEPTH
  1364. vec4 volumetric_fog_process(vec2 screen_uv, float z) {
  1365. vec3 fog_pos = vec3(screen_uv, z * scene_data.volumetric_fog_inv_length);
  1366. if (fog_pos.z < 0.0) {
  1367. return vec4(0.0);
  1368. } else if (fog_pos.z < 1.0) {
  1369. fog_pos.z = pow(fog_pos.z, scene_data.volumetric_fog_detail_spread);
  1370. }
  1371. return texture(sampler3D(volumetric_fog_texture, material_samplers[SAMPLER_LINEAR_CLAMP]), fog_pos);
  1372. }
  1373. vec4 fog_process(vec3 vertex) {
  1374. vec3 fog_color = scene_data.fog_light_color;
  1375. if (scene_data.fog_aerial_perspective > 0.0) {
  1376. vec3 sky_fog_color = vec3(0.0);
  1377. vec3 cube_view = scene_data.radiance_inverse_xform * vertex;
  1378. // mip_level always reads from the second mipmap and higher so the fog is always slightly blurred
  1379. float mip_level = mix(1.0 / MAX_ROUGHNESS_LOD, 1.0, 1.0 - (abs(vertex.z) - scene_data.z_near) / (scene_data.z_far - scene_data.z_near));
  1380. #ifdef USE_RADIANCE_CUBEMAP_ARRAY
  1381. float lod, blend;
  1382. blend = modf(mip_level * MAX_ROUGHNESS_LOD, lod);
  1383. sky_fog_color = texture(samplerCubeArray(radiance_cubemap, material_samplers[SAMPLER_LINEAR_WITH_MIPMAPS_CLAMP]), vec4(cube_view, lod)).rgb;
  1384. sky_fog_color = mix(sky_fog_color, texture(samplerCubeArray(radiance_cubemap, material_samplers[SAMPLER_LINEAR_WITH_MIPMAPS_CLAMP]), vec4(cube_view, lod + 1)).rgb, blend);
  1385. #else
  1386. sky_fog_color = textureLod(samplerCube(radiance_cubemap, material_samplers[SAMPLER_LINEAR_WITH_MIPMAPS_CLAMP]), cube_view, mip_level * MAX_ROUGHNESS_LOD).rgb;
  1387. #endif //USE_RADIANCE_CUBEMAP_ARRAY
  1388. fog_color = mix(fog_color, sky_fog_color, scene_data.fog_aerial_perspective);
  1389. }
  1390. if (scene_data.fog_sun_scatter > 0.001) {
  1391. vec4 sun_scatter = vec4(0.0);
  1392. float sun_total = 0.0;
  1393. vec3 view = normalize(vertex);
  1394. for (uint i = 0; i < scene_data.directional_light_count; i++) {
  1395. vec3 light_color = directional_lights.data[i].color * directional_lights.data[i].energy;
  1396. float light_amount = pow(max(dot(view, directional_lights.data[i].direction), 0.0), 8.0);
  1397. fog_color += light_color * light_amount * scene_data.fog_sun_scatter;
  1398. }
  1399. }
  1400. float fog_amount = 1.0 - exp(min(0.0, vertex.z * scene_data.fog_density));
  1401. if (abs(scene_data.fog_height_density) > 0.001) {
  1402. float y = (scene_data.camera_matrix * vec4(vertex, 1.0)).y;
  1403. float y_dist = scene_data.fog_height - y;
  1404. float vfog_amount = clamp(exp(y_dist * scene_data.fog_height_density), 0.0, 1.0);
  1405. fog_amount = max(vfog_amount, fog_amount);
  1406. }
  1407. return vec4(fog_color, fog_amount);
  1408. }
  1409. void cluster_get_item_range(uint p_offset, out uint item_min, out uint item_max, out uint item_from, out uint item_to) {
  1410. uint item_min_max = cluster_buffer.data[p_offset];
  1411. item_min = item_min_max & 0xFFFF;
  1412. item_max = item_min_max >> 16;
  1413. ;
  1414. item_from = item_min >> 5;
  1415. item_to = (item_max == 0) ? 0 : ((item_max - 1) >> 5) + 1; //side effect of how it is stored, as item_max 0 means no elements
  1416. }
  1417. uint cluster_get_range_clip_mask(uint i, uint z_min, uint z_max) {
  1418. int local_min = clamp(int(z_min) - int(i) * 32, 0, 31);
  1419. int mask_width = min(int(z_max) - int(z_min), 32 - local_min);
  1420. return bitfieldInsert(uint(0), uint(0xFFFFFFFF), local_min, mask_width);
  1421. }
  1422. float blur_shadow(float shadow) {
  1423. return shadow;
  1424. #if 0
  1425. //disabling for now, will investigate later
  1426. float interp_shadow = shadow;
  1427. if (gl_HelperInvocation) {
  1428. interp_shadow = -4.0; // technically anything below -4 will do but just to make sure
  1429. }
  1430. uvec2 fc2 = uvec2(gl_FragCoord.xy);
  1431. interp_shadow -= dFdx(interp_shadow) * (float(fc2.x & 1) - 0.5);
  1432. interp_shadow -= dFdy(interp_shadow) * (float(fc2.y & 1) - 0.5);
  1433. if (interp_shadow >= 0.0) {
  1434. shadow = interp_shadow;
  1435. }
  1436. return shadow;
  1437. #endif
  1438. }
  1439. #endif //!MODE_RENDER DEPTH
  1440. void main() {
  1441. #ifdef MODE_DUAL_PARABOLOID
  1442. if (dp_clip > 0.0)
  1443. discard;
  1444. #endif
  1445. //lay out everything, whathever is unused is optimized away anyway
  1446. vec3 vertex = vertex_interp;
  1447. vec3 view = -normalize(vertex_interp);
  1448. vec3 albedo = vec3(1.0);
  1449. vec3 backlight = vec3(0.0);
  1450. vec4 transmittance_color = vec4(0.0);
  1451. float transmittance_depth = 0.0;
  1452. float transmittance_curve = 1.0;
  1453. float transmittance_boost = 0.0;
  1454. float metallic = 0.0;
  1455. float specular = 0.5;
  1456. vec3 emission = vec3(0.0);
  1457. float roughness = 1.0;
  1458. float rim = 0.0;
  1459. float rim_tint = 0.0;
  1460. float clearcoat = 0.0;
  1461. float clearcoat_gloss = 0.0;
  1462. float anisotropy = 0.0;
  1463. vec2 anisotropy_flow = vec2(1.0, 0.0);
  1464. vec4 fog = vec4(0.0);
  1465. #if defined(CUSTOM_RADIANCE_USED)
  1466. vec4 custom_radiance = vec4(0.0);
  1467. #endif
  1468. #if defined(CUSTOM_IRRADIANCE_USED)
  1469. vec4 custom_irradiance = vec4(0.0);
  1470. #endif
  1471. float ao = 1.0;
  1472. float ao_light_affect = 0.0;
  1473. float alpha = 1.0;
  1474. #if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED)
  1475. vec3 binormal = normalize(binormal_interp);
  1476. vec3 tangent = normalize(tangent_interp);
  1477. #else
  1478. vec3 binormal = vec3(0.0);
  1479. vec3 tangent = vec3(0.0);
  1480. #endif
  1481. #ifdef NORMAL_USED
  1482. vec3 normal = normalize(normal_interp);
  1483. #if defined(DO_SIDE_CHECK)
  1484. if (!gl_FrontFacing) {
  1485. normal = -normal;
  1486. }
  1487. #endif
  1488. #endif //NORMAL_USED
  1489. #ifdef UV_USED
  1490. vec2 uv = uv_interp;
  1491. #endif
  1492. #if defined(UV2_USED) || defined(USE_LIGHTMAP)
  1493. vec2 uv2 = uv2_interp;
  1494. #endif
  1495. #if defined(COLOR_USED)
  1496. vec4 color = color_interp;
  1497. #endif
  1498. #if defined(NORMAL_MAP_USED)
  1499. vec3 normal_map = vec3(0.5);
  1500. #endif
  1501. float normal_map_depth = 1.0;
  1502. vec2 screen_uv = gl_FragCoord.xy * scene_data.screen_pixel_size + scene_data.screen_pixel_size * 0.5; //account for center
  1503. float sss_strength = 0.0;
  1504. #ifdef ALPHA_SCISSOR_USED
  1505. float alpha_scissor_threshold = 1.0;
  1506. #endif // ALPHA_SCISSOR_USED
  1507. #ifdef ALPHA_HASH_USED
  1508. float alpha_hash_scale = 1.0;
  1509. #endif // ALPHA_HASH_USED
  1510. #ifdef ALPHA_ANTIALIASING_EDGE_USED
  1511. float alpha_antialiasing_edge = 0.0;
  1512. vec2 alpha_texture_coordinate = vec2(0.0, 0.0);
  1513. #endif // ALPHA_ANTIALIASING_EDGE_USED
  1514. {
  1515. #CODE : FRAGMENT
  1516. }
  1517. #ifdef LIGHT_TRANSMITTANCE_USED
  1518. #ifdef SSS_MODE_SKIN
  1519. transmittance_color.a = sss_strength;
  1520. #else
  1521. transmittance_color.a *= sss_strength;
  1522. #endif
  1523. #endif
  1524. #ifndef USE_SHADOW_TO_OPACITY
  1525. #ifdef ALPHA_SCISSOR_USED
  1526. if (alpha < alpha_scissor_threshold) {
  1527. discard;
  1528. }
  1529. #endif // ALPHA_SCISSOR_USED
  1530. // alpha hash can be used in unison with alpha antialiasing
  1531. #ifdef ALPHA_HASH_USED
  1532. if (alpha < compute_alpha_hash_threshold(vertex, alpha_hash_scale)) {
  1533. discard;
  1534. }
  1535. #endif // ALPHA_HASH_USED
  1536. // If we are not edge antialiasing, we need to remove the output alpha channel from scissor and hash
  1537. #if (defined(ALPHA_SCISSOR_USED) || defined(ALPHA_HASH_USED)) && !defined(ALPHA_ANTIALIASING_EDGE_USED)
  1538. alpha = 1.0;
  1539. #endif
  1540. #ifdef ALPHA_ANTIALIASING_EDGE_USED
  1541. // If alpha scissor is used, we must further the edge threshold, otherwise we wont get any edge feather
  1542. #ifdef ALPHA_SCISSOR_USED
  1543. alpha_antialiasing_edge = clamp(alpha_scissor_threshold + alpha_antialiasing_edge, 0.0, 1.0);
  1544. #endif
  1545. alpha = compute_alpha_antialiasing_edge(alpha, alpha_texture_coordinate, alpha_antialiasing_edge);
  1546. #endif // ALPHA_ANTIALIASING_EDGE_USED
  1547. #ifdef USE_OPAQUE_PREPASS
  1548. if (alpha < opaque_prepass_threshold) {
  1549. discard;
  1550. }
  1551. #endif // USE_OPAQUE_PREPASS
  1552. #endif // !USE_SHADOW_TO_OPACITY
  1553. #ifdef NORMAL_MAP_USED
  1554. normal_map.xy = normal_map.xy * 2.0 - 1.0;
  1555. normal_map.z = sqrt(max(0.0, 1.0 - dot(normal_map.xy, normal_map.xy))); //always ignore Z, as it can be RG packed, Z may be pos/neg, etc.
  1556. normal = normalize(mix(normal, tangent * normal_map.x + binormal * normal_map.y + normal * normal_map.z, normal_map_depth));
  1557. #endif
  1558. #ifdef LIGHT_ANISOTROPY_USED
  1559. if (anisotropy > 0.01) {
  1560. //rotation matrix
  1561. mat3 rot = mat3(tangent, binormal, normal);
  1562. //make local to space
  1563. tangent = normalize(rot * vec3(anisotropy_flow.x, anisotropy_flow.y, 0.0));
  1564. binormal = normalize(rot * vec3(-anisotropy_flow.y, anisotropy_flow.x, 0.0));
  1565. }
  1566. #endif
  1567. #ifdef ENABLE_CLIP_ALPHA
  1568. if (albedo.a < 0.99) {
  1569. //used for doublepass and shadowmapping
  1570. discard;
  1571. }
  1572. #endif
  1573. /////////////////////// FOG //////////////////////
  1574. #ifndef MODE_RENDER_DEPTH
  1575. #ifndef CUSTOM_FOG_USED
  1576. // fog must be processed as early as possible and then packed.
  1577. // to maximize VGPR usage
  1578. // Draw "fixed" fog before volumetric fog to ensure volumetric fog can appear in front of the sky.
  1579. if (scene_data.fog_enabled) {
  1580. fog = fog_process(vertex);
  1581. }
  1582. if (scene_data.volumetric_fog_enabled) {
  1583. vec4 volumetric_fog = volumetric_fog_process(screen_uv, -vertex.z);
  1584. if (scene_data.fog_enabled) {
  1585. //must use the full blending equation here to blend fogs
  1586. vec4 res;
  1587. float sa = 1.0 - volumetric_fog.a;
  1588. res.a = fog.a * sa + volumetric_fog.a;
  1589. if (res.a == 0.0) {
  1590. res.rgb = vec3(0.0);
  1591. } else {
  1592. res.rgb = (fog.rgb * fog.a * sa + volumetric_fog.rgb * volumetric_fog.a) / res.a;
  1593. }
  1594. fog = res;
  1595. } else {
  1596. fog = volumetric_fog;
  1597. }
  1598. }
  1599. #endif //!CUSTOM_FOG_USED
  1600. uint fog_rg = packHalf2x16(fog.rg);
  1601. uint fog_ba = packHalf2x16(fog.ba);
  1602. #endif //!MODE_RENDER_DEPTH
  1603. /////////////////////// DECALS ////////////////////////////////
  1604. #ifndef MODE_RENDER_DEPTH
  1605. uvec2 cluster_pos = uvec2(gl_FragCoord.xy) >> scene_data.cluster_shift;
  1606. uint cluster_offset = (scene_data.cluster_width * cluster_pos.y + cluster_pos.x) * (scene_data.max_cluster_element_count_div_32 + 32);
  1607. uint cluster_z = uint(clamp((-vertex.z / scene_data.z_far) * 32.0, 0.0, 31.0));
  1608. //used for interpolating anything cluster related
  1609. vec3 vertex_ddx = dFdx(vertex);
  1610. vec3 vertex_ddy = dFdy(vertex);
  1611. { // process decals
  1612. uint cluster_decal_offset = cluster_offset + scene_data.cluster_type_size * 2;
  1613. uint item_min;
  1614. uint item_max;
  1615. uint item_from;
  1616. uint item_to;
  1617. cluster_get_item_range(cluster_decal_offset + scene_data.max_cluster_element_count_div_32 + cluster_z, item_min, item_max, item_from, item_to);
  1618. #ifdef USE_SUBGROUPS
  1619. item_from = subgroupBroadcastFirst(subgroupMin(item_from));
  1620. item_to = subgroupBroadcastFirst(subgroupMax(item_to));
  1621. #endif
  1622. for (uint i = item_from; i < item_to; i++) {
  1623. uint mask = cluster_buffer.data[cluster_decal_offset + i];
  1624. mask &= cluster_get_range_clip_mask(i, item_min, item_max);
  1625. #ifdef USE_SUBGROUPS
  1626. uint merged_mask = subgroupBroadcastFirst(subgroupOr(mask));
  1627. #else
  1628. uint merged_mask = mask;
  1629. #endif
  1630. while (merged_mask != 0) {
  1631. uint bit = findMSB(merged_mask);
  1632. merged_mask &= ~(1 << bit);
  1633. #ifdef USE_SUBGROUPS
  1634. if (((1 << bit) & mask) == 0) { //do not process if not originally here
  1635. continue;
  1636. }
  1637. #endif
  1638. uint decal_index = 32 * i + bit;
  1639. if (!bool(decals.data[decal_index].mask & instances.data[instance_index].layer_mask)) {
  1640. continue; //not masked
  1641. }
  1642. vec3 uv_local = (decals.data[decal_index].xform * vec4(vertex, 1.0)).xyz;
  1643. if (any(lessThan(uv_local, vec3(0.0, -1.0, 0.0))) || any(greaterThan(uv_local, vec3(1.0)))) {
  1644. continue; //out of decal
  1645. }
  1646. //we need ddx/ddy for mipmaps, so simulate them
  1647. vec2 ddx = (decals.data[decal_index].xform * vec4(vertex_ddx, 0.0)).xz;
  1648. vec2 ddy = (decals.data[decal_index].xform * vec4(vertex_ddy, 0.0)).xz;
  1649. float fade = pow(1.0 - (uv_local.y > 0.0 ? uv_local.y : -uv_local.y), uv_local.y > 0.0 ? decals.data[decal_index].upper_fade : decals.data[decal_index].lower_fade);
  1650. if (decals.data[decal_index].normal_fade > 0.0) {
  1651. fade *= smoothstep(decals.data[decal_index].normal_fade, 1.0, dot(normal_interp, decals.data[decal_index].normal) * 0.5 + 0.5);
  1652. }
  1653. if (decals.data[decal_index].albedo_rect != vec4(0.0)) {
  1654. //has albedo
  1655. vec4 decal_albedo = textureGrad(sampler2D(decal_atlas_srgb, material_samplers[SAMPLER_LINEAR_WITH_MIPMAPS_CLAMP]), uv_local.xz * decals.data[decal_index].albedo_rect.zw + decals.data[decal_index].albedo_rect.xy, ddx * decals.data[decal_index].albedo_rect.zw, ddy * decals.data[decal_index].albedo_rect.zw);
  1656. decal_albedo *= decals.data[decal_index].modulate;
  1657. decal_albedo.a *= fade;
  1658. albedo = mix(albedo, decal_albedo.rgb, decal_albedo.a * decals.data[decal_index].albedo_mix);
  1659. if (decals.data[decal_index].normal_rect != vec4(0.0)) {
  1660. vec3 decal_normal = textureGrad(sampler2D(decal_atlas, material_samplers[SAMPLER_LINEAR_WITH_MIPMAPS_CLAMP]), uv_local.xz * decals.data[decal_index].normal_rect.zw + decals.data[decal_index].normal_rect.xy, ddx * decals.data[decal_index].normal_rect.zw, ddy * decals.data[decal_index].normal_rect.zw).xyz;
  1661. decal_normal.xy = decal_normal.xy * vec2(2.0, -2.0) - vec2(1.0, -1.0); //users prefer flipped y normal maps in most authoring software
  1662. decal_normal.z = sqrt(max(0.0, 1.0 - dot(decal_normal.xy, decal_normal.xy)));
  1663. //convert to view space, use xzy because y is up
  1664. decal_normal = (decals.data[decal_index].normal_xform * decal_normal.xzy).xyz;
  1665. normal = normalize(mix(normal, decal_normal, decal_albedo.a));
  1666. }
  1667. if (decals.data[decal_index].orm_rect != vec4(0.0)) {
  1668. vec3 decal_orm = textureGrad(sampler2D(decal_atlas, material_samplers[SAMPLER_LINEAR_WITH_MIPMAPS_CLAMP]), uv_local.xz * decals.data[decal_index].orm_rect.zw + decals.data[decal_index].orm_rect.xy, ddx * decals.data[decal_index].orm_rect.zw, ddy * decals.data[decal_index].orm_rect.zw).xyz;
  1669. ao = mix(ao, decal_orm.r, decal_albedo.a);
  1670. roughness = mix(roughness, decal_orm.g, decal_albedo.a);
  1671. metallic = mix(metallic, decal_orm.b, decal_albedo.a);
  1672. }
  1673. }
  1674. if (decals.data[decal_index].emission_rect != vec4(0.0)) {
  1675. //emission is additive, so its independent from albedo
  1676. emission += textureGrad(sampler2D(decal_atlas_srgb, material_samplers[SAMPLER_LINEAR_WITH_MIPMAPS_CLAMP]), uv_local.xz * decals.data[decal_index].emission_rect.zw + decals.data[decal_index].emission_rect.xy, ddx * decals.data[decal_index].emission_rect.zw, ddy * decals.data[decal_index].emission_rect.zw).xyz * decals.data[decal_index].emission_energy * fade;
  1677. }
  1678. }
  1679. }
  1680. }
  1681. //pack albedo until needed again, saves 2 VGPRs in the meantime
  1682. #endif //not render depth
  1683. /////////////////////// LIGHTING //////////////////////////////
  1684. #ifdef NORMAL_USED
  1685. if (scene_data.roughness_limiter_enabled) {
  1686. //http://www.jp.square-enix.com/tech/library/pdf/ImprovedGeometricSpecularAA.pdf
  1687. float roughness2 = roughness * roughness;
  1688. vec3 dndu = dFdx(normal), dndv = dFdx(normal);
  1689. float variance = scene_data.roughness_limiter_amount * (dot(dndu, dndu) + dot(dndv, dndv));
  1690. float kernelRoughness2 = min(2.0 * variance, scene_data.roughness_limiter_limit); //limit effect
  1691. float filteredRoughness2 = min(1.0, roughness2 + kernelRoughness2);
  1692. roughness = sqrt(filteredRoughness2);
  1693. }
  1694. #endif
  1695. //apply energy conservation
  1696. vec3 specular_light = vec3(0.0, 0.0, 0.0);
  1697. vec3 diffuse_light = vec3(0.0, 0.0, 0.0);
  1698. vec3 ambient_light = vec3(0.0, 0.0, 0.0);
  1699. #if !defined(MODE_RENDER_DEPTH) && !defined(MODE_UNSHADED)
  1700. if (scene_data.use_reflection_cubemap) {
  1701. vec3 ref_vec = reflect(-view, normal);
  1702. ref_vec = scene_data.radiance_inverse_xform * ref_vec;
  1703. #ifdef USE_RADIANCE_CUBEMAP_ARRAY
  1704. float lod, blend;
  1705. blend = modf(roughness * MAX_ROUGHNESS_LOD, lod);
  1706. specular_light = texture(samplerCubeArray(radiance_cubemap, material_samplers[SAMPLER_LINEAR_WITH_MIPMAPS_CLAMP]), vec4(ref_vec, lod)).rgb;
  1707. specular_light = mix(specular_light, texture(samplerCubeArray(radiance_cubemap, material_samplers[SAMPLER_LINEAR_WITH_MIPMAPS_CLAMP]), vec4(ref_vec, lod + 1)).rgb, blend);
  1708. #else
  1709. specular_light = textureLod(samplerCube(radiance_cubemap, material_samplers[SAMPLER_LINEAR_WITH_MIPMAPS_CLAMP]), ref_vec, roughness * MAX_ROUGHNESS_LOD).rgb;
  1710. #endif //USE_RADIANCE_CUBEMAP_ARRAY
  1711. specular_light *= scene_data.ambient_light_color_energy.a;
  1712. }
  1713. #if defined(CUSTOM_RADIANCE_USED)
  1714. specular_light = mix(specular_light, custom_radiance.rgb, custom_radiance.a);
  1715. #endif
  1716. #ifndef USE_LIGHTMAP
  1717. //lightmap overrides everything
  1718. if (scene_data.use_ambient_light) {
  1719. ambient_light = scene_data.ambient_light_color_energy.rgb;
  1720. if (scene_data.use_ambient_cubemap) {
  1721. vec3 ambient_dir = scene_data.radiance_inverse_xform * normal;
  1722. #ifdef USE_RADIANCE_CUBEMAP_ARRAY
  1723. vec3 cubemap_ambient = texture(samplerCubeArray(radiance_cubemap, material_samplers[SAMPLER_LINEAR_WITH_MIPMAPS_CLAMP]), vec4(ambient_dir, MAX_ROUGHNESS_LOD)).rgb;
  1724. #else
  1725. vec3 cubemap_ambient = textureLod(samplerCube(radiance_cubemap, material_samplers[SAMPLER_LINEAR_WITH_MIPMAPS_CLAMP]), ambient_dir, MAX_ROUGHNESS_LOD).rgb;
  1726. #endif //USE_RADIANCE_CUBEMAP_ARRAY
  1727. ambient_light = mix(ambient_light, cubemap_ambient * scene_data.ambient_light_color_energy.a, scene_data.ambient_color_sky_mix);
  1728. }
  1729. }
  1730. #endif // USE_LIGHTMAP
  1731. #if defined(CUSTOM_IRRADIANCE_USED)
  1732. ambient_light = mix(specular_light, custom_irradiance.rgb, custom_irradiance.a);
  1733. #endif
  1734. #endif //!defined(MODE_RENDER_DEPTH) && !defined(MODE_UNSHADED)
  1735. //radiance
  1736. /// GI ///
  1737. #if !defined(MODE_RENDER_DEPTH) && !defined(MODE_UNSHADED)
  1738. #ifdef USE_LIGHTMAP
  1739. //lightmap
  1740. if (bool(instances.data[instance_index].flags & INSTANCE_FLAGS_USE_LIGHTMAP_CAPTURE)) { //has lightmap capture
  1741. uint index = instances.data[instance_index].gi_offset;
  1742. vec3 wnormal = mat3(scene_data.camera_matrix) * normal;
  1743. const float c1 = 0.429043;
  1744. const float c2 = 0.511664;
  1745. const float c3 = 0.743125;
  1746. const float c4 = 0.886227;
  1747. const float c5 = 0.247708;
  1748. ambient_light += (c1 * lightmap_captures.data[index].sh[8].rgb * (wnormal.x * wnormal.x - wnormal.y * wnormal.y) +
  1749. c3 * lightmap_captures.data[index].sh[6].rgb * wnormal.z * wnormal.z +
  1750. c4 * lightmap_captures.data[index].sh[0].rgb -
  1751. c5 * lightmap_captures.data[index].sh[6].rgb +
  1752. 2.0 * c1 * lightmap_captures.data[index].sh[4].rgb * wnormal.x * wnormal.y +
  1753. 2.0 * c1 * lightmap_captures.data[index].sh[7].rgb * wnormal.x * wnormal.z +
  1754. 2.0 * c1 * lightmap_captures.data[index].sh[5].rgb * wnormal.y * wnormal.z +
  1755. 2.0 * c2 * lightmap_captures.data[index].sh[3].rgb * wnormal.x +
  1756. 2.0 * c2 * lightmap_captures.data[index].sh[1].rgb * wnormal.y +
  1757. 2.0 * c2 * lightmap_captures.data[index].sh[2].rgb * wnormal.z);
  1758. } else if (bool(instances.data[instance_index].flags & INSTANCE_FLAGS_USE_LIGHTMAP)) { // has actual lightmap
  1759. bool uses_sh = bool(instances.data[instance_index].flags & INSTANCE_FLAGS_USE_SH_LIGHTMAP);
  1760. uint ofs = instances.data[instance_index].gi_offset & 0xFFFF;
  1761. vec3 uvw;
  1762. uvw.xy = uv2 * instances.data[instance_index].lightmap_uv_scale.zw + instances.data[instance_index].lightmap_uv_scale.xy;
  1763. uvw.z = float((instances.data[instance_index].gi_offset >> 16) & 0xFFFF);
  1764. if (uses_sh) {
  1765. uvw.z *= 4.0; //SH textures use 4 times more data
  1766. vec3 lm_light_l0 = textureLod(sampler2DArray(lightmap_textures[ofs], material_samplers[SAMPLER_LINEAR_CLAMP]), uvw + vec3(0.0, 0.0, 0.0), 0.0).rgb;
  1767. vec3 lm_light_l1n1 = textureLod(sampler2DArray(lightmap_textures[ofs], material_samplers[SAMPLER_LINEAR_CLAMP]), uvw + vec3(0.0, 0.0, 1.0), 0.0).rgb;
  1768. vec3 lm_light_l1_0 = textureLod(sampler2DArray(lightmap_textures[ofs], material_samplers[SAMPLER_LINEAR_CLAMP]), uvw + vec3(0.0, 0.0, 2.0), 0.0).rgb;
  1769. vec3 lm_light_l1p1 = textureLod(sampler2DArray(lightmap_textures[ofs], material_samplers[SAMPLER_LINEAR_CLAMP]), uvw + vec3(0.0, 0.0, 3.0), 0.0).rgb;
  1770. uint idx = instances.data[instance_index].gi_offset >> 20;
  1771. vec3 n = normalize(lightmaps.data[idx].normal_xform * normal);
  1772. ambient_light += lm_light_l0 * 0.282095f;
  1773. ambient_light += lm_light_l1n1 * 0.32573 * n.y;
  1774. ambient_light += lm_light_l1_0 * 0.32573 * n.z;
  1775. ambient_light += lm_light_l1p1 * 0.32573 * n.x;
  1776. if (metallic > 0.01) { // since the more direct bounced light is lost, we can kind of fake it with this trick
  1777. vec3 r = reflect(normalize(-vertex), normal);
  1778. specular_light += lm_light_l1n1 * 0.32573 * r.y;
  1779. specular_light += lm_light_l1_0 * 0.32573 * r.z;
  1780. specular_light += lm_light_l1p1 * 0.32573 * r.x;
  1781. }
  1782. } else {
  1783. ambient_light += textureLod(sampler2DArray(lightmap_textures[ofs], material_samplers[SAMPLER_LINEAR_CLAMP]), uvw, 0.0).rgb;
  1784. }
  1785. }
  1786. #elif defined(USE_FORWARD_GI)
  1787. if (bool(instances.data[instance_index].flags & INSTANCE_FLAGS_USE_SDFGI)) { //has lightmap capture
  1788. //make vertex orientation the world one, but still align to camera
  1789. vec3 cam_pos = mat3(scene_data.camera_matrix) * vertex;
  1790. vec3 cam_normal = mat3(scene_data.camera_matrix) * normal;
  1791. vec3 cam_reflection = mat3(scene_data.camera_matrix) * reflect(-view, normal);
  1792. //apply y-mult
  1793. cam_pos.y *= sdfgi.y_mult;
  1794. cam_normal.y *= sdfgi.y_mult;
  1795. cam_normal = normalize(cam_normal);
  1796. cam_reflection.y *= sdfgi.y_mult;
  1797. cam_normal = normalize(cam_normal);
  1798. cam_reflection = normalize(cam_reflection);
  1799. vec4 light_accum = vec4(0.0);
  1800. float weight_accum = 0.0;
  1801. vec4 light_blend_accum = vec4(0.0);
  1802. float weight_blend_accum = 0.0;
  1803. float blend = -1.0;
  1804. // helper constants, compute once
  1805. uint cascade = 0xFFFFFFFF;
  1806. vec3 cascade_pos;
  1807. vec3 cascade_normal;
  1808. for (uint i = 0; i < sdfgi.max_cascades; i++) {
  1809. cascade_pos = (cam_pos - sdfgi.cascades[i].position) * sdfgi.cascades[i].to_probe;
  1810. if (any(lessThan(cascade_pos, vec3(0.0))) || any(greaterThanEqual(cascade_pos, sdfgi.cascade_probe_size))) {
  1811. continue; //skip cascade
  1812. }
  1813. cascade = i;
  1814. break;
  1815. }
  1816. if (cascade < SDFGI_MAX_CASCADES) {
  1817. bool use_specular = true;
  1818. float blend;
  1819. vec3 diffuse, specular;
  1820. sdfgi_process(cascade, cascade_pos, cam_pos, cam_normal, cam_reflection, use_specular, roughness, diffuse, specular, blend);
  1821. if (blend > 0.0) {
  1822. //blend
  1823. if (cascade == sdfgi.max_cascades - 1) {
  1824. diffuse = mix(diffuse, ambient_light, blend);
  1825. if (use_specular) {
  1826. specular = mix(specular, specular_light, blend);
  1827. }
  1828. } else {
  1829. vec3 diffuse2, specular2;
  1830. float blend2;
  1831. cascade_pos = (cam_pos - sdfgi.cascades[cascade + 1].position) * sdfgi.cascades[cascade + 1].to_probe;
  1832. sdfgi_process(cascade + 1, cascade_pos, cam_pos, cam_normal, cam_reflection, use_specular, roughness, diffuse2, specular2, blend2);
  1833. diffuse = mix(diffuse, diffuse2, blend);
  1834. if (use_specular) {
  1835. specular = mix(specular, specular2, blend);
  1836. }
  1837. }
  1838. }
  1839. ambient_light = diffuse;
  1840. if (use_specular) {
  1841. specular_light = specular;
  1842. }
  1843. }
  1844. }
  1845. if (bool(instances.data[instance_index].flags & INSTANCE_FLAGS_USE_GIPROBE)) { // process giprobes
  1846. uint index1 = instances.data[instance_index].gi_offset & 0xFFFF;
  1847. vec3 ref_vec = normalize(reflect(normalize(vertex), normal));
  1848. //find arbitrary tangent and bitangent, then build a matrix
  1849. vec3 v0 = abs(normal.z) < 0.999 ? vec3(0.0, 0.0, 1.0) : vec3(0.0, 1.0, 0.0);
  1850. vec3 tangent = normalize(cross(v0, normal));
  1851. vec3 bitangent = normalize(cross(tangent, normal));
  1852. mat3 normal_mat = mat3(tangent, bitangent, normal);
  1853. vec4 amb_accum = vec4(0.0);
  1854. vec4 spec_accum = vec4(0.0);
  1855. gi_probe_compute(index1, vertex, normal, ref_vec, normal_mat, roughness * roughness, ambient_light, specular_light, spec_accum, amb_accum);
  1856. uint index2 = instances.data[instance_index].gi_offset >> 16;
  1857. if (index2 != 0xFFFF) {
  1858. gi_probe_compute(index2, vertex, normal, ref_vec, normal_mat, roughness * roughness, ambient_light, specular_light, spec_accum, amb_accum);
  1859. }
  1860. if (amb_accum.a > 0.0) {
  1861. amb_accum.rgb /= amb_accum.a;
  1862. }
  1863. if (spec_accum.a > 0.0) {
  1864. spec_accum.rgb /= spec_accum.a;
  1865. }
  1866. specular_light = spec_accum.rgb;
  1867. ambient_light = amb_accum.rgb;
  1868. }
  1869. #else
  1870. if (bool(instances.data[instance_index].flags & INSTANCE_FLAGS_USE_GI_BUFFERS)) { //use GI buffers
  1871. vec2 coord;
  1872. if (scene_data.gi_upscale_for_msaa) {
  1873. vec2 base_coord = screen_uv;
  1874. vec2 closest_coord = base_coord;
  1875. float closest_ang = dot(normal, textureLod(sampler2D(normal_roughness_buffer, material_samplers[SAMPLER_LINEAR_CLAMP]), base_coord, 0.0).xyz * 2.0 - 1.0);
  1876. for (int i = 0; i < 4; i++) {
  1877. const vec2 neighbours[4] = vec2[](vec2(-1, 0), vec2(1, 0), vec2(0, -1), vec2(0, 1));
  1878. vec2 neighbour_coord = base_coord + neighbours[i] * scene_data.screen_pixel_size;
  1879. float neighbour_ang = dot(normal, textureLod(sampler2D(normal_roughness_buffer, material_samplers[SAMPLER_LINEAR_CLAMP]), neighbour_coord, 0.0).xyz * 2.0 - 1.0);
  1880. if (neighbour_ang > closest_ang) {
  1881. closest_ang = neighbour_ang;
  1882. closest_coord = neighbour_coord;
  1883. }
  1884. }
  1885. coord = closest_coord;
  1886. } else {
  1887. coord = screen_uv;
  1888. }
  1889. vec4 buffer_ambient = textureLod(sampler2D(ambient_buffer, material_samplers[SAMPLER_LINEAR_CLAMP]), coord, 0.0);
  1890. vec4 buffer_reflection = textureLod(sampler2D(reflection_buffer, material_samplers[SAMPLER_LINEAR_CLAMP]), coord, 0.0);
  1891. ambient_light = mix(ambient_light, buffer_ambient.rgb, buffer_ambient.a);
  1892. specular_light = mix(specular_light, buffer_reflection.rgb, buffer_reflection.a);
  1893. }
  1894. #endif
  1895. if (scene_data.ssao_enabled) {
  1896. float ssao = texture(sampler2D(ao_buffer, material_samplers[SAMPLER_LINEAR_CLAMP]), screen_uv).r;
  1897. ao = min(ao, ssao);
  1898. ao_light_affect = mix(ao_light_affect, max(ao_light_affect, scene_data.ssao_light_affect), scene_data.ssao_ao_affect);
  1899. }
  1900. { // process reflections
  1901. vec4 reflection_accum = vec4(0.0, 0.0, 0.0, 0.0);
  1902. vec4 ambient_accum = vec4(0.0, 0.0, 0.0, 0.0);
  1903. uint cluster_reflection_offset = cluster_offset + scene_data.cluster_type_size * 3;
  1904. uint item_min;
  1905. uint item_max;
  1906. uint item_from;
  1907. uint item_to;
  1908. cluster_get_item_range(cluster_reflection_offset + scene_data.max_cluster_element_count_div_32 + cluster_z, item_min, item_max, item_from, item_to);
  1909. #ifdef USE_SUBGROUPS
  1910. item_from = subgroupBroadcastFirst(subgroupMin(item_from));
  1911. item_to = subgroupBroadcastFirst(subgroupMax(item_to));
  1912. #endif
  1913. for (uint i = item_from; i < item_to; i++) {
  1914. uint mask = cluster_buffer.data[cluster_reflection_offset + i];
  1915. mask &= cluster_get_range_clip_mask(i, item_min, item_max);
  1916. #ifdef USE_SUBGROUPS
  1917. uint merged_mask = subgroupBroadcastFirst(subgroupOr(mask));
  1918. #else
  1919. uint merged_mask = mask;
  1920. #endif
  1921. while (merged_mask != 0) {
  1922. uint bit = findMSB(merged_mask);
  1923. merged_mask &= ~(1 << bit);
  1924. #ifdef USE_SUBGROUPS
  1925. if (((1 << bit) & mask) == 0) { //do not process if not originally here
  1926. continue;
  1927. }
  1928. #endif
  1929. uint reflection_index = 32 * i + bit;
  1930. if (!bool(reflections.data[reflection_index].mask & instances.data[instance_index].layer_mask)) {
  1931. continue; //not masked
  1932. }
  1933. reflection_process(reflection_index, vertex, normal, roughness, ambient_light, specular_light, ambient_accum, reflection_accum);
  1934. }
  1935. }
  1936. if (reflection_accum.a > 0.0) {
  1937. specular_light = reflection_accum.rgb / reflection_accum.a;
  1938. }
  1939. #if !defined(USE_LIGHTMAP)
  1940. if (ambient_accum.a > 0.0) {
  1941. ambient_light = ambient_accum.rgb / ambient_accum.a;
  1942. }
  1943. #endif
  1944. }
  1945. //finalize ambient light here
  1946. ambient_light *= albedo.rgb;
  1947. ambient_light *= ao;
  1948. // convert ao to direct light ao
  1949. ao = mix(1.0, ao, ao_light_affect);
  1950. //this saves some VGPRs
  1951. vec3 f0 = F0(metallic, specular, albedo);
  1952. {
  1953. #if defined(DIFFUSE_TOON)
  1954. //simplify for toon, as
  1955. specular_light *= specular * metallic * albedo * 2.0;
  1956. #else
  1957. // scales the specular reflections, needs to be be computed before lighting happens,
  1958. // but after environment, GI, and reflection probes are added
  1959. // Environment brdf approximation (Lazarov 2013)
  1960. // see https://www.unrealengine.com/en-US/blog/physically-based-shading-on-mobile
  1961. const vec4 c0 = vec4(-1.0, -0.0275, -0.572, 0.022);
  1962. const vec4 c1 = vec4(1.0, 0.0425, 1.04, -0.04);
  1963. vec4 r = roughness * c0 + c1;
  1964. float ndotv = clamp(dot(normal, view), 0.0, 1.0);
  1965. float a004 = min(r.x * r.x, exp2(-9.28 * ndotv)) * r.x + r.y;
  1966. vec2 env = vec2(-1.04, 1.04) * a004 + r.zw;
  1967. specular_light *= env.x * f0 + env.y;
  1968. #endif
  1969. }
  1970. #endif //GI !defined(MODE_RENDER_DEPTH) && !defined(MODE_UNSHADED)
  1971. #if !defined(MODE_RENDER_DEPTH)
  1972. //this saves some VGPRs
  1973. uint orms = packUnorm4x8(vec4(ao, roughness, metallic, specular));
  1974. #endif
  1975. // LIGHTING
  1976. #if !defined(MODE_RENDER_DEPTH) && !defined(MODE_UNSHADED)
  1977. { //directional light
  1978. // Do shadow and lighting in two passes to reduce register pressure
  1979. uint shadow0 = 0;
  1980. uint shadow1 = 0;
  1981. for (uint i = 0; i < 8; i++) {
  1982. if (i >= scene_data.directional_light_count) {
  1983. break;
  1984. }
  1985. if (!bool(directional_lights.data[i].mask & instances.data[instance_index].layer_mask)) {
  1986. continue; //not masked
  1987. }
  1988. float shadow = 1.0;
  1989. #ifdef USE_SOFT_SHADOWS
  1990. //version with soft shadows, more expensive
  1991. if (directional_lights.data[i].shadow_enabled) {
  1992. float depth_z = -vertex.z;
  1993. vec4 pssm_coord;
  1994. vec3 shadow_color = vec3(0.0);
  1995. vec3 light_dir = directional_lights.data[i].direction;
  1996. #define BIAS_FUNC(m_var, m_idx) \
  1997. m_var.xyz += light_dir * directional_lights.data[i].shadow_bias[m_idx]; \
  1998. vec3 normal_bias = normalize(normal_interp) * (1.0 - max(0.0, dot(light_dir, -normalize(normal_interp)))) * directional_lights.data[i].shadow_normal_bias[m_idx]; \
  1999. normal_bias -= light_dir * dot(light_dir, normal_bias); \
  2000. m_var.xyz += normal_bias;
  2001. if (depth_z < directional_lights.data[i].shadow_split_offsets.x) {
  2002. vec4 v = vec4(vertex, 1.0);
  2003. BIAS_FUNC(v, 0)
  2004. pssm_coord = (directional_lights.data[i].shadow_matrix1 * v);
  2005. pssm_coord /= pssm_coord.w;
  2006. if (directional_lights.data[i].softshadow_angle > 0) {
  2007. float range_pos = dot(directional_lights.data[i].direction, v.xyz);
  2008. float range_begin = directional_lights.data[i].shadow_range_begin.x;
  2009. float test_radius = (range_pos - range_begin) * directional_lights.data[i].softshadow_angle;
  2010. vec2 tex_scale = directional_lights.data[i].uv_scale1 * test_radius;
  2011. shadow = sample_directional_soft_shadow(directional_shadow_atlas, pssm_coord.xyz, tex_scale * directional_lights.data[i].soft_shadow_scale);
  2012. } else {
  2013. shadow = sample_directional_pcf_shadow(directional_shadow_atlas, scene_data.directional_shadow_pixel_size * directional_lights.data[i].soft_shadow_scale, pssm_coord);
  2014. }
  2015. shadow_color = directional_lights.data[i].shadow_color1.rgb;
  2016. } else if (depth_z < directional_lights.data[i].shadow_split_offsets.y) {
  2017. vec4 v = vec4(vertex, 1.0);
  2018. BIAS_FUNC(v, 1)
  2019. pssm_coord = (directional_lights.data[i].shadow_matrix2 * v);
  2020. pssm_coord /= pssm_coord.w;
  2021. if (directional_lights.data[i].softshadow_angle > 0) {
  2022. float range_pos = dot(directional_lights.data[i].direction, v.xyz);
  2023. float range_begin = directional_lights.data[i].shadow_range_begin.y;
  2024. float test_radius = (range_pos - range_begin) * directional_lights.data[i].softshadow_angle;
  2025. vec2 tex_scale = directional_lights.data[i].uv_scale2 * test_radius;
  2026. shadow = sample_directional_soft_shadow(directional_shadow_atlas, pssm_coord.xyz, tex_scale * directional_lights.data[i].soft_shadow_scale);
  2027. } else {
  2028. shadow = sample_directional_pcf_shadow(directional_shadow_atlas, scene_data.directional_shadow_pixel_size * directional_lights.data[i].soft_shadow_scale, pssm_coord);
  2029. }
  2030. shadow_color = directional_lights.data[i].shadow_color2.rgb;
  2031. } else if (depth_z < directional_lights.data[i].shadow_split_offsets.z) {
  2032. vec4 v = vec4(vertex, 1.0);
  2033. BIAS_FUNC(v, 2)
  2034. pssm_coord = (directional_lights.data[i].shadow_matrix3 * v);
  2035. pssm_coord /= pssm_coord.w;
  2036. if (directional_lights.data[i].softshadow_angle > 0) {
  2037. float range_pos = dot(directional_lights.data[i].direction, v.xyz);
  2038. float range_begin = directional_lights.data[i].shadow_range_begin.z;
  2039. float test_radius = (range_pos - range_begin) * directional_lights.data[i].softshadow_angle;
  2040. vec2 tex_scale = directional_lights.data[i].uv_scale3 * test_radius;
  2041. shadow = sample_directional_soft_shadow(directional_shadow_atlas, pssm_coord.xyz, tex_scale * directional_lights.data[i].soft_shadow_scale);
  2042. } else {
  2043. shadow = sample_directional_pcf_shadow(directional_shadow_atlas, scene_data.directional_shadow_pixel_size * directional_lights.data[i].soft_shadow_scale, pssm_coord);
  2044. }
  2045. shadow_color = directional_lights.data[i].shadow_color3.rgb;
  2046. } else {
  2047. vec4 v = vec4(vertex, 1.0);
  2048. BIAS_FUNC(v, 3)
  2049. pssm_coord = (directional_lights.data[i].shadow_matrix4 * v);
  2050. pssm_coord /= pssm_coord.w;
  2051. if (directional_lights.data[i].softshadow_angle > 0) {
  2052. float range_pos = dot(directional_lights.data[i].direction, v.xyz);
  2053. float range_begin = directional_lights.data[i].shadow_range_begin.w;
  2054. float test_radius = (range_pos - range_begin) * directional_lights.data[i].softshadow_angle;
  2055. vec2 tex_scale = directional_lights.data[i].uv_scale4 * test_radius;
  2056. shadow = sample_directional_soft_shadow(directional_shadow_atlas, pssm_coord.xyz, tex_scale * directional_lights.data[i].soft_shadow_scale);
  2057. } else {
  2058. shadow = sample_directional_pcf_shadow(directional_shadow_atlas, scene_data.directional_shadow_pixel_size * directional_lights.data[i].soft_shadow_scale, pssm_coord);
  2059. }
  2060. shadow_color = directional_lights.data[i].shadow_color4.rgb;
  2061. }
  2062. if (directional_lights.data[i].blend_splits) {
  2063. vec3 shadow_color_blend = vec3(0.0);
  2064. float pssm_blend;
  2065. float shadow2;
  2066. if (depth_z < directional_lights.data[i].shadow_split_offsets.x) {
  2067. vec4 v = vec4(vertex, 1.0);
  2068. BIAS_FUNC(v, 1)
  2069. pssm_coord = (directional_lights.data[i].shadow_matrix2 * v);
  2070. pssm_coord /= pssm_coord.w;
  2071. if (directional_lights.data[i].softshadow_angle > 0) {
  2072. float range_pos = dot(directional_lights.data[i].direction, v.xyz);
  2073. float range_begin = directional_lights.data[i].shadow_range_begin.y;
  2074. float test_radius = (range_pos - range_begin) * directional_lights.data[i].softshadow_angle;
  2075. vec2 tex_scale = directional_lights.data[i].uv_scale2 * test_radius;
  2076. shadow2 = sample_directional_soft_shadow(directional_shadow_atlas, pssm_coord.xyz, tex_scale * directional_lights.data[i].soft_shadow_scale);
  2077. } else {
  2078. shadow2 = sample_directional_pcf_shadow(directional_shadow_atlas, scene_data.directional_shadow_pixel_size * directional_lights.data[i].soft_shadow_scale, pssm_coord);
  2079. }
  2080. pssm_blend = smoothstep(0.0, directional_lights.data[i].shadow_split_offsets.x, depth_z);
  2081. shadow_color_blend = directional_lights.data[i].shadow_color2.rgb;
  2082. } else if (depth_z < directional_lights.data[i].shadow_split_offsets.y) {
  2083. vec4 v = vec4(vertex, 1.0);
  2084. BIAS_FUNC(v, 2)
  2085. pssm_coord = (directional_lights.data[i].shadow_matrix3 * v);
  2086. pssm_coord /= pssm_coord.w;
  2087. if (directional_lights.data[i].softshadow_angle > 0) {
  2088. float range_pos = dot(directional_lights.data[i].direction, v.xyz);
  2089. float range_begin = directional_lights.data[i].shadow_range_begin.z;
  2090. float test_radius = (range_pos - range_begin) * directional_lights.data[i].softshadow_angle;
  2091. vec2 tex_scale = directional_lights.data[i].uv_scale3 * test_radius;
  2092. shadow2 = sample_directional_soft_shadow(directional_shadow_atlas, pssm_coord.xyz, tex_scale * directional_lights.data[i].soft_shadow_scale);
  2093. } else {
  2094. shadow2 = sample_directional_pcf_shadow(directional_shadow_atlas, scene_data.directional_shadow_pixel_size * directional_lights.data[i].soft_shadow_scale, pssm_coord);
  2095. }
  2096. pssm_blend = smoothstep(directional_lights.data[i].shadow_split_offsets.x, directional_lights.data[i].shadow_split_offsets.y, depth_z);
  2097. shadow_color_blend = directional_lights.data[i].shadow_color3.rgb;
  2098. } else if (depth_z < directional_lights.data[i].shadow_split_offsets.z) {
  2099. vec4 v = vec4(vertex, 1.0);
  2100. BIAS_FUNC(v, 3)
  2101. pssm_coord = (directional_lights.data[i].shadow_matrix4 * v);
  2102. pssm_coord /= pssm_coord.w;
  2103. if (directional_lights.data[i].softshadow_angle > 0) {
  2104. float range_pos = dot(directional_lights.data[i].direction, v.xyz);
  2105. float range_begin = directional_lights.data[i].shadow_range_begin.w;
  2106. float test_radius = (range_pos - range_begin) * directional_lights.data[i].softshadow_angle;
  2107. vec2 tex_scale = directional_lights.data[i].uv_scale4 * test_radius;
  2108. shadow2 = sample_directional_soft_shadow(directional_shadow_atlas, pssm_coord.xyz, tex_scale * directional_lights.data[i].soft_shadow_scale);
  2109. } else {
  2110. shadow2 = sample_directional_pcf_shadow(directional_shadow_atlas, scene_data.directional_shadow_pixel_size * directional_lights.data[i].soft_shadow_scale, pssm_coord);
  2111. }
  2112. pssm_blend = smoothstep(directional_lights.data[i].shadow_split_offsets.y, directional_lights.data[i].shadow_split_offsets.z, depth_z);
  2113. shadow_color_blend = directional_lights.data[i].shadow_color4.rgb;
  2114. } else {
  2115. pssm_blend = 0.0; //if no blend, same coord will be used (divide by z will result in same value, and already cached)
  2116. }
  2117. pssm_blend = sqrt(pssm_blend);
  2118. shadow = mix(shadow, shadow2, pssm_blend);
  2119. shadow_color = mix(shadow_color, shadow_color_blend, pssm_blend);
  2120. }
  2121. shadow = mix(shadow, 1.0, smoothstep(directional_lights.data[i].fade_from, directional_lights.data[i].fade_to, vertex.z)); //done with negative values for performance
  2122. #undef BIAS_FUNC
  2123. }
  2124. #else
  2125. // Soft shadow disabled version
  2126. if (directional_lights.data[i].shadow_enabled) {
  2127. float depth_z = -vertex.z;
  2128. vec4 pssm_coord;
  2129. vec3 light_dir = directional_lights.data[i].direction;
  2130. vec3 base_normal_bias = normalize(normal_interp) * (1.0 - max(0.0, dot(light_dir, -normalize(normal_interp))));
  2131. #define BIAS_FUNC(m_var, m_idx) \
  2132. m_var.xyz += light_dir * directional_lights.data[i].shadow_bias[m_idx]; \
  2133. vec3 normal_bias = base_normal_bias * directional_lights.data[i].shadow_normal_bias[m_idx]; \
  2134. normal_bias -= light_dir * dot(light_dir, normal_bias); \
  2135. m_var.xyz += normal_bias;
  2136. if (depth_z < directional_lights.data[i].shadow_split_offsets.x) {
  2137. vec4 v = vec4(vertex, 1.0);
  2138. BIAS_FUNC(v, 0)
  2139. pssm_coord = (directional_lights.data[i].shadow_matrix1 * v);
  2140. #ifdef LIGHT_TRANSMITTANCE_USED
  2141. {
  2142. vec4 trans_vertex = vec4(vertex - normalize(normal_interp) * directional_lights.data[i].shadow_transmittance_bias.x, 1.0);
  2143. vec4 trans_coord = directional_lights.data[i].shadow_matrix1 * trans_vertex;
  2144. trans_coord /= trans_coord.w;
  2145. float shadow_z = textureLod(sampler2D(directional_shadow_atlas, material_samplers[SAMPLER_LINEAR_CLAMP]), trans_coord.xy, 0.0).r;
  2146. shadow_z *= directional_lights.data[i].shadow_transmittance_z_scale.x;
  2147. float z = trans_coord.z * directional_lights.data[i].shadow_transmittance_z_scale.x;
  2148. transmittance_z = z - shadow_z;
  2149. }
  2150. #endif
  2151. } else if (depth_z < directional_lights.data[i].shadow_split_offsets.y) {
  2152. vec4 v = vec4(vertex, 1.0);
  2153. BIAS_FUNC(v, 1)
  2154. pssm_coord = (directional_lights.data[i].shadow_matrix2 * v);
  2155. #ifdef LIGHT_TRANSMITTANCE_USED
  2156. {
  2157. vec4 trans_vertex = vec4(vertex - normalize(normal_interp) * directional_lights.data[i].shadow_transmittance_bias.y, 1.0);
  2158. vec4 trans_coord = directional_lights.data[i].shadow_matrix2 * trans_vertex;
  2159. trans_coord /= trans_coord.w;
  2160. float shadow_z = textureLod(sampler2D(directional_shadow_atlas, material_samplers[SAMPLER_LINEAR_CLAMP]), trans_coord.xy, 0.0).r;
  2161. shadow_z *= directional_lights.data[i].shadow_transmittance_z_scale.y;
  2162. float z = trans_coord.z * directional_lights.data[i].shadow_transmittance_z_scale.y;
  2163. transmittance_z = z - shadow_z;
  2164. }
  2165. #endif
  2166. } else if (depth_z < directional_lights.data[i].shadow_split_offsets.z) {
  2167. vec4 v = vec4(vertex, 1.0);
  2168. BIAS_FUNC(v, 2)
  2169. pssm_coord = (directional_lights.data[i].shadow_matrix3 * v);
  2170. #ifdef LIGHT_TRANSMITTANCE_USED
  2171. {
  2172. vec4 trans_vertex = vec4(vertex - normalize(normal_interp) * directional_lights.data[i].shadow_transmittance_bias.z, 1.0);
  2173. vec4 trans_coord = directional_lights.data[i].shadow_matrix3 * trans_vertex;
  2174. trans_coord /= trans_coord.w;
  2175. float shadow_z = textureLod(sampler2D(directional_shadow_atlas, material_samplers[SAMPLER_LINEAR_CLAMP]), trans_coord.xy, 0.0).r;
  2176. shadow_z *= directional_lights.data[i].shadow_transmittance_z_scale.z;
  2177. float z = trans_coord.z * directional_lights.data[i].shadow_transmittance_z_scale.z;
  2178. transmittance_z = z - shadow_z;
  2179. }
  2180. #endif
  2181. } else {
  2182. vec4 v = vec4(vertex, 1.0);
  2183. BIAS_FUNC(v, 3)
  2184. pssm_coord = (directional_lights.data[i].shadow_matrix4 * v);
  2185. #ifdef LIGHT_TRANSMITTANCE_USED
  2186. {
  2187. vec4 trans_vertex = vec4(vertex - normalize(normal_interp) * directional_lights.data[i].shadow_transmittance_bias.w, 1.0);
  2188. vec4 trans_coord = directional_lights.data[i].shadow_matrix4 * trans_vertex;
  2189. trans_coord /= trans_coord.w;
  2190. float shadow_z = textureLod(sampler2D(directional_shadow_atlas, material_samplers[SAMPLER_LINEAR_CLAMP]), trans_coord.xy, 0.0).r;
  2191. shadow_z *= directional_lights.data[i].shadow_transmittance_z_scale.w;
  2192. float z = trans_coord.z * directional_lights.data[i].shadow_transmittance_z_scale.w;
  2193. transmittance_z = z - shadow_z;
  2194. }
  2195. #endif
  2196. }
  2197. pssm_coord /= pssm_coord.w;
  2198. shadow = sample_directional_pcf_shadow(directional_shadow_atlas, scene_data.directional_shadow_pixel_size * directional_lights.data[i].soft_shadow_scale, pssm_coord);
  2199. if (directional_lights.data[i].blend_splits) {
  2200. float pssm_blend;
  2201. if (depth_z < directional_lights.data[i].shadow_split_offsets.x) {
  2202. vec4 v = vec4(vertex, 1.0);
  2203. BIAS_FUNC(v, 1)
  2204. pssm_coord = (directional_lights.data[i].shadow_matrix2 * v);
  2205. pssm_blend = smoothstep(0.0, directional_lights.data[i].shadow_split_offsets.x, depth_z);
  2206. } else if (depth_z < directional_lights.data[i].shadow_split_offsets.y) {
  2207. vec4 v = vec4(vertex, 1.0);
  2208. BIAS_FUNC(v, 2)
  2209. pssm_coord = (directional_lights.data[i].shadow_matrix3 * v);
  2210. pssm_blend = smoothstep(directional_lights.data[i].shadow_split_offsets.x, directional_lights.data[i].shadow_split_offsets.y, depth_z);
  2211. } else if (depth_z < directional_lights.data[i].shadow_split_offsets.z) {
  2212. vec4 v = vec4(vertex, 1.0);
  2213. BIAS_FUNC(v, 3)
  2214. pssm_coord = (directional_lights.data[i].shadow_matrix4 * v);
  2215. pssm_blend = smoothstep(directional_lights.data[i].shadow_split_offsets.y, directional_lights.data[i].shadow_split_offsets.z, depth_z);
  2216. } else {
  2217. pssm_blend = 0.0; //if no blend, same coord will be used (divide by z will result in same value, and already cached)
  2218. }
  2219. pssm_coord /= pssm_coord.w;
  2220. float shadow2 = sample_directional_pcf_shadow(directional_shadow_atlas, scene_data.directional_shadow_pixel_size * directional_lights.data[i].soft_shadow_scale, pssm_coord);
  2221. shadow = mix(shadow, shadow2, pssm_blend);
  2222. }
  2223. shadow = mix(shadow, 1.0, smoothstep(directional_lights.data[i].fade_from, directional_lights.data[i].fade_to, vertex.z)); //done with negative values for performance
  2224. #undef BIAS_FUNC
  2225. }
  2226. #endif
  2227. if (i < 4) {
  2228. shadow0 |= uint(clamp(shadow * 255.0, 0.0, 255.0)) << (i * 8);
  2229. } else {
  2230. shadow1 |= uint(clamp(shadow * 255.0, 0.0, 255.0)) << ((i - 4) * 8);
  2231. }
  2232. }
  2233. for (uint i = 0; i < 8; i++) {
  2234. if (i >= scene_data.directional_light_count) {
  2235. break;
  2236. }
  2237. if (!bool(directional_lights.data[i].mask & instances.data[instance_index].layer_mask)) {
  2238. continue; //not masked
  2239. }
  2240. #ifdef LIGHT_TRANSMITTANCE_USED
  2241. float transmittance_z = transmittance_depth;
  2242. if (directional_lights.data[i].shadow_enabled) {
  2243. float depth_z = -vertex.z;
  2244. if (depth_z < directional_lights.data[i].shadow_split_offsets.x) {
  2245. vec4 trans_vertex = vec4(vertex - normalize(normal_interp) * directional_lights.data[i].shadow_transmittance_bias.x, 1.0);
  2246. vec4 trans_coord = directional_lights.data[i].shadow_matrix1 * trans_vertex;
  2247. trans_coord /= trans_coord.w;
  2248. float shadow_z = textureLod(sampler2D(directional_shadow_atlas, material_samplers[SAMPLER_LINEAR_CLAMP]), trans_coord.xy, 0.0).r;
  2249. shadow_z *= directional_lights.data[i].shadow_transmittance_z_scale.x;
  2250. float z = trans_coord.z * directional_lights.data[i].shadow_transmittance_z_scale.x;
  2251. transmittance_z = z - shadow_z;
  2252. } else if (depth_z < directional_lights.data[i].shadow_split_offsets.y) {
  2253. vec4 trans_vertex = vec4(vertex - normalize(normal_interp) * directional_lights.data[i].shadow_transmittance_bias.y, 1.0);
  2254. vec4 trans_coord = directional_lights.data[i].shadow_matrix2 * trans_vertex;
  2255. trans_coord /= trans_coord.w;
  2256. float shadow_z = textureLod(sampler2D(directional_shadow_atlas, material_samplers[SAMPLER_LINEAR_CLAMP]), trans_coord.xy, 0.0).r;
  2257. shadow_z *= directional_lights.data[i].shadow_transmittance_z_scale.y;
  2258. float z = trans_coord.z * directional_lights.data[i].shadow_transmittance_z_scale.y;
  2259. transmittance_z = z - shadow_z;
  2260. } else if (depth_z < directional_lights.data[i].shadow_split_offsets.z) {
  2261. vec4 trans_vertex = vec4(vertex - normalize(normal_interp) * directional_lights.data[i].shadow_transmittance_bias.z, 1.0);
  2262. vec4 trans_coord = directional_lights.data[i].shadow_matrix3 * trans_vertex;
  2263. trans_coord /= trans_coord.w;
  2264. float shadow_z = textureLod(sampler2D(directional_shadow_atlas, material_samplers[SAMPLER_LINEAR_CLAMP]), trans_coord.xy, 0.0).r;
  2265. shadow_z *= directional_lights.data[i].shadow_transmittance_z_scale.z;
  2266. float z = trans_coord.z * directional_lights.data[i].shadow_transmittance_z_scale.z;
  2267. transmittance_z = z - shadow_z;
  2268. } else {
  2269. vec4 trans_vertex = vec4(vertex - normalize(normal_interp) * directional_lights.data[i].shadow_transmittance_bias.w, 1.0);
  2270. vec4 trans_coord = directional_lights.data[i].shadow_matrix4 * trans_vertex;
  2271. trans_coord /= trans_coord.w;
  2272. float shadow_z = textureLod(sampler2D(directional_shadow_atlas, material_samplers[SAMPLER_LINEAR_CLAMP]), trans_coord.xy, 0.0).r;
  2273. shadow_z *= directional_lights.data[i].shadow_transmittance_z_scale.w;
  2274. float z = trans_coord.z * directional_lights.data[i].shadow_transmittance_z_scale.w;
  2275. transmittance_z = z - shadow_z;
  2276. }
  2277. #endif
  2278. float shadow = 1.0;
  2279. if (i < 4) {
  2280. shadow = float(shadow0 >> (i * 8) & 0xFF) / 255.0;
  2281. } else {
  2282. shadow = float(shadow1 >> ((i - 4) * 8) & 0xFF) / 255.0;
  2283. }
  2284. blur_shadow(shadow);
  2285. light_compute(normal, directional_lights.data[i].direction, normalize(view), directional_lights.data[i].color * directional_lights.data[i].energy, shadow, f0, orms, 1.0,
  2286. #ifdef LIGHT_BACKLIGHT_USED
  2287. backlight,
  2288. #endif
  2289. #ifdef LIGHT_TRANSMITTANCE_USED
  2290. transmittance_color,
  2291. transmittance_depth,
  2292. transmittance_curve,
  2293. transmittance_boost,
  2294. transmittance_z,
  2295. #endif
  2296. #ifdef LIGHT_RIM_USED
  2297. rim, rim_tint, albedo,
  2298. #endif
  2299. #ifdef LIGHT_CLEARCOAT_USED
  2300. clearcoat, clearcoat_gloss,
  2301. #endif
  2302. #ifdef LIGHT_ANISOTROPY_USED
  2303. binormal, tangent, anisotropy,
  2304. #endif
  2305. #ifdef USE_SOFT_SHADOW
  2306. directional_lights.data[i].size,
  2307. #endif
  2308. #ifdef USE_SHADOW_TO_OPACITY
  2309. alpha,
  2310. #endif
  2311. diffuse_light,
  2312. specular_light);
  2313. }
  2314. }
  2315. { //omni lights
  2316. uint cluster_omni_offset = cluster_offset;
  2317. uint item_min;
  2318. uint item_max;
  2319. uint item_from;
  2320. uint item_to;
  2321. cluster_get_item_range(cluster_omni_offset + scene_data.max_cluster_element_count_div_32 + cluster_z, item_min, item_max, item_from, item_to);
  2322. #ifdef USE_SUBGROUPS
  2323. item_from = subgroupBroadcastFirst(subgroupMin(item_from));
  2324. item_to = subgroupBroadcastFirst(subgroupMax(item_to));
  2325. #endif
  2326. for (uint i = item_from; i < item_to; i++) {
  2327. uint mask = cluster_buffer.data[cluster_omni_offset + i];
  2328. mask &= cluster_get_range_clip_mask(i, item_min, item_max);
  2329. #ifdef USE_SUBGROUPS
  2330. uint merged_mask = subgroupBroadcastFirst(subgroupOr(mask));
  2331. #else
  2332. uint merged_mask = mask;
  2333. #endif
  2334. while (merged_mask != 0) {
  2335. uint bit = findMSB(merged_mask);
  2336. merged_mask &= ~(1 << bit);
  2337. #ifdef USE_SUBGROUPS
  2338. if (((1 << bit) & mask) == 0) { //do not process if not originally here
  2339. continue;
  2340. }
  2341. #endif
  2342. uint light_index = 32 * i + bit;
  2343. if (!bool(omni_lights.data[light_index].mask & instances.data[instance_index].layer_mask)) {
  2344. continue; //not masked
  2345. }
  2346. float shadow = light_process_omni_shadow(light_index, vertex, view);
  2347. shadow = blur_shadow(shadow);
  2348. light_process_omni(light_index, vertex, view, normal, vertex_ddx, vertex_ddy, f0, orms, shadow,
  2349. #ifdef LIGHT_BACKLIGHT_USED
  2350. backlight,
  2351. #endif
  2352. #ifdef LIGHT_TRANSMITTANCE_USED
  2353. transmittance_color,
  2354. transmittance_depth,
  2355. transmittance_curve,
  2356. transmittance_boost,
  2357. #endif
  2358. #ifdef LIGHT_RIM_USED
  2359. rim,
  2360. rim_tint,
  2361. albedo,
  2362. #endif
  2363. #ifdef LIGHT_CLEARCOAT_USED
  2364. clearcoat, clearcoat_gloss,
  2365. #endif
  2366. #ifdef LIGHT_ANISOTROPY_USED
  2367. tangent, binormal, anisotropy,
  2368. #endif
  2369. #ifdef USE_SHADOW_TO_OPACITY
  2370. alpha,
  2371. #endif
  2372. diffuse_light, specular_light);
  2373. }
  2374. }
  2375. }
  2376. { //spot lights
  2377. uint cluster_spot_offset = cluster_offset + scene_data.cluster_type_size;
  2378. uint item_min;
  2379. uint item_max;
  2380. uint item_from;
  2381. uint item_to;
  2382. cluster_get_item_range(cluster_spot_offset + scene_data.max_cluster_element_count_div_32 + cluster_z, item_min, item_max, item_from, item_to);
  2383. #ifdef USE_SUBGROUPS
  2384. item_from = subgroupBroadcastFirst(subgroupMin(item_from));
  2385. item_to = subgroupBroadcastFirst(subgroupMax(item_to));
  2386. #endif
  2387. for (uint i = item_from; i < item_to; i++) {
  2388. uint mask = cluster_buffer.data[cluster_spot_offset + i];
  2389. mask &= cluster_get_range_clip_mask(i, item_min, item_max);
  2390. #ifdef USE_SUBGROUPS
  2391. uint merged_mask = subgroupBroadcastFirst(subgroupOr(mask));
  2392. #else
  2393. uint merged_mask = mask;
  2394. #endif
  2395. while (merged_mask != 0) {
  2396. uint bit = findMSB(merged_mask);
  2397. merged_mask &= ~(1 << bit);
  2398. #ifdef USE_SUBGROUPS
  2399. if (((1 << bit) & mask) == 0) { //do not process if not originally here
  2400. continue;
  2401. }
  2402. #endif
  2403. uint light_index = 32 * i + bit;
  2404. if (!bool(spot_lights.data[light_index].mask & instances.data[instance_index].layer_mask)) {
  2405. continue; //not masked
  2406. }
  2407. float shadow = light_process_spot_shadow(light_index, vertex, view);
  2408. shadow = blur_shadow(shadow);
  2409. light_process_spot(light_index, vertex, view, normal, vertex_ddx, vertex_ddy, f0, orms, shadow,
  2410. #ifdef LIGHT_BACKLIGHT_USED
  2411. backlight,
  2412. #endif
  2413. #ifdef LIGHT_TRANSMITTANCE_USED
  2414. transmittance_color,
  2415. transmittance_depth,
  2416. transmittance_curve,
  2417. transmittance_boost,
  2418. #endif
  2419. #ifdef LIGHT_RIM_USED
  2420. rim,
  2421. rim_tint,
  2422. albedo,
  2423. #endif
  2424. #ifdef LIGHT_CLEARCOAT_USED
  2425. clearcoat, clearcoat_gloss,
  2426. #endif
  2427. #ifdef LIGHT_ANISOTROPY_USED
  2428. tangent, binormal, anisotropy,
  2429. #endif
  2430. #ifdef USE_SHADOW_TO_OPACITY
  2431. alpha,
  2432. #endif
  2433. diffuse_light, specular_light);
  2434. }
  2435. }
  2436. }
  2437. #ifdef USE_SHADOW_TO_OPACITY
  2438. alpha = min(alpha, clamp(length(ambient_light), 0.0, 1.0));
  2439. #if defined(ALPHA_SCISSOR_USED)
  2440. if (alpha < alpha_scissor) {
  2441. discard;
  2442. }
  2443. #endif // ALPHA_SCISSOR_USED
  2444. #ifdef USE_OPAQUE_PREPASS
  2445. if (alpha < opaque_prepass_threshold) {
  2446. discard;
  2447. }
  2448. #endif // USE_OPAQUE_PREPASS
  2449. #endif // USE_SHADOW_TO_OPACITY
  2450. #endif //!defined(MODE_RENDER_DEPTH) && !defined(MODE_UNSHADED)
  2451. #ifdef MODE_RENDER_DEPTH
  2452. #ifdef MODE_RENDER_SDF
  2453. {
  2454. vec3 local_pos = (scene_data.sdf_to_bounds * vec4(vertex, 1.0)).xyz;
  2455. ivec3 grid_pos = scene_data.sdf_offset + ivec3(local_pos * vec3(scene_data.sdf_size));
  2456. uint albedo16 = 0x1; //solid flag
  2457. albedo16 |= clamp(uint(albedo.r * 31.0), 0, 31) << 11;
  2458. albedo16 |= clamp(uint(albedo.g * 31.0), 0, 31) << 6;
  2459. albedo16 |= clamp(uint(albedo.b * 31.0), 0, 31) << 1;
  2460. imageStore(albedo_volume_grid, grid_pos, uvec4(albedo16));
  2461. uint facing_bits = 0;
  2462. const vec3 aniso_dir[6] = vec3[](
  2463. vec3(1, 0, 0),
  2464. vec3(0, 1, 0),
  2465. vec3(0, 0, 1),
  2466. vec3(-1, 0, 0),
  2467. vec3(0, -1, 0),
  2468. vec3(0, 0, -1));
  2469. vec3 cam_normal = mat3(scene_data.camera_matrix) * normalize(normal_interp);
  2470. float closest_dist = -1e20;
  2471. for (uint i = 0; i < 6; i++) {
  2472. float d = dot(cam_normal, aniso_dir[i]);
  2473. if (d > closest_dist) {
  2474. closest_dist = d;
  2475. facing_bits = (1 << i);
  2476. }
  2477. }
  2478. imageAtomicOr(geom_facing_grid, grid_pos, facing_bits); //store facing bits
  2479. if (length(emission) > 0.001) {
  2480. float lumas[6];
  2481. vec3 light_total = vec3(0);
  2482. for (int i = 0; i < 6; i++) {
  2483. float strength = max(0.0, dot(cam_normal, aniso_dir[i]));
  2484. vec3 light = emission * strength;
  2485. light_total += light;
  2486. lumas[i] = max(light.r, max(light.g, light.b));
  2487. }
  2488. float luma_total = max(light_total.r, max(light_total.g, light_total.b));
  2489. uint light_aniso = 0;
  2490. for (int i = 0; i < 6; i++) {
  2491. light_aniso |= min(31, uint((lumas[i] / luma_total) * 31.0)) << (i * 5);
  2492. }
  2493. //compress to RGBE9995 to save space
  2494. const float pow2to9 = 512.0f;
  2495. const float B = 15.0f;
  2496. const float N = 9.0f;
  2497. const float LN2 = 0.6931471805599453094172321215;
  2498. float cRed = clamp(light_total.r, 0.0, 65408.0);
  2499. float cGreen = clamp(light_total.g, 0.0, 65408.0);
  2500. float cBlue = clamp(light_total.b, 0.0, 65408.0);
  2501. float cMax = max(cRed, max(cGreen, cBlue));
  2502. float expp = max(-B - 1.0f, floor(log(cMax) / LN2)) + 1.0f + B;
  2503. float sMax = floor((cMax / pow(2.0f, expp - B - N)) + 0.5f);
  2504. float exps = expp + 1.0f;
  2505. if (0.0 <= sMax && sMax < pow2to9) {
  2506. exps = expp;
  2507. }
  2508. float sRed = floor((cRed / pow(2.0f, exps - B - N)) + 0.5f);
  2509. float sGreen = floor((cGreen / pow(2.0f, exps - B - N)) + 0.5f);
  2510. float sBlue = floor((cBlue / pow(2.0f, exps - B - N)) + 0.5f);
  2511. //store as 8985 to have 2 extra neighbour bits
  2512. uint light_rgbe = ((uint(sRed) & 0x1FF) >> 1) | ((uint(sGreen) & 0x1FF) << 8) | (((uint(sBlue) & 0x1FF) >> 1) << 17) | ((uint(exps) & 0x1F) << 25);
  2513. imageStore(emission_grid, grid_pos, uvec4(light_rgbe));
  2514. imageStore(emission_aniso_grid, grid_pos, uvec4(light_aniso));
  2515. }
  2516. }
  2517. #endif
  2518. #ifdef MODE_RENDER_MATERIAL
  2519. albedo_output_buffer.rgb = albedo;
  2520. albedo_output_buffer.a = alpha;
  2521. normal_output_buffer.rgb = normal * 0.5 + 0.5;
  2522. normal_output_buffer.a = 0.0;
  2523. depth_output_buffer.r = -vertex.z;
  2524. orm_output_buffer.r = ao;
  2525. orm_output_buffer.g = roughness;
  2526. orm_output_buffer.b = metallic;
  2527. orm_output_buffer.a = sss_strength;
  2528. emission_output_buffer.rgb = emission;
  2529. emission_output_buffer.a = 0.0;
  2530. #endif
  2531. #ifdef MODE_RENDER_NORMAL_ROUGHNESS
  2532. normal_roughness_output_buffer = vec4(normal * 0.5 + 0.5, roughness);
  2533. #ifdef MODE_RENDER_GIPROBE
  2534. if (bool(instances.data[instance_index].flags & INSTANCE_FLAGS_USE_GIPROBE)) { // process giprobes
  2535. uint index1 = instances.data[instance_index].gi_offset & 0xFFFF;
  2536. uint index2 = instances.data[instance_index].gi_offset >> 16;
  2537. giprobe_buffer.x = index1 & 0xFF;
  2538. giprobe_buffer.y = index2 & 0xFF;
  2539. } else {
  2540. giprobe_buffer.x = 0xFF;
  2541. giprobe_buffer.y = 0xFF;
  2542. }
  2543. #endif
  2544. #endif //MODE_RENDER_NORMAL_ROUGHNESS
  2545. //nothing happens, so a tree-ssa optimizer will result in no fragment shader :)
  2546. #else
  2547. // multiply by albedo
  2548. diffuse_light *= albedo; // ambient must be multiplied by albedo at the end
  2549. // apply direct light AO
  2550. ao = unpackUnorm4x8(orms).x;
  2551. specular_light *= ao;
  2552. diffuse_light *= ao;
  2553. // apply metallic
  2554. metallic = unpackUnorm4x8(orms).z;
  2555. diffuse_light *= 1.0 - metallic;
  2556. ambient_light *= 1.0 - metallic;
  2557. //restore fog
  2558. fog = vec4(unpackHalf2x16(fog_rg), unpackHalf2x16(fog_ba));
  2559. #ifdef MODE_MULTIPLE_RENDER_TARGETS
  2560. #ifdef MODE_UNSHADED
  2561. diffuse_buffer = vec4(albedo.rgb, 0.0);
  2562. specular_buffer = vec4(0.0);
  2563. #else
  2564. #ifdef SSS_MODE_SKIN
  2565. sss_strength = -sss_strength;
  2566. #endif
  2567. diffuse_buffer = vec4(emission + diffuse_light + ambient_light, sss_strength);
  2568. specular_buffer = vec4(specular_light, metallic);
  2569. #endif
  2570. diffuse_buffer.rgb = mix(diffuse_buffer.rgb, fog.rgb, fog.a);
  2571. specular_buffer.rgb = mix(specular_buffer.rgb, vec3(0.0), fog.a);
  2572. #else //MODE_MULTIPLE_RENDER_TARGETS
  2573. #ifdef MODE_UNSHADED
  2574. frag_color = vec4(albedo, alpha);
  2575. #else
  2576. frag_color = vec4(emission + ambient_light + diffuse_light + specular_light, alpha);
  2577. //frag_color = vec4(1.0);
  2578. #endif //USE_NO_SHADING
  2579. // Draw "fixed" fog before volumetric fog to ensure volumetric fog can appear in front of the sky.
  2580. frag_color.rgb = mix(frag_color.rgb, fog.rgb, fog.a);
  2581. ;
  2582. #endif //MODE_MULTIPLE_RENDER_TARGETS
  2583. #endif //MODE_RENDER_DEPTH
  2584. }