HLSL Graphing Include File

general download section
Post Reply
  • Author
  • Message
Offline
User avatar
Posts: 99
Joined: 11 May 2014, 10:48

HLSL Graphing Include File

This is a little header file I made for drawing graphs in HLSL, so you or your users can get a better idea of the things that go on in a shader.

Image


Here's a simple example and its result:

Code: Select all

float4 ScreenSize; // boris's external variable

// ..

// include it somewhere after ScreenSize has been defined in your shader
#include "graphing.fx"

// ..

// make sure color.w is set to 1.0 prior to calling graphDraw() for the first time
color.w = 1.0;
// create a new graph
GraphStruct g = graphNew(float2(0, 0), float2(512, 256), v0.xy, float2(16, 8));
// plot a function with g.uv.x as input
float f = sin(g.uv.x * 16.0) * 0.5 + 0.5;
// add the plot to the graph
graphAddPlot(g, f, float3(1.0, 0.0, 0.0));
// draw the graph
graphDraw(g, color);
Image

The basic idea is once you create a new graph, you get its uv x coordinate, and use it as input for the function you want to graph. Then you make sure the result is scaled to a 0-1 range, and you add the plot to the graph with the graphAddPlot() function.


Here's a more complex example of actual usage with comments walking you through the process:

Code: Select all

// before the first time you call graphDraw(), make sure color.w is set to 1,
// and make sure color.w is untouched inbetween calls of graphDraw()
// this is necessary for correct drawing of drop shadows
color.w = 1.0;

// the usage of graphNew is:
// GraphStruct graphNew(float2 position, float2 size, float2 pixel_coordinate, float2 lines)
// note: the position, size, as well as coordinate input is in pixels, get pixel coordinates from SV_Position0 for DX11 or VPOS for DX9:
// float4 PS_Draw(VS_OUTPUT_POST IN, float4 v0 : SV_Position0) : SV_Target
// float4 PS_Draw(VS_OUTPUT_POST IN, float2 vPos : VPOS) : COLOR
GraphStruct g = graphNew(graphPosition, graphSize, v0.xy, float2(8, 4));

// a GraphStruct contains:

// --- treat these parameters as read-only ---
// float4 area: area.xy is position, area.zw is size
// float2 co: pixel coordinate
// float2 uv: uv coordinates of the graph (0-1 range)
// float2 lines: the number of lines to divide the background into, horizontally and vertically
// float4 color: sum of plotted functions, don't modify this directly. use graphAddPlot()

// --- modify these parameters directly for control over the graph ---
// float4 background_color: background color
// float roundness: roundedness of the corners of the graph
// float drop_shadow: intensity of the drop shadow
// float drop_shadow_radius: radius of the drop shadow
// float3 lines_x_color: color of horizontal background lines
// float3 lines_y_color: color of vertical background lines

// to plot a function, call that function using g.uv.x as the input
// in this case, this function for my split tone effect which returns
// a float3, we will add a plot for each component.
float3 f = splitRanges(g.uv.x, Lo, Hi);

// I want the graph's background to turn bright red showing areas where the sum
// of the weights does not add up to 1, so I will use this variable to do that
float err = abs(1.0 - (f.x+f.y+f.z));

// graph colors
float3 color_x, color_y, color_z;
if (VisualizeSplitTone == 2)
{
    color_x = float3(1.0, 0.0, 0.0);
    color_y = float3(0.0, 1.0, 0.0);
    color_z = float3(0.0, 0.0, 1.0);
}
else
{
    color_x = lerp(1.0, LowTone,  SplitToneAmount * LowSaturation);
    color_y = lerp(1.0, MidTone,  SplitToneAmount * MidSaturation);
    color_z = lerp(1.0, HighTone, SplitToneAmount * HighSaturation);

    // g.background_color.a controls alpha
    g.background_color.a = 0.9;
}

// the usage of graphAddPlot is:
// void graphAddPlot(inout GraphStruct g, float plotted_function, [float3/float4 color], [float line_thickness])
// you can add as many plots to a graph as you like
graphAddPlot(g, f.x, color_x);
graphAddPlot(g, f.y, color_y);
graphAddPlot(g, f.z, color_z);

// color background
g.background_color.rgb = (color_x*f.x + color_y*f.y + color_z*f.z) * 0.25;
g.background_color = lerp(g.background_color, float4(1,0,0,1), err);

// drop shadow strength
g.drop_shadow = 0.9;

// roundedness of corners
g.roundness = 10.0;

// draw the graph
// usage: void graphDraw(GraphStruct g, inout float4 color)
graphDraw(g, color);
Image
Attachments
graphing.fx
1.1 update:
* added anti-aliased, correct line drawing courtesy of Marty McFly.
* added transparency, drop shadow, and rounded corner options
(3.84 KiB) Downloaded 313 times
_________________
Reforged ENB for Dragon's Dogma, The Witcher 2, Kingdom Come: Deliverance
Post Reply