Keine Beschreibung
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

ProbeVolumeDebugFunctions.hlsl 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. #ifndef PROBEVOLUMEDEBUG_FUNCTIONS_HLSL
  2. #define PROBEVOLUMEDEBUG_FUNCTIONS_HLSL
  3. #ifdef PROBE_VOLUME_DEBUG_FUNCTION_MAIN
  4. v2f vert(appdata v)
  5. {
  6. v2f o;
  7. ZERO_INITIALIZE(v2f, o);
  8. UNITY_SETUP_INSTANCE_ID(v);
  9. UNITY_TRANSFER_INSTANCE_ID(v, o);
  10. if (!ShouldCull(o))
  11. {
  12. float3 probePosition_WS = mul(UNITY_MATRIX_M, float4(0.0f, 0.0f, 0.0f, 1.0f)).xyz;
  13. if (_AdjustmentVolumeCount > 0 && !IsInSelection(probePosition_WS))
  14. {
  15. DoCull(o);
  16. }
  17. else if (_DebugProbeVolumeSampling) // Only sampled probes (8 of them) should be shown, the other should be culled
  18. {
  19. float4 debugPosition = _positionNormalBuffer[0];
  20. float4 debugNormal = _positionNormalBuffer[1];
  21. float3 snappedProbePosition_WS; // worldspace position of main probe (a corner of the 8 probes cube)
  22. float3 samplingPositionNoAntiLeak_WS; // // worldspace sampling position after applying 'NormalBias', 'ViewBias'
  23. float probeDistance;
  24. float3 normalizedOffset; // normalized offset between sampling position and snappedProbePosition
  25. float validityWeight[8];
  26. FindSamplingData(debugPosition.xyz, debugNormal.xyz, _RenderingLayerMask, snappedProbePosition_WS, samplingPositionNoAntiLeak_WS, probeDistance, normalizedOffset, validityWeight);
  27. float samplingFactor = ComputeSamplingFactor(probePosition_WS, snappedProbePosition_WS, normalizedOffset, probeDistance);
  28. // Let's cull probes that are not sampled
  29. if (samplingFactor == -1.0f)
  30. {
  31. DoCull(o);
  32. return o;
  33. }
  34. float4 wsPos = mul(UNITY_MATRIX_M, float4(0.0f, 0.0f, 0.0f, 1.0f));
  35. wsPos += normalize(mul(UNITY_MATRIX_M, float4((v.vertex.xyz), 0.0f))) * _ProbeSize * 0.3f; // avoid scale from transformation matrix to be effective (otherwise some probes are bigger than others)
  36. float4 pos = mul(UNITY_MATRIX_VP, wsPos);
  37. float remappedDepth = Remap(-1.0f, 1.0f, 0.6f, 1.0f, pos.z); // remapped depth to draw gizmo on top of most other objects
  38. o.vertex = float4(pos.x, pos.y, remappedDepth * pos.w, pos.w);
  39. o.normal = normalize(mul(v.normal, (float3x3)UNITY_MATRIX_M));
  40. o.color = v.color;
  41. o.texCoord = v.texCoord;
  42. o.samplingFactor_ValidityWeight = float2(samplingFactor, 1.0f);
  43. }
  44. else
  45. {
  46. float4 wsPos = mul(UNITY_MATRIX_M, float4(v.vertex.xyz * _ProbeSize, 1.0));
  47. o.vertex = mul(UNITY_MATRIX_VP, wsPos);
  48. o.normal = normalize(mul(v.normal, (float3x3)UNITY_MATRIX_M));
  49. if (_ShadingMode == DEBUGPROBESHADINGMODE_RENDERING_LAYER_MASKS)
  50. {
  51. o.centerCoordSS = _ScreenSize.xy * ComputeNormalizedDeviceCoordinatesWithZ(probePosition_WS, UNITY_MATRIX_VP).xy;
  52. if (_APVLayerCount != 1 & (asuint(UNITY_ACCESS_INSTANCED_PROP(Props, _RenderingLayer)) & _RenderingLayerMask) == 0)
  53. DoCull(o);
  54. }
  55. }
  56. }
  57. return o;
  58. }
  59. float4 frag(v2f i) : SV_Target
  60. {
  61. UNITY_SETUP_INSTANCE_ID(i);
  62. if (_ShadingMode >= DEBUGPROBESHADINGMODE_SH && _ShadingMode <= DEBUGPROBESHADINGMODE_SHL0L1
  63. || _ShadingMode == DEBUGPROBESHADINGMODE_SKY_OCCLUSION_SH || _ShadingMode == DEBUGPROBESHADINGMODE_SKY_DIRECTION || _ShadingMode == DEBUGPROBESHADINGMODE_PROBE_OCCLUSION)
  64. {
  65. return float4(CalculateDiffuseLighting(i) * exp2(_ExposureCompensation) * GetCurrentExposureMultiplier(), 1);
  66. }
  67. else if (_ShadingMode == DEBUGPROBESHADINGMODE_INVALIDATED_BY_ADJUSTMENT_VOLUMES)
  68. {
  69. float4 defaultCol = float4(CalculateDiffuseLighting(i) * exp2(_ExposureCompensation) * GetCurrentExposureMultiplier(), 1);
  70. float touchupAction = UNITY_ACCESS_INSTANCED_PROP(Props, _TouchupedByVolume);
  71. if (touchupAction > 0 && touchupAction < 1)
  72. {
  73. return float4(1, 0, 0, 1);
  74. }
  75. return defaultCol;
  76. }
  77. else if (_ShadingMode == DEBUGPROBESHADINGMODE_VALIDITY)
  78. {
  79. float validity = UNITY_ACCESS_INSTANCED_PROP(Props, _Validity);
  80. float threshold = PROBE_VALIDITY_THRESHOLD;
  81. return lerp(float4(0, 1, 0, 1), float4(1, 0, 0, 1), validity > threshold);
  82. }
  83. else if (_ShadingMode == DEBUGPROBESHADINGMODE_VALIDITY_OVER_DILATION_THRESHOLD)
  84. {
  85. float validity = UNITY_ACCESS_INSTANCED_PROP(Props, _Validity);
  86. float threshold = UNITY_ACCESS_INSTANCED_PROP(Props, _DilationThreshold);
  87. if (validity > threshold)
  88. {
  89. return float4(1, 0, 0, 1);
  90. }
  91. else
  92. {
  93. return float4(0, 1, 0, 1);
  94. }
  95. }
  96. else if (_ShadingMode == DEBUGPROBESHADINGMODE_RENDERING_LAYER_MASKS)
  97. {
  98. float3 colors[4] = {
  99. float3(230, 159, 0) / 255.0f,
  100. float3(0, 158, 115) / 255.0f,
  101. float3(0, 114, 178) / 255.0f,
  102. float3(204, 121, 167) / 255.0f,
  103. };
  104. if (_APVLayerCount == 1) return _DebugEmptyProbeData; // Rendering layers are not baked
  105. uint renderingLayer = asuint(UNITY_ACCESS_INSTANCED_PROP(Props, _RenderingLayer)) & _RenderingLayerMask;
  106. uint stripeSize = 8;
  107. float3 result = float3(0, 0, 0);
  108. int2 positionSS = i.vertex.xy;
  109. uint layerId = 0, layerCount = countbits(renderingLayer);
  110. int colorIndex = 0;
  111. if (layerCount >= 2 && positionSS.y < i.centerCoordSS.y)
  112. colorIndex = 1;
  113. if (layerCount >= 3 && colorIndex == 1 && positionSS.x < i.centerCoordSS.x)
  114. colorIndex = 2;
  115. if (layerCount >= 4 && colorIndex == 0 && positionSS.x < i.centerCoordSS.x)
  116. colorIndex = 3;
  117. for (uint l = 0; (l < _APVLayerCount) && (layerId < layerCount); l++)
  118. {
  119. [branch]
  120. if (renderingLayer & (1U << l))
  121. {
  122. if (colorIndex == 0)
  123. result = colors[l];
  124. colorIndex--;
  125. }
  126. }
  127. // NdotV to make the debug view easier to understand
  128. float3 N = normalize(i.normal);
  129. float3 V = UNITY_MATRIX_V[2].xyz;
  130. return float4(result * max(0, dot(N, V)), 1);
  131. }
  132. else if (_ShadingMode == DEBUGPROBESHADINGMODE_SIZE)
  133. {
  134. float4 col = lerp(float4(0, 1, 0, 1), float4(1, 0, 0, 1), UNITY_ACCESS_INSTANCED_PROP(Props, _RelativeSize));
  135. return col;
  136. }
  137. return _Color;
  138. }
  139. #endif
  140. #ifdef PROBE_VOLUME_DEBUG_FUNCTION_FRAGMENTATION
  141. int _ChunkCount;
  142. StructuredBuffer<int> _DebugFragmentation;
  143. struct Attributes
  144. {
  145. uint vertexID : SV_VertexID;
  146. };
  147. struct Varyings
  148. {
  149. float4 positionCS : SV_POSITION;
  150. float2 texcoord : TEXCOORD0;
  151. };
  152. Varyings Vert(Attributes input)
  153. {
  154. Varyings output;
  155. output.positionCS = GetFullScreenTriangleVertexPosition(input.vertexID);
  156. output.texcoord = GetFullScreenTriangleTexCoord(input.vertexID);
  157. return output;
  158. }
  159. float4 Frag(Varyings input) : SV_Target
  160. {
  161. int lineSize = (int)ceil(sqrt(_ChunkCount));
  162. int2 coord = (int2)(input.texcoord * lineSize);
  163. int index = coord.y * lineSize + coord.x;
  164. float4 color = 0.0;
  165. if (index < _ChunkCount && _DebugFragmentation[index] != -1)
  166. color = float4(0.0, 1.0, 0.0, 1.0);
  167. return color;
  168. }
  169. #endif
  170. #ifdef PROBE_VOLUME_DEBUG_FUNCTION_OFFSET
  171. v2f vert(appdata v)
  172. {
  173. v2f o;
  174. UNITY_SETUP_INSTANCE_ID(v);
  175. UNITY_TRANSFER_INSTANCE_ID(v, o);
  176. o.vertex = 0;
  177. o.normal = 0;
  178. float3 probePosition_WS = mul(UNITY_MATRIX_M, float4(0.0f, 0.0f, 1.0f, 1.0f)).xyz;
  179. float3 offset = UNITY_ACCESS_INSTANCED_PROP(Props, _Offset).xyz;
  180. float offsetLenSqr = dot(offset, offset);
  181. if (offsetLenSqr <= 1e-6f)
  182. {
  183. DoCull(o);
  184. }
  185. else if (_AdjustmentVolumeCount > 0 && !IsInSelection(probePosition_WS))
  186. {
  187. DoCull(o);
  188. }
  189. else if (!ShouldCull(o))
  190. {
  191. float4 wsPos = mul(UNITY_MATRIX_M, float4(v.vertex.x * _OffsetSize, v.vertex.y * _OffsetSize, v.vertex.z, 1.f));
  192. o.vertex = mul(UNITY_MATRIX_VP, wsPos);
  193. o.normal = normalize(mul(v.normal, (float3x3)UNITY_MATRIX_M));
  194. }
  195. return o;
  196. }
  197. float4 frag(v2f i) : SV_Target
  198. {
  199. return float4(0, 0, 1, 1);
  200. }
  201. #endif
  202. #ifdef PROBE_VOLUME_DEBUG_FUNCTION_SAMPLING
  203. v2f vert(appdata v)
  204. {
  205. v2f o;
  206. ZERO_INITIALIZE(v2f, o);
  207. float4 debugPosition = _positionNormalBuffer[0];
  208. float4 debugNormal = _positionNormalBuffer[1];
  209. float4 wsPos = float4(0.0f, 0.0f, 0.0f, 1.0f);
  210. float samplingFactor = 0.0f; // probe sampling weight (when needed) is compute in vertex shader. Usefull for drawing 8 debug quads showing weights
  211. float3 snappedProbePosition_WS; // worldspace position of main probe (a corner of the 8 probes cube)
  212. float3 samplingPositionNoAntiLeak_WS; // worldspace sampling position after applying 'NormalBias', 'ViewBias'
  213. float probeDistance;
  214. float3 normalizedOffset; // normalized offset between sampling position and snappedProbePosition
  215. float validityWeights[8];
  216. float validityWeight = 1.0f;
  217. FindSamplingData(debugPosition.xyz, debugNormal.xyz, _RenderingLayerMask, snappedProbePosition_WS, samplingPositionNoAntiLeak_WS, probeDistance, normalizedOffset, validityWeights);
  218. // QUADS to write the sampling factor of each probe
  219. // each QUAD has an individual ID in vertex color blue channel
  220. if (v.color.z)
  221. {
  222. // QUAD 01
  223. float3 quadPosition = snappedProbePosition_WS;
  224. validityWeight = validityWeights[0];
  225. // QUAD 02
  226. if (abs(v.color.z - 0.2f) < 0.02f)
  227. {
  228. quadPosition = snappedProbePosition_WS + float3(0.0f, 1.0f, 0.0f) * probeDistance;
  229. validityWeight = validityWeights[2];
  230. }
  231. // QUAD 03
  232. if (abs(v.color.z - 0.3f) < 0.02f)
  233. {
  234. quadPosition = snappedProbePosition_WS + float3(1.0f, 1.0f, 0.0f) * probeDistance;
  235. validityWeight = validityWeights[3];
  236. }
  237. // QUAD 04
  238. if (abs(v.color.z - 0.4f) < 0.02f)
  239. {
  240. quadPosition = snappedProbePosition_WS + float3(1.0f, 0.0f, 0.0f) * probeDistance;
  241. validityWeight = validityWeights[1];
  242. }
  243. // QUAD 05
  244. if (abs(v.color.z - 0.5f) < 0.02f)
  245. {
  246. quadPosition = snappedProbePosition_WS + float3(0.0f, 0.0f, 1.0f) * probeDistance;
  247. validityWeight = validityWeights[4];
  248. }
  249. // QUAD 06
  250. if (abs(v.color.z - 0.6f) < 0.02f)
  251. {
  252. quadPosition = snappedProbePosition_WS + float3(0.0f, 1.0f, 1.0f) * probeDistance;
  253. validityWeight = validityWeights[6];
  254. }
  255. // QUAD 07
  256. if (abs(v.color.z - 0.7f) < 0.02f)
  257. {
  258. quadPosition = snappedProbePosition_WS + float3(1.0f, 1.0f, 1.0f) * probeDistance;
  259. validityWeight = validityWeights[7];
  260. }
  261. // QUAD 08
  262. if (abs(v.color.z - 0.8f) < 0.02f)
  263. {
  264. quadPosition = snappedProbePosition_WS + float3(1.0f, 0.0f, 1.0f) * probeDistance;
  265. validityWeight = validityWeights[5];
  266. }
  267. if (_APVLeakReductionMode == APVLEAKREDUCTIONMODE_QUALITY)
  268. samplingFactor = validityWeight; // this is not 100% accurate in some cases (cause we do max 3 samples)
  269. else
  270. samplingFactor = ComputeSamplingFactor(quadPosition, snappedProbePosition_WS, normalizedOffset, probeDistance);
  271. float4 cameraUp = mul(UNITY_MATRIX_I_V, float4(0.0f, 1.0f, 0.0f, 0.0f));
  272. float4 cameraRight = -mul(UNITY_MATRIX_I_V, float4(1.0f, 0.0f, 0.0f, 0.0f));
  273. wsPos = mul(UNITY_MATRIX_M, float4(0.0f, 0.0f, 0.0f, 1.0f));
  274. wsPos += float4(quadPosition + cameraUp.xyz * _ProbeSize / 1.5f, 0.0f);
  275. wsPos += float4((v.vertex.x * cameraRight.xyz + v.vertex.y * cameraUp.xyz * 0.5f) * 20.0f * _ProbeSize, 0.0f);
  276. }
  277. // ARROW to show the position and normal of the debugged fragment
  278. else if (v.color.y)
  279. {
  280. float3 forward = normalize(debugNormal.xyz);
  281. float3 up = float3(0.0f, 1.0f, 0.0f); if (dot(up, forward) > 0.9f) { up = float3(1.0f, 0.0f, 0.0f); }
  282. float3 right = normalize(cross(forward, up));
  283. up = cross(right, forward);
  284. float3x3 orientation = float3x3(
  285. right.x, up.x, forward.x,
  286. right.y, up.y, forward.y,
  287. right.z, up.z, forward.z);
  288. wsPos = float4(mul(orientation, (v.vertex.xyz * _ProbeSize * 5.0f)), 1.0f);
  289. wsPos = mul(UNITY_MATRIX_M, wsPos);
  290. wsPos.xyz += debugPosition.xyz;
  291. }
  292. // LOCATOR to debug sampling position
  293. else
  294. {
  295. if (v.color.x) // DEBUG NORMAL + VIEW BIAS
  296. {
  297. if (_ForceDebugNormalViewBias)
  298. {
  299. wsPos = mul(UNITY_MATRIX_M, float4(v.vertex.xyz * _ProbeSize * 1.5f, 1.0f));
  300. wsPos += float4(samplingPositionNoAntiLeak_WS, 0.0f);
  301. }
  302. else
  303. {
  304. DoCull(o);
  305. return o;
  306. }
  307. }
  308. else // DEBUG NORMAL + VIEW BIAS + ANTI LEAK
  309. {
  310. wsPos = mul(UNITY_MATRIX_M, float4(v.vertex.xyz * _ProbeSize * 3.0f, 1.0f));
  311. wsPos += float4(snappedProbePosition_WS + normalizedOffset * probeDistance, 0.0f);
  312. }
  313. }
  314. float4 pos = mul(UNITY_MATRIX_VP, wsPos);
  315. float remappedDepth = Remap(-1.0f, 1.0f, 0.6f, 1.0f, pos.z); // remapped depth to draw gizmo on top of most other objects
  316. o.vertex = float4(pos.x, pos.y, remappedDepth * pos.w, pos.w);
  317. o.normal = normalize(mul(v.normal, (float3x3)UNITY_MATRIX_M));
  318. o.color = v.color;
  319. o.texCoord = v.texCoord;
  320. o.samplingFactor_ValidityWeight = float2(samplingFactor, validityWeight);
  321. return o;
  322. }
  323. float4 frag(v2f i) : SV_Target
  324. {
  325. // QUADS to write the sampling factor of each probe
  326. if (i.color.z)
  327. {
  328. float samplingFactor = i.samplingFactor_ValidityWeight.x;
  329. float validityWeight = i.samplingFactor_ValidityWeight.y;
  330. half4 color = WriteFractNumber(samplingFactor, i.texCoord);
  331. if (validityWeight > 0.0f)
  332. color = lerp(half4(0.0f, 0.0f, 0.0f, 1.0f), half4(0.0f, 1.0f, 0.0f, 1.0f), color.x);
  333. else
  334. color = lerp(half4(1.0f, 1.0f, 1.0f, 1.0f), half4(1.0f, 0.0f, 0.0f, 1.0f), color.x);
  335. return color;
  336. }
  337. // ARROW to show debugging position and normal
  338. else if (i.color.y)
  339. {
  340. return _DebugArrowColor;
  341. }
  342. // LOCATOR to debug sampling position
  343. else
  344. {
  345. if (i.color.x) // DEBUG NORMAL + VIEW BIAS
  346. return _DebugLocator02Color;
  347. else // DEBUG NORMAL + VIEW BIAS + ANTILEAK MODE
  348. return _DebugLocator01Color;
  349. }
  350. }
  351. #endif
  352. #endif // PROBEVOLUMEDEBUG_FUNCTIONS_HLSL