浏览代码

Update font debug code, support char, int and float

/main
Sebastien Lagarde 7 年前
当前提交
2549f4ea
共有 4 个文件被更改,包括 86 次插入55 次删除
  1. 109
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Debug/DebugDisplay.hlsl
  2. 29
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Debug/DebugFullScreen.shader
  3. 2
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/RenderPipelineResources/DebugFont.tga.meta
  4. 1
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/ShaderVariables.hlsl

109
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Debug/DebugDisplay.hlsl


return originalColor;
}
// font texture is 256x128
// DebugFont code assume black and white font with texture size 256x128 with bloc of 16x16
#define DEBUG_FONT_TEXT_SIZE 1.0f
#define DEBUG_FONT_TEXT_SCALE 0.7f
#define DEBUG_FONT_TEXT_ASCII_START 32
float3 DrawCharacter(uint asciiValue, float2 currentAbsCoords, inout float2 referenceAbsCoord, float fontSize, float incrementSign)
// Only support ASCII symbol from DEBUG_FONT_TEXT_ASCII_START to 126
// return black or white depends if we hit font character or not
// currentUnormCoord is current unormalized screen position
// fixedUnormCoord is the position where we want to draw something, this will be incremented by block font size in provided direction
// color is current screen color
// color of the font to use
// direction is 1 or -1 and indicate fixedUnormCoord block shift
void DrawCharacter(uint asciiValue, float3 fontColor, uint2 currentUnormCoord, inout uint2 fixedUnormCoord, inout float3 color, int direction)
uint2 asciiCoord = uint2(asciiValue % DEBUG_FONT_TEXT_COUNT_X, asciiValue / DEBUG_FONT_TEXT_COUNT_Y);
const float charWidth = DEBUG_FONT_TEXT_WIDTH * fontSize;
const float charHeight = DEBUG_FONT_TEXT_HEIGHT * fontSize;
const float2 localCoords = currentAbsCoords - referenceAbsCoord;
// Are we inside a font display block on the screen ?
uint2 localCharCoord = currentUnormCoord - fixedUnormCoord;
if (localCharCoord.x >= 0 && localCharCoord.x < DEBUG_FONT_TEXT_WIDTH && localCharCoord.y >= 0 && localCharCoord.y < DEBUG_FONT_TEXT_HEIGHT)
{
#if UNITY_UV_STARTS_AT_TOP
localCharCoord.y = DEBUG_FONT_TEXT_HEIGHT - localCharCoord.y;
#endif
asciiValue -= DEBUG_FONT_TEXT_ASCII_START; // Our font start at ASCII table 32;
uint2 asciiCoord = uint2(asciiValue % DEBUG_FONT_TEXT_COUNT_X, asciiValue / DEBUG_FONT_TEXT_COUNT_X);
// Unorm coordinate inside the font texture
uint2 unormTexCoord = asciiCoord * uint2(DEBUG_FONT_TEXT_WIDTH, DEBUG_FONT_TEXT_HEIGHT) + localCharCoord;
// normalized coordinate
float2 normTexCoord = float2(unormTexCoord) / float2(DEBUG_FONT_TEXT_WIDTH * DEBUG_FONT_TEXT_COUNT_X, DEBUG_FONT_TEXT_HEIGHT * DEBUG_FONT_TEXT_COUNT_Y);
float3 output = float3(0, 0, 0);
if (localCoords.x >= 0 && localCoords.x < charWidth && localCoords.y >= 0 && localCoords.y < charHeight)
{
float2 texOffset = float2(asciiCoord) / float2(DEBUG_FONT_TEXT_COUNT_X, DEBUG_FONT_TEXT_COUNT_Y);
float2 texCoord = localCoords / float2(charWidth * DEBUG_FONT_TEXT_COUNT_X, charHeight * DEBUG_FONT_TEXT_COUNT_Y);
output = SAMPLE_TEXTURE2D_LOD(_DebugFont, s_linear_clamp_sampler, texCoord + texOffset, 0).xyz;
#if UNITY_UV_STARTS_AT_TOP
normTexCoord.y = 1.0 - normTexCoord.y;
#endif
float charColor = SAMPLE_TEXTURE2D_LOD(_DebugFont, s_point_clamp_sampler, normTexCoord, 0).r;
color = color * (1.0 - charColor) + charColor * fontColor;
referenceAbsCoord.x += charWidth * incrementSign * DEBUG_FONT_TEXT_SCALE;
return output;
fixedUnormCoord.x += DEBUG_FONT_TEXT_WIDTH * direction;
float3 DrawCharacter(uint asciiValue, float2 currentAbsCoords, inout float2 referenceAbsCoord, float fontSize)
// Shortcut to not have to file direction
void DrawCharacter(uint asciiValue, float3 fontColor, uint2 currentUnormCoord, inout uint2 fixedUnormCoord, inout float3 color)
return DrawCharacter(asciiValue, currentAbsCoords, referenceAbsCoord, fontSize, 1.0f);
DrawCharacter(asciiValue, fontColor, currentUnormCoord, fixedUnormCoord, color, 1);
float GetTextSize(uint intValue, float fontSize)
// Draw a signed integer
// Can't display more than 16 digit
void DrawInteger(int intValue, float3 fontColor, uint2 currentUnormCoord, inout uint2 fixedUnormCoord, inout float3 color)
const uint charWidth = DEBUG_FONT_TEXT_WIDTH * fontSize * DEBUG_FONT_TEXT_SCALE;
const uint maxCharCount = 16;
uint charCount = 0;
const uint maxStringSize = 16;
int absIntValue = abs(intValue);
// 1. Get size of the number of display
int numEntries = min((intValue == 0 ? 0 : log10(absIntValue)) + (intValue < 0 ? 1 : 0), maxStringSize);
// 2. Shift curseur to last location as we will go reverse
fixedUnormCoord.x += numEntries * DEBUG_FONT_TEXT_WIDTH;
for (uint charIt = 0; charIt < maxCharCount; ++charIt)
// 3. Display the number
for (uint i = 0; i < maxStringSize; ++i)
++charCount;
if (intValue < 10)
// Numeric value incurrent font start on the second row at 0
DrawCharacter((absIntValue % 10) + '0', fontColor, currentUnormCoord, fixedUnormCoord, color, -1);
if (absIntValue < 10)
intValue /= 10;
absIntValue /= 10;
return charCount * charWidth;
}
float3 DrawInteger(in uint intValue, in float2 coords, inout float2 referenceCoord, in float fontSize)
{
const uint charWidth = DEBUG_FONT_TEXT_WIDTH * fontSize * DEBUG_FONT_TEXT_SCALE;
const uint maxCharCount = 16;
float2 localRefCoord = referenceCoord + float2(GetTextSize(intValue, fontSize), 0);
referenceCoord = localRefCoord + float2(charWidth, 0);
float3 output = float3(0, 0, 0);
for (uint charIt = 0; charIt < maxCharCount; ++charIt)
// 4. Display sign
if (intValue < 0)
output += DrawCharacter((intValue % 10) + 48, coords, localRefCoord, fontSize, -1.0f).xyz;
if (intValue < 10)
break;
intValue /= 10;
DrawCharacter('-', fontColor, currentUnormCoord, fixedUnormCoord, color, -1);
return output;
// 5. Reset cursor at end location
fixedUnormCoord.x += (numEntries + 2) * DEBUG_FONT_TEXT_WIDTH;
}
void DrawFloat(float floatValue, float3 fontColor, uint2 currentUnormCoord, inout uint2 fixedUnormCoord, inout float3 color)
{
int intValue = int(floatValue);
DrawInteger(intValue, fontColor, currentUnormCoord, fixedUnormCoord, color);
DrawCharacter('.', fontColor, currentUnormCoord, fixedUnormCoord, color);
int fracValue = int(frac(floatValue) * 1e6); // 6 digit
DrawInteger(fracValue, fontColor, currentUnormCoord, fixedUnormCoord, color);
}
#endif

29
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Debug/DebugFullScreen.shader


if (_MousePixelCoord.z >= 0.0 && _MousePixelCoord.z <= 1.0 && _MousePixelCoord.w >= 0 && _MousePixelCoord.w <= 1.0)
{
float2 unormTextCoord2d = float2(_MousePixelCoord.x - DEBUG_FONT_TEXT_WIDTH, _MousePixelCoord.y - DEBUG_FONT_TEXT_WIDTH * DEBUG_FONT_TEXT_SIZE);
float2 unormCoord2d = input.positionCS.xy;
// Display message offset:
int displayTextOffsetX = 1.5 * DEBUG_FONT_TEXT_WIDTH;
#if UNITY_UV_STARTS_AT_TOP
int displayTextOffsetY = -DEBUG_FONT_TEXT_HEIGHT;
#else
int displayTextOffsetY = DEBUG_FONT_TEXT_HEIGHT;
#endif
float4 mouseResult = GetResult(input, _MousePixelCoord);
uint2 displayUnormCoord = uint2(_MousePixelCoord.x + displayTextOffsetX, _MousePixelCoord.y + displayTextOffsetY);
uint2 unormCoord = input.positionCS.xy;
float2 textCoord = unormTextCoord2d;
textCoord.x -= GetTextSize(mouseResult.x, DEBUG_FONT_TEXT_SIZE);
float4 mouseResult = GetResult(input, _MousePixelCoord.zw);
result.rgb += DrawInteger(mouseResult.x, unormCoord2d, textCoord, DEBUG_FONT_TEXT_SIZE);
result.rgb += DrawCharacter(' ', unormCoord2d, textCoord, 1.0f);
result.rgb += DrawCharacter('t', unormCoord2d, textCoord, 1.0f);
result.rgb += DrawCharacter('e', unormCoord2d, textCoord, 1.0f);
result.rgb += DrawCharacter('s', unormCoord2d, textCoord, 1.0f);
result.rgb += DrawCharacter('t', unormCoord2d, textCoord, 1.0f);
float3 fontColor = float3(1.0, 0.0, 0.0);
DrawFloat(mouseResult.x, fontColor, unormCoord, displayUnormCoord, result.rgb);
displayUnormCoord.x = _MousePixelCoord.x + displayTextOffsetX;
displayUnormCoord.y += displayTextOffsetY;
DrawFloat(mouseResult.y, fontColor, unormCoord, displayUnormCoord, result.rgb);
displayUnormCoord.x = _MousePixelCoord.x + displayTextOffsetX;
displayUnormCoord.y += displayTextOffsetY;
DrawFloat(mouseResult.z, fontColor, unormCoord, displayUnormCoord, result.rgb);
}
return result;

2
ScriptableRenderPipeline/HDRenderPipeline/HDRP/RenderPipelineResources/DebugFont.tga.meta


spritePivot: {x: 0.5, y: 0.5}
spriteBorder: {x: 0, y: 0, z: 0, w: 0}
spritePixelsToUnits: 100
alphaUsage: 1
alphaUsage: 0
alphaIsTransparency: 0
spriteTessellationDetail: -1
textureType: 0

1
ScriptableRenderPipeline/HDRenderPipeline/HDRP/ShaderVariables.hlsl


// These are the samplers available in the HDRenderPipeline.
// Avoid declaring extra samplers as they are 4x SGPR each on GCN.
SAMPLER(s_point_clamp_sampler);
SAMPLER(s_linear_clamp_sampler);
SAMPLER(s_trilinear_clamp_sampler);

正在加载...
取消
保存