Debug.hlsl 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. #ifndef UNITY_DEBUG_INCLUDED
  2. #define UNITY_DEBUG_INCLUDED
  3. #define TRANSPARENCY_OVERDRAW_COST 1.0
  4. #define TRANSPARENCY_OVERDRAW_A 1.0
  5. // Given an enum (represented by an int here), return a color.
  6. // Use for DebugView of enum
  7. real3 GetIndexColor(int index)
  8. {
  9. real3 outColor = real3(1.0, 0.0, 0.0);
  10. if (index == 0)
  11. outColor = real3(1.0, 0.5, 0.5);
  12. else if (index == 1)
  13. outColor = real3(0.5, 1.0, 0.5);
  14. else if (index == 2)
  15. outColor = real3(0.5, 0.5, 1.0);
  16. else if (index == 3)
  17. outColor = real3(1.0, 1.0, 0.5);
  18. else if (index == 4)
  19. outColor = real3(1.0, 0.5, 1.0);
  20. else if (index == 5)
  21. outColor = real3(0.5, 1.0, 1.0);
  22. else if (index == 6)
  23. outColor = real3(0.25, 0.75, 1.0);
  24. else if (index == 7)
  25. outColor = real3(1.0, 0.75, 0.25);
  26. else if (index == 8)
  27. outColor = real3(0.75, 1.0, 0.25);
  28. else if (index == 9)
  29. outColor = real3(0.75, 0.25, 1.0);
  30. else if (index == 10)
  31. outColor = real3(0.25, 1.0, 0.75);
  32. else if (index == 11)
  33. outColor = real3(0.75, 0.75, 0.25);
  34. else if (index == 12)
  35. outColor = real3(0.75, 0.25, 0.75);
  36. else if (index == 13)
  37. outColor = real3(0.25, 0.75, 0.75);
  38. else if (index == 14)
  39. outColor = real3(0.25, 0.25, 0.75);
  40. else if (index == 15)
  41. outColor = real3(0.75, 0.25, 0.25);
  42. return outColor;
  43. }
  44. bool SampleDebugFont(int2 pixCoord, uint digit)
  45. {
  46. if (pixCoord.x < 0 || pixCoord.y < 0 || pixCoord.x >= 5 || pixCoord.y >= 9 || digit > 9)
  47. return false;
  48. #define PACK_BITS25(_x0,_x1,_x2,_x3,_x4,_x5,_x6,_x7,_x8,_x9,_x10,_x11,_x12,_x13,_x14,_x15,_x16,_x17,_x18,_x19,_x20,_x21,_x22,_x23,_x24) (_x0|(_x1<<1)|(_x2<<2)|(_x3<<3)|(_x4<<4)|(_x5<<5)|(_x6<<6)|(_x7<<7)|(_x8<<8)|(_x9<<9)|(_x10<<10)|(_x11<<11)|(_x12<<12)|(_x13<<13)|(_x14<<14)|(_x15<<15)|(_x16<<16)|(_x17<<17)|(_x18<<18)|(_x19<<19)|(_x20<<20)|(_x21<<21)|(_x22<<22)|(_x23<<23)|(_x24<<24))
  49. #define _ 0
  50. #define x 1
  51. uint fontData[9][2] = {
  52. { PACK_BITS25(_,_,x,_,_, _,_,x,_,_, _,x,x,x,_, x,x,x,x,x, _,_,_,x,_), PACK_BITS25(x,x,x,x,x, _,x,x,x,_, x,x,x,x,x, _,x,x,x,_, _,x,x,x,_) },
  53. { PACK_BITS25(_,x,_,x,_, _,x,x,_,_, x,_,_,_,x, _,_,_,_,x, _,_,_,x,_), PACK_BITS25(x,_,_,_,_, x,_,_,_,x, _,_,_,_,x, x,_,_,_,x, x,_,_,_,x) },
  54. { PACK_BITS25(x,_,_,_,x, x,_,x,_,_, x,_,_,_,x, _,_,_,x,_, _,_,x,x,_), PACK_BITS25(x,_,_,_,_, x,_,_,_,_, _,_,_,x,_, x,_,_,_,x, x,_,_,_,x) },
  55. { PACK_BITS25(x,_,_,_,x, _,_,x,_,_, _,_,_,_,x, _,_,x,_,_, _,x,_,x,_), PACK_BITS25(x,_,x,x,_, x,_,_,_,_, _,_,_,x,_, x,_,_,_,x, x,_,_,_,x) },
  56. { PACK_BITS25(x,_,_,_,x, _,_,x,_,_, _,_,_,x,_, _,x,x,x,_, _,x,_,x,_), PACK_BITS25(x,x,_,_,x, x,x,x,x,_, _,_,x,_,_, _,x,x,x,_, _,x,x,x,x) },
  57. { PACK_BITS25(x,_,_,_,x, _,_,x,_,_, _,_,x,_,_, _,_,_,_,x, x,_,_,x,_), PACK_BITS25(_,_,_,_,x, x,_,_,_,x, _,_,x,_,_, x,_,_,_,x, _,_,_,_,x) },
  58. { PACK_BITS25(x,_,_,_,x, _,_,x,_,_, _,x,_,_,_, _,_,_,_,x, x,x,x,x,x), PACK_BITS25(_,_,_,_,x, x,_,_,_,x, _,x,_,_,_, x,_,_,_,x, _,_,_,_,x) },
  59. { PACK_BITS25(_,x,_,x,_, _,_,x,_,_, x,_,_,_,_, x,_,_,_,x, _,_,_,x,_), PACK_BITS25(x,_,_,_,x, x,_,_,_,x, _,x,_,_,_, x,_,_,_,x, x,_,_,_,x) },
  60. { PACK_BITS25(_,_,x,_,_, x,x,x,x,x, x,x,x,x,x, _,x,x,x,_, _,_,_,x,_), PACK_BITS25(_,x,x,x,_, _,x,x,x,_, _,x,_,_,_, _,x,x,x,_, _,x,x,x,_) }
  61. };
  62. #undef _
  63. #undef x
  64. #undef PACK_BITS25
  65. return (fontData[8 - pixCoord.y][digit >= 5] >> ((digit % 5) * 5 + pixCoord.x)) & 1;
  66. }
  67. bool SampleDebugFontNumber(int2 pixCoord, uint number)
  68. {
  69. pixCoord.y -= 4;
  70. if (number <= 9)
  71. {
  72. return SampleDebugFont(pixCoord - int2(6, 0), number);
  73. }
  74. else
  75. {
  76. return (SampleDebugFont(pixCoord, number / 10) | SampleDebugFont(pixCoord - int2(6, 0), number % 10));
  77. }
  78. }
  79. float4 GetStreamingMipColor(uint mipCount, float4 mipInfo)
  80. {
  81. // alpha is amount to blend with source color (0.0 = use original, 1.0 = use new color)
  82. // mipInfo :
  83. // x = quality setings minStreamingMipLevel
  84. // y = original mip count for texture
  85. // z = desired on screen mip level
  86. // w = 0
  87. uint originalTextureMipCount = uint(mipInfo.y);
  88. // If material/shader mip info (original mip level) has not been set its not a streamed texture
  89. if (originalTextureMipCount == 0)
  90. return float4(1.0, 1.0, 1.0, 0.0);
  91. uint desiredMipLevel = uint(mipInfo.z);
  92. uint mipCountDesired = uint(originalTextureMipCount)-uint(desiredMipLevel);
  93. if (mipCount == 0)
  94. {
  95. // Magenta if mip count invalid
  96. return float4(1.0, 0.0, 1.0, 1.0);
  97. }
  98. else if (mipCount < mipCountDesired)
  99. {
  100. // red tones when not at the desired mip level (reduction due to budget). Brighter is further from original, alpha 0 when at desired
  101. float ratioToDesired = float(mipCount) / float(mipCountDesired);
  102. return float4(1.0, 0.0, 0.0, 1.0 - ratioToDesired);
  103. }
  104. else if (mipCount >= originalTextureMipCount)
  105. {
  106. // original color when at (or beyond) original mip count
  107. return float4(1.0, 1.0, 1.0, 0.0);
  108. }
  109. else
  110. {
  111. // green tones when not at the original mip level. Brighter is closer to original, alpha 0 when at original
  112. float ratioToOriginal = float(mipCount) / float(originalTextureMipCount);
  113. return float4(0.0, 1.0, 0.0, 1.0 - ratioToOriginal);
  114. }
  115. }
  116. float4 GetSimpleMipCountColor(uint mipCount)
  117. {
  118. // Grey scale for mip counts where mip count of 14 = white
  119. float mipCountColor = float(mipCount) / 14.0;
  120. float4 color = float4(mipCountColor, mipCountColor, mipCountColor, 1.0f);
  121. // alpha is amount to blend with source color (0.0 = use original, 1.0 = use new color)
  122. // Magenta is no valid mip count
  123. // Red if greater than 14
  124. return mipCount==0 ? float4(1.0, 0.0, 1.0, 1.0) : (mipCount > 14 ? float4(1.0, 0.0, 0.0, 1.0) : color );
  125. }
  126. float4 GetMipLevelColor(float2 uv, float4 texelSize)
  127. {
  128. // Push down into colors list to "optimal level" in following table.
  129. // .zw is texture width,height so *2 is down one mip, *4 is down two mips
  130. texelSize.zw *= 4.0;
  131. float mipLevel = ComputeTextureLOD(uv, texelSize.wz);
  132. mipLevel = clamp(mipLevel, 0.0, 5.0 - 0.0001);
  133. float4 colors[6] = {
  134. float4(0.0, 0.0, 1.0, 0.8), // 0 BLUE = too little texture detail
  135. float4(0.0, 0.5, 1.0, 0.4), // 1
  136. float4(1.0, 1.0, 1.0, 0.0), // 2 = optimal level
  137. float4(1.0, 0.7, 0.0, 0.2), // 3 (YELLOW tint)
  138. float4(1.0, 0.3, 0.0, 0.6), // 4 (clamped mipLevel 4.9999)
  139. float4(1.0, 0.0, 0.0, 0.8) // 5 RED = too much texture detail (max blended value)
  140. };
  141. int mipLevelInt = floor(mipLevel);
  142. float t = frac(mipLevel);
  143. float4 a = colors[mipLevelInt];
  144. float4 b = colors[mipLevelInt + 1];
  145. float4 color = lerp(a, b, t);
  146. return color;
  147. }
  148. float3 GetDebugMipColor(float3 originalColor, Texture2D tex, float4 texelSize, float2 uv)
  149. {
  150. // https://aras-p.info/blog/2011/05/03/a-way-to-visualize-mip-levels/
  151. float4 mipColor= GetMipLevelColor(uv, texelSize);
  152. return lerp(originalColor, mipColor.rgb, mipColor.a);
  153. }
  154. float3 GetDebugMipCountColor(float3 originalColor, Texture2D tex)
  155. {
  156. uint mipCount = GetMipCount(tex);
  157. float4 mipColor = GetSimpleMipCountColor(mipCount);
  158. return lerp(originalColor, mipColor.rgb, mipColor.a);
  159. }
  160. float3 GetDebugStreamingMipColor(Texture2D tex, float4 mipInfo)
  161. {
  162. uint mipCount = GetMipCount(tex);
  163. return GetStreamingMipColor(mipCount, mipInfo).xyz;
  164. }
  165. float3 GetDebugStreamingMipColorBlended(float3 originalColor, Texture2D tex, float4 mipInfo)
  166. {
  167. uint mipCount = GetMipCount(tex);
  168. float4 mipColor = GetStreamingMipColor(mipCount, mipInfo);
  169. return lerp(originalColor, mipColor.rgb, mipColor.a);
  170. }
  171. float3 GetDebugMipColorIncludingMipReduction(float3 originalColor, Texture2D tex, float4 texelSize, float2 uv, float4 mipInfo)
  172. {
  173. uint originalTextureMipCount = uint(mipInfo.y);
  174. if (originalTextureMipCount != 0)
  175. {
  176. // mipInfo :
  177. // x = quality setings minStreamingMipLevel
  178. // y = original mip count for texture
  179. // z = desired on screen mip level
  180. // w = 0
  181. // Mip count has been reduced but the texelSize was not updated to take that into account
  182. uint mipCount = GetMipCount(tex);
  183. uint mipReductionLevel = originalTextureMipCount - mipCount;
  184. uint mipReductionFactor = 1 << mipReductionLevel;
  185. if (mipReductionFactor)
  186. {
  187. float oneOverMipReductionFactor = 1.0 / mipReductionFactor;
  188. // texelSize.xy *= mipReductionRatio; // Unused in GetDebugMipColor so lets not re-calculate it
  189. texelSize.zw *= oneOverMipReductionFactor;
  190. }
  191. }
  192. return GetDebugMipColor(originalColor, tex, texelSize, uv);
  193. }
  194. // mipInfo :
  195. // x = quality setings minStreamingMipLevel
  196. // y = original mip count for texture
  197. // z = desired on screen mip level
  198. // w = 0
  199. float3 GetDebugMipReductionColor(Texture2D tex, float4 mipInfo)
  200. {
  201. float3 outColor = float3(1.0, 0.0, 1.0); // Can't calculate without original mip count - return magenta
  202. uint originalTextureMipCount = uint(mipInfo.y);
  203. if (originalTextureMipCount != 0)
  204. {
  205. // Mip count has been reduced but the texelSize was not updated to take that into account
  206. uint mipCount = GetMipCount(tex);
  207. uint mipReductionLevel = originalTextureMipCount - mipCount;
  208. float mipCol = float(mipReductionLevel) / 14.0;
  209. outColor = float3(0, mipCol, 0);
  210. }
  211. return outColor;
  212. }
  213. // Convert an arbitrary range to color base on threshold provide to the function, threshold must be in growing order
  214. real3 GetColorCodeFunction(real value, real4 threshold)
  215. {
  216. const real3 red = { 1.0, 0.0, 0.0 };
  217. const real3 lightGreen = { 0.5, 1.0, 0.5 };
  218. const real3 darkGreen = { 0.1, 1.0, 0.1 };
  219. const real3 yellow = { 1.0, 1.0, 0.0 };
  220. real3 outColor = red;
  221. if (value < threshold[0])
  222. {
  223. outColor = red;
  224. }
  225. else if (value >= threshold[0] && value < threshold[1])
  226. {
  227. real scale = (value - threshold[0]) / (threshold[1] - threshold[0]);
  228. outColor = lerp(red, darkGreen, scale);
  229. }
  230. else if (value >= threshold[1] && value < threshold[2])
  231. {
  232. real scale = (value - threshold[1]) / (threshold[2] - threshold[1]);
  233. outColor = lerp(darkGreen, lightGreen, scale);
  234. }
  235. else if (value >= threshold[2] && value < threshold[3])
  236. {
  237. real scale = (value - threshold[2]) / (threshold[2] - threshold[2]);
  238. outColor = lerp(lightGreen, yellow, scale);
  239. }
  240. else
  241. {
  242. outColor = yellow;
  243. }
  244. return outColor;
  245. }
  246. #endif // UNITY_DEBUG_INCLUDED