Obsidian: [[11-01-01-GLSL]] [[09-01-01-U_Shaders]] [[15-01-01-Shading]]
avoiding conditionals
http://theorangeduck.com/page/avoiding-shader-conditionals
https://docs.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl-pguide
Intrinsic Data types
Buffer - Buffer, which contains one or more scalars
Scalar - One-component scalar
Vector, Matrix - Multiple-component vector or matrix
Sampler, Shader, Texture - Sampler, shader, or texture object
Struct, User Defined - Custom structure or typedef
Variables
bool true or false
int 32-bi integer - int Foo[3]; / int Foo[3] = {1,2,3};
half 16bit integer
float 32bit float - float Foo = 3.1f;
double 64bit double
float3 vectorTest
float vectorTest[3]
vector vectorTest
float2 vectorTest
bool3 vectorTest
float3x3: a 3×3 matrix, type float
float2x2: a 2×2 matrix, type float
.
float4 color;
uniform float4 position : SV_POSITION;
const float4 lightDirection = {0,0,1};
discard; Do not output the result of the current pixel.
http://www.iquilezles.org/www/articles/distfunctions/distfunctions.htm - SDF & HLSL
Key Words
return [value];
continue; - Stop loop (do, for, while) / update the loop conditions / begin executing from the top of the loop
discard; - Do not output the result of the current pixel
break; - Exit the surrounding loop (do, for, while).
float Scale;
float4 main(in float2 uv: TEXCOORD0): SV_TARGET
{
float x = sin(uv.x*Scale);
return float4(0.4,x,0.5,1.0);
}
https://www.bouncepatch.com/hlsl.html - hlsl tuts
https://www.ronja-tutorials.com/2018/03/20/hlsl-basics.html - hlsl tuts
http://www.catalinzima.com/xna/tutorials/crash-course-in-hlsl/ - hlsl ala wiki
http://rbwhitaker.wikidot.com/hlsl-tutorials - hlsl xna
http://www.alanzucconi.com/2016/07/01/volumetric-rendering/ - volume implementation hlsl
pos_object
pos_world
pos_camera
pos_clip (projective space)
Loop
[Attribute] do { Statement Block; } while( Conditional );
- Execute a series of statements continuously until the conditional expression fails. For statement. Iteratively executes a series of statements, based on the evaluation of the conditional expression.
[Attribute] while ( Conditional ) { Statement Block; }
- while
Conditioning
[Attribute] if ( Conditional ) { Statement Block; }
Unreal Overlay blend of: Base and Blend inputs:
return Base < 0.5f ? (2.0 * Base * Blend) : (1.0f - 2.0f * (1.0 - Base) * (1.0f - Blend));
Switch
[Attribute] switch( Selector ) { case 0 : { StatementBlock; } break; case 1 : { StatementBlock; } break; case n : { StatementBlock; } break; default : { StatementBlock; } break;
Fn
intrinsic functions list: https://docs.microsoft.com/en-us/windows/desktop/direct3dhlsl/dx-graphics-hlsl-intrinsic-functions
HLSL-Spherical-Harmonics
Vertex Shader Semantics:
BINORMAL[n] float4 - Binormal
BLENDINDICES[n] uint - Blend indices
BLENDWEIGHT[n] float - Blend weights
COLOR[n] float4 - Diffuse and specular color / Diffuse or specular color
NORMAL[n] float4 - Normal vector
POSITION[n] float4 - Vertex position in object space
POSITIONT float4 - Transformed vertex position
PSIZE[n] float - Point size
TANGENT[n] float4 - Tangent
TEXCOORD[n] float4 - Texture coordinates
Output:
POSITION[n] float4 - Vertex position in homogenous space. Compute position in screen-space by dividing (x,y,z) by w
FOG float - Vertex fog
PSIZEfloat - Point size
TESSFACTOR[n] float - Tessellation factor
TEXCOORD[n] float4 - Texture coordinates
Pixel Shader Semantics:
COLOR[n] float4 - Diffuse or specular color
TEXCOORD[n] float4 - Texture coordinates
SV_IsFrontFace float - Floating-point scalar that indicates a back-facing primitive. A negative value faces backwards
SV_Position float2 - The pixel location (x,y) in screen space
Output:
SV_Depth, COLOR[n] float4 - Output color
SV_Target, DEPTH[n] float - Output depth
DirectX10 Semantics:
SV_POSITION, which is interpreted by the rasterizer stage can be specified as an input to a vertex shader as well as an output Pixel shaders can only write to parameters with the SV_Depth and SV_Target system-value semantics
SV_VertexID, SV_InstanceID, SV_IsFrontFace can only be input into the first active shader
SV_Target[n], where 0 <= n <= 7 The output value that will be stored in a render target. available to all shaders
SV_Depth
UE Examples in Material Editor
float3 foo = InputName;
returen foo;
Exp fn.
Input_1, exp
float ip1 = Input_1;
float S = float3(pow(ip1,exp));
return S;
Lerp
float3 ip1 = input_1;
float3 ip2 = input_2;
float3 ret = float3(lerp(ip1,ip2,ips));
return ret;
Overlay
In: Base, Blend
return Base < 0.5f ? (2.0 * Base * Blend) : (1.0f - 2.0f * (1.0 - Base) * (1.0f - Blend));
Blur (UE docs)
Tex, UV, r, dist
float3 blur = Texture2DSample(Tex, TexSampler, UV);
for (int i = 0; i < r; i++)
{
blur += Texture2DSample(Tex, TexSampler, UV + float2(i * dist, 0));
blur += Texture2DSample(Tex, TexSampler, UV - float2(i * dist, 0));
}
for (int j = 0; j < r; j++)
{
blur += Texture2DSample(Tex, TexSampler, UV + float2(0, j * dist));
blur += Texture2DSample(Tex, TexSampler, UV - float2(0, j * dist));
}
blur /= 2*(2*r)+1;
return blur;
https://forums.unrealengine.com/development-discussion/rendering/1409859-custom-hlsl-tips
https://docs.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl-intrinsic-functions fn list
https://api.unrealengine.com/INT/API/Runtime/Engine/FViewUniformShaderParameters/index.html
https://docs.unrealengine.com/en-US/Programming/Rendering/ShaderDevelopment/HLSLCrossCompiler/index.html
https://polydna.blogspot.com/2020/01/working-with-hlsl-in-unreal.html
Get dimensions (of tex)
unit3 dimension;
Tex.GetDimensions(0,dimension.x,dimension.y,dimension.z);
return float2(dimension.xy);
Shader structure
// Inputs
float4x4 object_to_world: WORLD;
float4x4 object_to_clip: WORLDVIEWPROJECTION;
float3 light_pos: LIGHT_POS;
float3 light_color: LIGHT_COLOR;
float3x3 object_to_world3x3 = (float3x3)object_to_world;
// Structures
struct vs_in {
float4 pos_object: POSITION;
float3 normal_object: NORMAL;
};
struct ps_in {
float4 pos_clip: POSITION;
float3 normal_world: TEXCOORD0;
float3 light_world: TEXCOORD1;
};
// Vertex Shaders
ps_in vs_main(vs_in input) {
ps_in output;
output.pos_clip = mul(input.pos_object, object_to_clip);
output.normal_world =
mul(input.normal_object, object_to_world3x3);
float4 pos_world = mul(input.pos_object, object_to_world);
output.light_world = light_pos - pos_world.xyz;
return output;
}
// Pixel Shaders
float4 ps_main(ps_in input) : COLOR {
float3 result = light_world;
float3 normal_world = normalize(input.normal_world);
float3 light_world = normalize(input.light_world);
result *= saturate(dot(normal_world, light_world));
return float4(result, 1.f);
}
// Techniques
technique main {
pass p0 {
VertexShader = compile vs_3_0 vs_main();
PixelShader = compile ps_3_0 ps_main();
}
}
http://shaderjvo.blogspot.com/2011/08/introduction-to-hlsl-part-3.html
Compute shaders
https://medium.com/realities-io/using-compute-shaders-in-unreal-engine-4-f64bac65a907 https://catlikecoding.com/unity/tutorials/basics/compute-shaders/ https://www.youtube.com/watch?v=aKo0ESuVrgs&list=PL78XDi0TS4lEMvytsE_MoWEpzBcukXv9b&index=4